|
2 | 2 |
|
3 | 3 | //! Code that is useful in various codegen modules.
|
4 | 4 |
|
5 |
| -use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef}; |
| 5 | +use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef, ConstantInt}; |
6 | 6 | use crate::abi;
|
7 | 7 | use crate::consts;
|
8 | 8 | use crate::type_::Type;
|
@@ -86,6 +86,8 @@ impl Funclet<'ll> {
|
86 | 86 |
|
87 | 87 | impl BackendTypes for CodegenCx<'ll, 'tcx> {
|
88 | 88 | type Value = &'ll Value;
|
| 89 | + type Function = &'ll Value; |
| 90 | + |
89 | 91 | type BasicBlock = &'ll BasicBlock;
|
90 | 92 | type Type = &'ll Type;
|
91 | 93 | type Funclet = Funclet<'ll>;
|
@@ -243,33 +245,23 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
243 | 245 | struct_in_context(self.llcx, elts, packed)
|
244 | 246 | }
|
245 | 247 |
|
246 |
| - fn const_to_uint(&self, v: &'ll Value) -> u64 { |
247 |
| - unsafe { |
| 248 | + fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> { |
| 249 | + try_as_const_integral(v).map(|v| unsafe { |
248 | 250 | llvm::LLVMConstIntGetZExtValue(v)
|
249 |
| - } |
250 |
| - } |
251 |
| - |
252 |
| - fn is_const_integral(&self, v: &'ll Value) -> bool { |
253 |
| - unsafe { |
254 |
| - llvm::LLVMIsAConstantInt(v).is_some() |
255 |
| - } |
| 251 | + }) |
256 | 252 | }
|
257 | 253 |
|
258 | 254 | fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
|
259 |
| - unsafe { |
260 |
| - if self.is_const_integral(v) { |
261 |
| - let (mut lo, mut hi) = (0u64, 0u64); |
262 |
| - let success = llvm::LLVMRustConstInt128Get(v, sign_ext, |
263 |
| - &mut hi, &mut lo); |
264 |
| - if success { |
265 |
| - Some(hi_lo_to_u128(lo, hi)) |
266 |
| - } else { |
267 |
| - None |
268 |
| - } |
| 255 | + try_as_const_integral(v).and_then(|v| unsafe { |
| 256 | + let (mut lo, mut hi) = (0u64, 0u64); |
| 257 | + let success = llvm::LLVMRustConstInt128Get(v, sign_ext, |
| 258 | + &mut hi, &mut lo); |
| 259 | + if success { |
| 260 | + Some(hi_lo_to_u128(lo, hi)) |
269 | 261 | } else {
|
270 | 262 | None
|
271 | 263 | }
|
272 |
| - } |
| 264 | + }) |
273 | 265 | }
|
274 | 266 |
|
275 | 267 | fn scalar_to_backend(
|
@@ -305,7 +297,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
305 | 297 | }
|
306 | 298 | }
|
307 | 299 | Some(GlobalAlloc::Function(fn_instance)) => {
|
308 |
| - self.get_fn(fn_instance) |
| 300 | + self.get_fn_addr(fn_instance) |
309 | 301 | }
|
310 | 302 | Some(GlobalAlloc::Static(def_id)) => {
|
311 | 303 | assert!(self.tcx.is_static(def_id));
|
@@ -386,3 +378,9 @@ pub fn struct_in_context(
|
386 | 378 | fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
|
387 | 379 | ((hi as u128) << 64) | (lo as u128)
|
388 | 380 | }
|
| 381 | + |
| 382 | +fn try_as_const_integral(v: &'ll Value) -> Option<&'ll ConstantInt> { |
| 383 | + unsafe { |
| 384 | + llvm::LLVMIsAConstantInt(v) |
| 385 | + } |
| 386 | +} |
0 commit comments