Skip to content

Commit 1e0b818

Browse files
committed
rustc_target: introduce Abi
Improve type safety by using an enum rather than strings.
1 parent 0c5bf3a commit 1e0b818

File tree

111 files changed

+310
-257
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+310
-257
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
4747
use rustc_session::Session;
4848
use rustc_session::config::OutputFilenames;
4949
use rustc_span::{Symbol, sym};
50-
use rustc_target::spec::Arch;
50+
use rustc_target::spec::{Abi, Arch};
5151

5252
pub use crate::config::*;
5353
use crate::prelude::*;
@@ -216,7 +216,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
216216
Arch::X86_64
217217
if sess.target.os == "windows"
218218
&& sess.target.env == "gnu"
219-
&& sess.target.abi != "llvm" =>
219+
&& sess.target.abi != Abi::Llvm =>
220220
{
221221
false
222222
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_span::source_map::Spanned;
2929
use rustc_span::{DUMMY_SP, Span, Symbol};
3030
use rustc_symbol_mangling::mangle_internal_symbol;
3131
use rustc_target::spec::{
32-
Arch, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
32+
Abi, Arch, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
3333
};
3434
use smallvec::SmallVec;
3535

@@ -337,7 +337,7 @@ pub(crate) unsafe fn create_module<'ll>(
337337
if sess.target.is_like_msvc
338338
|| (sess.target.options.os == "windows"
339339
&& sess.target.options.env == "gnu"
340-
&& sess.target.options.abi == "llvm")
340+
&& sess.target.options.abi == Abi::Llvm)
341341
{
342342
match sess.opts.cg.control_flow_guard {
343343
CFGuard::Disabled => {}

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_fs_util::path_to_c_string;
1515
use rustc_middle::bug;
1616
use rustc_session::Session;
1717
use rustc_session::config::{PrintKind, PrintRequest};
18-
use rustc_target::spec::{Arch, MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
18+
use rustc_target::spec::{Abi, Arch, MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
1919
use smallvec::{SmallVec, smallvec};
2020

2121
use crate::back::write::create_informational_target_machine;
@@ -353,7 +353,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
353353
let target_arch = &sess.target.arch;
354354
let target_os = sess.target.options.os.as_ref();
355355
let target_env = sess.target.options.env.as_ref();
356-
let target_abi = sess.target.options.abi.as_ref();
356+
let target_abi = &sess.target.options.abi;
357357
let target_pointer_width = sess.target.pointer_width;
358358
let version = get_version();
359359
let lt_20_1_1 = version < (20, 1, 1);
@@ -371,7 +371,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
371371
// Selection failure <https://github.com/llvm/llvm-project/issues/50374> (fixed in llvm21)
372372
(Arch::S390x, _) if lt_21_0_0 => false,
373373
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
374-
(Arch::X86_64, "windows") if target_env == "gnu" && target_abi != "llvm" => false,
374+
(Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false,
375375
// Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
376376
(Arch::CSky, _) => false,
377377
(Arch::Hexagon, _) if lt_21_0_0 => false, // (fixed in llvm21)
@@ -403,7 +403,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
403403
// not fail if our compiler-builtins is linked. (fixed in llvm21)
404404
(Arch::X86, _) if lt_21_0_0 => false,
405405
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
406-
(Arch::X86_64, "windows") if target_env == "gnu" && target_abi != "llvm" => false,
406+
(Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false,
407407
// There are no known problems on other platforms, so the only requirement is that symbols
408408
// are available. `compiler-builtins` provides all symbols required for core `f128`
409409
// support, so this should work for everything else.

compiler/rustc_codegen_llvm/src/va_arg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_codegen_ssa::traits::{
77
};
88
use rustc_middle::ty::Ty;
99
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
10-
use rustc_target::spec::Arch;
10+
use rustc_target::spec::{Abi, Arch};
1111

1212
use crate::builder::Builder;
1313
use crate::llvm::{Type, Value};
@@ -270,7 +270,7 @@ fn emit_powerpc_va_arg<'ll, 'tcx>(
270270

271271
// Rust does not currently support any powerpc softfloat targets.
272272
let target = &bx.cx.tcx.sess.target;
273-
let is_soft_float_abi = target.abi == "softfloat";
273+
let is_soft_float_abi = target.abi == Abi::SoftFloat;
274274
assert!(!is_soft_float_abi);
275275

276276
// All instances of VaArgSafe are passed directly.

compiler/rustc_codegen_ssa/src/back/metadata.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_metadata::fs::METADATA_FILENAME;
2020
use rustc_middle::bug;
2121
use rustc_session::Session;
2222
use rustc_span::sym;
23-
use rustc_target::spec::{RelocModel, Target, ef_avr_arch};
23+
use rustc_target::spec::{Abi, RelocModel, Target, ef_avr_arch};
2424
use tracing::debug;
2525

2626
use super::apple;
@@ -379,11 +379,11 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
379379
}
380380
}
381381
Architecture::Csky => {
382-
let e_flags = match sess.target.options.abi.as_ref() {
383-
"abiv2" => elf::EF_CSKY_ABIV2,
384-
_ => elf::EF_CSKY_ABIV1,
385-
};
386-
e_flags
382+
if matches!(sess.target.options.abi, Abi::AbiV2) {
383+
elf::EF_CSKY_ABIV2
384+
} else {
385+
elf::EF_CSKY_ABIV1
386+
}
387387
}
388388
Architecture::PowerPc64 => {
389389
const EF_PPC64_ABI_UNKNOWN: u32 = 0;

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::{self, Instance, TyCtxt};
77
use rustc_middle::{bug, mir, span_bug};
88
use rustc_session::cstore::{DllCallingConvention, DllImport};
99
use rustc_span::Span;
10-
use rustc_target::spec::{Target, Vendor};
10+
use rustc_target::spec::{Abi, Target, Vendor};
1111

1212
use crate::traits::*;
1313

@@ -174,7 +174,7 @@ pub fn is_mingw_gnu_toolchain(target: &Target) -> bool {
174174
target.vendor == Vendor::Pc
175175
&& target.os == "windows"
176176
&& target.env == "gnu"
177-
&& target.abi.is_empty()
177+
&& target.abi == Abi::Unspecified
178178
}
179179

180180
pub fn i686_decorated_name(

compiler/rustc_session/src/config/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
239239
ins_none!(sym::sanitizer_cfi_normalize_integers);
240240
}
241241

242-
ins_str!(sym::target_abi, &sess.target.abi);
242+
ins_sym!(sym::target_abi, sess.target.abi.desc_symbol());
243243
ins_sym!(sym::target_arch, sess.target.arch.desc_symbol());
244244
ins_str!(sym::target_endian, sess.target.endian.as_str());
245245
ins_str!(sym::target_env, &sess.target.env);
@@ -447,7 +447,7 @@ impl CheckCfg {
447447
};
448448

449449
for target in Target::builtins().chain(iter::once(current_target.clone())) {
450-
values_target_abi.insert(Symbol::intern(&target.options.abi));
450+
values_target_abi.insert(target.options.abi.desc_symbol());
451451
values_target_arch.insert(target.arch.desc_symbol());
452452
values_target_endian.insert(Symbol::intern(target.options.endian.as_str()));
453453
values_target_env.insert(Symbol::intern(&target.options.env));

compiler/rustc_target/src/asm/powerpc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxIndexSet;
44
use rustc_span::Symbol;
55

66
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
7-
use crate::spec::{RelocModel, Target};
7+
use crate::spec::{Abi, RelocModel, Target};
88

99
def_reg_class! {
1010
PowerPC PowerPCInlineAsmRegClass {
@@ -104,10 +104,10 @@ fn reserved_v20to31(
104104
_is_clobber: bool,
105105
) -> Result<(), &'static str> {
106106
if target.is_like_aix {
107-
match &*target.options.abi {
108-
"vec-default" => Err("v20-v31 (vs52-vs63) are reserved on vec-default ABI"),
109-
"vec-extabi" => Ok(()),
110-
_ => unreachable!("unrecognized AIX ABI"),
107+
match &target.options.abi {
108+
Abi::VecDefault => Err("v20-v31 (vs52-vs63) are reserved on vec-default ABI"),
109+
Abi::VecExtAbi => Ok(()),
110+
abi => unreachable!("unrecognized AIX ABI: {abi}"),
111111
}
112112
} else {
113113
Ok(())

compiler/rustc_target/src/callconv/aarch64.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::iter;
33
use rustc_abi::{BackendRepr, HasDataLayout, Primitive, TyAbiInterface};
44

55
use crate::callconv::{ArgAbi, FnAbi, Reg, RegKind, Uniform};
6-
use crate::spec::{HasTargetSpec, Target};
6+
use crate::spec::{Abi, HasTargetSpec, Target};
77

88
/// Indicates the variant of the AArch64 ABI we are compiling for.
99
/// Used to accommodate Apple and Microsoft's deviations from the usual AAPCS ABI.
@@ -33,7 +33,7 @@ where
3333
RegKind::Integer => false,
3434
// The softfloat ABI treats floats like integers, so they
3535
// do not get homogeneous aggregate treatment.
36-
RegKind::Float => cx.target_spec().abi != "softfloat",
36+
RegKind::Float => cx.target_spec().abi != Abi::SoftFloat,
3737
RegKind::Vector => size.bits() == 64 || size.bits() == 128,
3838
};
3939

@@ -42,7 +42,7 @@ where
4242
}
4343

4444
fn softfloat_float_abi<Ty>(target: &Target, arg: &mut ArgAbi<'_, Ty>) {
45-
if target.abi != "softfloat" {
45+
if target.abi != Abi::SoftFloat {
4646
return;
4747
}
4848
// Do *not* use the float registers for passing arguments, as that would make LLVM pick the ABI

compiler/rustc_target/src/spec/base/aix.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use rustc_abi::Endian;
22

33
use crate::spec::{
4-
BinaryFormat, Cc, CodeModel, LinkOutputKind, LinkerFlavor, TargetOptions, Vendor, crt_objects,
5-
cvs,
4+
Abi, BinaryFormat, Cc, CodeModel, LinkOutputKind, LinkerFlavor, TargetOptions, Vendor,
5+
crt_objects, cvs,
66
};
77

88
pub(crate) fn opts() -> TargetOptions {
99
TargetOptions {
10-
abi: "vec-extabi".into(),
10+
abi: Abi::VecExtAbi,
1111
code_model: Some(CodeModel::Large),
1212
cpu: "pwr7".into(),
1313
os: "aix".into(),

0 commit comments

Comments
 (0)