1 // Copyright 2012-2014 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.
11 #![allow(non_camel_case_types, non_snake_case)]
13 //! Code that is useful in various trans modules.
15 pub use self::ExprOrMethodCall::*;
19 use llvm::{ValueRef, BasicBlockRef, BuilderRef, ContextRef, TypeKind};
20 use llvm::{True, False, Bool, OperandBundleDef};
23 use middle::def_id::DefId;
25 use middle::lang_items::LangItem;
26 use middle::subst::{self, Substs};
29 use trans::builder::Builder;
34 use trans::debuginfo::{self, DebugLoc};
37 use trans::monomorphize;
38 use trans::type_::Type;
41 use middle::ty::{self, Ty};
42 use middle::ty::fold::{TypeFolder, TypeFoldable};
44 use rustc::mir::repr::Mir;
45 use util::nodemap::{FnvHashMap, NodeMap};
47 use arena::TypedArena;
48 use libc::{c_uint, c_char};
50 use std::ffi::CString;
51 use std::cell::{Cell, RefCell};
54 use syntax::codemap::{DUMMY_SP, Span};
55 use syntax::parse::token::InternedString;
56 use syntax::parse::token;
58 pub use trans::context::CrateContext;
60 /// Is the type's representation size known at compile time?
61 pub fn type_is_sized<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
62 ty.is_sized(&tcx.empty_parameter_environment(), DUMMY_SP)
65 pub fn type_is_fat_ptr<'tcx>(cx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
67 ty::TyRawPtr(ty::TypeAndMut{ty, ..}) |
68 ty::TyRef(_, ty::TypeAndMut{ty, ..}) |
70 !type_is_sized(cx, ty)
78 fn type_is_newtype_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
80 ty::TyStruct(def, substs) => {
81 let fields = &def.struct_variant().fields;
82 fields.len() == 1 && {
83 type_is_immediate(ccx, monomorphize::field_ty(ccx.tcx(), substs, &fields[0]))
90 pub fn type_is_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
91 use trans::machine::llsize_of_alloc;
92 use trans::type_of::sizing_type_of;
95 let simple = ty.is_scalar() ||
96 ty.is_unique() || ty.is_region_ptr() ||
97 type_is_newtype_immediate(ccx, ty) ||
99 if simple && !type_is_fat_ptr(tcx, ty) {
102 if !type_is_sized(tcx, ty) {
106 ty::TyStruct(..) | ty::TyEnum(..) | ty::TyTuple(..) | ty::TyArray(_, _) |
107 ty::TyClosure(..) => {
108 let llty = sizing_type_of(ccx, ty);
109 llsize_of_alloc(ccx, llty) <= llsize_of_alloc(ccx, ccx.int_type())
111 _ => type_is_zero_size(ccx, ty)
115 /// Identify types which have size zero at runtime.
116 pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
117 use trans::machine::llsize_of_alloc;
118 use trans::type_of::sizing_type_of;
119 let llty = sizing_type_of(ccx, ty);
120 llsize_of_alloc(ccx, llty) == 0
123 /// Identifies types which we declare to be equivalent to `void` in C for the purpose of function
124 /// return types. These are `()`, bot, uninhabited enums and all other zero-sized types.
125 pub fn return_type_is_void<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
126 ty.is_nil() || ty.is_empty(ccx.tcx()) || type_is_zero_size(ccx, ty)
129 /// Generates a unique symbol based off the name given. This is used to create
130 /// unique symbols for things like closures.
131 pub fn gensym_name(name: &str) -> ast::Name {
132 let num = token::gensym(name).0;
133 // use one colon which will get translated to a period by the mangler, and
134 // we're guaranteed that `num` is globally unique for this crate.
135 token::gensym(&format!("{}:{}", name, num))
139 * A note on nomenclature of linking: "extern", "foreign", and "upcall".
141 * An "extern" is an LLVM symbol we wind up emitting an undefined external
142 * reference to. This means "we don't have the thing in this compilation unit,
143 * please make sure you link it in at runtime". This could be a reference to
144 * C code found in a C library, or rust code found in a rust crate.
146 * Most "externs" are implicitly declared (automatically) as a result of a
147 * user declaring an extern _module_ dependency; this causes the rust driver
148 * to locate an extern crate, scan its compilation metadata, and emit extern
149 * declarations for any symbols used by the declaring crate.
151 * A "foreign" is an extern that references C (or other non-rust ABI) code.
152 * There is no metadata to scan for extern references so in these cases either
153 * a header-digester like bindgen, or manual function prototypes, have to
154 * serve as declarators. So these are usually given explicitly as prototype
155 * declarations, in rust code, with ABI attributes on them noting which ABI to
158 * An "upcall" is a foreign call generated by the compiler (not corresponding
159 * to any user-written call in the code) into the runtime library, to perform
160 * some helper task such as bringing a task to life, allocating memory, etc.
166 #[derive(Copy, Clone)]
167 pub struct NodeIdAndSpan {
172 pub fn expr_info(expr: &hir::Expr) -> NodeIdAndSpan {
173 NodeIdAndSpan { id: expr.id, span: expr.span }
176 /// The concrete version of ty::FieldDef. The name is the field index if
177 /// the field is numeric.
178 pub struct Field<'tcx>(pub ast::Name, pub Ty<'tcx>);
180 /// The concrete version of ty::VariantDef
181 pub struct VariantInfo<'tcx> {
183 pub fields: Vec<Field<'tcx>>
186 impl<'tcx> VariantInfo<'tcx> {
187 pub fn from_ty(tcx: &ty::ctxt<'tcx>,
189 opt_def: Option<Def>)
193 ty::TyStruct(adt, substs) | ty::TyEnum(adt, substs) => {
194 let variant = match opt_def {
195 None => adt.struct_variant(),
196 Some(def) => adt.variant_of_def(def)
200 discr: Disr::from(variant.disr_val),
201 fields: variant.fields.iter().map(|f| {
202 Field(f.name, monomorphize::field_ty(tcx, substs, f))
207 ty::TyTuple(ref v) => {
210 fields: v.iter().enumerate().map(|(i, &t)| {
211 Field(token::intern(&i.to_string()), t)
217 tcx.sess.bug(&format!(
218 "cannot get field types from the type {:?}",
224 /// Return the variant corresponding to a given node (e.g. expr)
225 pub fn of_node(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>, id: ast::NodeId) -> Self {
226 let node_def = tcx.def_map.borrow().get(&id).map(|v| v.full_def());
227 Self::from_ty(tcx, ty, node_def)
230 pub fn field_index(&self, name: ast::Name) -> usize {
231 self.fields.iter().position(|&Field(n,_)| n == name).unwrap_or_else(|| {
232 panic!("unknown field `{}`", name)
237 pub struct BuilderRef_res {
241 impl Drop for BuilderRef_res {
244 llvm::LLVMDisposeBuilder(self.b);
249 pub fn BuilderRef_res(b: BuilderRef) -> BuilderRef_res {
255 pub type ExternMap = FnvHashMap<String, ValueRef>;
257 pub fn validate_substs(substs: &Substs) {
258 assert!(!substs.types.needs_infer());
261 // work around bizarre resolve errors
262 type RvalueDatum<'tcx> = datum::Datum<'tcx, datum::Rvalue>;
263 pub type LvalueDatum<'tcx> = datum::Datum<'tcx, datum::Lvalue>;
265 #[derive(Clone, Debug)]
266 struct HintEntry<'tcx> {
267 // The datum for the dropflag-hint itself; note that many
268 // source-level Lvalues will be associated with the same
269 // dropflag-hint datum.
270 datum: cleanup::DropHintDatum<'tcx>,
273 pub struct DropFlagHintsMap<'tcx> {
274 // Maps NodeId for expressions that read/write unfragmented state
275 // to that state's drop-flag "hint." (A stack-local hint
276 // indicates either that (1.) it is certain that no-drop is
277 // needed, or (2.) inline drop-flag must be consulted.)
278 node_map: NodeMap<HintEntry<'tcx>>,
281 impl<'tcx> DropFlagHintsMap<'tcx> {
282 pub fn new() -> DropFlagHintsMap<'tcx> { DropFlagHintsMap { node_map: NodeMap() } }
283 pub fn has_hint(&self, id: ast::NodeId) -> bool { self.node_map.contains_key(&id) }
284 pub fn insert(&mut self, id: ast::NodeId, datum: cleanup::DropHintDatum<'tcx>) {
285 self.node_map.insert(id, HintEntry { datum: datum });
287 pub fn hint_datum(&self, id: ast::NodeId) -> Option<cleanup::DropHintDatum<'tcx>> {
288 self.node_map.get(&id).map(|t|t.datum)
292 // Function context. Every LLVM function we create will have one of
294 pub struct FunctionContext<'a, 'tcx: 'a> {
295 // The MIR for this function. At present, this is optional because
296 // we only have MIR available for things that are local to the
298 pub mir: Option<&'a Mir<'tcx>>,
300 // The ValueRef returned from a call to llvm::LLVMAddFunction; the
301 // address of the first instruction in the sequence of
302 // instructions for this function that will go in the .text
303 // section of the executable we're generating.
306 // always an empty parameter-environment NOTE: @jroesch another use of ParamEnv
307 pub param_env: ty::ParameterEnvironment<'a, 'tcx>,
309 // The environment argument in a closure.
310 pub llenv: Option<ValueRef>,
312 // A pointer to where to store the return value. If the return type is
313 // immediate, this points to an alloca in the function. Otherwise, it's a
314 // pointer to the hidden first parameter of the function. After function
315 // construction, this should always be Some.
316 pub llretslotptr: Cell<Option<ValueRef>>,
318 // These pub elements: "hoisted basic blocks" containing
319 // administrative activities that have to happen in only one place in
320 // the function, due to LLVM's quirks.
321 // A marker for the place where we want to insert the function's static
322 // allocas, so that LLVM will coalesce them into a single alloca call.
323 pub alloca_insert_pt: Cell<Option<ValueRef>>,
324 pub llreturn: Cell<Option<BasicBlockRef>>,
326 // If the function has any nested return's, including something like:
327 // fn foo() -> Option<Foo> { Some(Foo { x: return None }) }, then
328 // we use a separate alloca for each return
329 pub needs_ret_allocas: bool,
331 // When working with landingpad-based exceptions this value is alloca'd and
332 // later loaded when using the resume instruction. This ends up being
333 // critical to chaining landing pads and resuing already-translated
336 // Note that for cleanuppad-based exceptions this is not used.
337 pub landingpad_alloca: Cell<Option<ValueRef>>,
339 // True if the caller expects this fn to use the out pointer to
340 // return. Either way, your code should write into the slot llretslotptr
341 // points to, but if this value is false, that slot will be a local alloca.
342 pub caller_expects_out_pointer: bool,
344 // Maps the DefId's for local variables to the allocas created for
345 // them in llallocas.
346 pub lllocals: RefCell<NodeMap<LvalueDatum<'tcx>>>,
348 // Same as above, but for closure upvars
349 pub llupvars: RefCell<NodeMap<ValueRef>>,
351 // Carries info about drop-flags for local bindings (longer term,
352 // paths) for the code being compiled.
353 pub lldropflag_hints: RefCell<DropFlagHintsMap<'tcx>>,
355 // The NodeId of the function, or -1 if it doesn't correspond to
356 // a user-defined function.
359 // If this function is being monomorphized, this contains the type
360 // substitutions used.
361 pub param_substs: &'tcx Substs<'tcx>,
363 // The source span and nesting context where this function comes from, for
364 // error reporting and symbol generation.
365 pub span: Option<Span>,
367 // The arena that blocks are allocated from.
368 pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>,
370 // This function's enclosing crate context.
371 pub ccx: &'a CrateContext<'a, 'tcx>,
373 // Used and maintained by the debuginfo module.
374 pub debug_context: debuginfo::FunctionDebugContext,
377 pub scopes: RefCell<Vec<cleanup::CleanupScope<'a, 'tcx>>>,
379 pub cfg: Option<cfg::CFG>,
382 impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
383 pub fn mir(&self) -> &'a Mir<'tcx> {
387 pub fn arg_offset(&self) -> usize {
388 self.env_arg_pos() + if self.llenv.is_some() { 1 } else { 0 }
391 pub fn env_arg_pos(&self) -> usize {
392 if self.caller_expects_out_pointer {
399 pub fn cleanup(&self) {
401 llvm::LLVMInstructionEraseFromParent(self.alloca_insert_pt
407 pub fn get_llreturn(&self) -> BasicBlockRef {
408 if self.llreturn.get().is_none() {
410 self.llreturn.set(Some(unsafe {
411 llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(), self.llfn,
412 "return\0".as_ptr() as *const _)
416 self.llreturn.get().unwrap()
419 pub fn get_ret_slot(&self, bcx: Block<'a, 'tcx>,
420 output: ty::FnOutput<'tcx>,
421 name: &str) -> ValueRef {
422 if self.needs_ret_allocas {
423 base::alloca(bcx, match output {
424 ty::FnConverging(output_type) => type_of::type_of(bcx.ccx(), output_type),
425 ty::FnDiverging => Type::void(bcx.ccx())
428 self.llretslotptr.get().unwrap()
432 pub fn new_block(&'a self,
434 opt_node_id: Option<ast::NodeId>)
437 let name = CString::new(name).unwrap();
438 let llbb = llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(),
441 BlockS::new(llbb, opt_node_id, self)
445 pub fn new_id_block(&'a self,
447 node_id: ast::NodeId)
449 self.new_block(name, Some(node_id))
452 pub fn new_temp_block(&'a self,
455 self.new_block(name, None)
458 pub fn join_blocks(&'a self,
460 in_cxs: &[Block<'a, 'tcx>])
462 let out = self.new_id_block("join", id);
463 let mut reachable = false;
465 if !bcx.unreachable.get() {
466 build::Br(*bcx, out.llbb, DebugLoc::None);
471 build::Unreachable(out);
476 pub fn monomorphize<T>(&self, value: &T) -> T
477 where T : TypeFoldable<'tcx>
479 monomorphize::apply_param_substs(self.ccx.tcx(),
484 /// This is the same as `common::type_needs_drop`, except that it
485 /// may use or update caches within this `FunctionContext`.
486 pub fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
487 self.ccx.tcx().type_needs_drop_given_env(ty, &self.param_env)
490 pub fn eh_personality(&self) -> ValueRef {
491 // The exception handling personality function.
493 // If our compilation unit has the `eh_personality` lang item somewhere
494 // within it, then we just need to translate that. Otherwise, we're
495 // building an rlib which will depend on some upstream implementation of
496 // this function, so we just codegen a generic reference to it. We don't
497 // specify any of the types for the function, we just make it a symbol
498 // that LLVM can later use.
500 // Note that MSVC is a little special here in that we don't use the
501 // `eh_personality` lang item at all. Currently LLVM has support for
502 // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
503 // *name of the personality function* to decide what kind of unwind side
504 // tables/landing pads to emit. It looks like Dwarf is used by default,
505 // injecting a dependency on the `_Unwind_Resume` symbol for resuming
506 // an "exception", but for MSVC we want to force SEH. This means that we
507 // can't actually have the personality function be our standard
508 // `rust_eh_personality` function, but rather we wired it up to the
509 // CRT's custom personality function, which forces LLVM to consider
510 // landing pads as "landing pads for SEH".
511 let target = &self.ccx.sess().target.target;
512 match self.ccx.tcx().lang_items.eh_personality() {
513 Some(def_id) if !base::wants_msvc_seh(self.ccx.sess()) => {
514 callee::trans_fn_ref(self.ccx, def_id, ExprId(0),
515 self.param_substs).val
518 let mut personality = self.ccx.eh_personality().borrow_mut();
520 Some(llpersonality) => llpersonality,
522 let name = if !base::wants_msvc_seh(self.ccx.sess()) {
523 "rust_eh_personality"
524 } else if target.arch == "x86" {
527 "__C_specific_handler"
529 let fty = Type::variadic_func(&[], &Type::i32(self.ccx));
530 let f = declare::declare_cfn(self.ccx, name, fty,
531 self.ccx.tcx().types.i32);
532 *personality = Some(f);
540 // Returns a ValueRef of the "eh_unwind_resume" lang item if one is defined,
541 // otherwise declares it as an external funtion.
542 pub fn eh_unwind_resume(&self) -> ValueRef {
543 use trans::attributes;
544 assert!(self.ccx.sess().target.target.options.custom_unwind_resume);
545 match self.ccx.tcx().lang_items.eh_unwind_resume() {
547 callee::trans_fn_ref(self.ccx, def_id, ExprId(0),
548 self.param_substs).val
551 let mut unwresume = self.ccx.eh_unwind_resume().borrow_mut();
555 let fty = Type::func(&[Type::i8p(self.ccx)], &Type::void(self.ccx));
556 let llfn = declare::declare_fn(self.ccx,
557 "rust_eh_unwind_resume",
559 fty, ty::FnDiverging);
560 attributes::unwind(llfn, true);
561 *unwresume = Some(llfn);
570 // Basic block context. We create a block context for each basic block
571 // (single-entry, single-exit sequence of instructions) we generate from Rust
572 // code. Each basic block we generate is attached to a function, typically
573 // with many basic blocks per function. All the basic blocks attached to a
574 // function are organized as a directed graph.
575 pub struct BlockS<'blk, 'tcx: 'blk> {
576 // The BasicBlockRef returned from a call to
577 // llvm::LLVMAppendBasicBlock(llfn, name), which adds a basic
578 // block to the function pointed to by llfn. We insert
579 // instructions into that block by way of this block context.
580 // The block pointing to this one in the function's digraph.
581 pub llbb: BasicBlockRef,
582 pub terminated: Cell<bool>,
583 pub unreachable: Cell<bool>,
585 // If this block part of a landing pad, then this is `Some` indicating what
586 // kind of landing pad its in, otherwise this is none.
587 pub lpad: RefCell<Option<LandingPad>>,
589 // AST node-id associated with this block, if any. Used for
590 // debugging purposes only.
591 pub opt_node_id: Option<ast::NodeId>,
593 // The function context for the function to which this block is
595 pub fcx: &'blk FunctionContext<'blk, 'tcx>,
598 pub type Block<'blk, 'tcx> = &'blk BlockS<'blk, 'tcx>;
600 impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
601 pub fn new(llbb: BasicBlockRef,
602 opt_node_id: Option<ast::NodeId>,
603 fcx: &'blk FunctionContext<'blk, 'tcx>)
604 -> Block<'blk, 'tcx> {
605 fcx.block_arena.alloc(BlockS {
607 terminated: Cell::new(false),
608 unreachable: Cell::new(false),
609 lpad: RefCell::new(None),
610 opt_node_id: opt_node_id,
615 pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
618 pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
621 pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> {
624 pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() }
626 pub fn mir(&self) -> &'blk Mir<'tcx> {
630 pub fn name(&self, name: ast::Name) -> String {
634 pub fn node_id_to_string(&self, id: ast::NodeId) -> String {
635 self.tcx().map.node_to_string(id).to_string()
638 pub fn def(&self, nid: ast::NodeId) -> Def {
639 match self.tcx().def_map.borrow().get(&nid) {
640 Some(v) => v.full_def(),
642 self.tcx().sess.bug(&format!(
643 "no def associated with node id {}", nid));
648 pub fn val_to_string(&self, val: ValueRef) -> String {
649 self.ccx().tn().val_to_string(val)
652 pub fn llty_str(&self, ty: Type) -> String {
653 self.ccx().tn().type_to_string(ty)
656 pub fn to_str(&self) -> String {
657 format!("[block {:p}]", self)
660 pub fn monomorphize<T>(&self, value: &T) -> T
661 where T : TypeFoldable<'tcx>
663 monomorphize::apply_param_substs(self.tcx(),
664 self.fcx.param_substs,
668 pub fn build(&'blk self) -> BlockAndBuilder<'blk, 'tcx> {
669 BlockAndBuilder::new(self, OwnedBuilder::new_with_ccx(self.ccx()))
673 pub struct OwnedBuilder<'blk, 'tcx: 'blk> {
674 builder: Builder<'blk, 'tcx>
677 impl<'blk, 'tcx> OwnedBuilder<'blk, 'tcx> {
678 pub fn new_with_ccx(ccx: &'blk CrateContext<'blk, 'tcx>) -> Self {
679 // Create a fresh builder from the crate context.
680 let llbuilder = unsafe {
681 llvm::LLVMCreateBuilderInContext(ccx.llcx())
685 llbuilder: llbuilder,
692 impl<'blk, 'tcx> Drop for OwnedBuilder<'blk, 'tcx> {
695 llvm::LLVMDisposeBuilder(self.builder.llbuilder);
700 pub struct BlockAndBuilder<'blk, 'tcx: 'blk> {
701 bcx: Block<'blk, 'tcx>,
702 owned_builder: OwnedBuilder<'blk, 'tcx>,
705 impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> {
706 pub fn new(bcx: Block<'blk, 'tcx>, owned_builder: OwnedBuilder<'blk, 'tcx>) -> Self {
707 // Set the builder's position to this block's end.
708 owned_builder.builder.position_at_end(bcx.llbb);
711 owned_builder: owned_builder,
715 pub fn with_block<F, R>(&self, f: F) -> R
716 where F: FnOnce(Block<'blk, 'tcx>) -> R
718 let result = f(self.bcx);
719 self.position_at_end(self.bcx.llbb);
723 pub fn map_block<F>(self, f: F) -> Self
724 where F: FnOnce(Block<'blk, 'tcx>) -> Block<'blk, 'tcx>
726 let BlockAndBuilder { bcx, owned_builder } = self;
728 BlockAndBuilder::new(bcx, owned_builder)
731 // Methods delegated to bcx
733 pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
736 pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
739 pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> {
742 pub fn sess(&self) -> &'blk Session {
746 pub fn llbb(&self) -> BasicBlockRef {
750 pub fn mir(&self) -> &'blk Mir<'tcx> {
754 pub fn val_to_string(&self, val: ValueRef) -> String {
755 self.bcx.val_to_string(val)
758 pub fn monomorphize<T>(&self, value: &T) -> T
759 where T: TypeFoldable<'tcx>
761 self.bcx.monomorphize(value)
765 impl<'blk, 'tcx> Deref for BlockAndBuilder<'blk, 'tcx> {
766 type Target = Builder<'blk, 'tcx>;
767 fn deref(&self) -> &Self::Target {
768 &self.owned_builder.builder
772 /// A structure representing an active landing pad for the duration of a basic
775 /// Each `Block` may contain an instance of this, indicating whether the block
776 /// is part of a landing pad or not. This is used to make decision about whether
777 /// to emit `invoke` instructions (e.g. in a landing pad we don't continue to
778 /// use `invoke`) and also about various function call metadata.
780 /// For GNU exceptions (`landingpad` + `resume` instructions) this structure is
781 /// just a bunch of `None` instances (not too interesting), but for MSVC
782 /// exceptions (`cleanuppad` + `cleanupret` instructions) this contains data.
783 /// When inside of a landing pad, each function call in LLVM IR needs to be
784 /// annotated with which landing pad it's a part of. This is accomplished via
785 /// the `OperandBundleDef` value created for MSVC landing pads.
786 pub struct LandingPad {
787 cleanuppad: Option<ValueRef>,
788 operand: Option<OperandBundleDef>,
792 pub fn gnu() -> LandingPad {
793 LandingPad { cleanuppad: None, operand: None }
796 pub fn msvc(cleanuppad: ValueRef) -> LandingPad {
798 cleanuppad: Some(cleanuppad),
799 operand: Some(OperandBundleDef::new("funclet", &[cleanuppad])),
803 pub fn bundle(&self) -> Option<&OperandBundleDef> {
804 self.operand.as_ref()
808 impl Clone for LandingPad {
809 fn clone(&self) -> LandingPad {
811 cleanuppad: self.cleanuppad,
812 operand: self.cleanuppad.map(|p| {
813 OperandBundleDef::new("funclet", &[p])
819 pub struct Result<'blk, 'tcx: 'blk> {
820 pub bcx: Block<'blk, 'tcx>,
824 impl<'b, 'tcx> Result<'b, 'tcx> {
825 pub fn new(bcx: Block<'b, 'tcx>, val: ValueRef) -> Result<'b, 'tcx> {
833 pub fn val_ty(v: ValueRef) -> Type {
835 Type::from_ref(llvm::LLVMTypeOf(v))
839 // LLVM constant constructors.
840 pub fn C_null(t: Type) -> ValueRef {
842 llvm::LLVMConstNull(t.to_ref())
846 pub fn C_undef(t: Type) -> ValueRef {
848 llvm::LLVMGetUndef(t.to_ref())
852 pub fn C_integral(t: Type, u: u64, sign_extend: bool) -> ValueRef {
854 llvm::LLVMConstInt(t.to_ref(), u, sign_extend as Bool)
858 pub fn C_floating(s: &str, t: Type) -> ValueRef {
860 let s = CString::new(s).unwrap();
861 llvm::LLVMConstRealOfString(t.to_ref(), s.as_ptr())
865 pub fn C_floating_f64(f: f64, t: Type) -> ValueRef {
867 llvm::LLVMConstReal(t.to_ref(), f)
871 pub fn C_nil(ccx: &CrateContext) -> ValueRef {
872 C_struct(ccx, &[], false)
875 pub fn C_bool(ccx: &CrateContext, val: bool) -> ValueRef {
876 C_integral(Type::i1(ccx), val as u64, false)
879 pub fn C_i32(ccx: &CrateContext, i: i32) -> ValueRef {
880 C_integral(Type::i32(ccx), i as u64, true)
883 pub fn C_u32(ccx: &CrateContext, i: u32) -> ValueRef {
884 C_integral(Type::i32(ccx), i as u64, false)
887 pub fn C_u64(ccx: &CrateContext, i: u64) -> ValueRef {
888 C_integral(Type::i64(ccx), i, false)
891 pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
894 let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
897 // make sure it doesn't overflow
898 assert!(v < (1<<(bit_size-1)) && v >= -(1<<(bit_size-1)));
901 C_integral(ccx.int_type(), v as u64, true)
904 pub fn C_uint<I: AsU64>(ccx: &CrateContext, i: I) -> ValueRef {
907 let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
910 // make sure it doesn't overflow
911 assert!(v < (1<<bit_size));
914 C_integral(ccx.int_type(), v, false)
917 pub trait AsI64 { fn as_i64(self) -> i64; }
918 pub trait AsU64 { fn as_u64(self) -> u64; }
920 // FIXME: remove the intptr conversions, because they
921 // are host-architecture-dependent
922 impl AsI64 for i64 { fn as_i64(self) -> i64 { self as i64 }}
923 impl AsI64 for i32 { fn as_i64(self) -> i64 { self as i64 }}
924 impl AsI64 for isize { fn as_i64(self) -> i64 { self as i64 }}
926 impl AsU64 for u64 { fn as_u64(self) -> u64 { self as u64 }}
927 impl AsU64 for u32 { fn as_u64(self) -> u64 { self as u64 }}
928 impl AsU64 for usize { fn as_u64(self) -> u64 { self as u64 }}
930 pub fn C_u8(ccx: &CrateContext, i: u8) -> ValueRef {
931 C_integral(Type::i8(ccx), i as u64, false)
935 // This is a 'c-like' raw string, which differs from
936 // our boxed-and-length-annotated strings.
937 pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> ValueRef {
939 match cx.const_cstr_cache().borrow().get(&s) {
940 Some(&llval) => return llval,
944 let sc = llvm::LLVMConstStringInContext(cx.llcx(),
945 s.as_ptr() as *const c_char,
947 !null_terminated as Bool);
949 let gsym = token::gensym("str");
950 let sym = format!("str{}", gsym.0);
951 let g = declare::define_global(cx, &sym[..], val_ty(sc)).unwrap_or_else(||{
952 cx.sess().bug(&format!("symbol `{}` is already defined", sym));
954 llvm::LLVMSetInitializer(g, sc);
955 llvm::LLVMSetGlobalConstant(g, True);
956 llvm::SetLinkage(g, llvm::InternalLinkage);
958 cx.const_cstr_cache().borrow_mut().insert(s, g);
963 // NB: Do not use `do_spill_noroot` to make this into a constant string, or
964 // you will be kicked off fast isel. See issue #4352 for an example of this.
965 pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
967 let cs = consts::ptrcast(C_cstr(cx, s, false), Type::i8p(cx));
968 C_named_struct(cx.tn().find_type("str_slice").unwrap(), &[cs, C_uint(cx, len)])
971 pub fn C_struct(cx: &CrateContext, elts: &[ValueRef], packed: bool) -> ValueRef {
972 C_struct_in_context(cx.llcx(), elts, packed)
975 pub fn C_struct_in_context(llcx: ContextRef, elts: &[ValueRef], packed: bool) -> ValueRef {
977 llvm::LLVMConstStructInContext(llcx,
978 elts.as_ptr(), elts.len() as c_uint,
983 pub fn C_named_struct(t: Type, elts: &[ValueRef]) -> ValueRef {
985 llvm::LLVMConstNamedStruct(t.to_ref(), elts.as_ptr(), elts.len() as c_uint)
989 pub fn C_array(ty: Type, elts: &[ValueRef]) -> ValueRef {
991 return llvm::LLVMConstArray(ty.to_ref(), elts.as_ptr(), elts.len() as c_uint);
995 pub fn C_vector(elts: &[ValueRef]) -> ValueRef {
997 return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
1001 pub fn C_bytes(cx: &CrateContext, bytes: &[u8]) -> ValueRef {
1002 C_bytes_in_context(cx.llcx(), bytes)
1005 pub fn C_bytes_in_context(llcx: ContextRef, bytes: &[u8]) -> ValueRef {
1007 let ptr = bytes.as_ptr() as *const c_char;
1008 return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
1012 pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint])
1015 let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
1017 debug!("const_get_elt(v={}, us={:?}, r={})",
1018 cx.tn().val_to_string(v), us, cx.tn().val_to_string(r));
1024 pub fn const_to_int(v: ValueRef) -> i64 {
1026 llvm::LLVMConstIntGetSExtValue(v)
1030 pub fn const_to_uint(v: ValueRef) -> u64 {
1032 llvm::LLVMConstIntGetZExtValue(v)
1036 fn is_const_integral(v: ValueRef) -> bool {
1038 !llvm::LLVMIsAConstantInt(v).is_null()
1042 pub fn const_to_opt_int(v: ValueRef) -> Option<i64> {
1044 if is_const_integral(v) {
1045 Some(llvm::LLVMConstIntGetSExtValue(v))
1052 pub fn const_to_opt_uint(v: ValueRef) -> Option<u64> {
1054 if is_const_integral(v) {
1055 Some(llvm::LLVMConstIntGetZExtValue(v))
1062 pub fn is_undef(val: ValueRef) -> bool {
1064 llvm::LLVMIsUndef(val) != False
1068 #[allow(dead_code)] // potentially useful
1069 pub fn is_null(val: ValueRef) -> bool {
1071 llvm::LLVMIsNull(val) != False
1075 pub fn monomorphize_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, t: Ty<'tcx>) -> Ty<'tcx> {
1076 bcx.fcx.monomorphize(&t)
1079 pub fn node_id_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, id: ast::NodeId) -> Ty<'tcx> {
1080 let tcx = bcx.tcx();
1081 let t = tcx.node_id_to_type(id);
1082 monomorphize_type(bcx, t)
1085 pub fn expr_ty<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, ex: &hir::Expr) -> Ty<'tcx> {
1086 node_id_type(bcx, ex.id)
1089 pub fn expr_ty_adjusted<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, ex: &hir::Expr) -> Ty<'tcx> {
1090 monomorphize_type(bcx, bcx.tcx().expr_ty_adjusted(ex))
1093 /// Attempts to resolve an obligation. The result is a shallow vtable resolution -- meaning that we
1094 /// do not (necessarily) resolve all nested obligations on the impl. Note that type check should
1095 /// guarantee to us that all nested obligations *could be* resolved if we wanted to.
1096 pub fn fulfill_obligation<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1098 trait_ref: ty::PolyTraitRef<'tcx>)
1099 -> traits::Vtable<'tcx, ()>
1101 let tcx = ccx.tcx();
1103 // Remove any references to regions; this helps improve caching.
1104 let trait_ref = tcx.erase_regions(&trait_ref);
1106 // First check the cache.
1107 match ccx.trait_cache().borrow().get(&trait_ref) {
1109 info!("Cache hit: {:?}", trait_ref);
1110 return (*vtable).clone();
1115 debug!("trans fulfill_obligation: trait_ref={:?} def_id={:?}",
1116 trait_ref, trait_ref.def_id());
1119 // Do the initial selection for the obligation. This yields the
1120 // shallow result we are looking for -- that is, what specific impl.
1121 let infcx = infer::normalizing_infer_ctxt(tcx, &tcx.tables);
1122 let mut selcx = traits::SelectionContext::new(&infcx);
1125 traits::Obligation::new(traits::ObligationCause::misc(span, ast::DUMMY_NODE_ID),
1126 trait_ref.to_poly_trait_predicate());
1127 let selection = match selcx.select(&obligation) {
1128 Ok(Some(selection)) => selection,
1130 // Ambiguity can happen when monomorphizing during trans
1131 // expands to some humongo type that never occurred
1132 // statically -- this humongo type can then overflow,
1133 // leading to an ambiguous result. So report this as an
1134 // overflow bug, since I believe this is the only case
1135 // where ambiguity can result.
1136 debug!("Encountered ambiguity selecting `{:?}` during trans, \
1137 presuming due to overflow",
1139 ccx.sess().span_fatal(
1141 "reached the recursion limit during monomorphization (selection ambiguity)");
1146 &format!("Encountered error `{:?}` selecting `{:?}` during trans",
1152 // Currently, we use a fulfillment context to completely resolve
1153 // all nested obligations. This is because they can inform the
1154 // inference of the impl's type parameters.
1155 let mut fulfill_cx = infcx.fulfillment_cx.borrow_mut();
1156 let vtable = selection.map(|predicate| {
1157 fulfill_cx.register_predicate_obligation(&infcx, predicate);
1159 let vtable = infer::drain_fulfillment_cx_or_panic(
1160 span, &infcx, &mut fulfill_cx, &vtable
1163 info!("Cache miss: {:?} => {:?}", trait_ref, vtable);
1165 ccx.trait_cache().borrow_mut().insert(trait_ref, vtable.clone());
1170 /// Normalizes the predicates and checks whether they hold. If this
1171 /// returns false, then either normalize encountered an error or one
1172 /// of the predicates did not hold. Used when creating vtables to
1173 /// check for unsatisfiable methods.
1174 pub fn normalize_and_test_predicates<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1175 predicates: Vec<ty::Predicate<'tcx>>)
1178 debug!("normalize_and_test_predicates(predicates={:?})",
1181 let tcx = ccx.tcx();
1182 let infcx = infer::normalizing_infer_ctxt(tcx, &tcx.tables);
1183 let mut selcx = traits::SelectionContext::new(&infcx);
1184 let mut fulfill_cx = infcx.fulfillment_cx.borrow_mut();
1185 let cause = traits::ObligationCause::dummy();
1186 let traits::Normalized { value: predicates, obligations } =
1187 traits::normalize(&mut selcx, cause.clone(), &predicates);
1188 for obligation in obligations {
1189 fulfill_cx.register_predicate_obligation(&infcx, obligation);
1191 for predicate in predicates {
1192 let obligation = traits::Obligation::new(cause.clone(), predicate);
1193 fulfill_cx.register_predicate_obligation(&infcx, obligation);
1196 infer::drain_fulfillment_cx(&infcx, &mut fulfill_cx, &()).is_ok()
1199 // Key used to lookup values supplied for type parameters in an expr.
1200 #[derive(Copy, Clone, PartialEq, Debug)]
1201 pub enum ExprOrMethodCall {
1202 // Type parameters for a path like `None::<int>`
1203 ExprId(ast::NodeId),
1205 // Type parameters for a method call like `a.foo::<int>()`
1206 MethodCallKey(ty::MethodCall)
1209 pub fn node_id_substs<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1210 node: ExprOrMethodCall,
1211 param_substs: &subst::Substs<'tcx>)
1212 -> subst::Substs<'tcx> {
1213 let tcx = ccx.tcx();
1215 let substs = match node {
1217 tcx.node_id_item_substs(id).substs
1219 MethodCallKey(method_call) => {
1220 tcx.tables.borrow().method_map[&method_call].substs.clone()
1224 if substs.types.needs_infer() {
1225 tcx.sess.bug(&format!("type parameters for node {:?} include inference types: {:?}",
1229 monomorphize::apply_param_substs(tcx,
1231 &substs.erase_regions())
1234 pub fn langcall(bcx: Block,
1239 match bcx.tcx().lang_items.require(li) {
1242 let msg = format!("{} {}", msg, s);
1244 Some(span) => bcx.tcx().sess.span_fatal(span, &msg[..]),
1245 None => bcx.tcx().sess.fatal(&msg[..]),
1251 /// Return the VariantDef corresponding to an inlined variant node
1252 pub fn inlined_variant_def<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1253 inlined_vid: ast::NodeId)
1254 -> ty::VariantDef<'tcx>
1257 let ctor_ty = ccx.tcx().node_id_to_type(inlined_vid);
1258 debug!("inlined_variant_def: ctor_ty={:?} inlined_vid={:?}", ctor_ty,
1260 let adt_def = match ctor_ty.sty {
1261 ty::TyBareFn(_, &ty::BareFnTy { sig: ty::Binder(ty::FnSig {
1262 output: ty::FnConverging(ty), ..
1265 }.ty_adt_def().unwrap();
1266 let inlined_vid_def_id = ccx.tcx().map.local_def_id(inlined_vid);
1267 adt_def.variants.iter().find(|v| {
1268 inlined_vid_def_id == v.did ||
1269 ccx.external().borrow().get(&v.did) == Some(&Some(inlined_vid))
1270 }).unwrap_or_else(|| {
1271 ccx.sess().bug(&format!("no variant for {:?}::{}", adt_def, inlined_vid))
1275 // To avoid UB from LLVM, these two functions mask RHS with an
1276 // appropriate mask unconditionally (i.e. the fallback behavior for
1277 // all shifts). For 32- and 64-bit types, this matches the semantics
1278 // of Java. (See related discussion on #1877 and #10183.)
1280 pub fn build_unchecked_lshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1283 binop_debug_loc: DebugLoc) -> ValueRef {
1284 let rhs = base::cast_shift_expr_rhs(bcx, hir::BinOp_::BiShl, lhs, rhs);
1285 // #1877, #10183: Ensure that input is always valid
1286 let rhs = shift_mask_rhs(bcx, rhs, binop_debug_loc);
1287 build::Shl(bcx, lhs, rhs, binop_debug_loc)
1290 pub fn build_unchecked_rshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1294 binop_debug_loc: DebugLoc) -> ValueRef {
1295 let rhs = base::cast_shift_expr_rhs(bcx, hir::BinOp_::BiShr, lhs, rhs);
1296 // #1877, #10183: Ensure that input is always valid
1297 let rhs = shift_mask_rhs(bcx, rhs, binop_debug_loc);
1298 let is_signed = lhs_t.is_signed();
1300 build::AShr(bcx, lhs, rhs, binop_debug_loc)
1302 build::LShr(bcx, lhs, rhs, binop_debug_loc)
1306 fn shift_mask_rhs<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1308 debug_loc: DebugLoc) -> ValueRef {
1309 let rhs_llty = val_ty(rhs);
1310 build::And(bcx, rhs, shift_mask_val(bcx, rhs_llty, rhs_llty, false), debug_loc)
1313 pub fn shift_mask_val<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1316 invert: bool) -> ValueRef {
1317 let kind = llty.kind();
1319 TypeKind::Integer => {
1320 // i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
1321 let val = llty.int_width() - 1;
1323 C_integral(mask_llty, !val, true)
1325 C_integral(mask_llty, val, false)
1328 TypeKind::Vector => {
1329 let mask = shift_mask_val(bcx, llty.element_type(), mask_llty.element_type(), invert);
1330 build::VectorSplat(bcx, mask_llty.vector_length(), mask)
1332 _ => panic!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
1336 pub fn get_static_val<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1340 if let Some(node_id) = ccx.tcx().map.as_local_node_id(did) {
1341 base::get_item_val(ccx, node_id)
1343 base::get_extern_const(ccx, did, ty)