## lustrec / src / normalization.ml @ b98a4a58

History | View | Annotate | Download (16.8 KB)

1 | a2d97a3e | ploc | (********************************************************************) |
---|---|---|---|

2 | (* *) |
||

3 | (* The LustreC compiler toolset / The LustreC Development Team *) |
||

4 | (* Copyright 2012 - -- ONERA - CNRS - INPT *) |
||

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 | 22fe1c93 | ploc | |

12 | open Utils |
||

13 | open LustreSpec |
||

14 | open Corelang |
||

15 | open Format |
||

16 | |||

17 | 2e6f9ba8 | xthirioux | let expr_true loc ck = |

18 | { expr_tag = Utils.new_tag (); |
||

19 | expr_desc = Expr_const (Const_tag tag_true); |
||

20 | expr_type = Type_predef.type_bool; |
||

21 | expr_clock = ck; |
||

22 | expr_delay = Delay.new_var (); |
||

23 | expr_annot = None; |
||

24 | expr_loc = loc } |
||

25 | |||

26 | let expr_false loc ck = |
||

27 | { expr_tag = Utils.new_tag (); |
||

28 | expr_desc = Expr_const (Const_tag tag_false); |
||

29 | expr_type = Type_predef.type_bool; |
||

30 | expr_clock = ck; |
||

31 | expr_delay = Delay.new_var (); |
||

32 | expr_annot = None; |
||

33 | expr_loc = loc } |
||

34 | |||

35 | let expr_once loc ck = |
||

36 | { expr_tag = Utils.new_tag (); |
||

37 | expr_desc = Expr_arrow (expr_true loc ck, expr_false loc ck); |
||

38 | expr_type = Type_predef.type_bool; |
||

39 | expr_clock = ck; |
||

40 | expr_delay = Delay.new_var (); |
||

41 | expr_annot = None; |
||

42 | expr_loc = loc } |
||

43 | |||

44 | let is_expr_once = |
||

45 | let dummy_expr_once = expr_once Location.dummy_loc (Clocks.new_var true) in |
||

46 | fun expr -> Corelang.is_eq_expr expr dummy_expr_once |
||

47 | |||

48 | let unfold_arrow expr = |
||

49 | match expr.expr_desc with |
||

50 | | Expr_arrow (e1, e2) -> |
||

51 | let loc = expr.expr_loc in |
||

52 | a38c681e | xthirioux | let ck = List.hd (Clocks.clock_list_of_clock expr.expr_clock) in |

53 | 2e6f9ba8 | xthirioux | { expr with expr_desc = Expr_ite (expr_once loc ck, e1, e2) } |

54 | | _ -> assert false |
||

55 | |||

56 | 8deaa2dd | tkahsai | let unfold_arrow_active = ref true |

57 | 22fe1c93 | ploc | let cpt_fresh = ref 0 |

58 | |||

59 | (* Generate a new local [node] variable *) |
||

60 | let mk_fresh_var node loc ty ck = |
||

61 | 01c7d5e1 | ploc | let vars = get_node_vars node in |

62 | 22fe1c93 | ploc | let rec aux () = |

63 | incr cpt_fresh; |
||

64 | let s = Printf.sprintf "__%s_%d" node.node_id !cpt_fresh in |
||

65 | if List.exists (fun v -> v.var_id = s) vars then aux () else |
||

66 | { |
||

67 | var_id = s; |
||

68 | 54d032f5 | xthirioux | var_orig = false; |

69 | 22fe1c93 | ploc | var_dec_type = dummy_type_dec; |

70 | var_dec_clock = dummy_clock_dec; |
||

71 | var_dec_const = false; |
||

72 | ec433d69 | xthirioux | var_dec_value = None; |

73 | 22fe1c93 | ploc | var_type = ty; |

74 | var_clock = ck; |
||

75 | var_loc = loc |
||

76 | } |
||

77 | in aux () |
||

78 | |||

