Skip to content

Commit 7b94195

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 a3fa116 commit 7b94195

File tree

5 files changed

+126
-108
lines changed

5 files changed

+126
-108
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: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -399,43 +399,37 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
399399
// Get the location information.
400400
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
401401
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
402-
let filename = bx.const_str_slice(filename);
403402
let line = bx.const_u32(loc.line as u32);
404403
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
405-
let align = self.cx.tcx().data_layout.aggregate_align.abi
406-
.max(self.cx.tcx().data_layout.i32_align.abi)
407-
.max(self.cx.tcx().data_layout.pointer_align.abi);
408404

409405
// Put together the arguments to the panic entry point.
410406
let (lang_item, args) = match *msg {
411407
EvalErrorKind::BoundsCheck { ref len, ref index } => {
412408
let len = self.codegen_operand(&mut bx, len).immediate();
413409
let index = self.codegen_operand(&mut bx, index).immediate();
414410

415-
let file_line_col = bx.const_struct(&[filename, line, col], false);
416-
let file_line_col = bx.static_addr_of(
417-
file_line_col,
418-
align,
419-
Some("panic_bounds_check_loc")
411+
let file_line_col = bx.static_panic_msg(
412+
None,
413+
filename,
414+
line,
415+
col,
416+
"panic_bounds_check_loc",
420417
);
421418
(lang_items::PanicBoundsCheckFnLangItem,
422-
vec![file_line_col, index, len])
419+
vec![file_line_col, index, len])
423420
}
424421
_ => {
425422
let str = msg.description();
426423
let msg_str = Symbol::intern(str).as_str();
427-
let msg_str = bx.const_str_slice(msg_str);
428-
let msg_file_line_col = bx.const_struct(
429-
&[msg_str, filename, line, col],
430-
false
431-
);
432-
let msg_file_line_col = bx.static_addr_of(
433-
msg_file_line_col,
434-
align,
435-
Some("panic_loc")
424+
let msg_file_line_col = bx.static_panic_msg(
425+
Some(msg_str),
426+
filename,
427+
line,
428+
col,
429+
"panic_loc",
436430
);
437431
(lang_items::PanicFnLangItem,
438-
vec![msg_file_line_col])
432+
vec![msg_file_line_col])
439433
}
440434
};
441435

@@ -539,27 +533,20 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
539533
if layout.abi.is_uninhabited() {
540534
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
541535
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
542-
let filename = bx.const_str_slice(filename);
543536
let line = bx.const_u32(loc.line as u32);
544537
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
545-
let align = self.cx.tcx().data_layout.aggregate_align.abi
546-
.max(self.cx.tcx().data_layout.i32_align.abi)
547-
.max(self.cx.tcx().data_layout.pointer_align.abi);
548538

549539
let str = format!(
550540
"Attempted to instantiate uninhabited type {}",
551541
ty
552542
);
553543
let msg_str = Symbol::intern(&str).as_str();
554-
let msg_str = bx.const_str_slice(msg_str);
555-
let msg_file_line_col = bx.const_struct(
556-
&[msg_str, filename, line, col],
557-
false,
558-
);
559-
let msg_file_line_col = bx.static_addr_of(
560-
msg_file_line_col,
561-
align,
562-
Some("panic_loc"),
544+
let msg_file_line_col = bx.static_panic_msg(
545+
Some(msg_str),
546+
filename,
547+
line,
548+
col,
549+
"panic_loc",
563550
);
564551

565552
// Obtain the panic entry point.

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)