lustrec / src / machine_code.ml @ 15003796
History  View  Annotate  Download (20.3 KB)
1 
(********************************************************************) 

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  
12 
open LustreSpec 
13 
open Corelang 
14 
open Clocks 
15 
open Causality 
16  
17 
exception NormalizationError 
18  
19 
module OrdVarDecl:Map.OrderedType with type t=var_decl = 
20 
struct type t = var_decl;; let compare = compare end 
21  
22 
module ISet = Set.Make(OrdVarDecl) 
23  
24 
type value_t = 
25 
 Cst of constant 
26 
 LocalVar of var_decl 
27 
 StateVar of var_decl 
28 
 Fun of ident * value_t list 
29 
 Array of value_t list 
30 
 Access of value_t * value_t 
31 
 Power of value_t * value_t 
32  
33 
type instr_t = 
34 
 MLocalAssign of var_decl * value_t 
35 
 MStateAssign of var_decl * value_t 
36 
 MReset of ident 
37 
 MStep of var_decl list * ident * value_t list 
38 
 MBranch of value_t * (label * instr_t list) list 
39  
40 
let rec pp_val fmt v = 
41 
match v with 
42 
 Cst c > Printers.pp_const fmt c 
43 
 LocalVar v > Format.pp_print_string fmt v.var_id 
44 
 StateVar v > Format.pp_print_string fmt v.var_id 
45 
 Array vl > Format.fprintf fmt "[%a]" (Utils.fprintf_list ~sep:", " pp_val) vl 
46 
 Access (t, i) > Format.fprintf fmt "%a[%a]" pp_val t pp_val i 
47 
 Power (v, n) > Format.fprintf fmt "(%a^%a)" pp_val v pp_val n 
48 
 Fun (n, vl) > Format.fprintf fmt "%s (%a)" n (Utils.fprintf_list ~sep:", " pp_val) vl 
49  
50 
let rec pp_instr fmt i = 
51 
match i with 
52 
 MLocalAssign (i,v) > Format.fprintf fmt "%s<l %a" i.var_id pp_val v 
53 
 MStateAssign (i,v) > Format.fprintf fmt "%s<s %a" i.var_id pp_val v 
54 
 MReset i > Format.fprintf fmt "reset %s" i 
55 
 MStep (il, i, vl) > 
56 
Format.fprintf fmt "%a = %s (%a)" 
57 
(Utils.fprintf_list ~sep:", " (fun fmt v > Format.pp_print_string fmt v.var_id)) il 
58 
i 
59 
(Utils.fprintf_list ~sep:", " pp_val) vl 
60 
 MBranch (g,hl) > 