79 | (* Get the equation in [defs] with [expr] as rhs, if any *) |
||

80 | let get_expr_alias defs expr = |
||

81 | try Some (List.find (fun eq -> is_eq_expr eq.eq_rhs expr) defs) |
||

82 | with |
||

83 | Not_found -> None |
||

84 | |||

85 | (* Replace [expr] with (tuple of) [locals] *) |
||

86 | let replace_expr locals expr = |
||

87 | match locals with |
||

88 | | [] -> assert false |
||

89 | | [v] -> { expr with |
||

90 | expr_tag = Utils.new_tag (); |
||

91 | expr_desc = Expr_ident v.var_id } |
||

92 | | _ -> { expr with |
||

93 | expr_tag = Utils.new_tag (); |
||

94 | fc886259 | xthirioux | expr_desc = Expr_tuple (List.map expr_of_vdecl locals) } |

95 | 22fe1c93 | ploc | |

96 | let unfold_offsets e offsets = |
||

97 | let add_offset e d = |
||

98 | fc886259 | xthirioux | (*Format.eprintf "add_offset %a(%a) %a @." Printers.pp_expr e Types.print_ty e.expr_type Dimension.pp_dimension d; |

99 | let res = *) |
||

100 | 22fe1c93 | ploc | { e with |

101 | expr_tag = Utils.new_tag (); |
||

102 | expr_loc = d.Dimension.dim_loc; |
||

103 | expr_type = Types.array_element_type e.expr_type; |
||

104 | fc886259 | xthirioux | expr_desc = Expr_access (e, d) } |

105 | (*in (Format.eprintf "= %a @." Printers.pp_expr res; res) *) |
||

106 | in |
||

107 | 22fe1c93 | ploc | List.fold_left add_offset e offsets |

108 | |||

109 | (* Create an alias for [expr], if none exists yet *) |
||

110 | let mk_expr_alias node (defs, vars) expr = |
||

111 | a38c681e | xthirioux | (*Format.eprintf "mk_expr_alias %a %a %a@." Printers.pp_expr expr Types.print_ty expr.expr_type Clocks.print_ck expr.expr_clock;*) |

112 | 22fe1c93 | ploc | match get_expr_alias defs expr with |

113 | | Some eq -> |
||

114 | let aliases = List.map (fun id -> List.find (fun v -> v.var_id = id) vars) eq.eq_lhs in |
||

115 | (defs, vars), replace_expr aliases expr |
||

116 | | None -> |
||

117 | let new_aliases = |
||

118 | List.map2 |
||

119 | (mk_fresh_var node expr.expr_loc) |
||

120 | (Types.type_list_of_type expr.expr_type) |
||

121 | (Clocks.clock_list_of_clock expr.expr_clock) in |
||

122 | let new_def = |
||

123 | mkeq expr.expr_loc (List.map (fun v -> v.var_id) new_aliases, expr) |
||

124 | 3ca6d126 | ploc | in |

125 | fc886259 | xthirioux | (* Format.eprintf "Checking def of alias: %a -> %a@." (fprintf_list ~sep:", " (fun fmt v -> Format.pp_print_string fmt v.var_id)) new_aliases Printers.pp_expr expr; *) |

126 | 3ca6d126 | ploc | (new_def::defs, new_aliases@vars), replace_expr new_aliases expr |

127 | 22fe1c93 | ploc | |

128 | d4807c3d | xthirioux | (* Create an alias for [expr], if [expr] is not already an alias (i.e. an ident) |

129 | and [opt] is true *) |
||

130 | 22fe1c93 | ploc | let mk_expr_alias_opt opt node defvars expr = |

131 | d4807c3d | xthirioux | match expr.expr_desc with |

132 | | Expr_ident alias -> |
||

133 | defvars, expr |
||

134 | 8deaa2dd | tkahsai | | _ -> |

135 | d4807c3d | xthirioux | if opt |

136 | then |
||

