Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / Horn / horn_backend_printers.ml @ fa91d4d0

History | View | Annotate | Download (26.5 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
(* The compilation presented here was first defined in Garoche, Gurfinkel,
13
   Kahsai, HCSV'14.
14

    
15
   This is a modified version that handle reset
16
*)
17

    
18
open Format
19
open Lustre_types
20
open Machine_code_types
21
open Corelang
22
open Machine_code_common
23

    
24
open Horn_backend_common
25
  
26
(********************************************************************************************)
27
(*                    Instruction Printing functions                                        *)
28
(********************************************************************************************)
29

    
30
let pp_horn_var m fmt id =
31
  (*if Types.is_array_type id.var_type
32
  then
33
    assert false (* no arrays in Horn output *)
34
  else*)
35
    fprintf fmt "%s" id.var_id
36

    
37
(* Used to print boolean constants *)
38
let pp_horn_tag fmt t =
39
  pp_print_string fmt (if t = tag_true then "true" else if t = tag_false then "false" else t)
40
    
41
(* Prints a constant value *)
42
let rec pp_horn_const fmt c =
43
  match c with
44
    | Const_int i    -> pp_print_int fmt i
45
    | Const_real (_,_,s)   -> pp_print_string fmt s
46
    | Const_tag t    -> pp_horn_tag fmt t
47
    | _              -> assert false
48

    
49
(* Default value for each type, used when building arrays. Eg integer array
50
   [2;7] is defined as (store (store (0) 1 7) 0 2) where 0 is this default value
51
   for the type integer (arrays).
52
*)
53
let rec pp_default_val fmt t =
54
  let t = Types.dynamic_type t in
55
  if Types.is_bool_type t  then fprintf fmt "true" else
56
  if Types.is_int_type t then fprintf fmt "0" else 
57
  if Types.is_real_type t then fprintf fmt "0" else 
58
  match (Types.dynamic_type t).Types.tdesc with
59
  | Types.Tarray(dim, l) -> (* TODO PL: this strange code has to be (heavily) checked *)
60
     let valt = Types.array_element_type t in
61
     fprintf fmt "((as const (Array Int %a)) %a)"
62
       pp_type valt 
63
       pp_default_val valt
64
  | Types.Tstruct(l) -> assert false
65
  | Types.Ttuple(l) -> assert false
66
  |_ -> assert false
67

    
68
let pp_mod pp_val v1 v2 fmt =
69
  if Types.is_int_type v1.value_type &&  not !Options.integer_div_euclidean then
70
    (* C semantics: converting it to Euclidian operators
71
       (a mod_M b) - (a < 0 ? abs(b) : 0)            
72
    *)
73
    Format.fprintf fmt "(- (mod %a %a) (ite (< %a 0) (abs %a) 0))"
74
      pp_val v1 pp_val v2 pp_val v1 pp_val v2
75
  else
76
    Format.fprintf fmt "(mod %a %a)" pp_val v1 pp_val v2
77

    
78
let pp_div pp_val v1 v2 fmt =
79
  if Types.is_int_type v1.value_type &&  not !Options.integer_div_euclidean then
80
    (* C semantics: converting it to Euclidian operators
81
       (a - ((a mod_M b) - (a < 0 ? abs(b) : 0))) div_M b
82
    *)
83
    Format.fprintf fmt "(div (- %a (- (mod %a %a) (ite (< %a 0) (abs %a) 0))) %a)"
84
      pp_val v1 pp_val v1 pp_val v2
85
      pp_val v1 pp_val v2 pp_val v2
86
  else
87
    Format.fprintf fmt "(div %a %a)" pp_val v1 pp_val v2
88

    
89

    
90
let pp_basic_lib_fun i pp_val fmt vl =
91
  match i, vl with
92
  | "ite", [v1; v2; v3] -> Format.fprintf fmt "(@[<hov 2>ite %a@ %a@ %a@])" pp_val v1 pp_val v2 pp_val v3
93
  | "uminus", [v] -> Format.fprintf fmt "(- %a)" pp_val v
94
  | "not", [v] -> Format.fprintf fmt "(not %a)" pp_val v
95
  | "=", [v1; v2] -> Format.fprintf fmt "(= %a %a)" pp_val v1 pp_val v2
96
  | "&&", [v1; v2] -> Format.fprintf fmt "(and %a %a)" pp_val v1 pp_val v2
97
  | "||", [v1; v2] -> Format.fprintf fmt "(or %a %a)" pp_val v1 pp_val v2
98
  | "impl", [v1; v2] -> Format.fprintf fmt "(=> %a %a)" pp_val v1 pp_val v2
99
  | "equi", [v1; v2] -> Format.fprintf fmt "(%a = %a)" pp_val v1 pp_val v2
100
  | "xor", [v1; v2] -> Format.fprintf fmt "(%a xor %a)" pp_val v1 pp_val v2
101
  | "!=", [v1; v2] -> Format.fprintf fmt "(not (= %a %a))" pp_val v1 pp_val v2
102
  | "mod", [v1; v2] -> pp_mod pp_val v1 v2 fmt
103
  | "/", [v1; v2] -> pp_div pp_val v1 v2 fmt
104
  | _, [v1; v2] -> Format.fprintf fmt "(%s %a %a)" i pp_val v1 pp_val v2
105
  | _ -> (Format.eprintf "internal error: Basic_library.pp_horn %s@." i; assert false)
106
(*  | "mod", [v1; v2] -> Format.fprintf fmt "(%a %% %a)" pp_val v1 pp_val v2
107

    
108
*)
109

    
110

    
111
(* Prints a value expression [v], with internal function calls only.
112
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
113
   but an offset suffix may be added for array variables
114
*)
115
let rec pp_horn_val ?(is_lhs=false) self pp_var fmt v =
116
  match v.value_desc with
117
  | Cst c       -> pp_horn_const fmt c
118

    
119
  (* Code specific for arrays *)
120
  | Array il    ->
121
     (* An array definition: 
122
	(store (
123
	  ...
124
 	    (store (
125
	       store (
126
	          default_val
127
	       ) 
128
	       idx_n val_n
129
	    ) 
130
	    idx_n-1 val_n-1)
131
	  ... 
132
	  idx_1 val_1
133
	) *)
134
     let rec print fmt (tab, x) =
135
       match tab with
136
       | [] -> pp_default_val fmt v.value_type(* (get_type v) *)
137
       | h::t ->
138
	  fprintf fmt "(store %a %i %a)"
139
	    print (t, (x+1))
140
	    x
141
	    (pp_horn_val ~is_lhs:is_lhs self pp_var) h
142
     in
143
     print fmt (il, 0)
144
       
145
  | Access(tab,index) ->
146
     fprintf fmt "(select %a %a)"
147
       (pp_horn_val ~is_lhs:is_lhs self pp_var) tab
148
       (pp_horn_val ~is_lhs:is_lhs self pp_var) index
149

    
150
  (* Code specific for arrays *)
151
    
152
  | Power (v, n)  -> assert false
153
  | LocalVar v    -> pp_var fmt (rename_machine self v)
154
  | StateVar v    ->
155
     if Types.is_array_type v.var_type
156
     then assert false
157
     else pp_var fmt (rename_machine self ((if is_lhs then rename_next else rename_current) (* self *) v))
158
  | Fun (n, vl)   -> fprintf fmt "%a" (pp_basic_lib_fun n (pp_horn_val self pp_var)) vl
159

    
160
(* Prints a [value] indexed by the suffix list [loop_vars] *)
161
let rec pp_value_suffix self pp_value fmt value =
162
 match value.value_desc with
163
 | Fun (n, vl)  ->
164
   pp_basic_lib_fun n (pp_value_suffix self pp_value) fmt vl
165
 |  _            ->
166
   pp_horn_val self pp_value fmt value
167

    
168
(* type_directed assignment: array vs. statically sized type
169
   - [var_type]: type of variable to be assigned
170
   - [var_name]: name of variable to be assigned
171
   - [value]: assigned value
172
   - [pp_var]: printer for variables
173
*)
174
let pp_assign m pp_var fmt var_name value =
175
  let self = m.mname.node_id in
176
  fprintf fmt "(= %a %a)" 
177
    (pp_horn_val ~is_lhs:true self pp_var) var_name
178
    (pp_value_suffix self pp_var) value
179
    
180

    
181
(* In case of no reset call, we define mid_mem = current_mem *)
182
let pp_no_reset machines m fmt i =
183
  let (n,_) = List.assoc i m.minstances in
184
  let target_machine = List.find (fun m  -> m.mname.node_id = (node_name n)) machines in
185

    
186
  let m_list = 
187
    rename_machine_list
188
      (concat m.mname.node_id i)
189
      (rename_mid_list (full_memory_vars machines target_machine))
190
  in
191
  let c_list =
192
    rename_machine_list
193
      (concat m.mname.node_id i)
194
      (rename_current_list (full_memory_vars machines target_machine))
195
  in
196
  match c_list, m_list with
197
  | [chd], [mhd] ->
198
    fprintf fmt "(= %a %a)"
199
      (pp_horn_var m) mhd
200
      (pp_horn_var m) chd
201
  
202
  | _ -> (
203
    fprintf fmt "@[<v 0>(and @[<v 0>";
204
    List.iter2 (fun mhd chd -> 
205
      fprintf fmt "(= %a %a)@ "
206
      (pp_horn_var m) mhd
207
      (pp_horn_var m) chd
208
    )
209
      m_list
210
      c_list      ;
211
    fprintf fmt ")@]@ @]"
212
  )
213

    
214
let pp_instance_reset machines m fmt i =
215
  let (n,_) = List.assoc i m.minstances in
216
  let target_machine = List.find (fun m  -> m.mname.node_id = (node_name n)) machines in
217
  
218
  fprintf fmt "(%a @[<v 0>%a)@]"
219
    pp_machine_reset_name (node_name n)
220
    (Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) 
221
    (
222
      (rename_machine_list
223
	 (concat m.mname.node_id i)
224
	 (rename_current_list (full_memory_vars machines target_machine))
225
      ) 
226
      @
227
	(rename_machine_list
228
	   (concat m.mname.node_id i)
229
	   (rename_mid_list (full_memory_vars machines target_machine))
230
	)
231
    )
232

    
233
let pp_instance_call machines reset_instances m fmt i inputs outputs =
234
  let self = m.mname.node_id in
235
  try (* stateful node instance *)
236
    begin
237
      let (n,_) = List.assoc i m.minstances in
238
      let target_machine = List.find (fun m  -> m.mname.node_id = node_name n) machines in
239
      (* Checking whether this specific instances has been reset yet *)
240
      if not (List.mem i reset_instances) then
241
	(* If not, declare mem_m = mem_c *)
242
	pp_no_reset machines m fmt i;
243
      
244
      let mems = full_memory_vars machines target_machine in
245
      let rename_mems f = rename_machine_list (concat m.mname.node_id i) (f mems) in
246
      let mid_mems = rename_mems rename_mid_list in
247
      let next_mems = rename_mems rename_next_list in
248

    
249
      match node_name n, inputs, outputs, mid_mems, next_mems with
250
      | "_arrow", [i1; i2], [o], [mem_m], [mem_x] -> begin
251
	fprintf fmt "@[<v 5>(and ";
252
	fprintf fmt "(= %a (ite %a %a %a))"
253
	  (pp_horn_val ~is_lhs:true self (pp_horn_var m)) (mk_val (LocalVar o) o.var_type) (* output var *)
254
	  (pp_horn_var m) mem_m 
255
	  (pp_horn_val self (pp_horn_var m)) i1
256
	  (pp_horn_val self (pp_horn_var m)) i2
257
	;
258
	fprintf fmt "@ ";
259
	fprintf fmt "(= %a false)" (pp_horn_var m) mem_x;
260
	fprintf fmt ")@]"
261
      end
262

    
263
      | node_name_n -> begin
264
	fprintf fmt "(%a @[<v 0>%a%t%a%t%a)@]"
265
	  pp_machine_step_name (node_name n)
266
	  (Utils.fprintf_list ~sep:"@ " (pp_horn_val self (pp_horn_var m))) inputs
267
	  (Utils.pp_final_char_if_non_empty "@ " inputs)
268
	  (Utils.fprintf_list ~sep:"@ " (pp_horn_val self (pp_horn_var m)))
269
	  (List.map (fun v -> mk_val (LocalVar v) v.var_type) outputs)
270
	  (Utils.pp_final_char_if_non_empty "@ " outputs)
271
	  (Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) (mid_mems@next_mems)
272
	
273
      end
274
    end
275
  with Not_found -> ( (* stateless node instance *)
276
    let (n,_) = List.assoc i m.mcalls in
277
    fprintf fmt "(%a @[<v 0>%a%t%a)@]"
278
      pp_machine_stateless_name (node_name n)
279
      (Utils.fprintf_list ~sep:"@ " (pp_horn_val self (pp_horn_var m)))
280
      inputs
281
      (Utils.pp_final_char_if_non_empty "@ " inputs)
282
      (Utils.fprintf_list ~sep:"@ " (pp_horn_val self (pp_horn_var m)))
283
      (List.map (fun v -> mk_val (LocalVar v) v.var_type) outputs)
284
  )
285
    
286
    
287
(* Print the instruction and update the set of reset instances *)
288
let rec pp_machine_instr machines reset_instances (m: machine_t) fmt instr : ident list =
289
  match get_instr_desc instr with
290
  | MComment _ -> reset_instances
291
  | MNoReset i -> (* we assign middle_mem with mem_m. And declare i as reset *)
292
    pp_no_reset machines m fmt i;
293
    i::reset_instances
294
  | MReset i -> (* we assign middle_mem with reset: reset(mem_m) *)
295
    pp_instance_reset machines m fmt i;
296
    i::reset_instances
297
  | MLocalAssign (i,v) ->
298
    pp_assign
299
      m (pp_horn_var m) fmt
300
      (mk_val (LocalVar i) i.var_type) v;
301
    reset_instances
302
  | MStateAssign (i,v) ->
303
    pp_assign
304
      m (pp_horn_var m) fmt
305
      (mk_val (StateVar i) i.var_type) v;
306
    reset_instances
307
  | MStep ([i0], i, vl) when Basic_library.is_internal_fun i (List.map (fun v -> v.value_type) vl) ->
308
    assert false (* This should not happen anymore *)
309
  | MStep (il, i, vl) ->
310
    (* if reset instance, just print the call over mem_m , otherwise declare mem_m =
311
       mem_c and print the call to mem_m *)
312
    pp_instance_call machines reset_instances m fmt i vl il;
313
    reset_instances (* Since this instance call will only happen once, we
314
		       don't have to update reset_instances *)
315

    
316
  | MBranch (g,hl) -> (* (g = tag1 => expr1) and (g = tag2 => expr2) ...
317
			 should not be produced yet. Later, we will have to
318
			 compare the reset_instances of each branch and
319
			 introduced the mem_m = mem_c for branches to do not
320
			 address it while other did. Am I clear ? *)
321
    (* For each branch we obtain the logical encoding, and the information
322
       whether a sub node has been reset or not. If a node has been reset in one
323
       of the branch, then all others have to have the mem_m = mem_c
324
       statement. *)
325
    let self = m.mname.node_id in
326
    let pp_branch fmt (tag, instrs) =
327
      fprintf fmt 
328
	"@[<v 3>(or (not (= %a %a))@ " 
329
	(*"@[<v 3>(=> (= %a %s)@ "*)  (* Issues with some versions of Z3. It
330
					  seems that => within Horn predicate
331
					  may cause trouble. I have hard time
332
					  producing a MWE, so I'll just keep the
333
					  fix here as (not a) or b *)
334
	(pp_horn_val self (pp_horn_var m)) g
335
	pp_horn_tag tag;
336
      let _ (* rs *) = pp_machine_instrs machines reset_instances m fmt instrs in 
337
      fprintf fmt "@])";
338
      () (* rs *)
339
    in
340
    pp_conj pp_branch fmt hl;
341
    reset_instances 
342

    
343
and pp_machine_instrs machines reset_instances m fmt instrs = 
344
  let ppi rs fmt i = pp_machine_instr machines rs m fmt i in
345
  match instrs with
346
  | [x] -> ppi reset_instances fmt x 
347
  | _::_ ->
348
    fprintf fmt "(and @[<v 0>";
349
    let rs = List.fold_left (fun rs i -> 
350
      let rs = ppi rs fmt i in
351
      fprintf fmt "@ ";
352
      rs
353
    )
354
      reset_instances instrs 
355
    in
356
    fprintf fmt "@])";
357
    rs
358

    
359
  | [] -> fprintf fmt "true"; reset_instances
360

    
361
let pp_machine_reset machines fmt m =
362
  let locals = local_memory_vars machines m in
363
  fprintf fmt "@[<v 5>(and @ ";
364

    
365
  (* print "x_m = x_c" for each local memory *)
366
  (Utils.fprintf_list ~sep:"@ " (fun fmt v -> 
367
    fprintf fmt "(= %a %a)"
368
      (pp_horn_var m) (rename_mid v)
369
      (pp_horn_var m) (rename_current v)
370
   )) fmt locals;
371
  fprintf fmt "@ ";
372

    
373
  (* print "child_reset ( associated vars _ {c,m} )" for each subnode.
374
     Special treatment for _arrow: _first = true
375
  *)
376
  (Utils.fprintf_list ~sep:"@ " (fun fmt (id, (n, _)) ->
377
    let name = node_name n in
378
    if name = "_arrow" then ( 
379
      fprintf fmt "(= %s._arrow._first_m true)"
380
	(concat m.mname.node_id id)  
381
    ) else (
382
      let machine_n = get_machine machines name in 
383
      fprintf fmt "(%s_reset @[<hov 0>%a@])" 
384
	name
385
	(Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) 
386
	(rename_machine_list (concat m.mname.node_id id) (reset_vars machines machine_n))
387
    )
388
   )) fmt m.minstances;
389

    
390
  fprintf fmt "@]@ )"
391

    
392

    
393

    
394
(**************************************************************)
395

    
396
let is_stateless m = m.minstances = [] && m.mmemory = []
397

    
398
(* Print the machine m:
399
   two functions: m_init and m_step
400
   - m_init is a predicate over m memories
401
   - m_step is a predicate over old_memories, inputs, new_memories, outputs
402
   We first declare all variables then the two /rules/.
403
*)
404
let print_machine machines fmt m =
405
  if m.mname.node_id = Arrow.arrow_id then
406
    (* We don't print arrow function *)
407
    ()
408
  else
409
    begin
410
      fprintf fmt "; %s@." m.mname.node_id;
411
      
412
      (* Printing variables *)
413
      Utils.fprintf_list ~sep:"@." pp_decl_var fmt
414
	(
415
	  (inout_vars machines m)@
416
	    (rename_current_list (full_memory_vars machines m)) @
417
	    (rename_mid_list (full_memory_vars machines m)) @
418
	    (rename_next_list (full_memory_vars machines m)) @
419
	    (rename_machine_list m.mname.node_id m.mstep.step_locals)
420
	);
421
      pp_print_newline fmt ();
422

    
423
      if is_stateless m then
424
	begin
425
	  (* Declaring single predicate *)
426
	  fprintf fmt "(declare-rel %a (%a))@."
427
	    pp_machine_stateless_name m.mname.node_id
428
	    (Utils.fprintf_list ~sep:" " pp_type)
429
	    (List.map (fun v -> v.var_type) (inout_vars machines m));
430

    
431
          match m.mstep.step_asserts with
432
	  | [] ->
433
	     begin
434

    
435
	       (* Rule for single predicate *)
436
	       fprintf fmt "; Stateless step rule @.";
437
	       fprintf fmt "@[<v 2>(rule (=> @ ";
438
	       ignore (pp_machine_instrs machines ([] (* No reset info for stateless nodes *) )  m fmt m.mstep.step_instrs);
439
	       fprintf fmt "@ (%a @[<v 0>%a)@]@]@.))@.@."
440
		 pp_machine_stateless_name m.mname.node_id
441
		 (Utils.fprintf_list ~sep:" " (pp_horn_var m)) (inout_vars machines m);
442
	     end
443
	  | assertsl ->
444
	     begin
445
	       let pp_val = pp_horn_val ~is_lhs:true m.mname.node_id (pp_horn_var m) in
446
	       
447
	       fprintf fmt "; Stateless step rule with Assertions @.";
448
	       (*Rule for step*)
449
	       fprintf fmt "@[<v 2>(rule (=> @ (and @ ";
450
	       ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
451
	       fprintf fmt "@. %a)@ (%a @[<v 0>%a)@]@]@.))@.@." (pp_conj pp_val) assertsl
452
		 pp_machine_stateless_name m.mname.node_id
453
		 (Utils.fprintf_list ~sep:" " (pp_horn_var m)) (step_vars machines m);
454
	  
455
	     end
456
	       
457
	end
458
      else
459
	begin
460
	  (* Declaring predicate *)
461
	  fprintf fmt "(declare-rel %a (%a))@."
462
	    pp_machine_reset_name m.mname.node_id
463
	    (Utils.fprintf_list ~sep:" " pp_type)
464
	    (List.map (fun v -> v.var_type) (reset_vars machines m));
465

    
466
	  fprintf fmt "(declare-rel %a (%a))@."
467
	    pp_machine_step_name m.mname.node_id
468
	    (Utils.fprintf_list ~sep:" " pp_type)
469
	    (List.map (fun v -> v.var_type) (step_vars machines m));
470

    
471
	  pp_print_newline fmt ();
472

    
473
	  (* Rule for reset *)
474
	  fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a @[<v 0>%a)@]@]@.))@.@."
