Skip to content

Commit

Permalink
property lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0mz committed May 3, 2024
1 parent 92e8a5d commit c314d5a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 17 deletions.
12 changes: 6 additions & 6 deletions lib/mdg/analyse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ and analyse (state : state) (statement : m Statement.t) : unit =
| _, AssignBinary {left; opLeft; opRght; id; _} ->
let _L1, _L2 = eval_expr opLeft, eval_expr opRght in
let l_i = Graph.alloc graph id in
LocationSet.iter (Graph.addDepEdge graph l_i) (LocationSet.union _L1 _L2);
LocationSet.iter (flip (Graph.addDepEdge graph) l_i) (LocationSet.union _L1 _L2);
Store.update store left (LocationSet.singleton l_i);

| _, AssignUnary {left; argument; id; _} ->
let _L1 = eval_expr argument in
let l_i = Graph.alloc graph id in
LocationSet.iter (Graph.addDepEdge graph l_i) _L1;
LocationSet.iter (flip (Graph.addDepEdge graph) l_i) _L1;
Store.update store left (LocationSet.singleton l_i)

(* -------- N E W O B J E C T -------- *)
Expand All @@ -49,21 +49,21 @@ and analyse (state : state) (statement : m Statement.t) : unit =
Store.update store left (LocationSet.singleton l_i);
Graph.addNode graph l_i;

(* -------- S T A T I C P R O P E R T Y L O O K U P -------- *)
(* -------- S T A T I C P R O P E R T Y L O O K U P -------- *)
| _, AssignStaticMember {left; _object; property=(_, {name=property; _}); id} ->
let _L = eval_expr _object in
Graph.staticAddProperty graph _L property id;
let _L' = LocationSet.map (fun loc -> Graph.lookup graph loc property) _L in
Store.update store left _L'

(* -------- D Y N A M I C P R O P E R T Y L O O K U P -------- *)
(* -------- D Y N A M I C P R O P E R T Y L O O K U P -------- *)
| _, AssignDynmicMember {left; _object; property; id} ->
let _L1, _L2 = eval_expr _object, eval_expr property in
Graph.dynamicAddProperty graph _L1 _L2 id;
let _L' = LocationSet.map (fun loc -> Graph.lookup graph loc "*") _L1 in
Store.update store left _L'

(* -------- S T A T I C P R O P E R T Y U P D A T E -------- *)
(* -------- S T A T I C P R O P E R T Y U P D A T E -------- *)
| _, StaticMemberAssign {_object; property=(_, {name=property; _}); right; id} ->
let _L1, _L2 = eval_expr _object, eval_expr right in
let _L1' = Graph.staticNewVersion graph store _L1 property id in
Expand All @@ -73,7 +73,7 @@ and analyse (state : state) (statement : m Statement.t) : unit =
) _L2
) _L1'

(* -------- D Y N A M I C P R O P E R T Y U P D A T E -------- *)
(* -------- D Y N A M I C P R O P E R T Y U P D A T E -------- *)
| _, DynmicMemberAssign {_object; property; right; id} ->
let _L1, _L2, _L3 = eval_expr _object,
eval_expr property,
Expand Down
61 changes: 51 additions & 10 deletions lib/mdg/graph.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ let lookup (graph : t) (l : location) (property : property) : location =
(* TODO : Indirect Lookup - Known Version and Indirect Lookup - Unknown Version *)
else
"!TODO!"



(* ------- G R A P H M A N I P U L A T I O N ------- *)
let addNode (graph : t) (loc : location) : unit =
Expand All @@ -78,14 +76,18 @@ let addEdge (graph : t) (edge : Edge.t) (_to : location) (from : location) : uni
let edges = get_edges graph from in
HashTable.replace graph from (EdgeSet.add edge edges)

let addDepEdge (graph : t) (_to : location) (from : location) : unit =
let addDepEdge (graph : t) (from : location) (_to : location) : unit =
let edge = {Edge._to = _to; info = Dependency} in
addEdge graph edge _to from

let addPropEdge (graph : t) (from : location) (_to : location) (property : property option) : unit =
let edge = {Edge._to = _to; info = Property property} in
addEdge graph edge _to from

let addVersionEdge (graph : t) (from : location) (_to : location) (property : property option) : unit =
let edge = {Edge._to = _to; info = Version property} in
addEdge graph edge _to from