137 | mk_expr_alias node defvars expr |
||

138 | else |
||

139 | defvars, expr |
||

140 | 22fe1c93 | ploc | |

141 | 8deaa2dd | tkahsai | (* Create a (normalized) expression from [ref_e], |

142 | 22fe1c93 | ploc | replacing description with [norm_d], |

143 | 8deaa2dd | tkahsai | taking propagated [offsets] into account |

144 | 22fe1c93 | ploc | in order to change expression type *) |

145 | let mk_norm_expr offsets ref_e norm_d = |
||

146 | fc886259 | xthirioux | (*Format.eprintf "mk_norm_expr %a %a @." Printers.pp_expr ref_e Printers.pp_expr { ref_e with expr_desc = norm_d};*) |

147 | 22fe1c93 | ploc | let drop_array_type ty = |

148 | Types.map_tuple_type Types.array_element_type ty in |
||

149 | { ref_e with |
||

150 | expr_desc = norm_d; |
||

151 | expr_type = Utils.repeat (List.length offsets) drop_array_type ref_e.expr_type } |
||

152 | |||

153 | (* normalize_<foo> : defs * used vars -> <foo> -> (updated defs * updated vars) * normalized <foo> *) |
||

154 | let rec normalize_list alias node offsets norm_element defvars elist = |
||

155 | List.fold_right |
||

156 | (fun t (defvars, qlist) -> |
||

157 | let defvars, norm_t = norm_element alias node offsets defvars t in |
||

158 | (defvars, norm_t :: qlist) |
||

159 | ) elist (defvars, []) |
||

160 | |||

161 | let rec normalize_expr ?(alias=true) node offsets defvars expr = |
||

