ast,build: add Attributes fields to Function and Global

Updates #2
This commit is contained in:
Michael McLoughlin
2018-12-30 23:35:49 -08:00
parent 5d270d8d76
commit e364d6398e
17 changed files with 86 additions and 50 deletions

View File

@@ -49,7 +49,26 @@ func (p *goasm) include(path string) {
func (p *goasm) function(f *avo.Function) {
p.NL()
p.Comment(f.Stub())
p.Printf("TEXT %s%s(SB), 0, %s\n", dot, f.Name, textsize(f))
// Reference: https://github.com/golang/go/blob/b115207baf6c2decc3820ada4574ef4e5ad940ec/src/cmd/internal/obj/util.go#L166-L176
//
// if p.As == ATEXT {
// // If there are attributes, print them. Otherwise, skip the comma.
// // In short, print one of these two:
// // TEXT foo(SB), DUPOK|NOSPLIT, $0
// // TEXT foo(SB), $0
// s := p.From.Sym.Attribute.TextAttrString()
// if s != "" {
// fmt.Fprintf(&buf, "%s%s", sep, s)
// sep = ", "
// }
// }
//
p.Printf("TEXT %s%s(SB)", dot, f.Name)
if f.Attributes != 0 {
p.Printf(", %s", f.Attributes.Asm())
}
p.Printf(", %s\n", textsize(f))
for _, node := range f.Nodes {
switch n := node.(type) {
@@ -73,8 +92,7 @@ func (p *goasm) global(g *avo.Global) {
a := operand.NewDataAddr(g.Symbol, d.Offset)
p.Printf("DATA %s/%d, %s\n", a.Asm(), d.Value.Bytes(), d.Value.Asm())
}
// TODO(mbm): replace hardcoded RODATA with an attributes list
p.Printf("GLOBL %s(SB), RODATA, $%d\n", g.Symbol, g.Size)
p.Printf("GLOBL %s(SB), %s, $%d\n", g.Symbol, g.Attributes.Asm(), g.Size)
}
func textsize(f *avo.Function) string {

View File

@@ -3,6 +3,7 @@ package printer_test
import (
"testing"
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/printer"
"github.com/mmcloughlin/avo/reg"
@@ -24,7 +25,7 @@ func TestBasic(t *testing.T) {
"#include \"textflag.h\"",
"",
"// func add(x uint64, y uint64) uint64",
"TEXT ·add(SB), 0, $0-24",
"TEXT ·add(SB), $0-24",
"\tMOVQ\tx(FP), AX",
"\tMOVQ\ty+8(FP), R9",
"\tADDQ\tAX, R9",
@@ -34,7 +35,7 @@ func TestBasic(t *testing.T) {
})
}
func TestTextSize(t *testing.T) {
func TestTextDecl(t *testing.T) {
ctx := build.NewContext()
ctx.Function("noargs")
@@ -46,17 +47,26 @@ func TestTextSize(t *testing.T) {
ctx.SignatureExpr("func(x, y uint64) uint64")
ctx.RET()
ctx.Function("withattr")
ctx.SignatureExpr("func()")
ctx.Attributes(avo.NOSPLIT | avo.TLSBSS)
ctx.RET()
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"#include \"textflag.h\"",
"",
"// func noargs()",
"TEXT ·noargs(SB), 0, $16", // expect only the frame size
"TEXT ·noargs(SB), $16", // expect only the frame size
"\tRET",
"",
"// func withargs(x uint64, y uint64) uint64",
"TEXT ·withargs(SB), 0, $0-24", // expect both frame size and argument size
"TEXT ·withargs(SB), $0-24", // expect both frame size and argument size
"\tRET",
"",
"// func withattr()",
"TEXT ·withattr(SB), NOSPLIT|TLSBSS, $0", // expect to see attributes
"\tRET",
"",
})