ack.git
6 years agoFix build with gcc. kernigh-stdc
George Koehler [Fri, 17 Nov 2017 22:52:37 +0000 (17:52 -0500)]
Fix build with gcc.

gcc gave an error because the `char *` parameter doesn't match the
`const char *` in the prototype of regsave().  clang didn't give an
error.  I added the prototype in commit 5301cce.

6 years agoFix my typo from commit 5bbbaf4.
George Koehler [Fri, 17 Nov 2017 20:46:24 +0000 (15:46 -0500)]
Fix my typo from commit 5bbbaf4.

6 years agoSwitch ego to libc <assert.h>
George Koehler [Thu, 16 Nov 2017 00:48:53 +0000 (19:48 -0500)]
Switch ego to libc <assert.h>

I also tried, in types.h, to switch ego to libc <stdbool.h>, but that
causes an infinite loop in the IL phase.

6 years agoAdd prototypes, void in util/ego/share
George Koehler [Wed, 15 Nov 2017 21:29:27 +0000 (16:29 -0500)]
Add prototypes, void in util/ego/share

This uncovers a problem in il/il_aux.c: it passes 3 arguments to
getlines(), but the function expects 4 arguments.  I add FALSE as the
4th argument.  TRUE would fill in the list of mesregs.  IL uses
mesregs during phase 1, but this call to getlines() is in phase 2.
TRUE would leak memory unless I added a call to Ldeleteset(mesregs).
So I pass FALSE.

Functions passed to go() now have a `void *` parameter because
no_action() now takes a `void *`.

6 years agoUse size_t and void with memory allocation in ego.
George Koehler [Wed, 15 Nov 2017 01:35:18 +0000 (20:35 -0500)]
Use size_t and void with memory allocation in ego.

alloc.h now needs to #include <stdlib.h> to find type size_t and
function free().

6 years agostrcmp, strncmp are in <string.h>
George Koehler [Tue, 14 Nov 2017 22:04:01 +0000 (17:04 -0500)]
strcmp, strncmp are in <string.h>

*Important:*  Do `make clean` to work around a problem and prevent
infinite rebuilds, https://github.com/davidgiven/ack/issues/68

I edit tokens.g in util/LLgen/src, so I regenerate tokens.c.  The
regeneration script bootstrap.sh can't find LLgen, but I can run the
same command by typing the path to llgen.

6 years agoFree buf in GetFile().
George Koehler [Tue, 14 Nov 2017 02:34:31 +0000 (21:34 -0500)]
Free buf in GetFile().

aprintf() returns a const char *; the assignment to char * caused both
clang and gcc to warn of the dropped const.

Commit 893471a introduced a tiny memory leak, because GetFile()
stopped freeing buf.  The const return type of aprintf() suggests that
the buffer must not be freed.

Now use Malloc() to allocate the buffer and free() to free it.  This
also checks if we are out of memory, because Malloc() does the check
and aprintf() currently doesn't.

6 years agoCheck each format string in tabgen.c
George Koehler [Tue, 14 Nov 2017 01:40:43 +0000 (20:40 -0500)]
Check each format string in tabgen.c

Silence warning from clang at `if (ch2 = ...)`

Delete `|| rm %{outs}` in build.lua, because it hid the exit status of
tabgen, so if tabgen failed, the build continued and failed later.

6 years agoSilence warning about pointer cast to int.
George Koehler [Mon, 13 Nov 2017 23:21:26 +0000 (18:21 -0500)]
Silence warning about pointer cast to int.

This cast was safe because the pointer is a small constant integer,
but it causes warning from gcc.

6 years agostrcpy() is in <string.h>
George Koehler [Mon, 13 Nov 2017 22:57:02 +0000 (17:57 -0500)]
strcpy() is in <string.h>

6 years agoDeclare machine-dependent functions in mach/proto/ncg
George Koehler [Mon, 13 Nov 2017 19:23:44 +0000 (14:23 -0500)]
Declare machine-dependent functions in mach/proto/ncg

This breaks all machines because the declared return type void
disagrees with the implicit return type int (when I compile mach.c
with clang).  Unbreak i386, i80, i86, m68020, powerpc, vc4 by adding
the return types to mach.c.  We don't build any other machines; they
are broken since commit a46ee91 (May 19, 2013) declared void prolog()
and commit fd91851 (Nov 10, 2016) declared void mes(), with both
declarations in mach/proto/ncg/fillem.c.

Also fix mach/vc4/ncg/mach.c where type full is long, so fprintf()
must use "%ld" not "%d" to print full nlocals.

6 years agoMore prototypes, less register in mach/proto/ncg
George Koehler [Mon, 13 Nov 2017 17:44:17 +0000 (12:44 -0500)]
More prototypes, less register in mach/proto/ncg

Files that #include "equiv.h" must do so after including "data.h", now
that a function prototype in equiv.h uses type rl_p from data.h.

Adjust style, changing some `for(...)` to `for (...)`.  The style in
mach/proto/ncg is less than consistent; the big annoyance now is that
some files want tabs at 4 spaces, others want tabs at 8 spaces.

6 years agoAdd prototypes to functions in subr.c
George Koehler [Sun, 12 Nov 2017 21:11:05 +0000 (16:11 -0500)]
Add prototypes to functions in subr.c

Put the declarations in "data.h", because that header declares the
types cost_t and token_p.  Also #include <cgg_cg.h> from "data.h" to
get types c3_p and set_p, and guard <cgg_cg.h> against multiple
inclusion.

6 years agoPrototypes for string functions. More static.
George Koehler [Sun, 12 Nov 2017 16:25:18 +0000 (11:25 -0500)]
Prototypes for string functions.  More static.

6 years agoRemove old "assert.h" in mach/proto/ncg
George Koehler [Sun, 12 Nov 2017 00:35:48 +0000 (19:35 -0500)]
Remove old "assert.h" in mach/proto/ncg

*Important:*  You must "make clean" after checking out this commit,
because the build had copied the old "assert.h" to several places in
obj/.  If you don't "make clean", then the compiler finds the old
"assert.h" before libc <assert.h>, and the build fails because this
commit removes badassertion() in subr.c.  After "make clean", the
compiler finds libc <assert.h> and the build succeeds.

