]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/glue.rs
librustc: Don't use the same alloca for match binding which we reassign to in arm...
[rust.git] / src / librustc / middle / trans / glue.rs
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.
4 //
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.
10
11 //!
12 //
13 // Code relating to taking, dropping, etc as well as type descriptors.
14
15
16 use back::abi;
17 use back::link::*;
18 use llvm::{ValueRef, True, get_param};
19 use llvm;
20 use middle::lang_items::{FreeFnLangItem, ExchangeFreeFnLangItem};
21 use middle::subst;
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};
35 use middle::ty;
36 use util::ppaux::ty_to_short_str;
37 use util::ppaux;
38
39 use arena::TypedArena;
40 use std::c_str::ToCStr;
41 use std::cell::Cell;
42 use libc::c_uint;
43 use syntax::ast;
44 use syntax::parse::token;
45
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
52 }
53
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");
57     let ccx = cx.ccx();
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
62 }
63
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);
68
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)
73     } else {
74         bcx
75     }
76 }
77
78 pub fn take_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
79                -> &'a Block<'a> {
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)
87         }
88         _ => bcx
89     }
90 }
91
92 pub fn get_drop_glue_type(ccx: &CrateContext, t: ty::t) -> ty::t {
93     let tcx = ccx.tcx();
94     if !ty::type_needs_drop(tcx, t) {
95         return ty::mk_i8();
96     }
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()),
100
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,
104                 _ => {
105                     let llty = sizing_type_of(ccx, typ);
106                     // `Box<ZeroSizeType>` does not allocate.
107                     if llsize_of_alloc(ccx, llty) == 0 {
108                         ty::mk_i8()
109                     } else {
110                         ty::mk_uniq(tcx, ty::mk_i8())
111                     }
112                 }
113             }
114         }
115         _ => t
116     }
117 }
118
119 pub fn drop_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
120                -> &'a Block<'a> {
121     // NB: v is an *alias* of type t here, not a direct value.
122     let _icx = push_ctxt("drop_ty");
123     let ccx = bcx.ccx();
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())
129         } else {
130             v
131         };
132         Call(bcx, glue, [ptr], None);
133     }
134     bcx
135 }
136
137 pub fn drop_ty_immediate<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
138                          -> &'a Block<'a> {
139     let _icx = push_ctxt("drop_ty_immediate");
140     let vp = alloca(bcx, type_of(bcx.ccx(), t), "");
141     Store(bcx, v, vp);
142     drop_ty(bcx, vp, t)
143 }
144
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,
149         _ => { }
150     }
151
152     let llfnty = Type::glue_fn(ccx, type_of(ccx, t).ptr_to());
153     let glue = declare_generic_glue(ccx, t, llfnty, "drop");
154
155     ccx.drop_glues.borrow_mut().insert(t, glue);
156
157     make_generic_glue(ccx, t, glue, make_drop_glue, "drop");
158
159     glue
160 }
161
162 pub fn lazily_emit_visit_glue(ccx: &CrateContext, ti: &tydesc_info) -> ValueRef {
163     let _icx = push_ctxt("lazily_emit_visit_glue");
164
165     let llfnty = Type::glue_fn(ccx, type_of(ccx, ti.ty).ptr_to());
166
167     match ti.visit_glue.get() {
168         Some(visit_glue) => visit_glue,
169         None => {
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));
175             glue_fn
176         }
177     }
178 }
179
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");
183
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()));
187
188     Call(bcx, llfn, [llrawptr], None);
189 }
190
191 fn make_visit_glue<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
192                    -> &'a Block<'a> {
193     let _icx = push_ctxt("make_visit_glue");
194     let mut bcx = bcx;
195     let (visitor_trait, object_ty) = match ty::visitor_object_ty(bcx.tcx(),
196                                                                  ty::ReStatic) {
197         Ok(pair) => pair,
198         Err(s) => {
199             bcx.tcx().sess.fatal(s.as_slice());
200         }
201     };
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);
204     bcx
205 }
206
207 fn trans_struct_drop_flag<'a>(mut bcx: &'a Block<'a>,
208                               t: ty::t,
209                               v0: ValueRef,
210                               dtor_did: ast::DefId,
211                               class_did: ast::DefId,
212                               substs: &subst::Substs)
213                               -> &'a Block<'a> {
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)
218     })
219 }
220
221 fn trans_struct_drop<'a>(bcx: &'a Block<'a>,
222                          t: ty::t,
223                          v0: ValueRef,
224                          dtor_did: ast::DefId,
225                          class_did: ast::DefId,
226                          substs: &subst::Substs)
227                          -> &'a Block<'a> {
228     let repr = adt::represent_type(bcx.ccx(), t);
229
230     // Find and call the actual destructor
231     let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, t,
232                                  class_did, substs);
233
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()
238     };
239
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();
245
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);
251
252         // Add all the fields as a value which needs to be cleaned at the end of
253         // this scope.
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),
257                                              llfld_a, *ty);
258         }
259
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);
263
264         variant_cx.fcx.pop_and_trans_custom_cleanup_scope(variant_cx, field_scope);
265         variant_cx
266     })
267 }
268
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)
275         }
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)
285                     })
286                 }
287                 ty::ty_str => {
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)
295                     })
296                 }
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);
303                         Call(bcx,
304                              dtor,
305                              [PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))],
306                              None);
307                         bcx
308                     })
309                 }
310                 _ => {
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)
316                     })
317                 }
318             }
319         }
320         ty::ty_struct(did, ref substs) | ty::ty_enum(did, ref substs) => {
321             let tcx = bcx.tcx();
322             match ty::ty_dtor(tcx, did) {
323                 ty::TraitDtor(dtor, true) => {
324                     trans_struct_drop_flag(bcx, t, v0, dtor, did, substs)
325                 }
326                 ty::TraitDtor(dtor, false) => {
327                     trans_struct_drop(bcx, t, v0, dtor, did, substs)
328                 }
329                 ty::NoDtor => {
330                     // No dtor? Just the default case
331                     iter_structural_ty(bcx, v0, t, drop_ty)
332                 }
333             }
334         }
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);
346
347                 // Free the environment itself
348                 // FIXME: #13994: pass align and size here
349                 trans_exchange_free(bcx, env, 0, 8)
350             })
351         }
352         _ => {
353             if ty::type_needs_drop(bcx.tcx(), t) &&
354                 ty::type_is_structural(t) {
355                 iter_structural_ty(bcx, v0, t, drop_ty)
356             } else {
357                 bcx
358             }
359         }
360     }
361 }
362
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");
367     let fcx = bcx.fcx;
368     let ccx = bcx.ccx();
369
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");
373
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);
377
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);
382
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);
388
389     next_bcx
390 }
391
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");
395     let ccx = bcx.ccx();
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);
401     bcx
402 }
403
404
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
408     // tydescs.
409     assert!(!ccx.finished_tydescs.get());
410
411     let llty = type_of(ccx, t);
412
413     if ccx.sess().count_type_sizes() {
414         println!("{}\t{}", llsize_of_real(ccx, llty),
415                  ppaux::ty_to_string(ccx.tcx(), t));
416     }
417
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| {
423         unsafe {
424             llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type().to_ref(), buf)
425         }
426     });
427     note_unique_llvm_symbol(ccx, name);
428
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);
432
433     debug!("--- declare_tydesc {}", ppaux::ty_to_string(ccx.tcx(), t));
434     tydesc_info {
435         ty: t,
436         tydesc: gvar,
437         size: llsize,
438         align: llalign,
439         name: ty_name,
440         visit_glue: Cell::new(None),
441     }
442 }
443
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(
448         ccx,
449         t,
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);
454     return llfn;
455 }
456
457 fn make_generic_glue(ccx: &CrateContext,
458                      t: ty::t,
459                      llfn: ValueRef,
460                      helper: <'a> |&'a Block<'a>, ValueRef, ty::t|
461                                   -> &'a Block<'a>,
462                      name: &str)
463                      -> ValueRef {
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);
467
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);
472
473     let bcx = init_function(&fcx, false, ty::mk_nil());
474
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
480     // passed by value.
481     //
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.
484
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());
488
489     llfn
490 }
491
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
501         // calling it.
502         let drop_glue = unsafe {
503             llvm::LLVMConstPointerCast(get_drop_glue(ccx, ti.ty), glue_fn_ty.to_ref())
504         };
505         ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() + 1);
506         let visit_glue =
507             match ti.visit_glue.get() {
508               None => {
509                   ccx.stats.n_null_glues.set(ccx.stats.n_null_glues.get() +
510                                              1u);
511                   C_null(glue_fn_ty)
512               }
513               Some(v) => {
514                 unsafe {
515                     ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() +
516                                                1);
517                     llvm::LLVMConstPointerCast(v, glue_fn_ty.to_ref())
518                 }
519               }
520             };
521
522         let tydesc = C_named_struct(ccx.tydesc_type(),
523                                     [ti.size, // size
524                                      ti.align, // align
525                                      drop_glue, // drop_glue
526                                      visit_glue, // visit_glue
527                                      ti.name]); // name
528
529         unsafe {
530             let gvar = ti.tydesc;
531             llvm::LLVMSetInitializer(gvar, tydesc);
532             llvm::LLVMSetGlobalConstant(gvar, True);
533             llvm::SetLinkage(gvar, llvm::InternalLinkage);
534         }
535     };
536 }