Commit Graph

2839 Commits

Author SHA1 Message Date
Christian Jullien
38dea90b2f OpenBSD: workaround missing SYS_gettid - WIP 2020-11-29 08:54:55 +01:00
Christian Jullien
dfe031caa6 OpenBSD: Fix crt startup names - WIP 2020-11-29 08:47:27 +01:00
Christian Jullien
4e12c2a6dc OpenBSD: improve partial support for x86_64 - WIP 2020-11-27 11:24:19 +01:00
grischka
cf8d9cf072 win64: fix pe_isafunc()
- tccpe.c: commit "tidy support for helper function" created
  STT_NOTYPE symbols and hence relied on ad-hoc detection which
  didn't work for x86_64 (as reported by Christian Jullien)

- tccgen.c: However to be more safe the helper symbols are
  now made STT_FUNC anyway (via new VT_ASM_FUNC).

Also:
- tcc.h: minor reorder
- riscv64-*, arm64-*, tccmacho.c: avoid some gcc format-warnings
  (mingw-gcc complains about "%llx" being "unknown conversion",
  although it does work since Vista or so)
2020-11-26 16:11:17 +01:00
Tyge Løvset
13a18906ef win32 tcc_libm.h: Implemented overrides for msvcrt.dll's frexp(), ldexp(), and fabs() with speedups 4.5X, 7.3X and 6.3X correspondingly.
This is the last commit on this for now.
2020-11-25 14:29:42 +01:00
grischka
4a42b0d95e tidy support for helper function such as memmove
tcc.h, tccgen.c: Introduce Sym *external_helper_sym(int v);
  to create an external reference with no specific type. This
  avoids type conflicts if the symbol is used from C too.
the other files: use it.
2020-11-24 11:47:02 +01:00
Tyge Løvset
2f78f54924 win32 libm: Final optimizations and added nan*() function. 2020-11-23 20:42:36 +01:00
Arthur Williams
3709f8de14 Treat func pointers with different return types as not compatible
Tcc considered function ptrs with different return types to be
compatible which disallowed some otherwise valid operations like:
`_Generic(foo, int(*)():0, void(*)(void):1)`
which would fail to compile with a error message of "type match twice"

This changed also required longjump's return type to be void and
munmap's to be int to be compatible with standard headers.
2020-11-22 16:30:34 -06:00
Tyge Løvset
3b1a42e734 Cleanup and minor fixes in win32/tcc/tcc_libm.h: truncf(), cbrtf(), exp2f(), log2f(), scalbln*() 2020-11-21 14:45:35 +01:00
Tyge Løvset
b11144d69c Overhauled WIN32 math and added missing functions:
1) Cleanup: moved function implementations from win32/include/math.h to win32/include/tcc/tcc_libm.h
2) Added missing math functions: MUSL: asinh(), acosh(), atanh(), scalbn(). My impl: log1p(), expm1(), ilogb(), scalbln(), nexttoward()
 - now only a few are missing: remquo(), remainder(), fma(), nan(), erf(), erfc(), nearbyint().
3) Added/defined all missing *f() and *l() math functions.
4) Added functions have short but accurate/fast implementations.
5) Added <tgmath.h> for all platforms. (not too useful, IMO, but is C99 standard).
2020-11-20 23:34:35 +01:00
Wirtos_new
82611f5e6d Fix char limits
By the standard CHAR_MIN and CHAR_MAX should be 0 and UCHAR_MAX respectively when char implementation is unsigned ( -fno-signed-char)
2020-11-17 09:06:48 +02:00
Tyge Løvset
1073b379c8 WIN32/64 only: Added C99 math functions: tgamma(), tgammaf(), lgamma(), lgammaf(), cbrt(), cbrtf(), log2(), log2f(), exp2(), exp2f().
The gamma() functions are accurate to 13 digits over the full range. Returns INFINITY / NAN identical to other compiler's math libs.

#include <stdio.h>
#include <math.h>

