## lustrec / src / normalization.ml @ 4f26dcf5

History | View | Annotate | Download (21.4 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 | 8446bf03 | ploc | open Lustre_types |

14 | 22fe1c93 | ploc | open Corelang |

15 | open Format |
||

16 | |||

17 | 27dc3869 | ploc | (** Normalisation iters through the AST of expressions and bind fresh definition |

18 | when some criteria are met. This creation of fresh definition is performed by |
||

19 | the function mk_expr_alias_opt when the alias argument is on. |
||

20 | |||

21 | Initial expressions, ie expressions attached a variable in an equation |
||

22 | definition are not aliased. This non-alias feature is propagated in the |
||

23 | expression AST for array access and power construct, tuple, and some special |
||

24 | cases of arrows. |
||

25 | |||

26 | Two global variables may impact the normalization process: |
||

27 | - unfold_arrow_active |
||

28 | - force_alias_ite: when set, bind a fresh alias for then and else |
||

29 | definitions. |
||

30 | *) |
||

31 | |||

32 | (* Two global variables *) |
||

33 | let unfold_arrow_active = ref true |
||

34 | let force_alias_ite = ref false |
||

35 | 3436d1a6 | ploc | let force_alias_internal_fun = ref false |

36 | 27dc3869 | ploc | |

37 | |||

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

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

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

41 | expr_type = Type_predef.type_bool; |
||

42 | expr_clock = ck; |
||

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

44 | expr_annot = None; |
||

45 | expr_loc = loc } |
||

46 | |||

47 | let expr_false loc ck = |
||

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

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

50 | expr_type = Type_predef.type_bool; |
||

51 | expr_clock = ck; |
||

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

53 | expr_annot = None; |
||

54 | expr_loc = loc } |
||

55 | |||

56 | let expr_once loc ck = |
||

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

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

59 | expr_type = Type_predef.type_bool; |
||

60 | expr_clock = ck; |
||

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

62 | expr_annot = None; |
||

63 | expr_loc = loc } |
||

64 | |||

65 | let is_expr_once = |
||

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

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

68 | |||

69 | let unfold_arrow expr = |
||

70 | match expr.expr_desc with |
||

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

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

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

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

75 | | _ -> assert false |
||

76 | |||

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

78 | |||

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

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

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

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

83 | incr cpt_fresh; |
||

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

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

86 | { |
||

87 | var_id = s; |
||

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

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

90 | var_dec_clock = dummy_clock_dec; |
||

91 | var_dec_const = false; |
||

92 | ec433d69 | xthirioux | var_dec_value = None; |

93 | 66359a5e | ploc | var_parent_nodeid = Some node.node_id; |

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

95 | var_clock = ck; |
||

96 | var_loc = loc |
||

97 | } |
||

98 | in aux () |
||

99 | |||

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

101 | let get_expr_alias defs expr = |
||

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

103 | with |
||

104 | e49b6d55 | xavier.thirioux | | Not_found -> None |

105 | |||

106 | 22fe1c93 | ploc | (* Replace [expr] with (tuple of) [locals] *) |

107 | let replace_expr locals expr = |
||

108 | match locals with |
||

109 | | [] -> assert false |
||

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

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

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

113 | | _ -> { expr with |
||

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

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

116 | 22fe1c93 | ploc | |

117 | let unfold_offsets e offsets = |
||

118 | let add_offset e d = |
||

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

120 | let res = *) |
||

121 | 22fe1c93 | ploc | { e with |

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

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

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

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

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

127 | in |
||

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

129 | |||

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

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

132 | 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;*) |

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

134 | | Some eq -> |
||

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

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

137 | | None -> |
||

138 | let new_aliases = |
||

139 | List.map2 |
||

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

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

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

143 | let new_def = |
||

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

145 | 3ca6d126 | ploc | in |

146 | 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; *) |

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

148 | 22fe1c93 | ploc | |

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

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

151 | 04a63d25 | xthirioux | let mk_expr_alias_opt opt node (defs, vars) expr = |

152 | (*Format.eprintf "mk_expr_alias_opt %B %a %a %a@." opt Printers.pp_expr expr Types.print_ty expr.expr_type Clocks.print_ck expr.expr_clock;*) |
||

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

154 | | Expr_ident alias -> |
||

