summaryrefslogtreecommitdiff
path: root/vendor/github.com/mmcloughlin/avo/ir/ir.go
blob: 6fb92169975f7aeeeda5a023745874cd716c59a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package ir

import (
	"errors"

	"github.com/mmcloughlin/avo/attr"
	"github.com/mmcloughlin/avo/buildtags"
	"github.com/mmcloughlin/avo/gotypes"
	"github.com/mmcloughlin/avo/operand"
	"github.com/mmcloughlin/avo/reg"
)

// Node is a part of a Function.
type Node interface {
	node()
}

// Label within a function.
type Label string

func (l Label) node() {}

// Comment represents a multi-line comment.
type Comment struct {
	Lines []string
}

func (c *Comment) node() {}

// NewComment builds a Comment consisting of the provided lines.
func NewComment(lines ...string) *Comment {
	return &Comment{
		Lines: lines,
	}
}

// Instruction is a single instruction in a function.
type Instruction struct {
	Opcode   string
	Operands []operand.Op

	Inputs  []operand.Op
	Outputs []operand.Op

	IsTerminal       bool
	IsBranch         bool
	IsConditional    bool
	CancellingInputs bool

	// ISA is the list of required instruction set extensions.
	ISA []string

	// CFG.
	Pred []*Instruction
	Succ []*Instruction

	// LiveIn/LiveOut are sets of live register IDs pre/post execution.
	LiveIn  reg.MaskSet
	LiveOut reg.MaskSet
}

func (i *Instruction) node() {}

// IsUnconditionalBranch reports whether i is an unconditional branch.
func (i Instruction) IsUnconditionalBranch() bool {
	return i.IsBranch && !i.IsConditional
}

// TargetLabel returns the label referenced by this instruction. Returns nil if
// no label is referenced.
func (i Instruction) TargetLabel() *Label {
	if !i.IsBranch {
		return nil
	}
	if len(i.Operands) == 0 {
		return nil
	}
	if ref, ok := i.Operands[0].(operand.LabelRef); ok {
		lbl := Label(ref)
		return &lbl
	}
	return nil
}

// Registers returns all registers involved in the instruction.
func (i Instruction) Registers() []reg.Register {
	var rs []reg.Register
	for _, op := range i.Operands {
		rs = append(rs, operand.Registers(op)...)
	}
	return rs
}

// InputRegisters returns all registers read by this instruction.
func (i Instruction) InputRegisters() []reg.Register {
	var rs []reg.Register
	for _, op := range i.Inputs {
		rs = append(rs, operand.Registers(op)...)
	}
	if i.CancellingInputs && rs[0] == rs[1] {
		rs = []reg.Register{}
	}
	for _, op := range i.Outputs {
		if operand.IsMem(op) {
			rs = append(rs, operand.Registers(op)...)
		}
	}
	return rs
}

// OutputRegisters returns all registers written by this instruction.
func (i Instruction) OutputRegisters() []reg.Register {
	var rs []reg.Register
	for _, op := range i.Outputs {
		if r, ok := op.(reg.Register); ok {
			rs = append(rs, r)
		}
	}
	return rs
}

// Section is a part of a file.
type Section interface {
	section()
}

// File represents an assembly file.
type File struct {
	Constraints buildtags.Constraints
	Includes    []string
	Sections    []Section
}

// NewFile initializes an empty file.
func NewFile() *File {
	return &File{}
}

// AddSection appends a Section to the file.
func (f *File) AddSection(s Section) {
	f.Sections = append(f.Sections, s)
}

// Functions returns all functions in the 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
}

// Pragma represents a function compiler directive.
type Pragma struct {
	Directive string
	Arguments []string
}

// Function represents an assembly function.
type Function struct {
	Name       string
	Attributes attr.Attribute
	Pragmas    []Pragma
	Doc        []string
	Signature  *gotypes.Signature
	LocalSize  int

	Nodes []Node

	// LabelTarget maps from label name to the following instruction.
	LabelTarget map[Label]*Instruction

	// Register allocation.
	Allocation reg.Allocation

	// ISA is the list of required instruction set extensions.
	ISA []string
}

func (f *Function) section() {}

// NewFunction builds an empty function of the given name.
func NewFunction(name string) *Function {
	return &Function{
		Name:      name,
		Signature: gotypes.NewSignatureVoid(),
	}
}

