jitcraft/Opcodes.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 23 01:19:44 2012 -0700 (2012-07-23)
changeset 85 06a0015b6a5c
parent 59 b7d61ddb3c8d
permissions -rw-r--r--
Added global variable support.
     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 ke {
    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       default:
    67         assert(op == OP_JLE);
    68         return OP_JG;
    69     }
    70 }
    71 
    72 static inline int
    73 READ_INT32(bytecode *pc)
    74 {
    75     return *reinterpret_cast<int *>(pc + 1);
    76 }
    77 static inline unsigned
    78 READ_UINT32(bytecode *pc)
    79 {
    80     return *reinterpret_cast<unsigned *>(pc + 1);
    81 }
    82 static inline float
    83 READ_FLOAT(bytecode *pc)
    84 {
    85     return *reinterpret_cast<float *>(pc + 1);
    86 }
    87 
    88 class Code;
    89 
    90 void Disassemble(Code *code, FILE *fp);
    91 
    92 }
    93 
    94 #endif // _include_opcodes_h_