| 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 ctors struct { |
| 11 | cfg printer.Config |
| 12 | prnt.Generator |
| 13 | } |
| 14 | |
| 15 | // NewCtors will build instruction constructors. Each constructor delegates to |
| 16 | // the optab-based instruction builder, providing it with a candidate list of |
| 17 | // forms to match against. |
| 18 | func NewCtors(cfg printer.Config) Interface { |
| 19 | return GoFmt(&ctors{cfg: cfg}) |
| 20 | } |
| 21 | |
| 22 | func (c *ctors) Generate(is []inst.Instruction) ([]byte, error) { |
| 23 | c.Printf("// %s\n\n", c.cfg.GeneratedWarning()) |
| 24 | c.Printf("package x86\n\n") |
| 25 | c.Printf("import (\n") |
| 26 | c.Printf("\tintrep %q\n", api.ImportPath(api.IRPackage)) |
| 27 | c.Printf("\t%q\n", api.ImportPath(api.OperandPackage)) |
| 28 | c.Printf(")\n\n") |
| 29 | |
| 30 | fns := api.InstructionsFunctions(is) |
| 31 | table := NewTable(is) |
| 32 | for _, fn := range fns { |
| 33 | c.function(fn, table) |
| 34 | } |
| 35 | |
| 36 | return c.Result() |
| 37 | } |
| 38 | |
| 39 | func (c *ctors) function(fn *api.Function, table *Table) { |
| 40 | c.Comment(fn.Doc()...) |
| 41 | |
| 42 | s := fn.Signature() |
| 43 | |
| 44 | c.Printf("func %s(%s) (*intrep.Instruction, error) {\n", fn.Name(), s.ParameterList()) |
| 45 | c.Printf( |
| 46 | "return build(%s.Forms(), %s, %s)\n", |
| 47 | table.OpcodeConst(fn.Instruction.Opcode), |
| 48 | table.SuffixesConst(fn.Suffixes), |
| 49 | s.ParameterSlice(), |
| 50 | ) |
| 51 | c.Printf("}\n\n") |
| 52 | } |