forked from AbsInt/CompCert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Driver.ml
551 lines (508 loc) · 20.7 KB
/
Driver.ml
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the INRIA Non-Commercial License Agreement. *)
(* *)
(* *********************************************************************)
open Printf
open Commandline
open Clflags
open CommonOptions
open Timing
open Driveraux
open Frontend
open Assembler
open Linker
open Diagnostics
(* Name used for version string etc. *)
let tool_name = "C verified compiler"
(* Optional sdump suffix *)
let sdump_suffix = ref ".json"
let nolink_flag = ref false
let nolink () =
!option_c || !option_S || !option_E || !option_interp || !nolink_flag
let object_filename sourcename suff =
if nolink () then
output_filename ~final: !option_c sourcename suff ".o"
else
tmp_file ".o"
(* From CompCert C AST to asm *)
let compile_c_file sourcename ifile ofile =
(* Prepare to dump Clight, RTL, etc, if requested *)
let set_dest dst opt ext =
dst := if !opt then Some (output_filename sourcename ".c" ext)
else None in
set_dest Cprint.destination option_dparse ".parsed.c";
set_dest PrintCsyntax.destination option_dcmedium ".compcert.c";
set_dest PrintClight.destination option_dclight ".light.c";
set_dest PrintCminor.destination option_dcminor ".cm";
set_dest PrintRTL.destination option_drtl ".rtl";
set_dest Regalloc.destination_alloctrace option_dalloctrace ".alloctrace";
set_dest PrintLTL.destination option_dltl ".ltl";
set_dest PrintMach.destination option_dmach ".mach";
set_dest AsmToJSON.destination option_sdump !sdump_suffix;
(* Parse the ast *)
let csyntax = parse_c_file sourcename ifile in
(* Convert to Asm *)
let asm =
match Compiler.apply_partial
(Compiler.transf_c_program csyntax)
Asmexpand.expand_program with
| Errors.OK asm ->
asm
| Errors.Error msg ->
let loc = file_loc sourcename in
fatal_error loc "%a" print_error msg in
(* Dump Asm in binary and JSON format *)
AsmToJSON.print_if asm sourcename;
(* Print Asm in text form *)
let oc = open_out ofile in
PrintAsm.print_program oc asm;
close_out oc
(* From C source to asm *)
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
(* From Stan to asm *)
let compile_stan_file sourcename ifile ofile =
(* Prepare to dump Clight, RTL, etc, if requested *)
let set_dest dst opt ext =
dst := if !opt then Some (output_filename sourcename ".stan" ext)
else None in
set_dest Cprint.destination option_dparse ".parsed.c";
set_dest PrintCsyntax.destination option_dcmedium ".compcert.c";
set_dest PrintClight.destination option_dclight ".light.c";
set_dest PrintCStan.destination option_dcstan ".stan.c";
set_dest PrintCminor.destination option_dcminor ".cm";
set_dest PrintRTL.destination option_drtl ".rtl";
set_dest Regalloc.destination_alloctrace option_dalloctrace ".alloctrace";
set_dest PrintLTL.destination option_dltl ".ltl";
set_dest PrintMach.destination option_dmach ".mach";
set_dest AsmToJSON.destination option_sdump !sdump_suffix;
(* Parse the ast *)
let stan = Sparse.parse_stan_file sourcename ifile in
(* generate the proposal function *)
let proposal = Spropose.generate_proposal stan in
let bridge_proposal = Sbridge_propose.generate_proposal stan in
(* Generate the prelude *)
Sprelude.generate_prelude sourcename stan proposal;
(* Generate the bridge prelude *)
Sbridge_prelude.generate_prelude sourcename stan bridge_proposal;
(* Convert to Asm *)
let asm =
match Compiler.apply_partial
(Scompiler.transf_stan_program_complete stan)
Asmexpand.expand_program with
| Errors.OK asm ->
asm
| Errors.Error msg ->
let loc = file_loc sourcename in
fatal_error loc "%a" print_error msg in
(* Dump Asm in binary and JSON format *)
AsmToJSON.print_if asm sourcename;
(* Print Asm in text form *)
let oc = open_out ofile in
PrintAsm.print_program oc asm;
close_out oc;
""
(* 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
(* Processing of a .i / .p file (preprocessed C) *)
let process_i_file sourcename =
ensure_inputfile_exists sourcename;
compile_i_file sourcename sourcename
(* Processing of .S and .s files *)
let process_s_file sourcename =
ensure_inputfile_exists sourcename;
let objname = object_filename sourcename ".s" in
assemble sourcename objname;
objname
let process_S_file sourcename =
ensure_inputfile_exists sourcename;
if !option_E then begin
preprocess sourcename (output_filename_default "-");
""
end else begin
let preproname = tmp_file ".s" in
preprocess sourcename preproname;
let objname = object_filename sourcename ".S" in
assemble preproname objname;
objname
end
(* Processing of .h files *)
let process_h_file sourcename =
if !option_E then begin
ensure_inputfile_exists sourcename;
preprocess sourcename (output_filename_default "-");
""
end else
fatal_error no_loc "input file %s ignored (not in -E mode)\n" sourcename
(* TODO: this seems non portable *)
let copy_file source dest =
let _ = Sys.command (Filename.quote_command "cp" [source; dest]) in
()
;;
let invoke_c_ccomp opts fname =
let outname = " -o " ^ Filename.quote (Filename.chop_suffix fname ".c" ^ ".o") in
let cmd = (Filename.quote (Sys.executable_name) ^ " " ^ opts ^ outname ^ " " ^ Filename.quote fname) in
Printf.printf "Running %s\n" cmd;
flush stdout;
let _ = Sys.command cmd in
()
;;
let invoke_c_ccomp_link opts files outname =
let outopt = " -o " ^ Filename.quote outname in
let file_str = String.concat " " (List.map (Filename.quote) files) in
let cmd = Filename.quote (Sys.executable_name) ^ " " ^ file_str ^ outopt ^ " " ^ opts in
Printf.printf "Running %s\n" cmd;
flush stdout;
let _ = Sys.command cmd in
()
;;
let process_stan_lib sourcename =
let source_dir = Filename.dirname sourcename ^ "/" in
Printf.printf "Found stanruntime in %s going to dump object files to %s\n" Configuration.stan_runtime_path source_dir;
let runtime_name fname =
(Configuration.stan_runtime_path ^ fname)
in
copy_file (runtime_name "/jsmn.h") source_dir;
copy_file (runtime_name "/parser.h") source_dir;
copy_file (runtime_name "/Bridgeruntime.c") source_dir;
copy_file (runtime_name "/Runtime.c") source_dir;
copy_file (runtime_name "/stanlib.c") source_dir;
copy_file (runtime_name "/stanlib.h") source_dir;
copy_file (runtime_name "/parser.c") source_dir;
invoke_c_ccomp "-c" (source_dir ^ "stanlib.c");
invoke_c_ccomp "-c" (source_dir ^ "parser.c");
;;
(* Determine names for output stan files. Like output_filename but puts in source directory instead
of current directory. *)
let output_stan_filename ?(final = false) source_file source_suffix output_suffix =
match !option_o with
| Some file when final -> file
| _ ->
(Filename.chop_suffix source_file source_suffix) ^ output_suffix
let post_process_stan_file fname =
let source_dir = Filename.dirname fname ^ "/" in
let stan_o = Filename.basename ((Filename.chop_suffix fname ".s") ^ ".o") in
invoke_c_ccomp "-c" fname;
invoke_c_ccomp ("-I" ^ (Filename.quote source_dir) ^ " -c ") (source_dir ^ "prelude.c");
invoke_c_ccomp ("-I" ^ (Filename.quote source_dir) ^ " -c ") (source_dir ^ "Runtime.c");
if not (!option_c) then
invoke_c_ccomp_link "-lm"
(List.map (fun x -> source_dir ^ x) ["parser.o"; "stanlib.o"; "prelude.o"; "Runtime.o"; stan_o])
(output_stan_filename ~final:true fname ".s" ".exe")
else
()
(* Processing of a .stan file (Stan) *)
let process_stan_file sourcename =
ensure_inputfile_exists sourcename;
process_stan_lib sourcename;
let str = compile_stan_file sourcename sourcename (output_stan_filename sourcename ".stan" ".s") in
post_process_stan_file (output_stan_filename sourcename ".stan" ".s");
str
let target_help =
if Configuration.arch = "arm" && Configuration.model <> "armv6" then
{|Target processor options:
-mthumb Use Thumb2 instruction encoding
-marm Use classic ARM instruction encoding
|}
else
""
let toolchain_help =
if not Configuration.gnu_toolchain then begin
{|Toolchain options:
-t tof:env Select target processor for the diab toolchain
|} end else
""
let usage_string =
version_string tool_name ^
{|Usage: ccomp [options] <source files>
Recognized source files:
.c C source file
.i or .p C source file that should not be preprocessed
.s Assembly file
.S or .sx Assembly file that must be preprocessed
.o Object file
.a Library file
Processing options:
-c Compile to object file only (no linking), result in <file>.o
-E Preprocess only, send result to standard output
-S Compile to assembler only, save result in <file>.s
-o <file> Generate output in <file>
|} ^
prepro_help ^
language_support_help ^
DebugInit.debugging_help ^
{|Optimization options: (use -fno-<opt> to turn off -f<opt>)
-O Optimize the compiled code [on by default]
-O0 Do not optimize the compiled code
-O1 -O2 -O3 Synonymous for -O
-Os Optimize for code size in preference to code speed
-Obranchless Optimize to generate fewer conditional branches; try to produce
branch-free instruction sequences as much as possible
-ftailcalls Optimize function calls in tail position [on]
-fconst-prop Perform global constant propagation [on]
-ffloat-const-prop <n> Control constant propagation of floats
(<n>=0: none, <n>=1: limited, <n>=2: full; default is full)
-fcse Perform common subexpression elimination [on]
-fredundancy Perform redundancy elimination [on]
-finline Perform inlining of functions [on]
-finline-functions-called-once Integrate functions only required by their
single caller [on]
-fif-conversion Perform if-conversion (generation of conditional moves) [on]
Code generation options: (use -fno-<opt> to turn off -f<opt>)
-ffpu Use FP registers for some integer operations [on]
-fsmall-data <n> Set maximal size <n> for allocation in small data area
-fsmall-const <n> Set maximal size <n> for allocation in small constant area
-falign-functions <n> Set alignment (in bytes) of function entry points
-falign-branch-targets <n> Set alignment (in bytes) of branch targets
-falign-cond-branches <n> Set alignment (in bytes) of conditional branches
-fcommon Put uninitialized globals in the common section [on].
|} ^
target_help ^
toolchain_help ^
assembler_help ^
linker_help ^
{|Tracing options:
-dprepro Save C file after preprocessing in <file>.i
-dparse Save C file after parsing and elaboration in <file>.parsed.c
-dc Save generated Compcert C in <file>.compcert.c
-dclight Save generated Clight in <file>.light.c
-dstan Save generated CStan in <file>.stan.c
-dcminor Save generated Cminor in <file>.cm
-drtl Save RTL at various optimization points in <file>.rtl.<n>
-dltl Save LTL after register allocation in <file>.ltl
-dmach Save generated Mach code in <file>.mach
-dasm Save generated assembly in <file>.s
-dall Save all generated intermediate files in <file>.<ext>
-sdump Save info for post-linking validation in <file>.json
|} ^
general_help ^
warning_help ^
{|Interpreter mode:
-interp Execute given .c files using the reference interpreter
-quiet Suppress diagnostic messages for the interpreter
-trace Have the interpreter produce a detailed trace of reductions
-random Randomize execution order
-all Simulate all possible execution orders
-main <name> Start executing at function <name> instead of main()
|}
let print_usage_and_exit () =
printf "%s" usage_string; exit 0
let dump_mnemonics destfile =
let oc = open_out_bin destfile in
let pp = Format.formatter_of_out_channel oc in
AsmToJSON.pp_mnemonics pp;
Format.pp_print_flush pp ();
close_out oc;
exit 0
let optimization_options = [
option_ftailcalls; option_fifconversion; option_fconstprop; option_fcse;
option_fredundancy; option_finline; option_finline_functions_called_once;
]
let set_all opts () = List.iter (fun r -> r := true) opts
let unset_all opts () = List.iter (fun r -> r := false) opts
let num_source_files = ref 0
let num_input_files = ref 0
let cmdline_actions =
let f_opt name ref =
[Exact("-f" ^ name), Set ref; Exact("-fno-" ^ name), Unset ref] in
let check_align n =
if n <= 0 || ((n land (n - 1)) <> 0) then
error no_loc "requested alignment %d is not a power of 2" n
in
[
(* Getting help *)
Exact "-help", Unit print_usage_and_exit;
Exact "--help", Unit print_usage_and_exit;]
(* Getting version info *)
@ version_options tool_name @
(* Enforcing CompCert build numbers for QSKs and mnemonics dump *)
(if Version.buildnr <> "" then
[Exact "-dump-mnemonics", String dump_mnemonics;]
else []) @
(* Processing options *)
[ Exact "-c", Set option_c;
Exact "-E", Set option_E;
Exact "-S", Set option_S;
Exact "-o", String(fun s -> option_o := Some s);
Prefix "-o", Self (fun s -> let s = String.sub s 2 ((String.length s) - 2) in
option_o := Some s);]
(* Preprocessing options *)
@ prepro_actions @
(* Language support options *)
language_support_options
(* Debugging options *)
@ DebugInit.debugging_actions @
(* Code generation options -- more below *)
[
Exact "-O0", Unit (unset_all optimization_options);
Exact "-O", Unit (set_all optimization_options);
_Regexp "-O[123]$", Unit (set_all optimization_options);
Exact "-Os", Set option_Osize;
Exact "-Obranchless", Set option_Obranchless;
Exact "-fsmall-data", Integer(fun n -> option_small_data := n);
Exact "-fsmall-const", Integer(fun n -> option_small_const := n);
Exact "-ffloat-const-prop", Integer(fun n -> option_ffloatconstprop := n);
Exact "-falign-functions", Integer(fun n -> check_align n; option_falignfunctions := Some n);
Exact "-falign-branch-targets", Integer(fun n -> check_align n; option_falignbranchtargets := n);
Exact "-falign-cond-branches", Integer(fun n -> check_align n; option_faligncondbranchs := n);] @
f_opt "common" option_fcommon @
(* Target processor options *)
(if Configuration.arch = "arm" then
if Configuration.model = "armv6" then
[ Exact "-marm", Ignore ] (* Thumb needs ARMv6T2 or ARMv7 *)
else
[ Exact "-mthumb", Set option_mthumb;
Exact "-marm", Unset option_mthumb; ]
else []) @
(* Toolchain options *)
(if not Configuration.gnu_toolchain then
[Exact "-t", String (fun arg -> push_linker_arg "-t"; push_linker_arg arg;
prepro_options := arg :: "-t" :: !prepro_options;
assembler_options := arg :: "-t" :: !assembler_options;)]
else
[]) @
(* Assembling options *)
assembler_actions @
(* Linking options *)
linker_actions @
(* Tracing options *)
[ Exact "-dprepro", Set option_dprepro;
Exact "-dparse", Set option_dparse;
Exact "-dc", Set option_dcmedium;
Exact "-dclight", Set option_dclight;
Exact "-dcstan", Set option_dcstan;
Exact "-dcminor", Set option_dcminor;
Exact "-drtl", Set option_drtl;
Exact "-dltl", Set option_dltl;
Exact "-dalloctrace", Set option_dalloctrace;
Exact "-dmach", Set option_dmach;
Exact "-dasm", Set option_dasm;
Exact "-dall", Self (fun _ ->
option_dprepro := true;
option_dparse := true;
option_dcmedium := true;
option_dclight := true;
option_dcminor := true;
option_drtl := true;
option_dltl := true;
option_dalloctrace := true;
option_dmach := true;
option_dasm := true);
Exact "-sdump", Set option_sdump;
Exact "-sdump-suffix", String (fun s -> option_sdump := true; sdump_suffix:= s);
Exact "-sdump-folder", String (fun s -> AsmToJSON.sdump_folder := s);] @
(* General options *)
general_options @
(* Diagnostic options *)
warning_options @
(* Interpreter mode *)
[ Exact "-interp", Set option_interp;
Exact "-quiet", Unit (fun () -> Interp.trace := 0);
Exact "-trace", Unit (fun () -> Interp.trace := 2);
Exact "-random", Unit (fun () -> Interp.mode := Interp.Random);
Exact "-all", Unit (fun () -> Interp.mode := Interp.All);
Exact "-main", String (fun s -> main_function_name := s)
]
(* Optimization options *)
(* -f options: come in -f and -fno- variants *)
@ f_opt "tailcalls" option_ftailcalls
@ f_opt "if-conversion" option_fifconversion
@ f_opt "const-prop" option_fconstprop
@ f_opt "cse" option_fcse
@ f_opt "redundancy" option_fredundancy
@ f_opt "inline" option_finline
@ f_opt "inline-functions-called-once" option_finline_functions_called_once
(* Code generation options *)
@ f_opt "fpu" option_ffpu
@ f_opt "sse" option_ffpu (* backward compatibility *)
@ [
(* Catch options that are not handled *)
Prefix "-", Self (fun s ->
fatal_error no_loc "Unknown option `%s'" s);
(* File arguments *)
Suffix ".c", Self (fun s ->
push_action process_c_file s; incr num_source_files; incr num_input_files);
Suffix ".i", Self (fun s ->
push_action process_i_file s; incr num_source_files; incr num_input_files);
Suffix ".p", Self (fun s ->
push_action process_i_file s; incr num_source_files; incr num_input_files);
Suffix ".s", Self (fun s ->
push_action process_s_file s; incr num_source_files; incr num_input_files);
Suffix ".S", Self (fun s ->
push_action process_S_file s; incr num_source_files; incr num_input_files);
Suffix ".sx", Self (fun s ->
push_action process_S_file s; incr num_source_files; incr num_input_files);
Suffix ".o", Self (fun s -> push_linker_arg s; incr num_input_files);
Suffix ".a", Self (fun s -> push_linker_arg s; incr num_input_files);
(* GCC compatibility: .o.ext files and .so files are also object files *)
_Regexp ".*\\.o\\.", Self (fun s -> push_linker_arg s; incr num_input_files);
Suffix ".so", Self (fun s -> push_linker_arg s; incr num_input_files);
(* GCC compatibility: .h files can be preprocessed with -E *)
Suffix ".h", Self (fun s ->
push_action process_h_file s; incr num_source_files; incr num_input_files);
Suffix ".stan", Self (fun s -> nolink_flag := true;
push_action process_stan_file s; incr num_source_files; incr num_input_files);
]
let _ =
try
Gc.set { (Gc.get()) with
Gc.minor_heap_size = 524288; (* 512k *)
Gc.major_heap_increment = 4194304 (* 4M *)
};
Printexc.record_backtrace true;
Frontend.init ();
parse_cmdline cmdline_actions;
DebugInit.init (); (* Initialize the debug functions *)
if nolink () && !option_o <> None && !num_source_files >= 2 then
fatal_error no_loc "ambiguous '-o' option (multiple source files)";
if !num_input_files = 0 then
fatal_error no_loc "no input file";
if not !option_interp && !main_function_name <> "main" then
fatal_error no_loc "option '-main' requires option '-interp'";
let linker_args = time "Total compilation time" perform_actions () in
if not (nolink ()) && linker_args <> [] then begin
linker (output_filename_default "a.out") linker_args
end;
check_errors ()
with
| Sys_error msg
| CmdError msg -> error no_loc "%s" msg; exit 2
| Abort -> error_summary (); exit 2
| e -> crash e