This is my graduation project, I wrote it to learn something interesting about compilation theory.
Using SSA form, and implements the constant folding, constant propagation, dead code elimination and other optimizations, generates VM code and peephole optimization, and finally generated bytecode to file, the virtual machine interpret and execute it.
This project include:
- CYX2 language, C like syntax.
- A Compiler, including hand-write parser and some optimizations.
- A Virtual Machine, register based VM, very simple and NAIVE.
// this is a simple sample
// check the test/testcase for more details
def main() {
for (i = 0; i < 10; i++) {
if (i == 3) { continue }
println(hello("World " + (i + 1)))
if (i == 6) { break }
}
}
def hello(str) {
a = ["H"]
a += "e";
return a[0] + a[1] + "llo " + str
}
- Read source code from file, put it into lexer and get the tokens.
- Put tokens to the parser, and get the AST.
- Simple semantic analysis(variable and function declaration), generate IR and optimize.
- Constructing dominator tree, build the SSA IR.
- Optimizations(SSA Based).
- VM bytecode generation(peephole optimizations).
- Write bytecode to file or interpret it.
mkdir build
cd build
cmake ..
cmake --build .
# run build/cyx2
Usage:
cyx2 [options] [src file]
cyx2 -i-bytecode <bytecode file>
where options include:
-ssa
enable SSA mode, default is disabled
-constant-folding
enable constant folding(SSA based)
-constant-propagation
enable constant propagation(constant folding and SSA based)
-no-code-simplify
disable clearing temporary variables(after normal ir construction)
-no-cfg-simplify
disable clearing redundant basicblocks(empty and useless basicblocks)
-remove-unused-code
remove unused variable definitions, base on normal IR(aggressively)
-dead-code-elimination
dead code elimination(SSA based)
-peephole
enable peephole optimization(base on bytecode)
-dump-cfg
dump CFG(Graphviz), dump to stdout if `-dump-as-file` is not set
-dump-ir
dump IR, dump to stdout if `-dump-as-file` is not set
-dump-ast
dump AST(Graphviz), dump to stdout if `-dump-as-file` is not set
-dump-vm-inst
dump vm instructions, dump to stdout if `-dump-as-file` is not set
-dump-as-file
dump (cfg, ir, ast) as file, if not set `-o-`, `cyx.TYPE` is default
-o-ast
<destination file> dump AST to file
-o-ir
<destination file> dump IR to file
-o-cfg
<destination file> dump CFG to file
-o-vm-inst
<destination file> dump vm instructions(text) to file
-o-bytecode
<destination file> dump bytecode(binary) to file
-i-bytecode
<bytecode file> only virtual machine mode, no compiler
Using Text editor to open the dumped AST/IR/CFG files and copy the contents to GraphViz website, and you will see what you want to see, it helps me find some bugs and fix it.
Including a lot of test sets, but not comprehensive.
run build/cyx2_test
for more details.
- Google Test
- dbg-macro
- GraphViz