Commit Graph

481 Commits (8227db3a23fd3cf11840eaa25eab5f3f5f813ac7)

Author SHA1 Message Date
grischka 8227db3a23 jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.

Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).

example:
    if (a && b || c && d)
        e = 0;

before this patch:
   a:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
   d:	83 f8 00             	cmp    $0x0,%eax
  10:	0f 84 11 00 00 00    	je     27 <main+0x27>
  16:	8b 45 f8             	mov    0xfffffff8(%ebp),%eax
  19:	83 f8 00             	cmp    $0x0,%eax
  1c:	0f 84 05 00 00 00    	je     27 <main+0x27>
  22:	e9 22 00 00 00       	jmp    49 <main+0x49>
  27:	8b 45 f4             	mov    0xfffffff4(%ebp),%eax
  2a:	83 f8 00             	cmp    $0x0,%eax
  2d:	0f 84 11 00 00 00    	je     44 <main+0x44>
  33:	8b 45 f0             	mov    0xfffffff0(%ebp),%eax
  36:	83 f8 00             	cmp    $0x0,%eax
  39:	0f 84 05 00 00 00    	je     44 <main+0x44>
  3f:	e9 05 00 00 00       	jmp    49 <main+0x49>
  44:	e9 08 00 00 00       	jmp    51 <main+0x51>
  49:	b8 00 00 00 00       	mov    $0x0,%eax
  4e:	89 45 ec             	mov    %eax,0xffffffec(%ebp)
  51:   ...

with this patch:
   a:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
   d:	83 f8 00             	cmp    $0x0,%eax
  10:	0f 84 0c 00 00 00    	je     22 <main+0x22>
  16:	8b 45 f8             	mov    0xfffffff8(%ebp),%eax
  19:	83 f8 00             	cmp    $0x0,%eax
  1c:	0f 85 18 00 00 00    	jne    3a <main+0x3a>
  22:	8b 45 f4             	mov    0xfffffff4(%ebp),%eax
  25:	83 f8 00             	cmp    $0x0,%eax
  28:	0f 84 14 00 00 00    	je     42 <main+0x42>
  2e:	8b 45 f0             	mov    0xfffffff0(%ebp),%eax
  31:	83 f8 00             	cmp    $0x0,%eax
  34:	0f 84 08 00 00 00    	je     42 <main+0x42>
  3a:	b8 00 00 00 00       	mov    $0x0,%eax
  3f:	89 45 ec             	mov    %eax,0xffffffec(%ebp)
  42:   ...
2019-06-24 11:40:01 +02:00
grischka 1b57560502 nocode, noreturn
A more automatic approach to code suppression (aka. nocode_wanted)

The simple rules are:
- Clear 'nocode_wanted' at (im/explicit) label IF it was used
- Set 'nocode_wanted' after unconditional jumps

Also in order to test this then I did add the "function might
return no value" warning, and then to make that work again I
did add the __attribute__((noreturn)).

