Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / Horn / horn_backend.ml @ 53206908

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

    
15
open Format
16
open LustreSpec
17
open Corelang
18
open Machine_code
19

    
20

    
21
let pp_machine_init_name fmt id = fprintf fmt "%s_init" id
22
let pp_machine_step_name fmt id = fprintf fmt "%s_step" id
23
let pp_machine_stateless_name fmt id = fprintf fmt "%s" id
24

    
25
let pp_type fmt t =
26
  match (Types.repr t).Types.tdesc with
27
  | Types.Tbool           -> Format.fprintf fmt "Bool"
28
  | Types.Tint            -> Format.fprintf fmt "Int"
29
  | Types.Treal           -> Format.fprintf fmt "Real"
30
  | Types.Tclock _
31
  | Types.Tarray _
32
  | Types.Tstatic _
33
  | Types.Tconst _
34
  | Types.Tarrow _
35
  | _                     -> Format.eprintf "internal error: pp_type %a@."
36
    Types.print_ty t; assert false
37

    
38
let pp_decl_var fmt id =
39
  Format.fprintf fmt "(declare-var %s %a)"
40
    id.var_id
41
    pp_type id.var_type
42

    
43
let pp_var fmt id = Format.pp_print_string fmt id.var_id
44

    
45

    
46
let pp_conj pp fmt l =
47
  match l with
48
    [] -> assert false
49
  | [x] -> pp fmt x
50
  | _ -> fprintf fmt "(and @[<v 0>%a@]@ )" (Utils.fprintf_list ~sep:" " pp) l
51

    
52

    
53

    
54
let concat prefix x = if prefix = "" then x else prefix ^ "." ^ x
55
let rename f = (fun v -> {v with var_id = f v.var_id } )
56
let rename_machine p = rename (fun n -> concat p n)
57
let rename_machine_list p = List.map (rename_machine p)
58

    
59
let rename_current =  rename (fun n -> n ^ "_c")
60
let rename_current_list = List.map rename_current
61
let rename_next = rename (fun n -> n ^ "_x")
62
let rename_next_list = List.map rename_next
63

    
64

    
65
let get_machine machines node_name =
66
  List.find (fun m  -> m.mname.node_id = node_name) machines
67

    
68

    
69
let full_memory_vars machines machine =
70
  let rec aux fst prefix m =
71
    (rename_machine_list (if fst then prefix else concat prefix m.mname.node_id) m.mmemory) @
72
      List.fold_left (fun accu (id, (n, _)) ->
73
	let name = node_name n in
74
	if name = "_arrow" then accu else
75
	  let machine_n = get_machine machines name in
76
	  ( aux false (concat prefix (if fst then id else concat m.mname.node_id id)) machine_n ) @ accu
77
      ) [] (m.minstances)
78
  in
79
  aux true machine.mname.node_id machine
80

    
81

    
82
let stateless_vars machines m =
83
  (rename_machine_list m.mname.node_id m.mstep.step_inputs)@
84
    (rename_machine_list m.mname.node_id m.mstep.step_outputs)
85

    
86
let step_vars machines m =
87
  (stateless_vars machines m)@
88
    (rename_current_list (full_memory_vars machines m)) @
89
    (rename_next_list (full_memory_vars machines m))
90

    
91
let init_vars machines m =
92
  (stateless_vars machines m) @ (rename_next_list (full_memory_vars machines m))
93

    
94
(********************************************************************************************)
95
(*                    Instruction Printing functions                                        *)
96
(********************************************************************************************)
97

    
98
let pp_horn_var m fmt id =
99
  if Types.is_array_type id.var_type
100
  then
101
    assert false (* no arrays in Horn output *)
102
  else
103
    Format.fprintf fmt "%s" id.var_id
104

    
105

    
106
(* Used to print boolean constants *)
107
let pp_horn_tag fmt t =
108
  pp_print_string fmt (if t = tag_true then "true" else if t = tag_false then "false" else t)
109

    
110
(* Prints a constant value *)
111
let rec pp_horn_const fmt c =
112
  match c with
113
    | Const_int i    -> pp_print_int fmt i
114
    | Const_real (c,e,s)   -> assert false (* TODO rational pp_print_string fmt r *)
115
    (* | Const_float r  -> pp_print_float fmt r *)
116
    | Const_tag t    -> pp_horn_tag fmt t
117
    | _              -> assert false
118

    
119
(* Prints a value expression [v], with internal function calls only.
120
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
121
   but an offset suffix may be added for array variables
122
*)
123
let rec pp_horn_val ?(is_lhs=false) self pp_var fmt v =
124
  match v.value_desc with
125
    | Cst c         -> pp_horn_const fmt c
126
    | Array _
127
    | Access _ -> assert false (* no arrays *)
128
    | Power (v, n)  -> assert false
129
    | LocalVar v    -> pp_var fmt (rename_machine self v)
130
    | StateVar v    ->
131
      if Types.is_array_type v.var_type
132
      then assert false
133
      else pp_var fmt (rename_machine self ((if is_lhs then rename_next else rename_current) (* self *) v))
134
    | Fun (n, vl)   -> Format.fprintf fmt "%a" (Basic_library.pp_horn n (pp_horn_val self pp_var)) vl
135

    
136
(* Prints a [value] indexed by the suffix list [loop_vars] *)
137
let rec pp_value_suffix self pp_value fmt value =
138
 match value.value_desc with
139
 | Fun (n, vl)  ->
140
   Basic_library.pp_horn n (pp_value_suffix self pp_value) fmt vl
141
 |  _            ->
142
   pp_horn_val self pp_value fmt value
143

    
144
(* type_directed assignment: array vs. statically sized type
145
   - [var_type]: type of variable to be assigned
146
   - [var_name]: name of variable to be assigned
147
   - [value]: assigned value
148
   - [pp_var]: printer for variables
149
*)
150
let pp_assign m self pp_var fmt var_type var_name value =
151
  fprintf fmt "(= %a %a)" (pp_horn_val ~is_lhs:true self pp_var) var_name (pp_value_suffix self pp_var) value
152

    
153
let pp_instance_call
154
    machines ?(init=false) m self fmt i (inputs: value_t list) (outputs: var_decl list) =
155
  try (* stateful node instance *)
156
    begin
157
      let (n,_) = List.assoc i m.minstances in
158
      match node_name n, inputs, outputs with
159
      | "_arrow", [i1; i2], [o] -> begin
160
        if init then
161
          pp_assign
162
   	    m
163
   	    self
164
   	    (pp_horn_var m)
165
	    fmt
166
   	    o.var_type (mk_val (LocalVar o) o.var_type) i1
167
        else
168
          pp_assign
169
   	    m self (pp_horn_var m) fmt
170
   	    o.var_type (mk_val (LocalVar o) o.var_type) i2
171
	    
172
      end
173
      | name, _, _ ->
174
	begin
175
	  let target_machine = List.find (fun m  -> m.mname.node_id = name) machines in
176
	  if init then
177
	    Format.fprintf fmt "(%a %a%t%a%t%a)"
178
	      pp_machine_init_name (node_name n)
179
	      (* inputs *)
180
	      (Utils.fprintf_list ~sep:" " (pp_horn_val self (pp_horn_var m)))
181
	      inputs
182
	      (Utils.pp_final_char_if_non_empty " " inputs)
183
	      (* outputs *)
184
	      (Utils.fprintf_list ~sep:" " (pp_horn_val self (pp_horn_var m))) 
185
	      (List.map (fun v -> mk_val (LocalVar v) v.var_type) outputs)
186
	      (Utils.pp_final_char_if_non_empty " " outputs)
187
	      (* memories (next) *)
