| 1 |
local |
| 2 |
|
| 3 |
structure I = |
| 4 |
struct |
| 5 |
include "sparcInstr.sml" |
| 6 |
end |
| 7 |
|
| 8 |
in |
| 9 |
|
| 10 |
functor SparcPeephole |
| 11 |
(structure Instr : SPARCINSTR |
| 12 |
structure Eval : MLTREE_EVAL |
| 13 |
sharing Eval.T = Instr.T |
| 14 |
) : PEEPHOLE = |
| 15 |
struct |
| 16 |
structure I = Instr |
| 17 |
structure CB = CellsBasis |
| 18 |
|
| 19 |
(* IMPORTANT: instructions are now given in forward order *) |
| 20 |
fun peephole instrs = |
| 21 |
let fun isZero(I.LAB le) = ((Eval.valueOf le = 0) handle _ => false) |
| 22 |
| isZero(I.REG r) = CB.registerNum r = 0 |
| 23 |
| isZero(I.IMMED i) = i = 0 |
| 24 |
| isZero _ = false |
| 25 |
|
| 26 |
fun removable(I.INSTR(I.ARITH{a=(I.ADD | I.SUB), r, i, d})) = |
| 27 |
CB.sameColor(r,d) andalso isZero i |
| 28 |
| removable(I.ANNOTATION{i,a}) = removable i |
| 29 |
| removable _ = false |
| 30 |
|
| 31 |
fun loop(current, instrs) = |
| 32 |
case current of |
| 33 |
[] => instrs |
| 34 |
| i::rest where removable i => loop(rest, instrs) |
| 35 |
| i::rest => loop(rest, i::instrs) |
| 36 |
in loop(instrs, []) |
| 37 |
end |
| 38 |
end |
| 39 |
|
| 40 |
end |