Also moved the look ahead label check into the type parser
to gain a little speed.
2019-06-24 11:40:01 +02:00
grischka 8569048031 work on local extern declarations
Example:
    int a = 1;
    void f(void)
    {
        int a = 2;
        {
             extern int a; // = 1 !!
             ....

To get this (more) correctly there is a new function to copy
syms between local to global stacks.

Also, this patch changes the meaning of VT_EXTERN back
to the simpler and IMO more useful notion of
    DECLARED but not (yet) DEFINED.
and that for both variables and functions.  That is, VT_EXTERN
in tcc doesn't have to do with the keyword 'extern' necessarily.

Also this patch does allow
    int x[];
as alias for
    extern int x[];
(as do gcc and msvc)
2019-06-24 11:38:32 +02:00
Pascal Cuoq cbbba01b46 reject invalid arrays in post_type() 2019-06-24 10:28:44 +02:00
Michael Matz cb73be5346 Fix last commit
it wasn't complete.
2019-06-17 20:52:09 +02:00
Michael Matz c3f0937012 Don't emit unreferenced static inlines
there's no need to emit unreferenced static function, even
if they are forced.
2019-06-17 19:36:59 +02:00
Michael Matz fe23a14ebb Deal with more tentative definitions
see testcase.
2019-06-17 18:52:49 +02:00
Michael Matz 69a46b0c53 Make mingw work again
my last inline changes caused parameter names to be overwritten
always (as VT_EXTERN now doesn't mark the current def anymore),
leading to a compile error when including windows.h.  Rework this.

Also silence a warning that currently happens for mingw, which is
written with gnu-inline behaviour in mind.  Our work-arounds
of using "static inline" actually create invalid C (which we warn
about).  Until we implement this properly, just silence the warning.
2019-06-17 18:28:56 +02:00
Michael Matz cb8bbf1ab9 TLC for C99 inline implementation
there's no need for two new flags in type.t .  We just can't use
VT_EXTERN as marker if functions are defined or not (like we can
for objects), and then can simply implement the rules of C99/C11
by not overwriting VT_STATIC/VT_EXTERN at all but rather only
look at them.  A function already on the inline list can be
forced by removing the VT_INLINE flag, and then linkage
follows from some combination of VT_STATIC, VT_EXTERN and VT_INLINE.
2019-06-17 03:34:03 +02:00
Petr Skocik 587e1f5598 standard conformant inline functions
- add tests for standard conformant inline functions
- implement it

The  old tinycc failed to provide a conforming implementation
of non-static inlines.  It would expose external symbols where it
shouldn't and hide them where it should expose them.

This commit provides a hopefully comprehensive test suite
for how things should be done. The .expect file can be obtained
by compiling the example c file (embedded in the test)
with a conforming compiler such as gcc, clang or icc and then
printing the exported symbols (e.g., with nm+awk+sort).

(The implementation currently reserves two new VT_ flags.
If anyone can provide an implementation without reserving
two extra flags, please replace mine.)
2019-06-11 16:29:24 +02:00
Vlad Vissoultchev 1dd6842654
Don't drop asm_label hack on external symbols for win32 DLL exports 2019-05-14 22:37:13 +03:00
matthias 14be3a1dc1 fix cleanup with for loop initialisation
Signed-off-by: matthias <uso.cosmo.ray@gmail.com>
2019-05-03 12:32:55 +02:00
matthias 0d54946dec fix cleanup with break and continue
fix cleanup
2019-05-03 12:32:55 +02:00
Michael Matz d30bc6d00a _Static_assert must be followed by semicolon
as per the C11 grammar.
2019-05-03 00:22:35 +02:00
matthias 5a0101856b add C11 _Static_assert support 2019-04-28 01:07:21 +02:00
Michael Matz c07e81b087 Tidy some code
the real difference is in decl0 where we can use external_sym
just fine also for function definitions, we don't have to use
external_global_sym.  Setting VT_EXTERN in external_sym isn't
necessary either (the type will have it set if necessary).
The rest is tidying: removing unused arguments and moving
some code around.
2019-04-18 03:42:23 +02:00
Michael Matz 0344c0b6a0 Fix more struct inits
anonymous struct members were somewhat broken as the testcase
demonstrates.  The reason is the jumping through hoops to fiddle
with the offsets I once introduced to avoid having to track
a cumulative offset.  That's now not necessary anymore and actively
harmful, doing the obvious thing is now better.
2019-04-11 00:30:41 +02:00
Michael Matz 38a6aba468 Fix _Alignas
* don't accept _Alignas as type qualifier (after pointer '*').
* accept type-names within _Alignas
* add testcases
2019-04-08 22:06:51 +02:00
Michael Matz 2a417b50ee Detect invalid VLA decls
see testcase, when the inner array dimension of multi-dimensional
VLAs isn't given TCC was generating invalid vstack accesses.
Those are actually invalid, so just diagnose them.
2019-04-07 04:09:25 +02:00
Michael Matz 5ac2a26666 Don't endlessly recurse on invalid nested typedefs
see testcase.
2019-04-07 03:15:05 +02:00
Michael Matz d00f98a7a5 Fix 98_al_ax_extend.c compile error
this test is run only on i386 so its failing went unnoticed for
a while, since 1fd3709379.  IS_ASM_SYMs should not be tested
for conflicting types, the C typing overrides.
2019-04-07 02:44:32 +02:00
Devin Hussey 9382a3ad58 C11 conformance: Add _Noreturn, stdnoreturn.h, and partial _Alignas support
_Noreturn, just like __attribute__((noreturn)), is ignored.
I also added stdnoreturn.h, in all its glorious uselessness.

_Alignas only works for integer expressions right now. In order
to comply, we need:
 - _Alignas(type) -> _Alignas(_Alignof(type)).
 - stdalign.h as soon as it is done.

Note: DR 444 is supported; it works on struct members.

Signed-off-by: Devin Hussey <husseydevin@gmail.com>
2019-03-20 15:03:27 -04:00
Michael Matz 1fd3709379 Fix local extern vardecl
see testcases.  A local 'extern int i' declaration needs to
refer to the global declaration, not to a local one it might
be shadowing.  Doesn't seem to happen in the wild very often as
this was broken forever.
2019-03-18 05:53:03 +01:00
Michael Matz e6980f6cc7 Detect more invalid initializers
in presence of invalid source code we can't rely on the
next token to determine if we have or haven't already parsed
an initializer element, we really have to track it in some separate
state; it's a flag, so merge it with the other two we have (size_only
and first).  Also add some syntax checks for situations which
formerly lead to vstack leaks, see the added testcases.
2019-03-18 03:31:11 +01:00
Michael Matz 4b46e0ec63 Handle corner case for abstract decls
sometimes abstract decls in parameter lists left the returned name
uninitialized potentially leading to segfaults, like in

  int f(int ()) {
    return 0;
  }

Deal with this.
2019-03-12 17:27:15 +01:00
Michael Matz ef0397cf3d Fix crash on invalid code
like on 'enum myenum { L = -1 } L;'.  It's a bit tedious as
there are two paths (for global vs local symbols), and because
the scope and enum_val share same storage.
2019-03-08 17:58:25 +01:00
matthias b082659f19 fix segv in "{,}" combound literal 2019-03-06 18:51:13 +01:00
Michael Matz 5f737fb4d3 tidy code 2019-01-31 01:04:55 +01:00
Michael Matz 5c862a08b4 suppress code after continue as well
fix an oversight in code suppression
2019-01-31 00:37:49 +01:00
matthias d27ea5155f remove incr/decr_local_scope functions 2019-01-29 21:32:38 +01:00
Michael Matz 4cc802a88e Tidy new support for cleanups
encode most things in Syms, do only as much work as necessary
(e.g. pending cleanups), don't track scopes in a large
structure (instead encode the scopes with cleanups directly
in the cleanups tree).  Removes ca. 120 lines of code.
2019-01-28 05:54:19 +01:00
matthias 46145af4a1 remove C99 'for' loop initial declarations 2019-01-23 19:42:04 +01:00
matthias 26f0cf0708 Fix scope limit for cleanup attribute
old implementation use only a global static array for storing
ScopeTracker which have the advantage to be fast, but you can't
use cleanup in a function that have move than SCOPE_TCK_STORE_SIZE
scopes.

I don't want to use only dynarray_* as it would slow down tcc for
every functions, so I keep both stores.
2019-01-23 17:21:14 +01:00
matthias 0d91ba749c Add gcc attribute cleanup support
The major difficulty was to handle cleanup when a goto happen
to do so, I've had a "ScopeTracker" struct.
I can't use local_scope because that would not work with code like below
as local_scope would be at the same level:

{
    char * __attribute__ ((cleanup(clean_function))) str = "hej";
    goto next;
}
{
    next:
}
2019-01-23 17:21:14 +01:00
Matthias Gatto 2b94c0c3b1 Revert "allow c11 feature only when -std=c11 is use"
This reverts commit 756988e8f9.
2019-01-11 10:35:44 +01:00
matthias gatto 756988e8f9 allow c11 feature only when -std=c11 is use 2019-01-10 23:42:45 +01:00
Petr Skocik 51f6e52dd3 Support multiple __label__ declarations
Support multiple __label__ declarations at the beginning of a block
as long as they're contiguous.

gcc and clang accept:
    { __label__ a,b; __label__ c;  /*...*/ }
.
Tcc would fail it. This patch makes it accept it.

The patch:
-        if (tok == TOK_LABEL) {
+        while (tok == TOK_LABEL) {
2019-01-11 02:32:47 +08:00
Pursuer b3b685d92a optimize the generated code when save_reg is required
Before this patch, save_reg can't reuse the temporary local variable
created before to save register. It may consume a lot of stack memory. this patch make save_reg reuse the temporary local variable.
2019-01-10 13:17:14 +08:00
Pursuer 0c313f491b Insert arm-xxx_FILES into Makefile to suport CROSS_TARGET and Remove duplicate function lexpand_nr 2019-01-09 02:06:26 +08:00
Michael Matz 3e007193a2 Fix more attribute placements
when parsing the type in this cast:
  (int (__attribute__(X) *)(int))foo
we ignored the attribute, which matters if it's e.g. a 'stdcall'
attribute on the function pointer.  Only this particular placement
was misparsed.  Putting the attribute after the '*' or outside the inner
parens worked.  This idiom seems to be used on SQLite, perhaps this
fixes a compilation problem on win32 with that.
2018-12-31 22:00:31 +01:00
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