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