Print this page
11972 resync smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/expand.c
+++ new/usr/src/tools/smatch/src/expand.c
1 1 /*
2 2 * sparse/expand.c
3 3 *
4 4 * Copyright (C) 2003 Transmeta Corp.
5 5 * 2003-2004 Linus Torvalds
6 6 *
7 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 8 * of this software and associated documentation files (the "Software"), to deal
9 9 * in the Software without restriction, including without limitation the rights
10 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 11 * copies of the Software, and to permit persons to whom the Software is
12 12 * furnished to do so, subject to the following conditions:
13 13 *
14 14 * The above copyright notice and this permission notice shall be included in
15 15 * all copies or substantial portions of the Software.
16 16 *
17 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 23 * THE SOFTWARE.
24 24 *
25 25 * expand constant expressions.
26 26 */
27 27 #include <stdlib.h>
28 28 #include <stdarg.h>
29 29 #include <stddef.h>
30 30 #include <stdio.h>
31 31 #include <string.h>
32 32 #include <ctype.h>
33 33 #include <unistd.h>
↓ open down ↓ |
33 lines elided |
↑ open up ↑ |
34 34 #include <fcntl.h>
35 35 #include <limits.h>
36 36
37 37 #include "lib.h"
38 38 #include "allocate.h"
39 39 #include "parse.h"
40 40 #include "token.h"
41 41 #include "symbol.h"
42 42 #include "target.h"
43 43 #include "expression.h"
44 +#include "evaluate.h"
44 45 #include "expand.h"
45 46
46 47
47 48 static int expand_expression(struct expression *);
48 49 static int expand_statement(struct statement *);
50 +
51 +// If set, don't issue a warning on divide-by-0, invalid shift, ...
52 +// and don't mark the expression as erroneous but leave it as-is.
53 +// This allows testing some characteristics of the expression
54 +// without creating any side-effects (e.g.: is_zero_constant()).
49 55 static int conservative;
50 56
51 57 static int expand_symbol_expression(struct expression *expr)
52 58 {
53 59 struct symbol *sym = expr->symbol;
54 60
55 61 if (sym == &zero_int) {
56 62 if (Wundef)
57 63 warning(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
58 64 expr->type = EXPR_VALUE;
59 65 expr->value = 0;
60 66 expr->taint = 0;
61 67 return 0;
62 68 }
63 69 /* The cost of a symbol expression is lower for on-stack symbols */
64 70 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
65 71 }
66 72
67 73 static long long get_longlong(struct expression *expr)
68 74 {
69 75 int no_expand = expr->ctype->ctype.modifiers & MOD_UNSIGNED;
70 76 long long mask = 1ULL << (expr->ctype->bit_size - 1);
71 77 long long value = expr->value;
72 78 long long ormask, andmask;
73 79
74 80 if (!(value & mask))
75 81 no_expand = 1;
76 82 andmask = mask | (mask-1);
77 83 ormask = ~andmask;
78 84 if (no_expand)
79 85 ormask = 0;
80 86 return (value & andmask) | ormask;
81 87 }
82 88
83 89 void cast_value(struct expression *expr, struct symbol *newtype,
84 90 struct expression *old, struct symbol *oldtype)
85 91 {
86 92 int old_size = oldtype->bit_size;
87 93 int new_size = newtype->bit_size;
88 94 long long value, mask, signmask;
89 95 long long oldmask, oldsignmask, dropped;
90 96
91 97 if (is_float_type(newtype) || is_float_type(oldtype))
92 98 goto Float;
93 99
94 100 // For pointers and integers, we can just move the value around
95 101 expr->type = EXPR_VALUE;
96 102 expr->taint = old->taint;
97 103 if (old_size == new_size) {
98 104 expr->value = old->value;
99 105 return;
100 106 }
101 107
102 108 // expand it to the full "long long" value
103 109 value = get_longlong(old);
104 110
105 111 Int:
106 112 // _Bool requires a zero test rather than truncation.
107 113 if (is_bool_type(newtype)) {
108 114 expr->value = !!value;
109 115 if (!conservative && value != 0 && value != 1)
110 116 warning(old->pos, "odd constant _Bool cast (%llx becomes 1)", value);
111 117 return;
112 118 }
113 119
114 120 // Truncate it to the new size
115 121 signmask = 1ULL << (new_size-1);
116 122 mask = signmask | (signmask-1);
117 123 expr->value = value & mask;
118 124
119 125 // Stop here unless checking for truncation
120 126 if (!Wcast_truncate || conservative)
121 127 return;
122 128
123 129 // Check if we dropped any bits..
124 130 oldsignmask = 1ULL << (old_size-1);
125 131 oldmask = oldsignmask | (oldsignmask-1);
126 132 dropped = oldmask & ~mask;
127 133
128 134 // OK if the bits were (and still are) purely sign bits
129 135 if (value & dropped) {
130 136 if (!(value & oldsignmask) || !(value & signmask) || (value & dropped) != dropped)
131 137 warning(old->pos, "cast truncates bits from constant value (%llx becomes %llx)",
132 138 value & oldmask,
133 139 value & mask);
134 140 }
135 141 return;
136 142
137 143 Float:
138 144 if (!is_float_type(newtype)) {
139 145 value = (long long)old->fvalue;
140 146 expr->type = EXPR_VALUE;
141 147 expr->taint = 0;
142 148 goto Int;
143 149 }
144 150
145 151 if (!is_float_type(oldtype))
146 152 expr->fvalue = (long double)get_longlong(old);
147 153 else
148 154 expr->fvalue = old->fvalue;
149 155
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
150 156 if (!(newtype->ctype.modifiers & MOD_LONGLONG) && \
151 157 !(newtype->ctype.modifiers & MOD_LONGLONGLONG)) {
152 158 if ((newtype->ctype.modifiers & MOD_LONG))
153 159 expr->fvalue = (double)expr->fvalue;
154 160 else
155 161 expr->fvalue = (float)expr->fvalue;
156 162 }
157 163 expr->type = EXPR_FVALUE;
158 164 }
159 165
160 -static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
166 +static void warn_shift_count(struct expression *expr, struct symbol *ctype, long long count)
161 167 {
162 - warning(expr->pos, "shift too big (%u) for type %s", count, show_typename(ctype));
163 - count &= ctype->bit_size-1;
164 - return count;
168 + if (count < 0) {
169 + if (!Wshift_count_negative)
170 + return;
171 + warning(expr->pos, "shift count is negative (%lld)", count);
172 + return;
173 + }
174 + if (ctype->type == SYM_NODE)
175 + ctype = ctype->ctype.base_type;
176 +
177 + if (!Wshift_count_overflow)
178 + return;
179 + warning(expr->pos, "shift too big (%llu) for type %s", count, show_typename(ctype));
165 180 }
166 181
182 +/* Return true if constant shift size is valid */
183 +static bool check_shift_count(struct expression *expr, struct expression *right)
184 +{
185 + struct symbol *ctype = expr->ctype;
186 + long long count = get_longlong(right);
187 +
188 + if (count >= 0 && count < ctype->bit_size)
189 + return true;
190 + if (!conservative)
191 + warn_shift_count(expr, ctype, count);
192 + return false;
193 +}
194 +
167 195 /*
168 196 * CAREFUL! We need to get the size and sign of the
169 197 * result right!
170 198 */
171 199 #define CONVERT(op,s) (((op)<<1)+(s))
172 200 #define SIGNED(op) CONVERT(op, 1)
173 201 #define UNSIGNED(op) CONVERT(op, 0)
174 202 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
175 203 {
176 204 struct expression *left = expr->left, *right = expr->right;
177 205 unsigned long long v, l, r, mask;
178 206 signed long long sl, sr;
179 207 int is_signed;
180 208
181 209 if (right->type != EXPR_VALUE)
182 210 return 0;
183 211 r = right->value;
184 212 if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) {
185 - if (r >= ctype->bit_size) {
186 - if (conservative)
187 - return 0;
188 - r = check_shift_count(expr, ctype, r);
189 - right->value = r;
190 - }
213 + if (!check_shift_count(expr, right))
214 + return 0;
191 215 }
192 216 if (left->type != EXPR_VALUE)
193 217 return 0;
194 218 l = left->value; r = right->value;
195 219 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
196 220 mask = 1ULL << (ctype->bit_size-1);
197 221 sl = l; sr = r;
198 222 if (is_signed && (sl & mask))
199 223 sl |= ~(mask-1);
200 224 if (is_signed && (sr & mask))
201 225 sr |= ~(mask-1);
202 226
203 227 switch (CONVERT(expr->op,is_signed)) {
204 228 case SIGNED('+'):
205 229 case UNSIGNED('+'):
206 230 v = l + r;
207 231 break;
208 232
209 233 case SIGNED('-'):
210 234 case UNSIGNED('-'):
211 235 v = l - r;
212 236 break;
213 237
214 238 case SIGNED('&'):
215 239 case UNSIGNED('&'):
216 240 v = l & r;
217 241 break;
218 242
219 243 case SIGNED('|'):
220 244 case UNSIGNED('|'):
221 245 v = l | r;
222 246 break;
223 247
224 248 case SIGNED('^'):
225 249 case UNSIGNED('^'):
226 250 v = l ^ r;
227 251 break;
228 252
229 253 case SIGNED('*'):
230 254 v = sl * sr;
231 255 break;
232 256
233 257 case UNSIGNED('*'):
234 258 v = l * r;
235 259 break;
236 260
237 261 case SIGNED('/'):
238 262 if (!r)
239 263 goto Div;
240 264 if (l == mask && sr == -1)
241 265 goto Overflow;
242 266 v = sl / sr;
243 267 break;
244 268
245 269 case UNSIGNED('/'):
246 270 if (!r) goto Div;
247 271 v = l / r;
248 272 break;
249 273
250 274 case SIGNED('%'):
251 275 if (!r)
252 276 goto Div;
253 277 if (l == mask && sr == -1)
254 278 goto Overflow;
255 279 v = sl % sr;
256 280 break;
257 281
258 282 case UNSIGNED('%'):
259 283 if (!r) goto Div;
260 284 v = l % r;
261 285 break;
262 286
263 287 case SIGNED(SPECIAL_LEFTSHIFT):
264 288 case UNSIGNED(SPECIAL_LEFTSHIFT):
265 289 v = l << r;
266 290 break;
267 291
268 292 case SIGNED(SPECIAL_RIGHTSHIFT):
269 293 v = sl >> r;
270 294 break;
271 295
272 296 case UNSIGNED(SPECIAL_RIGHTSHIFT):
273 297 v = l >> r;
274 298 break;
275 299
276 300 default:
277 301 return 0;
278 302 }
279 303 mask = mask | (mask-1);
280 304 expr->value = v & mask;
281 305 expr->type = EXPR_VALUE;
282 306 expr->taint = left->taint | right->taint;
283 307 return 1;
284 308 Div:
285 309 if (!conservative)
286 310 warning(expr->pos, "division by zero");
287 311 return 0;
288 312 Overflow:
289 313 if (!conservative)
290 314 warning(expr->pos, "constant integer operation overflow");
291 315 return 0;
292 316 }
293 317
294 318 static int simplify_cmp_binop(struct expression *expr, struct symbol *ctype)
295 319 {
296 320 struct expression *left = expr->left, *right = expr->right;
297 321 unsigned long long l, r, mask;
298 322 signed long long sl, sr;
299 323
300 324 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
301 325 return 0;
302 326 l = left->value; r = right->value;
303 327 mask = 1ULL << (ctype->bit_size-1);
304 328 sl = l; sr = r;
305 329 if (sl & mask)
306 330 sl |= ~(mask-1);
307 331 if (sr & mask)
308 332 sr |= ~(mask-1);
309 333 switch (expr->op) {
310 334 case '<': expr->value = sl < sr; break;
311 335 case '>': expr->value = sl > sr; break;
312 336 case SPECIAL_LTE: expr->value = sl <= sr; break;
313 337 case SPECIAL_GTE: expr->value = sl >= sr; break;
314 338 case SPECIAL_EQUAL: expr->value = l == r; break;
315 339 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
316 340 case SPECIAL_UNSIGNED_LT:expr->value = l < r; break;
317 341 case SPECIAL_UNSIGNED_GT:expr->value = l > r; break;
318 342 case SPECIAL_UNSIGNED_LTE:expr->value = l <= r; break;
319 343 case SPECIAL_UNSIGNED_GTE:expr->value = l >= r; break;
320 344 }
321 345 expr->type = EXPR_VALUE;
322 346 expr->taint = left->taint | right->taint;
323 347 return 1;
324 348 }
325 349
326 350 static int simplify_float_binop(struct expression *expr)
327 351 {
328 352 struct expression *left = expr->left, *right = expr->right;
329 353 unsigned long mod = expr->ctype->ctype.modifiers;
330 354 long double l, r, res;
331 355
332 356 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
333 357 return 0;
334 358
335 359 l = left->fvalue;
336 360 r = right->fvalue;
337 361
338 362 if (mod & MOD_LONGLONG) {
339 363 switch (expr->op) {
340 364 case '+': res = l + r; break;
341 365 case '-': res = l - r; break;
342 366 case '*': res = l * r; break;
343 367 case '/': if (!r) goto Div;
344 368 res = l / r; break;
345 369 default: return 0;
346 370 }
347 371 } else if (mod & MOD_LONG) {
348 372 switch (expr->op) {
349 373 case '+': res = (double) l + (double) r; break;
350 374 case '-': res = (double) l - (double) r; break;
351 375 case '*': res = (double) l * (double) r; break;
352 376 case '/': if (!r) goto Div;
353 377 res = (double) l / (double) r; break;
354 378 default: return 0;
355 379 }
356 380 } else {
357 381 switch (expr->op) {
358 382 case '+': res = (float)l + (float)r; break;
359 383 case '-': res = (float)l - (float)r; break;
360 384 case '*': res = (float)l * (float)r; break;
361 385 case '/': if (!r) goto Div;
362 386 res = (float)l / (float)r; break;
363 387 default: return 0;
364 388 }
365 389 }
366 390 expr->type = EXPR_FVALUE;
367 391 expr->fvalue = res;
368 392 return 1;
369 393 Div:
370 394 if (!conservative)
371 395 warning(expr->pos, "division by zero");
372 396 return 0;
373 397 }
374 398
375 399 static int simplify_float_cmp(struct expression *expr, struct symbol *ctype)
376 400 {
377 401 struct expression *left = expr->left, *right = expr->right;
378 402 long double l, r;
379 403
380 404 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
381 405 return 0;
382 406
383 407 l = left->fvalue;
384 408 r = right->fvalue;
385 409 switch (expr->op) {
386 410 case '<': expr->value = l < r; break;
387 411 case '>': expr->value = l > r; break;
388 412 case SPECIAL_LTE: expr->value = l <= r; break;
389 413 case SPECIAL_GTE: expr->value = l >= r; break;
390 414 case SPECIAL_EQUAL: expr->value = l == r; break;
391 415 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
392 416 }
393 417 expr->type = EXPR_VALUE;
394 418 expr->taint = 0;
395 419 return 1;
396 420 }
397 421
398 422 static int expand_binop(struct expression *expr)
399 423 {
400 424 int cost;
401 425
402 426 cost = expand_expression(expr->left);
403 427 cost += expand_expression(expr->right);
404 428 if (simplify_int_binop(expr, expr->ctype))
405 429 return 0;
406 430 if (simplify_float_binop(expr))
407 431 return 0;
408 432 return cost + 1;
409 433 }
410 434
411 435 static int expand_logical(struct expression *expr)
412 436 {
413 437 struct expression *left = expr->left;
414 438 struct expression *right;
415 439 int cost, rcost;
416 440
417 441 /* Do immediate short-circuiting ... */
418 442 cost = expand_expression(left);
419 443 if (left->type == EXPR_VALUE) {
420 444 if (expr->op == SPECIAL_LOGICAL_AND) {
421 445 if (!left->value) {
422 446 expr->type = EXPR_VALUE;
423 447 expr->value = 0;
424 448 expr->taint = left->taint;
425 449 return 0;
426 450 }
427 451 } else {
428 452 if (left->value) {
429 453 expr->type = EXPR_VALUE;
430 454 expr->value = 1;
431 455 expr->taint = left->taint;
432 456 return 0;
433 457 }
434 458 }
435 459 }
436 460
437 461 right = expr->right;
438 462 rcost = expand_expression(right);
439 463 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
440 464 /*
441 465 * We know the left value doesn't matter, since
442 466 * otherwise we would have short-circuited it..
443 467 */
444 468 expr->type = EXPR_VALUE;
445 469 expr->value = right->value != 0;
446 470 expr->taint = left->taint | right->taint;
447 471 return 0;
448 472 }
449 473
450 474 /*
451 475 * If the right side is safe and cheaper than a branch,
452 476 * just avoid the branch and turn it into a regular binop
453 477 * style SAFELOGICAL.
454 478 */
455 479 if (rcost < BRANCH_COST) {
456 480 expr->type = EXPR_BINOP;
457 481 rcost -= BRANCH_COST - 1;
458 482 }
459 483
460 484 return cost + BRANCH_COST + rcost;
461 485 }
462 486
463 487 static int expand_comma(struct expression *expr)
464 488 {
465 489 int cost;
466 490
467 491 cost = expand_expression(expr->left);
468 492 cost += expand_expression(expr->right);
469 493 if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE) {
470 494 unsigned flags = expr->flags;
↓ open down ↓ |
270 lines elided |
↑ open up ↑ |
471 495 unsigned taint;
472 496 taint = expr->left->type == EXPR_VALUE ? expr->left->taint : 0;
473 497 *expr = *expr->right;
474 498 expr->flags = flags;
475 499 if (expr->type == EXPR_VALUE)
476 500 expr->taint |= Taint_comma | taint;
477 501 }
478 502 return cost;
479 503 }
480 504
481 -#define MOD_IGN (MOD_VOLATILE | MOD_CONST)
505 +#define MOD_IGN (MOD_QUALIFIER)
482 506
483 507 static int compare_types(int op, struct symbol *left, struct symbol *right)
484 508 {
485 509 struct ctype c1 = {.base_type = left};
486 510 struct ctype c2 = {.base_type = right};
487 511 switch (op) {
488 512 case SPECIAL_EQUAL:
489 513 return !type_difference(&c1, &c2, MOD_IGN, MOD_IGN);
490 514 case SPECIAL_NOTEQUAL:
491 515 return type_difference(&c1, &c2, MOD_IGN, MOD_IGN) != NULL;
492 516 case '<':
493 517 return left->bit_size < right->bit_size;
494 518 case '>':
495 519 return left->bit_size > right->bit_size;
496 520 case SPECIAL_LTE:
497 521 return left->bit_size <= right->bit_size;
498 522 case SPECIAL_GTE:
499 523 return left->bit_size >= right->bit_size;
500 524 }
501 525 return 0;
502 526 }
503 527
504 528 static int expand_compare(struct expression *expr)
505 529 {
506 530 struct expression *left = expr->left, *right = expr->right;
507 531 int cost;
508 532
509 533 cost = expand_expression(left);
510 534 cost += expand_expression(right);
511 535
512 536 if (left && right) {
513 537 /* Type comparison? */
514 538 if (left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
515 539 int op = expr->op;
516 540 expr->type = EXPR_VALUE;
517 541 expr->value = compare_types(op, left->symbol, right->symbol);
518 542 expr->taint = 0;
519 543 return 0;
520 544 }
521 545 if (simplify_cmp_binop(expr, left->ctype))
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
522 546 return 0;
523 547 if (simplify_float_cmp(expr, left->ctype))
524 548 return 0;
525 549 }
526 550 return cost + 1;
527 551 }
528 552
529 553 static int expand_conditional(struct expression *expr)
530 554 {
531 555 struct expression *cond = expr->conditional;
532 - struct expression *true = expr->cond_true;
533 - struct expression *false = expr->cond_false;
556 + struct expression *valt = expr->cond_true;
557 + struct expression *valf = expr->cond_false;
534 558 int cost, cond_cost;
535 559
536 560 cond_cost = expand_expression(cond);
537 561 if (cond->type == EXPR_VALUE) {
538 562 unsigned flags = expr->flags;
539 563 if (!cond->value)
540 - true = false;
541 - if (!true)
542 - true = cond;
543 - cost = expand_expression(true);
544 - *expr = *true;
564 + valt = valf;
565 + if (!valt)
566 + valt = cond;
567 + cost = expand_expression(valt);
568 + *expr = *valt;
545 569 expr->flags = flags;
546 570 if (expr->type == EXPR_VALUE)
547 571 expr->taint |= cond->taint;
548 572 return cost;
549 573 }
550 574
551 - cost = expand_expression(true);
552 - cost += expand_expression(false);
575 + cost = expand_expression(valt);
576 + cost += expand_expression(valf);
553 577
554 578 if (cost < SELECT_COST) {
555 579 expr->type = EXPR_SELECT;
556 580 cost -= BRANCH_COST - 1;
557 581 }
558 582
559 583 return cost + cond_cost + BRANCH_COST;
560 584 }
561 -
585 +
586 +static void check_assignment(struct expression *expr)
587 +{
588 + struct expression *right;
589 +
590 + switch (expr->op) {
591 + case SPECIAL_SHL_ASSIGN:
592 + case SPECIAL_SHR_ASSIGN:
593 + right = expr->right;
594 + if (right->type != EXPR_VALUE)
595 + break;
596 + check_shift_count(expr, right);
597 + break;
598 + }
599 + return;
600 +}
601 +
562 602 static int expand_assignment(struct expression *expr)
563 603 {
564 604 expand_expression(expr->left);
565 605 expand_expression(expr->right);
606 +
607 + if (!conservative)
608 + check_assignment(expr);
566 609 return SIDE_EFFECTS;
567 610 }
568 611
569 612 static int expand_addressof(struct expression *expr)
570 613 {
571 614 return expand_expression(expr->unop);
572 615 }
573 616
574 617 /*
575 618 * Look up a trustable initializer value at the requested offset.
576 619 *
577 620 * Return NULL if no such value can be found or statically trusted.
578 621 *
579 622 * FIXME!! We should check that the size is right!
580 623 */
581 624 static struct expression *constant_symbol_value(struct symbol *sym, int offset)
582 625 {
583 626 struct expression *value;
584 627
585 - if (sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))
628 + if (sym->ctype.modifiers & MOD_ACCESS)
586 629 return NULL;
587 630 value = sym->initializer;
588 631 if (!value)
589 632 return NULL;
590 633 if (value->type == EXPR_INITIALIZER) {
591 634 struct expression *entry;
592 635 FOR_EACH_PTR(value->expr_list, entry) {
593 636 if (entry->type != EXPR_POS) {
594 637 if (offset)
595 638 continue;
596 639 return entry;
597 640 }
598 641 if (entry->init_offset < offset)
599 642 continue;
600 643 if (entry->init_offset > offset)
601 644 return NULL;
602 645 return entry->init_expr;
603 646 } END_FOR_EACH_PTR(entry);
604 647 return NULL;
605 648 }
606 649 return value;
607 650 }
608 651
609 652 static int expand_dereference(struct expression *expr)
610 653 {
611 654 struct expression *unop = expr->unop;
612 655 unsigned int offset;
613 656
614 657 expand_expression(unop);
615 658
616 659 /*
617 660 * NOTE! We get a bogus warning right now for some special
618 661 * cases: apparently I've screwed up the optimization of
619 662 * a zero-offset dereference, and the ctype is wrong.
620 663 *
621 664 * Leave the warning in anyway, since this is also a good
622 665 * test for me to get the type evaluation right..
623 666 */
624 667 if (expr->ctype->ctype.modifiers & MOD_NODEREF)
625 668 warning(unop->pos, "dereference of noderef expression");
626 669
627 670 /*
628 671 * Is it "symbol" or "symbol + offset"?
629 672 */
630 673 offset = 0;
631 674 if (unop->type == EXPR_BINOP && unop->op == '+') {
632 675 struct expression *right = unop->right;
633 676 if (right->type == EXPR_VALUE) {
634 677 offset = right->value;
635 678 unop = unop->left;
636 679 }
↓ open down ↓ |
41 lines elided |
↑ open up ↑ |
637 680 }
638 681
639 682 if (unop->type == EXPR_SYMBOL) {
640 683 struct symbol *sym = unop->symbol;
641 684 struct expression *value = constant_symbol_value(sym, offset);
642 685
643 686 /* Const symbol with a constant initializer? */
644 687 if (value) {
645 688 /* FIXME! We should check that the size is right! */
646 689 if (value->type == EXPR_VALUE) {
690 + if (is_bitfield_type(value->ctype))
691 + return UNSAFE;
647 692 expr->type = EXPR_VALUE;
648 693 expr->value = value->value;
649 694 expr->taint = 0;
650 695 return 0;
651 696 } else if (value->type == EXPR_FVALUE) {
652 697 expr->type = EXPR_FVALUE;
653 698 expr->fvalue = value->fvalue;
654 699 return 0;
655 700 }
656 701 }
657 702
658 703 /* Direct symbol dereference? Cheap and safe */
659 704 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
660 705 }
661 706
662 707 return UNSAFE;
663 708 }
664 709
665 710 static int simplify_preop(struct expression *expr)
666 711 {
667 712 struct expression *op = expr->unop;
668 713 unsigned long long v, mask;
669 714
670 715 if (op->type != EXPR_VALUE)
671 716 return 0;
672 717
673 718 mask = 1ULL << (expr->ctype->bit_size-1);
674 719 v = op->value;
675 720 switch (expr->op) {
676 721 case '+': break;
677 722 case '-':
678 723 if (v == mask && !(expr->ctype->ctype.modifiers & MOD_UNSIGNED))
679 724 goto Overflow;
680 725 v = -v;
681 726 break;
682 727 case '!': v = !v; break;
683 728 case '~': v = ~v; break;
684 729 default: return 0;
685 730 }
686 731 mask = mask | (mask-1);
687 732 expr->value = v & mask;
688 733 expr->type = EXPR_VALUE;
689 734 expr->taint = op->taint;
690 735 return 1;
691 736
692 737 Overflow:
693 738 if (!conservative)
694 739 warning(expr->pos, "constant integer operation overflow");
695 740 return 0;
696 741 }
697 742
698 743 static int simplify_float_preop(struct expression *expr)
699 744 {
700 745 struct expression *op = expr->unop;
701 746 long double v;
702 747
703 748 if (op->type != EXPR_FVALUE)
704 749 return 0;
705 750 v = op->fvalue;
706 751 switch (expr->op) {
707 752 case '+': break;
708 753 case '-': v = -v; break;
709 754 default: return 0;
710 755 }
711 756 expr->fvalue = v;
712 757 expr->type = EXPR_FVALUE;
713 758 return 1;
714 759 }
715 760
716 761 /*
717 762 * Unary post-ops: x++ and x--
718 763 */
719 764 static int expand_postop(struct expression *expr)
720 765 {
721 766 expand_expression(expr->unop);
722 767 return SIDE_EFFECTS;
723 768 }
724 769
725 770 static int expand_preop(struct expression *expr)
726 771 {
727 772 int cost;
728 773
729 774 switch (expr->op) {
730 775 case '*':
731 776 return expand_dereference(expr);
732 777
733 778 case '&':
734 779 return expand_addressof(expr);
735 780
736 781 case SPECIAL_INCREMENT:
737 782 case SPECIAL_DECREMENT:
738 783 /*
739 784 * From a type evaluation standpoint the preops are
740 785 * the same as the postops
741 786 */
742 787 return expand_postop(expr);
743 788
744 789 default:
745 790 break;
746 791 }
747 792 cost = expand_expression(expr->unop);
748 793
749 794 if (simplify_preop(expr))
750 795 return 0;
751 796 if (simplify_float_preop(expr))
752 797 return 0;
753 798 return cost + 1;
754 799 }
755 800
756 801 static int expand_arguments(struct expression_list *head)
757 802 {
758 803 int cost = 0;
759 804 struct expression *expr;
760 805
761 806 FOR_EACH_PTR (head, expr) {
762 807 cost += expand_expression(expr);
763 808 } END_FOR_EACH_PTR(expr);
764 809 return cost;
765 810 }
766 811
767 812 static int expand_cast(struct expression *expr)
768 813 {
769 814 int cost;
770 815 struct expression *target = expr->cast_expression;
771 816
772 817 cost = expand_expression(target);
773 818
774 819 /* Simplify normal integer casts.. */
775 820 if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
776 821 cast_value(expr, expr->ctype, target, target->ctype);
777 822 return 0;
778 823 }
779 824 return cost + 1;
780 825 }
↓ open down ↓ |
124 lines elided |
↑ open up ↑ |
781 826
782 827 /*
783 828 * expand a call expression with a symbol. This
784 829 * should expand builtins.
785 830 */
786 831 static int expand_symbol_call(struct expression *expr, int cost)
787 832 {
788 833 struct expression *fn = expr->fn;
789 834 struct symbol *ctype = fn->ctype;
790 835
836 + expand_expression(fn);
837 +
791 838 if (fn->type != EXPR_PREOP)
792 839 return SIDE_EFFECTS;
793 840
794 841 if (ctype->op && ctype->op->expand)
795 842 return ctype->op->expand(expr, cost);
796 843
797 844 if (ctype->ctype.modifiers & MOD_PURE)
798 845 return cost + 1;
799 846
800 847 return SIDE_EFFECTS;
801 848 }
802 849
803 850 static int expand_call(struct expression *expr)
804 851 {
805 852 int cost;
806 853 struct symbol *sym;
807 854 struct expression *fn = expr->fn;
808 855
809 856 cost = expand_arguments(expr->args);
810 857 sym = fn->ctype;
811 858 if (!sym) {
812 859 expression_error(expr, "function has no type");
813 860 return SIDE_EFFECTS;
814 861 }
815 862 if (sym->type == SYM_NODE)
816 863 return expand_symbol_call(expr, cost);
817 864
818 865 return SIDE_EFFECTS;
819 866 }
820 867
821 868 static int expand_expression_list(struct expression_list *list)
822 869 {
823 870 int cost = 0;
824 871 struct expression *expr;
825 872
826 873 FOR_EACH_PTR(list, expr) {
827 874 cost += expand_expression(expr);
828 875 } END_FOR_EACH_PTR(expr);
829 876 return cost;
830 877 }
831 878
832 879 /*
833 880 * We can simplify nested position expressions if
834 881 * this is a simple (single) positional expression.
835 882 */
836 883 static int expand_pos_expression(struct expression *expr)
837 884 {
838 885 struct expression *nested = expr->init_expr;
839 886 unsigned long offset = expr->init_offset;
840 887 int nr = expr->init_nr;
841 888
842 889 if (nr == 1) {
843 890 switch (nested->type) {
844 891 case EXPR_POS:
845 892 offset += nested->init_offset;
846 893 *expr = *nested;
847 894 expr->init_offset = offset;
848 895 nested = expr;
849 896 break;
850 897
851 898 case EXPR_INITIALIZER: {
852 899 struct expression *reuse = nested, *entry;
853 900 *expr = *nested;
854 901 FOR_EACH_PTR(expr->expr_list, entry) {
855 902 if (entry->type == EXPR_POS) {
856 903 entry->init_offset += offset;
857 904 } else {
858 905 if (!reuse) {
859 906 /*
860 907 * This happens rarely, but it can happen
861 908 * with bitfields that are all at offset
862 909 * zero..
863 910 */
864 911 reuse = alloc_expression(entry->pos, EXPR_POS);
865 912 }
866 913 reuse->type = EXPR_POS;
867 914 reuse->ctype = entry->ctype;
868 915 reuse->init_offset = offset;
869 916 reuse->init_nr = 1;
870 917 reuse->init_expr = entry;
871 918 REPLACE_CURRENT_PTR(entry, reuse);
872 919 reuse = NULL;
873 920 }
874 921 } END_FOR_EACH_PTR(entry);
875 922 nested = expr;
876 923 break;
877 924 }
878 925
879 926 default:
880 927 break;
881 928 }
882 929 }
883 930 return expand_expression(nested);
884 931 }
885 932
886 933 static unsigned long bit_offset(const struct expression *expr)
887 934 {
888 935 unsigned long offset = 0;
889 936 while (expr->type == EXPR_POS) {
890 937 offset += bytes_to_bits(expr->init_offset);
891 938 expr = expr->init_expr;
892 939 }
893 940 if (expr && expr->ctype)
894 941 offset += expr->ctype->bit_offset;
895 942 return offset;
896 943 }
897 944
898 945 static unsigned long bit_range(const struct expression *expr)
899 946 {
900 947 unsigned long range = 0;
901 948 unsigned long size = 0;
902 949 while (expr->type == EXPR_POS) {
903 950 unsigned long nr = expr->init_nr;
904 951 size = expr->ctype->bit_size;
905 952 range += (nr - 1) * size;
906 953 expr = expr->init_expr;
907 954 }
908 955 range += size;
909 956 return range;
910 957 }
911 958
912 959 static int compare_expressions(const void *_a, const void *_b)
913 960 {
914 961 const struct expression *a = _a;
915 962 const struct expression *b = _b;
916 963 unsigned long a_pos = bit_offset(a);
917 964 unsigned long b_pos = bit_offset(b);
918 965
919 966 return (a_pos < b_pos) ? -1 : (a_pos == b_pos) ? 0 : 1;
920 967 }
921 968
922 969 static void sort_expression_list(struct expression_list **list)
923 970 {
924 971 sort_list((struct ptr_list **)list, compare_expressions);
925 972 }
926 973
927 974 static void verify_nonoverlapping(struct expression_list **list, struct expression *expr)
928 975 {
929 976 struct expression *a = NULL;
930 977 unsigned long max = 0;
931 978 unsigned long whole = expr->ctype->bit_size;
932 979 struct expression *b;
933 980
934 981 if (!Woverride_init)
935 982 return;
936 983
937 984 FOR_EACH_PTR(*list, b) {
938 985 unsigned long off, end;
939 986 if (!b->ctype || !b->ctype->bit_size)
940 987 continue;
941 988 off = bit_offset(b);
942 989 if (a && off < max) {
943 990 warning(a->pos, "Initializer entry defined twice");
944 991 info(b->pos, " also defined here");
945 992 if (!Woverride_init_all)
946 993 return;
947 994 }
948 995 end = off + bit_range(b);
949 996 if (!a && !Woverride_init_whole_range) {
950 997 // If first entry is the whole range, do not let
951 998 // any warning about it (this allow to initialize
952 999 // an array with some default value and then override
953 1000 // some specific entries).
954 1001 if (off == 0 && end == whole)
955 1002 continue;
956 1003 }
957 1004 if (end > max) {
958 1005 max = end;
959 1006 a = b;
960 1007 }
961 1008 } END_FOR_EACH_PTR(b);
962 1009 }
963 1010
964 1011 static int expand_expression(struct expression *expr)
965 1012 {
966 1013 if (!expr)
967 1014 return 0;
968 1015 if (!expr->ctype || expr->ctype == &bad_ctype)
969 1016 return UNSAFE;
970 1017
971 1018 switch (expr->type) {
972 1019 case EXPR_VALUE:
973 1020 case EXPR_FVALUE:
974 1021 case EXPR_STRING:
975 1022 return 0;
976 1023 case EXPR_TYPE:
977 1024 case EXPR_SYMBOL:
978 1025 return expand_symbol_expression(expr);
979 1026 case EXPR_BINOP:
980 1027 return expand_binop(expr);
981 1028
982 1029 case EXPR_LOGICAL:
983 1030 return expand_logical(expr);
984 1031
985 1032 case EXPR_COMMA:
986 1033 return expand_comma(expr);
987 1034
988 1035 case EXPR_COMPARE:
989 1036 return expand_compare(expr);
990 1037
991 1038 case EXPR_ASSIGNMENT:
992 1039 return expand_assignment(expr);
993 1040
994 1041 case EXPR_PREOP:
995 1042 return expand_preop(expr);
996 1043
997 1044 case EXPR_POSTOP:
998 1045 return expand_postop(expr);
999 1046
1000 1047 case EXPR_CAST:
1001 1048 case EXPR_FORCE_CAST:
1002 1049 case EXPR_IMPLIED_CAST:
1003 1050 return expand_cast(expr);
1004 1051
1005 1052 case EXPR_CALL:
1006 1053 return expand_call(expr);
1007 1054
1008 1055 case EXPR_DEREF:
1009 1056 warning(expr->pos, "we should not have an EXPR_DEREF left at expansion time");
1010 1057 return UNSAFE;
1011 1058
1012 1059 case EXPR_SELECT:
1013 1060 case EXPR_CONDITIONAL:
1014 1061 return expand_conditional(expr);
1015 1062
1016 1063 case EXPR_STATEMENT: {
1017 1064 struct statement *stmt = expr->statement;
1018 1065 int cost = expand_statement(stmt);
1019 1066
1020 1067 if (stmt->type == STMT_EXPRESSION && stmt->expression)
1021 1068 *expr = *stmt->expression;
1022 1069 return cost;
1023 1070 }
1024 1071
1025 1072 case EXPR_LABEL:
1026 1073 return 0;
1027 1074
1028 1075 case EXPR_INITIALIZER:
1029 1076 sort_expression_list(&expr->expr_list);
1030 1077 verify_nonoverlapping(&expr->expr_list, expr);
1031 1078 return expand_expression_list(expr->expr_list);
1032 1079
1033 1080 case EXPR_IDENTIFIER:
1034 1081 return UNSAFE;
1035 1082
1036 1083 case EXPR_INDEX:
1037 1084 return UNSAFE;
1038 1085
1039 1086 case EXPR_SLICE:
1040 1087 return expand_expression(expr->base) + 1;
↓ open down ↓ |
240 lines elided |
↑ open up ↑ |
1041 1088
1042 1089 case EXPR_POS:
1043 1090 return expand_pos_expression(expr);
1044 1091
1045 1092 case EXPR_SIZEOF:
1046 1093 case EXPR_PTRSIZEOF:
1047 1094 case EXPR_ALIGNOF:
1048 1095 case EXPR_OFFSETOF:
1049 1096 expression_error(expr, "internal front-end error: sizeof in expansion?");
1050 1097 return UNSAFE;
1098 + case EXPR_ASM_OPERAND:
1099 + expression_error(expr, "internal front-end error: ASM_OPERAND in expansion?");
1100 + return UNSAFE;
1051 1101 }
1052 1102 return SIDE_EFFECTS;
1053 1103 }
1054 1104
1055 1105 static void expand_const_expression(struct expression *expr, const char *where)
1056 1106 {
1057 1107 if (expr) {
1058 1108 expand_expression(expr);
1059 1109 if (expr->type != EXPR_VALUE)
1060 1110 expression_error(expr, "Expected constant expression in %s", where);
1061 1111 }
1062 1112 }
1063 1113
1064 1114 int expand_symbol(struct symbol *sym)
1065 1115 {
1066 1116 int retval;
1067 1117 struct symbol *base_type;
1068 1118
1069 1119 if (!sym)
1070 1120 return 0;
1071 1121 base_type = sym->ctype.base_type;
1072 1122 if (!base_type)
1073 1123 return 0;
1074 1124
1075 1125 retval = expand_expression(sym->initializer);
1076 1126 /* expand the body of the symbol */
1077 1127 if (base_type->type == SYM_FN) {
1078 1128 if (base_type->stmt)
1079 1129 expand_statement(base_type->stmt);
1080 1130 }
1081 1131 return retval;
1082 1132 }
1083 1133
1084 1134 static void expand_return_expression(struct statement *stmt)
1085 1135 {
1086 1136 expand_expression(stmt->expression);
1087 1137 }
1088 1138
1089 1139 static int expand_if_statement(struct statement *stmt)
1090 1140 {
1091 1141 struct expression *expr = stmt->if_conditional;
1092 1142
1093 1143 if (!expr || !expr->ctype || expr->ctype == &bad_ctype)
1094 1144 return UNSAFE;
1095 1145
1096 1146 expand_expression(expr);
1097 1147
1098 1148 /* This is only valid if nobody jumps into the "dead" side */
1099 1149 #if 0
1100 1150 /* Simplify constant conditionals without even evaluating the false side */
1101 1151 if (expr->type == EXPR_VALUE) {
1102 1152 struct statement *simple;
1103 1153 simple = expr->value ? stmt->if_true : stmt->if_false;
1104 1154
1105 1155 /* Nothing? */
1106 1156 if (!simple) {
1107 1157 stmt->type = STMT_NONE;
1108 1158 return 0;
1109 1159 }
1110 1160 expand_statement(simple);
1111 1161 *stmt = *simple;
1112 1162 return SIDE_EFFECTS;
1113 1163 }
1114 1164 #endif
1115 1165 expand_statement(stmt->if_true);
1116 1166 expand_statement(stmt->if_false);
1117 1167 return SIDE_EFFECTS;
1118 1168 }
1119 1169
1120 1170 /*
1121 1171 * Expanding a compound statement is really just
1122 1172 * about adding up the costs of each individual
1123 1173 * statement.
1124 1174 *
1125 1175 * We also collapse a simple compound statement:
1126 1176 * this would trigger for simple inline functions,
1127 1177 * except we would have to check the "return"
1128 1178 * symbol usage. Next time.
1129 1179 */
1130 1180 static int expand_compound(struct statement *stmt)
1131 1181 {
1132 1182 struct statement *s, *last;
1133 1183 int cost, statements;
1134 1184
1135 1185 if (stmt->ret)
1136 1186 expand_symbol(stmt->ret);
1137 1187
1138 1188 last = stmt->args;
1139 1189 cost = expand_statement(last);
1140 1190 statements = last != NULL;
1141 1191 FOR_EACH_PTR(stmt->stmts, s) {
1142 1192 statements++;
1143 1193 last = s;
1144 1194 cost += expand_statement(s);
1145 1195 } END_FOR_EACH_PTR(s);
1146 1196
1147 1197 if (statements == 1 && !stmt->ret)
1148 1198 *stmt = *last;
1149 1199
1150 1200 return cost;
1151 1201 }
1152 1202
1153 1203 static int expand_statement(struct statement *stmt)
1154 1204 {
1155 1205 if (!stmt)
1156 1206 return 0;
1157 1207
1158 1208 switch (stmt->type) {
1159 1209 case STMT_DECLARATION: {
1160 1210 struct symbol *sym;
1161 1211 FOR_EACH_PTR(stmt->declaration, sym) {
1162 1212 expand_symbol(sym);
1163 1213 } END_FOR_EACH_PTR(sym);
1164 1214 return SIDE_EFFECTS;
1165 1215 }
1166 1216
1167 1217 case STMT_RETURN:
1168 1218 expand_return_expression(stmt);
1169 1219 return SIDE_EFFECTS;
1170 1220
1171 1221 case STMT_EXPRESSION:
1172 1222 return expand_expression(stmt->expression);
1173 1223
1174 1224 case STMT_COMPOUND:
1175 1225 return expand_compound(stmt);
1176 1226
1177 1227 case STMT_IF:
1178 1228 return expand_if_statement(stmt);
1179 1229
1180 1230 case STMT_ITERATOR:
1181 1231 expand_expression(stmt->iterator_pre_condition);
1182 1232 expand_expression(stmt->iterator_post_condition);
1183 1233 expand_statement(stmt->iterator_pre_statement);
1184 1234 expand_statement(stmt->iterator_statement);
1185 1235 expand_statement(stmt->iterator_post_statement);
1186 1236 return SIDE_EFFECTS;
1187 1237
1188 1238 case STMT_SWITCH:
1189 1239 expand_expression(stmt->switch_expression);
1190 1240 expand_statement(stmt->switch_statement);
1191 1241 return SIDE_EFFECTS;
1192 1242
1193 1243 case STMT_CASE:
1194 1244 expand_const_expression(stmt->case_expression, "case statement");
1195 1245 expand_const_expression(stmt->case_to, "case statement");
1196 1246 expand_statement(stmt->case_statement);
1197 1247 return SIDE_EFFECTS;
1198 1248
1199 1249 case STMT_LABEL:
1200 1250 expand_statement(stmt->label_statement);
1201 1251 return SIDE_EFFECTS;
1202 1252
1203 1253 case STMT_GOTO:
1204 1254 expand_expression(stmt->goto_expression);
1205 1255 return SIDE_EFFECTS;
1206 1256
1207 1257 case STMT_NONE:
1208 1258 break;
1209 1259 case STMT_ASM:
1210 1260 /* FIXME! Do the asm parameter evaluation! */
1211 1261 break;
1212 1262 case STMT_CONTEXT:
1213 1263 expand_expression(stmt->expression);
1214 1264 break;
1215 1265 case STMT_RANGE:
1216 1266 expand_expression(stmt->range_expression);
1217 1267 expand_expression(stmt->range_low);
1218 1268 expand_expression(stmt->range_high);
1219 1269 break;
1220 1270 }
1221 1271 return SIDE_EFFECTS;
1222 1272 }
1223 1273
1224 1274 static inline int bad_integer_constant_expression(struct expression *expr)
1225 1275 {
1226 1276 if (!(expr->flags & CEF_ICE))
1227 1277 return 1;
1228 1278 if (expr->taint & Taint_comma)
1229 1279 return 1;
1230 1280 return 0;
1231 1281 }
1232 1282
1233 1283 static long long __get_expression_value(struct expression *expr, int strict)
1234 1284 {
1235 1285 long long value, mask;
1236 1286 struct symbol *ctype;
1237 1287
1238 1288 if (!expr)
1239 1289 return 0;
1240 1290 ctype = evaluate_expression(expr);
↓ open down ↓ |
180 lines elided |
↑ open up ↑ |
1241 1291 if (!ctype) {
1242 1292 expression_error(expr, "bad constant expression type");
1243 1293 return 0;
1244 1294 }
1245 1295 expand_expression(expr);
1246 1296 if (expr->type != EXPR_VALUE) {
1247 1297 if (strict != 2)
1248 1298 expression_error(expr, "bad constant expression");
1249 1299 return 0;
1250 1300 }
1301 +#if 0 // This complains about "1 ? 1 :__bits_per()" which the kernel use
1251 1302 if ((strict == 1) && bad_integer_constant_expression(expr)) {
1252 1303 expression_error(expr, "bad integer constant expression");
1253 1304 return 0;
1254 1305 }
1306 +#endif
1255 1307
1256 1308 value = expr->value;
1257 1309 mask = 1ULL << (ctype->bit_size-1);
1258 1310
1259 1311 if (value & mask) {
1260 1312 while (ctype->type != SYM_BASETYPE)
1261 1313 ctype = ctype->ctype.base_type;
1262 1314 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1263 1315 value = value | mask | ~(mask-1);
1264 1316 }
1265 1317 return value;
1266 1318 }
1267 1319
1268 1320 long long get_expression_value(struct expression *expr)
1269 1321 {
1270 1322 return __get_expression_value(expr, 0);
1271 1323 }
1272 1324
1273 1325 long long const_expression_value(struct expression *expr)
1274 1326 {
1275 1327 return __get_expression_value(expr, 1);
1276 1328 }
1277 1329
1278 1330 long long get_expression_value_silent(struct expression *expr)
1279 1331 {
1280 1332
1281 1333 return __get_expression_value(expr, 2);
1282 1334 }
1283 1335
1284 1336 int expr_truth_value(struct expression *expr)
1285 1337 {
1286 1338 const int saved = conservative;
1287 1339 struct symbol *ctype;
1288 1340
1289 1341 if (!expr)
1290 1342 return 0;
1291 1343
1292 1344 ctype = evaluate_expression(expr);
1293 1345 if (!ctype)
1294 1346 return -1;
1295 1347
1296 1348 conservative = 1;
1297 1349 expand_expression(expr);
1298 1350 conservative = saved;
1299 1351
1300 1352 redo:
1301 1353 switch (expr->type) {
1302 1354 case EXPR_COMMA:
1303 1355 expr = expr->right;
1304 1356 goto redo;
1305 1357 case EXPR_VALUE:
1306 1358 return expr->value != 0;
1307 1359 case EXPR_FVALUE:
1308 1360 return expr->fvalue != 0;
1309 1361 default:
1310 1362 return -1;
1311 1363 }
1312 1364 }
1313 1365
1314 1366 int is_zero_constant(struct expression *expr)
1315 1367 {
1316 1368 const int saved = conservative;
1317 1369 conservative = 1;
1318 1370 expand_expression(expr);
1319 1371 conservative = saved;
1320 1372 return expr->type == EXPR_VALUE && !expr->value;
1321 1373 }
↓ open down ↓ |
57 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX