Assembler: the rule for when an instruction joins; paddd, punpcklqdq, punpckhqdq, pshufb, palignr and the SHA-NI three
| @@ -1173,19 +1173,39 @@ | ||
| 1173 | 1173 | `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations on 16 bytes |
| 1174 | 1174 | at a time, with `movdqu`, `movdqa` and `pmovmskb` beside them, and |
| 1175 | 1175 | `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, `lzcnt`, |
| 1176 | -`popcnt`). A kernel's instructions are there too, though the code | |
| 1177 | -generator never makes them: `cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, | |
| 1178 | -`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 | |
| 1176 | +`popcnt`; `ShuffleOps` the same SSE operands with a three-byte opcode: | |
| 1177 | +`pshufb`, `palignr`, `sha256rnds2`, `sha256msg1`, `sha256msg2`). A | |
| 1178 | +kernel's instructions are there too, though the code generator never makes them: `cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, | |
| 1179 | +`wrmsr`, `wbinvd`, `cld` and `std` in `Plain`; `push` of a | |
| 1180 | +constant (6A and a byte when it fits a signed byte, else 68 and four, | |
| 1181 | +sign-extended to 64 bits); `lgdt`, `lidt`, `invlpg`, `xsave` and | |
| 1179 | 1182 | `xrstor`, of one memory operand, in `MemOps`; `ltr` of a 16-bit |
| 1180 | 1183 | register; `mov` to and from `cr0`, `cr2`, `cr3`, `cr4` and `cr8` (an |
| 1181 | 1184 | operand of its own kind, `oCr`); and `in` and `out`, of `al`, `ax` or |
| 1182 | 1185 | `eax` and a port, a byte or `dx`. Each was checked against GNU `as`, and |
| 1183 | -`test/run/AsmKernel.raz` reads their bytes back. Labels are chains of jumps, as below. `db` gives bytes it | |
| 1184 | -doesn't know. An error in an instruction is returned as an `ERROR`, | |
| 1185 | -which `assemble` passes up. An offset that doesn't fit in 32 bits | |
| 1186 | -(`mem`) can't be returned, since an operand has nowhere to give it: it | |
| 1187 | -is shown, and the compile goes on to its end. `RegNames` and `CondNames` | |
| 1186 | +`test/run/AsmKernel.raz` reads their bytes back. `opcode` writes the | |
| 1187 | +three-byte opcodes, `0F 38` and `0F 3A`, whole, since `pshufb`'s last | |
| 1188 | +byte is 00, where any other opcode ends at its last non-zero byte. | |
| 1189 | +Labels are chains of jumps, as below. `db` gives bytes it doesn't | |
| 1190 | +know. An error in an instruction is returned as an `ERROR`, which | |
| 1191 | +`assemble` passes up. An offset that doesn't fit in 32 bits (`mem`) | |
| 1192 | +can't be returned, since an operand has nowhere to give it: it is | |
| 1193 | +shown, and the compile goes on to its end. `RegNames` and `CondNames` | |
| 1188 | 1194 | are the names of the registers and conditions. |
| 1195 | + | |
| 1196 | +*When an instruction joins the assembler.* The report leaves the | |
| 1197 | +instructions to the implementation, and `db` gives any other, which | |
| 1198 | +stays the answer for an instruction no program uses twice. An | |
| 1199 | +instruction joins when a program writes it in `db`, it is a member of | |
| 1200 | +a family the tables already hold (a code in `PackedOps`, an entry in | |
| 1201 | +`Plain`, `ShuffleOps`, ...), and its bytes are checked against GNU | |
| 1202 | +`as` (`.intel_syntax noprefix`, read back with `objdump`) in a test in | |
| 1203 | +`test/run/`, as `AsmKernel.raz` and `AsmSimd.raz` do, each form in | |
| 1204 | +registers and in memory, with the extended registers. AES-NI, | |
| 1205 | +`pclmulqdq` and AVX wait for a program. `sha256rnds2` takes `xmm0` as | |
| 1206 | +its implied third operand, which may be written or left out, as GNU | |
| 1207 | +`as` and NASM allow: `sha256rnds2 xmm1, xmm2, xmm0` and `sha256rnds2 | |
| 1208 | +xmm1, xmm2` are the same bytes; any other third operand is an error. | |
| 1189 | 1209 | |
| 1190 | 1210 | The listing, `raz -S`: with `listing` set (`setListing`), each routine |
| 1191 | 1211 | of the encoder says, with `say`, the instruction it is about to write, |
| @@ -116,10 +116,16 @@ | ||
| 116 | 116 | |
| 117 | 117 | (* Encoding. An instruction is: a legacy prefix, REX, the opcode, ModRM, |
| 118 | 118 | SIB and displacement. Opcodes of more than one byte are given with |
| 119 | - their first byte in the low eight bits. *) | |
| 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). *) | |
| 120 | 121 | |
| 121 | 122 | proc opcode(op: INT); |
| 122 | 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; | |
| 123 | 129 | repeat |
| 124 | 130 | Chunk.put1(op & 0FFH); |
| 125 | 131 | op := op shr 8; |
| @@ -1135,8 +1141,14 @@ | ||
| 1135 | 1141 | SseCodes: [7]INT = [51H, 58H, 59H, 5CH, 5DH, 5EH, 5FH]; |
| 1136 | 1142 | (* SSE2 integer instructions that work on 16 bytes at a time: 66 0F code, |
| 1137 | 1143 | xmm and xmm or memory (pshufd also takes a byte) *) |
| 1138 | - PackedOps = " punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb "; | |
| 1139 | - PackedCodes: [10]INT = [60H, 61H, 74H, 70H, 0DBH, 0DFH, 0EBH, 0EFH, 0FCH, 0F8H]; | |
| 1144 | + PackedOps = " punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb paddd punpcklqdq punpckhqdq "; | |
| 1145 | + PackedCodes: [13]INT = [60H, 61H, 74H, 70H, 0DBH, 0DFH, 0EBH, 0EFH, 0FCH, 0F8H, 0FEH, 6CH, 6DH]; | |
| 1146 | + (* the same, xmm and xmm or memory, with a three-byte opcode: pshufb, palignr | |
| 1147 | + (which also takes a byte), and the SHA extensions. sha256rnds2 takes xmm0 | |
| 1148 | + as an implied third operand, written or not *) | |
| 1149 | + ShuffleOps = " pshufb palignr sha256rnds2 sha256msg1 sha256msg2 "; | |
| 1150 | + ShufflePfx: [5]INT = [66H, 66H, 0, 0, 0]; | |
| 1151 | + ShuffleCodes: [5]INT = [380FH, 0F3A0FH, 0CB380FH, 0CC380FH, 0CD380FH]; | |
| 1140 | 1152 | (* bit scans and counts: reg, reg or memory, with the prefix and code that tell them *) |
| 1141 | 1153 | BitOps = " bsf bsr tzcnt lzcnt popcnt "; |
| 1142 | 1154 | BitPfx: [5]INT = [0, 0, 0F3H, 0F3H, 0F3H]; |
| @@ -1345,6 +1357,19 @@ | ||
| 1345 | 1357 | if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; |
| 1346 | 1358 | k := 0FH | (SseCodes[wordIndex(aw[0..LEN(aw) - 2], SseOps)] shl 8); |
| 1347 | 1359 | rm(realPfx(aw[LEN(aw) - 1] = 's'), false, k, ao[0].r, ao[1], false); |
| 1360 | + elsif wordIndex(aw, ShuffleOps) >= 0 then | |
| 1361 | + k := wordIndex(aw, ShuffleOps); | |
| 1362 | + if (an < 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; | |
| 1363 | + if aw = "palignr" then | |
| 1364 | + if (an # 3) or (ao[2].kind # oImm) then return bad(); end; | |
| 1365 | + elsif aw = "sha256rnds2" then | |
| 1366 | + if (an = 3) and ((ao[2].kind # oXmm) or (ao[2].r # 0)) then return bad(); end; | |
| 1367 | + if an > 3 then return bad(); end; | |
| 1368 | + elsif an # 2 then | |
| 1369 | + return bad(); | |
| 1370 | + end; | |
| 1371 | + rm(ShufflePfx[k], false, ShuffleCodes[k], ao[0].r, ao[1], false); | |
| 1372 | + if aw = "palignr" then Chunk.put1(INT(ao[2].v & 0FFH)); end; | |
| 1348 | 1373 | elsif wordIndex(aw, PackedOps) >= 0 then |
| 1349 | 1374 | if (an < 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; |
| 1350 | 1375 | k := wordIndex(aw, PackedOps); |
| @@ -0,0 +1,31 @@ | ||
| 1 | +paddd xmm1, xmm2: 66 0F FE CA | |
| 2 | +paddd xmm9, xmm14: 66 45 0F FE CE | |
| 3 | +paddd xmm0, [rdi]: 66 0F FE 07 | |
| 4 | +paddd xmm10, [r12 + rcx * 8 + 400]: 66 45 0F FE 94 CC 90 01 00 00 | |
| 5 | +punpcklqdq xmm1, xmm2: 66 0F 6C CA | |
| 6 | +punpcklqdq xmm8, xmm3: 66 44 0F 6C C3 | |
| 7 | +punpcklqdq xmm2, [rax + 16]: 66 0F 6C 50 10 | |
| 8 | +punpckhqdq xmm1, xmm2: 66 0F 6D CA | |
| 9 | +punpckhqdq xmm3, xmm15: 66 41 0F 6D DF | |
| 10 | +punpckhqdq xmm7, [r9]: 66 41 0F 6D 39 | |
| 11 | +pshufb xmm1, xmm2: 66 0F 38 00 CA | |
| 12 | +pshufb xmm8, xmm9: 66 45 0F 38 00 C1 | |
| 13 | +pshufb xmm0, [rdi]: 66 0F 38 00 07 | |
| 14 | +pshufb xmm12, [rsp + 32]: 66 44 0F 38 00 64 24 20 | |
| 15 | +pshufb xmm3, [r13 + 8]: 66 41 0F 38 00 5D 08 | |
| 16 | +palignr xmm1, xmm2, 8: 66 0F 3A 0F CA 08 | |
| 17 | +palignr xmm9, xmm0, 0: 66 44 0F 3A 0F C8 00 | |
| 18 | +palignr xmm2, [rdi + 16], 4: 66 0F 3A 0F 57 10 04 | |
| 19 | +palignr xmm11, [r12], 15: 66 45 0F 3A 0F 1C 24 0F | |
| 20 | +sha256rnds2 xmm1, xmm2, xmm0: 0F 38 CB CA | |
| 21 | +sha256rnds2 xmm9, xmm3, xmm0: 44 0F 38 CB CB | |
| 22 | +sha256rnds2 xmm1, [rdi], xmm0: 0F 38 CB 0F | |
| 23 | +sha256rnds2 xmm2, [r9 + rbx * 4 + 64], xmm0: 41 0F 38 CB 54 99 40 | |
| 24 | +sha256rnds2 xmm1, xmm2: 0F 38 CB CA | |
| 25 | +sha256rnds2 xmm1, [rdi]: 0F 38 CB 0F | |
| 26 | +sha256msg1 xmm1, xmm2: 0F 38 CC CA | |
| 27 | +sha256msg1 xmm8, xmm15: 45 0F 38 CC C7 | |
| 28 | +sha256msg1 xmm3, [rsi + 32]: 0F 38 CC 5E 20 | |
| 29 | +sha256msg2 xmm1, xmm2: 0F 38 CD CA | |
| 30 | +sha256msg2 xmm0, xmm9: 41 0F 38 CD C1 | |
| 31 | +sha256msg2 xmm4, [r10 + 8]: 41 0F 38 CD 62 08 |
| @@ -0,0 +1,127 @@ | ||
| 1 | +module AsmSimd; | |
| 2 | +(* The packed, shuffle and SHA instructions of the assembler: each one's bytes, | |
| 3 | + read back from the procedure they are made in. Each was checked against | |
| 4 | + GNU as (Intel syntax, objdump). *) | |
| 5 | +import Out; | |
| 6 | +import SYSTEM; | |
| 7 | + | |
| 8 | +const | |
| 9 | + Count = 31; | |
| 10 | + Names: [Count][]const U8 = [ | |
| 11 | + "paddd xmm1, xmm2", | |
| 12 | + "paddd xmm9, xmm14", | |
| 13 | + "paddd xmm0, [rdi]", | |
| 14 | + "paddd xmm10, [r12 + rcx * 8 + 400]", | |
| 15 | + "punpcklqdq xmm1, xmm2", | |
| 16 | + "punpcklqdq xmm8, xmm3", | |
| 17 | + "punpcklqdq xmm2, [rax + 16]", | |
| 18 | + "punpckhqdq xmm1, xmm2", | |
| 19 | + "punpckhqdq xmm3, xmm15", | |
| 20 | + "punpckhqdq xmm7, [r9]", | |
| 21 | + "pshufb xmm1, xmm2", | |
| 22 | + "pshufb xmm8, xmm9", | |
| 23 | + "pshufb xmm0, [rdi]", | |
| 24 | + "pshufb xmm12, [rsp + 32]", | |
| 25 | + "pshufb xmm3, [r13 + 8]", | |
| 26 | + "palignr xmm1, xmm2, 8", | |
| 27 | + "palignr xmm9, xmm0, 0", | |
| 28 | + "palignr xmm2, [rdi + 16], 4", | |
| 29 | + "palignr xmm11, [r12], 15", | |
| 30 | + "sha256rnds2 xmm1, xmm2, xmm0", | |
| 31 | + "sha256rnds2 xmm9, xmm3, xmm0", | |
| 32 | + "sha256rnds2 xmm1, [rdi], xmm0", | |
| 33 | + "sha256rnds2 xmm2, [r9 + rbx * 4 + 64], xmm0", | |
| 34 | + "sha256rnds2 xmm1, xmm2", | |
| 35 | + "sha256rnds2 xmm1, [rdi]", | |
| 36 | + "sha256msg1 xmm1, xmm2", | |
| 37 | + "sha256msg1 xmm8, xmm15", | |
| 38 | + "sha256msg1 xmm3, [rsi + 32]", | |
| 39 | + "sha256msg2 xmm1, xmm2", | |
| 40 | + "sha256msg2 xmm0, xmm9", | |
| 41 | + "sha256msg2 xmm4, [r10 + 8]"]; | |
| 42 | + Sizes: [Count]INT = [ | |
| 43 | + 4, | |
| 44 | + 5, | |
| 45 | + 4, | |
| 46 | + 10, | |
| 47 | + 4, | |
| 48 | + 5, | |
| 49 | + 5, | |
| 50 | + 4, | |
| 51 | + 5, | |
| 52 | + 5, | |
| 53 | + 5, | |
| 54 | + 6, | |
| 55 | + 5, | |
| 56 | + 8, | |
| 57 | + 7, | |
| 58 | + 6, | |
| 59 | + 7, | |
| 60 | + 7, | |
| 61 | + 8, | |
| 62 | + 4, | |
| 63 | + 5, | |
| 64 | + 4, | |
| 65 | + 7, | |
| 66 | + 4, | |
| 67 | + 4, | |
| 68 | + 4, | |
| 69 | + 5, | |
| 70 | + 5, | |
| 71 | + 4, | |
| 72 | + 5, | |
| 73 | + 6]; | |
| 74 | + | |
| 75 | +proc simd; | |
| 76 | +code | |
| 77 | + paddd xmm1, xmm2; | |
| 78 | + paddd xmm9, xmm14; | |
| 79 | + paddd xmm0, [rdi]; | |
| 80 | + paddd xmm10, [r12 + rcx * 8 + 400]; | |
| 81 | + punpcklqdq xmm1, xmm2; | |
| 82 | + punpcklqdq xmm8, xmm3; | |
| 83 | + punpcklqdq xmm2, [rax + 16]; | |
| 84 | + punpckhqdq xmm1, xmm2; | |
| 85 | + punpckhqdq xmm3, xmm15; | |
| 86 | + punpckhqdq xmm7, [r9]; | |
| 87 | + pshufb xmm1, xmm2; | |
| 88 | + pshufb xmm8, xmm9; | |
| 89 | + pshufb xmm0, [rdi]; | |
| 90 | + pshufb xmm12, [rsp + 32]; | |
| 91 | + pshufb xmm3, [r13 + 8]; | |
| 92 | + palignr xmm1, xmm2, 8; | |
| 93 | + palignr xmm9, xmm0, 0; | |
| 94 | + palignr xmm2, [rdi + 16], 4; | |
| 95 | + palignr xmm11, [r12], 15; | |
| 96 | + sha256rnds2 xmm1, xmm2, xmm0; | |
| 97 | + sha256rnds2 xmm9, xmm3, xmm0; | |
| 98 | + sha256rnds2 xmm1, [rdi], xmm0; | |
| 99 | + sha256rnds2 xmm2, [r9 + rbx * 4 + 64], xmm0; | |
| 100 | + sha256rnds2 xmm1, xmm2; | |
| 101 | + sha256rnds2 xmm1, [rdi]; | |
| 102 | + sha256msg1 xmm1, xmm2; | |
| 103 | + sha256msg1 xmm8, xmm15; | |
| 104 | + sha256msg1 xmm3, [rsi + 32]; | |
| 105 | + sha256msg2 xmm1, xmm2; | |
| 106 | + sha256msg2 xmm0, xmm9; | |
| 107 | + sha256msg2 xmm4, [r10 + 8]; | |
| 108 | + ret; | |
| 109 | +end simd; | |
| 110 | + | |
| 111 | +proc main*; | |
| 112 | +var p: proc; b: ^U8; i, j, at: INT; | |
| 113 | +begin | |
| 114 | + p := simd; | |
| 115 | + b := ^U8(p); | |
| 116 | + at := 0; | |
| 117 | + for i := 0 to Count - 1 do | |
| 118 | + Out.str(Names[i]); Out.str(":"); | |
| 119 | + for j := 0 to Sizes[i] - 1 do | |
| 120 | + Out.str(" "); Out.hex(U64(b[at + j]), 2); | |
| 121 | + end; | |
| 122 | + Out.ln; | |
| 123 | + at := at + Sizes[i]; | |
| 124 | + end; | |
| 125 | +end; | |
| 126 | + | |
| 127 | +end AsmSimd. |