Commit graph

75 commits

Author SHA1 Message Date
Michael Forney b9adc507d2 cc: fix incorrect octal range condition in mpatov
This does not have any adverse effect, since yylex never calls mpatov
with a string with leading 0 (and not 0x) that contains non-octal
digits, but the condition was wrong regardless.
2022-01-23 01:05:27 +00:00
cinap_lenrek b3c3c3e63d cc: do not expand function-like macros for non-function invocations
It is a bit of a annoyance that kenc will try to expand
function like macros on any symbol with the same name
and then complain when it doesnt see the '(' in the
invocation.

test case below:

void
foo(int)
{
}

struct Bar
{
	int	baz;	/* <- should not conflict */
};

void
main(void)
{
	baz(123);
}
2021-10-12 03:06:20 +00:00
Jacob Moody 88a8ca5c8d cc: create .$O files with DMTMP 2021-06-24 13:53:37 +00:00
Ori Bernstein 21e5726f43 cc: mk clean should delete cc.a$O
It's surprising when 'mk clean' doesn't actually
do a clean build in cc -- fix this.
2021-01-24 09:58:39 -08:00
Michael Forney e8b8cec9c7 [9front] cc: fix comparison warning with constant LHS
This prevents an incorrect warning for a comparison such as `0 < x`,
where x is an unsigned type.  Previously, this would get normalized as
`x >= 0` rather than `x > 0` when checking the comparison.
2021-01-23 18:28:08 -08:00
Michael Forney a8834acf73 [9front] cc: remove unnecessary 128-bit add function
Instead, just change the comparisons from <=/>= to </>.
2021-01-23 18:26:34 -08:00
BurnZeZ fd1db35c4d cc: add a couple notes to the comments regarding flags 2020-12-29 19:38:59 +00:00
Ori Bernstein 3ba1d83d20 cc: promote integer constants according to c99 spec.
C99 integer constants with no type suffix promote differently
depending on the way that they're written: hex and oct consts
promote as int => uint => long => ulong => vlong => uvlong.
Decimal constants are always signed.

We used to promote all values to uint on overflow, and never
went wider. This change fixes that, and adds a warning when
a decimal constant that would have been promoted to uint in
the past gets promoted to int.
2020-08-08 11:39:25 -07:00
cinap_lenrek 8ca102d42e cc: dont export gethunk(), hunk, nhunk and thunk 2020-05-12 23:18:48 +02:00
cinap_lenrek 34cf2725d2 cc: get rid of hunk pointer fiddling and just use alloc() 2020-05-12 22:45:05 +02:00
cinap_lenrek 7feab4dc59 cc: simplify macexpand() and off-by-one error
the caller of macexpand() needs one more byte in
the buffer to append peekc.

make macexpand() actually check for buffer overflow.

just use strdup() to duplicate include file name
instead of the hunk dance.

move GETC() macro in cc.h
2020-04-25 22:16:44 +02:00
cinap_lenrek 8499401ada ?a, ?c: fix macro debug prints 2020-04-19 23:57:33 +02:00
cinap_lenrek 313216e6fb ?c: get rid of sprint(), strcpy() and strcat()/strncat(), cleanup 2020-04-19 23:51:18 +02:00
cinap_lenrek c66b3739ac cc: cc.h changes needed by previous commit 2020-04-19 23:42:02 +02:00
cinap_lenrek 0d59a2358a ?a, cc: fix buffer overflows in built-in preprocessor (macbody)
add a buffer size argument to macexpand() and check for
overflow.

check for overflow when parsing #include directives.
2020-04-19 23:37:05 +02:00
cinap_lenrek e24bfa4941 ?a: catch symb[NSYMB] buffer overflow in lexer, cleanup, assume thechar is a rune 2020-04-19 22:59:21 +02:00
cinap_lenrek 3f87ffea44 cc: get rid of sprint() and temporary buffer for type conversion fuction
slookup() copies to symb, so use the symb[NSYMB] buffer directly
to declare type conversion functions and get rid of the arbitrary
sized local buffer. replace sprint() with snprint().
2020-04-19 20:44:19 +02:00
cinap_lenrek ca9d65e40b cc: remove mysbrk(), exponentially increase gethunk() allocation delta
mysbrk() was only used in gethunk() and should not be
called by anyone, so dont export the symbol.

simplify gethunk() using brk().

double allocation size on each call until we reach
1000*NHUNK.

use signed long for nhunk as alignment rountin might
make it negative and handle that case.
2020-04-11 14:19:35 +02:00
cinap_lenrek 1b8a569417 cc, ?[acl]: fix gethunk() and move common memory allocator code to cc/compat
for gethunk() to work, all allocators have to use it,
including allocations done by libc thru malloc(),
so the fake allocation functions are mandatory for
everyone.

