Small C Compiler generating ELF executable Arm architecture, supporting JIT execution
AMaCC is a 32-bit Arm architecture compiler built from scratch. It serves as a stripped-down version of C, designed as a pedagogical tool for learning about compilers, linkers, and loaders.
There are two execution modes AMaCC implements:
It is worth mentioning that AMaCC is designed to compile a subset of C necessary to self-host with the above execution modes. For instance, it supports global variables, particularly global arrays.
A simple stack-based Abstract Syntax Tree (AST) is generated through cooperative
stmt()
and expr()
parsing functions, both fed by a token-generating function.
The expr()
function performs some literal constant optimizations. The AST is
transformed into a stack-based VM Intermediate Representation (IR) using the
gen()
function. The IR can be examined via a command-line option. Finally, the
codegen()
function generates Arm32 instructions from the IR, which can be
executed via either jit()
or elf32()
executable generation
AMaCC combines classical recursive descent and operator precedence parsing. An operator precedence parser proves to be considerably faster than a recursive descent parser (RDP) for expressions when operator precedence is defined using grammar productions that would otherwise be turned into methods.
AMaCC is capable of compiling C source files written in the following syntax:
int i = [expr]
int foo[2][2] = { { 1, 0 }, { 0, 1 } };
The architecture support targets armv7hf with Linux ABI, and it has been verified on Raspberry Pi 2/3/4 with GNU/Linux.
Code generator in AMaCC relies on several GNU/Linux behaviors, and it is necessary to have Arm/Linux installed in your build environment.
Install GNU Toolchain for the A-profile Architecture
arm-linux-none-gnueabihf
(AArch32 target with hard float)Install QEMU for Arm user emulation
sudo apt-get install qemu-user
Run make check
and you should see this:
[ C to IR translation ] Passed
[ JIT compilation + execution ] Passed
[ ELF generation ] Passed
[ nested/self compilation ] Passed
[ Compatibility with GCC/Arm ] ........................................
----------------------------------------------------------------------
Ran 52 tests in 8.842s
OK
Check the messages generated by make help
to learn more.
AMaCC is able to generate machine code really fast and provides 70% of the performance of gcc -O0
.
Test environment:
Input source file: amacc.c
compiler driver | binary size (KiB) | compile time (s) |
---|---|---|
gcc with -O0 -ldl (compile+link) |
56 | 0.5683 |
gcc with -O0 -c (compile only) |
56 | 0.4884 |
AMaCC | 100 | 0.0217 |
Check Intermediate Representation (IR) for AMaCC Compilation.
AMaCC is based on the infrastructure of c4.