Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Printer hook enabling custom user defined file output like -stubs #350

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion build/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package build

import (
"flag"
"io"
"os"

"github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/buildtags"
"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"
)

Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions internal/gen/asmtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/mmcloughlin/avo/internal/inst"
"github.com/mmcloughlin/avo/internal/prnt"
"github.com/mmcloughlin/avo/printer"
)

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/gen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/gen/buildtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/gen/ctors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions internal/gen/ctorstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions internal/gen/godata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/gen/mov.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/gen/optab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions internal/gen/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/prnt/printer.go → printer/generator.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 1 addition & 2 deletions printer/goasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -15,7 +14,7 @@ const dot = "\u00b7"

type goasm struct {
cfg Config
prnt.Generator
Generator

instructions []*ir.Instruction
clear bool
Expand Down
3 changes: 1 addition & 2 deletions printer/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/thirdparty/make_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down