1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
(**************************************************************************)
(* *)
(* The Why platform for program certification *)
(* Copyright (C) 2002-2008 *)
(* Romain BARDOU *)
(* Jean-Franois COUCHOT *)
(* Mehdi DOGGUY *)
(* Jean-Christophe FILLITRE *)
(* Thierry HUBERT *)
(* Claude MARCH *)
(* Yannick MOY *)
(* Christine PAULIN *)
(* Yann RGIS-GIANAS *)
(* Nicolas ROUSSET *)
(* Xavier URBAIN *)
(* *)
(* This software is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU General Public *)
(* License version 2, as published by the Free Software Foundation. *)
(* *)
(* This software is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)
(* *)
(* See the GNU General Public License version 2 for more details *)
(* (enclosed in the file GPL). *)
(* *)
(**************************************************************************)
(*i $Id: info.ml,v 1.48 2008/05/28 14:53:34 marche Exp $ i*)
open Ctypes
open Creport
type why_type =
| Memory of why_type * zone
| Pointer of zone
| Addr of zone
| Int
| Real
| Unit
| Why_Logic of string
and zone =
{
zone_is_var : bool;
number : int;
mutable repr : zone option;
name : string;
}
let rec repr_aux z =
match z.repr with
| None -> z
| Some z -> repr_aux z
(* path compression *)
let repr z =
match z.repr with
| None -> z
| Some z' ->
let z'' = repr_aux z' in
z.repr <- Some z''; z''
let same_zone z1 z2 =
(repr z1) = (repr z2)
let rec same_why_type wt1 wt2 =
match wt1, wt2 with
| Pointer z1 , Pointer z2 ->
same_zone z1 z2
| Memory(a1,z1), Memory(a2,z2) ->
same_zone z1 z2 && same_why_type a1 a2
| Int, Int -> true
| Unit, Unit -> true
| Real, Real -> true
| Why_Logic s1, Why_Logic s2 -> s1=s2
| Addr _, _ | _,Addr _ -> assert false
| _ -> false
let rec same_why_type_no_zone wt1 wt2 =
match wt1, wt2 with
| Pointer z1, Pointer z2 -> true
| Memory (a1,_), Memory (a2,_) ->
same_why_type_no_zone a1 a2
| Int, Int -> true
| Unit, Unit -> true
| Real, Real -> true
| Why_Logic s1, Why_Logic s2 -> s1=s2
| Addr _, _ | _,Addr _ -> assert false
| _ -> false
let found_repr ?(quote_var=true) z =
let z = repr z in
if quote_var && z.zone_is_var then "'"^z.name else z.name
let output_zone_name ?(quote_var=true) z =
let name =
if Coptions.no_zone_type then
"global"
else found_repr ~quote_var z
in
{ Output.logic_type_name = name;
Output.logic_type_args = [] }
let rec output_why_type ?(quote_var=true) ty=
let rec output ty =
match ty with
| Int -> [], "int"
| Real -> [], "real"
| Pointer z -> [output_zone_name ~quote_var z] , "pointer"
| Addr z -> [output_zone_name ~quote_var z] , "addr"
| Memory(t,z) ->
[output_why_type ~quote_var t; output_zone_name ~quote_var z], "memory"
| Unit -> [], "unit"
| Why_Logic v -> [], v
in
let l,s = output ty in
{ Output.logic_type_name = s;
Output.logic_type_args = l }
type var_info =
{
var_name : string;
var_uniq_tag : int;
mutable var_unique_name : string;
mutable var_is_assigned : bool;
mutable var_is_referenced : bool;
mutable var_is_static : bool;
mutable var_is_a_formal_param : bool;
mutable enum_constant_value : int64;
mutable var_type : Ctypes.ctype;
mutable var_why_type : why_type;
}
let tag_counter = ref 0
let default_var_info x =
incr tag_counter;
{ var_name = x;
var_uniq_tag = !tag_counter;
var_unique_name = x;
var_is_assigned = false;
var_is_referenced = false;
var_is_static = false;
var_is_a_formal_param = false;
enum_constant_value = Int64.zero;
var_type = c_void;
var_why_type = Unit;
}
let set_assigned v = v.var_is_assigned <- true
let unset_assigned v = v.var_is_assigned <- false
let set_is_referenced v = v.var_is_referenced <- true
let without_dereference v f x =
let old = v.var_is_referenced in
try
v.var_is_referenced <- false;
let y = f x in
v.var_is_referenced <- old;
y
with e ->
v.var_is_referenced <- old;
raise e
let set_static v = v.var_is_static <- true
let set_formal_param v = v.var_is_a_formal_param <- true
let unset_formal_param v = v.var_is_a_formal_param <- false
let set_const_value v n = v.enum_constant_value <- n
module HeapVarSet =
Set.Make(struct type t = var_info
let compare i1 i2 =
Pervasives.compare
i1.var_uniq_tag i2.var_uniq_tag
end)
type label =
| Label_current
| Label_name of string
module LabelSet =
Set.Make(struct type t = label
let compare = compare end)
module HeapVarMap =
Map.Make(struct type t = var_info
let compare i1 i2 =
Pervasives.compare
i1.var_uniq_tag i2.var_uniq_tag
end)
let print_hvs fmt s =
HeapVarSet.iter (fun v -> Format.fprintf fmt "%s," v.var_unique_name) s
module ZoneSet =
Set.Make(struct type t = zone * string * why_type
let compare (i1,s1,_) (i2,s2,_) =
match Pervasives.compare (repr i1).number
(repr i2).number with
| 0 -> Pervasives.compare s1 s2
| x -> x
end)
type logic_info =
{
logic_name : string;
mutable logic_heap_zone : ZoneSet.t;
mutable logic_heap_args : HeapVarSet.t;
(*
mutable logic_heap_args : LabelSet.t HeapVarMap.t;
does not work because of hack in effect.mli, effect of logic funs:
reads_var = id.logic_heap_args;
which confuses global vars with heap vars
*)
mutable logic_args : var_info list;
mutable logic_why_type : why_type;
mutable logic_args_zones : zone list;
}
let default_logic_info x =
{ logic_name = x;
logic_heap_zone = ZoneSet.empty;
logic_heap_args = HeapVarSet.empty;
logic_args = [];
logic_why_type = Why_Logic "?";
logic_args_zones = [];
}
type fun_info =
{
fun_tag : int;
fun_name : string;
mutable fun_unique_name : string;
mutable function_reads : ZoneSet.t;
mutable function_writes : ZoneSet.t;
mutable function_reads_var : HeapVarSet.t;
mutable function_writes_var : HeapVarSet.t;
mutable has_assigns : bool;
mutable fun_type : Ctypes.ctype;
mutable args : var_info list;
mutable args_zones : zone list;
mutable graph : fun_info list;
mutable type_why_fun : why_type;
mutable has_body : bool;
}
let fun_tag_counter = ref 0
let default_fun_info x =
{ fun_tag = (let n = !fun_tag_counter in incr fun_tag_counter; n);
fun_name = x;
fun_unique_name = x;
function_reads = ZoneSet.empty;
function_writes = ZoneSet.empty;
function_reads_var = HeapVarSet.empty;
function_writes_var = HeapVarSet.empty;
has_assigns = false;
fun_type = c_void;
args = [];
args_zones = [];
graph = [];
type_why_fun = Unit;
has_body = false;
}
type env_info =
| Var_info of var_info
| Fun_info of fun_info
let env_name e =
match e with
| Var_info v -> v.var_name
| Fun_info f -> f.fun_name
let set_unique_name e n =
match e with
| Var_info v ->
(*
Coptions.lprintf "Setting unique name of %s to %s@." v.var_name n;
*)
v.var_unique_name <- n
| Fun_info f -> f.fun_unique_name <- n
let var_type d =
match d with
| Var_info v -> v.var_type
| Fun_info f -> f.fun_type
let set_var_type d ty whyty = match d with
| Var_info v ->
Coptions.lprintf "set_var_type %s <- %a@." v.var_name Ctypes.ctype ty;
v.var_type <- ty;
v.var_why_type <- whyty
| Fun_info f ->
Coptions.lprintf "set_var_type %s <- %a@." f.fun_name Ctypes.ctype ty;
f.fun_type <- ty;
f.type_why_fun <- whyty
let set_var_type_why d whyty = match d with
| Var_info v ->
v.var_why_type <- whyty
| Fun_info f ->
f.type_why_fun <- whyty
let get_why_type env =
match env with
| Var_info v -> v.var_why_type
| Fun_info f -> f.type_why_fun
type label_info =
{ label_info_name : string;
mutable times_used : int;
}
|