llvm.js 4.5 KB

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