Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for LLVM IR #2221

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@
"title": "LiveScript",
"owner": "Golmote"
},
"llvm": {
"title": "LLVM IR",
"owner": "porglezomp"
},
"lolcode": {
"title": "LOLCODE",
"owner": "Golmote"
Expand Down
19 changes: 19 additions & 0 deletions components/prism-llvm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function(Prism) {
Prism.languages.llvm = {
'comment': /;.*/,
'string': {
pattern: /"[^"]*"/,
greedy: true,
},
'boolean': /\b(?:true|false)\b/,
porglezomp marked this conversation as resolved.
Show resolved Hide resolved
'variable': /[%@!#](?:(?!\d)(?:[-$.\w]|\\[a-f\d]{2})+|\d+)/i,
'label': /(?!\d)(?:[-$.\w]|\\[a-f\d]{2})+:/i,
'type': {
pattern: /\b(?:double|float|fp128|half|i[1-9]\d*|label|metadata|ppc_fp128|token|void|x86_fp80|x86_mmx)\b/,
alias: 'class-name',
},
'keyword': /\b[a-z_][a-z_0-9]*\b/,
'number': /[+-]?\b\d+(?:.\d+)?(?:[eE][+-]?\d+)?\b|\b0x[\dA-Fa-f]+\b|\b0xK[\dA-Fa-f]{20}\b|\b0x[ML][\dA-Fa-f]{32}\b|\b0xH[\dA-Fa-f]{4}\b/,
porglezomp marked this conversation as resolved.
Show resolved Hide resolved
'punctuation': /[{}[\];(),.!*=<>]/,
};
}(Prism));
1 change: 1 addition & 0 deletions components/prism-llvm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions examples/prism-llvm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h2>Full Example</h2>
<pre><code>
; Declare the string constant as a global constant.
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"

; External declaration of the puts function
declare i32 @puts(i8* nocapture) nounwind

; Definition of main function
define i32 @main() { ; i32()*
entry:
; Convert [13 x i8]* to i8*...
%cast210 = getelementptr [13 x i8], [13 x i8]* @.str, i64 0, i64 0

; Call puts function to write out the string to stdout.
call i32 @puts(i8* %cast210)
ret i32 0
}

; Named metadata
!0 = !{i32 42, null, !"string"}
!foo = !{!0}
</code></pre>
1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"emacs": "Lisp",
"elisp": "Lisp",
"emacs-lisp": "Lisp",
"llvm": "LLVM IR",
"lolcode": "LOLCODE",
"md": "Markdown",
"markup-templating": "Markup templating",
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions tests/languages/llvm/attributes.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
; Target-independent attributes:
attributes #0 = { alwaysinline alignstack=4 }

; Target-dependent attributes:
attributes #1 = { "no-sse" }

; Function @f has attributes: alwaysinline, alignstack=4, and "no-sse".
define void @f() #0 #1 { ... }

----------------------------------------------------

[
["comment", "; Target-independent attributes:"],
["keyword", "attributes"],
["variable", "#0"],
["punctuation", "="],
["punctuation", "{"],
["keyword", "alwaysinline"],
["keyword", "alignstack"],
["punctuation", "="],
["number", "4"],
["punctuation", "}"],
["comment", "; Target-dependent attributes:"],
["keyword", "attributes"],
["variable", "#1"],
["punctuation", "="],
["punctuation", "{"],
["string", "\"no-sse\""],
["punctuation", "}"],
["comment", "; Function @f has attributes: alwaysinline, alignstack=4, and \"no-sse\"."],
["keyword", "define"],
["type", "void"],
["variable", "@f"],
["punctuation", "("],
["punctuation", ")"],
["variable", "#0"],
["variable", "#1"],
["punctuation", "{"],
["punctuation", "."],
["punctuation", "."],
["punctuation", "."],
["punctuation", "}"]
]

----------------------------------------------------

Tests the attribute "#id" syntax.
11 changes: 11 additions & 0 deletions tests/languages/llvm/edge-cases.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
" ; "
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved

----------------------------------------------------

[
["string", "\" ; \""]
]

----------------------------------------------------

Specific edge-cases
113 changes: 113 additions & 0 deletions tests/languages/llvm/example-iterative.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
define i32 @add(i32 %a, i32 %b) {
entry:
br label %tailrecurse

tailrecurse:
%a.tr = phi i32 [ %a, %entry ], [ %tmp2, %recurse ]
%b.tr = phi i32 [ %b, %entry ], [ %tmp3, %recurse ]
%tmp1 = icmp eq i32 %a.tr, 0
br i1 %tmp1, label %done, label %recurse

recurse:
%tmp2 = sub i32 %a.tr, 1
%tmp3 = add i32 %b.tr, 1
br label %tailrecurse

done:
ret i32 %b.tr
}

----------------------------------------------------

