ast: change file to have a list of sections
This commit is contained in:
22
ast.go
22
ast.go
@@ -10,10 +10,6 @@ type Asm interface {
|
|||||||
Asm() string
|
Asm() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Operand interface {
|
|
||||||
Asm
|
|
||||||
}
|
|
||||||
|
|
||||||
type Node interface {
|
type Node interface {
|
||||||
node()
|
node()
|
||||||
}
|
}
|
||||||
@@ -90,15 +86,29 @@ func (i Instruction) OutputRegisters() []reg.Register {
|
|||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Section interface {
|
||||||
|
section()
|
||||||
|
}
|
||||||
|
|
||||||
// File represents an assembly file.
|
// File represents an assembly file.
|
||||||
type File struct {
|
type File struct {
|
||||||
Functions []*Function
|
Sections []Section
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFile() *File {
|
func NewFile() *File {
|
||||||
return &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.
|
// Function represents an assembly function.
|
||||||
type Function struct {
|
type Function struct {
|
||||||
Name string
|
Name string
|
||||||
@@ -114,6 +124,8 @@ type Function struct {
|
|||||||
Allocation reg.Allocation
|
Allocation reg.Allocation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f Function) section() {}
|
||||||
|
|
||||||
func NewFunction(name string) *Function {
|
func NewFunction(name string) *Function {
|
||||||
return &Function{
|
return &Function{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func (c *Context) Package(path string) {
|
|||||||
|
|
||||||
func (c *Context) Function(name string) {
|
func (c *Context) Function(name string) {
|
||||||
c.function = avo.NewFunction(name)
|
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) {
|
func (c *Context) Signature(s *gotypes.Signature) {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func (p Func) Execute(f *avo.File) error {
|
|||||||
type FunctionPass func(*avo.Function) error
|
type FunctionPass func(*avo.Function) error
|
||||||
|
|
||||||
func (p FunctionPass) Execute(f *avo.File) 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 {
|
if err := p(fn); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,10 +65,11 @@ func ConstructLiveness(t *testing.T, ctx *build.Context) *avo.Function {
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(f.Functions) != 1 {
|
fns := f.Functions()
|
||||||
|
if len(fns) != 1 {
|
||||||
t.Fatalf("expect 1 function")
|
t.Fatalf("expect 1 function")
|
||||||
}
|
}
|
||||||
fn := f.Functions[0]
|
fn := fns[0]
|
||||||
|
|
||||||
passes := []func(*avo.Function) error{
|
passes := []func(*avo.Function) error{
|
||||||
pass.LabelTarget,
|
pass.LabelTarget,
|
||||||
|
|||||||
@@ -22,8 +22,13 @@ func NewGoAsm(cfg Config) Printer {
|
|||||||
|
|
||||||
func (p *goasm) Print(f *avo.File) ([]byte, error) {
|
func (p *goasm) Print(f *avo.File) ([]byte, error) {
|
||||||
p.header()
|
p.header()
|
||||||
for _, fn := range f.Functions {
|
for _, s := range f.Sections {
|
||||||
p.function(fn)
|
switch s := s.(type) {
|
||||||
|
case *avo.Function:
|
||||||
|
p.function(s)
|
||||||
|
default:
|
||||||
|
panic("unknown section type")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return p.Result()
|
return p.Result()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func (s *stubs) Print(f *avo.File) ([]byte, error) {
|
|||||||
s.Comment(s.cfg.GeneratedWarning())
|
s.Comment(s.cfg.GeneratedWarning())
|
||||||
s.NL()
|
s.NL()
|
||||||
s.Printf("package %s\n", s.cfg.Pkg)
|
s.Printf("package %s\n", s.cfg.Pkg)
|
||||||
for _, fn := range f.Functions {
|
for _, fn := range f.Functions() {
|
||||||
s.NL()
|
s.NL()
|
||||||
s.Printf("%s\n", fn.Stub())
|
s.Printf("%s\n", fn.Stub())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user