Debian Codegraph @

ir/ir.go

raw symbols SCIP
1 package ir
2
3 import (
4 "errors"
5
6 "github.com/mmcloughlin/avo/attr"
7 "github.com/mmcloughlin/avo/buildtags"
8 "github.com/mmcloughlin/avo/gotypes"
9 "github.com/mmcloughlin/avo/operand"
10 "github.com/mmcloughlin/avo/reg"
11 )
12
13 // Node is a part of a Function.
14 type Node interface {
15 node()
16 }
17
18 // Label within a function.
19 type Label string
20
21 func (l Label) node() {}
22
23 // Comment represents a multi-line comment.
24 type Comment struct {
25 Lines []string
26 }
27
28 func (c *Comment) node() {}
29
30 // NewComment builds a Comment consisting of the provided lines.
31 func NewComment(lines ...string) *Comment {
32 return &Comment{
33 Lines: lines,
34 }
35 }
36
37 // Instruction is a single instruction in a function.
38 type Instruction struct {
39 Opcode string
40 Suffixes []string
41 Operands []operand.Op
42
43 Inputs []operand.Op
44 Outputs []operand.Op
45
46 IsTerminal bool
47 IsBranch bool
48 IsConditional bool
49 CancellingInputs bool
50
51 // ISA is the list of required instruction set extensions.
52 ISA []string
53
54 // CFG.
55 Pred []*Instruction
56 Succ []*Instruction
57
58 // LiveIn/LiveOut are sets of live register IDs pre/post execution.
59 LiveIn reg.MaskSet
60 LiveOut reg.MaskSet
61 }
62
63 func (i *Instruction) node() {}
64
65 // OpcodeWithSuffixes returns the full opcode, including dot-separated suffixes.
66 func (i *Instruction) OpcodeWithSuffixes() string {
67 opcode := i.Opcode
68 for _, s := range i.Suffixes {
69 opcode += "." + s
70 }
71 return opcode
72 }
73
74 // IsUnconditionalBranch reports whether i is an unconditional branch.
75 func (i Instruction) IsUnconditionalBranch() bool {
76 return i.IsBranch && !i.IsConditional
77 }
78
79 // TargetLabel returns the label referenced by this instruction. Returns nil if
80 // no label is referenced.
81 func (i Instruction) TargetLabel() *Label {
82 if !i.IsBranch {
83 return nil
84 }
85 if len(i.Operands) == 0 {
86 return nil
87 }
88 if ref, ok := i.Operands[0].(operand.LabelRef); ok {
89 lbl := Label(ref)
90 return &lbl
91 }
92 return nil
93 }
94
95 // Registers returns all registers involved in the instruction.
96 func (i Instruction) Registers() []reg.Register {
97 var rs []reg.Register
98 for _, op := range i.Operands {
99 rs = append(rs, operand.Registers(op)...)
100 }
101 return rs
102 }
103
104 // InputRegisters returns all registers read by this instruction.
105 func (i Instruction) InputRegisters() []reg.Register {
106 var rs []reg.Register
107 for _, op := range i.Inputs {
108 rs = append(rs, operand.Registers(op)...)
109 }
110 if i.CancellingInputs && rs[0] == rs[1] {
111 rs = []reg.Register{}
112 }
113 for _, op := range i.Outputs {
114 if operand.IsMem(op) {
115 rs = append(rs, operand.Registers(op)...)
116 }
117 }
118 return rs
119 }
120
121 // OutputRegisters returns all registers written by this instruction.
122 func (i Instruction) OutputRegisters() []reg.Register {
123 var rs []reg.Register
124 for _, op := range i.Outputs {
125 if r, ok := op.(reg.Register); ok {
126 rs = append(rs, r)
127 }
128 }
129 return rs
130 }
131
132 // Section is a part of a file.
133 type Section interface {
134 section()
135 }
136
137 // File represents an assembly file.
138 type File struct {
139 Constraints buildtags.Constraints
140 Includes []string
141 Sections []Section
142 }
143
144 // NewFile initializes an empty file.
145 func NewFile() *File {
146 return &File{}
147 }
148
149 // AddSection appends a Section to the file.
150 func (f *File) AddSection(s Section) {
151 f.Sections = append(f.Sections, s)
152 }
153
154 // Functions returns all functions in the file.
155 func (f *File) Functions() []*Function {
156 var fns []*Function
157 for _, s := range f.Sections {
158 if fn, ok := s.(*Function); ok {
159 fns = append(fns, fn)
160 }
161 }
162 return fns
163 }
164
165 // Pragma represents a function compiler directive.
166 type Pragma struct {
167 Directive string
168 Arguments []string
169 }
170
171 // Function represents an assembly function.
172 type Function struct {
173 Name string
174 Attributes attr.Attribute
175 Pragmas []Pragma
176 Doc []string
177 Signature *gotypes.Signature
178 LocalSize int
179
180 Nodes []Node
181
182 // LabelTarget maps from label name to the following instruction.
183 LabelTarget map[Label]*Instruction
184
185 // Register allocation.
186 Allocation reg.Allocation
187
188 // ISA is the list of required instruction set extensions.
189 ISA []string
190 }
191
192 func (f *Function) section() {}
193
194 // NewFunction builds an empty function of the given name.
195 func NewFunction(name string) *Function {
196 return &Function{
197 Name: name,
198 Signature: gotypes.NewSignatureVoid(),
199 }
200 }
201
202 // AddPragma adds a pragma to this function.
203 func (f *Function) AddPragma(directive string, args ...string) {
204 f.Pragmas = append(f.Pragmas, Pragma{
205 Directive: directive,
206 Arguments: args,
207 })
208 }
209
210 // SetSignature sets the function signature.
211 func (f *Function) SetSignature(s *gotypes.Signature) {
212 f.Signature = s
213 }
214
215 // AllocLocal allocates size bytes in this function's stack.
216 // Returns a reference to the base pointer for the newly allocated region.
217 func (f *Function) AllocLocal(size int) operand.Mem {
218 ptr := operand.NewStackAddr(f.LocalSize)
219 f.LocalSize += size
220 return ptr
221 }
222
223 // AddInstruction appends an instruction to f.
224 func (f *Function) AddInstruction(i *Instruction) {
225 f.AddNode(i)
226 }
227
228 // AddLabel appends a label to f.
229 func (f *Function) AddLabel(l Label) {
230 f.AddNode(l)
231 }
232
233 // AddComment adds comment lines to f.
234 func (f *Function) AddComment(lines ...string) {
235 f.AddNode(NewComment(lines...))
236 }
237
238 // AddNode appends a Node to f.
239 func (f *Function) AddNode(n Node) {
240 f.Nodes = append(f.Nodes, n)
241 }
242
243 // Instructions returns just the list of instruction nodes.
244 func (f *Function) Instructions() []*Instruction {
245 var is []*Instruction
246 for _, n := range f.Nodes {
247 i, ok := n.(*Instruction)
248 if ok {
249 is = append(is, i)
250 }
251 }
252 return is
253 }
254
255 // Labels returns just the list of label nodes.
256 func (f *Function) Labels() []Label {
257 var lbls []Label
258 for _, n := range f.Nodes {
259 lbl, ok := n.(Label)
260 if ok {
261 lbls = append(lbls, lbl)
262 }
263 }
264 return lbls
265 }
266
267 // Stub returns the Go function declaration.
268 func (f *Function) Stub() string {
269 return "func " + f.Name + f.Signature.String()
270 }
271
272 // FrameBytes returns the size of the stack frame in bytes.
273 func (f *Function) FrameBytes() int {
274 return f.LocalSize
275 }
276
277 // ArgumentBytes returns the size of the arguments in bytes.
278 func (f *Function) ArgumentBytes() int {
279 return f.Signature.Bytes()
280 }
281
282 // Datum represents a data element at a particular offset of a data section.
283 type Datum struct {
284 Offset int
285 Value operand.Constant
286 }
287
288 // NewDatum builds a Datum from the given constant.
289 func NewDatum(offset int, v operand.Constant) Datum {
290 return Datum{
291 Offset: offset,
292 Value: v,
293 }
294 }
295
296 // Interval returns the range of bytes this datum will occupy within its section.
297 func (d Datum) Interval() (int, int) {
298 return d.Offset, d.Offset + d.Value.Bytes()
299 }
300
301 // Overlaps returns whether d overlaps with other.
302 func (d Datum) Overlaps(other Datum) bool {
303 s, e := d.Interval()
304 so, eo := other.Interval()
305 return !(eo <= s || e <= so)
306 }
307
308 // Global represents a DATA section.
309 type Global struct {
310 Symbol operand.Symbol
311 Attributes attr.Attribute
312 Data []Datum
313 Size int
314 }
315
316 // NewGlobal constructs an empty DATA section.
317 func NewGlobal(sym operand.Symbol) *Global {
318 return &Global{
319 Symbol: sym,
320 }
321 }
322
323 // NewStaticGlobal is a convenience for building a static DATA section.
324 func NewStaticGlobal(name string) *Global {
325 return NewGlobal(operand.NewStaticSymbol(name))
326 }
327
328 func (g *Global) section() {}
329
330 // Base returns a pointer to the start of the data section.
331 func (g *Global) Base() operand.Mem {
332 return operand.NewDataAddr(g.Symbol, 0)
333 }
334
335 // Grow ensures that the data section has at least the given size.
336 func (g *Global) Grow(size int) {
337 if g.Size < size {
338 g.Size = size
339 }
340 }
341
342 // AddDatum adds d to this data section, growing it if necessary. Errors if the datum overlaps with existing data.
343 func (g *Global) AddDatum(d Datum) error {
344 for _, other := range g.Data {
345 if d.Overlaps(other) {
346 return errors.New("overlaps existing datum")
347 }
348 }
349 g.add(d)
350 return nil
351 }
352
353 // Append the constant to the end of the data section.
354 func (g *Global) Append(v operand.Constant) {
355 g.add(Datum{
356 Offset: g.Size,
357 Value: v,
358 })
359 }
360
361 func (g *Global) add(d Datum) {
362 _, end := d.Interval()
363 g.Grow(end)
364 g.Data = append(g.Data, d)
365 }