-
Notifications
You must be signed in to change notification settings - Fork 15
/
stack.go
626 lines (582 loc) · 14.3 KB
/
stack.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package tlb
import (
"errors"
"fmt"
"io"
"math/big"
"reflect"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tl"
)
// VmStack
// vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack;
// vm_stk_cons#_ {n:#} rest:^(VmStackList n) tos:VmStackValue = VmStackList (n + 1);
// vm_stk_nil#_ = VmStackList 0;
type VmStack []VmStackValue
// VmCont
// _ cregs:(HashmapE 4 VmStackValue) = VmSaveList;
// vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList
// cp:(Maybe int16) = VmControlData;
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
// vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
// vmc_quit$1000 exit_code:int32 = VmCont;
// vmc_quit_exc$1001 = VmCont;
// vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont;
// vmc_until$110000 body:^VmCont after:^VmCont = VmCont;
// vmc_again$110001 body:^VmCont = VmCont;
// vmc_while_cond$110010 cond:^VmCont body:^VmCont
// after:^VmCont = VmCont;
// vmc_while_body$110011 cond:^VmCont body:^VmCont
// after:^VmCont = VmCont;
// vmc_pushint$1111 value:int32 next:^VmCont = VmCont;
type VmCont struct {
// TODO: implement
}
// VmStkTuple
// Custom type: len:(## 16) data:(VmTuple len). Tag excluded. Use with VmStackValue type.
// vm_tupref_nil$_ = VmTupleRef 0;
// vm_tupref_single$_ entry:^VmStackValue = VmTupleRef 1;
// vm_tupref_any$_ {n:#} ref:^(VmTuple (n + 2)) = VmTupleRef (n + 2);
// vm_tuple_nil$_ = VmTuple 0;
// vm_tuple_tcons$_ {n:#} head:(VmTupleRef n) tail:^VmStackValue = VmTuple (n + 1);
// vm_stk_tuple#07 len:(## 16) data:(VmTuple len) = VmStackValue;
type VmStkTuple struct {
Len uint16
Data *VmTuple
}
type VmTuple struct {
Head VmTupleRef
Tail VmStackValue `tlb:"^"`
}
type VmTupleRef struct {
Entry *VmStackValue `tlb:"^"`
Ref *VmTuple `tlb:"^"`
}
func (t VmStkTuple) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
// TODO: implement
return fmt.Errorf("VmStkTuple TLB marshaling not implemented")
}
func (t *VmStkTuple) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
l, err := c.ReadUint(16)
if err != nil {
return err
}
t.Len = uint16(l)
t.Data, err = vmTupleInner(t.Len, c)
if err != nil {
return err
}
return nil
}
func vmTupleInner(n uint16, c *boc.Cell) (*VmTuple, error) {
if n > 0 {
vmTuple := VmTuple{}
n -= 1
head, err := vmTupleRefInner(n, c)
if err != nil {
return nil, err
}
if head != nil {
vmTuple.Head = *head
}
c1, err := c.NextRef()
if err != nil {
return nil, err
}
vmStackValue := VmStackValue{}
err = Unmarshal(c1, &vmStackValue)
if err != nil {
return nil, err
}
vmTuple.Tail = vmStackValue
return &vmTuple, nil
}
return nil, nil
}
func vmTupleRefInner(n uint16, c *boc.Cell) (*VmTupleRef, error) {
vmTupleRef := VmTupleRef{}
if n == 1 {
c1, err := c.NextRef()
if err != nil {
return nil, err
}
vmStackValue := VmStackValue{}
Unmarshal(c1, &vmStackValue)
vmTupleRef.Entry = &vmStackValue
return &vmTupleRef, nil
} else if n > 1 {
c1, err := c.NextRef()
if err != nil {
return nil, err
}
ref, err := vmTupleInner(n, c1)
if err != nil {
return nil, err
}
vmTupleRef.Ref = ref
return &vmTupleRef, nil
}
return nil, nil
}
func (t *VmTuple) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
return fmt.Errorf("VmTuple TLB unmarshaling not implemented")
}
// VmCellSlice
// _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits }
// st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice;
type VmCellSlice struct {
cell *boc.Cell
stBits int
endBits int
stRef int
endRef int
}
// VmStackValue
// vm_stk_null#00 = VmStackValue;
// vm_stk_tinyint#01 value:int64 = VmStackValue;
// vm_stk_int#0201_ value:int257 = VmStackValue;
// vm_stk_nan#02ff = VmStackValue;
// vm_stk_cell#03 cell:^Cell = VmStackValue;
// vm_stk_slice#04 _:VmCellSlice = VmStackValue;
// vm_stk_builder#05 cell:^Cell = VmStackValue;
// vm_stk_cont#06 cont:VmCont = VmStackValue;
// vm_stk_tuple#07 len:(## 16) data:(VmTuple len) = VmStackValue;
type VmStackValue struct {
SumType
VmStkNull struct{} `tlbSumType:"vm_stk_null#00"`
VmStkTinyInt int64 `tlbSumType:"vm_stk_tinyint#01"`
VmStkInt Int257 `tlbSumType:"vm_stk_int$000000100000000"` // vm_stk_int#0201_
VmStkNan struct{} `tlbSumType:"vm_stk_nan#02ff"`
VmStkCell Ref[boc.Cell] `tlbSumType:"vm_stk_cell#03"`
VmStkSlice VmCellSlice `tlbSumType:"vm_stk_slice#04"`
VmStkBuilder Ref[boc.Cell] `tlbSumType:"vm_stk_builder#05"`
VmStkCont VmCont `tlbSumType:"vm_stk_cont#06"`
VmStkTuple VmStkTuple `tlbSumType:"vm_stk_tuple#07"`
}
func (s VmStack) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
depth := uint64(len(s))
err := c.WriteUint(depth, 24)
if err != nil {
return err
}
err = putStackListItems(c, s)
return err
}
func (s *VmStack) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
depth, err := c.ReadUint(24)
if err != nil {
return err
}
if depth == 0 {
return nil
}
list, err := getStackListItems(c, depth)
if err != nil {
return err
}
*s = list
return nil
}
func getStackListItems(c *boc.Cell, depth uint64) ([]VmStackValue, error) {
var (
res []VmStackValue
tos VmStackValue
)
if depth == 0 {
return nil, nil
}
restCell, err := c.NextRef()
if err != nil {
return nil, err
}
rest, err := getStackListItems(restCell, depth-1)
if err != nil {
return nil, err
}
res = append(res, rest...)
err = Unmarshal(c, &tos)
if err != nil {
return nil, err
}
res = append(res, tos)
return res, nil
}
func putStackListItems(c *boc.Cell, list []VmStackValue) error {
if len(list) == 0 {
return nil
}
restCell := boc.NewCell()
err := putStackListItems(restCell, list[1:])
if err != nil {
return err
}
err = c.AddRef(restCell)
if err != nil {
return err
}
err = Marshal(c, list[0])
return err
}
func (s VmStack) MarshalTL() ([]byte, error) {
cell := boc.NewCell()
err := Marshal(cell, s)
if err != nil {
return nil, err
}
b, err := cell.ToBocCustom(false, false, false, 0)
if err != nil {
return nil, err
}
return tl.Marshal(b)
}
func (s *VmStack) UnmarshalTL(r io.Reader) error {
var b []byte
err := tl.Unmarshal(r, &b)
if err != nil {
return err
}
if len(b) == 0 {
return nil
}
cell, err := boc.DeserializeBoc(b)
if err != nil {
return err
}
return Unmarshal(cell[0], s)
}
func (s *VmStack) Put(val VmStackValue) {
*s = append(VmStack{val}, *s...)
}
func (s VmCellSlice) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
if s.stBits > s.endBits {
return fmt.Errorf("invalid StBits and EndBits for CellSlice")
}
if s.stRef > s.endRef {
return fmt.Errorf("invalid StRef and EndRef for CellSlice")
}
if s.endBits > s.cell.BitSize() {
return fmt.Errorf("EndBits > Cell bit len")
}
if s.endRef > s.cell.RefsSize() {
return fmt.Errorf("EndRef > Cell ref qty")
}
err := c.AddRef(s.cell)
if err != nil {
return err
}
err = c.WriteUint(uint64(s.stBits), 10)
if err != nil {
return err
}
err = c.WriteUint(uint64(s.endBits), 10)
if err != nil {
return err
}
err = c.WriteLimUint(s.stRef, 4)
if err != nil {
return err
}
err = c.WriteLimUint(s.endRef, 4)
return err
}
func (s *VmCellSlice) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
cell, err := c.NextRef()
if err != nil {
return err
}
stBits, err := c.ReadUint(10)
if err != nil {
return err
}
endBits, err := c.ReadUint(10)
if err != nil {
return err
}
if stBits > endBits {
return fmt.Errorf("invalid StBits and EndBits for CellSlice")
}
stRef, err := c.ReadLimUint(4)
if err != nil {
return err
}
endRef, err := c.ReadLimUint(4)
if err != nil {
return err
}
if stRef > endRef {
return fmt.Errorf("invalid StRef and EndRef for CellSlice")
}
if int(endBits) > cell.BitSize() {
return fmt.Errorf("EndBits > Cell bit len")
}
if int(endRef) > cell.RefsSize() {
return fmt.Errorf("EndRef > Cell ref qty")
}
*s = VmCellSlice{
cell: cell,
stBits: int(stBits),
endBits: int(endBits),
stRef: int(stRef),
endRef: int(endRef),
}
return nil
}
func (s VmCellSlice) Cell() *boc.Cell {
// TODO: maybe add as a filed to VmCellSlice
cell := boc.NewCell()
s.cell.ResetCounters()
err := s.cell.Skip(s.stBits)
if err != nil {
panic("not enough cell bits")
}
bits, err := s.cell.ReadBits(s.endBits - s.stBits)
if err != nil {
panic("not enough cell bits")
}
refs := s.cell.Refs()
err = cell.WriteBitString(bits)
if err != nil {
panic("can not write bits to empty cell")
}
for _, ref := range refs[s.stRef:s.endRef] {
err = cell.AddRef(ref)
if err != nil {
panic("can not write ref to empty cell")
}
}
return cell
}
func (ct VmCont) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
// TODO: implement
return fmt.Errorf("VmCont TLB marshaling not implemented")
}
func (ct *VmCont) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
// TODO: implement
return fmt.Errorf("VmCont TLB unmarshaling not implemented")
}
func TlbStructToVmCellSlice(s any) (VmStackValue, error) {
cell := boc.NewCell()
err := Marshal(cell, s)
if err != nil {
return VmStackValue{}, err
}
slice := VmCellSlice{
cell: cell,
stBits: 0,
endBits: cell.BitSize(),
stRef: 0,
endRef: cell.RefsSize(),
}
return VmStackValue{
SumType: "VmStkSlice",
VmStkSlice: slice,
}, nil
}
func TlbStructToVmCell(s any) (VmStackValue, error) {
cell := boc.NewCell()
err := Marshal(cell, s)
if err != nil {
return VmStackValue{}, err
}
res := VmStackValue{
SumType: "VmStkCell",
}
res.VmStkCell.Value = *cell
return res, nil
}
func CellToVmCellSlice(cell *boc.Cell) (VmStackValue, error) {
slice := VmCellSlice{
cell: cell,
stBits: 0,
endBits: cell.BitSize(),
stRef: 0,
endRef: cell.RefsSize(),
}
return VmStackValue{
SumType: "VmStkSlice",
VmStkSlice: slice,
}, nil
}
func (s VmCellSlice) UnmarshalToTlbStruct(res any) error {
cell := s.Cell()
err := Unmarshal(cell, res)
if err != nil {
return err
}
return nil
}
// Deprecated: IsInt is deprecated.
func (v VmStackValue) IsInt() bool {
return v.SumType == "VmStkTinyInt" || v.SumType == "VmStkInt"
}
// Deprecated: IsCellSlice is deprecated.
func (v VmStackValue) IsCellSlice() bool {
return v.SumType == "VmStkSlice"
}
// Deprecated: IsNull is deprecated.
func (v VmStackValue) IsNull() bool {
return v.SumType == "VmStkNull"
}
// Deprecated: IsCell is deprecated.
func (v VmStackValue) IsCell() bool {
return v.SumType == "VmStkCell"
}
// Deprecated: IsTuple is deprecated.
func (v VmStackValue) IsTuple() bool {
return v.SumType == "VmStkTuple"
}
// Deprecated: CellSlice is deprecated.
func (v VmStackValue) CellSlice() *boc.Cell {
if !v.IsCellSlice() {
panic("stack value is not cell slice")
}
return v.VmStkSlice.Cell()
}
// Deprecated: Cell is deprecated.
func (v VmStackValue) Cell() *boc.Cell {
if !v.IsCell() {
panic("stack value is not cell")
}
return &v.VmStkCell.Value
}
// Deprecated: Int is deprecated.
func (v VmStackValue) Int257() Int257 {
switch v.SumType {
case "VmStkTinyInt":
return Int257(*big.NewInt(v.VmStkTinyInt))
case "VmStkInt":
return v.VmStkInt
default:
panic("stack value is not int")
}
}
// Deprecated: Int64 is deprecated.
func (v VmStackValue) Int64() int64 {
switch v.SumType {
case "VmStkTinyInt":
return v.VmStkTinyInt
case "VmStkInt":
x := big.Int(v.VmStkInt)
return x.Int64()
default:
panic("stack value is not int")
}
}
// Deprecated: Uint64 is deprecated.
func (v VmStackValue) Uint64() uint64 {
switch v.SumType {
case "VmStkTinyInt":
return uint64(v.VmStkTinyInt)
case "VmStkInt":
x := big.Int(v.VmStkInt)
return x.Uint64()
default:
panic("stack value is not int")
}
}
var bigIntType = reflect.TypeOf(big.Int{})
func (v VmStackValue) Unmarshal(dest any) error {
val := reflect.ValueOf(dest)
if val.Kind() != reflect.Pointer {
return fmt.Errorf("should be pointer, not %v", val.Kind())
}
val = val.Elem()
if !val.CanSet() {
return fmt.Errorf("can't set")
}
if v.SumType == "VmStkNull" {
if val.Kind() != reflect.Pointer {
return fmt.Errorf("can't unmarshal Null to not pointer type")
}
return nil
}
if val.Kind() == reflect.Pointer {
a := reflect.New(val.Type().Elem())
dest = a.Interface()
val.Set(a)
}
switch v.SumType {
case "VmStkTinyInt":
if val.CanConvert(bigIntType) {
bi := big.NewInt(v.VmStkTinyInt)
val.Set(reflect.ValueOf(*bi).Convert(val.Type()))
} else if val.Kind() == reflect.Bool {
val.SetBool(v.VmStkTinyInt != 0)
} else {
err := toInt(val, v.VmStkTinyInt)
if err != nil {
return err
}
}
return nil
case "VmStkInt":
b := big.Int(v.VmStkInt)
if reflect.Int <= val.Kind() && val.Kind() <= reflect.Uint64 {
if !b.IsInt64() {
return fmt.Errorf("int %v is to big to represented as %v", b, val.Kind())
}
toInt(val, b.Int64())
return nil
}
if i, ok := dest.(*Bits256); ok {
bytes := b.Bytes()
if len(bytes) > 32 {
return fmt.Errorf("integer257 can't be mapped to 256bits")
}
copy(i[32-len(bytes):], bytes)
return nil
}
if val.Kind() == reflect.Bool {
val.SetBool(b.Uint64() != 0)
return nil
}
if i, ok := dest.(*Int257); ok {
*i = v.VmStkInt
return nil
}
return fmt.Errorf("maping integer257 to %v is not supported", val.Kind())
case "VmStkTuple":
return v.VmStkTuple.Unmarshal(dest)
case "VmStkSlice":
return v.VmStkSlice.UnmarshalToTlbStruct(dest)
case "VmStkCell":
return Unmarshal(&v.VmStkCell.Value, dest)
default:
return fmt.Errorf("%v is not supported for unmarshaling", v.SumType)
}
}
func toInt(val reflect.Value, i int64) error {
if reflect.Uint <= val.Kind() && val.Kind() <= reflect.Uint64 {
val.SetUint(uint64(i))
return nil
} else if reflect.Int <= val.Kind() && val.Kind() <= reflect.Int64 {
val.SetInt(i)
return nil
}
return fmt.Errorf("invalid type for int - %v", val.Kind())
}
func (s VmStack) Unmarshal(dest any) error {
val := reflect.ValueOf(dest)
if val.Kind() != reflect.Pointer {
return fmt.Errorf("value should be a pointer")
}
if val.Elem().Type().NumField() > len(s) {
return fmt.Errorf("not enough values in stack")
}
for i := 0; i < val.Elem().Type().NumField(); i++ {
fieldType := val.Elem().Field(i).Type()
if s[i].SumType == "VmStkNull" {
kind := fieldType.Kind()
if kind == reflect.Pointer || kind == reflect.Slice {
continue
}
return errors.New("can't unmarshal null")
}
value := reflect.New(fieldType)
if err := s[i].Unmarshal(value.Interface()); err != nil {
return err
}
val.Elem().Field(i).Set(value.Elem())
}
return nil
}