Skip to content

Instantly share code, notes, and snippets.

@ndabas
Forked from jterrace/lang-llvm.js
Last active October 5, 2015 18:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndabas/2850418 to your computer and use it in GitHub Desktop.
Save ndabas/2850418 to your computer and use it in GitHub Desktop.
LLVM plugin for google-code-prettify

LLVM plugin for google-code-prettify

lang-llvm.js adds LLVM assembly language syntax highlighting support to google-code-prettify. I wrote this in response to a question on StackOverflow.

Usage

See the google-code-prettify README for instructions on how to setup google-code-prettify. Then:

  1. Include lang-llvm.js in your HTML head tag, after prettify.js. This file is now included in the official google-code-prettify distribution.
  2. Markup your LLVM code like this:
    <pre class="prettyprint lang-llvm">(my LLVM code)</pre>
    

Credits

The regular expressions were stolen from the TextMate syntax file from the LLVM TextMate Bundle project.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>LLVM plugin for google-code-prettify - demo</title>
<!-- You should not include prettify from svn like this in a production application. No gzip and 180-second cache expiration. -->
<link rel="stylesheet" href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css">
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/lang-llvm.js"></script>
</head>
<body onload="prettyPrint()">
<pre class="prettyprint lang-llvm">
; 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()*
; Convert [13 x i8]* to i8 *...
%cast210 = getelementptr [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
!1 = metadata !{i32 42}
!foo = !{!1, null}
</pre>
</body>
</html>
/**
* @fileoverview
* Registers a language handler for LLVM.
*
*
* To use, include prettify.js and this file in your HTML page.
* Then put your code in an HTML tag like
* <pre class="prettyprint lang-llvm">(my LLVM code)</pre>
*
*
* The regular expressions were adapted from:
* https://github.com/hansstimer/llvm.tmbundle/blob/76fedd8f50fd6108b1780c51d79fbe3223de5f34/Syntaxes/LLVM.tmLanguage
*
* @author Nikhil Dabas
*/
PR['registerLangHandler'](
PR['createSimpleLexer'](
[
// Whitespace
[PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
// A double quoted, possibly multi-line, string.
[PR['PR_STRING'], /^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"'],
// comment.llvm
[PR['PR_COMMENT'], /^;[^\r\n]*/, null, ';'],
],
[
// llvm instructions
[PR['PR_KEYWORD'], /^\b(?:add|alloca|and|ashr|bitcast|br|call|eq|exact|extractelement|extractvalue|fadd|fcmp|fdiv|fmul|fpext|fptosi|fptoui|fptrunc|free|frem|fsub|getelementptr|icmp|inbounds|indirectbr|insertelement|insertvalue|inttoptr|invoke|load|lshr|malloc|mul|ne|nsw|nuw|oeq|oge|ogt|ole|olt|one|or|ord|phi|ptrtoint|ret|sdiv|select|sext|sge|sgt|shl|shufflevector|sitofp|sle|slt|srem|store|sub|switch|trunc|udiv|ueq|uge|uge|ugt|ugt|uitofp|ule|ule|ult|ult|une|uno|unreachable|unwind|urem|va_arg|xor|zext)\b/, null],
// llvm keywords
[PR['PR_KEYWORD'], /^\b(?:addrspace|alias|align|alignstack|alwaysinline|appending|asm|blockaddress|byval|c|cc|ccc|coldcc|common|constant|datalayout|declare|default|define|deplibs|dllexport|dllimport|except|extern_weak|external|fastcc|gc|global|hidden|inlinehint|inreg|internal|linkonce|linkonce_odr|metadata|module|naked|nest|noalias|nocapture|noimplicitfloat|noinline|noredzone|noreturn|nounwind|optsize|private|protected|ptx_device|ptx_kernel|readnone|readonly|section|sideeffect|signext|sret|ssp|sspreq|tail|target|thread_local|to|triple|uwtable|volatile|weak|weak_odr|x86_fastcallcc|x86_stdcallcc|zeroext)\b/, null],
// variable.llvm
[PR['PR_TYPE'], /^\s(?:[%@][-a-zA-Z$._][-a-zA-Z$._0-9]*)/],
// variable.language.llvm
[PR['PR_TYPE'], /^\s(?:[%]\d+)/],
// storage.type.language.llvm
[PR['PR_PLAIN'], /^\b(?:i\d+\**)/],
// variable.metadata.llvm
[PR['PR_PLAIN'], /^(!\d+)/],
// constant.numeric.float.llvm
[PR['PR_LITERAL'], /^\b\d+\.\d+\b/],
// constant.numeric.integer.llvm
[PR['PR_LITERAL'], /^\b(?:\d+|0(?:x|X)[a-fA-F0-9]+)\b/],
]),
['llvm', 'll']);
@mikesamuel
Copy link

Nikhil, I'm the maintainer of google-code-prettify. Would you mind if I included this in code.google.com/p/google-code-prettify? If so, do you know whether the original TextMate syntax file and LLVM bundle project are license compatible with the Apache 2.0 license and do you mind if I slap a copyright header with your name on top?

@ndabas
Copy link
Author

ndabas commented Feb 10, 2013

@mikesamuel, I would love it if this was included in the google-code-prettify project! I have no problems in re-licensing this under the Apache 2.0 license, please go ahead. If you need to provide some sort of link with my name, you can use http://www.nikhildabas.com/.

The original project where I got the regular expressions from has not specified a license, so I guess adding a link to the project should be enough.

Let me know if you need anything else from me to make this happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment