Project

General

Profile

« Previous | Next » 

Revision 42f91c0b

Added by Pierre-Loïc Garoche about 4 years ago

Better EMF output, solved some invalid JSON produced

View differences:

src/backends/EMF/EMF_backend.ml
150 150
    | Expr_appl(_,_,reset) -> (
151 151
      match reset with None -> false | Some _ -> true
152 152
    )
153
    | _ ->  assert false
153
    | Expr_arrow _ -> true
154
    | _ -> Format.eprintf "reseting expr %a@.@?" Printers.pp_expr eq.eq_rhs; assert false
154 155
  )
155 156
  | None -> assert false (* should have been assigned to an original lustre equation *)
156 157

  
......
179 180
      ISet.union accu_def common_def_vars,
180 181
      VSet.union accu_read read_vars)
181 182
    (ISet.empty, ISet.empty, VSet.empty) il
183
  
182 184
and branch_instr_vars m i =
185
  (* Returns all_outputs, outputs, inputs of the instruction. It is
186
     only called on MBranch instructions but evaluate recursively
187
     instructions appearing in branches.
188

  
189
     It is used to gather the global input/output of a switch and
190
     print it at the begin of the JSON subtree.
191

  
192
     The set "All outputs" is used to filter out input variables
193
     belong to that set. *)
194

  
183 195
  match Corelang.get_instr_desc i with
184 196
  | MLocalAssign (var,expr) 
185
  | MStateAssign (var,expr) -> ISet.singleton var.var_id, ISet.singleton var.var_id, get_expr_vars expr
197
    | MStateAssign (var,expr) -> ISet.singleton var.var_id, ISet.singleton var.var_id, get_expr_vars expr
186 198
  | MStep (vars, f, args)  ->
187 199
     let is_stateful = List.mem_assoc f m.minstances in 
188 200
     let lhs = ISet.of_list (List.map (fun v -> v.var_id) vars) in
......
207 219
     )
208 220
  | MBranch (g,(_,hd_il)::tl)     -> (* We focus on variables defined in all branches *)
209 221
     let read_guard = get_expr_vars g in
222
     (* Bootstrapping with first item *) 
210 223
     let all_def_vars_hd, def_vars_hd, read_vars_hd = branch_block_vars m hd_il in
211 224
     let all_def_vars, def_vars, read_vars =
212 225
       List.fold_left
......
220 233
	 (all_def_vars_hd, def_vars_hd, read_vars_hd)
221 234
	 tl
222 235
     in
236
     (* all_def_vars correspond to variables written or defined in one
237
        of the branch. It may happen that a variable is defined in one
238
        but not in the other, because of reset for example.  
239

  
240
        def_vars are variables defined in all branches. *)
241

  
242

  
223 243
     all_def_vars, def_vars, VSet.union read_guard read_vars
224 244
  | MBranch _ -> assert false (* branch instruction should admit at least one case *)
225 245
  | MReset ni           
226
  | MNoReset ni ->
246
    | MNoReset ni ->
227 247
     let write = ISet.singleton (reset_name ni) in
228 248
     write, write, VSet.empty
229
  | MComment _ -> assert false (* not  available for EMF output *)
249
  | MSpec _ | MComment _ -> assert false (* not  available for EMF output *)
230 250
     
231 251
(* A kind of super join_guards: all MBranch are postponed and sorted by
232 252
   guards so they can be easier merged *)
......
358 378
    | MStep(outputs, f, inputs ) -> (* This is an imported node *)
359 379
       EMF_library_calls.pp_call fmt m f outputs inputs
360 380
	 
361
    | MComment _ 
381
    | MSpec _ | MComment _ 
362 382
      -> Format.eprintf "unhandled comment in EMF@.@?"; assert false
363 383
  (* not  available for EMF output *)
364 384
  in
......
380 400
  fprintf fmt "{@[";
381 401
  fprintf fmt "\"mode_id\": \"%s\",@ "
382 402
    m.mode_id;
383
  fprintf fmt "\"require\": [%a],@ "
403
  fprintf fmt "\"ensure\": [%a],@ "
384 404
    pp_emf_eexprs m.ensure;
385
  fprintf fmt "\"require\": [%a],@ "
405
  fprintf fmt "\"require\": [%a]@ "
386 406
    pp_emf_eexprs m.require;
387 407
  fprintf fmt "@]}"
388 408
  
......
402 422

  
403 423
let pp_emf_spec fmt spec =
404 424
  fprintf fmt "{ @[<hov 0>";
405
  fprintf fmt "\"consts\": [%a],@ "
406
    pp_emf_consts spec.consts;
407
  fprintf fmt "\"locals\": [%a],@ "
408
    pp_emf_vars_decl spec.locals;
409
  fprintf fmt "\"stmts\": [%a],@ "
410
    pp_emf_stmts spec.stmts;
