Skip to content

Commit 33a6b12

Browse files
committed
Improve README
1 parent 20de475 commit 33a6b12

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

README.md

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,84 @@
1-
# Babygo: a go compiler made from scratch
1+
# Babygo, a go compiler made from scratch
2+
Babygo is a small and simple go compiler. (Smallest and simplest in the world, I believe.)
3+
It is made from scratch and can compile itself.
24

5+
* No dependency to any libraries. Standard libraries and calling of system calls are home made.
6+
* Lexer, parser and code generator are hand written.
7+
* Emit assemble code which resutls in a single static binary.
38

4-
# Target machine
9+
It depends only on `as` as an assembler and `ld` as a linker.
510

6-
x86-64 Linux
11+
It is composed of only 3 files.
712

8-
# Test
13+
* main.go - the main compiler
14+
* runtime.go - runtime and standard library
15+
* runtime.s - low level of runtime
916

10-
```terminal
17+
# Design
18+
19+
## Lexer, Parser and AST
20+
The design and logic of ast, lexer and parser are borrowed (or should I say "stolen") from `go/ast`, `go/scanner` and `go/parser`.
21+
22+
## Code generator
23+
The design of code generator is borrowed from [chibicc](https://github.com/rui314/chibicc) , a C compiler.
24+
25+
## Remaining parts (Semantic analysis, Type management etc.)
26+
This is purely my design :)
27+
28+
# Environment
29+
30+
It supports x86-64 Linux only.
31+
32+
If you are not using Linux, you can use [a dedicated docker image](https://hub.docker.com/r/dqneo/ubuntu-build-essential/tags) for this project.
33+
34+
```termiinal
35+
$ docker pull dqneo/ubuntu-build-essential:go
1136
$ ./docker-run
12-
# make test
1337
```
1438

15-
# How to do self hosting
39+
# Usage
40+
41+
## Hello world
1642

1743
```terminal
18-
$ ./docker-run
44+
# Build babygo
45+
$ go build -o babygo main.go
46+
47+
48+
# Build the hello world program by babygo
49+
$ ./babygo < t/hello.go > /tmp/hello.s
50+
$ as -o hello.o /tmp/hello.s runtime.s
51+
$ ld -o hello hello.o
1952
20-
# go build -o babygo main.go # 1st generation compiler
21-
# ./babygo < main.go > /tmp/babygo2.s
22-
# as -o babygo2.o /tmp/babygo2.s runtime.s
23-
# ld -o babygo2 babygo2.o # 2nd generation compiler
24-
# ./babygo2 < main.go > /tmp/babygo3.s
25-
# diff /tmp/babygo2.s /tmp/babygo3.s # assert babygo2.s and babygo3.s are same.
53+
# You can confirm it's a single static binary
54+
$ ldd ./hello
55+
not a dynamic executable
56+
57+
# Run hello world
58+
$ ./hello
59+
hello world!
60+
```
61+
62+
## How to do self hosting
63+
64+
```terminal
65+
# Build babygo (1st generation)
66+
$ go build -o babygo main.go
67+
68+
# Build babygo by babygo (2nd generation)
69+
$ ./babygo < main.go > /tmp/babygo2.s
70+
$ as -o babygo2.o /tmp/babygo2.s runtime.s
71+
$ ld -o babygo2 babygo2.o # 2nd generation compiler
72+
$ ./babygo2 < main.go > /tmp/babygo3.s
73+
74+
# Assert babygo2.s and babygo3.s are exactly same
75+
$ diff /tmp/babygo2.s /tmp/babygo3.s
76+
```
77+
78+
## Test
79+
80+
```terminal
81+
$ make test
2682
```
2783

2884
# LICENSE
@@ -31,4 +87,4 @@ MIT
3187

3288
# AUTHOR
3389

34-
@DQNEO
90+
[@DQNEO](https://twitter.com/DQNEO)

t/hello.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "syscall"
4+
5+
func main() {
6+
var msg = []uint8("hello world!\n")
7+
syscall.Write(1, msg)
8+
}

0 commit comments

Comments
 (0)