Project

General

Profile

Revision fea041c5 src/backends/Horn/horn_backend.ml

View differences:

src/backends/Horn/horn_backend.ml
9 9
(*                                                                  *)
10 10
(********************************************************************)
11 11

  
12
(* The compilation presented here is defined in Garoche, Gurfinkel, Kahsai,
13
   HCSV'14 *)
12
(* The compilation presented here was first defined in Garoche, Gurfinkel,
13
   Kahsai, HCSV'14.
14

  
15
   This is a modified version that handles reset and automaton
16
*)
14 17

  
15 18
open Format
16 19
open LustreSpec
17 20
open Corelang
18 21
open Machine_code
19 22

  
23
open Horn_backend_common
24
open Horn_backend_printers
25
open Horn_backend_collecting_sem
20 26

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

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

  
27
(*
28
TODO:
29
- gerer les traces. Ca merde pour l'instant dans le calcul des memoires sur les arrows
63 30

  
64
let get_machine machines node_name =
65
  try
66
    List.find (fun m  -> m.mname.node_id = node_name) machines
67
  with Not_found ->
68
    begin
69
      Format.eprintf "internal error: get_machine %s@." node_name;
70
      assert false
71
    end
72

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

  
85
let stateless_vars machines m =
86
  (rename_machine_list m.mname.node_id m.mstep.step_inputs)@
87
    (rename_machine_list m.mname.node_id m.mstep.step_outputs)
88

  
89
let step_vars machines m =
90
  (stateless_vars machines m)@
91
    (rename_current_list (full_memory_vars machines m)) @
92
    (rename_next_list (full_memory_vars machines m))
93

  
94
let init_vars machines m =
95
  (stateless_vars machines m) @ (rename_next_list (full_memory_vars machines m))
96

  
97
(********************************************************************************************)
98
(*                    Instruction Printing functions                                        *)
99
(********************************************************************************************)
100

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

  
108

  
109
(* Used to print boolean constants *)
110
let pp_horn_tag fmt t =
111
  pp_print_string fmt (if t = tag_true then "true" else if t = tag_false then "false" else t)
112

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

  
122
(* Prints a value expression [v], with internal function calls only.
123
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
124
   but an offset suffix may be added for array variables
31
- gerer le reset --- DONE
32
- reconstruire les rechable states DONE
33
- reintroduire le cex/traces ... DONE
34
- traiter les types enum et les branchements sur ces types enum (en particulier les traitements des resets qui ont lieu dans certaines branches et pas dans d'autres )
125 35
*)
126
let rec pp_horn_val ?(is_lhs=false) self pp_var fmt v =
127
  match v.value_desc with
128
    | Cst c         -> pp_horn_const fmt c
129
    | Array _
130
    | Access _ -> assert false (* no arrays *)
131
    | Power (v, n)  -> assert false
132
    | LocalVar v    -> pp_var fmt (rename_machine self v)
133
    | StateVar v    ->
134
      if Types.is_array_type v.var_type
135
      then assert false
136
      else pp_var fmt (rename_machine self ((if is_lhs then rename_next else rename_current) (* self *) v))
137
    | Fun (n, vl)   -> Format.fprintf fmt "%a" (Basic_library.pp_horn n (pp_horn_val self pp_var)) vl
138

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

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

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

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

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

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

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

  
272

  
273
(**************************************************************)
274

  
275
let is_stateless m = m.minstances = [] && m.mmemory = []
276

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

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

  
298

  
299

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

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

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

  
332
       Format.pp_print_newline fmt ();
333

  
334
      (* Adding assertions *)
335
       (match m.mstep.step_asserts with
336
       | [] ->
337
          begin
338
            (* Rule for init *)
339
            Format.fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a %a)@]@.))@.@."
340
	                   (pp_conj (pp_instr true m.mname.node_id)) m.mstep.step_instrs
341
	                   pp_machine_init_name m.mname.node_id
342
	                   (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines m);
343
            (* Rule for step*)
344
            Format.fprintf fmt "@[<v 2>(rule (=> @ %a@ (%a %a)@]@.))@.@."
345
                           (pp_conj (pp_instr false m.mname.node_id)) m.mstep.step_instrs
346
                           pp_machine_step_name m.mname.node_id
347
                           (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines m);
348
          end
349
       | assertsl ->
350
          begin
351
	    let pp_val = pp_horn_val ~is_lhs:true m.mname.node_id pp_var in
352
            (* print_string pp_val; *)
353
            let instrs_concat = m.mstep.step_instrs in
354
            Format.fprintf fmt "; with Assertions @.";
355
            (*Rule for init*)
356
            Format.fprintf fmt "@[<v 2>(rule (=> @ (and @ %a@. %a)(%a %a)@]@.))@.@."
357
                           (pp_conj (pp_instr true m.mname.node_id)) instrs_concat
358
                           (pp_conj pp_val) assertsl
359
                           pp_machine_init_name m.mname.node_id
360
                           (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines m);
361
            (*Rule for step*)
362
            Format.fprintf fmt "@[<v 2>(rule (=> @ (and @ %a@. %a)(%a %a)@]@.))@.@."
363
                           (pp_conj (pp_instr false m.mname.node_id)) instrs_concat
364
                           (pp_conj pp_val) assertsl
365
                           pp_machine_step_name m.mname.node_id
366
                           (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines m);
367
          end
368
       );
369
     end
370
    end
371

  
372

  
373

  
374
let collecting_semantics machines fmt node machine =
375
    Format.fprintf fmt "; Collecting semantics for node %s@.@." node;
376
    (* We print the types of the main node "memory tree" TODO: add the output *)
377
    let main_output =
378
     rename_machine_list machine.mname.node_id machine.mstep.step_outputs
379
    in
380
    let main_output_dummy =
381
     rename_machine_list ("dummy" ^ machine.mname.node_id) machine.mstep.step_outputs
382
    in
383
    let main_memory_next =
384
      (rename_next_list (* machine.mname.node_id *) (full_memory_vars machines machine)) @
385
      main_output
386
    in
387
    let main_memory_current =
388
      (rename_current_list (* machine.mname.node_id *) (full_memory_vars machines machine)) @
389
      main_output_dummy
390
    in
391

  
392
    (* Special case when the main node is stateless *)
393
    let init_name, step_name =
394
      if is_stateless machine then
395
	pp_machine_stateless_name, pp_machine_stateless_name
396
      else
397
	pp_machine_init_name, pp_machine_step_name
398
    in
399

  
400
    Format.fprintf fmt "(declare-rel MAIN (%a))@."
401
      (Utils.fprintf_list ~sep:" " pp_type)
402
      (List.map (fun v -> v.var_type) main_memory_next);
403

  
404
    Format.fprintf fmt "; Initial set@.";
405
    Format.fprintf fmt "(declare-rel INIT_STATE ())@.";
406
    Format.fprintf fmt "(rule INIT_STATE)@.";
407
    Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>INIT_STATE@ (@[<v 0>%a %a@])@]@ )@ (MAIN %a)@]@.))@.@."
408
      init_name node
409
      (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines machine)
410
      (Utils.fprintf_list ~sep:" " pp_var) main_memory_next ;
411

  
412
    Format.fprintf fmt "; Inductive def@.";
413
    (Utils.fprintf_list ~sep:" " (fun fmt v -> Format.fprintf fmt "%a@." pp_decl_var v)) fmt main_output_dummy;
414
    Format.fprintf fmt
415
      "@[<v 2>(rule (=> @ (and @[<v 0>(MAIN %a)@ (@[<v 0>%a %a@])@]@ )@ (MAIN %a)@]@.))@.@."
416
      (Utils.fprintf_list ~sep:" " pp_var) main_memory_current
417
      step_name node
418
      (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines machine)
419
      (Utils.fprintf_list ~sep:" " pp_var) main_memory_next
420

  
421
let check_prop machines fmt node machine =
422
  let main_output =
423
    rename_machine_list machine.mname.node_id machine.mstep.step_outputs
424
  in
425
  let main_memory_next =
426
    (rename_next_list (full_memory_vars machines machine)) @ main_output
