Commit Graph

302 Commits

Author SHA1 Message Date
grischka
aef64762c1 tccpp: #define max(a,b) && max /= 8;
This fixes 20a1ebf854
where the '/' above was lost when max() was defined

Also: ignore binary files with -E (for example 'tcc -lxxx -E ...')
2022-10-09 20:30:15 +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
herman ten brugge
76b88e22a4 Fix last __has_include update
After this last change the radare2 project did not compile anymore.

Start with BOL flag after include ends in tccpp.c.
Update testcase 18.
2022-09-12 11:37:47 +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
grischka
85c32ddd0b tccpp: integrate __has_include() more nicely
- make only one function from previously four pieces
- use parse_pp_string() for the normal cases "..." and <...>
- use the sanity check for computed include as end-condition too.
- use 's1' instead of 'tcc_state' when possible

117 insertions(+), 165 deletions(-), less 48 lines
2022-08-18 10:23:13 +02:00
Michael Matz
64f4b00d34 tccpp: Fix #ifdef in macro args
see testcase.  We have to "empty" the macro-stack string on
end_macro, as it may be 'tokstr_buf' which is going to be used
twice in recursive invocations of macro_subst_tok.  The uses aren't
overlapping but the first one needs to be properly finalized.
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
herman ten brugge
47dc6e6d7b Add __has_include and __has_include_next support
tcctok.h:
- Add __has_include_next

tccpp.c:
- New functions parse_include and get_include_file
- Changed expr_preprocess and preprocess
2022-08-14 12:17:31 +02:00
herman ten brugge
b7733aeef1 Fix wchar_t strings with 'x', 'u' and 'U' 2022-08-11 08:37:00 +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
Arthur Williams
8482682061 Define __TCC_BACKTRACE_ENABLED__ if backtrace is enabled
If the `-btN` was specified, then the __TCC_BACKTRACE_ENABLED__
macro will be defined. This provides a way for user code
to tell if tcc_backtrace is defined or not.
2022-07-23 15:12:15 -07: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
e588b65390
Fix wrong handling of strings which do not end properly at the end of line 2022-07-13 15:43:26 +08:00
herman ten brugge
53dd065daf Fix unicode compiler warning again 2022-05-14 15:55:27 +02:00
herman ten brugge
19cc80788d Fix unicode compiler warning
The compiler warned about the unicode_to_utf8 code.
I fixed it by giving an error. This is the same as gcc does.
2022-05-12 11:18:37 +02:00
grischka
56481d554f bored...
/* Use "-g" as alias for "-g1". Use "-g0" to disable debug */

So not using -g is now the alias for -g0 ?!?

This reverts commit 8759b2581d.
This reverts commit 3ce7bc6efc.
This reverts commit 5fb582ab7f.
This reverts commit aea68dbb40.
This reverts commit fa9c31c3db.
This reverts commit b3bebdb20a.
This reverts commit ecf8e5a00e.
This reverts commit fe6b5c08dc.
This reverts commit e2e5377e7b.
This reverts commit 1cd7998905.
2022-05-09 22:19:15 +02:00
Detlef Riekenberg
3ce7bc6efc Accept option "-Os" and define "__OPTIMIZE_SIZE__"
Some headers and source code change macros and
implementation, when __OPTIMIZE_SIZE__ is defined.

This does not change our generated code.

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-10 17:32:38 +02:00
mingodad
1645616843 Revert "Move almost all global variables to TCCState, actually all tests pass on Ubuntu 18.04 x86_64"
This reverts commit af686a796b.
2021-10-22 07:39:54 +02:00
mingodad
2ce2dbcb09 Revert "Fix some errors on arm64-asm.c, rename some variables, fix several code style declarations"
This reverts commit 61537d899a.
2021-10-22 07:39:26 +02:00
mingodad
61537d899a Fix some errors on arm64-asm.c, rename some variables, fix several code style declarations 2021-10-22 07:20:00 +02:00
mingodad
af686a796b Move almost all global variables to TCCState, actually all tests pass on Ubuntu 18.04 x86_64 2021-10-21 20:09:42 +02:00
grischka
e97e108d0b tccpe: #pragma pack(push) - support this form 2021-09-01 19:36:24 +02:00
grischka
dda95e9b0b WAIT/POST_SEM(): generalize interface (and more)
Currently used only with 'tcc_compile_sem' to protect
tcc_compile(),  but can be used with other semaphores