int main() {
  double x;
  for (x = -7.0; x <= 520.0; x += 1.0) {
    printf("tgamma/lgamma %6.3f: %20.13g  %20.13g\n", x/3, tgamma(x/3.0), lgamma(x/3.0));
  }
  printf("tgamma/lgamma %g: %20.13g  %20.13g\n", 0.00000234, tgamma(0.00000234), lgamma(0.00000234));
  printf("log2, exp2, cbrt, cbrt: %.15g %.15g %.15g %.15g\n", log2(1024), exp2(10), cbrt(-10), cbrt(10));
}
2020-11-14 00:02:58 +01:00
herman ten brugge
08d8015750 Fix fork problem 114_bound_signal
There is a race condition in old libc in fork().
The result was that 'end' was sometimes printed twice.

This did not happen with glibc-2.32.
2020-11-10 10:08:30 +01:00
herman ten brugge
54b4aa3cd6 Disable signals in bcheck.c pthread_create
glibc-2.32 contains new code to prevent a race conditions with signals
in pthread_create.
2020-11-09 10:57:14 +01:00
grischka
8b69059f66 three small fixes & three reverts
- tcc.h: msvc doesn't grok __func__ (reverts previous commit)

- tccgen.c: fortify tcc against bogus code:
  - n[sizeof({3;})]; // statement expression outside of function
  - f(){"123"4}; // tokens with values following each other
  (also, add "type defaults to int" warning for variables)

- tccpe.c: removed a check that caused BSS symbols not to be
  exported.  Whatever that check was meant to prevent.

- win32/build-tcc.bat: cmd.exe sometimes doesn't grok '-' in labels

- Revert "libtcc: no need to undef"
  This reverts commit 2b7aa2a1e1.
- Revert "tcc.h libtcc.c: remove unused defines"
  This reverts commit 985d963745.

The point of these "unused defines" is to be unused,  that is
to remind people not to use malloc but please to "use_tcc_malloc",
instead.
2020-11-02 18:08:56 +01:00
Kyryl Melekhin
2b7aa2a1e1 libtcc: no need to undef 2020-11-01 15:51:50 +00:00
Kyryl Melekhin
985d963745 tcc.h libtcc.c: remove unused defines
pretty sure that functions with use_* do not exist
and attempts to use the macros will fail to link
I could rename them to be more up to date like tcc_malloc,
but to me overloading the std calls with macros is
probably taking away control from the programmer,
so for the best is to just get rid of them.
2020-11-01 15:24:45 +00:00
John Scott
53587ee415
tcc_internal_error: print function with __func__.
Though __func__ was introduced in C99, this is
preferable to __FUNCTION__, an obsolete GCC extension.
Found with gcc -Wpedantic.
2020-10-18 17:54:01 -04:00
John Scott
558c6f56e2
tcc_mallocz: clear with memset only if nonzero size
Even if non-NULL, dereferencing the pointer from malloc(0)
or passing it to memset() may invoke undefined behavior.
2020-10-16 21:20:33 -04:00
grischka
72b520e709 tccgen: flex arrays etc.
Fixes potential writes past the allocated space with mostly
illegal flex array initializers. (60_errors_and_warnings.c
:test_var_array)

In exchange suspicious precautions such as section_reserve
or checks with sec->data_allocated were removed.  (There is
an hard check 'init_assert()' for now but it's meant to be
just temporary)

Also, instead of filling holes, always memset(0) structures
& arrays on stack.  Sometimes more efficient, sometimes isn't.
At least we can omit putting null initializers.

About array range inititializers:  Reparsing tokens has a
small problem with sideeffects, for example

   int c = 0, dd[] = { [0 ... 1] = ++c, [2 ... 3] = ++c };

Also, instead of 'squeeze_multi_relocs()', delete pre-existing
relocations in advance. This works even if secondary initializers
don't even have relocations, as with
    [0 ... 7] = &stuff,
    [4] = NULL

Also, in tcc.h: new macro "tcc_internal_error()"
2020-10-03 18:12:46 +02:00
grischka
40395511d7 Revert "function pointer compare"
Always fine to try out things but not everything must be shown
to the public. ;)

Also, AFAIK pointers must compare equal only if derived directly
from each other (for example by cast to void* and back).

This reverts commit 8f9bf3f223.
2020-10-03 17:40:22 +02:00
grischka
cdc3df949b tcc -bench: show text/data/bss binary output sizes 2020-10-02 19:50:45 +02:00
herman ten brugge
ae1796fc34 Fix testcase 114 for arm and apple
Arm has a problem with tls after a fork. The pthread_key_create seems to
be forgotten?