155 | 04a63d25 | xthirioux | (defs, vars), expr |

156 | 8deaa2dd | tkahsai | | _ -> |

157 | 04a63d25 | xthirioux | match get_expr_alias defs expr with |

158 | | Some eq -> |
||

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

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

161 | | None -> |
||

162 | if opt |
||

163 | then |
||

164 | let new_aliases = |
||

165 | List.map2 |
||

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

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

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

169 | let new_def = |
||

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

171 | 66359a5e | ploc | in |

172 | (* Typing and Registering machine type *) |
||

173 | f4050bef | ploc | let _ = if Machine_types.is_active then Machine_types.type_def node new_aliases expr in |

174 | 66359a5e | ploc | (new_def::defs, new_aliases@vars), replace_expr new_aliases expr |

175 | 04a63d25 | xthirioux | else |

176 | (defs, vars), expr |
||

177 | 22fe1c93 | ploc | |

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

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

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

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

182 | let mk_norm_expr offsets ref_e norm_d = |
||

183 | e49b6d55 | xavier.thirioux | (*Format.eprintf "mk_norm_expr %a %a @." Printers.pp_expr ref_e Printers.pp_expr { ref_e with expr_desc = norm_d};*) |

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

185 | Types.map_tuple_type Types.array_element_type ty in |
||