Also fix deadlock when tcc_enter_state() is called
recursively for the same state, for example with
tcc_warning() from #pragma comment(option,"...")

Also:
- libtcc.c: error1(): use cstr_[v]printf()
- tcc.h: set TCC_USING_DOUBLE_FOR_LDOUBLE for macho-arm64
  (rather than for macho-X86_64)
- tcc.h: define TCC_TARGET_MACHO on __APPLE__ by default
- tcc.h: cleanup TCCState, move DEFASM token stuff to tcctok.h
- tccgen.c: more static
- Makefile/tcc.c: review githash
- tccpe/tcctools: use read() instead of fgets() in pe_load_def()
  (all files opened by tcc for reading are now read via 'int fd')
- configure/win32: don't preset CONFIG_TCCDIR (to allow to override it)
- tcc.c -bench: do not include output/run-time
2021-08-01 20:33:31 +02:00
grischka
4b2c6cf3a4 -W[no-]error...: features by shrinkage
136 insertions(+), 226 deletions(-), less 90 lines (except tests)
only one set_flag() function required
* can now turn off specific errors: -Werror -Wno-error=option
* new interface: tcc_warning_c(warn_option)("format", args...);
* new warning: -Wdiscarded-qualifiers (on by default)
* new variable 'warn_all' for conditional warnings with -Wall
see also the tests
2021-07-31 21:45:33 +02:00
Steffen Nurpmeso
0c16762418 -W[no-]error=X: gcc compat: when disabling X again, do not unset the warning 2021-07-27 19:48:29 +02:00
Steffen Nurpmeso
a7a13896b3 Add -W[no-]error=OPTION specific abortions 2021-07-27 00:37:22 +02:00
grischka
eadcee6501 macos: yet another tbd adjustment
configure:
- re-enable apple M1-arm64
- however, with --cpu=x86_64,  set -arch in CFLAGS/LDFLAGS too
  (assume rosetta)

Makefile:
- re-enable osx cross-test (Please do not disable tests)

tcc.h
- set TCC_IS_NATIVE for TCC_TARGET_MACHO on __APPLE__
- apply TCC_USING_DOUBLE_FOR_LDOUBLE for x86_64-osx

libtcc.c:
- cleanup tcc_add_file_internal()
- new function char *tcc_load_text(int fd); and use it for tbd files
2021-07-25 20:55:05 +02:00
Arthur Williams
ee75521dd5 Support -M, -MM, and -MMD
Add support for some more options to generate dependency fragments.
-M is likes -MD but doesn't compile anything so the command won't fail
when just supplied a file without all the relevant libs.
-MM and -MMD are like their counterparts but skip system libs.

Note the behavior of -MD has changed to include system libs. Use -MMD
for the old behavior. This matches gcc's corresponding flags
2021-07-01 01:44:34 -05:00
herman ten brugge
0378168c13 Fix macro processing
The code:

printf("%d\n", CALL(CONST));

did not work because we did not check for TOK_PLCHLDR.
2021-05-04 11:22:11 +02:00
grischka
48df89e10e stdatomics: tidy & cleanup
- remove any error messages that were just for debugging the
  templates really
- don't use c99 in tcc (array designators etc.)
- remove memory model type (cannot be an own type really)
  and move memory model defines from built-in to stdatomics.h
- apply normal casts to non-pointer atomic_function arguments
- tidy the library support
- add some tests for errors/warnings

Also:
- Makefile: move GIT_HASH stuff from CFLAGS to DEFINES and into
  main section (away from what is included by tests for example)
