why? because i wanted to learn about compilers and ocaml
Prerequisites:
- OCaml (recommended 4.08+)
- Dune (build system)
- A system assembler/linker (clang/ld or gcc) able to assemble the generated .s files. On macOS the system toolchain is used by the tutorial harness.
Build the compiler:
dune buildRun the compiler binary on a C file to generate an assembly file:
dune exec ./bin/main.exe -- path/to/input.c
# This will print the path to the generated .s file (in the same directory as the C file).Assemble and link the generated assembly yourself (optional):
# assemble and link with clang
clang -o output path/to/generated.s
# then run
./output-
Assembler dialect and externs
- The generator emits NASM-style directives (
section,dq,resq) and uses explicitexternfor undefined functions andglobalfor symbols the compiler defines (for example_main). The test harness expects this form on macOS. If you see assembler/linker errors about undefined symbols (for example_putchar), ensure the generated assembly includes anextern _putcharline.
- The generator emits NASM-style directives (
-
macOS specifics
- On macOS the assembler and linker can be more strict about relocations. This project uses RIP-relative references for global memory accesses where necessary to avoid text-relocation errors. If you change the generator, be careful to keep consistent RIP-relative addressing (or use the system assembler's accepted syntax).
-
Debugging validation errors
- If the validator reports errors like "Variable declared twice in same scope" or "Undeclared variable", check the corresponding C input and the AST printed by the compiler. The validator enforces declaration-before-use rules and proper block scoping.