Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / basic_library.ml @ 04a188ec

History | View | Annotate | Download (11.2 KB)

1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT - LIFL             *)
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
(* Parts of this file come from the Prelude compiler *)
13

    
14
(*open LustreSpec*)
15
open Type_predef
16
open Clock_predef
17
open Delay_predef
18
open Dimension
19

    
20
module TE = Env
21

    
22
let static_op ty =
23
  type_static (mkdim_var ()) ty
24

    
25
let type_env =
26
  List.fold_left
27
    (fun env (op, op_type) -> TE.add_value env op op_type)
28
    TE.initial
29
    [
30
      "true", (static_op type_bool);
31
      "false", (static_op type_bool);
32
      "+", (static_op type_bin_poly_op);
33
      "uminus", (static_op type_unary_poly_op);
34
      "-", (static_op type_bin_poly_op);
35
      "*", (static_op type_bin_poly_op);
36
      "/", (static_op type_bin_poly_op);
37
      "mod", (static_op type_bin_int_op);
38
      "&&", (static_op type_bin_bool_op);
39
      "||", (static_op type_bin_bool_op);
40
      "xor", (static_op type_bin_bool_op);
41
      "equi", (static_op type_bin_bool_op);
42
      "impl", (static_op type_bin_bool_op);
43
      "<", (static_op type_bin_comp_op);
44
      "<=", (static_op type_bin_comp_op);
45
      ">", (static_op type_bin_comp_op);
46
      ">=", (static_op type_bin_comp_op);
47
      "!=", (static_op type_bin_comp_op);
48
      "=", (static_op type_bin_comp_op);
49
      "not", (static_op type_unary_bool_op)
50
]
51

    
52
module CE = Env
53

    
54
let clock_env =
55
  let init_env = CE.initial in
56
  let env' =
57
    List.fold_right (fun op env -> CE.add_value env op ck_nullary_univ)
58
      ["true"; "false"] init_env in
59
  let env' =
60
    List.fold_right (fun op env -> CE.add_value env op ck_unary_univ)
61
      ["uminus"; "not"] env' in
62
  let env' =
63
    List.fold_right (fun op env -> CE.add_value env op ck_bin_univ)
64
      ["+"; "-"; "*"; "/"; "mod"; "&&"; "||"; "xor"; "equi"; "impl"; "<"; "<="; ">"; ">="; "!="; "="] env' in
65
  env'
66

    
67
module DE = Env
68

    
69
let delay_env =
70
  let init_env = DE.initial in
71
  let env' =
72
    List.fold_right (fun op env -> DE.add_value env op delay_nullary_poly_op)
73
      ["true"; "false"] init_env in
74
  let env' =
75
    List.fold_right (fun op env -> DE.add_value env op delay_unary_poly_op)
76
      ["uminus"; "not"] env' in
77
  let env' =
78
    List.fold_right (fun op env -> DE.add_value env op delay_binary_poly_op)
79
      ["+"; "-"; "*"; "/"; "mod"; "&&"; "||"; "xor"; "equi"; "impl"; "<"; "<="; ">"; ">="; "!="; "="] env' in
80
  let env' =
81
    List.fold_right (fun op env -> DE.add_value env op delay_ternary_poly_op)
82
      [] env' in
83
  env'
84

    
85
module VE = Env
86

    
87
let eval_dim_env =
88
  let defs = [
89
    "uminus", (function [Dint a] -> Dint (-a)           | _ -> assert false);
90
    "not", (function [Dbool b] -> Dbool (not b)         | _ -> assert false);
91
    "+", (function [Dint a; Dint b] -> Dint (a+b)       | _ -> assert false);
92
    "-", (function [Dint a; Dint b] -> Dint (a-b)       | _ -> assert false);
93
    "*", (function [Dint a; Dint b] -> Dint (a*b)       | _ -> assert false);
94
    "/", (function [Dint a; Dint b] -> Dint (a/b)       | _ -> assert false);
95
    "mod", (function [Dint a; Dint b] -> Dint (a mod b) | _ -> assert false);
96
    "&&", (function [Dbool a; Dbool b] -> Dbool (a&&b)  | _ -> assert false);
97
    "||", (function [Dbool a; Dbool b] -> Dbool (a||b)  | _ -> assert false);
98
    "xor", (function [Dbool a; Dbool b] -> Dbool (a<>b) | _ -> assert false);
99
    "equi", (function [Dbool a; Dbool b] -> Dbool (a=b) | _ -> assert false);
100
    "impl", (function [Dbool a; Dbool b] -> Dbool (a<=b)| _ -> assert false);
101
    "<", (function [Dint a; Dint b] -> Dbool (a<b)      | _ -> assert false);
102
    ">", (function [Dint a; Dint b] -> Dbool (a>b)      | _ -> assert false);
103
    "<=", (function [Dint a; Dint b] -> Dbool (a<=b)    | _ -> assert false);
104
    ">=", (function [Dint a; Dint b] -> Dbool (a>=b)    | _ -> assert false);
105
    "!=", (function [a; b] -> Dbool (a<>b)              | _ -> assert false);
106
    "=", (function [a; b] -> Dbool (a=b)                | _ -> assert false);
107
  ]
