Revision 7eafa0e1
Added by Pierre-Loïc Garoche over 7 years ago
src/backends/EMF/EMF_backend.ml | ||
---|---|---|
117 | 117 |
-> false *) |
118 | 118 |
let is_arrow_fun m i = |
119 | 119 |
match Corelang.get_instr_desc i with |
120 |
| MStep ([var], i, vl) -> ( |
|
121 |
try |
|
122 |
let name = (Machine_code.get_node_def i m).node_id in |
|
123 |
match name, vl with |
|
124 |
| "_arrow", [v1; v2] -> ( |
|
125 |
match v1.value_desc, v2.value_desc with |
|
126 |
| Cst c1, Cst c2 -> |
|
127 |
if c1 = Corelang.const_of_bool true && c2 = Corelang.const_of_bool false then |
|
128 |
true |
|
129 |
else |
|
130 |
assert false (* only handle true -> false *) |
|
131 |
| _ -> assert false |
|
132 |
) |
|
133 |
|
|
134 |
| _ -> false |
|
135 |
with |
|
136 |
| Not_found -> false (* Not declared (should have been detected now, or imported node *) |
|
137 |
) |
|
120 |
| MStep ([var], i, vl) -> |
|
121 |
( |
|
122 |
try |
|
123 |
let name = (Machine_code.get_node_def i m).node_id in |
|
124 |
match name, vl with |
|
125 |
| "_arrow", [v1; v2] -> ( |
|
126 |
match v1.value_desc, v2.value_desc with |
|
127 |
| Cst c1, Cst c2 -> |
|
128 |
if c1 = Corelang.const_of_bool true && c2 = Corelang.const_of_bool false then |
|
129 |
true |
|
130 |
else |
|
131 |
assert false (* only handle true -> false *) |
|
132 |
| _ -> assert false |
|
133 |
) |
|
134 |
|
|
135 |
| _ -> false |
|
136 |
with |
|
137 |
| Not_found -> false (* Not declared (should have been detected now, or |
|
138 |
imported node) *) |
|
139 |
) |
|
138 | 140 |
| _ -> false |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
let is_resetable_fun lustre_eq = |
|
145 |
(* We extract the clock if it exist from the original lustre equation *) |
|
146 |
match lustre_eq with |
|
147 |
| Some eq -> ( |
|
148 |
match eq.eq_rhs.expr_desc with |
|
149 |
| Expr_appl(_,_,reset) -> ( |
|
150 |
match reset with None -> false | Some _ -> true |
|
151 |
) |
|
152 |
| _ -> assert false |
|
153 |
) |
|
154 |
| None -> assert false (* should have been assigned to an original lustre equation *) |
|
139 | 155 |
|
140 | 156 |
(**********************************************) |
141 | 157 |
(* Printing machine code as EMF *) |
... | ... | |
154 | 170 |
(fun fmt -> fprintf fmt "%a_%s" (fprintf_list ~sep:"_" pp_var_name) outs id) |
155 | 171 |
| _ -> () (* No name *) |
156 | 172 |
|
157 |
let rec branch_block_vars il = |
|
173 |
let rec branch_block_vars m il =
|
|
158 | 174 |
List.fold_left |
159 | 175 |
(fun (accu_all_def, accu_def, accu_read) i -> |
160 |
let all_defined_vars, common_def_vars, read_vars = branch_instr_vars i in |
|
176 |
let all_defined_vars, common_def_vars, read_vars = branch_instr_vars m i in
|
|
161 | 177 |
ISet.union accu_all_def all_defined_vars, |
162 | 178 |
ISet.union accu_def common_def_vars, |
163 | 179 |
VSet.union accu_read read_vars) |
164 | 180 |
(ISet.empty, ISet.empty, VSet.empty) il |
165 |
and branch_instr_vars i = |
|
181 |
and branch_instr_vars m i =
|
|
166 | 182 |
match Corelang.get_instr_desc i with |
167 | 183 |
| MLocalAssign (var,expr) |
168 | 184 |
| MStateAssign (var,expr) -> ISet.singleton var.var_id, ISet.singleton var.var_id, get_expr_vars expr |
169 |
| MStep (vars, _, args) -> |
|
185 |
| MStep (vars, f, args) -> |
|
186 |
let is_stateful = List.mem_assoc f m.minstances in |
|
170 | 187 |
let lhs = ISet.of_list (List.map (fun v -> v.var_id) vars) in |
188 |
let args_vars = |
|
189 |
List.fold_left (fun accu v -> VSet.union accu (get_expr_vars v)) VSet.empty args in |
|
190 |
|
|
171 | 191 |
lhs, lhs, |
172 |
List.fold_left (fun accu v -> VSet.union accu (get_expr_vars v)) VSet.empty args |
|
192 |
( |
|
193 |
if is_stateful && is_resetable_fun i.lustre_eq then |
|
194 |
let reset_var = |
|
195 |
let loc = Location.dummy_loc in |
|
196 |
Corelang.mkvar_decl loc |
|
197 |
(reset_name f, |
|
198 |
Corelang.mktyp loc Tydec_bool, Corelang.mkclock loc Ckdec_any, |
|
199 |
false, |
|
200 |
None) |
|
201 |
in |
|
202 |
VSet.add reset_var args_vars |
|
203 |
else |
|
204 |
args_vars |
|
205 |
) |
|
173 | 206 |
| MBranch (g,(_,hd_il)::tl) -> (* We focus on variables defined in all branches *) |
174 | 207 |
let read_guard = get_expr_vars g in |
175 |
let all_def_vars_hd, def_vars_hd, read_vars_hd = branch_block_vars hd_il in |
|
208 |
let all_def_vars_hd, def_vars_hd, read_vars_hd = branch_block_vars m hd_il in
|
|
176 | 209 |
let all_def_vars, def_vars, read_vars = |
177 | 210 |
List.fold_left |
178 | 211 |
(fun (all_def_vars, def_vars, read_vars) (_, il) -> |
179 |
(* We accumulate reads but intersect writes *) |
|
180 |
let all_writes_il, writes_il, reads_il = branch_block_vars il in |
|
212 |
(* We accumulate reads but intersect writes *)
|
|
213 |
let all_writes_il, writes_il, reads_il = branch_block_vars m il in
|
|
181 | 214 |
ISet.union all_def_vars all_writes_il, |
182 | 215 |
ISet.inter def_vars writes_il, |
183 | 216 |
VSet.union read_vars reads_il |
... | ... | |
256 | 289 |
) |
257 | 290 |
|
258 | 291 |
| MBranch (g, hl) -> ( |
259 |
let all_outputs, outputs, inputs = branch_instr_vars i in |
|
292 |
let all_outputs, outputs, inputs = branch_instr_vars m i in
|
|
260 | 293 |
Format.eprintf "Mbranch %a@.vars: all_out: %a, out:%a, in:%a@.@." |
261 | 294 |
Machine_code.pp_instr i |
262 | 295 |
(fprintf_list ~sep:", " pp_var_string) (ISet.elements all_outputs) |
... | ... | |
283 | 316 |
fprintf fmt "@[<v 2>\"branches\": {@ @[<v 0>%a@]@]@ }" |
284 | 317 |
(fprintf_list ~sep:",@ " |
285 | 318 |
(fun fmt (tag, instrs_tag) -> |
286 |
let branch_all_lhs, _, branch_inputs = branch_block_vars instrs_tag in |
|
319 |
let branch_all_lhs, _, branch_inputs = branch_block_vars m instrs_tag in
|
|
287 | 320 |
let branch_inputs = VSet.filter (fun v -> not (ISet.mem v.var_id branch_all_lhs)) branch_inputs in |
288 | 321 |
fprintf fmt "@[<v 2>\"%s\": {@ " tag; |
289 | 322 |
fprintf fmt "\"guard_value\": \"%a\",@ " pp_tag_id tag; |
... | ... | |
317 | 350 |
if is_stateful then |
318 | 351 |
fprintf fmt ",@ \"reset\": { \"name\": \"%s\", \"resetable\": \"%b\"}" |
319 | 352 |
(reset_name f) |
320 |
( (* We extract the clock if it exist from the original lustre equation *) |
|
321 |
match i.lustre_eq with |
|
322 |
| Some eq -> ( |
|
323 |
match eq.eq_rhs.expr_desc with |
|
324 |
| Expr_appl(_,_,reset) -> ( |
|
325 |
match reset with None -> false | Some _ -> true |
|
326 |
) |
|
327 |
| _ -> assert false |
|
328 |
) |
|
329 |
| None -> assert false (* should have been assigned to an original lustre equation *) |
|
330 |
) |
|
353 |
(is_resetable_fun i.lustre_eq) |
|
331 | 354 |
else fprintf fmt "@ " |
332 | 355 |
) |
333 | 356 |
|
Also available in: Unified diff
[EMF] Added the reset signal (the every argument) as input to the JSON struct