@@ -6980,6 +6980,19 @@ tag output_type {
6980
6980
output_type_object;
6981
6981
}
6982
6982
6983
+ // Decides what to call an intermediate file, given the name of the output and
6984
+ // the extension to use.
6985
+ fn mk_intermediate_name ( str output_path , str extension ) -> str {
6986
+ auto dot_pos = _str. index ( output_path, '.' as u8 ) ;
6987
+ auto stem;
6988
+ if ( dot_pos < 0 ) {
6989
+ stem = output_path;
6990
+ } else {
6991
+ stem = _str. substr ( output_path, 0 u, dot_pos as uint ) ;
6992
+ }
6993
+ ret stem + "." + extension;
6994
+ }
6995
+
6983
6996
fn is_object_or_assembly ( output_type ot) -> bool {
6984
6997
if ( ot == output_type_assembly) {
6985
6998
ret true ;
@@ -6990,12 +7003,29 @@ fn is_object_or_assembly(output_type ot) -> bool {
6990
7003
ret false;
6991
7004
}
6992
7005
6993
- fn run_passes ( ModuleRef llmod, bool opt, bool verify , str output ,
6994
- output_type ot) {
7006
+ fn run_passes ( ModuleRef llmod, bool opt, bool verify , bool save_temps ,
7007
+ str output , output_type ot) {
6995
7008
auto pm = mk_pass_manager ( ) ;
6996
7009
6997
7010
// TODO: run the linter here also, once there are llvm-c bindings for it.
6998
7011
7012
+ // Generate a pre-optimization intermediate file if -save-temps was
7013
+ // specified.
7014
+ if ( save_temps) {
7015
+ alt ( ot) {
7016
+ case ( output_type_bitcode) {
7017
+ if ( opt) {
7018
+ auto filename = mk_intermediate_name ( output, "no-opt.bc" ) ;
7019
+ llvm. LLVMWriteBitcodeToFile ( llmod, _str. buf ( filename) ) ;
7020
+ }
7021
+ }
7022
+ case ( _) {
7023
+ auto filename = mk_intermediate_name ( output, "bc" ) ;
7024
+ llvm. LLVMWriteBitcodeToFile ( llmod, _str. buf ( filename) ) ;
7025
+ }
7026
+ }
7027
+ }
7028
+
6999
7029
// FIXME: This is mostly a copy of the bits of opt's -O2 that are
7000
7030
// available in the C api.
7001
7031
// FIXME2: We might want to add optmization levels like -O1, -O2, -Os, etc
@@ -7054,12 +7084,25 @@ fn run_passes(ModuleRef llmod, bool opt, bool verify, str output,
7054
7084
llvm. LLVMAddStripDeadPrototypesPass ( pm. llpm ) ;
7055
7085
llvm. LLVMAddDeadTypeEliminationPass ( pm. llpm ) ;
7056
7086
llvm. LLVMAddConstantMergePass ( pm. llpm ) ;
7087
+
7088
+ // Generate a post-optimization intermediate file if -save-temps was
7089
+ // specified.
7090
+ if ( save_temps) {
7091
+ alt ( ot) {
7092
+ case ( output_type_bitcode) { /* nothing to do */ }
7093
+ case ( _) {
7094
+ auto filename = mk_intermediate_name ( output, "opt.bc" ) ;
7095
+ llvm. LLVMWriteBitcodeToFile ( llmod, _str. buf ( filename) ) ;
7096
+ }
7097
+ }
7098
+ }
7057
7099
}
7058
7100
7059
7101
if ( verify) {
7060
7102
llvm. LLVMAddVerifierPass ( pm. llpm ) ;
7061
7103
}
7062
7104
7105
+ // TODO: Write .s if -c was specified and -save-temps was on.
7063
7106
if ( is_object_or_assembly ( ot) ) {
7064
7107
let int LLVMAssemblyFile = 0 ;
7065
7108
let int LLVMObjectFile = 1 ;
@@ -7414,7 +7457,7 @@ fn make_glues(ModuleRef llmod, type_names tn) -> @glue_fns {
7414
7457
vec_append_glue = make_vec_append_glue ( llmod, tn) ) ;
7415
7458
}
7416
7459
7417
- fn make_common_glue ( str output , bool optimize , bool verify ,
7460
+ fn make_common_glue ( str output , bool optimize , bool verify , bool save_temps ,
7418
7461
output_type ot) {
7419
7462
// FIXME: part of this is repetitive and is probably a good idea
7420
7463
// to autogen it, but things like the memcpy implementation are not
@@ -7441,7 +7484,7 @@ fn make_common_glue(str output, bool optimize, bool verify,
7441
7484
7442
7485
trans_exit_task_glue ( glues, new_str_hash[ ValueRef ] ( ) , tn, llmod) ;
7443
7486
7444
- run_passes ( llmod, optimize, verify, output, ot) ;
7487
+ run_passes ( llmod, optimize, verify, save_temps , output, ot) ;
7445
7488
}
7446
7489
7447
7490
fn create_module_map ( @crate_ctxt ccx ) -> ValueRef {
@@ -7494,7 +7537,7 @@ fn create_crate_map(@crate_ctxt ccx) -> ValueRef {
7494
7537
7495
7538
fn trans_crate ( session . session sess, @ast. crate crate, ty. ctxt tcx ,
7496
7539
& ty. type_cache type_cache , str output , bool shared ,
7497
- bool optimize , bool verify , output_type ot) {
7540
+ bool optimize , bool verify , bool save_temps , output_type ot) {
7498
7541
auto llmod =
7499
7542
llvm. LLVMModuleCreateWithNameInContext ( _str. buf ( "rust_out" ) ,
7500
7543
llvm. LLVMGetGlobalContext ( ) ) ;
@@ -7557,7 +7600,7 @@ fn trans_crate(session.session sess, @ast.crate crate, ty.ctxt tcx,
7557
7600
// Translate the metadata.
7558
7601
middle. metadata . write_metadata ( cx. ccx , crate ) ;
7559
7602
7560
- run_passes ( llmod, optimize, verify, output, ot) ;
7603
+ run_passes ( llmod, optimize, verify, save_temps , output, ot) ;
7561
7604
}
7562
7605
7563
7606
//
0 commit comments