Skip to content

Commit cb96752

Browse files
committed
enable comments in generated asm, ll
1 parent 96cdfa1 commit cb96752

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/comp/driver/rustc.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ options:
263263
--opt-level <lvl> optimize with possible levels 0-3
264264
-O equivalent to --opt-level=2
265265
-S compile only; do not assemble or link
266+
--no-asm-comments do not add comments into the assembly source
266267
-c compile and assemble, but do not link
267268
--emit-llvm produce an LLVM bitcode file
268269
--save-temps write intermediate files in addition to normal output
@@ -365,6 +366,11 @@ fn build_session_options(match: getopts::match)
365366
let run_typestate = !opt_present(match, "no-typestate");
366367
let sysroot_opt = getopts::opt_maybe_str(match, "sysroot");
367368
let target_opt = getopts::opt_maybe_str(match, "target");
369+
let no_asm_comments = getopts::opt_present(match, "no-asm-comments");
370+
alt output_type { // unless we're emitting huamn-readable assembly, omit comments.
371+
link::output_type_llvm_assembly. | link::output_type_assembly. {}
372+
_ { no_asm_comments = true; }
373+
}
368374
let opt_level: uint =
369375
if opt_present(match, "O") {
370376
if opt_present(match, "opt-level") {
@@ -414,7 +420,8 @@ fn build_session_options(match: getopts::match)
414420
parse_only: parse_only,
415421
no_trans: no_trans,
416422
do_gc: do_gc,
417-
stack_growth: stack_growth};
423+
stack_growth: stack_growth,
424+
no_asm_comments: no_asm_comments};
418425
ret sopts;
419426
}
420427

@@ -453,7 +460,8 @@ fn opts() -> [getopts::opt] {
453460
optflag("no-typestate"), optflag("noverify"),
454461
optmulti("cfg"), optflag("test"),
455462
optflag("lib"), optflag("static"), optflag("gc"),
456-
optflag("stack-growth")];
463+
optflag("stack-growth"), optflag("check-unsafe"),
464+
optflag("no-asm-comments")];
457465
}
458466

459467
fn build_output_filenames(ifile: str, ofile: option::t<str>,

src/comp/driver/session.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type options =
4343
parse_only: bool,
4444
no_trans: bool,
4545
do_gc: bool,
46-
stack_growth: bool};
46+
stack_growth: bool,
47+
no_asm_comments: bool};
4748

4849
type crate_metadata = {name: str, data: [u8]};
4950

src/comp/middle/trans.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import back::{link, abi, upcall};
2424
import syntax::{ast, ast_util};
2525
import syntax::visit;
2626
import syntax::codemap::span;
27-
import syntax::print::pprust::{expr_to_str};
27+
import syntax::print::pprust::{expr_to_str, path_to_str, stmt_to_str};
2828
import visit::vt;
2929
import util::common::*;
3030
import lib::llvm::{llvm, mk_target_data, mk_type_names};
@@ -4605,6 +4605,8 @@ fn zero_alloca(cx: @block_ctxt, llptr: ValueRef, t: ty::t)
46054605
fn trans_stmt(cx: @block_ctxt, s: ast::stmt) -> @block_ctxt {
46064606
// FIXME Fill in cx.sp
46074607

4608+
add_span_comment(cx, s.span, stmt_to_str(s));
4609+
46084610
let bcx = cx;
46094611
alt s.node {
46104612
ast::stmt_expr(e, _) { bcx = trans_expr(cx, e, ignore); }

src/comp/middle/trans_build.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import std::{vec, str};
22
import std::str::sbuf;
33
import lib::llvm::llvm;
4+
import syntax::codemap::span;
45
import llvm::{ValueRef, TypeRef, BasicBlockRef, BuilderRef, Opcode,
56
ModuleRef};
6-
import trans_common::{block_ctxt, T_ptr, T_nil, T_i8, T_i1,
7-
val_ty, C_i32};
7+
import trans_common::{block_ctxt, T_ptr, T_nil, T_int, T_i8, T_i1, T_void,
8+
T_fn, val_ty, bcx_ccx, C_i32};
89

910
fn B(cx: @block_ctxt) -> BuilderRef {
1011
let b = *cx.fcx.lcx.ccx.builder;
@@ -504,6 +505,24 @@ fn _UndefReturn(cx: @block_ctxt, Fn: ValueRef) -> ValueRef {
504505
ret llvm::LLVMGetUndef(retty);
505506
}
506507

508+
fn add_span_comment(bcx: @block_ctxt, sp: span, text: str) {
509+
let ccx = bcx_ccx(bcx);
510+
if (!ccx.sess.get_opts().no_asm_comments) {
511+
add_comment(bcx, text + " (" + ccx.sess.span_str(sp) + ")");
512+
}
513+
}
514+
515+
fn add_comment(bcx: @block_ctxt, text: str) {
516+
let ccx = bcx_ccx(bcx);
517+
if (!ccx.sess.get_opts().no_asm_comments) {
518+
let comment_text = "; " + text;
519+
let asm = str::as_buf(comment_text, { |c|
520+
str::as_buf("", { |e|
521+
llvm::LLVMConstInlineAsm(T_fn([], T_void()), c, e, 0, 0)})});
522+
Call(bcx, asm, []);
523+
}
524+
}
525+
507526
fn Call(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef]) -> ValueRef {
508527
if cx.unreachable { ret _UndefReturn(cx, Fn); }
509528
unsafe {

0 commit comments

Comments
 (0)