lustrec / src / plugins / mpfr / mpfr.ml @ 19a1e66b
History  View  Annotate  Download (10.4 KB)
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  
12 
open Utils 
13 
open Lustre_types 
14 
open Machine_code_types 
15 
open Corelang 
16 
open Normalization 
17 
open Machine_code_common 
18  
19 
let mpfr_module = mktop (Open(false, "mpfr_lustre")) 
20 
let cpt_fresh = ref 0 
21 

22 
let mpfr_rnd () = "MPFR_RNDN" 
23  
24 
let mpfr_prec () = !Options.mpfr_prec 
25  
26 
let inject_id = "MPFRId" 
27  
28 
let inject_copy_id = "mpfr_set" 
29  
30 
let inject_real_id = "mpfr_set_flt" 
31  
32 
let inject_init_id = "mpfr_init2" 
33  
34 
let inject_clear_id = "mpfr_clear" 
35  
36 
let mpfr_t = "mpfr_t" 
37  
38 
let unfoldable_value value = 
39 
not (Types.is_real_type value.value_type && is_const_value value) 
40  
41 
let inject_id_id expr = 
42 
let e = mkpredef_call expr.expr_loc inject_id [expr] in 
43 
{ e with 
44 
expr_type = Type_predef.type_real; 
45 
expr_clock = expr.expr_clock; 
46 
} 
47  
48 
let pp_inject_real pp_var pp_val fmt var value = 
49 
Format.fprintf fmt "%s(%a, %a, %s);" 
50 
inject_real_id 
51 
pp_var var 
52 
pp_val value 
53 
(mpfr_rnd ()) 
54  
55 
let inject_assign expr = 
56 
let e = mkpredef_call expr.expr_loc inject_copy_id [expr] in 
57 
{ e with 
58 
expr_type = Type_predef.type_real; 
59 
expr_clock = expr.expr_clock; 
60 
} 
61  
62 
let pp_inject_copy pp_var fmt var value = 
63 
Format.fprintf fmt "%s(%a, %a, %s);" 
64 
inject_copy_id 
65 
pp_var var 
66 
pp_var value 
67 
(mpfr_rnd ()) 
68  
69 
let rec pp_inject_assign pp_var fmt var value = 
70 
if is_const_value value 
71 
then 
72 
pp_inject_real pp_var pp_var fmt var value 
73 
else 
74 
pp_inject_copy pp_var fmt var value 
75  
76 
let pp_inject_init pp_var fmt var = 
77 
Format.fprintf fmt "%s(%a, %i);" 
78 
inject_init_id 
79 
pp_var var 
80 
(mpfr_prec ()) 
81  
82 
let pp_inject_clear pp_var fmt var = 
83 
Format.fprintf fmt "%s(%a);" 
84 
inject_clear_id 
85 
pp_var var 
86  
87 
let base_inject_op id = 
88 
match id with 
89 
 "+" > "MPFRPlus" 
90 
 "" > "MPFRMinus" 
91 
 "*" > "MPFRTimes" 
92 
 "/" > "MPFRDiv" 
93 
 "uminus" > "MPFRUminus" 
94 
 "<=" > "MPFRLe" 
95 
 "<" > "MPFRLt" 
96 
 ">=" > "MPFRGe" 
97 
 ">" > "MPFRGt" 
98 
 "=" > "MPFREq" 
99 
 "!=" > "MPFRNeq" 
100 
(* Math library functions *) 
101 
 "acos" > "MPFRacos" 
102 
 "acosh" > "MPFRacosh" 
103 
 "asin" > "MPFRasin" 
104 
 "asinh" > "MPFRasinh" 
105 
 "atan" > "MPFRatan" 
106 
 "atan2" > "MPFRatan2" 
107 
 "atanh" > "MPFRatanh" 
108 
 "cbrt" > "MPFRcbrt" 
109 
 "cos" > "MPFRcos" 
110 
 "cosh" > "MPFRcosh" 
111 
 "ceil" > "MPFRceil" 
112 
 "erf" > "MPFRerf" 
113 
 "exp" > "MPFRexp" 
114 
 "fabs" > "MPFRfabs" 
115 
 "floor" > "MPFRfloor" 
116 
 "fmod" > "MPFRfmod" 
117 
 "log" > "MPFRlog" 
118 
 "log10" > "MPFRlog10" 
119 
 "pow" > "MPFRpow" 
120 
 "round" > "MPFRround" 
121 
 "sin" > "MPFRsin" 
122 
 "sinh" > "MPFRsinh" 
123 
 "sqrt" > "MPFRsqrt" 
124 
 "trunc" > "MPFRtrunc" 
125 
 "tan" > "MPFRtan" 
126 
 _ > raise Not_found 
