Skip to main content

rucc_target/
lib.rs

1//! Target descriptions: triples, and the facts about a target that the rest of the
2//! compiler reads rather than hard-codes.
3//!
4//! Design: `spec/12-abi-and-runtime.md`. Layer rank 1, see `spec/18-package-layout.md`.
5//!
6//! The rule from `spec/18-package-layout.md` section 18.2 is that there is no
7//! target-specific code outside this crate and the per-target rule sets. Everything a pass
8//! needs to know about a target is a field it can read here. That rule is what makes the
9//! claim in `spec/10-backend.md` testable, namely that a new target is a rule set and a few
10//! data files, and `M10` brings up a fourth target specifically to put a number on it.
11//!
12//! [`TargetInfo::call`] is the other half of that rule and the one with teeth. How a structure
13//! travels between a caller and a callee is the target's answer rather than C's, so the walk to
14//! the IR flattens a C type into a [`Shape`] and asks here what form it takes. Every psABI rule
15//! is behind [`Call`] and nothing outside this crate matches on an architecture to find one.
16//!
17//! # Status
18//!
19//! Triple parsing and the basic data model are real, which is what `rucc --print-config`
20//! reports, and so is the argument classification of every psABI in
21//! `spec/12-abi-and-runtime.md` sections 12.2 to 12.5. x86-64's register file is written down,
22//! in [`x86_64`], along with what each of the two conventions over it does with each register,
23//! what each of its machine instructions does with its operands, and which instructions a frame
24//! is made of, which is [`FrameInsts`]. AArch64's and RISC-V's arrive with their backends.
25//! Machine models land in `M6`.
26//!
27//! This crate is tier 3 in `spec/18-package-layout.md` section 18.5: its Rust API is
28//! explicitly unstable and will change without a major version bump.
29
30#![doc(html_root_url = "https://docs.rs/rucc-target/0.3.9")]
31
32use std::fmt;
33use std::str::FromStr;
34
35use rucc_base::float::Format;
36
37mod abi;
38mod branch;
39mod frame;
40mod operand;
41mod regs;
42pub mod x86_64;
43
44pub use crate::abi::{Arg, Call, Kind, Pass, Piece, Scalar, Shape, Slot};
45pub use crate::branch::BranchInsts;
46pub use crate::frame::{ClassMoves, FrameInsts};
47pub use crate::operand::{Constraint, OperandDesc, Role};
48pub use crate::regs::{CallRegs, ClassInfo, PhysReg, Places, RegClass, RegFile, Where};
49
50/// A target architecture.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
52// Deliberately not `#[non_exhaustive]`. Adding a variant here has to break every
53// match that needs to change, in this workspace and in anyone else's code. That is
54// the property `spec/10-backend.md` section 10.8 is claiming when it says adding a
55// target is a data change: the compiler tells you every place the data is read.
56pub enum Arch {
57    /// x86-64, the first target and the one `M3` brings up.
58    X86_64,
59    /// AArch64, the second target, `M6`.
60    Aarch64,
61    /// 64-bit RISC-V. `spec/10-backend.md` calls this the middle-end canary, because it has
62    /// no condition codes and no complex addressing modes, so anything the middle end got
63    /// away with on x86-64 shows up here.
64    Riscv64,
65}
66
67impl Arch {
68    /// Pointer width in bits.
69    pub const fn pointer_width(self) -> u32 {
70        match self {
71            Arch::X86_64 | Arch::Aarch64 | Arch::Riscv64 => 64,
72        }
73    }
74
75    /// Whether the target is little-endian.
76    pub const fn is_little_endian(self) -> bool {
77        match self {
78            Arch::X86_64 | Arch::Aarch64 | Arch::Riscv64 => true,
79        }
80    }
81
82    /// The name as it appears in a triple.
83    pub const fn as_str(self) -> &'static str {
84        match self {
85            Arch::X86_64 => "x86_64",
86            Arch::Aarch64 => "aarch64",
87            Arch::Riscv64 => "riscv64",
88        }
89    }
90}
91
92/// The operating system a target runs on.
93#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
94// Deliberately not `#[non_exhaustive]`. Adding a variant here has to break every
95// match that needs to change, in this workspace and in anyone else's code. That is
96// the property `spec/10-backend.md` section 10.8 is claiming when it says adding a
97// target is a data change: the compiler tells you every place the data is read.
98pub enum Os {
99    /// Linux, hosted or freestanding.
100    Linux,
101    /// Apple platforms. `spec/12-abi-and-runtime.md` section 12.3 lists the four places
102    /// Apple diverges from AAPCS64, and every one of them is a real bug if missed.
103    Darwin,
104    /// Windows.
105    Windows,
106    /// No operating system, which is what `-ffreestanding` kernel work looks like.
107    None,
108}
109
110impl Os {
111    /// The name as it appears in a triple.
112    pub const fn as_str(self) -> &'static str {
113        match self {
114            Os::Linux => "linux",
115            Os::Darwin => "darwin",
116            Os::Windows => "windows",
117            Os::None => "none",
118        }
119    }
120
121    /// The object file format this operating system uses.
122    pub const fn object_format(self) -> ObjectFormat {
123        match self {
124            Os::Linux | Os::None => ObjectFormat::Elf,
125            Os::Darwin => ObjectFormat::MachO,
126            Os::Windows => ObjectFormat::Coff,
127        }
128    }
129}
130
131/// The C runtime and ABI variant.
132#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
133// Deliberately not `#[non_exhaustive]`. Adding a variant here has to break every
134// match that needs to change, in this workspace and in anyone else's code. That is
135// the property `spec/10-backend.md` section 10.8 is claiming when it says adding a
136// target is a data change: the compiler tells you every place the data is read.
137pub enum Env {
138    /// The default for the operating system.
139    None,
140    /// glibc.
141    Gnu,
142    /// musl.
143    Musl,
144    /// The MSVC ABI.
145    Msvc,
146}
147
148impl Env {
149    /// The name as it appears in a triple, if it appears at all.
150    pub const fn as_str(self) -> &'static str {
151        match self {
152            Env::None => "none",
153            Env::Gnu => "gnu",
154            Env::Musl => "musl",
155            Env::Msvc => "msvc",
156        }
157    }
158}
159
160/// The object file format to emit.
161#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
162// Deliberately not `#[non_exhaustive]`. Adding a variant here has to break every
163// match that needs to change, in this workspace and in anyone else's code. That is
164// the property `spec/10-backend.md` section 10.8 is claiming when it says adding a
165// target is a data change: the compiler tells you every place the data is read.
166pub enum ObjectFormat {
167    /// ELF.
168    Elf,
169    /// Mach-O.
170    MachO,
171    /// COFF.
172    Coff,
173}
174
175impl ObjectFormat {
176    /// The name used in diagnostics and in `--print-config`.
177    pub const fn as_str(self) -> &'static str {
178        match self {
179            ObjectFormat::Elf => "elf",
180            ObjectFormat::MachO => "macho",
181            ObjectFormat::Coff => "coff",
182        }
183    }
184}
185
186/// A target triple.
187///
188/// We accept the LLVM-style `arch-vendor-os-env` form because that is what build systems
189/// pass, and we normalise it to the three fields we actually branch on. The vendor field is
190/// parsed and discarded: no decision in the compiler depends on it, and keeping it would
191/// invite one.
192#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
193pub struct Triple {
194    /// The architecture.
195    pub arch: Arch,
196    /// The operating system.
197    pub os: Os,
198    /// The runtime and ABI variant.
199    pub env: Env,
200}
201
202impl Triple {
203    /// A triple from its three parts.
204    pub const fn new(arch: Arch, os: Os, env: Env) -> Self {
205        Self { arch, os, env }
206    }
207
208    /// The triple of the machine this compiler is running on.
209    ///
210    /// Used as the default target, which is what makes `rucc hello.c` work with no flags.
211    /// Unknown host combinations are not an error here: they are reported by the driver,
212    /// where there is somewhere to report them to.
213    pub fn host() -> Option<Self> {
214        let arch = match std::env::consts::ARCH {
215            "x86_64" => Arch::X86_64,
216            "aarch64" => Arch::Aarch64,
217            "riscv64" => Arch::Riscv64,
218            _ => return None,
219        };
220        // Which libc this is matters, and `std::env::consts` does not say. A compiler built on
221        // Alpine and defaulting to `x86_64-unknown-linux-gnu` describes a machine it is not
222        // running on: musl and glibc disagree about `int_fast16_t` among other things, and a
223        // header that is written out of the predefined type names picks the disagreement up.
224        // The libc rucc itself was linked against is the best evidence available about the one
225        // the code it compiles will be linked against, and it is right on every machine where
226        // rucc was built for the machine it runs on.
227        let linux = if cfg!(target_env = "musl") { Env::Musl } else { Env::Gnu };
228        let (os, env) = match std::env::consts::OS {
229            "linux" => (Os::Linux, linux),
230            "macos" => (Os::Darwin, Env::None),
231            "windows" => (Os::Windows, Env::Msvc),
232            _ => return None,
233        };
234        Some(Self::new(arch, os, env))
235    }
236}
237
238impl fmt::Display for Triple {
239    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240        // Always four fields, always the same spelling, because this string ends up in
241        // `--print-config` output that people diff.
242        write!(f, "{}-unknown-{}-{}", self.arch.as_str(), self.os.as_str(), self.env.as_str())
243    }
244}
245
246/// Why a triple failed to parse.
247#[derive(Debug, Clone, PartialEq, Eq)]
248pub struct ParseTripleError {
249    /// The triple as given.
250    pub input: String,
251    /// What specifically was not recognised.
252    pub reason: &'static str,
253}
254
255impl fmt::Display for ParseTripleError {
256    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257        write!(f, "unsupported target triple `{}`: {}", self.input, self.reason)
258    }
259}
260
261impl std::error::Error for ParseTripleError {}
262
263impl FromStr for Triple {
264    type Err = ParseTripleError;
265
266    fn from_str(s: &str) -> Result<Self, Self::Err> {
267        let err = |reason| ParseTripleError { input: s.to_owned(), reason };
268        let mut parts = s.split('-');
269
270        let arch = match parts.next() {
271            Some("x86_64" | "amd64") => Arch::X86_64,
272            Some("aarch64" | "arm64") => Arch::Aarch64,
273            Some("riscv64") => Arch::Riscv64,
274            _ => return Err(err("unknown architecture")),
275        };
276
277        // The vendor field is optional in practice. `x86_64-linux-gnu` and
278        // `x86_64-unknown-linux-gnu` both occur in the wild and mean the same thing, so the
279        // remaining fields are matched by content rather than by position.
280        let rest: Vec<&str> = parts.collect();
281        let mut os = None;
282        let mut env = None;
283        for part in &rest {
284            match *part {
285                "linux" => os = Some(Os::Linux),
286                "darwin" | "macos" | "macosx" | "ios" => os = Some(Os::Darwin),
287                "windows" | "win32" => os = Some(Os::Windows),
288                // `none` is the one token that means different things in the two positions.
289                // In `x86_64-unknown-none-elf` it is the operating system; in
290                // `aarch64-apple-darwin-none` it is the environment. Which one it is depends
291                // on whether an operating system has already been seen, and that rule is what
292                // makes `Display` round-trip through `FromStr`.
293                "none" if os.is_none() => os = Some(Os::None),
294                "none" => env = Some(Env::None),
295                "elf" => os = os.or(Some(Os::None)),
296                "gnu" | "gnueabi" | "gnueabihf" => env = Some(Env::Gnu),
297                "musl" | "musleabi" | "musleabihf" => env = Some(Env::Musl),
298                "msvc" => env = Some(Env::Msvc),
299                _ => {}
300            }
301        }
302
303        let os = os.ok_or_else(|| err("unknown operating system"))?;
304        let env = env.unwrap_or(match os {
305            Os::Linux => Env::Gnu,
306            Os::Windows => Env::Msvc,
307            Os::Darwin | Os::None => Env::None,
308        });
309        Ok(Self::new(arch, os, env))
310    }
311}
312
313/// The facts about a target that the compiler reads instead of hard-coding.
314///
315/// This is the whole of what a pass is allowed to know about where its output will run.
316/// It grows, and every field added here is one fewer `#[cfg]` somewhere it should not be.
317#[derive(Debug, Clone, PartialEq, Eq)]
318#[non_exhaustive]
319pub struct TargetInfo {
320    /// The triple this describes.
321    pub triple: Triple,
322    /// Width of a pointer in bits.
323    pub pointer_width: u32,
324    /// Whether bytes are ordered little end first.
325    pub little_endian: bool,
326    /// Whether a bare `char` is signed.
327    ///
328    /// Signed on x86-64 and unsigned on AArch64 Linux, which is the classic source of code
329    /// that works on one and not the other, so it is data rather than an assumption.
330    pub char_is_signed: bool,
331    /// Width of `long` in bits. This is the field that separates the LP64 world from
332    /// Windows LLP64.
333    pub long_width: u32,
334    /// Width of `long double` in bits: 80 bits of x87 stored in 128 on SysV x86-64,
335    /// 64 on Apple platforms, 64 on Windows.
336    pub long_double_width: u32,
337    /// The format `long double` actually is, which the width does not say.
338    ///
339    /// It is 128 bits wide on SysV x86-64 and on AArch64 Linux and the two are not the same
340    /// type: one is the x87 eighty bit format padded out to sixteen bytes and the other is
341    /// true quad precision with a hundred and thirteen bits of significand. Anything that
342    /// converts a constant or folds one has to know which, and the width alone cannot say.
343    pub long_double_format: Format,
344    /// The format `_Float64x` is, which is the widest format the target has short of a software
345    /// one.
346    ///
347    /// It follows the architecture and not the operating system, which is what makes it worth a
348    /// field of its own next to `long double`. Apple and Windows define `long double` as a
349    /// `double` and neither of them takes `_Float64x` down with it: the type has to be wider
350    /// than a `_Float64`, so it is the x87 eighty bit format on x86-64 and quad precision on
351    /// AArch64 and RISC-V wherever it is written.
352    pub float64x_format: Format,
353    /// Width of `wchar_t` in bits, which decides what a wide literal is encoded in.
354    ///
355    /// It is 16 on Windows, so a wide string there is UTF-16 and a character outside the basic
356    /// plane takes two elements, and 32 everywhere else, where a wide string is UTF-32 and no
357    /// character takes more than one.
358    pub wchar_width: u32,
359    /// Whether `wchar_t` is signed.
360    ///
361    /// x86-64 Linux makes it a signed `int` and AArch64 Linux makes it an `unsigned int`,
362    /// following the psABI's rule for plain `char`, so `L'\xffffffff'` is minus one on one of
363    /// them and four billion on the other.
364    pub wchar_is_signed: bool,
365    /// The granule a `_BitInt` wider than 64 bits is laid out in, in bits.
366    ///
367    /// Above 64 bits the psABIs stop treating a `_BitInt` like a standard integer type and
368    /// start treating it like an array of these, so its size is rounded up to a multiple of
369    /// this and its alignment is this. It is 64 on x86-64 and RISC-V and 128 on AArch64, which
370    /// is why `_BitInt(65)` is sixteen bytes aligned to eight on one and sixteen bytes aligned
371    /// to sixteen on the other. Measured with clang 18 on x86-64 Linux and clang on AArch64
372    /// Darwin rather than read off the documents.
373    pub bit_int_granule: u32,
374    /// The object format to emit.
375    pub object_format: ObjectFormat,
376    /// What `__builtin_va_list` is, which is the type every `va_list` in every header is a
377    /// typedef of.
378    pub va_list: VaList,
379    /// The registers the machine has, which is [`RegFile::EMPTY`] for an architecture nothing
380    /// has described yet.
381    pub regs: &'static RegFile,
382    /// Which registers the calling convention gives which job, or `None` while the
383    /// architecture has no register file to name them out of.
384    pub call_regs: Option<&'static CallRegs>,
385}
386
387/// The type a target's `__builtin_va_list` is.
388///
389/// A variable argument list is the one place a psABI dictates a C type rather than how a type
390/// travels, and the four answers below are not four spellings of one thing: `sizeof(va_list)` is
391/// eight bytes on Apple's AArch64 and thirty two on Linux's, and on SysV x86-64 a `va_list` is an
392/// array, so a `va_list` passed to a function is passed as a pointer and one assigned to another
393/// is a constraint violation rather than a copy. Code in the wild depends on all of that.
394#[derive(Debug, Clone, Copy, PartialEq, Eq)]
395// Deliberately not `#[non_exhaustive]`, for the reason [`Arch`] is not: a fifth answer here is
396// a fifth type to build, and every place that builds one should stop compiling until it does.
397pub enum VaList {
398    /// `char *`, which is what a target whose arguments are all passed in one place needs: the
399    /// address of the next argument and nothing else. Apple's AArch64 and both Windows targets.
400    CharPointer,
401    /// `void *`, which is the RISC-V psABI's spelling of the same thing.
402    VoidPointer,
403    /// `struct __va_list_tag { unsigned gp_offset, fp_offset; void *overflow_arg_area,
404    /// *reg_save_area; } [1]`, the SysV x86-64 one. Arguments arrive in two register files and
405    /// on the stack, so the list is a cursor into each, and the array of one is what makes
406    /// passing it to `vfprintf` pass its address.
407    SysV,
408    /// `struct __va_list { void *__stack, *__gr_top, *__vr_top; int __gr_offs, __vr_offs; }`,
409    /// the AAPCS64 one. The same idea as SysV's, counting down from the top of each save area
410    /// rather than up from the bottom, and not an array.
411    Aapcs,
412}
413
414impl VaList {
415    /// The name used in `--print-config`.
416    #[must_use]
417    pub const fn as_str(self) -> &'static str {
418        match self {
419            VaList::CharPointer => "char-pointer",
420            VaList::VoidPointer => "void-pointer",
421            VaList::SysV => "sysv",
422            VaList::Aapcs => "aapcs",
423        }
424    }
425}
426
427impl TargetInfo {
428    /// The description of `triple`.
429    pub fn new(triple: Triple) -> Self {
430        let char_is_signed = match (triple.arch, triple.os) {
431            // The AArch64 and RISC-V psABIs make plain `char` unsigned, and x86-64 SysV
432            // makes it signed. Apple and Windows both override that back to signed on
433            // AArch64, which is the kind of divergence that only ever surfaces as a bug
434            // report from someone whose lexer compares a `char` against a negative value.
435            (Arch::Aarch64 | Arch::Riscv64, Os::Linux | Os::None) => false,
436            _ => true,
437        };
438        let long_width = match triple.os {
439            // Windows is LLP64: `long` stays 32 bits on a 64-bit target.
440            Os::Windows => 32,
441            _ => triple.arch.pointer_width(),
442        };
443        let long_double_width = match triple.os {
444            // Apple defines `long double` as `double`, per spec/12-abi-and-runtime.md
445            // section 12.3, and Windows does the same. On the SysV targets it is a distinct
446            // type: 80 bits of x87 stored in 128 on x86-64, and true quad precision on
447            // AArch64 and RISC-V.
448            Os::Darwin | Os::Windows => 64,
449            Os::Linux | Os::None => 128,
450        };
451        let long_double_format = match (triple.arch, long_double_width) {
452            (_, 64) => Format::Double,
453            // The one place two targets agree on the width and disagree on the type.
454            (Arch::X86_64, _) => Format::X87Extended,
455            (Arch::Aarch64 | Arch::Riscv64, _) => Format::Quad,
456        };
457        let float64x_format = match triple.arch {
458            Arch::X86_64 => Format::X87Extended,
459            Arch::Aarch64 | Arch::Riscv64 => Format::Quad,
460        };
461        let bit_int_granule = match triple.arch {
462            Arch::Aarch64 => 128,
463            Arch::X86_64 | Arch::Riscv64 => 64,
464        };
465        // Windows makes `wchar_t` 16 bits so that a wide string is UTF-16, and AArch64 Linux
466        // makes it unsigned the way it makes plain `char` unsigned. Neither follows from
467        // anything else here, which is why both are their own field.
468        let wchar_width = if triple.os == Os::Windows { 16 } else { 32 };
469        let wchar_is_signed = !matches!(
470            (triple.arch, triple.os),
471            (_, Os::Windows) | (Arch::Aarch64, Os::Linux | Os::None)
472        );
473        let va_list = match (triple.arch, triple.os) {
474            // Windows passes every argument in one place and spills the register ones next to
475            // the stack ones, so the list is an address, and Apple does the same on AArch64.
476            (_, Os::Windows) | (Arch::Aarch64, Os::Darwin) => VaList::CharPointer,
477            (Arch::X86_64, _) => VaList::SysV,
478            (Arch::Aarch64, _) => VaList::Aapcs,
479            (Arch::Riscv64, _) => VaList::VoidPointer,
480        };
481        // AArch64 and RISC-V have register files and this crate has not written them down yet.
482        // They arrive with the backends that need them, in M6 and M7.
483        let regs = match triple.arch {
484            Arch::X86_64 => &x86_64::REGS,
485            Arch::Aarch64 | Arch::Riscv64 => &RegFile::EMPTY,
486        };
487        let call_regs = match (triple.arch, triple.os) {
488            (Arch::X86_64, Os::Windows) => Some(&x86_64::WIN64),
489            // Apple's x86-64 follows SysV, and its divergences from it are on AArch64.
490            (Arch::X86_64, _) => Some(&x86_64::SYSV),
491            (Arch::Aarch64 | Arch::Riscv64, _) => None,
492        };
493        Self {
494            triple,
495            pointer_width: triple.arch.pointer_width(),
496            little_endian: triple.arch.is_little_endian(),
497            char_is_signed,
498            long_width,
499            long_double_width,
500            long_double_format,
501            float64x_format,
502            wchar_width,
503            wchar_is_signed,
504            bit_int_granule,
505            object_format: triple.os.object_format(),
506            va_list,
507            regs,
508            call_regs,
509        }
510    }
511
512    /// The largest an object may be on this target, in bytes.
513    ///
514    /// `PTRDIFF_MAX`, which is what C 6.5.6 needs it to be: subtracting two pointers into one
515    /// object has to have an answer, and the answer has a `ptrdiff_t` to fit in. So an object
516    /// of exactly this many bytes is allowed and one byte more is not, which is the line GCC
517    /// draws too. It is the only size limit in the compiler and every layout question that has
518    /// one asks here rather than at whatever its own arithmetic happens to overflow at.
519    #[must_use]
520    pub const fn max_object_size(&self) -> u64 {
521        (1u64 << (self.pointer_width - 1)) - 1
522    }
523}
524
525#[cfg(test)]
526mod tests {
527    use super::*;
528
529    #[test]
530    fn parses_a_four_field_triple() {
531        let t: Triple = "x86_64-unknown-linux-gnu".parse().unwrap();
532        assert_eq!(t, Triple::new(Arch::X86_64, Os::Linux, Env::Gnu));
533    }
534
535    #[test]
536    fn parses_a_triple_with_no_vendor() {
537        let t: Triple = "aarch64-linux-musl".parse().unwrap();
538        assert_eq!(t, Triple::new(Arch::Aarch64, Os::Linux, Env::Musl));
539    }
540
541    #[test]
542    fn accepts_the_common_aliases() {
543        let a: Triple = "arm64-apple-darwin".parse().unwrap();
544        let b: Triple = "aarch64-apple-darwin".parse().unwrap();
545        assert_eq!(a, b);
546        assert_eq!(a.env, Env::None);
547    }
548
549    #[test]
550    fn fills_in_the_default_environment() {
551        let t: Triple = "x86_64-unknown-linux".parse().unwrap();
552        assert_eq!(t.env, Env::Gnu);
553        let w: Triple = "x86_64-pc-windows".parse().unwrap();
554        assert_eq!(w.env, Env::Msvc);
555    }
556
557    #[test]
558    fn rejects_what_it_does_not_support() {
559        let e = "sparc64-unknown-linux-gnu".parse::<Triple>().unwrap_err();
560        assert_eq!(e.reason, "unknown architecture");
561        let e = "x86_64-unknown-plan9".parse::<Triple>().unwrap_err();
562        assert_eq!(e.reason, "unknown operating system");
563    }
564
565    #[test]
566    fn displays_in_a_normalised_form() {
567        let t: Triple = "amd64-linux-gnu".parse().unwrap();
568        assert_eq!(t.to_string(), "x86_64-unknown-linux-gnu");
569    }
570
571    #[test]
572    fn display_round_trips_through_parse() {
573        for s in [
574            "x86_64-unknown-linux-gnu",
575            "aarch64-unknown-darwin-none",
576            "riscv64-unknown-linux-musl",
577        ] {
578            let t: Triple = s.parse().unwrap();
579            assert_eq!(t.to_string().parse::<Triple>().unwrap(), t);
580        }
581    }
582
583    #[test]
584    fn char_signedness_follows_the_psabi() {
585        let x86 = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
586        let arm = TargetInfo::new("aarch64-unknown-linux-gnu".parse().unwrap());
587        let mac = TargetInfo::new("aarch64-apple-darwin".parse().unwrap());
588        assert!(x86.char_is_signed);
589        assert!(!arm.char_is_signed);
590        assert!(mac.char_is_signed, "Apple overrides AAPCS64 back to a signed char");
591    }
592
593    #[test]
594    fn windows_is_llp64() {
595        let win = TargetInfo::new("x86_64-pc-windows-msvc".parse().unwrap());
596        assert_eq!(win.pointer_width, 64);
597        assert_eq!(win.long_width, 32);
598    }
599
600    #[test]
601    fn the_largest_object_is_ptrdiff_max() {
602        // Half the address space less one, which is what a pointer subtraction across the whole
603        // of one object has to fit in. gcc 16 on x86-64 prints this same number when it refuses
604        // an array, and takes an object of exactly this many bytes.
605        for triple in ["x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-pc-windows-msvc"]
606        {
607            let target = TargetInfo::new(triple.parse().unwrap());
608            assert_eq!(target.max_object_size(), 9_223_372_036_854_775_807, "{triple}");
609        }
610    }
611
612    #[test]
613    fn apple_long_double_is_double() {
614        let mac = TargetInfo::new("aarch64-apple-darwin".parse().unwrap());
615        assert_eq!(mac.long_double_width, 64);
616        assert_eq!(mac.long_double_format, Format::Double);
617        let linux = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
618        assert_eq!(linux.long_double_width, 128);
619    }
620
621    #[test]
622    fn wchar_t_divides_the_targets_in_two_directions_at_once() {
623        // Windows narrows it to sixteen bits, which makes a wide string UTF-16 there and
624        // UTF-32 everywhere else, and AArch64 Linux makes it unsigned without narrowing it.
625        let windows = TargetInfo::new("x86_64-pc-windows-msvc".parse().unwrap());
626        assert_eq!((windows.wchar_width, windows.wchar_is_signed), (16, false));
627        let arm = TargetInfo::new("aarch64-unknown-linux-gnu".parse().unwrap());
628        assert_eq!((arm.wchar_width, arm.wchar_is_signed), (32, false));
629        let linux = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
630        assert_eq!((linux.wchar_width, linux.wchar_is_signed), (32, true));
631        // Apple keeps it signed on the same processor where Linux does not, in the same way it
632        // keeps plain `char` signed there.
633        let mac = TargetInfo::new("aarch64-apple-darwin".parse().unwrap());
634        assert_eq!((mac.wchar_width, mac.wchar_is_signed), (32, true));
635    }
636
637    #[test]
638    fn va_list_is_the_psabis_type_and_not_one_type_with_four_spellings() {
639        let linux = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
640        assert_eq!(linux.va_list, VaList::SysV);
641        // x86-64 Darwin follows SysV here, and AArch64 Darwin does not follow AAPCS64.
642        let mac = TargetInfo::new("x86_64-apple-darwin".parse().unwrap());
643        assert_eq!(mac.va_list, VaList::SysV);
644        let arm_mac = TargetInfo::new("aarch64-apple-darwin".parse().unwrap());
645        assert_eq!(arm_mac.va_list, VaList::CharPointer);
646        let arm = TargetInfo::new("aarch64-unknown-linux-gnu".parse().unwrap());
647        assert_eq!(arm.va_list, VaList::Aapcs);
648        // Windows passes everything one way on both processors, so both get the simple one.
649        let win = TargetInfo::new("x86_64-pc-windows-msvc".parse().unwrap());
650        assert_eq!(win.va_list, VaList::CharPointer);
651        let arm_win = TargetInfo::new("aarch64-pc-windows-msvc".parse().unwrap());
652        assert_eq!(arm_win.va_list, VaList::CharPointer);
653        let riscv = TargetInfo::new("riscv64-unknown-linux-gnu".parse().unwrap());
654        assert_eq!(riscv.va_list, VaList::VoidPointer);
655    }
656
657    #[test]
658    fn two_targets_agree_on_the_width_of_long_double_and_not_on_the_type() {
659        // Sixteen bytes on both, and a different number in them: the x87 format has sixty four
660        // bits of significand and quad precision has a hundred and thirteen, so a constant
661        // converted for one is the wrong bits for the other.
662        let x86 = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
663        let arm = TargetInfo::new("aarch64-unknown-linux-gnu".parse().unwrap());
664        assert_eq!(x86.long_double_width, arm.long_double_width);
665        assert_eq!(x86.long_double_format, Format::X87Extended);
666        assert_eq!(arm.long_double_format, Format::Quad);
667        assert_eq!(x86.long_double_format.precision(), 64);
668        assert_eq!(arm.long_double_format.precision(), 113);
669        // Windows keeps the name and drops the type, the way Apple does.
670        let windows = TargetInfo::new("x86_64-pc-windows-msvc".parse().unwrap());
671        assert_eq!(windows.long_double_format, Format::Double);
672    }
673
674    #[test]
675    fn float64x_follows_the_processor_where_long_double_follows_the_operating_system() {
676        // `_Float64x` is the widest format the hardware has, and no ABI takes it away the way
677        // Apple and Windows take `long double` away. So the two fields say the same thing on
678        // Linux and disagree everywhere else, which is the whole reason there are two of them.
679        let x86 = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
680        assert_eq!(x86.float64x_format, Format::X87Extended);
681        let arm = TargetInfo::new("aarch64-unknown-linux-gnu".parse().unwrap());
682        assert_eq!(arm.float64x_format, Format::Quad);
683        let riscv = TargetInfo::new("riscv64-unknown-linux-gnu".parse().unwrap());
684        assert_eq!(riscv.float64x_format, Format::Quad);
685
686        let mac = TargetInfo::new("aarch64-apple-darwin".parse().unwrap());
687        assert_eq!(mac.long_double_format, Format::Double);
688        assert_eq!(mac.float64x_format, Format::Quad);
689        let windows = TargetInfo::new("x86_64-pc-windows-msvc".parse().unwrap());
690        assert_eq!(windows.long_double_format, Format::Double);
691        assert_eq!(windows.float64x_format, Format::X87Extended);
692    }
693
694    #[test]
695    fn the_object_format_follows_the_operating_system() {
696        assert_eq!(Os::Linux.object_format(), ObjectFormat::Elf);
697        assert_eq!(Os::Darwin.object_format(), ObjectFormat::MachO);
698        assert_eq!(Os::Windows.object_format(), ObjectFormat::Coff);
699    }
700
701    #[test]
702    fn a_target_carries_its_registers_and_says_so_when_it_has_none() {
703        let of = |triple: &str| TargetInfo::new(triple.parse().unwrap());
704        let linux = of("x86_64-unknown-linux-gnu");
705        assert_eq!(linux.regs.reg_named("rdi"), Some((x86_64::GPR, x86_64::RDI)));
706        assert_eq!(linux.call_regs.map(|regs| regs.int_args[0]), Some(x86_64::RDI));
707        // Apple's x86-64 is SysV and Windows is the one that is not.
708        let apple = of("x86_64-apple-darwin");
709        assert_eq!(apple.call_regs.map(|regs| regs.int_args[0]), Some(x86_64::RDI));
710        let windows = of("x86_64-pc-windows-msvc");
711        assert_eq!(windows.regs.len(x86_64::GPR), 16);
712        assert_eq!(windows.call_regs.map(|regs| regs.int_args[0]), Some(x86_64::RCX));
713        // Not described yet, and saying nothing is the answer rather than saying x86-64's.
714        let arm = of("aarch64-unknown-linux-gnu");
715        assert!(arm.regs.is_empty());
716        assert!(arm.call_regs.is_none());
717    }
718
719    #[test]
720    fn the_host_triple_is_one_we_support() {
721        // Every host in spec/15-testing.md section 15.7 must be recognised, and CI runs on
722        // all three, so a failure here means a host we claim support for stopped resolving.
723        let host = Triple::host().expect("the host must be a supported target");
724        assert_eq!(host.to_string().parse::<Triple>().unwrap(), host);
725    }
726}