Project

General

Profile

Download (26.6 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 _ 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 pp_horn_const fmt c =
43
  match c with
44
    | Const_int i    -> pp_print_int fmt i
45
    | Const_real r   -> Real.pp fmt r
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 _ -> (* 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 _ -> assert false
65
  | Types.Ttuple _ -> 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 _ -> 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
  | ResetFlag ->
167
    (* TODO: handle reset flag *)
168
    assert false
169

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

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

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

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

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

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

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

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

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

    
357
and pp_machine_instrs machines reset_instances m fmt instrs = 
358
  let ppi rs fmt i = pp_machine_instr machines rs m fmt i in
359
  match instrs with
360
  | [x] -> ppi reset_instances fmt x 
361
  | _::_ ->
362
    fprintf fmt "(and @[<v 0>";
363
    let rs = List.fold_left (fun rs i -> 
364
      let rs = ppi rs fmt i in
365
      fprintf fmt "@ ";
366
      rs
367
    )
368
      reset_instances instrs 
369
    in
370
    fprintf fmt "@])";
371
    rs
372

    
373
  | [] -> fprintf fmt "true"; reset_instances
374

    
375
let pp_machine_reset machines fmt m =
376
  let locals = local_memory_vars m in
377
  fprintf fmt "@[<v 5>(and @ ";
378

    
379
  (* print "x_m = x_c" for each local memory *)
380
  (Utils.fprintf_list ~sep:"@ " (fun fmt v -> 
381
    fprintf fmt "(= %a %a)"
382
      (pp_horn_var m) (rename_mid v)
383
      (pp_horn_var m) (rename_current v)
384
   )) fmt locals;
385
  fprintf fmt "@ ";
386

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

    
404
  fprintf fmt "@]@ )"
405

    
406

    
407

    
408
(**************************************************************)
409

    
410

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

    
436
      if is_stateless m then
437
	begin
438
	  (* Declaring single predicate *)
439
	  fprintf fmt "(declare-rel %a (%a))@."
440
	    pp_machine_stateless_name m.mname.node_id
441
	    (Utils.fprintf_list ~sep:" " pp_type)
442
	    (List.map (fun v -> v.var_type) (inout_vars m));
443

    
444
          match m.mstep.step_asserts with
445
	  | [] ->
446
	     begin
447

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

    
479
	  fprintf fmt "(declare-rel %a (%a))@."
480
	    pp_machine_step_name m.mname.node_id
481
	    (Utils.fprintf_list ~sep:" " pp_type)
482
	    (List.map (fun v -> v.var_type) (step_vars machines m));
483

    
484
	  pp_print_newline fmt ();
485

    
486
	  (* Rule for reset *)
487
	  fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a @[<v 0>%a)@]@]@.))@.@."
488
	    (pp_machine_reset machines) m 
489
	    pp_machine_reset_name m.mname.node_id
490
	    (Utils.fprintf_list ~sep:"@ " (pp_horn_var m)) (reset_vars machines m);
491

    
492
          match m.mstep.step_asserts with
493
	  | [] ->
494
	     begin
495
	       fprintf fmt "; Step rule @.";
496
	       (* Rule for step*)
497
	       fprintf fmt "@[<v 2>(rule (=> @ ";
498
	       ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
499
	       fprintf fmt "@ (%a @[<v 0>%a)@]@]@.))@.@."
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
	  | assertsl -> 
504
	     begin
505
	       let pp_val = pp_horn_val ~is_lhs:true m m.mname.node_id (pp_horn_var m) in
506
	       (* print_string pp_val; *)
507
	       fprintf fmt "; Step rule with Assertions @.";
508
	       
509
	       (*Rule for step*)
510
	       fprintf fmt "@[<v 2>(rule (=> @ (and @ ";
511
	       ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
512
	       fprintf fmt "@. %a)@ (%a @[<v 0>%a)@]@]@.))@.@." (pp_conj pp_val) assertsl
513
		 pp_machine_step_name m.mname.node_id
514
		 (Utils.fprintf_list ~sep:" " (pp_horn_var m)) (step_vars machines m);
515
	     end
516
	       
517
	       
518
	end
519
    end
520

    
521

    
522
let mk_flags arity =
523
  let b_range =
524
   let rec range i j =
525
     if i > arity then [] else i :: (range (i+1) j) in
526
   range 2 arity;
527
 in
528
 List.fold_left (fun acc _ -> acc ^ " false") "true" b_range
529

    
530

    
531
  (*Get sfunction infos from command line*)
532
let get_sf_info() =
533
  let splitted = Str.split (Str.regexp "@") !Options.sfunction in
534
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. sfunction name: %s@," !Options.sfunction);
535
  let sf_name, flags, arity = match splitted with
536
      [h;flg;par] -> h, flg, par
537
    | _ -> failwith "Wrong Sfunction info"
538

    
539
  in
540
  Log.report ~level:1 (fun fmt -> fprintf fmt "... sf_name: %s@, .. flags: %s@ .. arity: %s@," sf_name flags arity);
541
  sf_name, flags, arity
542

    
543

    
544
    (*a function to print the rules in case we have an s-function*)
545
  let print_sfunction machines fmt m =
