| 1 | package build |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "os" |
| 6 | |
| 7 | "github.com/mmcloughlin/avo/attr" |
| 8 | "github.com/mmcloughlin/avo/buildtags" |
| 9 | "github.com/mmcloughlin/avo/gotypes" |
| 10 | "github.com/mmcloughlin/avo/ir" |
| 11 | "github.com/mmcloughlin/avo/operand" |
| 12 | "github.com/mmcloughlin/avo/reg" |
| 13 | ) |
| 14 | |
| 15 | // ctx provides a global build context. |
| 16 | var ctx = NewContext() |
| 17 | |
| 18 | // TEXT starts building a new function called name, with attributes a, and sets its signature (see SignatureExpr). |
| 19 | func TEXT(name string, a attr.Attribute, signature string) { |
| 20 | ctx.Function(name) |
| 21 | ctx.Attributes(a) |
| 22 | ctx.SignatureExpr(signature) |
| 23 | } |
| 24 | |
| 25 | // GLOBL declares a new static global data section with the given attributes. |
| 26 | func GLOBL(name string, a attr.Attribute) operand.Mem { |
| 27 | // TODO(mbm): should this be static? |
| 28 | g := ctx.StaticGlobal(name) |
| 29 | ctx.DataAttributes(a) |
| 30 | return g |
| 31 | } |
| 32 | |
| 33 | // DATA adds a data value to the active data section. |
| 34 | func DATA(offset int, v operand.Constant) { |
| 35 | ctx.AddDatum(offset, v) |
| 36 | } |
| 37 | |
| 38 | var flags = NewFlags(flag.CommandLine) |
| 39 | |
| 40 | // Generate builds and compiles the avo file built with the global context. This |
| 41 | // should be the final line of any avo program. Configuration is determined from command-line flags. |
| 42 | func Generate() { |
| 43 | if !flag.Parsed() { |
| 44 | flag.Parse() |
| 45 | } |
| 46 | cfg := flags.Config() |
| 47 | |
| 48 | status := Main(cfg, ctx) |
| 49 | |
| 50 | // To record coverage of integration tests we wrap main() functions in a test |
| 51 | // functions. In this case we need the main function to terminate, therefore we |
| 52 | // only exit for failure status codes. |
| 53 | if status != 0 { |
| 54 | os.Exit(status) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Package sets the package the generated file will belong to. Required to be able to reference types in the package. |
| 59 | func Package(path string) { ctx.Package(path) } |
| 60 | |
| 61 | // Constraints sets build constraints for the file. |
| 62 | func Constraints(t buildtags.ConstraintsConvertable) { ctx.Constraints(t) } |
| 63 | |
| 64 | // Constraint appends a constraint to the file's build constraints. |
| 65 | func Constraint(t buildtags.ConstraintConvertable) { ctx.Constraint(t) } |
| 66 | |
| 67 | // ConstraintExpr appends a constraint to the file's build constraints. The |
| 68 | // constraint to add is parsed from the given expression. The expression should |
| 69 | // look the same as the content following "// +build " in regular build |
| 70 | // constraint comments. |
| 71 | func ConstraintExpr(expr string) { ctx.ConstraintExpr(expr) } |
| 72 | |
| 73 | // GP8L allocates and returns a general-purpose 8-bit register (low byte). |
| 74 | func GP8L() reg.GPVirtual { return ctx.GP8L() } |
| 75 | |
| 76 | // GP8H allocates and returns a general-purpose 8-bit register (high byte). |
| 77 | func GP8H() reg.GPVirtual { return ctx.GP8H() } |
| 78 | |
| 79 | // GP8 allocates and returns a general-purpose 8-bit register (low byte). |
| 80 | func GP8() reg.GPVirtual { return ctx.GP8() } |
| 81 | |
| 82 | // GP16 allocates and returns a general-purpose 16-bit register. |
| 83 | func GP16() reg.GPVirtual { return ctx.GP16() } |
| 84 | |
| 85 | // GP32 allocates and returns a general-purpose 32-bit register. |
| 86 | func GP32() reg.GPVirtual { return ctx.GP32() } |
| 87 | |
| 88 | // GP64 allocates and returns a general-purpose 64-bit register. |
| 89 | func GP64() reg.GPVirtual { return ctx.GP64() } |
| 90 | |
| 91 | // XMM allocates and returns a 128-bit vector register. |
| 92 | func XMM() reg.VecVirtual { return ctx.XMM() } |
| 93 | |
| 94 | // YMM allocates and returns a 256-bit vector register. |
| 95 | func YMM() reg.VecVirtual { return ctx.YMM() } |
| 96 | |
| 97 | // ZMM allocates and returns a 512-bit vector register. |
| 98 | func ZMM() reg.VecVirtual { return ctx.ZMM() } |
| 99 | |
| 100 | // K allocates and returns an opmask register. |
| 101 | func K() reg.OpmaskVirtual { return ctx.K() } |
| 102 | |
| 103 | // Param returns a the named argument of the active function. |
| 104 | func Param(name string) gotypes.Component { return ctx.Param(name) } |
| 105 | |
| 106 | // ParamIndex returns the ith argument of the active function. |
| 107 | func ParamIndex(i int) gotypes.Component { return ctx.ParamIndex(i) } |
| 108 | |
| 109 | // Return returns a the named return value of the active function. |
| 110 | func Return(name string) gotypes.Component { return ctx.Return(name) } |
| 111 | |
| 112 | // ReturnIndex returns the ith argument of the active function. |
| 113 | func ReturnIndex(i int) gotypes.Component { return ctx.ReturnIndex(i) } |
| 114 | |
| 115 | // Load the function argument src into register dst. Returns the destination |
| 116 | // register. This is syntactic sugar: it will attempt to select the right MOV |
| 117 | // instruction based on the types involved. |
| 118 | func Load(src gotypes.Component, dst reg.Register) reg.Register { return ctx.Load(src, dst) } |
| 119 | |
| 120 | // Store register src into return value dst. This is syntactic sugar: it will |
| 121 | // attempt to select the right MOV instruction based on the types involved. |
| 122 | func Store(src reg.Register, dst gotypes.Component) { ctx.Store(src, dst) } |
| 123 | |
| 124 | // Dereference loads a pointer and returns its element type. |
| 125 | func Dereference(ptr gotypes.Component) gotypes.Component { return ctx.Dereference(ptr) } |
| 126 | |
| 127 | // Function starts building a new function with the given name. |
| 128 | func Function(name string) { ctx.Function(name) } |
| 129 | |
| 130 | // Doc sets documentation comment lines for the currently active function. |
| 131 | func Doc(lines ...string) { ctx.Doc(lines...) } |
| 132 | |
| 133 | // Pragma adds a compiler directive to the currently active function. |
| 134 | func Pragma(directive string, args ...string) { ctx.Pragma(directive, args...) } |
| 135 | |
| 136 | // Attributes sets function attributes for the currently active function. |
| 137 | func Attributes(a attr.Attribute) { ctx.Attributes(a) } |
| 138 | |
| 139 | // SignatureExpr parses the signature expression and sets it as the active function's signature. |
| 140 | func SignatureExpr(expr string) { ctx.SignatureExpr(expr) } |
| 141 | |
| 142 | // Implement starts building a function of the given name, whose type is |
| 143 | // specified by a stub in the containing package. |
| 144 | func Implement(name string) { ctx.Implement(name) } |
| 145 | |
| 146 | // AllocLocal allocates size bytes in the stack of the currently active function. |
| 147 | // Returns a reference to the base pointer for the newly allocated region. |
| 148 | func AllocLocal(size int) operand.Mem { return ctx.AllocLocal(size) } |
| 149 | |
| 150 | // Label adds a label to the active function. |
| 151 | func Label(name string) { ctx.Label(name) } |
| 152 | |
| 153 | // Comment adds comment lines to the active function. |
| 154 | func Comment(lines ...string) { ctx.Comment(lines...) } |
| 155 | |
| 156 | // Commentf adds a formtted comment line. |
| 157 | func Commentf(format string, a ...any) { ctx.Commentf(format, a...) } |
| 158 | |
| 159 | // ConstData builds a static data section containing just the given constant. |
| 160 | func ConstData(name string, v operand.Constant) operand.Mem { return ctx.ConstData(name, v) } |
| 161 | |
| 162 | // Instruction adds an instruction to the active function. |
| 163 | func Instruction(i *ir.Instruction) { ctx.Instruction(i) } |