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 Format
|
13
|
open Lustre_types
|
14
|
open Machine_code_types
|
15
|
open Corelang
|
16
|
|
17
|
let get_machine = Machine_code_common.get_machine
|
18
|
|
19
|
let machine_reset_name id = id ^ "_reset"
|
20
|
let machine_step_name id = id ^ "_step"
|
21
|
let machine_stateless_name id = id ^ "_fun"
|
22
|
let pp_machine_reset_name fmt id = fprintf fmt "%s_reset" id
|
23
|
let pp_machine_step_name fmt id = fprintf fmt "%s_step" id
|
24
|
let pp_machine_stateless_name fmt id = fprintf fmt "%s_fun" id
|
25
|
|
26
|
let rec pp_type fmt t =
|
27
|
if Types.is_bool_type t then fprintf fmt "Bool" else
|
28
|
if Types.is_int_type t then fprintf fmt "Int" else
|
29
|
if Types.is_real_type t then fprintf fmt "Real" else
|
30
|
match (Types.repr t).Types.tdesc with
|
31
|
| Types.Tconst ty -> pp_print_string fmt ty
|
32
|
| Types.Tclock t -> pp_type fmt t
|
33
|
| Types.Tarray(dim,ty) -> fprintf fmt "(Array Int "; pp_type fmt ty; fprintf fmt ")"
|
34
|
| Types.Tstatic(d, ty)-> pp_type fmt ty
|
35
|
| Types.Tarrow _
|
36
|
| _ -> eprintf "internal error: pp_type %a@."
|
37
|
Types.print_ty t; assert false
|
38
|
|
39
|
let pp_decl_var fmt id =
|
40
|
fprintf fmt "(declare-var %s %a)"
|
41
|
id.var_id
|
42
|
pp_type id.var_type
|
43
|
|
44
|
(* let pp_var fmt id = pp_print_string fmt id.var_id *)
|
45
|
|
46
|
|
47
|
let pp_conj pp fmt l =
|
48
|
match l with
|
49
|
[] -> assert false
|
50
|
| [x] -> pp fmt x
|
51
|
| _ -> fprintf fmt "(and @[<v 0>%a@]@ )" (Utils.fprintf_list ~sep:"@ " pp) l
|
52
|
|
53
|
|
54
|
|
55
|
(********************************************************************************************)
|
56
|
(* Workaround to prevent the use of declared keywords as node name *)
|
57
|
(********************************************************************************************)
|
58
|
let registered_keywords = ["implies"]
|
59
|
|
60
|
let protect_kwd s =
|
61
|
if List.mem s registered_keywords then
|
62
|
"__" ^ s
|
63
|
else
|
64
|
s
|
65
|
|
66
|
let node_name n =
|
67
|
let name = node_name n in
|
68
|
protect_kwd name
|
69
|
|
70
|
|
71
|
let concat prefix x = if prefix = "" then x else prefix ^ "." ^ x
|
72
|
let rename f = (fun v -> {v with var_id = f v.var_id } )
|
73
|
let rename_machine p = rename (fun n -> concat p n)
|
74
|
let rename_machine_list p = List.map (rename_machine p)
|
75
|
|
76
|
let rename_current = rename (fun n -> n ^ "_c")
|
77
|
let rename_current_list = List.map rename_current
|
78
|
let rename_mid = rename (fun n -> n ^ "_m")
|
79
|
let rename_mid_list = List.map rename_mid
|
80
|
let rename_next = rename (fun n -> n ^ "_x")
|
81
|
let rename_next_list = List.map rename_next
|
82
|
|
83
|
|
84
|
let local_memory_vars machines machine =
|
85
|
rename_machine_list machine.mname.node_id machine.mmemory
|
86
|
|
87
|
let instances_memory_vars ?(without_arrow=false) machines machine =
|
88
|
let rec aux fst prefix m =
|
89
|
(
|
90
|
if not fst then (
|
91
|
(rename_machine_list (concat prefix m.mname.node_id) m.mmemory)
|
92
|
)
|
93
|
else []
|
94
|
) @
|
95
|
List.fold_left (fun accu (id, (n, _)) ->
|
96
|
let name = node_name n in
|
97
|
if without_arrow && name = "_arrow" then
|
98
|
accu
|
99
|
else
|
100
|
let machine_n = get_machine machines name in
|
101
|
( aux false (concat prefix
|
102
|
(if fst then id else concat m.mname.node_id id))
|
103
|
machine_n ) @ accu
|
104
|
) [] (m.minstances)
|
105
|
in
|
106
|
aux true machine.mname.node_id machine
|
107
|
|
108
|
(* Extract the arrows of a given node/machine *)
|
109
|
let arrow_vars machines machine : Lustre_types.var_decl list =
|
110
|
let rec aux fst prefix m =
|
111
|
List.fold_left (fun accu (id, (n, _)) ->
|
112
|
let name = node_name n in
|
113
|
if name = "_arrow" then
|
114
|
let arrow_machine = Machine_code_common.arrow_machine in
|
115
|
(rename_machine_list
|
116
|
(concat prefix (concat (if fst then id else concat m.mname.node_id id) "_arrow"))
|
117
|
arrow_machine.mmemory
|
118
|
) @ accu
|
119
|
else
|
120
|
let machine_n = get_machine machines name in
|
121
|
( aux false (concat prefix
|
122
|
(if fst then id else concat m.mname.node_id id))
|
123
|
machine_n ) @ accu
|
124
|
) [] (m.minstances)
|
125
|
in
|
126
|
aux true machine.mname.node_id machine
|
127
|
|
128
|
let full_memory_vars ?(without_arrow=false) machines machine =
|
129
|
(local_memory_vars machines machine)
|
130
|
@ (instances_memory_vars ~without_arrow machines machine)
|
131
|
|
132
|
let inout_vars machines m =
|
133
|
(rename_machine_list m.mname.node_id m.mstep.step_inputs)
|
134
|
@ (rename_machine_list m.mname.node_id m.mstep.step_outputs)
|
135
|
|
136
|
let step_vars machines m =
|
137
|
(inout_vars machines m)
|
138
|
@ (rename_current_list (full_memory_vars machines m))
|
139
|
@ (rename_next_list (full_memory_vars machines m))
|
140
|
|
141
|
let step_vars_m_x machines m =
|
142
|
(inout_vars machines m)
|
143
|
@ (rename_mid_list (full_memory_vars machines m))
|
144
|
@ (rename_next_list (full_memory_vars machines m))
|
145
|
|
146
|
let reset_vars machines m =
|
147
|
(rename_current_list (full_memory_vars machines m))
|
148
|
@ (rename_mid_list (full_memory_vars machines m))
|
149
|
|
150
|
let step_vars_c_m_x machines m =
|
151
|
(inout_vars machines m)
|
152
|
@ (rename_current_list (full_memory_vars machines m))
|
153
|
@ (rename_mid_list (full_memory_vars machines m))
|
154
|
@ (rename_next_list (full_memory_vars machines m))
|
155
|
|
156
|
|
157
|
(* Local Variables: *)
|
158
|
(* compile-command:"make -C ../.." *)
|
159
|
(* End: *)
|