lisp-compiler-llvm
lisp-compiler-llvm copied to clipboard
A Self-Hosting LISP to LLVM-IR Compiler
trafficstars
LISP Compiler
Example Code
(defn range (from to)
(if (eq? from to)
(list from)
(cons from
(range (fxadd1 from) to))))
(defn fizzbuzz (n)
(cond
((eq? (fxrem n 15) 0) (puts "FizzBuzz"))
((eq? (fxrem n 3) 0) (puts "Fizz"))
((eq? (fxrem n 5) 0) (puts "Buzz"))
(else (inspect n))))
(for-each fizzbuzz (range 1 100))
Getting Started
- Use chicken-scheme to build the compiler & the stdlib
- Compile some program to LLVM-IR
- Combine it w/ the stdlib files
- Run it
make bootstrap
./compiler < programs/fizzbuzz.csm > fizzbuzz-body.ll
cat stdlib-ll/*.ll stdlib.ll fizzbuzz-body.ll > fizzbuzz.ll
lli fizzbuzz.ll
Design Decisions
- 64bit only
- Tagged Pointers for values, 3bit tag, 61bit value
- 000: Integer
- 001: Symbol
- 010: Char
- 100: Closure
- 101: String
- 110: Pair
- 111: Hardcoded primitives, #t, #f, '()
- Symbols are limited to 31 chars
Limitations
- Not compliant to any standart
- No GC => compiling itself uses ~1GB of RAM
Project Structure
compiler.scmMain compiler logichelper.scmHelper functions that are not part of the stdlibllvm.scmHelper functions for outputting LLVM-IR codereader.scmRecursive descent parser for the input languagesyntax.scmDefines the syntax of the input language- `preprocessing/
desugar.scmConvertcondtoifetc.alpha-convert.scmRename variables to prevent naming collisionsclosure-convert.scmConvertfns to closures & lambdas w/o free variables
compatibility.scmSome macros & functions to make chicken scheme understand the compiler source codestdlib-ll/Collection of low-level stdlib functions written directly in LLVM-IR
Special Syntax
~>Thread-first macro like in clojure~>>Thread-last macro like in clojure