Project

General

Profile

Download (5.4 KB) Statistics
| Branch: | Tag: | Revision:
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

    
21
let machine_step_name id = id ^ "_step"
22

    
23
let machine_stateless_name id = id ^ "_fun"
24

    
25
let pp_machine_reset_name fmt id = fprintf fmt "%s_reset" id
26

    
27
let pp_machine_step_name fmt id = fprintf fmt "%s_step" id
28

    
29
let pp_machine_stateless_name fmt id = fprintf fmt "%s_fun" id
30

    
31
let rec pp_type fmt t =
32
  if Types.is_bool_type t then fprintf fmt "Bool"
33
  else if Types.is_int_type t then fprintf fmt "Int"
34
  else if Types.is_real_type t then fprintf fmt "Real"
35
  else
36
    match (Types.repr t).Types.tdesc with
37
    | Types.Tconst ty ->
38
      pp_print_string fmt ty
39
    | Types.Tclock t ->
40
      pp_type fmt t
41
    | Types.Tarray (_, ty) ->
42
      fprintf fmt "(Array Int ";
43
      pp_type fmt ty;
44
      fprintf fmt ")"
45
    | Types.Tstatic (_, ty) ->
46
      pp_type fmt ty
47
    | Types.Tarrow _ | _ ->
48
      eprintf "internal error: pp_type %a@." Types.print_ty t;
49
      assert false
50

    
51
let pp_decl_var fmt id =
52
  fprintf fmt "(declare-var %s %a)" id.var_id pp_type id.var_type
53

    
54
(* let pp_var fmt id = pp_print_string fmt id.var_id *)
55

    
56
let pp_conj pp fmt l =
57
  match l with
58
  | [] ->
59
    assert false
60
  | [ x ] ->
61
    pp fmt x
62
  | _ ->
63
    fprintf fmt "(and @[<v 0>%a@]@ )" (Utils.fprintf_list ~sep:"@ " pp) l
64

    
65
(********************************************************************************************)
66
(* Workaround to prevent the use of declared keywords as node name *)
67
(********************************************************************************************)
68
let registered_keywords = [ "implies" ]
69

    
70
let protect_kwd s = if List.mem s registered_keywords then "__" ^ s else s
71

    
72
let node_name n =
73
  let name = node_name n in
74
  protect_kwd name
75

    
76
let concat prefix x = if prefix = "" then x else prefix ^ "." ^ x
77

    
78
let rename f v = { v with var_id = f v.var_id }
79

    
80
let rename_machine p = rename (fun n -> concat p n)
81

    
82
let rename_machine_list p = List.map (rename_machine p)
83

    
84
let rename_current = rename (fun n -> n ^ "_c")
85

    
86
let rename_current_list = List.map rename_current
87

    
88
let rename_mid = rename (fun n -> n ^ "_m")
89

    
90
let rename_mid_list = List.map rename_mid
91

    
92
let rename_next = rename (fun n -> n ^ "_x")
93

    
94
let rename_next_list = List.map rename_next
95

    
96
let local_memory_vars machine =
97
  rename_machine_list machine.mname.node_id machine.mmemory
98

    
99
let instances_memory_vars ?(without_arrow = false) machines machine =
100
  let rec aux fst prefix m =
101
    (if not fst then
102
     rename_machine_list (concat prefix m.mname.node_id) m.mmemory
103
    else [])
104
    @ List.fold_left
105
        (fun accu (id, (n, _)) ->
106
          let name = node_name n in
107
          if without_arrow && name = "_arrow" then accu
108
          else
109
            let machine_n = get_machine machines name in
110
            aux false
111
              (concat prefix (if fst then id else concat m.mname.node_id id))
112
              machine_n
113
            @ accu)
114
        [] m.minstances
115
  in
116
  aux true machine.mname.node_id machine
117

    
118
(* Extract the arrows of a given node/machine *)
119
let arrow_vars machines machine : Lustre_types.var_decl list =
120
  let rec aux fst prefix m =
121
    List.fold_left
122
      (fun accu (id, (n, _)) ->
123
        let name = node_name n in
124
        if name = "_arrow" then
125
          let arrow_machine = Machine_code_common.arrow_machine in
126
          rename_machine_list
127
            (concat prefix
128
               (concat (if fst then id else concat m.mname.node_id id) "_arrow"))
129
            arrow_machine.mmemory
130
          @ accu
131
        else
132
          let machine_n = get_machine machines name in
133
          aux false
134
            (concat prefix (if fst then id else concat m.mname.node_id id))
135
            machine_n
136
          @ accu)
137
      [] m.minstances
138
  in
139
  aux true machine.mname.node_id machine
140

    
141
let full_memory_vars ?(without_arrow = false) machines machine =
142
  local_memory_vars machine
143
  @ instances_memory_vars ~without_arrow machines machine
144

    
145
let inout_vars m =
146
  rename_machine_list m.mname.node_id m.mstep.step_inputs
147
  @ rename_machine_list m.mname.node_id m.mstep.step_outputs
148

    
149
let step_vars machines m =
150
  inout_vars m
151
  @ rename_current_list (full_memory_vars machines m)
152
  @ rename_next_list (full_memory_vars machines m)
153

    
154
let step_vars_m_x machines m =
155
  inout_vars m
156
  @ rename_mid_list (full_memory_vars machines m)
157
  @ rename_next_list (full_memory_vars machines m)
158

    
159
let reset_vars machines m =
160
  rename_current_list (full_memory_vars machines m)
161
  @ rename_mid_list (full_memory_vars machines m)
162

    
163
let step_vars_c_m_x machines m =
164
  inout_vars m
165
  @ rename_current_list (full_memory_vars machines m)
166
  @ rename_mid_list (full_memory_vars machines m)
167
  @ rename_next_list (full_memory_vars machines m)
168

    
169
(* Local Variables: *)
170
(* compile-command:"make -C ../.." *)
171
(* End: *)
(3-3/5)