History

added compound literals parsing - fixed string init with {}

bellard committed 24 years ago

Commit df9c0471 parent 4298fcc1 file · +127 −92Browse files

Unified · Split

tcc.c+127−92
@@ -97,6 +97,8 @@ typedef struct { · 96 unmodified lines
9797 FILE *file;
9898 int tok, tok1, tokc, rsym, anon_sym,
9999 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 */
100102 int tok_ident;
101103 TokenSym **table_ident;
102104 TokenSym *hash_ident[521];
@@ -267,6 +269,7 @@ void expr_eq(); · 164 unmodified lines
267269 void expr();
268270 void decl();
269271 void decl_initializer(int t, int c, int first, int size_only);
272+int decl_initializer_alloc(int t, int has_init);
270273 int gv();
271274 void move_reg();
272275 void save_reg();
@@ -2363,8 +2366,22 @@ void unary() · 2090 unmodified lines
23632366 if (t = ist()) {
23642367 ft = type_decl(&n, t, TYPE_ABSTRACT);
23652368 skip(')');
2366− unary();
2367− vt = (vt & VT_TYPEN) | ft;
2369+ /* check ISOC99 compound literal */
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+ }
23682385 } else {
23692386 expr();
23702387 skip(')');
@@ -2993,9 +3010,16 @@ void decl_designator(int t, int c, · 622 unmodified lines
29933010
29943011 void init_putv(int t, int c, int v, int is_expr)
29953012 {
3013+ int saved_global_expr;
3014+
29963015 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;
29983020 v = expr_const();
3021+ global_expr = saved_global_expr;
3022+ }
29993023 if (t & VT_BYTE)
30003024 *(char *)c = v;
30013025 else if (t & VT_SHORT)
@@ -3054,14 +3078,20 @@ void decl_initializer(int t, int c, int first, int size_only) · 52 unmodified lines
30543078 array_length = 0;
30553079 t1 = pointed_type(t);
30563080 size1 = type_size(t1, &align1);
3057− if (tok == TOK_LSTR) {
3058− if ((t1 & VT_TYPE & ~VT_UNSIGNED) != VT_INT)
3059− error("invalid type");
3060− goto str_init;
3061− } else if (tok == TOK_STR) {
3062− if (!(t1 & VT_BYTE))
3063− error("invalid type");
3064− str_init:
3081+
3082+ no_oblock = 1;
3083+ if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
3084+ tok == '{') {
3085+ skip('{');
3086+ no_oblock = 0;
3087+ }
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)) {
30653095 /* XXX: move multiple string parsing in parser ? */
30663096 while (tok == TOK_STR || tok == TOK_LSTR) {
30673097 ts = (TokenSym *)tokc;
@@ -3089,11 +3119,6 @@ void decl_initializer(int t, int c, int first, int size_only) · 21 unmodified lines
30893119 array_length++;
30903120 }
30913121 } else {
3092− no_oblock = 0;
3093− if (!first && tok != '{')
3094− no_oblock = 1;
3095− else
3096− skip('{');
30973122 index = 0;
30983123 while (tok != '}') {
30993124 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
31173142 break;
31183143 skip(',');
31193144 }
3120− if (!no_oblock)
3121− skip('}');
31223145 }
3146+ if (!no_oblock)
3147+ skip('}');
31233148 /* put zeros at the end */
31243149 if (!size_only && n >= 0 && array_length < n) {
31253150 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
31783203 }
31793204 }
31803205
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+
31813286 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
31823287 void decl(l)
31833288 {
3184− int *a, t, b, size, align, v, u, n, addr, tok1;
3289+ int *a, t, b, v, u, n, addr, has_init;
31853290 Sym *sym;
3186− int *init_str, init_len, level, *saved_macro_ptr;
31873291
31883292 while (b = ist()) {
31893293 if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') {
@@ -3250,79 +3354,10 @@ void decl(l) · 60 unmodified lines
32503354 if (t & VT_STATIC)
32513355 u = VT_CONST;
32523356 u |= t;
3253− size = type_size(t, &align);
3254− /* If unknown size, we must evaluate it before
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 == '=') {
3357+ has_init = (tok == '=');
3358+ if (has_init)
33173359 next();
3318− decl_initializer(u, addr, 1, 0);
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− }
3360+ addr = decl_initializer_alloc(u, has_init);
33263361 sym_push(v, u, addr);
33273362 }
33283363 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t