Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_spec.ml @ cefc3744

History | View | Annotate | Download (5.62 KB)

1
(**************************************************************************)
2
(*     Printing spec for c *)
3

    
4
(**************************************************************************)
5

    
6
let pp_acsl_type var fmt t =
7
  let rec aux t pp_suffix =
8
  match (Types.repr t).Types.tdesc with
9
  | Types.Tclock t'       -> aux t' pp_suffix
10
  | Types.Tbool           -> fprintf fmt "int %s%a" var pp_suffix ()
11
  | Types.Treal           -> fprintf fmt "real %s%a" var pp_suffix ()
12
  | Types.Tint            -> fprintf fmt "int %s%a" var pp_suffix ()
13
  | Types.Tarray (d, t')  ->
14
    let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
15
    aux t' pp_suffix'
16
  (* | Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix *)
17
  (* | Types.Tconst ty       -> fprintf fmt "%s %s" ty var *)
18
  (* | Types.Tarrow (_, _)   -> fprintf fmt "void (\*%s)()" var *)
19
  | _                     -> eprintf "internal error: pp_acsl_type %a@." Types.print_ty t; assert false
20
  in aux t (fun fmt () -> ())
21

    
22
let pp_acsl_var_decl fmt id =
23
  pp_acsl_type id.var_id fmt id.var_type
24

    
25

    
26
let pp_econst fmt c = 
27
  match c with
28
    | EConst_int i -> pp_print_int fmt i
29
    | EConst_real r -> pp_print_string fmt r
30
    | EConst_float r -> pp_print_float fmt r
31
    | EConst_bool b -> pp_print_bool fmt b
32
    | EConst_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
33

    
34
let rec pp_eexpr is_output fmt eexpr = 
35
  let pp_eexpr = pp_eexpr is_output in
36
  match eexpr.eexpr_desc with
37
    | EExpr_const c -> pp_econst fmt c
38
    | EExpr_ident id -> 
39
      if is_output id then pp_print_string fmt ("*" ^ id) else pp_print_string fmt id
40
    | EExpr_tuple el -> Utils.fprintf_list ~sep:"," pp_eexpr fmt el
41
    | EExpr_arrow (e1, e2) -> fprintf fmt "%a -> %a" pp_eexpr e1 pp_eexpr e2
42
    | EExpr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_eexpr e1 pp_eexpr e2
43
    (* | EExpr_concat (e1, e2) -> fprintf fmt "%a::%a" pp_eexpr e1 pp_eexpr e2 *)
44
    (* | EExpr_tail e -> fprintf fmt "tail %a" pp_eexpr e *)
45
    | EExpr_pre e -> fprintf fmt "pre %a" pp_eexpr e
46
    | EExpr_when (e, id) -> fprintf fmt "%a when %s" pp_eexpr e id
47
    | EExpr_merge (id, e1, e2) -> 
48
      fprintf fmt "merge (%s, %a, %a)" id pp_eexpr e1 pp_eexpr e2
49
    | EExpr_appl (id, e, r) -> pp_eapp is_output fmt id e r
50
    | EExpr_forall (vars, e) -> fprintf fmt "forall %a; %a" Printers.pp_node_args vars pp_eexpr e 
51
    | EExpr_exists (vars, e) -> fprintf fmt "exists %a; %a" Printers.pp_node_args vars pp_eexpr e 
52

    
53

    
54
    (* | EExpr_whennot _ *)
55
    (* | EExpr_uclock _ *)
56
    (* | EExpr_dclock _ *)
57
    (* | EExpr_phclock _ -> assert false *)
58
and pp_eapp is_output fmt id e r =
59
  let pp_eexpr = pp_eexpr is_output in
60
  match r with
61
  | None ->
62
    (match id, e.eexpr_desc with
63
    | "+", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_eexpr e1 pp_eexpr e2
64
    | "uminus", _ -> fprintf fmt "(- %a)" pp_eexpr e
65
    | "-", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_eexpr e1 pp_eexpr e2
66
    | "*", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_eexpr e1 pp_eexpr e2
67
    | "/", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_eexpr e1 pp_eexpr e2
68
    | "mod", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_eexpr e1 pp_eexpr e2
69
    | "&&", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a && %a)" pp_eexpr e1 pp_eexpr e2
70
    | "||", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a || %a)" pp_eexpr e1 pp_eexpr e2
71
    | "xor", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ^^ %a)" pp_eexpr e1 pp_eexpr e2
72
    | "impl", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ==> %a)" pp_eexpr e1 pp_eexpr e2
73
    | "<", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_eexpr e1 pp_eexpr e2
74
    | "<=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_eexpr e1 pp_eexpr e2
75
    | ">", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_eexpr e1 pp_eexpr e2
76
    | ">=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_eexpr e1 pp_eexpr e2
77
    | "!=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_eexpr e1 pp_eexpr e2
78
    | "=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a == %a)" pp_eexpr e1 pp_eexpr e2
79
    | "not", _ -> fprintf fmt "(! %a)" pp_eexpr e
80
    | "ite", EExpr_tuple([e1;e2;e3]) -> fprintf fmt "(if %a then %a else %a)" pp_eexpr e1 pp_eexpr e2 pp_eexpr e3
81
    | _ -> fprintf fmt "%s (%a)" id pp_eexpr e)
82
  | Some x -> fprintf fmt "%s (%a) every %s" id pp_eexpr e x 
83

    
84
let pp_ensures is_output fmt e =
85
  match e with
86
    | EnsuresExpr e -> fprintf fmt "ensures %a;@ " (pp_eexpr is_output) e
87
    | SpecObserverNode (name, args) -> fprintf fmt "observer %s (%a);@ " name (Utils.fprintf_list ~sep:", " (pp_eexpr is_output)) args
88

    
89
let pp_acsl_spec outputs fmt spec =
90
  let is_output = fun oid -> List.exists (fun v -> v.var_id = oid) outputs in
91
  let pp_eexpr = pp_eexpr is_output in
92
  fprintf fmt "@[<v 2>/*@@ ";
93
  Utils.fprintf_list ~sep:"" (fun fmt r -> fprintf fmt "requires %a;@ " pp_eexpr r) fmt spec.requires;
94
  Utils.fprintf_list ~sep:"" (pp_ensures is_output) fmt spec.ensures;
95
  fprintf fmt "@ ";
96
  (* fprintf fmt "assigns *self%t%a;@ "  *)
97
  (*   (fun fmt -> if List.length outputs > 0 then fprintf fmt ", ") *)
98
  (*   (fprintf_list ~sep:"," (fun fmt v -> fprintf fmt "*%s" v.var_id)) outputs; *)
99
  Utils.fprintf_list ~sep:"@ " (fun fmt (name, assumes, requires) -> 
100
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
101
      name
102
      (Utils.fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
103
      (Utils.fprintf_list ~sep:"@ " (pp_ensures is_output)) requires
104
  ) fmt spec.behaviors;
105
  fprintf fmt "@]@ */@.";
106
  ()
107

    
108

    
109

    
110

    
111
let print_machine_decl_prefix fmt m =
112
   (* Print specification if any *)
113
   (match m.mspec with
114
  | None -> ()
115
  | Some spec -> 
116
    pp_acsl_spec m.mstep.step_outputs fmt spec
117
  )
118

    
119
(* Local Variables: *)
120
(* compile-command:"make -C ../../.." *)
121
(* End: *)