Commit Graph

441 Commits

Author SHA1 Message Date
Petr Skocik 070646b790 Recognize C11' _Alignof 2018-12-12 20:12:03 +01:00
Michael Matz c4787e3626 Fix type completion for array types as well
like qualifier merging the array sizes are merged as well
for the operands of ?:, and they must not statically influence
the types of decls.

Also fix some style problems, spaces aren't stinky :)
2018-11-30 23:43:30 +01:00
Petr Skocik f7779efe58 Fix incorrect one-sided void handling in ?:
Also make the case when one side in ?: is a ptr
and the other is neither a ptr nor a null-ptr constant
fail cleanly instead of segfaulting.
2018-11-29 13:40:48 +01:00
Petr Skocik 49dfb5755a Make fn designators in ?: decay to ptrs
This
    _Generic((__typeof(1?f:f)*){0}, void (**)(void): (void)0);
should compile like it does in gcc and clang.
2018-11-29 12:43:01 +01:00
Petr Skocik 3058d4116e Fix (Cexpr?struct1:struct2).member
Tcc used to fail this with `lvalue expected`
if the expression was a compile-time constant.
2018-11-29 10:26:56 +01:00
Petr Skocik 9d44b02a49 Fix the fix on type combining (e0012c2)
char **argv;
	    _Generic(argv, char**: (void)0);
	    _Generic(0?(char const*)0:argv[0], char const*: (void)0);
	    _Generic(argv, char**: (void)0);

    would fail because the type of argv would get modified by the
    ternary. Now allocate a separate type on the value stack to
    prevent this error.