to avoid duplication the code is moved to cc/compat
and prototypes provided in new cc/compat.h header.
2020-04-11 05:03:49 +02:00
cinap_lenrek 9d46360c9d backout the gethunk() again, as that breaks the assemblers
the assemblers share gethunk() cc/macbody but are compiled
without compat.c, so calls such as getenv() trigger malloc()
which does its own sbrk() calls, breaking the continuity
of the hunk.

so this change needs another revision. until then, this is
backed out.
2020-04-11 01:26:36 +02:00
cinap_lenrek 1d3644a168 cc, ?l: fix gethunk() to actually grow allocation
the compilers and linkers use ther own memory allocator.
free memory is between hunk and hunk+nhunk. allocation
works by checking if nhunk is bigger or equal to the
amount needed, and if not, repeatedly call gethunk()
until there is. after that, the allocated amount is added
from hunk and subtracted from nhunk by the user.

the problem was when the needed amount was bigger than
the default NHUNK size gethunk() allocates per call.
gethunk() would not actually grow nhunk, but instead
just set hunk and nhunk variables to the last allocated
block. this resulted in a infinite loop of calls to
gethunk() until sbrk() would hit the maximum size for
the BSS segment.

this change makes gethunk() actually grow the hunk space,
increasing nhunk, and only updating hunk when nhunk was
previously zero. we assume that mysbrk() retuns increasing
addresses and that the space between the previous hunk+nhunk
and the new block base returned by mysbrk() is usable.
2020-04-10 23:11:25 +02:00
cinap_lenrek e610ffaf1b merge 2020-04-10 22:26:37 +02:00
cinap_lenrek a39cc60c52 cc: backout gethunk() change
the real problem is that gethunk() does not grow the allocation
but just allocates a new hunk, so repeated calls to gethunk()
wont make nhunk grow to satisfy the allocation.

this will be fixed in a upcoming commit.
2020-04-10 22:25:29 +02:00
Sigrid 2cdf1a3c79 cc, ?a, ?l: change thunk type to uintptr 2020-04-10 20:38:45 +02:00
Sigrid 6018316eef cc: sbrk in bigger chunks as it grows, so it gets a chance to use the ram/swap available 2020-04-10 17:19:44 +02:00
Ori Bernstein 52dc943702 fix ccom idempotency
ccom may be called multiple times on the same
node, via 'goto loop' calls from the commute
label, OADD, and a few other places.

Casts to void could null out the LHS of the
node, which would cause the compiler to crash
if the cast was revisited due to one of these
cases, because we tried frobbing n->left.

Now, if n->left is nil, we just return.w
2020-03-15 15:08:04 -07:00
Ori Bernstein 3df95385bc fix special case for null pointer constants in cond expressions
Section 6.5.15 of the C99 spec requires that if
	one argument of a ?: expression is a null pointer
	constant, and the other has a pointer type T*, then
	the type of the expression is T*.

	We were attempting to follow this rule, however,
	we only handled literal expressions when checking
	for null pointers.

	This change looks through casts, so 'nil' and 'NULL',
	and their expansion '(void*)0' are all detected as
	null pointer constants.
2020-02-27 15:09:10 -05:00
Ori Bernstein 71939a82cc Allow address expressions in ?c after int casts.
This fixes ocaml on non-x86 architectures, where we have code
that looks like:

	#define Fl_head ((uintptr_t)(&sentinel.first_field))

Without this change, we get an error about a non-constant
initializer. This change takes the checks for pointers and
makes them apply to all expressions. It also makes the checks
stricter, preventing the following from compiling to junk:

	int x;
	int y = 42;
	int *p = &x + y
2019-09-07 18:25:04 -07:00
cinap_lenrek 766a641d25 cc: fix void cast crash
the following code reproduces the crash:

void
foo(void)
{
}

void
main(int argc, char **argv)
{
	(void)(1 ? (void)0 : foo());
}

the problem is that side() gives a false positive on the OCOND
with later constant folding eleminating the acutal side effect
and OCAST ending up with two nested OCATS with the nested one
being zapped (type == T).
2019-09-07 02:11:18 +02:00
cinap_lenrek ffd99348f3 cc: use 7 octal digits for 21 bit runes 2019-08-12 19:15:02 +02:00
cinap_lenrek 63191949b9 cc: remove nullwarn() from OCAST codegen, zap void casts
implicit casts would cause spurious "result of operation not used"
warnings such as ape's stdio putc() macro.

make (void) casts non-ops when the casted expression has no
side effects. this avoid spurious warning with ape's assert()
macro.
2019-06-19 23:50:33 +02:00
Ori Bernstein ff344562db Import compiler warnings and bugfixes from Charles.
This change imports a few warnings and minor fixes from Charles branch
here: https://bitbucket.org/plan9-from-bell-labs/plan9.

