about summary refs log tree commit diff
path: root/src/librustc_codegen_llvm
diff options
context:
space:
mode:
authorDenis Merigoux <denis.merigoux@gmail.com>2018-08-30 14:24:41 +0200
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2018-11-16 14:11:59 +0200
commit33eee83737b399ccea5b916fc6249259ca618208 (patch)
tree36bc8f81b34af106a96612b26913bb7e63609190 /src/librustc_codegen_llvm
parent4cc18d3de50b6540e53ddcfefb88dee9ed991264 (diff)
downloadrust-33eee83737b399ccea5b916fc6249259ca618208.tar.gz
rust-33eee83737b399ccea5b916fc6249259ca618208.zip
Removed code duplication for CommonWriteMethods
Diffstat (limited to 'src/librustc_codegen_llvm')
-rw-r--r--src/librustc_codegen_llvm/back/write.rs18
-rw-r--r--src/librustc_codegen_llvm/common.rs40
-rw-r--r--src/librustc_codegen_llvm/lib.rs20
3 files changed, 36 insertions, 42 deletions
diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs
index 3c32da43031..24f58ac59c1 100644
--- a/src/librustc_codegen_llvm/back/write.rs
+++ b/src/librustc_codegen_llvm/back/write.rs
@@ -24,7 +24,7 @@ use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Sanitize
 use rustc::session::Session;
 use rustc::util::nodemap::FxHashMap;
 use time_graph::{self, TimeGraph, Timeline};
-use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock, True};
+use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock};
 use llvm_util;
 use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
      CachedModuleCodegen};
@@ -47,6 +47,7 @@ use syntax_pos::symbol::Symbol;
 use type_::Type;
 use context::{is_pie_binary, get_reloc_model};
 use interfaces::{Backend, CommonWriteMethods};
+use common;
 use jobserver::{Client, Acquired};
 use rustc_demangle;
 use value::Value;
@@ -437,16 +438,11 @@ impl<'ll> Backend for CodegenContext<'ll> {
 
 impl CommonWriteMethods for CodegenContext<'ll> {
     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
-        unsafe {
-            llvm::LLVMTypeOf(v)
-        }
+        common::val_ty(v)
     }
 
     fn c_bytes_in_context(&self, 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);
-        }
+        common::c_bytes_in_context(llcx, bytes)
     }
 
     fn c_struct_in_context(
@@ -455,11 +451,7 @@ impl CommonWriteMethods for CodegenContext<'ll> {
         elts: &[&'a Value],
         packed: bool,
     ) -> &'a Value {
-        unsafe {
-            llvm::LLVMConstStructInContext(llcx,
-                                           elts.as_ptr(), elts.len() as c_uint,
-                                           packed as llvm::Bool)
-        }
+        common::c_struct_in_context(llcx, elts, packed)
     }
 }
 
diff --git a/src/librustc_codegen_llvm/common.rs b/src/librustc_codegen_llvm/common.rs
index ba8d92d37ac..80f8bbeabda 100644
--- a/src/librustc_codegen_llvm/common.rs
+++ b/src/librustc_codegen_llvm/common.rs
@@ -399,18 +399,38 @@ impl<'ll, 'tcx : 'll> CommonMethods for CodegenCx<'ll, 'tcx> {
     }
 }
 
+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 {
-        unsafe {
-            llvm::LLVMTypeOf(v)
-        }
+        val_ty(v)
     }
 
     fn c_bytes_in_context(&self, 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);
-        }
+        c_bytes_in_context(llcx, bytes)
     }
 
     fn c_struct_in_context(
@@ -419,11 +439,7 @@ impl<'ll, 'tcx : 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx> {
         elts: &[&'a Value],
         packed: bool,
     ) -> &'a Value {
-        unsafe {
-            llvm::LLVMConstStructInContext(llcx,
-                                           elts.as_ptr(), elts.len() as c_uint,
-                                           packed as Bool)
-        }
+        c_struct_in_context(llcx, elts, packed)
     }
 }
 
diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs
index 2ef90c03c67..e228dc044f1 100644
--- a/src/librustc_codegen_llvm/lib.rs
+++ b/src/librustc_codegen_llvm/lib.rs
@@ -76,7 +76,6 @@ pub use llvm_util::target_features;
 use std::any::Any;
 use std::sync::mpsc;
 use std::marker::PhantomData;
-use libc::{c_uint, c_char};
 use rustc_data_structures::sync::Lrc;
 
 use rustc::dep_graph::DepGraph;
@@ -361,20 +360,11 @@ impl ModuleLlvm<'ll> {
 
 impl CommonWriteMethods for ModuleLlvm<'ll> {
     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
-        unsafe {
-            llvm::LLVMTypeOf(v)
-        }
+        common::val_ty(v)
     }
 
     fn c_bytes_in_context(&self, 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,
-                llvm::True);
-        }
+        common::c_bytes_in_context(llcx, bytes)
     }
 
     fn c_struct_in_context(
@@ -383,11 +373,7 @@ impl CommonWriteMethods for ModuleLlvm<'ll> {
         elts: &[&'a Value],
         packed: bool,
     ) -> &'a Value {
-        unsafe {
-            llvm::LLVMConstStructInContext(llcx,
-                                           elts.as_ptr(), elts.len() as c_uint,
-                                           packed as llvm::Bool)
-        }
+        common::c_struct_in_context(llcx, elts, packed)
     }
 }