1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
package gen
import (
"bytes"
"fmt"
"strings"
"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
}
// NewCtorsTest autogenerates tests for the constructors build by NewCtors.
func NewCtorsTest(cfg printer.Config) Interface {
return GoFmt(&ctorstest{cfg: cfg})
}
func (c *ctorstest) Generate(is []inst.Instruction) ([]byte, error) {
c.Printf("// %s\n\n", c.cfg.GeneratedWarning())
c.BuildTag("!integration")
c.NL()
c.Printf("package x86\n\n")
c.Printf("import (\n")
c.Printf("\t\"math\"\n")
c.Printf("\t\"testing\"\n")
c.NL()
c.Printf("\t%q\n", api.ImportPath(api.OperandPackage))
c.Printf("\t%q\n", api.ImportPath(api.RegisterPackage))
c.Printf(")\n\n")
DeclareTestArguments(&c.Generator)
fns := api.InstructionsFunctions(is)
for _, fn := range fns {
c.function(fn)
}
return c.Result()
}
func (c *ctorstest) function(fn *api.Function) {
c.Printf("func Test%sValidFormsNoError(t *testing.T) {", fn.Name())
for _, f := range fn.Forms {
s := TestSignature(f)
c.Printf("if _, err := %s(%s); err != nil { t.Fatal(err) }\n", fn.Name(), s.Arguments())
}
c.Printf("}\n\n")
}
type ctorsstress struct {
cfg printer.Config
prnt.Generator
}
// NewCtorsStress autogenerates stress tests for instruction constructors.
func NewCtorsStress(cfg printer.Config) Interface {
return GoFmt(&ctorsstress{cfg: cfg})
}
func (c *ctorsstress) Generate(is []inst.Instruction) ([]byte, error) {
c.Printf("// %s\n\n", c.cfg.GeneratedWarning())
c.BuildTag("stress")
c.NL()
c.Printf("package x86\n\n")
c.Printf("import (\n")
c.Printf("\t\"reflect\"\n")
c.Printf("\t\"testing\"\n")
c.NL()
c.Printf("\t%q\n", api.ImportPath(api.IRPackage))
c.Printf("\t%q\n", api.ImportPath(api.OperandPackage))
c.Printf("\t%q\n", api.ImportPath(api.RegisterPackage))
c.Printf(")\n\n")
fns := api.InstructionsFunctions(is)
for _, fn := range fns {
c.function(fn)
}
return c.Result()
}
func (c *ctorsstress) function(fn *api.Function) {
c.Printf("func Test%sValidFormsCorrectInstruction(t *testing.T) {", fn.Name())
for _, f := range fn.Forms {
name := strings.Join(f.Signature(), "_")
c.Printf("t.Run(\"form=%s\", func(t *testing.T) {\n", name)
s := TestSignature(f)
c.Printf("expect := &%s\n", construct(fn, f, s))
c.Printf("got, err := %s(%s);\n", fn.Name(), s.Arguments())
c.Printf("if err != nil { t.Fatal(err) }\n")
c.Printf("if !reflect.DeepEqual(got, expect) { t.Fatal(\"mismatch\") }\n")
c.Printf("})\n")
}
c.Printf("}\n\n")
}
type ctorsbench struct {
cfg printer.Config
prnt.Generator
}
// NewCtorsBench autogenerates a benchmark for the instruction constructors.
func NewCtorsBench(cfg printer.Config) Interface {
return GoFmt(&ctorsbench{cfg: cfg})
}
func (c *ctorsbench) Generate(is []inst.Instruction) ([]byte, error) {
c.Printf("// %s\n\n", c.cfg.GeneratedWarning())
c.BuildTag("stress")
c.NL()
c.Printf("package x86\n\n")
c.Printf("import (\n")
c.Printf("\t\"time\"\n")
c.Printf("\t\"testing\"\n")
c.Printf(")\n\n")
c.Printf("func BenchmarkConstructors(b *testing.B) {\n")
c.Printf("start := time.Now()\n")
c.Printf("for i := 0; i < b.N; i++ {\n")
n := 0
for _, fn := range api.InstructionsFunctions(is) {
for _, f := range fn.Forms {
n++
c.Printf("%s(%s)\n", fn.Name(), TestSignature(f).Arguments())
}
}
c.Printf("}\n")
c.Printf("elapsed := time.Since(start)\n")
c.Printf("\tb.ReportMetric(%d * float64(b.N) / elapsed.Seconds(), \"inst/s\")\n", n)
c.Printf("}\n\n")
return c.Result()
}
func construct(fn *api.Function, f inst.Form, s api.Signature) string {
buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "ir.Instruction{\n")
fmt.Fprintf(buf, "\tOpcode: %#v,\n", fn.Instruction.Opcode)
if len(fn.Suffixes) > 0 {
fmt.Fprintf(buf, "\tSuffixes: %#v,\n", fn.Suffixes.Strings())
}
fmt.Fprintf(buf, "\tOperands: %s,\n", s.ParameterSlice())
// Inputs.
fmt.Fprintf(buf, "\tInputs: %s,\n", operandsWithAction(f, inst.R, s))
// Outputs.
fmt.Fprintf(buf, "\tOutputs: %s,\n", operandsWithAction(f, inst.W, s))
// ISAs.
if len(f.ISA) > 0 {
fmt.Fprintf(buf, "\tISA: %#v,\n", f.ISA)
}
// Branch variables.
if fn.Instruction.IsTerminal() {
fmt.Fprintf(buf, "\tIsTerminal: true,\n")
}
if fn.Instruction.IsBranch() {
fmt.Fprintf(buf, "\tIsBranch: true,\n")
fmt.Fprintf(buf, "\tIsConditional: %#v,\n", fn.Instruction.IsConditionalBranch())
}
// Cancelling inputs.
if f.CancellingInputs {
fmt.Fprintf(buf, "\tCancellingInputs: true,\n")
}
fmt.Fprintf(buf, "}")
return buf.String()
}
func operandsWithAction(f inst.Form, a inst.Action, s api.Signature) string {
var opexprs []string
for i, op := range f.Operands {
if op.Action.ContainsAny(a) {
opexprs = append(opexprs, s.ParameterName(i))
}
}
for _, op := range f.ImplicitOperands {
if op.Action.ContainsAny(a) {
opexprs = append(opexprs, api.ImplicitRegister(op.Register))
}
}
if len(opexprs) == 0 {
return "nil"
}
return fmt.Sprintf("[]%s{%s}", api.OperandType, strings.Join(opexprs, ", "))
}
|