| 1 |
(* |
| 2 |
* This module performs low-level flow insensitive points-to |
| 3 |
* analysis for type-safe languages. |
| 4 |
*) |
| 5 |
structure PointsTo : POINTS_TO = |
| 6 |
struct |
| 7 |
|
| 8 |
datatype edgekind = PI | DOM | RAN | RECORD | MARK |
| 9 |
|
| 10 |
structure C = CellsBasis |
| 11 |
|
| 12 |
datatype cell = |
| 13 |
LINK of cell ref |
| 14 |
| SREF of C.cell * (edgekind * int * cell ref) list ref |
| 15 |
| WREF of C.cell * (edgekind * int * cell ref) list ref |
| 16 |
| SCELL of C.cell * (edgekind * int * cell ref) list ref |
| 17 |
| WCELL of C.cell * (edgekind * int * cell ref) list ref |
| 18 |
| TOP of {mutable:bool, id:C.cell, name:string} |
| 19 |
(* a collapsed node *) |
| 20 |
type region = cell ref |
| 21 |
type edges = (edgekind * int * region) list |
| 22 |
|
| 23 |
fun error msg = MLRiscErrorMsg.error("PointsTo",msg) |
| 24 |
|
| 25 |
(* PI > DOM > RAN > RECORD *) |
| 26 |
fun greaterKind(PI,_) = false |
| 27 |
| greaterKind(DOM,PI) = false |
| 28 |
| greaterKind(RAN,(PI | DOM)) = false |
| 29 |
| greaterKind(RECORD,(PI | DOM | RAN)) = false |
| 30 |
| greaterKind(MARK,(PI | DOM | RAN | RECORD)) = false |
| 31 |
| greaterKind _ = true |
| 32 |
|
| 33 |
fun less(k,i,k',i') = k=k' andalso i > i' orelse greaterKind(k,k') |
| 34 |
|
| 35 |
val sort : (edgekind * int * region) list -> |
| 36 |
(edgekind * int * region) list = |
| 37 |
ListMergeSort.sort (fn ((k,i,_),(k',i',_)) => less(k,i,k',i')) |
| 38 |
|
| 39 |
val newMem = ref(fn _ => error "newMem") : (unit -> C.cell) ref |
| 40 |
fun reset f = newMem := f |
| 41 |
|
| 42 |
fun newSRef() = ref(SREF(!newMem(),ref [])) |
| 43 |
fun newWRef() = ref(WREF(!newMem(),ref [])) |
| 44 |
fun newSCell() = ref(SCELL(!newMem(),ref [])) |
| 45 |
fun newWCell() = ref(WCELL(!newMem(),ref [])) |
| 46 |
fun newTop{name,mutable} = |
| 47 |
ref(TOP{mutable=mutable, id= !newMem(), name=name}) |
| 48 |
|
| 49 |
fun find(ref(LINK x)) = find x |
| 50 |
| find x = x |
| 51 |
|
| 52 |
fun mut(ref(LINK x)) = mut x |
| 53 |
| mut(r as ref(TOP{mutable=false, id, name})) = |
| 54 |
(r := TOP{mutable=true, id=id, name=name}) |
| 55 |
| mut(r as ref(SCELL x)) = r := SREF x |
| 56 |
| mut(r as ref(WCELL x)) = r := WREF x |
| 57 |
| mut _ = () |
| 58 |
|
| 59 |
and weak(ref(LINK x)) = weak x |
| 60 |
| weak(ref(TOP _)) = () |
| 61 |
| weak(r as ref(SCELL x)) = (r := WCELL x; mergePis x) |
| 62 |
| weak(r as ref(SREF x)) = (r := WREF x; mergePis x) |
| 63 |
| weak _ = () |
| 64 |
|
| 65 |
and mergePis(_,edges) = |
| 66 |
let val x = newSCell() |
| 67 |
fun merge([],es') = es' |
| 68 |
| merge((PI,_,y)::es,es') = (unify(x,y); merge(es, es')) |
| 69 |
| merge(e::es,es') = merge(es,e::es') |
| 70 |
in edges := (PI,0,x)::merge(!edges, []) end |
| 71 |
|
| 72 |
and getIth(k,i,ref(LINK x)) = getIth(k,i,x) |
| 73 |
| getIth(k,i,r as ref(TOP _)) = r |
| 74 |
| getIth(k,i,ref(SREF(_,edges))) = getIth'(k,i,edges) |
| 75 |
| getIth(k,i,ref(WREF(_,edges))) = getIth'(k,i,edges) |
| 76 |
| getIth(k,i,ref(SCELL(_,edges))) = getIth'(k,i,edges) |
| 77 |
| getIth(k,i,ref(WCELL(_,edges))) = getIth'(k,i,edges) |
| 78 |
|
| 79 |
and getIth'(k,i,edges) = |
| 80 |
let fun search((k',i',x)::es) = |
| 81 |
if k = k' andalso i = i' then find x else search es |
| 82 |
| search [] = |
| 83 |
let val x = newSCell() |
| 84 |
in edges := (k,i,x) :: !edges; x end |
| 85 |
in search(!edges) end |
| 86 |
|
| 87 |
and unify(x,y) = |
| 88 |
let val x = find x |
| 89 |
val y = find y |
| 90 |
fun linkImmut(edges,x,y) = (x := LINK y; collapseAll(!edges,y)) |
| 91 |
fun linkMut(edges,x,y) = (x := LINK y; mut y; collapseAll(!edges,y)) |
| 92 |
fun linky(ex,ey,x,y) = (x := LINK y; ey := unifyList(!ex,!ey)) |
| 93 |
fun linkx(ex,ey,x,y) = (y := LINK x; ex := unifyList(!ex,!ey)) |
| 94 |
fun linkWREF(ex,ey,id,x,y) = |
| 95 |
let val ey = unifyList(!ex,!ey) |
| 96 |
val n = WREF(id,ref ey) |
| 97 |
in x := LINK y; y := n end |
| 98 |
|
| 99 |
in if x = y then () else |
| 100 |
case (!x,!y) of |
| 101 |
(TOP{mutable=false,...},TOP{mutable=false, ...}) => (x := LINK y) |
| 102 |
| (TOP _, TOP _) => (x := LINK y; mut y) |
| 103 |
|
| 104 |
| (SREF(_,edges), TOP _) => linkMut(edges,x,y) |
| 105 |
| (WREF(_,edges), TOP _) => linkMut(edges,x,y) |
| 106 |
| (SCELL(_,edges), TOP _) => linkImmut(edges,x,y) |
| 107 |
| (WCELL(_,edges), TOP _) => linkImmut(edges,x,y) |
| 108 |
|
| 109 |
| (TOP _, SREF(_,edges)) => linkMut(edges,y,x) |
| 110 |
| (TOP _, WREF(_,edges)) => linkMut(edges,y,x) |
| 111 |
| (TOP _, SCELL(_,edges)) => linkImmut(edges,y,x) |
| 112 |
| (TOP _, WCELL(_,edges)) => linkImmut(edges,y,x) |
| 113 |
|
| 114 |
| (WREF(_,e1), WREF(_,e2)) => linky(e1,e2,x,y) |
| 115 |
| (SREF(_,e1), WREF(_,e2)) => linky(e1,e2,x,y) |
| 116 |
| (WCELL(_,e1),WREF(_,e2)) => linky(e1,e2,x,y) |
| 117 |
| (SCELL(_,e1),WREF(_,e2)) => linky(e1,e2,x,y) |
| 118 |
|
| 119 |
| (WREF(_,e1), SREF(_,e2)) => linkx(e1,e2,x,y) |
| 120 |
| (SREF(_,e1), SREF(_,e2)) => linkx(e1,e2,x,y) |
| 121 |
| (WCELL(_,e1),SREF(id,e2)) => linkWREF(e1,e2,id,x,y) |
| 122 |
| (SCELL(_,e1),SREF(_,e2)) => linky(e1,e2,x,y) |
| 123 |
|
| 124 |
| (WREF(_,e1), WCELL(_,e2)) => linkx(e1,e2,x,y) |
| 125 |
| (SREF(_,e1), WCELL(id,e2)) => linkWREF(e1,e2,id,x,y) |
| 126 |
| (WCELL(_,e1),WCELL(_,e2)) => linkx(e1,e2,x,y) |
| 127 |
| (SCELL(_,e1),WCELL(_,e2)) => linky(e1,e2,x,y) |
| 128 |
|
| 129 |
| (WREF(_,e1), SCELL(_,e2)) => linkx(e1,e2,x,y) |
| 130 |
| (SREF(_,e1), SCELL(_,e2)) => linkx(e1,e2,x,y) |
| 131 |
| (WCELL(_,e1),SCELL(_,e2)) => linkx(e1,e2,x,y) |
| 132 |
| (SCELL(_,e1),SCELL(_,e2)) => linkx(e1,e2,x,y) |
| 133 |
| _ => error "unify" |
| 134 |
end |
| 135 |
|
| 136 |
and collapseAll([],_) = () |
| 137 |
| collapseAll((_,_,x)::xs,y) = (unify(x,y); collapseAll(xs,y)) |
| 138 |
|
| 139 |
and unifyList(l1,l2) = |
| 140 |
let fun merge([],l) = l |
| 141 |
| merge(l,[]) = l |
| 142 |
| merge(a as (c as (k,i,x))::u,b as (d as (k',i',y))::v) = |
| 143 |
if k=k' andalso i=i' then (unify(x,y); c::merge(u,v)) |
| 144 |
else if less(k,i,k',i') then d::merge(a,v) else c::merge(u,b) |
| 145 |
in merge(sort l1,sort l2) end |
| 146 |
|
| 147 |
fun pi(x,i) = getIth(PI,i,x) |
| 148 |
fun dom(x,i) = getIth(DOM,i,x) |
| 149 |
fun ran(x,i) = getIth(RAN,i,x) |
| 150 |
fun sub(x,i) = let val m = getIth(PI,i,x) in mut m; m end |
| 151 |
|
| 152 |
fun offset(x,i) = (unify(x,newTop{mutable=false,name=""}); find x) |
| 153 |
|
| 154 |
and unifyAll(x,[]) = () |
| 155 |
| unifyAll(x,(_,_,y)::l) = (unify(x,y); unifyAll(x,l)) |
| 156 |
|
| 157 |
fun mkHeader(NONE,es) = es |
| 158 |
| mkHeader(SOME h,es) = (PI,~1,h)::es |
| 159 |
|
| 160 |
fun mkAlloc(header, xs) = |
| 161 |
let fun collect(_,[],l) = l |
| 162 |
| collect(i,x::xs,l) = collect(i+1,xs,(PI,i,x)::l) |
| 163 |
in (!newMem(), ref(mkHeader(header,collect(0,xs,[])))) end |
| 164 |
|
| 165 |
fun mkRecord(header,xs) = ref(SCELL(mkAlloc(header, xs))) |
| 166 |
fun mkRef(header,x) = ref(SREF(mkAlloc(header, [x]))) |
| 167 |
fun mkArray(header,xs) = ref(SREF(mkAlloc(header, xs))) |
| 168 |
fun mkVector(header,xs) = ref(SCELL(mkAlloc(header, xs))) |
| 169 |
fun mkLambda(xs) = |
| 170 |
let fun collect(_,[],l) = l |
| 171 |
| collect(i,x::xs,l) = collect(i+1,xs,(DOM,i,x)::l) |
| 172 |
in ref(SCELL(!newMem(), ref(collect(0,xs,[])))) end |
| 173 |
|
| 174 |
fun app(f,xs) = |
| 175 |
let fun loop(_,[]) = () |
| 176 |
| loop(i,x::xs) = (unify(dom(f,i),x); loop(i+1,xs)) |
| 177 |
in loop(0,xs) end |
| 178 |
|
| 179 |
fun ret(f,xs) = |
| 180 |
let fun loop(_,[]) = () |
| 181 |
| loop(i,x::xs) = (unify(ran(f,i),x); loop(i+1,xs)) |
| 182 |
in loop(0,xs) end |
| 183 |
|
| 184 |
fun strongUpdate(a,i,x) = unify(sub(a,i),x) |
| 185 |
fun strongSubscript(a,i) = sub(a,i) |
| 186 |
fun weakUpdate(a,x) = |
| 187 |
let val elem = sub(a, 0) |
| 188 |
in weak elem; unify(elem, x) end |
| 189 |
fun weakSubscript(a) = |
| 190 |
let val elem = sub(a, 0) |
| 191 |
in weak elem; elem end |
| 192 |
|
| 193 |
fun interfere(x,y) = find x = find y |
| 194 |
|
| 195 |
val maxLevels = MLRiscControl.mkInt ("points-to-show-max-levels", |
| 196 |
"max # of level to show in pointsTo") |
| 197 |
val _ = maxLevels := 3 |
| 198 |
|
| 199 |
fun toString r = show(!r, !maxLevels) |
| 200 |
|
| 201 |
and show(LINK x, lvl) = show(!x, lvl) |
| 202 |
| show(SREF(id,es), lvl) = "sref"^C.toString id^edges(es, lvl) |
| 203 |
| show(WREF(id,es), lvl) = "wref"^C.toString id^edges(es, lvl) |
| 204 |
| show(SCELL(id,es), lvl) = "s"^C.toString id^edges(es, lvl) |
| 205 |
| show(WCELL(id,es), lvl) = "w"^C.toString id^edges(es, lvl) |
| 206 |
| show(TOP{name="",mutable=true,id,...}, _) = "var"^C.toString id |
| 207 |
| show(TOP{name="",mutable=false,id,...}, _) = "const"^C.toString id |
| 208 |
| show(TOP{name,...}, _) = name |
| 209 |
|
| 210 |
and edges(es, ~1) = "" |
| 211 |
| edges(es, lvl) = |
| 212 |
let fun prInt i = if i < 0 then "-"^Int.toString(~i) else Int.toString i |
| 213 |
fun add(a,"") = a |
| 214 |
| add(a,b) = a^","^b |
| 215 |
fun cnv((PI,i,x),s) = add(prInt i^"->"^show(!x, lvl-1),s) |
| 216 |
| cnv(_,s) = s |
| 217 |
in case foldr cnv "" (!es) of |
| 218 |
"" => "" |
| 219 |
| t => if lvl = 0 then "..." else "["^t^"]" |
| 220 |
end |
| 221 |
|
| 222 |
end |