summaryrefslogtreecommitdiff
path: root/vendor/github.com/goccy/go-json/internal/encoder/opcode.go
blob: 0dfd6fb954305ae34f9980150771f300cd8e8aac (plain)
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
package encoder

import (
	"fmt"
	"math"
	"strings"
	"unsafe"

	"github.com/goccy/go-json/internal/runtime"
)

const uintptrSize = 4 << (^uintptr(0) >> 63)

type Opcode struct {
	Op               OpType        // operation type
	Type             *runtime.Type // go type
	DisplayIdx       int           // opcode index
	Key              []byte        // struct field key
	EscapedKey       []byte        // struct field key ( HTML escaped )
	PtrNum           int           // pointer number: e.g. double pointer is 2.
	DisplayKey       string        // key text to display
	IsTaggedKey      bool          // whether tagged key
	AnonymousKey     bool          // whether anonymous key
	AnonymousHead    bool          // whether anonymous head or not
	Indirect         bool          // whether indirect or not
	Nilcheck         bool          // whether needs to nilcheck or not
	AddrForMarshaler bool          // whether needs to addr for marshaler or not
	IsNextOpPtrType  bool          // whether next operation is ptr type or not
	IsNilableType    bool          // whether type is nilable or not
	RshiftNum        uint8         // use to take bit for judging whether negative integer or not
	Mask             uint64        // mask for number
	Indent           int           // indent number

	Idx     uintptr // offset to access ptr
	HeadIdx uintptr // offset to access slice/struct head
	ElemIdx uintptr // offset to access array/slice/map elem
	Length  uintptr // offset to access slice/map length or array length
	MapIter uintptr // offset to access map iterator
	MapPos  uintptr // offset to access position list for sorted map
	Offset  uintptr // offset size from struct header
	Size    uintptr // array/slice elem size

	MapKey    *Opcode       // map key
	MapValue  *Opcode       // map value
	Elem      *Opcode       // array/slice elem
	End       *Opcode       // array/slice/struct/map end
	PrevField *Opcode       // prev struct field
	NextField *Opcode       // next struct field
	Next      *Opcode       // next opcode
	Jmp       *CompiledCode // for recursive call
}

func rshitNum(bitSize uint8) uint8 {
	return bitSize - 1
}

func (c *Opcode) setMaskAndRshiftNum(bitSize uint8) {
	switch bitSize {
	case 8:
		c.Mask = math.MaxUint8
	case 16:
		c.Mask = math.MaxUint16
	case 32:
		c.Mask = math.MaxUint32
	case 64:
		c.Mask = math.MaxUint64
	}
	c.RshiftNum = rshitNum(bitSize)
}

func (c *Opcode) ToHeaderType(isString bool) OpType {
	switch c.Op {
	case OpInt:
		if isString {
			return OpStructHeadIntString
		}
		return OpStructHeadInt
	case OpIntPtr:
		if isString {
			return OpStructHeadIntPtrString
		}
		return OpStructHeadIntPtr
	case OpUint:
		if isString {
			return OpStructHeadUintString
		}
		return OpStructHeadUint
	case OpUintPtr:
		if isString {
			return OpStructHeadUintPtrString
		}
		return OpStructHeadUintPtr
	case OpFloat32:
		if isString {
			return OpStructHeadFloat32String
		}
		return OpStructHeadFloat32
	case OpFloat32Ptr:
		if isString {
			return OpStructHeadFloat32PtrString
		}
		return OpStructHeadFloat32Ptr
	case OpFloat64:
		if isString {
			return OpStructHeadFloat64String
		}
		return OpStructHeadFloat64
	case OpFloat64Ptr:
		if isString {
			return OpStructHeadFloat64PtrString
		}
		return OpStructHeadFloat64Ptr
	case OpString:
		if isString {
			return OpStructHeadStringString
		}
		return OpStructHeadString
	case OpStringPtr:
		if isString {
			return OpStructHeadStringPtrString
		}
		return OpStructHeadStringPtr
	case OpNumber:
		if isString {
			return OpStructHeadNumberString
		}
		return OpStructHeadNumber
	case OpNumberPtr:
		if isString {
			return OpStructHeadNumberPtrString
		}
		return OpStructHeadNumberPtr
	case OpBool:
		if isString {
			return OpStructHeadBoolString
		}
		return OpStructHeadBool
	case OpBoolPtr:
		if isString {
			return OpStructHeadBoolPtrString
		}
		return OpStructHeadBoolPtr
	case OpBytes:
		return OpStructHeadBytes
	case OpBytesPtr:
		return OpStructHeadBytesPtr
	case OpMap:
		return OpStructHeadMap
	case OpMapPtr:
		c.Op = OpMap
		return OpStructHeadMapPtr
	case OpArray:
		return OpStructHeadArray
	case OpArrayPtr:
		c.Op = OpArray
		return OpStructHeadArrayPtr
	case OpSlice:
		return OpStructHeadSlice
	case OpSlicePtr:
		c.Op = OpSlice
		return OpStructHeadSlicePtr
	case OpMarshalJSON:
		return OpStructHeadMarshalJSON
	case OpMarshalJSONPtr:
		return OpStructHeadMarshalJSONPtr
	case OpMarshalText:
		return OpStructHeadMarshalText
	case OpMarshalTextPtr:
		return OpStructHeadMarshalTextPtr
	}
	return OpStructHead
}