[
["keyword", "define"],
["type", "i32"],
["variable", "@add"],
["punctuation", "("],
["type", "i32"],
["variable", "%a"],
["punctuation", ","],
["type", "i32"],
["variable", "%b"],
["punctuation", ")"],
["punctuation", "{"],
["label", "entry:"],
["keyword", "br"],
["type", "label"],
["variable", "%tailrecurse"],
["label", "tailrecurse:"],
["variable", "%a.tr"],
["punctuation", "="],
["keyword", "phi"],
["type", "i32"],
["punctuation", "["],
["variable", "%a"],
["punctuation", ","],
["variable", "%entry"],
["punctuation", "]"],
["punctuation", ","],
["punctuation", "["],
["variable", "%tmp2"],
["punctuation", ","],
["variable", "%recurse"],
["punctuation", "]"],
["variable", "%b.tr"],
["punctuation", "="],
["keyword", "phi"],
["type", "i32"],
["punctuation", "["],
["variable", "%b"],
["punctuation", ","],
["variable", "%entry"],
["punctuation", "]"],
["punctuation", ","],
["punctuation", "["],
["variable", "%tmp3"],
["punctuation", ","],
["variable", "%recurse"],
["punctuation", "]"],
["variable", "%tmp1"],
["punctuation", "="],
["keyword", "icmp"],
["keyword", "eq"],
["type", "i32"],
["variable", "%a.tr"],
["punctuation", ","],
["number", "0"],
["keyword", "br"],
["type", "i1"],
["variable", "%tmp1"],
["punctuation", ","],
["type", "label"],
["variable", "%done"],
["punctuation", ","],
["type", "label"],
["variable", "%recurse"],
["label", "recurse:"],
["variable", "%tmp2"],
["punctuation", "="],
["keyword", "sub"],
["type", "i32"],
["variable", "%a.tr"],
["punctuation", ","],
["number", "1"],
["variable", "%tmp3"],
["punctuation", "="],
["keyword", "add"],
["type", "i32"],
["variable", "%b.tr"],
["punctuation", ","],
["number", "1"],
["keyword", "br"],
["type", "label"],
["variable", "%tailrecurse"],
["label", "done:"],
["keyword", "ret"],
["type", "i32"],
["variable", "%b.tr"],
["punctuation", "}"]
]

----------------------------------------------------

An iterative example program.
87 changes: 87 additions & 0 deletions tests/languages/llvm/example-recursive.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
define i32 @add(i32 %a, i32 %b) {
entry:
%tmp1 = icmp eq i32 %a, 0
br i1 %tmp1, label %done, label %recurse

recurse:
%tmp2 = sub i32 %a, 1
%tmp3 = add i32 %b, 1
%tmp4 = call i32 @add(i32 %tmp2, i32 %tmp3)
ret i32 %tmp4

done:
ret i32 %b
}

----------------------------------------------------

[
["keyword", "define"],
["type", "i32"],
["variable", "@add"],
["punctuation", "("],
["type", "i32"],
["variable", "%a"],
["punctuation", ","],
["type", "i32"],
["variable", "%b"],
["punctuation", ")"],
["punctuation", "{"],
["label", "entry:"],
["variable", "%tmp1"],
["punctuation", "="],
["keyword", "icmp"],
["keyword", "eq"],
["type", "i32"],
["variable", "%a"],
["punctuation", ","],
["number", "0"],
["keyword", "br"],
["type", "i1"],
["variable", "%tmp1"],
["punctuation", ","],
["type", "label"],
["variable", "%done"],
["punctuation", ","],
["type", "label"],
["variable", "%recurse"],
["label", "recurse:"],
["variable", "%tmp2"],
["punctuation", "="],
["keyword", "sub"],
["type", "i32"],
["variable", "%a"],
["punctuation", ","],
["number", "1"],
["variable", "%tmp3"],
["punctuation", "="],
["keyword", "add"],
["type", "i32"],
["variable", "%b"],
["punctuation", ","],
["number", "1"],
["variable", "%tmp4"],
["punctuation", "="],
["keyword", "call"],
["type", "i32"],
["variable", "@add"],
["punctuation", "("],
["type", "i32"],
["variable", "%tmp2"],
["punctuation", ","],
["type", "i32"],
["variable", "%tmp3"],
["punctuation", ")"],
["keyword", "ret"],
["type", "i32"],
["variable", "%tmp4"],
["label", "done:"],
["keyword", "ret"],
["type", "i32"],
["variable", "%b"],
["punctuation", "}"]
]

----------------------------------------------------

A recursive example program.
49 changes: 49 additions & 0 deletions tests/languages/llvm/integers.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
0
1
+1
-1
0xdeadbeef
0x123456789ABCDEF
-0x2

+1.0
2.7e3
-1.0e+3

double 0x432ff973cafa8000
double 4.5e+15 ; equivalent forms as mentioned in the LangRef

0xK0123456789ABCDEFabcd ; x86_fp80
0xM0123456789ABCDEF0123456789abcdef ; ppc_fp128
0xL0123456789ABCDEF0123456789abcdef ; fp128
0xHFACE ; half

----------------------------------------------------

[
["number", "0"],
["number", "1"],
["number", "+1"],
["number", "-1"],
["number", "0xdeadbeef"],
["number", "0x123456789ABCDEF"],
["number", "-0x2"],
["number", "+1.0"],
["number", "2.7e3"],
["number", "-1.0e+3"],
["type", "double"],
["number", "0x432ff973cafa8000"],
["type", "double"],
["number", "4.5e+15"],
["comment", "; equivalent forms as mentioned in the LangRef"],
["number", "0xK0123456789ABCDEFabcd"],
["comment", "; x86_fp80"],
["number", "0xM0123456789ABCDEF0123456789abcdef"],
["comment", "; ppc_fp128"],
["number", "0xL0123456789ABCDEF0123456789abcdef"],
["comment", "; fp128"],
["number", "0xHFACE"],
["comment", "; half"]
]

----------------------------------------------------
Loading