Project

General

Profile

Download (7.89 KB) Statistics
| Branch: | Tag: | Revision:
1
open Format
2

    
3
open Machine_code_types
4
open Lustre_types
5
open Corelang
6
open Machine_code_common
7

    
8
(** All the pretty print functions common to the ada backend **)
9

    
10

    
11
(** Print a cleaned an identifier for ada exportation : Ada names must not start by an
12
    underscore and must not contain a double underscore 
13
   @param var name to be cleaned*)
14
let print_clean_ada_identifier fmt name =
15
  let base_size = String.length name in
16
  assert(base_size > 0);
17
  let rec remove_double_underscore s = function
18
    | i when i == String.length s - 1 -> s
19
    | i when String.get s i == '_' && String.get s (i+1) == '_' ->
20
        remove_double_underscore (sprintf "%s%s" (String.sub s 0 i) (String.sub s (i+1) (String.length s-i-1))) i
21
    | i -> remove_double_underscore s (i+1)
22
  in
23
  let name = remove_double_underscore name 0 in
24
  let prefix = if String.length name != base_size
25
                  || String.get name 0 == '_' then
26
                  "ada"
27
               else
28
                  ""
29
  in
30
  fprintf fmt "%s%s" prefix name
31

    
32

    
33
(* Package pretty print functions *)
34

    
35
(** Print the name of a package associated to a machine.
36
   @param fmt the formater to print on
37
   @param machine the machine
38
**)
39
let pp_package_name fmt node =
40
    fprintf fmt "%a" print_clean_ada_identifier node.node_id
41

    
42
(** Print the ada package introduction sentence it can be used for body and
43
declaration. Boolean parameter body should be true if it is a body delcaration.
44
   @param fmt the formater to print on
45
   @param fmt the formater to print on
46
   @param machine the machine
47
**)
48
let pp_begin_package body fmt machine =
49
  fprintf fmt "package %s%a is"
50
    (if body then "body " else "")
51
    pp_package_name machine.mname
52

    
53
(** Print the ada package conclusion sentence.
54
   @param fmt the formater to print on
55
   @param machine the machine
56
**)
57
let pp_end_package fmt machine =
58
  fprintf fmt "end %a" pp_package_name machine.mname
59

    
60
(** Print the access of an item from an other package.
61
   @param fmt the formater to print on
62
   @param package the package to use
63
   @param item the item which is accessed
64
**)
65
let pp_package_access fmt (package, item) =
66
  fprintf fmt "%t.%t" package item
67

    
68
(* Type pretty print functions *)
69

    
70
(** Print a type declaration
71
   @param fmt the formater to print on
72
   @param pp_name a format printer which print the type name
73
   @param pp_value a format printer which print the type definition
74
**)
75
let pp_type_decl fmt (pp_name, pp_definition) =
76
  fprintf fmt "type %t is %t" pp_name pp_definition
77

    
78
(** Print a private type declaration
79
   @param fmt the formater to print on
80
   @param pp_name a format printer which print the type name
81
**)
82
let pp_private_type_decl fmt pp_name =
83
  let pp_definition fmt = fprintf fmt "private" in
84
  pp_type_decl fmt (pp_name, pp_definition)
85

    
86
(** Print the type of the state variable.
87
   @param fmt the formater to print on
88
**)
89
let pp_state_type fmt =
90
  (* Type and variable names live in the same environement in Ada so name of
91
     this type and of the associated parameter : pp_state_name must be
92
     different *)
93
  fprintf fmt "TState"
94

    
95
(** Print the integer type name.
96
   @param fmt the formater to print on
97
**)
98
let pp_integer_type fmt = fprintf fmt "Integer"
99

    
100
(** Print the float type name.
101
   @param fmt the formater to print on
102
**)
103
let pp_float_type fmt = fprintf fmt "Float"
104

    
105
(** Print the boolean type name.
106
   @param fmt the formater to print on
107
**)
108
let pp_boolean_type fmt = fprintf fmt "Boolean"
109

    
110
(** Print the type of a variable.
111
   @param fmt the formater to print on
112
   @param id the variable
113
**)
114
let pp_var_type fmt id = 
115
  (match (Types.repr id.var_type).Types.tdesc with
116
    | Types.Tbasic Types.Basic.Tint -> pp_integer_type fmt
117
    | Types.Tbasic Types.Basic.Treal -> pp_float_type fmt
118
    | Types.Tbasic Types.Basic.Tbool -> pp_boolean_type fmt
119
    | _ -> eprintf "Type error : %a@." Types.print_ty id.var_type; assert false (*TODO*)
120
  )
121
  
