-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.c
286 lines (253 loc) · 7.83 KB
/
execute.c
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
/* This file is part of IRVM.
Copyright (C) 2014 Pablo de Oliveira
IRVM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
IRVM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with IRVM. If not, see <http://www.gnu.org/licenses/>. */
#include <assert.h>
#include "context.h"
#include "execute.h"
#include "error.h"
#include "mem.h"
#define TRACE(...) do {if (trace) warnl(current_loc, __VA_ARGS__);} while(0)
struct location *current_loc;
const char *op2str[] = { "(+)", "(-)", "(*)", "(/)", "(%)" };
const char *rop2str[] = { "(<)", "(<=)", "(>)", "(>=)", "(<>)", "(=)" };
/* applies relative operator ROPR to LEFT and RIGHT operands */
static bool
do_cmp (enum RELOP ropr, int32_t left, int32_t right)
{
switch (ropr)
{
case GT:
return left > right;
case LT:
return left < right;
case GE:
return left >= right;
case LE:
return left <= right;
case EQ:
return left == right;
case NE:
return left != right;
default:
assert (0 && "illegal operator");
}
}
/* applies arithmetic operator OPR to LEFT and RIGHT operands */
static int32_t
do_binop (enum OPER opr, int32_t left, int32_t right)
{
switch (opr)
{
case ADD:
return left + right;
case SUB:
return left - right;
case MUL:
return left * right;
case DIV:
return left / right;
case MOD:
return left % right;
default:
assert (0 && "illegal operator");
}
}
/* runs the IR tree NODE, if TRACE is true each
executed node is printed */
void
execute (struct Node *n, bool trace)
{
NEXT:
if (n == NULL)
errl (current_loc, 1, "program ended, but no label end was found");
/* update current_loc, so error messages are localized */
current_loc = &n->loc;
switch (n->kind)
{
/* Statements */
case T_MOVE_MEM:
{
struct MoveMem *m = container_of (n, struct MoveMem, node);
int32_t value = get_result (m->orig);
TRACE ("move mem [%d] %d", get_result (m->dest), value);
ir_write (get_result (m->dest), value);
n = n->next;
goto NEXT;
}
case T_MOVE_TEMP:
{
struct MoveTemp *m = container_of (n, struct MoveTemp, node);
int32_t value = get_result (m->orig);
TRACE ("move temp %s %d", m->dest, value);
set_temp (m->dest, value);
n = n->next;
goto NEXT;
}
case T_JUMP:
{
struct Jump *j = container_of (n, struct Jump, node);
TRACE ("jump %s", j->name);
n = j->target;
goto NEXT;
}
case T_CJUMP:
{
struct Cjump *c = container_of (n, struct Cjump, node);
int32_t left = get_result (c->left);
int32_t right = get_result (c->right);
if (do_cmp (c->ropr, left, right))
{
n = c->bthen_target;
TRACE ("cjump %d %d %s [%s] %s", left, right,
rop2str[c->ropr], c->bthen, c->belse);
}
else
{
n = c->belse_target;
TRACE ("cjump %d %d %s %s [%s]", left, right,
rop2str[c->ropr], c->bthen, c->belse);
}
goto NEXT;
}
/* Expressions */
case T_CONST:
{
struct Const *c = container_of (n, struct Const, node);
set_result (n, c->value);
TRACE ("const %d", c->value);
n = n->next;
goto NEXT;
}
case T_TEMP:
{
struct Temp *t = container_of (n, struct Temp, node);
set_result (n, get_temp (t->name));
TRACE ("temp %s = %d", t->name, get_result (n));
n = n->next;
goto NEXT;
}
case T_BINOP:
{
struct Binop *b = container_of (n, struct Binop, node);
int32_t left = get_result (b->left);
int32_t right = get_result (b->right);
set_result (n, do_binop (b->opr, left, right));
TRACE ("binop %s %d %d = %d", op2str[b->opr], left,
right, get_result (n));
n = n->next;
goto NEXT;
}
case T_ESEQ:
{
struct Eseq *e = container_of (n, struct Eseq, node);
set_result (n, get_result (e->exp));
TRACE ("eseq = %d", get_result (n));
n = n->next;
goto NEXT;
}
case T_MEM:
{
struct Mem *m = container_of (n, struct Mem, node);
set_result (n, ir_read (get_result (m->exp)));
TRACE ("mem [%d] = %d", get_result (m->exp), get_result (n));
n = n->next;
goto NEXT;
}
case T_CALL:
{
struct Call *c = container_of (n, struct Call, node);
struct Node *arg = NULL;
TRACE ("call %s", c->name);
/* first, read all arguments into an array */
int count = 0;
int32_t args[MAX_ARGS];
list_for_each (c->arguments, arg, list)
{
if (count >= MAX_ARGS)
errl (current_loc, 1, "too many arguments");
args[count] = get_result (arg);
count++;
}
/* then, push a new context */
push_context (n);
int i;
for (i = 0; i < count; i++)
{
/* and set i0..in temporaries */
set_temp (argid[i], args[i]);
TRACE (" %s = %d", argid[i], args[i]);
}
if (c->primitive)
{
/* a primitive is directly executed */
int32_t result = c->primitive ();
TRACE ("call end %s = %d", c->name, result);
pop_context ();
set_result (n, result);
n = n->next;
}
else
{
/* a user defined function requires a jump */
n = c->target;
}
goto NEXT;
}
case T_LABEL:
{
struct Label *l = container_of (n, struct Label, node);
/* label end is special: it marks the end of a function */
if (l->name == end)
{
/* result is in the rv temporary */
int32_t result = get_temp (rv);
/* pop the context */
struct Node *caller = pop_context ();
/* NULL is used as a sentinel in the context stack,
it marks the end of the program */
if (caller == NULL)
{
TRACE ("end of program");
return;
}
current_loc = &caller->loc;
struct Call *c = container_of (caller, struct Call, node);
TRACE ("call end %s = %d", c->name, result);
/* set result of function into the caller node */
set_result (caller, result);
/* return to the caller */
n = caller->next;
}
else
{
/* other labels can be safely skipped */
TRACE ("label %s", l->name);
n = n->next;
}
goto NEXT;
}
case T_NAME:
{
struct Name *a = container_of (n, struct Name, node);
int32_t result = a->lit;
set_result (n, result);
TRACE ("name %s = (%d) %s\n", a->name, result,
read_string (result));
n = n->next;
goto NEXT;
}
default:
n = n->next;
goto NEXT;
}
assert (0);
}