| 1 | package gen |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mmcloughlin/avo/internal/api" |
| 5 | "github.com/mmcloughlin/avo/internal/inst" |
| 6 | "github.com/mmcloughlin/avo/internal/prnt" |
| 7 | "github.com/mmcloughlin/avo/printer" |
| 8 | ) |
| 9 | |
| 10 | type godata struct { |
| 11 | cfg printer.Config |
| 12 | prnt.Generator |
| 13 | } |
| 14 | |
| 15 | // NewGoData writes a Go variable containing the instructions database. This is |
| 16 | // intended to provide a more friendly version of the instruction database, |
| 17 | // rather than having to use the raw data sources all the time. |
| 18 | func NewGoData(cfg printer.Config) Interface { |
| 19 | return GoFmt(&godata{cfg: cfg}) |
| 20 | } |
| 21 | |
| 22 | func (g *godata) Generate(is []inst.Instruction) ([]byte, error) { |
| 23 | g.Printf("// %s\n\n", g.cfg.GeneratedWarning()) |
| 24 | g.Printf("package inst\n\n") |
| 25 | |
| 26 | g.Printf("var Instructions = []Instruction{\n") |
| 27 | |
| 28 | for _, i := range is { |
| 29 | g.Printf("{\n") |
| 30 | |
| 31 | g.Printf("Opcode: %#v,\n", i.Opcode) |
| 32 | if i.AliasOf != "" { |
| 33 | g.Printf("AliasOf: %#v,\n", i.AliasOf) |
| 34 | } |
| 35 | g.Printf("Summary: %#v,\n", i.Summary) |
| 36 | |
| 37 | g.Printf("Forms: []Form{\n") |
| 38 | for _, f := range i.Forms { |
| 39 | g.Printf("{\n") |
| 40 | |
| 41 | if f.ISA != nil { |
| 42 | g.Printf("ISA: %#v,\n", f.ISA) |
| 43 | } |
| 44 | |
| 45 | if f.Operands != nil { |
| 46 | g.Printf("Operands: []Operand{\n") |
| 47 | for _, op := range f.Operands { |
| 48 | g.Printf("{Type: %#v, Action: %#v},\n", op.Type, op.Action) |
| 49 | } |
| 50 | g.Printf("},\n") |
| 51 | } |
| 52 | |
| 53 | if f.ImplicitOperands != nil { |
| 54 | g.Printf("ImplicitOperands: []ImplicitOperand{\n") |
| 55 | for _, op := range f.ImplicitOperands { |
| 56 | g.Printf("{Register: %#v, Action: %#v},\n", op.Register, op.Action) |
| 57 | } |
| 58 | g.Printf("},\n") |
| 59 | } |
| 60 | |
| 61 | g.Printf("EncodingType: %#v,\n", f.EncodingType) |
| 62 | |
| 63 | for _, flag := range []struct { |
| 64 | Field string |
| 65 | Enabled bool |
| 66 | }{ |
| 67 | {"CancellingInputs", f.CancellingInputs}, |
| 68 | {"Zeroing", f.Zeroing}, |
| 69 | {"EmbeddedRounding", f.EmbeddedRounding}, |
| 70 | {"SuppressAllExceptions", f.SuppressAllExceptions}, |
| 71 | {"Broadcast", f.Broadcast}, |
| 72 | } { |
| 73 | if flag.Enabled { |
| 74 | g.Printf("%s: true,\n", flag.Field) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | g.Printf("},\n") |
| 79 | } |
| 80 | g.Printf("},\n") |
| 81 | |
| 82 | g.Printf("},\n") |
| 83 | } |
| 84 | |
| 85 | g.Printf("}\n") |
| 86 | |
| 87 | return g.Result() |
| 88 | } |
| 89 | |
| 90 | type godatatest struct { |
| 91 | cfg printer.Config |
| 92 | prnt.Generator |
| 93 | } |
| 94 | |
| 95 | // NewGoDataTest writes a test case to confirm that NewGoData faithfully |
| 96 | // represented the list. The reason for this is that NewGoData uses custom code |
| 97 | // to "pretty print" the database so it is somewhat human-readable. In the |
| 98 | // process we could easily mistakenly print the database incorrectly. This test |
| 99 | // prints the same slice of instructions with the ugly but correct "%#v" format |
| 100 | // specifier, and confirms that the two arrays agree. |
| 101 | func NewGoDataTest(cfg printer.Config) Interface { |
| 102 | return GoFmt(&godatatest{cfg: cfg}) |
| 103 | } |
| 104 | |
| 105 | func (g *godatatest) Generate(is []inst.Instruction) ([]byte, error) { |
| 106 | g.Printf("// %s\n\n", g.cfg.GeneratedWarning()) |
| 107 | g.NL() |
| 108 | g.Printf("package inst_test\n\n") |
| 109 | |
| 110 | g.Printf(`import ( |
| 111 | "reflect" |
| 112 | "testing" |
| 113 | |
| 114 | "%s/internal/inst" |
| 115 | ) |
| 116 | `, api.Package) |
| 117 | |
| 118 | g.Printf("var raw = %#v\n\n", is) |
| 119 | |
| 120 | g.Printf(`func TestVerifyInstructionsList(t *testing.T) { |
| 121 | if !reflect.DeepEqual(raw, inst.Instructions) { |
| 122 | t.Fatal("bad code generation for instructions list") |
| 123 | } |
| 124 | } |
| 125 | `) |
| 126 | |
| 127 | return g.Result() |
| 128 | } |