Project

General

Profile

Revision ca88e660 src/main_lustre_compiler.ml

View differences:

src/main_lustre_compiler.ml
1
<<<<<<< HEAD
2 1
(********************************************************************)
3 2
(*                                                                  *)
4 3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
......
16 15
open Utils
17 16
open LustreSpec
18 17
open Compiler_common
18
 
19
exception StopPhase1 of program
19 20

  
20
let usage = "Usage: lustrec [options] <source-file>"
21
let usage = "Usage: lustrec [options] \x1b[4msource-file\x1b[0m"
21 22

  
22 23
let extensions = [".ec"; ".lus"; ".lusi"]
23 24

  
......
35 36
  end
36 37

  
37 38
(* compile a .lusi header file *)
38
let compile_header dirname  basename extension =
39
let compile_header dirname basename extension =
39 40
  let destname = !Options.dest_dir ^ "/" ^ basename in
40 41
  let header_name = basename ^ extension in
41 42
  let lusic_ext = extension ^ "c" in
......
71 72
	begin
72 73
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name);
73 74
       	  Lusic.write_lusic false (Lusic.extract_header dirname basename prog) destname lusic_ext;
74
	  (*List.iter (fun top_decl -> Format.eprintf "lusic: %a@." Printers.pp_decl top_decl) lusic.Lusic.contents;*)
75
(*List.iter (fun top_decl -> Format.eprintf "lusic: %a@." Printers.pp_decl top_decl) lusic.Lusic.contents;*)
75 76
	  Lusic.print_lusic_to_h destname lusic_ext
76 77
	end
77 78
      else
......
92 93
  | "horn" | "lustre" | "acsl" -> true
93 94
  | _ -> false
94 95

  
95
(* compile a .lus source file *)
96
let rec compile_source dirname basename extension =
97

  
98
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
99

  
100
  (* Parsing source *)
101
  let prog = parse_source (dirname ^ "/" ^ basename ^ extension) in
102

  
103
  (* Removing automata *)
96
(* From prog to prog *)
97
let stage1 prog dirname basename =
98
  (* Removing automata *) 
104 99
  let prog = Automata.expand_decls prog in
105 100

  
106 101
  (* Importing source *)
......
123 118
  in
124 119

  
125 120
  (* Checking stateless/stateful status *)
126
  check_stateless_decls prog;
121
  if Plugins.check_force_stateful () then
122
    force_stateful_decls prog
123
  else
124
    check_stateless_decls prog;
127 125

  
128 126
  (* Typing *)
129 127
  let computed_types_env = type_decls type_env prog in
......
133 131

  
134 132
  (* Generating a .lusi header file only *)
135 133
  if !Options.lusi then
136
    begin
137
      let lusi_ext = extension ^ "i" in
138
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@," (dirname ^ "/" ^ basename ^ lusi_ext));
139
      print_lusi prog dirname basename lusi_ext;
140
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
141
      exit 0
142
    end;
134
    (* We stop here the processing and produce the current prog. It will be
135
       exported as a lusi *)
136
    raise (StopPhase1 prog);
143 137

  
144 138
  (* Delay calculus *)