2018-11-29 10:26:35 +01:00
Petr Skocik e0012c2767 Fix ptr type combining inside the ternary operator
Make it match http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6
(or http://port70.net/~nsz/c/c11/n1570.html#6.5.15p6).
2018-11-28 10:36:58 +01:00
Petr Skocik c81116e29a Make casts lose top-level qualifiers
TODO: also make them lose lvalue status
2018-11-20 19:24:24 +01:00
Petr Skocik f85b1e393f Always allow ({ }) in the ctrl-expr of _Generic
tcc would reject e.g.,
    void f(){ struct {_Bool x:_Generic(({0;}),default:1);} my_x; }
with `expected constant`. This patch makes it accept it.

(The patch also makes tcc's _Generic a little more "generic" than that
 of gcc and clang in that that tcc now also accepts
`struct {_Bool x:_Generic(({0;}),default:1);} my_x;` in file scope
while gcc and clang don't, but I think there's no harm in that
and gcc and clang might as well accept it in filescope too, given
that they have no problem with
e.g., `/*filescope:*/int x=1, y=2, z=_Generic(x+y, int:3);`)
2018-11-13 12:51:16 +01:00
Petr Skocik 314843ffc3 Fix how _Generic treats pointers to arrays.
_Generic should distinguish pointers to differently sized
arrays such as `int(*)[2]` and `int(*)[4]`.
2018-11-12 20:52:14 +01:00
Petr Skocik 73ca09ff32 Fix array pointer stringification.
`int (*)[4];` should not be stringified as `int *[4];`
Add stringification tests.
2018-11-12 20:50:51 +01:00
Michael Matz 3b9c3fd186 Fix noreturn in main()
ISO C requires 'main' falling through the end without explicit
returns to implicitely return 0 (if declared as returning int).
2018-11-03 22:17:20 +01:00
Michael Matz 61ba9f2299 Check for void type in top-level decls
give an error message from the parser on things like "void x;" instead
of relying on codegen erroring out.
2018-08-03 22:51:35 +02:00
Michael Matz 22420ee1ee Fix misparsed type in presence of attributes
The type within the cast (int (__attribute__((foo)) *)(void))
was misparsed because of the presence of the attribute (parse_btype
prematurely concluded that (__attribute__() *) is a type.

Also see testcase.  This construct is used in sqlite it seems.
2018-07-28 18:55:54 +02:00
Jonathan Newman 0edbed1d52 Implement __attribute__((nodecorate))
Prevent any decoration that would otherwise affect an exported
PE function. For example, given the following:

__declspec(dllexport) __stdcall
int decorated(int arg) {
    return 0;
}

__declspec(dllexport) __stdcall __attribute__((nodecorate))
int undecorated(int arg) {
    return 0;
}

The following exported functions can now be seen in the DLL:
_decorated@4
undecorated

The attribute is recognised for all targets but only
affects PE codegen. I'm not sure whether this would be
useful for other targets; its intended use was to allow
the creation of a DLL matching an existing set of signatures.
2018-07-22 00:54:01 +01:00
grischka 8f6fcb709a misc fixes
misc fixes including:
- tcc.c: fix "tcc -vv" for libtcc1.a on win32/PE
- tccelf.c: fix a crash when GOT has no relocs (witn -nostdlib)
- tccelf.c: fix stab linkage for zero n_strx
- tccgen.c: fix stdcall decoration for array parameters
    int __stdcall func(char buf[10]) is _func@4 (was _func@12)
- tccgen.c: fix static variables with nocode/nodata_wanted
    see tests2/96_nodata_wanted.c
- tccrun.c: align sections using sh_addralign (for reliable function_alignment)
- tests2/Makefile sort 100 after 99
- win32/include/sys/stat.h fix _stat and _wstat
- x86_64-gen.c: win64/gfunc_call: fix a bug with xmmN register args
    previously overwrote valid other xmmN registers eventually
2018-06-01 12:52:01 +02:00
grischka 2b155a8c16 tccgen.c: fix warning for incompatible struct- and function pointers
see tests2/60_errors_and_warnings.c
2018-06-01 12:41:21 +02:00
Michael Matz 671dcace82 Implement function alignment via attributes
which requires being able to emit an arbitrary number of NOP
instructions, which is also implemented here.  For x86 we
could emit other sequences but these are the easiest.
2018-04-06 23:02:42 +02:00
Petr Skocik ef668aae1e Don't fail on const/restrict/static/* inside []
This patch makes tcc ignore them.

Normally (as per the C standard), They should
be only applicable inside parameter arrays
and affect (const/restrict) the pointer the
array gets converted to.

[matz: fix formatting, add volatile handling, add testcase,
add comment about above deficiency]
2018-04-01 00:48:09 +02:00
Petr Skocik d6d3cf00ec patch type_to_str to handle complex function-ptr decls better
Code like:

    #include <signal.h>
    int main() { _Generic(signal, int: 0); }

should fail with
    error: type 'extern void (*(int, void (*)(int)))(int)' does not match any association
not
    error: type 'extern void *(int)(int, void *(int))' does not match any association

[matz: fix formatting, fix function-to-pointer decay for operands of
_Generic, add testcase for this]
2018-04-01 00:38:11 +02:00
Michael Matz f0a25ca263 Fix shortening casts of long long
see added testcase.
2018-03-31 21:52:20 +02:00
Michael Matz 7ad2cf8d68 Code suppression fixes
See adjusted testcase, a lone break; in a do while loop was
incorrectly disabling the exit test.
2018-01-05 02:19:26 +01:00
Michael Matz 414b224efa Accept more floating point constant expressions
the rules for constant expressions in static initializers are more
relaxed than for integer constant expressions.  We need to accept
0.0/0.0 in static initializers (in non-static initializers the potential
exceptions need to be raised though, so no translation-time calculation
then).
2017-12-25 12:44:29 +01:00
Michael Matz 9e47b18229 Make type of __nan__ __inf__ and __snan__ be float
so that those builtins can be used directly for the C99 NAN and
INFINITY math.h macros.
2017-12-24 13:16:09 +01:00
grischka d348a9a51d final update for 0.9.27
tccgen.c:
- fix ldouble asm hack
- fix a VLA problem on Win64 (also x86_64-gen.c)
- patch_type(): make sure that no symbol ever changes
  from global to static

tcc.c:
- tcc -vv: print libtcc1.a path also on win32

tccpe.c, tcctools.c:
- use unix LF mode to for .def output files (that is for
  creating reproducible output trees)

Makefile:
- suppress some warnings when makeinfo is missing
- call 'which install' only on win32

tests/Makefile:
- change PATH only on WINNT systems (i.e. not if cross-compiling
  on linux for win32)
- asm-c-connect.test: slim output and do diff

tccrun.c tccpe.c *-link.c:
- integrate former 'pe_relocate_rva()' into normal relocation
  This also fixes linkage of the unwind data on WIN64 for -run
  (reported by Janus Lynggaard Thorborg)

tccasm.c, tests/tcctest.c:
- fix dot (sym_index of -1 crashed in put_elf_reloc)
- massage .set a bit (see test)

other:
- #define SECTION_ABS removed
- ST_DATA Section *strtab_section: removed
- put_extern_sym2(): take int section number

Conflicts:
	tccelf.c
	tccpe.c

Conflicts:
	tccelf.c
2017-12-12 17:57:20 +01:00
grischka 1a4d4b76e8 tccgen_begin/end_file
This is supposed to make compilation and linking with
multiple source files (tcc f1.c f2.S ...) behave just
the same as linking object files.

tccgen.c:put_extern_sym2():
- use put_elf_sym to enter new symbols unconditionally

tccelf.c:
- save section state before compilation
- disable symbol hashing during compilation
- merge symbols and update relocations after compilation

tccpe.c:
- re-create s1->uw_sym for each compilation (because it
  may change)
2017-12-12 17:33:37 +01:00
grischka 3ae1a2af1c tccgen: unify type redefinition checks
tccgen.c:
- improved function patch_storage() and added new function
  patch_type() for more consistent type redefinition and
  instance redefinition checks.
2017-12-05 02:06:26 +01:00
grischka 877e164d6a tccasm: use global(_symbol)_stack
* removed asm_label stack
* removed asm_free_labels() post-processing
* using "impossible C type" for asm labels (VT_ASM)
* tccgen.c:update_storage(): use it to refresh symbol attributes
* tccelf.c:find_elf_sym(): ignore STB_LOCAL symbols
* tccgen.c:unary(): asm symbols are supposed to be undeclared in C
2017-12-05 01:54:49 +01:00
Michael Matz 9e0d23cc47 tccasm: Unify C and asm symbol table
This makes the asm symbols use the same members as the C symbols
for global decls, e.g. using the ELF symbol to hold offset and
section.  That allows us to use only one symbol table for C and
asm symbols and to get rid of hacks to synch between them.

We still need some special handling for symbols that come purely
from asm sources.
2017-11-27 04:59:29 +01:00
Michael Matz e7c71e2473 tccasm: synch C and asm symtab tighter
See testcase.  The C and asm symtab are still separate,
but integrated tighter: the asm labels are only synched at file
end, not after each asm snippet (this fixes references from one
to another asm block), the C and asm syms are synched both ways,
so defining things in asm and refering from C, or the other way
around works.  In effect this model reflects what happens with
GCC better.

For this the asm labels aren't using the C label namespace anymore,
but their own, which increases the size of each TokenSym by a pointer.
2017-11-22 17:57:43 +01:00
grischka da8c62f75d various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
  cygwin's "GCC for Win32 Toolchain"
      ./configure --cross-prefix=i686-w64-mingw32-
  or with an existing tcc:
      ./configure --cc=<old-tccdir>/tcc.exe

tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)

tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)

