Skip to content

Commit 48c3baa

Browse files
committed
Remove const_{cstr,str_slice,get_elt,get_real} and is_const_real methods from cg_ssa
This introduces the static_panic_msg trait method to StaticBuilderMethods.
1 parent fbd969b commit 48c3baa

File tree

6 files changed

+129
-110
lines changed

6 files changed

+129
-110
lines changed

src/librustc_codegen_llvm/builder.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::context::CodegenCx;
55
use crate::type_::Type;
66
use crate::type_of::LayoutLlvmExt;
77
use crate::value::Value;
8+
use syntax::symbol::LocalInternedString;
89
use rustc_codegen_ssa::common::{IntPredicate, TypeKind, RealPredicate};
910
use rustc_codegen_ssa::MemFlags;
1011
use libc::{c_uint, c_char};
@@ -1475,6 +1476,36 @@ impl StaticBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
14751476
// Forward to the `get_static` method of `CodegenCx`
14761477
self.cx().get_static(def_id)
14771478
}
1479+
1480+
fn static_panic_msg(
1481+
&mut self,
1482+
msg: Option<LocalInternedString>,
1483+
filename: LocalInternedString,
1484+
line: Self::Value,
1485+
col: Self::Value,
1486+
kind: &str,
1487+
) -> Self::Value {
1488+
let align = self.tcx.data_layout.aggregate_align.abi
1489+
.max(self.tcx.data_layout.i32_align.abi)
1490+
.max(self.tcx.data_layout.pointer_align.abi);
1491+
1492+
let filename = self.const_str_slice(filename);
1493+
1494+
let with_msg_components;
1495+
let without_msg_components;
1496+
1497+
let components = if let Some(msg) = msg {
1498+
let msg = self.const_str_slice(msg);
1499+
with_msg_components = [msg, filename, line, col];
1500+
&with_msg_components as &[_]
1501+
} else {
1502+
without_msg_components = [filename, line, col];
1503+
&without_msg_components as &[_]
1504+
};
1505+
1506+
let struct_ = self.const_struct(&components, false);
1507+
self.static_addr_of(struct_, align, Some(kind))
1508+
}
14781509
}
14791510

14801511
impl Builder<'a, 'll, 'tcx> {

src/librustc_codegen_llvm/common.rs

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,72 @@ impl CodegenCx<'ll, 'tcx> {
119119
pub fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
120120
bytes_in_context(self.llcx, bytes)
121121
}
122+
123+
fn const_cstr(
124+
&self,
125+
s: LocalInternedString,
126+
null_terminated: bool,
127+
) -> &'ll Value {
128+
unsafe {
129+
if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
130+
return llval;
131+
}
132+
133+
let sc = llvm::LLVMConstStringInContext(self.llcx,
134+
s.as_ptr() as *const c_char,
135+
s.len() as c_uint,
136+
!null_terminated as Bool);
137+
let sym = self.generate_local_symbol_name("str");
138+
let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(||{
139+
bug!("symbol `{}` is already defined", sym);
140+
});
141+
llvm::LLVMSetInitializer(g, sc);
142+
llvm::LLVMSetGlobalConstant(g, True);
143+
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
144+
145+
self.const_cstr_cache.borrow_mut().insert(s, g);
146+
g
147+
}
148+
}
149+
150+
pub fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
151+
let len = s.len();
152+
let cs = consts::ptrcast(self.const_cstr(s, false),
153+
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
154+
self.const_fat_ptr(cs, self.const_usize(len as u64))
155+
}
156+
157+
pub fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
158+
unsafe {
159+
assert_eq!(idx as c_uint as u64, idx);
160+
let us = &[idx as c_uint];
161+
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
162+
163+
debug!("const_get_elt(v={:?}, idx={}, r={:?})",
164+
v, idx, r);
165+
166+
r
167+
}
168+
}
169+
170+
pub fn const_get_real(&self, v: &'ll Value) -> Option<(f64, bool)> {
171+
unsafe {
172+
if self.is_const_real(v) {
173+
let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
174+
let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
175+
let loses_info = if loses_info == 1 { true } else { false };
176+
Some((r, loses_info))
177+
} else {
178+
None
179+
}
180+
}
181+
}
182+
183+
fn is_const_real(&self, v: &'ll Value) -> bool {
184+
unsafe {
185+
llvm::LLVMIsAConstantFP(v).is_some()
186+
}
187+
}
122188
}
123189

124190
impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
@@ -183,40 +249,6 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
183249
self.const_uint(self.type_i8(), i as u64)
184250
}
185251

