get the basic add example working
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/mmcloughlin/avo/gotypes"
|
"github.com/mmcloughlin/avo/gotypes"
|
||||||
|
"github.com/mmcloughlin/avo/pass"
|
||||||
|
|
||||||
"github.com/mmcloughlin/avo"
|
"github.com/mmcloughlin/avo"
|
||||||
"github.com/mmcloughlin/avo/reg"
|
"github.com/mmcloughlin/avo/reg"
|
||||||
@@ -84,6 +85,11 @@ func (c *Context) Main(wout, werr io.Writer) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := pass.Compile.Execute(f); err != nil {
|
||||||
|
diag.Println(err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
p := avo.NewGoPrinter(wout)
|
p := avo.NewGoPrinter(wout)
|
||||||
if err := p.Print(f); err != nil {
|
if err := p.Print(f); err != nil {
|
||||||
diag.Println(err)
|
diag.Println(err)
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/gotypes"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/reg"
|
||||||
|
|
||||||
"github.com/mmcloughlin/avo"
|
"github.com/mmcloughlin/avo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,3 +44,18 @@ func EOF() {
|
|||||||
|
|
||||||
os.Exit(ctx.Main(w, os.Stderr))
|
os.Exit(ctx.Main(w, os.Stderr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GP8v() reg.Virtual { return ctx.GP8v() }
|
||||||
|
func GP16v() reg.Virtual { return ctx.GP16v() }
|
||||||
|
func GP32v() reg.Virtual { return ctx.GP32v() }
|
||||||
|
func GP64v() reg.Virtual { return ctx.GP64v() }
|
||||||
|
func Xv() reg.Virtual { return ctx.Xv() }
|
||||||
|
func Yv() reg.Virtual { return ctx.Yv() }
|
||||||
|
|
||||||
|
func Param(name string) gotypes.Component { return ctx.Param(name) }
|
||||||
|
func ParamIndex(i int) gotypes.Component { return ctx.ParamIndex(i) }
|
||||||
|
func Return(name string) gotypes.Component { return ctx.Return(name) }
|
||||||
|
func ReturnIndex(i int) gotypes.Component { return ctx.ReturnIndex(i) }
|
||||||
|
|
||||||
|
func Load(src gotypes.Component, dst reg.Register) reg.Register { return ctx.Load(src, dst) }
|
||||||
|
func Store(src reg.Register, dst gotypes.Component) { ctx.Store(src, dst) }
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
. "github.com/mmcloughlin/avo/build"
|
. "github.com/mmcloughlin/avo/build"
|
||||||
"github.com/mmcloughlin/avo/reg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
TEXT("add", "func(x, y uint64) uint64")
|
TEXT("add", "func(x, y uint64) uint64")
|
||||||
ADDQ(reg.R8, reg.R11)
|
x := Load(Param("x"), GP64v())
|
||||||
|
y := Load(Param("y"), GP64v())
|
||||||
|
ADDQ(x, y)
|
||||||
|
Store(x, ReturnIndex(0))
|
||||||
RET()
|
RET()
|
||||||
EOF()
|
EOF()
|
||||||
}
|
}
|
||||||
|
|||||||
41
pass/pass.go
41
pass/pass.go
@@ -2,16 +2,43 @@ package pass
|
|||||||
|
|
||||||
import "github.com/mmcloughlin/avo"
|
import "github.com/mmcloughlin/avo"
|
||||||
|
|
||||||
// TODO(mbm): pass types
|
var Compile = Concat(
|
||||||
|
FunctionPass(LabelTarget),
|
||||||
|
FunctionPass(CFG),
|
||||||
|
FunctionPass(Liveness),
|
||||||
|
FunctionPass(AllocateRegisters),
|
||||||
|
FunctionPass(BindRegisters),
|
||||||
|
FunctionPass(VerifyAllocation),
|
||||||
|
)
|
||||||
|
|
||||||
// FunctionPass builds a full pass that operates on all functions independently.
|
type Interface interface {
|
||||||
func FunctionPass(p func(*avo.Function) error) func(*avo.File) error {
|
Execute(*avo.File) error
|
||||||
return func(f *avo.File) error {
|
}
|
||||||
for _, fn := range f.Functions {
|
|
||||||
if err := p(fn); err != nil {
|
type Func func(*avo.File) error
|
||||||
|
|
||||||
|
func (p Func) Execute(f *avo.File) error {
|
||||||
|
return p(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
type FunctionPass func(*avo.Function) error
|
||||||
|
|
||||||
|
func (p FunctionPass) Execute(f *avo.File) error {
|
||||||
|
for _, fn := range f.Functions {
|
||||||
|
if err := p(fn); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Concat(passes ...Interface) Interface {
|
||||||
|
return Func(func(f *avo.File) error {
|
||||||
|
for _, p := range passes {
|
||||||
|
if err := p.Execute(f); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user