78

GitHub - DQNEO/minigo: a Go compiler from scratch. It can compile itself.

 5 years ago
source link: https://github.com/DQNEO/minigo
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 assenmbler 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 you to 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 do "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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK