feat(lint): flag writes to the platform-reserved register
This commit is contained in:
@@ -436,3 +436,62 @@ TEXT ·f(SB), NOSPLIT, $0
|
||||
t.Fatalf("canonical names must not be flagged: %+v", diags)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReservedRegisterWrite(t *testing.T) {
|
||||
// A write to R18, the arm64 platform register, is flagged.
|
||||
diags := lintSrcArch(t, "k_arm64.s", `
|
||||
#include "textflag.h"
|
||||
TEXT ·f(SB), NOSPLIT, $0
|
||||
MOVD $1, R18
|
||||
RET
|
||||
`)
|
||||
if codes(diags)[CodeReservedRegister] != 1 {
|
||||
t.Fatalf("want one reserved-register-write, got %+v", diags)
|
||||
}
|
||||
|
||||
// Reading R18 (as a base address) is legitimate.
|
||||
diags = lintSrcArch(t, "k_arm64.s", `
|
||||
#include "textflag.h"
|
||||
TEXT ·f(SB), NOSPLIT, $0
|
||||
MOVD (R18), R0
|
||||
RET
|
||||
`)
|
||||
if codes(diags)[CodeReservedRegister] != 0 {
|
||||
t.Fatalf("reads must not be flagged: %+v", diags)
|
||||
}
|
||||
|
||||
// Other registers are unaffected.
|
||||
diags = lintSrcArch(t, "k_arm64.s", `
|
||||
#include "textflag.h"
|
||||
TEXT ·f(SB), NOSPLIT, $0
|
||||
MOVD $1, R19
|
||||
RET
|
||||
`)
|
||||
if codes(diags)[CodeReservedRegister] != 0 {
|
||||
t.Fatalf("R19 must not be flagged: %+v", diags)
|
||||
}
|
||||
|
||||
// Macro-using files are exempt: an opaque macro may save and restore R18.
|
||||
diags = lintSrcArch(t, "k_arm64.s", `
|
||||
#include "textflag.h"
|
||||
#define SAVE_R18 MOVD R18, R4
|
||||
TEXT ·f(SB), NOSPLIT, $0
|
||||
SAVE_R18
|
||||
MOVD $1, R18
|
||||
RET
|
||||
`)
|
||||
if codes(diags)[CodeReservedRegister] != 0 {
|
||||
t.Fatalf("macro-using files must be exempt: %+v", diags)
|
||||
}
|
||||
|
||||
// The rule is arm64-only: an amd64 file has no reserved register.
|
||||
diags = lintSrc(t, `
|
||||
#include "textflag.h"
|
||||
TEXT ·f(SB), NOSPLIT, $0
|
||||
MOVQ $1, AX
|
||||
RET
|
||||
`)
|
||||
if codes(diags)[CodeReservedRegister] != 0 {
|
||||
t.Fatalf("amd64 must not be flagged: %+v", diags)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user