Project

General

Profile

Download (8.82 KB) Statistics
| Branch: | Tag: | Revision:
1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
5
(*                                                                  *)
6
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
7
(*  under the terms of the GNU Lesser General Public License        *)
8
(*  version 2.1.                                                    *)
9
(*                                                                  *)
10
(********************************************************************)
11
open Lustrec
12
open Format
13
open Lustrec.Lustre_types
14
open Machine_code
15
open C_backend_common
16
open Lustrec.Utils
17

    
18
(**************************************************************************)
19
(*     Printing spec for c *)
20

    
21
(**************************************************************************)
22
(* OLD STUFF ???
23

    
24

    
25
let pp_acsl_type var fmt t =
26
  let rec aux t pp_suffix =
27
  match (Lustrec.Types.repr t).Lustrec.Types.tdesc with
28
  | Lustrec.Types.Tclock t'       -> aux t' pp_suffix
29
  | Lustrec.Types.Tbool           -> fprintf fmt "int %s%a" var pp_suffix ()
30
  | Lustrec.Types.Treal           -> fprintf fmt "real %s%a" var pp_suffix ()
31
  | Lustrec.Types.Tint            -> fprintf fmt "int %s%a" var pp_suffix ()
32
  | Lustrec.Types.Tarray (d, t')  ->
33
    let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
34
    aux t' pp_suffix'
35
  (* | Lustrec.Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix *)
36
  (* | Lustrec.Types.Tconst ty       -> fprintf fmt "%s %s" ty var *)
37
  (* | Lustrec.Types.Tarrow (_, _)   -> fprintf fmt "void (\*%s)()" var *)
38
  | _                     -> eprintf "internal error: pp_acsl_type %a@." Lustrec.Types.print_ty t; assert false
39
  in aux t (fun fmt () -> ())
40

    
41
let pp_acsl_var_decl fmt id =
42
  pp_acsl_type id.var_id fmt id.var_type
43

    
44

    
45
let rec pp_eexpr is_output fmt eexpr = 
46
  let pp_eexpr = pp_eexpr is_output in
47
  match eexpr.eexpr_desc with
48
    | EExpr_const c -> Lustrec.Printers.pp_econst fmt c
49
    | EExpr_ident id -> 
50
      if is_output id then pp_print_string fmt ("*" ^ id) else pp_print_string fmt id
51
    | EExpr_tuple el -> Lustrec.Utils.fprintf_list ~sep:"," pp_eexpr fmt el
52
    | EExpr_arrow (e1, e2) -> fprintf fmt "%a -> %a" pp_eexpr e1 pp_eexpr e2
53
    | EExpr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_eexpr e1 pp_eexpr e2
54
    (* | EExpr_concat (e1, e2) -> fprintf fmt "%a::%a" pp_eexpr e1 pp_eexpr e2 *)
55
    (* | EExpr_tail e -> fprintf fmt "tail %a" pp_eexpr e *)
56
    | EExpr_pre e -> fprintf fmt "pre %a" pp_eexpr e
57
    | EExpr_when (e, id) -> fprintf fmt "%a when %s" pp_eexpr e id
58
    | EExpr_merge (id, e1, e2) -> 
59
      fprintf fmt "merge (%s, %a, %a)" id pp_eexpr e1 pp_eexpr e2
60
    | EExpr_appl (id, e, r) -> pp_eapp is_output fmt id e r
61
    | EExpr_forall (vars, e) -> fprintf fmt "forall %a; %a" Lustrec.Printers.pp_node_args vars pp_eexpr e 
62
    | EExpr_exists (vars, e) -> fprintf fmt "exists %a; %a" Lustrec.Printers.pp_node_args vars pp_eexpr e 
63

    
64

    
65
    (* | EExpr_whennot _ *)
66
    (* | EExpr_uclock _ *)
67
    (* | EExpr_dclock _ *)
68
    (* | EExpr_phclock _ -> assert false *)
69
and pp_eapp is_output fmt id e r =
70
  let pp_eexpr = pp_eexpr is_output in
71
  match r with
72
  | None ->
73
    (match id, e.eexpr_desc with
74
    | "+", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_eexpr e1 pp_eexpr e2
75
    | "uminus", _ -> fprintf fmt "(- %a)" pp_eexpr e
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
    | "mod", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_eexpr e1 pp_eexpr e2
80
    | "&&", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a && %a)" pp_eexpr e1 pp_eexpr e2
81
    | "||", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a || %a)" pp_eexpr e1 pp_eexpr e2
82
    | "xor", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ^^ %a)" pp_eexpr e1 pp_eexpr e2
83
    | "impl", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ==> %a)" pp_eexpr e1 pp_eexpr e2
84
    | "<", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_eexpr e1 pp_eexpr e2
85
    | "<=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_eexpr e1 pp_eexpr e2
86
    | ">", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_eexpr e1 pp_eexpr e2
87
    | ">=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_eexpr e1 pp_eexpr e2
88
    | "!=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_eexpr e1 pp_eexpr e2
89
    | "=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a == %a)" pp_eexpr e1 pp_eexpr e2
90
    | "not", _ -> fprintf fmt "(! %a)" pp_eexpr e
91
    | "ite", EExpr_tuple([e1;e2;e3]) -> fprintf fmt "(if %a then %a else %a)" pp_eexpr e1 pp_eexpr e2 pp_eexpr e3
92
    | _ -> fprintf fmt "%s (%a)" id pp_eexpr e)
93
  | Some x -> fprintf fmt "%s (%a) every %s" id pp_eexpr e x 
