raz / commit e99cab9b

commit
e99cab9b8685d604c3338e9f31339d8b2af63d18
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6d4487fb
Gen.arm64: reals, FILL, bit counts and rotations; nothing left not yet

PLAN.md 5, step 5c, the last of step 5. Reals in v16-v21 on the stack
and d8-d15 for variables, saved as Apple's convention asks; a real to
an integer mends fcvtzs's saturation to the report's smallest I64.
FILL, NEW's zeroing, CLZ, CTZ, POPCOUNT (cnt and addv, added to A64),
ROL and ROR at every width; a C procedure's arguments past x7 packed by
size. All of conform/run passes on the Mac (110, and SystemImport not
for this target), and test/mac's 12. x86-64's output is unchanged for
202 programs; the fixpoint holds.

patch · log

16 files changed, +884 -230

PLAN.md (history) modified +20 -1

@@ -227,7 +227,7 @@
227227 `fxPageOff12`, and `Scan.setBang` for a pre-index's `!`. What it
228228 left for later, `Link`'s patching of those kinds, `Gen.arm64.raz`
229229 importing `A64`, and padding with `brk`, came with 5a.
230-5. `Gen.arm64.raz`, in pieces, each run on the Mac (owner, 2026-11-15):
230+5. Done. `Gen.arm64.raz`, in pieces, each run on the Mac (owner, 2026-11-15):
231231 first a bare `Exe.darwin.raz` (a signed Mach-O over libSystem, no line
232232 numbers) and an `Rt` that only exits, built by the staged cross
233233 compiler in the container; then values and operations; jumps and
@@ -264,6 +264,25 @@
264264 `CLZ`, `ROL`, or `SYSCALL` (`SystemImport`, Linux's only). Left of
265265 step 5 (5c): the reals, `FILL`, `NEW`'s zeroing, the counts of bits
266266 and the rotations.
267+
268+ 5c, done: the reals, `F32` and `F64`, on the stack in v16 to v21 and
269+ as variables in d8 to d15, which the prologue saves; their
270+ operations, comparisons (a NaN equal to nothing), conversions both
271+ ways and between widths, a real out of an `I64`'s range or a NaN
272+ mended to the smallest `I64` after `fcvtzs`, `ABS` and `-`; real
273+ arguments in v0 to v7 and on the stack, results in d0; a code
274+ procedure that is `fsqrt` made in place of its call. `FILL`,
275+ `NEW`'s zeroing, `CLZ`, `CTZ`, `POPCOUNT` (by `cnt` and `addv`),
276+ `ROL` and `ROR` at every width. A C procedure's own arguments past
277+ x7 packed by their size, as Apple's convention wants. Nothing of
278+ `Gen.arm64.raz` is "not yet"; only `SYSCALL` is refused, so
279+ `test/mac.sh` skips conform/run's `SystemImport`, with the reason.
280+ conform/run passes on the Mac, 110 of 110; `test/mac` has the reals'
281+ edges (`Reals`), rotations and counts of every width (`Rotate`) and
282+ `fsqrt` in place (`Sqrt`). Left for later steps: `Math`'s arm64
283+ twin, so that `Math.sqrt` is made in place (step 7). A rotation of a
284+ negative `I8` or `I16` leaves the bits above its width zero, where
285+ x86-64's `rol al, cl` leaves them as they were.
267286 6. `Exe.darwin.raz`'s line numbers for lldb (its rebases came with
268287 5a); `Host.darwin.raz`.
269288 7. The library: `CAbi`, `Sys`, `Rt`, `Thread`, `Bytes`, `Math`, `Str`.

