Skip to content
A small Go compiler written from scratch. It can compile itself. The goal is to be the most easy-to-understand Go compiler.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
csamples
gosamples/sum
internal
stdlib
t
terror/panic
.gitignore
0debugbuiltin.go
LICENSE
Makefile
README.md
add-test
as
ast.go
builder.go
byte_stream.go
comparison-test.sh
concate-stdlib.sh
cp-internalcode.sh
debug.go
design.md
docker-test.sh
dumper.go
gen.go
gen_abi.go
gen_assign.go
gen_builtin.go
gen_call.go
gen_control_flow.go
gen_globalvar.go
gen_load.go
gen_macro.go
gen_main.go
gen_map.go
gen_save.go
gen_string.go
inferrer.go
main.go
parser.go
predeclare.go
run
run2
scope.go
sema.go
string_util.go
test1gen.sh
test2gen.sh
testerror.sh
token.go
token.txt
tokenizer.go
type.go
unit_test.sh
util.go

README.md

Minigo

CircleCI

A Go compiler from scratch.

Description

minigo is a small Go compiler made from scratch. It can compile itself.

  • No dependency on yacc/lex
  • No dependency on external libraries
  • Standard libraries are also made from scratch.

It depends only on gcc as an assembler and linker, and on libc as a runtime.

minigo supports x86-64 Linux only.

Design

I made this almost without reading the original Go compiler.

minigo inherits most of its design from the followings.

There are several steps in the compilation proccess.

[go source] -> byte_stream.go -> [byte stream] -> token.go -> [token stream] -> parser.go -> [AST] -> gen.go -> [assembly code]

How to run

You need Linux, so I would recommend that you use Docker.

$ docker run --rm -it -w /mnt -v `pwd`:/mnt dqneo/ubuntu-build-essential:go bash

After entering the container, you can build and run it.

$ make
$ ./minigo t/hello/hello.go > a.s
$ gcc -g -no-pie a.s
$ ./a.out
hello world

How to "self compile"

$ make
$ ./minigo --version
minigo 0.1.0
Copyright (C) 2019 @DQNEO

$ ./minigo *.go > /tmp/minigo2.s
$ gcc -no-pie -o minigo2 /tmp/minigo2.s
$ ./minigo2 --version
minigo 0.1.0
Copyright (C) 2019 @DQNEO

$ ./minigo2 *.go > /tmp/minigo3.s
$ gcc -no-pie -o minigo3 /tmp/minigo3.s
$ ./minigo3 --version
minigo 0.1.0
Copyright (C) 2019 @DQNEO

You will see that the contents of 2nd generation compiler and 3rd generation compiler are identical.

$ diff /tmp/minigo2.s /tmp/minigo3.s

Test

$ make test

AUTHOR

@DQNEO

LICENSE

MIT License

You can’t perform that action at this time.