fix(lint): guard unnamed TEXT and refresh the textflag table
This commit is contained in:
+22
-21
@@ -10,6 +10,7 @@ package lint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"sourcedock.dev/petrbalvin/gasm-devkit/arch"
|
||||
@@ -84,10 +85,12 @@ const (
|
||||
CodeReservedRegister = "reserved-register-write"
|
||||
)
|
||||
|
||||
// knownTextFlags are the flags recognised by the Go assembler's textflag.h.
|
||||
// knownTextFlags are the flags recognised by runtime/textflag.h, plus the
|
||||
// older REFLECTED spelling of REFLECTMETHOD.
|
||||
var knownTextFlags = map[string]bool{
|
||||
"NOSPLIT": true, "DUPOK": true, "RODATA": true, "NOPROF": true,
|
||||
"WRIT": true, "TLSBSS": true, "NOFRAME": true, "REFLECTED": true,
|
||||
"NOPTR": true, "WRAPPER": true, "NEEDCTXT": true, "TLSBSS": true,
|
||||
"NOFRAME": true, "REFLECTED": true, "REFLECTMETHOD": true,
|
||||
"TOPFRAME": true, "ABIWRAPPER": true,
|
||||
}
|
||||
|
||||
@@ -172,8 +175,9 @@ func File(f *ast.File, cfg Config) []Diagnostic {
|
||||
}
|
||||
}
|
||||
for _, fl := range flags {
|
||||
// Numeric flags (1, 8, 9) are legacy Go toolchain constants.
|
||||
if fl >= "0" && fl <= "9" {
|
||||
// Numeric flags are legacy textflag.h constants (1, 2, 8,
|
||||
// 9, 10, …); their meaning is decided at assembly time.
|
||||
if _, err := strconv.Atoi(fl); err == nil {
|
||||
continue
|
||||
}
|
||||
if !knownTextFlags[fl] {
|
||||
@@ -242,7 +246,7 @@ func lintText(t *ast.Text, tab *arch.Table, archKnown bool, cfg Config, macros m
|
||||
|
||||
// Unreachable code: a real instruction following a RET/UNDEF and
|
||||
// before any label, in a function whose control flow is fully
|
||||
// resolvable. Only RET/UNDEF are treated as terminators here — an
|
||||
// resolvable. Only RET/UNDEF are treated as terminators here, an
|
||||
// unconditional jump may be one entry of a hand-arranged branch
|
||||
// table (e.g. the generated callback tables), so it is not assumed
|
||||
// to make the following code dead. Pseudo-ops and macro invocations
|
||||
@@ -373,8 +377,10 @@ func lintText(t *ast.Text, tab *arch.Table, archKnown bool, cfg Config, macros m
|
||||
}
|
||||
|
||||
// Missing RET heuristic. Functions that invoke a macro are skipped: the
|
||||
// macro body (opaque to us) may supply the RET.
|
||||
if doLabelChecks && !cfg.Disable[CodeMissingRet] && instrCount > 0 && !hasRet && !lastTerminal && !hasMacro {
|
||||
// macro body (opaque to us) may supply the RET. A TEXT whose symbol is
|
||||
// missing (already reported by the parser) is skipped too.
|
||||
if doLabelChecks && !cfg.Disable[CodeMissingRet] && t.Name != nil &&
|
||||
instrCount > 0 && !hasRet && !lastTerminal && !hasMacro {
|
||||
out = append(out, Diagnostic{
|
||||
Pos: t.Keyword.Pos,
|
||||
Severity: Warning,
|
||||
@@ -506,7 +512,7 @@ func reachesRuntime(t *ast.Text) bool {
|
||||
}
|
||||
|
||||
// usesFPArgs reports whether a function references its arguments through the FP
|
||||
// pseudo-register — i.e. it uses the stack-based ABI0 layout, where the
|
||||
// pseudo-register, i.e. it uses the stack-based ABI0 layout, where the
|
||||
// declared argument size must match the signature.
|
||||
func usesFPArgs(t *ast.Text) bool {
|
||||
for _, s := range t.Body {
|
||||
@@ -572,7 +578,7 @@ func isMacroInvocation(mnem string, macros map[string]bool) bool {
|
||||
|
||||
// maskedEvex reports whether the instruction is a masked EVEX form: the
|
||||
// mnemonic carries a .Z suffix, or the operand list contains an opmask
|
||||
// register (K1–K7). Either way the operand count differs from the unmasked
|
||||
// register (K1-K7). Either way the operand count differs from the unmasked
|
||||
// form, so count checks are skipped.
|
||||
func maskedEvex(mnem string, ops []*ast.Operand) bool {
|
||||
if strings.Contains(mnem, ".") {
|
||||
@@ -587,7 +593,7 @@ func maskedEvex(mnem string, ops []*ast.Operand) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// isMaskReg reports whether name is an opmask register K0–K7.
|
||||
// isMaskReg reports whether name is an opmask register K0-K7.
|
||||
func isMaskReg(name string) bool {
|
||||
return len(name) == 2 && name[0] == 'K' && name[1] >= '0' && name[1] <= '7'
|
||||
}
|
||||
@@ -702,11 +708,6 @@ func stackDelta(t *ast.Text, a arch.Arch) int64 {
|
||||
}
|
||||
case arch.ARM64:
|
||||
switch upper {
|
||||
case "STP":
|
||||
// STP with pre-index: STP Xt1, Xt2, [SP, #imm]!
|
||||
if len(in.Operands) >= 3 && isSPReg(in.Operands[2], a) {
|
||||
// Could be pre-index decrement; skip for simplicity.
|
||||
}
|
||||
case "SUB":
|
||||
if len(in.Operands) >= 3 && isSPReg(in.Operands[2], a) {
|
||||
if in.Operands[1].Imm.HasVal {
|
||||
@@ -770,10 +771,10 @@ func isSPReg(op *ast.Operand, a arch.Arch) bool {
|
||||
|
||||
// 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
|
||||
// spellings in go tool asm), and AL–DH are the byte forms. The width comes
|
||||
// R8-R15 ARE the 64-bit register names (there are no separate EAX/RAX
|
||||
// spellings in go tool asm), and AL-DH are the byte forms. The width comes
|
||||
// from the opcode suffix, so an L/W operation over a canonical 64-bit name is
|
||||
// the normal, correct spelling — flagging it is pure noise on real kernels.
|
||||
// the normal, correct spelling, flagging it is pure noise on real kernels.
|
||||
// What remains worth flagging: a Q (64-bit) operation over a narrower spelled
|
||||
// register (EAX under the gasm alias extension, or a byte form), and byte
|
||||
// registers in L/W operations.
|
||||
@@ -812,8 +813,8 @@ func checkRegisterWidth(mnem string, ops []*ast.Operand) string {
|
||||
}
|
||||
|
||||
// amd64RegWidth returns the width in bytes of an amd64 register name under
|
||||
// the Go assembler's naming model: the canonical word names (AX…SP, R8–R15)
|
||||
// are 64-bit, AL–DH are the 8-bit forms, and the R/E-prefixed spellings are
|
||||
// the Go assembler's naming model: the canonical word names (AX…SP, R8-R15)
|
||||
// are 64-bit, AL-DH are the 8-bit forms, and the R/E-prefixed spellings are
|
||||
// the gasm alias extension with their intuitive widths.
|
||||
func amd64RegWidth(name string) int {
|
||||
switch name {
|
||||
@@ -833,7 +834,7 @@ func countRange(min, max int) string {
|
||||
if min == max {
|
||||
return fmt.Sprintf("%d operand(s)", min)
|
||||
}
|
||||
return fmt.Sprintf("%d–%d operands", min, max)
|
||||
return fmt.Sprintf("%d-%d operands", min, max)
|
||||
}
|
||||
|
||||
// sortDiagnostics orders diagnostics by line, then column, then code.
|
||||
|
||||
Reference in New Issue
Block a user