Skip to content

Commit 5df5e2e

Browse files
committed
zig.h: drop dependency on deleted compiler_rt functions
It turns out we did use these in the C backend. However, it's really just as easy, if not easier, to replicate the logic directly in C. Synchronizes stage1/zig.h to make sure the bootstrap doesn't depend on these functions either. The actual zig1 tarball is unmodified because regenerating it is unnecessary in this instance.
1 parent 69f3986 commit 5df5e2e

File tree

2 files changed

+37
-58
lines changed

2 files changed

+37
-58
lines changed

lib/zig.h

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -809,15 +809,13 @@ static inline bool zig_addo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8
809809
#endif
810810
}
811811

812-
zig_extern int32_t __addosi4(int32_t lhs, int32_t rhs, int *overflow);
813812
static inline bool zig_addo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
814813
#if zig_has_builtin(add_overflow) || defined(zig_gcc)
815814
int32_t full_res;
816815
bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
817816
#else
818-
int overflow_int;
819-
int32_t full_res = __addosi4(lhs, rhs, &overflow_int);
820-
bool overflow = overflow_int != 0;
817+
int32_t full_res = (int32_t)((uint32_t)lhs + (uint32_t)rhs);
818+
bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0;
821819
#endif
822820
*res = zig_wrap_i32(full_res, bits);
823821
return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
@@ -835,15 +833,13 @@ static inline bool zig_addo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8
835833
#endif
836834
}
837835

838-
zig_extern int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow);
839836
static inline bool zig_addo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
840837
#if zig_has_builtin(add_overflow) || defined(zig_gcc)
841838
int64_t full_res;
842839
bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
843840
#else
844-
int overflow_int;
845-
int64_t full_res = __addodi4(lhs, rhs, &overflow_int);
846-
bool overflow = overflow_int != 0;
841+
int64_t full_res = (int64_t)((uint64_t)lhs + (uint64_t)rhs);
842+
bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0;
847843
#endif
848844
*res = zig_wrap_i64(full_res, bits);
849845
return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
@@ -917,15 +913,13 @@ static inline bool zig_subo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8
917913
#endif
918914
}
919915

920-
zig_extern int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow);
921916
static inline bool zig_subo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
922917
#if zig_has_builtin(sub_overflow) || defined(zig_gcc)
923918
int32_t full_res;
924919
bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
925920
#else
926-
int overflow_int;
927-
int32_t full_res = __subosi4(lhs, rhs, &overflow_int);
928-
bool overflow = overflow_int != 0;
921+
int32_t full_res = (int32_t)((uint32_t)lhs - (uint32_t)rhs);
922+
bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0;
929923
#endif
930924
*res = zig_wrap_i32(full_res, bits);
931925
return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
@@ -943,15 +937,13 @@ static inline bool zig_subo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8
943937
#endif
944938
}
945939

946-
zig_extern int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow);
947940
static inline bool zig_subo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
948941
#if zig_has_builtin(sub_overflow) || defined(zig_gcc)
949942
int64_t full_res;
950943
bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
951944
#else
952-
int overflow_int;
953-
int64_t full_res = __subodi4(lhs, rhs, &overflow_int);
954-
bool overflow = overflow_int != 0;
945+
int64_t full_res = (int64_t)((uint64_t)lhs - (uint64_t)rhs);
946+
bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0;
955947
#endif
956948
*res = zig_wrap_i64(full_res, bits);
957949
return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
@@ -1755,15 +1747,13 @@ static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint
17551747
#endif
17561748
}
17571749

1758-
zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
17591750
static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17601751
#if zig_has_builtin(add_overflow)
17611752
zig_i128 full_res;
17621753
bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
17631754
#else
1764-
int overflow_int;
1765-
zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1766-
bool overflow = overflow_int != 0;
1755+
zig_i128 full_res = (zig_i128)((zig_u128)lhs + (zig_u128)rhs);
1756+
bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0;
17671757
#endif
17681758
*res = zig_wrap_i128(full_res, bits);
17691759
return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
@@ -1781,15 +1771,13 @@ static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint
17811771
#endif
17821772
}
17831773

