From 489d88109fc91f2ae7f3b2acbc375ccdf98ae62d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:41:40 +0000 Subject: [PATCH] src/A64.raz: arm64's machine code, X64's twin; test/a64 PLAN.md 5, step 4. The encoder Gen.arm64 will use (constants as Apple's assembler makes them, the ALU, loads and stores, branches within a procedure, LSE atomics, reals) and the assembler of code procedures, in Arm's syntax: bl p, b p, adr xN, v (adrp and add). Chunk gains the fixups fxBranch26, fxPage21 and fxPageOff12, which Link patches from step 6. test/a64 checks 428 words and 20 relocations against Apple's cc and objdump, expect.sh making the expectation on the Mac. x86-64's output is unchanged; the fixpoint holds. --- diff --git a/PLAN.md b/PLAN.md index 893b2b2..7d10b9f 100644 --- a/PLAN.md +++ b/PLAN.md @@ -220,8 +220,19 @@ 3. Done. `Gen.raz` to `Gen.x86_64.raz`, `Elf.raz` to `Exe.linux.raz` (the unit `Exe`), `Host.raz` to `Host.linux.raz`, `src/cross.sh`: what Linux's compiler makes unchanged, byte for byte. -4. `A64.raz`, its encoder checked against `llvm-mc`'s bytes. -5. `Gen.arm64.raz`, in pieces: values and operations; jumps and +4. Done. `A64.raz`: arm64's encoder, jumps within a procedure, and + the assembler in Arm's syntax (`bl p`, `b p`, `adr xN, v` for names), + checked against Apple's assembler (clang's, `llvm-mc`'s bytes) by + `test/a64`; Chunk's arm64 fixup kinds, `fxBranch26`, `fxPage21` and + `fxPageOff12`, and `Scan.setBang` for a pre-index's `!`. Left for + later: `Link`'s patching of those kinds comes with step 6; no + compiler imports `A64` until step 5's `Gen.arm64.raz`, and + `Chunk.beginChunk` still pads code with x86's `int3`, which arm64's + Gen will want as `brk`. +5. `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 checks; calls and frames; reals; atomics, deferred statements, the entry point. 6. `Exe.darwin.raz` and `Link`'s rebases, its line numbers for lldb; diff --git a/doc/compiler.md b/doc/compiler.md index e5d3e7b..47c94e9 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,7 +1,8 @@ # Reading the raz compiler -The compiler is twelve files of raz, about 13,200 lines of code (the -budget is 18,000). It is its own compiler: a release of it, the one +The compiler is thirteen files of raz, about 14,600 lines of code (the +budget is 18,000); one of them, `A64.raz`, arm64's encoder, waits for +the back end that will use it (`PLAN.md` 5). It is its own compiler: a release of it, the one `BOOT` names, builds `src/`, and `make fixpoint` checks that what it makes makes itself again, byte for byte. Each release was built by the one before it, back to v0.19.0, which the Pascal compiler it was ported @@ -17,10 +18,11 @@ | File | Lines | Code | What | |---|---|---|---| | `src/Diag.raz` | 479 | 363 | Messages: positions, source excerpts, colour, counts | -| `src/Scan.raz` | 1,267 | 1,040 | Symbols, literals, comments, positions | +| `src/Scan.raz` | 1,279 | 1,048 | Symbols, literals, comments, positions | | `src/Sym.raz` | 866 | 689 | Types, objects, scopes, layout | -| `src/Chunk.raz` | 364 | 280 | The compiled program: chunks, fixups, line notes | +| `src/Chunk.raz` | 389 | 288 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,777 | 1,450 | The instruction encoder, jumps within a procedure, the assembler | +| `src/A64.raz` | 1,848 | 1,365 | The same for arm64, not yet imported: the encoder, jumps, the assembler | | `src/Gen.x86_64.raz` | 2,860 | 1,998 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,938 | 4,191 | Declarations, statements, expressions, built-ins, recovery, warnings | @@ -51,7 +53,7 @@ each with the kind of the value (a word, a 64-bit integer, an `F32`, an `F64`). `Gen` is the only one that knows x86-64; a port to another machine is a new `Gen`, with an encoder of its own in place -of `X64.raz`. Likewise `Exe` is the only one that knows the executable's +of `X64.raz`, as `A64.raz` is arm64's. Likewise `Exe` is the only one that knows the executable's format, and `Host` the host's services. Each is a unit with a file per target, which the suffixes of the report's section 3 choose (`Gen.x86_64.raz`, `Exe.linux.raz`, `Host.linux.raz`); `Gen.Arch` and @@ -1146,7 +1148,14 @@ A *fixup* (`Fixup`) is a 32-bit field in the code that will hold the distance to another chunk: a call's target, or a rip-relative address. -`addFixup` records one; `Link.raz` fills them in. A constant's addresses +`addFixup` records one; `Link.raz` fills them in. arm64's fixups +(`addKindFixup`) are fields of an instruction, of a kind each +(`FixKind`): `fxBranch26`, a `bl`'s or `b`'s distance in words; +`fxPage21`, an `adrp`'s distance in pages of 4 KB; `fxPageOff12`, an +address's place in its page, in an `add` or, divided by the size the +instruction reads, a load's or store's offset; as the Mach-O relocations +of the same names. `Link.raz` patches only x86-64's yet; arm64's come +with the Mac's executable. A constant's addresses are *data fixups*, `addDataFixup`'s: 64-bit fields in rodata that will hold a chunk's address itself, since the executable is loaded where the linker placed it. `noteLine` records which line and column of the source @@ -1267,6 +1276,106 @@ the next instruction; the line notes made after it move back with it (`takeBack`, in `Chunk.raz`). +## A64.raz: arm64's machine code + +X64's twin, for arm64: what `Gen.arm64.raz` will call, and the +assembler of a code procedure on the Mac. No compiler imports it yet; +`test/a64` checks every routine against Apple's assembler. + +Every instruction is one word of 32 bits, which `emit` writes. A +routine builds the word from its fields with shifts and ors, and its +comment draws the fields as Arm's manual does, most significant first: +`sf op S 100010 sh imm12 Rn Rd` for add with an immediate. Registers +are numbers: x0 to x30, and 31, which an instruction reads as sp or as +the zero register by its kind (`SP` and `ZR` are both 31, and each +routine says which it takes); the reals' v0 to v31, with a size, s, d +or q. x18 is Apple's and never named; x16 is the encoder's own: a +routine given an immediate or an offset its instruction can't hold +builds it in x16 first, so Gen needn't know the limits. + +The routines, by family: `movRI` makes a constant as Apple's assembler +would, a `movz` or `movn` when one halfword of 16 bits differs from +zeros or ones, an `orr` of a logical immediate (`bitmask`, a run of +ones rotated and repeated), else a `movz` or `movn` and a `movk` for +each other halfword; `movRR`, `alu`, `aluShift`, `aluI` (an add of 12 +bits, of 12 bits shifted by 12, two adds below 2^24, else through x16; +a negative one the opposite operation), `cmp`, `cmpI`, `neg`, `mvn`; +`shift`, `shiftI` (every shift by a constant is a `ubfm`, `sbfm` or +`extr`), `field`, `extend`; `mul`, `madd`, `mulHigh`, `divide`, `clz`, +`rbit`, `csel`, `cset`; `load`, `store`, `loadR`, `storeR`, `loadIdx`, +`storeIdx`, `loadF`, `storeF` (one routine, `ldst`, from bits that tell +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 +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`. + +*Other chunks.* macOS loads a program anywhere, so every address is +made from the code's own: `addrChunk` is `adrp` of the chunk's page and +`add` of its place in it, each with a fixup (`Chunk.raz`); `loadChunk` +and `storeChunk`, and their reals' twins, put the page in x16 and the +place in the load's offset, which the linker divides by the size it +reads in the instruction; when the chunk's alignment doesn't make the +place a multiple of the size, the address is made whole first. A C +symbol's quadword is read so. `callChunk` and `jmpChunk` are `bl` and +`b` with a fixup, within 128 MB. + +*Jumps within a procedure* are chains, as in X64, but the field that +links a jump to the one before holds that one's place in words from the +procedure's chunk, plus one, 0 ending the chain, since the fields are +narrow: 26 bits in `b`, 19 in `b.cond`, `cbz` and `cbnz` (1 MB either +way), 14 in `tbz` and `tbnz` (32 KB). A procedure of more than 1 MB of +code, a quarter of a million instructions, is an error, rather than +every conditional jump two instructions for a procedure no one writes. +Gen's `tbz`, which a long procedure would outrun, is the opposite `tbz` +over a `b`, two instructions that reach as far as `b`; the assembler's +is one, in a code procedure that is never that long. `fixLink`, +`fixLinkTo`, `dropJumpTo` and `joinLinks` are X64's. + +*The assembler*, `assemble`, has X64's interface: the parser's +`ConstFn` and `NameFn`, labels found first (`findLabels`), and the +listing. It reads Arm's syntax, destination first: `add x0, x1, #16`, +`ldr w2, [x0, #4]`, `ldr x1, [x2, x3, lsl #3]`, `stp x29, x30, [sp, +#-16]!`, `ldr x0, [sp], #16`, `b.ne loop`, `csel x0, x1, x2, lt`, +`dmb ish`. The `#` is raz's "not equal", read and passed over, and may +be left out; numbers are raz's, `10H` and not `0x10`; `!` is a slip for +`not` elsewhere, and a symbol of its own while `assemble` runs +(`Scan.setBang`). A procedure or a module variable stands for its +address as in X64, in three places, written as Arm writes them: + + bl triple 94000000 fxBranch26 to triple + b AsmNamed.twice 14000000 fxBranch26 to AsmNamed.twice + adr x9, total adrp x9; add x9 fxPage21 and fxPageOff12 to total + +`adr x9, total` is two instructions, `adrp` and `add`, since a variable +may lie beyond `adr`'s 1 MB: it is the plain form of an address, as `lea +r9, [total]` is on x86. The instructions are those of the routines +above in all their forms, of x or w registers, and `movz`, `movk`, +`movn`, `cmn`, `tst`, `bic`, `orn`, `eor`, `csinc`, `csinv`, `csneg`, +`csetm`, `sxtb` to `uxth`, `ubfm`, `sbfm`, `ubfx`, `sbfx`, `rev`, every +`ldr`, `str`, `ldur` and `stur` of a size, `ldp` and `stp` of w, x, s, d +and q, `ldaxr` and `stlxr`, `dsb`, `isb`, `nop`, `yield`, `br`, `blr`, +`ret` of a register, `cbz`, `cbnz`, `tbz`, `tbnz`, `fmax` and `fmin`, +enough for the library's code procedures as they will be on the Mac; +`db` gives bytes and `dd` words for any other. The assembler checks +what it reads: a register where its instruction takes one of its width, +sp only where sp goes, an immediate in its field's range; where Gen's +routines would go through x16, the assembler says it can't. + +The listing is X64's: each routine says its instruction, as the +assembler would read it, and a jump to a label says `->`, which `list` +makes its target's offset from the jump's field. + +`test/a64` is its test, run by `make check`: `Encode.raz`, compiled +with `-I src`, assembles `Asm.txt` and calls every routine, and prints +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 +megabyte of `nop`. + ## Gen.raz: the stack machine This is the only unit that knows the machine. The calls it answers: @@ -2920,6 +3029,9 @@ - **An instruction:** a routine in `X64.raz` built on `opM` or `opR`, its encoding checked against `objdump -d` once, and a program in `conform/` or `test/` that uses it. + In `A64.raz`, a routine that builds its word, and a line in + `test/a64` (`Encode.raz` and `Api.s`, or `Asm.txt`), whose + `expect.sh` checks it against Apple's assembler. - **A machine:** a new `Gen.ARCH.raz`, answering the same calls, and an encoder and assembler for it in place of `X64.raz`; and `Exe.OS.raz` if the system's executable differs. diff --git a/src/A64.raz b/src/A64.raz new file mode 100644 index 0000000..4234970 --- /dev/null +++ b/src/A64.raz @@ -0,0 +1,1848 @@ +(* A64: machine code for arm64, X64's twin. + + The encoder writes arm64's instructions into the text of Chunk, each + one word of 32 bits, least significant byte first. Registers are + numbered as the hardware numbers them: x0 to x30, and 31, which is sp + in some places and the zero register, xzr, in others, as each + instruction says (SP and ZR below are both 31; a routine's comment + says which it means). The reals' registers, v0 to v31, have numbers + of their own, 0 to 31, and a size: s for an F32, d for an F64, q for + 16 bytes. Jumps within a procedure are patched here, as soon as their + target is known; calls and addresses of other chunks are fixups, + patched by Link. + + Apple's arm64 convention is the target's: x18 is the system's and + never touched, x29 is the frame pointer, x30 the link register, sp + is kept a multiple of 16. x16 (IP0) is this encoder's own scratch + register: a routine given an immediate or an offset too big for its + instruction builds it in x16 first, so Gen keeps nothing there. + + Each encoding's comment gives its fields, most significant first, as + Arm's reference manual draws them: `sf 0 0 100010 sh imm12 Rn Rd` is + add (immediate), sf the bit for 64 bits, then constant bits, then the + fields, Rd in the low five bits. Every encoding was checked against + Apple's assembler; test/a64 checks them all again. + + Depends on: Scan, for errors and the assembler's symbols; Diag, for + positions and text; Chunk, for the code buffer and fixups. + Leaves out: choosing instructions; that is Gen (Gen.arm64.raz, to + come). Only what Gen and the library's code procedures need is here; + `db` and `dd` give any other instruction. *) +module A64; +import Out, Fmt, Str, Diag, Scan, Chunk; +import SYSTEM; + +const + FP* = 29; LR* = 30; + SP* = 31; ZR* = 31; (* the same number: the instruction tells which *) + X16* = 16; X17* = 17; (* IP0 and IP1, the scratch registers; X16 is the encoder's *) + + (* condition codes, as b.cond and csel take them; cc xor 1 is the + opposite of cc (invert) *) + ccEQ* = 0; ccNE* = 1; ccHS* = 2; ccLO* = 3; ccMI* = 4; ccPL* = 5; ccVS* = 6; ccVC* = 7; + ccHI* = 8; ccLS* = 9; ccGE* = 10; ccLT* = 11; ccGT* = 12; ccLE* = 13; ccAL* = 14; + + (* the operations of two registers, alu's op: AluBits gives each its bits *) + opAdd* = 0; opAdds* = 1; opSub* = 2; opSubs* = 3; + opAnd* = 4; opOrr* = 5; opEor* = 6; opAnds* = 7; opBic* = 8; opOrn* = 9; + + (* shifts: of a register operand, and lsl, lsr, asr and ror themselves *) + shLsl* = 0; shLsr* = 1; shAsr* = 2; shRor* = 3; + + (* csel's op: rd := rn if the condition holds, else rm, rm + 1, ~rm or -rm *) + csSel* = 0; csInc* = 1; csInv* = 2; csNeg* = 3; + + (* the addressing of ldp, stp and the indexed loads, bits 23 and 24 of + a pair: [rn, #off]; [rn, #off]!, rn changed first; [rn], #off, after *) + mPost* = 1; mOff* = 2; mPre* = 3; + + (* reals: fop's op (the opcode field of two sources) and fop1's (of one) *) + fMul* = 0; fDiv* = 1; fAdd* = 2; fSub* = 3; fMax* = 4; fMin* = 5; + fMov* = 0; fAbs* = 1; fNeg* = 2; fSqrt* = 3; + + NoLabel* = -1; + + AluBits: [10]INT = [0B000000H, 2B000000H, 4B000000H, 6B000000H, + 0A000000H, 2A000000H, 4A000000H, 6A000000H, 0A200000H, 2A200000H]; + AluNames: [10][]const U8 = ["add", "adds", "sub", "subs", "and", "orr", "eor", "ands", "bic", "orn"]; + ShiftNames: [4][]const U8 = ["lsl", "lsr", "asr", "ror"]; + CselBits: [4]INT = [1A800000H, 1A800400H, 5A800000H, 5A800400H]; + CselNames: [4][]const U8 = ["csel", "csinc", "csinv", "csneg"]; + CondNames*: [16][]const U8 = ["eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc", + "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"]; + FopNames: [6][]const U8 = ["fmul", "fdiv", "fadd", "fsub", "fmax", "fmin"]; + Fop1Names: [4][]const U8 = ["fmov", "fabs", "fneg", "fsqrt"]; + Nop = 0D503201FH; + +type + (* The parser's reader of an integer constant in a code procedure: a + whole expression, or, when whole is false, a single factor *) + ConstFn* = proc(whole: BOOL): (I64, ERROR); + + (* The parser's reader of a name that stands for an address: a + procedure or a module variable. Its chunk, the name read; or -1, + nothing read, if the name is anything else *) + NameFn* = proc: (INT, ERROR); + + (* The text of the instruction that starts at at, for the listing *) + Said = record + at: INT; + text: []const U8; + end; + +var + listing*: BOOL; (* -S: each instruction's text is kept, for list *) + said: ^Said; + numSaid, saidCap: INT; + labelAt: INT; (* where a jump was last fixed to the place it was at *) + +(* The listing, as X64's *) + +proc setListing*(yes: BOOL); +begin + listing := yes; +end; + +proc saidCount*: INT; +begin + return numSaid; +end; + +proc unsay*(n: INT); +begin + numSaid := n; +end; + +proc sayAt*(at: INT; s: []const U8); +begin + if not listing then return; end; + if numSaid = saidCap then + said := ^Said(Scan.regrab(^U8(said), numSaid * SIZE(Said), (saidCap * 2 + 1024) * SIZE(Said))); + saidCap := saidCap * 2 + 1024; + end; + said[numSaid].at := at; + said[numSaid].text := s; + INC(numSaid); +end; + +proc say*(s: []const U8); +begin + sayAt(Chunk.pc, s); +end; + +proc restate*(at: INT; s: []const U8); +var i: INT; +begin + i := numSaid - 1; + while (i >= 0) and (said[i].at # at) do DEC(i); end; + if i >= 0 then said[i].text := s; end; +end; + +(* "op a, b, c": an instruction's text, from its operands' *) +proc sayOps(op: []const U8; ops: []const []const U8); +var s: []const U8; i: INT; +begin + s := op; + for i := 0 to LEN(ops) - 1 do + if i = 0 then s := Diag.say("{} {}", [Fmt.str(s), Fmt.str(ops[i])]); + else s := Diag.say("{}, {}", [Fmt.str(s), Fmt.str(ops[i])]); end; + end; + say(s); +end; + +(* 1 for true *) +proc bit(f: BOOL): INT; +begin + if f then return 1; end; + return 0; +end; + +(* log2 of a size of 1, 2, 4, 8 or 16 bytes *) +proc scaleOf(size: INT): INT; +var s: INT; +begin + s := 0; + while (1 shl s) < size do INC(s); end; + return s; +end; + +(* Register r's name: x or w by sf, 31 sp if sp, else the zero register *) +proc reg(r, sf: INT; sp: BOOL): []const U8; +const Pfx: [2][]const U8 = ["w", "x"]; +begin + if (r = 31) and sp and (sf = 1) then return "sp"; end; + if (r = 31) and sp then return "wsp"; end; + if r = 31 then return Diag.say("{}zr", [Fmt.str(Pfx[sf])]); end; + return Diag.say("{}{}", [Fmt.str(Pfx[sf]), Fmt.int(r)]); +end; + +proc x(r: INT): []const U8; +begin + return reg(r, 1, false); +end; + +proc xsp(r: INT): []const U8; +begin + return reg(r, 1, true); +end; + +(* A real's register, by its size: s, d or q *) +proc vreg(v, size: INT): []const U8; +const Pfx: [5][]const U8 = ["b", "h", "s", "d", "q"]; +begin + return Diag.say("{}{}", [Fmt.str(Pfx[scaleOf(size)]), Fmt.int(v)]); +end; + +proc imm(v: I64): []const U8; +begin + return Diag.say("#{}", [Fmt.int(v)]); +end; + +(* [rn, #off], or [rn, #off]! and [rn], #off by mode *) +proc memText(rn, off, mode: INT): []const U8; +begin + if mode = mPre then return Diag.say("[{}, #{}]!", [Fmt.str(xsp(rn)), Fmt.int(off)]); end; + if mode = mPost then return Diag.say("[{}], #{}", [Fmt.str(xsp(rn)), Fmt.int(off)]); end; + return Diag.say("[{}, #{}]", [Fmt.str(xsp(rn)), Fmt.int(off)]); +end; + +proc emit(insn: INT); +begin + Chunk.put4(insn); +end; + +proc invert*(cc: INT): INT; +begin + return cc xor 1; +end; + +(* Immediates and moves *) + +(* v as a logical instruction's immediate, its fields N:immr:imms, or -1 + if it can't be one. Such an immediate is an element of 2, 4, 8, 16, 32 + or 64 bits repeated across the register, the element a run of ones + rotated: N says 64 bits, imms holds the element's size and the run's + length less one (imms 0xxxxx for 32 bits, 10xxxx for 16, ... 11110x + for 2), and immr the rotation right. Of 32 bits (sf 0) the low half is + taken, and seen as repeated. *) +proc bitmask*(v: I64; sf: INT): INT; +var u, elem, pat, m, rot: U64; e, ones, r: INT; +begin + u := U64(v); + if sf = 0 then u := (u & 0FFFFFFFFH) | ((u & 0FFFFFFFFH) shl 32); end; + if (u = 0) or (u = MAX(U64)) then return -1; end; + (* the smallest element: u is made of halves alike while turning it + round by half its element changes nothing *) + e := 64; + while (e > 2) and (ROR(u, e div 2) = u) do e := e div 2; end; + if e = 64 then m := MAX(U64); else m := (U64(1) shl e) - 1; end; + elem := u & m; + ones := POPCOUNT(elem); + pat := (U64(1) shl ones) - 1; + for r := 0 to e - 1 do + rot := ((pat shr r) | (pat shl ((e - r) & 63))) & m; + if rot = elem then + return (bit(e = 64) shl 12) | (r shl 6) | ((~(e * 2 - 1)) & 3FH) | (ones - 1); + end; + end; + return -1; +end; + +(* movn, movz or movk (opc 0, 2, 3): sf opc 100101 hw imm16 Rd, imm16 + shifted left by 16 * hw; movn writes its inverse, movk keeps the rest *) +proc movWide(sf, opc, rd, imm16, hw: INT); +const Names: [4][]const U8 = ["movn", "", "movz", "movk"]; +begin + if listing then sayOps(Names[opc], [reg(rd, sf, false), imm(imm16 & 0FFFFH), Diag.say("lsl #{}", [Fmt.int(hw * 16)])]); end; + emit((sf shl 31) | (opc shl 29) | 12800000H | (hw shl 21) | ((imm16 & 0FFFFH) shl 5) | rd); +end; + +(* A logical instruction with an immediate, enc as bitmask gives it: + sf opc 100100 N immr imms Rn Rd, opc 00 and, 01 orr, 10 eor, 11 ands. + Rd is sp (but for ands), Rn the zero register. *) +proc logicImm(sf, op, rd, rn, enc: INT; v: I64); +begin + if listing then sayOps(AluNames[op], [reg(rd, sf, op # opAnds), reg(rn, sf, false), imm(v)]); end; + emit((sf shl 31) | (AluBits[op] + 08000000H) | (enc shl 10) | (rn shl 5) | rd); +end; + +(* rd := v, of 64 bits (sf 1) or 32, in as few instructions as it + takes: movz #0 or movn #0 for all zeros or all ones; a single movz + when v is one halfword of 16 bits and zeros, a movn when it is one + halfword and ones, an orr of the zero register when it is a logical + immediate; else a movz (or movn, if more of its halfwords are ones) + of the first halfword that isn't all zeros (ones), and a movk for + each other such halfword. Apple's assembler chooses the same. *) +proc movImm(sf, rd: INT; v: I64); +var n, zeros, ones, i, h, first, enc: INT; +begin + n := 2 + 2 * sf; + if sf = 0 then v := v & 0FFFFFFFFH; end; + zeros := 0; ones := 0; + for i := 0 to n - 1 do + h := INT((v shr (16 * i)) & 0FFFFH); + if h = 0 then INC(zeros); elsif h = 0FFFFH then INC(ones); end; + end; + if (zeros = n) or (ones = n) then + movWide(sf, 2 * bit(zeros = n), rd, 0, 0); + return; + end; + enc := bitmask(v, sf); + if (zeros < n - 1) and (ones < n - 1) and (enc >= 0) then + logicImm(sf, opOrr, rd, ZR, enc, v); + return; + end; + first := -1; + for i := 0 to n - 1 do + h := INT((v shr (16 * i)) & 0FFFFH); + if (zeros >= ones) and (h # 0) or (zeros < ones) and (h # 0FFFFH) then + if first >= 0 then movWide(sf, 3, rd, h, i); + elsif zeros >= ones then movWide(sf, 2, rd, h, i); + else movWide(sf, 0, rd, ~h, i); end; + first := i; + end; + end; +end; + +proc movRI*(rd: INT; v: I64); +begin + movImm(1, rd, v); +end; + +(* Add, subtract and the logical operations *) + +(* add, adds, sub or subs with an immediate of 12 bits, shifted left by + 12 if sh: sf op S 100010 sh imm12 Rn Rd. Rn is sp; so is Rd, but for + adds and subs, whose Rd 31 is the zero register (cmp, cmn). *) +proc addImm(sf, op, rd, rn, imm12, sh: INT); +begin + if listing then sayOps(AluNames[op], [reg(rd, sf, (op & 1) = 0), reg(rn, sf, true), imm(imm12), Diag.say("lsl #{}", [Fmt.int(12 * sh)])]); end; + emit((sf shl 31) | (AluBits[op] + 06000000H) | (sh shl 22) | (imm12 shl 10) | (rn shl 5) | rd); +end; + +(* op of two registers, rm shifted by n: sf op S 01011 shift 0 Rm imm6 Rn + Rd for add and sub, sf opc 01010 shift N Rm imm6 Rn Rd for the logical + ones (N 1 for bic and orn, which invert rm). 31 is the zero register. *) +proc aluShifted(sf, op, rd, rn, rm, sh, n: INT); +begin + if listing then sayOps(AluNames[op], [reg(rd, sf, false), reg(rn, sf, false), reg(rm, sf, false), Diag.say("{} #{}", [Fmt.str(ShiftNames[sh]), Fmt.int(n)])]); end; + emit((sf shl 31) | AluBits[op] | (sh shl 22) | (rm shl 16) | (n shl 10) | (rn shl 5) | rd); +end; + +(* add or sub of a register extended, here only uxtx (uxtw for 32 + bits), shifted left by n up to 4: sf op S 01011 00 1 Rm option imm3 + Rn Rd. The form that takes sp, as Rn, and as Rd but for adds and subs *) +proc addExt(sf, op, rd, rn, rm, n: INT); +begin + if listing then sayOps(AluNames[op], [reg(rd, sf, (op & 1) = 0), reg(rn, sf, true), reg(rm, sf, false), Diag.say("uxtx #{}", [Fmt.int(n)])]); end; + emit((sf shl 31) | AluBits[op] | 200000H | (rm shl 16) | ((2 + sf) shl 13) | (n shl 10) | (rn shl 5) | rd); +end; + +(* rd := rn op rm, of 64 bits; 31 is the zero register, so tst a, b is + alu(opAnds, ZR, a, b) *) +proc alu*(op, rd, rn, rm: INT); +begin + aluShifted(1, op, rd, rn, rm, shLsl, 0); +end; + +(* rd := rn op (rm shifted by n), as an element's address, rn + rm lsl 3 *) +proc aluShift*(op, rd, rn, rm, sh, n: INT); +begin + aluShifted(1, op, rd, rn, rm, sh, n); +end; + +(* rd := rn op v, of 64 bits. For add and sub, rn and rd may be sp: + v in 12 bits, or 12 bits shifted by 12, is one instruction, a + negative v the opposite one; below 2^24, without flags, two; else v + is built in x16 and added as a register. For the logical operations, + v as an immediate if it can be one, else built in x16. *) +proc aluI*(op, rd, rn: INT; v: I64); +var enc: INT; +begin + if op <= opSubs then + if (v < 0) and (v > MIN(I64)) then v := -v; op := op xor 2; end; + if (v >= 0) and (v < 1000H) then + addImm(1, op, rd, rn, INT(v), 0); + elsif (v > 0) and (v < 1000000H) and ((v & 0FFFH) = 0) then + addImm(1, op, rd, rn, INT(v shr 12), 1); + elsif (v > 0) and (v < 1000000H) and ((op & 1) = 0) then + addImm(1, op, rd, rn, INT(v shr 12), 1); + addImm(1, op, rd, rd, INT(v & 0FFFH), 0); + else + movRI(X16, v); + addExt(1, op, rd, rn, X16, 0); + end; + return; + end; + enc := bitmask(v, 1); + if (enc >= 0) and (op <= opAnds) then + logicImm(1, op, rd, rn, enc, v); + else + movRI(X16, v); + aluShifted(1, op, rd, rn, X16, shLsl, 0); + end; +end; + +(* rd := rn, by orr of the zero register; by add of 0 when either is sp *) +proc movRR*(rd, rn: INT); +begin + if (rd = SP) or (rn = SP) then addImm(1, opAdd, rd, rn, 0, 0); else aluShifted(1, opOrr, rd, ZR, rn, shLsl, 0); end; +end; + +(* the flags of rn - rm; and of rn - v, where rn 31 is sp *) +proc cmp*(rn, rm: INT); +begin + aluShifted(1, opSubs, ZR, rn, rm, shLsl, 0); +end; + +proc cmpI*(rn: INT; v: I64); +begin + aluI(opSubs, ZR, rn, v); +end; + +(* neg: sub from the zero register; mvn: orn of it *) +proc neg*(rd, rm: INT); +begin + aluShifted(1, opSub, rd, ZR, rm, shLsl, 0); +end; + +proc mvn*(rd, rm: INT); +begin + aluShifted(1, opOrn, rd, ZR, rm, shLsl, 0); +end; + +(* Shifts and bit fields *) + +(* sbfm, bfm or ubfm (opc 0, 1, 2): sf opc 100110 N immr imms Rn Rd, N + as sf. The bits imms down to immr of rn, or, if imms < immr, the low + imms + 1 bits put at width - immr; the rest of rd is the sign, kept, + or zeros. Every shift by a constant, extension and field is one. *) +proc bitfield(sf, opc, rd, rn, immr, imms: INT); +const Names: [3][]const U8 = ["sbfm", "bfm", "ubfm"]; +begin + if listing then sayOps(Names[opc], [reg(rd, sf, false), reg(rn, sf, false), imm(immr), imm(imms)]); end; + emit((sf shl 31) | (opc shl 29) | 13000000H | (sf shl 22) | (immr shl 16) | (imms shl 10) | (rn shl 5) | rd); +end; + +(* A shift by n, 0 to the width less one, of size 4 or 8 bytes: lsl is + ubfm by width - n of the low width - 1 - n bits, lsr ubfm and asr + sbfm of the bits from n up, ror extr of rn and rn from bit n: + sf 00 100111 N 0 Rm imms Rn Rd *) +proc shiftI*(op, rd, rn, n, size: INT); +var sf, w: INT; +begin + sf := bit(size = 8); w := 32 + 32 * sf; + match op of + | shLsl: bitfield(sf, 2, rd, rn, (w - n) & (w - 1), w - 1 - n); + | shLsr: bitfield(sf, 2, rd, rn, n, w - 1); + | shAsr: bitfield(sf, 0, rd, rn, n, w - 1); + | else: + if listing then sayOps("ror", [reg(rd, sf, false), reg(rn, sf, false), imm(n)]); end; + emit((sf shl 31) | 13800000H | (sf shl 22) | (rn shl 16) | (n shl 10) | (rn shl 5) | rd); + end; +end; + +(* rd := the width bits of rn from bit lsb, as ubfx, or sbfx if signed: + ubfm or sbfm of the bits lsb + width - 1 down to lsb *) +proc field*(signed: BOOL; rd, rn, lsb, width: INT); +begin + bitfield(1, 2 - 2 * bit(signed), rd, rn, lsb, lsb + width - 1); +end; + +(* rd := the low size bytes of rn, brought to 64 bits by their sign or + zeros: sxtb, sxth, sxtw (sbfm, 64 bits); uxtb and uxth (ubfm, 32 bits, + which zeroes the upper half), mov wd, wn for 4 *) +proc extend*(rd, rn, size: INT; signed: BOOL); +begin + if signed then + bitfield(1, 0, rd, rn, 0, size * 8 - 1); + elsif size < 4 then + bitfield(0, 2, rd, rn, 0, size * 8 - 1); + else + aluShifted(0, opOrr, rd, ZR, rn, shLsl, 0); + end; +end; + +(* The instructions of only registers, each its bits and n registers, + Rd, Rn, Rm and Ra, in their places; a field an instruction hasn't is + 0, or set in its bits: + madd, msub sf 00 11011 000 Rm o0 Ra Rn Rd rd := ra + rn * rm, ra - rn * rm + smulh, umulh 1 00 11011 U10 Rm 0 11111 Rn Rd the high 64 bits of the product + sdiv, udiv sf 0 0 11010110 Rm 00001 o1 Rn Rd + lslv ... rorv sf 0 0 11010110 Rm 0010 op2 Rn Rd by rm, modulo the width + clz, rbit, rev sf 1 0 11010110 00000 0000 opc Rn Rd + A division by zero gives 0, and the smallest value by -1 itself: Gen + checks for both. *) +proc regOp(name: []const U8; n, sf, bits, rd, rn, rm, ra: INT); +var t: [4][]const U8; +begin + if listing then + t := [reg(rd, sf, false), reg(rn, sf, false), reg(rm, sf, false), reg(ra, sf, false)]; + sayOps(name, t[0..n]); + end; + emit((sf shl 31) | bits | (rm shl 16) | (ra shl 10) | (rn shl 5) | rd); +end; + +proc mul*(rd, rn, rm: INT); +begin + regOp("mul", 3, 1, 1B007C00H, rd, rn, rm, 0); +end; + +(* rd := ra + rn * rm, or ra - rn * rm if sub *) +proc madd*(rd, rn, rm, ra: INT; sub: BOOL); +const Names: [2][]const U8 = ["madd", "msub"]; +begin + regOp(Names[bit(sub)], 4, 1, 1B000000H | (bit(sub) shl 15), rd, rn, rm, ra); +end; + +proc mulHigh*(signed: BOOL; rd, rn, rm: INT); +const Names: [2][]const U8 = ["umulh", "smulh"]; +begin + regOp(Names[bit(signed)], 3, 1, 1B407C00H | (bit(not signed) shl 23), rd, rn, rm, 0); +end; + +proc divide*(signed: BOOL; rd, rn, rm: INT); +const Names: [2][]const U8 = ["udiv", "sdiv"]; +begin + regOp(Names[bit(signed)], 3, 1, 1AC00800H | (bit(signed) shl 10), rd, rn, rm, 0); +end; + +(* lsl, lsr, asr or ror of size 4 or 8 bytes, by rm *) +proc shift*(op, rd, rn, rm, size: INT); +begin + regOp(ShiftNames[op], 3, bit(size = 8), 1AC02000H | (op shl 10), rd, rn, rm, 0); +end; + +(* the leading zeros of rn, of size 4 or 8 bytes: 64 for 0; and rn's + bits in the opposite order, of which clz counts trailing zeros *) +proc clz*(rd, rn, size: INT); +begin + regOp("clz", 2, bit(size = 8), 5AC01000H, rd, rn, 0, 0); +end; + +proc rbit*(rd, rn, size: INT); +begin + regOp("rbit", 2, bit(size = 8), 5AC00000H, rd, rn, 0, 0); +end; + +(* csel, csinc, csinv or csneg (op), on condition cc: + sf op 0 11010100 Rm cond 0 o2 Rn Rd *) +proc condSel(sf, op, cc, rd, rn, rm: INT); +begin + if listing then sayOps(CselNames[op], [reg(rd, sf, false), reg(rn, sf, false), reg(rm, sf, false), CondNames[cc]]); end; + emit((sf shl 31) | CselBits[op] | (rm shl 16) | (cc shl 12) | (rn shl 5) | rd); +end; + +proc csel*(op, cc, rd, rn, rm: INT); +begin + condSel(1, op, cc, rd, rn, rm); +end; + +(* rd := 1 if cc holds, else 0: cset, csinc of the zero register on the + opposite condition *) +proc cset*(cc, rd: INT); +begin + condSel(1, csInc, invert(cc), rd, ZR, ZR); +end; + +(* Loads and stores. ldst and its kin make them all from the bits that + tell size, register file and operation, (size << 30 | V << 26 | opc + << 22), and the form: + unsigned offset size 111 V 01 opc imm12 Rn Rt, imm12 = off / size + unscaled size 111 V 00 opc 0 imm9 00 Rn Rt (ldur, stur) + post-index size 111 V 00 opc 0 imm9 01 Rn Rt + pre-index size 111 V 00 opc 0 imm9 11 Rn Rt + register size 111 V 00 opc 1 Rm option S 10 Rn Rt + Rn 31 is sp; Rt 31 is the zero register. *) + +(* The bits of a load (or store) of size bytes; signed sign-extends to + 64 bits (to 32 if sf is 0), fp is a real's register. For an integer + opc is 00 store, 01 load, 10 load signed to x, 11 load signed to w; + for a real the same but q, 16 bytes, which is size 00 and opc 1x. *) +proc ldstBits(size: INT; load, signed, fp: BOOL; sf: INT): INT; +begin + if fp and (size = 16) then return 04000000H | ((2 + bit(load)) shl 22); end; + if fp then return (scaleOf(size) shl 30) | 04000000H | (bit(load) shl 22); end; + if signed and (size < 8) then return (scaleOf(size) shl 30) | ((3 - sf) shl 22); end; + return (scaleOf(size) shl 30) | (bit(load) shl 22); +end; + +(* The size of the load or store with these bits, 16 for q *) +proc sizeOf(bits: INT): INT; +begin + if (bits & 04800000H) = 04800000H then return 16; end; + return 1 shl INT(bits shr 30); +end; + +(* The text of a load or store, its name from its bits (ldrsh, sturb, + ...), its register's and its address m *) +proc sayLdst(bits, rt: INT; unscaled: BOOL; m: []const U8); +const Sizes: [4][]const U8 = ["b", "h", "w", ""]; +var opc: INT; s, r: []const U8; +begin + opc := (bits shr 22) & 3; + if (opc & 1) = 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)); + else + r := reg(rt, bit((opc = 2) or (bits shr 30 = 3)), false); + if opc >= 2 then s := Diag.say("{}s", [Fmt.str(s)]); end; + if (opc >= 2) or (bits shr 30 < 2) then s := Diag.say("{}{}", [Fmt.str(s), Fmt.str(Sizes[bits shr 30])]); end; + end; + sayOps(s, [r, m]); +end; + +(* rt at [rn, rm], or [rn, rm, lsl #scale] if scaled: option 011, S *) +proc ldstReg(bits, rt, rn, rm: INT; scaled: BOOL); +begin + if listing then sayLdst(bits, rt, false, Diag.say("[{}, {}, lsl #{}]", [Fmt.str(xsp(rn)), Fmt.str(x(rm)), Fmt.int(bit(scaled) * scaleOf(sizeOf(bits)))])); end; + emit(38206800H | bits | (rm shl 16) | (bit(scaled) shl 12) | (rn shl 5) | rt); +end; + +(* rt at [rn, #off]: by its unsigned offset if off is a multiple of the + size below 4096 of them, else unscaled if off is in -256 to 255; else + off is built in x16 for the register form *) +proc ldst(bits, rt, rn, off: INT); +var sc: INT; +begin + sc := scaleOf(sizeOf(bits)); + if (off >= 0) and ((off & ((1 shl sc) - 1)) = 0) and ((off shr sc) < 4096) then + if listing then sayLdst(bits, rt, false, memText(rn, off, mOff)); end; + emit(39000000H | bits | ((off shr sc) shl 10) | (rn shl 5) | rt); + elsif (off >= -256) and (off <= 255) then + if listing then sayLdst(bits, rt, true, memText(rn, off, mOff)); end; + emit(38000000H | bits | ((off & 1FFH) shl 12) | (rn shl 5) | rt); + else + movRI(X16, off); + ldstReg(bits, rt, rn, X16, false); + end; +end; + +(* rt at [rn, #off]!, rn changed first, or [rn], #off, after; off in + -256 to 255 *) +proc ldstIdx(bits, rt, rn, off, mode: INT); +begin + if (off < -256) or (off > 255) then _ := Scan.mark("an index beyond -256 to 255"); end; + if listing then sayLdst(bits, rt, false, memText(rn, off, mode)); end; + emit(38000000H | bits | ((off & 1FFH) shl 12) | ((2 * bit(mode = mPre) + 1) shl 10) | (rn shl 5) | rt); +end; + +(* rt := the size bytes at [rn, #off], 1, 2, 4 or 8, by their sign or + zeros; and the store *) +proc load*(rt, rn, off, size: INT; signed: BOOL); +begin + ldst(ldstBits(size, true, signed, false, 1), rt, rn, off); +end; + +proc store*(rt, rn, off, size: INT); +begin + ldst(ldstBits(size, false, false, false, 1), rt, rn, off); +end; + +(* [rn + rm], or [rn + rm * size] if scaled *) +proc loadR*(rt, rn, rm, size: INT; signed, scaled: BOOL); +begin + ldstReg(ldstBits(size, true, signed, false, 1), rt, rn, rm, scaled); +end; + +proc storeR*(rt, rn, rm, size: INT; scaled: BOOL); +begin + ldstReg(ldstBits(size, false, false, false, 1), rt, rn, rm, scaled); +end; + +(* with rn changed, mPre or mPost, as a push is str x0, [sp, #-16]! *) +proc loadIdx*(rt, rn, off, size, mode: INT); +begin + ldstIdx(ldstBits(size, true, false, false, 1), rt, rn, off, mode); +end; + +proc storeIdx*(rt, rn, off, size, mode: INT); +begin + ldstIdx(ldstBits(size, false, false, false, 1), rt, rn, off, mode); +end; + +(* A real's register, of size 4, 8 or 16 *) +proc loadF*(vt, rn, off, size: INT); +begin + ldst(ldstBits(size, true, false, true, 1), vt, rn, off); +end; + +proc storeF*(vt, rn, off, size: INT); +begin + ldst(ldstBits(size, false, false, true, 1), vt, rn, off); +end; + +(* ldp and stp: two registers at [rn, #off], off a multiple of the size + in -64 to 63 of them: opc 101 V mode L imm7 Rt2 Rn Rt, opc 00 for w or + s, 10 for x, 01 for d, 10 for q *) +proc pair(load, fp: BOOL; size, rt, rt2, rn, off, mode: INT); +const Names: [2][]const U8 = ["stp", "ldp"]; +var opc: INT; +begin + if fp then opc := scaleOf(size) - 2; else opc := 2 * bit(size = 8); end; + if ((off & (size - 1)) # 0) or (off < -64 * size) or (off > 63 * size) then + _ := Scan.mark("a pair's offset beyond -64 to 63 of its size"); + end; + if listing then sayOps(Names[bit(load)], [x(rt), x(rt2), memText(rn, off, mode)]); end; + emit((opc shl 30) | 28000000H | (bit(fp) shl 26) | (mode shl 23) | (bit(load) shl 22) + | (((off div size) & 7FH) shl 15) | (rt2 shl 10) | (rn shl 5) | rt); +end; + +proc ldp*(rt, rt2, rn, off, mode: INT); +begin + pair(true, false, 8, rt, rt2, rn, off, mode); +end; + +proc stp*(rt, rt2, rn, off, mode: INT); +begin + pair(false, false, 8, rt, rt2, rn, off, mode); +end; + +(* Atomics, by the LSE instructions every Apple processor has, of size 4 + or 8 bytes, each with acquire and release, so sequentially consistent: + ldaddal rs, rt, [rn] size 111 0 00 1 1 1 Rs 0 000 00 Rn Rt: rt := [rn], [rn] += rs + swpal rs, rt, [rn] size 111 0 00 1 1 1 Rs 1 000 00 Rn Rt: rt := [rn], [rn] := rs + casal rs, rt, [rn] size 001000 1 1 1 Rs 1 11111 Rn Rt: if [rn] = rs, [rn] := rt; rs := old [rn] + The assembler knows the exclusive pair too, for a loop of its own: + ldaxr rt, [rn] size 001000 0 1 0 11111 1 11111 Rn Rt + stlxr ws, rt, [rn] size 001000 0 0 0 Rs 1 11111 Rn Rt, ws 0 if it stored *) +const + AtomNames: [5][]const U8 = ["ldaddal", "swpal", "casal", "ldaxr", "stlxr"]; + AtomBits: [5]INT = [38E00000H, 38E08000H, 08E0FC00H, 085FFC00H, 0800FC00H]; + +proc atomic(op, size, rs, rt, rn: INT); +begin + if listing then sayOps(AtomNames[op], [reg(rs, bit(size = 8), false), reg(rt, bit(size = 8), false), memText(rn, 0, mOff)]); end; + emit(80000000H | (bit(size = 8) shl 30) | AtomBits[op] | (rs shl 16) | (rn shl 5) | rt); +end; + +proc ldaddal*(rs, rt, rn, size: INT); +begin + atomic(0, size, rs, rt, rn); +end; + +proc swpal*(rs, rt, rn, size: INT); +begin + atomic(1, size, rs, rt, rn); +end; + +proc casal*(rs, rt, rn, size: INT); +begin + atomic(2, size, rs, rt, rn); +end; + +(* The rest of the system's: a barrier, the inner shareable domain's, + which is every processor's (dmb ish, 1101010100 0 00 011 0011 CRm 1 01 + 11111, CRm 1011); a call of the system, svc #imm; a trap, brk #imm *) +proc dmb*; +begin + if listing then say("dmb ish"); end; + emit(0D5033BBFH); +end; + +proc svc*(n: INT); +begin + if listing then sayOps("svc", [imm(n)]); end; + emit(0D4000001H | ((n & 0FFFFH) shl 5)); +end; + +proc brk*(n: INT); +begin + if listing then sayOps("brk", [imm(n)]); end; + emit(0D4200000H | ((n & 0FFFFH) shl 5)); +end; + +(* Code made up to a multiple of n bytes with nop *) +proc align*(n: INT); +begin + while Chunk.pc mod n # 0 do + if listing then say("nop"); end; + emit(Nop); + end; +end; + +(* Reals: type 00 is single, an F32, 01 double *) + +(* fmul, fdiv, fadd, fsub, fmax and fmin, vd := vn op vm: + 0 0 0 11110 type 1 Rm opcode 10 Rn Rd *) +proc fop*(op, vd, vn, vm: INT; single: BOOL); +var s: INT; +begin + s := 8 - 4 * bit(single); + if listing then sayOps(FopNames[op], [vreg(vd, s), vreg(vn, s), vreg(vm, s)]); end; + emit(1E200800H | (bit(not single) shl 22) | (vm shl 16) | (op shl 12) | (vn shl 5) | vd); +end; + +(* fmov, fabs, fneg and fsqrt, of one register: + 0 0 0 11110 type 1 opcode 10000 Rn Rd *) +proc fop1*(op, vd, vn: INT; single: BOOL); +var s: INT; +begin + s := 8 - 4 * bit(single); + if listing then sayOps(Fop1Names[op], [vreg(vd, s), vreg(vn, s)]); end; + emit(1E204000H | (bit(not single) shl 22) | (op shl 15) | (vn shl 5) | vd); +end; + +(* vd := vn converted, to an F32 if toSingle, else to an F64: fop1's form + with opcode 0001, then the type made, 00 single or 01 double *) +proc fcvt*(vd, vn: INT; toSingle: BOOL); +begin + if listing then sayOps("fcvt", [vreg(vd, 8 - 4 * bit(toSingle)), vreg(vn, 4 + 4 * bit(toSingle))]); end; + emit(1E204000H | (bit(toSingle) shl 22) | ((5 - bit(toSingle)) shl 15) | (vn shl 5) | vd); +end; + +(* fcmp vn, vm, or vn with 0.0 if vm < 0: the flags as an unsigned + compare sets them, but for unordered (a NaN), which is C and V: + 0 0 0 11110 type 1 Rm 00 1000 Rn 0 opc 000, opc 01 for 0.0 *) +proc fcmp*(vn, vm: INT; single: BOOL); +var s: INT; +begin + s := 8 - 4 * bit(single); + if vm < 0 then + if listing then sayOps("fcmp", [vreg(vn, s), "#0.0"]); end; + emit(1E202008H | (bit(not single) shl 22) | (vn shl 5)); + else + if listing then sayOps("fcmp", [vreg(vn, s), vreg(vm, s)]); end; + emit(1E202000H | (bit(not single) shl 22) | (vm shl 16) | (vn shl 5)); + end; +end; + +(* Between the register files: sf 0 0 11110 type 1 rmode opcode 000000 + Rn Rd, the real of size 4 or 8 bytes, to it if toFp. rmode 00 opcode + 010 scvtf, 011 ucvtf, 110 fmov to a general register, 111 fmov from + one; rmode 11 opcode 000 fcvtzs, 001 fcvtzu, which round toward zero *) +proc fconv(name: []const U8; sf, size, rmode, opcode, rd, rn: INT; toFp: BOOL); +begin + if listing and toFp then sayOps(name, [vreg(rd, size), reg(rn, sf, false)]); end; + if listing and not toFp then sayOps(name, [reg(rd, sf, false), vreg(rn, size)]); end; + emit((sf shl 31) | 1E200000H | (bit(size = 8) shl 22) | (rmode shl 19) | (opcode shl 16) | (rn shl 5) | rd); +end; + +(* vd := the 64-bit integer in rn, signed or not, as a real *) +proc scvtf*(vd, rn: INT; single, signed: BOOL); +const Names: [2][]const U8 = ["ucvtf", "scvtf"]; +begin + fconv(Names[bit(signed)], 1, 8 - 4 * bit(single), 0, 3 - bit(signed), vd, rn, true); +end; + +(* rd := vn toward zero as a 64-bit integer; out of range it is the + nearest, and a NaN 0, where x86 gives the smallest I64: Gen mends it *) +proc fcvtz*(rd, vn: INT; single, signed: BOOL); +const Names: [2][]const U8 = ["fcvtzu", "fcvtzs"]; +begin + fconv(Names[bit(signed)], 1, 8 - 4 * bit(single), 3, bit(not signed), rd, vn, false); +end; + +(* The bits of a real between the files: s and w, d and x *) +proc fmovFR*(vd, rn: INT; single: BOOL); +begin + fconv("fmov", bit(not single), 8 - 4 * bit(single), 0, 7, vd, rn, true); +end; + +proc fmovRF*(rd, vn: INT; single: BOOL); +begin + fconv("fmov", bit(not single), 8 - 4 * bit(single), 0, 6, rd, vn, false); +end; + +(* Addresses, and references to other chunks *) + +(* rd := the address of this instruction plus off, within 1 MB (adr), + or its page of 4 KB plus off pages (adrp): op immlo 10000 immhi Rd *) +proc adrOp(page, rd, off: INT); +begin + emit((page shl 31) | 10000000H | ((off & 3) shl 29) | (((off shr 2) & 7FFFFH) shl 5) | rd); +end; + +proc adr*(rd, off: INT); +begin + if listing then sayOps("adr", [x(rd), Diag.say(". + {}", [Fmt.int(off)])]); end; + adrOp(0, rd, off); +end; + +(* adrp rd with a fixup to the page of chunk c plus addend *) +proc adrpChunk(rd, c, addend: INT); +begin + if listing then sayOps("adrp", [x(rd), Diag.say("{}@PAGE", [Fmt.str(Chunk.chunkName(c))])]); end; + Chunk.addKindFixup(Chunk.pc, c, addend, Chunk.fxPage21); + adrOp(1, rd, 0); +end; + +(* rd := the address of chunk c plus addend, wherever the linker puts + it, since macOS loads a program anywhere: adrp of its page, then add + of its place in the page, each with its fixup *) +proc addrChunk*(rd, c, addend: INT); +begin + adrpChunk(rd, c, addend); + if listing then sayOps("add", [x(rd), x(rd), Diag.say("{}@PAGEOFF", [Fmt.str(Chunk.chunkName(c))])]); end; + Chunk.addKindFixup(Chunk.pc, c, addend, Chunk.fxPageOff12); + emit(91000000H | (rd shl 5) | rd); +end; + +(* A load or store of the bits ldstBits gives at chunk c plus addend: + adrp x16, then the load's or store's unsigned offset is the place in + the page, divided by the size, which the linker reads from the + instruction, as Apple's linker does for ARM64_RELOC_PAGEOFF12. That + offset must then be a multiple of the size: when the chunk's + alignment doesn't make it one, the address is made whole first. As a + C symbol's quadword is read: loadChunk(rt, externChunk(name), 0, 8). *) +proc chunkMem(bits, rt, c, addend: INT); +begin + if ((addend & (sizeOf(bits) - 1)) # 0) or (Chunk.chunks[c].align < sizeOf(bits)) then + addrChunk(X16, c, addend); + ldst(bits, rt, X16, 0); + return; + end; + adrpChunk(X16, c, addend); + if listing then sayLdst(bits, rt, false, Diag.say("[x16, {}@PAGEOFF]", [Fmt.str(Chunk.chunkName(c))])); end; + Chunk.addKindFixup(Chunk.pc, c, addend, Chunk.fxPageOff12); + emit(39000000H | bits | (X16 shl 5) | rt); +end; + +proc loadChunk*(rt, c, addend, size: INT; signed: BOOL); +begin + chunkMem(ldstBits(size, true, signed, false, 1), rt, c, addend); +end; + +proc storeChunk*(rt, c, addend, size: INT); +begin + chunkMem(ldstBits(size, false, false, false, 1), rt, c, addend); +end; + +proc loadChunkF*(vt, c, addend, size: INT); +begin + chunkMem(ldstBits(size, true, false, true, 1), vt, c, addend); +end; + +proc storeChunkF*(vt, c, addend, size: INT); +begin + chunkMem(ldstBits(size, false, false, true, 1), vt, c, addend); +end; + +(* bl or b to the start of chunk c, a fixup: 1 00101 imm26 for bl, 0 + 00101 for b, imm26 the distance in words, within 128 MB *) +proc branchChunk(call: BOOL; c: INT); +const Names: [2][]const U8 = ["b", "bl"]; +begin + if listing then sayOps(Names[bit(call)], [Chunk.chunkName(c)]); end; + Chunk.addKindFixup(Chunk.pc, c, 0, Chunk.fxBranch26); + emit(14000000H | (bit(call) shl 31)); +end; + +proc callChunk*(c: INT); +begin + branchChunk(true, c); +end; + +(* a tail call *) +proc jmpChunk*(c: INT); +begin + branchChunk(false, c); +end; + +(* bl to target, a place in text already written *) +proc callBack*(target: INT); +begin + if listing then say("bl ->"); end; + emit(94000000H | (((target - Chunk.pc) div 4) & 3FFFFFFH)); +end; + +(* blr, br and ret, to the address in rn: + 1101011 0 0 opc 11111 000000 Rn 00000, opc 01 blr, 00 br, 10 ret *) +proc callR*(rn: INT); +begin + if listing then sayOps("blr", [x(rn)]); end; + emit(0D63F0000H | (rn shl 5)); +end; + +proc jmpR*(rn: INT); +begin + if listing then sayOps("br", [x(rn)]); end; + emit(0D61F0000H | (rn shl 5)); +end; + +proc ret*; +begin + if listing then say("ret"); end; + emit(0D65F03C0H); +end; + +(* Jumps within a procedure. A label not yet placed is the chain of the + jumps to it, as in X64, each jump's offset field holding the place + of the one before, in words from the start of the procedure's chunk, + plus one: 0 ends the chain. Placing the label walks the chain and + puts each jump's distance in its field. The fields are of three + widths: + b, bl 0 00101 imm26 within 128 MB + b.cond 0101010 0 imm19 0 cond within 1 MB + cbz, cbnz sf 011010 op imm19 Rt within 1 MB + tbz, tbnz b5 011011 op b40 imm14 Rt within 32 KB + A b.cond or a cbz reaches 1 MB, a procedure of a quarter of a million + instructions; past that the compile ends with an error, rather than + make every conditional jump two instructions for a procedure no one + writes. A tbz reaches only 32 KB, which a long procedure passes: so + Gen's tbz is the opposite tbz over a b, which reaches as far as any b + (tbz below); only the assembler's tbz is one instruction, in a code + procedure that is never that long. *) + +(* Where the offset field of the branch insn is, its lowest bit and its + width *) +proc fieldOf(insn: INT): (INT, INT); +begin + if (insn & 7C000000H) = 14000000H then return 0, 26; end; + if (insn & 7E000000H) = 36000000H then return 5, 14; end; + return 5, 19; +end; + +(* The jump before the one at L on its chain, from the link in its field *) +proc before(L: INT): INT; +var insn, lo, w, prev: INT; +begin + insn := Chunk.get4At(L) & 0FFFFFFFFH; + lo, w := fieldOf(insn); + prev := (insn shr lo) & ((1 shl w) - 1); + if prev = 0 then return NoLabel; end; + return Chunk.chunks[Chunk.curChunk].start + (prev - 1) * 4; +end; + +(* insn with its field set: to the distance d in words if back, else to + the link to the jump at d, or to none if d is NoLabel; either must fit *) +proc withField(insn, d: INT; back: BOOL): INT; +var lo, w: INT; +begin + lo, w := fieldOf(insn); + if not back and (d = NoLabel) then + d := 0; + elsif not back then + d := (d - Chunk.chunks[Chunk.curChunk].start) div 4 + 1; + end; + if back and ((d < -(1 shl (w - 1))) or (d >= (1 shl (w - 1)))) or not back and (d >= (1 shl w)) then + _ := Scan.mark("a jump within a procedure beyond what its instruction reaches (b.cond and cbz: 1 MB)"); + end; + return (insn & ~(((1 shl w) - 1) shl lo)) | ((d & ((1 shl w) - 1)) shl lo); +end; + +(* insn, a branch to L: back to the place L if back, else on L's chain; + the chain's new first jump *) +proc branch(insn, L: INT; back: BOOL): INT; +begin + if back then emit(withField(insn, (L - Chunk.pc) div 4, true)); return L; end; + emit(withField(insn, L, false)); + return Chunk.pc - 4; +end; + +proc jmp*(var L: INT); +begin + if listing then say("b ->"); end; + L := branch(14000000H, L, false); +end; + +proc jcc*(cc: INT; var L: INT); +begin + if listing then say(Diag.say("b.{} ->", [Fmt.str(CondNames[cc])])); end; + L := branch(54000000H | cc, L, false); +end; + +(* to L if the 64 bits of r are zero (cbz), or not (cbnz) *) +proc cbz*(nonzero: BOOL; r: INT; var L: INT); +const Names: [2][]const U8 = ["cbz", "cbnz"]; +begin + if listing then sayOps(Names[bit(nonzero)], [x(r), "->"]); end; + L := branch(0B4000000H | (bit(nonzero) shl 24) | r, L, false); +end; + +(* to L if bit b of r is zero (tbz) or one (tbnz): the opposite test + over the next instruction, a b to L: b5 011011 op b40 imm14 Rt, b's + number split as b5 and b40, imm14 2 *) +proc tbz*(nonzero: BOOL; r, b: INT; var L: INT); +const Names: [2][]const U8 = ["tbnz", "tbz"]; +begin + if listing then sayOps(Names[bit(nonzero)], [x(r), imm(b), ". + 8"]); end; + emit(36000000H | (bit(not nonzero) shl 24) | ((b shr 5) shl 31) | ((b & 31) shl 19) | (2 shl 5) | r); + jmp(L); +end; + +proc jmpBack*(target: INT); +begin + if listing then say("b ->"); end; + _ := branch(14000000H, target, true); +end; + +proc jccBack*(cc, target: INT); +begin + if listing then say(Diag.say("b.{} ->", [Fmt.str(CondNames[cc])])); end; + _ := branch(54000000H | cc, target, true); +end; + +proc fixLinkTo*(L, target: INT); +var next: INT; +begin + while L # NoLabel do + next := before(L); + Chunk.put4At(L, withField(Chunk.get4At(L) & 0FFFFFFFFH, (target - L) div 4, true)); + L := next; + end; +end; + +proc fixLink*(L: INT); +begin + if L # NoLabel then labelAt := Chunk.pc; end; + fixLinkTo(L, Chunk.pc); +end; + +(* If the last instruction made is a b to L, it is taken back, as in + X64: L is that b when it is the last word and an unconditional b + (000101 in the top bits), and no jump was fixed to just after it. + L becomes the jump before it on the chain. *) +proc dropJumpTo*(var L: INT); +begin + if (L = NoLabel) or (L # Chunk.pc - 4) or (labelAt = Chunk.pc) or ((Chunk.get4At(L) & 0FC000000H) # 14000000H) then return; end; + L := before(L); + Chunk.takeBack(4); + if listing and (numSaid > 0) and (said[numSaid - 1].at = Chunk.pc) then DEC(numSaid); end; +end; + +(* The jumps of chains L1 and L2, as one chain: the last of L2's links to + L1's first *) +proc joinLinks*(L1, L2: INT): INT; +var L: INT; +begin + if L2 = NoLabel then return L1; end; + if L1 # NoLabel then + L := L2; + while before(L) # NoLabel do L := before(L); end; + Chunk.put4At(L, withField(Chunk.get4At(L) & 0FFFFFFFFH, L1, false)); + end; + return L2; +end; + +(* The assembler, for code procedures: arm64 in Arm's syntax, as its + reference manual writes it, destination first (report, appendix A): + `add x0, x1, #16;`, `ldr w2, [x0, #4];`, `stp x29, x30, [sp, #-16]!;`, + `b.ne loop;`. The # before an immediate may be left out; numbers are + raz's, 10H and not 0x10, and a constant of the module stands where a + number does. A procedure or a module variable stands for its address + in `bl p`, `b p` and `adr xN, v`, the last made as adrp and add, two + instructions, since the variable may lie beyond adr's 1 MB. It reads + instructions with the scanner up to end, as X64's assembler does, and + encodes them with the routines above, the listing off: an immediate + or offset out of an instruction's range is an error here, where Gen's + routines build it in x16. *) + +const + NameOnly = "a procedure or a variable stands for its address only in bl p, b p and adr r, v"; + +type + OpKind = (oReg, oFp, oImm, oMem, oShift, oName, oLabel, oWord); + Operand = record + kind: OpKind; + r, sf: INT; (* oReg: the register, and 1 for x, 0 for w; oFp, oMem: the register *) + sp: BOOL; (* oReg: 31 written sp or wsp, not xzr or wzr *) + size: INT; (* oFp: 4, 8 or 16 *) + v: I64; (* oImm; oShift: the amount; oMem: the offset, or the index's shift *) + sh: INT; (* oShift: lsl, lsr, asr or ror *) + index, mode: INT; (* oMem: an index register or -1, mOff, mPre or mPost *) + scaled: BOOL; (* oMem: the index shifted by lsl *) + c: INT; (* oName: the chunk; oLabel: the label *) + w: []const U8; (* oWord: a condition or a barrier's option *) + at: Diag.Pos; + end; + +var + (* the labels of the procedure *) + lnames, lplaces, lchains: [64]INT; + lusedAt: [64]Diag.Pos; + numLabels: INT; + (* the instruction being assembled *) + aw: []const U8; + ao: [6]Operand; + an, acc: INT; (* how many operands; b.cond's condition *) + aat: Diag.Pos; + +proc asmWord: []const U8; +begin + if Scan.tok = Scan.sIdent then return Scan.nameOf(Scan.id); end; + if Scan.tok = Scan.sAnd then return "and"; end; + return ""; +end; + +(* The number w spells from its second letter, one or two digits, below + lim; else -1 *) +proc numberIn(w: []const U8; lim: INT): INT; +var i, n: INT; +begin + if (LEN(w) < 2) or (LEN(w) > 3) then return -1; end; + n := 0; + for i := 1 to LEN(w) - 1 do + if (w[i] < '0') or (w[i] > '9') then return -1; end; + n := n * 10 + INT(w[i] - '0'); + end; + if n >= lim then return -1; end; + return n; +end; + +(* Whether w names a general register, x0 to x30, w0 to w30, sp, wsp, + xzr, wzr, fp or lr, or a real's, s, d or q and 0 to 31: o is set if so *) +proc isReg(w: []const U8; var o: Operand): BOOL; +begin + o.kind := oReg; o.sf := 1; + if LEN(w) < 2 then + return false; + elsif (w = "sp") or (w = "xzr") or (w = "wsp") or (w = "wzr") then + o.r := 31; o.sf := bit(w[0] # 'w'); o.sp := (w = "sp") or (w = "wsp"); + elsif (w = "fp") or (w = "lr") then + o.r := 29 + bit(w = "lr"); + elsif ((w[0] = 'x') or (w[0] = 'w')) and (numberIn(w, 31) >= 0) then + o.r := numberIn(w, 31); o.sf := bit(w[0] = 'x'); + elsif ((w[0] = 's') or (w[0] = 'd') or (w[0] = 'q')) and (numberIn(w, 32) >= 0) then + o.kind := oFp; o.r := numberIn(w, 32); o.sf := 0; + if w[0] = 's' then o.size := 4; elsif w[0] = 'd' then o.size := 8; else o.size := 16; end; + else + return false; + end; + return true; +end; + +proc wordIndex(w: []const U8; list: []const []const U8): INT; +var i: INT; +begin + for i := 0 to LEN(list) - 1 do + if list[i] = w then return i; end; + end; + return -1; +end; + +(* The condition w names, cs and cc as hs and lo; -1 if none *) +proc cond(w: []const U8): INT; +begin + if w = "cs" then return ccHS; end; + if w = "cc" then return ccLO; end; + return wordIndex(w, CondNames); +end; + +proc labelIndex(n: INT): INT; +var i: INT; +begin + for i := 0 to numLabels - 1 do + if lnames[i] = n then return i; end; + end; + return -1; +end; + +proc lab(n: INT): (INT, ERROR); +var i: INT; +begin + i := labelIndex(n); + if i >= 0 then return i, nil; end; + if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; + lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; + lusedAt[numLabels] := Scan.tokPos(); + INC(numLabels); + return numLabels - 1, nil; +end; + +(* Every label, entered before an instruction is read, as in X64 *) +proc findLabels; +var st: Scan.State; wasQuiet, start: BOOL; w: []const U8; +begin + Scan.saveScan(st); + wasQuiet := Diag.quiet; + Diag.setQuiet(true); + start := true; + while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do + w := ""; + if start then w := asmWord(); end; + start := Scan.tok = Scan.sSemicolon; + Scan.next; + if (LEN(w) > 0) and (Scan.tok = Scan.sColon) and (numLabels < 64) then + _, _ := lab(Scan.intern(SYSTEM.PTR(w), LEN(w))); + start := true; + Scan.next; + end; + end; + Diag.setQuiet(wasQuiet); + Scan.restoreScan(st); +end; + +(* An immediate, with or without its #: # is raz's "not equal" *) +proc immediate(constExpr: ConstFn): (I64, ERROR); +begin + if Scan.tok = Scan.sNeq then Scan.next; end; + return constExpr(true); +end; + +(* [xn], [xn, #off], [xn, xm], [xn, xm, lsl #s], then ! for pre-index, + or , #off after for post-index *) +proc memory(var o: Operand; constExpr: ConstFn): ERROR; +var b: Operand; st: Scan.State; +begin + Scan.next; + o.kind := oMem; o.index := -1; o.mode := mOff; + if not isReg(asmWord(), b) or (b.kind # oReg) or (b.sf = 0) or ((b.r = 31) and not b.sp) then + return Scan.mark("expected a memory operand, [xn, #offset] or [xn, xm]"); + end; + o.r := b.r; + Scan.next; + if Scan.tok = Scan.sComma then + Scan.next; + if isReg(asmWord(), b) then + if (b.kind # oReg) or (b.sf = 0) or (b.r = 31) then return Scan.mark("expected an index register, x0 to x30"); end; + o.index := b.r; + Scan.next; + if Scan.tok = Scan.sComma then + Scan.next; + if asmWord() # "lsl" then return Scan.mark("expected lsl"); end; + Scan.next; + o.scaled := true; + end; + end; + if (o.index < 0) or o.scaled then o.v := try immediate(constExpr); end; + end; + if Scan.tok # Scan.sRbrak then return Scan.mark("expected \"]\""); end; + Scan.next; + if Scan.tok = Scan.sNot then + o.mode := mPre; + Scan.next; + elsif (Scan.tok = Scan.sComma) and (o.v = 0) and (o.index < 0) then + Scan.saveScan(st); + Scan.next; + if (Scan.tok = Scan.sNeq) or (Scan.tok = Scan.sInt) or (Scan.tok = Scan.sMinus) then + o.mode := mPost; + o.v := try immediate(constExpr); + else + Scan.restoreScan(st); + end; + end; + return nil; +end; + +(* An operand: a register, a memory operand, a shift (lsl #12), an + immediate; a label, where target is set (a jump's last operand), + entered if new, which assemble finds undefined; a name, a procedure or + a variable, by nameFn; or, where word is set, a condition or a + barrier's option. *) +proc operand(var o: Operand; target, word: BOOL; constExpr: ConstFn; nameFn: NameFn): ERROR; +var w: []const U8; k: INT; +begin + o := {}; + w := asmWord(); + o.at := Scan.tokPos(); + if Scan.tok = Scan.sLbrak then + try memory(o, constExpr); + elsif isReg(w, o) then + Scan.next; + elsif wordIndex(w, ShiftNames) >= 0 then + o.kind := oShift; o.sh := wordIndex(w, ShiftNames); + Scan.next; + o.v := try immediate(constExpr); + elsif word and (Scan.tok = Scan.sIdent) then + o.kind := oWord; o.w := w; + Scan.next; + else + o.c := -1; + k := -1; + if Scan.tok = Scan.sIdent then + k := labelIndex(Scan.id); + if (k >= 0) and not target then return Scan.mark(Diag.say("label {} is only for a jump", [Fmt.str(w)])); end; + if k < 0 then o.c := try nameFn(); end; + end; + if (k >= 0) or target and (o.c < 0) and (Scan.tok = Scan.sIdent) then + o.kind := oLabel; + o.c := try lab(Scan.id); + Scan.next; + elsif o.c >= 0 then + o.kind := oName; + else + o.kind := oImm; + o.v := try immediate(constExpr); + end; + end; + return nil; +end; + +proc bad: ERROR; +begin + return Scan.markAt(aat, Diag.say("can't assemble {} with these operands", [Fmt.str(aw)])); +end; + +(* Whether the operands are kinds, one letter each: r a general + register, f a real's, i an immediate, m memory, s a shift, n a name, + l a label, w a word; and as many *) +proc are(kinds: []const U8): BOOL; +const Letters = "rfimsnlw"; +var i: INT; +begin + if an # LEN(kinds) then return false; end; + for i := 0 to an - 1 do + if Letters[INT(ao[i].kind)] # kinds[i] then return false; end; + end; + return true; +end; + +(* Whether the n operands from first are general registers of the + width of operand 0, none of them written sp *) +proc regs(first, n: INT): BOOL; +var i: INT; +begin + for i := first to first + n - 1 do + if (i >= an) or (ao[i].kind # oReg) or (ao[i].sf # ao[0].sf) or ao[i].sp then return false; end; + end; + return true; +end; + +proc fits(v, lo, hi: I64): BOOL; +begin + return (v >= lo) and (v <= hi); +end; + +(* The zero register, of operand 0's width, put in as operand i: mov is + orr of it, neg sub from it, cmp subs to it *) +proc insertZR(i: INT); +var j, sf: INT; +begin + sf := ao[0].sf; + for j := an to i + 1 by -1 do ao[j] := ao[j - 1]; end; + ao[i] := {}; ao[i].kind := oReg; ao[i].r := ZR; ao[i].sf := sf; + INC(an); +end; + +(* a branch to label k: back if it is placed, else on its chain *) +proc toLabel(insn, k: INT); +begin + if lplaces[k] >= 0 then + _ := branch(insn, lplaces[k], true); + else + lchains[k] := branch(insn, lchains[k], false); + end; +end; + +(* The shift that is operand i, if there is one, the last: its kind and + amount, or lsl 0; false if it isn't a shift within the width *) +proc shiftAt(i: INT; var sh, n: INT): BOOL; +begin + sh := shLsl; n := 0; + if an = i then return true; end; + if (an # i + 1) or (ao[i].kind # oShift) or not fits(ao[i].v, 0, 31 + 32 * ao[0].sf) then return false; end; + sh := ao[i].sh; n := INT(ao[i].v); + return true; +end; + +(* add, adds, sub, subs, and, orr, eor, ands, bic and orn, op, of + operands 0, 1 and 2: of an immediate, 12 bits with lsl #12 or + negative for the opposite operation, or a logical immediate; or of a + register, shifted, or extended where sp is one *) +proc alu3(op: INT): ERROR; +var sh, n, enc: INT; v: I64; d: Operand; +begin + d := ao[0]; + if (d.kind # oReg) or (an < 3) or (ao[1].kind # oReg) or (ao[1].sf # d.sf) or ao[1].sp and (op > opSubs) then return bad(); end; + if ao[2].kind = oImm then + v := ao[2].v; + if op > opSubs then + enc := bitmask(v, d.sf); + if (an # 3) or (op > opAnds) or (enc < 0) or (d.sf = 0) and not fits(v, -80000000H, 0FFFFFFFFH) + or (d.r = 31) and (d.sp = (op = opAnds)) then return bad(); end; + logicImm(d.sf, op, d.r, ao[1].r, enc, v); + return nil; + end; + if not shiftAt(3, sh, n) or (sh # shLsl) or (n # 0) and (n # 12) then return bad(); end; + if (v < 0) and (n = 0) then v := -v; op := op xor 2; end; + if not fits(v, 0, 4095) or (d.r = 31) and (d.sp = ((op & 1) = 1)) or (ao[1].r = 31) and not ao[1].sp then return bad(); end; + addImm(d.sf, op, d.r, ao[1].r, INT(v), n div 12); + return nil; + end; + if not regs(2, 1) or not shiftAt(3, sh, n) then return bad(); end; + if (op <= opSubs) and (d.sp or ao[1].sp) then + if (sh # shLsl) or (n > 4) or (d.r = 31) and (d.sp = ((op & 1) = 1)) then return bad(); end; + addExt(d.sf, op, d.r, ao[1].r, ao[2].r, n); + elsif d.sp or (sh = shRor) and (op <= opSubs) then + return bad(); + else + aluShifted(d.sf, op, d.r, ao[1].r, ao[2].r, sh, n); + end; + return nil; +end; + +(* ldr, str and their kin: the size from the name's end (b, h, sb, sh, + sw) or from the register; ldur and stur, and an offset that can't + be scaled, are unscaled *) +proc loadStore(): ERROR; +var name, sfx: []const U8; size, sf, bits, sc: INT; load, signed, fp, unscaled: BOOL; m: Operand; +begin + unscaled := aw[2] = 'u'; + name := aw; + if unscaled then name := Diag.say("{}{}", [Fmt.str(aw[0..2]), Fmt.str(aw[3..LEN(aw)])]); end; + load := name[0] = 'l'; + sfx := name[3..LEN(name)]; + if not are("rm") and not are("fm") then return bad(); end; + m := ao[1]; + fp := ao[0].kind = oFp; + sf := ao[0].sf; + signed := (LEN(sfx) = 2) and (sfx[0] = 's'); + if fp and (LEN(sfx) = 0) then size := ao[0].size; + elsif sfx = "" then size := 4 + 4 * sf; + elsif (sfx = "b") or (sfx = "sb") then size := 1; + elsif (sfx = "h") or (sfx = "sh") then size := 2; + elsif sfx = "sw" then size := 4; + else return bad(); + end; + if fp and (LEN(sfx) # 0) or signed and not load or (sfx = "sw") and (sf = 0) or (LEN(sfx) = 1) and (sf = 1) or ao[0].sp then return bad(); end; + bits := ldstBits(size, load, signed, fp, sf); + sc := scaleOf(size); + if (m.mode # mOff) or unscaled then + if (m.index >= 0) or (m.mode # mOff) and unscaled or not fits(m.v, -256, 255) then return bad(); end; + if m.mode # mOff then ldstIdx(bits, ao[0].r, m.r, INT(m.v), m.mode); + else emit(38000000H | bits | (INT(m.v & 1FFH) shl 12) | (m.r shl 5) | ao[0].r); end; + elsif m.index >= 0 then + if m.scaled and (m.v # sc) and (m.v # 0) then return bad(); end; + ldstReg(bits, ao[0].r, m.r, m.index, m.scaled and (m.v = sc)); + elsif fits(m.v, 0, 4095 shl sc) and ((m.v & ((1 shl sc) - 1)) = 0) or fits(m.v, -256, 255) then + ldst(bits, ao[0].r, m.r, INT(m.v)); + else + return bad(); + end; + return nil; +end; + +const + (* the aliases of alu3's operations with the zero register put in: + cmp and cmn as subs and adds to it, tst ands to it, neg sub from + it, mvn orn of it *) + Aliases: [5][]const U8 = ["cmp", "cmn", "tst", "neg", "mvn"]; + AliasOps: [5]INT = [opSubs, opAdds, opAnds, opSub, opOrn]; + (* the instructions of only registers, regOp's: their bits and how + many registers *) + RegNames: [14][]const U8 = ["mul", "madd", "msub", "smulh", "umulh", "sdiv", "udiv", + "lsl", "lsr", "asr", "ror", "clz", "rbit", "rev"]; + RegBits: [14]INT = [1B007C00H, 1B000000H, 1B008000H, 9B407C00H, 9BC07C00H, 1AC00C00H, 1AC00800H, + 1AC02000H, 1AC02400H, 1AC02800H, 1AC02C00H, 5AC01000H, 5AC00000H, 5AC00800H]; + RegCount: [14]INT = [3, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2]; + Extends: [5][]const U8 = ["sxtb", "sxth", "sxtw", "uxtb", "uxth"]; + Barriers: [6][]const U8 = ["sy", "ish", "ishld", "ishst", "osh", "nsh"]; + BarrierCodes: [6]INT = [15, 11, 9, 10, 3, 7]; + (* the instructions of no operands *) + Plain: [4][]const U8 = ["nop", "yield", "isb", "ret"]; + PlainBits: [4]INT = [0D503201FH, 0D503203FH, 0D5033FDFH, 0D65F03C0H]; + (* between the register files, as fconv takes them: rmode * 8 + + opcode; the first two into a real's register *) + Convs: [4][]const U8 = ["scvtf", "ucvtf", "fcvtzs", "fcvtzu"]; + ConvCodes: [4]INT = [02H, 03H, 18H, 19H]; + +(* The instruction aw with its operands, in ao and an *) +proc instruction(): ERROR; +var k, sf, size, bits, g, f: INT; d: Operand; v: I64; +begin + d := ao[0]; + sf := d.sf; + if wordIndex(aw, Aliases) >= 0 then + insertZR(bit(wordIndex(aw, Aliases) >= 3)); + return alu3(AliasOps[wordIndex(aw, Aliases)]); + elsif wordIndex(aw, AluNames) >= 0 then + return alu3(wordIndex(aw, AluNames)); + elsif aw = "mov" then + (* movImm's choice for an immediate; add #0 with sp, else orr *) + if are("ri") and not d.sp and ((sf = 1) or fits(ao[1].v, -80000000H, 0FFFFFFFFH)) then + movImm(sf, d.r, ao[1].v); + return nil; + elsif are("rr") and (d.sp or ao[1].sp) then + ao[2] := {}; ao[2].kind := oImm; an := 3; + return alu3(opAdd); + end; + insertZR(1); + return alu3(opOrr); + elsif (aw = "movz") or (aw = "movk") or (aw = "movn") then + k := 0; + if (an = 3) and (ao[2].kind = oShift) and (ao[2].sh = shLsl) and ((ao[2].v & 15) = 0) and fits(ao[2].v, 0, 16 + 32 * sf) then + k := INT(ao[2].v) div 16; + DEC(an); + end; + if not are("ri") or d.sp or not fits(ao[1].v, 0, 0FFFFH) then return bad(); end; + movWide(sf, wordIndex(aw, ["movn", "", "movz", "movk"]), d.r, INT(ao[1].v), k); + elsif (wordIndex(aw, ShiftNames) >= 0) and are("rri") then + if not regs(1, 1) or not fits(ao[2].v, 0, 31 + 32 * sf) then return bad(); end; + shiftI(wordIndex(aw, ShiftNames), d.r, ao[1].r, INT(ao[2].v), 4 + 4 * sf); + elsif wordIndex(aw, RegNames) >= 0 then + (* smulh and umulh are of x only; rev's opc is 11 for x, 10 for w *) + k := wordIndex(aw, RegNames); + if (an # RegCount[k]) or not regs(0, an) or ((k = 3) or (k = 4)) and (sf = 0) then return bad(); end; + bits := RegBits[k]; + if aw = "rev" then bits := bits | (sf shl 10); end; + while an < 4 do ao[an].r := 0; INC(an); end; + regOp(aw, RegCount[k], sf, bits & 7FFFFFFFH, d.r, ao[1].r, ao[2].r, ao[3].r); + elsif wordIndex(aw, CselNames) >= 0 then + if (an # 4) or not regs(0, 3) or (ao[3].kind # oWord) or (cond(ao[3].w) < 0) then return bad(); end; + condSel(sf, wordIndex(aw, CselNames), cond(ao[3].w), d.r, ao[1].r, ao[2].r); + elsif (aw = "cset") or (aw = "csetm") then + (* csinc and csinv of the zero register on the opposite condition *) + if (an # 2) or not regs(0, 1) or (ao[1].kind # oWord) or (cond(ao[1].w) < 0) then return bad(); end; + condSel(sf, csInc + bit(aw = "csetm"), invert(cond(ao[1].w)), d.r, ZR, ZR); + elsif wordIndex(aw, Extends) >= 0 then + k := wordIndex(aw, Extends); + if (an # 2) or (d.kind # oReg) or d.sp or (ao[1].kind # oReg) or (ao[1].sf # 0) or ao[1].sp + or (k >= 3) and (sf = 1) or (k = 2) and (sf = 0) then return bad(); end; + bitfield(sf, 2 * bit(k >= 3), d.r, ao[1].r, 0, 8 * (1 shl (k mod 3)) - 1); + elsif (aw = "ubfm") or (aw = "sbfm") or (aw = "ubfx") or (aw = "sbfx") then + if (an # 4) or not regs(0, 2) or (ao[2].kind # oImm) or (ao[3].kind # oImm) then return bad(); end; + v := ao[3].v; + if aw[3] = 'x' then v := ao[2].v + v - 1; end; (* ubfx's width, as ubfm's last bit *) + if not fits(ao[2].v, 0, 31 + 32 * sf) or not fits(v, 0, 31 + 32 * sf) then return bad(); end; + bitfield(sf, 2 * bit(aw[0] = 'u'), d.r, ao[1].r, INT(ao[2].v), INT(v)); + elsif Str.hasPrefix(aw, "ldr") or Str.hasPrefix(aw, "str") or Str.hasPrefix(aw, "ldur") or Str.hasPrefix(aw, "stur") then + return loadStore(); + elsif (aw = "ldp") or (aw = "stp") then + if not are("rrm") and not are("ffm") then return bad(); end; + size := 4 + 4 * sf; + if d.kind = oFp then size := d.size; end; + if (ao[1].sf # sf) or (ao[1].size # d.size) or d.sp or ao[1].sp or (ao[2].index >= 0) + or ((ao[2].v & (size - 1)) # 0) or not fits(ao[2].v, -64 * size, 63 * size) then return bad(); end; + pair(aw[0] = 'l', d.kind = oFp, size, d.r, ao[1].r, ao[2].r, INT(ao[2].v), ao[2].mode); + elsif wordIndex(aw, AtomNames) >= 0 then + (* ldaxr has no rs, which is 31 in its bits; stlxr's is a w *) + k := wordIndex(aw, AtomNames); + if aw = "ldaxr" then insertZR(0); end; + if not are("rrm") or ao[0].sp or ao[1].sp or (ao[2].index >= 0) or (ao[2].v # 0) or (ao[2].mode # mOff) + or (k < 3) and (ao[1].sf # sf) or (k = 4) and (sf = 1) then return bad(); end; + atomic(k, 4 + 4 * ao[1].sf, ao[0].r, ao[1].r, ao[2].r); + elsif aw = "adr" then + if not are("rn") or (sf = 0) or d.sp then return bad(); end; + addrChunk(d.r, ao[1].c, 0); + elsif ((aw = "b") or (aw = "bl")) and are("n") then + branchChunk(aw = "bl", d.c); + elsif (aw = "b") and are("l") then + toLabel(14000000H, d.c); + elsif (aw = "b.") and are("l") then + toLabel(54000000H | acc, d.c); + elsif ((aw = "cbz") or (aw = "cbnz")) and are("rl") and not d.sp then + toLabel((sf shl 31) | 34000000H | (bit(aw = "cbnz") shl 24) | d.r, ao[1].c); + elsif ((aw = "tbz") or (aw = "tbnz")) and are("ril") and not d.sp and fits(ao[1].v, 0, 31 + 32 * sf) then + k := INT(ao[1].v); + toLabel(36000000H | (bit(aw = "tbnz") shl 24) | ((k shr 5) shl 31) | ((k & 31) shl 19) | d.r, ao[2].c); + elsif (aw = "br") or (aw = "blr") or (aw = "ret") and (an = 1) then + (* opc in bits 21 and 22: 00 br, 01 blr, 10 ret *) + if not are("r") or (sf = 0) or d.sp then return bad(); end; + emit(0D61F0000H | (wordIndex(aw, ["br", "blr", "ret"]) shl 21) | (d.r shl 5)); + elsif (aw = "svc") or (aw = "brk") then + if not are("i") or not fits(d.v, 0, 0FFFFH) then return bad(); end; + if aw = "svc" then svc(INT(d.v)); else brk(INT(d.v)); end; + elsif (aw = "dmb") or (aw = "dsb") then + if not are("w") or (wordIndex(d.w, Barriers) < 0) then return bad(); end; + emit(0D503309FH | (bit(aw = "dmb") shl 5) | (BarrierCodes[wordIndex(d.w, Barriers)] shl 8)); + elsif wordIndex(aw, Plain) >= 0 then + if an # 0 then return bad(); end; + emit(PlainBits[wordIndex(aw, Plain)]); + elsif wordIndex(aw, FopNames) >= 0 then + if not are("fff") or (d.size = 16) or (ao[1].size # d.size) or (ao[2].size # d.size) then return bad(); end; + fop(wordIndex(aw, FopNames), d.r, ao[1].r, ao[2].r, d.size = 4); + elsif (wordIndex(aw, Fop1Names) >= 0) and are("ff") and (d.size # 16) and (ao[1].size = d.size) then + fop1(wordIndex(aw, Fop1Names), d.r, ao[1].r, d.size = 4); + elsif (aw = "fmov") and are("fr") and (d.size = 4 + 4 * ao[1].sf) and not ao[1].sp then + fmovFR(d.r, ao[1].r, d.size = 4); + elsif (aw = "fmov") and are("rf") and (ao[1].size = 4 + 4 * sf) and not d.sp then + fmovRF(d.r, ao[1].r, ao[1].size = 4); + elsif (aw = "fcmp") and are("ff") and (ao[1].size = d.size) and (d.size # 16) then + fcmp(d.r, ao[1].r, d.size = 4); + elsif (aw = "fcmp") and are("fi") and (ao[1].v = 0) and (d.size # 16) then + fcmp(d.r, -1, d.size = 4); + elsif (aw = "fcvt") and are("ff") and (d.size + ao[1].size = 12) then + fcvt(d.r, ao[1].r, d.size = 4); + elsif wordIndex(aw, Convs) >= 0 then + (* scvtf and ucvtf from operand g, a general register, into f, a + real's; fcvtzs and fcvtzu the other way *) + k := wordIndex(aw, Convs); + g := bit(k < 2); f := 1 - g; + if (ao[g].kind # oReg) or (ao[f].kind # oFp) or (an # 2) or ao[g].sp or (ao[f].size = 16) then return bad(); end; + fconv(aw, ao[g].sf, ao[f].size, ConvCodes[k] shr 3, ConvCodes[k] & 7, d.r, ao[1].r, k < 2); + else + return Scan.markAt(aat, Diag.say("unknown instruction {}", [Fmt.str(aw)])); + end; + return nil; +end; + +(* Which operand of instruction w is a jump's target, which may be a + label: -1 if none *) +proc targetOf(w: []const U8): INT; +begin + if (w = "b") or (w = "bl") or (w = "b.") then return 0; end; + if (w = "cbz") or (w = "cbnz") then return 1; end; + if (w = "tbz") or (w = "tbnz") then return 2; end; + return -1; +end; + +(* Whether operand i of instruction w is a word: a condition or a + barrier's option *) +proc wordAt(w: []const U8; i: INT): BOOL; +begin + return (wordIndex(w, CselNames) >= 0) and (i = 3) or ((w = "cset") or (w = "csetm")) and (i = 1) + or ((w = "dmb") or (w = "dsb")) and (i = 0); +end; + +(* db's bytes or dd's words, each a constant *) +proc data(constExpr: ConstFn): ERROR; +var v: I64; start: Diag.Pos; n: INT; +begin + n := 1 + 3 * bit(aw = "dd"); + repeat + start := Scan.tokPos(); + v := try constExpr(true); + if (v < -(I64(1) shl (8 * n - 1))) or (v >= I64(1) shl (8 * n)) then + return Scan.markAt(Scan.spanFrom(start), Diag.say("{} doesn't fit in {} bits", [Fmt.int(v), Fmt.int(8 * n)])); + end; + if n = 1 then Chunk.put1(INT(v & 0FFH)); else Chunk.put4(INT(v & 0FFFFFFFFH)); end; + if Scan.tok # Scan.sComma then break; end; + Scan.next; + until false; + return nil; +end; + +(* One instruction or label *) +proc statement(constExpr: ConstFn; nameFn: NameFn): ERROR; +var i, k, first: INT; wasListing: BOOL; +begin + aat := Scan.tokPos(); + aw := asmWord(); + if LEN(aw) = 0 then return Scan.mark("expected an instruction"); end; + Scan.next; + if Scan.tok = Scan.sColon then + sayAt(Chunk.pc, Diag.say("{}:", [Fmt.str(aw)])); + k := try lab(Scan.intern(SYSTEM.PTR(aw), LEN(aw))); + if lplaces[k] >= 0 then + return Scan.markAt(aat, Diag.say("label {} is defined twice", [Fmt.str(aw)])); + end; + lplaces[k] := Chunk.pc; + fixLink(lchains[k]); + Scan.next; + return nil; + end; + if (aw = "b") and (Scan.tok = Scan.sPeriod) then + (* b.cond: the condition, after the period *) + Scan.next; + acc := cond(asmWord()); + if acc < 0 then return Scan.mark("expected a condition, as eq of b.eq"); end; + aw := "b."; + Scan.next; + end; + first := Chunk.pc; + wasListing := listing; + listing := false; (* the instruction is said as it is written *) + if (aw = "db") or (aw = "dd") then + try data(constExpr); + else + an := 0; + if Scan.tok # Scan.sSemicolon then + repeat + if an > 3 then return Scan.mark("too many operands"); end; + try operand(ao[an], an = targetOf(aw), wordAt(aw, an), constExpr, nameFn); + INC(an); + if Scan.tok # Scan.sComma then break; end; + Scan.next; + until false; + end; + (* a name stands only in bl p, b p and adr r, v *) + for i := 0 to an - 1 do + if (ao[i].kind = oName) and not ((i = 0) and ((aw = "b") or (aw = "bl")) or (i = 1) and (aw = "adr")) then + return Scan.markAt(ao[i].at, NameOnly); + end; + end; + try instruction(); + end; + if Scan.tok # Scan.sSemicolon then return Scan.mark("expected \";\""); end; + listing := wasListing; + sayAt(first, Diag.source(Scan.spanFrom(aat))); + Scan.next; + return nil; +end; + +(* A code procedure's instructions, up to end. ! of a pre-index is a + symbol of its own meanwhile (Scan.setBang), where elsewhere it is a + slip for not. *) +proc assemble*(constExpr: ConstFn; nameFn: NameFn): ERROR; +var k: INT; +begin + Scan.setBang(true); + defer Scan.setBang(false); + numLabels := 0; + findLabels(); + while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do + if Scan.tok = Scan.sSemicolon then + Scan.next; + else + try statement(constExpr, nameFn); + end; + end; + for k := 0 to numLabels - 1 do + if lplaces[k] < 0 then + return Scan.markAt(lusedAt[k], Diag.say("label {} isn't defined", [Fmt.str(Scan.nameOf(lnames[k]))])); + end; + end; + return nil; +end; + +(* n, in decimal, at least w wide *) +proc padded(n, w: INT); +var s: []const U8; i: INT; +begin + s := Diag.say("{}", [Fmt.int(n)]); + for i := LEN(s) to w - 1 do Out.str(" "); end; + Out.str(s); +end; + +(* The listing, raz -S, as X64's: a jump within the procedure, said with + -> for its target, says where to by its offset, read from its field *) +proc list*(home: INT); +var order: ^INT; n, i, j, k, c, t, line, insn, lo, w, d: INT; text: []const U8; +begin + order := ^INT(Scan.grab(Chunk.numChunks * SIZE(INT))); + n := 0; + for c := 0 to Chunk.numChunks - 1 do + if Chunk.chunks[c].live and (Chunk.chunks[c].sec = Chunk.secText) and (Chunk.chunks[c].home = home) then + i := n; + while (i > 0) and (Chunk.chunks[order[i - 1]].start > Chunk.chunks[c].start) do + order[i] := order[i - 1]; + DEC(i); + end; + order[i] := c; + INC(n); + end; + end; + i := 0; j := 0; + for k := 0 to n - 1 do + c := order[k]; + Out.ln; + Out.str(Chunk.chunkName(c)); Out.str(":\n"); + line := -1; + while (i < numSaid) and (said[i].at < Chunk.chunks[c].start) do INC(i); end; + while (i < numSaid) and (said[i].at < Chunk.chunks[c].start + Chunk.chunks[c].size) do + while (j < Chunk.numLines) and (Chunk.lines[j].at <= said[i].at) do + if (Chunk.lines[j].chunk = c) and (Chunk.lines[j].line # line) then + line := Chunk.lines[j].line; + padded(line, 6); + Out.str(" "); + Out.str(Diag.lineText(Scan.nameOf(Chunk.lines[j].path), line)); + Out.ln; + end; + INC(j); + end; + text := said[i].text; + t := said[i].at; + if (LEN(text) > 2) and (text[LEN(text) - 2..LEN(text)] = "->") then + insn := Chunk.get4At(t) & 0FFFFFFFFH; + lo, w := fieldOf(insn); + d := (insn shr lo) & ((1 shl w) - 1); + if d >= (1 shl (w - 1)) then d := d - (1 shl w); end; + text := Diag.say("{}{}", [Fmt.str(text[0..LEN(text) - 2]), Fmt.int(t + 4 * d - Chunk.chunks[c].start)]); + end; + padded(t - Chunk.chunks[c].start, 14); + Out.str(" "); + Out.str(text); + Out.ln; + INC(i); + end; + end; +end; + +begin + listing := false; + numSaid := 0; + saidCap := 0; + said := nil; +end A64. diff --git a/src/Chunk.raz b/src/Chunk.raz index e48479f..ad78165 100644 --- a/src/Chunk.raz +++ b/src/Chunk.raz @@ -9,10 +9,10 @@ point on is for. Nothing here depends on the machine: text is bytes, written by the - encoder for the target (X64). + encoder for the target (X64, or A64 for arm64). Depends on: Scan, for names and memory; Diag, for positions. - Leaves out: the instructions; that is X64. + Leaves out: the instructions; that is X64 or A64. Exported variables can't be set from outside, so the setters at the end do what the seed's other units do to these. *) @@ -52,12 +52,29 @@ kind*: INT; (* how Rt shows it: see Link *) end; + (* How a fixup's field is patched. fxRel32 is x86-64's: 32 bits, the + distance from the field's end, or with abs the address itself. The + others are arm64's (A64), each a field of the instruction at at, + as the Mach-O relocations of the same names: + fxBranch26 b or bl: the distance in words, in imm26 + fxPage21 adrp: the distance in 4 KB pages from the + instruction's page, in immhi:immlo + fxPageOff12 add, or a load or store's unsigned offset: the + address's place in its page, in imm12, divided + by the size a load or store reads from its own + bits (31 and 30, or 16 for a q register), as + ARM64_RELOC_PAGEOFF12 is + Link patches only fxRel32 yet: arm64's come with Exe.darwin.raz + (PLAN.md 5, step 6). *) + FixKind* = (fxRel32, fxBranch26, fxPage21, fxPageOff12); + Fixup* = record at*: INT; (* the 32-bit field, as an offset in text; in dataFixups, the 64-bit one, from its chunk's start *) chunk*: INT; (* the chunk it refers to *) addend*: INT; (* added to the chunk's address *) abs*: BOOL; (* text: the address itself, not from the field's end *) + kind*: FixKind; (* text: how the field is patched *) end; var @@ -249,7 +266,15 @@ fixups[numFixups].chunk := chunk; fixups[numFixups].addend := addend; fixups[numFixups].abs := false; + fixups[numFixups].kind := fxRel32; INC(numFixups); +end; + +(* The same, of an arm64 kind: the instruction at at *) +proc addKindFixup*(at, chunk, addend: INT; kind: FixKind); +begin + addFixup(at, chunk, addend); + fixups[numFixups - 1].kind := kind; end; (* The same, for X64.memAbs: the field holds the address itself *) diff --git a/src/Scan.raz b/src/Scan.raz index b554993..c85de64 100644 --- a/src/Scan.raz +++ b/src/Scan.raz @@ -89,6 +89,10 @@ error, so slips in it aren't reported again. *) skipping*: BOOL; + (* In arm64's code procedure, ! is a symbol, sNot, of a pre-index, as + in [sp, #-16]!, not a slip for not (A64.assemble) *) + bang: BOOL; + atCursor*: INT; (* the name the symbol at the cursor has *) prefix*: []const U8; (* what is typed of the name at the cursor *) word*: []const U8; (* the whole name at the cursor, as raz -q def asks of it *) @@ -122,6 +126,11 @@ skipping := yes; end; +proc setBang*(yes: BOOL); +begin + bang := yes; +end; + (* The bytes of the string just read, without the zero after them *) proc strBytes*: []const U8; begin @@ -1232,7 +1241,10 @@ tok := sBar; end; | '!': - if cur.buf[cur.pos + 1] = '=' then + if bang then + INC(cur.pos); + tok := sNot; + elsif cur.buf[cur.pos + 1] = '=' then slip(2, sNeq, "\"not equal\" is written #, not !="); else slip(1, sNot, "\"not\" is written not, not !"); diff --git a/test/a64/Api.s b/test/a64/Api.s new file mode 100644 index 0000000..6c8652a --- /dev/null +++ b/test/a64/Api.s @@ -0,0 +1,209 @@ +// Encode.raz's calls of A64, one line each in its order, in Apple's +// syntax: the instructions each call must make. expect.sh assembles it. +// moves +movz x0, #0 +movz x1, #0x1234 +movz x2, #0x1234, lsl #16 +movn x3, #0 +movn x4, #1, lsl #16 +orr x5, xzr, #0x00ff00ff00ff00ff +movz x6, #0xdef0 ; movk x6, #0x9abc, lsl #16 ; movk x6, #0x5678, lsl #32 ; movk x6, #0x1234, lsl #48 +movn x7, #0xa987 ; movk x7, #0x1234, lsl #32 +orr x8, xzr, #0x100000001 +orr x9, xzr, #0x5555555555555555 +movz x10, #0x8000, lsl #48 +movn x11, #0x8000, lsl #48 +orr x12, xzr, #0xffffffff +movz x13, #0xabcd ; movk x13, #0xffff, lsl #32 +orr x0, xzr, x1 +add sp, x29, #0 +add x29, sp, #0 +// two registers +add x0, x1, x2 +adds x3, x4, x5 +sub x6, x7, x8 +subs x9, x10, x11 +and x12, x13, x14 +orr x15, x16, x17 +eor x19, x20, x21 +ands x22, x23, x24 +bic x25, x26, x27 +orn x28, x29, x30 +add x0, x1, x2, lsl #3 +sub x3, x4, x5, asr #63 +orr x6, x7, x8, ror #1 +// immediates +add x0, x1, #4095 +add x0, x1, #1, lsl #12 +add sp, sp, #16 +sub sp, sp, #0x123, lsl #12 +sub x0, x1, #8 +sub x2, x3, #0x12, lsl #12 ; sub x2, x2, #0x345 +movz x16, #0x4567 ; movk x16, #0x123, lsl #16 ; add x4, sp, x16, uxtx +movz x16, #0x2345 ; movk x16, #0x1, lsl #16 ; adds x5, x6, x16, uxtx +and x0, x1, #0xff +orr sp, x1, #0xf0 +movz x16, #0x1234 ; eor x2, x3, x16 +subs xzr, x0, x1 +cmp sp, #32 +cmn x2, #5 +movz x16, #0x1001 ; movk x16, #0x100, lsl #16 ; subs xzr, x3, x16, uxtx +tst x4, x5 +tst x6, #1 +tst x7, #0x8000000000000000 +neg x0, x1 +mvn x2, x3 +// shifts and fields +lsl x0, x1, x2 +lsr x3, x4, x5 +asr x6, x7, x8 +ror x9, x10, x11 +lsl w3, w4, w5 +lsl x0, x1, #3 +lsr x0, x1, #63 +asr x2, x3, #1 +ror x4, x5, #7 +lsl w6, w7, #31 +ror w8, w9, #16 +ubfm x0, x1, #0, #63 +ubfx x0, x1, #8, #8 +sbfx x2, x3, #0, #32 +sxtb x0, w1 +sxth x2, w3 +sxtw x4, w5 +uxtb w6, w7 +uxth w8, w9 +mov w10, w11 +// multiply, divide, select, count +mul x0, x1, x2 +madd x3, x4, x5, x6 +msub x7, x8, x9, x10 +smulh x0, x1, x2 +umulh x3, x4, x5 +sdiv x3, x4, x5 +udiv x6, x7, x8 +csel x0, x1, x2, eq +csinc x3, x4, x5, ne +csneg x6, x7, x8, lt +cset x9, gt +clz x0, x1 +clz w2, w3 +rbit x4, x5 +// loads and stores +ldr x0, [x1] +ldr x0, [x1, #32760] +movz x16, #0x8000 ; ldr x2, [x3, x16] +ldur x4, [x5, #-8] +ldur x4, [x5, #-256] +ldur x6, [x7, #3] +ldrb w8, [x9, #4095] +ldrsb x10, [x11, #4] +ldrh w12, [x13, #8190] +ldrsh x14, [x15, #6] +ldr w0, [sp, #16380] +ldrsw x1, [x2, #4] +ldursb x3, [x4, #-1] +str x0, [x1, #8] +str w2, [sp] +sturh w3, [x4, #1] +strb w5, [x6, #4095] +str xzr, [x7] +ldr x0, [x1, x2, lsl #3] +ldrsb x3, [x4, x5] +ldr w6, [x7, x8, lsl #2] +strh w9, [x10, x11, lsl #1] +str x12, [x13, x14] +ldr x0, [sp], #16 +str x1, [sp, #-16]! +str w2, [x3, #-256]! +ldrb w4, [x5], #255 +ldr d0, [x1, #8] +ldr s2, [x3, #4] +ldr q4, [x5, #32] +str q6, [sp, #65520] +stur d7, [x8, #-4] +str s9, [x10] +stp x29, x30, [sp, #-16]! +ldp x29, x30, [sp], #16 +stp x19, x20, [sp, #504] +ldp x21, x22, [x1, #-512] +// atomics and the system's +ldaddal x1, x0, [x2] +ldaddal w3, w4, [x5] +swpal x6, x7, [x8] +swpal w9, w10, [x11] +casal x12, x13, [x14] +casal w15, w16, [x17] +dmb ish +svc #0x80 +svc #0 +brk #1 +brk #0xf000 +// reals +fmul d0, d1, d2 +fdiv d0, d1, d2 +fadd d0, d1, d2 +fsub d0, d1, d2 +fmax d0, d1, d2 +fmin d0, d1, d2 +fadd s3, s4, s5 +fmov d0, d1 +fabs d0, d1 +fneg d0, d1 +fsqrt d0, d1 +fsqrt s6, s7 +fcvt d0, s1 +fcvt s2, d3 +fcmp d0, d1 +fcmp s2, s3 +fcmp d4, #0.0 +fcmp s5, #0.0 +scvtf d0, x1 +scvtf s2, x3 +ucvtf d4, x5 +fcvtzs x0, d1 +fcvtzu x2, s3 +fmov d0, x1 +fmov s2, w3 +fmov x4, d5 +fmov w6, s7 +// addresses, calls +adr x0, . +adr x1, .-4 +adr x2, .+1048572 +adrp x0, _glob@PAGE ; add x0, x0, _glob@PAGEOFF +adrp x16, _glob@PAGE ; ldr x1, [x16, _glob@PAGEOFF] +adrp x16, _glob@PAGE ; str w2, [x16, _glob@PAGEOFF] +adrp x16, _glob@PAGE ; ldr d3, [x16, _glob@PAGEOFF] +adrp x16, _glob@PAGE ; str s4, [x16, _glob@PAGEOFF] +adrp x16, _byte@PAGE ; add x16, x16, _byte@PAGEOFF ; ldr x5, [x16] +bl _proc1 +b _proc1 +bl Lstart +blr x5 +br x6 +ret +// jumps within the procedure +b.eq L1 +cbz x3, L1 +cbnz x4, L1 +tbnz x5, #3, .+8 ; b L1 +tbz x6, #40, .+8 ; b L1 +b L1 +L1: +Ltop: +orr x0, xzr, x1 +b.ne Ltop +b Ltop +b L2 +b.lo L2 +b L2 +L2: +orr x1, xzr, x2 +.p2align 4 +// the farthest +b.ne Lfar +cbz x1, Lfar +.p2align 20 +Lfar: +b.eq Lstart diff --git a/test/a64/Asm.txt b/test/a64/Asm.txt new file mode 100644 index 0000000..2a1f10d --- /dev/null +++ b/test/a64/Asm.txt @@ -0,0 +1,58 @@ +(* A64's assembler on a code procedure's instructions, from just after + proc ... code, to end. expect.sh turns it into Apple's syntax. *) +Lat: +mov x0, x1; mov sp, x29; mov x29, sp; mov w2, w3; mov x4, 1234H; mov w5, -1; +mov x6, 0FFFF0000H; mov x7, -2; mov x8, #00FF00FF00FF00FFH; +movz x8, 0FFFFH, lsl 48; movk w9, 1, lsl 16; movn x10, 5; movz w11, #7; +add x0, x1, #16; add x0, x1, 1, lsl 12; add sp, sp, #32; sub sp, sp, #4095; +add x2, x3, -8; adds w4, w5, #1; cmp x6, #0; cmn w7, #3; cmp sp, #16; +add x8, x9, x10; add x11, x12, x13, lsl #2; sub w14, w15, w16, asr #31; +adds x17, x19, x20; subs x21, x22, x23, lsr #63; add sp, sp, x16; +add x0, sp, x1, lsl #3; cmp x1, x2; cmp w3, w4, lsl #4; cmn x5, x6; +neg x7, x8; neg w9, w10, lsl #2; mvn x11, x12; mvn w13, w14, ror #3; +and x0, x1, #0FFH; orr w2, w3, #0F0F0F0F0H; eor x4, x5, #5555555555555555H; +ands x6, x7, #1; tst x8, #8000000000000000H; tst w9, #0FFFFH; +and sp, x0, #-16; orr x10, x11, x12; eor w13, w14, w15, ror #8; +bic x16, x17, x19; orn w20, w21, w22, lsl #1; ands x23, x24, x25; tst x26, x27; +lsl x0, x1, x2; lsr w3, w4, w5; asr x6, x7, x8; ror w9, w10, w11; +lsl x0, x1, #12; lsr w2, w3, #31; asr x4, x5, #63; ror x6, x7, #1; ror w8, w9, #31; +mul x0, x1, x2; mul w3, w4, w5; madd x6, x7, x8, x9; msub w10, w11, w12, w13; +smulh x14, x15, x16; umulh x0, x1, x2; sdiv x3, x4, x5; udiv w6, w7, w8; +csel x0, x1, x2, eq; csinc w3, w4, w5, ne; csinv x6, x7, x8, hs; +csneg x9, x10, x11, lo; cset x12, mi; cset w13, ge; csetm x14, lt; +csel x15, x16, x17, cs; csel x0, x1, x2, cc; csel w3, wzr, w4, vs; +sxtb x0, w1; sxth w2, w3; sxtw x4, w5; uxtb w6, w7; uxth w8, w9; +ubfm x10, x11, #4, #7; sbfm w12, w13, #0, #15; ubfx x14, x15, #8, #16; sbfx x16, x17, #3, #61; +clz x0, x1; clz w2, w3; rbit x4, x5; rev x6, x7; rev w8, w9; +ldr x0, [x1]; ldr x2, [x3, #8]; ldr x4, [sp, #32760]; ldr w5, [x6, #16380]; +ldrb w7, [x8, #4095]; ldrh w9, [x10, #2]; ldrsb x11, [x12, #1]; ldrsb w13, [x14]; +ldrsh x15, [x16, #6]; ldrsh w17, [x19]; ldrsw x20, [x21, #4]; ldr x22, [x23, #-8]; +ldr x24, [x25, #3]; ldur x26, [x27, #8]; ldurb w0, [x1, #-1]; ldursw x2, [x3, #-256]; +ldr x4, [x5, x6]; ldr x7, [x8, x9, lsl #3]; ldrb w10, [x11, x12]; +ldrh w13, [x14, x15, lsl #1]; ldr w16, [x17, x19, lsl #2]; +ldr x0, [sp], #16; ldr x1, [x2, #-8]!; ldrb w3, [x4], #1; ldrsw x5, [x6, #255]!; +ldr d0, [x1, #8]; ldr s2, [x3]; ldr q4, [x5, #16]; ldr d6, [x7, x8, lsl #3]; ldur q9, [x10, #-16]; +str x0, [x1]; str w2, [sp, #4]; strb w3, [x4, #4095]; strh w5, [x6, #8190]; +str x7, [sp, #-16]!; str x8, [x9], #8; stur w10, [x11, #-4]; sturb w12, [x13, #255]; +str x14, [x15, x16, lsl #3]; str xzr, [x17]; str d0, [sp, #8]; str s1, [x2, #-4]; str q3, [x4, #65520]; +ldp x29, x30, [sp], #16; stp x29, x30, [sp, #-16]!; stp x19, x20, [sp, #504]; +ldp w0, w1, [x2, #-256]; ldp x3, x4, [x5]; stp d8, d9, [sp, #-512]!; +ldp q0, q1, [x0, #1008]; stp s2, s3, [x1], #-256; +ldaddal x0, x1, [x2]; ldaddal w3, w4, [sp]; swpal x5, x6, [x7]; swpal w8, w9, [x10]; +casal x11, x12, [x13]; casal w14, w15, [x16]; ldaxr x0, [x1]; ldaxr w2, [x3]; +stlxr w4, x5, [x6]; stlxr w7, w8, [x9]; +dmb ish; dmb sy; dsb ish; isb; nop; yield; svc #80H; svc 0; brk #1; brk 0F000H; +fmov d0, d1; fmov s2, s3; fmov d4, x5; fmov s6, w7; fmov x8, d9; fmov w10, s11; +fadd d0, d1, d2; fsub s3, s4, s5; fmul d6, d7, d8; fdiv s9, s10, s11; +fmax d12, d13, d14; fmin s15, s16, s17; fsqrt d0, d0; fsqrt s1, s2; fneg d3, d4; fabs s5, s6; +fcmp d0, d1; fcmp s2, s3; fcmp d4, #0; fcvt d0, s1; fcvt s2, d3; +scvtf d0, x1; scvtf s2, w3; ucvtf d4, w5; ucvtf s6, x7; +fcvtzs x0, d1; fcvtzs w2, s3; fcvtzu x4, s5; fcvtzu w6, d7; +Lloop: +cbz x0, Ldone; cbnz w1, Lloop; tbz x2, #3, Ldone; tbnz w3, #31, Lloop; tbz x4, #63, Ldone; +b.eq Lloop; b.ne Ldone; b.cs Lloop; b.hs Ldone; b.lo Lloop; b.al Ldone; +b Lloop; b Ldone; bl proc1; b proc1; adr x0, glob; adr x5, proc1; +br x1; blr x2; ret; ret x3; +Ldone: +db 1, 2, 3, 4; dd 0D503201FH, 12345678H, -1; +end diff --git a/test/a64/Encode.expect b/test/a64/Encode.expect new file mode 100644 index 0000000..ddc0c4c --- /dev/null +++ b/test/a64/Encode.expect @@ -0,0 +1,452 @@ +aa0103e0 +910003bf +910003fd +2a0303e2 +d2824684 +12800005 +d2bfffe6 +92800027 +b2009fe8 +d2ffffe8 +72a00029 +928000aa +528000eb +91004020 +91400420 +910083ff +d13fffff +d1002062 +310004a4 +f10000df +31000cff +f10043ff +8b0a0128 +8b0d098b +4b907dee +ab140271 +eb57fed5 +8b3063ff +8b216fe0 +eb02003f +6b04107f +ab0600bf +cb0803e7 +4b0a0be9 +aa2c03eb +2aee0fed +92401c20 +3204cc62 +d200f0a4 +f24000e6 +f241011f +72003d3f +927cec1f +aa0c016a +4acf21cd +8a330230 +2a3606b4 +ea190317 +ea1b035f +9ac22020 +1ac52483 +9ac828e6 +1acb2d49 +d374cc20 +531f7c62 +937ffca4 +93c704e6 +13897d28 +9b027c20 +1b057c83 +9b0824e6 +1b0cb56a +9b507dee +9bc27c20 +9ac50c83 +1ac808e6 +9a820020 +1a851483 +da8820e6 +da8b3549 +9a9f57ec +1a9fb7ed +da9fa3ee +9a91220f +9a823020 +1a8463e3 +93401c20 +13003c62 +93407ca4 +53001ce6 +53003d28 +d3441d6a +13003dac +d3485dee +9343fe30 +dac01020 +5ac01062 +dac000a4 +dac00ce6 +5ac00928 +f9400020 +f9400462 +f97fffe4 +b97ffcc5 +397ffd07 +79400549 +3980058b +39c001cd +79800e0f +79c00271 +b98006b4 +f85f82f6 +f8403338 +f840837a +385ff020 +b8900062 +f86668a4 +f8697907 +386c696a +786f79cd +b8737a30 +f84107e0 +f85f8c41 +38401483 +b88ffcc5 +fd400420 +bd400062 +3dc004a4 +fc6878e6 +3cdf0149 +f9000020 +b90007e2 +393ffc83 +793ffcc5 +f81f0fe7 +f8008528 +b81fc16a +380ff1ac +f83079ee +f900023f +fd0007e0 +bc1fc041 +3dbffc83 +a8c17bfd +a9bf7bfd +a91fd3f3 +29600440 +a94010a3 +6da027e8 +ad5f8400 +2ca00c22 +f8e00041 +b8e303e4 +f8e580e6 +b8e88149 +c8ebfdac +88eefe0f +c85ffc20 +885ffc62 +c804fcc5 +8807fd28 +d5033bbf +d5033fbf +d5033b9f +d5033fdf +nop * 1 +d503203f +d4001001 +d4000001 +d4200020 +d43e0000 +1e604020 +1e204062 +9e6700a4 +1e2700e6 +9e660128 +1e26016a +1e622820 +1e253883 +1e6808e6 +1e2b1949 +1e6e49ac +1e315a0f +1e61c000 +1e21c041 +1e614083 +1e20c0c5 +1e612000 +1e232040 +1e602088 +1e22c020 +1e624062 +9e620020 +1e220062 +1e6300a4 +9e2300e6 +9e780020 +1e380062 +9e3900a4 +1e7900e6 +b40002e0 +35ffffe1 +361802a2 +37ffffa3 +b6f80264 +54ffff60 +54000221 +54ffff22 +540001e2 +54fffee3 +540001ae +17fffff5 +1400000b +94000000 +14000000 +90000000 +91000000 +90000005 +910000a5 +d61f0020 +d63f0040 +d65f03c0 +d65f0060 +04030201 +nop * 1 +12345678 +ffffffff +d2800000 +d2824681 +d2a24682 +92800003 +92a00024 +b2009fe5 +d29bde06 +f2b35786 +f2cacf06 +f2e24686 +929530e7 +f2c24687 +b20003e8 +b200f3e9 +d2f0000a +92f0000b +b2407fec +d29579ad +f2dfffed +aa0103e0 +910003bf +910003fd +8b020020 +ab050083 +cb0800e6 +eb0b0149 +8a0e01ac +aa11020f +ca150293 +ea1802f6 +8a3b0359 +aa3e03bc +8b020c20 +cb85fc83 +aac804e6 +913ffc20 +91400420 +910043ff +d1448fff +d1002020 +d1404862 +d10d1442 +d288acf0 +f2a02470 +8b3063e4 +d28468b0 +f2a00030 +ab3060c5 +92401c20 +b27c0c3f +d2824690 +ca100062 +eb01001f +f10083ff +b100145f +d2820030 +f2a02010 +eb30607f +ea05009f +f24000df +f24100ff +cb0103e0 +aa2303e2 +9ac22020 +9ac52483 +9ac828e6 +9acb2d49 +1ac52083 +d37df020 +d37ffc20 +9341fc62 +93c51ca4 +530100e6 +13894128 +d340fc20 +d3483c20 +93407c62 +93401c20 +93403c62 +93407ca4 +53001ce6 +53003d28 +2a0b03ea +9b027c20 +9b051883 +9b09a907 +9b427c20 +9bc57c83 +9ac50c83 +9ac808e6 +9a820020 +9a851483 +da88b4e6 +9a9fd7e9 +dac01020 +5ac01062 +dac000a4 +f9400020 +f97ffc20 +d2900010 +f8706862 +f85f80a4 +f85000a4 +f84030e6 +397ffd28 +3980116a +797ffdac +79800dee +b97fffe0 +b9800441 +389ff083 +f9000420 +b90003e2 +78001083 +393ffcc5 +f90000ff +f8627820 +38a56883 +b86878e6 +782b7949 +f82e69ac +f84107e0 +f81f0fe1 +b8100c62 +384ff4a4 +fd400420 +bd400462 +3dc008a4 +3dbfffe6 +fc1fc107 +bd000149 +a9bf7bfd +a8c17bfd +a91fd3f3 +a9605835 +f8e10040 +b8e300a4 +f8e68107 +b8e9816a +c8ecfdcd +88effe30 +d5033bbf +d4001001 +d4000001 +d4200020 +d43e0000 +1e620820 +1e621820 +1e622820 +1e623820 +1e624820 +1e625820 +1e252883 +1e604020 +1e60c020 +1e614020 +1e61c020 +1e21c0e6 +1e22c020 +1e624062 +1e612000 +1e232040 +1e602088 +1e2020a8 +9e620020 +9e220062 +9e6300a4 +9e780020 +9e390062 +9e670020 +1e270062 +9e6600a4 +1e2600e6 +10000000 +10ffffe1 +107fffe2 +90000000 +91000000 +90000010 +f9400201 +90000010 +b9000202 +90000010 +fd400203 +90000010 +bd000204 +90000010 +91000210 +f9400205 +94000000 +14000000 +97fffe68 +d63f00a0 +d61f00c0 +d65f03c0 +54000100 +b40000e3 +b50000c4 +37180045 +14000004 +b6400046 +14000002 +14000001 +aa0103e0 +54ffffe1 +17fffffe +14000003 +54000043 +14000001 +aa0203e1 +nop * 1 +547fca81 +b47fca61 +nop * 261714 +54800000 +reloc 000000000000032c ARM64_RELOC_BRANCH26 _proc1 +reloc 0000000000000330 ARM64_RELOC_BRANCH26 _proc1 +reloc 0000000000000334 ARM64_RELOC_PAGE21 _glob +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 +reloc 0000000000000638 ARM64_RELOC_PAGEOFF12 _glob +reloc 000000000000063c ARM64_RELOC_PAGE21 _glob +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 diff --git a/test/a64/Encode.raz b/test/a64/Encode.raz new file mode 100644 index 0000000..ea2e931 --- /dev/null +++ b/test/a64/Encode.raz @@ -0,0 +1,306 @@ +(* A64's encodings, checked against Apple's assembler: its assembler on + Asm.txt, then every routine of its interface (api), into one + procedure's chunk, which is printed a word to a line in hex (a run of + nop as one line), then the fixups. Api.s holds the same instructions + as api's calls, a line for each, and expect.sh makes Encode.expect + from Asm.txt and it with cc and objdump on an arm64 Mac. The + assembler goes first, at the chunk's start, as a code procedure is: + a tbz's link, the place of the jump before it on its label's chain, + counts from there in 14 bits. The interface's last jumps are the + farthest b.cond and cbz reach, 1 MB. test/check.sh compiles this with + -I src, as A64 isn't in the compiler yet. *) +module Encode; +import Fmt, Diag, Scan, Chunk, A := A64; +import SYSTEM; + +var + body, proc1, glob, byte: INT; (* chunks: the code, and three it names *) + +(* The calls, in Api.s's order *) +proc api; +var L, L2, top: INT; +begin + (* moves *) + A.movRI(0, 0); + A.movRI(1, 1234H); + A.movRI(2, 12340000H); + A.movRI(3, -1); + A.movRI(4, -10001H); + A.movRI(5, 00FF00FF00FF00FFH); + A.movRI(6, 123456789ABCDEF0H); + A.movRI(7, I64(0FFFF1234FFFF5678H)); + A.movRI(8, 100000001H); + A.movRI(9, 5555555555555555H); + A.movRI(10, I64(8000000000000000H)); + A.movRI(11, 7FFFFFFFFFFFFFFFH); + A.movRI(12, 0FFFFFFFFH); + A.movRI(13, 0FFFF0000ABCDH); + A.movRR(0, 1); + A.movRR(A.SP, A.FP); + A.movRR(A.FP, A.SP); + (* two registers *) + A.alu(A.opAdd, 0, 1, 2); + A.alu(A.opAdds, 3, 4, 5); + A.alu(A.opSub, 6, 7, 8); + A.alu(A.opSubs, 9, 10, 11); + A.alu(A.opAnd, 12, 13, 14); + A.alu(A.opOrr, 15, 16, 17); + A.alu(A.opEor, 19, 20, 21); + A.alu(A.opAnds, 22, 23, 24); + A.alu(A.opBic, 25, 26, 27); + A.alu(A.opOrn, 28, 29, 30); + A.aluShift(A.opAdd, 0, 1, 2, A.shLsl, 3); + A.aluShift(A.opSub, 3, 4, 5, A.shAsr, 63); + A.aluShift(A.opOrr, 6, 7, 8, A.shRor, 1); + (* immediates *) + A.aluI(A.opAdd, 0, 1, 4095); + A.aluI(A.opAdd, 0, 1, 4096); + A.aluI(A.opAdd, A.SP, A.SP, 16); + A.aluI(A.opSub, A.SP, A.SP, 123000H); + A.aluI(A.opAdd, 0, 1, -8); + A.aluI(A.opSub, 2, 3, 12345H); + A.aluI(A.opAdd, 4, A.SP, 1234567H); + A.aluI(A.opAdds, 5, 6, 12345H); + A.aluI(A.opAnd, 0, 1, 0FFH); + A.aluI(A.opOrr, A.SP, 1, 0F0H); + A.aluI(A.opEor, 2, 3, 1234H); + A.cmp(0, 1); + A.cmpI(A.SP, 32); + A.cmpI(2, -5); + A.cmpI(3, 1001001H); + A.alu(A.opAnds, A.ZR, 4, 5); + A.aluI(A.opAnds, A.ZR, 6, 1); + A.aluI(A.opAnds, A.ZR, 7, I64(8000000000000000H)); + A.neg(0, 1); + A.mvn(2, 3); + (* shifts and fields *) + A.shift(A.shLsl, 0, 1, 2, 8); + A.shift(A.shLsr, 3, 4, 5, 8); + A.shift(A.shAsr, 6, 7, 8, 8); + A.shift(A.shRor, 9, 10, 11, 8); + A.shift(A.shLsl, 3, 4, 5, 4); + A.shiftI(A.shLsl, 0, 1, 3, 8); + A.shiftI(A.shLsr, 0, 1, 63, 8); + A.shiftI(A.shAsr, 2, 3, 1, 8); + A.shiftI(A.shRor, 4, 5, 7, 8); + A.shiftI(A.shLsl, 6, 7, 31, 4); + A.shiftI(A.shRor, 8, 9, 16, 4); + A.shiftI(A.shLsl, 0, 1, 0, 8); + A.field(false, 0, 1, 8, 8); + A.field(true, 2, 3, 0, 32); + A.extend(0, 1, 1, true); + A.extend(2, 3, 2, true); + A.extend(4, 5, 4, true); + A.extend(6, 7, 1, false); + A.extend(8, 9, 2, false); + A.extend(10, 11, 4, false); + (* multiply, divide, select, count *) + A.mul(0, 1, 2); + A.madd(3, 4, 5, 6, false); + A.madd(7, 8, 9, 10, true); + A.mulHigh(true, 0, 1, 2); + A.mulHigh(false, 3, 4, 5); + A.divide(true, 3, 4, 5); + A.divide(false, 6, 7, 8); + A.csel(A.csSel, A.ccEQ, 0, 1, 2); + A.csel(A.csInc, A.ccNE, 3, 4, 5); + A.csel(A.csNeg, A.ccLT, 6, 7, 8); + A.cset(A.ccGT, 9); + A.clz(0, 1, 8); + A.clz(2, 3, 4); + A.rbit(4, 5, 8); + (* loads and stores *) + A.load(0, 1, 0, 8, false); + A.load(0, 1, 32760, 8, false); + A.load(2, 3, 32768, 8, false); + A.load(4, 5, -8, 8, false); + A.load(4, 5, -256, 8, false); + A.load(6, 7, 3, 8, false); + A.load(8, 9, 4095, 1, false); + A.load(10, 11, 4, 1, true); + A.load(12, 13, 8190, 2, false); + A.load(14, 15, 6, 2, true); + A.load(0, A.SP, 16380, 4, false); + A.load(1, 2, 4, 4, true); + A.load(3, 4, -1, 1, true); + A.store(0, 1, 8, 8); + A.store(2, A.SP, 0, 4); + A.store(3, 4, 1, 2); + A.store(5, 6, 4095, 1); + A.store(A.ZR, 7, 0, 8); + A.loadR(0, 1, 2, 8, false, true); + A.loadR(3, 4, 5, 1, true, false); + A.loadR(6, 7, 8, 4, false, true); + A.storeR(9, 10, 11, 2, true); + A.storeR(12, 13, 14, 8, false); + A.loadIdx(0, A.SP, 16, 8, A.mPost); + A.storeIdx(1, A.SP, -16, 8, A.mPre); + A.storeIdx(2, 3, -256, 4, A.mPre); + A.loadIdx(4, 5, 255, 1, A.mPost); + A.loadF(0, 1, 8, 8); + A.loadF(2, 3, 4, 4); + A.loadF(4, 5, 32, 16); + A.storeF(6, A.SP, 65520, 16); + A.storeF(7, 8, -4, 8); + A.storeF(9, 10, 0, 4); + A.stp(A.FP, A.LR, A.SP, -16, A.mPre); + A.ldp(A.FP, A.LR, A.SP, 16, A.mPost); + A.stp(19, 20, A.SP, 504, A.mOff); + A.ldp(21, 22, 1, -512, A.mOff); + (* atomics and the system's *) + A.ldaddal(1, 0, 2, 8); + A.ldaddal(3, 4, 5, 4); + A.swpal(6, 7, 8, 8); + A.swpal(9, 10, 11, 4); + A.casal(12, 13, 14, 8); + A.casal(15, 16, 17, 4); + A.dmb(); + A.svc(80H); + A.svc(0); + A.brk(1); + A.brk(0F000H); + (* reals *) + A.fop(A.fMul, 0, 1, 2, false); + A.fop(A.fDiv, 0, 1, 2, false); + A.fop(A.fAdd, 0, 1, 2, false); + A.fop(A.fSub, 0, 1, 2, false); + A.fop(A.fMax, 0, 1, 2, false); + A.fop(A.fMin, 0, 1, 2, false); + A.fop(A.fAdd, 3, 4, 5, true); + A.fop1(A.fMov, 0, 1, false); + A.fop1(A.fAbs, 0, 1, false); + A.fop1(A.fNeg, 0, 1, false); + A.fop1(A.fSqrt, 0, 1, false); + A.fop1(A.fSqrt, 6, 7, true); + A.fcvt(0, 1, false); + A.fcvt(2, 3, true); + A.fcmp(0, 1, false); + A.fcmp(2, 3, true); + A.fcmp(4, -1, false); + A.fcmp(5, -1, true); + A.scvtf(0, 1, false, true); + A.scvtf(2, 3, true, true); + A.scvtf(4, 5, false, false); + A.fcvtz(0, 1, false, true); + A.fcvtz(2, 3, true, false); + A.fmovFR(0, 1, false); + A.fmovFR(2, 3, true); + A.fmovRF(4, 5, false); + A.fmovRF(6, 7, true); + (* addresses, calls *) + A.adr(0, 0); + A.adr(1, -4); + A.adr(2, 1048572); + A.addrChunk(0, glob, 0); + A.loadChunk(1, glob, 0, 8, false); + A.storeChunk(2, glob, 0, 4); + A.loadChunkF(3, glob, 0, 8); + A.storeChunkF(4, glob, 0, 4); + A.loadChunk(5, byte, 0, 8, false); + A.callChunk(proc1); + A.jmpChunk(proc1); + A.callBack(0); + A.callR(5); + A.jmpR(6); + A.ret(); + (* jumps within the procedure: forward on a chain, back *) + L := A.NoLabel; + A.jcc(A.ccEQ, L); + A.cbz(false, 3, L); + A.cbz(true, 4, L); + A.tbz(false, 5, 3, L); + A.tbz(true, 6, 40, L); + A.jmp(L); + A.fixLink(L); + top := Chunk.pc; + A.movRR(0, 1); + A.jccBack(A.ccNE, top); + A.jmpBack(top); + L := A.NoLabel; L2 := A.NoLabel; + A.jmp(L); + A.jcc(A.ccLO, L2); + A.jmp(L); + A.fixLink(A.joinLinks(L, L2)); + L := A.NoLabel; + A.movRR(1, 2); + A.jmp(L); + A.dropJumpTo(L); + A.fixLink(L); + A.align(16); + (* the farthest: b.cond and cbz to 1 MB on, and b.cond 1 MB back *) + L := A.NoLabel; + A.jcc(A.ccNE, L); + A.cbz(false, 1, L); + A.align(100000H); + A.fixLink(L); + A.jccBack(A.ccEQ, 0); +end; + +(* The test's reader of a constant: a number, perhaps negative *) +proc konst(_whole: BOOL): (I64, ERROR); +var neg: BOOL; v: I64; +begin + neg := Scan.tok = Scan.sMinus; + if neg then Scan.next; end; + if Scan.tok # Scan.sInt then return 0, Scan.mark("expected a number"); end; + v := Scan.ival; + Scan.next; + if neg then return -v, nil; end; + return v, nil; +end; + +(* and of a name: proc1 and glob *) +proc name: (INT, ERROR); +begin + if Scan.nameOf(Scan.id) = "proc1" then Scan.next; return proc1, nil; end; + if Scan.nameOf(Scan.id) = "glob" then Scan.next; return glob, nil; end; + return -1, nil; +end; + +proc chunk(n: []const U8; sec: Chunk.Section): INT; +begin + return Chunk.newChunk(sec, -1, Scan.intern(SYSTEM.PTR(n), LEN(n))); +end; + +const + Kinds: [4][]const U8 = ["", "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21", "ARM64_RELOC_PAGEOFF12"]; + Nop = 0D503201FH; + +var + err: ERROR; + i, n, w: INT; + why: []const U8; + +begin + body := chunk("code", Chunk.secText); + proc1 := chunk("proc1", Chunk.secText); + glob := chunk("glob", Chunk.secBss); + Chunk.setBss(glob, 8, 8); + byte := chunk("byte", Chunk.secBss); + Chunk.setBss(byte, 8, 1); + Chunk.beginChunk(body); + why := Scan.open("test/a64/Asm.txt"); + if LEN(why) > 0 then Fmt.print("test/a64/Asm.txt: {}\n", [Fmt.str(why)]); end; + Scan.next; + err := A.assemble(konst, name); + if err # nil then Fmt.print("the assembler failed\n", []); end; + api(); + Chunk.endChunk(body); + i := 0; + while i < Chunk.pc do + w := Chunk.get4At(i) & 0FFFFFFFFH; + n := 0; + while (i < Chunk.pc) and (w = Nop) and ((Chunk.get4At(i) & 0FFFFFFFFH) = Nop) do INC(n); INC(i, 4); end; + if n > 0 then + Fmt.print("nop * {}\n", [Fmt.int(n)]); + else + Fmt.print("{08}\n", [Fmt.lowHex(U64(w))]); + INC(i, 4); + end; + end; + for i := 0 to Chunk.numFixups - 1 do + Fmt.print("reloc {016} {} _{}\n", [Fmt.lowHex(U64(Chunk.fixups[i].at)), + Fmt.str(Kinds[INT(Chunk.fixups[i].kind)]), Fmt.str(Scan.nameOf(Chunk.chunks[Chunk.fixups[i].chunk].name))]); + end; + Diag.finish; +end Encode. diff --git a/test/a64/expect.sh b/test/a64/expect.sh new file mode 100644 index 0000000..5ed33bd --- /dev/null +++ b/test/a64/expect.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# Makes test/a64/Encode.expect, what Encode.raz must print, from Apple's +# assembler: Asm.txt, A64's assembler's part, turned into Apple's syntax +# (raz's comments dropped, 10H as 0x10, `adr x0, v` as the adrp and add +# it stands for, a name as its symbol, _v, db and dd as .byte and .long, +# and ; between instructions as a new line, since ; begins a comment +# there), then Api.s, the instructions of Encode.raz's calls of A64. +# Each word of the object's code, a run of nop as one line, then its +# relocations, sorted, as Encode.raz prints its fixups. +# Run on an arm64 Mac, from the repository's root: sh test/a64/expect.sh +set -e +d=test/a64 +t=$(mktemp -d) +trap 'rm -rf "$t"' EXIT +{ + echo Lstart: + sed -E -e '/^\(\*/,/\*\)/d' -e '/^end$/d' \ + -e 's/([0-9][0-9A-F]*)H/0x\1/g' \ + -e 's/adr (x[0-9]+), (glob|proc1)/adrp \1, _\2@PAGE; add \1, \1, _\2@PAGEOFF/g' \ + -e 's/(b|bl) proc1/\1 _proc1/g' \ + -e 's/(fcmp [ds][0-9]+, #0);/\1.0;/g' \ + -e 's/^db /.byte /; s/; dd /; .long /' $d/Asm.txt + cat $d/Api.s +} | tr ';' '\n' > $t/all.s +cc -c -arch arm64 $t/all.s -o $t/all.o +objdump -d $t/all.o | awk ' + /^ *[0-9a-f]+:/ { + if ($2 == "d503201f") { nops++; next } + if (nops) { print "nop * " nops; nops = 0 } + print $2 + } + END { if (nops) print "nop * " nops }' > $d/Encode.expect +objdump -r $t/all.o | awk '/ARM64_RELOC/ { print "reloc", $1, $2, $3 }' | sort >> $d/Encode.expect diff --git a/test/check.sh b/test/check.sh index 5fe86a7..c55ae7b 100755 --- a/test/check.sh +++ b/test/check.sh @@ -16,7 +16,8 @@ # the compiler's own version in a library's note written as N); there # X.args holds the compiler's options. The examples, test/libc, the # tools and the library's tests are compiled with -Werror, so warnings -# fail them. Then the version, tools/Doc.raz, the report's examples, +# fail them. test/a64/Encode.raz checks arm64's encoder, src/A64.raz, +# against Apple's assembler. Then the version, tools/Doc.raz, the report's examples, # the tutorial's, tools/Pretty.raz, and the library's own tests. raz=./raz @@ -101,6 +102,17 @@ libc=' -libc musl' for f in test/libc/*.raz; do run test/libc $(basename $f .raz); done libc= +fi + +# A64, arm64's encoder, which no compiler for this target imports: its +# test is compiled with src/ for its modules, and prints what Apple's +# assembler makes of the same instructions (test/a64/expect.sh) +if $raz -Werror -I src -I build/src -o $out/Encode test/a64/Encode.raz 2>$out/Encode.cerr && + $out/Encode > $out/Encode.out 2>&1 && cmp -s $out/Encode.out test/a64/Encode.expect; then + pass=$((pass + 1)) +else + echo "FAIL test/a64/Encode"; cat $out/Encode.cerr; diff $out/Encode.out test/a64/Encode.expect | head -10 + fail=$((fail + 1)) fi # raz run: a static program and one over C, from memory, with their