Print this page
new smatch


  55                 [SYM_PTR] = "ptr.",
  56                 [SYM_FN] = "fn..",
  57                 [SYM_ARRAY] = "arry",
  58                 [SYM_STRUCT] = "strt",
  59                 [SYM_UNION] = "unin",
  60                 [SYM_ENUM] = "enum",
  61                 [SYM_TYPEDEF] = "tdef",
  62                 [SYM_TYPEOF] = "tpof",
  63                 [SYM_MEMBER] = "memb",
  64                 [SYM_BITFIELD] = "bitf",
  65                 [SYM_LABEL] = "labl",
  66                 [SYM_RESTRICT] = "rstr",
  67                 [SYM_FOULED] = "foul",
  68                 [SYM_BAD] = "bad.",
  69         };
  70         struct context *context;
  71         int i;
  72 
  73         if (!sym)
  74                 return;
  75         fprintf(stderr, "%.*s%s%3d:%lu %s %s (as: %d) %p (%s:%d:%d) %s\n",
  76                 indent, indent_string, typestr[sym->type],
  77                 sym->bit_size, sym->ctype.alignment,
  78                 modifier_string(sym->ctype.modifiers), show_ident(sym->ident), sym->ctype.as,

  79                 sym, stream_name(sym->pos.stream), sym->pos.line, sym->pos.pos,
  80                 builtin_typename(sym) ?: "");
  81         i = 0;
  82         FOR_EACH_PTR(sym->ctype.contexts, context) {
  83                 /* FIXME: should print context expression */
  84                 fprintf(stderr, "< context%d: in=%d, out=%d\n",
  85                         i, context->in, context->out);
  86                 fprintf(stderr, "  end context%d >\n", i);
  87                 i++;
  88         } END_FOR_EACH_PTR(context);
  89         if (sym->type == SYM_FN) {
  90                 struct symbol *arg;
  91                 i = 0;
  92                 FOR_EACH_PTR(sym->arguments, arg) {
  93                         fprintf(stderr, "< arg%d:\n", i);
  94                         do_debug_symbol(arg, 0);
  95                         fprintf(stderr, "  end arg%d >\n", i);
  96                         i++;
  97                 } END_FOR_EACH_PTR(arg);
  98         }


 108  * Symbol type printout. The type system is by far the most
 109  * complicated part of C - everything else is trivial.
 110  */
 111 const char *modifier_string(unsigned long mod)
 112 {
 113         static char buffer[100];
 114         int len = 0;
 115         int i;
 116         struct mod_name {
 117                 unsigned long mod;
 118                 const char *name;
 119         } *m;
 120 
 121         static struct mod_name mod_names[] = {
 122                 {MOD_AUTO,              "auto"},
 123                 {MOD_REGISTER,          "register"},
 124                 {MOD_STATIC,            "static"},
 125                 {MOD_EXTERN,            "extern"},
 126                 {MOD_CONST,             "const"},
 127                 {MOD_VOLATILE,          "volatile"},


 128                 {MOD_SIGNED,            "[signed]"},
 129                 {MOD_UNSIGNED,          "[unsigned]"},
 130                 {MOD_CHAR,              "[char]"},
 131                 {MOD_SHORT,             "[short]"},
 132                 {MOD_LONG,              "[long]"},
 133                 {MOD_LONGLONG,          "[long long]"},
 134                 {MOD_LONGLONGLONG,      "[long long long]"},
 135                 {MOD_TYPEDEF,           "[typedef]"},
 136                 {MOD_TLS,               "[tls]"},
 137                 {MOD_INLINE,            "inline"},
 138                 {MOD_ADDRESSABLE,       "[addressable]"},
 139                 {MOD_NOCAST,            "[nocast]"},
 140                 {MOD_NODEREF,           "[noderef]"},
 141                 {MOD_ACCESSED,          "[accessed]"},
 142                 {MOD_TOPLEVEL,          "[toplevel]"},
 143                 {MOD_ASSIGNED,          "[assigned]"},
 144                 {MOD_TYPE,              "[type]"},
 145                 {MOD_SAFE,              "[safe]"},
 146                 {MOD_USERTYPE,          "[usertype]"},
 147                 {MOD_NORETURN,          "[noreturn]"},
 148                 {MOD_EXPLICITLY_SIGNED, "[explicitly-signed]"},
 149                 {MOD_BITWISE,           "[bitwise]"},
 150                 {MOD_PURE,              "[pure]"},
 151         };
 152 
 153         for (i = 0; i < ARRAY_SIZE(mod_names); i++) {
 154                 m = mod_names + i;
 155                 if (mod & m->mod) {
 156                         char c;
 157                         const char *name = m->name;
 158                         while ((c = *name++) != '\0' && len + 2 < sizeof buffer)
 159                                 buffer[len++] = c;
 160                         buffer[len++] = ' ';
 161                 }


 165 }
 166 
 167 static void show_struct_member(struct symbol *sym)
 168 {
 169         printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
 170         printf("\n");
 171 }
 172 
 173 void show_symbol_list(struct symbol_list *list, const char *sep)
 174 {
 175         struct symbol *sym;
 176         const char *prepend = "";
 177 
 178         FOR_EACH_PTR(list, sym) {
 179                 puts(prepend);
 180                 prepend = ", ";
 181                 show_symbol(sym);
 182         } END_FOR_EACH_PTR(sym);
 183 }
 184 







 185 struct type_name {
 186         char *start;
 187         char *end;
 188 };
 189 
 190 static void FORMAT_ATTR(2) prepend(struct type_name *name, const char *fmt, ...)
 191 {
 192         static char buffer[512];
 193         int n;
 194 
 195         va_list args;
 196         va_start(args, fmt);
 197         n = vsprintf(buffer, fmt, args);
 198         va_end(args);
 199 
 200         name->start -= n;
 201         memcpy(name->start, buffer, n);
 202 }
 203 
 204 static void FORMAT_ATTR(2) append(struct type_name *name, const char *fmt, ...)
 205 {
 206         static char buffer[512];
 207         int n;
 208 
 209         va_list args;
 210         va_start(args, fmt);
 211         n = vsprintf(buffer, fmt, args);
 212         va_end(args);
 213 
 214         memcpy(name->end, buffer, n);
 215         name->end += n;
 216 }
 217 
 218 static struct ctype_name {
 219         struct symbol *sym;
 220         const char *name;

 221 } typenames[] = {
 222         { & char_ctype,  "char" },
 223         { &schar_ctype,  "signed char" },
 224         { &uchar_ctype,  "unsigned char" },
 225         { & short_ctype, "short" },
 226         { &sshort_ctype, "signed short" },
 227         { &ushort_ctype, "unsigned short" },
 228         { & int_ctype,   "int" },
 229         { &sint_ctype,   "signed int" },
 230         { &uint_ctype,   "unsigned int" },
 231         { &slong_ctype,  "signed long" },
 232         { & long_ctype,  "long" },
 233         { &ulong_ctype,  "unsigned long" },
 234         { & llong_ctype, "long long" },
 235         { &sllong_ctype, "signed long long" },
 236         { &ullong_ctype, "unsigned long long" },
 237         { & lllong_ctype, "long long long" },
 238         { &slllong_ctype, "signed long long long" },
 239         { &ulllong_ctype, "unsigned long long long" },
 240 
 241         { &void_ctype,   "void" },
 242         { &bool_ctype,   "bool" },
 243         { &string_ctype, "string" },
 244 
 245         { &float_ctype,  "float" },
 246         { &double_ctype, "double" },
 247         { &ldouble_ctype,"long double" },
 248         { &incomplete_ctype, "incomplete type" },
 249         { &int_type, "abstract int" },
 250         { &fp_type, "abstract fp" },
 251         { &label_ctype, "label type" },
 252         { &bad_ctype, "bad type" },
 253 };
 254 
 255 const char *builtin_typename(struct symbol *sym)
 256 {
 257         int i;
 258 
 259         for (i = 0; i < ARRAY_SIZE(typenames); i++)
 260                 if (typenames[i].sym == sym)
 261                         return typenames[i].name;
 262         return NULL;
 263 }
 264 










 265 const char *builtin_ctypename(struct ctype *ctype)
 266 {
 267         int i;
 268 
 269         for (i = 0; i < ARRAY_SIZE(typenames); i++)
 270                 if (&typenames[i].sym->ctype == ctype)
 271                         return typenames[i].name;
 272         return NULL;
 273 }
 274 
 275 static void do_show_type(struct symbol *sym, struct type_name *name)
 276 {
 277         const char *typename;
 278         unsigned long mod = 0;
 279         int as = 0;
 280         int was_ptr = 0;
 281         int restr = 0;
 282         int fouled = 0;
 283 
 284 deeper:
 285         if (!sym || (sym->type != SYM_NODE && sym->type != SYM_ARRAY &&
 286                      sym->type != SYM_BITFIELD)) {
 287                 const char *s;
 288                 size_t len;
 289 
 290                 if (as)
 291                         prepend(name, "<asn:%d>", as);
 292 


 293                 s = modifier_string(mod);
 294                 len = strlen(s);
 295                 name->start -= len;    
 296                 memcpy(name->start, s, len);  
 297                 mod = 0;
 298                 as = 0;
 299         }
 300 
 301         if (!sym)
 302                 goto out;
 303 
 304         if ((typename = builtin_typename(sym))) {
 305                 int len = strlen(typename);
 306                 if (name->start != name->end)
 307                         *--name->start = ' ';
 308                 name->start -= len;
 309                 memcpy(name->start, typename, len);
 310                 goto out;
 311         }
 312 
 313         /* Prepend */
 314         switch (sym->type) {
 315         case SYM_PTR:
 316                 prepend(name, "*");
 317                 mod = sym->ctype.modifiers;
 318                 as = sym->ctype.as;


 328                 append(name, "( ... )");
 329                 break;
 330 
 331         case SYM_STRUCT:
 332                 if (name->start != name->end)
 333                         *--name->start = ' ';
 334                 prepend(name, "struct %s", show_ident(sym->ident));
 335                 goto out;
 336 
 337         case SYM_UNION:
 338                 if (name->start != name->end)
 339                         *--name->start = ' ';
 340                 prepend(name, "union %s", show_ident(sym->ident));
 341                 goto out;
 342 
 343         case SYM_ENUM:
 344                 prepend(name, "enum %s ", show_ident(sym->ident));
 345                 break;
 346 
 347         case SYM_NODE:

 348                 append(name, "%s", show_ident(sym->ident));
 349                 mod |= sym->ctype.modifiers;
 350                 as |= sym->ctype.as;
 351                 break;
 352 
 353         case SYM_BITFIELD:
 354                 mod |= sym->ctype.modifiers;
 355                 as |= sym->ctype.as;
 356                 append(name, ":%d", sym->bit_size);
 357                 break;
 358 
 359         case SYM_LABEL:
 360                 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
 361                 return;
 362 
 363         case SYM_ARRAY:
 364                 mod |= sym->ctype.modifiers;
 365                 as |= sym->ctype.as;
 366                 if (was_ptr) {
 367                         prepend(name, "( ");
 368                         append(name, " )");
 369                         was_ptr = 0;
 370                 }
 371                 append(name, "[%lld]", get_expression_value(sym->array_size));
 372                 break;
 373 
 374         case SYM_RESTRICT:
 375                 if (!sym->ident) {
 376                         restr = 1;
 377                         break;
 378                 }
 379                 if (name->start != name->end)
 380                         *--name->start = ' ';
 381                 prepend(name, "restricted %s", show_ident(sym->ident));
 382                 goto out;
 383 
 384         case SYM_FOULED:
 385                 fouled = 1;
 386                 break;
 387 
 388         default:
 389                 if (name->start != name->end)
 390                         *--name->start = ' ';
 391                 prepend(name, "unknown type %d", sym->type);
 392                 goto out;
 393         }
 394 
 395         sym = sym->ctype.base_type;
 396         goto deeper;
 397 
 398 out:
 399         if (restr)
 400                 prepend(name, "restricted ");
 401         if (fouled)
 402                 prepend(name, "fouled ");




 403 }
 404 
 405 void show_type(struct symbol *sym)
 406 {
 407         char array[200];
 408         struct type_name name;
 409 
 410         name.start = name.end = array+100;
 411         do_show_type(sym, &name);
 412         *name.end = 0;
 413         printf("%s", name.start);
 414 }
 415 
 416 const char *show_typename(struct symbol *sym)
 417 {
 418         static char array[200];
 419         struct type_name name;
 420 
 421         name.start = name.end = array+100;
 422         do_show_type(sym, &name);


 910         return show_regular_preop(expr);
 911 }
 912 
 913 static int show_postop(struct expression *expr)
 914 {
 915         return show_inc_dec(expr, 1);
 916 }       
 917 
 918 static int show_symbol_expr(struct symbol *sym)
 919 {
 920         int new = new_pseudo();
 921 
 922         if (sym->initializer && sym->initializer->type == EXPR_STRING)
 923                 return show_string_expr(sym->initializer);
 924 
 925         if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
 926                 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
 927                 return new;
 928         }
 929         if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
 930                 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, sym->value);
 931                 return new;
 932         }
 933         printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
 934         return new;
 935 }
 936 
 937 static int show_symbol_init(struct symbol *sym)
 938 {
 939         struct expression *expr = sym->initializer;
 940 
 941         if (expr) {
 942                 int val, addr, bits;
 943 
 944                 bits = expr->ctype->bit_size;
 945                 val = show_expression(expr);
 946                 addr = show_symbol_expr(sym);
 947                 show_store_gen(bits, val, NULL, addr);
 948         }
 949         return 0;
 950 }
 951 
 952 static int type_is_signed(struct symbol *sym)
 953 {
 954         if (sym->type == SYM_NODE)
 955                 sym = sym->ctype.base_type;
 956         if (sym->type == SYM_PTR)
 957                 return 0;
 958         return !(sym->ctype.modifiers & MOD_UNSIGNED);
 959 }
 960 
 961 static int show_cast_expr(struct expression *expr)
 962 {
 963         struct symbol *old_type, *new_type;
 964         int op = show_expression(expr->cast_expression);
 965         int oldbits, newbits;
 966         int new, is_signed;
 967 
 968         old_type = expr->cast_expression->ctype;
 969         new_type = expr->cast_type;
 970         
 971         oldbits = old_type->bit_size;
 972         newbits = new_type->bit_size;
 973         if (oldbits >= newbits)
 974                 return op;
 975         new = new_pseudo();
 976         is_signed = type_is_signed(old_type);
 977         if (is_signed) {
 978                 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
 979         } else {
 980                 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
 981         }
 982         return new;
 983 }
 984 
 985 static int show_value(struct expression *expr)
 986 {
 987         int new = new_pseudo();
 988         unsigned long long value = expr->value;
 989 
 990         printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
 991         return new;
 992 }
 993 
 994 static int show_fvalue(struct expression *expr)
 995 {
 996         int new = new_pseudo();
 997         long double value = expr->fvalue;
 998 
 999         printf("\tmovf.%d\t\tv%d,$%Lf\n", expr->ctype->bit_size, new, value);
1000         return new;
1001 }
1002 
1003 static int show_string_expr(struct expression *expr)
1004 {
1005         int new = new_pseudo();
1006 
1007         printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
1008         return new;
1009 }
1010 
1011 static int show_label_expr(struct expression *expr)
1012 {
1013         int new = new_pseudo();
1014         printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
1015         return new;
1016 }
1017 
1018 static int show_conditional_expr(struct expression *expr)
1019 {
1020         int cond = show_expression(expr->conditional);
1021         int true = show_expression(expr->cond_true);
1022         int false = show_expression(expr->cond_false);
1023         int new = new_pseudo();
1024 
1025         printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, true, false);
1026         return new;
1027 }
1028 
1029 static int show_statement_expr(struct expression *expr)
1030 {
1031         return show_statement(expr->statement);
1032 }
1033 
1034 static int show_position_expr(struct expression *expr, struct symbol *base)
1035 {
1036         int new = show_expression(expr->init_expr);
1037         struct symbol *ctype = expr->init_expr->ctype;
1038         int bit_offset;
1039 
1040         bit_offset = ctype ? ctype->bit_offset : -1;
1041 
1042         printf("\tinsert v%d at [%d:%d] of %s\n", new,
1043                 expr->init_offset, bit_offset,
1044                 show_ident(base->ident));
1045         return 0;


1152                 return show_statement_expr(expr);
1153         case EXPR_LABEL:
1154                 return show_label_expr(expr);
1155         case EXPR_SLICE:
1156                 return show_slice(expr);
1157 
1158         // None of these should exist as direct expressions: they are only
1159         // valid as sub-expressions of initializers.
1160         case EXPR_POS:
1161                 warning(expr->pos, "unable to show plain initializer position expression");
1162                 return 0;
1163         case EXPR_IDENTIFIER:
1164                 warning(expr->pos, "unable to show identifier expression");
1165                 return 0;
1166         case EXPR_INDEX:
1167                 warning(expr->pos, "unable to show index expression");
1168                 return 0;
1169         case EXPR_TYPE:
1170                 warning(expr->pos, "unable to show type expression");
1171                 return 0;



1172         }
1173         return 0;
1174 }


  55                 [SYM_PTR] = "ptr.",
  56                 [SYM_FN] = "fn..",
  57                 [SYM_ARRAY] = "arry",
  58                 [SYM_STRUCT] = "strt",
  59                 [SYM_UNION] = "unin",
  60                 [SYM_ENUM] = "enum",
  61                 [SYM_TYPEDEF] = "tdef",
  62                 [SYM_TYPEOF] = "tpof",
  63                 [SYM_MEMBER] = "memb",
  64                 [SYM_BITFIELD] = "bitf",
  65                 [SYM_LABEL] = "labl",
  66                 [SYM_RESTRICT] = "rstr",
  67                 [SYM_FOULED] = "foul",
  68                 [SYM_BAD] = "bad.",
  69         };
  70         struct context *context;
  71         int i;
  72 
  73         if (!sym)
  74                 return;
  75         fprintf(stderr, "%.*s%s%3d:%lu %s %s (as: %s) %p (%s:%d:%d) %s\n",
  76                 indent, indent_string, typestr[sym->type],
  77                 sym->bit_size, sym->ctype.alignment,
  78                 modifier_string(sym->ctype.modifiers), show_ident(sym->ident),
  79                 show_as(sym->ctype.as),
  80                 sym, stream_name(sym->pos.stream), sym->pos.line, sym->pos.pos,
  81                 builtin_typename(sym) ?: "");
  82         i = 0;
  83         FOR_EACH_PTR(sym->ctype.contexts, context) {
  84                 /* FIXME: should print context expression */
  85                 fprintf(stderr, "< context%d: in=%d, out=%d\n",
  86                         i, context->in, context->out);
  87                 fprintf(stderr, "  end context%d >\n", i);
  88                 i++;
  89         } END_FOR_EACH_PTR(context);
  90         if (sym->type == SYM_FN) {
  91                 struct symbol *arg;
  92                 i = 0;
  93                 FOR_EACH_PTR(sym->arguments, arg) {
  94                         fprintf(stderr, "< arg%d:\n", i);
  95                         do_debug_symbol(arg, 0);
  96                         fprintf(stderr, "  end arg%d >\n", i);
  97                         i++;
  98                 } END_FOR_EACH_PTR(arg);
  99         }


 109  * Symbol type printout. The type system is by far the most
 110  * complicated part of C - everything else is trivial.
 111  */
 112 const char *modifier_string(unsigned long mod)
 113 {
 114         static char buffer[100];
 115         int len = 0;
 116         int i;
 117         struct mod_name {
 118                 unsigned long mod;
 119                 const char *name;
 120         } *m;
 121 
 122         static struct mod_name mod_names[] = {
 123                 {MOD_AUTO,              "auto"},
 124                 {MOD_REGISTER,          "register"},
 125                 {MOD_STATIC,            "static"},
 126                 {MOD_EXTERN,            "extern"},
 127                 {MOD_CONST,             "const"},
 128                 {MOD_VOLATILE,          "volatile"},
 129                 {MOD_RESTRICT,          "restrict"},
 130                 {MOD_ATOMIC,            "[atomic]"},
 131                 {MOD_SIGNED,            "[signed]"},
 132                 {MOD_UNSIGNED,          "[unsigned]"},
 133                 {MOD_CHAR,              "[char]"},
 134                 {MOD_SHORT,             "[short]"},
 135                 {MOD_LONG,              "[long]"},
 136                 {MOD_LONGLONG,          "[long long]"},
 137                 {MOD_LONGLONGLONG,      "[long long long]"},

 138                 {MOD_TLS,               "[tls]"},
 139                 {MOD_INLINE,            "inline"},
 140                 {MOD_ADDRESSABLE,       "[addressable]"},
 141                 {MOD_NOCAST,            "[nocast]"},
 142                 {MOD_NODEREF,           "[noderef]"},

 143                 {MOD_TOPLEVEL,          "[toplevel]"},
 144                 {MOD_ASSIGNED,          "[assigned]"},
 145                 {MOD_TYPE,              "[type]"},
 146                 {MOD_SAFE,              "[safe]"},
 147                 {MOD_USERTYPE,          "[usertype]"},
 148                 {MOD_NORETURN,          "[noreturn]"},
 149                 {MOD_EXPLICITLY_SIGNED, "[explicitly-signed]"},
 150                 {MOD_BITWISE,           "[bitwise]"},
 151                 {MOD_PURE,              "[pure]"},
 152         };
 153 
 154         for (i = 0; i < ARRAY_SIZE(mod_names); i++) {
 155                 m = mod_names + i;
 156                 if (mod & m->mod) {
 157                         char c;
 158                         const char *name = m->name;
 159                         while ((c = *name++) != '\0' && len + 2 < sizeof buffer)
 160                                 buffer[len++] = c;
 161                         buffer[len++] = ' ';
 162                 }


 166 }
 167 
 168 static void show_struct_member(struct symbol *sym)
 169 {
 170         printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
 171         printf("\n");
 172 }
 173 
 174 void show_symbol_list(struct symbol_list *list, const char *sep)
 175 {
 176         struct symbol *sym;
 177         const char *prepend = "";
 178 
 179         FOR_EACH_PTR(list, sym) {
 180                 puts(prepend);
 181                 prepend = ", ";
 182                 show_symbol(sym);
 183         } END_FOR_EACH_PTR(sym);
 184 }
 185 
 186 const char *show_as(struct ident *as)
 187 {
 188         if (!as)
 189                 return "";
 190         return show_ident(as);
 191 }
 192 
 193 struct type_name {
 194         char *start;
 195         char *end;
 196 };
 197 
 198 static void FORMAT_ATTR(2) prepend(struct type_name *name, const char *fmt, ...)
 199 {
 200         static char buffer[512];
 201         int n;
 202 
 203         va_list args;
 204         va_start(args, fmt);
 205         n = vsprintf(buffer, fmt, args);
 206         va_end(args);
 207 
 208         name->start -= n;
 209         memcpy(name->start, buffer, n);
 210 }
 211 
 212 static void FORMAT_ATTR(2) append(struct type_name *name, const char *fmt, ...)
 213 {
 214         static char buffer[512];
 215         int n;
 216 
 217         va_list args;
 218         va_start(args, fmt);
 219         n = vsprintf(buffer, fmt, args);
 220         va_end(args);
 221 
 222         memcpy(name->end, buffer, n);
 223         name->end += n;
 224 }
 225 
 226 static struct ctype_name {
 227         struct symbol *sym;
 228         const char *name;
 229         const char *suffix;
 230 } typenames[] = {
 231         { & char_ctype,  "char", "" },
 232         { &schar_ctype,  "signed char", "" },
 233         { &uchar_ctype,  "unsigned char", "" },
 234         { & short_ctype, "short", "" },
 235         { &sshort_ctype, "signed short", "" },
 236         { &ushort_ctype, "unsigned short", "" },
 237         { & int_ctype,   "int", "" },
 238         { &sint_ctype,   "signed int", "" },
 239         { &uint_ctype,   "unsigned int", "U" },
 240         { & long_ctype,  "long", "L" },
 241         { &slong_ctype,  "signed long", "L" },
 242         { &ulong_ctype,  "unsigned long", "UL" },
 243         { & llong_ctype, "long long", "LL" },
 244         { &sllong_ctype, "signed long long", "LL" },
 245         { &ullong_ctype, "unsigned long long", "ULL" },
 246         { & lllong_ctype, "long long long", "LLL" },
 247         { &slllong_ctype, "signed long long long", "LLL" },
 248         { &ulllong_ctype, "unsigned long long long", "ULLL" },
 249 
 250         { &void_ctype,   "void", "" },
 251         { &bool_ctype,   "bool", "" },

 252 
 253         { &float_ctype,  "float", "F" },
 254         { &double_ctype, "double", "" },
 255         { &ldouble_ctype,"long double", "L" },
 256         { &incomplete_ctype, "incomplete type", "" },
 257         { &int_type, "abstract int", "" },
 258         { &fp_type, "abstract fp", "" },
 259         { &label_ctype, "label type", "" },
 260         { &bad_ctype, "bad type", "" },
 261 };
 262 
 263 const char *builtin_typename(struct symbol *sym)
 264 {
 265         int i;
 266 
 267         for (i = 0; i < ARRAY_SIZE(typenames); i++)
 268                 if (typenames[i].sym == sym)
 269                         return typenames[i].name;
 270         return NULL;
 271 }
 272 
 273 const char *builtin_type_suffix(struct symbol *sym)
 274 {
 275         int i;
 276 
 277         for (i = 0; i < ARRAY_SIZE(typenames); i++)
 278                 if (typenames[i].sym == sym)
 279                         return typenames[i].suffix;
 280         return NULL;
 281 }
 282 
 283 const char *builtin_ctypename(struct ctype *ctype)
 284 {
 285         int i;
 286 
 287         for (i = 0; i < ARRAY_SIZE(typenames); i++)
 288                 if (&typenames[i].sym->ctype == ctype)
 289                         return typenames[i].name;
 290         return NULL;
 291 }
 292 
 293 static void do_show_type(struct symbol *sym, struct type_name *name)
 294 {
 295         const char *typename;
 296         unsigned long mod = 0;
 297         struct ident *as = NULL;
 298         int was_ptr = 0;
 299         int restr = 0;
 300         int fouled = 0;
 301 
 302 deeper:
 303         if (!sym || (sym->type != SYM_NODE && sym->type != SYM_ARRAY &&
 304                      sym->type != SYM_BITFIELD)) {
 305                 const char *s;
 306                 size_t len;
 307 
 308                 if (as)
 309                         prepend(name, "%s ", show_as(as));
 310 
 311                 if (sym->type == SYM_BASETYPE || sym->type == SYM_ENUM)
 312                         mod &= ~MOD_SPECIFIER;
 313                 s = modifier_string(mod);
 314                 len = strlen(s);
 315                 name->start -= len;    
 316                 memcpy(name->start, s, len);  
 317                 mod = 0;
 318                 as = NULL;
 319         }
 320 
 321         if (!sym)
 322                 goto out;
 323 
 324         if ((typename = builtin_typename(sym))) {
 325                 int len = strlen(typename);
 326                 if (name->start != name->end)
 327                         *--name->start = ' ';
 328                 name->start -= len;
 329                 memcpy(name->start, typename, len);
 330                 goto out;
 331         }
 332 
 333         /* Prepend */
 334         switch (sym->type) {
 335         case SYM_PTR:
 336                 prepend(name, "*");
 337                 mod = sym->ctype.modifiers;
 338                 as = sym->ctype.as;


 348                 append(name, "( ... )");
 349                 break;
 350 
 351         case SYM_STRUCT:
 352                 if (name->start != name->end)
 353                         *--name->start = ' ';
 354                 prepend(name, "struct %s", show_ident(sym->ident));
 355                 goto out;
 356 
 357         case SYM_UNION:
 358                 if (name->start != name->end)
 359                         *--name->start = ' ';
 360                 prepend(name, "union %s", show_ident(sym->ident));
 361                 goto out;
 362 
 363         case SYM_ENUM:
 364                 prepend(name, "enum %s ", show_ident(sym->ident));
 365                 break;
 366 
 367         case SYM_NODE:
 368                 if (sym->ident)
 369                         append(name, "%s", show_ident(sym->ident));
 370                 mod |= sym->ctype.modifiers;
 371                 combine_address_space(sym->pos, &as, sym->ctype.as);
 372                 break;
 373 
 374         case SYM_BITFIELD:
 375                 mod |= sym->ctype.modifiers;
 376                 combine_address_space(sym->pos, &as, sym->ctype.as);
 377                 append(name, ":%d", sym->bit_size);
 378                 break;
 379 
 380         case SYM_LABEL:
 381                 append(name, "label(%s:%p)", show_ident(sym->ident), sym);
 382                 return;
 383 
 384         case SYM_ARRAY:
 385                 mod |= sym->ctype.modifiers;
 386                 combine_address_space(sym->pos, &as, sym->ctype.as);
 387                 if (was_ptr) {
 388                         prepend(name, "( ");
 389                         append(name, " )");
 390                         was_ptr = 0;
 391                 }
 392                 append(name, "[%lld]", get_expression_value(sym->array_size));
 393                 break;
 394 
 395         case SYM_RESTRICT:
 396                 if (!sym->ident) {
 397                         restr = 1;
 398                         break;
 399                 }
 400                 if (name->start != name->end)
 401                         *--name->start = ' ';
 402                 prepend(name, "restricted %s", show_ident(sym->ident));
 403                 goto out;
 404 
 405         case SYM_FOULED:
 406                 fouled = 1;
 407                 break;
 408 
 409         default:
 410                 if (name->start != name->end)
 411                         *--name->start = ' ';
 412                 prepend(name, "unknown type %d", sym->type);
 413                 goto out;
 414         }
 415 
 416         sym = sym->ctype.base_type;
 417         goto deeper;
 418 
 419 out:
 420         if (restr)
 421                 prepend(name, "restricted ");
 422         if (fouled)
 423                 prepend(name, "fouled ");
 424 
 425         // strip trailing space
 426         if (name->end > name->start && name->end[-1] == ' ')
 427                 name->end--;
 428 }
 429 
 430 void show_type(struct symbol *sym)
 431 {
 432         char array[200];
 433         struct type_name name;
 434 
 435         name.start = name.end = array+100;
 436         do_show_type(sym, &name);
 437         *name.end = 0;
 438         printf("%s", name.start);
 439 }
 440 
 441 const char *show_typename(struct symbol *sym)
 442 {
 443         static char array[200];
 444         struct type_name name;
 445 
 446         name.start = name.end = array+100;
 447         do_show_type(sym, &name);


 935         return show_regular_preop(expr);
 936 }
 937 
 938 static int show_postop(struct expression *expr)
 939 {
 940         return show_inc_dec(expr, 1);
 941 }       
 942 
 943 static int show_symbol_expr(struct symbol *sym)
 944 {
 945         int new = new_pseudo();
 946 
 947         if (sym->initializer && sym->initializer->type == EXPR_STRING)
 948                 return show_string_expr(sym->initializer);
 949 
 950         if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
 951                 printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new, show_ident(sym->ident));
 952                 return new;
 953         }
 954         if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
 955                 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new, 0LL);
 956                 return new;
 957         }
 958         printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new, show_ident(sym->ident), sym);
 959         return new;
 960 }
 961 
 962 static int show_symbol_init(struct symbol *sym)
 963 {
 964         struct expression *expr = sym->initializer;
 965 
 966         if (expr) {
 967                 int val, addr, bits;
 968 
 969                 bits = expr->ctype->bit_size;
 970                 val = show_expression(expr);
 971                 addr = show_symbol_expr(sym);
 972                 show_store_gen(bits, val, NULL, addr);
 973         }
 974         return 0;
 975 }
 976 









 977 static int show_cast_expr(struct expression *expr)
 978 {
 979         struct symbol *old_type, *new_type;
 980         int op = show_expression(expr->cast_expression);
 981         int oldbits, newbits;
 982         int new, is_signed;
 983 
 984         old_type = expr->cast_expression->ctype;
 985         new_type = expr->cast_type;
 986         
 987         oldbits = old_type->bit_size;
 988         newbits = new_type->bit_size;
 989         if (oldbits >= newbits)
 990                 return op;
 991         new = new_pseudo();
 992         is_signed = is_signed_type(old_type);
 993         if (is_signed) {
 994                 printf("\tsext%d.%d\tv%d,v%d\n", oldbits, newbits, new, op);
 995         } else {
 996                 printf("\tandl.%d\t\tv%d,v%d,$%lu\n", newbits, new, op, (1UL << oldbits)-1);
 997         }
 998         return new;
 999 }
