https://github.com/wangyu-/mycc
A tiny C compiler of 2k lines of codes, generates pure x86 asm code for DOS/Win32/Linux 3 backends.
https://github.com/wangyu-/mycc
tiny-compiler
Last synced: 10 months ago
JSON representation
A tiny C compiler of 2k lines of codes, generates pure x86 asm code for DOS/Win32/Linux 3 backends.
- Host: GitHub
- URL: https://github.com/wangyu-/mycc
- Owner: wangyu-
- License: mit
- Created: 2021-02-05T11:38:53.000Z (about 5 years ago)
- Default Branch: multi-file
- Last Pushed: 2021-03-08T02:13:23.000Z (almost 5 years ago)
- Last Synced: 2025-03-24T18:11:19.609Z (11 months ago)
- Topics: tiny-compiler
- Language: C++
- Homepage:
- Size: 73.2 KB
- Stars: 36
- Watchers: 2
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# mycc
A tiny C compiler of 2k lines of codes, generates pure x86 asm code for DOS/Win32/Linux 3 backends.



Mycc supports a reasonable subset of the C language. Check [tests](https://github.com/wangyu-/mycc/tree/master/tests) and [examples](https://github.com/wangyu-/mycc/tree/master/examples) folder to get an idea of what mycc can compile. Below are some examples:
[nqueen.c](https://github.com/wangyu-/mycc/blob/master/examples/nqueen.c) is a mycc-compilable program that finds a solution for the n queen puzzle:
```
Q . . . . . . .
. . . . Q . . .
. . . . . . . Q
. . . . . Q . .
. . Q . . . . .
. . . . . . Q .
. Q . . . . . .
. . . Q . . . .
```
[sudoku.c](https://github.com/wangyu-/mycc/blob/master/examples/sudoku.c) is a mycc-compilable program that fills the sudoku game for you:
```
input: output:
1 . 3 | . . . | 5 . 9 1 4 3 | 6 2 8 | 5 7 9
. . 2 | 1 . 9 | 4 . . 5 7 2 | 1 3 9 | 4 6 8
. . . | 7 . 4 | . . . 9 8 6 | 7 5 4 | 2 3 1
-------+-------+------ -------+-------+------
3 . . | 5 . 2 | . . 6 3 9 1 | 5 4 2 | 7 8 6
. 6 . | . . . | . 5 . 4 6 8 | 9 1 7 | 3 5 2
7 . . | 8 . 3 | . . 4 7 2 5 | 8 6 3 | 9 1 4
-------+-------+------ -------+-------+------
. . . | 4 . 1 | . . . 2 3 7 | 4 8 1 | 6 9 5
. . 9 | 2 . 5 | 8 . . 6 1 9 | 2 7 5 | 8 4 3
8 . 4 | . . . | 1 . 7 8 5 4 | 3 9 6 | 1 2 7
````
Mycc supports the syntax of embeding asm codes into C, mycc compiled programs are able to do syscall directly on their own:
```
char getc()
{
char a;
a=0;
__asm
{
mov eax, 3 ;the syscall num of "read()" on linux
mov ebx, 0 ;read from stdin, 0 is the fd of stdin
mov ecx, a@getc ;save the result to the address of variable "a" inside function "getc"
mov edx, 1 ;read 1 character
int 0x80
}
return a;
}
```
Mycc is shipped with a built-in library (check [lib](https://github.com/wangyu-/mycc/tree/master/lib) and [lib.platform](https://github.com/wangyu-/mycc/tree/master/lib.platform)), implemented in the language of mycc itself. Becasue of this, unlike most other tiny compilers, mycc generates "pure asm" codes that runs independently, without the need of linking to external libraries (e.g. libc).
# How to use
make sure you have `nasm` installed.
##### 1 build mycc with make:
```
make
```
##### 2a To compile for Linux backend:
```
./mycc