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.
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.
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.
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.
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_.
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.
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.
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'.
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.
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.
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.
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.
*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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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().