1000 
1001 static int show_value(struct expression *expr)
1002 {
1003         int new = new_pseudo();
1004         unsigned long long value = expr->value;
1005 
1006         printf("\tmovi.%d\t\tv%d,$%llu\n", expr->ctype->bit_size, new, value);
1007         return new;
1008 }
1009 
1010 static int show_fvalue(struct expression *expr)
1011 {
1012         int new = new_pseudo();
1013         long double value = expr->fvalue;
1014 
1015         printf("\tmovf.%d\t\tv%d,$%Le\n", expr->ctype->bit_size, new, value);
1016         return new;
1017 }
1018 
1019 static int show_string_expr(struct expression *expr)
1020 {
1021         int new = new_pseudo();
1022 
1023         printf("\tmovi.%d\t\tv%d,&%s\n", bits_in_pointer, new, show_string(expr->string));
1024         return new;
1025 }
1026 
1027 static int show_label_expr(struct expression *expr)
1028 {
1029         int new = new_pseudo();
1030         printf("\tmovi.%d\t\tv%d,.L%p\n",bits_in_pointer, new, expr->label_symbol);
1031         return new;
1032 }
1033 
1034 static int show_conditional_expr(struct expression *expr)
1035 {
1036         int cond = show_expression(expr->conditional);
1037         int valt = show_expression(expr->cond_true);
1038         int valf = show_expression(expr->cond_false);
1039         int new = new_pseudo();
1040 
1041         printf("[v%d]\tcmov.%d\t\tv%d,v%d,v%d\n", cond, expr->ctype->bit_size, new, valt, valf);
1042         return new;
1043 }
1044 
1045 static int show_statement_expr(struct expression *expr)
1046 {
1047         return show_statement(expr->statement);
1048 }
1049 
1050 static int show_position_expr(struct expression *expr, struct symbol *base)
1051 {
1052         int new = show_expression(expr->init_expr);
1053         struct symbol *ctype = expr->init_expr->ctype;
1054         int bit_offset;
1055 
1056         bit_offset = ctype ? ctype->bit_offset : -1;
1057 
1058         printf("\tinsert v%d at [%d:%d] of %s\n", new,
1059                 expr->init_offset, bit_offset,
1060                 show_ident(base->ident));
1061         return 0;


1168                 return show_statement_expr(expr);
1169         case EXPR_LABEL:
1170                 return show_label_expr(expr);
1171         case EXPR_SLICE:
1172                 return show_slice(expr);
1173 
1174         // None of these should exist as direct expressions: they are only
1175         // valid as sub-expressions of initializers.
1176         case EXPR_POS:
1177                 warning(expr->pos, "unable to show plain initializer position expression");
1178                 return 0;
1179         case EXPR_IDENTIFIER:
1180                 warning(expr->pos, "unable to show identifier expression");
1181                 return 0;
1182         case EXPR_INDEX:
1183                 warning(expr->pos, "unable to show index expression");
1184                 return 0;
1185         case EXPR_TYPE:
1186                 warning(expr->pos, "unable to show type expression");
1187                 return 0;
1188         case EXPR_ASM_OPERAND:
1189                 warning(expr->pos, "unable to show asm operand expression");
1190                 return 0;
1191         }
1192         return 0;
1193 }