View all files | ||||
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:
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
Install QEMU for Arm user emulation
Run make check and you should see this:
AMaCC supports the following command line options:
If no options are specified, the compiled code is executed immediately (if there were no compile errors) with any additional command line arguments passed to the program.
Check the messages generated by make help to learn more about build targets.
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
| 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.