| 1 | // Package pass implements processing passes on avo Files. |
| 2 | package pass |
| 3 | |
| 4 | import ( |
| 5 | "io" |
| 6 | |
| 7 | "github.com/mmcloughlin/avo/ir" |
| 8 | "github.com/mmcloughlin/avo/printer" |
| 9 | ) |
| 10 | |
| 11 | // Compile pass compiles an avo file. Upon successful completion the avo file |
| 12 | // may be printed to Go assembly. |
| 13 | var Compile = Concat( |
| 14 | Verify, |
| 15 | FunctionPass(PruneJumpToFollowingLabel), |
| 16 | FunctionPass(PruneDanglingLabels), |
| 17 | FunctionPass(LabelTarget), |
| 18 | FunctionPass(CFG), |
| 19 | InstructionPass(ZeroExtend32BitOutputs), |
| 20 | FunctionPass(Liveness), |
| 21 | FunctionPass(AllocateRegisters), |
| 22 | FunctionPass(BindRegisters), |
| 23 | FunctionPass(VerifyAllocation), |
| 24 | FunctionPass(EnsureBasePointerCalleeSaved), |
| 25 | Func(IncludeTextFlagHeader), |
| 26 | FunctionPass(PruneSelfMoves), |
| 27 | FunctionPass(RequiredISAExtensions), |
| 28 | ) |
| 29 | |
| 30 | // Interface for a processing pass. |
| 31 | type Interface interface { |
| 32 | Execute(*ir.File) error |
| 33 | } |
| 34 | |
| 35 | // Func adapts a function to the pass Interface. |
| 36 | type Func func(*ir.File) error |
| 37 | |
| 38 | // Execute calls p. |
| 39 | func (p Func) Execute(f *ir.File) error { |
| 40 | return p(f) |
| 41 | } |
| 42 | |
| 43 | // FunctionPass is a convenience for implementing a full file pass with a |
| 44 | // function that operates on each avo Function independently. |
| 45 | type FunctionPass func(*ir.Function) error |
| 46 | |
| 47 | // Execute calls p on every function in the file. Exits on the first error. |
| 48 | func (p FunctionPass) Execute(f *ir.File) error { |
| 49 | for _, fn := range f.Functions() { |
| 50 | if err := p(fn); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | // InstructionPass is a convenience for implementing a full file pass with a |
| 58 | // function that operates on each Instruction independently. |
| 59 | type InstructionPass func(*ir.Instruction) error |
| 60 | |
| 61 | // Execute calls p on every instruction in the file. Exits on the first error. |
| 62 | func (p InstructionPass) Execute(f *ir.File) error { |
| 63 | for _, fn := range f.Functions() { |
| 64 | for _, i := range fn.Instructions() { |
| 65 | if err := p(i); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | // Concat returns a pass that executes the given passes in order, stopping on the first error. |
| 74 | func Concat(passes ...Interface) Interface { |
| 75 | return Func(func(f *ir.File) error { |
| 76 | for _, p := range passes { |
| 77 | if err := p.Execute(f); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | } |
| 81 | return nil |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | // Output pass prints a file. |
| 86 | type Output struct { |
| 87 | Writer io.WriteCloser |
| 88 | Printer printer.Printer |
| 89 | } |
| 90 | |
| 91 | // Execute prints f with the configured Printer and writes output to Writer. |
| 92 | func (o *Output) Execute(f *ir.File) error { |
| 93 | b, err := o.Printer.Print(f) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | if _, err = o.Writer.Write(b); err != nil { |
| 98 | return err |
| 99 | } |
| 100 | return o.Writer.Close() |
| 101 | } |