475
	    (pp_machine_reset machines) m 
476
	    pp_machine_reset_name m.mname.node_id
477
	    (Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) (reset_vars machines m);
478

    
479
          match m.mstep.step_asserts with
480
	  | [] ->
481
	     begin
482
	       fprintf fmt "; Step rule @.";
483
	       (* Rule for step*)
484
	       fprintf fmt "@[<v 2>(rule (=> @ ";
485
	       ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
486
	       fprintf fmt "@ (%a @[<v 0>%a)@]@]@.))@.@."
487
		 pp_machine_step_name m.mname.node_id
488
		 (Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) (step_vars machines m);
489
	     end
490
	  | assertsl -> 
491
	     begin
492
	       let pp_val = pp_horn_val ~is_lhs:true m.mname.node_id (pp_horn_var m) in
493
	       (* print_string pp_val; *)
494
	       fprintf fmt "; Step rule with Assertions @.";
495
	       
496
	       (*Rule for step*)
497
	       fprintf fmt "@[<v 2>(rule (=> @ (and @ ";
498
	       ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
499
	       fprintf fmt "@. %a)@ (%a @[<v 0>%a)@]@]@.))@.@." (pp_conj pp_val) assertsl
500
		 pp_machine_step_name m.mname.node_id
501
		 (Utils.fprintf_list ~sep:" " (pp_horn_var m)) (step_vars machines m);
