forked from rems-project/sail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonomorphise.ml
4279 lines (4026 loc) · 176 KB
/
monomorphise.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* Sail *)
(* *)
(* Copyright (c) 2013-2017 *)
(* Kathyrn Gray *)
(* Shaked Flur *)
(* Stephen Kell *)
(* Gabriel Kerneis *)
(* Robert Norton-Wright *)
(* Christopher Pulte *)
(* Peter Sewell *)
(* Alasdair Armstrong *)
(* Brian Campbell *)
(* Thomas Bauereiss *)
(* Anthony Fox *)
(* Jon French *)
(* Dominic Mulligan *)
(* Stephen Kell *)
(* Mark Wassell *)
(* *)
(* All rights reserved. *)
(* *)
(* This software was developed by the University of Cambridge Computer *)
(* Laboratory as part of the Rigorous Engineering of Mainstream Systems *)
(* (REMS) project, funded by EPSRC grant EP/K008528/1. *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions *)
(* are met: *)
(* 1. Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* 2. Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in *)
(* the documentation and/or other materials provided with the *)
(* distribution. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' *)
(* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *)
(* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *)
(* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR *)
(* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *)
(* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *)
(* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF *)
(* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *)
(* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, *)
(* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *)
(* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *)
(* SUCH DAMAGE. *)
(**************************************************************************)
(* Could fix list:
- Can probably trigger non-termination in the analysis or constant
propagation with carefully constructed recursive (or mutually
recursive) functions
*)
open Parse_ast
open Ast
open Ast_util
module Big_int = Nat_big_num
open Type_check
let size_set_limit = 64
let optmap v f =
match v with
| None -> None
| Some v -> Some (f v)
let kbindings_from_list = List.fold_left (fun s (v,i) -> KBindings.add v i s) KBindings.empty
let bindings_from_list = List.fold_left (fun s (v,i) -> Bindings.add v i s) Bindings.empty
(* union was introduced in 4.03.0, a bit too recently *)
let bindings_union s1 s2 =
Bindings.merge (fun _ x y -> match x,y with
| _, (Some x) -> Some x
| (Some x), _ -> Some x
| _, _ -> None) s1 s2
let kbindings_union s1 s2 =
KBindings.merge (fun _ x y -> match x,y with
| _, (Some x) -> Some x
| (Some x), _ -> Some x
| _, _ -> None) s1 s2
let subst_nexp substs nexp =
let rec s_snexp substs (Nexp_aux (ne,l) as nexp) =
let re ne = Nexp_aux (ne,l) in
let s_snexp = s_snexp substs in
match ne with
| Nexp_var (Kid_aux (_,l) as kid) ->
(try KBindings.find kid substs
with Not_found -> nexp)
| Nexp_id _
| Nexp_constant _ -> nexp
| Nexp_times (n1,n2) -> re (Nexp_times (s_snexp n1, s_snexp n2))
| Nexp_sum (n1,n2) -> re (Nexp_sum (s_snexp n1, s_snexp n2))
| Nexp_minus (n1,n2) -> re (Nexp_minus (s_snexp n1, s_snexp n2))
| Nexp_exp ne -> re (Nexp_exp (s_snexp ne))
| Nexp_neg ne -> re (Nexp_neg (s_snexp ne))
| Nexp_app (id,args) -> re (Nexp_app (id,List.map s_snexp args))
in s_snexp substs nexp
let rec subst_nc substs (NC_aux (nc,l) as n_constraint) =
let snexp nexp = subst_nexp substs nexp in
let snc nc = subst_nc substs nc in
let re nc = NC_aux (nc,l) in
match nc with
| NC_equal (n1,n2) -> re (NC_equal (snexp n1, snexp n2))
| NC_bounded_ge (n1,n2) -> re (NC_bounded_ge (snexp n1, snexp n2))
| NC_bounded_le (n1,n2) -> re (NC_bounded_le (snexp n1, snexp n2))
| NC_not_equal (n1,n2) -> re (NC_not_equal (snexp n1, snexp n2))
| NC_set (kid,is) ->
begin
match KBindings.find kid substs with
| Nexp_aux (Nexp_constant i,_) ->
if List.exists (fun j -> Big_int.equal i j) is then re NC_true else re NC_false
| nexp ->
raise (Reporting_basic.err_general l
("Unable to substitute " ^ string_of_nexp nexp ^
" into set constraint " ^ string_of_n_constraint n_constraint))
| exception Not_found -> n_constraint
end
| NC_or (nc1,nc2) -> re (NC_or (snc nc1, snc nc2))
| NC_and (nc1,nc2) -> re (NC_and (snc nc1, snc nc2))
| NC_true
| NC_false
-> n_constraint
let subst_src_typ substs t =
let rec s_styp substs ((Typ_aux (t,l)) as ty) =
let re t = Typ_aux (t,l) in
match t with
| Typ_id _
| Typ_var _
-> ty
| Typ_fn (t1,t2,e) -> re (Typ_fn (s_styp substs t1, s_styp substs t2,e))
| Typ_tup ts -> re (Typ_tup (List.map (s_styp substs) ts))
| Typ_app (id,tas) -> re (Typ_app (id,List.map (s_starg substs) tas))
| Typ_exist (kids,nc,t) ->
let substs = List.fold_left (fun sub v -> KBindings.remove v sub) substs kids in
re (Typ_exist (kids,nc,s_styp substs t))
and s_starg substs (Typ_arg_aux (ta,l) as targ) =
match ta with
| Typ_arg_nexp ne -> Typ_arg_aux (Typ_arg_nexp (subst_nexp substs ne),l)
| Typ_arg_typ t -> Typ_arg_aux (Typ_arg_typ (s_styp substs t),l)
| Typ_arg_order _ -> targ
in s_styp substs t
let make_vector_lit sz i =
let f j = if Big_int.equal (Big_int.modulus (Big_int.shift_right i (sz-j-1)) (Big_int.of_int 2)) Big_int.zero then '0' else '1' in
let s = String.init sz f in
L_aux (L_bin s,Generated Unknown)
let tabulate f n =
let rec aux acc n =
let acc' = f n::acc in
if Big_int.equal n Big_int.zero then acc' else aux acc' (Big_int.sub n (Big_int.of_int 1))
in if Big_int.equal n Big_int.zero then [] else aux [] (Big_int.sub n (Big_int.of_int 1))
let make_vectors sz =
tabulate (make_vector_lit sz) (Big_int.shift_left (Big_int.of_int 1) sz)
let pat_id_is_variable env id =
match Env.lookup_id id env with
(* Unbound is returned for both variables and constructors which take
arguments, but the latter only don't appear in a P_id *)
| Unbound
(* Shadowing of immutable locals is allowed; mutable locals and registers
are rejected by the type checker, so don't matter *)
| Local _
| Register _
-> true
| Enum _ -> false
let rec is_value (E_aux (e,(l,annot))) =
let is_constructor id =
match destruct_tannot annot with
| None ->
(Reporting_basic.print_err false true l "Monomorphisation"
("Missing type information for identifier " ^ string_of_id id);
false) (* Be conservative if we have no info *)
| Some (env,_,_) ->
Env.is_union_constructor id env ||
(match Env.lookup_id id env with
| Enum _ -> true
| Unbound | Local _ | Register _ -> false)
in
match e with
| E_id id -> is_constructor id
| E_lit _ -> true
| E_tuple es -> List.for_all is_value es
| E_app (id,es) -> is_constructor id && List.for_all is_value es
(* We add casts to undefined to keep the type information in the AST *)
| E_cast (typ,E_aux (E_lit (L_aux (L_undef,_)),_)) -> true
(* TODO: more? *)
| _ -> false
let is_pure (Effect_opt_aux (e,_)) =
match e with
| Effect_opt_pure -> true
| Effect_opt_effect (Effect_aux (Effect_set [],_)) -> true
| _ -> false
let rec list_extract f = function
| [] -> None
| h::t -> match f h with None -> list_extract f t | Some v -> Some v
let rec cross = function
| [] -> failwith "cross"
| [(x,l)] -> List.map (fun y -> [(x,y)]) l
| (x,l)::t ->
let t' = cross t in
List.concat (List.map (fun y -> List.map (fun l' -> (x,y)::l') t') l)
let rec cross' = function
| [] -> [[]]
| (h::t) ->
let t' = cross' t in
List.concat (List.map (fun x -> List.map (fun l -> x::l) t') h)
let rec cross'' = function
| [] -> [[]]
| (k,None)::t -> List.map (fun l -> (k,None)::l) (cross'' t)
| (k,Some h)::t ->
let t' = cross'' t in
List.concat (List.map (fun x -> List.map (fun l -> (k,Some x)::l) t') h)
let kidset_bigunion = function
| [] -> KidSet.empty
| h::t -> List.fold_left KidSet.union h t
let rec flatten_constraints = function
| [] -> []
| (NC_aux (NC_and (nc1,nc2),_))::t -> flatten_constraints (nc1::nc2::t)
| h::t -> h::(flatten_constraints t)
(* NB: this only looks for direct equalities with the given kid. It would be
better in principle to find the entire set of equal kids, but it isn't
necessary to deal with the fresh kids produced by the type checker while
checking P_var patterns, so we don't do it for now. *)
let equal_kids_ncs kid ncs =
let is_eq = function
| NC_aux (NC_equal (Nexp_aux (Nexp_var var1,_), Nexp_aux (Nexp_var var2,_)),_) ->
if Kid.compare kid var1 == 0 then Some var2 else
if Kid.compare kid var2 == 0 then Some var1 else
None
| _ -> None
in
let kids = Util.map_filter is_eq ncs in
List.fold_left (fun s k -> KidSet.add k s) (KidSet.singleton kid) kids
let equal_kids env kid =
let ncs = flatten_constraints (Env.get_constraints env) in
equal_kids_ncs kid ncs
(* TODO: deal with non-set constraints, intersections, etc somehow *)
let extract_set_nc l var nc =
let vars = equal_kids_ncs var [nc] in
let rec aux_or (NC_aux (nc,l)) =
match nc with
| NC_equal (Nexp_aux (Nexp_var id,_), Nexp_aux (Nexp_constant n,_))
when KidSet.mem id vars ->
Some [n]
| NC_or (nc1,nc2) ->
(match aux_or nc1, aux_or nc2 with
| Some l1, Some l2 -> Some (l1 @ l2)
| _, _ -> None)
| _ -> None
in
let rec aux (NC_aux (nc,l) as nc_full) =
let re nc = NC_aux (nc,l) in
match nc with
| NC_set (id,is) when KidSet.mem id vars -> Some (is,re NC_true)
| NC_and (nc1,nc2) ->
(match aux nc1, aux nc2 with
| None, None -> None
| None, Some (is,nc2') -> Some (is, re (NC_and (nc1,nc2')))
| Some (is,nc1'), None -> Some (is, re (NC_and (nc1',nc2)))
| Some _, Some _ ->
raise (Reporting_basic.err_general l ("Multiple set constraints for " ^ string_of_kid var)))
| NC_or _ ->
(match aux_or nc_full with
| Some is -> Some (is, re NC_true)
| None -> None)
| _ -> None
in match aux nc with
| Some is -> is
| None ->
raise (Reporting_basic.err_general l ("No set constraint for " ^ string_of_kid var ^
" in " ^ string_of_n_constraint nc))
let rec peel = function
| [], l -> ([], l)
| h1::t1, h2::t2 -> let (l1,l2) = peel (t1, t2) in ((h1,h2)::l1,l2)
| _,_ -> assert false
let rec split_insts = function
| [] -> [],[]
| (k,None)::t -> let l1,l2 = split_insts t in l1,k::l2
| (k,Some v)::t -> let l1,l2 = split_insts t in (k,v)::l1,l2
let apply_kid_insts kid_insts t =
let kid_insts, kids' = split_insts kid_insts in
let kid_insts = List.map (fun (v,i) -> (v,Nexp_aux (Nexp_constant i,Generated Unknown))) kid_insts in
let subst = kbindings_from_list kid_insts in
kids', subst_src_typ subst t
let rec inst_src_type insts (Typ_aux (ty,l) as typ) =
match ty with
| Typ_id _
| Typ_var _
-> insts,typ
| Typ_fn _ ->
raise (Reporting_basic.err_general l "Function type in constructor")
| Typ_tup ts ->
let insts,ts =
List.fold_right
(fun typ (insts,ts) -> let insts,typ = inst_src_type insts typ in insts,typ::ts)
ts (insts,[])
in insts, Typ_aux (Typ_tup ts,l)
| Typ_app (id,args) ->
let insts,ts =
List.fold_right
(fun arg (insts,args) -> let insts,arg = inst_src_typ_arg insts arg in insts,arg::args)
args (insts,[])
in insts, Typ_aux (Typ_app (id,ts),l)
| Typ_exist (kids, nc, t) ->
let kid_insts, insts' = peel (kids,insts) in
let kids', t' = apply_kid_insts kid_insts t in
(* TODO: subst in nc *)
match kids' with
| [] -> insts', t'
| _ -> insts', Typ_aux (Typ_exist (kids', nc, t'), l)
and inst_src_typ_arg insts (Typ_arg_aux (ta,l) as tyarg) =
match ta with
| Typ_arg_nexp _
| Typ_arg_order _
-> insts, tyarg
| Typ_arg_typ typ ->
let insts', typ' = inst_src_type insts typ in
insts', Typ_arg_aux (Typ_arg_typ typ',l)
let rec contains_exist (Typ_aux (ty,_)) =
match ty with
| Typ_id _
| Typ_var _
-> false
| Typ_fn (t1,t2,_) -> contains_exist t1 || contains_exist t2
| Typ_tup ts -> List.exists contains_exist ts
| Typ_app (_,args) -> List.exists contains_exist_arg args
| Typ_exist _ -> true
and contains_exist_arg (Typ_arg_aux (arg,_)) =
match arg with
| Typ_arg_nexp _
| Typ_arg_order _
-> false
| Typ_arg_typ typ -> contains_exist typ
let rec size_nvars_nexp (Nexp_aux (ne,_)) =
match ne with
| Nexp_var v -> [v]
| Nexp_id _
| Nexp_constant _
-> []
| Nexp_times (n1,n2)
| Nexp_sum (n1,n2)
| Nexp_minus (n1,n2)
-> size_nvars_nexp n1 @ size_nvars_nexp n2
| Nexp_exp n
| Nexp_neg n
-> size_nvars_nexp n
| Nexp_app (_,args) -> List.concat (List.map size_nvars_nexp args)
(* Given a type for a constructor, work out which refinements we ought to produce *)
(* TODO collision avoidance *)
let split_src_type id ty (TypQ_aux (q,ql)) =
let i = string_of_id id in
(* This was originally written for the general case, but I cut it down to the
more manageable prenex-form below *)
let rec size_nvars_ty (Typ_aux (ty,l) as typ) =
match ty with
| Typ_id _
| Typ_var _
-> (KidSet.empty,[[],typ])
| Typ_fn _ ->
raise (Reporting_basic.err_general l ("Function type in constructor " ^ i))
| Typ_tup ts ->
let (vars,tys) = List.split (List.map size_nvars_ty ts) in
let insttys = List.map (fun x -> let (insts,tys) = List.split x in
List.concat insts, Typ_aux (Typ_tup tys,l)) (cross' tys) in
(kidset_bigunion vars, insttys)
| Typ_app (Id_aux (Id "vector",_),
[Typ_arg_aux (Typ_arg_nexp sz,_);
_;Typ_arg_aux (Typ_arg_typ (Typ_aux (Typ_id (Id_aux (Id "bit",_)),_)),_)]) ->
(KidSet.of_list (size_nvars_nexp sz), [[],typ])
| Typ_app (_, tas) ->
(KidSet.empty,[[],typ]) (* We only support sizes for bitvectors mentioned explicitly, not any buried
inside another type *)
| Typ_exist (kids, nc, t) ->
let (vars,tys) = size_nvars_ty t in
let find_insts k (insts,nc) =
let inst,nc' =
if KidSet.mem k vars then
let is,nc' = extract_set_nc l k nc in
Some is,nc'
else None,nc
in (k,inst)::insts,nc'
in
let (insts,nc') = List.fold_right find_insts kids ([],nc) in
let insts = cross'' insts in
let ty_and_inst (inst0,ty) inst =
let kids, ty = apply_kid_insts inst ty in
let ty =
(* Typ_exist is not allowed an empty list of kids *)
match kids with
| [] -> ty
| _ -> Typ_aux (Typ_exist (kids, nc', ty),l)
in inst@inst0, ty
in
let tys = List.concat (List.map (fun instty -> List.map (ty_and_inst instty) insts) tys) in
let free = List.fold_left (fun vars k -> KidSet.remove k vars) vars kids in
(free,tys)
in
(* Only single-variable prenex-form for now *)
let size_nvars_ty (Typ_aux (ty,l) as typ) =
match ty with
| Typ_exist (kids,_,t) ->
begin
match snd (size_nvars_ty typ) with
| [] -> []
| tys ->
(* One level of tuple type is stripped off by the type checker, so
add another here *)
let tys =
List.map (fun (x,ty) ->
x, match ty with
| Typ_aux (Typ_tup _,_) -> Typ_aux (Typ_tup [ty],Unknown)
| _ -> ty) tys in
if contains_exist t then
raise (Reporting_basic.err_general l
"Only prenex types in unions are supported by monomorphisation")
else if List.length kids > 1 then
raise (Reporting_basic.err_general l
"Only single-variable existential types in unions are currently supported by monomorphisation")
else tys
end
| _ -> []
in
(* TODO: reject universally quantification or monomorphise it *)
let variants = size_nvars_ty ty in
match variants with
| [] -> None
| sample::__ ->
let () = if List.length variants > size_set_limit then
raise (Reporting_basic.err_general ql
(string_of_int (List.length variants) ^ "variants for constructor " ^ i ^
"bigger than limit " ^ string_of_int size_set_limit)) else ()
in
let wrap = match id with
| Id_aux (Id i,l) -> (fun f -> Id_aux (Id (f i),Generated l))
| Id_aux (DeIid i,l) -> (fun f -> Id_aux (DeIid (f i),l))
in
let name_seg = function
| (_,None) -> ""
| (k,Some i) -> string_of_kid k ^ Big_int.to_string i
in
let name l i = String.concat "_" (i::(List.map name_seg l)) in
Some (List.map (fun (l,ty) -> (l, wrap (name l),ty)) variants)
let reduce_nexp subst ne =
let rec eval (Nexp_aux (ne,_) as nexp) =
match ne with
| Nexp_constant i -> i
| Nexp_sum (n1,n2) -> Big_int.add (eval n1) (eval n2)
| Nexp_minus (n1,n2) -> Big_int.sub (eval n1) (eval n2)
| Nexp_times (n1,n2) -> Big_int.mul (eval n1) (eval n2)
| Nexp_exp n -> Big_int.shift_left (eval n) 1
| Nexp_neg n -> Big_int.negate (eval n)
| _ ->
raise (Reporting_basic.err_general Unknown ("Couldn't turn nexp " ^
string_of_nexp nexp ^ " into concrete value"))
in eval ne
let typ_of_args args =
match args with
| [(E_aux (E_tuple args, (_, tannot)) as exp)] ->
begin match destruct_tannot tannot with
| Some (_,Typ_aux (Typ_exist _,_),_) ->
let tys = List.map Type_check.typ_of args in
Typ_aux (Typ_tup tys,Unknown)
| _ -> Type_check.typ_of exp
end
| [exp] ->
Type_check.typ_of exp
| _ ->
let tys = List.map Type_check.typ_of args in
Typ_aux (Typ_tup tys,Unknown)
(* Check to see if we need to monomorphise a use of a constructor. Currently
assumes that bitvector sizes are always given as a variable; don't yet handle
more general cases (e.g., 8 * var) *)
let refine_constructor refinements l env id args =
match List.find (fun (id',_) -> Id.compare id id' = 0) refinements with
| (_,irefinements) -> begin
let (_,constr_ty) = Env.get_val_spec id env in
match constr_ty with
| Typ_aux (Typ_fn (constr_ty,_,_),_) -> begin
let arg_ty = typ_of_args args in
match Type_check.destruct_exist env constr_ty with
| None -> None
| Some (kids,nc,constr_ty) ->
let (bindings,_,_) = Type_check.unify l env constr_ty arg_ty in
let find_kid kid = try Some (KBindings.find kid bindings) with Not_found -> None in
let bindings = List.map find_kid kids in
let matches_refinement (mapping,_,_) =
List.for_all2
(fun v (_,w) ->
match v,w with
| _,None -> true
| Some (U_nexp (Nexp_aux (Nexp_constant n, _))),Some m -> Big_int.equal n m
| _,_ -> false) bindings mapping
in
match List.find matches_refinement irefinements with
| (_,new_id,_) -> Some (E_app (new_id,args))
| exception Not_found ->
(Reporting_basic.print_err false true l "Monomorphisation"
("Unable to refine constructor " ^ string_of_id id);
None)
end
| _ -> None
end
| exception Not_found -> None
(* Substitute found nexps for variables in an expression, and rename constructors to reflect
specialisation *)
(* TODO: kid shadowing *)
let nexp_subst_fns substs =
let s_t t = subst_src_typ substs t in
(* let s_typschm (TypSchm_aux (TypSchm_ts (q,t),l)) = TypSchm_aux (TypSchm_ts (q,s_t t),l) in
hopefully don't need this anyway *)(*
let s_typschm tsh = tsh in*)
let s_tannot tannot =
match destruct_tannot tannot with
| None -> empty_tannot
| Some (env,t,eff) -> mk_tannot env (s_t t) eff (* TODO: what about env? *)
in
let rec s_pat (P_aux (p,(l,annot))) =
let re p = P_aux (p,(l,s_tannot annot)) in
match p with
| P_lit _ | P_wild | P_id _ -> re p
| P_or (p1, p2) -> re (P_or (s_pat p1, s_pat p2))
| P_not (p) -> re (P_not (s_pat p))
| P_var (p',tpat) -> re (P_var (s_pat p',tpat))
| P_as (p',id) -> re (P_as (s_pat p', id))
| P_typ (ty,p') -> re (P_typ (s_t ty,s_pat p'))
| P_app (id,ps) -> re (P_app (id, List.map s_pat ps))
| P_record (fps,flag) -> re (P_record (List.map s_fpat fps, flag))
| P_vector ps -> re (P_vector (List.map s_pat ps))
| P_vector_concat ps -> re (P_vector_concat (List.map s_pat ps))
| P_tup ps -> re (P_tup (List.map s_pat ps))
| P_list ps -> re (P_list (List.map s_pat ps))
| P_cons (p1,p2) -> re (P_cons (s_pat p1, s_pat p2))
and s_fpat (FP_aux (FP_Fpat (id, p), (l,annot))) =
FP_aux (FP_Fpat (id, s_pat p), (l,s_tannot annot))
in
let rec s_exp (E_aux (e,(l,annot))) =
let re e = E_aux (e,(l,s_tannot annot)) in
match e with
| E_block es -> re (E_block (List.map s_exp es))
| E_nondet es -> re (E_nondet (List.map s_exp es))
| E_id _
| E_ref _
| E_lit _
| E_internal_value _
-> re e
| E_sizeof ne -> begin
let ne' = subst_nexp substs ne in
match ne' with
| Nexp_aux (Nexp_constant i,l) -> re (E_lit (L_aux (L_num i,l)))
| _ -> re (E_sizeof ne')
end
| E_constraint nc -> re (E_constraint (subst_nc substs nc))
| E_cast (t,e') -> re (E_cast (s_t t, s_exp e'))
| E_app (id,es) -> re (E_app (id, List.map s_exp es))
| E_app_infix (e1,id,e2) -> re (E_app_infix (s_exp e1,id,s_exp e2))
| E_tuple es -> re (E_tuple (List.map s_exp es))
| E_if (e1,e2,e3) -> re (E_if (s_exp e1, s_exp e2, s_exp e3))
| E_for (id,e1,e2,e3,ord,e4) -> re (E_for (id,s_exp e1,s_exp e2,s_exp e3,ord,s_exp e4))
| E_loop (loop,e1,e2) -> re (E_loop (loop,s_exp e1,s_exp e2))
| E_vector es -> re (E_vector (List.map s_exp es))
| E_vector_access (e1,e2) -> re (E_vector_access (s_exp e1,s_exp e2))
| E_vector_subrange (e1,e2,e3) -> re (E_vector_subrange (s_exp e1,s_exp e2,s_exp e3))
| E_vector_update (e1,e2,e3) -> re (E_vector_update (s_exp e1,s_exp e2,s_exp e3))
| E_vector_update_subrange (e1,e2,e3,e4) -> re (E_vector_update_subrange (s_exp e1,s_exp e2,s_exp e3,s_exp e4))
| E_vector_append (e1,e2) -> re (E_vector_append (s_exp e1,s_exp e2))
| E_list es -> re (E_list (List.map s_exp es))
| E_cons (e1,e2) -> re (E_cons (s_exp e1,s_exp e2))
| E_record fes -> re (E_record (s_fexps fes))
| E_record_update (e,fes) -> re (E_record_update (s_exp e, s_fexps fes))
| E_field (e,id) -> re (E_field (s_exp e,id))
| E_case (e,cases) -> re (E_case (s_exp e, List.map s_pexp cases))
| E_let (lb,e) -> re (E_let (s_letbind lb, s_exp e))
| E_assign (le,e) -> re (E_assign (s_lexp le, s_exp e))
| E_exit e -> re (E_exit (s_exp e))
| E_return e -> re (E_return (s_exp e))
| E_assert (e1,e2) -> re (E_assert (s_exp e1,s_exp e2))
| E_var (le,e1,e2) -> re (E_var (s_lexp le, s_exp e1, s_exp e2))
| E_internal_plet (p,e1,e2) -> re (E_internal_plet (s_pat p, s_exp e1, s_exp e2))
| E_internal_return e -> re (E_internal_return (s_exp e))
| E_throw e -> re (E_throw (s_exp e))
| E_try (e,cases) -> re (E_try (s_exp e, List.map s_pexp cases))
and s_fexps (FES_aux (FES_Fexps (fes,flag), (l,annot))) =
FES_aux (FES_Fexps (List.map s_fexp fes, flag), (l,s_tannot annot))
and s_fexp (FE_aux (FE_Fexp (id,e), (l,annot))) =
FE_aux (FE_Fexp (id,s_exp e),(l,s_tannot annot))
and s_pexp = function
| (Pat_aux (Pat_exp (p,e),(l,annot))) ->
Pat_aux (Pat_exp (s_pat p, s_exp e),(l,s_tannot annot))
| (Pat_aux (Pat_when (p,e1,e2),(l,annot))) ->
Pat_aux (Pat_when (s_pat p, s_exp e1, s_exp e2),(l,s_tannot annot))
and s_letbind (LB_aux (lb,(l,annot))) =
match lb with
| LB_val (p,e) -> LB_aux (LB_val (s_pat p,s_exp e), (l,s_tannot annot))
and s_lexp (LEXP_aux (e,(l,annot))) =
let re e = LEXP_aux (e,(l,s_tannot annot)) in
match e with
| LEXP_id _ -> re e
| LEXP_cast (typ,id) -> re (LEXP_cast (s_t typ, id))
| LEXP_memory (id,es) -> re (LEXP_memory (id,List.map s_exp es))
| LEXP_tup les -> re (LEXP_tup (List.map s_lexp les))
| LEXP_vector (le,e) -> re (LEXP_vector (s_lexp le, s_exp e))
| LEXP_vector_range (le,e1,e2) -> re (LEXP_vector_range (s_lexp le, s_exp e1, s_exp e2))
| LEXP_vector_concat les -> re (LEXP_vector_concat (List.map s_lexp les))
| LEXP_field (le,id) -> re (LEXP_field (s_lexp le, id))
| LEXP_deref e -> re (LEXP_deref (s_exp e))
in (s_pat,s_exp)
let nexp_subst_pat substs = fst (nexp_subst_fns substs)
let nexp_subst_exp substs = snd (nexp_subst_fns substs)
let bindings_from_pat p =
let rec aux_pat (P_aux (p,(l,annot))) =
let env = Type_check.env_of_annot (l, annot) in
match p with
| P_lit _
| P_wild
-> []
| P_or (p1, p2) -> aux_pat p1 @ aux_pat p2
| P_not (p) -> aux_pat p
| P_as (p,id) -> id::(aux_pat p)
| P_typ (_,p) -> aux_pat p
| P_id id ->
if pat_id_is_variable env id then [id] else []
| P_var (p,kid) -> aux_pat p
| P_vector ps
| P_vector_concat ps
| P_app (_,ps)
| P_tup ps
| P_list ps
-> List.concat (List.map aux_pat ps)
| P_record (fps,_) -> List.concat (List.map aux_fpat fps)
| P_cons (p1,p2) -> aux_pat p1 @ aux_pat p2
and aux_fpat (FP_aux (FP_Fpat (_,p), _)) = aux_pat p
in aux_pat p
let remove_bound (substs,ksubsts) pat =
let bound = bindings_from_pat pat in
List.fold_left (fun sub v -> Bindings.remove v sub) substs bound, ksubsts
(* Attempt simple pattern matches *)
let lit_match = function
| (L_zero | L_false), (L_zero | L_false) -> true
| (L_one | L_true ), (L_one | L_true ) -> true
| L_num i1, L_num i2 -> Big_int.equal i1 i2
| l1,l2 -> l1 = l2
(* There's no undefined nexp, so replace undefined sizes with a plausible size.
32 is used as a sensible default. *)
let fabricate_nexp_exist env l typ kids nc typ' =
match kids,nc,Env.expand_synonyms env typ' with
| ([kid],NC_aux (NC_set (kid',i::_),_),
Typ_aux (Typ_app (Id_aux (Id "atom",_),
[Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var kid'',_)),_)]),_))
when Kid.compare kid kid' = 0 && Kid.compare kid kid'' = 0 ->
Nexp_aux (Nexp_constant i,Unknown)
| ([kid],NC_aux (NC_true,_),
Typ_aux (Typ_app (Id_aux (Id "atom",_),
[Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var kid'',_)),_)]),_))
when Kid.compare kid kid'' = 0 ->
nint 32
| ([kid],NC_aux (NC_set (kid',i::_),_),
Typ_aux (Typ_app (Id_aux (Id "range",_),
[Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var kid'',_)),_);
Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var kid''',_)),_)]),_))
when Kid.compare kid kid' = 0 && Kid.compare kid kid'' = 0 &&
Kid.compare kid kid''' = 0 ->
Nexp_aux (Nexp_constant i,Unknown)
| ([kid],NC_aux (NC_true,_),
Typ_aux (Typ_app (Id_aux (Id "range",_),
[Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var kid'',_)),_);
Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var kid''',_)),_)]),_))
when Kid.compare kid kid'' = 0 &&
Kid.compare kid kid''' = 0 ->
nint 32
| _ -> raise (Reporting_basic.err_general l
("Undefined value at unsupported type " ^ string_of_typ typ))
let fabricate_nexp l tannot =
match destruct_tannot tannot with
| None -> nint 32
| Some (env,typ,_) ->
match Type_check.destruct_exist env typ with
| None -> nint 32
| Some (kids,nc,typ') -> fabricate_nexp_exist env l typ kids nc typ'
let atom_typ_kid kid = function
| Typ_aux (Typ_app (Id_aux (Id "atom",_),
[Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var kid',_)),_)]),_) ->
Kid.compare kid kid' = 0
| _ -> false
(* We reduce casts in a few cases, in particular to ensure that where the
type checker has added a ({'n, true. atom('n)}) ex_int(...) cast we can
fill in the 'n. For undefined we fabricate a suitable value for 'n. *)
let reduce_cast typ exp l annot =
let env = env_of_annot (l,annot) in
let typ' = Env.base_typ_of env typ in
match exp, destruct_exist env typ' with
| E_aux (E_lit (L_aux (L_num n,_)),_), Some ([kid],nc,typ'') when atom_typ_kid kid typ'' ->
let nc_env = Env.add_typ_var l kid BK_int env in
let nc_env = Env.add_constraint (nc_eq (nvar kid) (nconstant n)) nc_env in
if prove nc_env nc
then exp
else raise (Reporting_basic.err_unreachable l
("Constant propagation error: literal " ^ Big_int.to_string n ^
" does not satisfy constraint " ^ string_of_n_constraint nc))
| E_aux (E_lit (L_aux (L_undef,_)),_), Some ([kid],nc,typ'') when atom_typ_kid kid typ'' ->
let nexp = fabricate_nexp_exist env Unknown typ [kid] nc typ'' in
let newtyp = subst_src_typ (KBindings.singleton kid nexp) typ'' in
E_aux (E_cast (newtyp, exp), (Generated l,replace_typ newtyp annot))
| E_aux (E_cast (_,
(E_aux (E_lit (L_aux (L_undef,_)),_) as exp)),_),
Some ([kid],nc,typ'') when atom_typ_kid kid typ'' ->
let nexp = fabricate_nexp_exist env Unknown typ [kid] nc typ'' in
let newtyp = subst_src_typ (KBindings.singleton kid nexp) typ'' in
E_aux (E_cast (newtyp, exp), (Generated l,replace_typ newtyp annot))
| _ -> E_aux (E_cast (typ,exp),(l,annot))
(* Used for constant propagation in pattern matches *)
type 'a matchresult =
| DoesMatch of 'a
| DoesNotMatch
| GiveUp
(* Remove top-level casts from an expression. Useful when we need to look at
subexpressions to reduce something, but could break type-checking if we used
it everywhere. *)
let rec drop_casts = function
| E_aux (E_cast (_,e),_) -> drop_casts e
| exp -> exp
let int_of_str_lit = function
| L_hex hex -> Big_int.of_string ("0x" ^ hex)
| L_bin bin -> Big_int.of_string ("0b" ^ bin)
| _ -> assert false
let bits_of_lit = function
| L_bin bin -> bin
| L_hex hex -> hex_to_bin hex
| _ -> assert false
let slice_lit (L_aux (lit,ll)) i len (Ord_aux (ord,_)) =
let i = Big_int.to_int i in
let len = Big_int.to_int len in
let bin = bits_of_lit lit in
match match ord with
| Ord_inc -> Some i
| Ord_dec -> Some (String.length bin - i - len)
| Ord_var _ -> None
with
| None -> None
| Some i ->
Some (L_aux (L_bin (String.sub bin i len),Generated ll))
let concat_vec lit1 lit2 =
let bits1 = bits_of_lit lit1 in
let bits2 = bits_of_lit lit2 in
L_bin (bits1 ^ bits2)
let lit_eq (L_aux (l1,_)) (L_aux (l2,_)) =
match l1,l2 with
| (L_zero|L_false), (L_zero|L_false)
| (L_one |L_true ), (L_one |L_true)
-> Some true
| (L_hex _| L_bin _), (L_hex _|L_bin _)
-> Some (Big_int.equal (int_of_str_lit l1) (int_of_str_lit l2))
| L_undef, _ | _, L_undef -> None
| L_num i1, L_num i2 -> Some (Big_int.equal i1 i2)
| _ -> Some (l1 = l2)
let try_app (l,ann) (id,args) =
let new_l = Generated l in
let env = env_of_annot (l,ann) in
let get_overloads f = List.map string_of_id
(Env.get_overloads (Id_aux (Id f, Parse_ast.Unknown)) env @
Env.get_overloads (Id_aux (DeIid f, Parse_ast.Unknown)) env) in
let is_id f = List.mem (string_of_id id) (f :: get_overloads f) in
if is_id "==" || is_id "!=" then
match args with
| [E_aux (E_lit l1,_); E_aux (E_lit l2,_)] ->
let lit b = if b then L_true else L_false in
let lit b = lit (if is_id "==" then b else not b) in
(match lit_eq l1 l2 with
| None -> None
| Some b -> Some (E_aux (E_lit (L_aux (lit b,new_l)),(l,ann))))
| _ -> None
else if is_id "cast_bit_bool" then
match args with
| [E_aux (E_lit L_aux (L_zero,_),_)] -> Some (E_aux (E_lit (L_aux (L_false,new_l)),(l,ann)))
| [E_aux (E_lit L_aux (L_one ,_),_)] -> Some (E_aux (E_lit (L_aux (L_true ,new_l)),(l,ann)))
| _ -> None
else if is_id "UInt" || is_id "unsigned" then
match args with
| [E_aux (E_lit L_aux ((L_hex _| L_bin _) as lit,_), _)] ->
Some (E_aux (E_lit (L_aux (L_num (int_of_str_lit lit),new_l)),(l,ann)))
| _ -> None
else if is_id "slice" then
match args with
| [E_aux (E_lit (L_aux ((L_hex _| L_bin _),_) as lit), annot);
E_aux (E_lit L_aux (L_num i,_), _);
E_aux (E_lit L_aux (L_num len,_), _)] ->
(match Env.base_typ_of (env_of_annot annot) (typ_of_annot annot) with
| Typ_aux (Typ_app (_,[_;Typ_arg_aux (Typ_arg_order ord,_);_]),_) ->
(match slice_lit lit i len ord with
| Some lit' -> Some (E_aux (E_lit lit',(l,ann)))
| None -> None)
| _ -> None)
| _ -> None
else if is_id "bitvector_concat" then
match args with
| [E_aux (E_lit L_aux ((L_hex _| L_bin _) as lit1,_), _);
E_aux (E_lit L_aux ((L_hex _| L_bin _) as lit2,_), _)] ->
Some (E_aux (E_lit (L_aux (concat_vec lit1 lit2,new_l)),(l,ann)))
| _ -> None
else if is_id "shl_int" then
match args with
| [E_aux (E_lit L_aux (L_num i,_),_); E_aux (E_lit L_aux (L_num j,_),_)] ->
Some (E_aux (E_lit (L_aux (L_num (Big_int.shift_left i (Big_int.to_int j)),new_l)),(l,ann)))
| _ -> None
else if is_id "mult_atom" || is_id "mult_int" || is_id "mult_range" then
match args with
| [E_aux (E_lit L_aux (L_num i,_),_); E_aux (E_lit L_aux (L_num j,_),_)] ->
Some (E_aux (E_lit (L_aux (L_num (Big_int.mul i j),new_l)),(l,ann)))
| _ -> None
else if is_id "quotient_nat" then
match args with
| [E_aux (E_lit L_aux (L_num i,_),_); E_aux (E_lit L_aux (L_num j,_),_)] ->
Some (E_aux (E_lit (L_aux (L_num (Big_int.div i j),new_l)),(l,ann)))
| _ -> None
else if is_id "add_atom" || is_id "add_int" || is_id "add_range" then
match args with
| [E_aux (E_lit L_aux (L_num i,_),_); E_aux (E_lit L_aux (L_num j,_),_)] ->
Some (E_aux (E_lit (L_aux (L_num (Big_int.add i j),new_l)),(l,ann)))
| _ -> None
else if is_id "negate_range" then
match args with
| [E_aux (E_lit L_aux (L_num i,_),_)] ->
Some (E_aux (E_lit (L_aux (L_num (Big_int.negate i),new_l)),(l,ann)))
| _ -> None
else if is_id "ex_int" then
match args with
| [E_aux (E_lit lit,(l,_))] -> Some (E_aux (E_lit lit,(l,ann)))
| [E_aux (E_cast (_,(E_aux (E_lit (L_aux (L_undef,_)),_) as e)),(l,_))] ->
Some (reduce_cast (typ_of_annot (l,ann)) e l ann)
| _ -> None
else if is_id "vector_access" || is_id "bitvector_access" then
match args with
| [E_aux (E_lit L_aux ((L_hex _ | L_bin _) as lit,_),_);
E_aux (E_lit L_aux (L_num i,_),_)] ->
let v = int_of_str_lit lit in
let b = Big_int.bitwise_and (Big_int.shift_right v (Big_int.to_int i)) (Big_int.of_int 1) in
let lit' = if Big_int.equal b (Big_int.of_int 1) then L_one else L_zero in
Some (E_aux (E_lit (L_aux (lit',new_l)),(l,ann)))
| _ -> None
else None
let construct_lit_vector args =
let rec aux l = function
| [] -> Some (L_aux (L_bin (String.concat "" (List.rev l)),Unknown))
| E_aux (E_lit (L_aux ((L_zero | L_one) as lit,_)),_)::t ->
aux ((if lit = L_zero then "0" else "1")::l) t
| _ -> None
in aux [] args
type pat_choice = Parse_ast.l * (int * int * (id * tannot exp) list)
(* We may need to split up a pattern match if (1) we've been told to case split
on a variable by the user or analysis, or (2) we monomorphised a constructor that's used
in the pattern. *)
type split =
| NoSplit
| VarSplit of (tannot pat * (* pattern for this case *)
(id * tannot Ast.exp) list * (* substitutions for arguments *)
pat_choice list * (* optional locations of constraints/case expressions to reduce *)
(kid * nexp) list) (* substitutions for type variables *)
list
| ConstrSplit of (tannot pat * nexp KBindings.t) list
let threaded_map f state l =
let l',state' =
List.fold_left (fun (tl,state) element -> let (el',state') = f state element in (el'::tl,state'))
([],state) l
in List.rev l',state'
let isubst_minus subst subst' =
Bindings.merge (fun _ x y -> match x,y with (Some a), None -> Some a | _, _ -> None) subst subst'
let isubst_minus_set subst set =
IdSet.fold Bindings.remove set subst
let assigned_vars exp =
fst (Rewriter.fold_exp
{ (Rewriter.compute_exp_alg IdSet.empty IdSet.union) with
Rewriter.lEXP_id = (fun id -> IdSet.singleton id, LEXP_id id);
Rewriter.lEXP_cast = (fun (ty,id) -> IdSet.singleton id, LEXP_cast (ty,id)) }
exp)
let referenced_vars exp =
let open Rewriter in
fst (fold_exp
{ (compute_exp_alg IdSet.empty IdSet.union) with
e_ref = (fun id -> IdSet.singleton id, E_ref id) } exp)
let assigned_vars_in_fexps (FES_aux (FES_Fexps (fes,_), _)) =
List.fold_left
(fun vs (FE_aux (FE_Fexp (_,e),_)) -> IdSet.union vs (assigned_vars e))
IdSet.empty
fes
let assigned_vars_in_pexp (Pat_aux (p,_)) =
match p with
| Pat_exp (_,e) -> assigned_vars e
| Pat_when (p,e1,e2) -> IdSet.union (assigned_vars e1) (assigned_vars e2)
let rec assigned_vars_in_lexp (LEXP_aux (le,_)) =
match le with
| LEXP_id id
| LEXP_cast (_,id) -> IdSet.singleton id
| LEXP_tup lexps
| LEXP_vector_concat lexps ->
List.fold_left (fun vs le -> IdSet.union vs (assigned_vars_in_lexp le)) IdSet.empty lexps
| LEXP_memory (_,es) -> List.fold_left (fun vs e -> IdSet.union vs (assigned_vars e)) IdSet.empty es
| LEXP_vector (le,e) -> IdSet.union (assigned_vars_in_lexp le) (assigned_vars e)
| LEXP_vector_range (le,e1,e2) ->
IdSet.union (assigned_vars_in_lexp le) (IdSet.union (assigned_vars e1) (assigned_vars e2))
| LEXP_field (le,_) -> assigned_vars_in_lexp le
| LEXP_deref e -> assigned_vars e
(* Add a cast to undefined so that it retains its type, otherwise it can't be
substituted safely *)
let keep_undef_typ value =
match value with
| E_aux (E_lit (L_aux (L_undef,lann)),eann) ->
E_aux (E_cast (typ_of_annot eann,value),(Generated Unknown,snd eann))
| _ -> value
let freshen_id =
let counter = ref 0 in
fun id ->
let n = !counter in
let () = counter := n + 1 in
match id with
| Id_aux (Id x, l) -> Id_aux (Id (x ^ "#m" ^ string_of_int n),Generated l)
| Id_aux (DeIid x, l) -> Id_aux (DeIid (x ^ "#m" ^ string_of_int n),Generated l)
(* TODO: only freshen bindings that might be shadowed *)
let rec freshen_pat_bindings p =
let rec aux (P_aux (p,(l,annot)) as pat) =
let mkp p = P_aux (p,(Generated l, annot)) in
match p with
| P_lit _
| P_wild -> pat, []
| P_or (p1, p2) ->
let (r1, vs1) = aux p1 in
let (r2, vs2) = aux p2 in
(mkp (P_or (r1, r2)), vs1 @ vs2)
| P_not p ->
let (r, vs) = aux p in
(mkp (P_not r), vs)
| P_as (p,_) -> aux p
| P_typ (typ,p) -> let p',vs = aux p in mkp (P_typ (typ,p')),vs
| P_id id -> let id' = freshen_id id in mkp (P_id id'),[id,E_aux (E_id id',(Generated Unknown,empty_tannot))]