History

__builtin_/__bound_: A 'stay tiny' approach to the matter

tccgen.c: - cleanup __builtin_... stuff - merge __attribute((alias("sym"))) with __asm__("sym") Now one cannot have both, however for alias underscores are added if enabled. For __asm__ they aren't. tccpp.c: - extend tcc_predefs accordingly. Was generated with 'cd tests/misc && tcc -run c2str.c tcc_predef.h tcc_predefs' xxx-gen.c: - move bcheck setjmp test to tccgen.c:gbound_args() i386-gen.c: - create win32 compatible stack space for big structures tcctest.c: - some cleanup + nicer output

grischka committed 6 years ago

Commit 2a0167a1 parent 6a4f3cf16 files · +641 −719Browse files

Unified · Split

16 files
  1. arm-gen.c +1 −9
  2. arm64-gen.c +1 −9
  3. i386-asm.c +1 −1
  4. i386-gen.c +11 −13
  5. libtcc.c +7 −8
  6. riscv64-gen.c +1 −9
  7. tcc.h +1 −1
  8. tccgen.c +71 −317
  9. tccpp.c +52 −7
  10. tcctok.h +0 −55
  11. tests/misc/c2str.c +112 −0
  12. tests/misc/tcc_predefs.h +111 −0
  13. tests/tcctest.c +145 −277
  14. tests/tests2/61_integers.c +70 −0
  15. tests/tests2/61_integers.expect +56 −0
  16. x86_64-gen.c +1 −13
arm-gen.c+1−9
@@ -161,7 +161,7 @@ static int leaffunc; · 160 unmodified lines
161#if defined(CONFIG_TCC_BCHECK)161#if defined(CONFIG_TCC_BCHECK)
162static addr_t func_bound_offset;162static addr_t func_bound_offset;
163static unsigned long func_bound_ind;163static unsigned long func_bound_ind;
164static int func_bound_add_epilog;164ST_DATA int func_bound_add_epilog;
165#endif165#endif
166 166
167#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)167#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
@@ -763,14 +763,6 @@ static void gcall_or_jmp(int is_jmp) · 595 unmodified lines
763 greloc(cur_text_section, vtop->sym, ind, R_ARM_ABS32);763 greloc(cur_text_section, vtop->sym, ind, R_ARM_ABS32);
764 o(vtop->c.i);764 o(vtop->c.i);
765 }765 }
766#ifdef CONFIG_TCC_BCHECK
767 if (tcc_state->do_bounds_check &&
768 (vtop->sym->v == TOK_setjmp ||
769 vtop->sym->v == TOK__setjmp ||
770 vtop->sym->v == TOK_sigsetjmp ||
771 vtop->sym->v == TOK___sigsetjmp))
772 func_bound_add_epilog = 1;
773#endif
774 }else{766 }else{
775 if(!is_jmp)767 if(!is_jmp)
776 o(0xE28FE004); // add lr,pc,#4768 o(0xE28FE004); // add lr,pc,#4
arm64-gen.c+1−9
@@ -84,7 +84,7 @@ ST_DATA const int reg_classes[NB_REGS] = { · 83 unmodified lines
84#if defined(CONFIG_TCC_BCHECK)84#if defined(CONFIG_TCC_BCHECK)
85static addr_t func_bound_offset;85static addr_t func_bound_offset;
86static unsigned long func_bound_ind;86static unsigned long func_bound_ind;
87static int func_bound_add_epilog;87ST_DATA int func_bound_add_epilog;
88#endif88#endif
89 89
90#define IS_FREG(x) ((x) >= TREG_F(0))90#define IS_FREG(x) ((x) >= TREG_F(0))
@@ -606,14 +606,6 @@ static void arm64_gen_bl_or_b(int b) · 515 unmodified lines
606 greloca(cur_text_section, vtop->sym, ind,606 greloca(cur_text_section, vtop->sym, ind,
607 b ? R_AARCH64_JUMP26 : R_AARCH64_CALL26, 0);607 b ? R_AARCH64_JUMP26 : R_AARCH64_CALL26, 0);
608 o(0x14000000 | (uint32_t)!b << 31); // b/bl .608 o(0x14000000 | (uint32_t)!b << 31); // b/bl .
609#ifdef CONFIG_TCC_BCHECK
610 if (tcc_state->do_bounds_check &&
611 (vtop->sym->v == TOK_setjmp ||
612 vtop->sym->v == TOK__setjmp ||
613 vtop->sym->v == TOK_sigsetjmp ||
614 vtop->sym->v == TOK___sigsetjmp))
615 func_bound_add_epilog = 1;
616#endif
617 }609 }
618 else {610 else {
619#ifdef CONFIG_TCC_BCHECK611#ifdef CONFIG_TCC_BCHECK
i386-asm.c+1−1
@@ -1202,7 +1202,7 @@ ST_FUNC int asm_parse_regvar (int t) · 1201 unmodified lines
1202{1202{
1203 const char *s;1203 const char *s;
1204 Operand op;1204 Operand op;
1205 if (t < TOK_IDENT)1205 if (t < TOK_IDENT || (t & SYM_FIELD))
1206 return -1;1206 return -1;
1207 s = table_ident[t - TOK_IDENT]->str;1207 s = table_ident[t - TOK_IDENT]->str;
1208 if (s[0] != '%')1208 if (s[0] != '%')
i386-gen.c+11−13
@@ -97,7 +97,7 @@ static int func_ret_sub; · 96 unmodified lines
97#ifdef CONFIG_TCC_BCHECK97#ifdef CONFIG_TCC_BCHECK
98static addr_t func_bound_offset;98static addr_t func_bound_offset;
99static unsigned long func_bound_ind;99static unsigned long func_bound_ind;
100static int func_bound_add_epilog;100ST_DATA int func_bound_add_epilog;
101static void gen_bounds_prolog(void);101static void gen_bounds_prolog(void);
102static void gen_bounds_epilog(void);102static void gen_bounds_epilog(void);
103#endif103#endif
@@ -359,18 +359,6 @@ static void gcall_or_jmp(int is_jmp) · 255 unmodified lines
359 /* constant and relocation case */359 /* constant and relocation case */
360 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);360 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
361 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */361 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
362#ifdef CONFIG_TCC_BCHECK
363 if (tcc_state->do_bounds_check &&
364 (vtop->sym->v == TOK_alloca ||
365 vtop->sym->v == TOK_setjmp ||
366 vtop->sym->v == TOK__setjmp
367#ifndef TCC_TARGET_PE
368 || vtop->sym->v == TOK_sigsetjmp
369 || vtop->sym->v == TOK___sigsetjmp
370#endif
371 ))
372 func_bound_add_epilog = 1;
373#endif
374 } else {362 } else {
375 /* otherwise, indirect call */363 /* otherwise, indirect call */
376 r = gv(RC_INT);364 r = gv(RC_INT);
@@ -429,6 +417,16 @@ ST_FUNC void gfunc_call(int nb_args) · 52 unmodified lines
429 /* align to stack align size */417 /* align to stack align size */
430 size = (size + 3) & ~3;418 size = (size + 3) & ~3;
431 /* allocate the necessary size on stack */419 /* allocate the necessary size on stack */
420#ifdef TCC_TARGET_PE
421 if (size >= 0x4096) {
422 o(0x5250);
423 oad(0x68, size);
424 vpush_global_sym(&func_old_type, TOK_alloca);
425 gcall_or_jmp(0);
426 vtop--;
427 o(0x58585a);
428 } else
429#endif
432 oad(0xec81, size); /* sub $xxx, %esp */430 oad(0xec81, size); /* sub $xxx, %esp */
433 /* generate structure store */431 /* generate structure store */
434 r = get_reg(RC_INT);432 r = get_reg(RC_INT);
libtcc.c+7−8
@@ -967,14 +967,13 @@ LIBTCCAPI TCCState *tcc_new(void) · 966 unmodified lines
967 tcc_define_symbol(s, "_FORTIFY_SOURCE", "0");967 tcc_define_symbol(s, "_FORTIFY_SOURCE", "0");
968#endif /* ndef TCC_TARGET_MACHO */968#endif /* ndef TCC_TARGET_MACHO */
969 969
970 if (PTR_SIZE == 4) {970#if LONG_SIZE == 4
971 tcc_define_symbol(s, "__SIZEOF_LONG__", "4");971 tcc_define_symbol(s, "__SIZEOF_LONG__", "4");
972 tcc_define_symbol(s, "__LONG_MAX__", "0x7fffffffL");972 tcc_define_symbol(s, "__LONG_MAX__", "0x7fffffffL");
973 }973#else
974 else {974 tcc_define_symbol(s, "__SIZEOF_LONG__", "8");
975 tcc_define_symbol(s, "__SIZEOF_LONG__", "8");975 tcc_define_symbol(s, "__LONG_MAX__", "0x7fffffffffffffffL");
976 tcc_define_symbol(s, "__LONG_MAX__", "0x7fffffffffffffffL");976#endif
977 }
978 tcc_define_symbol(s, "__SIZEOF_INT__", "4");977 tcc_define_symbol(s, "__SIZEOF_INT__", "4");
979 tcc_define_symbol(s, "__SIZEOF_LONG_LONG__", "8");978 tcc_define_symbol(s, "__SIZEOF_LONG_LONG__", "8");
980 tcc_define_symbol(s, "__CHAR_BIT__", "8");979 tcc_define_symbol(s, "__CHAR_BIT__", "8");
riscv64-gen.c+1−9
@@ -66,7 +66,7 @@ ST_DATA const int reg_classes[NB_REGS] = { · 65 unmodified lines
66#if defined(CONFIG_TCC_BCHECK)66#if defined(CONFIG_TCC_BCHECK)
67static addr_t func_bound_offset;67static addr_t func_bound_offset;
68static unsigned long func_bound_ind;68static unsigned long func_bound_ind;
69static int func_bound_add_epilog;69ST_DATA int func_bound_add_epilog;
70#endif70#endif
71 71
72static int ireg(int r)72static int ireg(int r)
@@ -387,14 +387,6 @@ static void gcall_or_jmp(int docall) · 314 unmodified lines
387 R_RISCV_CALL_PLT, (int)vtop->c.i);387 R_RISCV_CALL_PLT, (int)vtop->c.i);
388 o(0x17 | (tr << 7)); // auipc TR, 0 %call(func)388 o(0x17 | (tr << 7)); // auipc TR, 0 %call(func)
389 EI(0x67, 0, tr, tr, 0);// jalr TR, r(TR)389 EI(0x67, 0, tr, tr, 0);// jalr TR, r(TR)
390#ifdef CONFIG_TCC_BCHECK
391 if (tcc_state->do_bounds_check &&
392 (vtop->sym->v == TOK_setjmp ||
393 vtop->sym->v == TOK__setjmp ||
394 vtop->sym->v == TOK_sigsetjmp ||
395 vtop->sym->v == TOK___sigsetjmp))
396 func_bound_add_epilog = 1;
397#endif
398 } else if (vtop->r < VT_CONST) {390 } else if (vtop->r < VT_CONST) {
399 int r = ireg(vtop->r);391 int r = ireg(vtop->r);
400 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)392 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
tcc.h+1−1
@@ -643,7 +643,6 @@ typedef struct AttributeDef { · 642 unmodified lines
643 struct FuncAttr f;643 struct FuncAttr f;
644 struct Section *section;644 struct Section *section;
645 Sym *cleanup_func;645 Sym *cleanup_func;
646 int alias_target; /* token */
647 int asm_label; /* associated asm label */646 int asm_label; /* associated asm label */
648 char attr_mode; /* __attribute__((__mode__(...))) */647 char attr_mode; /* __attribute__((__mode__(...))) */
649} AttributeDef;648} AttributeDef;
@@ -1475,6 +1474,7 @@ ST_FUNC int classify_x86_64_va_arg(CType *ty); · 825 unmodified lines
1475#endif1474#endif
1476#ifdef CONFIG_TCC_BCHECK1475#ifdef CONFIG_TCC_BCHECK
1477ST_FUNC void gbound_args(int nb_args);1476ST_FUNC void gbound_args(int nb_args);
1477ST_DATA int func_bound_add_epilog;
1478#endif1478#endif
1479 1479
1480/* ------------ tccelf.c ------------ */1480/* ------------ tccelf.c ------------ */
tccgen.c+71−317
@@ -80,7 +80,7 @@ ST_DATA int func_var; /* true if current function is variadic (used by return in · 79 unmodified lines
80ST_DATA int func_vc;80ST_DATA int func_vc;
81static int last_line_num, new_file, func_ind; /* debug info control */81static int last_line_num, new_file, func_ind; /* debug info control */
82ST_DATA const char *funcname;82ST_DATA const char *funcname;
83ST_DATA CType int_type, func_old_type, char_pointer_type;83ST_DATA CType int_type, func_old_type, char_type, char_pointer_type;
84static CString initstr;84static CString initstr;
85 85
86#if PTR_SIZE == 486#if PTR_SIZE == 4
@@ -771,8 +771,13 @@ ST_FUNC void tccgen_init(TCCState *s1) · 684 unmodified lines
771 771
772 /* define some often used types */772 /* define some often used types */
773 int_type.t = VT_INT;773 int_type.t = VT_INT;
774 char_pointer_type.t = VT_BYTE;774
775 char_type.t = VT_BYTE;
776 if (s1->char_is_unsigned)
777 char_type.t |= VT_UNSIGNED;
778 char_pointer_type = char_type;
775 mk_pointer(&char_pointer_type);779 mk_pointer(&char_pointer_type);
780
776 func_old_type.t = VT_FUNC;781 func_old_type.t = VT_FUNC;
777 func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);782 func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);
778 func_old_type.ref->f.func_call = FUNC_CDECL;783 func_old_type.ref->f.func_call = FUNC_CDECL;
@@ -886,66 +891,9 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, · 107 unmodified lines
886 ElfSym *esym;891 ElfSym *esym;
887 const char *name;892 const char *name;
888 char buf1[256];893 char buf1[256];
889#ifdef CONFIG_TCC_BCHECK894
890 char buf[32];
891#endif
892 if (!sym->c) {895 if (!sym->c) {
893 name = get_tok_str(sym->v, NULL);896 name = get_tok_str(sym->v, NULL);
894#ifdef CONFIG_TCC_BCHECK
895 if (tcc_state->do_bounds_check) {
896 /* XXX: avoid doing that for statics ? */
897 /* if bound checking is activated, we change some function
898 names by adding the "__bound" prefix */
899#if defined(TCC_TARGET_ARM) && defined(TCC_ARM_EABI)
900 if (strcmp (name, "memcpy") == 0 ||
901 strcmp (name, "memmove") == 0 ||
902 strcmp (name, "memset") == 0)
903 goto add_bound;
904#endif
905 switch(sym->v) {
906#ifdef TCC_TARGET_PE
907 /* XXX: we rely only on malloc hooks */
908 case TOK_malloc:
909 case TOK_free:
910 case TOK_realloc:
911 case TOK_memalign:
912 case TOK_calloc:
913#endif
914 case TOK_memcpy:
915 case TOK_memmove:
916#if defined(TCC_TARGET_ARM) && defined(TCC_ARM_EABI)
917 case TOK_memmove4:
918 case TOK_memmove8:
919#endif
920 case TOK_memset:
921 case TOK_memcmp:
922 case TOK_strlen:
923 case TOK_strcpy:
924 case TOK_strncpy:
925 case TOK_strcmp:
926 case TOK_strncmp:
927 case TOK_strcat:
928 case TOK_strchr:
929 case TOK_strdup:
930#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
931 case TOK_alloca:
932#endif
933 case TOK_mmap:
934 case TOK_munmap:
935 case TOK_longjmp:
936#ifndef TCC_TARGET_PE
937 case TOK_siglongjmp:
938#endif
939#if defined(TCC_TARGET_ARM) && defined(TCC_ARM_EABI)
940add_bound:
941#endif
942 strcpy(buf, "__bound_");
943 strcat(buf, name);
944 name = buf;
945 break;
946 }
947 }
948#endif
949 t = sym->type.t;897 t = sym->type.t;
950 if ((t & VT_BTYPE) == VT_FUNC) {898 if ((t & VT_BTYPE) == VT_FUNC) {
951 sym_type = STT_FUNC;899 sym_type = STT_FUNC;
@@ -959,6 +907,7 @@ add_bound: · 7 unmodified lines
959 else907 else
960 sym_bind = STB_GLOBAL;908 sym_bind = STB_GLOBAL;
961 other = 0;909 other = 0;
910
962#ifdef TCC_TARGET_PE911#ifdef TCC_TARGET_PE
963 if (sym_type == STT_FUNC && sym->type.ref) {912 if (sym_type == STT_FUNC && sym->type.ref) {
964 Sym *ref = sym->type.ref;913 Sym *ref = sym->type.ref;
@@ -973,13 +922,20 @@ add_bound: · 8 unmodified lines
973 }922 }
974 }923 }
975#endif924#endif
925
926 if (sym->asm_label) {
927 name = get_tok_str(sym->asm_label & ~SYM_FIELD, NULL);
928 /* with SYM_FIELD it was __attribute__((alias("..."))) actually */
929 if (!(sym->asm_label & SYM_FIELD))
930 can_add_underscore = 0;
931 }
932
976 if (tcc_state->leading_underscore && can_add_underscore) {933 if (tcc_state->leading_underscore && can_add_underscore) {
977 buf1[0] = '_';934 buf1[0] = '_';
978 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);935 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
979 name = buf1;936 name = buf1;
980 }937 }
981 if (sym->asm_label)938
982 name = get_tok_str(sym->asm_label, NULL);
983 info = ELFW(ST_INFO)(sym_bind, sym_type);939 info = ELFW(ST_INFO)(sym_bind, sym_type);
984 sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);940 sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);
985 941
@@ -1553,8 +1509,6 @@ static void merge_attr(AttributeDef *ad, AttributeDef *ad1) · 567 unmodified lines
1553 1509
1554 if (ad1->section)1510 if (ad1->section)
1555 ad->section = ad1->section;1511 ad->section = ad1->section;
1556 if (ad1->alias_target)
1557 ad->alias_target = ad1->alias_target;
1558 if (ad1->asm_label)1512 if (ad1->asm_label)
1559 ad->asm_label = ad1->asm_label;1513 ad->asm_label = ad1->asm_label;
1560 if (ad1->attr_mode)1514 if (ad1->attr_mode)
@@ -1940,13 +1894,34 @@ static void gbound(void) · 379 unmodified lines
1940 args into registers */1894 args into registers */
1941ST_FUNC void gbound_args(int nb_args)1895ST_FUNC void gbound_args(int nb_args)
1942{1896{
1943 int i;1897 int i, v;
1898 SValue *sv;
1899
1944 for (i = 1; i <= nb_args; ++i)1900 for (i = 1; i <= nb_args; ++i)
1945 if (vtop[1 - i].r & VT_MUSTBOUND) {1901 if (vtop[1 - i].r & VT_MUSTBOUND) {
1946 vrotb(i);1902 vrotb(i);
1947 gbound();1903 gbound();
1948 vrott(i);1904 vrott(i);
1949 }1905 }
1906
1907 sv = vtop - nb_args;
1908 if (sv->r & VT_SYM) {
1909 v = sv->sym->v;
1910 if (v == TOK_setjmp
1911 || v == TOK__setjmp
1912#ifndef TCC_TARGET_PE
1913 || v == TOK_sigsetjmp
1914 || v == TOK___sigsetjmp
1915#endif
1916 ) {
1917 vpush_global_sym(&func_old_type, TOK___bound_setjmp);
1918 vpushv(sv + 1);
1919 gfunc_call(1);
1920 func_bound_add_epilog = 1;
1921 }
1922 if (v == TOK_alloca)
1923 func_bound_add_epilog = 1;
1924 }
1950}1925}
1951 1926
1952/* Add bounds for local symbols from S to E (via ->prev) */1927/* Add bounds for local symbols from S to E (via ->prev) */
@@ -4099,8 +4074,8 @@ redo: · 2146 unmodified lines
4099 case TOK_ALIAS2:4074 case TOK_ALIAS2:
4100 skip('(');4075 skip('(');
4101 parse_mult_str(&astr, "alias(\"target\")");4076 parse_mult_str(&astr, "alias(\"target\")");
4102 ad->alias_target = /* save string as token, for later */4077 ad->asm_label = /* save string as token, for later */
4103 tok_alloc((char*)astr.data, astr.size-1)->tok;4078 tok_alloc((char*)astr.data, astr.size-1)->tok | SYM_FIELD;
4104 skip(')');4079 skip(')');
4105 cstr_free(&astr);4080 cstr_free(&astr);
4106 break;4081 break;
@@ -5365,43 +5340,39 @@ static void parse_builtin_params(int nc, const char *args) · 1258 unmodified lines
5365 while ((c = *args++)) {5340 while ((c = *args++)) {
5366 skip(sep);5341 skip(sep);
5367 sep = ',';5342 sep = ',';
5343 if (c == 't') {
5344 parse_type(&type);
5345 vpush(&type);
5346 continue;
5347 }
5348 expr_eq();
5349 type.ref = NULL;
5350 type.t = 0;
5368 switch (c) {5351 switch (c) {
5369 case 'e': expr_eq();5352 case 'e':
5370 continue;5353 continue;
5371 case 't': parse_type(&type);5354 case 'V':
5372 vpush(&type);5355 type.t = VT_CONSTANT;
5373 continue;
5374 case 'v':5356 case 'v':
5375 case 'V': expr_eq();5357 type.t |= VT_VOID;
5376 type.t = VT_VOID;5358 mk_pointer (&type);
5377 if (c == 'V') type.t |= VT_CONSTANT;5359 break;
5378 type.ref = NULL;5360 case 'S':
5379 mk_pointer (&type);5361 type.t = VT_CONSTANT;
5380 gen_assign_cast(&type);
5381 continue;
5382 case 's':5362 case 's':
5383 case 'S': expr_eq();5363 type.t |= char_type.t;
5384 type.t = VT_BYTE;5364 mk_pointer (&type);
5385 if (tcc_state->char_is_unsigned)5365 break;
5386 type.t |= VT_UNSIGNED;5366 case 'i':
5387 if (c == 'S') type.t |= VT_CONSTANT;5367 type.t = VT_INT;
5388 type.ref = NULL;5368 break;
5389 mk_pointer (&type);5369 case 'l':
5390 gen_assign_cast(&type);5370 type.t = VT_SIZE_T;
5391 continue;5371 break;
5392 case 'i': expr_eq();5372 default:
5393 type.t = VT_INT;5373 tcc_error("internal error");
5394 type.ref = NULL;
5395 gen_assign_cast(&type);
5396 continue;
5397 case 'l': expr_eq();
5398 type.t = VT_SIZE_T;
5399 type.ref = NULL;
5400 gen_assign_cast(&type);
5401 continue;
5402 default: tcc_error("internal error");
5403 break;
5404 }5374 }
5375 gen_assign_cast(&type);
5405 }5376 }
5406 skip(')');5377 skip(')');
5407 if (nc)5378 if (nc)
@@ -5780,196 +5751,6 @@ ST_FUNC void unary(void) · 372 unmodified lines
5780 break;5751 break;
5781 }5752 }
5782#endif5753#endif
5783 case TOK___builtin_abort:
5784 vpush_global_sym(&func_old_type, TOK_abort);
5785 parse_builtin_params(0, "");
5786 gfunc_call(0);
5787builtin_void_return:
5788 vpushi(0);
5789 type.t = VT_VOID;
5790 type.ref = NULL;
5791 vtop->type = type;
5792 vtop->r = R_RET(type.t);
5793 break;
5794 case TOK___builtin_memcpy:
5795 t = TOK_memcpy;
5796#ifdef CONFIG_TCC_BCHECK
5797 if (tcc_state->do_bounds_check) t = TOK___bound_memcpy;
5798#endif
5799 vpush_global_sym(&func_old_type, t);
5800 parse_builtin_params(0, "vVl");
5801 gfunc_call(3);
5802builtin_void_ptr_return:
5803 vpushi(0);
5804 type.t = VT_VOID;
5805 type.ref = NULL;
5806 mk_pointer (&type);
5807 vtop->type = type;
5808 vtop->r = R_RET(type.t);
5809 break;
5810 case TOK___builtin_memcmp:
5811 t = TOK_memcmp;
5812#ifdef CONFIG_TCC_BCHECK
5813 if (tcc_state->do_bounds_check) t = TOK___bound_memcmp;
5814#endif
5815 vpush_global_sym(&func_old_type, t);
5816 parse_builtin_params(0, "VVl");
5817 gfunc_call(3);
5818builtin_int_return:
5819 vpushi(0);
5820 type.t = VT_INT;
5821 type.ref = NULL;
5822 vtop->type = type;
5823 vtop->r = R_RET(type.t);
5824 break;
5825 case TOK___builtin_memmove:
5826 t = TOK_memmove;
5827#ifdef CONFIG_TCC_BCHECK
5828 if (tcc_state->do_bounds_check) t = TOK___bound_memmove;
5829#endif
5830 vpush_global_sym(&func_old_type, t);
5831 parse_builtin_params(0, "vVl");
5832 gfunc_call(3);
5833 goto builtin_void_ptr_return;
5834 case TOK___builtin_memset:
5835 t = TOK_memset;
5836#ifdef CONFIG_TCC_BCHECK
5837 if (tcc_state->do_bounds_check) t = TOK___bound_memset;
5838#endif
5839 vpush_global_sym(&func_old_type, t);
5840 parse_builtin_params(0, "vil");
5841 gfunc_call(3);
5842 goto builtin_void_ptr_return;
5843 case TOK___builtin_strlen:
5844 t = TOK_strlen;
5845#ifdef CONFIG_TCC_BCHECK
5846 if (tcc_state->do_bounds_check) t = TOK___bound_strlen;
5847#endif
5848 vpush_global_sym(&func_old_type, t);
5849 parse_builtin_params(0, "S");
5850 gfunc_call(1);
5851 vpushi(0);
5852 type.t = VT_SIZE_T;
5853 type.ref = NULL;
5854 vtop->type = type;
5855 vtop->r = R_RET(type.t);
5856 break;
5857 case TOK___builtin_strcpy:
5858 t = TOK_strcpy;
5859#ifdef CONFIG_TCC_BCHECK
5860 if (tcc_state->do_bounds_check) t = TOK___bound_strcpy;
5861#endif
5862 vpush_global_sym(&func_old_type, t);
5863 parse_builtin_params(0, "sS");
5864 gfunc_call(2);
5865builtin_string_ptr_return:
5866 vpushi(0);
5867 type.t = VT_BYTE;
5868 if (tcc_state->char_is_unsigned)
5869 type.t |= VT_UNSIGNED;
5870 type.ref = NULL;
5871 mk_pointer (&type);
5872 vtop->type = type;
5873 vtop->r = R_RET(type.t);
5874 break;
5875 case TOK___builtin_strncpy:
5876 t = TOK_strncpy;
5877#ifdef CONFIG_TCC_BCHECK
5878 if (tcc_state->do_bounds_check) t = TOK___bound_strncpy;
5879#endif
5880 vpush_global_sym(&func_old_type, t);
5881 parse_builtin_params(0, "sSl");
5882 gfunc_call(3);
5883 goto builtin_string_ptr_return;
5884 case TOK___builtin_strcmp:
5885 t = TOK_strcmp;
5886#ifdef CONFIG_TCC_BCHECK
5887 if (tcc_state->do_bounds_check) t = TOK___bound_strcmp;
5888#endif
5889 vpush_global_sym(&func_old_type, t);
5890 parse_builtin_params(0, "SS");
5891 gfunc_call(2);
5892 goto builtin_int_return;
5893 case TOK___builtin_strncmp:
5894 t = TOK_strncmp;
5895#ifdef CONFIG_TCC_BCHECK
5896 if (tcc_state->do_bounds_check) t = TOK___bound_strncmp;
5897#endif
5898 vpush_global_sym(&func_old_type, t);
5899 parse_builtin_params(0, "SSl");
5900 gfunc_call(3);
5901 goto builtin_int_return;
5902 case TOK___builtin_strcat:
5903 t = TOK_strcat;
5904#ifdef CONFIG_TCC_BCHECK
5905 if (tcc_state->do_bounds_check) t = TOK___bound_strcat;
5906#endif
5907 vpush_global_sym(&func_old_type, t);
5908 parse_builtin_params(0, "sS");
5909 gfunc_call(2);
5910 goto builtin_string_ptr_return;
5911 case TOK___builtin_strchr:
5912 t = TOK_strchr;
5913#ifdef CONFIG_TCC_BCHECK
5914 if (tcc_state->do_bounds_check) t = TOK___bound_strchr;
5915#endif
5916 vpush_global_sym(&func_old_type, t);
5917 parse_builtin_params(0, "Si");
5918 gfunc_call(2);
5919 goto builtin_string_ptr_return;
5920 case TOK___builtin_strdup:
5921 t = TOK_strdup;
5922#ifdef CONFIG_TCC_BCHECK
5923 if (tcc_state->do_bounds_check) t = TOK___bound_strdup;
5924#endif
5925 vpush_global_sym(&func_old_type, t);
5926 parse_builtin_params(0, "S");
5927 gfunc_call(1);
5928 goto builtin_string_ptr_return;
5929 case TOK___builtin_malloc:
5930 t = TOK_malloc;
5931#if defined(CONFIG_TCC_BCHECK) && defined(TCC_TARGET_PE)
5932 if (tcc_state->do_bounds_check) t = TOK___bound_malloc;
5933#endif
5934 vpush_global_sym(&func_old_type, t);
5935 parse_builtin_params(0, "l");
5936 gfunc_call(1);
5937 goto builtin_void_ptr_return;
5938 case TOK___builtin_realloc:
5939 t = TOK_realloc;
5940#if defined(CONFIG_TCC_BCHECK) && defined(TCC_TARGET_PE)
5941 if (tcc_state->do_bounds_check) t = TOK___bound_realloc;
5942#endif
5943 vpush_global_sym(&func_old_type, t);
5944 parse_builtin_params(0, "vl");
5945 gfunc_call(2);
5946 goto builtin_void_ptr_return;
5947 case TOK___builtin_calloc:
5948 t = TOK_calloc;
5949#if defined(CONFIG_TCC_BCHECK) && defined(TCC_TARGET_PE)
5950 if (tcc_state->do_bounds_check) t = TOK___bound_calloc;
5951#endif
5952 vpush_global_sym(&func_old_type, t);
5953 parse_builtin_params(0, "ll");
5954 gfunc_call(2);
5955 goto builtin_void_ptr_return;
5956 case TOK___builtin_free:
5957 t = TOK_free;
5958#if defined(CONFIG_TCC_BCHECK) && defined(TCC_TARGET_PE)
5959 if (tcc_state->do_bounds_check) t = TOK___bound_free;
5960#endif
5961 vpush_global_sym(&func_old_type, t);
5962 parse_builtin_params(0, "v");
5963 gfunc_call(1);
5964 goto builtin_void_return;
5965#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
5966 case TOK_alloca:
5967 case TOK___builtin_alloca:
5968 vpush_global_sym(&func_old_type, TOK_alloca);
5969 parse_builtin_params(0, "l");
5970 gfunc_call(1);
5971 goto builtin_void_ptr_return;
5972#endif
5973 5754
5974 /* pre operations */5755 /* pre operations */
5975 case TOK_INC:5756 case TOK_INC:
@@ -6276,24 +6057,6 @@ special_math_val: · 300 unmodified lines
6276 if (sa)6057 if (sa)
6277 tcc_error("too few arguments to function");6058 tcc_error("too few arguments to function");
6278 skip(')');6059 skip(')');
6279#ifdef CONFIG_TCC_BCHECK
6280 if (tcc_state->do_bounds_check &&
6281 (nb_args == 1 || nb_args == 2) &&
6282 (vtop[-nb_args].r & VT_SYM) &&
6283 (vtop[-nb_args].sym->v == TOK_setjmp ||
6284 vtop[-nb_args].sym->v == TOK__setjmp
6285#ifndef TCC_TARGET_PE
6286 || vtop[-nb_args].sym->v == TOK_sigsetjmp
6287 || vtop[-nb_args].sym->v == TOK___sigsetjmp
6288#endif
6289 )) {
6290 vpush_global_sym(&func_old_type, TOK___bound_setjmp);
6291 vpushv(vtop - nb_args);
6292 if (nb_args == 2)
6293 vpushv(vtop - nb_args);
6294 gfunc_call(nb_args);
6295 }
6296#endif
6297 gfunc_call(nb_args);6060 gfunc_call(nb_args);
6298 6061
6299 if (ret_nregs < 0) {6062 if (ret_nregs < 0) {
@@ -8602,15 +8365,6 @@ found: · 2302 unmodified lines
8602 /* external variable or function */8365 /* external variable or function */
8603 type.t |= VT_EXTERN;8366 type.t |= VT_EXTERN;
8604 sym = external_sym(v, &type, r, &ad);8367 sym = external_sym(v, &type, r, &ad);
8605 if (ad.alias_target) {
8606 ElfSym *esym;
8607 Sym *alias_target;
8608 alias_target = sym_find(ad.alias_target);
8609 esym = elfsym(alias_target);
8610 if (!esym)
8611 tcc_error("unsupported forward __alias__ attribute");
8612 put_extern_sym2(sym, esym->st_shndx, esym->st_value, esym->st_size, 0);
8613 }
8614 } else {8368 } else {
8615 if (type.t & VT_STATIC)8369 if (type.t & VT_STATIC)
8616 r |= VT_CONST;8370 r |= VT_CONST;
tccpp.c+52−7
@@ -3625,10 +3625,13 @@ ST_INLN void unget_tok(int last_tok) · 3624 unmodified lines
3625static void tcc_predefs(CString *cstr)3625static void tcc_predefs(CString *cstr)
3626{3626{
3627 cstr_cat(cstr,3627 cstr_cat(cstr,
3628
3629 //"#include <tcc_predefs.h>\n"
3630
3628#if defined TCC_TARGET_X86_643631#if defined TCC_TARGET_X86_64
3629#ifndef TCC_TARGET_PE3632#ifndef TCC_TARGET_PE
3630 /* GCC compatible definition of va_list. This should be in sync3633 /* GCC compatible definition of va_list. */
3631 with the declaration in our lib/libtcc1.c */3634 /* This should be in sync with the declaration in our lib/libtcc1.c */
3632 "typedef struct{\n"3635 "typedef struct{\n"
3633 "unsigned gp_offset,fp_offset;\n"3636 "unsigned gp_offset,fp_offset;\n"
3634 "union{\n"3637 "union{\n"
@@ -3644,7 +3647,6 @@ static void tcc_predefs(CString *cstr) · 9 unmodified lines
3644#else /* TCC_TARGET_PE */3647#else /* TCC_TARGET_PE */
3645 "typedef char*__builtin_va_list;\n"3648 "typedef char*__builtin_va_list;\n"
3646 "#define __builtin_va_arg(ap,t) ((sizeof(t)>8||(sizeof(t)&(sizeof(t)-1)))?**(t**)((ap+=8)-8):*(t*)((ap+=8)-8))\n"3649 "#define __builtin_va_arg(ap,t) ((sizeof(t)>8||(sizeof(t)&(sizeof(t)-1)))?**(t**)((ap+=8)-8):*(t*)((ap+=8)-8))\n"
3647 "#define __builtin_va_copy(dest,src) (dest)=(src)\n"
3648#endif3650#endif
3649#elif defined TCC_TARGET_ARM3651#elif defined TCC_TARGET_ARM
3650 "typedef char*__builtin_va_list;\n"3652 "typedef char*__builtin_va_list;\n"
@@ -3652,26 +3654,69 @@ static void tcc_predefs(CString *cstr) · 1 unmodified line
3652 "#define _tcc_align(addr,type) (((unsigned)addr+_tcc_alignof(type)-1)&~(_tcc_alignof(type)-1))\n"3654 "#define _tcc_align(addr,type) (((unsigned)addr+_tcc_alignof(type)-1)&~(_tcc_alignof(type)-1))\n"
3653 "#define __builtin_va_start(ap,last) (ap=((char*)&(last))+((sizeof(last)+3)&~3))\n"3655 "#define __builtin_va_start(ap,last) (ap=((char*)&(last))+((sizeof(last)+3)&~3))\n"
3654 "#define __builtin_va_arg(ap,type) (ap=(void*)((_tcc_align(ap,type)+sizeof(type)+3)&~3),*(type*)(ap-((sizeof(type)+3)&~3)))\n"3656 "#define __builtin_va_arg(ap,type) (ap=(void*)((_tcc_align(ap,type)+sizeof(type)+3)&~3),*(type*)(ap-((sizeof(type)+3)&~3)))\n"
3655 "#define __builtin_va_copy(dest,src) (dest)=(src)\n"
3656#elif defined TCC_TARGET_ARM643657#elif defined TCC_TARGET_ARM64
3657 "typedef struct{\n"3658 "typedef struct{\n"
3658 "void*__stack,*__gr_top,*__vr_top;\n"3659 "void*__stack,*__gr_top,*__vr_top;\n"
3659 "int __gr_offs,__vr_offs;\n"3660 "int __gr_offs,__vr_offs;\n"
3660 "}__builtin_va_list;\n"3661 "}__builtin_va_list;\n"
3661 "#define __builtin_va_copy(dest,src) (dest)=(src)\n"
3662#elif defined TCC_TARGET_RISCV643662#elif defined TCC_TARGET_RISCV64
3663 "typedef char*__builtin_va_list;\n"3663 "typedef char*__builtin_va_list;\n"
3664 "#define __va_reg_size (__riscv_xlen>>3)\n"3664 "#define __va_reg_size (__riscv_xlen>>3)\n"
3665 "#define _tcc_align(addr,type) (((unsigned long)addr+__alignof__(type)-1)&-(__alignof__(type)))\n"3665 "#define _tcc_align(addr,type) (((unsigned long)addr+__alignof__(type)-1)&-(__alignof__(type)))\n"
3666 "#define __builtin_va_arg(ap,type) (*(sizeof(type)>(2*__va_reg_size)?*(type**)((ap+=__va_reg_size)-__va_reg_size):(ap=(va_list)(_tcc_align(ap,type)+(sizeof(type)+__va_reg_size-1)&-__va_reg_size),(type*)(ap-((sizeof(type)+__va_reg_size-1)&-__va_reg_size)))))\n"3666 "#define __builtin_va_arg(ap,type) (*(sizeof(type)>(2*__va_reg_size)?*(type**)((ap+=__va_reg_size)-__va_reg_size):(ap=(va_list)(_tcc_align(ap,type)+(sizeof(type)+__va_reg_size-1)&-__va_reg_size),(type*)(ap-((sizeof(type)+__va_reg_size-1)&-__va_reg_size)))))\n"
3667 "#define __builtin_va_copy(dest,src) (dest)=(src)\n"
3668#else /* TCC_TARGET_I386 */3667#else /* TCC_TARGET_I386 */
3669 "typedef char*__builtin_va_list;\n"3668 "typedef char*__builtin_va_list;\n"
3670 "#define __builtin_va_start(ap,last) (ap=((char*)&(last))+((sizeof(last)+3)&~3))\n"3669 "#define __builtin_va_start(ap,last) (ap=((char*)&(last))+((sizeof(last)+3)&~3))\n"
3671 "#define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3)))\n"3670 "#define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3)))\n"
3672 "#define __builtin_va_copy(dest,src) (dest)=(src)\n"
3673#endif3671#endif
3674 "#define __builtin_va_end(ap) (void)(ap)\n"3672 "#define __builtin_va_end(ap) (void)(ap)\n"
3673 "#ifndef __builtin_va_copy\n"
3674 "#define __builtin_va_copy(dest,src) (dest)=(src)\n"
3675 "#endif\n"
3676 /* TCC BBUILTIN AND BOUNDS ALIASES */
3677 "#ifdef __BOUNDS_CHECKING_ON\n"
3678 "#define __BUILTIN(ret,name,params) ret __builtin_##name params __attribute__((alias(\"__bound_\"#name)));\n"
3679 "#define __BOUND(ret,name,params) ret name params __attribute__((alias(\"__bound_\"#name)));\n"
3680 "#else\n"
3681 "#define __BUILTIN(ret,name,params) ret __builtin_##name params __attribute__((alias(#name)));\n"
3682 "#define __BOUND(ret,name,params)\n"
3683 "#endif\n"
3684 "#define __BOTH(ret,name,params) __BUILTIN(ret,name,params)__BOUND(ret,name,params)\n"
3685 "__BOTH(void*,memcpy,(void*,const void*,__SIZE_TYPE__))\n"
3686 "__BOTH(void*,memmove,(void*,const void*,__SIZE_TYPE__))\n"
3687 "__BOTH(void*,memset,(void*,int,__SIZE_TYPE__))\n"
3688 "__BOTH(int,memcmp,(const void*,const void*,__SIZE_TYPE__))\n"
3689 "__BOTH(__SIZE_TYPE__,strlen,(const char*))\n"
3690 "__BOTH(char*,strcpy,(char*,const char*))\n"
3691 "__BOTH(char*,strncpy,(char*,const char*,__SIZE_TYPE__))\n"
3692 "__BOTH(int,strcmp,(const char*,const char*))\n"
3693 "__BOTH(int,strncmp,(const char*,const char*,__SIZE_TYPE__))\n"
3694 "__BOTH(char*,strcat,(char*,const char*))\n"
3695 "__BOTH(char*,strchr,(const char*,int))\n"
3696 "__BOTH(char*,strdup,(const char*))\n"
3697#ifdef TCC_TARGET_PE
3698 "#define __MAYBE_REDIR __BOTH\n"
3699#else // HAVE MALLOC_REDIR
3700 "#define __MAYBE_REDIR __BUILTIN\n"
3701#endif
3702 "__MAYBE_REDIR(void*,malloc,(__SIZE_TYPE__))\n"
3703 "__MAYBE_REDIR(void*,realloc,(void*,__SIZE_TYPE__))\n"
3704 "__MAYBE_REDIR(void*,calloc,(__SIZE_TYPE__,__SIZE_TYPE__))\n"
3705 "__MAYBE_REDIR(void*,memalign,(__SIZE_TYPE__,__SIZE_TYPE__))\n"
3706 "__MAYBE_REDIR(void,free,(void*))\n"
3707#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
3708 "__BOTH(void*,alloca,(__SIZE_TYPE__))\n"
3709#endif
3710 "__BUILTIN(void,abort,(void))\n"
3711 "__BOUND(int,longjmp,())\n"
3712#ifndef TCC_TARGET_PE
3713 "__BOUND(void*,mmap,())\n"
3714 "__BOUND(void*,munmap,())\n"
3715#endif
3716 "#undef __BUILTIN\n"
3717 "#undef __BOUND\n"
3718 "#undef __BOTH\n"
3719 "#undef __MAYBE_REDIR\n"
3675 , -1);3720 , -1);
3676}3721}
3677 3722
tcctok.h+0−55
@@ -314,68 +314,13 @@ · 313 unmodified lines
314# ifdef TCC_TARGET_X86_64314# ifdef TCC_TARGET_X86_64
315 DEF(TOK___bound_alloca_nr, "__bound_alloca_nr")315 DEF(TOK___bound_alloca_nr, "__bound_alloca_nr")
316# endif316# endif
317 DEF(TOK_memalign, "memalign")
318# else317# else
319 DEF(TOK_sigsetjmp, "sigsetjmp")318 DEF(TOK_sigsetjmp, "sigsetjmp")
320 DEF(TOK___sigsetjmp, "__sigsetjmp")319 DEF(TOK___sigsetjmp, "__sigsetjmp")
321 DEF(TOK_siglongjmp, "siglongjmp")320 DEF(TOK_siglongjmp, "siglongjmp")
322# endif321# endif
323 DEF(TOK_mmap, "mmap")
324 DEF(TOK_munmap, "munmap")
325 DEF(TOK_setjmp, "setjmp")322 DEF(TOK_setjmp, "setjmp")
326 DEF(TOK__setjmp, "_setjmp")323 DEF(TOK__setjmp, "_setjmp")
327 DEF(TOK_longjmp, "longjmp")
328 DEF(TOK___bound_memcpy, "__bound_memcpy")
329 DEF(TOK___bound_memcmp, "__bound_memcmp")
330 DEF(TOK___bound_memmove, "__bound_memmove")
331 DEF(TOK___bound_memset, "__bound_memset")
332 DEF(TOK___bound_strlen, "__bound_strlen")
333 DEF(TOK___bound_strcpy, "__bound_strcpy")
334 DEF(TOK___bound_strncpy, "__bound_strncpy")
335 DEF(TOK___bound_strcmp, "__bound_strcmp")
336 DEF(TOK___bound_strncmp, "__bound_strncmp")
337 DEF(TOK___bound_strcat, "__bound_strcat")
338 DEF(TOK___bound_strchr, "__bound_strchr")
339 DEF(TOK___bound_strdup, "__bound_strdup")
340 DEF(TOK___bound_malloc, "__bound_malloc")
341 DEF(TOK___bound_free, "__bound_free")
342 DEF(TOK___bound_realloc, "__bound_realloc")
343 DEF(TOK___bound_calloc, "__bound_calloc")
344#endif
345 DEF(TOK_abort, "abort")
346 DEF(TOK_memcmp, "memcmp")
347 DEF(TOK_strlen, "strlen")
348 DEF(TOK_strcpy, "strcpy")
349 DEF(TOK_strncpy, "strncpy")
350 DEF(TOK_strcmp, "strcmp")
351 DEF(TOK_strncmp, "strncmp")
352 DEF(TOK_strcat, "strcat")
353 DEF(TOK_strchr, "strchr")
354 DEF(TOK_strdup, "strdup")
355 DEF(TOK_malloc, "malloc")
356 DEF(TOK_free, "free")
357 DEF(TOK_realloc, "realloc")
358 DEF(TOK_calloc, "calloc")
359
360 DEF(TOK___builtin_abort, "__builtin_abort")
361 DEF(TOK___builtin_memcpy, "__builtin_memcpy")
362 DEF(TOK___builtin_memcmp, "__builtin_memcmp")
363 DEF(TOK___builtin_memmove, "__builtin_memmove")
364 DEF(TOK___builtin_memset, "__builtin_memset")
365 DEF(TOK___builtin_strlen, "__builtin_strlen")
366 DEF(TOK___builtin_strcpy, "__builtin_strcpy")
367 DEF(TOK___builtin_strncpy, "__builtin_strncpy")
368 DEF(TOK___builtin_strcmp, "__builtin_strcmp")
369 DEF(TOK___builtin_strncmp, "__builtin_strncmp")
370 DEF(TOK___builtin_strcat, "__builtin_strcat")
371 DEF(TOK___builtin_strchr, "__builtin_strchr")
372 DEF(TOK___builtin_strdup, "__builtin_strdup")
373 DEF(TOK___builtin_malloc, "__builtin_malloc")
374 DEF(TOK___builtin_free, "__builtin_free")
375 DEF(TOK___builtin_realloc, "__builtin_realloc")
376 DEF(TOK___builtin_calloc, "__builtin_calloc")
377#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
378 DEF(TOK___builtin_alloca, "__builtin_alloca")
379#endif324#endif
380 325
381/* Tiny Assembler */326/* Tiny Assembler */
tests/misc/c2str.c+112−0
@@ -0,0 +1,112 @@
1#include <stdio.h>
2#include <string.h>
3
4int isid(int c)
5{
6 return (c >= 'a' && c <= 'z')
7 || (c >= 'A' && c <= 'Z')
8 || (c >= '0' && c <= '9')
9 || c == '_';
10}
11
12int isspc(int c)
13{
14 return c == ' ' || c == '\t';
15}
16
17int main(int argc, char **argv)
18{
19 char l[1000], *p, l2[1000], *q;
20 FILE *fp, *op;
21 int c, e, f;
22
23 if (argc < 3)
24 return 1;
25
26 fp = fopen(argv[1], "rb");
27 op = fopen(argv[2], "wb");
28 if (!fp || !op) {
29 fprintf(stderr, "c2str: file error\n");
30 return 1;
31 }
32
33 for (;;) {
34 p = l;
35 append:
36 if (fgets(p, sizeof l - (p - l), fp)) {
37 p = strchr(p, 0);
38 while (p > l && p[-1] == '\n')
39 --p;
40 *p = 0;
41 } else if (p == l)
42 break;
43 if (p > l && p[-1] == '\\') {
44 --p;
45 goto append;
46 }
47
48 if (l[0] == 0)
49 continue;
50 p = l, q = l2, f = e = 0;
51 while (*p && isspc(*p))
52 ++p, ++f;
53
54 if (f < 4) {
55 do {
56 static const char *sr[] = {
57 "__x86_64__", "TCC_TARGET_X86_64",
58 "_WIN64", "TCC_TARGET_PE",
59 "_WIN32", "TCC_TARGET_PE",
60 "__arm__", "TCC_TARGET_ARM",
61 "__aarch64__", "TCC_TARGET_ARM64",
62 "__riscv", "TCC_TARGET_RISCV64",
63 "__i386__", "TCC_TARGET_I386", 0 };
64 for (f = 0; sr[f]; f += 2) {
65 c = strlen(sr[f]);
66 if (0 == memcmp(p, sr[f], c)) {
67 p += c, ++f;
68 q = strchr(strcpy(q, sr[f]), 0);
69 break;
70 }
71
72 }
73 if (sr[f])
74 continue;
75 } while (!!(*q++ = *p++));
76
77 fprintf(op, "%s\n", l2);
78
79 } else if (*p == '/') {
80 strcpy(q, p);
81 fprintf(op, " %s\n", l2);
82
83 } else {
84 f = 0;
85 for (;;) {
86 c = *p++;
87 if (isspc(c)) {
88 if (q == l2 || isspc(q[-1]))
89 continue;
90 if ((f > 2 || e) || (q[-1] != ')' && *p != '(')) {
91 if (!isid(q[-1]) || !isid(*p))
92 continue;
93 }
94 }
95 if (c == '(')
96 ++e, ++f;
97 if (c == ')')
98 --e, ++f;
99 if (c == '\\' || c == '\"')
100 *q++ = '\\';
101 *q++ = c;
102 if (c == 0)
103 break;
104 }
105 fprintf(op, " \"%s\\n\"\n", l2);
106 }
107 }
108
109 fclose(fp);
110 fclose(op);
111 return 0;
112}
tests/misc/tcc_predefs.h+111−0
@@ -0,0 +1,111 @@
1#if defined __x86_64__
2#ifndef _WIN64
3 /* GCC compatible definition of va_list. */
4 /* This should be in sync with the declaration in our lib/libtcc1.c */
5 typedef struct {
6 unsigned gp_offset, fp_offset;
7 union {
8 unsigned overflow_offset;
9 char *overflow_arg_area;
10 };
11 char *reg_save_area;
12 } __builtin_va_list[1];
13
14 void *__va_arg(__builtin_va_list ap, int arg_type, int size, int align);
15 #define __builtin_va_start(ap, last) \
16 (*(ap) = *(__builtin_va_list)((char*)__builtin_frame_address(0) - 24))
17 #define __builtin_va_arg(ap, t) \
18 (*(t *)(__va_arg(ap, __builtin_va_arg_types(t), sizeof(t), __alignof__(t))))
19 #define __builtin_va_copy(dest, src) (*(dest) = *(src))
20
21#else /* _WIN64 */
22 typedef char *__builtin_va_list;
23 #define __builtin_va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \
24 ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8))
25#endif
26
27#elif defined __arm__
28 typedef char *__builtin_va_list;
29 #define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x)
30 #define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \
31 & ~(_tcc_alignof(type) - 1))
32 #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
33 #define __builtin_va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \
34 &~3), *(type *)(ap - ((sizeof(type)+3)&~3)))
35
36#elif defined __aarch64__
37 typedef struct {
38 void *__stack, *__gr_top, *__vr_top;
39 int __gr_offs, __vr_offs;
40 } __builtin_va_list;
41
42#elif defined __riscv
43 typedef char *__builtin_va_list;
44 #define __va_reg_size (__riscv_xlen >> 3)
45 #define _tcc_align(addr,type) (((unsigned long)addr + __alignof__(type) - 1) \
46 & -(__alignof__(type)))
47 #define __builtin_va_arg(ap,type) (*(sizeof(type) > (2*__va_reg_size) ? *(type **)((ap += __va_reg_size) - __va_reg_size) : (ap = (va_list)(_tcc_align(ap,type) + (sizeof(type)+__va_reg_size - 1)& -__va_reg_size), (type *)(ap - ((sizeof(type)+ __va_reg_size - 1)& -__va_reg_size)))))
48
49#else /* __i386__ */
50 typedef char *__builtin_va_list;
51 #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
52 #define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3)))
53
54#endif
55 #define __builtin_va_end(ap) (void)(ap)
56 #ifndef __builtin_va_copy
57 # define __builtin_va_copy(dest, src) (dest) = (src)
58 #endif
59
60
61 /* TCC BBUILTIN AND BOUNDS ALIASES */
62 #ifdef __BOUNDS_CHECKING_ON
63 # define __BUILTIN(ret,name,params) \
64 ret __builtin_##name params __attribute__((alias("__bound_" #name)));
65 # define __BOUND(ret,name,params) \
66 ret name params __attribute__((alias("__bound_" #name)));
67 #else
68 # define __BUILTIN(ret,name,params) \
69 ret __builtin_##name params __attribute__((alias(#name)));
70 # define __BOUND(ret,name,params)
71 #endif
72 # define __BOTH(ret,name,params) \
73 __BUILTIN(ret,name,params) __BOUND(ret,name,params)
74
75 __BOTH(void*, memcpy, (void *, const void*, __SIZE_TYPE__))
76 __BOTH(void*, memmove, (void *, const void*, __SIZE_TYPE__))
77 __BOTH(void*, memset, (void *, int, __SIZE_TYPE__))
78 __BOTH(int, memcmp, (const void *, const void*, __SIZE_TYPE__))
79 __BOTH(__SIZE_TYPE__, strlen, (const char *))
80 __BOTH(char*, strcpy, (char *, const char *))
81 __BOTH(char*, strncpy, (char *, const char*, __SIZE_TYPE__))
82 __BOTH(int, strcmp, (const char*, const char*))
83 __BOTH(int, strncmp, (const char*, const char*, __SIZE_TYPE__))
84 __BOTH(char*, strcat, (char*, const char*))
85 __BOTH(char*, strchr, (const char*, int))
86 __BOTH(char*, strdup, (const char*))
87
88#ifdef _WIN32
89 #define __MAYBE_REDIR __BOTH
90#else // HAVE MALLOC_REDIR
91 #define __MAYBE_REDIR __BUILTIN
92#endif
93 __MAYBE_REDIR(void*, malloc, (__SIZE_TYPE__))
94 __MAYBE_REDIR(void*, realloc, (void *, __SIZE_TYPE__))
95 __MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__))
96 __MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__))
97 __MAYBE_REDIR(void, free, (void*))
98
99#if defined __i386__ || defined __x86_64__
100 __BOTH(void*, alloca, (__SIZE_TYPE__))
101#endif
102 __BUILTIN(void, abort, (void))
103 __BOUND(int, longjmp, ())
104#ifndef _WIN32
105 __BOUND(void*, mmap, ())
106 __BOUND(void*, munmap, ())
107#endif
108 #undef __BUILTIN
109 #undef __BOUND
110 #undef __BOTH
111 #undef __MAYBE_REDIR
tests/tcctest.c+145−277
@@ -2,10 +2,11 @@ · 1 unmodified line
2 * TCC auto test program2 * TCC auto test program
3 */3 */
4#include "config.h"4#include "config.h"
5
6/* identify the configured reference compiler in use */
5#define CC_gcc 17#define CC_gcc 1
6#define CC_clang 28#define CC_clang 2
7#define CC_tcc 39#define CC_tcc 3
8#define CC_msvc 4 /* cannot compile this file */
9 10
10/* Unfortunately, gcc version < 3 does not handle that! */11/* Unfortunately, gcc version < 3 does not handle that! */
11#define ALL_ISOC9912#define ALL_ISOC99
@@ -13,15 +14,9 @@ · 1 unmodified line
13/* only gcc 3 handles _Bool correctly */14/* only gcc 3 handles _Bool correctly */
14#define BOOL_ISOC9915#define BOOL_ISOC99
15 16
16/* gcc 2.95.3 does not handle correctly CR in strings or after strays */
17#define CORRECT_CR_HANDLING
18
19/* __VA_ARGS__ and __func__ support */17/* __VA_ARGS__ and __func__ support */
20#define C99_MACROS18#define C99_MACROS
21 19
22/* deprecated and no longer supported in gcc 3.3 */
23//#define ACCEPT_CR_IN_STRINGS
24
25#ifndef __TINYC__20#ifndef __TINYC__
26typedef __SIZE_TYPE__ uintptr_t;21typedef __SIZE_TYPE__ uintptr_t;
27#endif22#endif
@@ -81,61 +76,6 @@ typedef __SIZE_TYPE__ uintptr_t; · 53 unmodified lines
81#include incname76#include incname
82#include stringify(funnyname)77#include stringify(funnyname)
83 78
84void intdiv_test();
85void string_test();
86void expr_test();
87void macro_test();
88void recursive_macro_test();
89void scope_test();
90void scope_test2();
91void forward_test();
92void funcptr_test();
93void if_test();
94void loop_test();
95void switch_test();
96void goto_test();
97void enum_test();
98void typedef_test();
99void struct_test();
100void array_test();
101void expr_ptr_test();
102void bool_test();
103void optimize_out();
104void expr2_test();
105void constant_expr_test();
106void expr_cmp_test();
107void char_short_test();
108void init_test(void);
109void compound_literal_test(void);
110int kr_test();
111void struct_assign_test(void);
112void cast_test(void);
113void bitfield_test(void);
114void c99_bool_test(void);
115void float_test(void);
116void longlong_test(void);
117void manyarg_test(void);
118void stdarg_test(void);
119void whitespace_test(void);
120void relocation_test(void);
121void old_style_function(void);
122void alloca_test(void);
123void c99_vla_test(int size1, int size2);
124void sizeof_test(void);
125void typeof_test(void);
126void local_label_test(void);
127void statement_expr_test(void);
128void asm_test(void);
129void builtin_test(void);
130void weak_test(void);
131void global_data_test(void);
132void cmp_comparison_test(void);
133void math_cmp_test(void);
134void callsave_test(void);
135void builtin_frame_address_test(void);
136void attrib_test(void);
137void bounds_check1_test(void);
138
139int fib(int n);79int fib(int n);
140void num(int n);80void num(int n);
141void forward_ref(void);81void forward_ref(void);
@@ -146,8 +86,6 @@ int isid(int c); · 4 unmodified lines
146void funny_line_continuation (int, ..\86void funny_line_continuation (int, ..\
147. );87. );
148 88
149char via_volatile (char);
150
151#define A 289#define A 2
152#define N 1234 + A90#define N 1234 + A
153#define pf printf91#define pf printf
@@ -207,74 +145,8 @@ int qq(int x) · 53 unmodified lines
207#define wq_spin_lock spin_lock145#define wq_spin_lock spin_lock
208#define TEST2() wq_spin_lock(a)146#define TEST2() wq_spin_lock(a)
209 147
210#define UINT_MAX ((unsigned) -1)
211
212void intdiv_test(void)
213{
214 printf("18/21=%u\n", 18/21);
215 printf("18%%21=%u\n", 18%21);
216 printf("41/21=%u\n", 41/21);
217 printf("41%%21=%u\n", 41%21);
218 printf("42/21=%u\n", 42/21);
219 printf("42%%21=%u\n", 42%21);
220 printf("43/21=%u\n", 43/21);
221 printf("43%%21=%u\n", 43%21);
222 printf("126/21=%u\n", 126/21);
223 printf("126%%21=%u\n", 126%21);
224 printf("131/21=%u\n", 131/21);
225 printf("131%%21=%u\n", 131%21);
226 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
227 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
228
229 printf("18/-21=%u\n", 18/-21);
230 printf("18%%-21=%u\n", 18%-21);
231 printf("41/-21=%u\n", 41/-21);
232 printf("41%%-21=%u\n", 41%-21);
233 printf("42/-21=%u\n", 42/-21);
234 printf("42%%-21=%u\n", 42%-21);
235 printf("43/-21=%u\n", 43/-21);
236 printf("43%%-21=%u\n", 43%-21);
237 printf("126/-21=%u\n", 126/-21);
238 printf("126%%-21=%u\n", 126%-21);
239 printf("131/-21=%u\n", 131/-21);
240 printf("131%%-21=%u\n", 131%-21);
241 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
242 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
243
244 printf("-18/21=%u\n", -18/21);
245 printf("-18%%21=%u\n", -18%21);
246 printf("-41/21=%u\n", -41/21);
247 printf("-41%%21=%u\n", -41%21);
248 printf("-42/21=%u\n", -42/21);
249 printf("-42%%21=%u\n", -42%21);
250 printf("-43/21=%u\n", -43/21);
251 printf("-43%%21=%u\n", -43%21);
252 printf("-126/21=%u\n", -126/21);
253 printf("-126%%21=%u\n", -126%21);
254 printf("-131/21=%u\n", -131/21);
255 printf("-131%%21=%u\n", -131%21);
256 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
257 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
258
259 printf("-18/-21=%u\n", -18/-21);
260 printf("-18%%-21=%u\n", -18%-21);
261 printf("-41/-21=%u\n", -41/-21);
262 printf("-41%%-21=%u\n", -41%-21);
263 printf("-42/-21=%u\n", -42/-21);
264 printf("-42%%-21=%u\n", -42%-21);
265 printf("-43/-21=%u\n", -43/-21);
266 printf("-43%%-21=%u\n", -43%-21);
267 printf("-126/-21=%u\n", -126/-21);
268 printf("-126%%-21=%u\n", -126%-21);
269 printf("-131/-21=%u\n", -131/-21);
270 printf("-131%%-21=%u\n", -131%-21);
271 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
272 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
273}
274
275void macro_test(void)148void macro_test(void)
276{149{
277 printf("macro:\n");
278 pf("N=%d\n", N);150 pf("N=%d\n", N);
279 printf("aaa=%d\n", AAA);151 printf("aaa=%d\n", AAA);
280 152
@@ -349,22 +221,6 @@ void macro_test(void) · 68 unmodified lines
349 221
350 MACRO_NOARGS();222 MACRO_NOARGS();
351 223
352#ifdef __LINE__
353 printf("__LINE__ defined\n");
354#endif
355
356 printf("__LINE__=%d __FILE__=%s\n",
357 __LINE__, __FILE__);
358#if 0
359#line 200
360 printf("__LINE__=%d __FILE__=%s\n",
361 __LINE__, __FILE__);
362#line 203 "test"
363 printf("__LINE__=%d __FILE__=%s\n",
364 __LINE__, __FILE__);
365#line 227 "tcctest.c"
366#endif
367
368 /* not strictly preprocessor, but we test it there */224 /* not strictly preprocessor, but we test it there */
369#ifdef C99_MACROS225#ifdef C99_MACROS
370 printf("__func__ = %s\n", __func__);226 printf("__func__ = %s\n", __func__);
@@ -677,7 +533,7 @@ void goto_test() · 306 unmodified lines
677 int i;533 int i;
678 static void *label_table[3] = { &&label1, &&label2, &&label3 };534 static void *label_table[3] = { &&label1, &&label2, &&label3 };
679 535
680 printf("goto:\n");536 printf("\ngoto:\n");
681 i = 0;537 i = 0;
682 /* This needs to parse as label, not as start of decl. */538 /* This needs to parse as label, not as start of decl. */
683 typedef_and_label x;539 typedef_and_label x;
@@ -745,8 +601,7 @@ void enum_test() · 61 unmodified lines
745 /* The following should give no warning */601 /* The following should give no warning */
746 unsigned *p = &b1;602 unsigned *p = &b1;
747 struct S_enum s = {E7};603 struct S_enum s = {E7};
748 printf("enum: %d\n", s.e);604 printf("%d %d %d %d %d %d %d\n", s.e,
749 printf("enum:\n%d %d %d %d %d %d\n",
750 E0, E1, E2, E3, E4, E5);605 E0, E1, E2, E3, E4, E5);
751 b1 = 1;606 b1 = 1;
752 printf("b1=%d\n", b1);607 printf("b1=%d\n", b1);
@@ -775,7 +630,6 @@ void typedef_test() · 22 unmodified lines
775 630
776 a = &b;631 a = &b;
777 *a = 1234;632 *a = 1234;
778 printf("typedef:\n");
779 printf("a=%d\n", *a);633 printf("a=%d\n", *a);
780 mytype2 = 2;634 mytype2 = 2;
781 printf("mytype2=%d\n", mytype2);635 printf("mytype2=%d\n", mytype2);
@@ -783,7 +637,6 @@ void typedef_test() · 1 unmodified line
783 637
784void forward_test()638void forward_test()
785{639{
786 printf("forward:\n");
787 forward_ref();640 forward_ref();
788 forward_ref();641 forward_ref();
789}642}
@@ -821,69 +674,6 @@ struct empty_mem { · 31 unmodified lines
821 int x;674 int x;
822};675};
823 676
824int main(int argc, char **argv)
825{
826 string_test();
827 expr_test();
828 macro_test();
829 recursive_macro_test();
830 scope_test();
831 scope_test2();
832 forward_test();
833 funcptr_test();
834 if_test();
835 loop_test();
836 switch_test();
837 goto_test();
838 enum_test();
839 typedef_test();
840 struct_test();
841 array_test();
842 expr_ptr_test();
843 bool_test();
844 optimize_out();
845 expr2_test();
846 constant_expr_test();
847 expr_cmp_test();
848 char_short_test();
849 init_test();
850 compound_literal_test();
851 kr_test();
852 struct_assign_test();
853 cast_test();
854 bitfield_test();
855 c99_bool_test();
856 float_test();
857 longlong_test();
858 manyarg_test();
859 stdarg_test();
860 whitespace_test();
861 relocation_test();
862 old_style_function();
863 alloca_test();
864 c99_vla_test(5, 2);
865 sizeof_test();
866 typeof_test();
867 statement_expr_test();
868 local_label_test();
869 asm_test();
870 builtin_test();
871#ifndef _WIN32
872 weak_test();
873#endif
874 global_data_test();
875 cmp_comparison_test();
876 math_cmp_test();
877 callsave_test();
878 builtin_frame_address_test();
879 intdiv_test();
880 if (via_volatile (42) != 42)
881 printf ("via_volatile broken\n");
882 attrib_test();
883 bounds_check1_test();
884 return 0;
885}
886
887int tab[3];677int tab[3];
888int tab2[3][2];678int tab2[3][2];
889 679
@@ -896,7 +686,6 @@ void f1(g) · 6 unmodified lines
896 686
897void scope_test()687void scope_test()
898{688{
899 printf("scope:\n");
900 g = 2;689 g = 2;
901 f1(1);690 f1(1);
902 printf("g2=%d\n", g);691 printf("g2=%d\n", g);
@@ -915,7 +704,7 @@ void scope_test() · 12 unmodified lines
915 704
916int st2_i;705int st2_i;
917int *st2_p = &st2_i;706int *st2_p = &st2_i;
918void scope_test2()707void scope2_test()
919{708{
920 char a[50];709 char a[50];
921 st2_i = 42;710 st2_i = 42;
@@ -941,7 +730,6 @@ void array_test() · 19 unmodified lines
941{730{
942 int i, j, a[4];731 int i, j, a[4];
943 732
944 printf("array:\n");
945 printf("sizeof(a) = %d\n", sizeof(a));733 printf("sizeof(a) = %d\n", sizeof(a));
946 printf("sizeof(\"a\") = %d\n", sizeof("a"));734 printf("sizeof(\"a\") = %d\n", sizeof("a"));
947#ifdef C99_MACROS735#ifdef C99_MACROS
@@ -1046,7 +834,6 @@ void expr2_test() · 98 unmodified lines
1046{834{
1047 int a, b;835 int a, b;
1048 836
1049 printf("expr2:\n");
1050 vstack_ptr = vstack;837 vstack_ptr = vstack;
1051 vpush(1432432, 2);838 vpush(1432432, 2);
1052 vstack_ptr[-2] &= ~0xffffff80;839 vstack_ptr[-2] &= ~0xffffff80;
@@ -1059,7 +846,6 @@ int const_len_ar[sizeof(1/0)]; /* div-by-zero, but in unevaluated context */ · 6 unmodified lines
1059void constant_expr_test()846void constant_expr_test()
1060{847{
1061 int a;848 int a;
1062 printf("constant_expr:\n");
1063 a = 3;849 a = 3;
1064 printf("%d\n", a * 16);850 printf("%d\n", a * 16);
1065 printf("%d\n", a * 1);851 printf("%d\n", a * 1);
@@ -1074,7 +860,6 @@ void expr_ptr_test() · 8 unmodified lines
1074 int *p, *q;860 int *p, *q;
1075 int i = -1;861 int i = -1;
1076 862
1077 printf("expr_ptr:\n");
1078 p = tab4;863 p = tab4;
1079 q = tab4 + 10;864 q = tab4 + 10;
1080 printf("diff=%d\n", q - p);865 printf("diff=%d\n", q - p);
@@ -1130,7 +915,6 @@ void expr_ptr_test() · 49 unmodified lines
1130void expr_cmp_test()915void expr_cmp_test()
1131{916{
1132 int a, b;917 int a, b;
1133 printf("constant_expr:\n");
1134 a = -1;918 a = -1;
1135 b = 1;919 b = 1;
1136 printf("%d\n", a == a);920 printf("%d\n", a == a);
@@ -1250,7 +1034,6 @@ void struct_test() · 113 unmodified lines
1250 union union2 u;1034 union union2 u;
1251 struct Large ls;1035 struct Large ls;
1252 1036
1253 printf("struct:\n");
1254 printf("sizes: %d %d %d %d\n",1037 printf("sizes: %d %d %d %d\n",
1255 sizeof(struct struct1),1038 sizeof(struct struct1),
1256 sizeof(struct struct2),1039 sizeof(struct struct2),
@@ -1326,8 +1109,6 @@ void char_short_test() · 69 unmodified lines
1326 signed char var3;1109 signed char var3;
1327 long long var4;1110 long long var4;
1328 1111
1329 printf("char_short:\n");
1330
1331 var1 = 0x01020304;1112 var1 = 0x01020304;
1332 var2 = 0xfffefdfc;1113 var2 = 0xfffefdfc;
1333 printf("s8=%d %d\n", 1114 printf("s8=%d %d\n",
@@ -1495,7 +1276,7 @@ static inline void refer_to_undefined(void) · 161 unmodified lines
1495 undefined_function();1276 undefined_function();
1496}1277}
1497 1278
1498void optimize_out(void)1279void optimize_out_test(void)
1499{1280{
1500 int i = 0 ? undefined_function() : defined_function();1281 int i = 0 ? undefined_function() : defined_function();
1501 printf ("oo:%d\n", i);1282 printf ("oo:%d\n", i);
@@ -1636,8 +1417,6 @@ void compound_literal_test(void) · 134 unmodified lines
1636 int *p, i;1417 int *p, i;
1637 char *q, *q3;1418 char *q, *q3;
1638 1419
1639 printf("compound_test:\n");
1640
1641 p = (int []){1, 2, 3};1420 p = (int []){1, 2, 3};
1642 for(i=0;i<3;i++)1421 for(i=0;i<3;i++)
1643 printf(" %d", p[i]);1422 printf(" %d", p[i]);
@@ -1689,7 +1468,6 @@ int kr_func2(a, b) · 45 unmodified lines
1689 1468
1690kr_test()1469kr_test()
1691{1470{
1692 printf("kr_test:\n");
1693 printf("func1=%d\n", kr_func1(3, 4));1471 printf("func1=%d\n", kr_func1(3, 4));
1694 printf("func2=%d\n", kr_func2(3, 4));1472 printf("func2=%d\n", kr_func2(3, 4));
1695 return 0;1473 return 0;
@@ -1745,8 +1523,6 @@ void struct_assign_test(void) · 49 unmodified lines
1745 ps = &s;1523 ps = &s;
1746 ps->i = 4;1524 ps->i = 4;
1747#if 01525#if 0
1748 printf("struct_assign_test:\n");
1749
1750 s.lsta1.f1 = 1;1526 s.lsta1.f1 = 1;
1751 s.lsta1.f2 = 2;1527 s.lsta1.f2 = 2;
1752 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);1528 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
@@ -1792,7 +1568,6 @@ void cast_test() · 39 unmodified lines
1792 unsigned long ul = 0x80000000UL;1568 unsigned long ul = 0x80000000UL;
1793 p -= 0x700000000042;1569 p -= 0x700000000042;
1794 1570
1795 printf("cast_test:\n");
1796 a = 0xfffff;1571 a = 0xfffff;
1797 cast1(a, a, a, a);1572 cast1(a, a, a, a);
1798 a = 0xffffe;1573 a = 0xffffe;
@@ -1833,7 +1608,7 @@ void cast_test() · 34 unmodified lines
1833 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));1608 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1834 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));1609 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1835 1610
1836#if CC_NAME != CC_clang /* doesn't like non-portable conversions */1611#if CC_NAME != CC_clang /* clang doesn't support non-portable conversions */
1837 /* from pointer to integer types */1612 /* from pointer to integer types */
1838 printf("%d %d %ld %ld %lld %lld\n",1613 printf("%d %d %ld %ld %lld %lld\n",
1839 (int)p, (unsigned int)p,1614 (int)p, (unsigned int)p,
@@ -1972,8 +1747,6 @@ void init_test(void) · 132 unmodified lines
1972 /* Addresses on non-weak symbols are non-zero, but not the access itself */1747 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1973 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };1748 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1974 1749
1975 printf("init_test:\n");
1976
1977 printf("sinit1=%d\n", sinit1);1750 printf("sinit1=%d\n", sinit1);
1978 printf("sinit2=%d\n", sinit2);1751 printf("sinit2=%d\n", sinit2);
1979 printf("sinit3=%d %d %d %d\n", 1752 printf("sinit3=%d %d %d %d\n",
@@ -2194,7 +1967,6 @@ void c99_bool_test(void) · 214 unmodified lines
2194 int a;1967 int a;
2195 _Bool b, b2;1968 _Bool b, b2;
2196 1969
2197 printf("bool_test:\n");
2198 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));1970 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
2199 a = 3;1971 a = 3;
2200 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);1972 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
@@ -2222,7 +1994,6 @@ void bitfield_test(void) · 21 unmodified lines
2222 int f4 : 7;1994 int f4 : 7;
2223 unsigned int f5 : 7;1995 unsigned int f5 : 7;
2224 } st1;1996 } st1;
2225 printf("bitfield_test:");
2226 printf("sizeof(st1) = %d\n", sizeof(st1));1997 printf("sizeof(st1) = %d\n", sizeof(st1));
2227 1998
2228 st1.f1 = 3;1999 st1.f1 = 3;
@@ -2442,7 +2213,6 @@ void float_test(void) · 213 unmodified lines
2442 static double inf1 = 1.0/0.0;2213 static double inf1 = 1.0/0.0;
2443 static double inf2 = 1e5000;2214 static double inf2 = 1e5000;
2444 2215
2445 printf("float_test:\n");
2446 printf("sizeof(float) = %d\n", sizeof(float));2216 printf("sizeof(float) = %d\n", sizeof(float));
2447 printf("sizeof(double) = %d\n", sizeof(double));2217 printf("sizeof(double) = %d\n", sizeof(double));
2448 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));2218 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
@@ -2490,7 +2260,6 @@ void funcptr_test() · 41 unmodified lines
2490 } st1;2260 } st1;
2491 long diff;2261 long diff;
2492 2262
2493 printf("funcptr:\n");
2494 func = &num;2263 func = &num;
2495 (*func)(12345);2264 (*func)(12345);
2496 func = num;2265 func = num;
@@ -2648,7 +2417,6 @@ void longlong_test(void) · 151 unmodified lines
2648 long long a, b, c;2417 long long a, b, c;
2649 int ia;2418 int ia;
2650 unsigned int ua;2419 unsigned int ua;
2651 printf("longlong_test:\n");
2652 printf("sizeof(long long) = %d\n", sizeof(long long));2420 printf("sizeof(long long) = %d\n", sizeof(long long));
2653 ia = -1;2421 ia = -1;
2654 ua = -2;2422 ua = -2;
@@ -2728,7 +2496,6 @@ void longlong_test(void) · 73 unmodified lines
2728void manyarg_test(void)2496void manyarg_test(void)
2729{2497{
2730 LONG_DOUBLE ld = 1234567891234LL;2498 LONG_DOUBLE ld = 1234567891234LL;
2731 printf("manyarg_test:\n");
2732 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",2499 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2733 1, 2, 3, 4, 5, 6, 7, 8,2500 1, 2, 3, 4, 5, 6, 7, 8,
2734 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);2501 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
@@ -2912,38 +2679,6 @@ void stdarg_test(void) · 177 unmodified lines
2912 stdarg_syntax(1, 17);2679 stdarg_syntax(1, 17);
2913}2680}
2914 2681
2915void whitespace_test(void)
2916{
2917 char *str;
2918
2919 #if 1
2920 pri\
2921ntf("whitspace:\n");
2922#endif
2923 pf("N=%d\n", 2);
2924
2925#ifdef CORRECT_CR_HANDLING
2926 pri\
2927ntf("aaa=%d\n", 3);
2928#endif
2929
2930 pri\
2931\
2932ntf("min=%d\n", 4);
2933
2934#ifdef ACCEPT_CR_IN_STRINGS
2935 printf("len1=%d\n", strlen("
2936"));
2937#ifdef CORRECT_CR_HANDLING
2938 str = "
2939";
2940 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2941#endif
2942 printf("len1=%d\n", strlen(" a
2943"));
2944#endif /* ACCEPT_CR_IN_STRINGS */
2945}
2946
2947int reltab[3] = { 1, 2, 3 };2682int reltab[3] = { 1, 2, 3 };
2948 2683
2949int *rel1 = &reltab[1];2684int *rel1 = &reltab[1];
@@ -3000,7 +2735,7 @@ int cmpfn(); · 50 unmodified lines
3000 printf("cmpfn=%lx\n", (long)cmpfn);2735 printf("cmpfn=%lx\n", (long)cmpfn);
3001}2736}
3002 2737
3003void old_style_function(void)2738void old_style_function_test(void)
3004{2739{
3005 old_style_f((void *)1, 2, 3.0);2740 old_style_f((void *)1, 2, 3.0);
3006 decl_func1(NULL);2741 decl_func1(NULL);
@@ -3027,7 +2762,7 @@ void *bounds_checking_is_enabled() · 20 unmodified lines
3027 2762
3028typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];2763typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
3029 2764
3030void c99_vla_test(int size1, int size2)2765void c99_vla_test_1(int size1, int size2)
3031{2766{
3032#if defined __i386__ || defined __x86_64__2767#if defined __i386__ || defined __x86_64__
3033 int size = size1 * size2;2768 int size = size1 * size2;
@@ -3078,6 +2813,12 @@ void c99_vla_test(int size1, int size2) · 44 unmodified lines
3078#endif2813#endif
3079}2814}
3080 2815
2816void c99_vla_test(void)
2817{
2818 c99_vla_test_1(5, 2);
2819}
2820
2821
3081void sizeof_test(void)2822void sizeof_test(void)
3082{2823{
3083 int a;2824 int a;
@@ -3750,8 +3491,6 @@ void asm_test(void) · 666 unmodified lines
3750#endif3491#endif
3751 register int regvar asm("%esi");3492 register int regvar asm("%esi");
3752 3493
3753 printf("inline asm:\n");
3754
3755 // parse 0x1E-1 as 3 tokens in asm mode3494 // parse 0x1E-1 as 3 tokens in asm mode
3756 asm volatile ("mov $0x1E-1,%eax");3495 asm volatile ("mov $0x1E-1,%eax");
3757 3496
@@ -3902,7 +3641,9 @@ void builtin_test(void) · 144 unmodified lines
3902 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));3641 //printf("bera: %p\n", __builtin_extract_return_addr((void*)43));
3903}3642}
3904 3643
3905#ifndef _WIN323644#ifdef _WIN32
3645void weak_test(void) {}
3646#else
3906extern int __attribute__((weak)) weak_f1(void);3647extern int __attribute__((weak)) weak_f1(void);
3907extern int __attribute__((weak)) weak_f2(void);3648extern int __attribute__((weak)) weak_f2(void);
3908extern int weak_f3(void);3649extern int weak_f3(void);
@@ -4170,6 +3911,14 @@ char via_volatile (char i) · 261 unmodified lines
4170 return vi;3911 return vi;
4171}3912}
4172 3913
3914void volatile_test(void)
3915{
3916 if (via_volatile (42) != 42)
3917 printf (" broken\n");
3918 else
3919 printf (" ok\n");
3920}
3921
4173struct __attribute__((__packed__)) Spacked {3922struct __attribute__((__packed__)) Spacked {
4174 char a;3923 char a;
4175 short b;3924 short b;
@@ -4250,3 +3999,122 @@ void bounds_check1_test (void) · 74 unmodified lines
4250 pv(x);3999 pv(x);
4251 pv(y);4000 pv(y);
4252}4001}
4002
4003/* gcc 2.95.3 does not handle correctly CR in strings or after strays */
4004#define CORRECT_CR_HANDLING
4005
4006/* deprecated and no longer supported in gcc 3.3 */
4007#ifdef __TINYC__
4008# define ACCEPT_CR_IN_STRINGS
4009#endif
4010
4011/* keep this as the last test because GCC messes up line-numbers
4012 with the ^L^K^M characters below */
4013void whitespace_test(void)
4014{
4015 char *str;
4016
4017 #if 1
4018 pri\
4019ntf("whitspace:\n");
4020#endif
4021 pf("N=%d\n", 2);
4022
4023#ifdef CORRECT_CR_HANDLING
4024 pri\
4025ntf("aaa=%d\n", 3);
4026#endif
4027
4028 pri\
4029\
4030ntf("min=%d\n", 4);
4031
4032#ifdef ACCEPT_CR_IN_STRINGS
4033 printf("len1=%d\n", strlen("
4034"));
4035#ifdef CORRECT_CR_HANDLING
4036 str = "
4037";
4038 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
4039#endif
4040 printf("len1=%d\n", strlen(" a
4041"));
4042#else
4043 printf("len1=1\nlen1=1 str[0]=10\nlen1=3\n");
4044#endif /* ACCEPT_CR_IN_STRINGS */
4045
4046#ifdef __LINE__
4047 printf("__LINE__ defined\n");
4048#endif
4049
4050#if 0
4051 /* wrong with GCC */
4052 printf("__LINE__=%d __FILE__=%s\n", __LINE__, __FILE__);
4053#line 1111
4054 printf("__LINE__=%d __FILE__=%s\n", __LINE__, __FILE__);
4055#line 2222 "test"
4056 printf("__LINE__=%d __FILE__=%s\n", __LINE__, __FILE__);
4057#endif
4058}
4059
4060#define RUN(test) puts("---- " #test " ----"), test(), puts("")
4061
4062int main(int argc, char **argv)
4063{
4064 RUN(whitespace_test);
4065 RUN(macro_test);
4066 RUN(recursive_macro_test);
4067 RUN(string_test);
4068 RUN(expr_test);
4069 RUN(scope_test);
4070 RUN(scope2_test);
4071 RUN(forward_test);
4072 RUN(funcptr_test);
4073 RUN(if_test);
4074 RUN(loop_test);
4075 RUN(switch_test);
4076 RUN(goto_test);
4077 RUN(enum_test);
4078 RUN(typedef_test);
4079 RUN(struct_test);
4080 RUN(array_test);
4081 RUN(expr_ptr_test);
4082 RUN(bool_test);
4083 RUN(optimize_out_test);
4084 RUN(expr2_test);
4085 RUN(constant_expr_test);
4086 RUN(expr_cmp_test);
4087 RUN(char_short_test);
4088 RUN(init_test);
4089 RUN(compound_literal_test);
4090 RUN(kr_test);
4091 RUN(struct_assign_test);
4092 RUN(cast_test);
4093 RUN(bitfield_test);
4094 RUN(c99_bool_test);
4095 RUN(float_test);
4096 RUN(longlong_test);
4097 RUN(manyarg_test);
4098 RUN(stdarg_test);
4099 RUN(relocation_test);
4100 RUN(old_style_function_test);
4101 RUN(alloca_test);
4102 RUN(c99_vla_test);
4103 RUN(sizeof_test);
4104 RUN(typeof_test);
4105 RUN(statement_expr_test);
4106 RUN(local_label_test);
4107 RUN(asm_test);
4108 RUN(builtin_test);
4109 RUN(weak_test);
4110 RUN(global_data_test);
4111 RUN(cmp_comparison_test);
4112 RUN(math_cmp_test);
4113 RUN(callsave_test);
4114 RUN(builtin_frame_address_test);
4115 RUN(volatile_test);
4116 RUN(attrib_test);
4117 RUN(bounds_check1_test);
4118
4119 return 0;
4120}
tests/tests2/61_integers.c+70−0
@@ -0,0 +1,70 @@
1#include <stdio.h>
2
3/* This was first introduced to test the ARM port */
4
5#define UINT_MAX ((unsigned) -1)
6
7int main()
8{
9 printf("18/21=%u\n", 18/21);
10 printf("18%%21=%u\n", 18%21);
11 printf("41/21=%u\n", 41/21);
12 printf("41%%21=%u\n", 41%21);
13 printf("42/21=%u\n", 42/21);
14 printf("42%%21=%u\n", 42%21);
15 printf("43/21=%u\n", 43/21);
16 printf("43%%21=%u\n", 43%21);
17 printf("126/21=%u\n", 126/21);
18 printf("126%%21=%u\n", 126%21);
19 printf("131/21=%u\n", 131/21);
20 printf("131%%21=%u\n", 131%21);
21 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
22 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
23
24 printf("18/-21=%u\n", 18/-21);
25 printf("18%%-21=%u\n", 18%-21);
26 printf("41/-21=%u\n", 41/-21);
27 printf("41%%-21=%u\n", 41%-21);
28 printf("42/-21=%u\n", 42/-21);
29 printf("42%%-21=%u\n", 42%-21);
30 printf("43/-21=%u\n", 43/-21);
31 printf("43%%-21=%u\n", 43%-21);
32 printf("126/-21=%u\n", 126/-21);
33 printf("126%%-21=%u\n", 126%-21);
34 printf("131/-21=%u\n", 131/-21);
35 printf("131%%-21=%u\n", 131%-21);
36 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
37 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
38
39 printf("-18/21=%u\n", -18/21);
40 printf("-18%%21=%u\n", -18%21);
41 printf("-41/21=%u\n", -41/21);
42 printf("-41%%21=%u\n", -41%21);
43 printf("-42/21=%u\n", -42/21);
44 printf("-42%%21=%u\n", -42%21);
45 printf("-43/21=%u\n", -43/21);
46 printf("-43%%21=%u\n", -43%21);
47 printf("-126/21=%u\n", -126/21);
48 printf("-126%%21=%u\n", -126%21);
49 printf("-131/21=%u\n", -131/21);
50 printf("-131%%21=%u\n", -131%21);
51 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
52 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
53
54 printf("-18/-21=%u\n", -18/-21);
55 printf("-18%%-21=%u\n", -18%-21);
56 printf("-41/-21=%u\n", -41/-21);
57 printf("-41%%-21=%u\n", -41%-21);
58 printf("-42/-21=%u\n", -42/-21);
59 printf("-42%%-21=%u\n", -42%-21);
60 printf("-43/-21=%u\n", -43/-21);
61 printf("-43%%-21=%u\n", -43%-21);
62 printf("-126/-21=%u\n", -126/-21);
63 printf("-126%%-21=%u\n", -126%-21);
64 printf("-131/-21=%u\n", -131/-21);
65 printf("-131%%-21=%u\n", -131%-21);
66 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
67 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
68
69 return 0;
70}
tests/tests2/61_integers.expect+56−0
@@ -0,0 +1,56 @@
118/21=0
218%21=18
341/21=1
441%21=20
542/21=2
642%21=0
743/21=2
843%21=1
9126/21=6
10126%21=0
11131/21=6
12131%21=5
13(UINT_MAX/2+3)/2=1073741825
14(UINT_MAX/2+3)%2=0
1518/-21=0
1618%-21=18
1741/-21=4294967295
1841%-21=20
1942/-21=4294967294
2042%-21=0
2143/-21=4294967294
2243%-21=1
23126/-21=4294967290
24126%-21=0
25131/-21=4294967290
26131%-21=5
27(UINT_MAX/2+3)/-2=0
28(UINT_MAX/2+3)%-2=2147483650
29-18/21=0
30-18%21=4294967278
31-41/21=4294967295
32-41%21=4294967276
33-42/21=4294967294
34-42%21=0
35-43/21=4294967294
36-43%21=4294967295
37-126/21=4294967290
38-126%21=0
39-131/21=4294967290
40-131%21=4294967291
41-(UINT_MAX/2+3)/2=1073741823
42-(UINT_MAX/2+3)%2=0
43-18/-21=0
44-18%-21=4294967278
45-41/-21=1
46-41%-21=4294967276
47-42/-21=2
48-42%-21=0
49-43/-21=2
50-43%-21=4294967295
51-126/-21=6
52-126%-21=0
53-131/-21=6
54-131%-21=4294967291
55-(UINT_MAX/2+3)/-2=0
56-(UINT_MAX/2+3)%-2=2147483646
x86_64-gen.c+1−13
@@ -149,7 +149,7 @@ static int func_ret_sub; · 148 unmodified lines
149#if defined(CONFIG_TCC_BCHECK)149#if defined(CONFIG_TCC_BCHECK)
150static addr_t func_bound_offset;150static addr_t func_bound_offset;
151static unsigned long func_bound_ind;151static unsigned long func_bound_ind;
152static int func_bound_add_epilog;152ST_DATA int func_bound_add_epilog;
153#endif153#endif
154 154
155#ifdef TCC_TARGET_PE155#ifdef TCC_TARGET_PE
@@ -639,18 +639,6 @@ static void gcall_or_jmp(int is_jmp) · 483 unmodified lines
639 greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));639 greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));
640#endif640#endif
641 oad(0xe8 + is_jmp, 0); /* call/jmp im */641 oad(0xe8 + is_jmp, 0); /* call/jmp im */
642#ifdef CONFIG_TCC_BCHECK
643 if (tcc_state->do_bounds_check &&
644 (vtop->sym->v == TOK_alloca ||
645 vtop->sym->v == TOK_setjmp ||
646 vtop->sym->v == TOK__setjmp
647#ifndef TCC_TARGET_PE
648 || vtop->sym->v == TOK_sigsetjmp
649 || vtop->sym->v == TOK___sigsetjmp
650#endif
651 ))
652 func_bound_add_epilog = 1;
653#endif
654 } else {642 } else {
655 /* otherwise, indirect call */643 /* otherwise, indirect call */
656 r = TREG_R11;644 r = TREG_R11;

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