122

    
123
(* Variable pretty print functions *)
124

    
125
(** Represent the possible mode for a type of a procedure parameter **)
126
type parameter_mode = NoMode | In | Out | InOut
127

    
128
(** Print a parameter_mode.
129
   @param fmt the formater to print on
130
   @param mode the modifier
131
**)
132
let pp_parameter_mode fmt mode =
133
  fprintf fmt "%s" (match mode with
134
                     | NoMode -> ""
135
                     | In     -> "in"
136
                     | Out    -> "out"
137
                     | InOut  -> "in out")
138

    
139
(** Print the name of the state variable.
140
   @param fmt the formater to print on
141
**)
142
let pp_state_name fmt =
143
  fprintf fmt "state"
144

    
145

    
146
(** Print the name of a variable.
147
   @param fmt the formater to print on
148
   @param id the variable
149
**)
150
let pp_var_name fmt id =
151
  fprintf fmt "%a" print_clean_ada_identifier id.var_id
152

    
153
(** Print a variable declaration
154
   @param mode input/output mode of the parameter
155
   @param pp_name a format printer wich print the variable name
156
   @param pp_type a format printer wich print the variable type
157
   @param fmt the formater to print on
158
   @param id the variable
159
**)
160
let pp_var_decl fmt (mode, pp_name, pp_type) =
161
  fprintf fmt "%t: %a%s%t"
162
    pp_name
163
    pp_parameter_mode mode
164
    (if mode = NoMode then "" else " ")
165
    pp_type
166

    
167
(** Print variable declaration for machine variable
168
   @param mode input/output mode of the parameter
169
   @param fmt the formater to print on
170
   @param id the variable
171
**)
172
let pp_machine_var_decl mode fmt id =
173
  let pp_name = function fmt -> pp_var_name fmt id in
174
  let pp_type = function fmt -> pp_var_type fmt id in
175
  pp_var_decl fmt (mode, pp_name, pp_type)
176

    
177
(** Print variable declaration for state variable
178
   @param fmt the formater to print on
179
   @param mode input/output mode of the parameter
180
**)
181
let pp_state_var_decl fmt mode =
182
  let pp_name = pp_state_name in
183
  let pp_type = pp_state_type in
184
  pp_var_decl fmt (mode, pp_name, pp_type)
185

    
186

    
187
(* Prototype pretty print functions *)
188

    
189
(** Print the name of the init procedure **)
190
let pp_init_procedure_name fmt = fprintf fmt "init"
191

    
192
(** Print the step of the init procedure **)
193
let pp_step_procedure_name fmt = fprintf fmt "step"
194

    
195
(** Print the reset of the init procedure **)
196
let pp_reset_procedure_name fmt = fprintf fmt "reset"
197

    
198
(** Print the clear of the init procedure **)
199
let pp_clear_procedure_name fmt = fprintf fmt "clear"
200

    
201
(** Print the prototype of a machine procedure. The first parameter is always
202
the state, state_modifier specify the modifier applying to it. The next
203
parameters are inputs and the last parameters are the outputs.
204
   @param fmt the formater to print on
205
   @param name the name of the procedure
206
   @param state_mode the input/output mode for the state parameter
207
   @param input list of the input parameter of the procedure
208
   @param output list of the output parameter of the procedure
209
**)
210
let pp_simple_prototype fmt (pp_name, state_mode, input, output) =
211
  fprintf fmt "procedure %t(@[<v>%a%t@[%a@]%t@[%a@])@]"
212
    pp_name
213
    pp_state_var_decl state_mode
214
    (Utils.pp_final_char_if_non_empty ";@," input)
215
    (Utils.fprintf_list ~sep:";@ " (pp_machine_var_decl In)) input
216
    (Utils.pp_final_char_if_non_empty ";@," output)
217
    (Utils.fprintf_list ~sep:";@ " (pp_machine_var_decl Out)) output
218

    
219
(** Print the prototype of the init procedure of a machine.
220
   @param fmt the formater to print on
221
   @param m the machine
222
**)
223
let pp_init_prototype fmt m =
224
  pp_simple_prototype fmt (pp_init_procedure_name, Out, m.mstatic, [])
225

    
226
(** Print the prototype of the step procedure of a machine.
227
   @param fmt the formater to print on
228
   @param m the machine
229
**)
230
let pp_step_prototype fmt m =
231
  pp_simple_prototype fmt (pp_step_procedure_name, InOut, m.mstep.step_inputs, m.mstep.step_outputs)
232

    
233
(** Print the prototype of the reset procedure of a machine.
234
   @param fmt the formater to print on
235
   @param m the machine
236
**)
237
let pp_reset_prototype fmt m =
238
  pp_simple_prototype fmt (pp_reset_procedure_name, InOut, m.mstatic, [])
239

    
240
(** Print the prototype of the clear procedure of a machine.
241
   @param fmt the formater to print on
242
   @param m the machine
243
**)
244
let pp_clear_prototype fmt m =
245
  pp_simple_prototype fmt (pp_clear_procedure_name, InOut, m.mstatic, [])
(5-5/6)