summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_codegen_llvm/src/common.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/common.rs')
-rw-r--r--compiler/rustc_codegen_llvm/src/common.rs87
1 files changed, 81 insertions, 6 deletions
diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs
index fb4da9a5f..acee9134f 100644
--- a/compiler/rustc_codegen_llvm/src/common.rs
+++ b/compiler/rustc_codegen_llvm/src/common.rs
@@ -10,13 +10,17 @@ use crate::value::Value;
use rustc_ast::Mutability;
use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::traits::*;
+use rustc_hir::def_id::DefId;
use rustc_middle::bug;
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
+use rustc_middle::ty::TyCtxt;
+use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer, Size};
+use rustc_target::spec::Target;
use libc::{c_char, c_uint};
-use tracing::debug;
+use std::fmt::Write;
/*
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
@@ -211,7 +215,11 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}
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).and_then(|v| unsafe {
+ let mut i = 0u64;
+ let success = llvm::LLVMRustConstIntGetZExtValue(v, &mut i);
+ success.then_some(i)
+ })
}
fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
@@ -222,10 +230,6 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
})
}
- fn zst_to_backend(&self, _llty: &'ll Type) -> &'ll Value {
- self.const_undef(self.type_ix(0))
- }
-
fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: &'ll Type) -> &'ll Value {
let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
match cv {
@@ -357,3 +361,74 @@ fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
fn try_as_const_integral(v: &Value) -> Option<&ConstantInt> {
unsafe { llvm::LLVMIsAConstantInt(v) }
}
+
+pub(crate) fn get_dllimport<'tcx>(
+ tcx: TyCtxt<'tcx>,
+ id: DefId,
+ name: &str,
+) -> Option<&'tcx DllImport> {
+ tcx.native_library(id)
+ .map(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name))
+ .flatten()
+}
+
+pub(crate) fn is_mingw_gnu_toolchain(target: &Target) -> bool {
+ target.vendor == "pc" && target.os == "windows" && target.env == "gnu" && target.abi.is_empty()
+}
+
+pub(crate) fn i686_decorated_name(
+ dll_import: &DllImport,
+ mingw: bool,
+ disable_name_mangling: bool,
+) -> String {
+ let name = dll_import.name.as_str();
+
+ let (add_prefix, add_suffix) = match dll_import.import_name_type {
+ Some(PeImportNameType::NoPrefix) => (false, true),
+ Some(PeImportNameType::Undecorated) => (false, false),
+ _ => (true, true),
+ };
+
+ // Worst case: +1 for disable name mangling, +1 for prefix, +4 for suffix (@@__).
+ let mut decorated_name = String::with_capacity(name.len() + 6);
+
+ if disable_name_mangling {
+ // LLVM uses a binary 1 ('\x01') prefix to a name to indicate that mangling needs to be disabled.
+ decorated_name.push('\x01');
+ }
+
+ let prefix = if add_prefix && dll_import.is_fn {
+ match dll_import.calling_convention {
+ DllCallingConvention::C | DllCallingConvention::Vectorcall(_) => None,
+ DllCallingConvention::Stdcall(_) => (!mingw
+ || dll_import.import_name_type == Some(PeImportNameType::Decorated))
+ .then_some('_'),
+ DllCallingConvention::Fastcall(_) => Some('@'),
+ }
+ } else if !dll_import.is_fn && !mingw {
+ // For static variables, prefix with '_' on MSVC.
+ Some('_')
+ } else {
+ None
+ };
+ if let Some(prefix) = prefix {
+ decorated_name.push(prefix);
+ }
+
+ decorated_name.push_str(name);
+
+ if add_suffix && dll_import.is_fn {
+ match dll_import.calling_convention {
+ DllCallingConvention::C => {}
+ DllCallingConvention::Stdcall(arg_list_size)
+ | DllCallingConvention::Fastcall(arg_list_size) => {
+ write!(&mut decorated_name, "@{}", arg_list_size).unwrap();
+ }
+ DllCallingConvention::Vectorcall(arg_list_size) => {
+ write!(&mut decorated_name, "@@{}", arg_list_size).unwrap();
+ }
+ }
+ }
+
+ decorated_name
+}