Skip to content

Commit 3dbf2c3

Browse files
committed
auto merge of #5592 : pcwalton/rust/xc-extern-statics, r=pcwalton
2 parents e549b80 + 58338dd commit 3dbf2c3

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

src/librustc/middle/trans/base.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2540,8 +2540,9 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::node_id) -> ValueRef {
25402540
}
25412541
}
25422542
2543-
_ => {
2544-
ccx.sess.bug(~"get_item_val(): unexpected variant")
2543+
ref variant => {
2544+
ccx.sess.bug(fmt!("get_item_val(): unexpected variant: %?",
2545+
variant))
25452546
}
25462547
};
25472548
if !(exprt || ccx.reachable.contains(&id)) {
@@ -3085,6 +3086,7 @@ pub fn trans_crate(sess: session::Session,
30853086
const_cstr_cache: @mut LinearMap::new(),
30863087
const_globals: @mut LinearMap::new(),
30873088
const_values: @mut LinearMap::new(),
3089+
extern_const_values: @mut LinearMap::new(),
30883090
module_data: @mut LinearMap::new(),
30893091
lltypes: @mut LinearMap::new(),
30903092
llsizingtypes: @mut LinearMap::new(),

src/librustc/middle/trans/common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ pub struct CrateContext {
201201

202202
// Cache of emitted const values
203203
const_values: @mut LinearMap<ast::node_id, ValueRef>,
204+
205+
// Cache of external const values
206+
extern_const_values: @mut LinearMap<ast::def_id, ValueRef>,
207+
204208
module_data: @mut LinearMap<~str, ValueRef>,
205209
lltypes: @mut LinearMap<ty::t, TypeRef>,
206210
llsizingtypes: @mut LinearMap<ty::t, TypeRef>,

src/librustc/middle/trans/expr.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ use core::prelude::*;
124124
use back::abi;
125125
use lib;
126126
use lib::llvm::{ValueRef, TypeRef, llvm, True};
127+
use metadata::csearch;
127128
use middle::borrowck::root_map_key;
128129
use middle::trans::_match;
129130
use middle::trans::adt;
@@ -150,6 +151,7 @@ use middle::ty::{AutoPtr, AutoBorrowVec, AutoBorrowVecRef, AutoBorrowFn,
150151
use util::common::indenter;
151152
use util::ppaux::ty_to_str;
152153

154+
use core::cast::transmute;
153155
use core::hashmap::linear::LinearMap;
154156
use syntax::print::pprust::{expr_to_str};
155157
use syntax::ast;
@@ -1079,11 +1081,35 @@ fn trans_lvalue_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
10791081
10801082
fn get_val(bcx: block, did: ast::def_id, const_ty: ty::t)
10811083
-> ValueRef {
1082-
// The LLVM global has the type of its initializer,
1083-
// which may not be equal to the enum's type for
1084-
// non-C-like enums.
1085-
PointerCast(bcx, base::get_item_val(bcx.ccx(), did.node),
1086-
T_ptr(type_of(bcx.ccx(), const_ty)))
1084+
if did.crate == ast::local_crate {
1085+
// The LLVM global has the type of its initializer,
1086+
// which may not be equal to the enum's type for
1087+
// non-C-like enums.
1088+
PointerCast(bcx,
1089+
base::get_item_val(bcx.ccx(), did.node),
1090+
T_ptr(type_of(bcx.ccx(), const_ty)))
1091+
} else {
1092+
// For external constants, we don't inline.
1093+
match bcx.ccx().extern_const_values.find(&did) {
1094+
None => {
1095+
unsafe {
1096+
let llty = type_of(bcx.ccx(), const_ty);
1097+
let symbol = csearch::get_symbol(
1098+
bcx.ccx().sess.cstore,
1099+
did);
1100+
let llval = llvm::LLVMAddGlobal(
1101+
bcx.ccx().llmod,
1102+
llty,
1103+
transmute::<&u8,*i8>(&symbol[0]));
1104+
bcx.ccx().extern_const_values.insert(
1105+
did,
1106+
llval);
1107+
llval
1108+
}
1109+
}
1110+
Some(llval) => *llval
1111+
}
1112+
}
10871113
}
10881114
10891115
let did = get_did(ccx, did);

0 commit comments

Comments
 (0)