1784-
zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
17851774
static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17861775
#if zig_has_builtin(sub_overflow)
17871776
zig_i128 full_res;
17881777
bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
17891778
#else
1790-
int overflow_int;
1791-
zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1792-
bool overflow = overflow_int != 0;
1779+
zig_i128 full_res = (zig_i128)((zig_u128)lhs - (zig_u128)rhs);
1780+
bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0;
17931781
#endif
17941782
*res = zig_wrap_i128(full_res, bits);
17951783
return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);

stage1/zig.h

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#elif defined(__mips__)
4141
#define zig_mips32
4242
#define zig_mips
43+
#elif defined(__or1k__)
44+
#define zig_or1k
4345
#elif defined(__powerpc64__)
4446
#define zig_powerpc64
4547
#define zig_powerpc
@@ -72,6 +74,9 @@
7274
#elif defined (__x86_64__) || (defined(zig_msvc) && defined(_M_X64))
7375
#define zig_x86_64
7476
#define zig_x86
77+
#elif defined(__I86__)
78+
#define zig_x86_16
79+
#define zig_x86
7580
#endif
7681

7782
#if defined(zig_msvc) || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
@@ -82,9 +87,7 @@
8287
#define zig_big_endian 1
8388
#endif
8489

85-
#if defined(_AIX)
86-
#define zig_aix
87-
#elif defined(__MACH__)
90+
#if defined(__MACH__)
8891
#define zig_darwin
8992
#elif defined(__DragonFly__)
9093
#define zig_dragonfly
@@ -114,20 +117,14 @@
114117
#define zig_wasi
115118
#elif defined(_WIN32)
116119
#define zig_windows
117-
#elif defined(__MVS__)
118-
#define zig_zos
119120
#endif
120121

121122
#if defined(zig_windows)
122123
#define zig_coff
123124
#elif defined(__ELF__)
124125
#define zig_elf
125-
#elif defined(zig_zos)
126-
#define zig_goff
127126
#elif defined(zig_darwin)
128127
#define zig_macho
129-
#elif defined(zig_aix)
130-
#define zig_xcoff
131128
#endif
132129

133130
#define zig_concat(lhs, rhs) lhs##rhs
@@ -390,12 +387,16 @@
390387
#define zig_trap() __asm__ volatile(".word 0x0")
391388
#elif defined(zig_mips)
392389
#define zig_trap() __asm__ volatile(".word 0x3d")
390+
#elif defined(zig_or1k)
391+
#define zig_trap() __asm__ volatile("l.cust8")
393392
#elif defined(zig_riscv)
394393
#define zig_trap() __asm__ volatile("unimp")
395394
#elif defined(zig_s390x)
396395
#define zig_trap() __asm__ volatile("j 0x2")
397396
#elif defined(zig_sparc)
398397
#define zig_trap() __asm__ volatile("illtrap")
398+
#elif defined(zig_x86_16)
399+
#define zig_trap() __asm__ volatile("int $0x3")
399400
#elif defined(zig_x86)
400401
#define zig_trap() __asm__ volatile("ud2")
401402
#else
@@ -422,6 +423,8 @@
422423
#define zig_breakpoint() __asm__ volatile("break 0x0")
423424
#elif defined(zig_mips)
424425
#define zig_breakpoint() __asm__ volatile("break")
426+
#elif defined(zig_or1k)
427+
#define zig_breakpoint() __asm__ volatile("l.trap 0x0")
425428
#elif defined(zig_powerpc)
426429
#define zig_breakpoint() __asm__ volatile("trap")
427430
#elif defined(zig_riscv)
@@ -804,15 +807,13 @@ static inline bool zig_addo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8
804807
#endif
805808
}
806809

