Project

General

Profile

Download (5.38 KB) Statistics
| Branch: | Tag: | Revision:
1
open Seal_slice
2
open Seal_extract
3
open Seal_utils
4

    
5
let active = ref false
6
let seal_export = ref None
7

    
8
let set_export s = match s with
9
  | "lustre" | "lus" | "m" | "matlab" -> seal_export := Some s
10
  | _ -> (Format.eprintf "Unrecognized seal export: %s@.@?" s; exit 1)
11
(* TODO
12

    
13
   - build the output function: for the moment we slice the node with
14
   its memories, building the function updating the memory. We will
15
   need later the output function, using inputs and memories to
16
   compute the output. A way to do this would be to declared memories
17
   as input, remove their definitions, and slice the node with its
18
   outputs. This should clean up unnecessary internal variables and
19
   give us the output function.
20

    
21
   - compute the dimension of the node (nb of memories)
22

    
23
   - if the updates are all linear or affine, provide the update as a
24
   matrix rather then a polynomial. Check if this is simpler to do
25
   here or in matlab.
26

    
27
   - analyzes require bounds on inputs or sometimes target property 
28
   over states. These could be specified as node annotations: eg     
29
     - /seal/bounds/inputs/semialg/: (in1^4 + in2^3, 1) 
30
       to specify that the inputs are constrained by a semialgebraic 
31
       set (p,b) such as p(inputs) <= b
32
     - /seal/bounds/inputs/LMI/: (todo_describe_a_matrix) .... and so on. 
33
       To be defined depending on needs.
34
     - /seal/prop/semialg/: (x3 - x5, 2) -- if x3 - x5 <= 2 is 
35
       the property to prove
36
     
37
 *)
38
           
39
(* Select the appropriate node, should have been inlined already and
40
   extract update/output functions. *)
41
let seal_run ~basename prog machines =
42
  let node_name =
43
    match !Options.main_node with
44
    | "" -> (
45
      Format.eprintf "SEAL verifier requires a main node.@.";
46
      Format.eprintf "@[<v 2>Available ones are:@ %a@]@.@?"
47
        (Utils.fprintf_list ~sep:"@ "
48
           (fun fmt m ->
49
             Format.fprintf fmt "%s" m.Machine_code_types.mname.node_id
50
           )
51
        )
52
        machines; 
53
      exit 1
54
    )
55
    | s -> ( (* should have been addessed before *)
56
      match Machine_code_common.get_machine_opt machines s with
57
      | None -> begin
58
          Global.main_node := s;
59
          Format.eprintf "Code generation error: %a@." Error.pp_error_msg Error.Main_not_found;
60
          raise (Corelang.Error (Location.dummy_loc, Error.Main_not_found))
61
        end
62
      | Some _ -> s
63
    )
64
  in
65
  let m = Machine_code_common.get_machine machines node_name in
66
  let nd = m.mname in
67
  (* Format.eprintf "Node %a@." Printers.pp_node nd; *)
68
  let mems = m.mmemory in
69
  (* Format.eprintf "Mems: %a@." (Utils.fprintf_list ~sep:"; " Printers.pp_var) mems; *)
70
  let msch = Utils.desome m.msch in
71
  (* Format.eprintf "graph: %a@." Causality.pp_dep_graph deps; *)
72
  let sliced_nd = slice_node (mems@nd.node_outputs) msch nd in
73
  if false then Format.eprintf "Sliced Node %a@." Printers.pp_node sliced_nd;
74
  report ~level:3 (fun fmt -> Format.fprintf fmt "Node sliced@.");
75

    
76
  let consts = Corelang.(List.map const_of_top (get_consts prog)) in
77
  let sw_init, sw_sys, init_out, update_out = node_as_switched_sys consts mems sliced_nd in
78
  let pp_res pp_expr =
79
    (Utils.fprintf_list ~sep:"@ "
80
       (fun fmt (g, up) ->
81
         Format.fprintf fmt "@[<v 2>[%t]:@ %a@]"
82
           (fun fmt -> match g with None -> () | Some g -> pp_expr fmt g)
83
           
84
           (* (Utils.fprintf_list ~sep:"; "
85
            *    (fun fmt (e,b) ->
86
            *      if b then pp_expr fmt e
87
            *      else Format.fprintf fmt "not(%a)"
88
            *             pp_expr e)) gel *)
89
           (Utils.fprintf_list ~sep:";@ "
90
              (fun fmt (id, e) ->
91
                Format.fprintf fmt "%s = @[<hov 0>%a@]"
92
                  id
93
                  pp_expr e)) up
94
    ))
95
  in
96
  report ~level:1 (
97
      fun fmt -> Format.fprintf fmt
98
                   "%i memories, %i init, %i step switch cases@."
99
                   (List.length mems)
100
                   (List.length sw_init)
101
                   (List.length sw_sys)
102
               
103
    );
104
  report ~level:1 (fun fmt ->
105
      (*let pp_res = pp_res (fun fmt e -> Format.fprintf fmt "%i" e.Lustre_types.expr_tag)  in*)
106
       let pp_res = pp_res Printers.pp_expr in
107
      Format.fprintf fmt "DynSys:@ @[<v 0>@[<v 3>Init:@ %a@]@ "
108
        pp_res  sw_init;
109
      Format.fprintf fmt "@[<v 3>Step:@ %a@]@]@ "
110
        pp_res  sw_sys
111
    );
112
  report ~level:1 (fun fmt ->
113
      (*let pp_res = pp_res (fun fmt e -> Format.fprintf fmt "%i" e.Lustre_types.expr_tag)  in*)
114
       let pp_res = pp_res Printers.pp_expr in
115
      Format.fprintf fmt "Output:@ @[<v 0>@[<v 3>Init:@ %a@]@ "
116
        pp_res  init_out;
117
      Format.fprintf fmt "@[<v 3>Step:@ %a@]@]@ "
118
        pp_res  update_out
119
    );
120
  let _ = match !seal_export with
121
    | Some "lustre" | Some "lus" ->
122
       Seal_export.to_lustre basename prog m sw_init sw_sys init_out update_out  
123
    | Some "matlab" | Some "m" -> assert false (* TODO *)
124
    | Some _ -> assert false 
125
    | None -> ()
126
  in
127
  ()
128
  
129
module Verifier =
130
  (struct
131
    include VerifierType.Default
132
    let name = "seal"
133
    let options =
134
      [
135
        "-export", Arg.String set_export, "seal export option (lustre, matlab)";
136
        "-debug", Arg.Set seal_debug, "seal debug"
137

    
138
      ]
139
    let activate () =
140
      active := true;
141
      Options.global_inline := true;
142
      Options.optimization := 0;
143
      Options.const_unfold := true;
144
      ()
145
      
146
    let is_active () = !active
147
    let run = seal_run
148
      
149
                    
150
  end: VerifierType.S)
151
    
(5-5/5)