61 
Format.fprintf fmt "@[<v 2>case(%a) {@,%a@,}@]" 
62 
pp_val g 
63 
(Utils.fprintf_list ~sep:"@," pp_branch) hl 
64  
65 
and pp_branch fmt (t, h) = 
66 
Format.fprintf fmt "@[<v 2>%s:@,%a@]" t (Utils.fprintf_list ~sep:"@," pp_instr) h 
67  
68 
and pp_instrs fmt il = 
69 
Format.fprintf fmt "@[<v 2>%a@]" (Utils.fprintf_list ~sep:"@," pp_instr) il 
70  
71 
type step_t = { 
72 
step_checks: (Location.t * value_t) list; 
73 
step_inputs: var_decl list; 
74 
step_outputs: var_decl list; 
75 
step_locals: var_decl list; 
76 
step_instrs: instr_t list; 
77 
step_asserts: value_t list; 
78 
} 
79  
80 
type static_call = top_decl * (Dimension.dim_expr list) 
81  
82 
type machine_t = { 
83 
mname: node_desc; 
84 
mmemory: var_decl list; 
85 
mcalls: (ident * static_call) list; (* map from stateful/stateless instance to node, no internals *) 
86 
minstances: (ident * static_call) list; (* submap of mcalls, from stateful instance to node *) 
87 
minit: instr_t list; 
88 
mstatic: var_decl list; (* static inputs only *) 
89 
mconst: instr_t list; (* assignments of node constant locals *) 
90 
mstep: step_t; 
91 
mspec: node_annot option; 
92 
mannot: expr_annot list; 
93 
} 
94  
95 
let pp_step fmt s = 
96 
Format.fprintf fmt "@[<v>inputs : %a@ outputs: %a@ locals : %a@ checks : %a@ instrs : @[%a@]@ asserts : @[%a@]@]@ " 
97 
(Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_inputs 
98 
(Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_outputs 
99 
(Utils.fprintf_list ~sep:", " Printers.pp_var) s.step_locals 
100 
(Utils.fprintf_list ~sep:", " (fun fmt (_, c) > pp_val fmt c)) s.step_checks 
101 
(Utils.fprintf_list ~sep:"@ " pp_instr) s.step_instrs 
102 
(Utils.fprintf_list ~sep:", " pp_val) s.step_asserts 
103  
104  
105 
let pp_static_call fmt (node, args) = 
106 
Format.fprintf fmt "%s<%a>" 
107 
(node_name node) 
108 
(Utils.fprintf_list ~sep:", " Dimension.pp_dimension) args 
109  
110 
let pp_machine fmt m = 
111 
Format.fprintf fmt 
112 
"@[<v 2>machine %s@ mem : %a@ instances: %a@ init : %a@ const : %a@ step :@ @[<v 2>%a@]@ @ spec : @[%t@]@ annot : @[%a@]@]@ " 
113 
m.mname.node_id 
114 
(Utils.fprintf_list ~sep:", " Printers.pp_var) m.mmemory 
115 
(Utils.fprintf_list ~sep:", " (fun fmt (o1, o2) > Format.fprintf fmt "(%s, %a)" o1 pp_static_call o2)) m.minstances 
116 
(Utils.fprintf_list ~sep:"@ " pp_instr) m.minit 
117 
(Utils.fprintf_list ~sep:"@ " pp_instr) m.mconst 
118 
pp_step m.mstep 
119 
(fun fmt > match m.mspec with  None > ()  Some spec > Printers.pp_spec fmt spec) 
120 
(Utils.fprintf_list ~sep:"@ " Printers.pp_expr_annot) m.mannot 
121  
122 
(* Returns the declared stateless status and the computed one. *) 
123 
let get_stateless_status m = 
124 
(m.mname.node_dec_stateless, Utils.desome m.mname.node_stateless) 
125  
126 
let is_input m id = 
127 
List.exists (fun o > o.var_id = id.var_id) m.mstep.step_inputs 
128  
129 
let is_output m id = 
130 
List.exists (fun o > o.var_id = id.var_id) m.mstep.step_outputs 
131  
132 
let is_memory m id = 
133 
List.exists (fun o > o.var_id = id.var_id) m.mmemory 
134  
135 
let conditional c t e = 
136 
MBranch(c, [ (tag_true, t); (tag_false, e) ]) 
137  
138 
let dummy_var_decl name typ = 
139 
{ 
140 
var_id = name; 
141 
var_orig = false; 
142 
var_dec_type = dummy_type_dec; 
143 
var_dec_clock = dummy_clock_dec; 
144 
var_dec_const = false; 
145 
var_dec_value = None; 
146 
var_type = typ; 
147 
var_clock = Clocks.new_ck (Clocks.Cvar Clocks.CSet_all) true; 
148 
var_loc = Location.dummy_loc 
149 
} 
150  
151 
let arrow_id = "_arrow" 
152  
153 
let arrow_typ = Types.new_ty Types.Tunivar 
154  
155 
let arrow_desc = 
156 
{ 
157 
node_id = arrow_id; 
158 
node_type = Type_predef.type_bin_poly_op; 
159 
node_clock = Clock_predef.ck_bin_univ; 
160 
node_inputs= [dummy_var_decl "_in1" arrow_typ; dummy_var_decl "_in2" arrow_typ]; 
161 
node_outputs= [dummy_var_decl "_out" arrow_typ]; 
162 
node_locals= []; 
163 
node_gencalls = []; 
164 
node_checks = []; 
165 
node_asserts = []; 
166 
node_stmts= []; 
167 
node_dec_stateless = false; 
168 
node_stateless = Some false; 
169 
node_spec = None; 
170 
node_annot = []; } 
171  
172 
let arrow_top_decl = 
173 
{ 
174 
top_decl_desc = Node arrow_desc; 
175 
top_decl_owner = Version.include_path; 
176 
top_decl_itf = false; 
177 
top_decl_loc = Location.dummy_loc 
178 
} 
179  
180 
let arrow_machine = 
181 
let state = "_first" in 
182 
let var_state = dummy_var_decl state (Types.new_ty Types.Tbool) in 
183 
let var_input1 = List.nth arrow_desc.node_inputs 0 in 
184 
let var_input2 = List.nth arrow_desc.node_inputs 1 in 
185 
let var_output = List.nth arrow_desc.node_outputs 0 in 
186 
{ 
187 
mname = arrow_desc; 
188 
mmemory = [var_state]; 
189 
mcalls = []; 
190 
minstances = []; 
191 
minit = [MStateAssign(var_state, Cst (const_of_bool true))]; 
192 
mconst = []; 
193 
mstatic = []; 
194 
mstep = { 
195 
step_inputs = arrow_desc.node_inputs; 
196 
step_outputs = arrow_desc.node_outputs; 
197 
step_locals = []; 
198 
step_checks = []; 
199 
step_instrs = [conditional (StateVar var_state) 
200 
[MStateAssign(var_state, Cst (const_of_bool false)); 
201 
MLocalAssign(var_output, LocalVar var_input1)] 
202 
[MLocalAssign(var_output, LocalVar var_input2)] ]; 
203 
step_asserts = []; 
204 
}; 
205 
mspec = None; 
206 
mannot = []; 
207 
} 
208  
209 
let new_instance = 
210 
let cpt = ref (1) in 
211 
fun caller callee tag > 
212 
begin 
213 
let o = 
214 
if Stateless.check_node callee then 
215 
node_name callee 
216 
else 
217 
Printf.sprintf "ni_%d" (incr cpt; !cpt) in 
218 
let o = 
219 
if !Options.ansi && is_generic_node callee 
220 
then Printf.sprintf "%s_inst_%d" o (Utils.position (fun e > e.expr_tag = tag) caller.node_gencalls) 
221 
else o in 
222 
o 
223 
end 
224  
225  
226 
(* translate_<foo> : node > context > <foo> > machine code/expression *) 
227 
(* the context contains m : state aka memory variables *) 
228 
(* si : initialization instructions *) 
229 
(* j : node aka machine instances *) 
230 
(* d : local variables *) 
231 
(* s : step instructions *) 
232 
let translate_ident node (m, si, j, d, s) id = 
233 
try (* id is a node var *) 
234 
let var_id = get_node_var id node in 
235 
if ISet.exists (fun v > v.var_id = id) m 
236 
then StateVar var_id 
237 
else LocalVar var_id 
238 
with Not_found > 
239 
try (* id is a constant *) 
240 
LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))) 
241 
with Not_found > 
242 
(* id is a tag *) 
243 
Cst (Const_tag id) 
244  
245 
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst = 
246 
match (Clocks.repr ck).cdesc with 
247 
 Con (ck1, cr, l) > 
