From 3670a48aeef0834b6a2d6454814764b46aa553f8 Mon Sep 17 00:00:00 2001 From: Tharo <17233964+Thar0@users.noreply.github.com> Date: Thu, 29 Feb 2024 20:11:54 +0000 Subject: [PATCH] Some GCC flags improvements (#1903) * Some GCC flags improvements * Move -fno-reorder-blocks -fno-zero-initialized-in-bss to assets only * Add -fno-PIC since some gcc versions use -fPIC as default * Enable builtin functions on gcc, with appropriate changes to missing_gcc_functions.c and ultra64/libc.h * Move -fno-merge-constants -mno-explicit-relocs -mno-split-addresses to overlays only as they are only needed there for reloc reasons * Remove unneeded casts in missing_gcc_functions.c * Change gcc assets flags handling --- Makefile | 7 ++++-- include/ultra64/libc.h | 6 +++++ src/gcc_fix/missing_gcc_functions.c | 37 ++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index ac9cb6e489..db137ca392 100644 --- a/Makefile +++ b/Makefile @@ -154,7 +154,7 @@ endif ASFLAGS := -march=vr4300 -32 -no-pad-sections -Iinclude ifeq ($(COMPILER),gcc) - CFLAGS += -G 0 -nostdinc $(INC) -march=vr4300 -mfix4300 -mabi=32 -mno-abicalls -mdivide-breaks -fno-zero-initialized-in-bss -fno-toplevel-reorder -ffreestanding -fno-common -fno-merge-constants -mno-explicit-relocs -mno-split-addresses $(CHECK_WARNINGS) -funsigned-char + CFLAGS += -G 0 -nostdinc $(INC) -march=vr4300 -mfix4300 -mabi=32 -mno-abicalls -mdivide-breaks -fno-PIC -fno-common -ffreestanding -fbuiltin -fno-builtin-sinf -fno-builtin-cosf $(CHECK_WARNINGS) -funsigned-char MIPS_VERSION := -mips3 else # Suppress warnings for wrong number of macro arguments (to fake variadic @@ -314,8 +314,11 @@ $(BUILD_DIR)/src/overlays/%.o: CC := $(PYTHON) tools/asm_processor/build.py $(CC $(BUILD_DIR)/assets/%.o: CC := $(PYTHON) tools/asm_processor/build.py $(CC) -- $(AS) $(ASFLAGS) -- else +# Note that if adding additional assets directories for modding reasons these flags must also be used there +$(BUILD_DIR)/assets/%.o: CFLAGS += -fno-zero-initialized-in-bss -fno-toplevel-reorder +$(BUILD_DIR)/src/%.o: CFLAGS += -fexec-charset=euc-jp $(BUILD_DIR)/src/libultra/libc/ll.o: OPTFLAGS := -Ofast -$(BUILD_DIR)/src/%.o: CC := $(CC) -fexec-charset=euc-jp +$(BUILD_DIR)/src/overlays/%.o: CFLAGS += -fno-merge-constants -mno-explicit-relocs -mno-split-addresses endif #### Main Targets ### diff --git a/include/ultra64/libc.h b/include/ultra64/libc.h index f297bfd0d7..e71cb879d7 100644 --- a/include/ultra64/libc.h +++ b/include/ultra64/libc.h @@ -5,8 +5,14 @@ void osSyncPrintf(const char* fmt, ...); +#ifdef __GNUC__ +void bzero(void* __s, unsigned int __n); +int bcmp(const void* __sl, const void* __s2, unsigned int __n); +void bcopy(const void* __src, void* __dest, unsigned int __n); +#else void bzero(void* __s, int __n); int bcmp(const void* __sl, const void* __s2, int __n); void bcopy(const void* __src, void* __dest, int __n); +#endif #endif diff --git a/src/gcc_fix/missing_gcc_functions.c b/src/gcc_fix/missing_gcc_functions.c index b4d87a8926..b955569372 100644 --- a/src/gcc_fix/missing_gcc_functions.c +++ b/src/gcc_fix/missing_gcc_functions.c @@ -10,10 +10,10 @@ // Self-hosted libc memory functions, gcc assumes these exist even in a freestanding // environment and there is no way to tell it otherwise. -int memcmp(void* s1, const void* s2, size_t n) { - u8* m1 = (u8*)s1; - u8* m2 = (u8*)s2; - u32 i; +int memcmp(const void* s1, const void* s2, size_t n) { + const u8* m1 = s1; + const u8* m2 = s2; + size_t i; for (i = 0; i < n; i++) { if (m1[i] < m2[i]) { @@ -26,17 +26,38 @@ int memcmp(void* s1, const void* s2, size_t n) { return 0; } -void* memset(void* str, s32 c, size_t n) { - u8* m1 = (u8*)str; - u32 i; +void* memset(void* str, int c, size_t n) { + u8* m = str; + size_t i; for (i = 0; i < n; i++) { - m1[i] = c; + m[i] = c; } return str; } +void* memmove(void* dest, const void* src, size_t len) { + u8* d = dest; + const u8* s = src; + + if (d == s) { + return dest; + } + if (d < s) { + while (len--) { + *d++ = *s++; + } + } else { + d += len - 1; + s += len - 1; + while (len--) { + *d-- = *s--; + } + } + return dest; +} + // Conversions involving 64-bit integer types required by the O32 MIPS ABI. // f32 -> u64, negative values become 0