jitcraft/Opcodes.tbl
author David Anderson <dvander@alliedmods.net>
Wed Jun 27 02:21:56 2012 -0700 (2012-06-27)
changeset 4 633176823669
parent 0 c455ebb9a5dc
child 6 172dd7799d0a
permissions -rw-r--r--
Add support for float variables and basic math.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 // Opcode macros:
    19 //  NAME            NUMBER      TEXT                LENGTH  USES  DEFS   
    20 //
    21 // Type suffixes:
    22 //  I = 32-bit integer
    23 //  P = reference to the UnsafeHeap or UnsafeStack
    24 
    25 // No operation.
    26 OPDEF(OP_NOP,           0,          "nop",          1,      0,  0)
    27 
    28 // Pushes a signed integer immediate onto the stack.
    29 OPDEF(OP_INT,           1,          "int",          5,      0,  1)    
    30 
    31 // Returns from the current routine, propagating the value at the top of the
    32 // stack to the caller.
    33 OPDEF(OP_RETURN,        2,          "return",       1,      1,  0)
    34 
    35 // Stack transitions for these comparison ops:
    36 // rhs = pop()
    37 // lhs = pop()
    38 // if (lhs COMPARE rhs)
    39 //    goto target (immediate, relative to the beginning of the jump instruction)
    40 OPDEF(OP_JL,            3,          "jl",           5,      2,  0)
    41 OPDEF(OP_JLE,           4,          "jle",          5,      2,  0)
    42 OPDEF(OP_JG,            5,          "jg",           5,      2,  0)
    43 OPDEF(OP_JGE,           6,          "jge",          5,      2,  0)
    44 OPDEF(OP_JEQ,           7,          "jeq",          5,      2,  0)
    45 OPDEF(OP_JNE,           8,          "jne",          5,      2,  0)
    46 
    47 // value = pop()
    48 // if (value == [true, false])
    49 //    goto target
    50 OPDEF(OP_JT,            9,          "jt",           5,      1,  0)
    51 OPDEF(OP_JF,            10,         "jf",           5,      1,  0)
    52 
    53 // Unconditional jump.
    54 OPDEF(OP_JUMP,          11,         "jump",         5,      0,  0)
    55 
    56 // Gets or sets the value of a local variable onto the stack. The local
    57 // variable is given by an immediate index.
    58 OPDEF(OP_GETLOCAL,      12,         "getlocal",     5,      0,  1)
    59 OPDEF(OP_SETLOCAL,      13,         "setlocal",     5,      1,  1)
    60 
    61 // Stack transitions for these binary operators:
    62 // rhs = pop()
    63 // lhs = pop()
    64 // push(lhs OP rhs)
    65 OPDEF(OP_ADD,           14,         "add",          1,      2,  1)
    66 OPDEF(OP_SUB,           15,         "sub",          1,      2,  1)
    67 OPDEF(OP_MUL,           16,         "mul",          1,      2,  1)
    68 OPDEF(OP_DIV,           17,         "div",          1,      2,  1)
    69 
    70 OPDEF(OP_POP,           18,         "pop",          1,      1,  0)
    71 
    72 // Unconditional terminator placed at the end of code.
    73 OPDEF(OP_STOP,          19,         "stop",         1,      0,  0)
    74 
    75 // Call a function with N arguments. Arguments are pushed in reverse order.
    76 //
    77 // CALLGLOBAL - followed by an extra immediate, containing the function index
    78 // in the code's global.
    79 OPDEF(OP_CALLGLOBAL,    20,         "callglobal",   9,      -1, 1)
    80 
    81 // Similar to GET/LOCAL, except for arguments.
    82 OPDEF(OP_GETARG,        21,         "getarg",       5,      0,  1)
    83 OPDEF(OP_SETARG,        22,         "setarg",       5,      1,  1)
    84 
    85 // Reads or writes an integer element to an unsafe array.
    86 //  index = pop()
    87 //  ref = pop()
    88 //  ref[index]
    89 //
    90 OPDEF(OP_LD_STA_I,      23,         "ld.sta.i",   1,      2,  1)
    91 OPDEF(OP_ST_STA_I,      24,         "st.sta.i",   1,      3,  1)
    92 OPDEF(OP_LD_STA_P,      25,         "ld.sta.p",   1,      2,  1)
    93 
    94 // Allocate an array of N dimensions into unsafe memory. Each dimension size
    95 // is encoded as an immediate. For the dynamic variant, they are popped off
    96 // the stack instead (sp[-1] == dimension 0, etc).
    97 //
    98 // Format:
    99 //  [u8 op] [u8 width] [u8 dims] [imm32 ...]
   100 //
   101 // if (dynamic)
   102 //    for (i = ndims - 1; i >= 0; i--)
   103 //        dims[ndims - i - 1] = pop()
   104 // 
   105 // push(array pref)
   106 //
   107 OPDEF(OP_STACKARRAY,    26,         "stackarray",   -1,     0,  1)
   108 
   109 // Unwinds the stack to its position before a local slot was allocated.
   110 OPDEF(OP_UNWIND,        27,         "unwind",       5,      0,  0)
   111 
   112 // Performs a bounds check on an unsafe array.
   113 //
   114 // index = pop()
   115 // array = pop()
   116 // push(array)
   117 // push(index)
   118 OPDEF(OP_BOUNDS_STA,    28,         "bounds.sta",   1,      2,  2)
   119 
   120 // Pushes a 32-bit IEEE-754 floating point immediate onto the stack.
   121 OPDEF(OP_FLOAT,         29,         "float",        5,      0,  1)
   122 
   123 // Converts an integer to a 32-bit IEEE-754 floating point immediate.
   124 OPDEF(OP_CVT_I2F,       30,         "cvt.i2f",      1,      1,  1)
   125 
   126 // Stack transitions for these binary operators:
   127 // rhs = pop()
   128 // lhs = pop()
   129 // push(lhs OP rhs)
   130 OPDEF(OP_ADD_F,         31,         "add.f",        1,      2,  1)
   131 OPDEF(OP_SUB_F,         32,         "sub.f",        1,      2,  1)
   132 OPDEF(OP_MUL_F,         33,         "mul.f",        1,      2,  1)
   133 OPDEF(OP_DIV_F,         34,         "div.f",        1,      2,  1)
   134