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