94

    
95
let pp_ensures is_output fmt e =
96
  match e with
97
    | EnsuresExpr e -> fprintf fmt "ensures %a;@ " (pp_eexpr is_output) e
98
    | SpecObserverNode (name, args) -> fprintf fmt "observer %s (%a);@ " name (Lustrec.Utils.fprintf_list ~sep:", " (pp_eexpr is_output)) args
99

    
100
let pp_acsl_spec outputs fmt spec =
101
  let is_output = fun oid -> List.exists (fun v -> v.var_id = oid) outputs in
102
  let pp_eexpr = pp_eexpr is_output in
103
  fprintf fmt "@[<v 2>/*@@ ";
104
  Lustrec.Utils.fprintf_list ~sep:"" (fun fmt r -> fprintf fmt "requires %a;@ " pp_eexpr r) fmt spec.requires;
105
  Lustrec.Utils.fprintf_list ~sep:"" (pp_ensures is_output) fmt spec.ensures;
106
  fprintf fmt "@ ";
107
  (* fprintf fmt "assigns *self%t%a;@ "  *)
108
  (*   (fun fmt -> if List.length outputs > 0 then fprintf fmt ", ") *)
109
  (*   (fprintf_list ~sep:"," (fun fmt v -> fprintf fmt "*%s" v.var_id)) outputs; *)
110
  Lustrec.Utils.fprintf_list ~sep:"@ " (fun fmt (name, assumes, requires) -> 
111
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
112
      name
113
      (Lustrec.Utils.fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
114
      (Lustrec.Utils.fprintf_list ~sep:"@ " (pp_ensures is_output)) requires
115
  ) fmt spec.behaviors;
116
  fprintf fmt "@]@ */@.";
117
  ()
118

    
119

    
120

    
121

    
122
let print_machine_decl_prefix fmt m =
123
   (* Print specification if any *)
124
   (match m.mspec with
125
  | None -> ()
126
  | Some spec -> 
127
    pp_acsl_spec m.mstep.step_outputs fmt spec
128
  )
129

    
130
  *)
131

    
132
(* TODO ACSL
133
   mspec are function body annotations, sush as loop invariants, acsl asserts ... *) 
134
let pp_mspec fmt s = ()
135

    
136
   (* TODO ACSL 
137
   Return updates machines (eg with local annotations) and acsl preamble *)
138
let preprocess_acsl machines = machines, []
139
                          
140
(* TODO: This preamble shall be a list of types, axiomatics, predicates, theorems *)
141
let pp_acsl_preamble fmt preamble =
142
  Format.fprintf fmt "";
143
  ()
144
(**************************************************************************)
145
(*                              MAKEFILE                                  *)
146
(**************************************************************************)
147

    
148
let makefile_targets fmt basename nodename dependencies =
149
  fprintf fmt "FRAMACEACSL=`frama-c -print-share-path`/e-acsl@.";
150
  (* EACSL version of library file . c *)
151
  fprintf fmt "%s_eacsl.c: %s.c %s.h@." basename basename basename;
152
  fprintf fmt 
153
    "\tframa-c -e-acsl-full-mmodel -machdep x86_64 -e-acsl %s.c -then-on e-acsl -print -ocode %s_eacsl.c@." 
154
    basename basename; 
155
  fprintf fmt "@.";
156
  fprintf fmt "@.";
157

    
158
  (* EACSL version of library file . c + main .c  *)
159
  fprintf fmt "%s_main_eacsl.c: %s.c %s.h %s_main.c@." basename basename basename basename;
160
  fprintf fmt "\tframa-c -e-acsl-full-mmodel -machdep x86_64 -e-acsl %s.c %s_main.c -then-on e-acsl -print -ocode %s_main_eacsl.i@." 
161
    basename basename basename; 
162
  (* Ugly hack to deal with eacsl bugs *)
163
  fprintf fmt "\tgrep -v _fc_stdout %s_main_eacsl.i > %s_main_eacsl.c" basename basename;
164
  fprintf fmt "@.";
165
  fprintf fmt "@.";
166

    
167
  (* EACSL version of binary *)
168
  fprintf fmt "%s_main_eacsl: %s_main_eacsl.c@." basename basename;
169
  fprintf fmt "\t${GCC} -Wno-attributes -I${INC} -I. -c %s_main_eacsl.c@." basename; (* compiling instrumented lib + main *)
170
  C_backend_makefile.fprintf_dependencies fmt dependencies; 
171
  fprintf fmt "\t${GCC} -Wno-attributes -o %s_main_eacsl io_frontend.o %a %s %s_main_eacsl.o %a@." 
172
    basename 
173
    (Lustrec.Utils.fprintf_list ~sep:" " (fun fmt dep -> Format.fprintf fmt "%s.o" dep.name)) 
174
    (C_backend_makefile.compiled_dependencies dependencies)
175
    ("${FRAMACEACSL}/e_acsl.c " 
176
     ^ "${FRAMACEACSL}/memory_model/e_acsl_bittree.c " 
177
     ^ "${FRAMACEACSL}/memory_model/e_acsl_mmodel.c")
178
    basename 
179
    (Lustrec.Utils.fprintf_list ~sep:" " (fun fmt lib -> fprintf fmt "-l%s" lib)) 
180
    (C_backend_makefile.lib_dependencies dependencies)
181
  ;
182
  fprintf fmt "@.";
183

    
184
module MakefileMod =
185
struct
186
  let other_targets = makefile_targets
187
    
188
end
189

    
190
(* Local Variables: *)
191
(* compile-command:"make -C ../../.." *)
192
(* End: *)
(6-6/8)