546
      if m.mname.node_id = Arrow.arrow_id then
547
        (* We don't print arrow function *)
548
        ()
549
      else
550
        begin
551
          Format.fprintf fmt "; SFUNCTION@.";
552
          Format.fprintf fmt "; %s@." m.mname.node_id;
553
          Format.fprintf fmt "; EndPoint Predicate %s." !Options.sfunction;
554

    
555
          (* Check if there is annotation for s-function *)
556
          if m.mannot != [] then(
557
              Format.fprintf fmt "; @[%a@]@]@\n" (Utils.fprintf_list ~sep:"@ " Printers.pp_s_function) m.mannot;
558
            );
559

    
560
       (* Printing variables *)
561
          Utils.fprintf_list ~sep:"@." pp_decl_var fmt
562
                             ((step_vars machines m)@
563
    	                        (rename_machine_list m.mname.node_id m.mstep.step_locals));
564
          Format.pp_print_newline fmt ();
565
          let sf_name, flags, _ = get_sf_info() in
566

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

    
591
           Format.fprintf fmt "(declare-rel %a (%a))@."
592
    	                  pp_machine_step_name m.mname.node_id
593
    	                  (Utils.fprintf_list ~sep:" " pp_type)
594
    	                  (List.map (fun v -> v.var_type) (step_vars machines m));
595

    
596
           Format.pp_print_newline fmt ();
597
          (* Adding assertions *)
598
           match m.mstep.step_asserts with
599
	  | [] ->
600
	    begin
601

    
602
	      (* Rule for step*)
603
	      fprintf fmt "@[<v 2>(rule (=> @ ";
604
	      ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
605
	      fprintf fmt "@ (%a @[<v 0>%a)@]@]@.))@.@."
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
	  | assertsl ->
610
	    begin
611
	      let pp_val = pp_horn_val ~is_lhs:true m m.mname.node_id (pp_horn_var m) in
612
	      (* print_string pp_val; *)
613
	      fprintf fmt "; with Assertions @.";
614

    
615
	      (*Rule for step*)
616
	      fprintf fmt "@[<v 2>(rule (=> @ (and @ ";
617
	      ignore (pp_machine_instrs machines [] m fmt m.mstep.step_instrs);
618
	      fprintf fmt "@. %a)(%a @[<v 0>%a)@]@]@.))@.@." (pp_conj pp_val) assertsl
619
		pp_machine_step_name m.mname.node_id
620
		(Utils.fprintf_list ~sep:" " (pp_horn_var m)) (step_vars machines m);
621
	    end
622

    
623
         end
624

    
625
        end
626

    
627

    
628
(**************** XML printing functions *************)
629

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

    
654
and pp_xml_handler fmt (t, h) =
655
 fprintf fmt "(%s -> %a)" t pp_xml_expr h
656

    
657
and pp_xml_handlers fmt hl =
658
 Utils.fprintf_list ~sep:" " pp_xml_handler fmt hl
659

    
660
and pp_xml_app fmt id e r =
661
  match r with
662
  | None -> pp_xml_call fmt id e
663
  | Some c -> fprintf fmt "%t every (%a)" (fun fmt -> pp_xml_call fmt id e) pp_xml_expr c 
664

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

    
687
and pp_xml_eexpr fmt e =
688
  fprintf fmt "%a%t %a"
689
    (Utils.fprintf_list ~sep:"; " Printers.pp_quantifiers) e.eexpr_quantifiers
690
    (fun fmt -> match e.eexpr_quantifiers with [] -> () | _ -> fprintf fmt ";")
691
    pp_xml_expr e.eexpr_qfexpr
692

    
693
and  pp_xml_sf_value fmt e =
694
   fprintf fmt "%a"
695
     (* (Utils.fprintf_list ~sep:"; " pp_xml_quantifiers) e.eexpr_quantifiers *)
696
     (* (fun fmt -> match e.eexpr_quantifiers *)
697
     (*             with [] -> () *)
698
     (*                | _ -> fprintf fmt ";") *)
699
     pp_xml_expr e.eexpr_qfexpr
700

    
701
and pp_xml_s_function fmt expr_ann =
702
  let pp_xml_annot fmt (kwds, ee) =
703
    Format.fprintf fmt " %t : %a"
704
                   (fun fmt -> match kwds with
705
                               | [] -> assert false
706
                               | [x] -> Format.pp_print_string fmt x
707
                               | _ -> Format.fprintf fmt "%a" (Utils.fprintf_list ~sep:"/" Format.pp_print_string) kwds)
708
                   pp_xml_sf_value ee
709
  in
710
  Utils.fprintf_list ~sep:"@ " pp_xml_annot fmt expr_ann.annots
711

    
712
and pp_xml_expr_annot fmt expr_ann =
713
  let pp_xml_annot fmt (kwds, ee) =
714
    Format.fprintf fmt "(*! %t: %a; *)"
715
      (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)
716
      pp_xml_eexpr ee
717
  in
718
  Utils.fprintf_list ~sep:"@ " pp_xml_annot fmt expr_ann.annots
719

    
720

    
721
(* Local Variables: *)
722
(* compile-command:"make -C ../../.." *)
723
(* End: *)
(4-4/5)