Debian Codegraph @

printer/goasm.go

raw symbols SCIP
1 package printer
2
3 import (
4 "strconv"
5 "strings"
6
7 "github.com/mmcloughlin/avo/buildtags"
8 "github.com/mmcloughlin/avo/internal/prnt"
9 "github.com/mmcloughlin/avo/ir"
10 "github.com/mmcloughlin/avo/operand"
11 )
12
13 // dot is the pesky unicode dot used in Go assembly.
14 const dot = "\u00b7"
15
16 type goasm struct {
17 cfg Config
18 prnt.Generator
19
20 instructions []*ir.Instruction
21 clear bool
22 }
23
24 // NewGoAsm constructs a printer for writing Go assembly files.
25 func NewGoAsm(cfg Config) Printer {
26 return &goasm{cfg: cfg}
27 }
28
29 func (p *goasm) Print(f *ir.File) ([]byte, error) {
30 p.header(f)
31 for _, s := range f.Sections {
32 switch s := s.(type) {
33 case *ir.Function:
34 p.function(s)
35 case *ir.Global:
36 p.global(s)
37 default:
38 panic("unknown section type")
39 }
40 }
41 return p.Result()
42 }
43
44 func (p *goasm) header(f *ir.File) {
45 p.Comment(p.cfg.GeneratedWarning())
46
47 if len(f.Constraints) > 0 {
48 constraints, err := buildtags.Format(f.Constraints)
49 if err != nil {
50 p.AddError(err)
51 }
52 p.NL()
53 p.Printf(constraints)
54 }
55
56 if len(f.Includes) > 0 {
57 p.NL()
58 p.includes(f.Includes)
59 }
60 }
61
62 func (p *goasm) includes(paths []string) {
63 for _, path := range paths {
64 p.Printf("#include \"%s\"\n", path)
65 }
66 }
67
68 func (p *goasm) function(f *ir.Function) {
69 p.NL()
70 p.Comment(f.Stub())
71
72 if len(f.ISA) > 0 {
73 p.Comment("Requires: " + strings.Join(f.ISA, ", "))
74 }
75
76 // Reference: https://github.com/golang/go/blob/b115207baf6c2decc3820ada4574ef4e5ad940ec/src/cmd/internal/obj/util.go#L166-L176
77 //
78 // if p.As == ATEXT {
79 // // If there are attributes, print them. Otherwise, skip the comma.
80 // // In short, print one of these two:
81 // // TEXT foo(SB), DUPOK|NOSPLIT, $0
82 // // TEXT foo(SB), $0
83 // s := p.From.Sym.Attribute.TextAttrString()
84 // if s != "" {
85 // fmt.Fprintf(&buf, "%s%s", sep, s)
86 // sep = ", "
87 // }
88 // }
89 //
90 p.Printf("TEXT %s%s(SB)", dot, f.Name)
91 if f.Attributes != 0 {
92 p.Printf(", %s", f.Attributes.Asm())
93 }
94 p.Printf(", %s\n", textsize(f))
95
96 p.clear = true
97 for _, node := range f.Nodes {
98 switch n := node.(type) {
99 case *ir.Instruction:
100 p.instruction(n)
101 if n.IsTerminal || n.IsUnconditionalBranch() {
102 p.flush()
103 }
104 case ir.Label:
105 p.flush()
106 p.ensureclear()
107 p.Printf("%s:\n", n)
108 case *ir.Comment:
109 p.flush()
110 p.ensureclear()
111 for _, line := range n.Lines {
112 p.Printf("\t// %s\n", line)
113 }
114 default:
115 panic("unexpected node type")
116 }
117 }
118 p.flush()
119 }
120
121 func (p *goasm) instruction(i *ir.Instruction) {
122 p.instructions = append(p.instructions, i)
123 p.clear = false
124 }
125
126 func (p *goasm) flush() {
127 if len(p.instructions) == 0 {
128 return
129 }
130
131 // Determine instruction width. Instructions with no operands are not
132 // considered in this calculation.
133 width := 0
134 for _, i := range p.instructions {
135 opcode := i.OpcodeWithSuffixes()
136 if len(i.Operands) > 0 && len(opcode) > width {
137 width = len(opcode)
138 }
139 }
140
141 // Output instruction block.
142 for _, i := range p.instructions {
143 if len(i.Operands) > 0 {
144 p.Printf("\t%-*s%s\n", width+1, i.OpcodeWithSuffixes(), joinOperands(i.Operands))
145 } else {
146 p.Printf("\t%s\n", i.OpcodeWithSuffixes())
147 }
148 }
149
150 p.instructions = nil
151 }
152
153 func (p *goasm) ensureclear() {
154 if !p.clear {
155 p.NL()
156 p.clear = true
157 }
158 }
159
160 func (p *goasm) global(g *ir.Global) {
161 p.NL()
162 for _, d := range g.Data {
163 a := operand.NewDataAddr(g.Symbol, d.Offset)
164 p.Printf("DATA %s/%d, %s\n", a.Asm(), d.Value.Bytes(), d.Value.Asm())
165 }
166 p.Printf("GLOBL %s(SB), %s, $%d\n", g.Symbol, g.Attributes.Asm(), g.Size)
167 }
168
169 func textsize(f *ir.Function) string {
170 // Reference: https://github.com/golang/go/blob/b115207baf6c2decc3820ada4574ef4e5ad940ec/src/cmd/internal/obj/util.go#L260-L265
171 //
172 // case TYPE_TEXTSIZE:
173 // if a.Val.(int32) == objabi.ArgsSizeUnknown {
174 // str = fmt.Sprintf("$%d", a.Offset)
175 // } else {
176 // str = fmt.Sprintf("$%d-%d", a.Offset, a.Val.(int32))
177 // }
178 //
179 s := "$" + strconv.Itoa(f.FrameBytes())
180 if argsize := f.ArgumentBytes(); argsize > 0 {
181 return s + "-" + strconv.Itoa(argsize)
182 }
183 return s
184 }
185
186 func joinOperands(operands []operand.Op) string {
187 asm := make([]string, len(operands))
188 for i, op := range operands {
189 asm[i] = op.Asm()
190 }
191 return strings.Join(asm, ", ")
192 }