From 20525e14376e4aaf1dd4f6f1cd26e7608046f726 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Sat, 8 Dec 2018 22:02:02 -0800 Subject: [PATCH] get the basic add example working --- build/context.go | 6 ++++++ build/global.go | 19 +++++++++++++++++++ examples/add/asm.go | 6 ++++-- pass/pass.go | 41 ++++++++++++++++++++++++++++++++++------- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/build/context.go b/build/context.go index 9b1212b..400bfdd 100644 --- a/build/context.go +++ b/build/context.go @@ -6,6 +6,7 @@ import ( "log" "github.com/mmcloughlin/avo/gotypes" + "github.com/mmcloughlin/avo/pass" "github.com/mmcloughlin/avo" "github.com/mmcloughlin/avo/reg" @@ -84,6 +85,11 @@ func (c *Context) Main(wout, werr io.Writer) int { return 1 } + if err := pass.Compile.Execute(f); err != nil { + diag.Println(err) + return 1 + } + p := avo.NewGoPrinter(wout) if err := p.Print(f); err != nil { diag.Println(err) diff --git a/build/global.go b/build/global.go index 317b5bc..1a2d808 100644 --- a/build/global.go +++ b/build/global.go @@ -6,6 +6,10 @@ import ( "log" "os" + "github.com/mmcloughlin/avo/gotypes" + + "github.com/mmcloughlin/avo/reg" + "github.com/mmcloughlin/avo" ) @@ -40,3 +44,18 @@ func EOF() { 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) } diff --git a/examples/add/asm.go b/examples/add/asm.go index ff83217..63d99ad 100644 --- a/examples/add/asm.go +++ b/examples/add/asm.go @@ -2,12 +2,14 @@ package main import ( . "github.com/mmcloughlin/avo/build" - "github.com/mmcloughlin/avo/reg" ) func main() { 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() EOF() } diff --git a/pass/pass.go b/pass/pass.go index eff0c33..e60df46 100644 --- a/pass/pass.go +++ b/pass/pass.go @@ -2,16 +2,43 @@ package pass 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. -func FunctionPass(p func(*avo.Function) error) func(*avo.File) error { - return func(f *avo.File) error { - for _, fn := range f.Functions { - if err := p(fn); err != nil { +type Interface interface { + Execute(*avo.File) error +} + +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 nil - } + }) }