Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Writing a compiler in go

2,147 views

Published on

mercari.go #6

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Writing a compiler in go

  1. 1. Writing a Compiler in Go mercari.go #6 @kitasuke
  2. 2. About me • Yusuke Kita - @kitasuke • Mercari US • Backend/Frontend • Swift Compiler Contributor
  3. 3. Writing an Interpreter in Go
  4. 4. Writing a Compiler in Go
  5. 5. kitasuke/monkey-go
  6. 6. kitasuke/monkey- swift
  7. 7. Go Compiler
  8. 8. Go package main func main() { s := sum(2, 3) println(s) } func sum(x, y int) int { z := x + y return z }
  9. 9. ast *ast.File { Package: 1:1 Name: *ast.Ident { Name: "main" } Decls: []ast.Decl (len = 2) { 0: *ast.FuncDecl { Name: *ast.Ident { Name: "main" Obj: *ast.Object { Kind: func Name: "main" } } Type: *ast.FuncType {...} Body: *ast.BlockStmt {...} } 1: *(obj @ 49) } ... }
  10. 10. IR $ env GOSSAFUNC=sum go build sample.go && open ssa.html b1: v1 (?) = InitMem <mem> v2 (?) = SP <uintptr> v6 (?) = LocalAddr <*int> {~r2} v2 v1 v7 (8) = Arg <int> {x} v8 (8) = Arg <int> {y} v10 (+9) = Add64 <int> v7 v8 (z[int]) v11 (10) = VarDef <mem> {~r2} v1 v12 (+10) = Store <mem> {int} v6 v10 v11 Ret v12 (line +10) name z[int]: v10
  11. 11. ssa $ env GOSSAFUNC=sum go build sample.go && open ssa.html 00000 (8) TEXT "".sum(SB) 00001 (8) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB) 00002 (8) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB) 00003 (8) FUNCDATA $3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB) v6 00004 (+9) PCDATA $2, $0 v6 00005 (+9) PCDATA $0, $0 v6 00006 (+9) MOVQ "".y+8(SP), AX v9 00007 (9) MOVQ "".x(SP), CX v10 00008 (9) ADDQ CX, AX v12 00009 (+10) MOVQ AX, "".~r2+16(SP) b1 00010 (10) RET 00011 (?) END
  12. 12. Monkey Compiler
  13. 13. Interpreter Modules • token • lexer • parser • ast • object • evaluater • repl
  14. 14. Compiler Modules • code • compiler • vm
  15. 15. token const ( Identifier = "Identifier" // add, x ,y, ... Int = "Int" // 123456 String = "String" // "x", "y" Assign = "=" Plus = "+" ... Function = "Function" Let = "Let" If = "If" Else = "Else" Return = "Return" )
  16. 16. lexer let|x|=|5 let|identifier|equal|int
  17. 17. ast let x = 5 &LetStatement{ Token: token.Token{Type: token.Let, Literal: "let"}, Name: &Identifier{ Token: token.Token{Type: token.Identifier, Literal: "x"}, }, Value: &IntegerLiteral{ Token: token.Token{Type: token.Int, Literal: "5"}, }, }
  18. 18. object type Integer struct { Value int64 } type Null struct{} type Array struct { Elements []Object }
  19. 19. evaluater let x = 5 &object.Integer{Value: 5}
  20. 20. code const ( OpConstant Opcode = iota OpPop OpAdd OpSub OpMul OpDiv OpTrue OpFalse ... )
  21. 21. compiler let x = 5; x; 0000 OpConstant 0 0003 OpSetGlobal 0 0006 OpGetGlobal 0 0009 OpPop
  22. 22. vm let x = 5; x; 5
  23. 23. Demo
  24. 24. Takeaway
  25. 25. Takeaway • Ideas of how compiler works
  26. 26. Takeaway • Ideas of how compiler works • Better understanding about Go
  27. 27. Takeaway • Ideas of how compiler works • Better understanding about Go • Good start for static code analysis tools
  28. 28. References • Writing an Interpreter in Go • Writing a Compiler in Go • Debugging code generation in Go • How a Go Program Compiles down to Machine Code • Looking at your program’s structure in Go 1.7

×