rswier / c4

C in four functions
GNU General Public License v2.0
9.63k stars 1.42k forks source link

how to compile c4.c by c4? #29

Closed rksys closed 7 years ago

rksys commented 7 years ago

I succeeded c4 hello.c, then I wanna try to compile c4.c by c4 itself. I tried c4 c4.c, it output:

usage: c4 [-s] [-d] file ... exit(-1) cycle = 45

Then, I tried c4 -s -d c4.c, it output a bunch of ascii format of assembly code. which is like:

525: else { printf("unknown instruction = %d! cycle = %d\n", i, cycle); return -1; } JMP 0 IMM 3356448 PSH LEA -11 LI PSH LEA -10 LI PSH PRTF ADJ 3 IMM -1 LEV 526: } 527: } JMP 3123380 LEV

So I am wondering how to compile the c4.c into binary by c4 itself. Thanks!

rswier commented 7 years ago

c4 compiles the program into binary and then immediately runs it in the built-in virtual machine interpreter. The binary is not saved to a file. So when you typed "c4 c4.c" it did compile c4.c and then interpreted the binary. Since c4.c expected a file to compile (another one!) it generated the "usage..." message and quit. If you type "c4 c4.c hello.c" what happens is that: c4 compiles c4.c and then runs the results with the argument "hello.c". Then hello.c is compiled by c4.c and ran, all within the virtual machine interpreter. The difference between running "c4 c4.c hello.c" and just "c4 hello.c" is that the hello binary is running in the virtual interpreter nested within the first interpreter.

I hope that is not too confusing! :-)

Also, there are forks of the c4 project that generate machine code for real CPU's if that is something you need.

rksys commented 7 years ago

wow, that is very clear and helpful! 👍 by the way, do you remeber the name of those:

there are forks of the c4 project that generate machine code for real CPU

I have done a bit of google search, but only find rswier/c4/, could you please give me the address of the fork, thanks and much appreciated!

rswier commented 7 years ago

https://github.com/pclouds/c4 https://github.com/clarkok/c4 https://github.com/EarlGray/c4

rksys commented 7 years ago

Thank you very much!