Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / machine_code_common.ml @ e4edf171

History | View | Annotate | Download (11.2 KB)

1
open Lustre_types
2
open Machine_code_types
3
open Corelang
4
  
5
let print_statelocaltag = true
6

    
7
let rec pp_val fmt v =
8
  match v.value_desc with
9
    | Cst c         -> Printers.pp_const fmt c 
10
    | LocalVar v    ->
11
       if print_statelocaltag then
12
	 Format.fprintf fmt "%s(L)" v.var_id
13
       else
14
	 Format.pp_print_string fmt v.var_id
15
	   
16
    | StateVar v    ->
17
       if print_statelocaltag then
18
	 Format.fprintf fmt "%s(S)" v.var_id
19
       else
20
	 Format.pp_print_string fmt v.var_id
21
    | Array vl      -> Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:", " pp_val)  vl
22
    | Access (t, i) -> Format.fprintf fmt "%a[%a]" pp_val t pp_val i
23
    | Power (v, n)  -> Format.fprintf fmt "(%a^%a)" pp_val v pp_val n
24
    | Fun (n, vl)   -> Format.fprintf fmt "%s (%a)" n (Utils.fprintf_list ~sep:", " pp_val)  vl
25

    
26
let rec pp_instr fmt i =
27
  let _ =
28
    match i.instr_desc with
29
    | MLocalAssign (i,v) -> Format.fprintf fmt "%s<-l- %a" i.var_id pp_val v
30
    | MStateAssign (i,v) -> Format.fprintf fmt "%s<-s- %a" i.var_id pp_val v
31
    | MReset i           -> Format.fprintf fmt "reset %s" i
32
    | MNoReset i         -> Format.fprintf fmt "noreset %s" i
33
    | MStep (il, i, vl)  ->
34
       Format.fprintf fmt "%a = %s (%a)"
35
	 (Utils.fprintf_list ~sep:", " (fun fmt v -> Format.pp_print_string fmt v.var_id)) il
36
	 i
37
	 (Utils.fprintf_list ~sep:", " pp_val) vl
38
    | MBranch (g,hl)     ->
39
       Format.fprintf fmt "@[<v 2>case(%a) {@,%a@,}@]"
40
	 pp_val g
41
	 (Utils.fprintf_list ~sep:"@," pp_branch) hl
42
    | MComment s -> Format.pp_print_string fmt s
43
       
44
  in
45
  (* Annotation *)
46
  (* let _ = *)
47
  (*   match i.lustre_expr with None -> () | Some e -> Format.fprintf fmt " -- original expr: %a" Printers.pp_expr e *)
48
  (* in *)
49
  let _ = 
50
    match i.lustre_eq with None -> () | Some eq -> Format.fprintf fmt " -- original eq: %a" Printers.pp_node_eq eq
51
  in
52
  ()
53
    
54
and pp_branch fmt (t, h) =
55
  Format.fprintf fmt "@[<v 2>%s:@,%a@]" t (Utils.fprintf_list ~sep:"@," pp_instr) h
56

    
57
and pp_instrs fmt il =
58
  Format.fprintf fmt "@[<v 2>%a@]" (Utils.fprintf_list ~sep:"@," pp_instr) il
59

    
60

    
61
(* merge log: get_node_def was in c0f8 *)
62
(* Returns the node/machine associated to id in m calls *)
63
let get_node_def id m =
64
  try
65
    let (decl, _) = List.assoc id m.mcalls in
66
    Corelang.node_of_top decl
67
  with Not_found -> ( 
68
    (* Format.eprintf "Unable to find node %s in list [%a]@.@?" *)
69
    (*   id *)
70
    (*   (Utils.fprintf_list ~sep:", " (fun fmt (n,_) -> Format.fprintf fmt "%s" n)) m.mcalls *)
71
    (* ; *)
72
    raise Not_found
73
  )
74
    
75
(* merge log: machine_vars was in 44686 *)
76
let machine_vars m = m.mstep.step_inputs @ m.mstep.step_locals @ m.mstep.step_outputs @ m.mmemory
77

    
78
let pp_step fmt s =
79
  Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ "
80
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs
81
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_outputs
82
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_locals
83
    (Utils.fprintf_list ~sep:", " (fun fmt (_, c) -> pp_val fmt c)) s.step_checks
84
    (Utils.fprintf_list ~sep:"@ " pp_instr) s.step_instrs
85
    (Utils.fprintf_list ~sep:", " pp_val) s.step_asserts
86

    
87

    
88
let pp_static_call fmt (node, args) =
89
 Format.fprintf fmt "%s<%a>"
90
   (node_name node)
91
   (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) args
92

    
93
let pp_machine fmt m =
94
  Format.fprintf fmt
95
    "@[<v 2>machine %s@ mem      : %a@ instances: %a@ init     : %a@ const    : %a@ step     :@   @[<v 2>%a@]@ @  spec : @[%t@]@  annot : @[%a@]@]@ "