188
	      (Utils.fprintf_list ~sep:" " pp_var) (
189
  		rename_machine_list
190
		  (concat m.mname.node_id i)
191
		  (rename_next_list (full_memory_vars machines target_machine)
192
		  )
193
	       )
194
	  else
195
	    Format.fprintf fmt "(%a %a%t%a%t%a)"
196
	      pp_machine_step_name (node_name n)
197
	      (Utils.fprintf_list ~sep:" " (pp_horn_val self (pp_horn_var m))) inputs
198
	      (Utils.pp_final_char_if_non_empty " " inputs) 
199
	      (Utils.fprintf_list ~sep:" " (pp_horn_val self (pp_horn_var m))) 
200
	      (List.map (fun v -> mk_val (LocalVar v) v.var_type) outputs)
201
	      (Utils.pp_final_char_if_non_empty " " outputs)
202
	      (Utils.fprintf_list ~sep:" " pp_var) (
203
		(rename_machine_list
204
		   (concat m.mname.node_id i)
205
		   (rename_current_list (full_memory_vars machines target_machine))
206
		) @
207
		  (rename_machine_list
208
		     (concat m.mname.node_id i)
209
		     (rename_next_list (full_memory_vars machines target_machine))
210
		  )
211
	       )
212

    
213
	end
214
    end
215
    with Not_found -> ( (* stateless node instance *)
216
      let (n,_) = List.assoc i m.mcalls in
217
      Format.fprintf fmt "(%s %a%t%a)"
218
	(node_name n)
219
	(Utils.fprintf_list ~sep:" " (pp_horn_val self (pp_horn_var m)))
220
	inputs
221
	(Utils.pp_final_char_if_non_empty " " inputs) 
222
	(Utils.fprintf_list ~sep:" " (pp_horn_val self (pp_horn_var m))) 
223
	(List.map (fun v -> mk_val (LocalVar v) v.var_type) outputs)
224
    )
225

    
226
let pp_machine_init (m: machine_t) self fmt inst =
227
  let (node, static) = List.assoc inst m.minstances in
228
  fprintf fmt "(%a %a%t%s->%s)"
229
    pp_machine_init_name (node_name node)
230
    (Utils.fprintf_list ~sep:" " Dimension.pp_dimension) static
231
    (Utils.pp_final_char_if_non_empty " " static)
232
    self inst
233

    
234
(* TODO *)
235
let rec pp_conditional machines ?(init=false)  (m: machine_t) self fmt c tl el =
236
  fprintf fmt "@[<v 2>if (%a) {%t%a@]@,@[<v 2>} else {%t%a@]@,}"
237
    (pp_horn_val self (pp_horn_var m)) c
238
    (Utils.pp_newline_if_non_empty tl)
239
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr machines ~init:init  m self)) tl
240
    (Utils.pp_newline_if_non_empty el)
241
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr machines ~init:init  m self)) el
242

    
243
and pp_machine_instr machines ?(init=false) (m: machine_t) self fmt instr =
244
  match instr with
245
  | MReset i ->
246
    pp_machine_init m self fmt i
247
  | MLocalAssign (i,v) ->
248
    pp_assign
249
      m self (pp_horn_var m) fmt
250
      i.var_type (mk_val (LocalVar i) i.var_type) v
251
  | MStateAssign (i,v) ->
252
    pp_assign
253
      m self (pp_horn_var m) fmt
254
      i.var_type (mk_val (StateVar i) i.var_type) v
255
  | MStep ([i0], i, vl) when Basic_library.is_value_internal_fun (mk_val (Fun (i, vl)) i0.var_type)  -> 
256
    assert false (* This should not happen anymore *)
257
  | MStep (il, i, vl) ->
258
    pp_instance_call machines ~init:init m self fmt i vl il
259
  | MBranch (g,hl) ->
260
    if hl <> [] && let t = fst (List.hd hl) in t = tag_true || t = tag_false
