GithubHelp home page GithubHelp logo

solnone / amacc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jserv/amacc

0.0 0.0 0.0 403 KB

Small C Compiler generating ELF executable Arm architecture, supporting JIT execution

License: Other

Shell 1.09% Python 2.56% C 92.89% Makefile 3.46%

amacc's Introduction

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:

  • Just-in-Time (JIT) compiler for Arm backend.
  • Generation of valid GNU/Linux executables using the Executable and Linkable Format (ELF).

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:

  • support for all C89 statements except typedef.
  • support for all C89 expression operators.
  • data types: char, int, enum, struct, union, and multi-level pointers
    • type modifiers, qualifiers, and storage class specifiers are currently unsupported, though many keywords of this nature are not routinely used, and can be easily worked around with simple alternative constructs.
    • struct/union assignments are not supported at the language level in AMaCC, e.g. s1 = s2. This also applies to function return values and parameters. Passing and returning pointers is recommended. Use memcpy if you want to copy a full struct, e.g. memcpy(&s1, &s2, sizeof(struct xxx));
  • global/local variable initializations for supported data types
    • e.g., int i = [expr]
    • New variables are allowed to be declared within functions anywhere.
    • item-by-item array initialization is supported
    • but aggregate array declaration and initialization is yet to be supported e.g., 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.

Prerequisites

  • 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

    • Select arm-linux-none-gnueabihf (AArch32 target with hard float)
  • Install QEMU for Arm user emulation

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:

  • Raspberry Pi 4B (SoC: bcm2711, ARMv8-A architecture)
  • Raspbian GNU/Linux, kernel 5.10.17-v7l+, gcc 8.3.0 (armv7l userland)

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

Internals

Check Intermediate Representation (IR) for AMaCC Compilation.

Acknowledgements

AMaCC is based on the infrastructure of c4.

Related Materials

amacc's People

Contributors

coldnew avatar cykutw avatar dzhustin avatar hpcguy avatar johnnylchang avatar jserv avatar lecopzer avatar loganchien avatar oscarshiang avatar spacemiqote avatar splasky avatar thekk avatar timgates42 avatar vacantron avatar yodalee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.