]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_codegen_llvm/common.rs
Rollup merge of #68156 - JohnTitor:fix-path-in-doc, r=Dylan-DPC
[rust.git] / src / librustc_codegen_llvm / common.rs
index cda8fbc3517a9cb31c41219f330e4acf099cd73d..76f6eeb3f79070c8bf5d386a4280670d5b5dd598 100644 (file)
@@ -2,23 +2,23 @@
 
 //! Code that is useful in various codegen modules.
 
-use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef, ConstantInt};
 use crate::consts;
+use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, OperandBundleDef, True};
 use crate::type_::Type;
 use crate::type_of::LayoutLlvmExt;
 use crate::value::Value;
-use rustc_codegen_ssa::traits::*;
-use rustc::bug;
 use log::debug;
+use rustc::bug;
+use rustc_codegen_ssa::traits::*;
 
 use crate::consts::const_alloc_to_llvm;
-use rustc::ty::layout::{HasDataLayout, LayoutOf, self, TyLayout, Size};
-use rustc::mir::interpret::{Scalar, GlobalAlloc, Allocation};
+use rustc::mir::interpret::{Allocation, GlobalAlloc, Scalar};
+use rustc::ty::layout::{self, HasDataLayout, LayoutOf, Size, TyLayout};
 use rustc_codegen_ssa::mir::place::PlaceRef;
 
-use libc::{c_uint, c_char};
+use libc::{c_char, c_uint};
 
-use syntax::symbol::Symbol;
+use rustc_span::symbol::Symbol;
 use syntax::ast::Mutability;
 
 pub use crate::context::CodegenCx;
@@ -70,10 +70,7 @@ pub struct Funclet<'ll> {
 
 impl Funclet<'ll> {
     pub fn new(cleanuppad: &'ll Value) -> Self {
-        Funclet {
-            cleanuppad,
-            operand: OperandBundleDef::new("funclet", &[cleanuppad]),
-        }
+        Funclet { cleanuppad, operand: OperandBundleDef::new("funclet", &[cleanuppad]) }
     }
 
     pub fn cleanuppad(&self) -> &'ll Value {
@@ -113,23 +110,21 @@ pub fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
         bytes_in_context(self.llcx, bytes)
     }
 
-    fn const_cstr(
-        &self,
-        s: Symbol,
-        null_terminated: bool,
-    ) -> &'ll Value {
+    fn const_cstr(&self, s: Symbol, null_terminated: bool) -> &'ll Value {
         unsafe {
             if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
                 return llval;
             }
 
             let s_str = s.as_str();
-            let sc = llvm::LLVMConstStringInContext(self.llcx,
-                                                    s_str.as_ptr() as *const c_char,
-                                                    s_str.len() as c_uint,
-                                                    !null_terminated as Bool);
+            let sc = llvm::LLVMConstStringInContext(
+                self.llcx,
+                s_str.as_ptr() as *const c_char,
+                s_str.len() as c_uint,
+                !null_terminated as Bool,
+            );
             let sym = self.generate_local_symbol_name("str");
