package vm

import (
	
	
	
	

	
	
	
)

// The functions which the template calls as appendInt, appendUint and so on are the ones of the encoder
// package here, and the generator makes the VM call them directly: a call through a function variable is a load
// and an indirect call, for every value the VM appends. appendScalar has them as variables for its own calls.
var (
	appendInt           = encoder.AppendInt
	appendUint          = encoder.AppendUint
	appendFloat64       = encoder.AppendFloat64
	appendByteSlice     = encoder.AppendByteSlice
	appendNumber        = encoder.AppendNumber
	appendNumberString  = encoder.AppendNumberString
	errUnsupportedFloat = encoder.ErrUnsupportedFloat
)

type emptyInterface struct {
	typ unsafe.Pointer
	ptr unsafe.Pointer
}

func errUnimplementedOp( encoder.OpType) error {
	return fmt.Errorf("encoder: opcode %s has not been implemented", )
}

// load / store are for the half of a slot for a pointer, and loadInt / storeInt are for the other half.
// A pointer is stored as uintptr, which needs no write barrier: see encoder.Slot.

func load( unsafe.Pointer,  uint32) unsafe.Pointer {
	return *(*unsafe.Pointer)(unsafe.Add(, ))
}

func store( unsafe.Pointer,  uint32,  unsafe.Pointer) {
	*(*uintptr)(unsafe.Add(, )) = uintptr()
}

func loadInt( unsafe.Pointer,  uint32) uintptr {
	return *(*uintptr)(unsafe.Add(, ))
}

func storeInt( unsafe.Pointer,  uint32,  uintptr) {
	*(*uintptr)(unsafe.Add(, )) = 
}

func ptrToUint64( unsafe.Pointer,  uint8) uint64 {
	switch  {
	case 8:
		return uint64(*(*uint8)())
	case 16:
		return uint64(*(*uint16)())
	case 32:
		return uint64(*(*uint32)())
	case 64:
		return *(*uint64)()
	}
	return 0
}
func ptrToFloat32( unsafe.Pointer) float32 { return *(*float32)() }

// isInfOrNaN is whether the float is not finite, which encoding/json refuses: the exponent is all ones.
// math.IsInf is not inlined into Run, which is a big function to the inliner; this is.
func isInfOrNaN( float64) bool {
	const  = 0x7ff << 52
	return math.Float64bits()& == 
}

// appendFloat32 appends the float, or returns the error for one which is not finite as encoding/json does.
func appendFloat32( *encoder.RuntimeContext,  *encoder.Opcode,  []byte,  float32) ([]byte, error) {
	if isInfOrNaN(float64()) {
		return nil, errUnsupportedFloat(, , float64())
	}
	return encoder.AppendFloat32(, , ), nil
}
func ptrToFloat64( unsafe.Pointer) float64            { return *(*float64)() }
func ptrToBool( unsafe.Pointer) bool                  { return *(*bool)() }
func ptrToBytes( unsafe.Pointer) []byte               { return *(*[]byte)() }
func ptrToNumber( unsafe.Pointer) json.Number         { return *(*json.Number)() }
func ptrToString( unsafe.Pointer) string              { return *(*string)() }
func ptrToSlice( unsafe.Pointer) *runtime.SliceHeader { return (*runtime.SliceHeader)() }
func ptrToPtr( unsafe.Pointer) unsafe.Pointer         { return *(*unsafe.Pointer)() }

// ptrToNPtr follows the pointer ptrNum times, or up to a nil one. It is written to be inlined into Run,
// which is a big function: only a function whose cost is 20 or less is inlined into it.
func ptrToNPtr( unsafe.Pointer,  uint8) unsafe.Pointer {
	for ;  > 0 &&  != nil; -- {
		 = *(*unsafe.Pointer)()
	}
	return 
}

func appendBool( *encoder.RuntimeContext,  []byte,  bool) []byte {
	if  {
		return append(, "true"...)
	}
	return append(, "false"...)
}

func appendNull( *encoder.RuntimeContext,  []byte) []byte {
	return append(, "null"...)
}

func appendComma( *encoder.RuntimeContext,  []byte) []byte {
	return append(, ',')
}

func appendNullComma( *encoder.RuntimeContext,  []byte) []byte {
	return append(, "null,"...)
}

func appendColon( *encoder.RuntimeContext,  []byte) []byte {
	 := len() - 1
	[] = ':'
	return 
}