186 | { ref_e with |
||

187 | expr_desc = norm_d; |
||

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

189 | e49b6d55 | xavier.thirioux | |

190 | 22fe1c93 | ploc | (* normalize_<foo> : defs * used vars -> <foo> -> (updated defs * updated vars) * normalized <foo> *) |

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

192 | List.fold_right |
||

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

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

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

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

197 | |||

198 | 0a10042e | ploc | let rec normalize_expr ?(alias=true) ?(alias_basic=false) node offsets defvars expr = |

199 | e49b6d55 | xavier.thirioux | (*Format.eprintf "normalize %B %a:%a [%a]@." alias Printers.pp_expr expr Types.print_ty expr.expr_type (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*) |

200 | 22fe1c93 | ploc | match expr.expr_desc with |

201 | 8deaa2dd | tkahsai | | Expr_const _ |

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

203 | | Expr_array elist -> |
||

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

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

206 | mk_expr_alias_opt alias node defvars norm_expr |
||

207 | 22fe1c93 | ploc | | Expr_power (e1, d) when offsets = [] -> |

208 | 0a10042e | ploc | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |

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

210 | mk_expr_alias_opt alias node defvars norm_expr |
||

211 | 22fe1c93 | ploc | | Expr_power (e1, d) -> |

212 | 0a10042e | ploc | normalize_expr ~alias:alias node (List.tl offsets) defvars e1 |

213 | 22fe1c93 | ploc | | Expr_access (e1, d) -> |

214 | 0a10042e | ploc | normalize_expr ~alias:alias node (d::offsets) defvars e1 |

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

216 | 0a10042e | ploc | let defvars, norm_elist = |

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

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

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

220 | 04a63d25 | xthirioux | when Basic_library.is_homomorphic_fun id |

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

222 | 0a10042e | ploc | let defvars, norm_args = |

223 | normalize_list |
||

224 | alias |
||

225 | node |
||

226 | offsets |
||

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

228 | defvars |
||

229 | (expr_list_of_expr args) |
||

230 | in |
||

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

232 | 3436d1a6 | ploc | | Expr_appl (id, args, None) when Basic_library.is_expr_internal_fun expr |

233 | 0a10042e | ploc | && not (!force_alias_internal_fun || alias_basic) -> |

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

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

236 | 22fe1c93 | ploc | | Expr_appl (id, args, r) -> |

237 | 0a10042e | ploc | let defvars, r = |

238 | match r with |
||

239 | | None -> defvars, None |
||

240 | | Some r -> |
||

241 | let defvars, norm_r = normalize_expr ~alias_basic:true node [] defvars r in |
||

242 | defvars, Some norm_r |
||

243 | in |
||

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

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

246 | if offsets <> [] |
||

247 | then |
||

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

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

250 | else |
||

251 | mk_expr_alias_opt (alias && (!force_alias_internal_fun || alias_basic |
||

252 | || not (Basic_library.is_expr_internal_fun expr))) |
||

253 | node defvars norm_expr |
||

254 | 5fb5b031 | Ploc | | Expr_arrow (e1,e2) when !unfold_arrow_active && not (is_expr_once expr) -> |

255 | 0a10042e | ploc | (* Here we differ from Colaco paper: arrows are pushed to the top *) |

256 | normalize_expr ~alias:alias node offsets defvars (unfold_arrow expr) |
||

257 | 2e6f9ba8 | xthirioux | | Expr_arrow (e1,e2) -> |

258 | 0a10042e | ploc | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |

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

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

261 | mk_expr_alias_opt alias node defvars norm_expr |
||

262 | 22fe1c93 | ploc | | Expr_pre e -> |

263 | 0a10042e | ploc | let defvars, norm_e = normalize_expr node offsets defvars e in |

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

265 | mk_expr_alias_opt alias node defvars norm_expr |
||

266 | 22fe1c93 | ploc | | Expr_fby (e1, e2) -> |

267 | 0a10042e | ploc | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |

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

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

270 | mk_expr_alias_opt alias node defvars norm_expr |
||

271 | 22fe1c93 | ploc | | Expr_when (e, c, l) -> |

272 | 0a10042e | ploc | let defvars, norm_e = normalize_expr node offsets defvars e in |

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

274 | 22fe1c93 | ploc | | Expr_ite (c, t, e) -> |

275 | 0a10042e | ploc | let defvars, norm_c = normalize_guard node defvars c in |

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

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

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

279 | mk_expr_alias_opt alias node defvars norm_expr |
||

280 | 22fe1c93 | ploc | | Expr_merge (c, hl) -> |

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

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

283 | mk_expr_alias_opt alias node defvars norm_expr |
||

284 | 8deaa2dd | tkahsai | |

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

286 | (* |
||

287 | 0a10042e | ploc | let norm_conditional_as_merge alias node norm_expr offsets defvars expr = |

288 | match expr.expr_desc with |
||

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

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

291 | | _ -> assert false |
||

292 | 52cfee34 | xthirioux | *) |

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

294 | 0a10042e | ploc | List.fold_right |

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

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

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

298 | ) |
||

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

300 | 22fe1c93 | ploc | |

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

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

303 | 22fe1c93 | ploc | match expr.expr_desc with |

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

305 | 0a10042e | ploc | let defvars, norm_e1 = normalize_expr node offsets defvars e1 in |

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

307 | 22fe1c93 | ploc | | Expr_power (e1, d) -> |

308 | 0a10042e | ploc | normalize_array_expr ~alias:alias node (List.tl offsets) defvars e1 |

309 | 22fe1c93 | ploc | | Expr_access (e1, d) -> normalize_array_expr ~alias:alias node (d::offsets) defvars e1 |

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

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

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

313 | 04a63d25 | xthirioux | | Expr_appl (id, args, None) when Basic_library.is_expr_internal_fun expr -> |

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

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

316 | 22fe1c93 | ploc | | _ -> normalize_expr ~alias:alias node offsets defvars expr |

317 | |||

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

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

320 | match expr.expr_desc with |
||

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

322 | 0a10042e | ploc | normalize_cond_expr ~alias:alias node (d::offsets) defvars e1 |

323 | 22fe1c93 | ploc | | Expr_ite (c, t, e) -> |

324 | 0a10042e | ploc | let defvars, norm_c = normalize_guard node defvars c in |

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

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

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

328 | 22fe1c93 | ploc | | Expr_merge (c, hl) -> |

329 | 0a10042e | ploc | let defvars, norm_hl = normalize_branches node offsets defvars hl in |

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

331 | 27dc3869 | ploc | | _ when !force_alias_ite -> |

332 | (* Forcing alias creation for then/else expressions *) |
||

333 | let defvars, norm_expr = |
||

334 | normalize_expr ~alias:alias node offsets defvars expr |
||

335 | in |
||

336 | mk_expr_alias_opt true node defvars norm_expr |
||

337 | | _ -> (* default case without the force_alias_ite option *) |
||

338 | normalize_expr ~alias:alias node offsets defvars expr |
||

339 | 0a10042e | ploc | |

340 | 22fe1c93 | ploc | and normalize_guard node defvars expr = |

341 | 0a10042e | ploc | let defvars, norm_expr = normalize_expr ~alias_basic:true node [] defvars expr in |

342 | d4807c3d | xthirioux | mk_expr_alias_opt true node defvars norm_expr |

343 | 22fe1c93 | ploc | |

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

345 | *) |
||

