github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/typ/opcode.go (about)

     1  package typ
     2  
     3  import "unsafe"
     4  
     5  type Inst struct {
     6  	Opcode    byte
     7  	OpcodeExt byte
     8  	A         uint16
     9  	B         uint16
    10  	C         uint16
    11  }
    12  
    13  const InstSize = unsafe.Sizeof(Inst{})
    14  
    15  func (i Inst) D() int32 {
    16  	// return int32(uint32(i.B)<<16 | uint32(i.C))
    17  	return *(*int32)(unsafe.Pointer(&i.B))
    18  }
    19  
    20  func (i Inst) SetD(d int32) Inst {
    21  	// i.B = uint16(uint32(d) >> 16)
    22  	// i.C = uint16(uint32(d))
    23  	*(*int32)(unsafe.Pointer(&i.B)) = d
    24  	return i
    25  }
    26  
    27  const (
    28  	_ = iota
    29  	OpSet
    30  	OpStore
    31  	OpLoad
    32  	OpExt
    33  	OpAdd
    34  	OpSub
    35  	OpMul
    36  	OpDiv
    37  	OpIDiv
    38  	OpInc
    39  	OpMod
    40  	OpLen
    41  	OpNext
    42  	OpNot
    43  	OpEq
    44  	OpNeq
    45  	OpLess
    46  	OpLessEq
    47  	OpJmpFalse
    48  	OpJmp
    49  	OpFunction
    50  	OpPush
    51  	OpPushUnpack
    52  	OpCreateArray
    53  	OpCreateObject
    54  	OpCall
    55  	OpTailCall
    56  	OpIsProto
    57  	OpSlice
    58  	OpRet
    59  	OpLoadTop
    60  
    61  	_ = iota
    62  	OpExtAdd16
    63  	OpExtRSub16
    64  	OpExtLess16
    65  	OpExtGreat16
    66  	OpExtEq16
    67  	OpExtNeq16
    68  	OpExtInc16
    69  	OpExtLoad16
    70  	OpExtStore16
    71  	OpExtBitAnd
    72  	OpExtBitOr
    73  	OpExtBitXor
    74  	OpExtBitLsh
    75  	OpExtBitRsh
    76  	OpExtBitURsh
    77  	OpExtBitAnd16
    78  	OpExtBitOr16
    79  	OpExtBitXor16
    80  	OpExtBitLsh16
    81  	OpExtBitRsh16
    82  	OpExtBitURsh16
    83  )
    84  
    85  func JmpInst(op byte, distance int) Inst {
    86  	if distance < -(1<<30) || distance >= 1<<30 {
    87  		panic("long jump")
    88  	}
    89  	return (Inst{Opcode: op}).SetD(int32(distance))
    90  }
    91  
    92  var BinaryOpcode = map[byte]string{
    93  	OpAdd:        "add",
    94  	OpSub:        "sub",
    95  	OpMul:        "mul",
    96  	OpDiv:        "div",
    97  	OpIDiv:       "idiv",
    98  	OpMod:        "mod",
    99  	OpEq:         "eq",
   100  	OpNeq:        "neq",
   101  	OpLess:       "less",
   102  	OpLessEq:     "lesseq",
   103  	OpLoad:       "load",
   104  	OpNext:       "next",
   105  	OpIsProto:    "isproto",
   106  	OpInc:        "inc",
   107  	OpExtBitAnd:  "and",
   108  	OpExtBitOr:   "or",
   109  	OpExtBitXor:  "xor",
   110  	OpExtBitLsh:  "lsh",
   111  	OpExtBitRsh:  "rsh",
   112  	OpExtBitURsh: "ursh",
   113  }
   114  
   115  var UnaryOpcode = map[byte]string{
   116  	OpNot:        "not",
   117  	OpRet:        "return",
   118  	OpLen:        "len",
   119  	OpPush:       "push",
   120  	OpPushUnpack: "pushvarg",
   121  }
   122  
   123  var TenaryOpcode = map[byte]string{
   124  	OpLoad:  "load",
   125  	OpStore: "store",
   126  	OpSlice: "slice",
   127  }