Project

General

Profile

Download (9.64 KB) Statistics
| Branch: | Tag: | Revision:
1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT - ISAE-SUPAERO     *)
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
open Format
13
open Machine_code_types
14
open Lustre_types
15
open Misc_lustre_function
16
open Ada_printer
17
open Ada_backend_common
18

    
19
(** Functions printing the .ads file **)
20
module Main = struct
21
  let rec init f = function i when i < 0 -> [] | i -> f i :: init f (i - 1)
22
  (*should be replaced by the init of list from ocaml std lib*)
23

    
24
  let suffixOld = "_old"
25

    
26
  let suffixNew = "_new"
27

    
28
  let pp_invariant_name fmt = fprintf fmt "inv"
29

    
30
  let pp_transition_name fmt = fprintf fmt "transition"
31

    
32
  let pp_init_name fmt = fprintf fmt "init"
33

    
34
  let pp_state_name_predicate suffix fmt =
35
    fprintf fmt "%t%s" pp_state_name suffix
36

    
37
  let pp_axiomatize_package_name fmt = fprintf fmt "axiomatize"
38

    
39
  (** Print the expression function representing the transition predicate.
40
      @param fmt the formater to print on **)
41
  let pp_init_predicate fmt () =
42
    let new_state =
43
      AdaIn, pp_state_name_predicate suffixNew, pp_state_type, None
44
    in
45
    pp_predicate pp_init_name [ [ new_state ] ] true fmt None
46

    
47
  (** Print the expression function representing the transition predicate.
48
      @param fmt the formater to print on @param machine the machine **)
49
  let pp_transition_predicate fmt (_, m) =
50
    let old_state =
51
      AdaIn, pp_state_name_predicate suffixOld, pp_state_type, None
52
    in
53
    let new_state =
54
      AdaIn, pp_state_name_predicate suffixNew, pp_state_type, None
55
    in
56
    let inputs = build_pp_var_decl_step_input AdaIn None m in
57
    let outputs = build_pp_var_decl_step_output AdaIn None m in
58
    pp_predicate pp_transition_name
59
      ([ [ old_state; new_state ] ] @ inputs @ outputs)
60
      true fmt None
61

    
62
  let pp_invariant_predicate fmt () =
63
    pp_predicate pp_invariant_name
64
      [ [ build_pp_state_decl AdaIn None ] ]
65
      true fmt None
66

    
67
  (** Print a new statement instantiating a generic package. @param fmt the
68
      formater to print on @param substitutions the instanciation substitution
69
      @param machine the machine to instanciate **)
70
  let pp_new_package fmt (substitutions, machine) =
71
    let pp_name = pp_package_name machine in
72
    let pp_new_name = pp_package_name_with_polymorphic substitutions machine in
73
    let instanciations =
74
      List.map
75
        (fun (id, typ) -> pp_polymorphic_type id, fun fmt -> pp_type fmt typ)
76
        substitutions
77
    in
78
    pp_package_instanciation pp_new_name pp_name fmt instanciations
79

    
80
  (** Remove duplicates from a list according to a given predicate. @param eq
81
      the predicate defining equality @param l the list to parse **)
82
  let remove_duplicates eq l =
83
    let aux l x = if List.exists (eq x) l then l else x :: l in
84
    List.fold_left aux [] l
85

    
86
  (** Compare two typed machines. **)
87
  let eq_typed_machine (subst1, machine1) (subst2, machine2) =
88
    String.equal machine1.mname.node_id machine2.mname.node_id
89
    && List.for_all2 (fun a b -> pp_eq_type (snd a) (snd b)) subst1 subst2
90

    
91
  (** Print the package declaration(ads) of a machine. It requires the list of
92
      all typed instance. A typed submachine is a (ident, typed_machine) with -
93
      ident: the name - typed_machine: a (substitution, machine) with - machine:
94
      the submachine struct - substitution the instanciation of all its
95
      polymorphic types. @param fmt the formater to print on @param
96
      typed_submachines list of all typed submachines of this machine @param m
97
      the machine **)
98
  let pp_file fmt (typed_submachines, ((m_spec_opt, guarantees), m)) =
99
    let typed_machines = snd (List.split typed_submachines) in
100
    let typed_machines_set =
101
      remove_duplicates eq_typed_machine typed_machines
102
    in
103

    
104
    let machines_to_import =
105
      List.map pp_package_name (snd (List.split typed_machines_set))
106
    in
107

    
108
    let polymorphic_types = find_all_polymorphic_type m in
109

    
110
    let typed_machines_to_instanciate =
111
      List.filter (fun (l, _) -> l != []) typed_machines_set
112
    in
113

    
114
    let typed_instances =
115
      List.filter is_submachine_statefull typed_submachines
116
    in
117

    
118
    let memories =
119
      match m_spec_opt with
120
      | None ->
121
        []
122
      | Some m ->
123
        List.map
124
          (fun x ->
125
            pp_var_decl
126
              (build_pp_var_decl AdaNoMode (Some (true, false, [], [])) x))
127
          m.mmemory
128
    in
129
    let ghost_private = memories in
130
    (* Commented since not used. Could be reinjected in the code let vars_spec =
131
       match m_spec_opt with | None -> [] | Some m_spec -> List.map
132
       (build_pp_var_decl AdaNoMode (Some (true, false, [], [])))
133
       (m_spec.mmemory) in *)
134
    let vars = List.map (build_pp_var_decl AdaNoMode None) m.mmemory in
135
    let states =
