Skip to content

Commit

Permalink
trans: Use fmt::Debug for debugging instead of ad-hoc methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Mar 17, 2016
1 parent abb1515 commit b47fcb8
Show file tree
Hide file tree
Showing 29 changed files with 280 additions and 392 deletions.
1 change: 1 addition & 0 deletions src/librustc_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub enum DLLStorageClassTypes {
}

bitflags! {
#[derive(Debug)]
flags Attribute : u64 {
const ZExt = 1 << 0,
const SExt = 1 << 1,
Expand Down
65 changes: 26 additions & 39 deletions src/librustc_trans/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ use trans::monomorphize;
use trans::tvec;
use trans::type_of;
use trans::Disr;
use trans::value::Value;
use middle::ty::{self, Ty, TyCtxt};
use middle::traits::ProjectionMode;
use session::config::NoDebugInfo;
Expand Down Expand Up @@ -448,6 +449,12 @@ impl<'tcx> Datum<'tcx, Lvalue> {
}
}

impl fmt::Debug for MatchInput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&Value(self.val), f)
}
}

impl MatchInput {
fn from_val(val: ValueRef) -> MatchInput {
MatchInput {
Expand All @@ -466,11 +473,8 @@ fn expand_nested_bindings<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
col: usize,
val: MatchInput)
-> Vec<Match<'a, 'p, 'blk, 'tcx>> {
debug!("expand_nested_bindings(bcx={}, m={:?}, col={}, val={})",
bcx.to_str(),
m,
col,
bcx.val_to_string(val.val));
debug!("expand_nested_bindings(bcx={}, m={:?}, col={}, val={:?})",
bcx.to_str(), m, col, val);
let _indenter = indenter();

m.iter().map(|br| {
Expand Down Expand Up @@ -506,11 +510,8 @@ fn enter_match<'a, 'b, 'p, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
-> Vec<Match<'a, 'p, 'blk, 'tcx>> where
F: FnMut(&[&'p hir::Pat]) -> Option<Vec<&'p hir::Pat>>,
{
debug!("enter_match(bcx={}, m={:?}, col={}, val={})",
bcx.to_str(),
m,
col,
bcx.val_to_string(val.val));
debug!("enter_match(bcx={}, m={:?}, col={}, val={:?})",
bcx.to_str(), m, col, val);
let _indenter = indenter();

m.iter().filter_map(|br| {
Expand Down Expand Up @@ -549,11 +550,8 @@ fn enter_default<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
col: usize,
val: MatchInput)
-> Vec<Match<'a, 'p, 'blk, 'tcx>> {
debug!("enter_default(bcx={}, m={:?}, col={}, val={})",
bcx.to_str(),
m,
col,
bcx.val_to_string(val.val));
debug!("enter_default(bcx={}, m={:?}, col={}, val={:?})",
bcx.to_str(), m, col, val);
let _indenter = indenter();

// Collect all of the matches that can match against anything.
Expand Down Expand Up @@ -606,12 +604,8 @@ fn enter_opt<'a, 'p, 'blk, 'tcx>(
variant_size: usize,
val: MatchInput)
-> Vec<Match<'a, 'p, 'blk, 'tcx>> {
debug!("enter_opt(bcx={}, m={:?}, opt={:?}, col={}, val={})",
bcx.to_str(),
m,
*opt,
col,
bcx.val_to_string(val.val));
debug!("enter_opt(bcx={}, m={:?}, opt={:?}, col={}, val={:?})",
bcx.to_str(), m, *opt, col, val);
let _indenter = indenter();

let ctor = match opt {
Expand Down Expand Up @@ -1032,7 +1026,7 @@ fn insert_lllocals<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
bcx.fcx.schedule_drop_and_fill_mem(cs, llval, binding_info.ty, opt_datum);
}

debug!("binding {} to {}", binding_info.id, bcx.val_to_string(llval));
debug!("binding {} to {:?}", binding_info.id, Value(llval));
bcx.fcx.lllocals.borrow_mut().insert(binding_info.id, datum);
debuginfo::create_match_binding_metadata(bcx, name, binding_info);
}
Expand All @@ -1047,11 +1041,8 @@ fn compile_guard<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
chk: &FailureHandler,
has_genuine_default: bool)
-> Block<'blk, 'tcx> {
debug!("compile_guard(bcx={}, guard_expr={:?}, m={:?}, vals=[{}])",
bcx.to_str(),
guard_expr,
m,
vals.iter().map(|v| bcx.val_to_string(v.val)).collect::<Vec<_>>().join(", "));
debug!("compile_guard(bcx={}, guard_expr={:?}, m={:?}, vals={:?})",
bcx.to_str(), guard_expr, m, vals);
let _indenter = indenter();

let mut bcx = insert_lllocals(bcx, &data.bindings_map, None);
Expand Down Expand Up @@ -1093,10 +1084,8 @@ fn compile_submatch<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
vals: &[MatchInput],
chk: &FailureHandler,
has_genuine_default: bool) {
debug!("compile_submatch(bcx={}, m={:?}, vals=[{}])",
bcx.to_str(),
m,
vals.iter().map(|v| bcx.val_to_string(v.val)).collect::<Vec<_>>().join(", "));
debug!("compile_submatch(bcx={}, m={:?}, vals=[{:?}])",
bcx.to_str(), m, vals);
let _indenter = indenter();
let _icx = push_ctxt("match::compile_submatch");
let mut bcx = bcx;
Expand Down Expand Up @@ -1256,7 +1245,7 @@ fn compile_submatch_continue<'a, 'p, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
debug!("options={:?}", opts);
let mut kind = NoBranch;
let mut test_val = val.val;
debug!("test_val={}", bcx.val_to_string(test_val));
debug!("test_val={:?}", Value(test_val));
if !opts.is_empty() {
match opts[0] {
ConstantValue(..) | ConstantRange(..) => {
Expand Down Expand Up @@ -1761,8 +1750,8 @@ fn mk_binding_alloca<'blk, 'tcx, A, F>(bcx: Block<'blk, 'tcx>,
let lvalue = Lvalue::new_with_hint(caller_name, bcx, p_id, HintKind::DontZeroJustUse);
let datum = Datum::new(llval, var_ty, lvalue);

debug!("mk_binding_alloca cleanup_scope={:?} llval={} var_ty={:?}",
cleanup_scope, bcx.ccx().tn().val_to_string(llval), var_ty);
debug!("mk_binding_alloca cleanup_scope={:?} llval={:?} var_ty={:?}",
cleanup_scope, Value(llval), var_ty);

// Subtle: be sure that we *populate* the memory *before*
// we schedule the cleanup.
Expand Down Expand Up @@ -1794,10 +1783,8 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
val: MatchInput,
cleanup_scope: cleanup::ScopeId)
-> Block<'blk, 'tcx> {
debug!("bind_irrefutable_pat(bcx={}, pat={:?}, val={})",
bcx.to_str(),
pat,
bcx.val_to_string(val.val));
debug!("bind_irrefutable_pat(bcx={}, pat={:?}, val={:?})",
bcx.to_str(), pat, val);

if bcx.sess().asm_comments() {
add_comment(bcx, &format!("bind_irrefutable_pat(pat={:?})",
Expand Down Expand Up @@ -1923,7 +1910,7 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
// a regular one
if !type_is_sized(tcx, fty) {
let scratch = alloc_ty(bcx, fty, "__struct_field_fat_ptr");
debug!("Creating fat pointer {}", bcx.val_to_string(scratch));
debug!("Creating fat pointer {:?}", Value(scratch));
Store(bcx, fldptr, expr::get_dataptr(bcx, scratch));
Store(bcx, val.meta, expr::get_meta(bcx, scratch));
fldptr = scratch;
Expand Down
34 changes: 14 additions & 20 deletions src/librustc_trans/trans/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use trans::machine;
use trans::monomorphize;
use trans::type_::Type;
use trans::type_of;
use trans::value::Value;

type Hint = attr::ReprAttr;

Expand All @@ -88,11 +89,6 @@ impl TypeContext {
fn may_need_drop_flag(t: Type, needs_drop_flag: bool) -> TypeContext {
TypeContext { prefix: t, needs_drop_flag: needs_drop_flag }
}
pub fn to_string(self) -> String {
let TypeContext { prefix, needs_drop_flag } = self;
format!("TypeContext {{ prefix: {}, needs_drop_flag: {} }}",
prefix.to_string(), needs_drop_flag)
}
}

/// Representations.
Expand Down Expand Up @@ -1139,9 +1135,8 @@ pub fn struct_field_ptr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, st: &Struct<'tcx>, v

// There's no metadata available, log the case and just do the GEP.
if !val.has_meta() {
debug!("Unsized field `{}`, of `{}` has no metadata for adjustment",
ix,
bcx.val_to_string(ptr_val));
debug!("Unsized field `{}`, of `{:?}` has no metadata for adjustment",
ix, Value(ptr_val));
return StructGEP(bcx, ptr_val, ix);
}

Expand Down Expand Up @@ -1189,16 +1184,15 @@ pub fn struct_field_ptr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, st: &Struct<'tcx>, v
Neg(bcx, align, dbloc),
dbloc);

debug!("struct_field_ptr: DST field offset: {}",
bcx.val_to_string(offset));
debug!("struct_field_ptr: DST field offset: {:?}", Value(offset));

// Cast and adjust pointer
let byte_ptr = PointerCast(bcx, ptr_val, Type::i8p(bcx.ccx()));
let byte_ptr = GEP(bcx, byte_ptr, &[offset]);

// Finally, cast back to the type expected
let ll_fty = type_of::in_memory_type_of(bcx.ccx(), fty);
debug!("struct_field_ptr: Field type is {}", ll_fty.to_string());
debug!("struct_field_ptr: Field type is {:?}", ll_fty);
PointerCast(bcx, byte_ptr, ll_fty.ptr_to())
}

Expand Down Expand Up @@ -1442,7 +1436,7 @@ fn padding(ccx: &CrateContext, size: u64) -> ValueRef {
fn roundup(x: u64, a: u32) -> u64 { let a = a as u64; ((x + (a - 1)) / a) * a }

/// Get the discriminant of a constant value.
pub fn const_get_discrim(ccx: &CrateContext, r: &Repr, val: ValueRef) -> Disr {
pub fn const_get_discrim(r: &Repr, val: ValueRef) -> Disr {
match *r {
CEnum(ity, _, _) => {
match ity {
Expand All @@ -1452,13 +1446,13 @@ pub fn const_get_discrim(ccx: &CrateContext, r: &Repr, val: ValueRef) -> Disr {
}
General(ity, _, _) => {
match ity {
attr::SignedInt(..) => Disr(const_to_int(const_get_elt(ccx, val, &[0])) as u64),
attr::UnsignedInt(..) => Disr(const_to_uint(const_get_elt(ccx, val, &[0])))
attr::SignedInt(..) => Disr(const_to_int(const_get_elt(val, &[0])) as u64),
attr::UnsignedInt(..) => Disr(const_to_uint(const_get_elt(val, &[0])))
}
}
Univariant(..) => Disr(0),
RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
ccx.sess().bug("const discrim access of non c-like enum")
unreachable!("const discrim access of non c-like enum")
}
}
}
Expand All @@ -1472,25 +1466,25 @@ pub fn const_get_field(ccx: &CrateContext, r: &Repr, val: ValueRef,
_discr: Disr, ix: usize) -> ValueRef {
match *r {
CEnum(..) => ccx.sess().bug("element access in C-like enum const"),
Univariant(..) => const_struct_field(ccx, val, ix),
General(..) => const_struct_field(ccx, val, ix + 1),
Univariant(..) => const_struct_field(val, ix),
General(..) => const_struct_field(val, ix + 1),
RawNullablePointer { .. } => {
assert_eq!(ix, 0);
val
},
StructWrappedNullablePointer{ .. } => const_struct_field(ccx, val, ix)
StructWrappedNullablePointer{ .. } => const_struct_field(val, ix)
}
}

/// Extract field of struct-like const, skipping our alignment padding.
fn const_struct_field(ccx: &CrateContext, val: ValueRef, ix: usize) -> ValueRef {
fn const_struct_field(val: ValueRef, ix: usize) -> ValueRef {
// Get the ix-th non-undef element of the struct.
let mut real_ix = 0; // actual position in the struct
let mut ix = ix; // logical index relative to real_ix
let mut field;
loop {
loop {
field = const_get_elt(ccx, val, &[real_ix]);
field = const_get_elt(val, &[real_ix]);
if !is_undef(field) {
break;
}
Expand Down
16 changes: 6 additions & 10 deletions src/librustc_trans/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,9 +948,9 @@ pub fn invoke<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
}

if need_invoke(bcx) {
debug!("invoking {} at {:?}", bcx.val_to_string(llfn), bcx.llbb);
debug!("invoking {:?} at {:?}", Value(llfn), bcx.llbb);
for &llarg in llargs {
debug!("arg: {}", bcx.val_to_string(llarg));
debug!("arg: {:?}", Value(llarg));
}
let normal_bcx = bcx.fcx.new_temp_block("normal-return");
let landing_pad = bcx.fcx.get_landing_pad();
Expand All @@ -964,9 +964,9 @@ pub fn invoke<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
debug_loc);
return (llresult, normal_bcx);
} else {
debug!("calling {} at {:?}", bcx.val_to_string(llfn), bcx.llbb);
debug!("calling {:?} at {:?}", Value(llfn), bcx.llbb);
for &llarg in llargs {
debug!("arg: {}", bcx.val_to_string(llarg));
debug!("arg: {:?}", Value(llarg));
}

let llresult = Call(bcx, llfn, &llargs[..], Some(attributes), debug_loc);
Expand Down Expand Up @@ -1058,10 +1058,7 @@ pub fn store_ty<'blk, 'tcx>(cx: Block<'blk, 'tcx>, v: ValueRef, dst: ValueRef, t
return;
}

debug!("store_ty: {} : {:?} <- {}",
cx.val_to_string(dst),
t,
cx.val_to_string(v));
debug!("store_ty: {:?} : {:?} <- {:?}", Value(dst), t, Value(v));

if common::type_is_fat_ptr(cx.tcx(), t) {
Store(cx,
Expand Down Expand Up @@ -2030,8 +2027,7 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
debug!("trans_closure: monomorphized_arg_type: {:?}",
monomorphized_arg_type);
}
debug!("trans_closure: function lltype: {}",
bcx.fcx.ccx.tn().val_to_string(bcx.fcx.llfn));
debug!("trans_closure: function lltype: {:?}", Value(bcx.fcx.llfn));

let has_tupled_arg = match closure_env {
closure::ClosureEnv::NotClosure => abi == Abi::RustCall,
Expand Down
9 changes: 6 additions & 3 deletions src/librustc_trans/trans/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use syntax::codemap::Span;

use trans::builder::Builder;
use trans::type_::Type;
use trans::value::Value;
use trans::debuginfo::DebugLoc;

use libc::{c_uint, c_char};
Expand Down Expand Up @@ -146,9 +147,11 @@ pub fn Invoke(cx: Block,
}
check_not_terminated(cx);
terminate(cx, "Invoke");
debug!("Invoke({} with arguments ({}))",
cx.val_to_string(fn_),
args.iter().map(|a| cx.val_to_string(*a)).collect::<Vec<String>>().join(", "));
debug!("Invoke({:?} with arguments ({}))",
Value(fn_),
args.iter().map(|a| {
format!("{:?}", Value(*a))
}).collect::<Vec<String>>().join(", "));
debug_loc.apply(cx.fcx);
let bundle = cx.lpad().and_then(|b| b.bundle());
B(cx).invoke(fn_, args, then, catch, bundle, attributes)
Expand Down
Loading

0 comments on commit b47fcb8

Please sign in to comment.