Project

General

Profile

Download (3.63 KB) Statistics
| Branch: | Tag: | Revision:
1

    
2
(********************************************************************)
3
(*                                                                  *)
4
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
5
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
6
(*                                                                  *)
7
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
8
(*  under the terms of the GNU Lesser General Public License        *)
9
(*  version 2.1.                                                    *)
10
(*                                                                  *)
11
(********************************************************************)
12

    
13
open Lustre_types
14

    
15
(********************************************************************************************)
16
(*                      Lusic to/from Header Printing functions                             *)
17
(********************************************************************************************)
18

    
19
type lusic =
20
{
21
  obsolete  : bool;
22
  from_lusi : bool;
23
  contents  : top_decl list;
24
}
25

    
26
module HeaderMod = C_backend_header.EmptyMod
27
module Header = C_backend_header.Main (HeaderMod)
28

    
29
(* extracts a header from a program representing module owner = dirname/basename *)
30
let extract_header dirname basename prog =
31
  let owner = dirname ^ "/" ^ basename in
32
  List.fold_right
33
    (fun decl header ->
34
       (* Format.eprintf "Lusic.extract_header: header = %B, owner = %s, decl_owner = %s@."
35
        *   decl.top_decl_itf owner decl.top_decl_owner; *)
36
       if decl.top_decl_itf || decl.top_decl_owner <> owner then header
37
       else match decl.top_decl_desc with
38
         | Node nd ->
39
           { decl with top_decl_desc =
40
                         ImportedNode (Corelang.get_node_interface nd) }
41
           :: header
42
         | ImportedNode _ -> header
43
         | Const _ | TypeDef _ | Include _ | Open _  -> decl :: header)
44
    prog []
45

    
46
let check_obsolete lusic basename =
47
  if lusic.obsolete then raise (Error.Error (Location.dummy_loc, Error.Wrong_number basename))
48

    
49
(* encode and write a header in a file *)
50
let write_lusic lusi (header : top_decl list) basename extension =
51
  let target_name = basename ^ extension in
52
  let outchan = open_out_bin target_name in
53
  begin
54
    (*Format.eprintf "write_lusic: %i items.@." (List.length header);*)
55
    Marshal.to_channel outchan (Version.number, lusi : string * bool) [];
56
    Marshal.to_channel outchan (header : top_decl list) [];
57
    close_out outchan
58
  end
59

    
60
(* read and decode a header from a file *)
61
let read_lusic basename extension =
62
  let source_name = basename ^ extension in
63
  let inchan = open_in_bin source_name in
64
  let number, from_lusi = (Marshal.from_channel inchan : string * bool) in
65
  if number <> Version.number
66
  then
67
    begin
68
      close_in inchan;
69
      {
70
	obsolete  = true;
71
	from_lusi = from_lusi;
72
	contents  = [];
73
      }
74
    end
75
  else    
76
    begin
77
      let lusic = (Marshal.from_channel inchan : top_decl list) in
78
      close_in inchan;
79
      {
80
	obsolete  = false;
81
	from_lusi = from_lusi;
82
	contents  = lusic;
83
      }
84
    end
85

    
86
(* let print_lusic_to_h basename extension =
87
 *   let lusic = read_lusic basename extension in
88
 *   let header_name = basename ^ ".h" in
89
 *   let h_out = open_out header_name in
90
 *   let h_fmt = formatter_of_out_channel h_out in
91
 *   begin
92
 *     assert (not lusic.obsolete);
93
 *     (\*Format.eprintf "lusic to h: %i items.@." (List.length lusic.contents);*\)
94
 *     Typing.uneval_prog_generics lusic.contents;
95
 *     Clock_calculus.uneval_prog_generics lusic.contents;
96
 *     Header.print_header_from_header h_fmt (Filename.basename basename) lusic.contents;
97
 *     close_out h_out
98
 *   end *)
(26-26/63)