145 139
  (* TO BE DONE LATER (Xavier)
......
162 156

  
163 157
  (* Compatibility with Lusi *)
164 158
  (* Checking the existence of a lusi (Lustre Interface file) *)
165
  (match !Options.output with
166
    "C" ->
167
      begin
168
        let extension = ".lusi" in
169
        compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension;
170
      end
171
   |_ -> ());
159
  let extension = ".lusi" in
160
  compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension;
172 161

  
173 162
  Typing.uneval_prog_generics prog;
174 163
  Clock_calculus.uneval_prog_generics prog;
......
188 177
	orig prog type_env clock_env
189 178
    end;
190 179

  
191
(*Format.eprintf "Inliner.global_inline<<@.%a@.>>@." Printers.pp_prog prog;*)
192 180
  (* Computes and stores generic calls for each node,
193 181
     only useful for ANSI C90 compliant generic node compilation *)
194 182
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
195
  (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with Corelang.Node nd -> Format.eprintf "%s calls %a" id Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*)
183
  (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with
184
    Corelang.Node nd -> Format.eprintf "%s calls %a" id
185
    Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*)
186

  
187
  (* Optimization of prog: 
188
     - Unfold consts 
189
     - eliminate trivial expressions
190
  *)
191
  let prog = 
192
    if !Options.const_unfold || !Options.optimization >= 4 then 
193
      Optimize_prog.prog_unfold_consts prog 
194
    else
195
      prog
196
  in
196 197

  
197 198
  (* Normalization phase *)
198 199
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
......
200 201
  if !Options.output = "lustre" then
201 202
    Normalization.unfold_arrow_active := false;
202 203
  let prog = Normalization.normalize_prog prog in
204
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
203 205

  
206
  let prog =
207
    if !Options.mpfr
208
    then
209
      begin
210
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. targetting MPFR library@,");
211
	Mpfr.inject_prog prog
212
      end
213
    else
214
      begin
215
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. keeping FP numbers@,");
216
	prog
217
      end in
204 218
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
219

  
205 220
  (* Checking array accesses *)
206 221
  if !Options.check then
207 222
    begin
......
209 224
      Access.check_prog prog;
210 225
    end;
211 226

  
227
  prog, dependencies
228

  
229
(* prog -> machine *)
230

  
231
let stage2 prog =    
232

  
212 233
  (* Computation of node equation scheduling. It also breaks dependency cycles
213 234
     and warns about unused input or memory variables *)
214 235
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
215 236
  let prog, node_schs = Scheduling.schedule_prog prog in
216
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
237
  Log.report ~level:1 (fun fmt -> fprintf fmt "%a"              Scheduling.pp_warning_unused node_schs);
217 238
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
218 239
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
240
  Log.report ~level:5 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_dep_graph node_schs);
219 241
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
220 242

  
221 243
 (* Optimization of prog:
222 244
    - Unfold consts
223 245
    - eliminate trivial expressions
246
	TODO (PL): should we keep this phase or just remove it 
224 247
 *)
225 248
  let prog =
226 249
    if !Options.optimization >= 5 then
......
231 254
    else
232 255
      prog
233 256
  in
257

  
258
  (* TODO Salsa optimize prog: 
259
     - emits warning for programs with pre inside expressions
260
     - make sure each node arguments and memory is bounded by a local annotation
261
     - introduce fresh local variables for each real pure subexpression
262
  *)
263
 (*  let prog =  *)
264
 (*    if true then *)
265
 (*      Salsa.Prog.normalize prog *)
266
 (*    else *)
267
 (*      prog *)
268
 (* in *)
269

  
234 270
  (* DFS with modular code generation *)
235
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
271
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@ ");
236 272
  let machine_code = Machine_code.translate_prog prog node_schs in
237 273

  
238
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
239
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
240
  machine_code);
241

  
242
  (* Optimize machine code *)
274
   (* Optimize machine code *)
243 275
  let machine_code =
244 276
    if !Options.optimization >= 4 (* && !Options.output <> "horn" *) then
245 277
      begin
......
250 282
    else
251 283
      machine_code
252 284
  in
253

  
285
  
254 286
  (* Optimize machine code *)
255
  let machine_code =
256
    if !Options.optimization >= 2 (* && !Options.output <> "horn" *) then
287
  let machine_code, removed_table = 
288
    if !Options.optimization >= 2 (*&& !Options.output <> "horn"*) then
257 289
      begin
258
	Log.report ~level:1 
259
	  (fun fmt -> fprintf fmt 
260
	    ".. machines optimization: const. inlining (partial eval. with const)@,");
290
	Log.report ~level:1 (fun fmt -> fprintf fmt 
291
    ".. machines optimization: const. inlining (partial eval. with const)@,");
261 292
	Optimize_machine.machines_unfold (Corelang.get_consts prog) node_schs machine_code
262 293
      end
263 294
    else
264
      machine_code
265
  in
295
      machine_code, IMap.empty
296
  in  
266 297
  (* Optimize machine code *)
267
  let machine_code = (* TODO reactivate. I disabled it because output variables were removed *)
298
  let machine_code =  (* TODO reactivate. I disabled it because output variables were removed *)
268 299
    if false && !Options.optimization >= 3 && not (functional_backend ()) then
269 300
      begin
270
	Log.report ~level:1 
271
	  (fun fmt -> fprintf fmt ".. machines optimization: minimize heap alloc by reusing vars@,");
272
	Optimize_machine.machines_fusion 
273
	  (Optimize_machine.machines_reuse_variables machine_code node_schs)
301
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization: minimize heap alloc by reusing vars@,");
302
	let node_schs    = Scheduling.remove_prog_inlined_locals removed_table node_schs in
303
	let reuse_tables = Scheduling.compute_prog_reuse_table node_schs in
