From f317a47cd8d64191af586e569de829aff36014c1 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 20 Jun 2022 04:32:25 -0700 Subject: [PATCH] Fixup and polish chibicc some more Fixes #433 --- ape/ape.lds | 52 ++-- libc/calls/clock_gettime.c | 2 +- ...ock_gettime.h => clock_gettime.internal.h} | 4 +- libc/calls/now.c | 2 +- libc/fmt/conv.h | 18 +- libc/nt/version.h | 15 +- libc/str/slice.h | 17 +- libc/tinymath/emod.h | 9 +- libc/tinymath/emodl.h | 9 +- test/libc/calls/clock_gettime_test.c | 2 +- test/libc/release/test.mk | 289 ++++++++++-------- third_party/chibicc/asm.c | 2 +- third_party/chibicc/chibicc.c | 21 +- third_party/xed/x86.h | 52 ++-- third_party/xed/x86isa.h | 74 ++--- 15 files changed, 315 insertions(+), 253 deletions(-) rename libc/calls/{clock_gettime.h => clock_gettime.internal.h} (79%) diff --git a/ape/ape.lds b/ape/ape.lds index 7582a24da..ca1b0956e 100644 --- a/ape/ape.lds +++ b/ape/ape.lds @@ -553,32 +553,6 @@ SHSTUB2(ape_loader_dd_count, ? ROUNDUP(ape_loader_end - ape_loader, PAGESIZE) / 64 : 0); -#if SupportsXnu() -SHSTUB2(ape_macho_dd_skip, RVA(ape_macho) / 8); -SHSTUB2(ape_macho_dd_count, (ape_macho_end - ape_macho) / 8); -#endif - -#if SupportsWindows() || SupportsMetal() -#define LINK_WINDOWS (SupportsWindows() && !DEFINED(EfiMain)) -PFSTUB4(ape_pe_offset, ape_pe - ape_mz); -HIDDEN(ape_pe_optsz = ape_pe_sections - (ape_pe + 24)); -HIDDEN(ape_pe_shnum = (ape_pe_sections_end - ape_pe_sections) / 40); -HIDDEN(ape_pe_base = IMAGE_BASE_VIRTUAL); -HIDDEN(ape_idata = LINK_WINDOWS ? RVA(ape_idata_iat) : 0); -HIDDEN(ape_idata_iatsize = LINK_WINDOWS ? ape_idata_iatend - ape_idata_iat : 0); -HIDDEN(ape_idata = LINK_WINDOWS ? RVA(ape_idata_idt) : 0); -HIDDEN(ape_idata_idtsize = LINK_WINDOWS ? ape_idata_idtend - ape_idata_idt : 0); -HIDDEN(v_ntversion = LINK_WINDOWS ? 6 : 1); -HIDDEN(v_ntdllchar = LINK_WINDOWS ? 288 : 0); -HIDDEN(v_ntsubversion = LINK_WINDOWS ? 6 : 5); -HIDDEN(v_ntsubsystem = (LINK_WINDOWS - ? (DEFINED(GetMessage) - ? kNtImageSubsystemWindowsGui - : kNtImageSubsystemWindowsCui) - : kNtImageSubsystemEfiApplication)); -HIDDEN(ape_pe_entry = LINK_WINDOWS ? WinMain : EfiMain); -#endif - #if SupportsMetal() HIDDEN(v_ape_realsectors = MIN(0x70000 - IMAGE_BASE_REAL, @@ -685,6 +659,32 @@ CHURN(WinMain); #endif /* SupportsWindows() */ #endif /* SupportsXnu() */ +#if SupportsWindows() || SupportsMetal() +#define LINK_WINDOWS (SupportsWindows() && !DEFINED(EfiMain)) +PFSTUB4(ape_pe_offset, ape_pe - ape_mz); +HIDDEN(ape_pe_optsz = ape_pe_sections - (ape_pe + 24)); +HIDDEN(ape_pe_shnum = (ape_pe_sections_end - ape_pe_sections) / 40); +HIDDEN(ape_pe_base = IMAGE_BASE_VIRTUAL); +HIDDEN(ape_idata = LINK_WINDOWS ? RVA(ape_idata_iat) : 0); +HIDDEN(ape_idata_iatsize = LINK_WINDOWS ? ape_idata_iatend - ape_idata_iat : 0); +HIDDEN(ape_idata = LINK_WINDOWS ? RVA(ape_idata_idt) : 0); +HIDDEN(ape_idata_idtsize = LINK_WINDOWS ? ape_idata_idtend - ape_idata_idt : 0); +HIDDEN(v_ntversion = LINK_WINDOWS ? 6 : 1); +HIDDEN(v_ntdllchar = LINK_WINDOWS ? 288 : 0); +HIDDEN(v_ntsubversion = LINK_WINDOWS ? 6 : 5); +HIDDEN(v_ntsubsystem = (LINK_WINDOWS + ? (DEFINED(GetMessage) + ? kNtImageSubsystemWindowsGui + : kNtImageSubsystemWindowsCui) + : kNtImageSubsystemEfiApplication)); +HIDDEN(ape_pe_entry = LINK_WINDOWS ? WinMain : EfiMain); +#endif + +#if SupportsXnu() +SHSTUB2(ape_macho_dd_skip, RVA(ape_macho) / 8); +SHSTUB2(ape_macho_dd_count, (ape_macho_end - ape_macho) / 8); +#endif + ASSERT(DEFINED(ape_mz) ? ape_mz == IMAGE_BASE_VIRTUAL : 1, "linker panic"); ASSERT((DEFINED(__init_bss_end) ? __init_bss_end : 0) % __SIZEOF_POINTER__ == 0, "__init_bss misalign"); diff --git a/libc/calls/clock_gettime.c b/libc/calls/clock_gettime.c index 0030389f0..68e4bcf62 100644 --- a/libc/calls/clock_gettime.c +++ b/libc/calls/clock_gettime.c @@ -20,7 +20,7 @@ #include "libc/bits/asmflag.h" #include "libc/bits/bits.h" #include "libc/calls/asan.internal.h" -#include "libc/calls/clock_gettime.h" +#include "libc/calls/clock_gettime.internal.h" #include "libc/calls/internal.h" #include "libc/calls/state.internal.h" #include "libc/calls/strace.internal.h" diff --git a/libc/calls/clock_gettime.h b/libc/calls/clock_gettime.internal.h similarity index 79% rename from libc/calls/clock_gettime.h rename to libc/calls/clock_gettime.internal.h index e6afedd45..ba359b990 100644 --- a/libc/calls/clock_gettime.h +++ b/libc/calls/clock_gettime.internal.h @@ -7,8 +7,8 @@ COSMOPOLITAN_C_START_ typedef int clock_gettime_f(int, struct timespec *); extern clock_gettime_f *__clock_gettime; -hidden clock_gettime_f __clock_gettime_init; -hidden clock_gettime_f *__clock_gettime_get(bool *); +clock_gettime_f *__clock_gettime_get(bool *) hidden; +int __clock_gettime_init(int, struct timespec *) hidden; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/calls/now.c b/libc/calls/now.c index b6c878956..68fa408af 100644 --- a/libc/calls/now.c +++ b/libc/calls/now.c @@ -20,7 +20,7 @@ #include "libc/bits/initializer.internal.h" #include "libc/bits/safemacros.internal.h" #include "libc/calls/calls.h" -#include "libc/calls/clock_gettime.h" +#include "libc/calls/clock_gettime.internal.h" #include "libc/calls/internal.h" #include "libc/calls/state.internal.h" #include "libc/calls/strace.internal.h" diff --git a/libc/fmt/conv.h b/libc/fmt/conv.h index c16567951..cc67298b9 100644 --- a/libc/fmt/conv.h +++ b/libc/fmt/conv.h @@ -50,14 +50,18 @@ int64_t TimeValToWindowsTime(struct timeval) libcesque nosideeffect; struct timeval WindowsDurationToTimeVal(int64_t) libcesque nosideeffect; struct timespec WindowsDurationToTimeSpec(int64_t) libcesque nosideeffect; -static inline struct NtFileTime MakeFileTime(int64_t x) { - return (struct NtFileTime){(uint32_t)x, (uint32_t)(x >> 32)}; -} +#define MakeFileTime(x) \ + ({ \ + int64_t __x = x; \ + (struct NtFileTime){(uint32_t)__x, (uint32_t)(__x >> 32)}; \ + }) -static inline int64_t ReadFileTime(struct NtFileTime t) { - uint64_t x = t.dwHighDateTime; - return x << 32 | t.dwLowDateTime; -} +#define ReadFileTime(t) \ + ({ \ + struct NtFileTime __t = t; \ + uint64_t x = __t.dwHighDateTime; \ + (int64_t)(x << 32 | __t.dwLowDateTime); \ + }) #define FileTimeToTimeSpec(x) WindowsTimeToTimeSpec(ReadFileTime(x)) #define FileTimeToTimeVal(x) WindowsTimeToTimeVal(ReadFileTime(x)) diff --git a/libc/nt/version.h b/libc/nt/version.h index c7da8aab0..106cc54eb 100644 --- a/libc/nt/version.h +++ b/libc/nt/version.h @@ -9,13 +9,14 @@ bool32 GetVersionEx(struct NtOsVersionInfo *lpVersionInformation); #if defined(__GCC_ASM_FLAG_OUTPUTS__) && !defined(__STRICT_ANSI__) #define IsAtLeastWindows10() (GetNtMajorVersion() >= 10) -static pureconst inline unsigned char GetNtMajorVersion(void) { - uintptr_t _x; - asm("mov\t%%gs:96,%q0\r\n" - "mov\t280(%q0),%b0" - : "=q"(_x)); - return _x; -} +#define GetNtMajorVersion() \ + ({ \ + uintptr_t __x; \ + asm("mov\t%%gs:96,%q0\r\n" \ + "mov\t280(%q0),%b0" \ + : "=q"(__x)); \ + (unsigned char)__x; \ + }) #endif COSMOPOLITAN_C_END_ diff --git a/libc/str/slice.h b/libc/str/slice.h index a7ced4564..b582f21de 100644 --- a/libc/str/slice.h +++ b/libc/str/slice.h @@ -4,14 +4,17 @@ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -forceinline bool SlicesEqual(const char *a, size_t n, const char *b, size_t m) { - return n == m && !memcmp(a, b, n); -} +#define SlicesEqual(a, n, b, m) \ + ({ \ + size_t __n = (n); \ + __n == (m) && !memcmp(a, b, __n); \ + }) -forceinline bool SlicesEqualCase(const void *a, size_t n, const void *b, - size_t m) { - return n == m && !memcasecmp(a, b, n); -} +#define SlicesEqualCase(a, n, b, m) \ + ({ \ + size_t __n = (n); \ + __n == (m) && !memcasecmp(a, b, __n); \ + }) int CompareSlices(const char *, size_t, const char *, size_t); int CompareSlicesCase(const char *, size_t, const char *, size_t); diff --git a/libc/tinymath/emod.h b/libc/tinymath/emod.h index 114f60217..3e3a4139b 100644 --- a/libc/tinymath/emod.h +++ b/libc/tinymath/emod.h @@ -9,9 +9,12 @@ * @return (𝑥 mod 𝑦) ∈ [0.,𝑦) * @see fmod() */ -static inline double emod(double x, double y) { - return x - fabs(y) * floor(x / fabs(y)); -} +#define emod(x, y) \ + ({ \ + double __x = x; \ + double __y = y; \ + __x - fabs(__y) * floor(__x / fabs(__y)); \ + }) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_TINYMATH_EMOD_H_ */ diff --git a/libc/tinymath/emodl.h b/libc/tinymath/emodl.h index 00bbac5d2..b0c29eab8 100644 --- a/libc/tinymath/emodl.h +++ b/libc/tinymath/emodl.h @@ -9,9 +9,12 @@ * @return (𝑥 mod 𝑦) ∈ [0.,𝑦) * @see fmodl() */ -static inline long double emodl(long double x, long double y) { - return x - fabsl(y) * floorl(x / fabsl(y)); -} +#define emodl(x, y) \ + ({ \ + long double __x = x; \ + long double __y = y; \ + __x - fabsl(__y) * floorl(__x / fabsl(__y)); \ + }) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_TINYMATH_EMODL_H_ */ diff --git a/test/libc/calls/clock_gettime_test.c b/test/libc/calls/clock_gettime_test.c index fc9c2cc35..fe279a468 100644 --- a/test/libc/calls/clock_gettime_test.c +++ b/test/libc/calls/clock_gettime_test.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/clock_gettime.h" +#include "libc/calls/clock_gettime.internal.h" #include "libc/calls/internal.h" #include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timeval.h" diff --git a/test/libc/release/test.mk b/test/libc/release/test.mk index b1edcb485..ae5f89d6b 100644 --- a/test/libc/release/test.mk +++ b/test/libc/release/test.mk @@ -1,148 +1,191 @@ #-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ -o/$(MODE)/test/libc/release/cosmopolitan.zip: \ - o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ - o/$(MODE)/ape/ape-no-modify-self.o \ - o/$(MODE)/cosmopolitan.a \ +o/$(MODE)/test/libc/release/cosmopolitan.zip: \ + o/cosmopolitan.h \ + o/$(MODE)/ape/ape.lds \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ + o/$(MODE)/ape/ape-no-modify-self.o \ + o/$(MODE)/cosmopolitan.a \ o/$(MODE)/third_party/zip/zip.com - @$(COMPILE) -AZIP -T$@ \ - o/$(MODE)/third_party/zip/zip.com \ - -qj $@ \ - o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ - o/$(MODE)/ape/ape-no-modify-self.o \ + @$(COMPILE) -AZIP -T$@ \ + o/$(MODE)/third_party/zip/zip.com \ + -qj $@ \ + o/cosmopolitan.h \ + o/$(MODE)/ape/ape.lds \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ + o/$(MODE)/ape/ape-no-modify-self.o \ o/$(MODE)/cosmopolitan.a -o/$(MODE)/test/libc/release/smoke.com: \ - o/$(MODE)/test/libc/release/smoke.com.dbg - @$(COMPILE) -AOBJCOPY -T$< $(OBJCOPY) -S -O binary $< $@ +o/$(MODE)/test/libc/release/smoke.o: \ + test/libc/release/smoke.c \ + o/cosmopolitan.h + @$(COMPILE) -ACC $(CC) \ + -o $@ \ + -c \ + -Os \ + -fno-pie \ + -nostdinc \ + -fno-omit-frame-pointer \ + -include o/cosmopolitan.h \ + $< -o/$(MODE)/test/libc/release/smoke.com.dbg: \ - test/libc/release/smoke.c \ - o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ - o/$(MODE)/cosmopolitan.a - @$(COMPILE) -ACC $(CC) \ - -o $@ \ - -Os \ - -static \ - -no-pie \ - -fno-pie \ - -nostdlib \ - -nostdinc \ - -mno-red-zone \ - -fno-omit-frame-pointer \ - -Wl,-T,o/$(MODE)/ape/ape.lds \ - -include o/cosmopolitan.h \ - test/libc/release/smoke.c \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ +o/$(MODE)/test/libc/release/smoke.com.dbg: \ + o/$(MODE)/test/libc/release/smoke.o \ + o/$(MODE)/ape/ape.lds \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ o/$(MODE)/cosmopolitan.a + @$(COMPILE) -ALD $(LD) \ + -static \ + -no-pie \ + -nostdlib \ + -T o/$(MODE)/ape/ape.lds \ + o/$(MODE)/test/libc/release/smoke.o \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ + o/$(MODE)/cosmopolitan.a \ + -o $@ -o/$(MODE)/test/libc/release/smoke-nms.com.dbg: \ - test/libc/release/smoke.c \ - o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape-no-modify-self.o \ - o/$(MODE)/cosmopolitan.a - @$(COMPILE) -ACC $(CC) \ - -o $@ \ - -Os \ - -static \ - -no-pie \ - -fno-pie \ - -nostdlib \ - -nostdinc \ - -mno-red-zone \ - -fno-omit-frame-pointer \ - -Wl,-T,o/$(MODE)/ape/ape.lds \ - -include o/cosmopolitan.h \ - test/libc/release/smoke.c \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape-no-modify-self.o \ +o/$(MODE)/test/libc/release/smoke-nms.com.dbg: \ + o/$(MODE)/test/libc/release/smoke.o \ + o/$(MODE)/ape/ape.lds \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape-no-modify-self.o \ o/$(MODE)/cosmopolitan.a + @$(COMPILE) -ALD $(LD) \ + -static \ + -no-pie \ + -nostdlib \ + -T o/$(MODE)/ape/ape.lds \ + o/$(MODE)/test/libc/release/smoke.o \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape-no-modify-self.o \ + o/$(MODE)/cosmopolitan.a \ + -o $@ -o/$(MODE)/test/libc/release/smokecxx.com: \ - o/$(MODE)/test/libc/release/smokecxx.com.dbg - @$(COMPILE) -AOBJCOPY -T$< $(OBJCOPY) -S -O binary $< $@ +o/$(MODE)/test/libc/release/smoke-chibicc.com.dbg: \ + o/$(MODE)/test/libc/release/smoke-chibicc.o \ + o/$(MODE)/ape/ape.lds \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape-no-modify-self.o \ + o/$(MODE)/cosmopolitan.a \ + o/$(MODE)/third_party/chibicc/chibicc.com + @$(COMPILE) -ALD $(LD) \ + -static \ + -no-pie \ + -nostdlib \ + -T o/$(MODE)/ape/ape.lds \ + o/$(MODE)/test/libc/release/smoke-chibicc.o \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape-no-modify-self.o \ + o/$(MODE)/cosmopolitan.a \ + -o $@ -o/$(MODE)/test/libc/release/smokecxx.com.dbg: \ - test/libc/release/smokecxx.cc \ - o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ - o/$(MODE)/cosmopolitan.a - @$(COMPILE) -ACXX $(CXX) \ - -o $@ \ - -Os \ - -static \ - -no-pie \ - -fno-pie \ - -nostdlib \ - -nostdinc \ - -mno-red-zone \ - -fno-omit-frame-pointer \ - -Wl,-T,o/$(MODE)/ape/ape.lds \ - -include o/cosmopolitan.h \ - test/libc/release/smokecxx.cc \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ - o/$(MODE)/cosmopolitan.a +o/$(MODE)/test/libc/release/smoke-chibicc.o: \ + test/libc/release/smoke.c \ + o/cosmopolitan.h \ + o/$(MODE)/third_party/chibicc/chibicc.com + @$(COMPILE) -ACHIBICC \ + o/$(MODE)/third_party/chibicc/chibicc.com \ + $(CHIBICC_FLAGS) \ + -o $@ \ + -c \ + -Os \ + -static \ + -fno-pie \ + -nostdlib \ + -nostdinc \ + -mno-red-zone \ + -fno-omit-frame-pointer \ + -include o/cosmopolitan.h \ + $< -o/$(MODE)/test/libc/release/smokeansi.com.dbg: \ - test/libc/release/smoke.c \ - o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ +o/$(MODE)/test/libc/release/smokecxx.com.dbg: \ + o/$(MODE)/test/libc/release/smokecxx.o \ + o/$(MODE)/ape/ape.lds \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ o/$(MODE)/cosmopolitan.a - @$(COMPILE) -AANSI $(CC) \ - -o $@ \ - -Os \ - -ansi \ - -static \ - -no-pie \ - -fno-pie \ - -nostdlib \ - -nostdinc \ - -mno-red-zone \ - -fno-omit-frame-pointer \ - -Wl,-T,o/$(MODE)/ape/ape.lds \ - -include o/cosmopolitan.h \ - test/libc/release/smoke.c \ - o/$(MODE)/libc/crt/crt.o \ - o/$(MODE)/ape/ape.o \ + @$(COMPILE) -ALD $(LD) \ + -static \ + -no-pie \ + -nostdlib \ + -T o/$(MODE)/ape/ape.lds \ + o/$(MODE)/test/libc/release/smokecxx.o \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ + o/$(MODE)/cosmopolitan.a \ + -o $@ + +o/$(MODE)/test/libc/release/smokecxx.o: \ + test/libc/release/smokecxx.cc \ + o/cosmopolitan.h + @$(COMPILE) -ACXX $(CXX) \ + -o $@ \ + -c \ + -Os \ + -fno-pie \ + -nostdinc \ + -fno-omit-frame-pointer \ + -include o/cosmopolitan.h \ + test/libc/release/smokecxx.cc + +o/$(MODE)/test/libc/release/smokeansi.com.dbg: \ + o/$(MODE)/test/libc/release/smokeansi.o \ + o/$(MODE)/ape/ape.lds \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ o/$(MODE)/cosmopolitan.a + @$(COMPILE) -ALD $(LD) \ + -static \ + -no-pie \ + -nostdlib \ + -T o/$(MODE)/ape/ape.lds \ + o/$(MODE)/test/libc/release/smokeansi.o \ + o/$(MODE)/libc/crt/crt.o \ + o/$(MODE)/ape/ape.o \ + o/$(MODE)/cosmopolitan.a \ + -o $@ + +o/$(MODE)/test/libc/release/smokeansi.o: \ + test/libc/release/smoke.c \ + o/cosmopolitan.h + @$(COMPILE) -AANSI $(CC) \ + -o $@ \ + -c \ + -Os \ + -ansi \ + -static \ + -fno-pie \ + -nostdinc \ + -fno-omit-frame-pointer \ + -include o/cosmopolitan.h \ + test/libc/release/smoke.c # TODO(jart): Rewrite these shell scripts as C code. -# o/$(MODE)/test/libc/release/metal.ok: \ -# test/libc/release/metal.sh \ -# o/$(MODE)/examples/hello.com \ +# o/$(MODE)/test/libc/release/metal.ok: \ +# test/libc/release/metal.sh \ +# o/$(MODE)/examples/hello.com \ # o/$(MODE)/tool/build/blinkenlights.com.dbg # @$(COMPILE) -ASHTEST -tT$@ $< # o/$(MODE)/test/libc/release/emulate.ok: \ -# test/libc/release/emulate.sh \ -# o/$(MODE)/examples/hello.com \ +# test/libc/release/emulate.sh \ +# o/$(MODE)/examples/hello.com \ # o/$(MODE)/tool/build/blinkenlights.com.dbg # @$(COMPILE) -ASHTEST -tT$@ $< .PHONY: o/$(MODE)/test/libc/release -o/$(MODE)/test/libc/release: \ - o/$(MODE)/test/libc/release/smoke.com \ - o/$(MODE)/test/libc/release/smoke.com.runs \ - o/$(MODE)/test/libc/release/smoke-nms.com \ - o/$(MODE)/test/libc/release/smoke-nms.com.runs \ - o/$(MODE)/test/libc/release/smokecxx.com \ - o/$(MODE)/test/libc/release/smokecxx.com.runs \ - o/$(MODE)/test/libc/release/smokeansi.com \ +o/$(MODE)/test/libc/release: \ + o/$(MODE)/test/libc/release/smoke.com \ + o/$(MODE)/test/libc/release/smoke.com.runs \ + o/$(MODE)/test/libc/release/smoke-nms.com \ + o/$(MODE)/test/libc/release/smoke-nms.com.runs \ + o/$(MODE)/test/libc/release/smoke-chibicc.com \ + o/$(MODE)/test/libc/release/smoke-chibicc.com.runs \ + o/$(MODE)/test/libc/release/smokecxx.com \ + o/$(MODE)/test/libc/release/smokecxx.com.runs \ + o/$(MODE)/test/libc/release/smokeansi.com \ o/$(MODE)/test/libc/release/smokeansi.com.runs diff --git a/third_party/chibicc/asm.c b/third_party/chibicc/asm.c index b88d13810..6ec640eaa 100644 --- a/third_party/chibicc/asm.c +++ b/third_party/chibicc/asm.c @@ -285,7 +285,7 @@ static void CouldNotAllocateRegister(AsmOperand *op, const char *kind) { static void PickAsmRegisters(Asm *a) { int i, j, m, pick, regset, xmmset, x87sts; - regset = 0b1111111111000111; // exclude bx,sp,bp + regset = 0b1111111111001111; // exclude bx,sp,bp xmmset = 0b1111111111111111; x87sts = 0b0000000011111111; regset ^= regset & a->regclob; // don't allocate from clobber list diff --git a/third_party/chibicc/chibicc.c b/third_party/chibicc/chibicc.c index c189381d5..c4b1a3c16 100644 --- a/third_party/chibicc/chibicc.c +++ b/third_party/chibicc/chibicc.c @@ -1,6 +1,7 @@ #include "libc/calls/calls.h" #include "libc/calls/struct/siginfo.h" #include "libc/calls/ucontext.h" +#include "libc/intrin/kprintf.h" #include "libc/runtime/gc.internal.h" #include "libc/runtime/runtime.h" #include "libc/x/x.h" @@ -264,9 +265,9 @@ static void parse_args(int argc, char **argv) { } else if (!strncmp(argv[i], "-x", 2)) { opt_x = parse_opt_x(argv[i] + 2); } else if (startswith(argv[i], "-Wa")) { - strarray_push_comma(&as_extra_args, argv[i]); + strarray_push_comma(&as_extra_args, argv[i] + 3); } else if (startswith(argv[i], "-Wl")) { - strarray_push_comma(&ld_extra_args, argv[i]); + strarray_push_comma(&ld_extra_args, argv[i] + 3); } else if (!strcmp(argv[i], "-Xassembler")) { strarray_push(&as_extra_args, argv[++i]); } else if (!strcmp(argv[i], "-Xlinker")) { @@ -447,8 +448,7 @@ static bool run_subprocess(char **argv) { _Exit(1); } // Wait for the child process to finish. - do - rc = wait(&ws); + do rc = wait(&ws); while (rc == -1 && errno == EINTR); return WIFEXITED(ws) && WEXITSTATUS(ws) == 0; } @@ -657,27 +657,28 @@ static void run_linker(StringArray *inputs, char *output) { if (!ld || !*ld) ld = "ld"; StringArray arr = {0}; strarray_push(&arr, ld); - strarray_push(&arr, "-o"); - strarray_push(&arr, output); strarray_push(&arr, "-m"); strarray_push(&arr, "elf_x86_64"); strarray_push(&arr, "-z"); strarray_push(&arr, "max-page-size=0x1000"); + strarray_push(&arr, "-static"); strarray_push(&arr, "-nostdlib"); strarray_push(&arr, "--gc-sections"); strarray_push(&arr, "--build-id=none"); strarray_push(&arr, "--no-dynamic-linker"); strarray_push(&arr, xasprintf("-Ttext-segment=%#x", IMAGE_BASE_VIRTUAL)); - strarray_push(&arr, "-T"); - strarray_push(&arr, LDS); - strarray_push(&arr, APE); - strarray_push(&arr, CRT); + /* strarray_push(&arr, "-T"); */ + /* strarray_push(&arr, LDS); */ + /* strarray_push(&arr, APE); */ + /* strarray_push(&arr, CRT); */ for (int i = 0; i < ld_extra_args.len; i++) { strarray_push(&arr, ld_extra_args.data[i]); } for (int i = 0; i < inputs->len; i++) { strarray_push(&arr, inputs->data[i]); } + strarray_push(&arr, "-o"); + strarray_push(&arr, output); handle_exit(run_subprocess(arr.data)); } diff --git a/third_party/xed/x86.h b/third_party/xed/x86.h index 26754c621..6cfe397ef 100644 --- a/third_party/xed/x86.h +++ b/third_party/xed/x86.h @@ -202,31 +202,33 @@ struct XedDecodedInst { struct XedOperands op; }; -forceinline void xed_operands_set_mode(struct XedOperands *p, int mmode) { - p->realmode = false; - switch (mmode) { - default: - case XED_MACHINE_MODE_LONG_64: - p->mode = XED_MODE_LONG; - return; - case XED_MACHINE_MODE_LEGACY_32: - case XED_MACHINE_MODE_LONG_COMPAT_32: - p->mode = XED_MODE_LEGACY; - break; - case XED_MACHINE_MODE_REAL: - p->realmode = true; - p->mode = XED_MODE_REAL; - break; - case XED_MACHINE_MODE_UNREAL: - p->realmode = true; - p->mode = XED_MODE_LEGACY; - break; - case XED_MACHINE_MODE_LEGACY_16: - case XED_MACHINE_MODE_LONG_COMPAT_16: - p->mode = XED_MODE_REAL; - break; - } -} +#define xed_operands_set_mode(p, machine_mode) \ + do { \ + struct XedOperands *__p = p; \ + __p->realmode = false; \ + switch (machine_mode) { \ + default: \ + case XED_MACHINE_MODE_LONG_64: \ + __p->mode = XED_MODE_LONG; \ + break; \ + case XED_MACHINE_MODE_LEGACY_32: \ + case XED_MACHINE_MODE_LONG_COMPAT_32: \ + __p->mode = XED_MODE_LEGACY; \ + break; \ + case XED_MACHINE_MODE_REAL: \ + __p->realmode = true; \ + __p->mode = XED_MODE_REAL; \ + break; \ + case XED_MACHINE_MODE_UNREAL: \ + __p->realmode = true; \ + __p->mode = XED_MODE_LEGACY; \ + break; \ + case XED_MACHINE_MODE_LEGACY_16: \ + case XED_MACHINE_MODE_LONG_COMPAT_16: \ + __p->mode = XED_MODE_REAL; \ + break; \ + } \ + } while (0) extern const char kXedErrorNames[]; extern const uint8_t kXedEamode[2][3]; diff --git a/third_party/xed/x86isa.h b/third_party/xed/x86isa.h index 9edfd26cc..3b03b3c6d 100644 --- a/third_party/xed/x86isa.h +++ b/third_party/xed/x86isa.h @@ -211,42 +211,44 @@ struct XedChipFeatures { uint64_t f[3]; }; -forceinline void xed_set_chip_modes(struct XedDecodedInst *d, int chip) { - switch (chip) { - case XED_CHIP_INVALID: - break; - case XED_CHIP_I86: - case XED_CHIP_I86FP: - case XED_CHIP_I186: - case XED_CHIP_I186FP: - case XED_CHIP_I286REAL: - case XED_CHIP_I286: - case XED_CHIP_I2186FP: - case XED_CHIP_I386REAL: - case XED_CHIP_I386: - case XED_CHIP_I386FP: - case XED_CHIP_I486REAL: - case XED_CHIP_I486: - case XED_CHIP_QUARK: - case XED_CHIP_PENTIUM: - case XED_CHIP_PENTIUMREAL: - case XED_CHIP_PENTIUMMMX: - case XED_CHIP_PENTIUMMMXREAL: - d->op.mode_first_prefix = 1; - break; - default: - break; - } - switch (chip) { - case XED_CHIP_INVALID: - case XED_CHIP_ALL: - case XED_CHIP_AMD: - break; - default: - d->op.is_intel_specific = 1; - break; - } -} +#define xed_set_chip_modes(d, chip) \ + do { \ + struct XedDecodedInst *__d = d; \ + switch (chip) { \ + case XED_CHIP_INVALID: \ + break; \ + case XED_CHIP_I86: \ + case XED_CHIP_I86FP: \ + case XED_CHIP_I186: \ + case XED_CHIP_I186FP: \ + case XED_CHIP_I286REAL: \ + case XED_CHIP_I286: \ + case XED_CHIP_I2186FP: \ + case XED_CHIP_I386REAL: \ + case XED_CHIP_I386: \ + case XED_CHIP_I386FP: \ + case XED_CHIP_I486REAL: \ + case XED_CHIP_I486: \ + case XED_CHIP_QUARK: \ + case XED_CHIP_PENTIUM: \ + case XED_CHIP_PENTIUMREAL: \ + case XED_CHIP_PENTIUMMMX: \ + case XED_CHIP_PENTIUMMMXREAL: \ + __d->op.mode_first_prefix = 1; \ + break; \ + default: \ + break; \ + } \ + switch (chip) { \ + case XED_CHIP_INVALID: \ + case XED_CHIP_ALL: \ + case XED_CHIP_AMD: \ + break; \ + default: \ + __d->op.is_intel_specific = 1; \ + break; \ + } \ + } while (0) extern const uint64_t kXedChipFeatures[XED_CHIP_LAST][3];