Skip to content

Commit 1ad1f5c

Browse files
committed
---
yaml --- r: 273230 b: refs/heads/beta c: 5af3c12 h: refs/heads/master
1 parent 91cf70e commit 1ad1f5c

File tree

7 files changed

+124
-151
lines changed

7 files changed

+124
-151
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: c3f856e7e24be568902a38c6a488aee6098f94cc
26+
refs/heads/beta: 5af3c12cfc07887b66ed1d8cd9e59e1c77cc8790
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/librustc_trans/trans/base.rs

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//! Translate the completed AST to the LLVM IR.
1111
//!
1212
//! Some functions here, such as trans_block and trans_expr, return a value --
13-
//! the result of the translation to LLVM -- while others, such as trans_fn,
14-
//! trans_impl, and trans_item, are called only for the side effect of adding a
13+
//! the result of the translation to LLVM -- while others, such as trans_fn
14+
//! and trans_item, are called only for the side effect of adding a
1515
//! particular definition to the LLVM IR output we're producing.
1616
//!
1717
//! Hopefully useful general knowledge about trans:
@@ -228,36 +228,6 @@ pub fn kind_for_closure(ccx: &CrateContext, closure_id: DefId) -> ty::ClosureKin
228228
*ccx.tcx().tables.borrow().closure_kinds.get(&closure_id).unwrap()
229229
}
230230

231-
pub fn get_extern_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
232-
did: DefId,
233-
t: Ty<'tcx>)
234-
-> ValueRef {
235-
let name = ccx.sess().cstore.item_symbol(did);
236-
let ty = type_of(ccx, t);
237-
if let Some(n) = ccx.externs().borrow_mut().get(&name) {
238-
return *n;
239-
}
240-
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
241-
// FIXME(nagisa): investigate whether it can be changed into define_global
242-
let c = declare::declare_global(ccx, &name[..], ty);
243-
// Thread-local statics in some other crate need to *always* be linked
244-
// against in a thread-local fashion, so we need to be sure to apply the
245-
// thread-local attribute locally if it was present remotely. If we
246-
// don't do this then linker errors can be generated where the linker
247-
// complains that one object files has a thread local version of the
248-
// symbol and another one doesn't.
249-
for attr in ccx.tcx().get_attrs(did).iter() {
250-
if attr.check_name("thread_local") {
251-
llvm::set_thread_local(c, true);
252-
}
253-
}
254-
if ccx.use_dll_storage_attrs() {
255-
llvm::SetDLLStorageClass(c, llvm::DLLImportStorageClass);
256-
}
257-
ccx.externs().borrow_mut().insert(name.to_string(), c);
258-
return c;
259-
}
260-
261231
fn require_alloc_fn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, info_ty: Ty<'tcx>, it: LangItem) -> DefId {
262232
match bcx.tcx().lang_items.require(it) {
263233
Ok(id) => id,
@@ -2714,11 +2684,11 @@ pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
27142684
}
27152685
}
27162686

