Project

General

Profile

Download (26.5 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

    
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 from Euclidean operators
71
       (a mod_M b) - ((a mod_M b > 0 && a < 0) ? abs(b) : 0)            
72
    *)
73
    Format.fprintf fmt "(- (mod %a %a) (ite (and (> (mod %a %a) 0) (< %a 0)) (abs %a) 0))"
74
      pp_val v1 pp_val v2
75
      pp_val v1 pp_val v2
76
      pp_val v1
77
      pp_val v2
78
  else
79
    Format.fprintf fmt "(mod %a %a)" pp_val v1 pp_val v2
80

    
81
let pp_div pp_val v1 v2 fmt =
82
  if Types.is_int_type v1.value_type &&  not !Options.integer_div_euclidean then
83
    (* C semantics: converting it from Euclidean operators
84
       (a - (a mod_C b)) div_M b
85
    *)
86
    Format.fprintf fmt "(div (- %a %t) %a)"
87
      pp_val v1
88
      (pp_mod pp_val v1 v2)
89
      pp_val v2
90
  else
91
    Format.fprintf fmt "(div %a %a)" pp_val v1 pp_val v2
92

    
93

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

    
112
*)
113

    
114

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

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

    
154
  (* Code specific for arrays *)
155
    
156
  | Power (v, n)  -> assert false
157
  | Var v    ->
158
     if is_memory m v then
159
       if Types.is_array_type v.var_type
160
       then assert false
161
       else pp_var fmt (rename_machine self ((if is_lhs then rename_next else rename_current) (* self *) v))
162
     else
163
       pp_var fmt (rename_machine self v)
164
    
165
  | Fun (n, vl)   -> fprintf fmt "%a" (pp_basic_lib_fun n (pp_horn_val m self pp_var)) vl
166

    
167
(* Prints a [value] indexed by the suffix list [loop_vars] *)
168
let rec pp_value_suffix m self pp_value fmt value =
169
 match value.value_desc with
170
 | Fun (n, vl)  ->
171
    pp_basic_lib_fun n (pp_value_suffix m self pp_value) fmt vl
172
 |  _            ->
173
   pp_horn_val m self pp_value fmt value
174

    
175
(* type_directed assignment: array vs. statically sized type
176
   - [var_type]: type of variable to be assigned
177
   - [var_name]: name of variable to be assigned
178
   - [value]: assigned value
179
   - [pp_var]: printer for variables
180
*)
181
let pp_assign m pp_var fmt var_name value =
182
  let self = m.mname.node_id in
183
  fprintf fmt "(= %a %a)" 
184
    (pp_horn_val ~is_lhs:true m self pp_var) var_name
185
    (pp_value_suffix m self pp_var) value
186
    
187

    
188
(* In case of no reset call, we define mid_mem = current_mem *)
189
let pp_no_reset machines m fmt i =
190
  let (n,_) = List.assoc i m.minstances in
191
  let target_machine = List.find (fun m  -> m.mname.node_id = (node_name n)) machines in
192

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

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

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

    
256
      match node_name n, inputs, outputs, mid_mems, next_mems with
257
      | "_arrow", [i1; i2], [o], [mem_m], [mem_x] -> begin
258
	fprintf fmt "@[<v 5>(and ";
259
	fprintf fmt "(= %a (ite %a %a %a))"
260
	  (pp_horn_val ~is_lhs:true m self (pp_horn_var m)) (mk_val (Var o) o.var_type) (* output var *)
261
	  (pp_horn_var m) mem_m 
262
	  (pp_horn_val m self (pp_horn_var m)) i1
263
	  (pp_horn_val m self (pp_horn_var m)) i2
264
	;
265
	fprintf fmt "@ ";
266
	fprintf fmt "(= %a false)" (pp_horn_var m) mem_x;
267
	fprintf fmt ")@]"
268
      end
269

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

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

    
350
and pp_machine_instrs machines reset_instances m fmt instrs = 
351
  let ppi rs fmt i = pp_machine_instr machines rs m fmt i in
352
  match instrs with
353
  | [x] -> ppi reset_instances fmt x 
354
  | _::_ ->
355
    fprintf fmt "(and @[<v 0>";