func (c *Opcode) ToFieldType(isString bool) OpType {
	switch c.Op {
	case OpInt:
		if isString {
			return OpStructFieldIntString
		}
		return OpStructFieldInt
	case OpIntPtr:
		if isString {
			return OpStructFieldIntPtrString
		}
		return OpStructFieldIntPtr
	case OpUint:
		if isString {
			return OpStructFieldUintString
		}
		return OpStructFieldUint
	case OpUintPtr:
		if isString {
			return OpStructFieldUintPtrString
		}
		return OpStructFieldUintPtr
	case OpFloat32:
		if isString {
			return OpStructFieldFloat32String
		}
		return OpStructFieldFloat32
	case OpFloat32Ptr:
		if isString {
			return OpStructFieldFloat32PtrString
		}
		return OpStructFieldFloat32Ptr
	case OpFloat64:
		if isString {
			return OpStructFieldFloat64String
		}
		return OpStructFieldFloat64
	case OpFloat64Ptr:
		if isString {
			return OpStructFieldFloat64PtrString
		}
		return OpStructFieldFloat64Ptr
	case OpString:
		if isString {
			return OpStructFieldStringString
		}
		return OpStructFieldString
	case OpStringPtr:
		if isString {
			return OpStructFieldStringPtrString
		}
		return OpStructFieldStringPtr
	case OpNumber:
		if isString {
			return OpStructFieldNumberString
		}
		return OpStructFieldNumber
	case OpNumberPtr:
		if isString {
			return OpStructFieldNumberPtrString
		}
		return OpStructFieldNumberPtr
	case OpBool:
		if isString {
			return OpStructFieldBoolString
		}
		return OpStructFieldBool
	case OpBoolPtr:
		if isString {
			return OpStructFieldBoolPtrString
		}
		return OpStructFieldBoolPtr
	case OpBytes:
		return OpStructFieldBytes
	case OpBytesPtr:
		return OpStructFieldBytesPtr
	case OpMap:
		return OpStructFieldMap
	case OpMapPtr:
		c.Op = OpMap
		return OpStructFieldMapPtr
	case OpArray:
		return OpStructFieldArray
	case OpArrayPtr:
		c.Op = OpArray
		return OpStructFieldArrayPtr
	case OpSlice:
		return OpStructFieldSlice
	case OpSlicePtr:
		c.Op = OpSlice
		return OpStructFieldSlicePtr
	case OpMarshalJSON:
		return OpStructFieldMarshalJSON
	case OpMarshalJSONPtr:
		return OpStructFieldMarshalJSONPtr
	case OpMarshalText:
		return OpStructFieldMarshalText
	case OpMarshalTextPtr:
		return OpStructFieldMarshalTextPtr
	}
	return OpStructField
}

func newOpCode(ctx *compileContext, op OpType) *Opcode {
	return newOpCodeWithNext(ctx, op, newEndOp(ctx))
}

func opcodeOffset(idx int) uintptr {
	return uintptr(idx) * uintptrSize
}

func copyOpcode(code *Opcode) *Opcode {
	codeMap := map[uintptr]*Opcode{}
	return code.copy(codeMap)
}

func newOpCodeWithNext(ctx *compileContext, op OpType, next *Opcode) *Opcode {
	return &Opcode{
		Op:         op,
		Type:       ctx.typ,
		DisplayIdx: ctx.opcodeIndex,
		Indent:     ctx.indent,
		Idx:        opcodeOffset(ctx.ptrIndex),
		Next:       next,
	}
}

func newEndOp(ctx *compileContext) *Opcode {
	return newOpCodeWithNext(ctx, OpEnd, nil)
}

func (c *Opcode) copy(codeMap map[uintptr]*Opcode) *Opcode {
	if c == nil {
		return nil
	}
	addr := uintptr(unsafe.Pointer(c))
	if code, exists := codeMap[addr]; exists {
		return code
	}
	copied := &Opcode{
		Op:               c.Op,
		Type:             c.Type,
		DisplayIdx:       c.DisplayIdx,
		Key:              c.Key,
		EscapedKey:       c.EscapedKey,
		DisplayKey:       c.DisplayKey,
		PtrNum:           c.PtrNum,
		Mask:             c.Mask,
		RshiftNum:        c.RshiftNum,
		IsTaggedKey:      c.IsTaggedKey,
		AnonymousKey:     c.AnonymousKey,
		AnonymousHead:    c.AnonymousHead,
		Indirect:         c.Indirect,
		Nilcheck:         c.Nilcheck,
		AddrForMarshaler: c.AddrForMarshaler,
		IsNextOpPtrType:  c.IsNextOpPtrType,
		IsNilableType:    c.IsNilableType,
		Indent:           c.Indent,
		Idx:              c.Idx,
		HeadIdx:          c.HeadIdx,
		ElemIdx:          c.ElemIdx,
		Length:           c.Length,
		MapIter:          c.MapIter,
		MapPos:           c.MapPos,
		Offset:           c.Offset,
		Size:             c.Size,
	}
	codeMap[addr] = copied
	copied.MapKey = c.MapKey.copy(codeMap)
	copied.MapValue = c.MapValue.copy(codeMap)
	copied.Elem = c.Elem.copy(codeMap)
	copied.End = c.End.copy(codeMap)
	copied.PrevField = c.PrevField.copy(codeMap)
	copied.NextField = c.NextField.copy(codeMap)
	copied.Next = c.Next.copy(codeMap)
	copied.Jmp = c.Jmp
	return copied
}

func (c *Opcode) BeforeLastCode() *Opcode {
	code := c
	for {
		var nextCode *Opcode
		switch code.Op.CodeType() {
		case CodeArrayElem, CodeSliceElem, CodeMapKey:
			nextCode = code.End
		default:
			nextCode = code.Next
		}
		if nextCode.Op == OpEnd {
			return code
		}
		code = nextCode
	}
}

func (c *Opcode) TotalLength() int {
	var idx int
	for code := c; code.Op != OpEnd; {
		idx = int(code.Idx / uintptrSize)
		if code.Op == OpRecursiveEnd {
			break
		}
		switch code.Op.CodeType() {
		case CodeArrayElem, CodeSliceElem, CodeMapKey:
			code = code.End
		default:
			code = code.Next
		}
	}
	return idx + 2 // opEnd + 1
}

func (c *Opcode) decOpcodeIndex() {
	for code := c; code.Op != OpEnd; {
		code.DisplayIdx--
		code.Idx -= uintptrSize
		if code.HeadIdx > 0 {
			code.HeadIdx -= uintptrSize
		}
		if code.ElemIdx > 0 {
			code.ElemIdx -= uintptrSize
		}
		if code.MapIter > 0 {
			code.MapIter -= uintptrSize
		}
		if code.Length > 0 && code.Op.CodeType() != CodeArrayHead && code.Op.CodeType() != CodeArrayElem {
			code.Length -= uintptrSize
		}
		switch code.Op.CodeType() {
		case CodeArrayElem, CodeSliceElem, CodeMapKey:
			code = code.End
		default:
			code = code.Next
		}
	}
}

func (c *Opcode) decIndent() {
	for code := c; code.Op != OpEnd; {
		code.Indent--
		switch code.Op.CodeType() {
		case CodeArrayElem, CodeSliceElem, CodeMapKey:
			code = code.End
		default:
			code = code.Next
		}
	}
}

func (c *Opcode) dumpHead(code *Opcode) string {
	var length uintptr
	if code.Op.CodeType() == CodeArrayHead {
		length = code.Length
	} else {
		length = code.Length / uintptrSize
	}
	return fmt.Sprintf(
		`[%d]%s%s ([idx:%d][headIdx:%d][elemIdx:%d][length:%d])`,
		code.DisplayIdx,
		strings.Repeat("-", code.Indent),
		code.Op,
		code.Idx/uintptrSize,
		code.HeadIdx/uintptrSize,
		code.ElemIdx/uintptrSize,
		length,
	)
}

func (c *Opcode) dumpMapHead(code *Opcode) string {
	return fmt.Sprintf(
		`[%d]%s%s ([idx:%d][headIdx:%d][elemIdx:%d][length:%d][mapIter:%d])`,
		code.DisplayIdx,
		strings.Repeat("-", code.Indent),
		code.Op,
		code.Idx/uintptrSize,
		code.HeadIdx/uintptrSize,
		code.ElemIdx/uintptrSize,
		code.Length/uintptrSize,
		code.MapIter/uintptrSize,
	)
}

func (c *Opcode) dumpMapEnd(code *Opcode) string {
	return fmt.Sprintf(
		`[%d]%s%s ([idx:%d][mapPos:%d][length:%d])`,
		code.DisplayIdx,
		strings.Repeat("-", code.Indent),
		code.Op,
		code.Idx/uintptrSize,
		code.MapPos/uintptrSize,
		code.Length/uintptrSize,
	)
}

func (c *Opcode) dumpElem(code *Opcode) string {
	var length uintptr
	if code.Op.CodeType() == CodeArrayElem {
		length = code.Length
	} else {
		length = code.Length / uintptrSize
	}
	return fmt.Sprintf(
		`[%d]%s%s ([idx:%d][headIdx:%d][elemIdx:%d][length:%d][size:%d])`,
		code.DisplayIdx,
		strings.Repeat("-", code.Indent),
		code.Op,
		code.Idx/uintptrSize,
		code.HeadIdx/uintptrSize,
		code.ElemIdx/uintptrSize,
		length,
		code.Size,
	)
}

func (c *Opcode) dumpField(code *Opcode) string {
	return fmt.Sprintf(
		`[%d]%s%s ([idx:%d][key:%s][offset:%d][headIdx:%d])`,
		code.DisplayIdx,
		strings.Repeat("-", code.Indent),
		code.Op,
		code.Idx/uintptrSize,
		code.DisplayKey,
		code.Offset,
		code.HeadIdx/uintptrSize,
	)
}

func (c *Opcode) dumpKey(code *Opcode) string {
	return fmt.Sprintf(
		`[%d]%s%s ([idx:%d][elemIdx:%d][length:%d][mapIter:%d])`,
		code.DisplayIdx,
		strings.Repeat("-", code.Indent),
		code.Op,
		code.Idx/uintptrSize,
		code.ElemIdx/uintptrSize,
		code.Length/uintptrSize,
		code.MapIter/uintptrSize,
	)
}

func (c *Opcode) dumpValue(code *Opcode) string {
	return fmt.Sprintf(
		`[%d]%s%s ([idx:%d][mapIter:%d])`,
		code.DisplayIdx,
		strings.Repeat("-", code.Indent),
		code.Op,
		code.Idx/uintptrSize,
		code.MapIter/uintptrSize,
	)
}

func (c *Opcode) Dump() string {
	codes := []string{}
	for code := c; code.Op != OpEnd; {
		switch code.Op.CodeType() {
		case CodeSliceHead:
			codes = append(codes, c.dumpHead(code))
			code = code.Next
		case CodeMapHead:
			codes = append(codes, c.dumpMapHead(code))
			code = code.Next
		case CodeArrayElem, CodeSliceElem:
			codes = append(codes, c.dumpElem(code))
			code = code.End
		case CodeMapKey:
			codes = append(codes, c.dumpKey(code))
			code = code.End
		case CodeMapValue:
			codes = append(codes, c.dumpValue(code))
			code = code.Next
		case CodeMapEnd:
			codes = append(codes, c.dumpMapEnd(code))
			code = code.Next
		case CodeStructField:
			codes = append(codes, c.dumpField(code))
			code = code.Next
		case CodeStructEnd:
			codes = append(codes, c.dumpField(code))
			code = code.Next
		default:
			codes = append(codes, fmt.Sprintf(
				"[%d]%s%s ([idx:%d])",
				code.DisplayIdx,
				strings.Repeat("-", code.Indent),
				code.Op,
				code.Idx/uintptrSize,
			))
			code = code.Next
		}
	}
	return strings.Join(codes, "\n")
}

func prevField(code *Opcode, removedFields map[*Opcode]struct{}) *Opcode {
	if _, exists := removedFields[code]; exists {
		return prevField(code.PrevField, removedFields)
	}
	return code
}

func nextField(code *Opcode, removedFields map[*Opcode]struct{}) *Opcode {
	if _, exists := removedFields[code]; exists {
		return nextField(code.NextField, removedFields)
	}
	return code
}

func linkPrevToNextField(cur *Opcode, removedFields map[*Opcode]struct{}) {
	prev := prevField(cur.PrevField, removedFields)
	prev.NextField = nextField(cur.NextField, removedFields)
	code := prev
	fcode := cur
	for {
		var nextCode *Opcode
		switch code.Op.CodeType() {
		case CodeArrayElem, CodeSliceElem, CodeMapKey:
			nextCode = code.End
		default:
			nextCode = code.Next
		}
		if nextCode == fcode {
			code.Next = fcode.Next
			break
		} else if nextCode.Op == OpEnd {
			break
		}
		code = nextCode
	}
}

