diff --git a/ast.go b/ast.go index 6fb60fc..b4f5cd5 100644 --- a/ast.go +++ b/ast.go @@ -10,10 +10,6 @@ type Asm interface { Asm() string } -type Operand interface { - Asm -} - type Node interface { node() } @@ -90,15 +86,29 @@ func (i Instruction) OutputRegisters() []reg.Register { return rs } +type Section interface { + section() +} + // File represents an assembly file. type File struct { - Functions []*Function + Sections []Section } func NewFile() *File { return &File{} } +func (f *File) Functions() []*Function { + var fns []*Function + for _, s := range f.Sections { + if fn, ok := s.(*Function); ok { + fns = append(fns, fn) + } + } + return fns +} + // Function represents an assembly function. type Function struct { Name string @@ -114,6 +124,8 @@ type Function struct { Allocation reg.Allocation } +func (f Function) section() {} + func NewFunction(name string) *Function { return &Function{ Name: name, diff --git a/build/context.go b/build/context.go index 4891376..429bd00 100644 --- a/build/context.go +++ b/build/context.go @@ -47,7 +47,7 @@ func (c *Context) Package(path string) { func (c *Context) Function(name string) { c.function = avo.NewFunction(name) - c.file.Functions = append(c.file.Functions, c.function) + c.file.Sections = append(c.file.Sections, c.function) } func (c *Context) Signature(s *gotypes.Signature) { diff --git a/pass/pass.go b/pass/pass.go index b9e2a9f..530bd1e 100644 --- a/pass/pass.go +++ b/pass/pass.go @@ -29,7 +29,7 @@ func (p Func) Execute(f *avo.File) error { type FunctionPass func(*avo.Function) error func (p FunctionPass) Execute(f *avo.File) error { - for _, fn := range f.Functions { + for _, fn := range f.Functions() { if err := p(fn); err != nil { return err } diff --git a/pass/reg_test.go b/pass/reg_test.go index 901a2cd..59e2aa1 100644 --- a/pass/reg_test.go +++ b/pass/reg_test.go @@ -65,10 +65,11 @@ func ConstructLiveness(t *testing.T, ctx *build.Context) *avo.Function { t.FailNow() } - if len(f.Functions) != 1 { + fns := f.Functions() + if len(fns) != 1 { t.Fatalf("expect 1 function") } - fn := f.Functions[0] + fn := fns[0] passes := []func(*avo.Function) error{ pass.LabelTarget, diff --git a/printer/goasm.go b/printer/goasm.go index bb3f663..3680368 100644 --- a/printer/goasm.go +++ b/printer/goasm.go @@ -22,8 +22,13 @@ func NewGoAsm(cfg Config) Printer { func (p *goasm) Print(f *avo.File) ([]byte, error) { p.header() - for _, fn := range f.Functions { - p.function(fn) + for _, s := range f.Sections { + switch s := s.(type) { + case *avo.Function: + p.function(s) + default: + panic("unknown section type") + } } return p.Result() } diff --git a/printer/stubs.go b/printer/stubs.go index b1ae7a6..7a25332 100644 --- a/printer/stubs.go +++ b/printer/stubs.go @@ -18,7 +18,7 @@ func (s *stubs) Print(f *avo.File) ([]byte, error) { s.Comment(s.cfg.GeneratedWarning()) s.NL() s.Printf("package %s\n", s.cfg.Pkg) - for _, fn := range f.Functions { + for _, fn := range f.Functions() { s.NL() s.Printf("%s\n", fn.Stub()) }