248 
let id = Clocks.const_of_carrier cr in 
249 
control_on_clock node args ck1 (MBranch (translate_ident node args id, 
250 
[l, [inst]] )) 
251 
 _ > inst 
252  
253 
let rec join_branches hl1 hl2 = 
254 
match hl1, hl2 with 
255 
 [] , _ > hl2 
256 
 _ , [] > hl1 
257 
 (t1, h1)::q1, (t2, h2)::q2 > 
258 
if t1 < t2 then (t1, h1) :: join_branches q1 hl2 else 
259 
if t1 > t2 then (t2, h2) :: join_branches hl1 q2 
260 
else (t1, List.fold_right join_guards h1 h2) :: join_branches q1 q2 
261  
262 
and join_guards inst1 insts2 = 
263 
match inst1, insts2 with 
264 
 _ , [] > 
265 
[inst1] 
266 
 MBranch (x1, hl1), MBranch (x2, hl2) :: q when x1 = x2 > 
267 
MBranch (x1, join_branches (sort_handlers hl1) (sort_handlers hl2)) 
268 
:: q 
269 
 _ > inst1 :: insts2 
270  
271 
let join_guards_list insts = 
272 
List.fold_right join_guards insts [] 
273  
274 
(* specialize predefined (polymorphic) operators 
275 
wrt their instances, so that the C semantics 
276 
is preserved *) 
277 
let specialize_to_c expr = 
278 
match expr.expr_desc with 
279 
 Expr_appl (id, e, r) > 
280 
if List.exists (fun e > Types.is_bool_type e.expr_type) (expr_list_of_expr e) 
281 
then let id = 
282 
match id with 
283 
 "=" > "equi" 
284 
 "!=" > "xor" 
285 
 _ > id in 
286 
{ expr with expr_desc = Expr_appl (id, e, r) } 
287 
else expr 
288 
 _ > expr 