136
      List.map
137
        (build_pp_state_decl_from_subinstance AdaNoMode None)
138
        typed_instances
139
    in
140
    let var_lists =
141
      (if states = [] then [] else [ states ])
142
      @ if vars = [] then [] else [ vars ]
143
    in
144

    
145
    let pp_ifstatefull fmt pp =
146
      if is_machine_statefull m then fprintf fmt "%t" pp else fprintf fmt ""
147
    in
148

    
149
    let pp_state_decl_and_reset fmt =
150
      let init fmt =
151
        pp_call fmt
152
          ( pp_access pp_axiomatize_package_name pp_init_name,
153
            [ [ pp_state_name ] ] )
154
      in
155
      let contract = Some (false, false, [], [ init ]) in
156
      fprintf fmt "%t;@,@,%a;@,@,"
157
        (*Declare the state type*)
158
        (pp_type_decl pp_state_type AdaPrivate)
159
        (*Declare the reset procedure*)
160
        (pp_procedure pp_reset_procedure_name (build_pp_arg_reset m) contract)
161
        AdaNoContent
162
    in
163

    
164
    let pp_private_section fmt =
165
      fprintf fmt "@,private@,@,%a%t%a%t%a"
166
        (*Instantiate the polymorphic type that need to be instantiated*)
167
        (Utils.fprintf_list ~sep:";@," pp_new_package)
168
        typed_machines_to_instanciate
169
        (Utils.pp_final_char_if_non_empty ";@,@," typed_machines_to_instanciate)
170
        (*Define the state type*)
171
        pp_ifstatefull
172
        (fun fmt -> pp_record pp_state_type fmt var_lists)
173
        (Utils.pp_final_char_if_non_empty ";@,@," ghost_private)
174
        (Utils.fprintf_list ~sep:";@," (fun fmt pp -> pp fmt))
175
        ghost_private
176
    in
177

    
178
    let pp_content fmt =
179
      let pp_contract_opt =
180
        let pp_var x fmt = pp_clean_ada_identifier fmt x in
181
        let guarantee_post_conditions = List.map pp_var guarantees in
182
        let state_pre_conditions, state_post_conditions =
183
          if is_machine_statefull m then
184
            let input = List.map pp_var_name m.mstep.step_inputs in
185
            let output = List.map pp_var_name m.mstep.step_outputs in
186
            let args =
187
              [ [ pp_old pp_state_name; pp_state_name ] ]
188
              @ (if input != [] then [ input ] else [])
189
              @ if output != [] then [ output ] else []
190
            in
191
            let transition fmt =
192
              pp_call fmt
193
                (pp_access pp_axiomatize_package_name pp_transition_name, args)
194
            in
195
            let invariant fmt =
196
              pp_call fmt
197
                ( pp_access pp_axiomatize_package_name pp_invariant_name,
198
                  [ [ pp_state_name ] ] )
199
            in
200
            [ invariant ], [ transition; invariant ]
201
          else [], []
202
        in
203
        let post_conditions =
204
          state_post_conditions @ guarantee_post_conditions
205
        in
206
        let pre_conditions = state_pre_conditions in
207
        if post_conditions = [] && pre_conditions = [] then None
208
        else Some (false, false, pre_conditions, post_conditions)
209
      in
210
      let pp_guarantee name =
211
        pp_var_decl
212
          ( AdaNoMode,
213
            (fun fmt -> pp_clean_ada_identifier fmt name),
214
            pp_boolean_type,
215
            Some (true, false, [], []) )
216
      in
217
      let ghost_public = List.map pp_guarantee guarantees in
218
      fprintf fmt "@,%a%t%a%a%a@,@,%a;@,@,%t"
219
        (Utils.fprintf_list ~sep:";@," (fun fmt pp -> pp fmt))
220
        ghost_public
221
        (Utils.pp_final_char_if_non_empty ";@,@," ghost_public)
222
        pp_ifstatefull pp_state_decl_and_reset
223
        (*Declare the step procedure*)
224
        (pp_procedure pp_step_procedure_name (build_pp_arg_step m)
225
           pp_contract_opt)
226
        AdaNoContent pp_ifstatefull
227
        (fun fmt -> fprintf fmt ";@,")
228
        (pp_package pp_axiomatize_package_name [] false)
229
        (fun fmt ->
230
          fprintf fmt
231
            "pragma Annotate (GNATProve, External_Axiomatization);@,\
232
             @,\
233
             %a;@,\
234
             %a;@,\
235
             %a"
236
            (*Declare the init predicate*)
237
            pp_init_predicate ()
238
            (*Declare the transition predicate*)
239
            pp_transition_predicate (m_spec_opt, m)
240
            (*Declare the invariant predicate*)
241
            pp_invariant_predicate ())
242
        (*Print the private section*)
243
        pp_private_section
244
    in
245

    
246
    let pp_poly_type id = pp_type_decl (pp_polymorphic_type id) AdaPrivate in
247
    let pp_generics = List.map pp_poly_type polymorphic_types in
248

    
249
    fprintf fmt "@[<v>%a%t%a;@]@."
250
      (* Include all the subinstance package*)
251
      (Utils.fprintf_list ~sep:";@," (pp_with AdaNoVisibility))
252
      machines_to_import
253
      (Utils.pp_final_char_if_non_empty ";@,@," machines_to_import)
254
      (*Begin the package*)
255
      (pp_package (pp_package_name m) pp_generics false)
256
      pp_content
257
end
(4-4/11)