From 0fb54300b56512754221d80adda85ddb9815bceb Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Fri, 4 Sep 2026 18:46:13 +0200 Subject: [PATCH] Make bound checking faster. With this commit bound checking will be about 20% .. 25% faster. (Tested with running testsuites with bound checking. About 1000 files.) The speedup is made possible because of TLS implementation. TLS is not possible with -run so create a new bcheck_run.o Makefile:lib/Makefile:win32/build-tcc.bat - Compile new bcheck_run.o tccelf.c:tccpe.c - Use bcheck_run.o lib/bcheck.c - Use TLS code --- Makefile | 2 +- lib/Makefile | 6 ++++-- lib/bcheck.c | 12 ++++++++---- tccelf.c | 5 ++++- tccpe.c | 8 ++++++-- win32/build-tcc.bat | 1 + 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index fc1e31b2..62078ea3 100644 --- a/Makefile +++ b/Makefile @@ -372,7 +372,7 @@ IR = $(IM) mkdir -p $2 && cp -r $1/. $2 IM = @echo "-> $2 : $1" ; BINCHECK = $(if $(wildcard $(PROGS) *-tcc$(EXESUF)),,@echo "Makefile: nothing found to install" && exit 1) -EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o +EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o bcheck_run.o # install progs & libs install-unx: diff --git a/lib/Makefile b/lib/Makefile index 99e8b58d..3b166a86 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -53,10 +53,10 @@ Cbc = $(Cbt)$(subst yes,,$(CONFIG_bcheck)) $(Nat)COMMON_O += runmain.o tcov.o $(Cbt)COMMON_O += bt-exe.o bt-log.o $(Cbt)WIN_O += bt-dll.o -$(Cbc)COMMON_O += bcheck.o +$(Cbc)COMMON_O += bcheck.o bcheck_run.o # not in libtcc1.a -EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o +EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o bcheck_run.o OBJ-i386 = $(I386_O) pic86.o $(LIN_O) OBJ-x86_64 = $(X86_64_O) va_list.o $(LIN_O) @@ -95,6 +95,8 @@ $(TOP)/%.o : %.c $(TCC) $S$(XCC) -c $< -o $@ $(XFLAGS) $(TOP)/bcheck.o : XFLAGS += $(BFLAGS) +$(TOP)/bcheck_run.o : bcheck.c $(TCC) + $S$(XCC) -c bcheck.c -o $@ $(XFLAGS) $(BFLAGS) -DBCHECK_RUN $(X)crt1w.o : crt1.c $(X)wincrt1w.o : wincrt1.c diff --git a/lib/bcheck.c b/lib/bcheck.c index cd7c8a79..0f1d5e58 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -53,7 +53,11 @@ /* an __attribute__ macro is defined in the system headers */ #undef __attribute__ #endif +#ifdef __i386__ #define FASTCALL __attribute__((regparm(3))) +#else +#define FASTCALL +#endif #ifdef _WIN32 # define DLL_EXPORT __declspec(dllexport) @@ -157,12 +161,12 @@ static pthread_spinlock_t bounds_spin; #define HAVE_SIGNAL (1) #define HAVE_SIGACTION (1) #define HAVE_FORK (1) -#if !defined(__APPLE__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) -#define HAVE_TLS_FUNC (0) -#define HAVE_TLS_VAR (1) -#else +#if defined(__APPLE__) || defined(__arm__) || defined(__GNUC__) || defined(BCHECK_RUN) #define HAVE_TLS_FUNC (1) #define HAVE_TLS_VAR (0) +#else +#define HAVE_TLS_FUNC (0) +#define HAVE_TLS_VAR (1) #endif #if defined CONFIG_TCC_MUSL || defined __ANDROID__ # undef HAVE_CTYPE diff --git a/tccelf.c b/tccelf.c index 26bbeb9b..def5aebc 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1839,7 +1839,10 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) #ifdef CONFIG_TCC_BCHECK if (s1->do_bounds_check && s1->output_type != TCC_OUTPUT_DLL) { - tcc_add_support(s1, "bcheck.o"); + if (s1->output_type == TCC_OUTPUT_MEMORY) + tcc_add_support(s1, "bcheck_run.o"); + else + tcc_add_support(s1, "bcheck.o"); # if !(TARGETOS_OpenBSD || TARGETOS_NetBSD) tcc_add_library(s1, "dl"); # endif diff --git a/tccpe.c b/tccpe.c index 701a76af..d366a35a 100644 --- a/tccpe.c +++ b/tccpe.c @@ -2131,8 +2131,12 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) #ifdef CONFIG_TCC_BACKTRACE if (s1->do_backtrace) { #ifdef CONFIG_TCC_BCHECK - if (s1->do_bounds_check && s1->output_type != TCC_OUTPUT_DLL) - tcc_add_support(s1, "bcheck.o"); + if (s1->do_bounds_check && s1->output_type != TCC_OUTPUT_DLL) { + if (s1->output_type == TCC_OUTPUT_MEMORY) + tcc_add_support(s1, "bcheck_run.o"); + else + tcc_add_support(s1, "bcheck.o"); + } #endif if (s1->output_type == TCC_OUTPUT_EXE) tcc_add_support(s1, "bt-exe.o"); diff --git a/win32/build-tcc.bat b/win32/build-tcc.bat index 16f69e17..83b5636e 100644 --- a/win32/build-tcc.bat +++ b/win32/build-tcc.bat @@ -201,6 +201,7 @@ exit /B %ERRORLEVEL% .\%1tcc -B. -c ../lib/builtin.c .\%1tcc -ar lib/%1libtcc1.a %LIBTCC1%.o crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o winex.o chkstk.o alloca.o alloca-bt.o stdatomic.o atomic.o builtin.o .\%1tcc -B. -c ../lib/bcheck.c -o lib/%1bcheck.o -bt -I.. +.\%1tcc -B. -c ../lib/bcheck.c -o lib/%1bcheck_run.o -bt -I.. -DBCHECK_RUN .\%1tcc -B. -c ../lib/bt-exe.c -o lib/%1bt-exe.o .\%1tcc -B. -c ../lib/bt-log.c -o lib/%1bt-log.o .\%1tcc -B. -c ../lib/bt-dll.c -o lib/%1bt-dll.o