| 1 | package build |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "go/types" |
| 7 | |
| 8 | "golang.org/x/tools/go/packages" |
| 9 | |
| 10 | "github.com/mmcloughlin/avo/attr" |
| 11 | "github.com/mmcloughlin/avo/buildtags" |
| 12 | "github.com/mmcloughlin/avo/gotypes" |
| 13 | "github.com/mmcloughlin/avo/ir" |
| 14 | "github.com/mmcloughlin/avo/operand" |
| 15 | "github.com/mmcloughlin/avo/reg" |
| 16 | ) |
| 17 | |
| 18 | //go:generate avogen -output zinstructions.go build |
| 19 | //go:generate avogen -output zinstructions_test.go buildtest |
| 20 | |
| 21 | // Context maintains state for incrementally building an avo File. |
| 22 | type Context struct { |
| 23 | pkg *packages.Package |
| 24 | file *ir.File |
| 25 | function *ir.Function |
| 26 | global *ir.Global |
| 27 | errs ErrorList |
| 28 | reg.Collection |
| 29 | } |
| 30 | |
| 31 | // NewContext initializes an empty build Context. |
| 32 | func NewContext() *Context { |
| 33 | return &Context{ |
| 34 | file: ir.NewFile(), |
| 35 | Collection: *reg.NewCollection(), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Package sets the package the generated file will belong to. Required to be able to reference types in the package. |
| 40 | func (c *Context) Package(path string) { |
| 41 | cfg := &packages.Config{ |
| 42 | Mode: packages.NeedTypes | packages.NeedDeps | packages.NeedImports, |
| 43 | } |
| 44 | pkgs, err := packages.Load(cfg, path) |
| 45 | if err != nil { |
| 46 | c.adderror(err) |
| 47 | return |
| 48 | } |
| 49 | pkg := pkgs[0] |
| 50 | if len(pkg.Errors) > 0 { |
| 51 | for _, err := range pkg.Errors { |
| 52 | c.adderror(err) |
| 53 | } |
| 54 | return |
| 55 | } |
| 56 | c.pkg = pkg |
| 57 | } |
| 58 | |
| 59 | // Constraints sets build constraints for the file. |
| 60 | func (c *Context) Constraints(t buildtags.ConstraintsConvertable) { |
| 61 | cs := t.ToConstraints() |
| 62 | if err := cs.Validate(); err != nil { |
| 63 | c.adderror(err) |
| 64 | return |
| 65 | } |
| 66 | c.file.Constraints = cs |
| 67 | } |
| 68 | |
| 69 | // Constraint appends a constraint to the file's build constraints. |
| 70 | func (c *Context) Constraint(t buildtags.ConstraintConvertable) { |
| 71 | c.Constraints(append(c.file.Constraints, t.ToConstraint())) |
| 72 | } |
| 73 | |
| 74 | // ConstraintExpr appends a constraint to the file's build constraints. The |
| 75 | // constraint to add is parsed from the given expression. The expression should |
| 76 | // look the same as the content following "// +build " in regular build |
| 77 | // constraint comments. |
| 78 | func (c *Context) ConstraintExpr(expr string) { |
| 79 | constraint, err := buildtags.ParseConstraint(expr) |
| 80 | if err != nil { |
| 81 | c.adderror(err) |
| 82 | return |
| 83 | } |
| 84 | c.Constraint(constraint) |
| 85 | } |
| 86 | |
| 87 | // Function starts building a new function with the given name. |
| 88 | func (c *Context) Function(name string) { |
| 89 | c.function = ir.NewFunction(name) |
| 90 | c.file.AddSection(c.function) |
| 91 | } |
| 92 | |
| 93 | // Doc sets documentation comment lines for the currently active function. |
| 94 | func (c *Context) Doc(lines ...string) { |
| 95 | c.activefunc().Doc = lines |
| 96 | } |
| 97 | |
| 98 | // Pragma adds a compiler directive to the currently active function. |
| 99 | func (c *Context) Pragma(directive string, args ...string) { |
| 100 | c.activefunc().AddPragma(directive, args...) |
| 101 | } |
| 102 | |
| 103 | // Attributes sets function attributes for the currently active function. |
| 104 | func (c *Context) Attributes(a attr.Attribute) { |
| 105 | c.activefunc().Attributes = a |
| 106 | } |
| 107 | |
| 108 | // Signature sets the signature for the currently active function. |
| 109 | func (c *Context) Signature(s *gotypes.Signature) { |
| 110 | c.activefunc().SetSignature(s) |
| 111 | } |
| 112 | |
| 113 | // SignatureExpr parses the signature expression and sets it as the active function's signature. |
| 114 | func (c *Context) SignatureExpr(expr string) { |
| 115 | s, err := gotypes.ParseSignatureInPackage(c.types(), expr) |
| 116 | if err != nil { |
| 117 | c.adderror(err) |
| 118 | return |
| 119 | } |
| 120 | c.Signature(s) |
| 121 | } |
| 122 | |
| 123 | // Implement starts building a function of the given name, whose type is |
| 124 | // specified by a stub in the containing package. |
| 125 | func (c *Context) Implement(name string) { |
| 126 | pkg := c.types() |
| 127 | if pkg == nil { |
| 128 | c.adderrormessage("no package specified") |
| 129 | return |
| 130 | } |
| 131 | s, err := gotypes.LookupSignature(pkg, name) |
| 132 | if err != nil { |
| 133 | c.adderror(err) |
| 134 | return |
| 135 | } |
| 136 | c.Function(name) |
| 137 | c.Signature(s) |
| 138 | } |
| 139 | |
| 140 | func (c *Context) types() *types.Package { |
| 141 | if c.pkg == nil { |
| 142 | return nil |
| 143 | } |
| 144 | return c.pkg.Types |
| 145 | } |
| 146 | |
| 147 | // AllocLocal allocates size bytes in the stack of the currently active function. |
| 148 | // Returns a reference to the base pointer for the newly allocated region. |
| 149 | func (c *Context) AllocLocal(size int) operand.Mem { |
| 150 | return c.activefunc().AllocLocal(size) |
| 151 | } |
| 152 | |
| 153 | // Instruction adds an instruction to the active function. |
| 154 | func (c *Context) Instruction(i *ir.Instruction) { |
| 155 | c.activefunc().AddInstruction(i) |
| 156 | } |
| 157 | |
| 158 | // Label adds a label to the active function. |
| 159 | func (c *Context) Label(name string) { |
| 160 | c.activefunc().AddLabel(ir.Label(name)) |
| 161 | } |
| 162 | |
| 163 | // Comment adds comment lines to the active function. |
| 164 | func (c *Context) Comment(lines ...string) { |
| 165 | c.activefunc().AddComment(lines...) |
| 166 | } |
| 167 | |
| 168 | // Commentf adds a formtted comment line. |
| 169 | func (c *Context) Commentf(format string, a ...any) { |
| 170 | c.Comment(fmt.Sprintf(format, a...)) |
| 171 | } |
| 172 | |
| 173 | func (c *Context) activefunc() *ir.Function { |
| 174 | if c.function == nil { |
| 175 | c.adderrormessage("no active function") |
| 176 | return ir.NewFunction("") |
| 177 | } |
| 178 | return c.function |
| 179 | } |
| 180 | |
| 181 | // StaticGlobal adds a new static data section to the file and returns a pointer to it. |
| 182 | func (c *Context) StaticGlobal(name string) operand.Mem { |
| 183 | c.global = ir.NewStaticGlobal(name) |
| 184 | c.file.AddSection(c.global) |
| 185 | return c.global.Base() |
| 186 | } |
| 187 | |
| 188 | // DataAttributes sets the attributes on the current active global data section. |
| 189 | func (c *Context) DataAttributes(a attr.Attribute) { |
| 190 | c.activeglobal().Attributes = a |
| 191 | } |
| 192 | |
| 193 | // AddDatum adds constant v at offset to the current active global data section. |
| 194 | func (c *Context) AddDatum(offset int, v operand.Constant) { |
| 195 | if err := c.activeglobal().AddDatum(ir.NewDatum(offset, v)); err != nil { |
| 196 | c.adderror(err) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // AppendDatum appends a constant to the current active global data section. |
| 201 | func (c *Context) AppendDatum(v operand.Constant) { |
| 202 | c.activeglobal().Append(v) |
| 203 | } |
| 204 | |
| 205 | func (c *Context) activeglobal() *ir.Global { |
| 206 | if c.global == nil { |
| 207 | c.adderrormessage("no active global") |
| 208 | return ir.NewStaticGlobal("") |
| 209 | } |
| 210 | return c.global |
| 211 | } |
| 212 | |
| 213 | func (c *Context) adderror(err error) { |
| 214 | c.errs.addext(err) |
| 215 | } |
| 216 | |
| 217 | func (c *Context) adderrormessage(msg string) { |
| 218 | c.adderror(errors.New(msg)) |
| 219 | } |
| 220 | |
| 221 | // Result returns the built file and any accumulated errors. |
| 222 | func (c *Context) Result() (*ir.File, error) { |
| 223 | return c.file, c.errs.Err() |
| 224 | } |