Apple has a problem with the exit(0) code in do_fork(). An IO mutex
is still held after a fork().
2020-10-02 17:50:31 +02:00
Michael Matz
78da4586a0 Fix tests2/120_alias.c on macos
While MacOS doesn't natively support the alias attribute, let's support
it with TCC anyway.  This means we need to make a decision if the
string in the alias attribute is decorated or not due to the implicit
underscore on MacOS.  To make life easier we decide that it's the C name,
i.e. without underscore, and so TCC needs to emit alias names with
underscore handling.

Irrespective of that the test case needs to deal with the underscore
itself for __asm__ renaming which is always requiring the assembler name.
2020-10-01 18:03:56 +02:00
herman ten brugge
0da93838c1 Init range wth symbols
The init range with symbols did only init the first value.
The relocation for all other symbols was missing.
Also see testcase.

tccgen.c:
- New function get_init_string
- Use macro processing in decl_designator for each init string
- Use get_init_string in decl_initializer_alloc

tccelf.c:
- Fix insertion sort in squeeze_multi_relocs

tests/tests2/90_struct-init.c:
- Add test case test_init_ranges
2020-10-01 17:50:20 +02:00
herman ten brugge
afc0917f88 Bound checking fixes
tccgen.c:
- Fix 'tcc -b conftest.s'
- Add offset during bound checking for struct return

lib/bcheck.c:
- Check overlap when reusing vla/alloca

arm-gen.c:
arm64-gen.c:
riscv64-gen.c:
lib/alloca86-bt.S:
- add space for vla/alloca during bound checking

tests/tests2/Makefile:
tests/tests2/121_struct_return:
tests/tests2/122_vla_reuse:
- New test cases with bound checking enabled to test vla and struct return
2020-10-01 17:09:09 +02:00
Michael Matz
352e1d0fc4 Reinstate attribute alias handling
commit 2a0167a merged alias and asm symbol renaming, but broke
semantics of aliases, see testcase.  Basically the difference between
the two is that an asm rename doesn't generate a new symbol, i.e. with

  int foo __asm__("bar");

all source reference to 'foo' will be to 'bar', nothing of the name
'foo' will remain in the object file, and for instance reference to
'foo' from other compilation units won't be resolved to this one.

Aliases OTOH create an additional symbol.  With:

  void target (void) { return; }
  void afunc (void) __attribute__((alias("target")));

reference to 'afunc' will remain 'afunc' in the object file.  It will
generate two symbols, 'afunc' and 'target' referring to the same entity.
This difference matters if other compilation units make references to
'afunc'.

A side requirement of this is that for alias to work that the target
symbol needs to be defined in the same unit.  For TCC we even require a
stricter variant: it must be defined before the alias is created.

Now, with this I merely re-instated the old flow of events before above
commit.  It didn't seem useful anymore to place both names in the
asm_label member of attributes, and the asm_label member of Sym now
again only needs the hold the __asm__ rename.

