diff --git a/build/global.go b/build/global.go index 3a1690c1..6cc379ba 100644 --- a/build/global.go +++ b/build/global.go @@ -2,6 +2,7 @@ package build import ( "flag" + "io" "os" "github.com/mmcloughlin/avo/attr" @@ -9,6 +10,7 @@ import ( "github.com/mmcloughlin/avo/gotypes" "github.com/mmcloughlin/avo/ir" "github.com/mmcloughlin/avo/operand" + "github.com/mmcloughlin/avo/printer" "github.com/mmcloughlin/avo/reg" ) @@ -35,7 +37,8 @@ func DATA(offset int, v operand.Constant) { ctx.AddDatum(offset, v) } -var flags = NewFlags(flag.CommandLine) +var flagSet = flag.CommandLine +var flags = NewFlags(flagSet) // Generate builds and compiles the avo file built with the global context. This // should be the final line of any avo program. Configuration is determined from command-line flags. @@ -161,3 +164,10 @@ func ConstData(name string, v operand.Constant) operand.Mem { return ctx.ConstDa // Instruction adds an instruction to the active function. func Instruction(i *ir.Instruction) { ctx.Instruction(i) } + +// AddPrinter registers a custom printer +func AddPrinter(flag, desc string, pB printer.Builder, dflt io.WriteCloser) { + pV := newPrinterValue(pB, dflt) + flagSet.Var(pV, flag, desc) + flags.printers = append(flags.printers, pV) +} diff --git a/internal/gen/asmtest.go b/internal/gen/asmtest.go index d4c27d34..ea0f1f36 100644 --- a/internal/gen/asmtest.go +++ b/internal/gen/asmtest.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) @@ -16,7 +15,7 @@ type asmtest struct { sym string // reference to the test function symbol rel8 string // label to be used for near jumps rel32 string // label for far jumps - prnt.Generator + printer.Generator } // NewAsmTest prints one massive assembly function containing a line for every diff --git a/internal/gen/build.go b/internal/gen/build.go index 4905b810..06d62efb 100644 --- a/internal/gen/build.go +++ b/internal/gen/build.go @@ -5,13 +5,12 @@ import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) type build struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewBuild builds a printer that will generate instruction functions in the diff --git a/internal/gen/buildtest.go b/internal/gen/buildtest.go index ad24d269..0d449996 100644 --- a/internal/gen/buildtest.go +++ b/internal/gen/buildtest.go @@ -3,13 +3,12 @@ package gen import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) type buildtest struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewBuildTest autogenerates tests for instruction methods on the build diff --git a/internal/gen/ctors.go b/internal/gen/ctors.go index b4955107..388236f1 100644 --- a/internal/gen/ctors.go +++ b/internal/gen/ctors.go @@ -3,13 +3,12 @@ package gen import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) type ctors struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewCtors will build instruction constructors. Each constructor delegates to diff --git a/internal/gen/ctorstest.go b/internal/gen/ctorstest.go index b271d2f8..39f30532 100644 --- a/internal/gen/ctorstest.go +++ b/internal/gen/ctorstest.go @@ -7,13 +7,12 @@ import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) type ctorstest struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewCtorsTest autogenerates tests for the constructors build by NewCtors. @@ -55,7 +54,7 @@ func (c *ctorstest) function(fn *api.Function) { type ctorsstress struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewCtorsStress autogenerates stress tests for instruction constructors. @@ -102,7 +101,7 @@ func (c *ctorsstress) function(fn *api.Function) { type ctorsbench struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewCtorsBench autogenerates a benchmark for the instruction constructors. diff --git a/internal/gen/godata.go b/internal/gen/godata.go index 443d53c2..07da1a53 100644 --- a/internal/gen/godata.go +++ b/internal/gen/godata.go @@ -3,13 +3,12 @@ package gen import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) type godata struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewGoData writes a Go variable containing the instructions database. This is @@ -89,7 +88,7 @@ func (g *godata) Generate(is []inst.Instruction) ([]byte, error) { type godatatest struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewGoDataTest writes a test case to confirm that NewGoData faithfully diff --git a/internal/gen/mov.go b/internal/gen/mov.go index 0b1ec1e2..110b3c16 100644 --- a/internal/gen/mov.go +++ b/internal/gen/mov.go @@ -7,13 +7,12 @@ import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) type mov struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewMOV generates a function that will auto-select the correct MOV instruction diff --git a/internal/gen/optab.go b/internal/gen/optab.go index ca51c33a..01ca745f 100644 --- a/internal/gen/optab.go +++ b/internal/gen/optab.go @@ -8,12 +8,11 @@ import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" ) type optab struct { - prnt.Generator + printer.Generator cfg printer.Config diff --git a/internal/gen/testing.go b/internal/gen/testing.go index ee2ecd64..a2c589c0 100644 --- a/internal/gen/testing.go +++ b/internal/gen/testing.go @@ -3,12 +3,12 @@ package gen import ( "github.com/mmcloughlin/avo/internal/api" "github.com/mmcloughlin/avo/internal/inst" - "github.com/mmcloughlin/avo/internal/prnt" + "github.com/mmcloughlin/avo/printer" ) // DeclareTestArguments prints a block of variables declaring a valid operand of // each operand type. -func DeclareTestArguments(g *prnt.Generator) { +func DeclareTestArguments(g *printer.Generator) { g.Printf("var (\n") for _, arg := range validArgs { g.Printf("\t%s operand.Op = %s\n", TestArgumentName(arg.Type), arg.Code) diff --git a/internal/prnt/printer.go b/printer/generator.go similarity index 96% rename from internal/prnt/printer.go rename to printer/generator.go index 06d6e913..ecd71cb8 100644 --- a/internal/prnt/printer.go +++ b/printer/generator.go @@ -1,5 +1,5 @@ -// Package prnt provides common functionality for code generators. -package prnt +// Package printer implements printing of avo files in various formats. +package printer import ( "bytes" diff --git a/printer/goasm.go b/printer/goasm.go index 23f5b2f7..e93988d6 100644 --- a/printer/goasm.go +++ b/printer/goasm.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/mmcloughlin/avo/buildtags" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/ir" "github.com/mmcloughlin/avo/operand" ) @@ -15,7 +14,7 @@ const dot = "\u00b7" type goasm struct { cfg Config - prnt.Generator + Generator instructions []*ir.Instruction clear bool diff --git a/printer/stubs.go b/printer/stubs.go index 1fd9ddcb..8dbc9671 100644 --- a/printer/stubs.go +++ b/printer/stubs.go @@ -4,13 +4,12 @@ import ( "go/format" "github.com/mmcloughlin/avo/buildtags" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/ir" ) type stubs struct { cfg Config - prnt.Generator + Generator } // NewStubs constructs a printer for writing stub function declarations. diff --git a/tests/thirdparty/make_workflow.go b/tests/thirdparty/make_workflow.go index 669d627e..92cdaf79 100644 --- a/tests/thirdparty/make_workflow.go +++ b/tests/thirdparty/make_workflow.go @@ -10,7 +10,7 @@ import ( "path/filepath" "runtime" - "github.com/mmcloughlin/avo/internal/prnt" + "github.com/mmcloughlin/avo/printer" "github.com/mmcloughlin/avo/tests/thirdparty" ) @@ -64,7 +64,7 @@ func mainerr() error { } func GenerateWorkflow(s *thirdparty.Suite) ([]byte, error) { - g := &prnt.Generator{} + g := &printer.Generator{} g.SetIndentString(" ") _, self, _, _ := runtime.Caller(0)