Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to write a self hosted Go compiler from scr...

DQNEO
November 12, 2020

How to write a self hosted Go compiler from scratch (Gophercon 2020)

GopherCon 2020 (GoVircon) Talk

My compiler: https://github.com/DQNEO/babygo

DQNEO

November 12, 2020
Tweet

More Decks by DQNEO

Other Decks in Programming

Transcript

  1. How to write a self hosted Go compiler from scratch

    Daisuke Kashiwagi Gophercon 2020 November 12
  2. About me Daisuke Kashiwagi https://github.com/DQNEO • Software engineer at Mercari

    • Living in Japan • Longtime PHP user mostly on web • Wrote some compilers for fun • Hardly no knowledge about compilers at firs
  3. Today’s Goal Convince you that • You can write your

    own Go compiler • It’s really fun !! • Hardly no knowledge about compilers at firs
  4. Agenda • Demo • Writing a C compiler in Go

    • Writing a Go compiler in Go • Contribution to the official Go compiler • Writing another Go compiler in Go
  5. My compilers 1. 8cc.go C compiler in Go 2. minigo

    Go compiler in Go 3. babygo Go compiler in Go ← can compile itself ← can compile itself
  6. Architecture of my compilers my compiler source → assembly →

    object file → executable GCC (assembler & linker)
  7. minigo & babygo • Targeting x86-64 Linux only • Lexer

    and parser are handwritten • Standard libs are made from scratch • Stack machine • Far from production quality (for now) ◦ No garbage collection ◦ No concurrency ◦ Minimal error check
  8. Self hosting Go compiler *.go compiler source *.go my compile3

    my compiler *.go my compile2 1st generation 2nd generation 3rd generation official compiler
  9. Me before the journey • Zero knowledge about compilers ◦

    Did not major in CS • Not very good at Go ◦ Mostly a PHP programmer ◦ Gave up on “Tour of Go” twice • Wanted to be better at Go • Interested in low level programming
  10. Encounter with 8cc • self hosting C compiler • written

    from scratch • 9,000 lines of code Diary: https://www.sigbus.info/how-i-wrote-a-self-ho sting-c-compiler-in-40-days
  11. 8cc: First commit #include <stdio.h> #include <stdlib.h> int main(int argc,

    char **argv) { int val; if (scanf("%d", &val) == EOF) { perror("scanf"); exit(1); } printf("\t.text\n\t" ".global mymain\n" "mymain:\n\t" "mov $%d, %%eax\n\t" "ret\n", val); return 0; } #include <stdio.h> extern int mymain(void); int main(int argc, char **argv) { int val = mymain(); printf("%d\n", val); return 0; } https://github.com/rui314/8cc/commit/3764b2071b9601067b81976d80175a0851d0f209
  12. My work Inspired by ▶ 1 8cc.go 8cc 2 minigo

    8cc 3 babygo chibicc, go/parser 8cc.go: Porting 8cc to Go https://github.com/DQNEO/8cc.go
  13. • C compiler written in Go • Ported commits from

    the beginning from C to Go 8cc.go: Porting 8cc to Go
  14. • Continued for 5 months • Ported over 100 commits

    • Covered most major syntax Porting commits from C to C to Go
  15. Porting 8cc : I learned • How to write C

    and Go • How the C language works internally • How to read/write assembly code • What stack machines are like • How similar is C to Go
  16. Learn C and Go at the same time static char

    *REGS[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"}; var REGS = [...]string{"rdi", "rsi", "rdx","rcx", "r8", "r9"} C Go
  17. static Ast *ast_uop(int type, Ctype *ctype, Ast *operand) { Ast

    *r = malloc(sizeof(Ast)); r->type = type; r->ctype = ctype; r->operand = operand; return r; } func ast_uop(typ int, ctype *Ctype, operand *Ast) *Ast { r := &Ast{} r.typ = typ r.ctype = ctype r.operand = operand return r } C Go Learn C and Go at the same time
  18. My work Inspired by 1 8cc.go 8cc ▶ 2 minigo

    8cc 3 babygo chibicc, go/parser Tried writing a Go compiler https://github.com/DQNEO/minigo
  19. minigo: My first go compiler First commit .globl main main:

    movl $0, %eax ret a program which exits with status 0
  20. • Day 1: Arithmetic addition worked minigo: My first go

    compiler $ echo '2 + 5' | go run main.go # ==== Start Dump Tokens === 2 + 5 # ==== End Dump Tokens === # right=5 # ==== Dump Ast === # ast.binop=binop # left=2 # right=5 .globl main main: movl $2, %ebx movl $5, %eax addl %ebx, %eax ret
  21. • Month 1: FizzBuzz worked • Month 2: It was

    able to parse itself minigo: rapid progress in the first half
  22. • Designed as such • Parser can determine mode only

    by looking at one token in top level ◦ ”type” ,“var”, “func” • types can be read from left to right ◦ e.g. []*int • few historical twists and turns in its syntax Go language is easy to scan and parse
  23. Writing a Go compiler in Go: the easy parts •

    Lexer and parser can be easily implemented • You can use powerful tools like slice, map, for-range
  24. Writing a Go compiler in Go: the hard parts •

    You must implement powerful tools like slice, map, for-range • Some data types are larger than a single register ◦ string (16 bytes), slice (24 bytes) ▪ handling them on a stack machine is not trivial • Runtime features ◦ Goroutine ◦ Memory management
  25. • Assignment is not an expression ( x = 1

    ) • Increment is not an expression (x++) • How iota works • How identifiers are “resolved” • Role of the universe block • etc. Learning Go spec by writing its compiler
  26. • Month 3: implement append, map, interface • Month 4:

    SEGV in 2nd generation compiler • Month 5: SEGV in 2nd generation compiler minigo: Struggles in the last half
  27. bugs in the 2nd gen compilation *.go source *.go minigo1

    *.go minigo2 1 generation: an ordinary go program 2 generation: my assembly with a lot of mistakes Official go
  28. • 10,000 lines of code • Without taking any look

    at the official compiler • Supports ◦ slice, array, struct ◦ map, interface, method call ◦ type assertion, type switch ◦ etc. minigo: self hosted
  29. minigo: Added more features • Environment variables • GOPATH •

    importing of 3rd party libraries • Eliminated libc dependency
  30. Implementation of “append” func append1(x []byte, elm byte) []byte {

    var z []byte xlen := len(x) zlen := xlen + 1 if cap(x) >= zlen { z = x[:zlen] } else { var newcap int if xlen == 0 { newcap = 1 } else { newcap = xlen * 2 } z = makeSlice(zlen, newcap, 1) for i:=0;i<xlen;i++ { z[i] = x[i] } } z[xlen] = elm return z } Borrowed from the “Go programming language”
  31. Implementation of malloc (1st ver) • Using a static area

    (pseudo heap) • each malloc() consumes a piece of segment var heap [640485760]byte var heapTail *int func malloc(size int) *int { if heapTail+ size > len(heap) + heap { panic("malloc failed") } r := heapTail heapTail += size return r }
  32. Implementation of “map” • array of pairs of key and

    value • “map get” is just a linear search • Mostly written in assembly code
  33. Implementation of "interface" • Serialize string representation of a type

    on assignment ◦ e.g. var x *T var i interface{} = x *T → “*G_NAMED(main.T)” • type switch / type assertion compares those string representations • Lookup of method call is like “map get”
  34. minigo lacks... • Garbage collection • Go routine ◦ extremely

    difficult • Floating point numbers • Multiplatform (OS,CPU) • etc
  35. Funny bug: break for { … break ... } for

    { … ... } Super jump ! f
  36. minigo : Room for improvement - Not Go-ish • Internal

    ABI (Application Binary Interface) is very close to that of C compilers ◦ e.g. registers assignment in function call • Started with null-terminated string and libc dependency ◦ Changed the fundamental design in the end ▪ null-terminated string → slice-like struct ▪ Eliminated libc dependency ◦ I wish I had done it from the beginning
  37. minigo: Room for improvement • Code generation is a chaos

    ◦ Assignment is super complicated due to my poor understanding of stack machine
  38. Tried reading the official Go compiler • After minigo, I

    started to look at the official compiler • Found myself being able to understand some parts ◦ I had an overall map in my mind about what compilers look like • Could read code by thinking “What’s different between mine and theirs?”
  39. • How size of each embedded type is designed ?

    src/cmd/compile/internal/gc/align.go src/cmd/compile/internal/gc/go.go Tried reading the official Go compiler
  40. Official compiler: size of slice Why is the size of

    slice named “sizeof_Array” ? case TSLICE: if t.Elem() == nil { break } w = int64(sizeof_Array)
  41. Official compiler: variable names for slice // note this is

    the runtime representation // of the compilers arrays. // // typedef struct // { // uchar array[8]; // pointer to data // uchar nel[4]; // number of elements // uchar cap[4]; // allocated number of elements // } Array; var array_array int // runtime offsetof(Array,array) - same for String var array_nel int // runtime offsetof(Array,nel) - same for String var array_cap int // runtime offsetof(Array,cap) var sizeof_Array int // runtime sizeof(Array) Could we improve these ?
  42. Tried submitting a patch • “array” → “slice” • Tried

    Gerrit https://go-review.googlesource.com/c/go/+/180919
  43. Lingering questions • Could I do self-host much more easily

    if I try another one… ? • What would it be like to take a different approach … ? ◦ If I started without libc from the beginning ? ◦ if I used go/parser ? ◦ What is the ideal stack machine … ?
  44. My work Inspired by 1 8cc.go 8cc 2 minigo 8cc

    ▶ 3 babygo chibicc, go/parser Started writing another Go compiler https://github.com/DQNEO/babygo
  45. babygo: First commit // runtime .text .global _start _start: movq

    $42, %rdi movq $60, %rax syscall a program which exits with status 42
  46. First commit: minigo vs babygo .global _start _start: movq $42,

    %rdi movq $60, %rax syscall (apple to apple comparison) .global main main: movl $42, %eax ret minigo babygo
  47. babygo: different approaches • less features • better stack machine

    • more Go-like • the order of implementation
  48. babygo: less features • as small as possible • omitted

    ◦ map, interface, method ◦ packaging system ◦ etc.
  49. Stack machine (chibicc style) pushq $3 pushq $5 popq %rcx

    popq %rax addq %rcx, %rax pushq %rax 3 + 5 Go Assembly (gas x86-64)
  50. x = y leaq -16(%rbp), %rax pushq %rax leaq -8(%rbp),

    %rax pushq %rax popq %rax movq 0(%rax), %rax pushq %rax popq %rdi popq %rax movq %rdi, (%rax) address of x address of y value of y assign value to x Go Assembly (gas x86-64) babygo: stack machine (chibicc-like)
  51. Source <calc address> pushq %rax <calc address> pushq %rax popq

    %rax movq 0(%rax), %rax pushq %rax popq %rdi popq %rax movq %rdi, (%rax) address of left expr address of right expr value of right assign value to left Assembly (gas x86-64) a.b[c].d = e[f].g[h] babygo: stack machine (chibicc-like)
  52. babygo: being more Go-like • Independent from libc • string

    is a combination of a pointer and a length • make ABI (Application Binary Interface) more similar to that of the official Go
  53. babygo: Handwritten syscall syscall.Syscall: movq 8(%rsp), %rax # syscall number

    movq 16(%rsp), %rdi # arg0 movq 24(%rsp), %rsi # arg1 movq 32(%rsp), %rdx # arg2 syscall ret syscall.Syscall( uintptr(SYS_BRK), addr, uintptr(0), uintptr(0) ) runtime.s (callee) runtime.go (caller)
  54. ABI of official Go func sum(a int, b int) int

    { return a + b } TEXT "".sum(SB), ..., $0-24 MOVQ $0, "".~r2+24(SP) MOVQ "".a+8(SP), AX ADDQ "".b+16(SP), AX MOVQ AX, "".~r2+24(SP) RET source Go's Assembler
  55. ABI of babygo func sum(a int, b int) int {

    return a + b } main.sum: pushq %rbp movq %rsp, %rbp leaq 16(%rbp), %rax # address of a pushq %rax popq %rax movq 0(%rax), %rax # load value pushq %rax leaq 24(%rbp), %rax # address of b pushq %rax popq %rax movq 0(%rax), %rax # load value pushq %rax popq %rcx # right popq %rax # left addq %rcx, %rax pushq %rax popq %rax # returned value leave ret source GNU assembler
  56. import “go/ast” import “go/parser” func codegen() { … } func

    main() { … } • Write codegen first using go/parser, go/ast • Evaluate codegen design first 1st gen compiler compile package main func main() { … } test code babygo: Order of implementation
  57. 2nd gen compiler func scanner() { … } func parser()

    { … } func main() { … } • Write 2nd gen compiler with the minimum grammar that 1st gen supports • Re-invent go/* packages • Easy to debug codegen compile babygo: Order of implementation import “go/ast” import “go/parser” func codegen() { … } func main() { … } 1st gen compiler
  58. • 2nd gen can compile itself • 1st gen is

    not needed any more 2nd gen compiler func scanner() { …. } func parser() { …. } func codegen() { …. } func main() { …. } compile compile (self host) babygo: Order of implementation import “go/ast” import “go/parser” func codegen() { … } func main() { … } 1st gen compiler
  59. Achieved self-host again • with half time • with half

    lines of code (4,900 lines) ◦ Composed of only 3 files • main.go • runtime.go • runtime.s • with much higher readability
  60. Conclusion • Writing a Go compiler is not that hard

    ◦ as long as you don’t pursue a perfect one • Making something is the best way to understand it • This experience helped me understand and contribute to the official compiler
  61. Conclusion • If you want to learn compilers, ◦ I’d

    recommend babygo or chibicc as materials ▪ https://github.com/DQNEO/babygo ▪ https://github.com/rui314/chibicc ◦ Replaying the commit history is a good way
  62. Conclusion • No need to be a computer science expert

    beforehand • You can just get started
  63. About chibicc versions chibicc was renewed while I was working

    on this presentation. The old version I was referring to is here. https://github.com/rui314/chibicc/tree/historical/old
  64. How I learned assembly language • I didn’t read any

    book about assembly. • Googled • StackOverfolwed • Fed chibicc or gcc with small pieces of C code, and read the output assembly code • Official documentation (GAS, Intel CPU) are sometimes useful after you’ve got some knowledge
  65. Intel® 64 and IA-32 Architectures Software Developer’s Manual Intel’s manual

    can be helpful e.g. How to realize multiple returned values
  66. Refs • GNU Assembler ◦ https://sourceware.org/binutils/docs/as/ • Intel Software Developer

    Manuals ◦ https://software.intel.com/content/www/us/en/de velop/articles/intel-sdm.html#combined