6 years agoUse libc assert(); fix dependencies; unbreak isduo().
George Koehler [Sat, 11 Nov 2017 21:09:05 +0000 (16:09 -0500)]
Use libc assert(); fix dependencies; unbreak isduo().

Switch from custom assert() to libc assert() in mach/proto/as.
Continue to disable asserts if DEBUG == 0.

This change found a problem in the build system; comm2.y was missing
depedencies on comm0.h and comm1.h.  Add the missing dependencies to
the cppfile rule.  Allow the dependencies by modifying cppfile in
first/build.lua to act like cfile if t.dir is false.

Now that comm2.y gets rebuilt, I must fix the wrong prototype of
yyparse() in comm1.h.

I got unlucky as induo() in comm5.c was reading beyond the end of the
array.  It found an operator "= " ('=' then space) in the garbage, so
it returned a garbage token number, and "VAR = 123" became a syntax
error.  Unbreak induo() by terminating the array.

6 years agoAdd more const in <object.h>.
George Koehler [Sat, 11 Nov 2017 18:08:13 +0000 (13:08 -0500)]
Add more const in <object.h>.

6 years agoAdd more prototypes in mach/proto/as
George Koehler [Sat, 11 Nov 2017 04:30:46 +0000 (23:30 -0500)]
Add more prototypes in mach/proto/as

Change "register i;" to "int i;" to so clang stops warning about
implicit int.  Use function prototypes so clang stops warning about
implicitly declared functions.

6 years agobts2str(), long2str() are in <ack_string.h>
George Koehler [Fri, 10 Nov 2017 22:31:11 +0000 (17:31 -0500)]
bts2str(), long2str() are in <ack_string.h>

6 years agoAdjust dependencies in modules/src{print,string,system}
George Koehler [Fri, 10 Nov 2017 21:26:27 +0000 (16:26 -0500)]
Adjust dependencies in modules/src{print,string,system}

Drop dependency on <ansi.h> in modules+headers; assume that compiler
knows ANSI C89.

Add missing dependency from print to string; #include <ack_string.h>.

Because <print.h> had commented out the declarations of sys_lock() and
sys_unlock(), I now stop building lock.c and unlock.c.

6 years agoRemove functions that also exist in libc.
George Koehler [Fri, 10 Nov 2017 04:25:17 +0000 (23:25 -0500)]
Remove functions that also exist in libc.

Some of these functions were slightly different from libc:

 - This strncpy() didn't pad the buffer with '\0' bytes beyond the end
   of the string; libc does the padding.  This string.3 manual said
   that this strncpy() does "null-padding", but it didn't.

 - This strcmp() and strncmp() compared using char (which might be
   signed); libc compares using unsigned char.

6 years agoDelete old "assert.h" files; use libc <assert.h>.
George Koehler [Fri, 10 Nov 2017 03:22:13 +0000 (22:22 -0500)]
Delete old "assert.h" files; use libc <assert.h>.

Edit build.lua for programs losing their private assert.h, so they
depend on a list of .h files excluding assert.h.

Remove modules/src/assert; it would be a dependency of cpp.ansi but we
didn't build it, so cpp.ansi uses the libc assert.

I hope that libc <assert.h> can better report failed assertions.  Some
old "assert.h" files didn't report the expression.  Some reported a
literal "x", because traditional C expanded the macro parameter x in
"x", but ANSI C89 doesn't expand macro parameters in string literals.

6 years agoMerge pull request #63 from kernigh/kernigh-rm-fix
David Given [Tue, 31 Oct 2017 10:33:52 +0000 (11:33 +0100)]
Merge pull request #63 from kernigh/kernigh-rm-fix

small changes to C, Pascal, Modula-2, em_opt, getopt(), Makefile

6 years agoUpdate comment after commit 9a965ef.
George Koehler [Tue, 31 Oct 2017 01:24:18 +0000 (21:24 -0400)]
Update comment after commit 9a965ef.

6 years agoImprove how Makefile handles multiple goals.
George Koehler [Mon, 30 Oct 2017 21:09:19 +0000 (17:09 -0400)]
Improve how Makefile handles multiple goals.

 - Don't run BUILDSYSTEM more than once if there is more than one goal
   with '+'.
 - Don't pass goals without '+' to BUILDSYSTEM.
 - Use $(MAKE) because "make" might not be GNU make.  For me, "make"
   is BSD make.
 - Add a comment so readers know MAKECMDGOALS is special.

Over in README, remove Lua from requirements; we always ignore any
installed Lua and build our own.  Increase guesses for free space
because we build more platforms.  Don't need to type MAKEFLAGS=.

6 years agoDon't use '-' in option string to getopt().
George Koehler [Mon, 30 Oct 2017 02:00:43 +0000 (22:00 -0400)]
Don't use '-' in option string to getopt().

@dram reported a build failure in FreeBSD at
https://github.com/davidgiven/ack/issues/1#issuecomment-273668299

Linux manual for getopt(3) says:
> If the first character of optstring is '-', then each nonoption
> argv-element is handled as if it were the argument of an option with
> character code 1....
>
> The use of '+' and '-' in optstring is a GNU extension.

GNU/Linux and OpenBSD handle '-' in this special way, but FreeBSD
seems not to.  If '-' is not special, then em_ego can't find its input
file, so the build must fail.  This commit stops using '-' in both
em_b and em_ego, but doesn't change mcg.

Also fix em_ego -O3 to not act like -O4.

6 years agoUse (arith) 1 << ... when getting the sign bit.
George Koehler [Sun, 29 Oct 2017 21:45:10 +0000 (17:45 -0400)]
Use (arith) 1 << ... when getting the sign bit.

This prevents an overflow reported by @hexcoder- in
https://github.com/davidgiven/ack/issues/56

lang/cem/cpp.ansi/LLlex.c used a plain 1 << ... and caused an overflow
on machines where sizeof(int) < sizeof(long).  Using 1L << ... would
work for now but might fail later if arith became long long.