261
    then (* boolean case, needs special treatment in C because truth value is not unique *)
262
      (* may disappear if we optimize code by replacing last branch test with default *)
263
      let tl = try List.assoc tag_true  hl with Not_found -> [] in
264
      let el = try List.assoc tag_false hl with Not_found -> [] in
265
      pp_conditional machines ~init:init m self fmt g tl el
266
    else assert false (* enum type case *)
267
  | MComment _ -> ()
268

    
269

    
270
(**************************************************************)
271

    
272
let is_stateless m = m.minstances = [] && m.mmemory = []
273

    
274
(* Print the machine m:
275
   two functions: m_init and m_step
276
   - m_init is a predicate over m memories
277
   - m_step is a predicate over old_memories, inputs, new_memories, outputs
278
   We first declare all variables then the two /rules/.
279
*)
280
let print_machine machines fmt m =
281
  let pp_instr init = pp_machine_instr machines ~init:init m in
282
  if m.mname.node_id = arrow_id then
283
    (* We don't print arrow function *)
284
    ()
285
  else
286
    begin
287
      Format.fprintf fmt "; %s@." m.mname.node_id;
288

    
289
   (* Printing variables *)
290
   Utils.fprintf_list ~sep:"@." pp_decl_var fmt
291
     ((step_vars machines m)@
292
	 (rename_machine_list m.mname.node_id m.mstep.step_locals));
293
   Format.pp_print_newline fmt ();
294

    
295

    
296

    
297
   if is_stateless m then
298
     begin
299
       (* Declaring single predicate *)
300
       Format.fprintf fmt "(declare-rel %a (%a))@."
301
	 pp_machine_stateless_name m.mname.node_id
302
	 (Utils.fprintf_list ~sep:" " pp_type)
303
	 (List.map (fun v -> v.var_type) (stateless_vars machines m));
304

    
305
       (* Rule for single predicate *)
306
       Format.fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a %a)@]@.))@.@."
307
	 (pp_conj (pp_instr
308
		     true (* In this case, the boolean init can be set to true or false.
309
			     The node is stateless. *)
310
		     m.mname.node_id)
311
	 )
312
	 m.mstep.step_instrs
313
	 pp_machine_stateless_name m.mname.node_id
314
	 (Utils.fprintf_list ~sep:" " pp_var) (stateless_vars machines m);
315
     end
316
   else
317
     begin
318
       (* Declaring predicate *)
319
       Format.fprintf fmt "(declare-rel %a (%a))@."
320
	 pp_machine_init_name m.mname.node_id
321
	 (Utils.fprintf_list ~sep:" " pp_type)
322
	 (List.map (fun v -> v.var_type) (init_vars machines m));
323

    
324
       Format.fprintf fmt "(declare-rel %a (%a))@."
325
	 pp_machine_step_name m.mname.node_id
326
	 (Utils.fprintf_list ~sep:" " pp_type)
327
	 (List.map (fun v -> v.var_type) (step_vars machines m));
328

    
329
       Format.pp_print_newline fmt ();
330

    
331
      (* Adding assertions *)
332
       (match m.mstep.step_asserts with
333
       | [] ->
334
          begin
335
            (* Rule for init *)
336
            Format.fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a %a)@]@.))@.@."
337
	                   (pp_conj (pp_instr true m.mname.node_id)) m.mstep.step_instrs
338
	                   pp_machine_init_name m.mname.node_id
339
	                   (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines m);
340
            (* Rule for step*)
341
            Format.fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a %a)@]@.))@.@."
342
                           (pp_conj (pp_instr false m.mname.node_id)) m.mstep.step_instrs
343
                           pp_machine_step_name m.mname.node_id
344
                           (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines m);
345
          end
346
       | assertsl ->
347
          begin
348
	    let pp_val = pp_horn_val ~is_lhs:true m.mname.node_id pp_var in
349
            (* print_string pp_val; *)