108
  in
109
  List.fold_left
110
    (fun env (op, op_eval) -> VE.add_value env op op_eval)
111
    VE.initial
112
    defs
113

    
114
let arith_funs = ["+";"-";"*";"/";"mod"; "uminus"]
115
let bool_funs  = ["&&";"||";"xor";"equi";"impl"; "not"]
116
let rel_funs   = ["<";">";"<=";">=";"!=";"="]
117

    
118
let internal_funs = arith_funs@bool_funs@rel_funs
119
 
120
let rec is_internal_fun x targs =
121
(*Format.eprintf "is_internal_fun %s %a@." x Types.print_ty (List.hd targs);*)
122
  match targs with
123
  | []                              -> assert false
124
  | t::_ when Types.is_real_type t  -> List.mem x internal_funs && not !Options.mpfr 
125
  | t::_ when Types.is_array_type t -> is_internal_fun x [Types.array_element_type t]
126
  | _                               -> List.mem x internal_funs
127

    
128
let is_expr_internal_fun expr =
129
  let open Lustre_types in
130
  match expr.expr_desc with
131
  | Expr_appl (f, e, _) -> is_internal_fun f (Types.type_list_of_type e.expr_type)
132
  | _                   -> assert false
133

    
134
let is_value_internal_fun v =
135
  let open Machine_code_types in
136
  match v.value_desc with
137
  | Fun (f, vl) -> is_internal_fun f (List.map (fun v -> v.value_type) vl)
138
  | _           -> assert false
139

    
140
let is_numeric_operator x =
141
  List.mem x arith_funs
142

    
143
let is_homomorphic_fun x =
144
  List.mem x internal_funs
145

    
146
let is_stateless_fun x =
147
  List.mem x internal_funs
148

    
149

    
150
(* let pp_java i pp_val fmt vl = *)
151
(*   match i, vl with *)
152
(*   (\*  | "ite", [v1; v2; v3] -> Format.fprintf fmt "(%a?(%a):(%a))" pp_val v1 pp_val v2 pp_val v3 *\) *)
153
(*     | "uminus", [v] -> Format.fprintf fmt "(- %a)" pp_val v *)
154
(*     | "not", [v] -> Format.fprintf fmt "(!%a)" pp_val v *)
155
(*     | "impl", [v1; v2] -> Format.fprintf fmt "(!%a || %a)" pp_val v1 pp_val v2 *)
156
(*     | "=", [v1; v2] -> Format.fprintf fmt "(%a == %a)" pp_val v1 pp_val v2 *)
157
(*     | "mod", [v1; v2] -> Format.fprintf fmt "(%a %% %a)" pp_val v1 pp_val v2 *)
158
(*     | "equi", [v1; v2] -> Format.fprintf fmt "(%a == %a)" pp_val v1 pp_val v2 *)
159
(*     | "xor", [v1; v2] -> Format.fprintf fmt "(%a != %a)" pp_val v1 pp_val v2 *)
160
(*     | _, [v1; v2] -> Format.fprintf fmt "(%a %s %a)" pp_val v1 i pp_val v2 *)
161
(*     | _ -> (Format.eprintf "internal error: Basic_library.pp_java %s@." i; assert false) *)
162

    
163
let rec partial_eval op e opt =
164
  let open Lustre_types in
165
  let is_zero e =
166
    match e.expr_desc with
167
    | Expr_const (Const_int 0) -> true
168
    | Expr_const (Const_real r) when Real.is_zero r -> true
169
    | _ -> false
170
  in
171
  let is_one e =
172
    match e.expr_desc with
173
    | Expr_const (Const_int 1) -> true
174
    | Expr_const (Const_real r) when Real.is_one r -> true
175
    | _ -> false
176
  in
177
  let is_true, is_false =
178
    let is_tag t e = e.expr_desc = Expr_const (Const_tag t) in
179
    is_tag tag_true, is_tag tag_false
180
  in
181
  let int_arith_op, real_arith_op=
182
    let assoc op=
183
      match op with
184
      | "+" -> (+), Real.add
185
      | "-" -> (-), Real.minus
186
      | "*" -> ( * ), Real.times
187
      | "/" -> (/), Real.div
188
      | "mod" -> (mod), (fun _ _ -> assert false)
189
      | _ -> assert false  
190
    in
191
    (fun op -> fst(assoc op)), (fun op -> snd(assoc op))
192
  in
193
  let int_rel_op, real_rel_op =
194
    let assoc op =
195
      match op with
196
      | "<" -> (<), Real.lt
197
      |">" -> (>), Real.gt
198
      |"<="-> (<=), Real.le
199
      | ">=" -> (>=), Real.ge
200
      |"!=" -> (!=), Real.diseq
