]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_codegen_llvm/common.rs
Use the method form for CodegenCx everywhere
[rust.git] / src / librustc_codegen_llvm / common.rs
index 00bc9adf5c39973e17136e339e3ae5ef09a57a30..569fbc9f0813efb8cf6a0765419bbb8c15cdd028 100644 (file)
@@ -24,7 +24,7 @@
 use type_::Type;
 use type_of::LayoutLlvmExt;
 use value::Value;
-use interfaces::{Backend, CommonMethods};
+use interfaces::{Backend, CommonMethods, CommonWriteMethods};
 
 use rustc::ty::{self, Ty, TyCtxt};
 use rustc::ty::layout::{HasDataLayout, LayoutOf};
@@ -50,7 +50,7 @@ pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bo
     ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP)
 }
 
-pub struct OperandBundleDef<'a, Value : 'a> {
+pub struct OperandBundleDef<'a, Value: 'a> {
     pub name: &'a str,
     pub val: Value
 }
@@ -200,39 +200,34 @@ impl Backend for CodegenCx<'ll, 'tcx> {
     type Context = &'ll llvm::Context;
 }
 
-impl<'ll, 'tcx : 'll> CommonMethods for CodegenCx<'ll, 'tcx> {
-    fn val_ty(v: &'ll Value) -> &'ll Type {
-        unsafe {
-            llvm::LLVMTypeOf(v)
-        }
-    }
+impl<'ll, 'tcx: 'll> CommonMethods for CodegenCx<'ll, 'tcx> {
 
     // LLVM constant constructors.
-    fn c_null(t: &'ll Type) -> &'ll Value {
+    fn c_null(&self, t: &'ll Type) -> &'ll Value {
         unsafe {
             llvm::LLVMConstNull(t)
         }
     }
 
-    fn c_undef(t: &'ll Type) -> &'ll Value {
+    fn c_undef(&self, t: &'ll Type) -> &'ll Value {
         unsafe {
             llvm::LLVMGetUndef(t)
         }
     }
 
-    fn c_int(t: &'ll Type, i: i64) -> &'ll Value {
+    fn c_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
         unsafe {
             llvm::LLVMConstInt(t, i as u64, True)
         }
     }
 
-    fn c_uint(t: &'ll Type, i: u64) -> &'ll Value {
+    fn c_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
         unsafe {
             llvm::LLVMConstInt(t, i, False)
         }
     }
 
-    fn c_uint_big(t: &'ll Type, u: u128) -> &'ll Value {
+    fn c_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
         unsafe {
             let words = [u as u64, (u >> 64) as u64];
             llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
@@ -240,19 +235,19 @@ fn c_uint_big(t: &'ll Type, u: u128) -> &'ll Value {
     }
 
     fn c_bool(&self, val: bool) -> &'ll Value {
-        Self::c_uint(Type::i1(&self), val as u64)
+        &self.c_uint(Type::i1(&self), val as u64)
     }
 
     fn c_i32(&self, i: i32) -> &'ll Value {
-        Self::c_int(Type::i32(&self), i as i64)
+        &self.c_int(Type::i32(&self), i as i64)
     }
 
     fn c_u32(&self, i: u32) -> &'ll Value {
-        Self::c_uint(Type::i32(&self), i as u64)
+        &self.c_uint(Type::i32(&self), i as u64)
     }
 
     fn c_u64(&self, i: u64) -> &'ll Value {
-        Self::c_uint(Type::i64(&self), i)
+        &self.c_uint(Type::i64(&self), i)
     }
 
     fn c_usize(&self, i: u64) -> &'ll Value {
@@ -262,11 +257,11 @@ fn c_usize(&self, i: u64) -> &'ll Value {
             assert!(i < (1<<bit_size));
         }
 
-        Self::c_uint(&self.isize_ty, i)
+        &self.c_uint(&self.isize_ty, i)
     }
 
     fn c_u8(&self, i: u8) -> &'ll Value {
-        Self::c_uint(Type::i8(&self), i as u64)
+        &self.c_uint(Type::i8(&self), i as u64)
     }
 
 
@@ -287,7 +282,7 @@ fn c_cstr(
                                                     s.len() as c_uint,
                                                     !null_terminated as Bool);
             let sym = &self.generate_local_symbol_name("str");
-            let g = declare::define_global(&self, &sym[..], Self::val_ty(sc)).unwrap_or_else(||{
+            let g = declare::define_global(&self, &sym[..], &self.val_ty(sc)).unwrap_or_else(||{
                 bug!("symbol `{}` is already defined", sym);
             });
             llvm::LLVMSetInitializer(g, sc);
@@ -323,45 +318,26 @@ fn c_struct(
         elts: &[&'ll Value],
         packed: bool
     ) -> &'ll Value {
-        Self::c_struct_in_context(&self.llcx, elts, packed)
-    }
-
-    fn c_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)
-        }
+        &self.c_struct_in_context(&self.llcx, elts, packed)
     }
 
-    fn c_array(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
+    fn c_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
         unsafe {
             return llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint);
         }
     }
 
-    fn c_vector(elts: &[&'ll Value]) -> &'ll Value {
+    fn c_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
         unsafe {
             return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
         }
     }
 
     fn c_bytes(&self, bytes: &[u8]) -> &'ll Value {
-        Self::c_bytes_in_context(&self.llcx, bytes)
-    }
-
-    fn c_bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
-        unsafe {
-            let ptr = bytes.as_ptr() as *const c_char;
-            return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
-        }
+        &self.c_bytes_in_context(&self.llcx, bytes)
     }
 
-    fn const_get_elt(v: &'ll Value, idx: u64) -> &'ll Value {
+    fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
         unsafe {
             assert_eq!(idx as c_uint as u64, idx);
             let us = &[idx as c_uint];
@@ -374,9 +350,9 @@ fn const_get_elt(v: &'ll Value, idx: u64) -> &'ll Value {
         }
     }
 
-    fn const_get_real(v: &'ll Value) -> Option<(f64, bool)> {
+    fn const_get_real(&self, v: &'ll Value) -> Option<(f64, bool)> {
         unsafe {
-            if Self::is_const_real(v) {
+            if self.is_const_real(v) {
                 let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
                 let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
                 let loses_info = if loses_info == 1 { true } else { false };
@@ -387,27 +363,27 @@ fn const_get_real(v: &'ll Value) -> Option<(f64, bool)> {
         }
     }
 
-    fn const_to_uint(v: &'ll Value) -> u64 {
+    fn const_to_uint(&self, v: &'ll Value) -> u64 {
         unsafe {
             llvm::LLVMConstIntGetZExtValue(v)
         }
     }
 
-    fn is_const_integral(v: &'ll Value) -> bool {
+    fn is_const_integral(&self, v: &'ll Value) -> bool {
         unsafe {
             llvm::LLVMIsAConstantInt(v).is_some()
         }
     }
 
-    fn is_const_real(v: &'ll Value) -> bool {
+    fn is_const_real(&self, v: &'ll Value) -> bool {
         unsafe {
             llvm::LLVMIsAConstantFP(v).is_some()
         }
     }
 
-    fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
+    fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
         unsafe {
-            if Self::is_const_integral(v) {
+            if self.is_const_integral(v) {
                 let (mut lo, mut hi) = (0u64, 0u64);
                 let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
                                                            &mut hi, &mut lo);
@@ -423,6 +399,51 @@ fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
     }
 }
 
+pub fn val_ty(v: &'ll Value) -> &'ll Type {
+    unsafe {
+        llvm::LLVMTypeOf(v)
+    }
+}
+
+pub fn c_bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
+    unsafe {
+        let ptr = bytes.as_ptr() as *const c_char;
+        return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
+    }
+}
+
+pub fn c_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)
+    }
+}
+
+impl<'ll, 'tcx: 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx> {
+    fn val_ty(&self, v: &'ll Value) -> &'ll Type {
+        val_ty(v)
+    }
+
+    fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
+        c_bytes_in_context(llcx, bytes)
+    }
+
+    fn c_struct_in_context(
+        &self,
+        llcx: &'a llvm::Context,
+        elts: &[&'a Value],
+        packed: bool,
+    ) -> &'a Value {
+        c_struct_in_context(llcx, elts, packed)
+    }
+}
+
+
 #[inline]
 fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
     ((hi as u128) << 64) | (lo as u128)
@@ -473,7 +494,7 @@ pub fn build_unchecked_rshift(
 }
 
 fn shift_mask_rhs(bx: &Builder<'a, 'll, 'tcx>, rhs: &'ll Value) -> &'ll Value {
-    let rhs_llty = CodegenCx::val_ty(rhs);
+    let rhs_llty = bx.cx().val_ty(rhs);
     bx.and(rhs, shift_mask_val(bx, rhs_llty, rhs_llty, false))
 }
 
@@ -489,9 +510,9 @@ pub fn shift_mask_val(
             // i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
             let val = llty.int_width() - 1;
             if invert {
-                CodegenCx::c_int(mask_llty, !val as i64)
+                bx.cx.c_int(mask_llty, !val as i64)
             } else {
-                CodegenCx::c_uint(mask_llty, val)
+                bx.cx.c_uint(mask_llty, val)
             }
         },
         TypeKind::Vector => {