127  
128 
let inject_op id = 
129 
Format.eprintf "trying to inject mpfr into function %s@." id; 
130 
try 
131 
base_inject_op id 
132 
with Not_found > id 
133  
134 
let homomorphic_funs = 
135 
List.fold_right (fun id res > try base_inject_op id :: res with Not_found > res) Basic_library.internal_funs [] 
136  
137 
let is_homomorphic_fun id = 
138 
List.mem id homomorphic_funs 
139  
140 
let inject_call expr = 
141 
match expr.expr_desc with 
142 
 Expr_appl (id, args, None) when not (Basic_library.is_expr_internal_fun expr) > 
143 
{ expr with expr_desc = Expr_appl (inject_op id, args, None) } 
144 
 _ > expr 
145  
146 
let expr_of_const_array expr = 
147 
match expr.expr_desc with 
148 
 Expr_const (Const_array cl) > 
149 
let typ = Types.array_element_type expr.expr_type in 
150 
let expr_of_const c = 
151 
{ expr_desc = Expr_const c; 
152 
expr_type = typ; 
153 
expr_clock = expr.expr_clock; 
154 
expr_loc = expr.expr_loc; 
155 
expr_delay = Delay.new_var (); 
156 
expr_annot = None; 
157 
expr_tag = new_tag (); 
158 
} 
159 
in { expr with expr_desc = Expr_array (List.map expr_of_const cl) } 
160 
 _ > assert false 
161  
162 
(* inject_<foo> : defs * used vars > <foo> > (updated defs * updated vars) * normalized <foo> *) 
163 
let rec inject_list alias node inject_element defvars elist = 
164 
List.fold_right 
165 
(fun t (defvars, qlist) > 
166 
let defvars, norm_t = inject_element alias node defvars t in 
167 
(defvars, norm_t :: qlist) 
168 
) elist (defvars, []) 
169  
170 
let rec inject_expr ?(alias=true) node defvars expr = 
171 
let res = 
172 
match expr.expr_desc with 
173 
 Expr_const (Const_real _) > mk_expr_alias_opt alias node defvars expr 
174 
 Expr_const (Const_array _) > inject_expr ~alias:alias node defvars (expr_of_const_array expr) 
175 
 Expr_const (Const_struct _) > assert false 
176 
 Expr_ident _ 
177 
 Expr_const _ > defvars, expr 
178 
 Expr_array elist > 
179 
let defvars, norm_elist = inject_list alias node (fun _ > inject_expr ~alias:true) defvars elist in 
180 
let norm_expr = { expr with expr_desc = Expr_array norm_elist } in 
181 
defvars, norm_expr 
182 
 Expr_power (e1, d) > 
183 
let defvars, norm_e1 = inject_expr node defvars e1 in 
184 
let norm_expr = { expr with expr_desc = Expr_power (norm_e1, d) } in 
185 
defvars, norm_expr 
186 
 Expr_access (e1, d) > 
187 
let defvars, norm_e1 = inject_expr node defvars e1 in 
188 
let norm_expr = { expr with expr_desc = Expr_access (norm_e1, d) } in 
189 
defvars, norm_expr 
190 
 Expr_tuple elist > 
191 
let defvars, norm_elist = 
192 
inject_list alias node (fun alias > inject_expr ~alias:alias) defvars elist in 
193 
let norm_expr = { expr with expr_desc = Expr_tuple norm_elist } in 
194 
defvars, norm_expr 
195 
 Expr_appl (id, args, r) > 
196 
let defvars, norm_args = inject_expr node defvars args in 
197 
let norm_expr = { expr with expr_desc = Expr_appl (id, norm_args, r) } in 
198 
mk_expr_alias_opt alias node defvars (inject_call norm_expr) 
199 
 Expr_arrow _ > defvars, expr 
200 
 Expr_pre e > 
201 
let defvars, norm_e = inject_expr node defvars e in 
202 
let norm_expr = { expr with expr_desc = Expr_pre norm_e } in 
203 
defvars, norm_expr 
204 
 Expr_fby (e1, e2) > 
205 
let defvars, norm_e1 = inject_expr node defvars e1 in 
206 
let defvars, norm_e2 = inject_expr node defvars e2 in 
207 
let norm_expr = { expr with expr_desc = Expr_fby (norm_e1, norm_e2) } in 
208 
defvars, norm_expr 
209 
 Expr_when (e, c, l) > 
210 
let defvars, norm_e = inject_expr node defvars e in 
211 
let norm_expr = { expr with expr_desc = Expr_when (norm_e, c, l) } in 
212 
defvars, norm_expr 
213 
 Expr_ite (c, t, e) > 
214 
let defvars, norm_c = inject_expr node defvars c in 
215 
let defvars, norm_t = inject_expr node defvars t in 
216 
let defvars, norm_e = inject_expr node defvars e in 
217 
let norm_expr = { expr with expr_desc = Expr_ite (norm_c, norm_t, norm_e) } in 
218 
defvars, norm_expr 
219 
 Expr_merge (c, hl) > 
