Skip to content

Commit bb764bd

Browse files
committed
Move is_mingw_gnu_toolchain and i686_decorated_name to cg_ssa
1 parent ee89db9 commit bb764bd

File tree

5 files changed

+73
-70
lines changed

5 files changed

+73
-70
lines changed

compiler/rustc_codegen_llvm/src/back/archive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use rustc_codegen_ssa::back::archive::{
88
try_extract_macho_fat_archive, ArArchiveBuilder, ArchiveBuildFailure, ArchiveBuilder,
99
ArchiveBuilderBuilder, ObjectReader, UnknownArchiveKind, DEFAULT_OBJECT_READER,
1010
};
11+
use rustc_codegen_ssa::common;
1112
use rustc_session::cstore::DllImport;
1213
use rustc_session::Session;
1314
use tracing::trace;
1415

15-
use crate::common;
1616
use crate::errors::{
1717
DlltoolFailImportLibrary, ErrorCallingDllTool, ErrorCreatingImportLibrary, ErrorWritingDEFFile,
1818
};

compiler/rustc_codegen_llvm/src/callee.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
//! and methods are represented as just a fn ptr and not a full
55
//! closure.
66
7+
use rustc_codegen_ssa::common;
78
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
89
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
910
use tracing::debug;
1011

1112
use crate::context::CodegenCx;
1213
use crate::value::Value;
13-
use crate::{attributes, common, llvm};
14+
use crate::{attributes, llvm};
1415

1516
/// Codegens a reference to a fn/method item, monomorphizing and
1617
/// inlining as it goes.
@@ -46,7 +47,7 @@ pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) ->
4647
} else {
4748
let instance_def_id = instance.def_id();
4849
let llfn = if tcx.sess.target.arch == "x86"
49-
&& let Some(dllimport) = common::get_dllimport(tcx, instance_def_id, sym)
50+
&& let Some(dllimport) = crate::common::get_dllimport(tcx, instance_def_id, sym)
5051
{
5152
// Fix for https://github.com/rust-lang/rust/issues/104453
5253
// On x86 Windows, LLVM uses 'L' as the prefix for any private

compiler/rustc_codegen_llvm/src/common.rs

+1-65
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Code that is useful in various codegen modules.
22
3-
use std::fmt::Write;
4-
53
use libc::{c_char, c_uint};
64
use rustc_ast::Mutability;
75
use rustc_codegen_ssa::traits::*;
@@ -10,9 +8,8 @@ use rustc_hir::def_id::DefId;
108
use rustc_middle::bug;
119
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
1210
use rustc_middle::ty::TyCtxt;
13-
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
11+
use rustc_session::cstore::DllImport;
1412
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer};
15-
use rustc_target::spec::Target;
1613
use tracing::debug;
1714

1815
use crate::consts::const_alloc_to_llvm;
@@ -379,64 +376,3 @@ pub(crate) fn get_dllimport<'tcx>(
379376
tcx.native_library(id)
380377
.and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name))
381378
}
382-
383-
pub(crate) fn is_mingw_gnu_toolchain(target: &Target) -> bool {
384-
target.vendor == "pc" && target.os == "windows" && target.env == "gnu" && target.abi.is_empty()
385-
}
386-
387-
pub(crate) fn i686_decorated_name(
388-
dll_import: &DllImport,
389-
mingw: bool,
390-
disable_name_mangling: bool,
391-
) -> String {
392-
let name = dll_import.name.as_str();
393-
394-
let (add_prefix, add_suffix) = match dll_import.import_name_type {
395-
Some(PeImportNameType::NoPrefix) => (false, true),
396-
Some(PeImportNameType::Undecorated) => (false, false),
397-
_ => (true, true),
398-
};
399-
400-
// Worst case: +1 for disable name mangling, +1 for prefix, +4 for suffix (@@__).
401-
let mut decorated_name = String::with_capacity(name.len() + 6);
402-
403-
if disable_name_mangling {
404-
// LLVM uses a binary 1 ('\x01') prefix to a name to indicate that mangling needs to be disabled.
405-
decorated_name.push('\x01');
406-
}
407-
408-
let prefix = if add_prefix && dll_import.is_fn {
409-
match dll_import.calling_convention {
410-
DllCallingConvention::C | DllCallingConvention::Vectorcall(_) => None,
411-
DllCallingConvention::Stdcall(_) => (!mingw
412-
|| dll_import.import_name_type == Some(PeImportNameType::Decorated))
413-
.then_some('_'),
414-
DllCallingConvention::Fastcall(_) => Some('@'),
415-
}
416-
} else if !dll_import.is_fn && !mingw {
417-
// For static variables, prefix with '_' on MSVC.
418-
Some('_')
419-
} else {
420-
None
421-
};
422-
if let Some(prefix) = prefix {
423-
decorated_name.push(prefix);
424-
}
425-
426-
decorated_name.push_str(name);
427-
428-
if add_suffix && dll_import.is_fn {
429-
match dll_import.calling_convention {
430-
DllCallingConvention::C => {}
431-
DllCallingConvention::Stdcall(arg_list_size)
432-
| DllCallingConvention::Fastcall(arg_list_size) => {
433-
write!(&mut decorated_name, "@{arg_list_size}").unwrap();
434-
}
435-
DllCallingConvention::Vectorcall(arg_list_size) => {
436-
write!(&mut decorated_name, "@@{arg_list_size}").unwrap();
437-
}
438-
}
439-
}
440-
441-
decorated_name
442-
}

