enable comments in generated asm, ll

This commit is contained in:
Niko Matsakis 2011-11-14 14:03:20 -08:00
parent 96cdfa11db
commit cb9675259f
4 changed files with 36 additions and 6 deletions

View file

@ -263,6 +263,7 @@ options:
--opt-level <lvl> optimize with possible levels 0-3
-O equivalent to --opt-level=2
-S compile only; do not assemble or link
--no-asm-comments do not add comments into the assembly source
-c compile and assemble, but do not link
--emit-llvm produce an LLVM bitcode file
--save-temps write intermediate files in addition to normal output
@ -365,6 +366,11 @@ fn build_session_options(match: getopts::match)
let run_typestate = !opt_present(match, "no-typestate");
let sysroot_opt = getopts::opt_maybe_str(match, "sysroot");
let target_opt = getopts::opt_maybe_str(match, "target");
let no_asm_comments = getopts::opt_present(match, "no-asm-comments");
alt output_type { // unless we're emitting huamn-readable assembly, omit comments.
link::output_type_llvm_assembly. | link::output_type_assembly. {}
_ { no_asm_comments = true; }
}
let opt_level: uint =
if opt_present(match, "O") {
if opt_present(match, "opt-level") {
@ -414,7 +420,8 @@ fn build_session_options(match: getopts::match)
parse_only: parse_only,
no_trans: no_trans,
do_gc: do_gc,
stack_growth: stack_growth};
stack_growth: stack_growth,
no_asm_comments: no_asm_comments};
ret sopts;
}
@ -453,7 +460,8 @@ fn opts() -> [getopts::opt] {
optflag("no-typestate"), optflag("noverify"),
optmulti("cfg"), optflag("test"),
optflag("lib"), optflag("static"), optflag("gc"),
optflag("stack-growth")];
optflag("stack-growth"), optflag("check-unsafe"),
optflag("no-asm-comments")];
}
fn build_output_filenames(ifile: str, ofile: option::t<str>,

View file

@ -43,7 +43,8 @@ type options =
parse_only: bool,
no_trans: bool,
do_gc: bool,
stack_growth: bool};
stack_growth: bool,
no_asm_comments: bool};
type crate_metadata = {name: str, data: [u8]};

View file

@ -24,7 +24,7 @@ import back::{link, abi, upcall};
import syntax::{ast, ast_util};
import syntax::visit;
import syntax::codemap::span;
import syntax::print::pprust::{expr_to_str};
import syntax::print::pprust::{expr_to_str, path_to_str, stmt_to_str};
import visit::vt;
import util::common::*;
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)
fn trans_stmt(cx: @block_ctxt, s: ast::stmt) -> @block_ctxt {
// FIXME Fill in cx.sp
add_span_comment(cx, s.span, stmt_to_str(s));
let bcx = cx;
alt s.node {
ast::stmt_expr(e, _) { bcx = trans_expr(cx, e, ignore); }

View file

@ -1,10 +1,11 @@
import std::{vec, str};
import std::str::sbuf;
import lib::llvm::llvm;
import syntax::codemap::span;
import llvm::{ValueRef, TypeRef, BasicBlockRef, BuilderRef, Opcode,
ModuleRef};
import trans_common::{block_ctxt, T_ptr, T_nil, T_i8, T_i1,
val_ty, C_i32};
import trans_common::{block_ctxt, T_ptr, T_nil, T_int, T_i8, T_i1, T_void,
T_fn, val_ty, bcx_ccx, C_i32};
fn B(cx: @block_ctxt) -> BuilderRef {
let b = *cx.fcx.lcx.ccx.builder;
@ -504,6 +505,24 @@ fn _UndefReturn(cx: @block_ctxt, Fn: ValueRef) -> ValueRef {
ret llvm::LLVMGetUndef(retty);
}
fn add_span_comment(bcx: @block_ctxt, sp: span, text: str) {
let ccx = bcx_ccx(bcx);
if (!ccx.sess.get_opts().no_asm_comments) {
add_comment(bcx, text + " (" + ccx.sess.span_str(sp) + ")");
}
}
fn add_comment(bcx: @block_ctxt, text: str) {
let ccx = bcx_ccx(bcx);
if (!ccx.sess.get_opts().no_asm_comments) {
let comment_text = "; " + text;
let asm = str::as_buf(comment_text, { |c|
str::as_buf("", { |e|
llvm::LLVMConstInlineAsm(T_fn([], T_void()), c, e, 0, 0)})});
Call(bcx, asm, []);
}
}
fn Call(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef]) -> ValueRef {
if cx.unreachable { ret _UndefReturn(cx, Fn); }
unsafe {