289  
290 
let specialize_op expr = 
291 
match !Options.output with 
292 
 "C" > specialize_to_c expr 
293 
 _ > expr 
294  
295 
let rec translate_expr ?(ite=false) node ((m, si, j, d, s) as args) expr = 
296 
let expr = specialize_op expr in 
297 
match expr.expr_desc with 
298 
 Expr_const v > Cst v 
299 
 Expr_ident x > translate_ident node args x 
300 
 Expr_array el > Array (List.map (translate_expr node args) el) 
301 
 Expr_access (t, i) > Access (translate_expr node args t, translate_expr node args (expr_of_dimension i)) 
302 
 Expr_power (e, n) > Power (translate_expr node args e, translate_expr node args (expr_of_dimension n)) 
303 
 Expr_tuple _ 
304 
 Expr_arrow _ 
305 
 Expr_fby _ 
306 
 Expr_pre _ > (Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError) 
307 
 Expr_when (e1, _, _) > translate_expr node args e1 
308 
 Expr_merge (x, _) > raise NormalizationError 
309 
 Expr_appl (id, e, _) when Basic_library.is_internal_fun id > 
310 
let nd = node_from_name id in 
311 
Fun (node_name nd, List.map (translate_expr node args) (expr_list_of_expr e)) 
312 
 Expr_ite (g,t,e) > ( 
313 
(* special treatment depending on the active backend. For horn backend, ite 
314 
are preserved in expression. While they are removed for C or Java 
315 
backends. *) 
316 
match !Options.output with 
317 
 "horn" > 
318 
Fun ("ite", [translate_expr node args g; translate_expr node args t; translate_expr node args e]) 
319 
 ("C"  "java") when ite > 
320 
Fun ("ite", [translate_expr node args g; translate_expr node args t; translate_expr node args e]) 
321 
 _ > 
322 
(Format.eprintf "option:%s@." !Options.output; Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError) 
323 
) 
324 
 _ > raise NormalizationError 
325  
326 
let translate_guard node args expr = 
327 
match expr.expr_desc with 
328 
 Expr_ident x > translate_ident node args x 
329 
 _ > (Format.eprintf "internal error: translate_guard %s %a@." node.node_id Printers.pp_expr expr;assert false) 
330  
331 
let rec translate_act node ((m, si, j, d, s) as args) (y, expr) = 
332 
match expr.expr_desc with 
333 
 Expr_ite (c, t, e) > let g = translate_guard node args c in 
334 
conditional g [translate_act node args (y, t)] 
335 
[translate_act node args (y, e)] 
336 
 Expr_merge (x, hl) > MBranch (translate_ident node args x, List.map (fun (t, h) > t, [translate_act node args (y, h)]) hl) 
337 
 _ > 
338 
MLocalAssign (y, translate_expr node args expr) 
339  
340 
let reset_instance node args i r c = 
341 
match r with 
342 
 None > [] 
343 
 Some r > let g = translate_guard node args r in 
344 
[control_on_clock node args c (conditional g [MReset i] [])] 
345  
346 
let translate_eq node ((m, si, j, d, s) as args) eq = 
347 
(* Format.eprintf "translate_eq %a with clock %a@." Printers.pp_node_eq eq Clocks.print_ck eq.eq_rhs.expr_clock; *) 
348 
match eq.eq_lhs, eq.eq_rhs.expr_desc with 
349 
 [x], Expr_arrow (e1, e2) > 
350 
let var_x = get_node_var x node in 
351 
let o = new_instance node arrow_top_decl eq.eq_rhs.expr_tag in 
352 
let c1 = translate_expr node args e1 in 
353 
let c2 = translate_expr node args e2 in 
354 
(m, 
355 
MReset o :: si, 
356 
Utils.IMap.add o (arrow_top_decl, []) j, 
357 
d, 
358 
(control_on_clock node args eq.eq_rhs.expr_clock (MStep ([var_x], o, [c1;c2]))) :: s) 
359 
 [x], Expr_pre e1 when ISet.mem (get_node_var x node) d > 
360 
let var_x = get_node_var x node in 
361 
(ISet.add var_x m, 
362 
si, 
363 
j, 
364 
d, 
365 
control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e1)) :: s) 
366 
 [x], Expr_fby (e1, e2) when ISet.mem (get_node_var x node) d > 
