Unified · Split
| @@ -97,6 +97,8 @@ typedef struct { · 96 unmodified lines | |||
| 97 | FILE *file; | 97 | FILE *file; |
| 98 | int tok, tok1, tokc, rsym, anon_sym, | 98 | int tok, tok1, tokc, rsym, anon_sym, |
| 99 | prog, ind, loc, glo, vt, vc, const_wanted, line_num; | 99 | prog, ind, loc, glo, vt, vc, const_wanted, line_num; |
| 100 | int global_expr; /* true if compound literals must be allocated | ||
| 101 | globally (used during initializers parsing */ | ||
| 100 | int tok_ident; | 102 | int tok_ident; |
| 101 | TokenSym **table_ident; | 103 | TokenSym **table_ident; |
| 102 | TokenSym *hash_ident[521]; | 104 | TokenSym *hash_ident[521]; |
| @@ -267,6 +269,7 @@ void expr_eq(); · 164 unmodified lines | |||
| 267 | void expr(); | 269 | void expr(); |
| 268 | void decl(); | 270 | void decl(); |
| 269 | void decl_initializer(int t, int c, int first, int size_only); | 271 | void decl_initializer(int t, int c, int first, int size_only); |
| 272 | int decl_initializer_alloc(int t, int has_init); | ||
| 270 | int gv(); | 273 | int gv(); |
| 271 | void move_reg(); | 274 | void move_reg(); |
| 272 | void save_reg(); | 275 | void save_reg(); |
| @@ -2363,8 +2366,22 @@ void unary() · 2090 unmodified lines | |||
| 2363 | if (t = ist()) { | 2366 | if (t = ist()) { |
| 2364 | ft = type_decl(&n, t, TYPE_ABSTRACT); | 2367 | ft = type_decl(&n, t, TYPE_ABSTRACT); |
| 2365 | skip(')'); | 2368 | skip(')'); |
| 2366 | unary(); | 2369 | /* check ISOC99 compound literal */ |
| 2367 | vt = (vt & VT_TYPEN) | ft; | 2370 | if (tok == '{') { |
| 2371 | /* data is allocated locally by default */ | ||
| 2372 | if (global_expr) | ||
| 2373 | ft |= VT_CONST; | ||
| 2374 | else | ||
| 2375 | ft |= VT_LOCAL; | ||
| 2376 | /* all except arrays are lvalues */ | ||
| 2377 | if (!(ft & VT_ARRAY)) | ||
| 2378 | ft |= VT_LVAL; | ||
| 2379 | fc = decl_initializer_alloc(ft, 1); | ||
| 2380 | vset(ft, fc); | ||
| 2381 | } else { | ||
| 2382 | unary(); | ||
| 2383 | vt = (vt & VT_TYPEN) | ft; | ||
| 2384 | } | ||
| 2368 | } else { | 2385 | } else { |
| 2369 | expr(); | 2386 | expr(); |
| 2370 | skip(')'); | 2387 | skip(')'); |
| @@ -2993,9 +3010,16 @@ void decl_designator(int t, int c, · 622 unmodified lines | |||
| 2993 | 3010 | ||
| 2994 | void init_putv(int t, int c, int v, int is_expr) | 3011 | void init_putv(int t, int c, int v, int is_expr) |
| 2995 | { | 3012 | { |
| 3013 | int saved_global_expr; | ||
| 3014 | |||
| 2996 | if ((t & VT_VALMASK) == VT_CONST) { | 3015 | if ((t & VT_VALMASK) == VT_CONST) { |
| 2997 | if (is_expr) | 3016 | if (is_expr) { |
| 3017 | /* compound literals must be allocated globally in this case */ | ||
| 3018 | saved_global_expr = global_expr; | ||
| 3019 | global_expr = 1; | ||
| 2998 | v = expr_const(); | 3020 | v = expr_const(); |
| 3021 | global_expr = saved_global_expr; | ||
| 3022 | } | ||
| 2999 | if (t & VT_BYTE) | 3023 | if (t & VT_BYTE) |
| 3000 | *(char *)c = v; | 3024 | *(char *)c = v; |
| 3001 | else if (t & VT_SHORT) | 3025 | else if (t & VT_SHORT) |
| @@ -3054,14 +3078,20 @@ void decl_initializer(int t, int c, int first, int size_only) · 52 unmodified lines | |||
| 3054 | array_length = 0; | 3078 | array_length = 0; |
| 3055 | t1 = pointed_type(t); | 3079 | t1 = pointed_type(t); |
| 3056 | size1 = type_size(t1, &align1); | 3080 | size1 = type_size(t1, &align1); |
| 3057 | if (tok == TOK_LSTR) { | 3081 | |
| 3058 | if ((t1 & VT_TYPE & ~VT_UNSIGNED) != VT_INT) | 3082 | no_oblock = 1; |
| 3059 | error("invalid type"); | 3083 | if ((first && tok != TOK_LSTR && tok != TOK_STR) || |
| 3060 | goto str_init; | 3084 | tok == '{') { |
| 3061 | } else if (tok == TOK_STR) { | 3085 | skip('{'); |
| 3062 | if (!(t1 & VT_BYTE)) | 3086 | no_oblock = 0; |
| 3063 | error("invalid type"); | 3087 | } |
| 3064 | str_init: | 3088 | |
| 3089 | /* only parse strings here if correct type (otherwise: handle | ||
| 3090 | them as ((w)char *) expressions */ | ||
| 3091 | if ((tok == TOK_LSTR && | ||
| 3092 | (t1 & VT_TYPE & ~VT_UNSIGNED) == VT_INT) || | ||
| 3093 | (tok == TOK_STR && | ||
| 3094 | (t1 & VT_TYPE & ~VT_UNSIGNED) == VT_BYTE)) { | ||
| 3065 | /* XXX: move multiple string parsing in parser ? */ | 3095 | /* XXX: move multiple string parsing in parser ? */ |
| 3066 | while (tok == TOK_STR || tok == TOK_LSTR) { | 3096 | while (tok == TOK_STR || tok == TOK_LSTR) { |
| 3067 | ts = (TokenSym *)tokc; | 3097 | ts = (TokenSym *)tokc; |
| @@ -3089,11 +3119,6 @@ void decl_initializer(int t, int c, int first, int size_only) · 21 unmodified lines | |||
| 3089 | array_length++; | 3119 | array_length++; |
| 3090 | } | 3120 | } |
| 3091 | } else { | 3121 | } else { |
| 3092 | no_oblock = 0; | ||
| 3093 | if (!first && tok != '{') | ||
| 3094 | no_oblock = 1; | ||
| 3095 | else | ||
| 3096 | skip('{'); | ||
| 3097 | index = 0; | 3122 | index = 0; |
| 3098 | while (tok != '}') { | 3123 | while (tok != '}') { |
| 3099 | decl_designator(t, c, &index, NULL, size_only); | 3124 | decl_designator(t, c, &index, NULL, size_only); |
| @@ -3117,9 +3142,9 @@ void decl_initializer(int t, int c, int first, int size_only) · 17 unmodified lines | |||
| 3117 | break; | 3142 | break; |
| 3118 | skip(','); | 3143 | skip(','); |
| 3119 | } | 3144 | } |
| 3120 | if (!no_oblock) | ||
| 3121 | skip('}'); | ||
| 3122 | } | 3145 | } |
| 3146 | if (!no_oblock) | ||
| 3147 | skip('}'); | ||
| 3123 | /* put zeros at the end */ | 3148 | /* put zeros at the end */ |
| 3124 | if (!size_only && n >= 0 && array_length < n) { | 3149 | if (!size_only && n >= 0 && array_length < n) { |
| 3125 | init_putz(t1, c + array_length * size1, | 3150 | init_putz(t1, c + array_length * size1, |
| @@ -3178,12 +3203,91 @@ void decl_initializer(int t, int c, int first, int size_only) · 52 unmodified lines | |||
| 3178 | } | 3203 | } |
| 3179 | } | 3204 | } |
| 3180 | 3205 | ||
| 3206 | /* parse an initializer for type 't' if 'has_init' is true, and | ||
| 3207 | allocate space in local or global data space. The allocated address | ||
| 3208 | in returned */ | ||
| 3209 | int decl_initializer_alloc(int t, int has_init) | ||
| 3210 | { | ||
| 3211 | int size, align, addr, tok1; | ||
| 3212 | int *init_str, init_len, level, *saved_macro_ptr; | ||
| 3213 | |||
| 3214 | size = type_size(t, &align); | ||
| 3215 | /* If unknown size, we must evaluate it before | ||
| 3216 | evaluating initializers because | ||
| 3217 | initializers can generate global data too | ||
| 3218 | (e.g. string pointers or ISOC99 compound | ||
| 3219 | literals). It also simplifies local | ||
| 3220 | initializers handling */ | ||
| 3221 | init_len = 0; | ||
| 3222 | init_str = NULL; | ||
| 3223 | saved_macro_ptr = NULL; /* avoid warning */ | ||
| 3224 | tok1 = 0; | ||
| 3225 | if (size < 0) { | ||
| 3226 | if (!has_init) | ||
| 3227 | error("unknown type size"); | ||
| 3228 | /* get all init string */ | ||
| 3229 | level = 0; | ||
| 3230 | while (level > 0 || (tok != ',' && tok != ';')) { | ||
| 3231 | if (tok < 0) | ||
| 3232 | error("unexpect end of file in initializer"); | ||
| 3233 | tok_add2(&init_str, &init_len, tok, tokc); | ||
| 3234 | if (tok == '{') | ||
| 3235 | level++; | ||
| 3236 | else if (tok == '}') { | ||
| 3237 | if (level == 0) | ||
| 3238 | break; | ||
| 3239 | level--; | ||
| 3240 | } | ||
| 3241 | next(); | ||
| 3242 | } | ||
| 3243 | tok1 = tok; | ||
| 3244 | tok_add(&init_str, &init_len, -1); | ||
| 3245 | tok_add(&init_str, &init_len, 0); | ||
| 3246 | |||
| 3247 | /* compute size */ | ||
| 3248 | saved_macro_ptr = macro_ptr; | ||
| 3249 | macro_ptr = init_str; | ||
| 3250 | next(); | ||
| 3251 | decl_initializer(t, 0, 1, 1); | ||
| 3252 | /* prepare second initializer parsing */ | ||
| 3253 | macro_ptr = init_str; | ||
| 3254 | next(); | ||
| 3255 | |||
| 3256 | /* if still unknown size, error */ | ||
| 3257 | size = type_size(t, &align); | ||
| 3258 | if (size < 0) | ||
| 3259 | error("unknown type size"); | ||
| 3260 | } | ||
| 3261 | if ((t & VT_VALMASK) == VT_LOCAL) { | ||
| 3262 | loc = (loc - size) & -align; | ||
| 3263 | addr = loc; | ||
| 3264 | } else { | ||
| 3265 | glo = (glo + align - 1) & -align; | ||
| 3266 | addr = glo; | ||
| 3267 | /* very important to increment global | ||
| 3268 | pointer at this time because | ||
| 3269 | initializers themselves can create new | ||
| 3270 | initializers */ | ||
| 3271 | glo += size; | ||
| 3272 | } | ||
| 3273 | if (has_init) { | ||
| 3274 | decl_initializer(t, addr, 1, 0); | ||
| 3275 | /* restore parse state if needed */ | ||
| 3276 | if (init_str) { | ||
| 3277 | free(init_str); | ||
| 3278 | macro_ptr = saved_macro_ptr; | ||
| 3279 | tok = tok1; | ||
| 3280 | } | ||
| 3281 | } | ||
| 3282 | return addr; | ||
| 3283 | } | ||
| 3284 | |||
| 3285 | |||
| 3181 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ | 3286 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 3182 | void decl(l) | 3287 | void decl(l) |
| 3183 | { | 3288 | { |
| 3184 | int *a, t, b, size, align, v, u, n, addr, tok1; | 3289 | int *a, t, b, v, u, n, addr, has_init; |
| 3185 | Sym *sym; | 3290 | Sym *sym; |
| 3186 | int *init_str, init_len, level, *saved_macro_ptr; | ||
| 3187 | 3291 | ||
| 3188 | while (b = ist()) { | 3292 | while (b = ist()) { |
| 3189 | if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') { | 3293 | if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') { |
| @@ -3250,79 +3354,10 @@ void decl(l) · 60 unmodified lines | |||
| 3250 | if (t & VT_STATIC) | 3354 | if (t & VT_STATIC) |
| 3251 | u = VT_CONST; | 3355 | u = VT_CONST; |
| 3252 | u |= t; | 3356 | u |= t; |
| 3253 | size = type_size(t, &align); | 3357 | has_init = (tok == '='); |
| 3254 | /* If unknown size, we must evaluate it before | 3358 | if (has_init) |
| 3255 | evaluating initializers because | ||
| 3256 | initializers can generate global data too | ||
| 3257 | (e.g. string pointers or ISOC99 compound | ||
| 3258 | literals). It also simplifies local | ||
| 3259 | initializers handling */ | ||
| 3260 | init_len = 0; | ||
| 3261 | init_str = NULL; | ||
| 3262 | saved_macro_ptr = NULL; /* avoid warning */ | ||
| 3263 | tok1 = 0; | ||
| 3264 | if (size < 0) { | ||
| 3265 | if (tok != '=') | ||
| 3266 | error("unknown size for '%s'", get_tok_str(v, 0)); | ||
| 3267 | tok_add2(&init_str, &init_len, tok, tokc); | ||
| 3268 | next(); | ||
| 3269 | /* get all init string */ | ||
| 3270 | level = 0; | ||
| 3271 | while (level > 0 || (tok != ',' && tok != ';')) { | ||
| 3272 | if (tok < 0) | ||
| 3273 | error("unexpect end of file in initializer for '%s'", | ||
| 3274 | get_tok_str(v, 0)); | ||
| 3275 | tok_add2(&init_str, &init_len, tok, tokc); | ||
| 3276 | if (tok == '{') | ||
| 3277 | level++; | ||
| 3278 | else if (tok == '}') { | ||
| 3279 | if (level == 0) | ||
| 3280 | break; | ||
| 3281 | level--; | ||
| 3282 | } | ||
| 3283 | next(); | ||
| 3284 | } | ||
| 3285 | tok1 = tok; | ||
| 3286 | tok_add(&init_str, &init_len, -1); | ||
| 3287 | tok_add(&init_str, &init_len, 0); | ||
| 3288 | |||
| 3289 | /* compute size */ | ||
| 3290 | saved_macro_ptr = macro_ptr; | ||
| 3291 | macro_ptr = init_str; | ||
| 3292 | next(); | ||
| 3293 | skip('='); | ||
| 3294 | decl_initializer(u, 0, 1, 1); | ||
| 3295 | /* prepare second initializer parsing */ | ||
| 3296 | macro_ptr = init_str; | ||
| 3297 | next(); | ||
| 3298 | |||
| 3299 | /* if still unknown size, error */ | ||
| 3300 | size = type_size(t, &align); | ||
| 3301 | if (size < 0) | ||
| 3302 | error("unknown size for '%s'", get_tok_str(v, 0)); | ||
| 3303 | } | ||
| 3304 | if ((u & VT_VALMASK) == VT_LOCAL) { | ||
| 3305 | loc = (loc - size) & -align; | ||
| 3306 | addr = loc; | ||
| 3307 | } else { | ||
| 3308 | glo = (glo + align - 1) & -align; | ||
| 3309 | addr = glo; | ||
| 3310 | /* very important to increment global | ||
| 3311 | pointer at this time because | ||
| 3312 | initializers themselves can create new | ||
| 3313 | initializers */ | ||
| 3314 | glo += size; | ||
| 3315 | } | ||
| 3316 | if (tok == '=') { | ||
| 3317 | next(); | 3359 | next(); |
| 3318 | decl_initializer(u, addr, 1, 0); | 3360 | addr = decl_initializer_alloc(u, has_init); |
| 3319 | /* restore parse state if needed */ | ||
| 3320 | if (init_str) { | ||
| 3321 | free(init_str); | ||
| 3322 | macro_ptr = saved_macro_ptr; | ||
| 3323 | tok = tok1; | ||
| 3324 | } | ||
| 3325 | } | ||
| 3326 | sym_push(v, u, addr); | 3361 | sym_push(v, u, addr); |
| 3327 | } | 3362 | } |
| 3328 | } | 3363 | } |