| | @@ -0,0 +1,1848 @@ |
| 1 | +(* A64: machine code for arm64, X64's twin. |
| 2 | + |
| 3 | + The encoder writes arm64's instructions into the text of Chunk, each |
| 4 | + one word of 32 bits, least significant byte first. Registers are |
| 5 | + numbered as the hardware numbers them: x0 to x30, and 31, which is sp |
| 6 | + in some places and the zero register, xzr, in others, as each |
| 7 | + instruction says (SP and ZR below are both 31; a routine's comment |
| 8 | + says which it means). The reals' registers, v0 to v31, have numbers |
| 9 | + of their own, 0 to 31, and a size: s for an F32, d for an F64, q for |
| 10 | + 16 bytes. Jumps within a procedure are patched here, as soon as their |
| 11 | + target is known; calls and addresses of other chunks are fixups, |
| 12 | + patched by Link. |
| 13 | + |
| 14 | + Apple's arm64 convention is the target's: x18 is the system's and |
| 15 | + never touched, x29 is the frame pointer, x30 the link register, sp |
| 16 | + is kept a multiple of 16. x16 (IP0) is this encoder's own scratch |
| 17 | + register: a routine given an immediate or an offset too big for its |
| 18 | + instruction builds it in x16 first, so Gen keeps nothing there. |
| 19 | + |
| 20 | + Each encoding's comment gives its fields, most significant first, as |
| 21 | + Arm's reference manual draws them: `sf 0 0 100010 sh imm12 Rn Rd` is |
| 22 | + add (immediate), sf the bit for 64 bits, then constant bits, then the |
| 23 | + fields, Rd in the low five bits. Every encoding was checked against |
| 24 | + Apple's assembler; test/a64 checks them all again. |
| 25 | + |
| 26 | + Depends on: Scan, for errors and the assembler's symbols; Diag, for |
| 27 | + positions and text; Chunk, for the code buffer and fixups. |
| 28 | + Leaves out: choosing instructions; that is Gen (Gen.arm64.raz, to |
| 29 | + come). Only what Gen and the library's code procedures need is here; |
| 30 | + `db` and `dd` give any other instruction. *) |
| 31 | +module A64; |
| 32 | +import Out, Fmt, Str, Diag, Scan, Chunk; |
| 33 | +import SYSTEM; |
| 34 | + |
| 35 | +const |
| 36 | + FP* = 29; LR* = 30; |
| 37 | + SP* = 31; ZR* = 31; (* the same number: the instruction tells which *) |
| 38 | + X16* = 16; X17* = 17; (* IP0 and IP1, the scratch registers; X16 is the encoder's *) |
| 39 | + |
| 40 | + (* condition codes, as b.cond and csel take them; cc xor 1 is the |
| 41 | + opposite of cc (invert) *) |
| 42 | + ccEQ* = 0; ccNE* = 1; ccHS* = 2; ccLO* = 3; ccMI* = 4; ccPL* = 5; ccVS* = 6; ccVC* = 7; |
| 43 | + ccHI* = 8; ccLS* = 9; ccGE* = 10; ccLT* = 11; ccGT* = 12; ccLE* = 13; ccAL* = 14; |
| 44 | + |
| 45 | + (* the operations of two registers, alu's op: AluBits gives each its bits *) |
| 46 | + opAdd* = 0; opAdds* = 1; opSub* = 2; opSubs* = 3; |
| 47 | + opAnd* = 4; opOrr* = 5; opEor* = 6; opAnds* = 7; opBic* = 8; opOrn* = 9; |
| 48 | + |
| 49 | + (* shifts: of a register operand, and lsl, lsr, asr and ror themselves *) |
| 50 | + shLsl* = 0; shLsr* = 1; shAsr* = 2; shRor* = 3; |
| 51 | + |
| 52 | + (* csel's op: rd := rn if the condition holds, else rm, rm + 1, ~rm or -rm *) |
| 53 | + csSel* = 0; csInc* = 1; csInv* = 2; csNeg* = 3; |
| 54 | + |
| 55 | + (* the addressing of ldp, stp and the indexed loads, bits 23 and 24 of |
| 56 | + a pair: [rn, #off]; [rn, #off]!, rn changed first; [rn], #off, after *) |
| 57 | + mPost* = 1; mOff* = 2; mPre* = 3; |
| 58 | + |
| 59 | + (* reals: fop's op (the opcode field of two sources) and fop1's (of one) *) |
| 60 | + fMul* = 0; fDiv* = 1; fAdd* = 2; fSub* = 3; fMax* = 4; fMin* = 5; |
| 61 | + fMov* = 0; fAbs* = 1; fNeg* = 2; fSqrt* = 3; |
| 62 | + |
| 63 | + NoLabel* = -1; |
| 64 | + |
| 65 | + AluBits: [10]INT = [0B000000H, 2B000000H, 4B000000H, 6B000000H, |
| 66 | + 0A000000H, 2A000000H, 4A000000H, 6A000000H, 0A200000H, 2A200000H]; |
| 67 | + AluNames: [10][]const U8 = ["add", "adds", "sub", "subs", "and", "orr", "eor", "ands", "bic", "orn"]; |
| 68 | + ShiftNames: [4][]const U8 = ["lsl", "lsr", "asr", "ror"]; |
| 69 | + CselBits: [4]INT = [1A800000H, 1A800400H, 5A800000H, 5A800400H]; |
| 70 | + CselNames: [4][]const U8 = ["csel", "csinc", "csinv", "csneg"]; |
| 71 | + CondNames*: [16][]const U8 = ["eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc", |
| 72 | + "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"]; |
| 73 | + FopNames: [6][]const U8 = ["fmul", "fdiv", "fadd", "fsub", "fmax", "fmin"]; |
| 74 | + Fop1Names: [4][]const U8 = ["fmov", "fabs", "fneg", "fsqrt"]; |
| 75 | + Nop = 0D503201FH; |
| 76 | + |
| 77 | +type |
| 78 | + (* The parser's reader of an integer constant in a code procedure: a |
| 79 | + whole expression, or, when whole is false, a single factor *) |
| 80 | + ConstFn* = proc(whole: BOOL): (I64, ERROR); |
| 81 | + |
| 82 | + (* The parser's reader of a name that stands for an address: a |
| 83 | + procedure or a module variable. Its chunk, the name read; or -1, |
| 84 | + nothing read, if the name is anything else *) |
| 85 | + NameFn* = proc: (INT, ERROR); |
| 86 | + |
| 87 | + (* The text of the instruction that starts at at, for the listing *) |
| 88 | + Said = record |
| 89 | + at: INT; |
| 90 | + text: []const U8; |
| 91 | + end; |
| 92 | + |
| 93 | +var |
| 94 | + listing*: BOOL; (* -S: each instruction's text is kept, for list *) |
| 95 | + said: ^Said; |
| 96 | + numSaid, saidCap: INT; |
| 97 | + labelAt: INT; (* where a jump was last fixed to the place it was at *) |
| 98 | + |
| 99 | +(* The listing, as X64's *) |
| 100 | + |
| 101 | +proc setListing*(yes: BOOL); |
| 102 | +begin |
| 103 | + listing := yes; |
| 104 | +end; |
| 105 | + |
| 106 | +proc saidCount*: INT; |
| 107 | +begin |
| 108 | + return numSaid; |
| 109 | +end; |
| 110 | + |
| 111 | +proc unsay*(n: INT); |
| 112 | +begin |
| 113 | + numSaid := n; |
| 114 | +end; |
| 115 | + |
| 116 | +proc sayAt*(at: INT; s: []const U8); |
| 117 | +begin |
| 118 | + if not listing then return; end; |
| 119 | + if numSaid = saidCap then |
| 120 | + said := ^Said(Scan.regrab(^U8(said), numSaid * SIZE(Said), (saidCap * 2 + 1024) * SIZE(Said))); |
| 121 | + saidCap := saidCap * 2 + 1024; |
| 122 | + end; |
| 123 | + said[numSaid].at := at; |
| 124 | + said[numSaid].text := s; |
| 125 | + INC(numSaid); |
| 126 | +end; |
| 127 | + |
| 128 | +proc say*(s: []const U8); |
| 129 | +begin |
| 130 | + sayAt(Chunk.pc, s); |
| 131 | +end; |
| 132 | + |
| 133 | +proc restate*(at: INT; s: []const U8); |
| 134 | +var i: INT; |
| 135 | +begin |
| 136 | + i := numSaid - 1; |
| 137 | + while (i >= 0) and (said[i].at # at) do DEC(i); end; |
| 138 | + if i >= 0 then said[i].text := s; end; |
| 139 | +end; |
| 140 | + |
| 141 | +(* "op a, b, c": an instruction's text, from its operands' *) |
| 142 | +proc sayOps(op: []const U8; ops: []const []const U8); |
| 143 | +var s: []const U8; i: INT; |
| 144 | +begin |
| 145 | + s := op; |
| 146 | + for i := 0 to LEN(ops) - 1 do |
| 147 | + if i = 0 then s := Diag.say("{} {}", [Fmt.str(s), Fmt.str(ops[i])]); |
| 148 | + else s := Diag.say("{}, {}", [Fmt.str(s), Fmt.str(ops[i])]); end; |
| 149 | + end; |
| 150 | + say(s); |
| 151 | +end; |
| 152 | + |
| 153 | +(* 1 for true *) |
| 154 | +proc bit(f: BOOL): INT; |
| 155 | +begin |
| 156 | + if f then return 1; end; |
| 157 | + return 0; |
| 158 | +end; |
| 159 | + |
| 160 | +(* log2 of a size of 1, 2, 4, 8 or 16 bytes *) |
| 161 | +proc scaleOf(size: INT): INT; |
| 162 | +var s: INT; |
| 163 | +begin |
| 164 | + s := 0; |
| 165 | + while (1 shl s) < size do INC(s); end; |
| 166 | + return s; |
| 167 | +end; |
| 168 | + |
| 169 | +(* Register r's name: x or w by sf, 31 sp if sp, else the zero register *) |
| 170 | +proc reg(r, sf: INT; sp: BOOL): []const U8; |
| 171 | +const Pfx: [2][]const U8 = ["w", "x"]; |
| 172 | +begin |
| 173 | + if (r = 31) and sp and (sf = 1) then return "sp"; end; |
| 174 | + if (r = 31) and sp then return "wsp"; end; |
| 175 | + if r = 31 then return Diag.say("{}zr", [Fmt.str(Pfx[sf])]); end; |
| 176 | + return Diag.say("{}{}", [Fmt.str(Pfx[sf]), Fmt.int(r)]); |
| 177 | +end; |
| 178 | + |
| 179 | +proc x(r: INT): []const U8; |
| 180 | +begin |
| 181 | + return reg(r, 1, false); |
| 182 | +end; |
| 183 | + |
| 184 | +proc xsp(r: INT): []const U8; |
| 185 | +begin |
| 186 | + return reg(r, 1, true); |
| 187 | +end; |
| 188 | + |
| 189 | +(* A real's register, by its size: s, d or q *) |
| 190 | +proc vreg(v, size: INT): []const U8; |
| 191 | +const Pfx: [5][]const U8 = ["b", "h", "s", "d", "q"]; |
| 192 | +begin |
| 193 | + return Diag.say("{}{}", [Fmt.str(Pfx[scaleOf(size)]), Fmt.int(v)]); |
| 194 | +end; |
| 195 | + |
| 196 | +proc imm(v: I64): []const U8; |
| 197 | +begin |
| 198 | + return Diag.say("#{}", [Fmt.int(v)]); |
| 199 | +end; |
| 200 | + |
| 201 | +(* [rn, #off], or [rn, #off]! and [rn], #off by mode *) |
| 202 | +proc memText(rn, off, mode: INT): []const U8; |
| 203 | +begin |
| 204 | + if mode = mPre then return Diag.say("[{}, #{}]!", [Fmt.str(xsp(rn)), Fmt.int(off)]); end; |
| 205 | + if mode = mPost then return Diag.say("[{}], #{}", [Fmt.str(xsp(rn)), Fmt.int(off)]); end; |
| 206 | + return Diag.say("[{}, #{}]", [Fmt.str(xsp(rn)), Fmt.int(off)]); |
| 207 | +end; |
| 208 | + |
| 209 | +proc emit(insn: INT); |
| 210 | +begin |
| 211 | + Chunk.put4(insn); |
| 212 | +end; |
| 213 | + |
| 214 | +proc invert*(cc: INT): INT; |
| 215 | +begin |
| 216 | + return cc xor 1; |
| 217 | +end; |
| 218 | + |
| 219 | +(* Immediates and moves *) |
| 220 | + |
| 221 | +(* v as a logical instruction's immediate, its fields N:immr:imms, or -1 |
| 222 | + if it can't be one. Such an immediate is an element of 2, 4, 8, 16, 32 |
| 223 | + or 64 bits repeated across the register, the element a run of ones |
| 224 | + rotated: N says 64 bits, imms holds the element's size and the run's |
| 225 | + length less one (imms 0xxxxx for 32 bits, 10xxxx for 16, ... 11110x |
| 226 | + for 2), and immr the rotation right. Of 32 bits (sf 0) the low half is |
| 227 | + taken, and seen as repeated. *) |
| 228 | +proc bitmask*(v: I64; sf: INT): INT; |
| 229 | +var u, elem, pat, m, rot: U64; e, ones, r: INT; |
| 230 | +begin |
| 231 | + u := U64(v); |
| 232 | + if sf = 0 then u := (u & 0FFFFFFFFH) | ((u & 0FFFFFFFFH) shl 32); end; |
| 233 | + if (u = 0) or (u = MAX(U64)) then return -1; end; |
| 234 | + (* the smallest element: u is made of halves alike while turning it |
| 235 | + round by half its element changes nothing *) |
| 236 | + e := 64; |
| 237 | + while (e > 2) and (ROR(u, e div 2) = u) do e := e div 2; end; |
| 238 | + if e = 64 then m := MAX(U64); else m := (U64(1) shl e) - 1; end; |
| 239 | + elem := u & m; |
| 240 | + ones := POPCOUNT(elem); |
| 241 | + pat := (U64(1) shl ones) - 1; |
| 242 | + for r := 0 to e - 1 do |
| 243 | + rot := ((pat shr r) | (pat shl ((e - r) & 63))) & m; |
| 244 | + if rot = elem then |
| 245 | + return (bit(e = 64) shl 12) | (r shl 6) | ((~(e * 2 - 1)) & 3FH) | (ones - 1); |
| 246 | + end; |
| 247 | + end; |
| 248 | + return -1; |
| 249 | +end; |
| 250 | + |
| 251 | +(* movn, movz or movk (opc 0, 2, 3): sf opc 100101 hw imm16 Rd, imm16 |
| 252 | + shifted left by 16 * hw; movn writes its inverse, movk keeps the rest *) |
| 253 | +proc movWide(sf, opc, rd, imm16, hw: INT); |
| 254 | +const Names: [4][]const U8 = ["movn", "", "movz", "movk"]; |
| 255 | +begin |
| 256 | + if listing then sayOps(Names[opc], [reg(rd, sf, false), imm(imm16 & 0FFFFH), Diag.say("lsl #{}", [Fmt.int(hw * 16)])]); end; |
| 257 | + emit((sf shl 31) | (opc shl 29) | 12800000H | (hw shl 21) | ((imm16 & 0FFFFH) shl 5) | rd); |
| 258 | +end; |
| 259 | + |
| 260 | +(* A logical instruction with an immediate, enc as bitmask gives it: |
| 261 | + sf opc 100100 N immr imms Rn Rd, opc 00 and, 01 orr, 10 eor, 11 ands. |
| 262 | + Rd is sp (but for ands), Rn the zero register. *) |
| 263 | +proc logicImm(sf, op, rd, rn, enc: INT; v: I64); |
| 264 | +begin |
| 265 | + if listing then sayOps(AluNames[op], [reg(rd, sf, op # opAnds), reg(rn, sf, false), imm(v)]); end; |
| 266 | + emit((sf shl 31) | (AluBits[op] + 08000000H) | (enc shl 10) | (rn shl 5) | rd); |
| 267 | +end; |
| 268 | + |
| 269 | +(* rd := v, of 64 bits (sf 1) or 32, in as few instructions as it |
| 270 | + takes: movz #0 or movn #0 for all zeros or all ones; a single movz |
| 271 | + when v is one halfword of 16 bits and zeros, a movn when it is one |
| 272 | + halfword and ones, an orr of the zero register when it is a logical |
| 273 | + immediate; else a movz (or movn, if more of its halfwords are ones) |
| 274 | + of the first halfword that isn't all zeros (ones), and a movk for |
| 275 | + each other such halfword. Apple's assembler chooses the same. *) |
| 276 | +proc movImm(sf, rd: INT; v: I64); |
| 277 | +var n, zeros, ones, i, h, first, enc: INT; |
| 278 | +begin |
| 279 | + n := 2 + 2 * sf; |
| 280 | + if sf = 0 then v := v & 0FFFFFFFFH; end; |
| 281 | + zeros := 0; ones := 0; |
| 282 | + for i := 0 to n - 1 do |
| 283 | + h := INT((v shr (16 * i)) & 0FFFFH); |
| 284 | + if h = 0 then INC(zeros); elsif h = 0FFFFH then INC(ones); end; |
| 285 | + end; |
| 286 | + if (zeros = n) or (ones = n) then |
| 287 | + movWide(sf, 2 * bit(zeros = n), rd, 0, 0); |
| 288 | + return; |
| 289 | + end; |
| 290 | + enc := bitmask(v, sf); |
| 291 | + if (zeros < n - 1) and (ones < n - 1) and (enc >= 0) then |
| 292 | + logicImm(sf, opOrr, rd, ZR, enc, v); |
| 293 | + return; |
| 294 | + end; |
| 295 | + first := -1; |
| 296 | + for i := 0 to n - 1 do |
| 297 | + h := INT((v shr (16 * i)) & 0FFFFH); |
| 298 | + if (zeros >= ones) and (h # 0) or (zeros < ones) and (h # 0FFFFH) then |
| 299 | + if first >= 0 then movWide(sf, 3, rd, h, i); |
| 300 | + elsif zeros >= ones then movWide(sf, 2, rd, h, i); |
| 301 | + else movWide(sf, 0, rd, ~h, i); end; |
| 302 | + first := i; |
| 303 | + end; |
| 304 | + end; |
| 305 | +end; |
| 306 | + |
| 307 | +proc movRI*(rd: INT; v: I64); |
| 308 | +begin |
| 309 | + movImm(1, rd, v); |
| 310 | +end; |
| 311 | + |
| 312 | +(* Add, subtract and the logical operations *) |
| 313 | + |
| 314 | +(* add, adds, sub or subs with an immediate of 12 bits, shifted left by |
| 315 | + 12 if sh: sf op S 100010 sh imm12 Rn Rd. Rn is sp; so is Rd, but for |
| 316 | + adds and subs, whose Rd 31 is the zero register (cmp, cmn). *) |
| 317 | +proc addImm(sf, op, rd, rn, imm12, sh: INT); |
| 318 | +begin |
| 319 | + 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; |
| 320 | + emit((sf shl 31) | (AluBits[op] + 06000000H) | (sh shl 22) | (imm12 shl 10) | (rn shl 5) | rd); |
| 321 | +end; |
| 322 | + |
| 323 | +(* op of two registers, rm shifted by n: sf op S 01011 shift 0 Rm imm6 Rn |
| 324 | + Rd for add and sub, sf opc 01010 shift N Rm imm6 Rn Rd for the logical |
| 325 | + ones (N 1 for bic and orn, which invert rm). 31 is the zero register. *) |
| 326 | +proc aluShifted(sf, op, rd, rn, rm, sh, n: INT); |
| 327 | +begin |
| 328 | + 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; |
| 329 | + emit((sf shl 31) | AluBits[op] | (sh shl 22) | (rm shl 16) | (n shl 10) | (rn shl 5) | rd); |
| 330 | +end; |
| 331 | + |
| 332 | +(* add or sub of a register extended, here only uxtx (uxtw for 32 |
| 333 | + bits), shifted left by n up to 4: sf op S 01011 00 1 Rm option imm3 |
| 334 | + Rn Rd. The form that takes sp, as Rn, and as Rd but for adds and subs *) |
| 335 | +proc addExt(sf, op, rd, rn, rm, n: INT); |
| 336 | +begin |
| 337 | + 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; |
| 338 | + emit((sf shl 31) | AluBits[op] | 200000H | (rm shl 16) | ((2 + sf) shl 13) | (n shl 10) | (rn shl 5) | rd); |
| 339 | +end; |
| 340 | + |
| 341 | +(* rd := rn op rm, of 64 bits; 31 is the zero register, so tst a, b is |
| 342 | + alu(opAnds, ZR, a, b) *) |
| 343 | +proc alu*(op, rd, rn, rm: INT); |
| 344 | +begin |
| 345 | + aluShifted(1, op, rd, rn, rm, shLsl, 0); |
| 346 | +end; |
| 347 | + |
| 348 | +(* rd := rn op (rm shifted by n), as an element's address, rn + rm lsl 3 *) |
| 349 | +proc aluShift*(op, rd, rn, rm, sh, n: INT); |
| 350 | +begin |
| 351 | + aluShifted(1, op, rd, rn, rm, sh, n); |
| 352 | +end; |
| 353 | + |
| 354 | +(* rd := rn op v, of 64 bits. For add and sub, rn and rd may be sp: |
| 355 | + v in 12 bits, or 12 bits shifted by 12, is one instruction, a |
| 356 | + negative v the opposite one; below 2^24, without flags, two; else v |
| 357 | + is built in x16 and added as a register. For the logical operations, |
| 358 | + v as an immediate if it can be one, else built in x16. *) |
| 359 | +proc aluI*(op, rd, rn: INT; v: I64); |
| 360 | +var enc: INT; |
| 361 | +begin |
| 362 | + if op <= opSubs then |
| 363 | + if (v < 0) and (v > MIN(I64)) then v := -v; op := op xor 2; end; |
| 364 | + if (v >= 0) and (v < 1000H) then |
| 365 | + addImm(1, op, rd, rn, INT(v), 0); |
| 366 | + elsif (v > 0) and (v < 1000000H) and ((v & 0FFFH) = 0) then |
| 367 | + addImm(1, op, rd, rn, INT(v shr 12), 1); |
| 368 | + elsif (v > 0) and (v < 1000000H) and ((op & 1) = 0) then |
| 369 | + addImm(1, op, rd, rn, INT(v shr 12), 1); |
| 370 | + addImm(1, op, rd, rd, INT(v & 0FFFH), 0); |
| 371 | + else |
| 372 | + movRI(X16, v); |
| 373 | + addExt(1, op, rd, rn, X16, 0); |
| 374 | + end; |
| 375 | + return; |
| 376 | + end; |
| 377 | + enc := bitmask(v, 1); |
| 378 | + if (enc >= 0) and (op <= opAnds) then |
| 379 | + logicImm(1, op, rd, rn, enc, v); |
| 380 | + else |
| 381 | + movRI(X16, v); |
| 382 | + aluShifted(1, op, rd, rn, X16, shLsl, 0); |
| 383 | + end; |
| 384 | +end; |
| 385 | + |
| 386 | +(* rd := rn, by orr of the zero register; by add of 0 when either is sp *) |
| 387 | +proc movRR*(rd, rn: INT); |
| 388 | +begin |
| 389 | + if (rd = SP) or (rn = SP) then addImm(1, opAdd, rd, rn, 0, 0); else aluShifted(1, opOrr, rd, ZR, rn, shLsl, 0); end; |
| 390 | +end; |
| 391 | + |
| 392 | +(* the flags of rn - rm; and of rn - v, where rn 31 is sp *) |
| 393 | +proc cmp*(rn, rm: INT); |
| 394 | +begin |
| 395 | + aluShifted(1, opSubs, ZR, rn, rm, shLsl, 0); |
| 396 | +end; |
| 397 | + |
| 398 | +proc cmpI*(rn: INT; v: I64); |
| 399 | +begin |
| 400 | + aluI(opSubs, ZR, rn, v); |
| 401 | +end; |
| 402 | + |
| 403 | +(* neg: sub from the zero register; mvn: orn of it *) |
| 404 | +proc neg*(rd, rm: INT); |
| 405 | +begin |
| 406 | + aluShifted(1, opSub, rd, ZR, rm, shLsl, 0); |
| 407 | +end; |
| 408 | + |
| 409 | +proc mvn*(rd, rm: INT); |
| 410 | +begin |
| 411 | + aluShifted(1, opOrn, rd, ZR, rm, shLsl, 0); |
| 412 | +end; |
| 413 | + |
| 414 | +(* Shifts and bit fields *) |
| 415 | + |
| 416 | +(* sbfm, bfm or ubfm (opc 0, 1, 2): sf opc 100110 N immr imms Rn Rd, N |
| 417 | + as sf. The bits imms down to immr of rn, or, if imms < immr, the low |
| 418 | + imms + 1 bits put at width - immr; the rest of rd is the sign, kept, |
| 419 | + or zeros. Every shift by a constant, extension and field is one. *) |
| 420 | +proc bitfield(sf, opc, rd, rn, immr, imms: INT); |
| 421 | +const Names: [3][]const U8 = ["sbfm", "bfm", "ubfm"]; |
| 422 | +begin |
| 423 | + if listing then sayOps(Names[opc], [reg(rd, sf, false), reg(rn, sf, false), imm(immr), imm(imms)]); end; |
| 424 | + emit((sf shl 31) | (opc shl 29) | 13000000H | (sf shl 22) | (immr shl 16) | (imms shl 10) | (rn shl 5) | rd); |
| 425 | +end; |
| 426 | + |
| 427 | +(* A shift by n, 0 to the width less one, of size 4 or 8 bytes: lsl is |
| 428 | + ubfm by width - n of the low width - 1 - n bits, lsr ubfm and asr |
| 429 | + sbfm of the bits from n up, ror extr of rn and rn from bit n: |
| 430 | + sf 00 100111 N 0 Rm imms Rn Rd *) |
| 431 | +proc shiftI*(op, rd, rn, n, size: INT); |
| 432 | +var sf, w: INT; |
| 433 | +begin |
| 434 | + sf := bit(size = 8); w := 32 + 32 * sf; |
| 435 | + match op of |
| 436 | + | shLsl: bitfield(sf, 2, rd, rn, (w - n) & (w - 1), w - 1 - n); |
| 437 | + | shLsr: bitfield(sf, 2, rd, rn, n, w - 1); |
| 438 | + | shAsr: bitfield(sf, 0, rd, rn, n, w - 1); |
| 439 | + | else: |
| 440 | + if listing then sayOps("ror", [reg(rd, sf, false), reg(rn, sf, false), imm(n)]); end; |
| 441 | + emit((sf shl 31) | 13800000H | (sf shl 22) | (rn shl 16) | (n shl 10) | (rn shl 5) | rd); |
| 442 | + end; |
| 443 | +end; |
| 444 | + |
| 445 | +(* rd := the width bits of rn from bit lsb, as ubfx, or sbfx if signed: |
| 446 | + ubfm or sbfm of the bits lsb + width - 1 down to lsb *) |
| 447 | +proc field*(signed: BOOL; rd, rn, lsb, width: INT); |
| 448 | +begin |
| 449 | + bitfield(1, 2 - 2 * bit(signed), rd, rn, lsb, lsb + width - 1); |
| 450 | +end; |
| 451 | + |
| 452 | +(* rd := the low size bytes of rn, brought to 64 bits by their sign or |
| 453 | + zeros: sxtb, sxth, sxtw (sbfm, 64 bits); uxtb and uxth (ubfm, 32 bits, |
| 454 | + which zeroes the upper half), mov wd, wn for 4 *) |
| 455 | +proc extend*(rd, rn, size: INT; signed: BOOL); |
| 456 | +begin |
| 457 | + if signed then |
| 458 | + bitfield(1, 0, rd, rn, 0, size * 8 - 1); |
| 459 | + elsif size < 4 then |
| 460 | + bitfield(0, 2, rd, rn, 0, size * 8 - 1); |
| 461 | + else |
| 462 | + aluShifted(0, opOrr, rd, ZR, rn, shLsl, 0); |
| 463 | + end; |
| 464 | +end; |
| 465 | + |
| 466 | +(* The instructions of only registers, each its bits and n registers, |
| 467 | + Rd, Rn, Rm and Ra, in their places; a field an instruction hasn't is |
| 468 | + 0, or set in its bits: |
| 469 | + madd, msub sf 00 11011 000 Rm o0 Ra Rn Rd rd := ra + rn * rm, ra - rn * rm |
| 470 | + smulh, umulh 1 00 11011 U10 Rm 0 11111 Rn Rd the high 64 bits of the product |
| 471 | + sdiv, udiv sf 0 0 11010110 Rm 00001 o1 Rn Rd |
| 472 | + lslv ... rorv sf 0 0 11010110 Rm 0010 op2 Rn Rd by rm, modulo the width |
| 473 | + clz, rbit, rev sf 1 0 11010110 00000 0000 opc Rn Rd |
| 474 | + A division by zero gives 0, and the smallest value by -1 itself: Gen |
| 475 | + checks for both. *) |
| 476 | +proc regOp(name: []const U8; n, sf, bits, rd, rn, rm, ra: INT); |
| 477 | +var t: [4][]const U8; |
| 478 | +begin |
| 479 | + if listing then |
| 480 | + t := [reg(rd, sf, false), reg(rn, sf, false), reg(rm, sf, false), reg(ra, sf, false)]; |
| 481 | + sayOps(name, t[0..n]); |
| 482 | + end; |
| 483 | + emit((sf shl 31) | bits | (rm shl 16) | (ra shl 10) | (rn shl 5) | rd); |
| 484 | +end; |
| 485 | + |
| 486 | +proc mul*(rd, rn, rm: INT); |
| 487 | +begin |
| 488 | + regOp("mul", 3, 1, 1B007C00H, rd, rn, rm, 0); |
| 489 | +end; |
| 490 | + |
| 491 | +(* rd := ra + rn * rm, or ra - rn * rm if sub *) |
| 492 | +proc madd*(rd, rn, rm, ra: INT; sub: BOOL); |
| 493 | +const Names: [2][]const U8 = ["madd", "msub"]; |
| 494 | +begin |
| 495 | + regOp(Names[bit(sub)], 4, 1, 1B000000H | (bit(sub) shl 15), rd, rn, rm, ra); |
| 496 | +end; |
| 497 | + |
| 498 | +proc mulHigh*(signed: BOOL; rd, rn, rm: INT); |
| 499 | +const Names: [2][]const U8 = ["umulh", "smulh"]; |
| 500 | +begin |
| 501 | + regOp(Names[bit(signed)], 3, 1, 1B407C00H | (bit(not signed) shl 23), rd, rn, rm, 0); |
| 502 | +end; |
| 503 | + |
| 504 | +proc divide*(signed: BOOL; rd, rn, rm: INT); |
| 505 | +const Names: [2][]const U8 = ["udiv", "sdiv"]; |
| 506 | +begin |
| 507 | + regOp(Names[bit(signed)], 3, 1, 1AC00800H | (bit(signed) shl 10), rd, rn, rm, 0); |
| 508 | +end; |
| 509 | + |
| 510 | +(* lsl, lsr, asr or ror of size 4 or 8 bytes, by rm *) |
| 511 | +proc shift*(op, rd, rn, rm, size: INT); |
| 512 | +begin |
| 513 | + regOp(ShiftNames[op], 3, bit(size = 8), 1AC02000H | (op shl 10), rd, rn, rm, 0); |
| 514 | +end; |
| 515 | + |
| 516 | +(* the leading zeros of rn, of size 4 or 8 bytes: 64 for 0; and rn's |
| 517 | + bits in the opposite order, of which clz counts trailing zeros *) |
| 518 | +proc clz*(rd, rn, size: INT); |
| 519 | +begin |
| 520 | + regOp("clz", 2, bit(size = 8), 5AC01000H, rd, rn, 0, 0); |
| 521 | +end; |
| 522 | + |
| 523 | +proc rbit*(rd, rn, size: INT); |
| 524 | +begin |
| 525 | + regOp("rbit", 2, bit(size = 8), 5AC00000H, rd, rn, 0, 0); |
| 526 | +end; |
| 527 | + |
| 528 | +(* csel, csinc, csinv or csneg (op), on condition cc: |
| 529 | + sf op 0 11010100 Rm cond 0 o2 Rn Rd *) |
| 530 | +proc condSel(sf, op, cc, rd, rn, rm: INT); |
| 531 | +begin |
| 532 | + if listing then sayOps(CselNames[op], [reg(rd, sf, false), reg(rn, sf, false), reg(rm, sf, false), CondNames[cc]]); end; |
| 533 | + emit((sf shl 31) | CselBits[op] | (rm shl 16) | (cc shl 12) | (rn shl 5) | rd); |
| 534 | +end; |
| 535 | + |
| 536 | +proc csel*(op, cc, rd, rn, rm: INT); |
| 537 | +begin |
| 538 | + condSel(1, op, cc, rd, rn, rm); |
| 539 | +end; |
| 540 | + |
| 541 | +(* rd := 1 if cc holds, else 0: cset, csinc of the zero register on the |
| 542 | + opposite condition *) |
| 543 | +proc cset*(cc, rd: INT); |
| 544 | +begin |
| 545 | + condSel(1, csInc, invert(cc), rd, ZR, ZR); |
| 546 | +end; |
| 547 | + |
| 548 | +(* Loads and stores. ldst and its kin make them all from the bits that |
| 549 | + tell size, register file and operation, (size << 30 | V << 26 | opc |
| 550 | + << 22), and the form: |
| 551 | + unsigned offset size 111 V 01 opc imm12 Rn Rt, imm12 = off / size |
| 552 | + unscaled size 111 V 00 opc 0 imm9 00 Rn Rt (ldur, stur) |
| 553 | + post-index size 111 V 00 opc 0 imm9 01 Rn Rt |
| 554 | + pre-index size 111 V 00 opc 0 imm9 11 Rn Rt |
| 555 | + register size 111 V 00 opc 1 Rm option S 10 Rn Rt |
| 556 | + Rn 31 is sp; Rt 31 is the zero register. *) |
| 557 | + |
| 558 | +(* The bits of a load (or store) of size bytes; signed sign-extends to |
| 559 | + 64 bits (to 32 if sf is 0), fp is a real's register. For an integer |
| 560 | + opc is 00 store, 01 load, 10 load signed to x, 11 load signed to w; |
| 561 | + for a real the same but q, 16 bytes, which is size 00 and opc 1x. *) |
| 562 | +proc ldstBits(size: INT; load, signed, fp: BOOL; sf: INT): INT; |
| 563 | +begin |
| 564 | + if fp and (size = 16) then return 04000000H | ((2 + bit(load)) shl 22); end; |
| 565 | + if fp then return (scaleOf(size) shl 30) | 04000000H | (bit(load) shl 22); end; |
| 566 | + if signed and (size < 8) then return (scaleOf(size) shl 30) | ((3 - sf) shl 22); end; |
| 567 | + return (scaleOf(size) shl 30) | (bit(load) shl 22); |
| 568 | +end; |
| 569 | + |
| 570 | +(* The size of the load or store with these bits, 16 for q *) |
| 571 | +proc sizeOf(bits: INT): INT; |
| 572 | +begin |
| 573 | + if (bits & 04800000H) = 04800000H then return 16; end; |
| 574 | + return 1 shl INT(bits shr 30); |
| 575 | +end; |
| 576 | + |
| 577 | +(* The text of a load or store, its name from its bits (ldrsh, sturb, |
| 578 | + ...), its register's and its address m *) |
| 579 | +proc sayLdst(bits, rt: INT; unscaled: BOOL; m: []const U8); |
| 580 | +const Sizes: [4][]const U8 = ["b", "h", "w", ""]; |
| 581 | +var opc: INT; s, r: []const U8; |
| 582 | +begin |
| 583 | + opc := (bits shr 22) & 3; |
| 584 | + if (opc & 1) = 0 then s := "str"; else s := "ldr"; end; |
| 585 | + if unscaled then s := Diag.say("{}ur", [Fmt.str(s[0..2])]); end; |
| 586 | + if (bits & 04000000H) # 0 then |
| 587 | + r := vreg(rt, sizeOf(bits)); |
| 588 | + else |
| 589 | + r := reg(rt, bit((opc = 2) or (bits shr 30 = 3)), false); |
| 590 | + if opc >= 2 then s := Diag.say("{}s", [Fmt.str(s)]); end; |
| 591 | + if (opc >= 2) or (bits shr 30 < 2) then s := Diag.say("{}{}", [Fmt.str(s), Fmt.str(Sizes[bits shr 30])]); end; |
| 592 | + end; |
| 593 | + sayOps(s, [r, m]); |
| 594 | +end; |
| 595 | + |
| 596 | +(* rt at [rn, rm], or [rn, rm, lsl #scale] if scaled: option 011, S *) |
| 597 | +proc ldstReg(bits, rt, rn, rm: INT; scaled: BOOL); |
| 598 | +begin |
| 599 | + 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; |
| 600 | + emit(38206800H | bits | (rm shl 16) | (bit(scaled) shl 12) | (rn shl 5) | rt); |
| 601 | +end; |
| 602 | + |
| 603 | +(* rt at [rn, #off]: by its unsigned offset if off is a multiple of the |
| 604 | + size below 4096 of them, else unscaled if off is in -256 to 255; else |
| 605 | + off is built in x16 for the register form *) |
| 606 | +proc ldst(bits, rt, rn, off: INT); |
| 607 | +var sc: INT; |
| 608 | +begin |
| 609 | + sc := scaleOf(sizeOf(bits)); |
| 610 | + if (off >= 0) and ((off & ((1 shl sc) - 1)) = 0) and ((off shr sc) < 4096) then |
| 611 | + if listing then sayLdst(bits, rt, false, memText(rn, off, mOff)); end; |
| 612 | + emit(39000000H | bits | ((off shr sc) shl 10) | (rn shl 5) | rt); |
| 613 | + elsif (off >= -256) and (off <= 255) then |
| 614 | + if listing then sayLdst(bits, rt, true, memText(rn, off, mOff)); end; |
| 615 | + emit(38000000H | bits | ((off & 1FFH) shl 12) | (rn shl 5) | rt); |
| 616 | + else |
| 617 | + movRI(X16, off); |
| 618 | + ldstReg(bits, rt, rn, X16, false); |
| 619 | + end; |
| 620 | +end; |
| 621 | + |
| 622 | +(* rt at [rn, #off]!, rn changed first, or [rn], #off, after; off in |
| 623 | + -256 to 255 *) |
| 624 | +proc ldstIdx(bits, rt, rn, off, mode: INT); |
| 625 | +begin |
| 626 | + if (off < -256) or (off > 255) then _ := Scan.mark("an index beyond -256 to 255"); end; |
| 627 | + if listing then sayLdst(bits, rt, false, memText(rn, off, mode)); end; |
| 628 | + emit(38000000H | bits | ((off & 1FFH) shl 12) | ((2 * bit(mode = mPre) + 1) shl 10) | (rn shl 5) | rt); |
| 629 | +end; |
| 630 | + |
| 631 | +(* rt := the size bytes at [rn, #off], 1, 2, 4 or 8, by their sign or |
| 632 | + zeros; and the store *) |
| 633 | +proc load*(rt, rn, off, size: INT; signed: BOOL); |
| 634 | +begin |
| 635 | + ldst(ldstBits(size, true, signed, false, 1), rt, rn, off); |
| 636 | +end; |
| 637 | + |
| 638 | +proc store*(rt, rn, off, size: INT); |
| 639 | +begin |
| 640 | + ldst(ldstBits(size, false, false, false, 1), rt, rn, off); |
| 641 | +end; |
| 642 | + |
| 643 | +(* [rn + rm], or [rn + rm * size] if scaled *) |
| 644 | +proc loadR*(rt, rn, rm, size: INT; signed, scaled: BOOL); |
| 645 | +begin |
| 646 | + ldstReg(ldstBits(size, true, signed, false, 1), rt, rn, rm, scaled); |
| 647 | +end; |
| 648 | + |
| 649 | +proc storeR*(rt, rn, rm, size: INT; scaled: BOOL); |
| 650 | +begin |
| 651 | + ldstReg(ldstBits(size, false, false, false, 1), rt, rn, rm, scaled); |
| 652 | +end; |
| 653 | + |
| 654 | +(* with rn changed, mPre or mPost, as a push is str x0, [sp, #-16]! *) |
| 655 | +proc loadIdx*(rt, rn, off, size, mode: INT); |
| 656 | +begin |
| 657 | + ldstIdx(ldstBits(size, true, false, false, 1), rt, rn, off, mode); |
| 658 | +end; |
| 659 | + |
| 660 | +proc storeIdx*(rt, rn, off, size, mode: INT); |
| 661 | +begin |
| 662 | + ldstIdx(ldstBits(size, false, false, false, 1), rt, rn, off, mode); |
| 663 | +end; |
| 664 | + |
| 665 | +(* A real's register, of size 4, 8 or 16 *) |
| 666 | +proc loadF*(vt, rn, off, size: INT); |
| 667 | +begin |
| 668 | + ldst(ldstBits(size, true, false, true, 1), vt, rn, off); |
| 669 | +end; |
| 670 | + |
| 671 | +proc storeF*(vt, rn, off, size: INT); |
| 672 | +begin |
| 673 | + ldst(ldstBits(size, false, false, true, 1), vt, rn, off); |
| 674 | +end; |
| 675 | + |
| 676 | +(* ldp and stp: two registers at [rn, #off], off a multiple of the size |
| 677 | + in -64 to 63 of them: opc 101 V mode L imm7 Rt2 Rn Rt, opc 00 for w or |
| 678 | + s, 10 for x, 01 for d, 10 for q *) |
| 679 | +proc pair(load, fp: BOOL; size, rt, rt2, rn, off, mode: INT); |
| 680 | +const Names: [2][]const U8 = ["stp", "ldp"]; |
| 681 | +var opc: INT; |
| 682 | +begin |
| 683 | + if fp then opc := scaleOf(size) - 2; else opc := 2 * bit(size = 8); end; |
| 684 | + if ((off & (size - 1)) # 0) or (off < -64 * size) or (off > 63 * size) then |
| 685 | + _ := Scan.mark("a pair's offset beyond -64 to 63 of its size"); |
| 686 | + end; |
| 687 | + if listing then sayOps(Names[bit(load)], [x(rt), x(rt2), memText(rn, off, mode)]); end; |
| 688 | + emit((opc shl 30) | 28000000H | (bit(fp) shl 26) | (mode shl 23) | (bit(load) shl 22) |
| 689 | + | (((off div size) & 7FH) shl 15) | (rt2 shl 10) | (rn shl 5) | rt); |
| 690 | +end; |
| 691 | + |
| 692 | +proc ldp*(rt, rt2, rn, off, mode: INT); |
| 693 | +begin |
| 694 | + pair(true, false, 8, rt, rt2, rn, off, mode); |
| 695 | +end; |
| 696 | + |
| 697 | +proc stp*(rt, rt2, rn, off, mode: INT); |
| 698 | +begin |
| 699 | + pair(false, false, 8, rt, rt2, rn, off, mode); |
| 700 | +end; |
| 701 | + |
| 702 | +(* Atomics, by the LSE instructions every Apple processor has, of size 4 |
| 703 | + or 8 bytes, each with acquire and release, so sequentially consistent: |
| 704 | + ldaddal rs, rt, [rn] size 111 0 00 1 1 1 Rs 0 000 00 Rn Rt: rt := [rn], [rn] += rs |
| 705 | + swpal rs, rt, [rn] size 111 0 00 1 1 1 Rs 1 000 00 Rn Rt: rt := [rn], [rn] := rs |
| 706 | + casal rs, rt, [rn] size 001000 1 1 1 Rs 1 11111 Rn Rt: if [rn] = rs, [rn] := rt; rs := old [rn] |
| 707 | + The assembler knows the exclusive pair too, for a loop of its own: |
| 708 | + ldaxr rt, [rn] size 001000 0 1 0 11111 1 11111 Rn Rt |
| 709 | + stlxr ws, rt, [rn] size 001000 0 0 0 Rs 1 11111 Rn Rt, ws 0 if it stored *) |
| 710 | +const |
| 711 | + AtomNames: [5][]const U8 = ["ldaddal", "swpal", "casal", "ldaxr", "stlxr"]; |
| 712 | + AtomBits: [5]INT = [38E00000H, 38E08000H, 08E0FC00H, 085FFC00H, 0800FC00H]; |
| 713 | + |
| 714 | +proc atomic(op, size, rs, rt, rn: INT); |
| 715 | +begin |
| 716 | + if listing then sayOps(AtomNames[op], [reg(rs, bit(size = 8), false), reg(rt, bit(size = 8), false), memText(rn, 0, mOff)]); end; |
| 717 | + emit(80000000H | (bit(size = 8) shl 30) | AtomBits[op] | (rs shl 16) | (rn shl 5) | rt); |
| 718 | +end; |
| 719 | + |
| 720 | +proc ldaddal*(rs, rt, rn, size: INT); |
| 721 | +begin |
| 722 | + atomic(0, size, rs, rt, rn); |
| 723 | +end; |
| 724 | + |
| 725 | +proc swpal*(rs, rt, rn, size: INT); |
| 726 | +begin |
| 727 | + atomic(1, size, rs, rt, rn); |
| 728 | +end; |
| 729 | + |
| 730 | +proc casal*(rs, rt, rn, size: INT); |
| 731 | +begin |
| 732 | + atomic(2, size, rs, rt, rn); |
| 733 | +end; |
| 734 | + |
| 735 | +(* The rest of the system's: a barrier, the inner shareable domain's, |
| 736 | + which is every processor's (dmb ish, 1101010100 0 00 011 0011 CRm 1 01 |
| 737 | + 11111, CRm 1011); a call of the system, svc #imm; a trap, brk #imm *) |
| 738 | +proc dmb*; |
| 739 | +begin |
| 740 | + if listing then say("dmb ish"); end; |
| 741 | + emit(0D5033BBFH); |
| 742 | +end; |
| 743 | + |
| 744 | +proc svc*(n: INT); |
| 745 | +begin |
| 746 | + if listing then sayOps("svc", [imm(n)]); end; |
| 747 | + emit(0D4000001H | ((n & 0FFFFH) shl 5)); |
| 748 | +end; |
| 749 | + |
| 750 | +proc brk*(n: INT); |
| 751 | +begin |
| 752 | + if listing then sayOps("brk", [imm(n)]); end; |
| 753 | + emit(0D4200000H | ((n & 0FFFFH) shl 5)); |
| 754 | +end; |
| 755 | + |
| 756 | +(* Code made up to a multiple of n bytes with nop *) |
| 757 | +proc align*(n: INT); |
| 758 | +begin |
| 759 | + while Chunk.pc mod n # 0 do |
| 760 | + if listing then say("nop"); end; |
| 761 | + emit(Nop); |
| 762 | + end; |
| 763 | +end; |
| 764 | + |
| 765 | +(* Reals: type 00 is single, an F32, 01 double *) |
| 766 | + |
| 767 | +(* fmul, fdiv, fadd, fsub, fmax and fmin, vd := vn op vm: |
| 768 | + 0 0 0 11110 type 1 Rm opcode 10 Rn Rd *) |
| 769 | +proc fop*(op, vd, vn, vm: INT; single: BOOL); |
| 770 | +var s: INT; |
| 771 | +begin |
| 772 | + s := 8 - 4 * bit(single); |
| 773 | + if listing then sayOps(FopNames[op], [vreg(vd, s), vreg(vn, s), vreg(vm, s)]); end; |
| 774 | + emit(1E200800H | (bit(not single) shl 22) | (vm shl 16) | (op shl 12) | (vn shl 5) | vd); |
| 775 | +end; |
| 776 | + |
| 777 | +(* fmov, fabs, fneg and fsqrt, of one register: |
| 778 | + 0 0 0 11110 type 1 opcode 10000 Rn Rd *) |
| 779 | +proc fop1*(op, vd, vn: INT; single: BOOL); |
| 780 | +var s: INT; |
| 781 | +begin |
| 782 | + s := 8 - 4 * bit(single); |
| 783 | + if listing then sayOps(Fop1Names[op], [vreg(vd, s), vreg(vn, s)]); end; |
| 784 | + emit(1E204000H | (bit(not single) shl 22) | (op shl 15) | (vn shl 5) | vd); |
| 785 | +end; |
| 786 | + |
| 787 | +(* vd := vn converted, to an F32 if toSingle, else to an F64: fop1's form |
| 788 | + with opcode 0001, then the type made, 00 single or 01 double *) |
| 789 | +proc fcvt*(vd, vn: INT; toSingle: BOOL); |
| 790 | +begin |
| 791 | + if listing then sayOps("fcvt", [vreg(vd, 8 - 4 * bit(toSingle)), vreg(vn, 4 + 4 * bit(toSingle))]); end; |
| 792 | + emit(1E204000H | (bit(toSingle) shl 22) | ((5 - bit(toSingle)) shl 15) | (vn shl 5) | vd); |
| 793 | +end; |
| 794 | + |
| 795 | +(* fcmp vn, vm, or vn with 0.0 if vm < 0: the flags as an unsigned |
| 796 | + compare sets them, but for unordered (a NaN), which is C and V: |
| 797 | + 0 0 0 11110 type 1 Rm 00 1000 Rn 0 opc 000, opc 01 for 0.0 *) |
| 798 | +proc fcmp*(vn, vm: INT; single: BOOL); |
| 799 | +var s: INT; |
| 800 | +begin |
| 801 | + s := 8 - 4 * bit(single); |
| 802 | + if vm < 0 then |
| 803 | + if listing then sayOps("fcmp", [vreg(vn, s), "#0.0"]); end; |
| 804 | + emit(1E202008H | (bit(not single) shl 22) | (vn shl 5)); |
| 805 | + else |
| 806 | + if listing then sayOps("fcmp", [vreg(vn, s), vreg(vm, s)]); end; |
| 807 | + emit(1E202000H | (bit(not single) shl 22) | (vm shl 16) | (vn shl 5)); |
| 808 | + end; |
| 809 | +end; |
| 810 | + |
| 811 | +(* Between the register files: sf 0 0 11110 type 1 rmode opcode 000000 |
| 812 | + Rn Rd, the real of size 4 or 8 bytes, to it if toFp. rmode 00 opcode |
| 813 | + 010 scvtf, 011 ucvtf, 110 fmov to a general register, 111 fmov from |
| 814 | + one; rmode 11 opcode 000 fcvtzs, 001 fcvtzu, which round toward zero *) |
| 815 | +proc fconv(name: []const U8; sf, size, rmode, opcode, rd, rn: INT; toFp: BOOL); |
| 816 | +begin |
| 817 | + if listing and toFp then sayOps(name, [vreg(rd, size), reg(rn, sf, false)]); end; |
| 818 | + if listing and not toFp then sayOps(name, [reg(rd, sf, false), vreg(rn, size)]); end; |
| 819 | + emit((sf shl 31) | 1E200000H | (bit(size = 8) shl 22) | (rmode shl 19) | (opcode shl 16) | (rn shl 5) | rd); |
| 820 | +end; |
| 821 | + |
| 822 | +(* vd := the 64-bit integer in rn, signed or not, as a real *) |
| 823 | +proc scvtf*(vd, rn: INT; single, signed: BOOL); |
| 824 | +const Names: [2][]const U8 = ["ucvtf", "scvtf"]; |
| 825 | +begin |
| 826 | + fconv(Names[bit(signed)], 1, 8 - 4 * bit(single), 0, 3 - bit(signed), vd, rn, true); |
| 827 | +end; |
| 828 | + |
| 829 | +(* rd := vn toward zero as a 64-bit integer; out of range it is the |
| 830 | + nearest, and a NaN 0, where x86 gives the smallest I64: Gen mends it *) |
| 831 | +proc fcvtz*(rd, vn: INT; single, signed: BOOL); |
| 832 | +const Names: [2][]const U8 = ["fcvtzu", "fcvtzs"]; |
| 833 | +begin |
| 834 | + fconv(Names[bit(signed)], 1, 8 - 4 * bit(single), 3, bit(not signed), rd, vn, false); |
| 835 | +end; |
| 836 | + |
| 837 | +(* The bits of a real between the files: s and w, d and x *) |
| 838 | +proc fmovFR*(vd, rn: INT; single: BOOL); |
| 839 | +begin |
| 840 | + fconv("fmov", bit(not single), 8 - 4 * bit(single), 0, 7, vd, rn, true); |
| 841 | +end; |
| 842 | + |
| 843 | +proc fmovRF*(rd, vn: INT; single: BOOL); |
| 844 | +begin |
| 845 | + fconv("fmov", bit(not single), 8 - 4 * bit(single), 0, 6, rd, vn, false); |
| 846 | +end; |
| 847 | + |
| 848 | +(* Addresses, and references to other chunks *) |
| 849 | + |
| 850 | +(* rd := the address of this instruction plus off, within 1 MB (adr), |
| 851 | + or its page of 4 KB plus off pages (adrp): op immlo 10000 immhi Rd *) |
| 852 | +proc adrOp(page, rd, off: INT); |
| 853 | +begin |
| 854 | + emit((page shl 31) | 10000000H | ((off & 3) shl 29) | (((off shr 2) & 7FFFFH) shl 5) | rd); |
| 855 | +end; |
| 856 | + |
| 857 | +proc adr*(rd, off: INT); |
| 858 | +begin |
| 859 | + if listing then sayOps("adr", [x(rd), Diag.say(". + {}", [Fmt.int(off)])]); end; |
| 860 | + adrOp(0, rd, off); |
| 861 | +end; |
| 862 | + |
| 863 | +(* adrp rd with a fixup to the page of chunk c plus addend *) |
| 864 | +proc adrpChunk(rd, c, addend: INT); |
| 865 | +begin |
| 866 | + if listing then sayOps("adrp", [x(rd), Diag.say("{}@PAGE", [Fmt.str(Chunk.chunkName(c))])]); end; |
| 867 | + Chunk.addKindFixup(Chunk.pc, c, addend, Chunk.fxPage21); |
| 868 | + adrOp(1, rd, 0); |
| 869 | +end; |
| 870 | + |
| 871 | +(* rd := the address of chunk c plus addend, wherever the linker puts |
| 872 | + it, since macOS loads a program anywhere: adrp of its page, then add |
| 873 | + of its place in the page, each with its fixup *) |
| 874 | +proc addrChunk*(rd, c, addend: INT); |
| 875 | +begin |
| 876 | + adrpChunk(rd, c, addend); |
| 877 | + if listing then sayOps("add", [x(rd), x(rd), Diag.say("{}@PAGEOFF", [Fmt.str(Chunk.chunkName(c))])]); end; |
| 878 | + Chunk.addKindFixup(Chunk.pc, c, addend, Chunk.fxPageOff12); |
| 879 | + emit(91000000H | (rd shl 5) | rd); |
| 880 | +end; |
| 881 | + |
| 882 | +(* A load or store of the bits ldstBits gives at chunk c plus addend: |
| 883 | + adrp x16, then the load's or store's unsigned offset is the place in |
| 884 | + the page, divided by the size, which the linker reads from the |
| 885 | + instruction, as Apple's linker does for ARM64_RELOC_PAGEOFF12. That |
| 886 | + offset must then be a multiple of the size: when the chunk's |
| 887 | + alignment doesn't make it one, the address is made whole first. As a |
| 888 | + C symbol's quadword is read: loadChunk(rt, externChunk(name), 0, 8). *) |
| 889 | +proc chunkMem(bits, rt, c, addend: INT); |
| 890 | +begin |
| 891 | + if ((addend & (sizeOf(bits) - 1)) # 0) or (Chunk.chunks[c].align < sizeOf(bits)) then |
| 892 | + addrChunk(X16, c, addend); |
| 893 | + ldst(bits, rt, X16, 0); |
| 894 | + return; |
| 895 | + end; |
| 896 | + adrpChunk(X16, c, addend); |
| 897 | + if listing then sayLdst(bits, rt, false, Diag.say("[x16, {}@PAGEOFF]", [Fmt.str(Chunk.chunkName(c))])); end; |
| 898 | + Chunk.addKindFixup(Chunk.pc, c, addend, Chunk.fxPageOff12); |
| 899 | + emit(39000000H | bits | (X16 shl 5) | rt); |
| 900 | +end; |
| 901 | + |
| 902 | +proc loadChunk*(rt, c, addend, size: INT; signed: BOOL); |
| 903 | +begin |
| 904 | + chunkMem(ldstBits(size, true, signed, false, 1), rt, c, addend); |
| 905 | +end; |
| 906 | + |
| 907 | +proc storeChunk*(rt, c, addend, size: INT); |
| 908 | +begin |
| 909 | + chunkMem(ldstBits(size, false, false, false, 1), rt, c, addend); |
| 910 | +end; |
| 911 | + |
| 912 | +proc loadChunkF*(vt, c, addend, size: INT); |
| 913 | +begin |
| 914 | + chunkMem(ldstBits(size, true, false, true, 1), vt, c, addend); |
| 915 | +end; |
| 916 | + |
| 917 | +proc storeChunkF*(vt, c, addend, size: INT); |
| 918 | +begin |
| 919 | + chunkMem(ldstBits(size, false, false, true, 1), vt, c, addend); |
| 920 | +end; |
| 921 | + |
| 922 | +(* bl or b to the start of chunk c, a fixup: 1 00101 imm26 for bl, 0 |
| 923 | + 00101 for b, imm26 the distance in words, within 128 MB *) |
| 924 | +proc branchChunk(call: BOOL; c: INT); |
| 925 | +const Names: [2][]const U8 = ["b", "bl"]; |
| 926 | +begin |
| 927 | + if listing then sayOps(Names[bit(call)], [Chunk.chunkName(c)]); end; |
| 928 | + Chunk.addKindFixup(Chunk.pc, c, 0, Chunk.fxBranch26); |
| 929 | + emit(14000000H | (bit(call) shl 31)); |
| 930 | +end; |
| 931 | + |
| 932 | +proc callChunk*(c: INT); |
| 933 | +begin |
| 934 | + branchChunk(true, c); |
| 935 | +end; |
| 936 | + |
| 937 | +(* a tail call *) |
| 938 | +proc jmpChunk*(c: INT); |
| 939 | +begin |
| 940 | + branchChunk(false, c); |
| 941 | +end; |
| 942 | + |
| 943 | +(* bl to target, a place in text already written *) |
| 944 | +proc callBack*(target: INT); |
| 945 | +begin |
| 946 | + if listing then say("bl ->"); end; |
| 947 | + emit(94000000H | (((target - Chunk.pc) div 4) & 3FFFFFFH)); |
| 948 | +end; |
| 949 | + |
| 950 | +(* blr, br and ret, to the address in rn: |
| 951 | + 1101011 0 0 opc 11111 000000 Rn 00000, opc 01 blr, 00 br, 10 ret *) |
| 952 | +proc callR*(rn: INT); |
| 953 | +begin |
| 954 | + if listing then sayOps("blr", [x(rn)]); end; |
| 955 | + emit(0D63F0000H | (rn shl 5)); |
| 956 | +end; |
| 957 | + |
| 958 | +proc jmpR*(rn: INT); |
| 959 | +begin |
| 960 | + if listing then sayOps("br", [x(rn)]); end; |
| 961 | + emit(0D61F0000H | (rn shl 5)); |
| 962 | +end; |
| 963 | + |
| 964 | +proc ret*; |
| 965 | +begin |
| 966 | + if listing then say("ret"); end; |
| 967 | + emit(0D65F03C0H); |
| 968 | +end; |
| 969 | + |
| 970 | +(* Jumps within a procedure. A label not yet placed is the chain of the |
| 971 | + jumps to it, as in X64, each jump's offset field holding the place |
| 972 | + of the one before, in words from the start of the procedure's chunk, |
| 973 | + plus one: 0 ends the chain. Placing the label walks the chain and |
| 974 | + puts each jump's distance in its field. The fields are of three |
| 975 | + widths: |
| 976 | + b, bl 0 00101 imm26 within 128 MB |
| 977 | + b.cond 0101010 0 imm19 0 cond within 1 MB |
| 978 | + cbz, cbnz sf 011010 op imm19 Rt within 1 MB |
| 979 | + tbz, tbnz b5 011011 op b40 imm14 Rt within 32 KB |
| 980 | + A b.cond or a cbz reaches 1 MB, a procedure of a quarter of a million |
| 981 | + instructions; past that the compile ends with an error, rather than |
| 982 | + make every conditional jump two instructions for a procedure no one |
| 983 | + writes. A tbz reaches only 32 KB, which a long procedure passes: so |
| 984 | + Gen's tbz is the opposite tbz over a b, which reaches as far as any b |
| 985 | + (tbz below); only the assembler's tbz is one instruction, in a code |
| 986 | + procedure that is never that long. *) |
| 987 | + |
| 988 | +(* Where the offset field of the branch insn is, its lowest bit and its |
| 989 | + width *) |
| 990 | +proc fieldOf(insn: INT): (INT, INT); |
| 991 | +begin |
| 992 | + if (insn & 7C000000H) = 14000000H then return 0, 26; end; |
| 993 | + if (insn & 7E000000H) = 36000000H then return 5, 14; end; |
| 994 | + return 5, 19; |
| 995 | +end; |
| 996 | + |
| 997 | +(* The jump before the one at L on its chain, from the link in its field *) |
| 998 | +proc before(L: INT): INT; |
| 999 | +var insn, lo, w, prev: INT; |
| 1000 | +begin |
| 1001 | + insn := Chunk.get4At(L) & 0FFFFFFFFH; |
| 1002 | + lo, w := fieldOf(insn); |
| 1003 | + prev := (insn shr lo) & ((1 shl w) - 1); |
| 1004 | + if prev = 0 then return NoLabel; end; |
| 1005 | + return Chunk.chunks[Chunk.curChunk].start + (prev - 1) * 4; |
| 1006 | +end; |
| 1007 | + |
| 1008 | +(* insn with its field set: to the distance d in words if back, else to |
| 1009 | + the link to the jump at d, or to none if d is NoLabel; either must fit *) |
| 1010 | +proc withField(insn, d: INT; back: BOOL): INT; |
| 1011 | +var lo, w: INT; |
| 1012 | +begin |
| 1013 | + lo, w := fieldOf(insn); |
| 1014 | + if not back and (d = NoLabel) then |
| 1015 | + d := 0; |
| 1016 | + elsif not back then |
| 1017 | + d := (d - Chunk.chunks[Chunk.curChunk].start) div 4 + 1; |
| 1018 | + end; |
| 1019 | + if back and ((d < -(1 shl (w - 1))) or (d >= (1 shl (w - 1)))) or not back and (d >= (1 shl w)) then |
| 1020 | + _ := Scan.mark("a jump within a procedure beyond what its instruction reaches (b.cond and cbz: 1 MB)"); |
| 1021 | + end; |
| 1022 | + return (insn & ~(((1 shl w) - 1) shl lo)) | ((d & ((1 shl w) - 1)) shl lo); |
| 1023 | +end; |
| 1024 | + |
| 1025 | +(* insn, a branch to L: back to the place L if back, else on L's chain; |
| 1026 | + the chain's new first jump *) |
| 1027 | +proc branch(insn, L: INT; back: BOOL): INT; |
| 1028 | +begin |
| 1029 | + if back then emit(withField(insn, (L - Chunk.pc) div 4, true)); return L; end; |
| 1030 | + emit(withField(insn, L, false)); |
| 1031 | + return Chunk.pc - 4; |
| 1032 | +end; |
| 1033 | + |
| 1034 | +proc jmp*(var L: INT); |
| 1035 | +begin |
| 1036 | + if listing then say("b ->"); end; |
| 1037 | + L := branch(14000000H, L, false); |
| 1038 | +end; |
| 1039 | + |
| 1040 | +proc jcc*(cc: INT; var L: INT); |
| 1041 | +begin |
| 1042 | + if listing then say(Diag.say("b.{} ->", [Fmt.str(CondNames[cc])])); end; |
| 1043 | + L := branch(54000000H | cc, L, false); |
| 1044 | +end; |
| 1045 | + |
| 1046 | +(* to L if the 64 bits of r are zero (cbz), or not (cbnz) *) |
| 1047 | +proc cbz*(nonzero: BOOL; r: INT; var L: INT); |
| 1048 | +const Names: [2][]const U8 = ["cbz", "cbnz"]; |
| 1049 | +begin |
| 1050 | + if listing then sayOps(Names[bit(nonzero)], [x(r), "->"]); end; |
| 1051 | + L := branch(0B4000000H | (bit(nonzero) shl 24) | r, L, false); |
| 1052 | +end; |
| 1053 | + |
| 1054 | +(* to L if bit b of r is zero (tbz) or one (tbnz): the opposite test |
| 1055 | + over the next instruction, a b to L: b5 011011 op b40 imm14 Rt, b's |
| 1056 | + number split as b5 and b40, imm14 2 *) |
| 1057 | +proc tbz*(nonzero: BOOL; r, b: INT; var L: INT); |
| 1058 | +const Names: [2][]const U8 = ["tbnz", "tbz"]; |
| 1059 | +begin |
| 1060 | + if listing then sayOps(Names[bit(nonzero)], [x(r), imm(b), ". + 8"]); end; |
| 1061 | + emit(36000000H | (bit(not nonzero) shl 24) | ((b shr 5) shl 31) | ((b & 31) shl 19) | (2 shl 5) | r); |
| 1062 | + jmp(L); |
| 1063 | +end; |
| 1064 | + |
| 1065 | +proc jmpBack*(target: INT); |
| 1066 | +begin |
| 1067 | + if listing then say("b ->"); end; |
| 1068 | + _ := branch(14000000H, target, true); |
| 1069 | +end; |
| 1070 | + |
| 1071 | +proc jccBack*(cc, target: INT); |
| 1072 | +begin |
| 1073 | + if listing then say(Diag.say("b.{} ->", [Fmt.str(CondNames[cc])])); end; |
| 1074 | + _ := branch(54000000H | cc, target, true); |
| 1075 | +end; |
| 1076 | + |
| 1077 | +proc fixLinkTo*(L, target: INT); |
| 1078 | +var next: INT; |
| 1079 | +begin |
| 1080 | + while L # NoLabel do |
| 1081 | + next := before(L); |
| 1082 | + Chunk.put4At(L, withField(Chunk.get4At(L) & 0FFFFFFFFH, (target - L) div 4, true)); |
| 1083 | + L := next; |
| 1084 | + end; |
| 1085 | +end; |
| 1086 | + |
| 1087 | +proc fixLink*(L: INT); |
| 1088 | +begin |
| 1089 | + if L # NoLabel then labelAt := Chunk.pc; end; |
| 1090 | + fixLinkTo(L, Chunk.pc); |
| 1091 | +end; |
| 1092 | + |
| 1093 | +(* If the last instruction made is a b to L, it is taken back, as in |
| 1094 | + X64: L is that b when it is the last word and an unconditional b |
| 1095 | + (000101 in the top bits), and no jump was fixed to just after it. |
| 1096 | + L becomes the jump before it on the chain. *) |
| 1097 | +proc dropJumpTo*(var L: INT); |
| 1098 | +begin |
| 1099 | + if (L = NoLabel) or (L # Chunk.pc - 4) or (labelAt = Chunk.pc) or ((Chunk.get4At(L) & 0FC000000H) # 14000000H) then return; end; |
| 1100 | + L := before(L); |
| 1101 | + Chunk.takeBack(4); |
| 1102 | + if listing and (numSaid > 0) and (said[numSaid - 1].at = Chunk.pc) then DEC(numSaid); end; |
| 1103 | +end; |
| 1104 | + |
| 1105 | +(* The jumps of chains L1 and L2, as one chain: the last of L2's links to |
| 1106 | + L1's first *) |
| 1107 | +proc joinLinks*(L1, L2: INT): INT; |
| 1108 | +var L: INT; |
| 1109 | +begin |
| 1110 | + if L2 = NoLabel then return L1; end; |
| 1111 | + if L1 # NoLabel then |
| 1112 | + L := L2; |
| 1113 | + while before(L) # NoLabel do L := before(L); end; |
| 1114 | + Chunk.put4At(L, withField(Chunk.get4At(L) & 0FFFFFFFFH, L1, false)); |
| 1115 | + end; |
| 1116 | + return L2; |
| 1117 | +end; |
| 1118 | + |
| 1119 | +(* The assembler, for code procedures: arm64 in Arm's syntax, as its |
| 1120 | + reference manual writes it, destination first (report, appendix A): |
| 1121 | + `add x0, x1, #16;`, `ldr w2, [x0, #4];`, `stp x29, x30, [sp, #-16]!;`, |
| 1122 | + `b.ne loop;`. The # before an immediate may be left out; numbers are |
| 1123 | + raz's, 10H and not 0x10, and a constant of the module stands where a |
| 1124 | + number does. A procedure or a module variable stands for its address |
| 1125 | + in `bl p`, `b p` and `adr xN, v`, the last made as adrp and add, two |
| 1126 | + instructions, since the variable may lie beyond adr's 1 MB. It reads |
| 1127 | + instructions with the scanner up to end, as X64's assembler does, and |
| 1128 | + encodes them with the routines above, the listing off: an immediate |
| 1129 | + or offset out of an instruction's range is an error here, where Gen's |
| 1130 | + routines build it in x16. *) |
| 1131 | + |
| 1132 | +const |
| 1133 | + NameOnly = "a procedure or a variable stands for its address only in bl p, b p and adr r, v"; |
| 1134 | + |
| 1135 | +type |
| 1136 | + OpKind = (oReg, oFp, oImm, oMem, oShift, oName, oLabel, oWord); |
| 1137 | + Operand = record |
| 1138 | + kind: OpKind; |
| 1139 | + r, sf: INT; (* oReg: the register, and 1 for x, 0 for w; oFp, oMem: the register *) |
| 1140 | + sp: BOOL; (* oReg: 31 written sp or wsp, not xzr or wzr *) |
| 1141 | + size: INT; (* oFp: 4, 8 or 16 *) |
| 1142 | + v: I64; (* oImm; oShift: the amount; oMem: the offset, or the index's shift *) |
| 1143 | + sh: INT; (* oShift: lsl, lsr, asr or ror *) |
| 1144 | + index, mode: INT; (* oMem: an index register or -1, mOff, mPre or mPost *) |
| 1145 | + scaled: BOOL; (* oMem: the index shifted by lsl *) |
| 1146 | + c: INT; (* oName: the chunk; oLabel: the label *) |
| 1147 | + w: []const U8; (* oWord: a condition or a barrier's option *) |
| 1148 | + at: Diag.Pos; |
| 1149 | + end; |
| 1150 | + |
| 1151 | +var |
| 1152 | + (* the labels of the procedure *) |
| 1153 | + lnames, lplaces, lchains: [64]INT; |
| 1154 | + lusedAt: [64]Diag.Pos; |
| 1155 | + numLabels: INT; |
| 1156 | + (* the instruction being assembled *) |
| 1157 | + aw: []const U8; |
| 1158 | + ao: [6]Operand; |
| 1159 | + an, acc: INT; (* how many operands; b.cond's condition *) |
| 1160 | + aat: Diag.Pos; |
| 1161 | + |
| 1162 | +proc asmWord: []const U8; |
| 1163 | +begin |
| 1164 | + if Scan.tok = Scan.sIdent then return Scan.nameOf(Scan.id); end; |
| 1165 | + if Scan.tok = Scan.sAnd then return "and"; end; |
| 1166 | + return ""; |
| 1167 | +end; |
| 1168 | + |
| 1169 | +(* The number w spells from its second letter, one or two digits, below |
| 1170 | + lim; else -1 *) |
| 1171 | +proc numberIn(w: []const U8; lim: INT): INT; |
| 1172 | +var i, n: INT; |
| 1173 | +begin |
| 1174 | + if (LEN(w) < 2) or (LEN(w) > 3) then return -1; end; |
| 1175 | + n := 0; |
| 1176 | + for i := 1 to LEN(w) - 1 do |
| 1177 | + if (w[i] < '0') or (w[i] > '9') then return -1; end; |
| 1178 | + n := n * 10 + INT(w[i] - '0'); |
| 1179 | + end; |
| 1180 | + if n >= lim then return -1; end; |
| 1181 | + return n; |
| 1182 | +end; |
| 1183 | + |
| 1184 | +(* Whether w names a general register, x0 to x30, w0 to w30, sp, wsp, |
| 1185 | + xzr, wzr, fp or lr, or a real's, s, d or q and 0 to 31: o is set if so *) |
| 1186 | +proc isReg(w: []const U8; var o: Operand): BOOL; |
| 1187 | +begin |
| 1188 | + o.kind := oReg; o.sf := 1; |
| 1189 | + if LEN(w) < 2 then |
| 1190 | + return false; |
| 1191 | + elsif (w = "sp") or (w = "xzr") or (w = "wsp") or (w = "wzr") then |
| 1192 | + o.r := 31; o.sf := bit(w[0] # 'w'); o.sp := (w = "sp") or (w = "wsp"); |
| 1193 | + elsif (w = "fp") or (w = "lr") then |
| 1194 | + o.r := 29 + bit(w = "lr"); |
| 1195 | + elsif ((w[0] = 'x') or (w[0] = 'w')) and (numberIn(w, 31) >= 0) then |
| 1196 | + o.r := numberIn(w, 31); o.sf := bit(w[0] = 'x'); |
| 1197 | + elsif ((w[0] = 's') or (w[0] = 'd') or (w[0] = 'q')) and (numberIn(w, 32) >= 0) then |
| 1198 | + o.kind := oFp; o.r := numberIn(w, 32); o.sf := 0; |
| 1199 | + if w[0] = 's' then o.size := 4; elsif w[0] = 'd' then o.size := 8; else o.size := 16; end; |
| 1200 | + else |
| 1201 | + return false; |
| 1202 | + end; |
| 1203 | + return true; |
| 1204 | +end; |
| 1205 | + |
| 1206 | +proc wordIndex(w: []const U8; list: []const []const U8): INT; |
| 1207 | +var i: INT; |
| 1208 | +begin |
| 1209 | + for i := 0 to LEN(list) - 1 do |
| 1210 | + if list[i] = w then return i; end; |
| 1211 | + end; |
| 1212 | + return -1; |
| 1213 | +end; |
| 1214 | + |
| 1215 | +(* The condition w names, cs and cc as hs and lo; -1 if none *) |
| 1216 | +proc cond(w: []const U8): INT; |
| 1217 | +begin |
| 1218 | + if w = "cs" then return ccHS; end; |
| 1219 | + if w = "cc" then return ccLO; end; |
| 1220 | + return wordIndex(w, CondNames); |
| 1221 | +end; |
| 1222 | + |
| 1223 | +proc labelIndex(n: INT): INT; |
| 1224 | +var i: INT; |
| 1225 | +begin |
| 1226 | + for i := 0 to numLabels - 1 do |
| 1227 | + if lnames[i] = n then return i; end; |
| 1228 | + end; |
| 1229 | + return -1; |
| 1230 | +end; |
| 1231 | + |
| 1232 | +proc lab(n: INT): (INT, ERROR); |
| 1233 | +var i: INT; |
| 1234 | +begin |
| 1235 | + i := labelIndex(n); |
| 1236 | + if i >= 0 then return i, nil; end; |
| 1237 | + if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; |
| 1238 | + lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; |
| 1239 | + lusedAt[numLabels] := Scan.tokPos(); |
| 1240 | + INC(numLabels); |
| 1241 | + return numLabels - 1, nil; |
| 1242 | +end; |
| 1243 | + |
| 1244 | +(* Every label, entered before an instruction is read, as in X64 *) |
| 1245 | +proc findLabels; |
| 1246 | +var st: Scan.State; wasQuiet, start: BOOL; w: []const U8; |
| 1247 | +begin |
| 1248 | + Scan.saveScan(st); |
| 1249 | + wasQuiet := Diag.quiet; |
| 1250 | + Diag.setQuiet(true); |
| 1251 | + start := true; |
| 1252 | + while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do |
| 1253 | + w := ""; |
| 1254 | + if start then w := asmWord(); end; |
| 1255 | + start := Scan.tok = Scan.sSemicolon; |
| 1256 | + Scan.next; |
| 1257 | + if (LEN(w) > 0) and (Scan.tok = Scan.sColon) and (numLabels < 64) then |
| 1258 | + _, _ := lab(Scan.intern(SYSTEM.PTR(w), LEN(w))); |
| 1259 | + start := true; |
| 1260 | + Scan.next; |
| 1261 | + end; |
| 1262 | + end; |
| 1263 | + Diag.setQuiet(wasQuiet); |
| 1264 | + Scan.restoreScan(st); |
| 1265 | +end; |
| 1266 | + |
| 1267 | +(* An immediate, with or without its #: # is raz's "not equal" *) |
| 1268 | +proc immediate(constExpr: ConstFn): (I64, ERROR); |
| 1269 | +begin |
| 1270 | + if Scan.tok = Scan.sNeq then Scan.next; end; |
| 1271 | + return constExpr(true); |
| 1272 | +end; |
| 1273 | + |
| 1274 | +(* [xn], [xn, #off], [xn, xm], [xn, xm, lsl #s], then ! for pre-index, |
| 1275 | + or , #off after for post-index *) |
| 1276 | +proc memory(var o: Operand; constExpr: ConstFn): ERROR; |
| 1277 | +var b: Operand; st: Scan.State; |
| 1278 | +begin |
| 1279 | + Scan.next; |
| 1280 | + o.kind := oMem; o.index := -1; o.mode := mOff; |
| 1281 | + if not isReg(asmWord(), b) or (b.kind # oReg) or (b.sf = 0) or ((b.r = 31) and not b.sp) then |
| 1282 | + return Scan.mark("expected a memory operand, [xn, #offset] or [xn, xm]"); |
| 1283 | + end; |
| 1284 | + o.r := b.r; |
| 1285 | + Scan.next; |
| 1286 | + if Scan.tok = Scan.sComma then |
| 1287 | + Scan.next; |
| 1288 | + if isReg(asmWord(), b) then |
| 1289 | + if (b.kind # oReg) or (b.sf = 0) or (b.r = 31) then return Scan.mark("expected an index register, x0 to x30"); end; |
| 1290 | + o.index := b.r; |
| 1291 | + Scan.next; |
| 1292 | + if Scan.tok = Scan.sComma then |
| 1293 | + Scan.next; |
| 1294 | + if asmWord() # "lsl" then return Scan.mark("expected lsl"); end; |
| 1295 | + Scan.next; |
| 1296 | + o.scaled := true; |
| 1297 | + end; |
| 1298 | + end; |
| 1299 | + if (o.index < 0) or o.scaled then o.v := try immediate(constExpr); end; |
| 1300 | + end; |
| 1301 | + if Scan.tok # Scan.sRbrak then return Scan.mark("expected \"]\""); end; |
| 1302 | + Scan.next; |
| 1303 | + if Scan.tok = Scan.sNot then |
| 1304 | + o.mode := mPre; |
| 1305 | + Scan.next; |
| 1306 | + elsif (Scan.tok = Scan.sComma) and (o.v = 0) and (o.index < 0) then |
| 1307 | + Scan.saveScan(st); |
| 1308 | + Scan.next; |
| 1309 | + if (Scan.tok = Scan.sNeq) or (Scan.tok = Scan.sInt) or (Scan.tok = Scan.sMinus) then |
| 1310 | + o.mode := mPost; |
| 1311 | + o.v := try immediate(constExpr); |
| 1312 | + else |
| 1313 | + Scan.restoreScan(st); |
| 1314 | + end; |
| 1315 | + end; |
| 1316 | + return nil; |
| 1317 | +end; |
| 1318 | + |
| 1319 | +(* An operand: a register, a memory operand, a shift (lsl #12), an |
| 1320 | + immediate; a label, where target is set (a jump's last operand), |
| 1321 | + entered if new, which assemble finds undefined; a name, a procedure or |
| 1322 | + a variable, by nameFn; or, where word is set, a condition or a |
| 1323 | + barrier's option. *) |
| 1324 | +proc operand(var o: Operand; target, word: BOOL; constExpr: ConstFn; nameFn: NameFn): ERROR; |
| 1325 | +var w: []const U8; k: INT; |
| 1326 | +begin |
| 1327 | + o := {}; |
| 1328 | + w := asmWord(); |
| 1329 | + o.at := Scan.tokPos(); |
| 1330 | + if Scan.tok = Scan.sLbrak then |
| 1331 | + try memory(o, constExpr); |
| 1332 | + elsif isReg(w, o) then |
| 1333 | + Scan.next; |
| 1334 | + elsif wordIndex(w, ShiftNames) >= 0 then |
| 1335 | + o.kind := oShift; o.sh := wordIndex(w, ShiftNames); |
| 1336 | + Scan.next; |
| 1337 | + o.v := try immediate(constExpr); |
| 1338 | + elsif word and (Scan.tok = Scan.sIdent) then |
| 1339 | + o.kind := oWord; o.w := w; |
| 1340 | + Scan.next; |
| 1341 | + else |
| 1342 | + o.c := -1; |
| 1343 | + k := -1; |
| 1344 | + if Scan.tok = Scan.sIdent then |
| 1345 | + k := labelIndex(Scan.id); |
| 1346 | + if (k >= 0) and not target then return Scan.mark(Diag.say("label {} is only for a jump", [Fmt.str(w)])); end; |
| 1347 | + if k < 0 then o.c := try nameFn(); end; |
| 1348 | + end; |
| 1349 | + if (k >= 0) or target and (o.c < 0) and (Scan.tok = Scan.sIdent) then |
| 1350 | + o.kind := oLabel; |
| 1351 | + o.c := try lab(Scan.id); |
| 1352 | + Scan.next; |
| 1353 | + elsif o.c >= 0 then |
| 1354 | + o.kind := oName; |
| 1355 | + else |
| 1356 | + o.kind := oImm; |
| 1357 | + o.v := try immediate(constExpr); |
| 1358 | + end; |
| 1359 | + end; |
| 1360 | + return nil; |
| 1361 | +end; |
| 1362 | + |
| 1363 | +proc bad: ERROR; |
| 1364 | +begin |
| 1365 | + return Scan.markAt(aat, Diag.say("can't assemble {} with these operands", [Fmt.str(aw)])); |
| 1366 | +end; |
| 1367 | + |
| 1368 | +(* Whether the operands are kinds, one letter each: r a general |
| 1369 | + register, f a real's, i an immediate, m memory, s a shift, n a name, |
| 1370 | + l a label, w a word; and as many *) |
| 1371 | +proc are(kinds: []const U8): BOOL; |
| 1372 | +const Letters = "rfimsnlw"; |
| 1373 | +var i: INT; |
| 1374 | +begin |
| 1375 | + if an # LEN(kinds) then return false; end; |
| 1376 | + for i := 0 to an - 1 do |
| 1377 | + if Letters[INT(ao[i].kind)] # kinds[i] then return false; end; |
| 1378 | + end; |
| 1379 | + return true; |
| 1380 | +end; |
| 1381 | + |
| 1382 | +(* Whether the n operands from first are general registers of the |
| 1383 | + width of operand 0, none of them written sp *) |
| 1384 | +proc regs(first, n: INT): BOOL; |
| 1385 | +var i: INT; |
| 1386 | +begin |
| 1387 | + for i := first to first + n - 1 do |
| 1388 | + if (i >= an) or (ao[i].kind # oReg) or (ao[i].sf # ao[0].sf) or ao[i].sp then return false; end; |
| 1389 | + end; |
| 1390 | + return true; |
| 1391 | +end; |
| 1392 | + |
| 1393 | +proc fits(v, lo, hi: I64): BOOL; |
| 1394 | +begin |
| 1395 | + return (v >= lo) and (v <= hi); |
| 1396 | +end; |
| 1397 | + |
| 1398 | +(* The zero register, of operand 0's width, put in as operand i: mov is |
| 1399 | + orr of it, neg sub from it, cmp subs to it *) |
| 1400 | +proc insertZR(i: INT); |
| 1401 | +var j, sf: INT; |
| 1402 | +begin |
| 1403 | + sf := ao[0].sf; |
| 1404 | + for j := an to i + 1 by -1 do ao[j] := ao[j - 1]; end; |
| 1405 | + ao[i] := {}; ao[i].kind := oReg; ao[i].r := ZR; ao[i].sf := sf; |
| 1406 | + INC(an); |
| 1407 | +end; |
| 1408 | + |
| 1409 | +(* a branch to label k: back if it is placed, else on its chain *) |
| 1410 | +proc toLabel(insn, k: INT); |
| 1411 | +begin |
| 1412 | + if lplaces[k] >= 0 then |
| 1413 | + _ := branch(insn, lplaces[k], true); |
| 1414 | + else |
| 1415 | + lchains[k] := branch(insn, lchains[k], false); |
| 1416 | + end; |
| 1417 | +end; |
| 1418 | + |
| 1419 | +(* The shift that is operand i, if there is one, the last: its kind and |
| 1420 | + amount, or lsl 0; false if it isn't a shift within the width *) |
| 1421 | +proc shiftAt(i: INT; var sh, n: INT): BOOL; |
| 1422 | +begin |
| 1423 | + sh := shLsl; n := 0; |
| 1424 | + if an = i then return true; end; |
| 1425 | + 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; |
| 1426 | + sh := ao[i].sh; n := INT(ao[i].v); |
| 1427 | + return true; |
| 1428 | +end; |
| 1429 | + |
| 1430 | +(* add, adds, sub, subs, and, orr, eor, ands, bic and orn, op, of |
| 1431 | + operands 0, 1 and 2: of an immediate, 12 bits with lsl #12 or |
| 1432 | + negative for the opposite operation, or a logical immediate; or of a |
| 1433 | + register, shifted, or extended where sp is one *) |
| 1434 | +proc alu3(op: INT): ERROR; |
| 1435 | +var sh, n, enc: INT; v: I64; d: Operand; |
| 1436 | +begin |
| 1437 | + d := ao[0]; |
| 1438 | + 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; |
| 1439 | + if ao[2].kind = oImm then |
| 1440 | + v := ao[2].v; |
| 1441 | + if op > opSubs then |
| 1442 | + enc := bitmask(v, d.sf); |
| 1443 | + if (an # 3) or (op > opAnds) or (enc < 0) or (d.sf = 0) and not fits(v, -80000000H, 0FFFFFFFFH) |
| 1444 | + or (d.r = 31) and (d.sp = (op = opAnds)) then return bad(); end; |
| 1445 | + logicImm(d.sf, op, d.r, ao[1].r, enc, v); |
| 1446 | + return nil; |
| 1447 | + end; |
| 1448 | + if not shiftAt(3, sh, n) or (sh # shLsl) or (n # 0) and (n # 12) then return bad(); end; |
| 1449 | + if (v < 0) and (n = 0) then v := -v; op := op xor 2; end; |
| 1450 | + 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; |
| 1451 | + addImm(d.sf, op, d.r, ao[1].r, INT(v), n div 12); |
| 1452 | + return nil; |
| 1453 | + end; |
| 1454 | + if not regs(2, 1) or not shiftAt(3, sh, n) then return bad(); end; |
| 1455 | + if (op <= opSubs) and (d.sp or ao[1].sp) then |
| 1456 | + if (sh # shLsl) or (n > 4) or (d.r = 31) and (d.sp = ((op & 1) = 1)) then return bad(); end; |
| 1457 | + addExt(d.sf, op, d.r, ao[1].r, ao[2].r, n); |
| 1458 | + elsif d.sp or (sh = shRor) and (op <= opSubs) then |
| 1459 | + return bad(); |
| 1460 | + else |
| 1461 | + aluShifted(d.sf, op, d.r, ao[1].r, ao[2].r, sh, n); |
| 1462 | + end; |
| 1463 | + return nil; |
| 1464 | +end; |
| 1465 | + |
| 1466 | +(* ldr, str and their kin: the size from the name's end (b, h, sb, sh, |
| 1467 | + sw) or from the register; ldur and stur, and an offset that can't |
| 1468 | + be scaled, are unscaled *) |
| 1469 | +proc loadStore(): ERROR; |
| 1470 | +var name, sfx: []const U8; size, sf, bits, sc: INT; load, signed, fp, unscaled: BOOL; m: Operand; |
| 1471 | +begin |
| 1472 | + unscaled := aw[2] = 'u'; |
| 1473 | + name := aw; |
| 1474 | + if unscaled then name := Diag.say("{}{}", [Fmt.str(aw[0..2]), Fmt.str(aw[3..LEN(aw)])]); end; |
| 1475 | + load := name[0] = 'l'; |
| 1476 | + sfx := name[3..LEN(name)]; |
| 1477 | + if not are("rm") and not are("fm") then return bad(); end; |
| 1478 | + m := ao[1]; |
| 1479 | + fp := ao[0].kind = oFp; |
| 1480 | + sf := ao[0].sf; |
| 1481 | + signed := (LEN(sfx) = 2) and (sfx[0] = 's'); |
| 1482 | + if fp and (LEN(sfx) = 0) then size := ao[0].size; |
| 1483 | + elsif sfx = "" then size := 4 + 4 * sf; |
| 1484 | + elsif (sfx = "b") or (sfx = "sb") then size := 1; |
| 1485 | + elsif (sfx = "h") or (sfx = "sh") then size := 2; |
| 1486 | + elsif sfx = "sw" then size := 4; |
| 1487 | + else return bad(); |
| 1488 | + end; |
| 1489 | + 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; |
| 1490 | + bits := ldstBits(size, load, signed, fp, sf); |
| 1491 | + sc := scaleOf(size); |
| 1492 | + if (m.mode # mOff) or unscaled then |
| 1493 | + if (m.index >= 0) or (m.mode # mOff) and unscaled or not fits(m.v, -256, 255) then return bad(); end; |
| 1494 | + if m.mode # mOff then ldstIdx(bits, ao[0].r, m.r, INT(m.v), m.mode); |
| 1495 | + else emit(38000000H | bits | (INT(m.v & 1FFH) shl 12) | (m.r shl 5) | ao[0].r); end; |
| 1496 | + elsif m.index >= 0 then |
| 1497 | + if m.scaled and (m.v # sc) and (m.v # 0) then return bad(); end; |
| 1498 | + ldstReg(bits, ao[0].r, m.r, m.index, m.scaled and (m.v = sc)); |
| 1499 | + elsif fits(m.v, 0, 4095 shl sc) and ((m.v & ((1 shl sc) - 1)) = 0) or fits(m.v, -256, 255) then |
| 1500 | + ldst(bits, ao[0].r, m.r, INT(m.v)); |
| 1501 | + else |
| 1502 | + return bad(); |
| 1503 | + end; |
| 1504 | + return nil; |
| 1505 | +end; |
| 1506 | + |
| 1507 | +const |
| 1508 | + (* the aliases of alu3's operations with the zero register put in: |
| 1509 | + cmp and cmn as subs and adds to it, tst ands to it, neg sub from |
| 1510 | + it, mvn orn of it *) |
| 1511 | + Aliases: [5][]const U8 = ["cmp", "cmn", "tst", "neg", "mvn"]; |
| 1512 | + AliasOps: [5]INT = [opSubs, opAdds, opAnds, opSub, opOrn]; |
| 1513 | + (* the instructions of only registers, regOp's: their bits and how |
| 1514 | + many registers *) |
| 1515 | + RegNames: [14][]const U8 = ["mul", "madd", "msub", "smulh", "umulh", "sdiv", "udiv", |
| 1516 | + "lsl", "lsr", "asr", "ror", "clz", "rbit", "rev"]; |
| 1517 | + RegBits: [14]INT = [1B007C00H, 1B000000H, 1B008000H, 9B407C00H, 9BC07C00H, 1AC00C00H, 1AC00800H, |
| 1518 | + 1AC02000H, 1AC02400H, 1AC02800H, 1AC02C00H, 5AC01000H, 5AC00000H, 5AC00800H]; |
| 1519 | + RegCount: [14]INT = [3, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2]; |
| 1520 | + Extends: [5][]const U8 = ["sxtb", "sxth", "sxtw", "uxtb", "uxth"]; |
| 1521 | + Barriers: [6][]const U8 = ["sy", "ish", "ishld", "ishst", "osh", "nsh"]; |
| 1522 | + BarrierCodes: [6]INT = [15, 11, 9, 10, 3, 7]; |
| 1523 | + (* the instructions of no operands *) |
| 1524 | + Plain: [4][]const U8 = ["nop", "yield", "isb", "ret"]; |
| 1525 | + PlainBits: [4]INT = [0D503201FH, 0D503203FH, 0D5033FDFH, 0D65F03C0H]; |
| 1526 | + (* between the register files, as fconv takes them: rmode * 8 + |
| 1527 | + opcode; the first two into a real's register *) |
| 1528 | + Convs: [4][]const U8 = ["scvtf", "ucvtf", "fcvtzs", "fcvtzu"]; |
| 1529 | + ConvCodes: [4]INT = [02H, 03H, 18H, 19H]; |
| 1530 | + |
| 1531 | +(* The instruction aw with its operands, in ao and an *) |
| 1532 | +proc instruction(): ERROR; |
| 1533 | +var k, sf, size, bits, g, f: INT; d: Operand; v: I64; |
| 1534 | +begin |
| 1535 | + d := ao[0]; |
| 1536 | + sf := d.sf; |
| 1537 | + if wordIndex(aw, Aliases) >= 0 then |
| 1538 | + insertZR(bit(wordIndex(aw, Aliases) >= 3)); |
| 1539 | + return alu3(AliasOps[wordIndex(aw, Aliases)]); |
| 1540 | + elsif wordIndex(aw, AluNames) >= 0 then |
| 1541 | + return alu3(wordIndex(aw, AluNames)); |
| 1542 | + elsif aw = "mov" then |
| 1543 | + (* movImm's choice for an immediate; add #0 with sp, else orr *) |
| 1544 | + if are("ri") and not d.sp and ((sf = 1) or fits(ao[1].v, -80000000H, 0FFFFFFFFH)) then |
| 1545 | + movImm(sf, d.r, ao[1].v); |
| 1546 | + return nil; |
| 1547 | + elsif are("rr") and (d.sp or ao[1].sp) then |
| 1548 | + ao[2] := {}; ao[2].kind := oImm; an := 3; |
| 1549 | + return alu3(opAdd); |
| 1550 | + end; |
| 1551 | + insertZR(1); |
| 1552 | + return alu3(opOrr); |
| 1553 | + elsif (aw = "movz") or (aw = "movk") or (aw = "movn") then |
| 1554 | + k := 0; |
| 1555 | + 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 |
| 1556 | + k := INT(ao[2].v) div 16; |
| 1557 | + DEC(an); |
| 1558 | + end; |
| 1559 | + if not are("ri") or d.sp or not fits(ao[1].v, 0, 0FFFFH) then return bad(); end; |
| 1560 | + movWide(sf, wordIndex(aw, ["movn", "", "movz", "movk"]), d.r, INT(ao[1].v), k); |
| 1561 | + elsif (wordIndex(aw, ShiftNames) >= 0) and are("rri") then |
| 1562 | + if not regs(1, 1) or not fits(ao[2].v, 0, 31 + 32 * sf) then return bad(); end; |
| 1563 | + shiftI(wordIndex(aw, ShiftNames), d.r, ao[1].r, INT(ao[2].v), 4 + 4 * sf); |
| 1564 | + elsif wordIndex(aw, RegNames) >= 0 then |
| 1565 | + (* smulh and umulh are of x only; rev's opc is 11 for x, 10 for w *) |
| 1566 | + k := wordIndex(aw, RegNames); |
| 1567 | + if (an # RegCount[k]) or not regs(0, an) or ((k = 3) or (k = 4)) and (sf = 0) then return bad(); end; |
| 1568 | + bits := RegBits[k]; |
| 1569 | + if aw = "rev" then bits := bits | (sf shl 10); end; |
| 1570 | + while an < 4 do ao[an].r := 0; INC(an); end; |
| 1571 | + regOp(aw, RegCount[k], sf, bits & 7FFFFFFFH, d.r, ao[1].r, ao[2].r, ao[3].r); |
| 1572 | + elsif wordIndex(aw, CselNames) >= 0 then |
| 1573 | + if (an # 4) or not regs(0, 3) or (ao[3].kind # oWord) or (cond(ao[3].w) < 0) then return bad(); end; |
| 1574 | + condSel(sf, wordIndex(aw, CselNames), cond(ao[3].w), d.r, ao[1].r, ao[2].r); |
| 1575 | + elsif (aw = "cset") or (aw = "csetm") then |
| 1576 | + (* csinc and csinv of the zero register on the opposite condition *) |
| 1577 | + if (an # 2) or not regs(0, 1) or (ao[1].kind # oWord) or (cond(ao[1].w) < 0) then return bad(); end; |
| 1578 | + condSel(sf, csInc + bit(aw = "csetm"), invert(cond(ao[1].w)), d.r, ZR, ZR); |
| 1579 | + elsif wordIndex(aw, Extends) >= 0 then |
| 1580 | + k := wordIndex(aw, Extends); |
| 1581 | + if (an # 2) or (d.kind # oReg) or d.sp or (ao[1].kind # oReg) or (ao[1].sf # 0) or ao[1].sp |
| 1582 | + or (k >= 3) and (sf = 1) or (k = 2) and (sf = 0) then return bad(); end; |
| 1583 | + bitfield(sf, 2 * bit(k >= 3), d.r, ao[1].r, 0, 8 * (1 shl (k mod 3)) - 1); |
| 1584 | + elsif (aw = "ubfm") or (aw = "sbfm") or (aw = "ubfx") or (aw = "sbfx") then |
| 1585 | + if (an # 4) or not regs(0, 2) or (ao[2].kind # oImm) or (ao[3].kind # oImm) then return bad(); end; |
| 1586 | + v := ao[3].v; |
| 1587 | + if aw[3] = 'x' then v := ao[2].v + v - 1; end; (* ubfx's width, as ubfm's last bit *) |
| 1588 | + if not fits(ao[2].v, 0, 31 + 32 * sf) or not fits(v, 0, 31 + 32 * sf) then return bad(); end; |
| 1589 | + bitfield(sf, 2 * bit(aw[0] = 'u'), d.r, ao[1].r, INT(ao[2].v), INT(v)); |
| 1590 | + elsif Str.hasPrefix(aw, "ldr") or Str.hasPrefix(aw, "str") or Str.hasPrefix(aw, "ldur") or Str.hasPrefix(aw, "stur") then |
| 1591 | + return loadStore(); |
| 1592 | + elsif (aw = "ldp") or (aw = "stp") then |
| 1593 | + if not are("rrm") and not are("ffm") then return bad(); end; |
| 1594 | + size := 4 + 4 * sf; |
| 1595 | + if d.kind = oFp then size := d.size; end; |
| 1596 | + if (ao[1].sf # sf) or (ao[1].size # d.size) or d.sp or ao[1].sp or (ao[2].index >= 0) |
| 1597 | + or ((ao[2].v & (size - 1)) # 0) or not fits(ao[2].v, -64 * size, 63 * size) then return bad(); end; |
| 1598 | + pair(aw[0] = 'l', d.kind = oFp, size, d.r, ao[1].r, ao[2].r, INT(ao[2].v), ao[2].mode); |
| 1599 | + elsif wordIndex(aw, AtomNames) >= 0 then |
| 1600 | + (* ldaxr has no rs, which is 31 in its bits; stlxr's is a w *) |
| 1601 | + k := wordIndex(aw, AtomNames); |
| 1602 | + if aw = "ldaxr" then insertZR(0); end; |
| 1603 | + 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) |
| 1604 | + or (k < 3) and (ao[1].sf # sf) or (k = 4) and (sf = 1) then return bad(); end; |
| 1605 | + atomic(k, 4 + 4 * ao[1].sf, ao[0].r, ao[1].r, ao[2].r); |
| 1606 | + elsif aw = "adr" then |
| 1607 | + if not are("rn") or (sf = 0) or d.sp then return bad(); end; |
| 1608 | + addrChunk(d.r, ao[1].c, 0); |
| 1609 | + elsif ((aw = "b") or (aw = "bl")) and are("n") then |
| 1610 | + branchChunk(aw = "bl", d.c); |
| 1611 | + elsif (aw = "b") and are("l") then |
| 1612 | + toLabel(14000000H, d.c); |
| 1613 | + elsif (aw = "b.") and are("l") then |
| 1614 | + toLabel(54000000H | acc, d.c); |
| 1615 | + elsif ((aw = "cbz") or (aw = "cbnz")) and are("rl") and not d.sp then |
| 1616 | + toLabel((sf shl 31) | 34000000H | (bit(aw = "cbnz") shl 24) | d.r, ao[1].c); |
| 1617 | + elsif ((aw = "tbz") or (aw = "tbnz")) and are("ril") and not d.sp and fits(ao[1].v, 0, 31 + 32 * sf) then |
| 1618 | + k := INT(ao[1].v); |
| 1619 | + toLabel(36000000H | (bit(aw = "tbnz") shl 24) | ((k shr 5) shl 31) | ((k & 31) shl 19) | d.r, ao[2].c); |
| 1620 | + elsif (aw = "br") or (aw = "blr") or (aw = "ret") and (an = 1) then |
| 1621 | + (* opc in bits 21 and 22: 00 br, 01 blr, 10 ret *) |
| 1622 | + if not are("r") or (sf = 0) or d.sp then return bad(); end; |
| 1623 | + emit(0D61F0000H | (wordIndex(aw, ["br", "blr", "ret"]) shl 21) | (d.r shl 5)); |
| 1624 | + elsif (aw = "svc") or (aw = "brk") then |
| 1625 | + if not are("i") or not fits(d.v, 0, 0FFFFH) then return bad(); end; |
| 1626 | + if aw = "svc" then svc(INT(d.v)); else brk(INT(d.v)); end; |
| 1627 | + elsif (aw = "dmb") or (aw = "dsb") then |
| 1628 | + if not are("w") or (wordIndex(d.w, Barriers) < 0) then return bad(); end; |
| 1629 | + emit(0D503309FH | (bit(aw = "dmb") shl 5) | (BarrierCodes[wordIndex(d.w, Barriers)] shl 8)); |
| 1630 | + elsif wordIndex(aw, Plain) >= 0 then |
| 1631 | + if an # 0 then return bad(); end; |
| 1632 | + emit(PlainBits[wordIndex(aw, Plain)]); |
| 1633 | + elsif wordIndex(aw, FopNames) >= 0 then |
| 1634 | + if not are("fff") or (d.size = 16) or (ao[1].size # d.size) or (ao[2].size # d.size) then return bad(); end; |
| 1635 | + fop(wordIndex(aw, FopNames), d.r, ao[1].r, ao[2].r, d.size = 4); |
| 1636 | + elsif (wordIndex(aw, Fop1Names) >= 0) and are("ff") and (d.size # 16) and (ao[1].size = d.size) then |
| 1637 | + fop1(wordIndex(aw, Fop1Names), d.r, ao[1].r, d.size = 4); |
| 1638 | + elsif (aw = "fmov") and are("fr") and (d.size = 4 + 4 * ao[1].sf) and not ao[1].sp then |
| 1639 | + fmovFR(d.r, ao[1].r, d.size = 4); |
| 1640 | + elsif (aw = "fmov") and are("rf") and (ao[1].size = 4 + 4 * sf) and not d.sp then |
| 1641 | + fmovRF(d.r, ao[1].r, ao[1].size = 4); |
| 1642 | + elsif (aw = "fcmp") and are("ff") and (ao[1].size = d.size) and (d.size # 16) then |
| 1643 | + fcmp(d.r, ao[1].r, d.size = 4); |
| 1644 | + elsif (aw = "fcmp") and are("fi") and (ao[1].v = 0) and (d.size # 16) then |
| 1645 | + fcmp(d.r, -1, d.size = 4); |
| 1646 | + elsif (aw = "fcvt") and are("ff") and (d.size + ao[1].size = 12) then |
| 1647 | + fcvt(d.r, ao[1].r, d.size = 4); |
| 1648 | + elsif wordIndex(aw, Convs) >= 0 then |
| 1649 | + (* scvtf and ucvtf from operand g, a general register, into f, a |
| 1650 | + real's; fcvtzs and fcvtzu the other way *) |
| 1651 | + k := wordIndex(aw, Convs); |
| 1652 | + g := bit(k < 2); f := 1 - g; |
| 1653 | + 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; |
| 1654 | + fconv(aw, ao[g].sf, ao[f].size, ConvCodes[k] shr 3, ConvCodes[k] & 7, d.r, ao[1].r, k < 2); |
| 1655 | + else |
| 1656 | + return Scan.markAt(aat, Diag.say("unknown instruction {}", [Fmt.str(aw)])); |
| 1657 | + end; |
| 1658 | + return nil; |
| 1659 | +end; |
| 1660 | + |
| 1661 | +(* Which operand of instruction w is a jump's target, which may be a |
| 1662 | + label: -1 if none *) |
| 1663 | +proc targetOf(w: []const U8): INT; |
| 1664 | +begin |
| 1665 | + if (w = "b") or (w = "bl") or (w = "b.") then return 0; end; |
| 1666 | + if (w = "cbz") or (w = "cbnz") then return 1; end; |
| 1667 | + if (w = "tbz") or (w = "tbnz") then return 2; end; |
| 1668 | + return -1; |
| 1669 | +end; |
| 1670 | + |
| 1671 | +(* Whether operand i of instruction w is a word: a condition or a |
| 1672 | + barrier's option *) |
| 1673 | +proc wordAt(w: []const U8; i: INT): BOOL; |
| 1674 | +begin |
| 1675 | + return (wordIndex(w, CselNames) >= 0) and (i = 3) or ((w = "cset") or (w = "csetm")) and (i = 1) |
| 1676 | + or ((w = "dmb") or (w = "dsb")) and (i = 0); |
| 1677 | +end; |
| 1678 | + |
| 1679 | +(* db's bytes or dd's words, each a constant *) |
| 1680 | +proc data(constExpr: ConstFn): ERROR; |
| 1681 | +var v: I64; start: Diag.Pos; n: INT; |
| 1682 | +begin |
| 1683 | + n := 1 + 3 * bit(aw = "dd"); |
| 1684 | + repeat |
| 1685 | + start := Scan.tokPos(); |
| 1686 | + v := try constExpr(true); |
| 1687 | + if (v < -(I64(1) shl (8 * n - 1))) or (v >= I64(1) shl (8 * n)) then |
| 1688 | + return Scan.markAt(Scan.spanFrom(start), Diag.say("{} doesn't fit in {} bits", [Fmt.int(v), Fmt.int(8 * n)])); |
| 1689 | + end; |
| 1690 | + if n = 1 then Chunk.put1(INT(v & 0FFH)); else Chunk.put4(INT(v & 0FFFFFFFFH)); end; |
| 1691 | + if Scan.tok # Scan.sComma then break; end; |
| 1692 | + Scan.next; |
| 1693 | + until false; |
| 1694 | + return nil; |
| 1695 | +end; |
| 1696 | + |
| 1697 | +(* One instruction or label *) |
| 1698 | +proc statement(constExpr: ConstFn; nameFn: NameFn): ERROR; |
| 1699 | +var i, k, first: INT; wasListing: BOOL; |
| 1700 | +begin |
| 1701 | + aat := Scan.tokPos(); |
| 1702 | + aw := asmWord(); |
| 1703 | + if LEN(aw) = 0 then return Scan.mark("expected an instruction"); end; |
| 1704 | + Scan.next; |
| 1705 | + if Scan.tok = Scan.sColon then |
| 1706 | + sayAt(Chunk.pc, Diag.say("{}:", [Fmt.str(aw)])); |
| 1707 | + k := try lab(Scan.intern(SYSTEM.PTR(aw), LEN(aw))); |
| 1708 | + if lplaces[k] >= 0 then |
| 1709 | + return Scan.markAt(aat, Diag.say("label {} is defined twice", [Fmt.str(aw)])); |
| 1710 | + end; |
| 1711 | + lplaces[k] := Chunk.pc; |
| 1712 | + fixLink(lchains[k]); |
| 1713 | + Scan.next; |
| 1714 | + return nil; |
| 1715 | + end; |
| 1716 | + if (aw = "b") and (Scan.tok = Scan.sPeriod) then |
| 1717 | + (* b.cond: the condition, after the period *) |
| 1718 | + Scan.next; |
| 1719 | + acc := cond(asmWord()); |
| 1720 | + if acc < 0 then return Scan.mark("expected a condition, as eq of b.eq"); end; |
| 1721 | + aw := "b."; |
| 1722 | + Scan.next; |
| 1723 | + end; |
| 1724 | + first := Chunk.pc; |
| 1725 | + wasListing := listing; |
| 1726 | + listing := false; (* the instruction is said as it is written *) |
| 1727 | + if (aw = "db") or (aw = "dd") then |
| 1728 | + try data(constExpr); |
| 1729 | + else |
| 1730 | + an := 0; |
| 1731 | + if Scan.tok # Scan.sSemicolon then |
| 1732 | + repeat |
| 1733 | + if an > 3 then return Scan.mark("too many operands"); end; |
| 1734 | + try operand(ao[an], an = targetOf(aw), wordAt(aw, an), constExpr, nameFn); |
| 1735 | + INC(an); |
| 1736 | + if Scan.tok # Scan.sComma then break; end; |
| 1737 | + Scan.next; |
| 1738 | + until false; |
| 1739 | + end; |
| 1740 | + (* a name stands only in bl p, b p and adr r, v *) |
| 1741 | + for i := 0 to an - 1 do |
| 1742 | + if (ao[i].kind = oName) and not ((i = 0) and ((aw = "b") or (aw = "bl")) or (i = 1) and (aw = "adr")) then |
| 1743 | + return Scan.markAt(ao[i].at, NameOnly); |
| 1744 | + end; |
| 1745 | + end; |
| 1746 | + try instruction(); |
| 1747 | + end; |
| 1748 | + if Scan.tok # Scan.sSemicolon then return Scan.mark("expected \";\""); end; |
| 1749 | + listing := wasListing; |
| 1750 | + sayAt(first, Diag.source(Scan.spanFrom(aat))); |
| 1751 | + Scan.next; |
| 1752 | + return nil; |
| 1753 | +end; |
| 1754 | + |
| 1755 | +(* A code procedure's instructions, up to end. ! of a pre-index is a |
| 1756 | + symbol of its own meanwhile (Scan.setBang), where elsewhere it is a |
| 1757 | + slip for not. *) |
| 1758 | +proc assemble*(constExpr: ConstFn; nameFn: NameFn): ERROR; |
| 1759 | +var k: INT; |
| 1760 | +begin |
| 1761 | + Scan.setBang(true); |
| 1762 | + defer Scan.setBang(false); |
| 1763 | + numLabels := 0; |
| 1764 | + findLabels(); |
| 1765 | + while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do |
| 1766 | + if Scan.tok = Scan.sSemicolon then |
| 1767 | + Scan.next; |
| 1768 | + else |
| 1769 | + try statement(constExpr, nameFn); |
| 1770 | + end; |
| 1771 | + end; |
| 1772 | + for k := 0 to numLabels - 1 do |
| 1773 | + if lplaces[k] < 0 then |
| 1774 | + return Scan.markAt(lusedAt[k], Diag.say("label {} isn't defined", [Fmt.str(Scan.nameOf(lnames[k]))])); |
| 1775 | + end; |
| 1776 | + end; |
| 1777 | + return nil; |
| 1778 | +end; |
| 1779 | + |
| 1780 | +(* n, in decimal, at least w wide *) |
| 1781 | +proc padded(n, w: INT); |
| 1782 | +var s: []const U8; i: INT; |
| 1783 | +begin |
| 1784 | + s := Diag.say("{}", [Fmt.int(n)]); |
| 1785 | + for i := LEN(s) to w - 1 do Out.str(" "); end; |
| 1786 | + Out.str(s); |
| 1787 | +end; |
| 1788 | + |
| 1789 | +(* The listing, raz -S, as X64's: a jump within the procedure, said with |
| 1790 | + -> for its target, says where to by its offset, read from its field *) |
| 1791 | +proc list*(home: INT); |
| 1792 | +var order: ^INT; n, i, j, k, c, t, line, insn, lo, w, d: INT; text: []const U8; |
| 1793 | +begin |
| 1794 | + order := ^INT(Scan.grab(Chunk.numChunks * SIZE(INT))); |
| 1795 | + n := 0; |
| 1796 | + for c := 0 to Chunk.numChunks - 1 do |
| 1797 | + if Chunk.chunks[c].live and (Chunk.chunks[c].sec = Chunk.secText) and (Chunk.chunks[c].home = home) then |
| 1798 | + i := n; |
| 1799 | + while (i > 0) and (Chunk.chunks[order[i - 1]].start > Chunk.chunks[c].start) do |
| 1800 | + order[i] := order[i - 1]; |
| 1801 | + DEC(i); |
| 1802 | + end; |
| 1803 | + order[i] := c; |
| 1804 | + INC(n); |
| 1805 | + end; |
| 1806 | + end; |
| 1807 | + i := 0; j := 0; |
| 1808 | + for k := 0 to n - 1 do |
| 1809 | + c := order[k]; |
| 1810 | + Out.ln; |
| 1811 | + Out.str(Chunk.chunkName(c)); Out.str(":\n"); |
| 1812 | + line := -1; |
| 1813 | + while (i < numSaid) and (said[i].at < Chunk.chunks[c].start) do INC(i); end; |
| 1814 | + while (i < numSaid) and (said[i].at < Chunk.chunks[c].start + Chunk.chunks[c].size) do |
| 1815 | + while (j < Chunk.numLines) and (Chunk.lines[j].at <= said[i].at) do |
| 1816 | + if (Chunk.lines[j].chunk = c) and (Chunk.lines[j].line # line) then |
| 1817 | + line := Chunk.lines[j].line; |
| 1818 | + padded(line, 6); |
| 1819 | + Out.str(" "); |
| 1820 | + Out.str(Diag.lineText(Scan.nameOf(Chunk.lines[j].path), line)); |
| 1821 | + Out.ln; |
| 1822 | + end; |
| 1823 | + INC(j); |
| 1824 | + end; |
| 1825 | + text := said[i].text; |
| 1826 | + t := said[i].at; |
| 1827 | + if (LEN(text) > 2) and (text[LEN(text) - 2..LEN(text)] = "->") then |
| 1828 | + insn := Chunk.get4At(t) & 0FFFFFFFFH; |
| 1829 | + lo, w := fieldOf(insn); |
| 1830 | + d := (insn shr lo) & ((1 shl w) - 1); |
| 1831 | + if d >= (1 shl (w - 1)) then d := d - (1 shl w); end; |
| 1832 | + text := Diag.say("{}{}", [Fmt.str(text[0..LEN(text) - 2]), Fmt.int(t + 4 * d - Chunk.chunks[c].start)]); |
| 1833 | + end; |
| 1834 | + padded(t - Chunk.chunks[c].start, 14); |
| 1835 | + Out.str(" "); |
| 1836 | + Out.str(text); |
| 1837 | + Out.ln; |
| 1838 | + INC(i); |
| 1839 | + end; |
| 1840 | + end; |
| 1841 | +end; |
| 1842 | + |
| 1843 | +begin |
| 1844 | + listing := false; |
| 1845 | + numSaid := 0; |
| 1846 | + saidCap := 0; |
| 1847 | + said := nil; |
| 1848 | +end A64. |