compiler/rustc_codegen_llvm/src/consts.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::ops::Range;
22

3+
use rustc_codegen_ssa::common;
34
use rustc_codegen_ssa::traits::*;
45
use rustc_hir::def::DefKind;
56
use rustc_hir::def_id::DefId;
@@ -18,7 +19,7 @@ use rustc_target::abi::{
1819
};
1920
use tracing::{debug, instrument, trace};
2021

21-
use crate::common::{self, CodegenCx};
22+
use crate::common::CodegenCx;
2223
use crate::errors::{
2324
InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined,
2425
};
@@ -195,7 +196,7 @@ fn check_and_apply_linkage<'ll, 'tcx>(
195196
g2
196197
}
197198
} else if cx.tcx.sess.target.arch == "x86"
198-
&& let Some(dllimport) = common::get_dllimport(cx.tcx, def_id, sym)
199+
&& let Some(dllimport) = crate::common::get_dllimport(cx.tcx, def_id, sym)
199200
{
200201
cx.declare_global(
201202
&common::i686_decorated_name(

compiler/rustc_codegen_ssa/src/common.rs

+65
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use rustc_hir::LangItem;
44
use rustc_middle::ty::layout::TyAndLayout;
55
use rustc_middle::ty::{self, Instance, TyCtxt};
66
use rustc_middle::{bug, mir, span_bug};
7+
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
78
use rustc_span::Span;
9+
use rustc_target::spec::Target;
810

911
use crate::traits::*;
1012

@@ -176,3 +178,66 @@ pub fn asm_const_to_str<'tcx>(
176178
_ => span_bug!(sp, "asm const has bad type {}", ty_and_layout.ty),
177179
}
178180
}
181+
182+
pub fn is_mingw_gnu_toolchain(target: &Target) -> bool {
183+
target.vendor == "pc" && target.os == "windows" && target.env == "gnu" && target.abi.is_empty()
184+
}
185+
186+
pub fn i686_decorated_name(
187+
dll_import: &DllImport,
188+
mingw: bool,
189+
disable_name_mangling: bool,
190+
) -> String {
191+
let name = dll_import.name.as_str();
192+
193+
let (add_prefix, add_suffix) = match dll_import.import_name_type {
194+
Some(PeImportNameType::NoPrefix) => (false, true),
195+
Some(PeImportNameType::Undecorated) => (false, false),
196+
_ => (true, true),
197+
};
198+
199+
// Worst case: +1 for disable name mangling, +1 for prefix, +4 for suffix (@@__).
200+
let mut decorated_name = String::with_capacity(name.len() + 6);
201+
202+
if disable_name_mangling {
203+
// LLVM uses a binary 1 ('\x01') prefix to a name to indicate that mangling needs to be disabled.
204+
decorated_name.push('\x01');
205+
}
206+
207+
let prefix = if add_prefix && dll_import.is_fn {
208+
match dll_import.calling_convention {
209+
DllCallingConvention::C | DllCallingConvention::Vectorcall(_) => None,
210+
DllCallingConvention::Stdcall(_) => (!mingw
211+
|| dll_import.import_name_type == Some(PeImportNameType::Decorated))
212+
.then_some('_'),
213+
DllCallingConvention::Fastcall(_) => Some('@'),
214+
}
215+
} else if !dll_import.is_fn && !mingw {
216+
// For static variables, prefix with '_' on MSVC.
217+
Some('_')
218+
} else {
219+
None
220+
};
221+
if let Some(prefix) = prefix {
222+
decorated_name.push(prefix);
223+
}
224+
225+
decorated_name.push_str(name);
226+
227+
if add_suffix && dll_import.is_fn {
228+
use std::fmt::Write;
229+
230+
match dll_import.calling_convention {
231+
DllCallingConvention::C => {}
232+
DllCallingConvention::Stdcall(arg_list_size)
233+
| DllCallingConvention::Fastcall(arg_list_size) => {
234+
write!(&mut decorated_name, "@{arg_list_size}").unwrap();
235+
}
236+
DllCallingConvention::Vectorcall(arg_list_size) => {
237+
write!(&mut decorated_name, "@@{arg_list_size}").unwrap();
238+
}
239+
}
240+
}
241+
242+
decorated_name
243+
}

0 commit comments

Comments
 (0)