304
	Optimize_machine.machines_fusion (Optimize_machine.machines_reuse_variables machine_code reuse_tables)
274 305
      end
275 306
    else
276 307
      machine_code
277 308
  in
309
  
310
  (* Salsa optimize machine code *)
311
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@ "
312
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
313
    machine_code);
314

  
315
  machine_code
278 316

  
279
  if !Options.optimization >= 2 then
280
    begin
281
      Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
282
	(Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
283
	machine_code);
284
    end;
285 317

  
318
let stage3 prog machine_code dependencies basename =
286 319
  (* Printing code *)
287 320
  let basename    =  Filename.basename basename in
288 321
  let destname = !Options.dest_dir ^ "/" ^ basename in
289
  let _ = match !Options.output with
290
    | "C" ->
291
      begin
322
  match !Options.output with
323
      "C" -> 
324
	begin
292 325
	  let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *)
293 326
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
294 327
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
......
316 349
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,");
317 350
        Horn_backend.translate fmt basename prog (Machine_code.arrow_machine::machine_code);
318 351
	(* Tracability file if option is activated *)
319
	if !Options.traces then (
352
	if !Options.horntraces then (
320 353
	let traces_file = destname ^ ".traces.xml" in (* Could be changed *)
321 354
	let traces_out = open_out traces_file in
322 355
	let fmt = formatter_of_out_channel traces_out in
......
335 368
      end
336 369

  
337 370
    | _ -> assert false
371

  
372
(* compile a .lus source file *)
373
let compile_source dirname basename extension =
374

  
375
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
376

  
377
  (* Parsing source *)
378
  let prog = parse_source (dirname ^ "/" ^ basename ^ extension) in
379

  
380
  let prog =
381
    if !Options.mpfr then
382
      Mpfr.mpfr_module::prog
383
    else
384
      prog
338 385
  in
386
  let prog, dependencies = 
387
    try 
388
      stage1 prog dirname basename
389
    with StopPhase1 prog -> (
390
      if !Options.lusi then
391
	begin
392
	  let lusi_ext = ".lusi" (* extension ^ "i" *) in
393
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@," (dirname ^ "/" ^ basename ^ lusi_ext));
394
	  print_lusi prog dirname basename lusi_ext;
395
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
396
	  exit 0
397
	end
398
      else
399
        assert false
400
    )
401
  in
402

  
403
  let machine_code = 
404
    stage2 prog 
405
  in
406

  
407
  let machine_code = Plugins.refine_machine_code prog machine_code in
408
  
409
  stage3 prog machine_code dependencies basename;
339 410
  begin
340
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done @ @]@.");
341
  (* We stop the process here *)
411
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
412
    (* We stop the process here *)
342 413
    exit 0
343 414
  end
344 415

  
......
364 435
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
365 436

  
366 437
let _ =
438
  Global.initialize ();
367 439
  Corelang.add_internal_funs ();
368 440
  try
369 441
    Printexc.record_backtrace true;
370
    Arg.parse Options.options anonymous usage
442

  
443
    let options = Options.options @ (Plugins.options ()) in
444
    
445
    Arg.parse options anonymous usage
371 446
  with
372 447
  | Parse.Syntax_err _ | Lexer_lustre.Error _
373 448
  | Types.Error (_,_) | Clocks.Error (_,_)
......
379 454
(* Local Variables: *)
380 455
(* compile-command:"make -C .." *)
381 456
(* End: *)
382
=======
383
(********************************************************************)
384
(*                                                                  *)
385
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
386
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
387
(*                                                                  *)
388
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
389
(*  under the terms of the GNU Lesser General Public License        *)
390
(*  version 2.1.                                                    *)
391
(*                                                                  *)
392
(********************************************************************)
393

  
394
open Format
395
open Log
396

  
397
open Utils
398
open LustreSpec
399
open Compiler_common
400
 
401
exception StopPhase1 of program
402

  
403
let usage = "Usage: lustrec [options] \x1b[4msource-file\x1b[0m"
404

  
405
let extensions = [".ec"; ".lus"; ".lusi"]
406

  
407
(* print a .lusi header file from a source prog *)
408
let print_lusi prog dirname basename extension =
409
  let header = Lusic.extract_header dirname basename prog in
410
  let header_name = dirname ^ "/" ^ basename ^ extension in
411
  let h_out = open_out header_name in
412
  let h_fmt = formatter_of_out_channel h_out in
413
  begin
414
    Typing.uneval_prog_generics header;
415
    Clock_calculus.uneval_prog_generics header;
416
    Printers.pp_lusi_header h_fmt basename header;
417
    close_out h_out
418
  end
419

  
420
(* compile a .lusi header file *)
421
let compile_header dirname basename extension =
422
  let destname = !Options.dest_dir ^ "/" ^ basename in
423
  let header_name = basename ^ extension in
424
  let lusic_ext = extension ^ "c" in
425
  begin
426
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
427
    let header = parse_header true (dirname ^ "/" ^ header_name) in
428
    ignore (Modules.load_header ISet.empty header);
429
    ignore (check_top_decls header);
430
    create_dest_dir ();
431
    Log.report ~level:1
432
      (fun fmt -> fprintf fmt ".. generating compiled header file %sc@," (destname ^ extension));
433
    Lusic.write_lusic true header destname lusic_ext;
434
    Lusic.print_lusic_to_h destname lusic_ext;
435
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.")
436
  end
437

  
438
(* check whether a source file has a compiled header,
439
   if not, generate the compiled header *)
440
let compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension =
441
  let destname = !Options.dest_dir ^ "/" ^ basename in
442
  let lusic_ext = extension ^ "c" in
443
  let header_name = destname ^ lusic_ext in
444
  begin
445
    if not (Sys.file_exists header_name) then
446
      begin
447
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name);
448
	Lusic.write_lusic false (Lusic.extract_header dirname basename prog) destname lusic_ext;
