1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 // Code relating to taking, dropping, etc as well as type descriptors.
18 use llvm::{ValueRef, True, get_param};
20 use middle::lang_items::{FreeFnLangItem, ExchangeFreeFnLangItem};
22 use middle::trans::adt;
23 use middle::trans::base::*;
24 use middle::trans::build::*;
25 use middle::trans::callee;
26 use middle::trans::cleanup;
27 use middle::trans::cleanup::CleanupMethods;
28 use middle::trans::common::*;
29 use middle::trans::expr;
30 use middle::trans::machine::*;
31 use middle::trans::reflect;
32 use middle::trans::tvec;
33 use middle::trans::type_::Type;
34 use middle::trans::type_of::{type_of, sizing_type_of};
36 use util::ppaux::ty_to_short_str;
39 use arena::TypedArena;
40 use std::c_str::ToCStr;
44 use syntax::parse::token;
46 pub fn trans_free<'a>(cx: &'a Block<'a>, v: ValueRef) -> &'a Block<'a> {
47 let _icx = push_ctxt("trans_free");
48 callee::trans_lang_call(cx,
49 langcall(cx, None, "", FreeFnLangItem),
50 [PointerCast(cx, v, Type::i8p(cx.ccx()))],
51 Some(expr::Ignore)).bcx
54 fn trans_exchange_free<'a>(cx: &'a Block<'a>, v: ValueRef, size: u64,
55 align: u64) -> &'a Block<'a> {
56 let _icx = push_ctxt("trans_exchange_free");
58 callee::trans_lang_call(cx,
59 langcall(cx, None, "", ExchangeFreeFnLangItem),
60 [PointerCast(cx, v, Type::i8p(ccx)), C_uint(ccx, size as uint), C_uint(ccx, align as uint)],
61 Some(expr::Ignore)).bcx
64 pub fn trans_exchange_free_ty<'a>(bcx: &'a Block<'a>, ptr: ValueRef,
65 content_ty: ty::t) -> &'a Block<'a> {
66 let sizing_type = sizing_type_of(bcx.ccx(), content_ty);
67 let content_size = llsize_of_alloc(bcx.ccx(), sizing_type);
69 // `Box<ZeroSizeType>` does not allocate.
70 if content_size != 0 {
71 let content_align = llalign_of_min(bcx.ccx(), sizing_type);
72 trans_exchange_free(bcx, ptr, content_size, content_align)
78 pub fn take_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
80 // NB: v is an *alias* of type t here, not a direct value.
81 let _icx = push_ctxt("take_ty");
82 match ty::get(t).sty {
83 ty::ty_box(_) => incr_refcnt_of_boxed(bcx, v),
84 _ if ty::type_is_structural(t)
85 && ty::type_needs_drop(bcx.tcx(), t) => {
86 iter_structural_ty(bcx, v, t, take_ty)
92 pub fn get_drop_glue_type(ccx: &CrateContext, t: ty::t) -> ty::t {
94 if !ty::type_needs_drop(tcx, t) {
97 match ty::get(t).sty {
98 ty::ty_box(typ) if !ty::type_needs_drop(tcx, typ) =>
99 ty::mk_box(tcx, ty::mk_i8()),
101 ty::ty_uniq(typ) if !ty::type_needs_drop(tcx, typ) => {
102 match ty::get(typ).sty {
103 ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => t,
105 let llty = sizing_type_of(ccx, typ);
106 // `Box<ZeroSizeType>` does not allocate.
107 if llsize_of_alloc(ccx, llty) == 0 {
110 ty::mk_uniq(tcx, ty::mk_i8())
119 pub fn drop_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
121 // NB: v is an *alias* of type t here, not a direct value.
122 let _icx = push_ctxt("drop_ty");
124 if ty::type_needs_drop(bcx.tcx(), t) {
125 let glue = get_drop_glue(ccx, t);
126 let glue_type = get_drop_glue_type(ccx, t);
127 let ptr = if glue_type != t {
128 PointerCast(bcx, v, type_of(ccx, glue_type).ptr_to())
132 Call(bcx, glue, [ptr], None);
137 pub fn drop_ty_immediate<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
139 let _icx = push_ctxt("drop_ty_immediate");
140 let vp = alloca(bcx, type_of(bcx.ccx(), t), "");
145 pub fn get_drop_glue(ccx: &CrateContext, t: ty::t) -> ValueRef {
146 let t = get_drop_glue_type(ccx, t);
147 match ccx.drop_glues.borrow().find(&t) {
148 Some(&glue) => return glue,
152 let llfnty = Type::glue_fn(ccx, type_of(ccx, t).ptr_to());
153 let glue = declare_generic_glue(ccx, t, llfnty, "drop");
155 ccx.drop_glues.borrow_mut().insert(t, glue);
157 make_generic_glue(ccx, t, glue, make_drop_glue, "drop");
162 pub fn lazily_emit_visit_glue(ccx: &CrateContext, ti: &tydesc_info) -> ValueRef {
163 let _icx = push_ctxt("lazily_emit_visit_glue");
165 let llfnty = Type::glue_fn(ccx, type_of(ccx, ti.ty).ptr_to());
167 match ti.visit_glue.get() {
168 Some(visit_glue) => visit_glue,
170 debug!("+++ lazily_emit_tydesc_glue VISIT {}", ppaux::ty_to_string(ccx.tcx(), ti.ty));
171 let glue_fn = declare_generic_glue(ccx, ti.ty, llfnty, "visit");
172 ti.visit_glue.set(Some(glue_fn));
173 make_generic_glue(ccx, ti.ty, glue_fn, make_visit_glue, "visit");
174 debug!("--- lazily_emit_tydesc_glue VISIT {}", ppaux::ty_to_string(ccx.tcx(), ti.ty));
180 // See [Note-arg-mode]
181 pub fn call_visit_glue(bcx: &Block, v: ValueRef, tydesc: ValueRef) {
182 let _icx = push_ctxt("call_visit_glue");
184 // Select the glue function to call from the tydesc
185 let llfn = Load(bcx, GEPi(bcx, tydesc, [0u, abi::tydesc_field_visit_glue]));
186 let llrawptr = PointerCast(bcx, v, Type::i8p(bcx.ccx()));
188 Call(bcx, llfn, [llrawptr], None);
191 fn make_visit_glue<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
193 let _icx = push_ctxt("make_visit_glue");
195 let (visitor_trait, object_ty) = match ty::visitor_object_ty(bcx.tcx(),
199 bcx.tcx().sess.fatal(s.as_slice());
202 let v = PointerCast(bcx, v, type_of(bcx.ccx(), object_ty).ptr_to());
203 bcx = reflect::emit_calls_to_trait_visit_ty(bcx, t, v, visitor_trait.def_id);
207 fn trans_struct_drop_flag<'a>(mut bcx: &'a Block<'a>,
210 dtor_did: ast::DefId,
211 class_did: ast::DefId,
212 substs: &subst::Substs)
214 let repr = adt::represent_type(bcx.ccx(), t);
215 let drop_flag = unpack_datum!(bcx, adt::trans_drop_flag_ptr(bcx, &*repr, v0));
216 with_cond(bcx, load_ty(bcx, drop_flag.val, ty::mk_bool()), |cx| {
217 trans_struct_drop(cx, t, v0, dtor_did, class_did, substs)
221 fn trans_struct_drop<'a>(bcx: &'a Block<'a>,
224 dtor_did: ast::DefId,
225 class_did: ast::DefId,
226 substs: &subst::Substs)
228 let repr = adt::represent_type(bcx.ccx(), t);
230 // Find and call the actual destructor
231 let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, t,
234 // The second argument is the "self" argument for drop
235 let params = unsafe {
236 let ty = Type::from_ref(llvm::LLVMTypeOf(dtor_addr));
237 ty.element_type().func_params()
240 adt::fold_variants(bcx, &*repr, v0, |variant_cx, st, value| {
241 // Be sure to put all of the fields into a scope so we can use an invoke
242 // instruction to call the user destructor but still call the field
243 // destructors if the user destructor fails.
244 let field_scope = variant_cx.fcx.push_custom_cleanup_scope();
246 // Class dtors have no explicit args, so the params should
247 // just consist of the environment (self).
248 assert_eq!(params.len(), 1);
249 let self_arg = PointerCast(variant_cx, value, *params.get(0));
250 let args = vec!(self_arg);
252 // Add all the fields as a value which needs to be cleaned at the end of
254 for (i, ty) in st.fields.iter().enumerate() {
255 let llfld_a = adt::struct_field_ptr(variant_cx, &*st, value, i, false);
256 variant_cx.fcx.schedule_drop_mem(cleanup::CustomScope(field_scope),
260 let dtor_ty = ty::mk_ctor_fn(variant_cx.tcx(), ast::DUMMY_NODE_ID,
261 [get_drop_glue_type(bcx.ccx(), t)], ty::mk_nil());
262 let (_, variant_cx) = invoke(variant_cx, dtor_addr, args, dtor_ty, None);
264 variant_cx.fcx.pop_and_trans_custom_cleanup_scope(variant_cx, field_scope);
269 fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'a> {
270 // NB: v0 is an *alias* of type t here, not a direct value.
271 let _icx = push_ctxt("make_drop_glue");
272 match ty::get(t).sty {
273 ty::ty_box(body_ty) => {
274 decr_refcnt_maybe_free(bcx, v0, body_ty)
276 ty::ty_uniq(content_ty) => {
277 match ty::get(content_ty).sty {
278 ty::ty_vec(mt, None) => {
279 let llbox = Load(bcx, v0);
280 let not_null = IsNotNull(bcx, llbox);
281 with_cond(bcx, not_null, |bcx| {
282 let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, mt.ty);
283 // FIXME: #13994: the old `Box<[T]>` will not support sized deallocation
284 trans_exchange_free(bcx, llbox, 0, 8)
288 let llbox = Load(bcx, v0);
289 let not_null = IsNotNull(bcx, llbox);
290 with_cond(bcx, not_null, |bcx| {
291 let unit_ty = ty::sequence_element_type(bcx.tcx(), t);
292 let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, unit_ty);
293 // FIXME: #13994: the old `Box<str>` will not support sized deallocation
294 trans_exchange_free(bcx, llbox, 0, 8)
297 ty::ty_trait(..) => {
298 let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
299 // Only drop the value when it is non-null
300 with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {
301 let dtor_ptr = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_vtable]));
302 let dtor = Load(bcx, dtor_ptr);
305 [PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))],
311 let llbox = Load(bcx, v0);
312 let not_null = IsNotNull(bcx, llbox);
313 with_cond(bcx, not_null, |bcx| {
314 let bcx = drop_ty(bcx, llbox, content_ty);
315 trans_exchange_free_ty(bcx, llbox, content_ty)
320 ty::ty_struct(did, ref substs) | ty::ty_enum(did, ref substs) => {
322 match ty::ty_dtor(tcx, did) {
323 ty::TraitDtor(dtor, true) => {
324 trans_struct_drop_flag(bcx, t, v0, dtor, did, substs)
326 ty::TraitDtor(dtor, false) => {
327 trans_struct_drop(bcx, t, v0, dtor, did, substs)
330 // No dtor? Just the default case
331 iter_structural_ty(bcx, v0, t, drop_ty)
335 ty::ty_unboxed_closure(..) => iter_structural_ty(bcx, v0, t, drop_ty),
336 ty::ty_closure(ref f) if f.store == ty::UniqTraitStore => {
337 let box_cell_v = GEPi(bcx, v0, [0u, abi::fn_field_box]);
338 let env = Load(bcx, box_cell_v);
339 let env_ptr_ty = Type::at_box(bcx.ccx(), Type::i8(bcx.ccx())).ptr_to();
340 let env = PointerCast(bcx, env, env_ptr_ty);
341 with_cond(bcx, IsNotNull(bcx, env), |bcx| {
342 let dtor_ptr = GEPi(bcx, env, [0u, abi::box_field_tydesc]);
343 let dtor = Load(bcx, dtor_ptr);
344 let cdata = GEPi(bcx, env, [0u, abi::box_field_body]);
345 Call(bcx, dtor, [PointerCast(bcx, cdata, Type::i8p(bcx.ccx()))], None);
347 // Free the environment itself
348 // FIXME: #13994: pass align and size here
349 trans_exchange_free(bcx, env, 0, 8)
353 if ty::type_needs_drop(bcx.tcx(), t) &&
354 ty::type_is_structural(t) {
355 iter_structural_ty(bcx, v0, t, drop_ty)
363 fn decr_refcnt_maybe_free<'a>(bcx: &'a Block<'a>,
364 box_ptr_ptr: ValueRef,
365 t: ty::t) -> &'a Block<'a> {
366 let _icx = push_ctxt("decr_refcnt_maybe_free");
370 let decr_bcx = fcx.new_temp_block("decr");
371 let free_bcx = fcx.new_temp_block("free");
372 let next_bcx = fcx.new_temp_block("next");
374 let box_ptr = Load(bcx, box_ptr_ptr);
375 let llnotnull = IsNotNull(bcx, box_ptr);
376 CondBr(bcx, llnotnull, decr_bcx.llbb, next_bcx.llbb);
378 let rc_ptr = GEPi(decr_bcx, box_ptr, [0u, abi::box_field_refcnt]);
379 let rc = Sub(decr_bcx, Load(decr_bcx, rc_ptr), C_int(ccx, 1));
380 Store(decr_bcx, rc, rc_ptr);
381 CondBr(decr_bcx, IsNull(decr_bcx, rc), free_bcx.llbb, next_bcx.llbb);
383 let v = Load(free_bcx, box_ptr_ptr);
384 let body = GEPi(free_bcx, v, [0u, abi::box_field_body]);
385 let free_bcx = drop_ty(free_bcx, body, t);
386 let free_bcx = trans_free(free_bcx, v);
387 Br(free_bcx, next_bcx.llbb);
392 fn incr_refcnt_of_boxed<'a>(bcx: &'a Block<'a>,
393 box_ptr_ptr: ValueRef) -> &'a Block<'a> {
394 let _icx = push_ctxt("incr_refcnt_of_boxed");
396 let box_ptr = Load(bcx, box_ptr_ptr);
397 let rc_ptr = GEPi(bcx, box_ptr, [0u, abi::box_field_refcnt]);
398 let rc = Load(bcx, rc_ptr);
399 let rc = Add(bcx, rc, C_int(ccx, 1));
400 Store(bcx, rc, rc_ptr);
405 // Generates the declaration for (but doesn't emit) a type descriptor.
406 pub fn declare_tydesc(ccx: &CrateContext, t: ty::t) -> tydesc_info {
407 // If emit_tydescs already ran, then we shouldn't be creating any new
409 assert!(!ccx.finished_tydescs.get());
411 let llty = type_of(ccx, t);
413 if ccx.sess().count_type_sizes() {
414 println!("{}\t{}", llsize_of_real(ccx, llty),
415 ppaux::ty_to_string(ccx.tcx(), t));
418 let llsize = llsize_of(ccx, llty);
419 let llalign = llalign_of(ccx, llty);
420 let name = mangle_internal_name_by_type_and_seq(ccx, t, "tydesc");
421 debug!("+++ declare_tydesc {} {}", ppaux::ty_to_string(ccx.tcx(), t), name);
422 let gvar = name.as_slice().with_c_str(|buf| {
424 llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type().to_ref(), buf)
427 note_unique_llvm_symbol(ccx, name);
429 let ty_name = token::intern_and_get_ident(
430 ppaux::ty_to_string(ccx.tcx(), t).as_slice());
431 let ty_name = C_str_slice(ccx, ty_name);
433 debug!("--- declare_tydesc {}", ppaux::ty_to_string(ccx.tcx(), t));
440 visit_glue: Cell::new(None),
444 fn declare_generic_glue(ccx: &CrateContext, t: ty::t, llfnty: Type,
445 name: &str) -> ValueRef {
446 let _icx = push_ctxt("declare_generic_glue");
447 let fn_nm = mangle_internal_name_by_type_and_seq(
450 format!("glue_{}", name).as_slice());
451 debug!("{} is for type {}", fn_nm, ppaux::ty_to_string(ccx.tcx(), t));
452 let llfn = decl_cdecl_fn(ccx, fn_nm.as_slice(), llfnty, ty::mk_nil());
453 note_unique_llvm_symbol(ccx, fn_nm);
457 fn make_generic_glue(ccx: &CrateContext,
460 helper: <'a> |&'a Block<'a>, ValueRef, ty::t|
464 let _icx = push_ctxt("make_generic_glue");
465 let glue_name = format!("glue {} {}", name, ty_to_short_str(ccx.tcx(), t));
466 let _s = StatRecorder::new(ccx, glue_name);
468 let arena = TypedArena::new();
469 let empty_param_substs = param_substs::empty();
470 let fcx = new_fn_ctxt(ccx, llfn, -1, false, ty::mk_nil(),
471 &empty_param_substs, None, &arena, TranslateItems);
473 let bcx = init_function(&fcx, false, ty::mk_nil());
475 llvm::SetLinkage(llfn, llvm::InternalLinkage);
476 ccx.stats.n_glues_created.set(ccx.stats.n_glues_created.get() + 1u);
477 // All glue functions take values passed *by alias*; this is a
478 // requirement since in many contexts glue is invoked indirectly and
479 // the caller has no idea if it's dealing with something that can be
482 // llfn is expected be declared to take a parameter of the appropriate
483 // type, so we don't need to explicitly cast the function parameter.
485 let llrawptr0 = get_param(llfn, fcx.arg_pos(0) as c_uint);
486 let bcx = helper(bcx, llrawptr0, t);
487 finish_fn(&fcx, bcx, ty::mk_nil());
492 pub fn emit_tydescs(ccx: &CrateContext) {
493 let _icx = push_ctxt("emit_tydescs");
494 // As of this point, allow no more tydescs to be created.
495 ccx.finished_tydescs.set(true);
496 let glue_fn_ty = Type::generic_glue_fn(ccx).ptr_to();
497 for (_, ti) in ccx.tydescs.borrow().iter() {
498 // Each of the glue functions needs to be cast to a generic type
499 // before being put into the tydesc because we only have a singleton
500 // tydesc type. Then we'll recast each function to its real type when
502 let drop_glue = unsafe {
503 llvm::LLVMConstPointerCast(get_drop_glue(ccx, ti.ty), glue_fn_ty.to_ref())
505 ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() + 1);
507 match ti.visit_glue.get() {
509 ccx.stats.n_null_glues.set(ccx.stats.n_null_glues.get() +
515 ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() +
517 llvm::LLVMConstPointerCast(v, glue_fn_ty.to_ref())
522 let tydesc = C_named_struct(ccx.tydesc_type(),
525 drop_glue, // drop_glue
526 visit_glue, // visit_glue
530 let gvar = ti.tydesc;
531 llvm::LLVMSetInitializer(gvar, tydesc);
532 llvm::LLVMSetGlobalConstant(gvar, True);
533 llvm::SetLinkage(gvar, llvm::InternalLinkage);