350
            let instrs_concat = m.mstep.step_instrs in
351
            Format.fprintf fmt "; with Assertions @.";
352
            (*Rule for init*)
353
            Format.fprintf fmt "@[<v 2>(rule (=> @ (and @ %a@. %a)(%a %a)@]@.))@.@."
354
                           (pp_conj (pp_instr true m.mname.node_id)) instrs_concat
355
                           (pp_conj pp_val) assertsl
356
                           pp_machine_init_name m.mname.node_id
357
                           (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines m);
358
            (*Rule for step*)
359
            Format.fprintf fmt "@[<v 2>(rule (=> @ (and @ %a@. %a)(%a %a)@]@.))@.@."
360
                           (pp_conj (pp_instr false m.mname.node_id)) instrs_concat
361
                           (pp_conj pp_val) assertsl
362
                           pp_machine_step_name m.mname.node_id
363
                           (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines m);
364
          end
365
       );
366
       
367
(*
368
       match m.mspec with
369
	 None -> () (* No node spec; we do nothing *)
370
       | Some {requires = []; ensures = [EnsuresExpr e]; behaviors = []} -> 
371
	 ( 
372
       (* For the moment, we only deal with simple case: single ensures, no other parameters *)
373
	   ()
374
	     
375
	 )
376
       | _ -> () (* Other cases give nothing *)
377
*)      
378
     end
379
    end
380

    
381

    
382

    
383
let collecting_semantics machines fmt node machine =
384
    Format.fprintf fmt "; Collecting semantics for node %s@.@." node;
385
    (* We print the types of the main node "memory tree" TODO: add the output *)
386
    let main_output =
387
     rename_machine_list machine.mname.node_id machine.mstep.step_outputs
388
    in
389
    let main_output_dummy =
390
     rename_machine_list ("dummy" ^ machine.mname.node_id) machine.mstep.step_outputs
391
    in
392
    let main_memory_next =
393
      (rename_next_list (* machine.mname.node_id *) (full_memory_vars machines machine)) @
394
      main_output
395
    in
396
    let main_memory_current =
397
      (rename_current_list (* machine.mname.node_id *) (full_memory_vars machines machine)) @
398
      main_output_dummy
399
    in
400

    
401
    (* Special case when the main node is stateless *)
402
    let init_name, step_name =
403
      if is_stateless machine then
404
	pp_machine_stateless_name, pp_machine_stateless_name
405
      else
406
	pp_machine_init_name, pp_machine_step_name
407
    in
408

    
409
    Format.fprintf fmt "(declare-rel MAIN (%a))@."
410
      (Utils.fprintf_list ~sep:" " pp_type)
411
      (List.map (fun v -> v.var_type) main_memory_next);
412

    
413
    Format.fprintf fmt "; Initial set@.";
414
    Format.fprintf fmt "(declare-rel INIT_STATE ())@.";
415
    Format.fprintf fmt "(rule INIT_STATE)@.";
416
    Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>INIT_STATE@ (@[<v 0>%a %a@])@]@ )@ (MAIN %a)@]@.))@.@."
417
      init_name node
418
      (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines machine)
419
      (Utils.fprintf_list ~sep:" " pp_var) main_memory_next ;
420

    
421
    Format.fprintf fmt "; Inductive def@.";
422
    (Utils.fprintf_list ~sep:" " (fun fmt v -> Format.fprintf fmt "%a@." pp_decl_var v)) fmt main_output_dummy;
423
    Format.fprintf fmt
424
      "@[<v 2>(rule (=> @ (and @[<v 0>(MAIN %a)@ (@[<v 0>%a %a@])@]@ )@ (MAIN %a)@]@.))@.@."
425
      (Utils.fprintf_list ~sep:" " pp_var) main_memory_current
426
      step_name node
427
      (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines machine)
428
      (Utils.fprintf_list ~sep:" " pp_var) main_memory_next
