fix(asm): route the arm64 logical immediates to ZR through REGTMP
Assisted-by: GLM 5.3 Flash
This commit is contained in:
1 parent
fbdad8424f
commit
d853432dba
2 files changed
+110
-44
No files matched your search
+72
-44
@@ -311,6 +311,37 @@ func arm64InstrSize(instr *ast.Instr, fi arm64FrameInfo, pos int) int {
|
||||
case "MOV", "MOVD", "MOVW", "MOVWU", "MOVH", "MOVHU", "MOVB", "MOVBU",
|
||||
"FMOVS", "FMOVD", "FMOVQ":
|
||||
return arm64MovSize(mnem, ops, fi)
|
||||
}
|
||||
// The logical-immediate family: one word on the bitmask fast path (a
|
||||
// real destination, or the flags-only TST spellings), two words for the
|
||||
// materialisation the ZR destinations and the non-bitmask immediates
|
||||
// take. Mirrors encodeARM64DPSR's decision exactly.
|
||||
if len(ops) >= 2 && len(ops) <= 3 && isImmOperand(ops[0]) {
|
||||
switch mnem {
|
||||
case "AND", "ANDW", "ANDS", "ANDSW", "ORR", "ORRW", "EOR", "EORW",
|
||||
"BIC", "BICW", "BICS", "BICSW", "ORN", "ORNW", "EON", "EONW",
|
||||
"TST", "TSTW":
|
||||
if v, ok := arm64ImmOperandValue(ops[0]); ok {
|
||||
switch mnem {
|
||||
case "BIC", "BICW", "BICS", "BICSW", "ORN", "ORNW", "EON", "EONW":
|
||||
v = ^v
|
||||
}
|
||||
width := 64
|
||||
if strings.HasSuffix(mnem, "W") {
|
||||
width = 32
|
||||
}
|
||||
rd := 31
|
||||
if mnem != "TST" && mnem != "TSTW" {
|
||||
rd = arm64RegNum(operandRegName(ops[len(ops)-1]))
|
||||
}
|
||||
if _, _, _, bc := a64LogicalImm(v, width); bc && (rd != 31 || strings.HasPrefix(mnem, "TST")) {
|
||||
return 4
|
||||
}
|
||||
return 8
|
||||
}
|
||||
}
|
||||
}
|
||||
switch mnem {
|
||||
case "ADD", "ADDW", "SUB", "SUBW", "CMP", "CMPW", "CMN", "CMNW",
|
||||
"ADDS", "ADDSW", "SUBS", "SUBSW":
|
||||
if len(ops) >= 2 && isImmOperand(ops[0]) {
|
||||
@@ -839,48 +870,6 @@ func encodeARM64DPSR(mnem string, baseOp uint32, ops []*ast.Operand) ([]byte, er
|
||||
if strings.HasSuffix(mnem, "W") {
|
||||
width = 32
|
||||
}
|
||||
n, immr, imms, ok := a64LogicalImm(v, width)
|
||||
if !ok {
|
||||
// Beyond the bitmask immediates the toolchain materialises
|
||||
// the constant into REGTMP (R27) and uses the register form
|
||||
// (asm7.go cases 62 and 13). BIC/ORN/EON read the written
|
||||
// value, so the materialisation uses v before any inversion.
|
||||
written := v
|
||||
if inverted {
|
||||
written = ^v
|
||||
}
|
||||
width := mnem
|
||||
if strings.HasSuffix(mnem, "W") {
|
||||
width = "MOVW"
|
||||
} else {
|
||||
width = "MOVD"
|
||||
}
|
||||
mw, merr := encodeARM64LoadImm(27, written, width)
|
||||
var rn, rd int
|
||||
switch len(ops) {
|
||||
case 3:
|
||||
rn = arm64RegNum(operandRegName(ops[1]))
|
||||
rd = arm64RegNum(operandRegName(ops[2]))
|
||||
default:
|
||||
rd = arm64RegNum(operandRegName(ops[1]))
|
||||
rn = rd
|
||||
}
|
||||
if isCmp {
|
||||
rd = 31
|
||||
}
|
||||
if merr != nil || rn < 0 || rd < 0 {
|
||||
return nil, fmt.Errorf("%s: immediate %q is not a logical (bitmask) immediate", mnem, strings.Join(strings.Fields(ops[0].Raw), " "))
|
||||
}
|
||||
// The register tail against SP takes the extended form, like the
|
||||
// plain register path below.
|
||||
tail := baseOp | 27<<16 | uint32(rn)<<5 | uint32(rd)
|
||||
if opt, spok := arm64SpExtendOpt(mnem, ops[1:]); spok {
|
||||
tail = baseOp | 1<<21 | opt<<13 | 27<<16 | uint32(rn)<<5 | uint32(rd)
|
||||
}
|
||||
return append(mw, a64wordLE(tail)...), nil
|
||||
}
|
||||
opc := (baseOp >> 29) & 7
|
||||
sf := (baseOp >> 31) & 1
|
||||
var rn, rd int
|
||||
switch len(ops) {
|
||||
case 3:
|
||||
@@ -890,11 +879,50 @@ func encodeARM64DPSR(mnem string, baseOp uint32, ops []*ast.Operand) ([]byte, er
|
||||
rd = arm64RegNum(operandRegName(ops[1]))
|
||||
rn = rd
|
||||
}
|
||||
if isCmp {
|
||||
// CMP/CMN/TST write the flags alone: the destination is ZR
|
||||
// whatever the spelling says.
|
||||
rd = 31
|
||||
}
|
||||
if rn < 0 || rd < 0 {
|
||||
return nil, fmt.Errorf("invalid register operand in %s", mnem)
|
||||
}
|
||||
return a64wordLE(sf<<31 | opc<<29 | 0x24<<23 | n<<22 | immr<<16 | imms<<10 |
|
||||
uint32(rn)<<5 | uint32(rd)), nil
|
||||
n, immr, imms, ok := a64LogicalImm(v, width)
|
||||
// The toolchain's logical-immediate rows take a real destination
|
||||
// only (omovconst guards the bitmask path with rt != REGZERO): a
|
||||
// non-flag-setting logical to ZR materialises the constant into
|
||||
// REGTMP (R27) and takes the register form. The flags-only TST
|
||||
// spellings keep the fast path: ANDS ZR, Rn, #imm is their form.
|
||||
if ok && (rd != 31 || isCmp) {
|
||||
opc := (baseOp >> 29) & 7
|
||||
sf := (baseOp >> 31) & 1
|
||||
return a64wordLE(sf<<31 | opc<<29 | 0x24<<23 | n<<22 | immr<<16 | imms<<10 |
|
||||
uint32(rn)<<5 | uint32(rd)), nil
|
||||
}
|
||||
// Beyond the bitmask immediates, and for the ZR destinations, the
|
||||
// toolchain materialises the constant into REGTMP and uses the
|
||||
// register form (asm7.go cases 62 and 13). BIC/ORN/EON read the
|
||||
// written value, so the materialisation uses v before any
|
||||
// inversion.
|
||||
written := v
|
||||
if inverted {
|
||||
written = ^v
|
||||
}
|
||||
mwMnem := "MOVD"
|
||||
if strings.HasSuffix(mnem, "W") {
|
||||
mwMnem = "MOVW"
|
||||
}
|
||||
mw, merr := encodeARM64LoadImm(27, written, mwMnem)
|
||||
if merr != nil {
|
||||
return nil, fmt.Errorf("%s: immediate %q is not a logical (bitmask) immediate", mnem, strings.Join(strings.Fields(ops[0].Raw), " "))
|
||||
}
|
||||
// The register tail against SP takes the extended form, like the
|
||||
// plain register path below.
|
||||
tail := baseOp | 27<<16 | uint32(rn)<<5 | uint32(rd)
|
||||
if opt, spok := arm64SpExtendOpt(mnem, ops[1:]); spok {
|
||||
tail = baseOp | 1<<21 | opt<<13 | 27<<16 | uint32(rn)<<5 | uint32(rd)
|
||||
}
|
||||
return append(mw, a64wordLE(tail)...), nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user