| 1 | (* X64: machine code for x86-64. |
| 2 | |
| 3 | The encoder writes the instructions the code generator needs, and |
| 4 | no others, into the text of Chunk. Registers are numbered as the |
| 5 | hardware numbers them. Jumps within a procedure are patched here, as |
| 6 | soon as their target is known. |
| 7 | |
| 8 | Depends on: Scan, for errors and the assembler's symbols; Diag, for |
| 9 | positions; Chunk for the code buffer and fixups. |
| 10 | Leaves out: choosing instructions; that is Gen. |
| 11 | |
| 12 | The seed's `Mem` that is too large an offset is marked and the code |
| 13 | goes on, since a function that makes an operand has no place to give |
| 14 | an error back from; the compile ends with the error shown. *) |
| 15 | module X64; |
| 16 | import Out, Fmt, Heap, Buf, Str, Diag, Scan, Chunk; |
| 17 | import SYSTEM; |
| 18 | |
| 19 | const |
| 20 | RAX* = 0; RCX* = 1; RDX* = 2; RBX* = 3; RSP* = 4; RBP* = 5; RSI* = 6; RDI* = 7; |
| 21 | R8* = 8; R9* = 9; R10* = 10; R11* = 11; R12* = 12; R13* = 13; R14* = 14; R15* = 15; |
| 22 | RIP* = -1; (* a base meaning rip-relative, to a chunk *) |
| 23 | NoIndex* = -1; |
| 24 | |
| 25 | (* condition codes *) |
| 26 | ccB* = 2; ccAE* = 3; ccE* = 4; ccNE* = 5; ccBE* = 6; ccA* = 7; ccS* = 8; |
| 27 | ccP* = 10; ccNP* = 11; ccL* = 12; ccGE* = 13; ccLE* = 14; ccG* = 15; |
| 28 | |
| 29 | (* group 1 operations, the /digit of 81 and 83 *) |
| 30 | aluAdd* = 0; aluOr* = 1; aluAnd* = 4; aluSub* = 5; aluXor* = 6; aluCmp* = 7; |
| 31 | |
| 32 | (* the /digit of D3 and C1, and of D2 and C0 for a byte *) |
| 33 | shRol* = 0; shRor* = 1; shShl* = 4; shShr* = 5; shSar* = 7; |
| 34 | |
| 35 | NoLabel* = -1; |
| 36 | |
| 37 | type |
| 38 | Mem* = record |
| 39 | base*: INT; (* a register, or RIP; RIP with an index is the |
| 40 | chunk's address itself, as below (memAbs) *) |
| 41 | index*: INT; (* a register, or NoIndex *) |
| 42 | scale*: INT; (* 1, 2, 4 or 8 *) |
| 43 | disp*: INT; |
| 44 | chunk*: INT; (* with RIP: the chunk addressed *) |
| 45 | end; |
| 46 | |
| 47 | (* The parser's readers of a constant and of a name (Scan says) *) |
| 48 | ConstFn* = Scan.ConstFn; |
| 49 | NameFn* = Scan.NameFn; |
| 50 | |
| 51 | (* The text of the instruction that starts at at, for the listing *) |
| 52 | Said = record |
| 53 | at: INT; |
| 54 | text: []const U8; |
| 55 | end; |
| 56 | |
| 57 | var |
| 58 | listing*: BOOL; (* -S: each instruction's text is kept, for list *) |
| 59 | said: ^Said; |
| 60 | numSaid, saidCap: INT; |
| 61 | labelAt: INT; (* where a jump was last fixed to the place it was at *) |
| 62 | |
| 63 | proc setListing*(yes: BOOL); |
| 64 | begin |
| 65 | listing := yes; |
| 66 | end; |
| 67 | |
| 68 | (* Operands *) |
| 69 | |
| 70 | proc mem*(base: INT; disp: I64): Mem; |
| 71 | var m: Mem; |
| 72 | begin |
| 73 | if (disp < -2147483648) or (disp > 2147483647) then |
| 74 | _ := Scan.mark("an offset beyond 32 bits"); |
| 75 | end; |
| 76 | m.base := base; m.index := NoIndex; m.scale := 1; |
| 77 | m.disp := INT(disp); m.chunk := -1; |
| 78 | return m; |
| 79 | end; |
| 80 | |
| 81 | proc memRip*(chunk: INT; disp: INT): Mem; |
| 82 | var m: Mem; |
| 83 | begin |
| 84 | m.base := RIP; m.index := NoIndex; m.scale := 1; |
| 85 | m.disp := disp; m.chunk := chunk; |
| 86 | return m; |
| 87 | end; |
| 88 | |
| 89 | (* An element of the array in chunk, [chunk + index * scale + disp]. x86 |
| 90 | has no rip + index, but the executable is loaded where the linker put |
| 91 | it, below 2 GB, so the chunk's address fits the 32 bits of a |
| 92 | displacement with no base: one instruction, where rip-relative needs a |
| 93 | lea of the chunk first. *) |
| 94 | proc memAbs*(chunk, index, scale, disp: INT): Mem; |
| 95 | var m: Mem; |
| 96 | begin |
| 97 | m := memRip(chunk, disp); |
| 98 | m.index := index; m.scale := scale; |
| 99 | return m; |
| 100 | end; |
| 101 | |
| 102 | proc memIdx*(base, index, scale: INT; disp: I64): Mem; |
| 103 | var m: Mem; |
| 104 | begin |
| 105 | m := mem(base, disp); |
| 106 | m.index := index; |
| 107 | m.scale := scale; |
| 108 | return m; |
| 109 | end; |
| 110 | |
| 111 | (* The address of the code about to be written, as an operand *) |
| 112 | proc here*: Mem; |
| 113 | begin |
| 114 | return memRip(Chunk.curChunk, Chunk.pc - Chunk.chunks[Chunk.curChunk].start); |
| 115 | end; |
| 116 | |
| 117 | (* Encoding. An instruction is: a legacy prefix, REX, the opcode, ModRM, |
| 118 | SIB and displacement. Opcodes of more than one byte are given with |
| 119 | their first byte in the low eight bits; one ends at its last non-zero |
| 120 | byte, but for the three-byte ones (opcode below). *) |
| 121 | |
| 122 | proc opcode(op: INT); |
| 123 | begin |
| 124 | (* 0F 38 and 0F 3A begin a three-byte opcode, whose last byte may be 00 *) |
| 125 | if (op & 0FDFFH) = 380FH then |
| 126 | Chunk.put1(op & 0FFH); Chunk.put1((op shr 8) & 0FFH); Chunk.put1((op shr 16) & 0FFH); |
| 127 | return; |
| 128 | end; |
| 129 | repeat |
| 130 | Chunk.put1(op & 0FFH); |
| 131 | op := op shr 8; |
| 132 | until op = 0; |
| 133 | end; |
| 134 | |
| 135 | (* The legacy prefix pfx, if any, and REX: wide is REX.W; byte4 says |
| 136 | a byte register of 4 to 7 is used, spl to dil, which exist only with |
| 137 | a REX prefix. *) |
| 138 | proc prefix(pfx: INT; w: BOOL; reg, index, base: INT; byte4: BOOL); |
| 139 | var rex: INT; |
| 140 | begin |
| 141 | if pfx # 0 then Chunk.put1(pfx); end; |
| 142 | rex := 0; |
| 143 | if w then rex := rex | 8; end; |
| 144 | if reg >= 8 then rex := rex | 4; end; |
| 145 | if index >= 8 then rex := rex | 2; end; |
| 146 | if base >= 8 then rex := rex | 1; end; |
| 147 | if (rex # 0) or byte4 then Chunk.put1(40H | rex); end; |
| 148 | end; |
| 149 | |
| 150 | (* op reg, [mem] *) |
| 151 | proc opM(pfx: INT; w: BOOL; op, reg: INT; m: Mem; byteReg: BOOL); |
| 152 | var md, b, sc: INT; |
| 153 | begin |
| 154 | byteReg := byteReg and (reg >= 4) and (reg <= 7); |
| 155 | if (m.base = RIP) and (m.index = NoIndex) then |
| 156 | prefix(pfx, w, reg, NoIndex, 0, byteReg); |
| 157 | else |
| 158 | prefix(pfx, w, reg, m.index, m.base, byteReg); |
| 159 | end; |
| 160 | opcode(op); |
| 161 | reg := reg & 7; |
| 162 | if m.base = RIP then |
| 163 | if m.index # NoIndex then |
| 164 | if m.scale = 1 then sc := 0; elsif m.scale = 2 then sc := 1; elsif m.scale = 4 then sc := 2; else sc := 3; end; |
| 165 | (* memAbs: mod 00, r/m 100 for a SIB byte, whose base 101 |
| 166 | under mod 00 means none, but a disp32: the chunk's address *) |
| 167 | Chunk.put1((reg shl 3) | 4); |
| 168 | Chunk.put1((sc shl 6) | ((m.index & 7) shl 3) | 5); |
| 169 | Chunk.addAbsFixup(Chunk.pc, m.chunk, m.disp); |
| 170 | Chunk.put4(0); |
| 171 | return; |
| 172 | end; |
| 173 | Chunk.put1((reg shl 3) | 5); |
| 174 | (* mod 00 with r/m 101 is rip + disp32. The displacement counts |
| 175 | from the end of the instruction, and the fixup takes it to end |
| 176 | with these four bytes: an instruction with an immediate after |
| 177 | them, aluMI's or storeI's, takes the immediate's size off disp |
| 178 | first (the assembler names a chunk only in lea, which has none). *) |
| 179 | Chunk.addFixup(Chunk.pc, m.chunk, m.disp); |
| 180 | Chunk.put4(0); |
| 181 | return; |
| 182 | end; |
| 183 | b := m.base & 7; |
| 184 | (* mod 00 takes no displacement, but with r/m 101 (RBP or R13) it means |
| 185 | rip-relative, or disp32 under a SIB byte, so those bases take a |
| 186 | zero disp8 *) |
| 187 | if (m.disp = 0) and (b # RBP) then |
| 188 | md := 0; |
| 189 | elsif (m.disp >= -128) and (m.disp <= 127) then |
| 190 | md := 1; |
| 191 | else |
| 192 | md := 2; |
| 193 | end; |
| 194 | (* r/m 100 (RSP or R12) means a SIB byte follows, so those bases need |
| 195 | one, with index 100 meaning none *) |
| 196 | if (m.index # NoIndex) or (b = RSP) then |
| 197 | Chunk.put1((md shl 6) | (reg shl 3) | 4); |
| 198 | if m.scale = 1 then sc := 0; elsif m.scale = 2 then sc := 1; elsif m.scale = 4 then sc := 2; else sc := 3; end; |
| 199 | if m.index = NoIndex then |
| 200 | Chunk.put1((sc shl 6) | (4 shl 3) | b); |
| 201 | else |
| 202 | Chunk.put1((sc shl 6) | ((m.index & 7) shl 3) | b); |
| 203 | end; |
| 204 | else |
| 205 | Chunk.put1((md shl 6) | (reg shl 3) | b); |
| 206 | end; |
| 207 | if md = 1 then |
| 208 | Chunk.put1(m.disp); |
| 209 | elsif md = 2 then |
| 210 | Chunk.put4(m.disp); |
| 211 | end; |
| 212 | end; |
| 213 | |
| 214 | (* op reg, rm (both registers) *) |
| 215 | proc opR(pfx: INT; w: BOOL; op, reg, rm: INT; byteReg: BOOL); |
| 216 | begin |
| 217 | prefix(pfx, w, reg, NoIndex, rm, byteReg and (((reg >= 4) and (reg <= 7)) or ((rm >= 4) and (rm <= 7)))); |
| 218 | opcode(op); |
| 219 | Chunk.put1(0C0H | ((reg & 7) shl 3) | (rm & 7)); |
| 220 | end; |
| 221 | |
| 222 | (* The listing: the text of each instruction, as it is made *) |
| 223 | |
| 224 | (* How many instructions the listing has, and back to n of them, for |
| 225 | code made again *) |
| 226 | proc saidCount*: INT; |
| 227 | begin |
| 228 | return numSaid; |
| 229 | end; |
| 230 | |
| 231 | proc unsay*(n: INT); |
| 232 | begin |
| 233 | numSaid := n; |
| 234 | end; |
| 235 | |
| 236 | proc sayAt*(at: INT; s: []const U8); |
| 237 | begin |
| 238 | if not listing then return; end; |
| 239 | if numSaid = saidCap then |
| 240 | said := ^Said(Scan.regrab(^U8(said), numSaid * SIZE(Said), (saidCap * 2 + 1024) * SIZE(Said))); |
| 241 | saidCap := saidCap * 2 + 1024; |
| 242 | end; |
| 243 | said[numSaid].at := at; |
| 244 | said[numSaid].text := s; |
| 245 | INC(numSaid); |
| 246 | end; |
| 247 | |
| 248 | (* The text of the instruction about to be written *) |
| 249 | proc say*(s: []const U8); |
| 250 | begin |
| 251 | sayAt(Chunk.pc, s); |
| 252 | end; |
| 253 | |
| 254 | (* The text of the instruction at at, said again, as when its operand is |
| 255 | patched *) |
| 256 | proc restate*(at: INT; s: []const U8); |
| 257 | var i: INT; |
| 258 | begin |
| 259 | i := numSaid - 1; |
| 260 | while (i >= 0) and (said[i].at # at) do DEC(i); end; |
| 261 | if i >= 0 then said[i].text := s; end; |
| 262 | end; |
| 263 | |
| 264 | const |
| 265 | RegNames: [4][16][]const U8 = [ |
| 266 | ["rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", |
| 267 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"], |
| 268 | ["eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", |
| 269 | "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"], |
| 270 | ["ax", "cx", "dx", "bx", "sp", "bp", "si", "di", |
| 271 | "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"], |
| 272 | ["al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", |
| 273 | "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b"]]; |
| 274 | CondNames*: [16][]const U8 = ["o", "no", "b", "ae", "e", "ne", "be", "a", "s", "ns", "p", "np", "l", "ge", "le", "g"]; |
| 275 | AluNames: [8][]const U8 = ["add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"]; |
| 276 | ShiftNames: [8][]const U8 = ["rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar"]; |
| 277 | UnaryNames: [8][]const U8 = ["", "", "not", "neg", "mul", "imul", "div", "idiv"]; (* from 2 *) |
| 278 | |
| 279 | (* The name of register r, of size bytes *) |
| 280 | proc rnSized(r, size: INT): []const U8; |
| 281 | begin |
| 282 | if size = 1 then return RegNames[3][r]; end; |
| 283 | if size = 2 then return RegNames[2][r]; end; |
| 284 | if size = 4 then return RegNames[1][r]; end; |
| 285 | return RegNames[0][r]; |
| 286 | end; |
| 287 | |
| 288 | proc rn(r: INT): []const U8; |
| 289 | begin |
| 290 | return RegNames[0][r]; |
| 291 | end; |
| 292 | |
| 293 | proc xn(x: INT): []const U8; |
| 294 | begin |
| 295 | return Diag.say("xmm{}", [Fmt.int(x)]); |
| 296 | end; |
| 297 | |
| 298 | (* A memory operand, with the size of what it holds, as the assembler |
| 299 | reads it *) |
| 300 | proc mnSized(m: Mem; size: INT): []const U8; |
| 301 | var b: ^Buf.Buf; |
| 302 | begin |
| 303 | b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); |
| 304 | Buf.init(b^, Heap.heap); |
| 305 | if size = 1 then Buf.str(b^, "byte "); |
| 306 | elsif size = 2 then Buf.str(b^, "word "); |
| 307 | elsif size = 4 then Buf.str(b^, "dword "); |
| 308 | elsif size = 8 then Buf.str(b^, "qword "); |
| 309 | end; |
| 310 | Buf.str(b^, "["); |
| 311 | if m.base = RIP then Buf.str(b^, Chunk.chunkName(m.chunk)); else Buf.str(b^, rn(m.base)); end; |
| 312 | if m.index # NoIndex then Fmt.write(b^, " + {} * {}", [Fmt.str(rn(m.index)), Fmt.int(m.scale)]); end; |
| 313 | if m.disp > 0 then |
| 314 | Fmt.write(b^, " + {}", [Fmt.int(m.disp)]); |
| 315 | elsif m.disp < 0 then |
| 316 | Fmt.write(b^, " - {}", [Fmt.int(-m.disp)]); |
| 317 | end; |
| 318 | Buf.str(b^, "]"); |
| 319 | return Buf.bytes(b^); |
| 320 | end; |
| 321 | |
| 322 | proc mn(m: Mem): []const U8; |
| 323 | begin |
| 324 | return mnSized(m, 0); |
| 325 | end; |
| 326 | |
| 327 | (* "op a, b" and "op a" *) |
| 328 | proc text2(op, a, b: []const U8): []const U8; |
| 329 | begin |
| 330 | return Diag.say("{} {}, {}", [Fmt.str(op), Fmt.str(a), Fmt.str(b)]); |
| 331 | end; |
| 332 | |
| 333 | proc text1(op, a: []const U8): []const U8; |
| 334 | begin |
| 335 | return Diag.say("{} {}", [Fmt.str(op), Fmt.str(a)]); |
| 336 | end; |
| 337 | |
| 338 | (* v in decimal *) |
| 339 | proc num(v: INT): []const U8; |
| 340 | begin |
| 341 | return Diag.say("{}", [Fmt.int(v)]); |
| 342 | end; |
| 343 | |
| 344 | proc movRR*(dst, src: INT); |
| 345 | begin |
| 346 | if listing then say(text2("mov", rn(dst), rn(src))); end; |
| 347 | opR(0, true, 89H, src, dst, false); |
| 348 | end; |
| 349 | |
| 350 | (* The operand-size prefix a size of 2 bytes takes *) |
| 351 | proc pfx16(size: INT): INT; |
| 352 | begin |
| 353 | if size = 2 then return 66H; end; |
| 354 | return 0; |
| 355 | end; |
| 356 | |
| 357 | (* The immediate of n bytes that ends an instruction *) |
| 358 | proc putImm(v, n: INT); |
| 359 | begin |
| 360 | if n = 4 then |
| 361 | Chunk.put4(v); |
| 362 | else |
| 363 | Chunk.put1(v & 0FFH); |
| 364 | if n = 2 then Chunk.put1((v shr 8) & 0FFH); end; |
| 365 | end; |
| 366 | end; |
| 367 | |
| 368 | (* op [m], v, on size bytes; v fits in them, or in 32 bits for 8 *) |
| 369 | proc aluMI*(op: INT; m: Mem; v, size: INT); |
| 370 | var n: INT; |
| 371 | begin |
| 372 | if listing then say(text2(AluNames[op], mnSized(m, size), num(v))); end; |
| 373 | if (size = 1) or ((v >= -128) and (v <= 127)) then |
| 374 | n := 1; |
| 375 | elsif size = 2 then |
| 376 | n := 2; |
| 377 | else |
| 378 | n := 4; |
| 379 | end; |
| 380 | if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end; |
| 381 | if size = 1 then |
| 382 | opM(0, false, 80H, op, m, false); |
| 383 | elsif n = 1 then |
| 384 | opM(pfx16(size), size = 8, 83H, op, m, false); |
| 385 | else |
| 386 | opM(pfx16(size), size = 8, 81H, op, m, false); |
| 387 | end; |
| 388 | putImm(v, n); |
| 389 | end; |
| 390 | |
| 391 | (* mov [m], v, of size bytes; v fits in them, or in 32 bits for 8 *) |
| 392 | proc storeI*(m: Mem; v, size: INT); |
| 393 | var n: INT; |
| 394 | begin |
| 395 | if listing then say(text2("mov", mnSized(m, size), num(v))); end; |
| 396 | n := size; |
| 397 | if n > 4 then n := 4; end; |
| 398 | if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end; |
| 399 | if size = 1 then |
| 400 | opM(0, false, 0C6H, 0, m, false); |
| 401 | else |
| 402 | opM(pfx16(size), size = 8, 0C7H, 0, m, false); |
| 403 | end; |
| 404 | putImm(v, n); |
| 405 | end; |
| 406 | |
| 407 | proc movRI*(r: INT; v: I64); |
| 408 | begin |
| 409 | if listing then |
| 410 | if v = 0 then |
| 411 | say(text2("xor", rnSized(r, 4), rnSized(r, 4))); |
| 412 | else |
| 413 | say(text2("mov", rn(r), Diag.say("{}", [Fmt.int(v)]))); |
| 414 | end; |
| 415 | end; |
| 416 | if (v >= -2147483648) and (v <= 2147483647) then |
| 417 | if v = 0 then |
| 418 | opR(0, false, 31H, r, r, false); (* xor r32, r32 *) |
| 419 | elsif v > 0 then (* mov r32, imm32 *) |
| 420 | prefix(0, false, 0, NoIndex, r, false); Chunk.put1(0B8H + (r & 7)); Chunk.put4(INT(v)); |
| 421 | else |
| 422 | opR(0, true, 0C7H, 0, r, false); Chunk.put4(INT(v)); (* sign-extended imm32 *) |
| 423 | end; |
| 424 | elsif (v > 0) and (v <= 0FFFFFFFFH) then |
| 425 | prefix(0, false, 0, NoIndex, r, false); Chunk.put1(0B8H + (r & 7)); Chunk.put4(INT(v)); |
| 426 | else |
| 427 | prefix(0, true, 0, NoIndex, r, false); Chunk.put1(0B8H + (r & 7)); Chunk.put8(v); |
| 428 | end; |
| 429 | end; |
| 430 | |
| 431 | proc load*(r: INT; m: Mem; size: INT; signed: BOOL); |
| 432 | begin |
| 433 | if listing then |
| 434 | if size = 8 then |
| 435 | say(text2("mov", rn(r), mn(m))); |
| 436 | elsif (size = 4) and not signed then |
| 437 | say(text2("mov", rnSized(r, 4), mn(m))); |
| 438 | elsif size = 4 then |
| 439 | say(text2("movsxd", rn(r), mnSized(m, 4))); |
| 440 | elsif signed then |
| 441 | say(text2("movsx", rn(r), mnSized(m, size))); |
| 442 | else |
| 443 | say(text2("movzx", rnSized(r, 4), mnSized(m, size))); |
| 444 | end; |
| 445 | end; |
| 446 | if size = 1 then |
| 447 | if signed then opM(0, true, 0BE0FH, r, m, false); else opM(0, false, 0B60FH, r, m, false); end; |
| 448 | elsif size = 2 then |
| 449 | if signed then opM(0, true, 0BF0FH, r, m, false); else opM(0, false, 0B70FH, r, m, false); end; |
| 450 | elsif size = 4 then |
| 451 | if signed then opM(0, true, 63H, r, m, false); else opM(0, false, 8BH, r, m, false); end; |
| 452 | else |
| 453 | opM(0, true, 8BH, r, m, false); |
| 454 | end; |
| 455 | end; |
| 456 | |
| 457 | proc store*(m: Mem; r: INT; size: INT); |
| 458 | begin |
| 459 | if listing then say(text2("mov", mn(m), rnSized(r, size))); end; |
| 460 | if size = 1 then |
| 461 | opM(0, false, 88H, r, m, true); |
| 462 | elsif size = 2 then |
| 463 | opM(66H, false, 89H, r, m, false); |
| 464 | elsif size = 4 then |
| 465 | opM(0, false, 89H, r, m, false); |
| 466 | else |
| 467 | opM(0, true, 89H, r, m, false); |
| 468 | end; |
| 469 | end; |
| 470 | |
| 471 | proc lea*(r: INT; m: Mem); |
| 472 | begin |
| 473 | if listing then say(text2("lea", rn(r), mn(m))); end; |
| 474 | opM(0, true, 8DH, r, m, false); |
| 475 | end; |
| 476 | |
| 477 | proc alu*(op, dst, src: INT); |
| 478 | begin |
| 479 | if listing then say(text2(AluNames[op], rn(dst), rn(src))); end; |
| 480 | opR(0, true, (op shl 3) | 1, src, dst, false); |
| 481 | end; |
| 482 | |
| 483 | proc aluI*(op, r: INT; v: INT); |
| 484 | begin |
| 485 | if listing then say(text2(AluNames[op], rn(r), num(v))); end; |
| 486 | if (v >= -128) and (v <= 127) then |
| 487 | opR(0, true, 83H, op, r, false); Chunk.put1(v); |
| 488 | else |
| 489 | opR(0, true, 81H, op, r, false); Chunk.put4(v); |
| 490 | end; |
| 491 | end; |
| 492 | |
| 493 | (* op r, [m] *) |
| 494 | proc aluM*(op, r: INT; m: Mem); |
| 495 | begin |
| 496 | if listing then say(text2(AluNames[op], rn(r), mn(m))); end; |
| 497 | opM(0, true, (op shl 3) | 3, r, m, false); |
| 498 | end; |
| 499 | |
| 500 | proc imul*(dst, src: INT); |
| 501 | begin |
| 502 | if listing then say(text2("imul", rn(dst), rn(src))); end; |
| 503 | opR(0, true, 0AF0FH, dst, src, false); |
| 504 | end; |
| 505 | |
| 506 | proc imulI*(r: INT; v: INT); |
| 507 | begin |
| 508 | if listing then say(Diag.say("imul {}, {}, {}", [Fmt.str(rn(r)), Fmt.str(rn(r)), Fmt.int(v)])); end; |
| 509 | opR(0, true, 69H, r, r, false); |
| 510 | Chunk.put4(v); |
| 511 | end; |
| 512 | |
| 513 | proc shift*(op, r: INT); |
| 514 | begin |
| 515 | if listing then say(text2(ShiftNames[op], rn(r), "cl")); end; |
| 516 | opR(0, true, 0D3H, op, r, false); |
| 517 | end; |
| 518 | |
| 519 | proc shiftI*(op, r, n: INT); |
| 520 | begin |
| 521 | if listing then say(text2(ShiftNames[op], rn(r), num(n))); end; |
| 522 | opR(0, true, 0C1H, op, r, false); |
| 523 | Chunk.put1(n); |
| 524 | end; |
| 525 | |
| 526 | (* rol or ror, op, of the size bytes of r, by cl, or by n if n >= 0: |
| 527 | D3 by cl and C1 by an imm8, D2 and C0 for a byte. Unlike a shift, a |
| 528 | rotation is done at its value's own width, rol eax and not rol rax, |
| 529 | so the bits leaving one end come back in at the other end of it. *) |
| 530 | proc rotate*(op, r, size, n: INT); |
| 531 | var opc: INT; |
| 532 | begin |
| 533 | if listing then |
| 534 | if n < 0 then say(text2(ShiftNames[op], rnSized(r, size), "cl")); |
| 535 | else say(text2(ShiftNames[op], rnSized(r, size), num(n))); end; |
| 536 | end; |
| 537 | if n < 0 then opc := 0D2H; else opc := 0C0H; end; |
| 538 | if size > 1 then INC(opc); end; |
| 539 | opR(pfx16(size), size = 8, opc, op, r, size = 1); |
| 540 | if n >= 0 then Chunk.put1(n); end; |
| 541 | end; |
| 542 | |
| 543 | (* bsr r, r, if reverse, or bsf r, r: the place of r's highest, or |
| 544 | lowest, one bit, 0F BD and 0F BC. ZF is set if r is 0, which has no |
| 545 | one bit, and r is then left undefined. *) |
| 546 | proc bitScan*(reverse: BOOL; r: INT); |
| 547 | var opc: INT; name: []const U8; |
| 548 | begin |
| 549 | if reverse then opc := 0BD0FH; name := "bsr"; else opc := 0BC0FH; name := "bsf"; end; |
| 550 | if listing then say(text2(name, rn(r), rn(r))); end; |
| 551 | opR(0, true, opc, r, r, false); |
| 552 | end; |
| 553 | |
| 554 | (* F7 /ext: 2 not, 3 neg, 6 div, 7 idiv *) |
| 555 | proc unary*(ext, r: INT); |
| 556 | begin |
| 557 | if listing then say(text1(UnaryNames[ext], rn(r))); end; |
| 558 | opR(0, true, 0F7H, ext, r, false); |
| 559 | end; |
| 560 | |
| 561 | proc cqo*; |
| 562 | begin |
| 563 | if listing then say("cqo"); end; |
| 564 | Chunk.put1(48H); Chunk.put1(99H); |
| 565 | end; |
| 566 | |
| 567 | (* Brings a value of size bytes in r to 64 bits, by its sign or zeros. *) |
| 568 | proc extend*(r, size: INT; signed: BOOL); |
| 569 | begin |
| 570 | if listing then |
| 571 | if size = 4 then |
| 572 | if signed then |
| 573 | say(text2("movsxd", rn(r), rnSized(r, 4))); |
| 574 | else |
| 575 | say(text2("mov", rnSized(r, 4), rnSized(r, 4))); |
| 576 | end; |
| 577 | elsif signed then |
| 578 | say(text2("movsx", rn(r), rnSized(r, size))); |
| 579 | else |
| 580 | say(text2("movzx", rnSized(r, 4), rnSized(r, size))); |
| 581 | end; |
| 582 | end; |
| 583 | if size = 1 then |
| 584 | if signed then opR(0, true, 0BE0FH, r, r, true); else opR(0, false, 0B60FH, r, r, true); end; |
| 585 | elsif size = 2 then |
| 586 | if signed then opR(0, true, 0BF0FH, r, r, false); else opR(0, false, 0B70FH, r, r, false); end; |
| 587 | elsif size = 4 then |
| 588 | if signed then opR(0, true, 63H, r, r, false); else opR(0, false, 89H, r, r, false); end; |
| 589 | end; |
| 590 | end; |
| 591 | |
| 592 | (* r := 1 if the condition holds, else 0 *) |
| 593 | proc setCC*(cc, r: INT); |
| 594 | begin |
| 595 | if listing then say(text1(Diag.say("set{}", [Fmt.str(CondNames[cc])]), rnSized(r, 1))); end; |
| 596 | opR(0, false, 900FH + (cc shl 8), 0, r, true); |
| 597 | if listing then say(text2("movzx", rnSized(r, 4), rnSized(r, 1))); end; |
| 598 | opR(0, false, 0B60FH, r, r, true); |
| 599 | end; |
| 600 | |
| 601 | proc testRR*(a, b: INT); |
| 602 | begin |
| 603 | if listing then say(text2("test", rn(a), rn(b))); end; |
| 604 | opR(0, true, 85H, b, a, false); |
| 605 | end; |
| 606 | |
| 607 | proc push*(r: INT); |
| 608 | begin |
| 609 | if listing then say(text1("push", rn(r))); end; |
| 610 | if r >= 8 then Chunk.put1(41H); end; |
| 611 | Chunk.put1(50H + (r & 7)); |
| 612 | end; |
| 613 | |
| 614 | proc pop*(r: INT); |
| 615 | begin |
| 616 | if listing then say(text1("pop", rn(r))); end; |
| 617 | if r >= 8 then Chunk.put1(41H); end; |
| 618 | Chunk.put1(58H + (r & 7)); |
| 619 | end; |
| 620 | |
| 621 | proc callChunk*(c: INT); |
| 622 | begin |
| 623 | if listing then say(text1("call", Chunk.chunkName(c))); end; |
| 624 | Chunk.put1(0E8H); |
| 625 | Chunk.addFixup(Chunk.pc, c, 0); |
| 626 | Chunk.put4(0); |
| 627 | end; |
| 628 | |
| 629 | (* jmp to the start of chunk c, as a tail call: E9 and the distance *) |
| 630 | proc jmpChunk*(c: INT); |
| 631 | begin |
| 632 | if listing then say(text1("jmp", Chunk.chunkName(c))); end; |
| 633 | Chunk.put1(0E9H); |
| 634 | Chunk.addFixup(Chunk.pc, c, 0); |
| 635 | Chunk.put4(0); |
| 636 | end; |
| 637 | |
| 638 | proc callBack*(target: INT); |
| 639 | begin |
| 640 | if listing then say("call"); end; |
| 641 | Chunk.put1(0E8H); |
| 642 | Chunk.put4(target - (Chunk.pc + 4)); |
| 643 | end; |
| 644 | |
| 645 | proc callR*(r: INT); |
| 646 | begin |
| 647 | if listing then say(text1("call", rn(r))); end; |
| 648 | opR(0, false, 0FFH, 2, r, false); |
| 649 | end; |
| 650 | |
| 651 | proc jmpR*(r: INT); |
| 652 | begin |
| 653 | if listing then say(text1("jmp", rn(r))); end; |
| 654 | opR(0, false, 0FFH, 4, r, false); |
| 655 | end; |
| 656 | |
| 657 | proc callM*(m: Mem); |
| 658 | begin |
| 659 | if listing then say(text1("call", mn(m))); end; |
| 660 | opM(0, false, 0FFH, 2, m, false); |
| 661 | end; |
| 662 | |
| 663 | proc ret*; |
| 664 | begin |
| 665 | if listing then say("ret"); end; |
| 666 | Chunk.put1(0C3H); |
| 667 | end; |
| 668 | |
| 669 | proc leave*; |
| 670 | begin |
| 671 | say("leave"); Chunk.put1(0C9H); |
| 672 | end; |
| 673 | |
| 674 | proc syscall*; |
| 675 | begin |
| 676 | say("syscall"); Chunk.put1(0FH); Chunk.put1(05H); |
| 677 | end; |
| 678 | |
| 679 | proc mfence*; |
| 680 | begin |
| 681 | say("mfence"); Chunk.put1(0FH); Chunk.put1(0AEH); Chunk.put1(0F0H); |
| 682 | end; |
| 683 | |
| 684 | proc ud2*; |
| 685 | begin |
| 686 | say("ud2"); Chunk.put1(0FH); Chunk.put1(0BH); |
| 687 | end; |
| 688 | |
| 689 | (* Code made up to a multiple of n bytes with no-ops, the fewest: the |
| 690 | long forms of nop, of up to 8 bytes, that x86 decodes as one *) |
| 691 | proc align*(n: INT); |
| 692 | const |
| 693 | Nops: [8][]const U8 = ["\x90", "\x66\x90", "\x0F\x1F\x00", "\x0F\x1F\x40\x00", |
| 694 | "\x0F\x1F\x44\x00\x00", "\x66\x0F\x1F\x44\x00\x00", "\x0F\x1F\x80\x00\x00\x00\x00", |
| 695 | "\x0F\x1F\x84\x00\x00\x00\x00\x00"]; |
| 696 | var k, i: INT; |
| 697 | begin |
| 698 | while Chunk.pc mod n # 0 do |
| 699 | k := n - Chunk.pc mod n; |
| 700 | if k > 8 then k := 8; end; |
| 701 | say("nop"); |
| 702 | for i := 0 to k - 1 do Chunk.put1(INT(Nops[k - 1][i])); end; |
| 703 | end; |
| 704 | end; |
| 705 | |
| 706 | proc repMovsb*; |
| 707 | begin |
| 708 | say("rep movsb"); Chunk.put1(0F3H); Chunk.put1(0A4H); |
| 709 | end; |
| 710 | |
| 711 | (* rep stos of an element of n bytes, 1, 2, 4 or 8: al, ax, eax or rax |
| 712 | into rcx elements from [rdi] up. The width is that of stosb, with 66H |
| 713 | for a word, or with REX.W for a quadword. *) |
| 714 | proc repStos*(n: INT); |
| 715 | begin |
| 716 | match n of |
| 717 | | 1: say("rep stosb"); |
| 718 | | 2: say("rep stosw"); Chunk.put1(66H); |
| 719 | | 4: say("rep stosd"); |
| 720 | | else: say("rep stosq"); |
| 721 | end; |
| 722 | Chunk.put1(0F3H); |
| 723 | if n = 8 then Chunk.put1(48H); end; |
| 724 | if n = 1 then Chunk.put1(0AAH); else Chunk.put1(0ABH); end; |
| 725 | end; |
| 726 | |
| 727 | proc repeCmpsb*; |
| 728 | begin |
| 729 | say("repe cmpsb"); Chunk.put1(0F3H); Chunk.put1(0A6H); |
| 730 | end; |
| 731 | |
| 732 | proc lockCmpxchg*(m: Mem; r: INT); |
| 733 | begin |
| 734 | if listing then say(text2("lock cmpxchg", mn(m), rn(r))); end; |
| 735 | Chunk.put1(0F0H); |
| 736 | opM(0, true, 0B10FH, r, m, false); |
| 737 | end; |
| 738 | |
| 739 | proc xchg*(m: Mem; r: INT); |
| 740 | begin |
| 741 | if listing then say(text2("xchg", mn(m), rn(r))); end; |
| 742 | opM(0, true, 87H, r, m, false); |
| 743 | end; |
| 744 | |
| 745 | proc lockXadd*(m: Mem; r: INT); |
| 746 | begin |
| 747 | if listing then say(text2("lock xadd", mn(m), rn(r))); end; |
| 748 | Chunk.put1(0F0H); |
| 749 | opM(0, true, 0C10FH, r, m, false); |
| 750 | end; |
| 751 | |
| 752 | proc movqXR*(x, r: INT); |
| 753 | begin |
| 754 | if listing then say(text2("movq", xn(x), rn(r))); end; |
| 755 | opR(66H, true, 6E0FH, x, r, false); |
| 756 | end; |
| 757 | |
| 758 | proc movqRX*(r, x: INT); |
| 759 | begin |
| 760 | if listing then say(text2("movq", rn(r), xn(x))); end; |
| 761 | opR(66H, true, 7E0FH, x, r, false); |
| 762 | end; |
| 763 | |
| 764 | (* movaps: all of xmm y to xmm x, the shortest move between two *) |
| 765 | proc movX*(x, y: INT); |
| 766 | begin |
| 767 | if listing then say(text2("movaps", xn(x), xn(y))); end; |
| 768 | opR(0, false, 280FH, x, y, false); |
| 769 | end; |
| 770 | |
| 771 | (* movdqu: 16 bytes between xmm x and memory, and pxor, which zeroes |
| 772 | xmm x: how Gen copies and clears a record in a few instructions *) |
| 773 | proc movdquXM*(x: INT; m: Mem); |
| 774 | begin |
| 775 | if listing then say(text2("movdqu", xn(x), mn(m))); end; |
| 776 | opM(0F3H, false, 6F0FH, x, m, false); |
| 777 | end; |
| 778 | |
| 779 | proc movdquMX*(m: Mem; x: INT); |
| 780 | begin |
| 781 | if listing then say(text2("movdqu", mn(m), xn(x))); end; |
| 782 | opM(0F3H, false, 7F0FH, x, m, false); |
| 783 | end; |
| 784 | |
| 785 | proc pxorXX*(x: INT); |
| 786 | begin |
| 787 | if listing then say(text2("pxor", xn(x), xn(x))); end; |
| 788 | opR(66H, false, 0EF0FH, x, x, false); |
| 789 | end; |
| 790 | |
| 791 | (* The prefix of a real instruction, and the end of its name: sd, or ss for single *) |
| 792 | proc realPfx(single: BOOL): INT; |
| 793 | begin |
| 794 | if single then return 0F3H; end; |
| 795 | return 0F2H; |
| 796 | end; |
| 797 | |
| 798 | proc realEnd(single: BOOL): []const U8; |
| 799 | begin |
| 800 | if single then return "ss"; end; |
| 801 | return "sd"; |
| 802 | end; |
| 803 | |
| 804 | (* The real instructions take single: F32 (ss) rather than F64 (sd) *) |
| 805 | proc movsXM*(x: INT; m: Mem; single: BOOL); |
| 806 | begin |
| 807 | if listing then say(text2(Diag.say("movs{}", [Fmt.str(realEnd(single)[1..2])]), xn(x), mn(m))); end; |
| 808 | opM(realPfx(single), false, 100FH, x, m, false); |
| 809 | end; |
| 810 | |
| 811 | proc movsMX*(m: Mem; x: INT; single: BOOL); |
| 812 | begin |
| 813 | if listing then say(text2(Diag.say("movs{}", [Fmt.str(realEnd(single)[1..2])]), mn(m), xn(x))); end; |
| 814 | opM(realPfx(single), false, 110FH, x, m, false); |
| 815 | end; |
| 816 | |
| 817 | proc sseName(op: INT; single: BOOL): []const U8; |
| 818 | begin |
| 819 | if op = 58H then return Diag.say("add{}", [Fmt.str(realEnd(single))]); end; |
| 820 | if op = 59H then return Diag.say("mul{}", [Fmt.str(realEnd(single))]); end; |
| 821 | if op = 5CH then return Diag.say("sub{}", [Fmt.str(realEnd(single))]); end; |
| 822 | if op = 5EH then return Diag.say("div{}", [Fmt.str(realEnd(single))]); end; |
| 823 | if op = 51H then return Diag.say("sqrt{}", [Fmt.str(realEnd(single))]); end; |
| 824 | return Diag.say("cvt{}2{}", [Fmt.str(realEnd(single)), Fmt.str(realEnd(not single))]); |
| 825 | end; |
| 826 | |
| 827 | (* 58 add, 59 mul, 5C sub, 5E div, 5A convert *) |
| 828 | proc sse*(op, x, y: INT; single: BOOL); |
| 829 | begin |
| 830 | if listing then say(text2(sseName(op, single), xn(x), xn(y))); end; |
| 831 | opR(realPfx(single), false, 0FH | (op shl 8), x, y, false); |
| 832 | end; |
| 833 | |
| 834 | (* The same, its right operand in memory: addsd x, [m] *) |
| 835 | proc sseM*(op, x: INT; m: Mem; single: BOOL); |
| 836 | begin |
| 837 | if listing then say(text2(sseName(op, single), xn(x), mn(m))); end; |
| 838 | opM(realPfx(single), false, 0FH | (op shl 8), x, m, false); |
| 839 | end; |
| 840 | |
| 841 | proc ucomis*(x, y: INT; single: BOOL); |
| 842 | begin |
| 843 | if listing then say(text2(Diag.say("ucomi{}", [Fmt.str(realEnd(single))]), xn(x), xn(y))); end; |
| 844 | if single then opR(0, false, 2E0FH, x, y, false); else opR(66H, false, 2E0FH, x, y, false); end; |
| 845 | end; |
| 846 | |
| 847 | proc cvtsi2s*(x, r: INT; single: BOOL); |
| 848 | begin |
| 849 | if listing then say(text2(Diag.say("cvtsi2{}", [Fmt.str(realEnd(single))]), xn(x), rn(r))); end; |
| 850 | opR(realPfx(single), true, 2A0FH, x, r, false); |
| 851 | end; |
| 852 | |
| 853 | proc cvtts2si*(r, x: INT; single: BOOL); |
| 854 | begin |
| 855 | if listing then say(text2(Diag.say("cvtt{}2si", [Fmt.str(realEnd(single))]), rn(r), xn(x))); end; |
| 856 | opR(realPfx(single), true, 2C0FH, r, x, false); |
| 857 | end; |
| 858 | |
| 859 | proc btc*(r, bit: INT); |
| 860 | begin |
| 861 | if listing then say(text2("btc", rn(r), num(bit))); end; |
| 862 | opR(0, true, 0BA0FH, 7, r, false); |
| 863 | Chunk.put1(bit); |
| 864 | end; |
| 865 | |
| 866 | (* btr r, bit: the bit cleared, 0F BA /6 *) |
| 867 | proc btr*(r, bit: INT); |
| 868 | begin |
| 869 | if listing then say(text2("btr", rn(r), num(bit))); end; |
| 870 | opR(0, true, 0BA0FH, 6, r, false); |
| 871 | Chunk.put1(bit); |
| 872 | end; |
| 873 | |
| 874 | (* cmovcc r, x: r := x if the condition holds, 0F 40+cc *) |
| 875 | proc cmov*(cc, r, x: INT); |
| 876 | begin |
| 877 | if listing then say(text2(Diag.say("cmov{}", [Fmt.str(CondNames[cc])]), rn(r), rn(x))); end; |
| 878 | opR(0, true, 400FH + (cc shl 8), r, x, false); |
| 879 | end; |
| 880 | |
| 881 | (* Jumps. A label not yet placed is the chain of the jumps to it, each |
| 882 | rel32 field holding the position of the one before, NoLabel ending |
| 883 | it. Placing the label walks the chain and patches each. *) |
| 884 | |
| 885 | proc jmp*(var L: INT); |
| 886 | begin |
| 887 | if listing then say("jmp"); end; |
| 888 | Chunk.put1(0E9H); |
| 889 | Chunk.put4(L); |
| 890 | L := Chunk.pc - 4; |
| 891 | end; |
| 892 | |
| 893 | proc jcc*(cc: INT; var L: INT); |
| 894 | begin |
| 895 | if listing then say(Diag.say("j{}", [Fmt.str(CondNames[cc])])); end; |
| 896 | Chunk.put1(0FH); Chunk.put1(80H + cc); |
| 897 | Chunk.put4(L); |
| 898 | L := Chunk.pc - 4; |
| 899 | end; |
| 900 | |
| 901 | proc jmpBack*(target: INT); |
| 902 | begin |
| 903 | if listing then say("jmp"); end; |
| 904 | Chunk.put1(0E9H); |
| 905 | Chunk.put4(target - (Chunk.pc + 4)); |
| 906 | end; |
| 907 | |
| 908 | proc jccBack*(cc, target: INT); |
| 909 | begin |
| 910 | if listing then say(Diag.say("j{}", [Fmt.str(CondNames[cc])])); end; |
| 911 | Chunk.put1(0FH); Chunk.put1(80H + cc); |
| 912 | Chunk.put4(target - (Chunk.pc + 4)); |
| 913 | end; |
| 914 | |
| 915 | proc fixLinkTo*(L, target: INT); |
| 916 | var next: INT; |
| 917 | begin |
| 918 | while L # NoLabel do |
| 919 | next := Chunk.get4At(L); |
| 920 | Chunk.put4At(L, target - (L + 4)); |
| 921 | L := next; |
| 922 | end; |
| 923 | end; |
| 924 | |
| 925 | proc fixLink*(L: INT); |
| 926 | begin |
| 927 | if L # NoLabel then labelAt := Chunk.pc; end; |
| 928 | fixLinkTo(L, Chunk.pc); |
| 929 | end; |
| 930 | |
| 931 | (* If the last instruction made is a jmp to L, it is taken back, since L |
| 932 | is to be placed just after it: a return at the end of a procedure |
| 933 | needn't jump to the code that returns. L is the place of the latest |
| 934 | jump's 4-byte field, so the last instruction is that jump when L is |
| 935 | 4 bytes back and an E9 byte (jmp) is before it; a jcc's field follows |
| 936 | the bytes 0F 8x, never E9. L then becomes the next jump on the chain, |
| 937 | which the field held. The caller must place L next, before anything |
| 938 | can have noted where the jmp ended; and no jump may have been fixed |
| 939 | to just after it, as an if's, which would then land beyond. *) |
| 940 | proc dropJumpTo*(var L: INT); |
| 941 | begin |
| 942 | if (L = NoLabel) or (L # Chunk.pc - 4) or (Chunk.text[L - 1] # 0E9H) or (labelAt = Chunk.pc) then return; end; |
| 943 | L := Chunk.get4At(L); |
| 944 | Chunk.takeBack(5); |
| 945 | if listing and (numSaid > 0) and (said[numSaid - 1].at = Chunk.pc) then DEC(numSaid); end; |
| 946 | end; |
| 947 | |
| 948 | (* The jumps of chains L1 and L2, as one chain: each jump's field holds |
| 949 | the next one's place until it is fixed, the last NoLabel *) |
| 950 | proc joinLinks*(L1, L2: INT): INT; |
| 951 | var L: INT; |
| 952 | begin |
| 953 | if L2 = NoLabel then return L1; end; |
| 954 | if L1 # NoLabel then |
| 955 | L := L2; |
| 956 | while Chunk.get4At(L) # NoLabel do L := Chunk.get4At(L); end; |
| 957 | Chunk.put4At(L, L1); |
| 958 | end; |
| 959 | return L2; |
| 960 | end; |
| 961 | |
| 962 | (* The assembler, for code procedures: x86-64 in Intel's syntax as NASM |
| 963 | writes it (report, appendix A). It reads instructions with the |
| 964 | scanner up to end, and encodes them with opM and opR; constants are |
| 965 | read by the parser's constExpr, and the names of procedures and |
| 966 | module variables by its nameFn, which gives their chunks: call p and |
| 967 | jmp p reach one by a fixup, as Gen's calls do, and lea r, [v] by a |
| 968 | rip-relative fixup, as Gen's globals are reached. *) |
| 969 | |
| 970 | const |
| 971 | NoBase = -3; (* a memory operand's base, not yet read *) |
| 972 | (* the places a procedure's or variable's name may stand (report section 8) *) |
| 973 | NameOnly = "a procedure or a variable stands for its address only in call p, jmp p and lea r, [v]"; |
| 974 | |
| 975 | type |
| 976 | (* oName is a procedure or module variable, its chunk in m, for call *) |
| 977 | OpKind = (oReg, oXmm, oCr, oMem, oImm, oName); |
| 978 | Operand = record |
| 979 | kind: OpKind; |
| 980 | r, size: INT; (* oReg, oXmm, oCr: the register and its size; oMem: its size, or 0 *) |
| 981 | m: Mem; |
| 982 | v: I64; (* oImm *) |
| 983 | named: BOOL; (* oName, or oMem as [v]: a procedure's or variable's address *) |
| 984 | at: Diag.Pos; (* where its name is, if named *) |
| 985 | end; |
| 986 | |
| 987 | (* The current symbol as a word: an identifier, or a reserved word that |
| 988 | is also an instruction *) |
| 989 | proc asmWord: []const U8; |
| 990 | begin |
| 991 | match Scan.tok of |
| 992 | | sIdent: return Scan.nameOf(Scan.id); |
| 993 | | sAnd: return "and"; |
| 994 | | sOr: return "or"; |
| 995 | | sXor: return "xor"; |
| 996 | | sNot: return "not"; |
| 997 | | sDiv: return "div"; |
| 998 | | sShl: return "shl"; |
| 999 | | sShr: return "shr"; |
| 1000 | | sIn: return "in"; |
| 1001 | | else: return ""; |
| 1002 | end; |
| 1003 | end; |
| 1004 | |
| 1005 | (* Whether w names a register: rax, eax, ax, al and so on, or xmm0 to |
| 1006 | xmm15 (size 16); r and size are set if so *) |
| 1007 | proc isReg(w: []const U8; var r, size: INT): BOOL; |
| 1008 | const Sizes: [4]INT = [8, 4, 2, 1]; |
| 1009 | var i, j, n, d: INT; |
| 1010 | begin |
| 1011 | for i := 0 to 3 do |
| 1012 | for j := 0 to 15 do |
| 1013 | if RegNames[i][j] = w then r := j; size := Sizes[i]; return true; end; |
| 1014 | end; |
| 1015 | end; |
| 1016 | size := 16; |
| 1017 | if not Str.hasPrefix(w, "xmm") or (LEN(w) <= 3) then return false; end; |
| 1018 | (* the seed read the two bytes after xmm as a number, whatever follows *) |
| 1019 | n := LEN(w) - 3; |
| 1020 | if n > 2 then n := 2; end; |
| 1021 | r := 0; |
| 1022 | for i := 0 to n - 1 do |
| 1023 | d := INT(w[3 + i]) - 48; |
| 1024 | if (d < 0) or (d > 9) then return false; end; |
| 1025 | r := r * 10 + d; |
| 1026 | end; |
| 1027 | return r <= 15; |
| 1028 | end; |
| 1029 | |
| 1030 | (* Which word of list, a list of words each with a space either side, |
| 1031 | w is, from 0; -1 if none *) |
| 1032 | proc wordIndex(w: []const U8; list: []const U8): INT; |
| 1033 | var i, n, start: INT; |
| 1034 | begin |
| 1035 | n := 0; |
| 1036 | start := 1; |
| 1037 | for i := 1 to LEN(list) - 1 do |
| 1038 | if list[i] = ' ' then |
| 1039 | if (i - start = LEN(w)) and (list[start..i] = w) then return n; end; |
| 1040 | INC(n); |
| 1041 | start := i + 1; |
| 1042 | end; |
| 1043 | end; |
| 1044 | return -1; |
| 1045 | end; |
| 1046 | |
| 1047 | (* w from byte from on, at most n bytes of it, as copy does *) |
| 1048 | proc part(w: []const U8; from, n: INT): []const U8; |
| 1049 | var e: INT; |
| 1050 | begin |
| 1051 | e := from + n; |
| 1052 | if e > LEN(w) then e := LEN(w); end; |
| 1053 | if from >= e then return ""; end; |
| 1054 | return w[from..e]; |
| 1055 | end; |
| 1056 | |
| 1057 | (* The condition a name ends in: e of je, nz of jnz *) |
| 1058 | proc cond(w: []const U8): INT; |
| 1059 | const |
| 1060 | Alias: [12][]const U8 = ["c", "nae", "nc", "nb", "z", "nz", "na", "nbe", "nge", "nl", "ng", "nle"]; |
| 1061 | AliasCc: [12]INT = [2, 2, 3, 3, 4, 5, 6, 7, 12, 13, 14, 15]; |
| 1062 | var i: INT; |
| 1063 | begin |
| 1064 | for i := 0 to 15 do |
| 1065 | if CondNames[i] = w then return i; end; |
| 1066 | end; |
| 1067 | for i := 0 to 11 do |
| 1068 | if Alias[i] = w then return AliasCc[i]; end; |
| 1069 | end; |
| 1070 | return -1; |
| 1071 | end; |
| 1072 | |
| 1073 | var |
| 1074 | (* the labels of the procedure *) |
| 1075 | lnames, lplaces, lchains: [64]INT; |
| 1076 | lusedAt: [64]Diag.Pos; |
| 1077 | numLabels: INT; |
| 1078 | |
| 1079 | (* Which label is called n, or -1 if none is *) |
| 1080 | proc labelIndex(n: INT): INT; |
| 1081 | var i: INT; |
| 1082 | begin |
| 1083 | for i := 0 to numLabels - 1 do |
| 1084 | if lnames[i] = n then return i; end; |
| 1085 | end; |
| 1086 | return -1; |
| 1087 | end; |
| 1088 | |
| 1089 | (* The label called n, entered if new *) |
| 1090 | proc lab(n: INT): (INT, ERROR); |
| 1091 | var i: INT; |
| 1092 | begin |
| 1093 | i := labelIndex(n); |
| 1094 | if i >= 0 then return i, nil; end; |
| 1095 | if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; |
| 1096 | lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; |
| 1097 | lusedAt[numLabels] := Scan.tokPos(); |
| 1098 | numLabels := numLabels + 1; |
| 1099 | return numLabels - 1, nil; |
| 1100 | end; |
| 1101 | |
| 1102 | (* Every label of the procedure, entered before an instruction is read, |
| 1103 | so that a name that is a label is the label wherever it is written, |
| 1104 | before its place or after (report section 8: a label is found first). |
| 1105 | A label is a word, at the start of an instruction, before a colon. The |
| 1106 | instructions are read quietly here, as they are read again after. *) |
| 1107 | proc findLabels; |
| 1108 | var st: Scan.State; wasQuiet, start: BOOL; w: []const U8; |
| 1109 | begin |
| 1110 | Scan.saveScan(st); |
| 1111 | wasQuiet := Diag.quiet; |
| 1112 | Diag.setQuiet(true); |
| 1113 | start := true; |
| 1114 | while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do |
| 1115 | w := ""; |
| 1116 | if start then w := asmWord(); end; |
| 1117 | start := Scan.tok = Scan.sSemicolon; |
| 1118 | Scan.next; |
| 1119 | if (LEN(w) > 0) and (Scan.tok = Scan.sColon) and (numLabels < 64) then |
| 1120 | _, _ := lab(Scan.intern(SYSTEM.PTR(w), LEN(w))); |
| 1121 | start := true; |
| 1122 | Scan.next; |
| 1123 | end; |
| 1124 | end; |
| 1125 | Diag.setQuiet(wasQuiet); |
| 1126 | Scan.restoreScan(st); |
| 1127 | end; |
| 1128 | |
| 1129 | (* The address a name at the scan stands for: a procedure's or a module |
| 1130 | variable's chunk, read by nameFn, its place noted in o.at; -1 if it is |
| 1131 | none, with nothing read. A label is found first, and only a jump takes |
| 1132 | one. *) |
| 1133 | proc nameAt(var o: Operand; nameFn: NameFn): (INT, ERROR); |
| 1134 | begin |
| 1135 | if Scan.tok # Scan.sIdent then return -1, nil; end; |
| 1136 | o.at := Scan.tokPos(); |
| 1137 | if labelIndex(Scan.id) >= 0 then |
| 1138 | return -1, Scan.mark(Diag.say("label {} is only for a jump", [Fmt.str(Scan.nameOf(Scan.id))])); |
| 1139 | end; |
| 1140 | return nameFn(); |
| 1141 | end; |
| 1142 | |
| 1143 | proc operand(var o: Operand; constExpr: ConstFn; nameFn: NameFn): ERROR; |
| 1144 | var w: []const U8; r, size, sz, c: INT; st: Scan.State; v: I64; isR, wasMinus: BOOL; |
| 1145 | begin |
| 1146 | o := {}; |
| 1147 | w := asmWord(); |
| 1148 | sz := 0; |
| 1149 | if w = "byte" then sz := 1; |
| 1150 | elsif w = "word" then sz := 2; |
| 1151 | elsif w = "dword" then sz := 4; |
| 1152 | elsif w = "qword" then sz := 8; |
| 1153 | end; |
| 1154 | if sz # 0 then Scan.next; w := asmWord(); end; |
| 1155 | if Scan.tok = Scan.sLbrak then |
| 1156 | (* [base + index * scale + disp], the displacement last *) |
| 1157 | Scan.next; |
| 1158 | o.kind := oMem; o.size := sz; |
| 1159 | o.m := mem(NoBase, 0); |
| 1160 | repeat |
| 1161 | if isReg(asmWord(), r, size) and (size = 8) then |
| 1162 | Scan.next; |
| 1163 | (* the scale is a single number or constant: a whole expression |
| 1164 | would take in the displacement after it, as 4 + 16 in |
| 1165 | [rdi + rsi * 4 + 16] *) |
| 1166 | if Scan.tok = Scan.sTimes then |
| 1167 | Scan.next; |
| 1168 | o.m.index := r; |
| 1169 | v := try constExpr(false); |
| 1170 | o.m.scale := INT(v); |
| 1171 | elsif o.m.base = NoBase then |
| 1172 | o.m.base := r; |
| 1173 | else |
| 1174 | o.m.index := r; o.m.scale := 1; |
| 1175 | end; |
| 1176 | else |
| 1177 | c := try nameAt(o, nameFn); |
| 1178 | if c < 0 then |
| 1179 | v := try constExpr(true); |
| 1180 | o.m.disp := o.m.disp + INT(v); |
| 1181 | elsif o.named then |
| 1182 | return Scan.markAt(o.at, NameOnly); |
| 1183 | else |
| 1184 | o.named := true; |
| 1185 | o.m.chunk := c; |
| 1186 | end; |
| 1187 | end; |
| 1188 | if (Scan.tok # Scan.sPlus) and (Scan.tok # Scan.sMinus) then break; end; |
| 1189 | wasMinus := Scan.tok = Scan.sMinus; |
| 1190 | Scan.saveScan(st); |
| 1191 | Scan.next; |
| 1192 | isR := isReg(asmWord(), r, size); |
| 1193 | if not isR and wasMinus then Scan.restoreScan(st); end; |
| 1194 | until false; |
| 1195 | if o.named then |
| 1196 | (* [v], whose address lea takes, alone, as statement checks *) |
| 1197 | if (o.m.base # NoBase) or (o.m.index # NoIndex) or (o.m.disp # 0) then |
| 1198 | return Scan.markAt(o.at, NameOnly); |
| 1199 | end; |
| 1200 | o.m := memRip(o.m.chunk, 0); |
| 1201 | end; |
| 1202 | if (Scan.tok # Scan.sRbrak) or (o.m.base = NoBase) |
| 1203 | or not ((o.m.scale = 1) or (o.m.scale = 2) or (o.m.scale = 4) or (o.m.scale = 8)) then |
| 1204 | return Scan.mark("expected a memory operand, [base + index * scale + disp]"); |
| 1205 | end; |
| 1206 | Scan.next; |
| 1207 | elsif (w = "cr0") or (w = "cr2") or (w = "cr3") or (w = "cr4") or (w = "cr8") then |
| 1208 | (* a control register, for mov to and from a general one *) |
| 1209 | o.kind := oCr; o.r := INT(w[2]) - 48; o.size := 8; |
| 1210 | Scan.next; |
| 1211 | elsif isReg(w, o.r, o.size) then |
| 1212 | if o.size = 16 then o.kind := oXmm; else o.kind := oReg; end; |
| 1213 | Scan.next; |
| 1214 | else |
| 1215 | c := try nameAt(o, nameFn); |
| 1216 | if c >= 0 then |
| 1217 | o.kind := oName; o.named := true; |
| 1218 | o.m := memRip(c, 0); |
| 1219 | else |
| 1220 | o.kind := oImm; |
| 1221 | o.v := try constExpr(true); |
| 1222 | end; |
| 1223 | end; |
| 1224 | return nil; |
| 1225 | end; |
| 1226 | |
| 1227 | (* op with register reg and the register or memory operand o *) |
| 1228 | proc rm(pfx: INT; w: BOOL; op, reg: INT; o: Operand; byteReg: BOOL); |
| 1229 | begin |
| 1230 | if o.kind = oMem then opM(pfx, w, op, reg, o.m, byteReg); else opR(pfx, w, op, reg, o.r, byteReg); end; |
| 1231 | end; |
| 1232 | |
| 1233 | proc putImmV(v: I64; size: INT); |
| 1234 | begin |
| 1235 | if size = 1 then |
| 1236 | Chunk.put1(INT(v & 0FFH)); |
| 1237 | elsif size = 2 then |
| 1238 | Chunk.put1(INT(v & 0FFH)); Chunk.put1(INT((v shr 8) & 0FFH)); |
| 1239 | else |
| 1240 | Chunk.put4(INT(v & 0FFFFFFFFH)); |
| 1241 | end; |
| 1242 | end; |
| 1243 | |
| 1244 | const |
| 1245 | Alus = " add or adc sbb and sub xor cmp "; |
| 1246 | Unaries = " not neg mul imul div idiv "; |
| 1247 | Shifts = " rol ror rcl rcr shl shr sal sar "; |
| 1248 | SseOps = " sqrt add mul sub min div max "; |
| 1249 | SseCodes: [7]INT = [51H, 58H, 59H, 5CH, 5DH, 5EH, 5FH]; |
| 1250 | (* SSE2 integer instructions that work on 16 bytes at a time: 66 0F code, |
| 1251 | xmm and xmm or memory (pshufd also takes a byte) *) |
| 1252 | PackedOps = " punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb paddd punpcklqdq punpckhqdq "; |
| 1253 | PackedCodes: [13]INT = [60H, 61H, 74H, 70H, 0DBH, 0DFH, 0EBH, 0EFH, 0FCH, 0F8H, 0FEH, 6CH, 6DH]; |
| 1254 | (* the same, xmm and xmm or memory, with a three-byte opcode: pshufb, palignr |
| 1255 | (which also takes a byte), and the SHA extensions. sha256rnds2 takes xmm0 |
| 1256 | as an implied third operand, written or not *) |
| 1257 | ShuffleOps = " pshufb palignr sha256rnds2 sha256msg1 sha256msg2 "; |
| 1258 | ShufflePfx: [5]INT = [66H, 66H, 0, 0, 0]; |
| 1259 | ShuffleCodes: [5]INT = [380FH, 0F3A0FH, 0CB380FH, 0CC380FH, 0CD380FH]; |
| 1260 | (* bit scans and counts: reg, reg or memory, with the prefix and code that tell them *) |
| 1261 | BitOps = " bsf bsr tzcnt lzcnt popcnt "; |
| 1262 | BitPfx: [5]INT = [0, 0, 0F3H, 0F3H, 0F3H]; |
| 1263 | BitCodes: [5]INT = [0BC0FH, 0BD0FH, 0BC0FH, 0BD0FH, 0B80FH]; |
| 1264 | (* the instructions of no operands; the last nine are a kernel's *) |
| 1265 | Plain: [24][]const U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc", |
| 1266 | "pause", "nop", "ud2", "int3", "hlt", "cqo", "cdq", "mfence", "lfence", "sfence", |
| 1267 | "cli", "sti", "iretq", "swapgs", "rdmsr", "wrmsr", "wbinvd", "cld", "std"]; |
| 1268 | PlainOp: [24]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH, |
| 1269 | 90F3H, 90H, 0B0FH, 0CCH, 0F4H, 9948H, 99H, 0F0AE0FH, 0E8AE0FH, 0F8AE0FH, |
| 1270 | 0FAH, 0FBH, 0CF48H, 0F8010FH, 320FH, 300FH, 090FH, 0FCH, 0FDH]; |
| 1271 | (* the privileged instructions of one memory operand, 0F 01 /digit |
| 1272 | but for xsave and xrstor, 0F AE /digit *) |
| 1273 | MemOps = " lgdt lidt invlpg xsave xrstor "; |
| 1274 | MemCodes: [5]INT = [010FH, 010FH, 010FH, 0AE0FH, 0AE0FH]; |
| 1275 | MemDigits: [5]INT = [2, 3, 7, 4, 5]; |
| 1276 | |
| 1277 | var |
| 1278 | (* the instruction being assembled *) |
| 1279 | aw: []const U8; (* its word *) |
| 1280 | ao: [3]Operand; (* its operands *) |
| 1281 | an: INT; (* how many *) |
| 1282 | asize, apfx: INT; |
| 1283 | awide, abyte: BOOL; |
| 1284 | aat: Diag.Pos; (* where it starts *) |
| 1285 | proc bad: ERROR; |
| 1286 | begin |
| 1287 | return Scan.markAt(aat, Diag.say("can't assemble {} with these operands", [Fmt.str(aw)])); |
| 1288 | end; |
| 1289 | |
| 1290 | (* an instruction of two operands: a register or memory, and a |
| 1291 | register (opcode toRM), the other way (fromRM), or an immediate |
| 1292 | (opImm /digit, or none if opImm < 0) *) |
| 1293 | proc two(toRM, fromRM, opImm, digit: INT): ERROR; |
| 1294 | var b: INT; |
| 1295 | begin |
| 1296 | b := 0; |
| 1297 | if abyte then b := 1; end; |
| 1298 | if an # 2 then return bad(); end; |
| 1299 | if ao[1].kind = oImm then |
| 1300 | if (ao[0].kind = oImm) or (opImm < 0) then return bad(); end; |
| 1301 | if (opImm = 81H) and not abyte and (ao[1].v >= -128) and (ao[1].v <= 127) then |
| 1302 | rm(apfx, awide, 83H, digit, ao[0], false); (* a byte, sign-extended *) |
| 1303 | Chunk.put1(INT(ao[1].v & 0FFH)); |
| 1304 | else |
| 1305 | rm(apfx, awide, opImm - b, digit, ao[0], false); |
| 1306 | putImmV(ao[1].v, asize); |
| 1307 | end; |
| 1308 | elsif (ao[1].kind = oReg) and ((ao[0].kind = oReg) or (ao[0].kind = oMem)) then |
| 1309 | rm(apfx, awide, toRM - b, ao[1].r, ao[0], abyte); |
| 1310 | elsif (ao[0].kind = oReg) and (ao[1].kind = oMem) then |
| 1311 | rm(apfx, awide, fromRM - b, ao[0].r, ao[1], abyte); |
| 1312 | else |
| 1313 | return bad(); |
| 1314 | end; |
| 1315 | return nil; |
| 1316 | end; |
| 1317 | |
| 1318 | (* whether operand i is an xmm register or a memory operand *) |
| 1319 | proc isXM(i: INT): BOOL; |
| 1320 | begin |
| 1321 | return (ao[i].kind = oXmm) or (ao[i].kind = oMem); |
| 1322 | end; |
| 1323 | |
| 1324 | (* whether operand i is a general register or a memory operand, for an |
| 1325 | xmm register's partner *) |
| 1326 | proc isRMem(i: INT): BOOL; |
| 1327 | begin |
| 1328 | return (ao[i].kind = oReg) or (ao[i].kind = oMem); |
| 1329 | end; |
| 1330 | |
| 1331 | (* b as 1 for true *) |
| 1332 | proc bit(f: BOOL): INT; |
| 1333 | begin |
| 1334 | if f then return 1; end; |
| 1335 | return 0; |
| 1336 | end; |
| 1337 | |
| 1338 | (* The instruction w with its operands, in ao and an, apart from the |
| 1339 | jumps to labels and db, which assemble does *) |
| 1340 | proc instruction(): ERROR; |
| 1341 | var k: INT; |
| 1342 | begin |
| 1343 | if wordIndex(aw, Alus) >= 0 then |
| 1344 | k := wordIndex(aw, Alus); |
| 1345 | return two(k * 8 + 1, k * 8 + 3, 81H, k); |
| 1346 | elsif aw = "mov" then |
| 1347 | (* to a control register, 0F 22, and from one, 0F 20: the control |
| 1348 | register in ModRM's reg, always 64 bits wide with no REX.W *) |
| 1349 | if (an = 2) and (ao[0].kind = oCr) and (ao[1].kind = oReg) and awide then |
| 1350 | opR(0, false, 220FH, ao[0].r, ao[1].r, false); |
| 1351 | return nil; |
| 1352 | elsif (an = 2) and (ao[0].kind = oReg) and (ao[1].kind = oCr) and awide then |
| 1353 | opR(0, false, 200FH, ao[1].r, ao[0].r, false); |
| 1354 | return nil; |
| 1355 | elsif (an = 2) and ((ao[0].kind = oCr) or (ao[1].kind = oCr)) then |
| 1356 | return bad(); |
| 1357 | end; |
| 1358 | if (an = 2) and (ao[0].kind = oReg) and (ao[1].kind = oImm) and (asize >= 4) then |
| 1359 | if awide then |
| 1360 | movRI(ao[0].r, ao[1].v); |
| 1361 | else |
| 1362 | prefix(0, false, 0, NoIndex, ao[0].r, false); Chunk.put1(0B8H + (ao[0].r & 7)); Chunk.put4(INT(ao[1].v & 0FFFFFFFFH)); |
| 1363 | end; |
| 1364 | return nil; |
| 1365 | end; |
| 1366 | return two(89H, 8BH, 0C7H, 0); |
| 1367 | elsif aw = "test" then |
| 1368 | return two(85H, 85H, 0F7H, 0); |
| 1369 | elsif aw = "xchg" then |
| 1370 | return two(87H, 87H, -1, 0); |
| 1371 | elsif (aw = "cmpxchg") or (aw = "xadd") then |
| 1372 | if (an # 2) or (ao[1].kind # oReg) then return bad(); end; |
| 1373 | if aw = "xadd" then k := 0C10FH; else k := 0B10FH; end; |
| 1374 | rm(apfx, awide, k - (bit(abyte) shl 8), ao[1].r, ao[0], abyte); |
| 1375 | elsif aw = "lea" then |
| 1376 | if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind # oMem) then return bad(); end; |
| 1377 | rm(apfx, awide, 8DH, ao[0].r, ao[1], false); |
| 1378 | elsif (aw = "movzx") or (aw = "movsx") or (aw = "movsxd") then |
| 1379 | if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) then return bad(); end; |
| 1380 | if aw = "movsxd" then |
| 1381 | k := 63H; |
| 1382 | elsif ao[1].size = 1 then |
| 1383 | k := 0B60FH; |
| 1384 | elsif ao[1].size = 2 then |
| 1385 | k := 0B70FH; |
| 1386 | else |
| 1387 | return bad(); |
| 1388 | end; |
| 1389 | if aw = "movsx" then k := k + 0800H; end; |
| 1390 | rm(apfx, awide, k, ao[0].r, ao[1], ao[1].size = 1); |
| 1391 | elsif ((wordIndex(aw, Unaries) >= 0) and (an = 1)) or (aw = "inc") or (aw = "dec") then |
| 1392 | if ao[0].kind = oImm then return bad(); end; |
| 1393 | if aw = "inc" then |
| 1394 | rm(apfx, awide, 0FFH - bit(abyte), 0, ao[0], abyte); |
| 1395 | elsif aw = "dec" then |
| 1396 | rm(apfx, awide, 0FFH - bit(abyte), 1, ao[0], abyte); |
| 1397 | else |
| 1398 | rm(apfx, awide, 0F7H - bit(abyte), wordIndex(aw, Unaries) + 2, ao[0], abyte); |
| 1399 | end; |
| 1400 | elsif aw = "imul" then |
| 1401 | if (an < 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) then return bad(); end; |
| 1402 | if an = 2 then |
| 1403 | rm(apfx, awide, 0AF0FH, ao[0].r, ao[1], false); |
| 1404 | else |
| 1405 | rm(apfx, awide, 69H, ao[0].r, ao[1], false); |
| 1406 | putImmV(ao[2].v, asize); |
| 1407 | end; |
| 1408 | elsif wordIndex(aw, Shifts) >= 0 then |
| 1409 | k := wordIndex(aw, Shifts); |
| 1410 | if k = 6 then k := 4; end; (* sal is shl *) |
| 1411 | if (an # 2) or (ao[0].kind = oImm) then return bad(); end; |
| 1412 | if ao[1].kind = oImm then |
| 1413 | rm(apfx, awide, 0C1H - bit(abyte), k, ao[0], abyte); |
| 1414 | Chunk.put1(INT(ao[1].v & 0FFH)); |
| 1415 | elsif (ao[1].kind = oReg) and (ao[1].r = RCX) and (ao[1].size = 1) then |
| 1416 | rm(apfx, awide, 0D3H - bit(abyte), k, ao[0], abyte); |
| 1417 | else |
| 1418 | return bad(); |
| 1419 | end; |
| 1420 | elsif (aw = "push") or (aw = "pop") then |
| 1421 | if (aw = "push") and (an = 1) and (ao[0].kind = oImm) then |
| 1422 | (* a constant, sign-extended to 64 bits: 6A and a byte if it fits |
| 1423 | one, else 68 and four bytes *) |
| 1424 | if (ao[0].v < -80000000H) or (ao[0].v > 7FFFFFFFH) then return bad(); end; |
| 1425 | if (ao[0].v >= -128) and (ao[0].v <= 127) then |
| 1426 | Chunk.put1(6AH); Chunk.put1(INT(ao[0].v & 0FFH)); |
| 1427 | else |
| 1428 | Chunk.put1(68H); Chunk.put4(INT(ao[0].v & 0FFFFFFFFH)); |
| 1429 | end; |
| 1430 | return nil; |
| 1431 | end; |
| 1432 | if (an # 1) or (ao[0].kind # oReg) or not awide then return bad(); end; |
| 1433 | if aw = "push" then push(ao[0].r); else pop(ao[0].r); end; |
| 1434 | elsif (aw = "call") or (aw = "jmp") then |
| 1435 | if (an # 1) or (ao[0].kind = oImm) then return bad(); end; |
| 1436 | if ao[0].kind = oName then |
| 1437 | callChunk(ao[0].m.chunk); (* call p, as Gen's calls; jmp p is read as a jump *) |
| 1438 | elsif aw = "call" then |
| 1439 | rm(0, false, 0FFH, 2, ao[0], false); |
| 1440 | else |
| 1441 | rm(0, false, 0FFH, 4, ao[0], false); |
| 1442 | end; |
| 1443 | elsif Str.hasPrefix(aw, "set") and (cond(part(aw, 3, 3)) >= 0) then |
| 1444 | if (an # 1) or (ao[0].kind = oImm) or (asize # 1) then return bad(); end; |
| 1445 | rm(0, false, 900FH + (cond(part(aw, 3, 3)) shl 8), 0, ao[0], true); |
| 1446 | elsif Str.hasPrefix(aw, "cmov") and (cond(part(aw, 4, 3)) >= 0) then |
| 1447 | if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) then return bad(); end; |
| 1448 | rm(apfx, awide, 400FH + (cond(part(aw, 4, 3)) shl 8), ao[0].r, ao[1], false); |
| 1449 | elsif (LEN(aw) > 2) and (wordIndex(aw[0..LEN(aw) - 2], SseOps) >= 0) |
| 1450 | and ((aw[LEN(aw) - 2..LEN(aw)] = "sd") or (aw[LEN(aw) - 2..LEN(aw)] = "ss")) then |
| 1451 | (* sqrtsd, addss and the like: xmm, xmm or memory *) |
| 1452 | if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; |
| 1453 | k := 0FH | (SseCodes[wordIndex(aw[0..LEN(aw) - 2], SseOps)] shl 8); |
| 1454 | rm(realPfx(aw[LEN(aw) - 1] = 's'), false, k, ao[0].r, ao[1], false); |
| 1455 | elsif wordIndex(aw, ShuffleOps) >= 0 then |
| 1456 | k := wordIndex(aw, ShuffleOps); |
| 1457 | if (an < 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; |
| 1458 | if aw = "palignr" then |
| 1459 | if (an # 3) or (ao[2].kind # oImm) or (ao[2].v < 0) or (ao[2].v > 255) then return bad(); end; |
| 1460 | elsif aw = "sha256rnds2" then |
| 1461 | if (an = 3) and ((ao[2].kind # oXmm) or (ao[2].r # 0)) then return bad(); end; |
| 1462 | if an > 3 then return bad(); end; |
| 1463 | elsif an # 2 then |
| 1464 | return bad(); |
| 1465 | end; |
| 1466 | rm(ShufflePfx[k], false, ShuffleCodes[k], ao[0].r, ao[1], false); |
| 1467 | if aw = "palignr" then Chunk.put1(INT(ao[2].v & 0FFH)); end; |
| 1468 | elsif wordIndex(aw, PackedOps) >= 0 then |
| 1469 | if (an < 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; |
| 1470 | k := wordIndex(aw, PackedOps); |
| 1471 | rm(66H, false, 0FH | (PackedCodes[k] shl 8), ao[0].r, ao[1], false); |
| 1472 | if aw = "pshufd" then |
| 1473 | if (an # 3) or (ao[2].kind # oImm) then return bad(); end; |
| 1474 | Chunk.put1(INT(ao[2].v & 0FFH)); |
| 1475 | elsif an # 2 then |
| 1476 | return bad(); |
| 1477 | end; |
| 1478 | elsif (aw = "movdqu") or (aw = "movdqa") then |
| 1479 | (* the load is 6F and the store 7F; movdqu is F3, movdqa 66 *) |
| 1480 | if aw = "movdqu" then k := 0F3H; else k := 66H; end; |
| 1481 | if (an = 2) and (ao[0].kind = oXmm) and isXM(1) then |
| 1482 | rm(k, false, 6F0FH, ao[0].r, ao[1], false); |
| 1483 | elsif (an = 2) and (ao[0].kind = oMem) and (ao[1].kind = oXmm) then |
| 1484 | rm(k, false, 7F0FH, ao[1].r, ao[0], false); |
| 1485 | else |
| 1486 | return bad(); |
| 1487 | end; |
| 1488 | elsif aw = "pmovmskb" then |
| 1489 | if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind # oXmm) then return bad(); end; |
| 1490 | rm(66H, false, 0D70FH, ao[0].r, ao[1], false); |
| 1491 | elsif wordIndex(aw, BitOps) >= 0 then |
| 1492 | if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) or (asize < 4) then return bad(); end; |
| 1493 | k := wordIndex(aw, BitOps); |
| 1494 | rm(BitPfx[k], awide, BitCodes[k], ao[0].r, ao[1], false); |
| 1495 | elsif wordIndex(aw, MemOps) >= 0 then |
| 1496 | if (an # 1) or (ao[0].kind # oMem) then return bad(); end; |
| 1497 | k := wordIndex(aw, MemOps); |
| 1498 | rm(0, false, MemCodes[k], MemDigits[k], ao[0], false); |
| 1499 | elsif aw = "ltr" then |
| 1500 | (* 0F 00 /3 with a 16-bit register: written out, since opcode |
| 1501 | ends an opcode at its first zero byte *) |
| 1502 | if (an # 1) or (ao[0].kind # oReg) or (asize # 2) then return bad(); end; |
| 1503 | prefix(0, false, 0, NoIndex, ao[0].r, false); |
| 1504 | Chunk.put1(0FH); Chunk.put1(0); Chunk.put1(0C0H | (3 shl 3) | (ao[0].r & 7)); |
| 1505 | elsif (aw = "in") or (aw = "out") then |
| 1506 | (* in al, ax or eax from a port, a byte or dx: E4 or EC; out the |
| 1507 | other way, E6 or EE; one more for a word or more. The size is |
| 1508 | the register's, not the first operand's, which is dx for out *) |
| 1509 | if aw = "in" then k := 1; else k := 0; end; (* the port's operand *) |
| 1510 | if (an # 2) or (ao[1 - k].kind # oReg) or (ao[1 - k].r # RAX) or (ao[1 - k].size = 8) then return bad(); end; |
| 1511 | if ao[1 - k].size = 2 then Chunk.put1(66H); end; |
| 1512 | if ao[k].kind = oImm then |
| 1513 | if (ao[k].v < 0) or (ao[k].v > 255) then return bad(); end; |
| 1514 | Chunk.put1(0E4H + 2 * (1 - k) + bit(ao[1 - k].size # 1)); |
| 1515 | Chunk.put1(INT(ao[k].v)); |
| 1516 | elsif (ao[k].kind = oReg) and (ao[k].r = RDX) and (ao[k].size = 2) then |
| 1517 | Chunk.put1(0ECH + 2 * (1 - k) + bit(ao[1 - k].size # 1)); |
| 1518 | else |
| 1519 | return bad(); |
| 1520 | end; |
| 1521 | elsif (aw = "movsd") or (aw = "movss") then |
| 1522 | if aw = "movsd" then k := 0F2H; else k := 0F3H; end; |
| 1523 | if (an = 2) and (ao[0].kind = oXmm) and isXM(1) then |
| 1524 | rm(k, false, 100FH, ao[0].r, ao[1], false); |
| 1525 | elsif (an = 2) and (ao[0].kind = oMem) and (ao[1].kind = oXmm) then |
| 1526 | rm(k, false, 110FH, ao[1].r, ao[0], false); |
| 1527 | else |
| 1528 | return bad(); |
| 1529 | end; |
| 1530 | elsif (aw = "movq") or (aw = "movd") then |
| 1531 | if (an = 2) and (ao[0].kind = oXmm) and isRMem(1) then |
| 1532 | rm(66H, aw = "movq", 6E0FH, ao[0].r, ao[1], false); |
| 1533 | elsif (an = 2) and (ao[1].kind = oXmm) and isRMem(0) then |
| 1534 | rm(66H, aw = "movq", 7E0FH, ao[1].r, ao[0], false); |
| 1535 | else |
| 1536 | return bad(); |
| 1537 | end; |
| 1538 | elsif (aw = "ucomisd") or (aw = "ucomiss") then |
| 1539 | if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; |
| 1540 | if aw = "ucomisd" then |
| 1541 | rm(66H, false, 2E0FH, ao[0].r, ao[1], false); |
| 1542 | else |
| 1543 | rm(0, false, 2E0FH, ao[0].r, ao[1], false); |
| 1544 | end; |
| 1545 | elsif (aw = "cvtsi2sd") or (aw = "cvtsi2ss") then |
| 1546 | if (an # 2) or (ao[0].kind # oXmm) or not isRMem(1) then return bad(); end; |
| 1547 | rm(realPfx(aw = "cvtsi2ss"), ao[1].size # 4, 2A0FH, ao[0].r, ao[1], false); |
| 1548 | elsif (aw = "cvttsd2si") or (aw = "cvttss2si") then |
| 1549 | if (an # 2) or (ao[0].kind # oReg) or not isXM(1) then return bad(); end; |
| 1550 | rm(realPfx(aw = "cvttss2si"), awide, 2C0FH, ao[0].r, ao[1], false); |
| 1551 | elsif (aw = "cvtsd2ss") or (aw = "cvtss2sd") then |
| 1552 | if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; |
| 1553 | rm(realPfx(aw = "cvtss2sd"), false, 5A0FH, ao[0].r, ao[1], false); |
| 1554 | else |
| 1555 | k := 0; |
| 1556 | while (k < LEN(Plain)) and (Plain[k] # aw) do INC(k); end; |
| 1557 | if (k = LEN(Plain)) or (an # 0) then |
| 1558 | return Scan.markAt(aat, Diag.say("unknown instruction {}", [Fmt.str(aw)])); |
| 1559 | end; |
| 1560 | opcode(PlainOp[k]); |
| 1561 | end; |
| 1562 | return nil; |
| 1563 | end; |
| 1564 | |
| 1565 | (* Whether the identifier at the scan is a register name, not a label *) |
| 1566 | proc atRegister(): BOOL; |
| 1567 | var r, size: INT; |
| 1568 | begin |
| 1569 | return isReg(asmWord(), r, size); |
| 1570 | end; |
| 1571 | |
| 1572 | (* One instruction or label, as assemble reads it *) |
| 1573 | proc statement(constExpr: ConstFn; nameFn: NameFn): ERROR; |
| 1574 | var |
| 1575 | i, k, c, cc, first: INT; wasListing: BOOL; start, at: Diag.Pos; v: I64; |
| 1576 | begin |
| 1577 | aat := Scan.tokPos(); |
| 1578 | aw := asmWord(); |
| 1579 | if LEN(aw) = 0 then return Scan.mark("expected an instruction"); end; |
| 1580 | Scan.next; |
| 1581 | if Scan.tok = Scan.sColon then |
| 1582 | sayAt(Chunk.pc, Diag.say("{}:", [Fmt.str(aw)])); |
| 1583 | k := try lab(Scan.intern(SYSTEM.PTR(aw), LEN(aw))); |
| 1584 | if lplaces[k] >= 0 then |
| 1585 | return Scan.markAt(aat, Diag.say("label {} is defined twice", [Fmt.str(aw)])); |
| 1586 | end; |
| 1587 | lplaces[k] := Chunk.pc; |
| 1588 | fixLink(lchains[k]); |
| 1589 | Scan.next; |
| 1590 | return nil; |
| 1591 | end; |
| 1592 | first := Chunk.pc; |
| 1593 | wasListing := listing; |
| 1594 | listing := false; (* the instruction is said as it is written *) |
| 1595 | if aw = "lock" then |
| 1596 | Chunk.put1(0F0H); |
| 1597 | aw := asmWord(); |
| 1598 | Scan.next; |
| 1599 | end; |
| 1600 | cc := -1; |
| 1601 | if aw = "db" then |
| 1602 | repeat |
| 1603 | start := Scan.tokPos(); |
| 1604 | v := try constExpr(true); |
| 1605 | if (v < -128) or (v > 255) then |
| 1606 | return Scan.markAt(Scan.spanFrom(start), Diag.say("expected a byte from 0 to 255, found {}", [Fmt.int(v)])); |
| 1607 | end; |
| 1608 | Chunk.put1(INT(v & 0FFH)); |
| 1609 | if Scan.tok # Scan.sComma then break; end; |
| 1610 | Scan.next; |
| 1611 | until false; |
| 1612 | elsif (aw[0] = 'j') and ((aw = "jmp") or (cond(part(aw, 1, 3)) >= 0)) and (Scan.tok = Scan.sIdent) and not atRegister() then |
| 1613 | (* a jump to a label, which is found first; else jmp p, to a |
| 1614 | procedure (a tail call) or a variable, by nameFn; else a label |
| 1615 | still, which isn't defined *) |
| 1616 | if aw # "jmp" then cc := cond(part(aw, 1, 3)); end; |
| 1617 | c := -1; |
| 1618 | if labelIndex(Scan.id) < 0 then |
| 1619 | at := Scan.tokPos(); |
| 1620 | c := try nameFn(); |
| 1621 | end; |
| 1622 | if c >= 0 then |
| 1623 | if cc >= 0 then return Scan.markAt(at, NameOnly); end; |
| 1624 | jmpChunk(c); |
| 1625 | else |
| 1626 | k := try lab(Scan.id); |
| 1627 | Scan.next; |
| 1628 | if lplaces[k] >= 0 then |
| 1629 | if cc < 0 then jmpBack(lplaces[k]); else jccBack(cc, lplaces[k]); end; |
| 1630 | elsif cc < 0 then |
| 1631 | jmp(lchains[k]); |
| 1632 | else |
| 1633 | jcc(cc, lchains[k]); |
| 1634 | end; |
| 1635 | end; |
| 1636 | else |
| 1637 | an := 0; |
| 1638 | if Scan.tok # Scan.sSemicolon then |
| 1639 | repeat |
| 1640 | if an > 2 then return Scan.mark("too many operands"); end; |
| 1641 | try operand(ao[an], constExpr, nameFn); |
| 1642 | INC(an); |
| 1643 | if Scan.tok # Scan.sComma then break; end; |
| 1644 | Scan.next; |
| 1645 | until false; |
| 1646 | end; |
| 1647 | (* a name stands only in call p and lea r, [v]; jmp p is read above *) |
| 1648 | for i := 0 to an - 1 do |
| 1649 | if ao[i].named and not (((aw = "call") and (ao[i].kind = oName)) or ((aw = "lea") and (i = 1))) then |
| 1650 | return Scan.markAt(ao[i].at, NameOnly); |
| 1651 | end; |
| 1652 | end; |
| 1653 | (* the size: a register's, or what the memory operand says *) |
| 1654 | asize := 8; |
| 1655 | for i := an - 1 to 0 by -1 do |
| 1656 | if (ao[i].kind = oReg) or ((ao[i].kind = oMem) and (ao[i].size # 0)) then asize := ao[i].size; end; |
| 1657 | end; |
| 1658 | if an > 0 then |
| 1659 | if (ao[0].kind = oReg) or ((ao[0].kind = oMem) and (ao[0].size # 0)) then asize := ao[0].size; end; |
| 1660 | end; |
| 1661 | awide := asize = 8; |
| 1662 | abyte := asize = 1; |
| 1663 | if asize = 2 then apfx := 66H; else apfx := 0; end; |
| 1664 | try instruction(); |
| 1665 | end; |
| 1666 | if Scan.tok # Scan.sSemicolon then return Scan.mark("expected \";\""); end; |
| 1667 | listing := wasListing; |
| 1668 | sayAt(first, Diag.source(Scan.spanFrom(aat))); |
| 1669 | Scan.next; |
| 1670 | return nil; |
| 1671 | end; |
| 1672 | |
| 1673 | proc assemble*(constExpr: ConstFn; nameFn: NameFn): ERROR; |
| 1674 | var k: INT; |
| 1675 | begin |
| 1676 | numLabels := 0; |
| 1677 | findLabels(); |
| 1678 | while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do |
| 1679 | if Scan.tok = Scan.sSemicolon then |
| 1680 | Scan.next; |
| 1681 | else |
| 1682 | try statement(constExpr, nameFn); |
| 1683 | end; |
| 1684 | end; |
| 1685 | for k := 0 to numLabels - 1 do |
| 1686 | if lplaces[k] < 0 then |
| 1687 | return Scan.markAt(lusedAt[k], Diag.say("label {} isn't defined", [Fmt.str(Scan.nameOf(lnames[k]))])); |
| 1688 | end; |
| 1689 | end; |
| 1690 | return nil; |
| 1691 | end; |
| 1692 | |
| 1693 | (* Whether s holds a space *) |
| 1694 | proc hasSpace(s: []const U8): BOOL; |
| 1695 | var i: INT; |
| 1696 | begin |
| 1697 | for i := 0 to LEN(s) - 1 do |
| 1698 | if s[i] = ' ' then return true; end; |
| 1699 | end; |
| 1700 | return false; |
| 1701 | end; |
| 1702 | |
| 1703 | (* n, in decimal, at least w wide, as write(n: w) *) |
| 1704 | proc padded(n, w: INT); |
| 1705 | var s: []const U8; i: INT; |
| 1706 | begin |
| 1707 | s := Diag.say("{}", [Fmt.int(n)]); |
| 1708 | for i := LEN(s) to w - 1 do Out.str(" "); end; |
| 1709 | Out.str(s); |
| 1710 | end; |
| 1711 | |
| 1712 | (* The listing, raz -S: each live procedure of the module in the order |
| 1713 | it was made, each line of source it comes from, and under it the |
| 1714 | instructions made for it, at their offsets in the procedure; a jump |
| 1715 | within it says where to by its offset too. *) |
| 1716 | proc list*(home: INT); |
| 1717 | var order: ^INT; n, i, j, k, c, t, line: INT; text: []const U8; |
| 1718 | begin |
| 1719 | order := ^INT(Scan.grab(Chunk.numChunks * SIZE(INT))); |
| 1720 | n := 0; |
| 1721 | for c := 0 to Chunk.numChunks - 1 do |
| 1722 | if Chunk.chunks[c].live and (Chunk.chunks[c].sec = Chunk.secText) and (Chunk.chunks[c].home = home) then |
| 1723 | i := n; |
| 1724 | while (i > 0) and (Chunk.chunks[order[i - 1]].start > Chunk.chunks[c].start) do |
| 1725 | order[i] := order[i - 1]; |
| 1726 | DEC(i); |
| 1727 | end; |
| 1728 | order[i] := c; |
| 1729 | INC(n); |
| 1730 | end; |
| 1731 | end; |
| 1732 | i := 0; j := 0; |
| 1733 | for k := 0 to n - 1 do |
| 1734 | c := order[k]; |
| 1735 | Out.ln; |
| 1736 | Out.str(Chunk.chunkName(c)); Out.str(":\n"); |
| 1737 | line := -1; |
| 1738 | while (i < numSaid) and (said[i].at < Chunk.chunks[c].start) do INC(i); end; |
| 1739 | while (i < numSaid) and (said[i].at < Chunk.chunks[c].start + Chunk.chunks[c].size) do |
| 1740 | while (j < Chunk.numLines) and (Chunk.lines[j].at <= said[i].at) do |
| 1741 | if (Chunk.lines[j].chunk = c) and (Chunk.lines[j].line # line) then |
| 1742 | line := Chunk.lines[j].line; |
| 1743 | padded(line, 6); |
| 1744 | Out.str(" "); |
| 1745 | Out.str(Diag.lineText(Scan.nameOf(Chunk.lines[j].path), line)); |
| 1746 | Out.ln; |
| 1747 | end; |
| 1748 | INC(j); |
| 1749 | end; |
| 1750 | text := said[i].text; |
| 1751 | t := said[i].at; |
| 1752 | if (text = "jmp") or (text = "call") then |
| 1753 | text := Diag.say("{} {}", [Fmt.str(text), Fmt.int(t + 5 + Chunk.get4At(t + 1) - Chunk.chunks[c].start)]); |
| 1754 | elsif (text[0] = 'j') and not hasSpace(text) then |
| 1755 | text := Diag.say("{} {}", [Fmt.str(text), Fmt.int(t + 6 + Chunk.get4At(t + 2) - Chunk.chunks[c].start)]); |
| 1756 | end; |
| 1757 | padded(t - Chunk.chunks[c].start, 14); |
| 1758 | Out.str(" "); |
| 1759 | Out.str(text); |
| 1760 | Out.ln; |
| 1761 | INC(i); |
| 1762 | end; |
| 1763 | end; |
| 1764 | end; |
| 1765 | |
| 1766 | begin |
| 1767 | listing := false; |
| 1768 | numSaid := 0; |
| 1769 | saidCap := 0; |
| 1770 | said := nil; |
| 1771 | end X64. |