fix(asm): match go tool asm encodings and strictness

This commit is contained in:
2026-08-28 19:55:25 +02:00
parent 19a26e049b
commit 78b12dd427
4 changed files with 106 additions and 6 deletions
+47 -4
View File
@@ -51,13 +51,23 @@ func (e *enc) encodeMov(ops []Operand, size int) error {
// Integer scalar XMM moves: MOVQ with an XMM operand is the SSE2
// packed-quadword move, NOT a GPR move: mem→xmm encodes as F3 0F 7E
// (reg = dst, no REX.W — the Go assembler's form), xmm→mem as
// 66 0F D6 (rm = xmm). MOVL is the packed-dword move instead:
// 66 0F 6E load, 66 0F 7E store. A GPR-move fallback would silently
// emit REX.W 8B with the wrong operand meaning.
// 66 0F D6 (rm = xmm). Register forms against a GPR use the MOVD
// opcodes with REX.W instead: 66 REX.W 0F 6E (gpr→xmm) and
// 66 REX.W 0F 7E (xmm→gpr); the memory opcodes with a register r/m
// would be undefined forms. MOVL is the packed-dword move:
// 66 0F 6E load, 66 0F 7E store, no REX.W. A GPR-move fallback would
// silently emit REX.W 8B with the wrong operand meaning.
_, srcVec := vecReg(src)
dstReg, dstVec := vecReg(dst)
if srcVec || dstVec {
if dstVec {
if g, ok := src.(Reg); ok && !g.isVec() {
i := &instr{prefix: 0x66, opcode: []byte{0x0F, 0x6E}, modrm: -1, sib: -1, rexW: size == 8}
if err := setRM(i, dstReg, src, 8); err != nil {
return err
}
return e.emit(i)
}
i := &instr{prefix: 0xF3, opcode: []byte{0x0F, 0x7E}, modrm: -1, sib: -1}
if size == 4 {
i.prefix = 0x66
@@ -72,6 +82,13 @@ func (e *enc) encodeMov(ops []Operand, size int) error {
if !srcIsXMM || !srcXMM.isVec() {
return fmt.Errorf("MOV: store needs an XMM source")
}
if g, ok := dst.(Reg); ok && !g.isVec() {
i := &instr{prefix: 0x66, opcode: []byte{0x0F, 0x7E}, modrm: -1, sib: -1, rexW: size == 8}
if err := setRM(i, srcXMM, dst, 8); err != nil {
return err
}
return e.emit(i)
}
i := &instr{prefix: 0x66, opcode: []byte{0x0F, 0xD6}, modrm: -1, sib: -1}
if size == 4 {
i.opcode = []byte{0x0F, 0x7E}
@@ -199,7 +216,13 @@ func (e *enc) encodeALU(op struct {
}
src, dst := ops[0], ops[1]
// CMP never takes its immediate first: the Go assembler rejects
// CMPL $0, AX outright (only CMPL AX, $0 is legal, unlike TEST and the
// writing ALU ops whose immediate is naturally the source).
if imm, ok := src.(Imm); ok {
if op.digit == 7 {
return fmt.Errorf("CMP immediate must be the second operand (reg, $imm)")
}
return e.encodeALUImm(op.digit, dst, int64(imm), size)
}
@@ -290,6 +313,15 @@ func (e *enc) encodeALUImm(digit int, dst Operand, imm int64, size int) error {
i.imm = []byte{byte(int8(imm))}
return e.emit(i)
}
// 0x81 /digit, imm16/imm32 — or the Go assembler's accumulator short
// form (opcode+5, no ModR/M) when the destination is AX/AL, which it
// prefers over the generic form exactly here.
if r, ok := dst.(Reg); ok && r.idx == 0 {
accOp := map[int]byte{0: 0x05, 1: 0x0D, 2: 0x15, 3: 0x1D, 4: 0x25, 5: 0x2D, 6: 0x35, 7: 0x3D}[digit]
i := newInstr(size, []byte{accOp})
i.imm = immediate(imm, size, false)
return e.emit(i)
}
// 0x81 /digit, imm16/imm32.
i := newInstr(size, []byte{0x81})
if err := setRMDigit(i, digit, dst, size); err != nil {
@@ -307,7 +339,18 @@ func (e *enc) encodeTest(ops []Operand, size int) error {
}
src, dst := ops[0], ops[1]
if imm, ok := src.(Imm); ok {
// TEST r/m, imm: 0xF6 (8-bit) / 0xF7 /0.
// TEST r/m, imm: 0xF6 (8-bit) / 0xF7 /0 — but the Go assembler
// always uses the accumulator forms (A8/A9, no ModR/M) when the
// register operand is AL/AX, whatever the immediate's width.
if r, ok := dst.(Reg); ok && r.idx == 0 {
op := byte(0xA9)
if size == 1 {
op = 0xA8
}
i := newInstr(size, []byte{op})
i.imm = immediate(int64(imm), size, false)
return e.emit(i)
}
op := byte(0xF7)
if size == 1 {
op = 0xF6