Print this page
new smatch


  24  *
  25  * expand constant expressions.
  26  */
  27 #include <stdlib.h>
  28 #include <stdarg.h>
  29 #include <stddef.h>
  30 #include <stdio.h>
  31 #include <string.h>
  32 #include <ctype.h>
  33 #include <unistd.h>
  34 #include <fcntl.h>
  35 #include <limits.h>
  36 
  37 #include "lib.h"
  38 #include "allocate.h"
  39 #include "parse.h"
  40 #include "token.h"
  41 #include "symbol.h"
  42 #include "target.h"
  43 #include "expression.h"

  44 #include "expand.h"
  45 
  46 
  47 static int expand_expression(struct expression *);
  48 static int expand_statement(struct statement *);





  49 static int conservative;
  50 
  51 static int expand_symbol_expression(struct expression *expr)
  52 {
  53         struct symbol *sym = expr->symbol;
  54 
  55         if (sym == &zero_int) {
  56                 if (Wundef)
  57                         warning(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
  58                 expr->type = EXPR_VALUE;
  59                 expr->value = 0;
  60                 expr->taint = 0;
  61                 return 0;
  62         }
  63         /* The cost of a symbol expression is lower for on-stack symbols */
  64         return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
  65 }
  66 
  67 static long long get_longlong(struct expression *expr)
  68 {


 140                 expr->type = EXPR_VALUE;
 141                 expr->taint = 0;
 142                 goto Int;
 143         }
 144 
 145         if (!is_float_type(oldtype))
 146                 expr->fvalue = (long double)get_longlong(old);
 147         else
 148                 expr->fvalue = old->fvalue;
 149 
 150         if (!(newtype->ctype.modifiers & MOD_LONGLONG) && \
 151             !(newtype->ctype.modifiers & MOD_LONGLONGLONG)) {
 152                 if ((newtype->ctype.modifiers & MOD_LONG))
 153                         expr->fvalue = (double)expr->fvalue;
 154                 else
 155                         expr->fvalue = (float)expr->fvalue;
 156         }
 157         expr->type = EXPR_FVALUE;
 158 }
 159 
 160 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
 161 {
 162         warning(expr->pos, "shift too big (%u) for type %s", count, show_typename(ctype));
 163         count &= ctype->bit_size-1;
 164         return count;









 165 }
 166 













 167 /*
 168  * CAREFUL! We need to get the size and sign of the
 169  * result right!
 170  */
 171 #define CONVERT(op,s)   (((op)<<1)+(s))
 172 #define SIGNED(op)      CONVERT(op, 1)
 173 #define UNSIGNED(op)    CONVERT(op, 0)
 174 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
 175 {
 176         struct expression *left = expr->left, *right = expr->right;
 177         unsigned long long v, l, r, mask;
 178         signed long long sl, sr;
 179         int is_signed;
 180 
 181         if (right->type != EXPR_VALUE)
 182                 return 0;
 183         r = right->value;
 184         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                 }
 191         }
 192         if (left->type != EXPR_VALUE)
 193                 return 0;
 194         l = left->value; r = right->value;
 195         is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
 196         mask = 1ULL << (ctype->bit_size-1);
 197         sl = l; sr = r;
 198         if (is_signed && (sl & mask))
 199                 sl |= ~(mask-1);
 200         if (is_signed && (sr & mask))
 201                 sr |= ~(mask-1);
 202         
 203         switch (CONVERT(expr->op,is_signed)) {
 204         case SIGNED('+'):
 205         case UNSIGNED('+'):
 206                 v = l + r;
 207                 break;
 208 
 209         case SIGNED('-'):
 210         case UNSIGNED('-'):
 211                 v = l - r;


 461 }
 462 
 463 static int expand_comma(struct expression *expr)
 464 {
 465         int cost;
 466 
 467         cost = expand_expression(expr->left);
 468         cost += expand_expression(expr->right);
 469         if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE) {
 470                 unsigned flags = expr->flags;
 471                 unsigned taint;
 472                 taint = expr->left->type == EXPR_VALUE ? expr->left->taint : 0;
 473                 *expr = *expr->right;
 474                 expr->flags = flags;
 475                 if (expr->type == EXPR_VALUE)
 476                         expr->taint |= Taint_comma | taint;
 477         }
 478         return cost;
 479 }
 480 
 481 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
 482 
 483 static int compare_types(int op, struct symbol *left, struct symbol *right)
 484 {
 485         struct ctype c1 = {.base_type = left};
 486         struct ctype c2 = {.base_type = right};
 487         switch (op) {
 488         case SPECIAL_EQUAL:
 489                 return !type_difference(&c1, &c2, MOD_IGN, MOD_IGN);
 490         case SPECIAL_NOTEQUAL:
 491                 return type_difference(&c1, &c2, MOD_IGN, MOD_IGN) != NULL;
 492         case '<':
 493                 return left->bit_size < right->bit_size;
 494         case '>':
 495                 return left->bit_size > right->bit_size;
 496         case SPECIAL_LTE:
 497                 return left->bit_size <= right->bit_size;
 498         case SPECIAL_GTE:
 499                 return left->bit_size >= right->bit_size;
 500         }
 501         return 0;


 512         if (left && right) {
 513                 /* Type comparison? */
 514                 if (left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
 515                         int op = expr->op;
 516                         expr->type = EXPR_VALUE;
 517                         expr->value = compare_types(op, left->symbol, right->symbol);
 518                         expr->taint = 0;
 519                         return 0;
 520                 }
 521                 if (simplify_cmp_binop(expr, left->ctype))
 522                         return 0;
 523                 if (simplify_float_cmp(expr, left->ctype))
 524                         return 0;
 525         }
 526         return cost + 1;
 527 }
 528 
 529 static int expand_conditional(struct expression *expr)
 530 {
 531         struct expression *cond = expr->conditional;
 532         struct expression *true = expr->cond_true;
 533         struct expression *false = expr->cond_false;
 534         int cost, cond_cost;
 535 
 536         cond_cost = expand_expression(cond);
 537         if (cond->type == EXPR_VALUE) {
 538                 unsigned flags = expr->flags;
 539                 if (!cond->value)
 540                         true = false;
 541                 if (!true)
 542                         true = cond;
 543                 cost = expand_expression(true);
 544                 *expr = *true;
 545                 expr->flags = flags;
 546                 if (expr->type == EXPR_VALUE)
 547                         expr->taint |= cond->taint;
 548                 return cost;
 549         }
 550 
 551         cost = expand_expression(true);
 552         cost += expand_expression(false);
 553 
 554         if (cost < SELECT_COST) {
 555                 expr->type = EXPR_SELECT;
 556                 cost -= BRANCH_COST - 1;
 557         }
 558 
 559         return cost + cond_cost + BRANCH_COST;
 560 }
 561                 
















 562 static int expand_assignment(struct expression *expr)
 563 {
 564         expand_expression(expr->left);
 565         expand_expression(expr->right);



 566         return SIDE_EFFECTS;
 567 }
 568 
 569 static int expand_addressof(struct expression *expr)
 570 {
 571         return expand_expression(expr->unop);
 572 }
 573 
 574 /*
 575  * Look up a trustable initializer value at the requested offset.
 576  *
 577  * Return NULL if no such value can be found or statically trusted.
 578  *
 579  * FIXME!! We should check that the size is right!
 580  */
 581 static struct expression *constant_symbol_value(struct symbol *sym, int offset)
 582 {
 583         struct expression *value;
 584 
 585         if (sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))
 586                 return NULL;
 587         value = sym->initializer;
 588         if (!value)
 589                 return NULL;
 590         if (value->type == EXPR_INITIALIZER) {
 591                 struct expression *entry;
 592                 FOR_EACH_PTR(value->expr_list, entry) {
 593                         if (entry->type != EXPR_POS) {
 594                                 if (offset)
 595                                         continue;
 596                                 return entry;
 597                         }
 598                         if (entry->init_offset < offset)
 599                                 continue;
 600                         if (entry->init_offset > offset)
 601                                 return NULL;
 602                         return entry->init_expr;
 603                 } END_FOR_EACH_PTR(entry);
 604                 return NULL;
 605         }


 627         /*
 628          * Is it "symbol" or "symbol + offset"?
 629          */
 630         offset = 0;
 631         if (unop->type == EXPR_BINOP && unop->op == '+') {
 632                 struct expression *right = unop->right;
 633                 if (right->type == EXPR_VALUE) {
 634                         offset = right->value;
 635                         unop = unop->left;
 636                 }
 637         }
 638 
 639         if (unop->type == EXPR_SYMBOL) {
 640                 struct symbol *sym = unop->symbol;
 641                 struct expression *value = constant_symbol_value(sym, offset);
 642 
 643                 /* Const symbol with a constant initializer? */
 644                 if (value) {
 645                         /* FIXME! We should check that the size is right! */
 646                         if (value->type == EXPR_VALUE) {


 647                                 expr->type = EXPR_VALUE;
 648                                 expr->value = value->value;
 649                                 expr->taint = 0;
 650                                 return 0;
 651                         } else if (value->type == EXPR_FVALUE) {
 652                                 expr->type = EXPR_FVALUE;
 653                                 expr->fvalue = value->fvalue;
 654                                 return 0;
 655                         }
 656                 }
 657 
 658                 /* Direct symbol dereference? Cheap and safe */
 659                 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
 660         }
 661 
 662         return UNSAFE;
 663 }
 664 
 665 static int simplify_preop(struct expression *expr)
 666 {


 771 
 772         cost = expand_expression(target);
 773 
 774         /* Simplify normal integer casts.. */
 775         if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
 776                 cast_value(expr, expr->ctype, target, target->ctype);
 777                 return 0;
 778         }
 779         return cost + 1;
 780 }
 781 
 782 /*
 783  * expand a call expression with a symbol. This
 784  * should expand builtins.
 785  */
 786 static int expand_symbol_call(struct expression *expr, int cost)
 787 {
 788         struct expression *fn = expr->fn;
 789         struct symbol *ctype = fn->ctype;
 790 


 791         if (fn->type != EXPR_PREOP)
 792                 return SIDE_EFFECTS;
 793 
 794         if (ctype->op && ctype->op->expand)
 795                 return ctype->op->expand(expr, cost);
 796 
 797         if (ctype->ctype.modifiers & MOD_PURE)
 798                 return cost + 1;
 799 
 800         return SIDE_EFFECTS;
 801 }
 802 
 803 static int expand_call(struct expression *expr)
 804 {
 805         int cost;
 806         struct symbol *sym;
 807         struct expression *fn = expr->fn;
 808 
 809         cost = expand_arguments(expr->args);
 810         sym = fn->ctype;


1031                 return expand_expression_list(expr->expr_list);
1032 
1033         case EXPR_IDENTIFIER:
1034                 return UNSAFE;
1035 
1036         case EXPR_INDEX:
1037                 return UNSAFE;
1038 
1039         case EXPR_SLICE:
1040                 return expand_expression(expr->base) + 1;
1041 
1042         case EXPR_POS:
1043                 return expand_pos_expression(expr);
1044 
1045         case EXPR_SIZEOF:
1046         case EXPR_PTRSIZEOF:
1047         case EXPR_ALIGNOF:
1048         case EXPR_OFFSETOF:
1049                 expression_error(expr, "internal front-end error: sizeof in expansion?");
1050                 return UNSAFE;



1051         }
1052         return SIDE_EFFECTS;
1053 }
1054 
1055 static void expand_const_expression(struct expression *expr, const char *where)
1056 {
1057         if (expr) {
1058                 expand_expression(expr);
1059                 if (expr->type != EXPR_VALUE)
1060                         expression_error(expr, "Expected constant expression in %s", where);
1061         }
1062 }
1063 
1064 int expand_symbol(struct symbol *sym)
1065 {
1066         int retval;
1067         struct symbol *base_type;
1068 
1069         if (!sym)
1070                 return 0;


1231 }
1232 
1233 static long long __get_expression_value(struct expression *expr, int strict)
1234 {
1235         long long value, mask;
1236         struct symbol *ctype;
1237 
1238         if (!expr)
1239                 return 0;
1240         ctype = evaluate_expression(expr);
1241         if (!ctype) {
1242                 expression_error(expr, "bad constant expression type");
1243                 return 0;
1244         }
1245         expand_expression(expr);
1246         if (expr->type != EXPR_VALUE) {
1247                 if (strict != 2)
1248                         expression_error(expr, "bad constant expression");
1249                 return 0;
1250         }

1251         if ((strict == 1) && bad_integer_constant_expression(expr)) {
1252                 expression_error(expr, "bad integer constant expression");
1253                 return 0;
1254         }

1255 
1256         value = expr->value;
1257         mask = 1ULL << (ctype->bit_size-1);
1258 
1259         if (value & mask) {
1260                 while (ctype->type != SYM_BASETYPE)
1261                         ctype = ctype->ctype.base_type;
1262                 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1263                         value = value | mask | ~(mask-1);
1264         }
1265         return value;
1266 }
1267 
1268 long long get_expression_value(struct expression *expr)
1269 {
1270         return __get_expression_value(expr, 0);
1271 }
1272 
1273 long long const_expression_value(struct expression *expr)
1274 {




  24  *
  25  * expand constant expressions.
  26  */
  27 #include <stdlib.h>
  28 #include <stdarg.h>
  29 #include <stddef.h>
  30 #include <stdio.h>
  31 #include <string.h>
  32 #include <ctype.h>
  33 #include <unistd.h>
  34 #include <fcntl.h>
  35 #include <limits.h>
  36 
  37 #include "lib.h"
  38 #include "allocate.h"
  39 #include "parse.h"
  40 #include "token.h"
  41 #include "symbol.h"
  42 #include "target.h"
  43 #include "expression.h"
  44 #include "evaluate.h"
  45 #include "expand.h"
  46 
  47 
  48 static int expand_expression(struct expression *);
  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()).
  55 static int conservative;
  56 
  57 static int expand_symbol_expression(struct expression *expr)
  58 {
  59         struct symbol *sym = expr->symbol;
  60 
  61         if (sym == &zero_int) {
  62                 if (Wundef)
  63                         warning(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
  64                 expr->type = EXPR_VALUE;
  65                 expr->value = 0;
  66                 expr->taint = 0;
  67                 return 0;
  68         }
  69         /* The cost of a symbol expression is lower for on-stack symbols */
  70         return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
  71 }
  72 
  73 static long long get_longlong(struct expression *expr)
  74 {


 146                 expr->type = EXPR_VALUE;
 147                 expr->taint = 0;
 148                 goto Int;
 149         }
 150 
 151         if (!is_float_type(oldtype))
 152                 expr->fvalue = (long double)get_longlong(old);
 153         else
 154                 expr->fvalue = old->fvalue;
 155 
 156         if (!(newtype->ctype.modifiers & MOD_LONGLONG) && \
 157             !(newtype->ctype.modifiers & MOD_LONGLONGLONG)) {
 158                 if ((newtype->ctype.modifiers & MOD_LONG))
 159                         expr->fvalue = (double)expr->fvalue;
 160                 else
 161                         expr->fvalue = (float)expr->fvalue;
 162         }
 163         expr->type = EXPR_FVALUE;
 164 }
 165 
 166 static void warn_shift_count(struct expression *expr, struct symbol *ctype, long long count)
 167 {
 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));
 180 }
 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 
 195 /*
 196  * CAREFUL! We need to get the size and sign of the
 197  * result right!
 198  */
 199 #define CONVERT(op,s)   (((op)<<1)+(s))
 200 #define SIGNED(op)      CONVERT(op, 1)
 201 #define UNSIGNED(op)    CONVERT(op, 0)
 202 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
 203 {
 204         struct expression *left = expr->left, *right = expr->right;
 205         unsigned long long v, l, r, mask;
 206         signed long long sl, sr;
 207         int is_signed;
 208 
 209         if (right->type != EXPR_VALUE)
 210                 return 0;
 211         r = right->value;
 212         if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) {
 213                 if (!check_shift_count(expr, right))

 214                         return 0;


 215         }

 216         if (left->type != EXPR_VALUE)
 217                 return 0;
 218         l = left->value; r = right->value;
 219         is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
 220         mask = 1ULL << (ctype->bit_size-1);
 221         sl = l; sr = r;
 222         if (is_signed && (sl & mask))
 223                 sl |= ~(mask-1);
 224         if (is_signed && (sr & mask))
 225                 sr |= ~(mask-1);
 226         
 227         switch (CONVERT(expr->op,is_signed)) {
 228         case SIGNED('+'):
 229         case UNSIGNED('+'):
 230                 v = l + r;
 231                 break;
 232 
 233         case SIGNED('-'):
 234         case UNSIGNED('-'):
 235                 v = l - r;


 485 }
 486 
 487 static int expand_comma(struct expression *expr)
 488 {
 489         int cost;
 490 
 491         cost = expand_expression(expr->left);
 492         cost += expand_expression(expr->right);
 493         if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE) {
 494                 unsigned flags = expr->flags;
 495                 unsigned taint;
 496                 taint = expr->left->type == EXPR_VALUE ? expr->left->taint : 0;
 497                 *expr = *expr->right;
 498                 expr->flags = flags;
 499                 if (expr->type == EXPR_VALUE)
 500                         expr->taint |= Taint_comma | taint;
 501         }
 502         return cost;
 503 }
 504 
 505 #define MOD_IGN (MOD_QUALIFIER)
 506 
 507 static int compare_types(int op, struct symbol *left, struct symbol *right)
 508 {
 509         struct ctype c1 = {.base_type = left};
 510         struct ctype c2 = {.base_type = right};
 511         switch (op) {
 512         case SPECIAL_EQUAL:
 513                 return !type_difference(&c1, &c2, MOD_IGN, MOD_IGN);
 514         case SPECIAL_NOTEQUAL:
 515                 return type_difference(&c1, &c2, MOD_IGN, MOD_IGN) != NULL;
 516         case '<':
 517                 return left->bit_size < right->bit_size;
 518         case '>':
 519                 return left->bit_size > right->bit_size;
 520         case SPECIAL_LTE:
 521                 return left->bit_size <= right->bit_size;
 522         case SPECIAL_GTE:
 523                 return left->bit_size >= right->bit_size;
 524         }
 525         return 0;


 536         if (left && right) {
 537                 /* Type comparison? */
 538                 if (left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
 539                         int op = expr->op;
 540                         expr->type = EXPR_VALUE;
 541                         expr->value = compare_types(op, left->symbol, right->symbol);
 542                         expr->taint = 0;
 543                         return 0;
 544                 }
 545                 if (simplify_cmp_binop(expr, left->ctype))
 546                         return 0;
 547                 if (simplify_float_cmp(expr, left->ctype))
 548                         return 0;
 549         }
 550         return cost + 1;
 551 }
 552 
 553 static int expand_conditional(struct expression *expr)
 554 {
 555         struct expression *cond = expr->conditional;
 556         struct expression *valt = expr->cond_true;
 557         struct expression *valf = expr->cond_false;
 558         int cost, cond_cost;
 559 
 560         cond_cost = expand_expression(cond);
 561         if (cond->type == EXPR_VALUE) {
 562                 unsigned flags = expr->flags;
 563                 if (!cond->value)
 564                         valt = valf;
 565                 if (!valt)
 566                         valt = cond;
 567                 cost = expand_expression(valt);
 568                 *expr = *valt;
 569                 expr->flags = flags;
 570                 if (expr->type == EXPR_VALUE)
 571                         expr->taint |= cond->taint;
 572                 return cost;
 573         }
 574 
 575         cost = expand_expression(valt);
 576         cost += expand_expression(valf);
 577 
 578         if (cost < SELECT_COST) {
 579                 expr->type = EXPR_SELECT;
 580                 cost -= BRANCH_COST - 1;
 581         }
 582 
 583         return cost + cond_cost + BRANCH_COST;
 584 }
 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 
 602 static int expand_assignment(struct expression *expr)
 603 {
 604         expand_expression(expr->left);
 605         expand_expression(expr->right);
 606 
 607         if (!conservative)
 608                 check_assignment(expr);
 609         return SIDE_EFFECTS;
 610 }
 611 
 612 static int expand_addressof(struct expression *expr)
 613 {
 614         return expand_expression(expr->unop);
 615 }
 616 
 617 /*
 618  * Look up a trustable initializer value at the requested offset.
 619  *
 620  * Return NULL if no such value can be found or statically trusted.
 621  *
 622  * FIXME!! We should check that the size is right!
 623  */
 624 static struct expression *constant_symbol_value(struct symbol *sym, int offset)
 625 {
 626         struct expression *value;
 627 
 628         if (sym->ctype.modifiers & MOD_ACCESS)
 629                 return NULL;
 630         value = sym->initializer;
 631         if (!value)
 632                 return NULL;
 633         if (value->type == EXPR_INITIALIZER) {
 634                 struct expression *entry;
 635                 FOR_EACH_PTR(value->expr_list, entry) {
 636                         if (entry->type != EXPR_POS) {
 637                                 if (offset)
 638                                         continue;
 639                                 return entry;
 640                         }
 641                         if (entry->init_offset < offset)
 642                                 continue;
 643                         if (entry->init_offset > offset)
 644                                 return NULL;
 645                         return entry->init_expr;
 646                 } END_FOR_EACH_PTR(entry);
 647                 return NULL;
 648         }


 670         /*
 671          * Is it "symbol" or "symbol + offset"?
 672          */
 673         offset = 0;
 674         if (unop->type == EXPR_BINOP && unop->op == '+') {
 675                 struct expression *right = unop->right;
 676                 if (right->type == EXPR_VALUE) {
 677                         offset = right->value;
 678                         unop = unop->left;
 679                 }
 680         }
 681 
 682         if (unop->type == EXPR_SYMBOL) {
 683                 struct symbol *sym = unop->symbol;
 684                 struct expression *value = constant_symbol_value(sym, offset);
 685 
 686                 /* Const symbol with a constant initializer? */
 687                 if (value) {
 688                         /* FIXME! We should check that the size is right! */
 689                         if (value->type == EXPR_VALUE) {
 690                                 if (is_bitfield_type(value->ctype))
 691                                         return UNSAFE;
 692                                 expr->type = EXPR_VALUE;
 693                                 expr->value = value->value;
 694                                 expr->taint = 0;
 695                                 return 0;
 696                         } else if (value->type == EXPR_FVALUE) {
 697                                 expr->type = EXPR_FVALUE;
 698                                 expr->fvalue = value->fvalue;
 699                                 return 0;
 700                         }
 701                 }
 702 
 703                 /* Direct symbol dereference? Cheap and safe */
 704                 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
 705         }
 706 
 707         return UNSAFE;
 708 }
 709 
 710 static int simplify_preop(struct expression *expr)
 711 {


 816 
 817         cost = expand_expression(target);
 818 
 819         /* Simplify normal integer casts.. */
 820         if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
 821                 cast_value(expr, expr->ctype, target, target->ctype);
 822                 return 0;
 823         }
 824         return cost + 1;
 825 }
 826 
 827 /*
 828  * expand a call expression with a symbol. This
 829  * should expand builtins.
 830  */
 831 static int expand_symbol_call(struct expression *expr, int cost)
 832 {
 833         struct expression *fn = expr->fn;
 834         struct symbol *ctype = fn->ctype;
 835 
 836         expand_expression(fn);
 837 
 838         if (fn->type != EXPR_PREOP)
 839                 return SIDE_EFFECTS;
 840 
 841         if (ctype->op && ctype->op->expand)
 842                 return ctype->op->expand(expr, cost);
 843 
 844         if (ctype->ctype.modifiers & MOD_PURE)
 845                 return cost + 1;
 846 
 847         return SIDE_EFFECTS;
 848 }
 849 
 850 static int expand_call(struct expression *expr)
 851 {
 852         int cost;
 853         struct symbol *sym;
 854         struct expression *fn = expr->fn;
 855 
 856         cost = expand_arguments(expr->args);
 857         sym = fn->ctype;


1078                 return expand_expression_list(expr->expr_list);
1079 
1080         case EXPR_IDENTIFIER:
1081                 return UNSAFE;
1082 
1083         case EXPR_INDEX:
1084                 return UNSAFE;
1085 
1086         case EXPR_SLICE:
1087                 return expand_expression(expr->base) + 1;
1088 
1089         case EXPR_POS:
1090                 return expand_pos_expression(expr);
1091 
1092         case EXPR_SIZEOF:
1093         case EXPR_PTRSIZEOF:
1094         case EXPR_ALIGNOF:
1095         case EXPR_OFFSETOF:
1096                 expression_error(expr, "internal front-end error: sizeof in expansion?");
1097                 return UNSAFE;
1098         case EXPR_ASM_OPERAND:
1099                 expression_error(expr, "internal front-end error: ASM_OPERAND in expansion?");
1100                 return UNSAFE;
1101         }
1102         return SIDE_EFFECTS;
1103 }
1104 
1105 static void expand_const_expression(struct expression *expr, const char *where)
1106 {
1107         if (expr) {
1108                 expand_expression(expr);
1109                 if (expr->type != EXPR_VALUE)
1110                         expression_error(expr, "Expected constant expression in %s", where);
1111         }
1112 }
1113 
1114 int expand_symbol(struct symbol *sym)
1115 {
1116         int retval;
1117         struct symbol *base_type;
1118 
1119         if (!sym)
1120                 return 0;


1281 }
1282 
1283 static long long __get_expression_value(struct expression *expr, int strict)
1284 {
1285         long long value, mask;
1286         struct symbol *ctype;
1287 
1288         if (!expr)
1289                 return 0;
1290         ctype = evaluate_expression(expr);
1291         if (!ctype) {
1292                 expression_error(expr, "bad constant expression type");
1293                 return 0;
1294         }
1295         expand_expression(expr);
1296         if (expr->type != EXPR_VALUE) {
1297                 if (strict != 2)
1298                         expression_error(expr, "bad constant expression");
1299                 return 0;
1300         }
1301 #if 0   // This complains about "1 ? 1 :__bits_per()" which the kernel use
1302         if ((strict == 1) && bad_integer_constant_expression(expr)) {
1303                 expression_error(expr, "bad integer constant expression");
1304                 return 0;
1305         }
1306 #endif
1307 
1308         value = expr->value;
1309         mask = 1ULL << (ctype->bit_size-1);
1310 
1311         if (value & mask) {
1312                 while (ctype->type != SYM_BASETYPE)
1313                         ctype = ctype->ctype.base_type;
1314                 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1315                         value = value | mask | ~(mask-1);
1316         }
1317         return value;
1318 }
1319 
1320 long long get_expression_value(struct expression *expr)
1321 {
1322         return __get_expression_value(expr, 0);
1323 }
1324 
1325 long long const_expression_value(struct expression *expr)
1326 {