449
	Lusic.print_lusic_to_h destname lusic_ext
450
      end
451
    else
452
      let lusic = Lusic.read_lusic destname lusic_ext in
453
      if not lusic.Lusic.from_lusi then
454
	begin
455
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name);
456
       	  Lusic.write_lusic false (Lusic.extract_header dirname basename prog) destname lusic_ext;
457
(*List.iter (fun top_decl -> Format.eprintf "lusic: %a@." Printers.pp_decl top_decl) lusic.Lusic.contents;*)
458
	  Lusic.print_lusic_to_h destname lusic_ext
459
	end
460
      else
461
	begin
462
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. loading compiled header file %s@," header_name);
463
	  Modules.check_dependency lusic destname;
464
	  let header = lusic.Lusic.contents in
465
	  let (declared_types_env, declared_clocks_env) = get_envs_from_top_decls header in
466
	  check_compatibility
467
	    (prog, computed_types_env, computed_clocks_env)
468
	    (header, declared_types_env, declared_clocks_env)
469
	end
470
  end
471

  
472
(* From prog to prog *)
473
let stage1 prog dirname basename =
474
  (* Removing automata *) 
475
  let prog = Automata.expand_decls prog in
476

  
477
  (* Importing source *)
478
  let _ = Modules.load_program ISet.empty prog in
479

  
480
  (* Extracting dependencies *)
481
  let dependencies, type_env, clock_env = import_dependencies prog in
482

  
483
  (* Sorting nodes *)
484
  let prog = SortProg.sort prog in
485

  
486
  (* Perform inlining before any analysis *)
487
  let orig, prog =
488
    if !Options.global_inline && !Options.main_node <> "" then
489
      (if !Options.witnesses then prog else []),
490
      Inliner.global_inline basename prog type_env clock_env
491
    else (* if !Option.has_local_inline *)
492
      [],
493
      Inliner.local_inline basename prog type_env clock_env
494
  in
495

  
496
  (* Checking stateless/stateful status *)
497
  if Plugins.check_force_stateful () then
498
    force_stateful_decls prog
499
  else
500
    check_stateless_decls prog;
501

  
502
  (* Typing *)
503
  let computed_types_env = type_decls type_env prog in
504

  
505
  (* Clock calculus *)
506
  let computed_clocks_env = clock_decls clock_env prog in
507

  
508
  (* Generating a .lusi header file only *)
509
  if !Options.lusi then
510
    (* We stop here the processing and produce the current prog. It will be
511
       exported as a lusi *)
512
    raise (StopPhase1 prog);
513

  
514
  (* Delay calculus *)
515
  (* TO BE DONE LATER (Xavier)
516
    if(!Options.delay_calculus)
517
    then
518
    begin
519
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
520
    try
521
    Delay_calculus.delay_prog Basic_library.delay_env prog
522
    with (Delay.Error (loc,err)) as exc ->
523
    Location.print loc;
524
    eprintf "%a" Delay.pp_error err;
525
    Utils.track_exception ();
526
    raise exc
527
    end;
528
  *)
529

  
530
  (* Creating destination directory if needed *)
