History

tccgen: void values, etc...

originally, this was meant to avoid 'void' values leak to the gen-xxx.c generators which would otherwise try to load void from/to registers. Also catch invalid types such as 'unsigned bool' eariler. tccgen.c: - expr_cond()/gexpr(): ignore VT_VOID - gen_cast(): set float reg with static initializers to make backends happier with invalid input - init_putv(): improve static compound literal initializers Also: - ignore "missing prototype" and "might return no value" for old style K&R functions tcc.c, libtcc.c: - tcc_set_output_type(): return errors from loading crt1.o tcc.h/tccpp.c: - fix horrible longstanding mistake with sizeof SValue.tab. Must have place for sizeof (long double) rather than the target's LDOUBLE_SIZE. tccdefs.h: - add a fake __[u]int128_t for all platforms arm-gen.c: - gen_cvt_ftof() must load to reg always arm64-gen.c: - simplify arm64_pcs() a bit i386/x86_64-gen.c: - do not assume char is signed (for cross compilation arm->x86) tccelf/macho linker message: - instead of "undefined symbol 'X'" now say "unresolved reference to 'X'" which makes it more clear that it is a _linker_error_. tccasm.c: - .align/p2align: use 'nop's in exec sections

grischka committed 10 months ago

Commit 7e01b201 parent 016087c15 files · +161 −151Browse files

Unified · Split

15 files
  1. arm-gen.c +2 −1
  2. arm64-gen.c +32 −39
  3. i386-gen.c +5 −5
  4. include/tccdefs.h +6 −8
  5. libtcc.c +3 −3
  6. tcc.c +10 −6
  7. tcc.h +6 −3
  8. tccasm.c +11 −6
  9. tccelf.c +5 −3
  10. tccgen.c +43 −34
  11. tccmacho.c +3 −3
  12. tccpp.c +4 −26
  13. tests/tcctest.c +22 −2
  14. tests/tests2/60_errors_and_warnings.c +1 −1
  15. x86_64-gen.c +8 −11
