Move is_mingw_gnu_toolchain and i686_decorated_name to cg_ssa

This commit is contained in:
bjorn3 2024-07-25 20:02:56 +00:00
parent ee89db9b17
commit bb764bd406
5 changed files with 73 additions and 70 deletions

View File

@ -8,11 +8,11 @@ use rustc_codegen_ssa::back::archive::{
try_extract_macho_fat_archive, ArArchiveBuilder, ArchiveBuildFailure, ArchiveBuilder, try_extract_macho_fat_archive, ArArchiveBuilder, ArchiveBuildFailure, ArchiveBuilder,
ArchiveBuilderBuilder, ObjectReader, UnknownArchiveKind, DEFAULT_OBJECT_READER, ArchiveBuilderBuilder, ObjectReader, UnknownArchiveKind, DEFAULT_OBJECT_READER,
}; };
use rustc_codegen_ssa::common;
use rustc_session::cstore::DllImport; use rustc_session::cstore::DllImport;
use rustc_session::Session; use rustc_session::Session;
use tracing::trace; use tracing::trace;
use crate::common;
use crate::errors::{ use crate::errors::{
DlltoolFailImportLibrary, ErrorCallingDllTool, ErrorCreatingImportLibrary, ErrorWritingDEFFile, DlltoolFailImportLibrary, ErrorCallingDllTool, ErrorCreatingImportLibrary, ErrorWritingDEFFile,
}; };

View File

@ -4,13 +4,14 @@
//! and methods are represented as just a fn ptr and not a full //! and methods are represented as just a fn ptr and not a full
//! closure. //! closure.
use rustc_codegen_ssa::common;
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt}; use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
use rustc_middle::ty::{self, Instance, TypeVisitableExt}; use rustc_middle::ty::{self, Instance, TypeVisitableExt};
use tracing::debug; use tracing::debug;
use crate::context::CodegenCx; use crate::context::CodegenCx;
use crate::value::Value; use crate::value::Value;
use crate::{attributes, common, llvm}; use crate::{attributes, llvm};
/// Codegens a reference to a fn/method item, monomorphizing and /// Codegens a reference to a fn/method item, monomorphizing and
/// inlining as it goes. /// inlining as it goes.
@ -46,7 +47,7 @@ pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) ->
} else { } else {
let instance_def_id = instance.def_id(); let instance_def_id = instance.def_id();
let llfn = if tcx.sess.target.arch == "x86" let llfn = if tcx.sess.target.arch == "x86"
&& let Some(dllimport) = common::get_dllimport(tcx, instance_def_id, sym) && let Some(dllimport) = crate::common::get_dllimport(tcx, instance_def_id, sym)
{ {
// Fix for https://github.com/rust-lang/rust/issues/104453 // Fix for https://github.com/rust-lang/rust/issues/104453
// On x86 Windows, LLVM uses 'L' as the prefix for any private // On x86 Windows, LLVM uses 'L' as the prefix for any private

View File

@ -1,7 +1,5 @@
//! Code that is useful in various codegen modules. //! Code that is useful in various codegen modules.
use std::fmt::Write;
use libc::{c_char, c_uint}; use libc::{c_char, c_uint};
use rustc_ast::Mutability; use rustc_ast::Mutability;
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
@ -10,9 +8,8 @@ use rustc_hir::def_id::DefId;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar}; use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType}; use rustc_session::cstore::DllImport;
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer}; use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer};
use rustc_target::spec::Target;
use tracing::debug; use tracing::debug;
use crate::consts::const_alloc_to_llvm; use crate::consts::const_alloc_to_llvm;
@ -379,64 +376,3 @@ pub(crate) fn get_dllimport<'tcx>(
tcx.native_library(id) tcx.native_library(id)
.and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name)) .and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name))
} }
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
}

View File

@ -1,5 +1,6 @@
use std::ops::Range; use std::ops::Range;
use rustc_codegen_ssa::common;
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
@ -18,7 +19,7 @@ use rustc_target::abi::{
}; };
use tracing::{debug, instrument, trace}; use tracing::{debug, instrument, trace};
use crate::common::{self, CodegenCx}; use crate::common::CodegenCx;
use crate::errors::{ use crate::errors::{
InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined, InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined,
}; };
@ -195,7 +196,7 @@ fn check_and_apply_linkage<'ll, 'tcx>(
g2 g2
} }
} else if cx.tcx.sess.target.arch == "x86" } else if cx.tcx.sess.target.arch == "x86"
&& let Some(dllimport) = common::get_dllimport(cx.tcx, def_id, sym) && let Some(dllimport) = crate::common::get_dllimport(cx.tcx, def_id, sym)
{ {
cx.declare_global( cx.declare_global(
&common::i686_decorated_name( &common::i686_decorated_name(

View File

@ -4,7 +4,9 @@ use rustc_hir::LangItem;
use rustc_middle::ty::layout::TyAndLayout; use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{self, Instance, TyCtxt}; use rustc_middle::ty::{self, Instance, TyCtxt};
use rustc_middle::{bug, mir, span_bug}; use rustc_middle::{bug, mir, span_bug};
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
use rustc_span::Span; use rustc_span::Span;
use rustc_target::spec::Target;
use crate::traits::*; use crate::traits::*;
@ -176,3 +178,66 @@ pub fn asm_const_to_str<'tcx>(
_ => span_bug!(sp, "asm const has bad type {}", ty_and_layout.ty), _ => span_bug!(sp, "asm const has bad type {}", ty_and_layout.ty),
} }
} }
pub fn is_mingw_gnu_toolchain(target: &Target) -> bool {
target.vendor == "pc" && target.os == "windows" && target.env == "gnu" && target.abi.is_empty()
}
pub 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 {
use std::fmt::Write;
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
}