MyCC
This is a compiler written in C++ as part of the MAC5750 class - Compilers Theory and Building (University of São Paulo).
Why I keep this project? It was the best class during university and is good summary of what a learned during the course. Also, the professor responsible for the discipline was one of the most important ones in my life.
Running
It is simple to, build the compiler:
make
At the moment, it only works on Linux x86. If you are running in odd system, you can use the Dockerfile.
To generate an assembly run:
./mycc <PROGRAM_NAME> > out.s
There is still some work to do the transcript the assembly to machine code. You
can achieve by using gcc
:
gcc out.s
Notes
Using MYCC_NUMBER_OF_REGISTERS
env var you can set the number of register used
by the final code. At the moment, it can be a value between 2 and 12. The
default register are: rbp, rsp, rdi, rax, rbx
Steps
The compiler doesn’t use any external dependency, just pure C++. The parser and the lexical analyser (lex) were written from the scratch.
The lex and the parser are build together with the compiler. The tokens are
defined in final/lex.lex
and the grammar in final/parser
.
Depending on the flags this program can generate a Graphviz representation of the internal state and the transformations.
Directories
final
: contains the compiler corelex
: lexical analyserparser
: grammar parserprograms
: examples and expected resultsscripts
: helper scripts to build this site and run testssite
: this site in Hugo
TODOs
Memory leaks? Yes, it is the default. Arrays? Not supported. However, you can create objects.
Tests:
Program | 2 Registers | 6 Registers | 12 Registers |
---|---|---|---|
./2args | ✓ | ✓ | ✓ |
./4class_var | ✓ | ✓ | ✓ |
./call_empty | ✓ | ✓ | ✓ |
./call_merge | ✓ | ✓ | ✓ |
./call_recursion | ✓ | ✓ | ✓ |
./call_same_class | ✓ | ✓ | ✓ |
./class_var | ✓ | ✓ | ✓ |
./factorial | ✓ | ✓ | ✓ |
./if_false | ✓ | ✓ | ✓ |
./if_true | ✓ | ✓ | ✓ |
./main | ✓ | ✓ | ✓ |
./op_and | ✓ | ✓ | ✓ |
./op_div | ✓ | ✓ | ✓ |
./op_minus | ✓ | ✓ | ✓ |
./op_mod | ✓ | ✓ | ✓ |
./op_not | ✓ | ✓ | ✓ |
./op_times | ✓ | ✓ | ✓ |
./poo_array | ✗ | ✓ | ✓ |
./poo_basic | ✓ | ✓ | ✓ |
./poo_print | ✓ | ✓ | ✓ |
./poo_reference | ✓ | ✓ | ✓ |
./print_two_numbers | ✓ | ✓ | ✓ |
./sort | ✗ | ✗ | ✗ |
./spill | ✓ | ✓ | ✓ |
./spill3 | ✓ | ✓ | ✓ |
./while | ✓ | ✓ | ✓ |