fix(lint): exempt shift counts, SETcc and ABIInternal from false positives
Assisted-by: GLM 5.3 Flash
This commit is contained in:
+111
-8
@@ -338,10 +338,8 @@ func lintText(t *ast.Text, tab *arch.Table, archKnown bool, cfg Config, macros m
|
||||
}
|
||||
|
||||
if isJump(cfg.Arch, upper) {
|
||||
for _, op := range st.Operands {
|
||||
if name, pos, ok := localLabelRef(op); ok && !tab.IsRegister(name) && !arch.IsPseudoReg(name) {
|
||||
referenced[name] = pos
|
||||
}
|
||||
if name, pos, ok := branchTargetRef(cfg.Arch, upper, st.Operands, tab); ok {
|
||||
referenced[name] = pos
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -649,17 +647,65 @@ func localLabelRef(op *ast.Operand) (string, token.Position, bool) {
|
||||
return sym.Name, op.Pos, true
|
||||
}
|
||||
|
||||
// branchTargetRef returns the local label a branch transfers control to: the
|
||||
// bare symbol in the destination position, the last operand, since that is
|
||||
// where the Plan 9 branch target sits. A register-named target is a
|
||||
// register-indirect branch (JMP AX, arm64 BR R5, riscv64 JALR X6, loong64
|
||||
// JIRL R1) and yields no reference, unless the encoder reads the target
|
||||
// positionally (positionalBranchTarget): there a label may legitimately
|
||||
// collide with a register alias, riscv64 ZERO being the ABI name of X0, and
|
||||
// a label named zero is ordinary code.
|
||||
func branchTargetRef(a arch.Arch, upper string, ops []*ast.Operand, tab *arch.Table) (string, token.Position, bool) {
|
||||
if len(ops) == 0 {
|
||||
return "", token.Position{}, false
|
||||
}
|
||||
name, pos, ok := localLabelRef(ops[len(ops)-1])
|
||||
if !ok {
|
||||
return "", token.Position{}, false
|
||||
}
|
||||
if !positionalBranchTarget(a, upper) && (tab.IsRegister(name) || arch.IsPseudoReg(name)) {
|
||||
return "", token.Position{}, false
|
||||
}
|
||||
return name, pos, true
|
||||
}
|
||||
|
||||
// positionalBranchTarget reports whether the encoder reads a bare-symbol
|
||||
// operand of the branch as its label target from a fixed position, without
|
||||
// consulting the register file. The riscv64 branch, JMP and JAL encoders do
|
||||
// (labelFromOperand in asm/riscv_assemble.go), as do the loong64 branch,
|
||||
// BFPT/BFPF and jump encoders (l64Label in asm/loong64_assemble.go). amd64
|
||||
// never does, because a bare register operand to JMP/CALL/Jcc is a
|
||||
// register-indirect branch; nor do the register-indirect forms of the RISC
|
||||
// families (arm64 BR/BLR, riscv64 JALR/JR, loong64 JIRL).
|
||||
func positionalBranchTarget(a arch.Arch, upper string) bool {
|
||||
switch a {
|
||||
case arch.RISCV:
|
||||
return riscvBranches[upper] || upper == "JMP" || upper == "JAL"
|
||||
case arch.LOONG64:
|
||||
return loong64Branches[upper] || upper == "JMP" || upper == "B" ||
|
||||
upper == "JAL" || upper == "BL"
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// riscvBranches and loong64Branches are the conditional-branch mnemonics; they
|
||||
// are listed explicitly rather than matched by a "B" prefix so that bit-manip
|
||||
// instructions (BCLR, BSET, …) are never mistaken for branches.
|
||||
// instructions (BCLR, BSET, …) are never mistaken for branches. The sets
|
||||
// mirror the encoder's own branch cases: the B-type table entries
|
||||
// (riscv_encode.go), the branch-zero pseudos and the reversed branches
|
||||
// BGT/BGTU/BLE/BLEU (riscv_assemble.go), and for loong64 the 16-bit branch
|
||||
// table plus the single-register forms of l64branch21Table (BEQZ/BNEZ and the
|
||||
// floating-point branches BFPT/BFPF).
|
||||
var riscvBranches = map[string]bool{
|
||||
"BEQ": true, "BNE": true, "BLT": true, "BGE": true, "BLTU": true, "BGEU": true,
|
||||
"BEQZ": true, "BNEZ": true, "BLEZ": true, "BGEZ": true, "BLTZ": true, "BGTZ": true,
|
||||
"BGT": true, "BGTU": true, "BLE": true, "BLEU": true,
|
||||
}
|
||||
|
||||
var loong64Branches = map[string]bool{
|
||||
"BEQ": true, "BNE": true, "BLT": true, "BGE": true, "BLTU": true, "BGEU": true,
|
||||
"BLEZ": true, "BLTZ": true, "BGEZ": true, "BGTZ": true,
|
||||
"BEQZ": true, "BNEZ": true, "BFPT": true, "BFPF": true,
|
||||
}
|
||||
|
||||
// isJump reports whether the mnemonic is any branch.
|
||||
@@ -676,7 +722,8 @@ func isJump(a arch.Arch, upper string) bool {
|
||||
upper == "JR" || upper == "BR"
|
||||
case arch.LOONG64:
|
||||
return upper == "CALL" || loong64Branches[upper] ||
|
||||
upper == "JIRL" || upper == "JMP" || upper == "BR"
|
||||
upper == "JIRL" || upper == "JMP" || upper == "BR" ||
|
||||
upper == "B" || upper == "JAL" || upper == "BL"
|
||||
default: // amd64
|
||||
return upper == "CALL" || strings.HasPrefix(upper, "J")
|
||||
}
|
||||
@@ -692,7 +739,8 @@ func isUnconditionalJump(a arch.Arch, upper string) bool {
|
||||
return upper == "JMP" || upper == "J" || upper == "JAL" ||
|
||||
upper == "JALR" || upper == "JR" || upper == "BR"
|
||||
case arch.LOONG64:
|
||||
return upper == "JMP" || upper == "JIRL" || upper == "BR"
|
||||
return upper == "JMP" || upper == "JIRL" || upper == "BR" || upper == "B" ||
|
||||
upper == "JAL" || upper == "BL"
|
||||
default:
|
||||
return upper == "JMP"
|
||||
}
|
||||
@@ -792,6 +840,43 @@ func isSPReg(op *ast.Operand, a arch.Arch) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// shiftRotateBases are the shift and rotate mnemonics without their width
|
||||
// suffix. These are the instructions whose encoder path (encodeShift) reads
|
||||
// the count from the first operand.
|
||||
var shiftRotateBases = map[string]bool{
|
||||
"SHL": true, "SHR": true, "SAR": true, "SAL": true,
|
||||
"ROL": true, "ROR": true, "RCL": true, "RCR": true,
|
||||
}
|
||||
|
||||
// isShiftCountOperand reports whether operand i of mnem is the shift count.
|
||||
// The ISA fixes the shift/rotate count register at CL: the D2/D3 group (and
|
||||
// C0/C1 for immediates) encode the count outside the ModRM register field,
|
||||
// so the count operand is 8-bit by definition no matter how wide the data is.
|
||||
// The count arrives as the first of the two operands; the one-operand form
|
||||
// does not exist.
|
||||
func isShiftCountOperand(mnem string, i, nops int) bool {
|
||||
if nops != 2 || i != 0 {
|
||||
return false
|
||||
}
|
||||
if shiftRotateBases[mnem] {
|
||||
return true
|
||||
}
|
||||
if len(mnem) > 1 {
|
||||
switch mnem[len(mnem)-1] {
|
||||
case 'Q', 'L', 'W', 'B':
|
||||
return shiftRotateBases[mnem[:len(mnem)-1]]
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// isSetcc reports whether the mnemonic is a SETcc: SET plus a condition code.
|
||||
// The membership test is the encoder's own SET dispatch, which asm.Encodable
|
||||
// mirrors.
|
||||
func isSetcc(mnem string) bool {
|
||||
return strings.HasPrefix(mnem, "SET") && asm.Encodable(mnem)
|
||||
}
|
||||
|
||||
// checkRegisterWidth detects amd64 register-width mismatches. The naming
|
||||
// truth of the Go assembler governs: AX, BX, CX, DX, SI, DI, BP, SP and
|
||||
// R8-R15 ARE the 64-bit register names (there are no separate EAX/RAX
|
||||
@@ -802,6 +887,14 @@ func isSPReg(op *ast.Operand, a arch.Arch) bool {
|
||||
// register (EAX under the gasm alias extension, or a byte form), and byte
|
||||
// registers in L/W operations.
|
||||
func checkRegisterWidth(mnem string, ops []*ast.Operand) string {
|
||||
// A SETcc stores one byte: the destination is an 8-bit register or an
|
||||
// 8-bit memory location by definition (0F 90+cc), whichever condition it
|
||||
// tests. The trailing letter of spellings like SETPL or SETEQ is part of
|
||||
// the condition code, not an operand width, so the whole family is
|
||||
// exempt from the suffix logic.
|
||||
if isSetcc(mnem) {
|
||||
return ""
|
||||
}
|
||||
// Determine expected width from mnemonic suffix.
|
||||
var expected int // 0=unknown, 8/4/2/1=bytes
|
||||
switch {
|
||||
@@ -816,10 +909,20 @@ func checkRegisterWidth(mnem string, ops []*ast.Operand) string {
|
||||
default:
|
||||
return "" // no suffix, can't determine width
|
||||
}
|
||||
for _, op := range ops {
|
||||
for i, op := range ops {
|
||||
if op.Kind != ast.OpAddr || op.Addr.Sym == nil {
|
||||
continue
|
||||
}
|
||||
// Only a bare register carries a width to compare: frame and static
|
||||
// symbol references (ch+8(FP), foo(SB)) and memory operands are not
|
||||
// registers even when their name collides with one.
|
||||
if op.Addr.Sym.Pseudo != "" || op.Addr.Base != "" || op.Addr.Index != "" {
|
||||
continue
|
||||
}
|
||||
// The shift/rotate count is exempt: fixed at 8 bits by the ISA.
|
||||
if isShiftCountOperand(mnem, i, len(ops)) {
|
||||
continue
|
||||
}
|
||||
name := strings.ToLower(op.Addr.Sym.Name)
|
||||
regWidth := amd64RegWidth(name)
|
||||
if regWidth == 0 {
|
||||
|
||||
Reference in New Issue
Block a user