From 013c05d6e768fe7e2d304becf0021045b87c1be2 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:08:43 +0000 Subject: [PATCH] Assembler: the rule for when an instruction joins; paddd, punpcklqdq, punpckhqdq, pshufb, palignr and the SHA-NI three --- diff --git a/doc/compiler.md b/doc/compiler.md index 8f27133..9ea2873 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1173,19 +1173,39 @@ `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside them, and `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, `lzcnt`, -`popcnt`). A kernel's instructions are there too, though the code -generator never makes them: `cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, -`wrmsr`, `wbinvd`, `cld` and `std` in `Plain`; `push` of a constant (6A and a byte when it fits a signed byte, else 68 and four, sign-extended to 64 bits); `lgdt`, `lidt`, `invlpg`, `xsave` and +`popcnt`; `ShuffleOps` the same SSE operands with a three-byte opcode: +`pshufb`, `palignr`, `sha256rnds2`, `sha256msg1`, `sha256msg2`). A +kernel's instructions are there too, though the code generator never makes them: `cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, +`wrmsr`, `wbinvd`, `cld` and `std` in `Plain`; `push` of a +constant (6A and a byte when it fits a signed byte, else 68 and four, +sign-extended to 64 bits); `lgdt`, `lidt`, `invlpg`, `xsave` and `xrstor`, of one memory operand, in `MemOps`; `ltr` of a 16-bit register; `mov` to and from `cr0`, `cr2`, `cr3`, `cr4` and `cr8` (an operand of its own kind, `oCr`); and `in` and `out`, of `al`, `ax` or `eax` and a port, a byte or `dx`. Each was checked against GNU `as`, and -`test/run/AsmKernel.raz` reads their bytes back. Labels are chains of jumps, as below. `db` gives bytes it -doesn't know. An error in an instruction is returned as an `ERROR`, -which `assemble` passes up. An offset that doesn't fit in 32 bits -(`mem`) can't be returned, since an operand has nowhere to give it: it -is shown, and the compile goes on to its end. `RegNames` and `CondNames` +`test/run/AsmKernel.raz` reads their bytes back. `opcode` writes the +three-byte opcodes, `0F 38` and `0F 3A`, whole, since `pshufb`'s last +byte is 00, where any other opcode ends at its last non-zero byte. +Labels are chains of jumps, as below. `db` gives bytes it doesn't +know. An error in an instruction is returned as an `ERROR`, which +`assemble` passes up. An offset that doesn't fit in 32 bits (`mem`) +can't be returned, since an operand has nowhere to give it: it is +shown, and the compile goes on to its end. `RegNames` and `CondNames` are the names of the registers and conditions. + +*When an instruction joins the assembler.* The report leaves the +instructions to the implementation, and `db` gives any other, which +stays the answer for an instruction no program uses twice. An +instruction joins when a program writes it in `db`, it is a member of +a family the tables already hold (a code in `PackedOps`, an entry in +`Plain`, `ShuffleOps`, ...), and its bytes are checked against GNU +`as` (`.intel_syntax noprefix`, read back with `objdump`) in a test in +`test/run/`, as `AsmKernel.raz` and `AsmSimd.raz` do, each form in +registers and in memory, with the extended registers. AES-NI, +`pclmulqdq` and AVX wait for a program. `sha256rnds2` takes `xmm0` as +its implied third operand, which may be written or left out, as GNU +`as` and NASM allow: `sha256rnds2 xmm1, xmm2, xmm0` and `sha256rnds2 +xmm1, xmm2` are the same bytes; any other third operand is an error. The listing, `raz -S`: with `listing` set (`setListing`), each routine of the encoder says, with `say`, the instruction it is about to write, diff --git a/src/X64.raz b/src/X64.raz index f2c05d6..4fe0d04 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -116,10 +116,16 @@ (* Encoding. An instruction is: a legacy prefix, REX, the opcode, ModRM, SIB and displacement. Opcodes of more than one byte are given with - their first byte in the low eight bits. *) + their first byte in the low eight bits; one ends at its last non-zero + byte, but for the three-byte ones (opcode below). *) proc opcode(op: INT); begin + (* 0F 38 and 0F 3A begin a three-byte opcode, whose last byte may be 00 *) + if (op & 0FDFFH) = 380FH then + Chunk.put1(op & 0FFH); Chunk.put1((op shr 8) & 0FFH); Chunk.put1((op shr 16) & 0FFH); + return; + end; repeat Chunk.put1(op & 0FFH); op := op shr 8; @@ -1135,8 +1141,14 @@ SseCodes: [7]INT = [51H, 58H, 59H, 5CH, 5DH, 5EH, 5FH]; (* SSE2 integer instructions that work on 16 bytes at a time: 66 0F code, xmm and xmm or memory (pshufd also takes a byte) *) - PackedOps = " punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb "; - PackedCodes: [10]INT = [60H, 61H, 74H, 70H, 0DBH, 0DFH, 0EBH, 0EFH, 0FCH, 0F8H]; + PackedOps = " punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb paddd punpcklqdq punpckhqdq "; + PackedCodes: [13]INT = [60H, 61H, 74H, 70H, 0DBH, 0DFH, 0EBH, 0EFH, 0FCH, 0F8H, 0FEH, 6CH, 6DH]; + (* the same, xmm and xmm or memory, with a three-byte opcode: pshufb, palignr + (which also takes a byte), and the SHA extensions. sha256rnds2 takes xmm0 + as an implied third operand, written or not *) + ShuffleOps = " pshufb palignr sha256rnds2 sha256msg1 sha256msg2 "; + ShufflePfx: [5]INT = [66H, 66H, 0, 0, 0]; + ShuffleCodes: [5]INT = [380FH, 0F3A0FH, 0CB380FH, 0CC380FH, 0CD380FH]; (* bit scans and counts: reg, reg or memory, with the prefix and code that tell them *) BitOps = " bsf bsr tzcnt lzcnt popcnt "; BitPfx: [5]INT = [0, 0, 0F3H, 0F3H, 0F3H]; @@ -1345,6 +1357,19 @@ if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; k := 0FH | (SseCodes[wordIndex(aw[0..LEN(aw) - 2], SseOps)] shl 8); rm(realPfx(aw[LEN(aw) - 1] = 's'), false, k, ao[0].r, ao[1], false); + elsif wordIndex(aw, ShuffleOps) >= 0 then + k := wordIndex(aw, ShuffleOps); + if (an < 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; + if aw = "palignr" then + if (an # 3) or (ao[2].kind # oImm) then return bad(); end; + elsif aw = "sha256rnds2" then + if (an = 3) and ((ao[2].kind # oXmm) or (ao[2].r # 0)) then return bad(); end; + if an > 3 then return bad(); end; + elsif an # 2 then + return bad(); + end; + rm(ShufflePfx[k], false, ShuffleCodes[k], ao[0].r, ao[1], false); + if aw = "palignr" then Chunk.put1(INT(ao[2].v & 0FFH)); end; elsif wordIndex(aw, PackedOps) >= 0 then if (an < 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; k := wordIndex(aw, PackedOps); diff --git a/test/run/AsmSimd.expect b/test/run/AsmSimd.expect new file mode 100644 index 0000000..14fabbf --- /dev/null +++ b/test/run/AsmSimd.expect @@ -0,0 +1,31 @@ +paddd xmm1, xmm2: 66 0F FE CA +paddd xmm9, xmm14: 66 45 0F FE CE +paddd xmm0, [rdi]: 66 0F FE 07 +paddd xmm10, [r12 + rcx * 8 + 400]: 66 45 0F FE 94 CC 90 01 00 00 +punpcklqdq xmm1, xmm2: 66 0F 6C CA +punpcklqdq xmm8, xmm3: 66 44 0F 6C C3 +punpcklqdq xmm2, [rax + 16]: 66 0F 6C 50 10 +punpckhqdq xmm1, xmm2: 66 0F 6D CA +punpckhqdq xmm3, xmm15: 66 41 0F 6D DF +punpckhqdq xmm7, [r9]: 66 41 0F 6D 39 +pshufb xmm1, xmm2: 66 0F 38 00 CA +pshufb xmm8, xmm9: 66 45 0F 38 00 C1 +pshufb xmm0, [rdi]: 66 0F 38 00 07 +pshufb xmm12, [rsp + 32]: 66 44 0F 38 00 64 24 20 +pshufb xmm3, [r13 + 8]: 66 41 0F 38 00 5D 08 +palignr xmm1, xmm2, 8: 66 0F 3A 0F CA 08 +palignr xmm9, xmm0, 0: 66 44 0F 3A 0F C8 00 +palignr xmm2, [rdi + 16], 4: 66 0F 3A 0F 57 10 04 +palignr xmm11, [r12], 15: 66 45 0F 3A 0F 1C 24 0F +sha256rnds2 xmm1, xmm2, xmm0: 0F 38 CB CA +sha256rnds2 xmm9, xmm3, xmm0: 44 0F 38 CB CB +sha256rnds2 xmm1, [rdi], xmm0: 0F 38 CB 0F +sha256rnds2 xmm2, [r9 + rbx * 4 + 64], xmm0: 41 0F 38 CB 54 99 40 +sha256rnds2 xmm1, xmm2: 0F 38 CB CA +sha256rnds2 xmm1, [rdi]: 0F 38 CB 0F +sha256msg1 xmm1, xmm2: 0F 38 CC CA +sha256msg1 xmm8, xmm15: 45 0F 38 CC C7 +sha256msg1 xmm3, [rsi + 32]: 0F 38 CC 5E 20 +sha256msg2 xmm1, xmm2: 0F 38 CD CA +sha256msg2 xmm0, xmm9: 41 0F 38 CD C1 +sha256msg2 xmm4, [r10 + 8]: 41 0F 38 CD 62 08 diff --git a/test/run/AsmSimd.raz b/test/run/AsmSimd.raz new file mode 100644 index 0000000..5fa46b9 --- /dev/null +++ b/test/run/AsmSimd.raz @@ -0,0 +1,127 @@ +module AsmSimd; +(* The packed, shuffle and SHA instructions of the assembler: each one's bytes, + read back from the procedure they are made in. Each was checked against + GNU as (Intel syntax, objdump). *) +import Out; +import SYSTEM; + +const + Count = 31; + Names: [Count][]const U8 = [ + "paddd xmm1, xmm2", + "paddd xmm9, xmm14", + "paddd xmm0, [rdi]", + "paddd xmm10, [r12 + rcx * 8 + 400]", + "punpcklqdq xmm1, xmm2", + "punpcklqdq xmm8, xmm3", + "punpcklqdq xmm2, [rax + 16]", + "punpckhqdq xmm1, xmm2", + "punpckhqdq xmm3, xmm15", + "punpckhqdq xmm7, [r9]", + "pshufb xmm1, xmm2", + "pshufb xmm8, xmm9", + "pshufb xmm0, [rdi]", + "pshufb xmm12, [rsp + 32]", + "pshufb xmm3, [r13 + 8]", + "palignr xmm1, xmm2, 8", + "palignr xmm9, xmm0, 0", + "palignr xmm2, [rdi + 16], 4", + "palignr xmm11, [r12], 15", + "sha256rnds2 xmm1, xmm2, xmm0", + "sha256rnds2 xmm9, xmm3, xmm0", + "sha256rnds2 xmm1, [rdi], xmm0", + "sha256rnds2 xmm2, [r9 + rbx * 4 + 64], xmm0", + "sha256rnds2 xmm1, xmm2", + "sha256rnds2 xmm1, [rdi]", + "sha256msg1 xmm1, xmm2", + "sha256msg1 xmm8, xmm15", + "sha256msg1 xmm3, [rsi + 32]", + "sha256msg2 xmm1, xmm2", + "sha256msg2 xmm0, xmm9", + "sha256msg2 xmm4, [r10 + 8]"]; + Sizes: [Count]INT = [ + 4, + 5, + 4, + 10, + 4, + 5, + 5, + 4, + 5, + 5, + 5, + 6, + 5, + 8, + 7, + 6, + 7, + 7, + 8, + 4, + 5, + 4, + 7, + 4, + 4, + 4, + 5, + 5, + 4, + 5, + 6]; + +proc simd; +code + paddd xmm1, xmm2; + paddd xmm9, xmm14; + paddd xmm0, [rdi]; + paddd xmm10, [r12 + rcx * 8 + 400]; + punpcklqdq xmm1, xmm2; + punpcklqdq xmm8, xmm3; + punpcklqdq xmm2, [rax + 16]; + punpckhqdq xmm1, xmm2; + punpckhqdq xmm3, xmm15; + punpckhqdq xmm7, [r9]; + pshufb xmm1, xmm2; + pshufb xmm8, xmm9; + pshufb xmm0, [rdi]; + pshufb xmm12, [rsp + 32]; + pshufb xmm3, [r13 + 8]; + palignr xmm1, xmm2, 8; + palignr xmm9, xmm0, 0; + palignr xmm2, [rdi + 16], 4; + palignr xmm11, [r12], 15; + sha256rnds2 xmm1, xmm2, xmm0; + sha256rnds2 xmm9, xmm3, xmm0; + sha256rnds2 xmm1, [rdi], xmm0; + sha256rnds2 xmm2, [r9 + rbx * 4 + 64], xmm0; + sha256rnds2 xmm1, xmm2; + sha256rnds2 xmm1, [rdi]; + sha256msg1 xmm1, xmm2; + sha256msg1 xmm8, xmm15; + sha256msg1 xmm3, [rsi + 32]; + sha256msg2 xmm1, xmm2; + sha256msg2 xmm0, xmm9; + sha256msg2 xmm4, [r10 + 8]; + ret; +end simd; + +proc main*; +var p: proc; b: ^U8; i, j, at: INT; +begin + p := simd; + b := ^U8(p); + at := 0; + for i := 0 to Count - 1 do + Out.str(Names[i]); Out.str(":"); + for j := 0 to Sizes[i] - 1 do + Out.str(" "); Out.hex(U64(b[at + j]), 2); + end; + Out.ln; + at := at + Sizes[i]; + end; +end; + +end AsmSimd.