427
  in
428
  Format.fprintf fmt "; Property def@.";
429
  Format.fprintf fmt "(declare-rel ERR ())@.";
430
  Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>(not %a)@ (MAIN %a)@])@ ERR))@."
431
    (pp_conj pp_var) main_output
432
    (Utils.fprintf_list ~sep:" " pp_var) main_memory_next
433
    ;
434
   if !Options.horn_query then Format.fprintf fmt "(query ERR)@."
435

  
436

  
437
let cex_computation machines fmt node machine =
438
    Format.fprintf fmt "; CounterExample computation for node %s@.@." node;
439
    (* We print the types of the cex node "memory tree" TODO: add the output *)
440
    let cex_input =
441
     rename_machine_list machine.mname.node_id machine.mstep.step_inputs
442
    in
443
    let cex_input_dummy =
444
     rename_machine_list ("dummy" ^ machine.mname.node_id) machine.mstep.step_inputs
445
    in
446
    let cex_output =
447
     rename_machine_list machine.mname.node_id machine.mstep.step_outputs
448
    in
449
    let cex_output_dummy =
450
     rename_machine_list ("dummy" ^ machine.mname.node_id) machine.mstep.step_outputs
451
    in
452
    let cex_memory_next =
453
      cex_input @ (rename_next_list (full_memory_vars machines machine)) @ cex_output
454
    in
455
    let cex_memory_current =
456
      cex_input_dummy @ (rename_current_list (full_memory_vars machines machine)) @ cex_output_dummy
457
    in
458

  
459
    (* Special case when the cex node is stateless *)
460
    let init_name, step_name =
461
      if is_stateless machine then
462
	pp_machine_stateless_name, pp_machine_stateless_name
463
      else
464
	pp_machine_init_name, pp_machine_step_name
465
    in
466

  
467
    Format.fprintf fmt "(declare-rel CEX (Int %a))@.@."
468
      (Utils.fprintf_list ~sep:" " pp_type)
469
      (List.map (fun v -> v.var_type) cex_memory_next);
470

  
471
    Format.fprintf fmt "; Initial set@.";
472
    Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>INIT_STATE@ (@[<v 0>%a %a@])@]@ )@ (CEX 0 %a)@]@.))@.@."
473
      init_name node
474
      (Utils.fprintf_list ~sep:" " pp_var) (init_vars machines machine)
475
      (Utils.fprintf_list ~sep:" " pp_var) cex_memory_next ;
476

  
477
    Format.fprintf fmt "; Inductive def@.";
478
    (* Declare dummy inputs. Outputs should have been declared previously with collecting sem *)
479
    (Utils.fprintf_list ~sep:" " (fun fmt v -> Format.fprintf fmt "%a@." pp_decl_var v)) fmt cex_input_dummy;
480
    Format.fprintf fmt "(declare-var cexcpt Int)@.";
481
    Format.fprintf fmt
482
      "@[<v 2>(rule (=> @ (and @[<v 0>(CEX cexcpt %a)@ (@[<v 0>%a %a@])@]@ )@ (CEX (+ 1 cexcpt) %a)@]@.))@.@."
483
      (Utils.fprintf_list ~sep:" " pp_var) cex_memory_current
484
      step_name node
485
      (Utils.fprintf_list ~sep:" " pp_var) (step_vars machines machine)
486
      (Utils.fprintf_list ~sep:" " pp_var) cex_memory_next
487

  
488
let get_cex machines fmt node machine =
489
    let cex_input =
490
     rename_machine_list machine.mname.node_id machine.mstep.step_inputs
491
    in
492
    let cex_output =
493
     rename_machine_list machine.mname.node_id machine.mstep.step_outputs
494
    in
495
  let cex_memory_next =
496
    cex_input @ (rename_next_list (full_memory_vars machines machine)) @ cex_output
497
  in
498
  Format.fprintf fmt "; Property def@.";
499
  Format.fprintf fmt "(declare-rel CEXTRACE ())@.";
500
  Format.fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>(not %a)@ (CEX cexcpt %a)@])@ CEXTRACE))@."
501
    (pp_conj pp_var) cex_output