346 | let decouple_outputs node defvars eq = |
||

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

348 | match lhs, tys, cks with |
||

349 | | [], [], [] -> defvars, [] |
||

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

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

352 | then |
||

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

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

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

356 | else |
||

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

358 | | _ -> assert false in |
||

359 | let defvars', lhs' = |
||

360 | fold_lhs |
||

361 | defvars |
||

362 | eq.eq_lhs |
||

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

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

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

366 | |||

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

368 | 04a63d25 | xthirioux | (*Format.eprintf "normalize_eq %a@." Types.print_ty eq.eq_rhs.expr_type;*) |

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

370 | | Expr_pre _ |
||

371 | | Expr_fby _ -> |
||

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

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

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

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

376 | | Expr_array _ -> |
||

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

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

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

380 | 04a63d25 | xthirioux | | Expr_appl (id, _, None) when Basic_library.is_homomorphic_fun id && Types.is_array_type eq.eq_rhs.expr_type -> |

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

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

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

384 | | Expr_appl _ -> |
||

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

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

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

388 | | _ -> |
||

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

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

391 | norm_eq::defs', vars' |
||

392 | |||

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

394 | ie. |
||

395 | af5af1e8 | ploc | - updated locals |

396 | - new equations |
||

397 | 8deaa2dd | tkahsai | - |

398 | af5af1e8 | ploc | *) |

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

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

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

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

403 | 50dadc21 | ploc | let not_is_orig_var v = |

404 | List.for_all ((!=) v) orig_vars in |
||

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

406 | 333e3a25 | ploc | let eqs, auts = get_node_eqs node in |

407 | if auts != [] then assert false; (* Automata should be expanded by now. *) |
||

408 | List.fold_left (normalize_eq node) ([], orig_vars) eqs in |
||

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

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

411 | af5af1e8 | ploc | List.fold_left ( |

412 | 50dadc21 | ploc | fun (vars, def_accu, assert_accu) assert_ -> |

413 | let assert_expr = assert_.assert_expr in |
||

414 | let (defs, vars'), expr = |
||

415 | normalize_expr |
||

416 | ~alias:true (* forcing introduction of new equations for fcn calls *) |
||

417 | node |
||

418 | [] (* empty offset for arrays *) |
||

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

420 | assert_expr |
||

421 | in |
||

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

423 | 50dadc21 | ploc | vars', defs@def_accu, {assert_ with assert_expr = expr}::assert_accu |

424 | af5af1e8 | ploc | ) (vars, [], []) node.node_asserts in |

425 | 50dadc21 | ploc | let new_locals = List.filter not_is_orig_var vars in (* we filter out inout |

426 | vars and initial locals ones *) |
||

427 | 66359a5e | ploc | |

428 | let all_locals = node.node_locals @ new_locals in (* we add again, at the |
||

429 | 50dadc21 | ploc | beginning of the list the |

430 | local declared ones *) |
||

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

432 | f2b1c245 | ploc | |

433 | 66359a5e | ploc | |

434 | (* Updating annotations: traceability and machine types for fresh variables *) |
||

435 | |||

436 | (* Compute traceability info: |
||

437 | - gather newly bound variables |
||

438 | - compute the associated expression without aliases |
||

439 | *) |
||

440 | ea1c2906 | ploc | let new_annots = |

441 | if !Options.traces then |
||

442 | begin |
||

443 | 66359a5e | ploc | let diff_vars = List.filter (fun v -> not (List.mem v node.node_locals) ) all_locals in |

444 | ea1c2906 | ploc | let norm_traceability = { |

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

446 | let eq = |
||

447 | try |
||

448 | 45f0f48d | xthirioux | List.find (fun eq -> List.exists (fun v' -> v' = v.var_id ) eq.eq_lhs) (defs@assert_defs) |

449 | with Not_found -> |
||

450 | ( |
||

451 | Format.eprintf "Traceability annotation generation: var %s not found@." v.var_id; |
||

452 | assert false |
||

453 | ) |
||

454 | in |
||

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

456 | 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 |

457 | 66359a5e | ploc | Annotations.add_expr_ann node.node_id pair.eexpr_tag ["traceability"]; |

458 | ea1c2906 | ploc | (["traceability"], pair) |

459 | ) diff_vars; |
||

460 | annot_loc = Location.dummy_loc |
||

461 | 8deaa2dd | tkahsai | } |

