Commit Graph

2324 Commits (9e0d23cc47359149a39eafffdbb133963980b6ed)
 

Author SHA1 Message Date
Michael Matz ed680da951 bitfields: fix PCC layout
Fixes some corner cases in PCC layout.  Testcases coming
up.
2016-12-15 17:49:56 +01:00
Michael Matz bd69bce20f bitfields: Implement MS compatible layout
Bit-fields are layed out differently in visual C, this implements
a compatible mode.  Checked against Visual C/C++ 2016.
Unfortunately the GCC implementation of MS layout (behind
-mms-bitfields) actually is different, and hence not compatible
with MS in all cases :-/
2016-12-15 17:49:56 +01:00
Michael Matz 78c7096162 Fix struct layout some more
Anonymous sub-sub-members weren't handled correctly.  Bit-fields
neither: this implements PCC layout for now.  It temporarily disables
MS-compatible bit-field layout.
2016-12-15 17:49:56 +01:00
Michael Matz ddecb0e685 Split off record layouting
Such struct decl:

  struct S { char a; int i;} __attribute__((packed));

should be accepted and cause S to be five bytes long (i.e.
the packed attribute should matter).  So we can't layout
the members during parsing already.  Split off the offset
and alignment calculation for this.
2016-12-15 17:49:56 +01:00
Michael Matz 5d6a9e797a x86-asm: Fix segfault
We need to access cur_text_section, not text_section.
2016-12-15 17:49:56 +01:00
Michael Matz 22f5fccc2c Fix 64bit enums and switch cases
See testcases.  We now support 64bit case constants.  At the same time
also 64bit enum constants on L64 platforms (otherwise the Sym struct
isn't large enough for now).  The testcase also checks for various
cases where sign/zero extension was confused.
2016-12-15 17:49:56 +01:00
Michael Matz 3e77bfb6e9 tccpp: Fix token pasting
See testcase.  We must always paste tokens (at least if not
currently substing a normal argument, which is a speed optimization
only now) but at the same time must not regard a ## token
coming from argument expansion as the token-paste operator, nor
if we constructed a ## token due to pasting itself (that was already
checked by pp/01.c).
2016-12-15 17:49:56 +01:00
Michael Matz 3db037387c libtcc1: Don't use stdlib functions
libtcc1 is the compiler support library and therefore needs
to function in a freestanding environment.  In particular
it can't just use fprintf or stderr, which it was on x86-64
(but only when compiled by GCC).  The tight integration between
libtcc1 and tcc itself makes it impossible to ever reach that
case so the abort() there is enough.  abort() is strictly speaking
also not available in a freestanding environment, but it often is
nevertheless.
2016-12-15 17:49:56 +01:00
Michael Matz d042e71e9f Fix miscompile with dead switches
In certain very specific situations (involving switches
with asms inside dead statement expressions) we could generate
invalid code (clobbering the buffer so much that we generated
invalid instructions).  Don't emit the decision table if the
switch itself is dead.
2016-12-15 17:49:55 +01:00
Michael Matz 7ae35bf1bb Handle multiple -O options
the last one wins, i.e. "-O2 -O0" does _not_ set __OPTIMIZ__.
2016-12-15 17:49:55 +01:00
Michael Matz a158260e84 build: Respect CPPFLAGS override
so that e.g. make CPPFLAGS=-DSOMETHING works.
2016-12-15 17:49:55 +01:00
Michael Matz 235711f3d3 64bit: Fix addends > 32 bits
If a symbolic reference is offsetted by a constant > 32bit
the backends can't deal with that, so don't construct such
values.
2016-12-15 17:49:55 +01:00
Michael Matz a2a596e767 x86-64-asm: Accept high register in clobbers
The callee saved registers (among them r12-r15) really need
saving/restoring if mentioned in asm clobbers, even if TCC
itself doesn't use them.  E.g. the linux kernel relies on that
in its switch_to() implementation.
2016-12-15 17:49:55 +01:00
Michael Matz ddd461dcc8 Fix initializing members multiple times
When intializing members where the initializer needs relocations
and the member is initialized multiple times we can't allow
that to lead to multiple relocations to the same place.  The last
one must win.
2016-12-15 17:49:53 +01:00
Michael Matz f081acbfba Support local register variables
Similar to GCC a local asm register variable enforces the use of a
specified register in asm operands (and doesn't otherwise
matter).  Works only if the variable is directly mentioned as
operand.  For that we now generally store a backpointer from
an SValue to a Sym when the SValue was the result of unary()
parsing a symbol identifier.
2016-12-15 17:47:13 +01:00
Michael Matz 3bc9c325c5 Fix const folding of 64bit pointer constants
See testcase.
2016-12-15 17:47:12 +01:00
Michael Matz 0b0e64c2c9 x86-asm: Correct register size for pointer ops
A pointer is 64 bit as well, so it needs a full
register for register operands.
2016-12-15 17:47:12 +01:00
Michael Matz 7ab35c6265 struct-init: Copy relocs for compound literals
When copying the content of compound literals we must
include relocations as well.
2016-12-15 17:47:12 +01:00
Michael Matz 0bca6cab06 x86_64-asm: fix copy-out registers
If the destination is an indirect pointer access (which ends up
as VT_LLOCAL) the intermediate pointer must be loaded as VT_PTR,
not as whatever the pointed to type is.
2016-12-15 17:47:12 +01:00
Michael Matz ad723a419f x86_64: Add -mno-sse option
This disables generation of any SSE instructions (in particular
in stdarg function prologues).  Necessary for kernel compiles.
2016-12-15 17:47:12 +01:00
Michael Matz b5669a952b x86-64: relocation addend is 64bit
Some routines were using the wrong type (int) in passing addends,
truncating it.  This matters when bit 31 isn't set and the high
32 bits are set: the truncation would make it unsigned where in
reality it's signed (happen e.g. on the x86-64 with it's load
address at top-2GB).
2016-12-15 17:47:12 +01:00
Michael Matz 975c74c1f5 x86-64: Prefer 32S relocations
This target has _32 and _32S relocs (the latter being for signed
32 bit entities).  All instruction displacements have to use
the 32S variants.  Normal references like
  .long s
