-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.c
445 lines (424 loc) · 10.1 KB
/
expr.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
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
/*
* generate expression trees
*/
#include "ccc.h"
struct expr *
makeexpr(char op, struct expr *left)
{
struct expr *e;
e = malloc(sizeof(*e));
e->op = op;
e->left = left;
if (left) {
e->cost = left->cost + 1;
} else {
e->cost = 1;
}
return e;
}
/*
* deallocate an expression tree
* this needs to not leak memory
*/
void
destroy_expr(struct expr *e)
{
if (e->left) {
destroy_expr(e->left);
}
if (e->right) {
destroy_expr(e->left);
}
free(e);
}
char
lvalue(struct expr *e)
{
if (e->op == DEREF) {
return 1;
}
return 0;
}
/*
* worker function to factor out common expr stuff for unary ops
*/
void
unop_set(struct expr *e)
{
e->type = e->left->type;
e->cost = e->left->cost;
e->left->up = e;
}
/*
* not sure how this priority stuff is supposed to work.
* XXX - write this up here.
*/
char
binop_pri(char t)
{
char po;
char v;
#ifdef notdef
po = t - MIN_OP;
if ((po < 0) || (t > MAX_OP)) {
return 0;
}
v = pritab[po / 2];
if (po & 1) {
v >>= 4;
}
v &= 0xf;
#endif
return v;
}
#ifdef notdef
#define PP(l,h) ((h << 4) | l)
char pritab[] = {
PP(x,y),
};
#endif
/*
* parse an expression
*/
struct expr *
parse_expr(char pri, struct stmt *st)
{
char op;
struct expr *e;
char p;
switch (cur.type) { // prefix
#ifdef notdef
case SYM:
e = makeexpr(DEREF, makeexpr(VAR, 0));
e->left->var = findvar(strbuf, st);
/* never seen the name and function call */
if ((!(e->left->var)) && (next.type == LPAR)) {
e->left->var = makevar(strdup(strbuf),
maketype(0, TK_FUNC, ptype[PT_INT]));
e->left->var->flags |= V_GLOBAL;
e->left->var->type->flags |= T_INCOMPLETE;
}
unop_set(e);
e->left->type = normalizetype(maketype(0, TK_PTR, e->left->var->type));
break;
#endif
case NUMBER:
e = makeexpr(CONST, 0);
e->type = inttype;
e->v = cur.v.numeric;
gettoken();
break;
#ifdef notdef
case STRING:
e = makeexpr(VAR, 0);
e->var = makevar(makelabel('S', (int)e),
maketype(0, TK_ARRAY, ptype[PTYPE_CHAR]),
V_STATIC|V_CONST|V_GLOBAL);
e->var->type->len = strlen(strbuf);
e->var->type->flags &= ~T_INCOMPLETE;
e->var->init = makeexpr(BYTES,
(struct expr *)strdup(strbuf));
e->var->init->v = e->var->type->len;
e->type = e->var->type;
break;
/* unary */
case LPAR:
gettoken();
t = getbasetype(); // type cast
if (t) {
v = declare(&t);
if (v) {
err(ER_E_CS);
}
e = makeexpr(CAST, parse_expr(pri, st));
unop_set();
e->type = t;
e = cfold(e);
need(RPAR, RPAR, ER_E_CP);
break;
}
e = expr(PRI_PAREN, st);
need(RPAR, RPAR, ER_E_SP);
unop_set();
e = cfold(e);
break;
case SIZEOF:
gettoken();
need(LPAR, LPAR, ER_E_SP);
/* XXX - get a declaration */
need(RPAR, RPAR, ER_E_SP);
break;
case INCR:
case DECR:
op = cur.type;
gettoken();
e = makeexpr(op, parse_expr(pri, st));
if (!lvalue(e->left)) {
err(ER_E_LV);
}
unop_set(e);
break;
case STAR: // deref
gettoken();
e = makeexpr(DEREF, parse_expr(pri, st));
if (e->left->type->kind == TK_PTR) {
unop_set(e);
e->type = e->left->type->sub;
} else {
err(ER_E_DP);
}
break;
case AND: // addrof
gettoken();
e1 = parse_expr(pri, st)
if (!lvalue(e->left)) {
err(ER_E_LV);
}
e = e1->left;
free(e1);
e->left->up = c;
break;
case BANG: // unary not
gettoken();
e = makeexpr(NOT, parse_expr(pri, st));
unop_set();
e->type = ptype[PT_BOOL];
e = cfold(e);
break;
case MINUS:
cur.type = NEG;
/* fall through */
case TWIDDLE:
op = cur.type;
gettoken();
e = makeexpr(op, parse_expr(pri, st));
if (e->left->type->kind != TK_SCALAR) {
err(ER_E_SC);
}
unop_set(e);
e = cfold(e);
break;
#endif
}
/*
* the recursive nature of this expression parser will have exhausted
* the unary operators and terminals by this point. now we have postfix
* and binary operators to deal with
*/
while (1) { // operators
switch (cur.type) {
#ifdef notdef
case INCR:
if (!lvalue(e)) {
err(ER_E_LV);
}
e = makeexpr(POSTINC, e);
unop_set(e);
continue;
case DECR:
if (!lvalue(e)) {
err(ER_E_LV);
}
e = makeexpr(POSTDEC, e);
unop_set(e);
continue;
case LBRACK: // array reference
gettoken();
if (e->type->kind != TK_PTR) {
err(ER_E_IT);
}
e = makeexpr(INDEX, e);
e->right = expr(PRI_INDEX, st);
e->type = e->left->type->sub;
e->left->up = e->right->up = e;
e->cost = e->left->cost + e->right->cost;
need(RBRACK, RBRACK, ER_E_IB);
e = cfold(e);
continue;
case LPAR: // function call
gettoken();
if ((e->type->kind == TK_PTR) &&
(e->left->type->kind == TK_FUNC)) {
e = makeexpr(CALL, e);
e->left->up = e;
e->type = e->left->type->sub;
e->cost = e->left->cost;
while (cur.type != E_O_F) {
e1 = expr(PRI_PAREN, st);
e->up = e;
if (e->next) {
e->next->prev = e1;
}
e1->next = e->next;
e->next = e1;
if (cur.type == COMMA) {
gettoken();
continue;
}
if (cur.type == RPAR) {
break;
}
}
} else {
recover(RPAR, ER_E_NF);
}
need(RPAR, RPAR, ER_E_FA);
continue;
case DOT:
gettoken();
if ((cur.type != SYM) || (e->type->kind != TK_PTR) ||
(!e->type->sub->flags & T_AGGREGATE)) {
err(ER_E_SM);
continue;
}
v = varonlist(strbuf, e->type->sub->elem);
if (!v) {
err(ER_E_NT);
}
e = makeexpr(ADD, e);
e->left->up = e;
e->right = makeexpr(CONST, 0);
e->right->up = e;
e->right->type = ptype[PT_CHAR];
e->type = v->type;
e->right->v = v->offset;
e = cfold(e);
continue;
case DEREF: // ->
gettoken();
if ((cur.type != SYM) || (e->type->kind != TK_PTR) ||
(e->type->sub->kind != TK_PTR) ||
(e->type->sub->sub->flags & T_AGGREGATE)) {
err(ER_E_SM);
continue;
}
v = varonlist(strbuf, e->type->sub->sub->elem);
if (!v) {
err(ER_E_NT);
}
e = makeexpr(ADD, e);
e->left->up = e;
e->right = makeexpr(CONST, 0);
e->right->up = e;
e->right->type = ptype[PT_CHAR];
e->type = v->type;
e = cfold(e);
continue;
#endif
default:
printf("bzzt");
}
p = binop_pri(cur.type);
if (p == 0) {
err(ER_E_UO);
return 0;
}
if (p > pri) {
break;
}
e = makeexpr(cur.type, e);
e->left->up = e;
gettoken();
e->right = parse_expr(p, st);
e->right->up = e;
e->type = opresult(e->op, e->left, e->right);
if (lookupc(symops, e->op) != -1) {
if (e->left->cost < e->right->cost) {
e1 = e->left;
e->left = e->right;
e->right = e1;
}
}
e->cost = e->left->cost + e->right->cost;
e = cfold(e);
}
return e;
}
struct expr *
xreplace(struct expr *out, struct expr *in)
{
in->next = out->next;
in->prev = out->prev;
if (out->prev) {
out->prev->next = in;
}
if (out->next) {
out->next->prev = in;
}
in->up = out->up;
if (out->flags & E_FUNARG) {
in->flags |= E_FUNARG;
}
free(out);
return in;
}
struct expr *
cfold(struct expr *e) {
long val;
long vl, vr;
switch (e->op) {
case NEG:
if (e->left->op == CONST) {
val = -e->v;
e = xreplace(e, e->left);
e->v = val;
}
return e;
case TWIDDLE:
if (e->left->op == CONST) {
val = ~e->v;
e = xreplace(e, e->left);
e->v = val;
}
return e;
case NOT:
if (e->left->op == CONST) {
val = e->v ? 0 : 1;
e = xreplace(e, e->left);
e->v = val;
}
return e;
}
if (!e->right) {
err(ER_E_CF);
return e;
}
if ((e->left->op != CONST) || (e->right->op != CONST)) {
return e;
}
vl = e->left->v;
vr = e->right->v;
switch (e->op) {
case PLUS:
val = vl + vr;
break;
case MINUS:
val = vl - vr;
break;
default:
return e;
}
e = xreplace(e, e->left);
return e;
}
/*
* parse an expression that must yeild a constant.
* used for array declarations and CPP stuff
*/
int
parse_const()
{
struct expr *e;
int val;
e = parse_expr(PRI_ALL, 0);
if (!(e->flags & E_CONST)) {
err (ER_C_CE);
return 0;
}
val = e->v;
destroy_expr(e);
return val;
}
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/