-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patheval.c
More file actions
424 lines (383 loc) · 11.8 KB
/
eval.c
File metadata and controls
424 lines (383 loc) · 11.8 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
#include <assert.h>
#include "cc.h"
/// constant expression evaluation
#define xxcv(func, ty, l) \
do { \
if (OPKIND(l->op) == CNST) { \
cv##func(ty, l); \
l->op = mkop(CNST, ty); \
l->type = ty; \
return l; \
} \
} while (0)
#define foldcnst1(oper, vf, ty, l) \
do { \
if (OPKIND(l->op) == CNST) { \
l->s.value.vf = oper l->s.value.vf; \
l->op = mkop(CNST, ty); \
l->type = ty; \
return l; \
} \
} while (0)
#define foldcnst1i(oper, vf, ty, l) foldcnst1(oper, vf, ty, l)
#define foldcnst1f(oper, vf, ty, l) foldcnst1(oper, vf, ty, l)
#define foldcnst2(oper, vf, ty, l, r) \
do { \
if (OPKIND(l->op) == CNST && OPKIND(r->op) == CNST) { \
l->s.value.vf = l->s.value.vf oper r->s.value.vf; \
l->op = mkop(CNST, ty); \
l->type = ty; \
return l; \
} \
} while (0)
#define foldcnst2i(oper, vf, ty, l, r) foldcnst2(oper, vf, ty, l, r)
#define foldcnst2f(oper, vf, ty, l, r) foldcnst2(oper, vf, ty, l, r)
#define exchange(l, r) \
do { \
if (OPKIND(l->op) == CNST) { \
struct tree *tmp; tmp = l; l = r; r = tmp; \
} \
} while (0)
#define xfoldcnst2(func, opid, ty, l, r) \
do { \
if ((OPKIND(l->op) == ADD || OPKIND(l->op) == SUB) && \
OPKIND(l->kids[1]->op) == CNST) { \
if (OPKIND(r->op) == CNST) \
return xfold##func(opid, ty, l, r); \
else \
return xswap(opid, ty, l, r); \
} \
} while (0)
#define doxfoldadd(oper, vfi, vff, op, ty, a, b) \
do { \
switch (OPTYPE(op)) { \
case I: \
case U: \
case P: \
a->s.value.vfi = a->s.value.vfi oper b->s.value.vfi; \
break; \
case F: \
a->s.value.vff = a->s.value.vff oper b->s.value.vff; \
break; \
} \
} while (0)
#define foldlogic(opid, oper, ty, l, r) \
do { \
if (OPKIND(l->op) == CNST && OPKIND(r->op) == CNST) { \
int i; \
if (OPTYPE(l->op) == P && OPTYPE(r->op) == P) \
i = l->s.value.p oper r->s.value.p; \
else if (OPTYPE(l->op) == P) \
i = l->s.value.p oper r->s.value.u; \
else if (OPTYPE(r->op) == P) \
i = l->s.value.u oper r->s.value.p; \
else \
i = l->s.value.u oper r->s.value.u; \
return cnsti(i, ty); \
} else if (OPKIND(l->op) == CNST) { \
bool b; \
if (OPTYPE(l->op) == P) \
b = l->s.value.p; \
else \
b = l->s.value.u; \
if (opid == AND && !b) \
return cnsti(0, ty); \
else if (opid == OR && b) \
return cnsti(1, ty); \
} \
} while (0)
static void cvii(struct type *ty, struct tree *l)
{
if (TYPE_SIZE(l->type) > TYPE_SIZE(ty))
// narrow
l->s.value.u &= TYPE_LIMITS(ty).max.u;
}
static void cvif(struct type *ty, struct tree *l)
{
switch (TYPE_KIND(ty)) {
case FLOAT:
l->s.value.d = (float)l->s.value.i;
break;
case DOUBLE:
l->s.value.d = (double)l->s.value.i;
break;
case LONG+DOUBLE:
l->s.value.d = (long double)l->s.value.i;
break;
default:
CC_UNAVAILABLE();
}
}
static void cvuf(struct type *ty, struct tree *l)
{
switch (TYPE_KIND(ty)) {
case FLOAT:
l->s.value.d = (float)l->s.value.u;
break;
case DOUBLE:
l->s.value.d = (double)l->s.value.u;
break;
case LONG+DOUBLE:
l->s.value.d = (long double)l->s.value.u;
break;
default:
CC_UNAVAILABLE();
}
}
static void cvfi(struct type *ty, struct tree *l)
{
switch (TYPE_KIND(l->type)) {
case FLOAT:
l->s.value.u = (float)l->s.value.d;
break;
case DOUBLE:
l->s.value.u = (double)l->s.value.d;
break;
case LONG+DOUBLE:
l->s.value.u = (long double)l->s.value.d;
break;
default:
CC_UNAVAILABLE();
}
}
static void cvff(struct type *ty, struct tree *l)
{
int dkind = TYPE_KIND(ty);
int skind = TYPE_KIND(l->type);
switch (skind) {
case FLOAT:
if (dkind == DOUBLE)
l->s.value.d = (double)l->s.value.d;
else if (dkind == LONG+DOUBLE)
l->s.value.d = (long double)l->s.value.d;
break;
case DOUBLE:
if (dkind == FLOAT)
l->s.value.d = (float)l->s.value.d;
else if (dkind == LONG+DOUBLE)
l->s.value.d = (long double)l->s.value.d;
break;
case LONG+DOUBLE:
if (dkind == FLOAT)
l->s.value.d = (float)l->s.value.d;
else if (dkind == DOUBLE)
l->s.value.d = (double)l->s.value.d;
break;
default:
CC_UNAVAILABLE();
}
}
static void cvpi(struct type *ty, struct tree *l)
{
assert(TYPE_SIZE(ty) == TYPE_SIZE(uptrtype) &&
"Fatal: Non-converted pointer type");
}
static void cvip(struct type *ty, struct tree *l)
{
assert(TYPE_SIZE(l->type) == TYPE_SIZE(uptrtype) &&
"Fatal: Non-converted pointer type");
}
// fold l->kids[1] and r
static struct tree *xfoldadd(int op, struct type *ty,
struct tree *l, struct tree *r)
{
struct tree *r2 = l->kids[1]; // cnst
int kind1 = OPKIND(op);
int kind2 = OPKIND(l->op);
if ((kind1 == ADD && kind2 == ADD) ||
(kind1 == SUB && kind2 == SUB)) {
doxfoldadd(+, u, d, op, ty, r2, r);
l->type = ty;
return l;
} else if ((kind1 == ADD && kind2 == SUB) ||
(kind1 == SUB && kind2 == ADD)) {
doxfoldadd(-, u, d, op, ty, r2, r);
l->type = ty;
return l;
}
CC_UNAVAILABLE();
}
// swap l->kids[1] andr
static struct tree *xswap(int op, struct type *ty,
struct tree *l, struct tree *r)
{
struct tree *r2 = l->kids[1]; // cnst
int op2 = l->op;
l->op = op;
l->kids[1] = r;
return ast_expr(op2, ty, l, r2);
}
// fold constants
struct tree *fold(int op, struct type *ty, struct tree *l, struct tree *r)
{
// return ast_expr(op, ty, l, r);
switch (op) {
// binary
case ADD+I:
case ADD+U:
case ADD+P:
foldcnst2i(+, u, ty, l, r);
exchange(l, r);
xfoldcnst2(add, op, ty, l, r);
break;
case ADD+F:
foldcnst2f(+, d, ty, l, r);
exchange(l, r);
xfoldcnst2(add, op, ty, l, r);
break;
case SUB+I:
case SUB+U:
case SUB+P:
foldcnst2i(-, u, ty, l, r);
exchange(l, r);
xfoldcnst2(add, op, ty, l, r);
break;
case SUB+F:
foldcnst2f(-, d, ty, l, r);
exchange(l, r);
xfoldcnst2(add, op, ty, l, r);
break;
case MUL+I:
foldcnst2i(*, i, ty, l, r);
break;
case MUL+U:
foldcnst2i(*, u, ty, l, r);
break;
case MUL+F:
foldcnst2f(*, d, ty, l, r);
break;
case DIV+I:
foldcnst2i(/, i, ty, l, r);
break;
case DIV+U:
foldcnst2i(/, u, ty, l, r);
break;
case DIV+F:
foldcnst2f(/, d, ty, l, r);
break;
case MOD+I:
foldcnst2i(%, i, ty, l, r);
break;
case MOD+U:
foldcnst2i(%, u, ty, l, r);
break;
case SHL+I:
case SHL+U:
foldcnst2i(<<, u, ty, l, r);
break;
case SHR+I:
case SHR+U:
foldcnst2i(>>, u, ty, l, r);
break;
case BAND+I:
case BAND+U:
foldcnst2i(&, u, ty, l, r);
break;
case BOR+I:
case BOR+U:
foldcnst2i(|, u, ty, l, r);
break;
case XOR+I:
case XOR+U:
foldcnst2i(^, u, ty, l, r);
break;
// rel
case GT+I:
case GT+U:
case GT+P:
foldcnst2i(>, u, ty, l, r);
break;
case GT+F:
foldcnst2f(>, d, ty, l, r);
break;
case GE+I:
case GE+U:
case GE+P:
foldcnst2i(>=, u, ty, l, r);
break;
case GE+F:
foldcnst2f(>=, d, ty, l, r);
break;
case LT+I:
case LT+U:
case LT+P:
foldcnst2i(<, u, ty, l, r);
break;
case LT+F:
foldcnst2f(<, d, ty, l, r);
break;
case LE+I:
case LE+U:
case LE+P:
foldcnst2i(<=, u, ty, l, r);
break;
case LE+F:
foldcnst2f(<=, d, ty, l, r);
break;
// eq
case EQ+I:
case EQ+U:
case EQ+P:
foldcnst2i(==, u, ty, l, r);
break;
case EQ+F:
foldcnst2f(==, d, ty, l, r);
break;
case NE+I:
case NE+U:
case NE+P:
foldcnst2i(!=, u, ty, l, r);
break;
case NE+F:
foldcnst2f(!=, d, ty, l, r);
break;
// logical
case AND:
foldlogic(op, &&, ty, l, r);
break;
case OR:
foldlogic(op, ||, ty, l, r);
break;
// unary
case NEG+I:
case NEG+U:
foldcnst1i(-, u, ty, l);
break;
case NEG+F:
foldcnst1f(-, d, ty, l);
break;
case BNOT+I:
case BNOT+U:
foldcnst1i(~, u, ty, l);
break;
// cast
case CVI+I:
case CVI+U:
case CVU+U:
case CVU+I:
xxcv(ii, ty, l);
break;
case CVI+F:
xxcv(if, ty, l);
break;
case CVU+F:
xxcv(uf, ty, l);
break;
case CVF+I:
case CVF+U:
xxcv(fi, ty, l);
break;
case CVF+F:
xxcv(ff, ty, l);
break;
case CVP+I:
case CVP+U:
xxcv(pi, ty, l);
break;
case CVI+P:
case CVU+P:
xxcv(ip, ty, l);
break;
}
return ast_expr(op, ty, l, r);
}