feat(cmd): add gofmt-style -l and -d modes to gasm fmt

This commit is contained in:
2026-09-14 18:47:08 +02:00
parent 95f1d6f083
commit 50db6615b2
3 changed files with 258 additions and 1 deletions
+24 -1
View File
@@ -56,6 +56,8 @@ func main() {
os.Exit(cmdLint(os.Args[2:]))
case "asm":
os.Exit(cmdAsm(os.Args[2:]))
case "dis":
os.Exit(cmdDis(os.Args[2:]))
case "verify":
os.Exit(cmdVerify(os.Args[2:]))
case "debug":
@@ -132,6 +134,7 @@ func usage(w io.Writer) {
{"fmt", "canonicalise formatting (gofmt for assembly)"},
{"lint", "run static checks"},
{"asm", "assemble .s files to machine code (amd64, arm64, riscv64, loong64)"},
{"dis", "disassemble machine code (raw bytes or an assembled .s file)"},
{"verify", "JIT-assemble and run dynamic checks (amd64, arm64, riscv64, loong64)"},
{"debug", "interactive source-level debugger (amd64, arm64, riscv64, loong64)"},
{"diff", "compare machine code of two .s files"},
@@ -268,7 +271,7 @@ standard input.
}
func cmdFmt(args []string) int {
fs := newCommand("fmt", "gasm fmt [-w] [path...]", `
fs := newCommand("fmt", "gasm fmt [-w|-l|-d] [path...]", `
Canonicalise the formatting of Plan 9 assembly sources: indentation, operand
spacing, per-function mnemonic alignment and blank-line layout (exactly one
blank line before each label, TEXT and GLOBL block). Formatting is
@@ -278,9 +281,19 @@ With no paths, or a directory path, every .s file below it is reformatted
in place and the changed files are listed, the way go fmt does; "." and "_"
directories are skipped. Explicit file paths print to stdout unless -w is
given.
-l and -d rewrite nothing: -l prints the paths whose formatting differs
from gasm's (empty output means everything is formatted, which is what a CI
check wants), -d prints the diffs. They are mutually exclusive.
`)
write := fs.Bool("w", false, "write result to the source file")
list := fs.Bool("l", false, "list files whose formatting differs from gasm's")
diffMode := fs.Bool("d", false, "print diffs instead of rewriting files")
fs.Parse(args)
if *list && *diffMode {
fmt.Fprintln(os.Stderr, "gasm fmt: -l and -d are mutually exclusive")
return 2
}
// Like go fmt: with no arguments, or with a directory argument, every .s
// file below the directory is formatted in place and the names of the
// changed files are listed; explicit file arguments keep the -w / stdout
@@ -318,6 +331,16 @@ given.
continue
}
out := format.Source(src)
if *list || *diffMode {
if out != src {
if *list {
fmt.Println(path)
} else {
fmt.Print(unifiedDiff(path, strings.Split(src, "\n"), strings.Split(out, "\n")))
}
}
continue
}
if dirMode || *write {
if out != src {
if err := os.WriteFile(path, []byte(out), 0o644); err != nil {