DQNEO / Babygo
Licence: mit
Go compiler made from scratch, which can compile itself. It's going to be the smallest and simplest go compiler in the world.
Stars: ✭ 143
Projects that are alternatives of or similar to Babygo
Minijit
A basic x86-64 JIT compiler written from scratch in stock Python
Stars: ✭ 185 (+29.37%)
Mutual labels: compiler, x86-64
Crust
C compiler toolchain in Rust. [WIP, early development stage]
Stars: ✭ 150 (+4.9%)
Mutual labels: compiler, x86-64
Mir
A light-weight JIT compiler based on MIR (Medium Internal Representation)
Stars: ✭ 1,075 (+651.75%)
Mutual labels: compiler, x86-64
Ppci
A compiler for ARM, X86, MSP430, xtensa and more implemented in pure Python
Stars: ✭ 210 (+46.85%)
Mutual labels: compiler, x86-64
Mazucc
A minimalist C compiler with x86_64 code generation
Stars: ✭ 437 (+205.59%)
Mutual labels: compiler, x86-64
Pyast64
Compile a subset of the Python AST to x64-64 assembler
Stars: ✭ 93 (-34.97%)
Mutual labels: compiler, x86-64
Peachpy
x86-64 assembler embedded in Python
Stars: ✭ 1,592 (+1013.29%)
Mutual labels: compiler, x86-64
Instapack
All-in-one TypeScript and Sass compiler for web applications! 📦 🚀
Stars: ✭ 131 (-8.39%)
Mutual labels: compiler
Koto
A simple, expressive, embeddable programming language, made with Rust
Stars: ✭ 134 (-6.29%)
Mutual labels: compiler
Babygo, a go compiler made from scratch
Babygo is a small and simple go compiler. (Smallest and simplest in the world, I believe.) It is made from scratch and can compile itself.
- No dependency to any libraries. Standard libraries and calling of system calls are home made.
- Lexer, parser and code generator are hand written.
- Emit assemble code which resutls in a single static binary.
It depends only on as
as an assembler and ld
as a linker.
It is composed of only a fiew files.
- main.go - the main compiler
- runtime.s - low level of runtime
- src/ - internal packages
- lib/ - libraries
Design
Lexer, Parser and AST
The design and logic of ast, lexer and parser are borrowed (or should I say "stolen") from go/ast
, go/scanner
and go/parser
.
Code generator
The design of code generator is borrowed from chibicc , a C compiler.
Remaining parts (Semantic analysis, Type management etc.)
This is purely my design :)
Environment
It supports x86-64 Linux only.
If you are not using Linux, you can use a dedicated docker image for this project.
$ docker pull dqneo/ubuntu-build-essential:go
$ ./docker-run
Usage
Hello world
# Build babygo
$ go build -o babygo *.go
# Compile the hello world program by babygo
$ ./babygo t/hello.go > /tmp/hello.s
# Assemble and link
$ as -o hello.o /tmp/hello.s runtime.s
$ ld -e _rt0_amd64_linux -o hello hello.o
# Run hello world
$ ./hello
hello world!
How to do self hosting
# Build babygo (1st generation)
$ go build -o babygo *.go
# Build babygo by babygo (2nd generation)
$ ./babygo *.go > /tmp/babygo2.s
$ as -o babygo2.o /tmp/babygo2.s runtime.s
$ ld -e _rt0_amd64_linux -o babygo2 babygo2.o # 2nd generation compiler
# Assert babygo2.s and babygo3.s are exactly same
$ ./babygo2 *.go > /tmp/babygo3.s
$ diff /tmp/babygo2.s /tmp/babygo3.s
Test
$ make test
Reference
- https://golang.org/ref/spec The Go Programming Language Specification
- https://golang.org/pkg/go/parser/ go/parser
- https://sourceware.org/binutils/docs/as/ GNU assembler
- https://github.com/rui314/chibicc C compiler
- https://github.com/DQNEO/minigo Go compiler (my previous work)
License
MIT
Author
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].