From e99cab9b8685d604c3338e9f31339d8b2af63d18 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:05:23 +0000 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index a3508e7..cb5a5f0 100644 --- a/PLAN.md +++ b/PLAN.md @@ -227,7 +227,7 @@ `fxPageOff12`, and `Scan.setBang` for a pre-index's `!`. What it left for later, `Link`'s patching of those kinds, `Gen.arm64.raz` importing `A64`, and padding with `brk`, came with 5a. -5. `Gen.arm64.raz`, in pieces, each run on the Mac (owner, 2026-11-15): +5. Done. `Gen.arm64.raz`, in pieces, each run on the Mac (owner, 2026-11-15): first a bare `Exe.darwin.raz` (a signed Mach-O over libSystem, no line numbers) and an `Rt` that only exits, built by the staged cross compiler in the container; then values and operations; jumps and @@ -264,6 +264,25 @@ `CLZ`, `ROL`, or `SYSCALL` (`SystemImport`, Linux's only). Left of step 5 (5c): the reals, `FILL`, `NEW`'s zeroing, the counts of bits and the rotations. + + 5c, done: the reals, `F32` and `F64`, on the stack in v16 to v21 and + as variables in d8 to d15, which the prologue saves; their + operations, comparisons (a NaN equal to nothing), conversions both + ways and between widths, a real out of an `I64`'s range or a NaN + mended to the smallest `I64` after `fcvtzs`, `ABS` and `-`; real + arguments in v0 to v7 and on the stack, results in d0; a code + procedure that is `fsqrt` made in place of its call. `FILL`, + `NEW`'s zeroing, `CLZ`, `CTZ`, `POPCOUNT` (by `cnt` and `addv`), + `ROL` and `ROR` at every width. A C procedure's own arguments past + x7 packed by their size, as Apple's convention wants. Nothing of + `Gen.arm64.raz` is "not yet"; only `SYSCALL` is refused, so + `test/mac.sh` skips conform/run's `SystemImport`, with the reason. + conform/run passes on the Mac, 110 of 110; `test/mac` has the reals' + edges (`Reals`), rotations and counts of every width (`Rotate`) and + `fsqrt` in place (`Sqrt`). Left for later steps: `Math`'s arm64 + twin, so that `Math.sqrt` is made in place (step 7). A rotation of a + negative `I8` or `I16` leaves the bits above its width zero, where + x86-64's `rol al, cl` leaves them as they were. 6. `Exe.darwin.raz`'s line numbers for lldb (its rebases came with 5a); `Host.darwin.raz`. 7. The library: `CAbi`, `Sys`, `Rt`, `Thread`, `Bytes`, `Math`, `Str`. diff --git a/doc/compiler.md b/doc/compiler.md index 4239580..2bb2538 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is fifteen files of raz, about 16,400 lines of code (the +The compiler is fifteen files of raz, about 16,500 lines of code (the budget is 18,000); three of them, `A64.raz`, `Gen.arm64.raz` and `Exe.darwin.raz`, are the Mac's back end, begun (`PLAN.md` 5), built 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 @@ | `src/Sym.raz` | 866 | 689 | Types, objects, scopes, layout | | `src/Chunk.raz` | 410 | 302 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,771 | 1,450 | The instruction encoder, jumps within a procedure, the assembler | -| `src/A64.raz` | 1,843 | 1,365 | The same for arm64: the encoder, jumps, the assembler | -| `src/Gen.x86_64.raz` | 2,861 | 1,999 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Gen.arm64.raz` | 1,993 | 1,398 | The same for arm64, begun: frames, values, operations, checks, calls | +| `src/A64.raz` | 1,860 | 1,375 | The same for arm64: the encoder, jumps, the assembler | +| `src/Gen.x86_64.raz` | 2,863 | 2,001 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Gen.arm64.raz` | 2,295 | 1,573 | The same for arm64: values, operations, reals, checks, calls, frames | | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,948 | 4,198 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,947 | 4,197 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 481 | 365 | Tree shaking, layout, patching, the places | | `src/Exe.linux.raz` | 455 | 358 | The executable, ELF: static and dynamic, symbol table, line numbers | | `src/Exe.darwin.raz` | 458 | 332 | The executable for macOS, Mach-O: dyld's binds and rebases, the signature | @@ -1313,7 +1313,8 @@ size, register file and operation: a scaled offset, else unscaled, else through x16), `ldp`, `stp`; the LSE atomics `ldaddal`, `swpal` and `casal`, and `dmb`, `svc`, `brk`, `align`; for reals `fop`, `fop1`, -`fcvt`, `fcmp`, `scvtf`, `fcvtz`, `fmovFR` and `fmovRF`. Two things +`fcvt`, `fcmp`, `scvtf`, `fcvtz`, `fmovFR` and `fmovRF`; and two vector +instructions, `cnt` and `addv`, for `POPCOUNT`. Two things x86 does and arm64 doesn't are Gen's to mend, as their comments say: `sdiv` by zero gives 0, and `fcvtzs` saturates where x86 gives the smallest `I64`. @@ -1379,7 +1380,7 @@ each word in hex and each fixup; `expect.sh`, on an arm64 Mac, makes `Encode.expect` from `Asm.txt` in Apple's syntax and `Api.s`, the calls' instructions a line each, with `cc -c -arch arm64` and `objdump`: -428 words and 20 relocations, the farthest jumps among them, across a +430 words and 20 relocations, the farthest jumps among them, across a megabyte of `nop`. ## Gen.raz: the stack machine @@ -1712,36 +1713,25 @@ ## Gen.arm64.raz: the stack machine on arm64 -arm64's `Gen`, for macOS on Apple Silicon (`PLAN.md` 5), is begun. It -answers every call `Gen.x86_64.raz` answers, with the same parameters, -as the interface check holds the two files to (report, 3), and makes -frames, constants, the values of variables and their stores, the -integer operations, division with its checks, comparisons and jumps, -jump tables, elements and slices and their checks, calls with integer -arguments in registers and on the stack, results in memory, C's calls -and Apple's variadic ones, `try`, deferred statements, the atomics, -`COPY`, code procedures (`A64.assemble`) and the entry point; not yet -the reals, `FILL`, `NEW`'s zeroing, the counts of bits and the -rotations (step 5c). `Gen.Arch` is `"arm64"`. +arm64's `Gen`, for macOS on Apple Silicon (`PLAN.md` 5). It answers +every call `Gen.x86_64.raz` answers, with the same parameters, as the +interface check holds the two files to (report, 3), and makes all that +x86-64's makes, but `SYSTEM.SYSCALL`, which the report gives only on +Linux (6.8). `Gen.Arch` is `"arm64"`. It was made in three steps, 5a +to 5c, each run on the Mac; conform/run passes there whole, but for +`SystemImport`. -*Not yet.* What it can't make yet isn't refused at once, since a -library module holds much that a program never calls: the C library's -`Sys` is compiled whole for the Mac, as the interface check of `Rt` -reads `Rt.raz`, which imports it. So a routine not yet made calls -`notYet`, which notes on the procedure's chunk what it is and where, -the first only (`Chunk.setMissing`); keeps the stack as the real -routine would, so that the compile reads on; and makes what code it -likes, never run. Once `Link.markLive` has marked what the program -reaches, `Raz` makes each live chunk so noted an error at its place: +*What it won't make.* `SYSCALL` isn't refused at once, since a library +module may hold what a program never calls. `missing` notes on the +procedure's chunk what it is and where (`Chunk.setMissing`), and keeps +the stack as the real routine would, so that the compile reads on. +Once `Link.markLive` has marked what the program reaches, `Raz` makes +each live chunk so noted an error at its place: - D.raz:8:19: error: not yet on arm64: div and mod + S.raz:21:25: error: SYSTEM.SYSCALL is Linux's only: call the C library -A program that uses what isn't made fails to compile, and one that -doesn't compiles. `beginProc` comes before its chunk is begun, so its -notes wait for `enterBody` (`pending`). A routine not yet made reads -into `_` the parameters it doesn't use yet, so that none is warned of. -`SYSTEM.SYSCALL`, which the report gives only on Linux (6.8), is noted -the same way, with a message of its own, and stays so. +Until 5c every routine not yet made was noted so, which let the port +run its first programs before the whole of `Gen` was there. *The stack* is x86-64's, kept as plainly as it can be: the value at depth d is in `Regs[d mod 6]`, x9 to x14, which a call may change and @@ -1757,6 +1747,67 @@ between calls; x8 is kept for the address of a result in memory, as Apple's convention has it, and x18, the system's, is never touched. +*Reals* are held as x86-64 holds them, in the registers that compute +with reals: the value at depth d in `XRegs[d mod 6]`, v16 to v21, as +d or s, so that each depth has two registers, one of each file, and +`owner` doesn't tell them apart (`loaded`, `xTarget`). Like x9 to x14 +they are a call's to change and carry no argument, so `call` moves a +real into v0 to v7 in any order; v23 is Gen's scratch (`XT`). A spilled +real takes 8 bytes, an F32 with what lies above it. arm64 has no +instruction that holds any real, and `fmov`'s immediate holds few, so a +constant is made from its bits in x15 and moved across, which for most +constants of a program, 0.25 or 2.0, is one `movz`; 0.0 comes from the +zero register (`xValueTo`): + +``` +movz x15, #16368, lsl #48 ; 1.0 +fmov d17, x15 +fadd d16, d16, d17 +``` + +The operations are one instruction each, on two registers (`realOp`), +the right operand loaded first. `neg` and `ABS` are `fneg` and +`fabs`, which flip or clear the sign bit and nothing else, a NaN's +too, as x86-64's `btc` and `btr` do. A comparison is `fcmp` and a +`cset` of the left's integer register, by `CcReal`: an unordered pair, +a NaN on either side, sets C and V and not N or Z, so `=` is `eq` and +`#` is `ne`, as IEEE wants, but `<` and `<=` can't be `lt` and `le`, +which test N # V and hold for a NaN; they are `mi` and `ls`. `>` and +`>=`, `gt` and `ge`, don't hold for one. An integer becomes a real by +`scvtf` of its 64 bits, or `ucvtf` for a `U64`, which rounds as +x86-64's halving and doubling does; a real becomes the other by `fcvt`. +A real becomes an integer by `fcvtzs`, toward zero, as the report's +table has it; but where x86-64's `cvttsd2si` gives the smallest `I64` +for a NaN or a real out of range, `fcvtzs` gives 0 for a NaN and the +nearest `I64` beyond, so `realToInt` mends it. A NaN is unordered with +itself, which sets V; the largest `I64`, which no real converts to +exactly, is the one value 1 overflows, which sets V too; too small is +the smallest already: + +``` +fcvtzs x9, d16 +mov x15, #8000000000000000H +fcmp d16, d16 +csel x9, x15, x9, vs ; a NaN +cmn x9, #1 +csel x9, x15, x9, vs ; too big +``` + +then cut to a narrow type's width as on x86-64, so `I32(nan)` is 0. + +A real argument goes in the next of v0 to v7, the ninth on the stack +as an integer past x7 does, and a real result comes back in d0 +(`call`, `beginProc`, `returnJump`). `FILL` stores a real's bits as an +integer's. A real variable takes one of d8 to d15 (`XVarRegs`), whose +low 64 bits Apple's convention has a called procedure keep: so, unlike +x86-64, where no xmm register outlives a call and each real variable +is loaded again after one, the prologue saves those it uses, beside +x19 to x23, the epilogue restores them (`saveRegs`), and a call leaves +them be. `popVar` still writes each through to its slot (`homes`, +`xHome`), as x86-64's does, where a trap report finds it. A code +procedure that is `fsqrt d0, d0` and `ret`, as `Math.sqrt` will be, is +made in place of its call (`oneRealOp`, `realInPlace`), as on x86-64. + A place in memory is a `Mem`: a register and an offset, or a chunk and an offset, which A64 reaches by `adrp` and the place in the page (`loadChunk`, `storeChunk`), since macOS loads a program anywhere. @@ -1786,9 +1837,9 @@ a body, so sp stays a multiple of 16 and `pushed` is always 0. The busiest variables take x19 to x23 (`VarRegs`), which a callee gives back; no parameter is kept where it arrives (`keepArrived` does -nothing); the reals' variables will take d8 to d15 (`XVarRegs`, -numbered from `XmmVar`, 32, above every x register, which `Parse` tells -apart by it). +nothing); the reals' variables take d8 to d15 (`XVarRegs`, numbered +from `XmmVar`, 32, above every x register, which `Parse` tells apart by +it). *Calls* move each argument into x0 to x7, a slice's two words into two of them or both onto the stack. The arguments past the registers go on @@ -1797,9 +1848,11 @@ the callee finds them above its frame record, at x29 + 16 on, as x86's finds them above rbp. A C procedure that ends in `...` takes what comes past its own parameters on the stack, registers free or not, as -Apple's convention has it (C's own parameters past x7 narrower than 8 -bytes, which Apple packs, raz doesn't make). A single result comes back -in x0, two words in x0 and x1, and a narrow result of C's is extended, +Apple's convention has it. On the stack each argument takes 8 bytes, +but a C procedure's own parameter of an integer, `BOOL`, enumeration, +set or real takes its size, at a multiple of it, as Apple packs them: +an `I32` and an `I16` past x7 go at sp and sp + 4. A single result +comes back in x0, or d0, two words in x0 and x1, and a narrow result of C's is extended, as C leaves the upper bits as they fall; a result in memory, a record or several results, goes to the caller's temporary, whose address the caller puts in x8 and the callee keeps in a slot (`retSlot`) for @@ -1833,8 +1886,44 @@ after a `br`, found by `adr`; the atomics by the LSE instructions, `casal`, `swpal` and `ldaddal`; `COPY` (`copySlice`) by words, then bytes, back from the ends when the destination starts inside the -source; `ABS` by `cneg`. +source; `ABS` by `cneg`; `FILL` (`fillSlice`) by a store each into an +array of 16 elements or fewer, else a loop of stores, and a record or +array element copied into `d[0]` and from there forward, by words for +one of 8 bytes or more, each read after it is written; `NEW`'s zeroing +by `zeroBytes`. `CLZ` is `clz`, of 32 or 64 bits, less 24 or 16 for a +`U8` or a `U16`; `CTZ` is `clz` of the bits reversed, `rbit`, a one bit +set just above a narrow value first, so that 0 counts its width. +`POPCOUNT` is plainer than x86-64's sums of bits: every arm64 counts +the ones of each byte of a vector register, `cnt`, and adds the bytes, +`addv`, as C's compilers do: +``` +fmov d23, x9 +cnt v23.8b, v23.8b +addv b23, v23.8b +fmov x9, d23 +``` + +A rotation is `ror`, which arm64 has only to the right and only of 32 +or 64 bits: `ROL` by n is `ror` by -n, which `ror` takes modulo the +width as x86's `rol` does, and a `U8` or a `U16` is first repeated +through 32 bits, so that what leaves its low end comes back at its top +and a rotation of 32 bits by any count rotates each copy alike, the +count modulo 8 or 16 too; the result is cut back to the width: + +``` +and x9, x9, #0FFH ; ROL(x, n) of a U8: 000000AB +orr x9, x9, x9, lsl #8 ; to ABABABAB +orr x9, x9, x9, lsl #16 +neg x17, x10 +ror w9, w9, w17 +and x9, x9, #0FFH +``` + +x86-64's `rol al, cl` leaves the bits above a narrow value as they +were, here they are zero; no program sees the difference, as those bits +aren't the value's and `ROL` takes only an unsigned type. + *The entry point* (`entryPoint`) is the C `main` that dyld calls: it pushes a frame record, which links to dyld's, saves argc, an `int` it extends to 64 bits, and argv, gives `Rt` C's `exit` in `cExit` and the @@ -2613,8 +2702,10 @@ compiler for the Mac this way, in the linux/amd64 container, and `make mac-check` (`test/mac.sh`), on an Apple Silicon Mac, has it compile `test/mac`'s programs there and runs each here, natively, against its -`X.expect` and `X.status`; then conform/run's, which are counted, not -failures yet, those that fail listed in `build/mac/conform.fail`. The host changes nothing of the target: +`X.expect` and `X.status`; then conform/run's, which are counted, those +that fail listed in `build/mac/conform.fail`, and those not for the Mac, +named in the script with why (`SystemImport`, as `SYSCALL` is Linux's), +skipped. The host changes nothing of the target: a program for another system still needs that system's `Sys` and `Rt`, and for a kernel the options `-bare`, `-s` and `-base`. diff --git a/src/A64.raz b/src/A64.raz index 2281a62..9a98ea2 100644 --- a/src/A64.raz +++ b/src/A64.raz @@ -576,7 +576,8 @@ var opc: INT; s, r: []const U8; begin opc := (bits shr 22) & 3; - if (opc & 1) = 0 then s := "str"; else s := "ldr"; end; + (* opc 0 stores, and 2 a real's q; else it loads, 2 an ldrsw *) + if (opc = 0) or (opc = 2) and ((bits & 04000000H) # 0) then s := "str"; else s := "ldr"; end; if unscaled then s := Diag.say("{}ur", [Fmt.str(s[0..2])]); end; if (bits & 04000000H) # 0 then r := vreg(rt, sizeOf(bits)); @@ -838,6 +839,22 @@ proc fmovRF*(rd, vn: INT; single: BOOL); begin fconv("fmov", bit(not single), 8 - 4 * bit(single), 0, 6, rd, vn, false); +end; + +(* Two of the vector instructions, on the low 8 bytes of a register, + for POPCOUNT: cnt vd.8b, vn.8b, the ones in each byte, and addv bd, + vn.8b, the eight bytes added: 0 0 0 01110 00 10000 00101 10 Rn Rd, + and 0 0 0 01110 00 11000 11011 10 Rn Rd *) +proc cnt*(vd, vn: INT); +begin + if listing then say(Diag.say("cnt v{}.8b, v{}.8b", [Fmt.int(vd), Fmt.int(vn)])); end; + emit(0E205800H | (vn shl 5) | vd); +end; + +proc addv*(vd, vn: INT); +begin + if listing then say(Diag.say("addv b{}, v{}.8b", [Fmt.int(vd), Fmt.int(vn)])); end; + emit(0E31B800H | (vn shl 5) | vd); end; (* Addresses, and references to other chunks *) diff --git a/src/Gen.arm64.raz b/src/Gen.arm64.raz index f867943..5efad62 100644 --- a/src/Gen.arm64.raz +++ b/src/Gen.arm64.raz @@ -4,16 +4,9 @@ The same stack machine as x86-64's, with the same calls, which Item and Parse make: they push constants, addresses and the values of variables, operate on the values on top, store them and jump on - them. What it makes so far is frames, constants, the values of - variables and their stores, the integer operations, comparisons and - jumps, jump tables, elements and slices and their checks, calls with - integer arguments in registers and on the stack, C's through the - quadword the loader fills in, results in memory, try, deferred - statements, the atomics, COPY, code procedures and the program's - entry point. What else - is asked is noted as "not yet on arm64" (notYet), and a program that - uses it fails to compile rather than run wrong; PLAN.md 5 says the - pieces still to come. + them. It makes all that x86-64's does, in arm64's instructions, but + SYSCALL, which is Linux's only: a program that uses it fails to + compile (missing). The stack is kept in registers as on x86-64: the value at depth d, depth 0 being the bottom, is in x9 to x14, register d mod 6, all of @@ -25,29 +18,39 @@ loaded when it is pushed: no instruction of arm64 but a load or a store takes memory. + A real, an F32 or an F64, is held as x86-64 holds it, in the + registers that compute with reals: the value at depth d in v16 to + v21, XRegs[d mod 6], d or s, so each depth has two registers, and + owner doesn't tell them apart. They are a call's to change and carry + no argument, as x9 to x14 are; v23 is Gen's scratch. A real constant + is made in x15 and moved across (fmov), as arm64 has no instruction + that holds any real. + Registers outside the stack: x15 and x17, Gen's scratch, as rax and rcx are on x86-64; x16, the encoder's (A64); x0 to x3 for copying and slices, between calls, where they hold nothing; x8, which Apple's convention keeps for the address of a result in memory; x18, the system's, never touched; x19 to x23, a procedure's busiest variables (VarRegs), - which a called procedure gives back as it found them; x29, the frame - pointer, and x30, the return address. + and d8 to d15, its busiest reals (XVarRegs), which a called procedure + gives back as it found them; x29, the frame pointer, and x30, the + return address. Calls use Apple's arm64 convention: the first eight integer arguments - in x0 to x7, the rest on the machine's stack, results in x0 and x1, - or in memory at the address in x8, sp a multiple of 16 always, a + in x0 to x7, the first eight reals in v0 to v7, the rest on the + machine's stack, results in x0 and x1, or d0, or in memory at the + address in x8, sp a multiple of 16 always, a frame record of x29 and x30 that x29 points at, so that the chain of frames can be followed. A frame is made whole by the prologue, so nothing is pushed in a body but a call's stack arguments, for the call, and a deferred statement's return address: pushed is always 0. - What x86-64 catches by itself, and arm64 doesn't, the code checks: a - division by zero, and the smallest value by -1 (divide). + What x86-64 does by itself, and arm64 doesn't, the code does: it + checks a division by zero, and the smallest value by -1 (divide), + and gives the smallest I64 for a real out of range (realToInt). Depends on: Scan and Diag for errors, Sym for types, Chunk for code and data, A64 for instructions. - Leaves out, for now: reals, FILL, NEW's zeroing, the counts of bits - and the rotations; and SYSCALL, which is Linux's only. *) + Leaves out SYSCALL, which is Linux's only. *) module Gen; import Fmt, Diag, Scan, Sym, Chunk, A := A64; import SYSTEM; @@ -66,6 +69,7 @@ FP = A.FP; LR = A.LR; SP = A.SP; ZR = A.ZR; T = 15; U = 17; (* Gen's scratch *) Dst = 0; Src = 1; Tmp = 2; (* copying's *) + XT = 23; (* Gen's scratch for a real *) type Kind* = (kWord, kLong, kF32, kF64); @@ -160,9 +164,14 @@ StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"]; NumRegs = 6; Regs: [NumRegs]INT = [9, 10, 11, 12, 13, 14]; + XRegs: [NumRegs]INT = [16, 17, 18, 19, 20, 21]; (* v16 to v21 *) (* the condition codes of the comparisons, from sEql: = # < <= > >= *) CcSigned: [6]INT = [A.ccEQ, A.ccNE, A.ccLT, A.ccLE, A.ccGT, A.ccGE]; CcUnsigned: [6]INT = [A.ccEQ, A.ccNE, A.ccLO, A.ccLS, A.ccHI, A.ccHS]; + (* and of reals, after fcmp: an unordered pair, a NaN, sets C and V + and not N or Z, so that only # holds, as IEEE wants; lt and le + would hold, as they test N # V, so < is mi and <= ls *) + CcReal: [6]INT = [A.ccEQ, A.ccNE, A.ccMI, A.ccLS, A.ccGT, A.ccGE]; (* sub sp, sp, #imm12 and the same shifted by 12: the prologue's two, whose immediates endProc patches *) SubSp = 0D10003FFH; SubSp12 = 0D14003FFH; @@ -177,48 +186,34 @@ retSlot: I64; resType: ^Sym.Type; (* the procedure's result, or nil *) exitL: INT; - (* parameters arriving in registers, x0 to x7, and a result's address - in x8: where each is stored on entry *) - inReg: [9]INT; - inSlot: [9]I64; - inSize: [9]INT; - inObj: [9]^Sym.Obj; + (* parameters arriving in registers, x0 to x7 or, told by a negative + number, -1 - n, v0 to v7, and a result's address in x8: where each + is stored on entry *) + inReg: [17]INT; + inSlot: [17]I64; + inSize: [17]INT; + inObj: [17]^Sym.Obj; numIn: INT; procParams: ^Sym.Obj; (* the parameters, for enterBody *) regMask: INT; (* the registers of variables, as bits (XBit) *) saveAt: I64; (* where they are saved, a quadword each *) + homes: [8]I64; (* the slots of the variables in XVarRegs *) frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *) - pending: []const U8; (* what beginProc found not yet made, for enterBody's chunk *) - pendingAt: Diag.Pos; (* the stubs that a failed check calls, one for each kind of failure, made by entryPoint for those used; and the procedure's checks *) failStubs: [8]INT; fails: [4096]Fail; numFails: INT; -(* What this Gen can't make yet marks the procedure being compiled +(* What this Gen won't make, SYSCALL, marks the procedure being compiled (Chunk.setMissing), which is an error if the program uses it, and the compile reads on: a module the program imports may hold what it never - calls, as Sys does. So each routine not yet made keeps the stack as - the real one would, and makes what code it likes, never run; it - reads into _ the parameters it doesn't use yet, so that none is - warned of. What this Gen will never make, SYSCALL, is marked so too, - with its own message. *) + calls. The routine keeps the stack as the real one would. *) proc missing(msg: []const U8); begin - if codeChunk >= 0 then - Chunk.setMissing(codeChunk, msg, Scan.tokPos()); - elsif LEN(pending) = 0 then (* beginProc's, before enterBody names the chunk *) - pending := msg; - pendingAt := Scan.tokPos(); - end; + if codeChunk >= 0 then Chunk.setMissing(codeChunk, msg, Scan.tokPos()); end; end; -proc notYet(what: []const U8); -begin - missing(Diag.say("not yet on arm64: {}", [Fmt.str(what)])); -end; - (* -S: the text of each instruction is kept, and list prints it *) proc setListing*(yes: BOOL); begin @@ -258,13 +253,14 @@ proc allocPerm*(size, align: I64): I64; forward; proc loaded(i: INT): INT; forward; -(* The value i, in its register, goes to a frame slot *) +(* The value i, in its register, goes to a frame slot: a real as 8 + bytes, an F32 with what lies above it *) proc spill(i: INT); -var s: I64; +var s: I64; r: INT; begin - _ := loaded(i); + r := loaded(i); s := allocTemp(8, 8); - A.store(Regs[i mod NumRegs], FP, INT(s), 8); + if isReal(i) then A.storeF(r, FP, INT(s), 8); else A.store(r, FP, INT(s), 8); end; stack[i].loc := lFrame; stack[i].val := s; owner[i mod NumRegs] := -1; @@ -292,7 +288,7 @@ end; (* The value i into register r, from wherever it is; it stays where it - was, so r is a new copy *) + was, so r is a new copy. A real goes as its bits. *) proc valueTo(i, r: INT); begin match stack[i].loc of @@ -300,20 +296,36 @@ | lAdr: if stack[i].chunk >= 0 then A.addrChunk(r, stack[i].chunk, INT(stack[i].val)); else A.aluI(A.opAdd, r, FP, stack[i].val); end; - | lReg: if Regs[i mod NumRegs] # r then A.movRR(r, Regs[i mod NumRegs]); end; + | lReg: + if isReal(i) then A.fmovRF(r, XRegs[i mod NumRegs], false); + elsif Regs[i mod NumRegs] # r then A.movRR(r, Regs[i mod NumRegs]); end; | lFrame: A.load(r, FP, INT(stack[i].val), 8, false); end; end; -(* The value i, in its register *) +(* The real i into register v, as valueTo does for an integer: a + constant by its bits, made in x15, or the zero register's *) +proc xValueTo(i, v: INT); +begin + match stack[i].loc of + | lConst: + if stack[i].val = 0 then A.fmovFR(v, ZR, false); + else A.movRI(T, stack[i].val); A.fmovFR(v, T, false); end; + | lReg: if XRegs[i mod NumRegs] # v then A.fop1(A.fMov, v, XRegs[i mod NumRegs], false); end; + | else: A.loadF(v, FP, INT(stack[i].val), 8); (* lFrame *) + end; +end; + +(* The value i, in its register, of its kind *) proc loaded(i: INT): INT; var r: INT; begin if stack[i].loc # lReg then r := claim(i); - valueTo(i, r); + if isReal(i) then xValueTo(i, XRegs[i mod NumRegs]); else valueTo(i, r); end; stack[i].loc := lReg; end; + if isReal(i) then return XRegs[i mod NumRegs]; end; return Regs[i mod NumRegs]; end; @@ -339,12 +351,18 @@ INC(depth); end; -(* The register a new value on top is computed in *) +(* The register a new value on top is computed in, and a real's *) proc target: INT; begin return claim(depth); end; +proc xTarget: INT; +begin + _ := claim(depth); + return XRegs[depth mod NumRegs]; +end; + (* The value just computed in target goes on top *) proc computed(k: Kind); begin @@ -378,9 +396,10 @@ for r := 0 to NumRegs - 1 do owner[r] := -1; end; end; +(* A constant; a real's v is its bits, an F64's or, in the low 32, an + F32's *) proc pushConst*(k: Kind; v: I64); begin - if k in {kF32, kF64} then notYet("reals"); end; newEntry(k, lConst, v); end; @@ -435,6 +454,15 @@ else A.store(r, m.base, INT(m.off), size); end; end; +(* A real's register v loaded from m, or stored there if not load *) +proc realMem(v: INT; m: Mem; size: INT; load: BOOL); +begin + if (m.chunk >= 0) and load then A.loadChunkF(v, m.chunk, INT(m.off), size); + elsif m.chunk >= 0 then A.storeChunkF(v, m.chunk, INT(m.off), size); + elsif load then A.loadF(v, m.base, INT(m.off), size); + else A.storeF(v, m.base, INT(m.off), size); end; +end; + (* Register r := the address m *) proc adrTo(r: INT; m: Mem); begin @@ -465,21 +493,21 @@ end; proc pushVar*(a: Adr; size: INT; signed: BOOL; k: Kind); -var m: Mem; r: INT; +var m: Mem; r: INT; fl: BOOL; begin - if k in {kF32, kF64} then notYet("reals"); end; - if a.base = bReg then - r := target(); - A.movRR(r, regAt(a)); - computed(k); - return; - end; - if a.base = bStack then - m := place(a, U); r := target(); + fl := k in {kF32, kF64}; + if (a.base = bReg) and fl then + A.fop1(A.fMov, xTarget(), regAt(a) - XmmVar, false); + elsif a.base = bReg then + A.movRR(target(), regAt(a)); else - r := target(); m := place(a, r); + if a.base = bStack then + m := place(a, U); r := target(); + else + r := target(); m := place(a, r); + end; + if fl then realMem(xTarget(), m, size, true); else loadMem(r, m, size, signed); end; end; - loadMem(r, m, size, signed); computed(k); end; @@ -492,32 +520,44 @@ end; (* Pops a value into the variable at a; an address on the stack is under - the value. A zero is stored from the zero register. *) + the value. A zero is stored from the zero register, and a real + constant as its bits. A real variable in a register is written + through to its slot, as on x86-64, where the trap report finds it. *) proc popVar*(a: Adr; size: INT); -var r: INT; v: I64; c: BOOL; +var r: INT; v: I64; c, fl: BOOL; begin - if isReal(depth - 1) then notYet("reals"); end; + if (a.base = bReg) and isReal(depth - 1) then + r := regAt(a) - XmmVar; + xValueTo(depth - 1, r); + drop(); + A.storeF(r, FP, INT(homes[r - 8]), 8); + return; + end; if a.base = bReg then popTo(regAt(a)); return; end; c, v := topConst(); + fl := not c and isReal(depth - 1); if c and (v = 0) then r := ZR; elsif c then r := T; A.movRI(T, v); else r := loaded(depth - 1); end; drop(); - storeMem(r, place(a, U), size); + if fl then realMem(r, place(a, U), size, false); else storeMem(r, place(a, U), size); end; end; -(* The two values on top change places, each loaded in its register *) +(* The two values on top, of one kind, change places, each loaded in + its register *) proc swap*; var e: Entry; x, y: INT; begin x := loaded(depth - 2); y := loaded(depth - 1); - A.movRR(T, x); - A.movRR(x, y); - A.movRR(y, T); + if isReal(depth - 1) then + A.fop1(A.fMov, XT, x, false); A.fop1(A.fMov, x, y, false); A.fop1(A.fMov, y, XT, false); + else + A.movRR(T, x); A.movRR(x, y); A.movRR(y, T); + end; e := stack[depth - 2]; stack[depth - 2] := stack[depth - 1]; stack[depth - 1] := e; end; @@ -740,6 +780,23 @@ if sy = Scan.sDiv then A.movRR(r, T); else A.madd(r, T, y, r, true); end; end; +(* The two reals on top, the left op the right, in the left's + register: fadd d16, d16, d17 *) +proc realOp(sy: Scan.Sym; k: Kind); +var x, y, o: INT; +begin + match sy of + | sPlus: o := A.fAdd; + | sMinus: o := A.fSub; + | sTimes: o := A.fMul; + | else: o := A.fDiv; + end; + y := loaded(depth - 1); + drop(); + x := top(); + A.fop(o, x, x, y, k = kF32); +end; + (* The two values on top, the left op the right, an integer of size bytes, signed or not, computed in 64 bits and brought back to its width, which is how a narrow integer wraps. A shift's count is taken @@ -747,7 +804,7 @@ proc op*(sy: Scan.Sym; k: Kind; size: INT; signed: BOOL); var v: I64; r, y, bits, sh: INT; c, alu: BOOL; begin - if k in {kF32, kF64} then notYet("reals"); end; + if k in {kF32, kF64} then realOp(sy, k); return; end; bits := size * 8; alu := (sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor); (* a constant goes right, where it can be an immediate *) @@ -780,27 +837,101 @@ stack[depth - 1].k := k; end; +(* ROL, if left, or ROR of the integer of size bytes under the count on + top, at its own width, the count taken modulo it, a negative one as + a shift's is (Gen.x86_64.raz says more). arm64 rotates only right, + by ror, and only 32 or 64 bits: a left rotation is a right one by + minus the count, and a U8 or a U16 is first repeated through 32 + bits, so that what leaves its low end comes back at its top, as a + rotation of 32 bits by any count rotates each of the copies alike. + ROL(x, n) of a U8: + + and x9, x9, #0FFH ; repeated: 000000AB to ABABABAB + orr x9, x9, x9, lsl #8 + orr x9, x9, x9, lsl #16 + neg x17, x10 ; left by n is right by -n + ror w9, w9, w17 ; the count modulo 32, so modulo 8 too + and x9, x9, #0FFH + + A constant count is rotated by as ror x9, x9, #k, none if 0. As on + x86-64, a rotation of 32 bits leaves the upper ones zero; a narrow + one here leaves them zero too, where x86-64 leaves them as they + were, which differs only for a signed integer. *) proc rotate*(left: BOOL; size: INT); +var c: BOOL; v: I64; r, y, w, sz: INT; begin - _ := left; _ := size; - notYet("ROL and ROR"); - drop(); (* the count *) + w := size * 8; + sz := 4; + if size = 8 then sz := 8; end; + c, v := topConst(); + v := v & I64(w - 1); + if left then v := (I64(w) - v) & I64(w - 1); end; + if c then + drop(); + r := top(); + if v = 0 then stack[depth - 1].k := kWord; return; end; + else + r, y := operands(); + if left then A.neg(U, y); y := U; end; + end; + if w < 32 then + A.aluI(A.opAnd, r, r, (I64(1) shl w) - 1); + A.aluShift(A.opOrr, r, r, r, A.shLsl, w); + if w = 8 then A.aluShift(A.opOrr, r, r, r, A.shLsl, 16); end; + end; + if c then A.shiftI(A.shRor, r, r, INT(v), sz); else A.shift(A.shRor, r, r, y, sz); end; + if w < 32 then A.aluI(A.opAnd, r, r, (I64(1) shl w) - 1); end; + stack[depth - 1].k := kWord; end; + +(* CLZ, if leading, or CTZ of the unsigned integer of size bytes on top + (report section 6.1), whose bits above its width are zero. clz + counts the leading zeros of 32 or 64 bits, the whole width for 0; + of a U8 or a U16, those of 32 less the 24 or 16 above it. CTZ is + clz of the bits reversed (rbit), and of a U8 or a U16 a one bit set + just above its width first, so that 0 counts the width: + CLZ, a U8 CTZ, a U8 + clz w9, w9 orr x9, x9, #100H + sub x9, x9, #24 rbit w9, w9 + clz w9, w9 *) proc countZeros*(leading: BOOL; size: INT); +var r, w, sz: INT; begin - _ := leading; _ := size; - notYet("CLZ and CTZ"); + r := top(); + w := size * 8; + sz := 4; + if size = 8 then sz := 8; end; + if not leading and (w < 32) then A.aluI(A.opOrr, r, r, I64(1) shl w); end; + if not leading then A.rbit(r, r, sz); end; + A.clz(r, r, sz); + if leading and (w < 32) then A.aluI(A.opSub, r, r, 32 - w); end; + stack[depth - 1].k := kWord; end; + +(* POPCOUNT of the integer or set on top (report section 6.1), by the + vector instructions every arm64 has, as C's compilers make it: cnt + counts the ones in each of the eight bytes, and addv adds them: + fmov d23, x9 + cnt v23.8b, v23.8b + addv b23, v23.8b + fmov x9, d23 *) proc popCount*; +var r: INT; begin - notYet("POPCOUNT"); + r := top(); + A.fmovFR(XT, r, false); + A.cnt(XT, XT); + A.addv(XT, XT); + A.fmovRF(r, XT, false); + stack[depth - 1].k := kWord; end; -(* ABS of the signed integer on top: negated if negative, and cut back - to its width, so that the smallest value gives itself, as -x does - (Gen.x86_64.raz says more): +(* ABS of the real, or the signed integer, on top. A real's sign bit is + cleared, fabs, a NaN's too, as x86-64 clears it. An integer is + negated if negative, and cut back to its width, so that the smallest + value gives itself, as -x does (Gen.x86_64.raz says more): cmp x9, #0 cneg x9, x9, lt ; csneg x9, x9, x9, ge @@ -808,30 +939,43 @@ proc abs*(k: Kind; size: INT); var r: INT; begin - if k in {kF32, kF64} then notYet("reals"); end; r := top(); + if k in {kF32, kF64} then A.fop1(A.fAbs, r, r, k = kF32); return; end; A.cmpI(r, 0); A.csel(A.csNeg, A.ccGE, r, r, r); if size < 8 then A.extend(r, r, size, true); end; end; -(* The two values on top, compared: a BOOL, 1 or 0 *) +(* The two values on top, compared: a BOOL, 1 or 0. Two reals are + compared by fcmp, and the BOOL is made in the left's integer + register: fcmp d16, d17; cset x9, mi for < (CcReal) *) proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL); -var r, y, i: INT; +var r, x, y, i: INT; begin - if k in {kF32, kF64} then notYet("reals"); end; i := INT(sy) - INT(Scan.sEql); + if k in {kF32, kF64} then + y := loaded(depth - 1); + drop(); + x := top(); + drop(); + r := target(); + A.fcmp(x, y, k = kF32); + A.cset(CcReal[i], r); + computed(kWord); + return; + end; r, y := operands(); A.cmp(r, y); if signed then A.cset(CcSigned[i], r); else A.cset(CcUnsigned[i], r); end; stack[depth - 1].k := kWord; end; +(* -x; of a real, its sign bit flipped, fneg, as x86-64 flips it *) proc neg*(k: Kind; size: INT; signed: BOOL); var r: INT; begin - if k in {kF32, kF64} then notYet("reals"); end; r := top(); + if k in {kF32, kF64} then A.fop1(A.fNeg, r, r, k = kF32); return; end; A.neg(r, r); if size < 8 then A.extend(r, r, size, signed); end; end; @@ -860,24 +1004,53 @@ stack[depth - 1].k := kWord; end; +(* The integer on top, as a real of kind k, in its real register: scvtf + of the 64 bits, or ucvtf if big, a U64 that may be above high(I64), + which x86-64 halves and doubles to the same value *) proc intToReal*(k: Kind; big: BOOL); +var r: INT; begin - _ := big; - notYet("reals"); + r := top(); + A.scvtf(XRegs[(depth - 1) mod NumRegs], r, k = kF32, not big); stack[depth - 1].k := k; end; +(* The real of kind k on top, toward zero, as an I64 (report 6.1, the + table of conversions); out of an I64's range, or a NaN, it is the + smallest I64, as x86-64's cvttsd2si gives. fcvtzs gives the nearest + I64 instead, and 0 for a NaN, so the result is mended: a NaN is + unordered with itself, which sets V, and only the largest I64, which + no real converts to exactly, overflows when 1 is added, setting V: + + fcvtzs x9, d16 + mov x15, #8000000000000000H + fcmp d16, d16 + csel x9, x15, x9, vs ; a NaN + cmn x9, #1 + csel x9, x15, x9, vs ; too big + Too small is the smallest already. *) proc realToInt*(k: Kind); +var x, r: INT; begin - _ := k; - notYet("reals"); + x := top(); + r := Regs[(depth - 1) mod NumRegs]; + A.fcvtz(r, x, k = kF32, true); + A.movRI(T, MIN(I64)); + A.fcmp(x, x, k = kF32); + A.csel(A.csSel, A.ccVS, r, T, r); + A.cmpI(r, -1); + A.csel(A.csSel, A.ccVS, r, T, r); stack[depth - 1].k := kWord; end; +(* The real of kind k on top, as the other kind: fcvt, which rounds to + the nearest as x86-64's cvtsd2ss does *) proc realToReal*(k: Kind); +var x: INT; begin - notYet("reals"); - stack[depth - 1].k := k; + x := top(); + A.fcvt(x, x, k = kF64); + if k = kF32 then stack[depth - 1].k := kF64; else stack[depth - 1].k := kF32; end; end; (* Whether the slices in the frame at a and b (the offsets of their @@ -966,10 +1139,18 @@ if size < 8 then A.extend(r, r, size, signed); end; end; +(* NEW's memory, at the address on top, size bytes zeroed unless the + address is nil (the allocator had no room): cbz x9, L; then + zeroBytes from x0 *) proc zeroNew*(size: I64); +var L, r: INT; begin - _ := size; - notYet("NEW"); + r := top(); + L := NoLabel; + A.cbz(false, r, L); + A.movRR(Dst, r); + zeroBytes(size); + A.fixLink(L); end; (* CAS(v, old, new), XCHG(v, x) or XADD(v, x), from the stack, v's @@ -1378,11 +1559,74 @@ A.fixLink(out); end; +(* FILL: every element of the slice in the frame at dOff, of size bytes + each, set to the value on top if the element is a scalar, or else to + the size bytes at the address on top; n is how many elements there + are if that is known, as of an array, else -1 (Gen.x86_64.raz says + more). A constant zero into an array is its bytes zeroed + (zeroBytes). A scalar, a real's bits as an integer's, goes into x15, + and is stored into each element, by a store each into an array of + FewFills elements or fewer, else by a loop: + + ldr x0, [x29, #dOff] + ldr x2, [x29, #dOff + 8] + cbz x2, out + top: str w15, [x0], #4 ; a U32 + subs x2, x2, #1 + b.ne top + out: + + Any other element is copied into d[0], and from there forward, from + d[0] on to d[1] on, as copySlice copies, by words if it is 8 bytes + or more, else by bytes: each is read after it is written, size + bytes before, so d[0] repeats through the rest. + + ... ; d[0] := x, as for a copy, from x1 + ldr x1, [x29, #dOff] + add x0, x1, #size + ldr x2, [x29, #dOff + 8] + sub x2, x2, #1 + ... ; times size: the bytes of d[1..n] + ... ; copyRun(8), copyRun(1) *) +const FewFills = 16; + proc fillSlice*(dOff, size, n: I64; scalar: BOOL); +var c: BOOL; v, i: I64; out, top: INT; begin - _ := dOff; _ := size; _ := n; _ := scalar; - notYet("FILL"); - drop(); + c, v := topConst(); + out := NoLabel; + if scalar and c and (v = 0) and (n >= 0) then + drop(); + A.load(Dst, FP, INT(dOff), 8, false); + zeroBytes(n * size); + elsif scalar and (n >= 0) and (n <= FewFills) then + popTo(T); + A.load(Dst, FP, INT(dOff), 8, false); + for i := 0 to n - 1 do A.store(T, Dst, INT(i * size), INT(size)); end; + elsif scalar then + popTo(T); + A.load(Dst, FP, INT(dOff), 8, false); + A.load(Tmp, FP, INT(dOff) + 8, 8, false); + A.cbz(false, Tmp, out); + top := Chunk.pc; + A.storeIdx(T, Dst, INT(size), INT(size), A.mPost); + A.aluI(A.opSubs, Tmp, Tmp, 1); + A.jccBack(A.ccNE, top); + else + popTo(Src); + A.load(Dst, FP, INT(dOff), 8, false); + A.load(Tmp, FP, INT(dOff) + 8, 8, false); + A.cbz(false, Tmp, out); + copyBytes(size); + A.load(Src, FP, INT(dOff), 8, false); + A.aluI(A.opAdd, Dst, Src, size); + A.load(Tmp, FP, INT(dOff) + 8, 8, false); + A.aluI(A.opSub, Tmp, Tmp, 1); + mulI(Tmp, size); + if size >= 8 then copyRun(8, false); end; + copyRun(1, false); + end; + A.fixLink(out); end; (* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp. Its @@ -1418,23 +1662,27 @@ end; (* Calls. The arguments are the c.n values on top, each moved into the - next of x0 to x7 just before the call; the values under them are - spilled first, as the call may change their registers. As the stack's - registers carry no argument, the moves may go in any order. A slice - goes as its two words, both in registers or both on the stack, a - record by value as the address of a copy (Item makes it). The - arguments past the registers go on the machine's stack, 8 bytes - each, the first at sp, in an area sp is lowered by for the call, a - multiple of 16; and so do those past a C procedure's own parameters - where it ends in ..., in registers or not, as Apple's convention has - it (where a C procedure's own parameters past x7 narrower than 8 - bytes would be packed, which raz doesn't do). A result in memory, a - record, an array or several results, goes to the caller's temporary - c.ret, its address in x8. A single result comes back in x0, two words - in x0 and x1: a slice is stored in c.ret, two results pushed as two - values. *) + next of x0 to x7, or a real into the next of v0 to v7, just before + the call; the values under them are spilled first, as the call may + change their registers. As the stack's registers carry no argument, + the moves may go in any order. A slice goes as its two words, both + in registers or both on the stack, a record by value as the address + of a copy (Item makes it). The arguments past the registers go on + the machine's stack, the first at sp, in an area sp is lowered by + for the call, a multiple of 16; and so do those past a C procedure's + own parameters where it ends in ..., in registers or not, as Apple's + convention has it. Each takes 8 bytes, a real as its bits, but a C + procedure's own parameter of an integer, BOOL, enumeration, set or + real, which takes its size, at a multiple of it, as Apple packs + them: an I32 then an I64 go at sp and sp + 8, an I32 then an I16 at + sp and sp + 4. A result in memory, a record, an array or several + results, goes to the caller's temporary c.ret, its address in x8. A + single result comes back in x0, or d0, two words in x0 and x1: a + slice is stored in c.ret, two results pushed as two values. *) proc call*(var c: CallInfo); -var res: ^Sym.Type; i, ni, k, first, own, r: INT; area: I64; onStack: [64]BOOL; +var + res: ^Sym.Type; q: ^Sym.Obj; i, ni, nf, first, own, r: INT; area: I64; + onStack: [64]BOOL; at, size: [64]INT; (* where on the stack, and how wide *) begin first := depth - c.n; spillBelow(first); @@ -1444,27 +1692,39 @@ c.ret := allocPerm(res^.size, res^.align); (* kept, as on x86-64 (report 8) *) elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then c.ret := allocTemp(res^.size, res^.align); - elsif (res # nil) and (res^.form = Sym.fReal) then - notYet("reals"); end; own := c.n; if c.sig^.variadic then own := paramWords(c.sig); end; - ni := 0; k := 0; + ni := 0; nf := 0; area := 0; + q := c.sig^.fields; (* the parameter of argument i, while there is one *) for i := 0 to c.n - 1 do - if c.isF[i] then notYet("reals"); end; + size[i] := 8; + if c.ext and (q # nil) and not q^.isVar + and (q^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet, Sym.fReal}) then + size[i] := INT(q^.typ^.size); + end; + if (q # nil) and not c.pair[i] then q := q^.next; end; if i >= own then onStack[i] := true; + elsif c.isF[i] then onStack[i] := nf = 8; elsif (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1]; else onStack[i] := ni + 1 + INT(c.pair[i]) > 8; end; - if onStack[i] then INC(k); else INC(ni); end; + if onStack[i] then + area := Sym.alignUp(area, size[i]); + at[i] := INT(area); + INC(area, size[i]); + elsif c.isF[i] then INC(nf); + else INC(ni); end; end; - area := Sym.alignUp(8 * k, 16); + area := Sym.alignUp(area, 16); if area > 0 then A.aluI(A.opSub, SP, SP, area); end; - k := 0; ni := 0; + ni := 0; nf := 0; for i := 0 to c.n - 1 do if onStack[i] then valueTo(first + i, T); - A.store(T, SP, 8 * k, 8); - INC(k); + A.store(T, SP, at[i], size[i]); + elsif c.isF[i] then + xValueTo(first + i, nf); + INC(nf); else valueTo(first + i, ni); INC(ni); @@ -1489,6 +1749,9 @@ elsif Sym.inTwoRegs(res) then A.movRR(target(), 0); computed(kWord); A.movRR(target(), 1); computed(kWord); + elsif (c.ret = 0) and (res^.form = Sym.fReal) then + A.fop1(A.fMov, xTarget(), 0, false); + computed(kindOf(res)); elsif c.ret = 0 then (* C leaves the bits above a narrow result as they fall *) r := target(); @@ -1539,10 +1802,12 @@ (* The parameters' places: each in a frame slot of its own, stored there from its register on entry, or above the frame where the caller put it, past the frame record (call). A var parameter, and a record or - array passed by value, holds an address (ind). The address of a - result in memory arrives in x8, and is kept in retSlot. *) + array passed by value, holds an address (ind). A real comes in the + next of v0 to v7, and the ninth on, as call puts them, on the stack. + The address of a result in memory arrives in x8, and is kept in + retSlot. *) proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj); -var p: ^Sym.Obj; ni, k, w: INT; stackOff: I64; +var p: ^Sym.Obj; ni, nf, k, w: INT; stackOff: I64; f: BOOL; begin frameTop := 0; frameMax := 0; permTop := 0; resetStack(0, 0); @@ -1557,18 +1822,20 @@ inReg[0] := 8; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; numIn := 1; end; - ni := 0; + ni := 0; nf := 0; stackOff := 16; p := params; while p # nil do p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice)); + f := (p^.typ^.form = Sym.fReal) and not p^.isVar; k := 1; if (p^.typ^.form = Sym.fSlice) and not p^.isVar then k := 2; end; - if (p^.typ^.form = Sym.fReal) and not p^.isVar then - notYet("a real parameter"); + if f and (nf < 8) then p^.val := allocPerm(8, 8); + inReg[numIn] := -1 - nf; inSlot[numIn] := p^.val; inSize[numIn] := 8; inObj[numIn] := p; + INC(numIn); INC(nf); k := 0; - elsif ni + k > 8 then (* on the stack, as call puts it *) + elsif f or (ni + k > 8) then (* on the stack, as call puts it *) p^.val := stackOff; INC(stackOff, 8 * k); k := 0; @@ -1615,16 +1882,42 @@ zeroTop := permTop; regMask := mask; k := 0; - for i := 0 to 4 do + for i := 0 to XBit + 7 do if (mask & (1 shl i)) # 0 then INC(k); end; end; if k > 0 then saveAt := allocPerm(8 * k, 8); end; frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; +end; + +(* The registers of variables in regMask, saved from saveAt on, a + quadword each, x19 to x23 first, then d8 to d15, of which a called + procedure keeps the low 64 bits, and so must give them back; and + each zeroed, as a variable starts at zero. Or, if back, restored. *) +proc saveRegs(back: BOOL); +var i, n, r: INT; +begin + n := 0; + for i := 0 to XBit + 7 do + if (regMask & (1 shl i)) # 0 then + if i < XBit then + r := VarRegs[i]; + if back then A.load(r, FP, INT(saveAt) + 8 * n, 8, false); + else A.store(r, FP, INT(saveAt) + 8 * n, 8); A.movRI(r, 0); end; + else + r := XVarRegs[i - XBit] - XmmVar; + if back then A.loadF(r, FP, INT(saveAt) + 8 * n, 8); + else A.storeF(r, FP, INT(saveAt) + 8 * n, 8); A.fmovFR(r, ZR, false); end; + end; + INC(n); + end; + end; end; +(* The variable in register r of XVarRegs has a slot of 8 bytes at + slot, which popVar writes as it writes r *) proc xHome*(r: INT; slot: I64); begin - _ := r; _ := slot; + homes[r - XVarRegs[0]] := slot; end; proc markCode*(var m: CodeMark); @@ -1667,7 +1960,6 @@ begin Chunk.beginChunk(c); codeChunk := c; - if LEN(pending) > 0 then Chunk.setMissing(c, pending, pendingAt); pending := ""; end; Chunk.noteLine(line, 1); (* a declaration starts a line *) A.stp(FP, LR, SP, -16, A.mPre); A.movRR(FP, SP); @@ -1685,18 +1977,13 @@ A.aluI(A.opSubs, U, U, 1); A.jccBack(A.ccNE, loop); end; - if (regMask shr XBit) # 0 then notYet("reals in registers"); end; - Chunk.setSaves(c, regMask, INT(saveAt)); - n := 0; - for i := 0 to 4 do - if (regMask & (1 shl i)) # 0 then - A.store(VarRegs[i], FP, INT(saveAt + 8 * n), 8); - A.movRI(VarRegs[i], 0); - INC(n); - end; - end; + Chunk.setSaves(c, regMask & ((1 shl XBit) - 1), INT(saveAt)); + saveRegs(false); for i := 0 to numIn - 1 do - if (inObj[i] # nil) and (inObj[i]^.reg # 0) then + if inReg[i] < 0 then (* a real, from v0 to v7 *) + A.storeF(-1 - inReg[i], FP, INT(inSlot[i]), 8); + if inObj[i]^.reg # 0 then A.fop1(A.fMov, inObj[i]^.reg - XmmVar, -1 - inReg[i], false); end; + elsif (inObj[i] # nil) and (inObj[i]^.reg # 0) then r := inObj[i]^.reg; if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end; A.movRR(r, inReg[i]); @@ -1711,7 +1998,9 @@ p := procParams; while p # nil do if (p^.reg # 0) and (p^.val > 0) then - if not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) then + if not p^.ind and (p^.typ^.form = Sym.fReal) then + A.loadF(p^.reg - XmmVar, FP, INT(p^.val), INT(p^.typ^.size)); + elsif not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) then A.load(p^.reg, FP, INT(p^.val), INT(p^.typ^.size), p^.typ^.signed); else A.load(p^.reg, FP, INT(p^.val), 8, false); @@ -1730,12 +2019,13 @@ (* Pops one of several results, a value of size bytes, into its place off bytes into the caller's, at the address that came in x8 *) proc resultTo*(off: I64; size: INT); -var r: INT; +var r: INT; fl: BOOL; begin r := top(); + fl := isReal(depth - 1); drop(); A.load(U, FP, INT(retSlot), 8, false); - A.store(r, U, INT(off), size); + if fl then A.storeF(r, U, INT(off), size); else A.store(r, U, INT(off), size); end; end; (* Pops an address, and copies size bytes from it to the result, off @@ -1748,11 +2038,14 @@ copyBytes(size); end; -(* return: the result, a value on top, goes in x0; two words, the two - values on top, in x0 and x1; one in memory is there already *) +(* return: the result, a value on top, goes in x0, or d0; two words, + the two values on top, in x0 and x1; one in memory is there already *) proc returnJump*; begin - if (resType # nil) and not Sym.behindPointer(resType) then + if (resType # nil) and (resType^.form = Sym.fReal) then + xValueTo(depth - 1, 0); + drop(); + elsif (resType # nil) and not Sym.behindPointer(resType) then if Sym.inTwoRegs(resType) then popTo(1); end; popTo(0); end; @@ -1816,7 +2109,7 @@ frame's size, known only now, patched into its prologue. If it is open, running off its end, it returns zeros. *) proc endProc*(c: INT; open: BOOL): ERROR; -var n: I64; i: INT; +var n: I64; begin codeChunk := -1; if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end; @@ -1826,17 +2119,12 @@ elsif open then A.movRI(0, 0); if Sym.inTwoRegs(resType) then A.movRI(1, 0); end; + A.fmovFR(0, ZR, false); else A.dropJumpTo(exitL); end; A.fixLink(exitL); - n := 0; - for i := 0 to 4 do - if (regMask & (1 shl i)) # 0 then - A.load(VarRegs[i], FP, INT(saveAt + 8 * n), 8, false); - INC(n); - end; - end; + saveRegs(true); A.movRR(SP, FP); A.ldp(FP, LR, SP, 16, A.mPost); A.ret(); @@ -1964,18 +2252,33 @@ return nil; end; -(* A code procedure that is one instruction on a real, as Math.sqrt on - x86-64, is made in place of its call there; none is, yet, here *) -proc oneRealOp*(c: INT): INT; +(* Whether the code procedure in chunk c is a square root of d0, or of + s0 if single, in itself, then ret, as Math.sqrt is, which a call of + it does in place (realInPlace), as on x86-64: its first word, or 0. *) +const FsqrtD = 1E61C000H; FsqrtS = 1E21C000H; Ret = 0D65F03C0H; + +proc oneRealOp*(c: INT; single: BOOL): INT; +var s, i, w, want: INT; words: [2]INT; begin - _ := c; + s := Chunk.chunks[c].start; + if (Chunk.chunks[c].size # 8) or (Chunk.chunks[c].numFix # 0) then return 0; end; + for w := 0 to 1 do + words[w] := 0; + for i := 3 to 0 by -1 do words[w] := words[w] shl 8 | INT(Chunk.text[s + 4 * w + i]); end; + end; + want := FsqrtD; + if single then want := FsqrtS; end; + if (words[0] = want) and (words[1] = Ret) then return want; end; return 0; end; +(* The square root of oneRealOp on the real on top, in its register: + Math.sqrt(d2) is fsqrt d16, d16, with no call *) proc realInPlace*(op: INT); +var x: INT; begin - _ := op; - notYet("reals"); + x := top(); + A.fop1(A.fSqrt, x, x, op = FsqrtS); end; var i: INT; @@ -1983,7 +2286,6 @@ for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; Chunk.setPad(0D4200020H); (* brk #1, a trap, between procedures *) codeChunk := -1; - pending := ""; resetStack(0, 0); rtFail := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr("fail")); argcChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argc")); diff --git a/src/Gen.x86_64.raz b/src/Gen.x86_64.raz index 5ef16e6..49b2f98 100644 --- a/src/Gen.x86_64.raz +++ b/src/Gen.x86_64.raz @@ -2816,7 +2816,7 @@ end; (* Whether the code procedure in chunk c is a square root of xmm0 in - itself, then ret, as Math.sqrt is: + itself, an F32's if single, then ret, as Math.sqrt is: sqrtsd xmm0, xmm0 F2 0F 51 C0 ret C3 @@ -2825,12 +2825,14 @@ on the value's own register (realInPlace); else 0. A call would spill the stack's reals and reload the variables' xmm registers around it, as System V keeps none through one, for an instruction of four bytes. *) -proc oneRealOp*(c: INT): INT; -var s: INT; +proc oneRealOp*(c: INT; single: BOOL): INT; +var s, pfx: INT; begin s := Chunk.chunks[c].start; if (Chunk.chunks[c].size # 5) or (Chunk.chunks[c].numFix # 0) then return 0; end; - if ((Chunk.text[s] # 0F2H) and (Chunk.text[s] # 0F3H)) or (Chunk.text[s + 1] # 0FH) + pfx := 0F2H; + if single then pfx := 0F3H; end; (* sqrtss *) + if (INT(Chunk.text[s]) # pfx) or (Chunk.text[s + 1] # 0FH) or (Chunk.text[s + 2] # 51H) or (Chunk.text[s + 3] # 0C0H) or (Chunk.text[s + 4] # 0C3H) then return 0; end; diff --git a/src/Parse.raz b/src/Parse.raz index b68f1b3..09e3465 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3606,8 +3606,7 @@ q := sig^.fields; if (q # nil) and (q^.next = nil) and not q^.isVar and (q^.typ^.form = Sym.fReal) and (sig^.base # nil) and Sym.same(sig^.base, q^.typ) then - p^.inl := Gen.oneRealOp(p^.chunk); - if (p^.inl shr 8 = 0F3H) # (q^.typ^.size = 4) then p^.inl := 0; end; + p^.inl := Gen.oneRealOp(p^.chunk, q^.typ^.size = 4); end; return expect(Scan.sEnd); end; diff --git a/test/a64/Api.s b/test/a64/Api.s index 6c8652a..2be7f35 100644 --- a/test/a64/Api.s +++ b/test/a64/Api.s @@ -167,6 +167,8 @@ fmov s2, w3 fmov x4, d5 fmov w6, s7 +cnt v16.8b, v17.8b +addv b18, v19.8b // addresses, calls adr x0, . adr x1, .-4 diff --git a/test/a64/Encode.expect b/test/a64/Encode.expect index ddc0c4c..98ff057 100644 --- a/test/a64/Encode.expect +++ b/test/a64/Encode.expect @@ -388,6 +388,8 @@ 1e270062 9e6600a4 1e2600e6 +0e205a30 +0e31ba72 10000000 10ffffe1 107fffe2 @@ -406,7 +408,7 @@ f9400205 94000000 14000000 -97fffe68 +97fffe66 d63f00a0 d61f00c0 d65f03c0 @@ -425,10 +427,10 @@ 54000043 14000001 aa0203e1 -nop * 1 -547fca81 -b47fca61 -nop * 261714 +nop * 3 +547fca01 +b47fc9e1 +nop * 261710 54800000 reloc 000000000000032c ARM64_RELOC_BRANCH26 _proc1 reloc 0000000000000330 ARM64_RELOC_BRANCH26 _proc1 @@ -436,8 +438,6 @@ reloc 0000000000000338 ARM64_RELOC_PAGEOFF12 _glob reloc 000000000000033c ARM64_RELOC_PAGE21 _proc1 reloc 0000000000000340 ARM64_RELOC_PAGEOFF12 _proc1 -reloc 0000000000000624 ARM64_RELOC_PAGE21 _glob -reloc 0000000000000628 ARM64_RELOC_PAGEOFF12 _glob reloc 000000000000062c ARM64_RELOC_PAGE21 _glob reloc 0000000000000630 ARM64_RELOC_PAGEOFF12 _glob reloc 0000000000000634 ARM64_RELOC_PAGE21 _glob @@ -446,7 +446,9 @@ reloc 0000000000000640 ARM64_RELOC_PAGEOFF12 _glob reloc 0000000000000644 ARM64_RELOC_PAGE21 _glob reloc 0000000000000648 ARM64_RELOC_PAGEOFF12 _glob -reloc 000000000000064c ARM64_RELOC_PAGE21 _byte -reloc 0000000000000650 ARM64_RELOC_PAGEOFF12 _byte -reloc 0000000000000658 ARM64_RELOC_BRANCH26 _proc1 -reloc 000000000000065c ARM64_RELOC_BRANCH26 _proc1 +reloc 000000000000064c ARM64_RELOC_PAGE21 _glob +reloc 0000000000000650 ARM64_RELOC_PAGEOFF12 _glob +reloc 0000000000000654 ARM64_RELOC_PAGE21 _byte +reloc 0000000000000658 ARM64_RELOC_PAGEOFF12 _byte +reloc 0000000000000660 ARM64_RELOC_BRANCH26 _proc1 +reloc 0000000000000664 ARM64_RELOC_BRANCH26 _proc1 diff --git a/test/a64/Encode.raz b/test/a64/Encode.raz index ea2e931..27600b2 100644 --- a/test/a64/Encode.raz +++ b/test/a64/Encode.raz @@ -187,6 +187,8 @@ A.fmovFR(2, 3, true); A.fmovRF(4, 5, false); A.fmovRF(6, 7, true); + A.cnt(16, 17); + A.addv(18, 19); (* addresses, calls *) A.adr(0, 0); A.adr(1, -4); diff --git a/test/mac.sh b/test/mac.sh index e7e46aa..f3af30d 100755 --- a/test/mac.sh +++ b/test/mac.sh @@ -9,7 +9,8 @@ # Then conform/run's programs the same way, into build/mac/conform, # with their arguments (X.args): until the port is whole some fail, # which are counted and listed in build/mac/conform.fail, not failures -# of the run. +# of the run. A few of conform/run are not for this target, each named +# in notHere with the reason, and skipped. # # test/mac.sh build in the container: the cross compiler, then the programs # test/mac.sh run on the Mac: each program, run with a timeout @@ -19,6 +20,8 @@ # written again in place with another one. set -e out=build/mac +# conform/run's programs that are not for darwin-arm64, and why +notHere='SystemImport: SYSTEM.SYSCALL is Linux'"'"'s only (report 6.8)' case $1 in build) src/cross.sh darwin-arm64 @@ -62,9 +65,11 @@ c=$out/conform cpass=0 cfail=0 + cskip=0 : > $c.fail for f in conform/run/*.raz; do name=$(basename $f .raz) + if echo "$notHere" | grep -q "^$name:"; then cskip=$((cskip + 1)); continue; fi want=0 [ -f conform/run/$name.status ] && want=$(cat conform/run/$name.status) if [ ! -x $c/$name ]; then @@ -87,7 +92,7 @@ cpass=$((cpass + 1)) fi done - echo "conform/run: $cpass of $((cpass + cfail)) passed; the others in $c.fail" + echo "conform/run: $cpass of $((cpass + cfail)) passed, $cskip not for this target; the others in $c.fail" [ $fail = 0 ] ;; *) diff --git a/test/mac/Reals.expect b/test/mac/Reals.expect new file mode 100644 index 0000000..b931e76 --- /dev/null +++ b/test/mac/Reals.expect @@ -0,0 +1,22 @@ +nan inf -inf -0 0 0 -inf +FTFFFF FTFFFF FTFFFF TFFTFT TFFTFT +FTFFFF FTFFFF TFFTFT +nan nan F +-9223372036854775808 0 0 0 -9223372036854775808 +-9223372036854775808 0 0 0 -9223372036854775808 +-9223372036854775808 0 0 0 -9223372036854775808 +-9223372036854775808 0 0 0 -9223372036854775808 +-9223372036854775808 0 0 0 -9223372036854775808 +-9223372036854775808 0 0 0 -9223372036854775808 +-9223372036854775808 0 0 0 -9223372036854775808 +9223372036854774784 -1024 -1024 0 -9223372036854775808 +-9223372036854775808 0 0 0 -9223372036854775808 +2 2 2 2 2 +-2 -2 -2 254 -2 +70000 70000 4464 112 70000 +0 0 0 0 0 +300 300 300 44 300 +0 0 0 0 0 +0.10000000149011612 16777216 0.30000001192092896 0.020000001415610313 0.5 -0.10000000149011612 inf 0 -0 inf 1.5 +1.8446744073709552E19 1.8446744073709552E19 9.223372036854778E18 -9.223372036854776E18 16777216 -7 +430.25 -1.25 2.5 -22 -3.5 0 diff --git a/test/mac/Reals.raz b/test/mac/Reals.raz new file mode 100644 index 0000000..1e7c27a --- /dev/null +++ b/test/mac/Reals.raz @@ -0,0 +1,104 @@ +(* Reals at their edges, as x86-64 gives them: a NaN equal to nothing, + a real out of an I64's range or a NaN converted to the smallest I64, + then cut; -0.0; F32's rounding; a U64 above high(I64); reals as + arguments past v7, results, in records, slices and registers *) +module Reals; +import Out, Fmt; + +type Pt = record x: F64; y: F32; n: INT; end; + +var zero, one: F64; + +proc real(x: F64); +var b: [Fmt.ShortMost]U8; +begin + Out.str(Fmt.short(x, b)); Out.str(" "); +end; + +proc bool(b: BOOL); +begin + if b then Out.str("T"); else Out.str("F"); end; +end; + +(* every comparison of a and b *) +proc compare(a, b: F64); +begin + bool(a = b); bool(a # b); bool(a < b); bool(a <= b); bool(a > b); bool(a >= b); + Out.str(" "); +end; + +proc compare32(a, b: F32); +begin + bool(a = b); bool(a # b); bool(a < b); bool(a <= b); bool(a > b); bool(a >= b); + Out.str(" "); +end; + +(* x converted to each integer width *) +proc toInt(x: F64); +begin + Out.int(I64(x)); Out.str(" "); Out.int(I64(I32(x))); Out.str(" "); + Out.int(I64(I16(x))); Out.str(" "); Out.int(I64(U8(x))); Out.str(" "); + Out.int(I64(F32(x))); Out.ln; +end; + +(* ten reals and ten integers: the ninth and tenth of each on the stack *) +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; +begin + return a + b * 2.0 + c * 3.0 + d * 4.0 + e * 5.0 + f * 6.0 + g * 7.0 + h * 8.0 + + i * 9.0 + j * 10.0 + F64(k + l + m + n + o + p + q + r + t) + F64(s); +end; + +proc half(x: F32): F32; +begin + return x / 2.0; +end; + +proc swapped(p: Pt): (F32, F64); +begin + return p.y, p.x; +end; + +proc main*; +var + nan, inf, nz, x, sum: F64; + f, g: F32; + u: U64; + p: Pt; + a: [5]F64; + s: []F64; + i: INT; +begin + nan := zero / zero; + inf := one / zero; + nz := -zero; + real(nan); real(inf); real(-inf); real(nz); real(ABS(nz)); real(-nz); real(one / nz); Out.ln; + compare(nan, nan); compare(nan, one); compare(one, nan); compare(one, one); compare(nz, zero); Out.ln; + f := F32(nan); g := 1.5; + compare32(f, g); compare32(g, f); compare32(g, g); Out.ln; + real(ABS(nan)); real(-nan); bool(ABS(nan) = ABS(nan)); Out.ln; + toInt(nan); toInt(inf); toInt(-inf); toInt(1.0E300); toInt(-1.0E300); + toInt(9.3E18); toInt(-9.3E18); toInt(9.2233720368547748E18); toInt(-9.2233720368547758E18); + toInt(2.75); toInt(-2.75); toInt(70000.9); toInt(-0.5); toInt(300.0); toInt(nz); + (* F32's rounding *) + f := F32(0.1); real(F64(f)); f := F32(16777217.0); real(F64(f)); + f := 0.1; g := 0.2; real(F64(f + g)); real(F64(f * g)); real(F64(f / g)); real(F64(f - g)); + f := F32(1.0E39); real(F64(f)); f := F32(1.0E-46); real(F64(f)); real(F64(F32(-1.0E-46))); + g := 3.4028234E38; real(F64(g * 2.0)); real(F64(half(3.0))); Out.ln; + (* integers to reals *) + u := MAX(U64); real(F64(u)); real(F64(F32(u))); u := 8000000000000401H; real(F64(u)); + real(F64(MIN(I64))); i := 16777217; real(F64(F32(i))); real(F64(I8(-7))); Out.ln; + (* calls, records, slices, registers *) + 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)); + p.x := 2.5; p.y := -1.25; p.n := 3; + f, x := swapped(p); real(F64(f)); real(x); + for i := 0 to 4 do a[i] := F64(i) * 0.5; end; + s := a[1..5]; + FILL(s[2..4], -3.5); + sum := 0.0; + for i := 0 to LEN(s) - 1 do sum := sum + s[i] * F64(i + 1); end; + real(sum); real(a[3]); real(a[0]); Out.ln; +end; + +begin + one := 1.0; +end Reals. diff --git a/test/mac/Rotate.expect b/test/mac/Rotate.expect new file mode 100644 index 0000000..c449ec5 --- /dev/null +++ b/test/mac/Rotate.expect @@ -0,0 +1,11 @@ +81 81 8001 8001 80000001 80000001 8000000000000001 8000000000000001 +3 C0 3 C000 3 C0000000 3 C000000000000000 +C 30 C 3000 C 30000000 C 3000000000000000 +81 81 180 180 180 1800000 180 180000000000000 +30 C 3000 C 3000 C0000 3000 C000000000000 +C0 3 C000 3 C0000000 3 C000000000000000 3 +C 30 300 C0 3 3 3 8000000000000001 +4B 4B4B F 0 +8 8 0 16 16 0 32 32 0 64 64 0 +3 3 2 3 11 2 11 19 2 27 35 2 +0 0 8 0 0 16 0 0 32 0 0 64 diff --git a/test/mac/Rotate.raz b/test/mac/Rotate.raz new file mode 100644 index 0000000..d4a1fdc --- /dev/null +++ b/test/mac/Rotate.raz @@ -0,0 +1,40 @@ +(* ROL and ROR at every width, by constants and by variables, negative + and past the width; CLZ, CTZ and POPCOUNT at every width, as x86-64 + gives them *) +module Rotate; +import Out; + +proc show(n: U64); +begin + Out.hex(n, 1); Out.str(" "); +end; + +proc main*; +var + b: U8; h: U16; w: U32; q: U64; n, i: INT; + counts: [6]INT; +begin + counts := [0, 1, 3, 8, 13, -1]; + b := 81H; h := 8001H; w := 80000001H; q := 8000000000000001H; + for i := 0 to 5 do + n := counts[i]; + show(U64(ROL(b, n))); show(U64(ROR(b, n))); show(U64(ROL(h, n))); show(U64(ROR(h, n))); + show(U64(ROL(w, n))); show(U64(ROR(w, n))); show(U64(ROL(q, n))); show(U64(ROR(q, n))); + Out.ln; + end; + show(U64(ROL(b, 3))); show(U64(ROR(b, 3))); show(U64(ROL(h, 9))); show(U64(ROR(h, 9))); + show(U64(ROL(w, 33))); show(U64(ROR(w, -1))); show(U64(ROL(q, 65))); show(U64(ROR(q, 0))); Out.ln; + b := 0B4H; h := 0B4B4H; w := 0F0000H; q := 0; + 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; + b := 0; h := 0; w := 0; q := 0; + for i := 0 to 2 do + Out.int(CLZ(b)); Out.str(" "); Out.int(CTZ(b)); Out.str(" "); Out.int(POPCOUNT(b)); Out.str(" "); + Out.int(CLZ(h)); Out.str(" "); Out.int(CTZ(h)); Out.str(" "); Out.int(POPCOUNT(h)); Out.str(" "); + Out.int(CLZ(w)); Out.str(" "); Out.int(CTZ(w)); Out.str(" "); Out.int(POPCOUNT(w)); Out.str(" "); + Out.int(CLZ(q)); Out.str(" "); Out.int(CTZ(q)); Out.str(" "); Out.int(POPCOUNT(q)); Out.ln; + b := b + 18H; h := h + 1800H; w := w + 180000H; q := q + 1800000000H; + if i = 1 then b := 0FFH; h := 0FFFFH; w := 0FFFFFFFFH; q := MAX(U64); end; + end; +end; + +end Rotate. diff --git a/test/mac/Sqrt.expect b/test/mac/Sqrt.expect new file mode 100644 index 0000000..f7b7d10 --- /dev/null +++ b/test/mac/Sqrt.expect @@ -0,0 +1 @@ +1.4142135623730951 5 1.4142135381698608 nan diff --git a/test/mac/Sqrt.raz b/test/mac/Sqrt.raz new file mode 100644 index 0000000..19cf77e --- /dev/null +++ b/test/mac/Sqrt.raz @@ -0,0 +1,35 @@ +(* A code procedure that is a square root of d0 and ret is made in + place of its call (Gen.oneRealOp), as Math.sqrt is on x86-64; F32's + too. Its own here, as lib/Math.raz has x86-64's yet. *) +module Sqrt; +import Out, Fmt; +import SYSTEM; + +proc root(x: F64): F64; +code + fsqrt d0, d0; + ret; +end root; + +proc root32(x: F32): F32; +code + fsqrt s0, s0; + ret; +end root32; + +proc real(x: F64); +var b: [Fmt.ShortMost]U8; +begin + Out.str(Fmt.short(x, b)); Out.str(" "); +end; + +proc main*; +var x: F64; f: F32; +begin + x := 2.0; + real(root(x)); real(root(x * 8.0) + 1.0); + f := 2.0; + real(F64(root32(f))); real(root(-x)); Out.ln; +end; + +end Sqrt.