summaryrefslogtreecommitdiff
path: root/vendor/github.com/mmcloughlin/avo/pass/pass.go
blob: 62f37b1079f333c4f42bd0ffaa033820dd08f15e (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
// Package pass implements processing passes on avo Files.
package pass

import (
	"io"

	"github.com/mmcloughlin/avo/ir"
	"github.com/mmcloughlin/avo/printer"
)

// Compile pass compiles an avo file. Upon successful completion the avo file
// may be printed to Go assembly.
var Compile = Concat(
	Verify,
	FunctionPass(PruneJumpToFollowingLabel),
	FunctionPass(PruneDanglingLabels),
	FunctionPass(LabelTarget),
	FunctionPass(CFG),
	InstructionPass(ZeroExtend32BitOutputs),
	FunctionPass(Liveness),
	FunctionPass(AllocateRegisters),
	FunctionPass(BindRegisters),
	FunctionPass(VerifyAllocation),
	Func(IncludeTextFlagHeader),
	FunctionPass(PruneSelfMoves),
	FunctionPass(RequiredISAExtensions),
)

// Interface for a processing pass.
type Interface interface {
	Execute(*ir.File) error
}

// Func adapts a function to the pass Interface.
type Func func(*ir.File) error

// Execute calls p.
func (p Func) Execute(f *ir.File) error {
	return p(f)
}

// FunctionPass is a convenience for implementing a full file pass with a
// function that operates on each avo Function independently.
type FunctionPass func(*ir.Function) error

// Execute calls p on every function in the file. Exits on the first error.
func (p FunctionPass) Execute(f *ir.File) error {
	for _, fn := range f.Functions() {
		if err := p(fn); err != nil {
			return err
		}
	}
	return nil
}

// InstructionPass is a convenience for implementing a full file pass with a
// function that operates on each Instruction independently.
type InstructionPass func(*ir.Instruction) error

// Execute calls p on every instruction in the file. Exits on the first error.
func (p InstructionPass) Execute(f *ir.File) error {
	for _, fn := range f.Functions() {
		for _, i := range fn.Instructions() {
			if err := p(i); err != nil {
				return err
			}
		}
	}
	return nil
}

// Concat returns a pass that executes the given passes in order, stopping on the first error.
func Concat(passes ...Interface) Interface {
	return Func(func(f *ir.File) error {
		for _, p := range passes {
			if err := p.Execute(f); err != nil {
				return err
			}
		}
		return nil
	})
}

// Output pass prints a file.
type Output struct {
	Writer  io.WriteCloser
	Printer printer.Printer
}

// Execute prints f with the configured Printer and writes output to Writer.
func (o *Output) Execute(f *ir.File) error {
	b, err := o.Printer.Print(f)
	if err != nil {
		return err
	}
	if _, err = o.Writer.Write(b); err != nil {
		return err
	}
	return o.Writer.Close()
}