From d6b759bc4f412a4c75d4014daab100b63833967e Mon Sep 17 00:00:00 2001 From: Vaughn Iverson Date: Thu, 18 Nov 2021 14:48:18 -0800 Subject: [PATCH] Prototype AddPrinter() API function --- build/global.go | 12 +++++++++++- internal/gen/asmtest.go | 3 +-- internal/gen/build.go | 3 +-- internal/gen/buildtest.go | 3 +-- internal/gen/ctors.go | 3 +-- internal/gen/ctorstest.go | 7 +++---- internal/gen/godata.go | 5 ++--- internal/gen/mov.go | 3 +-- internal/gen/optab.go | 3 +-- internal/gen/testing.go | 4 ++-- internal/prnt/printer.go => printer/generator.go | 4 ++-- printer/goasm.go | 3 +-- printer/stubs.go | 3 +-- 13 files changed, 28 insertions(+), 28 deletions(-) rename internal/prnt/printer.go => printer/generator.go (96%) diff --git a/build/global.go b/build/global.go index e23da2c7..915d5ea8 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" ) @@ -36,7 +38,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. @@ -162,3 +165,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 582b58e1..93a1c983 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 85860b6b..7096e63b 100644 --- a/internal/gen/ctorstest.go +++ b/internal/gen/ctorstest.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/mmcloughlin/avo/internal/api" - "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/printer" "github.com/mmcloughlin/avo/internal/inst" @@ -14,7 +13,7 @@ import ( type ctorstest struct { cfg printer.Config - prnt.Generator + printer.Generator } // NewCtorsTest autogenerates tests for the constructors build by NewCtors. @@ -56,7 +55,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. @@ -103,7 +102,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 a281addc..30700dd6 100644 --- a/internal/gen/mov.go +++ b/internal/gen/mov.go @@ -8,13 +8,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 cda73de2..48dfbd3e 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 cc9e89ef..e4570d25 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 73c26494..9a641802 100644 --- a/printer/stubs.go +++ b/printer/stubs.go @@ -2,13 +2,12 @@ package printer import ( "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.