Home

Awesome

AMaCC = Arguably Minimalist Arm C Compiler

Introduction

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.

Compatibility

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.

Prerequisites

sudo apt-get install qemu-user

Running AMaCC

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.

Benchmark

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 driverbinary size (KiB)compile time (s)
gcc with -O0 -ldl (compile+link)560.5683
gcc with -O0 -c (compile only)560.4884
AMaCC1000.0217

Internals

Check Intermediate Representation (IR) for AMaCC Compilation.

Acknowledgements

AMaCC is based on the infrastructure of c4.

Related Materials