UNPKG

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