Project

General

Profile

Download (9.96 KB) Statistics
| Branch: | Tag: | Revision:
1
(* ----------------------------------------------------------------------------
2
 * SchedMCore - A MultiCore Scheduling Framework
3
 * Copyright (C) 2009-2013, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE
4
 * Copyright (C) 2012-2013, INPT, Toulouse, FRANCE
5
 *
6
 * This file is part of Prelude
7
 *
8
 * Prelude is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation ; either version 2 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * Prelude is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY ; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this program ; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21
 * USA
22
 *---------------------------------------------------------------------------- *)
23

    
24
(* This module is used for the lustre mutator *)
25

    
26
open Format
27
open Log
28

    
29
let usage = "Usage: lustrem [options] <source-file>"
30

    
31
let extensions = [".ec";".lus"]
32

    
33
let type_decls env decls =  
34
  report ~level:1 (fun fmt -> fprintf fmt ".. typing@,@?");
35
  let new_env = 
36
    begin
37
      try
38
	Typing.type_prog env decls
39
    (*Typing.uneval_prog_generics prog*)
40
      with (Types.Error (loc,err)) as exc ->
41
	Format.eprintf "Typing error at loc %a: %a@]@."
42
	  Location.pp_loc loc
43
	  Types.pp_error err;
44
	raise exc
45
    end 
46
  in
47
  if !Options.print_types then
48
    report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Corelang.pp_prog_type decls);
49
  new_env
50
      
51
let clock_decls env decls = 
52
  report ~level:1 (fun fmt -> fprintf fmt ".. clock calculus@,@?");
53
  let new_env =
54
    begin
55
      try
56
	Clock_calculus.clock_prog env decls
57
      with (Clocks.Error (loc,err)) as exc ->
58
	Location.print loc;
59
	eprintf "Clock calculus error at loc %a: %a@]@." Location.pp_loc loc Clocks.pp_error err;
60
	raise exc
61
    end
62
  in
63
  if !Options.print_clocks then
64
    report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Corelang.pp_prog_clock decls);
65
  new_env
66

    
67
(* Loading Lusi file and filing type tables with parsed
68
   functions/nodes *)
69
let load_lusi filename =
70
  Location.input_name := filename;
71
  let lexbuf = Lexing.from_channel (open_in filename) in
72
  Location.init lexbuf filename;
73
  (* Parsing *)
74
  report ~level:1 (fun fmt -> fprintf fmt "@[<v>.. parsing header file %s@,@?" filename);
75
  let header = 
76
    try
77
      Parse.prog Parser_lustre.header Lexer_lustre.token lexbuf
78
    with (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
79
      Parse.report_error err;
80
      raise exc
81
  in
82
  let new_tenv = type_decls Basic_library.type_env header in   (* Typing *)
83
  let new_cenv: Clocks.clock_expr Utils.IMap.t = clock_decls Basic_library.clock_env header in   (* Clock calculus *)
84
  header, new_tenv, new_cenv
85
  
86
    
87
let rec mutate basename extension =
88
  (* Loading the input file *)
89
  let source_name = basename^extension in
90
  Location.input_name := source_name;
91
  let lexbuf = Lexing.from_channel (open_in source_name) in
92
  Location.init lexbuf source_name;
93

    
94
  (* Parsing *)
95
  report ~level:1 
96
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@,@?" source_name);
97
  let prog =
98
    try
99
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
100
    with (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
101
      Parse.report_error err;
102
      raise exc
103
  in
104

    
105
  (* Extracting dependencies *)
106
  report ~level:1 (fun fmt -> fprintf fmt ".. extracting dependencies@,@?");
107
  let dependencies = 
108
    List.fold_right 
109
      (fun d accu -> match d.Corelang.top_decl_desc with 
110
      | Corelang.Open s -> s::accu 
111
      | _ -> accu) 
112
      prog [] 
113
  in
114
  let type_env, clock_env =
115
    List.fold_left (fun (type_env, clock_env) s -> 
116
      try
117
	let basename = s ^ ".lusi" in 
118
	report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>Library %s@ " s);
119
	let _, lusi_type_env, lusi_clock_env = load_lusi basename in 
120
	report ~level:1 (fun fmt -> fprintf fmt "@]@,@?");
121
	Env.overwrite type_env lusi_type_env,
122
	Env.overwrite clock_env lusi_clock_env      
123
      with Sys_error msg -> (
124
	Format.eprintf "Failure: impossible to load library %s.@.%s@." s msg;
125
	exit 1
126
      )
127
    )  (Basic_library.type_env, Basic_library.clock_env) dependencies
128
  in
129
  
130
  (* Sorting nodes *)
131
  let prog = SortProg.sort prog in
132
  
133
  (* Typing *)
134
  let computed_types_env = type_decls type_env prog in
135
  
136
  (* Clock calculus *)
137
  let computed_clocks_env = clock_decls clock_env prog in
138

    
139
  (* generate mutants *)
140
  let mutants = Mutation.mutate !Options.nb_mutants prog in
141
  
142
  (* Print generated mutants in target directory. *)
143
  let cpt = ref 0 in
144
  List.iter (fun mutant ->
145
    incr cpt;
146
    let mutant_filename = 
147
      match !Options.dest_dir with
148
      | "" -> (* Mutants are generated in source directory *)
149
	basename^ ".mutant.n" ^ (string_of_int !cpt) ^ extension 
150
      | dir ->  (* Mutants are generated in targer directory *)
151
	dir ^ "/" ^ (Filename.basename basename)^ ".mutant.n" ^ (string_of_int !cpt) ^ extension 
152
    in
153
    let mutant_out = (
154
      try 
155
	open_out mutant_filename 
156
      with
157
	Sys_error _ -> Format.eprintf "Unable to open file %s for writing.@." mutant_filename; exit 1
158
    )
159
    in
160
    let mutant_fmt = formatter_of_out_channel mutant_out in
161
    report ~level:1 (fun fmt -> fprintf fmt ".. generating mutant %s@,@?" mutant_filename);
162
    Format.fprintf mutant_fmt "%a@." Printers.pp_prog mutant    
163
  )
164
    mutants;
165

    
166
  (*
167

    
168

    
169
  (* Normalization phase *)
170
  report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,@?");
171
  let normalized_prog = Normalization.normalize_prog prog in
172
  Typing.uneval_prog_generics normalized_prog;
173
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Printers.pp_prog normalized_prog);
174
  (* Checking array accesses *)
175
  if !Options.check then
176
    begin
177
      report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,@?");
178
      Access.check_prog normalized_prog;
179
    end;
180

    
181
  (* DFS with modular code generation *)
182
  report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,@?");
183
  let machine_code = Machine_code.translate_prog normalized_prog in
184
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?"
185
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
186
    machine_code);
187

    
188
  (* Checking the existence of a lusi (Lustre Interface file) *)
189
  let lusi_name = basename ^ ".lusi" in
190
  let _ = 
191
    try 
192
      let _ = open_in lusi_name in
193
      let _, declared_types_env, declared_clocks_env = load_lusi lusi_name in
194
      (* checking type compatibilty with computed types*)
195
      Typing.check_env_compat declared_types_env computed_types_env;
196
      (* checking clocks compatibilty with computed clocks*)
197
      Clock_calculus.check_env_compat declared_clocks_env computed_clocks_env;
198
      
199
    with Sys_error _ -> ( 
200
      (* Printing lusi file is necessary *)
201
      report ~level:1 
202
	(fun fmt -> 
203
	  fprintf fmt 
204
	    ".. generating lustre interface file %s@,@?" lusi_name);
205
      let lusi_out = open_out lusi_name in
206
      let lusi_fmt = formatter_of_out_channel lusi_out in
207
      Printers.pp_lusi_header lusi_fmt source_name normalized_prog
208
    )
209
    | (Types.Error (loc,err)) as exc ->
210
      Format.eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@]@."