96
    m.mname.node_id
97
    (Utils.fprintf_list ~sep:", " Printers.pp_var) m.mmemory
98
    (Utils.fprintf_list ~sep:", " (fun fmt (o1, o2) -> Format.fprintf fmt "(%s, %a)" o1 pp_static_call o2)) m.minstances
99
    (Utils.fprintf_list ~sep:"@ " pp_instr) m.minit
100
    (Utils.fprintf_list ~sep:"@ " pp_instr) m.mconst
101
    pp_step m.mstep
102
    (fun fmt -> match m.mspec with | None -> () | Some spec -> Printers.pp_spec fmt spec)
103
    (Utils.fprintf_list ~sep:"@ " Printers.pp_expr_annot) m.mannot
104

    
105
let pp_machines fmt ml =
106
  Format.fprintf fmt "@[<v 0>%a@]" (Utils.fprintf_list ~sep:"@," pp_machine) ml
107

    
108
  
109
let rec is_const_value v =
110
  match v.value_desc with
111
  | Cst _          -> true
112
  | Fun (id, args) -> Basic_library.is_value_internal_fun v && List.for_all is_const_value args
113
  | _              -> false
114

    
115
(* Returns the declared stateless status and the computed one. *)
116
let get_stateless_status m =
117
 (m.mname.node_dec_stateless, try Utils.desome m.mname.node_stateless with _ -> failwith ("stateless status of machine " ^ m.mname.node_id ^ " not computed"))
118

    
119
let is_stateless m = m.minstances = [] && m.mmemory = []
120

    
121
let is_input m id =
122
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_inputs
123

    
124
let is_output m id =
125
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs
126

    
127
let is_memory m id =
128
  List.exists (fun o -> o.var_id = id.var_id) m.mmemory
129

    
130
let mk_conditional ?lustre_eq c t e =
131
  mkinstr ?lustre_eq:lustre_eq  (MBranch(c, [ (tag_true, t); (tag_false, e) ]))
132

    
133

    
134

    
135
let mk_val v t =
136
  { value_desc = v; 
137
    value_type = t; 
138
    value_annot = None }
139
    
140
let arrow_machine =
141
  let state = "_first" in
142
  let var_state = dummy_var_decl state Type_predef.type_bool(* (Types.new_ty Types.Tbool) *) in
143
  let var_input1 = List.nth Arrow.arrow_desc.node_inputs 0 in
144
  let var_input2 = List.nth Arrow.arrow_desc.node_inputs 1 in
145
  let var_output = List.nth Arrow.arrow_desc.node_outputs 0 in
146
  let cst b = mk_val (Cst (const_of_bool b)) Type_predef.type_bool in
147
  let t_arg = Types.new_univar () in (* TODO Xavier: c'est bien la bonne def ? *)
148
  {
149
    mname = Arrow.arrow_desc;
150
    mmemory = [var_state];
151
    mcalls = [];
152
    minstances = [];
153
    minit = [mkinstr (MStateAssign(var_state, cst true))];
154
    mstatic = [];
155
    mconst = [];
156
    mstep = {
157
      step_inputs = Arrow.arrow_desc.node_inputs;
158
      step_outputs = Arrow.arrow_desc.node_outputs;
159
      step_locals = [];
160
      step_checks = [];
161
      step_instrs = [mk_conditional (mk_val (StateVar var_state) Type_predef.type_bool)
162
			(List.map mkinstr
163
			[MStateAssign(var_state, cst false);
164
			 MLocalAssign(var_output, mk_val (LocalVar var_input1) t_arg)])
165
                        (List.map mkinstr
166
			[MLocalAssign(var_output, mk_val (LocalVar var_input2) t_arg)]) ];
167
      step_asserts = [];
168
    };
169
    mspec = None;
170
    mannot = [];
171
  }
172

    
173
let empty_desc =
174
  {
175
    node_id = Arrow.arrow_id;
176
    node_type = Types.bottom;
177
    node_clock = Clocks.bottom;
178
    node_inputs= [];
179
    node_outputs= [];
180
    node_locals= [];
181
    node_gencalls = [];
182
    node_checks = [];
183
    node_asserts = [];
184
    node_stmts= [];
185
    node_dec_stateless = true;
186
    node_stateless = Some true;
187
    node_spec = None;
188
    node_annot = [];  }
189

    
190
let empty_machine =
191
  {
192
    mname = empty_desc;
193
    mmemory = [];
194
    mcalls = [];
195
    minstances = [];
196
    minit = [];
197
    mstatic = [];
198
    mconst = [];
199
    mstep = {
200
      step_inputs = [];
201
      step_outputs = [];
202
      step_locals = [];
203
      step_checks = [];
204
      step_instrs = [];
205
      step_asserts = [];
206
    };
207
    mspec = None;
208
    mannot = [];
209
  }
210

    
211
let new_instance =
212
  let cpt = ref (-1) in
213
  fun caller callee tag ->
214
    begin
215
      let o =
216
	if Stateless.check_node callee then
217
	  node_name callee
218
	else
219
	  Printf.sprintf "ni_%d" (incr cpt; !cpt) in
220
      let o =
221
	if !Options.ansi && is_generic_node callee
222
	then Printf.sprintf "%s_inst_%d" o (Utils.position (fun e -> e.expr_tag = tag) caller.node_gencalls)
223
	else o in
224
      o
225
    end
226

    
227

    
228
let get_machine_opt machines name =
229
  List.fold_left
230
    (fun res m ->
231
      match res with
232
      | Some _ -> res
233
      | None -> if m.mname.node_id = name then Some m else None)
234
    None machines
235

    
236
let get_machine machines node_name =
237
 try
238
  List.find (fun m  -> m.mname.node_id = node_name) machines
239
 with Not_found -> Format.eprintf "Unable to find machine %s in machines %a@.@?"
240
   node_name
241
   (Utils.fprintf_list ~sep:", " (fun fmt m -> Format.pp_print_string fmt m.mname.node_id)) machines
242
   ; assert false
243
     
244
    
245
let get_const_assign m id =
246
  try
247
    match get_instr_desc (List.find
248
	     (fun instr -> match get_instr_desc instr with
249
	     | MLocalAssign (v, _) -> v == id
250
	     | _ -> false)
251
	     m.mconst
252
    ) with
253
    | MLocalAssign (_, e) -> e
254
    | _                   -> assert false
255
  with Not_found -> assert false
256

    
257

    
258
let value_of_ident loc m id =
259
  (* is is a state var *)
260
  try
261
    let v = List.find (fun v -> v.var_id = id) m.mmemory
262
    in mk_val (StateVar v) v.var_type 
263
  with Not_found ->
264
    try (* id is a node var *)
265
      let v = get_node_var id m.mname
266
      in mk_val (LocalVar v) v.var_type
267
  with Not_found ->
268
    try (* id is a constant *)
269
      let c = Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))
