| 1 | package build |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mmcloughlin/avo/attr" |
| 5 | "github.com/mmcloughlin/avo/gotypes" |
| 6 | "github.com/mmcloughlin/avo/operand" |
| 7 | "github.com/mmcloughlin/avo/reg" |
| 8 | ) |
| 9 | |
| 10 | //go:generate avogen -output zmov.go mov |
| 11 | |
| 12 | // Param returns a the named argument of the active function. |
| 13 | func (c *Context) Param(name string) gotypes.Component { |
| 14 | return c.activefunc().Signature.Params().Lookup(name) |
| 15 | } |
| 16 | |
| 17 | // ParamIndex returns the ith argument of the active function. |
| 18 | func (c *Context) ParamIndex(i int) gotypes.Component { |
| 19 | return c.activefunc().Signature.Params().At(i) |
| 20 | } |
| 21 | |
| 22 | // Return returns a the named return value of the active function. |
| 23 | func (c *Context) Return(name string) gotypes.Component { |
| 24 | return c.activefunc().Signature.Results().Lookup(name) |
| 25 | } |
| 26 | |
| 27 | // ReturnIndex returns the ith argument of the active function. |
| 28 | func (c *Context) ReturnIndex(i int) gotypes.Component { |
| 29 | return c.activefunc().Signature.Results().At(i) |
| 30 | } |
| 31 | |
| 32 | // Load the function argument src into register dst. Returns the destination |
| 33 | // register. This is syntactic sugar: it will attempt to select the right MOV |
| 34 | // instruction based on the types involved. |
| 35 | func (c *Context) Load(src gotypes.Component, dst reg.Register) reg.Register { |
| 36 | b, err := src.Resolve() |
| 37 | if err != nil { |
| 38 | c.adderror(err) |
| 39 | return dst |
| 40 | } |
| 41 | c.mov(b.Addr, dst, int(gotypes.Sizes.Sizeof(b.Type)), int(dst.Size()), b.Type) |
| 42 | return dst |
| 43 | } |
| 44 | |
| 45 | // Store register src into return value dst. This is syntactic sugar: it will |
| 46 | // attempt to select the right MOV instruction based on the types involved. |
| 47 | func (c *Context) Store(src reg.Register, dst gotypes.Component) { |
| 48 | b, err := dst.Resolve() |
| 49 | if err != nil { |
| 50 | c.adderror(err) |
| 51 | return |
| 52 | } |
| 53 | c.mov(src, b.Addr, int(src.Size()), int(gotypes.Sizes.Sizeof(b.Type)), b.Type) |
| 54 | } |
| 55 | |
| 56 | // Dereference loads a pointer and returns its element type. |
| 57 | func (c *Context) Dereference(ptr gotypes.Component) gotypes.Component { |
| 58 | r := c.GP64() |
| 59 | c.Load(ptr, r) |
| 60 | return ptr.Dereference(r) |
| 61 | } |
| 62 | |
| 63 | // ConstData builds a static data section containing just the given constant. |
| 64 | func (c *Context) ConstData(name string, v operand.Constant) operand.Mem { |
| 65 | g := c.StaticGlobal(name) |
| 66 | c.DataAttributes(attr.RODATA | attr.NOPTR) |
| 67 | c.AppendDatum(v) |
| 68 | return g |
| 69 | } |