367 
let var_x = get_node_var x node in 
368 
(ISet.add var_x m, 
369 
MStateAssign (var_x, translate_expr node args e1) :: si, 
370 
j, 
371 
d, 
372 
control_on_clock node args eq.eq_rhs.expr_clock (MStateAssign (var_x, translate_expr node args e2)) :: s) 
373  
374 
 p , Expr_appl (f, arg, r) when not (Basic_library.is_internal_fun f) > 
375 
let var_p = List.map (fun v > get_node_var v node) p in 
376 
let el = expr_list_of_expr arg in 
377 
let vl = List.map (translate_expr node args) el in 
378 
let node_f = node_from_name f in 
379 
let call_f = 
380 
node_f, 
381 
NodeDep.filter_static_inputs (node_inputs node_f) el in 
382 
let o = new_instance node node_f eq.eq_rhs.expr_tag in 
383 
let env_cks = List.fold_right (fun arg cks > arg.expr_clock :: cks) el [eq.eq_rhs.expr_clock] in 
384 
let call_ck = Clock_calculus.compute_root_clock (Clock_predef.ck_tuple env_cks) in 
385 
(*Clocks.new_var true in 
386 
Clock_calculus.unify_imported_clock (Some call_ck) eq.eq_rhs.expr_clock eq.eq_rhs.expr_loc; 
387 
Format.eprintf "call %a: %a: %a@," Printers.pp_expr eq.eq_rhs Clocks.print_ck (Clock_predef.ck_tuple env_cks) Clocks.print_ck call_ck;*) 
388 
(m, 
389 
(if Stateless.check_node node_f then si else MReset o :: si), 
390 
Utils.IMap.add o call_f j, 
391 
d, 
392 
(if Stateless.check_node node_f 
393 
then [] 
394 
else reset_instance node args o r call_ck) @ 
395 
(control_on_clock node args call_ck (MStep (var_p, o, vl))) :: s) 
396  
397 
(* special treatment depending on the active backend. For horn backend, x = ite (g,t,e) 
398 
are preserved. While they are replaced as if g then x = t else x = e in C or Java 
399 
backends. *) 
400 
 [x], Expr_ite (c, t, e) 
401 
when (match !Options.output with  "horn" > true  "C"  "java"  _ > false) 
402 
> 
403 
let var_x = get_node_var x node in 
404 
(m, 
405 
si, 
406 
j, 
407 
d, 
408 
(control_on_clock node args eq.eq_rhs.expr_clock 
409 
(MLocalAssign (var_x, translate_expr node args eq.eq_rhs))::s) 
410 
) 
411  
412 
 [x], _ > ( 
413 
let var_x = get_node_var x node in 
414 
(m, si, j, d, 
415 
control_on_clock 
416 
node 
417 
args 
418 
eq.eq_rhs.expr_clock 
419 
(translate_act node args (var_x, eq.eq_rhs)) :: s 
420 
) 
421 
) 
422 
 _ > 
423 
begin 
424 
Format.eprintf "unsupported equation: %a@?" Printers.pp_node_eq eq; 
425 
assert false 
426 
end 
427  
428  
429  
430 
let constant_equations nd = 
431 
List.fold_right (fun vdecl eqs > 
432 
if vdecl.var_dec_const 
433 
then 
434 
{ eq_lhs = [vdecl.var_id]; 
435 
eq_rhs = Utils.desome vdecl.var_dec_value; 
436 
eq_loc = vdecl.var_loc 
437 
} :: eqs 
438 
else eqs) 
439 
nd.node_locals [] 
440  
441 
let translate_eqs node args eqs = 
442 
List.fold_right (fun eq args > translate_eq node args eq) eqs args;; 
443  
444 
let translate_decl nd sch = 
445 
(*Log.report ~level:1 (fun fmt > Printers.pp_node fmt nd);*) 
446  
447 
let sorted_eqs = Scheduling.sort_equations_from_schedule nd sch in 
448 
let constant_eqs = constant_equations nd in 
449 