356
    let rs = List.fold_left (fun rs i -> 
357
      let rs = ppi rs fmt i in
358
      fprintf fmt "@ ";
359
      rs
360
    )
361
      reset_instances instrs 
362
    in
363
    fprintf fmt "@])";
364
    rs
365

    
366
  | [] -> fprintf fmt "true"; reset_instances
367

    
368
let pp_machine_reset machines fmt m =
369
  let locals = local_memory_vars machines m in
370
  fprintf fmt "@[<v 5>(and @ ";
371

    
372
  (* print "x_m = x_c" for each local memory *)
373
  (Utils.fprintf_list ~sep:"@ " (fun fmt v -> 
374
    fprintf fmt "(= %a %a)"
375
      (pp_horn_var m) (rename_mid v)
376
      (pp_horn_var m) (rename_current v)
377
   )) fmt locals;
378
  fprintf fmt "@ ";
379

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

    
397
  fprintf fmt "@]@ )"
398

    
399

    
400

    
401
(**************************************************************)
402

    
403

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

    
429
      if is_stateless m then
430
	begin
431
	  (* Declaring single predicate *)
432
	  fprintf fmt "(declare-rel %a (%a))@."
433
	    pp_machine_stateless_name m.mname.node_id
434
	    (Utils.fprintf_list ~sep:" " pp_type)
435
	    (List.map (fun v -> v.var_type) (inout_vars machines m));
436

    
437
          match m.mstep.step_asserts with
438
	  | [] ->
439
	     begin
440

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

    
472
	  fprintf fmt "(declare-rel %a (%a))@."
473
	    pp_machine_step_name m.mname.node_id
474
	    (Utils.fprintf_list ~sep:" " pp_type)
475
	    (List.map (fun v -> v.var_type) (step_vars machines m));
476

    
477
	  pp_print_newline fmt ();
478

    
479
	  (* Rule for reset *)
480
	  fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a @[<v 0>%a)@]@]@.))@.@."
481
	    (pp_machine_reset machines) m 
482
	    pp_machine_reset_name m.mname.node_id
483
	    (Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) (reset_vars machines m);
484

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

    
514

    
515
let mk_flags arity =
516
  let b_range =
517
   let rec range i j =
518
     if i > arity then [] else i :: (range (i+1) j) in
519
   range 2 arity;
520
 in
521
 List.fold_left (fun acc x -> acc ^ " false") "true" b_range
522

    
523

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

    
532
  in
533
  Log.report ~level:1 (fun fmt -> fprintf fmt "... sf_name: %s@, .. flags: %s@ .. arity: %s@," sf_name flags arity);
534
  sf_name, flags, arity
535

    
536

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

    
548
          (* Check if there is annotation for s-function *)
549
          if m.mannot != [] then(
550
              Format.fprintf fmt "; @[%a@]@]@\n" (Utils.fprintf_list ~sep:"@ " Printers.pp_s_function) m.mannot;
551
            );
552

    
553
       (* Printing variables *)
554
          Utils.fprintf_list ~sep:"@." pp_decl_var fmt
555
                             ((step_vars machines m)@
556
    	                        (rename_machine_list m.mname.node_id m.mstep.step_locals));
557
          Format.pp_print_newline fmt ();
558
          let sf_name, flags, arity = get_sf_info() in
559

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

    
584
           Format.fprintf fmt "(declare-rel %a (%a))@."
585
    	                  pp_machine_step_name m.mname.node_id
586
    	                  (Utils.fprintf_list ~sep:" " pp_type)
587
    	                  (List.map (fun v -> v.var_type) (step_vars machines m));
588

    
589
           Format.pp_print_newline fmt ();
590
          (* Adding assertions *)
591
           match m.mstep.step_asserts with
592
	  | [] ->
593
	    begin
594

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

    
608
	      (*Rule for step*)
609
	      fprintf fmt "@[<v 2>(rule (=> @ (and @ ";
610
	      ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
611
	      fprintf fmt "@. %a)(%a @[<v 0>%a)@]@]@.))@.@." (pp_conj pp_val) assertsl
612
		pp_machine_step_name m.mname.node_id
613
		(Utils.fprintf_list ~sep:" " (pp_horn_var m)) (step_vars machines m);
614
	    end
615

    
616
         end
617

    
618
        end