// mapKeyFooterLen returns the length of the codes of a color which end the names of the keys written by code: none
// without colors.
func mapKeyFooterLen( *encoder.RuntimeContext,  *encoder.Opcode) int { return 0 }

// appendSortedMapKey appends the key of an entry of a sorted map, as it was encoded with a comma after it, and the
// colon after it.
func appendSortedMapKey( *encoder.RuntimeContext,  *encoder.Opcode, ,  []byte) []byte {
	 = append(, ...)
	[len()-1] = ':'
	return 
}

func appendMarshalJSON( *encoder.RuntimeContext,  *encoder.Opcode,  []byte,  unsafe.Pointer) ([]byte, error) {
	return encoder.AppendMarshalJSON(, , , )
}

func appendMarshalText( *encoder.RuntimeContext,  *encoder.Opcode,  []byte,  unsafe.Pointer) ([]byte, error) {
	return encoder.AppendMarshalText(, , , )
}

func appendArrayHead( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	return append(, '[')
}

func appendArrayEnd( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	 := len() - 1
	[] = ']'
	return append(, ',')
}

func appendEmptyArray( *encoder.RuntimeContext,  []byte) []byte {
	return append(, '[', ']', ',')
}

func appendEmptyObject( *encoder.RuntimeContext,  []byte) []byte {
	return append(, '{', '}', ',')
}

func appendObjectEnd( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	 := len() - 1
	[] = '}'
	return append(, ',')
}

func appendStructHead( *encoder.RuntimeContext,  []byte) []byte {
	return append(, '{')
}

// appendStructKey appends a key which is up to a chunk long, by two copies of a half chunk each: the key is
// followed by its padding in memory ( encoder.PaddedKey ), and the length is cut back to the key after.
// A copy of a half chunk is inlined by the compiler on amd64 and arm64; a call of memmove, or of this
// function, makes the VM spill and restore its registers, which costs more than the copy of a key.
//
// Run is a big function to the inliner, which inlines into it only a function whose cost is 20 or less:
// this one costs 19. A key longer than a chunk is written by appendLongStructKey, by the generic field opcode.
func appendStructKey( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	const  = encoder.KeyChunkSize / 2
	return append(append(, .KeyChunk[:]...), .KeyChunk[:]...)[:len()+len(.Key)]
}

// appendLongStructKey appends a key of any length.
func appendLongStructKey( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	return append(, .Key...)
}

func appendStructEnd( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	return append(, '}', ',')
}

// commaAtEnd is 1 for a comma: what appendStructEndSkipLast cuts from the end.
var commaAtEnd = [256]uint8{',': 1}

// appendStructEndSkipLast ends the struct, over the comma after its last field if there is one: there is none
// after an empty struct or when every field is omitted. The comma is cut by a table, not a branch, so that
// the function costs 18 to the inliner and is inlined into Run ( see appendStructKey ).
func appendStructEndSkipLast( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	return append([:len()-int(commaAtEnd[[len()-1]])], '}', ',')
}

func appendMapKeyIndent( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte    { return  }
func appendArrayElemIndent( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte { return  }

// appendScalar appends the value at p by the opcode of a scalar, with the comma: what the VM does for the opcode.
// It is for a scalar held by an interface value, which is encoded without a frame.
//
//go:noinline
func appendScalar( *encoder.RuntimeContext,  []byte,  *encoder.Opcode,  unsafe.Pointer) ([]byte, error) {
	switch .Op {
	case encoder.OpInt:
		 = appendInt(, , , )
	case encoder.OpUint:
		 = appendUint(, , , )
	case encoder.OpFloat32:
		,  := appendFloat32(, , , ptrToFloat32())
		if  != nil {
			return nil, 
		}
		 = 
	case encoder.OpFloat64:
		 := ptrToFloat64()
		if isInfOrNaN() {
			return nil, errUnsupportedFloat(, , )
		}
		 = appendFloat64(, , )
	case encoder.OpString:
		, _ = jsonstring.AppendQuoted(encoder.StringEscaper(), , ptrToString())
	case encoder.OpBool:
		 = appendBool(, , ptrToBool())
	case encoder.OpBytes:
		 = appendByteSlice(, , ptrToBytes())
	case encoder.OpNumber:
		,  := appendNumber(, , ptrToNumber())
		if  != nil {
			return nil, 
		}
		 = 
	default:
		return nil, errUnimplementedOp(.Op)
	}
	return appendComma(, ), nil
}