Skip to content

Commit

Permalink
Added support for LLVM IR (#2221)
Browse files Browse the repository at this point in the history
This adds support for LLVM's assembly language.
https://llvm.org/docs/LangRef.html
  • Loading branch information
porglezomp authored Mar 23, 2020
1 parent e635260 commit 43efde2
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 2 deletions.
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/,
'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/,
'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.
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.
Loading

0 comments on commit 43efde2

Please sign in to comment.