425
  (* fprintf fmt "\"consts\": [%a],@ "
426
   *   pp_emf_consts spec.consts;
427
   * fprintf fmt "\"locals\": [%a],@ "
428
   *   pp_emf_vars_decl spec.locals;
429
   * fprintf fmt "\"stmts\": [%a],@ "
430
   *   pp_emf_stmts spec.stmts; *)
411 431
  fprintf fmt "\"assume\": [%a],@ "
412 432
    pp_emf_eexprs spec.assume;
413 433
  fprintf fmt "\"guarantees\": [%a],@ "
414 434
    pp_emf_eexprs spec.guarantees;
415
  fprintf fmt "\"modes\": [%a],@ "
435
  fprintf fmt "\"modes\": [%a]@ "
416 436
    pp_emf_spec_modes spec.modes;
417
  fprintf fmt "\"imports\": [%a]@ "
418
    pp_emf_spec_imports spec.imports;  
437
  (* fprintf fmt "\"imports\": [%a]@ "
438
   *   pp_emf_spec_imports spec.imports;   *)
419 439
  fprintf fmt "@] }"
420 440
  
421 441
let pp_emf_annots cpt fmt annots = fprintf_list ~sep:",@ " (pp_emf_annot cpt) fmt annots.annots
422 442
let pp_emf_annots_list cpt fmt annots_list = fprintf_list ~sep:",@ " (pp_emf_annots cpt) fmt annots_list
423 443

  
424
let pp_emf_contract fmt nd =
425
  let c = Printers.node_as_contract nd in
426
  fprintf fmt "@[v 2>\"%a\": {@ "
427
    print_protect (fun fmt -> pp_print_string fmt nd.node_id);
428
  fprintf fmt "\"contract\": %a@ "
429
    pp_emf_spec c;
430
  fprintf fmt "@]@ }"
444
(* let pp_emf_contract fmt nd =
445
 *   let c = Printers.node_as_contract nd in
446
 *   fprintf fmt "@[<v 2>\"%a\": {@ "
447
 *     print_protect (fun fmt -> pp_print_string fmt nd.node_id);
448
 *   fprintf fmt "\"contract\": %a@ "
449
 *     pp_emf_spec c;
450
 *   fprintf fmt "@]@ }" *)
431 451
  
432 452
let pp_machine fmt m =
433 453
  let instrs = (*merge_branches*) m.mstep.step_instrs in
434 454
  try
435 455
    fprintf fmt "@[<v 2>\"%a\": {@ "
436 456
      print_protect (fun fmt -> pp_print_string fmt m.mname.node_id);
457
    (match m.mspec with Some (Contract _) -> fprintf fmt "\"contract\": \"true\",@ " | _ -> ());
437 458
    fprintf fmt "\"imported\": \"false\",@ ";
438 459
    fprintf fmt "\"kind\": %t,@ "
439 460
      (fun fmt -> if not ( snd (get_stateless_status m) )
......
452 473
    fprintf fmt "\"instrs\": {@[<v 0> %a@]@ },@ "
453 474
      (pp_emf_instrs m) instrs;
454 475
    (match m.mspec with | None -> () 
455
                        | Some (Contract _) -> assert false 
456
                        | Some (NodeSpec id) -> fprintf fmt "\"coco_contract\": %s" id
476
                        | Some (Contract c) -> (
477
                          assert (c.locals = [] && c.consts = [] && c.stmts = [] && c.imports = []);
478
                          fprintf fmt "\"spec\": %a,@ " pp_emf_spec c
479
                        )
480
                        | Some (NodeSpec id) -> fprintf fmt "\"contract\": \"%s\",@ " id
457 481
    );
458 482
    fprintf fmt "\"annots\": {@[<v 0> %a@]@ }" (pp_emf_annots_list (ref 0)) m.mannot;
459 483
    fprintf fmt "@]@ }"
......
464 488
    eprintf "node skipped - no output generated@ @]@."
465 489
  )
466 490

  
467
let pp_machine fmt m =                      
491
(*let pp_machine fmt m =                      
468 492
  match m.mspec with
469 493
  | None | Some (NodeSpec _) -> pp_machine fmt m
470
  | Some (Contract _) -> pp_emf_contract fmt m.mname 
471
                       
494
  | Some (Contract _) -> pp_emf_contract fmt m
495
 *)
496
                      
472 497
let pp_emf_imported_node fmt top =
473 498
  let ind = Corelang.imported_node_of_top top in
474 499
  try
src/backends/EMF/EMF_common.ml
102 102
  match dec_t with
103 103
  | Tydec_any -> (* Dynamical built variable. No declared type. Shall
104 104
                    use the infered one. *)
105
     pp_infered_type fmt infered_t
105
     fprintf fmt "{ \"kind\": \"%a\" }" pp_infered_type infered_t
106 106
  | Tydec_int -> fprintf fmt "{ \"kind\": \"int\" }" (* !Options.int_type *)
107 107
  | Tydec_real -> fprintf fmt "{ \"kind\": \"real\" }" (* !Options.real_type *)
