1 |
f20d8ac7
|
Christophe Garion
|
(********************************************************************)
|
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 |
48a6309c
|
Guillaume DAVY
|
open Format
|
13 |
|
|
open Machine_code_types
|
14 |
|
|
open Lustre_types
|
15 |
|
|
open Corelang
|
16 |
|
|
open Machine_code_common
|
17 |
230b168e
|
Guillaume DAVY
|
open Misc_printer
|
18 |
|
|
open Misc_lustre_function
|
19 |
|
|
open Ada_printer
|
20 |
bdc471f3
|
Guillaume DAVY
|
open Ada_backend_common
|
21 |
|
|
|
22 |
ca7ff3f7
|
Lélio Brun
|
(** Main module for generating packages bodies **)
|
23 |
|
|
module Main = struct
|
24 |
b5b745fb
|
Guillaume DAVY
|
(** Printing function for basic assignement [var := value].
|
25 |
768f60f0
|
Christophe Garion
|
|
26 |
ca7ff3f7
|
Lélio Brun
|
@param fmt the formater to print on @param var_name the name of the
|
27 |
|
|
variable @param value the value to be assigned **)
|
28 |
b5b745fb
|
Guillaume DAVY
|
let pp_assign env fmt var value =
|
29 |
ca7ff3f7
|
Lélio Brun
|
fprintf fmt "%a := %a" (pp_var env) var (pp_value env) value
|
30 |
768f60f0
|
Christophe Garion
|
|
31 |
ca7ff3f7
|
Lélio Brun
|
(** Printing function for instruction. See {!type:Machine_code_types.instr_t}
|
32 |
|
|
for more details on machine types.
|
33 |
768f60f0
|
Christophe Garion
|
|
34 |
ca7ff3f7
|
Lélio Brun
|
@param typed_submachines list of all typed machine instances of this
|
35 |
|
|
machine @param machine the current machine @param fmt the formater to
|
36 |
|
|
print on @param instr the instruction to print **)
|
37 |
b5b745fb
|
Guillaume DAVY
|
let rec pp_machine_instr typed_submachines env instr fmt =
|
38 |
|
|
let pp_instr = pp_machine_instr typed_submachines env in
|
39 |
6e3cdaf6
|
Guillaume DAVY
|
(* Print args for a step call *)
|
40 |
826063db
|
Guillaume DAVY
|
let pp_state i fmt = fprintf fmt "%t.%s" pp_state_name i in
|
41 |
6e3cdaf6
|
Guillaume DAVY
|
(* Print a when branch of a case *)
|
42 |
230b168e
|
Guillaume DAVY
|
let pp_when (cond, instrs) fmt =
|
43 |
|
|
fprintf fmt "when %s =>@,%a" cond pp_block (List.map pp_instr instrs)
|
44 |
6e3cdaf6
|
Guillaume DAVY
|
in
|
45 |
|
|
(* Print a case *)
|
46 |
525eebd1
|
Guillaume DAVY
|
let pp_case fmt (g, hl) =
|
47 |
ca7ff3f7
|
Lélio Brun
|
fprintf fmt "case %a is@,%aend case" (pp_value env) g pp_block
|
48 |
|
|
(List.map pp_when hl)
|
49 |
525eebd1
|
Guillaume DAVY
|
in
|
50 |
|
|
(* Print a if *)
|
51 |
|
|
(* If neg is true the we must test for the negation of the condition. It
|
52 |
|
|
first check that we don't have a negation and a else case, if so it
|
53 |
ca7ff3f7
|
Lélio Brun
|
inverses the two branch and remove the negation doing a recursive call. *)
|
54 |
b5b745fb
|
Guillaume DAVY
|
let pp_if fmt (neg, g, instrs1, instrs2) =
|
55 |
|
|
let pp_cond =
|
56 |
ca7ff3f7
|
Lélio Brun
|
if neg then fun fmt x -> fprintf fmt "! (%a)" (pp_value env) x
|
57 |
|
|
else pp_value env
|
58 |
b5b745fb
|
Guillaume DAVY
|
in
|
59 |
ca7ff3f7
|
Lélio Brun
|
let pp_else =
|
60 |
|
|
match instrs2 with
|
61 |
|
|
| None ->
|
62 |
|
|
fun fmt -> fprintf fmt ""
|
63 |
|
|
| Some i2 ->
|
64 |
|
|
fun fmt -> fprintf fmt "else@,%a" pp_block (List.map pp_instr i2)
|
65 |
b5b745fb
|
Guillaume DAVY
|
in
|
66 |
ca7ff3f7
|
Lélio Brun
|
fprintf fmt "if %a then@,%a%tend if" pp_cond g pp_block
|
67 |
|
|
(List.map pp_instr instrs1)
|
68 |
b5b745fb
|
Guillaume DAVY
|
pp_else
|
69 |
6e3cdaf6
|
Guillaume DAVY
|
in
|
70 |
768f60f0
|
Christophe Garion
|
match get_instr_desc instr with
|
71 |
ca7ff3f7
|
Lélio Brun
|
(* no reset *)
|
72 |
|
|
| MNoReset _ ->
|
73 |
|
|
()
|
74 |
|
|
(* TODO: handle clear_reset *)
|
75 |
|
|
| MClearReset ->
|
76 |
|
|
()
|
77 |
|
|
(* reset *)
|
78 |
|
|
| MSetReset i when List.mem_assoc i typed_submachines ->
|
79 |
|
|
let substitution, submachine = get_instance i typed_submachines in
|
80 |
|
|
let pp_package =
|
81 |
|
|
pp_package_name_with_polymorphic substitution submachine
|
82 |
|
|
in
|
83 |
|
|
let args =
|
84 |
|
|
if is_machine_statefull submachine then [ [ pp_state i ] ] else []
|
85 |
|
|
in
|
86 |
|
|
pp_call fmt (pp_package_access (pp_package, pp_reset_procedure_name), args)
|
87 |
|
|
| MLocalAssign (ident, value) ->
|
88 |
|
|
pp_assign env fmt ident value
|
89 |
|
|
| MStateAssign (ident, value) ->
|
90 |
|
|
pp_assign env fmt ident value
|
91 |
|
|
| MStep ([ i0 ], i, vl) when is_builtin_fun i ->
|
92 |
|
|
let value = mk_val (Fun (i, vl)) i0.var_type in
|
93 |
|
|
pp_assign env fmt i0 value
|
94 |
|
|
| MStep (il, i, vl) when List.mem_assoc i typed_submachines ->
|
95 |
|
|
let substitution, submachine = get_instance i typed_submachines in
|
96 |
|
|
let pp_package =
|
97 |
|
|
pp_package_name_with_polymorphic substitution submachine
|
98 |
|
|
in
|
99 |
|
|
let input = List.map (fun x fmt -> pp_value env fmt x) vl in
|
100 |
|
|
let output = List.map pp_var_name il in
|
101 |
|
|
let args =
|
102 |
|
|
(if is_machine_statefull submachine then [ [ pp_state i ] ] else [])
|
103 |
|
|
@ (if input != [] then [ input ] else [])
|
104 |
|
|
@ if output != [] then [ output ] else []
|
105 |
|
|
in
|
106 |
|
|
pp_call fmt (pp_package_access (pp_package, pp_step_procedure_name), args)
|
107 |
|
|
| MBranch (_, []) ->
|
108 |
|
|
assert false
|
109 |
|
|
| MBranch (g, (c1, i1) :: tl) when c1 = tag_false || c1 = tag_true ->
|
110 |
|
|
pp_if fmt (build_if g c1 i1 tl)
|
111 |
|
|
| MBranch (g, hl) ->
|
112 |
|
|
pp_case fmt (g, hl)
|
113 |
|
|
| MComment s ->
|
114 |
|
|
let lines = String.split_on_char '\n' s in
|
115 |
|
|
fprintf fmt "%a" (Utils.fprintf_list ~sep:"" pp_oneline_comment) lines
|
116 |
|
|
| _ ->
|
117 |
|
|
assert false
|
118 |
3d85297f
|
Guillaume DAVY
|
|
119 |
8d22ea35
|
Guillaume DAVY
|
(** Print the definition of the step procedure from a machine.
|
120 |
|
|
|
121 |
ca7ff3f7
|
Lélio Brun
|
@param typed_submachines list of all typed machine instances of this
|
122 |
|
|
machine @param fmt the formater to print on @param machine the machine **)
|
123 |
173a2a8f
|
Guillaume DAVY
|
let pp_step_definition env typed_submachines fmt (m, m_spec_opt, guarantees) =
|
124 |
ca7ff3f7
|
Lélio Brun
|
let transform_local_to_state_assign instr =
|
125 |
|
|
match instr.instr_desc with
|
126 |
|
|
| MLocalAssign (ident, value) ->
|
127 |
75c459f4
|
Lélio Brun
|
{ instr with instr_desc = MStateAssign (ident, value) }
|
128 |
ca7ff3f7
|
Lélio Brun
|
| _ ->
|
129 |
|
|
instr
|
130 |
230b168e
|
Guillaume DAVY
|
in
|
131 |
ca7ff3f7
|
Lélio Brun
|
let pp_local_ghost_list, spec_instrs =
|
132 |
|
|
match m_spec_opt with
|
133 |
|
|
| None ->
|
134 |
|
|
[], []
|
135 |
173a2a8f
|
Guillaume DAVY
|
| Some m_spec ->
|
136 |
ca7ff3f7
|
Lélio Brun
|
( List.map
|
137 |
|
|
(build_pp_var_decl_local (Some (true, false, [], [])))
|
138 |
|
|
(List.filter
|
139 |
|
|
(fun x -> not (List.mem x.var_id guarantees))
|
140 |
|
|
m_spec.mstep.step_locals),
|
141 |
|
|
List.map transform_local_to_state_assign m_spec.mstep.step_instrs )
|
142 |
|
|
in
|
143 |
|
|
let pp_local_list =
|
144 |
|
|
List.map (build_pp_var_decl_local None) m.mstep.step_locals
|
145 |
|
|
in
|
146 |
|
|
let pp_instr_list =
|
147 |
|
|
List.map
|
148 |
|
|
(pp_machine_instr typed_submachines env)
|
149 |
|
|
(m.mstep.step_instrs @ spec_instrs)
|
150 |
|
|
in
|
151 |
|
|
let content =
|
152 |
|
|
AdaProcedureContent
|
153 |
|
|
( ((if pp_local_ghost_list = [] then [] else [ pp_local_ghost_list ])
|
154 |
|
|
@ if pp_local_list = [] then [] else [ pp_local_list ]),
|
155 |
|
|
pp_instr_list )
|
156 |
230b168e
|
Guillaume DAVY
|
in
|
157 |
|
|
pp_procedure pp_step_procedure_name (build_pp_arg_step m) None fmt content
|
158 |
8d22ea35
|
Guillaume DAVY
|
|
159 |
|
|
(** Print the definition of the reset procedure from a machine.
|
160 |
|
|
|
161 |
ca7ff3f7
|
Lélio Brun
|
@param typed_submachines list of all typed machine instances of this
|
162 |
|
|
machine @param fmt the formater to print on @param machine the machine **)
|
163 |
173a2a8f
|
Guillaume DAVY
|
let pp_reset_definition env typed_submachines fmt (m, m_spec_opt) =
|
164 |
ca7ff3f7
|
Lélio Brun
|
let build_assign = function
|
165 |
|
|
| var ->
|
166 |
|
|
mkinstr (MStateAssign (var, mk_default_value var.var_type))
|
167 |
61e0c3c4
|
Guillaume DAVY
|
in
|
168 |
ca7ff3f7
|
Lélio Brun
|
let env, memory =
|
169 |
|
|
match m_spec_opt with None -> env, m.mmemory | Some _ -> env, m.mmemory
|
170 |
173a2a8f
|
Guillaume DAVY
|
in
|
171 |
|
|
let assigns = List.map build_assign memory in
|
172 |
ca7ff3f7
|
Lélio Brun
|
let pp_instr_list =
|
173 |
|
|
List.map (pp_machine_instr typed_submachines env) (assigns @ m.minit)
|
174 |
|
|
in
|
175 |
|
|
pp_procedure pp_reset_procedure_name (build_pp_arg_reset m) None fmt
|
176 |
|
|
(AdaProcedureContent ([], pp_instr_list))
|
177 |
8d22ea35
|
Guillaume DAVY
|
|
178 |
ca7ff3f7
|
Lélio Brun
|
(** Print the package definition(ads) of a machine. It requires the list of
|
179 |
|
|
all typed instance. A typed submachine instance is (ident, type_machine)
|
180 |
|
|
with ident the instance name and typed_machine is (substitution, machine)
|
181 |
|
|
with machine the machine associated to the instance and substitution the
|
182 |
|
|
instanciation of all its polymorphic types. @param fmt the formater to
|
183 |
|
|
print on @param typed_submachines list of all typed machine instances of
|
184 |
|
|
this machine @param m the machine **)
|
185 |
|
|
let pp_file fmt (typed_submachines, ((opt_spec_machine, guarantees), machine))
|
186 |
|
|
=
|
187 |
b5b745fb
|
Guillaume DAVY
|
let env = List.map (fun x -> x.var_id, pp_state_name) machine.mmemory in
|
188 |
8d22ea35
|
Guillaume DAVY
|
let pp_reset fmt =
|
189 |
|
|
if is_machine_statefull machine then
|
190 |
ca7ff3f7
|
Lélio Brun
|
fprintf fmt "%a;@,@,"
|
191 |
|
|
(pp_reset_definition env typed_submachines)
|
192 |
|
|
(machine, opt_spec_machine)
|
193 |
|
|
else fprintf fmt ""
|
194 |
8d22ea35
|
Guillaume DAVY
|
in
|
195 |
|
|
let aux pkgs (id, _) =
|
196 |
|
|
try
|
197 |
ca7ff3f7
|
Lélio Brun
|
let pkg, _ = List.assoc id ada_supported_funs in
|
198 |
|
|
if List.mem pkg pkgs then pkgs else pkg :: pkgs
|
199 |
8d22ea35
|
Guillaume DAVY
|
with Not_found -> pkgs
|
200 |
|
|
in
|
201 |
230b168e
|
Guillaume DAVY
|
let packages = List.map pp_str (List.fold_left aux [] machine.mcalls) in
|
202 |
|
|
let pp_content fmt =
|
203 |
ca7ff3f7
|
Lélio Brun
|
fprintf fmt "%t%a" (*Define the reset procedure*) pp_reset
|
204 |
230b168e
|
Guillaume DAVY
|
(*Define the step procedure*)
|
205 |
ca7ff3f7
|
Lélio Brun
|
(pp_step_definition env typed_submachines)
|
206 |
|
|
(machine, opt_spec_machine, guarantees)
|
207 |
230b168e
|
Guillaume DAVY
|
in
|
208 |
|
|
fprintf fmt "%a%t%a;@."
|
209 |
8d22ea35
|
Guillaume DAVY
|
(* Include all the required packages*)
|
210 |
ca7ff3f7
|
Lélio Brun
|
(Utils.fprintf_list ~sep:";@," (pp_with AdaPrivate))
|
211 |
|
|
packages
|
212 |
8d22ea35
|
Guillaume DAVY
|
(Utils.pp_final_char_if_non_empty ";@,@," packages)
|
213 |
230b168e
|
Guillaume DAVY
|
(*Print package*)
|
214 |
ca7ff3f7
|
Lélio Brun
|
(pp_package (pp_package_name machine) [] true)
|
215 |
|
|
pp_content
|
216 |
f20d8ac7
|
Christophe Garion
|
end
|
217 |
768f60f0
|
Christophe Garion
|
|
218 |
|
|
(* Local Variables: *)
|
219 |
|
|
(* compile-command: "make -C ../../.." *)
|
220 |
|
|
(* End: *)
|