1 #ifndef INNOCENT_VARIABLES_H
2 #define INNOCENT_VARIABLES_H
6 #include "common/endian.h"
9 #include "innocent/debug.h"
29 class Value
: public NumericInspectable
<uint16
> {
32 virtual ValueType
type() const { return kValueVoid
; }
34 virtual operator uint16() const { assert(false); }
35 virtual int16
signd() const { const uint16 v
= *this; return *reinterpret_cast<const int16
*>(&v
); }
36 virtual Value
&operator=(uint16 value
) { assert(false); }
37 virtual Value
&operator=(const Value
&) { assert(false); }
38 virtual bool operator==(const Value
&other
) { return uint16(*this) == other
; }
39 virtual bool operator<(const Value
&other
) { return uint16(*this) < other
; }
40 virtual bool operator>(const Value
&other
) { return other
< *this; }
41 virtual Value
&operator++() { return *this = uint16(*this) + 1; }
42 virtual uint16
operator++(int) { uint16 old
= *this; ++*this; return old
; }
43 virtual Value
&operator--() { return *this = uint16(*this) - 1; }
44 virtual uint16
operator--(int) { uint16 old
= *this; --*this; return old
; }
46 T
operator-(T she
) { T me
= *this; return me
- she
; }
48 virtual bool holdsCode() const { assert(false); }
50 virtual operator byte
*() { assert(false); }
55 explicit Value(Value
&) : NumericInspectable
<uint16
>() { assert(false); } // no copying
58 class Constant
: public Value
{
60 Constant(uint16 value
) : _value(value
) {}
61 virtual ~Constant() {}
63 virtual operator uint16() const { return _value
; }
64 virtual ValueType
type() const { return kValueConstant
; }
69 class ByteVariable
: public Value
{
71 ByteVariable(byte
*ptr
) : _ptr(ptr
) {}
72 virtual Value
&operator=(uint16 value
) { assert(value
< 256); *_ptr
= value
; return *this; }
73 virtual operator uint16() const { return *_ptr
; }
78 class WordVariable
: public Value
{
80 WordVariable(byte
*ptr
) : _ptr(ptr
) {}
81 virtual operator uint16() const { return READ_LE_UINT16(_ptr
); }
82 virtual Value
&operator=(uint16 value
);
83 virtual Value
&operator=(const Value
&other
) { return *this = uint16(other
); }
91 for (std::vector
<Value
*>::iterator it
= _values
.begin(); it
!= _values
.end(); ++it
)
92 /*delete *it*/; // FIXME segfaults, why?
94 void push_back(Value
*element
) { _values
.push_back(element
); }
95 Value
&operator[](uint8 idx
) { return *_values
[idx
]; }
97 std::vector
<Value
*> _values
;
100 class CodePointer
: public Value
{
102 CodePointer() : _interpreter(0) {}
103 CodePointer(const CodePointer
&c
) : Value(), _offset(c
._offset
), _interpreter(c
._interpreter
) { init(); }
104 CodePointer(uint16 offset
, Interpreter
*interpreter
);
106 CodePointer
&operator=(const CodePointer
&cp
) { _offset
= cp
._offset
; _interpreter
= cp
._interpreter
; return *this; }
108 virtual const char *operator+() const { return _inspect
; }
109 virtual void run() const;
110 virtual void run(OpcodeMode mode
) const;
111 uint16
offset() const { return _offset
; }
113 virtual operator uint16() const { return _offset
; }
114 virtual bool holdsCode() const { return true; }
115 virtual byte
*code() const;
116 Interpreter
*interpreter() const { return _interpreter
; }
117 bool isEmpty() const { return _interpreter
== 0; }
118 void reset() { _interpreter
= 0; }
120 template<typename T
> T
&field(T
&, int) const;
126 Interpreter
*_interpreter
;
129 template<typename Enum
, int N
>
131 static const char *name() {
136 static const char *findName(Enum a
){
140 return EnumName
<Enum
, N
-1>::findName(a
);
144 template<typename Enum
>
145 struct EnumName
<Enum
, -1> {
146 static const char *findName(Enum a
) {
153 template<typename Enum
>
154 class EnumPack
: public Inspectable
{
157 EnumPack(Enum a
) : _a(a
) { }
158 const char *operator+() const { return EnumName
<Enum
,40>::findName(Enum(_a
)); }
159 operator Enum() const { return _a
; }
164 #define ENAME(en, v, s) template<> const char *EnumName<en, v>::name() { return s; } enum {}