History

Revert "Fix wrong handling of strings..." (almost)

See commit e588b65390bcfadabfb00c32ae0bfee12a8a8002. Was not "wrong" really, just different. But appears to be outdated. Now disabled by default (top of tccpp.c: ACCEPT_LF_IN_STRINGS) Also, in skipped code, just warn. Also: cleanup "Optimize small structure copying on x86_64" (commit 3715f1d7ee302c220d36f545107bbf808fd979d9) - remove some copy&paste coding (tccgen.c) - RSI/RDI need to be preserved on windows - simply don't use under bcheck (this is tinycc)

grischka committed 4 years ago

Commit af1abf11 parent e588b658 files · +98 −129Browse files

Unified · Split

8 files
  1. lib/bcheck.c +0 −13
  2. tcc.h +0 −3
  3. tccgen.c +27 −53
  4. tccpp.c +16 −2
  5. tcctok.h +0 −1
  6. tests/tcctest.c +25 −14
  7. tests/tests2/Makefile +1 −2
  8. x86_64-gen.c +29 −41
lib/bcheck.c+0−13
@@ -294,7 +294,6 @@ DLL_EXPORT int __bound_strncmp(const char *s1, const char *s2, size_t n); · 293 unmodified lines
294294 DLL_EXPORT char *__bound_strcat(char *dest, const char *src);
295295 DLL_EXPORT char *__bound_strchr(const char *string, int ch);
296296 DLL_EXPORT char *__bound_strdup(const char *s);
297−DLL_EXPORT void __bound_struct_copy(void *dst,void *src,size_t size);
298297
299298 #if defined(__arm__) && defined(__ARM_EABI__)
300299 DLL_EXPORT void *__bound___aeabi_memcpy(void *dst, const void *src, size_t size);
@@ -427,7 +426,6 @@ static unsigned long long bound_strncmp_count; · 126 unmodified lines
427426 static unsigned long long bound_strcat_count;
428427 static unsigned long long bound_strchr_count;
429428 static unsigned long long bound_strdup_count;
430−static unsigned long long bound_struct_copy_count;
431429 static unsigned long long bound_not_found;
432430 #define INCR_COUNT(x) ++x
433431 #else
@@ -1262,7 +1260,6 @@ void __attribute__((destructor)) __bound_exit(void) · 828 unmodified lines
12621260 fprintf (stderr, "bound_strcat_count %llu\n", bound_strcat_count);
12631261 fprintf (stderr, "bound_strchr_count %llu\n", bound_strchr_count);
12641262 fprintf (stderr, "bound_strdup_count %llu\n", bound_strdup_count);
1265− fprintf (stderr, "bound_struct_copy_count %llu\n", bound_struct_copy_count);
12661263 fprintf (stderr, "bound_not_found %llu\n", bound_not_found);
12671264 #endif
12681265 #if BOUND_STATISTIC_SPLAY
@@ -1786,16 +1783,6 @@ void *__bound_memset(void *s, int c, size_t n) · 517 unmodified lines
17861783 return memset(s, c, n);
17871784 }
17881785
1789−void __bound_struct_copy(void *dst,void *src,size_t size)
1790−{
1791− dprintf(stderr, "Copy struct from %p to %p,size %lx\n",
1792− src,dst,size);
1793− INCR_COUNT(bound_struct_copy_count);
1794− __bound_check(dst,size,"struct copy destination");
1795− __bound_check(src,size,"struct copy source");
1796− return;
1797−}
1798−
17991786 #if defined(__arm__) && defined(__ARM_EABI__)
18001787 void *__bound___aeabi_memcpy(void *dest, const void *src, size_t n)
18011788 {
tcc.h+0−3
@@ -1644,8 +1644,6 @@ ST_FUNC void gen_increment_tcov (SValue *sv); · 1643 unmodified lines
16441644
16451645 /* ------------ x86_64-gen.c ------------ */
16461646 #ifdef TCC_TARGET_X86_64
1647−#define TCC_TARGET_NATIVE_STRUCT_COPY
1648−ST_FUNC void gen_struct_copy(int size);
16491647 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
16501648 ST_FUNC void gen_opl(int op);
16511649 #ifdef TCC_TARGET_PE
@@ -1691,7 +1689,6 @@ ST_FUNC void gen_increment_tcov (SValue *sv); · 39 unmodified lines
16911689 #endif
16921690
16931691 /* ------------ tcccoff.c ------------ */
1694−
16951692 #ifdef TCC_TARGET_COFF
16961693 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
16971694 ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
tccgen.c+27−53
@@ -3468,54 +3468,42 @@ ST_FUNC void vstore(void) · 3467 unmodified lines
34683468 ft = vtop[-1].type.t;
34693469 sbt = vtop->type.t & VT_BTYPE;
34703470 dbt = ft & VT_BTYPE;
3471−
34723471 verify_assign_cast(&vtop[-1].type);
34733472
34743473 if (sbt == VT_STRUCT) {
34753474 /* if structure, only generate pointer */
34763475 /* structure assignment : generate memcpy */
3477− size = type_size(&vtop->type, &align);
3478−
3479−#ifdef TCC_TARGET_NATIVE_STRUCT_COPY
3480− if (size <= (PTR_SIZE << 4)) {
3481− vswap();
3476+ size = type_size(&vtop->type, &align);
3477+ /* destination, keep on stack() as result */
3478+ vpushv(vtop - 1);
34823479 #ifdef CONFIG_TCC_BCHECK
3483− if (vtop->r & VT_MUSTBOUND)
3484− gbound();
3480+ if (vtop->r & VT_MUSTBOUND)
3481+ gbound(); /* check would be wrong after gaddrof() */
34853482 #endif
3486− vtop->type.t = VT_PTR;
3487− gaddrof();
3488−
3489− vpushv(vtop - 1);
3490−#ifdef CONFIG_TCC_BCHECK
3491− if (vtop->r & VT_MUSTBOUND)
3492− gbound();
3493−#endif
3494− vtop->type.t = VT_PTR;
3495− gaddrof(); /* src dest src */
3483+ vtop->type.t = VT_PTR;
3484+ gaddrof();
3485+ /* source */
3486+ vswap();
34963487 #ifdef CONFIG_TCC_BCHECK
3497− if (tcc_state->do_bounds_check) {
3498− vpush_helper_func(TOK___bound_struct_copy);
3499− vpushv(vtop - 2);
3500− vpushv(vtop - 2);
3501− vpushi(size);
3502− gfunc_call(3);
3503− }
3488+ if (vtop->r & VT_MUSTBOUND)
3489+ gbound();
35043490 #endif
3491+ vtop->type.t = VT_PTR;
3492+ gaddrof();
35053493
3506− gen_struct_copy(size);
3507− } else {
3508−#endif
3509− /* destination */
3510− vswap();
3494+#ifdef TCC_TARGET_NATIVE_STRUCT_COPY
3495+ if (1
35113496 #ifdef CONFIG_TCC_BCHECK
3512− if (vtop->r & VT_MUSTBOUND)
3513− gbound(); /* check would be wrong after gaddrof() */
3497+ && !tcc_state->do_bounds_check
35143498 #endif
3515− vtop->type.t = VT_PTR;
3516− gaddrof();
3517−
3518− /* address of memcpy() */
3499+ ) {
3500+ gen_struct_copy(size);
3501+ } else
3502+#endif
3503+ {
3504+ /* type size */
3505+ vpushi(size);
3506+ /* Use memmove, rather than memcpy, as dest and src may be same: */
35193507 #ifdef TCC_ARM_EABI
35203508 if(!(align & 7))
35213509 vpush_helper_func(TOK_memmove8);
@@ -3523,25 +3511,11 @@ ST_FUNC void vstore(void) · 1 unmodified line
35233511 vpush_helper_func(TOK_memmove4);
35243512 else
35253513 #endif
3526− /* Use memmove, rather than memcpy, as dest and src may be same: */
35273514 vpush_helper_func(TOK_memmove);
3528−
3529− vswap();
3530− /* source */
3531− vpushv(vtop - 2);
3532−#ifdef CONFIG_TCC_BCHECK
3533− if (vtop->r & VT_MUSTBOUND)
3534− gbound();
3535−#endif
3536− vtop->type.t = VT_PTR;
3537− gaddrof();
3538− /* type size */
3539− vpushi(size);
3515+ vrott(4);
35403516 gfunc_call(3);
3541− /* leave source on stack */
3542−#ifdef TCC_TARGET_NATIVE_STRUCT_COPY
3543− }
3544−#endif
3517+ }
3518+
35453519 } else if (ft & VT_BITFIELD) {
35463520 /* bitfield store handling */
35473521
tccpp.c+16−2
@@ -21,6 +21,9 @@ · 20 unmodified lines
2121 #define USING_GLOBALS
2222 #include "tcc.h"
2323
24+/* #define to 1 to enable (see parse_pp_string()) */
25+#define ACCEPT_LF_IN_STRINGS 0
26+
2427 /********************************************************/
2528 /* global variables */
2629
@@ -944,16 +947,26 @@ static uint8_t *parse_pp_string(uint8_t *p, · 917 unmodified lines
944947 }
945948 }
946949 } else if (c == '\n') {
947− tcc_error("missing terminating %c character",sep);
950+ add_lf:
951+ if (ACCEPT_LF_IN_STRINGS) {
952+ file->line_num++;
953+ goto add_char;
954+ } else if (str) { /* not skipping */
955+ goto unterminated_string;
956+ } else {
957+ tcc_warning("missing terminating %c character", sep);
958+ return p;
959+ }
948960 } else if (c == '\r') {
949961 PEEKC_EOB(c, p);
950962 if (c != '\n') {
951963 if (str)
952964 cstr_ccat(str, '\r');
953965 } else {
954− tcc_error("missing terminating %c character",sep);
966+ goto add_lf;
955967 }
956968 } else {
969+ add_char:
957970 if (str)
958971 cstr_ccat(str, c);
959972 p++;
@@ -1006,6 +1019,7 @@ redo_start: · 46 unmodified lines
10061019 case '\'':
10071020 if (in_warn_or_error)
10081021 goto _default;
1022+ tok_flags &= ~TOK_FLAG_BOL;
10091023 p = parse_pp_string(p, c, NULL);
10101024 break;
10111025 /* skip comments */
tcctok.h+0−1
@@ -330,7 +330,6 @@ · 329 unmodified lines
330330 DEF(TOK___bound_setjmp, "__bound_setjmp")
331331 DEF(TOK___bound_longjmp, "__bound_longjmp")
332332 DEF(TOK___bound_new_region, "__bound_new_region")
333− DEF(TOK___bound_struct_copy,"__bound_struct_copy")
334333 # ifdef TCC_TARGET_PE
335334 # ifdef TCC_TARGET_X86_64
336335 DEF(TOK___bound_alloca_nr, "__bound_alloca_nr")
tests/tcctest.c+25−14
@@ -1519,8 +1519,6 @@ struct structa1 { · 1518 unmodified lines
15191519 char f2;
15201520 };
15211521
1522−struct structa1 ssta1;
1523−
15241522 void struct_assign_test1(struct structa1 s1, int t, float f)
15251523 {
15261524 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
@@ -1538,22 +1536,12 @@ void struct_assign_test(void) · 11 unmodified lines
15381536 struct S {
15391537 struct structa1 lsta1, lsta2;
15401538 int i;
1541− } s, *ps;
1539+ } s = {{1,2}, {3,4}}, *ps;
15421540
15431541 ps = &s;
15441542 ps->i = 4;
1545−#if 0
1546− s.lsta1.f1 = 1;
1547− s.lsta1.f2 = 2;
1548− printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1549− s.lsta2 = s.lsta1;
1550− printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1551−#else
1552− s.lsta2.f1 = 1;
1553− s.lsta2.f2 = 2;
1554−#endif
1543+
15551544 struct_assign_test1(ps->lsta2, 3, 4.5);
1556−
15571545 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
15581546 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
15591547 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
@@ -1565,6 +1553,9 @@ void struct_assign_test(void) · 5 unmodified lines
15651553 { struct_assign_test }
15661554 };
15671555 printf("%d\n", struct_assign_test == t[0].elem);
1556+
1557+ s.lsta1 = s.lsta2 = struct_assign_test2(s.lsta1, 1);
1558+ printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
15681559 }
15691560
15701561 /* casts to short/char */
@@ -4253,6 +4244,12 @@ void func_arg_test(void) · 2682 unmodified lines
42534244 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
42544245 #define CORRECT_CR_HANDLING
42554246
4247+/* deprecated and no longer supported in gcc 3.3 */
4248+/* no longer supported by default in TinyCC */
4249+#ifdef __TINYC__
4250+/* # define ACCEPT_LF_IN_STRINGS */
4251+#endif
4252+
42564253 /* keep this as the last test because GCC messes up line-numbers
42574254 with the ^L^K^M characters below */
42584255 void whitespace_test(void)
@@ -4274,6 +4271,20 @@ ntf("aaa=%d\n", 3); · 15 unmodified lines
42744271 \
42754272 ntf("min=%d\n", 4);
42764273
4274+#ifdef ACCEPT_LF_IN_STRINGS
4275+ printf("len1=%d\n", strlen("
4276+"));
4277+#ifdef CORRECT_CR_HANDLING
4278+ str = "
4279+";
4280+ printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
4281+#endif
4282+ printf("len1=%d\n", strlen(" a
4283+"));
4284+#else
4285+ printf("len1=1\nlen1=1 str[0]=10\nlen1=3\n");
4286+#endif /* ACCEPT_LF_IN_STRINGS */
4287+
42774288 #ifdef __LINE__
42784289 printf("__LINE__ defined\n");
42794290 #endif
tests/tests2/Makefile+1−2
@@ -105,8 +105,7 @@ GEN-ALWAYS = · 104 unmodified lines
105105 112_backtrace.test: FLAGS += -dt -b
106106 112_backtrace.test 113_btdll.test 126_bound_global.test: FILTER += \
107107 -e 's;[0-9A-Fa-fx]\{5,\};........;g' \
108− -e 's;0x[0-9A-Fa-f]\{1,\};0x?;g' \
109− -e 's;struct copy destination;memmove dest;g' \
108+ -e 's;0x[0-9A-Fa-f]\{1,\};0x?;g'
110109
111110 # this test creates two DLLs and an EXE
112111 113_btdll.test: T1 = \
x86_64-gen.c+29−41
@@ -35,6 +35,8 @@ · 34 unmodified lines
3535 #define RC_RAX 0x0004
3636 #define RC_RCX 0x0008
3737 #define RC_RDX 0x0010
38+#define RC_RSI 0x0020
39+#define RC_RDI 0x0040
3840 #define RC_ST0 0x0080 /* only for long double */
3941 #define RC_R8 0x0100
4042 #define RC_R9 0x0200
@@ -105,6 +107,10 @@ enum { · 64 unmodified lines
105107 /* define if return values need to be extended explicitely
106108 at caller side (for interfacing with non-TCC compilers) */
107109 #define PROMOTE_RET
110+
111+#define TCC_TARGET_NATIVE_STRUCT_COPY
112+ST_FUNC void gen_struct_copy(int size);
113+
108114 /******************************************************/
109115 #else /* ! TARGET_DEFS_ONLY */
110116 /******************************************************/
@@ -124,8 +130,8 @@ ST_DATA const int reg_classes[NB_REGS] = { · 13 unmodified lines
124130 0,
125131 0,
126132 0,
127− 0,
128− 0,
133+ RC_RSI,
134+ RC_RDI,
129135 RC_R8,
130136 RC_R9,
131137 RC_R10,
@@ -2228,7 +2234,7 @@ ST_FUNC void gen_increment_tcov (SValue *sv) · 2096 unmodified lines
22282234 }
22292235
22302236 /* computed goto support */
2231−void ggoto(void)
2237+ST_FUNC void ggoto(void)
22322238 {
22332239 gcall_or_jmp(1);
22342240 vtop--;
@@ -2286,53 +2292,35 @@ ST_FUNC void gen_vla_alloc(CType *type, int align) { · 51 unmodified lines
22862292 * Assmuing the top part of the stack looks like below,
22872293 * src dest src
22882294 */
2289−void gen_struct_copy(int size)
2295+ST_FUNC void gen_struct_copy(int size)
22902296 {
2291− save_reg(TREG_RSI);
2292− load(TREG_RSI,vtop);
2293− vtop->r = TREG_RSI;
2294− vswap(); /* dest src src */
2295− save_reg(TREG_RDI);
2296− load(TREG_RDI,vtop);
2297− vtop->r = TREG_RDI;
2298− /* Not aligned by 8bytes */
2299− if (size & 0x04) {
2300− o(0xa5);
2301− }
2302− if (size & 0x02) {
2303− o(0xa566);
2304− }
2305− if (size & 0x01) {
2306− o(0xa4);
2307− }
2308−
2309− size >>= 3;
2310− if (!size)
2311− goto done;
2312− /* Although this function is only called when the struct is smaller */
2313− /* than 32 bytes(4 * PTR_SIZE),a common implementation is included */
2314− if (size <= 4 && size) {
2315− switch (size) {
2316− case 4: o(0xa548);
2317− case 3: o(0xa548);
2318− case 2: o(0xa548);
2319− case 1: o(0xa548);
2320− }
2297+ int n = size / PTR_SIZE;
2298+#ifdef TCC_TARGET_PE
2299+ o(0x5756); /* push rsi, rdi */
2300+#endif
2301+ gv2(RC_RDI, RC_RSI);
2302+ if (n <= 4) {
2303+ while (n)
2304+ o(0xa548), --n;
23212305 } else {
2322− save_reg(TREG_RCX);
2323− vpushi(size);
2324− load(TREG_RCX,vtop);
2325− vtop->r = TREG_RCX;
2306+ vpushi(n);
2307+ gv(RC_RCX);
23262308 o(0xa548f3);
23272309 vpop();
23282310 }
2329−done:
2311+ if (size & 0x04)
2312+ o(0xa5);
2313+ if (size & 0x02)
2314+ o(0xa566);
2315+ if (size & 0x01)
2316+ o(0xa4);
2317+#ifdef TCC_TARGET_PE
2318+ o(0x5e5f); /* pop rdi, rsi */
2319+#endif
23302320 vpop();
23312321 vpop();
2332− return;
23332322 }
23342323
2335−
23362324 /* end of x86-64 code generator */
23372325 /*************************************************************/
23382326 #endif /* ! TARGET_DEFS_ONLY */

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