-
Notifications
You must be signed in to change notification settings - Fork 8
/
debug.go
269 lines (259 loc) · 5.2 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import "fmt"
func (ctype *Ctype) c2s_int(dict map[string]bool) string {
if ctype == nil {
return "(nil)"
}
switch ctype.typ {
case CTYPE_VOID:
return "void"
case CTYPE_CHAR:
return "char"
case CTYPE_SHORT:
return "short"
case CTYPE_INT:
return "int"
case CTYPE_LONG:
return "long"
case CTYPE_LLONG:
return "long long"
case CTYPE_FLOAT:
return "float"
case CTYPE_LDOUBLE:
return "long double"
case CTYPE_DOUBLE:
return "double"
case CTYPE_PTR:
return format("*%s", ctype.ptr.c2s_int(dict))
case CTYPE_ARRAY:
return format("[%d]%s", ctype.len, ctype.ptr.c2s_int(dict))
case CTYPE_STRUCT:
if dict[format("%p", ctype)] {
return "(struct"
}
dict[format("%p", ctype)] = true
s := "(struct"
for _, v := range ctype.fields.Values() {
field := v
s += format(" (%s)", field.ctype.c2s_int(dict))
}
s += ")"
return s
case CTYPE_FUNC:
s := format("(")
params := ctype.params
for i, t := range params {
s += format("%s", t.c2s_int(dict))
if i != len(params)-1 {
s += ","
}
}
s += format(") -> %s", ctype.rettype.c2s_int(dict))
return s
default:
fmt.Sprintf("Unknown ctype: %d", ctype.typ)
}
return ""
}
func (ctype *Ctype) String() string {
dict := make(map[string]bool)
return ctype.c2s_int(dict)
}
type Block []*Ast
func uop_to_string(op string, ast *Ast) string {
return format("(%s %s)", op, ast.operand)
}
func binop_to_string(op string, ast *Ast) string {
return format("(%s %s %s)", op, ast.left, ast.right)
}
func (ast *Ast) String() string {
if ast == nil {
return "(nil)"
}
switch ast.typ {
case AST_LITERAL:
switch ast.ctype.typ {
case CTYPE_CHAR:
if ast.ival == '\n' {
return "'\n'"
} else if ast.ival == '\\' {
return "'\\\\'"
} else if ast.ival == 0 {
return "'\\0'"
} else {
return format("'%c'", ast.ival)
}
case CTYPE_INT:
return format("%d", ast.ival)
case CTYPE_LONG:
return format("%dL", ast.ival)
case CTYPE_FLOAT, CTYPE_DOUBLE:
return format("%f", ast.fval)
default:
errorf("internal error")
return ""
}
case AST_STRING:
return format("\"%s\"", quote_cstring(ast.val))
case AST_LVAR:
return format("%s", ast.varname)
case AST_GVAR:
return format("%s", ast.varname)
case AST_FUNCALL:
s := format("(%s)%s(", ast.ctype, ast.fname)
for i, v := range ast.args {
s += v.String()
if i < len(ast.args)-1 {
s += ","
}
}
s += ")"
return s
case AST_FUNC:
s := format("(%s)%s(",
ast.ctype,
ast.fname)
for i, p := range ast.params {
s += format("%s %s", p.ctype, p)
if i < (len(ast.params) - 1) {
s += ","
}
}
s += ")"
s += ast.body.String()
return s
case AST_DECL:
var vname string
if ast.declvar.typ == AST_GVAR {
vname = ast.declvar.varname
} else {
vname = ast.declvar.varname
}
s := format("(decl %s %s",
ast.declvar.ctype,
vname)
if ast.declinit != nil {
s += format(" %s", ast.declinit)
}
s += ")"
return s
case AST_INIT_LIST:
s := "{"
for i, v := range ast.initlist {
s += v.String()
if i != len(ast.initlist)-1 {
s += ","
}
}
s += "}"
return s
case AST_IF:
s := format("(if %s %s",
ast.cond,
ast.then)
if ast.els != nil {
s += format(" %s", ast.els)
}
s += ")"
return s
case AST_TERNARY:
return format("(? %s %s %s)",
ast.cond,
ast.then,
ast.els)
case AST_FOR:
s := format("(for %s %s %s %s)",
ast.init,
ast.cond,
ast.step,
ast.body)
return s
case AST_RETURN:
return format("(return %s)", ast.retval)
case AST_COMPOUND_STMT:
s := "{"
for _, v := range ast.stmts {
s += v.String()
s += ";"
}
s += "}"
return s
case AST_STRUCT_REF:
s := ast.struc.String()
s += "."
s += ast.field
return s
case AST_ADDR:
return uop_to_string("addr", ast)
case AST_DEREF:
return uop_to_string("deref", ast)
case OP_GE:
return binop_to_string( ">=", ast)
case OP_LE:
return binop_to_string("<=", ast)
case OP_NE:
return binop_to_string("!=", ast)
case OP_INC:
return uop_to_string("++", ast)
case OP_DEC:
return uop_to_string("--", ast)
case OP_LOGAND:
return binop_to_string("and", ast)
case OP_LOGOR:
return binop_to_string("or", ast)
case '!':
return uop_to_string("!", ast)
case '&':
return binop_to_string("&", ast)
case '|':
return binop_to_string("|", ast)
default:
left := ast.left
right := ast.right
var s string
if ast.typ == OP_EQ {
s += "(== "
} else {
s += format("(%c ", ast.typ)
}
s += format("%s %s)", left, right)
return s
}
}
func (tok *Token) String() string {
if tok == nil {
return "(null)"
}
switch tok.typ {
case TTYPE_IDENT:
return tok.sval
case TTYPE_PUNCT:
if tok.is_punct(OP_EQ) {
return "=="
} else if tok.is_punct(OP_NE) {
return "!="
}
return format("%c", tok.punct)
case TTYPE_CHAR:
return format("%c", tok.c)
case TTYPE_NUMBER:
return tok.sval
case TTYPE_STRING:
return format("\"%s\"", tok.sval)
case TTYPE_NEWLINE:
return "(newline)"
case TTYPE_SPACE:
return "(space)"
case TTYPE_MACRO_PARAM:
return "(macro-param)"
}
errorf("internal error: unknown token type: %d", tok.typ)
return ""
}
func (tokens TokenList) String() string {
r := ""
for _, token := range tokens {
r = r + token.String() + " "
}
return r
}