270
      in mk_val (LocalVar c) c.var_type
271
    with Not_found ->
272
      (* id is a tag *)
273
      let t = Const_tag id
274
      in mk_val (Cst t) (Typing.type_const loc t)
275

    
276
(* type of internal fun used in dimension expression *)
277
let type_of_value_appl f args =
278
  if List.mem f Basic_library.arith_funs
279
  then (List.hd args).value_type
280
  else Type_predef.type_bool
281

    
282
let rec value_of_dimension m dim =
283
  match dim.Dimension.dim_desc with
284
  | Dimension.Dbool b         ->
285
     mk_val (Cst (Const_tag (if b then Corelang.tag_true else Corelang.tag_false))) Type_predef.type_bool
286
  | Dimension.Dint i          ->
287
     mk_val (Cst (Const_int i)) Type_predef.type_int
288
  | Dimension.Dident v        -> value_of_ident dim.Dimension.dim_loc m v
289
  | Dimension.Dappl (f, args) ->
290
     let vargs = List.map (value_of_dimension m) args
291
     in mk_val (Fun (f, vargs)) (type_of_value_appl f vargs) 
292
  | Dimension.Dite (i, t, e)  ->
293
     (match List.map (value_of_dimension m) [i; t; e] with
294
     | [vi; vt; ve] -> mk_val (Fun ("ite", [vi; vt; ve])) vt.value_type
295
     | _            -> assert false)
296
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
297
  | _                         -> assert false
298

    
299
let rec dimension_of_value value =
300
  match value.value_desc with
301
  | Cst (Const_tag t) when t = Corelang.tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
302
  | Cst (Const_tag t) when t = Corelang.tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
303
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i
304
  | LocalVar v                                    -> Dimension.mkdim_ident Location.dummy_loc v.var_id
305
  | Fun (f, args)                                 -> Dimension.mkdim_appl  Location.dummy_loc f (List.map dimension_of_value args)
306
  | _                                             -> assert false
307

    
308

    
309
     let rec join_branches hl1 hl2 =
310
 match hl1, hl2 with
311
 | []          , _            -> hl2
312
 | _           , []           -> hl1
313
 | (t1, h1)::q1, (t2, h2)::q2 ->
314
   if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else
315
   if t1 > t2 then (t2, h2) :: join_branches hl1 q2
316
   else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2
317

    
318
and join_guards inst1 insts2 =
319
 match get_instr_desc inst1, List.map get_instr_desc insts2 with
320
 | _                   , []                               ->
321
   [inst1]
322
 | MBranch (x1, hl1), MBranch (x2, hl2) :: q when x1 = x2 ->
323
    mkinstr
324
      (* TODO on pourrait uniquement concatener les lustres de inst1 et hd(inst2) *)
325
      (MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2)))
326
   :: (List.tl insts2)
327
 | _ -> inst1 :: insts2
328

    
329
let join_guards_list insts =
330
 List.fold_right join_guards insts []