2717-
fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2718-
id: ast::NodeId,
2719-
ty: Ty<'tcx>,
2720-
attrs: &[ast::Attribute])
2721-
-> String {
2687+
pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2688+
id: ast::NodeId,
2689+
ty: Ty<'tcx>,
2690+
attrs: &[ast::Attribute])
2691+
-> String {
27222692
match ccx.external_srcs().borrow().get(&id) {
27232693
Some(&did) => {
27242694
let sym = ccx.sess().cstore.item_symbol(did);
@@ -2768,25 +2738,6 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
27682738
let sym = || exported_name(ccx, id, ty, &i.attrs);
27692739

27702740
let v = match i.node {
2771-
hir::ItemStatic(..) => {
2772-
// If this static came from an external crate, then
2773-
// we need to get the symbol from metadata instead of
2774-
// using the current crate's name/version
2775-
// information in the hash of the symbol
2776-
let sym = sym();
2777-
debug!("making {}", sym);
2778-
2779-
// Create the global before evaluating the initializer;
2780-
// this is necessary to allow recursive statics.
2781-
let llty = type_of(ccx, ty);
2782-
let g = declare::define_global(ccx, &sym[..], llty).unwrap_or_else(|| {
2783-
ccx.sess()
2784-
.span_fatal(i.span, &format!("symbol `{}` is already defined", sym))
2785-
});
2786-
2787-
ccx.item_symbols().borrow_mut().insert(i.id, sym);
2788-
g
2789-
}
27902741

27912742
hir::ItemFn(_, _, _, abi, _, _) => {
27922743
let sym = sym();

branches/beta/src/librustc_trans/trans/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
241241

242242
// Create the closure.
243243
for (i, freevar) in freevars.iter().enumerate() {
244-
let datum = expr::trans_local_var(bcx, freevar.def);
244+
let datum = expr::trans_var(bcx, freevar.def);
245245
let upvar_slot_dest = adt::trans_field_ptr(
246246
bcx, &repr, adt::MaybeSizedValue::sized(dest_addr), Disr(0), i);
247247
let upvar_id = ty::UpvarId { var_id: freevar.def.var_id(),

branches/beta/src/librustc_trans/trans/common.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,14 +1341,3 @@ pub fn shift_mask_val<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
13411341
_ => panic!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
13421342
}
13431343
}
1344-
1345-
pub fn get_static_val<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1346-
did: DefId,
1347-
ty: Ty<'tcx>)
1348-
-> ValueRef {
1349-
if let Some(node_id) = ccx.tcx().map.as_local_node_id(did) {
1350-
base::get_item_val(ccx, node_id)
1351-
} else {
1352-
base::get_extern_const(ccx, did, ty)
1353-
}
1354-
}

branches/beta/src/librustc_trans/trans/consts.rs

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ use middle::cstore::LOCAL_CRATE;
1818
use middle::const_eval::{self, ConstEvalErr};
1919
use middle::def::Def;
2020
use middle::def_id::DefId;
21+
use rustc::front::map as hir_map;
2122
use trans::{adt, closure, debuginfo, expr, inline, machine};
22-
use trans::base::{self, push_ctxt};
23+
use trans::base::{self, exported_name, push_ctxt};
2324
use trans::callee::Callee;
2425
use trans::collector::{self, TransItem};
2526
use trans::common::{self, type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
2627
use trans::common::{CrateContext, C_integral, C_floating, C_bool, C_str_slice, C_bytes, val_ty};
2728
use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, VariantInfo, C_uint};
2829
use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null, ExprId, MethodCallKey};
30+
use trans::datum::{Datum, Lvalue};
2931
use trans::declare;
3032
use trans::monomorphize;
33+
use trans::foreign;
3134
use trans::type_::Type;
3235
use trans::type_of;
3336
use trans::value::Value;
@@ -46,7 +49,7 @@ use std::ffi::{CStr, CString};
4649
use std::borrow::Cow;
4750
use libc::c_uint;
4851
use syntax::ast::{self, LitKind};
49-
use syntax::attr;
52+
use syntax::attr::{self, AttrMetaMethods};
5053
use syntax::parse::token;
5154
use syntax::ptr::P;
5255

@@ -806,7 +809,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
806809
}
807810
let opt_def = cx.tcx().def_map.borrow().get(&cur.id).map(|d| d.full_def());
808811
if let Some(Def::Static(def_id, _)) = opt_def {
809-
common::get_static_val(cx, def_id, ety)
812+
get_static(cx, def_id).val
810813
} else {
811814
// If this isn't the address of a static, then keep going through
812815
// normal constant evaluation.
@@ -1013,6 +1016,65 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
10131016
})
10141017
}
10151018

1019+
pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
1020+
-> Datum<'tcx, Lvalue> {
1021+
let ty = ccx.tcx().lookup_item_type(def_id).ty;
1022+
1023+
let g = if let Some(id) = ccx.tcx().map.as_local_node_id(def_id) {
1024+
match ccx.tcx().map.get(id) {
1025+
hir_map::NodeItem(&hir::Item {
1026+
ref attrs, span, node: hir::ItemStatic(..), ..
1027+
}) => {
1028+
// If this static came from an external crate, then
1029+
// we need to get the symbol from metadata instead of
1030+
// using the current crate's name/version
1031+
// information in the hash of the symbol
1032+
let sym = exported_name(ccx, id, ty, attrs);
1033+
debug!("making {}", sym);
1034+
1035+
// Create the global before evaluating the initializer;
1036+
// this is necessary to allow recursive statics.
1037+
let llty = type_of::type_of(ccx, ty);
1038+
let g = declare::define_global(ccx, &sym, llty).unwrap_or_else(|| {
1039+
ccx.sess().span_fatal(span,
1040+
&format!("symbol `{}` is already defined", sym))
1041+
});
1042+
1043+
ccx.item_symbols().borrow_mut().insert(id, sym);
1044+
g
1045+
}
1046+
1047+
hir_map::NodeForeignItem(ni @ &hir::ForeignItem {
1048+
node: hir::ForeignItemStatic(..), ..
1049+
}) => foreign::register_static(ccx, ni),
1050+
1051+
item => unreachable!("get_static: expected static, found {:?}", item)
1052+
}
1053+
} else {
1054+
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
1055+
// FIXME(nagisa): investigate whether it can be changed into define_global
1056+
let name = ccx.sess().cstore.item_symbol(def_id);
1057+
let g = declare::declare_global(ccx, &name, type_of::type_of(ccx, ty));
1058+
// Thread-local statics in some other crate need to *always* be linked
1059+
// against in a thread-local fashion, so we need to be sure to apply the
1060+
// thread-local attribute locally if it was present remotely. If we
1061+
// don't do this then linker errors can be generated where the linker
1062+
// complains that one object files has a thread local version of the
1063+
// symbol and another one doesn't.
1064+
for attr in ccx.tcx().get_attrs(def_id).iter() {
1065+
if attr.check_name("thread_local") {
1066+
llvm::set_thread_local(g, true);
1067+
}
1068+
}
1069+
if ccx.use_dll_storage_attrs() {
1070+
llvm::SetDLLStorageClass(g, llvm::DLLImportStorageClass);
1071+
}
1072+
g
1073+
};
1074+
1075+
Datum::new(g, ty, Lvalue::new("static"))
1076+
}
1077+
10161078
pub fn trans_static(ccx: &CrateContext,
10171079
m: hir::Mutability,
10181080
expr: &hir::Expr,
@@ -1026,7 +1088,8 @@ pub fn trans_static(ccx: &CrateContext,
10261088

10271089
unsafe {
10281090
let _icx = push_ctxt("trans_static");
1029-
let g = base::get_item_val(ccx, id);
1091+
let def_id = ccx.tcx().map.local_def_id(id);
1092+
let datum = get_static(ccx, def_id);
10301093

10311094
let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
10321095
let (v, _) = try!(const_expr(
@@ -1039,40 +1102,39 @@ pub fn trans_static(ccx: &CrateContext,
10391102

10401103
// boolean SSA values are i1, but they have to be stored in i8 slots,
10411104
// otherwise some LLVM optimization passes don't work as expected
1042-
let mut val_llty = llvm::LLVMTypeOf(v);
1043-
let v = if val_llty == Type::i1(ccx).to_ref() {
1044-
val_llty = Type::i8(ccx).to_ref();
1045-
llvm::LLVMConstZExt(v, val_llty)
1105+
let mut val_llty = val_ty(v);
1106+
let v = if val_llty == Type::i1(ccx) {
1107+
val_llty = Type::i8(ccx);
1108+
llvm::LLVMConstZExt(v, val_llty.to_ref())
10461109
} else {
10471110
v
10481111
};
10491112

1050-
let ty = ccx.tcx().node_id_to_type(id);
1051-
let llty = type_of::type_of(ccx, ty);
1052-
let g = if val_llty == llty.to_ref() {
1053-
g
1113+
let llty = type_of::type_of(ccx, datum.ty);
1114+
let g = if val_llty == llty {
1115+
datum.val
10541116
} else {
10551117
// If we created the global with the wrong type,
10561118
// correct the type.
10571119
let empty_string = CString::new("").unwrap();
1058-
let name_str_ref = CStr::from_ptr(llvm::LLVMGetValueName(g));
1120+
let name_str_ref = CStr::from_ptr(llvm::LLVMGetValueName(datum.val));
10591121
let name_string = CString::new(name_str_ref.to_bytes()).unwrap();
1060-
llvm::LLVMSetValueName(g, empty_string.as_ptr());
1122+
llvm::LLVMSetValueName(datum.val, empty_string.as_ptr());
10611123
let new_g = llvm::LLVMGetOrInsertGlobal(
1062-
ccx.llmod(), name_string.as_ptr(), val_llty);
1124+
ccx.llmod(), name_string.as_ptr(), val_llty.to_ref());
10631125
// To avoid breaking any invariants, we leave around the old
10641126
// global for the moment; we'll replace all references to it
10651127
// with the new global later. (See base::trans_crate.)
1066-
ccx.statics_to_rauw().borrow_mut().push((g, new_g));
1128+
ccx.statics_to_rauw().borrow_mut().push((datum.val, new_g));
10671129
new_g
10681130
};
1069-
llvm::LLVMSetAlignment(g, type_of::align_of(ccx, ty));
1131+
llvm::LLVMSetAlignment(g, type_of::align_of(ccx, datum.ty));
10701132
llvm::LLVMSetInitializer(g, v);
10711133

10721134
// As an optimization, all shared statics which do not have interior
10731135
// mutability are placed into read-only memory.
10741136
if m != hir::MutMutable {
1075-
let tcontents = ty.type_contents(ccx.tcx());
1137+
let tcontents = datum.ty.type_contents(ccx.tcx());
10761138
if !tcontents.interior_unsafe() {
10771139
llvm::LLVMSetGlobalConstant(g, llvm::True);
10781140
}

0 commit comments

Comments
 (0)