doc/compiler.md (history) modified +134 -43

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is fifteen files of raz, about 16,400 lines of code (the
3+The compiler is fifteen files of raz, about 16,500 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
@@ -23,11 +23,11 @@
2323 | `src/Sym.raz` | 866 | 689 | Types, objects, scopes, layout |
2424 | `src/Chunk.raz` | 410 | 302 | The compiled program: chunks, fixups, line notes |
2525 | `src/X64.raz` | 1,771 | 1,450 | The instruction encoder, jumps within a procedure, the assembler |
26-| `src/A64.raz` | 1,843 | 1,365 | The same for arm64: the encoder, jumps, the assembler |
27-| `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,993 | 1,398 | The same for arm64, begun: frames, values, operations, checks, calls |
26+| `src/A64.raz` | 1,860 | 1,375 | The same for arm64: the encoder, jumps, the assembler |
27+| `src/Gen.x86_64.raz` | 2,863 | 2,001 | The stack machine, for x86-64: values, operations, checks, calls, frames |
28+| `src/Gen.arm64.raz` | 2,295 | 1,573 | The same for arm64: values, operations, reals, checks, calls, frames |
2929 | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls |
30-| `src/Parse.raz` | 4,948 | 4,198 | Declarations, statements, expressions, built-ins, recovery, warnings |
30+| `src/Parse.raz` | 4,947 | 4,197 | Declarations, statements, expressions, built-ins, recovery, warnings |
3131 | `src/Link.raz` | 481 | 365 | Tree shaking, layout, patching, the places |
3232 | `src/Exe.linux.raz` | 455 | 358 | The executable, ELF: static and dynamic, symbol table, line numbers |
3333 | `src/Exe.darwin.raz` | 458 | 332 | The executable for macOS, Mach-O: dyld's binds and rebases, the signature |
@@ -1313,7 +1313,8 @@
13131313 size, register file and operation: a scaled offset, else unscaled, else
13141314 through x16), `ldp`, `stp`; the LSE atomics `ldaddal`, `swpal` and
13151315 `casal`, and `dmb`, `svc`, `brk`, `align`; for reals `fop`, `fop1`,
1316-`fcvt`, `fcmp`, `scvtf`, `fcvtz`, `fmovFR` and `fmovRF`. Two things
1316+`fcvt`, `fcmp`, `scvtf`, `fcvtz`, `fmovFR` and `fmovRF`; and two vector
1317+instructions, `cnt` and `addv`, for `POPCOUNT`. Two things
13171318 x86 does and arm64 doesn't are Gen's to mend, as their comments say:
13181319 `sdiv` by zero gives 0, and `fcvtzs` saturates where x86 gives the
13191320 smallest `I64`.
@@ -1379,7 +1380,7 @@
13791380 each word in hex and each fixup; `expect.sh`, on an arm64 Mac, makes
13801381 `Encode.expect` from `Asm.txt` in Apple's syntax and `Api.s`, the
13811382 calls' instructions a line each, with `cc -c -arch arm64` and `objdump`:
1382-428 words and 20 relocations, the farthest jumps among them, across a
1383+430 words and 20 relocations, the farthest jumps among them, across a
13831384 megabyte of `nop`.
13841385
13851386 ## Gen.raz: the stack machine
@@ -1712,36 +1713,25 @@
17121713
17131714 ## Gen.arm64.raz: the stack machine on arm64
17141715
1715-arm64's `Gen`, for macOS on Apple Silicon (`PLAN.md` 5), is begun. It
1716-answers every call `Gen.x86_64.raz` answers, with the same parameters,
1717-as the interface check holds the two files to (report, 3), and makes
1718-frames, constants, the values of variables and their stores, the
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"`.
1716+arm64's `Gen`, for macOS on Apple Silicon (`PLAN.md` 5). It answers
1717+every call `Gen.x86_64.raz` answers, with the same parameters, as the
1718+interface check holds the two files to (report, 3), and makes all that
1719+x86-64's makes, but `SYSTEM.SYSCALL`, which the report gives only on
1720+Linux (6.8). `Gen.Arch` is `"arm64"`. It was made in three steps, 5a
1721+to 5c, each run on the Mac; conform/run passes there whole, but for
1722+`SystemImport`.
17261723
1727-*Not yet.* What it can't make yet isn't refused at once, since a
1728-library module holds much that a program never calls: the C library's
1729-`Sys` is compiled whole for the Mac, as the interface check of `Rt`
1730-reads `Rt.raz`, which imports it. So a routine not yet made calls
1731-`notYet`, which notes on the procedure's chunk what it is and where,
1732-the first only (`Chunk.setMissing`); keeps the stack as the real
1733-routine would, so that the compile reads on; and makes what code it
1734-likes, never run. Once `Link.markLive` has marked what the program
1735-reaches, `Raz` makes each live chunk so noted an error at its place:
1724+*What it won't make.* `SYSCALL` isn't refused at once, since a library
1725+module may hold what a program never calls. `missing` notes on the
1726+procedure's chunk what it is and where (`Chunk.setMissing`), and keeps
1727+the stack as the real routine would, so that the compile reads on.
1728+Once `Link.markLive` has marked what the program reaches, `Raz` makes
1729+each live chunk so noted an error at its place:
17361730
1737- D.raz:8:19: error: not yet on arm64: div and mod
1731+ S.raz:21:25: error: SYSTEM.SYSCALL is Linux's only: call the C library
17381732
1739-A program that uses what isn't made fails to compile, and one that
1740-doesn't compiles. `beginProc` comes before its chunk is begun, so its
1741-notes wait for `enterBody` (`pending`). A routine not yet made reads
1742-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.
1733+Until 5c every routine not yet made was noted so, which let the port
1734+run its first programs before the whole of `Gen` was there.
17451735
17461736 *The stack* is x86-64's, kept as plainly as it can be: the value at
17471737 depth d is in `Regs[d mod 6]`, x9 to x14, which a call may change and
@@ -1757,6 +1747,67 @@
17571747 between calls; x8 is kept for the address of a result in memory, as
17581748 Apple's convention has it, and x18, the system's, is never touched.
17591749
1750+*Reals* are held as x86-64 holds them, in the registers that compute
1751+with reals: the value at depth d in `XRegs[d mod 6]`, v16 to v21, as
1752+d or s, so that each depth has two registers, one of each file, and
1753+`owner` doesn't tell them apart (`loaded`, `xTarget`). Like x9 to x14
1754+they are a call's to change and carry no argument, so `call` moves a
1755+real into v0 to v7 in any order; v23 is Gen's scratch (`XT`). A spilled
1756+real takes 8 bytes, an F32 with what lies above it. arm64 has no
1757+instruction that holds any real, and `fmov`'s immediate holds few, so a
1758+constant is made from its bits in x15 and moved across, which for most
1759+constants of a program, 0.25 or 2.0, is one `movz`; 0.0 comes from the
1760+zero register (`xValueTo`):
1761+
1762+```
1763+movz x15, #16368, lsl #48 ; 1.0
1764+fmov d17, x15
1765+fadd d16, d16, d17
1766+```
1767+
1768+The operations are one instruction each, on two registers (`realOp`),
1769+the right operand loaded first. `neg` and `ABS` are `fneg` and
1770+`fabs`, which flip or clear the sign bit and nothing else, a NaN's
1771+too, as x86-64's `btc` and `btr` do. A comparison is `fcmp` and a
1772+`cset` of the left's integer register, by `CcReal`: an unordered pair,
1773+a NaN on either side, sets C and V and not N or Z, so `=` is `eq` and
1774+`#` is `ne`, as IEEE wants, but `<` and `<=` can't be `lt` and `le`,
1775+which test N # V and hold for a NaN; they are `mi` and `ls`. `>` and
1776+`>=`, `gt` and `ge`, don't hold for one. An integer becomes a real by
1777+`scvtf` of its 64 bits, or `ucvtf` for a `U64`, which rounds as
1778+x86-64's halving and doubling does; a real becomes the other by `fcvt`.
1779+A real becomes an integer by `fcvtzs`, toward zero, as the report's
1780+table has it; but where x86-64's `cvttsd2si` gives the smallest `I64`
1781+for a NaN or a real out of range, `fcvtzs` gives 0 for a NaN and the
1782+nearest `I64` beyond, so `realToInt` mends it. A NaN is unordered with
1783+itself, which sets V; the largest `I64`, which no real converts to
1784+exactly, is the one value 1 overflows, which sets V too; too small is
1785+the smallest already:
1786+
1787+```
1788+fcvtzs x9, d16
1789+mov x15, #8000000000000000H
1790+fcmp d16, d16
1791+csel x9, x15, x9, vs ; a NaN
1792+cmn x9, #1
1793+csel x9, x15, x9, vs ; too big
1794+```
1795+
1796+then cut to a narrow type's width as on x86-64, so `I32(nan)` is 0.
1797+
1798+A real argument goes in the next of v0 to v7, the ninth on the stack
1799+as an integer past x7 does, and a real result comes back in d0
1800+(`call`, `beginProc`, `returnJump`). `FILL` stores a real's bits as an
1801+integer's. A real variable takes one of d8 to d15 (`XVarRegs`), whose
1802+low 64 bits Apple's convention has a called procedure keep: so, unlike
1803+x86-64, where no xmm register outlives a call and each real variable
1804+is loaded again after one, the prologue saves those it uses, beside
1805+x19 to x23, the epilogue restores them (`saveRegs`), and a call leaves
1806+them be. `popVar` still writes each through to its slot (`homes`,
1807+`xHome`), as x86-64's does, where a trap report finds it. A code
1808+procedure that is `fsqrt d0, d0` and `ret`, as `Math.sqrt` will be, is
1809+made in place of its call (`oneRealOp`, `realInPlace`), as on x86-64.
1810+
17601811 A place in memory is a `Mem`: a register and an offset, or a chunk and
17611812 an offset, which A64 reaches by `adrp` and the place in the page
17621813 (`loadChunk`, `storeChunk`), since macOS loads a program anywhere.
@@ -1786,9 +1837,9 @@
17861837 a body, so sp stays a multiple of 16 and `pushed` is always 0. The
17871838 busiest variables take x19 to x23 (`VarRegs`), which a callee gives
17881839 back; no parameter is kept where it arrives (`keepArrived` does
1789-nothing); the reals' variables will take d8 to d15 (`XVarRegs`,
1790-numbered from `XmmVar`, 32, above every x register, which `Parse` tells
1791-apart by it).
1840+nothing); the reals' variables take d8 to d15 (`XVarRegs`, numbered
1841+from `XmmVar`, 32, above every x register, which `Parse` tells apart by
1842+it).
17921843
17931844 *Calls* move each argument into x0 to x7, a slice's two words into two
17941845 of them or both onto the stack. The arguments past the registers go on
@@ -1797,9 +1848,11 @@
17971848 the callee finds them above its frame record, at x29 + 16 on, as x86's
17981849 finds them above rbp. A C procedure that ends in `...` takes what comes
17991850 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,
1851+Apple's convention has it. On the stack each argument takes 8 bytes,
1852+but a C procedure's own parameter of an integer, `BOOL`, enumeration,
1853+set or real takes its size, at a multiple of it, as Apple packs them:
1854+an `I32` and an `I16` past x7 go at sp and sp + 4. A single result
1855+comes back in x0, or d0, two words in x0 and x1, and a narrow result of C's is extended,
18031856 as C leaves the upper bits as they fall; a result in memory, a record
18041857 or several results, goes to the caller's temporary, whose address the
18051858 caller puts in x8 and the callee keeps in a slot (`retSlot`) for
@@ -1833,8 +1886,44 @@
18331886 after a `br`, found by `adr`; the atomics by the LSE instructions,
18341887 `casal`, `swpal` and `ldaddal`; `COPY` (`copySlice`) by words, then
18351888 bytes, back from the ends when the destination starts inside the
1836-source; `ABS` by `cneg`.
1889+source; `ABS` by `cneg`; `FILL` (`fillSlice`) by a store each into an
1890+array of 16 elements or fewer, else a loop of stores, and a record or
1891+array element copied into `d[0]` and from there forward, by words for
1892+one of 8 bytes or more, each read after it is written; `NEW`'s zeroing
1893+by `zeroBytes`. `CLZ` is `clz`, of 32 or 64 bits, less 24 or 16 for a
1894+`U8` or a `U16`; `CTZ` is `clz` of the bits reversed, `rbit`, a one bit
1895+set just above a narrow value first, so that 0 counts its width.
1896+`POPCOUNT` is plainer than x86-64's sums of bits: every arm64 counts
1897+the ones of each byte of a vector register, `cnt`, and adds the bytes,
1898+`addv`, as C's compilers do:
18371899
1900+```
1901+fmov d23, x9
1902+cnt v23.8b, v23.8b
1903+addv b23, v23.8b
1904+fmov x9, d23
1905+```
1906+
1907+A rotation is `ror`, which arm64 has only to the right and only of 32
1908+or 64 bits: `ROL` by n is `ror` by -n, which `ror` takes modulo the
1909+width as x86's `rol` does, and a `U8` or a `U16` is first repeated
1910+through 32 bits, so that what leaves its low end comes back at its top
1911+and a rotation of 32 bits by any count rotates each copy alike, the
1912+count modulo 8 or 16 too; the result is cut back to the width:
1913+
1914+```
1915+and x9, x9, #0FFH ; ROL(x, n) of a U8: 000000AB
1916+orr x9, x9, x9, lsl #8 ; to ABABABAB
1917+orr x9, x9, x9, lsl #16
1918+neg x17, x10
1919+ror w9, w9, w17
1920+and x9, x9, #0FFH
1921+```
1922+
1923+x86-64's `rol al, cl` leaves the bits above a narrow value as they
1924+were, here they are zero; no program sees the difference, as those bits
1925+aren't the value's and `ROL` takes only an unsigned type.
1926+
18381927 *The entry point* (`entryPoint`) is the C `main` that dyld calls: it
18391928 pushes a frame record, which links to dyld's, saves argc, an `int` it
18401929 extends to 64 bits, and argv, gives `Rt` C's `exit` in `cExit` and the
@@ -2613,8 +2702,10 @@
26132702 compiler for the Mac this way, in the linux/amd64 container, and `make
26142703 mac-check` (`test/mac.sh`), on an Apple Silicon Mac, has it compile
26152704 `test/mac`'s programs there and runs each here, natively, against its
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:
2705+`X.expect` and `X.status`; then conform/run's, which are counted, those
2706+that fail listed in `build/mac/conform.fail`, and those not for the Mac,
2707+named in the script with why (`SystemImport`, as `SYSCALL` is Linux's),
2708+skipped. The host changes nothing of the target:
26182709 a program for another system still needs that system's `Sys` and `Rt`,
26192710 and for a kernel the options `-bare`, `-s` and `-base`.
26202711

src/A64.raz (history) modified +18 -1

@@ -576,7 +576,8 @@
576576 var opc: INT; s, r: []const U8;
577577 begin
578578 opc := (bits shr 22) & 3;
579- if (opc & 1) = 0 then s := "str"; else s := "ldr"; end;
579+ (* opc 0 stores, and 2 a real's q; else it loads, 2 an ldrsw *)
580+ if (opc = 0) or (opc = 2) and ((bits & 04000000H) # 0) then s := "str"; else s := "ldr"; end;
580581 if unscaled then s := Diag.say("{}ur", [Fmt.str(s[0..2])]); end;
581582 if (bits & 04000000H) # 0 then
582583 r := vreg(rt, sizeOf(bits));
@@ -838,6 +839,22 @@
838839 proc fmovRF*(rd, vn: INT; single: BOOL);
839840 begin
840841 fconv("fmov", bit(not single), 8 - 4 * bit(single), 0, 6, rd, vn, false);
842+end;
843+
844+(* Two of the vector instructions, on the low 8 bytes of a register,
845+ for POPCOUNT: cnt vd.8b, vn.8b, the ones in each byte, and addv bd,
846+ vn.8b, the eight bytes added: 0 0 0 01110 00 10000 00101 10 Rn Rd,
847+ and 0 0 0 01110 00 11000 11011 10 Rn Rd *)
848+proc cnt*(vd, vn: INT);
849+begin
850+ if listing then say(Diag.say("cnt v{}.8b, v{}.8b", [Fmt.int(vd), Fmt.int(vn)])); end;
851+ emit(0E205800H | (vn shl 5) | vd);
852+end;
853+
854+proc addv*(vd, vn: INT);
855+begin
856+ if listing then say(Diag.say("addv b{}, v{}.8b", [Fmt.int(vd), Fmt.int(vn)])); end;
857+ emit(0E31B800H | (vn shl 5) | vd);
841858 end;
842859
843860 (* Addresses, and references to other chunks *)

src/Gen.arm64.raz (history) modified +468 -166

@@ -4,16 +4,9 @@
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. 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
15- uses it fails to compile rather than run wrong; PLAN.md 5 says the
16- pieces still to come.
7+ them. It makes all that x86-64's does, in arm64's instructions, but
8+ SYSCALL, which is Linux's only: a program that uses it fails to
9+ compile (missing).
1710
1811 The stack is kept in registers as on x86-64: the value at depth d,
1912 depth 0 being the bottom, is in x9 to x14, register d mod 6, all of
@@ -25,29 +18,39 @@
2518 loaded when it is pushed: no instruction of arm64 but a load or a
2619 store takes memory.
2720
21+ A real, an F32 or an F64, is held as x86-64 holds it, in the
22+ registers that compute with reals: the value at depth d in v16 to
23+ v21, XRegs[d mod 6], d or s, so each depth has two registers, and
24+ owner doesn't tell them apart. They are a call's to change and carry
25+ no argument, as x9 to x14 are; v23 is Gen's scratch. A real constant
26+ is made in x15 and moved across (fmov), as arm64 has no instruction
27+ that holds any real.
28+
2829 Registers outside the stack: x15 and x17, Gen's scratch, as rax and
2930 rcx are on x86-64; x16, the encoder's (A64); x0 to x3 for copying
3031 and slices, between calls, where they hold nothing; x8, which Apple's convention
3132 keeps for the address of a result in memory; x18, the system's,
3233 never touched; x19 to x23, a procedure's busiest variables (VarRegs),
33- which a called procedure gives back as it found them; x29, the frame
34- pointer, and x30, the return address.
34+ and d8 to d15, its busiest reals (XVarRegs), which a called procedure
35+ gives back as it found them; x29, the frame pointer, and x30, the
36+ return address.
3537
3638 Calls use Apple's arm64 convention: the first eight integer arguments
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
39+ in x0 to x7, the first eight reals in v0 to v7, the rest on the
40+ machine's stack, results in x0 and x1, or d0, or in memory at the
41+ address in x8, sp a multiple of 16 always, a
3942 frame record of x29 and x30 that x29 points at, so that the chain of
4043 frames can be followed. A frame is made whole by the prologue, so
4144 nothing is pushed in a body but a call's stack arguments, for the
4245 call, and a deferred statement's return address: pushed is always 0.
4346
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).
47+ What x86-64 does by itself, and arm64 doesn't, the code does: it
48+ checks a division by zero, and the smallest value by -1 (divide),
49+ and gives the smallest I64 for a real out of range (realToInt).
4650
4751 Depends on: Scan and Diag for errors, Sym for types, Chunk for code
4852 and data, A64 for instructions.
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. *)
53+ Leaves out SYSCALL, which is Linux's only. *)
5154 module Gen;
5255 import Fmt, Diag, Scan, Sym, Chunk, A := A64;
5356 import SYSTEM;
@@ -66,6 +69,7 @@
6669 FP = A.FP; LR = A.LR; SP = A.SP; ZR = A.ZR;
6770 T = 15; U = 17; (* Gen's scratch *)
6871 Dst = 0; Src = 1; Tmp = 2; (* copying's *)
72+ XT = 23; (* Gen's scratch for a real *)
6973
7074 type
7175 Kind* = (kWord, kLong, kF32, kF64);
@@ -160,9 +164,14 @@
160164 StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"];
161165 NumRegs = 6;
162166 Regs: [NumRegs]INT = [9, 10, 11, 12, 13, 14];
167+ XRegs: [NumRegs]INT = [16, 17, 18, 19, 20, 21]; (* v16 to v21 *)
163168 (* the condition codes of the comparisons, from sEql: = # < <= > >= *)
164169 CcSigned: [6]INT = [A.ccEQ, A.ccNE, A.ccLT, A.ccLE, A.ccGT, A.ccGE];
165170 CcUnsigned: [6]INT = [A.ccEQ, A.ccNE, A.ccLO, A.ccLS, A.ccHI, A.ccHS];
171+ (* and of reals, after fcmp: an unordered pair, a NaN, sets C and V
172+ and not N or Z, so that only # holds, as IEEE wants; lt and le
173+ would hold, as they test N # V, so < is mi and <= ls *)
174+ CcReal: [6]INT = [A.ccEQ, A.ccNE, A.ccMI, A.ccLS, A.ccGT, A.ccGE];
166175 (* sub sp, sp, #imm12 and the same shifted by 12: the prologue's two,
167176 whose immediates endProc patches *)
168177 SubSp = 0D10003FFH; SubSp12 = 0D14003FFH;
@@ -177,48 +186,34 @@
177186 retSlot: I64;
178187 resType: ^Sym.Type; (* the procedure's result, or nil *)
179188 exitL: INT;
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;
189+ (* parameters arriving in registers, x0 to x7 or, told by a negative
190+ number, -1 - n, v0 to v7, and a result's address in x8: where each
191+ is stored on entry *)
192+ inReg: [17]INT;
193+ inSlot: [17]I64;
194+ inSize: [17]INT;
195+ inObj: [17]^Sym.Obj;
186196 numIn: INT;
187197 procParams: ^Sym.Obj; (* the parameters, for enterBody *)
188198 regMask: INT; (* the registers of variables, as bits (XBit) *)
189199 saveAt: I64; (* where they are saved, a quadword each *)
200+ homes: [8]I64; (* the slots of the variables in XVarRegs *)
190201 frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *)
191- pending: []const U8; (* what beginProc found not yet made, for enterBody's chunk *)
192- pendingAt: Diag.Pos;
193202 (* the stubs that a failed check calls, one for each kind of failure,
194203 made by entryPoint for those used; and the procedure's checks *)
195204 failStubs: [8]INT;
196205 fails: [4096]Fail;
197206 numFails: INT;
198207
199-(* What this Gen can't make yet marks the procedure being compiled
208+(* What this Gen won't make, SYSCALL, marks the procedure being compiled
200209 (Chunk.setMissing), which is an error if the program uses it, and the
201210 compile reads on: a module the program imports may hold what it never
202- calls, as Sys does. So each routine not yet made keeps the stack as
203- the real one would, and makes what code it likes, never run; it
204- reads into _ the parameters it doesn't use yet, so that none is
205- warned of. What this Gen will never make, SYSCALL, is marked so too,
206- with its own message. *)
211+ calls. The routine keeps the stack as the real one would. *)
207212 proc missing(msg: []const U8);
208213 begin
209- if codeChunk >= 0 then
210- Chunk.setMissing(codeChunk, msg, Scan.tokPos());
211- elsif LEN(pending) = 0 then (* beginProc's, before enterBody names the chunk *)
212- pending := msg;
213- pendingAt := Scan.tokPos();
214- end;
214+ if codeChunk >= 0 then Chunk.setMissing(codeChunk, msg, Scan.tokPos()); end;
215215 end;
216216
217-proc notYet(what: []const U8);
218-begin
219- missing(Diag.say("not yet on arm64: {}", [Fmt.str(what)]));
220-end;
221-
222217 (* -S: the text of each instruction is kept, and list prints it *)
223218 proc setListing*(yes: BOOL);
224219 begin
@@ -258,13 +253,14 @@
258253 proc allocPerm*(size, align: I64): I64; forward;
259254 proc loaded(i: INT): INT; forward;
260255
261-(* The value i, in its register, goes to a frame slot *)
256+(* The value i, in its register, goes to a frame slot: a real as 8
257+ bytes, an F32 with what lies above it *)
262258 proc spill(i: INT);
263-var s: I64;
259+var s: I64; r: INT;
264260 begin
265- _ := loaded(i);
261+ r := loaded(i);
266262 s := allocTemp(8, 8);
267- A.store(Regs[i mod NumRegs], FP, INT(s), 8);
263+ if isReal(i) then A.storeF(r, FP, INT(s), 8); else A.store(r, FP, INT(s), 8); end;
268264 stack[i].loc := lFrame;
269265 stack[i].val := s;
270266 owner[i mod NumRegs] := -1;
@@ -292,7 +288,7 @@
292288 end;
293289
294290 (* The value i into register r, from wherever it is; it stays where it
295- was, so r is a new copy *)
291+ was, so r is a new copy. A real goes as its bits. *)
296292 proc valueTo(i, r: INT);
297293 begin
298294 match stack[i].loc of
@@ -300,20 +296,36 @@
300296 | lAdr:
301297 if stack[i].chunk >= 0 then A.addrChunk(r, stack[i].chunk, INT(stack[i].val));
302298 else A.aluI(A.opAdd, r, FP, stack[i].val); end;
303- | lReg: if Regs[i mod NumRegs] # r then A.movRR(r, Regs[i mod NumRegs]); end;
299+ | lReg:
300+ if isReal(i) then A.fmovRF(r, XRegs[i mod NumRegs], false);
301+ elsif Regs[i mod NumRegs] # r then A.movRR(r, Regs[i mod NumRegs]); end;
304302 | lFrame: A.load(r, FP, INT(stack[i].val), 8, false);
305303 end;
306304 end;
307305
308-(* The value i, in its register *)
306+(* The real i into register v, as valueTo does for an integer: a
307+ constant by its bits, made in x15, or the zero register's *)
308+proc xValueTo(i, v: INT);
309+begin
310+ match stack[i].loc of
311+ | lConst:
312+ if stack[i].val = 0 then A.fmovFR(v, ZR, false);
313+ else A.movRI(T, stack[i].val); A.fmovFR(v, T, false); end;
314+ | lReg: if XRegs[i mod NumRegs] # v then A.fop1(A.fMov, v, XRegs[i mod NumRegs], false); end;
315+ | else: A.loadF(v, FP, INT(stack[i].val), 8); (* lFrame *)
316+ end;
317+end;
318+
319+(* The value i, in its register, of its kind *)
309320 proc loaded(i: INT): INT;
310321 var r: INT;
311322 begin
312323 if stack[i].loc # lReg then
313324 r := claim(i);
314- valueTo(i, r);
325+ if isReal(i) then xValueTo(i, XRegs[i mod NumRegs]); else valueTo(i, r); end;
315326 stack[i].loc := lReg;
316327 end;
328+ if isReal(i) then return XRegs[i mod NumRegs]; end;
317329 return Regs[i mod NumRegs];
318330 end;
319331
@@ -339,12 +351,18 @@
339351 INC(depth);
340352 end;
341353
342-(* The register a new value on top is computed in *)
354+(* The register a new value on top is computed in, and a real's *)
343355 proc target: INT;
344356 begin
345357 return claim(depth);
346358 end;
347359
360+proc xTarget: INT;
361+begin
362+ _ := claim(depth);
363+ return XRegs[depth mod NumRegs];
364+end;
365+
348366 (* The value just computed in target goes on top *)
349367 proc computed(k: Kind);
350368 begin
@@ -378,9 +396,10 @@
378396 for r := 0 to NumRegs - 1 do owner[r] := -1; end;
379397 end;
380398
399+(* A constant; a real's v is its bits, an F64's or, in the low 32, an
400+ F32's *)
381401 proc pushConst*(k: Kind; v: I64);
382402 begin
383- if k in {kF32, kF64} then notYet("reals"); end;
384403 newEntry(k, lConst, v);
385404 end;
386405
@@ -435,6 +454,15 @@
435454 else A.store(r, m.base, INT(m.off), size); end;
436455 end;
437456
457+(* A real's register v loaded from m, or stored there if not load *)
458+proc realMem(v: INT; m: Mem; size: INT; load: BOOL);
459+begin
460+ if (m.chunk >= 0) and load then A.loadChunkF(v, m.chunk, INT(m.off), size);
461+ elsif m.chunk >= 0 then A.storeChunkF(v, m.chunk, INT(m.off), size);
462+ elsif load then A.loadF(v, m.base, INT(m.off), size);
463+ else A.storeF(v, m.base, INT(m.off), size); end;
464+end;
465+
438466 (* Register r := the address m *)
439467 proc adrTo(r: INT; m: Mem);
440468 begin
@@ -465,21 +493,21 @@
465493 end;
466494
467495 proc pushVar*(a: Adr; size: INT; signed: BOOL; k: Kind);
468-var m: Mem; r: INT;
496+var m: Mem; r: INT; fl: BOOL;
469497 begin
470- if k in {kF32, kF64} then notYet("reals"); end;
471- if a.base = bReg then
472- r := target();
473- A.movRR(r, regAt(a));
474- computed(k);
475- return;
476- end;
477- if a.base = bStack then
478- m := place(a, U); r := target();
498+ fl := k in {kF32, kF64};
499+ if (a.base = bReg) and fl then
500+ A.fop1(A.fMov, xTarget(), regAt(a) - XmmVar, false);
501+ elsif a.base = bReg then
502+ A.movRR(target(), regAt(a));
479503 else
480- r := target(); m := place(a, r);
504+ if a.base = bStack then
505+ m := place(a, U); r := target();
506+ else
507+ r := target(); m := place(a, r);
508+ end;
509+ if fl then realMem(xTarget(), m, size, true); else loadMem(r, m, size, signed); end;
481510 end;
482- loadMem(r, m, size, signed);
483511 computed(k);
484512 end;
485513
@@ -492,32 +520,44 @@
492520 end;
493521
494522 (* Pops a value into the variable at a; an address on the stack is under
495- the value. A zero is stored from the zero register. *)
523+ the value. A zero is stored from the zero register, and a real
524+ constant as its bits. A real variable in a register is written
525+ through to its slot, as on x86-64, where the trap report finds it. *)
496526 proc popVar*(a: Adr; size: INT);
497-var r: INT; v: I64; c: BOOL;
527+var r: INT; v: I64; c, fl: BOOL;
498528 begin
499- if isReal(depth - 1) then notYet("reals"); end;
529+ if (a.base = bReg) and isReal(depth - 1) then
530+ r := regAt(a) - XmmVar;
531+ xValueTo(depth - 1, r);
532+ drop();
533+ A.storeF(r, FP, INT(homes[r - 8]), 8);
534+ return;
535+ end;
500536 if a.base = bReg then
501537 popTo(regAt(a));
502538 return;
503539 end;
504540 c, v := topConst();
541+ fl := not c and isReal(depth - 1);
505542 if c and (v = 0) then r := ZR;
506543 elsif c then r := T; A.movRI(T, v);
507544 else r := loaded(depth - 1); end;
508545 drop();
509- storeMem(r, place(a, U), size);
546+ if fl then realMem(r, place(a, U), size, false); else storeMem(r, place(a, U), size); end;
510547 end;
511548
512-(* The two values on top change places, each loaded in its register *)
549+(* The two values on top, of one kind, change places, each loaded in
550+ its register *)
513551 proc swap*;
514552 var e: Entry; x, y: INT;
515553 begin
516554 x := loaded(depth - 2);
517555 y := loaded(depth - 1);
518- A.movRR(T, x);
519- A.movRR(x, y);
520- A.movRR(y, T);
556+ if isReal(depth - 1) then
557+ A.fop1(A.fMov, XT, x, false); A.fop1(A.fMov, x, y, false); A.fop1(A.fMov, y, XT, false);
558+ else
559+ A.movRR(T, x); A.movRR(x, y); A.movRR(y, T);
560+ end;
521561 e := stack[depth - 2]; stack[depth - 2] := stack[depth - 1]; stack[depth - 1] := e;
522562 end;
523563
@@ -740,6 +780,23 @@
740780 if sy = Scan.sDiv then A.movRR(r, T); else A.madd(r, T, y, r, true); end;
741781 end;
742782
783+(* The two reals on top, the left op the right, in the left's
784+ register: fadd d16, d16, d17 *)
785+proc realOp(sy: Scan.Sym; k: Kind);
786+var x, y, o: INT;
787+begin
788+ match sy of
789+ | sPlus: o := A.fAdd;
790+ | sMinus: o := A.fSub;
791+ | sTimes: o := A.fMul;
792+ | else: o := A.fDiv;
793+ end;
794+ y := loaded(depth - 1);
795+ drop();
796+ x := top();
797+ A.fop(o, x, x, y, k = kF32);
798+end;
799+
743800 (* The two values on top, the left op the right, an integer of size
744801 bytes, signed or not, computed in 64 bits and brought back to its
745802 width, which is how a narrow integer wraps. A shift's count is taken
@@ -747,7 +804,7 @@
747804 proc op*(sy: Scan.Sym; k: Kind; size: INT; signed: BOOL);
748805 var v: I64; r, y, bits, sh: INT; c, alu: BOOL;
749806 begin
750- if k in {kF32, kF64} then notYet("reals"); end;
807+ if k in {kF32, kF64} then realOp(sy, k); return; end;
751808 bits := size * 8;
752809 alu := (sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor);
753810 (* a constant goes right, where it can be an immediate *)
@@ -780,27 +837,101 @@
780837 stack[depth - 1].k := k;
781838 end;
782839
840+(* ROL, if left, or ROR of the integer of size bytes under the count on
841+ top, at its own width, the count taken modulo it, a negative one as
842+ a shift's is (Gen.x86_64.raz says more). arm64 rotates only right,
843+ by ror, and only 32 or 64 bits: a left rotation is a right one by
844+ minus the count, and a U8 or a U16 is first repeated through 32
845+ bits, so that what leaves its low end comes back at its top, as a
846+ rotation of 32 bits by any count rotates each of the copies alike.
847+ ROL(x, n) of a U8:
848+
849+ and x9, x9, #0FFH ; repeated: 000000AB to ABABABAB
850+ orr x9, x9, x9, lsl #8
851+ orr x9, x9, x9, lsl #16
852+ neg x17, x10 ; left by n is right by -n
853+ ror w9, w9, w17 ; the count modulo 32, so modulo 8 too
854+ and x9, x9, #0FFH
855+
856+ A constant count is rotated by as ror x9, x9, #k, none if 0. As on
857+ x86-64, a rotation of 32 bits leaves the upper ones zero; a narrow
858+ one here leaves them zero too, where x86-64 leaves them as they
859+ were, which differs only for a signed integer. *)
783860 proc rotate*(left: BOOL; size: INT);
861+var c: BOOL; v: I64; r, y, w, sz: INT;
784862 begin
785- _ := left; _ := size;
786- notYet("ROL and ROR");
787- drop(); (* the count *)
863+ w := size * 8;
864+ sz := 4;
865+ if size = 8 then sz := 8; end;
866+ c, v := topConst();
867+ v := v & I64(w - 1);
868+ if left then v := (I64(w) - v) & I64(w - 1); end;
869+ if c then
870+ drop();
871+ r := top();
872+ if v = 0 then stack[depth - 1].k := kWord; return; end;
873+ else
874+ r, y := operands();
875+ if left then A.neg(U, y); y := U; end;
876+ end;
877+ if w < 32 then
878+ A.aluI(A.opAnd, r, r, (I64(1) shl w) - 1);
879+ A.aluShift(A.opOrr, r, r, r, A.shLsl, w);
880+ if w = 8 then A.aluShift(A.opOrr, r, r, r, A.shLsl, 16); end;
881+ end;
882+ if c then A.shiftI(A.shRor, r, r, INT(v), sz); else A.shift(A.shRor, r, r, y, sz); end;
883+ if w < 32 then A.aluI(A.opAnd, r, r, (I64(1) shl w) - 1); end;
884+ stack[depth - 1].k := kWord;
788885 end;
886+
887+(* CLZ, if leading, or CTZ of the unsigned integer of size bytes on top
888+ (report section 6.1), whose bits above its width are zero. clz
889+ counts the leading zeros of 32 or 64 bits, the whole width for 0;
890+ of a U8 or a U16, those of 32 less the 24 or 16 above it. CTZ is
891+ clz of the bits reversed (rbit), and of a U8 or a U16 a one bit set
892+ just above its width first, so that 0 counts the width:
789893
894+ CLZ, a U8 CTZ, a U8
895+ clz w9, w9 orr x9, x9, #100H
896+ sub x9, x9, #24 rbit w9, w9
897+ clz w9, w9 *)
790898 proc countZeros*(leading: BOOL; size: INT);
899+var r, w, sz: INT;
791900 begin
792- _ := leading; _ := size;
793- notYet("CLZ and CTZ");
901+ r := top();
902+ w := size * 8;
903+ sz := 4;
904+ if size = 8 then sz := 8; end;
905+ if not leading and (w < 32) then A.aluI(A.opOrr, r, r, I64(1) shl w); end;
906+ if not leading then A.rbit(r, r, sz); end;
907+ A.clz(r, r, sz);
908+ if leading and (w < 32) then A.aluI(A.opSub, r, r, 32 - w); end;
909+ stack[depth - 1].k := kWord;
794910 end;
911+
912+(* POPCOUNT of the integer or set on top (report section 6.1), by the
913+ vector instructions every arm64 has, as C's compilers make it: cnt
914+ counts the ones in each of the eight bytes, and addv adds them:
795915
916+ fmov d23, x9
917+ cnt v23.8b, v23.8b
918+ addv b23, v23.8b
919+ fmov x9, d23 *)
796920 proc popCount*;
921+var r: INT;
797922 begin
798- notYet("POPCOUNT");
923+ r := top();
924+ A.fmovFR(XT, r, false);
925+ A.cnt(XT, XT);
926+ A.addv(XT, XT);
927+ A.fmovRF(r, XT, false);
928+ stack[depth - 1].k := kWord;
799929 end;
800930
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):
931+(* ABS of the real, or the signed integer, on top. A real's sign bit is
932+ cleared, fabs, a NaN's too, as x86-64 clears it. An integer is
933+ negated if negative, and cut back to its width, so that the smallest
934+ value gives itself, as -x does (Gen.x86_64.raz says more):
804935
805936 cmp x9, #0
806937 cneg x9, x9, lt ; csneg x9, x9, x9, ge
@@ -808,30 +939,43 @@
808939 proc abs*(k: Kind; size: INT);
809940 var r: INT;
810941 begin
811- if k in {kF32, kF64} then notYet("reals"); end;
812942 r := top();
943+ if k in {kF32, kF64} then A.fop1(A.fAbs, r, r, k = kF32); return; end;
813944 A.cmpI(r, 0);
814945 A.csel(A.csNeg, A.ccGE, r, r, r);
815946 if size < 8 then A.extend(r, r, size, true); end;
816947 end;
817948
818-(* The two values on top, compared: a BOOL, 1 or 0 *)
949+(* The two values on top, compared: a BOOL, 1 or 0. Two reals are
950+ compared by fcmp, and the BOOL is made in the left's integer
951+ register: fcmp d16, d17; cset x9, mi for < (CcReal) *)
819952 proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL);
820-var r, y, i: INT;
953+var r, x, y, i: INT;
821954 begin
822- if k in {kF32, kF64} then notYet("reals"); end;
823955 i := INT(sy) - INT(Scan.sEql);
956+ if k in {kF32, kF64} then
957+ y := loaded(depth - 1);
958+ drop();
959+ x := top();
960+ drop();
961+ r := target();
962+ A.fcmp(x, y, k = kF32);
963+ A.cset(CcReal[i], r);
964+ computed(kWord);
965+ return;
966+ end;
824967 r, y := operands();
825968 A.cmp(r, y);
826969 if signed then A.cset(CcSigned[i], r); else A.cset(CcUnsigned[i], r); end;
827970 stack[depth - 1].k := kWord;
828971 end;
829972
973+(* -x; of a real, its sign bit flipped, fneg, as x86-64 flips it *)
830974 proc neg*(k: Kind; size: INT; signed: BOOL);
831975 var r: INT;
832976 begin
833- if k in {kF32, kF64} then notYet("reals"); end;
834977 r := top();
978+ if k in {kF32, kF64} then A.fop1(A.fNeg, r, r, k = kF32); return; end;
835979 A.neg(r, r);
836980 if size < 8 then A.extend(r, r, size, signed); end;
837981 end;
@@ -860,24 +1004,53 @@
8601004 stack[depth - 1].k := kWord;
8611005 end;
8621006
1007+(* The integer on top, as a real of kind k, in its real register: scvtf
1008+ of the 64 bits, or ucvtf if big, a U64 that may be above high(I64),
1009+ which x86-64 halves and doubles to the same value *)
8631010 proc intToReal*(k: Kind; big: BOOL);
1011+var r: INT;
8641012 begin
865- _ := big;
866- notYet("reals");
1013+ r := top();
1014+ A.scvtf(XRegs[(depth - 1) mod NumRegs], r, k = kF32, not big);
8671015 stack[depth - 1].k := k;
8681016 end;
8691017
1018+(* The real of kind k on top, toward zero, as an I64 (report 6.1, the
1019+ table of conversions); out of an I64's range, or a NaN, it is the
1020+ smallest I64, as x86-64's cvttsd2si gives. fcvtzs gives the nearest
1021+ I64 instead, and 0 for a NaN, so the result is mended: a NaN is
1022+ unordered with itself, which sets V, and only the largest I64, which
1023+ no real converts to exactly, overflows when 1 is added, setting V:
1024+
1025+ fcvtzs x9, d16
1026+ mov x15, #8000000000000000H
1027+ fcmp d16, d16
1028+ csel x9, x15, x9, vs ; a NaN
1029+ cmn x9, #1
1030+ csel x9, x15, x9, vs ; too big
1031+ Too small is the smallest already. *)
8701032 proc realToInt*(k: Kind);
1033+var x, r: INT;
8711034 begin
872- _ := k;
873- notYet("reals");
1035+ x := top();
1036+ r := Regs[(depth - 1) mod NumRegs];
1037+ A.fcvtz(r, x, k = kF32, true);
1038+ A.movRI(T, MIN(I64));
1039+ A.fcmp(x, x, k = kF32);
1040+ A.csel(A.csSel, A.ccVS, r, T, r);
1041+ A.cmpI(r, -1);
1042+ A.csel(A.csSel, A.ccVS, r, T, r);
8741043 stack[depth - 1].k := kWord;
8751044 end;
8761045
1046+(* The real of kind k on top, as the other kind: fcvt, which rounds to
1047+ the nearest as x86-64's cvtsd2ss does *)
8771048 proc realToReal*(k: Kind);
1049+var x: INT;
8781050 begin
879- notYet("reals");
880- stack[depth - 1].k := k;
1051+ x := top();
1052+ A.fcvt(x, x, k = kF64);
1053+ if k = kF32 then stack[depth - 1].k := kF64; else stack[depth - 1].k := kF32; end;
8811054 end;
8821055
8831056 (* Whether the slices in the frame at a and b (the offsets of their
@@ -966,10 +1139,18 @@
9661139 if size < 8 then A.extend(r, r, size, signed); end;
9671140 end;
9681141
1142+(* NEW's memory, at the address on top, size bytes zeroed unless the
1143+ address is nil (the allocator had no room): cbz x9, L; then
1144+ zeroBytes from x0 *)
9691145 proc zeroNew*(size: I64);
1146+var L, r: INT;
9701147 begin
971- _ := size;
972- notYet("NEW");
1148+ r := top();
1149+ L := NoLabel;
1150+ A.cbz(false, r, L);
1151+ A.movRR(Dst, r);
1152+ zeroBytes(size);
1153+ A.fixLink(L);
9731154 end;
9741155
9751156 (* CAS(v, old, new), XCHG(v, x) or XADD(v, x), from the stack, v's
@@ -1378,11 +1559,74 @@
13781559 A.fixLink(out);
13791560 end;
13801561
1562+(* FILL: every element of the slice in the frame at dOff, of size bytes
1563+ each, set to the value on top if the element is a scalar, or else to
1564+ the size bytes at the address on top; n is how many elements there
1565+ are if that is known, as of an array, else -1 (Gen.x86_64.raz says
1566+ more). A constant zero into an array is its bytes zeroed
1567+ (zeroBytes). A scalar, a real's bits as an integer's, goes into x15,
1568+ and is stored into each element, by a store each into an array of
1569+ FewFills elements or fewer, else by a loop:
1570+
1571+ ldr x0, [x29, #dOff]
1572+ ldr x2, [x29, #dOff + 8]
1573+ cbz x2, out
1574+ top: str w15, [x0], #4 ; a U32
1575+ subs x2, x2, #1
1576+ b.ne top
1577+ out:
1578+
1579+ Any other element is copied into d[0], and from there forward, from
1580+ d[0] on to d[1] on, as copySlice copies, by words if it is 8 bytes
1581+ or more, else by bytes: each is read after it is written, size
1582+ bytes before, so d[0] repeats through the rest.
1583+
1584+ ... ; d[0] := x, as for a copy, from x1
1585+ ldr x1, [x29, #dOff]
1586+ add x0, x1, #size
1587+ ldr x2, [x29, #dOff + 8]
1588+ sub x2, x2, #1
1589+ ... ; times size: the bytes of d[1..n]
1590+ ... ; copyRun(8), copyRun(1) *)
1591+const FewFills = 16;
1592+
13811593 proc fillSlice*(dOff, size, n: I64; scalar: BOOL);
1594+var c: BOOL; v, i: I64; out, top: INT;
13821595 begin
1383- _ := dOff; _ := size; _ := n; _ := scalar;
1384- notYet("FILL");
1385- drop();
1596+ c, v := topConst();
1597+ out := NoLabel;
1598+ if scalar and c and (v = 0) and (n >= 0) then
1599+ drop();
1600+ A.load(Dst, FP, INT(dOff), 8, false);
1601+ zeroBytes(n * size);
1602+ elsif scalar and (n >= 0) and (n <= FewFills) then
1603+ popTo(T);
1604+ A.load(Dst, FP, INT(dOff), 8, false);
1605+ for i := 0 to n - 1 do A.store(T, Dst, INT(i * size), INT(size)); end;
1606+ elsif scalar then
1607+ popTo(T);
1608+ A.load(Dst, FP, INT(dOff), 8, false);
1609+ A.load(Tmp, FP, INT(dOff) + 8, 8, false);
1610+ A.cbz(false, Tmp, out);
1611+ top := Chunk.pc;
1612+ A.storeIdx(T, Dst, INT(size), INT(size), A.mPost);
1613+ A.aluI(A.opSubs, Tmp, Tmp, 1);
1614+ A.jccBack(A.ccNE, top);
1615+ else
1616+ popTo(Src);
1617+ A.load(Dst, FP, INT(dOff), 8, false);
1618+ A.load(Tmp, FP, INT(dOff) + 8, 8, false);
1619+ A.cbz(false, Tmp, out);
1620+ copyBytes(size);
1621+ A.load(Src, FP, INT(dOff), 8, false);
1622+ A.aluI(A.opAdd, Dst, Src, size);
1623+ A.load(Tmp, FP, INT(dOff) + 8, 8, false);
1624+ A.aluI(A.opSub, Tmp, Tmp, 1);
1625+ mulI(Tmp, size);
1626+ if size >= 8 then copyRun(8, false); end;
1627+ copyRun(1, false);
1628+ end;
1629+ A.fixLink(out);
13861630 end;
13871631
13881632 (* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp. Its
@@ -1418,23 +1662,27 @@
14181662 end;
14191663
14201664 (* Calls. The arguments are the c.n values on top, each moved into the
1421- next of x0 to x7 just before the call; the values under them are
1422- spilled first, as the call may change their registers. As the stack's
1423- registers carry no argument, the moves may go in any order. A slice
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. *)
1665+ next of x0 to x7, or a real into the next of v0 to v7, just before
1666+ the call; the values under them are spilled first, as the call may
1667+ change their registers. As the stack's registers carry no argument,
1668+ the moves may go in any order. A slice goes as its two words, both
1669+ in registers or both on the stack, a record by value as the address
1670+ of a copy (Item makes it). The arguments past the registers go on
1671+ the machine's stack, the first at sp, in an area sp is lowered by
1672+ for the call, a multiple of 16; and so do those past a C procedure's
1673+ own parameters where it ends in ..., in registers or not, as Apple's
1674+ convention has it. Each takes 8 bytes, a real as its bits, but a C
1675+ procedure's own parameter of an integer, BOOL, enumeration, set or
1676+ real, which takes its size, at a multiple of it, as Apple packs
1677+ them: an I32 then an I64 go at sp and sp + 8, an I32 then an I16 at
1678+ sp and sp + 4. A result in memory, a record, an array or several
1679+ results, goes to the caller's temporary c.ret, its address in x8. A
1680+ single result comes back in x0, or d0, two words in x0 and x1: a
1681+ slice is stored in c.ret, two results pushed as two values. *)
14361682 proc call*(var c: CallInfo);
1437-var res: ^Sym.Type; i, ni, k, first, own, r: INT; area: I64; onStack: [64]BOOL;
1683+var
1684+ res: ^Sym.Type; q: ^Sym.Obj; i, ni, nf, first, own, r: INT; area: I64;
1685+ onStack: [64]BOOL; at, size: [64]INT; (* where on the stack, and how wide *)
14381686 begin
14391687 first := depth - c.n;
14401688 spillBelow(first);
@@ -1444,27 +1692,39 @@
14441692 c.ret := allocPerm(res^.size, res^.align); (* kept, as on x86-64 (report 8) *)
14451693 elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then
14461694 c.ret := allocTemp(res^.size, res^.align);
1447- elsif (res # nil) and (res^.form = Sym.fReal) then
1448- notYet("reals");
14491695 end;
14501696 own := c.n;
14511697 if c.sig^.variadic then own := paramWords(c.sig); end;
1452- ni := 0; k := 0;
1698+ ni := 0; nf := 0; area := 0;
1699+ q := c.sig^.fields; (* the parameter of argument i, while there is one *)
14531700 for i := 0 to c.n - 1 do
1454- if c.isF[i] then notYet("reals"); end;
1701+ size[i] := 8;
1702+ if c.ext and (q # nil) and not q^.isVar
1703+ and (q^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet, Sym.fReal}) then
1704+ size[i] := INT(q^.typ^.size);
1705+ end;
1706+ if (q # nil) and not c.pair[i] then q := q^.next; end;
14551707 if i >= own then onStack[i] := true;
1708+ elsif c.isF[i] then onStack[i] := nf = 8;
14561709 elsif (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1];
14571710 else onStack[i] := ni + 1 + INT(c.pair[i]) > 8; end;
1458- if onStack[i] then INC(k); else INC(ni); end;
1711+ if onStack[i] then
1712+ area := Sym.alignUp(area, size[i]);
1713+ at[i] := INT(area);
1714+ INC(area, size[i]);
1715+ elsif c.isF[i] then INC(nf);
1716+ else INC(ni); end;
14591717 end;
1460- area := Sym.alignUp(8 * k, 16);
1718+ area := Sym.alignUp(area, 16);
14611719 if area > 0 then A.aluI(A.opSub, SP, SP, area); end;
1462- k := 0; ni := 0;
1720+ ni := 0; nf := 0;
14631721 for i := 0 to c.n - 1 do
14641722 if onStack[i] then
14651723 valueTo(first + i, T);
1466- A.store(T, SP, 8 * k, 8);
1467- INC(k);
1724+ A.store(T, SP, at[i], size[i]);
1725+ elsif c.isF[i] then
1726+ xValueTo(first + i, nf);
1727+ INC(nf);
14681728 else
14691729 valueTo(first + i, ni);
14701730 INC(ni);
@@ -1489,6 +1749,9 @@
14891749 elsif Sym.inTwoRegs(res) then
14901750 A.movRR(target(), 0); computed(kWord);
14911751 A.movRR(target(), 1); computed(kWord);
1752+ elsif (c.ret = 0) and (res^.form = Sym.fReal) then
1753+ A.fop1(A.fMov, xTarget(), 0, false);
1754+ computed(kindOf(res));
14921755 elsif c.ret = 0 then
14931756 (* C leaves the bits above a narrow result as they fall *)
14941757 r := target();
@@ -1539,10 +1802,12 @@
15391802 (* The parameters' places: each in a frame slot of its own, stored there
15401803 from its register on entry, or above the frame where the caller put
15411804 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. *)
1805+ array passed by value, holds an address (ind). A real comes in the
1806+ next of v0 to v7, and the ninth on, as call puts them, on the stack.
1807+ The address of a result in memory arrives in x8, and is kept in
1808+ retSlot. *)
15441809 proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj);
1545-var p: ^Sym.Obj; ni, k, w: INT; stackOff: I64;
1810+var p: ^Sym.Obj; ni, nf, k, w: INT; stackOff: I64; f: BOOL;
15461811 begin
15471812 frameTop := 0; frameMax := 0; permTop := 0;
15481813 resetStack(0, 0);
@@ -1557,18 +1822,20 @@
15571822 inReg[0] := 8; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil;
15581823 numIn := 1;
15591824 end;
1560- ni := 0;
1825+ ni := 0; nf := 0;
15611826 stackOff := 16;
15621827 p := params;
15631828 while p # nil do
15641829 p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice));
1830+ f := (p^.typ^.form = Sym.fReal) and not p^.isVar;
15651831 k := 1;
15661832 if (p^.typ^.form = Sym.fSlice) and not p^.isVar then k := 2; end;
1567- if (p^.typ^.form = Sym.fReal) and not p^.isVar then
1568- notYet("a real parameter");
1833+ if f and (nf < 8) then
15691834 p^.val := allocPerm(8, 8);
1835+ inReg[numIn] := -1 - nf; inSlot[numIn] := p^.val; inSize[numIn] := 8; inObj[numIn] := p;
1836+ INC(numIn); INC(nf);
15701837 k := 0;
1571- elsif ni + k > 8 then (* on the stack, as call puts it *)
1838+ elsif f or (ni + k > 8) then (* on the stack, as call puts it *)
15721839 p^.val := stackOff;
15731840 INC(stackOff, 8 * k);
15741841 k := 0;
@@ -1615,16 +1882,42 @@
16151882 zeroTop := permTop;
16161883 regMask := mask;
16171884 k := 0;
1618- for i := 0 to 4 do
1885+ for i := 0 to XBit + 7 do
16191886 if (mask & (1 shl i)) # 0 then INC(k); end;
16201887 end;
16211888 if k > 0 then saveAt := allocPerm(8 * k, 8); end;
16221889 frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop;
1890+end;
1891+
1892+(* The registers of variables in regMask, saved from saveAt on, a
1893+ quadword each, x19 to x23 first, then d8 to d15, of which a called
1894+ procedure keeps the low 64 bits, and so must give them back; and
1895+ each zeroed, as a variable starts at zero. Or, if back, restored. *)
1896+proc saveRegs(back: BOOL);
1897+var i, n, r: INT;
1898+begin
1899+ n := 0;
1900+ for i := 0 to XBit + 7 do
1901+ if (regMask & (1 shl i)) # 0 then
1902+ if i < XBit then
1903+ r := VarRegs[i];
1904+ if back then A.load(r, FP, INT(saveAt) + 8 * n, 8, false);
1905+ else A.store(r, FP, INT(saveAt) + 8 * n, 8); A.movRI(r, 0); end;
1906+ else
1907+ r := XVarRegs[i - XBit] - XmmVar;
1908+ if back then A.loadF(r, FP, INT(saveAt) + 8 * n, 8);
1909+ else A.storeF(r, FP, INT(saveAt) + 8 * n, 8); A.fmovFR(r, ZR, false); end;
1910+ end;
1911+ INC(n);
1912+ end;
1913+ end;
16231914 end;
16241915
1916+(* The variable in register r of XVarRegs has a slot of 8 bytes at
1917+ slot, which popVar writes as it writes r *)
16251918 proc xHome*(r: INT; slot: I64);
16261919 begin
1627- _ := r; _ := slot;
1920+ homes[r - XVarRegs[0]] := slot;
16281921 end;
16291922
16301923 proc markCode*(var m: CodeMark);
@@ -1667,7 +1960,6 @@
16671960 begin
16681961 Chunk.beginChunk(c);
16691962 codeChunk := c;
1670- if LEN(pending) > 0 then Chunk.setMissing(c, pending, pendingAt); pending := ""; end;
16711963 Chunk.noteLine(line, 1); (* a declaration starts a line *)
16721964 A.stp(FP, LR, SP, -16, A.mPre);
16731965 A.movRR(FP, SP);
@@ -1685,18 +1977,13 @@
16851977 A.aluI(A.opSubs, U, U, 1);
16861978 A.jccBack(A.ccNE, loop);
16871979 end;
1688- if (regMask shr XBit) # 0 then notYet("reals in registers"); end;
1689- Chunk.setSaves(c, regMask, INT(saveAt));
1690- n := 0;
1691- for i := 0 to 4 do
1692- if (regMask & (1 shl i)) # 0 then
1693- A.store(VarRegs[i], FP, INT(saveAt + 8 * n), 8);
1694- A.movRI(VarRegs[i], 0);
1695- INC(n);
1696- end;
1697- end;
1980+ Chunk.setSaves(c, regMask & ((1 shl XBit) - 1), INT(saveAt));
1981+ saveRegs(false);
16981982 for i := 0 to numIn - 1 do
1699- if (inObj[i] # nil) and (inObj[i]^.reg # 0) then
1983+ if inReg[i] < 0 then (* a real, from v0 to v7 *)
1984+ A.storeF(-1 - inReg[i], FP, INT(inSlot[i]), 8);
1985+ if inObj[i]^.reg # 0 then A.fop1(A.fMov, inObj[i]^.reg - XmmVar, -1 - inReg[i], false); end;
1986+ elsif (inObj[i] # nil) and (inObj[i]^.reg # 0) then
17001987 r := inObj[i]^.reg;
17011988 if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end;
17021989 A.movRR(r, inReg[i]);
@@ -1711,7 +1998,9 @@
17111998 p := procParams;
17121999 while p # nil do
17132000 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
2001+ if not p^.ind and (p^.typ^.form = Sym.fReal) then
2002+ A.loadF(p^.reg - XmmVar, FP, INT(p^.val), INT(p^.typ^.size));
2003+ elsif not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) then
17152004 A.load(p^.reg, FP, INT(p^.val), INT(p^.typ^.size), p^.typ^.signed);
17162005 else
17172006 A.load(p^.reg, FP, INT(p^.val), 8, false);
@@ -1730,12 +2019,13 @@
17302019 (* Pops one of several results, a value of size bytes, into its place
17312020 off bytes into the caller's, at the address that came in x8 *)
17322021 proc resultTo*(off: I64; size: INT);
1733-var r: INT;
2022+var r: INT; fl: BOOL;
17342023 begin
17352024 r := top();
2025+ fl := isReal(depth - 1);
17362026 drop();
17372027 A.load(U, FP, INT(retSlot), 8, false);
1738- A.store(r, U, INT(off), size);
2028+ if fl then A.storeF(r, U, INT(off), size); else A.store(r, U, INT(off), size); end;
17392029 end;
17402030
17412031 (* Pops an address, and copies size bytes from it to the result, off
@@ -1748,11 +2038,14 @@
17482038 copyBytes(size);
17492039 end;
17502040
1751-(* return: the result, a value on top, goes in x0; two words, the two
1752- values on top, in x0 and x1; one in memory is there already *)
2041+(* return: the result, a value on top, goes in x0, or d0; two words,
2042+ the two values on top, in x0 and x1; one in memory is there already *)
17532043 proc returnJump*;
17542044 begin
1755- if (resType # nil) and not Sym.behindPointer(resType) then
2045+ if (resType # nil) and (resType^.form = Sym.fReal) then
2046+ xValueTo(depth - 1, 0);
2047+ drop();
2048+ elsif (resType # nil) and not Sym.behindPointer(resType) then
17562049 if Sym.inTwoRegs(resType) then popTo(1); end;
17572050 popTo(0);
17582051 end;
@@ -1816,7 +2109,7 @@
18162109 frame's size, known only now, patched into its prologue. If it is
18172110 open, running off its end, it returns zeros. *)
18182111 proc endProc*(c: INT; open: BOOL): ERROR;
1819-var n: I64; i: INT;
2112+var n: I64;
18202113 begin
18212114 codeChunk := -1;
18222115 if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end;
@@ -1826,17 +2119,12 @@
18262119 elsif open then
18272120 A.movRI(0, 0);
18282121 if Sym.inTwoRegs(resType) then A.movRI(1, 0); end;
2122+ A.fmovFR(0, ZR, false);
18292123 else
18302124 A.dropJumpTo(exitL);
18312125 end;
18322126 A.fixLink(exitL);
1833- n := 0;
1834- for i := 0 to 4 do
1835- if (regMask & (1 shl i)) # 0 then
1836- A.load(VarRegs[i], FP, INT(saveAt + 8 * n), 8, false);
1837- INC(n);
1838- end;
1839- end;
2127+ saveRegs(true);
18402128 A.movRR(SP, FP);
18412129 A.ldp(FP, LR, SP, 16, A.mPost);
18422130 A.ret();
@@ -1964,18 +2252,33 @@
19642252 return nil;
19652253 end;
19662254
1967-(* A code procedure that is one instruction on a real, as Math.sqrt on
1968- x86-64, is made in place of its call there; none is, yet, here *)
1969-proc oneRealOp*(c: INT): INT;
2255+(* Whether the code procedure in chunk c is a square root of d0, or of
2256+ s0 if single, in itself, then ret, as Math.sqrt is, which a call of
2257+ it does in place (realInPlace), as on x86-64: its first word, or 0. *)
2258+const FsqrtD = 1E61C000H; FsqrtS = 1E21C000H; Ret = 0D65F03C0H;
2259+
2260+proc oneRealOp*(c: INT; single: BOOL): INT;
2261+var s, i, w, want: INT; words: [2]INT;
19702262 begin
1971- _ := c;
2263+ s := Chunk.chunks[c].start;
2264+ if (Chunk.chunks[c].size # 8) or (Chunk.chunks[c].numFix # 0) then return 0; end;
2265+ for w := 0 to 1 do
2266+ words[w] := 0;
2267+ for i := 3 to 0 by -1 do words[w] := words[w] shl 8 | INT(Chunk.text[s + 4 * w + i]); end;
2268+ end;
2269+ want := FsqrtD;
2270+ if single then want := FsqrtS; end;
2271+ if (words[0] = want) and (words[1] = Ret) then return want; end;
19722272 return 0;
19732273 end;
19742274
2275+(* The square root of oneRealOp on the real on top, in its register:
2276+ Math.sqrt(d2) is fsqrt d16, d16, with no call *)
19752277 proc realInPlace*(op: INT);
2278+var x: INT;
19762279 begin
1977- _ := op;
1978- notYet("reals");
2280+ x := top();
2281+ A.fop1(A.fSqrt, x, x, op = FsqrtS);
19792282 end;
19802283
19812284 var i: INT;
@@ -1983,7 +2286,6 @@
19832286 for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end;
19842287 Chunk.setPad(0D4200020H); (* brk #1, a trap, between procedures *)
19852288 codeChunk := -1;
1986- pending := "";
19872289 resetStack(0, 0);
19882290 rtFail := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr("fail"));
19892291 argcChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argc"));

src/Gen.x86_64.raz (history) modified +6 -4

@@ -2816,7 +2816,7 @@
28162816 end;
28172817
28182818 (* Whether the code procedure in chunk c is a square root of xmm0 in
2819- itself, then ret, as Math.sqrt is:
2819+ itself, an F32's if single, then ret, as Math.sqrt is:
28202820
28212821 sqrtsd xmm0, xmm0 F2 0F 51 C0
28222822 ret C3
@@ -2825,12 +2825,14 @@
28252825 on the value's own register (realInPlace); else 0. A call would spill
28262826 the stack's reals and reload the variables' xmm registers around it,
28272827 as System V keeps none through one, for an instruction of four bytes. *)
2828-proc oneRealOp*(c: INT): INT;
2829-var s: INT;
2828+proc oneRealOp*(c: INT; single: BOOL): INT;
2829+var s, pfx: INT;
28302830 begin
28312831 s := Chunk.chunks[c].start;
28322832 if (Chunk.chunks[c].size # 5) or (Chunk.chunks[c].numFix # 0) then return 0; end;
2833- if ((Chunk.text[s] # 0F2H) and (Chunk.text[s] # 0F3H)) or (Chunk.text[s + 1] # 0FH)
2833+ pfx := 0F2H;
2834+ if single then pfx := 0F3H; end; (* sqrtss *)
2835+ if (INT(Chunk.text[s]) # pfx) or (Chunk.text[s + 1] # 0FH)
28342836 or (Chunk.text[s + 2] # 51H) or (Chunk.text[s + 3] # 0C0H) or (Chunk.text[s + 4] # 0C3H) then
28352837 return 0;
28362838 end;

src/Parse.raz (history) modified +1 -2

@@ -3606,8 +3606,7 @@
36063606 q := sig^.fields;
36073607 if (q # nil) and (q^.next = nil) and not q^.isVar and (q^.typ^.form = Sym.fReal)
36083608 and (sig^.base # nil) and Sym.same(sig^.base, q^.typ) then
3609- p^.inl := Gen.oneRealOp(p^.chunk);
3610- if (p^.inl shr 8 = 0F3H) # (q^.typ^.size = 4) then p^.inl := 0; end;
3609+ p^.inl := Gen.oneRealOp(p^.chunk, q^.typ^.size = 4);
36113610 end;
36123611 return expect(Scan.sEnd);
36133612 end;

test/a64/Api.s (history) modified +2 -0

@@ -167,6 +167,8 @@
167167 fmov s2, w3
168168 fmov x4, d5
169169 fmov w6, s7
170+cnt v16.8b, v17.8b
171+addv b18, v19.8b
170172 // addresses, calls
171173 adr x0, .
172174 adr x1, .-4

test/a64/Encode.expect (history) modified +13 -11

@@ -388,6 +388,8 @@
388388 1e270062
389389 9e6600a4
390390 1e2600e6
391+0e205a30
392+0e31ba72
391393 10000000
392394 10ffffe1
393395 107fffe2
@@ -406,7 +408,7 @@
406408 f9400205
407409 94000000
408410 14000000
409-97fffe68
411+97fffe66
410412 d63f00a0
411413 d61f00c0
412414 d65f03c0
@@ -425,10 +427,10 @@
425427 54000043
426428 14000001
427429 aa0203e1
428-nop * 1
429-547fca81
430-b47fca61
431-nop * 261714
430+nop * 3
431+547fca01
432+b47fc9e1
433+nop * 261710
432434 54800000
433435 reloc 000000000000032c ARM64_RELOC_BRANCH26 _proc1
434436 reloc 0000000000000330 ARM64_RELOC_BRANCH26 _proc1
@@ -436,8 +438,6 @@
436438 reloc 0000000000000338 ARM64_RELOC_PAGEOFF12 _glob
437439 reloc 000000000000033c ARM64_RELOC_PAGE21 _proc1
438440 reloc 0000000000000340 ARM64_RELOC_PAGEOFF12 _proc1
439-reloc 0000000000000624 ARM64_RELOC_PAGE21 _glob
440-reloc 0000000000000628 ARM64_RELOC_PAGEOFF12 _glob
441441 reloc 000000000000062c ARM64_RELOC_PAGE21 _glob
442442 reloc 0000000000000630 ARM64_RELOC_PAGEOFF12 _glob
443443 reloc 0000000000000634 ARM64_RELOC_PAGE21 _glob
@@ -446,7 +446,9 @@
446446 reloc 0000000000000640 ARM64_RELOC_PAGEOFF12 _glob
447447 reloc 0000000000000644 ARM64_RELOC_PAGE21 _glob
448448 reloc 0000000000000648 ARM64_RELOC_PAGEOFF12 _glob
449-reloc 000000000000064c ARM64_RELOC_PAGE21 _byte
450-reloc 0000000000000650 ARM64_RELOC_PAGEOFF12 _byte
451-reloc 0000000000000658 ARM64_RELOC_BRANCH26 _proc1
452-reloc 000000000000065c ARM64_RELOC_BRANCH26 _proc1
449+reloc 000000000000064c ARM64_RELOC_PAGE21 _glob
450+reloc 0000000000000650 ARM64_RELOC_PAGEOFF12 _glob
451+reloc 0000000000000654 ARM64_RELOC_PAGE21 _byte
452+reloc 0000000000000658 ARM64_RELOC_PAGEOFF12 _byte
453+reloc 0000000000000660 ARM64_RELOC_BRANCH26 _proc1
454+reloc 0000000000000664 ARM64_RELOC_BRANCH26 _proc1

test/a64/Encode.raz (history) modified +2 -0

@@ -187,6 +187,8 @@
187187 A.fmovFR(2, 3, true);
188188 A.fmovRF(4, 5, false);
189189 A.fmovRF(6, 7, true);
190+ A.cnt(16, 17);
191+ A.addv(18, 19);
190192 (* addresses, calls *)
191193 A.adr(0, 0);
192194 A.adr(1, -4);

test/mac.sh (history) modified +7 -2

@@ -9,7 +9,8 @@
99 # Then conform/run's programs the same way, into build/mac/conform,
1010 # with their arguments (X.args): until the port is whole some fail,
1111 # which are counted and listed in build/mac/conform.fail, not failures
12-# of the run.
12+# of the run. A few of conform/run are not for this target, each named
13+# in notHere with the reason, and skipped.
1314 #
1415 # test/mac.sh build in the container: the cross compiler, then the programs
1516 # test/mac.sh run on the Mac: each program, run with a timeout
@@ -19,6 +20,8 @@
1920 # written again in place with another one.
2021 set -e
2122 out=build/mac
23+# conform/run's programs that are not for darwin-arm64, and why
24+notHere='SystemImport: SYSTEM.SYSCALL is Linux'"'"'s only (report 6.8)'
2225 case $1 in
2326 build)
2427 src/cross.sh darwin-arm64
@@ -62,9 +65,11 @@
6265 c=$out/conform
6366 cpass=0
6467 cfail=0
68+ cskip=0
6569 : > $c.fail
6670 for f in conform/run/*.raz; do
6771 name=$(basename $f .raz)
72+ if echo "$notHere" | grep -q "^$name:"; then cskip=$((cskip + 1)); continue; fi
6873 want=0
6974 [ -f conform/run/$name.status ] && want=$(cat conform/run/$name.status)
7075 if [ ! -x $c/$name ]; then
@@ -87,7 +92,7 @@
8792 cpass=$((cpass + 1))
8893 fi
8994 done
90- echo "conform/run: $cpass of $((cpass + cfail)) passed; the others in $c.fail"
95+ echo "conform/run: $cpass of $((cpass + cfail)) passed, $cskip not for this target; the others in $c.fail"
9196 [ $fail = 0 ]
9297 ;;
9398 *)

test/mac/Reals.expect (history) added +22 -0

@@ -0,0 +1,22 @@
1+nan inf -inf -0 0 0 -inf
2+FTFFFF FTFFFF FTFFFF TFFTFT TFFTFT
3+FTFFFF FTFFFF TFFTFT
4+nan nan F
5+-9223372036854775808 0 0 0 -9223372036854775808
6+-9223372036854775808 0 0 0 -9223372036854775808
7+-9223372036854775808 0 0 0 -9223372036854775808
8+-9223372036854775808 0 0 0 -9223372036854775808
9+-9223372036854775808 0 0 0 -9223372036854775808
10+-9223372036854775808 0 0 0 -9223372036854775808
11+-9223372036854775808 0 0 0 -9223372036854775808
12+9223372036854774784 -1024 -1024 0 -9223372036854775808
13+-9223372036854775808 0 0 0 -9223372036854775808
14+2 2 2 2 2
15+-2 -2 -2 254 -2
16+70000 70000 4464 112 70000
17+0 0 0 0 0
18+300 300 300 44 300
19+0 0 0 0 0
20+0.10000000149011612 16777216 0.30000001192092896 0.020000001415610313 0.5 -0.10000000149011612 inf 0 -0 inf 1.5
21+1.8446744073709552E19 1.8446744073709552E19 9.223372036854778E18 -9.223372036854776E18 16777216 -7
22+430.25 -1.25 2.5 -22 -3.5 0

test/mac/Reals.raz (history) added +104 -0

@@ -0,0 +1,104 @@
1+(* Reals at their edges, as x86-64 gives them: a NaN equal to nothing,
2+ a real out of an I64's range or a NaN converted to the smallest I64,
3+ then cut; -0.0; F32's rounding; a U64 above high(I64); reals as
4+ arguments past v7, results, in records, slices and registers *)
5+module Reals;
6+import Out, Fmt;
7+
8+type Pt = record x: F64; y: F32; n: INT; end;
9+
10+var zero, one: F64;
11+
12+proc real(x: F64);
13+var b: [Fmt.ShortMost]U8;
14+begin
15+ Out.str(Fmt.short(x, b)); Out.str(" ");
16+end;
17+
18+proc bool(b: BOOL);
19+begin
20+ if b then Out.str("T"); else Out.str("F"); end;
21+end;
22+
23+(* every comparison of a and b *)
24+proc compare(a, b: F64);
25+begin
26+ bool(a = b); bool(a # b); bool(a < b); bool(a <= b); bool(a > b); bool(a >= b);
27+ Out.str(" ");
28+end;
29+
30+proc compare32(a, b: F32);
31+begin
32+ bool(a = b); bool(a # b); bool(a < b); bool(a <= b); bool(a > b); bool(a >= b);
33+ Out.str(" ");
34+end;
35+
36+(* x converted to each integer width *)
37+proc toInt(x: F64);
38+begin
39+ Out.int(I64(x)); Out.str(" "); Out.int(I64(I32(x))); Out.str(" ");
40+ Out.int(I64(I16(x))); Out.str(" "); Out.int(I64(U8(x))); Out.str(" ");
41+ Out.int(I64(F32(x))); Out.ln;
42+end;
43+
44+(* ten reals and ten integers: the ninth and tenth of each on the stack *)
45+proc many(a, b, c, d, e, f, g, h, i, j: F64; k, l, m, n, o, p, q, r: INT; s: F32; t: INT): F64;
46+begin
47+ return a + b * 2.0 + c * 3.0 + d * 4.0 + e * 5.0 + f * 6.0 + g * 7.0 + h * 8.0
48+ + i * 9.0 + j * 10.0 + F64(k + l + m + n + o + p + q + r + t) + F64(s);
49+end;
50+
51+proc half(x: F32): F32;
52+begin
53+ return x / 2.0;
54+end;
55+
56+proc swapped(p: Pt): (F32, F64);
57+begin
58+ return p.y, p.x;
59+end;
60+
61+proc main*;
62+var
63+ nan, inf, nz, x, sum: F64;
64+ f, g: F32;
65+ u: U64;
66+ p: Pt;
67+ a: [5]F64;
68+ s: []F64;
69+ i: INT;
70+begin
71+ nan := zero / zero;
72+ inf := one / zero;
73+ nz := -zero;
74+ real(nan); real(inf); real(-inf); real(nz); real(ABS(nz)); real(-nz); real(one / nz); Out.ln;
75+ compare(nan, nan); compare(nan, one); compare(one, nan); compare(one, one); compare(nz, zero); Out.ln;
76+ f := F32(nan); g := 1.5;
77+ compare32(f, g); compare32(g, f); compare32(g, g); Out.ln;
78+ real(ABS(nan)); real(-nan); bool(ABS(nan) = ABS(nan)); Out.ln;
79+ toInt(nan); toInt(inf); toInt(-inf); toInt(1.0E300); toInt(-1.0E300);
80+ toInt(9.3E18); toInt(-9.3E18); toInt(9.2233720368547748E18); toInt(-9.2233720368547758E18);
81+ toInt(2.75); toInt(-2.75); toInt(70000.9); toInt(-0.5); toInt(300.0); toInt(nz);
82+ (* F32's rounding *)
83+ f := F32(0.1); real(F64(f)); f := F32(16777217.0); real(F64(f));
84+ f := 0.1; g := 0.2; real(F64(f + g)); real(F64(f * g)); real(F64(f / g)); real(F64(f - g));
85+ f := F32(1.0E39); real(F64(f)); f := F32(1.0E-46); real(F64(f)); real(F64(F32(-1.0E-46)));
86+ g := 3.4028234E38; real(F64(g * 2.0)); real(F64(half(3.0))); Out.ln;
87+ (* integers to reals *)
88+ u := MAX(U64); real(F64(u)); real(F64(F32(u))); u := 8000000000000401H; real(F64(u));
89+ real(F64(MIN(I64))); i := 16777217; real(F64(F32(i))); real(F64(I8(-7))); Out.ln;
90+ (* calls, records, slices, registers *)
91+ real(many(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 1, 2, 3, 4, 5, 6, 7, 8, 0.25, 9));
92+ p.x := 2.5; p.y := -1.25; p.n := 3;
93+ f, x := swapped(p); real(F64(f)); real(x);
94+ for i := 0 to 4 do a[i] := F64(i) * 0.5; end;
95+ s := a[1..5];
96+ FILL(s[2..4], -3.5);
97+ sum := 0.0;
98+ for i := 0 to LEN(s) - 1 do sum := sum + s[i] * F64(i + 1); end;
99+ real(sum); real(a[3]); real(a[0]); Out.ln;
100+end;
101+
102+begin
103+ one := 1.0;
104+end Reals.

test/mac/Rotate.expect (history) added +11 -0

@@ -0,0 +1,11 @@
1+81 81 8001 8001 80000001 80000001 8000000000000001 8000000000000001
2+3 C0 3 C000 3 C0000000 3 C000000000000000
3+C 30 C 3000 C 30000000 C 3000000000000000
4+81 81 180 180 180 1800000 180 180000000000000
5+30 C 3000 C 3000 C0000 3000 C000000000000
6+C0 3 C000 3 C0000000 3 C000000000000000 3
7+C 30 300 C0 3 3 3 8000000000000001
8+4B 4B4B F 0
9+8 8 0 16 16 0 32 32 0 64 64 0
10+3 3 2 3 11 2 11 19 2 27 35 2
11+0 0 8 0 0 16 0 0 32 0 0 64

test/mac/Rotate.raz (history) added +40 -0

@@ -0,0 +1,40 @@
1+(* ROL and ROR at every width, by constants and by variables, negative
2+ and past the width; CLZ, CTZ and POPCOUNT at every width, as x86-64
3+ gives them *)
4+module Rotate;
5+import Out;
6+
7+proc show(n: U64);
8+begin
9+ Out.hex(n, 1); Out.str(" ");
10+end;
11+
12+proc main*;
13+var
14+ b: U8; h: U16; w: U32; q: U64; n, i: INT;
15+ counts: [6]INT;
16+begin
17+ counts := [0, 1, 3, 8, 13, -1];
18+ b := 81H; h := 8001H; w := 80000001H; q := 8000000000000001H;
19+ for i := 0 to 5 do
20+ n := counts[i];
21+ show(U64(ROL(b, n))); show(U64(ROR(b, n))); show(U64(ROL(h, n))); show(U64(ROR(h, n)));
22+ show(U64(ROL(w, n))); show(U64(ROR(w, n))); show(U64(ROL(q, n))); show(U64(ROR(q, n)));
23+ Out.ln;
24+ end;
25+ show(U64(ROL(b, 3))); show(U64(ROR(b, 3))); show(U64(ROL(h, 9))); show(U64(ROR(h, 9)));
26+ show(U64(ROL(w, 33))); show(U64(ROR(w, -1))); show(U64(ROL(q, 65))); show(U64(ROR(q, 0))); Out.ln;
27+ b := 0B4H; h := 0B4B4H; w := 0F0000H; q := 0;
28+ show(U64(ROL(b, 4) | ROR(b, 4))); show(U64(ROL(h, 12))); show(U64(ROR(w, 16))); show(U64(ROL(q, 5))); Out.ln;
29+ b := 0; h := 0; w := 0; q := 0;
30+ for i := 0 to 2 do
31+ Out.int(CLZ(b)); Out.str(" "); Out.int(CTZ(b)); Out.str(" "); Out.int(POPCOUNT(b)); Out.str(" ");
32+ Out.int(CLZ(h)); Out.str(" "); Out.int(CTZ(h)); Out.str(" "); Out.int(POPCOUNT(h)); Out.str(" ");
33+ Out.int(CLZ(w)); Out.str(" "); Out.int(CTZ(w)); Out.str(" "); Out.int(POPCOUNT(w)); Out.str(" ");
34+ Out.int(CLZ(q)); Out.str(" "); Out.int(CTZ(q)); Out.str(" "); Out.int(POPCOUNT(q)); Out.ln;
35+ b := b + 18H; h := h + 1800H; w := w + 180000H; q := q + 1800000000H;
36+ if i = 1 then b := 0FFH; h := 0FFFFH; w := 0FFFFFFFFH; q := MAX(U64); end;
37+ end;
38+end;
39+
40+end Rotate.

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

@@ -0,0 +1 @@
1+1.4142135623730951 5 1.4142135381698608 nan

test/mac/Sqrt.raz (history) added +35 -0

@@ -0,0 +1,35 @@
1+(* A code procedure that is a square root of d0 and ret is made in
2+ place of its call (Gen.oneRealOp), as Math.sqrt is on x86-64; F32's
3+ too. Its own here, as lib/Math.raz has x86-64's yet. *)
4+module Sqrt;
5+import Out, Fmt;
6+import SYSTEM;
7+
8+proc root(x: F64): F64;
9+code
10+ fsqrt d0, d0;
11+ ret;
12+end root;
13+
14+proc root32(x: F32): F32;
15+code
16+ fsqrt s0, s0;
17+ ret;
18+end root32;
19+
20+proc real(x: F64);
21+var b: [Fmt.ShortMost]U8;
22+begin
23+ Out.str(Fmt.short(x, b)); Out.str(" ");
24+end;
25+
26+proc main*;
27+var x: F64; f: F32;
28+begin
29+ x := 2.0;
30+ real(root(x)); real(root(x * 8.0) + 1.0);
31+ f := 2.0;
32+ real(F64(root32(f))); real(root(-x)); Out.ln;
33+end;
34+
35+end Sqrt.