tests:
- remove -I "expr" diff option to allow tests with
  busybox-diff.

libtcc.c, tcc.c:
- removed -iwithprefix option.  It is supposed to be
  combined with -iprefix which we don't have either.

tccgen.c:
- fix assignments and return of 'void', as in
     void f() {
         void *p, *q;
         *p = *q:
         return *p;
     }
  This appears to be allowed but should do nothing.

tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
  This reverts commit 1d5e386b0a.

  The patch was giving tcc's system includes priority over -I which
  is not how it should be.

tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
  used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
  (weak) symbols

tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
  (it was so once except the RX mapping wasn't used at all).

tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.

tccasm.c:
- keep forward asm labels static, otherwise they will endup
  in dynsym eventually.

configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
      make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
  a cross compiler for example
      make cross-arm
      make install
- use name <target>-libtcc1.a

build-tcc.bat:
- add  options: -clean, -b bindir
2017-10-11 18:13:43 +02:00
Larry Doolittle 1b6806e5bb Spelling fixes
Comments only, no change to functionality
2017-09-24 18:03:26 -07:00
grischka 1443039416 'long' review
add some features for more complete 'long' support

tcc.h:
- use LONG_SIZE=4/8 instead of TCC_LONG_ARE_64_BIT
tccgen.c:
- add ptrdiff_type, update size_type
- support shift and ?: operations
- support long enum types
- display 'long' from type_to_str
- nwchar_t is unsigned short on windows
- unrelated: use memcpy in init_putv for long doubles to avoid
  random bytes in the image (if tcc was compiled by gcc) for
  diff purposes.
tccpp.c:
- make parse_number return correct types
- improve multi-character-constants 'XX' 'abcd'
Changelog:
- update
2017-09-24 18:57:48 +02:00
matthias c18fc950d7 Revert "simplify VT_LONG parsing"
Too simple long parsion.
Take me a long long long time to see my mistake,
Sorry

(long long long wasn't see as an error)
This reverts commit a4cd2805f9.
2017-07-25 22:04:24 +02:00
Matthias Gatto a4cd2805f9 simplify VT_LONG parsing 2017-07-25 18:56:41 +02:00
Matthias Gatto b72cddaa6e remove inside_generic hack
define uint64_t and int64_t as 'long' instead of 'long long'
when __LP64__ is define.
2017-07-24 11:52:15 +02:00
grischka 4b3c6e74ab tccgen: nodata_wanted fix, default ONE_SOURCE, etc...
tccgen.c:
  doubles need to be aligned, on ARM.  The section_reserve()
  in init_putv does not do that.
-D ONE_SOURCE: is now the default and not longer needed. Also,
  tcc.h now sets the default native target.  These both make
  compiling tcc simple as "gcc tcc.c -o tcc -ldl" again.
arm-asm.c:
  enable pseudo asm also for inline asm
tests/tests2/Makefile:
  disable bitfield tests except on windows and x86_64
  and don't generate-always
tcc.c:
  fix a loop with -dt on errors
configure:
  print compiler version (as recognized)
tccpp.c:
  actually define symbols for tcc -dt
  clear static variables (needed for -dt or libtcc usage)
96_nodata_wanted.c:
  use __label__ instead of asm
lib/files:
  use native symbols (__i386__ etc.) instead of TCC_TARGET_...
2017-07-23 21:24:11 +02:00
matthias fdc18d307a mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
  this case is usefull as with -funsigned-char, 'char *' are unsigned

* change VT_LONG so it's now a qualifier

  VT_LONG are never use for code generation, but only durring parsing state,
  in _Generic we need to be able to make diference between
  'long' and 'long long'
  So VT_LONG is now use as a type qualifier, it's old behaviour is still
  here, but we can keep trace of what was a long and what wasn't

* add TOK_CLONG and TOK_CULONG

  tcc was directly converting value like '7171L' into TOK_CLLONG or
  TOK_CINT depending of the machine architecture.

  because of that, we was unable to make diference between a long and a
  long long, which doesn't work with _Generic.

  So now 7171L is a TOK_CLONG, and we can handle _Generic properly

* check that _Generic can make diference between long and long long

* uncomment "type match twice" as it should now pass tests on any platforms

* add inside_generic global

  the point of this variable is to use VT_LONG in comparaison only
  when we are evaluating a _Generic.
  problem is with my lastest patchs tcc can now make the diference between
  a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
  typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
  break when stdint.h and stddef.h are include together.

  Another solution woud be to modifie include/stddef.h so it define uint64_t as
  unsigned long int when processor is 64 bit, but this could break some
  legacy code, so for now, VT_LONG are use only inside generc.

* check that _Generic parse first argument correctly

* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-21 19:30:31 +02:00
grischka 0cc24d0e84 tcc -dt -run ... : simpler is better
* -dt now with lowercase t

* test snippets now separated by real preprocessor statements
  which is valid C also for other compilers

    #if defined test_xxx
       < test snippet x >
    #elif defined test_yyy
       < test snippet y >
    #elif ...
    #endif

* simpler implementation, behaves like -run if no 'test_...' macros
  are seen, works with -E too

* for demonstration I combined some of the small tests for errors
  and warnings (56..63,74) in "60_errors_and_warnings.c"

Also:
* libtcc.c:
  put tcc_preprocess() and tcc_assemble() under the setjmp clause
  to let them return to caller after errors.  This is for -dt -E.
* tccgen.c:
  - get rid of save/restore_parse_state(), macro_ptr is saved
    by begin_macro anyway, now line_num too.
  - use expr_eq for parsing _Generic's controlling_type
  - set nocode_wanted with const_wanted. too, This is to keep
    VT_JMP on vtop when parsing preprocessor expressions.
* tccpp.c: tcc -E: suppress trailing whitespace from lines with
  comments (that -E removes) such as
       NO_GOTPLT_ENTRY,\t    /* never generate ... */
2017-07-20 22:21:27 +02:00
Matthias Gatto ba2b25e4ea fix typo sellector -> selector 2017-07-18 20:28:51 +02:00
matthias 2821644553 fix typo: march -> match 2017-07-18 20:28:51 +02:00
Matthias Gatto 2020a312ca unqualify volatile in _Generic type checking 2017-07-18 20:28:51 +02:00
grischka 7f1ab9b1e1 tccgen: nodata_wanted
The existing variable 'nocode_wanted' is now used to control
output of static data too. So...

(nocode_wanted == 0)
    code and data (normal within functions)
(nocode_wanted < 0)
    means: no code, but data (global or static data)
(nocode_wanted > 0)
    means: no code and no data (code and data suppressed)
(nocode_wanted & 0xC0000000)
    means:  we're in declaration of static data

Also: new option '-dT' to be used with -run
    tcc -dT -run file.c
This will look in file.c for certain comment-boundaries:
    /*-* test-xxx: ...some description */
and then for each test below run it from memory.  This way
various features and error messages can be tested with one
single file.  See 96_nodata_wanted.c for an example.

Also: tccgen.c: one more bitfield fix
2017-07-16 12:10:00 +02:00
grischka 69a137ff88 #pragma comment(option,"-..."), bitfields test, etc...
tccpp.c:
* #pragma comment(option,"-some-option")
  to set commandline option from C code.  May work only
  for some options.
libtcc.c:
* option "-d1..9": sets a 'g_debug' global variable.
  (for development)
tests2/Makefile:
* new make targets: tests2.37 / tests2.37+
  run single test from tests2,  optionally update .expect
* new variable GEN-ALWAYS to always generate certain .expects
* bitfields test
tccgen.c:
* bitfields: fix a bug and improve slightly more
* _Generic: ignore "type match twice"
2017-07-14 19:26:01 +02:00
Michael Matz 04418c7add Fix function types
various cases of function type uses were broken by recent
attribute refactoring, this fixes and adds testcases for them.
2017-07-14 17:42:48 +02:00
Michael Matz 2acb04f7f2 tccasm: Fix local statics referenced from asms
The assembler uses the ->sym_scope member to walk up the symbols
until finding a non-automatic symbol.  Since reordering the
members of Sym the sym_scope member contains a scope even for local
statics.  Formerly the use of asm_label for statics was implicitely
clearing sym_scope, now we have to do that explicitely.

Add a testcase for that, and one I encountered when moving the
clearing of sym_scope too deep into the call chain (into put_extern_sym).
2017-07-10 22:29:28 +02:00
Michael Matz 9bea88d616 Fix statement exprs returning a local label
Like returned local variables also labels local to a statement expression
can be returned, and so their symbols must not be immediately freed
(though they need to be removed from the symbol table).
2017-07-10 22:25:11 +02:00
Michael Matz 2240422da9 enums: Accept GNU extension
See testcase.  Happens e.g. in the linux kernel.
2017-07-10 22:20:34 +02:00
grischka a9e502cc3b refactor bitfields
Use 2 level strategy to access packed bitfields cleanly:

1) Allow to override the original declaration type with
   an auxilary "access type".  This solves cases such as

    struct {
        ...
        unsigned f1:1;
    };

    by using VT_BYTE to access f1.

2) Allow byte-wise split accesses using two new functions
   load/store_packed_bf. This solves any cases, also ones
   such as

    struct __attribute((packed)) _s {
        unsigned x : 12;
        unsigned char y : 7;
        unsigned z : 28;
        unsigned a: 3;
        unsigned b: 3;
        unsigned c: 3;
    };

    where for field 'z':
    - VT_INT access from offset 2 would be unaligned
    - VT_LLONG from offset 0 would go past the total
      struct size (7)

    and for field 'a' because it is in two bytes and
    aligned access with VT_SHORT/INT is not possible.

Also, static bitfield initializers are stored byte-wise always.
Also, cleanup the struct_layout function a bit.
2017-07-09 12:38:59 +02:00
grischka f87afa72e0 refactor enums
Eliminate VT_ENUM as a basic type.  Instead use the integral
types VT_InT/VT_LLONG with an additional flag to indicate
that it is an enum.
2017-07-09 12:38:25 +02:00