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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
#define NSTAK 32
#define SGN 0
#define UNS 1
#define UND 2
#define UNSMARK 0x1000
struct value {
long val;
int type;
};
/* conversion types */
#define RELAT 1
#define ARITH 2
#define LOGIC 3
#define SPCL 4
#define SHIFT 5
#define UNARY 6
/* operator priority, arity, and conversion type, indexed by tokentype */
struct pri {
char pri;
char arity;
char ctype;
} priority[] = {
{ 0, 0, 0 }, /* END */
{ 0, 0, 0 }, /* UNCLASS */
{ 0, 0, 0 }, /* NAME */
{ 0, 0, 0 }, /* NUMBER */
{ 0, 0, 0 }, /* STRING */
{ 0, 0, 0 }, /* CCON */
{ 0, 0, 0 }, /* NL */
{ 0, 0, 0 }, /* WS */
{ 0, 0, 0 }, /* DSHARP */
{ 11, 2, RELAT }, /* EQ */
{ 11, 2, RELAT }, /* NEQ */
{ 12, 2, RELAT }, /* LEQ */
{ 12, 2, RELAT }, /* GEQ */
{ 13, 2, SHIFT }, /* LSH */
{ 13, 2, SHIFT }, /* RSH */
{ 7, 2, LOGIC }, /* LAND */
{ 6, 2, LOGIC }, /* LOR */
{ 0, 0, 0 }, /* PPLUS */
{ 0, 0, 0 }, /* MMINUS */
{ 0, 0, 0 }, /* ARROW */
{ 0, 0, 0 }, /* SBRA */
{ 0, 0, 0 }, /* SKET */
{ 3, 0, 0 }, /* LP */
{ 3, 0, 0 }, /* RP */
{ 0, 0, 0 }, /* DOT */
{ 10, 2, ARITH }, /* AND */
{ 15, 2, ARITH }, /* STAR */
{ 14, 2, ARITH }, /* PLUS */
{ 14, 2, ARITH }, /* MINUS */
{ 16, 1, UNARY }, /* TILDE */
{ 16, 1, UNARY }, /* NOT */
{ 15, 2, ARITH }, /* SLASH */
{ 15, 2, ARITH }, /* PCT */
{ 12, 2, RELAT }, /* LT */
{ 12, 2, RELAT }, /* GT */
{ 9, 2, ARITH }, /* CIRC */
{ 8, 2, ARITH }, /* OR */
{ 5, 2, SPCL }, /* QUEST */
{ 5, 2, SPCL }, /* COLON */
{ 0, 0, 0 }, /* ASGN */
{ 4, 2, 0 }, /* COMMA */
{ 0, 0, 0 }, /* SHARP */
{ 0, 0, 0 }, /* SEMIC */
{ 0, 0, 0 }, /* CBRA */
{ 0, 0, 0 }, /* CKET */
{ 0, 0, 0 }, /* ASPLUS */
{ 0, 0, 0 }, /* ASMINUS */
{ 0, 0, 0 }, /* ASSTAR */
{ 0, 0, 0 }, /* ASSLASH */
{ 0, 0, 0 }, /* ASPCT */
{ 0, 0, 0 }, /* ASCIRC */
{ 0, 0, 0 }, /* ASLSH */
{ 0, 0, 0 }, /* ASRSH */
{ 0, 0, 0 }, /* ASOR */
{ 0, 0, 0 }, /* ASAND */
{ 0, 0, 0 }, /* ELLIPS */
{ 0, 0, 0 }, /* DSHARP1 */
{ 0, 0, 0 }, /* NAME1 */
{ 16, 1, UNARY }, /* DEFINED */
{ 16, 0, UNARY }, /* UMINUS */
};
int evalop(struct pri);
struct value tokval(Token *);
struct value vals[NSTAK], *vp;
enum toktype ops[NSTAK], *op;
/*
* Evaluate an #if #elif #ifdef #ifndef line. trp->tp points to the keyword.
*/
long
eval(Tokenrow *trp, int kw)
{
Token *tp;
Nlist *np;
int ntok, rand;
trp->tp++;
if (kw==KIFDEF || kw==KIFNDEF) {
if (trp->lp - trp->bp != 4 || trp->tp->type!=NAME) {
error(ERROR, "Syntax error in #ifdef/#ifndef");
return 0;
}
np = lookup(trp->tp, 0);
return (kw==KIFDEF) == (np && np->flag&(ISDEFINED|ISMAC));
}
ntok = trp->tp - trp->bp;
kwdefined->val = KDEFINED; /* activate special meaning of defined */
expandrow(trp, "<if>");
kwdefined->val = NAME;
vp = vals;
op = ops;
*op++ = END;
for (rand=0, tp = trp->bp+ntok; tp < trp->lp; tp++) {
switch(tp->type) {
case WS:
case NL:
continue;
/* nilary */
case NAME:
case NAME1:
case NUMBER:
case CCON:
case STRING:
if (rand)
goto syntax;
*vp++ = tokval(tp);
rand = 1;
continue;
/* unary */
case DEFINED:
case TILDE:
case NOT:
if (rand)
goto syntax;
*op++ = tp->type;
continue;
/* unary-binary */
case PLUS: case MINUS: case STAR: case AND:
if (rand==0) {
if (tp->type==MINUS)
*op++ = UMINUS;
if (tp->type==STAR || tp->type==AND) {
error(ERROR, "Illegal operator * or & in #if/#elsif");
return 0;
}
continue;
}
/* flow through */
/* plain binary */
case EQ: case NEQ: case LEQ: case GEQ: case LSH: case RSH:
case LAND: case LOR: case SLASH: case PCT:
case LT: case GT: case CIRC: case OR: case QUEST:
case COLON: case COMMA:
if (rand==0)
goto syntax;
if (evalop(priority[tp->type])!=0)
return 0;
*op++ = tp->type;
rand = 0;
continue;
case LP:
if (rand)
goto syntax;
*op++ = LP;
continue;
case RP:
if (!rand)
goto syntax;
if (evalop(priority[RP])!=0)
return 0;
if (op<=ops || op[-1]!=LP) {
goto syntax;
}
op--;
continue;
default:
error(ERROR,"Bad operator (%t) in #if/#elsif", tp);
return 0;
}
}
if (rand==0)
goto syntax;
if (evalop(priority[END])!=0)
return 0;
if (op!=&ops[1] || vp!=&vals[1]) {
error(ERROR, "Botch in #if/#elsif");
return 0;
}
if (vals[0].type==UND)
error(ERROR, "Undefined expression value");
return vals[0].val;
syntax:
error(ERROR, "Syntax error in #if/#elsif");
return 0;
}
int
evalop(struct pri pri)
{
struct value v1, v2;
long rv1, rv2;
int rtype, oper;
/* prevent compiler whining. */
v1.val = v2.val = 0;
v1.type = v2.type = 0;
rv2=0;
rtype=0;
while (pri.pri < priority[op[-1]].pri) {
oper = *--op;
if (priority[oper].arity==2) {
v2 = *--vp;
rv2 = v2.val;
}
v1 = *--vp;
rv1 = v1.val;
/*lint -e574 -e644 */
switch (priority[oper].ctype) {
case 0:
default:
error(WARNING, "Syntax error in #if/#endif");
return 1;
case ARITH:
case RELAT:
if (v1.type==UNS || v2.type==UNS)
rtype = UNS;
else
rtype = SGN;
if (v1.type==UND || v2.type==UND)
rtype = UND;
if (priority[oper].ctype==RELAT && rtype==UNS) {
oper |= UNSMARK;
rtype = SGN;
}
break;
case SHIFT:
if (v1.type==UND || v2.type==UND)
rtype = UND;
else
rtype = v1.type;
if (rtype==UNS)
oper |= UNSMARK;
break;
case UNARY:
rtype = v1.type;
break;
case LOGIC:
case SPCL:
break;
}
switch (oper) {
case EQ: case EQ|UNSMARK:
rv1 = rv1==rv2; break;
case NEQ: case NEQ|UNSMARK:
rv1 = rv1!=rv2; break;
case LEQ:
rv1 = rv1<=rv2; break;
case GEQ:
rv1 = rv1>=rv2; break;
case LT:
rv1 = rv1<rv2; break;
case GT:
rv1 = rv1>rv2; break;
case LEQ|UNSMARK:
rv1 = (unsigned long)rv1<=rv2; break;
case GEQ|UNSMARK:
rv1 = (unsigned long)rv1>=rv2; break;
case LT|UNSMARK:
rv1 = (unsigned long)rv1<rv2; break;
case GT|UNSMARK:
rv1 = (unsigned long)rv1>rv2; break;
case LSH:
rv1 <<= rv2; break;
case LSH|UNSMARK:
rv1 = (unsigned long)rv1<<rv2; break;
case RSH:
rv1 >>= rv2; break;
case RSH|UNSMARK:
rv1 = (unsigned long)rv1>>rv2; break;
case LAND:
rtype = UND;
if (v1.type==UND)
break;
if (rv1!=0) {
if (v2.type==UND)
break;
rv1 = rv2!=0;
} else
rv1 = 0;
rtype = SGN;
break;
case LOR:
rtype = UND;
if (v1.type==UND)
break;
if (rv1==0) {
if (v2.type==UND)
break;
rv1 = rv2!=0;
} else
rv1 = 1;
rtype = SGN;
break;
case AND:
rv1 &= rv2; break;
case STAR:
rv1 *= rv2; break;
case PLUS:
rv1 += rv2; break;
case MINUS:
rv1 -= rv2; break;
case UMINUS:
if (v1.type==UND)
rtype = UND;
rv1 = -rv1; break;
case OR:
rv1 |= rv2; break;
case CIRC:
rv1 ^= rv2; break;
case TILDE:
rv1 = ~rv1; break;
case NOT:
rv1 = !rv1; if (rtype!=UND) rtype = SGN; break;
case SLASH:
if (rv2==0) {
rtype = UND;
break;
}
if (rtype==UNS)
rv1 /= (unsigned long)rv2;
else
rv1 /= rv2;
break;
case PCT:
if (rv2==0) {
rtype = UND;
break;
}
if (rtype==UNS)
rv1 %= (unsigned long)rv2;
else
rv1 %= rv2;
break;
case COLON:
if (op[-1] != QUEST)
error(ERROR, "Bad ?: in #if/endif");
else {
op--;
if ((--vp)->val==0)
v1 = v2;
rtype = v1.type;
rv1 = v1.val;
}
break;
case DEFINED:
break;
default:
error(ERROR, "Eval botch (unknown operator)");
return 1;
}
/*lint +e574 +e644 */
v1.val = rv1;
v1.type = rtype;
*vp++ = v1;
}
return 0;
}
struct value
tokval(Token *tp)
{
struct value v;
Nlist *np;
int i, base, c;
unsigned long n;
uchar *p;
v.type = SGN;
v.val = 0;
switch (tp->type) {
case NAME:
v.val = 0;
break;
case NAME1:
if ((np = lookup(tp, 0)) != NULL && np->flag&(ISDEFINED|ISMAC))
v.val = 1;
break;
case NUMBER:
n = 0;
base = 10;
p = tp->t;
c = p[tp->len];
p[tp->len] = '\0';
if (*p=='0') {
base = 8;
if (p[1]=='x' || p[1]=='X') {
base = 16;
p++;
}
p++;
}
for (;; p++) {
if ((i = digit(*p)) < 0)
break;
if (i>=base)
error(WARNING,
"Bad digit in number %t", tp);
n *= base;
n += i;
}
if (n>=0x80000000 && base!=10)
v.type = UNS;
for (; *p; p++) {
if (*p=='u' || *p=='U')
v.type = UNS;
else if (*p=='l' || *p=='L')
;
else {
error(ERROR,
"Bad number %t in #if/#elsif", tp);
break;
}
}
v.val = n;
tp->t[tp->len] = c;
break;
case CCON:
n = 0;
p = tp->t;
if (*p=='L') {
p += 1;
error(WARNING, "Wide char constant value undefined");
}
p += 1;
if (*p=='\\') {
p += 1;
if ((i = digit(*p))>=0 && i<=7) {
n = i;
p += 1;
if ((i = digit(*p))>=0 && i<=7) {
p += 1;
n <<= 3;
n += i;
if ((i = digit(*p))>=0 && i<=7) {
p += 1;
n <<= 3;
n += i;
}
}
} else if (*p=='x') {
p += 1;
while ((i = digit(*p))>=0 && i<=15) {
p += 1;
n <<= 4;
n += i;
}
} else {
static char cvcon[]
= "b\bf\fn\nr\rt\tv\v''\"\"??\\\\";
for (i=0; i<sizeof(cvcon); i+=2) {
if (*p == cvcon[i]) {
n = cvcon[i+1];
break;
}
}
p += 1;
if (i>=sizeof(cvcon))
error(WARNING,
"Undefined escape in character constant");
}
} else if (*p=='\'')
error(ERROR, "Empty character constant");
else
n = *p++;
if (*p!='\'')
error(WARNING, "Multibyte character constant undefined");
else if (n>127)
error(WARNING, "Character constant taken as not signed");
v.val = n;
break;
case STRING:
error(ERROR, "String in #if/#elsif");
break;
}
return v;
}
int
digit(int i)
{
if ('0'<=i && i<='9')
i -= '0';
else if ('a'<=i && i<='f')
i -= 'a'-10;
else if ('A'<=i && i<='F')
i -= 'A'-10;
else
i = -1;
return i;
}
|