// AddPragma adds a pragma to this function.
func (f *Function) AddPragma(directive string, args ...string) {
	f.Pragmas = append(f.Pragmas, Pragma{
		Directive: directive,
		Arguments: args,
	})
}

// SetSignature sets the function signature.
func (f *Function) SetSignature(s *gotypes.Signature) {
	f.Signature = s
}

// AllocLocal allocates size bytes in this function's stack.
// Returns a reference to the base pointer for the newly allocated region.
func (f *Function) AllocLocal(size int) operand.Mem {
	ptr := operand.NewStackAddr(f.LocalSize)
	f.LocalSize += size
	return ptr
}

// AddInstruction appends an instruction to f.
func (f *Function) AddInstruction(i *Instruction) {
	f.AddNode(i)
}

// AddLabel appends a label to f.
func (f *Function) AddLabel(l Label) {
	f.AddNode(l)
}

// AddComment adds comment lines to f.
func (f *Function) AddComment(lines ...string) {
	f.AddNode(NewComment(lines...))
}

// AddNode appends a Node to f.
func (f *Function) AddNode(n Node) {
	f.Nodes = append(f.Nodes, n)
}

// Instructions returns just the list of instruction nodes.
func (f *Function) Instructions() []*Instruction {
	var is []*Instruction
	for _, n := range f.Nodes {
		i, ok := n.(*Instruction)
		if ok {
			is = append(is, i)
		}
	}
	return is
}

// Labels returns just the list of label nodes.
func (f *Function) Labels() []Label {
	var lbls []Label
	for _, n := range f.Nodes {
		lbl, ok := n.(Label)
		if ok {
			lbls = append(lbls, lbl)
		}
	}
	return lbls
}

// Stub returns the Go function declaration.
func (f *Function) Stub() string {
	return "func " + f.Name + f.Signature.String()
}

// FrameBytes returns the size of the stack frame in bytes.
func (f *Function) FrameBytes() int {
	return f.LocalSize
}

// ArgumentBytes returns the size of the arguments in bytes.
func (f *Function) ArgumentBytes() int {
	return f.Signature.Bytes()
}

// Datum represents a data element at a particular offset of a data section.
type Datum struct {
	Offset int
	Value  operand.Constant
}

// NewDatum builds a Datum from the given constant.
func NewDatum(offset int, v operand.Constant) Datum {
	return Datum{
		Offset: offset,
		Value:  v,
	}
}

// Interval returns the range of bytes this datum will occupy within its section.
func (d Datum) Interval() (int, int) {
	return d.Offset, d.Offset + d.Value.Bytes()
}

// Overlaps returns true
func (d Datum) Overlaps(other Datum) bool {
	s, e := d.Interval()
	so, eo := other.Interval()
	return !(eo <= s || e <= so)
}

// Global represents a DATA section.
type Global struct {
	Symbol     operand.Symbol
	Attributes attr.Attribute
	Data       []Datum
	Size       int
}

// NewGlobal constructs an empty DATA section.
func NewGlobal(sym operand.Symbol) *Global {
	return &Global{
		Symbol: sym,
	}
}

// NewStaticGlobal is a convenience for building a static DATA section.
func NewStaticGlobal(name string) *Global {
	return NewGlobal(operand.NewStaticSymbol(name))
}

func (g *Global) section() {}

// Base returns a pointer to the start of the data section.
func (g *Global) Base() operand.Mem {
	return operand.NewDataAddr(g.Symbol, 0)
}

// Grow ensures that the data section has at least the given size.
func (g *Global) Grow(size int) {
	if g.Size < size {
		g.Size = size
	}
}

// AddDatum adds d to this data section, growing it if necessary. Errors if the datum overlaps with existing data.
func (g *Global) AddDatum(d Datum) error {
	for _, other := range g.Data {
		if d.Overlaps(other) {
			return errors.New("overlaps existing datum")
		}
	}
	g.add(d)
	return nil
}

// Append the constant to the end of the data section.
func (g *Global) Append(v operand.Constant) {
	g.add(Datum{
		Offset: g.Size,
		Value:  v,
	})
}

func (g *Global) add(d Datum) {
	_, end := d.Interval()
	g.Grow(end)
	g.Data = append(g.Data, d)
}