502
    (Utils.fprintf_list ~sep:" " pp_var) cex_memory_next
503
    ;
504
  Format.fprintf fmt "(query CEXTRACE)@."
505

  
506 36

  
507 37
let main_print machines fmt =
508 38
if !Options.main_node <> "" then
509 39
  begin
510 40
    let node = !Options.main_node in
511 41
    let machine = get_machine machines node in
512

  
513

  
514
    collecting_semantics machines fmt node machine;
515
    check_prop machines fmt node machine;
516 42
    if !Options.horn_cex then(
517 43
      cex_computation machines fmt node machine;
518 44
      get_cex machines fmt node machine)
45
    else (
46
      collecting_semantics machines fmt node machine;
47
      check_prop machines fmt node machine;
48
    )
519 49
end
520 50

  
51
let print_type_definitions fmt =
52
  let cpt_type = ref 0 in
53
  Hashtbl.iter (fun typ decl ->
54
    match typ with
55
    | Tydec_const var ->
56
      (match decl.top_decl_desc with
57
      | TypeDef tdef -> (
58
	match tdef.tydef_desc with
59
	| Tydec_enum tl ->
60
	  incr cpt_type;
61
	  fprintf fmt "(declare-datatypes () ((%s %a)));@.@."
62
	    var
63
	    (Utils.fprintf_list ~sep:" " pp_print_string) tl
64
	| _ -> assert false
65
      )
66
      | _ -> assert false
67
      )
68
    | _        -> ()) type_table
69

  
521 70

  
522 71
let translate fmt basename prog machines =
72
  (* We print typedef *)
73
  print_type_definitions fmt;
523 74
  List.iter (print_machine machines fmt) (List.rev machines);
524 75
  main_print machines fmt
525 76

  
526

  
527
let traces_file fmt basename prog machines =
528

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

  
532
  (* We extract the annotation dealing with traceability *)
533
  let machines_traces = List.map (fun m ->
534
    let traces : (ident * expr) list=
535
      let all_annots = List.flatten (List.map (fun ann -> ann.annots) m.mannot) in
536
      let filtered =
537
	List.filter (fun (kwds, _) -> kwds = ["traceability"]) all_annots
538
      in
539
      let content = List.map snd filtered in
540
      (* Elements are supposed to be a pair (tuple): variable, expression *)
541
      List.map (fun ee ->
542
	match ee.eexpr_quantifiers, ee.eexpr_qfexpr.expr_desc with
543
	| [], Expr_tuple [v;e] -> (
544
	  match v.expr_desc with
545
	  | Expr_ident vid -> vid, e
546
	  | _ -> assert false )
547
	| _ -> assert false)
548
	content
549
    in
550

  
551
    m, traces
552

  
553
  ) machines
554
  in
555

  
556
  (* Compute memories associated to each machine *)
557
  let compute_mems m =
558
    let rec aux fst prefix m =
559
      (List.map (fun mem -> (prefix, mem)) m.mmemory) @
560
	List.fold_left (fun accu (id, (n, _)) ->
561
	  let name = node_name n in
562
	  if name = "_arrow" then accu else
563
	    let machine_n = get_machine machines name in
564
	    ( aux false ((id,machine_n)::prefix) machine_n )
565
	    @ accu
566
	) [] m.minstances
567
    in
568
    aux true [] m
569
  in