186-
fn const_cstr(
187-
&self,
188-
s: LocalInternedString,
189-
null_terminated: bool,
190-
) -> &'ll Value {
191-
unsafe {
192-
if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
193-
return llval;
194-
}
195-
196-
let sc = llvm::LLVMConstStringInContext(self.llcx,
197-
s.as_ptr() as *const c_char,
198-
s.len() as c_uint,
199-
!null_terminated as Bool);
200-
let sym = self.generate_local_symbol_name("str");
201-
let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(||{
202-
bug!("symbol `{}` is already defined", sym);
203-
});
204-
llvm::LLVMSetInitializer(g, sc);
205-
llvm::LLVMSetGlobalConstant(g, True);
206-
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
207-
208-
self.const_cstr_cache.borrow_mut().insert(s, g);
209-
g
210-
}
211-
}
212-
213-
fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
214-
let len = s.len();
215-
let cs = consts::ptrcast(self.const_cstr(s, false),
216-
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
217-
self.const_fat_ptr(cs, self.const_usize(len as u64))
218-
}
219-
220252
fn const_struct(
221253
&self,
222254
elts: &[&'ll Value],
@@ -225,32 +257,6 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
225257
struct_in_context(self.llcx, elts, packed)
226258
}
227259

228-
fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
229-
unsafe {
230-
assert_eq!(idx as c_uint as u64, idx);
231-
let us = &[idx as c_uint];
232-
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
233-
234-
debug!("const_get_elt(v={:?}, idx={}, r={:?})",
235-
v, idx, r);
236-
237-
r
238-
}
239-
}
240-
241-
fn const_get_real(&self, v: &'ll Value) -> Option<(f64, bool)> {
242-
unsafe {
243-
if self.is_const_real(v) {
244-
let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
245-
let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
246-
let loses_info = if loses_info == 1 { true } else { false };
247-
Some((r, loses_info))
248-
} else {
249-
None
250-
}
251-
}
252-
}
253-
254260
fn const_to_uint(&self, v: &'ll Value) -> u64 {
255261
unsafe {
256262
llvm::LLVMConstIntGetZExtValue(v)
@@ -263,12 +269,6 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
263269
}
264270
}
265271