429

    
430
let check_prop machines fmt node machine =
431
  let main_output =
432
    rename_machine_list machine.mname.node_id machine.mstep.step_outputs
433
  in
434
  let main_memory_next =
435
    (rename_next_list (full_memory_vars machines machine)) @ main_output
436
  in
437
  Format.fprintf fmt "; Property def@.";
438
  Format.fprintf fmt "(declare-rel ERR ())@.";
439
  Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>(not %a)@ (MAIN %a)@])@ ERR))@."
440
    (pp_conj pp_var) main_output
441
    (Utils.fprintf_list ~sep:" " pp_var) main_memory_next
442
    ;
443
  if !Options.horn_queries then
444
    Format.fprintf fmt "(query ERR)@."
445

    
446

    
447
let cex_computation machines fmt node machine =
448
    Format.fprintf fmt "; CounterExample computation for node %s@.@." node;
449
    (* We print the types of the cex node "memory tree" TODO: add the output *)
450
    let cex_input =
451
     rename_machine_list machine.mname.node_id machine.mstep.step_inputs
452
    in
453
    let cex_input_dummy =
454
     rename_machine_list ("dummy" ^ machine.mname.node_id) machine.mstep.step_inputs
455
    in
456
    let cex_output =
457
     rename_machine_list machine.mname.node_id machine.mstep.step_outputs
458
    in
459
    let cex_output_dummy =
460
     rename_machine_list ("dummy" ^ machine.mname.node_id) machine.mstep.step_outputs
461
    in
462
    let cex_memory_next =
463
      cex_input @ (rename_next_list (full_memory_vars machines machine)) @ cex_output
464
    in
465
    let cex_memory_current =
466
      cex_input_dummy @ (rename_current_list (full_memory_vars machines machine)) @ cex_output_dummy
467
    in
468

    
469
    (* Special case when the cex node is stateless *)
470
    let init_name, step_name =
471
      if is_stateless machine then
472
	pp_machine_stateless_name, pp_machine_stateless_name
473
      else
474
	pp_machine_init_name, pp_machine_step_name
475
    in
476

    
477
    Format.fprintf fmt "(declare-rel CEX (Int %a))@.@."
478
      (Utils.fprintf_list ~sep:" " pp_type)
479
      (List.map (fun v -> v.var_type) cex_memory_next);
480

    
481
    Format.fprintf fmt "; Initial set@.";
482
    Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>INIT_STATE@ (@[<v 0>%a %a@])@]@ )@ (CEX 0 %a)@]@.))@.@."
483
      init_name node
484
      (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines machine)
485
      (Utils.fprintf_list ~sep:" " pp_var) cex_memory_next ;
486

    
487
    Format.fprintf fmt "; Inductive def@.";
488
    (* Declare dummy inputs. Outputs should have been declared previously with collecting sem *)
489
    (Utils.fprintf_list ~sep:" " (fun fmt v -> Format.fprintf fmt "%a@." pp_decl_var v)) fmt cex_input_dummy;
490
    Format.fprintf fmt "(declare-var cexcpt Int)@.";
491
    Format.fprintf fmt
492
      "@[<v 2>(rule (=> @ (and @[<v 0>(CEX cexcpt %a)@ (@[<v 0>%a %a@])@]@ )@ (CEX (+ 1 cexcpt) %a)@]@.))@.@."
493
      (Utils.fprintf_list ~sep:" " pp_var) cex_memory_current
494
      step_name node
495
      (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines machine)
496
      (Utils.fprintf_list ~sep:" " pp_var) cex_memory_next
497

    
498
let get_cex machines fmt node machine =
499
    let cex_input =
500
     rename_machine_list machine.mname.node_id machine.mstep.step_inputs
501
    in
502
    let cex_output =
503
     rename_machine_list machine.mname.node_id machine.mstep.step_outputs
504
    in
505
  let cex_memory_next =
506
    cex_input @ (rename_next_list (full_memory_vars machines machine)) @ cex_output
