diff --git a/build/cli.go b/build/cli.go index 0e12753f..61847832 100644 --- a/build/cli.go +++ b/build/cli.go @@ -58,7 +58,7 @@ func NewFlags(fs *flag.FlagSet) *Flags { } func (f *Flags) Config() *Config { - pc := printer.NewDefaultConfig() + pc := printer.NewGoRunConfig() passes := []pass.Interface{pass.Compile} for _, pv := range f.printers { p := pv.Build(pc) diff --git a/examples/add/add.s b/examples/add/add.s index 0b694ec0..cec93e46 100644 --- a/examples/add/add.s +++ b/examples/add/add.s @@ -1,3 +1,4 @@ +// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT. #include "textflag.h" diff --git a/examples/add/stub.go b/examples/add/stub.go index 9e266319..a8054fce 100644 --- a/examples/add/stub.go +++ b/examples/add/stub.go @@ -1,3 +1,5 @@ +// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT. + package add func Add(x uint64, y uint64) uint64 diff --git a/examples/complex/complex.s b/examples/complex/complex.s index ba668512..44d369bc 100644 --- a/examples/complex/complex.s +++ b/examples/complex/complex.s @@ -1,3 +1,4 @@ +// Code generated by command: go run asm.go -out complex.s -stubs stub.go. DO NOT EDIT. #include "textflag.h" diff --git a/examples/complex/stub.go b/examples/complex/stub.go index aac8dfe8..70929e29 100644 --- a/examples/complex/stub.go +++ b/examples/complex/stub.go @@ -1,5 +1,9 @@ +// Code generated by command: go run asm.go -out complex.s -stubs stub.go. DO NOT EDIT. + package complex func Real(x complex128) float64 + func Imag(x complex128) float64 + func Norm(x complex128) float64 diff --git a/examples/components/components.s b/examples/components/components.s index 41c73a54..65e53138 100644 --- a/examples/components/components.s +++ b/examples/components/components.s @@ -1,3 +1,4 @@ +// Code generated by command: go run asm.go -out components.s -stubs stub.go. DO NOT EDIT. #include "textflag.h" diff --git a/examples/components/stub.go b/examples/components/stub.go index a24b351b..9991c74f 100644 --- a/examples/components/stub.go +++ b/examples/components/stub.go @@ -1,15 +1,29 @@ +// Code generated by command: go run asm.go -out components.s -stubs stub.go. DO NOT EDIT. + package components func FieldByte(s Struct) byte + func FieldInt8(s Struct) int8 + func FieldUint16(s Struct) uint16 + func FieldInt32(s Struct) int32 + func FieldUint64(s Struct) uint64 + func FieldFloat32(s Struct) float32 + func FieldFloat64(s Struct) float64 + func FieldStringLen(s Struct) int + func FieldSliceCap(s Struct) int + func FieldArrayTwoBTwo(s Struct) byte + func FieldArrayOneC(s Struct) uint16 + func FieldComplex64Imag(s Struct) float32 + func FieldComplex128Real(s Struct) float64 diff --git a/examples/fnv1a/fnv1a.s b/examples/fnv1a/fnv1a.s index c83baa84..0da13fb2 100644 --- a/examples/fnv1a/fnv1a.s +++ b/examples/fnv1a/fnv1a.s @@ -1,3 +1,4 @@ +// Code generated by command: go run asm.go -out fnv1a.s -stubs stub.go. DO NOT EDIT. #include "textflag.h" diff --git a/examples/fnv1a/stub.go b/examples/fnv1a/stub.go index 11e554a6..2425a75a 100644 --- a/examples/fnv1a/stub.go +++ b/examples/fnv1a/stub.go @@ -1,3 +1,5 @@ +// Code generated by command: go run asm.go -out fnv1a.s -stubs stub.go. DO NOT EDIT. + package fnv1a func Hash64(data []byte) uint64 diff --git a/examples/sum/stub.go b/examples/sum/stub.go index 859a440e..d7bc64b0 100644 --- a/examples/sum/stub.go +++ b/examples/sum/stub.go @@ -1,3 +1,5 @@ +// Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT. + package sum func Sum(xs []uint64) uint64 diff --git a/examples/sum/sum.s b/examples/sum/sum.s index d06f0387..94978105 100644 --- a/examples/sum/sum.s +++ b/examples/sum/sum.s @@ -1,3 +1,4 @@ +// Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT. #include "textflag.h" diff --git a/internal/cmd/avogen/main.go b/internal/cmd/avogen/main.go index 70e4ffc5..1ff96011 100644 --- a/internal/cmd/avogen/main.go +++ b/internal/cmd/avogen/main.go @@ -46,7 +46,7 @@ func main() { log.Fatalf("unknown generator type '%s'", t) } - g := builder(printer.NewDefaultConfig()) + g := builder(printer.NewArgvConfig()) // Determine output writer. w := os.Stdout diff --git a/internal/inst/table_test.go b/internal/inst/table_test.go index 9bd6efd3..74f7add1 100644 --- a/internal/inst/table_test.go +++ b/internal/inst/table_test.go @@ -71,7 +71,7 @@ func TestInstructionProperties(t *testing.T) { } func TestAssembles(t *testing.T) { - g := gen.NewAsmTest(printer.NewDefaultConfig()) + g := gen.NewAsmTest(printer.NewArgvConfig()) b, err := g.Generate(inst.Instructions) if err != nil { t.Fatal(err) diff --git a/internal/load/load_test.go b/internal/load/load_test.go index 0b7d3f10..4e607583 100644 --- a/internal/load/load_test.go +++ b/internal/load/load_test.go @@ -22,7 +22,7 @@ func Load(t *testing.T) []inst.Instruction { func TestAssembles(t *testing.T) { is := Load(t) - g := gen.NewAsmTest(printer.NewDefaultConfig()) + g := gen.NewAsmTest(printer.NewArgvConfig()) b, err := g.Generate(is) if err != nil { t.Fatal(err) diff --git a/printer/goasm.go b/printer/goasm.go index e417a489..bb3f663b 100644 --- a/printer/goasm.go +++ b/printer/goasm.go @@ -29,6 +29,7 @@ func (p *goasm) Print(f *avo.File) ([]byte, error) { } func (p *goasm) header() { + p.Comment(p.cfg.GeneratedWarning()) p.NL() p.include("textflag.h") } diff --git a/printer/printer.go b/printer/printer.go index 7b1ee9b0..c237b186 100644 --- a/printer/printer.go +++ b/printer/printer.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "strings" "github.com/mmcloughlin/avo" @@ -22,13 +23,34 @@ type Config struct { } func NewDefaultConfig() Config { - cfg := Config{ + return Config{ + Name: "avo", + Pkg: pkg(), + } +} + +func NewArgvConfig() Config { + return Config{ Argv: os.Args, + Pkg: pkg(), } - if cwd, err := os.Getwd(); err == nil { - cfg.Pkg = filepath.Base(cwd) +} + +// NewGoRunConfig produces a Config for a generator that's expected to be +// executed via "go run ...". +func NewGoRunConfig() Config { + path := mainfile() + if path == "" { + return NewDefaultConfig() + } + argv := []string{"go", "run", filepath.Base(path)} + if len(os.Args) > 1 { + argv = append(argv, os.Args[1:]...) + } + return Config{ + Argv: argv, + Pkg: pkg(), } - return cfg } func (c Config) GeneratedBy() string { @@ -41,3 +63,33 @@ func (c Config) GeneratedBy() string { func (c Config) GeneratedWarning() string { return fmt.Sprintf("Code generated by %s. DO NOT EDIT.", c.GeneratedBy()) } + +// mainfile attempts to determine the file path of the main function by +// inspecting the stack. Returns empty string on failure. +func mainfile() string { + pc := make([]uintptr, 10) + n := runtime.Callers(0, pc) + if n == 0 { + return "" + } + pc = pc[:n] + frames := runtime.CallersFrames(pc) + for { + frame, more := frames.Next() + if frame.Function == "main.main" { + return frame.File + } + if !more { + break + } + } + return "" +} + +// pkg guesses the name of the package from the working directory. +func pkg() string { + if cwd, err := os.Getwd(); err == nil { + return filepath.Base(cwd) + } + return "" +} diff --git a/printer/stubs.go b/printer/stubs.go index 9e7b518a..b1ae7a67 100644 --- a/printer/stubs.go +++ b/printer/stubs.go @@ -15,8 +15,11 @@ func NewStubs(cfg Config) Printer { } func (s *stubs) Print(f *avo.File) ([]byte, error) { - s.Printf("package %s\n\n", s.cfg.Pkg) + s.Comment(s.cfg.GeneratedWarning()) + s.NL() + s.Printf("package %s\n", s.cfg.Pkg) for _, fn := range f.Functions { + s.NL() s.Printf("%s\n", fn.Stub()) } return s.Result()