jitcraft/Opcodes.h
author David Anderson <dvander@alliedmods.net>
Wed Jun 27 02:21:56 2012 -0700 (2012-06-27)
changeset 4 633176823669
parent 0 c455ebb9a5dc
child 28 a9665857cfd5
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 #ifndef _include_opcodes_h_
    19 #define _include_opcodes_h_
    20 
    21 #include <assert.h>
    22 #include <stdio.h>
    23 
    24 namespace jc {
    25 
    26 enum Opcode
    27 {
    28 #define OPDEF(name, number, text, length, uses, defs)   \
    29     name = number,
    30 #include "Opcodes.tbl"
    31 #undef OPDEF
    32     OPCODES_TOTAL
    33 };
    34 
    35 #define OPDEF(name, number, text, length, uses, defs)   \
    36 static const unsigned name##_LENGTH = length;
    37 #include "Opcodes.tbl"
    38 #undef OPDEF
    39 
    40 struct OpInfo
    41 {
    42     const char *name;
    43     int length;
    44     int nuses;
    45     int ndefs;
    46 };
    47 
    48 typedef unsigned char bytecode;
    49 
    50 extern OpInfo OpcodeInfo[];
    51 
    52 static inline Opcode
    53 Invert(Opcode op)
    54 {
    55     switch (op) {
    56       case OP_JEQ:
    57         return OP_JNE;
    58       case OP_JNE:
    59         return OP_JEQ;
    60       case OP_JG:
    61         return OP_JLE;
    62       case OP_JGE:
    63         return OP_JL;
    64       case OP_JL:
    65         return OP_JGE;
    66       case OP_JLE:
    67         return OP_JG;
    68     }
    69     assert(false);
    70     return OP_NOP;
    71 }
    72 
    73 static inline int
    74 READ_INT32(bytecode *pc)
    75 {
    76     return *reinterpret_cast<int *>(pc + 1);
    77 }
    78 static inline unsigned
    79 READ_UINT32(bytecode *pc)
    80 {
    81     return *reinterpret_cast<unsigned *>(pc + 1);
    82 }
    83 static inline float
    84 READ_FLOAT(bytecode *pc)
    85 {
    86     return *reinterpret_cast<float *>(pc + 1);
    87 }
    88 
    89 class Code;
    90 
    91 void Disassemble(Code *code, FILE *fp);
    92 
    93 }
    94 
    95 #endif // _include_opcodes_h_