C doesn't specify whether negative integers use 2's complement or some
other format.  Therefore, (arith) 1 << ... has an undefined value.  It
should still work because the value is some integer where the sign bit
is set and all other bits are clear.

(unsigned arith) 1 << ... would also get the sign bit, but casting it
from unsigned back to signed would make the same undefined value.

(arith) -1 << ... would assume 2's complement.

6 years agoRemove UNSIGNED_ARITH from a few more files.
George Koehler [Sun, 29 Oct 2017 21:03:51 +0000 (17:03 -0400)]
Remove UNSIGNED_ARITH from a few more files.

I missed these in commits 649410b and 1ab1306.

6 years agoAlways use unsigned long in lang/cem
George Koehler [Sun, 29 Oct 2017 21:01:29 +0000 (17:01 -0400)]
Always use unsigned long in lang/cem

Same reason as commit 649410b.

6 years agoFix pattern that was rewriting func(! var, var) as func(1).
George Koehler [Sun, 29 Oct 2017 18:53:33 +0000 (14:53 -0400)]
Fix pattern that was rewriting func(! var, var) as func(1).

Bug reported by Rune, see
 - https://sourceforge.net/p/tack/mailman/message/35809953/
 - https://github.com/davidgiven/ack/issues/62

In EM code, beq and bne pop 2 values and compare them, but teq and tne
pop only 1 value and compare it with zero.  We need cms to compare 2
values; other patterns may convert cmi or cmu to cms.

6 years agoFor DIAGOPT, change outshort(patno) to outshort(lino - 1).
George Koehler [Sun, 29 Oct 2017 18:18:47 +0000 (14:18 -0400)]
For DIAGOPT, change outshort(patno) to outshort(lino - 1).

This is more useful when looking for patterns; lino - 1 is probably
the line number in the patterns file.  DIAGOPT is off by default but
one can edit optim.h to enable it.

The other changes just clean up whitespace.

6 years agomktab depends on some of the *.h files.
George Koehler [Sun, 29 Oct 2017 01:41:59 +0000 (21:41 -0400)]
mktab depends on some of the *.h files.

If I edit optim.h to #define DIAGOPT then mktab must get rebuilt.

6 years agoGet correct sign of a MOD b when (a > 0) and (b < 0).
George Koehler [Sat, 28 Oct 2017 23:55:06 +0000 (19:55 -0400)]
Get correct sign of a MOD b when (a > 0) and (b < 0).

Reported by me in https://github.com/davidgiven/ack/issues/60

This doesn't change DIV.  Right now a DIV b does floor division and
a MOD b has the sign of b.  This is the same as Lua, Python, Ruby,
Tcl; but is different from other Modula-2 implementations.

6 years agoAlways use unsigned long.
George Koehler [Sat, 28 Oct 2017 21:56:20 +0000 (17:56 -0400)]
Always use unsigned long.

Traditional C compilers had long but not unsigned long.  I now assume
that everyone can compile unsigned long.  Remove macro UNSIGNED_ARITH
and act like it is always defined.  The type `unsigned arith` works
because arith is a macro for long.

6 years agoTerminal now writes to fd 1, not fd 0.
George Koehler [Sat, 28 Oct 2017 21:20:39 +0000 (17:20 -0400)]
Terminal now writes to fd 1, not fd 0.

Fixes problem where `./program </dev/null` didn't show output.

6 years agoDon't check ferror(fp) when reading fp.
George Koehler [Sat, 28 Oct 2017 20:20:48 +0000 (16:20 -0400)]
Don't check ferror(fp) when reading fp.

If feof(fp) or ferror(fp) was set, then our libc returned EOF for all
later reads without trying to read.  Our libc now behaves like BSD
(and probably Illumos and musl) by checking only feof(fp).  For
difference, glibc doesn't check feof(fp).

I described the difference between our libc and BSD libc in
https://sourceforge.net/p/tack/mailman/message/35430300/

6 years agoDelete unused misc/getpw.c from libc.
George Koehler [Sat, 28 Oct 2017 18:25:39 +0000 (14:25 -0400)]
Delete unused misc/getpw.c from libc.

@hexcoder- reported in https://github.com/davidgiven/ack/issues/57
that our getpw() has bugs.

I don't fix these bugs, because Illumos and Linux manual pages say
that getpw() is obsolete.  The function can overflow its buffer, so it
is never safe to use.  Our libc did not build getpw().

6 years agoDelete malloc.h and tgmath.h from libc.
George Koehler [Sat, 28 Oct 2017 17:57:10 +0000 (13:57 -0400)]
Delete malloc.h and tgmath.h from libc.

This malloc.h might get confused with the private malloc.h in our
libc.  C programs should #include <stdlib.h> for malloc().

This tgmath.h has no useful content, and never worked because
complex.h is missing.

Touch build.lua (by deleting some whitespace) so the *.h globs see
the deletions.

6 years agoBuild fdopen(), hypot(), putenv() in libc.
George Koehler [Sat, 28 Oct 2017 17:06:38 +0000 (13:06 -0400)]
Build fdopen(), hypot(), putenv() in libc.

These functions are in POSIX; hypot() is in C99.  Also build cabs()
because it rides with hypot(), but don't declare cabs() in any header
file, because our compiler can't parse C99 "double complex" type.

Touch build.lua so it sees that .c files moved.

6 years agoAdd .pas as a Pascal suffix.
George Koehler [Sat, 28 Oct 2017 15:59:35 +0000 (11:59 -0400)]
Add .pas as a Pascal suffix.

Requested by @dram in https://github.com/davidgiven/ack/issues/41

6 years agoRemove old files: TakeAction config.pm
George Koehler [Sat, 28 Oct 2017 14:55:51 +0000 (10:55 -0400)]
Remove old files: TakeAction config.pm

These files come from old build systems, and are useless since the old
build systems got deleted.  TakeAction is from proto.make system and
config.pm is from pmfile system.

6 years agoMerge pull request #59 from kernigh/kernigh-ppc-regs
David Given [Thu, 26 Oct 2017 14:15:55 +0000 (15:15 +0100)]
Merge pull request #59 from kernigh/kernigh-ppc-regs

