src/Opcodes.h
author Michael Busby <prodigysim@gmail.com>
Mon Jun 10 01:50:18 2013 +0100 (2013-06-10)
changeset 264 70b8f318c379
parent 263 ba85a47ee414
permissions -rw-r--r--
Parse assignment RTL instead of LTR, fixes multiple assignment (bug 5420).
asherkin@263
     1
/* vim: set ts=4 sw=4 tw=99 et:
asherkin@263
     2
 *
asherkin@263
     3
 * Copyright (C) 2012 David Anderson
asherkin@263
     4
 *
asherkin@263
     5
 * This file is part of SourcePawn.
asherkin@263
     6
 *
asherkin@263
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     8
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     9
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
    10
 * any later version.
asherkin@263
    11
 * 
asherkin@263
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    15
 *
asherkin@263
    16
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    18
 */
asherkin@263
    19
#ifndef _include_opcodes_h_
asherkin@263
    20
#define _include_opcodes_h_
asherkin@263
    21
asherkin@263
    22
#include <assert.h>
asherkin@263
    23
#include <stdio.h>
asherkin@263
    24
asherkin@263
    25
namespace ke {
asherkin@263
    26
    
asherkin@263
    27
struct SourcePosition
asherkin@263
    28
{
asherkin@263
    29
    int line;
asherkin@263
    30
    int col;
asherkin@263
    31
asherkin@263
    32
    SourcePosition()
asherkin@263
    33
      : line(-1),
asherkin@263
    34
        col(-1)
asherkin@263
    35
    {
asherkin@263
    36
    }
asherkin@263
    37
asherkin@263
    38
    SourcePosition(unsigned line, unsigned col)
asherkin@263
    39
      : line(line),
asherkin@263
    40
        col(col)
asherkin@263
    41
    {
asherkin@263
    42
    }
asherkin@263
    43
};
asherkin@263
    44
asherkin@263
    45
enum Opcode
asherkin@263
    46
{
asherkin@263
    47
#define OPDEF(name, text, length, uses, defs)   \
asherkin@263
    48
    name,
asherkin@263
    49
#include "Opcodes.tbl"
asherkin@263
    50
#undef OPDEF
asherkin@263
    51
    OPCODES_TOTAL
asherkin@263
    52
};
asherkin@263
    53
asherkin@263
    54
#define OPDEF(name, text, length, uses, defs)   \
asherkin@263
    55
static const unsigned name##_LENGTH = length;
asherkin@263
    56
#include "Opcodes.tbl"
asherkin@263
    57
#undef OPDEF
asherkin@263
    58
asherkin@263
    59
struct OpInfo
asherkin@263
    60
{
asherkin@263
    61
    const char *name;
asherkin@263
    62
    int length;
asherkin@263
    63
    int nuses;
asherkin@263
    64
    int ndefs;
asherkin@263
    65
};
asherkin@263
    66
asherkin@263
    67
typedef unsigned char bytecode;
asherkin@263
    68
asherkin@263
    69
extern OpInfo OpcodeInfo[];
asherkin@263
    70
asherkin@263
    71
static const unsigned MAX_TABLESWITCH_ENTRIES = 256;
asherkin@263
    72
asherkin@263
    73
static inline unsigned
asherkin@263
    74
TableSwitchEntries(int low, int high)
asherkin@263
    75
{
asherkin@263
    76
    return unsigned(high - low) + 1;
asherkin@263
    77
}
asherkin@263
    78
asherkin@263
    79
static inline int
asherkin@263
    80
READ_INT32(bytecode *pc)
asherkin@263
    81
{
asherkin@263
    82
    return *reinterpret_cast<int *>(pc + 1);
asherkin@263
    83
}
asherkin@263
    84
static inline unsigned
asherkin@263
    85
READ_UINT32(bytecode *pc)
asherkin@263
    86
{
asherkin@263
    87
    return *reinterpret_cast<unsigned *>(pc + 1);
asherkin@263
    88
}
asherkin@263
    89
static inline float
asherkin@263
    90
READ_FLOAT(bytecode *pc)
asherkin@263
    91
{
asherkin@263
    92
    return *reinterpret_cast<float *>(pc + 1);
asherkin@263
    93
}
asherkin@263
    94
asherkin@263
    95
static inline int
asherkin@263
    96
READ_TABLESWITCH_LOW(bytecode *pc)
asherkin@263
    97
{
asherkin@263
    98
    return READ_INT32(pc);
asherkin@263
    99
}
asherkin@263
   100
asherkin@263
   101
static inline int
asherkin@263
   102
READ_TABLESWITCH_HIGH(bytecode *pc)
asherkin@263
   103
{
asherkin@263
   104
    return READ_INT32(pc + sizeof(int));
asherkin@263
   105
}
asherkin@263
   106
asherkin@263
   107
class Code;
asherkin@263
   108
asherkin@263
   109
void Disassemble(Code *code, FILE *fp);
asherkin@263
   110
asherkin@263
   111
}
asherkin@263
   112
asherkin@263
   113
#endif // _include_opcodes_h_