Project

General

Profile

Download (4.37 KB) Statistics
| Branch: | Tag: | Revision:
1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT - ISAE-SUPAERO     *)
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 Machine_code_types
14

    
15
open Ada_backend_common
16

    
17
let indent_size = 2
18

    
19
(** Log at level 2 a string message with some indentation.
20
   @param indent the indentation level
21
   @param info the message
22
**)
23
let log_str_level_two indent info =
24
  let str_indent = String.make (indent*indent_size) ' ' in
25
  let pp_message fmt = fprintf fmt "%s.. %s@." str_indent info in
26
  Log.report ~level:2 pp_message
27

    
28
(** Write a new file with formatter
29
   @param destname folder where the file shoudl be created
30
   @param pp_filename function printing the filename
31
   @param pp_file function wich pretty print the file
32
   @param arg will be given to pp_filename and pp_file
33
**)
34
let write_file destname pp_filename pp_file arg =
35
  let path = asprintf "%s%a" destname pp_filename arg in
36
  let out = open_out path in
37
  let fmt = formatter_of_out_channel out in
38
  pp_file fmt arg;
39
  pp_print_flush fmt ();
40
  close_out out;
41
  log_str_level_two 2 (path^" generated")
42

    
43

    
44
(** Print the filename of a machine package.
45
   @param extension the extension to append to the package name
46
   @param fmt the formatter
47
   @param machine the machine corresponding to the package
48
**)
49
let pp_machine_filename extension fmt machine =
50
  pp_filename extension fmt (function fmt -> pp_package_name fmt machine)
51

    
52
(** Exception raised when a machine contains a feature not supported by the
53
  Ada backend*)
54
exception CheckFailed of string
55

    
56

    
57
(** Check that a machine match the requirement for an Ada compilation :
58
      - No constants.
59
   @param machine the machine to test
60
**)
61
let check machine =
62
  match machine.mconst with
63
    | [] -> ()
64
    | _ -> raise (CheckFailed "machine.mconst should be void")
65

    
66
(** Print the name of the ada project file.
67
   @param fmt the formater to print on
68
   @param main_machine the machine associated to the main node
69
**)
70
let pp_project_name fmt main_machine =
71
  fprintf fmt "%a.gpr" pp_package_name main_machine
72

    
73
(** Main function of the Ada backend. It calls all the subfunction creating all
74
the file and fill them with Ada code representing the machines list given.
75
   @param basename useless
76
   @param prog useless
77
   @param prog list of machines to translate
78
   @param dependencies useless
79
**)
80
let translate_to_ada basename prog machines dependencies =
81
  let module Ads = Ada_backend_ads.Main in
82
  let module Adb = Ada_backend_adb.Main in
83
  let module Wrapper = Ada_backend_wrapper.Main in
84

    
85
  (* Extract the main machine if there is one *)
86
  let main_machine = (match !Options.main_node with
87
  | "" -> None
88
  | main_node -> (
89
    match Machine_code_common.get_machine_opt main_node machines with
90
    | None -> begin
91
      Format.eprintf "Ada Code generation error: %a@." Error.pp_error_msg Error.Main_not_found;
92
      raise (Corelang.Error (Location.dummy_loc, Error.Main_not_found))
93
    end
94
    | Some m -> Some m
95
  )) in
96

    
97
  let destname = !Options.dest_dir ^ "/" in
98

    
99
  log_str_level_two 1 "Checking machines";
100
  List.iter check machines;
101

    
102
  log_str_level_two 1 "Generating ads";
103
  List.iter (write_file destname (pp_machine_filename "ads") (Ads.pp_file machines) ) machines;
104

    
105
  log_str_level_two 1 "Generating adb";
106
  List.iter (write_file destname (pp_machine_filename "adb") Adb.pp_file) machines;
107

    
108
  (* If a main node is given we generate a main adb file and a project file *)
109
  log_str_level_two 1 "Generating wrapper files";
110
  match main_machine with
111
    | None -> log_str_level_two 2 "File not generated(no -node argument)";
112
    | Some machine ->
113
begin
114
  let pp_main_filename fmt _ =
115
    pp_filename "adb" fmt pp_main_procedure_name in
116
  write_file destname pp_project_name Wrapper.pp_project_file machine;
117
  write_file destname pp_main_filename Wrapper.pp_main_adb machine;
118
end
119

    
120

    
121
(* Local Variables: *)
122
(* compile-command:"make -C ../../.." *)
123
(* End: *)
(2-2/6)