162 | (* Format.eprintf "normalize %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*) |
||

163 | match expr.expr_desc with |
||

164 | 8deaa2dd | tkahsai | | Expr_const _ |

165 | 22fe1c93 | ploc | | Expr_ident _ -> defvars, unfold_offsets expr offsets |

166 | | Expr_array elist -> |
||

167 | let defvars, norm_elist = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars elist in |
||

168 | let norm_expr = mk_norm_expr offsets expr (Expr_array norm_elist) in |
||

169 | mk_expr_alias_opt alias node defvars norm_expr |
||

170 | | Expr_power (e1, d) when offsets = [] -> |
||

171 | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |
||

172 | let norm_expr = mk_norm_expr offsets expr (Expr_power (norm_e1, d)) in |
||

173 | mk_expr_alias_opt alias node defvars norm_expr |
||

174 | | Expr_power (e1, d) -> |
||

175 | normalize_expr ~alias:alias node (List.tl offsets) defvars e1 |
||

176 | | Expr_access (e1, d) -> |
||

177 | normalize_expr ~alias:alias node (d::offsets) defvars e1 |
||

178 | 8deaa2dd | tkahsai | | Expr_tuple elist -> |

179 | 22fe1c93 | ploc | let defvars, norm_elist = |

180 | normalize_list alias node offsets (fun alias -> normalize_expr ~alias:alias) defvars elist in |
||

181 | defvars, mk_norm_expr offsets expr (Expr_tuple norm_elist) |
||

182 | 8deaa2dd | tkahsai | | Expr_appl (id, args, None) |

183 | when Basic_library.is_internal_fun id |
||

184 | af5af1e8 | ploc | && Types.is_array_type expr.expr_type -> |

185 | 8deaa2dd | tkahsai | let defvars, norm_args = |

186 | normalize_list |
||

187 | af5af1e8 | ploc | alias |

188 | node |
||

189 | 8deaa2dd | tkahsai | offsets |

190 | (fun _ -> normalize_array_expr ~alias:true) |
||

191 | defvars |
||

192 | (expr_list_of_expr args) |
||

193 | af5af1e8 | ploc | in |

194 | 22fe1c93 | ploc | defvars, mk_norm_expr offsets expr (Expr_appl (id, expr_of_expr_list args.expr_loc norm_args, None)) |

195 | | Expr_appl (id, args, None) when Basic_library.is_internal_fun id -> |
||

196 | let defvars, norm_args = normalize_expr ~alias:true node offsets defvars args in |
||

197 | defvars, mk_norm_expr offsets expr (Expr_appl (id, norm_args, None)) |
||

198 | | Expr_appl (id, args, r) -> |
||

199 | let defvars, norm_args = normalize_expr node [] defvars args in |
||

200 | let norm_expr = mk_norm_expr [] expr (Expr_appl (id, norm_args, r)) in |
||

201 | if offsets <> [] |
||

202 | then |
||

203 | let defvars, norm_expr = normalize_expr node [] defvars norm_expr in |
||

204 | normalize_expr ~alias:alias node offsets defvars norm_expr |
||

205 | else |
||

206 | mk_expr_alias_opt (alias && not (Basic_library.is_internal_fun id)) node defvars norm_expr |
||

207 | b84a138e | ploc | | Expr_arrow (e1,e2) when !unfold_arrow_active && not (is_expr_once expr) -> (* Here we differ from Colaco paper: arrows are pushed to the top *) |

208 | 2e6f9ba8 | xthirioux | normalize_expr ~alias:alias node offsets defvars (unfold_arrow expr) |

209 | | Expr_arrow (e1,e2) -> |
||

210 | 22fe1c93 | ploc | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |

211 | let defvars, norm_e2 = normalize_expr node offsets defvars e2 in |
||

212 | let norm_expr = mk_norm_expr offsets expr (Expr_arrow (norm_e1, norm_e2)) in |
||

213 | mk_expr_alias_opt alias node defvars norm_expr |
||

214 | | Expr_pre e -> |
||

215 | let defvars, norm_e = normalize_expr node offsets defvars e in |
||

216 | let norm_expr = mk_norm_expr offsets expr (Expr_pre norm_e) in |
||

217 | mk_expr_alias_opt alias node defvars norm_expr |
||

218 | | Expr_fby (e1, e2) -> |
||

219 | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |
||

220 | let defvars, norm_e2 = normalize_expr node offsets defvars e2 in |
||

221 | let norm_expr = mk_norm_expr offsets expr (Expr_fby (norm_e1, norm_e2)) in |
||

222 | mk_expr_alias_opt alias node defvars norm_expr |
||

223 | | Expr_when (e, c, l) -> |
||

224 | let defvars, norm_e = normalize_expr node offsets defvars e in |
||

225 | defvars, mk_norm_expr offsets expr (Expr_when (norm_e, c, l)) |
||

226 | | Expr_ite (c, t, e) -> |
||

227 | let defvars, norm_c = normalize_guard node defvars c in |
||

228 | let defvars, norm_t = normalize_cond_expr node offsets defvars t in |
||

229 | let defvars, norm_e = normalize_cond_expr node offsets defvars e in |
||

230 | let norm_expr = mk_norm_expr offsets expr (Expr_ite (norm_c, norm_t, norm_e)) in |
||

231 | mk_expr_alias_opt alias node defvars norm_expr |
||

232 | | Expr_merge (c, hl) -> |
||

233 | let defvars, norm_hl = normalize_branches node offsets defvars hl in |
||

234 | let norm_expr = mk_norm_expr offsets expr (Expr_merge (c, norm_hl)) in |
||

235 | mk_expr_alias_opt alias node defvars norm_expr |
||

236 | 8deaa2dd | tkahsai | |

237 | 52cfee34 | xthirioux | (* Creates a conditional with a merge construct, which is more lazy *) |

238 | (* |
||

239 | let norm_conditional_as_merge alias node norm_expr offsets defvars expr = |
||

240 | match expr.expr_desc with |
||

241 | | Expr_ite (c, t, e) -> |
||

242 | let defvars, norm_t = norm_expr (alias node offsets defvars t in |
||

243 | | _ -> assert false |
||

244 | *) |
||

245 | 22fe1c93 | ploc | and normalize_branches node offsets defvars hl = |

246 | List.fold_right |
||

247 | (fun (t, h) (defvars, norm_q) -> |
||

248 | let (defvars, norm_h) = normalize_cond_expr node offsets defvars h in |
||

249 | defvars, (t, norm_h) :: norm_q |
||

250 | ) |
||

251 | hl (defvars, []) |
||

252 | |||

253 | and normalize_array_expr ?(alias=true) node offsets defvars expr = |
||

254 | (* Format.eprintf "normalize_array %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*) |
||

255 | match expr.expr_desc with |
||

256 | | Expr_power (e1, d) when offsets = [] -> |
||

257 | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |
||

258 | defvars, mk_norm_expr offsets expr (Expr_power (norm_e1, d)) |
||

259 | | Expr_power (e1, d) -> |
||

260 | normalize_array_expr ~alias:alias node (List.tl offsets) defvars e1 |
||

261 | | Expr_access (e1, d) -> normalize_array_expr ~alias:alias node (d::offsets) defvars e1 |
||

262 | | Expr_array elist when offsets = [] -> |
||

263 | let defvars, norm_elist = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars elist in |
||

264 | defvars, mk_norm_expr offsets expr (Expr_array norm_elist) |
||

265 | | Expr_appl (id, args, None) when Basic_library.is_internal_fun id -> |
||

266 | let defvars, norm_args = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars (expr_list_of_expr args) in |
||

267 | defvars, mk_norm_expr offsets expr (Expr_appl (id, expr_of_expr_list args.expr_loc norm_args, None)) |
||

268 | | _ -> normalize_expr ~alias:alias node offsets defvars expr |
||

269 | |||

270 | and normalize_cond_expr ?(alias=true) node offsets defvars expr = |
||

271 | (*Format.eprintf "normalize_cond %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*) |
||

272 | match expr.expr_desc with |
||

273 | | Expr_access (e1, d) -> |
||

274 | normalize_cond_expr ~alias:alias node (d::offsets) defvars e1 |
||

275 | | Expr_ite (c, t, e) -> |
||

276 | let defvars, norm_c = normalize_guard node defvars c in |
||

277 | let defvars, norm_t = normalize_cond_expr node offsets defvars t in |
||

278 | let defvars, norm_e = normalize_cond_expr node offsets defvars e in |
||

279 | defvars, mk_norm_expr offsets expr (Expr_ite (norm_c, norm_t, norm_e)) |
||

280 | | Expr_merge (c, hl) -> |
||

281 | let defvars, norm_hl = normalize_branches node offsets defvars hl in |
||

282 | defvars, mk_norm_expr offsets expr (Expr_merge (c, norm_hl)) |
||

283 | | _ -> normalize_expr ~alias:alias node offsets defvars expr |
||

284 | |||

285 | and normalize_guard node defvars expr = |
||

286 | d4807c3d | xthirioux | let defvars, norm_expr = normalize_expr node [] defvars expr in |

287 | mk_expr_alias_opt true node defvars norm_expr |
||

288 | 22fe1c93 | ploc | |

289 | (* outputs cannot be memories as well. If so, introduce new local variable. |
||

290 | *) |
||

291 | let decouple_outputs node defvars eq = |
||

292 | let rec fold_lhs defvars lhs tys cks = |
||

293 | match lhs, tys, cks with |
||

294 | | [], [], [] -> defvars, [] |
||

295 | | v::qv, t::qt, c::qc -> let (defs_q, vars_q), lhs_q = fold_lhs defvars qv qt qc in |
||

296 | if List.exists (fun o -> o.var_id = v) node.node_outputs |
||

297 | then |
||

298 | let newvar = mk_fresh_var node eq.eq_loc t c in |
||

299 | fc886259 | xthirioux | let neweq = mkeq eq.eq_loc ([v], expr_of_vdecl newvar) in |

300 | 22fe1c93 | ploc | (neweq :: defs_q, newvar :: vars_q), newvar.var_id :: lhs_q |

301 | else |
||

302 | (defs_q, vars_q), v::lhs_q |
||

303 | | _ -> assert false in |
||

304 | let defvars', lhs' = |
||

305 | fold_lhs |
||

306 | defvars |
||

307 | eq.eq_lhs |
||

308 | (Types.type_list_of_type eq.eq_rhs.expr_type) |
||

309 | (Clocks.clock_list_of_clock eq.eq_rhs.expr_clock) in |
||

310 | defvars', {eq with eq_lhs = lhs' } |
||

311 | |||

312 | 8deaa2dd | tkahsai | let rec normalize_eq node defvars eq = |

313 | 22fe1c93 | ploc | match eq.eq_rhs.expr_desc with |

314 | | Expr_pre _ |
||

315 | | Expr_fby _ -> |
||

316 | let (defvars', eq') = decouple_outputs node defvars eq in |
||

317 | let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars' eq'.eq_rhs in |
||

318 | let norm_eq = { eq' with eq_rhs = norm_rhs } in |
||

319 | (norm_eq::defs', vars') |
||

320 | | Expr_array _ -> |
||

321 | let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in |
||

322 | let norm_eq = { eq with eq_rhs = norm_rhs } in |
||

323 | (norm_eq::defs', vars') |
||

324 | | Expr_appl (id, _, None) when Basic_library.is_internal_fun id && Types.is_array_type eq.eq_rhs.expr_type -> |
||

325 | let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in |
||

326 | let norm_eq = { eq with eq_rhs = norm_rhs } in |
||

327 | (norm_eq::defs', vars') |
||

328 | | Expr_appl _ -> |
||

329 | let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars eq.eq_rhs in |
||

330 | let norm_eq = { eq with eq_rhs = norm_rhs } in |
||

331 | (norm_eq::defs', vars') |
||

332 | | _ -> |
||

333 | let (defs', vars'), norm_rhs = normalize_cond_expr ~alias:false node [] defvars eq.eq_rhs in |
||

334 | let norm_eq = { eq with eq_rhs = norm_rhs } in |
||

335 | norm_eq::defs', vars' |
||

336 | |||

337 | 8deaa2dd | tkahsai | (** normalize_node node returns a normalized node, |

338 | ie. |
||

339 | af5af1e8 | ploc | - updated locals |

340 | - new equations |
||

341 | 8deaa2dd | tkahsai | - |

342 | af5af1e8 | ploc | *) |

343 | 8deaa2dd | tkahsai | let normalize_node node = |

344 | 22fe1c93 | ploc | cpt_fresh := 0; |

345 | let inputs_outputs = node.node_inputs@node.node_outputs in |
||

346 | let is_local v = |
||

347 | List.for_all ((!=) v) inputs_outputs in |
||

348 | af5af1e8 | ploc | let orig_vars = inputs_outputs@node.node_locals in |

349 | 8deaa2dd | tkahsai | let defs, vars = |

350 | b08ffca7 | xthirioux | List.fold_left (normalize_eq node) ([], orig_vars) (get_node_eqs node) in |

351 | af5af1e8 | ploc | (* Normalize the asserts *) |

352 | 8deaa2dd | tkahsai | let vars, assert_defs, asserts = |

353 | af5af1e8 | ploc | List.fold_left ( |

354 | fun (vars, def_accu, assert_accu) assert_ -> |
||

355 | let assert_expr = assert_.assert_expr in |
||

356 | 8deaa2dd | tkahsai | let (defs, vars'), expr = |

357 | normalize_expr |
||

358 | ~alias:true |
||

359 | node |
||

360 | af5af1e8 | ploc | [] (* empty offset for arrays *) |

361 | ([], vars) (* defvar only contains vars *) |
||

362 | assert_expr |
||

363 | in |
||

364 | 8deaa2dd | tkahsai | (*Format.eprintf "New assert vars: %a@.@?" (fprintf_list ~sep:", " Printers.pp_var) vars';*) |

365 | af5af1e8 | ploc | vars', defs@def_accu, {assert_ with assert_expr = expr}::assert_accu |

366 | ) (vars, [], []) node.node_asserts in |
||

367 | 22fe1c93 | ploc | let new_locals = List.filter is_local vars in |

368 | 8deaa2dd | tkahsai | (*Format.eprintf "New locals: %a@.@?" (fprintf_list ~sep:", " Printers.pp_var) new_locals;*) |

369 | f2b1c245 | ploc | |

370 | ea1c2906 | ploc | let new_annots = |

371 | if !Options.traces then |
||

372 | begin |
||

373 | 8deaa2dd | tkahsai | (* Compute traceability info: |

374 | ea1c2906 | ploc | - gather newly bound variables |

375 | 8deaa2dd | tkahsai | - compute the associated expression without aliases |

376 | ea1c2906 | ploc | *) |

377 | let diff_vars = List.filter (fun v -> not (List.mem v node.node_locals) ) new_locals in |
||

378 | let norm_traceability = { |
||

379 | annots = List.map (fun v -> |
||

380 | let eq = |
||

381 | try |
||

382 | b98a4a58 | ploc | List.find (fun eq -> eq.eq_lhs = [v.var_id]) (defs@assert_defs) |

383 | ea1c2906 | ploc | with Not_found -> (Format.eprintf "var not found %s@." v.var_id; assert false) in |

384 | b98a4a58 | ploc | let expr = substitute_expr diff_vars (defs@assert_defs) eq.eq_rhs in |

385 | ea1c2906 | ploc | let pair = mkeexpr expr.expr_loc (mkexpr expr.expr_loc (Expr_tuple [expr_of_ident v.var_id expr.expr_loc; expr])) in |

386 | (["traceability"], pair) |
||

387 | ) diff_vars; |
||

388 | annot_loc = Location.dummy_loc |
||

389 | 8deaa2dd | tkahsai | } |

390 | ea1c2906 | ploc | in |

391 | norm_traceability::node.node_annot |
||

392 | end |
||

393 | else |
||

394 | node.node_annot |
||

395 | af5af1e8 | ploc | in |

396 | ea1c2906 | ploc | |

397 | 22fe1c93 | ploc | let node = |

398 | 8deaa2dd | tkahsai | { node with |

399 | node_locals = new_locals; |
||

400 | b08ffca7 | xthirioux | node_stmts = List.map (fun eq -> Eq eq) (defs @ assert_defs); |

401 | af5af1e8 | ploc | node_asserts = asserts; |

402 | ea1c2906 | ploc | node_annot = new_annots; |

403 | af5af1e8 | ploc | } |

404 | 8deaa2dd | tkahsai | in ((*Printers.pp_node Format.err_formatter node;*) |

405 | f2b1c245 | ploc | node |

406 | ) |
||

407 | |||

408 | 22fe1c93 | ploc | |

409 | let normalize_decl decl = |
||

410 | match decl.top_decl_desc with |
||

411 | | Node nd -> |
||

412 | f2b1c245 | ploc | let decl' = {decl with top_decl_desc = Node (normalize_node nd)} in |

413 | Hashtbl.replace Corelang.node_table nd.node_id decl'; |
||

414 | decl' |
||

415 | ef34b4ae | xthirioux | | Open _ | ImportedNode _ | Const _ | TypeDef _ -> decl |

416 | 8deaa2dd | tkahsai | |

417 | let normalize_prog decls = |
||

418 | 22fe1c93 | ploc | List.map normalize_decl decls |

419 | |||

420 | (* Local Variables: *) |
||

421 | (* compile-command:"make -C .." *) |
||

422 | (* End: *) |