Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.
/ MapleCompiler Public archive

MapleCompiler is a compiler for a subset of the C language

License

Notifications You must be signed in to change notification settings

Heptabarnak/MapleCompiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8f5cd32 · Apr 6, 2018
Apr 5, 2018
Apr 4, 2018
Apr 6, 2018
Mar 14, 2018
Apr 6, 2018
Apr 4, 2018
Mar 28, 2018
Apr 6, 2018
Apr 6, 2018
Feb 26, 2018
Apr 6, 2018
Mar 29, 2018

Repository files navigation

MapleCompiler

MapleCompiler is a compiler for a subset of the C language.

It is written in C++ and uses antlr4 for parsing.

Authors

  • Tom Bourret
  • Julien Charles-Nicolas
  • Marc-Antoine Fernandes
  • Justin Gally
  • Vincent Guillon
  • Loïc Sérazin

Compile

The project use cmake to compile.

To build an executable:

mkdir build
cd build
cmake ..
cmake --build . --target MapleCompiler -- -j 2

Testing

You can launch tests with:

ctest -V # -V to see cout

If you want to see some errors, programs are available in /tests directory.

Usage

./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

Current state

Language used

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 and getchar 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 an int64_t, so errors can appear!)
  • Function declaration (to allow recursion between two functions)

Other changes

  • 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);
  • Everything is cast without any checks!

Static analysis

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

Back-end completion

  • 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 ... elseblock
  • 5.7: Compile whileloops
  • 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

About

MapleCompiler is a compiler for a subset of the C language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages