*: generalize "const trick"

While at it, change all "__asm__" to "asm"

Co-authored-by: canyie <31466456+canyie@users.noreply.github.com>
Signed-off-by: YU Jincheng <shana@zju.edu.cn>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
YU Jincheng 2021-09-29 17:37:26 +08:00 committed by Denys Vlasenko
parent 17e6fb06b3
commit 1f925038ab
6 changed files with 34 additions and 38 deletions

View File

@ -435,7 +435,7 @@ struct test_statics {
}; };
/* See test_ptr_hack.c */ /* See test_ptr_hack.c */
extern struct test_statics *const test_ptr_to_statics; extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics;
#define S (*test_ptr_to_statics) #define S (*test_ptr_to_statics)
#define args (S.args ) #define args (S.args )
@ -446,8 +446,7 @@ extern struct test_statics *const test_ptr_to_statics;
#define leaving (S.leaving ) #define leaving (S.leaving )
#define INIT_S() do { \ #define INIT_S() do { \
(*(struct test_statics**)not_const_pp(&test_ptr_to_statics)) = xzalloc(sizeof(S)); \ ASSIGN_CONST_PTR(test_ptr_to_statics, xzalloc(sizeof(S))); \
barrier(); \
} while (0) } while (0)
#define DEINIT_S() do { \ #define DEINIT_S() do { \
free(group_array); \ free(group_array); \

View File

@ -365,13 +365,27 @@ struct BUG_off_t_size_is_misdetected {
#endif #endif
#endif #endif
/* We use a trick to have more optimized code (fewer pointer reloads
* and reduced binary size by a few kilobytes) like:
* ash.c: extern struct globals *const ash_ptr_to_globals;
* ash_ptr_hack.c: struct globals *ash_ptr_to_globals;
* This way, compiler in ash.c knows the pointer can not change.
*
* However, this may break on weird arches or toolchains. In this case,
* set "-DBB_GLOBAL_CONST=''" in CONFIG_EXTRA_CFLAGS to disable
* this optimization.
*/
#ifndef BB_GLOBAL_CONST
# define BB_GLOBAL_CONST const
#endif
#if defined(errno) #if defined(errno)
/* If errno is a define, assume it's "define errno (*__errno_location())" /* If errno is a define, assume it's "define errno (*__errno_location())"
* and we will cache it's result in this variable */ * and we will cache it's result in this variable */
extern int *const bb_errno; extern int *BB_GLOBAL_CONST bb_errno;
#undef errno # undef errno
#define errno (*bb_errno) # define errno (*bb_errno)
#define bb_cached_errno_ptr 1 # define bb_cached_errno_ptr 1
#endif #endif
#if !(ULONG_MAX > 0xffffffff) #if !(ULONG_MAX > 0xffffffff)
@ -2270,6 +2284,8 @@ struct globals;
* If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */ * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
extern struct globals *const ptr_to_globals; extern struct globals *const ptr_to_globals;
#define barrier() asm volatile ("":::"memory")
#if defined(__clang_major__) && __clang_major__ >= 9 #if defined(__clang_major__) && __clang_major__ >= 9
/* Clang/llvm drops assignment to "constant" storage. Silently. /* Clang/llvm drops assignment to "constant" storage. Silently.
* Needs serious convincing to not eliminate the store. * Needs serious convincing to not eliminate the store.
@ -2277,7 +2293,7 @@ extern struct globals *const ptr_to_globals;
static ALWAYS_INLINE void* not_const_pp(const void *p) static ALWAYS_INLINE void* not_const_pp(const void *p)
{ {
void *pp; void *pp;
__asm__ __volatile__( asm volatile (
"# forget that p points to const" "# forget that p points to const"
: /*outputs*/ "=r" (pp) : /*outputs*/ "=r" (pp)
: /*inputs*/ "0" (p) : /*inputs*/ "0" (p)
@ -2288,13 +2304,13 @@ static ALWAYS_INLINE void* not_const_pp(const void *p)
static ALWAYS_INLINE void* not_const_pp(const void *p) { return (void*)p; } static ALWAYS_INLINE void* not_const_pp(const void *p) { return (void*)p; }
#endif #endif
/* At least gcc 3.4.6 on mipsel system needs optimization barrier */ #define ASSIGN_CONST_PTR(p, v) do { \
#define barrier() __asm__ __volatile__("":::"memory") *(void**)not_const_pp(&p) = (void*)(v); \
#define SET_PTR_TO_GLOBALS(x) do { \ /* At least gcc 3.4.6 on mipsel needs optimization barrier */ \
(*(struct globals**)not_const_pp(&ptr_to_globals)) = (void*)(x); \
barrier(); \ barrier(); \
} while (0) } while (0)
#define SET_PTR_TO_GLOBALS(x) ASSIGN_CONST_PTR(ptr_to_globals, x)
#define FREE_PTR_TO_GLOBALS() do { \ #define FREE_PTR_TO_GLOBALS() do { \
if (ENABLE_FEATURE_CLEAN_UP) { \ if (ENABLE_FEATURE_CLEAN_UP) { \
free(ptr_to_globals); \ free(ptr_to_globals); \

View File

@ -247,8 +247,7 @@ void lbb_prepare(const char *applet
IF_FEATURE_INDIVIDUAL(, char **argv)) IF_FEATURE_INDIVIDUAL(, char **argv))
{ {
#ifdef bb_cached_errno_ptr #ifdef bb_cached_errno_ptr
(*(int **)not_const_pp(&bb_errno)) = get_perrno(); ASSIGN_CONST_PTR(bb_errno, get_perrno());
barrier();
#endif #endif
applet_name = applet; applet_name = applet;

View File

@ -192,7 +192,7 @@ struct lineedit_statics {
}; };
/* See lineedit_ptr_hack.c */ /* See lineedit_ptr_hack.c */
extern struct lineedit_statics *const lineedit_ptr_to_statics; extern struct lineedit_statics *BB_GLOBAL_CONST lineedit_ptr_to_statics;
#define S (*lineedit_ptr_to_statics) #define S (*lineedit_ptr_to_statics)
#define state (S.state ) #define state (S.state )
@ -214,8 +214,7 @@ extern struct lineedit_statics *const lineedit_ptr_to_statics;
#define delbuf (S.delbuf ) #define delbuf (S.delbuf )
#define INIT_S() do { \ #define INIT_S() do { \
(*(struct lineedit_statics**)not_const_pp(&lineedit_ptr_to_statics)) = xzalloc(sizeof(S)); \ ASSIGN_CONST_PTR(lineedit_ptr_to_statics, xzalloc(sizeof(S))); \
barrier(); \
} while (0) } while (0)
static void deinit_S(void) static void deinit_S(void)