201
      |"=" -> (=), Real.eq
202
      | _ -> assert false
203
    in
204
    (fun op -> fst(assoc op)), (fun op -> snd(assoc op))
205
  in
206
  let eval_bool_fun op e1 e2  =
207
    let s2b s = if s= tag_true then true else if s=tag_false then false else assert false in
208
    let e1, e2 = s2b e1, s2b e2 in
209
    let r =
210
      match op with
211
      "&&" -> e1 && e2
212
    | "||" -> e1 || e2
213
    | "xor" -> (e1 && e2) || ((not e1) && (not e2)) 
214
    | "impl" -> (not e1) || e2
215
    | "equi" -> ((not e1) || e2) && ((not e2) || e1)
216
    | _ -> assert false
217
    in
218
    if r then Const_tag tag_true else Const_tag tag_false
219
    in
220
  let is_const e =
221
    match e.expr_desc with Expr_const _ -> true | _ -> false
222
  in
223
  let deconst e =
224
    match e.expr_desc with
225
    | Expr_const c -> c
226
    | _ -> assert false
227
  in
228
  match op, (match  e.expr_desc with Expr_tuple el -> el | _ -> [e]) with
229
  | _, el when List.for_all is_const el -> (
230
    let new_cst = 
231
      match op, List.map deconst el with
232
      | ("+"|"-"|"*"|"/"|"mod"), [Const_int c1; Const_int c2] ->
233
          Const_int (int_arith_op op c1 c2)
234
      | ("+"|"-"|"*"|"/"), [Const_real c1; Const_real c2] ->
235
         Const_real (real_arith_op op c1 c2)
236
      | "uminus", [Const_int c] -> Const_int (-c)
237
      | "uminus", [Const_real c] -> Const_real (Real.uminus c)
238
      | rel_fun, [Const_int c1; Const_int c2]
239
           when List.mem rel_fun rel_funs ->
240
         if int_rel_op op c1 c2 then
241
           Const_tag tag_true
242
         else
243
           Const_tag tag_false
244
      | rel_fun, [Const_real c1; Const_real c2]
245
           when List.mem rel_fun rel_funs ->
246
         if real_rel_op op c1 c2 then
247
           Const_tag tag_true
248
         else
249
           Const_tag tag_false
250
      | "=", [Const_tag t1; Const_tag t2]
251
             ->
252
         if t1 = t2 then
253
           Const_tag tag_true
254
         else
255
           Const_tag tag_false
256
      | "!=", [Const_tag t1; Const_tag t2]
257
             ->
258
         if t1 = t2 then
259
           Const_tag tag_false
260
         else
261
           Const_tag tag_true
262
      | "not", [Const_tag c] -> Const_tag( if c = tag_true then tag_false else if c = tag_false then tag_true else assert false)
263
      | bool_fun, [Const_tag c1; Const_tag c2]
264
           when List.mem bool_fun bool_funs ->
265
         eval_bool_fun bool_fun c1 c2 
266
      | _ -> let loc= e.expr_loc in
267
             let err =Error.Unbound_symbol (op ^ (string_of_bool (List.mem op rel_funs)) ^ " in basic library") in
268
             raise (Error.Error (loc, err))
269
    in
270
    Expr_const new_cst 
271
  )       
272
  | op, el -> ( (* at least one of the arguments is non constant *)
273
    match op, el with
274
    | "+", [e0; e] when is_zero e0 ->
275
       e.expr_desc
276
    | "+", [e; e0] when is_zero e0 ->
277
       e.expr_desc
278
    | "-", [e; e0] when is_zero e0 ->
279
       e.expr_desc
280
    | "-", [e0; e] when is_zero e0 ->
281
       Expr_appl("uminus", e, None)
282
    | ("*"|"/"), [e0; e] when is_zero e0 -> e0.expr_desc
283
    | "*", [e; e0] when is_zero e0 -> e0.expr_desc
284
    | "*", [e1; e] when is_one e1 -> e.expr_desc
285
    | "/", [e; e1] when is_one e1 -> e.expr_desc
286
    | "&&", [efalse; _] when is_false efalse ->
287
       Expr_const (Const_tag tag_false)
288
    | "&&", [_; efalse] when is_false efalse ->
289
       Expr_const (Const_tag tag_false)
290
    | "||", [etrue; _] when is_true etrue ->
291
       Expr_const (Const_tag tag_true)
292
    | "||", [_; etrue] when is_true etrue ->
293
       Expr_const (Const_tag tag_true)
294
    | "impl", [efalse; _] when is_false efalse ->
295
       Expr_const (Const_tag tag_true)
296
    | _ ->
297
       Expr_appl(op, e, opt)
298
                               )
299
                  (* Local Variables: *)
300
                  (* compile-command:"make -C .." *)
301
                  (* End: *)
302

    
303

    
304
let _ =
305
  (* Loading environement *)
306
  Global.type_env := type_env;
307
  Global.clock_env := clock_env