- tccelf.c/tccgen.c: avoid some warnings with newer GCC
2021-04-09 10:47:35 +02:00
grischka
72f1dea537 tccelf: use rodata_section, use more rodata in tcc itself
libtcc.c: add -Wwrite-strings to -Wall
tccgen.c: ro float-consts, string-consts, ro arrays if base type is
tccpe.c: merge IAT with rodata
tccrun.c: mprotect rodata accordingly. free section data after copy
x86_64.c: do not use got for static data.
tcc -bench: show data.rw/ro

Probably STB_LOCAL should never get to put_got_entry(), and currently
it doesn't seem to happen (See "Hack Alarm" there)

Other files: use more ro-data in tinycc
2021-02-18 14:43:34 +01:00
CodeHz
a1d10c8bde
Fix unnecessary symbol collision 2021-02-18 20:41:47 +08:00
Dmitry Selyutin
a110287c31 stdatomic: atomic builtins parsing support 2021-01-26 22:37:44 +03:00
grischka
1ed4b6ba1a debug_modes, re-unalign, cleanups
tccgen.c: debug_modes
- don't waste debug function calls during normal execution.
libtcc.c:
- mem_debug: no C99 features in tcc please, for example
  ({compound expressions}): do not use.
tccgen.c: struct_layout:
- unaligned access is completely ok for most targets.
- Moreover the patch was triggering single byte mode even
  for normal aligned access (as with tcc's SymAttr)

static Sym label: don't do this

arm-gen.c:
- use some #ifdefs to explain some code
tccpp.c:
- cleanup UCN chars
libtcc.c:
- replace openbsd library search
configure:
- cleanup strip fallouts
tccgen.c:
- expr_cond(): remove an exotic optimization that eventually
  got fixed to do the contrary by a gv(RC_InT)
- pop_local_syms(): remove some args
- init_putv() : use write##le functions to avoid cross-compiler
  unaligned access
- __bt_init(): remove unused param 'mode'
2021-01-24 18:00:33 +01:00
herman ten brugge
38eaf9b3a7 Fix compiler warnings utf8 code 2021-01-23 19:21:33 +01:00
Petr Skocik
ffb95c2e0c Better handling of UCNs in strings
As the standard requires, take 4 hex digits after the \u opener of a
Universal Character Name, or take 8 hex digits after \U, but reject
smaller counts and don't consume more (https://port70.net/~nsz/c/c11/n1570.html#6.4.3,
https://port70.net/~nsz/c/c99/n1256.html#6.4.3).

The unicode codepoint used to get truncated to 1 byte. Now it gets expanded into UTF-8,
matching gcc & clang behavior on Linux.

TODO: Universal character names should also be supported in identifiers,
as in, e.g., char \u010dau_sv\u011bte[]="čau_světe";
2021-01-18 00:49:24 +01:00
grischka
aeb8f427e2 tccgen: introduce TOK_NEG for unary minus
for floats (currently only).  On x86_64 uses built-in fp
constants (in libtcc1.c) to avoid multiple anonymous
instances.

Also: win32/i386: use __alloca for big struct stack store
- use new function int tok_alloc_const(const char*);
- change alloca86.S to preserve EDX

tccelf.c: fix a warning with 'roinf_use'
2021-01-06 01:44:22 +01:00
Danny Milosavljevic
c5428cd19c
arm-asm: Add parse_operand, Operand
tccpp: Allow '#' token to reach the assembler.
2021-01-03 02:34:10 +01:00
grischka
ea82d0826d tccpp: cleanup target-os defines
moved target_machine defines to the <target>-gen.c files.

Also:
- c2str.c moved into conftest.c
- tccdefs.h ; defined(__TINYC__) && !defined(_LOCORE) removed
  (in tinycc __TINYC__ is always defined and _LO... is never.)
- stddef.h : too many #ifdefs, removed
- tccgen.c:stabs: support win32 long doubles aka doubles.
- win32: math.h/tcc_libm.h: fix pointer mismatch in modfl
- tccpp.c: increment include_stack_ptr after the file was
  actually found otherwise it would print
  "in file included from <itself>: file not found..."
2020-12-31 02:03:31 +01:00
grischka
9f6b65230a include/tccdefs.h: moved and use it
tcc_define_symbol(): now only for -D on command line
include/tccdefs.h: converted to strings and compiled into
the tcc executable.  Can be disabled with
    ./configure --config-predefs=no
In this case include/tccdefs.h is loaded at runtime.  This is
default for other build-methods (build-tcc.bat) also (to avoid
some complexity).

Also:
- lib/Makefile: fix typo
- tcc.h : avoid _strto(u)i64 (for TCC on WIN98/2K)
- tccpp.h:cstr_printf() : workaround incompatible vsnprintf's
  (generally faster too)
2020-12-22 11:06:19 +01:00
grischka
e2e62fcb8b replace native platform macros in the compiler
- The compiler should not use these
- However tccrun.c & libtcc1.a files should use these
Also:
- use s1->loaded_dlls for loaded dlls instead of dlopens
- alpine musl: fully supported now and tested
- ./configure ...
   --config-backtrace=no : disable backtraces
   --config-bcheck=no : disable bcheck
- tests:dlltest: enable by default
- tccrun.c : simplify mmaps
- __builtin_alloca : always use asm-alias (instead of #define)
- tccpe.c : use write32le
2020-12-17 12:39:16 +01:00
herman ten brugge
6eef0e35b4 OpenBSD: testcases
Fix all testcases for openbsd except dlltest.

Remember dlopen calls and use them to fix openbsd dlsym problem
Use crtbeginS.o/crtendS.o for DLL for bsd
Do not include -ldl for __NetBSD__
Redirect malloc, realloc, ..., free for bsd
Align stack in tests/asm-c-connect-*.c for x86_64
Remove -B in tests/pp/Makefile (not supported on bsd)
2020-12-16 17:52:59 +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
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
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
20fa63488a Fix bounds checking
i386-gen.c:
- Fix large stack size alloca code.
  The returned value of alloca was not used corectly.

libtcc.c:
- Use __SIZE_TYPE__ for __builtin_offsetof

tccpp.c:
- Fix __MAYBE_REDIR and abort builtins.

tests/tests2/Makefile
- Run 117_gcc_test also with bound checking enabled
  This found the above problems.
2020-07-07 21:10:51 +02:00
herman ten brugge
fc05da3c0b Fix alloca and arm problems
alloca is only defined for i386 and x86_64
arm has __aeabi_ prefixes for mem... calls
2020-07-06 20:10:56 +02:00
Michael Matz
40671f76e4 Fix parsing of .s files
those aren't preprocessed, but our use of a fake file in
preprocess_start requires inline stack processing (which isn't done
without preprocessing).  Just don't try to setup anything requiring
preprocessing at all in this case.
2020-07-06 18:12:35 +02:00
Michael Matz
de06193d88 tccpp: Add -bench -E mode
for benchmarking the preprocessor, without having to use the PP_BENCH
macro.  No output will be produced for '-E -bench', so it's the raw
tokenization/preprocess speed.
2020-07-06 17:41:05 +02:00
grischka
2a0167adfe __builtin_/__bound_: A 'stay tiny' approach to the matter
tccgen.c:
- cleanup __builtin_... stuff
- merge __attribute((alias("sym"))) with __asm__("sym")
    Now one cannot have both, however for alias underscores are
    added if enabled.  For __asm__ they aren't.
tccpp.c:
- extend tcc_predefs accordingly.  Was generated with
  'cd tests/misc && tcc -run c2str.c tcc_predef.h tcc_predefs'
xxx-gen.c:
- move bcheck setjmp test to tccgen.c:gbound_args()
i386-gen.c:
- create win32 compatible stack space for big structures
tcctest.c:
- some cleanup + nicer output
2020-07-06 13:42:02 +02:00