-
-
Save magodo/16ee713b67a5c18f960851fc51dc08f6 to your computer and use it in GitHub Desktop.
Go: Source vs AST vs SSA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func hello(name string) { | |
if name == "" { | |
fmt.Print("name?") | |
name = readString() | |
} | |
fmt.Print("name") | |
} | |
func readString() string { return "" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 *ast.File { | |
1 . Package: main.go:1:1 | |
2 . Name: *ast.Ident { | |
3 . . NamePos: main.go:1:9 | |
4 . . Name: "main" | |
5 . } | |
6 . Decls: []ast.Decl (len = 3) { | |
7 . . 0: *ast.GenDecl { | |
8 . . . TokPos: main.go:3:1 | |
9 . . . Tok: import | |
10 . . . Lparen: - | |
11 . . . Specs: []ast.Spec (len = 1) { | |
12 . . . . 0: *ast.ImportSpec { | |
13 . . . . . Path: *ast.BasicLit { | |
14 . . . . . . ValuePos: main.go:3:8 | |
15 . . . . . . Kind: STRING | |
16 . . . . . . Value: "\"fmt\"" | |
17 . . . . . } | |
18 . . . . . EndPos: - | |
19 . . . . } | |
20 . . . } | |
21 . . . Rparen: - | |
22 . . } | |
23 . . 1: *ast.FuncDecl { | |
24 . . . Name: *ast.Ident { | |
25 . . . . NamePos: main.go:5:6 | |
26 . . . . Name: "hello" | |
27 . . . . Obj: *ast.Object { | |
28 . . . . . Kind: func | |
29 . . . . . Name: "hello" | |
30 . . . . . Decl: *(obj @ 23) | |
31 . . . . } | |
32 . . . } | |
33 . . . Type: *ast.FuncType { | |
34 . . . . Func: main.go:5:1 | |
35 . . . . Params: *ast.FieldList { | |
36 . . . . . Opening: main.go:5:11 | |
37 . . . . . List: []*ast.Field (len = 1) { | |
38 . . . . . . 0: *ast.Field { | |
39 . . . . . . . Names: []*ast.Ident (len = 1) { | |
40 . . . . . . . . 0: *ast.Ident { | |
41 . . . . . . . . . NamePos: main.go:5:12 | |
42 . . . . . . . . . Name: "name" | |
43 . . . . . . . . . Obj: *ast.Object { | |
44 . . . . . . . . . . Kind: var | |
45 . . . . . . . . . . Name: "name" | |
46 . . . . . . . . . . Decl: *(obj @ 38) | |
47 . . . . . . . . . } | |
48 . . . . . . . . } | |
49 . . . . . . . } | |
50 . . . . . . . Type: *ast.Ident { | |
51 . . . . . . . . NamePos: main.go:5:17 | |
52 . . . . . . . . Name: "string" | |
53 . . . . . . . } | |
54 . . . . . . } | |
55 . . . . . } | |
56 . . . . . Closing: main.go:5:23 | |
57 . . . . } | |
58 . . . } | |
59 . . . Body: *ast.BlockStmt { | |
60 . . . . Lbrace: main.go:5:25 | |
61 . . . . List: []ast.Stmt (len = 2) { | |
62 . . . . . 0: *ast.IfStmt { | |
63 . . . . . . If: main.go:6:2 | |
64 . . . . . . Cond: *ast.BinaryExpr { | |
65 . . . . . . . X: *ast.Ident { | |
66 . . . . . . . . NamePos: main.go:6:5 | |
67 . . . . . . . . Name: "name" | |
68 . . . . . . . . Obj: *(obj @ 43) | |
69 . . . . . . . } | |
70 . . . . . . . OpPos: main.go:6:10 | |
71 . . . . . . . Op: == | |
72 . . . . . . . Y: *ast.BasicLit { | |
73 . . . . . . . . ValuePos: main.go:6:13 | |
74 . . . . . . . . Kind: STRING | |
75 . . . . . . . . Value: "\"\"" | |
76 . . . . . . . } | |
77 . . . . . . } | |
78 . . . . . . Body: *ast.BlockStmt { | |
79 . . . . . . . Lbrace: main.go:6:16 | |
80 . . . . . . . List: []ast.Stmt (len = 2) { | |
81 . . . . . . . . 0: *ast.ExprStmt { | |
82 . . . . . . . . . X: *ast.CallExpr { | |
83 . . . . . . . . . . Fun: *ast.SelectorExpr { | |
84 . . . . . . . . . . . X: *ast.Ident { | |
85 . . . . . . . . . . . . NamePos: main.go:7:3 | |
86 . . . . . . . . . . . . Name: "fmt" | |
87 . . . . . . . . . . . } | |
88 . . . . . . . . . . . Sel: *ast.Ident { | |
89 . . . . . . . . . . . . NamePos: main.go:7:7 | |
90 . . . . . . . . . . . . Name: "Print" | |
91 . . . . . . . . . . . } | |
92 . . . . . . . . . . } | |
93 . . . . . . . . . . Lparen: main.go:7:12 | |
94 . . . . . . . . . . Args: []ast.Expr (len = 1) { | |
95 . . . . . . . . . . . 0: *ast.BasicLit { | |
96 . . . . . . . . . . . . ValuePos: main.go:7:13 | |
97 . . . . . . . . . . . . Kind: STRING | |
98 . . . . . . . . . . . . Value: "\"name?\"" | |
99 . . . . . . . . . . . } | |
100 . . . . . . . . . . } | |
101 . . . . . . . . . . Ellipsis: - | |
102 . . . . . . . . . . Rparen: main.go:7:20 | |
103 . . . . . . . . . } | |
104 . . . . . . . . } | |
105 . . . . . . . . 1: *ast.AssignStmt { | |
106 . . . . . . . . . Lhs: []ast.Expr (len = 1) { | |
107 . . . . . . . . . . 0: *ast.Ident { | |
108 . . . . . . . . . . . NamePos: main.go:8:3 | |
109 . . . . . . . . . . . Name: "name" | |
110 . . . . . . . . . . . Obj: *(obj @ 43) | |
111 . . . . . . . . . . } | |
112 . . . . . . . . . } | |
113 . . . . . . . . . TokPos: main.go:8:8 | |
114 . . . . . . . . . Tok: = | |
115 . . . . . . . . . Rhs: []ast.Expr (len = 1) { | |
116 . . . . . . . . . . 0: *ast.CallExpr { | |
117 . . . . . . . . . . . Fun: *ast.Ident { | |
118 . . . . . . . . . . . . NamePos: main.go:8:10 | |
119 . . . . . . . . . . . . Name: "readString" | |
120 . . . . . . . . . . . . Obj: *ast.Object { | |
121 . . . . . . . . . . . . . Kind: func | |
122 . . . . . . . . . . . . . Name: "readString" | |
123 . . . . . . . . . . . . . Decl: *ast.FuncDecl { | |
124 . . . . . . . . . . . . . . Name: *ast.Ident { | |
125 . . . . . . . . . . . . . . . NamePos: main.go:14:6 | |
126 . . . . . . . . . . . . . . . Name: "readString" | |
127 . . . . . . . . . . . . . . . Obj: *(obj @ 120) | |
128 . . . . . . . . . . . . . . } | |
129 . . . . . . . . . . . . . . Type: *ast.FuncType { | |
130 . . . . . . . . . . . . . . . Func: main.go:14:1 | |
131 . . . . . . . . . . . . . . . Params: *ast.FieldList { | |
132 . . . . . . . . . . . . . . . . Opening: main.go:14:16 | |
133 . . . . . . . . . . . . . . . . Closing: main.go:14:17 | |
134 . . . . . . . . . . . . . . . } | |
135 . . . . . . . . . . . . . . . Results: *ast.FieldList { | |
136 . . . . . . . . . . . . . . . . Opening: - | |
137 . . . . . . . . . . . . . . . . List: []*ast.Field (len = 1) { | |
138 . . . . . . . . . . . . . . . . . 0: *ast.Field { | |
139 . . . . . . . . . . . . . . . . . . Type: *ast.Ident { | |
140 . . . . . . . . . . . . . . . . . . . NamePos: main.go:14:19 | |
141 . . . . . . . . . . . . . . . . . . . Name: "string" | |
142 . . . . . . . . . . . . . . . . . . } | |
143 . . . . . . . . . . . . . . . . . } | |
144 . . . . . . . . . . . . . . . . } | |
145 . . . . . . . . . . . . . . . . Closing: - | |
146 . . . . . . . . . . . . . . . } | |
147 . . . . . . . . . . . . . . } | |
148 . . . . . . . . . . . . . . Body: *ast.BlockStmt { | |
149 . . . . . . . . . . . . . . . Lbrace: main.go:14:26 | |
150 . . . . . . . . . . . . . . . List: []ast.Stmt (len = 1) { | |
151 . . . . . . . . . . . . . . . . 0: *ast.ReturnStmt { | |
152 . . . . . . . . . . . . . . . . . Return: main.go:14:28 | |
153 . . . . . . . . . . . . . . . . . Results: []ast.Expr (len = 1) { | |
154 . . . . . . . . . . . . . . . . . . 0: *ast.BasicLit { | |
155 . . . . . . . . . . . . . . . . . . . ValuePos: main.go:14:35 | |
156 . . . . . . . . . . . . . . . . . . . Kind: STRING | |
157 . . . . . . . . . . . . . . . . . . . Value: "\"\"" | |
158 . . . . . . . . . . . . . . . . . . } | |
159 . . . . . . . . . . . . . . . . . } | |
160 . . . . . . . . . . . . . . . . } | |
161 . . . . . . . . . . . . . . . } | |
162 . . . . . . . . . . . . . . . Rbrace: main.go:14:38 | |
163 . . . . . . . . . . . . . . } | |
164 . . . . . . . . . . . . . } | |
165 . . . . . . . . . . . . } | |
166 . . . . . . . . . . . } | |
167 . . . . . . . . . . . Lparen: main.go:8:20 | |
168 . . . . . . . . . . . Ellipsis: - | |
169 . . . . . . . . . . . Rparen: main.go:8:21 | |
170 . . . . . . . . . . } | |
171 . . . . . . . . . } | |
172 . . . . . . . . } | |
173 . . . . . . . } | |
174 . . . . . . . Rbrace: main.go:9:2 | |
175 . . . . . . } | |
176 . . . . . } | |
177 . . . . . 1: *ast.ExprStmt { | |
178 . . . . . . X: *ast.CallExpr { | |
179 . . . . . . . Fun: *ast.SelectorExpr { | |
180 . . . . . . . . X: *ast.Ident { | |
181 . . . . . . . . . NamePos: main.go:11:2 | |
182 . . . . . . . . . Name: "fmt" | |
183 . . . . . . . . } | |
184 . . . . . . . . Sel: *ast.Ident { | |
185 . . . . . . . . . NamePos: main.go:11:6 | |
186 . . . . . . . . . Name: "Print" | |
187 . . . . . . . . } | |
188 . . . . . . . } | |
189 . . . . . . . Lparen: main.go:11:11 | |
190 . . . . . . . Args: []ast.Expr (len = 1) { | |
191 . . . . . . . . 0: *ast.BasicLit { | |
192 . . . . . . . . . ValuePos: main.go:11:12 | |
193 . . . . . . . . . Kind: STRING | |
194 . . . . . . . . . Value: "\"name\"" | |
195 . . . . . . . . } | |
196 . . . . . . . } | |
197 . . . . . . . Ellipsis: - | |
198 . . . . . . . Rparen: main.go:11:18 | |
199 . . . . . . } | |
200 . . . . . } | |
201 . . . . } | |
202 . . . . Rbrace: main.go:12:1 | |
203 . . . } | |
204 . . } | |
205 . . 2: *(obj @ 123) | |
206 . } | |
207 . Scope: *ast.Scope { | |
208 . . Objects: map[string]*ast.Object (len = 2) { | |
209 . . . "readString": *(obj @ 120) | |
210 . . . "hello": *(obj @ 27) | |
211 . . } | |
212 . } | |
213 . Imports: []*ast.ImportSpec (len = 1) { | |
214 . . 0: *(obj @ 12) | |
215 . } | |
216 . Unresolved: []*ast.Ident (len = 4) { | |
217 . . 0: *(obj @ 50) | |
218 . . 1: *(obj @ 84) | |
219 . . 2: *(obj @ 180) | |
220 . . 3: *(obj @ 139) | |
221 . } | |
222 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Name: foo.hello | |
# Package: foo | |
# Location: /tmp/gofoo/main.go:5:6 | |
func hello(name string): | |
0: entry P:0 S:2 | |
t0 = name == "":string bool | |
if t0 goto 1 else 2 | |
1: if.then P:1 S:1 | |
t1 = new [1]interface{} (varargs) *[1]interface{} | |
t2 = &t1[0:int] *interface{} | |
t3 = make interface{} <- string ("name?":string) interface{} | |
*t2 = t3 | |
t4 = slice t1[:] []interface{} | |
t5 = fmt.Print(t4...) (n int, err error) | |
t6 = readString() string | |
jump 2 | |
2: if.done P:2 S:0 | |
t7 = new [1]interface{} (varargs) *[1]interface{} | |
t8 = &t7[0:int] *interface{} | |
t9 = make interface{} <- string ("name":string) interface{} | |
*t8 = t9 | |
t10 = slice t7[:] []interface{} | |
t11 = fmt.Print(t10...) (n int, err error) | |
return | |
# Name: foo.readString | |
# Package: foo | |
# Location: /tmp/gofoo/main.go:14:6 | |
func readString() string: | |
0: entry P:0 S:0 | |
return "":string | |
# Name: foo.init | |
# Package: foo | |
# Synthetic: package initializer | |
func init(): | |
0: entry P:0 S:2 | |
t0 = *init$guard bool | |
if t0 goto 2 else 1 | |
1: init.start P:1 S:1 | |
*init$guard = true:bool | |
t1 = fmt.init() () | |
jump 2 | |
2: init.done P:2 S:0 | |
return | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment