Dummy C-ish Compiler for Learning Purposes, targeting RV32IM.
The file tokens.md contains the definition of the tokens currently implemented in the parser.
The file grammar.md contains the grammar of the C-like language implemented. While generating the AST, many conditions are checked for the correctness of the program. In particular, no automatic type conversion is performed (casting must always be explicit).
The file LIR.md contains the grammar the linear intermediate representation employed in the middle end. It is a stack-based lir in SSA format (although without phi functions). It can be used to obtain a CFG of the code and implement some optimizations. While the project does not focus on optimizing the result, some simple local techniques are employed.
The file Optimization.md describes the techniques adopted for different level of optimizations. Some of these techniques are implemented directly during the LIR construction, thus they can be found in lirgen.rs.
The file Backend.md describes the backend process. The one and only target of the compiler is RISC-V on 32 bits, supporting extensions I and M. The result is compliant with the RISC-V ABI.
cargo build -r
The program in on a single file. It has to follow the syntax described in grammar.md. Relevant messages are shown in case of errors, with references to the input file.
Usage: dummy_cc [OPTIONS] --file-name <FILE_NAME>
Options:
-f, --file-name <FILE_NAME> Path of the file to compile
-o, --o <O> Path of the result file [default: out.asm]
--opt <OPT> Required level of optimization [default: 0]
--print-ast Show result of parsing
--print-lir Show result of lirgen
-a, --arch <ARCH> Target architecture [default: rv32im] [possible values: rv32im]
-h, --help Print help
-V, --version Print version
Available levels of optimization are 0
, 1
and 2
.
Using option --print-ast
you can see a printed version of the ast, highlighting the order in which expressions are evaluated.
Using option --print-lir
you can see a printed version of intermediate representation after it has been optimized.
- Engineering a Compiler, Second Edition, Cooper & Torcson. This was the main source of the project, as I build the compiler while studying it.
- Crafting Interpreters, Chapter 2, Nystrom. This was fundamental to obtain a working recursive descent parser.
- r/compilers.
- r/ProgrammingLanguages.
- carbon-ir. As inspiration for my IR.
- Compiler Explorer. To actively understand different compilations stages.
- RISC-V Interpreter. To test the initial results of the compilation.
- RISC-V Specifications V2.2. To understand the target ISA.
- This RISC-V Cheatsheet. Always on my side!
- Many online material. Unfortunately I have not kept track of all of them. Here are some: