2021-03-16 13:44:32 +03:00
|
|
|
package encoder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/goccy/go-json/internal/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
const uintptrSize = 4 << (^uintptr(0) >> 63)
|
|
|
|
|
2021-06-12 11:06:26 +03:00
|
|
|
type OpFlags uint16
|
2021-05-29 08:47:18 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
AnonymousHeadFlags OpFlags = 1 << 0
|
|
|
|
AnonymousKeyFlags OpFlags = 1 << 1
|
|
|
|
IndirectFlags OpFlags = 1 << 2
|
|
|
|
IsTaggedKeyFlags OpFlags = 1 << 3
|
|
|
|
NilCheckFlags OpFlags = 1 << 4
|
|
|
|
AddrForMarshalerFlags OpFlags = 1 << 5
|
|
|
|
IsNextOpPtrTypeFlags OpFlags = 1 << 6
|
|
|
|
IsNilableTypeFlags OpFlags = 1 << 7
|
2021-06-12 11:06:26 +03:00
|
|
|
MarshalerContextFlags OpFlags = 1 << 8
|
2021-05-29 08:47:18 +03:00
|
|
|
)
|
|
|
|
|
2021-03-16 13:44:32 +03:00
|
|
|
type Opcode struct {
|
2021-05-29 08:47:18 +03:00
|
|
|
Op OpType // operation type
|
|
|
|
Idx uint32 // offset to access ptr
|
|
|
|
Next *Opcode // next opcode
|
|
|
|
End *Opcode // array/slice/struct/map end
|
|
|
|
NextField *Opcode // next struct field
|
|
|
|
Key string // struct field key
|
|
|
|
Offset uint32 // offset size from struct header
|
|
|
|
PtrNum uint8 // pointer number: e.g. double pointer is 2.
|
|
|
|
NumBitSize uint8
|
2021-06-12 11:06:26 +03:00
|
|
|
Flags OpFlags
|
2021-05-29 08:47:18 +03:00
|
|
|
|
|
|
|
Type *runtime.Type // go type
|
|
|
|
PrevField *Opcode // prev struct field
|
|
|
|
Jmp *CompiledCode // for recursive call
|
|
|
|
ElemIdx uint32 // offset to access array/slice/map elem
|
|
|
|
Length uint32 // offset to access slice/map length or array length
|
|
|
|
MapIter uint32 // offset to access map iterator
|
|
|
|
MapPos uint32 // offset to access position list for sorted map
|
|
|
|
Indent uint32 // indent number
|
|
|
|
Size uint32 // array/slice elem size
|
|
|
|
DisplayIdx uint32 // opcode index
|
|
|
|
DisplayKey string // key text to display
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-06-02 13:02:36 +03:00
|
|
|
func (c *Opcode) MaxIdx() uint32 {
|
|
|
|
max := uint32(0)
|
|
|
|
for _, value := range []uint32{
|
|
|
|
c.Idx,
|
|
|
|
c.ElemIdx,
|
|
|
|
c.Length,
|
|
|
|
c.MapIter,
|
|
|
|
c.MapPos,
|
|
|
|
c.Size,
|
|
|
|
} {
|
|
|
|
if max < value {
|
|
|
|
max = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:54:22 +03:00
|
|
|
func (c *Opcode) ToHeaderType(isString bool) OpType {
|
2021-03-16 13:44:32 +03:00
|
|
|
switch c.Op {
|
|
|
|
case OpInt:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadIntString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadInt
|
|
|
|
case OpIntPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadIntPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadIntPtr
|
|
|
|
case OpUint:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadUintString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadUint
|
|
|
|
case OpUintPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadUintPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadUintPtr
|
|
|
|
case OpFloat32:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadFloat32String
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadFloat32
|
|
|
|
case OpFloat32Ptr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadFloat32PtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadFloat32Ptr
|
|
|
|
case OpFloat64:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadFloat64String
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadFloat64
|
|
|
|
case OpFloat64Ptr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadFloat64PtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadFloat64Ptr
|
|
|
|
case OpString:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadStringString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadString
|
|
|
|
case OpStringPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadStringPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadStringPtr
|
|
|
|
case OpNumber:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadNumberString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadNumber
|
|
|
|
case OpNumberPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadNumberPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadNumberPtr
|
|
|
|
case OpBool:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadBoolString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadBool
|
|
|
|
case OpBoolPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructHeadBoolPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructHeadBoolPtr
|
2021-03-19 17:31:29 +03:00
|
|
|
case OpBytes:
|
|
|
|
return OpStructHeadBytes
|
|
|
|
case OpBytesPtr:
|
|
|
|
return OpStructHeadBytesPtr
|
2021-03-16 13:44:32 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:54:22 +03:00
|
|
|
func (c *Opcode) ToFieldType(isString bool) OpType {
|
2021-03-16 13:44:32 +03:00
|
|
|
switch c.Op {
|
|
|
|
case OpInt:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldIntString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldInt
|
|
|
|
case OpIntPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldIntPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldIntPtr
|
|
|
|
case OpUint:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldUintString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldUint
|
|
|
|
case OpUintPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldUintPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldUintPtr
|
|
|
|
case OpFloat32:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldFloat32String
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldFloat32
|
|
|
|
case OpFloat32Ptr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldFloat32PtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldFloat32Ptr
|
|
|
|
case OpFloat64:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldFloat64String
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldFloat64
|
|
|
|
case OpFloat64Ptr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldFloat64PtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldFloat64Ptr
|
|
|
|
case OpString:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldStringString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldString
|
|
|
|
case OpStringPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldStringPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldStringPtr
|
|
|
|
case OpNumber:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldNumberString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldNumber
|
|
|
|
case OpNumberPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldNumberPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldNumberPtr
|
|
|
|
case OpBool:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldBoolString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldBool
|
|
|
|
case OpBoolPtr:
|
2021-05-06 14:54:22 +03:00
|
|
|
if isString {
|
|
|
|
return OpStructFieldBoolPtrString
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
return OpStructFieldBoolPtr
|
2021-03-19 17:31:29 +03:00
|
|
|
case OpBytes:
|
|
|
|
return OpStructFieldBytes
|
|
|
|
case OpBytesPtr:
|
|
|
|
return OpStructFieldBytesPtr
|
2021-03-16 13:44:32 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2021-05-29 08:47:18 +03:00
|
|
|
func opcodeOffset(idx int) uint32 {
|
|
|
|
return uint32(idx) * uintptrSize
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-05-29 08:47:18 +03:00
|
|
|
Idx: opcodeOffset(ctx.ptrIndex),
|
|
|
|
Next: next,
|
2021-03-16 13:44:32 +03:00
|
|
|
Type: ctx.typ,
|
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
|
|
|
Indent: ctx.indent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2021-05-29 08:47:18 +03:00
|
|
|
Op: c.Op,
|
|
|
|
Key: c.Key,
|
|
|
|
PtrNum: c.PtrNum,
|
|
|
|
NumBitSize: c.NumBitSize,
|
|
|
|
Flags: c.Flags,
|
|
|
|
Idx: c.Idx,
|
|
|
|
Offset: c.Offset,
|
|
|
|
Type: c.Type,
|
|
|
|
DisplayIdx: c.DisplayIdx,
|
|
|
|
DisplayKey: c.DisplayKey,
|
|
|
|
ElemIdx: c.ElemIdx,
|
|
|
|
Length: c.Length,
|
|
|
|
MapIter: c.MapIter,
|
|
|
|
MapPos: c.MapPos,
|
|
|
|
Size: c.Size,
|
|
|
|
Indent: c.Indent,
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
codeMap[addr] = copied
|
|
|
|
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; {
|
2021-06-02 13:02:36 +03:00
|
|
|
maxIdx := int(code.MaxIdx() / uintptrSize)
|
|
|
|
if idx < maxIdx {
|
|
|
|
idx = maxIdx
|
|
|
|
}
|
2021-03-19 09:46:53 +03:00
|
|
|
if code.Op == OpRecursiveEnd {
|
2021-03-16 13:44:32 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
switch code.Op.CodeType() {
|
|
|
|
case CodeArrayElem, CodeSliceElem, CodeMapKey:
|
|
|
|
code = code.End
|
|
|
|
default:
|
|
|
|
code = code.Next
|
|
|
|
}
|
|
|
|
}
|
2021-06-02 13:02:36 +03:00
|
|
|
return idx + 1
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Opcode) decOpcodeIndex() {
|
|
|
|
for code := c; code.Op != OpEnd; {
|
|
|
|
code.DisplayIdx--
|
2021-05-29 08:47:18 +03:00
|
|
|
if code.Idx > 0 {
|
|
|
|
code.Idx -= uintptrSize
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
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 {
|
2021-05-29 08:47:18 +03:00
|
|
|
var length uint32
|
2021-03-16 13:44:32 +03:00
|
|
|
if code.Op.CodeType() == CodeArrayHead {
|
|
|
|
length = code.Length
|
|
|
|
} else {
|
|
|
|
length = code.Length / uintptrSize
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(
|
2021-05-29 08:47:18 +03:00
|
|
|
`[%d]%s%s ([idx:%d][elemIdx:%d][length:%d])`,
|
2021-03-16 13:44:32 +03:00
|
|
|
code.DisplayIdx,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
code.Op,
|
|
|
|
code.Idx/uintptrSize,
|
|
|
|
code.ElemIdx/uintptrSize,
|
|
|
|
length,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Opcode) dumpMapHead(code *Opcode) string {
|
|
|
|
return fmt.Sprintf(
|
2021-05-29 08:47:18 +03:00
|
|
|
`[%d]%s%s ([idx:%d][elemIdx:%d][length:%d][mapIter:%d])`,
|
2021-03-16 13:44:32 +03:00
|
|
|
code.DisplayIdx,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
code.Op,
|
|
|
|
code.Idx/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,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
code.Op,
|
|
|
|
code.Idx/uintptrSize,
|
|
|
|
code.MapPos/uintptrSize,
|
|
|
|
code.Length/uintptrSize,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Opcode) dumpElem(code *Opcode) string {
|
2021-05-29 08:47:18 +03:00
|
|
|
var length uint32
|
2021-03-16 13:44:32 +03:00
|
|
|
if code.Op.CodeType() == CodeArrayElem {
|
|
|
|
length = code.Length
|
|
|
|
} else {
|
|
|
|
length = code.Length / uintptrSize
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(
|
2021-05-29 08:47:18 +03:00
|
|
|
`[%d]%s%s ([idx:%d][elemIdx:%d][length:%d][size:%d])`,
|
2021-03-16 13:44:32 +03:00
|
|
|
code.DisplayIdx,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
code.Op,
|
|
|
|
code.Idx/uintptrSize,
|
|
|
|
code.ElemIdx/uintptrSize,
|
|
|
|
length,
|
|
|
|
code.Size,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Opcode) dumpField(code *Opcode) string {
|
|
|
|
return fmt.Sprintf(
|
2021-05-29 08:47:18 +03:00
|
|
|
`[%d]%s%s ([idx:%d][key:%s][offset:%d])`,
|
2021-03-16 13:44:32 +03:00
|
|
|
code.DisplayIdx,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
code.Op,
|
|
|
|
code.Idx/uintptrSize,
|
|
|
|
code.DisplayKey,
|
|
|
|
code.Offset,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Opcode) dumpKey(code *Opcode) string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
`[%d]%s%s ([idx:%d][elemIdx:%d][length:%d][mapIter:%d])`,
|
|
|
|
code.DisplayIdx,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
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,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
code.Op,
|
|
|
|
code.Idx/uintptrSize,
|
|
|
|
code.MapIter/uintptrSize,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:19 +03:00
|
|
|
func (c *Opcode) Dump() string {
|
2021-03-16 13:44:32 +03:00
|
|
|
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,
|
2021-05-29 08:47:18 +03:00
|
|
|
strings.Repeat("-", int(code.Indent)),
|
2021-03-16 13:44:32 +03:00
|
|
|
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,
|
|
|
|
Idx: idx,
|
2021-05-29 08:47:18 +03:00
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
2021-03-16 13:44:32 +03:00
|
|
|
ElemIdx: elemIdx,
|
|
|
|
Length: length,
|
|
|
|
Indent: ctx.indent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSliceElemCode(ctx *compileContext, head *Opcode, size uintptr) *Opcode {
|
|
|
|
return &Opcode{
|
|
|
|
Op: OpSliceElem,
|
2021-05-29 08:47:18 +03:00
|
|
|
Idx: head.Idx,
|
2021-03-16 13:44:32 +03:00
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
|
|
|
ElemIdx: head.ElemIdx,
|
|
|
|
Length: head.Length,
|
|
|
|
Indent: ctx.indent,
|
2021-05-29 08:47:18 +03:00
|
|
|
Size: uint32(size),
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newArrayHeaderCode(ctx *compileContext, alen int) *Opcode {
|
|
|
|
idx := opcodeOffset(ctx.ptrIndex)
|
|
|
|
ctx.incPtrIndex()
|
|
|
|
elemIdx := opcodeOffset(ctx.ptrIndex)
|
|
|
|
return &Opcode{
|
|
|
|
Op: OpArray,
|
|
|
|
Idx: idx,
|
2021-05-29 08:47:18 +03:00
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
2021-03-16 13:44:32 +03:00
|
|
|
ElemIdx: elemIdx,
|
|
|
|
Indent: ctx.indent,
|
2021-05-29 08:47:18 +03:00
|
|
|
Length: uint32(alen),
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newArrayElemCode(ctx *compileContext, head *Opcode, length int, size uintptr) *Opcode {
|
|
|
|
return &Opcode{
|
|
|
|
Op: OpArrayElem,
|
2021-05-29 08:47:18 +03:00
|
|
|
Idx: head.Idx,
|
2021-03-16 13:44:32 +03:00
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
|
|
|
ElemIdx: head.ElemIdx,
|
2021-05-29 08:47:18 +03:00
|
|
|
Length: uint32(length),
|
2021-03-16 13:44:32 +03:00
|
|
|
Indent: ctx.indent,
|
2021-05-29 08:47:18 +03:00
|
|
|
Size: uint32(size),
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-05-29 08:47:18 +03:00
|
|
|
Idx: idx,
|
2021-03-16 13:44:32 +03:00
|
|
|
Type: ctx.typ,
|
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
|
|
|
ElemIdx: elemIdx,
|
|
|
|
Length: length,
|
|
|
|
MapIter: mapIter,
|
|
|
|
Indent: ctx.indent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMapKeyCode(ctx *compileContext, head *Opcode) *Opcode {
|
|
|
|
return &Opcode{
|
|
|
|
Op: OpMapKey,
|
|
|
|
Idx: opcodeOffset(ctx.ptrIndex),
|
2021-05-29 08:47:18 +03:00
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
2021-03-16 13:44:32 +03:00
|
|
|
ElemIdx: head.ElemIdx,
|
|
|
|
Length: head.Length,
|
|
|
|
MapIter: head.MapIter,
|
|
|
|
Indent: ctx.indent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMapValueCode(ctx *compileContext, head *Opcode) *Opcode {
|
|
|
|
return &Opcode{
|
|
|
|
Op: OpMapValue,
|
|
|
|
Idx: opcodeOffset(ctx.ptrIndex),
|
2021-05-29 08:47:18 +03:00
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
2021-03-16 13:44:32 +03:00
|
|
|
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,
|
|
|
|
Idx: idx,
|
2021-05-29 08:47:18 +03:00
|
|
|
Next: newEndOp(ctx),
|
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
2021-03-16 13:44:32 +03:00
|
|
|
Length: head.Length,
|
|
|
|
MapPos: mapPos,
|
|
|
|
Indent: ctx.indent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInterfaceCode(ctx *compileContext) *Opcode {
|
|
|
|
return &Opcode{
|
|
|
|
Op: OpInterface,
|
2021-05-29 08:47:18 +03:00
|
|
|
Idx: opcodeOffset(ctx.ptrIndex),
|
|
|
|
Next: newEndOp(ctx),
|
2021-03-16 13:44:32 +03:00
|
|
|
Type: ctx.typ,
|
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
|
|
|
Indent: ctx.indent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRecursiveCode(ctx *compileContext, jmp *CompiledCode) *Opcode {
|
|
|
|
return &Opcode{
|
2021-03-19 09:46:53 +03:00
|
|
|
Op: OpRecursive,
|
2021-05-29 08:47:18 +03:00
|
|
|
Idx: opcodeOffset(ctx.ptrIndex),
|
|
|
|
Next: newEndOp(ctx),
|
2021-03-16 13:44:32 +03:00
|
|
|
Type: ctx.typ,
|
|
|
|
DisplayIdx: ctx.opcodeIndex,
|
|
|
|
Indent: ctx.indent,
|
|
|
|
Jmp: jmp,
|
|
|
|
}
|
|
|
|
}
|