220 
let defvars, norm_hl = inject_branches node defvars hl in 
221 
let norm_expr = { expr with expr_desc = Expr_merge (c, norm_hl) } in 
222 
defvars, norm_expr 
223 
in 
224 
(*Format.eprintf "inject_expr %B %a = %a@." alias Printers.pp_expr expr Printers.pp_expr (snd res);*) 
225 
res 
226  
227 
and inject_branches node defvars hl = 
228 
List.fold_right 
229 
(fun (t, h) (defvars, norm_q) > 
230 
let (defvars, norm_h) = inject_expr node defvars h in 
231 
defvars, (t, norm_h) :: norm_q 
232 
) 
233 
hl (defvars, []) 
234  
235  
236 
let rec inject_eq node defvars eq = 
237 
let (defs', vars'), norm_rhs = inject_expr ~alias:false node defvars eq.eq_rhs in 
238 
let norm_eq = { eq with eq_rhs = norm_rhs } in 
239 
norm_eq::defs', vars' 
240  
241 
(* let inject_eexpr ee = 
242 
* { ee with eexpr_qfexpr = inject_expr ee.eexpr_qfexpr } 
243 
* 
244 
* let inject_spec s = 
245 
* { s with 
246 
* assume = List.map inject_eexpr s.assume; 
247 
* guarantees = List.map inject_eexpr s.guarantees; 
248 
* modes = List.map (fun m > 
249 
* { m with 
250 
* require = List.map inject_eexpr m.require; 
251 
* ensure = List.map inject_eexpr m.ensure 
252 
* } 
253 
* ) s.modes 
254 
* } *) 
255 

256 
(** normalize_node node returns a normalized node, 
257 
ie. 
258 
 updated locals 
259 
 new equations 
260 
 
261 
*) 
262 
let inject_node node = 
263 
cpt_fresh := 0; 
264 
let inputs_outputs = node.node_inputs@node.node_outputs in 
265 
let is_local v = 
266 
List.for_all ((!=) v) inputs_outputs in 
267 
let orig_vars = inputs_outputs@node.node_locals in 
268 
let defs, vars = 
269 
let eqs, auts = get_node_eqs node in 
270 
if auts != [] then assert false; (* Automata should be expanded by now. *) 
271 
List.fold_left (inject_eq node) ([], orig_vars) eqs in 
272 
(* Normalize the asserts *) 
273 
let vars, assert_defs, asserts = 
274 
List.fold_left ( 
275 
fun (vars, def_accu, assert_accu) assert_ > 
276 
let assert_expr = assert_.assert_expr in 
277 
let (defs, vars'), expr = 
278 
inject_expr 
279 
~alias:false 
280 
node 
281 
([], vars) (* defvar only contains vars *) 
282 
assert_expr 
283 
in 
284 
vars', defs@def_accu, {assert_ with assert_expr = expr}::assert_accu 
285 
) (vars, [], []) node.node_asserts in 
286 
let new_locals = List.filter is_local vars in 
287 
(* Compute traceability info: 
288 
 gather newly bound variables 
289 
 compute the associated expression without aliases 
290 
*) 
291 
(* let diff_vars = List.filter (fun v > not (List.mem v node.node_locals)) new_locals in *) 
292 
(* See comment below 
293 
* let spec = match node.node_spec with 
294 
*  None > None 
295 
*  Some spec > Some (inject_spec spec) 
296 
* in *) 
297 
let node = 
298 
{ node with 
299 
node_locals = new_locals; 
300 
node_stmts = List.map (fun eq > Eq eq) (defs @ assert_defs); 
301 
(* Incomplete work: TODO. Do we have to inject MPFR code here? 
302 
Does it make sense for annotations? For me, only if we produce 
303 
C code for annotations. Otherwise the various verification 
304 
backend should have their own understanding, but would not 
305 
necessarily require this additional normalization. *) 
306 
(* 
307 
node_spec = spec; 
308 
node_annot = List.map (fun ann > {ann with 
309 
annots = List.map (fun (ids, ee) > ids, inject_eexpr ee) ann.annots} 
310 
) node.node_annot *) 
311 
} 
312 
in ((*Printers.pp_node Format.err_formatter node;*) node) 
313  
314 
let inject_decl decl = 
315 
match decl.top_decl_desc with 
316 
 Node nd > 
317 
{decl with top_decl_desc = Node (inject_node nd)} 
318 
 Include _  Open _  ImportedNode _  Const _  TypeDef _ > decl 
319 

320 
let inject_prog decls = 
321 
List.map inject_decl decls 
322  
323  
324 
(* Local Variables: *) 
325 
(* compilecommand:"make C .." *) 
326 
(* End: *) 