507
  in
508
  Format.fprintf fmt "; Property def@.";
509
  Format.fprintf fmt "(declare-rel CEXTRACE ())@.";
510
  Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>(not %a)@ (CEX cexcpt %a)@])@ CEXTRACE))@."
511
    (pp_conj pp_var) cex_output
512
    (Utils.fprintf_list ~sep:" " pp_var) cex_memory_next
513
    ;
514
  Format.fprintf fmt "(query CEXTRACE)@."
515

    
516

    
517
let main_print machines fmt =
518
if !Options.main_node <> "" then
519
  begin
520
    let node = !Options.main_node in
521
    let machine = get_machine machines node in
522

    
523

    
524
    collecting_semantics machines fmt node machine;
525
    check_prop machines fmt node machine;
526
    if !Options.horn_cex then(
527
      cex_computation machines fmt node machine;
528
      get_cex machines fmt node machine)
529
end
530

    
531

    
532
let translate fmt basename prog machines =
533
  List.iter (print_machine machines fmt) (List.rev machines);
534
  main_print machines fmt
535

    
536

    
537
let traces_file fmt basename prog machines =
538

    
539
  Format.fprintf fmt
540
  "<?xml version=\"1.0\"?>\n<Traces xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
541

    
542
  (* We extract the annotation dealing with traceability *)
543
  let machines_traces = List.map (fun m ->
544
    let traces : (ident * expr) list=
545
      let all_annots = List.flatten (List.map (fun ann -> ann.annots) m.mannot) in
546
      let filtered =
547
	List.filter (fun (kwds, _) -> kwds = ["traceability"]) all_annots
548
      in
549
      let content = List.map snd filtered in
550
      (* Elements are supposed to be a pair (tuple): variable, expression *)
551
      List.map (fun ee ->
552
	match ee.eexpr_quantifiers, ee.eexpr_qfexpr.expr_desc with
553
	| [], Expr_tuple [v;e] -> (
554
	  match v.expr_desc with
555
	  | Expr_ident vid -> vid, e
556
	  | _ -> assert false )
557
	| _ -> assert false)
558
	content
559
    in
560

    
561
    m, traces
562

    
563
  ) machines
564
  in
565

    
566
  (* Compute memories associated to each machine *)
567
  let compute_mems m =
568
    let rec aux fst prefix m =
569
      (List.map (fun mem -> (prefix, mem)) m.mmemory) @
570
	List.fold_left (fun accu (id, (n, _)) ->
571
	  let name = node_name n in
572
	  if name = "_arrow" then accu else
573
	    let machine_n = get_machine machines name in
574
	    ( aux false ((id,machine_n)::prefix) machine_n )
575
	    @ accu
576
	) [] m.minstances
577
    in
578
    aux true [] m
579
  in
