aboutsummaryrefslogtreecommitdiffstats
path: root/extraction/driver.ml
blob: eb14d4510f2a3405589c2c7637356772e0e7f7d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
open Compcert

let parse_c_file sourcename ifile =
  (* Simplification options *)
  let simplifs =
    "b" (* blocks: mandatory *)
    ^ (if false then "s" else "")
    ^ (if false then "f" else "")
    ^ (if false then "p" else "")
  in
  (* Parsing and production of a simplified C AST *)
  let ast = Parse.preprocessed_file simplifs sourcename ifile in
  (* Conversion to Csyntax *)
  let csyntax = Timing.time "CompCert C generation" C2C.convertProgram ast in
  (* Save CompCert C AST if requested *)
  PrintCsyntax.print_if csyntax;
  csyntax

let compile_c_file sourcename ifile ofile =
  (* Parse the ast *)
  let csyntax = parse_c_file sourcename ifile in
  (* Convert to Asm *)
  let rtl =
    match Compiler.apply_partial
            (CoqUp.transf_frontend csyntax) with
    | Errors.OK rtl ->
       rtl
    | Errors.Error msg ->
       let loc = file_loc sourcename in
       fatal_error loc "%a"  print_error msg in
  (* Print Asm in text form *)
  let oc = open_out ofile in
  PrintAsm.print_program oc asm;
  close_out oc

let compile_i_file sourcename preproname =
  if !option_interp then begin
      Machine.config := Machine.compcert_interpreter !Machine.config;
      let csyntax = parse_c_file sourcename preproname in
      Interp.execute csyntax;
      ""
    end else if !option_S then begin
      compile_c_file sourcename preproname
        (output_filename ~final:true sourcename ".c" ".s");
      ""
    end else begin
      let asmname =
        if !option_dasm
        then output_filename sourcename ".c" ".s"
        else tmp_file ".s" in
      compile_c_file sourcename preproname asmname;
      let objname = object_filename sourcename ".c" in
      assemble asmname objname;
      objname
    end

(* Processing of a .c file *)

let process_c_file sourcename =
  ensure_inputfile_exists sourcename;
  if !option_E then begin
      preprocess sourcename (output_filename_default "-");
      ""
    end else begin
      let preproname = if !option_dprepro then
                         output_filename sourcename ".c" ".i"
                       else
                         tmp_file ".i" in
      preprocess sourcename preproname;
      compile_i_file sourcename preproname
    end

let _ = print_endline "Hello world"