Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 9eed27c

Browse files
LLVM mode courtesy Nikhil Dabas
1 parent db00d4e commit 9eed27c

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

src/lang-llvm.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (C) 2013 Nikhil Dabas
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
/**
17+
* @fileoverview
18+
* Registers a language handler for LLVM.
19+
* From https://gist.github.com/ndabas/2850418
20+
*
21+
*
22+
* To use, include prettify.js and this file in your HTML page.
23+
* Then put your code in an HTML tag like
24+
* <pre class="prettyprint lang-llvm">(my LLVM code)</pre>
25+
*
26+
*
27+
* The regular expressions were adapted from:
28+
* https://github.com/hansstimer/llvm.tmbundle/blob/76fedd8f50fd6108b1780c51d79fbe3223de5f34/Syntaxes/LLVM.tmLanguage
29+
*
30+
* http://llvm.org/docs/LangRef.html#constants describes the language grammar.
31+
*
32+
* @author Nikhil Dabas
33+
*/
34+
PR['registerLangHandler'](
35+
PR['createSimpleLexer'](
36+
[
37+
// Whitespace
38+
[PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
39+
// A double quoted, possibly multi-line, string.
40+
[PR['PR_STRING'], /^!?\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"'],
41+
// comment.llvm
42+
[PR['PR_COMMENT'], /^;[^\r\n]*/, null, ';']
43+
],
44+
[
45+
// variable.llvm
46+
[PR['PR_PLAIN'], /^[%@!](?:[-a-zA-Z$._][-a-zA-Z$._0-9]*|\d+)/],
47+
48+
// According to http://llvm.org/docs/LangRef.html#well-formedness
49+
// These reserved words cannot conflict with variable names, because none of them start with a prefix character ('%' or '@').
50+
[PR['PR_KEYWORD'], /^[A-Za-z_][0-9A-Za-z_]*/, null],
51+
52+
// constant.numeric.float.llvm
53+
[PR['PR_LITERAL'], /^\d+\.\d+/],
54+
55+
// constant.numeric.integer.llvm
56+
[PR['PR_LITERAL'], /^(?:\d+|0[xX][a-fA-F0-9]+)/],
57+
58+
// punctuation
59+
[PR['PR_PUNCTUATION'], /^[()\[\]{},=*<>:]|\.\.\.$/]
60+
]),
61+
['llvm', 'll']);

tests/prettify_test_2.html

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// Language extensions tested.
1313
"lang-clj.js",
1414
"lang-lisp.js",
15+
"lang-llvm.js",
1516
"lang-xq.js",
1617
"lang-mumps.js",
1718
"lang-n.js",
@@ -538,8 +539,6 @@ <h1>BASIC w/ language specified</h1>
538539
1000 DATA "one", 1, 0
539540
</pre>
540541

541-
</body>
542-
543542
<h1>Dart language handler</h1>
544543
<pre class="prettyprint lang-dart" id="dart">
545544
part of myLib;
@@ -775,6 +774,32 @@ <h1>MUMPS</h1>
775774
Q
776775
</pre>
777776

777+
<h1>LLVM</h1>
778+
<a href="http://llvm.org/docs/LangRef.html">Hello world module</a>
779+
<pre class="prettyprint lang-llvm" id="llvm">
780+
; Declare the string constant as a global constant.
781+
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"
782+
783+
; External declaration of the puts function
784+
declare i32 @puts(i8* nocapture) nounwind
785+
786+
; Definition of main function
787+
define i32 @main() { ; i32()*
788+
; Convert [13 x i8]* to i8 *...
789+
%cast210 = getelementptr [13 x i8]* @.str, i64 0, i64 0
790+
791+
; Call puts function to write out the string to stdout.
792+
call i32 @puts(i8* %cast210)
793+
ret i32 0
794+
}
795+
796+
; Named metadata
797+
!1 = metadata !{i32 42}
798+
!foo = !{!1, null}
799+
</pre>
800+
801+
</body>
802+
778803
<script type="text/javascript">
779804
/**
780805
* maps ids of rewritten code to the expected output.
@@ -1459,6 +1484,27 @@ <h1>MUMPS</h1>
14591484
' `END`KWDEND`END`PUN;`END`PLN\n' +
14601485
' `END`KWDEND`END`PUN;`END`PLN\n' +
14611486
'`END`KWDEND`END`PUN;`END'
1487+
),
1488+
llvm: (
1489+
'`COM; Declare the string constant as a global constant.`END`PLN\n' +
1490+
'@.str `END`PUN=`END`PLN `END`KWDprivate`END`PLN `END`KWDunnamed_addr`END`PLN `END`KWDconstant`END`PLN `END`PUN[`END`LIT13`END`PLN `END`KWDx`END`PLN `END`KWDi8`END`PUN]`END`PLN `END`KWDc`END`STR"hello world\\0A\\00"`END`PLN\n' +
1491+
'\n' +
1492+
'`END`COM; External declaration of the puts function`END`PLN\n' +
1493+
'`END`KWDdeclare`END`PLN `END`KWDi32`END`PLN @puts`END`PUN(`END`KWDi8`END`PUN*`END`PLN `END`KWDnocapture`END`PUN)`END`PLN `END`KWDnounwind`END`PLN\n' +
1494+
'\n' +
1495+
'`END`COM; Definition of main function`END`PLN\n' +
1496+
'`END`KWDdefine`END`PLN `END`KWDi32`END`PLN @main`END`PUN()`END`PLN `END`PUN{`END`PLN `END`COM; i32()*`END`PLN\n' +
1497+
' `END`COM; Convert [13 x i8]* to i8 *...`END`PLN\n' +
1498+
' %cast210 `END`PUN=`END`PLN `END`KWDgetelementptr`END`PLN `END`PUN[`END`LIT13`END`PLN `END`KWDx`END`PLN `END`KWDi8`END`PUN]*`END`PLN @.str`END`PUN,`END`PLN `END`KWDi64`END`PLN `END`LIT0`END`PUN,`END`PLN `END`KWDi64`END`PLN `END`LIT0`END`PLN\n' +
1499+
'\n' +
1500+
' `END`COM; Call puts function to write out the string to stdout.`END`PLN\n' +
1501+
' `END`KWDcall`END`PLN `END`KWDi32`END`PLN @puts`END`PUN(`END`KWDi8`END`PUN*`END`PLN %cast210`END`PUN)`END`PLN\n' +
1502+
' `END`KWDret`END`PLN `END`KWDi32`END`PLN `END`LIT0`END`PLN\n' +
1503+
'`END`PUN}`END`PLN\n' +
1504+
'\n' +
1505+
'`END`COM; Named metadata`END`PLN\n' +
1506+
'!1 `END`PUN=`END`PLN `END`KWDmetadata`END`PLN !`END`PUN{`END`KWDi32`END`PLN `END`LIT42`END`PUN}`END`PLN\n' +
1507+
'!foo `END`PUN=`END`PLN !`END`PUN{`END`PLN!1`END`PUN,`END`PLN `END`KWDnull`END`PUN}`END'
14621508
)
14631509
};
14641510
</script>

0 commit comments

Comments
 (0)