Project

General

Profile

Download (28.8 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
(** Exception for unsupported features in Ada backend **)
9
exception Ada_not_supported of string
10

    
11
(** All the pretty print and aux functions common to the ada backend **)
12

    
13
(* Misc pretty print functions *)
14

    
15
let is_machine_statefull m = not m.mname.node_dec_stateless
16

    
17
let ada_supported_funs =
18
  [("sin", ("Ada.Numerics.Elementary_Functions", "Sin"));
19
   ("cos", ("Ada.Numerics.Elementary_Functions", "Cos"));
20
   ("tan", ("Ada.Numerics.Elementary_Functions", "Tan"))]
21

    
22
let is_builtin_fun ident =
23
  List.mem ident Basic_library.internal_funs ||
24
    List.mem_assoc ident ada_supported_funs
25

    
26
(** Print a cleaned an identifier for ada exportation : Ada names must not start by an
27
    underscore and must not contain a double underscore
28
   @param var name to be cleaned*)
29
let pp_clean_ada_identifier fmt name =
30
  let reserved_words = ["abort"; "else"; "new"; "return";
31
                        "abs"; "elsif"; "not"; "reverse"; "abstract"; "end";
32
                        "null"; "accept"; "entry"; "select"; "access";
33
                        "exception"; "of"; "separate"; "aliased"; "exit";
34
                        "or"; "some"; "all"; "others"; "subtype"; "and";
35
                        "for"; "out"; "synchronized"; "array"; "function";
36
                        "overriding"; "at"; "tagged"; "generic"; "package";
37
                        "task"; "begin"; "goto"; "pragma"; "terminate";
38
                        "body"; "private"; "then"; "if"; "procedure"; "type";
39
                        "case"; "in"; "protected"; "constant"; "interface";
40
                        "until"; "is"; "raise"; "use"; "declare"; "	range";
41
                        "delay"; "limited"; "record"; "when"; "delta"; "loop";
42
                        "rem"; "while"; "digits"; "renames"; "with"; "do";
43
                        "mod"; "requeue"; "xor"] in
44
  let base_size = String.length name in
45
  assert(base_size > 0);
46
  let rec remove_double_underscore s = function
47
    | i when i == String.length s - 1 -> s
48
    | i when String.get s i == '_' && String.get s (i+1) == '_' ->
49
        remove_double_underscore (sprintf "%s%s" (String.sub s 0 i) (String.sub s (i+1) (String.length s-i-1))) i
50
    | i -> remove_double_underscore s (i+1)
51
  in
52
  let name = if String.get name (base_size-1) == '_' then name^"ada" else name in
53
  let name = remove_double_underscore name 0 in
54
  let prefix = if String.length name != base_size
55
                  || String.get name 0 == '_' 
56
                  || List.exists (String.equal (String.lowercase_ascii name)) reserved_words then
57
                  "ada"
58
               else
59
                  ""
60
  in
61
  fprintf fmt "%s%s" prefix name
62

    
63
(** Encapsulate a pretty print function to lower case its result when applied
64
   @param pp the pretty print function
65
   @param fmt the formatter
66
   @param arg the argument of the pp function
67
**)
68
let pp_lowercase pp fmt =
69
  let str = asprintf "%t" pp in
70
  fprintf fmt "%s" (String. lowercase_ascii str)
71

    
72
(** Print a filename by lowercasing the base and appending an extension.
73
   @param extension the extension to append to the package name
74
   @param fmt the formatter
75
   @param pp_name the file base name printer
76
**)
77
let pp_filename extension fmt pp_name =
78
  fprintf fmt "%t.%s"
79
    (pp_lowercase pp_name)
80
    extension
81

    
82

    
83
(* Package pretty print functions *)
84

    
85
(** Print the name of the arrow package.
86
   @param fmt the formater to print on
87
**)
88
let pp_arrow_package_name fmt = fprintf fmt "Arrow"
89

    
90
(** Print the name of a package associated to a node.
91
   @param fmt the formater to print on
92
   @param machine the machine
93
**)
94
let pp_package_name_from_node fmt node =
95
  if String.equal Arrow.arrow_id node.node_id then
96
      fprintf fmt "%t" pp_arrow_package_name
97
  else
98
      fprintf fmt "%a" pp_clean_ada_identifier node.node_id
99

    
100
(** Print the name of a package associated to a machine.
101
   @param fmt the formater to print on
102
   @param machine the machine
103
**)
104
let pp_package_name fmt machine =
105
  pp_package_name_from_node fmt machine.mname
106

    
107
(** Print the ada package introduction sentence it can be used for body and
108
declaration. Boolean parameter body should be true if it is a body delcaration.
109
   @param fmt the formater to print on
110
   @param fmt the formater to print on
111
   @param machine the machine
112
**)
113
let pp_begin_package body fmt machine =
114
  fprintf fmt "package %s%a is"
115
    (if body then "body " else "")
116
    pp_package_name machine
117

    
118
(** Print the ada package conclusion sentence.
119
   @param fmt the formater to print on
120
   @param machine the machine
121
**)
122
let pp_end_package fmt machine =
123
  fprintf fmt "end %a" pp_package_name machine
124

    
125
(** Print the access of an item from an other package.
126
   @param fmt the formater to print on
127
   @param package the package to use
128
   @param item the item which is accessed
129
**)
130
let pp_package_access fmt (package, item) =
131
  fprintf fmt "%t.%t" package item
132

    
133
(** Print the name of the main procedure.
134
   @param fmt the formater to print on
135
**)
136
let pp_main_procedure_name fmt =
137
  fprintf fmt "ada_main"
138

    
139
(** Print a with statement to include a package.
140
   @param fmt the formater to print on
141
   @param pp_pakage_name the package name printer
142
**)
143
let pp_private_with fmt pp_pakage_name =
144
  fprintf fmt "private with %t" pp_pakage_name
145

    
146
(** Print a with statement to include a package.
147
   @param fmt the formater to print on
148
   @param name the package name
149
**)
150
let pp_with fmt name =
151
  fprintf fmt "with %s" name
152

    
153
(** Print a with statement to include a machine.
154
   @param fmt the formater to print on
155
   @param machine the machine
156
**)
157
let pp_with_machine fmt machine =
158
  fprintf fmt "private with %a" pp_package_name machine
159

    
160
(** Extract a node from an instance.
161
   @param instance the instance
162
**)
163
let extract_node instance =
164
  let (_, (node, _)) = instance in
165
  match node.top_decl_desc with
166
    | Node nd         -> nd
167
    | _ -> assert false (*TODO*)
168

    
169
(** Extract from a machine list the one corresponding to the given instance.
170
      assume that the machine is in the list.
171
   @param machines list of all machines
172
   @param instance instance of a machine
173
   @return the machine corresponding to hte given instance
174
**)
175
let get_machine machines instance =
176
    let id = (extract_node instance).node_id in
177
    try
178
      List.find (function m -> m.mname.node_id=id) machines
179
    with
180
      Not_found -> assert false
181

    
182

    
183
(* Type pretty print functions *)
184

    
185
(** Print a type declaration
186
   @param fmt the formater to print on
187
   @param pp_name a format printer which print the type name
188
   @param pp_value a format printer which print the type definition
189
**)
190
let pp_type_decl fmt (pp_name, pp_definition) =
191
  fprintf fmt "type %t is %t" pp_name pp_definition
192

    
193
(** Print a limited private type declaration
194
   @param fmt the formater to print on
195
   @param pp_name a format printer which print the type name
196
**)
197
let pp_private_limited_type_decl fmt pp_name =
198
  let pp_definition fmt = fprintf fmt "limited private" in
199
  pp_type_decl fmt (pp_name, pp_definition)
200

    
201
(** Print the type of the state variable.
202
   @param fmt the formater to print on
203
**)
204
let pp_state_type fmt =
205
  (* Type and variable names live in the same environement in Ada so name of
206
     this type and of the associated parameter : pp_state_name must be
207
     different *)
208
  fprintf fmt "TState"
209

    
210
(** Print the integer type name.
211
   @param fmt the formater to print on
212
**)
213
let pp_integer_type fmt = fprintf fmt "Integer"
214

    
215
(** Print the float type name.
216
   @param fmt the formater to print on
217
**)
218
let pp_float_type fmt = fprintf fmt "Float"
219

    
220
(** Print the boolean type name.
221
   @param fmt the formater to print on
222
**)
223
let pp_boolean_type fmt = fprintf fmt "Boolean"
224

    
225
(** Print the type of a polymorphic type.
226
   @param fmt the formater to print on
227
   @param id the id of the polymorphic type
228
**)
229
let pp_polymorphic_type fmt id =
230
  fprintf fmt "T_%i" id
231

    
232
(** Print a type.
233
   @param fmt the formater to print on
234
   @param type the type
235
**)
236
let pp_type fmt typ = 
237
  (match (Types.repr typ).Types.tdesc with
238
    | Types.Tbasic Types.Basic.Tint  -> pp_integer_type fmt
239
    | Types.Tbasic Types.Basic.Treal -> pp_float_type fmt
240
    | Types.Tbasic Types.Basic.Tbool -> pp_boolean_type fmt
241
    | Types.Tunivar _                -> pp_polymorphic_type fmt typ.tid
242
    | Types.Tbasic _                 -> eprintf "Tbasic@."; assert false (*TODO*)
243
    | Types.Tconst _                 -> eprintf "Tconst@."; assert false (*TODO*)
244
    | Types.Tclock _                 -> eprintf "Tclock@."; assert false (*TODO*)
245
    | Types.Tarrow _                 -> eprintf "Tarrow@."; assert false (*TODO*)
246
    | Types.Ttuple l                 -> eprintf "Ttuple %a @." (Utils.fprintf_list ~sep:" " Types.print_ty) l; assert false (*TODO*)
247
    | Types.Tenum _                  -> eprintf "Tenum@.";  assert false (*TODO*)
248
    | Types.Tstruct _                -> eprintf "Tstruct@.";assert false (*TODO*)
249
    | Types.Tarray _                 -> eprintf "Tarray@."; assert false (*TODO*)
250
    | Types.Tstatic _                -> eprintf "Tstatic@.";assert false (*TODO*)
251
    | Types.Tlink _                  -> eprintf "Tlink@.";  assert false (*TODO*)
252
    | Types.Tvar _                   -> eprintf "Tvar@.";   assert false (*TODO*)
253
    | _ -> eprintf "Type error : %a@." Types.print_ty typ; assert false (*TODO*)
254
  )
255

    
256

    
257
(** Test if two types are the same.
258
   @param typ1 the first type
259
   @param typ2 the second type
260
**)
261
let pp_eq_type typ1 typ2 = 
262
  let get_basic typ = match (Types.repr typ).Types.tdesc with
263
    | Types.Tbasic Types.Basic.Tint -> Types.Basic.Tint
264
    | Types.Tbasic Types.Basic.Treal -> Types.Basic.Treal
265
    | Types.Tbasic Types.Basic.Tbool -> Types.Basic.Tbool
266
    | _ -> assert false (*TODO*)
267
  in
268
  get_basic typ1 = get_basic typ2
269

    
270

    
271
(** Print the type of a variable.
272
   @param fmt the formater to print on
273
   @param id the variable
274
**)
275
let pp_var_type fmt id = 
276
  pp_type fmt id.var_type
277

    
278
(** Extract all the inputs and outputs.
279
   @param machine the machine
280
   @return a list of all the var_decl of a macine
281
**)
282
let get_all_vars_machine m =
283
  m.mmemory@m.mstep.step_inputs@m.mstep.step_outputs@m.mstatic
284

    
285
(** Check if a type is polymorphic.
286
   @param typ the type
287
   @return true if its polymorphic
288
**)
289
let is_Tunivar typ = (Types.repr typ).tdesc == Types.Tunivar
290

    
291
(** Find all polymorphic type : Types.Tunivar in a machine.
292
   @param machine the machine
293
   @return a list of id corresponding to polymorphic type
294
**)
295
let find_all_polymorphic_type m =
296
  let vars = get_all_vars_machine m in
297
  let extract id = id.var_type.tid in
298
  let polymorphic_type_vars =
299
    List.filter (function x-> is_Tunivar x.var_type) vars in
300
  List.sort_uniq (-) (List.map extract polymorphic_type_vars)
301

    
302
(** Print a package name with polymorphic types specified.
303
   @param substitution correspondance between polymorphic type id and their instantiation
304
   @param fmt the formater to print on
305
   @param machine the machine
306
**)
307
let pp_package_name_with_polymorphic substitution fmt machine =
308
  let polymorphic_types = find_all_polymorphic_type machine in
309
  assert(List.length polymorphic_types = List.length substitution);
310
  let substituion = List.sort_uniq (fun x y -> fst x - fst y) substitution in
311
  assert(List.for_all2 (fun poly1 (poly2, _) -> poly1 = poly2)
312
            polymorphic_types substituion);
313
  let instantiated_types = snd (List.split substitution) in
314
  fprintf fmt "%a%t%a"
315
    pp_package_name machine
316
    (Utils.pp_final_char_if_non_empty "_" instantiated_types)
317
    (Utils.fprintf_list ~sep:"_" pp_type) instantiated_types
318

    
319

    
320
(* Variable pretty print functions *)
321

    
322
(** Represent the possible mode for a type of a procedure parameter **)
323
type parameter_mode = NoMode | In | Out | InOut
324

    
325
(** Print a parameter_mode.
326
   @param fmt the formater to print on
327
   @param mode the modifier
328
**)
329
let pp_parameter_mode fmt mode =
330
  fprintf fmt "%s" (match mode with
331
                     | NoMode -> ""
332
                     | In     -> "in"
333
                     | Out    -> "out"
334
                     | InOut  -> "in out")
335

    
336
(** Print the name of the state variable.
337
   @param fmt the formater to print on
338
**)
339
let pp_state_name fmt =
340
  fprintf fmt "state"
341

    
342

    
343
(** Print the name of a variable.
344
   @param fmt the formater to print on
345
   @param id the variable
346
**)
347
let pp_var_name fmt id =
348
  fprintf fmt "%a" pp_clean_ada_identifier id.var_id
349

    
350
(** Print the complete name of variable.
351
   @param m the machine to check if it is memory
352
   @param fmt the formater to print on
353
   @param var the variable
354
**)
355
let pp_access_var m fmt var =
356
  if is_memory m var then
357
    fprintf fmt "%t.%a" pp_state_name pp_var_name var
358
  else
359
    pp_var_name fmt var
360

    
361
(** Print a variable declaration
362
   @param mode input/output mode of the parameter
363
   @param pp_name a format printer wich print the variable name
364
   @param pp_type a format printer wich print the variable type
365
   @param fmt the formater to print on
366
   @param id the variable
367
**)
368
let pp_var_decl fmt (mode, pp_name, pp_type) =
369
  fprintf fmt "%t: %a%s%t"
370
    pp_name
371
    pp_parameter_mode mode
372
    (if mode = NoMode then "" else " ")
373
    pp_type
374

    
375
(** Print variable declaration for machine variable
376
   @param mode input/output mode of the parameter
377
   @param fmt the formater to print on
378
   @param id the variable
379
**)
380
let pp_machine_var_decl mode fmt id =
381
  let pp_name = function fmt -> pp_var_name fmt id in
382
  let pp_type = function fmt -> pp_var_type fmt id in
383
  pp_var_decl fmt (mode, pp_name, pp_type)
384

    
385
(** Print variable declaration for a local state variable
386
   @param fmt the formater to print on
387
   @param mode input/output mode of the parameter
388
**)
389
let pp_state_var_decl fmt mode =
390
  let pp_name = pp_state_name in
391
  let pp_type = pp_state_type in
392
  pp_var_decl fmt (mode, pp_name, pp_type)
393

    
394
(** Print the declaration of a state element of a machine.
395
   @param substitution correspondance between polymorphic type id and their instantiation
396
   @param name name of the variable
397
   @param fmt the formater to print on
398
   @param machine the machine
399
**)
400
let pp_node_state_decl substitution name fmt machine =
401
  let pp_package fmt = pp_package_name_with_polymorphic substitution fmt machine in
402
  let pp_type fmt = pp_package_access fmt (pp_package, pp_state_type) in
403
  let pp_name fmt = pp_clean_ada_identifier fmt name in
404
  pp_var_decl fmt (NoMode, pp_name, pp_type)
405

    
406

    
407
(* Prototype pretty print functions *)
408

    
409
(** Print the name of the reset procedure **)
410
let pp_reset_procedure_name fmt = fprintf fmt "reset"
411

    
412
(** Print the name of the step procedure **)
413
let pp_step_procedure_name fmt = fprintf fmt "step"
414

    
415
(** Print the name of the init procedure **)
416
let pp_init_procedure_name fmt = fprintf fmt "init"
417

    
418
(** Print the name of the clear procedure **)
419
let pp_clear_procedure_name fmt = fprintf fmt "clear"
420

    
421
(** Print the prototype of a procedure with non input/outputs
422
   @param fmt the formater to print on
423
   @param name the name of the procedure
424
**)
425
let pp_simple_prototype pp_name fmt =
426
  fprintf fmt "procedure %t" pp_name
427

    
428
(** Print the prototype of a machine procedure. The first parameter is always
429
the state, state_modifier specify the modifier applying to it. The next
430
parameters are inputs and the last parameters are the outputs.
431
   @param state_mode_opt None if no state parameter required and some input/output mode for it else
432
   @param input list of the input parameter of the procedure
433
   @param output list of the output parameter of the procedure
434
   @param fmt the formater to print on
435
   @param name the name of the procedure
436
**)
437
let pp_base_prototype state_mode_opt input output fmt pp_name =
438
  let pp_var_decl_state fmt = match state_mode_opt with
439
    | None -> fprintf fmt ""
440
    | Some state_mode -> fprintf fmt "%a" pp_state_var_decl state_mode
441
  in
442
  fprintf fmt "procedure %t(@[<v>%t%t@[%a@]%t@[%a@])@]"
443
    pp_name
444
    pp_var_decl_state
445
    (fun fmt -> if state_mode_opt != None && input!=[] then
446
      fprintf fmt ";@," else fprintf fmt "")
447
    (Utils.fprintf_list ~sep:";@ " (pp_machine_var_decl In)) input
448
    (fun fmt -> if (state_mode_opt != None || input!=[]) && output != [] then
449
      fprintf fmt ";@," else fprintf fmt "")
450
    (Utils.fprintf_list ~sep:";@ " (pp_machine_var_decl Out)) output
451

    
452
(** Print the prototype of the step procedure of a machine.
453
   @param m the machine
454
   @param fmt the formater to print on
455
   @param pp_name name function printer
456
**)
457
let pp_step_prototype m fmt =
458
  let state_mode = if is_machine_statefull m then Some InOut else None in
459
  pp_base_prototype state_mode m.mstep.step_inputs m.mstep.step_outputs fmt pp_step_procedure_name
460

    
461
(** Print the prototype of the reset procedure of a machine.
462
   @param m the machine
463
   @param fmt the formater to print on
464
   @param pp_name name function printer
465
**)
466
let pp_reset_prototype m fmt =
467
  let state_mode = if is_machine_statefull m then Some InOut else None in
468
  pp_base_prototype state_mode m.mstatic [] fmt pp_reset_procedure_name
469

    
470
(** Print the prototype of the init procedure of a machine.
471
   @param m the machine
472
   @param fmt the formater to print on
473
   @param pp_name name function printer
474
**)
475
let pp_init_prototype m fmt =
476
  let state_mode = if is_machine_statefull m then Some Out else None in
477
  pp_base_prototype state_mode m.mstatic [] fmt pp_init_procedure_name
478

    
479
(** Print the prototype of the clear procedure of a machine.
480
   @param m the machine
481
   @param fmt the formater to print on
482
   @param pp_name name function printer
483
**)
484
let pp_clear_prototype m fmt =
485
  let state_mode = if is_machine_statefull m then Some InOut else None in
486
  pp_base_prototype state_mode m.mstatic [] fmt pp_clear_procedure_name
487

    
488
(** Print a one line comment with the final new line character to avoid
489
      commenting anything else.
490
   @param fmt the formater to print on
491
   @param s the comment without newline character
492
**)
493
let pp_oneline_comment fmt s =
494
  assert (not (String.contains s '\n'));
495
  fprintf fmt "-- %s@," s
496

    
497
(* Functions which computes the substitution for polymorphic type *)
498

    
499

    
500
(** Check if a submachine is statefull.
501
    @param submachine a submachine
502
    @return true if the submachine is statefull
503
**)
504
let is_submachine_statefull submachine =
505
    not (snd (snd submachine)).mname.node_dec_stateless
506

    
507
(** Find a submachine step call in a list of instructions.
508
    @param ident submachine instance ident
509
    @param instr_list List of instruction sto search
510
    @return a list of pair containing input types and output types for each step call found
511
**)
512
let rec find_submachine_step_call ident instr_list =
513
  let search_instr instruction = 
514
    match instruction.instr_desc with
515
      | MStep (il, i, vl) when String.equal i ident -> [
516
        (List.map (function x-> x.value_type) vl,
517
            List.map (function x-> x.var_type) il)]
518
      | MBranch (_, l) -> List.flatten
519
          (List.map (function x, y -> find_submachine_step_call ident y) l)
520
      | _ -> []
521
  in
522
  List.flatten (List.map search_instr instr_list)
523

    
524
(** Check that two types are the same.
525
   @param t1 a type
526
   @param t2 an other type
527
   @param return true if the two types are Tbasic or Tunivar and equal
528
**)
529
let rec check_type_equal (t1:Types.type_expr) (t2:Types.type_expr) =
530
  match (Types.repr t1).Types.tdesc, (Types.repr t2).Types.tdesc with
531
    | Types.Tbasic x, Types.Tbasic y -> x = y
532
    | Types.Tunivar,  Types.Tunivar  -> t1.tid = t2.tid
533
    | Types.Ttuple l, _ -> assert (List.length l = 1); check_type_equal (List.hd l) t2
534
    | _, Types.Ttuple l -> assert (List.length l = 1); check_type_equal t1 (List.hd l)
535
    | Types.Tstatic (_, t), _ -> check_type_equal t t2
536
    | _, Types.Tstatic (_, t) -> check_type_equal t1 t
537
    | _ -> eprintf "ERROR: %a | %a" pp_type t1 pp_type t2; assert false (* TODO *)
538

    
539
(** Extend a substitution to unify the two given types. Only the
540
  first type can be polymorphic.
541
    @param subsitution the base substitution
542
    @param type_poly the type which can be polymorphic
543
    @param typ the type to match type_poly with
544
**)
545
let unification (substituion:(int*Types.type_expr) list) ((type_poly:Types.type_expr), (typ:Types.type_expr)) =
546
  assert(not (is_Tunivar typ));
547
  (* If type_poly is polymorphic *)
548
  if is_Tunivar type_poly then
549
    (* If a subsitution exists for it *)
550
    if List.mem_assoc type_poly.tid substituion then
551
    begin
552
      (* We check that the type corresponding to type_poly in the subsitution
553
         match typ *)
554
      (try
555
        assert(check_type_equal (List.assoc type_poly.tid substituion) typ)
556
      with
557
        Not_found -> assert false);
558
      (* We return the original substituion, it is already correct *)
559
      substituion
560
    end
561
    (* If type_poly is not in the subsitution *)
562
    else
563
      (* We add it to the substituion *)
564
      (type_poly.tid, typ)::substituion
565
  (* iftype_poly is not polymorphic *)
566
  else
567
  begin
568
    (* We check that type_poly and typ are the same *)
569
    assert(check_type_equal type_poly typ);
570
    (* We return the original substituion, it is already correct *)
571
    substituion
572
  end
573

    
574
(** Check that two calls are equal. A call is
575
  a pair of list of types, the inputs and the outputs.
576
   @param calls a list of pair of list of types
577
   @param return true if the two pairs are equal
578
**)
579
let check_call_equal (i1, o1) (i2, o2) =
580
  (List.for_all2 check_type_equal i1 i2)
581
    && (List.for_all2 check_type_equal i1 i2)
582

    
583
(** Check that all the elements of list of calls are equal to one.
584
  A call is a pair of list of types, the inputs and the outputs.
585
   @param call a pair of list of types
586
   @param calls a list of pair of list of types
587
   @param return true if all the elements are equal
588
**)
589
let check_calls call calls =
590
  List.for_all (check_call_equal call) calls
591

    
592
(** Extract from a subinstance that can have polymorphic type the instantiation
593
    of all its polymorphic type instanciation for a given machine. It searches
594
    the step calls and extract a substitution for all polymorphic type from
595
    it.
596
   @param machine the machine which instantiate the subinstance
597
   @param ident the identifier of the instance which permits to find the step call
598
   @param submachine the machine corresponding to the subinstance
599
   @return the correspondance between polymorphic type id and their instantiation
600
**)
601
let get_substitution machine ident submachine =
602
  (* extract the calls to submachines from the machine *)
603
  let calls = find_submachine_step_call ident machine.mstep.step_instrs in
604
  (* extract the first call  *)
605
  let call = match calls with
606
              (* assume that there is always one call to a subinstance *)
607
              | []    -> assert(false)
608
              | h::t  -> h in
609
  (* assume that all the calls to a subinstance are using the same type *)
610
  assert(check_calls call calls);
611
  (* make a list of all types from input and output vars *)
612
  let call_types = (fst call)@(snd call) in
613
  (* extract all the input and output vars from the submachine *)
614
  let machine_vars = submachine.mstep.step_inputs@submachine.mstep.step_outputs in
615
  (* keep only the type of vars *)
616
  let machine_types = List.map (function x-> x.var_type) machine_vars in
617
  (* assume that there is the same numer of input and output in the submachine
618
      and the call *)
619
  assert (List.length machine_types = List.length call_types);
620
  (* Unify the two lists of types *)
621
  let substitution = List.fold_left unification [] (List.combine machine_types call_types) in
622
  (* Assume that our substitution match all the possible
623
       polymorphic type of the node *)
624
  let polymorphic_types = find_all_polymorphic_type submachine in
625
  assert (List.length polymorphic_types = List.length substitution);
626
  (try
627
    assert (List.for_all (fun x -> List.mem_assoc x substitution) polymorphic_types)
628
  with
629
    Not_found -> assert false);
630
  substitution
631

    
632

    
633
(* Procedure pretty print functions *)
634

    
635
(** Print the definition of a procedure
636
   @param pp_name the procedure name printer
637
   @param pp_prototype the prototype printer
638
   @param pp_instr local var printer
639
   @param pp_instr instruction printer
640
   @param fmt the formater to print on
641
   @param locals locals var list
642
   @param instrs instructions list
643
**)
644
let pp_procedure_definition pp_name pp_prototype pp_local pp_instr fmt (locals, instrs) =
645
  fprintf fmt "@[<v>%t is%t@[<v>%a%t@]@,begin@,  @[<v>%a%t@]@,end %t@]"
646
    pp_prototype
647
    (Utils.pp_final_char_if_non_empty "@,  " locals)
648
    (Utils.fprintf_list ~sep:";@," pp_local) locals
649
    (Utils.pp_final_char_if_non_empty ";" locals)
650
    (Utils.fprintf_list ~sep:";@," pp_instr) instrs
651
    (Utils.pp_final_char_if_non_empty ";" instrs)
652
    pp_name
653

    
654

    
655
(* Expression print functions *)
656

    
657
  (* Printing functions for basic operations and expressions *)
658
  (* TODO: refactor code -> use let rec and for basic pretty printing
659
     function *)
660
  (** Printing function for Ada tags, mainly booleans.
661

    
662
      @param fmt the formater to use
663
      @param t the tag to print
664
   **)
665
  let pp_ada_tag fmt t =
666
    pp_print_string fmt
667
      (if t = tag_true then "True" else if t = tag_false then "False" else t)
668

    
669
  (** Printing function for machine type constants. For the moment,
670
      arrays are not supported.
671

    
672
      @param fmt the formater to use
673
      @param c the constant to print
674
   **)
675
  let pp_ada_const fmt c =
676
    match c with
677
    | Const_int i                     -> pp_print_int fmt i
678
    | Const_real (c, e, s)            -> pp_print_string fmt s
679
    | Const_tag t                     -> pp_ada_tag fmt t
680
    | Const_string _ | Const_modeid _ ->
681
      (Format.eprintf
682
         "internal error: Ada_backend_adb.pp_ada_const cannot print string or modeid.";
683
       assert false)
684
    | _                               ->
685
      raise (Ada_not_supported "unsupported: Ada_backend_adb.pp_ada_const does not
686
      support this constant")
687

    
688
  (** Printing function for expressions [v1 modulo v2]. Depends
689
      on option [integer_div_euclidean] to choose between mathematical
690
      modulo or remainder ([rem] in Ada).
691

    
692
      @param pp_value pretty printer for values
693
      @param v1 the first value in the expression
694
      @param v2 the second value in the expression
695
      @param fmt the formater to print on
696
   **)
697
  let pp_mod pp_value v1 v2 fmt =
698
    if !Options.integer_div_euclidean then
699
      (* (a rem b) + (a rem b < 0 ? abs(b) : 0) *)
700
      Format.fprintf fmt
701
        "((%a rem %a) + (if (%a rem %a) < 0 then abs(%a) else 0))"
702
        pp_value v1 pp_value v2
703
        pp_value v1 pp_value v2
704
        pp_value v2
705
    else (* Ada behavior for rem *)
706
      Format.fprintf fmt "(%a rem %a)" pp_value v1 pp_value v2
707

    
708
  (** Printing function for expressions [v1 div v2]. Depends on
709
      option [integer_div_euclidean] to choose between mathematic
710
      division or Ada division.
711

    
712
      @param pp_value pretty printer for values
713
      @param v1 the first value in the expression
714
      @param v2 the second value in the expression
715
      @param fmt the formater to print in
716
   **)
717
  let pp_div pp_value v1 v2 fmt =
718
    if !Options.integer_div_euclidean then
719
      (* (a - ((a rem b) + (if a rem b < 0 then abs (b) else 0))) / b) *)
720
      Format.fprintf fmt "(%a - %t) / %a"
721
        pp_value v1
722
        (pp_mod pp_value v1 v2)
723
        pp_value v2
724
    else (* Ada behavior for / *)
725
      Format.fprintf fmt "(%a / %a)" pp_value v1 pp_value v2
726

    
727
  (** Printing function for basic lib functions.
728

    
729
      @param pp_value pretty printer for values
730
      @param i a string representing the function
731
      @param fmt the formater to print on
732
      @param vl the list of operands
733
   **)
734
  let pp_basic_lib_fun pp_value ident fmt vl =
735
    match ident, vl with
736
    | "uminus", [v]    ->
737
      Format.fprintf fmt "(- %a)" pp_value v
738
    | "not", [v]       ->
739
      Format.fprintf fmt "(not %a)" pp_value v
740
    | "impl", [v1; v2] ->
741
      Format.fprintf fmt "(not %a or else %a)" pp_value v1 pp_value v2
742
    | "=", [v1; v2]    ->
743
      Format.fprintf fmt "(%a = %a)" pp_value v1 pp_value v2
744
    | "mod", [v1; v2]  -> pp_mod pp_value v1 v2 fmt
745
    | "equi", [v1; v2] ->
746
      Format.fprintf fmt "((not %a) = (not %a))" pp_value v1 pp_value v2
747
    | "xor", [v1; v2]  ->
748
      Format.fprintf fmt "((not %a) /= (not %a))" pp_value v1 pp_value v2
749
    | "/", [v1; v2]    -> pp_div pp_value v1 v2 fmt
750
    | "&&", [v1; v2]    ->
751
      Format.fprintf fmt "(%a %s %a)" pp_value v1 "and then" pp_value v2
752
    | "||", [v1; v2]    ->
753
      Format.fprintf fmt "(%a %s %a)" pp_value v1 "or else" pp_value v2
754
    | "!=", [v1; v2]    ->
755
      Format.fprintf fmt "(%a %s %a)" pp_value v1 "/=" pp_value v2
756
    | op, [v1; v2]     ->
757
      Format.fprintf fmt "(%a %s %a)" pp_value v1 op pp_value v2
758
    | op, [v1] when  List.mem_assoc ident ada_supported_funs ->
759
      let pkg, name = try List.assoc ident ada_supported_funs
760
        with Not_found -> assert false in
761
      let pkg = pkg^(if String.equal pkg "" then "" else ".") in
762
        Format.fprintf fmt "%s%s(%a)" pkg name pp_value v1
763
    | fun_name, _      ->
764
      (Format.eprintf "internal compilation error: basic function %s@." fun_name; assert false)
765

    
766
  (** Printing function for values.
767

    
768
      @param m the machine to know the state variable
769
      @param fmt the formater to use
770
      @param value the value to print. Should be a
771
             {!type:Machine_code_types.value_t} value
772
   **)
773
  let rec pp_value m fmt value =
774
    match value.value_desc with
775
    | Cst c             -> pp_ada_const fmt c
776
    | Var var      -> pp_access_var m fmt var
777
    | Fun (f_ident, vl) -> pp_basic_lib_fun (pp_value m) f_ident fmt vl
778
    | _                 ->
779
      raise (Ada_not_supported
780
               "unsupported: Ada_backend.adb.pp_value does not support this value type")
(5-5/6)