Project

General

Profile

Download (9.27 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

    
14
open Machine_code_types
15
open Lustre_types
16

    
17
open Misc_lustre_function
18
open Ada_printer
19
open Ada_backend_common
20

    
21

    
22

    
23
(** Functions printing the .ads file **)
24
module Main =
25
struct
26

    
27
  let rec init f = function i when i < 0 -> [] | i -> (f i)::(init f (i-1)) (*should be replaced by the init of list from ocaml std lib*)
28

    
29
  let suffixOld = "_old"
30
  let suffixNew = "_new"
31
  let pp_invariant_name fmt = fprintf fmt "inv"
32
  let pp_transition_name fmt = fprintf fmt "transition"
33
  let pp_init_name fmt = fprintf fmt "init"
34
  let pp_state_name_predicate suffix fmt = fprintf fmt "%t%s" pp_state_name suffix
35
  let pp_axiomatize_package_name fmt = fprintf  fmt "axiomatize"
36

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

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

    
55
  let pp_invariant_predicate fmt () =
56
    pp_predicate pp_invariant_name [[build_pp_state_decl AdaIn None]] true fmt None
57

    
58
  (** Print a new statement instantiating a generic package.
59
     @param fmt the formater to print on
60
     @param substitutions the instanciation substitution
61
     @param machine the machine to instanciate
62
  **)
63
  let pp_new_package fmt (substitutions, machine) =
64
    let pp_name = pp_package_name machine in
65
    let pp_new_name = pp_package_name_with_polymorphic substitutions machine in
66
    let instanciations = List.map (fun (id, typ) -> (pp_polymorphic_type id, fun fmt -> pp_type fmt typ)) substitutions in
67
    pp_package_instanciation pp_new_name pp_name fmt instanciations
68

    
69
  (** Remove duplicates from a list according to a given predicate.
70
     @param eq the predicate defining equality
71
     @param l the list to parse
72
  **)
73
  let remove_duplicates eq l =
74
    let aux l x = if List.exists (eq x) l then l else x::l in
75
    List.fold_left aux [] l
76

    
77

    
78
  (** Compare two typed machines.
79
  **)
80
  let eq_typed_machine (subst1, machine1) (subst2, machine2) =
81
    (String.equal machine1.mname.node_id machine2.mname.node_id) &&
82
    (List.for_all2 (fun a b -> pp_eq_type (snd a) (snd b)) subst1 subst2)
83

    
84

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

    
102
    let polymorphic_types = find_all_polymorphic_type m in
103
    
104
    let typed_machines_to_instanciate =
105
      List.filter (fun (l, _) -> l != []) typed_machines_set in
106

    
107
    let typed_instances = List.filter is_submachine_statefull typed_submachines in
108

    
109
    let memories = match m_spec_opt with
110
      | None -> []
111
      | Some m -> List.map (fun x-> pp_var_decl (build_pp_var_decl AdaNoMode (Some (true, false, [], [])) x)) m.mmemory
112
    in
113
    let ghost_private = memories in
114
    (* Commented since not used. Could be reinjected in the code 
115
    let vars_spec = match m_spec_opt with
116
      | None -> []
117
      | Some m_spec -> List.map (build_pp_var_decl AdaNoMode (Some (true, false, [], []))) (m_spec.mmemory)
118
    in *)
119
    let vars = List.map (build_pp_var_decl AdaNoMode None) m.mmemory in
120
    let states = List.map (build_pp_state_decl_from_subinstance AdaNoMode None) typed_instances in
121
    let var_lists =
122
      (if states = [] then [] else [states]) @
123
      (if vars = [] then [] else [vars]) in
124
    
125
    let pp_ifstatefull fmt pp =
126
      if is_machine_statefull m then
127
        fprintf fmt "%t" pp
128
      else
129
        fprintf fmt ""
130
    in
131

    
132
    let pp_state_decl_and_reset fmt =
133
      let init fmt = pp_call fmt (pp_access pp_axiomatize_package_name pp_init_name, [[pp_state_name]]) in
134
      let contract = Some (false, false, [], [init]) in
135
      fprintf fmt "%t;@,@,%a;@,@,"
136
        (*Declare the state type*)
137
        (pp_type_decl pp_state_type AdaPrivate)
138
        
139
        (*Declare the reset procedure*)
140
        (pp_procedure pp_reset_procedure_name (build_pp_arg_reset m) contract) AdaNoContent
141
    in
142

    
143
    let pp_private_section fmt =
144
      fprintf fmt "@,private@,@,%a%t%a%t%a"
145
      (*Instantiate the polymorphic type that need to be instantiated*)
146
      (Utils.fprintf_list ~sep:";@," pp_new_package) typed_machines_to_instanciate
147
      (Utils.pp_final_char_if_non_empty ";@,@," typed_machines_to_instanciate)
148
      
149
      (*Define the state type*)
150
      pp_ifstatefull (fun fmt-> pp_record pp_state_type fmt var_lists)
151
        
152
      (Utils.pp_final_char_if_non_empty ";@,@," ghost_private)
153
      (Utils.fprintf_list ~sep:";@," (fun fmt pp -> pp fmt)) ghost_private
154
    in
155

    
156
    let pp_content fmt =
157
      let pp_contract_opt =
158
        let pp_var x fmt =
159
            pp_clean_ada_identifier fmt x
160
        in
161
        let guarantee_post_conditions = List.map pp_var guarantees in
162
        let state_pre_conditions, state_post_conditions =
163
          if is_machine_statefull m then
164
          begin
165
            let input = List.map pp_var_name m.mstep.step_inputs in
166
            let output = List.map pp_var_name m.mstep.step_outputs in
167
            let args =
168
              [[pp_old pp_state_name;pp_state_name]]
169
                @(if input!=[] then [input] else [])
170
                @(if output!=[] then [output] else [])
171
            in
172
            let transition fmt = pp_call fmt (pp_access pp_axiomatize_package_name pp_transition_name, args) in
173
            let invariant fmt = pp_call fmt (pp_access pp_axiomatize_package_name pp_invariant_name, [[pp_state_name]]) in
174
            [invariant], [transition;invariant]
175
          end
176
          else
177
            [], []
178
        in
179
        let post_conditions = state_post_conditions@guarantee_post_conditions in
180
        let pre_conditions = state_pre_conditions in
181
        if post_conditions = [] && pre_conditions = [] then
182
          None
183
        else
184
          Some (false, false, pre_conditions, post_conditions)
185
      in
186
      let pp_guarantee name = pp_var_decl (AdaNoMode, (fun fmt -> pp_clean_ada_identifier fmt name), pp_boolean_type , (Some (true, false, [], []))) in
187
      let ghost_public = List.map pp_guarantee guarantees in
188
      fprintf fmt "@,%a%t%a%a%a@,@,%a;@,@,%t"
189
        
190
        (Utils.fprintf_list ~sep:";@," (fun fmt pp -> pp fmt)) ghost_public
191
        (Utils.pp_final_char_if_non_empty ";@,@," ghost_public)
192
        
193
        pp_ifstatefull pp_state_decl_and_reset
194
        
195
        (*Declare the step procedure*)
196
        (pp_procedure pp_step_procedure_name (build_pp_arg_step m) pp_contract_opt) AdaNoContent
197
        
198
        pp_ifstatefull (fun fmt -> fprintf fmt ";@,")
199
        
200
        (pp_package (pp_axiomatize_package_name) [] false)
201
          (fun fmt -> fprintf fmt "pragma Annotate (GNATProve, External_Axiomatization);@,@,%a;@,%a;@,%a"
202
            (*Declare the init predicate*)
203
            pp_init_predicate ()
204
            (*Declare the transition predicate*)
205
            pp_transition_predicate (m_spec_opt, m)
206
            (*Declare the invariant predicate*)
207
            pp_invariant_predicate ())
208
        
209
        (*Print the private section*)
210
        pp_private_section
211
    in
212
    
213
    let pp_poly_type id = pp_type_decl (pp_polymorphic_type id) AdaPrivate in
214
    let pp_generics = List.map pp_poly_type polymorphic_types in
215
    
216
    fprintf fmt "@[<v>%a%t%a;@]@."
217
      
218
      (* Include all the subinstance package*)
219
      (Utils.fprintf_list ~sep:";@," (pp_with AdaNoVisibility)) machines_to_import
220
      (Utils.pp_final_char_if_non_empty ";@,@," machines_to_import)
221
      
222
      (*Begin the package*)
223
      (pp_package (pp_package_name m) pp_generics false) pp_content
224

    
225
end
(4-4/11)