Project

General

Profile

Download (14.1 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
(* Misc pretty print functions *)
12

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

    
34
(** Encapsulate a pretty print function to lower case its result when applied
35
   @param pp the pretty print function
36
   @param fmt the formatter
37
   @param arg the argument of the pp function
38
**)
39
let pp_lowercase pp fmt =
40
  let str = asprintf "%t" pp in
41
  fprintf fmt "%s" (String. lowercase_ascii str)
42

    
43
(** Print a filename by lowercasing the base and appending an extension.
44
   @param extension the extension to append to the package name
45
   @param fmt the formatter
46
   @param pp_name the file base name printer
47
**)
48
let pp_filename extension fmt pp_name =
49
  fprintf fmt "%t.%s"
50
    (pp_lowercase pp_name)
51
    extension
52

    
53

    
54
(* Package pretty print functions *)
55

    
56
(** Print the name of the arrow package.
57
   @param fmt the formater to print on
58
**)
59
let pp_arrow_package_name fmt = fprintf fmt "Arrow"
60

    
61
(** Print the name of a package associated to a node.
62
   @param fmt the formater to print on
63
   @param machine the machine
64
**)
65
let pp_package_name_from_node fmt node =
66
  if String.equal Arrow.arrow_id node.node_id then
67
      fprintf fmt "%t" pp_arrow_package_name
68
  else
69
      fprintf fmt "%a" pp_clean_ada_identifier node.node_id
70

    
71
(** Print the name of a package associated to a machine.
72
   @param fmt the formater to print on
73
   @param machine the machine
74
**)
75
let pp_package_name fmt machine =
76
  pp_package_name_from_node fmt machine.mname
77

    
78
(** Print the ada package introduction sentence it can be used for body and
79
declaration. Boolean parameter body should be true if it is a body delcaration.
80
   @param fmt the formater to print on
81
   @param fmt the formater to print on
82
   @param machine the machine
83
**)
84
let pp_begin_package body fmt machine =
85
  fprintf fmt "package %s%a is"
86
    (if body then "body " else "")
87
    pp_package_name machine
88

    
89
(** Print the ada package conclusion sentence.
90
   @param fmt the formater to print on
91
   @param machine the machine
92
**)
93
let pp_end_package fmt machine =
94
  fprintf fmt "end %a" pp_package_name machine
95

    
96
(** Print the access of an item from an other package.
97
   @param fmt the formater to print on
98
   @param package the package to use
99
   @param item the item which is accessed
100
**)
101
let pp_package_access fmt (package, item) =
102
  fprintf fmt "%t.%t" package item
103

    
104
(** Print the name of the main procedure.
105
   @param fmt the formater to print on
106
**)
107
let pp_main_procedure_name fmt =
108
  fprintf fmt "main"
109

    
110
(** Extract a node from an instance.
111
   @param instance the instance
112
**)
113
let extract_node instance =
114
  let (_, (node, _)) = instance in
115
  match node.top_decl_desc with
116
    | Node nd         -> nd
117
    | _ -> assert false (*TODO*)
118

    
119
(** Print a with statement to include a machine.
120
   @param fmt the formater to print on
121
   @param machine the machine
122
**)
123
let pp_with_machine fmt machine =
124
  fprintf fmt "private with %a" pp_package_name machine
125

    
126

    
127
(* Type pretty print functions *)
128

    
129
(** Print a type declaration
130
   @param fmt the formater to print on
131
   @param pp_name a format printer which print the type name
132
   @param pp_value a format printer which print the type definition
133
**)
134
let pp_type_decl fmt (pp_name, pp_definition) =
135
  fprintf fmt "type %t is %t" pp_name pp_definition
136

    
137
(** Print a limited private type declaration
138
   @param fmt the formater to print on
139
   @param pp_name a format printer which print the type name
140
**)
141
let pp_private_limited_type_decl fmt pp_name =
142
  let pp_definition fmt = fprintf fmt "limited private" in
143
  pp_type_decl fmt (pp_name, pp_definition)
144

    
145
(** Print the type of the state variable.
146
   @param fmt the formater to print on
147
**)
148
let pp_state_type fmt =
149
  (* Type and variable names live in the same environement in Ada so name of
150
     this type and of the associated parameter : pp_state_name must be
151
     different *)
152
  fprintf fmt "TState"
153

    
154
(** Print the integer type name.
155
   @param fmt the formater to print on
156
**)
157
let pp_integer_type fmt = fprintf fmt "Integer"
158

    
159
(** Print the float type name.
160
   @param fmt the formater to print on
161
**)
162
let pp_float_type fmt = fprintf fmt "Float"
163

    
164
(** Print the boolean type name.
165
   @param fmt the formater to print on
166
**)
167
let pp_boolean_type fmt = fprintf fmt "Boolean"
168

    
169
(** Print the type of a polymorphic type.
170
   @param fmt the formater to print on
171
   @param id the id of the polymorphic type
172
**)
173
let pp_polymorphic_type fmt id =
174
  fprintf fmt "T_%i" id
175

    
176
(** Print a type.
177
   @param fmt the formater to print on
178
   @param type the type
179
**)
180
let pp_type fmt typ = 
181
  (match (Types.repr typ).Types.tdesc with
182
    | Types.Tbasic Types.Basic.Tint  -> pp_integer_type fmt
183
    | Types.Tbasic Types.Basic.Treal -> pp_float_type fmt
184
    | Types.Tbasic Types.Basic.Tbool -> pp_boolean_type fmt
185
    | Types.Tunivar _                -> pp_polymorphic_type fmt typ.tid
186
    | Types.Tconst _                 -> eprintf "Tconst@."; assert false (*TODO*)
187
    | Types.Tclock _                 -> eprintf "Tclock@."; assert false (*TODO*)
188
    | Types.Tarrow _                 -> eprintf "Tarrow@."; assert false (*TODO*)
189
    | Types.Ttuple l                 -> eprintf "Ttuple %a @." (Utils.fprintf_list ~sep:" " Types.print_ty) l; assert false (*TODO*)
190
    | Types.Tenum _                  -> eprintf "Tenum@.";  assert false (*TODO*)
191
    | Types.Tstruct _                -> eprintf "Tstruct@.";assert false (*TODO*)
192
    | Types.Tarray _                 -> eprintf "Tarray@."; assert false (*TODO*)
193
    | Types.Tstatic _                -> eprintf "Tstatic@.";assert false (*TODO*)
194
    | Types.Tlink _                  -> eprintf "Tlink@.";  assert false (*TODO*)
195
    | Types.Tvar _                   -> eprintf "Tvar@.";   assert false (*TODO*)
196
    | _ -> eprintf "Type error : %a@." Types.print_ty typ; assert false (*TODO*)
197
  )
198

    
199
(** Print the type of a variable.
200
   @param fmt the formater to print on
201
   @param id the variable
202
**)
203
let pp_var_type fmt id = 
204
  pp_type fmt id.var_type
205

    
206
(** Extract all the inputs and outputs.
207
   @param machine the machine
208
   @return a list of all the var_decl of a macine
209
**)
210
let get_all_vars_machine m =
211
  m.mmemory@m.mstep.step_inputs@m.mstep.step_outputs@m.mstatic
212

    
213
(** Check if a type is polymorphic.
214
   @param typ the type
215
   @return true if its polymorphic
216
**)
217
let is_Tunivar typ = (Types.repr typ).tdesc == Types.Tunivar
218

    
219
(** Find all polymorphic type : Types.Tunivar in a machine.
220
   @param machine the machine
221
   @return a list of id corresponding to polymorphic type
222
**)
223
let find_all_polymorphic_type m =
224
  let vars = get_all_vars_machine m in
225
  let extract id = id.var_type.tid in
226
  let polymorphic_type_vars =
227
    List.filter (function x-> is_Tunivar x.var_type) vars in
228
  List.sort_uniq (-) (List.map extract polymorphic_type_vars)
229

    
230
(** Print a package name with polymorphic types specified.
231
   @param substitution correspondance between polymorphic type id and their instantiation
232
   @param fmt the formater to print on
233
   @param machine the machine
234
**)
235
let pp_package_name_with_polymorphic substitution fmt machine =
236
  let polymorphic_types = find_all_polymorphic_type machine in
237
  assert(List.length polymorphic_types = List.length substitution);
238
  let substituion = List.sort_uniq (fun x y -> fst x - fst y) substitution in
239
  assert(List.for_all2 (fun poly1 (poly2, _) -> poly1 = poly2)
240
            polymorphic_types substituion);
241
  let instantiated_types = snd (List.split substitution) in
242
  fprintf fmt "%a%t%a"
243
    pp_package_name machine
244
    (Utils.pp_final_char_if_non_empty "_" instantiated_types)
245
    (Utils.fprintf_list ~sep:"_" pp_type) instantiated_types
246

    
247

    
248
(* Variable pretty print functions *)
249

    
250
(** Represent the possible mode for a type of a procedure parameter **)
251
type parameter_mode = NoMode | In | Out | InOut
252

    
253
(** Print a parameter_mode.
254
   @param fmt the formater to print on
255
   @param mode the modifier
256
**)
257
let pp_parameter_mode fmt mode =
258
  fprintf fmt "%s" (match mode with
259
                     | NoMode -> ""
260
                     | In     -> "in"
261
                     | Out    -> "out"
262
                     | InOut  -> "in out")
263

    
264
(** Print the name of the state variable.
265
   @param fmt the formater to print on
266
**)
267
let pp_state_name fmt =
268
  fprintf fmt "state"
269

    
270

    
271
(** Print the name of a variable.
272
   @param fmt the formater to print on
273
   @param id the variable
274
**)
275
let pp_var_name fmt id =
276
  fprintf fmt "%a" pp_clean_ada_identifier id.var_id
277

    
278
(** Print a variable declaration
279
   @param mode input/output mode of the parameter
280
   @param pp_name a format printer wich print the variable name
281
   @param pp_type a format printer wich print the variable type
282
   @param fmt the formater to print on
283
   @param id the variable
284
**)
285
let pp_var_decl fmt (mode, pp_name, pp_type) =
286
  fprintf fmt "%t: %a%s%t"
287
    pp_name
288
    pp_parameter_mode mode
289
    (if mode = NoMode then "" else " ")
290
    pp_type
291

    
292
(** Print variable declaration for machine variable
293
   @param mode input/output mode of the parameter
294
   @param fmt the formater to print on
295
   @param id the variable
296
**)
297
let pp_machine_var_decl mode fmt id =
298
  let pp_name = function fmt -> pp_var_name fmt id in
299
  let pp_type = function fmt -> pp_var_type fmt id in
300
  pp_var_decl fmt (mode, pp_name, pp_type)
301

    
302
(** Print variable declaration for a local state variable
303
   @param fmt the formater to print on
304
   @param mode input/output mode of the parameter
305
**)
306
let pp_state_var_decl fmt mode =
307
  let pp_name = pp_state_name in
308
  let pp_type = pp_state_type in
309
  pp_var_decl fmt (mode, pp_name, pp_type)
310

    
311
(** Print the declaration of a state element of a machine.
312
   @param substitution correspondance between polymorphic type id and their instantiation
313
   @param name name of the variable
314
   @param fmt the formater to print on
315
   @param machine the machine
316
**)
317
let pp_node_state_decl substitution name fmt machine =
318
  let pp_package fmt = pp_package_name_with_polymorphic substitution fmt machine in
319
  let pp_type fmt = pp_package_access fmt (pp_package, pp_state_type) in
320
  let pp_name fmt = pp_clean_ada_identifier fmt name in
321
  pp_var_decl fmt (NoMode, pp_name, pp_type)
322

    
323

    
324
(* Prototype pretty print functions *)
325

    
326
(** Print the name of the reset procedure **)
327
let pp_reset_procedure_name fmt = fprintf fmt "reset"
328

    
329
(** Print the name of the step procedure **)
330
let pp_step_procedure_name fmt = fprintf fmt "step"
331

    
332
(** Print the name of the init procedure **)
333
let pp_init_procedure_name fmt = fprintf fmt "init"
334

    
335
(** Print the name of the clear procedure **)
336
let pp_clear_procedure_name fmt = fprintf fmt "clear"
337

    
338
(** Print the prototype of a procedure with non input/outputs
339
   @param fmt the formater to print on
340
   @param name the name of the procedure
341
**)
342
let pp_simple_prototype pp_name fmt =
343
  fprintf fmt "procedure %t" pp_name
344

    
345
(** Print the prototype of a machine procedure. The first parameter is always
346
the state, state_modifier specify the modifier applying to it. The next
347
parameters are inputs and the last parameters are the outputs.
348
   @param state_mode the input/output mode for the state parameter
349
   @param input list of the input parameter of the procedure
350
   @param output list of the output parameter of the procedure
351
   @param fmt the formater to print on
352
   @param name the name of the procedure
353
**)
354
let pp_base_prototype state_mode input output fmt pp_name =
355
  fprintf fmt "procedure %t(@[<v>%a%t@[%a@]%t@[%a@])@]"
356
    pp_name
357
    pp_state_var_decl state_mode
358
    (Utils.pp_final_char_if_non_empty ";@," input)
359
    (Utils.fprintf_list ~sep:";@ " (pp_machine_var_decl In)) input
360
    (Utils.pp_final_char_if_non_empty ";@," output)
361
    (Utils.fprintf_list ~sep:";@ " (pp_machine_var_decl Out)) output
362

    
363
(** Print the prototype of the step procedure of a machine.
364
   @param m the machine
365
   @param fmt the formater to print on
366
   @param pp_name name function printer
367
**)
368
let pp_step_prototype m fmt =
369
  pp_base_prototype InOut m.mstep.step_inputs m.mstep.step_outputs fmt pp_step_procedure_name
370

    
371
(** Print the prototype of the reset procedure of a machine.
372
   @param m the machine
373
   @param fmt the formater to print on
374
   @param pp_name name function printer
375
**)
376
let pp_reset_prototype m fmt =
377
  pp_base_prototype InOut m.mstatic [] fmt pp_reset_procedure_name
378

    
379
(** Print the prototype of the init procedure of a machine.
380
   @param m the machine
381
   @param fmt the formater to print on
382
   @param pp_name name function printer
383
**)
384
let pp_init_prototype m fmt =
385
  pp_base_prototype Out m.mstatic [] fmt pp_init_procedure_name
386

    
387
(** Print the prototype of the clear procedure of a machine.
388
   @param m the machine
389
   @param fmt the formater to print on
390
   @param pp_name name function printer
391
**)
392
let pp_clear_prototype m fmt =
393
  pp_base_prototype InOut m.mstatic [] fmt pp_clear_procedure_name
394

    
395

    
396
(* Procedure pretty print functions *)
397

    
398
(** Print the definition of a procedure
399
   @param pp_name the procedure name printer
400
   @param pp_prototype the prototype printer
401
   @param pp_instr local var printer
402
   @param pp_instr instruction printer
403
   @param fmt the formater to print on
404
   @param locals locals var list
405
   @param instrs instructions list
406
**)
407
let pp_procedure_definition pp_name pp_prototype pp_local pp_instr fmt (locals, instrs) =
408
  fprintf fmt "@[<v>%t is%t@[<v>%a%t@]@,begin@,  @[<v>%a%t@]@,end %t@]"
409
    pp_prototype
410
    (Utils.pp_final_char_if_non_empty "@,  " locals)
411
    (Utils.fprintf_list ~sep:";@," pp_local) locals
412
    (Utils.pp_final_char_if_non_empty ";" locals)
413
    (Utils.fprintf_list ~sep:";@," pp_instr) instrs
414
    (Utils.pp_final_char_if_non_empty ";" instrs)
415
    pp_name
(5-5/6)