llvm.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {...(RegExp | string) } args
  16. * @returns {string}
  17. */
  18. function concat(...args) {
  19. const joined = args.map((x) => source(x)).join("");
  20. return joined;
  21. }
  22. /*
  23. Language: LLVM IR
  24. Author: Michael Rodler <contact@f0rki.at>
  25. Description: language used as intermediate representation in the LLVM compiler framework
  26. Website: https://llvm.org/docs/LangRef.html
  27. Category: assembler
  28. Audit: 2020
  29. */
  30. /** @type LanguageFn */
  31. function llvm(hljs) {
  32. const IDENT_RE = /([-a-zA-Z$._][\w$.-]*)/;
  33. const TYPE = {
  34. className: 'type',
  35. begin: /\bi\d+(?=\s|\b)/
  36. };
  37. const OPERATOR = {
  38. className: 'operator',
  39. relevance: 0,
  40. begin: /=/
  41. };
  42. const PUNCTUATION = {
  43. className: 'punctuation',
  44. relevance: 0,
  45. begin: /,/
  46. };
  47. const NUMBER = {
  48. className: 'number',
  49. variants: [
  50. { begin: /0[xX][a-fA-F0-9]+/ },
  51. { begin: /-?\d+(?:[.]\d+)?(?:[eE][-+]?\d+(?:[.]\d+)?)?/ }
  52. ],
  53. relevance: 0
  54. };
  55. const LABEL = {
  56. className: 'symbol',
  57. variants: [
  58. { begin: /^\s*[a-z]+:/ }, // labels
  59. ],
  60. relevance: 0
  61. };
  62. const VARIABLE = {
  63. className: 'variable',
  64. variants: [
  65. { begin: concat(/%/, IDENT_RE) },
  66. { begin: /%\d+/ },
  67. { begin: /#\d+/ },
  68. ]
  69. };
  70. const FUNCTION = {
  71. className: 'title',
  72. variants: [
  73. { begin: concat(/@/, IDENT_RE) },
  74. { begin: /@\d+/ },
  75. { begin: concat(/!/, IDENT_RE) },
  76. { begin: concat(/!\d+/, IDENT_RE) },
  77. // https://llvm.org/docs/LangRef.html#namedmetadatastructure
  78. // obviously a single digit can also be used in this fashion
  79. { begin: /!\d+/ }
  80. ]
  81. };
  82. return {
  83. name: 'LLVM IR',
  84. // TODO: split into different categories of keywords
  85. keywords:
  86. 'begin end true false declare define global ' +
  87. 'constant private linker_private internal ' +
  88. 'available_externally linkonce linkonce_odr weak ' +
  89. 'weak_odr appending dllimport dllexport common ' +
  90. 'default hidden protected extern_weak external ' +
  91. 'thread_local zeroinitializer undef null to tail ' +
  92. 'target triple datalayout volatile nuw nsw nnan ' +
  93. 'ninf nsz arcp fast exact inbounds align ' +
  94. 'addrspace section alias module asm sideeffect ' +
  95. 'gc dbg linker_private_weak attributes blockaddress ' +
  96. 'initialexec localdynamic localexec prefix unnamed_addr ' +
  97. 'ccc fastcc coldcc x86_stdcallcc x86_fastcallcc ' +
  98. 'arm_apcscc arm_aapcscc arm_aapcs_vfpcc ptx_device ' +
  99. 'ptx_kernel intel_ocl_bicc msp430_intrcc spir_func ' +
  100. 'spir_kernel x86_64_sysvcc x86_64_win64cc x86_thiscallcc ' +
  101. 'cc c signext zeroext inreg sret nounwind ' +
  102. 'noreturn noalias nocapture byval nest readnone ' +
  103. 'readonly inlinehint noinline alwaysinline optsize ssp ' +
  104. 'sspreq noredzone noimplicitfloat naked builtin cold ' +
  105. 'nobuiltin noduplicate nonlazybind optnone returns_twice ' +
  106. 'sanitize_address sanitize_memory sanitize_thread sspstrong ' +
  107. 'uwtable returned type opaque eq ne slt sgt ' +
  108. 'sle sge ult ugt ule uge oeq one olt ogt ' +
  109. 'ole oge ord uno ueq une x acq_rel acquire ' +
  110. 'alignstack atomic catch cleanup filter inteldialect ' +
  111. 'max min monotonic nand personality release seq_cst ' +
  112. 'singlethread umax umin unordered xchg add fadd ' +
  113. 'sub fsub mul fmul udiv sdiv fdiv urem srem ' +
  114. 'frem shl lshr ashr and or xor icmp fcmp ' +
  115. 'phi call trunc zext sext fptrunc fpext uitofp ' +
  116. 'sitofp fptoui fptosi inttoptr ptrtoint bitcast ' +
  117. 'addrspacecast select va_arg ret br switch invoke ' +
  118. 'unwind unreachable indirectbr landingpad resume ' +
  119. 'malloc alloca free load store getelementptr ' +
  120. 'extractelement insertelement shufflevector getresult ' +
  121. 'extractvalue insertvalue atomicrmw cmpxchg fence ' +
  122. 'argmemonly double',
  123. contains: [
  124. TYPE,
  125. // this matches "empty comments"...
  126. // ...because it's far more likely this is a statement terminator in
  127. // another language than an actual comment
  128. hljs.COMMENT(/;\s*$/, null, { relevance: 0 }),
  129. hljs.COMMENT(/;/, /$/),
  130. hljs.QUOTE_STRING_MODE,
  131. {
  132. className: 'string',
  133. variants: [
  134. // Double-quoted string
  135. { begin: /"/, end: /[^\\]"/ },
  136. ]
  137. },
  138. FUNCTION,
  139. PUNCTUATION,
  140. OPERATOR,
  141. VARIABLE,
  142. LABEL,
  143. NUMBER
  144. ]
  145. };
  146. }
  147. module.exports = llvm;