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
622BOUND_PTR_INDIR(12)622BOUND_PTR_INDIR(12)
623BOUND_PTR_INDIR(16)623BOUND_PTR_INDIR(16)
624 624
625#if defined(__GNUC__) && (__GNUC__ >= 6)625/* Needed when using ...libtcc1-usegcc=yes in lib/Makefile */
626#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
626/*627/*
627 * At least gcc 6.2 complains when __builtin_frame_address is used with628 * At least gcc 6.2 complains when __builtin_frame_address is used with
628 * nonzero argument.629 * nonzero argument.
@@ -927,7 +928,7 @@ void __bound_siglongjmp(jmp_buf env, int val) · 298 unmodified lines
927}928}
928#endif929#endif
929 930
930#if defined(__GNUC__) && (__GNUC__ >= 6)931#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
931#pragma GCC diagnostic pop932#pragma GCC diagnostic pop
932#endif933#endif
933 934
lib/bt-log.c+10−0
@@ -13,6 +13,12 @@ int (*__rt_error)(void*, void*, const char *, va_list); · 12 unmodified lines
13# define DLL_EXPORT13# define DLL_EXPORT
14#endif14#endif
15 15
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
16DLL_EXPORT int tcc_backtrace(const char *fmt, ...)22DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
17{23{
18 va_list ap;24 va_list ap;
@@ -35,3 +41,7 @@ DLL_EXPORT int tcc_backtrace(const char *fmt, ...) · 16 unmodified lines
35 }41 }
36 return ret;42 return ret;
37}43}
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
1/* ---------------------------------------------- */10/* ---------------------------------------------- */
2/* This file implements:11/* This file implements:
3 * __builtin_ffs12 * __builtin_ffs
@@ -66,59 +75,90 @@ static const unsigned char table_2_64[] = { · 62 unmodified lines
66 75
67/* Returns one plus the index of the least significant 1-bit of x,76/* Returns one plus the index of the least significant 1-bit of x,
68 or if x is zero, returns zero. */77 or if x is zero, returns zero. */
69int __builtin_ffs (int x) { FFSI(x) }78int BUILTIN(ffs) (int x) { FFSI(x) }
79int BUILTIN(ffsll) (long long x) { FFSL(x) }
70#if __SIZEOF_LONG__ == 480#if __SIZEOF_LONG__ == 4
71int __builtin_ffsl (long x) { FFSI(x) }81int BUILTIN(ffsl) (long x) __attribute__((alias(BUILTINN(ffs))));
72#else82#else
73int __builtin_ffsl (long x) { FFSL(x) }83int BUILTIN(ffsl) (long x) __attribute__((alias(BUILTINN(ffsll))));
74#endif84#endif
75int __builtin_ffsll (long long x) { FFSL(x) }
76 85
77/* Returns the number of leading 0-bits in x, starting at the most significant86/* Returns the number of leading 0-bits in x, starting at the most significant
78 bit position. If x is 0, the result is undefined. */87 bit position. If x is 0, the result is undefined. */
79int __builtin_clz (unsigned int x) { CLZI(x) }88int BUILTIN(clz) (unsigned int x) { CLZI(x) }
89int BUILTIN(clzll) (unsigned long long x) { CLZL(x) }
80#if __SIZEOF_LONG__ == 490#if __SIZEOF_LONG__ == 4
81int __builtin_clzl (unsigned long x) { CLZI(x) }91int BUILTIN(clzl) (unsigned long x) __attribute__((alias(BUILTINN(clz))));
82#else92#else
83int __builtin_clzl (unsigned long x) { CLZL(x) }93int BUILTIN(clzl) (unsigned long x) __attribute__((alias(BUILTINN(clzll))));
84#endif94#endif
85int __builtin_clzll (unsigned long long x) { CLZL(x) }
86 95
87/* Returns the number of trailing 0-bits in x, starting at the least96/* Returns the number of trailing 0-bits in x, starting at the least
88 significant bit position. If x is 0, the result is undefined. */97 significant bit position. If x is 0, the result is undefined. */
89int __builtin_ctz (unsigned int x) { CTZI(x) }98int BUILTIN(ctz) (unsigned int x) { CTZI(x) }
99int BUILTIN(ctzll) (unsigned long long x) { CTZL(x) }
90#if __SIZEOF_LONG__ == 4100#if __SIZEOF_LONG__ == 4
91int __builtin_ctzl (unsigned long x) { CTZI(x) }101int BUILTIN(ctzl) (unsigned long x) __attribute__((alias(BUILTINN(ctz))));
92#else102#else
93int __builtin_ctzl (unsigned long x) { CTZL(x) }103int BUILTIN(ctzl) (unsigned long x) __attribute__((alias(BUILTINN(ctzll))));
94#endif104#endif
95int __builtin_ctzll (unsigned long long x) { CTZL(x) }
96 105
97/* Returns the number of leading redundant sign bits in x, i.e. the number106/* Returns the number of leading redundant sign bits in x, i.e. the number
98 of bits following the most significant bit that are identical to it.107 of bits following the most significant bit that are identical to it.
99 There are no special cases for 0 or other values. */108 There are no special cases for 0 or other values. */
100int __builtin_clrsb (int x) { if (x < 0) x = ~x; x <<= 1; CLZI(x) }109int BUILTIN(clrsb) (int x) { if (x < 0) x = ~x; x <<= 1; CLZI(x) }
110int BUILTIN(clrsbll) (long long x) { if (x < 0) x = ~x; x <<= 1; CLZL(x) }
101#if __SIZEOF_LONG__ == 4111#if __SIZEOF_LONG__ == 4
102int __builtin_clrsbl (long x) { if (x < 0) x = ~x; x <<= 1; CLZI(x) }112int BUILTIN(clrsbl) (long x) __attribute__((alias(BUILTINN(clrsb))));
103#else113#else
104int __builtin_clrsbl (long x) { if (x < 0) x = ~x; x <<= 1; CLZL(x) }114int BUILTIN(clrsbl) (long x) __attribute__((alias(BUILTINN(clrsbll))));
105#endif115#endif
106int __builtin_clrsbll (long long x) { if (x < 0) x = ~x; x <<= 1; CLZL(x) }
107 116
108/* Returns the number of 1-bits in x.*/117/* Returns the number of 1-bits in x.*/
109int __builtin_popcount (unsigned int x) { POPCOUNTI(x, 0x3f) }118int BUILTIN(popcount) (unsigned int x) { POPCOUNTI(x, 0x3f) }
119int BUILTIN(popcountll) (unsigned long long x) { POPCOUNTL(x, 0x7f) }
110#if __SIZEOF_LONG__ == 4120#if __SIZEOF_LONG__ == 4
111int __builtin_popcountl (unsigned long x) { POPCOUNTI(x, 0x3f) }121int BUILTIN(popcountl) (unsigned long x) __attribute__((alias(BUILTINN(popcount))));
112#else122#else
113int __builtin_popcountl (unsigned long x) { POPCOUNTL(x, 0x7f) }123int BUILTIN(popcountl ) (unsigned long x) __attribute__((alias(BUILTINN(popcountll))));
114#endif124#endif
115int __builtin_popcountll (unsigned long long x) { POPCOUNTL(x, 0x7f) }
116 125
117/* Returns the parity of x, i.e. the number of 1-bits in x modulo 2. */126/* Returns the parity of x, i.e. the number of 1-bits in x modulo 2. */
118int __builtin_parity (unsigned int x) { POPCOUNTI(x, 0x01) }127int BUILTIN(parity) (unsigned int x) { POPCOUNTI(x, 0x01) }
128int BUILTIN(parityll) (unsigned long long x) { POPCOUNTL(x, 0x01) }
119#if __SIZEOF_LONG__ == 4129#if __SIZEOF_LONG__ == 4
120int __builtin_parityl (unsigned long x) { POPCOUNTI(x, 0x01) }130int BUILTIN(parityl) (unsigned long x) __attribute__((alias(BUILTINN(parity))));
121#else131#else
122int __builtin_parityl (unsigned long x) { POPCOUNTL(x, 0x01) }132int 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
145int __builtin_ffs(int x) __attribute__((alias("__tcc_builtin_ffs")));
146int __builtin_ffsl(long x) __attribute__((alias("__tcc_builtin_ffsl")));
147int __builtin_ffsll(long long x) __attribute__((alias("__tcc_builtin_ffsll")));
148#endif
149int __builtin_clz(unsigned int x) __attribute__((alias("__tcc_builtin_clz")));
150int __builtin_clzl(unsigned long x) __attribute__((alias("__tcc_builtin_clzl")));
151int __builtin_clzll(unsigned long long x) __attribute__((alias("__tcc_builtin_clzll")));
152int __builtin_ctz(unsigned int x) __attribute__((alias("__tcc_builtin_ctz")));
153int __builtin_ctzl(unsigned long x) __attribute__((alias("__tcc_builtin_ctzl")));
154int __builtin_ctzll(unsigned long long x) __attribute__((alias("__tcc_builtin_ctzll")));
155int __builtin_clrsb(int x) __attribute__((alias("__tcc_builtin_clrsb")));
156int __builtin_clrsbl(long x) __attribute__((alias("__tcc_builtin_clrsbl")));
157int __builtin_clrsbll(long long x) __attribute__((alias("__tcc_builtin_clrsbll")));
158int __builtin_popcount(unsigned int x) __attribute__((alias("__tcc_builtin_popcount")));
159int __builtin_popcountl(unsigned long x) __attribute__((alias("__tcc_builtin_popcountl")));
160int __builtin_popcountll(unsigned long long x) __attribute__((alias("__tcc_builtin_popcountll")));
161int __builtin_parity(unsigned int x) __attribute__((alias("__tcc_builtin_parity")));
162int __builtin_parityl(unsigned long x) __attribute__((alias("__tcc_builtin_parityl")));
163int __builtin_parityll(unsigned long long x) __attribute__((alias("__tcc_builtin_parityll")));
123#endif164#endif
124int __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
115ATOMIC_GEN(uint64_t, 8, "q")115ATOMIC_GEN(uint64_t, 8, "q")
116#endif116#endif
117 117
118void __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
125void ATOMIC(signal_fence) (int memorder)
119{126{
120}127}
121 128
122void __atomic_thread_fence (int memorder)129void ATOMIC(thread_fence) (int memorder)
123{130{
124#if defined __i386__131#if defined __i386__
125 __asm__ volatile("lock orl $0, (%esp)");132 __asm__ volatile("lock orl $0, (%esp)");
@@ -134,7 +141,7 @@ void __atomic_thread_fence (int memorder) · 8 unmodified lines
134#endif141#endif
135}142}
136 143
137bool __atomic_is_lock_free(size_t size, void *ptr)144bool ATOMIC(is_lock_free) (unsigned long size, const volatile void *ptr)
138{145{
139 bool ret;146 bool ret;
140 147
@@ -151,3 +158,9 @@ bool __atomic_is_lock_free(size_t size, void *ptr) · 10 unmodified lines
151 }158 }
152 return ret;159 return ret;
153}160}
161
162#ifndef __TINYC__
163void __atomic_signal_fence(int memorder) __attribute__((alias("__tcc_atomic_signal_fence")));
164void __atomic_thread_fence(int memorder) __attribute__((alias("__tcc_atomic_thread_fence")));
165bool __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
234 size_t len = strlen (file->filename);234 size_t len = strlen (file->filename);
235 235
236 while (fgets(str, sizeof(str), fp) &&236 while (fgets(str, sizeof(str), fp) &&
237 (p = strstr(str, "0:File:")) == NULL);237 (p = strstr(str, "0:File:")) == NULL) {}
238 if ((p = strstr(str, "0:File:")) == NULL ||238 if ((p = strstr(str, "0:File:")) == NULL ||
239 strncmp (p + strlen("0:File:"), file->filename, len) != 0 ||239 strncmp (p + strlen("0:File:"), file->filename, len) != 0 ||
240 p[strlen("0:File:") + len] != ' ')240 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