raz / commit 6d4487fb

commit
6d4487fbe2e3e0c6213e719b1f1d28e0880fafa6
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
2b92b442
Gen.arm64: division and the checks, calls in full, try and defer, atomics, COPY

PLAN.md 5, step 5b. Division and mod check zero and the smallest value
by -1, as arm64 doesn't trap; every check Gen.x86_64 makes reaches
Rt.fail by stubs that pass the place. Arguments past x7, results
through x8, Apple's variadic C calls, try, deferred statements, return
f(x) of several results, jump tables, compareSeq; and the atomics and
COPY, which Lock and Sys need. Rt.darwin-arm64 tells a failed check
and a fault as Linux does. mac-check runs conform/run too: 91 of 111
pass on the Mac, the rest waiting on reals, FILL, CLZ and ROL (5c).
x86-64's output is unchanged for 202 programs; the fixpoint holds.

patch · log

15 files changed, +1118 -166

PLAN.md (history) modified +17 -6

@@ -247,12 +247,23 @@
247247 exit, one procedure at exit, fail and stop by `write`. As the interface
248248 check of `Rt` reads `Rt.raz`, and so compiles `Sys` over C, also
249249 `lib/CAbi.darwin-arm64.raz`, from `tools/cabi.c` on the Mac, and a
250- code procedure skipped in a file read only for its interface. Left of
251- step 5: division and its check, the other checks and their stubs for
252- `Rt.fail`, the reals, arguments and results past the registers or in
253- memory (`x8`), Apple's variadic calls, `try`, deferred statements,
254- atomics and system calls, `COPY`, `FILL`, `NEW`'s zeroing, the bit
255- counts and rotations, copies past 256 bytes.
250+ code procedure skipped in a file read only for its interface.
251+
252+ 5b, done: division and its checks (by zero, the smallest by -1, in
253+ every width), the checks of indexes, slices, views and enumerations
254+ with their stubs for `Rt.fail`, arguments past x7 on the stack,
255+ results in memory through x8, Apple's variadic calls, `try`, deferred
256+ statements, jump tables, `compareSeq`, copies past 256 bytes, `ABS`
257+ of an integer; and, since `Out` needs them, the atomics and `COPY`.
258+ `SYSTEM.SYSCALL` is an error on the Mac, Linux's only (report 6.8).
259+ `lib/Rt.darwin-arm64.raz` says what failed with its values, by its
260+ address, runs 32 procedures at exit, and tells a nil pointer or
261+ procedure and a stack overflow by a handler of SIGSEGV and SIGBUS.
262+ `make mac-check` runs conform/run too and counts it: 91 of 111 pass,
263+ the others needing reals (13), `FILL` (4, `Heap`'s among them),
264+ `CLZ`, `ROL`, or `SYSCALL` (`SystemImport`, Linux's only). Left of
265+ step 5 (5c): the reals, `FILL`, `NEW`'s zeroing, the counts of bits
266+ and the rotations.
256267 6. `Exe.darwin.raz`'s line numbers for lldb (its rebases came with
257268 5a); `Host.darwin.raz`.
258269 7. The library: `CAbi`, `Sys`, `Rt`, `Thread`, `Bytes`, `Math`, `Str`.

doc/compiler.md (history) modified +57 -13

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is fifteen files of raz, about 16,100 lines of code (the
3+The compiler is fifteen files of raz, about 16,400 lines of code (the
44 budget is 18,000); three of them, `A64.raz`, `Gen.arm64.raz` and
55 `Exe.darwin.raz`, are the Mac's back end, begun (`PLAN.md` 5), built
66 only into a compiler for `darwin-arm64` (`src/cross.sh`). It is its own compiler: a release of it, the one
@@ -25,7 +25,7 @@
2525 | `src/X64.raz` | 1,771 | 1,450 | The instruction encoder, jumps within a procedure, the assembler |
2626 | `src/A64.raz` | 1,843 | 1,365 | The same for arm64: the encoder, jumps, the assembler |
2727 | `src/Gen.x86_64.raz` | 2,861 | 1,999 | The stack machine, for x86-64: values, operations, checks, calls, frames |
28-| `src/Gen.arm64.raz` | 1,468 | 1,107 | The same for arm64, begun: frames, values, operations, jumps, calls |
28+| `src/Gen.arm64.raz` | 1,993 | 1,398 | The same for arm64, begun: frames, values, operations, checks, calls |
2929 | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls |
3030 | `src/Parse.raz` | 4,948 | 4,198 | Declarations, statements, expressions, built-ins, recovery, warnings |
3131 | `src/Link.raz` | 481 | 365 | Tree shaking, layout, patching, the places |
@@ -1716,10 +1716,13 @@
17161716 answers every call `Gen.x86_64.raz` answers, with the same parameters,
17171717 as the interface check holds the two files to (report, 3), and makes
17181718 frames, constants, the values of variables and their stores, the
1719-integer operations but division, comparisons and jumps, elements and
1720-slices unchecked, calls with integer arguments in registers, C's
1721-calls, code procedures (`A64.assemble`) and the entry point. `Gen.Arch`
1722-is `"arm64"`.
1719+integer operations, division with its checks, comparisons and jumps,
1720+jump tables, elements and slices and their checks, calls with integer
1721+arguments in registers and on the stack, results in memory, C's calls
1722+and Apple's variadic ones, `try`, deferred statements, the atomics,
1723+`COPY`, code procedures (`A64.assemble`) and the entry point; not yet
1724+the reals, `FILL`, `NEW`'s zeroing, the counts of bits and the
1725+rotations (step 5c). `Gen.Arch` is `"arm64"`.
17231726
17241727 *Not yet.* What it can't make yet isn't refused at once, since a
17251728 library module holds much that a program never calls: the C library's
@@ -1737,6 +1740,8 @@
17371740 doesn't compiles. `beginProc` comes before its chunk is begun, so its
17381741 notes wait for `enterBody` (`pending`). A routine not yet made reads
17391742 into `_` the parameters it doesn't use yet, so that none is warned of.
1743+`SYSTEM.SYSCALL`, which the report gives only on Linux (6.8), is noted
1744+the same way, with a message of its own, and stays so.
17401745
17411746 *The stack* is x86-64's, kept as plainly as it can be: the value at
17421747 depth d is in `Regs[d mod 6]`, x9 to x14, which a call may change and
@@ -1785,13 +1790,51 @@
17851790 numbered from `XmmVar`, 32, above every x register, which `Parse` tells
17861791 apart by it).
17871792
1788-*Calls* move each argument into x0 to x7; a single result comes back in
1789-x0, two words in x0 and x1, and a narrow result of C's is extended, as
1790-C leaves the upper bits as they fall. C's procedure is called through
1791-the quadword the loader fills in, `adrp x16` of its page, `ldr x17,
1792-[x16, ...]` and `blr x17`; a procedure value from its slot, `blr x17`
1793-too.
1793+*Calls* move each argument into x0 to x7, a slice's two words into two
1794+of them or both onto the stack. The arguments past the registers go on
1795+the machine's stack, 8 bytes each, the first at sp, in an area that sp
1796+is lowered by for the call alone and that keeps it a multiple of 16;
1797+the callee finds them above its frame record, at x29 + 16 on, as x86's
1798+finds them above rbp. A C procedure that ends in `...` takes what comes
1799+past its own parameters on the stack, registers free or not, as
1800+Apple's convention has it (C's own parameters past x7 narrower than 8
1801+bytes, which Apple packs, raz doesn't make). A single result comes back
1802+in x0, two words in x0 and x1, and a narrow result of C's is extended,
1803+as C leaves the upper bits as they fall; a result in memory, a record
1804+or several results, goes to the caller's temporary, whose address the
1805+caller puts in x8 and the callee keeps in a slot (`retSlot`) for
1806+`resultTo`, `copyResult` and `try`'s `returnError`. C's procedure is
1807+called through the quadword the loader fills in, `adrp x16` of its
1808+page, `ldr x17, [x16, ...]` and `blr x17`; a procedure value from its
1809+slot, `blr x17` too. A deferred statement is a subroutine of its
1810+procedure, called by `bl`, as on x86-64; since its own calls would
1811+change x30, it keeps x30 on the machine's stack, 16 bytes, for its
1812+`ret`.
17941813
1814+*Checks* are x86-64's (`check`, `endFails`): a branch, not taken when
1815+the check holds, to code at the procedure's end that puts `Rt.fail`'s
1816+values in x1, x2 (a slice's b), x3 (the length) and x5 (an
1817+enumeration's name) and calls a stub of its kind, `check0` to `check7`,
1818+which `entryPoint` makes: `sub x4, x30, #4`, the place of the check's
1819+`bl`; the kind in x0; x6 and x7, the registers x86's trap report shows,
1820+zero; and `bl Rt.fail`. A slice in memory has its length loaded into x3
1821+first, where the failing code finds it. Two things x86 catches and
1822+arm64 doesn't are checked as they are made: a division by zero, and
1823+the smallest value of a signed type divided by -1, which `sdiv` takes
1824+without a fault (`divide`), both stopping the program as `Rt.fail`'s
1825+kind 5, a division fault, as SIGFPE does on Linux. What the Mac does
1826+catch, `Rt`'s handler tells: a nil pointer within page zero, 4 GB
1827+(`NilPage`), a call to nil, and a stack overflow at the guard page,
1828+each a SIGSEGV or SIGBUS, on a stack of its own (`lib/Rt.darwin-arm64.raz`).
1829+
1830+The rest is x86's, in arm64's instructions, each shown in its
1831+routine's comment: `copyBytes` and `zeroBytes` past 256 bytes by a loop
1832+of words; `compareSeq` a byte at a time; `jumpTable`'s table of words
1833+after a `br`, found by `adr`; the atomics by the LSE instructions,
1834+`casal`, `swpal` and `ldaddal`; `COPY` (`copySlice`) by words, then
1835+bytes, back from the ends when the destination starts inside the
1836+source; `ABS` by `cneg`.
1837+
17951838 *The entry point* (`entryPoint`) is the C `main` that dyld calls: it
17961839 pushes a frame record, which links to dyld's, saves argc, an `int` it
17971840 extends to 64 bits, and argv, gives `Rt` C's `exit` in `cExit` and the
@@ -2570,7 +2613,8 @@
25702613 compiler for the Mac this way, in the linux/amd64 container, and `make
25712614 mac-check` (`test/mac.sh`), on an Apple Silicon Mac, has it compile
25722615 `test/mac`'s programs there and runs each here, natively, against its
2573-`X.expect` and `X.status`. The host changes nothing of the target:
2616+`X.expect` and `X.status`; then conform/run's, which are counted, not
2617+failures yet, those that fail listed in `build/mac/conform.fail`. The host changes nothing of the target:
25742618 a program for another system still needs that system's `Sys` and `Rt`,
25752619 and for a kernel the options `-bare`, `-s` and `-base`.
25762620

lib/Rt.darwin-arm64.raz (history) modified +171 -27

@@ -1,53 +1,76 @@
1-(* The run time, for macOS on arm64: for now the least a program needs
2- to end, with lib/Rt.raz's interface, which every file of Rt keeps.
3- The compiler finds exit, fail and cExit by these names. exit runs
4- the procedure atExit registered, then ends the program by C's exit,
5- which the entry point gives cExit, as every program on a Mac reaches
6- the kernel through libSystem; fail and stop say what failed and end
7- it with status 2. It leaves out what lib/Rt.raz does on Linux, which
8- PLAN.md 5's step 7 brings here: more than one procedure at exit, the
9- places a failure is told by, the signals' handlers, the trace of
10- calls and the profiler; place, trace and startProfile do nothing. *)
1+(* The run time, for macOS on arm64, with lib/Rt.raz's interface, which
2+ every file of Rt keeps. The compiler finds exit, fail and cExit by
3+ these names. exit runs the procedures atExit registered, then ends
4+ the program by C's exit, which the entry point gives cExit, as every
5+ program on a Mac reaches the kernel through libSystem; fail says
6+ what failed, with its values, and stop its message, and they end it
7+ with status 2; and a fault the machine catches, a nil pointer or
8+ procedure or a stack overflow, is told by a handler, on a stack of
9+ its own, as on Linux, and ends it so too. A failure is told by its
10+ address, as on Linux in a program built with raz -s: the places, the
11+ calls under way and the profiler are what PLAN.md 5's step 7 brings
12+ here; place, trace and startProfile do nothing yet. *)
1113 module Rt;
1214 import SYSTEM;
1315
1416 const
1517 Full*: ERROR = "too many procedures to run at exit"; (* atExit's *)
16- Failed = "raz: a check failed\n";
18+ MaxAtExit = 32;
19+ Digits = "0123456789abcdef";
20+ (* the signals a fault raises, and sigaction's flags SA_ONSTACK,
21+ SA_RESETHAND and SA_SIGINFO, as macOS's <signal.h> has them *)
22+ SigIll = 4; SigTrap = 5; SigFpe = 8; SigBus = 10; SigSegv = 11;
23+ Flags = 1H + 4H + 40H;
24+ Low = 100000000H; (* page zero: no address below this is ever mapped *)
1725
26+type
27+ (* What sigaction and sigaltstack take, as macOS lays them out *)
28+ Action = record handler: proc(sig: I32; info, ctx: ^U8); mask, flags: I32; end;
29+ Stack = record sp: ^U8; size: INT; flags: I32; end;
30+
1831 extern proc write(fd: I32; buf: ^U8; n: UINT): INT;
32+extern proc sigaction(sig: I32; act, old: ^U8): I32;
33+extern proc sigaltstack(ss, old: ^U8): I32;
1934
2035 var
21- atExitProc: proc; (* the procedure atExit registered, or nil *)
36+ atExits: [MaxAtExit]proc;
37+ numAtExit: INT;
2238 ending: INT; (* the status exit was given, once it has been *)
2339 cExit: proc(status: I32); (* C's exit, set first thing *)
2440 placer: proc(a: UINT); (* place, set first thing, or nil under raz -s *)
2541 profiler: proc; (* startProfile, set first thing, or nil *)
42+ line: [256]U8; (* the message being made *)
43+ lineLen: INT;
44+ act: Action;
45+ alt: Stack;
46+ altStack: [65536]U8; (* where the handler runs, even when the stack is full *)
2647
27-(* Registers p to run when the program ends by exit: one, for now *)
48+(* Registers p to run when the program ends by exit *)
2849 proc atExit*(p: proc): ERROR;
2950 begin
30- if atExitProc # nil then return Full; end;
31- atExitProc := p;
51+ if numAtExit = MaxAtExit then return Full; end;
52+ atExits[numAtExit] := p;
53+ INC(numAtExit);
3254 return nil;
3355 end;
3456
35-(* The status exit was given, while the procedure atExit registered
36- runs; 0 before *)
57+(* The status exit was given, while the procedures atExit registered
58+ run; 0 before *)
3759 proc exitStatus*: INT;
3860 begin
3961 return ending;
4062 end;
4163
42-(* Runs the procedure atExit registered, taken off first so that it
43- runs once, then ends the program with status *)
64+(* Runs the procedures registered with atExit, the last first, each
65+ taken off first so that it runs once, then ends the program with
66+ status *)
4467 proc exit*(status: INT);
4568 var p: proc;
4669 begin
4770 ending := status;
48- if atExitProc # nil then
49- p := atExitProc;
50- atExitProc := nil;
71+ while numAtExit > 0 do
72+ DEC(numAtExit);
73+ p := atExits[numAtExit];
5174 p();
5275 end;
5376 cExit(I32(status));
@@ -62,6 +85,72 @@
6285 exit(2);
6386 end;
6487
88+(* Messages, made in line, as lib/Rt.raz makes them, one byte kept for
89+ the line feed, and written by say in one piece *)
90+proc put(s: []const U8);
91+var i: INT;
92+begin
93+ for i := 0 to LEN(s) - 1 do
94+ if lineLen < LEN(line) - 1 then
95+ line[lineLen] := s[i];
96+ INC(lineLen);
97+ end;
98+ end;
99+end;
100+
101+(* The digits from the last, a negative n's from its negative
102+ remainders, so that the lowest INT comes out too *)
103+proc putInt(n: INT);
104+var buf: [20]U8; i: INT; neg: BOOL;
105+begin
106+ neg := n < 0;
107+ i := 20;
108+ repeat
109+ DEC(i);
110+ if neg then buf[i] := U8(-(n mod 10)) + '0'; else buf[i] := U8(n mod 10) + '0'; end;
111+ n := n div 10;
112+ until n = 0;
113+ if neg then put("-"); end;
114+ put(buf[i..20]);
115+end;
116+
117+proc putHex(u: UINT);
118+var buf: [16]U8; i: INT;
119+begin
120+ i := 16;
121+ repeat
122+ DEC(i);
123+ buf[i] := Digits[INT(u & 15)];
124+ u := u shr 4;
125+ until u = 0;
126+ put("0x");
127+ put(buf[i..16]);
128+end;
129+
130+proc putStr(s: ^U8);
131+var n: INT;
132+begin
133+ n := 0;
134+ while s[n] # 0 do INC(n); end;
135+ put(s[0..n]);
136+end;
137+
138+proc say;
139+begin
140+ line[lineLen] := '\n';
141+ _ := write(2, SYSTEM.PTR(line), UINT(lineLen + 1));
142+ lineLen := 0;
143+end;
144+
145+(* Puts where the code at address a is: its address, for now *)
146+proc where(a: UINT);
147+begin
148+ if placer # nil then placer(a); end;
149+ put("at ");
150+ putHex(a);
151+ put(": ");
152+end;
153+
65154 (* Where the code at address a comes from: not told yet *)
66155 proc place*(a: UINT);
67156 begin
@@ -73,13 +162,60 @@
73162 begin
74163 end;
75164
76-(* A check failed: the compiler's code calls this with what failed,
77- its values and where, none of which it tells yet *)
165+(* A check failed in the code at address at: says where, and what
166+ failed, as lib/Rt.raz's fail, whose comment says what each kind is,
167+ then ends the program by exit with status 2. The registers that
168+ x86-64's trap report shows are none here. *)
78169 proc fail*(what, a, b, len: INT; at: UINT; name: ^U8; rdi, rsi: UINT);
79170 begin
80- _ := what; _ := a; _ := b; _ := len; _ := name; _ := rdi; _ := rsi;
81- if placer # nil then placer(at); end;
82- _ := write(2, SYSTEM.PTR(Failed), LEN(Failed));
171+ _ := rdi; _ := rsi;
172+ where(at);
173+ if what = 4 then put("stopped by a nil pointer");
174+ elsif what = 5 then put("stopped by a division fault");
175+ elsif what = 6 then put("stopped by a stack overflow");
176+ elsif what = 0 then put("index "); putInt(a);
177+ elsif what = 7 then
178+ put("a view at index "); putInt(a);
179+ put(" runs past a length of "); putInt(len);
180+ elsif (what = 1) or (what = 3) then
181+ put("slice "); putInt(a); put(".."); putInt(b);
182+ if what = 3 then put(" runs backwards"); end;
183+ else
184+ putInt(a); put(" isn't a position of "); putStr(name);
185+ end;
186+ if what < 2 then put(" is out of range for a length of "); putInt(len); end;
187+ say;
188+ exit(2);
189+end;
190+
191+(* A fault stops the program: says where it was and why on standard
192+ error, then ends it by exit with status 2, as a failed check does.
193+ info and ctx are the kernel's siginfo and ucontext: the address that
194+ faulted is at 24 in siginfo, and the registers at uc_mcontext, a
195+ pointer at 48 in ucontext, past 16 bytes of the exception's state:
196+ x0 to x28, then fp, lr, sp and pc. *)
197+proc onSignal(sig: I32; info, ctx: ^U8);
198+var adr, sp, pc: UINT; q: ^UINT; why: []const U8;
199+begin
200+ q := ^UINT(info + 24);
201+ adr := q^;
202+ q := ^UINT(ctx + 48);
203+ q := ^UINT(q^ + 16);
204+ sp := q[31];
205+ pc := q[32];
206+ if sig = SigFpe then why := "a division fault";
207+ elsif pc < Low then
208+ why := "a nil procedure";
209+ pc := q[30] - 4; (* the call, by its return address in lr *)
210+ elsif adr < Low then why := "a nil pointer";
211+ elsif adr - sp + 100000H < 200000H then why := "a stack overflow"; (* within a megabyte of sp *)
212+ elsif (sig = SigIll) or (sig = SigTrap) then why := "a trap";
213+ else why := "a bad address";
214+ end;
215+ where(pc);
216+ put("stopped by ");
217+ put(why);
218+ say;
83219 exit(2);
84220 end;
85221
@@ -90,4 +226,12 @@
90226
91227 begin
92228 _ := profiler; (* started here when RAZ_PROFILE is set, by step 7's Rt *)
229+ alt := {sp: SYSTEM.ADR(altStack[0]), size: LEN(altStack)};
230+ _ := sigaltstack(^U8(SYSTEM.ADR(alt)), nil);
231+ act := {handler: onSignal, flags: Flags};
232+ _ := sigaction(SigSegv, ^U8(SYSTEM.ADR(act)), nil);
233+ _ := sigaction(SigBus, ^U8(SYSTEM.ADR(act)), nil);
234+ _ := sigaction(SigFpe, ^U8(SYSTEM.ADR(act)), nil);
235+ _ := sigaction(SigIll, ^U8(SYSTEM.ADR(act)), nil);
236+ _ := sigaction(SigTrap, ^U8(SYSTEM.ADR(act)), nil);
93237 end Rt.

src/Gen.arm64.raz (history) modified +645 -120

@@ -4,13 +4,14 @@
44 The same stack machine as x86-64's, with the same calls, which Item
55 and Parse make: they push constants, addresses and the values of
66 variables, operate on the values on top, store them and jump on
7- them. This one is begun: what it makes so far is frames, constants,
8- the values of variables and their stores, the integer operations but
9- division, comparisons and jumps, elements and slices unchecked,
10- calls of procedures with integer arguments in registers, C's through
11- the quadword the loader fills in, code procedures and the program's
12- entry point. What else is
13- asked is noted as "not yet on arm64" (notYet), and a program that
7+ them. What it makes so far is frames, constants, the values of
8+ variables and their stores, the integer operations, comparisons and
9+ jumps, jump tables, elements and slices and their checks, calls with
10+ integer arguments in registers and on the stack, C's through the
11+ quadword the loader fills in, results in memory, try, deferred
12+ statements, the atomics, COPY, code procedures and the program's
13+ entry point. What else
14+ is asked is noted as "not yet on arm64" (notYet), and a program that
1415 uses it fails to compile rather than run wrong; PLAN.md 5 says the
1516 pieces still to come.
1617
@@ -33,17 +34,20 @@
3334 pointer, and x30, the return address.
3435
3536 Calls use Apple's arm64 convention: the first eight integer arguments
36- in x0 to x7, results in x0 and x1, sp a multiple of 16 always, a
37+ in x0 to x7, the rest on the machine's stack, results in x0 and x1,
38+ or in memory at the address in x8, sp a multiple of 16 always, a
3739 frame record of x29 and x30 that x29 points at, so that the chain of
3840 frames can be followed. A frame is made whole by the prologue, so
39- nothing is pushed in a body: sp stays where the prologue put it, and
40- pushed is always 0.
41+ nothing is pushed in a body but a call's stack arguments, for the
42+ call, and a deferred statement's return address: pushed is always 0.
4143
44+ What x86-64 catches by itself, and arm64 doesn't, the code checks: a
45+ division by zero, and the smallest value by -1 (divide).
46+
4247 Depends on: Scan and Diag for errors, Sym for types, Chunk for code
4348 and data, A64 for instructions.
44- Leaves out, for now: reals, division, checks, COPY and FILL,
45- arguments and parameters on the machine's stack, results in memory,
46- try, deferred statements, atomics and system calls. *)
49+ Leaves out, for now: reals, FILL, NEW's zeroing, the counts of bits
50+ and the rotations; and SYSCALL, which is Linux's only. *)
4751 module Gen;
4852 import Fmt, Diag, Scan, Sym, Chunk, A := A64;
4953 import SYSTEM;
@@ -53,9 +57,11 @@
5357 NoLabel* = -1;
5458 WordSize* = 8; (* the size of an address, and of INT *)
5559 (* What the machine catches by itself: a load or store within
56- NilPage bytes of address 0 (the Mac's page zero is 4 GB; this is
57- Linux's figure until Rt tells the Mac's faults) *)
58- NilPage* = 4096;
60+ NilPage bytes of address 0, in the Mac's page zero of 4 GB, which
61+ Rt's handler tells as a nil pointer; and a call to nil, and a
62+ stack overflow at the guard page under the stack. A division by
63+ zero it doesn't catch: op checks for it. *)
64+ NilPage* = 100000000H;
5965
6066 FP = A.FP; LR = A.LR; SP = A.SP; ZR = A.ZR;
6167 T = 15; U = 17; (* Gen's scratch *)
@@ -140,8 +146,18 @@
140146 into that chunk, reached by adrp (A64.loadChunk) *)
141147 Mem = record base, chunk: INT; off: I64; end;
142148
149+ (* a check of the procedure, whose failure endFails makes: what
150+ failed, a's register, the length's (or -1 for the constant n),
151+ the enumeration's name, and the place *)
152+ Fail = record
153+ L, what, ra, lenReg, name: INT;
154+ n: I64;
155+ path, line, col: INT;
156+ end;
157+
143158 const
144159 MaxStack = 1024; (* values waiting in one expression *)
160+ StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"];
145161 NumRegs = 6;
146162 Regs: [NumRegs]INT = [9, 10, 11, 12, 13, 14];
147163 (* the condition codes of the comparisons, from sEql: = # < <= > >= *)
@@ -161,17 +177,24 @@
161177 retSlot: I64;
162178 resType: ^Sym.Type; (* the procedure's result, or nil *)
163179 exitL: INT;
164- (* parameters arriving in registers: where each is stored on entry *)
165- inReg: [8]INT;
166- inSlot: [8]I64;
167- inSize: [8]INT;
168- inObj: [8]^Sym.Obj;
180+ (* parameters arriving in registers, x0 to x7, and a result's address
181+ in x8: where each is stored on entry *)
182+ inReg: [9]INT;
183+ inSlot: [9]I64;
184+ inSize: [9]INT;
185+ inObj: [9]^Sym.Obj;
169186 numIn: INT;
187+ procParams: ^Sym.Obj; (* the parameters, for enterBody *)
170188 regMask: INT; (* the registers of variables, as bits (XBit) *)
171189 saveAt: I64; (* where they are saved, a quadword each *)
172190 frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *)
173191 pending: []const U8; (* what beginProc found not yet made, for enterBody's chunk *)
174192 pendingAt: Diag.Pos;
193+ (* the stubs that a failed check calls, one for each kind of failure,
194+ made by entryPoint for those used; and the procedure's checks *)
195+ failStubs: [8]INT;
196+ fails: [4096]Fail;
197+ numFails: INT;
175198
176199 (* What this Gen can't make yet marks the procedure being compiled
177200 (Chunk.setMissing), which is an error if the program uses it, and the
@@ -179,21 +202,21 @@
179202 calls, as Sys does. So each routine not yet made keeps the stack as
180203 the real one would, and makes what code it likes, never run; it
181204 reads into _ the parameters it doesn't use yet, so that none is
182- warned of. *)
183-proc notYetAt(what: []const U8; p: Diag.Pos);
205+ warned of. What this Gen will never make, SYSCALL, is marked so too,
206+ with its own message. *)
207+proc missing(msg: []const U8);
184208 begin
185209 if codeChunk >= 0 then
186- Chunk.setMissing(codeChunk, Diag.say("not yet on arm64: {}", [Fmt.str(what)]), p);
210+ Chunk.setMissing(codeChunk, msg, Scan.tokPos());
187211 elsif LEN(pending) = 0 then (* beginProc's, before enterBody names the chunk *)
188- pending := Diag.say("not yet on arm64: {}", [Fmt.str(what)]);
189- pendingAt := p;
212+ pending := msg;
213+ pendingAt := Scan.tokPos();
190214 end;
191215 end;
192216
193-(* The same, at the place being read *)
194217 proc notYet(what: []const U8);
195218 begin
196- notYetAt(what, Scan.tokPos());
219+ missing(Diag.say("not yet on arm64: {}", [Fmt.str(what)]));
197220 end;
198221
199222 (* -S: the text of each instruction is kept, and list prints it *)
@@ -499,14 +522,40 @@
499522 end;
500523
501524 (* Copying, by loads and stores of 8, 4, 2 and 1 bytes, from [x1] to
502- [x0], through x2 *)
525+ [x0], through x2. Past MaxMoves bytes the words go by a loop, which
526+ moves x0 and x1 on, counting down in x3, and the last few bytes
527+ after it as before:
503528
529+ mov x3, #words
530+ top: ldr x2, [x1], #8
531+ str x2, [x0], #8
532+ subs x3, x3, #1
533+ b.ne top
534+ ... ; the size mod 8 bytes left, from [x1] to [x0]
535+
536+ zeroBytes is the same, storing the zero register. *)
537+
504538 const MaxMoves = 256;
505539
540+(* The loop's head and tail, of the words of size bytes, by store and
541+ load if load; returns what is left after the words *)
542+proc wordLoop(size: I64; load: BOOL): I64;
543+var top: INT;
544+begin
545+ if size <= MaxMoves then return size; end;
546+ A.movRI(3, size div 8);
547+ top := Chunk.pc;
548+ if load then A.loadIdx(Tmp, Src, 8, 8, A.mPost); A.storeIdx(Tmp, Dst, 8, 8, A.mPost);
549+ else A.storeIdx(ZR, Dst, 8, 8, A.mPost); end;
550+ A.aluI(A.opSubs, 3, 3, 1);
551+ A.jccBack(A.ccNE, top);
552+ return size mod 8;
553+end;
554+
506555 proc copyBytes(size: I64);
507556 var o, n: I64;
508557 begin
509- if size > MaxMoves then notYet("a copy of more than 256 bytes"); return; end;
558+ size := wordLoop(size, true);
510559 o := 0;
511560 while o < size do
512561 n := 8;
@@ -520,7 +569,7 @@
520569 proc zeroBytes(size: I64);
521570 var o, n: I64;
522571 begin
523- if size > MaxMoves then notYet("zeroing more than 256 bytes"); return; end;
572+ size := wordLoop(size, false);
524573 o := 0;
525574 while o < size do
526575 n := 8;
@@ -560,6 +609,71 @@
560609 zeroBytes(size);
561610 end;
562611
612+(* Checks *)
613+
614+(* The name s, interned *)
615+proc internStr(s: []const U8): INT;
616+begin
617+ return Scan.intern(SYSTEM.PTR(s), LEN(s));
618+end;
619+
620+(* The program goes on if the condition cc holds, and otherwise stops
621+ in Rt.fail(what, a, b, len, at, name), as on x86-64, whose check says
622+ what each is (Gen.x86_64.raz). Here a is in register ra and b, where
623+ there is one, in x2; len is in register lenReg, or the constant n if
624+ lenReg is -1. The failing code is out of the way, at the procedure's
625+ end (endFails), so that a check that holds is a branch not taken; it
626+ has p for its place, worked out now. *)
627+proc check(cc, what: INT; p: Diag.Pos; ra, lenReg: INT; n: I64; name: INT);
628+var f: INT;
629+begin
630+ if numFails > LEN(fails) - 1 then
631+ _ := Scan.mark(Diag.say("more than {} checks in one procedure", [Fmt.int(LEN(fails))]));
632+ return;
633+ end;
634+ f := numFails;
635+ fails[f].L := NoLabel;
636+ A.jcc(A.invert(cc), fails[f].L);
637+ fails[f].what := what;
638+ fails[f].ra := ra;
639+ fails[f].lenReg := lenReg;
640+ fails[f].n := n;
641+ fails[f].name := name;
642+ fails[f].path := Chunk.curFilePath();
643+ fails[f].line, fails[f].col := Scan.lineCol(p.a);
644+ INC(numFails);
645+end;
646+
647+(* The failures of the procedure's checks: each puts Rt.fail's values in
648+ the registers it takes them in, x3 first, since a is never there,
649+ then x1 and x5, and calls the stub for its kind, which adds what
650+ failed and where (entryPoint):
651+
652+ mov x3, x10 ; the length, or mov x3, #n
653+ mov x1, x15 ; a
654+ adrp x5, name; add x5 ... ; an enumeration's name
655+ bl check0 *)
656+proc endFails;
657+var i, path: INT; f: Fail;
658+begin
659+ path := Chunk.curFilePath();
660+ for i := 0 to numFails - 1 do
661+ f := fails[i];
662+ A.fixLink(f.L);
663+ Chunk.setCurFile(f.path);
664+ Chunk.noteLine(f.line, f.col);
665+ Chunk.setCurFile(path);
666+ if f.lenReg < 0 then A.movRI(3, f.n); elsif f.lenReg # 3 then A.movRR(3, f.lenReg); end;
667+ if f.ra # 1 then A.movRR(1, f.ra); end;
668+ if f.name >= 0 then A.addrChunk(5, f.name, 0); end;
669+ if failStubs[f.what] < 0 then
670+ failStubs[f.what] := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr(StubNames[f.what]));
671+ end;
672+ A.callChunk(failStubs[f.what]);
673+ end;
674+ numFails := 0;
675+end;
676+
563677 (* Operations *)
564678
565679 (* The right operand of the two values on top, popped, in register y:
@@ -586,6 +700,46 @@
586700 end;
587701 end;
588702
703+(* r div y, or r mod y, of bits bits, signed or not, y being the
704+ constant v if c. x86's idiv stops the program for a division by
705+ zero, and for the smallest value divided by -1, which overflows; the
706+ report binds both (6.1), and arm64's sdiv and udiv give a value for
707+ each, so the code checks first, as Rt.fail's kind 5, a division
708+ fault. The smallest value is that of the type's width, for a narrow
709+ one, an I32's -2^31, sign-extended in 64 bits. Of an I64:
710+
711+ cmp x17, #0
712+ b.eq fail ; by zero
713+ cmn x17, #1
714+ b.ne L
715+ mov x15, #8000000000000000H
716+ cmp x9, x15
717+ b.eq fail ; the smallest by -1
718+ L: sdiv x15, x9, x17
719+ mov x9, x15 ; div; for mod, msub x9, x15, x17, x9
720+
721+ msub is x9 - x15 * x17, what is left. A constant divisor other than
722+ 0 needs no first check, and other than -1 no second. *)
723+proc divide(sy: Scan.Sym; r, y, bits: INT; signed, c: BOOL; v: I64);
724+var L: INT;
725+begin
726+ if not c or (v = 0) then
727+ A.cmpI(y, 0);
728+ check(A.ccNE, 5, Scan.tokPos(), r, -1, 0, -1);
729+ end;
730+ if signed and (not c or (v = -1)) then
731+ L := NoLabel;
732+ A.cmpI(y, -1);
733+ A.jcc(A.ccNE, L);
734+ A.movRI(T, I64(-1) shl (bits - 1));
735+ A.cmp(r, T);
736+ check(A.ccNE, 5, Scan.tokPos(), r, -1, 0, -1);
737+ A.fixLink(L);
738+ end;
739+ A.divide(signed, T, r, y);
740+ if sy = Scan.sDiv then A.movRR(r, T); else A.madd(r, T, y, r, true); end;
741+end;
742+
589743 (* The two values on top, the left op the right, an integer of size
590744 bytes, signed or not, computed in 64 bits and brought back to its
591745 width, which is how a narrow integer wraps. A shift's count is taken
@@ -594,7 +748,6 @@
594748 var v: I64; r, y, bits, sh: INT; c, alu: BOOL;
595749 begin
596750 if k in {kF32, kF64} then notYet("reals"); end;
597- if (sy = Scan.sDiv) or (sy = Scan.sMod) then notYet("div and mod"); end;
598751 bits := size * 8;
599752 alu := (sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor);
600753 (* a constant goes right, where it can be an immediate *)
@@ -616,6 +769,8 @@
616769 A.alu(aluOf(sy), r, r, y);
617770 elsif sy = Scan.sTimes then
618771 A.mul(r, r, y);
772+ elsif (sy = Scan.sDiv) or (sy = Scan.sMod) then
773+ divide(sy, r, y, bits, signed, c, v);
619774 else (* sShl, sShr: the machine takes the count modulo 64 *)
620775 if bits < 64 then A.aluI(A.opAnd, U, y, bits - 1); y := U; end;
621776 A.shift(sh, r, r, y, 8);
@@ -643,10 +798,21 @@
643798 notYet("POPCOUNT");
644799 end;
645800
801+(* ABS of the signed integer on top: negated if negative, and cut back
802+ to its width, so that the smallest value gives itself, as -x does
803+ (Gen.x86_64.raz says more):
804+
805+ cmp x9, #0
806+ cneg x9, x9, lt ; csneg x9, x9, x9, ge
807+ sxtw x9, w9 ; an I32 *)
646808 proc abs*(k: Kind; size: INT);
809+var r: INT;
647810 begin
648- _ := k; _ := size;
649- notYet("ABS");
811+ if k in {kF32, kF64} then notYet("reals"); end;
812+ r := top();
813+ A.cmpI(r, 0);
814+ A.csel(A.csNeg, A.ccGE, r, r, r);
815+ if size < 8 then A.extend(r, r, size, true); end;
650816 end;
651817
652818 (* The two values on top, compared: a BOOL, 1 or 0 *)
@@ -714,11 +880,48 @@
714880 stack[depth - 1].k := k;
715881 end;
716882
883+(* Whether the slices in the frame at a and b (the offsets of their
884+ pointers, the lengths 8 bytes on) are equal (or not, if not eq): the
885+ lengths, then the bytes, len shl shift of them, a byte at a time. The
886+ flags say equal or not, where the loop leaves it:
887+
888+ ldr x2, [x29, #b + 8]
889+ ldr x15, [x29, #a + 8]
890+ cmp x2, x15
891+ b.ne out ; the lengths differ
892+ ldr x1, [x29, #b]
893+ ldr x0, [x29, #a]
894+ lsl x2, x2, #shift
895+ top: cmp x2, #0
896+ b.eq out ; no bytes left: equal
897+ ldrb w15, [x0], #1
898+ ldrb w17, [x1], #1
899+ sub x2, x2, #1
900+ cmp x15, x17
901+ b.eq top
902+ out: cset x9, eq ; or ne *)
717903 proc compareSeq*(a, b: I64; shift: INT; eq: BOOL);
904+var r, out, loop: INT;
718905 begin
719- _ := a; _ := b; _ := shift; _ := eq;
720- notYet("comparing slices");
721- _ := target();
906+ r := target();
907+ out := NoLabel;
908+ A.load(Tmp, FP, INT(b) + 8, 8, false);
909+ A.load(T, FP, INT(a) + 8, 8, false);
910+ A.cmp(Tmp, T);
911+ A.jcc(A.ccNE, out);
912+ A.load(Src, FP, INT(b), 8, false);
913+ A.load(Dst, FP, INT(a), 8, false);
914+ if shift > 0 then A.shiftI(A.shLsl, Tmp, Tmp, shift, 8); end;
915+ loop := Chunk.pc;
916+ A.cmpI(Tmp, 0);
917+ A.jcc(A.ccEQ, out);
918+ A.loadIdx(T, Dst, 1, 1, A.mPost);
919+ A.loadIdx(U, Src, 1, 1, A.mPost);
920+ A.aluI(A.opSub, Tmp, Tmp, 1);
921+ A.cmp(T, U);
922+ A.jccBack(A.ccEQ, loop);
923+ A.fixLink(out);
924+ if eq then A.cset(A.ccEQ, r); else A.cset(A.ccNE, r); end;
722925 computed(kWord);
723926 end;
724927
@@ -768,19 +971,39 @@
768971 _ := size;
769972 notYet("NEW");
770973 end;
974+
975+(* CAS(v, old, new), XCHG(v, x) or XADD(v, x), from the stack, v's
976+ address under the rest: the result where the address was. By the
977+ LSE instructions every Apple processor has, each with acquire and
978+ release, as x86's lock does: casal stores new if [v] holds old,
979+ leaving what [v] held where old was, which says whether it did:
980+
981+ mov x17, x15 ; old
982+ casal x17, x10, [x9] ; x10, new
983+ cmp x17, x15
984+ cset x9, eq
771985
986+ swpal and ldaddal leave what [v] held in x17, the result. *)
772987 proc atomic*(which: INT);
988+var r, n: INT;
773989 begin
774- notYet("CAS, XCHG and XADD");
775- if which = Sym.bCas then drop(); end;
776- drop();
990+ if which = Sym.bCas then n := top(); drop(); end;
991+ popTo(T);
992+ r := top();
993+ match which of
994+ | Sym.bCas: A.movRR(U, T); A.casal(U, n, r, 8); A.cmp(U, T); A.cset(A.ccEQ, r);
995+ | Sym.bXchg: A.swpal(T, U, r, 8); A.movRR(r, U);
996+ | else: A.ldaddal(T, U, r, 8); A.movRR(r, U);
997+ end;
777998 stack[depth - 1].k := kWord;
778999 end;
7791000
1001+(* SYSTEM.SYSCALL is Linux's only (report 6.8): macOS has no stable
1002+ system calls, and a program calls the C library there *)
7801003 proc sysCall*(n: INT);
7811004 var i: INT;
7821005 begin
783- notYet("SYSCALL");
1006+ missing("SYSTEM.SYSCALL is Linux's only: call the C library");
7841007 for i := 1 to n do drop(); end;
7851008 _ := target();
7861009 computed(kWord);
@@ -856,14 +1079,45 @@
8561079 A.fixLink(L);
8571080 end;
8581081
1082+(* A jump table, for a match on the integer in frame slot slot, whose
1083+ labels run from lo up for n values. The selector less lo, compared
1084+ unsigned (so below lo is above n too), is out of the table if it
1085+ isn't below n, and goes to the else arm at elseAt, or, if there is
1086+ none, to endL, the end of the match. Else it picks a word of the
1087+ table that follows the jump, an offset from the table's start, which
1088+ adr finds four instructions on:
1089+
1090+ ldr x15, [x29, #slot]
1091+ sub x15, x15, #lo
1092+ cmp x15, #n - 1
1093+ b.hi else
1094+ adr x17, table
1095+ ldrsw x15, [x17, x15, lsl #2]
1096+ add x15, x17, x15
1097+ br x15
1098+ table: .word arm0 - table, arm1 - table, ...
1099+
1100+ It returns the table's start; the caller then makes the n entries
1101+ with tableEntry. *)
8591102 proc jumpTable*(slot, lo: I64; n, elseAt: INT; var endL: INT): INT;
1103+const TableAt = 16;
1104+var start: INT;
8601105 begin
861- _ := slot; _ := lo; _ := n; _ := elseAt; _ := endL;
862- notYet("a jump table");
863- return Chunk.pc;
1106+ A.load(T, FP, INT(slot), 8, false);
1107+ if lo # 0 then A.aluI(A.opSub, T, T, lo); end;
1108+ A.cmpI(T, n - 1);
1109+ if elseAt # NoLabel then A.jccBack(A.ccHI, elseAt); else A.jcc(A.ccHI, endL); end;
1110+ start := Chunk.pc + TableAt;
1111+ A.adr(U, TableAt);
1112+ A.loadR(T, U, T, 4, true, true);
1113+ A.alu(A.opAdd, T, U, T);
1114+ A.jmpR(T);
1115+ if Chunk.pc # start then Diag.fatal("internal error: the jump table is not where it was said"); end;
1116+ return start;
8641117 end;
8651118
866-(* The next entry of the table that jumpTable started at table *)
1119+(* The next entry of the table that jumpTable started at table: the
1120+ code at target *)
8671121 proc tableEntry*(table, target: INT);
8681122 begin
8691123 Chunk.put4(target - table);
@@ -884,30 +1138,55 @@
8841138 A.fixLinkTo(L, top);
8851139 end;
8861140
887-(* Checks, and what checks as it goes *)
1141+(* Checks, and what checks as it goes, as on x86-64, whose comments
1142+ say more; a comparison is unsigned, so that a negative index is a
1143+ huge one *)
8881144
1145+(* The index on top is below n *)
8891146 proc checkIndex*(n: I64; p: Diag.Pos);
1147+var r: INT;
8901148 begin
891- _ := n;
892- notYetAt("checks", p);
1149+ r := top();
1150+ A.cmpI(r, n);
1151+ check(A.ccLO, 0, p, r, -1, n, -1);
8931152 end;
8941153
1154+(* The index on top is where a view starts in an array of n elements,
1155+ below below *)
8951156 proc checkView*(n, below: I64; p: Diag.Pos);
1157+var r: INT;
8961158 begin
897- _ := n; _ := below;
898- notYetAt("checks", p);
1159+ r := top();
1160+ A.cmpI(r, below);
1161+ check(A.ccLO, 7, p, r, -1, n, -1);
8991162 end;
9001163
1164+(* The integer on top is a position of an enumeration of n names *)
9011165 proc checkEnum*(n: I64; name: INT; p: Diag.Pos);
1166+var r: INT;
9021167 begin
903- _ := n; _ := name;
904- notYetAt("checks", p);
1168+ r := top();
1169+ A.cmpI(r, n);
1170+ check(A.ccLO, 2, p, r, -1, n, name);
9051171 end;
9061172
1173+(* The address on top, to be used reach bytes past, isn't nil, unless
1174+ the machine would stop it, within page zero *)
9071175 proc checkNil*(reach: I64; p: Diag.Pos);
1176+var r: INT;
1177+begin
1178+ if reach <= NilPage then return; end;
1179+ r := top();
1180+ A.cmpI(r, 0);
1181+ check(A.ccNE, 4, p, r, -1, 0, -1);
1182+end;
1183+
1184+(* What a failed index check is: an index past the length (0), or a
1185+ view that runs past it (7) when it needs room elements more *)
1186+proc kindOfIndex(room: I64): INT;
9081187 begin
909- _ := reach;
910- notYetAt("checks", p);
1188+ if room > 0 then return 7; end;
1189+ return 0;
9111190 end;
9121191
9131192 (* r times v, an element's size: by a shift where v is a power of two,
@@ -927,17 +1206,42 @@
9271206
9281207 (* Pops an index, and the address of the slice at a if it is on the
9291208 stack, and makes the address of the element it picks: the slice's
930- pointer plus the index times size *)
1209+ pointer plus the index times size. The index is checked against the
1210+ length, in its register or loaded into x3; a view, which needs room
1211+ elements after the one indexed, against the length less the index,
1212+ which can't wrap once the index is below it:
1213+
1214+ ldr x3, [x29, #-24] ; the length
1215+ cmp x15, x3
1216+ b.hs fail
1217+ sub x2, x3, x15
1218+ cmp x2, #room
1219+ b.ls fail *)
9311220 proc indexSlice*(a: Adr; size, room: I64; checked: BOOL; p: Diag.Pos);
932-var m: Mem; r: INT;
1221+var m, ml: Mem; r, len: INT;
9331222 begin
934- if checked or (room > 0) then notYetAt("checks", p); end;
9351223 popTo(T);
1224+ if a.base = bReg then
1225+ len := a.hi;
1226+ else
1227+ m := place(a, U);
1228+ ml := m; ml.off := ml.off + 8;
1229+ len := 3;
1230+ if checked then loadMem(len, ml, 8, false); end;
1231+ end;
1232+ if checked then
1233+ A.cmp(T, len);
1234+ check(A.ccLO, kindOfIndex(room), p, T, len, 0, -1);
1235+ if room > 0 then
1236+ A.alu(A.opSub, Tmp, len, T);
1237+ A.cmpI(Tmp, room);
1238+ check(A.ccHI, 7, p, T, len, 0, -1);
1239+ end;
1240+ end;
9361241 mulI(T, size);
9371242 if a.base = bReg then
9381243 A.alu(A.opAdd, T, T, a.chunk);
9391244 else
940- m := place(a, U);
9411245 loadMem(U, m, 8, false);
9421246 A.alu(A.opAdd, T, T, U);
9431247 end;
@@ -966,20 +1270,32 @@
9661270 (* Pops hi and lo, and the address of the array or slice at a if it is
9671271 on the stack, and makes the slice lo..hi of it, of elements of size
9681272 bytes, in the frame at tmp: its pointer, the start plus lo times size,
969- and its length, hi - lo *)
1273+ and its length, hi - lo. The array has n elements. Two checks, both
1274+ unsigned, so that a negative bound fails too: hi against the length,
1275+ then lo against hi; lo is in x1 and hi in x2, as Rt.fail takes a and
1276+ b. *)
9701277 proc slice*(a: Adr; isArray: BOOL; n, size, tmp: I64; checked: BOOL; p: Diag.Pos);
971-var m: Mem;
1278+var m, ml: Mem; len: INT;
9721279 begin
973- _ := n;
974- if checked then notYetAt("checks", p); end;
9751280 popTo(Tmp); (* hi *)
9761281 popTo(Src); (* lo *)
977- if a.base = bReg then
978- A.movRR(U, a.chunk);
979- else
980- m := place(a, U);
981- if isArray then adrTo(U, m); else loadMem(U, m, 8, false); end;
1282+ if a.base # bReg then m := place(a, U); end;
1283+ if checked then
1284+ if isArray then
1285+ len := -1;
1286+ A.cmpI(Tmp, n);
1287+ else
1288+ len := a.hi;
1289+ if a.base # bReg then len := 3; ml := m; ml.off := ml.off + 8; loadMem(len, ml, 8, false); end;
1290+ A.cmp(Tmp, len);
1291+ end;
1292+ check(A.ccLS, 1, p, Src, len, n, -1);
1293+ A.cmp(Src, Tmp);
1294+ check(A.ccLS, 1, p, Src, len, n, -1);
9821295 end;
1296+ if a.base = bReg then A.movRR(U, a.chunk);
1297+ elsif isArray then adrTo(U, m);
1298+ else loadMem(U, m, 8, false); end;
9831299 A.alu(A.opSub, Tmp, Tmp, Src);
9841300 A.store(Tmp, FP, INT(tmp) + 8, 8);
9851301 mulI(Src, size);
@@ -987,10 +1303,79 @@
9871303 A.store(U, FP, INT(tmp), 8);
9881304 end;
9891305
1306+(* The x2 bytes from [x1] to [x0], n at a time while x2 holds n or more,
1307+ through x15: forward, moving x1 and x0 on after each, or back, from
1308+ the ends, moving them down first *)
1309+proc copyRun(n: INT; back: BOOL);
1310+var top, out: INT;
1311+begin
1312+ top := Chunk.pc;
1313+ out := NoLabel;
1314+ A.cmpI(Tmp, n);
1315+ A.jcc(A.ccLO, out);
1316+ if back then
1317+ A.loadIdx(T, Src, -n, n, A.mPre); A.storeIdx(T, Dst, -n, n, A.mPre);
1318+ else
1319+ A.loadIdx(T, Src, n, n, A.mPost); A.storeIdx(T, Dst, n, n, A.mPost);
1320+ end;
1321+ A.aluI(A.opSub, Tmp, Tmp, n);
1322+ A.jmpBack(top);
1323+ A.fixLink(out);
1324+end;
1325+
1326+(* COPY: the elements of the slice in the frame at sOff, of size bytes
1327+ each, to the start of the one at dOff, which must be at least as long
1328+ (the source may be shorter): slice 0..LEN(s) of d, checked. They go
1329+ by words, then the last few bytes one by one, forward; but back, from
1330+ the ends, when the destination starts inside the source, so that an
1331+ overlap comes out right (report 6.8):
1332+
1333+ ldr x2, [x29, #sOff + 8]
1334+ ldr x3, [x29, #dOff + 8]
1335+ cmp x2, x3
1336+ b.hi fail ; the source longer than the destination
1337+ ldr x1, [x29, #sOff]
1338+ ldr x0, [x29, #dOff]
1339+ cmp x0, x1
1340+ b.ls fwd ; the destination not above: forward
1341+ add x15, x1, x2
1342+ cmp x0, x15
1343+ b.hs fwd ; nor inside the source
1344+ add x1, x1, x2 ; back, from the ends
1345+ add x0, x0, x2
1346+ ... ; copyRun(8, back), copyRun(1, back)
1347+ b out
1348+ fwd: ... ; copyRun(8), copyRun(1)
1349+ out: *)
9901350 proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos);
1351+var fwd, out: INT;
9911352 begin
992- _ := dOff; _ := sOff; _ := size; _ := checked;
993- notYetAt("COPY", p);
1353+ A.load(Tmp, FP, INT(sOff) + 8, 8, false);
1354+ if checked then
1355+ A.load(3, FP, INT(dOff) + 8, 8, false);
1356+ A.movRI(Src, 0); (* a, the slice's start *)
1357+ A.cmp(Tmp, 3);
1358+ check(A.ccLS, 1, p, Src, 3, 0, -1);
1359+ end;
1360+ mulI(Tmp, size);
1361+ A.load(Src, FP, INT(sOff), 8, false);
1362+ A.load(Dst, FP, INT(dOff), 8, false);
1363+ fwd := NoLabel;
1364+ A.cmp(Dst, Src);
1365+ A.jcc(A.ccLS, fwd);
1366+ A.alu(A.opAdd, T, Src, Tmp);
1367+ A.cmp(Dst, T);
1368+ A.jcc(A.ccHS, fwd);
1369+ A.alu(A.opAdd, Src, Src, Tmp);
1370+ A.alu(A.opAdd, Dst, Dst, Tmp);
1371+ copyRun(8, true);
1372+ copyRun(1, true);
1373+ out := NoLabel;
1374+ A.jmp(out);
1375+ A.fixLink(fwd);
1376+ copyRun(8, false);
1377+ copyRun(1, false);
1378+ A.fixLink(out);
9941379 end;
9951380
9961381 proc fillSlice*(dOff, size, n: I64; scalar: BOOL);
@@ -1000,13 +1385,17 @@
10001385 drop();
10011386 end;
10021387
1003-(* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp *)
1388+(* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp. Its
1389+ bounds can't be known, but it can't run backwards. *)
10041390 proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos);
10051391 begin
1006- if checked then notYetAt("checks", p); end;
10071392 popTo(Tmp); (* hi *)
10081393 popTo(Src); (* lo *)
10091394 popTo(U); (* the pointer *)
1395+ if checked then
1396+ A.cmp(Src, Tmp);
1397+ check(A.ccLE, 3, p, Src, -1, 0, -1);
1398+ end;
10101399 A.alu(A.opSub, Tmp, Tmp, Src);
10111400 A.store(Tmp, FP, INT(tmp) + 8, 8);
10121401 mulI(Src, size);
@@ -1014,36 +1403,74 @@
10141403 A.store(U, FP, INT(tmp), 8);
10151404 end;
10161405
1406+(* The words the parameters of procedure type sig take: two for a
1407+ slice passed by value, one for anything else *)
1408+proc paramWords(sig: ^Sym.Type): INT;
1409+var q: ^Sym.Obj; n: INT;
1410+begin
1411+ n := 0;
1412+ q := sig^.fields;
1413+ while q # nil do
1414+ if (q^.typ^.form = Sym.fSlice) and not q^.isVar then INC(n, 2); else INC(n); end;
1415+ q := q^.next;
1416+ end;
1417+ return n;
1418+end;
1419+
10171420 (* Calls. The arguments are the c.n values on top, each moved into the
10181421 next of x0 to x7 just before the call; the values under them are
10191422 spilled first, as the call may change their registers. As the stack's
10201423 registers carry no argument, the moves may go in any order. A slice
1021- goes as its two words, a record by value as the address of a copy
1022- (Item makes it). A single result comes back in x0, two words in x0
1023- and x1: a slice is stored in the temporary c.ret, two results pushed
1024- as two values. *)
1424+ goes as its two words, both in registers or both on the stack, a
1425+ record by value as the address of a copy (Item makes it). The
1426+ arguments past the registers go on the machine's stack, 8 bytes
1427+ each, the first at sp, in an area sp is lowered by for the call, a
1428+ multiple of 16; and so do those past a C procedure's own parameters
1429+ where it ends in ..., in registers or not, as Apple's convention has
1430+ it (where a C procedure's own parameters past x7 narrower than 8
1431+ bytes would be packed, which raz doesn't do). A result in memory, a
1432+ record, an array or several results, goes to the caller's temporary
1433+ c.ret, its address in x8. A single result comes back in x0, two words
1434+ in x0 and x1: a slice is stored in c.ret, two results pushed as two
1435+ values. *)
10251436 proc call*(var c: CallInfo);
1026-var res: ^Sym.Type; i, ni, first, r: INT;
1437+var res: ^Sym.Type; i, ni, k, first, own, r: INT; area: I64; onStack: [64]BOOL;
10271438 begin
10281439 first := depth - c.n;
10291440 spillBelow(first);
10301441 res := c.sig^.base;
10311442 c.ret := 0;
1032- if c.sig^.variadic then notYet("a call of a C procedure that takes ..."); end;
1033- if (res # nil) and (Sym.holdsArray(res) or Sym.behindPointer(res)) then
1034- notYet("a result in memory");
1035- c.ret := allocPerm(res^.size, res^.align);
1036- elsif (res # nil) and (res^.form = Sym.fSlice) then
1443+ if (res # nil) and Sym.holdsArray(res) then
1444+ c.ret := allocPerm(res^.size, res^.align); (* kept, as on x86-64 (report 8) *)
1445+ elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then
10371446 c.ret := allocTemp(res^.size, res^.align);
10381447 elsif (res # nil) and (res^.form = Sym.fReal) then
10391448 notYet("reals");
10401449 end;
1041- ni := 0;
1450+ own := c.n;
1451+ if c.sig^.variadic then own := paramWords(c.sig); end;
1452+ ni := 0; k := 0;
10421453 for i := 0 to c.n - 1 do
10431454 if c.isF[i] then notYet("reals"); end;
1044- if ni < 8 then valueTo(first + i, ni); else notYet("an argument past x7"); end;
1045- INC(ni);
1455+ if i >= own then onStack[i] := true;
1456+ elsif (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1];
1457+ else onStack[i] := ni + 1 + INT(c.pair[i]) > 8; end;
1458+ if onStack[i] then INC(k); else INC(ni); end;
10461459 end;
1460+ area := Sym.alignUp(8 * k, 16);
1461+ if area > 0 then A.aluI(A.opSub, SP, SP, area); end;
1462+ k := 0; ni := 0;
1463+ for i := 0 to c.n - 1 do
1464+ if onStack[i] then
1465+ valueTo(first + i, T);
1466+ A.store(T, SP, 8 * k, 8);
1467+ INC(k);
1468+ else
1469+ valueTo(first + i, ni);
1470+ INC(ni);
1471+ end;
1472+ end;
1473+ if (c.ret # 0) and Sym.behindPointer(res) then A.aluI(A.opAdd, 8, FP, c.ret); end;
10471474 for i := 1 to c.n do drop(); end;
10481475 if c.ext then
10491476 A.loadChunk(U, c.chunk, 0, 8, false);
@@ -1054,6 +1481,7 @@
10541481 A.load(U, FP, INT(c.target), 8, false);
10551482 A.callR(U);
10561483 end;
1484+ if area > 0 then A.aluI(A.opAdd, SP, SP, area); end;
10571485 if res # nil then
10581486 if res^.form = Sym.fSlice then
10591487 A.store(0, FP, INT(c.ret), 8);
@@ -1109,10 +1537,12 @@
11091537 end;
11101538
11111539 (* The parameters' places: each in a frame slot of its own, stored there
1112- from its register on entry. A var parameter, and a record or array
1113- passed by value, holds an address (ind). *)
1540+ from its register on entry, or above the frame where the caller put
1541+ it, past the frame record (call). A var parameter, and a record or
1542+ array passed by value, holds an address (ind). The address of a
1543+ result in memory arrives in x8, and is kept in retSlot. *)
11141544 proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj);
1115-var p: ^Sym.Obj; ni, k, w: INT;
1545+var p: ^Sym.Obj; ni, k, w: INT; stackOff: I64;
11161546 begin
11171547 frameTop := 0; frameMax := 0; permTop := 0;
11181548 resetStack(0, 0);
@@ -1120,21 +1550,30 @@
11201550 numIn := 0;
11211551 lost := false;
11221552 resType := sig^.base;
1553+ procParams := params;
11231554 regMask := 0;
11241555 if (resType # nil) and Sym.behindPointer(resType) then
1125- notYet("a result in memory");
11261556 retSlot := allocPerm(8, 8);
1557+ inReg[0] := 8; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil;
1558+ numIn := 1;
11271559 end;
11281560 ni := 0;
1561+ stackOff := 16;
11291562 p := params;
11301563 while p # nil do
11311564 p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice));
11321565 k := 1;
11331566 if (p^.typ^.form = Sym.fSlice) and not p^.isVar then k := 2; end;
1134- p^.val := allocPerm(8 * k, 8);
1135- if (p^.typ^.form = Sym.fReal) and not p^.isVar or (ni + k > 8) then
1136- notYet("a real parameter, or one past x7");
1567+ if (p^.typ^.form = Sym.fReal) and not p^.isVar then
1568+ notYet("a real parameter");
1569+ p^.val := allocPerm(8, 8);
11371570 k := 0;
1571+ elsif ni + k > 8 then (* on the stack, as call puts it *)
1572+ p^.val := stackOff;
1573+ INC(stackOff, 8 * k);
1574+ k := 0;
1575+ else
1576+ p^.val := allocPerm(8 * k, 8);
11381577 end;
11391578 for w := 0 to k - 1 do (* a slice's two words, in two slots *)
11401579 inReg[numIn] := ni; inSlot[numIn] := p^.val + I64(8 * w); inObj[numIn] := p;
@@ -1191,7 +1630,7 @@
11911630 proc markCode*(var m: CodeMark);
11921631 begin
11931632 m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := A.saidCount();
1194- m.fails := 0; m.exit := exitL;
1633+ m.fails := numFails; m.exit := exitL;
11951634 end;
11961635
11971636 (* The code made since m taken back; the frame is left as it is *)
@@ -1199,6 +1638,7 @@
11991638 begin
12001639 Chunk.rewindTo(m.pc, m.fixups, m.lines);
12011640 A.unsay(m.said);
1641+ numFails := m.fails;
12021642 exitL := m.exit;
12031643 end;
12041644
@@ -1208,6 +1648,7 @@
12081648 begin
12091649 Chunk.rewindTo(m.pc, m.fixups, m.lines);
12101650 A.unsay(m.said);
1651+ numFails := 0;
12111652 frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2];
12121653 exitL := NoLabel;
12131654 end;
@@ -1222,7 +1663,7 @@
12221663 parameters stored from their registers to their slots, or moved to
12231664 their registers. *)
12241665 proc enterBody*(c, line: INT);
1225-var i, loop, r: INT; n: I64;
1666+var i, loop, r: INT; n: I64; p: ^Sym.Obj;
12261667 begin
12271668 Chunk.beginChunk(c);
12281669 codeChunk := c;
@@ -1255,7 +1696,7 @@
12551696 end;
12561697 end;
12571698 for i := 0 to numIn - 1 do
1258- if inObj[i]^.reg # 0 then
1699+ if (inObj[i] # nil) and (inObj[i]^.reg # 0) then
12591700 r := inObj[i]^.reg;
12601701 if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end;
12611702 A.movRR(r, inReg[i]);
@@ -1264,6 +1705,21 @@
12641705 A.store(inReg[i], FP, INT(inSlot[i]), inSize[i]);
12651706 end;
12661707 end;
1708+ (* a register variable's parameter that came on the stack, above the
1709+ frame (val > 0), is loaded from there: one passed by its address
1710+ (ind) is the address, 8 bytes, whatever its type *)
1711+ p := procParams;
1712+ while p # nil do
1713+ if (p^.reg # 0) and (p^.val > 0) then
1714+ if not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) then
1715+ A.load(p^.reg, FP, INT(p^.val), INT(p^.typ^.size), p^.typ^.signed);
1716+ else
1717+ A.load(p^.reg, FP, INT(p^.val), 8, false);
1718+ if not p^.ind and (p^.typ^.form = Sym.fSlice) then A.load(p^.reg2, FP, INT(p^.val) + 8, 8, false); end;
1719+ end;
1720+ end;
1721+ p := p^.next;
1722+ end;
12671723 end;
12681724
12691725 proc resultSlot*: I64;
@@ -1271,27 +1727,32 @@
12711727 return retSlot;
12721728 end;
12731729
1730+(* Pops one of several results, a value of size bytes, into its place
1731+ off bytes into the caller's, at the address that came in x8 *)
12741732 proc resultTo*(off: I64; size: INT);
1733+var r: INT;
12751734 begin
1276- _ := off; _ := size;
1277- notYet("a result in memory");
1735+ r := top();
12781736 drop();
1737+ A.load(U, FP, INT(retSlot), 8, false);
1738+ A.store(r, U, INT(off), size);
12791739 end;
12801740
1741+(* Pops an address, and copies size bytes from it to the result, off
1742+ bytes into it *)
12811743 proc copyResult*(off, size: I64);
12821744 begin
1283- _ := off; _ := size;
1284- notYet("a result in memory");
1285- drop();
1745+ popTo(Src);
1746+ A.load(Dst, FP, INT(retSlot), 8, false);
1747+ if off # 0 then A.aluI(A.opAdd, Dst, Dst, off); end;
1748+ copyBytes(size);
12861749 end;
12871750
12881751 (* return: the result, a value on top, goes in x0; two words, the two
1289- values on top, in x0 and x1 *)
1752+ values on top, in x0 and x1; one in memory is there already *)
12901753 proc returnJump*;
12911754 begin
1292- if (resType # nil) and Sym.behindPointer(resType) then
1293- A.load(0, FP, INT(retSlot), 8, false); (* not yet made: marked by beginProc *)
1294- elsif resType # nil then
1755+ if (resType # nil) and not Sym.behindPointer(resType) then
12951756 if Sym.inTwoRegs(resType) then popTo(1); end;
12961757 popTo(0);
12971758 end;
@@ -1304,10 +1765,35 @@
13041765 begin
13051766 end;
13061767
1768+(* The procedure returns the error in register r, and a zero for each
1769+ of its other results; r isn't x0, x1 or x2, which this uses. *)
1770+proc returnError(r: INT);
1771+var f: ^Sym.Obj;
1772+begin
1773+ if Sym.behindPointer(resType) then
1774+ A.load(Dst, FP, INT(retSlot), 8, false);
1775+ zeroBytes(resType^.size);
1776+ f := resType^.fields;
1777+ while f^.next # nil do f := f^.next; end;
1778+ A.load(Dst, FP, INT(retSlot), 8, false);
1779+ A.store(r, Dst, INT(f^.val), 8);
1780+ elsif Sym.inTwoRegs(resType) then
1781+ A.movRR(1, r);
1782+ A.movRI(0, 0);
1783+ else
1784+ A.movRR(0, r);
1785+ end;
1786+ A.jmp(exitL);
1787+end;
1788+
1789+(* The rest of what try does where the error isn't nil: the procedure
1790+ returns the error in the frame slot. Only code is made: the stack of
1791+ values is left as it is, for the code that follows, where the error
1792+ is nil. *)
13071793 proc tryReturn*(slot: I64);
13081794 begin
1309- _ := slot;
1310- notYet("try");
1795+ A.load(U, FP, INT(slot), 8, false);
1796+ returnError(U);
13111797 end;
13121798
13131799 (* try, with no deferred statement to run: the error on top is tested in
@@ -1323,8 +1809,7 @@
13231809
13241810 proc tryReturnFrom*(r: INT);
13251811 begin
1326- _ := r;
1327- notYet("try");
1812+ returnError(r);
13281813 end;
13291814
13301815 (* The end of the procedure in chunk c: the code that returns, and the
@@ -1335,7 +1820,10 @@
13351820 begin
13361821 codeChunk := -1;
13371822 if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end;
1338- if open then
1823+ if open and Sym.behindPointer(resType) then
1824+ A.load(Dst, FP, INT(retSlot), 8, false);
1825+ zeroBytes(resType^.size);
1826+ elsif open then
13391827 A.movRI(0, 0);
13401828 if Sym.inTwoRegs(resType) then A.movRI(1, 0); end;
13411829 else
@@ -1352,6 +1840,7 @@
13521840 A.movRR(SP, FP);
13531841 A.ldp(FP, LR, SP, 16, A.mPost);
13541842 A.ret();
1843+ endFails();
13551844 n := Sym.alignUp(frameMax, 16);
13561845 if n >= 1000000H then return Scan.mark("frame too big: 16 MB or more"); end;
13571846 Chunk.put4At(frameFix, SubSp12 | INT((n shr 12) shl 10));
@@ -1364,29 +1853,40 @@
13641853 return nil;
13651854 end;
13661855
1856+(* A deferred statement is compiled where it stands, as a subroutine of
1857+ its procedure that the code jumps over, in the procedure's own frame
1858+ (the same x29), so that it sees all its variables; deferBegin gives
1859+ its address and what pushed was. bl, which runs it, leaves its return
1860+ address in x30, which its own calls would change: so it keeps x30 on
1861+ the machine's stack, 16 bytes, so that sp stays a multiple of 16:
1862+
1863+ b over
1864+ at: str x30, [sp, #-16]!
1865+ ... ; the statement
1866+ ldr x30, [sp], #16
1867+ ret
1868+ over: *)
13671869 proc deferBegin*(var L: INT): (INT, INT);
1870+var at: INT;
13681871 begin
1369- _ := L;
1370- notYet("defer");
1371- return Chunk.pc, pushed;
1872+ A.jmp(L);
1873+ at := Chunk.pc;
1874+ A.storeIdx(LR, SP, -16, 8, A.mPre);
1875+ return at, pushed;
13721876 end;
13731877
13741878 proc deferEnd*(L, saved: INT);
13751879 begin
1376- _ := L; _ := saved;
13771880 keepTemps();
1881+ A.loadIdx(LR, SP, 16, 8, A.mPost);
1882+ A.ret();
1883+ A.fixLink(L);
1884+ pushed := saved;
13781885 end;
13791886
13801887 proc runDefer*(at: INT);
13811888 begin
1382- _ := at;
1383- notYet("defer");
1384-end;
1385-
1386-(* The name s, interned *)
1387-proc internStr(s: []const U8): INT;
1388-begin
1389- return Scan.intern(SYSTEM.PTR(s), LEN(s));
1889+ A.callBack(at);
13901890 end;
13911891
13921892 (* The entry point, main, and its chunk. macOS starts every program in
@@ -1428,6 +1928,29 @@
14281928 A.callChunk(exitProc);
14291929 A.brk(1); (* exit never returns: trap if it does *)
14301930 Chunk.endChunk(entry);
1931+ (* Rt.fail, for the failures of each kind, given a, b, len and name
1932+ by the check's code (endFails), which called the stub by bl: the
1933+ place is that bl's, 4 bytes before the return address. Rt.fail's
1934+ last two arguments, the registers x86-64's trap report shows, are
1935+ none here.
1936+
1937+ sub x4, x30, #4
1938+ mov x0, #kind
1939+ mov x6, #0
1940+ mov x7, #0
1941+ bl Rt.fail *)
1942+ for i := 0 to LEN(failStubs) - 1 do
1943+ if failStubs[i] >= 0 then
1944+ Chunk.beginChunk(failStubs[i]);
1945+ A.aluI(A.opSub, 4, LR, 4);
1946+ A.movRI(0, i);
1947+ A.movRI(6, 0);
1948+ A.movRI(7, 0);
1949+ A.callChunk(rtFail);
1950+ A.brk(1);
1951+ Chunk.endChunk(failStubs[i]);
1952+ end;
1953+ end;
14311954 return entry;
14321955 end;
14331956
@@ -1455,7 +1978,9 @@
14551978 notYet("reals");
14561979 end;
14571980
1981+var i: INT;
14581982 begin
1983+ for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end;
14591984 Chunk.setPad(0D4200020H); (* brk #1, a trap, between procedures *)
14601985 codeChunk := -1;
14611986 pending := "";

test/mac.sh (history) modified +40 -0

@@ -6,6 +6,10 @@
66 # compared, as test/check.sh compares a test's:
77 # X.expect its standard output (optional; empty if missing)
88 # X.status its exit status (optional; 0 if missing)
9+# Then conform/run's programs the same way, into build/mac/conform,
10+# with their arguments (X.args): until the port is whole some fail,
11+# which are counted and listed in build/mac/conform.fail, not failures
12+# of the run.
913 #
1014 # test/mac.sh build in the container: the cross compiler, then the programs
1115 # test/mac.sh run on the Mac: each program, run with a timeout
@@ -23,6 +27,12 @@
2327 for f in test/mac/*.raz; do
2428 name=$(basename $f .raz)
2529 build/cross/darwin-arm64/raz -o $out/$name $f 2>$out/$name.cerr || true
30+ done
31+ mkdir -p $out/conform
32+ for f in conform/run/*.raz; do
33+ name=$(basename $f .raz)
34+ build/cross/darwin-arm64/raz -I conform/run/mods -o $out/conform/$name $f \
35+ 2>$out/conform/$name.cerr || true
2636 done
2737 ;;
2838 run)
@@ -48,6 +58,36 @@
4858 fi
4959 done
5060 echo "$pass passed, $fail failed"
61+ # conform/run, counted: why each fails goes to conform.fail
62+ c=$out/conform
63+ cpass=0
64+ cfail=0
65+ : > $c.fail
66+ for f in conform/run/*.raz; do
67+ name=$(basename $f .raz)
68+ want=0
69+ [ -f conform/run/$name.status ] && want=$(cat conform/run/$name.status)
70+ if [ ! -x $c/$name ]; then
71+ echo "$name: doesn't compile: $(grep -m1 error $c/$name.cerr)" >> $c.fail
72+ cfail=$((cfail + 1)); continue
73+ fi
74+ set --
75+ if [ -f conform/run/$name.args ]; then
76+ while IFS= read -r a; do set -- "$@" "$a"; done < conform/run/$name.args
77+ fi
78+ status=0
79+ perl -e 'alarm 10; exec @ARGV' ./$c/$name "$@" > $c/$name.out 2>$c/$name.err || status=$?
80+ if [ $status != $want ]; then
81+ echo "$name: status $status, not $want: $(head -1 $c/$name.err)" >> $c.fail
82+ cfail=$((cfail + 1))
83+ elif ! cmp -s $c/$name.out conform/run/$name.expect; then
84+ echo "$name: output differs" >> $c.fail
85+ cfail=$((cfail + 1))
86+ else
87+ cpass=$((cpass + 1))
88+ fi
89+ done
90+ echo "conform/run: $cpass of $((cpass + cfail)) passed; the others in $c.fail"
5191 [ $fail = 0 ]
5292 ;;
5393 *)

test/mac/Calls.expect (history) added +8 -0

@@ -0,0 +1,8 @@
1+385
2+-178672
3+120
4+1605
5+1926
6+86
7+42
8+42 and 1234567890123 -5

test/mac/Calls.raz (history) added +76 -0

@@ -0,0 +1,76 @@
1+(* Calls past the registers (PLAN.md 5, 5b): arguments past x7 on the
2+ machine's stack, a slice among them; a record and several results
3+ through x8; two results in x0 and x1; a procedure value; C's printf,
4+ whose arguments past its own go on the stack; and copies past 256
5+ bytes. *)
6+module Calls;
7+import Out;
8+import SYSTEM;
9+
10+extern proc printf(fmt: ^U8; ...): I32;
11+extern proc fflush(f: ^U8): I32;
12+
13+type
14+ Big = record a: [40]INT; n: INT; end;
15+
16+proc ten(a, b, c, d, e, f, g, h, i, j: INT): INT;
17+begin
18+ return a + 2 * b + 3 * c + 4 * d + 5 * e + 6 * f + 7 * g + 8 * h + 9 * i + 10 * j;
19+end;
20+
21+(* the slice takes x7 and the stack's first word, or both on the stack *)
22+proc late(a, b, c, d, e, f, g: INT; s: []const U8; n: I8): INT;
23+begin
24+ return a + b + c + d + e + f + g + LEN(s) * 100 + INT(s[1]) * 1000 + INT(n) * 100000;
25+end;
26+
27+proc big(n: INT): Big;
28+var r: Big; i: INT;
29+begin
30+ for i := 0 to 39 do r.a[i] := i * n; end;
31+ r.n := n;
32+ return r;
33+end;
34+
35+proc three(x: INT): (INT, INT, INT);
36+begin
37+ return x, x * 2, x * 3;
38+end;
39+
40+proc passOn(x: INT): (INT, INT, INT);
41+begin
42+ return three(x + 1);
43+end;
44+
45+proc two(x: INT): (INT, INT);
46+begin
47+ return x + 1, x - 1;
48+end;
49+
50+proc twice(x: INT): INT;
51+begin
52+ return 2 * x;
53+end;
54+
55+proc main*;
56+var b, c: Big; p, q, r: INT; f: proc(x: INT): INT;
57+begin
58+ Out.int(ten(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); Out.ln();
59+ Out.int(late(1, 2, 3, 4, 5, 6, 7, "xyz", -3)); Out.ln();
60+ b := big(3);
61+ c := b;
62+ Out.int(c.a[39] + c.n); Out.ln();
63+ p, q, r := three(5);
64+ Out.int(p + q * 10 + r * 100); Out.ln();
65+ p, q, r := passOn(5);
66+ Out.int(p + q * 10 + r * 100); Out.ln();
67+ p, q := two(7);
68+ Out.int(p * 10 + q); Out.ln();
69+ f := twice;
70+ Out.int(f(21)); Out.ln();
71+ Out.flush();
72+ _ := printf("%d %s %ld %d\n", 42, "and", 1234567890123, -5);
73+ _ := fflush(nil);
74+end;
75+
76+end Calls.

test/mac/DivNarrow.expect (history) added +1 -0

@@ -0,0 +1 @@
1+before

test/mac/DivNarrow.raz (history) added +16 -0

@@ -0,0 +1,16 @@
1+(* The smallest I16 divided by -1 stops the program, as an I64's does:
2+ arm64's sdiv wouldn't, so the code checks *)
3+module DivNarrow;
4+import Out;
5+
6+proc main*;
7+var a, b: I16;
8+begin
9+ a := -32767 - 1;
10+ b := -1;
11+ Out.str("before\n");
12+ Out.int(I64(a div b));
13+ Out.ln();
14+end;
15+
16+end DivNarrow.

test/mac/DivNarrow.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/mac/DivZero.expect (history) added +1 -0

@@ -0,0 +1 @@
1+before

test/mac/DivZero.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+(* A U8 divided by zero, in mod, stops the program *)
2+module DivZero;
3+import Out;
4+
5+proc main*;
6+var a, b: U8;
7+begin
8+ a := 7;
9+ Out.str("before\n");
10+ Out.int(I64(a mod b));
11+ Out.ln();
12+end;
13+
14+end DivZero.

test/mac/DivZero.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/mac/Divide.expect (history) added +9 -0

@@ -0,0 +1,9 @@
1+-3 -2 -4 -1 17
2+-1073741823 -1 -2147483
3+16383 -1
4+-42 -1 127
5+35 5 15 10
6+1333333333 1
7+many one two three or four three or four five many seven many
8+[halve]5
9+[halve]odd

test/mac/Divide.raz (history) added +61 -0

@@ -0,0 +1,61 @@
1+(* Division and mod in every width, signed and unsigned, by constants
2+ and variables, and a jump table, deferred statements and try *)
3+module Divide;
4+import Out;
5+
6+const Odd: ERROR = "odd";
7+
8+proc show(n: I64);
9+begin
10+ Out.int(n); Out.str(" ");
11+end;
12+
13+proc even(n: INT): ERROR;
14+begin
15+ if n mod 2 # 0 then return Odd; end;
16+ return nil;
17+end;
18+
19+proc halve(n: INT): (INT, ERROR);
20+begin
21+ defer Out.str("[halve]");
22+ try even(n);
23+ return n div 2, nil;
24+end;
25+
26+proc name(n: INT): []const U8;
27+begin
28+ match n of
29+ | 1: return "one";
30+ | 2: return "two";
31+ | 3, 4: return "three or four";
32+ | 5: return "five";
33+ | 7: return "seven";
34+ | else: return "many";
35+ end;
36+end;
37+
38+proc main*;
39+var a, b: I64; c, d: I32; e, f: I16; g, h: I8; u, v: U8; w, x: U32; m, k: INT; err: ERROR;
40+begin
41+ a := -17; b := 5;
42+ show(a div b); show(a mod b); show(a div 4); show(a mod 4); show(a div (-1)); Out.ln();
43+ c := -2147483647; d := 2;
44+ show(I64(c div d)); show(I64(c mod d)); show(I64(c div 1000)); Out.ln();
45+ e := -32767; f := -2;
46+ show(I64(e div f)); show(I64(e mod f)); Out.ln();
47+ g := -127; h := 3;
48+ show(I64(g div h)); show(I64(g mod h)); show(I64(g div (-1))); Out.ln();
49+ u := 250; v := 7;
50+ show(I64(u div v)); show(I64(u mod v)); show(I64(u div 16)); show(I64(u mod 16)); Out.ln();
51+ w := 4000000000; x := 3;
52+ show(I64(w div x)); show(I64(w mod x)); Out.ln();
53+ for m := 0 to 8 do Out.str(name(m)); Out.str(" "); end;
54+ Out.ln();
55+ k, err := halve(10); show(k); Out.ln();
56+ k, err := halve(7);
57+ if err = Odd then Out.str("odd"); end;
58+ Out.ln();
59+end;
60+
61+end Divide.