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