normally would use the _32 variant.  For normal executables this
doesn't matter.  For shared libraries neither (which use PC-relative
relocs).  But it matters for things like the kernel that are linked
to high addresses (signed ones).  There the GNU linker would error
out on overflow for the _32 variant.

To keep life simple we simply switch from _32 to _32S altogether.
Strictly speaking it's still wrong, but in practice using _32 is
more often wrong than using _32S ;)
2016-12-15 17:47:12 +01:00
Michael Matz ad8e14b740 opt: Don't emit inline functions from dead code
Inside dead code don't regard inline functions as being
referenced.
2016-12-15 17:47:12 +01:00
Michael Matz ce55d03eef Handle __builtin_extract_return_addr
Our architectures don't need anything special for this
builtin, just pass through the argument.
2016-12-15 17:47:12 +01:00
Michael Matz fb933ae0eb opt: constprop also 'cond && 0'
We didn't handle constants in logical expressions when they weren't
the first operand.  Some reordering in the loop structure is enough
to handle them.
2016-12-15 17:47:12 +01:00
Michael Matz ca435dc2e3 opt: Make break and goto not fallthrough
As we can optimize dead code a bit already it's fitting
to disable code emission after break and goto.
2016-12-15 17:47:12 +01:00
Michael Matz 31c7ea0165 opt: Start optimizing dead code a bit
If a condition is always zero/non-zero we can omit the
then or else code.  This is complicated a bit by having to
deal with labels that might make such code reachable without
us yet knowing during parsing.
2016-12-15 17:47:12 +01:00
Michael Matz b303a00ce0 Revert "Reject jumping inside stmtexprs"
Not fully thought out.  You can't jump inside stmt exprs,
but you can jump out of them.  So there's a difference
between undefined but declared labels at the end of stmt
exprs and those defined inside.  Additionally it should
also be checked if a label defined inside a stmt expr
was tentatively created as declared from outside.

I'm not prepared doing that right now, so simply revert.

This reverts commit 9160e4cab9147d77840cc44a285031fdb4640cf9.
2016-12-15 17:47:11 +01:00
Michael Matz d4d3144e75 Factor out const condition detection
Creating condition_3way for this.
2016-12-15 17:47:11 +01:00
Michael Matz 892c3d996f Reject jumping inside stmtexprs
One can't jump into statement expressions from outside
them, like the following:

  int i = ({ label: foo(); 42; });
  goto label;

