forked from goccy/go-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_compile.go
1350 lines (1243 loc) · 33.5 KB
/
encode_compile.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package json
import (
"encoding"
"fmt"
"reflect"
"strings"
"unsafe"
)
type compiledCode struct {
code *opcode
linked bool // whether recursive code already have linked
curLen uintptr
nextLen uintptr
}
type opcodeSet struct {
code *opcode
codeLength int
}
var (
marshalJSONType = reflect.TypeOf((*Marshaler)(nil)).Elem()
marshalTextType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
)
func encodeCompileToGetCodeSetSlowPath(typeptr uintptr) (*opcodeSet, error) {
opcodeMap := loadOpcodeMap()
if codeSet, exists := opcodeMap[typeptr]; exists {
return codeSet, nil
}
// noescape trick for header.typ ( reflect.*rtype )
copiedType := *(**rtype)(unsafe.Pointer(&typeptr))
code, err := encodeCompileHead(&encodeCompileContext{
typ: copiedType,
root: true,
structTypeToCompiledCode: map[uintptr]*compiledCode{},
})
if err != nil {
return nil, err
}
code = copyOpcode(code)
codeLength := code.totalLength()
codeSet := &opcodeSet{
code: code,
codeLength: codeLength,
}
storeOpcodeSet(typeptr, codeSet, opcodeMap)
return codeSet, nil
}
func encodeCompileHead(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ
switch {
case typ.Implements(marshalJSONType):
return encodeCompileMarshalJSON(ctx)
case rtype_ptrTo(typ).Implements(marshalJSONType):
return encodeCompileMarshalJSONPtr(ctx)
case typ.Implements(marshalTextType):
return encodeCompileMarshalText(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType):
return encodeCompileMarshalTextPtr(ctx)
}
isPtr := false
orgType := typ
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
isPtr = true
}
if typ.Kind() == reflect.Map {
return encodeCompileMap(ctx.withType(typ), isPtr)
} else if typ.Kind() == reflect.Struct {
code, err := encodeCompileStruct(ctx.withType(typ), isPtr)
if err != nil {
return nil, err
}
encodeConvertHeadOnlyCode(code, isPtr)
encodeOptimizeStructEnd(code)
encodeLinkRecursiveCode(code)
return code, nil
} else if isPtr && typ.Implements(marshalTextType) {
typ = orgType
} else if isPtr && typ.Implements(marshalJSONType) {
typ = orgType
}
code, err := encodeCompile(ctx.withType(typ))
if err != nil {
return nil, err
}
encodeConvertHeadOnlyCode(code, isPtr)
encodeOptimizeStructEnd(code)
encodeLinkRecursiveCode(code)
return code, nil
}
func encodeLinkRecursiveCode(c *opcode) {
for code := c; code.op != opEnd && code.op != opStructFieldRecursiveEnd; {
switch code.op {
case opStructFieldRecursive,
opStructFieldPtrAnonymousHeadRecursive,
opStructFieldAnonymousHeadRecursive:
if code.jmp.linked {
code = code.next
continue
}
code.jmp.code = copyOpcode(code.jmp.code)
c := code.jmp.code
c.end.next = newEndOp(&encodeCompileContext{})
c.op = c.op.ptrHeadToHead()
beforeLastCode := c.end
lastCode := beforeLastCode.next
lastCode.idx = beforeLastCode.idx + uintptrSize
lastCode.elemIdx = lastCode.idx + uintptrSize
// extend length to alloc slot for elemIdx
totalLength := uintptr(code.totalLength() + 1)
nextTotalLength := uintptr(c.totalLength() + 1)
c.end.next.op = opStructFieldRecursiveEnd
code.jmp.curLen = totalLength
code.jmp.nextLen = nextTotalLength
code.jmp.linked = true
encodeLinkRecursiveCode(code.jmp.code)
code = code.next
continue
}
switch code.op.codeType() {
case codeArrayElem, codeSliceElem, codeMapKey:
code = code.end
default:
code = code.next
}
}
}
func encodeOptimizeStructEnd(c *opcode) {
for code := c; code.op != opEnd; {
if code.op == opStructFieldRecursive {
// ignore if exists recursive operation
return
}
switch code.op.codeType() {
case codeArrayElem, codeSliceElem, codeMapKey:
code = code.end
default:
code = code.next
}
}
for code := c; code.op != opEnd; {
switch code.op.codeType() {
case codeArrayElem, codeSliceElem, codeMapKey:
code = code.end
case codeStructEnd:
switch code.op {
case opStructEnd:
prev := code.prevField
if strings.Contains(prev.op.String(), "Head") {
// not exists field
code = code.next
break
}
if prev.op != prev.op.fieldToEnd() {
prev.op = prev.op.fieldToEnd()
prev.next = code.next
}
code = code.next
default:
code = code.next
}
default:
code = code.next
}
}
}
func encodeConvertHeadOnlyCode(c *opcode, isPtrHead bool) {
if c.nextField == nil {
return
}
if c.nextField.op.codeType() != codeStructEnd {
return
}
switch c.op {
case opStructFieldHead:
encodeConvertHeadOnlyCode(c.next, false)
if !strings.Contains(c.next.op.String(), "Only") {
return
}
c.op = opStructFieldHeadOnly
case opStructFieldHeadOmitEmpty:
encodeConvertHeadOnlyCode(c.next, false)
if !strings.Contains(c.next.op.String(), "Only") {
return
}
c.op = opStructFieldHeadOmitEmptyOnly
case opStructFieldHeadStringTag:
encodeConvertHeadOnlyCode(c.next, false)
if !strings.Contains(c.next.op.String(), "Only") {
return
}
c.op = opStructFieldHeadStringTagOnly
case opStructFieldPtrHead:
}
if strings.Contains(c.op.String(), "Marshal") {
return
}
if strings.Contains(c.op.String(), "Slice") {
return
}
if strings.Contains(c.op.String(), "Map") {
return
}
isPtrOp := strings.Contains(c.op.String(), "Ptr")
if isPtrOp && !isPtrHead {
c.op = c.op.headToOnlyHead()
} else if !isPtrOp && isPtrHead {
c.op = c.op.headToPtrHead().headToOnlyHead()
} else if isPtrOp && isPtrHead {
c.op = c.op.headToPtrHead().headToOnlyHead()
}
}
func encodeImplementsMarshaler(typ *rtype) bool {
switch {
case typ.Implements(marshalJSONType):
return true
case rtype_ptrTo(typ).Implements(marshalJSONType):
return true
case typ.Implements(marshalTextType):
return true
case rtype_ptrTo(typ).Implements(marshalTextType):
return true
}
return false
}
func encodeCompile(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ
switch {
case typ.Implements(marshalJSONType):
return encodeCompileMarshalJSON(ctx)
case rtype_ptrTo(typ).Implements(marshalJSONType):
return encodeCompileMarshalJSONPtr(ctx)
case typ.Implements(marshalTextType):
return encodeCompileMarshalText(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType):
return encodeCompileMarshalTextPtr(ctx)
}
switch typ.Kind() {
case reflect.Ptr:
return encodeCompilePtr(ctx)
case reflect.Slice:
elem := typ.Elem()
if !encodeImplementsMarshaler(elem) && elem.Kind() == reflect.Uint8 {
return encodeCompileBytes(ctx)
}
return encodeCompileSlice(ctx)
case reflect.Array:
return encodeCompileArray(ctx)
case reflect.Map:
return encodeCompileMap(ctx, true)
case reflect.Struct:
return encodeCompileStruct(ctx, false)
case reflect.Interface:
return encodeCompileInterface(ctx)
case reflect.Int:
return encodeCompileInt(ctx)
case reflect.Int8:
return encodeCompileInt8(ctx)
case reflect.Int16:
return encodeCompileInt16(ctx)
case reflect.Int32:
return encodeCompileInt32(ctx)
case reflect.Int64:
return encodeCompileInt64(ctx)
case reflect.Uint:
return encodeCompileUint(ctx)
case reflect.Uint8:
return encodeCompileUint8(ctx)
case reflect.Uint16:
return encodeCompileUint16(ctx)
case reflect.Uint32:
return encodeCompileUint32(ctx)
case reflect.Uint64:
return encodeCompileUint64(ctx)
case reflect.Uintptr:
return encodeCompileUint(ctx)
case reflect.Float32:
return encodeCompileFloat32(ctx)
case reflect.Float64:
return encodeCompileFloat64(ctx)
case reflect.String:
return encodeCompileString(ctx)
case reflect.Bool:
return encodeCompileBool(ctx)
}
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
}
func encodeCompileKey(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ
switch {
case rtype_ptrTo(typ).Implements(marshalJSONType):
return encodeCompileMarshalJSONPtr(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType):
return encodeCompileMarshalTextPtr(ctx)
}
switch typ.Kind() {
case reflect.Ptr:
return encodeCompilePtr(ctx)
case reflect.Interface:
return encodeCompileInterface(ctx)
case reflect.String:
return encodeCompileString(ctx)
case reflect.Int:
return encodeCompileIntString(ctx)
case reflect.Int8:
return encodeCompileInt8String(ctx)
case reflect.Int16:
return encodeCompileInt16String(ctx)
case reflect.Int32:
return encodeCompileInt32String(ctx)
case reflect.Int64:
return encodeCompileInt64String(ctx)
case reflect.Uint:
return encodeCompileUintString(ctx)
case reflect.Uint8:
return encodeCompileUint8String(ctx)
case reflect.Uint16:
return encodeCompileUint16String(ctx)
case reflect.Uint32:
return encodeCompileUint32String(ctx)
case reflect.Uint64:
return encodeCompileUint64String(ctx)
case reflect.Uintptr:
return encodeCompileUintString(ctx)
}
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
}
func encodeCompilePtr(ctx *encodeCompileContext) (*opcode, error) {
ptrOpcodeIndex := ctx.opcodeIndex
ptrIndex := ctx.ptrIndex
ctx.incIndex()
code, err := encodeCompile(ctx.withType(ctx.typ.Elem()))
if err != nil {
return nil, err
}
ptrHeadOp := code.op.headToPtrHead()
if code.op != ptrHeadOp {
code.op = ptrHeadOp
code.decOpcodeIndex()
ctx.decIndex()
return code, nil
}
c := ctx.context()
c.opcodeIndex = ptrOpcodeIndex
c.ptrIndex = ptrIndex
return newOpCodeWithNext(c, opPtr, code), nil
}
func encodeCompileMarshalJSON(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opMarshalJSON)
ctx.incIndex()
return code, nil
}
func encodeCompileMarshalJSONPtr(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx.withType(rtype_ptrTo(ctx.typ)), opMarshalJSON)
ctx.incIndex()
return code, nil
}
func encodeCompileMarshalText(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opMarshalText)
ctx.incIndex()
return code, nil
}
func encodeCompileMarshalTextPtr(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx.withType(rtype_ptrTo(ctx.typ)), opMarshalText)
ctx.incIndex()
return code, nil
}
func encodeCompileInt(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt)
ctx.incIndex()
return code, nil
}
func encodeCompileInt8(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt8)
ctx.incIndex()
return code, nil
}
func encodeCompileInt16(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt16)
ctx.incIndex()
return code, nil
}
func encodeCompileInt32(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt32)
ctx.incIndex()
return code, nil
}
func encodeCompileInt64(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt64)
ctx.incIndex()
return code, nil
}
func encodeCompileUint(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint)
ctx.incIndex()
return code, nil
}
func encodeCompileUint8(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint8)
ctx.incIndex()
return code, nil
}
func encodeCompileUint16(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint16)
ctx.incIndex()
return code, nil
}
func encodeCompileUint32(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint32)
ctx.incIndex()
return code, nil
}
func encodeCompileUint64(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint64)
ctx.incIndex()
return code, nil
}
func encodeCompileIntString(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opIntString)
ctx.incIndex()
return code, nil
}
func encodeCompileInt8String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt8String)
ctx.incIndex()
return code, nil
}
func encodeCompileInt16String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt16String)
ctx.incIndex()
return code, nil
}
func encodeCompileInt32String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt32String)
ctx.incIndex()
return code, nil
}
func encodeCompileInt64String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opInt64String)
ctx.incIndex()
return code, nil
}
func encodeCompileUintString(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUintString)
ctx.incIndex()
return code, nil
}
func encodeCompileUint8String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint8String)
ctx.incIndex()
return code, nil
}
func encodeCompileUint16String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint16String)
ctx.incIndex()
return code, nil
}
func encodeCompileUint32String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint32String)
ctx.incIndex()
return code, nil
}
func encodeCompileUint64String(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opUint64String)
ctx.incIndex()
return code, nil
}
func encodeCompileFloat32(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opFloat32)
ctx.incIndex()
return code, nil
}
func encodeCompileFloat64(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opFloat64)
ctx.incIndex()
return code, nil
}
func encodeCompileString(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opString)
ctx.incIndex()
return code, nil
}
func encodeCompileBool(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opBool)
ctx.incIndex()
return code, nil
}
func encodeCompileBytes(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opBytes)
ctx.incIndex()
return code, nil
}
func encodeCompileInterface(ctx *encodeCompileContext) (*opcode, error) {
code := newInterfaceCode(ctx)
ctx.incIndex()
return code, nil
}
func encodeCompileSlice(ctx *encodeCompileContext) (*opcode, error) {
ctx.root = false
elem := ctx.typ.Elem()
size := elem.Size()
header := newSliceHeaderCode(ctx)
ctx.incIndex()
code, err := encodeCompile(ctx.withType(ctx.typ.Elem()).incIndent())
if err != nil {
return nil, err
}
// header => opcode => elem => end
// ^ |
// |________|
elemCode := newSliceElemCode(ctx, header, size)
ctx.incIndex()
end := newOpCode(ctx, opSliceEnd)
ctx.incIndex()
header.elem = elemCode
header.end = end
header.next = code
code.beforeLastCode().next = (*opcode)(unsafe.Pointer(elemCode))
elemCode.next = code
elemCode.end = end
return (*opcode)(unsafe.Pointer(header)), nil
}
func encodeCompileArray(ctx *encodeCompileContext) (*opcode, error) {
ctx.root = false
typ := ctx.typ
elem := typ.Elem()
alen := typ.Len()
size := elem.Size()
header := newArrayHeaderCode(ctx, alen)
ctx.incIndex()
code, err := encodeCompile(ctx.withType(elem).incIndent())
if err != nil {
return nil, err
}
// header => opcode => elem => end
// ^ |
// |________|
elemCode := newArrayElemCode(ctx, header, alen, size)
ctx.incIndex()
end := newOpCode(ctx, opArrayEnd)
ctx.incIndex()
header.elem = elemCode
header.end = end
header.next = code
code.beforeLastCode().next = (*opcode)(unsafe.Pointer(elemCode))
elemCode.next = code
elemCode.end = end
return (*opcode)(unsafe.Pointer(header)), nil
}
//go:linkname mapiterinit reflect.mapiterinit
//go:noescape
func mapiterinit(mapType *rtype, m unsafe.Pointer) unsafe.Pointer
//go:linkname mapiterkey reflect.mapiterkey
//go:noescape
func mapiterkey(it unsafe.Pointer) unsafe.Pointer
//go:linkname mapiternext reflect.mapiternext
//go:noescape
func mapiternext(it unsafe.Pointer)
//go:linkname maplen reflect.maplen
//go:noescape
func maplen(m unsafe.Pointer) int
func encodeCompileMap(ctx *encodeCompileContext, withLoad bool) (*opcode, error) {
// header => code => value => code => key => code => value => code => end
// ^ |
// |_______________________|
ctx = ctx.incIndent()
header := newMapHeaderCode(ctx, withLoad)
ctx.incIndex()
typ := ctx.typ
keyType := ctx.typ.Key()
keyCode, err := encodeCompileKey(ctx.withType(keyType))
if err != nil {
return nil, err
}
value := newMapValueCode(ctx, header)
ctx.incIndex()
valueType := typ.Elem()
valueCode, err := encodeCompile(ctx.withType(valueType))
if err != nil {
return nil, err
}
key := newMapKeyCode(ctx, header)
ctx.incIndex()
ctx = ctx.decIndent()
header.mapKey = key
header.mapValue = value
end := newMapEndCode(ctx, header)
ctx.incIndex()
header.next = keyCode
keyCode.beforeLastCode().next = (*opcode)(unsafe.Pointer(value))
value.next = valueCode
valueCode.beforeLastCode().next = (*opcode)(unsafe.Pointer(key))
key.next = keyCode
header.end = end
key.end = end
value.end = end
return (*opcode)(unsafe.Pointer(header)), nil
}
func encodeTypeToHeaderType(ctx *encodeCompileContext, code *opcode) opType {
switch code.op {
case opPtr:
ptrNum := 1
c := code
ctx.decIndex()
for {
if code.next.op == opPtr {
ptrNum++
code = code.next
ctx.decIndex()
continue
}
break
}
c.ptrNum = ptrNum
if ptrNum > 1 {
switch code.next.op {
case opInt:
return opStructFieldHeadIntNPtr
case opInt8:
return opStructFieldHeadInt8NPtr
case opInt16:
return opStructFieldHeadInt16NPtr
case opInt32:
return opStructFieldHeadInt32NPtr
case opInt64:
return opStructFieldHeadInt64NPtr
case opUint:
return opStructFieldHeadUintNPtr
case opUint8:
return opStructFieldHeadUint8NPtr
case opUint16:
return opStructFieldHeadUint16NPtr
case opUint32:
return opStructFieldHeadUint32NPtr
case opUint64:
return opStructFieldHeadUint64NPtr
case opFloat32:
return opStructFieldHeadFloat32NPtr
case opFloat64:
return opStructFieldHeadFloat64NPtr
case opString:
return opStructFieldHeadStringNPtr
case opBool:
return opStructFieldHeadBoolNPtr
}
} else {
switch code.next.op {
case opInt:
return opStructFieldHeadIntPtr
case opInt8:
return opStructFieldHeadInt8Ptr
case opInt16:
return opStructFieldHeadInt16Ptr
case opInt32:
return opStructFieldHeadInt32Ptr
case opInt64:
return opStructFieldHeadInt64Ptr
case opUint:
return opStructFieldHeadUintPtr
case opUint8:
return opStructFieldHeadUint8Ptr
case opUint16:
return opStructFieldHeadUint16Ptr
case opUint32:
return opStructFieldHeadUint32Ptr
case opUint64:
return opStructFieldHeadUint64Ptr
case opFloat32:
return opStructFieldHeadFloat32Ptr
case opFloat64:
return opStructFieldHeadFloat64Ptr
case opString:
return opStructFieldHeadStringPtr
case opBool:
return opStructFieldHeadBoolPtr
}
}
case opInt:
return opStructFieldHeadInt
case opInt8:
return opStructFieldHeadInt8
case opInt16:
return opStructFieldHeadInt16
case opInt32:
return opStructFieldHeadInt32
case opInt64:
return opStructFieldHeadInt64
case opUint:
return opStructFieldHeadUint
case opUint8:
return opStructFieldHeadUint8
case opUint16:
return opStructFieldHeadUint16
case opUint32:
return opStructFieldHeadUint32
case opUint64:
return opStructFieldHeadUint64
case opFloat32:
return opStructFieldHeadFloat32
case opFloat64:
return opStructFieldHeadFloat64
case opString:
return opStructFieldHeadString
case opBool:
return opStructFieldHeadBool
case opMapHead:
return opStructFieldHeadMap
case opMapHeadLoad:
return opStructFieldHeadMapLoad
case opArrayHead:
return opStructFieldHeadArray
case opSliceHead:
return opStructFieldHeadSlice
case opStructFieldHead:
return opStructFieldHeadStruct
case opMarshalJSON:
return opStructFieldHeadMarshalJSON
case opMarshalText:
return opStructFieldHeadMarshalText
}
return opStructFieldHead
}
func encodeTypeToFieldType(ctx *encodeCompileContext, code *opcode) opType {
switch code.op {
case opPtr:
ptrNum := 1
ctx.decIndex()
c := code
for {
if code.next.op == opPtr {
ptrNum++
code = code.next
ctx.decIndex()
continue
}
break
}
c.ptrNum = ptrNum
if ptrNum > 1 {
switch code.next.op {
case opInt:
return opStructFieldIntNPtr
case opInt8:
return opStructFieldInt8NPtr
case opInt16:
return opStructFieldInt16NPtr
case opInt32:
return opStructFieldInt32NPtr
case opInt64:
return opStructFieldInt64NPtr
case opUint:
return opStructFieldUintNPtr
case opUint8:
return opStructFieldUint8NPtr
case opUint16:
return opStructFieldUint16NPtr
case opUint32:
return opStructFieldUint32NPtr
case opUint64:
return opStructFieldUint64NPtr
case opFloat32:
return opStructFieldFloat32NPtr
case opFloat64:
return opStructFieldFloat64NPtr
case opString:
return opStructFieldStringNPtr
case opBool:
return opStructFieldBoolNPtr
}
} else {
switch code.next.op {
case opInt:
return opStructFieldIntPtr
case opInt8:
return opStructFieldInt8Ptr
case opInt16:
return opStructFieldInt16Ptr
case opInt32:
return opStructFieldInt32Ptr
case opInt64:
return opStructFieldInt64Ptr
case opUint:
return opStructFieldUintPtr
case opUint8:
return opStructFieldUint8Ptr
case opUint16:
return opStructFieldUint16Ptr
case opUint32:
return opStructFieldUint32Ptr
case opUint64:
return opStructFieldUint64Ptr
case opFloat32:
return opStructFieldFloat32Ptr
case opFloat64:
return opStructFieldFloat64Ptr
case opString:
return opStructFieldStringPtr
case opBool:
return opStructFieldBoolPtr
}
}
case opInt:
return opStructFieldInt
case opInt8:
return opStructFieldInt8
case opInt16:
return opStructFieldInt16
case opInt32:
return opStructFieldInt32
case opInt64:
return opStructFieldInt64
case opUint:
return opStructFieldUint
case opUint8:
return opStructFieldUint8
case opUint16:
return opStructFieldUint16
case opUint32:
return opStructFieldUint32
case opUint64:
return opStructFieldUint64
case opFloat32:
return opStructFieldFloat32
case opFloat64:
return opStructFieldFloat64
case opString:
return opStructFieldString
case opBool:
return opStructFieldBool
case opMapHead:
return opStructFieldMap
case opMapHeadLoad:
return opStructFieldMapLoad
case opArrayHead:
return opStructFieldArray
case opSliceHead:
return opStructFieldSlice
case opStructFieldHead:
return opStructFieldStruct
case opMarshalJSON:
return opStructFieldMarshalJSON
case opMarshalText:
return opStructFieldMarshalText
}
return opStructField
}
func encodeOptimizeStructHeader(ctx *encodeCompileContext, code *opcode, tag *structTag) opType {
headType := encodeTypeToHeaderType(ctx, code)
switch {
case tag.isOmitEmpty:
headType = headType.headToOmitEmptyHead()
case tag.isString:
headType = headType.headToStringTagHead()
}
return headType
}
func encodeOptimizeStructField(ctx *encodeCompileContext, code *opcode, tag *structTag) opType {
fieldType := encodeTypeToFieldType(ctx, code)
switch {
case tag.isOmitEmpty:
fieldType = fieldType.fieldToOmitEmptyField()
case tag.isString:
fieldType = fieldType.fieldToStringTagField()
}
return fieldType
}
func encodeRecursiveCode(ctx *encodeCompileContext, jmp *compiledCode) *opcode {
code := newRecursiveCode(ctx, jmp)
ctx.incIndex()
return code
}
func encodeCompiledCode(ctx *encodeCompileContext) *opcode {
typ := ctx.typ
typeptr := uintptr(unsafe.Pointer(typ))
if compiledCode, exists := ctx.structTypeToCompiledCode[typeptr]; exists {
return encodeRecursiveCode(ctx, compiledCode)
}
return nil
}
func encodeStructHeader(ctx *encodeCompileContext, fieldCode *opcode, valueCode *opcode, tag *structTag) *opcode {
fieldCode.indent--
op := encodeOptimizeStructHeader(ctx, valueCode, tag)
fieldCode.op = op
fieldCode.ptrNum = valueCode.ptrNum
switch op {
case opStructFieldHead,
opStructFieldHeadSlice,
opStructFieldHeadArray,
opStructFieldHeadMap,
opStructFieldHeadMapLoad,
opStructFieldHeadStruct,
opStructFieldHeadOmitEmpty,
opStructFieldHeadOmitEmptySlice,
opStructFieldHeadOmitEmptyArray,
opStructFieldHeadOmitEmptyMap,
opStructFieldHeadOmitEmptyMapLoad,
opStructFieldHeadOmitEmptyStruct,
opStructFieldHeadStringTag:
return valueCode.beforeLastCode()
}
ctx.decOpcodeIndex()
return (*opcode)(unsafe.Pointer(fieldCode))
}
func encodeStructField(ctx *encodeCompileContext, fieldCode *opcode, valueCode *opcode, tag *structTag) *opcode {
code := (*opcode)(unsafe.Pointer(fieldCode))
op := encodeOptimizeStructField(ctx, valueCode, tag)
fieldCode.op = op
fieldCode.ptrNum = valueCode.ptrNum
switch op {
case opStructField,
opStructFieldSlice,