1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use llvm;
use llvm::{SetUnnamedAddr};
use llvm::{ValueRef, True};
use rustc_const_eval::ConstEvalErr;
use rustc::hir::def_id::DefId;
use rustc::hir::map as hir_map;
use {debuginfo, machine};
use base;
use trans_item::TransItem;
use common::{self, CrateContext, val_ty};
use declare;
use monomorphize::Instance;
use type_::Type;
use type_of;
use rustc::ty;
use rustc::ty::subst::Substs;
use rustc::hir;
use std::ffi::{CStr, CString};
use syntax::ast;
use syntax::attr;
pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
unsafe {
llvm::LLVMConstPointerCast(val, ty.to_ref())
}
}
pub fn addr_of_mut(ccx: &CrateContext,
cv: ValueRef,
align: machine::llalign,
kind: &str)
-> ValueRef {
unsafe {
let name = ccx.generate_local_symbol_name(kind);
let gv = declare::define_global(ccx, &name[..], val_ty(cv)).unwrap_or_else(||{
bug!("symbol `{}` is already defined", name);
});
llvm::LLVMSetInitializer(gv, cv);
llvm::LLVMSetAlignment(gv, align);
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::InternalLinkage);
SetUnnamedAddr(gv, true);
gv
}
}
pub fn addr_of(ccx: &CrateContext,
cv: ValueRef,
align: machine::llalign,
kind: &str)
-> ValueRef {
if let Some(&gv) = ccx.const_globals().borrow().get(&cv) {
unsafe {
if align > llvm::LLVMGetAlignment(gv) {
llvm::LLVMSetAlignment(gv, align);
}
}
return gv;
}
let gv = addr_of_mut(ccx, cv, align, kind);
unsafe {
llvm::LLVMSetGlobalConstant(gv, True);
}
ccx.const_globals().borrow_mut().insert(cv, gv);
gv
}
pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
let instance = Instance::mono(ccx.shared(), def_id);
if let Some(&g) = ccx.instances().borrow().get(&instance) {
return g;
}
let ty = common::def_ty(ccx.shared(), def_id, Substs::empty());
let g = if let Some(id) = ccx.tcx().hir.as_local_node_id(def_id) {
let llty = type_of::type_of(ccx, ty);
let (g, attrs) = match ccx.tcx().hir.get(id) {
hir_map::NodeItem(&hir::Item {
ref attrs, span, node: hir::ItemStatic(..), ..
}) => {
let sym = ccx.symbol_map()
.get(TransItem::Static(id))
.expect("Local statics should always be in the SymbolMap");
let defined_in_current_codegen_unit = ccx.codegen_unit()
.items()
.contains_key(&TransItem::Static(id));
assert!(!defined_in_current_codegen_unit);
if declare::get_declared_value(ccx, sym).is_some() {
span_bug!(span, "trans: Conflicting symbol names for static?");
}
let g = declare::define_global(ccx, sym, llty).unwrap();
(g, attrs)
}
hir_map::NodeForeignItem(&hir::ForeignItem {
ref attrs, span, node: hir::ForeignItemStatic(..), ..
}) => {
let sym = instance.symbol_name(ccx.shared());
let g = if let Some(name) =
attr::first_attr_value_str_by_name(&attrs, "linkage") {
let linkage = match base::llvm_linkage_by_name(&name.as_str()) {
Some(linkage) => linkage,
None => {
ccx.sess().span_fatal(span, "invalid linkage specified");
}
};
let llty2 = match ty.sty {
ty::TyRawPtr(ref mt) => type_of::type_of(ccx, mt.ty),
_ => {
ccx.sess().span_fatal(span, "must have type `*const T` or `*mut T`");
}
};
unsafe {
let g1 = declare::declare_global(ccx, &sym, llty2);
llvm::LLVMRustSetLinkage(g1, linkage);
let mut real_name = "_rust_extern_with_linkage_".to_string();
real_name.push_str(&sym);
let g2 = declare::define_global(ccx, &real_name, llty).unwrap_or_else(||{
ccx.sess().span_fatal(span,
&format!("symbol `{}` is already defined", &sym))
});
llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
llvm::LLVMSetInitializer(g2, g1);
g2
}
} else {
declare::declare_global(ccx, &sym, llty)
};
(g, attrs)
}
item => bug!("get_static: expected static, found {:?}", item)
};
for attr in attrs {
if attr.check_name("thread_local") {
llvm::set_thread_local(g, true);
}
}
g
} else {
let sym = instance.symbol_name(ccx.shared());
let g = declare::declare_global(ccx, &sym, type_of::type_of(ccx, ty));
for attr in ccx.tcx().get_attrs(def_id).iter() {
if attr.check_name("thread_local") {
llvm::set_thread_local(g, true);
}
}
if ccx.use_dll_storage_attrs() && !ccx.sess().cstore.is_foreign_item(def_id) {
unsafe {
llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
}
}
g
};
if ccx.use_dll_storage_attrs() && ccx.sess().cstore.is_dllimport_foreign_item(def_id) {
unsafe {
llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
}
}
ccx.instances().borrow_mut().insert(instance, g);
ccx.statics().borrow_mut().insert(g, def_id);
g
}
pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
m: hir::Mutability,
id: ast::NodeId,
attrs: &[ast::Attribute])
-> Result<ValueRef, ConstEvalErr<'tcx>> {
unsafe {
let def_id = ccx.tcx().hir.local_def_id(id);
let g = get_static(ccx, def_id);
let v = ::mir::trans_static_initializer(ccx, def_id)?;
let mut val_llty = val_ty(v);
let v = if val_llty == Type::i1(ccx) {
val_llty = Type::i8(ccx);
llvm::LLVMConstZExt(v, val_llty.to_ref())
} else {
v
};
let ty = common::def_ty(ccx.shared(), def_id, Substs::empty());
let llty = type_of::type_of(ccx, ty);
let g = if val_llty == llty {
g
} else {
let empty_string = CString::new("").unwrap();
let name_str_ref = CStr::from_ptr(llvm::LLVMGetValueName(g));
let name_string = CString::new(name_str_ref.to_bytes()).unwrap();
llvm::LLVMSetValueName(g, empty_string.as_ptr());
let new_g = llvm::LLVMRustGetOrInsertGlobal(
ccx.llmod(), name_string.as_ptr(), val_llty.to_ref());
ccx.statics_to_rauw().borrow_mut().push((g, new_g));
new_g
};
llvm::LLVMSetAlignment(g, type_of::align_of(ccx, ty));
llvm::LLVMSetInitializer(g, v);
if m != hir::MutMutable {
let tcontents = ty.type_contents(ccx.tcx());
if !tcontents.interior_unsafe() {
llvm::LLVMSetGlobalConstant(g, llvm::True);
}
}
debuginfo::create_global_var_metadata(ccx, id, g);
if attr::contains_name(attrs,
"thread_local") {
llvm::set_thread_local(g, true);
}
base::set_link_section(ccx, g, attrs);
Ok(g)
}
}