| @@ -79,6 +79,7 @@ typedef struct SValue { · 78 unmodified lines |
| 79 | 79 | typedef struct Sym { |
| 80 | 80 | int v; /* symbol token */ |
| 81 | 81 | int t; /* associated type */ |
| 82 | + int r; /* associated register */ |
| 82 | 83 | int c; /* associated number */ |
| 83 | 84 | struct Sym *next; /* next related symbol */ |
| 84 | 85 | struct Sym *prev; /* prev symbol in stack */ |
| @@ -165,49 +166,47 @@ int gnu_ext = 1; · 80 unmodified lines |
| 165 | 166 | int tcc_ext = 1; |
| 166 | 167 | |
| 167 | 168 | /* The current value can be: */ |
| 168 | | −#define VT_VALMASK 0x000f |
| 169 | | −#define VT_CONST 0x000a /* constant in vc |
| 169 | +#define VT_VALMASK 0x00ff |
| 170 | +#define VT_CONST 0x00f0 /* constant in vc |
| 170 | 171 | (must be first non register value) */ |
| 171 | | −#define VT_LLOCAL 0x000b /* lvalue, offset on stack */ |
| 172 | | −#define VT_LOCAL 0x000c /* offset on stack */ |
| 173 | | −#define VT_CMP 0x000d /* the value is stored in processor flags (in vc) */ |
| 174 | | −#define VT_JMP 0x000e /* value is the consequence of jmp true */ |
| 175 | | −#define VT_JMPI 0x000f /* value is the consequence of jmp false */ |
| 176 | | −#define VT_LVAL 0x0010 /* var is an lvalue */ |
| 177 | | −#define VT_LVALN -17 /* ~VT_LVAL */ |
| 178 | | −#define VT_FORWARD 0x0020 /* value is forward reference |
| 179 | | − (only used for functions) */ |
| 180 | | −/* storage */ |
| 181 | | −#define VT_EXTERN 0x00000040 /* extern definition */ |
| 182 | | −#define VT_STATIC 0x00000080 /* static variable */ |
| 183 | | −#define VT_TYPEDEF 0x00000100 /* typedef definition */ |
| 172 | +#define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */ |
| 173 | +#define VT_LOCAL 0x00f2 /* offset on stack */ |
| 174 | +#define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */ |
| 175 | +#define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */ |
| 176 | +#define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */ |
| 177 | +#define VT_LVAL 0x0100 /* var is an lvalue */ |
| 178 | +#define VT_FORWARD 0x0200 /* value is forward reference */ |
| 184 | 179 | |
| 185 | 180 | /* types */ |
| 186 | 181 | #define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */ |
| 187 | 182 | |
| 188 | | −#define VT_BTYPE_SHIFT 9 |
| 189 | | −#define VT_INT (0 << VT_BTYPE_SHIFT) /* integer type */ |
| 190 | | −#define VT_BYTE (1 << VT_BTYPE_SHIFT) /* signed byte type */ |
| 191 | | −#define VT_SHORT (2 << VT_BTYPE_SHIFT) /* short type */ |
| 192 | | −#define VT_VOID (3 << VT_BTYPE_SHIFT) /* void type */ |
| 193 | | −#define VT_PTR (4 << VT_BTYPE_SHIFT) /* pointer increment */ |
| 194 | | −#define VT_ENUM (5 << VT_BTYPE_SHIFT) /* enum definition */ |
| 195 | | −#define VT_FUNC (6 << VT_BTYPE_SHIFT) /* function type */ |
| 196 | | −#define VT_STRUCT (7 << VT_BTYPE_SHIFT) /* struct/union definition */ |
| 197 | | −#define VT_FLOAT (8 << VT_BTYPE_SHIFT) /* IEEE float */ |
| 198 | | −#define VT_DOUBLE (9 << VT_BTYPE_SHIFT) /* IEEE double */ |
| 199 | | −#define VT_LDOUBLE (10 << VT_BTYPE_SHIFT) /* IEEE long double */ |
| 200 | | −#define VT_BOOL (11 << VT_BTYPE_SHIFT) /* ISOC99 boolean type */ |
| 201 | | −#define VT_LLONG (12 << VT_BTYPE_SHIFT) /* 64 bit integer */ |
| 202 | | −#define VT_LONG (13 << VT_BTYPE_SHIFT) /* long integer (NEVER |
| 203 | | − USED as type, only |
| 204 | | − during parsing) */ |
| 205 | | −#define VT_BTYPE (0xf << VT_BTYPE_SHIFT) /* mask for basic type */ |
| 206 | | −#define VT_UNSIGNED (0x10 << VT_BTYPE_SHIFT) /* unsigned type */ |
| 207 | | −#define VT_ARRAY (0x20 << VT_BTYPE_SHIFT) /* array type (also has VT_PTR) */ |
| 208 | | −#define VT_BITFIELD (0x40 << VT_BTYPE_SHIFT) /* bitfield modifier */ |
| 209 | | − |
| 210 | | −#define VT_TYPE 0xfffffe00 /* type mask */ |
| 183 | +#define VT_INT 0 /* integer type */ |
| 184 | +#define VT_BYTE 1 /* signed byte type */ |
| 185 | +#define VT_SHORT 2 /* short type */ |
| 186 | +#define VT_VOID 3 /* void type */ |
| 187 | +#define VT_PTR 4 /* pointer increment */ |
| 188 | +#define VT_ENUM 5 /* enum definition */ |
| 189 | +#define VT_FUNC 6 /* function type */ |
| 190 | +#define VT_STRUCT 7 /* struct/union definition */ |
| 191 | +#define VT_FLOAT 8 /* IEEE float */ |
| 192 | +#define VT_DOUBLE 9 /* IEEE double */ |
| 193 | +#define VT_LDOUBLE 10 /* IEEE long double */ |
| 194 | +#define VT_BOOL 11 /* ISOC99 boolean type */ |
| 195 | +#define VT_LLONG 12 /* 64 bit integer */ |
| 196 | +#define VT_LONG 13 /* long integer (NEVER USED as type, only |
| 197 | + during parsing) */ |
| 198 | +#define VT_BTYPE 0x000f /* mask for basic type */ |
| 199 | +#define VT_UNSIGNED 0x0010 /* unsigned type */ |
| 200 | +#define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */ |
| 201 | +#define VT_BITFIELD 0x0040 /* bitfield modifier */ |
| 202 | + |
| 203 | +/* storage */ |
| 204 | +#define VT_EXTERN 0x00000080 /* extern definition */ |
| 205 | +#define VT_STATIC 0x00000100 /* static variable */ |
| 206 | +#define VT_TYPEDEF 0x00000200 /* typedef definition */ |
| 207 | + |
| 208 | +/* type mask (except storage) */ |
| 209 | +#define VT_TYPE (~(VT_EXTERN | VT_STATIC | VT_TYPEDEF)) |
| 211 | 210 | |
| 212 | 211 | /* token values */ |
| 213 | 212 | |
| @@ -339,8 +338,8 @@ int expr_const(void); · 125 unmodified lines |
| 339 | 338 | void expr_eq(void); |
| 340 | 339 | void gexpr(void); |
| 341 | 340 | void decl(int l); |
| 342 | | −void decl_initializer(int t, int c, int first, int size_only); |
| 343 | | −int decl_initializer_alloc(int t, int has_init); |
| 341 | +void decl_initializer(int t, int r, int c, int first, int size_only); |
| 342 | +int decl_initializer_alloc(int t, int sec, int has_init); |
| 344 | 343 | int gv(void); |
| 345 | 344 | void move_reg(int r, int s); |
| 346 | 345 | void save_reg(int r); |
| @@ -358,7 +357,7 @@ void vstore(void); · 11 unmodified lines |
| 358 | 357 | int type_size(int t, int *a); |
| 359 | 358 | int pointed_type(int t); |
| 360 | 359 | int pointed_size(int t); |
| 361 | | −int ist(void); |
| 360 | +int parse_btype(int *type_ptr); |
| 362 | 361 | int type_decl(int *v, int t, int td); |
| 363 | 362 | void error(const char *fmt, ...); |
| 364 | 363 | void vset(int t, int r, int v); |
| @@ -668,12 +667,15 @@ Sym *sym_find(int v) · 303 unmodified lines |
| 668 | 667 | } |
| 669 | 668 | |
| 670 | 669 | /* push a given symbol on the symbol stack */ |
| 671 | | −Sym *sym_push(int v, int t, int c) |
| 670 | +Sym *sym_push(int v, int t, int r, int c) |
| 672 | 671 | { |
| 672 | + Sym *s; |
| 673 | 673 | if (local_stack.top) |
| 674 | | − return sym_push1(&local_stack, v, t, c); |
| 674 | + s = sym_push1(&local_stack, v, t, c); |
| 675 | 675 | else |
| 676 | | − return sym_push1(&global_stack, v, t, c); |
| 676 | + s = sym_push1(&global_stack, v, t, c); |
| 677 | + s->r = r; |
| 678 | + return s; |
| 677 | 679 | } |
| 678 | 680 | |
| 679 | 681 | /* pop symbols until top reaches 'b' */ |
| @@ -2354,9 +2356,9 @@ void gen_cast(int t) · 1674 unmodified lines |
| 2354 | 2356 | do_ftoi: |
| 2355 | 2357 | gen_cvt_ftoi(dt1); |
| 2356 | 2358 | } |
| 2357 | | − if (dt1 == VT_INT && (t & (VT_TYPE | VT_UNSIGNED)) != dt1) { |
| 2359 | + if (dt1 == VT_INT && (t & (VT_BTYPE | VT_UNSIGNED)) != dt1) { |
| 2358 | 2360 | /* additionnal cast for char/short/bool... */ |
| 2359 | | − vtop->t = (vtop->t & ~VT_TYPE) | dt1; |
| 2361 | + vtop->t = dt1; |
| 2360 | 2362 | gen_cast(t); |
| 2361 | 2363 | } |
| 2362 | 2364 | } else if (dbt == VT_BOOL) { |
| @@ -2436,7 +2438,7 @@ int mk_pointer(int t) · 73 unmodified lines |
| 2436 | 2438 | { |
| 2437 | 2439 | int p; |
| 2438 | 2440 | p = anon_sym++; |
| 2439 | | − sym_push(p, t, -1); |
| 2441 | + sym_push(p, t, 0, -1); |
| 2440 | 2442 | return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE); |
| 2441 | 2443 | } |
| 2442 | 2444 | |
| @@ -2744,7 +2746,7 @@ int struct_decl(int u) · 301 unmodified lines |
| 2744 | 2746 | } else { |
| 2745 | 2747 | v = anon_sym++; |
| 2746 | 2748 | } |
| 2747 | | − s = sym_push(v | SYM_STRUCT, a, 0); |
| 2749 | + s = sym_push(v | SYM_STRUCT, a, 0, 0); |
| 2748 | 2750 | /* put struct/union/enum name in type */ |
| 2749 | 2751 | do_decl: |
| 2750 | 2752 | u = u | (v << VT_STRUCT_SHIFT); |
| @@ -2768,12 +2770,12 @@ int struct_decl(int u) · 17 unmodified lines |
| 2768 | 2770 | c = expr_const(); |
| 2769 | 2771 | } |
| 2770 | 2772 | /* enum symbols have static storage */ |
| 2771 | | − sym_push(v, VT_CONST | VT_STATIC, c); |
| 2773 | + sym_push(v, VT_STATIC | VT_INT, VT_CONST, c); |
| 2772 | 2774 | if (tok == ',') |
| 2773 | 2775 | next(); |
| 2774 | 2776 | c++; |
| 2775 | 2777 | } else { |
| 2776 | | − b = ist(); |
| 2778 | + parse_btype(&b); |
| 2777 | 2779 | while (1) { |
| 2778 | 2780 | bit_size = -1; |
| 2779 | 2781 | v = 0; |
| @@ -2858,7 +2860,7 @@ int struct_decl(int u) · 78 unmodified lines |
| 2858 | 2860 | } |
| 2859 | 2861 | printf("\n"); |
| 2860 | 2862 | #endif |
| 2861 | | − ss = sym_push(v | SYM_FIELD, t, offset); |
| 2863 | + ss = sym_push(v | SYM_FIELD, t, 0, offset); |
| 2862 | 2864 | *ps = ss; |
| 2863 | 2865 | ps = &ss->next; |
| 2864 | 2866 | } |
| @@ -2880,13 +2882,13 @@ int struct_decl(int u) · 15 unmodified lines |
| 2880 | 2882 | |
| 2881 | 2883 | /* return 0 if no type declaration. otherwise, return the basic type |
| 2882 | 2884 | and skip it. |
| 2883 | | − XXX: A '2' is ored to ensure non zero return if int type. |
| 2884 | 2885 | */ |
| 2885 | | −int ist(void) |
| 2886 | +int parse_btype(int *type_ptr) |
| 2886 | 2887 | { |
| 2887 | | − int t, u; |
| 2888 | + int t, u, type_found; |
| 2888 | 2889 | Sym *s; |
| 2889 | 2890 | |
| 2891 | + type_found = 0; |
| 2890 | 2892 | t = 0; |
| 2891 | 2893 | while(1) { |
| 2892 | 2894 | switch(tok) { |
| @@ -2979,13 +2981,14 @@ int ist(void) · 86 unmodified lines |
| 2979 | 2981 | next(); |
| 2980 | 2982 | break; |
| 2981 | 2983 | } |
| 2982 | | − t |= 2; |
| 2984 | + type_found = 1; |
| 2983 | 2985 | } |
| 2984 | 2986 | the_end: |
| 2985 | 2987 | /* long is never used as type */ |
| 2986 | 2988 | if ((t & VT_BTYPE) == VT_LONG) |
| 2987 | 2989 | t = (t & ~VT_BTYPE) | VT_INT; |
| 2988 | | − return t; |
| 2990 | + *type_ptr = t; |
| 2991 | + return type_found; |
| 2989 | 2992 | } |
| 2990 | 2993 | |
| 2991 | 2994 | int post_type(int t) |
| @@ -3002,7 +3005,7 @@ int post_type(int t) · 10 unmodified lines |
| 3002 | 3005 | while (tok != ')') { |
| 3003 | 3006 | /* read param name and compute offset */ |
| 3004 | 3007 | if (l != FUNC_OLD) { |
| 3005 | | − if (!(pt = ist())) { |
| 3008 | + if (!parse_btype(&pt)) { |
| 3006 | 3009 | if (l) { |
| 3007 | 3010 | error("invalid type"); |
| 3008 | 3011 | } else { |
| @@ -3024,7 +3027,7 @@ int post_type(int t) · 15 unmodified lines |
| 3024 | 3027 | } |
| 3025 | 3028 | /* array must be transformed to pointer according to ANSI C */ |
| 3026 | 3029 | pt &= ~VT_ARRAY; |
| 3027 | | − s = sym_push(n | SYM_FIELD, pt, 0); |
| 3030 | + s = sym_push(n | SYM_FIELD, pt, 0, 0); |
| 3028 | 3031 | *plast = s; |
| 3029 | 3032 | plast = &s->next; |
| 3030 | 3033 | if (tok == ',') { |
| @@ -3044,7 +3047,7 @@ int post_type(int t) · 13 unmodified lines |
| 3044 | 3047 | t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN)); |
| 3045 | 3048 | /* we push a anonymous symbol which will contain the function prototype */ |
| 3046 | 3049 | p = anon_sym++; |
| 3047 | | − s = sym_push(p, t, l); |
| 3050 | + s = sym_push(p, t, 0, l); |
| 3048 | 3051 | s->next = first; |
| 3049 | 3052 | t = t1 | VT_FUNC | (p << VT_STRUCT_SHIFT); |
| 3050 | 3053 | } else if (tok == '[') { |
| @@ -3064,7 +3067,7 @@ int post_type(int t) · 13 unmodified lines |
| 3064 | 3067 | /* we push a anonymous symbol which will contain the array |
| 3065 | 3068 | element type */ |
| 3066 | 3069 | p = anon_sym++; |
| 3067 | | − sym_push(p, t, n); |
| 3070 | + sym_push(p, t, 0, n); |
| 3068 | 3071 | t = t1 | VT_ARRAY | VT_PTR | (p << VT_STRUCT_SHIFT); |
| 3069 | 3072 | } |
| 3070 | 3073 | return t; |
| @@ -3077,7 +3080,6 @@ int type_decl(int *v, int t, int td) · 6 unmodified lines |
| 3077 | 3080 | int u, p; |
| 3078 | 3081 | Sym *s; |
| 3079 | 3082 | |
| 3080 | | − t = t & -3; /* suppress the ored '2' */ |
| 3081 | 3083 | while (tok == '*') { |
| 3082 | 3084 | next(); |
| 3083 | 3085 | while (tok == TOK_CONST || tok == TOK_VOLATILE || tok == TOK_RESTRICT) |
| @@ -3120,14 +3122,15 @@ int type_decl(int *v, int t, int td) · 36 unmodified lines |
| 3120 | 3122 | } |
| 3121 | 3123 | |
| 3122 | 3124 | /* define a new external reference to a function 'v' of type 'u' */ |
| 3123 | | −Sym *external_sym(int v, int u) |
| 3125 | +Sym *external_sym(int v, int u, int r) |
| 3124 | 3126 | { |
| 3125 | 3127 | Sym *s; |
| 3126 | 3128 | s = sym_find(v); |
| 3127 | 3129 | if (!s) { |
| 3128 | 3130 | /* push forward reference */ |
| 3129 | 3131 | s = sym_push1(&global_stack, |
| 3130 | | − v, u | VT_CONST | VT_FORWARD, 0); |
| 3132 | + v, u, 0); |
| 3133 | + s->r = r | VT_CONST | VT_FORWARD; |
| 3131 | 3134 | } |
| 3132 | 3135 | return s; |
| 3133 | 3136 | } |
| @@ -3163,7 +3166,7 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 29 unmodified lines |
| 3163 | 3166 | |
| 3164 | 3167 | void unary(void) |
| 3165 | 3168 | { |
| 3166 | | − int n, t, ft, fc, p, align, size; |
| 3169 | + int n, t, ft, fc, p, align, size, r; |
| 3167 | 3170 | Sym *s; |
| 3168 | 3171 | GFuncContext gf; |
| 3169 | 3172 | |
| @@ -3197,8 +3200,8 @@ void unary(void) · 27 unmodified lines |
| 3197 | 3200 | glo = (glo + align - 1) & -align; |
| 3198 | 3201 | fc = glo; |
| 3199 | 3202 | /* we must declare it as an array first to use initializer parser */ |
| 3200 | | − t = VT_CONST | VT_ARRAY | mk_pointer(t); |
| 3201 | | − decl_initializer(t, glo, 1, 0); |
| 3203 | + t = VT_ARRAY | mk_pointer(t); |
| 3204 | + decl_initializer(t, VT_CONST, glo, 1, 0); |
| 3202 | 3205 | glo += type_size(t, &align); |
| 3203 | 3206 | /* put it as pointer */ |
| 3204 | 3207 | vset(t & ~VT_ARRAY, VT_CONST, fc); |
| @@ -3207,21 +3210,21 @@ void unary(void) · 2 unmodified lines |
| 3207 | 3210 | next(); |
| 3208 | 3211 | if (t == '(') { |
| 3209 | 3212 | /* cast ? */ |
| 3210 | | − if (t = ist()) { |
| 3213 | + if (parse_btype(&t)) { |
| 3211 | 3214 | ft = type_decl(&n, t, TYPE_ABSTRACT); |
| 3212 | 3215 | skip(')'); |
| 3213 | 3216 | /* check ISOC99 compound literal */ |
| 3214 | 3217 | if (tok == '{') { |
| 3215 | 3218 | /* data is allocated locally by default */ |
| 3216 | 3219 | if (global_expr) |
| 3217 | | − ft |= VT_CONST; |
| 3220 | + r = VT_CONST; |
| 3218 | 3221 | else |
| 3219 | | − ft |= VT_LOCAL; |
| 3222 | + r = VT_LOCAL; |
| 3220 | 3223 | /* all except arrays are lvalues */ |
| 3221 | 3224 | if (!(ft & VT_ARRAY)) |
| 3222 | | − ft |= VT_LVAL; |
| 3223 | | − fc = decl_initializer_alloc(ft, 1); |
| 3224 | | − vset(ft & VT_TYPE, ft & (VT_VALMASK | VT_LVAL), fc); |
| 3225 | + r |= VT_LVAL; |
| 3226 | + fc = decl_initializer_alloc(ft, r, 1); |
| 3227 | + vset(ft, r, fc); |
| 3225 | 3228 | } else { |
| 3226 | 3229 | unary(); |
| 3227 | 3230 | gen_cast(ft); |
| @@ -3264,9 +3267,9 @@ void unary(void) · 36 unmodified lines |
| 3264 | 3267 | if (t == TOK_SIZEOF) { |
| 3265 | 3268 | if (tok == '(') { |
| 3266 | 3269 | next(); |
| 3267 | | − if (t = ist()) |
| 3270 | + if (parse_btype(&t)) { |
| 3268 | 3271 | t = type_decl(&n, t, TYPE_ABSTRACT); |
| 3269 | | − else { |
| 3272 | + } else { |
| 3270 | 3273 | /* XXX: some code could be generated: add eval |
| 3271 | 3274 | flag */ |
| 3272 | 3275 | gexpr(); |
| @@ -3299,10 +3302,9 @@ void unary(void) · 26 unmodified lines |
| 3299 | 3302 | p = anon_sym++; |
| 3300 | 3303 | sym_push1(&global_stack, p, 0, FUNC_OLD); |
| 3301 | 3304 | /* int() function */ |
| 3302 | | − s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT)); |
| 3305 | + s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT), 0); |
| 3303 | 3306 | } |
| 3304 | | − vset(s->t & VT_TYPE, |
| 3305 | | − s->t & (VT_VALMASK | VT_LVAL | VT_FORWARD), s->c); |
| 3307 | + vset(s->t, s->r, s->c); |
| 3306 | 3308 | /* if forward reference, we must point to s */ |
| 3307 | 3309 | if (vtop->r & VT_FORWARD) |
| 3308 | 3310 | vtop->c.sym = s; |
| @@ -3880,7 +3882,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 571 unmodified lines |
| 3880 | 3882 | address. cur_index/cur_field is the pointer to the current |
| 3881 | 3883 | value. 'size_only' is true if only size info is needed (only used |
| 3882 | 3884 | in arrays) */ |
| 3883 | | −void decl_designator(int t, int c, |
| 3885 | +void decl_designator(int t, int r, int c, |
| 3884 | 3886 | int *cur_index, Sym **cur_field, |
| 3885 | 3887 | int size_only) |
| 3886 | 3888 | { |
| @@ -3949,25 +3951,36 @@ void decl_designator(int t, int c, · 62 unmodified lines |
| 3949 | 3951 | c += f->c; |
| 3950 | 3952 | } |
| 3951 | 3953 | } |
| 3952 | | − decl_initializer(t, c, 0, size_only); |
| 3954 | + decl_initializer(t, r, c, 0, size_only); |
| 3953 | 3955 | } |
| 3954 | 3956 | |
| 3955 | | −/* store a value or an expression directly in global data or in local array */ |
| 3957 | +#define EXPR_VAL 0 |
| 3958 | +#define EXPR_CONST 1 |
| 3959 | +#define EXPR_ANY 2 |
| 3956 | 3960 | |
| 3957 | | −void init_putv(int t, int c, int v, int is_expr) |
| 3961 | +/* store a value or an expression directly in global data or in local array */ |
| 3962 | +void init_putv(int t, int r, int c, |
| 3963 | + int v, int expr_type) |
| 3958 | 3964 | { |
| 3959 | 3965 | int saved_global_expr, bt; |
| 3960 | 3966 | |
| 3961 | | − if ((t & VT_VALMASK) == VT_CONST) { |
| 3962 | | − if (is_expr) { |
| 3963 | | − /* compound literals must be allocated globally in this case */ |
| 3964 | | − saved_global_expr = global_expr; |
| 3965 | | − global_expr = 1; |
| 3966 | | − expr_const1(); |
| 3967 | | − global_expr = saved_global_expr; |
| 3968 | | − } else { |
| 3969 | | − vpushi(v); |
| 3970 | | − } |
| 3967 | + switch(expr_type) { |
| 3968 | + case EXPR_VAL: |
| 3969 | + vpushi(v); |
| 3970 | + break; |
| 3971 | + case EXPR_CONST: |
| 3972 | + /* compound literals must be allocated globally in this case */ |
| 3973 | + saved_global_expr = global_expr; |
| 3974 | + global_expr = 1; |
| 3975 | + expr_const1(); |
| 3976 | + global_expr = saved_global_expr; |
| 3977 | + break; |
| 3978 | + case EXPR_ANY: |
| 3979 | + expr_eq(); |
| 3980 | + break; |
| 3981 | + } |
| 3982 | + |
| 3983 | + if ((r & VT_VALMASK) == VT_CONST) { |
| 3971 | 3984 | /* XXX: do casting */ |
| 3972 | 3985 | /* XXX: not portable */ |
| 3973 | 3986 | bt = vtop->t & VT_BTYPE; |
| @@ -3995,22 +4008,19 @@ void init_putv(int t, int c, int v, int is_expr) · 21 unmodified lines |
| 3995 | 4008 | } |
| 3996 | 4009 | vpop(); |
| 3997 | 4010 | } else { |
| 3998 | | − vset(t & VT_TYPE, t & (VT_VALMASK | VT_LVAL | VT_FORWARD), c); |
| 3999 | | − if (is_expr) |
| 4000 | | − expr_eq(); |
| 4001 | | − else |
| 4002 | | − vpushi(v); |
| 4011 | + vset(t, r, c); |
| 4012 | + vswap(); |
| 4003 | 4013 | vstore(); |
| 4004 | 4014 | vpop(); |
| 4005 | 4015 | } |
| 4006 | 4016 | } |
| 4007 | 4017 | |
| 4008 | 4018 | /* put zeros for variable based init */ |
| 4009 | | −void init_putz(int t, int c, int size) |
| 4019 | +void init_putz(int t, int r, int c, int size) |
| 4010 | 4020 | { |
| 4011 | 4021 | GFuncContext gf; |
| 4012 | 4022 | |
| 4013 | | − if ((t & VT_VALMASK) == VT_CONST) { |
| 4023 | + if ((r & VT_VALMASK) == VT_CONST) { |
| 4014 | 4024 | /* nothing to do because global are already set to zero */ |
| 4015 | 4025 | } else { |
| 4016 | 4026 | gfunc_start(&gf); |
| @@ -4029,10 +4039,10 @@ void init_putz(int t, int c, int size) · 12 unmodified lines |
| 4029 | 4039 | object. 'first' is true if array '{' must be read (multi dimension |
| 4030 | 4040 | implicit array init handling). 'size_only' is true if size only |
| 4031 | 4041 | evaluation is wanted (only for arrays). */ |
| 4032 | | −void decl_initializer(int t, int c, int first, int size_only) |
| 4042 | +void decl_initializer(int t, int r, int c, int first, int size_only) |
| 4033 | 4043 | { |
| 4034 | 4044 | int index, array_length, n, no_oblock, nb, parlevel, i; |
| 4035 | | − int t1, size1, align1; |
| 4045 | + int t1, size1, align1, expr_type; |
| 4036 | 4046 | Sym *s, *f; |
| 4037 | 4047 | TokenSym *ts; |
| 4038 | 4048 | |
| @@ -4067,8 +4077,8 @@ void decl_initializer(int t, int c, int first, int size_only) · 28 unmodified lines |
| 4067 | 4077 | if (ts->len > nb) |
| 4068 | 4078 | warning("initializer-string for array is too long"); |
| 4069 | 4079 | for(i=0;i<nb;i++) { |
| 4070 | | − init_putv(t1, c + (array_length + i) * size1, |
| 4071 | | − ts->str[i], 0); |
| 4080 | + init_putv(t1, r, c + (array_length + i) * size1, |
| 4081 | + ts->str[i], EXPR_VAL); |
| 4072 | 4082 | } |
| 4073 | 4083 | } |
| 4074 | 4084 | array_length += nb; |
| @@ -4078,20 +4088,20 @@ void decl_initializer(int t, int c, int first, int size_only) · 3 unmodified lines |
| 4078 | 4088 | warning in this case since it is standard) */ |
| 4079 | 4089 | if (n < 0 || array_length < n) { |
| 4080 | 4090 | if (!size_only) { |
| 4081 | | − init_putv(t1, c + (array_length * size1), 0, 0); |
| 4091 | + init_putv(t1, r, c + (array_length * size1), 0, EXPR_VAL); |
| 4082 | 4092 | } |
| 4083 | 4093 | array_length++; |
| 4084 | 4094 | } |
| 4085 | 4095 | } else { |
| 4086 | 4096 | index = 0; |
| 4087 | 4097 | while (tok != '}') { |
| 4088 | | − decl_designator(t, c, &index, NULL, size_only); |
| 4098 | + decl_designator(t, r, c, &index, NULL, size_only); |
| 4089 | 4099 | if (n >= 0 && index >= n) |
| 4090 | 4100 | error("index too large"); |
| 4091 | 4101 | /* must put zero in holes (note that doing it that way |
| 4092 | 4102 | ensures that it even works with designators) */ |
| 4093 | 4103 | if (!size_only && array_length < index) { |
| 4094 | | − init_putz(t1, c + array_length * size1, |
| 4104 | + init_putz(t1, r, c + array_length * size1, |
| 4095 | 4105 | (index - array_length) * size1); |
| 4096 | 4106 | } |
| 4097 | 4107 | index++; |
| @@ -4111,7 +4121,7 @@ void decl_initializer(int t, int c, int first, int size_only) · 13 unmodified lines |
| 4111 | 4121 | skip('}'); |
| 4112 | 4122 | /* put zeros at the end */ |
| 4113 | 4123 | if (!size_only && n >= 0 && array_length < n) { |
| 4114 | | − init_putz(t1, c + array_length * size1, |
| 4124 | + init_putz(t1, r, c + array_length * size1, |
| 4115 | 4125 | (n - array_length) * size1); |
| 4116 | 4126 | } |
| 4117 | 4127 | /* patch type size if needed */ |
| @@ -4126,11 +4136,11 @@ void decl_initializer(int t, int c, int first, int size_only) · 8 unmodified lines |
| 4126 | 4136 | index = 0; |
| 4127 | 4137 | n = s->c; |
| 4128 | 4138 | while (tok != '}') { |
| 4129 | | − decl_designator(t, c, NULL, &f, size_only); |
| 4139 | + decl_designator(t, r, c, NULL, &f, size_only); |
| 4130 | 4140 | /* fill with zero between fields */ |
| 4131 | 4141 | index = f->c; |
| 4132 | 4142 | if (!size_only && array_length < index) { |
| 4133 | | − init_putz(t, c + array_length, |
| 4143 | + init_putz(t, r, c + array_length, |
| 4134 | 4144 | index - array_length); |
| 4135 | 4145 | } |
| 4136 | 4146 | index = index + type_size(f->t, &align1); |
| @@ -4143,13 +4153,13 @@ void decl_initializer(int t, int c, int first, int size_only) · 6 unmodified lines |
| 4143 | 4153 | } |
| 4144 | 4154 | /* put zeros at the end */ |
| 4145 | 4155 | if (!size_only && array_length < n) { |
| 4146 | | − init_putz(t, c + array_length, |
| 4156 | + init_putz(t, r, c + array_length, |
| 4147 | 4157 | n - array_length); |
| 4148 | 4158 | } |
| 4149 | 4159 | skip('}'); |
| 4150 | 4160 | } else if (tok == '{') { |
| 4151 | 4161 | next(); |
| 4152 | | − decl_initializer(t, c, first, size_only); |
| 4162 | + decl_initializer(t, r, c, first, size_only); |
| 4153 | 4163 | skip('}'); |
| 4154 | 4164 | } else if (size_only) { |
| 4155 | 4165 | /* just skip expression */ |
| @@ -4163,14 +4173,19 @@ void decl_initializer(int t, int c, int first, int size_only) · 7 unmodified lines |
| 4163 | 4173 | next(); |
| 4164 | 4174 | } |
| 4165 | 4175 | } else { |
| 4166 | | − init_putv(t, c, 0, 1); |
| 4176 | + /* currently, we always use constant expression for globals |
| 4177 | + (may change for scripting case) */ |
| 4178 | + expr_type = EXPR_CONST; |
| 4179 | + if ((r & VT_VALMASK) == VT_LOCAL) |
| 4180 | + expr_type = EXPR_ANY; |
| 4181 | + init_putv(t, r, c, 0, expr_type); |
| 4167 | 4182 | } |
| 4168 | 4183 | } |
| 4169 | 4184 | |
| 4170 | 4185 | /* parse an initializer for type 't' if 'has_init' is true, and |
| 4171 | | − allocate space in local or global data space. The allocated address |
| 4172 | | − in returned */ |
| 4173 | | −int decl_initializer_alloc(int t, int has_init) |
| 4186 | + allocate space in local or global data space ('r' is either |
| 4187 | + VT_LOCAL or VT_CONST). The allocated address in returned */ |
| 4188 | +int decl_initializer_alloc(int t, int r, int has_init) |
| 4174 | 4189 | { |
| 4175 | 4190 | int size, align, addr, tok1; |
| 4176 | 4191 | int *init_str, init_len, level, *saved_macro_ptr; |
| @@ -4212,7 +4227,7 @@ int decl_initializer_alloc(int t, int has_init) · 35 unmodified lines |
| 4212 | 4227 | saved_macro_ptr = macro_ptr; |
| 4213 | 4228 | macro_ptr = init_str; |
| 4214 | 4229 | next(); |
| 4215 | | − decl_initializer(t, 0, 1, 1); |
| 4230 | + decl_initializer(t, r, 0, 1, 1); |
| 4216 | 4231 | /* prepare second initializer parsing */ |
| 4217 | 4232 | macro_ptr = init_str; |
| 4218 | 4233 | next(); |
| @@ -4222,7 +4237,7 @@ int decl_initializer_alloc(int t, int has_init) · 3 unmodified lines |
| 4222 | 4237 | if (size < 0) |
| 4223 | 4238 | error("unknown type size"); |
| 4224 | 4239 | } |
| 4225 | | − if ((t & VT_VALMASK) == VT_LOCAL) { |
| 4240 | + if ((r & VT_VALMASK) == VT_LOCAL) { |
| 4226 | 4241 | loc = (loc - size) & -align; |
| 4227 | 4242 | addr = loc; |
| 4228 | 4243 | } else { |
| @@ -4235,7 +4250,7 @@ int decl_initializer_alloc(int t, int has_init) · 6 unmodified lines |
| 4235 | 4250 | glo += size; |
| 4236 | 4251 | } |
| 4237 | 4252 | if (has_init) { |
| 4238 | | − decl_initializer(t, addr, 1, 0); |
| 4253 | + decl_initializer(t, r, addr, 1, 0); |
| 4239 | 4254 | /* restore parse state if needed */ |
| 4240 | 4255 | if (init_str) { |
| 4241 | 4256 | free(init_str); |
| @@ -4250,12 +4265,11 @@ int decl_initializer_alloc(int t, int has_init) · 8 unmodified lines |
| 4250 | 4265 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 4251 | 4266 | void decl(int l) |
| 4252 | 4267 | { |
| 4253 | | − int *a, t, b, v, u, addr, has_init, size, align; |
| 4268 | + int *a, t, b, v, addr, has_init, size, align, r, u; |
| 4254 | 4269 | Sym *sym; |
| 4255 | 4270 | |
| 4256 | 4271 | while (1) { |
| 4257 | | − b = ist(); |
| 4258 | | − if (!b) { |
| 4272 | + if (!parse_btype(&b)) { |
| 4259 | 4273 | /* skip redundant ';' */ |
| 4260 | 4274 | /* XXX: find more elegant solution */ |
| 4261 | 4275 | if (tok == ';') { |
| @@ -4290,13 +4304,14 @@ void decl(int l) · 28 unmodified lines |
| 4290 | 4304 | if (!(t & VT_FUNC)) |
| 4291 | 4305 | expect("function definition"); |
| 4292 | 4306 | /* patch forward references */ |
| 4293 | | − if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) { |
| 4307 | + if ((sym = sym_find(v)) && (sym->r & VT_FORWARD)) { |
| 4294 | 4308 | greloc_patch(sym, ind); |
| 4295 | | − sym->t = VT_CONST | t; |
| 4309 | + sym->t = t; |
| 4296 | 4310 | } else { |
| 4297 | 4311 | /* put function address */ |
| 4298 | | − sym_push1(&global_stack, v, VT_CONST | t, ind); |
| 4312 | + sym = sym_push1(&global_stack, v, t, ind); |
| 4299 | 4313 | } |
| 4314 | + sym->r = VT_CONST; |
| 4300 | 4315 | funcname = get_tok_str(v, NULL); |
| 4301 | 4316 | /* push a dummy symbol to enable local sym storage */ |
| 4302 | 4317 | sym_push1(&local_stack, 0, 0, 0); |
| @@ -4314,9 +4329,8 @@ void decl(int l) · 11 unmodified lines |
| 4314 | 4329 | } |
| 4315 | 4330 | while (sym = sym->next) { |
| 4316 | 4331 | u = sym->t; |
| 4317 | | − sym_push(sym->v & ~SYM_FIELD, |
| 4318 | | − u | VT_LOCAL | VT_LVAL, |
| 4319 | | − addr); |
| 4332 | + sym_push(sym->v & ~SYM_FIELD, u, |
| 4333 | + VT_LOCAL | VT_LVAL, addr); |
| 4320 | 4334 | size = type_size(u, &align); |
| 4321 | 4335 | size = (size + 3) & ~3; |
| 4322 | 4336 | #ifdef FUNC_STRUCT_PARAM_AS_PTR |
| @@ -4345,40 +4359,42 @@ void decl(int l) · 22 unmodified lines |
| 4345 | 4359 | if (b & VT_TYPEDEF) { |
| 4346 | 4360 | /* save typedefed type */ |
| 4347 | 4361 | /* XXX: test storage specifiers ? */ |
| 4348 | | − sym_push(v, t | VT_TYPEDEF, 0); |
| 4362 | + sym_push(v, t | VT_TYPEDEF, 0, 0); |
| 4349 | 4363 | } else if ((t & VT_BTYPE) == VT_FUNC) { |
| 4350 | 4364 | /* external function definition */ |
| 4351 | | − external_sym(v, t); |
| 4365 | + external_sym(v, t, 0); |
| 4352 | 4366 | } else { |
| 4353 | 4367 | /* not lvalue if array */ |
| 4368 | + r = 0; |
| 4354 | 4369 | if (!(t & VT_ARRAY)) |
| 4355 | | − t |= VT_LVAL; |
| 4370 | + r |= VT_LVAL; |
| 4356 | 4371 | if (b & VT_EXTERN) { |
| 4357 | 4372 | /* external variable */ |
| 4358 | | − external_sym(v, t); |
| 4373 | + external_sym(v, t, r); |
| 4359 | 4374 | } else { |
| 4360 | | − u = l; |
| 4361 | 4375 | if (t & VT_STATIC) |
| 4362 | | − u = VT_CONST; |
| 4363 | | − u |= t; |
| 4376 | + r |= VT_CONST; |
| 4377 | + else |
| 4378 | + r |= l; |
| 4364 | 4379 | has_init = (tok == '='); |
| 4365 | 4380 | if (has_init) |
| 4366 | 4381 | next(); |
| 4367 | | − addr = decl_initializer_alloc(u, has_init); |
| 4382 | + addr = decl_initializer_alloc(t, r, |
| 4383 | + has_init); |
| 4368 | 4384 | if (l == VT_CONST) { |
| 4369 | 4385 | /* global scope: see if already defined */ |
| 4370 | 4386 | sym = sym_find(v); |
| 4371 | 4387 | if (!sym) |
| 4372 | 4388 | goto do_def; |
| 4373 | | − if (!is_compatible_types(sym->t, u)) |
| 4389 | + if (!is_compatible_types(sym->t, t)) |
| 4374 | 4390 | error("incompatible types for redefinition of '%s'", |
| 4375 | 4391 | get_tok_str(v, NULL)); |
| 4376 | | − if (!(sym->t & VT_FORWARD)) |
| 4392 | + if (!(sym->r & VT_FORWARD)) |
| 4377 | 4393 | error("redefinition of '%s'", get_tok_str(v, NULL)); |
| 4378 | 4394 | greloc_patch(sym, addr); |
| 4379 | 4395 | } else { |
| 4380 | 4396 | do_def: |
| 4381 | | − sym_push(v, u, addr); |
| 4397 | + sym_push(v, t, r, addr); |
| 4382 | 4398 | } |
| 4383 | 4399 | } |
| 4384 | 4400 | } |
| @@ -4411,11 +4427,12 @@ void resolve_global_syms(void) · 26 unmodified lines |
| 4411 | 4427 | ext_sym = sym_find1(&extern_stack, s->v); |
| 4412 | 4428 | if (!ext_sym) { |
| 4413 | 4429 | /* if the symbol do not exist, we simply save it */ |
| 4414 | | − sym_push1(&extern_stack, s->v, s->t, s->c); |
| 4415 | | − } else if (ext_sym->t & VT_FORWARD) { |
| 4430 | + ext_sym = sym_push1(&extern_stack, s->v, s->t, s->c); |
| 4431 | + ext_sym->r = s->r; |
| 4432 | + } else if (ext_sym->r & VT_FORWARD) { |
| 4416 | 4433 | /* external symbol already exists, but only as forward |
| 4417 | 4434 | definition */ |
| 4418 | | − if (!(s->t & VT_FORWARD)) { |
| 4435 | + if (!(s->r & VT_FORWARD)) { |
| 4419 | 4436 | /* s is not forward, so we can relocate all symbols */ |
| 4420 | 4437 | greloc_patch(ext_sym, s->c); |
| 4421 | 4438 | } else { |
| @@ -4428,7 +4445,7 @@ void resolve_global_syms(void) · 6 unmodified lines |
| 4428 | 4445 | } else { |
| 4429 | 4446 | /* external symbol already exists and is defined : |
| 4430 | 4447 | patch all references to it */ |
| 4431 | | − if (!(s->t & VT_FORWARD)) |
| 4448 | + if (!(s->r & VT_FORWARD)) |
| 4432 | 4449 | error("'%s' defined twice", get_tok_str(s->v, NULL)); |
| 4433 | 4450 | greloc_patch(s, ext_sym->c); |
| 4434 | 4451 | } |
| @@ -4497,7 +4514,7 @@ void resolve_extern_syms(void) · 62 unmodified lines |
| 4497 | 4514 | s = extern_stack.top; |
| 4498 | 4515 | while (s != NULL) { |
| 4499 | 4516 | s1 = s->prev; |
| 4500 | | − if (s->t & VT_FORWARD) { |
| 4517 | + if (s->r & VT_FORWARD) { |
| 4501 | 4518 | /* if there is at least one relocation to do, then find it |
| 4502 | 4519 | and patch it */ |
| 4503 | 4520 | if (s->c) { |
| @@ -4601,7 +4618,7 @@ int main(int argc, char **argv) · 97 unmodified lines |
| 4601 | 4618 | return 0; |
| 4602 | 4619 | } else { |
| 4603 | 4620 | s = sym_find1(&extern_stack, TOK_MAIN); |
| 4604 | | − if (!s || (s->t & VT_FORWARD)) |
| 4621 | + if (!s || (s->r & VT_FORWARD)) |
| 4605 | 4622 | error("main() not defined"); |
| 4606 | 4623 | t = (int (*)())s->c; |
| 4607 | 4624 | return (*t)(argc - optind, argv + optind); |