619

    
620

    
621
(**************** XML printing functions *************)
622

    
623
	  let rec pp_xml_expr fmt expr =
624
  (match expr.expr_annot with 
625
  | None -> fprintf fmt "%t" 
626
  | Some ann -> fprintf fmt "@[(%a %t)@]" pp_xml_expr_annot ann)
627
    (fun fmt -> 
628
      match expr.expr_desc with
629
    | Expr_const c -> Printers.pp_const fmt c
630
    | Expr_ident id -> fprintf fmt "%s" id
631
    | Expr_array a -> fprintf fmt "[%a]" pp_xml_tuple a
632
    | Expr_access (a, d) -> fprintf fmt "%a[%a]" pp_xml_expr a Dimension.pp_dimension d
633
    | Expr_power (a, d) -> fprintf fmt "(%a^%a)" pp_xml_expr a Dimension.pp_dimension d
634
    | Expr_tuple el -> fprintf fmt "(%a)" pp_xml_tuple el
635
    | 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
636
    | Expr_arrow (e1, e2) -> fprintf fmt "(%a -> %a)" pp_xml_expr e1 pp_xml_expr e2
637
    | Expr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_xml_expr e1 pp_xml_expr e2
638
    | Expr_pre e -> fprintf fmt "pre %a" pp_xml_expr e
639
    | Expr_when (e, id, l) -> fprintf fmt "%a when %s(%s)" pp_xml_expr e l id
640
    | Expr_merge (id, hl) -> 
641
      fprintf fmt "merge %s %a" id pp_xml_handlers hl
642
    | Expr_appl (id, e, r) -> pp_xml_app fmt id e r
643
    )
644
and pp_xml_tuple fmt el =
645
 Utils.fprintf_list ~sep:"," pp_xml_expr fmt el
646

    
647
and pp_xml_handler fmt (t, h) =
648
 fprintf fmt "(%s -> %a)" t pp_xml_expr h
649

    
650
and pp_xml_handlers fmt hl =
651
 Utils.fprintf_list ~sep:" " pp_xml_handler fmt hl
652

    
653
and pp_xml_app fmt id e r =
654
  match r with
655
  | None -> pp_xml_call fmt id e
656
  | Some c -> fprintf fmt "%t every (%a)" (fun fmt -> pp_xml_call fmt id e) pp_xml_expr c 
657

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

    
680
and pp_xml_eexpr fmt e =
681
  fprintf fmt "%a%t %a"
682
    (Utils.fprintf_list ~sep:"; " Printers.pp_quantifiers) e.eexpr_quantifiers
683
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
684
    pp_xml_expr e.eexpr_qfexpr
685

    
686
and  pp_xml_sf_value fmt e =
687
   fprintf fmt "%a"
688
     (* (Utils.fprintf_list ~sep:"; " pp_xml_quantifiers) e.eexpr_quantifiers *)
689
     (* (fun fmt -> match e.eexpr_quantifiers *)
690
     (*             with [] -> () *)
691
     (*                | _ -> fprintf fmt ";") *)
692
     pp_xml_expr e.eexpr_qfexpr
693

    
694
and pp_xml_s_function fmt expr_ann =
695
  let pp_xml_annot fmt (kwds, ee) =
696
    Format.fprintf fmt " %t : %a"
697
                   (fun fmt -> match kwds with
698
                               | [] -> assert false
699
                               | [x] -> Format.pp_print_string fmt x
700
                               | _ -> Format.fprintf fmt "%a" (Utils.fprintf_list ~sep:"/" Format.pp_print_string) kwds)
701
                   pp_xml_sf_value ee
702
  in
703
  Utils.fprintf_list ~sep:"@ " pp_xml_annot fmt expr_ann.annots
704

    
705
and pp_xml_expr_annot fmt expr_ann =
706
  let pp_xml_annot fmt (kwds, ee) =
707
    Format.fprintf fmt "(*! %t: %a; *)"
708
      (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)
709
      pp_xml_eexpr ee
710
  in
711
  Utils.fprintf_list ~sep:"@ " pp_xml_annot fmt expr_ann.annots
712

    
713

    
714
(* Local Variables: *)
715
(* compile-command:"make -C ../../.." *)
716
(* End: *)
(5-5/6)