History

Allow building lib with gcc/clang

Needed when using ...libtcc1-usegcc=yes in lib/Makefile. lib/bcheck.c: Add __clang__ for pragma diagnostic lib/bt-log.c: Add pragma diagnostic as in lib/bcheck.c lib/builtin.c: Add a lot of alias code to allow building with gcc/clang lib/stdatomic.c: Same as lib/builtin.c lib/tcov.c: Avoid clang compiler warning

herman ten brugge committed 3 years ago

Commit 04810db1 parent d66a62f5 files · +94 −30Browse files

Unified · Split

5 files
  1. lib/bcheck.c +3 −2
  2. lib/bt-log.c +10 −0
  3. lib/builtin.c +64 −24
  4. lib/stdatomic.c +16 −3
  5. lib/tcov.c +1 −1
lib/bcheck.c+3−2
@@ -622,7 +622,8 @@ BOUND_PTR_INDIR(8) · 621 unmodified lines
622622 BOUND_PTR_INDIR(12)
623623 BOUND_PTR_INDIR(16)
624624
625−#if defined(__GNUC__) && (__GNUC__ >= 6)
625+/* Needed when using ...libtcc1-usegcc=yes in lib/Makefile */
626+#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
626627 /*
627628 * At least gcc 6.2 complains when __builtin_frame_address is used with
628629 * nonzero argument.
@@ -927,7 +928,7 @@ void __bound_siglongjmp(jmp_buf env, int val) · 298 unmodified lines
927928 }
928929 #endif
929930
930−#if defined(__GNUC__) && (__GNUC__ >= 6)
931+#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
931932 #pragma GCC diagnostic pop
932933 #endif
933934
lib/bt-log.c+10−0
@@ -13,6 +13,12 @@ int (*__rt_error)(void*, void*, const char *, va_list); · 12 unmodified lines
1313 # define DLL_EXPORT
1414 #endif
1515
16+/* Needed when using ...libtcc1-usegcc=yes in lib/Makefile */
17+#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
18+#pragma GCC diagnostic push
19+#pragma GCC diagnostic ignored "-Wframe-address"
20+#endif
21+
1622 DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
1723 {
1824 va_list ap;
@@ -35,3 +41,7 @@ DLL_EXPORT int tcc_backtrace(const char *fmt, ...) · 16 unmodified lines
3541 }
3642 return ret;
3743 }
44+
45+#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
46+#pragma GCC diagnostic pop
47+#endif
lib/builtin.c+64−24
@@ -1,3 +1,12 @@
1+/* uses alias to allow building with gcc/clang */
2+#ifdef __TINYC__
3+#define BUILTIN(x) __builtin_##x
4+#define BUILTINN(x) "__builtin_" # x
5+#else
6+#define BUILTIN(x) __tcc_builtin_##x
7+#define BUILTINN(x) "__tcc_builtin_" # x
8+#endif
9+
110 /* ---------------------------------------------- */
211 /* This file implements:
312 * __builtin_ffs
@@ -66,59 +75,90 @@ static const unsigned char table_2_64[] = { · 62 unmodified lines
6675
6776 /* Returns one plus the index of the least significant 1-bit of x,
6877 or if x is zero, returns zero. */
69−int __builtin_ffs (int x) { FFSI(x) }
78+int BUILTIN(ffs) (int x) { FFSI(x) }
79+int BUILTIN(ffsll) (long long x) { FFSL(x) }
7080 #if __SIZEOF_LONG__ == 4
71−int __builtin_ffsl (long x) { FFSI(x) }
81+int BUILTIN(ffsl) (long x) __attribute__((alias(BUILTINN(ffs))));
7282 #else
73−int __builtin_ffsl (long x) { FFSL(x) }
83+int BUILTIN(ffsl) (long x) __attribute__((alias(BUILTINN(ffsll))));
7484 #endif
75−int __builtin_ffsll (long long x) { FFSL(x) }
7685
7786 /* Returns the number of leading 0-bits in x, starting at the most significant
7887 bit position. If x is 0, the result is undefined. */
79−int __builtin_clz (unsigned int x) { CLZI(x) }
88+int BUILTIN(clz) (unsigned int x) { CLZI(x) }
89+int BUILTIN(clzll) (unsigned long long x) { CLZL(x) }
8090 #if __SIZEOF_LONG__ == 4
81−int __builtin_clzl (unsigned long x) { CLZI(x) }
91+int BUILTIN(clzl) (unsigned long x) __attribute__((alias(BUILTINN(clz))));
8292 #else
83−int __builtin_clzl (unsigned long x) { CLZL(x) }
93+int BUILTIN(clzl) (unsigned long x) __attribute__((alias(BUILTINN(clzll))));
8494 #endif
85−int __builtin_clzll (unsigned long long x) { CLZL(x) }
8695
8796 /* Returns the number of trailing 0-bits in x, starting at the least
8897 significant bit position. If x is 0, the result is undefined. */
89−int __builtin_ctz (unsigned int x) { CTZI(x) }
98+int BUILTIN(ctz) (unsigned int x) { CTZI(x) }
99+int BUILTIN(ctzll) (unsigned long long x) { CTZL(x) }
90100 #if __SIZEOF_LONG__ == 4
91−int __builtin_ctzl (unsigned long x) { CTZI(x) }
101+int BUILTIN(ctzl) (unsigned long x) __attribute__((alias(BUILTINN(ctz))));
92102 #else
93−int __builtin_ctzl (unsigned long x) { CTZL(x) }
103+int BUILTIN(ctzl) (unsigned long x) __attribute__((alias(BUILTINN(ctzll))));
94104 #endif
95−int __builtin_ctzll (unsigned long long x) { CTZL(x) }
96105
97106 /* Returns the number of leading redundant sign bits in x, i.e. the number
98107 of bits following the most significant bit that are identical to it.
99108 There are no special cases for 0 or other values. */
100−int __builtin_clrsb (int x) { if (x < 0) x = ~x; x <<= 1; CLZI(x) }
109+int BUILTIN(clrsb) (int x) { if (x < 0) x = ~x; x <<= 1; CLZI(x) }
110+int BUILTIN(clrsbll) (long long x) { if (x < 0) x = ~x; x <<= 1; CLZL(x) }
101111 #if __SIZEOF_LONG__ == 4
102−int __builtin_clrsbl (long x) { if (x < 0) x = ~x; x <<= 1; CLZI(x) }
112+int BUILTIN(clrsbl) (long x) __attribute__((alias(BUILTINN(clrsb))));
103113 #else
104−int __builtin_clrsbl (long x) { if (x < 0) x = ~x; x <<= 1; CLZL(x) }
114+int BUILTIN(clrsbl) (long x) __attribute__((alias(BUILTINN(clrsbll))));
105115 #endif
106−int __builtin_clrsbll (long long x) { if (x < 0) x = ~x; x <<= 1; CLZL(x) }
107116
108117 /* Returns the number of 1-bits in x.*/
109−int __builtin_popcount (unsigned int x) { POPCOUNTI(x, 0x3f) }
118+int BUILTIN(popcount) (unsigned int x) { POPCOUNTI(x, 0x3f) }
119+int BUILTIN(popcountll) (unsigned long long x) { POPCOUNTL(x, 0x7f) }
110120 #if __SIZEOF_LONG__ == 4
111−int __builtin_popcountl (unsigned long x) { POPCOUNTI(x, 0x3f) }
121+int BUILTIN(popcountl) (unsigned long x) __attribute__((alias(BUILTINN(popcount))));
112122 #else
113−int __builtin_popcountl (unsigned long x) { POPCOUNTL(x, 0x7f) }
123+int BUILTIN(popcountl ) (unsigned long x) __attribute__((alias(BUILTINN(popcountll))));
114124 #endif
115−int __builtin_popcountll (unsigned long long x) { POPCOUNTL(x, 0x7f) }
116125
117126 /* Returns the parity of x, i.e. the number of 1-bits in x modulo 2. */
118−int __builtin_parity (unsigned int x) { POPCOUNTI(x, 0x01) }
127+int BUILTIN(parity) (unsigned int x) { POPCOUNTI(x, 0x01) }
128+int BUILTIN(parityll) (unsigned long long x) { POPCOUNTL(x, 0x01) }
119129 #if __SIZEOF_LONG__ == 4
120−int __builtin_parityl (unsigned long x) { POPCOUNTI(x, 0x01) }
130+int BUILTIN(parityl) (unsigned long x) __attribute__((alias(BUILTINN(parity))));
121131 #else
122−int __builtin_parityl (unsigned long x) { POPCOUNTL(x, 0x01) }
132+int BUILTIN(parityl) (unsigned long x) __attribute__((alias(BUILTINN(parityll))));
133+#endif
134+
135+#ifndef __TINYC__
136+#if defined(__GNUC__) && (__GNUC__ >= 6)
137+/* gcc overrides alias from __builtin_ffs... to ffs.. so use assembly code */
138+__asm__(".globl __builtin_ffs");
139+__asm__(".set __builtin_ffs,__tcc_builtin_ffs");
140+__asm__(".globl __builtin_ffsl");
141+__asm__(".set __builtin_ffsl,__tcc_builtin_ffsl");
142+__asm__(".globl __builtin_ffsll");
143+__asm__(".set __builtin_ffsll,__tcc_builtin_ffsll");
144+#else
145+int __builtin_ffs(int x) __attribute__((alias("__tcc_builtin_ffs")));
146+int __builtin_ffsl(long x) __attribute__((alias("__tcc_builtin_ffsl")));
147+int __builtin_ffsll(long long x) __attribute__((alias("__tcc_builtin_ffsll")));
148+#endif
149+int __builtin_clz(unsigned int x) __attribute__((alias("__tcc_builtin_clz")));
150+int __builtin_clzl(unsigned long x) __attribute__((alias("__tcc_builtin_clzl")));
151+int __builtin_clzll(unsigned long long x) __attribute__((alias("__tcc_builtin_clzll")));
152+int __builtin_ctz(unsigned int x) __attribute__((alias("__tcc_builtin_ctz")));
153+int __builtin_ctzl(unsigned long x) __attribute__((alias("__tcc_builtin_ctzl")));
154+int __builtin_ctzll(unsigned long long x) __attribute__((alias("__tcc_builtin_ctzll")));
155+int __builtin_clrsb(int x) __attribute__((alias("__tcc_builtin_clrsb")));
156+int __builtin_clrsbl(long x) __attribute__((alias("__tcc_builtin_clrsbl")));
157+int __builtin_clrsbll(long long x) __attribute__((alias("__tcc_builtin_clrsbll")));
158+int __builtin_popcount(unsigned int x) __attribute__((alias("__tcc_builtin_popcount")));
159+int __builtin_popcountl(unsigned long x) __attribute__((alias("__tcc_builtin_popcountl")));
160+int __builtin_popcountll(unsigned long long x) __attribute__((alias("__tcc_builtin_popcountll")));
161+int __builtin_parity(unsigned int x) __attribute__((alias("__tcc_builtin_parity")));
162+int __builtin_parityl(unsigned long x) __attribute__((alias("__tcc_builtin_parityl")));
163+int __builtin_parityll(unsigned long long x) __attribute__((alias("__tcc_builtin_parityll")));
123164 #endif
124−int __builtin_parityll (unsigned long long x) { POPCOUNTL(x, 0x01) }
lib/stdatomic.c+16−3
@@ -115,11 +115,18 @@ ATOMIC_GEN(uint32_t, 4, "l") · 114 unmodified lines
115115 ATOMIC_GEN(uint64_t, 8, "q")
116116 #endif
117117
118−void __atomic_signal_fence (int memorder)
118+/* uses alias to allow building with gcc/clang */
119+#ifdef __TINYC__
120+#define ATOMIC(x) __atomic_##x
121+#else
122+#define ATOMIC(x) __tcc_atomic_##x
123+#endif
124+
125+void ATOMIC(signal_fence) (int memorder)
119126 {
120127 }
121128
122−void __atomic_thread_fence (int memorder)
129+void ATOMIC(thread_fence) (int memorder)
123130 {
124131 #if defined __i386__
125132 __asm__ volatile("lock orl $0, (%esp)");
@@ -134,7 +141,7 @@ void __atomic_thread_fence (int memorder) · 8 unmodified lines
134141 #endif
135142 }
136143
137−bool __atomic_is_lock_free(size_t size, void *ptr)
144+bool ATOMIC(is_lock_free) (unsigned long size, const volatile void *ptr)
138145 {
139146 bool ret;
140147
@@ -151,3 +158,9 @@ bool __atomic_is_lock_free(size_t size, void *ptr) · 10 unmodified lines
151158 }
152159 return ret;
153160 }
161+
162+#ifndef __TINYC__
163+void __atomic_signal_fence(int memorder) __attribute__((alias("__tcc_atomic_signal_fence")));
164+void __atomic_thread_fence(int memorder) __attribute__((alias("__tcc_atomic_thread_fence")));
165+bool __atomic_is_lock_free(unsigned long size, const volatile void *ptr) __attribute__((alias("__tcc_atomic_is_lock_free")));
166+#endif
lib/tcov.c+1−1
@@ -234,7 +234,7 @@ static void merge_test_coverage (tcov_file *file, FILE *fp, · 233 unmodified lines
234234 size_t len = strlen (file->filename);
235235
236236 while (fgets(str, sizeof(str), fp) &&
237− (p = strstr(str, "0:File:")) == NULL);
237+ (p = strstr(str, "0:File:")) == NULL) {}
238238 if ((p = strstr(str, "0:File:")) == NULL ||
239239 strncmp (p + strlen("0:File:"), file->filename, len) != 0 ||
240240 p[strlen("0:File:") + len] != ' ')

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t