func newSliceHeaderCode(ctx *compileContext) *Opcode {
	idx := opcodeOffset(ctx.ptrIndex)
	ctx.incPtrIndex()
	elemIdx := opcodeOffset(ctx.ptrIndex)
	ctx.incPtrIndex()
	length := opcodeOffset(ctx.ptrIndex)
	return &Opcode{
		Op:         OpSlice,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        idx,
		HeadIdx:    idx,
		ElemIdx:    elemIdx,
		Length:     length,
		Indent:     ctx.indent,
	}
}

func newSliceElemCode(ctx *compileContext, head *Opcode, size uintptr) *Opcode {
	return &Opcode{
		Op:         OpSliceElem,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        opcodeOffset(ctx.ptrIndex),
		HeadIdx:    head.Idx,
		ElemIdx:    head.ElemIdx,
		Length:     head.Length,
		Indent:     ctx.indent,
		Size:       size,
	}
}

func newArrayHeaderCode(ctx *compileContext, alen int) *Opcode {
	idx := opcodeOffset(ctx.ptrIndex)
	ctx.incPtrIndex()
	elemIdx := opcodeOffset(ctx.ptrIndex)
	return &Opcode{
		Op:         OpArray,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        idx,
		HeadIdx:    idx,
		ElemIdx:    elemIdx,
		Indent:     ctx.indent,
		Length:     uintptr(alen),
	}
}

func newArrayElemCode(ctx *compileContext, head *Opcode, length int, size uintptr) *Opcode {
	return &Opcode{
		Op:         OpArrayElem,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        opcodeOffset(ctx.ptrIndex),
		ElemIdx:    head.ElemIdx,
		HeadIdx:    head.HeadIdx,
		Length:     uintptr(length),
		Indent:     ctx.indent,
		Size:       size,
	}
}

func newMapHeaderCode(ctx *compileContext) *Opcode {
	idx := opcodeOffset(ctx.ptrIndex)
	ctx.incPtrIndex()
	elemIdx := opcodeOffset(ctx.ptrIndex)
	ctx.incPtrIndex()
	length := opcodeOffset(ctx.ptrIndex)
	ctx.incPtrIndex()
	mapIter := opcodeOffset(ctx.ptrIndex)
	return &Opcode{
		Op:         OpMap,
		Type:       ctx.typ,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        idx,
		ElemIdx:    elemIdx,
		Length:     length,
		MapIter:    mapIter,
		Indent:     ctx.indent,
	}
}

func newMapKeyCode(ctx *compileContext, head *Opcode) *Opcode {
	return &Opcode{
		Op:         OpMapKey,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        opcodeOffset(ctx.ptrIndex),
		ElemIdx:    head.ElemIdx,
		Length:     head.Length,
		MapIter:    head.MapIter,
		Indent:     ctx.indent,
	}
}

func newMapValueCode(ctx *compileContext, head *Opcode) *Opcode {
	return &Opcode{
		Op:         OpMapValue,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        opcodeOffset(ctx.ptrIndex),
		ElemIdx:    head.ElemIdx,
		Length:     head.Length,
		MapIter:    head.MapIter,
		Indent:     ctx.indent,
	}
}

func newMapEndCode(ctx *compileContext, head *Opcode) *Opcode {
	mapPos := opcodeOffset(ctx.ptrIndex)
	ctx.incPtrIndex()
	idx := opcodeOffset(ctx.ptrIndex)
	return &Opcode{
		Op:         OpMapEnd,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        idx,
		Length:     head.Length,
		MapPos:     mapPos,
		Indent:     ctx.indent,
		Next:       newEndOp(ctx),
	}
}

func newInterfaceCode(ctx *compileContext) *Opcode {
	return &Opcode{
		Op:         OpInterface,
		Type:       ctx.typ,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        opcodeOffset(ctx.ptrIndex),
		Indent:     ctx.indent,
		Next:       newEndOp(ctx),
	}
}

func newRecursiveCode(ctx *compileContext, jmp *CompiledCode) *Opcode {
	return &Opcode{
		Op:         OpRecursive,
		Type:       ctx.typ,
		DisplayIdx: ctx.opcodeIndex,
		Idx:        opcodeOffset(ctx.ptrIndex),
		Indent:     ctx.indent,
		Next:       newEndOp(ctx),
		Jmp:        jmp,
	}
}