https://github.com/ShinyaKato/sk2cc
Simple C compiler developed by @ShinyaKato
https://github.com/ShinyaKato/sk2cc
Last synced: 7 months ago
JSON representation
Simple C compiler developed by @ShinyaKato
- Host: GitHub
- URL: https://github.com/ShinyaKato/sk2cc
- Owner: ShinyaKato
- License: mit
- Created: 2018-07-08T07:40:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-24T16:01:05.000Z (about 6 years ago)
- Last Synced: 2024-11-09T23:02:26.185Z (about 1 year ago)
- Language: C
- Homepage:
- Size: 547 KB
- Stars: 43
- Watchers: 6
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- AwesomeCompiler - sk2cc
README
# sk2cc
sk2cc is a compiler for the subset of the C language.
It aims to support almost all language features of C11 while keeping the implementation as simple as possible.
For example, in lexical analyzer and parser, all code is hand-written, parser generators such as lex/flex and yacc/bison are not used.
So you can easily understand the implementation.
sk2cc has been achieved self-hosting, which means we can compile the source code of sk2cc with sk2cc itself.
You can understand what features are supported by sk2cc by reading its implementation because the all written code can be compiled with sk2cc.
In sk2cc's code generation, we succeeded in simplifying register allocation by pushing all intermediate calculation results onto the stack.
For example, when calculating `1 + 2`, sk2cc pushes `1` and `2` onto the stack, and then these values are popped into the registers.
Then two values are added, and resulting value is pushed onto the stack again.
While the register allocation is easy, the execution speed of generated assembly is slow.
sk2cc also includes the assembly, so you can generate object file from assembly source code.
However, sk2cc can not link object files and generate executable file, and the existing linker is needed for linking.
You can link object files by simply passing them gcc.
## Build
You can build sk2cc with make command:
```bash
make
```
This repository contains test script of sk2cc.
We check the self-hosted executable (self) and the executable generated by self-hosted executable (self2) in addition to the executable generated by gcc (sk2cc).
You can run all test suites:
```bash
make test
```
## Usage
sk2cc recieves path to a source file and generates assembly to stdout.
sk2cc also includes assembler, and can convert assmebly source code to object file.
To generate executable, use gcc with static link.
The example of compiling the sample programs is as follows:
```c
// hello.c
int printf(char *format, ...);
int main() {
printf("Hello World\n");
return 0;
}
```
```bash
# compile
./sk2cc hello.c > hello.s
# assemble
./sk2cc --as hello.s hello.o
# link with gcc
gcc -static hello.o -o hello
# execution
./hello
```
## Example
For example, sk2cc can compile the following program to find fibonacci numbers recursively:
```c
int printf(char *format, ...);
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
int main() {
for (int i = 0; i < 20) {
printf("%d: %d\n", i, fib(i));
}
return 0;
}
```
For another example, sk2cc can convert the following assembly program to print `Hello World`:
```asm
.section .rodata
.S0:
.ascii "Hello World\n\0"
.data
.global hello
hello:
.quad .S0
.text
.global main
main:
pushq %rbp
movq %rsp, %rbp
movq hello(%rip), %rdi
xorl %eax, %eax
call printf
leave
ret
```
Furthermore, the sk2cc source code itself can also be used as a compilable program.
## Author
This compiler is developed by Shinya Kato (Twitter: 0x19f).