807-
zig_extern int32_t __addosi4(int32_t lhs, int32_t rhs, int *overflow);
808810
static inline bool zig_addo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
809811
#if zig_has_builtin(add_overflow) || defined(zig_gcc)
810812
int32_t full_res;
811813
bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
812814
#else
813-
int overflow_int;
814-
int32_t full_res = __addosi4(lhs, rhs, &overflow_int);
815-
bool overflow = overflow_int != 0;
815+
int32_t full_res = (int32_t)((uint32_t)lhs + (uint32_t)rhs);
816+
bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0;
816817
#endif
817818
*res = zig_wrap_i32(full_res, bits);
818819
return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
@@ -830,15 +831,13 @@ static inline bool zig_addo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8
830831
#endif
831832
}
832833

833-
zig_extern int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow);
834834
static inline bool zig_addo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
835835
#if zig_has_builtin(add_overflow) || defined(zig_gcc)
836836
int64_t full_res;
837837
bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
838838
#else
839-
int overflow_int;
840-
int64_t full_res = __addodi4(lhs, rhs, &overflow_int);
841-
bool overflow = overflow_int != 0;
839+
int64_t full_res = (int64_t)((uint64_t)lhs + (uint64_t)rhs);
840+
bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0;
842841
#endif
843842
*res = zig_wrap_i64(full_res, bits);
844843
return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
@@ -912,15 +911,13 @@ static inline bool zig_subo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8
912911
#endif
913912
}
914913

915-
zig_extern int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow);
916914
static inline bool zig_subo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
917915
#if zig_has_builtin(sub_overflow) || defined(zig_gcc)
918916
int32_t full_res;
919917
bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
920918
#else
921-
int overflow_int;
922-
int32_t full_res = __subosi4(lhs, rhs, &overflow_int);
923-
bool overflow = overflow_int != 0;
919+
int32_t full_res = (int32_t)((uint32_t)lhs - (uint32_t)rhs);
920+
bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0;
924921
#endif
925922
*res = zig_wrap_i32(full_res, bits);
926923
return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
@@ -938,15 +935,13 @@ static inline bool zig_subo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8
938935
#endif
939936
}
940937

941-
zig_extern int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow);
942938
static inline bool zig_subo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
943939
#if zig_has_builtin(sub_overflow) || defined(zig_gcc)
944940
int64_t full_res;
945941
bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
946942
#else
947-
int overflow_int;
948-
int64_t full_res = __subodi4(lhs, rhs, &overflow_int);
949-
bool overflow = overflow_int != 0;
943+
int64_t full_res = (int64_t)((uint64_t)lhs - (uint64_t)rhs);
944+
bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0;
950945
#endif
951946
*res = zig_wrap_i64(full_res, bits);
952947
return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
@@ -1750,15 +1745,13 @@ static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint
17501745
#endif
17511746
}
17521747

1753-
zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
17541748
static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17551749
#if zig_has_builtin(add_overflow)
17561750
zig_i128 full_res;
17571751
bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
17581752
#else
1759-
int overflow_int;
1760-
zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1761-
bool overflow = overflow_int != 0;
1753+
zig_i128 full_res = (zig_i128)((zig_u128)lhs + (zig_u128)rhs);
1754+
bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0;
17621755
#endif
17631756
*res = zig_wrap_i128(full_res, bits);
17641757
return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
@@ -1776,15 +1769,13 @@ static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint
17761769
#endif
17771770
}
17781771

1779-
zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
17801772
static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17811773
#if zig_has_builtin(sub_overflow)
17821774
zig_i128 full_res;
17831775
bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
17841776
#else
1785-
int overflow_int;
1786-
zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1787-
bool overflow = overflow_int != 0;
1777+
zig_i128 full_res = (zig_i128)((zig_u128)lhs - (zig_u128)rhs);
1778+
bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0;
17881779
#endif
17891780
*res = zig_wrap_i128(full_res, bits);
17901781
return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
@@ -4213,7 +4204,7 @@ static inline void zig_loongarch_cpucfg(uint32_t word, uint32_t* result) {
42134204
#endif
42144205
}
42154206

4216-
#elif defined(zig_x86)
4207+
#elif defined(zig_x86) && !defined(zig_x86_16)
42174208

42184209
static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) {
42194210
#if defined(zig_msvc)

0 commit comments

Comments
 (0)