View File

@ -505,7 +505,7 @@ static void cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx,
unsigned int *edx) unsigned int *edx)
{ {
/* EAX value specifies what information to return */ /* EAX value specifies what information to return */
__asm__( asm (
" pushl %%ebx\n" /* Save EBX */ " pushl %%ebx\n" /* Save EBX */
" cpuid\n" " cpuid\n"
" movl %%ebx, %1\n" /* Save content of EBX */ " movl %%ebx, %1\n" /* Save content of EBX */

View File

@ -303,20 +303,6 @@ typedef long arith_t;
# error "Do not even bother, ash will not run on NOMMU machine" # error "Do not even bother, ash will not run on NOMMU machine"
#endif #endif
/* We use a trick to have more optimized code (fewer pointer reloads):
* ash.c: extern struct globals *const ash_ptr_to_globals;
* ash_ptr_hack.c: struct globals *ash_ptr_to_globals;
* This way, compiler in ash.c knows the pointer can not change.
*
* However, this may break on weird arches or toolchains. In this case,
* set "-DBB_GLOBAL_CONST=''" in CONFIG_EXTRA_CFLAGS to disable
* this optimization.
*/
#ifndef BB_GLOBAL_CONST
# define BB_GLOBAL_CONST const
#endif
/* ============ Hash table sizes. Configurable. */ /* ============ Hash table sizes. Configurable. */
#define VTABSIZE 39 #define VTABSIZE 39
@ -518,8 +504,7 @@ extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc;
#define random_gen (G_misc.random_gen ) #define random_gen (G_misc.random_gen )
#define backgndpid (G_misc.backgndpid ) #define backgndpid (G_misc.backgndpid )
#define INIT_G_misc() do { \ #define INIT_G_misc() do { \
(*(struct globals_misc**)not_const_pp(&ash_ptr_to_globals_misc)) = xzalloc(sizeof(G_misc)); \ ASSIGN_CONST_PTR(ash_ptr_to_globals_misc, xzalloc(sizeof(G_misc))); \
barrier(); \
savestatus = -1; \ savestatus = -1; \
curdir = nullstr; \ curdir = nullstr; \
physdir = nullstr; \ physdir = nullstr; \
@ -1597,8 +1582,7 @@ extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack;
#define g_stacknleft (G_memstack.g_stacknleft) #define g_stacknleft (G_memstack.g_stacknleft)
#define stackbase (G_memstack.stackbase ) #define stackbase (G_memstack.stackbase )
#define INIT_G_memstack() do { \ #define INIT_G_memstack() do { \
(*(struct globals_memstack**)not_const_pp(&ash_ptr_to_globals_memstack)) = xzalloc(sizeof(G_memstack)); \ ASSIGN_CONST_PTR(ash_ptr_to_globals_memstack, xzalloc(sizeof(G_memstack))); \
barrier(); \
g_stackp = &stackbase; \ g_stackp = &stackbase; \
g_stacknxt = stackbase.space; \ g_stacknxt = stackbase.space; \
g_stacknleft = MINSIZE; \ g_stacknleft = MINSIZE; \
@ -2229,8 +2213,7 @@ extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var;
#endif #endif
#define INIT_G_var() do { \ #define INIT_G_var() do { \
unsigned i; \ unsigned i; \
(*(struct globals_var**)not_const_pp(&ash_ptr_to_globals_var)) = xzalloc(sizeof(G_var)); \ ASSIGN_CONST_PTR(ash_ptr_to_globals_var, xzalloc(sizeof(G_var))); \
barrier(); \
for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
varinit[i].flags = varinit_data[i].flags; \ varinit[i].flags = varinit_data[i].flags; \
varinit[i].var_text = varinit_data[i].var_text; \ varinit[i].var_text = varinit_data[i].var_text; \