450 
let init_args = ISet.empty, [], Utils.IMap.empty, List.fold_right (fun l > ISet.add l) nd.node_locals ISet.empty, [] in 
451 
(* memories, init instructions, node calls, local variables (including memories), step instrs *) 
452 
let m0, init0, j0, locals0, s0 = translate_eqs nd init_args constant_eqs in 
453 
assert (ISet.is_empty m0); 
454 
assert (init0 = []); 
455 
assert (Utils.IMap.is_empty j0); 
456 
let m, init, j, locals, s = translate_eqs nd (m0, init0, j0, locals0, s0) sorted_eqs in 
457 
let mmap = Utils.IMap.fold (fun i n res > (i, n)::res) j [] in 
458 
{ 
459 
mname = nd; 
460 
mmemory = ISet.elements m; 
461 
mcalls = mmap; 
462 
minstances = List.filter (fun (_, (n,_)) > not (Stateless.check_node n)) mmap; 
463 
minit = init; 
464 
mconst = s0; 
465 
mstatic = List.filter (fun v > v.var_dec_const) nd.node_inputs; 
466 
mstep = { 
467 
step_inputs = nd.node_inputs; 
468 
step_outputs = nd.node_outputs; 
469 
step_locals = ISet.elements (ISet.diff locals m); 
470 
step_checks = List.map (fun d > d.Dimension.dim_loc, translate_expr nd init_args (expr_of_dimension d)) nd.node_checks; 
471 
step_instrs = ( 
472 
(* special treatment depending on the active backend. For horn backend, 
473 
common branches are not merged while they are in C or Java 
474 
backends. *) 
475 
match !Options.output with 
476 
 "horn" > s 
477 
 "C"  "java"  _ > join_guards_list s 
478 
); 
479 
step_asserts = 
480 
let exprl = List.map (fun assert_ > assert_.assert_expr ) nd.node_asserts in 
481 
List.map (translate_expr nd init_args) exprl 
482 
; 
483 
}; 
484 
mspec = nd.node_spec; 
485 
mannot = nd.node_annot; 
486 
} 
487  
488 
(** takes the global declarations and the scheduling associated to each node *) 
489 
let translate_prog decls node_schs = 
490 
let nodes = get_nodes decls in 
491 
List.map 
492 
(fun decl > 
493 
let node = node_of_top decl in 
494 
let sch = (Utils.IMap.find node.node_id node_schs).Scheduling.schedule in 
495 
translate_decl node sch 
496 
) nodes 
497  
498 
let get_machine_opt name machines = 
499 
List.fold_left 
500 
(fun res m > 
501 
match res with 
502 
 Some _ > res 
503 
 None > if m.mname.node_id = name then Some m else None) 
504 
None machines 
505  
506 
let get_const_assign m id = 
507 
try 
508 
match (List.find (fun instr > match instr with MLocalAssign (v, _) > v == id  _ > false) m.mconst) with 
509 
 MLocalAssign (_, e) > e 
510 
 _ > assert false 
511 
with Not_found > assert false 
512  
513  
514 
let value_of_ident m id = 
515 
(* is is a state var *) 
516 
try 
517 
StateVar (List.find (fun v > v.var_id = id) m.mmemory) 
518 
with Not_found > 
519 
try (* id is a node var *) 
520 
LocalVar (get_node_var id m.mname) 
521 
with Not_found > 
522 
try (* id is a constant *) 
523 
LocalVar (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))) 
524 
with Not_found > 
525 
(* id is a tag *) 
526 
Cst (Const_tag id) 
527  
528 
let rec value_of_dimension m dim = 
529 
match dim.Dimension.dim_desc with 
530 
 Dimension.Dbool b > Cst (Const_tag (if b then Corelang.tag_true else Corelang.tag_false)) 
531 
 Dimension.Dint i > Cst (Const_int i) 
532 
 Dimension.Dident v > value_of_ident m v 
533 
 Dimension.Dappl (f, args) > Fun (f, List.map (value_of_dimension m) args) 
534 
 Dimension.Dite (i, t, e) > Fun ("ite", List.map (value_of_dimension m) [i; t; e]) 
535 
 Dimension.Dlink dim' > value_of_dimension m dim' 
536 
 _ > assert false 
537  
538 
let rec dimension_of_value value = 
539 
match value with 
540 
 Cst (Const_tag t) when t = Corelang.tag_true > Dimension.mkdim_bool Location.dummy_loc true 
541 
 Cst (Const_tag t) when t = Corelang.tag_false > Dimension.mkdim_bool Location.dummy_loc false 
542 
 Cst (Const_int i) > Dimension.mkdim_int Location.dummy_loc i 
543 
 LocalVar v > Dimension.mkdim_ident Location.dummy_loc v.var_id 
544 
 Fun (f, args) > Dimension.mkdim_appl Location.dummy_loc f (List.map dimension_of_value args) 
545 
 _ > assert false 
546  
547 
(* Local Variables: *) 
548 
(* compilecommand:"make C .." *) 
549 
(* End: *) 