PowerPC ncg: register allocation, floats, lxl

6 years agolwzu writes to the register in the token.
George Koehler [Thu, 19 Oct 2017 16:44:46 +0000 (12:44 -0400)]
lwzu writes to the register in the token.

6 years agoAdd a few more error checks and adjustments to reglap.
George Koehler [Thu, 19 Oct 2017 02:00:12 +0000 (22:00 -0400)]
Add a few more error checks and adjustments to reglap.

In util/ncgg, add two more errors for tables using reglap:
 - "Two sizes of reg_float can't be same size"
 - "Missing reg_float of size %d to contain %s"

In mach/proto/ncg, rename macro isregvar_size() to PICK_REGVAR(), so
the macro doesn't look like a function.  This macro sometimes doesn't
evaluate its second argument.

In mach/powerpc/ncg/mach.c, change type of lfs_set to uint32_t, and
change the left shifts from 1U<<regno to (uint32_t)1<<regno, because
1U would be too small for machines with 16-bit int.

6 years agoRename RELOLIS to RELOPPC_LIS.
George Koehler [Wed, 18 Oct 2017 19:39:31 +0000 (15:39 -0400)]
Rename RELOLIS to RELOPPC_LIS.

This relocation is specific to PowerPC.  @davidgiven suggested the
name RELOPPC_LIS in
https://github.com/davidgiven/ack/pull/52#issuecomment-279856501

Reindent the list in h/out.h and util/led/ack.out.5 because
RELOLIS_PPC is a long name.  I use spaces and no tabs because the tabs
looked bad in the manual page.

6 years agoEdit descr for linuxppc. Use powerpc.descr of ego.
George Koehler [Wed, 18 Oct 2017 17:23:01 +0000 (13:23 -0400)]
Edit descr for linuxppc.  Use powerpc.descr of ego.

ack -mlinuxppc -O4 now runs more phases of ego, including the register
allocation phase, so ncg emits better code.

Set MACHOPT_F=-m3 as I did it for osxppc; see commit 0c2b6f5.

Remove CC_ALIGN=-Vr so bitfields agree with gcc for PowerPC Linux.

Remove unused C_LIB and OLD_C_LIB.

6 years agoUse lwzu, stwu to tighten more loops.
George Koehler [Wed, 18 Oct 2017 16:12:42 +0000 (12:12 -0400)]
Use lwzu, stwu to tighten more loops.

Because lwzu or stwu moves the pointer, I can remove an addi
instruction from the loop, so the loop is slightly faster.

I wrote a benchmark in Modula-2 that exercises some of these loops.  I
measured its time on my old PowerPC Mac.  Its user time decreases from
8.401s to 8.217s with the tighter loops.

6 years agoAdd more rules for single-precision reg_float.
George Koehler [Tue, 17 Oct 2017 21:53:03 +0000 (17:53 -0400)]
Add more rules for single-precision reg_float.

The result of single-precision fadds, fsubs, and such can go into a
register variable, like we already do with double precision.  This
avoids an extra fmr from a temporary register to the regvar.

6 years agoStop inlining code to convert integers to floats.
George Koehler [Tue, 17 Oct 2017 21:00:28 +0000 (17:00 -0400)]
Stop inlining code to convert integers to floats.

Do the conversion by calling .cif8 or .cuf8 in libem, as it was done
before my commit 1de1e8f.  I used the inline conversion to experiment
with the register allocator, which was too slow until c5bb3be.

Now that libem has the only copy of the code, move some comments and
code changes there.

6 years agoUse my new regvar_w() and regvar_d() in PowerPC ncg.
George Koehler [Tue, 17 Oct 2017 18:15:33 +0000 (14:15 -0400)]
Use my new regvar_w() and regvar_d() in PowerPC ncg.

Rename GPRE to GPR_EXPR, then define FPR_EXPR and FSREG_EXPR.  Use
them for moves to register variables.

Keep "kills regvar($1)", because deleting it and recompiling libc
would cause many failures in my test programs.  Add comment to warn,
  /* ncg fails to infer that regvar($1) is dead! */

Remove "kills LOCAL %off==$1" because it seems to have no effect.

6 years agoAdd regvar_w() and regvar_d() for use with reglap.
George Koehler [Tue, 17 Oct 2017 16:05:41 +0000 (12:05 -0400)]
Add regvar_w() and regvar_d() for use with reglap.

If the ncg table uses reglap, then regvar($1, reg_float) would have
two sizes of registers.  An error from ncgg would happen if regvar()
was in a token that allows only one size.  Now one can pick a size
with regvar_w() for word size or regvar_d() for double-word size.

Add regvar_d and regvar_w as keywords in ncgg.  Modify EX_REGVAR to
include the register size.  In ncg, add some checks for the register
size.  In tables without reglap, regvar() works as before, and ncg
ignores the register size in EX_REGVAR.

6 years agoDo a move when coercing FREG to FREG or FSREG to FSREG.
George Koehler [Mon, 16 Oct 2017 16:07:55 +0000 (12:07 -0400)]
Do a move when coercing FREG to FREG or FSREG to FSREG.

6 years agoPowerPC ncg never uses the rules to stack LOCAL or DLOCAL.
George Koehler [Sun, 15 Oct 2017 19:22:52 +0000 (15:22 -0400)]
PowerPC ncg never uses the rules to stack LOCAL or DLOCAL.

6 years agoFix reglap for procedures that use both sizes of reg_float.
George Koehler [Sun, 15 Oct 2017 17:15:03 +0000 (13:15 -0400)]
Fix reglap for procedures that use both sizes of reg_float.

After the RA phase of ego, a procedure may put single-word and
double-word values in the same reg_float.  Then ncg will use both
LOCAL and DLOCAL tokens at the same offset.

I add isregvar_size() to ncg.  It receives the size of the LOCAL or
DLOCAL token, and picks the register of the correct size.  This fixes
a problem where ncg got the wrong-size register and corrupted the
stack.  This problem caused one of my test programs to segfault from
stack underflow.