It also follows that tcc_predefs.h can't make use of attribute alias to
e.g. map __builtin_memcpy to __bound_memcpy (simply because the latter
isn't defined in all units), but rather must use __asm__ renaming, which
in turn means that the underscore handling needs to be done by hand.
2020-09-30 17:46:01 +02:00
herman ten brugge
727e24cb0a Add typedef debug info
tccgen.c:
- In tcc_get_debug_info mask VT_STORAGE instead of VT_EXTERN | VT_STATIC
- New function tcc_debug_typedef
- Call tcc_debug_typedef in decl0
2020-09-27 11:13:37 +02:00
herman ten brugge
8fd7a384e2 Fix bitfields init : tiny solution
tccgen.c: Check struct/union size for bitfield.
tests/tcctest.c: Add test code.
2020-09-25 12:23:48 +02:00
herman ten brugge
89ea62481d clang7 does not support zero sized structs
This happens with aarch64 on raspberry pi.
2020-09-21 15:55:58 +02:00
herman ten brugge
8f9bf3f223 function pointer compare
tccelf.c:
- Check if symbol is in data section and UNDEF. Then generate new
  relocation and let dynamic linker solve it.

tests/tests2/42_function_pointer.c:
- Add new test code
2020-09-21 09:18:48 +02:00
Christian Jullien
ffac4e7688 Herman proposed patch seems to fix clang issue on x64 Debian 2020-09-19 08:04:20 +02:00
grischka
8cb3e5368f bitfields init : tiny solution
This replaces commit 5c6356ff8e,
except the tests.
2020-09-18 23:31:34 +02:00
herman ten brugge
5c6356ff8e default-initialization of bitfields
The code:

struct bf_SS {unsigned int bit:1,bits31:31; };
void func(void) {
  struct bf_SS bf_finit = { .bit = 1 };
}

will not init bits31 to 0.

tccgen.c:
- check_bf: New function to check if bitfield is present in struct/union
- decl_initializer: Call check_bf and set value to 0 is bitfield found

tests/tcctest.c:
- Add struct bitfield test code
2020-09-18 19:20:57 +02:00
Christian Jullien
6d819d7267 Indentation was missing after empty string check was added by wanjochan. 2020-09-18 06:39:30 +02:00
wanjochan
bea7dcde86 check strlen(TCC_LIBTCC1) before tcc_add_support(s1, TCC_LIBTCC1) 2020-09-18 06:57:56 +08:00
grischka
aed5cd0ce9 revert 'no-libtcc1' feature
from 43ae350390
and a3578379fb

Sorry, but this feature appears to be rather a personal
experiment than generically interesting for other people.
Also those "other people" always have some interest in
TCC staying simple.

One can still avoid the startup code from libtcc1.a simply
by providing __start explicitly.

While at it: Tidy tccpe.c:pe_add_runtime(), somewhat.
2020-09-17 20:57:21 +02:00
grischka
4cd6298f9d libtcc1.c: cleanup fp2ull
- replace '>>' by '<<' in __fixunsxfdi (typo)
- replace 'long' by 'long long' (long may be 32 bits)
- return an overflow result more similar to GCC
2020-09-17 20:25:31 +02:00
Kyryl Melekhin
55b4754e84 Revert "add tests for float conversions to u64"
Because test's are not applicable in this case.

This reverts commit a5e714abec.
2020-09-17 07:12:49 +00:00
herman ten brugge
c9bbd4e707 Allow strings in __builtin_constant_p
tccgen.c:
- Fix handling __builtin_constant_p

tests/bug.c:
- Remove tst3

tests/tcctest.c:
- Add new tests for __builtin_constant_p
2020-09-17 09:11:10 +02:00
herman ten brugge
4a16bebfab Struct va_arg fix
lib/va_list.c:
- Handle struct {double, double} correctly

arm64-gen.c:
riscv64-gen.c:
x86_64-gen.c:
- Allow zero sized structs to work with va_arg

tcctest.c:
- Add new va_arg test code

test/bug.c:
- Remove tst2 va_arg test
2020-09-17 08:42:28 +02:00
herman ten brugge
757a97466f Fix testcase 114 for macos
lib/bt-exe.c:
- call __bound_init before sigset_exception_handler because sigaction
  is redirected.

tests/tests2/Makefile:
- run testcase 114 on macos again
2020-09-17 08:22:53 +02:00
Christian Jullien
13c66526ed Fix wanjochan commit 'when tcc1 omit: use main()' when tcc is bootstrapped by tcc on Windows. Variable must be NULL initialized 2020-09-17 06:52:39 +02:00
Christian Jullien
15182d7fdd Don't mix code and declaration 2020-09-17 06:51:59 +02:00
wanjochan
a3578379fb when tcc1 omit: use main() directly 2020-09-17 10:26:16 +08:00
wanjochan
7eebf614dc tcc c-tyle-compliance: space between if and ( 2020-09-17 06:54:13 +08:00
wanjochan
89935229a7 tcc_add_support(): void return for win 2020-09-17 00:53:50 +08:00
wanjochan
43ae350390 tcc_add_support(): skip when filename is empty 2020-09-17 00:47:32 +08:00
wanjochan
55eafa66b7 test 114: skip bcheck for osx (tcc -run not support fork with -b) 2020-09-17 00:43:19 +08:00
herman ten brugge
d55e586bc6 Fix boundschecking fork for macos/SELINUX
lib/bcheck.c:
- Use INIT_SEM for child process fork on macos

tests/tests2/Makefile:
- tcc -run does not work for fork due to SHARED mmap
2020-09-14 19:31:56 +02:00