266-
fn is_const_real(&self, v: &'ll Value) -> bool {
267-
unsafe {
268-
llvm::LLVMIsAConstantFP(v).is_some()
269-
}
270-
}
271-
272272
fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
273273
unsafe {
274274
if self.is_const_integral(v) {

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc::middle::lang_items;
22
use rustc::ty::{self, Ty, TypeFoldable};
3-
use rustc::ty::layout::{self, HasTyCtxt};
3+
use rustc::ty::layout::{self, HasTyCtxt, LayoutOf};
44
use rustc::mir;
55
use rustc::mir::interpret::EvalErrorKind;
66
use rustc_target::abi::call::{ArgType, FnType, PassMode, IgnoreMode};
@@ -394,43 +394,37 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
394394
// Get the location information.
395395
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
396396
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
397-
let filename = bx.const_str_slice(filename);
398397
let line = bx.const_u32(loc.line as u32);
399398
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
400-
let align = self.cx.tcx().data_layout.aggregate_align.abi
401-
.max(self.cx.tcx().data_layout.i32_align.abi)
402-
.max(self.cx.tcx().data_layout.pointer_align.abi);
403399

404400
// Put together the arguments to the panic entry point.
405401
let (lang_item, args) = match *msg {
406402
EvalErrorKind::BoundsCheck { ref len, ref index } => {
407403
let len = self.codegen_operand(&mut bx, len).immediate();
408404
let index = self.codegen_operand(&mut bx, index).immediate();
409405

410-
let file_line_col = bx.const_struct(&[filename, line, col], false);
411-
let file_line_col = bx.static_addr_of(
412-
file_line_col,
413-
align,
414-
Some("panic_bounds_check_loc")
406+
let file_line_col = bx.static_panic_msg(
407+
None,
408+
filename,
409+
line,
410+
col,
411+
"panic_bounds_check_loc",
415412
);
416413
(lang_items::PanicBoundsCheckFnLangItem,
417-
vec![file_line_col, index, len])
414+
vec![file_line_col, index, len])
418415
}
419416
_ => {
420417
let str = msg.description();
421418
let msg_str = Symbol::intern(str).as_str();
422-
let msg_str = bx.const_str_slice(msg_str);
423-
let msg_file_line_col = bx.const_struct(
424-
&[msg_str, filename, line, col],
425-
false
426-
);
427-
let msg_file_line_col = bx.static_addr_of(
428-
msg_file_line_col,
429-
align,
430-
Some("panic_loc")
419+
let msg_file_line_col = bx.static_panic_msg(
420+
Some(msg_str),
421+
filename,
422+
line,
423+
col,
424+
"panic_loc",
431425
);
432426
(lang_items::PanicFnLangItem,
433-
vec![msg_file_line_col])
427+
vec![msg_file_line_col])
434428
}
435429
};
436430

@@ -534,27 +528,20 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
534528
if layout.abi.is_uninhabited() {
535529
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
536530
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
537-
let filename = bx.const_str_slice(filename);
538531
let line = bx.const_u32(loc.line as u32);
539532
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
540-
let align = self.cx.tcx().data_layout.aggregate_align.abi
541-
.max(self.cx.tcx().data_layout.i32_align.abi)
542-
.max(self.cx.tcx().data_layout.pointer_align.abi);
543533

544534
let str = format!(
545535
"Attempted to instantiate uninhabited type {}",
546536
ty
547537
);
548538
let msg_str = Symbol::intern(&str).as_str();
549-
let msg_str = bx.const_str_slice(msg_str);
550-
let msg_file_line_col = bx.const_struct(
551-
&[msg_str, filename, line, col],
552-
false,
553-
);
554-
let msg_file_line_col = bx.static_addr_of(
555-
msg_file_line_col,
556-
align,
557-
Some("panic_loc"),
539+
let msg_file_line_col = bx.static_panic_msg(
540+
Some(msg_str),
541+
filename,
542+
line,
543+
col,
544+
"panic_loc",
558545
);
559546

560547
// Obtain the panic entry point.

src/librustc_codegen_ssa/mir/place.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
439439
// NB: The layout of a static may be unsized as is the case when working
440440
// with a static that is an extern_type.
441441
let layout = cx.layout_of(self.monomorphize(&ty));
442-
PlaceRef::new_thin_place(bx, bx.get_static(def_id), layout, layout.align.abi)
442+
let static_ = bx.get_static(def_id);
443+
PlaceRef::new_thin_place(bx, static_, layout, layout.align.abi)
443444
},
444445
mir::Place::Projection(box mir::Projection {
445446
ref base,

src/librustc_codegen_ssa/traits/consts.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::mir::place::PlaceRef;
33
use rustc::mir::interpret::Allocation;
44
use rustc::mir::interpret::Scalar;
55
use rustc::ty::layout;
6-
use syntax::symbol::LocalInternedString;
76

87
pub trait ConstMethods<'tcx>: BackendTypes {
98
// Constant constructors
@@ -19,20 +18,12 @@ pub trait ConstMethods<'tcx>: BackendTypes {
1918
fn const_usize(&self, i: u64) -> Self::Value;
2019
fn const_u8(&self, i: u8) -> Self::Value;
2120

22-
// This is a 'c-like' raw string, which differs from
23-
// our boxed-and-length-annotated strings.
24-
fn const_cstr(&self, s: LocalInternedString, null_terminated: bool) -> Self::Value;
25-
26-
fn const_str_slice(&self, s: LocalInternedString) -> Self::Value;
2721
fn const_struct(&self, elts: &[Self::Value], packed: bool) -> Self::Value;
2822

29-
fn const_get_elt(&self, v: Self::Value, idx: u64) -> Self::Value;
30-
fn const_get_real(&self, v: Self::Value) -> Option<(f64, bool)>;
3123
fn const_to_uint(&self, v: Self::Value) -> u64;
3224
fn const_to_opt_u128(&self, v: Self::Value, sign_ext: bool) -> Option<u128>;
3325

3426
fn is_const_integral(&self, v: Self::Value) -> bool;
35-
fn is_const_real(&self, v: Self::Value) -> bool;
3627

3728
fn scalar_to_backend(
3829
&self,

src/librustc_codegen_ssa/traits/statics.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::BackendTypes;
2+
use syntax_pos::symbol::LocalInternedString;
23
use rustc::hir::def_id::DefId;
34
use rustc::ty::layout::Align;
45

@@ -9,4 +10,12 @@ pub trait StaticMethods: BackendTypes {
910

1011
pub trait StaticBuilderMethods<'tcx>: BackendTypes {
1112
fn get_static(&mut self, def_id: DefId) -> Self::Value;
13+
fn static_panic_msg(
14+
&mut self,
15+
msg: Option<LocalInternedString>,
16+
filename: LocalInternedString,
17+
line: Self::Value,
18+
col: Self::Value,
19+
kind: &str,
20+
) -> Self::Value;
1221
}

0 commit comments

Comments
 (0)