Also adjust how fixregvars() handles both sizes.

6 years agoAdd function prototypes to mach/proto/ncg/regvar.c
George Koehler [Sun, 15 Oct 2017 15:01:18 +0000 (11:01 -0400)]
Add function prototypes to mach/proto/ncg/regvar.c

6 years agoMerge branch 'default' into kernigh-linuxppc
George Koehler [Sat, 14 Oct 2017 17:50:49 +0000 (13:50 -0400)]
Merge branch 'default' into kernigh-linuxppc

This merges several fixes and improvements from upstream.  This
includes commit 5f6a773 to turn off qemuppc.  I see several failing
tests from qemuppc; this merge will hide the test failures.

6 years agoAdd reglap to ncg. Add 4-byte reg_float to PowerPC ncg.
George Koehler [Sat, 14 Oct 2017 16:40:04 +0000 (12:40 -0400)]
Add reglap to ncg.  Add 4-byte reg_float to PowerPC ncg.

The new feature "reglap" allows two sizes of floating-point register
variables (reg_float), if each register overlaps a single register of
the other size.  PowerPC ncg uses reglap to define 4-byte instances
of f14 to f31 that overlap the 8-byte instances.

When ncgg sees the definition of fs14("f14")=f14, it removes the
8-byte f14 from its rvnumbers array, and adds the 4-byte fs14 in its
place.  Later, when ncg puts a variable in fs14, if it is an 8-byte
variable, then ncg switches to the 8-byte f14.  The code has
/* reglap */ comments in util/ncgg or #ifdef REGLAP in mach/proto/ncg

reglap became necessary because my commit a20b87c caused PowerPC ego
to allocate reg_float in both 4-byte and 8-byte sizes.

7 years agoMore ansification.
David Given [Sun, 6 Aug 2017 13:57:49 +0000 (15:57 +0200)]
More ansification.

7 years agoDon't try to run the tests on Travis.
David Given [Sun, 6 Aug 2017 12:27:15 +0000 (14:27 +0200)]
Don't try to run the tests on Travis.