462 | ea1c2906 | ploc | in |

463 | norm_traceability::node.node_annot |
||

464 | end |
||

465 | else |
||

466 | node.node_annot |
||

467 | af5af1e8 | ploc | in |

468 | ea1c2906 | ploc | |

469 | 66359a5e | ploc | let new_annots = |

470 | List.fold_left (fun annots v -> |
||

471 | f4050bef | ploc | if Machine_types.is_active && Machine_types.is_exportable v then |

472 | 66359a5e | ploc | let typ = Machine_types.get_specified_type v in |

473 | let typ_name = Machine_types.type_name typ in |
||

474 | |||

475 | let loc = v.var_loc in |
||

476 | let typ_as_string = |
||

477 | mkexpr |
||

478 | loc |
||

479 | (Expr_const |
||

480 | (Const_string typ_name)) |
||

481 | in |
||

482 | let pair = expr_to_eexpr (expr_of_expr_list loc [expr_of_vdecl v; typ_as_string]) in |
||

483 | Annotations.add_expr_ann node.node_id pair.eexpr_tag Machine_types.keyword; |
||

484 | {annots = [Machine_types.keyword, pair]; annot_loc = loc}::annots |
||

485 | else |
||

486 | annots |
||

487 | ) new_annots new_locals |
||

488 | in |
||

489 | 22fe1c93 | ploc | let node = |

490 | 50dadc21 | ploc | { node with |

491 | 66359a5e | ploc | node_locals = all_locals; |

492 | 50dadc21 | ploc | node_stmts = List.map (fun eq -> Eq eq) (defs @ assert_defs); |

493 | node_asserts = asserts; |
||

494 | node_annot = new_annots; |
||

495 | } |
||

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

497 | f2b1c245 | ploc | node |

498 | 50dadc21 | ploc | ) |

499 | f2b1c245 | ploc | |

500 | 22fe1c93 | ploc | |

501 | let normalize_decl decl = |
||

502 | match decl.top_decl_desc with |
||

503 | | Node nd -> |
||

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

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

506 | decl' |
||

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

508 | 8deaa2dd | tkahsai | |

509 | 27dc3869 | ploc | let normalize_prog ?(backend="C") decls = |

510 | let old_unfold_arrow_active = !unfold_arrow_active in |
||

511 | let old_force_alias_ite = !force_alias_ite in |
||

512 | 3436d1a6 | ploc | let old_force_alias_internal_fun = !force_alias_internal_fun in |

513 | 27dc3869 | ploc | |

514 | (* Backend specific configurations for normalization *) |
||

515 | let _ = |
||

516 | match backend with |
||

517 | | "lustre" -> |
||

518 | (* Special treatment of arrows in lustre backend. We want to keep them *) |
||

519 | unfold_arrow_active := false; |
||

520 | 3436d1a6 | ploc | | "emf" -> ( |

521 | (* Forcing ite normalization *) |
||

522 | force_alias_ite := true; |
||

523 | force_alias_internal_fun := true; |
||

524 | ) |
||

525 | 27dc3869 | ploc | | _ -> () (* No fancy options for other backends *) |

526 | in |
||

527 | 22fe1c93 | ploc | |

528 | 27dc3869 | ploc | (* Main algorithm: iterates over nodes *) |

529 | let res = List.map normalize_decl decls in |
||

530 | |||

531 | (* Restoring previous settings *) |
||

532 | unfold_arrow_active := old_unfold_arrow_active; |
||

533 | force_alias_ite := old_force_alias_ite; |
||

534 | 3436d1a6 | ploc | force_alias_internal_fun := old_force_alias_internal_fun; |

535 | 27dc3869 | ploc | res |

536 | |||

537 | (* Local Variables: *) |
||

538 | 22fe1c93 | ploc | (* compile-command:"make -C .." *) |

539 | (* End: *) |