| 1 |
(* |
| 2 |
* This module is reponsible for generating garbage collection |
| 3 |
* code for all gc-points in the program. That is, we delay the generation |
| 4 |
* of garbage collection code until all optimizations have been performed. |
| 5 |
* The gc code to be generated is determined by a callback to the client. |
| 6 |
*) |
| 7 |
|
| 8 |
functor GCGen |
| 9 |
(structure MLTreeComp : MLTREECOMP |
| 10 |
structure IR : MLRISC_IR |
| 11 |
structure GCCallBack : GC_CALLBACK |
| 12 |
structure InsnProps : INSN_PROPERTIES |
| 13 |
sharing GCCallBack.T = MLTreeComp.T |
| 14 |
sharing GCCallBack.C = IR.I.C |
| 15 |
sharing MLTreeComp.T.Constant = IR.I.Constant |
| 16 |
sharing MLTreeComp.T.PseudoOp = IR.CFG.P |
| 17 |
sharing IR.I = InsnProps.I = MLTreeComp.I |
| 18 |
) : MLRISC_IR_OPTIMIZATION = |
| 19 |
struct |
| 20 |
|
| 21 |
structure C = IR.I.C |
| 22 |
structure T = MLTreeComp.T |
| 23 |
structure IR = IR |
| 24 |
structure CFG = IR.CFG |
| 25 |
structure GC = GCCallBack.GC |
| 26 |
structure G = Graph |
| 27 |
structure A = Array |
| 28 |
structure Liveness = |
| 29 |
GCLiveness(structure IR = IR |
| 30 |
structure GC = GC |
| 31 |
structure InsnProps = InsnProps) |
| 32 |
|
| 33 |
structure Gen = CFGGen |
| 34 |
(structure CFG = CFG |
| 35 |
structure MLTree = T |
| 36 |
structure InsnProps = InsnProps |
| 37 |
) |
| 38 |
|
| 39 |
type flowgraph = IR.IR |
| 40 |
|
| 41 |
fun error msg = MLRiscErrorMsg.error("GCGen",msg) |
| 42 |
|
| 43 |
val gc_bug = MLRiscControl.getCounter "gc-bug" |
| 44 |
|
| 45 |
val name = "Generate GC code" |
| 46 |
|
| 47 |
fun run (IR as G.GRAPH cfg) = |
| 48 |
let (* |
| 49 |
* Run gc-typed liveness analysis |
| 50 |
*) |
| 51 |
val table = Liveness.liveness IR |
| 52 |
val instrStream = Gen.newStream{compile=fn _ => (), flowgraph=SOME IR} |
| 53 |
fun dummy _ = error "no extension" |
| 54 |
val stream as T.Stream.STREAM{beginCluster, endCluster, ...} = |
| 55 |
MLTreeComp.selectInstructions instrStream |
| 56 |
val cfgAnnotations = CFG.annotations IR |
| 57 |
|
| 58 |
(* |
| 59 |
* For each gc-point, invoke the callback to generate GC code. |
| 60 |
*) |
| 61 |
fun process(b,b' as CFG.BLOCK{annotations,insns,...}) = |
| 62 |
case #get MLRiscAnnotations.GCSAFEPOINT (!annotations) of |
| 63 |
NONE => () |
| 64 |
| SOME msg => |
| 65 |
let val {liveIn,liveOut} = A.sub(table,b) |
| 66 |
val roots = liveIn |
| 67 |
val return = #node_info cfg (hd(#succ cfg b)) |
| 68 |
in CFG.changed IR; |
| 69 |
GCCallBack.callgcCallback |
| 70 |
{ id = b, |
| 71 |
msg = msg, |
| 72 |
gcLabel = CFG.defineLabel b', |
| 73 |
returnLabel = CFG.defineLabel return, |
| 74 |
roots = liveIn, |
| 75 |
stream = stream |
| 76 |
} handle _ => gc_bug := !gc_bug + 1 (* continue on error *) |
| 77 |
end |
| 78 |
|
| 79 |
in beginCluster 0; |
| 80 |
#forall_nodes cfg process; |
| 81 |
endCluster []; |
| 82 |
IR |
| 83 |
end |
| 84 |
|
| 85 |
end |