Skip to content

Commit 48b846b

Browse files
committed
---
yaml --- r: 2216 b: refs/heads/master c: e0479c9 h: refs/heads/master v: v3
1 parent 57fcd24 commit 48b846b

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: e102413aadf4cb0e2740b26d249a710ad98cbd14
2+
refs/heads/master: e0479c902b0b5a5c521ce4d3bc38c937a80faf21

trunk/src/comp/driver/rustc.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ fn compile_input(session.session sess,
6262
bool shared,
6363
bool optimize,
6464
bool verify,
65+
bool save_temps,
6566
trans.output_type ot,
6667
vec[str] library_search_paths) {
6768
auto def = tup(0, 0);
@@ -80,7 +81,7 @@ fn compile_input(session.session sess,
8081
// FIXME: uncomment once typestate_check works
8182
// crate = typestate_check.check_crate(crate);
8283
trans.trans_crate(sess, crate, ty_cx, type_cache, output, shared,
83-
optimize, verify, ot);
84+
optimize, verify, save_temps, ot);
8485
}
8586

8687
fn pretty_print_input(session.session sess,
@@ -111,6 +112,11 @@ options:
111112
-ls list the symbols defined by a crate file
112113
-L <path> add a directory to the library search path
113114
-noverify suppress LLVM verification step (slight speedup)
115+
-parse-only parse only; do not compile, assemble, or link
116+
-O optimize
117+
-S compile only; do not assemble or link
118+
-c compile and assemble, but do not link
119+
-save-temps write intermediate files in addition to normal output
114120
-h display this message\n\n");
115121
}
116122

@@ -146,6 +152,7 @@ fn main(vec[str] args) {
146152
auto ot = trans.output_type_bitcode;
147153
let bool glue = false;
148154
let bool verify = true;
155+
let bool save_temps = false;
149156

150157
// FIXME: Maybe we should support -O0, -O1, -Os, etc
151158
let bool optimize = false;
@@ -183,6 +190,8 @@ fn main(vec[str] args) {
183190
usage(sess, args.(0));
184191
sess.err("-o requires an argument");
185192
}
193+
} else if (_str.eq(arg, "-save-temps")) {
194+
save_temps = true;
186195
} else if (_str.eq(arg, "-L")) {
187196
if (i+1u < len) {
188197
library_search_paths += vec(args.(i+1u));
@@ -221,10 +230,11 @@ fn main(vec[str] args) {
221230
alt (output_file) {
222231
case (none[str]) {
223232
middle.trans.make_common_glue("glue.bc", optimize, verify,
224-
ot);
233+
save_temps, ot);
225234
}
226235
case (some[str](?s)) {
227-
middle.trans.make_common_glue(s, optimize, verify, ot);
236+
middle.trans.make_common_glue(s, optimize, verify, save_temps,
237+
ot);
228238
}
229239
}
230240
ret;
@@ -250,12 +260,12 @@ fn main(vec[str] args) {
250260
parts += vec(".bc");
251261
auto ofile = _str.concat(parts);
252262
compile_input(sess, env, ifile, ofile, shared,
253-
optimize, verify, ot,
263+
optimize, verify, save_temps, ot,
254264
library_search_paths);
255265
}
256266
case (some[str](?ofile)) {
257267
compile_input(sess, env, ifile, ofile, shared,
258-
optimize, verify, ot,
268+
optimize, verify, save_temps, ot,
259269
library_search_paths);
260270
}
261271
}

trunk/src/comp/middle/trans.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6980,6 +6980,19 @@ tag output_type {
69806980
output_type_object;
69816981
}
69826982

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, 0u, dot_pos as uint);
6992+
}
6993+
ret stem + "." + extension;
6994+
}
6995+
69836996
fn is_object_or_assembly(output_type ot) -> bool {
69846997
if (ot == output_type_assembly) {
69856998
ret true;
@@ -6990,12 +7003,29 @@ fn is_object_or_assembly(output_type ot) -> bool {
69907003
ret false;
69917004
}
69927005

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) {
69957008
auto pm = mk_pass_manager();
69967009

69977010
// TODO: run the linter here also, once there are llvm-c bindings for it.
69987011

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+
69997029
// FIXME: This is mostly a copy of the bits of opt's -O2 that are
70007030
// available in the C api.
70017031
// 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,
70547084
llvm.LLVMAddStripDeadPrototypesPass(pm.llpm);
70557085
llvm.LLVMAddDeadTypeEliminationPass(pm.llpm);
70567086
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+
}
70577099
}
70587100

70597101
if (verify) {
70607102
llvm.LLVMAddVerifierPass(pm.llpm);
70617103
}
70627104

7105+
// TODO: Write .s if -c was specified and -save-temps was on.
70637106
if (is_object_or_assembly(ot)) {
70647107
let int LLVMAssemblyFile = 0;
70657108
let int LLVMObjectFile = 1;
@@ -7414,7 +7457,7 @@ fn make_glues(ModuleRef llmod, type_names tn) -> @glue_fns {
74147457
vec_append_glue = make_vec_append_glue(llmod, tn));
74157458
}
74167459

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,
74187461
output_type ot) {
74197462
// FIXME: part of this is repetitive and is probably a good idea
74207463
// 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,
74417484

74427485
trans_exit_task_glue(glues, new_str_hash[ValueRef](), tn, llmod);
74437486

7444-
run_passes(llmod, optimize, verify, output, ot);
7487+
run_passes(llmod, optimize, verify, save_temps, output, ot);
74457488
}
74467489

74477490
fn create_module_map(@crate_ctxt ccx) -> ValueRef {
@@ -7494,7 +7537,7 @@ fn create_crate_map(@crate_ctxt ccx) -> ValueRef {
74947537

74957538
fn trans_crate(session.session sess, @ast.crate crate, ty.ctxt tcx,
74967539
&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) {
74987541
auto llmod =
74997542
llvm.LLVMModuleCreateWithNameInContext(_str.buf("rust_out"),
75007543
llvm.LLVMGetGlobalContext());
@@ -7557,7 +7600,7 @@ fn trans_crate(session.session sess, @ast.crate crate, ty.ctxt tcx,
75577600
// Translate the metadata.
75587601
middle.metadata.write_metadata(cx.ccx, crate);
75597602

7560-
run_passes(llmod, optimize, verify, output, ot);
7603+
run_passes(llmod, optimize, verify, save_temps, output, ot);
75617604
}
75627605

75637606
//

0 commit comments

Comments
 (0)