Skip to content

Commit 1b9cf91

Browse files
committed
Create exp2eq.ml
A pass to simplify the language of expressions. It rewrites : - Ematch into EQmatch; - Ereset into EQreset.
1 parent 08ebec4 commit 1b9cf91

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/compiler/rewrite/exp2eq.ml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
(***********************************************************************)
2+
(* *)
3+
(* *)
4+
(* Zelus, a synchronous language for hybrid systems *)
5+
(* *)
6+
(* (c) 2024 Inria Paris (see the AUTHORS file) *)
7+
(* *)
8+
(* Copyright Institut National de Recherche en Informatique et en *)
9+
(* Automatique. All rights reserved. This file is distributed under *)
10+
(* the terms of the INRIA Non-Commercial License Agreement (see the *)
11+
(* LICENSE file). *)
12+
(* *)
13+
(* *********************************************************************)
14+
15+
(* translate some expressions into equations *)
16+
(* the constructs that are concerned are:
17+
*- [match e with P1 -> e1 | ... | Pn -> en] =>
18+
[let match e with P1 -> r = e1 | ... | Pn -> r = en in r]
19+
*- [reset e every c] => let reset r = e every c in r]
20+
*)
21+
22+
open Misc
23+
open Location
24+
open Ident
25+
open Zelus
26+
open Mapfold
27+
28+
let empty = ()
29+
30+
let fresh () = Ident.fresh "r"
31+
32+
let expression funs acc e =
33+
let ({ e_desc; e_loc } as e), acc = Mapfold.expression funs acc e in
34+
match e_desc with
35+
| Ematch { is_total; e; handlers } ->
36+
let result = fresh () in
37+
let handler { m_pat; m_body; m_env; m_loc; m_reset; m_zero } =
38+
{ m_pat; m_body = Aux.id_eq result m_body; m_env; m_loc; m_reset; m_zero } in
39+
let eq =
40+
{ eq_desc = EQmatch { is_total; e; handlers = List.map handler handlers };
41+
eq_write = Defnames.singleton result;
42+
eq_safe = true; eq_index = -1; eq_loc = e_loc } in
43+
Aux.e_let (Aux.leq [eq]) (Aux.var result), acc
44+
| Ereset(e, e_r) ->
45+
let result = fresh () in
46+
let eq =
47+
{ eq_desc = EQreset(Aux.id_eq result e, e_r);
48+
eq_write = Defnames.singleton result;
49+
eq_safe = true; eq_index = -1; eq_loc = e_loc } in
50+
Aux.e_let (Aux.leq [eq]) (Aux.var result), acc
51+
| _ -> e, acc
52+
53+
let set_index funs acc n =
54+
let _ = Ident.set n in n, acc
55+
let get_index funs acc n = Ident.get (), acc
56+
57+
let program _ p =
58+
let global_funs = Mapfold.default_global_funs in
59+
let funs =
60+
{ Mapfold.defaults with expression; set_index; get_index; global_funs } in
61+
let { p_impl_list } as p, _ =
62+
Mapfold.program_it funs empty p in
63+
{ p with p_impl_list = p_impl_list }
64+

0 commit comments

Comments
 (0)