-            let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(||{
+            let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(|| {
                 bug!("symbol `{}` is already defined", sym);
             });
             llvm::LLVMSetInitializer(g, sc);
@@ -147,8 +142,7 @@ pub fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
             let us = &[idx as c_uint];
             let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
 
-            debug!("const_get_elt(v={:?}, idx={}, r={:?})",
-                   v, idx, r);
+            debug!("const_get_elt(v={:?}, idx={}, r={:?})", v, idx, r);
 
             r
         }
@@ -157,27 +151,19 @@ pub fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
 
 impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
     fn const_null(&self, t: &'ll Type) -> &'ll Value {
-        unsafe {
-            llvm::LLVMConstNull(t)
-        }
+        unsafe { llvm::LLVMConstNull(t) }
     }
 
     fn const_undef(&self, t: &'ll Type) -> &'ll Value {
-        unsafe {
-            llvm::LLVMGetUndef(t)
-        }
+        unsafe { llvm::LLVMGetUndef(t) }
     }
 
     fn const_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
-        unsafe {
-            llvm::LLVMConstInt(t, i as u64, True)
-        }
+        unsafe { llvm::LLVMConstInt(t, i as u64, True) }
     }
 
     fn const_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
-        unsafe {
-            llvm::LLVMConstInt(t, i, False)
-        }
+        unsafe { llvm::LLVMConstInt(t, i, False) }
     }
 
     fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
@@ -207,7 +193,7 @@ fn const_usize(&self, i: u64) -> &'ll Value {
         let bit_size = self.data_layout().pointer_size.bits();
         if bit_size < 64 {
             // make sure it doesn't overflow
-            assert!(i < (1<<bit_size));
+            assert!(i < (1 << bit_size));
         }
 
         self.const_uint(self.isize_ty, i)
@@ -223,30 +209,25 @@ fn const_real(&self, t: &'ll Type, val: f64) -> &'ll Value {
 
     fn const_str(&self, s: Symbol) -> (&'ll Value, &'ll Value) {
         let len = s.as_str().len();
-        let cs = consts::ptrcast(self.const_cstr(s, false),
-            self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
+        let cs = consts::ptrcast(
+            self.const_cstr(s, false),
+            self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)),
+        );
         (cs, self.const_usize(len as u64))
     }
 
-    fn const_struct(
-        &self,
-        elts: &[&'ll Value],
-        packed: bool
-    ) -> &'ll Value {
+    fn const_struct(&self, elts: &[&'ll Value], packed: bool) -> &'ll Value {
         struct_in_context(self.llcx, elts, packed)
     }
 
     fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
-        try_as_const_integral(v).map(|v| unsafe {
-            llvm::LLVMConstIntGetZExtValue(v)
-        })
+        try_as_const_integral(v).map(|v| unsafe { llvm::LLVMConstIntGetZExtValue(v) })
     }
 
     fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
         try_as_const_integral(v).and_then(|v| unsafe {
             let (mut lo, mut hi) = (0u64, 0u64);
-            let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
-                                                        &mut hi, &mut lo);
+            let success = llvm::LLVMRustConstInt128Get(v, sign_ext, &mut hi, &mut lo);
             success.then_some(hi_lo_to_u128(lo, hi))
         })
     }
@@ -262,7 +243,7 @@ fn scalar_to_backend(
             Scalar::Raw { size: 0, .. } => {
                 assert_eq!(0, layout.value.size(self).bytes());
                 self.const_undef(self.type_ix(0))
-            },
+            }
             Scalar::Raw { data, size } => {
                 assert_eq!(size as u64, layout.value.size(self).bytes());
                 let llval = self.const_uint_big(self.type_ix(bitsize), data);
@@ -271,7 +252,7 @@ fn scalar_to_backend(
                 } else {
                     self.const_bitcast(llval, llty)
                 }
-            },
+            }
             Scalar::Ptr(ptr) => {
                 let alloc_kind = self.tcx.alloc_map.lock().get(ptr.alloc_id);
                 let base_addr = match alloc_kind {
@@ -283,20 +264,20 @@ fn scalar_to_backend(
                             self.static_addr_of(init, alloc.align, None)
                         }
                     }
-                    Some(GlobalAlloc::Function(fn_instance)) => {
-                        self.get_fn_addr(fn_instance)
-                    }
+                    Some(GlobalAlloc::Function(fn_instance)) => self.get_fn_addr(fn_instance),
                     Some(GlobalAlloc::Static(def_id)) => {
                         assert!(self.tcx.is_static(def_id));
                         self.get_static(def_id)
                     }
                     None => bug!("missing allocation {:?}", ptr.alloc_id),
                 };
-                let llval = unsafe { llvm::LLVMConstInBoundsGEP(
-                    self.const_bitcast(base_addr, self.type_i8p()),
-                    &self.const_usize(ptr.offset.bytes()),
-                    1,
-                ) };
+                let llval = unsafe {
+                    llvm::LLVMConstInBoundsGEP(
+                        self.const_bitcast(base_addr, self.type_i8p()),
+                        &self.const_usize(ptr.offset.bytes()),
+                        1,
+                    )
+                };
                 if layout.value != layout::Pointer {
                     unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
                 } else {
@@ -321,11 +302,13 @@ fn from_const_alloc(
             let init = const_alloc_to_llvm(self, alloc);
             let base_addr = self.static_addr_of(init, alloc.align, None);
 
-            let llval = unsafe { llvm::LLVMConstInBoundsGEP(
-                self.const_bitcast(base_addr, self.type_i8p()),
-                &self.const_usize(offset.bytes()),
-                1,
-            )};
+            let llval = unsafe {
+                llvm::LLVMConstInBoundsGEP(
+                    self.const_bitcast(base_addr, self.type_i8p()),
+                    &self.const_usize(offset.bytes()),
+                    1,
+                )
+            };
             self.const_bitcast(llval, llty)
         };
         PlaceRef::new_sized(llval, layout)
@@ -337,9 +320,7 @@ fn const_ptrcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
 }
 
 pub fn val_ty(v: &'ll Value) -> &'ll Type {
-    unsafe {
-        llvm::LLVMTypeOf(v)
-    }
+    unsafe { llvm::LLVMTypeOf(v) }
 }
 
 pub fn bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
@@ -349,15 +330,9 @@ pub fn bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
     }
 }
 
-pub fn struct_in_context(
-    llcx: &'a llvm::Context,
-    elts: &[&'a Value],
-    packed: bool,
-) -> &'a Value {
+pub fn struct_in_context(llcx: &'a llvm::Context, elts: &[&'a Value], packed: bool) -> &'a Value {
     unsafe {
-        llvm::LLVMConstStructInContext(llcx,
-                                       elts.as_ptr(), elts.len() as c_uint,
-                                       packed as Bool)
+        llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), elts.len() as c_uint, packed as Bool)
     }
 }
 
@@ -367,7 +342,5 @@ fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
 }
 
 fn try_as_const_integral(v: &'ll Value) -> Option<&'ll ConstantInt> {
-    unsafe {
-        llvm::LLVMIsAConstantInt(v)
-    }
+    unsafe { llvm::LLVMIsAConstantInt(v) }
 }