531
  create_dest_dir ();
532

  
533
  (* Compatibility with Lusi *)
534
  (* Checking the existence of a lusi (Lustre Interface file) *)
535
  let extension = ".lusi" in
536
  compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension;
537

  
538
  Typing.uneval_prog_generics prog;
539
  Clock_calculus.uneval_prog_generics prog;
540

  
541
  if !Options.global_inline && !Options.main_node <> "" && !Options.witnesses then
542
    begin
543
      let orig = Corelang.copy_prog orig in
544
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating witness file@,");
545
      check_stateless_decls orig;
546
      let _ = Typing.type_prog type_env orig in
547
      let _ = Clock_calculus.clock_prog clock_env orig in
548
      Typing.uneval_prog_generics orig;
549
      Clock_calculus.uneval_prog_generics orig;
550
      Inliner.witness
551
	basename
552
	!Options.main_node
553
	orig prog type_env clock_env
554
    end;
555

  
556
  (* Computes and stores generic calls for each node,
557
     only useful for ANSI C90 compliant generic node compilation *)
558
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
559
  (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with
560
    Corelang.Node nd -> Format.eprintf "%s calls %a" id
561
    Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*)
562

  
563
  (* Optimization of prog: 
564
     - Unfold consts 
565
     - eliminate trivial expressions
566
  *)
567
  let prog = 
568
    if !Options.const_unfold || !Options.optimization >= 4 then 
569
      Optimize_prog.prog_unfold_consts prog 
570
    else
571
      prog
572
  in
573

  
574
  (* Normalization phase *)
575
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
576
  (* Special treatment of arrows in lustre backend. We want to keep them *)
577
  if !Options.output = "lustre" then
578
    Normalization.unfold_arrow_active := false;
579
  let prog = Normalization.normalize_prog prog in
580
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
581

  
582
  let prog =
583
    if !Options.mpfr
584
    then
585
      begin
586
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. targetting MPFR library@,");
587
	Mpfr.inject_prog prog
588
      end
589
    else
590
      begin
591
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. keeping FP numbers@,");
592
	prog
593
      end in
594
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
595

  
596
  (* Checking array accesses *)
597
  if !Options.check then
598
    begin
599
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
600
      Access.check_prog prog;
601
    end;
602

  
603
  prog, dependencies
604

  
605
(* prog -> machine *)
606

  
607
let stage2 prog =    
608

  
609
  (* Computation of node equation scheduling. It also breaks dependency cycles
610
     and warns about unused input or memory variables *)
611
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
612
  let prog, node_schs = Scheduling.schedule_prog prog in
613
  Log.report ~level:1 (fun fmt -> fprintf fmt "%a"              Scheduling.pp_warning_unused node_schs);
614
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
615
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
616
  Log.report ~level:5 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_dep_graph node_schs);
617
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
618

  
619

  
620
  (* TODO Salsa optimize prog: 
621
     - emits warning for programs with pre inside expressions
622
     - make sure each node arguments and memory is bounded by a local annotation
623
     - introduce fresh local variables for each real pure subexpression
624
  *)
625
 (*  let prog =  *)
626
 (*    if true then *)
627
 (*      Salsa.Prog.normalize prog *)
628
 (*    else *)
629
 (*      prog *)
630
 (* in *)
631

  
632
  (* DFS with modular code generation *)
633
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@ ");
634
  let machine_code = Machine_code.translate_prog prog node_schs in
635

  
636
  (* Optimize machine code *)
637
  let machine_code, removed_table = 
638
    if !Options.optimization >= 2 && !Options.output <> "horn" then
639
      begin
640
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 1)@ ");
641
	Optimize_machine.machines_unfold (Corelang.get_consts prog) node_schs machine_code
642
      end
643
    else
644
      machine_code, IMap.empty
645
  in  
646
  (* Optimize machine code *)
647
  let machine_code = 
648
    if !Options.optimization >= 3 && !Options.output <> "horn" then
649
      begin
650
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 2)@ ");
651
	let node_schs    = Scheduling.remove_prog_inlined_locals removed_table node_schs in
652
	let reuse_tables = Scheduling.compute_prog_reuse_table node_schs in
653
	Optimize_machine.machines_fusion (Optimize_machine.machines_reuse_variables machine_code reuse_tables)
654
      end
655
    else
656
      machine_code
657
  in
658
  
659
  (* Salsa optimize machine code *)
660
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@ "
661
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
662
    machine_code);
663

  
664
  machine_code