let staticAddProperty (graph : t) (_L : LocationSet.t) (property : property) (id : int) : unit =
LocationSet.iter (fun l ->
let l_o = orig graph l in
Expand All @@ -103,17 +105,56 @@ let dynamicAddProperty (graph : t) (_L_obj : LocationSet.t) (_L_prop : LocationS
let edges = get_edges graph l_o in
if (EdgeSet.exists (has_property_edge None) edges) then
let {Edge._to; _} = EdgeSet.find_last (has_property_edge None) edges in
LocationSet.iter (addDepEdge graph _to) _L_prop
LocationSet.iter (flip (addDepEdge graph) _to) _L_prop
else
( let l_i = alloc graph id in
addPropEdge graph l_o l_i None;
LocationSet.iter (addDepEdge graph l_i) _L_prop )
LocationSet.iter (flip (addDepEdge graph) l_i) _L_prop )

) _L_obj

let staticNewVersion (_ : t) (_ : Store.t) (_ : LocationSet.t) (_ : property) (_ : int) : LocationSet.t =
LocationSet.empty

let dynamicNewVersion (_ : t) (_ : Store.t) (_ : LocationSet.t) (_ : LocationSet.t) (_ : int) : LocationSet.t =
LocationSet.empty
let staticNewVersion (graph : t) (store : Store.t) (_L : LocationSet.t) (property : property) (id : int) : LocationSet.t =
let l_i = alloc graph id in
(* add version edges *)
LocationSet.iter ( fun l ->
addVersionEdge graph l l_i (Some property)
) _L;

if (LocationSet.cardinal _L) = 1
(* strong udpate *)
then let l = LocationSet.min_elt _L in
Store.strong_update store l l_i
(* TODO : weak update*)
else LocationSet.iter (fun l ->
let _new = LocationSet.of_list [l; l_i] in
Store.weak_update store l _new
) _L;

(* return *)
LocationSet.singleton l_i

let dynamicNewVersion (graph : t) (store : Store.t) (_L_obj : LocationSet.t) (_L_prop : LocationSet.t) (id : int) : LocationSet.t =
let l_i = alloc graph id in
(* add version edges *)
LocationSet.iter ( fun l ->
addVersionEdge graph l l_i None
) _L_obj;

(* add dependency edges *)
LocationSet.iter (fun l_prop ->
addDepEdge graph l_prop l_i
) _L_prop;

if (LocationSet.cardinal _L_obj) = 1
(* strong udpate *)
then let l = LocationSet.min_elt _L_obj in
Store.strong_update store l l_i
(* TODO : weak update*)
else LocationSet.iter (fun l ->
let _new = LocationSet.of_list [l; l_i] in
Store.weak_update store l _new
) _L_obj;

(* return *)
LocationSet.singleton l_i

15 changes: 15 additions & 0 deletions lib/mdg/store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ let get (store : t) ((_, {name; _}) : m Identifier.t) : LocationSet.t =
let update (store : t) ((_, {name; _}) : m Identifier.t) (locs : LocationSet.t) : unit =
HashTable.replace store name locs

let strong_update (store : t) (old : location) (_new : location) : unit =
HashTable.iter (fun id locations ->
let new_locations = LocationSet.map (fun loc -> if loc = old then _new else loc) locations in
HashTable.replace store id new_locations
) store

let weak_update (store : t) (old : location) (_new : LocationSet.t) : unit =
HashTable.iter (fun id locations ->
let new_locations = LocationSet.fold (fun loc acc -> if loc = old
then LocationSet.union acc _new
else LocationSet.add loc acc
) locations LocationSet.empty in
HashTable.replace store id new_locations
) store

2 changes: 1 addition & 1 deletion lib/mdg/structures.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Edge = struct
let to_string ({_to; info} : t) : string =
let edge_info = match info with
| Property prop -> map_default (fun prop -> "P(" ^ prop ^ ")") "P(*)" prop
| Version prop -> map_default (fun prop -> "P(" ^ prop ^ ")") "P(*)" prop
| Version prop -> map_default (fun prop -> "V(" ^ prop ^ ")") "V(*)" prop
| Dependency -> "D"
in
" --" ^ edge_info ^ "-> " ^ _to
Expand Down

0 comments on commit c314d5a

Please sign in to comment.