Git Product home page Git Product logo

ox's Introduction

Summary

A C compiler written in Rust for experimentation and understanding compilers. Should be easy to fork from and work with. Everything is handwritten and the compiler uses no third-party libraries.

Please keep in mind, the compiler is currently EXPERIMENTAL, and is NOT PRODUCTION READY.

The compiler is roughly following this article.

Backend

The compiler generates 32-bit AT&T-style assembly. The backend can be easily replaced with any other. The code generator uses a recursive descent style similar to the parser. See src/generator.rs.

Where The Language is Right Now

All of tests/stage_1 through tests/stage_5.

Testing

$ cargo build
...
$ .\target\debug\oxc.exe .\test\stage_5\valid\exp_return_val.c
.\test\stage_5\valid\exp_return_val.c:
int main() {
    int a;
    int b;
    a = b = 4;
    return a - b;
}

Scanner production:
[Keyword(Int), Id("main"), Symbol(LParen), Symbol(RParen), Symbol(LBrace), Keyword(Int), Id("a"), Symbol(Semicolon), Keyword(Int), Id("b"), Symbol(Semicolon), Id("a"), Operator(Assignment), Id("b"),
Operator(Assignment), Integer(4), Symbol(Semicolon), Keyword(Return), Id("a"), Operator(Minus), Id("b"), Symbol(Semicolon), Symbol(RBrace)]

Abstract syntax tree:
Func(
    "main",
    [
        Declare(
            "a",
            None
        ),
        Declare(
            "b",
            None
        ),
        Expr(
            Assign(
                "a",
                Assign(
                    "b",
                    Const(
                        4
                    )
                )
            )
        ),
        Return(
            BinOp(
                Minus,
                Var(
                    "a"
                ),
                Var(
                    "b"
                )
            )
        )
    ]
)

Generated assembly:
  .globl _main
_main:
  push %ebp
  movl %esp, %ebp
  pushl $0
  pushl $0
  movl $4, %eax
  movl %eax, -8(%ebp)
  movl %eax, -4(%ebp)
  movl -8(%ebp), %eax
  push %eax
  movl -4(%ebp), %eax
  pop %ecx
  subl %ecx, %eax
_main_epilogue:
  movl %ebp, %esp
  pop %ebp
  ret

$ ./exp_return_val
$ echo $?
0

ox's People

Contributors

fivemoreminix avatar

Watchers

 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.