570

  
571
  List.iter (fun m ->
572
    (* Format.fprintf fmt "; Node %s@." m.mname.node_id; *)
573
    Format.fprintf fmt "    <Node name=\"%s\">@." m.mname.node_id;
574

  
575
    let memories_old =
576
      List.map (fun (p, v) ->
577
	let machine = match p with | [] -> m | (_,m')::_ -> m' in
578
	let traces = List.assoc machine machines_traces in
579
	if List.mem_assoc v.var_id traces then (
580
	  (* We take the expression associated to variable v in the trace info *)
581
	  (* Format.eprintf "Found variable %a in traces: %a@."  pp_var v Printers.pp_expr (List.assoc v.var_id traces); *)
582
	  p, List.assoc v.var_id traces
583
      )
584
	else (
585
	  (* We keep the variable as is: we create an expression v *)
586
	  (* 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); *)
587
	  p, mkexpr Location.dummy_loc (Expr_ident v.var_id)
588
	)
589

  
590
      ) (compute_mems m)
591
    in
592
    let memories_next = (* We remove the topest pre in each expression *)
593
      List.map
594
      	(fun (prefix, ee) ->
595
      	  match ee.expr_desc with
596
      	  | Expr_pre e -> prefix, e
597
      	  | _ -> Format.eprintf
598
      	    "Mem Failure: (prefix: %a, eexpr: %a)@.@?"
599
      	    (Utils.fprintf_list ~sep:","
600
      	       (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id ))
601
      	    (List.rev prefix)
602
      	    Printers.pp_expr ee;
603
      	    assert false)
604
	memories_old
605
    in
606

  
607
    let pp_prefix_rev fmt prefix =
608
      Utils.fprintf_list ~sep:"." (fun fmt (id,n) -> fprintf fmt "(%s,%s)" id n.mname.node_id) fmt (List.rev prefix)
609
    in
610

  
611
    let input_vars = (rename_machine_list m.mname.node_id m.mstep.step_inputs) in
612
    let output_vars = (rename_machine_list m.mname.node_id m.mstep.step_outputs) in
613
     Format.fprintf fmt "     <input name=\"%a\" type=\"%a\">%a</input> @."
614
                   (Utils.fprintf_list ~sep:" | " pp_var) input_vars
615
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) input_vars
616
                   (Utils.fprintf_list ~sep:" | " pp_var) (m.mstep.step_inputs);
617

  
618
    Format.fprintf fmt "      <output name=\"%a\" type=\"%a\">%a</output> @."
619
                   (Utils.fprintf_list ~sep:" | " pp_var)  output_vars
620
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) output_vars
621
                   (Utils.fprintf_list ~sep:" | " pp_var) (m.mstep.step_outputs);
622

  
623
    let init_local_vars = (rename_next_list (full_memory_vars machines m)) in
624
    let step_local_vars = (rename_current_list (full_memory_vars machines m)) in
625

  
626
    Format.fprintf fmt "      <localInit name=\"%a\" type=\"%a\">%t%a</localInit> @."
627
                   (Utils.fprintf_list ~sep:" | " pp_var) init_local_vars
628
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) init_local_vars
629
                   (fun fmt -> match memories_next with [] -> () | _ -> fprintf fmt "")
630
                   (Utils.fprintf_list ~sep:" | " (fun fmt (prefix, ee) -> fprintf fmt "%a" Printers.pp_expr ee)) memories_next;
631

  
632
    Format.fprintf fmt "      <localStep name=\"%a\" type=\"%a\">%t%a</localStep> @."
633
                   (Utils.fprintf_list ~sep:" | " pp_var) step_local_vars
634
                   (Utils.fprintf_list ~sep:" | "  (fun fmt id -> pp_type fmt id.var_type)) step_local_vars
635
                   (fun fmt -> match memories_old with [] -> () | _ -> fprintf fmt "")
636
                     (Utils.fprintf_list ~sep:" | " (fun fmt (prefix,ee) -> fprintf fmt "(%a)"
637
                                    Printers.pp_expr ee)) (memories_old);
638

  
639
     Format.fprintf fmt "    </Node>@.";
640

  
641
  ) (List.rev machines);
642
  Format.fprintf fmt "</Traces>@.";
643

  
644
          (* (Utils.fprintf_list ~sep:" | " (fun fmt (prefix, ee) -> fprintf fmt "%a%a" pp_prefix_rev prefix Printers.pp_expr ee)) memories_next; *)
645
   (* (Utils.fprintf_list ~sep:" | " (fun fmt (prefix,ee) -> fprintf fmt "%a(%a)" *)
646
   (*                                  pp_prefix_rev prefix Printers.pp_expr ee)) (memories_old); *)
647

  
648 77
(* Local Variables: *)
649 78
(* compile-command:"make -C ../.." *)
650 79
(* End: *)

Also available in: Unified diff