Skip to content
/ cyx2 Public

Compiler and VM(interpreter), Using SSA form and doing some optimizations.

Notifications You must be signed in to change notification settings

flylai/cyx2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CYX2

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:

  1. CYX2 language, C like syntax.
  2. A Compiler, including hand-write parser and some optimizations.
  3. A Virtual Machine, register based VM, very simple and NAIVE.

Syntax

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

Architecture

  1. Read source code from file, put it into lexer and get the tokens.
  2. Put tokens to the parser, and get the AST.
  3. Simple semantic analysis(variable and function declaration), generate IR and optimize.
  4. Constructing dominator tree, build the SSA IR.
  5. Optimizations(SSA Based).
  6. VM bytecode generation(peephole optimizations).
  7. Write bytecode to file or interpret it.

Build

mkdir build
cd build
cmake ..
cmake --build .

Usage

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

Test

Including a lot of test sets, but not comprehensive.

run build/cyx2_test for more details.

Thanks

3rd parties

  • Google Test
  • dbg-macro
  • GraphViz

Releases

No releases published

Packages

No packages published