src/Opcodes.h
author David Anderson <dvander@alliedmods.net>
Sat Jan 12 16:35:26 2013 -0800 (2013-01-12)
changeset 260 05de34f712fe
parent 195 c1ba166f3fc4
child 263 ba85a47ee414
permissions -rw-r--r--
Add support for mutually recursive imports.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn 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  * SourcePawn 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  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_opcodes_h_
    20 #define _include_opcodes_h_
    21 
    22 #include <assert.h>
    23 #include <stdio.h>
    24 
    25 namespace ke {
    26     
    27 struct SourcePosition
    28 {
    29     int line;
    30     int col;
    31 
    32     SourcePosition()
    33       : line(-1),
    34         col(-1)
    35     {
    36     }
    37 
    38     SourcePosition(unsigned line, unsigned col)
    39       : line(line),
    40         col(col)
    41     {
    42     }
    43 };
    44 
    45 enum Opcode
    46 {
    47 #define OPDEF(name, text, length, uses, defs)   \
    48     name,
    49 #include "Opcodes.tbl"
    50 #undef OPDEF
    51     OPCODES_TOTAL
    52 };
    53 
    54 #define OPDEF(name, text, length, uses, defs)   \
    55 static const unsigned name##_LENGTH = length;
    56 #include "Opcodes.tbl"
    57 #undef OPDEF
    58 
    59 struct OpInfo
    60 {
    61     const char *name;
    62     int length;
    63     int nuses;
    64     int ndefs;
    65 };
    66 
    67 typedef unsigned char bytecode;
    68 
    69 extern OpInfo OpcodeInfo[];
    70 
    71 static const unsigned MAX_TABLESWITCH_ENTRIES = 256;
    72 
    73 static inline unsigned
    74 TableSwitchEntries(int low, int high)
    75 {
    76     return unsigned(high - low) + 1;
    77 }
    78 
    79 static inline int
    80 READ_INT32(bytecode *pc)
    81 {
    82     return *reinterpret_cast<int *>(pc + 1);
    83 }
    84 static inline unsigned
    85 READ_UINT32(bytecode *pc)
    86 {
    87     return *reinterpret_cast<unsigned *>(pc + 1);
    88 }
    89 static inline float
    90 READ_FLOAT(bytecode *pc)
    91 {
    92     return *reinterpret_cast<float *>(pc + 1);
    93 }
    94 
    95 static inline int
    96 READ_TABLESWITCH_LOW(bytecode *pc)
    97 {
    98     return READ_INT32(pc);
    99 }
   100 
   101 static inline int
   102 READ_TABLESWITCH_HIGH(bytecode *pc)
   103 {
   104     return READ_INT32(pc + sizeof(int));
   105 }
   106 
   107 class Code;
   108 
   109 void Disassemble(Code *code, FILE *fp);
   110 
   111 }
   112 
   113 #endif // _include_opcodes_h_