502
	     end
503
	       
504
	       
505
	end
506
    end
507

    
508

    
509
let mk_flags arity =
510
  let b_range =
511
   let rec range i j =
512
     if i > arity then [] else i :: (range (i+1) j) in
513
   range 2 arity;
514
 in
515
 List.fold_left (fun acc x -> acc ^ " false") "true" b_range
516

    
517

    
518
  (*Get sfunction infos from command line*)
519
let get_sf_info() =
520
  let splitted = Str.split (Str.regexp "@") !Options.sfunction in
521
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. sfunction name: %s@," !Options.sfunction);
522
  let sf_name, flags, arity = match splitted with
523
      [h;flg;par] -> h, flg, par
524
    | _ -> failwith "Wrong Sfunction info"
525

    
526
  in
527
  Log.report ~level:1 (fun fmt -> fprintf fmt "... sf_name: %s@, .. flags: %s@ .. arity: %s@," sf_name flags arity);
528
  sf_name, flags, arity
529

    
530

    
531
    (*a function to print the rules in case we have an s-function*)
532
  let print_sfunction machines fmt m =
533
      if m.mname.node_id = Arrow.arrow_id then
534
        (* We don't print arrow function *)
535
        ()
536
      else
537
        begin
538
          Format.fprintf fmt "; SFUNCTION@.";
539
          Format.fprintf fmt "; %s@." m.mname.node_id;
540
          Format.fprintf fmt "; EndPoint Predicate %s." !Options.sfunction;
541

    
542
          (* Check if there is annotation for s-function *)
543
          if m.mannot != [] then(
544
              Format.fprintf fmt "; @[%a@]@]@\n" (Utils.fprintf_list ~sep:"@ " Printers.pp_s_function) m.mannot;
545
            );
546

    
547
       (* Printing variables *)
548
          Utils.fprintf_list ~sep:"@." pp_decl_var fmt
549
                             ((step_vars machines m)@
550
    	                        (rename_machine_list m.mname.node_id m.mstep.step_locals));
551
          Format.pp_print_newline fmt ();
552
          let sf_name, flags, arity = get_sf_info() in
553

    
554
       if is_stateless m then
555
         begin
556
           (* Declaring single predicate *)
557
           Format.fprintf fmt "(declare-rel %a (%a))@."
558
    	                  pp_machine_stateless_name m.mname.node_id
559
    	                  (Utils.fprintf_list ~sep:" " pp_type)
560
    	                  (List.map (fun v -> v.var_type) (reset_vars machines m));
561
           Format.pp_print_newline fmt ();
562
           (* Rule for single predicate *)
563
           let str_flags = sf_name ^ " " ^ mk_flags (int_of_string flags) in
564
           Format.fprintf fmt "@[<v 2>(rule (=> @ (%s %a) (%a %a)@]@.))@.@."
565
                          str_flags
566
                          (Utils.fprintf_list ~sep:" " (pp_horn_var m)) (reset_vars machines m)
567
	                  pp_machine_stateless_name m.mname.node_id
568
	                  (Utils.fprintf_list ~sep:" " (pp_horn_var m)) (reset_vars machines m);
569
         end
570
      else
571
         begin
572
           (* Declaring predicate *)
573
           Format.fprintf fmt "(declare-rel %a (%a))@."
574
    	                  pp_machine_reset_name m.mname.node_id
575
    	                  (Utils.fprintf_list ~sep:" " pp_type)
576
    	                  (List.map (fun v -> v.var_type) (inout_vars machines m));
577

    
578
           Format.fprintf fmt "(declare-rel %a (%a))@."
579
    	                  pp_machine_step_name m.mname.node_id
580
    	                  (Utils.fprintf_list ~sep:" " pp_type)
581
    	                  (List.map (fun v -> v.var_type) (step_vars machines m));
582

    
583
           Format.pp_print_newline fmt ();
584
          (* Adding assertions *)
585
           match m.mstep.step_asserts with
586
	  | [] ->
587
	    begin
588

    
589
	      (* Rule for step*)
590
	      fprintf fmt "@[<v 2>(rule (=> @ ";
591
	      ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
592
	      fprintf fmt "@ (%a @[<v 0>%a)@]@]@.))@.@."
593
		pp_machine_step_name m.mname.node_id
594
		(Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) (step_vars machines m);
595
	    end
596
	  | assertsl ->
597
	    begin
598
	      let pp_val = pp_horn_val ~is_lhs:true m.mname.node_id (pp_horn_var m) in
599
	      (* print_string pp_val; *)
600
	      fprintf fmt "; with Assertions @.";
601

    
602
	      (*Rule for step*)
603
	      fprintf fmt "@[<v 2>(rule (=> @ (and @ ";
604
	      ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
605
	      fprintf fmt "@. %a)(%a @[<v 0>%a)@]@]@.))@.@." (pp_conj pp_val) assertsl
606
		pp_machine_step_name m.mname.node_id
607
		(Utils.fprintf_list ~sep:" " (pp_horn_var m)) (step_vars machines m);
608
	    end
609

    
610
         end
611

    
612
        end
613

    
614

    
615
(**************** XML printing functions *************)
616

    
617
	  let rec pp_xml_expr fmt expr =
618
  (match expr.expr_annot with 
619
  | None -> fprintf fmt "%t" 
620
  | Some ann -> fprintf fmt "@[(%a %t)@]" pp_xml_expr_annot ann)
621
    (fun fmt -> 
622
      match expr.expr_desc with
623
    | Expr_const c -> Printers.pp_const fmt c
624
    | Expr_ident id -> fprintf fmt "%s" id
625
    | Expr_array a -> fprintf fmt "[%a]" pp_xml_tuple a
626
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_xml_expr a Dimension.pp_dimension d
627
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_xml_expr a Dimension.pp_dimension d
628
    | Expr_tuple el -> fprintf fmt "(%a)" pp_xml_tuple el
629
    | Expr_ite (c, t, e) -> fprintf fmt "@[<hov 1>(if %a then@ @[<hov 2>%a@]@ else@ @[<hov 2>%a@]@])" pp_xml_expr c pp_xml_expr t pp_xml_expr e
630
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_xml_expr e1 pp_xml_expr e2
631
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_xml_expr e1 pp_xml_expr e2
632
    | Expr_pre e -> fprintf fmt "pre %a" pp_xml_expr e
633
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_xml_expr e l id
634
    | Expr_merge (id, hl) -> 
635
      fprintf fmt "merge %s %a" id pp_xml_handlers hl
636
    | Expr_appl (id, e, r) -> pp_xml_app fmt id e r
637
    )
638
and pp_xml_tuple fmt el =
639
 Utils.fprintf_list ~sep:"," pp_xml_expr fmt el
640

    
641
and pp_xml_handler fmt (t, h) =
642
 fprintf fmt "(%s -> %a)" t pp_xml_expr h
643

    
644
and pp_xml_handlers fmt hl =
645
 Utils.fprintf_list ~sep:" " pp_xml_handler fmt hl
646

    
647
and pp_xml_app fmt id e r =
648
  match r with
649
  | None -> pp_xml_call fmt id e
650
  | Some c -> fprintf fmt "%t every (%a)" (fun fmt -> pp_xml_call fmt id e) pp_xml_expr c 
651

    
652
and pp_xml_call fmt id e =
653
  match id, e.expr_desc with
654
  | "+", Expr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_xml_expr e1 pp_xml_expr e2
655
  | "uminus", _ -> fprintf fmt "(- %a)" pp_xml_expr e
656
  | "-", Expr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_xml_expr e1 pp_xml_expr e2
657
  | "*", Expr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_xml_expr e1 pp_xml_expr e2
658
  | "/", Expr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_xml_expr e1 pp_xml_expr e2
659
  | "mod", Expr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_xml_expr e1 pp_xml_expr e2
660
  | "&&", Expr_tuple([e1;e2]) -> fprintf fmt "(%a and %a)" pp_xml_expr e1 pp_xml_expr e2
661
  | "||", Expr_tuple([e1;e2]) -> fprintf fmt "(%a or %a)" pp_xml_expr e1 pp_xml_expr e2
662
  | "xor", Expr_tuple([e1;e2]) -> fprintf fmt "(%a xor %a)" pp_xml_expr e1 pp_xml_expr e2
663
  | "impl", Expr_tuple([e1;e2]) -> fprintf fmt "(%a => %a)" pp_xml_expr e1 pp_xml_expr e2
664
  | "<", Expr_tuple([e1;e2]) -> fprintf fmt "(%a &lt; %a)" pp_xml_expr e1 pp_xml_expr e2
665
  | "<=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a &lt;= %a)" pp_xml_expr e1 pp_xml_expr e2
666
  | ">", Expr_tuple([e1;e2]) -> fprintf fmt "(%a &gt; %a)" pp_xml_expr e1 pp_xml_expr e2
667
  | ">=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a &gt;= %a)" pp_xml_expr e1 pp_xml_expr e2
668
  | "!=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_xml_expr e1 pp_xml_expr e2
669
  | "=", Expr_tuple([e1;e2]) -> fprintf fmt "(%a = %a)" pp_xml_expr e1 pp_xml_expr e2
670
  | "not", _ -> fprintf fmt "(not %a)" pp_xml_expr e
671
  | _, Expr_tuple _ -> fprintf fmt "%s %a" id pp_xml_expr e
672
  | _ -> fprintf fmt "%s (%a)" id pp_xml_expr e
673

    
674
and pp_xml_eexpr fmt e =
675
  fprintf fmt "%a%t %a"
676
    (Utils.fprintf_list ~sep:"; " Printers.pp_quantifiers) e.eexpr_quantifiers
677
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
678
    pp_xml_expr e.eexpr_qfexpr
679

    
680
and  pp_xml_sf_value fmt e =
681
   fprintf fmt "%a"
682
     (* (Utils.fprintf_list ~sep:"; " pp_xml_quantifiers) e.eexpr_quantifiers *)
683
     (* (fun fmt -> match e.eexpr_quantifiers *)
684
     (*             with [] -> () *)
685
     (*                | _ -> fprintf fmt ";") *)
686
     pp_xml_expr e.eexpr_qfexpr
687

    
688
and pp_xml_s_function fmt expr_ann =
689
  let pp_xml_annot fmt (kwds, ee) =
690
    Format.fprintf fmt " %t : %a"
691
                   (fun fmt -> match kwds with
692
                               | [] -> assert false
693
                               | [x] -> Format.pp_print_string fmt x
694
                               | _ -> Format.fprintf fmt "%a" (Utils.fprintf_list ~sep:"/" Format.pp_print_string) kwds)
695
                   pp_xml_sf_value ee
696
  in
697
  Utils.fprintf_list ~sep:"@ " pp_xml_annot fmt expr_ann.annots
698

    
699
and pp_xml_expr_annot fmt expr_ann =
700
  let pp_xml_annot fmt (kwds, ee) =
701
    Format.fprintf fmt "(*! %t: %a; *)"
702
      (fun fmt -> match kwds with | [] -> assert false | [x] -> Format.pp_print_string fmt x | _ -> Format.fprintf fmt "/%a/" (Utils.fprintf_list ~sep:"/" Format.pp_print_string) kwds)
703
      pp_xml_eexpr ee
704
  in
705
  Utils.fprintf_list ~sep:"@ " pp_xml_annot fmt expr_ann.annots
706

    
707

    
708
(* Local Variables: *)
709
(* compile-command:"make -C ../../.." *)
710
(* End: *)