support signatures and param load/stores

This commit is contained in:
Michael McLoughlin
2018-12-08 21:16:03 -08:00
parent 69ee0e39cb
commit 5431f2edef
9 changed files with 115 additions and 18 deletions

View File

@@ -5,6 +5,8 @@ import (
"io"
"log"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/reg"
)
@@ -28,20 +30,33 @@ func (c *Context) Function(name string) {
c.file.Functions = append(c.file.Functions, c.function)
}
func (c *Context) Signature(s *gotypes.Signature) {
c.activefunc().SetSignature(s)
}
func (c *Context) SignatureExpr(expr string) {
s, err := gotypes.ParseSignature(expr)
if err != nil {
c.AddError(err)
return
}
c.Signature(s)
}
func (c *Context) Instruction(i *avo.Instruction) {
c.node(i)
c.activefunc().AddNode(i)
}
func (c *Context) Label(l avo.Label) {
c.node(l)
c.activefunc().AddLabel(l)
}
func (c *Context) node(n avo.Node) {
func (c *Context) activefunc() *avo.Function {
if c.function == nil {
c.AddErrorMessage("no active function")
return
return avo.NewFunction("")
}
c.function.AddNode(n)
return c.function
}
//go:generate avogen -output zinstructions.go build

View File

@@ -12,7 +12,11 @@ import (
// ctx provides a global build context.
var ctx = NewContext()
func TEXT(name string) { ctx.Function(name) }
func TEXT(name, signature string) {
ctx.Function(name)
ctx.SignatureExpr(signature)
}
func LABEL(name string) { ctx.Label(avo.Label(name)) }
var (

View File

@@ -8,11 +8,37 @@ import (
//go:generate avogen -output zmov.go mov
func (c *Context) Load(src gotypes.Component, dst reg.Register) {
func (c *Context) Param(name string) gotypes.Component {
return c.activefunc().Signature.Params().Lookup(name)
}
func (c *Context) ParamIndex(i int) gotypes.Component {
return c.activefunc().Signature.Params().At(i)
}
func (c *Context) Return(name string) gotypes.Component {
return c.activefunc().Signature.Results().Lookup(name)
}
func (c *Context) ReturnIndex(i int) gotypes.Component {
return c.activefunc().Signature.Results().At(i)
}
func (c *Context) Load(src gotypes.Component, dst reg.Register) reg.Register {
b, err := src.Resolve()
if err != nil {
c.AddError(err)
return dst
}
c.mov(b.Addr, dst, int(gotypes.Sizes.Sizeof(b.Type)), int(dst.Bytes()), b.Type)
return dst
}
func (c *Context) Store(src reg.Register, dst gotypes.Component) {
b, err := dst.Resolve()
if err != nil {
c.AddError(err)
return
}
c.mov(b.Addr, dst, int(gotypes.Sizes.Sizeof(b.Type)), int(dst.Bytes()), b.Type)
c.mov(src, b.Addr, int(src.Bytes()), int(gotypes.Sizes.Sizeof(b.Type)), b.Type)
}