Skip to content

ae-bii/coco

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

coco

why? because i wanted to learn about compilers and ocaml

Quick start

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 build

Run 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

Notes and troubleshooting

  • Assembler dialect and externs

    • The generator emits NASM-style directives (section, dq, resq) and uses explicit extern for undefined functions and global for 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 an extern _putchar line.
  • 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.

References

About

subset of C compiler written in OCaml

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages