-
Notifications
You must be signed in to change notification settings - Fork 43
/
opcodes_marshal.go
581 lines (520 loc) · 14.2 KB
/
opcodes_marshal.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package yakvm
import (
"encoding/json"
"github.com/yaklang/yaklang/common/utils"
"reflect"
"strconv"
"google.golang.org/protobuf/encoding/protowire"
)
type CodesMarshaller struct {
sourceCode string
table map[int]*SymbolTable
}
func NewCodesMarshaller() *CodesMarshaller {
return &CodesMarshaller{sourceCode: "", table: map[int]*SymbolTable{}}
}
func (c *CodesMarshaller) SetSourceCode(s string) {
c.sourceCode = s
}
func (c *CodesMarshaller) Unmarshal(buf []byte) (*SymbolTable, []*Code, error) {
tbl, buf, err := c.consumeSymbolTable(buf)
if err != nil {
return nil, nil, utils.Errorf("consume symbol table failed: %s", err)
}
i, n := protowire.ConsumeVarint(buf)
buf = buf[n:]
codeLen := int(i)
codes := make([]*Code, codeLen)
for i := 0; i < codeLen; i++ {
codes[i], buf, err = c.consumeCode(buf)
if err != nil {
return nil, nil, utils.Errorf("unmarshal yakvm.Code failed: %s", err)
}
}
return tbl, codes, nil
}
func (c *CodesMarshaller) MarshalWithDebugInfo(tbl *SymbolTable, codes []*Code) ([]byte, error) {
return c.Marshal(tbl, codes)
}
func (c *CodesMarshaller) Marshal(tbl *SymbolTable, codes []*Code) ([]byte, error) {
var buf []byte
var err error
buf, err = c.marshalSymbolTable(buf, tbl)
if err != nil {
return nil, utils.Errorf("marshal symbol table: %s", err)
}
buf = protowire.AppendVarint(buf, uint64(len(codes)))
for _, code := range codes {
buf, err = c.marshalCode(buf, code)
if err != nil {
return nil, utils.Errorf("marshal code error: %s", err)
}
}
return buf, nil
}
func (c *CodesMarshaller) walkSymbolTable(tbl *SymbolTable) []*SymbolTable {
if tbl == nil {
return nil
}
var tables = []*SymbolTable{tbl}
for _, t := range tbl.children {
tables = append(tables, c.walkSymbolTable(t)...)
}
return tables
}
type SymbolTableDesc struct {
Verbose string
currentSymbolIndex int
Index int
Parent int
Children []int
NameToId map[string]int
}
func (c *CodesMarshaller) descToIns(desc *SymbolTableDesc, id int) *SymbolTable {
if id == 0 {
return nil
}
tbl := c.getSymbolTableById(desc.Index)
tbl.index = desc.Index
tbl.Verbose = desc.Verbose
tbl.currentSymbolIndex = desc.currentSymbolIndex
tbl.parent = c.getSymbolTableById(desc.Parent)
tbl.symbolToId = desc.NameToId
if len(desc.Children) != 0 {
tbl.children = make([]*SymbolTable, len(desc.Children))
for i, childId := range desc.Children {
tbl.children[i] = c.getSymbolTableById(childId)
}
}
return tbl
}
func (c *CodesMarshaller) getSymbolTableById(id int) *SymbolTable {
if tbl, ok := c.table[id]; ok {
return tbl
}
return nil
}
func (c *CodesMarshaller) consumeSymbolTable(buf []byte) (*SymbolTable, []byte, error) {
i, n := protowire.ConsumeVarint(buf)
buf = buf[n:]
blockLen := int(i)
c.table = make(map[int]*SymbolTable, blockLen)
var tableDesc = make(map[int]*SymbolTableDesc)
for index := 1; index <= blockLen; index++ {
tbl := &SymbolTableDesc{NameToId: make(map[string]int)}
// index
i, n = protowire.ConsumeVarint(buf)
tbl.Index = int(i)
buf = buf[n:]
// verbose
raw, n := protowire.ConsumeBytes(buf)
tbl.Verbose = string(raw)
buf = buf[n:]
// currentSymbolIndex
i, n = protowire.ConsumeVarint(buf)
tbl.currentSymbolIndex = int(i)
buf = buf[n:]
// parent
i, n = protowire.ConsumeVarint(buf)
tbl.Parent = int(i)
buf = buf[n:]
// children
i, n = protowire.ConsumeVarint(buf)
tbl.Children = make([]int, int(i))
buf = buf[n:]
for i := 0; i < len(tbl.Children); i++ {
childId, childIndexOffset := protowire.ConsumeVarint(buf)
if childIndexOffset <= 0 {
return nil, nil, utils.Error("consume children index failed")
}
buf = buf[childIndexOffset:]
tbl.Children[i] = int(childId)
}
// symbol name to id
i, n = protowire.ConsumeVarint(buf)
buf = buf[n:]
for index := 0; index < int(i); index++ {
name, n := protowire.ConsumeBytes(buf)
buf = buf[n:]
id, n := protowire.ConsumeVarint(buf)
buf = buf[n:]
tbl.NameToId[string(name)] = int(id)
}
tableDesc[tbl.Index] = tbl
}
// 先初始化symboltable
for index := 1; index <= blockLen; index++ {
c.table[index] = &SymbolTable{}
}
// 对symboltable赋值
for id, desc := range tableDesc {
c.descToIns(desc, id)
}
rootTable := c.table[1]
// 设置rootTable
rootTable.tableCount = blockLen
rootTable.idToSymbolTable = c.table
return rootTable, buf, nil
}
func (c *CodesMarshaller) marshalSymbolTable(buf []byte, tbl *SymbolTable) ([]byte, error) {
if tbl.parent != nil {
return nil, utils.Error("Symbol table is not root table.")
}
tables := c.walkSymbolTable(tbl)
buf = protowire.AppendVarint(buf, uint64(len(tables)))
for _, tbl := range tables {
// index - verbose(bytes) index(varint) parentIndex(varint) - childrenCount(varint) childrenIndex(varint) - symbolCount(varint) symbolName(bytes) symbolId(varint)
// index 最小为1
// index
buf = protowire.AppendVarint(buf, uint64(tbl.index))
// verbose
buf = protowire.AppendBytes(buf, []byte(tbl.Verbose))
// currentSymbolIndex
buf = protowire.AppendVarint(buf, uint64(tbl.currentSymbolIndex))
// parent
if tbl.parent == nil {
buf = protowire.AppendVarint(buf, 0)
} else {
buf = protowire.AppendVarint(buf, uint64(tbl.parent.index))
}
// children
buf = protowire.AppendVarint(buf, uint64(len(tbl.children)))
for _, child := range tbl.children {
buf = protowire.AppendVarint(buf, uint64(child.index))
}
// symbol name to id
buf = protowire.AppendVarint(buf, uint64(len(tbl.symbolToId)))
for name, id := range tbl.symbolToId {
buf = protowire.AppendBytes(buf, []byte(name))
buf = protowire.AppendVarint(buf, uint64(id))
}
}
return buf, nil
}
func (c *CodesMarshaller) consumeFunc(buf []byte) (*Function, []byte, error) {
f := &Function{
sourceCode: c.sourceCode,
id: 0,
name: "",
codes: nil,
symbolTable: nil,
paramSymbols: nil,
isVariableParameter: false,
}
results, n := protowire.ConsumeBytes(buf)
f.name = string(results)
buf = buf[n:]
i, n := protowire.ConsumeVarint(buf)
f.id = int(i)
buf = buf[n:]
i, n = protowire.ConsumeVarint(buf)
if i == 1 {
// 可变参数
f.isVariableParameter = true
}
buf = buf[n:]
// symbol table
i, n = protowire.ConsumeVarint(buf)
f.symbolTable = c.getSymbolTableById(int(i))
buf = buf[n:]
// param symbols
i, n = protowire.ConsumeVarint(buf)
f.paramSymbols = make([]int, int(i))
buf = buf[n:]
for index := range f.paramSymbols {
i, n = protowire.ConsumeVarint(buf)
f.paramSymbols[index] = int(i)
buf = buf[n:]
}
// codes
i, n = protowire.ConsumeVarint(buf)
f.codes = make([]*Code, int(i))
buf = buf[n:]
for index := range f.codes {
var err error
f.codes[index], buf, err = c.consumeCode(buf)
if err != nil {
return nil, nil, err
}
}
return f, buf, nil
}
func (c *CodesMarshaller) marshalFunc(buf []byte, f *Function) ([]byte, error) {
buf = protowire.AppendBytes(buf, []byte(f.name))
buf = protowire.AppendVarint(buf, uint64(f.id))
if f.isVariableParameter {
buf = protowire.AppendVarint(buf, uint64(1))
} else {
buf = protowire.AppendVarint(buf, uint64(0))
}
if f.symbolTable == nil {
return nil, utils.Error("marshal function failed: no symbol table")
}
buf = protowire.AppendVarint(buf, uint64(f.symbolTable.index))
buf = protowire.AppendVarint(buf, uint64(len(f.paramSymbols)))
for _, i := range f.paramSymbols {
buf = protowire.AppendVarint(buf, uint64(i))
}
buf = protowire.AppendVarint(buf, uint64(len(f.codes)))
var err error
for _, code := range f.codes {
buf, err = c.marshalCode(buf, code)
if err != nil {
return nil, utils.Errorf("marshal code error: %s", err)
}
}
return buf, nil
}
func (c *CodesMarshaller) consumeAny(buf []byte) (interface{}, []byte, error) {
i, n := protowire.ConsumeVarint(buf)
buf = buf[n:]
switch i {
case 1:
// function
var f *Function
var err error
f, buf, err = c.consumeFunc(buf)
if err != nil {
return nil, nil, err
}
return f, buf, nil
case 0:
// type
i, n := protowire.ConsumeVarint(buf)
buf = buf[n:]
if i == 0 {
return nil, buf, nil
}
var typ reflect.Type
kind := reflect.Kind(i)
switch kind {
case reflect.Int:
typ = literalReflectType_Int
case reflect.Int8:
typ = literalReflectType_Int8
case reflect.Int16:
typ = literalReflectType_Int16
case reflect.Int32:
typ = literalReflectType_Int32
case reflect.Int64:
typ = literalReflectType_Int64
case reflect.Uint:
typ = literalReflectType_Uint
case reflect.Uint8:
typ = literalReflectType_Uint8
case reflect.Uint16:
typ = literalReflectType_Uint16
case reflect.Uint32:
typ = literalReflectType_Uint32
case reflect.Uint64:
typ = literalReflectType_Uint64
case reflect.Float32:
typ = literalReflectType_Float32
case reflect.Float64:
typ = literalReflectType_Float64
case reflect.String:
typ = literalReflectType_String
case reflect.Bool:
typ = literalReflectType_Bool
case reflect.Map:
typ = reflect.MapOf(literalReflectType_String, literalReflectType_Interface)
case 27: // bytes
typ = reflect.SliceOf(literalReflectType_Byte)
}
if typ == nil {
return nil, nil, utils.Errorf("unsupported type: %s", kind)
}
refret := reflect.New(typ)
b, n := protowire.ConsumeBytes(buf)
buf = buf[n:]
if typ == literalReflectType_String {
ret, err := strconv.Unquote(string(b))
if err != nil {
return nil, nil, err
}
return ret, buf, nil
} else {
err := json.Unmarshal(b, refret.Interface())
ret := refret.Elem().Interface()
if err != nil {
return nil, nil, err
}
return ret, buf, nil
}
case 2: // []*Code
iInt, n := protowire.ConsumeVarint(buf)
buf = buf[n:]
codeLen := int(iInt)
codes := make([]*Code, codeLen)
var err error
for i := 0; i < codeLen; i++ {
codes[i], buf, err = c.consumeCode(buf)
if err != nil {
return nil, nil, utils.Errorf("consume code in Any(Op) failed: %s", err)
}
}
return codes, buf, nil
default:
return nil, nil, utils.Errorf("cannot parse obj: %v", i)
}
}
func (c *CodesMarshaller) marshalAny(buf []byte, i interface{}) ([]byte, error) {
var err error
switch ret := i.(type) {
case []*Code:
buf = protowire.AppendVarint(buf, 2)
buf = protowire.AppendVarint(buf, uint64(len(ret)))
for _, code := range ret {
buf, err = c.marshalCode(buf, code)
if err != nil {
return nil, utils.Errorf("marshan Any.Codes in Op failed: %s", err)
}
}
return buf, nil
case *Function:
buf = protowire.AppendVarint(buf, 1)
buf, err = c.marshalFunc(buf, ret)
if err != nil {
return nil, utils.Errorf("marshal function failed: %s", err)
}
return buf, nil
default:
buf = protowire.AppendVarint(buf, 0)
// isNil
if i == nil {
buf = protowire.AppendVarint(buf, 0)
return buf, nil
} else {
switch v := i.(type) {
case string: // string 直接quote,不需要json序列化
buf = protowire.AppendVarint(buf, uint64(reflect.TypeOf(i).Kind()))
buf = protowire.AppendBytes(buf, []byte(strconv.Quote(v)))
return buf, nil
case []byte: // 对[]byte做特殊处理
buf = protowire.AppendVarint(buf, 27)
default:
buf = protowire.AppendVarint(buf, uint64(reflect.TypeOf(i).Kind()))
}
bufBytes, err := json.Marshal(i)
if err != nil {
return nil, err
}
buf = protowire.AppendBytes(buf, bufBytes)
return buf, nil
}
}
}
func (c *CodesMarshaller) consumeValue(buf []byte) (*Value, []byte, error) {
v := &Value{
TypeVerbose: "",
Value: nil,
Literal: "",
SymbolId: 0,
CallerRef: nil,
CalleeRef: nil,
}
i, n := protowire.ConsumeBytes(buf)
buf = buf[n:]
v.TypeVerbose = string(i)
i, n = protowire.ConsumeBytes(buf)
buf = buf[n:]
v.Literal = string(i)
// value
var err error
v.Value, buf, err = c.consumeAny(buf)
if err != nil {
return nil, nil, utils.Errorf("consume any value error: %v", err)
}
return v, buf, nil
}
func (c *CodesMarshaller) marshalValue(buf []byte, v *Value) ([]byte, error) {
buf = protowire.AppendBytes(buf, []byte(v.TypeVerbose))
buf = protowire.AppendBytes(buf, []byte(v.Literal))
var err error
buf, err = c.marshalAny(buf, v.Value)
if err != nil {
return nil, err
}
return buf, nil
}
func (c *CodesMarshaller) consumeCode(buf []byte) (*Code, []byte, error) {
code := &Code{}
i, n := protowire.ConsumeVarint(buf)
code.Opcode = OpcodeFlag(i)
buf = buf[n:]
i, n = protowire.ConsumeVarint(buf)
code.Unary = int(i)
buf = buf[n:]
var err error
i, n = protowire.ConsumeVarint(buf)
buf = buf[n:]
if i == 1 {
// 有 op1
code.Op1, buf, err = c.consumeValue(buf)
if err != nil {
return nil, nil, utils.Errorf("consume value(op1) failed: %v", err)
}
}
i, n = protowire.ConsumeVarint(buf)
buf = buf[n:]
if i == 1 {
code.Op2, buf, err = c.consumeValue(buf)
if err != nil {
return nil, nil, utils.Errorf("consume value(op2) failed: %v", err)
}
}
i, n = protowire.ConsumeVarint(buf)
buf = buf[n:]
if i == 1 {
i, n = protowire.ConsumeVarint(buf)
code.StartLineNumber = int(i)
buf = buf[n:]
i, n = protowire.ConsumeVarint(buf)
code.StartColumnNumber = int(i)
buf = buf[n:]
i, n = protowire.ConsumeVarint(buf)
code.EndLineNumber = int(i)
buf = buf[n:]
i, n = protowire.ConsumeVarint(buf)
code.EndColumnNumber = int(i)
buf = buf[n:]
}
return code, buf, nil
}
func (c *CodesMarshaller) marshalCode(buf []byte, code *Code) ([]byte, error) {
buf = protowire.AppendVarint(buf, uint64(code.Opcode))
buf = protowire.AppendVarint(buf, uint64(code.Unary))
if code.Op1 == nil {
// no op1
buf = protowire.AppendVarint(buf, 0)
} else {
// have op1
buf = protowire.AppendVarint(buf, 1)
var err error
buf, err = c.marshalValue(buf, code.Op1)
if err != nil {
return nil, err
}
}
if code.Op2 == nil {
// have op2
buf = protowire.AppendVarint(buf, 0)
} else {
buf = protowire.AppendVarint(buf, 1)
var err error
buf, err = c.marshalValue(buf, code.Op2)
if err != nil {
return nil, err
}
}
if code.StartLineNumber > 0 {
buf = protowire.AppendVarint(buf, 1)
buf = protowire.AppendVarint(buf, uint64(code.StartLineNumber))
buf = protowire.AppendVarint(buf, uint64(code.StartColumnNumber))
buf = protowire.AppendVarint(buf, uint64(code.EndLineNumber))
buf = protowire.AppendVarint(buf, uint64(code.EndColumnNumber))
} else {
buf = protowire.AppendVarint(buf, 0)
}
return buf, nil
}