MapleCompiler is a compiler for a subset of the C language.
It is written in C++ and uses antlr4 for parsing.
- Tom Bourret
- Julien Charles-Nicolas
- Marc-Antoine Fernandes
- Justin Gally
- Vincent Guillon
- Loïc Sérazin
The project use cmake
to compile.
To build an executable:
mkdir build
cd build
cmake ..
cmake --build . --target MapleCompiler -- -j 2
You can launch tests with:
ctest -V # -V to see cout
If you want to see some errors,
programs are available in /tests
directory.
./mapleCompiler [-a] [-o] [-c] [-l] [-t <x64|msp430|java>] <file.c>
-a
for static analysis-o
for optimisation-c
to generate ASM-l
to create the executable-t
to select target with x86 as default value
The subset include:
- Types for variables:
char
,int32_t
,int64_t
- Initialization at declaration for variables
- One dimension arrays
- Function definition: functions without return type will use the type
void
- Control structure:
if
,else
,while
- Block structure with
{
and}
- Expressions: Every operators available in C including affectation
- Declaration of variables are always in the beginning of the function (before instructions)
- Global variables
- Global functions
putchar
andgetchar
for I/O - A constant char like
'a'
- Only one source file with no pre-processing (will ignore every pre-processor directives)
Everything was implemented and is working, so we added some new things:
for
control structure- String initialization as an array of
char
(or anything actually) - Exponential notation like
5e2
(casted to anint64_t
, so errors can appear!) - Function declaration (to allow recursion between two functions)
- During array initialization: We forbid a bigger list of values than the size of the array (e.g.
a[1]={1,2}
is not allowed) - Arrays in arguments does not work as it implies pointers (it will not fail, but will be false!)
- We can assign an array to an int by passing the address of the tab to value of the int.
- e.g.
int a = tab;
=>int a = (int)(tab);
- e.g.
- Everything is cast without any checks!
Static analysis detect 3 potentials errors:
- If a variable is never used
- If a variables is never read
- If a variables is used before affectation
- 5.1: Generate an empty frame
- 5.2: Understand activation registration and the ABI
- 5.3: Compile a program which executes a
putchar()
- 5.4: Compile variable affectation
- 5.5: Compile expressions
- 5.6: Compile an
if ... else
block - 5.7: Compile
while
loops - 5.8: Compile value return
- 5.9: Compile
lvalue
affectation with any value - 5.10: Compile arrays
- 5.11: Compile function calls up to 6 arguments
- 5.12: Compile
for
loops - 5.13: Complex programs testing (Collatz conjecture/Suite de Syracuse, Ackermann function, ...)
- 5.14: Correct handling of types (90%) -> Available on the branch
type-size
- 5.15: Compile function callswith more than 6 arguments