108 108
  (* TODO we could add more concrete types here if they were available in
......
215 215
  
216 216
(* Print the variable declaration *)
217 217
let pp_emf_var_decl fmt v =
218
  fprintf fmt "@[{\"name\": \"%a\", \"datatype\":\"%a\", \"original_name\": \"%a\"}@]"
218
  fprintf fmt "@[{\"name\": \"%a\", \"datatype\": %a, \"original_name\": \"%a\"}@]"
219 219
    pp_var_name v
220 220
    pp_var_type v
221 221
    Printers.pp_var_name v
......
251 251
    
252 252
let pp_emf_cst c inf fmt =
253 253
  let pp_typ fmt =
254
    fprintf fmt "\"datatype\": \"";
255
    pp_cst_type c inf fmt;
256
    fprintf fmt "\"@ "
254
    fprintf fmt "\"datatype\": { \"kind\": \"%t\" } @ "
255
      (pp_cst_type c inf)   
257 256
  in
258 257
  match c with
259 258
  | Const_tag t->
......
293 292
    fprintf fmt "{@[\"type\": \"variable\",@ \"value\": \"%a\",@ "
294 293
      pp_var_name v;
295 294
    (*    fprintf fmt "\"original_name\": \"%a\",@ " Printers.pp_var_name v; *)
296
    fprintf fmt "\"datatype\": \"%a\"@ " pp_var_type v;
295
    fprintf fmt "\"datatype\": %a@ " pp_var_type v;
297 296
    fprintf fmt "@]}"
298 297
  )
299 298
  | _ -> eprintf "Not of cst or var: %a@." (pp_val m) v ; assert false (* Invalid argument *)
......
310 309
  | Expr_ident id ->
311 310
     fprintf fmt "{@[\"type\": \"variable\",@ \"value\": \"%a\",@ "
312 311
       print_protect (fun fmt -> pp_print_string fmt id);
313
    fprintf fmt "\"datatype\": \"%t\"@ "
312
    fprintf fmt "\"datatype\": %t@ "
314 313
      (pp_concrete_type
315 314
	 Tydec_any (* don't know much about that time since it was not
316 315
		      declared. That may not work with clock constants *)
......
338 337
      (fun fmt ->
339 338
	fprintf fmt "Warning: unhandled expression %a in annotation.@ "
340 339
	  Printers.pp_expr e;
341
	fprintf fmt "Will not be produced in the experted JSON EMF"
340
 	fprintf fmt "Will not be produced in the experted JSON EMF@."
342 341
      );    
343 342
    fprintf fmt "\"unhandled construct, complain to Ploc\""
344 343
  )
......
357 356
let pp_emf_exprs = pp_emf_list pp_emf_expr
358 357
       
359 358
let pp_emf_const fmt v =
360
  fprintf fmt "@[{\"name\": \"%a\", \"datatype\":\"%a\", \"original_name\": \"%a\", \"value\": \"%a\"}@]"
359
  fprintf fmt "@[<hov 0>{\"name\": \"%a\",@ \"datatype\":%a,@ \"original_name\": \"%a\",@ \"value\": %a}@]"
361 360
    pp_var_name v
362 361
    pp_var_type v
363 362
    Printers.pp_var_name v
......
386 385
  | Eq eq -> (
387 386
    fprintf fmt "@[ @[<v 2>\"%a\": {@ " (Utils.fprintf_list ~sep:"_" pp_print_string) eq.eq_lhs;
388 387
    fprintf fmt "\"lhs\": [%a],@ " (Utils.fprintf_list ~sep:", " (fun fmt vid -> fprintf fmt "\"%s\"" vid)) eq.eq_lhs;
389
    fprintf fmt "\"rhs\": \"%a\",@ " pp_emf_expr eq.eq_rhs;
388
    fprintf fmt "\"rhs\": %a,@ " pp_emf_expr eq.eq_rhs;
390 389
    fprintf fmt "@]@]@ }"
391 390
  )
392 391

  
......
419 418
  
420 419
let pp_emf_top_const fmt const_top = 
421 420
  let const = Corelang.const_of_top const_top in
422
  fprintf fmt "\"%s\": " const.const_id;
423
  pp_emf_cst const.const_value const.const_type fmt
421
  fprintf fmt "{ \"%s\": %t }"
422
    const.const_id
423
    (pp_emf_cst const.const_value const.const_type)
424 424

  
425 425
(* Local Variables: *)
426 426
(* compile-command: "make -C ../.." *)
src/backends/Horn/horn_backend_printers.ml
294 294
(* Print the instruction and update the set of reset instances *)
295 295
let rec pp_machine_instr machines reset_instances (m: machine_t) fmt instr : ident list =
296 296
  match get_instr_desc instr with
297
  | MComment _ -> reset_instances
297
  | MSpec _ | MComment _ -> reset_instances
298 298
  | MNoReset i -> (* we assign middle_mem with mem_m. And declare i as reset *)
299 299
    pp_no_reset machines m fmt i;
300 300
    i::reset_instances

Also available in: Unified diff