-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmips32_tree.h
More file actions
471 lines (380 loc) · 10.6 KB
/
mips32_tree.h
File metadata and controls
471 lines (380 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#ifndef MIPS32_TREE_H
#define MIPS32_TREE_H
#include <stdint.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include <sstream>
#include <bitset>
#include "mips32_sim.h"
#include "util.h"
#include "mempool.h"
using namespace std;
#define UNUSED(x) ((void)(x))
void reportRuntimeError(const char *format, ...);
/* Mips32 Size Directives */
#define MSD_None 0
#define MSD_Byte 8
#define MSD_HWord 16
#define MSD_Word 32
/* Argument kinds */
#define MARGUMENT 100
#define MARG_REGISTER 101
#define MARG_IMMEDIATE 102
#define MARG_IDENTIFIER 103
#define MARG_MEMREF 104
#define MARG_HIGH_HWORD 105
#define MARG_LOW_HWORD 106
#define MARG_PHY_ADDR 107
#define MARG_EXTFUNC_NAME 108
/* Instruction node kinds */
#define MCMD_Show 200
#define MCMD_Set 201
#define MCMD_Exec 202
#define MCMD_Stop 203
#define MINST_1ARG 300
#define MINST_2ARG 301
#define MINST_3ARG 302
#define MINST_TAGGED 303
/* Address Expression kinds */
#define MADDR_EXPR 400
#define MADDR_EXPR_REG 401
#define MADDR_EXPR_CONST 402
#define MADDR_EXPR_IDENT 403
#define MADDR_EXPR_BASE_OFFSET 404
class MIPS32Sim;
class MReference;
class MArgument;
enum NumberBaseFormat {
NBF_SignedDecimal,
NBF_UnsignedDecimal,
NBF_Hexadecimal,
NBF_Octal,
NBF_Binary,
NBF_Ascii
};
typedef list<MArgument *> MArgumentList;
/* AST node definitions start here. */
class MNode {
public:
static void *operator new(std::size_t sz);
static void operator delete(void* ptrb);
virtual string toString() = 0;
virtual int getKind() = 0;
bool isA(int kind) { return (getKind() == kind); }
public:
int line; //Source line
};
typedef list<MNode *> MNodeList;
/* Address expressions */
class MAddrExpr: public MNode {
protected:
MAddrExpr() {}
public:
virtual bool eval(MIPS32Sim *sim, uint32_t &value) = 0;
virtual string toString() = 0;
};
class MAddrExprConstant: public MAddrExpr {
public:
MAddrExprConstant(MArgument *argValue) {
this->argValue = argValue;
}
int getKind() { return MADDR_EXPR_CONST; }
bool eval(MIPS32Sim *sim, uint32_t &value);
string toString();
MArgument *argValue;
};
class MAddrExprIdentifier: public MAddrExpr {
public:
MAddrExprIdentifier(string ident) {
this->ident = ident;
}
int getKind() { return MADDR_EXPR_IDENT; }
bool eval(MIPS32Sim *sim, uint32_t &value);
string toString();
string ident;
};
class MAddrExprRegister: public MAddrExpr {
public:
MAddrExprRegister(uint32_t value) {
this->regIndex = value;
}
int getKind() { return MADDR_EXPR_REG; }
bool eval(MIPS32Sim *sim, uint32_t &value);
string toString();
uint32_t regIndex;
};
class MAddrExprBaseOffset: public MAddrExpr {
public:
MAddrExprBaseOffset(MArgument *argOffset, uint32_t baseRegIndex) {
this->argOffset = argOffset;
this->baseRegIndex = baseRegIndex;
}
int getKind() { return MADDR_EXPR_BASE_OFFSET; }
bool eval(MIPS32Sim *sim, uint32_t &value);
string toString();
MArgument *argOffset;
uint32_t baseRegIndex;
};
/* Argument nodes base class */
class MArgument: public MNode {
protected:
MArgument() {}
public:
virtual bool getReference(MIPS32Sim *sim, MReference &ref) = 0;
virtual string toString() = 0;
};
/* Node defitions for arguments */
class MArgRegister: public MArgument
{
public:
MArgRegister(string regName, unsigned regIndex) {
this->regIndex = regIndex;
this->regName = regName;
}
int getKind() { return MARG_REGISTER; }
string toString() { return regName; }
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
unsigned regIndex;
string regName;
};
class MArgMemRef: public MArgument
{
public:
MArgMemRef(int sizeDirective, MArgument *arg1, MArgument *arg2, MArgument *argCount) {
this->sizeDirective = sizeDirective;
this->arg1 = arg1;
this->arg2 = arg2;
this->argCount = argCount;
}
int getKind() { return MARG_MEMREF; }
string toString() { return "MemRef"; }
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
int sizeDirective;
MArgument *arg1;
MArgument *arg2;
MArgument *argCount;
};
class MArgIdentifier: public MArgument
{
public:
MArgIdentifier(string name) {
this->name = name;
}
int getKind() { return MARG_IDENTIFIER; }
string toString() { return name; }
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
string name;
};
class MArgConstant: public MArgument
{
public:
MArgConstant(NumberBaseFormat numberBase, uint32_t value) {
this->numberBase = numberBase;
this->value = value;
}
MArgConstant(uint32_t value) {
this->numberBase = NBF_UnsignedDecimal;
this->value = value;
}
int getKind() { return MARG_IMMEDIATE; }
string toString() {
stringstream ss;
switch (numberBase) {
case NBF_Hexadecimal: ss << "0x" << hex << value << dec; break;
case NBF_SignedDecimal: ss << ((int32_t)value); break;
case NBF_UnsignedDecimal: ss << value; break;
case NBF_Binary: {
ss << "0b";
for (uint32_t mask = (1 << 31); mask > 0; mask >>= 1) {
ss << (((value & mask) == 0) ? '0' : '1');
}
break;
}
case NBF_Octal: ss << value; break;
case NBF_Ascii: ss << (char)value;
default:
ss << value;
}
return ss.str();
}
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
NumberBaseFormat numberBase;
uint32_t value;
};
class MArgHighHalfWord: public MArgument
{
public:
MArgHighHalfWord(MArgument *arg) {
this->arg = arg;
}
int getKind() { return MARG_HIGH_HWORD; }
string toString() { return "#hihw(" + arg->toString() + ")"; }
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
MArgument *arg;
};
class MArgLowHalfWord: public MArgument
{
public:
MArgLowHalfWord(MArgument *arg) {
this->arg = arg;
}
int getKind() { return MARG_LOW_HWORD; }
string toString() { return "#lohw(" + arg->toString() + ")"; }
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
MArgument *arg;
};
class MArgPhyAddress: public MArgument
{
public:
MArgPhyAddress(MAddrExpr *expr) {
this->expr = expr;
}
int getKind() { return MARG_PHY_ADDR; }
string toString() { return "#paddr(" + expr->toString() + ")"; }
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
MAddrExpr *expr;
};
class MArgExternalFuntionId: public MArgument
{
public:
MArgExternalFuntionId(string name1, string name2) {
this->name1 = name1;
this->name2 = name2;
}
int getKind() { return MARG_EXTFUNC_NAME; }
string toString() { return "@" + name1 + "." + name2; }
bool getReference(MIPS32Sim *sim, MReference &ref);
public:
string name1;
string name2;
};
/* Node definition for x86 instructions and simulator commands */
class MInstruction: public MNode {
protected:
MInstruction() {}
public:
virtual int getArgumentCount() = 0;
virtual bool resolveArguments(MIPS32Sim *sim, uint32_t values[]) { return false; }
string name;
};
class MCmd_Show: public MInstruction {
public:
MCmd_Show(string name, MArgument *arg, PrintFormat dataFormat): MInstruction() {
this->name = name;
this->arg = arg;
this->dataFormat = dataFormat;
}
string toString() { return "show"; }
int getKind() { return MCMD_Show; }
int getArgumentCount() { return 2; }
bool exec(MIPS32Sim *sim);
public:
MArgument *arg;
PrintFormat dataFormat;
};
class MCmd_Set: public MInstruction {
public:
MCmd_Set(string name, MArgument *arg, MArgumentList &lvalue): MInstruction() {
this->name = name;
this->arg = arg;
this->lvalue = lvalue;
}
string toString() { return "set"; }
int getKind() { return MCMD_Set; }
int getArgumentCount() { return 2; }
bool exec(MIPS32Sim *sim);
public:
MArgument *arg;
MArgumentList lvalue;
};
class MCmd_Exec: public MInstruction {
public:
MCmd_Exec(string name, string file_path): MInstruction() {
this->name = name;
this->file_path = file_path;
}
string toString() { return "#exec"; }
int getKind() { return MCMD_Exec; }
int getArgumentCount() { return 1; }
bool exec(MIPS32Sim *sim);
public:
string file_path;
};
class MCmd_Stop: public MInstruction {
public:
MCmd_Stop() {}
string toString() { return "#stop"; }
int getKind() { return MCMD_Stop; }
int getArgumentCount() { return 0; }
bool exec(MIPS32Sim *sim) {}
};
class MInstTagged: public MInstruction {
public:
MInstTagged(string tag, MInstruction *inst): MInstruction() {
this->name = "nop";
this->tag = tag;
this->inst = inst;
}
string toString() { return tag + string(": ") + ((inst != NULL)? inst->toString() : string("")); }
int getKind() { return MINST_TAGGED; }
int getArgumentCount() { return 0; }
public:
string tag;
MInstruction *inst;
};
class MInst_1Arg: public MInstruction {
public:
MInst_1Arg(string name, MArgument *arg): MInstruction() {
this->name = name;
this->arg1 = arg;
}
string toString() { return name; }
int getKind() { return MINST_1ARG; }
int getArgumentCount() { return 1; }
bool resolveArguments(MIPS32Sim *sim, uint32_t values[]);
public:
MArgument *arg1;
};
class MInst_2Arg: public MInstruction {
public:
MInst_2Arg(string name, MArgument *arg1, MArgument *arg2): MInstruction() {
this->name = name;
this->arg1 = arg1;
this->arg2 = arg2;
}
string toString() { return name; }
int getKind() { return MINST_2ARG; }
int getArgumentCount() { return 2; }
bool resolveArguments(MIPS32Sim *sim, uint32_t values[]);
public:
MArgument *arg1;
MArgument *arg2;
};
class MInst_3Arg: public MInstruction {
public:
MInst_3Arg(string name, MArgument *arg1, MArgument *arg2, MArgument *arg3): MInstruction() {
this->name = name;
this->arg1 = arg1;
this->arg2 = arg2;
this->arg3 = arg3;
}
string toString() { return name; }
int getKind() { return MINST_3ARG; }
int getArgumentCount() { return 3; }
bool resolveArguments(MIPS32Sim *sim, uint32_t values[]);
public:
MArgument *arg1;
MArgument *arg2;
MArgument *arg3;
};
#endif // MIPS32_TREE_H