Commit Graph

708 Commits

Author SHA1 Message Date
grischka
452045422b win32: pe dwarf sections & option -g.pdb
On windows, create a .pdb file with option "-g.pdb"
Such executables created by tcc can be debugged with
"ollydbg" or "x64dbg"

This currently relies on the 3rd party tool cv2pdb from
    https://github.com/rainers/cv2pdb
which again relies on
    mspdbsrv.exe mspdbcore.dll msobj80.dll mspdb80.dll
from a MSVC installation.

cv2pdb.exe + the ms* files may be put in the path or in the
same directory as tcc.exe.
2023-09-06 22:42:44 +02:00
grischka
cd75ca692a stuff & fixes
libtcc.c:
- revert "Small patch to allow..." (someone's personal easteregg)
  (see da3a763e97)
- check return value from macho_load_tbd/dylib

tcc.c:
- remove help for "not yet implemented" option

tccelf.c:
- check PIE's for "unresolved symbols"

tccgen.c:
- avoid int->double->int cast
  (see a46372e910)
- fix constant propagation with pseudo long doubles
  (must mask out VT_LONG from type)
- cleanup find_field() (again)

tccpp.c:
- disallow strings and double constants in #if expressions

win32/include/uchar.h:
- change file mode
2023-09-06 22:42:30 +02:00
grischka
3f3cbb51ed bcheck: remove "tcc_location()" & x86_64 double fix
using (modified) tcc_backtrace() instead.
Also

Also fix the original bug with doubles on x86_64.
(which was not caused by incr_offset() actually).
See 598134fff6

Also cleanup on_exit() stuff
From fef701b57f
2023-09-06 22:42:21 +02:00
herman ten brugge
598134fff6 Undo part of incr_offset patch.
The incr_offset offset code was not working with bounds checking.
So I reverted part of tccgen.c.
See new test code 132.

Also added some debugging code that prints location of
bounds checking calls. Needed this to find the problem.
See lib/bcheck.c, lib/bt-dll.c, lib/bt-exe.c, lib/bt-log.c, tccrun.c
2023-08-30 16:10:39 +02:00
grischka
c29420ab0d tccgen: update "Fix invalid load generated by gfunc_return()"
tccgen.c:
- new function incr_offset(int) to increment a lvalue
- use it in gv/vstore to load/store from/to two-word types
- use it to advance the pointer to struct fields
- use it to load/store structs passed in registers
- structs: always assume that reg-classes of registers are 2^n
- adjust stack space when regsize > sizeof the_struct

x86_64-gen.c:
- return regsize=16 for VT_QLONG/QFLOAT

i386-gen.c:
- pass structs of size(8) as two VT_INT rather than one VT_LLONG
  (both should work now)

fixes a82aff3337
fixes fd6d2180c5 (slightly)
2023-07-31 12:22:15 +02:00
herman ten brugge
fd6d2180c5 Align stack when returning structs.
The stack was not aligned when a returned structure was stored on stack.
This resulted in destoying of previous values stored on stack.
See testcase 119 (tst_struct_return_align) where value d is overwritten.
2023-07-31 09:24:06 +02:00
Yao Zi
a82aff3337
Fix invalid load generated by gfunc_return()
On backends that rely on gfunc_return() to handle structures
  returned in registers (like RISC-V), gfunc_return() may generate
  invalid loads for structures without VT_LOCAL and VT_LVAL. This
  commit fixes it and adds a regression test
  (131_return_struct_in_reg)
2023-07-02 04:02:36 +08:00
herman ten brugge
a46372e910 Add support for constant float compare 2023-05-22 15:09:59 +02:00
grischka
0ec3a40dfd about CONST_WANTED & VT_NONCONST
- integrate CONST_WANTED with nocode_wanted
- expr_const() checks VT_NONCONST
2023-04-25 15:43:38 +02:00
grischka
a045400501 CStrings leakless
avoid memory leaks with lost CStrings on stack after errors.

tccpp.c:
- use/abuse static Cstring tokcstr where possible
tccgen.c:
- use/abuse static Cstring initstr where possible
tcc.h/libtcc.a:
- add 'stk_data' array to track memory pointer on stack
- add macros stk_push/pop() and cstr_new/free_s()
tccasm.c:
- use that
- use char[16] instead of char* for op.constraint
2023-04-25 15:22:20 +02:00
grischka
40131b7e0f MEM_DEBUG thread-safe & tccdbg leakless
libtcc.c:
- guard tcc_realloc/free_debug() with a semaphore
tccdbg.c:
- allow tcc_debug_end() to be called twice safely
2023-04-25 15:22:15 +02:00
grischka
7916cf71cc tcc_error_noabort(): always use this unless compiling
This avoids 'exit(1)' with errors outside of compilation
(nasty in particular with libtcc usage)

As a sideeffect multiple errors can be seen for linker
errors (such as undefined symbols, relocation errors, ...)
2023-04-25 15:22:03 +02:00
grischka
19ef024aa9 lib/armeabi.c: fix zero from/to double conversion
tccgen.c:
- allow cross-compiling 0.0L (cross-compile tcc with tcc)
tccelf.c:
- use alignment of TLS section for PT_TLS
tccpp.c:
- support long double constants on systems that do not support
  long doubles (i.e. mark them (VT_DOUBLE | VT_LONG))
tccdefs.h:
  #define __has_feature() for android
  remove _unaligned (why define a msvc extension under !_WIN32)
2023-04-25 08:59:42 +02:00
herman ten brugge
6a24b762d3 Do not crach when compiling with libtcc. 2023-04-15 15:36:13 +02:00
grischka
19e3e10e4b small scopes cleanup etc.
tccgen.c:
- just track local_stack for small scopes (can't declare variables)
- fix a vla problem with nested scopes
- move debug N_L/RBRAC into curly braced block

Also:
- tccpp.c: move 'label_...' functions to tccgen.c
- tccpp.c: let get_tok_str() say "<no name>" for anonymous symbols
- tcctest.c: let __pa_symbol() work for memory > 2GB
- 119_random_stuff.c: revert strtoll test (no reason to test libc)
- tccdefs.h/tcctest.c: enable bit fncs for _WIN32
- Makefile:
  - use i686-linux-gnu instead of i386-linux-gnu for cross-i386
  - update 'make help'
  - revert umplicit 'make all' with 'make install' (but print warning)
2023-03-12 20:40:50 +01:00
grischka
5f08561e10 Revert ""Fix" nocode_wanted in expr_landor"
instead in vcheck_cmp(), pass the CODE_OFF_BIT to generators
unless other nocode purposes are set.

This reverts commit cad8739594.

Also in 128_run_atexit.c:
avoid line output disorder which may happen on windows when
tcc itself and runned code are using two different printf
implementations and tcc would print the "[Returns 1]" above
any output from the runned test.
2023-03-12 13:39:07 +01:00
herman ten brugge
cad8739594 Fix nocode_wanted in expr_landor
The generated code by yarpgen failed. I traced the problem to
expr_landor. The problem was that nocode_wanted was removing
too much code.
See also testcase 33.
2023-03-11 07:51:00 +01:00
herman ten brugge
88d5c70bdb Update casted boolean expressions
The problem was that enums can also be present in vset_VT_JMP.
Also see testcode.
2023-03-11 07:35:42 +01:00
Michael Matz
ef3eb02ccb Fix type of cond-op
the optimization of a ternary op with two boolean operands had
the same problem (as in the last commit) of loosing the type.
2023-03-10 17:23:55 +01:00
Michael Matz
96b31ba670 Fix casted boolean expressions
the casted type was lost when a delayed bool was finally converted
to a value.  See testcase, in the wrong case the '(unsigned int)' cast
was ignored, and hence the division was signed reulting in -1 instead of
the proper 0x7fffffff.
2023-03-10 16:49:27 +01:00
herman ten brugge
2cb8bddd82 Add attribute nodebug support and skip bitfield padding
The following code:
typedef struct {
  unsigned int a __attribute__((nodebug));
  unsigned int b;
  unsigned int : 32;
  unsigned int c;
} tst;

Supresses a and also suppresses bitfield padding.
So debugger shows only b and c in above example.
2023-03-10 12:41:43 +01:00
Michael Matz
2cf3a6eb4d Fix more jumpopts (in ternary op)
the code in expr_cond save nocode_wanted around some parts of
expression evaluation, but at the wrong spots.  If the evaluation
of the condition itself (e.g. in the testcase the first whole ternary
expression) resulted in CODE_OFF, then that was saved, and restored
before return, even if in-between codegen would have CODE_ON'ed already.
Thus the whole CODE_OFF state bled out to outside the expression
evaluation and also disabled the whole if-block.  Found by yarpgen v1
(seed 64).
2023-03-09 18:23:16 +01:00
herman ten brugge
3f0a1719dc Fix init bitfield padding with size 32/64
The init code did not work for padding bitfields with size 32 and 64.
2023-03-09 15:53:48 +01:00
Michael Matz
c771cb52fa Fix code suppression during nocode_wanted
we activate code (CODE_ON) only when the target labels are used,
which doesn't happen during nocode_wanted regions.  So, we cannot
just switch off code either during nocode_wanted regions, nothing
would switch it on again (except in the happy coincidences when we
outright save/restore nocode_wanted).  See the testcase for one
example, reduced from code generated by yarpgen: in
  ext = (xxx || 1)           // #1
        || ((xxx && 1) || 1) // #2
code is first suppressed in #1 normally, then (before this commit)
was suppressed via CODE_OFF during #2 (via indirect gjmp),
then the suppression from #1 was undone, but nothing undoes the
suppression from #2 anymore as everything therein was generated
while nocode_wanted was active.

So, we would either need to save/restore nocode_wanted around
some more expressions, activate CODE_ON also with unused labels (breaks
some optimizations we want), or deactivate CODE_OFF only when not
already in nocode_wanted state.  This does the latter.
2023-03-07 18:10:02 +01:00
Petr Skocik
ccc1651075 Create scopes for switches/whiles/do-whiles
The C standard has required this since at least C99.
A new scope should also be created for ifs, but that's currently
breaking tests2/122_vla_reuse.
2023-03-05 22:31:40 +01:00
Michael Matz
605538f46d Fix sym_scope of typedefs
Sym.sym_scope and Sym.f (FuncAttr) share space, so blindly setting
one clobbers the other.  Right now this only leads to missing errors
on incompatible typedefs (see testcase), which this commit fixes.

But it points to a larger problem:
Generally we can only manipulate Sym.f for anonymous and field symbols,
not for anything that has a top-level name (basically any proper decl),
because the latter use sym_scope.  Luckily the functions type always
contains an anonymous symbol (in sym->type.ref), so we can use that.
But some of the functions attributes actually _do_ apply to the decl,
not the type (e.g. always_inline), so we still have a problem possibly,
when we update an pre-existing type that may already be shared with
another decl.

Would need untangling and perhaps using accessor functions that check
that Sym.f and Sym.sym_scope aren't used for the same symbol.
2023-02-22 17:59:31 +01:00
herman ten brugge
79d439ee4f Allow const expr in builtin_frame_address/builtin_return_address 2023-01-16 08:37:47 +01:00
grischka
62d857a6f9 tccgen: better find_field() not found error messages
Also: return all of 'cumofs' (no extra '+' required at caller)
2022-12-22 16:17:39 +01:00
Petr Skocik
ea0c57e90a Allow _Static_assert declarations in structs
This usage has been part of the C grammar since C11
(http://port70.net/~nsz/c/c11/n1570.html#6.7.2.1) and is
also supported by gcc and clang.
2022-12-10 01:12:44 +01:00
noneofyourbusiness
ab39d34dde
produce a more informative error message when _Thread_local is used 2022-12-03 20:09:11 +01:00
herman ten brugge
6b398686d6 Fix bounds checking without -run on macos
Update tccrun.c to detect rebase for dwarf debug info.
Enabled testcase 126 on macos.
Add prologue_end/epilogue_begin supoort in tcc.h, tccdbg.c, tccgen.c.
2022-12-02 13:09:47 +01:00
herman ten brugge
afcdaf121a Fix __builtin_constant_p with comma expression
See: https://savannah.nongnu.org/bugs/?58606
and: mpfr-4.1.1/tests/tcmp_ui.c

The code '__builtin_constant_p ((i++, 7))' was not working.
I Fixed it in tccgen.c

I added a testcase in tests/tcctest.c. I also wanted to add a test like
'__builtin_constant_p ((10, 7))' but gcc needs -O1 for that to work.
clang (and now tcc) work as expected.
2022-11-29 00:56:26 -06:00
noneofyourbusiness
7d76420857
produce a more informative error message when parsing _Complex 2022-10-19 14:06:04 +02:00
herman ten brugge
79a8229fb5 Add atomic functions for arm/arm64/riscv
Make code more compatible with gcc. Change
__atomic_store
__atomic_load
__atomic_exchange
__atomic_compare_exchange

Also add (include/stdatomic.h, lib/stdatomic.c):
atomic_thread_fence
atomic_signal_fence
atomic_is_lock_free

And gcc extensions (tcctok.h, tccgen.c, lib/stdatomic.c):
__atomic_fetch_nand
__atomic_and_fetch
__atomic_sub_fetch
__atomic_or_fetch
__atomic_xor_fetch
__atomic_and_fetch
__atomic_nand_fetch

Add new file lib/atomic.S with assembly code for __atomic_compare_exchange_n
for arm/arm64/riscv. Also update lib/Makefile.

Update testcode in 124_atomic_counter and 125_atomic_misc to test new functions.
Also update tests/tests2/Makefile to run tests on arm/arm64/riscv.
2022-10-16 18:51:56 +02:00
grischka
bb80cbe0d9 tcctest.c: sizeof (long) != 8
Bad assumption make bad things happen:

    long d3;
    asm(..."1:\tdec %3\n\t" : ... "=&c" (d3)

Which wants 'dec RDI' but did 'dec EDI' on _WIN64.

Also:
- tcctest.c: enable more asm tests for win64
- configure: show errors if any with 'gcc conftest.c'
- tccgen.c: remove decl0(x, y, z)
2022-10-14 21:56:16 +02:00
grischka
e41730f11a - tcc -vv: show cross-libtcc1.a correctly (and more)
(As long as it is in the default install location and was not
moved elsewhere into the library search path manually)

Also:
- libtcc.c:
  - error1(): show correct line with "In file included from ..."
  - support "tcc -Bxxx -vv"
  - tcc_new()/tcc_compile(): Don't create elf sections for tcc -E
- tccdbg.c:
  - tcc -E -g : revert 1de025c13a
    Let's keep things simple, everybody understands 'do_debug'
    and dState is set by tcov too (but no debug sections).
- tccgen.c:
  - avoid the extra parameter for gind()
    (from c3e3a07ed4)
  - vla func params: use skip_or_save_block() and enable
    VT_LVAL (see 313855c232)
  - cleanup nocode_wanted a bit
- tccelf.c:
  - tccelf_end_file(): don't try to translate zero-sym relocs
    (seems to happen with asm "jmp 0x1000")
  - version_add(): do not make "ld-linux.so" DT_NEEDED
2022-09-24 09:46:17 +02:00
Michael Matz
fa25630ce4 vla: Fix check for vla used by autoconf
1) recursive types have no storage anymore, so a
     static int (*p)[x];
   declaration is just fine
2) since somewhen VT_VLA doesn't imply VT_ARRAY anymore, so we
   now need to check VLA in at least one place before checking
   test_lvalue.

((2) should probably be cleaned up again so that VLA does again imply
ARRAY)
2022-09-19 15:32:07 +02:00
grischka
20a1ebf854 tccpp : get rid of 'ch'
- also simplify parse(_line)_comment() and parse_pp_string()
- fixes a continuation problem in strings (see tcctest.c)
- no differences in performance could be observed

161 insertions(+), 246 deletions(-), less 85 lines
2022-08-18 11:34:36 +02:00
Michael Matz
aa9093a144 Fix null pointer constants
an expression like 'i*0', even though it's value is constant and
can be evaluated at compile time is not an integer constant expression,
and hence no null pointer constant, and therefore the conditional
operator doesn't select the other type.
2022-08-16 16:58:00 +02:00
Michael Matz
c3e3a07ed4 dce: Don't force unreachable case label to be live
if a switch is unreachable then so are the case labels.
Unlike normal labels they can't possibly be reached from not-yet
parsed code, so there's no reason to enable codegen again for those.
2022-08-16 15:59:41 +02:00
Michael Matz
1da92cdd93 Accept attributes after labels
newer GCC can associate attributes with statements and labels,
this implements only the latter (and ignores the attributes).
2022-08-16 15:59:41 +02:00
Michael Matz
98bab41cba Support asm goto
this adds generic support for asm goto, but I've tested only
on x86.
2022-08-16 15:59:41 +02:00
Michael Matz
154c3e7450 Fix unnamed bitfield struct members
they look similar to labels 'foobar : 32' (when foobar is a typedef),
but cannot be handled as such in parse_btype, the context is important
(labels can only happen when called from within 'block', aka decl0 in
VT_LOCAL context).  This got broken back in 2019 (1b57560).
2022-08-16 15:59:41 +02:00
Detlef Riekenberg
8b906427e3 tccgen: cleanup _Alignas implementation
Remove the unneeded compare, introduced in the recent _Alignas update.
sorry for that

--
bye bye Detlef
2022-08-04 12:02:52 +02:00
Vincent Lefevre
ef19711ba5 Use && rather than & for a Boolean operation. 2022-07-29 12:43:44 +02:00
Detlef Riekenberg
746560dc45 Do not fail with _Alignas(0) and _Alignas(1), used by autotools
Used by configure scripts, generated by newer GNU autotools
Autotools now checks for C11 and C99 compatibbility

--
bye bye Detlef
2022-07-28 18:30:54 +02:00
grischka
09808f327f tcc android-enabled (armeabi-v7a)
On an armeabi-v7a device (phone) in the termux app with
clang & make installed this passes all the tests.

Can be used as a cross compiler to create "native apps" as well.
Example 'config-extra.mak' for the cross arm-eabi-tcc:

  SYSROOT = <path_to_android_ndk...>/sysroot/usr
  TRIPLET = arm-linux-androideabi
  ANDRVER = 32

  ROOT-arm-eabi = $(SYSROOT)

  CRT-arm-eabi = {R}/lib/$(TRIPLET)/$(ANDRVER)
  LIB-arm-eabi = {B};{R}/lib/$(TRIPLET)/$(ANDRVER);{R}/lib/$(TRIPLET)
  INC-arm-eabi = {B}/lib/include;{R}/include/$(TRIPLET);{R}/include

  DEF-arm-eabi = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI
  DEF-arm-eabi += -DTARGETOS_ANDROID -DCONFIG_TCC_PIE -DCONFIG_NEW_DTAGS
  DEF-arm-eabi += -DCONFIG_TCC_ELFINTERP=\"/system/bin/linker\"

  # on unix replace ';' by ':'.
  $ ./configure && make cross-arm-eabi && make install
2022-07-24 09:51:32 +02:00
grischka
c81519e1c4 tccgen: don't generate elf-symbols under nocode/NODATA
seems that symbols under nocode/NODATA should not produce
elf symbols at all, not just elf symbols with no size.
See commit 4c82b00342

5 insertions(+), 5 deletions(-)
2022-07-24 09:41:57 +02:00
grischka
af1abf1f45 Revert "Fix wrong handling of strings..." (almost)
See commit e588b65390.
Was not "wrong" really, just different. But appears to be outdated.
Now disabled by default (top of tccpp.c: ACCEPT_LF_IN_STRINGS)
Also, in skipped code, just warn.

Also: cleanup "Optimize small structure copying on x86_64"
(commit 3715f1d7ee)
- remove some copy&paste coding (tccgen.c)
- RSI/RDI need to be preserved on windows
- simply don't use under bcheck (this is tinycc)
2022-07-16 00:41:37 +02:00
Ziyao
3715f1d7ee
Optimize small structure copying on x86_64 2022-07-09 12:53:29 +08:00