You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -21,6 +22,7 @@ This compiler contains multiple backends not found in the reference implementati
21
22
- JVM bytecode, formatted for the Krakatau assembler
22
23
- CIL bytecode, formatted for the Mono ilasm assembler
23
24
- WASM, in WAT format
25
+
- LLVM IR, in text format
24
26
25
27
The test suite includes both static validation of generated/annotated ASTs, as well as runtime tests that actually execute the output programs to check correctness. Many of the AST validation test cases are taken from test suites included in the release code for Berkeley's CS164, with some additional tests written for more coverage.
26
28
@@ -35,6 +37,10 @@ The test suite includes both static validation of generated/annotated ASTs, as w
35
37
- WASM Backend Requirements:
36
38
-[WebAssembly Binary Toolkit (wabt)](https://github.com/WebAssembly/wabt), specifically the `wat2wasm` tool
37
39
- NodeJS for the runtime
40
+
- LLVM Backend Requirements
41
+
- LLVM toolchain
42
+
-`llvmlite`
43
+
- Tested with LLVM 16.0.6
38
44
39
45
## Usage
40
46
@@ -61,6 +67,7 @@ The input file should have extension `.py`. If the output file is not provided,
61
67
-`jvm` - output JVM bytecode formatted for the Krakatau assembler
62
68
-`cil` - output CIL bytecode formatted for the Mono ilasm assembler
63
69
-`wasm` - output WASM as plaintext in WAT format
70
+
-`llvm` - output LLVM IR in text format
64
71
65
72
## Differences from the reference implementation:
66
73
@@ -145,6 +152,38 @@ Strings, lists, objects, and refs holding nonlocals are stored in the heap, alig
145
152
146
153
To provide memory safety, string/list indexing have bounds checking and list operations have a null-check, which crashes the program with a generic "unreachable" instruction.
147
154
155
+
## LLVM Backend Notes:
156
+
157
+
The LLVM backend for this compiler outputs LLVM IR in plaintext `.ll` format which can be compiled using `llc` or interpreted using `lli`:
158
+
1. Use this compiler to generate plaintext LLVM IR
The `demo_llvm.sh` script is a useful utility to compile and run files with the LLVM backend with a single command (provide the path to the input source file as an argument).
166
+
- To run the same example as above, run `./demo_llvm.sh tests/runtime/binary_tree.py`
167
+
168
+
Generated programs should only depend on the C standard library, so there's no custom runtime to link to.
169
+
170
+
### LLVM Backend - Unsupported Features:
171
+
-`input` stdlib function - TODO
172
+
173
+
### LLVM Backend - Memory Format, Safety, and Management:
174
+
175
+
- strings - null-terminated `char*`, same as C strings
176
+
- lists - first 4 bytes for length, followed by the contents as a packed array
177
+
- ints - 32 bits
178
+
- pointers (objects, strings, lists) - same as C pointers, where `None` is the null pointer
179
+
- objects - struct containing vtable address followed by attributes
180
+
181
+
Memory does not get freed/garbage collected once it is allocated, so large programs may run out of memory.
182
+
183
+
To provide some memory safety, string/list indexing have bounds checking and list operations have a null-check, which exits the program with a generic error message and line number.
184
+
185
+
Error handling is done using the `setjmp`/`longjmp` strategy, with the line of the error/assertion used as the argument for `longjmp`.
0 commit comments