Git Product home page Git Product logo

cyx2's Introduction

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

cyx2's People

Contributors

flylai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.