You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
604 lines
19 KiB
604 lines
19 KiB
7 years ago
|
(*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
7 years ago
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*)
|
||
7 years ago
|
|
||
5 years ago
|
(** LLAIR (Low-Level Analysis Internal Representation) *)
|
||
|
|
||
|
[@@@warning "+9"]
|
||
7 years ago
|
|
||
|
type inst =
|
||
5 years ago
|
| Move of {reg_exps: (Reg.t * Exp.t) vector; loc: Loc.t}
|
||
|
| Load of {reg: Reg.t; ptr: Exp.t; len: Exp.t; loc: Loc.t}
|
||
6 years ago
|
| Store of {ptr: Exp.t; exp: Exp.t; len: Exp.t; loc: Loc.t}
|
||
6 years ago
|
| Memset of {dst: Exp.t; byt: Exp.t; len: Exp.t; loc: Loc.t}
|
||
7 years ago
|
| Memcpy of {dst: Exp.t; src: Exp.t; len: Exp.t; loc: Loc.t}
|
||
|
| Memmov of {dst: Exp.t; src: Exp.t; len: Exp.t; loc: Loc.t}
|
||
5 years ago
|
| Alloc of {reg: Reg.t; num: Exp.t; len: Exp.t; loc: Loc.t}
|
||
7 years ago
|
| Free of {ptr: Exp.t; loc: Loc.t}
|
||
5 years ago
|
| Nondet of {reg: Reg.t option; msg: string; loc: Loc.t}
|
||
6 years ago
|
| Abort of {loc: Loc.t}
|
||
7 years ago
|
[@@deriving sexp]
|
||
|
|
||
|
type cmnd = inst vector [@@deriving sexp]
|
||
|
type label = string [@@deriving sexp]
|
||
|
|
||
5 years ago
|
type jump = {mutable dst: block; mutable retreating: bool}
|
||
7 years ago
|
|
||
5 years ago
|
and 'a call =
|
||
|
{ callee: 'a
|
||
|
; typ: Typ.t
|
||
5 years ago
|
; actuals: Exp.t list
|
||
5 years ago
|
; areturn: Reg.t option
|
||
5 years ago
|
; return: jump
|
||
|
; throw: jump option
|
||
|
; mutable recursive: bool
|
||
|
; loc: Loc.t }
|
||
7 years ago
|
|
||
|
and term =
|
||
6 years ago
|
| Switch of {key: Exp.t; tbl: (Exp.t * jump) vector; els: jump; loc: Loc.t}
|
||
6 years ago
|
| Iswitch of {ptr: Exp.t; tbl: jump vector; loc: Loc.t}
|
||
5 years ago
|
| Call of Exp.t call
|
||
7 years ago
|
| Return of {exp: Exp.t option; loc: Loc.t}
|
||
|
| Throw of {exc: Exp.t; loc: Loc.t}
|
||
|
| Unreachable
|
||
|
|
||
|
and block =
|
||
|
{ lbl: label
|
||
|
; cmnd: cmnd
|
||
|
; term: term
|
||
|
; mutable parent: func
|
||
|
; mutable sort_index: int }
|
||
|
|
||
6 years ago
|
and func =
|
||
|
{ name: Global.t
|
||
5 years ago
|
; formals: Reg.t list
|
||
5 years ago
|
; freturn: Reg.t option
|
||
|
; fthrow: Reg.t
|
||
|
; locals: Reg.Set.t
|
||
5 years ago
|
; entry: block }
|
||
6 years ago
|
|
||
6 years ago
|
let sexp_cons (hd : Sexp.t) (tl : Sexp.t) =
|
||
|
match tl with
|
||
|
| List xs -> Sexp.List (hd :: xs)
|
||
|
| Atom _ -> Sexp.List [hd; tl]
|
||
|
|
||
|
let sexp_ctor label args = sexp_cons (Sexp.Atom label) args
|
||
|
|
||
5 years ago
|
let sexp_of_jump {dst; retreating} =
|
||
|
[%sexp {dst: label = dst.lbl; retreating: bool}]
|
||
6 years ago
|
|
||
|
let sexp_of_term = function
|
||
|
| Switch {key; tbl; els; loc} ->
|
||
|
sexp_ctor "Switch"
|
||
|
[%sexp
|
||
|
{key: Exp.t; tbl: (Exp.t * jump) vector; els: jump; loc: Loc.t}]
|
||
|
| Iswitch {ptr; tbl; loc} ->
|
||
|
sexp_ctor "Iswitch" [%sexp {ptr: Exp.t; tbl: jump vector; loc: Loc.t}]
|
||
5 years ago
|
| Call {callee; typ; actuals; areturn; return; throw; recursive; loc} ->
|
||
6 years ago
|
sexp_ctor "Call"
|
||
|
[%sexp
|
||
5 years ago
|
{ callee: Exp.t
|
||
6 years ago
|
; typ: Typ.t
|
||
5 years ago
|
; actuals: Exp.t list
|
||
5 years ago
|
; areturn: Reg.t option
|
||
6 years ago
|
; return: jump
|
||
|
; throw: jump option
|
||
5 years ago
|
; recursive: bool
|
||
6 years ago
|
; loc: Loc.t }]
|
||
|
| Return {exp; loc} ->
|
||
|
sexp_ctor "Return" [%sexp {exp: Exp.t option; loc: Loc.t}]
|
||
|
| Throw {exc; loc} -> sexp_ctor "Throw" [%sexp {exc: Exp.t; loc: Loc.t}]
|
||
|
| Unreachable -> Sexp.Atom "Unreachable"
|
||
|
|
||
5 years ago
|
let sexp_of_block {lbl; cmnd; term; parent; sort_index} =
|
||
6 years ago
|
[%sexp
|
||
6 years ago
|
{ lbl: label
|
||
|
; cmnd: cmnd
|
||
|
; term: term
|
||
5 years ago
|
; parent: Reg.t = parent.name.reg
|
||
6 years ago
|
; sort_index: int }]
|
||
6 years ago
|
|
||
5 years ago
|
let sexp_of_func {name; formals; freturn; fthrow; locals; entry} =
|
||
5 years ago
|
[%sexp
|
||
|
{ name: Global.t
|
||
5 years ago
|
; formals: Reg.t list
|
||
5 years ago
|
; freturn: Reg.t option
|
||
|
; fthrow: Reg.t
|
||
|
; locals: Reg.Set.t
|
||
5 years ago
|
; entry: block }]
|
||
6 years ago
|
|
||
|
(* blocks in a [t] are uniquely identified by [sort_index] *)
|
||
|
let compare_block x y = Int.compare x.sort_index y.sort_index
|
||
6 years ago
|
let equal_block x y = Int.equal x.sort_index y.sort_index
|
||
6 years ago
|
|
||
5 years ago
|
type functions = func Map.M(String).t [@@deriving sexp_of]
|
||
6 years ago
|
|
||
|
type t = {globals: Global.t vector; functions: functions}
|
||
6 years ago
|
[@@deriving sexp_of]
|
||
|
|
||
|
let pp_inst fs inst =
|
||
|
let pf pp = Format.fprintf fs pp in
|
||
|
match inst with
|
||
5 years ago
|
| Move {reg_exps; loc} ->
|
||
|
let regs, exps = Vector.unzip reg_exps in
|
||
5 years ago
|
pf "@[<2>@[%a@]@ := @[%a@];@]\t%a" (Vector.pp ",@ " Reg.pp) regs
|
||
5 years ago
|
(Vector.pp ",@ " Exp.pp) exps Loc.pp loc
|
||
6 years ago
|
| Load {reg; ptr; len; loc} ->
|
||
5 years ago
|
pf "@[<2>%a@ := load %a@ %a;@]\t%a" Reg.pp reg Exp.pp len Exp.pp ptr
|
||
6 years ago
|
Loc.pp loc
|
||
|
| Store {ptr; exp; len; loc} ->
|
||
|
pf "@[<2>store %a@ %a@ %a;@]\t%a" Exp.pp len Exp.pp ptr Exp.pp exp
|
||
|
Loc.pp loc
|
||
6 years ago
|
| Memset {dst; byt; len; loc} ->
|
||
|
pf "@[<2>memset %a %a %a;@]\t%a" Exp.pp len Exp.pp dst Exp.pp byt
|
||
|
Loc.pp loc
|
||
6 years ago
|
| Memcpy {dst; src; len; loc} ->
|
||
|
pf "@[<2>memcpy %a %a %a;@]\t%a" Exp.pp len Exp.pp dst Exp.pp src
|
||
|
Loc.pp loc
|
||
|
| Memmov {dst; src; len; loc} ->
|
||
|
pf "@[<2>memmov %a %a %a;@]\t%a" Exp.pp len Exp.pp dst Exp.pp src
|
||
|
Loc.pp loc
|
||
|
| Alloc {reg; num; len; loc} ->
|
||
5 years ago
|
pf "@[<2>%a@ := alloc [%a x %a];@]\t%a" Reg.pp reg Exp.pp num Exp.pp
|
||
5 years ago
|
len Loc.pp loc
|
||
6 years ago
|
| Free {ptr; loc} -> pf "@[<2>free %a;@]\t%a" Exp.pp ptr Loc.pp loc
|
||
6 years ago
|
| Nondet {reg; msg; loc} ->
|
||
5 years ago
|
pf "@[<2>%anondet \"%s\";@]\t%a"
|
||
5 years ago
|
(Option.pp "%a := " Reg.pp)
|
||
5 years ago
|
reg msg Loc.pp loc
|
||
6 years ago
|
| Abort {loc} -> pf "@[<2>abort;@]\t%a" Loc.pp loc
|
||
6 years ago
|
|
||
5 years ago
|
let pp_actuals pp_actual fs actuals =
|
||
|
Format.fprintf fs "@ (@[%a@])" (List.pp ",@ " pp_actual)
|
||
|
(List.rev actuals)
|
||
6 years ago
|
|
||
5 years ago
|
let pp_formal fs reg = Reg.pp fs reg
|
||
6 years ago
|
|
||
5 years ago
|
let pp_jump fs {dst; retreating} =
|
||
|
Format.fprintf fs "@[<2>%s%%%s@]"
|
||
6 years ago
|
(if retreating then "↑" else "")
|
||
5 years ago
|
dst.lbl
|
||
6 years ago
|
|
||
|
let pp_term fs term =
|
||
|
let pf pp = Format.fprintf fs pp in
|
||
|
let pp_goto fs jmp = Format.fprintf fs "goto %a;" pp_jump jmp in
|
||
|
match term with
|
||
|
| Switch {key; tbl; els; loc} -> (
|
||
|
match Vector.to_array tbl with
|
||
|
| [||] -> pf "@[%a@]\t%a" pp_goto els Loc.pp loc
|
||
6 years ago
|
| [|(z, jmp)|] when Exp.is_false z ->
|
||
6 years ago
|
pf "@[if %a@;<1 2>%a@ @[else@;<1 2>%a@]@]\t%a" Exp.pp key pp_goto
|
||
6 years ago
|
els pp_goto jmp Loc.pp loc
|
||
6 years ago
|
| _ ->
|
||
|
pf "@[<2>switch %a@ @[%a@ else: %a@]@]\t%a" Exp.pp key
|
||
6 years ago
|
(Vector.pp "@ " (fun fs (case, jmp) ->
|
||
|
Format.fprintf fs "%a: %a" Exp.pp case pp_goto jmp ))
|
||
6 years ago
|
tbl pp_goto els Loc.pp loc )
|
||
|
| Iswitch {ptr; tbl; loc} ->
|
||
|
pf "@[<2>iswitch %a@ @[<hv>%a@]@]\t%a" Exp.pp ptr
|
||
5 years ago
|
(Vector.pp "@ " (fun fs jmp ->
|
||
|
Format.fprintf fs "%s: %a" jmp.dst.lbl pp_goto jmp ))
|
||
6 years ago
|
tbl Loc.pp loc
|
||
5 years ago
|
| Call {callee; actuals; areturn; return; throw; recursive; loc; _} ->
|
||
5 years ago
|
pf "@[<2>@[<7>%acall @[<2>%s%a%a@]@]@ @[returnto %a%a;@]@]\t%a"
|
||
5 years ago
|
(Option.pp "%a := " Reg.pp)
|
||
5 years ago
|
areturn
|
||
|
(if recursive then "↑" else "")
|
||
5 years ago
|
Exp.pp callee (pp_actuals Exp.pp) actuals pp_jump return
|
||
6 years ago
|
(Option.pp "@ throwto %a" pp_jump)
|
||
|
throw Loc.pp loc
|
||
|
| Return {exp; loc} ->
|
||
|
pf "@[<2>return%a@]\t%a" (Option.pp " %a" Exp.pp) exp Loc.pp loc
|
||
|
| Throw {exc; loc} -> pf "@[<2>throw %a@]\t%a" Exp.pp exc Loc.pp loc
|
||
|
| Unreachable -> pf "unreachable"
|
||
|
|
||
|
let pp_cmnd = Vector.pp "@ " pp_inst
|
||
|
|
||
5 years ago
|
let pp_block fs {lbl; cmnd; term; parent= _; sort_index} =
|
||
5 years ago
|
Format.fprintf fs "@[<v 2>%%%s: #%i@ @[<v>%a%t%a@]@]" lbl sort_index
|
||
|
pp_cmnd cmnd
|
||
6 years ago
|
(fun fs -> if Vector.is_empty cmnd then () else Format.fprintf fs "@ ")
|
||
|
pp_term term
|
||
7 years ago
|
|
||
|
(** Initial cyclic values *)
|
||
|
|
||
|
let rec dummy_block =
|
||
|
{ lbl= "dummy"
|
||
|
; cmnd= Vector.empty
|
||
|
; term= Unreachable
|
||
|
; parent= dummy_func
|
||
|
; sort_index= 0 }
|
||
|
|
||
|
and dummy_func =
|
||
5 years ago
|
let dummy_reg = Reg.program ~global:() Typ.ptr "dummy" in
|
||
|
{ name= Global.mk dummy_reg Typ.ptr Loc.none
|
||
5 years ago
|
; formals= []
|
||
5 years ago
|
; freturn= None
|
||
5 years ago
|
; fthrow= dummy_reg
|
||
|
; locals= Reg.Set.empty
|
||
5 years ago
|
; entry= dummy_block }
|
||
7 years ago
|
|
||
6 years ago
|
(** Instructions *)
|
||
6 years ago
|
|
||
7 years ago
|
module Inst = struct
|
||
6 years ago
|
type t = inst [@@deriving sexp]
|
||
|
|
||
|
let pp = pp_inst
|
||
5 years ago
|
let move ~reg_exps ~loc = Move {reg_exps; loc}
|
||
6 years ago
|
let load ~reg ~ptr ~len ~loc = Load {reg; ptr; len; loc}
|
||
|
let store ~ptr ~exp ~len ~loc = Store {ptr; exp; len; loc}
|
||
6 years ago
|
let memset ~dst ~byt ~len ~loc = Memset {dst; byt; len; loc}
|
||
6 years ago
|
let memcpy ~dst ~src ~len ~loc = Memcpy {dst; src; len; loc}
|
||
|
let memmov ~dst ~src ~len ~loc = Memmov {dst; src; len; loc}
|
||
|
let alloc ~reg ~num ~len ~loc = Alloc {reg; num; len; loc}
|
||
|
let free ~ptr ~loc = Free {ptr; loc}
|
||
6 years ago
|
let nondet ~reg ~msg ~loc = Nondet {reg; msg; loc}
|
||
6 years ago
|
let abort ~loc = Abort {loc}
|
||
6 years ago
|
|
||
|
let loc = function
|
||
5 years ago
|
| Move {loc; _}
|
||
|
|Load {loc; _}
|
||
|
|Store {loc; _}
|
||
|
|Memset {loc; _}
|
||
|
|Memcpy {loc; _}
|
||
|
|Memmov {loc; _}
|
||
|
|Alloc {loc; _}
|
||
|
|Free {loc; _}
|
||
|
|Nondet {loc; _}
|
||
|
|Abort {loc; _} ->
|
||
6 years ago
|
loc
|
||
|
|
||
|
let union_locals inst vs =
|
||
7 years ago
|
match inst with
|
||
5 years ago
|
| Move {reg_exps; _} ->
|
||
5 years ago
|
Vector.fold ~f:(fun vs (reg, _) -> Set.add vs reg) ~init:vs reg_exps
|
||
5 years ago
|
| Load {reg; _} | Alloc {reg; _} | Nondet {reg= Some reg; _} ->
|
||
|
Set.add vs reg
|
||
6 years ago
|
| Store _ | Memcpy _ | Memmov _ | Memset _ | Free _
|
||
5 years ago
|
|Nondet {reg= None; _}
|
||
6 years ago
|
|Abort _ ->
|
||
6 years ago
|
vs
|
||
7 years ago
|
|
||
5 years ago
|
let locals inst = union_locals inst Reg.Set.empty
|
||
5 years ago
|
|
||
|
let fold_exps inst ~init ~f =
|
||
|
match inst with
|
||
|
| Move {reg_exps; loc= _} ->
|
||
|
Vector.fold reg_exps ~init ~f:(fun acc (_reg, exp) -> f acc exp)
|
||
|
| Load {reg= _; ptr; len; loc= _} -> f (f init ptr) len
|
||
|
| Store {ptr; exp; len; loc= _} -> f (f (f init ptr) exp) len
|
||
|
| Memset {dst; byt; len; loc= _} -> f (f (f init dst) byt) len
|
||
|
| Memcpy {dst; src; len; loc= _} | Memmov {dst; src; len; loc= _} ->
|
||
|
f (f (f init dst) src) len
|
||
|
| Alloc {reg= _; num; len; loc= _} -> f (f init num) len
|
||
|
| Free {ptr; loc= _} -> f init ptr
|
||
|
| Nondet {reg= _; msg= _; loc= _} -> init
|
||
|
| Abort {loc= _} -> init
|
||
6 years ago
|
end
|
||
7 years ago
|
|
||
6 years ago
|
(** Jumps *)
|
||
6 years ago
|
|
||
7 years ago
|
module Jump = struct
|
||
6 years ago
|
type t = jump [@@deriving sexp_of]
|
||
|
|
||
5 years ago
|
let compare x y = compare_block x.dst y.dst
|
||
|
let equal x y = equal_block x.dst y.dst
|
||
6 years ago
|
let pp = pp_jump
|
||
5 years ago
|
let mk lbl = {dst= {dummy_block with lbl}; retreating= false}
|
||
7 years ago
|
end
|
||
|
|
||
6 years ago
|
(** Basic-Block Terminators *)
|
||
7 years ago
|
|
||
6 years ago
|
module Term = struct
|
||
|
type t = term [@@deriving sexp_of]
|
||
6 years ago
|
|
||
6 years ago
|
let pp = pp_term
|
||
6 years ago
|
|
||
6 years ago
|
let invariant ?parent term =
|
||
6 years ago
|
Invariant.invariant [%here] term [%sexp_of: t]
|
||
|
@@ fun () ->
|
||
|
match term with
|
||
5 years ago
|
| Switch _ | Iswitch _ -> assert true
|
||
5 years ago
|
| Call {typ; actuals; areturn; _} -> (
|
||
6 years ago
|
match typ with
|
||
5 years ago
|
| Pointer {elt= Function {args; return= retn_typ; _}} ->
|
||
|
assert (Vector.length args = List.length actuals) ;
|
||
5 years ago
|
assert (Option.is_some retn_typ || Option.is_none areturn)
|
||
6 years ago
|
| _ -> assert false )
|
||
5 years ago
|
| Return {exp; _} -> (
|
||
6 years ago
|
match parent with
|
||
|
| Some parent ->
|
||
|
assert (Bool.(Option.is_some exp = Option.is_some parent.freturn))
|
||
|
| None -> assert true )
|
||
|
| Throw _ | Unreachable -> assert true
|
||
6 years ago
|
|
||
6 years ago
|
let goto ~dst ~loc =
|
||
5 years ago
|
Switch {key= Exp.false_; tbl= Vector.empty; els= dst; loc}
|
||
6 years ago
|
|> check invariant
|
||
|
|
||
6 years ago
|
let branch ~key ~nzero ~zero ~loc =
|
||
5 years ago
|
let tbl = Vector.of_array [|(Exp.false_, zero)|] in
|
||
6 years ago
|
let els = nzero in
|
||
|
Switch {key; tbl; els; loc} |> check invariant
|
||
|
|
||
6 years ago
|
let switch ~key ~tbl ~els ~loc =
|
||
|
Switch {key; tbl; els; loc} |> check invariant
|
||
|
|
||
|
let iswitch ~ptr ~tbl ~loc = Iswitch {ptr; tbl; loc} |> check invariant
|
||
|
|
||
5 years ago
|
let call ~callee ~typ ~actuals ~areturn ~return ~throw ~loc =
|
||
5 years ago
|
Call
|
||
5 years ago
|
{callee; typ; actuals; areturn; return; throw; recursive= false; loc}
|
||
5 years ago
|
|> check invariant
|
||
7 years ago
|
|
||
6 years ago
|
let return ~exp ~loc = Return {exp; loc} |> check invariant
|
||
|
let throw ~exc ~loc = Throw {exc; loc} |> check invariant
|
||
|
let unreachable = Unreachable |> check invariant
|
||
6 years ago
|
|
||
|
let loc = function
|
||
5 years ago
|
| Switch {loc; _}
|
||
|
|Iswitch {loc; _}
|
||
|
|Call {loc; _}
|
||
|
|Return {loc; _}
|
||
|
|Throw {loc; _} ->
|
||
6 years ago
|
loc
|
||
|
| Unreachable -> Loc.none
|
||
5 years ago
|
|
||
|
let union_locals term vs =
|
||
|
match term with
|
||
|
| Call {areturn; _} -> Set.add_option areturn vs
|
||
|
| _ -> vs
|
||
7 years ago
|
end
|
||
|
|
||
6 years ago
|
(** Basic-Blocks *)
|
||
7 years ago
|
|
||
6 years ago
|
module Block = struct
|
||
6 years ago
|
module T = struct type t = block [@@deriving compare, equal, sexp_of] end
|
||
6 years ago
|
include T
|
||
|
include Comparator.Make (T)
|
||
6 years ago
|
|
||
6 years ago
|
let pp = pp_block
|
||
7 years ago
|
|
||
5 years ago
|
let mk ~lbl ~cmnd ~term =
|
||
6 years ago
|
{ lbl
|
||
|
; cmnd
|
||
|
; term
|
||
|
; parent= dummy_block.parent
|
||
|
; sort_index= dummy_block.sort_index }
|
||
7 years ago
|
end
|
||
|
|
||
5 years ago
|
(* Blocks compared by label, which are unique within a function, used to
|
||
|
compute unique sort_index ids *)
|
||
|
module Block_label = struct
|
||
|
module T = struct
|
||
|
module T0 = struct
|
||
|
type t = block [@@deriving sexp_of]
|
||
|
|
||
|
let compare x y =
|
||
|
[%compare: string * Global.t] (x.lbl, x.parent.name)
|
||
|
(y.lbl, y.parent.name)
|
||
|
|
||
|
let hash b = [%hash: string * Global.t] (b.lbl, b.parent.name)
|
||
|
end
|
||
|
|
||
|
include T0
|
||
|
include Comparator.Make (T0)
|
||
|
end
|
||
|
|
||
|
include T
|
||
|
|
||
|
let empty_set = Set.empty (module T)
|
||
|
end
|
||
|
|
||
|
module BlockQ = Hash_queue.Make (Block_label)
|
||
|
module FuncQ = Hash_queue.Make (Reg)
|
||
|
|
||
6 years ago
|
(** Functions *)
|
||
7 years ago
|
|
||
6 years ago
|
module Func = struct
|
||
|
type t = func [@@deriving sexp_of]
|
||
6 years ago
|
|
||
7 years ago
|
let is_undefined = function
|
||
5 years ago
|
| {entry= {cmnd; term= Unreachable; _}; _} -> Vector.is_empty cmnd
|
||
7 years ago
|
| _ -> false
|
||
|
|
||
5 years ago
|
let fold_cfg ~init ~f func =
|
||
|
let seen = Hash_set.create (module Block_label) in
|
||
|
let rec fold_cfg_ s blk =
|
||
|
if Result.is_error (Hash_set.strict_add seen blk) then s
|
||
|
else
|
||
|
let s =
|
||
|
let f s j = fold_cfg_ s j.dst in
|
||
|
match blk.term with
|
||
|
| Switch {tbl; els; _} ->
|
||
|
let s = Vector.fold ~f:(fun s (_, j) -> f s j) ~init:s tbl in
|
||
|
f s els
|
||
|
| Iswitch {tbl; _} -> Vector.fold ~f ~init:s tbl
|
||
|
| Call {return; throw; _} ->
|
||
|
let s = f s return in
|
||
|
Option.fold ~f ~init:s throw
|
||
|
| Return _ | Throw _ | Unreachable -> s
|
||
|
in
|
||
|
f s blk
|
||
|
in
|
||
|
fold_cfg_ init func.entry
|
||
|
|
||
|
let fold_term func ~init ~f =
|
||
|
fold_cfg func ~init ~f:(fun s blk -> f s blk.term)
|
||
|
|
||
|
let iter_term func ~f =
|
||
|
fold_cfg func ~init:() ~f:(fun () blk -> f blk.term)
|
||
|
|
||
|
let entry_cfg func = fold_cfg ~init:[] ~f:(fun cfg blk -> blk :: cfg) func
|
||
|
|
||
5 years ago
|
let pp fs func =
|
||
|
let {name; formals; freturn; entry; _} = func in
|
||
|
let {cmnd; term; sort_index; _} = entry in
|
||
6 years ago
|
let pp_if cnd str fs = if cnd then Format.fprintf fs str in
|
||
5 years ago
|
Format.fprintf fs "@[<v>@[<v>%a%a@[<2>%a%a@]%t@]"
|
||
|
(Option.pp "%a " Typ.pp)
|
||
6 years ago
|
( match name.typ with
|
||
5 years ago
|
| Pointer {elt= Function {return; _}} -> return
|
||
6 years ago
|
| _ -> None )
|
||
5 years ago
|
(Option.pp " %a := " Reg.pp)
|
||
5 years ago
|
freturn Global.pp name (pp_actuals pp_formal) formals
|
||
6 years ago
|
(fun fs ->
|
||
|
if is_undefined func then Format.fprintf fs " #%i@]" sort_index
|
||
|
else
|
||
5 years ago
|
let cfg =
|
||
|
List.sort ~compare:Block.compare (List.tl_exn (entry_cfg func))
|
||
|
in
|
||
6 years ago
|
Format.fprintf fs " { #%i %a@;<1 4>@[<v>%a@ %a@]%t%a@]@ }"
|
||
|
sort_index Loc.pp name.loc pp_cmnd cmnd Term.pp term
|
||
5 years ago
|
(pp_if (not (List.is_empty cfg)) "@ @ ")
|
||
|
(List.pp "@\n@\n " Block.pp)
|
||
6 years ago
|
cfg )
|
||
6 years ago
|
|
||
6 years ago
|
let invariant func =
|
||
|
Invariant.invariant [%here] func [%sexp_of: t]
|
||
|
@@ fun () ->
|
||
6 years ago
|
assert (func == func.entry.parent) ;
|
||
5 years ago
|
match func.name.typ with
|
||
5 years ago
|
| Pointer {elt= Function {return; _}; _} ->
|
||
6 years ago
|
assert (
|
||
|
not
|
||
5 years ago
|
(List.contains_dup (entry_cfg func) ~compare:(fun b1 b2 ->
|
||
6 years ago
|
String.compare b1.lbl b2.lbl )) ) ;
|
||
6 years ago
|
assert (Bool.(Option.is_some return = Option.is_some func.freturn)) ;
|
||
|
iter_term func ~f:(fun term -> Term.invariant ~parent:func term)
|
||
6 years ago
|
| _ -> assert false
|
||
|
|
||
6 years ago
|
let find functions name = Map.find functions name
|
||
6 years ago
|
|
||
5 years ago
|
let mk ~(name : Global.t) ~formals ~freturn ~fthrow ~entry ~cfg =
|
||
6 years ago
|
let locals =
|
||
6 years ago
|
let locals_cmnd locals cmnd =
|
||
|
Vector.fold_right ~f:Inst.union_locals cmnd ~init:locals
|
||
|
in
|
||
|
let locals_block locals block =
|
||
5 years ago
|
locals_cmnd (Term.union_locals block.term locals) block.cmnd
|
||
6 years ago
|
in
|
||
5 years ago
|
let init = locals_block Reg.Set.empty entry in
|
||
6 years ago
|
Vector.fold ~f:locals_block cfg ~init
|
||
6 years ago
|
in
|
||
5 years ago
|
let func = {name; formals; freturn; fthrow; locals; entry} in
|
||
7 years ago
|
let resolve_parent_and_jumps block =
|
||
|
block.parent <- func ;
|
||
|
let lookup cfg lbl : block =
|
||
|
Vector.find_exn cfg ~f:(fun k -> String.equal lbl k.lbl)
|
||
|
in
|
||
|
let set_dst jmp = jmp.dst <- lookup cfg jmp.dst.lbl in
|
||
|
match block.term with
|
||
5 years ago
|
| Switch {tbl; els; _} ->
|
||
7 years ago
|
Vector.iter tbl ~f:(fun (_, jmp) -> set_dst jmp) ;
|
||
|
set_dst els
|
||
5 years ago
|
| Iswitch {tbl; _} -> Vector.iter tbl ~f:set_dst
|
||
|
| Call {return; throw; _} ->
|
||
7 years ago
|
set_dst return ;
|
||
|
Option.iter throw ~f:set_dst
|
||
|
| Return _ | Throw _ | Unreachable -> ()
|
||
|
in
|
||
|
resolve_parent_and_jumps entry ;
|
||
6 years ago
|
Vector.iter cfg ~f:resolve_parent_and_jumps ;
|
||
|
func |> check invariant
|
||
6 years ago
|
|
||
5 years ago
|
let mk_undefined ~name ~formals ~freturn ~fthrow =
|
||
7 years ago
|
let entry =
|
||
5 years ago
|
Block.mk ~lbl:"" ~cmnd:Vector.empty ~term:Term.unreachable
|
||
7 years ago
|
in
|
||
|
let cfg = Vector.empty in
|
||
5 years ago
|
mk ~name ~entry ~formals ~freturn ~fthrow ~cfg
|
||
6 years ago
|
end
|
||
7 years ago
|
|
||
6 years ago
|
(** Derived meta-data *)
|
||
6 years ago
|
|
||
7 years ago
|
let set_derived_metadata functions =
|
||
|
let compute_roots functions =
|
||
6 years ago
|
let roots = FuncQ.create () in
|
||
6 years ago
|
Map.iter functions ~f:(fun func ->
|
||
5 years ago
|
FuncQ.enqueue_back_exn roots func.name.reg func ) ;
|
||
6 years ago
|
Map.iter functions ~f:(fun func ->
|
||
7 years ago
|
Func.fold_term func ~init:() ~f:(fun () -> function
|
||
5 years ago
|
| Call {callee; _} -> (
|
||
5 years ago
|
match Reg.of_exp callee with
|
||
6 years ago
|
| Some callee ->
|
||
|
FuncQ.remove roots callee |> (ignore : [> ] -> unit)
|
||
7 years ago
|
| None -> () )
|
||
|
| _ -> () ) ) ;
|
||
|
roots
|
||
|
in
|
||
|
let topsort functions roots =
|
||
6 years ago
|
let tips_to_roots = BlockQ.create () in
|
||
7 years ago
|
let rec visit ancestors func src =
|
||
6 years ago
|
if BlockQ.mem tips_to_roots src then ()
|
||
|
else
|
||
|
let ancestors = Set.add ancestors src in
|
||
7 years ago
|
let jump jmp =
|
||
6 years ago
|
if Set.mem ancestors jmp.dst then jmp.retreating <- true
|
||
7 years ago
|
else visit ancestors func jmp.dst
|
||
|
in
|
||
|
( match src.term with
|
||
5 years ago
|
| Switch {tbl; els; _} ->
|
||
7 years ago
|
Vector.iter tbl ~f:(fun (_, jmp) -> jump jmp) ;
|
||
|
jump els
|
||
5 years ago
|
| Iswitch {tbl; _} -> Vector.iter tbl ~f:jump
|
||
|
| Call ({callee; return; throw; _} as call) ->
|
||
5 years ago
|
( match
|
||
5 years ago
|
Option.bind ~f:(Func.find functions)
|
||
|
(Option.map ~f:Reg.name (Reg.of_exp callee))
|
||
5 years ago
|
with
|
||
6 years ago
|
| Some func ->
|
||
5 years ago
|
if Set.mem ancestors func.entry then call.recursive <- true
|
||
6 years ago
|
else visit ancestors func func.entry
|
||
7 years ago
|
| None ->
|
||
|
(* conservatively assume all virtual calls are recursive *)
|
||
5 years ago
|
call.recursive <- true ) ;
|
||
7 years ago
|
jump return ; Option.iter ~f:jump throw
|
||
|
| Return _ | Throw _ | Unreachable -> () ) ;
|
||
6 years ago
|
BlockQ.enqueue_back_exn tips_to_roots src ()
|
||
7 years ago
|
in
|
||
6 years ago
|
FuncQ.iter roots ~f:(fun root ->
|
||
|
visit Block_label.empty_set root root.entry ) ;
|
||
7 years ago
|
tips_to_roots
|
||
|
in
|
||
|
let set_sort_indices tips_to_roots =
|
||
6 years ago
|
let index = ref (BlockQ.length tips_to_roots) in
|
||
|
BlockQ.iteri tips_to_roots ~f:(fun ~key:block ~data:_ ->
|
||
7 years ago
|
block.sort_index <- !index ;
|
||
6 years ago
|
index := !index - 1 )
|
||
7 years ago
|
in
|
||
6 years ago
|
let functions =
|
||
5 years ago
|
List.fold functions
|
||
|
~init:(Map.empty (module String))
|
||
|
~f:(fun m func ->
|
||
5 years ago
|
Map.add_exn m ~key:(Reg.name func.name.reg) ~data:func )
|
||
7 years ago
|
in
|
||
|
let roots = compute_roots functions in
|
||
|
let tips_to_roots = topsort functions roots in
|
||
|
set_sort_indices tips_to_roots ;
|
||
6 years ago
|
functions
|
||
7 years ago
|
|
||
6 years ago
|
let invariant pgm =
|
||
|
Invariant.invariant [%here] pgm [%sexp_of: t]
|
||
|
@@ fun () ->
|
||
|
assert (
|
||
|
not
|
||
6 years ago
|
(Vector.contains_dup pgm.globals ~compare:(fun g1 g2 ->
|
||
5 years ago
|
Reg.compare g1.Global.reg g2.Global.reg )) )
|
||
7 years ago
|
|
||
6 years ago
|
let mk ~globals ~functions =
|
||
|
{ globals= Vector.of_list_rev globals
|
||
|
; functions= set_derived_metadata functions }
|
||
|
|> check invariant
|
||
6 years ago
|
|
||
6 years ago
|
let pp fs {globals; functions} =
|
||
|
Format.fprintf fs "@[<v>@[%a@]@ @ @ @[%a@]@]"
|
||
|
(Vector.pp "@\n@\n" Global.pp_defn)
|
||
7 years ago
|
globals
|
||
6 years ago
|
(List.pp "@\n@\n" Func.pp)
|
||
|
( Map.data functions
|
||
6 years ago
|
|> List.sort ~compare:(fun x y -> compare_block x.entry y.entry) )
|