Git Product home page Git Product logo

c6's Introduction

C6 -- 700 行的 C 語言編譯器

使用方式

建議在 Linux 下編譯本專案 (WSL 亦可,Windows 的 MinGW 比較容易有錯)

$ sudo apt update
$ sudo apt install gcc-multilib

$ make
gcc -w -g -m32 c6.c -o c6
gcc -w -g -m32 genasm.c -o genasm
gcc -w -g -m32 jit.c -o jit -ldl

$ ./c6 test/hello.c
hello, world
exit(0) cycle = 9

$ ./c6 test/sum.c
sum(10)=55
exit(0) cycle = 303

$ ./c6 test/fib.c
f(7)=13
exit(8) cycle = 920

自我編譯

$ ./c6 c6.c test/fib.c
f(7)=13
exit(8) cycle = 920
exit(8) cycle = 147464

$ ./c6 c6.c c6.c test/fib.c
f(7)=13
exit(8) cycle = 920
exit(8) cycle = 147464
exit(8) cycle = 24476550

印出組合語言 (堆疊機)

$ ./c6 -s test/hello.c
1: #include <stdio.h>
2:
3: int main()
4: {
5:   printf("hello, world\n");
    1:F7D47014     ENT  0
    3:F7D4701C     ADDR 0:F7D06010
    5:F7D47024     PSH
    6:F7D47028     PRTF
    7:F7D4702C     ADJ  1
6: }
    9:F7D47034     LEV

印出執行過程

$ ./c6 -d test/hello.c
    1:F7C5E014     ENT  0
    3:F7C5E01C     ADDR 0:F7C1D010
    5:F7C5E024     PSH
    6:F7C5E028     PRTF
hello, world
    7:F7C5E02C     ADJ  1
    9:F7C5E034     LEV
 -67586:F7C1C008     PSH
 -67585:F7C1C00C     EXIT
exit(13) cycle = 8

JIT 編譯執行

$ wsl
wsl> make clean
rm -f c6 genasm jit

wsl> make
gcc -w -g -m32 c6.c -o c6
gcc -w -g -m32 genasm.c -o genasm
gcc -w -g -m32 jit.c -o jit -ldl

wsl> ./jit_test.sh hello
Compile test/hello.c success!
Output: test/hello.vm
hello, world

wsl> ./jit_test.sh sum
Compile test/sum.c success!
Output: test/sum.vm
sum(10)=55

wsl> ./jit_test.sh fib
Compile test/fib.c success!
Output: test/fib.vm
f(7)=13

c6's People

Contributors

ccckmit avatar austin362667 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.