| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open CErrors |
| 12 |
open Names |
| 13 |
open Constr |
| 14 |
open Context |
| 15 |
open Declarations |
| 16 |
open Util |
| 17 |
open Nativevalues |
| 18 |
open Genlambda |
| 19 |
open Nativelambda |
| 20 |
open Environ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
let debug_native_flag, debug_native_compiler = CDebug.create_full ~name:"native-compiler" () |
| 27 |
|
| 28 |
let keep_debug_files () = |
| 29 |
CDebug.get_flag debug_native_flag |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
type lname = { lname : Name.t; luid : int } |
| 35 |
|
| 36 |
let eq_lname ln1 ln2 = |
| 37 |
Int.equal ln1.luid ln2.luid |
| 38 |
|
| 39 |
let dummy_lname = { lname = Anonymous; luid = -1 } |
| 40 |
|
| 41 |
module LNord = |
| 42 |
struct |
| 43 |
type t = lname |
| 44 |
let compare l1 l2 = l1.luid - l2.luid |
| 45 |
end |
| 46 |
module LNmap = Map.Make(LNord) |
| 47 |
module LNset = Set.Make(LNord) |
| 48 |
|
| 49 |
let rec is_lazy env t = |
| 50 |
match Constr.kind t with |
| 51 |
| App _ | LetIn _ | Case _ | Proj _ -> true |
| 52 |
| Array (_, t, d, _) -> Array.exists (fun t -> is_lazy env t) t || is_lazy env d |
| 53 |
| Cast (c, _, _) | Prod (_, c, _) -> is_lazy env c |
| 54 |
| Const (c, _) -> get_const_lazy env c |
| 55 |
| Rel _ | Meta _ | Var _ | Sort _ | Ind _ | Construct _ | Int _ |
| 56 |
| Float _ | String _ | Lambda _ | Evar _ | Fix _ | CoFix _ -> |
| 57 |
false |
| 58 |
|
| 59 |
and is_lazy_constant env cb = |
| 60 |
|
| 61 |
UVars.AbstractContext.is_constant (Declareops.constant_polymorphic_context cb) && |
| 62 |
|
| 63 |
List.is_empty cb.const_hyps && |
| 64 |
match cb.const_body with |
| 65 |
| Def body -> is_lazy env body |
| 66 |
| Undef _ | OpaqueDef _ | Primitive _ | Symbol _ -> false |
| 67 |
|
| 68 |
and get_const_lazy env c = |
| 69 |
let cb = Environ.lookup_constant c env in |
| 70 |
is_lazy_constant env cb |
| 71 |
|
| 72 |
type prefix = string |
| 73 |
|
| 74 |
|
| 75 |
let get_mind_prefix env mind = |
| 76 |
let _,name,_ = lookup_mind_key mind env in |
| 77 |
match !name with |
| 78 |
| NotLinked -> "" |
| 79 |
| Linked s -> s |
| 80 |
|
| 81 |
let get_const_prefix env c = |
| 82 |
let _,(nameref,_),_ = lookup_constant_key c env in |
| 83 |
match !nameref with |
| 84 |
| NotLinked -> "" |
| 85 |
| Linked s -> s |
| 86 |
|
| 87 |
|
| 88 |
type gname = |
| 89 |
| Gind of string * inductive |
| 90 |
| Gconstant of string * Constant.t |
| 91 |
| Gproj of string * inductive * int |
| 92 |
| Gcase of Id.t option * int |
| 93 |
| Gpred of Id.t option * int |
| 94 |
| Gfixtype of Id.t option * int |
| 95 |
| Gnorm of Id.t option * int |
| 96 |
| Gnormtbl of Id.t option * int |
| 97 |
| Ginternal of string |
| 98 |
| Grel of int |
| 99 |
| Gnamed of Id.t |
| 100 |
|
| 101 |
let eq_gname gn1 gn2 = |
| 102 |
match gn1, gn2 with |
| 103 |
| Gind (s1, ind1), Gind (s2, ind2) -> |
| 104 |
String.equal s1 s2 && Ind.UserOrd.equal ind1 ind2 |
| 105 |
| Gconstant (s1, c1), Gconstant (s2, c2) -> |
| 106 |
String.equal s1 s2 && Constant.UserOrd.equal c1 c2 |
| 107 |
| Gproj (s1, ind1, i1), Gproj (s2, ind2, i2) -> |
| 108 |
String.equal s1 s2 && Ind.UserOrd.equal ind1 ind2 && Int.equal i1 i2 |
| 109 |
| Gcase (None, i1), Gcase (None, i2) -> Int.equal i1 i2 |
| 110 |
| Gcase (Some l1, i1), Gcase (Some l2, i2) -> Int.equal i1 i2 && Id.equal l1 l2 |
| 111 |
| Gpred (None, i1), Gpred (None, i2) -> Int.equal i1 i2 |
| 112 |
| Gpred (Some l1, i1), Gpred (Some l2, i2) -> Int.equal i1 i2 && Id.equal l1 l2 |
| 113 |
| Gfixtype (None, i1), Gfixtype (None, i2) -> Int.equal i1 i2 |
| 114 |
| Gfixtype (Some l1, i1), Gfixtype (Some l2, i2) -> |
| 115 |
Int.equal i1 i2 && Id.equal l1 l2 |
| 116 |
| Gnorm (None, i1), Gnorm (None, i2) -> Int.equal i1 i2 |
| 117 |
| Gnorm (Some l1, i1), Gnorm (Some l2, i2) -> Int.equal i1 i2 && Id.equal l1 l2 |
| 118 |
| Gnormtbl (None, i1), Gnormtbl (None, i2) -> Int.equal i1 i2 |
| 119 |
| Gnormtbl (Some l1, i1), Gnormtbl (Some l2, i2) -> |
| 120 |
Int.equal i1 i2 && Id.equal l1 l2 |
| 121 |
| Ginternal s1, Ginternal s2 -> String.equal s1 s2 |
| 122 |
| Grel i1, Grel i2 -> Int.equal i1 i2 |
| 123 |
| Gnamed id1, Gnamed id2 -> Id.equal id1 id2 |
| 124 |
| (Gind _| Gconstant _ | Gproj _ | Gcase _ | Gpred _ |
| 125 |
| Gfixtype _ | Gnorm _ | Gnormtbl _ | Ginternal _ | Grel _ | Gnamed _), _ -> |
| 126 |
false |
| 127 |
|
| 128 |
let dummy_gname = |
| 129 |
Grel 0 |
| 130 |
|
| 131 |
open Hashset.Combine |
| 132 |
|
| 133 |
let gname_hash gn = match gn with |
| 134 |
| Gind (s, ind) -> |
| 135 |
combinesmall 1 (combine (String.hash s) (Ind.UserOrd.hash ind)) |
| 136 |
| Gconstant (s, c) -> |
| 137 |
combinesmall 2 (combine (String.hash s) (Constant.UserOrd.hash c)) |
| 138 |
| Gcase (l, i) -> combinesmall 3 (combine (Option.hash Id.hash l) (Int.hash i)) |
| 139 |
| Gpred (l, i) -> combinesmall 4 (combine (Option.hash Id.hash l) (Int.hash i)) |
| 140 |
| Gfixtype (l, i) -> combinesmall 5 (combine (Option.hash Id.hash l) (Int.hash i)) |
| 141 |
| Gnorm (l, i) -> combinesmall 6 (combine (Option.hash Id.hash l) (Int.hash i)) |
| 142 |
| Gnormtbl (l, i) -> combinesmall 7 (combine (Option.hash Id.hash l) (Int.hash i)) |
| 143 |
| Ginternal s -> combinesmall 8 (String.hash s) |
| 144 |
| Grel i -> combinesmall 9 (Int.hash i) |
| 145 |
| Gnamed id -> combinesmall 10 (Id.hash id) |
| 146 |
| Gproj (s, p, i) -> combinesmall 11 (combine (String.hash s) (combine (Ind.UserOrd.hash p) i)) |
| 147 |
|
| 148 |
|
| 149 |
let eq_annot_sw asw1 asw2 = |
| 150 |
Ind.UserOrd.equal asw1.asw_ind asw2.asw_ind && |
| 151 |
String.equal asw1.asw_prefix asw2.asw_prefix |
| 152 |
|
| 153 |
open Hashset.Combine |
| 154 |
|
| 155 |
let hash_annot_sw asw = |
| 156 |
combine (Ind.UserOrd.hash asw.asw_ind) (String.hash asw.asw_prefix) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
let eq_symbol sy1 sy2 = |
| 161 |
match sy1, sy2 with |
| 162 |
| SymbValue v1, SymbValue v2 -> (=) v1 v2 |
| 163 |
| SymbSort s1, SymbSort s2 -> Sorts.equal s1 s2 |
| 164 |
| SymbName n1, SymbName n2 -> Name.equal n1 n2 |
| 165 |
| SymbConst kn1, SymbConst kn2 -> Constant.UserOrd.equal kn1 kn2 |
| 166 |
| SymbMatch sw1, SymbMatch sw2 -> eq_annot_sw sw1 sw2 |
| 167 |
| SymbInd ind1, SymbInd ind2 -> Ind.UserOrd.equal ind1 ind2 |
| 168 |
| SymbEvar evk1, SymbEvar evk2 -> Evar.equal evk1 evk2 |
| 169 |
| SymbInstance u1, SymbInstance u2 -> UVars.Instance.equal u1 u2 |
| 170 |
| SymbProj (i1, k1), SymbProj (i2, k2) -> Ind.UserOrd.equal i1 i2 && Int.equal k1 k2 |
| 171 |
|
| 172 |
| (SymbValue _ |
| 173 |
| SymbSort _ |
| 174 |
| SymbName _ |
| 175 |
| SymbConst _ |
| 176 |
| SymbMatch _ |
| 177 |
| SymbInd _ |
| 178 |
| SymbEvar _ |
| 179 |
| SymbInstance _ |
| 180 |
| SymbProj _), _ |
| 181 |
-> false |
| 182 |
|
| 183 |
|
| 184 |
let hash_symbol symb = |
| 185 |
match symb with |
| 186 |
| SymbValue v -> combinesmall 1 (Hashtbl.hash v) |
| 187 |
| SymbSort s -> combinesmall 2 (Sorts.hash s) |
| 188 |
| SymbName name -> combinesmall 3 (Name.hash name) |
| 189 |
| SymbConst c -> combinesmall 4 (Constant.UserOrd.hash c) |
| 190 |
| SymbMatch sw -> combinesmall 5 (hash_annot_sw sw) |
| 191 |
| SymbInd ind -> combinesmall 6 (Ind.UserOrd.hash ind) |
| 192 |
| SymbEvar evk -> combinesmall 7 (Evar.hash evk) |
| 193 |
| SymbInstance u -> combinesmall 8 (UVars.Instance.hash u) |
| 194 |
| SymbProj (i, k) -> combinesmall 9 (combine (Ind.UserOrd.hash i) k) |
| 195 |
|
| 196 |
module HashedTypeSymbol = struct |
| 197 |
type t = symbol |
| 198 |
let equal = eq_symbol |
| 199 |
let hash = hash_symbol |
| 200 |
end |
| 201 |
|
| 202 |
module HashtblSymbol = Hashtbl.Make(HashedTypeSymbol) |
| 203 |
|
| 204 |
let get_value tbl i = |
| 205 |
match tbl.(i) with |
| 206 |
| SymbValue v -> v |
| 207 |
| _ -> anomaly (Pp.str "get_value failed.") |
| 208 |
|
| 209 |
let get_sort tbl i = |
| 210 |
match tbl.(i) with |
| 211 |
| SymbSort s -> s |
| 212 |
| _ -> anomaly (Pp.str "get_sort failed.") |
| 213 |
|
| 214 |
let get_name tbl i = |
| 215 |
match tbl.(i) with |
| 216 |
| SymbName id -> id |
| 217 |
| _ -> anomaly (Pp.str "get_name failed.") |
| 218 |
|
| 219 |
let get_const tbl i = |
| 220 |
match tbl.(i) with |
| 221 |
| SymbConst kn -> kn |
| 222 |
| _ -> anomaly (Pp.str "get_const failed.") |
| 223 |
|
| 224 |
let get_match tbl i = |
| 225 |
match tbl.(i) with |
| 226 |
| SymbMatch case_info -> case_info |
| 227 |
| _ -> anomaly (Pp.str "get_match failed.") |
| 228 |
|
| 229 |
let get_ind tbl i = |
| 230 |
match tbl.(i) with |
| 231 |
| SymbInd ind -> ind |
| 232 |
| _ -> anomaly (Pp.str "get_ind failed.") |
| 233 |
|
| 234 |
let get_evar tbl i = |
| 235 |
match tbl.(i) with |
| 236 |
| SymbEvar ev -> ev |
| 237 |
| _ -> anomaly (Pp.str "get_evar failed.") |
| 238 |
|
| 239 |
let get_instance tbl i = |
| 240 |
match tbl.(i) with |
| 241 |
| SymbInstance u -> u |
| 242 |
| _ -> anomaly (Pp.str "get_instance failed.") |
| 243 |
|
| 244 |
let get_proj tbl i = |
| 245 |
match tbl.(i) with |
| 246 |
| SymbProj p -> p |
| 247 |
| _ -> anomaly (Pp.str "get_proj failed.") |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
type primitive = |
| 252 |
| Mk_prod |
| 253 |
| Mk_sort |
| 254 |
| Mk_ind |
| 255 |
| Mk_const |
| 256 |
| Mk_sw |
| 257 |
| Mk_fix of rec_pos * int |
| 258 |
| Mk_cofix of int |
| 259 |
| Mk_rel of int |
| 260 |
| Mk_var of Id.t |
| 261 |
| Mk_proj |
| 262 |
| Is_int |
| 263 |
| Is_float |
| 264 |
| Is_string |
| 265 |
| Is_parray |
| 266 |
| Cast_accu |
| 267 |
| Array_get |
| 268 |
| Force_cofix |
| 269 |
| Mk_uint |
| 270 |
| Mk_float |
| 271 |
| Mk_string |
| 272 |
| Mk_int |
| 273 |
| Val_to_int |
| 274 |
| Mk_evar |
| 275 |
| MLand |
| 276 |
| MLnot |
| 277 |
| MLland |
| 278 |
| MLmagic |
| 279 |
| MLsubst_instance_instance |
| 280 |
| MLsubst_instance_sort |
| 281 |
| MLparray_of_array |
| 282 |
| Get_value |
| 283 |
| Get_sort |
| 284 |
| Get_name |
| 285 |
| Get_const |
| 286 |
| Get_match |
| 287 |
| Get_ind |
| 288 |
| Get_evar |
| 289 |
| Get_instance |
| 290 |
| Get_proj |
| 291 |
| Get_symbols |
| 292 |
| Lazy |
| 293 |
| Coq_primitive of CPrimitives.t * bool |
| 294 |
| Mk_empty_instance |
| 295 |
|
| 296 |
let eq_primitive p1 p2 = |
| 297 |
match p1, p2 with |
| 298 |
| Mk_prod, Mk_prod |
| 299 |
| Mk_sort, Mk_sort |
| 300 |
| Mk_ind, Mk_ind |
| 301 |
| Mk_const, Mk_const |
| 302 |
| Mk_sw, Mk_sw |
| 303 |
| Mk_proj, Mk_proj |
| 304 |
| Is_int, Is_int |
| 305 |
| Is_float, Is_float |
| 306 |
| Is_string, Is_string |
| 307 |
| Is_parray, Is_parray |
| 308 |
| Cast_accu, Cast_accu |
| 309 |
| Array_get, Array_get |
| 310 |
| Force_cofix, Force_cofix |
| 311 |
| Mk_uint, Mk_uint |
| 312 |
| Mk_float, Mk_float |
| 313 |
| Mk_string, Mk_string |
| 314 |
| Mk_int, Mk_int |
| 315 |
| Val_to_int, Val_to_int |
| 316 |
| Mk_evar, Mk_evar |
| 317 |
| MLand, MLand |
| 318 |
| MLnot, MLnot |
| 319 |
| MLland, MLland |
| 320 |
| MLmagic, MLmagic |
| 321 |
| MLsubst_instance_instance, MLsubst_instance_instance |
| 322 |
| MLsubst_instance_sort, MLsubst_instance_sort |
| 323 |
| MLparray_of_array, MLparray_of_array |
| 324 |
| Get_value, Get_value |
| 325 |
| Get_sort, Get_sort |
| 326 |
| Get_name, Get_name |
| 327 |
| Get_const, Get_const |
| 328 |
| Get_match, Get_match |
| 329 |
| Get_ind, Get_ind |
| 330 |
| Get_evar, Get_evar |
| 331 |
| Get_instance, Get_instance |
| 332 |
| Get_proj, Get_proj |
| 333 |
| Get_symbols, Get_symbols |
| 334 |
| Lazy, Lazy |
| 335 |
| Mk_empty_instance, Mk_empty_instance |
| 336 |
-> true |
| 337 |
|
| 338 |
| Mk_fix (rp1, i1), Mk_fix (rp2, i2) -> Int.equal i1 i2 && eq_rec_pos rp1 rp2 |
| 339 |
| Mk_cofix i1, Mk_cofix i2 -> Int.equal i1 i2 |
| 340 |
| Mk_rel i1, Mk_rel i2 -> Int.equal i1 i2 |
| 341 |
| Mk_var id1, Mk_var id2 -> Id.equal id1 id2 |
| 342 |
| Coq_primitive (prim1,b1), Coq_primitive (prim2,b2) -> |
| 343 |
CPrimitives.equal prim1 prim2 && Bool.equal b1 b2 |
| 344 |
|
| 345 |
| (Mk_prod |
| 346 |
| Mk_sort |
| 347 |
| Mk_ind |
| 348 |
| Mk_const |
| 349 |
| Mk_sw |
| 350 |
| Mk_fix _ |
| 351 |
| Mk_cofix _ |
| 352 |
| Mk_rel _ |
| 353 |
| Mk_var _ |
| 354 |
| Mk_proj |
| 355 |
| Is_int |
| 356 |
| Is_float |
| 357 |
| Is_string |
| 358 |
| Is_parray |
| 359 |
| Cast_accu |
| 360 |
| Array_get |
| 361 |
| Force_cofix |
| 362 |
| Mk_uint |
| 363 |
| Mk_float |
| 364 |
| Mk_string |
| 365 |
| Mk_int |
| 366 |
| Val_to_int |
| 367 |
| Mk_evar |
| 368 |
| MLand |
| 369 |
| MLnot |
| 370 |
| MLland |
| 371 |
| MLmagic |
| 372 |
| MLsubst_instance_instance |
| 373 |
| MLsubst_instance_sort |
| 374 |
| MLparray_of_array |
| 375 |
| Get_value |
| 376 |
| Get_sort |
| 377 |
| Get_name |
| 378 |
| Get_const |
| 379 |
| Get_match |
| 380 |
| Get_ind |
| 381 |
| Get_evar |
| 382 |
| Get_instance |
| 383 |
| Get_proj |
| 384 |
| Get_symbols |
| 385 |
| Lazy |
| 386 |
| Coq_primitive _ |
| 387 |
| Mk_empty_instance), _ |
| 388 |
-> false |
| 389 |
|
| 390 |
let primitive_hash = function |
| 391 |
| Mk_prod -> 1 |
| 392 |
| Mk_sort -> 2 |
| 393 |
| Mk_ind -> 3 |
| 394 |
| Mk_const -> 4 |
| 395 |
| Mk_sw -> 5 |
| 396 |
| Mk_fix (r, i) -> |
| 397 |
let h = Array.fold_left (fun h i -> combine h (Int.hash i)) 0 r in |
| 398 |
combinesmall 6 (combine h (Int.hash i)) |
| 399 |
| Mk_cofix i -> |
| 400 |
combinesmall 7 (Int.hash i) |
| 401 |
| Mk_rel i -> |
| 402 |
combinesmall 8 (Int.hash i) |
| 403 |
| Mk_var id -> |
| 404 |
combinesmall 9 (Id.hash id) |
| 405 |
| Is_int -> 11 |
| 406 |
| Cast_accu -> 12 |
| 407 |
| Array_get -> 13 |
| 408 |
| Force_cofix -> 14 |
| 409 |
| Mk_uint -> 15 |
| 410 |
| Mk_int -> 16 |
| 411 |
| Val_to_int -> 17 |
| 412 |
| Mk_evar -> 18 |
| 413 |
| MLand -> 19 |
| 414 |
| MLland -> 20 |
| 415 |
| MLmagic -> 21 |
| 416 |
| Coq_primitive (prim, b) -> combinesmall 22 (combine (CPrimitives.hash prim) (Hashtbl.hash b)) |
| 417 |
| Mk_proj -> 23 |
| 418 |
| MLsubst_instance_instance -> 24 |
| 419 |
| MLsubst_instance_sort -> 25 |
| 420 |
| Mk_float -> 26 |
| 421 |
| Is_float -> 27 |
| 422 |
| Is_string -> 28 |
| 423 |
| Is_parray -> 29 |
| 424 |
| MLnot -> 30 |
| 425 |
| MLparray_of_array -> 31 |
| 426 |
| Get_value -> 32 |
| 427 |
| Get_sort -> 33 |
| 428 |
| Get_name -> 34 |
| 429 |
| Get_const -> 35 |
| 430 |
| Get_match -> 36 |
| 431 |
| Get_ind -> 37 |
| 432 |
| Get_evar -> 38 |
| 433 |
| Get_instance -> 39 |
| 434 |
| Get_proj -> 40 |
| 435 |
| Get_symbols -> 41 |
| 436 |
| Lazy -> 42 |
| 437 |
| Mk_empty_instance -> 43 |
| 438 |
| Mk_string -> 44 |
| 439 |
|
| 440 |
type mllambda = |
| 441 |
| MLlocal of lname |
| 442 |
| MLglobal of gname |
| 443 |
| MLprimitive of primitive * mllambda array |
| 444 |
| MLlam of lname array * mllambda |
| 445 |
| MLletrec of (lname * lname array * mllambda) array * mllambda |
| 446 |
| MLlet of lname * mllambda * mllambda |
| 447 |
| MLapp of mllambda * mllambda array |
| 448 |
| MLif of mllambda * mllambda * mllambda |
| 449 |
| MLmatch of annot_sw * mllambda * mllambda * mllam_branches |
| 450 |
|
| 451 |
| MLconstruct of string * inductive * int * mllambda array |
| 452 |
|
| 453 |
| MLint of int |
| 454 |
| MLuint of Uint63.t |
| 455 |
| MLfloat of Float64.t |
| 456 |
| MLstring of Pstring.t |
| 457 |
| MLsetref of string * mllambda |
| 458 |
| MLsequence of mllambda * mllambda |
| 459 |
| MLarray of mllambda array |
| 460 |
| MLisaccu of string * inductive * mllambda |
| 461 |
|
| 462 |
and 'a mllam_pattern = |
| 463 |
| ConstPattern of int |
| 464 |
| NonConstPattern of tag * 'a array |
| 465 |
|
| 466 |
and mllam_branches = (lname option mllam_pattern list * mllambda) array |
| 467 |
|
| 468 |
let push_lnames n env lns = |
| 469 |
snd (Array.fold_left (fun (i,r) x -> (i+1, LNmap.add x i r)) (n,env) lns) |
| 470 |
|
| 471 |
let opush_lnames n env lns = |
| 472 |
let oadd x i r = match x with Some ln -> LNmap.add ln i r | None -> r in |
| 473 |
snd (Array.fold_left (fun (i,r) x -> (i+1, oadd x i r)) (n,env) lns) |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
let rec eq_mllambda gn1 gn2 n env1 env2 t1 t2 = |
| 478 |
match t1, t2 with |
| 479 |
| MLlocal ln1, MLlocal ln2 -> |
| 480 |
(try |
| 481 |
Int.equal (LNmap.find ln1 env1) (LNmap.find ln2 env2) |
| 482 |
with Not_found -> |
| 483 |
eq_lname ln1 ln2) |
| 484 |
| MLglobal gn1', MLglobal gn2' -> |
| 485 |
eq_gname gn1' gn2' || (eq_gname gn1 gn1' && eq_gname gn2 gn2') |
| 486 |
|| (eq_gname gn1 gn2' && eq_gname gn2 gn1') |
| 487 |
| MLprimitive (prim1, args1), MLprimitive (prim2, args2) -> |
| 488 |
eq_primitive prim1 prim2 && |
| 489 |
Array.equal (eq_mllambda gn1 gn2 n env1 env2) args1 args2 |
| 490 |
| MLlam (lns1, ml1), MLlam (lns2, ml2) -> |
| 491 |
Int.equal (Array.length lns1) (Array.length lns2) && |
| 492 |
let env1 = push_lnames n env1 lns1 in |
| 493 |
let env2 = push_lnames n env2 lns2 in |
| 494 |
eq_mllambda gn1 gn2 (n+Array.length lns1) env1 env2 ml1 ml2 |
| 495 |
| MLletrec (defs1, body1), MLletrec (defs2, body2) -> |
| 496 |
Int.equal (Array.length defs1) (Array.length defs2) && |
| 497 |
let lns1 = Array.map (fun (x,_,_) -> x) defs1 in |
| 498 |
let lns2 = Array.map (fun (x,_,_) -> x) defs2 in |
| 499 |
let env1 = push_lnames n env1 lns1 in |
| 500 |
let env2 = push_lnames n env2 lns2 in |
| 501 |
let n = n + Array.length defs1 in |
| 502 |
eq_letrec gn1 gn2 n env1 env2 defs1 defs2 && |
| 503 |
eq_mllambda gn1 gn2 n env1 env2 body1 body2 |
| 504 |
| MLlet (ln1, def1, body1), MLlet (ln2, def2, body2) -> |
| 505 |
eq_mllambda gn1 gn2 n env1 env2 def1 def2 && |
| 506 |
let env1 = LNmap.add ln1 n env1 in |
| 507 |
let env2 = LNmap.add ln2 n env2 in |
| 508 |
eq_mllambda gn1 gn2 (n+1) env1 env2 body1 body2 |
| 509 |
| MLapp (ml1, args1), MLapp (ml2, args2) -> |
| 510 |
eq_mllambda gn1 gn2 n env1 env2 ml1 ml2 && |
| 511 |
Array.equal (eq_mllambda gn1 gn2 n env1 env2) args1 args2 |
| 512 |
| MLif (cond1,br1,br'1), MLif (cond2,br2,br'2) -> |
| 513 |
eq_mllambda gn1 gn2 n env1 env2 cond1 cond2 && |
| 514 |
eq_mllambda gn1 gn2 n env1 env2 br1 br2 && |
| 515 |
eq_mllambda gn1 gn2 n env1 env2 br'1 br'2 |
| 516 |
| MLmatch (annot1, c1, accu1, br1), MLmatch (annot2, c2, accu2, br2) -> |
| 517 |
eq_annot_sw annot1 annot2 && |
| 518 |
eq_mllambda gn1 gn2 n env1 env2 c1 c2 && |
| 519 |
eq_mllambda gn1 gn2 n env1 env2 accu1 accu2 && |
| 520 |
eq_mllam_branches gn1 gn2 n env1 env2 br1 br2 |
| 521 |
| MLconstruct (pf1, ind1, tag1, args1), MLconstruct (pf2, ind2, tag2, args2) -> |
| 522 |
String.equal pf1 pf2 && |
| 523 |
Ind.UserOrd.equal ind1 ind2 && |
| 524 |
Int.equal tag1 tag2 && |
| 525 |
Array.equal (eq_mllambda gn1 gn2 n env1 env2) args1 args2 |
| 526 |
| MLint i1, MLint i2 -> |
| 527 |
Int.equal i1 i2 |
| 528 |
| MLuint i1, MLuint i2 -> |
| 529 |
Uint63.equal i1 i2 |
| 530 |
| MLfloat f1, MLfloat f2 -> |
| 531 |
Float64.equal f1 f2 |
| 532 |
| MLstring s1, MLstring s2 -> |
| 533 |
Pstring.equal s1 s2 |
| 534 |
| MLsetref (id1, ml1), MLsetref (id2, ml2) -> |
| 535 |
String.equal id1 id2 && |
| 536 |
eq_mllambda gn1 gn2 n env1 env2 ml1 ml2 |
| 537 |
| MLsequence (ml1, ml'1), MLsequence (ml2, ml'2) -> |
| 538 |
eq_mllambda gn1 gn2 n env1 env2 ml1 ml2 && |
| 539 |
eq_mllambda gn1 gn2 n env1 env2 ml'1 ml'2 |
| 540 |
| MLarray arr1, MLarray arr2 -> |
| 541 |
Array.equal (eq_mllambda gn1 gn2 n env1 env2) arr1 arr2 |
| 542 |
|
| 543 |
| MLisaccu (s1, ind1, ml1), MLisaccu (s2, ind2, ml2) -> |
| 544 |
String.equal s1 s2 && Ind.UserOrd.equal ind1 ind2 && |
| 545 |
eq_mllambda gn1 gn2 n env1 env2 ml1 ml2 |
| 546 |
| (MLlocal _ | MLglobal _ | MLprimitive _ | MLlam _ | MLletrec _ | MLlet _ | |
| 547 |
MLapp _ | MLif _ | MLmatch _ | MLconstruct _ | MLint _ | MLuint _ | |
| 548 |
MLfloat _ | MLstring _ | MLsetref _ | MLsequence _ | |
| 549 |
MLarray _ | MLisaccu _), _ -> false |
| 550 |
|
| 551 |
and eq_letrec gn1 gn2 n env1 env2 defs1 defs2 = |
| 552 |
let eq_def (_,args1,ml1) (_,args2,ml2) = |
| 553 |
Int.equal (Array.length args1) (Array.length args2) && |
| 554 |
let env1 = push_lnames n env1 args1 in |
| 555 |
let env2 = push_lnames n env2 args2 in |
| 556 |
eq_mllambda gn1 gn2 (n + Array.length args1) env1 env2 ml1 ml2 |
| 557 |
in |
| 558 |
Array.equal eq_def defs1 defs2 |
| 559 |
|
| 560 |
|
| 561 |
and eq_mllam_branches gn1 gn2 n env1 env2 br1 br2 = |
| 562 |
let eq_cargs args1 args2 body1 body2 = |
| 563 |
Int.equal (Array.length args1) (Array.length args2) && |
| 564 |
let env1 = opush_lnames n env1 args1 in |
| 565 |
let env2 = opush_lnames n env2 args2 in |
| 566 |
eq_mllambda gn1 gn2 (n + Array.length args1) env1 env2 body1 body2 |
| 567 |
in |
| 568 |
let eq_pattern pat1 pat2 body1 body2 = |
| 569 |
match pat1, pat2 with |
| 570 |
| ConstPattern tag1, ConstPattern tag2 -> |
| 571 |
Int.equal tag1 tag2 && eq_mllambda gn1 gn2 n env1 env2 body1 body2 |
| 572 |
| NonConstPattern (tag1,args1), NonConstPattern (tag2,args2) -> |
| 573 |
Int.equal tag1 tag2 && eq_cargs args1 args2 body1 body2 |
| 574 |
| (ConstPattern _ | NonConstPattern _), _ -> false |
| 575 |
in |
| 576 |
let eq_branch (patl1,body1) (patl2,body2) = |
| 577 |
List.equal (fun pt1 pt2 -> eq_pattern pt1 pt2 body1 body2) patl1 patl2 |
| 578 |
in |
| 579 |
Array.equal eq_branch br1 br2 |
| 580 |
|
| 581 |
|
| 582 |
let rec hash_mllambda gn n env t = |
| 583 |
match t with |
| 584 |
| MLlocal ln -> combinesmall 1 (LNmap.find ln env) |
| 585 |
| MLglobal gn' -> combinesmall 2 (if eq_gname gn gn' then 0 else gname_hash gn') |
| 586 |
| MLprimitive (prim, args) -> |
| 587 |
let h = primitive_hash prim in |
| 588 |
combinesmall 3 (hash_mllambda_array gn n env h args) |
| 589 |
| MLlam (lns, ml) -> |
| 590 |
let env = push_lnames n env lns in |
| 591 |
combinesmall 4 (combine (Array.length lns) (hash_mllambda gn (n+1) env ml)) |
| 592 |
| MLletrec (defs, body) -> |
| 593 |
let lns = Array.map (fun (x,_,_) -> x) defs in |
| 594 |
let env = push_lnames n env lns in |
| 595 |
let n = n + Array.length defs in |
| 596 |
let h = combine (hash_mllambda gn n env body) (Array.length defs) in |
| 597 |
combinesmall 5 (hash_mllambda_letrec gn n env h defs) |
| 598 |
| MLlet (ln, def, body) -> |
| 599 |
let hdef = hash_mllambda gn n env def in |
| 600 |
let env = LNmap.add ln n env in |
| 601 |
combinesmall 6 (combine hdef (hash_mllambda gn (n+1) env body)) |
| 602 |
| MLapp (ml, args) -> |
| 603 |
let h = hash_mllambda gn n env ml in |
| 604 |
combinesmall 7 (hash_mllambda_array gn n env h args) |
| 605 |
| MLif (cond,br,br') -> |
| 606 |
let hcond = hash_mllambda gn n env cond in |
| 607 |
let hbr = hash_mllambda gn n env br in |
| 608 |
let hbr' = hash_mllambda gn n env br' in |
| 609 |
combinesmall 8 (combine3 hcond hbr hbr') |
| 610 |
| MLmatch (annot, c, accu, br) -> |
| 611 |
let hannot = hash_annot_sw annot in |
| 612 |
let hc = hash_mllambda gn n env c in |
| 613 |
let haccu = hash_mllambda gn n env accu in |
| 614 |
combinesmall 9 (hash_mllam_branches gn n env (combine3 hannot hc haccu) br) |
| 615 |
| MLconstruct (pf, ind, tag, args) -> |
| 616 |
let hpf = String.hash pf in |
| 617 |
let hcs = Ind.UserOrd.hash ind in |
| 618 |
let htag = Int.hash tag in |
| 619 |
combinesmall 10 (hash_mllambda_array gn n env (combine3 hpf hcs htag) args) |
| 620 |
| MLint i -> |
| 621 |
combinesmall 11 i |
| 622 |
| MLuint i -> |
| 623 |
combinesmall 12 (Uint63.hash i) |
| 624 |
| MLsetref (id, ml) -> |
| 625 |
let hid = String.hash id in |
| 626 |
let hml = hash_mllambda gn n env ml in |
| 627 |
combinesmall 13 (combine hid hml) |
| 628 |
| MLsequence (ml, ml') -> |
| 629 |
let hml = hash_mllambda gn n env ml in |
| 630 |
let hml' = hash_mllambda gn n env ml' in |
| 631 |
combinesmall 14 (combine hml hml') |
| 632 |
| MLarray arr -> |
| 633 |
combinesmall 15 (hash_mllambda_array gn n env 1 arr) |
| 634 |
| MLisaccu (s, ind, c) -> |
| 635 |
combinesmall 16 (combine (String.hash s) (combine (Ind.UserOrd.hash ind) (hash_mllambda gn n env c))) |
| 636 |
| MLfloat f -> |
| 637 |
combinesmall 17 (Float64.hash f) |
| 638 |
| MLstring s -> |
| 639 |
combinesmall 18 (Pstring.hash s) |
| 640 |
|
| 641 |
and hash_mllambda_letrec gn n env init defs = |
| 642 |
let hash_def (_,args,ml) = |
| 643 |
let env = push_lnames n env args in |
| 644 |
let nargs = Array.length args in |
| 645 |
combine nargs (hash_mllambda gn (n + nargs) env ml) |
| 646 |
in |
| 647 |
Array.fold_left (fun acc t -> combine (hash_def t) acc) init defs |
| 648 |
|
| 649 |
and hash_mllambda_array gn n env init arr = |
| 650 |
Array.fold_left (fun acc t -> combine (hash_mllambda gn n env t) acc) init arr |
| 651 |
|
| 652 |
and hash_mllam_branches gn n env init br = |
| 653 |
let hash_cargs args body = |
| 654 |
let nargs = Array.length args in |
| 655 |
let env = opush_lnames n env args in |
| 656 |
let hbody = hash_mllambda gn (n + nargs) env body in |
| 657 |
combine nargs hbody |
| 658 |
in |
| 659 |
let hash_pattern pat body = match pat with |
| 660 |
| ConstPattern i -> combinesmall 1 (Int.hash i) |
| 661 |
| NonConstPattern (tag,args) -> combinesmall 2 (combine (Int.hash tag) (hash_cargs args body)) |
| 662 |
in |
| 663 |
let hash_branch acc (ptl,body) = |
| 664 |
List.fold_left (fun acc t -> combine (hash_pattern t body) acc) acc ptl |
| 665 |
in |
| 666 |
Array.fold_left hash_branch init br |
| 667 |
|
| 668 |
let fv_lam l = |
| 669 |
let rec aux l bind fv = |
| 670 |
match l with |
| 671 |
| MLlocal l -> |
| 672 |
if LNset.mem l bind then fv else LNset.add l fv |
| 673 |
| MLglobal _ | MLint _ | MLuint _ | MLfloat _ | MLstring _ -> fv |
| 674 |
| MLprimitive (_, args) -> |
| 675 |
let fv_arg arg fv = aux arg bind fv in |
| 676 |
Array.fold_right fv_arg args fv |
| 677 |
| MLlam (ln,body) -> |
| 678 |
let bind = Array.fold_right LNset.add ln bind in |
| 679 |
aux body bind fv |
| 680 |
| MLletrec(bodies,def) -> |
| 681 |
let bind = |
| 682 |
Array.fold_right (fun (id,_,_) b -> LNset.add id b) bodies bind in |
| 683 |
let fv_body (_,ln,body) fv = |
| 684 |
let bind = Array.fold_right LNset.add ln bind in |
| 685 |
aux body bind fv in |
| 686 |
Array.fold_right fv_body bodies (aux def bind fv) |
| 687 |
| MLlet(l,def,body) -> |
| 688 |
aux body (LNset.add l bind) (aux def bind fv) |
| 689 |
| MLapp(f,args) -> |
| 690 |
let fv_arg arg fv = aux arg bind fv in |
| 691 |
Array.fold_right fv_arg args (aux f bind fv) |
| 692 |
| MLif(t,b1,b2) -> |
| 693 |
aux t bind (aux b1 bind (aux b2 bind fv)) |
| 694 |
| MLmatch(_,a,p,bs) -> |
| 695 |
let fv = aux a bind (aux p bind fv) in |
| 696 |
let fv_bs (cargs, body) fv = |
| 697 |
let bind = |
| 698 |
List.fold_right (fun pat bind -> |
| 699 |
match pat with |
| 700 |
| ConstPattern _ -> bind |
| 701 |
| NonConstPattern(_,args) -> |
| 702 |
Array.fold_right |
| 703 |
(fun o bind -> match o with |
| 704 |
| Some l -> LNset.add l bind |
| 705 |
| _ -> bind) args bind) |
| 706 |
cargs bind in |
| 707 |
aux body bind fv in |
| 708 |
Array.fold_right fv_bs bs fv |
| 709 |
|
| 710 |
| MLconstruct (_,_,_,p) -> |
| 711 |
Array.fold_right (fun a fv -> aux a bind fv) p fv |
| 712 |
| MLsetref(_,l) -> aux l bind fv |
| 713 |
| MLsequence(l1,l2) -> aux l1 bind (aux l2 bind fv) |
| 714 |
| MLarray arr -> Array.fold_right (fun a fv -> aux a bind fv) arr fv |
| 715 |
| MLisaccu (_, _, body) -> aux body bind fv |
| 716 |
in |
| 717 |
aux l LNset.empty LNset.empty |
| 718 |
|
| 719 |
|
| 720 |
let mkMLlam params body = |
| 721 |
if Array.is_empty params then body |
| 722 |
else |
| 723 |
match body with |
| 724 |
| MLlam (params', body) -> MLlam(Array.append params params', body) |
| 725 |
| _ -> MLlam(params,body) |
| 726 |
|
| 727 |
let mkMLapp f args = |
| 728 |
if Array.is_empty args then f |
| 729 |
else |
| 730 |
match f with |
| 731 |
| MLapp(f,args') -> MLapp(f,Array.append args' args) |
| 732 |
| _ -> MLapp(f,args) |
| 733 |
|
| 734 |
let empty_params = [||] |
| 735 |
|
| 736 |
let decompose_MLlam c = |
| 737 |
match c with |
| 738 |
| MLlam(ids,c) -> ids,c |
| 739 |
| _ -> empty_params,c |
| 740 |
|
| 741 |
|
| 742 |
type global = |
| 743 |
|
| 744 |
| Gtblnorm of gname * lname array * mllambda array |
| 745 |
| Gtblcofix of gname * lname array * mllambda array |
| 746 |
| Gtblfixtype of gname * lname array * mllambda array |
| 747 |
| Glet of gname * mllambda |
| 748 |
| Gletcase of |
| 749 |
gname * lname array * annot_sw * mllambda * mllambda * mllam_branches |
| 750 |
| Gopen of string |
| 751 |
| Gtype of inductive * (tag * int) array |
| 752 |
|
| 753 |
| Gcomment of string |
| 754 |
|
| 755 |
|
| 756 |
let eq_global g1 g2 = |
| 757 |
match g1, g2 with |
| 758 |
| Gtblnorm (gn1,lns1,mls1), Gtblnorm (gn2,lns2,mls2) |
| 759 |
| Gtblcofix (gn1,lns1,mls1), Gtblcofix (gn2,lns2,mls2) |
| 760 |
| Gtblfixtype (gn1,lns1,mls1), Gtblfixtype (gn2,lns2,mls2) -> |
| 761 |
Int.equal (Array.length lns1) (Array.length lns2) && |
| 762 |
Int.equal (Array.length mls1) (Array.length mls2) && |
| 763 |
let env1 = push_lnames 0 LNmap.empty lns1 in |
| 764 |
let env2 = push_lnames 0 LNmap.empty lns2 in |
| 765 |
Array.for_all2 (eq_mllambda gn1 gn2 (Array.length lns1) env1 env2) mls1 mls2 |
| 766 |
| Glet (gn1, def1), Glet (gn2, def2) -> |
| 767 |
eq_mllambda gn1 gn2 0 LNmap.empty LNmap.empty def1 def2 |
| 768 |
| Gletcase (gn1,lns1,annot1,c1,accu1,br1), |
| 769 |
Gletcase (gn2,lns2,annot2,c2,accu2,br2) -> |
| 770 |
Int.equal (Array.length lns1) (Array.length lns2) && |
| 771 |
let env1 = push_lnames 0 LNmap.empty lns1 in |
| 772 |
let env2 = push_lnames 0 LNmap.empty lns2 in |
| 773 |
let t1 = MLmatch (annot1,c1,accu1,br1) in |
| 774 |
let t2 = MLmatch (annot2,c2,accu2,br2) in |
| 775 |
eq_mllambda gn1 gn2 (Array.length lns1) env1 env2 t1 t2 |
| 776 |
| Gopen s1, Gopen s2 -> String.equal s1 s2 |
| 777 |
| Gtype (ind1, arr1), Gtype (ind2, arr2) -> |
| 778 |
Ind.UserOrd.equal ind1 ind2 && |
| 779 |
Array.equal (fun (tag1,ar1) (tag2,ar2) -> Int.equal tag1 tag2 && Int.equal ar1 ar2) arr1 arr2 |
| 780 |
| Gcomment s1, Gcomment s2 -> String.equal s1 s2 |
| 781 |
| _, _ -> false |
| 782 |
|
| 783 |
let hash_global g = |
| 784 |
match g with |
| 785 |
| Gtblnorm (gn,lns,mls) -> |
| 786 |
let nlns = Array.length lns in |
| 787 |
let nmls = Array.length mls in |
| 788 |
let env = push_lnames 0 LNmap.empty lns in |
| 789 |
let hmls = hash_mllambda_array gn nlns env (combine nlns nmls) mls in |
| 790 |
combinesmall 1 hmls |
| 791 |
| Gtblcofix (gn,lns,mls) -> |
| 792 |
let nlns = Array.length lns in |
| 793 |
let nmls = Array.length mls in |
| 794 |
let env = push_lnames 0 LNmap.empty lns in |
| 795 |
let hmls = hash_mllambda_array gn nlns env (combine nlns nmls) mls in |
| 796 |
combinesmall 2 hmls |
| 797 |
| Gtblfixtype (gn,lns,mls) -> |
| 798 |
let nlns = Array.length lns in |
| 799 |
let nmls = Array.length mls in |
| 800 |
let env = push_lnames 0 LNmap.empty lns in |
| 801 |
let hmls = hash_mllambda_array gn nlns env (combine nlns nmls) mls in |
| 802 |
combinesmall 3 hmls |
| 803 |
| Glet (gn, def) -> |
| 804 |
combinesmall 4 (hash_mllambda gn 0 LNmap.empty def) |
| 805 |
| Gletcase (gn,lns,annot,c,accu,br) -> |
| 806 |
let nlns = Array.length lns in |
| 807 |
let env = push_lnames 0 LNmap.empty lns in |
| 808 |
let t = MLmatch (annot,c,accu,br) in |
| 809 |
combinesmall 5 (combine nlns (hash_mllambda gn nlns env t)) |
| 810 |
| Gopen s -> combinesmall 5 (String.hash s) |
| 811 |
| Gtype (ind, arr) -> |
| 812 |
let hash_aux acc (tag,ar) = |
| 813 |
combine3 acc (Int.hash tag) (Int.hash ar) |
| 814 |
in |
| 815 |
combinesmall 7 (combine (Ind.UserOrd.hash ind) (Array.fold_left hash_aux 0 arr)) |
| 816 |
| Gcomment s -> combinesmall 8 (String.hash s) |
| 817 |
|
| 818 |
module HashedTypeGlobal = struct |
| 819 |
type t = global |
| 820 |
let equal = eq_global |
| 821 |
let hash = hash_global |
| 822 |
end |
| 823 |
|
| 824 |
module HashtblGlobal = Hashtbl.Make(HashedTypeGlobal) |
| 825 |
|
| 826 |
type cenv = { |
| 827 |
mutable lname_ctr : int; |
| 828 |
mutable case_ctr : int; |
| 829 |
mutable pred_ctr : int; |
| 830 |
mutable fixtype_ctr : int; |
| 831 |
mutable norm_ctr : int; |
| 832 |
mutable normtbl_ctr : int; |
| 833 |
global_tbl : gname HashtblGlobal.t; |
| 834 |
mutable global_stack : global list; |
| 835 |
symb_tbl : arity HashtblSymbol.t; |
| 836 |
} |
| 837 |
|
| 838 |
let make_cenv () = { |
| 839 |
lname_ctr = -1; |
| 840 |
case_ctr = -1; |
| 841 |
pred_ctr = -1; |
| 842 |
fixtype_ctr = -1; |
| 843 |
norm_ctr = -1; |
| 844 |
normtbl_ctr = -1; |
| 845 |
global_tbl = HashtblGlobal.create 91; |
| 846 |
global_stack = []; |
| 847 |
symb_tbl = HashtblSymbol.create 211; |
| 848 |
} |
| 849 |
|
| 850 |
let fresh_lname cenv n = |
| 851 |
let () = cenv.lname_ctr <- cenv.lname_ctr + 1 in |
| 852 |
{ lname = n; luid = cenv.lname_ctr } |
| 853 |
|
| 854 |
let fresh_gcase cenv l = |
| 855 |
let () = cenv.case_ctr <- cenv.case_ctr + 1 in |
| 856 |
Gcase (l, cenv.case_ctr) |
| 857 |
|
| 858 |
let fresh_gpred cenv l = |
| 859 |
let () = cenv.pred_ctr <- cenv.pred_ctr + 1 in |
| 860 |
Gpred (l, cenv.pred_ctr) |
| 861 |
|
| 862 |
let fresh_gfixtype cenv l = |
| 863 |
let () = cenv.fixtype_ctr <- cenv.fixtype_ctr + 1 in |
| 864 |
Gfixtype (l, cenv.fixtype_ctr) |
| 865 |
|
| 866 |
let fresh_gnorm cenv l = |
| 867 |
let () = cenv.norm_ctr <- cenv.norm_ctr + 1 in |
| 868 |
Gnorm (l, cenv.norm_ctr) |
| 869 |
|
| 870 |
let fresh_gnormtbl cenv l = |
| 871 |
let () = cenv.normtbl_ctr <- cenv.normtbl_ctr + 1 in |
| 872 |
Gnormtbl (l, cenv.normtbl_ctr) |
| 873 |
|
| 874 |
let mkForceCofix cenv prefix ind arg = |
| 875 |
let name = fresh_lname cenv Anonymous in |
| 876 |
MLlet (name, arg, |
| 877 |
MLif ( |
| 878 |
MLisaccu (prefix, ind, MLlocal name), |
| 879 |
MLprimitive (Force_cofix, [|MLlocal name|]), |
| 880 |
MLlocal name)) |
| 881 |
|
| 882 |
let push_global cenv gn t = |
| 883 |
try HashtblGlobal.find cenv.global_tbl t |
| 884 |
with Not_found -> |
| 885 |
let () = cenv.global_stack <- t :: cenv.global_stack in |
| 886 |
let () = HashtblGlobal.add cenv.global_tbl t gn in |
| 887 |
gn |
| 888 |
|
| 889 |
let push_global_let cenv gn body = |
| 890 |
push_global cenv gn (Glet (gn,body)) |
| 891 |
|
| 892 |
let push_global_fixtype cenv gn params body = |
| 893 |
push_global cenv gn (Gtblfixtype (gn,params,body)) |
| 894 |
|
| 895 |
let push_global_norm cenv gn params body = |
| 896 |
push_global cenv gn (Gtblnorm (gn, params, body)) |
| 897 |
|
| 898 |
let push_global_cofix cenv gn params self = |
| 899 |
push_global cenv gn (Gtblcofix (gn, params, self)) |
| 900 |
|
| 901 |
let push_global_case cenv gn params annot a accu bs = |
| 902 |
push_global cenv gn (Gletcase (gn, params, annot, a, accu, bs)) |
| 903 |
|
| 904 |
let push_symbol cenv x = |
| 905 |
try HashtblSymbol.find cenv.symb_tbl x |
| 906 |
with Not_found -> |
| 907 |
let i = HashtblSymbol.length cenv.symb_tbl in |
| 908 |
let () = HashtblSymbol.add cenv.symb_tbl x i in |
| 909 |
i |
| 910 |
|
| 911 |
let get_cenv_symbols cenv = |
| 912 |
match HashtblSymbol.to_seq cenv.symb_tbl () with |
| 913 |
| Nil -> [||] |
| 914 |
| Cons ((x,_), rest) -> |
| 915 |
let tbl = Array.make (HashtblSymbol.length cenv.symb_tbl) x in |
| 916 |
let () = Seq.iter (fun (x, i) -> tbl.(i) <- x) rest in |
| 917 |
tbl |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
let eq_mllambda t1 t2 = |
| 922 |
eq_mllambda dummy_gname dummy_gname 0 LNmap.empty LNmap.empty t1 t2 |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
type uinstance = |
| 927 |
| UGlobal |
| 928 |
| ULocal of lname option |
| 929 |
|
| 930 |
type env = |
| 931 |
{ env_rel : mllambda list; |
| 932 |
env_bound : int; |
| 933 |
|
| 934 |
env_urel : (int * mllambda) list ref; |
| 935 |
env_named : (Id.t * mllambda) list ref; |
| 936 |
env_univ : uinstance; |
| 937 |
env_const_prefix : Constant.t -> prefix; |
| 938 |
env_const_lazy : Constant.t -> bool; |
| 939 |
env_mind_prefix : MutInd.t -> prefix; |
| 940 |
env_cenv : cenv; |
| 941 |
} |
| 942 |
|
| 943 |
let empty_env cenv univ get_const const_lazy get_mind = |
| 944 |
{ env_rel = []; |
| 945 |
env_bound = 0; |
| 946 |
env_urel = ref []; |
| 947 |
env_named = ref []; |
| 948 |
env_univ = univ; |
| 949 |
env_const_prefix = get_const; |
| 950 |
env_const_lazy = const_lazy; |
| 951 |
env_mind_prefix = get_mind; |
| 952 |
env_cenv = cenv; |
| 953 |
} |
| 954 |
|
| 955 |
let restart_env env = |
| 956 |
empty_env env.env_cenv env.env_univ env.env_const_prefix env.env_const_lazy env.env_mind_prefix |
| 957 |
|
| 958 |
let push_rel env id = |
| 959 |
let local = fresh_lname env.env_cenv id.binder_name in |
| 960 |
local, { env with |
| 961 |
env_rel = MLlocal local :: env.env_rel; |
| 962 |
env_bound = env.env_bound + 1 |
| 963 |
} |
| 964 |
|
| 965 |
let push_rels env ids = |
| 966 |
let lnames, env_rel = |
| 967 |
Array.fold_left (fun (names,env_rel) id -> |
| 968 |
let local = fresh_lname env.env_cenv id.binder_name in |
| 969 |
(local::names, MLlocal local::env_rel)) ([],env.env_rel) ids in |
| 970 |
Array.of_list (List.rev lnames), { env with |
| 971 |
env_rel = env_rel; |
| 972 |
env_bound = env.env_bound + Array.length ids |
| 973 |
} |
| 974 |
|
| 975 |
let get_rel env id i = |
| 976 |
if i <= env.env_bound then |
| 977 |
List.nth env.env_rel (i-1) |
| 978 |
else |
| 979 |
let i = i - env.env_bound in |
| 980 |
try Int.List.assoc i !(env.env_urel) |
| 981 |
with Not_found -> |
| 982 |
let local = MLlocal (fresh_lname env.env_cenv id) in |
| 983 |
env.env_urel := (i,local) :: !(env.env_urel); |
| 984 |
local |
| 985 |
|
| 986 |
let get_var env id = |
| 987 |
try Id.List.assoc id !(env.env_named) |
| 988 |
with Not_found -> |
| 989 |
let local = MLlocal (fresh_lname env.env_cenv (Name id)) in |
| 990 |
env.env_named := (id, local)::!(env.env_named); |
| 991 |
local |
| 992 |
|
| 993 |
let fresh_univ cenv = |
| 994 |
fresh_lname cenv (Name (Id.of_string "univ")) |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
let get_prod_name codom = |
| 999 |
match codom with |
| 1000 |
| MLlam(ids,_) -> ids.(0).lname |
| 1001 |
| _ -> assert false |
| 1002 |
|
| 1003 |
let get_lname (_,l) = |
| 1004 |
match l with |
| 1005 |
| MLlocal id -> id |
| 1006 |
| _ -> invalid_arg "Nativecode.get_lname" |
| 1007 |
|
| 1008 |
|
| 1009 |
let fv_params env = |
| 1010 |
let fvn, fvr = !(env.env_named), !(env.env_urel) in |
| 1011 |
let size = List.length fvn + List.length fvr in |
| 1012 |
let start,params = match env.env_univ with |
| 1013 |
| UGlobal | ULocal None -> |
| 1014 |
(0, Array.make size dummy_lname) |
| 1015 |
| ULocal (Some u) -> |
| 1016 |
let t = Array.make (size + 1) dummy_lname in |
| 1017 |
let () = t.(0) <- u in |
| 1018 |
(1, t) |
| 1019 |
in |
| 1020 |
if Array.is_empty params then empty_params |
| 1021 |
else begin |
| 1022 |
let fvn = ref fvn in |
| 1023 |
let i = ref start in |
| 1024 |
while not (List.is_empty !fvn) do |
| 1025 |
params.(!i) <- get_lname (List.hd !fvn); |
| 1026 |
fvn := List.tl !fvn; |
| 1027 |
incr i |
| 1028 |
done; |
| 1029 |
let fvr = ref fvr in |
| 1030 |
while not (List.is_empty !fvr) do |
| 1031 |
params.(!i) <- get_lname (List.hd !fvr); |
| 1032 |
fvr := List.tl !fvr; |
| 1033 |
incr i |
| 1034 |
done; |
| 1035 |
params |
| 1036 |
end |
| 1037 |
|
| 1038 |
let generalize_fv env body = |
| 1039 |
mkMLlam (fv_params env) body |
| 1040 |
|
| 1041 |
let empty_args = [||] |
| 1042 |
|
| 1043 |
let fv_args env fvn fvr = |
| 1044 |
let size = List.length fvn + List.length fvr in |
| 1045 |
let start,args = match env.env_univ with |
| 1046 |
| UGlobal | ULocal None -> |
| 1047 |
(0, Array.make size (MLint 0)) |
| 1048 |
| ULocal (Some u) -> |
| 1049 |
let t = Array.make (size + 1) (MLint 0) in |
| 1050 |
let () = t.(0) <- MLlocal u in |
| 1051 |
(1, t) |
| 1052 |
in |
| 1053 |
if Array.is_empty args then empty_args |
| 1054 |
else |
| 1055 |
begin |
| 1056 |
let fvn = ref fvn in |
| 1057 |
let i = ref start in |
| 1058 |
while not (List.is_empty !fvn) do |
| 1059 |
args.(!i) <- get_var env (fst (List.hd !fvn)); |
| 1060 |
fvn := List.tl !fvn; |
| 1061 |
incr i |
| 1062 |
done; |
| 1063 |
let fvr = ref fvr in |
| 1064 |
while not (List.is_empty !fvr) do |
| 1065 |
let (k,_ as kml) = List.hd !fvr in |
| 1066 |
let n = get_lname kml in |
| 1067 |
args.(!i) <- get_rel env n.lname k; |
| 1068 |
fvr := List.tl !fvr; |
| 1069 |
incr i |
| 1070 |
done; |
| 1071 |
args |
| 1072 |
end |
| 1073 |
|
| 1074 |
let symbols_tbl_name = Ginternal "symbols_tbl" |
| 1075 |
|
| 1076 |
let get_value_code i = |
| 1077 |
MLprimitive (Get_value, |
| 1078 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1079 |
|
| 1080 |
let get_sort_code i = |
| 1081 |
MLprimitive (Get_sort, |
| 1082 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1083 |
|
| 1084 |
let get_name_code i = |
| 1085 |
MLprimitive (Get_name, |
| 1086 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1087 |
|
| 1088 |
let get_const_code i = |
| 1089 |
MLprimitive (Get_const, |
| 1090 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1091 |
|
| 1092 |
let get_match_code i = |
| 1093 |
MLprimitive (Get_match, |
| 1094 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1095 |
|
| 1096 |
let get_ind_code i = |
| 1097 |
MLprimitive (Get_ind, |
| 1098 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1099 |
|
| 1100 |
let get_evar_code i = |
| 1101 |
MLprimitive (Get_evar, |
| 1102 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1103 |
|
| 1104 |
let get_instance_code i = |
| 1105 |
MLprimitive (Get_instance, |
| 1106 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1107 |
|
| 1108 |
let get_proj_code i = |
| 1109 |
MLprimitive (Get_proj, |
| 1110 |
[|MLglobal symbols_tbl_name; MLint i|]) |
| 1111 |
|
| 1112 |
type rlist = |
| 1113 |
| Rnil |
| 1114 |
| Rcons of lname option mllam_pattern list ref * LNset.t * mllambda * rlist' |
| 1115 |
and rlist' = rlist ref |
| 1116 |
|
| 1117 |
let rm_params fv params = |
| 1118 |
Array.map (fun l -> if LNset.mem l fv then Some l else None) params |
| 1119 |
|
| 1120 |
let rec insert pat body rl = |
| 1121 |
match !rl with |
| 1122 |
| Rnil -> |
| 1123 |
let fv = fv_lam body in |
| 1124 |
begin match pat with |
| 1125 |
| ConstPattern _ as p -> |
| 1126 |
rl:= Rcons(ref [p], fv, body, ref Rnil) |
| 1127 |
| NonConstPattern (tag,args) -> |
| 1128 |
let args = rm_params fv args in |
| 1129 |
rl:= Rcons(ref [NonConstPattern (tag,args)], fv, body, ref Rnil) |
| 1130 |
end |
| 1131 |
| Rcons(l,fv,body',rl) -> |
| 1132 |
if eq_mllambda body body' then |
| 1133 |
match pat with |
| 1134 |
| ConstPattern _ as p -> |
| 1135 |
l := p::!l |
| 1136 |
| NonConstPattern (tag,args) -> |
| 1137 |
let args = rm_params fv args in |
| 1138 |
l := NonConstPattern (tag,args)::!l |
| 1139 |
else insert pat body rl |
| 1140 |
|
| 1141 |
let rec to_list rl = |
| 1142 |
match !rl with |
| 1143 |
| Rnil -> [] |
| 1144 |
| Rcons(l,_,body,tl) -> (!l,body)::to_list tl |
| 1145 |
|
| 1146 |
let merge_branches t = |
| 1147 |
let newt = ref Rnil in |
| 1148 |
Array.iter (fun (pat,body) -> insert pat body newt) t; |
| 1149 |
Array.of_list (to_list newt) |
| 1150 |
|
| 1151 |
let app_prim p args = MLprimitive (p, args) |
| 1152 |
|
| 1153 |
let ml_empty_instance = MLprimitive (Mk_empty_instance, [||]) |
| 1154 |
|
| 1155 |
type prim_aux = |
| 1156 |
| PAprim of string * pconstant * CPrimitives.t * prim_aux array |
| 1157 |
| PAml of mllambda |
| 1158 |
|
| 1159 |
let add_check cond targs args = |
| 1160 |
let aux cond t a = |
| 1161 |
match t, a with |
| 1162 |
| CPrimitives.(PITT_type (PT_int63, _)), PAml(MLprimitive (Mk_uint, _)) -> cond |
| 1163 |
| CPrimitives.(PITT_type (PT_array, _)), PAml(MLprimitive (MLparray_of_array, _)) -> cond |
| 1164 |
| CPrimitives.(PITT_type (PT_array, _)), PAml(MLprimitive (Get_value ,_)) -> cond |
| 1165 |
| CPrimitives.(PITT_type (prim_ty, _)), PAml ml -> |
| 1166 |
|
| 1167 |
let c = (CPrimitives.PTE prim_ty, ml) in |
| 1168 |
if List.mem c cond then cond else c::cond |
| 1169 |
| _ -> cond |
| 1170 |
in |
| 1171 |
Array.fold_left2 aux cond targs args |
| 1172 |
|
| 1173 |
let extract_prim env ml_of l = |
| 1174 |
let decl = ref [] in |
| 1175 |
let cond = ref [] in |
| 1176 |
let type_args p = |
| 1177 |
let params, args_ty, _ = CPrimitives.types p in |
| 1178 |
List.length params, Array.of_list args_ty in |
| 1179 |
let rec aux l = |
| 1180 |
match node l with |
| 1181 |
| Lprim (kn, p, args) -> |
| 1182 |
let prefix = env.env_const_prefix (fst kn) in |
| 1183 |
let nparams, targs = type_args p in |
| 1184 |
let args = Array.map aux args in |
| 1185 |
let checked_args = Array.init (Array.length args - nparams) (fun i -> args.(i+nparams)) in |
| 1186 |
cond := add_check !cond targs checked_args; |
| 1187 |
PAprim (prefix, kn, p, args) |
| 1188 |
| Lrel _ | Lvar _ | Luint _ | Lval _ | Lconst _ -> PAml (ml_of l) |
| 1189 |
| _ -> |
| 1190 |
let x = fresh_lname env.env_cenv Anonymous in |
| 1191 |
decl := (x,ml_of l)::!decl; |
| 1192 |
PAml (MLlocal x) in |
| 1193 |
let res = aux l in |
| 1194 |
(!decl, !cond, res) |
| 1195 |
|
| 1196 |
let cast_to_int v = |
| 1197 |
match v with |
| 1198 |
| MLint _ -> v |
| 1199 |
| _ -> MLprimitive (Val_to_int, [|v|]) |
| 1200 |
|
| 1201 |
let ml_of_instance env u = |
| 1202 |
if UVars.Instance.is_empty u then [||] |
| 1203 |
else |
| 1204 |
let i = push_symbol env.env_cenv (SymbInstance u) in |
| 1205 |
let u_code = get_instance_code i in |
| 1206 |
let u_code = match env.env_univ with |
| 1207 |
| UGlobal -> u_code |
| 1208 |
| ULocal instance -> |
| 1209 |
let has_variable = |
| 1210 |
let qs, us = UVars.Instance.to_array u in |
| 1211 |
Array.exists (fun q -> Option.has_some (Sorts.Quality.var_index q)) qs |
| 1212 |
|| Array.exists (fun u -> Option.has_some (Univ.Level.var_index u)) us |
| 1213 |
in |
| 1214 |
let u_code = |
| 1215 |
if has_variable then |
| 1216 |
|
| 1217 |
let univ = MLprimitive (MLmagic, [|MLlocal (Option.get instance)|]) in |
| 1218 |
MLprimitive (MLsubst_instance_instance, [|univ; u_code|]) |
| 1219 |
else u_code |
| 1220 |
in |
| 1221 |
u_code |
| 1222 |
in |
| 1223 |
[|MLprimitive (MLmagic, [|u_code|])|] |
| 1224 |
|
| 1225 |
let ml_of_sort env s = |
| 1226 |
let i = push_symbol env.env_cenv (SymbSort s) in |
| 1227 |
let s_code = get_sort_code i in |
| 1228 |
let s_code = match env.env_univ with |
| 1229 |
| UGlobal | ULocal None -> s_code |
| 1230 |
| ULocal (Some u) -> |
| 1231 |
|
| 1232 |
let u = MLprimitive (MLmagic, [|MLlocal u|]) in |
| 1233 |
MLprimitive (MLsubst_instance_sort, [|u; s_code|]) |
| 1234 |
in |
| 1235 |
MLprimitive (Mk_sort, [|s_code|]) |
| 1236 |
|
| 1237 |
let compile_prim env decl cond paux = |
| 1238 |
|
| 1239 |
let rec opt_prim_aux paux = |
| 1240 |
match paux with |
| 1241 |
| PAprim(_prefix, _kn, op, args) -> |
| 1242 |
let n = CPrimitives.nparams op in |
| 1243 |
let args = Array.map opt_prim_aux (Array.sub args n (Array.length args - n)) in |
| 1244 |
app_prim (Coq_primitive(op, false)) args |
| 1245 |
| PAml ml -> ml |
| 1246 |
|
| 1247 |
and naive_prim_aux paux = |
| 1248 |
match paux with |
| 1249 |
| PAprim(prefix, (kn,u), op, args) -> |
| 1250 |
let uarg = ml_of_instance env u in |
| 1251 |
let prim_const = mkMLapp (MLglobal (Gconstant(prefix,kn))) uarg in |
| 1252 |
let prim = MLprimitive ((Coq_primitive(op, true)), [|prim_const|]) in |
| 1253 |
mkMLapp prim (Array.map naive_prim_aux args) |
| 1254 |
| PAml ml -> ml |
| 1255 |
in |
| 1256 |
|
| 1257 |
let compile_cond cond paux = |
| 1258 |
match cond with |
| 1259 |
| [] -> opt_prim_aux paux |
| 1260 |
| [CPrimitives.(PTE PT_int63), c1] -> |
| 1261 |
MLif(app_prim Is_int [|c1|], opt_prim_aux paux, naive_prim_aux paux) |
| 1262 |
| _ -> |
| 1263 |
let ci, co = |
| 1264 |
let is_int = |
| 1265 |
function CPrimitives.(PTE PT_int63), _ -> true | _ -> false in |
| 1266 |
List.partition is_int cond in |
| 1267 |
let condi = |
| 1268 |
let cond = |
| 1269 |
List.fold_left |
| 1270 |
(fun ml (_, c) -> app_prim MLland [| ml; cast_to_int c|]) |
| 1271 |
(MLint 0) ci in |
| 1272 |
app_prim MLmagic [|cond|] in |
| 1273 |
let condo = match co with |
| 1274 |
| [] -> MLint 0 |
| 1275 |
| (CPrimitives.PTE ty, c1) :: condo -> |
| 1276 |
let check = match ty with |
| 1277 |
| CPrimitives.PT_float64 -> Is_float |
| 1278 |
| CPrimitives.PT_array -> Is_parray |
| 1279 |
| CPrimitives.PT_int63 -> assert false |
| 1280 |
| CPrimitives.PT_string -> Is_string |
| 1281 |
in |
| 1282 |
List.fold_left |
| 1283 |
(fun ml (_, c) -> app_prim MLand [| ml; app_prim check [|c|]|]) |
| 1284 |
(app_prim check [|c1|]) condo in |
| 1285 |
match ci, co with |
| 1286 |
| [], [] -> opt_prim_aux paux |
| 1287 |
| _ :: _, [] -> |
| 1288 |
MLif(condi, naive_prim_aux paux, opt_prim_aux paux) |
| 1289 |
| [], _ :: _ -> |
| 1290 |
MLif(condo, opt_prim_aux paux, naive_prim_aux paux) |
| 1291 |
| _ :: _, _ :: _ -> |
| 1292 |
let cond = app_prim MLand [|condo; app_prim MLnot [|condi|]|] in |
| 1293 |
MLif(cond, opt_prim_aux paux, naive_prim_aux paux) in |
| 1294 |
|
| 1295 |
let add_decl decl body = |
| 1296 |
List.fold_left (fun body (x,d) -> MLlet(x,d,body)) body decl in |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
if max_int = 1073741823 then |
| 1301 |
add_decl decl (naive_prim_aux paux) |
| 1302 |
else |
| 1303 |
add_decl decl (compile_cond cond paux) |
| 1304 |
|
| 1305 |
let rec ml_of_lam env l t = |
| 1306 |
match node t with |
| 1307 |
| Lrel(id ,i) -> get_rel env id i |
| 1308 |
| Lvar id -> get_var env id |
| 1309 |
| Levar(evk, args) -> |
| 1310 |
let i = push_symbol env.env_cenv (SymbEvar evk) in |
| 1311 |
|
| 1312 |
let args = MLarray(Array.map (ml_of_lam env l) args) in |
| 1313 |
MLprimitive (Mk_evar, [|get_evar_code i; args|]) |
| 1314 |
| Lprod(dom,codom) -> |
| 1315 |
let dom = ml_of_lam env l dom in |
| 1316 |
let codom = ml_of_lam env l codom in |
| 1317 |
let n = get_prod_name codom in |
| 1318 |
let i = push_symbol env.env_cenv (SymbName n) in |
| 1319 |
MLprimitive (Mk_prod, [|get_name_code i;dom;codom|]) |
| 1320 |
| Llam(ids,body) -> |
| 1321 |
let lnames,env = push_rels env ids in |
| 1322 |
MLlam(lnames, ml_of_lam env l body) |
| 1323 |
| Llet(id,def,body) -> |
| 1324 |
let def = ml_of_lam env l def in |
| 1325 |
let lname, env = push_rel env id in |
| 1326 |
let body = ml_of_lam env l body in |
| 1327 |
MLlet(lname,def,body) |
| 1328 |
| Lapp(f,args) -> |
| 1329 |
MLapp(ml_of_lam env l f, Array.map (ml_of_lam env l) args) |
| 1330 |
| Lconst (c, u) -> |
| 1331 |
let prefix = env.env_const_prefix c in |
| 1332 |
let args = ml_of_instance env u in |
| 1333 |
let ans = mkMLapp (MLglobal(Gconstant (prefix, c))) args in |
| 1334 |
if env.env_const_lazy c then MLapp (MLglobal (Ginternal "Lazy.force"), [|ans|]) |
| 1335 |
else ans |
| 1336 |
| Lproj (p, c) -> |
| 1337 |
let ind = Projection.Repr.inductive p in |
| 1338 |
let i = Projection.Repr.arg p in |
| 1339 |
let prefix = env.env_mind_prefix (fst ind) in |
| 1340 |
MLapp (MLglobal(Gproj (prefix, ind, i)), [| ml_of_lam env l c |]) |
| 1341 |
| Lprim _ -> |
| 1342 |
let decl,cond,paux = extract_prim env (ml_of_lam env l) t in |
| 1343 |
compile_prim env decl cond paux |
| 1344 |
| Lcase (annot,p,a,bs) -> |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
let annot, finite = |
| 1356 |
let (ci, tbl, finite) = annot in { |
| 1357 |
asw_ind = ci.ci_ind; |
| 1358 |
asw_reloc = tbl; |
| 1359 |
asw_prefix = env.env_mind_prefix (fst ci.ci_ind); |
| 1360 |
}, finite in |
| 1361 |
let env_p = restart_env env in |
| 1362 |
let pn = fresh_gpred env.env_cenv l in |
| 1363 |
let mlp = ml_of_lam env_p l p in |
| 1364 |
let mlp = generalize_fv env_p mlp in |
| 1365 |
let (pfvn,pfvr) = !(env_p.env_named), !(env_p.env_urel) in |
| 1366 |
let pn = push_global_let env.env_cenv pn mlp in |
| 1367 |
|
| 1368 |
let env_c = restart_env env in |
| 1369 |
let a_uid = fresh_lname env.env_cenv Anonymous in |
| 1370 |
let la_uid = MLlocal a_uid in |
| 1371 |
|
| 1372 |
let nbconst = Array.length bs.constant_branches in |
| 1373 |
let nbtotal = nbconst + Array.length bs.nonconstant_branches in |
| 1374 |
let br = Array.init nbtotal (fun i -> if i < Array.length bs.constant_branches then |
| 1375 |
(ConstPattern i, ml_of_lam env_c l bs.constant_branches.(i)) |
| 1376 |
else |
| 1377 |
let (params, body) = bs.nonconstant_branches.(i-nbconst) in |
| 1378 |
let lnames, env_c = push_rels env_c params in |
| 1379 |
(NonConstPattern (i-nbconst+1,lnames), ml_of_lam env_c l body) |
| 1380 |
) |
| 1381 |
in |
| 1382 |
let cn = fresh_gcase env.env_cenv l in |
| 1383 |
|
| 1384 |
let pred = MLapp(MLglobal pn, fv_args env_c pfvn pfvr) in |
| 1385 |
let (fvn, fvr) = !(env_c.env_named), !(env_c.env_urel) in |
| 1386 |
let cn_fv = mkMLapp (MLglobal cn) (fv_args env_c fvn fvr) in |
| 1387 |
|
| 1388 |
let i = push_symbol env.env_cenv (SymbMatch annot) in |
| 1389 |
let accu = |
| 1390 |
MLprimitive (Mk_sw, |
| 1391 |
[| get_match_code i; MLprimitive (Cast_accu, [|la_uid|]); |
| 1392 |
pred; |
| 1393 |
cn_fv |]) in |
| 1394 |
|
| 1395 |
|
| 1396 |
let cn = push_global_case env.env_cenv cn (Array.append (fv_params env_c) [|a_uid|]) |
| 1397 |
annot la_uid accu (merge_branches br) |
| 1398 |
in |
| 1399 |
|
| 1400 |
let arg = ml_of_lam env l a in |
| 1401 |
let force = |
| 1402 |
if finite <> CoFinite then arg |
| 1403 |
else mkForceCofix env.env_cenv annot.asw_prefix annot.asw_ind arg in |
| 1404 |
mkMLapp (MLapp (MLglobal cn, fv_args env fvn fvr)) [|force|] |
| 1405 |
| Lfix ((rec_pos, inds, start), (ids, tt, tb)) -> |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
let env_t = restart_env env in |
| 1423 |
let ml_t = Array.map (ml_of_lam env_t l) tt in |
| 1424 |
let params_t = fv_params env_t in |
| 1425 |
let args_t = fv_args env !(env_t.env_named) !(env_t.env_urel) in |
| 1426 |
let gft = fresh_gfixtype env.env_cenv l in |
| 1427 |
let gft = push_global_fixtype env.env_cenv gft params_t ml_t in |
| 1428 |
let mk_type = MLapp(MLglobal gft, args_t) in |
| 1429 |
|
| 1430 |
let ndef = Array.length ids in |
| 1431 |
let lf,env_n = push_rels (restart_env env) ids in |
| 1432 |
let t_params = Array.make ndef [||] in |
| 1433 |
let t_norm_f = Array.make ndef (Gnorm (l,-1)) in |
| 1434 |
let mk_let _envi (id,def) t = MLlet (id,def,t) in |
| 1435 |
let mk_lam_or_let (params,lets,env) (id,def) = |
| 1436 |
let ln,env' = push_rel env id in |
| 1437 |
match def with |
| 1438 |
| None -> (ln::params,lets,env') |
| 1439 |
| Some lam -> (params, (ln,ml_of_lam env l lam)::lets,env') |
| 1440 |
in |
| 1441 |
let ml_of_fix i body = |
| 1442 |
let varsi, bodyi = decompose_Llam_Llet body in |
| 1443 |
let paramsi,letsi,envi = |
| 1444 |
Array.fold_left mk_lam_or_let ([],[],env_n) varsi |
| 1445 |
in |
| 1446 |
let paramsi,letsi = |
| 1447 |
Array.of_list (List.rev paramsi), Array.of_list (List.rev letsi) |
| 1448 |
in |
| 1449 |
t_norm_f.(i) <- fresh_gnorm env.env_cenv l; |
| 1450 |
let bodyi = ml_of_lam envi l bodyi in |
| 1451 |
t_params.(i) <- paramsi; |
| 1452 |
let bodyi = Array.fold_right (mk_let envi) letsi bodyi in |
| 1453 |
mkMLlam paramsi bodyi |
| 1454 |
in |
| 1455 |
let tnorm = Array.mapi ml_of_fix tb in |
| 1456 |
let fvn,fvr = !(env_n.env_named), !(env_n.env_urel) in |
| 1457 |
let fv_params = fv_params env_n in |
| 1458 |
let fv_args' = Array.map (fun id -> MLlocal id) fv_params in |
| 1459 |
let norm_params = Array.append fv_params lf in |
| 1460 |
let t_norm_f = Array.mapi (fun i body -> |
| 1461 |
push_global_let env.env_cenv (t_norm_f.(i)) (mkMLlam norm_params body)) tnorm in |
| 1462 |
let norm = fresh_gnormtbl env.env_cenv l in |
| 1463 |
let norm = push_global_norm env.env_cenv norm fv_params |
| 1464 |
(Array.map (fun g -> mkMLapp (MLglobal g) fv_args') t_norm_f) in |
| 1465 |
|
| 1466 |
let fv_args = fv_args env fvn fvr in |
| 1467 |
let lf, _env = push_rels env ids in |
| 1468 |
let lf_args = Array.map (fun id -> MLlocal id) lf in |
| 1469 |
let mk_norm = MLapp(MLglobal norm, fv_args) in |
| 1470 |
let mkrec i lname = |
| 1471 |
let paramsi = t_params.(i) in |
| 1472 |
let reci = MLlocal (paramsi.(rec_pos.(i))) in |
| 1473 |
let pargsi = Array.map (fun id -> MLlocal id) paramsi in |
| 1474 |
let ind = inds.(i) in |
| 1475 |
let prefix = env.env_mind_prefix (fst ind) in |
| 1476 |
let body = |
| 1477 |
MLif(MLisaccu (prefix, ind, reci), |
| 1478 |
mkMLapp |
| 1479 |
(MLprimitive ((Mk_fix(rec_pos,i)), |
| 1480 |
[|mk_type; mk_norm|])) |
| 1481 |
pargsi, |
| 1482 |
MLapp(MLglobal t_norm_f.(i), |
| 1483 |
Array.concat [fv_args;lf_args;pargsi])) |
| 1484 |
in |
| 1485 |
(lname, paramsi, body) in |
| 1486 |
MLletrec(Array.mapi mkrec lf, lf_args.(start)) |
| 1487 |
| Lcofix (start, (ids, tt, tb)) -> |
| 1488 |
|
| 1489 |
let env_t = restart_env env in |
| 1490 |
let ml_t = Array.map (ml_of_lam env_t l) tt in |
| 1491 |
let params_t = fv_params env_t in |
| 1492 |
let args_t = Array.map (fun id -> MLlocal id) params_t in |
| 1493 |
let gft = fresh_gfixtype env.env_cenv l in |
| 1494 |
let gft = push_global_fixtype env.env_cenv gft params_t ml_t in |
| 1495 |
let mk_type = MLapp(MLglobal gft, args_t) in |
| 1496 |
|
| 1497 |
let ndef = Array.length ids in |
| 1498 |
let lf,env_n = push_rels env_t ids in |
| 1499 |
let t_params = Array.make ndef [||] in |
| 1500 |
let t_norm_f = Array.init ndef (fun _i -> fresh_gnorm env.env_cenv l) in |
| 1501 |
let ml_of_fix i body = |
| 1502 |
let idsi,bodyi = decompose_Llam body in |
| 1503 |
let paramsi, envi = push_rels env_n idsi in |
| 1504 |
let bodyi = ml_of_lam envi l bodyi in |
| 1505 |
t_params.(i) <- paramsi; |
| 1506 |
mkMLlam paramsi bodyi |
| 1507 |
in |
| 1508 |
let tnorm = Array.mapi ml_of_fix tb in |
| 1509 |
let fvn,fvr = !(env_n.env_named), !(env_n.env_urel) in |
| 1510 |
let fv_params = fv_params env_n in |
| 1511 |
let fv_args' = Array.map (fun id -> MLlocal id) fv_params in |
| 1512 |
let norm_params = Array.append fv_params lf in |
| 1513 |
let t_norm_f = Array.mapi (fun i body -> |
| 1514 |
push_global_let env.env_cenv (t_norm_f.(i)) (mkMLlam norm_params body)) tnorm in |
| 1515 |
let norm = fresh_gnormtbl env.env_cenv l in |
| 1516 |
let norm = push_global_norm env.env_cenv norm fv_params |
| 1517 |
(Array.map (fun g -> mkMLapp (MLglobal g) fv_args') t_norm_f) in |
| 1518 |
|
| 1519 |
let fv_args = fv_args env fvn fvr in |
| 1520 |
let mk_norm = MLapp(MLglobal norm, fv_args') in |
| 1521 |
|
| 1522 |
let knot = fresh_gnormtbl env.env_cenv l in |
| 1523 |
let map i g = |
| 1524 |
|
| 1525 |
let unit = fresh_lname env.env_cenv Anonymous in |
| 1526 |
let args = Array.map (fun id -> MLlocal id) t_params.(i) in |
| 1527 |
let mk_let i lname cont = |
| 1528 |
MLlet (lname, MLprimitive (Array_get, [|MLglobal knot; MLint i|]), cont) |
| 1529 |
in |
| 1530 |
let self = Array.map (fun id -> MLlocal id) lf in |
| 1531 |
let body = mkMLapp (MLglobal g) (Array.concat [fv_args'; self; args]) in |
| 1532 |
let body = MLprimitive (MLmagic, [|MLlam ([|unit|], Array.fold_right_i mk_let lf body)|]) in |
| 1533 |
let typs = mk_type in |
| 1534 |
let self = mk_norm in |
| 1535 |
mkMLlam t_params.(i) (MLprimitive ((Mk_cofix i), [| typs; self; body; MLarray args |])) |
| 1536 |
in |
| 1537 |
|
| 1538 |
let knot = push_global_cofix env.env_cenv knot fv_params (Array.mapi map t_norm_f) in |
| 1539 |
MLprimitive (Array_get, [|MLapp (MLglobal knot, fv_args); MLint start|]) |
| 1540 |
|
| 1541 |
| Lint tag -> MLprimitive (Mk_int, [|MLint tag|]) |
| 1542 |
|
| 1543 |
| Lmakeblock (cn,tag,args) -> |
| 1544 |
let prefix = env.env_mind_prefix (fst cn) in |
| 1545 |
let args = Array.map (ml_of_lam env l) args in |
| 1546 |
MLconstruct(prefix,cn,tag,args) |
| 1547 |
| Luint i -> MLprimitive (Mk_uint, [|MLuint i|]) |
| 1548 |
| Lfloat f -> MLprimitive (Mk_float, [|MLfloat f|]) |
| 1549 |
| Lstring s -> MLprimitive (Mk_string, [|MLstring s|]) |
| 1550 |
| Lparray (t,def) -> |
| 1551 |
let def = ml_of_lam env l def in |
| 1552 |
MLprimitive (MLparray_of_array, [| MLarray (Array.map (ml_of_lam env l) t); def |]) |
| 1553 |
| Lval v -> |
| 1554 |
let i = push_symbol env.env_cenv (SymbValue v) in get_value_code i |
| 1555 |
| Lsort s -> |
| 1556 |
ml_of_sort env s |
| 1557 |
| Lind (ind, u) -> |
| 1558 |
let prefix = env.env_mind_prefix (fst ind) in |
| 1559 |
let uargs = ml_of_instance env u in |
| 1560 |
mkMLapp (MLglobal (Gind (prefix, ind))) uargs |
| 1561 |
|
| 1562 |
let mllambda_of_lambda cenv univ constpref constlazy mindpref auxdefs l t = |
| 1563 |
let env = empty_env cenv univ constpref constlazy mindpref in |
| 1564 |
let () = cenv.global_stack <- auxdefs in |
| 1565 |
let ml = ml_of_lam env l t in |
| 1566 |
let fv_rel = !(env.env_urel) in |
| 1567 |
let fv_named = !(env.env_named) in |
| 1568 |
|
| 1569 |
let get_lname (_,t) = |
| 1570 |
match t with |
| 1571 |
| MLlocal x -> x |
| 1572 |
| _ -> assert false in |
| 1573 |
let params = |
| 1574 |
List.append (List.map get_lname fv_rel) (List.map get_lname fv_named) in |
| 1575 |
if List.is_empty params then |
| 1576 |
(cenv.global_stack, ([],[]), ml) |
| 1577 |
|
| 1578 |
else |
| 1579 |
(cenv.global_stack, (fv_named, fv_rel), mkMLlam (Array.of_list params) ml) |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
let can_subst l = |
| 1586 |
match l with |
| 1587 |
| MLlocal _ | MLint _ | MLuint _ | MLglobal _ -> true |
| 1588 |
| _ -> false |
| 1589 |
|
| 1590 |
let subst s l = |
| 1591 |
if LNmap.is_empty s then l |
| 1592 |
else |
| 1593 |
let rec aux l = |
| 1594 |
match l with |
| 1595 |
| MLlocal id -> (try LNmap.find id s with Not_found -> l) |
| 1596 |
| MLglobal _ | MLint _ | MLuint _ | MLfloat _ | MLstring _ -> l |
| 1597 |
| MLprimitive (p, args) -> MLprimitive (p, Array.map aux args) |
| 1598 |
| MLlam(params,body) -> MLlam(params, aux body) |
| 1599 |
| MLletrec(defs,body) -> |
| 1600 |
let arec (f,params,body) = (f,params,aux body) in |
| 1601 |
MLletrec(Array.map arec defs, aux body) |
| 1602 |
| MLlet(id,def,body) -> MLlet(id,aux def, aux body) |
| 1603 |
| MLapp(f,args) -> MLapp(aux f, Array.map aux args) |
| 1604 |
| MLif(t,b1,b2) -> MLif(aux t, aux b1, aux b2) |
| 1605 |
| MLmatch(annot,a,accu,bs) -> |
| 1606 |
let auxb (cargs,body) = (cargs,aux body) in |
| 1607 |
MLmatch(annot,a,aux accu, Array.map auxb bs) |
| 1608 |
| MLconstruct(prefix,c,tag,args) -> MLconstruct(prefix,c,tag,Array.map aux args) |
| 1609 |
| MLsetref(s,l1) -> MLsetref(s,aux l1) |
| 1610 |
| MLsequence(l1,l2) -> MLsequence(aux l1, aux l2) |
| 1611 |
| MLarray arr -> MLarray (Array.map aux arr) |
| 1612 |
| MLisaccu (s, ind, l) -> MLisaccu (s, ind, aux l) |
| 1613 |
in |
| 1614 |
aux l |
| 1615 |
|
| 1616 |
let add_subst id v s = |
| 1617 |
match v with |
| 1618 |
| MLlocal id' when Int.equal id.luid id'.luid -> s |
| 1619 |
| _ -> LNmap.add id v s |
| 1620 |
|
| 1621 |
let subst_norm params args s = |
| 1622 |
let len = Array.length params in |
| 1623 |
assert (Int.equal (Array.length args) len && Array.for_all can_subst args); |
| 1624 |
let s = ref s in |
| 1625 |
for i = 0 to len - 1 do |
| 1626 |
s := add_subst params.(i) args.(i) !s |
| 1627 |
done; |
| 1628 |
!s |
| 1629 |
|
| 1630 |
let subst_case params args s = |
| 1631 |
let len = Array.length params in |
| 1632 |
assert (len > 0 && |
| 1633 |
Int.equal (Array.length args) len && |
| 1634 |
let r = ref true and i = ref 0 in |
| 1635 |
|
| 1636 |
while !i < len - 1 && !r do r := can_subst args.(!i); incr i done; |
| 1637 |
!r); |
| 1638 |
let s = ref s in |
| 1639 |
for i = 0 to len - 2 do |
| 1640 |
s := add_subst params.(i) args.(i) !s |
| 1641 |
done; |
| 1642 |
!s, params.(len-1), args.(len-1) |
| 1643 |
|
| 1644 |
let empty_gdef = Int.Map.empty, Int.Map.empty |
| 1645 |
let get_norm (gnorm, _) i = Int.Map.find i gnorm |
| 1646 |
let get_case (_, gcase) i = Int.Map.find i gcase |
| 1647 |
|
| 1648 |
let all_lam n bs = |
| 1649 |
let f (_, l) = |
| 1650 |
match l with |
| 1651 |
| MLlam(params, _) -> Int.equal (Array.length params) n |
| 1652 |
| _ -> false in |
| 1653 |
Array.for_all f bs |
| 1654 |
|
| 1655 |
let commutative_cut annot a accu bs args = |
| 1656 |
let mkb (c,b) = |
| 1657 |
match b with |
| 1658 |
| MLlam(params, body) -> |
| 1659 |
(c, Array.fold_left2 (fun body x v -> MLlet(x,v,body)) body params args) |
| 1660 |
| _ -> assert false in |
| 1661 |
MLmatch(annot, a, mkMLapp accu args, Array.map mkb bs) |
| 1662 |
|
| 1663 |
let optimize gdef l = |
| 1664 |
let rec optimize s l = |
| 1665 |
match l with |
| 1666 |
| MLlocal id -> (try LNmap.find id s with Not_found -> l) |
| 1667 |
| MLglobal _ | MLint _ | MLuint _ | MLfloat _ | MLstring _ -> l |
| 1668 |
| MLprimitive (p, args) -> |
| 1669 |
MLprimitive (p, Array.map (optimize s) args) |
| 1670 |
| MLlam(params,body) -> |
| 1671 |
MLlam(params, optimize s body) |
| 1672 |
| MLletrec(decls,body) -> |
| 1673 |
let opt_rec (f,params,body) = (f,params,optimize s body ) in |
| 1674 |
MLletrec(Array.map opt_rec decls, optimize s body) |
| 1675 |
| MLlet(id,def,body) -> |
| 1676 |
let def = optimize s def in |
| 1677 |
if can_subst def then optimize (add_subst id def s) body |
| 1678 |
else MLlet(id,def,optimize s body) |
| 1679 |
| MLapp(f, args) -> |
| 1680 |
let args = Array.map (optimize s) args in |
| 1681 |
begin match f with |
| 1682 |
| MLglobal (Gnorm (_,i)) -> |
| 1683 |
(try |
| 1684 |
let params,body = get_norm gdef i in |
| 1685 |
let s = subst_norm params args s in |
| 1686 |
optimize s body |
| 1687 |
with Not_found -> MLapp(optimize s f, args)) |
| 1688 |
| MLglobal (Gcase (_,i)) -> |
| 1689 |
(try |
| 1690 |
let params,body = get_case gdef i in |
| 1691 |
let s, id, arg = subst_case params args s in |
| 1692 |
if can_subst arg then optimize (add_subst id arg s) body |
| 1693 |
else MLlet(id, arg, optimize s body) |
| 1694 |
with Not_found -> MLapp(optimize s f, args)) |
| 1695 |
| _ -> |
| 1696 |
let f = optimize s f in |
| 1697 |
match f with |
| 1698 |
| MLmatch (annot,a,accu,bs) -> |
| 1699 |
if all_lam (Array.length args) bs then |
| 1700 |
commutative_cut annot a accu bs args |
| 1701 |
else MLapp(f, args) |
| 1702 |
| _ -> MLapp(f, args) |
| 1703 |
|
| 1704 |
end |
| 1705 |
| MLif(t,b1,b2) -> |
| 1706 |
|
| 1707 |
|
| 1708 |
let t = optimize s t in |
| 1709 |
let b1 = optimize s b1 in |
| 1710 |
let b2 = optimize s b2 in |
| 1711 |
begin match t, b2 with |
| 1712 |
| MLisaccu (_, _, l1), MLmatch(annot, l2, _, bs) |
| 1713 |
when eq_mllambda l1 l2 -> MLmatch(annot, l1, b1, bs) |
| 1714 |
| _, _ -> MLif(t, b1, b2) |
| 1715 |
end |
| 1716 |
| MLmatch(annot,a,accu,bs) -> |
| 1717 |
let opt_b (cargs,body) = (cargs,optimize s body) in |
| 1718 |
MLmatch(annot, optimize s a, subst s accu, Array.map opt_b bs) |
| 1719 |
| MLconstruct(prefix,c,tag,args) -> |
| 1720 |
MLconstruct(prefix,c,tag,Array.map (optimize s) args) |
| 1721 |
| MLsetref(r,l) -> MLsetref(r, optimize s l) |
| 1722 |
| MLsequence(l1,l2) -> MLsequence(optimize s l1, optimize s l2) |
| 1723 |
| MLarray arr -> MLarray (Array.map (optimize s) arr) |
| 1724 |
| MLisaccu (pf, ind, l) -> MLisaccu (pf, ind, optimize s l) |
| 1725 |
in |
| 1726 |
optimize LNmap.empty l |
| 1727 |
|
| 1728 |
let optimize_stk stk = |
| 1729 |
let add_global gdef g = |
| 1730 |
match g with |
| 1731 |
| Glet (Gnorm (_,i), body) -> |
| 1732 |
let (gnorm, gcase) = gdef in |
| 1733 |
(Int.Map.add i (decompose_MLlam body) gnorm, gcase) |
| 1734 |
| Gletcase(Gcase (_,i), params, annot,a,accu,bs) -> |
| 1735 |
let (gnorm,gcase) = gdef in |
| 1736 |
(gnorm, Int.Map.add i (params,MLmatch(annot,a,accu,bs)) gcase) |
| 1737 |
| Gletcase _ -> assert false |
| 1738 |
| _ -> gdef in |
| 1739 |
let gdef = List.fold_left add_global empty_gdef stk in |
| 1740 |
let optimize_global g = |
| 1741 |
match g with |
| 1742 |
| Glet(Gconstant (prefix, c), body) -> |
| 1743 |
Glet(Gconstant (prefix, c), optimize gdef body) |
| 1744 |
| _ -> g in |
| 1745 |
List.map optimize_global stk |
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
let string_of_id s = Unicode.ascii_of_ident (Id.to_string s) |
| 1751 |
|
| 1752 |
let string_of_dirpath = function |
| 1753 |
| [] -> "_" |
| 1754 |
| sl -> String.concat "_" (List.rev_map string_of_id sl) |
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
let string_of_dirpath s = "N"^string_of_dirpath s |
| 1759 |
|
| 1760 |
let mod_uid_of_dirpath dir = string_of_dirpath (DirPath.repr dir) |
| 1761 |
|
| 1762 |
let link_info_of_dirpath dir = |
| 1763 |
Linked (mod_uid_of_dirpath dir ^ ".") |
| 1764 |
|
| 1765 |
let string_of_name x = |
| 1766 |
match x with |
| 1767 |
| Anonymous -> "anonymous" |
| 1768 |
| Name id -> string_of_id id |
| 1769 |
|
| 1770 |
let string_of_label_def l = |
| 1771 |
match l with |
| 1772 |
| None -> "" |
| 1773 |
| Some l -> string_of_id l |
| 1774 |
|
| 1775 |
|
| 1776 |
let rec list_of_mp acc = function |
| 1777 |
| MPdot (mp,l) -> list_of_mp (string_of_id l::acc) mp |
| 1778 |
| MPfile dp -> |
| 1779 |
let dp = DirPath.repr dp in |
| 1780 |
string_of_dirpath dp :: acc |
| 1781 |
| MPbound mbid -> ("X"^string_of_id (MBId.to_id mbid))::acc |
| 1782 |
|
| 1783 |
let list_of_mp mp = list_of_mp [] mp |
| 1784 |
|
| 1785 |
let string_of_kn kn = |
| 1786 |
let (mp,l) = KerName.repr kn in |
| 1787 |
let mp = list_of_mp mp in |
| 1788 |
String.concat "_" mp ^ "_" ^ string_of_id l |
| 1789 |
|
| 1790 |
let string_of_con c = string_of_kn (Constant.user c) |
| 1791 |
let string_of_mind mind = string_of_kn (MutInd.user mind) |
| 1792 |
let string_of_ind (mind,i) = string_of_kn (MutInd.user mind) ^ "_" ^ string_of_int i |
| 1793 |
|
| 1794 |
let string_of_gname g = |
| 1795 |
match g with |
| 1796 |
| Gind (prefix, (mind, i)) -> |
| 1797 |
Format.sprintf "%sindaccu_%s_%i" prefix (string_of_mind mind) i |
| 1798 |
| Gconstant (prefix, c) -> |
| 1799 |
Format.sprintf "%sconst_%s" prefix (string_of_con c) |
| 1800 |
| Gproj (prefix, (mind, n), i) -> |
| 1801 |
Format.sprintf "%sproj_%s_%i_%i" prefix (string_of_mind mind) n i |
| 1802 |
| Gcase (l,i) -> |
| 1803 |
Format.sprintf "case_%s_%i" (string_of_label_def l) i |
| 1804 |
| Gpred (l,i) -> |
| 1805 |
Format.sprintf "pred_%s_%i" (string_of_label_def l) i |
| 1806 |
| Gfixtype (l,i) -> |
| 1807 |
Format.sprintf "fixtype_%s_%i" (string_of_label_def l) i |
| 1808 |
| Gnorm (l,i) -> |
| 1809 |
Format.sprintf "norm_%s_%i" (string_of_label_def l) i |
| 1810 |
| Ginternal s -> Format.sprintf "%s" s |
| 1811 |
| Gnormtbl (l,i) -> |
| 1812 |
Format.sprintf "normtbl_%s_%i" (string_of_label_def l) i |
| 1813 |
| Grel i -> |
| 1814 |
Format.sprintf "rel_%i" i |
| 1815 |
| Gnamed id -> |
| 1816 |
Format.sprintf "named_%s" (string_of_id id) |
| 1817 |
|
| 1818 |
let pp_gname fmt g = |
| 1819 |
Format.fprintf fmt "%s" (string_of_gname g) |
| 1820 |
|
| 1821 |
let pp_lname fmt ln = |
| 1822 |
Format.fprintf fmt "x_%s_%i" (string_of_name ln.lname) ln.luid |
| 1823 |
|
| 1824 |
let pp_ldecls fmt ids = |
| 1825 |
let len = Array.length ids in |
| 1826 |
for i = 0 to len - 1 do |
| 1827 |
Format.fprintf fmt " (%a : Nativevalues.t)" pp_lname ids.(i) |
| 1828 |
done |
| 1829 |
|
| 1830 |
let string_of_construct prefix ~constant ind tag = |
| 1831 |
let base = if constant then "Int" else "Construct" in |
| 1832 |
Format.sprintf "%s%s_%s_%i" prefix base (string_of_ind ind) tag |
| 1833 |
|
| 1834 |
let string_of_accu_construct prefix ind = |
| 1835 |
Format.sprintf "%sAccu_%s" prefix (string_of_ind ind) |
| 1836 |
|
| 1837 |
let pp_int fmt i = |
| 1838 |
if i < 0 then Format.fprintf fmt "(%i)" i else Format.fprintf fmt "%i" i |
| 1839 |
|
| 1840 |
let pp_mllam fmt l = |
| 1841 |
|
| 1842 |
let rec pp_mllam fmt l = |
| 1843 |
match l with |
| 1844 |
| MLlocal ln -> Format.fprintf fmt "@[%a@]" pp_lname ln |
| 1845 |
| MLglobal g -> Format.fprintf fmt "@[%a@]" pp_gname g |
| 1846 |
| MLprimitive (p, args) -> |
| 1847 |
Format.fprintf fmt "@[<2>%a@ %a@]" pp_primitive p (pp_args true) args |
| 1848 |
| MLlam(ids,body) -> |
| 1849 |
Format.fprintf fmt "@[(fun%a ->@ %a)@]" |
| 1850 |
pp_ldecls ids pp_mllam body |
| 1851 |
| MLletrec(defs, body) -> |
| 1852 |
Format.fprintf fmt "@[(%a@ in@\n%a)@]" pp_letrec defs |
| 1853 |
pp_mllam body |
| 1854 |
| MLlet(id,def,body) -> |
| 1855 |
Format.fprintf fmt "@[(@[let@ %a@ =@ %a@ in@]@\n%a)@]" |
| 1856 |
pp_lname id pp_mllam def pp_mllam body |
| 1857 |
| MLapp(f, args) -> |
| 1858 |
Format.fprintf fmt "@[<2>%a@ %a@]" pp_mllam f (pp_args true) args |
| 1859 |
| MLif(t,l1,l2) -> |
| 1860 |
Format.fprintf fmt "@[(if %a then@\n %a@\nelse@\n %a)@]" |
| 1861 |
pp_mllam t pp_mllam l1 pp_mllam l2 |
| 1862 |
| MLmatch (annot, c, accu_br, br) -> |
| 1863 |
let ind = annot.asw_ind in |
| 1864 |
let prefix = annot.asw_prefix in |
| 1865 |
let accu = string_of_accu_construct prefix ind in |
| 1866 |
Format.fprintf fmt |
| 1867 |
"@[begin match Obj.magic (%a) with@\n| %s _ ->@\n %a@\n%aend@]" |
| 1868 |
pp_mllam c accu pp_mllam accu_br (pp_branches prefix ind) br |
| 1869 |
|
| 1870 |
| MLconstruct(prefix,ind,tag,args) -> |
| 1871 |
Format.fprintf fmt "@[<2>(Obj.magic@ @[<2>(%s%a)@] : Nativevalues.t)@]" |
| 1872 |
(string_of_construct prefix ~constant:false ind tag) pp_cargs args |
| 1873 |
| MLint i -> pp_int fmt i |
| 1874 |
| MLuint i -> Format.fprintf fmt "(%s)" (Uint63.compile i) |
| 1875 |
| MLfloat f -> Format.fprintf fmt "(%s)" (Float64.compile f) |
| 1876 |
| MLstring s -> Format.fprintf fmt "(%s)" (Pstring.compile s) |
| 1877 |
| MLsetref (s, body) -> |
| 1878 |
Format.fprintf fmt "@[%s@ :=@\n Some (%a)@]" s pp_mllam body |
| 1879 |
| MLsequence(l1,l2) -> |
| 1880 |
Format.fprintf fmt "@[%a;@\n%a@]" pp_mllam l1 pp_mllam l2 |
| 1881 |
| MLarray arr -> |
| 1882 |
|
| 1883 |
|
| 1884 |
let len = Array.length arr in |
| 1885 |
if Int.equal len 0 then begin |
| 1886 |
Format.fprintf fmt "@[(Obj.magic [||])@]" |
| 1887 |
end else if Int.equal len 1 then begin |
| 1888 |
|
| 1889 |
Format.fprintf fmt "@[(Obj.magic (ref (%a)))@]" pp_mllam arr.(0) |
| 1890 |
end else begin |
| 1891 |
Format.fprintf fmt "@[(Obj.magic ("; |
| 1892 |
for i = 0 to len - 2 do |
| 1893 |
Format.fprintf fmt "%a,@ " pp_mllam arr.(i) |
| 1894 |
done; |
| 1895 |
pp_mllam fmt arr.(len-1); |
| 1896 |
Format.fprintf fmt "))@]" |
| 1897 |
end; |
| 1898 |
| MLisaccu (prefix, ind, c) -> |
| 1899 |
let accu = string_of_accu_construct prefix ind in |
| 1900 |
Format.fprintf fmt |
| 1901 |
"@[begin match Obj.magic (%a) with@\n| %s _ ->@\n true@\n| _ ->@\n false@\nend@]" |
| 1902 |
pp_mllam c accu |
| 1903 |
|
| 1904 |
and pp_letrec fmt defs = |
| 1905 |
let len = Array.length defs in |
| 1906 |
let pp_one_rec (fn, argsn, body) = |
| 1907 |
Format.fprintf fmt "%a%a =@\n %a" |
| 1908 |
pp_lname fn |
| 1909 |
pp_ldecls argsn pp_mllam body in |
| 1910 |
Format.fprintf fmt "@[let rec "; |
| 1911 |
pp_one_rec defs.(0); |
| 1912 |
for i = 1 to len - 1 do |
| 1913 |
Format.fprintf fmt "@\nand "; |
| 1914 |
pp_one_rec defs.(i) |
| 1915 |
done; |
| 1916 |
|
| 1917 |
and pp_blam fmt l = |
| 1918 |
match l with |
| 1919 |
| MLprimitive (_, _) | MLlam _ | MLletrec _ | MLlet _ | MLapp _ | MLif _ -> |
| 1920 |
Format.fprintf fmt "(%a)" pp_mllam l |
| 1921 |
| MLconstruct(_,_,_,args) when Array.length args > 0 -> |
| 1922 |
Format.fprintf fmt "(%a)" pp_mllam l |
| 1923 |
| _ -> pp_mllam fmt l |
| 1924 |
|
| 1925 |
and pp_args sep fmt args = |
| 1926 |
let sep = if sep then "" else "," in |
| 1927 |
let len = Array.length args in |
| 1928 |
if len > 0 then begin |
| 1929 |
Format.fprintf fmt "%a" pp_blam args.(0); |
| 1930 |
for i = 1 to len - 1 do |
| 1931 |
Format.fprintf fmt "%s@ %a" sep pp_blam args.(i) |
| 1932 |
done |
| 1933 |
end |
| 1934 |
|
| 1935 |
and pp_cargs fmt args = |
| 1936 |
let len = Array.length args in |
| 1937 |
match len with |
| 1938 |
| 0 -> () |
| 1939 |
| 1 -> Format.fprintf fmt "@ %a" pp_blam args.(0) |
| 1940 |
| _ -> Format.fprintf fmt "@ @[<2>(%a)@]" (pp_args false) args |
| 1941 |
|
| 1942 |
and pp_cparam fmt param = |
| 1943 |
match param with |
| 1944 |
| Some l -> pp_mllam fmt (MLlocal l) |
| 1945 |
| None -> Format.fprintf fmt "_" |
| 1946 |
|
| 1947 |
and pp_cparams fmt params = |
| 1948 |
let len = Array.length params in |
| 1949 |
match len with |
| 1950 |
| 0 -> () |
| 1951 |
| 1 -> Format.fprintf fmt " %a" pp_cparam params.(0) |
| 1952 |
| _ -> |
| 1953 |
let aux fmt params = |
| 1954 |
Format.fprintf fmt "%a" pp_cparam params.(0); |
| 1955 |
for i = 1 to len - 1 do |
| 1956 |
Format.fprintf fmt ",%a" pp_cparam params.(i) |
| 1957 |
done in |
| 1958 |
Format.fprintf fmt "(%a)" aux params |
| 1959 |
|
| 1960 |
and pp_branches prefix ind fmt bs = |
| 1961 |
let pp_branch (cargs,body) = |
| 1962 |
let pp_pat fmt = function |
| 1963 |
| ConstPattern i -> |
| 1964 |
Format.fprintf fmt "| %s " |
| 1965 |
(string_of_construct prefix ~constant:true ind i) |
| 1966 |
| NonConstPattern (tag,args) -> |
| 1967 |
Format.fprintf fmt "| %s%a " |
| 1968 |
(string_of_construct prefix ~constant:false ind tag) pp_cparams args in |
| 1969 |
let rec pp_pats fmt pats = |
| 1970 |
match pats with |
| 1971 |
| [] -> () |
| 1972 |
| pat::pats -> |
| 1973 |
Format.fprintf fmt "%a%a" pp_pat pat pp_pats pats |
| 1974 |
in |
| 1975 |
Format.fprintf fmt "%a ->@\n %a@\n" pp_pats cargs pp_mllam body |
| 1976 |
in |
| 1977 |
Array.iter pp_branch bs |
| 1978 |
|
| 1979 |
and pp_primitive fmt = function |
| 1980 |
| Mk_prod -> Format.fprintf fmt "mk_prod" |
| 1981 |
| Mk_sort -> Format.fprintf fmt "mk_sort_accu" |
| 1982 |
| Mk_ind -> Format.fprintf fmt "mk_ind_accu" |
| 1983 |
| Mk_const -> Format.fprintf fmt "mk_constant_accu" |
| 1984 |
| Mk_sw -> Format.fprintf fmt "mk_sw_accu" |
| 1985 |
| Mk_fix(rec_pos,start) -> |
| 1986 |
let pp_rec_pos fmt rec_pos = |
| 1987 |
Format.fprintf fmt "@[[| %i" rec_pos.(0); |
| 1988 |
for i = 1 to Array.length rec_pos - 1 do |
| 1989 |
Format.fprintf fmt ";@ %i" rec_pos.(i) |
| 1990 |
done; |
| 1991 |
Format.fprintf fmt " |]@]" in |
| 1992 |
Format.fprintf fmt "mk_fix_accu %a %i" pp_rec_pos rec_pos start |
| 1993 |
| Mk_cofix(start) -> Format.fprintf fmt "mk_cofix_accu %i" start |
| 1994 |
| Mk_rel i -> Format.fprintf fmt "mk_rel_accu %i" i |
| 1995 |
| Mk_var id -> |
| 1996 |
Format.fprintf fmt "mk_var_accu (Names.Id.of_string \"%s\")" (string_of_id id) |
| 1997 |
| Mk_proj -> Format.fprintf fmt "mk_proj_accu" |
| 1998 |
| Mk_empty_instance -> Format.fprintf fmt "UVars.Instance.empty" |
| 1999 |
| Is_int -> Format.fprintf fmt "is_int" |
| 2000 |
| Is_float -> Format.fprintf fmt "is_float" |
| 2001 |
| Is_string -> Format.fprintf fmt "is_string" |
| 2002 |
| Is_parray -> Format.fprintf fmt "is_parray" |
| 2003 |
| Cast_accu -> Format.fprintf fmt "cast_accu" |
| 2004 |
| Array_get -> Format.fprintf fmt "Array.get" |
| 2005 |
| Force_cofix -> Format.fprintf fmt "force_cofix" |
| 2006 |
| Mk_uint -> Format.fprintf fmt "mk_uint" |
| 2007 |
| Mk_float -> Format.fprintf fmt "mk_float" |
| 2008 |
| Mk_string -> Format.fprintf fmt "mk_string" |
| 2009 |
| Mk_int -> Format.fprintf fmt "mk_int" |
| 2010 |
| Val_to_int -> Format.fprintf fmt "val_to_int" |
| 2011 |
| Mk_evar -> Format.fprintf fmt "mk_evar_accu" |
| 2012 |
| MLand -> Format.fprintf fmt "(&&)" |
| 2013 |
| MLnot -> Format.fprintf fmt "not" |
| 2014 |
| MLland -> Format.fprintf fmt "(land)" |
| 2015 |
| MLmagic -> Format.fprintf fmt "Obj.magic" |
| 2016 |
| MLsubst_instance_instance -> Format.fprintf fmt "UVars.subst_instance_instance" |
| 2017 |
| MLsubst_instance_sort -> Format.fprintf fmt "UVars.subst_instance_sort" |
| 2018 |
| MLparray_of_array -> Format.fprintf fmt "parray_of_array" |
| 2019 |
| Coq_primitive (op, false) -> |
| 2020 |
Format.fprintf fmt "no_check_%s" (CPrimitives.to_string op) |
| 2021 |
| Coq_primitive (op, true) -> Format.fprintf fmt "%s" (CPrimitives.to_string op) |
| 2022 |
| Get_value -> Format.fprintf fmt "get_value" |
| 2023 |
| Get_sort -> Format.fprintf fmt "get_sort" |
| 2024 |
| Get_name -> Format.fprintf fmt "get_name" |
| 2025 |
| Get_const -> Format.fprintf fmt "get_const" |
| 2026 |
| Get_match -> Format.fprintf fmt "get_match" |
| 2027 |
| Get_ind -> Format.fprintf fmt "get_ind" |
| 2028 |
| Get_evar -> Format.fprintf fmt "get_evar" |
| 2029 |
| Get_instance -> Format.fprintf fmt "get_instance" |
| 2030 |
| Get_proj -> Format.fprintf fmt "get_proj" |
| 2031 |
| Get_symbols -> Format.fprintf fmt "get_symbols" |
| 2032 |
| Lazy -> Format.fprintf fmt "lazy" |
| 2033 |
in |
| 2034 |
Format.fprintf fmt "@[%a@]" pp_mllam l |
| 2035 |
|
| 2036 |
let pp_array fmt t = |
| 2037 |
let len = Array.length t in |
| 2038 |
Format.fprintf fmt "@[<2>[|"; |
| 2039 |
for i = 0 to len - 2 do |
| 2040 |
Format.fprintf fmt "%a;@ " pp_mllam t.(i) |
| 2041 |
done; |
| 2042 |
if len > 0 then |
| 2043 |
Format.fprintf fmt "%a" pp_mllam t.(len - 1); |
| 2044 |
Format.fprintf fmt "|]@]" |
| 2045 |
|
| 2046 |
let pp_cofix fmt (gn, s) = |
| 2047 |
let pp_dummy fmt len = |
| 2048 |
let dummy = String.concat "; " (List.make len "0") in |
| 2049 |
Format.fprintf fmt "@[(Obj.magic [|%s|] : Nativevalues.t array)@]" dummy |
| 2050 |
in |
| 2051 |
let pp_knot fmt n = |
| 2052 |
for i = 0 to n - 1 do |
| 2053 |
Format.fprintf fmt "@[<2>let () = (%a).(%i) <-@ Obj.magic @[<2>(%a)@] in@]@\n" pp_gname gn i pp_mllam s.(i) |
| 2054 |
done |
| 2055 |
in |
| 2056 |
let len = Array.length s in |
| 2057 |
Format.fprintf fmt "@[let %a = %a in@\n%a%a@]" pp_gname gn pp_dummy len pp_knot len pp_gname gn |
| 2058 |
|
| 2059 |
let type_of_global gn c = match gn with |
| 2060 |
| Ginternal "symbols_tbl" -> "" |
| 2061 |
| _ -> match c with |
| 2062 |
| MLprimitive (Lazy, _) -> " : Nativevalues.t Lazy.t" |
| 2063 |
| MLlam ([|_|], MLprimitive (Lazy, _)) -> " : Nativevalues.t -> Nativevalues.t Lazy.t" |
| 2064 |
| MLprimitive ((Mk_ind | Mk_const), [|_|]) -> " : UVars.Instance.t -> Nativevalues.t" |
| 2065 |
| MLsetref (_,_) -> " : unit" |
| 2066 |
| _ -> " : Nativevalues.t" |
| 2067 |
|
| 2068 |
let pp_global fmt g = |
| 2069 |
match g with |
| 2070 |
| Glet (gn, c) -> |
| 2071 |
Format.fprintf fmt "@[let %a%s = let Refl = Nativevalues.t_eq in@\n %a@]@\n@." pp_gname gn |
| 2072 |
(type_of_global gn c) |
| 2073 |
pp_mllam c |
| 2074 |
| Gopen s -> |
| 2075 |
Format.fprintf fmt "@[open %s@]@." s |
| 2076 |
| Gtype (ind, lar) -> |
| 2077 |
let rec aux s arity = |
| 2078 |
if Int.equal arity 0 then s else aux (s^" * Nativevalues.t") (arity-1) in |
| 2079 |
let pp_const_sig fmt (tag,arity) = |
| 2080 |
if arity > 0 then |
| 2081 |
let sig_str = aux "of Nativevalues.t" (arity-1) in |
| 2082 |
let cstr = string_of_construct "" ~constant:false ind tag in |
| 2083 |
Format.fprintf fmt " | %s %s@\n" cstr sig_str |
| 2084 |
else |
| 2085 |
let sig_str = if arity > 0 then aux "of Nativevalues.t" (arity-1) else "" in |
| 2086 |
let cstr = string_of_construct "" ~constant:true ind tag in |
| 2087 |
Format.fprintf fmt " | %s %s@\n" cstr sig_str |
| 2088 |
in |
| 2089 |
let pp_const_sigs fmt lar = |
| 2090 |
Format.fprintf fmt " | %s of Nativevalues.t@\n" (string_of_accu_construct "" ind); |
| 2091 |
Array.iter (pp_const_sig fmt) lar |
| 2092 |
in |
| 2093 |
Format.fprintf fmt "@[type ind_%s =@\n%a@]@\n@." (string_of_ind ind) pp_const_sigs lar |
| 2094 |
| Gtblfixtype (g, params, t) -> |
| 2095 |
Format.fprintf fmt "@[let %a %a : Nativevalues.t array = let Refl = Nativevalues.t_eq in@\n %a@]@\n@." pp_gname g |
| 2096 |
pp_ldecls params pp_array t |
| 2097 |
| Gtblnorm (g, params, t) -> |
| 2098 |
Format.fprintf fmt "@[let %a %a : Nativevalues.t array = let Refl = Nativevalues.t_eq in@\n %a@]@\n@." pp_gname g |
| 2099 |
pp_ldecls params pp_array t |
| 2100 |
| Gtblcofix (g, params, s) -> |
| 2101 |
Format.fprintf fmt "@[let %a%a : Nativevalues.t array = let Refl = Nativevalues.t_eq in@\n %a@]@\n@." pp_gname g |
| 2102 |
pp_ldecls params pp_cofix (g, s); |
| 2103 |
| Gletcase(gn,params,annot,a,accu,bs) -> |
| 2104 |
Format.fprintf fmt "@[(* Hash = %i *)@\nlet rec %a %a : Nativevalues.t = let Refl = Nativevalues.t_eq in@\n %a@]@\n@." |
| 2105 |
(hash_global g) |
| 2106 |
pp_gname gn pp_ldecls params |
| 2107 |
pp_mllam (MLmatch(annot,a,accu,bs)) |
| 2108 |
| Gcomment s -> |
| 2109 |
Format.fprintf fmt "@[(* %s *)@]@." s |
| 2110 |
|
| 2111 |
|
| 2112 |
let rec compile_with_fv ?(wrap = fun t -> t) cenv env sigma univ auxdefs l t = |
| 2113 |
let const_prefix c = get_const_prefix env c in |
| 2114 |
let const_lazy = get_const_lazy env in |
| 2115 |
let mind_prefix c = get_mind_prefix env c in |
| 2116 |
let (auxdefs,(fv_named,fv_rel),ml) = mllambda_of_lambda cenv univ const_prefix const_lazy mind_prefix auxdefs l t in |
| 2117 |
let ml = wrap ml in |
| 2118 |
if List.is_empty fv_named && List.is_empty fv_rel then (auxdefs,ml) |
| 2119 |
else apply_fv cenv env sigma univ (fv_named,fv_rel) auxdefs ml |
| 2120 |
|
| 2121 |
and apply_fv cenv env sigma univ (fv_named,fv_rel) auxdefs ml = |
| 2122 |
let get_rel_val (n,_) auxdefs = |
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
compile_rel cenv env sigma univ auxdefs n |
| 2128 |
|
| 2129 |
in |
| 2130 |
let get_named_val (id,_) auxdefs = |
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
compile_named cenv env sigma univ auxdefs id |
| 2136 |
|
| 2137 |
in |
| 2138 |
let auxdefs = List.fold_right get_rel_val fv_rel auxdefs in |
| 2139 |
let auxdefs = List.fold_right get_named_val fv_named auxdefs in |
| 2140 |
let lvl = Context.Rel.length (rel_context env) in |
| 2141 |
let fv_rel = List.map (fun (n,_) -> MLglobal (Grel (lvl-n))) fv_rel in |
| 2142 |
let fv_named = List.map (fun (id,_) -> MLglobal (Gnamed id)) fv_named in |
| 2143 |
let aux_name = fresh_lname cenv Anonymous in |
| 2144 |
auxdefs, MLlet(aux_name, ml, mkMLapp (MLlocal aux_name) (Array.of_list (fv_rel@fv_named))) |
| 2145 |
|
| 2146 |
and compile_rel cenv env sigma univ auxdefs n = |
| 2147 |
let open Context.Rel.Declaration in |
| 2148 |
let decl = lookup_rel n env in |
| 2149 |
let n = List.length (rel_context env) - n in |
| 2150 |
match decl with |
| 2151 |
| LocalDef (_,t,_) -> |
| 2152 |
let code = lambda_of_constr env sigma t in |
| 2153 |
let auxdefs,code = compile_with_fv cenv env sigma univ auxdefs None code in |
| 2154 |
Glet(Grel n, code)::auxdefs |
| 2155 |
| LocalAssum _ -> |
| 2156 |
Glet(Grel n, MLprimitive (Mk_rel n, [||]))::auxdefs |
| 2157 |
|
| 2158 |
and compile_named cenv env sigma univ auxdefs id = |
| 2159 |
let open Context.Named.Declaration in |
| 2160 |
match lookup_named id env with |
| 2161 |
| LocalDef (_,t,_) -> |
| 2162 |
let code = lambda_of_constr env sigma t in |
| 2163 |
let auxdefs,code = compile_with_fv cenv env sigma univ auxdefs None code in |
| 2164 |
Glet(Gnamed id, code)::auxdefs |
| 2165 |
| LocalAssum _ -> |
| 2166 |
Glet(Gnamed id, MLprimitive (Mk_var id, [||]))::auxdefs |
| 2167 |
|
| 2168 |
let compile_constant cenv env sigma con cb = |
| 2169 |
let no_univs = UVars.AbstractContext.is_constant (Declareops.constant_polymorphic_context cb) in |
| 2170 |
begin match cb.const_body with |
| 2171 |
| Def t -> |
| 2172 |
let code = lambda_of_constr env sigma t in |
| 2173 |
debug_native_compiler (fun () -> Pp.str "Generated lambda code"); |
| 2174 |
let is_lazy = is_lazy_constant env cb in |
| 2175 |
let wrap t = if is_lazy then MLprimitive (Lazy, [|t|]) else t in |
| 2176 |
let l = Constant.label con in |
| 2177 |
let auxdefs,code = |
| 2178 |
if no_univs then |
| 2179 |
compile_with_fv ~wrap cenv env sigma (ULocal None) [] (Some l) code |
| 2180 |
else |
| 2181 |
let univ = fresh_univ cenv in |
| 2182 |
let (auxdefs,code) = compile_with_fv ~wrap cenv env sigma (ULocal (Some univ)) [] (Some l) code in |
| 2183 |
(auxdefs,mkMLlam [|univ|] code) |
| 2184 |
in |
| 2185 |
debug_native_compiler (fun () -> Pp.str "Generated mllambda code"); |
| 2186 |
let code = |
| 2187 |
optimize_stk (Glet(Gconstant ("", con),code)::auxdefs) |
| 2188 |
in |
| 2189 |
debug_native_compiler (fun () -> Pp.str "Optimized mllambda code"); |
| 2190 |
code |
| 2191 |
| _ -> |
| 2192 |
let i = push_symbol cenv (SymbConst con) in |
| 2193 |
let args = |
| 2194 |
if no_univs then [|get_const_code i; ml_empty_instance|] |
| 2195 |
else [|get_const_code i|] |
| 2196 |
in |
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
[Glet(Gconstant ("", con), MLprimitive (Mk_const, args))] |
| 2201 |
end |
| 2202 |
|
| 2203 |
module StringOrd = struct type t = string let compare = String.compare end |
| 2204 |
module StringSet = Set.Make(StringOrd) |
| 2205 |
|
| 2206 |
let loaded_native_files = ref StringSet.empty |
| 2207 |
|
| 2208 |
let is_loaded_native_file s = StringSet.mem s !loaded_native_files |
| 2209 |
|
| 2210 |
let register_native_file s = |
| 2211 |
loaded_native_files := StringSet.add s !loaded_native_files |
| 2212 |
|
| 2213 |
let is_code_loaded name = |
| 2214 |
match !name with |
| 2215 |
| NotLinked -> false |
| 2216 |
| Linked s -> |
| 2217 |
if is_loaded_native_file s then true |
| 2218 |
else (name := NotLinked; false) |
| 2219 |
|
| 2220 |
let compile_mind cenv mb mind stack = |
| 2221 |
let u = Declareops.inductive_polymorphic_context mb in |
| 2222 |
|
| 2223 |
let f i stack ob = |
| 2224 |
let ind = (mind, i) in |
| 2225 |
let gtype = Gtype(ind, ob.mind_reloc_tbl) in |
| 2226 |
let j = push_symbol cenv (SymbInd ind) in |
| 2227 |
let name = Gind ("", ind) in |
| 2228 |
let accu = |
| 2229 |
let args = |
| 2230 |
if UVars.AbstractContext.is_constant u then |
| 2231 |
[|get_ind_code j; ml_empty_instance|] |
| 2232 |
else [|get_ind_code j|] |
| 2233 |
in |
| 2234 |
|
| 2235 |
Glet(name, MLprimitive (Mk_ind, args)) |
| 2236 |
in |
| 2237 |
let add_proj proj_arg acc _pb = |
| 2238 |
let tbl = ob.mind_reloc_tbl in |
| 2239 |
|
| 2240 |
let asw = { asw_ind = ind; asw_prefix = ""; |
| 2241 |
asw_reloc = tbl } in |
| 2242 |
let c_uid = fresh_lname cenv Anonymous in |
| 2243 |
let cf_uid = fresh_lname cenv Anonymous in |
| 2244 |
let tag, arity = tbl.(0) in |
| 2245 |
assert (arity > 0); |
| 2246 |
let ci_uid = fresh_lname cenv Anonymous in |
| 2247 |
let cargs = Array.init arity |
| 2248 |
(fun i -> if Int.equal i proj_arg then Some ci_uid else None) |
| 2249 |
in |
| 2250 |
let i = push_symbol cenv (SymbProj (ind, proj_arg)) in |
| 2251 |
let accu = MLprimitive (Cast_accu, [|MLlocal cf_uid|]) in |
| 2252 |
let accu_br = MLprimitive (Mk_proj, [|get_proj_code i;accu|]) in |
| 2253 |
let code = MLmatch(asw,MLlocal cf_uid,accu_br,[|[NonConstPattern (tag,cargs)],MLlocal ci_uid|]) in |
| 2254 |
let force_c = |
| 2255 |
if mb.mind_finite <> CoFinite |
| 2256 |
then MLlocal c_uid |
| 2257 |
else mkForceCofix cenv "" ind (MLlocal c_uid) |
| 2258 |
in |
| 2259 |
let code = MLlet(cf_uid, force_c, code) in |
| 2260 |
let gn = Gproj ("", ind, proj_arg) in |
| 2261 |
Glet (gn, mkMLlam [|c_uid|] code) :: acc |
| 2262 |
in |
| 2263 |
let projs = match ob.mind_record with |
| 2264 |
| NotRecord | FakeRecord -> [] |
| 2265 |
| PrimRecord { tys ; _ } -> |
| 2266 |
Array.fold_left_i add_proj [] tys |
| 2267 |
in |
| 2268 |
projs @ gtype :: accu :: stack |
| 2269 |
in |
| 2270 |
Array.fold_left_i f stack mb.mind_packets |
| 2271 |
|
| 2272 |
type code_location_update = { |
| 2273 |
upd_info : link_info ref; |
| 2274 |
upd_prefix : string; |
| 2275 |
} |
| 2276 |
|
| 2277 |
type code_location_updates = |
| 2278 |
code_location_update Mindmap_env.t * code_location_update Cmap_env.t |
| 2279 |
|
| 2280 |
type linkable_code = global list * symbols * code_location_updates |
| 2281 |
|
| 2282 |
let empty_updates = Mindmap_env.empty, Cmap_env.empty |
| 2283 |
|
| 2284 |
let compile_mind_deps cenv env prefix |
| 2285 |
(comp_stack, (mind_updates, const_updates) as init) mind = |
| 2286 |
let mib,nameref,_ = lookup_mind_key mind env in |
| 2287 |
if is_code_loaded nameref |
| 2288 |
|| Mindmap_env.mem mind mind_updates |
| 2289 |
then init |
| 2290 |
else |
| 2291 |
let comp_stack = |
| 2292 |
compile_mind cenv mib mind comp_stack |
| 2293 |
in |
| 2294 |
let upd = { |
| 2295 |
upd_info = nameref; |
| 2296 |
upd_prefix = prefix; |
| 2297 |
} in |
| 2298 |
let mind_updates = Mindmap_env.add mind upd mind_updates in |
| 2299 |
(comp_stack, (mind_updates, const_updates)) |
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
let compile_deps cenv env sigma prefix init t = |
| 2304 |
let rec aux env lvl init t = |
| 2305 |
match kind t with |
| 2306 |
| Ind ((mind,_),_u) -> compile_mind_deps cenv env prefix init mind |
| 2307 |
| Const (c, _u) -> |
| 2308 |
let c, _ = get_alias env sigma c in |
| 2309 |
let cb,(nameref,_),_ = lookup_constant_key c env in |
| 2310 |
let (_, (_, const_updates)) = init in |
| 2311 |
if is_code_loaded nameref |
| 2312 |
|| (Cmap_env.mem c const_updates) |
| 2313 |
then init |
| 2314 |
else |
| 2315 |
let comp_stack, (mind_updates, const_updates) = |
| 2316 |
match cb.const_body with |
| 2317 |
| Def t -> |
| 2318 |
aux env lvl init t |
| 2319 |
| _ -> init |
| 2320 |
in |
| 2321 |
let code = compile_constant cenv env sigma c cb in |
| 2322 |
let upd = { |
| 2323 |
upd_info = nameref; |
| 2324 |
upd_prefix = prefix; |
| 2325 |
} in |
| 2326 |
let comp_stack = code@comp_stack in |
| 2327 |
let const_updates = Cmap_env.add c upd const_updates in |
| 2328 |
comp_stack, (mind_updates, const_updates) |
| 2329 |
| Construct (((mind,_),_),_u) -> compile_mind_deps cenv env prefix init mind |
| 2330 |
| Proj (p,_,c) -> |
| 2331 |
let init = compile_mind_deps cenv env prefix init (Projection.mind p) in |
| 2332 |
aux env lvl init c |
| 2333 |
| Case (ci, _u, _pms, _p, _iv, _c, _ac) -> |
| 2334 |
let mind = fst ci.ci_ind in |
| 2335 |
let init = compile_mind_deps cenv env prefix init mind in |
| 2336 |
fold_constr_with_binders succ (aux env) lvl init t |
| 2337 |
| Var id -> |
| 2338 |
let open Context.Named.Declaration in |
| 2339 |
begin match lookup_named id env with |
| 2340 |
| LocalDef (_,t,_) -> |
| 2341 |
aux env lvl init t |
| 2342 |
| _ -> init |
| 2343 |
end |
| 2344 |
| Rel n when n > lvl -> |
| 2345 |
let open Context.Rel.Declaration in |
| 2346 |
let decl = lookup_rel n env in |
| 2347 |
let env = env_of_rel n env in |
| 2348 |
begin match decl with |
| 2349 |
| LocalDef (_,t,_) -> |
| 2350 |
aux env lvl init t |
| 2351 |
| LocalAssum _ -> init |
| 2352 |
end |
| 2353 |
| _ -> fold_constr_with_binders succ (aux env) lvl init t |
| 2354 |
in |
| 2355 |
aux env 0 init t |
| 2356 |
|
| 2357 |
let compile_constant_field cenv env con acc cb = |
| 2358 |
let gl = compile_constant cenv env (empty_evars env) con cb in |
| 2359 |
gl@acc |
| 2360 |
|
| 2361 |
let compile_mind_field cenv mp l acc mb = |
| 2362 |
let mind = MutInd.make2 mp l in |
| 2363 |
compile_mind cenv mb mind acc |
| 2364 |
|
| 2365 |
let warn_native_rules = |
| 2366 |
CWarnings.create ~name:"native-rewrite-rules" |
| 2367 |
(fun lbl -> Pp.(str "Cannot translate the following rewrite rules: " ++ Id.print lbl)) |
| 2368 |
|
| 2369 |
let compile_rewrite_rules _env lbl acc rrb = |
| 2370 |
warn_native_rules lbl; |
| 2371 |
ignore rrb; |
| 2372 |
acc |
| 2373 |
|
| 2374 |
let mk_open s = Gopen s |
| 2375 |
|
| 2376 |
let mk_internal_let s code = |
| 2377 |
Glet(Ginternal s, code) |
| 2378 |
|
| 2379 |
|
| 2380 |
let mk_conv_code env sigma prefix t1 t2 = |
| 2381 |
let cenv = make_cenv () in |
| 2382 |
let gl, (mind_updates, const_updates) = |
| 2383 |
let init = ([], empty_updates) in |
| 2384 |
compile_deps cenv env sigma prefix init t1 |
| 2385 |
in |
| 2386 |
let gl, (mind_updates, const_updates) = |
| 2387 |
let init = (gl, (mind_updates, const_updates)) in |
| 2388 |
compile_deps cenv env sigma prefix init t2 |
| 2389 |
in |
| 2390 |
let code1 = lambda_of_constr env sigma t1 in |
| 2391 |
let code2 = lambda_of_constr env sigma t2 in |
| 2392 |
let (gl,code1) = compile_with_fv cenv env sigma UGlobal gl None code1 in |
| 2393 |
let (gl,code2) = compile_with_fv cenv env sigma UGlobal gl None code2 in |
| 2394 |
let t1 = mk_internal_let "t1" code1 in |
| 2395 |
let t2 = mk_internal_let "t2" code2 in |
| 2396 |
let g1 = MLglobal (Ginternal "t1") in |
| 2397 |
let g2 = MLglobal (Ginternal "t2") in |
| 2398 |
let setref1 = Glet(Ginternal "_", MLsetref("rt1",g1)) in |
| 2399 |
let setref2 = Glet(Ginternal "_", MLsetref("rt2",g2)) in |
| 2400 |
let gl = List.rev (setref2 :: setref1 :: t2 :: t1 :: gl) in |
| 2401 |
let header = Glet(Ginternal "symbols_tbl", |
| 2402 |
MLprimitive (Get_symbols, |
| 2403 |
[|MLglobal (Ginternal "()")|])) in |
| 2404 |
let symbols = get_cenv_symbols cenv in |
| 2405 |
header::gl, symbols, (mind_updates, const_updates) |
| 2406 |
|
| 2407 |
let mk_norm_code env sigma prefix t = |
| 2408 |
let cenv = make_cenv () in |
| 2409 |
let gl, (mind_updates, const_updates) = |
| 2410 |
let init = ([], empty_updates) in |
| 2411 |
compile_deps cenv env sigma prefix init t |
| 2412 |
in |
| 2413 |
let code = lambda_of_constr env sigma t in |
| 2414 |
let (gl,code) = compile_with_fv cenv env sigma UGlobal gl None code in |
| 2415 |
let t1 = mk_internal_let "t1" code in |
| 2416 |
let g1 = MLglobal (Ginternal "t1") in |
| 2417 |
let setref = Glet(Ginternal "_", MLsetref("rt1",g1)) in |
| 2418 |
let gl = List.rev (setref :: t1 :: gl) in |
| 2419 |
let header = Glet(Ginternal "symbols_tbl", |
| 2420 |
MLprimitive (Get_symbols, |
| 2421 |
[|MLglobal (Ginternal "()")|])) in |
| 2422 |
let symbols = get_cenv_symbols cenv in |
| 2423 |
header::gl, symbols, (mind_updates, const_updates) |
| 2424 |
|
| 2425 |
let mk_library_header (symbols : Nativevalues.symbols) = |
| 2426 |
let symbols = Format.sprintf "(str_decode \"%s\")" (str_encode symbols) in |
| 2427 |
[Glet(Ginternal "symbols_tbl", MLglobal (Ginternal symbols))] |
| 2428 |
|
| 2429 |
let update_location r = |
| 2430 |
r.upd_info := Linked r.upd_prefix |
| 2431 |
|
| 2432 |
let update_locations (ind_updates,const_updates) = |
| 2433 |
Mindmap_env.iter (fun _ -> update_location) ind_updates; |
| 2434 |
Cmap_env.iter (fun _ -> update_location) const_updates |
| 2435 |
|
| 2436 |
let add_header_comment mlcode s = |
| 2437 |
Gcomment s :: mlcode |
| 2438 |
|
| 2439 |
|