History

last patches to separate type and value handling

bellard committed 24 years ago

Commit 49855111 parent f7181902 files · +161 −144Browse files

Unified · Split

2 files
  1. i386-gen.c +1 −1
  2. tcc.c +160 −143
i386-gen.c+1−1
@@ -122,7 +122,7 @@ void greloc_patch(Sym *s, int val) · 121 unmodified lines
122122 p = p1;
123123 }
124124 s->c = val;
125− s->t &= ~VT_FORWARD;
125+ s->r &= ~VT_FORWARD;
126126 }
127127
128128 /* output a symbol and patch all calls to it */
tcc.c+160−143
@@ -79,6 +79,7 @@ typedef struct SValue { · 78 unmodified lines
7979 typedef struct Sym {
8080 int v; /* symbol token */
8181 int t; /* associated type */
82+ int r; /* associated register */
8283 int c; /* associated number */
8384 struct Sym *next; /* next related symbol */
8485 struct Sym *prev; /* prev symbol in stack */
@@ -165,49 +166,47 @@ int gnu_ext = 1; · 80 unmodified lines
165166 int tcc_ext = 1;
166167
167168 /* 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
170171 (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 */
184179
185180 /* types */
186181 #define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */
187182
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))
211210
212211 /* token values */
213212
@@ -339,8 +338,8 @@ int expr_const(void); · 125 unmodified lines
339338 void expr_eq(void);
340339 void gexpr(void);
341340 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);
344343 int gv(void);
345344 void move_reg(int r, int s);
346345 void save_reg(int r);
@@ -358,7 +357,7 @@ void vstore(void); · 11 unmodified lines
358357 int type_size(int t, int *a);
359358 int pointed_type(int t);
360359 int pointed_size(int t);
361−int ist(void);
360+int parse_btype(int *type_ptr);
362361 int type_decl(int *v, int t, int td);
363362 void error(const char *fmt, ...);
364363 void vset(int t, int r, int v);
@@ -668,12 +667,15 @@ Sym *sym_find(int v) · 303 unmodified lines
668667 }
669668
670669 /* 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)
672671 {
672+ Sym *s;
673673 if (local_stack.top)
674− return sym_push1(&local_stack, v, t, c);
674+ s = sym_push1(&local_stack, v, t, c);
675675 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;
677679 }
678680
679681 /* pop symbols until top reaches 'b' */
@@ -2354,9 +2356,9 @@ void gen_cast(int t) · 1674 unmodified lines
23542356 do_ftoi:
23552357 gen_cvt_ftoi(dt1);
23562358 }
2357− if (dt1 == VT_INT && (t & (VT_TYPE | VT_UNSIGNED)) != dt1) {
2359+ if (dt1 == VT_INT && (t & (VT_BTYPE | VT_UNSIGNED)) != dt1) {
23582360 /* additionnal cast for char/short/bool... */
2359− vtop->t = (vtop->t & ~VT_TYPE) | dt1;
2361+ vtop->t = dt1;
23602362 gen_cast(t);
23612363 }
23622364 } else if (dbt == VT_BOOL) {
@@ -2436,7 +2438,7 @@ int mk_pointer(int t) · 73 unmodified lines
24362438 {
24372439 int p;
24382440 p = anon_sym++;
2439− sym_push(p, t, -1);
2441+ sym_push(p, t, 0, -1);
24402442 return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE);
24412443 }
24422444
@@ -2744,7 +2746,7 @@ int struct_decl(int u) · 301 unmodified lines
27442746 } else {
27452747 v = anon_sym++;
27462748 }
2747− s = sym_push(v | SYM_STRUCT, a, 0);
2749+ s = sym_push(v | SYM_STRUCT, a, 0, 0);
27482750 /* put struct/union/enum name in type */
27492751 do_decl:
27502752 u = u | (v << VT_STRUCT_SHIFT);
@@ -2768,12 +2770,12 @@ int struct_decl(int u) · 17 unmodified lines
27682770 c = expr_const();
27692771 }
27702772 /* 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);
27722774 if (tok == ',')
27732775 next();
27742776 c++;
27752777 } else {
2776− b = ist();
2778+ parse_btype(&b);
27772779 while (1) {
27782780 bit_size = -1;
27792781 v = 0;
@@ -2858,7 +2860,7 @@ int struct_decl(int u) · 78 unmodified lines
28582860 }
28592861 printf("\n");
28602862 #endif
2861− ss = sym_push(v | SYM_FIELD, t, offset);
2863+ ss = sym_push(v | SYM_FIELD, t, 0, offset);
28622864 *ps = ss;
28632865 ps = &ss->next;
28642866 }
@@ -2880,13 +2882,13 @@ int struct_decl(int u) · 15 unmodified lines
28802882
28812883 /* return 0 if no type declaration. otherwise, return the basic type
28822884 and skip it.
2883− XXX: A '2' is ored to ensure non zero return if int type.
28842885 */
2885−int ist(void)
2886+int parse_btype(int *type_ptr)
28862887 {
2887− int t, u;
2888+ int t, u, type_found;
28882889 Sym *s;
28892890
2891+ type_found = 0;
28902892 t = 0;
28912893 while(1) {
28922894 switch(tok) {
@@ -2979,13 +2981,14 @@ int ist(void) · 86 unmodified lines
29792981 next();
29802982 break;
29812983 }
2982− t |= 2;
2984+ type_found = 1;
29832985 }
29842986 the_end:
29852987 /* long is never used as type */
29862988 if ((t & VT_BTYPE) == VT_LONG)
29872989 t = (t & ~VT_BTYPE) | VT_INT;
2988− return t;
2990+ *type_ptr = t;
2991+ return type_found;
29892992 }
29902993
29912994 int post_type(int t)
@@ -3002,7 +3005,7 @@ int post_type(int t) · 10 unmodified lines
30023005 while (tok != ')') {
30033006 /* read param name and compute offset */
30043007 if (l != FUNC_OLD) {
3005− if (!(pt = ist())) {
3008+ if (!parse_btype(&pt)) {
30063009 if (l) {
30073010 error("invalid type");
30083011 } else {
@@ -3024,7 +3027,7 @@ int post_type(int t) · 15 unmodified lines
30243027 }
30253028 /* array must be transformed to pointer according to ANSI C */
30263029 pt &= ~VT_ARRAY;
3027− s = sym_push(n | SYM_FIELD, pt, 0);
3030+ s = sym_push(n | SYM_FIELD, pt, 0, 0);
30283031 *plast = s;
30293032 plast = &s->next;
30303033 if (tok == ',') {
@@ -3044,7 +3047,7 @@ int post_type(int t) · 13 unmodified lines
30443047 t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN));
30453048 /* we push a anonymous symbol which will contain the function prototype */
30463049 p = anon_sym++;
3047− s = sym_push(p, t, l);
3050+ s = sym_push(p, t, 0, l);
30483051 s->next = first;
30493052 t = t1 | VT_FUNC | (p << VT_STRUCT_SHIFT);
30503053 } else if (tok == '[') {
@@ -3064,7 +3067,7 @@ int post_type(int t) · 13 unmodified lines
30643067 /* we push a anonymous symbol which will contain the array
30653068 element type */
30663069 p = anon_sym++;
3067− sym_push(p, t, n);
3070+ sym_push(p, t, 0, n);
30683071 t = t1 | VT_ARRAY | VT_PTR | (p << VT_STRUCT_SHIFT);
30693072 }
30703073 return t;
@@ -3077,7 +3080,6 @@ int type_decl(int *v, int t, int td) · 6 unmodified lines
30773080 int u, p;
30783081 Sym *s;
30793082
3080− t = t & -3; /* suppress the ored '2' */
30813083 while (tok == '*') {
30823084 next();
30833085 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
31203122 }
31213123
31223124 /* 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)
31243126 {
31253127 Sym *s;
31263128 s = sym_find(v);
31273129 if (!s) {
31283130 /* push forward reference */
31293131 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;
31313134 }
31323135 return s;
31333136 }
@@ -3163,7 +3166,7 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 29 unmodified lines
31633166
31643167 void unary(void)
31653168 {
3166− int n, t, ft, fc, p, align, size;
3169+ int n, t, ft, fc, p, align, size, r;
31673170 Sym *s;
31683171 GFuncContext gf;
31693172
@@ -3197,8 +3200,8 @@ void unary(void) · 27 unmodified lines
31973200 glo = (glo + align - 1) & -align;
31983201 fc = glo;
31993202 /* 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);
32023205 glo += type_size(t, &align);
32033206 /* put it as pointer */
32043207 vset(t & ~VT_ARRAY, VT_CONST, fc);
@@ -3207,21 +3210,21 @@ void unary(void) · 2 unmodified lines
32073210 next();
32083211 if (t == '(') {
32093212 /* cast ? */
3210− if (t = ist()) {
3213+ if (parse_btype(&t)) {
32113214 ft = type_decl(&n, t, TYPE_ABSTRACT);
32123215 skip(')');
32133216 /* check ISOC99 compound literal */
32143217 if (tok == '{') {
32153218 /* data is allocated locally by default */
32163219 if (global_expr)
3217− ft |= VT_CONST;
3220+ r = VT_CONST;
32183221 else
3219− ft |= VT_LOCAL;
3222+ r = VT_LOCAL;
32203223 /* all except arrays are lvalues */
32213224 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);
32253228 } else {
32263229 unary();
32273230 gen_cast(ft);
@@ -3264,9 +3267,9 @@ void unary(void) · 36 unmodified lines
32643267 if (t == TOK_SIZEOF) {
32653268 if (tok == '(') {
32663269 next();
3267− if (t = ist())
3270+ if (parse_btype(&t)) {
32683271 t = type_decl(&n, t, TYPE_ABSTRACT);
3269− else {
3272+ } else {
32703273 /* XXX: some code could be generated: add eval
32713274 flag */
32723275 gexpr();
@@ -3299,10 +3302,9 @@ void unary(void) · 26 unmodified lines
32993302 p = anon_sym++;
33003303 sym_push1(&global_stack, p, 0, FUNC_OLD);
33013304 /* 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);
33033306 }
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);
33063308 /* if forward reference, we must point to s */
33073309 if (vtop->r & VT_FORWARD)
33083310 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
38803882 address. cur_index/cur_field is the pointer to the current
38813883 value. 'size_only' is true if only size info is needed (only used
38823884 in arrays) */
3883−void decl_designator(int t, int c,
3885+void decl_designator(int t, int r, int c,
38843886 int *cur_index, Sym **cur_field,
38853887 int size_only)
38863888 {
@@ -3949,25 +3951,36 @@ void decl_designator(int t, int c, · 62 unmodified lines
39493951 c += f->c;
39503952 }
39513953 }
3952− decl_initializer(t, c, 0, size_only);
3954+ decl_initializer(t, r, c, 0, size_only);
39533955 }
39543956
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
39563960
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)
39583964 {
39593965 int saved_global_expr, bt;
39603966
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) {
39713984 /* XXX: do casting */
39723985 /* XXX: not portable */
39733986 bt = vtop->t & VT_BTYPE;
@@ -3995,22 +4008,19 @@ void init_putv(int t, int c, int v, int is_expr) · 21 unmodified lines
39954008 }
39964009 vpop();
39974010 } 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();
40034013 vstore();
40044014 vpop();
40054015 }
40064016 }
40074017
40084018 /* 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)
40104020 {
40114021 GFuncContext gf;
40124022
4013− if ((t & VT_VALMASK) == VT_CONST) {
4023+ if ((r & VT_VALMASK) == VT_CONST) {
40144024 /* nothing to do because global are already set to zero */
40154025 } else {
40164026 gfunc_start(&gf);
@@ -4029,10 +4039,10 @@ void init_putz(int t, int c, int size) · 12 unmodified lines
40294039 object. 'first' is true if array '{' must be read (multi dimension
40304040 implicit array init handling). 'size_only' is true if size only
40314041 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)
40334043 {
40344044 int index, array_length, n, no_oblock, nb, parlevel, i;
4035− int t1, size1, align1;
4045+ int t1, size1, align1, expr_type;
40364046 Sym *s, *f;
40374047 TokenSym *ts;
40384048
@@ -4067,8 +4077,8 @@ void decl_initializer(int t, int c, int first, int size_only) · 28 unmodified lines
40674077 if (ts->len > nb)
40684078 warning("initializer-string for array is too long");
40694079 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);
40724082 }
40734083 }
40744084 array_length += nb;
@@ -4078,20 +4088,20 @@ void decl_initializer(int t, int c, int first, int size_only) · 3 unmodified lines
40784088 warning in this case since it is standard) */
40794089 if (n < 0 || array_length < n) {
40804090 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);
40824092 }
40834093 array_length++;
40844094 }
40854095 } else {
40864096 index = 0;
40874097 while (tok != '}') {
4088− decl_designator(t, c, &index, NULL, size_only);
4098+ decl_designator(t, r, c, &index, NULL, size_only);
40894099 if (n >= 0 && index >= n)
40904100 error("index too large");
40914101 /* must put zero in holes (note that doing it that way
40924102 ensures that it even works with designators) */
40934103 if (!size_only && array_length < index) {
4094− init_putz(t1, c + array_length * size1,
4104+ init_putz(t1, r, c + array_length * size1,
40954105 (index - array_length) * size1);
40964106 }
40974107 index++;
@@ -4111,7 +4121,7 @@ void decl_initializer(int t, int c, int first, int size_only) · 13 unmodified lines
41114121 skip('}');
41124122 /* put zeros at the end */
41134123 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,
41154125 (n - array_length) * size1);
41164126 }
41174127 /* patch type size if needed */
@@ -4126,11 +4136,11 @@ void decl_initializer(int t, int c, int first, int size_only) · 8 unmodified lines
41264136 index = 0;
41274137 n = s->c;
41284138 while (tok != '}') {
4129− decl_designator(t, c, NULL, &f, size_only);
4139+ decl_designator(t, r, c, NULL, &f, size_only);
41304140 /* fill with zero between fields */
41314141 index = f->c;
41324142 if (!size_only && array_length < index) {
4133− init_putz(t, c + array_length,
4143+ init_putz(t, r, c + array_length,
41344144 index - array_length);
41354145 }
41364146 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
41434153 }
41444154 /* put zeros at the end */
41454155 if (!size_only && array_length < n) {
4146− init_putz(t, c + array_length,
4156+ init_putz(t, r, c + array_length,
41474157 n - array_length);
41484158 }
41494159 skip('}');
41504160 } else if (tok == '{') {
41514161 next();
4152− decl_initializer(t, c, first, size_only);
4162+ decl_initializer(t, r, c, first, size_only);
41534163 skip('}');
41544164 } else if (size_only) {
41554165 /* just skip expression */
@@ -4163,14 +4173,19 @@ void decl_initializer(int t, int c, int first, int size_only) · 7 unmodified lines
41634173 next();
41644174 }
41654175 } 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);
41674182 }
41684183 }
41694184
41704185 /* 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)
41744189 {
41754190 int size, align, addr, tok1;
41764191 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
42124227 saved_macro_ptr = macro_ptr;
42134228 macro_ptr = init_str;
42144229 next();
4215− decl_initializer(t, 0, 1, 1);
4230+ decl_initializer(t, r, 0, 1, 1);
42164231 /* prepare second initializer parsing */
42174232 macro_ptr = init_str;
42184233 next();
@@ -4222,7 +4237,7 @@ int decl_initializer_alloc(int t, int has_init) · 3 unmodified lines
42224237 if (size < 0)
42234238 error("unknown type size");
42244239 }
4225− if ((t & VT_VALMASK) == VT_LOCAL) {
4240+ if ((r & VT_VALMASK) == VT_LOCAL) {
42264241 loc = (loc - size) & -align;
42274242 addr = loc;
42284243 } else {
@@ -4235,7 +4250,7 @@ int decl_initializer_alloc(int t, int has_init) · 6 unmodified lines
42354250 glo += size;
42364251 }
42374252 if (has_init) {
4238− decl_initializer(t, addr, 1, 0);
4253+ decl_initializer(t, r, addr, 1, 0);
42394254 /* restore parse state if needed */
42404255 if (init_str) {
42414256 free(init_str);
@@ -4250,12 +4265,11 @@ int decl_initializer_alloc(int t, int has_init) · 8 unmodified lines
42504265 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
42514266 void decl(int l)
42524267 {
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;
42544269 Sym *sym;
42554270
42564271 while (1) {
4257− b = ist();
4258− if (!b) {
4272+ if (!parse_btype(&b)) {
42594273 /* skip redundant ';' */
42604274 /* XXX: find more elegant solution */
42614275 if (tok == ';') {
@@ -4290,13 +4304,14 @@ void decl(int l) · 28 unmodified lines
42904304 if (!(t & VT_FUNC))
42914305 expect("function definition");
42924306 /* patch forward references */
4293− if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) {
4307+ if ((sym = sym_find(v)) && (sym->r & VT_FORWARD)) {
42944308 greloc_patch(sym, ind);
4295− sym->t = VT_CONST | t;
4309+ sym->t = t;
42964310 } else {
42974311 /* put function address */
4298− sym_push1(&global_stack, v, VT_CONST | t, ind);
4312+ sym = sym_push1(&global_stack, v, t, ind);
42994313 }
4314+ sym->r = VT_CONST;
43004315 funcname = get_tok_str(v, NULL);
43014316 /* push a dummy symbol to enable local sym storage */
43024317 sym_push1(&local_stack, 0, 0, 0);
@@ -4314,9 +4329,8 @@ void decl(int l) · 11 unmodified lines
43144329 }
43154330 while (sym = sym->next) {
43164331 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);
43204334 size = type_size(u, &align);
43214335 size = (size + 3) & ~3;
43224336 #ifdef FUNC_STRUCT_PARAM_AS_PTR
@@ -4345,40 +4359,42 @@ void decl(int l) · 22 unmodified lines
43454359 if (b & VT_TYPEDEF) {
43464360 /* save typedefed type */
43474361 /* XXX: test storage specifiers ? */
4348− sym_push(v, t | VT_TYPEDEF, 0);
4362+ sym_push(v, t | VT_TYPEDEF, 0, 0);
43494363 } else if ((t & VT_BTYPE) == VT_FUNC) {
43504364 /* external function definition */
4351− external_sym(v, t);
4365+ external_sym(v, t, 0);
43524366 } else {
43534367 /* not lvalue if array */
4368+ r = 0;
43544369 if (!(t & VT_ARRAY))
4355− t |= VT_LVAL;
4370+ r |= VT_LVAL;
43564371 if (b & VT_EXTERN) {
43574372 /* external variable */
4358− external_sym(v, t);
4373+ external_sym(v, t, r);
43594374 } else {
4360− u = l;
43614375 if (t & VT_STATIC)
4362− u = VT_CONST;
4363− u |= t;
4376+ r |= VT_CONST;
4377+ else
4378+ r |= l;
43644379 has_init = (tok == '=');
43654380 if (has_init)
43664381 next();
4367− addr = decl_initializer_alloc(u, has_init);
4382+ addr = decl_initializer_alloc(t, r,
4383+ has_init);
43684384 if (l == VT_CONST) {
43694385 /* global scope: see if already defined */
43704386 sym = sym_find(v);
43714387 if (!sym)
43724388 goto do_def;
4373− if (!is_compatible_types(sym->t, u))
4389+ if (!is_compatible_types(sym->t, t))
43744390 error("incompatible types for redefinition of '%s'",
43754391 get_tok_str(v, NULL));
4376− if (!(sym->t & VT_FORWARD))
4392+ if (!(sym->r & VT_FORWARD))
43774393 error("redefinition of '%s'", get_tok_str(v, NULL));
43784394 greloc_patch(sym, addr);
43794395 } else {
43804396 do_def:
4381− sym_push(v, u, addr);
4397+ sym_push(v, t, r, addr);
43824398 }
43834399 }
43844400 }
@@ -4411,11 +4427,12 @@ void resolve_global_syms(void) · 26 unmodified lines
44114427 ext_sym = sym_find1(&extern_stack, s->v);
44124428 if (!ext_sym) {
44134429 /* 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) {
44164433 /* external symbol already exists, but only as forward
44174434 definition */
4418− if (!(s->t & VT_FORWARD)) {
4435+ if (!(s->r & VT_FORWARD)) {
44194436 /* s is not forward, so we can relocate all symbols */
44204437 greloc_patch(ext_sym, s->c);
44214438 } else {
@@ -4428,7 +4445,7 @@ void resolve_global_syms(void) · 6 unmodified lines
44284445 } else {
44294446 /* external symbol already exists and is defined :
44304447 patch all references to it */
4431− if (!(s->t & VT_FORWARD))
4448+ if (!(s->r & VT_FORWARD))
44324449 error("'%s' defined twice", get_tok_str(s->v, NULL));
44334450 greloc_patch(s, ext_sym->c);
44344451 }
@@ -4497,7 +4514,7 @@ void resolve_extern_syms(void) · 62 unmodified lines
44974514 s = extern_stack.top;
44984515 while (s != NULL) {
44994516 s1 = s->prev;
4500− if (s->t & VT_FORWARD) {
4517+ if (s->r & VT_FORWARD) {
45014518 /* if there is at least one relocation to do, then find it
45024519 and patch it */
45034520 if (s->c) {
@@ -4601,7 +4618,7 @@ int main(int argc, char **argv) · 97 unmodified lines
46014618 return 0;
46024619 } else {
46034620 s = sym_find1(&extern_stack, TOK_MAIN);
4604− if (!s || (s->t & VT_FORWARD))
4621+ if (!s || (s->r & VT_FORWARD))
46054622 error("main() not defined");
46064623 t = (int (*)())s->c;
46074624 return (*t)(argc - optind, argv + optind);

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