665

  
666

  
667
let stage3 prog machine_code dependencies basename =
668
  (* Printing code *)
669
  let basename    =  Filename.basename basename in
670
  let destname = !Options.dest_dir ^ "/" ^ basename in
671
  match !Options.output with
672
      "C" -> 
673
	begin
674
	  let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *)
675
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
676
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
677
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
678
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
679
	  C_backend.translate_to_c
680
	    alloc_header_file source_lib_file source_main_file makefile_file
681
	    basename prog machine_code dependencies
682
	end
683
    | "java" ->
684
      begin
685
	failwith "Sorry, but not yet supported !"
686
    (*let source_file = basename ^ ".java" in
687
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
688
      let source_out = open_out source_file in
689
      let source_fmt = formatter_of_out_channel source_out in
690
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
691
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
692
      end
693
    | "horn" ->
694
      begin
695
	let source_file = destname ^ ".smt2" in (* Could be changed *)
696
	let source_out = open_out source_file in
697
	let fmt = formatter_of_out_channel source_out in
698
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,");
699
    Horn_backend.translate fmt basename prog machine_code;
700
	(* Tracability file if option is activated *)
701
	if !Options.horntraces then (
702
	let traces_file = destname ^ ".traces" in (* Could be changed *)
703
	let traces_out = open_out traces_file in
704
	let fmt = formatter_of_out_channel traces_out in
705
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. tracing info@,");
706
	Horn_backend.traces_file fmt basename prog machine_code;
707
	)
708
      end
709
    | "lustre" ->
710
      begin
711
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
712
	let source_out = open_out source_file in
713
	let fmt = formatter_of_out_channel source_out in
714
	Printers.pp_prog fmt prog;
715
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
716
	()
717
      end
718

  
719
    | _ -> assert false
720

  
721
(* compile a .lus source file *)
722
let compile_source dirname basename extension =
723

  
724
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
725

  
726
  (* Parsing source *)
727
  let prog = parse_source (dirname ^ "/" ^ basename ^ extension) in
728

  
729
  let prog =
730
    if !Options.mpfr then
731
      Mpfr.mpfr_module::prog
732
    else
733
      prog
734
  in
735
  let prog, dependencies = 
736
    try 
737
      stage1 prog dirname basename
738
    with StopPhase1 prog -> (
739
      if !Options.lusi then
740
	begin
741
	  let lusi_ext = ".lusi" (* extension ^ "i" *) in
742
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@," (dirname ^ "/" ^ basename ^ lusi_ext));
743
	  print_lusi prog dirname basename lusi_ext;
744
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
745
	  exit 0
746
	end
747
      else
748
        assert false
749
    )
750
  in
751

  
752
  let machine_code = 
753
    stage2 prog 
754
  in
755

  
756
  let machine_code = Plugins.refine_machine_code prog machine_code in
757
  
758
  stage3 prog machine_code dependencies basename;
759
  begin
760
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
761
    (* We stop the process here *)
762
    exit 0
763
  end
764

  
765
let compile dirname basename extension =
766
  match extension with
767
  | ".lusi"  -> compile_header dirname basename extension
768
  | ".lus"   -> compile_source dirname basename extension
769
  | _        -> assert false
770

  
771
let anonymous filename =
772
  let ok_ext, ext = List.fold_left
773
    (fun (ok, ext) ext' ->
774
      if not ok && Filename.check_suffix filename ext' then
775
	true, ext'
776
      else
777
	ok, ext)
778
    (false, "") extensions in
779
  if ok_ext then
780
    let dirname = Filename.dirname filename in
781
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
782
    compile dirname basename ext
783
  else
784
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
785

  
786
let _ =
787
  Global.initialize ();
788
  Corelang.add_internal_funs ();
789
  try
790
    Printexc.record_backtrace true;
791

  
792
    let options = Options.options @ (Plugins.options ()) in
793
    
794
    Arg.parse options anonymous usage
795
  with
796
  | Parse.Syntax_err _ | Lexer_lustre.Error _
797
  | Types.Error (_,_) | Clocks.Error (_,_)
798
  | Corelang.Error _ (*| Task_set.Error _*)
799
  | Causality.Cycle _ -> exit 1
800
  | Sys_error msg -> (eprintf "Failure: %s@." msg)
801
  | exc -> (Utils.track_exception (); raise exc)
802

  
803
(* Local Variables: *)
804
(* compile-command:"make -C .." *)
805
(* End: *)
806
>>>>>>> salsa

Also available in: Unified diff