-
Notifications
You must be signed in to change notification settings - Fork 3
/
binary.go
381 lines (336 loc) · 6.8 KB
/
binary.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package fakego
import (
"math"
"strconv"
)
type command uint64
var EMPTY_CMD = command(math.MaxInt64)
const (
COMMAND_OPCODE = iota
COMMAND_ADDR
COMMAND_POS
)
const (
OPCODE_ASSIGN = iota
OPCODE_PLUS
OPCODE_MINUS
OPCODE_MULTIPLY
OPCODE_DIVIDE
OPCODE_DIVIDE_MOD
OPCODE_STRING_CAT
OPCODE_PLUS_ASSIGN
OPCODE_MINUS_ASSIGN
OPCODE_MULTIPLY_ASSIGN
OPCODE_DIVIDE_ASSIGN
OPCODE_DIVIDE_MOD_ASSIGN
OPCODE_RETURN
OPCODE_JNE
OPCODE_JMP
OPCODE_AND
OPCODE_OR
OPCODE_LESS
OPCODE_MORE
OPCODE_EQUAL
OPCODE_MOREEQUAL
OPCODE_LESSEQUAL
OPCODE_NOTEQUAL
OPCODE_NOT
OPCODE_AND_JNE
OPCODE_OR_JNE
OPCODE_LESS_JNE
OPCODE_MORE_JNE
OPCODE_EQUAL_JNE
OPCODE_MOREEQUAL_JNE
OPCODE_LESSEQUAL_JNE
OPCODE_NOTEQUAL_JNE
OPCODE_NOT_JNE
OPCODE_CALL
OPCODE_FOR
OPCODE_MAX
)
const (
ADDR_STACK = iota
ADDR_CONST
ADDR_CONTAINER
)
type container_addr struct {
con command
key command
}
const (
CALL_NORMAL = iota
CALL_FAKE
CALL_CLASSMEM
)
func _MAKE_COMMAND(ty int, code int) command {
return command(_MAKEINT64(int32(ty), int32(code)))
}
func _MAKE_ADDR(addrtype int, pos int) command {
return _MAKE_COMMAND(COMMAND_ADDR, int(_MAKEINT32(int16(addrtype), int16(pos))))
}
func _MAKE_OPCODE(op int) command {
return _MAKE_COMMAND(COMMAND_OPCODE, op)
}
func _MAKE_POS(pos int) command {
return _MAKE_COMMAND(COMMAND_POS, pos)
}
func _COMMAND_TYPE(cmd command) int {
return int(_HIINT32(int64(cmd)))
}
func _COMMAND_CODE(cmd command) int {
return int(_LOINT32(int64(cmd)))
}
func _ADDR_TYPE(code int) int {
return int(_HIINT16(int32(code)))
}
func _ADDR_POS(code int) int {
return int(_LOINT16(int32(code)))
}
func opcodeStr(opcode int) string {
switch opcode {
case OPCODE_ASSIGN:
return "OPCODE_ASSIGN"
case OPCODE_PLUS:
return "OPCODE_PLUS"
case OPCODE_MINUS:
return "OPCODE_MINUS"
case OPCODE_MULTIPLY:
return "OPCODE_MULTIPLY"
case OPCODE_DIVIDE:
return "OPCODE_DIVIDE"
case OPCODE_DIVIDE_MOD:
return "OPCODE_DIVIDE_MOD"
case OPCODE_STRING_CAT:
return "OPCODE_STRING_CAT"
case OPCODE_PLUS_ASSIGN:
return "OPCODE_PLUS_ASSIGN"
case OPCODE_MINUS_ASSIGN:
return "OPCODE_MINUS_ASSIGN"
case OPCODE_MULTIPLY_ASSIGN:
return "OPCODE_MULTIPLY_ASSIGN"
case OPCODE_DIVIDE_ASSIGN:
return "OPCODE_DIVIDE_ASSIGN"
case OPCODE_DIVIDE_MOD_ASSIGN:
return "OPCODE_DIVIDE_MOD_ASSIGN"
case OPCODE_RETURN:
return "OPCODE_RETURN"
case OPCODE_JNE:
return "OPCODE_JNE"
case OPCODE_JMP:
return "OPCODE_JMP"
case OPCODE_AND:
return "OPCODE_AND"
case OPCODE_OR:
return "OPCODE_OR"
case OPCODE_LESS:
return "OPCODE_LESS"
case OPCODE_MORE:
return "OPCODE_MORE"
case OPCODE_EQUAL:
return "OPCODE_EQUAL"
case OPCODE_MOREEQUAL:
return "OPCODE_MOREEQUAL"
case OPCODE_LESSEQUAL:
return "OPCODE_LESSEQUAL"
case OPCODE_NOTEQUAL:
return "OPCODE_NOTEQUAL"
case OPCODE_NOT:
return "OPCODE_NOT"
case OPCODE_AND_JNE:
return "OPCODE_AND_JNE"
case OPCODE_OR_JNE:
return "OPCODE_OR_JNE"
case OPCODE_LESS_JNE:
return "OPCODE_LESS_JNE"
case OPCODE_MORE_JNE:
return "OPCODE_MORE_JNE"
case OPCODE_EQUAL_JNE:
return "OPCODE_EQUAL_JNE"
case OPCODE_MOREEQUAL_JNE:
return "OPCODE_MOREEQUAL_JNE"
case OPCODE_LESSEQUAL_JNE:
return "OPCODE_LESSEQUAL_JNE"
case OPCODE_NOTEQUAL_JNE:
return "OPCODE_NOTEQUAL_JNE"
case OPCODE_NOT_JNE:
return "OPCODE_NOT_JNE"
case OPCODE_CALL:
return "OPCODE_CALL"
case OPCODE_FOR:
return "OPCODE_FOR"
}
return "unknow"
}
type stack_variant_info struct {
name string
line int
pos int
}
type func_binary struct {
// 最大栈空间
maxstack int
// 参数个数
paramnum int
// 名字
name string
// 文件名
filename string
// 包名
packagename string
// 二进制缓冲区
buff []command
// 二进制行号缓冲区
lineno_buff []int
// 常量
const_list []variant
// container地址
container_addr_list []container_addr
// 调试信息,栈变量
debug_stack_variant_info []stack_variant_info
}
func dump_addr(code int) string {
ret := ""
addrtype := _HIINT16(int32(code))
pos := _LOINT16(int32(code))
switch addrtype {
case ADDR_STACK:
ret += "STACK"
break
case ADDR_CONST:
ret += "CONST"
break
case ADDR_CONTAINER:
ret += "CONTAINER"
break
default:
ret += "unknow "
ret += strconv.Itoa(int(addrtype))
}
ret += "\t"
ret += strconv.Itoa(int(pos))
return ret
}
func (fb *func_binary) dump(pos int) string {
ret := ""
// 名字
ret += "\n["
ret += fb.name
ret += "]\n"
// 最大栈
ret += "\tmaxstack:\t"
ret += strconv.Itoa(fb.maxstack)
ret += "\n\n"
// 常量表
ret += "\t////// const define "
ret += strconv.Itoa(len(fb.const_list))
ret += " //////\n"
for i := 0; i < len(fb.const_list); i++ {
ret += "\t["
ret += strconv.Itoa(i)
ret += "]\t"
ret += vartypetostring(fb.const_list[i].ty)
ret += "\t"
ret += vartostring(fb.const_list[i])
ret += "\n"
}
// 容器地址表
ret += "\n\t////// container addr "
ret += strconv.Itoa(len(fb.container_addr_list))
ret += " //////\n"
for i := 0; i < len(fb.container_addr_list); i++ {
ret += "\t["
ret += strconv.Itoa(i)
ret += "]\t"
concmd := fb.container_addr_list[i].con
concode := _COMMAND_CODE(concmd)
ret += "[ CONTAINER ]\t"
ret += dump_addr(concode)
keycmd := fb.container_addr_list[i].key
keycode := _COMMAND_CODE(keycmd)
ret += "\t[ KEY ]\t"
ret += dump_addr(keycode)
ret += "\n"
}
// 变量地址
ret += "\n\t////// stack variant addr "
ret += strconv.Itoa(len(fb.debug_stack_variant_info))
ret += " //////\n"
for i := 0; i < len(fb.debug_stack_variant_info); i++ {
info := fb.debug_stack_variant_info[i]
ret += "\t["
ret += strconv.Itoa(info.pos)
ret += "]\t"
ret += info.name
ret += "\t\tLINE\t"
ret += strconv.Itoa(info.line)
ret += "\n"
}
ret += "\n\t////// byte code "
ret += strconv.Itoa(len(fb.buff))
ret += " //////\n"
// 字节码
for i := 0; i < len(fb.buff); i++ {
cmd := fb.buff[i]
ty := _COMMAND_TYPE(cmd)
code := _COMMAND_CODE(cmd)
if i == pos {
ret += "->"
}
ret += "\t["
ret += strconv.Itoa(i)
ret += "]"
ret += "[LINE "
ret += strconv.Itoa(fb.lineno_buff[i])
ret += "]\t"
ret += fkxtoa(cmd, 16)
ret += "\t"
switch ty {
case COMMAND_OPCODE:
{
ret += "["
ret += opcodeStr(code)
ret += "]\t"
}
break
case COMMAND_ADDR:
{
ret += "[ ADDR ]\t"
ret += dump_addr(code)
}
break
case COMMAND_POS:
{
ret += "[ POS ]\t"
ret += strconv.Itoa(code)
}
break
default:
{
ret += "[unknow]\t"
}
break
}
ret += "\n"
}
ret += "\n"
return ret
}
func (fb *func_binary) binary_size() int {
return len(fb.buff)
}
type binary struct {
}
func (b *binary) dump() string {
str := ""
gfs.fm.shh.Range(func(key, value interface{}) bool {
f := value.(*funcunion)
if f.havefb {
str += f.fb.dump(-1)
}
return true
})
return str
}
func (b *binary) add_func(name variant, bin *func_binary) {
gfs.fm.add_func(name, bin)
}