-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtycheck.ml
More file actions
executable file
·1345 lines (1264 loc) · 51.5 KB
/
tycheck.ml
File metadata and controls
executable file
·1345 lines (1264 loc) · 51.5 KB
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
(* Poling: Proof Of Linearizability Generator
* Poling is built on top of CAVE and shares the same license with CAVE
* See LICENSE.txt for license.
* Contact: He Zhu, Department of Computer Science, Purdue University
* Email: zhu103@purdue.edu
*)
(******************************************************************************)
(* __ ___ CAVE: Concurrent Algorithm VErifier *)
(* / /\ \ / | Copyright (c) 2010, Viktor Vafeiadis *)
(* | /--\ \ / |--- *)
(* \__ / \ \/ |___ See LICENSE.txt for license. *)
(* *)
(******************************************************************************)
open Misc
open Parsetree
open Exp
open Assertions
open Commands
open Indpreds
open Convert
(* -------------------------------------------------------------------------- *)
(** {2 Error handling} *)
(* -------------------------------------------------------------------------- *)
exception Vcgen_error of string * Location.t
let error_handler do_f =
let res =
try
do_f ()
with
| Conversion_error (s, loc) ->
print_endline ("\n" ^ Location.sprint loc ^ "Syntax error: " ^ s);
invalid_arg ("Errors found in input.")
| Vcgen_error (s, loc) ->
print_endline ("\n" ^ Location.sprint loc ^ s);
invalid_arg ("Errors found in input.")
in
res
let error (s,loc) =
raise (Vcgen_error (s, loc))
let warning (s,loc) =
print_endline ("\n" ^ Location.sprint loc ^ "Warning: " ^ s)
let argnum_error (s1,s2,l1,l2,loc) =
error (s1 ^ " has " ^ string_of_int (List.length l2) ^ s2 ^ " arguments, but "
^ string_of_int (List.length l1) ^ " are expected.", loc)
(** [empty_or_error x (s1,s2,loc)] checks that the set [x] is empty. *)
let empty_or_error x (s1,s2,loc) =
if not (IdSet.is_empty x)
then error (s1 ^ Id.to_string (IdSet.choose x) ^ s2, loc)
(* -------------------------------------------------------------------------- *)
(** {2 Set operations} *)
(* -------------------------------------------------------------------------- *)
let (++) = IdSet.union
let (--) = IdSet.diff
let (+) s x = s ++ (IdSet.singleton x)
let (-) s x = IdSet.remove x s
let set_of_list l =
List.fold_left (+) IdSet.empty l
let union_map f l =
List.fold (fun x ss -> (f x) ++ ss) l IdSet.empty
(* -------------------------------------------------------------------------- *)
(** {2 Environment} *)
(* -------------------------------------------------------------------------- *)
type typ =
| Pty_null
| Pty_int
| Pty_bool
| Pty_void
| Pty_tid
| Pty_set
| Pty_list
| Pty_item
| Pty_class of string
let string_of_ty = function
| Pty_null -> "0"
| Pty_int -> "int"
| Pty_bool -> "bool"
| Pty_void -> "void"
| Pty_tid -> "thread_id"
| Pty_set -> "set"
| Pty_list -> "list"
| Pty_item -> "@item(...)"
| Pty_class s -> s
let ty_of_string = function
| "int" -> Pty_int
| "bool" -> Pty_bool
| "void" -> Pty_void
| "thread_id" -> Pty_tid
| "set" -> Pty_set
| "list" -> Pty_list
| s -> Pty_class s
(* User-defined structures *)
type class_item =
{ cla_fields: (typ * Location.t) StringMap.t
; cla_tdesc : Predicate.t option
; cla_loc: Location.t }
type function_item =
{ fn_resty: typ
; param: (Id.t * typ * Location.t) list * (Id.t * typ * Location.t) list
; locals: (Id.t * typ * Location.t) list
; bound : IdSet.t (** locals + params *)
; mutable calls: CompSet.t StringMap.t
(** map from fid to minimum set of resources
always acquired before calling fid *)
; mutable in_parallel_with: StringSet.t
; mutable requires: CompSet.t
; mutable writes_heap: bool
; mutable modif: IdSet.t
; mutable vars: IdSet.t
; pre: cprop
; purespec: (string * Predicate.t)
; body: p_statement list
; post: cprop
; effspec: (string * Predicate.t)
; fn_loc: Location.t }
type action_item =
{ act_id: string
; act_param: (string * Location.t) list
; act_locals: IdSet.t
; act_bound: IdSet.t (** locals + params *)
; act_ctx: cprop
; act_pre: cprop
; act_post: cprop
; act_code: p_statement list
(** code to be inserted after each corresponding do...as action block *)
; act_loc: Location.t }
type resource_item =
{ owned: IdSet.t
; rvars: IdSet.t (** Free variables of the resource invariant *)
; res_locals : (Id.t * typ * Location.t) list
; res_cons : p_statement list
; res_interf : p_statement list option
; rinv : cprop option (** Resource invariant *)
; ractions: action_item StringMap.t (** Resource actions *)
; res_loc: Location.t
; res_def: Predicate.t }
type env =
{ mutable comments: string list
; classes: (string,class_item) Hashtbl.t
; mutable variables: (typ * Location.t) IdMap.t
; functions: (string,function_item) Hashtbl.t
; mutable env_funcs: string list
(** in reverse order wrt the original program *)
; resources: (component,resource_item) Hashtbl.t
; mutable env_rsrcs: component list
(** in reverse order wrt the original program *)
; mutable enable : IdSet.t * IdSet.t -> CompSet.t
}
let initial_env () =
let classes = Hashtbl.create 16 in
Hashtbl.add classes "any" {cla_fields=StringMap.empty;
cla_tdesc = None; cla_loc=Location.none};
{ comments = []
; classes = classes
; variables = IdMap.add Id.mytid (Pty_tid, Location.none)
(IdMap.add Id.tid (Pty_tid, Location.none)
IdMap.empty)
; functions = Hashtbl.create 16
; env_funcs = []
; resources = Hashtbl.create 4
; env_rsrcs = []
; enable = fun _ -> assert false}
let lookup_class env x = Hashtbl.find env.classes x
let lookup_function env x = Hashtbl.find env.functions x
let lookup_resource env x = Hashtbl.find env.resources x
let lookup_action res x = StringMap.find x res.ractions
let env_add_fun env x fi =
if Hashtbl.mem env.functions x then
error ("Function " ^ x ^ " is already defined.", fi.fn_loc)
else begin
Hashtbl.add env.functions x fi;
env.env_funcs <- x :: env.env_funcs
end
let env_add_res env x ri =
if Hashtbl.mem env.resources x then
error ("Resource " ^ string_of_component x ^ " is already defined.", ri.res_loc)
else begin
let ri_vars = ri.owned ++ ri.rvars in
empty_or_error (IdSet.filter (fun x -> not (IdMap.mem x env.variables)) ri_vars)
("Undeclared variable ", " in resource declaration.", ri.res_loc);
Hashtbl.iter
(fun y r ->
empty_or_error (IdSet.inter r.owned ri_vars)
("Variable ", " is already owned by resource " ^ string_of_component y ^ ".", ri.res_loc);
empty_or_error (IdSet.inter r.rvars ri.owned)
("Variable ", " is already used by resource " ^ string_of_component y ^ ".", ri.res_loc)
) env.resources;
Hashtbl.add env.resources x ri;
env.env_rsrcs <- x :: env.env_rsrcs
end
(* -------------------------------------------------------------------------- *)
(** {2 Type checking} *)
(* -------------------------------------------------------------------------- *)
let ty_unify = function
| (ty1,ty2) when ty1=ty2 -> Some ty1
| (Pty_null, ((Pty_int | Pty_tid | Pty_class _) as ty)) -> Some ty
| (((Pty_int | Pty_tid | Pty_class _) as ty), Pty_null) -> Some ty
| (Pty_class "any", (Pty_class _ as ty)) -> Some ty
| ((Pty_class _ as ty), Pty_class "any") -> Some ty
| (Pty_item, ((Pty_set | Pty_list) as ty)) -> Some ty
| (((Pty_set | Pty_list) as ty), Pty_item) -> Some ty
| (ty1,ty2) -> None
let expect_type loc ty1 ty2 =
match ty_unify (ty1,ty2) with
| Some _ -> ()
| None ->
error ("Type error: expecting type `" ^ string_of_ty ty1
^ "', but type `" ^ string_of_ty ty2 ^ "' found.", loc)
let type_of_var loc env x =
try fst (IdMap.find (Id.create x) env.variables)
with Not_found ->
error ("Variable " ^ x ^ " has not been declared, or is out of scope.", loc)
let lookup_class_from_type loc env ty = match ty with
| Pty_class t ->
begin
try lookup_class env t
with Not_found -> error("Reference to undeclared class " ^ t ^ ".", loc)
end
| _ -> error ("Type error: expecting a pointer type.", loc)
let rec type_of_pexp env e = match e.pexp_desc with
| Pexp_ident x -> type_of_var e.pexp_loc env x
| Pexp_num i -> if i=0 then Pty_null else Pty_int
| Pexp_bool _ -> Pty_bool
| Pexp_prefix (s,e1) ->
begin match s with
| ("+" | "-") ->
check_pexp env Pty_int e1; Pty_int
| ("!") ->
check_pexp env Pty_bool e1; Pty_bool
| _ -> error("Error: found unknown unary operator (" ^ s ^ ").", e.pexp_loc)
end
| Pexp_infix (s,e1,e2) ->
begin match s with
| "+" ->
let ty1 = type_of_pexp env e1 in
let ty2 = type_of_pexp env e2 in
begin match ty1, ty2 with
| (Pty_null | Pty_int), (Pty_int | Pty_class _ | Pty_null) -> ty2
| Pty_class _, (Pty_int | Pty_null) -> ty1
| _, _ -> error("Type Error: Wrong argument types for + operator.", e.pexp_loc)
end
| "-" ->
let ty1 = type_of_pexp env e1 in
let ty2 = type_of_pexp env e2 in
begin match ty1, ty2 with
| (Pty_null | Pty_int), (Pty_int | Pty_null) -> ty2
| Pty_class _, (Pty_int | Pty_null) -> ty1
| Pty_class x, Pty_class y when x = y -> ty1
| _, _ -> error("Type Error: Wrong argument types for - operator.", e.pexp_loc)
end
| ("*" | "/" | "%" | "^") ->
check_pexp env Pty_int e1;
check_pexp env Pty_int e2;
Pty_int
| ("==" | "!=") ->
let ty1 = type_of_pexp env e1 in
let ty2 = type_of_pexp env e2 in
begin match ty_unify (ty1,ty2) with
| None -> error ("Type error: Incompatible types in equality.", e.pexp_loc)
| Some _ -> ()
end;
Pty_bool
| ("<" | "<=" | ">" | ">=") ->
check_pexp env Pty_int e1;
check_pexp env Pty_int e2;
Pty_bool
| ("&&" | "||") ->
check_pexp env Pty_bool e1;
check_pexp env Pty_bool e2;
Pty_bool
| _ -> error("Error: found unknown binary operator (" ^ s ^ ").", e.pexp_loc)
end
| Pexp_cast (e1,ty) ->
let _ = type_of_pexp env e1 in
ty_of_string ty
| Pexp_fld (e1,s) ->
let ty = type_of_pexp env e1 in
let ci = lookup_class_from_type e.pexp_loc env ty in
begin try fst (StringMap.find (string_of_component s) ci.cla_fields)
with Not_found ->
error("Type error: expression has type " ^ string_of_ty ty
^ ", which does not have field " ^ string_of_component s ^ ".", e1.pexp_loc)
end
| Pexp_new (s,e) ->
let ty = Pty_class s in
ignore (lookup_class_from_type e.pexp_loc env ty);
check_pexp env Pty_int e;
ty
| Pexp_fun(s,el) ->
let check_one ty1 =
match el with
| [x] -> check_pexp env ty1 x
| _ -> error("Error: Function "^s^" expects one argument.", e.pexp_loc) in
let check_one_any () =
match el with
| [x] -> ignore (type_of_pexp env x)
| _ -> error("Error: Function "^s^" expects one argument.", e.pexp_loc) in
let check_two ty1 ty2 =
match el with
| [x;y] ->
check_pexp env ty1 x;
check_pexp env ty2 y
| _ -> error("Error: Function "^s^" expects two arguments.", e.pexp_loc) in
begin match s with
| "@item" -> check_one_any (); Pty_item
| "@list" -> List.iter (check_pexp env Pty_list) el; Pty_list
| "@set" -> List.iter (check_pexp env Pty_set ) el; Pty_set
| "@zero" -> List.iter (type_of_pexp env >> ignore) el; Pty_null
| "@hd" -> check_one Pty_list; Pty_int
| "@tl" -> check_one Pty_list; Pty_list
| "@sorted" -> check_one Pty_list; Pty_bool
| "@set_of_list" -> check_one Pty_list; Pty_set
| "@sublist" -> check_two Pty_list Pty_list; Pty_bool
| "@list_minus" -> check_two Pty_list Pty_list; Pty_list
| "@minus" -> check_two Pty_set Pty_set; Pty_set
| "@subset" -> check_two Pty_set Pty_set; Pty_bool
| "@can_return" -> check_one_any (); Pty_bool
| _ -> error("Error: found unknown pure function (" ^ s ^ ").", e.pexp_loc)
end
| Pexp_fcall (fid,(vl,el)) ->
let fi =
try lookup_function env fid
with Not_found -> error ("Error: Function " ^ fid ^ " has not been defined.", e.pexp_loc) in
check_pexp_list ("Function call", " reference", e.pexp_loc) env (fst fi.param) (List.map (fun (v,l) -> {pexp_desc=Pexp_ident v; pexp_loc=l}) vl);
check_pexp_list ("Function call", " value", e.pexp_loc) env (snd fi.param) el;
fi.fn_resty
and check_pexp env ty e =
let ty1 = type_of_pexp env e in
expect_type e.pexp_loc ty ty1
and check_pexp_list (s1,s2,loc) env tyl el =
if List.length tyl = List.length el then
List.iter2 (fun (_,ty,_) e -> check_pexp env ty e) tyl el
else
argnum_error(s1,s2,tyl,el,loc)
(** Check an action annotation. *)
let check_action0 env rid actid loc =
let ri =
try lookup_resource env rid with Not_found ->
error ("Error: The resource "^string_of_component rid^" has not been declared.", loc) in
if actid <> "" then begin
try ignore (lookup_action ri actid) with Not_found ->
error ("Error: The definition of resource "^string_of_component rid^" does not have the action "^actid^".", loc)
end
(** Check an action annotation. *)
let check_action env rid actid argl loc =
let ri =
try lookup_resource env rid with Not_found ->
error ("Error: The resource "^string_of_component rid^" has not been declared.", loc) in
if actid <> "" then begin
let ai =
try lookup_action ri actid with Not_found ->
error ("Error: The definition of resource "^string_of_component rid^" does not have the action "^actid^".", loc) in
if List.length ai.act_param <> List.length argl then
argnum_error ("Action annotation","",ai.act_param,argl,loc)
end
(* Make function call code *)
let mk_fcall_stm loc (reso,f,args) =
let e = {pexp_desc = Pexp_fcall(f,args); pexp_loc = loc} in
match reso with
| None -> {pstm_desc = Pstm_exp(e); pstm_loc = loc}
| Some x -> {pstm_desc= Pstm_assign(x,Some e); pstm_loc = loc}
(** Check variables in the proposition [p] are defined. *)
let tycheck_cprop env loc s p =
let g v =
if not (IdMap.mem v env.variables) then
error ("Error: The " ^ s ^ " contains variable " ^ Id.to_string v
^ " is undeclared or out of scope.", loc) in
IdSet.iter g (fv_norm_cprop p)
(** Simple type checking *)
let tycheck_cmd env fn_resty c =
let rec go_stmt in_loop s = match s.pstm_desc with
| Pstm_comment _ -> ()
| Pstm_exp e ->
begin match type_of_pexp env e with
| Pty_void -> ()
| ty -> warning("The program ignores value of type `" ^ string_of_ty ty ^ "'.", s.pstm_loc)
end
| Pstm_assign(v,None) -> ignore (type_of_var s.pstm_loc env v)
| Pstm_assign(v,Some e) -> check_pexp env (type_of_var s.pstm_loc env v) e
| Pstm_fldassign l ->
List.iter
(fun (e1,c,e2) -> check_pexp env
(type_of_pexp env {pexp_desc=Pexp_fld(e1,c); pexp_loc=s.pstm_loc}) e2)
l
| Pstm_dispose (e1,e2) ->
ignore (lookup_class_from_type e1.pexp_loc env (type_of_pexp env e1));
check_pexp env Pty_int e2
| Pstm_block sl -> List.iter (go_stmt in_loop) sl
| Pstm_assume e -> check_pexp env Pty_bool e
| Pstm_if (eo,s1,s2) ->
(match eo with None -> () | Some e -> check_pexp env Pty_bool e);
go_stmt in_loop s1;
go_stmt in_loop s2
| Pstm_while (po,eo,s1) ->
(match po with None -> () | Some p -> tycheck_cprop env s.pstm_loc "loop invariant" (convert_prop p s.pstm_loc));
(match eo with None -> () | Some e -> check_pexp env Pty_bool e);
go_stmt true s1
| Pstm_withres (rid,e,s,actid,el) ->
check_pexp env Pty_bool e;
check_action env rid actid el s.pstm_loc;
List.iter (type_of_pexp env >> ignore) el;
go_stmt in_loop s
| Pstm_interfere (rid,actid) ->
check_action0 env rid actid s.pstm_loc
| Pstm_action (s,rid,actid,el) ->
check_action env rid actid el s.pstm_loc;
List.iter (type_of_pexp env >> ignore) el;
go_stmt in_loop s
| Pstm_parblock par_calls ->
List.iter (mk_fcall_stm s.pstm_loc >> go_stmt false) par_calls
| Pstm_return eo ->
begin match eo with
| None ->
if fn_resty <> Pty_void then
error ("Error: The return type of this function is " ^ string_of_ty fn_resty, s.pstm_loc)
| Some e -> check_pexp env fn_resty e
end
| Pstm_break -> if not in_loop then error ("Syntax error: `break' must be inside a loop.", s.pstm_loc)
| Pstm_continue -> if not in_loop then error ("Syntax error: `continue' must be inside a loop.", s.pstm_loc)
in
List.iter (go_stmt false) c
let tycheck_res env ri =
let gvars = env.variables in
let add_var r (v,ty,loc) =
if IdMap.mem v r then error ("Variable " ^ Id.to_string v ^ " has already been defined and is in scope.", loc)
else IdMap.add v (ty,loc) r in
let ff x res = List.fold_left add_var res x in
(* add locals *)
env.variables <- ff ri.res_locals env.variables;
tycheck_cmd env Pty_void ri.res_cons;
(* remove locals + params *)
env.variables <- gvars;
begin match ri.res_interf with
| None -> ()
| Some c -> tycheck_cmd env Pty_void c
end
let tycheck_fun env fi =
let gvars = env.variables in
let add_var r (v,ty,loc) =
if IdMap.mem v r then error ("Variable " ^ Id.to_string v ^ " has already been defined and is in scope.", loc)
else IdMap.add v (ty,loc) r in
let ff x res = List.fold_left add_var res x in
(* add params *)
env.variables <- ff (snd fi.param) (ff (fst fi.param) gvars);
tycheck_cprop env fi.fn_loc "precondition" fi.pre;
(* add Result *)
if fi.fn_resty <> Pty_void then
env.variables <-
add_var env.variables (Id.create "Result",fi.fn_resty,Location.none);
tycheck_cprop env fi.fn_loc "postcondition" fi.post;
(* add locals *)
env.variables <- ff fi.locals env.variables;
tycheck_cmd env fi.fn_resty fi.body;
(* remove locals + params *)
env.variables <- gvars
(* -------------------------------------------------------------------------- *)
(** {2 Translation to intermediate form} *)
(* -------------------------------------------------------------------------- *)
(* translate an invariant to a proposition *)
let inv_to_cprop po loc = match po with
| None -> cprop_empty
| Some p -> convert_prop p loc
type mod_var_req_modh =
{ md: IdSet.t (** Variables modifies *)
; vr: IdSet.t (** Variables accessed *)
; rq: CompSet.t (** Requires resources *)
; mh: bool (** Modifies heap? *) }
let mvr_empty =
{ md = IdSet.empty; vr = IdSet.empty;
rq = CompSet.empty; mh = false }
let (++.) mvr1 mvr2 =
{ md = mvr1.md ++ mvr2.md;
vr = mvr1.vr ++ mvr2.vr;
rq = CompSet.union mvr1.rq mvr2.rq;
mh = mvr1.mh || mvr2.mh }
let mvr_union_map f l =
List.fold_left (fun mvr x -> mvr ++. f x) mvr_empty l
let mvr_exp env e =
let rec go e = match e.pexp_desc with
| Pexp_new _
| Pexp_bool _
| Pexp_num _ -> mvr_empty
| Pexp_ident x ->
let x = Id.create x in
if Id.is_existential x then mvr_empty
else {mvr_empty with vr = IdSet.singleton x}
| Pexp_cast (e1,_)
| Pexp_prefix (_,e1)
| Pexp_fld (e1,_) -> go e1
| Pexp_infix (_,e1,e2) -> go e1 ++. go e2
| Pexp_fun (i,el) -> mvr_union_map go el
| Pexp_fcall (fid,(vl,el)) ->
let fi = lookup_function env fid in
let md = List.fold (fun (v,_) -> IdSet.add (Id.create v)) vl IdSet.empty in
{ mvr_empty with md = md; vr = md }
++. mvr_union_map go el
++. {md = fi.modif; vr = fi.vars; rq = fi.requires; mh = fi.writes_heap} in
let mvr = go e in
{ mvr with rq = env.enable(mvr.md, mvr.vr) }
(** (modified, vars, required) for a sequence of statements *)
let mvr_body env stl =
let f_e e = mvr_exp env e in
let f_oe e = match e with None -> mvr_empty | Some e -> mvr_exp env e in
let f_p p = {mvr_empty with
vr = fv_norm_cprop (inv_to_cprop p Location.none)} in
let rec f st = match st.pstm_desc with
| Pstm_exp e -> mvr_exp env e
| Pstm_assign (i,oe) ->
let id = IdSet.singleton (Id.create i) in
f_oe oe ++. {mvr_empty with md=id; vr=id}
| Pstm_fldassign l ->
List.fold_left
(fun res (e1,_,e2) -> res ++. mvr_exp env e1 ++. mvr_exp env e2)
{mvr_empty with mh=true} l
| Pstm_dispose (e1,e2) -> {(mvr_exp env e1 ++. mvr_exp env e2) with mh=true}
| Pstm_block stl -> mvr_union_map f stl
| Pstm_assume e -> f_e e
| Pstm_if (a,st1,st2) -> f_oe a ++. f st1 ++. f st2
| Pstm_while (inv,a,st) -> f_oe a ++. f_p inv ++. f st
| Pstm_withres (rid,a,st,aid,argl) ->
let ri = lookup_resource env rid in
let mvr = f st ++. f_e a in
(* calculate rq properly *)
let mvr = { mvr with rq = env.enable(mvr.md, mvr.vr) } in
let md = mvr.md -- ri.owned in
{md = md; vr = md ++ (mvr.vr -- ri.rvars);
rq = CompSet.remove rid mvr.rq; mh=mvr.mh}
| Pstm_action (st,rid,aid,argl) ->
f st
| Pstm_parblock par_calls ->
mvr_union_map f (List.map (mk_fcall_stm st.pstm_loc) par_calls)
| Pstm_return eo -> f_oe eo
| (Pstm_comment _ | Pstm_interfere _ | Pstm_break | Pstm_continue) ->
mvr_empty
in
let mvr = mvr_union_map f stl in
{ mvr with rq = env.enable(mvr.md, mvr.vr) }
let (+++) sm (i,s) =
try let s' = StringMap.find i sm
in StringMap.add i (CompSet.inter s s') sm
with Not_found -> StringMap.add i s sm
let (++++) sm1 sm2 =
StringMap.fold (fun i acq sm -> sm +++ (i,acq)) sm1 sm2
(* initial call graph: only functions called directly *)
let initial_call_graph env _ fi =
let check_fcall fid (vl,el) loc =
let fi =
try lookup_function env fid
with Not_found ->
error ("Function " ^ fid ^ " has not been declared.", loc) in
if List.length vl <> List.length (fst fi.param) then
argnum_error ("Function call", " reference", fst fi.param, vl, loc);
if List.length el <> List.length (snd fi.param) then
argnum_error ("Function call", " value", snd fi.param, el, loc) in
let check_withres rid loc =
try ignore (lookup_resource env rid)
with Not_found ->
error ("Resource " ^ string_of_component rid ^ " has not been defined.",
loc) in
let add_fcall fid acquired = fi.calls <- fi.calls +++ (fid,acquired) in
let rec f_e acquired e = match e.pexp_desc with
| Pexp_new _
| Pexp_num _
| Pexp_bool _
| Pexp_ident _ -> ()
| Pexp_cast (e1,_)
| Pexp_prefix (_,e1)
| Pexp_fld (e1,_) -> f_e acquired e1
| Pexp_infix (_,e1,e2) -> f_e acquired e1; f_e acquired e2
| Pexp_fun (_,el) -> List.iter (f_e acquired) el
| Pexp_fcall (i,(vl,el)) ->
check_fcall i (vl,el) e.pexp_loc;
add_fcall i acquired;
begin match List.filter (fun (x,_) -> IdMap.mem (Id.create x) env.variables) vl with
| [] -> ()
| (x,loc)::_ -> error ("Global variable " ^ x ^ " cannot be passed by reference.", loc)
end;
List.iter (f_e acquired) el in
let rec check_pure e = match e.pexp_desc with
| Pexp_new _
| Pexp_fld _
| Pexp_fcall _ -> error ("Arguments of a parallel call must be pure.",e.pexp_loc)
| Pexp_num _
| Pexp_bool _
| Pexp_ident _ -> ()
| Pexp_cast (e1,_)
| Pexp_prefix (_,e1) -> check_pure e1;
| Pexp_infix (_,e1,e2) -> check_pure e1; check_pure e2
| Pexp_fun (_,el) -> List.iter check_pure el in
let f_eo acquired eo = match eo with
| None -> ()
| Some e -> f_e acquired e in
let rec f acquired st = match st.pstm_desc with
| Pstm_if (a,st1,st2) -> f_eo acquired a; f acquired st1; f acquired st2
| Pstm_while (_,a,st) -> f_eo acquired a; f acquired st
| Pstm_block stl -> List.iter (f acquired) stl
| Pstm_withres (rid,_,st,_,_) ->
check_withres rid st.pstm_loc;
f (CompSet.add rid acquired) st
| Pstm_action (st,_,_,_) -> f acquired st
| Pstm_parblock par_calls ->
List.iter (fun (reso,i,args) ->
check_fcall i args st.pstm_loc;
add_fcall i acquired;
List.iter check_pure (snd args)) par_calls
| Pstm_comment _
| Pstm_interfere _
| Pstm_break
| Pstm_continue
| Pstm_assign (_,None)
| Pstm_return None -> ()
| Pstm_assume e
| Pstm_exp e
| Pstm_return (Some e)
| Pstm_assign (_,Some e) -> f_e acquired e
| Pstm_dispose (e1,e2) -> f_e acquired e1; f_e acquired e2
| Pstm_fldassign l ->
List.iter (fun (e1,_,e2) -> f_e acquired e1; f_e acquired e2) l
in List.iter (f CompSet.empty) fi.body
(* compute the entire call graph of fi assuming _.calls in stop are complete *)
let call_graph_fid env fi stop =
let stopr = ref stop in
let rec f acquired fi =
StringMap.fold
(fun fid' acquired' sm ->
let fi' = lookup_function env fid' in
let acq = CompSet.union acquired acquired' in
let deep_calls =
if StringSet.mem fid' !stopr
then StringMap.map (CompSet.union acq) fi'.calls
else begin
stopr := StringSet.add fid' !stopr;
f (CompSet.union acquired acquired') fi'
end in
sm ++++ (deep_calls +++ (fid',acq)))
fi.calls StringMap.empty in
f CompSet.empty fi
(* entire call graph of all the functions *)
let calc_call_graph env =
let fil = env.functions in
let _ = Hashtbl.iter (initial_call_graph env) fil in
let stopr = ref StringSet.empty in
let f x fi =
fi.calls <- call_graph_fid env fi !stopr;
stopr := StringSet.add x !stopr in
Hashtbl.iter f fil
(* create a function enable (mS aS) returning the resources required to modify
mS and access aS *)
let mk_enable_fun env =
let m = ref IdMap.empty in
let find id =
try IdMap.find id !m
with Not_found -> (CompSet.empty, CompSet.empty) in
let add_mods rid id =
let (mods,vars) = find id in
m := IdMap.add id (CompSet.add rid mods,vars) !m in
let add_vars rid id =
let (mods,vars) = find id in
m := IdMap.add id (mods,CompSet.add rid vars) !m in
let f x ri =
IdSet.iter (add_mods x) ri.rvars;
IdSet.iter (add_vars x) ri.owned in
Hashtbl.iter f env.resources;
let enable (mS,aS) =
IdSet.fold (fun id -> CompSet.union (fst (find id))) mS
(IdSet.fold (fun id -> CompSet.union (snd (find id))) aS CompSet.empty)
in enable
(* calculates the modified and vars and required set for each function *)
let calc_mvr env =
let enable = mk_enable_fun env in
env.enable <- enable;
let shallow_mod _ fi =
let mvr = mvr_body env fi.body in
let fv_spec = (fv_norm_cprop fi.pre ++ fv_norm_cprop fi.post) in
fi.modif <- mvr.md -- fi.bound;
fi.vars <- (mvr.vr ++ fv_spec) -- fi.bound;
fi.requires <- CompSet.union mvr.rq
(enable (IdSet.empty, fv_spec -- fi.bound));
fi.writes_heap <- mvr.mh;
in Hashtbl.iter shallow_mod env.functions;
let deep_mod _ fi =
let call_set fid' fi' =
StringMap.fold (fun fid _ s -> StringSet.add fid s) fi'.calls
(StringSet.singleton fid') in
let in_par_with loc = function
| Pstm_parblock par_calls ->
let add_call_set_to_function cs f =
let fi = lookup_function env f in
fi.in_parallel_with <- StringSet.union fi.in_parallel_with cs in
let rec go cs1 csl2 = match csl2 with
| [] -> ()
| cs::csl3 ->
StringSet.iter (add_call_set_to_function cs) cs1;
List.iter (StringSet.iter (add_call_set_to_function cs)) csl3;
go (StringSet.union cs1 cs) csl3 in
go StringSet.empty (List.map (fun (_,f,_) -> call_set f (lookup_function env f)) par_calls)
| _ -> ()
in Parsetree.iter in_par_with fi.body;
let owned_set acq =
CompSet.fold (fun rid s -> (lookup_resource env rid).owned ++ s) acq IdSet.empty in
let modif =
StringMap.fold
(fun fid acq modif -> modif ++ ((lookup_function env fid).modif -- owned_set acq))
fi.calls fi.modif
and vars =
StringMap.fold
(fun fid acq vars -> vars ++ ((lookup_function env fid).vars -- owned_set acq))
fi.calls fi.vars
and requires =
StringMap.fold
(fun fid acq req -> CompSet.union req (CompSet.diff (lookup_function env fid).requires acq))
fi.calls fi.requires
and writes_heap =
StringMap.fold
(fun fid acq writes_heap -> writes_heap || (lookup_function env fid).writes_heap)
fi.calls fi.writes_heap
in fi.requires <- requires;
fi.modif <- modif;
fi.vars <- vars;
fi.writes_heap <- writes_heap in
Hashtbl.iter deep_mod env.functions
(** Convert a p_action to an action_item *)
let process_action env (s,params,r,p,q,code,loc) acts =
if StringMap.mem s acts then
error ("Action " ^ s ^ " is already defined.", loc);
let r = convert_prop r loc in
let p = convert_prop p loc in
let q = convert_prop q loc in
let fv = fv_norm_cprop r ++ fv_norm_cprop p ++ fv_norm_cprop q in
let exfv = prop_exfv r (prop_exfv p (prop_exfv q IdSet.empty)) in
let bound = set_of_list (List.map (fst >> Id.create) params) ++ exfv in
empty_or_error
(IdSet.filter (fun x -> not (IdMap.mem x env.variables)) (fv -- bound))
("Variable ", " must either be global or bound by the action.", loc);
tycheck_cmd env (Pty_void) code;
StringMap.add s
{ act_id=s; act_param=params; act_locals=exfv; act_bound=bound;
act_ctx=r; act_pre=p; act_post=q; act_code=code; act_loc=loc}
acts
let add_poss_link_field x =
let x = Misc.component_of_string x in
if List.exists (fun y -> x==y) (!Misc.possible_link_fields) then ()
else Misc.possible_link_fields := x :: !Misc.possible_link_fields
let rec process_declarations iteml env =
(* First pass -- class definitions and global variables *)
let go1 = function
| Pdec_comment s -> env.comments <- s::env.comments;
| Pdec_class (sid,tdesc,fields,loc) ->
let fld = List.fold_left (fun res (x,ty,loc) ->
if StringMap.mem x res then error ("Error: Duplicate definition of field " ^ x ^ " in class " ^ sid ^ ".", loc);
if ty=sid then add_poss_link_field x;
StringMap.add x (ty_of_string ty,loc) res) StringMap.empty fields in
let tdesc = match tdesc with
| Some tdesc -> (Some (Qualdecl.transl_patpred_single tdesc) )
| _ -> (None) in
Hashtbl.add env.classes sid {cla_fields = fld; cla_tdesc = tdesc; cla_loc = loc}
| Pdec_var (x,ty,loc) ->
let id = Id.create x in
if IdMap.mem id env.variables then
error ("Global variable " ^ x ^ " is already defined.", loc);
env.variables <- IdMap.add id (ty_of_string ty,loc) env.variables
| Pdec_fun _ | Pdec_resource _ | Pdec_indpred _ -> () in
(* Second pass -- predicates, functions, resources *)
let go2 = function
| (Pdec_class _ | Pdec_var _ | Pdec_comment _) -> ()
| Pdec_fun (fid,resty,param,(pre,post,purespec,effspec),(locals,body),loc) ->
let pre = inv_to_cprop pre loc in
let post = inv_to_cprop post loc in
let f = List.map (fun (x,y,loc) -> (Id.create x,ty_of_string y,loc)) in
let locals = f locals in
let param = (f (fst param), f (snd param)) in
let bound =
(List.map (fun (x,_,_) -> x) >> set_of_list) (locals @ fst param @ snd param) in
env_add_fun env fid
{fn_resty=ty_of_string resty; param=param; locals=locals; bound=bound;
calls=StringMap.empty; in_parallel_with=StringSet.empty; writes_heap=false;
requires=CompSet.empty; modif=IdSet.empty; vars=IdSet.empty;
pre=pre;
purespec= (match purespec with
| Some (r, purespec) -> (r,Qualdecl.transl_patpred_single purespec)
| _ -> ("", Predicate.True));
body=body; post=post;
effspec= (match effspec with
| Some (r,effspec) -> (r,Qualdecl.transl_patpred_single effspec)
| _ -> ("", Predicate.True));
fn_loc=loc}
| Pdec_resource (rid,owned,inv,(locals,body),interf,actions,def,loc) ->
let rec modvars r st = match st.pstm_desc with
| (Pstm_interfere _ | Pstm_assume _ | Pstm_exp _ | Pstm_fldassign _ | Pstm_dispose _
| Pstm_break | Pstm_continue | Pstm_return _ | Pstm_comment _) -> r
| Pstm_assign (i,_) -> IdSet.add (Id.create i) r
| Pstm_block stl -> List.fold_left modvars r stl
| Pstm_if (_,st1,st2) -> modvars (modvars r st1) st2
| Pstm_while (_,_,st) -> modvars r st
| Pstm_withres _ | Pstm_action _ | Pstm_parblock _ -> r (* TODO *) in
let owned = set_of_list (List.map (fst >> Id.create) owned) in
let (rinv,rvars) = match inv with
| None ->
let locals = set_of_list (List.map (fun (x,_,_) -> Id.create x) locals) in
(None, IdSet.diff (List.fold_left modvars IdSet.empty body) locals)
| Some inv ->
let inv = convert_prop inv loc in
(Some inv, fv_norm_cprop inv) in
let acts = List.fold (process_action env) actions StringMap.empty in
let locals = List.map (fun (x,y,loc) -> (Id.create x,ty_of_string y,loc)) locals in
env_add_res env rid
{owned=owned; rvars=rvars; rinv=rinv; ractions=acts;
res_locals = locals; res_cons = body; res_interf=interf; res_loc=loc;
res_def = Qualdecl.transl_patpred_single def}
| Pdec_indpred (pid,param,body,loc) ->
let param = List.map (fst >> Id.create) param in
let body = prop_to_ip_body body loc in
let node = List.length param = 3 && (match body with
| [(x,((_,sl),PNil))] when Pure.is_true x ->
begin match spat_fold_spred (fun x r -> x::r) sl [] with
| [Csp_node(_,_,e,_)] -> e == E.id (List.hd param)
| _ -> false
end
| _ -> false) in
begin try
add_ip_decl
{ip_id=pid;ip_args=param;ip_body=body;ip_node=node;ip_loc=loc}
with Indpred_error (s,loc) -> error (s,loc) end
in
List.iter go1 iteml;
List.iter go2 iteml;
(* Type checking *)
let check_ty s (ty,loc) = match ty with
| Pty_class t ->
begin
try ignore (lookup_class env t)
with Not_found ->
error("The type of " ^ s ^ " refers to the undeclared class "
^ t ^ ".", loc)
end
| _ -> ()
in
Hashtbl.iter (fun _ ci -> StringMap.iter (fun x -> check_ty ("field " ^ x)) ci.cla_fields) env.classes;
IdMap.iter (fun x -> check_ty ("global variable " ^ Id.to_string x))
env.variables;
List.iter (lookup_resource env >> tycheck_res env) (List.rev env.env_rsrcs);
List.iter (lookup_function env >> tycheck_fun env) (List.rev env.env_funcs)
open Format
let pp_component pp c =
Format.pp_print_string pp (string_of_component c)
let rec pp_seq pp f = function
| [] -> ()
| [x] -> pp f x
| x::l -> pp f x; Format.pp_print_char f ','; pp_seq pp f l
let pp_call_item pp (fid,acq) =
begin match CompSet.elements acq with
| [] -> ()
| l ->
Format.pp_print_char pp '{';
pp_seq pp_component pp l;
Format.pp_print_char pp '}'
end;
Format.pp_print_string pp fid
let pp_set x pp f s = match s with
| [] -> ()
| l ->
Format.pp_print_string f x;
pp_seq pp f l
let print_env pp env =
let f fid fi =
Format.pp_print_string pp "FUNCTION ";
Format.pp_print_string pp fid;
pp_set " CALLS" pp_call_item pp (StringMap.fold (fun fid acq l -> (fid,acq)::l) fi.calls []);
pp_set " IN_PARALLEL_WITH" Format.pp_print_string pp (StringSet.elements fi.in_parallel_with);
pp_set " REQUIRES" pp_component pp (CompSet.elements fi.requires);
pp_set " MODIFIES" pp_ident pp (IdSet.elements fi.modif);
pp_set " READS" pp_ident pp (IdSet.elements fi.vars) in
Hashtbl.iter f env.functions
(* star resource invariants *)
let desugar_rsrc_initzers iteml env =
let rsrc_invs =
List.fold_right
(fun item invs -> match item with
| Pdec_resource(rid,prots,Some inv,([],[]),_,actions,_,loc) -> (
let can_inv = convert_prop inv loc in
cprop_star can_inv invs)
| _ -> invs)
iteml
cprop_empty
(* if absent, extend iteml with an init function consisting of the
resource initializers *)
in (rsrc_invs,
if (List.exists (function Pdec_fun("init",_,_,_,_,_) -> true | _ -> false) iteml)
|| not (List.exists (function Pdec_resource _ -> true | _ -> false) iteml)
then iteml
else (Pdec_fun("init", "void",
([],[]),
(Some a_prop_empty, Some a_prop_empty, None, None),
([],[]),
Location.none)
:: iteml))
let compose_init_and_main rsrc_invs env =
let init_post =
try
let init_fun = lookup_function env "init" in
Hashtbl.replace env.functions "init" {init_fun with post = (cprop_star init_fun.post rsrc_invs)};
init_fun.post
with Not_found -> cprop_empty in
try
let main_fun = lookup_function env "main" in
env_add_fun env "compose_init_main"
{fn_resty=Pty_void;
pre= init_post;
body= [];
post= main_fun.pre;
param= [],[];
locals= [];
bound= IdSet.empty;
calls= StringMap.empty;
in_parallel_with= StringSet.empty;
requires= CompSet.empty;
writes_heap = false;
modif= IdSet.empty;
vars= IdSet.empty;
fn_loc= Location.none;
purespec = ("", Predicate.True);
effspec = ("", Predicate.True)}
with Not_found ->
() (*pp "// No main() found.@."*)
let check_variable_conditions env =
let check_main_req_empty () =
try (* check that main requires no resources *)
let main_fun = lookup_function env "main" in