2013-06-13 19:19:50 +12:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-07-07 17:58:01 -07:00
|
|
|
use llvm;
|
2014-09-05 09:18:53 -07:00
|
|
|
use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
|
2015-03-28 02:23:20 -07:00
|
|
|
use llvm::TargetData;
|
2014-07-07 17:58:01 -07:00
|
|
|
use llvm::mk_target_data;
|
2013-06-13 14:02:33 +12:00
|
|
|
use metadata::common::LinkMeta;
|
2014-12-19 00:03:00 +02:00
|
|
|
use middle::def::ExportMap;
|
2014-09-12 11:42:58 -04:00
|
|
|
use middle::traits;
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::adt;
|
|
|
|
use trans::base;
|
|
|
|
use trans::builder::Builder;
|
2015-03-15 04:01:57 +02:00
|
|
|
use trans::common::{ExternMap,BuilderRef_res};
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::debuginfo;
|
2015-03-04 01:08:06 +02:00
|
|
|
use trans::declare;
|
2015-04-22 11:52:08 +02:00
|
|
|
use trans::glue::DropGlueKind;
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::monomorphize::MonoId;
|
|
|
|
use trans::type_::{Type, TypeNames};
|
2015-01-29 14:03:34 +02:00
|
|
|
use middle::subst::Substs;
|
2015-01-03 22:42:21 -05:00
|
|
|
use middle::ty::{self, Ty};
|
2014-11-15 20:30:33 -05:00
|
|
|
use session::config::NoDebugInfo;
|
|
|
|
use session::Session;
|
2014-10-14 23:40:21 +03:00
|
|
|
use util::ppaux::Repr;
|
2013-12-09 14:56:53 -07:00
|
|
|
use util::sha2::Sha256;
|
2014-11-10 00:59:56 +02:00
|
|
|
use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
|
2013-12-09 14:56:53 -07:00
|
|
|
|
2014-11-25 13:28:35 -08:00
|
|
|
use std::ffi::CString;
|
2013-12-18 18:35:33 -08:00
|
|
|
use std::cell::{Cell, RefCell};
|
2014-03-15 22:29:34 +02:00
|
|
|
use std::ptr;
|
2014-04-22 03:03:02 +03:00
|
|
|
use std::rc::Rc;
|
2013-06-13 14:49:01 +12:00
|
|
|
use syntax::ast;
|
2014-01-10 14:02:36 -08:00
|
|
|
use syntax::parse::token::InternedString;
|
2013-06-13 14:49:01 +12:00
|
|
|
|
2014-04-22 03:25:56 +03:00
|
|
|
pub struct Stats {
|
2015-03-25 17:06:52 -07:00
|
|
|
pub n_glues_created: Cell<usize>,
|
|
|
|
pub n_null_glues: Cell<usize>,
|
|
|
|
pub n_real_glues: Cell<usize>,
|
|
|
|
pub n_fns: Cell<usize>,
|
|
|
|
pub n_monos: Cell<usize>,
|
|
|
|
pub n_inlines: Cell<usize>,
|
|
|
|
pub n_closures: Cell<usize>,
|
|
|
|
pub n_llvm_insns: Cell<usize>,
|
|
|
|
pub llvm_insns: RefCell<FnvHashMap<String, usize>>,
|
2014-11-10 12:27:56 -08:00
|
|
|
// (ident, llvm-instructions)
|
2015-03-25 17:06:52 -07:00
|
|
|
pub fn_stats: RefCell<Vec<(String, usize)> >,
|
2014-04-22 03:25:56 +03:00
|
|
|
}
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
/// The shared portion of a `CrateContext`. There is one `SharedCrateContext`
|
|
|
|
/// per crate. The data here is shared between all compilation units of the
|
|
|
|
/// crate, so it must not contain references to any LLVM data structures
|
|
|
|
/// (aside from metadata-related ones).
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct SharedCrateContext<'tcx> {
|
2014-09-29 22:11:30 +03:00
|
|
|
local_ccxs: Vec<LocalCrateContext<'tcx>>,
|
2014-07-16 11:27:57 -07:00
|
|
|
|
|
|
|
metadata_llmod: ModuleRef,
|
|
|
|
metadata_llcx: ContextRef,
|
|
|
|
|
2014-12-19 00:03:00 +02:00
|
|
|
export_map: ExportMap,
|
2014-07-16 11:27:57 -07:00
|
|
|
reachable: NodeSet,
|
|
|
|
item_symbols: RefCell<NodeMap<String>>,
|
|
|
|
link_meta: LinkMeta,
|
|
|
|
symbol_hasher: RefCell<Sha256>,
|
2014-04-22 15:56:37 +03:00
|
|
|
tcx: ty::ctxt<'tcx>,
|
2014-07-16 11:27:57 -07:00
|
|
|
stats: Stats,
|
2015-01-06 00:56:30 -05:00
|
|
|
check_overflow: bool,
|
2015-03-25 11:57:55 +01:00
|
|
|
check_drop_flag_for_sanity: bool,
|
2014-07-31 16:45:29 -07:00
|
|
|
|
2014-11-10 00:59:56 +02:00
|
|
|
available_monomorphizations: RefCell<FnvHashSet<String>>,
|
2015-04-22 11:52:08 +02:00
|
|
|
available_drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, String>>,
|
2015-05-12 14:46:19 -07:00
|
|
|
use_dll_storage_attrs: bool,
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The local portion of a `CrateContext`. There is one `LocalCrateContext`
|
|
|
|
/// per compilation unit. Each one has its own LLVM `ContextRef` so that
|
|
|
|
/// several compilation units may be optimized in parallel. All other LLVM
|
|
|
|
/// data structures in the `LocalCrateContext` are tied to that `ContextRef`.
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct LocalCrateContext<'tcx> {
|
2014-09-05 09:18:53 -07:00
|
|
|
llmod: ModuleRef,
|
|
|
|
llcx: ContextRef,
|
|
|
|
td: TargetData,
|
|
|
|
tn: TypeNames,
|
|
|
|
externs: RefCell<ExternMap>,
|
|
|
|
item_vals: RefCell<NodeMap<ValueRef>>,
|
2014-12-16 15:00:05 -05:00
|
|
|
needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
|
2014-12-03 20:23:15 -05:00
|
|
|
fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
|
2015-04-22 11:52:08 +02:00
|
|
|
drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, ValueRef>>,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Track mapping of external ids to local items imported for inlining
|
2014-09-05 09:18:53 -07:00
|
|
|
external: RefCell<DefIdMap<Option<ast::NodeId>>>,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Backwards version of the `external` map (inlined items to where they
|
|
|
|
/// came from)
|
2014-09-05 09:18:53 -07:00
|
|
|
external_srcs: RefCell<NodeMap<ast::DefId>>,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache instances of monomorphized functions
|
2014-09-29 22:11:30 +03:00
|
|
|
monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
|
2015-03-25 17:06:52 -07:00
|
|
|
monomorphizing: RefCell<DefIdMap<usize>>,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache generated vtables
|
2015-03-14 02:36:41 +02:00
|
|
|
vtables: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>>,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache of constant strings,
|
2014-11-10 00:59:56 +02:00
|
|
|
const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
|
2013-06-13 14:02:33 +12:00
|
|
|
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Reverse-direction for const ptrs cast from globals.
|
2015-01-29 14:03:34 +02:00
|
|
|
/// Key is a ValueRef holding a *T,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Val is a ValueRef holding a *[T].
|
|
|
|
///
|
|
|
|
/// Needed because LLVM loses pointer->pointee association
|
|
|
|
/// when we ptrcast, and we have to ptrcast during translation
|
2015-01-29 14:03:34 +02:00
|
|
|
/// of a [T] const because we form a slice, a (*T,usize) pair, not
|
|
|
|
/// a pointer to an LLVM array type. Similar for trait objects.
|
|
|
|
const_unsized: RefCell<FnvHashMap<ValueRef, ValueRef>>,
|
|
|
|
|
|
|
|
/// Cache of emitted const globals (value -> global)
|
|
|
|
const_globals: RefCell<FnvHashMap<ValueRef, ValueRef>>,
|
2013-06-13 14:02:33 +12:00
|
|
|
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache of emitted const values
|
2015-01-29 14:03:34 +02:00
|
|
|
const_values: RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>), ValueRef>>,
|
2013-06-13 14:02:33 +12:00
|
|
|
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 08:17:01 -07:00
|
|
|
/// Cache of emitted static values
|
|
|
|
static_values: RefCell<NodeMap<ValueRef>>,
|
|
|
|
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache of external const values
|
2014-09-05 09:18:53 -07:00
|
|
|
extern_const_values: RefCell<DefIdMap<ValueRef>>,
|
2013-06-13 14:02:33 +12:00
|
|
|
|
2014-11-10 00:59:56 +02:00
|
|
|
impl_method_cache: RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>>,
|
2013-06-14 17:38:17 +12:00
|
|
|
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache of closure wrappers for bare fn's.
|
2014-11-10 00:59:56 +02:00
|
|
|
closure_bare_wrapper_cache: RefCell<FnvHashMap<ValueRef, ValueRef>>,
|
2014-09-05 09:18:53 -07:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
lltypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
|
|
|
|
llsizingtypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
|
|
|
|
adt_reprs: RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>>,
|
|
|
|
type_hashcodes: RefCell<FnvHashMap<Ty<'tcx>, String>>,
|
2014-09-05 09:18:53 -07:00
|
|
|
int_type: Type,
|
|
|
|
opaque_vec_type: Type,
|
|
|
|
builder: BuilderRef_res,
|
2014-05-28 22:26:56 -07:00
|
|
|
|
|
|
|
/// Holds the LLVM values for closure IDs.
|
2015-01-24 22:00:03 +02:00
|
|
|
closure_vals: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
|
2014-05-28 22:26:56 -07:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
|
2014-04-09 19:56:31 -04:00
|
|
|
|
2014-09-05 09:18:53 -07:00
|
|
|
eh_personality: RefCell<Option<ValueRef>>,
|
2014-05-19 09:30:09 -07:00
|
|
|
|
2014-11-10 00:59:56 +02:00
|
|
|
intrinsics: RefCell<FnvHashMap<&'static str, ValueRef>>,
|
2014-07-21 16:42:34 -07:00
|
|
|
|
|
|
|
/// Number of LLVM instructions translated into this `LocalCrateContext`.
|
|
|
|
/// This is used to perform some basic load-balancing to keep all LLVM
|
|
|
|
/// contexts around the same size.
|
2015-03-25 17:06:52 -07:00
|
|
|
n_llvm_insns: Cell<usize>,
|
2014-09-12 11:42:58 -04:00
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
trait_cache: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
traits::Vtable<'tcx, ()>>>,
|
2013-06-13 14:02:33 +12:00
|
|
|
}
|
2013-06-13 14:49:01 +12:00
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct CrateContext<'a, 'tcx: 'a> {
|
|
|
|
shared: &'a SharedCrateContext<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
local: &'a LocalCrateContext<'tcx>,
|
2014-08-01 12:27:12 -07:00
|
|
|
/// The index of `local` in `shared.local_ccxs`. This is used in
|
|
|
|
/// `maybe_iter(true)` to identify the original `LocalCrateContext`.
|
2015-03-25 17:06:52 -07:00
|
|
|
index: usize,
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct CrateContextIterator<'a, 'tcx: 'a> {
|
|
|
|
shared: &'a SharedCrateContext<'tcx>,
|
2015-03-25 17:06:52 -07:00
|
|
|
index: usize,
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 10:52:52 -07:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:54:03 -05:00
|
|
|
impl<'a, 'tcx> Iterator for CrateContextIterator<'a,'tcx> {
|
|
|
|
type Item = CrateContext<'a, 'tcx>;
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
fn next(&mut self) -> Option<CrateContext<'a, 'tcx>> {
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 10:52:52 -07:00
|
|
|
if self.index >= self.shared.local_ccxs.len() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = self.index;
|
|
|
|
self.index += 1;
|
|
|
|
|
|
|
|
Some(CrateContext {
|
|
|
|
shared: self.shared,
|
|
|
|
local: &self.shared.local_ccxs[index],
|
2014-08-01 12:27:12 -07:00
|
|
|
index: index,
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 10:52:52 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-01 12:27:12 -07:00
|
|
|
/// The iterator produced by `CrateContext::maybe_iter`.
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct CrateContextMaybeIterator<'a, 'tcx: 'a> {
|
|
|
|
shared: &'a SharedCrateContext<'tcx>,
|
2015-03-25 17:06:52 -07:00
|
|
|
index: usize,
|
2014-08-01 12:27:12 -07:00
|
|
|
single: bool,
|
2015-03-25 17:06:52 -07:00
|
|
|
origin: usize,
|
2014-08-01 12:27:12 -07:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:54:03 -05:00
|
|
|
impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
|
|
|
|
type Item = (CrateContext<'a, 'tcx>, bool);
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
fn next(&mut self) -> Option<(CrateContext<'a, 'tcx>, bool)> {
|
2014-08-01 12:27:12 -07:00
|
|
|
if self.index >= self.shared.local_ccxs.len() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = self.index;
|
|
|
|
self.index += 1;
|
|
|
|
if self.single {
|
|
|
|
self.index = self.shared.local_ccxs.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
let ccx = CrateContext {
|
|
|
|
shared: self.shared,
|
|
|
|
local: &self.shared.local_ccxs[index],
|
|
|
|
index: index,
|
|
|
|
};
|
|
|
|
Some((ccx, index == self.origin))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
|
|
|
|
let llcx = llvm::LLVMContextCreate();
|
2015-02-17 22:47:40 -08:00
|
|
|
let mod_name = CString::new(mod_name).unwrap();
|
2014-11-25 13:28:35 -08:00
|
|
|
let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
|
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
let data_layout = sess.target.target.data_layout.as_bytes();
|
|
|
|
let data_layout = CString::new(data_layout).unwrap();
|
2014-11-25 13:28:35 -08:00
|
|
|
llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
|
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
let llvm_target = sess.target.target.llvm_target.as_bytes();
|
|
|
|
let llvm_target = CString::new(llvm_target).unwrap();
|
2014-11-25 13:28:35 -08:00
|
|
|
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
|
2014-07-16 11:27:57 -07:00
|
|
|
(llcx, llmod)
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
impl<'tcx> SharedCrateContext<'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
pub fn new(crate_name: &str,
|
2015-03-25 17:06:52 -07:00
|
|
|
local_count: usize,
|
2014-04-22 15:56:37 +03:00
|
|
|
tcx: ty::ctxt<'tcx>,
|
2014-12-19 00:03:00 +02:00
|
|
|
export_map: ExportMap,
|
2013-12-09 14:56:53 -07:00
|
|
|
symbol_hasher: Sha256,
|
2013-06-18 09:39:16 -07:00
|
|
|
link_meta: LinkMeta,
|
2015-01-06 00:56:30 -05:00
|
|
|
reachable: NodeSet,
|
2015-03-25 11:57:55 +01:00
|
|
|
check_overflow: bool,
|
|
|
|
check_drop_flag_for_sanity: bool)
|
2014-04-22 15:56:37 +03:00
|
|
|
-> SharedCrateContext<'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
let (metadata_llcx, metadata_llmod) = unsafe {
|
|
|
|
create_context_and_module(&tcx.sess, "metadata")
|
|
|
|
};
|
|
|
|
|
2015-05-12 14:46:19 -07:00
|
|
|
// An interesting part of Windows which MSVC forces our hand on (and
|
|
|
|
// apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
|
|
|
|
// attributes in LLVM IR as well as native dependencies (in C these
|
|
|
|
// correspond to `__declspec(dllimport)`).
|
|
|
|
//
|
|
|
|
// Whenever a dynamic library is built by MSVC it must have its public
|
|
|
|
// interface specified by functions tagged with `dllexport` or otherwise
|
|
|
|
// they're not available to be linked against. This poses a few problems
|
|
|
|
// for the compiler, some of which are somewhat fundamental, but we use
|
|
|
|
// the `use_dll_storage_attrs` variable below to attach the `dllexport`
|
|
|
|
// attribute to all LLVM functions that are reachable (e.g. they're
|
|
|
|
// already tagged with external linkage). This is suboptimal for a few
|
|
|
|
// reasons:
|
|
|
|
//
|
|
|
|
// * If an object file will never be included in a dynamic library,
|
|
|
|
// there's no need to attach the dllexport attribute. Most object
|
|
|
|
// files in Rust are not destined to become part of a dll as binaries
|
|
|
|
// are statically linked by default.
|
|
|
|
// * If the compiler is emitting both an rlib and a dylib, the same
|
|
|
|
// source object file is currently used but with MSVC this may be less
|
|
|
|
// feasible. The compiler may be able to get around this, but it may
|
|
|
|
// involve some invasive changes to deal with this.
|
|
|
|
//
|
|
|
|
// The flipside of this situation is that whenever you link to a dll and
|
|
|
|
// you import a function from it, the import should be tagged with
|
|
|
|
// `dllimport`. At this time, however, the compiler does not emit
|
|
|
|
// `dllimport` for any declarations other than constants (where it is
|
|
|
|
// required), which is again suboptimal for even more reasons!
|
|
|
|
//
|
|
|
|
// * Calling a function imported from another dll without using
|
|
|
|
// `dllimport` causes the linker/compiler to have extra overhead (one
|
|
|
|
// `jmp` instruction on x86) when calling the function.
|
|
|
|
// * The same object file may be used in different circumstances, so a
|
|
|
|
// function may be imported from a dll if the object is linked into a
|
|
|
|
// dll, but it may be just linked against if linked into an rlib.
|
|
|
|
// * The compiler has no knowledge about whether native functions should
|
|
|
|
// be tagged dllimport or not.
|
|
|
|
//
|
|
|
|
// For now the compiler takes the perf hit (I do not have any numbers to
|
|
|
|
// this effect) by marking very little as `dllimport` and praying the
|
|
|
|
// linker will take care of everything. Fixing this problem will likely
|
|
|
|
// require adding a few attributes to Rust itself (feature gated at the
|
|
|
|
// start) and then strongly recommending static linkage on MSVC!
|
|
|
|
let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
let mut shared_ccx = SharedCrateContext {
|
|
|
|
local_ccxs: Vec::with_capacity(local_count),
|
|
|
|
metadata_llmod: metadata_llmod,
|
|
|
|
metadata_llcx: metadata_llcx,
|
2014-12-18 20:27:17 +02:00
|
|
|
export_map: export_map,
|
2014-07-16 11:27:57 -07:00
|
|
|
reachable: reachable,
|
2015-01-16 14:27:43 -08:00
|
|
|
item_symbols: RefCell::new(NodeMap()),
|
2014-07-16 11:27:57 -07:00
|
|
|
link_meta: link_meta,
|
|
|
|
symbol_hasher: RefCell::new(symbol_hasher),
|
|
|
|
tcx: tcx,
|
|
|
|
stats: Stats {
|
2015-01-25 10:58:43 +00:00
|
|
|
n_glues_created: Cell::new(0),
|
|
|
|
n_null_glues: Cell::new(0),
|
|
|
|
n_real_glues: Cell::new(0),
|
|
|
|
n_fns: Cell::new(0),
|
|
|
|
n_monos: Cell::new(0),
|
|
|
|
n_inlines: Cell::new(0),
|
|
|
|
n_closures: Cell::new(0),
|
|
|
|
n_llvm_insns: Cell::new(0),
|
2015-01-16 14:27:43 -08:00
|
|
|
llvm_insns: RefCell::new(FnvHashMap()),
|
2014-07-16 11:27:57 -07:00
|
|
|
fn_stats: RefCell::new(Vec::new()),
|
|
|
|
},
|
2015-01-06 00:56:30 -05:00
|
|
|
check_overflow: check_overflow,
|
2015-03-25 11:57:55 +01:00
|
|
|
check_drop_flag_for_sanity: check_drop_flag_for_sanity,
|
2015-01-16 14:27:43 -08:00
|
|
|
available_monomorphizations: RefCell::new(FnvHashSet()),
|
|
|
|
available_drop_glues: RefCell::new(FnvHashMap()),
|
2015-05-12 14:46:19 -07:00
|
|
|
use_dll_storage_attrs: use_dll_storage_attrs,
|
2014-07-16 11:27:57 -07:00
|
|
|
};
|
|
|
|
|
2015-01-26 15:46:12 -05:00
|
|
|
for i in 0..local_count {
|
2014-07-16 11:27:57 -07:00
|
|
|
// Append ".rs" to crate name as LLVM module identifier.
|
|
|
|
//
|
|
|
|
// LLVM code generator emits a ".file filename" directive
|
|
|
|
// for ELF backends. Value of the "filename" is set as the
|
|
|
|
// LLVM module identifier. Due to a LLVM MC bug[1], LLVM
|
|
|
|
// crashes if the module identifier is same as other symbols
|
|
|
|
// such as a function name in the module.
|
|
|
|
// 1. http://llvm.org/bugs/show_bug.cgi?id=11479
|
|
|
|
let llmod_id = format!("{}.{}.rs", crate_name, i);
|
2015-02-18 14:48:57 -05:00
|
|
|
let local_ccx = LocalCrateContext::new(&shared_ccx, &llmod_id[..]);
|
2014-07-16 11:27:57 -07:00
|
|
|
shared_ccx.local_ccxs.push(local_ccx);
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ccx
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn iter<'a>(&'a self) -> CrateContextIterator<'a, 'tcx> {
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 10:52:52 -07:00
|
|
|
CrateContextIterator {
|
|
|
|
shared: self,
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
pub fn get_ccx<'a>(&'a self, index: usize) -> CrateContext<'a, 'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
CrateContext {
|
|
|
|
shared: self,
|
|
|
|
local: &self.local_ccxs[index],
|
2014-08-01 12:27:12 -07:00
|
|
|
index: index,
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
fn get_smallest_ccx<'a>(&'a self) -> CrateContext<'a, 'tcx> {
|
2014-08-01 12:27:12 -07:00
|
|
|
let (local_ccx, index) =
|
2014-07-21 16:42:34 -07:00
|
|
|
self.local_ccxs
|
|
|
|
.iter()
|
2015-01-26 16:05:07 -05:00
|
|
|
.zip(0..self.local_ccxs.len())
|
2014-08-01 12:27:12 -07:00
|
|
|
.min_by(|&(local_ccx, _idx)| local_ccx.n_llvm_insns.get())
|
2014-07-21 16:42:34 -07:00
|
|
|
.unwrap();
|
|
|
|
CrateContext {
|
|
|
|
shared: self,
|
|
|
|
local: local_ccx,
|
2014-08-01 12:27:12 -07:00
|
|
|
index: index,
|
2014-07-21 16:42:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
|
|
|
|
pub fn metadata_llmod(&self) -> ModuleRef {
|
|
|
|
self.metadata_llmod
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metadata_llcx(&self) -> ContextRef {
|
|
|
|
self.metadata_llcx
|
|
|
|
}
|
|
|
|
|
2014-12-19 00:03:00 +02:00
|
|
|
pub fn export_map<'a>(&'a self) -> &'a ExportMap {
|
2014-12-18 20:27:17 +02:00
|
|
|
&self.export_map
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reachable<'a>(&'a self) -> &'a NodeSet {
|
|
|
|
&self.reachable
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
|
|
|
|
&self.item_symbols
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
|
|
|
|
&self.link_meta
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.tcx
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn take_tcx(self) -> ty::ctxt<'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sess<'a>(&'a self) -> &'a Session {
|
|
|
|
&self.tcx.sess
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stats<'a>(&'a self) -> &'a Stats {
|
|
|
|
&self.stats
|
|
|
|
}
|
2015-05-12 14:46:19 -07:00
|
|
|
|
|
|
|
pub fn use_dll_storage_attrs(&self) -> bool {
|
|
|
|
self.use_dll_storage_attrs
|
|
|
|
}
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> LocalCrateContext<'tcx> {
|
|
|
|
fn new(shared: &SharedCrateContext<'tcx>,
|
2014-07-16 11:27:57 -07:00
|
|
|
name: &str)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> LocalCrateContext<'tcx> {
|
2013-06-13 14:49:01 +12:00
|
|
|
unsafe {
|
2014-07-16 11:27:57 -07:00
|
|
|
let (llcx, llmod) = create_context_and_module(&shared.tcx.sess, name);
|
|
|
|
|
2015-01-07 11:58:31 -05:00
|
|
|
let td = mk_target_data(&shared.tcx
|
2014-07-16 11:27:57 -07:00
|
|
|
.sess
|
2014-07-23 11:56:36 -07:00
|
|
|
.target
|
|
|
|
.target
|
2014-07-16 11:27:57 -07:00
|
|
|
.data_layout
|
2015-02-20 14:08:14 -05:00
|
|
|
);
|
2014-07-16 11:27:57 -07:00
|
|
|
|
|
|
|
let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
|
2014-01-27 14:58:40 +01:00
|
|
|
Some(debuginfo::CrateDebugContext::new(llmod))
|
2013-06-13 14:49:01 +12:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
let mut local_ccx = LocalCrateContext {
|
2014-03-15 22:29:34 +02:00
|
|
|
llmod: llmod,
|
|
|
|
llcx: llcx,
|
|
|
|
td: td,
|
|
|
|
tn: TypeNames::new(),
|
2015-01-16 14:27:43 -08:00
|
|
|
externs: RefCell::new(FnvHashMap()),
|
|
|
|
item_vals: RefCell::new(NodeMap()),
|
|
|
|
needs_unwind_cleanup_cache: RefCell::new(FnvHashMap()),
|
|
|
|
fn_pointer_shims: RefCell::new(FnvHashMap()),
|
|
|
|
drop_glues: RefCell::new(FnvHashMap()),
|
|
|
|
external: RefCell::new(DefIdMap()),
|
|
|
|
external_srcs: RefCell::new(NodeMap()),
|
|
|
|
monomorphized: RefCell::new(FnvHashMap()),
|
|
|
|
monomorphizing: RefCell::new(DefIdMap()),
|
|
|
|
vtables: RefCell::new(FnvHashMap()),
|
|
|
|
const_cstr_cache: RefCell::new(FnvHashMap()),
|
2015-01-29 14:03:34 +02:00
|
|
|
const_unsized: RefCell::new(FnvHashMap()),
|
2015-01-16 14:27:43 -08:00
|
|
|
const_globals: RefCell::new(FnvHashMap()),
|
2015-01-29 14:03:34 +02:00
|
|
|
const_values: RefCell::new(FnvHashMap()),
|
2015-01-16 14:27:43 -08:00
|
|
|
static_values: RefCell::new(NodeMap()),
|
|
|
|
extern_const_values: RefCell::new(DefIdMap()),
|
|
|
|
impl_method_cache: RefCell::new(FnvHashMap()),
|
|
|
|
closure_bare_wrapper_cache: RefCell::new(FnvHashMap()),
|
|
|
|
lltypes: RefCell::new(FnvHashMap()),
|
|
|
|
llsizingtypes: RefCell::new(FnvHashMap()),
|
|
|
|
adt_reprs: RefCell::new(FnvHashMap()),
|
|
|
|
type_hashcodes: RefCell::new(FnvHashMap()),
|
2014-09-14 20:27:36 -07:00
|
|
|
int_type: Type::from_ref(ptr::null_mut()),
|
|
|
|
opaque_vec_type: Type::from_ref(ptr::null_mut()),
|
2014-03-15 22:29:34 +02:00
|
|
|
builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
|
2015-01-24 22:00:03 +02:00
|
|
|
closure_vals: RefCell::new(FnvHashMap()),
|
2014-03-15 22:29:34 +02:00
|
|
|
dbg_cx: dbg_cx,
|
2014-05-19 09:30:09 -07:00
|
|
|
eh_personality: RefCell::new(None),
|
2015-01-16 14:27:43 -08:00
|
|
|
intrinsics: RefCell::new(FnvHashMap()),
|
2015-01-25 10:58:43 +00:00
|
|
|
n_llvm_insns: Cell::new(0),
|
2015-01-16 14:27:43 -08:00
|
|
|
trait_cache: RefCell::new(FnvHashMap()),
|
2014-03-15 22:29:34 +02:00
|
|
|
};
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
local_ccx.int_type = Type::int(&local_ccx.dummy_ccx(shared));
|
|
|
|
local_ccx.opaque_vec_type = Type::opaque_vec(&local_ccx.dummy_ccx(shared));
|
|
|
|
|
|
|
|
// Done mutating local_ccx directly. (The rest of the
|
|
|
|
// initialization goes through RefCell.)
|
|
|
|
{
|
|
|
|
let ccx = local_ccx.dummy_ccx(shared);
|
2014-03-15 22:29:34 +02:00
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
|
2014-11-17 21:39:01 +13:00
|
|
|
str_slice_ty.set_struct_body(&[Type::i8p(&ccx), ccx.int_type()], false);
|
2014-07-16 11:27:57 -07:00
|
|
|
ccx.tn().associate_type("str_slice", &str_slice_ty);
|
2014-03-15 22:29:34 +02:00
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
if ccx.sess().count_llvm_insns() {
|
|
|
|
base::init_insn_ctxt()
|
|
|
|
}
|
2013-06-17 16:23:24 +12:00
|
|
|
}
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
local_ccx
|
2013-06-13 14:49:01 +12:00
|
|
|
}
|
|
|
|
}
|
2013-07-21 15:33:40 +02:00
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
/// Create a dummy `CrateContext` from `self` and the provided
|
|
|
|
/// `SharedCrateContext`. This is somewhat dangerous because `self` may
|
|
|
|
/// not actually be an element of `shared.local_ccxs`, which can cause some
|
2014-10-09 15:17:22 -04:00
|
|
|
/// operations to panic unexpectedly.
|
2014-07-16 11:27:57 -07:00
|
|
|
///
|
|
|
|
/// This is used in the `LocalCrateContext` constructor to allow calling
|
|
|
|
/// functions that expect a complete `CrateContext`, even before the local
|
|
|
|
/// portion is fully initialized and attached to the `SharedCrateContext`.
|
2014-09-29 22:11:30 +03:00
|
|
|
fn dummy_ccx<'a>(&'a self, shared: &'a SharedCrateContext<'tcx>)
|
|
|
|
-> CrateContext<'a, 'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
CrateContext {
|
|
|
|
shared: shared,
|
|
|
|
local: self,
|
2015-04-01 19:53:32 +02:00
|
|
|
index: !0 as usize,
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
2014-03-15 22:29:34 +02:00
|
|
|
}
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
2014-03-15 22:29:34 +02:00
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
impl<'b, 'tcx> CrateContext<'b, 'tcx> {
|
|
|
|
pub fn shared(&self) -> &'b SharedCrateContext<'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.shared
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn local(&self) -> &'b LocalCrateContext<'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.local
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-21 16:42:34 -07:00
|
|
|
/// Get a (possibly) different `CrateContext` from the same
|
|
|
|
/// `SharedCrateContext`.
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn rotate(&self) -> CrateContext<'b, 'tcx> {
|
2014-07-21 16:42:34 -07:00
|
|
|
self.shared.get_smallest_ccx()
|
|
|
|
}
|
|
|
|
|
2014-08-01 12:27:12 -07:00
|
|
|
/// Either iterate over only `self`, or iterate over all `CrateContext`s in
|
|
|
|
/// the `SharedCrateContext`. The iterator produces `(ccx, is_origin)`
|
|
|
|
/// pairs, where `is_origin` is `true` if `ccx` is `self` and `false`
|
|
|
|
/// otherwise. This method is useful for avoiding code duplication in
|
|
|
|
/// cases where it may or may not be necessary to translate code into every
|
|
|
|
/// context.
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn maybe_iter(&self, iter_all: bool) -> CrateContextMaybeIterator<'b, 'tcx> {
|
2014-08-01 12:27:12 -07:00
|
|
|
CrateContextMaybeIterator {
|
|
|
|
shared: self.shared,
|
|
|
|
index: if iter_all { 0 } else { self.index },
|
|
|
|
single: !iter_all,
|
|
|
|
origin: self.index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.shared.tcx
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-03-05 16:36:01 +02:00
|
|
|
pub fn sess<'a>(&'a self) -> &'a Session {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.shared.tcx.sess
|
2014-03-05 16:36:01 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn builder<'a>(&'a self) -> Builder<'a, 'tcx> {
|
2013-07-21 15:33:40 +02:00
|
|
|
Builder::new(self)
|
|
|
|
}
|
2013-08-11 13:29:14 -04:00
|
|
|
|
2014-09-05 09:18:53 -07:00
|
|
|
pub fn raw_builder<'a>(&'a self) -> BuilderRef {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.local.builder.b
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:56:31 -04:00
|
|
|
pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
|
2014-11-29 16:41:21 -05:00
|
|
|
if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
|
|
|
|
return v;
|
2014-04-09 19:56:31 -04:00
|
|
|
}
|
|
|
|
match declare_intrinsic(self, key) {
|
|
|
|
Some(v) => return v,
|
2014-10-09 15:17:22 -04:00
|
|
|
None => panic!()
|
2014-04-09 19:56:31 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-12 20:03:34 +03:00
|
|
|
|
|
|
|
pub fn is_split_stack_supported(&self) -> bool {
|
2014-07-23 11:56:36 -07:00
|
|
|
self.sess().target.target.options.morestack
|
2014-05-12 20:03:34 +03:00
|
|
|
}
|
2014-09-05 09:18:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
pub fn llmod(&self) -> ModuleRef {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.local.llmod
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn llcx(&self) -> ContextRef {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.local.llcx
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn td<'a>(&'a self) -> &'a TargetData {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.td
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tn<'a>(&'a self) -> &'a TypeNames {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.tn
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn externs<'a>(&'a self) -> &'a RefCell<ExternMap> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.externs
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn item_vals<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.item_vals
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:03:00 +02:00
|
|
|
pub fn export_map<'a>(&'a self) -> &'a ExportMap {
|
2014-12-18 20:27:17 +02:00
|
|
|
&self.shared.export_map
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reachable<'a>(&'a self) -> &'a NodeSet {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.shared.reachable
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.shared.item_symbols
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.shared.link_meta
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-12-16 15:00:05 -05:00
|
|
|
pub fn needs_unwind_cleanup_cache(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, bool>> {
|
|
|
|
&self.local.needs_unwind_cleanup_cache
|
|
|
|
}
|
|
|
|
|
2014-12-03 20:23:15 -05:00
|
|
|
pub fn fn_pointer_shims(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
|
|
|
|
&self.local.fn_pointer_shims
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:52:08 +02:00
|
|
|
pub fn drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<DropGlueKind<'tcx>, ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.drop_glues
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.external
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn external_srcs<'a>(&'a self) -> &'a RefCell<NodeMap<ast::DefId>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.external_srcs
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn monomorphized<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.monomorphized
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<usize>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.monomorphizing
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:36:41 +02:00
|
|
|
pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.vtables
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:59:56 +02:00
|
|
|
pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<InternedString, ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.const_cstr_cache
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2015-01-29 14:03:34 +02:00
|
|
|
pub fn const_unsized<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
|
|
|
|
&self.local.const_unsized
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_globals<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.const_globals
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2015-01-29 14:03:34 +02:00
|
|
|
pub fn const_values<'a>(&'a self) -> &'a RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>),
|
|
|
|
ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.const_values
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 08:17:01 -07:00
|
|
|
pub fn static_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
|
|
|
|
&self.local.static_values
|
|
|
|
}
|
|
|
|
|
2014-09-05 09:18:53 -07:00
|
|
|
pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.extern_const_values
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn impl_method_cache<'a>(&'a self)
|
2014-11-10 00:59:56 +02:00
|
|
|
-> &'a RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.impl_method_cache
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:59:56 +02:00
|
|
|
pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.closure_bare_wrapper_cache
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn lltypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.lltypes
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.llsizingtypes
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.adt_reprs
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.shared.symbol_hasher
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.type_hashcodes
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stats<'a>(&'a self) -> &'a Stats {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.shared.stats
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:59:56 +02:00
|
|
|
pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
|
2014-07-31 16:45:29 -07:00
|
|
|
&self.shared.available_monomorphizations
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:52:08 +02:00
|
|
|
pub fn available_drop_glues(&self) -> &RefCell<FnvHashMap<DropGlueKind<'tcx>, String>> {
|
2014-07-31 16:45:29 -07:00
|
|
|
&self.shared.available_drop_glues
|
|
|
|
}
|
|
|
|
|
2014-09-05 09:18:53 -07:00
|
|
|
pub fn int_type(&self) -> Type {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.local.int_type
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn opaque_vec_type(&self) -> Type {
|
2014-07-16 11:27:57 -07:00
|
|
|
self.local.opaque_vec_type
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
pub fn closure_vals<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
|
|
|
|
&self.local.closure_vals
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.dbg_cx
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eh_personality<'a>(&'a self) -> &'a RefCell<Option<ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.eh_personality
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:59:56 +02:00
|
|
|
fn intrinsics<'a>(&'a self) -> &'a RefCell<FnvHashMap<&'static str, ValueRef>> {
|
2014-07-16 11:27:57 -07:00
|
|
|
&self.local.intrinsics
|
2014-09-05 09:18:53 -07:00
|
|
|
}
|
2014-07-21 16:42:34 -07:00
|
|
|
|
|
|
|
pub fn count_llvm_insn(&self) {
|
|
|
|
self.local.n_llvm_insns.set(self.local.n_llvm_insns.get() + 1);
|
|
|
|
}
|
2014-09-12 11:42:58 -04:00
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
pub fn trait_cache(&self) -> &RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
traits::Vtable<'tcx, ()>>> {
|
2014-09-12 11:42:58 -04:00
|
|
|
&self.local.trait_cache
|
|
|
|
}
|
2014-10-14 23:40:21 +03:00
|
|
|
|
2014-11-06 21:30:49 -05:00
|
|
|
/// Return exclusive upper bound on object size.
|
|
|
|
///
|
|
|
|
/// The theoretical maximum object size is defined as the maximum positive `int` value. This
|
|
|
|
/// ensures that the `offset` semantics remain well-defined by allowing it to correctly index
|
|
|
|
/// every address within an object along with one byte past the end, along with allowing `int`
|
|
|
|
/// to store the difference between any two pointers into an object.
|
|
|
|
///
|
|
|
|
/// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer to
|
|
|
|
/// represent object size in bits. It would need to be 1 << 61 to account for this, but is
|
|
|
|
/// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
|
|
|
|
/// address space on 64-bit ARMv8 and x86_64.
|
|
|
|
pub fn obj_size_bound(&self) -> u64 {
|
2015-02-20 14:08:14 -05:00
|
|
|
match &self.sess().target.target.target_pointer_width[..] {
|
2014-11-06 21:30:49 -05:00
|
|
|
"32" => 1 << 31,
|
|
|
|
"64" => 1 << 47,
|
|
|
|
_ => unreachable!() // error handled by config::build_target_config
|
|
|
|
}
|
2014-10-14 23:40:21 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {
|
2014-10-14 23:40:21 +03:00
|
|
|
self.sess().fatal(
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!("the type `{}` is too big for the current architecture",
|
2015-02-20 14:08:14 -05:00
|
|
|
obj.repr(self.tcx())))
|
2014-10-14 23:40:21 +03:00
|
|
|
}
|
2015-01-06 00:56:30 -05:00
|
|
|
|
|
|
|
pub fn check_overflow(&self) -> bool {
|
|
|
|
self.shared.check_overflow
|
|
|
|
}
|
2015-03-25 11:57:55 +01:00
|
|
|
|
|
|
|
pub fn check_drop_flag_for_sanity(&self) -> bool {
|
|
|
|
// This controls whether we emit a conditional llvm.debugtrap
|
|
|
|
// guarded on whether the dropflag is one of its (two) valid
|
|
|
|
// values.
|
|
|
|
self.shared.check_drop_flag_for_sanity
|
|
|
|
}
|
2015-05-12 14:46:19 -07:00
|
|
|
|
|
|
|
pub fn use_dll_storage_attrs(&self) -> bool {
|
|
|
|
self.shared.use_dll_storage_attrs()
|
|
|
|
}
|
2014-04-09 19:56:31 -04:00
|
|
|
}
|
|
|
|
|
2015-04-28 18:20:30 -07:00
|
|
|
/// Declare any llvm intrinsics that you might need
|
2014-04-09 19:56:31 -04:00
|
|
|
fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
|
2015-01-02 14:44:21 -08:00
|
|
|
macro_rules! ifn {
|
2015-01-05 01:51:03 -05:00
|
|
|
($name:expr, fn() -> $ret:expr) => (
|
2014-04-09 19:56:31 -04:00
|
|
|
if *key == $name {
|
2015-03-04 01:08:06 +02:00
|
|
|
let f = declare::declare_cfn(ccx, $name, Type::func(&[], &$ret),
|
|
|
|
ty::mk_nil(ccx.tcx()));
|
2014-09-05 09:18:53 -07:00
|
|
|
ccx.intrinsics().borrow_mut().insert($name, f.clone());
|
2014-04-09 19:56:31 -04:00
|
|
|
return Some(f);
|
|
|
|
}
|
|
|
|
);
|
2015-01-05 01:51:03 -05:00
|
|
|
($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
|
2014-04-09 19:56:31 -04:00
|
|
|
if *key == $name {
|
2015-03-04 01:08:06 +02:00
|
|
|
let f = declare::declare_cfn(ccx, $name, Type::func(&[$($arg),*], &$ret),
|
|
|
|
ty::mk_nil(ccx.tcx()));
|
2014-09-05 09:18:53 -07:00
|
|
|
ccx.intrinsics().borrow_mut().insert($name, f.clone());
|
2014-04-09 19:56:31 -04:00
|
|
|
return Some(f);
|
|
|
|
}
|
|
|
|
)
|
2015-01-02 14:44:21 -08:00
|
|
|
}
|
|
|
|
macro_rules! mk_struct {
|
2014-11-17 21:39:01 +13:00
|
|
|
($($field_ty:expr),*) => (Type::struct_(ccx, &[$($field_ty),*], false))
|
2015-01-02 14:44:21 -08:00
|
|
|
}
|
2014-04-09 19:56:31 -04:00
|
|
|
|
|
|
|
let i8p = Type::i8p(ccx);
|
|
|
|
let void = Type::void(ccx);
|
|
|
|
let i1 = Type::i1(ccx);
|
|
|
|
let t_i8 = Type::i8(ccx);
|
|
|
|
let t_i16 = Type::i16(ccx);
|
|
|
|
let t_i32 = Type::i32(ccx);
|
|
|
|
let t_i64 = Type::i64(ccx);
|
|
|
|
let t_f32 = Type::f32(ccx);
|
|
|
|
let t_f64 = Type::f64(ccx);
|
|
|
|
|
2015-01-05 01:51:03 -05:00
|
|
|
ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
|
|
|
|
ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
|
|
|
|
ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
|
|
|
|
ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
|
|
|
|
ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
|
|
|
|
ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
|
|
|
|
|
|
|
|
ifn!("llvm.trap", fn() -> void);
|
|
|
|
ifn!("llvm.debugtrap", fn() -> void);
|
|
|
|
ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
|
|
|
|
|
|
|
|
ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
|
|
|
|
ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
|
|
|
|
ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
2015-01-24 12:00:35 +01:00
|
|
|
ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
2015-01-05 01:51:03 -05:00
|
|
|
ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
|
|
|
|
|
|
|
|
ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
|
|
|
|
ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
|
|
|
|
ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
|
|
|
|
ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
|
|
|
|
|
|
|
|
ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
|
|
|
|
ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
|
|
|
|
ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
|
|
|
|
ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
|
|
|
|
|
|
|
|
ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
|
|
|
|
|
|
|
|
ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
|
|
|
|
ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
|
|
|
|
ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
|
|
|
|
ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
|
|
|
|
|
|
|
|
ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
|
|
|
|
ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
|
|
|
|
ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
|
|
|
|
ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
|
|
|
|
|
|
|
|
ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
|
|
|
|
ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
|
|
|
|
ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
|
|
|
|
ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
|
|
|
|
|
|
|
|
ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
|
|
|
|
ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
|
|
|
|
ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
|
|
|
|
ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
|
|
|
|
|
|
|
|
ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
|
|
|
|
ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
|
|
|
|
ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
|
|
|
|
ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
|
|
|
|
|
|
|
|
ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
|
|
|
|
ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
|
|
|
|
ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
|
|
|
|
ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
|
|
|
|
|
|
|
|
ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
|
|
|
|
ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
|
|
|
|
|
|
|
|
ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
|
|
|
|
ifn!("llvm.assume", fn(i1) -> void);
|
2014-04-09 19:56:31 -04:00
|
|
|
|
|
|
|
// Some intrinsics were introduced in later versions of LLVM, but they have
|
2015-01-24 12:00:35 +01:00
|
|
|
// fallbacks in libc or libm and such.
|
2015-01-02 14:44:21 -08:00
|
|
|
macro_rules! compatible_ifn {
|
2015-01-24 12:00:35 +01:00
|
|
|
($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr, $llvm_version:expr) => (
|
|
|
|
if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
|
|
|
|
// The `if key == $name` is already in ifn!
|
|
|
|
ifn!($name, fn($($arg),*) -> $ret);
|
|
|
|
} else if *key == $name {
|
|
|
|
let f = declare::declare_cfn(ccx, stringify!($cname),
|
|
|
|
Type::func(&[$($arg),*], &$ret),
|
|
|
|
ty::mk_nil(ccx.tcx()));
|
|
|
|
ccx.intrinsics().borrow_mut().insert($name, f.clone());
|
|
|
|
return Some(f);
|
|
|
|
}
|
2014-04-09 19:56:31 -04:00
|
|
|
)
|
2015-01-02 14:44:21 -08:00
|
|
|
}
|
2014-04-09 19:56:31 -04:00
|
|
|
|
|
|
|
if ccx.sess().opts.debuginfo != NoDebugInfo {
|
2015-01-05 01:51:03 -05:00
|
|
|
ifn!("llvm.dbg.declare", fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
|
|
|
|
ifn!("llvm.dbg.value", fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
|
2014-04-09 19:56:31 -04:00
|
|
|
}
|
|
|
|
return None;
|
2013-06-13 14:49:01 +12:00
|
|
|
}
|