Skip to content

Commit 16b1b60

Browse files
committed
Move is_mingw_gnu_toolchain and i686_decorated_name to cg_ssa
1 parent 335bbc6 commit 16b1b60

File tree

5 files changed

+72
-69
lines changed

5 files changed

+72
-69
lines changed

compiler/rustc_codegen_llvm/src/back/archive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::path::{Path, PathBuf};
88
use std::ptr;
99
use std::str;
1010

11-
use crate::common;
1211
use crate::errors::{
1312
DlltoolFailImportLibrary, ErrorCallingDllTool, ErrorCreatingImportLibrary, ErrorWritingDEFFile,
1413
};
@@ -18,6 +17,7 @@ use rustc_codegen_ssa::back::archive::{
1817
try_extract_macho_fat_archive, ArArchiveBuilder, ArchiveBuildFailure, ArchiveBuilder,
1918
ArchiveBuilderBuilder, ObjectReader, UnknownArchiveKind, DEFAULT_OBJECT_READER,
2019
};
20+
use rustc_codegen_ssa::common;
2121
use tracing::trace;
2222

2323
use rustc_session::cstore::DllImport;

compiler/rustc_codegen_llvm/src/callee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
//! closure.
66
77
use crate::attributes;
8-
use crate::common;
98
use crate::context::CodegenCx;
109
use crate::llvm;
1110
use crate::value::Value;
1211

12+
use rustc_codegen_ssa::common;
1313
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
1414
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
1515
use tracing::debug;
@@ -48,7 +48,7 @@ pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) ->
4848
} else {
4949
let instance_def_id = instance.def_id();
5050
let llfn = if tcx.sess.target.arch == "x86"
51-
&& let Some(dllimport) = common::get_dllimport(tcx, instance_def_id, sym)
51+
&& let Some(dllimport) = crate::common::get_dllimport(tcx, instance_def_id, sym)
5252
{
5353
// Fix for https://github.com/rust-lang/rust/issues/104453
5454
// On x86 Windows, LLVM uses 'L' as the prefix for any private

compiler/rustc_codegen_llvm/src/common.rs

+1-64
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ use rustc_hir::def_id::DefId;
1313
use rustc_middle::bug;
1414
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
1515
use rustc_middle::ty::TyCtxt;
16-
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
16+
use rustc_session::cstore::DllImport;
1717
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer};
18-
use rustc_target::spec::Target;
1918

2019
use libc::{c_char, c_uint};
21-
use std::fmt::Write;
2220
use tracing::debug;
2321

2422
/*
@@ -379,64 +377,3 @@ pub(crate) fn get_dllimport<'tcx>(
379377
tcx.native_library(id)
380378
.and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name))
381379
}
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,5 @@
11
use crate::base;
2-
use crate::common::{self, CodegenCx};
2+
use crate::common::CodegenCx;
33
use crate::debuginfo;
44
use crate::errors::{
55
InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined,
@@ -8,6 +8,7 @@ use crate::llvm::{self, True};
88
use crate::type_::Type;
99
use crate::type_of::LayoutLlvmExt;
1010
use crate::value::Value;
11+
use rustc_codegen_ssa::common;
1112
use rustc_codegen_ssa::traits::*;
1213
use rustc_hir::def::DefKind;
1314
use rustc_hir::def_id::DefId;
@@ -194,7 +195,7 @@ fn check_and_apply_linkage<'ll, 'tcx>(
194195
g2
195196
}
196197
} else if cx.tcx.sess.target.arch == "x86"
197-
&& let Some(dllimport) = common::get_dllimport(cx.tcx, def_id, sym)
198+
&& let Some(dllimport) = crate::common::get_dllimport(cx.tcx, def_id, sym)
198199
{
199200
cx.declare_global(
200201
&common::i686_decorated_name(

compiler/rustc_codegen_ssa/src/common.rs

+65
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use rustc_middle::mir;
55
use rustc_middle::ty::Instance;
66
use rustc_middle::ty::{self, layout::TyAndLayout, TyCtxt};
77
use rustc_middle::{bug, span_bug};
8+
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
89
use rustc_span::Span;
10+
use rustc_target::spec::Target;
911

1012
use crate::traits::*;
1113

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

0 commit comments

Comments
 (0)