211
	Types.pp_error err;
212
      raise exc
213
  in
214

    
215
  (* Printing code *)
216
  let basename    = Filename.basename basename in
217
  if !Options.java then
218
    failwith "Sorry, but not yet supported !"
219
    (*let source_file = basename ^ ".java" in
220
      report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
221
      let source_out = open_out source_file in
222
      let source_fmt = formatter_of_out_channel source_out in
223
      report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
224
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
225
  else begin
226
    let header_file = basename ^ ".h" in (* Could be changed *)
227
    let source_file = basename ^ ".c" in (* Could be changed *)
228
    let makefile_file = basename ^ ".makefile" in (* Could be changed *)
229
    let spec_file_opt = if !Options.c_spec then 
230
	(
231
	  let spec_file = basename ^ "_spec.c" in
232
	  report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@,@?" header_file source_file spec_file);
233
	  Some spec_file 
234
	) else (
235
	  report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@,@?" header_file source_file);
236
	  None 
237
	 )
238
    in 
239
    let header_out = open_out header_file in
240
    let header_fmt = formatter_of_out_channel header_out in
241
    let source_out = open_out source_file in
242
    let source_fmt = formatter_of_out_channel source_out in
243
    let makefile_out = open_out makefile_file in
244
    let makefile_fmt = formatter_of_out_channel makefile_out in
245
    let spec_fmt_opt = match spec_file_opt with
246
	None -> None
247
      | Some f -> Some (formatter_of_out_channel (open_out f))
248
    in
249
    report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,@?");
250
    C_backend.translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename normalized_prog machine_code;
251
  end;
252
  report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
253
  (* We stop the process here *)
254
  *)
255
  exit 0
256
  
257
let anonymous filename =
258
  let ok_ext, ext = List.fold_left (fun (ok, ext) ext' -> if not ok && Filename.check_suffix filename ext' then true, ext' else ok, ext) (false, "") extensions in
259
  if ok_ext then
260
    let basename = Filename.chop_suffix filename ext in
261
    mutate basename ext
262
  else
263
    raise (Arg.Bad ("Can only compile *.lus or *.ec files"))
264

    
265
let _ =
266
  Corelang.add_internal_funs ();
267
  try
268
    Printexc.record_backtrace true;
269
    Arg.parse Options.lustrem_options anonymous usage
270
  with
271
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
272
  | Types.Error (_,_) | Clocks.Error (_,_)
273
  | Corelang.Error _ (*| Task_set.Error _*) 
274
  | Causality.Cycle _ -> exit 1
275
  | exc -> (Utils.track_exception (); raise exc)
276

    
277
(* Local Variables: *)
278
(* compile-command:"make -C .." *)
279
(* End: *)
(31-31/46)