Giter Site home page Giter Site logo

compiler's Introduction

compiler

A toy compiler for a syntax subset of c++ written in python
requires python3, antlr4, antlr4-python3-runtime
in the follwing instructions, you may replace python3 with python and pip3 with pip in your enviroment
To install antlr4-python3-runtime, run pip3 install antlr4-python3-runtime==4.6

install

For installing, run python3 setup.py install

run a demo

  • First, make a new file, write some codes (like the follwing) in it, name it test.c
class A {
  int foo() {
    if (a < 0) {
      return a++;
    }
    else {
      return a--;
    }
  }
  int a;
}
int sum(int i) {
    int s = 0;
    int j;
    for (j = 0; j < i; j++) {
      s += j;
    }
    return s;
}
int main() {
  A a;
  a.foo();
  int s = sum(10);
  return 0;
}
  • Then, run python3 -m compiler -in test.c -out out.asm
  • At last, run cat out.asm, you may see the follwing assembly codes
  	section .data
	
	section .text
	ALIGN 16
sum__func__:
	
sum_begin0:
	mov rax, 0
	mov rsi, 0
loop_test2:
	cmp rsi, rdi
	jge loop_end3
loop_begin1:
	add rsi, 1
	jmp loop_test2
loop_end3:
sum_end4:
	ret
	
	ALIGN 16
:
	sub rsp, 8
	
main_begin5:
	call A_foo__func__
	mov rdi, 10
	call sum__func__
	mov rax, 0
main_end6:
	add rsp, 8
	ret
	
	ALIGN 16
A_foo__func__:
	
foo_begin7:
	mov rsi, qword [rdi]
	cmp rsi, 0
	jge if_else9
if_then8:
	mov rax, qword [rdi]
	mov rsi, qword [rdi]
	add rsi, 1
	mov qword [rdi], rsi
foo_end11:
	ret
cfg_added_0:
if_end10:
	jmp foo_end11
if_else9:
	mov rax, qword [rdi]
	mov rsi, qword [rdi]
	sub rsi, 1
	mov qword [rdi], rsi
	jmp foo_end11

compiler's People

Contributors

akkaze avatar

Watchers

James Cloos 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.