src/Opcodes.tbl
author David Anderson <dvander@alliedmods.net>
Sat Dec 22 18:22:14 2012 -0800 (2012-12-22)
changeset 230 fb3cd08bc7fb
parent 208 7c5be05296e3
child 234 26e3035cdffc
permissions -rw-r--r--
Re-add LIFO stack optimizations.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 // Opcode macros:
    20 //  NAME                TEXT            LENGTH  USES  DEFS   
    21 //
    22 // Type suffixes:
    23 //  I = 32-bit integer
    24 //  F = 32-bit IEEE-754 float
    25 //  O = object reference
    26 
    27 // No operation.
    28 OPDEF(OP_NOP,           "nop",          1,      0,  0)
    29 
    30 // Pushes a signed integer immediate onto the stack.
    31 OPDEF(OP_INT,           "int",          5,      0,  1)    
    32 
    33 // Returns from the current routine, propagating the value at the top of the
    34 // stack to the caller.
    35 OPDEF(OP_RETURN,        "return",       1,      1,  0)
    36 
    37 // value = pop()
    38 // if (value == [true, false])
    39 //    goto target
    40 OPDEF(OP_JT,            "jt",           5,      1,  0)
    41 OPDEF(OP_JF,            "jf",           5,      1,  0)
    42 
    43 // Unconditional jump.
    44 OPDEF(OP_JUMP,          "jump",         5,      0,  0)
    45 
    46 // Aliases for JUMP, that automatically leave LR regions.
    47 OPDEF(OP_BREAK,         "break",        5,      0,  0)
    48 OPDEF(OP_CONTINUE,      "continue",     5,      0,  0)
    49 
    50 // Gets or sets the value of a local variable onto the stack. The local
    51 // variable is given by an immediate index.
    52 OPDEF(OP_GETLOCAL,      "getlocal",     5,      0,  1)
    53 OPDEF(OP_SETLOCAL,      "setlocal",     5,      1,  1)
    54 
    55 // Stack transitions for these binary operators:
    56 // rhs = pop()
    57 // lhs = pop()
    58 // push(lhs OP rhs)
    59 OPDEF(OP_ADD,           "add",          1,      2,  1)
    60 OPDEF(OP_SUB,           "sub",          1,      2,  1)
    61 OPDEF(OP_MUL,           "mul",          1,      2,  1)
    62 OPDEF(OP_DIV,           "div",          1,      2,  1)
    63 OPDEF(OP_MOD,           "mod",          1,      2,  1)
    64 
    65 OPDEF(OP_LT,            "lt",           1,      2,  1)
    66 OPDEF(OP_LE,            "le",           1,      2,  1)
    67 OPDEF(OP_GE,            "ge",           1,      2,  1)
    68 OPDEF(OP_GT,            "gt",           1,      2,  1)
    69 OPDEF(OP_EQ,            "eq",           1,      2,  1)
    70 OPDEF(OP_NE,            "ne",           1,      2,  1)
    71 
    72 OPDEF(OP_NOT,           "not",          1,      1,  1)
    73 OPDEF(OP_NEG,           "neg",          1,      1,  1)
    74 
    75 OPDEF(OP_POP,           "pop",          1,      1,  0)
    76 
    77 // Unconditional terminator placed at the end of code. If a function returns
    78 // void, this acts as a RETURNVOID. Otherwise, it must be unreachable.
    79 OPDEF(OP_STOP,          "stop",         1,      0,  0)
    80 
    81 // Call a function with N arguments. The target function must be the last item
    82 // on the stack.
    83 OPDEF(OP_CALL,          "call",         1,      -1, 1)
    84 
    85 // Similar to GET/LOCAL, except for arguments.
    86 OPDEF(OP_GETARG,        "getarg",       5,      0,  1)
    87 OPDEF(OP_SETARG,        "setarg",       5,      1,  1)
    88 
    89 // Allocates either:
    90 //   A fixed array, 
    91 //   A 0-length dynamic array, or
    92 //   A dynamic array whose size is specified on the stack.
    93 //
    94 // The HeapTenure is stored as a byte following the pc. The ArrayMap to use
    95 // is specified as an index into the script's object table.
    96 //
    97 // Format:
    98 //  [u8 op] [u8 tenure] [u32 index]
    99 OPDEF(OP_NEWFIXED,      "newfixed",     6,      0, 1)
   100 OPDEF(OP_NEWEMPTY,      "newempty",     6,      0, 1)
   101 OPDEF(OP_NEWSIZED,      "newsized",     6,     -1, 1)
   102 
   103 // Performs an array-to-array assignment.
   104 //   Left = pop()
   105 //   Right = pop()
   106 //   copy(Left, Right)
   107 //   push(Left)
   108 OPDEF(OP_COPYARRAY,     "arraycopy",    1,      2,  1)
   109 
   110 // Reads or writes an integer element to an unsafe array.
   111 //  index = pop()
   112 //  ref = pop()
   113 //  ref[index]
   114 //
   115 OPDEF(OP_GETELEM,       "getelem",      1,      2,  1)
   116 OPDEF(OP_SETELEM,       "setelem",      1,      3,  1)
   117 
   118 // Pushes a 32-bit IEEE-754 floating point immediate onto the stack.
   119 OPDEF(OP_FLOAT,         "float",        5,      0,  1)
   120 
   121 // Converts an integer to a 32-bit IEEE-754 floating point immediate.
   122 OPDEF(OP_CVT_I2F,       "cvt.i2f",      1,      1,  1)
   123 OPDEF(OP_CVT_I2B,       "cvt.i2b",      1,      1,  1)
   124 
   125 // Stack transitions for these binary operators:
   126 // rhs = pop()
   127 // lhs = pop()
   128 // push(lhs OP rhs)
   129 OPDEF(OP_ADD_F,         "add.f",        1,      2,  1)
   130 OPDEF(OP_SUB_F,         "sub.f",        1,      2,  1)
   131 OPDEF(OP_MUL_F,         "mul.f",        1,      2,  1)
   132 OPDEF(OP_DIV_F,         "div.f",        1,      2,  1)
   133 
   134 OPDEF(OP_LT_F,          "lt.f",         1,      2,  1)
   135 OPDEF(OP_LE_F,          "le.f",         1,      2,  1)
   136 OPDEF(OP_GE_F,          "ge.f",         1,      2,  1)
   137 OPDEF(OP_GT_F,          "gt.f",         1,      2,  1)
   138 OPDEF(OP_EQ_F,          "eq.f",         1,      2,  1)
   139 OPDEF(OP_NE_F,          "ne.f",         1,      2,  1)
   140 
   141 OPDEF(OP_NOT_F,         "not.f",        1,      1,  1)
   142 OPDEF(OP_NEG_F,         "neg.f",        1,      1,  1)
   143 
   144 // Bitwise operators.
   145 OPDEF(OP_SHL,           "shl",          1,      2,  1)
   146 OPDEF(OP_SHR,           "shr",          1,      2,  1)
   147 OPDEF(OP_USHR,          "ushr",         1,      2,  1)
   148 OPDEF(OP_BITAND,        "bitand",       1,      2,  1)
   149 OPDEF(OP_BITOR,         "bitor",        1,      2,  1)
   150 OPDEF(OP_BITXOR,        "bitxor",       1,      2,  1)
   151 OPDEF(OP_BITNOT,        "bitnot",       1,      1,  1)
   152 
   153 // Saves or restores the position of the LIFO heap to/from a local slot.
   154 OPDEF(OP_SAVELIFO,      "savelifo",     5,      0,  0)
   155 OPDEF(OP_UNWINDLIFO,    "unwindlifo",   5,      0,  0)
   156 
   157 // Composes a variable reference to a stack slot or argument.
   158 OPDEF(OP_VARREF_LIFO,   "varref.lifo",  5,      0,  1)
   159 OPDEF(OP_ARGREF_LIFO,   "argref.lifo",  5,      0,  1)
   160 OPDEF(OP_SLOTREF_LIFO,  "slotref.lifo", 1,      1,  1)
   161 OPDEF(OP_GETREF_I,      "getref.i",     1,      1,  1)
   162 OPDEF(OP_SETREF_I,      "setref.i",     1,      2,  1)
   163 OPDEF(OP_GETREF_F,      "getref.f",     1,      1,  1)
   164 OPDEF(OP_SETREF_F,      "setref.f",     1,      2,  1)
   165 
   166 // Pushes an object reference onto the stack.
   167 OPDEF(OP_OBJECT,        "object",       5,      0,  1)
   168 
   169 // Duplicates the top item on the stack.
   170 OPDEF(OP_DUP,           "dup",          1,      1,  2)
   171 
   172 // Duplicates the top two items on the stack.
   173 OPDEF(OP_DUP2,          "dup2",         1,      2,  4)
   174 
   175 // Swaps the two items at the top of the stack.
   176 OPDEF(OP_SWAP,          "swap",         1,      2,  2)
   177 
   178 // Roll operation is the same as Forth. It steals a value on the stack,
   179 // shifting all stack values down, and then places the stolen value back at
   180 // the top of the stack.
   181 //
   182 //  p = sp[-3]
   183 //  sp[-3] = sp[-2]
   184 //  sp[-2] = sp[-1]
   185 //  sp[-1] = p
   186 OPDEF(OP_ROLL3,         "roll3",        1,      3, 3)
   187 
   188 // Pick operation is the same as Forth. It pushes a copy of a slot at a
   189 // specific depth.
   190 //
   191 // push(sp[-n])
   192 OPDEF(OP_PICK2,         "pick2",        1,      2, 3)
   193 OPDEF(OP_PICK3,         "pick3",        1,      3, 4)
   194 
   195 // Calls a native function. During the course of calling a native, arguments
   196 // on the stack are re-pushed to match the cell-based native calling
   197 // convention expected by ISourcePawnEngine.
   198 //
   199 // Unlike the normal CALL instruction, natives take truly variadic arguments
   200 // and so we must specify the number of arguments.
   201 //
   202 // [u8 Opcode] [u32 ArgCount]
   203 //
   204 OPDEF(OP_CALLNATIVE,    "callnative",   5,     -1, 1)
   205 
   206 // Returns from a function that does return a value.
   207 OPDEF(OP_RETURNVOID,    "returnvoid",   1,      0, 0)
   208 
   209 // Opcodes for accessing an environment slot in the global scope. The index
   210 // maps into the code object's string table, and represents the name of the
   211 // global variable.
   212 OPDEF(OP_SETGLOBAL,     "setglobal",    5,      1, 1)
   213 OPDEF(OP_GETGLOBAL,     "getglobal",    5,      0, 1)
   214 
   215 // Reinterprets a 32-bit integer as a float, or vice-versa.
   216 OPDEF(OP_BITS_I2F,      "bits.i2f",     1,      1, 1)
   217 OPDEF(OP_BITS_F2I,      "bits.f2i",     1,      1, 1)
   218 
   219 // Pushes a boolean value.
   220 OPDEF(OP_TRUE,          "true",         1,      0, 1)
   221 OPDEF(OP_FALSE,         "false",        1,      0, 1)
   222 
   223 // Allocate a struct. The HeapTenure is stored as a byte following pc. The
   224 // StructMap to use is specified as an index into the script's Map table.
   225 //
   226 // Format:
   227 //  [u8 op] [u8 tenure] [u32 index]
   228 OPDEF(OP_NEWSTRUCT,     "newstruct",    6,      0, 1)
   229 
   230 // Performs a struct-to-struct assignment.
   231 //   Left = pop()
   232 //   Right = pop()
   233 //   copy(Left, Right)
   234 //   push(Left)
   235 OPDEF(OP_COPYSTRUCT,    "structcopy",   1,      2, 1)
   236 
   237 // Sets or gets a property in a struct. The index of the field is specified as
   238 // a 32-bit integer. Note that these opcodes follow a new style of deriving
   239 // their types at runtime.
   240 OPDEF(OP_GETFIELD,      "getfield",     5,      1, 1)
   241 OPDEF(OP_SETFIELD,      "setfield",     5,      2, 1)
   242 
   243 // Table-based jump.
   244 // [u8 op] [i32 low] [i32 high] [u32 jump] [u32 jumps...]
   245 OPDEF(OP_TABLESWITCH,   "tableswitch",  9,      1, 0)
   246 
   247 OPDEF(OP_SIZEOF,        "sizeof",       1,      1, 1)
   248 
   249 // Registers a function as a publicly exported name.
   250 OPDEF(OP_PUBLIC,        "public",       1,      1, 0)
   251 
   252 // Enter and leave loop bodies. This is to track positions in the LIFO stack.
   253 // This is only used for the outermost scope inside loop bodies. BREAK and
   254 // CONTINUE implicitly perform a LEAVELOOP.
   255 //
   256 // If the second byte to ENTERLOOP is 0, then no LIFO region is needed.
   257 OPDEF(OP_ENTERLOOP,     "enterloop",    2,      0, 0)
   258 OPDEF(OP_LEAVELOOP,     "leaveloop",    1,      0, 0)