580

    
581
  List.iter (fun m ->
582
    (* Format.fprintf fmt "; Node %s@." m.mname.node_id; *)
583
    Format.fprintf fmt "    <Node name=\"%s\">@." m.mname.node_id;
584

    
585
    let memories_old =
586
      List.map (fun (p, v) ->
587
	let machine = match p with | [] -> m | (_,m')::_ -> m' in
588
	let traces = List.assoc machine machines_traces in
589
	if List.mem_assoc v.var_id traces then (
590
	  (* We take the expression associated to variable v in the trace info *)
591
	  (* Format.eprintf "Found variable %a in traces: %a@."  pp_var v Printers.pp_expr (List.assoc v.var_id traces); *)
592
	  p, List.assoc v.var_id traces
593
      )
594
	else (
595
	  (* We keep the variable as is: we create an expression v *)
596
	  (* Format.eprintf "Unable to found variable %a in traces (%a)@."  pp_var v (Utils.fprintf_list ~sep:", " Format.pp_print_string) (List.map fst traces); *)
597
	  p, mkexpr Location.dummy_loc (Expr_ident v.var_id)
598
	)
599

    
600
      ) (compute_mems m)
601
    in
602
    let memories_next = (* We remove the topest pre in each expression *)
603
      List.map
604
      	(fun (prefix, ee) ->
605
      	  match ee.expr_desc with
606
      	  | Expr_pre e -> prefix, e
607
      	  | _ -> Format.eprintf
608
      	    "Mem Failure: (prefix: %a, eexpr: %a)@.@?"
609
      	    (Utils.fprintf_list ~sep:","
610
      	       (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id ))
611
      	    (List.rev prefix)
612
      	    Printers.pp_expr ee;
613
      	    assert false)
614
	memories_old
615
    in
616

    
617
    (* let pp_prefix_rev fmt prefix = *)
618
    (*   Utils.fprintf_list ~sep:"." (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id) fmt (List.rev prefix) *)
619
    (* in *)
620

    
621
    let pp_prefix_rev fmt prefix =
622
      Utils.fprintf_list ~sep:"." (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id) fmt (List.rev prefix)
623
    in
624

    
625
    let input_vars = (rename_machine_list m.mname.node_id m.mstep.step_inputs) in
626
    let output_vars = (rename_machine_list m.mname.node_id m.mstep.step_outputs) in
627
     Format.fprintf fmt "     <input name=\"%a\" type=\"%a\">%a</input> @."
628
                   (Utils.fprintf_list ~sep:" | " pp_var) input_vars
629
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) input_vars
630
                   (Utils.fprintf_list ~sep:" | " pp_var) (m.mstep.step_inputs);
631

    
632
    Format.fprintf fmt "      <output name=\"%a\" type=\"%a\">%a</output> @."
633
                   (Utils.fprintf_list ~sep:" | " pp_var)  output_vars
634
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) output_vars
635
                   (Utils.fprintf_list ~sep:" | " pp_var) (m.mstep.step_outputs);
636

    
637
    let init_local_vars = (rename_next_list (full_memory_vars machines m)) in
638
    let step_local_vars = (rename_current_list (full_memory_vars machines m)) in
639

    
640
    Format.fprintf fmt "      <localInit name=\"%a\" type=\"%a\">%t%a</localInit> @."
641
                   (Utils.fprintf_list ~sep:" | " pp_var) init_local_vars
642
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) init_local_vars
643
                   (fun fmt -> match memories_next with [] -> () | _ -> fprintf fmt "")
644
                   (Utils.fprintf_list ~sep:" | " (fun fmt (prefix, ee) -> fprintf fmt "%a" Printers.pp_expr ee)) memories_next;
645

    
646
    Format.fprintf fmt "      <localStep name=\"%a\" type=\"%a\">%t%a</localStep> @."
647
                   (Utils.fprintf_list ~sep:" | " pp_var) step_local_vars
648
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) step_local_vars
649
                   (fun fmt -> match memories_old with [] -> () | _ -> fprintf fmt "")
650
                     (Utils.fprintf_list ~sep:" | " (fun fmt (prefix,ee) -> fprintf fmt "(%a)"
651
                                    Printers.pp_expr ee)) (memories_old);
652

    
653
     Format.fprintf fmt "    </Node>@.";
654

    
655
  ) (List.rev machines);
656
  Format.fprintf fmt "</Traces>@.";
657

    
658
          (* (Utils.fprintf_list ~sep:" | " (fun fmt (prefix, ee) -> fprintf fmt "%a%a" pp_prefix_rev prefix Printers.pp_expr ee)) memories_next; *)
659
   (* (Utils.fprintf_list ~sep:" | " (fun fmt (prefix,ee) -> fprintf fmt "%a(%a)" *)
660
   (*                                  pp_prefix_rev prefix Printers.pp_expr ee)) (memories_old); *)
661

    
662
(* Local Variables: *)
663
(* compile-command:"make -C ../.." *)
664
(* End: *)