A small compiler written in C++ that translates a minimal programming language into ARM64 assembly, then assembles and links it into a native executable.
This is a learning project whose goal is to understand the entire compilation pipeline, from tokenization to code generation and assembly, before moving on to more advanced compiler concepts.
The language currently supports a single instruction:
exit <code>;
Example (test.txt):
exit 42;
Compiling this file produces an executable that exits with status code 42.
- Tokenizer (
src/tokenization.hpp): splits the source code into tokens (exit, an integer literal, and;). - Code Generation (
tokens_to_asminsrc/main.cpp): walks through the token stream and generates the ARM64 assembly corresponding toexit N;(using theexitsystem call with exit codeN). - Assembly and Linking: the generated
.sfile is assembled withasand linked withldto produce a native executable.
-
A C++ compiler (C++17 or later)
-
ARM64 cross-compilation tools:
aarch64-linux-gnu-asaarch64-linux-gnu-ld
On Debian/Ubuntu:
sudo apt install g++ binutils-aarch64-linux-gnug++ -std=c++17 -o comp src/main.cpp./comp test.txt
./exec
echo $? # Prints the exit status defined by "exit N;"- Only one instruction is supported (
exit). - No syntax error handling. Invalid input silently produces an empty program.
- ARM64 is the only supported target architecture (cross-compilation tools are required on non-ARM64 hosts).
- No Abstract Syntax Tree (AST) yet. Code generation operates directly on the token stream.
- Add arithmetic operations (
+,-, etc.) - Add variables
- Replace the flat token stream with a proper parser and an Abstract Syntax Tree (AST)
- Improve syntax error reporting