Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / machine_code_common.ml @ f4cba4b8

History | View | Annotate | Download (11.5 KB)

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

    
7
let is_memory m id =
8
  List.exists (fun o -> o.var_id = id.var_id) m.mmemory
9

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

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

    
63
and pp_instrs m fmt il =
64
  Format.fprintf fmt "@[<v 2>%a@]" (Utils.fprintf_list ~sep:"@," (pp_instr m)) il
65

    
66

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

    
84
let pp_step m fmt s =
85
  Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ "
86
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs
87
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_outputs
88
    (Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_locals
89
    (Utils.fprintf_list ~sep:", " (fun fmt (_, c) -> pp_val m fmt c)) s.step_checks
90
    (Utils.fprintf_list ~sep:"@ " (pp_instr m)) s.step_instrs
91
    (Utils.fprintf_list ~sep:", " (pp_val m)) s.step_asserts
92

    
93

    
94
let pp_static_call fmt (node, args) =
95
 Format.fprintf fmt "%s<%a>"
96
   (node_name node)
97
   (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) args
98

    
99
let pp_machine fmt m =
100
  Format.fprintf fmt
101
    "@[<v 2>machine %s@ mem      : %a@ instances: %a@ init     : %a@ const    : %a@ step     :@   @[<v 2>%a@]@ @  spec : @[%t@]@  annot : @[%a@]@]@ "
102
    m.mname.node_id
103
    (Utils.fprintf_list ~sep:", " Printers.pp_var) m.mmemory
104
    (Utils.fprintf_list ~sep:", " (fun fmt (o1, o2) -> Format.fprintf fmt "(%s, %a)" o1 pp_static_call o2)) m.minstances
105
    (Utils.fprintf_list ~sep:"@ " (pp_instr m)) m.minit
106
    (Utils.fprintf_list ~sep:"@ " (pp_instr m)) m.mconst
107
    (pp_step m) m.mstep
108
    (fun fmt -> match m.mspec with | None -> ()
109
                                   | Some (NodeSpec id) -> Format.fprintf fmt "cocospec: %s" id
110
                                   | Some (Contract spec) -> Printers.pp_spec fmt spec)
111
    (Utils.fprintf_list ~sep:"@ " Printers.pp_expr_annot) m.mannot
112

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

    
116
  
117
let rec is_const_value v =
118
  match v.value_desc with
119
  | Cst _          -> true
120
  | Fun (id, args) -> Basic_library.is_value_internal_fun v && List.for_all is_const_value args
121
  | _              -> false
122

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

    
127
let is_stateless m = m.minstances = [] && m.mmemory = []
128

    
129
let is_input m id =
130
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_inputs
131

    
132
let is_output m id =
133
  List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs
134

    
135

    
136
let mk_conditional ?lustre_eq c t e =
137
  mkinstr ?lustre_eq:lustre_eq  (MBranch(c, [ (tag_true, t); (tag_false, e) ]))
138

    
139

    
140

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

    
180
let empty_desc =
181
  {
182
    node_id = Arrow.arrow_id;
183
    node_type = Types.bottom;
184
    node_clock = Clocks.bottom;
185
    node_inputs= [];
186
    node_outputs= [];
187
    node_locals= [];
188
    node_gencalls = [];
189
    node_checks = [];
190
    node_asserts = [];
191
    node_stmts= [];
192
    node_dec_stateless = true;
193
    node_stateless = Some true;
194
    node_spec = None;
195
    node_annot = [];
196
    node_iscontract = false;
197
}
198

    
199
let empty_machine =
200
  {
201
    mname = empty_desc;
202
    mmemory = [];
203
    mcalls = [];
204
    minstances = [];
205
    minit = [];
206
    mstatic = [];
207
    mconst = [];
208
    mstep = {
209
      step_inputs = [];
210
      step_outputs = [];
211
      step_locals = [];
212
      step_checks = [];
213
      step_instrs = [];
214
      step_asserts = [];
215
    };
216
    mspec = None;
217
    mannot = [];
218
    msch = None
219
  }
220

    
221
let new_instance =
222
  let cpt = ref (-1) in
223
  fun callee tag ->
224
    begin
225
      let o =
226
	if Stateless.check_node callee then
227
	  node_name callee
228
	else
229
	  Printf.sprintf "ni_%d" (incr cpt; !cpt) in
230
      let o =
231
	if !Options.ansi && is_generic_node callee
232
	then Printf.sprintf "%s_inst_%d"
233
               o
234
               (incr cpt; !cpt)
235
	else o in
236
      o
237
    end
238

    
239

    
240
let get_machine_opt machines name =
241
  List.fold_left
242
    (fun res m ->
243
      match res with
244
      | Some _ -> res
245
      | None -> if m.mname.node_id = name then Some m else None)
246
    None machines
247

    
248
let get_machine machines node_name =
249
 try
250
  List.find (fun m  -> m.mname.node_id = node_name) machines
251
 with Not_found -> Format.eprintf "Unable to find machine %s in machines %a@.@?"
252
   node_name
253
   (Utils.fprintf_list ~sep:", " (fun fmt m -> Format.pp_print_string fmt m.mname.node_id)) machines
254
   ; assert false
255
     
256
    
257
let get_const_assign m id =
258
  try
259
    match get_instr_desc (List.find
260
	     (fun instr -> match get_instr_desc instr with
261
	     | MLocalAssign (v, _) -> v == id
262
	     | _ -> false)
263
	     m.mconst
264
    ) with
265
    | MLocalAssign (_, e) -> e
266
    | _                   -> assert false
267
  with Not_found -> assert false
268

    
269

    
270
let value_of_ident loc m id =
271
  (* is is a state var *)
272
  try
273
    let v = List.find (fun v -> v.var_id = id) m.mmemory
274
    in mk_val (Var v) v.var_type 
275
  with Not_found ->
276
    try (* id is a node var *)
277
      let v = get_node_var id m.mname
278
      in mk_val (Var v) v.var_type
279
  with Not_found ->
280
    try (* id is a constant *)
281
      let c = Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))
