ack.git
6 years agoModify the pc86 test harness to use 8086tiny instead of qemu. Sadly, some tests fail. dtrg-pc86
David Given [Sun, 3 Jun 2018 13:13:43 +0000 (15:13 +0200)]
Modify the pc86 test harness to use 8086tiny instead of qemu. Sadly, some tests fail.

6 years agoSet the terminal to raw nonblocking mode (so we don't need the shell script to
David Given [Sun, 3 Jun 2018 13:09:01 +0000 (15:09 +0200)]
Set the terminal to raw nonblocking mode (so we don't need the shell script to
run the emulator).

6 years agoModify 8086tiny so that the bios image is built in (which makes invocation
David Given [Sun, 3 Jun 2018 12:50:20 +0000 (14:50 +0200)]
Modify 8086tiny so that the bios image is built in (which makes invocation
vastly simpler).

6 years agoAdd a clean copy of the 8086tiny emulator.
David Given [Sun, 3 Jun 2018 10:13:26 +0000 (12:13 +0200)]
Add a clean copy of the 8086tiny emulator.

6 years agoMerge pull request #87 from davidgiven/dtrg-travis
David Given [Sat, 2 Jun 2018 20:00:48 +0000 (23:00 +0300)]
Merge pull request #87 from davidgiven/dtrg-travis

Get clang working on travis.

6 years agoTypo fix.
David Given [Sat, 2 Jun 2018 19:48:18 +0000 (21:48 +0200)]
Typo fix.

6 years agoCrudely tweak some of the prototypes to please clang, which is pickier about
David Given [Sat, 2 Jun 2018 19:31:18 +0000 (21:31 +0200)]
Crudely tweak some of the prototypes to please clang, which is pickier about
K&R C than gcc is.

6 years agoHonour CFLAGS set in the Makefile.
David Given [Sat, 2 Jun 2018 19:30:52 +0000 (21:30 +0200)]
Honour CFLAGS set in the Makefile.

6 years agoTry and *actually* use clang for the clang build.
David Given [Sat, 2 Jun 2018 19:01:03 +0000 (21:01 +0200)]
Try and *actually* use clang for the clang build.

Fixes: #67

6 years agoFix resource leak.
David Given [Sat, 2 Jun 2018 18:59:11 +0000 (20:59 +0200)]
Fix resource leak.

Closes: #75

6 years agoFormat.
David Given [Sat, 2 Jun 2018 18:57:43 +0000 (20:57 +0200)]
Format.

6 years agoCrudely bounds check some naked fscanf("%s") parsing. Fixes #79, mostly.
David Given [Sat, 2 Jun 2018 18:51:41 +0000 (20:51 +0200)]
Crudely bounds check some naked fscanf("%s") parsing. Fixes #79, mostly.

6 years agoMerge pull request #85 from davidgiven/dtrg-ed
David Given [Sat, 2 Jun 2018 16:23:19 +0000 (19:23 +0300)]
Merge pull request #85 from davidgiven/dtrg-ed

Fix the incompatibility with modern ed

6 years agoDon't need ed any more in the travis configuration.
David Given [Sat, 2 Jun 2018 16:08:55 +0000 (18:08 +0200)]
Don't need ed any more in the travis configuration.

6 years agoAdopt a copy of Minix 2's ed; this allows the ACK's antiquated ed scripts to
David Given [Sat, 2 Jun 2018 16:02:51 +0000 (18:02 +0200)]
Adopt a copy of Minix 2's ed; this allows the ACK's antiquated ed scripts to
run with a minimum of tweaking. Rewriting them for modern ed looks really hard.

Fixes: #84

6 years agoMerge pull request #81 from kernigh/kernigh-libfp
David Given [Fri, 11 May 2018 22:59:05 +0000 (00:59 +0200)]
Merge pull request #81 from kernigh/kernigh-libfp

software floats, line editor for CP/M

6 years agoUpdate cpm/README for console changes, `ack -fp`.
George Koehler [Mon, 7 May 2018 20:08:44 +0000 (16:08 -0400)]
Update cpm/README for console changes, `ack -fp`.

6 years agoBuild a smaller startrek for CP/M.
George Koehler [Fri, 4 May 2018 22:32:24 +0000 (18:32 -0400)]
Build a smaller startrek for CP/M.

The game was larger than the 64K address space.  I remove the intro
and replace part of libc.  Now I can play startrek in YAZE-AG.

6 years agoEnable the line editor in read() for CP/M.
George Koehler [Fri, 4 May 2018 22:21:01 +0000 (18:21 -0400)]
Enable the line editor in read() for CP/M.

This changes the BDOS call from CPM_BDOS_CONSOLE_INPUT to
CPN_BDOS_READ_CONSOLE_BUFFER.  This allows commands like ^H to delete
characters and ^C to exit to CCP.  This is more like how Unix read(2)
uses canonical mode of termios to read a line.

This change has a disadvantage: the user buffer to read(2) must now be
large enough for an entire line.  This is because CP/M, unlike Unix,
lacks a kernel buffer to hold the rest of the line.  If you use a
buffered input library like stdio to call read(2), then it works; but
if you try to read part of a line or a single character, then it
doesn't work.

6 years agoChange readstring() to use buffered input.
George Koehler [Fri, 4 May 2018 22:06:51 +0000 (18:06 -0400)]
Change readstring() to use buffered input.

Change from `uread(0, c, 1)` to `read(c)`, so input goes through
libpc's buffer.  If input is a tty in Unix, this reduces the number of
read(2) system calls from one per character to one per line.

This change will become necessary in CP/M when I enable the line
editor.

6 years agoUse `ack -mcpm -fp` for mandelbrot.c, startrek.c
George Koehler [Sat, 28 Apr 2018 17:18:27 +0000 (13:18 -0400)]
Use `ack -mcpm -fp` for mandelbrot.c, startrek.c

Add a variable %{ackldflags} so I can pass `-fp`.  This change seems
to cause the build to relink every ackprogram, because the link now
needs to use %{ackldflags} even if the flags are empty.

mandelbrot_c_cpm runs in YAZE-AG; startrek_c_cpm doesn't run because
it doesn't fit in the 16-bit address space.

6 years agoOptimize libfp. Don't lose -O6 in ackcflags.
George Koehler [Thu, 26 Apr 2018 02:48:28 +0000 (22:48 -0400)]
Optimize libfp.  Don't lose -O6 in ackcflags.

This drops 124 bytes from the mandelbrot command (from 15015 to 14891
bytes) but has almost no effect on performance; the command takes
about 144 seconds (in YAZE-AG) both before and after optimizing libfp.

6 years agoTeach i80 ncg to use libfp. Enable `ack -mcpm -fp`.
George Koehler [Wed, 25 Apr 2018 20:09:56 +0000 (16:09 -0400)]
Teach i80 ncg to use libfp.  Enable `ack -mcpm -fp`.

Old .o files stop working if they use floating point.  One must
recompile those files.  Old files don't call libfp in the correct way,
and may use symbols that I removed from libem.  I don't keep old
symbols in libem/flp.s, because a program that pulls both libfp and
flp.s would get "multiply defined" errors in the linker.

I teach mach/i80/ncg/table to use libfp by copying or adapting the
patterns from mach/i86/ncg/table.  I did not test all the patterns,
but I did use `ack -mcpm -fp -O4` to compile examples/mandelbrot.c,
then I ran it in the emulator YAZE-AG.  It worked, but it was slow.

6 years agoBuild (but don't use) libfp for cpm.
George Koehler [Wed, 25 Apr 2018 04:17:19 +0000 (00:17 -0400)]
Build (but don't use) libfp for cpm.

This library is for software floating point.  The i80 back end has
never implemented floating point, and might not be ready for libfp.
This commit only builds libfp without using it.

I edit first/build.lua and plat/build.lua to allow `ack -c.s`, then
use FP.script to edit the assembly code.  I edit FP.script so it
writes the edited assembly code to stdout, not to the input file.

6 years agoMerge pull request #73 from kernigh/kernigh-pr
David Given [Tue, 13 Mar 2018 12:57:28 +0000 (13:57 +0100)]
Merge pull request #73 from kernigh/kernigh-pr

better code from PowerPC ncg and mcg

6 years agoCheck LOI expressions to prevent a read after free.
George Koehler [Tue, 13 Mar 2018 00:58:31 +0000 (20:58 -0400)]
Check LOI expressions to prevent a read after free.

CS eliminates outer expressions before inner ones, as `x * y * z`
before `x * y`.  It does this by reversing the order of expressions in
the code.  This almost always works, but it sometimes doesn't work if
a STI changes the value number of a LOI.  In code like `expr1 LOI
expr2 STI expr2 LOI`, CS might eliminate the inner `expr2` before the
outer `expr2 LOI`.  This caused a read after free because the
occurrence of `expr2 LOI` pointed to the eliminated lines of `expr2`.

This bug went unnoticed until my recent changes caused CS to crash
with a double free.  I did not get the crash in OpenBSD, but I saw the
crash in Travis, then David Given reproduced the crash in Linux.  See
the discussion in https://github.com/davidgiven/ack/pull/73

6 years agoDon't read INSTR(l) after oldline(l) frees it.
George Koehler [Mon, 12 Mar 2018 00:10:13 +0000 (20:10 -0400)]
Don't read INSTR(l) after oldline(l) frees it.

This bug got in my way while I was looking for another read-after-free
bug in the CS phase.

6 years agoThere are two places where names are added to the global symbol table; one via
David Given [Sun, 11 Mar 2018 11:37:23 +0000 (12:37 +0100)]
There are two places where names are added to the global symbol table; one via
the -U command line option, and one via file scanning. Turns out only the
second would increment the number of global names, so adding names with -U
would cause names found via scanning to fall off the end of the list! This
wouldn't cause linker errors because fixups don't use the list, but would cause
the generated symbol table in the output to be incorrect.

6 years agolchar() needs to return the character written. (This happens by magic on 386,
David Given [Sun, 11 Mar 2018 10:00:35 +0000 (11:00 +0100)]
lchar() needs to return the character written. (This happens by magic on 386,
but no on PPC.) See #72.

6 years agoSolve some gcc warnings in ego. kernigh-linuxppc kernigh-pr
George Koehler [Thu, 8 Mar 2018 23:51:07 +0000 (18:51 -0500)]
Solve some gcc warnings in ego.

Some of these are from gcc -Wimplicit

6 years agoRead from new, not old, buffer after realloc.
George Koehler [Thu, 8 Mar 2018 17:04:02 +0000 (12:04 -0500)]
Read from new, not old, buffer after realloc.

This got caught by MALLOC_OPTIONS=S in OpenBSD.  The B compiler filled
the buffer while compiling hilo.b.  Then realloc moved the buffer and
unmapped the old buffer.  The compiler tried to read the old buffer
and segfaulted.

6 years agoFix wr_ranlib() for big-endian machines.
George Koehler [Thu, 8 Mar 2018 16:49:40 +0000 (11:49 -0500)]
Fix wr_ranlib() for big-endian machines.

With this change, I built and ran ack on a big-endian PowerPC Linux
machine.  I used gcc 4.9.4 to build ack, and I only built the linuxppc
back end.

Before this change, wr_ranlib() corrupted a value by changing it from
0x66 to 0x66000066.  This value was too big, so led made a fatal
error, "bad ranlib string offset".

6 years agoAdd instructions like "lwarx". Extend manual.
George Koehler [Wed, 7 Mar 2018 18:37:31 +0000 (13:37 -0500)]
Add instructions like "lwarx".  Extend manual.

Add more page numbers from PowerPC version 2.01.  Remove "xnop" not in
2.01, add "mtcr" from 2.01.  Add "lwarx" and the other instructions
from Book II.  I did not try all the newly added instructions, but
these seem to work: dcbt, dcbtst, icibi, isync, lwarx, stwcx., mftb,
mftbu

In man/powerpc_as.6 (not installed), add a summary of the registers
and addressing modes (like in i386_as.6), describe short forms, update
description of hi16/ha16, add CAVEATS about instructions that some
processors can't run.

6 years agoOptimize procedures that do both a / b and a % b.
George Koehler [Mon, 5 Mar 2018 18:32:06 +0000 (13:32 -0500)]
Optimize procedures that do both a / b and a % b.

Enable this in CS for PowerPC; disable it for all other machines.
PowerPC has no remainder instruction; the back end uses division to
compute remainder.  If CS finds both a / b and a % b, then CS now
rewrites a % b as a - b * (a / b) and computes a / b only once.  This
removes an extra division in the PowerPC code, so it saves both time
and space.

I have not considered whether to enable this optimization for other
machines.  It might be less useful in machines with a remainder
instruction.  Also, if a % b occurs before a / b, the EM code gets a
DUP.  PowerPC ncg handles this DUP well; other back ends might not.

6 years agoCheck AAR earlier to prevent LOI/STI unknown size.
George Koehler [Fri, 2 Mar 2018 21:06:21 +0000 (16:06 -0500)]
Check AAR earlier to prevent LOI/STI unknown size.

In ego, the CS phase may convert a LAR/SAR to AAR LOI/STI so it can
optimize multiple occurrences of AAR of the same array element.  This
conversion should not happen if it would LOI/STI a large or unknown
size.

cs_profit.c okay_lines() checked the size of each occurrence of AAR
except the first.  If the first AAR was the implicit AAR in a LAR/SAR,
then the conversion happened without checking the size.  For unknown
size, this made a bad LOI -1 or STI -1.  Fix by checking the size
earlier: if a LAR/SAR has a bad size, then don't enter it as an AAR.

This Modula-2 code showed the bug.  Given M.def:

    DEFINITION MODULE M;
    TYPE S = SET OF [0..95];
    PROCEDURE F(a: ARRAY OF S; i, j: INTEGER);
    END M.

and M.mod:

    (*$R-*) IMPLEMENTATION MODULE M;
    FROM SYSTEM IMPORT ADDRESS, ADR;
    PROCEDURE G(s: S; p, q: ADDRESS; t: S); BEGIN
      s := s; p := p; q := q; t := t;
    END G;
    PROCEDURE F(a: ARRAY OF S; i, j: INTEGER); BEGIN
      G(a[i + j], ADR(a[i + j]), ADR(a[i + j]), a[i + j])
    END F;
    END M.

then the bug caused an error:

    $ ack -mlinuxppc -O3 -c.e M.mod
    /tmp/Ack_b357d.g, line 57: Argument range error

The bug had put LOI -1 in the code, then em_decode got an error
because -1 is out of range for LOI.

Procedure F has 4 occurrences of `a[i + j]`.  The size of `a[i + j]`
is 96 bits, or 12 bytes, but the EM code hides the size in an array
descriptor, so the size is unknown to CS.  The pragma `(*$R-*)`
disables a range check on `i + j` so CS can work.  EM uses AAR for the
2 `ADR(a[i + j])` and LAR for the other 2 `a[i + j]`.  EM pushes the
arguments to G in reverse order, so the last `a[i + j]` in Modula-2 is
the first LAR in EM.

CS found 4 occurrences of AAR.  The first AAR was an implicit AAR in
LAR.  Because of the bug, CS converted this LAR 4 to AAR 4 LOI -1.

6 years agoMerge pull request #71 from tkchia/default
David Given [Fri, 2 Mar 2018 17:48:11 +0000 (18:48 +0100)]
Merge pull request #71 from tkchia/default

Define appropriate macros when building Lua on Linux or OS X.

6 years agoFixes for compiling ego with -DTRACE
George Koehler [Thu, 1 Mar 2018 18:19:38 +0000 (13:19 -0500)]
Fixes for compiling ego with -DTRACE

 - In share/debug.c, undo my mistake in commit 9037d13 by changing
   vfprintf back to fprintf in OUTTRACE.

 - In ud/ud.c, move the trace output from stdout to stderr, because
   stdout has ego's output file, which becomes opt2's input file.  If
   trace output goes to stdout, it gets prepended to the output file,
   and opt2 errors with "wrong input file".

I also edit both build.lua files so ego depends on its header files;
this part isn't needed for -DTRACE.

One can now use -DTRACE by adding it to the cflags in both build.lua
files.

6 years agoMerge pull request #70 from davidgiven/dtrg-pdp11
David Given [Fri, 23 Feb 2018 21:50:08 +0000 (13:50 -0800)]
Merge pull request #70 from davidgiven/dtrg-pdp11

Make the PDP/11 code generator work.

6 years agoI figured out how to get top working.
David Given [Fri, 23 Feb 2018 21:42:38 +0000 (22:42 +0100)]
I figured out how to get top working.

6 years agoUpdate README.
David Given [Fri, 23 Feb 2018 21:36:03 +0000 (22:36 +0100)]
Update README.

6 years agoMade the cgg and the cg code generator work; use this to beat the PDP/11
David Given [Fri, 23 Feb 2018 21:31:46 +0000 (22:31 +0100)]
Made the cgg and the cg code generator work; use this to beat the PDP/11
backend into shape. It now generates binaries --- no idea whether they work or
not.

6 years agoUse prototypes in ego/cs, ego/sp.
George Koehler [Mon, 5 Feb 2018 21:09:30 +0000 (16:09 -0500)]
Use prototypes in ego/cs, ego/sp.

6 years agoDon't use '-' in option string to getopt().
George Koehler [Mon, 5 Feb 2018 19:55:10 +0000 (14:55 -0500)]
Don't use '-' in option string to getopt().

Using '-' might fail on platforms like FreeBSD.  Commit 50a7031
stopped using '-' in the B compiler and ego.  I now stop using '-' in
mcg, because I can now check that mcg still works.

6 years agoOnly lower "addi sp, sp, X" if X > 0.
George Koehler [Thu, 1 Feb 2018 17:20:31 +0000 (12:20 -0500)]
Only lower "addi sp, sp, X" if X > 0.

If X < 0, then lowering the addi might cause the code to use the stack
space before allocating it.  This is a bug because an asynchronous
signal handler can overwrite the unallocated stack space.

6 years agoTeach mcg to pass our tests.
George Koehler [Tue, 30 Jan 2018 20:53:26 +0000 (15:53 -0500)]
Teach mcg to pass our tests.

Tests pass if one edits the top build.lua to uncomment "qemuppc" from
both vars.plats and vars.plats_with_tests, and one leaves mcg in
plat/qemuppc/descr.

Add or correct some EM instructions in treebuilder.c:
 - "lof", "stf": handle negative offsets in load() and store().
 - "cuu": add using IR_FROMUI.
 - "lim", "sim": keep an entire word in ".ignmask", to be compatible
   with mach/powerpc/libem/trp.s and ncg.  We also keep a word in
   ".ignmask" in ncg for both i386 and m68020.
 - "trp": pass trap number in register.  See comment in
   helper_function_with_arg().
 - "sig": push the old value of .trppc on the stack.
 - "and ?", "ior ?", "xor ?", "com ?", "cms ?", "set ?", "inn ?":
   connect to helper functions in libem.
 - "blm", "bls": drop call to memmove() and use new helper ".bls4",
   because tests/plat/structcopy_e.c can't call memmove().
 - "xor s", "cms s": if s is large, fall back on helper function.
 - "rol", "ror": add by decomposing each rotate into 4 IR ops.
 - "rck s", "bls s": make fatal unless s is word size.
 - "loi": push multiple loads in the correct order.
 - "dup s", "exg s": if s is large, fall back on helper.
 - "dus": add using new helper ".dus4".
 - "lxl", "lxa": follow the static chain, not the dynamic chain.
 - "lor 1": materialise the stack before pushing the stack pointer.
 - "lor 2", "str 2": make fatal.
 - "los", "sts": drop calls to memcpy() and use helpers ".los4" and
   and ".sts4", so lang/m2/libm2/LtoUset.e starts working.
 - "gto": correctly read descriptor.

Change mach/powerpc/mcg/table:
 - ANY.L: add for "asp -8".
 - LOAD.L: work around register corruption.
 - COMPAREUL.I: add for "cms 8".

6 years agoDefine appropriate macros when building Lua on Linux or OS X.
Tee-Kiah Chia [Sun, 28 Jan 2018 16:05:17 +0000 (00:05 +0800)]
Define appropriate macros when building Lua on Linux or OS X.

6 years agoSync qemuppc with linuxppc.
George Koehler [Sun, 28 Jan 2018 02:41:13 +0000 (21:41 -0500)]
Sync qemuppc with linuxppc.

 - Don't reverse bitfields; do use ego (41f3bf7).
 - Use MACHOPT_F=-m2 (3dae9e4).
 - Remove old trap.s (26de4c1).

At this commit, one can build qemuppc with mcg by editing the root
build.lua to uncomment "qemuppc" in "vars.plats".  If one also
uncomments "qemuppc" from "vars.plats_with_tests", then mcg fails to
build the tests.  If one uses ncg (by editing plat/qemuppc/descr to
change "mcg" to "ncg"), then the tests pass.

6 years agoAdd tests for clearing BSS, copying C structs.
George Koehler [Sun, 28 Jan 2018 01:09:16 +0000 (20:09 -0500)]
Add tests for clearing BSS, copying C structs.

The new tests are bss_e.c, structcopy_e.c.  We do clear the BSS before
calling _m_a_i_n, so fix the comments in the other tests.

6 years agoFor osxppc, change size 8 to alignment 4.
George Koehler [Sat, 27 Jan 2018 21:35:48 +0000 (16:35 -0500)]
For osxppc, change size 8 to alignment 4.

You may need to delete and recompile some .o files!  This changes the
alignment of 8-byte values in C structs to match what Apple's gcc
does.  See Apple's "32-bit PowerPC Function Calling Conventions" at

    https://developer.apple.com
      /library/content/documentation/DeveloperTools/Conceptual/LowLevelABI
      /100-32-bit_PowerPC_Function_Calling_Conventions/32bitPowerPC.html

6 years agoUse subfic (val - reg) and mulli (reg * val).
George Koehler [Sat, 27 Jan 2018 20:33:43 +0000 (15:33 -0500)]
Use subfic (val - reg) and mulli (reg * val).

In the instruction list, put /* kills xer */ for sraw, srawi, subfic;
and correct the (now unused) "addi." and "lfdu".

Change MACHOPT_F from -m3 to -m2.  This changes the code for 15 * i
from

    slwi r3,r4,4
    subfic r5,r4,0
    add r3,r3,r5

to

    mulli r3,r4,15

If the sequence "slwi subfic addi" takes 3 cycles and 12 bytes, and
mulli takes 3 cycles and 4 bytes, then mulli is better.

6 years agoGet `ack -mosxppc -g` to partly work with gdb.
George Koehler [Sat, 27 Jan 2018 01:08:03 +0000 (20:08 -0500)]
Get `ack -mosxppc -g` to partly work with gdb.

Copy and adapt code from mach/{i386,m68020}/ncg/mach.c to pass the
debugging stabs from EM to assembly.  The next tools (as, led, cv)
already know how to put the stabs in the Mach-o executable.

Modify the function prolog/prologue so gdb uses fp, not sp, for N_LSYM
and N_PSYM stabs.  Simplify prolog() by reducing differences between
stabs and no stabs, and zero and nonzero framesize.  For files without
stabs, the new prolog has the same number of instructions and memory
accesses as the old prolog, and to run at about the same speed on my
PowerPC Mac.

This is enough to see some info for global and local variables in gdb
for Mac OS X.  I still can't get a backtrace; gdb gets confused
because EM and ncg don't link 0(sp) to the previous stack frame.

I don't expect `ack -mlinuxppc -g` to work with gdb for Linux, because
we prepend underscores to the symbol table, which is correct for
Mach-o but wrong for ELF.

6 years agoAdd some comments before I forget how this stuff works.
George Koehler [Wed, 24 Jan 2018 20:17:32 +0000 (15:17 -0500)]
Add some comments before I forget how this stuff works.

6 years agoAllow sp and fp on the fake stack.
George Koehler [Tue, 23 Jan 2018 23:18:40 +0000 (18:18 -0500)]
Allow sp and fp on the fake stack.

This simplifies parts of the PowerPC table and causes ncg to better
decide whether to push sp or fp to the real stack, or coerce it to
REG3, or coerce it to REG-REG3, or move it to a regvar.  These better
decisions remove extra _mr_ instructions.

The idea comes from mach/powerpc/arm/table, where SP has a property
STACKPOINTER and LB has LOCALBASE.  I don't need two properties, so I
make one property SPFP for both registers.

6 years agoMake osx386 and osxppc use _hol0.s like the other platforms.
George Koehler [Tue, 23 Jan 2018 18:55:39 +0000 (13:55 -0500)]
Make osx386 and osxppc use _hol0.s like the other platforms.

Because I'm lazy, I didn't make another copy of _hol0.s; I am building
plat/linux/libsys/_hol0.s for OS X.

6 years agoAdd fef 4, fif 4. Improve fef 8, fif 8. Other float changes.
George Koehler [Mon, 22 Jan 2018 19:04:15 +0000 (14:04 -0500)]
Add fef 4, fif 4.  Improve fef 8, fif 8.  Other float changes.

When I wrote fef 8, I forgot to test denormalized numbers.  Oops.  Now
fix two of my mistakes:

 - When checking for zero, `extrwi r6, r3, 22, 12` needs to be
   `extrwi r6, r3, 20, 12`.  There are only 20 bits to extract.

 - After the multiplication by 2**64, I forgot to put the fraction in
   [0.5, 1) or (-1, 0.5] by setting IEEE exponent = 1022.

Teach fif 8 about signed zero and NaN.

In ncg/table, change cmf so NaN is not equal to any value, and comment
why ordered comparisons don't work with NaN.  Also add cost for
fctwiz, remove extra `uses REG`.

Edit comment in cfu8.s because the conditional branch might be before
or after fctwiz.

6 years agoUse extended mnemonics and ha16/lo16.
George Koehler [Fri, 12 Jan 2018 01:04:27 +0000 (20:04 -0500)]
Use extended mnemonics and ha16/lo16.

Remove wrong comment: that's a right shift, not a left shift.

6 years agoRewrite sigaction() to prevent another race.
George Koehler [Thu, 11 Jan 2018 22:59:02 +0000 (17:59 -0500)]
Rewrite sigaction() to prevent another race.

A signal handler might call sigaction().  We must block all signals,
not only our signal, to prevent a race between us and the next signal
handler.

Use /* comments */ because cpp might expand macros in ! comments
though such expansion is probably harmless.

The bridge is now shorter by 2 instructions.

6 years agoThese are EM trap numbers.
George Koehler [Tue, 9 Jan 2018 05:39:03 +0000 (00:39 -0500)]
These are EM trap numbers.

Remove .sect; absolute symbols don't go in a section.

6 years agoHide some i386 stuff from linux68k, linuxppc.
George Koehler [Tue, 9 Jan 2018 03:26:24 +0000 (22:26 -0500)]
Hide some i386 stuff from linux68k, linuxppc.

Rename plat/linux/libsys/errno.s to plat/linux386/libsys/trapno.s and
stop building it for linux68k and linuxppc.  It defines symbols for
mach/i386/libem.

In syscalls.h, the numbers after 165 are only for i386, so hide them
from 68k, ppc.  These numbers are unused, because the system calls now
in libsys use the lower numbers.

Also teach the build system that libsys depends on the internal
headers in plat/linux/libsys/*.h

6 years agoShrink .cfu8
George Koehler [Sun, 7 Jan 2018 21:03:55 +0000 (16:03 -0500)]
Shrink .cfu8

With my PowerBook G4, a program that converts values from 1.0 to
4000000.0 runs in about 0.32s with the old .cfu8 and 0.29s with this
shrunken .cfu8

Leave a comment about other ways to implement .cfu8

6 years agoTeach top to merge or delete "addi sp, sp, X".
George Koehler [Fri, 5 Jan 2018 22:55:50 +0000 (17:55 -0500)]
Teach top to merge or delete "addi sp, sp, X".

This reduces code size, because ncg emits too many "addi sp, sp, X"
instructions when unstacking things.  Now top lowers "addi sp, sp, X"
by lifting other instructions.  This sometimes creates chances to
merge or delete _addi_ instructions.  If no such chance is found, the
_addi_ remains uselessly lowered.

Edit ncg/table to remove something that top now does.

Edit ncg/mach.c to remove some spaces after commas.  This removes a
whitespace difference between *.s and *.so files, because top removes
the space.

6 years agoFix lim. Improve lxl, lxa, lor, str, procs with no locals.
George Koehler [Fri, 5 Jan 2018 01:40:35 +0000 (20:40 -0500)]
Fix lim.  Improve lxl, lxa, lor, str, procs with no locals.

_lim_ must use _loe_ (load word external), not _lde_ (load double-word
external).

The new patterns for _lxl_, _lxa_, _lor_, _str_ emit shorter code in
some cases.  The change from GPR_EXPR to REG_EXPR allows moving
LXFRAME to a register variable.

Add more "reusing" clauses.  We have enough registers that ncg almost
never reuses a register, but sometimes it can reuse r3.

In mach.c, emit one fewer instruction in procedures with no locals.

6 years agoAdd tests for C <setjmp.h> and Modula-2 Semaphores.
George Koehler [Wed, 3 Jan 2018 19:51:14 +0000 (14:51 -0500)]
Add tests for C <setjmp.h> and Modula-2 Semaphores.

Fix PowerPC ncg so setjmp() returns the correct value.  I got unlucky
when ncg picked r3 for "uses REG"; this destroyed the return value in
r3 and caused the new test to fail.

6 years agoAdd test for EM _rck_. Fix traps in PowerPC ncg.
George Koehler [Mon, 25 Dec 2017 03:37:52 +0000 (22:37 -0500)]
Add test for EM _rck_.  Fix traps in PowerPC ncg.

The new test rck_e.e segfaults on PowerPC unless I make some changes.
The inline code for _rck_ was wrong because it didn't allow the trap
handler to return.  _sig_ forgot to push the old trap handler.

Move plat/linuxppc/libsys/trap.s to mach/powerpc/libem/trp.s and
rewrite it with simplified/extended mnemonics.  Remove .trap alias for
.trp procedure.  Add a missing `mtspr lr, r0` so we can return from
the trap handler.  Call write() and _exit() so trp.s works with both
linuxppc and osxppc.  Before, Mac OS X was wrongly using the trap.s
for Linux.

In powerpc/libem, simplify .aar4; teach .csa and .csb to raise the
trap if the default target is zero.

C programs don't need these changes.  You may relink your C programs
with the changed .csa and .csb, but C code doesn't raise the trap.
Modula-2 code can raise traps, so you may want to relink your Modula-2
programs with the changed libem, but you might keep your old .o files
from Modula-2.  You may need to recompile your Pascal programs (delete
old .o files from Pascal) because the Pascal compiler might use _rck_.

6 years agoOptimize `mr. X, X` after some instructions.
George Koehler [Sat, 23 Dec 2017 03:32:16 +0000 (22:32 -0500)]
Optimize `mr. X, X` after some instructions.

For example, when ncg emits
    slw r9,r8,r5
    mr. r9,r9
then top simplifies the code to
    slw. r9,r8,r5

6 years agoRemove INT32 and such. Adjust indentation.
George Koehler [Sat, 23 Dec 2017 02:18:58 +0000 (21:18 -0500)]
Remove INT32 and such.  Adjust indentation.

I understand `loi 4` more easily than `loi INT32`, because `loi 4`
appears in .e files.  So remove INT8, INT16, INT32, INT64.

Add a comment to explain r3 during unconditional jumps.

6 years agoGenerate shorter code for ret 4 and ret 8.
George Koehler [Sat, 23 Dec 2017 01:37:39 +0000 (20:37 -0500)]
Generate shorter code for ret 4 and ret 8.

6 years agoRemove two obsolete patterns.
George Koehler [Sat, 23 Dec 2017 00:57:42 +0000 (19:57 -0500)]
Remove two obsolete patterns.

These patterns seem to have no effect on the generated code.

6 years agoAdd FRAME_V tokens for local variables.
George Koehler [Fri, 22 Dec 2017 22:04:16 +0000 (17:04 -0500)]
Add FRAME_V tokens for local variables.

When storing to a local, stop killing the tokens of other locals,
unless they might overlap with the stored local.  This helps some
procedures that juggle locals when the locals aren't in registers.

Also use FRAME_V tokens for locals in statically enclosing procedures.
Rewrite _lxa_ as _lxl_, to skip the `addi ?,?,8` if we can add 8 to
the next constant.  The PowerPC code from _lxl_ is now sometimes
better, sometimes worse than before.

The i386 table provided the idea to use %size to find overlapping
locals.

6 years agoRevise the comments in the EM tests.
George Koehler [Thu, 21 Dec 2017 23:19:26 +0000 (18:19 -0500)]
Revise the comments in the EM tests.

You can cheat these tests if _cms_ and _cmu_ always push zero.

6 years agoAdd some tests for Modula-2.
George Koehler [Thu, 21 Dec 2017 22:44:03 +0000 (17:44 -0500)]
Add some tests for Modula-2.

6 years agoPass 4 bytes to fail(uint32_t)
George Koehler [Tue, 19 Dec 2017 02:58:57 +0000 (21:58 -0500)]
Pass 4 bytes to fail(uint32_t)

This would become necessary if something failed on a platform with
16-bit int (EM_WSIZE == 2).

Remove unreachable `ret 0`.  If reached, it wouldn't work to return
from _m_a_i_n.

6 years agoSimplify code by using cms EM_WSIZE to compare bytes.
George Koehler [Tue, 19 Dec 2017 02:52:13 +0000 (21:52 -0500)]
Simplify code by using cms EM_WSIZE to compare bytes.

This should work because the C compiler does it.

6 years agoRemove '\0' from output. Fix a compiler warning.
George Koehler [Tue, 19 Dec 2017 02:17:42 +0000 (21:17 -0500)]
Remove '\0' from output.  Fix a compiler warning.

Don't output '\0' in "@@FINISHED\0".

Cast code to unsigned int.  This helps platforms with 16-bit int, by
doing only the low 16 bits of the bitwise-and.  It also removes the
"(warning) conversion of long to pointer loses accuracy".

6 years agoAdd splitting coercions for IND_ALL_D.
George Koehler [Tue, 19 Dec 2017 01:59:04 +0000 (20:59 -0500)]
Add splitting coercions for IND_ALL_D.

Delete my wrong comment (from commits cfbc537a8f62f45432bd0) which
claimed that such coercions are not possible.

6 years agoEnable conditional expressions in splitting coercions.
George Koehler [Tue, 19 Dec 2017 01:39:56 +0000 (20:39 -0500)]
Enable conditional expressions in splitting coercions.

ncgg has parsed the optional conditional expression (optexpr) of each
splitting coercion since commit 72b83cc in 1985; but for almost 33
years, ncg has ignored the expression in c2_expr.

Few tables had conditional coercions (I only found them in arm and
m68020), and no tables had conditional splitting coercions, so this
only becomes a problem now as I try to add a conditional splitting
coercion to powerpc.

6 years agoRename two tokens. CONST_HZ was not hertz (Hz).
George Koehler [Mon, 18 Dec 2017 17:36:10 +0000 (12:36 -0500)]
Rename two tokens.  CONST_HZ was not hertz (Hz).

6 years agoIn coercions, try to reuse a register with the same token.
George Koehler [Sun, 17 Dec 2017 17:45:27 +0000 (12:45 -0500)]
In coercions, try to reuse a register with the same token.

This reduces code size.

6 years agoRename ANY_BHW to INT_W; add FLOAT_W, FLOAT_D.
George Koehler [Thu, 14 Dec 2017 21:26:19 +0000 (16:26 -0500)]
Rename ANY_BHW to INT_W; add FLOAT_W, FLOAT_D.

INT_W, the integer set, continues to exclude FSREG, because we can't
easily move FSREG to GPR.

ANY4 becomes ISET+FLOAT_W and ANY8 becomes FLOAT_D.

6 years agoDelete rules for sti 8 with REG IND_RC_D, with REG IND_RR_D.
George Koehler [Tue, 12 Dec 2017 18:36:43 +0000 (13:36 -0500)]
Delete rules for sti 8 with REG IND_RC_D, with REG IND_RR_D.

Prefer the rule with REG FREG, by coercing IND_RC_D or IND_RR_D to
FREG.  This rule looks better to ncg.  When ncg chose between coercion
to REG IND_RC_D or coercion to REG FREG, it chose REG FREG.  It only
chose REG IND_RC_D if the stack had exact REG IND_RC_D.

6 years agoThese instructions write to the CR.
George Koehler [Sun, 10 Dec 2017 19:01:14 +0000 (14:01 -0500)]
These instructions write to the CR.

6 years agoRevise stack shuffles and integer conversions in PowerPC ncg.
George Koehler [Sat, 9 Dec 2017 22:21:06 +0000 (17:21 -0500)]
Revise stack shuffles and integer conversions in PowerPC ncg.

Allow asp 4, exg 4 to shuffle tokens without coercing them into
registers; but comment why dup 4, dup 8 coerce tokens into registers.

Allow dup, dus, exg with larger sizes; and add tests dup_e.e and
exg_e.e to check that dup 20, dus, exg 20 work as well in powerpc as
in i80 and i86.

Then powerpc failed to compile loc 2 loc 4 cuu in dup_e.e.  Revise the
integer conversions, so powerpc can compile and pass the test.

6 years agoAdd more chances to put results in register variables.
George Koehler [Fri, 8 Dec 2017 22:19:26 +0000 (17:19 -0500)]
Add more chances to put results in register variables.

When a rule `uses REG ... yields %a`, the result %a is always a
temporary, never a regvar.  If the EM code uses _stl_ to put the
result in a regvar, then ncg emits _mr_ to move %a to the regvar.

There are two ways to put the result in the regvar without %a:

  1. Yield a token, as in `yields {MUL_RR, %2, %1}`, so that _stl_
     can move the token to the regvar without using %a.

  2. Provide a pattern, like `sli stl`, that just puts the result
     in `{LOCAL, $2}` and not %a.

Allow some tokens, like SUM_RIS and XEQ, onto the stack; and add
tokens like MUL_RR, and patterns like `sli stl`.

Delete patterns for `stl lol` and `sdl ldl` to avoid an extra
temporary %a when the local is a regvar.  Delete `lal sti lal loi`
because it would emit wrong code.

6 years agoSplit C from CONST.
George Koehler [Fri, 8 Dec 2017 00:24:09 +0000 (19:24 -0500)]
Split C from CONST.

Rename token CONST to C.  Define set CONST = C + CONST_STACK.  The
instructions with CONST operands can now accept CONST_STACK tokens;
some cases of {CONST, %1.val} become %1.

Also simplify two of _rlwinm_ into _slwi_ and _srwi_.

6 years agoAdd test for EM rol, ror. Fix i80, i86, powerpc.
George Koehler [Thu, 7 Dec 2017 22:16:21 +0000 (17:16 -0500)]
Add test for EM rol, ror.  Fix i80, i86, powerpc.

EM instructions _rol_ and _ror_ do rotate an integer left or right.
Our compilers and optimizers never emit _rol_ nor _ror_, but I might
want to use them in the future.

Add _rol_ and _ror_ to powerpc.  Fix `rol 4` and `ror 4` in both i80
and i86, where the rules for `rol 4` and `ror 4` seem to have never
been tested until now.

6 years agoCorrect the stack pointer when i80 shrinks an integer.
George Koehler [Thu, 7 Dec 2017 20:39:41 +0000 (15:39 -0500)]
Correct the stack pointer when i80 shrinks an integer.

The code used `sphl` to set the stack pointer, but the correct value
was in de, not hl.  Fix by swapping the values of de and hl, so `sphl`
is now correct.  When we shrink an integer from 4 to 2 bytes, both
registers de and hl point to copies of the result, but only one
register preserves the stack below the result.

This fixes writehex() in tests/plat/lib/test.c, when I compile it with
ack -mcpm, so it preserves the pointer to "0123456789abcdef", so it
writes hexadecimal digits and not garbage.

This bug goes back to commit 157b243 of Mar 18, 1985, so the bug is
32 years old, and probably the oldest bug that I ever fixed.

6 years agoKill registers a, de, when i80 ncg does Call libem.
George Koehler [Thu, 7 Dec 2017 03:14:00 +0000 (22:14 -0500)]
Kill registers a, de, when i80 ncg does Call libem.

I compiled tests/plat/lib/test.c with ack -mcpm, but i80 ncg did emit
wrong code in writehex(uint32_t) for

    "0123456789abcdef"[code & 0xf]

The code called '.and' to evaluate `code & 0xf`, then tried to call
'.cii' to narrow the result from 4 to 2 bytes, but it passed garbage
instead of 4 to '.cii'.  The rule for '.and' was

    pat and defined($1)
    kills ALL
    uses dereg={const2,$1}
    gen Call {label,".and"}

This failed to kill register de={const2,4}, so ncg pushed de,
expecting to push 4, but actually pushing garbage.

Fix such rules using `mvi a,...` or `lxi de,...` so ncg doesn't track
the token in the register.  This is like the i86 table.  A different
fix would use a dummy instruction `killreg a` or `killreg de` like the
m68020 table.

Also correct 1 to $1 when calling '.exg'.

6 years agoUse <stdarg.h> in util/misc/convert.c
George Koehler [Wed, 6 Dec 2017 22:09:12 +0000 (17:09 -0500)]
Use <stdarg.h> in util/misc/convert.c

I made a syntax error in some .e file, and em_encode dumped core
because a 64-bit pointer didn't fit in a 32-bit int.  Now use stdarg
to pass pointers to error() and fatal().

Stop using the number of errors as the exit status.  Many systems use
only the low 8 bits of the exit status, so 256 errors would become 0.

Also change modules/src/print to accept const char *buf

6 years agoMerge pull request #69 from kernigh/kernigh-stdc
David Given [Sun, 19 Nov 2017 11:00:40 +0000 (12:00 +0100)]
Merge pull request #69 from kernigh/kernigh-stdc

use libc assert, strcmp; declare more functions; fewer clang warnings

6 years agoMerge pull request #66 from davidgiven/dtrg-warnings
David Given [Sun, 19 Nov 2017 10:39:45 +0000 (11:39 +0100)]
Merge pull request #66 from davidgiven/dtrg-warnings

lang/basic/lib: fewer warnings

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 agoLLgen won't update the output file timestamp if the file contents doesn't
David Given [Wed, 15 Nov 2017 18:41:39 +0000 (19:41 +0100)]
LLgen won't update the output file timestamp if the file contents doesn't
change, which confuses ninja no end. Fix this.

Fixes: #68

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.