arm-gen.c+2−1
@@ -2283,8 +2283,9 @@ void gen_cvt_ftoi(int t) · 2282 unmodified lines
22832283 void gen_cvt_ftof(int t)
22842284 {
22852285 #ifdef TCC_ARM_VFP
2286+ uint32_t r = gv(RC_FLOAT);
22862287 if(((vtop->type.t & VT_BTYPE) == VT_FLOAT) != ((t & VT_BTYPE) == VT_FLOAT)) {
2287− uint32_t r = vfpr(gv(RC_FLOAT));
2288+ r = vfpr(r);
22882289 o(0xEEB70AC0|(r<<12)|r|T2CPR(vtop->type.t));
22892290 }
22902291 #else
arm64-gen.c+32−39
@@ -278,7 +278,6 @@ static int arm64_type_size(int t) · 277 unmodified lines
278278 case VT_DOUBLE: return 3;
279279 case VT_LDOUBLE: return 4;
280280 case VT_BOOL: return 0;
281− case VT_VOID: return 0;
282281 }
283282 assert(0);
284283 return 0;
@@ -832,11 +831,10 @@ static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned l · 547 unmodified lines
832831
833832 for (i = 0; i < n; i++) {
834833 int hfa = arm64_hfa(type[i], 0);
835− int win_vararg_float = 0;
836− int size, align;
834+ int size, align, bt;
837835
838− if ((type[i]->t & VT_ARRAY) ||
839− (type[i]->t & VT_BTYPE) == VT_FUNC)
836+ bt = type[i]->t & VT_BTYPE;
837+ if (bt == VT_PTR || bt == VT_FUNC)
840838 size = align = 8;
841839 else
842840 size = type_size(type[i], &align);
@@ -848,13 +846,10 @@ static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned l · 5 unmodified lines
848846 }
849847
850848 #elif defined(TCC_TARGET_PE)
851− if (variadic && i >= variadic && (hfa || is_float(type[i]->t))) {
849+ if (variadic && i >= variadic) {
852850 hfa = 0;
853− if (is_float(type[i]->t)) {
854− win_vararg_float = 1;
855− size = 8;
856− align = 8;
857− }
851+ if (is_float(bt))
852+ bt = VT_INT, size = align = 8;
858853 }
859854 #endif
860855 if (hfa)
@@ -871,12 +866,12 @@ static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned l · 10 unmodified lines
871866 }
872867 continue;
873868 }
874− else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
869+ else if (bt == VT_STRUCT)
875870 // B.4
876871 size = (size + 7) & ~7;
877872
878873 // C.1
879− if (!win_vararg_float && is_float(type[i]->t) && nv < 8) {
874+ if (is_float(bt) && nv < 8) {
880875 a[i] = 16 + (nv++ << 1);
881876 continue;
882877 }
@@ -895,24 +890,24 @@ static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned l · 12 unmodified lines
895890 }
896891
897892 // C.4
898− if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
893+ if (hfa || bt == VT_LDOUBLE) {
899894 ns = (ns + 7) & ~7;
900895 ns = (ns + align - 1) & -align;
901896 }
902897
903898 // C.5
904− if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
899+ if (bt == VT_FLOAT)
905900 size = 8;
906901
907902 // C.6
908− if (!win_vararg_float && (hfa || is_float(type[i]->t))) {
903+ if (hfa || is_float(bt)) {
909904 a[i] = ns;
910905 ns += size;
911906 continue;
912907 }
913908
914909 // C.7
915− if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
910+ if (bt != VT_STRUCT && size <= 8 && nx < 8) {
916911 a[i] = nx++ << 1;
917912 continue;
918913 }
@@ -922,14 +917,14 @@ static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned l · 3 unmodified lines
922917 nx = (nx + 1) & ~1;
923918
924919 // C.9
925− if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
920+ if (bt != VT_STRUCT && size == 16 && nx < 7) {
926921 a[i] = nx << 1;
927922 nx += 2;
928923 continue;
929924 }
930925
931926 // C.10
932− if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
927+ if (bt == VT_STRUCT && size <= (8 - nx) * 8) {
933928 a[i] = nx << 1;
934929 nx += (size + 7) >> 3;
935930 continue;
@@ -943,7 +938,7 @@ static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned l · 7 unmodified lines
943938 ns = (ns + align - 1) & -align;
944939
945940 // C.13
946− if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
941+ if (bt == VT_STRUCT) {
947942 a[i] = ns;
948943 ns += size;
949944 continue;
@@ -974,11 +969,11 @@ static unsigned long arm64_pcs(int variadic, int n, CType **type, unsigned long · 24 unmodified lines
974969 }
975970
976971 // Argument types:
977− stack = arm64_pcs_aux(variadic, n, type + 1, a + 1);
972+ stack = arm64_pcs_aux(variadic, n - 1, type + 1, a + 1);
978973
979974 if (0) {
980975 int i;
981− for (i = 0; i <= n; i++) {
976+ for (i = 0; i < n; i++) {
982977 if (!i)
983978 printf("arm64_pcs return: ");
984979 else
@@ -1076,9 +1071,9 @@ ST_FUNC void gfunc_call(int nb_args) · 91 unmodified lines
10761071
10771072 stack = arm64_pcs(
10781073 #ifdef TCC_TARGET_PE
1079− old_style ? -1 :
1074+ old_style ? /* all args like varargs in K&R style */ -1 :
10801075 #endif
1081− var_nb_arg, nb_args, t, a);
1076+ var_nb_arg, nb_args + 1, t, a);
10821077
10831078 // Allocate space for structs replaced by pointer:
10841079 for (i = nb_args; i; i--)
@@ -1258,7 +1253,6 @@ ST_FUNC void gfunc_prolog(Sym *func_sym) · 173 unmodified lines
12581253 CType *func_type = &func_sym->type;
12591254 int n = 0;
12601255 int i = 0;
1261− int pcs_n;
12621256 Sym *sym;
12631257 CType **t;
12641258 unsigned long *a;
@@ -1266,35 +1260,32 @@ ST_FUNC void gfunc_prolog(Sym *func_sym) · 1 unmodified line
12661260 int last_int = 0;
12671261 int last_float = 0;
12681262 int variadic = func_sym->type.ref->f.func_type == FUNC_ELLIPSIS;
1269− int var_nb_arg = n_func_args(&func_sym->type);
1270− int c;
1263+ int var_nb_arg = variadic ? n_func_args(&func_sym->type) : 0;
12711264
12721265 func_vc = 144; // offset of where x8 is stored
12731266
12741267 for (sym = func_type->ref; sym; sym = sym->next)
12751268 ++n;
12761269
1277− pcs_n = n - 1;
1278− c = n + variadic;
1279− t = tcc_malloc(c * sizeof(*t));
1280− a = tcc_malloc(c * sizeof(*a));
1270+#ifdef TCC_TARGET_PE
1271+ n += variadic;
1272+#endif
12811273
1274+ t = tcc_malloc(n * sizeof(*t));
1275+ a = tcc_malloc(n * sizeof(*a));
12821276 for (sym = func_type->ref; sym; sym = sym->next)
12831277 t[i++] = &sym->type;
12841278
12851279 #ifdef TCC_TARGET_PE
1286− if (variadic) {
1287− t[i++] = &int_type;
1288− ++pcs_n;
1289− }
1280+ if (variadic)
1281+ t[i] = &int_type;
12901282 #endif
12911283
1292− arm64_func_va_list_stack = arm64_pcs(variadic ? var_nb_arg : 0,
1293− pcs_n, t, a);
1284+ arm64_func_va_list_stack = arm64_pcs(var_nb_arg, n, t, a);
12941285
12951286 #ifdef TCC_TARGET_PE
12961287 if (variadic)
1297− arm64_func_va_list_stack = arm64_pe_param_off(a[n]);
1288+ arm64_func_va_list_stack = arm64_pe_param_off(a[n - 1]);
12981289 #endif
12991290
13001291 #if !defined(TCC_TARGET_MACHO)
@@ -1304,6 +1295,7 @@ ST_FUNC void gfunc_prolog(Sym *func_sym) · 3 unmodified lines
13041295 last_float = 4;
13051296 }
13061297 #endif
1298+
13071299 if (a && a[0] == 1)
13081300 use_x8 = 1;
13091301 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
@@ -1563,7 +1555,8 @@ ST_FUNC void gfunc_return(CType *func_type) · 253 unmodified lines
15631555 CType *t = func_type;
15641556 unsigned long a;
15651557
1566− arm64_pcs(0, 0, &t, &a);
1558+ arm64_pcs(0, 1, &t, &a);
1559+
15671560 switch (a) {
15681561 case -1:
15691562 break;
i386-gen.c+5−5
@@ -271,7 +271,7 @@ static void gen_modrm(int opc, int op_r2, int r, Sym *sym, int c) · 270 unmodified lines
271271 } else if ((r & VT_VALMASK) == VT_LOCAL) {
272272 o(opc);
273273 /* currently, we use only ebp as base */
274− if (c == (char)c) {
274+ if (c == (signed char)c) {
275275 /* short reference */
276276 o(0x45 | op_reg);
277277 g(c);
@@ -439,7 +439,7 @@ ST_FUNC void store(int r, SValue *v) · 161 unmodified lines
439439
440440 static void gadd_sp(int val)
441441 {
442− if (val == (char)val) {
442+ if (val == (signed char)val) {
443443 o(0xc483);
444444 g(val);
445445 } else {
@@ -774,7 +774,7 @@ ST_FUNC void gjmp_addr(int a) · 328 unmodified lines
774774 {
775775 int r;
776776 r = a - ind - 2;
777− if (r == (char)r) {
777+ if (r == (signed char)r) {
778778 g(0xeb);
779779 g(r);
780780 } else {
@@ -787,7 +787,7 @@ ST_FUNC void gjmp_addr(int a) · 6 unmodified lines
787787 ST_FUNC void gjmp_cond_addr(int a, int op)
788788 {
789789 int r = a - ind - 2;
790− if (r == (char)r)
790+ if (r == (signed char)r)
791791 g(op - 32), g(r);
792792 else
793793 g(0x0f), gjmp2(op - 16, r - 4);
@@ -830,7 +830,7 @@ ST_FUNC void gen_opi(int op) · 36 unmodified lines
830830 r = gv(RC_INT);
831831 vswap();
832832 c = vtop->c.i;
833− if (c == (char)c) {
833+ if (c == (signed char)c) {
834834 /* generate inc and dec for smaller code */
835835 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
836836 opc = (c == 1) ^ (op == '+');
include/tccdefs.h+6−8
@@ -90,8 +90,6 @@ · 89 unmodified lines
9090 #define __NO_TLS 1
9191 #define __RUNETYPE_INTERNAL 1
9292 # if __SIZEOF_POINTER__ == 8
93− /* FIXME, __int128_t is used by setjump */
94− #define __int128_t struct { unsigned char _dummy[16] __attribute((aligned(16))); }
9593 #define __SIZEOF_SIZE_T__ 8
9694 #define __SIZEOF_PTRDIFF_T__ 8
9795 #else
@@ -142,12 +140,6 @@ · 44 unmodified lines
142140 #endif
143141 #define __INT32_TYPE__ int
144142
145−#if defined __aarch64__
146− /* GCC's __uint128_t appears in some Linux/OSX header files. Make it a
147− synonym for long double to get the size and alignment right. */
148− #define __uint128_t long double
149−#endif
150−
151143 #if !defined _WIN32
152144 /* glibc defines. We do not support __USER_NAME_PREFIX__ */
153145 #define __REDIRECT(name, proto, alias) name proto __asm__ (#alias)
@@ -187,6 +179,12 @@ · 33 unmodified lines
187179 # endif
188180 #endif
189181
182+ /* GCC's __uint128_t appears in some Linux/OSX header files.
183+ Just make it some type with same size and alignment. */
184+ struct __uint128__ { char x[16]; } __attribute((__aligned__(16)));
185+ #define __int128_t struct __uint128__
186+ #define __uint128_t struct __uint128__
187+
190188 /* __builtin_va_list */
191189 #if defined __x86_64__
192190 #if !defined _WIN32
libtcc.c+3−3
@@ -252,7 +252,7 @@ static void *default_reallocator(void *ptr, unsigned long size) · 251 unmodified lines
252252 else {
253253 ptr1 = realloc(ptr, size);
254254 if (!ptr1) {
255− fprintf(stderr, "memory full\n");
255+ fprintf(stderr, "tcc: memory full\n");
256256 exit (1);
257257 }
258258 }
@@ -1003,9 +1003,9 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) · 744 unmodified lines
10031003 /* paths for crt objects */
10041004 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
10051005 if (output_type != TCC_OUTPUT_MEMORY && !s->nostdlib)
1006− tccelf_add_crtbegin(s);
1006+ tccelf_add_crtbegin(s); /* may produce errors */
10071007 #endif
1008− return 0;
1008+ return s->nb_errors ? -1 : 0;
10091009 }
10101010
10111011 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
tcc.c+10−6
@@ -35,7 +35,7 @@ static const char help[] = · 34 unmodified lines
3535 "General options:\n"
3636 " -c compile only - generate an object file\n"
3737 " -o outfile set output filename\n"
38− " -run run compiled source [with custom stdin: -rstdin FILE]\n"
38+ " -run run compiled source\n"
3939 " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
4040 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
4141 " -w disable all warnings\n"
@@ -105,6 +105,7 @@ static const char help2[] = · 63 unmodified lines
105105 " -static link to static libraries (not recommended)\n"
106106 " -dumpversion print version\n"
107107 " -print-search-dirs print search paths\n"
108+ " -rstdin file with -run: use 'file' as custom stdin\n"
108109 " -dt with -run/-E: auto-define 'test_...' macros\n"
109110 "Ignored options:\n"
110111 " -arch -C --param -pedantic -pipe -s -traditional\n"
@@ -354,9 +355,10 @@ redo: · 243 unmodified lines
354355 set_environment(s);
355356 if (s->output_type == 0)
356357 s->output_type = TCC_OUTPUT_EXE;
357− tcc_set_output_type(s, s->output_type);
358+ ret = tcc_set_output_type(s, s->output_type);
358359 if (ppfp)
359360 s->ppfp = ppfp;
361+
360362 if ((s->output_type == TCC_OUTPUT_MEMORY
361363 || s->output_type == TCC_OUTPUT_PREPROCESS)
362364 && (s->dflag & 16)) { /* -dt option */
@@ -369,7 +371,7 @@ redo: · 6 unmodified lines
369371
370372 /* compile or add each files or library */
371373 first_file = NULL;
372− do {
374+ while (0 == ret) {
373375 struct filespec *f = s->files[n];
374376 s->filetype = f->type;
375377 if (f->type & AFF_TYPE_LIB) {
@@ -381,9 +383,11 @@ redo: · 5 unmodified lines
381383 first_file = f->name;
382384 ret = tcc_add_file(s, f->name);
383385 }
384− } while (++n < s->nb_files
385− && 0 == ret
386− && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
386+ if (++n == s->nb_files)
387+ break;
388+ if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r)
389+ break;
390+ }
387391
388392 if (s->do_bench)
389393 end_time = getclock_ms();
tcc.h+6−3
@@ -475,6 +475,9 @@ typedef struct CType { · 474 unmodified lines
475475 struct Sym *ref;
476476 } CType;
477477
478+/* long double words on host(!) platform */
479+#define LDOUBLE_WORDS ((sizeof(long double)+3)/4)
480+
478481 /* constant value */
479482 typedef union CValue {
480483 long double ld;
@@ -485,7 +488,7 @@ typedef union CValue { · 4 unmodified lines
485488 char *data;
486489 int size;
487490 } str;
488− int tab[LDOUBLE_SIZE/4];
491+ int tab[LDOUBLE_WORDS];
489492 } CValue;
490493
491494 /* value on stack */
@@ -1446,8 +1449,8 @@ ST_DATA int nocode_wanted; /* true if no code generation wanted for an expressio · 954 unmodified lines
14461449 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
14471450 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
14481451 ST_DATA int func_var; /* true if current function is variadic */
1449−ST_DATA int func_vc;
1450−ST_DATA int func_ind;
1452+ST_DATA int func_vc; /* stack address for implicit struct return storage */
1453+ST_DATA int func_ind; /* function start address */
14511454 ST_DATA const char *funcname;
14521455
14531456 ST_FUNC void tccgen_init(TCCState *s1);
tccasm.c+11−6
@@ -510,7 +510,7 @@ static void pop_section(TCCState *s1) · 509 unmodified lines
510510
511511 static void asm_parse_directive(TCCState *s1, int global)
512512 {
513− int n, offset, v, size, tok1;
513+ int n, offset, v, size, tok1, c;
514514 Section *sec;
515515 uint8_t *ptr;
516516
@@ -540,18 +540,25 @@ static void asm_parse_directive(TCCState *s1, int global) · 23 unmodified lines
540540 /* the section must have a compatible alignment */
541541 if (sec->sh_addralign < n)
542542 sec->sh_addralign = n;
543+ c = sec->sh_flags & SHF_EXECINSTR;
543544 } else {
544545 if (n < 0)
545546 n = 0;
546− size = n;
547+ size = n, c = 0;
547548 }
548549 v = 0;
549550 if (tok == ',') {
550551 next();
551− v = asm_int_expr(s1);
552+ v = asm_int_expr(s1), c = 0;
552553 }
553554 zero_pad:
555+ if ((uint64_t)ind + size >= 1<<30)
556+ tcc_error("too much data");
554557 if (sec->sh_type != SHT_NOBITS) {
558+ if (c) {
559+ gen_fill_nops(size);
560+ break;
561+ }
555562 sec->data_offset = ind;
556563 ptr = section_ptr_add(sec, size);
557564 memset(ptr, v, size);
@@ -706,11 +713,9 @@ static void asm_parse_directive(TCCState *s1, int global) · 148 unmodified lines
706713 expect("constant or same-section symbol");
707714 n += esym->st_value;
708715 }
709− if (n < 0 || n > 0x100000)
710− tcc_error(".org out of range");
711716 if (n < ind)
712717 tcc_error("attempt to .org backwards");
713− v = 0;
718+ v = c = 0;
714719 size = n - ind;
715720 goto zero_pad;
716721 }
tccelf.c+5−3
@@ -1106,7 +1106,7 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve) · 1105 unmodified lines
11061106 if (sym_bind == STB_WEAK)
11071107 sym->st_value = 0;
11081108 else
1109− tcc_error_noabort("undefined symbol '%s'", name);
1109+ tcc_error_noabort("unresolved reference to '%s'", name);
11101110
11111111 } else if (sh_num < SHN_LORESERVE) {
11121112 /* add section base */
@@ -2074,7 +2074,7 @@ static void bind_exe_dynsyms(TCCState *s1, int is_PIE) · 961 unmodified lines
20742074 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
20752075 !strcmp(name, "_fp_hw")) {
20762076 } else {
2077− tcc_error_noabort("undefined symbol '%s'", name);
2077+ tcc_error_noabort("unresolved reference to '%s'", name);
20782078 }
20792079 }
20802080 }
@@ -2105,7 +2105,7 @@ static void bind_libs_dynsyms(TCCState *s1) · 24 unmodified lines
21052105 if (esym->st_shndx == SHN_UNDEF) {
21062106 /* weak symbols can stay undefined */
21072107 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
2108− tcc_warning("undefined dynamic symbol '%s'", name);
2108+ tcc_warning("unresolved dynamic reference to '%s'", name);
21092109 }
21102110 }
21112111 }
@@ -3301,6 +3301,8 @@ invalid: · 1189 unmodified lines
33013301 continue;
33023302 if (sh->sh_type != s->sh_type
33033303 && strcmp (s->name, ".eh_frame")
3304+ /* some crt1.o seem to have two ".note.GNU-stack" (SHT_NOTE & SHT_PROGBITS) */
3305+ && strcmp (s->name, ".note.GNU-stack")
33043306 ) {
33053307 tcc_error_noabort("section type conflict: %s %02x <> %02x", s->name, sh->sh_type, s->sh_type);
33063308 goto the_end;
tccgen.c+43−34
@@ -70,8 +70,9 @@ ST_DATA int nocode_wanted; /* no code generation wanted */ · 69 unmodified lines
7070 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
7171 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
7272 ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
73−ST_DATA int func_vc;
74−ST_DATA int func_ind;
73+ST_DATA int func_vc; /* stack address for implicit struct return storage */
74+ST_DATA int func_ind; /* function start address */
75+static int func_old;
7576 ST_DATA const char *funcname;
7677 ST_DATA CType int_type, func_old_type, char_type, char_pointer_type;
7778 static CString initstr;
@@ -1385,7 +1386,7 @@ static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad) · 1307 unmodified lines
13851386 /* make sure that type->ref is on global stack */
13861387 move_ref_to_global(s);
13871388 /* put into local scope */
1388− s = sym_copy(s, &local_stack);
1389+ sym_copy(s, &local_stack);
13891390 }
13901391 return s;
13911392 }
@@ -1906,6 +1907,8 @@ ST_FUNC int gv(int rc) · 514 unmodified lines
19061907 #endif
19071908
19081909 bt = vtop->type.t & VT_BTYPE;
1910+ if (bt == VT_VOID || bt == VT_STRUCT) /* should not happen */
1911+ return vtop->r;
19091912
19101913 #ifdef TCC_TARGET_RISCV64
19111914 /* XXX mega hack */
@@ -2929,7 +2932,8 @@ static int combine_types(CType *dest, SValue *op1, SValue *op2, int op) · 1017 unmodified lines
29292932 type.ref = NULL;
29302933
29312934 if (bt1 == VT_VOID || bt2 == VT_VOID) {
2932− ret = op == '?' ? 1 : 0;
2935+ if (op != '?')
2936+ tcc_error("operation on void value");
29332937 /* NOTE: as an extension, we accept void on only one side */
29342938 type.t = VT_VOID;
29352939 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
@@ -3176,7 +3180,7 @@ op_err: · 240 unmodified lines
31763180 }
31773181 // Make sure that we have converted to an rvalue:
31783182 if (vtop->r & VT_LVAL)
3179− gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
3183+ gv(RC_TYPE(vtop->type.t));
31803184 }
31813185
31823186 #if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64 || defined TCC_TARGET_ARM
@@ -3281,8 +3285,10 @@ again: · 98 unmodified lines
32813285 df = is_float(dbt);
32823286 dbt_bt = dbt & VT_BTYPE;
32833287 sbt_bt = sbt & VT_BTYPE;
3284− if (dbt_bt == VT_VOID)
3288+ if (dbt_bt == VT_VOID) {
3289+ vtop->r = vtop->r2 = VT_CONST;
32853290 goto done;
3291+ }
32863292 if (sbt_bt == VT_VOID) {
32873293 error:
32883294 cast_error(&vtop->type, type);
@@ -3361,8 +3367,11 @@ error: · 72 unmodified lines
33613367 }
33623368
33633369 /* cannot generate code for global or static initializers */
3364− if (nocode_wanted & DATA_ONLY_WANTED)
3370+ if (nocode_wanted & DATA_ONLY_WANTED) {
3371+ if (df)
3372+ vtop->r = get_reg(RC_FLOAT); /* don't confuse backends */
33653373 goto done;
3374+ }
33663375
33673376 /* non constant case: generate code */
33683377 if (dbt == VT_BOOL) {
@@ -4753,6 +4762,8 @@ static int parse_btype(CType *type, AttributeDef *ad, int ignore_label) · 1384 unmodified lines
47534762 } else {
47544763 if (bt != -1 || (st != -1 && u != VT_INT))
47554764 goto tmbt;
4765+ if ((t & VT_DEFSIGN) && (u == VT_VOID || u > VT_LLONG))
4766+ goto tmbt;
47564767 bt = u;
47574768 }
47584769 if (u != VT_INT)
@@ -6112,6 +6123,7 @@ special_math_val: · 1353 unmodified lines
61126123 tcc_error("'%s' undeclared", name);
61136124 /* for simple function calls, we tolerate undeclared
61146125 external reference to int() function */
6126+ if (!func_old)
61156127 tcc_warning_c(warn_implicit_function_declaration)(
61166128 "implicit declaration of function '%s'", name);
61176129 s = external_global_sym(t, &func_old_type);
@@ -6660,7 +6672,7 @@ static void expr_cond(void) · 542 unmodified lines
66606672
66616673 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
66626674 that `(expr ? a : b).mem` does not error with "lvalue expected" */
6663− islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);
6675+ islv = VT_STRUCT == (type.t & VT_BTYPE);
66646676
66656677 /* now we convert second operand */
66666678 if (c != 1) {
@@ -6668,8 +6680,7 @@ static void expr_cond(void) · 1 unmodified line
66686680 if (islv) {
66696681 mk_pointer(&vtop->type);
66706682 gaddrof();
6671− } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
6672− gaddrof();
6683+ }
66736684 }
66746685
66756686 rc = RC_TYPE(type.t);
@@ -6680,7 +6691,8 @@ static void expr_cond(void) · 4 unmodified lines
66806691
66816692 tt = r2 = 0;
66826693 if (c < 0) {
6683− r2 = gv(rc);
6694+ if (type.t != VT_VOID)
6695+ r2 = gv(rc);
66846696 tt = gjmp(0);
66856697 }
66866698 gsym(u);
@@ -6695,14 +6707,15 @@ static void expr_cond(void) · 8 unmodified lines
66956707 if (islv) {
66966708 mk_pointer(&vtop->type);
66976709 gaddrof();
6698− } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
6699− gaddrof();
6710+ }
67006711 }
67016712
67026713 if (c < 0) {
6703− r1 = gv(rc);
6704− move_reg(r2, r1, islv ? VT_PTR : type.t);
6705− vtop->r = r2;
6714+ if (type.t != VT_VOID) {
6715+ r1 = gv(rc);
6716+ move_reg(r2, r1, islv ? VT_PTR : type.t);
6717+ vtop->r = r2;
6718+ }
67066719 gsym(tt);
67076720 }
67086721
@@ -6745,7 +6758,8 @@ ST_FUNC void gexpr(void) · 36 unmodified lines
67456758
67466759 /* make builtin_constant_p((1,2)) return 0 (like on gcc) */
67476760 if ((vtop->r & VT_VALMASK) == VT_CONST && nocode_wanted && !CONST_WANTED)
6748− gv(RC_TYPE(vtop->type.t));
6761+ if (vtop->type.t != VT_VOID && (vtop->type.t & VT_BTYPE) != VT_STRUCT)
6762+ gv(RC_TYPE(vtop->type.t));
67496763 }
67506764 }
67516765
@@ -6855,11 +6869,10 @@ static void check_func_return(void) · 103 unmodified lines
68556869 {
68566870 if ((func_vt.t & VT_BTYPE) == VT_VOID)
68576871 return;
6858− if (!strcmp (funcname, "main")
6872+ if ((!strcmp(funcname, "main") || func_old)
68596873 && (func_vt.t & VT_BTYPE) == VT_INT) {
68606874 /* main returns 0 by default */
68616875 vpushi(0);
6862− gen_assign_cast(&func_vt);
68636876 gfunc_return(&func_vt);
68646877 } else {
68656878 tcc_warning("function might return no value: '%s'", funcname);
@@ -7242,6 +7255,8 @@ again: · 376 unmodified lines
72427255 tcc_warning("void function returns a value");
72437256 vtop--;
72447257 }
7258+ } else if (b && func_old && (func_vt.t & VT_BTYPE) == VT_INT) {
7259+ vpushi(0);
72457260 } else if (b) {
72467261 tcc_warning("'return' with no value");
72477262 b = 0;
@@ -7806,21 +7821,12 @@ static void init_putv(init_params *p, CType *type, unsigned long c) · 558 unmodified lines
78067821 ptr = sec->data + c;
78077822 val = vtop->c.i;
78087823
7809− /* XXX: make code faster ? */
7810− if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) &&
7811− vtop->sym->v >= SYM_FIRST_ANOM &&
7812− /* XXX This rejects compound literals like
7813− '(void *){ptr}'. The problem is that '&sym' is
7814− represented the same way, which would be ruled out
7815− by the SYM_FIRST_ANOM check above, but also '"string"'
7816− in 'char *p = "string"' is represented the same
7817− with the type being VT_PTR and the symbol being an
7818− anonymous one. That is, there's no difference in vtop
7819− between '(void *){x}' and '&(void *){x}'. Ignore
7820− pointer typed entities here. Hopefully no real code
7821− will ever use compound literals with scalar type. */
7822− (vtop->type.t & VT_BTYPE) != VT_PTR) {
7823− /* These come from compound literals, memcpy stuff over. */
7824+ if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST)
7825+ && vtop->sym->v >= SYM_FIRST_ANOM
7826+ && ((vtop->r & VT_LVAL) /* compound literal */
7827+ || bt == VT_STRUCT /* designator */
7828+ )) {
7829+ /* memcpy stuff over. */
78247830 Section *ssec;
78257831 ElfSym *esym;
78267832 ElfW_Rel *rel;
@@ -8072,6 +8078,8 @@ static void decl_initializer(init_params *p, CType *type, unsigned long c, int f · 245 unmodified lines
80728078 }
80738079 }
80748080 }
8081+ if (tok == ',' && !no_oblock) /* static const char s[] = { "123", }; */
8082+ next();
80758083 } else {
80768084
80778085 do_init_array:
@@ -8528,6 +8536,7 @@ static void gen_function(Sym *sym) · 450 unmodified lines
85288536 func_ind = ind;
85298537 func_vt = sym->type.ref->type;
85308538 func_var = sym->type.ref->f.func_type == FUNC_ELLIPSIS;
8539+ func_old = sym->type.ref->f.func_type == FUNC_OLD;
85318540
85328541 /* NOTE: we patch the symbol size later */
85338542 put_extern_sym(sym, cur_text_section, ind, 0);
tccmacho.c+3−3
@@ -702,7 +702,7 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 701 unmodified lines
702702 goti = tcc_realloc(goti, (mo->n_got + 1) * sizeof(*goti));
703703 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
704704 if (sym->st_shndx == SHN_UNDEF)
705− tcc_error("undefined local symbo: '%s'",
705+ tcc_error("unresolved local reference to '%s'",
706706 (char *) symtab_section->link->data + sym->st_name);
707707 goti[mo->n_got++] = INDIRECT_SYMBOL_LOCAL;
708708 } else {
@@ -859,7 +859,7 @@ static void check_relocs(TCCState *s1, struct macho *mo) · 150 unmodified lines
859859 goti = tcc_realloc(goti, (mo->n_got + 1) * sizeof(*goti));
860860 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
861861 if (sym->st_shndx == SHN_UNDEF)
862− tcc_error("undefined local symbo: '%s'",
862+ tcc_error("unresolved local reference to '%s'",
863863 (char *) symtab_section->link->data + sym->st_name);
864864 goti[mo->n_got++] = INDIRECT_SYMBOL_LOCAL;
865865 } else {
@@ -1042,7 +1042,7 @@ static int check_symbols(TCCState *s1, struct macho *mo) · 176 unmodified lines
10421042 sym->st_shndx = SHN_FROMDLL;
10431043 continue;
10441044 }
1045− tcc_error_noabort("undefined symbol '%s'", name);
1045+ tcc_error_noabort("unresolved reference to '%s'", name);
10461046 ret = -1;
10471047 }
10481048 }
tccpp.c+4−26
@@ -987,11 +987,7 @@ static inline int tok_size(const int *p) · 986 unmodified lines
987987 case TOK_CULLONG:
988988 return 1 + 2;
989989 case TOK_CLDOUBLE:
990−#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
991− return 1 + 8 / 4;
992−#else
993− return 1 + LDOUBLE_SIZE / 4;
994−#endif
990+ return 1 + LDOUBLE_WORDS;
995991 default:
996992 return 1 + 0;
997993 }
@@ -1130,22 +1126,12 @@ static void tok_str_add2(TokenString *s, int t, CValue *cv) · 132 unmodified lines
11301126 str[len++] = cv->tab[1];
11311127 break;
11321128 case TOK_CLDOUBLE:
1133−#if LDOUBLE_SIZE == 8 || defined TCC_USING_DOUBLE_FOR_LDOUBLE
1134− str[len++] = cv->tab[0];
1135− str[len++] = cv->tab[1];
1136−#elif LDOUBLE_SIZE == 12
1137− str[len++] = cv->tab[0];
1138− str[len++] = cv->tab[1];
1139− str[len++] = cv->tab[2];
1140−#elif LDOUBLE_SIZE == 16
11411129 str[len++] = cv->tab[0];
11421130 str[len++] = cv->tab[1];
1131+ if (LDOUBLE_WORDS >= 3)
11431132 str[len++] = cv->tab[2];
1133+ if (LDOUBLE_WORDS >= 4)
11441134 str[len++] = cv->tab[3];
1145−#else
1146−#error add long double size support
1147−#endif
1148− break;
11491135 default:
11501136 break;
11511137 }
@@ -1219,15 +1205,7 @@ static inline void tok_get(int *t, const int **pp, CValue *cv) · 67 unmodified lines
12191205 n = 2;
12201206 goto copy;
12211207 case TOK_CLDOUBLE:
1222−#if LDOUBLE_SIZE == 8 || defined TCC_USING_DOUBLE_FOR_LDOUBLE
1223− n = 2;
1224−#elif LDOUBLE_SIZE == 12
1225− n = 3;
1226−#elif LDOUBLE_SIZE == 16
1227− n = 4;
1228−#else
1229−# error add long double size support
1230−#endif
1208+ n = LDOUBLE_WORDS;
12311209 copy:
12321210 do
12331211 *tab++ = *p++;
tests/tcctest.c+22−2
@@ -1465,17 +1465,37 @@ static int tab_reinit[10]; · 1464 unmodified lines
14651465 static int tentative_ar[];
14661466 static int tentative_ar[] = {1,2,3};
14671467
1468−//int cinit1; /* a global variable can be defined several times without error ! */
1469−int cinit1;
1468+int cinit1; /* a global variable can be defined several times without error ! */
14701469 int cinit1;
14711470 int cinit1 = 0;
14721471 int *cinit2 = (int []){3, 2, 1};
1472+uintptr_t cinit3 = (uintptr_t)"AA";
1473+char const * const cinit8[] = { [0 ... 1] = "BB", [2 ... 4] = "CC" };
1474+void *cinit52 = &(void*){ (void*) 52 };
1475+
1476+#if __TINYC__ || __GNUC__ >= 6
1477+int cinit4 = (int){44};
1478+void *cinit51 = (void*){ (void*) 51 };
1479+struct _c6 { int a,b; } cinit6 = (struct _c6){61,62}, *cinit7 = &(struct _c6){71,72};
1480+#else
1481+int cinit4 = 44;
1482+void *cinit51 = (void*)51;
1483+struct _c6 { int a,b; } cinit6 = { 61,62 }, cinit70 = {71,72}, *cinit7 = &cinit70;
1484+#endif
14731485
14741486 void compound_literal_test(void)
14751487 {
14761488 int *p, i;
14771489 char *q, *q3;
14781490
1491+ printf("cinit3 : %s\n", cinit3);
1492+ printf("cinit4 : %d\n", cinit4);
1493+ printf("cinit51 : %d\n", (int)cinit51);
1494+ printf("cinit52 : %d\n", *(int*)cinit52);
1495+ printf("cinit6 : %d %d\n", cinit6.a, cinit6.b);
1496+ printf("cinit7 : %d %d\n", cinit7->a, cinit7->b);
1497+ printf("cinit8 : %s %s %s %s %s\n", cinit8[0], cinit8[1], cinit8[2], cinit8[3], cinit8[4]);
1498+
14791499 p = (int []){1, 2, 3};
14801500 for(i=0;i<3;i++)
14811501 printf(" %d", p[i]);
tests/tests2/60_errors_and_warnings.c+1−1
@@ -421,7 +421,7 @@ int f() { return v(); } · 420 unmodified lines
421421 #elif defined test_switch_W4
422422 #pragma comment(option, "-Wunsupported -Wno-error=implicit-function-declaration -Werror")
423423 #endif
424−void func()
424+void func(void)
425425 {
426426 char *ccp = "123";
427427 fink();
x86_64-gen.c+8−11
@@ -314,7 +314,7 @@ static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got) · 313 unmodified lines
314314 }
315315 } else if ((r & VT_VALMASK) == VT_LOCAL) {
316316 /* currently, we use only ebp as base */
317− if (c == (char)c) {
317+ if (c == (signed char)c) {
318318 /* short reference */
319319 o(0x45 | op_reg);
320320 g(c);
@@ -436,8 +436,7 @@ void load(int r, SValue *sv) · 115 unmodified lines
436436 b = 0xdb, r = 5; /* fldt */
437437 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
438438 b = 0xbe0f; /* movsbl */
439− } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED) ||
440− (ft & VT_TYPE) == (VT_BOOL | VT_UNSIGNED)) {
439+ } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
441440 b = 0xb60f; /* movzbl */
442441 } else if ((ft & VT_TYPE) == VT_SHORT) {
443442 b = 0xbf0f; /* movswl */
@@ -532,8 +531,7 @@ void load(int r, SValue *sv) · 88 unmodified lines
532531 o(0x44 + REG_VALUE(r)*8); /* %xmmN */
533532 o(0xf024);
534533 } else {
535− if (!nocode_wanted)
536− assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
534+ assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
537535 if ((ft & VT_BTYPE) == VT_FLOAT) {
538536 o(0x100ff3);
539537 } else {
@@ -543,8 +541,7 @@ void load(int r, SValue *sv) · 3 unmodified lines
543541 o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8);
544542 }
545543 } else if (r == TREG_ST0) {
546− if (!nocode_wanted)
547− assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
544+ assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
548545 /* gen_cvt_ftof(VT_LDOUBLE); */
549546 /* movsd %xmmN,-0x10(%rsp) */
550547 o(0x110ff2);
@@ -749,7 +746,7 @@ static int arg_prepare_reg(int idx) { · 198 unmodified lines
749746 static void gen_offs_sp(int b, int r, int d)
750747 {
751748 orex(1,0,r & 0x100 ? 0 : r, b);
752− if (d == (char)d) {
749+ if (d == (signed char)d) {
753750 o(0x2444 | (REG_VALUE(r) << 3));
754751 g(d);
755752 } else {
@@ -1052,7 +1049,7 @@ void gfunc_epilog(void) · 296 unmodified lines
10521049
10531050 static void gadd_sp(int val)
10541051 {
1055− if (val == (char)val) {
1052+ if (val == (signed char)val) {
10561053 o(0xc48348);
10571054 g(val);
10581055 } else {
@@ -1635,7 +1632,7 @@ void gjmp_addr(int a) · 576 unmodified lines
16351632 {
16361633 int r;
16371634 r = a - ind - 2;
1638− if (r == (char)r) {
1635+ if (r == (signed char)r) {
16391636 g(0xeb);
16401637 g(r);
16411638 } else {
@@ -1704,7 +1701,7 @@ void gen_opi(int op) · 62 unmodified lines
17041701 r = gv(RC_INT);
17051702 vswap();
17061703 c = vtop->c.i;
1707− if (c == (char)c) {
1704+ if (c == (signed char)c) {
17081705 /* XXX: generate inc and dec for smaller code ? */
17091706 orex(ll, r, 0, 0x83);
17101707 o(0xc0 | (opc << 3) | REG_VALUE(r));

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

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

Repository code

Go to file
t