The changes included here:


	changeset:   1374:9185dc017be0
	summary:     declare castucom; move a declaration into order;
	             use cast instead of ULL suffix
	changeset:   1353:5fe8380b1818
	summary:     supporting functions:
	             1. castucom to match unlikely mask operation;
  	             2. be sure to snap both sides of pointer subtraction completely;
	             3. add extra operators as side-effect free
	changeset:   1352:90058c092d66
	summary:     1. correct result type for mixed-mode assignment operators
	             2. detect divide by zero (erik);
	             3. detect masks misformed by sign-extension;
	             4. diagnose mixed old/new prototypes
2019-06-18 22:17:19 -07:00
cinap_lenrek 5d58b9573c cc: fix %.*s format usage in lexer "token too long" error 2019-02-25 04:15:43 +01:00
aiju b5f7e27f44 kencc: make "function not declared" a warning unless compiling with -T 2018-12-02 11:36:58 +00:00
aiju d9b9e15d6b kencc: turn "function args not checked" warning into "function not declared" error if appropriate 2018-12-01 11:39:49 +00:00
cinap_lenrek 485a3301e6 cc: fix wrong "useless or misleading comparison" warning
to reproduce:

 	u8int x, y;

 	x = 0xff;
 	y = 0xc0;
 	if((s8int)(x & y) >= 0)
 		print("help\n");

compiles correctly but prints a warning

warning: test.c:11 useless or misleading comparison: UINT >= 0

the issue is that compar() unconditionally skipped over
all left casts ignoring the case when a cast would sign
extend the value.

the new code only skips over the cast when the original
type with is smaller than the cast result or when they
are equal width and types have same signedness. so the
effective left hand side type is the last truncation
or sign extension.
2018-11-18 20:42:45 +01:00
aiju ce17224eaf ?a: getc() needs to increment lineno if it gets \n from peekc 2017-05-26 00:01:53 +00:00
spew 8b6621a360 [012568kqv]a: correctly lex full range of integers in the assemblers (thanks Ori_B)
The Plan 9 assemblers use strtoll to parse the integer literals
in their input. It turns out that this is almost correct, but
VLONG_MIN is clamped. This patch changes to use strtoull
in order to allow the full range of integers.
2017-03-22 00:04:24 -05:00
cinap_lenrek 57ff297cc0 cc: add OROL op to side effect free op list 2016-06-26 15:00:40 +02:00
cinap_lenrek a00b6bdbfa 8c, 6c: native ROL (cyclic shift) instruction support, improve peephole optimizers
introduce rolor() function to subsitute (a << c) | (a >> (bits(a) - c))
with (a <<< c) where <<< is cyclic rotation and c is constant.
this almost doubles the speed of chacha encryption of 386 and amd64.

the peephole optimizer used to stop when it hit a shift or rol
instruction when attempting to eleminate moves by register
substitution. but we do not have to as long as the shift count
operand is not CX (which cannot be substituted) and CX is not
a subject for substitution.
2016-06-09 23:12:46 +02:00
cinap_lenrek 83f8d59168 cc: prevent symbol buffer overflow 2016-05-16 22:24:39 +02:00
cinap_lenrek 32e23bdb59 cc: use UTFmax not 4 (djc) 2016-05-16 22:02:17 +02:00
BurnZeZ f0744bae5e cc: populate the flag list comment in lex.c
This way the various compiler flags are documented
somewhere, even if not in full.

Also fixed a typo. 'r' should have been 'R'
2016-03-01 19:00:41 -05:00
BurnZeZ edec6e6a70 cc: sort the lex.c comment listing debug flags 2016-03-01 18:12:04 -05:00
cinap_lenrek 9c99d0c8d3 6c: return vlong result for pointer subtraction 2016-01-07 04:40:47 +01:00
cinap_lenrek 3e38194d72 introduce signed intptr and %z format modifier for formating uintptr and intptr 2016-01-07 04:39:09 +01:00
cinap_lenrek 2598a9e312 cc: restore side(), but do not consider OINDEX as side effect free
from charles forsuth:

because the previous version thought OINDEX might have a side effect, it
stopped it building a tower of them.
probably the best thing is to limit that anyway, since each one consumes
2-3 registers, so towering them can
keep even more active, and the x86 hasn't got that many.

the quick hack is to return that case to the earlier state by treating
OINDEX as a side-effect in side().
it's not a bad thing to do in the OSTRUCT case, for similar reasons: it's
better to collapse the indexed pointer
into a direct register, instead of repeating the indexing operation through
the copying of the value.
OINDEX isn't a machine-independent operation, so it doesn't affect the uses
in ../cc
2015-10-08 17:14:07 +02:00
cinap_lenrek fa1f9f7ca6 cc: back out side() change unil 8c is fixed
this change made 8c fail to build libvorbis and gs, until this
is fixed we can live with non-optimal code.
2015-10-07 13:35:36 +02:00
cinap_lenrek fe3430ea99 cc: include $builtin as keywoard in pickle() (from charles forsyth) 2015-10-06 06:47:55 +02:00
cinap_lenrek 37d2856f55 cc: getflag() fmt will be terminated at end of loop (from charles forsyth) 2015-10-06 06:38:37 +02:00