We reject this by making the labels simply not available
outside (GCC has a nicer error message about jumping into
a statement expression).
2016-12-15 17:47:11 +01:00
Michael Matz 1602998751 Fix more nocode_wanted jump problems
In statement expression we really mustn't emit backward jumps
under nocode_wanted (they will form infinte loops as no expressions
are evaluated).  Do-while and explicit loop with gotos weren't
handled.
2016-12-15 17:47:11 +01:00
Michael Matz f2a071e808 Fix aliases on 64 bit
Use correct width ELF structure.
2016-12-15 17:47:11 +01:00
Michael Matz 9656560f14 Fix sizeof(char[a])
The sizes of VLAs need to be evaluated even inside sizeof,
i.e. when nocode_wanted is set.
2016-12-15 17:47:11 +01:00
Michael Matz 49bb5a7e06 Fix __builtin_constant_p(1000/x)
was incorrectly treated as constant because the vpop removed
all traces of non-constness.
2016-12-15 17:47:11 +01:00
Michael Matz 5bd8aeb917 tccasm: Support refs to anon symbols from asm
This happens when e.g. string constants (or other static data)
are passed as operands to inline asm as immediates.  The produced
symbol ref wouldn't be found.  So tighten the connection between
C and asm-local symbol table even more.
2016-12-15 17:47:11 +01:00
Michael Matz dd57a34866 tccasm: Don't ignore # in preprocessor directives
Our preprocessor throws away # line-comments in asm mode.
It did so also inside preprocessor directives, thereby
removing stringification.  Parse defines in non-asm mode (but
retain '.' as identifier character inside macro definitions).
2016-12-15 17:47:11 +01:00
Michael Matz e7ef087598 x86-asm: Accept all 32bit immediates
In particular don't care if they're signed or unsigned, they're all
acceptable as immediates.
2016-12-15 17:47:11 +01:00
Michael Matz 372f4b6a4e Fix enum bitfields passed to stdarg functions
VT_ENUM types use the .ref member and can be VT_BITFIELD,
so we need to copy it as well.  Simply do it always.
2016-12-15 17:47:11 +01:00
Michael Matz d720865fb6 Addresses of non-weak symbols are non-zero
Use this fact in some foldings of comparisons.  See testcase.
2016-12-15 17:47:10 +01:00
Michael Matz be6d8ffc10 Fix access-after-free with statement expressions
The return value of statement expressions might refer to local
symbols, so those can't be popped.  The old error message always
was just a band-aid, and since disabling it for pointer types it
wasn't effective anyway.  It also never considered that also the
vtop->sym member might have referred to such symbols (see the
testcase with the local static, that used to segfault).

For fixing this (can be seen better with valgrind and SYM_DEBUG)
simply leave local symbols of stmt exprs on the stack.
2016-12-15 17:47:10 +01:00
Michael Matz d0d25ec7df tccpp: Allow computed include like 42.h
The include directive needs to be parsed as pp-tokens, not
as token (i.e. no conversion to TOK_STR or TOK_NUM).  Also fix
parsing computed includes using quoted strings.
2016-12-15 17:47:10 +01:00
Michael Matz 0381387640 x86-asm: Correctly infer register size for bools
Register operands of type _Bool weren't correctly getting
the 8-bit sized registers (but rather used the default 32-bit
ones).
2016-12-15 17:47:10 +01:00
Michael Matz 9e0af6d2b5 x86-64-asm: Implement cmpxchg16b 2016-12-15 17:47:10 +01:00
Michael Matz ca8c1cd643 x86-64: Allow loads from some structs/unions
GCC allows register loads for asms based on type mode,
and correctly sized structs/union have an allowed mode
(basically 1,2,4,8 sized aggregates).
2016-12-15 17:47:10 +01:00
Michael Matz 9ae10cad1f tccasm: Lookup C symbols from ASM blocks
It's now possible to use symbols defined in C code to be used
from later inline asm blocks.  See testcase.
2016-12-15 17:47:10 +01:00
Michael Matz c4edfb4e08 tccasm: Implement .set sym, expr
That, as well as "sym = expr", if expr contains symbols.
Slightly tricky because a definition from .set is overridable,
whereas proper definitions aren't.

This doesn't yet allow using this for override tricks from C
and global asm blocks because the symbol tables from C and asm
are separate.
2016-12-15 17:47:10 +01:00
Michael Matz 34fc6435ee enums and ints are compatible
But like GCC do warn about changes in signedness.  The latter
leads to some changes in gen_assign_cast to not also warn about
  unsigned* = int*
(where GCC warns, but only with extra warnings).
2016-12-15 17:47:10 +01:00
Michael Matz b1a906b970 enums and ints are compatible 2016-12-15 17:47:10 +01:00
Michael Matz c0368604e1 x86-64-asm: Fix ltr/str and push/pop operands
str accepts rm16/r32/r64, and push/pop defaults to 64 when given
memory operands (to 32 on i386).
2016-12-15 17:47:10 +01:00
Michael Matz 45b24c37a0 x86-64-asm: Implement high %cr registers 2016-12-15 17:47:09 +01:00