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