282
      in mk_val (Var c) c.var_type
283
    with Not_found ->
284
      (* id is a tag *)
285
      let t = Const_tag id
286
      in mk_val (Cst t) (Typing.type_const loc t)
287

    
288
(* type of internal fun used in dimension expression *)
289
let type_of_value_appl f args =
290
  if List.mem f Basic_library.arith_funs
291
  then (List.hd args).value_type
292
  else Type_predef.type_bool
293

    
294
let rec value_of_dimension m dim =
295
  match dim.Dimension.dim_desc with
296
  | Dimension.Dbool b         ->
297
     mk_val (Cst (Const_tag (if b then Corelang.tag_true else Corelang.tag_false))) Type_predef.type_bool
298
  | Dimension.Dint i          ->
299
     mk_val (Cst (Const_int i)) Type_predef.type_int
300
  | Dimension.Dident v        -> value_of_ident dim.Dimension.dim_loc m v
301
  | Dimension.Dappl (f, args) ->
302
     let vargs = List.map (value_of_dimension m) args
303
     in mk_val (Fun (f, vargs)) (type_of_value_appl f vargs) 
304
  | Dimension.Dite (i, t, e)  ->
305
     (match List.map (value_of_dimension m) [i; t; e] with
306
     | [vi; vt; ve] -> mk_val (Fun ("ite", [vi; vt; ve])) vt.value_type
307
     | _            -> assert false)
308
  | Dimension.Dlink dim'      -> value_of_dimension m dim'
309
  | _                         -> assert false
310

    
311
let rec dimension_of_value value =
312
  match value.value_desc with
313
  | Cst (Const_tag t) when t = Corelang.tag_true  -> Dimension.mkdim_bool  Location.dummy_loc true
314
  | Cst (Const_tag t) when t = Corelang.tag_false -> Dimension.mkdim_bool  Location.dummy_loc false
315
  | Cst (Const_int i)                             -> Dimension.mkdim_int   Location.dummy_loc i
316
  | Var v                                         -> Dimension.mkdim_ident Location.dummy_loc v.var_id
317
  | Fun (f, args)                                 -> Dimension.mkdim_appl  Location.dummy_loc f (List.map dimension_of_value args)
318
  | _                                             -> assert false
319

    
320

    
321
     let rec join_branches hl1 hl2 =
322
 match hl1, hl2 with
323
 | []          , _            -> hl2
324
 | _           , []           -> hl1
325
 | (t1, h1)::q1, (t2, h2)::q2 ->
326
   if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else
327
   if t1 > t2 then (t2, h2) :: join_branches hl1 q2
328
   else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2
329

    
330
and join_guards inst1 insts2 =
331
 match get_instr_desc inst1, List.map get_instr_desc insts2 with
332
 | _                   , []                               ->
333
   [inst1]
334
 | MBranch (x1, hl1), MBranch (x2, hl2) :: q when x1 = x2 ->
335
    mkinstr
336
      (* TODO on pourrait uniquement concatener les lustres de inst1 et hd(inst2) *)
337
      (MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2)))
338
   :: (List.tl insts2)
339
 | _ -> inst1 :: insts2
340

    
341
let join_guards_list insts =
342
 List.fold_right join_guards insts []