Mostly revert "tccpp.c: minor fix I'd accidentally not committed"
[tinycc.git] / tccgen.c
Commit [+]AuthorDateLineData
a93bcdff grischka2009-05-05 20:17:11 +02001/*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
88a3ccab grischka2009-12-20 01:53:49 +010021#include "tcc.h"
22
23/********************************************************/
24/* global variables */
25
26/* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
30*/
31ST_DATA int rsym, anon_sym, ind, loc;
32
33ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34ST_DATA Section *cur_text_section; /* current section where function code is generated */
35#ifdef CONFIG_TCC_ASM
36ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37#endif
38#ifdef CONFIG_TCC_BCHECK
39/* bound check related sections */
40ST_DATA Section *bounds_section; /* contains global data bound description */
41ST_DATA Section *lbounds_section; /* contains local data bound description */
42#endif
43/* symbol sections */
44ST_DATA Section *symtab_section, *strtab_section;
45/* debug sections */
46ST_DATA Section *stab_section, *stabstr_section;
47ST_DATA Sym *sym_free_first;
48ST_DATA void **sym_pools;
49ST_DATA int nb_sym_pools;
50
51ST_DATA Sym *global_stack;
52ST_DATA Sym *local_stack;
cf95ac39 Thomas Preud'homme2012-10-25 19:40:04 +020053ST_DATA Sym *scope_stack_bottom;
88a3ccab grischka2009-12-20 01:53:49 +010054ST_DATA Sym *define_stack;
55ST_DATA Sym *global_label_stack;
56ST_DATA Sym *local_label_stack;
57
41b3c7a5
JL
James Lyon2013-04-27 20:39:34 +010058ST_DATA int vla_sp_loc_tmp; /* vla_sp_loc is set to this when the value won't be needed later */
59ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
60ST_DATA int *vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61ST_DATA int vla_flags; /* VLA_* flags */
62
43a11a7e Kirill Smelkov2012-12-01 01:00:23 +040063ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop;
88a3ccab grischka2009-12-20 01:53:49 +010064
65ST_DATA int const_wanted; /* true if constant wanted */
66ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
67ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
68ST_DATA CType func_vt; /* current function return type (used by return instruction) */
8efaa711 Thomas Preud'homme2014-01-06 22:27:39 +080069ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
88a3ccab grischka2009-12-20 01:53:49 +010070ST_DATA int func_vc;
71ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
72ST_DATA char *funcname;
73
718fd591 Michael Matz2012-04-16 01:13:25 +020074ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
88a3ccab grischka2009-12-20 01:53:49 +010075
76/* ------------------------------------------------------------------------- */
77static void gen_cast(CType *type);
78static inline CType *pointed_type(CType *type);
79static int is_compatible_types(CType *type1, CType *type2);
80static int parse_btype(CType *type, AttributeDef *ad);
81static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
82static void parse_expr_type(CType *type);
367bb6f4 seyko2015-03-23 07:40:41 +030083static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
88a3ccab grischka2009-12-20 01:53:49 +010084static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
11b2d335 Thomas Preud'homme2011-02-07 23:43:18 +010085static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
5eb82755 Joe Soroka2011-03-08 13:36:04 -080086static int decl0(int l, int is_for_loop_init);
88a3ccab grischka2009-12-20 01:53:49 +010087static void expr_eq(void);
88static void unary_type(CType *type);
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -070089static void vla_runtime_type_size(CType *type, int *a);
41b3c7a5 James Lyon2013-04-27 20:39:34 +010090static void vla_sp_save(void);
88a3ccab grischka2009-12-20 01:53:49 +010091static int is_compatible_parameter_types(CType *type1, CType *type2);
92static void expr_type(CType *type);
822f4630
UJ
Urs Janssen2014-04-10 11:53:54 +020093ST_FUNC void vpush64(int ty, unsigned long long v);
94ST_FUNC void vpush(CType *type);
95ST_FUNC int gvtst(int inv, int t);
96ST_FUNC int is_btype_size(int bt);
88a3ccab grischka2009-12-20 01:53:49 +010097
98ST_INLN int is_float(int t)
99{
100 int bt;
101 bt = t & VT_BTYPE;
0e17671f James Lyon2013-04-19 15:33:16 +0100102 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
88a3ccab grischka2009-12-20 01:53:49 +0100103}
104
df9cce24 grischka2011-08-01 01:10:36 +0200105/* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107/* XXX: endianness dependent */
108ST_FUNC int ieee_finite(double d)
109{
3fe2a95d grischka2014-01-07 14:57:07 +0100110 int p[4];
111 memcpy(p, &d, sizeof(double));
df9cce24 grischka2011-08-01 01:10:36 +0200112 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
113}
114
88a3ccab grischka2009-12-20 01:53:49 +0100115ST_FUNC void test_lvalue(void)
116{
117 if (!(vtop->r & VT_LVAL))
118 expect("lvalue");
119}
120
121/* ------------------------------------------------------------------------- */
122/* symbol allocator */
123static Sym *__sym_malloc(void)
124{
125 Sym *sym_pool, *sym, *last_sym;
126 int i;
127
128 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
129 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
130
131 last_sym = sym_free_first;
132 sym = sym_pool;
133 for(i = 0; i < SYM_POOL_NB; i++) {
134 sym->next = last_sym;
135 last_sym = sym;
136 sym++;
137 }
138 sym_free_first = last_sym;
139 return last_sym;
140}
141
142static inline Sym *sym_malloc(void)
143{
144 Sym *sym;
145 sym = sym_free_first;
146 if (!sym)
147 sym = __sym_malloc();
148 sym_free_first = sym->next;
149 return sym;
150}
151
152ST_INLN void sym_free(Sym *sym)
153{
154 sym->next = sym_free_first;
7c27186a grischka2015-04-23 23:26:46 +0200155 tcc_free(sym->asm_label);
88a3ccab grischka2009-12-20 01:53:49 +0100156 sym_free_first = sym;
157}
158
159/* push, without hashing */
160ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
161{
162 Sym *s;
cf95ac39
TP
Thomas Preud'homme2012-10-25 19:40:04 +0200163 if (ps == &local_stack) {
164 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
165 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
166 tcc_error("incompatible types for redefinition of '%s'",
167 get_tok_str(v, NULL));
168 }
88a3ccab grischka2009-12-20 01:53:49 +0100169 s = sym_malloc();
32a682b8 Thomas Preud'homme2011-02-07 22:42:38 +0100170 s->asm_label = NULL;
88a3ccab grischka2009-12-20 01:53:49 +0100171 s->v = v;
172 s->type.t = t;
173 s->type.ref = NULL;
174#ifdef _WIN64
175 s->d = NULL;
176#endif
177 s->c = c;
178 s->next = NULL;
179 /* add in stack */
180 s->prev = *ps;
181 *ps = s;
182 return s;
183}
184
185/* find a symbol and return its associated structure. 's' is the top
186 of the symbol stack */
187ST_FUNC Sym *sym_find2(Sym *s, int v)
188{
189 while (s) {
190 if (s->v == v)
191 return s;
c2422ba8
TP
Thomas Preud'homme2014-04-07 21:12:08 +0800192 else if (s->v == -1)
193 return NULL;
88a3ccab grischka2009-12-20 01:53:49 +0100194 s = s->prev;
195 }
196 return NULL;
197}
198
199/* structure lookup */
200ST_INLN Sym *struct_find(int v)
201{
202 v -= TOK_IDENT;
203 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
204 return NULL;
205 return table_ident[v]->sym_struct;
206}
207
208/* find an identifier */
209ST_INLN Sym *sym_find(int v)
210{
211 v -= TOK_IDENT;
212 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
213 return NULL;
214 return table_ident[v]->sym_identifier;
215}
216
217/* push a given symbol on the symbol stack */
218ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
219{
220 Sym *s, **ps;
221 TokenSym *ts;
222
223 if (local_stack)
224 ps = &local_stack;
225 else
226 ps = &global_stack;
227 s = sym_push2(ps, v, type->t, c);
228 s->type.ref = type->ref;
229 s->r = r;
230 /* don't record fields or anonymous symbols */
231 /* XXX: simplify */
232 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
233 /* record symbol in token array */
234 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
235 if (v & SYM_STRUCT)
236 ps = &ts->sym_struct;
237 else
238 ps = &ts->sym_identifier;
239 s->prev_tok = *ps;
240 *ps = s;
241 }
242 return s;
243}
244
245/* push a global identifier */
246ST_FUNC Sym *global_identifier_push(int v, int t, int c)
247{
248 Sym *s, **ps;
249 s = sym_push2(&global_stack, v, t, c);
250 /* don't record anonymous symbol */
251 if (v < SYM_FIRST_ANOM) {
252 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
253 /* modify the top most local identifier, so that
254 sym_identifier will point to 's' when popped */
255 while (*ps != NULL)
256 ps = &(*ps)->prev_tok;
257 s->prev_tok = NULL;
258 *ps = s;
259 }
260 return s;
261}
262
263/* pop symbols until top reaches 'b' */
264ST_FUNC void sym_pop(Sym **ptop, Sym *b)
265{
266 Sym *s, *ss, **ps;
267 TokenSym *ts;
268 int v;
269
270 s = *ptop;
271 while(s != b) {
272 ss = s->prev;
273 v = s->v;
274 /* remove symbol in token array */
275 /* XXX: simplify */
276 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
277 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
278 if (v & SYM_STRUCT)
279 ps = &ts->sym_struct;
280 else
281 ps = &ts->sym_identifier;
282 *ps = s->prev_tok;
283 }
284 sym_free(s);
285 s = ss;
286 }
287 *ptop = b;
288}
289
8bcb2ae1
JS
Joe Soroka2011-03-07 01:02:23 -0800290static void weaken_symbol(Sym *sym)
291{
292 sym->type.t |= VT_WEAK;
293 if (sym->c > 0) {
294 int esym_type;
295 ElfW(Sym) *esym;
296
297 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
298 esym_type = ELFW(ST_TYPE)(esym->st_info);
299 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
300 }
301}
302
fbda78ae
MM
Michael Matz2014-04-14 02:53:11 +0200303static void apply_visibility(Sym *sym, CType *type)
304{
305 int vis = sym->type.t & VT_VIS_MASK;
306 int vis2 = type->t & VT_VIS_MASK;
307 if (vis == (STV_DEFAULT << VT_VIS_SHIFT))
308 vis = vis2;
309 else if (vis2 == (STV_DEFAULT << VT_VIS_SHIFT))
310 ;
311 else
312 vis = (vis < vis2) ? vis : vis2;
313 sym->type.t &= ~VT_VIS_MASK;
314 sym->type.t |= vis;
315
316 if (sym->c > 0) {
fbda78ae
MM
Michael Matz2014-04-14 02:53:11 +0200317 ElfW(Sym) *esym;
318
319 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
320 vis >>= VT_VIS_SHIFT;
321 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) | vis;
322 }
323}
324
88a3ccab grischka2009-12-20 01:53:49 +0100325/* ------------------------------------------------------------------------- */
326
327ST_FUNC void swap(int *p, int *q)
0d1ed741 grischka2009-05-05 20:18:10 +0200328{
329 int t;
330 t = *p;
331 *p = *q;
332 *q = t;
333}
334
88a3ccab grischka2009-12-20 01:53:49 +0100335static void vsetc(CType *type, int r, CValue *vc)
0d1ed741 grischka2009-05-05 20:18:10 +0200336{
337 int v;
338
339 if (vtop >= vstack + (VSTACK_SIZE - 1))
2bd0daab grischka2014-01-06 19:56:26 +0100340 tcc_error("memory full (vstack)");
0d1ed741 grischka2009-05-05 20:18:10 +0200341 /* cannot let cpu flags if other instruction are generated. Also
342 avoid leaving VT_JMP anywhere except on the top of the stack
343 because it would complicate the code generator. */
344 if (vtop >= vstack) {
345 v = vtop->r & VT_VALMASK;
346 if (v == VT_CMP || (v & ~1) == VT_JMP)
347 gv(RC_INT);
348 }
349 vtop++;
350 vtop->type = *type;
351 vtop->r = r;
352 vtop->r2 = VT_CONST;
353 vtop->c = *vc;
354}
355
8de9b7a6 Thomas Preud'homme2010-04-11 01:53:40 +0200356/* push constant of type "type" with useless value */
822f4630 Urs Janssen2014-04-10 11:53:54 +0200357ST_FUNC void vpush(CType *type)
8de9b7a6
TP
Thomas Preud'homme2010-04-11 01:53:40 +0200358{
359 CValue cval;
360 vsetc(type, VT_CONST, &cval);
361}
362
0d1ed741 grischka2009-05-05 20:18:10 +0200363/* push integer constant */
88a3ccab grischka2009-12-20 01:53:49 +0100364ST_FUNC void vpushi(int v)
0d1ed741 grischka2009-05-05 20:18:10 +0200365{
366 CValue cval;
367 cval.i = v;
368 vsetc(&int_type, VT_CONST, &cval);
369}
370
718fd591 Michael Matz2012-04-16 01:13:25 +0200371/* push a pointer sized constant */
5879c854 grischka2014-04-04 20:18:39 +0200372static void vpushs(addr_t v)
718fd591
MM
Michael Matz2012-04-16 01:13:25 +0200373{
374 CValue cval;
5879c854 grischka2014-04-04 20:18:39 +0200375 cval.ptr_offset = v;
718fd591
MM
Michael Matz2012-04-16 01:13:25 +0200376 vsetc(&size_type, VT_CONST, &cval);
377}
378
8eb86ab7 Thomas Preud'homme2010-05-06 02:19:00 +0200379/* push arbitrary 64bit constant */
822f4630 Urs Janssen2014-04-10 11:53:54 +0200380ST_FUNC void vpush64(int ty, unsigned long long v)
8eb86ab7 Thomas Preud'homme2010-05-06 02:19:00 +0200381{
4bc83ac3 mingodad2014-03-26 20:14:39 +0000382 CValue cval;
5879c854 grischka2014-04-04 20:18:39 +0200383 CType ctype;
8eb86ab7 Thomas Preud'homme2010-05-06 02:19:00 +0200384 ctype.t = ty;
d6d7686b grischka2013-02-08 19:07:11 +0100385 ctype.ref = NULL;
8eb86ab7
TP
Thomas Preud'homme2010-05-06 02:19:00 +0200386 cval.ull = v;
387 vsetc(&ctype, VT_CONST, &cval);
388}
389
d6d7686b grischka2013-02-08 19:07:11 +0100390/* push long long constant */
391static inline void vpushll(long long v)
392{
393 vpush64(VT_LLONG, v);
394}
395
9e11476e
MM
Michael Matz2014-01-11 23:42:58 +0100396/* push a symbol value of TYPE */
397static inline void vpushsym(CType *type, Sym *sym)
398{
399 CValue cval;
5879c854 grischka2014-04-04 20:18:39 +0200400 cval.ptr_offset = 0;
9e11476e
MM
Michael Matz2014-01-11 23:42:58 +0100401 vsetc(type, VT_CONST | VT_SYM, &cval);
402 vtop->sym = sym;
403}
404
0d1ed741 grischka2009-05-05 20:18:10 +0200405/* Return a static symbol pointing to a section */
88a3ccab grischka2009-12-20 01:53:49 +0100406ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
0d1ed741 grischka2009-05-05 20:18:10 +0200407{
408 int v;
409 Sym *sym;
410
411 v = anon_sym++;
412 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
413 sym->type.ref = type->ref;
414 sym->r = VT_CONST | VT_SYM;
415 put_extern_sym(sym, sec, offset, size);
416 return sym;
417}
418
419/* push a reference to a section offset by adding a dummy symbol */
420static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
421{
9e11476e Michael Matz2014-01-11 23:42:58 +0100422 vpushsym(type, get_sym_ref(type, sec, offset, size));
0d1ed741 grischka2009-05-05 20:18:10 +0200423}
424
425/* define a new external reference to a symbol 'v' of type 'u' */
88a3ccab grischka2009-12-20 01:53:49 +0100426ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
0d1ed741 grischka2009-05-05 20:18:10 +0200427{
428 Sym *s;
429
430 s = sym_find(v);
431 if (!s) {
432 /* push forward reference */
433 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
434 s->type.ref = type->ref;
435 s->r = r | VT_CONST | VT_SYM;
436 }
437 return s;
438}
439
32a682b8
TP
Thomas Preud'homme2011-02-07 22:42:38 +0100440/* define a new external reference to a symbol 'v' with alternate asm
441 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
442 is no alternate name (most cases) */
443static Sym *external_sym(int v, CType *type, int r, char *asm_label)
0d1ed741 grischka2009-05-05 20:18:10 +0200444{
445 Sym *s;
446
447 s = sym_find(v);
448 if (!s) {
449 /* push forward reference */
450 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
32a682b8 Thomas Preud'homme2011-02-07 22:42:38 +0100451 s->asm_label = asm_label;
0d1ed741 grischka2009-05-05 20:18:10 +0200452 s->type.t |= VT_EXTERN;
6a004ed1 grischka2009-06-17 02:09:52 +0200453 } else if (s->type.ref == func_old_type.ref) {
454 s->type.ref = type->ref;
455 s->r = r | VT_CONST | VT_SYM;
456 s->type.t |= VT_EXTERN;
457 } else if (!is_compatible_types(&s->type, type)) {
bf374a5f grischka2011-08-11 17:07:56 +0200458 tcc_error("incompatible types for redefinition of '%s'",
6a004ed1 grischka2009-06-17 02:09:52 +0200459 get_tok_str(v, NULL));
0d1ed741 grischka2009-05-05 20:18:10 +0200460 }
fbda78ae
MM
Michael Matz2014-04-14 02:53:11 +0200461 /* Merge some storage attributes. */
462 if (type->t & VT_WEAK)
463 weaken_symbol(s);
464
465 if (type->t & VT_VIS_MASK)
466 apply_visibility(s, type);
467
0d1ed741 grischka2009-05-05 20:18:10 +0200468 return s;
469}
470
471/* push a reference to global symbol v */
0de95730 grischka2009-12-20 20:33:41 +0100472ST_FUNC void vpush_global_sym(CType *type, int v)
0d1ed741 grischka2009-05-05 20:18:10 +0200473{
9e11476e Michael Matz2014-01-11 23:42:58 +0100474 vpushsym(type, external_global_sym(v, type, 0));
0d1ed741 grischka2009-05-05 20:18:10 +0200475}
476
88a3ccab grischka2009-12-20 01:53:49 +0100477ST_FUNC void vset(CType *type, int r, int v)
0d1ed741 grischka2009-05-05 20:18:10 +0200478{
9e11476e Michael Matz2014-01-11 23:42:58 +0100479 CValue cval;
0d1ed741 grischka2009-05-05 20:18:10 +0200480
481 cval.i = v;
482 vsetc(type, r, &cval);
483}
484
88a3ccab grischka2009-12-20 01:53:49 +0100485static void vseti(int r, int v)
0d1ed741 grischka2009-05-05 20:18:10 +0200486{
487 CType type;
488 type.t = VT_INT;
41e11236 grischka2009-12-19 22:10:13 +0100489 type.ref = 0;
0d1ed741 grischka2009-05-05 20:18:10 +0200490 vset(&type, r, v);
491}
492
88a3ccab grischka2009-12-20 01:53:49 +0100493ST_FUNC void vswap(void)
0d1ed741 grischka2009-05-05 20:18:10 +0200494{
c5892fe4 grischka2013-01-14 18:33:59 +0100495 SValue tmp;
9ca9c82f
MM
Michael Matz2012-04-16 02:52:15 +0200496 /* cannot let cpu flags if other instruction are generated. Also
497 avoid leaving VT_JMP anywhere except on the top of the stack
498 because it would complicate the code generator. */
499 if (vtop >= vstack) {
500 int v = vtop->r & VT_VALMASK;
501 if (v == VT_CMP || (v & ~1) == VT_JMP)
502 gv(RC_INT);
503 }
c5892fe4 grischka2013-01-14 18:33:59 +0100504 tmp = vtop[0];
505 vtop[0] = vtop[-1];
506 vtop[-1] = tmp;
63193d17 Kirill Smelkov2012-12-21 13:55:01 +0400507
c5892fe4 grischka2013-01-14 18:33:59 +0100508/* XXX: +2% overall speed possible with optimized memswap
509 *
510 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
511 */
0d1ed741 grischka2009-05-05 20:18:10 +0200512}
513
4a01eb09 grischka2009-12-19 22:41:26 +0100514ST_FUNC void vpushv(SValue *v)
0d1ed741 grischka2009-05-05 20:18:10 +0200515{
516 if (vtop >= vstack + (VSTACK_SIZE - 1))
2bd0daab grischka2014-01-06 19:56:26 +0100517 tcc_error("memory full (vstack)");
0d1ed741 grischka2009-05-05 20:18:10 +0200518 vtop++;
519 *vtop = *v;
520}
521
88a3ccab grischka2009-12-20 01:53:49 +0100522static void vdup(void)
0d1ed741 grischka2009-05-05 20:18:10 +0200523{
524 vpushv(vtop);
525}
526
527/* save r to the memory stack, and mark it as being free */
88a3ccab grischka2009-12-20 01:53:49 +0100528ST_FUNC void save_reg(int r)
0d1ed741 grischka2009-05-05 20:18:10 +0200529{
530 int l, saved, size, align;
531 SValue *p, sv;
532 CType *type;
533
534 /* modify all stack values */
535 saved = 0;
536 l = 0;
537 for(p=vstack;p<=vtop;p++) {
538 if ((p->r & VT_VALMASK) == r ||
539 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
540 /* must save value on stack if not already done */
541 if (!saved) {
542 /* NOTE: must reload 'r' because r might be equal to r2 */
543 r = p->r & VT_VALMASK;
544 /* store register in the stack */
545 type = &p->type;
546 if ((p->r & VT_LVAL) ||
547 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000548#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +0200549 type = &char_pointer_type;
550#else
551 type = &int_type;
552#endif
553 size = type_size(type, &align);
554 loc = (loc - size) & -align;
555 sv.type.t = type->t;
556 sv.r = VT_LOCAL | VT_LVAL;
557 sv.c.ul = loc;
558 store(r, &sv);
559#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
560 /* x86 specific: need to pop fp register ST0 if saved */
561 if (r == TREG_ST0) {
c3701df1 Soloist Deng2009-06-08 19:26:19 +0200562 o(0xd8dd); /* fstp %st(0) */
0d1ed741 grischka2009-05-05 20:18:10 +0200563 }
564#endif
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000565#if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +0200566 /* special long long case */
567 if ((type->t & VT_BTYPE) == VT_LLONG) {
568 sv.c.ul += 4;
569 store(p->r2, &sv);
570 }
571#endif
572 l = loc;
573 saved = 1;
574 }
575 /* mark that stack entry as being saved on the stack */
576 if (p->r & VT_LVAL) {
577 /* also clear the bounded flag because the
578 relocation address of the function was stored in
579 p->c.ul */
580 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
581 } else {
582 p->r = lvalue_type(p->type.t) | VT_LOCAL;
583 }
584 p->r2 = VT_CONST;
585 p->c.ul = l;
586 }
587 }
588}
589
88a3ccab grischka2009-12-20 01:53:49 +0100590#ifdef TCC_TARGET_ARM
0d1ed741 grischka2009-05-05 20:18:10 +0200591/* find a register of class 'rc2' with at most one reference on stack.
592 * If none, call get_reg(rc) */
88a3ccab grischka2009-12-20 01:53:49 +0100593ST_FUNC int get_reg_ex(int rc, int rc2)
0d1ed741 grischka2009-05-05 20:18:10 +0200594{
595 int r;
596 SValue *p;
597
598 for(r=0;r<NB_REGS;r++) {
599 if (reg_classes[r] & rc2) {
600 int n;
601 n=0;
602 for(p = vstack; p <= vtop; p++) {
603 if ((p->r & VT_VALMASK) == r ||
604 (p->r2 & VT_VALMASK) == r)
605 n++;
606 }
607 if (n <= 1)
608 return r;
609 }
610 }
611 return get_reg(rc);
612}
88a3ccab grischka2009-12-20 01:53:49 +0100613#endif
0d1ed741 grischka2009-05-05 20:18:10 +0200614
615/* find a free register of class 'rc'. If none, save one register */
88a3ccab grischka2009-12-20 01:53:49 +0100616ST_FUNC int get_reg(int rc)
0d1ed741 grischka2009-05-05 20:18:10 +0200617{
618 int r;
619 SValue *p;
620
621 /* find a free register */
622 for(r=0;r<NB_REGS;r++) {
623 if (reg_classes[r] & rc) {
624 for(p=vstack;p<=vtop;p++) {
625 if ((p->r & VT_VALMASK) == r ||
626 (p->r2 & VT_VALMASK) == r)
627 goto notfound;
628 }
629 return r;
630 }
631 notfound: ;
632 }
633
634 /* no register left : free the first one on the stack (VERY
635 IMPORTANT to start from the bottom to ensure that we don't
636 spill registers used in gen_opi()) */
637 for(p=vstack;p<=vtop;p++) {
d1694f7d
TP
Thomas Preud'homme2012-07-11 23:39:05 +0200638 /* look at second register (if long long) */
639 r = p->r2 & VT_VALMASK;
0d1ed741 grischka2009-05-05 20:18:10 +0200640 if (r < VT_CONST && (reg_classes[r] & rc))
641 goto save_found;
d1694f7d Thomas Preud'homme2012-07-11 23:39:05 +0200642 r = p->r & VT_VALMASK;
0d1ed741 grischka2009-05-05 20:18:10 +0200643 if (r < VT_CONST && (reg_classes[r] & rc)) {
644 save_found:
645 save_reg(r);
646 return r;
647 }
648 }
649 /* Should never comes here */
650 return -1;
651}
652
653/* save registers up to (vtop - n) stack entry */
88a3ccab grischka2009-12-20 01:53:49 +0100654ST_FUNC void save_regs(int n)
0d1ed741 grischka2009-05-05 20:18:10 +0200655{
656 int r;
657 SValue *p, *p1;
658 p1 = vtop - n;
659 for(p = vstack;p <= p1; p++) {
660 r = p->r & VT_VALMASK;
661 if (r < VT_CONST) {
662 save_reg(r);
663 }
664 }
665}
666
946afd23 James Lyon2013-04-19 18:31:24 +0100667/* move register 's' (of type 't') to 'r', and flush previous value of r to memory
0d1ed741 grischka2009-05-05 20:18:10 +0200668 if needed */
946afd23 James Lyon2013-04-19 18:31:24 +0100669static void move_reg(int r, int s, int t)
0d1ed741 grischka2009-05-05 20:18:10 +0200670{
671 SValue sv;
672
673 if (r != s) {
674 save_reg(r);
946afd23
JL
James Lyon2013-04-19 18:31:24 +0100675 sv.type.t = t;
676 sv.type.ref = NULL;
0d1ed741 grischka2009-05-05 20:18:10 +0200677 sv.r = s;
678 sv.c.ul = 0;
679 load(r, &sv);
680 }
681}
682
683/* get address of vtop (vtop MUST BE an lvalue) */
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000684ST_FUNC void gaddrof(void)
0d1ed741 grischka2009-05-05 20:18:10 +0200685{
5bcc3eed Thomas Preud'homme2015-03-10 23:23:00 +0800686 if (vtop->r & VT_REF && !nocode_wanted)
8d107d9f grischka2010-12-17 13:15:01 +0100687 gv(RC_INT);
0d1ed741 grischka2009-05-05 20:18:10 +0200688 vtop->r &= ~VT_LVAL;
689 /* tricky: if saved lvalue, then we can go back to lvalue */
690 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
691 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
8d107d9f grischka2010-12-17 13:15:01 +0100692
693
0d1ed741 grischka2009-05-05 20:18:10 +0200694}
695
696#ifdef CONFIG_TCC_BCHECK
697/* generate lvalue bound code */
88a3ccab grischka2009-12-20 01:53:49 +0100698static void gbound(void)
0d1ed741 grischka2009-05-05 20:18:10 +0200699{
700 int lval_type;
701 CType type1;
702
703 vtop->r &= ~VT_MUSTBOUND;
704 /* if lvalue, then use checking code before dereferencing */
705 if (vtop->r & VT_LVAL) {
706 /* if not VT_BOUNDED value, then make one */
707 if (!(vtop->r & VT_BOUNDED)) {
708 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
709 /* must save type because we must set it to int to get pointer */
710 type1 = vtop->type;
559675b9 seyko2015-04-10 15:17:22 +0300711 vtop->type.t = VT_PTR;
0d1ed741 grischka2009-05-05 20:18:10 +0200712 gaddrof();
713 vpushi(0);
714 gen_bounded_ptr_add();
715 vtop->r |= lval_type;
716 vtop->type = type1;
717 }
718 /* then check for dereferencing */
719 gen_bounded_ptr_deref();
720 }
721}
722#endif
723
724/* store vtop a register belonging to class 'rc'. lvalues are
725 converted to values. Cannot be used if cannot be converted to
726 register value (such as structures). */
88a3ccab grischka2009-12-20 01:53:49 +0100727ST_FUNC int gv(int rc)
0d1ed741 grischka2009-05-05 20:18:10 +0200728{
ee06ef9d Thomas Preud'homme2011-05-16 14:15:32 +0200729 int r, bit_pos, bit_size, size, align, i;
ee06ef9d Thomas Preud'homme2011-05-16 14:15:32 +0200730 int rc2;
0d1ed741 grischka2009-05-05 20:18:10 +0200731
732 /* NOTE: get_reg can modify vstack[] */
733 if (vtop->type.t & VT_BITFIELD) {
734 CType type;
735 int bits = 32;
736 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
737 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
738 /* remove bit field info to avoid loops */
739 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
740 /* cast to int to propagate signedness in following ops */
741 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
742 type.t = VT_LLONG;
743 bits = 64;
744 } else
745 type.t = VT_INT;
746 if((vtop->type.t & VT_UNSIGNED) ||
747 (vtop->type.t & VT_BTYPE) == VT_BOOL)
748 type.t |= VT_UNSIGNED;
749 gen_cast(&type);
750 /* generate shifts */
751 vpushi(bits - (bit_pos + bit_size));
752 gen_op(TOK_SHL);
753 vpushi(bits - bit_size);
754 /* NOTE: transformed to SHR if unsigned */
755 gen_op(TOK_SAR);
756 r = gv(rc);
757 } else {
758 if (is_float(vtop->type.t) &&
759 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
760 Sym *sym;
761 int *ptr;
762 unsigned long offset;
763#if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
764 CValue check;
765#endif
766
767 /* XXX: unify with initializers handling ? */
768 /* CPUs usually cannot use float constants, so we store them
769 generically in data segment */
770 size = type_size(&vtop->type, &align);
771 offset = (data_section->data_offset + align - 1) & -align;
772 data_section->data_offset = offset;
773 /* XXX: not portable yet */
774#if defined(__i386__) || defined(__x86_64__)
775 /* Zero pad x87 tenbyte long doubles */
f115c123 grischka2011-08-06 16:08:03 +0200776 if (size == LDOUBLE_SIZE) {
0d1ed741 grischka2009-05-05 20:18:10 +0200777 vtop->c.tab[2] &= 0xffff;
f115c123 grischka2011-08-06 16:08:03 +0200778#if LDOUBLE_SIZE == 16
779 vtop->c.tab[3] = 0;
780#endif
781 }
0d1ed741 grischka2009-05-05 20:18:10 +0200782#endif
783 ptr = section_ptr_add(data_section, size);
784 size = size >> 2;
785#if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
786 check.d = 1;
787 if(check.tab[0])
788 for(i=0;i<size;i++)
789 ptr[i] = vtop->c.tab[size-1-i];
790 else
791#endif
792 for(i=0;i<size;i++)
793 ptr[i] = vtop->c.tab[i];
794 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
795 vtop->r |= VT_LVAL | VT_SYM;
796 vtop->sym = sym;
5879c854 grischka2014-04-04 20:18:39 +0200797 vtop->c.ptr_offset = 0;
0d1ed741 grischka2009-05-05 20:18:10 +0200798 }
799#ifdef CONFIG_TCC_BCHECK
800 if (vtop->r & VT_MUSTBOUND)
801 gbound();
802#endif
803
804 r = vtop->r & VT_VALMASK;
cbce6d2b James Lyon2013-04-19 22:05:49 +0100805 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000806#ifndef TCC_TARGET_ARM64
0d1ed741 grischka2009-05-05 20:18:10 +0200807 if (rc == RC_IRET)
808 rc2 = RC_LRET;
0e17671f
JL
James Lyon2013-04-19 15:33:16 +0100809#ifdef TCC_TARGET_X86_64
810 else if (rc == RC_FRET)
811 rc2 = RC_QRET;
ee06ef9d Thomas Preud'homme2011-05-16 14:15:32 +0200812#endif
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000813#endif
0e17671f James Lyon2013-04-19 15:33:16 +0100814
0d1ed741 grischka2009-05-05 20:18:10 +0200815 /* need to reload if:
816 - constant
817 - lvalue (need to dereference pointer)
818 - already a register, but not in the right class */
ab4a4ab2 grischka2009-12-17 21:09:53 +0100819 if (r >= VT_CONST
820 || (vtop->r & VT_LVAL)
821 || !(reg_classes[r] & rc)
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000822#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
0e17671f
JL
James Lyon2013-04-19 15:33:16 +0100823 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
824 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
825#else
ab4a4ab2 grischka2009-12-17 21:09:53 +0100826 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
827#endif
828 )
829 {
0d1ed741 grischka2009-05-05 20:18:10 +0200830 r = get_reg(rc);
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000831#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
0e17671f James Lyon2013-04-19 15:33:16 +0100832 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
23f73e92 James Lyon2013-04-19 22:55:09 +0100833 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
0e17671f James Lyon2013-04-19 15:33:16 +0100834#else
0d1ed741 grischka2009-05-05 20:18:10 +0200835 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
23f73e92 James Lyon2013-04-19 22:55:09 +0100836 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
0f5942c6 Thomas Preud'homme2013-09-24 15:36:04 +0200837 unsigned long long ll;
0e17671f James Lyon2013-04-19 15:33:16 +0100838#endif
23f73e92 James Lyon2013-04-19 22:55:09 +0100839 int r2, original_type;
23f73e92 James Lyon2013-04-19 22:55:09 +0100840 original_type = vtop->type.t;
0d1ed741 grischka2009-05-05 20:18:10 +0200841 /* two register type load : expand to two words
842 temporarily */
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000843#if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +0200844 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
845 /* load constant */
846 ll = vtop->c.ull;
847 vtop->c.ui = ll; /* first word */
848 load(r, vtop);
849 vtop->r = r; /* save register value */
850 vpushi(ll >> 32); /* second word */
0e17671f
JL
James Lyon2013-04-19 15:33:16 +0100851 } else
852#endif
853 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
0d1ed741 grischka2009-05-05 20:18:10 +0200854 (vtop->r & VT_LVAL)) {
855 /* We do not want to modifier the long long
856 pointer here, so the safest (and less
857 efficient) is to save all the other registers
858 in the stack. XXX: totally inefficient. */
859 save_regs(1);
860 /* load from memory */
0e17671f James Lyon2013-04-19 15:33:16 +0100861 vtop->type.t = load_type;
0d1ed741 grischka2009-05-05 20:18:10 +0200862 load(r, vtop);
863 vdup();
864 vtop[-1].r = r; /* save register value */
865 /* increment pointer to get second word */
0e17671f James Lyon2013-04-19 15:33:16 +0100866 vtop->type.t = addr_type;
0d1ed741 grischka2009-05-05 20:18:10 +0200867 gaddrof();
0e17671f James Lyon2013-04-19 15:33:16 +0100868 vpushi(load_size);
0d1ed741 grischka2009-05-05 20:18:10 +0200869 gen_op('+');
870 vtop->r |= VT_LVAL;
0e17671f James Lyon2013-04-19 15:33:16 +0100871 vtop->type.t = load_type;
0d1ed741 grischka2009-05-05 20:18:10 +0200872 } else {
873 /* move registers */
874 load(r, vtop);
875 vdup();
876 vtop[-1].r = r; /* save register value */
877 vtop->r = vtop[-1].r2;
878 }
d1694f7d
TP
Thomas Preud'homme2012-07-11 23:39:05 +0200879 /* Allocate second register. Here we rely on the fact that
880 get_reg() tries first to free r2 of an SValue. */
0d1ed741 grischka2009-05-05 20:18:10 +0200881 r2 = get_reg(rc2);
882 load(r2, vtop);
883 vpop();
884 /* write second register */
885 vtop->r2 = r2;
23f73e92 James Lyon2013-04-19 22:55:09 +0100886 vtop->type.t = original_type;
0e17671f James Lyon2013-04-19 15:33:16 +0100887 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
0d1ed741 grischka2009-05-05 20:18:10 +0200888 int t1, t;
889 /* lvalue of scalar type : need to use lvalue type
890 because of possible cast */
891 t = vtop->type.t;
892 t1 = t;
893 /* compute memory access type */
5c35ba66 James Lyon2013-04-24 02:19:15 +0100894 if (vtop->r & VT_REF)
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000895#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5c35ba66
JL
James Lyon2013-04-24 02:19:15 +0100896 t = VT_PTR;
897#else
898 t = VT_INT;
899#endif
900 else if (vtop->r & VT_LVAL_BYTE)
0d1ed741 grischka2009-05-05 20:18:10 +0200901 t = VT_BYTE;
902 else if (vtop->r & VT_LVAL_SHORT)
903 t = VT_SHORT;
904 if (vtop->r & VT_LVAL_UNSIGNED)
905 t |= VT_UNSIGNED;
906 vtop->type.t = t;
907 load(r, vtop);
908 /* restore wanted type */
909 vtop->type.t = t1;
910 } else {
911 /* one register type load */
912 load(r, vtop);
913 }
914 }
915 vtop->r = r;
916#ifdef TCC_TARGET_C67
917 /* uses register pairs for doubles */
918 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
919 vtop->r2 = r+1;
920#endif
921 }
922 return r;
923}
924
925/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
88a3ccab grischka2009-12-20 01:53:49 +0100926ST_FUNC void gv2(int rc1, int rc2)
0d1ed741 grischka2009-05-05 20:18:10 +0200927{
928 int v;
929
930 /* generate more generic register first. But VT_JMP or VT_CMP
931 values must be generated first in all cases to avoid possible
932 reload errors */
933 v = vtop[0].r & VT_VALMASK;
934 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
935 vswap();
936 gv(rc1);
937 vswap();
938 gv(rc2);
939 /* test if reload is needed for first register */
940 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
941 vswap();
942 gv(rc1);
943 vswap();
944 }
945 } else {
946 gv(rc2);
947 vswap();
948 gv(rc1);
949 vswap();
950 /* test if reload is needed for first register */
951 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
952 gv(rc2);
953 }
954 }
955}
956
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000957#ifndef TCC_TARGET_ARM64
0d1ed741 grischka2009-05-05 20:18:10 +0200958/* wrapper around RC_FRET to return a register by type */
88a3ccab grischka2009-12-20 01:53:49 +0100959static int rc_fret(int t)
0d1ed741 grischka2009-05-05 20:18:10 +0200960{
961#ifdef TCC_TARGET_X86_64
962 if (t == VT_LDOUBLE) {
963 return RC_ST0;
964 }
965#endif
966 return RC_FRET;
967}
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +0000968#endif
0d1ed741 grischka2009-05-05 20:18:10 +0200969
970/* wrapper around REG_FRET to return a register by type */
88a3ccab grischka2009-12-20 01:53:49 +0100971static int reg_fret(int t)
0d1ed741 grischka2009-05-05 20:18:10 +0200972{
973#ifdef TCC_TARGET_X86_64
974 if (t == VT_LDOUBLE) {
975 return TREG_ST0;
976 }
977#endif
978 return REG_FRET;
979}
980
981/* expand long long on stack in two int registers */
88a3ccab grischka2009-12-20 01:53:49 +0100982static void lexpand(void)
0d1ed741 grischka2009-05-05 20:18:10 +0200983{
984 int u;
985
b0b5165d Thomas Preud'homme2014-02-06 20:51:47 +0800986 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
0d1ed741 grischka2009-05-05 20:18:10 +0200987 gv(RC_INT);
988 vdup();
989 vtop[0].r = vtop[-1].r2;
990 vtop[0].r2 = VT_CONST;
991 vtop[-1].r2 = VT_CONST;
992 vtop[0].type.t = VT_INT | u;
993 vtop[-1].type.t = VT_INT | u;
994}
995
996#ifdef TCC_TARGET_ARM
997/* expand long long on stack */
88a3ccab grischka2009-12-20 01:53:49 +0100998ST_FUNC void lexpand_nr(void)
0d1ed741 grischka2009-05-05 20:18:10 +0200999{
1000 int u,v;
1001
b0b5165d Thomas Preud'homme2014-02-06 20:51:47 +08001002 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
0d1ed741 grischka2009-05-05 20:18:10 +02001003 vdup();
1004 vtop->r2 = VT_CONST;
1005 vtop->type.t = VT_INT | u;
1006 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
1007 if (v == VT_CONST) {
1008 vtop[-1].c.ui = vtop->c.ull;
1009 vtop->c.ui = vtop->c.ull >> 32;
1010 vtop->r = VT_CONST;
1011 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1012 vtop->c.ui += 4;
1013 vtop->r = vtop[-1].r;
1014 } else if (v > VT_CONST) {
1015 vtop--;
1016 lexpand();
1017 } else
1018 vtop->r = vtop[-1].r2;
1019 vtop[-1].r2 = VT_CONST;
1020 vtop[-1].type.t = VT_INT | u;
1021}
1022#endif
1023
1024/* build a long long from two ints */
88a3ccab grischka2009-12-20 01:53:49 +01001025static void lbuild(int t)
0d1ed741 grischka2009-05-05 20:18:10 +02001026{
1027 gv2(RC_INT, RC_INT);
1028 vtop[-1].r2 = vtop[0].r;
1029 vtop[-1].type.t = t;
1030 vpop();
1031}
1032
1033/* rotate n first stack elements to the bottom
1034 I1 ... In -> I2 ... In I1 [top is right]
1035*/
bfb00494 Thomas Preud'homme2012-03-14 15:39:16 +01001036ST_FUNC void vrotb(int n)
0d1ed741 grischka2009-05-05 20:18:10 +02001037{
1038 int i;
1039 SValue tmp;
1040
1041 tmp = vtop[-n + 1];
1042 for(i=-n+1;i!=0;i++)
1043 vtop[i] = vtop[i+1];
1044 vtop[0] = tmp;
1045}
1046
7f6095bf
TP
Thomas Preud'homme2011-12-10 07:22:09 +01001047/* rotate the n elements before entry e towards the top
1048 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
0d1ed741 grischka2009-05-05 20:18:10 +02001049 */
7f6095bf Thomas Preud'homme2011-12-10 07:22:09 +01001050ST_FUNC void vrote(SValue *e, int n)
0d1ed741 grischka2009-05-05 20:18:10 +02001051{
1052 int i;
1053 SValue tmp;
1054
7f6095bf Thomas Preud'homme2011-12-10 07:22:09 +01001055 tmp = *e;
0d1ed741 grischka2009-05-05 20:18:10 +02001056 for(i = 0;i < n - 1; i++)
7f6095bf
TP
Thomas Preud'homme2011-12-10 07:22:09 +01001057 e[-i] = e[-i - 1];
1058 e[-n + 1] = tmp;
1059}
1060
1061/* rotate n first stack elements to the top
1062 I1 ... In -> In I1 ... I(n-1) [top is right]
1063 */
1064ST_FUNC void vrott(int n)
1065{
1066 vrote(vtop, n);
0d1ed741 grischka2009-05-05 20:18:10 +02001067}
1068
0d1ed741 grischka2009-05-05 20:18:10 +02001069/* pop stack value */
88a3ccab grischka2009-12-20 01:53:49 +01001070ST_FUNC void vpop(void)
0d1ed741 grischka2009-05-05 20:18:10 +02001071{
1072 int v;
1073 v = vtop->r & VT_VALMASK;
1074#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1075 /* for x86, we need to pop the FP stack */
1076 if (v == TREG_ST0 && !nocode_wanted) {
c3701df1 Soloist Deng2009-06-08 19:26:19 +02001077 o(0xd8dd); /* fstp %st(0) */
0d1ed741 grischka2009-05-05 20:18:10 +02001078 } else
1079#endif
1080 if (v == VT_JMP || v == VT_JMPI) {
1081 /* need to put correct jump if && or || without test */
1082 gsym(vtop->c.ul);
1083 }
1084 vtop--;
1085}
1086
1087/* convert stack entry to register and duplicate its value in another
1088 register */
88a3ccab grischka2009-12-20 01:53:49 +01001089static void gv_dup(void)
0d1ed741 grischka2009-05-05 20:18:10 +02001090{
1091 int rc, t, r, r1;
1092 SValue sv;
1093
1094 t = vtop->type.t;
1095 if ((t & VT_BTYPE) == VT_LLONG) {
1096 lexpand();
1097 gv_dup();
1098 vswap();
1099 vrotb(3);
1100 gv_dup();
1101 vrotb(4);
1102 /* stack: H L L1 H1 */
1103 lbuild(t);
1104 vrotb(3);
1105 vrotb(3);
1106 vswap();
1107 lbuild(t);
1108 vswap();
1109 } else {
1110 /* duplicate value */
1111 rc = RC_INT;
1112 sv.type.t = VT_INT;
1113 if (is_float(t)) {
1114 rc = RC_FLOAT;
1115#ifdef TCC_TARGET_X86_64
1116 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1117 rc = RC_ST0;
1118 }
1119#endif
1120 sv.type.t = t;
1121 }
1122 r = gv(rc);
1123 r1 = get_reg(rc);
1124 sv.r = r;
1125 sv.c.ul = 0;
1126 load(r1, &sv); /* move r to r1 */
1127 vdup();
1128 /* duplicates value */
c3701df1
SD
Soloist Deng2009-06-08 19:26:19 +02001129 if (r != r1)
1130 vtop->r = r1;
0d1ed741 grischka2009-05-05 20:18:10 +02001131 }
1132}
1133
eda2c756
TP
Thomas Preud'homme2013-12-31 23:51:20 +08001134/* Generate value test
1135 *
1136 * Generate a test for any value (jump, comparison and integers) */
822f4630 Urs Janssen2014-04-10 11:53:54 +02001137ST_FUNC int gvtst(int inv, int t)
eda2c756
TP
Thomas Preud'homme2013-12-31 23:51:20 +08001138{
1139 int v = vtop->r & VT_VALMASK;
1140 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1141 vpushi(0);
1142 gen_op(TOK_NE);
1143 }
1144 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1145 /* constant jmp optimization */
1146 if ((vtop->c.i != 0) != inv)
1147 t = gjmp(t);
1148 vtop--;
1149 return t;
1150 }
1151 return gtst(inv, t);
1152}
1153
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00001154#if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +02001155/* generate CPU independent (unsigned) long long operations */
88a3ccab grischka2009-12-20 01:53:49 +01001156static void gen_opl(int op)
0d1ed741 grischka2009-05-05 20:18:10 +02001157{
1158 int t, a, b, op1, c, i;
1159 int func;
1160 unsigned short reg_iret = REG_IRET;
1161 unsigned short reg_lret = REG_LRET;
1162 SValue tmp;
1163
1164 switch(op) {
1165 case '/':
1166 case TOK_PDIV:
1167 func = TOK___divdi3;
1168 goto gen_func;
1169 case TOK_UDIV:
1170 func = TOK___udivdi3;
1171 goto gen_func;
1172 case '%':
1173 func = TOK___moddi3;
1174 goto gen_mod_func;
1175 case TOK_UMOD:
1176 func = TOK___umoddi3;
1177 gen_mod_func:
1178#ifdef TCC_ARM_EABI
1179 reg_iret = TREG_R2;
1180 reg_lret = TREG_R3;
1181#endif
1182 gen_func:
1183 /* call generic long long function */
1184 vpush_global_sym(&func_old_type, func);
1185 vrott(3);
1186 gfunc_call(2);
1187 vpushi(0);
1188 vtop->r = reg_iret;
1189 vtop->r2 = reg_lret;
1190 break;
1191 case '^':
1192 case '&':
1193 case '|':
1194 case '*':
1195 case '+':
1196 case '-':
1197 t = vtop->type.t;
1198 vswap();
1199 lexpand();
1200 vrotb(3);
1201 lexpand();
1202 /* stack: L1 H1 L2 H2 */
1203 tmp = vtop[0];
1204 vtop[0] = vtop[-3];
1205 vtop[-3] = tmp;
1206 tmp = vtop[-2];
1207 vtop[-2] = vtop[-3];
1208 vtop[-3] = tmp;
1209 vswap();
1210 /* stack: H1 H2 L1 L2 */
1211 if (op == '*') {
1212 vpushv(vtop - 1);
1213 vpushv(vtop - 1);
1214 gen_op(TOK_UMULL);
1215 lexpand();
1216 /* stack: H1 H2 L1 L2 ML MH */
1217 for(i=0;i<4;i++)
1218 vrotb(6);
1219 /* stack: ML MH H1 H2 L1 L2 */
1220 tmp = vtop[0];
1221 vtop[0] = vtop[-2];
1222 vtop[-2] = tmp;
1223 /* stack: ML MH H1 L2 H2 L1 */
1224 gen_op('*');
1225 vrotb(3);
1226 vrotb(3);
1227 gen_op('*');
1228 /* stack: ML MH M1 M2 */
1229 gen_op('+');
1230 gen_op('+');
1231 } else if (op == '+' || op == '-') {
1232 /* XXX: add non carry method too (for MIPS or alpha) */
1233 if (op == '+')
1234 op1 = TOK_ADDC1;
1235 else
1236 op1 = TOK_SUBC1;
1237 gen_op(op1);
1238 /* stack: H1 H2 (L1 op L2) */
1239 vrotb(3);
1240 vrotb(3);
1241 gen_op(op1 + 1); /* TOK_xxxC2 */
1242 } else {
1243 gen_op(op);
1244 /* stack: H1 H2 (L1 op L2) */
1245 vrotb(3);
1246 vrotb(3);
1247 /* stack: (L1 op L2) H1 H2 */
1248 gen_op(op);
1249 /* stack: (L1 op L2) (H1 op H2) */
1250 }
1251 /* stack: L H */
1252 lbuild(t);
1253 break;
1254 case TOK_SAR:
1255 case TOK_SHR:
1256 case TOK_SHL:
1257 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1258 t = vtop[-1].type.t;
1259 vswap();
1260 lexpand();
1261 vrotb(3);
1262 /* stack: L H shift */
1263 c = (int)vtop->c.i;
1264 /* constant: simpler */
1265 /* NOTE: all comments are for SHL. the other cases are
1266 done by swaping words */
1267 vpop();
1268 if (op != TOK_SHL)
1269 vswap();
1270 if (c >= 32) {
1271 /* stack: L H */
1272 vpop();
1273 if (c > 32) {
1274 vpushi(c - 32);
1275 gen_op(op);
1276 }
1277 if (op != TOK_SAR) {
1278 vpushi(0);
1279 } else {
1280 gv_dup();
1281 vpushi(31);
1282 gen_op(TOK_SAR);
1283 }
1284 vswap();
1285 } else {
1286 vswap();
1287 gv_dup();
1288 /* stack: H L L */
1289 vpushi(c);
1290 gen_op(op);
1291 vswap();
1292 vpushi(32 - c);
1293 if (op == TOK_SHL)
1294 gen_op(TOK_SHR);
1295 else
1296 gen_op(TOK_SHL);
1297 vrotb(3);
1298 /* stack: L L H */
1299 vpushi(c);
1300 if (op == TOK_SHL)
1301 gen_op(TOK_SHL);
1302 else
1303 gen_op(TOK_SHR);
1304 gen_op('|');
1305 }
1306 if (op != TOK_SHL)
1307 vswap();
1308 lbuild(t);
1309 } else {
1310 /* XXX: should provide a faster fallback on x86 ? */
1311 switch(op) {
1312 case TOK_SAR:
1313 func = TOK___ashrdi3;
1314 goto gen_func;
1315 case TOK_SHR:
1316 func = TOK___lshrdi3;
1317 goto gen_func;
1318 case TOK_SHL:
1319 func = TOK___ashldi3;
1320 goto gen_func;
1321 }
1322 }
1323 break;
1324 default:
1325 /* compare operations */
1326 t = vtop->type.t;
1327 vswap();
1328 lexpand();
1329 vrotb(3);
1330 lexpand();
1331 /* stack: L1 H1 L2 H2 */
1332 tmp = vtop[-1];
1333 vtop[-1] = vtop[-2];
1334 vtop[-2] = tmp;
1335 /* stack: L1 L2 H1 H2 */
1336 /* compare high */
1337 op1 = op;
1338 /* when values are equal, we need to compare low words. since
1339 the jump is inverted, we invert the test too. */
1340 if (op1 == TOK_LT)
1341 op1 = TOK_LE;
1342 else if (op1 == TOK_GT)
1343 op1 = TOK_GE;
1344 else if (op1 == TOK_ULT)
1345 op1 = TOK_ULE;
1346 else if (op1 == TOK_UGT)
1347 op1 = TOK_UGE;
1348 a = 0;
1349 b = 0;
1350 gen_op(op1);
1351 if (op1 != TOK_NE) {
eda2c756 Thomas Preud'homme2013-12-31 23:51:20 +08001352 a = gvtst(1, 0);
0d1ed741 grischka2009-05-05 20:18:10 +02001353 }
1354 if (op != TOK_EQ) {
1355 /* generate non equal test */
1356 /* XXX: NOT PORTABLE yet */
1357 if (a == 0) {
eda2c756 Thomas Preud'homme2013-12-31 23:51:20 +08001358 b = gvtst(0, 0);
0d1ed741 grischka2009-05-05 20:18:10 +02001359 } else {
1360#if defined(TCC_TARGET_I386)
1361 b = psym(0x850f, 0);
1362#elif defined(TCC_TARGET_ARM)
1363 b = ind;
1364 o(0x1A000000 | encbranch(ind, 0, 1));
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00001365#elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
bf374a5f grischka2011-08-11 17:07:56 +02001366 tcc_error("not implemented");
0d1ed741 grischka2009-05-05 20:18:10 +02001367#else
1368#error not supported
1369#endif
1370 }
1371 }
1372 /* compare low. Always unsigned */
1373 op1 = op;
1374 if (op1 == TOK_LT)
1375 op1 = TOK_ULT;
1376 else if (op1 == TOK_LE)
1377 op1 = TOK_ULE;
1378 else if (op1 == TOK_GT)
1379 op1 = TOK_UGT;
1380 else if (op1 == TOK_GE)
1381 op1 = TOK_UGE;
1382 gen_op(op1);
eda2c756 Thomas Preud'homme2013-12-31 23:51:20 +08001383 a = gvtst(1, a);
0d1ed741 grischka2009-05-05 20:18:10 +02001384 gsym(b);
1385 vseti(VT_JMPI, a);
1386 break;
1387 }
1388}
1389#endif
1390
1391/* handle integer constant optimizations and various machine
1392 independent opt */
88a3ccab grischka2009-12-20 01:53:49 +01001393static void gen_opic(int op)
0d1ed741 grischka2009-05-05 20:18:10 +02001394{
1395 int c1, c2, t1, t2, n;
1396 SValue *v1, *v2;
1397 long long l1, l2;
1398 typedef unsigned long long U;
1399
1400 v1 = vtop - 1;
1401 v2 = vtop;
1402 t1 = v1->type.t & VT_BTYPE;
1403 t2 = v2->type.t & VT_BTYPE;
1404
1405 if (t1 == VT_LLONG)
1406 l1 = v1->c.ll;
1407 else if (v1->type.t & VT_UNSIGNED)
1408 l1 = v1->c.ui;
1409 else
1410 l1 = v1->c.i;
1411
1412 if (t2 == VT_LLONG)
1413 l2 = v2->c.ll;
1414 else if (v2->type.t & VT_UNSIGNED)
1415 l2 = v2->c.ui;
1416 else
1417 l2 = v2->c.i;
1418
1419 /* currently, we cannot do computations with forward symbols */
1420 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1421 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1422 if (c1 && c2) {
1423 switch(op) {
1424 case '+': l1 += l2; break;
1425 case '-': l1 -= l2; break;
1426 case '&': l1 &= l2; break;
1427 case '^': l1 ^= l2; break;
1428 case '|': l1 |= l2; break;
1429 case '*': l1 *= l2; break;
1430
1431 case TOK_PDIV:
1432 case '/':
1433 case '%':
1434 case TOK_UDIV:
1435 case TOK_UMOD:
1436 /* if division by zero, generate explicit division */
1437 if (l2 == 0) {
1438 if (const_wanted)
bf374a5f grischka2011-08-11 17:07:56 +02001439 tcc_error("division by zero in constant");
0d1ed741 grischka2009-05-05 20:18:10 +02001440 goto general_case;
1441 }
1442 switch(op) {
1443 default: l1 /= l2; break;
1444 case '%': l1 %= l2; break;
1445 case TOK_UDIV: l1 = (U)l1 / l2; break;
1446 case TOK_UMOD: l1 = (U)l1 % l2; break;
1447 }
1448 break;
1449 case TOK_SHL: l1 <<= l2; break;
1450 case TOK_SHR: l1 = (U)l1 >> l2; break;
1451 case TOK_SAR: l1 >>= l2; break;
1452 /* tests */
1453 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1454 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1455 case TOK_EQ: l1 = l1 == l2; break;
1456 case TOK_NE: l1 = l1 != l2; break;
1457 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1458 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1459 case TOK_LT: l1 = l1 < l2; break;
1460 case TOK_GE: l1 = l1 >= l2; break;
1461 case TOK_LE: l1 = l1 <= l2; break;
1462 case TOK_GT: l1 = l1 > l2; break;
1463 /* logical */
1464 case TOK_LAND: l1 = l1 && l2; break;
1465 case TOK_LOR: l1 = l1 || l2; break;
1466 default:
1467 goto general_case;
1468 }
1469 v1->c.ll = l1;
1470 vtop--;
1471 } else {
1472 /* if commutative ops, put c2 as constant */
1473 if (c1 && (op == '+' || op == '&' || op == '^' ||
1474 op == '|' || op == '*')) {
1475 vswap();
1476 c2 = c1; //c = c1, c1 = c2, c2 = c;
1477 l2 = l1; //l = l1, l1 = l2, l2 = l;
1478 }
ac70e6b8
EGE
Edmund Grimley Evans2015-03-07 11:25:27 +00001479 if (!const_wanted &&
1480 c1 && ((l1 == 0 &&
1481 (op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) ||
1482 (l1 == -1 && op == TOK_SAR))) {
1483 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1484 vtop--;
1485 } else if (!const_wanted &&
1486 c2 && ((l2 == 0 && (op == '&' || op == '*')) ||
1487 (l2 == -1 && op == '|') ||
1488 (l2 == 0xffffffff && t2 != VT_LLONG && op == '|') ||
1489 (l2 == 1 && (op == '%' || op == TOK_UMOD)))) {
1490 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1491 if (l2 == 1)
1492 vtop->c.ll = 0;
1493 vswap();
1494 vtop--;
1495 } else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1496 op == TOK_PDIV) &&
1497 l2 == 1) ||
1498 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1499 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1500 l2 == 0) ||
1501 (op == '&' &&
1502 l2 == -1))) {
1503 /* filter out NOP operations like x*1, x-0, x&-1... */
0d1ed741 grischka2009-05-05 20:18:10 +02001504 vtop--;
1505 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1506 /* try to use shifts instead of muls or divs */
1507 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1508 n = -1;
1509 while (l2) {
1510 l2 >>= 1;
1511 n++;
1512 }
1513 vtop->c.ll = n;
1514 if (op == '*')
1515 op = TOK_SHL;
1516 else if (op == TOK_PDIV)
1517 op = TOK_SAR;
1518 else
1519 op = TOK_SHR;
1520 }
1521 goto general_case;
1522 } else if (c2 && (op == '+' || op == '-') &&
2341ee51 grischka2010-01-14 20:55:51 +01001523 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1524 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
0d1ed741 grischka2009-05-05 20:18:10 +02001525 /* symbol + constant case */
1526 if (op == '-')
1527 l2 = -l2;
1528 vtop--;
1529 vtop->c.ll += l2;
1530 } else {
1531 general_case:
1532 if (!nocode_wanted) {
1533 /* call low level op generator */
b14ef0e2
EGE
Edmund Grimley Evans2015-02-13 18:58:31 +00001534 if (t1 == VT_LLONG || t2 == VT_LLONG ||
1535 (PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR)))
0d1ed741 grischka2009-05-05 20:18:10 +02001536 gen_opl(op);
1537 else
1538 gen_opi(op);
1539 } else {
1540 vtop--;
1541 }
1542 }
1543 }
1544}
1545
1546/* generate a floating point operation with constant propagation */
88a3ccab grischka2009-12-20 01:53:49 +01001547static void gen_opif(int op)
0d1ed741 grischka2009-05-05 20:18:10 +02001548{
1549 int c1, c2;
1550 SValue *v1, *v2;
1551 long double f1, f2;
1552
1553 v1 = vtop - 1;
1554 v2 = vtop;
1555 /* currently, we cannot do computations with forward symbols */
1556 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1557 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1558 if (c1 && c2) {
1559 if (v1->type.t == VT_FLOAT) {
1560 f1 = v1->c.f;
1561 f2 = v2->c.f;
1562 } else if (v1->type.t == VT_DOUBLE) {
1563 f1 = v1->c.d;
1564 f2 = v2->c.d;
1565 } else {
1566 f1 = v1->c.ld;
1567 f2 = v2->c.ld;
1568 }
1569
1570 /* NOTE: we only do constant propagation if finite number (not
1571 NaN or infinity) (ANSI spec) */
1572 if (!ieee_finite(f1) || !ieee_finite(f2))
1573 goto general_case;
1574
1575 switch(op) {
1576 case '+': f1 += f2; break;
1577 case '-': f1 -= f2; break;
1578 case '*': f1 *= f2; break;
1579 case '/':
1580 if (f2 == 0.0) {
1581 if (const_wanted)
bf374a5f grischka2011-08-11 17:07:56 +02001582 tcc_error("division by zero in constant");
0d1ed741 grischka2009-05-05 20:18:10 +02001583 goto general_case;
1584 }
1585 f1 /= f2;
1586 break;
1587 /* XXX: also handles tests ? */
1588 default:
1589 goto general_case;
1590 }
1591 /* XXX: overflow test ? */
1592 if (v1->type.t == VT_FLOAT) {
1593 v1->c.f = f1;
1594 } else if (v1->type.t == VT_DOUBLE) {
1595 v1->c.d = f1;
1596 } else {
1597 v1->c.ld = f1;
1598 }
1599 vtop--;
1600 } else {
1601 general_case:
1602 if (!nocode_wanted) {
1603 gen_opf(op);
1604 } else {
1605 vtop--;
1606 }
1607 }
1608}
1609
1610static int pointed_size(CType *type)
1611{
1612 int align;
1613 return type_size(pointed_type(type), &align);
1614}
1615
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07001616static void vla_runtime_pointed_size(CType *type)
1617{
1618 int align;
1619 vla_runtime_type_size(pointed_type(type), &align);
1620}
1621
0d1ed741 grischka2009-05-05 20:18:10 +02001622static inline int is_null_pointer(SValue *p)
1623{
1624 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1625 return 0;
1626 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
6471ec0a Michael Matz2012-04-14 23:50:21 +02001627 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
5879c854 grischka2014-04-04 20:18:39 +02001628 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr_offset == 0);
0d1ed741 grischka2009-05-05 20:18:10 +02001629}
1630
1631static inline int is_integer_btype(int bt)
1632{
1633 return (bt == VT_BYTE || bt == VT_SHORT ||
1634 bt == VT_INT || bt == VT_LLONG);
1635}
1636
3e9a7e9d Vincent Lefevre2014-04-07 13:31:00 +02001637/* check types for comparison or subtraction of pointers */
0d1ed741 grischka2009-05-05 20:18:10 +02001638static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1639{
1640 CType *type1, *type2, tmp_type1, tmp_type2;
1641 int bt1, bt2;
1642
1643 /* null pointers are accepted for all comparisons as gcc */
1644 if (is_null_pointer(p1) || is_null_pointer(p2))
1645 return;
1646 type1 = &p1->type;
1647 type2 = &p2->type;
1648 bt1 = type1->t & VT_BTYPE;
1649 bt2 = type2->t & VT_BTYPE;
1650 /* accept comparison between pointer and integer with a warning */
1651 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1652 if (op != TOK_LOR && op != TOK_LAND )
bf374a5f grischka2011-08-11 17:07:56 +02001653 tcc_warning("comparison between pointer and integer");
0d1ed741 grischka2009-05-05 20:18:10 +02001654 return;
1655 }
1656
1657 /* both must be pointers or implicit function pointers */
1658 if (bt1 == VT_PTR) {
1659 type1 = pointed_type(type1);
1660 } else if (bt1 != VT_FUNC)
1661 goto invalid_operands;
1662
1663 if (bt2 == VT_PTR) {
1664 type2 = pointed_type(type2);
1665 } else if (bt2 != VT_FUNC) {
1666 invalid_operands:
bf374a5f grischka2011-08-11 17:07:56 +02001667 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
0d1ed741 grischka2009-05-05 20:18:10 +02001668 }
1669 if ((type1->t & VT_BTYPE) == VT_VOID ||
1670 (type2->t & VT_BTYPE) == VT_VOID)
1671 return;
1672 tmp_type1 = *type1;
1673 tmp_type2 = *type2;
b0b5165d
TP
Thomas Preud'homme2014-02-06 20:51:47 +08001674 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1675 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
0d1ed741 grischka2009-05-05 20:18:10 +02001676 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1677 /* gcc-like error if '-' is used */
1678 if (op == '-')
1679 goto invalid_operands;
1680 else
bf374a5f grischka2011-08-11 17:07:56 +02001681 tcc_warning("comparison of distinct pointer types lacks a cast");
0d1ed741 grischka2009-05-05 20:18:10 +02001682 }
1683}
1684
1685/* generic gen_op: handles types problems */
88a3ccab grischka2009-12-20 01:53:49 +01001686ST_FUNC void gen_op(int op)
0d1ed741 grischka2009-05-05 20:18:10 +02001687{
1688 int u, t1, t2, bt1, bt2, t;
1689 CType type1;
1690
1691 t1 = vtop[-1].type.t;
1692 t2 = vtop[0].type.t;
1693 bt1 = t1 & VT_BTYPE;
1694 bt2 = t2 & VT_BTYPE;
1695
1696 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1697 /* at least one operand is a pointer */
1698 /* relationnal op: must be both pointers */
1699 if (op >= TOK_ULT && op <= TOK_LOR) {
1700 check_comparison_pointer_types(vtop - 1, vtop, op);
1701 /* pointers are handled are unsigned */
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00001702#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +02001703 t = VT_LLONG | VT_UNSIGNED;
1704#else
1705 t = VT_INT | VT_UNSIGNED;
1706#endif
1707 goto std_op;
1708 }
1709 /* if both pointers, then it must be the '-' op */
1710 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1711 if (op != '-')
bf374a5f grischka2011-08-11 17:07:56 +02001712 tcc_error("cannot use pointers here");
0d1ed741 grischka2009-05-05 20:18:10 +02001713 check_comparison_pointer_types(vtop - 1, vtop, op);
1714 /* XXX: check that types are compatible */
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07001715 if (vtop[-1].type.t & VT_VLA) {
1716 vla_runtime_pointed_size(&vtop[-1].type);
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07001717 } else {
812781cd Joe Soroka2011-04-11 23:39:27 -07001718 vpushi(pointed_size(&vtop[-1].type));
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07001719 }
812781cd Joe Soroka2011-04-11 23:39:27 -07001720 vrott(3);
0d1ed741 grischka2009-05-05 20:18:10 +02001721 gen_opic(op);
1722 /* set to integer type */
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00001723#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +02001724 vtop->type.t = VT_LLONG;
1725#else
1726 vtop->type.t = VT_INT;
1727#endif
812781cd Joe Soroka2011-04-11 23:39:27 -07001728 vswap();
0d1ed741 grischka2009-05-05 20:18:10 +02001729 gen_op(TOK_PDIV);
1730 } else {
1731 /* exactly one pointer : must be '+' or '-'. */
1732 if (op != '-' && op != '+')
bf374a5f grischka2011-08-11 17:07:56 +02001733 tcc_error("cannot use pointers here");
0d1ed741 grischka2009-05-05 20:18:10 +02001734 /* Put pointer as first operand */
1735 if (bt2 == VT_PTR) {
1736 vswap();
1737 swap(&t1, &t2);
1738 }
1739 type1 = vtop[-1].type;
7c7ca3c6 Joe Soroka2011-04-08 01:09:39 -07001740 type1.t &= ~VT_ARRAY;
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07001741 if (vtop[-1].type.t & VT_VLA)
1742 vla_runtime_pointed_size(&vtop[-1].type);
1743 else {
1744 u = pointed_size(&vtop[-1].type);
1745 if (u < 0)
bf374a5f grischka2011-08-11 17:07:56 +02001746 tcc_error("unknown array element size");
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00001747#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07001748 vpushll(u);
0d1ed741 grischka2009-05-05 20:18:10 +02001749#else
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07001750 /* XXX: cast to int ? (long long case) */
1751 vpushi(u);
0d1ed741 grischka2009-05-05 20:18:10 +02001752#endif
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07001753 }
0d1ed741 grischka2009-05-05 20:18:10 +02001754 gen_op('*');
559675b9 seyko2015-04-10 15:17:22 +03001755#if 0
1756/* #ifdef CONFIG_TCC_BCHECK
1757 The main reason to removing this code:
1758 #include <stdio.h>
1759 int main ()
1760 {
1761 int v[10];
1762 int i = 10;
1763 int j = 9;
1764 fprintf(stderr, "v+i-j = %p\n", v+i-j);
1765 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
1766 }
1767 When this code is on. then the output looks like
1768 v+i-j = 0xfffffffe
1769 v+(i-j) = 0xbff84000
1770 */
0d1ed741 grischka2009-05-05 20:18:10 +02001771 /* if evaluating constant expression, no code should be
1772 generated, so no bound check */
f9181416 grischka2009-05-11 18:45:44 +02001773 if (tcc_state->do_bounds_check && !const_wanted) {
0d1ed741 grischka2009-05-05 20:18:10 +02001774 /* if bounded pointers, we generate a special code to
1775 test bounds */
1776 if (op == '-') {
1777 vpushi(0);
1778 vswap();
1779 gen_op('-');
1780 }
1781 gen_bounded_ptr_add();
1782 } else
1783#endif
1784 {
1785 gen_opic(op);
1786 }
1787 /* put again type if gen_opic() swaped operands */
1788 vtop->type = type1;
1789 }
1790 } else if (is_float(bt1) || is_float(bt2)) {
1791 /* compute bigger type and do implicit casts */
1792 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1793 t = VT_LDOUBLE;
1794 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1795 t = VT_DOUBLE;
1796 } else {
1797 t = VT_FLOAT;
1798 }
1799 /* floats can only be used for a few operations */
1800 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1801 (op < TOK_ULT || op > TOK_GT))
bf374a5f grischka2011-08-11 17:07:56 +02001802 tcc_error("invalid operands for binary operation");
0d1ed741 grischka2009-05-05 20:18:10 +02001803 goto std_op;
240064c0 Vincent Lefevre2012-06-27 13:31:44 +02001804 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
d27a0b35
VL
Vincent Lefevre2012-07-06 14:22:37 +02001805 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1806 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1807 t |= VT_UNSIGNED;
240064c0 Vincent Lefevre2012-06-27 13:31:44 +02001808 goto std_op;
0d1ed741 grischka2009-05-05 20:18:10 +02001809 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1810 /* cast to biggest op */
1811 t = VT_LLONG;
1812 /* convert to unsigned if it does not fit in a long long */
1813 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1814 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1815 t |= VT_UNSIGNED;
1816 goto std_op;
5eb64357
HM
Hitoshi Mitake2012-11-05 12:26:26 +09001817 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1818 tcc_error("comparison of struct");
0d1ed741 grischka2009-05-05 20:18:10 +02001819 } else {
1820 /* integer operations */
1821 t = VT_INT;
1822 /* convert to unsigned if it does not fit in an integer */
1823 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1824 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1825 t |= VT_UNSIGNED;
1826 std_op:
1827 /* XXX: currently, some unsigned operations are explicit, so
1828 we modify them here */
1829 if (t & VT_UNSIGNED) {
1830 if (op == TOK_SAR)
1831 op = TOK_SHR;
1832 else if (op == '/')
1833 op = TOK_UDIV;
1834 else if (op == '%')
1835 op = TOK_UMOD;
1836 else if (op == TOK_LT)
1837 op = TOK_ULT;
1838 else if (op == TOK_GT)
1839 op = TOK_UGT;
1840 else if (op == TOK_LE)
1841 op = TOK_ULE;
1842 else if (op == TOK_GE)
1843 op = TOK_UGE;
1844 }
1845 vswap();
1846 type1.t = t;
1847 gen_cast(&type1);
1848 vswap();
1849 /* special case for shifts and long long: we keep the shift as
1850 an integer */
1851 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1852 type1.t = VT_INT;
1853 gen_cast(&type1);
1854 if (is_float(t))
1855 gen_opif(op);
1856 else
1857 gen_opic(op);
1858 if (op >= TOK_ULT && op <= TOK_GT) {
1859 /* relationnal op: the result is an int */
1860 vtop->type.t = VT_INT;
1861 } else {
1862 vtop->type.t = t;
1863 }
1864 }
40f7e11c Edmund Grimley Evans2015-02-20 22:18:41 +00001865 // Make sure that we have converted to an rvalue:
5bcc3eed Thomas Preud'homme2015-03-10 23:23:00 +08001866 if (vtop->r & VT_LVAL && !nocode_wanted)
40f7e11c Edmund Grimley Evans2015-02-20 22:18:41 +00001867 gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
0d1ed741 grischka2009-05-05 20:18:10 +02001868}
1869
1870#ifndef TCC_TARGET_ARM
1871/* generic itof for unsigned long long case */
88a3ccab grischka2009-12-20 01:53:49 +01001872static void gen_cvt_itof1(int t)
0d1ed741 grischka2009-05-05 20:18:10 +02001873{
b14ef0e2
EGE
Edmund Grimley Evans2015-02-13 18:58:31 +00001874#ifdef TCC_TARGET_ARM64
1875 gen_cvt_itof(t);
1876#else
0d1ed741 grischka2009-05-05 20:18:10 +02001877 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1878 (VT_LLONG | VT_UNSIGNED)) {
1879
1880 if (t == VT_FLOAT)
1881 vpush_global_sym(&func_old_type, TOK___floatundisf);
1882#if LDOUBLE_SIZE != 8
1883 else if (t == VT_LDOUBLE)
1884 vpush_global_sym(&func_old_type, TOK___floatundixf);
1885#endif
1886 else
1887 vpush_global_sym(&func_old_type, TOK___floatundidf);
1888 vrott(2);
1889 gfunc_call(1);
1890 vpushi(0);
1891 vtop->r = reg_fret(t);
1892 } else {
1893 gen_cvt_itof(t);
1894 }
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00001895#endif
0d1ed741 grischka2009-05-05 20:18:10 +02001896}
1897#endif
1898
1899/* generic ftoi for unsigned long long case */
88a3ccab grischka2009-12-20 01:53:49 +01001900static void gen_cvt_ftoi1(int t)
0d1ed741 grischka2009-05-05 20:18:10 +02001901{
b14ef0e2
EGE
Edmund Grimley Evans2015-02-13 18:58:31 +00001902#ifdef TCC_TARGET_ARM64
1903 gen_cvt_ftoi(t);
1904#else
0d1ed741 grischka2009-05-05 20:18:10 +02001905 int st;
1906
1907 if (t == (VT_LLONG | VT_UNSIGNED)) {
1908 /* not handled natively */
1909 st = vtop->type.t & VT_BTYPE;
1910 if (st == VT_FLOAT)
1911 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1912#if LDOUBLE_SIZE != 8
1913 else if (st == VT_LDOUBLE)
1914 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1915#endif
1916 else
1917 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1918 vrott(2);
1919 gfunc_call(1);
1920 vpushi(0);
1921 vtop->r = REG_IRET;
1922 vtop->r2 = REG_LRET;
1923 } else {
1924 gen_cvt_ftoi(t);
1925 }
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00001926#endif
0d1ed741 grischka2009-05-05 20:18:10 +02001927}
1928
1929/* force char or short cast */
88a3ccab grischka2009-12-20 01:53:49 +01001930static void force_charshort_cast(int t)
0d1ed741 grischka2009-05-05 20:18:10 +02001931{
1932 int bits, dbt;
1933 dbt = t & VT_BTYPE;
1934 /* XXX: add optimization if lvalue : just change type and offset */
1935 if (dbt == VT_BYTE)
1936 bits = 8;
1937 else
1938 bits = 16;
1939 if (t & VT_UNSIGNED) {
1940 vpushi((1 << bits) - 1);
1941 gen_op('&');
1942 } else {
1943 bits = 32 - bits;
1944 vpushi(bits);
1945 gen_op(TOK_SHL);
1946 /* result must be signed or the SAR is converted to an SHL
1947 This was not the case when "t" was a signed short
1948 and the last value on the stack was an unsigned int */
1949 vtop->type.t &= ~VT_UNSIGNED;
1950 vpushi(bits);
1951 gen_op(TOK_SAR);
1952 }
1953}
1954
1955/* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1956static void gen_cast(CType *type)
1957{
1958 int sbt, dbt, sf, df, c, p;
1959
1960 /* special delayed cast for char/short */
1961 /* XXX: in some cases (multiple cascaded casts), it may still
1962 be incorrect */
1963 if (vtop->r & VT_MUSTCAST) {
1964 vtop->r &= ~VT_MUSTCAST;
1965 force_charshort_cast(vtop->type.t);
1966 }
1967
1968 /* bitfields first get cast to ints */
5bcc3eed Thomas Preud'homme2015-03-10 23:23:00 +08001969 if (vtop->type.t & VT_BITFIELD && !nocode_wanted) {
0d1ed741 grischka2009-05-05 20:18:10 +02001970 gv(RC_INT);
1971 }
1972
1973 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1974 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1975
1976 if (sbt != dbt) {
1977 sf = is_float(sbt);
1978 df = is_float(dbt);
1979 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1980 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1981 if (c) {
1982 /* constant case: we can do it now */
1983 /* XXX: in ISOC, cannot do it if error in convert */
1984 if (sbt == VT_FLOAT)
1985 vtop->c.ld = vtop->c.f;
1986 else if (sbt == VT_DOUBLE)
1987 vtop->c.ld = vtop->c.d;
1988
1989 if (df) {
1990 if ((sbt & VT_BTYPE) == VT_LLONG) {
1991 if (sbt & VT_UNSIGNED)
1992 vtop->c.ld = vtop->c.ull;
1993 else
1994 vtop->c.ld = vtop->c.ll;
1995 } else if(!sf) {
1996 if (sbt & VT_UNSIGNED)
1997 vtop->c.ld = vtop->c.ui;
1998 else
1999 vtop->c.ld = vtop->c.i;
2000 }
2001
2002 if (dbt == VT_FLOAT)
2003 vtop->c.f = (float)vtop->c.ld;
2004 else if (dbt == VT_DOUBLE)
2005 vtop->c.d = (double)vtop->c.ld;
2006 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
2007 vtop->c.ull = (unsigned long long)vtop->c.ld;
2008 } else if (sf && dbt == VT_BOOL) {
2009 vtop->c.i = (vtop->c.ld != 0);
2010 } else {
2011 if(sf)
2012 vtop->c.ll = (long long)vtop->c.ld;
2013 else if (sbt == (VT_LLONG|VT_UNSIGNED))
2014 vtop->c.ll = vtop->c.ull;
2015 else if (sbt & VT_UNSIGNED)
2016 vtop->c.ll = vtop->c.ui;
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00002017#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
614790dc
CJ
Christian Jullien2009-12-15 17:45:15 +01002018 else if (sbt == VT_PTR)
2019 ;
2020#endif
0d1ed741 grischka2009-05-05 20:18:10 +02002021 else if (sbt != VT_LLONG)
2022 vtop->c.ll = vtop->c.i;
2023
2024 if (dbt == (VT_LLONG|VT_UNSIGNED))
2025 vtop->c.ull = vtop->c.ll;
2026 else if (dbt == VT_BOOL)
2027 vtop->c.i = (vtop->c.ll != 0);
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00002028#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
9e11476e
MM
Michael Matz2014-01-11 23:42:58 +01002029 else if (dbt == VT_PTR)
2030 ;
2031#endif
0d1ed741 grischka2009-05-05 20:18:10 +02002032 else if (dbt != VT_LLONG) {
2033 int s = 0;
2034 if ((dbt & VT_BTYPE) == VT_BYTE)
2035 s = 24;
2036 else if ((dbt & VT_BTYPE) == VT_SHORT)
2037 s = 16;
0d1ed741 grischka2009-05-05 20:18:10 +02002038 if(dbt & VT_UNSIGNED)
2039 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
2040 else
2041 vtop->c.i = ((int)vtop->c.ll << s) >> s;
2042 }
2043 }
2044 } else if (p && dbt == VT_BOOL) {
2045 vtop->r = VT_CONST;
2046 vtop->c.i = 1;
2047 } else if (!nocode_wanted) {
2048 /* non constant case: generate code */
2049 if (sf && df) {
2050 /* convert from fp to fp */
2051 gen_cvt_ftof(dbt);
2052 } else if (df) {
2053 /* convert int to fp */
2054 gen_cvt_itof1(dbt);
2055 } else if (sf) {
2056 /* convert fp to int */
2057 if (dbt == VT_BOOL) {
2058 vpushi(0);
2059 gen_op(TOK_NE);
2060 } else {
2061 /* we handle char/short/etc... with generic code */
2062 if (dbt != (VT_INT | VT_UNSIGNED) &&
2063 dbt != (VT_LLONG | VT_UNSIGNED) &&
2064 dbt != VT_LLONG)
2065 dbt = VT_INT;
2066 gen_cvt_ftoi1(dbt);
2067 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
2068 /* additional cast for char/short... */
2069 vtop->type.t = dbt;
2070 gen_cast(type);
2071 }
2072 }
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00002073#if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +02002074 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5bcc3eed Thomas Preud'homme2015-03-10 23:23:00 +08002075 if ((sbt & VT_BTYPE) != VT_LLONG && !nocode_wanted) {
0d1ed741 grischka2009-05-05 20:18:10 +02002076 /* scalar to long long */
2077 /* machine independent conversion */
2078 gv(RC_INT);
2079 /* generate high word */
2080 if (sbt == (VT_INT | VT_UNSIGNED)) {
2081 vpushi(0);
2082 gv(RC_INT);
2083 } else {
2084 if (sbt == VT_PTR) {
2085 /* cast from pointer to int before we apply
2086 shift operation, which pointers don't support*/
2087 gen_cast(&int_type);
2088 }
2089 gv_dup();
2090 vpushi(31);
2091 gen_op(TOK_SAR);
2092 }
2093 /* patch second register */
2094 vtop[-1].r2 = vtop->r;
2095 vpop();
2096 }
2097#else
2098 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
c31dc7aa
SH
Shinichiro Hamaji2010-08-31 08:35:31 +09002099 (dbt & VT_BTYPE) == VT_PTR ||
2100 (dbt & VT_BTYPE) == VT_FUNC) {
2101 if ((sbt & VT_BTYPE) != VT_LLONG &&
2102 (sbt & VT_BTYPE) != VT_PTR &&
5bcc3eed Thomas Preud'homme2015-03-10 23:23:00 +08002103 (sbt & VT_BTYPE) != VT_FUNC && !nocode_wanted) {
c31dc7aa Shinichiro Hamaji2010-08-31 08:35:31 +09002104 /* need to convert from 32bit to 64bit */
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00002105 gv(RC_INT);
c31dc7aa Shinichiro Hamaji2010-08-31 08:35:31 +09002106 if (sbt != (VT_INT | VT_UNSIGNED)) {
b14ef0e2
EGE
Edmund Grimley Evans2015-02-13 18:58:31 +00002107#if defined(TCC_TARGET_ARM64)
2108 gen_cvt_sxtw();
2109#elif defined(TCC_TARGET_X86_64)
2110 int r = gv(RC_INT);
0d1ed741 grischka2009-05-05 20:18:10 +02002111 /* x86_64 specific: movslq */
2112 o(0x6348);
2113 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
b14ef0e2
EGE
Edmund Grimley Evans2015-02-13 18:58:31 +00002114#else
2115#error
2116#endif
0d1ed741 grischka2009-05-05 20:18:10 +02002117 }
2118 }
2119#endif
2120 } else if (dbt == VT_BOOL) {
2121 /* scalar to bool */
2122 vpushi(0);
2123 gen_op(TOK_NE);
2124 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2125 (dbt & VT_BTYPE) == VT_SHORT) {
2126 if (sbt == VT_PTR) {
2127 vtop->type.t = VT_INT;
bf374a5f grischka2011-08-11 17:07:56 +02002128 tcc_warning("nonportable conversion from pointer to char/short");
0d1ed741 grischka2009-05-05 20:18:10 +02002129 }
2130 force_charshort_cast(dbt);
2131 } else if ((dbt & VT_BTYPE) == VT_INT) {
2132 /* scalar to int */
5bcc3eed Thomas Preud'homme2015-03-10 23:23:00 +08002133 if (sbt == VT_LLONG && !nocode_wanted) {
0d1ed741 grischka2009-05-05 20:18:10 +02002134 /* from long long: just take low order word */
2135 lexpand();
2136 vpop();
2137 }
2138 /* if lvalue and single word type, nothing to do because
2139 the lvalue already contains the real type size (see
2140 VT_LVAL_xxx constants) */
2141 }
2142 }
2143 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2144 /* if we are casting between pointer types,
2145 we must update the VT_LVAL_xxx size */
2146 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2147 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2148 }
2149 vtop->type = *type;
2150}
2151
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07002152/* return type size as known at compile time. Put alignment at 'a' */
88a3ccab grischka2009-12-20 01:53:49 +01002153ST_FUNC int type_size(CType *type, int *a)
0d1ed741 grischka2009-05-05 20:18:10 +02002154{
2155 Sym *s;
2156 int bt;
2157
2158 bt = type->t & VT_BTYPE;
2159 if (bt == VT_STRUCT) {
2160 /* struct/union */
2161 s = type->ref;
06a7c415 Joe Soroka2011-03-18 17:47:35 -07002162 *a = s->r;
0d1ed741 grischka2009-05-05 20:18:10 +02002163 return s->c;
2164 } else if (bt == VT_PTR) {
7c7ca3c6 Joe Soroka2011-04-08 01:09:39 -07002165 if (type->t & VT_ARRAY) {
0d1ed741 grischka2009-05-05 20:18:10 +02002166 int ts;
2167
2168 s = type->ref;
2169 ts = type_size(&s->type, a);
2170
2171 if (ts < 0 && s->c < 0)
2172 ts = -ts;
2173
2174 return ts * s->c;
2175 } else {
2176 *a = PTR_SIZE;
2177 return PTR_SIZE;
2178 }
2179 } else if (bt == VT_LDOUBLE) {
2180 *a = LDOUBLE_ALIGN;
2181 return LDOUBLE_SIZE;
2182 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2183#ifdef TCC_TARGET_I386
2184#ifdef TCC_TARGET_PE
2185 *a = 8;
2186#else
2187 *a = 4;
2188#endif
2189#elif defined(TCC_TARGET_ARM)
2190#ifdef TCC_ARM_EABI
2191 *a = 8;
2192#else
2193 *a = 4;
2194#endif
2195#else
2196 *a = 8;
2197#endif
2198 return 8;
2199 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2200 *a = 4;
2201 return 4;
2202 } else if (bt == VT_SHORT) {
2203 *a = 2;
2204 return 2;
b961ba53
JL
James Lyon2013-04-19 11:08:12 +01002205 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2206 *a = 8;
2207 return 16;
0d1ed741 grischka2009-05-05 20:18:10 +02002208 } else {
2209 /* char, void, function, _Bool */
2210 *a = 1;
2211 return 1;
2212 }
2213}
2214
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07002215/* push type size as known at runtime time on top of value stack. Put
2216 alignment at 'a' */
2217ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2218{
2219 if (type->t & VT_VLA) {
1446b543 Joe Soroka2011-04-09 22:52:25 -07002220 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07002221 } else {
1446b543 Joe Soroka2011-04-09 22:52:25 -07002222 vpushi(type_size(type, a));
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07002223 }
2224}
2225
41b3c7a5
JL
James Lyon2013-04-27 20:39:34 +01002226static void vla_sp_save(void) {
2227 if (!(vla_flags & VLA_SP_LOC_SET)) {
2228 *vla_sp_loc = (loc -= PTR_SIZE);
2229 vla_flags |= VLA_SP_LOC_SET;
2230 }
2231 if (!(vla_flags & VLA_SP_SAVED)) {
2232 gen_vla_sp_save(*vla_sp_loc);
2233 vla_flags |= VLA_SP_SAVED;
2234 }
2235}
2236
0d1ed741 grischka2009-05-05 20:18:10 +02002237/* return the pointed type of t */
2238static inline CType *pointed_type(CType *type)
2239{
2240 return &type->ref->type;
2241}
2242
2243/* modify type so that its it is a pointer to type. */
88a3ccab grischka2009-12-20 01:53:49 +01002244ST_FUNC void mk_pointer(CType *type)
0d1ed741 grischka2009-05-05 20:18:10 +02002245{
2246 Sym *s;
2247 s = sym_push(SYM_FIELD, type, 0, -1);
2248 type->t = VT_PTR | (type->t & ~VT_TYPE);
2249 type->ref = s;
2250}
2251
2252/* compare function types. OLD functions match any new functions */
2253static int is_compatible_func(CType *type1, CType *type2)
2254{
2255 Sym *s1, *s2;
2256
2257 s1 = type1->ref;
2258 s2 = type2->ref;
2259 if (!is_compatible_types(&s1->type, &s2->type))
2260 return 0;
2261 /* check func_call */
3fe2a95d grischka2014-01-07 14:57:07 +01002262 if (s1->a.func_call != s2->a.func_call)
0d1ed741 grischka2009-05-05 20:18:10 +02002263 return 0;
2264 /* XXX: not complete */
2265 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2266 return 1;
2267 if (s1->c != s2->c)
2268 return 0;
2269 while (s1 != NULL) {
2270 if (s2 == NULL)
2271 return 0;
2272 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2273 return 0;
2274 s1 = s1->next;
2275 s2 = s2->next;
2276 }
2277 if (s2)
2278 return 0;
2279 return 1;
2280}
2281
2282/* return true if type1 and type2 are the same. If unqualified is
2283 true, qualifiers on the types are ignored.
2284
2285 - enums are not checked as gcc __builtin_types_compatible_p ()
2286 */
2287static int compare_types(CType *type1, CType *type2, int unqualified)
2288{
2289 int bt1, t1, t2;
2290
2291 t1 = type1->t & VT_TYPE;
2292 t2 = type2->t & VT_TYPE;
2293 if (unqualified) {
2294 /* strip qualifiers before comparing */
2295 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2296 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2297 }
b0b5165d
TP
Thomas Preud'homme2014-02-06 20:51:47 +08002298 /* Default Vs explicit signedness only matters for char */
2299 if ((t1 & VT_BTYPE) != VT_BYTE) {
2300 t1 &= ~VT_DEFSIGN;
2301 t2 &= ~VT_DEFSIGN;
2302 }
0d1ed741 grischka2009-05-05 20:18:10 +02002303 /* XXX: bitfields ? */
2304 if (t1 != t2)
2305 return 0;
2306 /* test more complicated cases */
2307 bt1 = t1 & VT_BTYPE;
2308 if (bt1 == VT_PTR) {
2309 type1 = pointed_type(type1);
2310 type2 = pointed_type(type2);
2311 return is_compatible_types(type1, type2);
2312 } else if (bt1 == VT_STRUCT) {
2313 return (type1->ref == type2->ref);
2314 } else if (bt1 == VT_FUNC) {
2315 return is_compatible_func(type1, type2);
2316 } else {
2317 return 1;
2318 }
2319}
2320
2321/* return true if type1 and type2 are exactly the same (including
2322 qualifiers).
2323*/
2324static int is_compatible_types(CType *type1, CType *type2)
2325{
2326 return compare_types(type1,type2,0);
2327}
2328
2329/* return true if type1 and type2 are the same (ignoring qualifiers).
2330*/
2331static int is_compatible_parameter_types(CType *type1, CType *type2)
2332{
2333 return compare_types(type1,type2,1);
2334}
2335
2336/* print a type. If 'varstr' is not NULL, then the variable is also
2337 printed in the type */
2338/* XXX: union */
2339/* XXX: add array and function pointers */
88a3ccab grischka2009-12-20 01:53:49 +01002340static void type_to_str(char *buf, int buf_size,
0d1ed741 grischka2009-05-05 20:18:10 +02002341 CType *type, const char *varstr)
2342{
2343 int bt, v, t;
2344 Sym *s, *sa;
2345 char buf1[256];
2346 const char *tstr;
2347
2348 t = type->t & VT_TYPE;
2349 bt = t & VT_BTYPE;
2350 buf[0] = '\0';
2351 if (t & VT_CONSTANT)
2352 pstrcat(buf, buf_size, "const ");
2353 if (t & VT_VOLATILE)
2354 pstrcat(buf, buf_size, "volatile ");
33cea54d Thomas Preud'homme2014-03-09 13:32:24 +08002355 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
0d1ed741 grischka2009-05-05 20:18:10 +02002356 pstrcat(buf, buf_size, "unsigned ");
b0b5165d
TP
Thomas Preud'homme2014-02-06 20:51:47 +08002357 else if (t & VT_DEFSIGN)
2358 pstrcat(buf, buf_size, "signed ");
0d1ed741 grischka2009-05-05 20:18:10 +02002359 switch(bt) {
2360 case VT_VOID:
2361 tstr = "void";
2362 goto add_tstr;
2363 case VT_BOOL:
2364 tstr = "_Bool";
2365 goto add_tstr;
2366 case VT_BYTE:
2367 tstr = "char";
2368 goto add_tstr;
2369 case VT_SHORT:
2370 tstr = "short";
2371 goto add_tstr;
2372 case VT_INT:
2373 tstr = "int";
2374 goto add_tstr;
2375 case VT_LONG:
2376 tstr = "long";
2377 goto add_tstr;
2378 case VT_LLONG:
2379 tstr = "long long";
2380 goto add_tstr;
2381 case VT_FLOAT:
2382 tstr = "float";
2383 goto add_tstr;
2384 case VT_DOUBLE:
2385 tstr = "double";
2386 goto add_tstr;
2387 case VT_LDOUBLE:
2388 tstr = "long double";
2389 add_tstr:
2390 pstrcat(buf, buf_size, tstr);
2391 break;
2392 case VT_ENUM:
2393 case VT_STRUCT:
2394 if (bt == VT_STRUCT)
2395 tstr = "struct ";
2396 else
2397 tstr = "enum ";
2398 pstrcat(buf, buf_size, tstr);
2399 v = type->ref->v & ~SYM_STRUCT;
2400 if (v >= SYM_FIRST_ANOM)
2401 pstrcat(buf, buf_size, "<anonymous>");
2402 else
2403 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2404 break;
2405 case VT_FUNC:
2406 s = type->ref;
2407 type_to_str(buf, buf_size, &s->type, varstr);
2408 pstrcat(buf, buf_size, "(");
2409 sa = s->next;
2410 while (sa != NULL) {
2411 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2412 pstrcat(buf, buf_size, buf1);
2413 sa = sa->next;
2414 if (sa)
2415 pstrcat(buf, buf_size, ", ");
2416 }
2417 pstrcat(buf, buf_size, ")");
2418 goto no_var;
2419 case VT_PTR:
2420 s = type->ref;
2421 pstrcpy(buf1, sizeof(buf1), "*");
2422 if (varstr)
2423 pstrcat(buf1, sizeof(buf1), varstr);
2424 type_to_str(buf, buf_size, &s->type, buf1);
2425 goto no_var;
2426 }
2427 if (varstr) {
2428 pstrcat(buf, buf_size, " ");
2429 pstrcat(buf, buf_size, varstr);
2430 }
2431 no_var: ;
2432}
2433
2434/* verify type compatibility to store vtop in 'dt' type, and generate
2435 casts if needed. */
2436static void gen_assign_cast(CType *dt)
2437{
2438 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2439 char buf1[256], buf2[256];
2440 int dbt, sbt;
2441
2442 st = &vtop->type; /* source type */
2443 dbt = dt->t & VT_BTYPE;
2444 sbt = st->t & VT_BTYPE;
6fd4e5ba seyko2015-03-03 15:39:57 +03002445 if (sbt == VT_VOID || dbt == VT_VOID) {
2446 if (sbt == VT_VOID && dbt == VT_VOID)
2447 ; /*
2448 It is Ok if both are void
2449 A test program:
2450 void func1() {}
2451 void func2() {
2452 return func1();
2453 }
2454 gcc accepts this program
2455 */
2456 else
2457 tcc_error("cannot cast from/to void");
2458 }
0d1ed741 grischka2009-05-05 20:18:10 +02002459 if (dt->t & VT_CONSTANT)
bf374a5f grischka2011-08-11 17:07:56 +02002460 tcc_warning("assignment of read-only location");
0d1ed741 grischka2009-05-05 20:18:10 +02002461 switch(dbt) {
2462 case VT_PTR:
2463 /* special cases for pointers */
2464 /* '0' can also be a pointer */
2465 if (is_null_pointer(vtop))
2466 goto type_ok;
2467 /* accept implicit pointer to integer cast with warning */
2468 if (is_integer_btype(sbt)) {
bf374a5f grischka2011-08-11 17:07:56 +02002469 tcc_warning("assignment makes pointer from integer without a cast");
0d1ed741 grischka2009-05-05 20:18:10 +02002470 goto type_ok;
2471 }
2472 type1 = pointed_type(dt);
2473 /* a function is implicitely a function pointer */
2474 if (sbt == VT_FUNC) {
2475 if ((type1->t & VT_BTYPE) != VT_VOID &&
2476 !is_compatible_types(pointed_type(dt), st))
bf374a5f grischka2011-08-11 17:07:56 +02002477 tcc_warning("assignment from incompatible pointer type");
956b4bee grischka2009-06-17 02:10:24 +02002478 goto type_ok;
0d1ed741 grischka2009-05-05 20:18:10 +02002479 }
2480 if (sbt != VT_PTR)
2481 goto error;
2482 type2 = pointed_type(st);
2483 if ((type1->t & VT_BTYPE) == VT_VOID ||
2484 (type2->t & VT_BTYPE) == VT_VOID) {
2485 /* void * can match anything */
2486 } else {
2487 /* exact type match, except for unsigned */
2488 tmp_type1 = *type1;
2489 tmp_type2 = *type2;
b0b5165d
TP
Thomas Preud'homme2014-02-06 20:51:47 +08002490 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2491 VT_VOLATILE);
2492 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2493 VT_VOLATILE);
0d1ed741 grischka2009-05-05 20:18:10 +02002494 if (!is_compatible_types(&tmp_type1, &tmp_type2))
bf374a5f grischka2011-08-11 17:07:56 +02002495 tcc_warning("assignment from incompatible pointer type");
0d1ed741 grischka2009-05-05 20:18:10 +02002496 }
2497 /* check const and volatile */
2498 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2499 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
bf374a5f grischka2011-08-11 17:07:56 +02002500 tcc_warning("assignment discards qualifiers from pointer target type");
0d1ed741 grischka2009-05-05 20:18:10 +02002501 break;
2502 case VT_BYTE:
2503 case VT_SHORT:
2504 case VT_INT:
2505 case VT_LLONG:
2506 if (sbt == VT_PTR || sbt == VT_FUNC) {
bf374a5f grischka2011-08-11 17:07:56 +02002507 tcc_warning("assignment makes integer from pointer without a cast");
0d1ed741 grischka2009-05-05 20:18:10 +02002508 }
2509 /* XXX: more tests */
2510 break;
2511 case VT_STRUCT:
2512 tmp_type1 = *dt;
2513 tmp_type2 = *st;
2514 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2515 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2516 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2517 error:
2518 type_to_str(buf1, sizeof(buf1), st, NULL);
2519 type_to_str(buf2, sizeof(buf2), dt, NULL);
bf374a5f grischka2011-08-11 17:07:56 +02002520 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
0d1ed741 grischka2009-05-05 20:18:10 +02002521 }
2522 break;
2523 }
2524 type_ok:
2525 gen_cast(dt);
2526}
2527
2528/* store vtop in lvalue pushed on stack */
88a3ccab grischka2009-12-20 01:53:49 +01002529ST_FUNC void vstore(void)
0d1ed741 grischka2009-05-05 20:18:10 +02002530{
2531 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2532
2533 ft = vtop[-1].type.t;
2534 sbt = vtop->type.t & VT_BTYPE;
2535 dbt = ft & VT_BTYPE;
5c0a2366
MM
Michael Matz2012-04-15 01:06:46 +02002536 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2537 (sbt == VT_INT && dbt == VT_SHORT))
2538 && !(vtop->type.t & VT_BITFIELD)) {
0d1ed741 grischka2009-05-05 20:18:10 +02002539 /* optimize char/short casts */
2540 delayed_cast = VT_MUSTCAST;
2541 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2542 /* XXX: factorize */
2543 if (ft & VT_CONSTANT)
bf374a5f grischka2011-08-11 17:07:56 +02002544 tcc_warning("assignment of read-only location");
0d1ed741 grischka2009-05-05 20:18:10 +02002545 } else {
2546 delayed_cast = 0;
2547 if (!(ft & VT_BITFIELD))
2548 gen_assign_cast(&vtop[-1].type);
2549 }
2550
2551 if (sbt == VT_STRUCT) {
2552 /* if structure, only generate pointer */
2553 /* structure assignment : generate memcpy */
2554 /* XXX: optimize if small size */
2555 if (!nocode_wanted) {
2556 size = type_size(&vtop->type, &align);
2557
c0620c8a bobbl2009-11-30 19:33:44 +01002558 /* destination */
2559 vswap();
2560 vtop->type.t = VT_PTR;
2561 gaddrof();
2562
2563 /* address of memcpy() */
0d1ed741 grischka2009-05-05 20:18:10 +02002564#ifdef TCC_ARM_EABI
2565 if(!(align & 7))
2566 vpush_global_sym(&func_old_type, TOK_memcpy8);
2567 else if(!(align & 3))
2568 vpush_global_sym(&func_old_type, TOK_memcpy4);
2569 else
2570#endif
2571 vpush_global_sym(&func_old_type, TOK_memcpy);
2572
c0620c8a bobbl2009-11-30 19:33:44 +01002573 vswap();
0d1ed741 grischka2009-05-05 20:18:10 +02002574 /* source */
2575 vpushv(vtop - 2);
2576 vtop->type.t = VT_PTR;
2577 gaddrof();
2578 /* type size */
2579 vpushi(size);
2580 gfunc_call(3);
0d1ed741 grischka2009-05-05 20:18:10 +02002581 } else {
2582 vswap();
2583 vpop();
2584 }
2585 /* leave source on stack */
2586 } else if (ft & VT_BITFIELD) {
2587 /* bitfield store handling */
9d7fb333 grischka2014-09-23 12:30:08 +02002588
2589 /* save lvalue as expression result (example: s.b = s.a = n;) */
2590 vdup(), vtop[-1] = vtop[-2];
2591
0d1ed741 grischka2009-05-05 20:18:10 +02002592 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2593 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2594 /* remove bit field info to avoid loops */
2595 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2596
0d1ed741 grischka2009-05-05 20:18:10 +02002597 if((ft & VT_BTYPE) == VT_BOOL) {
2598 gen_cast(&vtop[-1].type);
2599 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2600 }
2601
2602 /* duplicate destination */
2603 vdup();
2604 vtop[-1] = vtop[-2];
2605
2606 /* mask and shift source */
2607 if((ft & VT_BTYPE) != VT_BOOL) {
2608 if((ft & VT_BTYPE) == VT_LLONG) {
2609 vpushll((1ULL << bit_size) - 1ULL);
2610 } else {
2611 vpushi((1 << bit_size) - 1);
2612 }
2613 gen_op('&');
2614 }
2615 vpushi(bit_pos);
2616 gen_op(TOK_SHL);
2617 /* load destination, mask and or with source */
2618 vswap();
2619 if((ft & VT_BTYPE) == VT_LLONG) {
2620 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2621 } else {
2622 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2623 }
2624 gen_op('&');
2625 gen_op('|');
2626 /* store result */
2627 vstore();
9d7fb333 grischka2014-09-23 12:30:08 +02002628 /* ... and discard */
0d1ed741 grischka2009-05-05 20:18:10 +02002629 vpop();
2630
2631 } else {
5bcc3eed Thomas Preud'homme2015-03-10 23:23:00 +08002632 if (!nocode_wanted) {
0d1ed741 grischka2009-05-05 20:18:10 +02002633#ifdef CONFIG_TCC_BCHECK
5bcc3eed
TP
Thomas Preud'homme2015-03-10 23:23:00 +08002634 /* bound check case */
2635 if (vtop[-1].r & VT_MUSTBOUND) {
2636 vswap();
2637 gbound();
2638 vswap();
2639 }
0d1ed741 grischka2009-05-05 20:18:10 +02002640#endif
0d1ed741 grischka2009-05-05 20:18:10 +02002641 rc = RC_INT;
2642 if (is_float(ft)) {
2643 rc = RC_FLOAT;
2644#ifdef TCC_TARGET_X86_64
2645 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2646 rc = RC_ST0;
0e17671f
JL
James Lyon2013-04-19 15:33:16 +01002647 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2648 rc = RC_FRET;
0d1ed741 grischka2009-05-05 20:18:10 +02002649 }
2650#endif
2651 }
2652 r = gv(rc); /* generate value */
2653 /* if lvalue was saved on stack, must read it */
2654 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2655 SValue sv;
2656 t = get_reg(RC_INT);
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00002657#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
0d1ed741 grischka2009-05-05 20:18:10 +02002658 sv.type.t = VT_PTR;
2659#else
2660 sv.type.t = VT_INT;
2661#endif
2662 sv.r = VT_LOCAL | VT_LVAL;
2663 sv.c.ul = vtop[-1].c.ul;
2664 load(t, &sv);
2665 vtop[-1].r = t | VT_LVAL;
2666 }
55ea6d3f James Lyon2013-04-19 00:40:48 +01002667 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
b14ef0e2 Edmund Grimley Evans2015-02-13 18:58:31 +00002668#if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
0e17671f
JL
James Lyon2013-04-19 15:33:16 +01002669 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2670 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
55ea6d3f James Lyon2013-04-19 00:40:48 +01002671#else
0d1ed741 grischka2009-05-05 20:18:10 +02002672 if ((ft & VT_BTYPE) == VT_LLONG) {
0e17671f James Lyon2013-04-19 15:33:16 +01002673 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
55ea6d3f James Lyon2013-04-19 00:40:48 +01002674#endif
0e17671f
JL
James Lyon2013-04-19 15:33:16 +01002675 vtop[-1].type.t = load_type;
2676 store(r, vtop - 1);
0d1ed741 grischka2009-05-05 20:18:10 +02002677 vswap();
2678 /* convert to int to increment easily */
0e17671f James Lyon2013-04-19 15:33:16 +01002679 vtop->type.t = addr_type;
0d1ed741 grischka2009-05-05 20:18:10 +02002680 gaddrof();
0e17671f James Lyon2013-04-19 15:33:16 +01002681 vpushi(load_size);
0d1ed741 grischka2009-05-05 20:18:10 +02002682 gen_op('+');
2683 vtop->r |= VT_LVAL;
2684 vswap();
0e17671f James Lyon2013-04-19 15:33:16 +01002685 vtop[-1].type.t = load_type;
0d1ed741 grischka2009-05-05 20:18:10 +02002686 /* XXX: it works because r2 is spilled last ! */
2687 store(vtop->r2, vtop - 1);
0e17671f
JL
James Lyon2013-04-19 15:33:16 +01002688 } else {
2689 store(r, vtop - 1);
0d1ed741 grischka2009-05-05 20:18:10 +02002690 }
0d1ed741 grischka2009-05-05 20:18:10 +02002691 }
2692 vswap();
2693 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2694 vtop->r |= delayed_cast;
2695 }
2696}
2697
2698/* post defines POST/PRE add. c is the token ++ or -- */
88a3ccab grischka2009-12-20 01:53:49 +01002699ST_FUNC void inc(int post, int c)
0d1ed741 grischka2009-05-05 20:18:10 +02002700{
2701 test_lvalue();
2702 vdup(); /* save lvalue */
2703 if (post) {
5bcc3eed
TP
Thomas Preud'homme2015-03-10 23:23:00 +08002704 if (!nocode_wanted)
2705 gv_dup(); /* duplicate value */
2706 else
2707 vdup(); /* duplicate value */
0d1ed741 grischka2009-05-05 20:18:10 +02002708 vrotb(3);
2709 vrotb(3);
2710 }
2711 /* add constant */
2712 vpushi(c - TOK_MID);
2713 gen_op('+');
2714 vstore(); /* store value */
2715 if (post)
2716 vpop(); /* if post op, return saved value */
2717}
2718
2719/* Parse GNUC __attribute__ extension. Currently, the following
2720 extensions are recognized:
2721 - aligned(n) : set data/function alignment.
2722 - packed : force data alignment to 1
2723 - section(x) : generate data/code in this section.
2724 - unused : currently ignored, but may be used someday.
2725 - regparm(n) : pass function parameters in registers (i386 only)
2726 */
2727static void parse_attribute(AttributeDef *ad)
2728{
2729 int t, n;
2730
2731 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2732 next();
2733 skip('(');
2734 skip('(');
2735 while (tok != ')') {
2736 if (tok < TOK_IDENT)
2737 expect("attribute name");
2738 t = tok;
2739 next();
2740 switch(t) {
2741 case TOK_SECTION1:
2742 case TOK_SECTION2:
2743 skip('(');
2744 if (tok != TOK_STR)
2745 expect("section name");
2746 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2747 next();
2748 skip(')');
2749 break;
c93eca4f
JS
Joe Soroka2011-03-03 01:58:45 -08002750 case TOK_ALIAS1:
2751 case TOK_ALIAS2:
2752 skip('(');
2753 if (tok != TOK_STR)
2754 expect("alias(\"target\")");
2755 ad->alias_target = /* save string as token, for later */
2756 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2757 next();
2758 skip(')');
2759 break;
fbda78ae
MM
Michael Matz2014-04-14 02:53:11 +02002760 case TOK_VISIBILITY1:
2761 case TOK_VISIBILITY2:
2762 skip('(');
2763 if (tok != TOK_STR)
2764 expect("visibility(\"default|hidden|internal|protected\")");
2765 if (!strcmp (tokc.cstr->data, "default"))
2766 ad->a.visibility = STV_DEFAULT;
2767 else if (!strcmp (tokc.cstr->data, "hidden"))
2768 ad->a.visibility = STV_HIDDEN;
2769 else if (!strcmp (tokc.cstr->data, "internal"))
2770 ad->a.visibility = STV_INTERNAL;
2771 else if (!strcmp (tokc.cstr->data, "protected"))
2772 ad->a.visibility = STV_PROTECTED;
2773 else
2774 expect("visibility(\"default|hidden|internal|protected\")");
2775 next();
2776 skip(')');
2777 break;
0d1ed741 grischka2009-05-05 20:18:10 +02002778 case TOK_ALIGNED1:
2779 case TOK_ALIGNED2:
2780 if (tok == '(') {
2781 next();
2782 n = expr_const();
2783 if (n <= 0 || (n & (n - 1)) != 0)
bf374a5f grischka2011-08-11 17:07:56 +02002784 tcc_error("alignment must be a positive power of two");
0d1ed741 grischka2009-05-05 20:18:10 +02002785 skip(')');
2786 } else {
2787 n = MAX_ALIGN;
2788 }
3fe2a95d grischka2014-01-07 14:57:07 +01002789 ad->a.aligned = n;
0d1ed741 grischka2009-05-05 20:18:10 +02002790 break;
2791 case TOK_PACKED1:
2792 case TOK_PACKED2:
3fe2a95d grischka2014-01-07 14:57:07 +01002793 ad->a.packed = 1;
0d1ed741 grischka2009-05-05 20:18:10 +02002794 break;
95b9a477
MS
Manuel Simoni2010-02-27 17:37:59 +01002795 case TOK_WEAK1:
2796 case TOK_WEAK2:
3fe2a95d grischka2014-01-07 14:57:07 +01002797 ad->a.weak = 1;
95b9a477 Manuel Simoni2010-02-27 17:37:59 +01002798 break;
0d1ed741 grischka2009-05-05 20:18:10 +02002799 case TOK_UNUSED1:
2800 case TOK_UNUSED2:
2801 /* currently, no need to handle it because tcc does not
2802 track unused objects */
2803 break;
2804 case TOK_NORETURN1:
2805 case TOK_NORETURN2:
2806 /* currently, no need to handle it because tcc does not
2807 track unused objects */
2808 break;
2809 case TOK_CDECL1:
2810 case TOK_CDECL2:
2811 case TOK_CDECL3:
3fe2a95d grischka2014-01-07 14:57:07 +01002812 ad->a.func_call = FUNC_CDECL;
0d1ed741 grischka2009-05-05 20:18:10 +02002813 break;
2814 case TOK_STDCALL1:
2815 case TOK_STDCALL2:
2816 case TOK_STDCALL3:
3fe2a95d grischka2014-01-07 14:57:07 +01002817 ad->a.func_call = FUNC_STDCALL;
0d1ed741 grischka2009-05-05 20:18:10 +02002818 break;
2819#ifdef TCC_TARGET_I386
2820 case TOK_REGPARM1:
2821 case TOK_REGPARM2:
2822 skip('(');
2823 n = expr_const();
2824 if (n > 3)
2825 n = 3;
2826 else if (n < 0)
2827 n = 0;
2828 if (n > 0)
3fe2a95d grischka2014-01-07 14:57:07 +01002829 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
0d1ed741 grischka2009-05-05 20:18:10 +02002830 skip(')');
2831 break;
2832 case TOK_FASTCALL1:
2833 case TOK_FASTCALL2:
2834 case TOK_FASTCALL3:
3fe2a95d grischka2014-01-07 14:57:07 +01002835 ad->a.func_call = FUNC_FASTCALLW;
0d1ed741 grischka2009-05-05 20:18:10 +02002836 break;
2837#endif
a975008a
DR
Detlef Riekenberg2010-01-26 22:56:22 +01002838 case TOK_MODE:
2839 skip('(');
2840 switch(tok) {
2841 case TOK_MODE_DI:
3fe2a95d grischka2014-01-07 14:57:07 +01002842 ad->a.mode = VT_LLONG + 1;
a975008a
DR
Detlef Riekenberg2010-01-26 22:56:22 +01002843 break;
2844 case TOK_MODE_HI:
3fe2a95d grischka2014-01-07 14:57:07 +01002845 ad->a.mode = VT_SHORT + 1;
a975008a
DR
Detlef Riekenberg2010-01-26 22:56:22 +01002846 break;
2847 case TOK_MODE_SI:
3fe2a95d grischka2014-01-07 14:57:07 +01002848 ad->a.mode = VT_INT + 1;
a975008a
DR
Detlef Riekenberg2010-01-26 22:56:22 +01002849 break;
2850 default:
bf374a5f grischka2011-08-11 17:07:56 +02002851 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
a975008a
DR
Detlef Riekenberg2010-01-26 22:56:22 +01002852 break;
2853 }
2854 next();
2855 skip(')');
2856 break;
0d1ed741 grischka2009-05-05 20:18:10 +02002857 case TOK_DLLEXPORT:
3fe2a95d grischka2014-01-07 14:57:07 +01002858 ad->a.func_export = 1;
0d1ed741 grischka2009-05-05 20:18:10 +02002859 break;
5b113f3e grischka2009-11-13 17:14:05 +01002860 case TOK_DLLIMPORT:
3fe2a95d grischka2014-01-07 14:57:07 +01002861 ad->a.func_import = 1;
5b113f3e grischka2009-11-13 17:14:05 +01002862 break;
0d1ed741 grischka2009-05-05 20:18:10 +02002863 default:
2864 if (tcc_state->warn_unsupported)
bf374a5f grischka2011-08-11 17:07:56 +02002865 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
0d1ed741 grischka2009-05-05 20:18:10 +02002866 /* skip parameters */
2867 if (tok == '(') {
2868 int parenthesis = 0;
2869 do {
2870 if (tok == '(')
2871 parenthesis++;
2872 else if (tok == ')')
2873 parenthesis--;
2874 next();
2875 } while (parenthesis && tok != -1);
2876 }
2877 break;
2878 }
2879 if (tok != ',')
2880 break;
2881 next();
2882 }
2883 skip(')');
2884 skip(')');
2885 }
2886}
2887
2888/* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
82969f04 Thomas Preud'homme2013-09-20 21:16:53 +02002889static void struct_decl(CType *type, int u, int tdef)
0d1ed741 grischka2009-05-05 20:18:10 +02002890{
3b07a15f Amine Najahi2013-10-06 14:43:16 +02002891 int a, v, size, align, maxalign, c, offset, flexible;
06a7c415 Joe Soroka2011-03-18 17:47:35 -07002892 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
0d1ed741 grischka2009-05-05 20:18:10 +02002893 Sym *s, *ss, *ass, **ps;
2894 AttributeDef ad;
2895 CType type1, btype;
2896
2897 a = tok; /* save decl type */
2898 next();
2899 if (tok != '{') {
2900 v = tok;
2901 next();
2902 /* struct already defined ? return it */
2903 if (v < TOK_IDENT)
2904 expect("struct/union/enum name");
2905 s = struct_find(v);
2906 if (s) {
2907 if (s->type.t != a)
bf374a5f grischka2011-08-11 17:07:56 +02002908 tcc_error("invalid type");
0d1ed741 grischka2009-05-05 20:18:10 +02002909 goto do_decl;
82969f04
TP
Thomas Preud'homme2013-09-20 21:16:53 +02002910 } else if (tok >= TOK_IDENT && !tdef)
2911 tcc_error("unknown struct/union/enum");
0d1ed741 grischka2009-05-05 20:18:10 +02002912 } else {
2913 v = anon_sym++;
2914 }
2915 type1.t = a;
69c2e7f9 grischka2013-07-24 17:06:13 +02002916 type1.ref = NULL;
0d1ed741 grischka2009-05-05 20:18:10 +02002917 /* we put an undefined size for struct/union */
2918 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2919 s->r = 0; /* default alignment is zero as gcc */
2920 /* put struct/union/enum name in type */
2921 do_decl:
2922 type->t = u;
2923 type->ref = s;
2924
2925 if (tok == '{') {
2926 next();
2927 if (s->c != -1)
bf374a5f grischka2011-08-11 17:07:56 +02002928 tcc_error("struct/union/enum already defined");
0d1ed741 grischka2009-05-05 20:18:10 +02002929 /* cannot be empty */
2930 c = 0;
2931 /* non empty enums are not allowed */
2932 if (a == TOK_ENUM) {
2933 for(;;) {
2934 v = tok;
2935 if (v < TOK_UIDENT)
2936 expect("identifier");
673befd2 Thomas Preud'homme2013-09-20 22:49:49 +02002937 ss = sym_find(v);
3e565842 Thomas Preud'homme2014-03-31 22:58:17 +08002938 if (ss && !local_stack)
673befd2
TP
Thomas Preud'homme2013-09-20 22:49:49 +02002939 tcc_error("redefinition of enumerator '%s'",
2940 get_tok_str(v, NULL));
0d1ed741 grischka2009-05-05 20:18:10 +02002941 next();
2942 if (tok == '=') {
2943 next();
2944 c = expr_const();
2945 }
2946 /* enum symbols have static storage */
2947 ss = sym_push(v, &int_type, VT_CONST, c);
2948 ss->type.t |= VT_STATIC;
2949 if (tok != ',')
2950 break;
2951 next();
2952 c++;
2953 /* NOTE: we accept a trailing comma */
2954 if (tok == '}')
2955 break;
2956 }
0f522fb3 Thomas Preud'homme2013-09-20 01:06:43 +02002957 s->c = type_size(&int_type, &align);
0d1ed741 grischka2009-05-05 20:18:10 +02002958 skip('}');
2959 } else {
2960 maxalign = 1;
2961 ps = &s->next;
2962 prevbt = VT_INT;
2963 bit_pos = 0;
2964 offset = 0;
3b07a15f Amine Najahi2013-10-06 14:43:16 +02002965 flexible = 0;
0d1ed741 grischka2009-05-05 20:18:10 +02002966 while (tok != '}') {
370547a5 Thomas Preud'homme2013-01-31 13:02:04 +01002967 parse_btype(&btype, &ad);
0d1ed741 grischka2009-05-05 20:18:10 +02002968 while (1) {
3b07a15f
AN
Amine Najahi2013-10-06 14:43:16 +02002969 if (flexible)
2970 tcc_error("flexible array member '%s' not at the end of struct",
2971 get_tok_str(v, NULL));
0d1ed741 grischka2009-05-05 20:18:10 +02002972 bit_size = -1;
2973 v = 0;
2974 type1 = btype;
2975 if (tok != ':') {
2976 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
dec95935 seyko2015-04-10 06:31:58 +03002977 if (v == 0) {
2978 if ((type1.t & VT_BTYPE) != VT_STRUCT)
2979 expect("identifier");
2980 else {
2981 int v = btype.ref->v;
2982 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
2983 if (tcc_state->ms_extensions == 0)
2984 expect("identifier");
2985 }
2986 }
2987 }
3b07a15f
AN
Amine Najahi2013-10-06 14:43:16 +02002988 if (type_size(&type1, &align) < 0) {
2989 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2990 flexible = 1;
2991 else
2992 tcc_error("field '%s' has incomplete type",
2993 get_tok_str(v, NULL));
2994 }
0d1ed741 grischka2009-05-05 20:18:10 +02002995 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2996 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
bf374a5f grischka2011-08-11 17:07:56 +02002997 tcc_error("invalid type for '%s'",
0d1ed741 grischka2009-05-05 20:18:10 +02002998 get_tok_str(v, NULL));
2999 }
3000 if (tok == ':') {
3001 next();
3002 bit_size = expr_const();
3003 /* XXX: handle v = 0 case for messages */
3004 if (bit_size < 0)
bf374a5f grischka2011-08-11 17:07:56 +02003005 tcc_error("negative width in bit-field '%s'",
0d1ed741 grischka2009-05-05 20:18:10 +02003006 get_tok_str(v, NULL));
3007 if (v && bit_size == 0)
bf374a5f grischka2011-08-11 17:07:56 +02003008 tcc_error("zero width for bit-field '%s'",
0d1ed741 grischka2009-05-05 20:18:10 +02003009 get_tok_str(v, NULL));
3010 }
3011 size = type_size(&type1, &align);
3fe2a95d grischka2014-01-07 14:57:07 +01003012 if (ad.a.aligned) {
3013 if (align < ad.a.aligned)
3014 align = ad.a.aligned;
3015 } else if (ad.a.packed) {
0d1ed741 grischka2009-05-05 20:18:10 +02003016 align = 1;
3017 } else if (*tcc_state->pack_stack_ptr) {
3018 if (align > *tcc_state->pack_stack_ptr)
3019 align = *tcc_state->pack_stack_ptr;
3020 }
3021 lbit_pos = 0;
3022 if (bit_size >= 0) {
3023 bt = type1.t & VT_BTYPE;
3024 if (bt != VT_INT &&
3025 bt != VT_BYTE &&
3026 bt != VT_SHORT &&
3027 bt != VT_BOOL &&
3028 bt != VT_ENUM &&
3029 bt != VT_LLONG)
bf374a5f grischka2011-08-11 17:07:56 +02003030 tcc_error("bitfields must have scalar type");
0d1ed741 grischka2009-05-05 20:18:10 +02003031 bsize = size * 8;
3032 if (bit_size > bsize) {
bf374a5f grischka2011-08-11 17:07:56 +02003033 tcc_error("width of '%s' exceeds its type",
0d1ed741 grischka2009-05-05 20:18:10 +02003034 get_tok_str(v, NULL));
3035 } else if (bit_size == bsize) {
3036 /* no need for bit fields */
3037 bit_pos = 0;
3038 } else if (bit_size == 0) {
3039 /* XXX: what to do if only padding in a
3040 structure ? */
3041 /* zero size: means to pad */
3042 bit_pos = 0;
3043 } else {
3044 /* we do not have enough room ?
3045 did the type change?
3046 is it a union? */
3047 if ((bit_pos + bit_size) > bsize ||
3048 bt != prevbt || a == TOK_UNION)
3049 bit_pos = 0;
3050 lbit_pos = bit_pos;
3051 /* XXX: handle LSB first */
3052 type1.t |= VT_BITFIELD |
3053 (bit_pos << VT_STRUCT_SHIFT) |
3054 (bit_size << (VT_STRUCT_SHIFT + 6));
3055 bit_pos += bit_size;
3056 }
3057 prevbt = bt;
3058 } else {
3059 bit_pos = 0;
3060 }
3061 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
3062 /* add new memory data only if starting
3063 bit field */
3064 if (lbit_pos == 0) {
3065 if (a == TOK_STRUCT) {
3066 c = (c + align - 1) & -align;
3067 offset = c;
3068 if (size > 0)
3069 c += size;
3070 } else {
3071 offset = 0;
3072 if (size > c)
3073 c = size;
3074 }
3075 if (align > maxalign)
3076 maxalign = align;
3077 }
3078#if 0
3079 printf("add field %s offset=%d",
3080 get_tok_str(v, NULL), offset);
3081 if (type1.t & VT_BITFIELD) {
3082 printf(" pos=%d size=%d",
3083 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
3084 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
3085 }
3086 printf("\n");
3087#endif
3088 }
3089 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
3090 ass = type1.ref;
3091 while ((ass = ass->next) != NULL) {
3092 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
3093 *ps = ss;
3094 ps = &ss->next;
3095 }
3096 } else if (v) {
3097 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
3098 *ps = ss;
3099 ps = &ss->next;
3100 }
3101 if (tok == ';' || tok == TOK_EOF)
3102 break;
3103 skip(',');
3104 }
3105 skip(';');
3106 }
3107 skip('}');
3108 /* store size and alignment */
3109 s->c = (c + maxalign - 1) & -maxalign;
06a7c415 Joe Soroka2011-03-18 17:47:35 -07003110 s->r = maxalign;
0d1ed741 grischka2009-05-05 20:18:10 +02003111 }
3112 }
3113}
3114
b68499e9 Thomas Preud'homme2014-03-24 23:28:56 +08003115/* return 1 if basic type is a type size (short, long, long long) */
822f4630 Urs Janssen2014-04-10 11:53:54 +02003116ST_FUNC int is_btype_size(int bt)
b68499e9
TP
Thomas Preud'homme2014-03-24 23:28:56 +08003117{
3118 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
3119}
3120
0d1ed741 grischka2009-05-05 20:18:10 +02003121/* return 0 if no type declaration. otherwise, return the basic type
3122 and skip it.
3123 */
3124static int parse_btype(CType *type, AttributeDef *ad)
3125{
b68499e9 Thomas Preud'homme2014-03-24 23:28:56 +08003126 int t, u, bt_size, complete, type_found, typespec_found;
0d1ed741 grischka2009-05-05 20:18:10 +02003127 Sym *s;
3128 CType type1;
3129
3130 memset(ad, 0, sizeof(AttributeDef));
b68499e9 Thomas Preud'homme2014-03-24 23:28:56 +08003131 complete = 0;
0d1ed741 grischka2009-05-05 20:18:10 +02003132 type_found = 0;
3133 typespec_found = 0;
0d1ed741 grischka2009-05-05 20:18:10 +02003134 t = 0;
3135 while(1) {
3136 switch(tok) {
3137 case TOK_EXTENSION:
3138 /* currently, we really ignore extension */
3139 next();
3140 continue;
3141
3142 /* basic types */
3143 case TOK_CHAR:
3144 u = VT_BYTE;
3145 basic_type:
3146 next();
3147 basic_type1:
b68499e9 Thomas Preud'homme2014-03-24 23:28:56 +08003148 if (complete)
bf374a5f grischka2011-08-11 17:07:56 +02003149 tcc_error("too many basic types");
0d1ed741 grischka2009-05-05 20:18:10 +02003150 t |= u;
b68499e9
TP
Thomas Preud'homme2014-03-24 23:28:56 +08003151 bt_size = is_btype_size (u & VT_BTYPE);
3152 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3153 complete = 1;
0d1ed741 grischka2009-05-05 20:18:10 +02003154 typespec_found = 1;
3155 break;
3156 case TOK_VOID:
3157 u = VT_VOID;
3158 goto basic_type;
3159 case TOK_SHORT:
3160 u = VT_SHORT;
3161 goto basic_type;
3162 case TOK_INT:
b68499e9
TP
Thomas Preud'homme2014-03-24 23:28:56 +08003163 u = VT_INT;
3164 goto basic_type;
0d1ed741 grischka2009-05-05 20:18:10 +02003165 case TOK_LONG:
3166 next();
3167 if ((t & VT_BTYPE) == VT_DOUBLE) {
9fda4f42 grischka2009-07-18 22:07:17 +02003168#ifndef TCC_TARGET_PE
0d1ed741 grischka2009-05-05 20:18:10 +02003169 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
9fda4f42 grischka2009-07-18 22:07:17 +02003170#endif
0d1ed741 grischka2009-05-05 20:18:10 +02003171 } else if ((t & VT_BTYPE) == VT_LONG) {
3172 t = (t & ~VT_BTYPE) | VT_LLONG;
3173 } else {
3174 u = VT_LONG;
3175 goto basic_type1;
3176 }
3177 break;
b14ef0e2
EGE
Edmund Grimley Evans2015-02-13 18:58:31 +00003178#ifdef TCC_TARGET_ARM64
3179 case TOK_UINT128:
3180 /* GCC's __uint128_t appears in some Linux header files. Make it a
3181 synonym for long double to get the size and alignment right. */
3182 u = VT_LDOUBLE;
3183 goto basic_type;
3184#endif
0d1ed741 grischka2009-05-05 20:18:10 +02003185 case TOK_BOOL:
3186 u = VT_BOOL;
3187 goto basic_type;
3188 case TOK_FLOAT:
3189 u = VT_FLOAT;
3190 goto basic_type;
3191 case TOK_DOUBLE:
3192 next();
3193 if ((t & VT_BTYPE) == VT_LONG) {
9fda4f42 grischka2009-07-18 22:07:17 +02003194#ifdef TCC_TARGET_PE
3195 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3196#else
0d1ed741 grischka2009-05-05 20:18:10 +02003197 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
9fda4f42 grischka2009-07-18 22:07:17 +02003198#endif
0d1ed741 grischka2009-05-05 20:18:10 +02003199 } else {
3200 u = VT_DOUBLE;
3201 goto basic_type1;
3202 }
3203 break;
3204 case TOK_ENUM:
bbf8221e seyko2015-03-03 15:00:13 +03003205 struct_decl(&type1, VT_ENUM, t & (VT_TYPEDEF | VT_EXTERN));
0d1ed741 grischka2009-05-05 20:18:10 +02003206 basic_type2:
3207 u = type1.t;
3208 type->ref = type1.ref;
3209 goto basic_type1;
3210 case TOK_STRUCT:
3211 case TOK_UNION:
bbf8221e seyko2015-03-03 15:00:13 +03003212 struct_decl(&type1, VT_STRUCT, t & (VT_TYPEDEF | VT_EXTERN));
0d1ed741 grischka2009-05-05 20:18:10 +02003213 goto basic_type2;
3214
3215 /* type modifiers */
3216 case TOK_CONST1:
3217 case TOK_CONST2:
3218 case TOK_CONST3:
3219 t |= VT_CONSTANT;
3220 next();
3221 break;
3222 case TOK_VOLATILE1:
3223 case TOK_VOLATILE2:
3224 case TOK_VOLATILE3:
3225 t |= VT_VOLATILE;
3226 next();
3227 break;
3228 case TOK_SIGNED1:
3229 case TOK_SIGNED2:
3230 case TOK_SIGNED3:
b0b5165d
TP
Thomas Preud'homme2014-02-06 20:51:47 +08003231 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3232 tcc_error("signed and unsigned modifier");
0d1ed741 grischka2009-05-05 20:18:10 +02003233 typespec_found = 1;
b0b5165d Thomas Preud'homme2014-02-06 20:51:47 +08003234 t |= VT_DEFSIGN;
0d1ed741 grischka2009-05-05 20:18:10 +02003235 next();
3236 break;
3237 case TOK_REGISTER:
3238 case TOK_AUTO:
3239 case TOK_RESTRICT1:
3240 case TOK_RESTRICT2:
3241 case TOK_RESTRICT3:
3242 next();
3243 break;
3244 case TOK_UNSIGNED:
b0b5165d
TP
Thomas Preud'homme2014-02-06 20:51:47 +08003245 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3246 tcc_error("signed and unsigned modifier");
3247 t |= VT_DEFSIGN | VT_UNSIGNED;
0d1ed741 grischka2009-05-05 20:18:10 +02003248 next();
3249 typespec_found = 1;
3250 break;
3251
3252 /* storage */
3253 case TOK_EXTERN:
3254 t |= VT_EXTERN;
3255 next();
3256 break;
3257 case TOK_STATIC:
3258 t |= VT_STATIC;
3259 next();
3260 break;
3261 case TOK_TYPEDEF:
3262 t |= VT_TYPEDEF;
3263 next();
3264 break;
3265 case TOK_INLINE1:
3266 case TOK_INLINE2:
3267 case TOK_INLINE3:
3268 t |= VT_INLINE;
3269 next();
3270 break;
3271
3272 /* GNUC attribute */
3273 case TOK_ATTRIBUTE1:
3274 case TOK_ATTRIBUTE2:
3275 parse_attribute(ad);
3fe2a95d grischka2014-01-07 14:57:07 +01003276 if (ad->a.mode) {
3277 u = ad->a.mode -1;
a975008a
DR
Detlef Riekenberg2010-01-26 22:56:22 +01003278 t = (t & ~VT_BTYPE) | u;
3279 }
0d1ed741 grischka2009-05-05 20:18:10 +02003280 break;
3281 /* GNUC typeof */
3282 case TOK_TYPEOF1:
3283 case TOK_TYPEOF2:
3284 case TOK_TYPEOF3:
3285 next();
3286 parse_expr_type(&type1);
38cbb40e
JS
Joe Soroka2011-03-06 22:32:35 -08003287 /* remove all storage modifiers except typedef */
3288 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
0d1ed741 grischka2009-05-05 20:18:10 +02003289 goto basic_type2;
3290 default:
b68499e9 Thomas Preud'homme2014-03-24 23:28:56 +08003291 if (typespec_found)
0d1ed741 grischka2009-05-05 20:18:10 +02003292 goto the_end;
3293 s = sym_find(tok);
3294 if (!s || !(s->type.t & VT_TYPEDEF))
3295 goto the_end;
0d1ed741 grischka2009-05-05 20:18:10 +02003296 t |= (s->type.t & ~VT_TYPEDEF);
3297 type->ref = s->type.ref;
56d6abdb grischka2009-12-06 17:37:33 +01003298 if (s->r) {
3299 /* get attributes from typedef */
3fe2a95d grischka2014-01-07 14:57:07 +01003300 if (0 == ad->a.aligned)
3301 ad->a.aligned = s->a.aligned;
3302 if (0 == ad->a.func_call)
3303 ad->a.func_call = s->a.func_call;
3304 ad->a.packed |= s->a.packed;
56d6abdb grischka2009-12-06 17:37:33 +01003305 }
0d1ed741 grischka2009-05-05 20:18:10 +02003306 next();
3307 typespec_found = 1;
3308 break;
3309 }
3310 type_found = 1;
3311 }
3312the_end:
0d1ed741 grischka2009-05-05 20:18:10 +02003313 if (tcc_state->char_is_unsigned) {
b0b5165d Thomas Preud'homme2014-02-06 20:51:47 +08003314 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
0d1ed741 grischka2009-05-05 20:18:10 +02003315 t |= VT_UNSIGNED;
3316 }
0d1ed741 grischka2009-05-05 20:18:10 +02003317
3318 /* long is never used as type */
3319 if ((t & VT_BTYPE) == VT_LONG)
b14ef0e2
EGE
Edmund Grimley Evans2015-02-13 18:58:31 +00003320#if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3321 defined TCC_TARGET_PE
0d1ed741 grischka2009-05-05 20:18:10 +02003322 t = (t & ~VT_BTYPE) | VT_INT;
3323#else
3324 t = (t & ~VT_BTYPE) | VT_LLONG;
3325#endif
3326 type->t = t;
3327 return type_found;
3328}
3329
3330/* convert a function parameter type (array to pointer and function to
3331 function pointer) */
3332static inline void convert_parameter_type(CType *pt)
3333{
3334 /* remove const and volatile qualifiers (XXX: const could be used
3335 to indicate a const function parameter */
3336 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3337 /* array must be transformed to pointer according to ANSI C */
3338 pt->t &= ~VT_ARRAY;
3339 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3340 mk_pointer(pt);
3341 }
3342}
3343
b8adf009
TP
Thomas Preud'homme2010-09-14 19:17:39 +02003344ST_FUNC void parse_asm_str(CString *astr)
3345{
3346 skip('(');
3347 /* read the string */
3348 if (tok != TOK_STR)
3349 expect("string constant");
3350 cstr_new(astr);
3351 while (tok == TOK_STR) {
3352 /* XXX: add \0 handling too ? */
3353 cstr_cat(astr, tokc.cstr->data);
3354 next();
3355 }
3356 cstr_ccat(astr, '\0');
3357}
3358
3359/* Parse an asm label and return the label
3360 * Don't forget to free the CString in the caller! */
3361static void asm_label_instr(CString *astr)
3362{
3363 next();
3364 parse_asm_str(astr);
3365 skip(')');
3366#ifdef ASM_DEBUG
3367 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3368#endif
3369}
3370
b3a8eed4 Joe Soroka2011-03-08 12:56:13 -08003371static void post_type(CType *type, AttributeDef *ad)
0d1ed741 grischka2009-05-05 20:18:10 +02003372{
3373 int n, l, t1, arg_size, align;
3374 Sym **plast, *s, *first;
3375 AttributeDef ad1;
3376 CType pt;
3377
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003378 if (tok == '(') {
3379 /* function declaration */
3380 next();
3381 l = 0;
3382 first = NULL;
3383 plast = &first;
3384 arg_size = 0;
3385 if (tok != ')') {
3386 for(;;) {
3387 /* read param name and compute offset */
3388 if (l != FUNC_OLD) {
3389 if (!parse_btype(&pt, &ad1)) {
3390 if (l) {
bf374a5f grischka2011-08-11 17:07:56 +02003391 tcc_error("invalid type");
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003392 } else {
3393 l = FUNC_OLD;
3394 goto old_proto;
3395 }
0d1ed741 grischka2009-05-05 20:18:10 +02003396 }
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003397 l = FUNC_NEW;
3398 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3399 break;
3400 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3401 if ((pt.t & VT_BTYPE) == VT_VOID)
bf374a5f grischka2011-08-11 17:07:56 +02003402 tcc_error("parameter declared as void");
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003403 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3404 } else {
3405 old_proto:
3406 n = tok;
3407 if (n < TOK_UIDENT)
3408 expect("identifier");
3409 pt.t = VT_INT;
3410 next();
0d1ed741 grischka2009-05-05 20:18:10 +02003411 }
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003412 convert_parameter_type(&pt);
3413 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3414 *plast = s;
3415 plast = &s->next;
3416 if (tok == ')')
0d1ed741 grischka2009-05-05 20:18:10 +02003417 break;
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003418 skip(',');
3419 if (l == FUNC_NEW && tok == TOK_DOTS) {
3420 l = FUNC_ELLIPSIS;
3421 next();
3422 break;
3423 }
0d1ed741 grischka2009-05-05 20:18:10 +02003424 }
3425 }
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003426 /* if no parameters, then old type prototype */
3427 if (l == 0)
3428 l = FUNC_OLD;
3429 skip(')');
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003430 /* NOTE: const is ignored in returned type as it has a special
3431 meaning in gcc / C++ */
9ff91d4c Joe Soroka2011-03-08 15:12:09 -08003432 type->t &= ~VT_CONSTANT;
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003433 /* some ancient pre-K&R C allows a function to return an array
3434 and the array brackets to be put after the arguments, such
9ff91d4c
JS
Joe Soroka2011-03-08 15:12:09 -08003435 that "int c()[]" means something like "int[] c()" */
3436 if (tok == '[') {
3437 next();
3438 skip(']'); /* only handle simple "[]" */
3439 type->t |= VT_PTR;
3440 }
b3a8eed4 Joe Soroka2011-03-08 12:56:13 -08003441 /* we push a anonymous symbol which will contain the function prototype */
3fe2a95d grischka2014-01-07 14:57:07 +01003442 ad->a.func_args = arg_size;
3443 s = sym_push(SYM_FIELD, type, 0, l);
3444 s->a = ad->a;
b3a8eed4 Joe Soroka2011-03-08 12:56:13 -08003445 s->next = first;
810aca9e Joe Soroka2011-04-06 12:08:50 -07003446 type->t = VT_FUNC;
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003447 type->ref = s;
3448 } else if (tok == '[') {
3449 /* array definition */
0d1ed741 grischka2009-05-05 20:18:10 +02003450 next();
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003451 if (tok == TOK_RESTRICT1)
3452 next();
3453 n = -1;
810aca9e Joe Soroka2011-04-06 12:08:50 -07003454 t1 = 0;
b3a8eed4 Joe Soroka2011-03-08 12:56:13 -08003455 if (tok != ']') {
9b52e16a
JS
Joe Soroka2011-07-22 02:09:28 -07003456 if (!local_stack || nocode_wanted)
3457 vpushi(expr_const());
3458 else gexpr();
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07003459 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3460 n = vtop->c.i;
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07003461 if (n < 0)
bf374a5f grischka2011-08-11 17:07:56 +02003462 tcc_error("invalid array size");
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07003463 } else {
3464 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
bf374a5f grischka2011-08-11 17:07:56 +02003465 tcc_error("size of variable length array should be an integer");
810aca9e Joe Soroka2011-04-06 12:08:50 -07003466 t1 = VT_VLA;
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07003467 }
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003468 }
3469 skip(']');
3470 /* parse next post type */
b3a8eed4 Joe Soroka2011-03-08 12:56:13 -08003471 post_type(type, ad);
a465b7f5
TP
Thomas Preud'homme2013-09-19 18:58:46 +02003472 if (type->t == VT_FUNC)
3473 tcc_error("declaration of an array of functions");
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07003474 t1 |= type->t & VT_VLA;
b3a8eed4 Joe Soroka2011-03-08 12:56:13 -08003475
1446b543
JS
Joe Soroka2011-04-09 22:52:25 -07003476 if (t1 & VT_VLA) {
3477 loc -= type_size(&int_type, &align);
3478 loc &= -align;
3479 n = loc;
3480
3481 vla_runtime_type_size(type, &align);
3482 gen_op('*');
44c330d6 Philip2015-04-28 09:23:29 +00003483 vset(&int_type, VT_LOCAL|VT_LVAL, n);
1446b543
JS
Joe Soroka2011-04-09 22:52:25 -07003484 vswap();
3485 vstore();
3486 }
3487 if (n != -1)
3488 vpop();
3489
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -07003490 /* we push an anonymous symbol which will contain the array
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003491 element type */
3492 s = sym_push(SYM_FIELD, type, 0, n);
7c7ca3c6 Joe Soroka2011-04-08 01:09:39 -07003493 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
b3a8eed4
JS
Joe Soroka2011-03-08 12:56:13 -08003494 type->ref = s;
3495 }
0d1ed741 grischka2009-05-05 20:18:10 +02003496}
3497
3498/* Parse a type declaration (except basic type), and return the type
3499 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3500 expected. 'type' should contain the basic type. 'ad' is the
3501 attribute definition of the basic type. It can be modified by
3502 type_decl().
3503 */
3504static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3505{
3506 Sym *s;
3507 CType type1, *type2;
2358b378 grischka2013-01-06 17:20:44 +01003508 int qualifiers, storage;
3509
0d1ed741 grischka2009-05-05 20:18:10 +02003510 while (tok == '*') {
3511 qualifiers = 0;
3512 redo:
3513 next();
3514 switch(tok) {
3515 case TOK_CONST1:
3516 case TOK_CONST2:
3517 case TOK_CONST3:
3518 qualifiers |= VT_CONSTANT;
3519 goto redo;
3520 case TOK_VOLATILE1:
3521 case TOK_VOLATILE2:
3522 case TOK_VOLATILE3:
3523 qualifiers |= VT_VOLATILE;
3524 goto redo;
3525 case TOK_RESTRICT1:
3526 case TOK_RESTRICT2:
3527 case TOK_RESTRICT3:
3528 goto redo;
3529 }
3530 mk_pointer(type);
3531 type->t |= qualifiers;
3532 }
3533
3534 /* XXX: clarify attribute handling */
3535 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3536 parse_attribute(ad);
3537
3538 /* recursive type */
3539 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3540 type1.t = 0; /* XXX: same as int */
3541 if (tok == '(') {
3542 next();
3543 /* XXX: this is not correct to modify 'ad' at this point, but
3544 the syntax is not clear */
3545 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3546 parse_attribute(ad);
3547 type_decl(&type1, ad, v, td);
3548 skip(')');
3549 } else {
3550 /* type identifier */
3551 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3552 *v = tok;
3553 next();
3554 } else {
3555 if (!(td & TYPE_ABSTRACT))
3556 expect("identifier");
3557 *v = 0;
3558 }
3559 }
810aca9e
JS
Joe Soroka2011-04-06 12:08:50 -07003560 storage = type->t & VT_STORAGE;
3561 type->t &= ~VT_STORAGE;
85f6fad3 Thomas Preud'homme2012-10-25 18:07:13 +02003562 if (storage & VT_STATIC) {
2358b378 grischka2013-01-06 17:20:44 +01003563 int saved_nocode_wanted = nocode_wanted;
85f6fad3 Thomas Preud'homme2012-10-25 18:07:13 +02003564 nocode_wanted = 1;
2358b378 grischka2013-01-06 17:20:44 +01003565 post_type(type, ad);
508df168 Thomas Preud'homme2012-10-25 20:14:55 +02003566 nocode_wanted = saved_nocode_wanted;
2358b378 grischka2013-01-06 17:20:44 +01003567 } else
3568 post_type(type, ad);
810aca9e Joe Soroka2011-04-06 12:08:50 -07003569 type->t |= storage;
0d1ed741 grischka2009-05-05 20:18:10 +02003570 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3571 parse_attribute(ad);
4d5105c8 Joe Soroka2011-02-01 00:37:53 -08003572
0d1ed741 grischka2009-05-05 20:18:10 +02003573 if (!type1.t)
3574 return;
3575 /* append type at the end of type1 */
3576 type2 = &type1;
3577 for(;;) {
3578 s = type2->ref;
3579 type2 = &s->type;
3580 if (!type2->t) {
3581 *type2 = *type;
3582 break;
3583 }
3584 }
3585 *type = type1;
3586}
3587
3588/* compute the lvalue VT_LVAL_xxx needed to match type t. */
88a3ccab grischka2009-12-20 01:53:49 +01003589ST_FUNC int lvalue_type(int t)
0d1ed741 grischka2009-05-05 20:18:10 +02003590{
3591 int bt, r;
3592 r = VT_LVAL;
3593 bt = t & VT_BTYPE;
3594 if (bt == VT_BYTE || bt == VT_BOOL)
3595 r |= VT_LVAL_BYTE;
3596 else if (bt == VT_SHORT)
3597 r |= VT_LVAL_SHORT;
3598 else
3599 return r;
3600 if (t & VT_UNSIGNED)
3601 r |= VT_LVAL_UNSIGNED;
3602 return r;
3603}
3604
3605/* indirection with full error checking and bound check */
88a3ccab grischka2009-12-20 01:53:49 +01003606ST_FUNC void indir(void)
0d1ed741 grischka2009-05-05 20:18:10 +02003607{
3608 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3609 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3610 return;
3611 expect("pointer");
3612 }
3613 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3614 gv(RC_INT);
3615 vtop->type = *pointed_type(&vtop->type);
3616 /* Arrays and functions are never lvalues */
7c7ca3c6 Joe Soroka2011-04-08 01:09:39 -07003617 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
0d1ed741 grischka2009-05-05 20:18:10 +02003618 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3619 vtop->r |= lvalue_type(vtop->type.t);
3620 /* if bound checking, the referenced pointer must be checked */
7fa712e0 grischka2009-12-19 22:22:43 +01003621#ifdef CONFIG_TCC_BCHECK
f9181416 grischka2009-05-11 18:45:44 +02003622 if (tcc_state->do_bounds_check)
0d1ed741 grischka2009-05-05 20:18:10 +02003623 vtop->r |= VT_MUSTBOUND;
7fa712e0 grischka2009-12-19 22:22:43 +01003624#endif
0d1ed741 grischka2009-05-05 20:18:10 +02003625 }
3626}
3627
3628/* pass a parameter to a function and do type checking and casting */
3629static void gfunc_param_typed(Sym *func, Sym *arg)
3630{
3631 int func_type;
3632 CType type;
3633
3634 func_type = func->c;
3635 if (func_type == FUNC_OLD ||
3636 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3637 /* default casting : only need to convert float to double */
3638 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3639 type.t = VT_DOUBLE;
3640 gen_cast(&type);
17314a1f
TP
Thomas Preud'homme2014-02-04 20:54:28 +08003641 } else if (vtop->type.t & VT_BITFIELD) {
3642 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3643 gen_cast(&type);
0d1ed741 grischka2009-05-05 20:18:10 +02003644 }
3645 } else if (arg == NULL) {
bf374a5f grischka2011-08-11 17:07:56 +02003646 tcc_error("too many arguments to function");
0d1ed741 grischka2009-05-05 20:18:10 +02003647 } else {
3648 type = arg->type;
3649 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3650 gen_assign_cast(&type);
3651 }
3652}
3653
3654/* parse an expression of the form '(type)' or '(expr)' and return its
3655 type */
3656static void parse_expr_type(CType *type)
3657{
3658 int n;
3659 AttributeDef ad;
3660
3661 skip('(');
3662 if (parse_btype(type, &ad)) {
3663 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3664 } else {
3665 expr_type(type);
3666 }
3667 skip(')');
3668}
3669
3670static void parse_type(CType *type)
3671{
3672 AttributeDef ad;
3673 int n;
3674
3675 if (!parse_btype(type, &ad)) {
3676 expect("type");
3677 }
3678 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3679}
3680
3681static void vpush_tokc(int t)
3682{
3683 CType type;
3684 type.t = t;
41e11236 grischka2009-12-19 22:10:13 +01003685 type.ref = 0;
0d1ed741 grischka2009-05-05 20:18:10 +02003686 vsetc(&type, VT_CONST, &tokc);
3687}
3688
88a3ccab grischka2009-12-20 01:53:49 +01003689ST_FUNC void unary(void)
0d1ed741 grischka2009-05-05 20:18:10 +02003690{
8de9b7a6 Thomas Preud'homme2010-04-11 01:53:40 +02003691 int n, t, align, size, r, sizeof_caller;
0d1ed741 grischka2009-05-05 20:18:10 +02003692 CType type;
3693 Sym *s;
3694 AttributeDef ad;
8de9b7a6 Thomas Preud'homme2010-04-11 01:53:40 +02003695 static int in_sizeof = 0;
0d1ed741 grischka2009-05-05 20:18:10 +02003696
8de9b7a6
TP
Thomas Preud'homme2010-04-11 01:53:40 +02003697 sizeof_caller = in_sizeof;
3698 in_sizeof = 0;
0d1ed741 grischka2009-05-05 20:18:10 +02003699 /* XXX: GCC 2.95.3 does not generate a table although it should be
3700 better here */
3701 tok_next:
3702 switch(tok) {
3703 case TOK_EXTENSION:
3704 next();
3705 goto tok_next;
3706 case TOK_CINT:
3707 case TOK_CCHAR:
3708 case TOK_LCHAR:
3709 vpushi(tokc.i);
3710 next();
3711 break;
3712 case TOK_CUINT:
3713 vpush_tokc(VT_INT | VT_UNSIGNED);
3714 next();
3715 break;
3716 case TOK_CLLONG:
3717 vpush_tokc(VT_LLONG);
3718 next();
3719 break;
3720 case TOK_CULLONG:
3721 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3722 next();
3723 break;
3724 case TOK_CFLOAT:
3725 vpush_tokc(VT_FLOAT);
3726 next();
3727 break;
3728 case TOK_CDOUBLE:
3729 vpush_tokc(VT_DOUBLE);
3730 next();
3731 break;
3732 case TOK_CLDOUBLE:
3733 vpush_tokc(VT_LDOUBLE);
3734 next();
3735 break;
3736 case TOK___FUNCTION__:
3737 if (!gnu_ext)
3738 goto tok_identifier;
3739 /* fall thru */
3740 case TOK___FUNC__:
3741 {
3742 void *ptr;
3743 int len;
3744 /* special function name identifier */