7 years agomkstemp() is a bit more complex than it looks; because ego wants to use the
David Given [Sun, 6 Aug 2017 12:25:12 +0000 (14:25 +0200)]
mkstemp() is a bit more complex than it looks; because ego wants to use the
same base name and generate multiple files based on it, we can't really use
mkstemp() for every temporary file. Instead, use mkstemp() once on a
placeholder, then generate temporary names based on this. (And delete the
placeholder once we've finished.)

7 years agoDon't build mcg as part of linuxppc; it's not used and crashes Travis.
David Given [Sun, 6 Aug 2017 11:54:07 +0000 (13:54 +0200)]
Don't build mcg as part of linuxppc; it's not used and crashes Travis.

7 years agoTurn of qemuppc for now; it's crashing on Travis builds.
David Given [Sun, 6 Aug 2017 11:48:01 +0000 (13:48 +0200)]
Turn of qemuppc for now; it's crashing on Travis builds.

7 years agoFix Travis syntax error...
David Given [Sun, 6 Aug 2017 11:39:21 +0000 (13:39 +0200)]
Fix Travis syntax error...

7 years agoStop using mktemp() --- on Haiku, it always generates the same filenames,
David Given [Sun, 6 Aug 2017 11:22:05 +0000 (13:22 +0200)]
Stop using mktemp() --- on Haiku, it always generates the same filenames,
pretty much guaranteeing temporary file overwrites on parallel builds. Use
mkstemp() instead which creates the files atomically.

7 years agoAnsification, warning fixes, C89ification.
David Given [Sun, 6 Aug 2017 10:42:17 +0000 (12:42 +0200)]
Ansification, warning fixes, C89ification.

7 years agoTurn off OSX Travis builds --- this is going to be hard to make work.
David Given [Sun, 6 Aug 2017 10:38:38 +0000 (12:38 +0200)]
Turn off OSX Travis builds --- this is going to be hard to make work.

7 years agoAnsification and warning fixes.
David Given [Sun, 6 Aug 2017 09:58:36 +0000 (11:58 +0200)]
Ansification and warning fixes.

7 years agoAttempt to correct file system case sensitivity.
David Given [Sun, 6 Aug 2017 09:15:53 +0000 (11:15 +0200)]
Attempt to correct file system case sensitivity.

7 years agoTry using trusty and non-sudo build environments.
David Given [Sun, 6 Aug 2017 08:56:40 +0000 (10:56 +0200)]
Try using trusty and non-sudo build environments.

7 years agoBuild with linux/clang; try building on osx/clang.
David Given [Sun, 6 Aug 2017 08:44:38 +0000 (10:44 +0200)]
Build with linux/clang; try building on osx/clang.

7 years agoMerge from trunk.
David Given [Sun, 6 Aug 2017 08:42:16 +0000 (10:42 +0200)]
Merge from trunk.

7 years agoMerge.
David Given [Sat, 5 Aug 2017 19:47:40 +0000 (21:47 +0200)]
Merge.

7 years agoAdd missing headers.
David Given [Sat, 5 Aug 2017 19:46:43 +0000 (21:46 +0200)]
Add missing headers.

7 years agoUpdate man page not to mention the filename length restriction removed in
David Given [Tue, 1 Aug 2017 22:07:51 +0000 (00:07 +0200)]
Update man page not to mention the filename length restriction removed in
893471a42eb4d189cc0ba1dac1864d6f151bccd7.

7 years agoDon't define functions called itoa(), because this causes problems on platforms
David Given [Sun, 23 Jul 2017 19:19:07 +0000 (21:19 +0200)]
Don't define functions called itoa(), because this causes problems on platforms
that define itoa() in their libcs.

7 years agoReorder registers. Fix problem with ret 8.
George Koehler [Sat, 18 Feb 2017 00:32:27 +0000 (19:32 -0500)]
Reorder registers.  Fix problem with ret 8.

After c5bb3be, ncg began to allocate regvars from r13 up.  I reorder
the regvars so ncg again allocates them from r31 down.  I also reorder
the other registers.

This exposed a bug in my rule for ret 8.  It was wrong if item %2 was
in r3, because I moved %1 to r3 before %2 to r4.  Fix it by adding
back an individual register class for r3 (called REG3 here, GPR3 in
c5bb3be).

Also fix my typo in mach.c that made a syntax error in assembly.

7 years agoFix comparison of 4-byte floats.
George Koehler [Sat, 18 Feb 2017 00:29:45 +0000 (19:29 -0500)]
Fix comparison of 4-byte floats.

I broke it in f64b7d8.  My stack pattern had the wrong type of
registers.  The comparison popped too many bytes and corrupted the
stack.

7 years agoRemove .ret from libem and inline the code.
George Koehler [Fri, 17 Feb 2017 02:18:39 +0000 (21:18 -0500)]
Remove .ret from libem and inline the code.

This removes a wrong-way dependency of libsys on libem.  The C
functions in libsys called .ret, but libsys is after libem in the
linker arguments, so the linker didn't find .ret unless something else
had called .ret.  Almost everything called .ret, but I got a linker
error when I wrote an assembly program using the EM runtime, because
my assembly program didn't call .ret.

Add a dummy comment to build.lua, so git checkout touches that file,
the build system reconfigures itself, and the *.s glob sees that ret.s
has gone.

7 years agoFor PowerPC, never put a reg_float value in a reg_any.
George Koehler [Fri, 17 Feb 2017 01:30:17 +0000 (20:30 -0500)]
For PowerPC, never put a reg_float value in a reg_any.

With this type check, I can change the size checks into assertions.

7 years agoSwitch error() and fatal() in mach/proto/ncg to stdarg.
George Koehler [Fri, 17 Feb 2017 01:26:53 +0000 (20:26 -0500)]
Switch error() and fatal() in mach/proto/ncg to stdarg.

This is like David Given's change to util/ncgg in d89f172.  I need
this change in mach/proto/ncg to see fatal messages, because a 64-bit
pointer doesn't fit in an int.

7 years agoIn ego, put both words and double-words in reg_float.
George Koehler [Fri, 17 Feb 2017 00:55:52 +0000 (19:55 -0500)]
In ego, put both words and double-words in reg_float.

The size of a reg_float isn't in the descr file, so ego doesn't know.
PowerPC and SPARC are the only arches with floating-point registers in
their descr files.  PowerPC and SPARC registers can hold both 4-byte
and 8-byte floats, so I want ego to do both sizes.

This might break our SPARC code expander because ego doesn't know that
8-byte values take 2 registers in SPARC.  (So ego might allocate too
many registers and deallocate too much stack space.)  We don't build
the SPARC code expander, and its descr file is already wrong: its list
of register save costs is too short, so ego will read past the end of
the array.

This commit doesn't fix the problem with ego and PowerPC ncg.  Right
now, ncg refuses to put 4-byte floats in registers, but ego expects
them to get registers and deallocates their stack space.  So ncg emits
programs that use the deallocated space, and the values of 4-byte
floats become corrupt.

7 years agoAdd floating-point register variables to PowerPC ncg.
George Koehler [Thu, 16 Feb 2017 00:34:07 +0000 (19:34 -0500)]
Add floating-point register variables to PowerPC ncg.

Use f14 to f31 as register variables for 8-byte double-precison.
There are no regvars for 4-byte double precision, because all
regvar(reg_float) must have the same size.  I expect more programs to
prefer 8-byte double precision.

Teach mach/powerpc/ncg/mach.c to emit stfd and lfd instructions to
save and restore 8-byte regvars.  Delay emitting the function prolog
until f_regsave(), so we can use one addi to make stack space for both
local vars and saved registers.  Be more careful with types in mach.c;
don't assume that int and long and full are the same.

In ncg table, add f14 to f31 as register variables, and some rules to
use them.  Add rules to put the result of fadd, fsub, fmul, fdiv, fneg
in a regvar.  Without such rules, the result would go in a scratch
FREG, and we would need fmr to move it to the regvar.  Also add a rule
for pat sdl inreg($1)==reg_float with STACK, so we can unstack the
value directly into the regvar, again without a scratch FREG and fmr.

Edit util/ego/descr/powerpc.descr to tell ego about the new float
regvars.  This might not be working right; ego usually decides against
using any float regvars, so ack -O1 (not running ego) uses the
regvars, but ack -O4 (running ego) doesn't use the regvars.

Beware that ack -mosxppc runs ego using powerpc.descr but -mlinuxppc
and -mqemuppc run ego without a config file (since 8ef7c31).  I am
testing powerpc.descr with a local edit to plat/linuxppc/descr to run
ego with powerpc.descr there, but I did not commit my local edit.

7 years agoMerge pull request #52 from kernigh/pr-relolis
David Given [Wed, 15 Feb 2017 21:04:14 +0000 (21:04 +0000)]
Merge pull request #52 from kernigh/pr-relolis

PowerPC: more ha16/lo16 from ncg, new RELOLIS

7 years agoImplement lxl for PowerPC ncg.
George Koehler [Tue, 14 Feb 2017 04:22:31 +0000 (23:22 -0500)]
Implement lxl for PowerPC ncg.

This fixes lxl 1 (so it follows the static chain, not the dynamic
chain) and provides lxl 2 and greater.  The Modula-2 compiler uses lxl
for nested procedures, so they can access the variables of the
enclosing procedures.

7 years agoRemove REG_PAIR.
George Koehler [Mon, 13 Feb 2017 23:11:27 +0000 (18:11 -0500)]
Remove REG_PAIR.

I added REG_PAIR in cfbc537 to speed up the register allocator,
because ncg was taking about 2 seconds on each sti 8.  I defined only
4 such pairs, so allocating REG_PAIR was much faster than allocating
REG REG.

After my last commit c5bb3be, allocation of REG REG is fast, and
REG_PAIR seems unnecessary.

7 years agoSpeed up register allocation by removing some register classes.
George Koehler [Mon, 13 Feb 2017 22:44:46 +0000 (17:44 -0500)]
Speed up register allocation by removing some register classes.

The table for PowerPC had placed each GPR and FPR into an individual
register class (like GPR3, GPR4, FPR1, FPR2), and had used these
classes to coerce stack values into specific registers.  But ncg does
not like having many register classes.

In http://tack.sourceforge.net/olddocs/ncg.pdf
Hans van Staveren wrote:

> Every extra property means the register set is more unorthogonal and
> *cg* execution time is influenced by that, because it has to take
> into account a larger set of registers that are not equivalent.  So
> try to keep the number of different register classes to a minimum.

Recent changes to the PowerPC table have removed many coercions to
specific registers.  Many functions in libem switched from taking
values in registers to taking them from the stack (see dc05cb2).

I now remove all 64 individual register classes of GPR and FPR.  In
the few cases where I need a stack value in a specific register, I now
do a move (as the arm and m68020 tables do).

This commit speeds the compilation of some files.  For my test file
fconv.c, the compilation time goes from over 20 seconds to under 1
second.  My fconv.c has 4 conversions from floats to integers, and the
table has my experimental rules that do the conversions by allocating
4 or 5 registers.

7 years agoAdd pat cms !defined($1)
George Koehler [Mon, 13 Feb 2017 21:52:32 +0000 (16:52 -0500)]
Add pat cms !defined($1)

Switch .cms to pass inputs and outputs on the real stack, not in
registers; like we do with .and, .or (81c677d) and .xor (c578c49).

At this point, nearly all functions in libem use the real stack, not
registers, for passing inputs and outputs.  This simplifies the ncg
table (which needs fewer lists of specific registers) but slows calls
to libem.

For example, after ba9b021, each call to .aar4 is about 10
instructions slower.  I moved 3 inputs and 1 output from registers to
the real stack.  A program would take 4 instructions to move registers
to stack, 4 to move stack to registers, and perhaps 2 to adjust the
stack pointer.

7 years agoAdd missing instances of "kills ALL" or "with STACK".
George Koehler [Mon, 13 Feb 2017 21:38:26 +0000 (16:38 -0500)]
Add missing instances of "kills ALL" or "with STACK".

7 years agoUse .los4 in lar 4 and .sts4 in sar 4.
George Koehler [Mon, 13 Feb 2017 20:22:00 +0000 (15:22 -0500)]
Use .los4 in lar 4 and .sts4 in sar 4.

Our libem had two implementations of loading a block from a stack, one
for lar 4 and one for los 4.  Now lar 4 and los 4 share the code in
.los4.  Likewise, sar 4 and sts 4 share the code in .sts4.

Rename .los to .los4 and .sts to .sts4, because they implement los 4
and sts 4.  Remove the special case for loading or storing 4 bytes,
because we can do it with 1 iteration of the loop.  Remove the lines
to "align size" where the size must already be a multiple of 4.

Fix the upper bound check in .aar4.

Change .aar4, .lar4, .los4, .sar4, .sts4 to pass all operands on the
real stack, except that .los4 and .sts4 take the size in register r3.
Have .aar4 set r3 to the size of the array element.  So lar 4 is just
.aar4 then .los4, and sar 4 is just .aar4 then .sts4.

ncg no longer calls .lar4 and .sar4 in libem, because it inlines the
code; but I keep .lar4 and .sar4 in libem, because mcg references
them.  They might or might not work in mcg.

7 years agoChange .fef8 and .fif8 to pass values on the stack.
George Koehler [Sun, 12 Feb 2017 21:44:37 +0000 (16:44 -0500)]
Change .fef8 and .fif8 to pass values on the stack.

Reorder the code in .fef8 and .fif8 so that in the usual case, we fall
through to the blr without taking any branches.  The usual case, by my
guess, is .fef8 with normalized numbers or .fif8 with small integers.

I change .fef8 and .fif8 to pass values on the real stack, not in
specific registers.  This simplifies the ncg table, and might help me
experiment with changes to the ncg table.

This change might or might not help mcg.  Seems that mcg always uses
the stack to pass values to libem, but I have not tested .fef8 or
.fif8 with mcg.

7 years agoExperiment with conversions between integers and floats.
George Koehler [Sun, 12 Feb 2017 04:23:47 +0000 (23:23 -0500)]
Experiment with conversions between integers and floats.

Switch some conversions from libem calls to inline code.  The
conversions from integers to floats are now too slow, because each
conversion allocates 4 or 5 registers, and the register allocator is
too slow.  I might use these slow conversions to experiment with the
register allocator.

I add the missing conversions between 4-byte single floats and
integers, simply by going through 8-byte double floats.  (These
replace the calls to nonexistant functions in libem.)

I remove the placeholder for fef 4, because it doesn't exist in libem,
and our language runtimes only use fef 8.

7 years agoImplement blm and bls using an inline loop.
George Koehler [Sun, 12 Feb 2017 00:30:12 +0000 (19:30 -0500)]
Implement blm and bls using an inline loop.

This replaces a call to memmove() in libc.  That was working for me,
but it can fail because EM programs don't always link to libc.

blm and bls only need to copy aligned words.  They don't need to copy
bytes, and they don't need to copy between overlapping buffers, as
memmove() does.  So the new loop is simpler than memmove().

7 years agoEdit PowerPC assembly for .and, .cms, .ior, .xor, .zer
George Koehler [Sat, 11 Feb 2017 23:00:56 +0000 (18:00 -0500)]
Edit PowerPC assembly for .and, .cms, .ior, .xor, .zer

Remove one addi instruction from some loops.  These loops had
increased 2 pointers, they now increase 1 index.  I must initialize
the index, so I add "li r6, 0" before each loop.

Change .zer to use subf instead of neg, add.

Change .xor to take the size on the real stack, as .and and .or have
done since 81c677d.

7 years agoDocument RELOLIS from commit 1bf58cf.
George Koehler [Fri, 10 Feb 2017 16:59:34 +0000 (11:59 -0500)]
Document RELOLIS from commit 1bf58cf.

I hastily chose the name RELOLIS for this relocation type.  If we want
to rename it, we only need to edit these files:

 - h/out.h
 - mach/powerpc/as/mach5.c
 - util/amisc/ashow.c
 - util/led/ack.out.5
 - util/led/relocate.c

7 years agoUse "mr" and make a few other tweaks in PowerPC ncg table.
George Koehler [Fri, 10 Feb 2017 16:45:50 +0000 (11:45 -0500)]
Use "mr" and make a few other tweaks in PowerPC ncg table.

Use extended "mr" instead of basic "or" to move registers.  Both "mr"
and "or" encode the same machine instruction.  With "mr", I can more
easily search the assembly output for register moves.

Fold several stacking rules into a single rule ANY_BHW-REG to STACK.

Remove the EM patterns for loc mlu $2==2 and loc slu.  The first
pattern had the wrong size (should be $2==4, not $2==2).  Both
patterns were redundant.  They rewrote loc mlu as loc mli and loc slu
as loc sli, but this table doesn't have patterns for loc mli or loc
sli, so it is enough to rewrite mlu as mli and slu as sli.

7 years agoUse ha16/lo16 to load or store 1, 2, 8 bytes from labels.
George Koehler [Wed, 8 Feb 2017 17:31:14 +0000 (12:31 -0500)]
Use ha16/lo16 to load or store 1, 2, 8 bytes from labels.

Add the tokens IND_RL_B, IND_RL_H, IND_RL_H_S, IND_RL_D, along with
the rules to use them.  These rules emit shorter code.  For example,
loading a byte becomes lis, lbz instead of lis, addi, lbz.

While making this, I wrongly set IND_RL_D to size 4.  Then ncg made
infinite recursion in codegen() and stackupto(), until it crashed by
stack overflow.  I correctly set IND_RL_D to size 8, preventing the
crash.

7 years agoTrimming mach/powerpc/ncg/table
George Koehler [Wed, 8 Feb 2017 17:27:16 +0000 (12:27 -0500)]
Trimming mach/powerpc/ncg/table

Remove coercion from LABEL to REG.  The coercion never happens because
I have stopped putting LABEL on the stack.  Also remove LABEL from set
ANY_BHW.  Retain the move from LABEL to REG because pat gto uses it.

Remove li32 instruction, unused after the switch to the hi16, ha16,
lo16 syntax.

Remove COMMENT(...) lines from most moves.  In my opinion, they took
too much space, both in the table and in the assembly output.  The
stacking rules and coercions keep their COMMENT(...)  lines.

In test GPR, don't write to RSCRATCH.

Fold several coercions into a single coercion from ANY_BHW uses REG.

Use REG instead of GPR in stack patterns.  REG and GPR act the same,
because every GPR on the stack is a REG, but I want to be clear that I
expect a REG, not r0.

In code rules, sort SUM_RC before SORT_RR, so I can add SUM_RL later.

Remove rules to optimize loc loc cii loc loc cii.  If $2==$4, the
peephole optimizer can optimize it.  If $2!=$4, then the EM program is
missing a conversion from size $2 to size $4.

Remove rules to store a SEX_B with sti 1 or a SEX_H with sti 2.  These
rules would never get used, unless the EM program is missing a
conversion from size 4 to size 1 or 2.

7 years agoIn PowerPC ncg, allocate register for ha16[label].
George Koehler [Wed, 8 Feb 2017 17:23:06 +0000 (12:23 -0500)]
In PowerPC ncg, allocate register for ha16[label].

Use it to generate code like

    lis r12,ha16[__II0]
    lis r11,ha16[_f]
    lfs f1,lo16[_f](r11)
    lfs f2,lo16[__II0](r12)
    fadds f13,f2,f1
    stfs f13,lo16[_f](r11)

Here ncg has allocated r11 for ha16[_f].  We use r11 in lfs and again
in stfs.  Before this change, we needed an extra lis before stfs,
because ncg did not remember that ha16[_f] was in a register.

This example has a gap between ha16[__II0] and lo16[__II0], because
the lo16 is not in the next instruction.  This requires my previous
commit 1bf58cf for RELOLIS.  There is a gap because ncg emits the lis
as soon as I allocate it.  The "lfs f2,lo16[__II0](r12)" happens in a
coercion from IND_RL_W to FSREG.  The coercion allocates one FSREG but
may not allocate any other registers.  So I must allocate r12 earlier.
I allocate r12 in pat lae, but this causes a gap.

7 years agoUse ha16/lo16 to emit pairs of lis/stw, lis/lfs, lis/stfs.
George Koehler [Thu, 2 Feb 2017 15:48:25 +0000 (10:48 -0500)]
Use ha16/lo16 to emit pairs of lis/stw, lis/lfs, lis/stfs.

A 4-byte load from a label yields a token IND_RL_W.  This token emits
either lis/lwz or lis/lfs, if we want a general-purpose register or a
floating-point register.

7 years agoTweak some tokens in PowerPC ncg.
George Koehler [Wed, 8 Feb 2017 17:12:28 +0000 (12:12 -0500)]
Tweak some tokens in PowerPC ncg.

Remove the GPRINDIRECT token, and use the IND_RC_* tokens as operands
to instructions.  We no longer need to unpack an IND_RC_* token and
repack it as a GPRINDIRECT to use it in an instruction.

Allow storing IND_ALL_B and IND_ALL_H in register variables.  Create a
set ANY_BHW for anything that we can store in a regvar.

Push register variables on the stack without using GPRE, by changing
stwu to accept LOCAL.  Then ncg will replace the string ">>> BUG IN
LOCAL" with the register name.  (I copied ">>> BUG IN LOCAL" from
mach/arm/ncg/table.)

Fix the rule for "pat lil inreg($1)>0" to yield a IND_RC_W token, not
a register.  We might need to kill the token with "kills MEMORY".

Rename CONST_ALL to CONST_STACK, because it only includes constants on
the stack, and excludes CONST tokens.  Instructions still don't allow
CONST_STACK operands, so we still need to repack each CONST_STACK as a
CONST to use it in an instruction.

Rename LABEL_OFFSET_HI to just LABEL_HI, and same for LABEL_HA and
LABEL_HO.