ast: change file to have a list of sections

This commit is contained in:
Michael McLoughlin
2018-12-26 18:58:12 -08:00
parent bf6031521f
commit d29c6340d7
6 changed files with 30 additions and 12 deletions

22
ast.go
View File

@@ -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,

View File

@@ -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) {

View File

@@ -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
}

View File

@@ -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,

View File

@@ -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()
}

View File

@@ -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())
}