1use rucc_base::float::Format;
23use rucc_session::{GnucVersion, OptLevel, Options, Std};
24use rucc_target::{Arch, Env, Os, TargetInfo, Triple};
25use rucc_tuple::{self as tuple};
26
27pub const BUILT_IN: &str = "<built-in>";
29
30pub const COMMAND_LINE: &str = "<command-line>";
32
33#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct Timestamp {
40 pub date: String,
42 pub time: String,
44}
45
46impl Timestamp {
47 pub fn now() -> Timestamp {
52 let seconds = match std::env::var("SOURCE_DATE_EPOCH").ok().and_then(|v| v.parse().ok()) {
53 Some(fixed) => fixed,
54 None => std::time::SystemTime::now()
55 .duration_since(std::time::UNIX_EPOCH)
56 .map_or(0, |d| d.as_secs() as i64),
57 };
58 Timestamp::from_unix(seconds)
59 }
60
61 pub fn from_unix(seconds: i64) -> Timestamp {
66 let days = seconds.div_euclid(86_400);
67 let rest = seconds.rem_euclid(86_400);
68 let (year, month, day) = civil_from_days(days);
69 const MONTHS: [&str; 12] =
70 ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
71 let name = MONTHS[(month - 1) as usize];
72 Timestamp {
73 date: format!("{name} {day:2} {year}"),
74 time: format!("{:02}:{:02}:{:02}", rest / 3600, (rest / 60) % 60, rest % 60),
75 }
76 }
77}
78
79fn civil_from_days(days: i64) -> (i64, u32, u32) {
85 let shifted = days + 719_468;
88 let era = shifted.div_euclid(146_097);
89 let day_of_era = shifted.rem_euclid(146_097);
90 let year_of_era =
91 (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
92 let year = year_of_era + era * 400;
93 let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
94 let marched = (5 * day_of_year + 2) / 153;
95 let day = (day_of_year - (153 * marched + 2) / 5 + 1) as u32;
96 let month = if marched < 10 { marched + 3 } else { marched - 9 } as u32;
97 (year + i64::from(month <= 2), month, day)
98}
99
100#[derive(Debug, Clone, PartialEq, Eq)]
102pub struct Predef {
103 pub std: Std,
105 pub gnu_extensions: bool,
108 pub gnu89_inline: bool,
112 pub gnuc: GnucVersion,
114 pub opt_level: OptLevel,
116 pub hosted: bool,
118 pub timestamp: Timestamp,
120 pub defines: Vec<String>,
122 pub undefines: Vec<String>,
124}
125
126impl Predef {
127 pub fn new() -> Predef {
129 Predef {
130 std: Std::default(),
131 gnu_extensions: true,
132 gnu89_inline: false,
133 gnuc: GnucVersion::default(),
134 opt_level: OptLevel::O0,
135 hosted: true,
136 timestamp: Timestamp::now(),
137 defines: Vec::new(),
138 undefines: Vec::new(),
139 }
140 }
141}
142
143impl Predef {
144 pub fn for_options(opts: &Options) -> Predef {
150 Predef {
151 std: opts.std,
152 gnu_extensions: opts.gnu_extensions,
153 gnu89_inline: opts.gnu89_inline,
154 gnuc: opts.gnuc,
155 opt_level: opts.opt_level,
156 hosted: opts.hosted,
157 timestamp: Timestamp::now(),
158 defines: opts.defines.clone(),
159 undefines: opts.undefines.clone(),
160 }
161 }
162}
163
164impl Default for Predef {
165 fn default() -> Predef {
166 Predef::new()
167 }
168}
169
170struct Defs {
172 text: String,
173}
174
175impl Defs {
176 fn new() -> Defs {
177 Defs { text: String::new() }
178 }
179
180 fn set(&mut self, name: &str, value: &str) {
182 self.text.push_str("#define ");
183 self.text.push_str(name);
184 self.text.push(' ');
185 self.text.push_str(value);
186 self.text.push('\n');
187 }
188
189 fn flag(&mut self, name: &str) {
191 self.set(name, "1");
192 }
193
194 fn set_if(&mut self, when: bool, name: &str, value: &str) {
195 if when {
196 self.set(name, value);
197 }
198 }
199
200 fn flag_if(&mut self, when: bool, name: &str) {
201 if when {
202 self.flag(name);
203 }
204 }
205}
206
207pub(crate) fn built_in(target: &TargetInfo, opts: &Predef) -> String {
209 let mut d = Defs::new();
210 identity(&mut d, target, opts);
211 d.set("__DATE__", &format!("\"{}\"", opts.timestamp.date));
215 d.set("__TIME__", &format!("\"{}\"", opts.timestamp.time));
216 dialect(&mut d, opts);
217 optimization(&mut d, opts);
218 platform(&mut d, target, opts);
219 sizes(&mut d, target);
220 integers(&mut d, target);
221 floats(&mut d, target);
222 atomics(&mut d, target);
223 d.text
224}
225
226pub(crate) fn command_line(opts: &Predef) -> String {
232 let mut d = Defs::new();
233 for define in &opts.defines {
234 match define.split_once('=') {
235 Some((name, value)) => d.set(name, value),
236 None => d.flag(define),
239 }
240 }
241 for name in &opts.undefines {
242 d.text.push_str("#undef ");
243 d.text.push_str(name);
244 d.text.push('\n');
245 }
246 d.text
247}
248
249fn identity(d: &mut Defs, target: &TargetInfo, opts: &Predef) {
251 d.flag("__rucc__");
252 d.set("__rucc_version__", "\"0.1.0\"");
253 d.set("__rucc_major__", "0");
254 d.set("__rucc_minor__", "1");
255 d.set("__rucc_patchlevel__", "0");
256 d.set("__GNUC__", &opts.gnuc.major.to_string());
258 d.set("__GNUC_MINOR__", &opts.gnuc.minor.to_string());
259 d.set("__GNUC_PATCHLEVEL__", &opts.gnuc.patch.to_string());
260 d.set("__VERSION__", "\"rucc 0.1.0\"");
261 let gnu_inline = opts.gnu89_inline || opts.std == Std::C89;
270 d.flag_if(gnu_inline, "__GNUC_GNU_INLINE__");
271 d.flag_if(!gnu_inline, "__GNUC_STDC_INLINE__");
272 d.set("__GNUC_EXECUTION_CHARSET_NAME", "\"UTF-8\"");
277 let wide = if target.wchar_width == 16 { "\"UTF-16LE\"" } else { "\"UTF-32LE\"" };
278 d.set("__GNUC_WIDE_EXECUTION_CHARSET_NAME", wide);
279 d.set("__GXX_ABI_VERSION", "1021");
284}
285
286fn dialect(d: &mut Defs, opts: &Predef) {
288 d.flag("__STDC__");
289 d.set_if(opts.hosted, "__STDC_HOSTED__", "1");
290 d.set_if(!opts.hosted, "__STDC_HOSTED__", "0");
291 if let Some(version) = opts.std.stdc_version() {
292 d.set("__STDC_VERSION__", version);
293 }
294 d.flag_if(!opts.gnu_extensions, "__STRICT_ANSI__");
297 d.flag("__STDC_UTF_16__");
298 d.flag("__STDC_UTF_32__");
299 d.flag("__STDC_IEC_559__");
300 d.flag("__STDC_IEC_559_COMPLEX__");
301 d.set_if(opts.std == Std::C23, "__STDC_IEC_60559_BFP__", "202311L");
302 d.set("__STDC_ISO_10646__", "201706L");
303 d.set_if(opts.std == Std::C23, "__CHAR8_TYPE__", "unsigned char");
309 if opts.std.has_c11() {
321 d.flag("__STDC_NO_ATOMICS__");
322 d.flag("__STDC_NO_THREADS__");
323 d.flag("__STDC_NO_COMPLEX__");
324 }
325 d.set("__STDC_EMBED_NOT_FOUND__", "0");
330 d.set("__STDC_EMBED_FOUND__", "1");
331 d.set("__STDC_EMBED_EMPTY__", "2");
332}
333
334fn atomics(d: &mut Defs, target: &TargetInfo) {
346 d.set("__ATOMIC_RELAXED", "0");
347 d.set("__ATOMIC_CONSUME", "1");
348 d.set("__ATOMIC_ACQUIRE", "2");
349 d.set("__ATOMIC_RELEASE", "3");
350 d.set("__ATOMIC_ACQ_REL", "4");
351 d.set("__ATOMIC_SEQ_CST", "5");
352 let llong = if target.pointer_width == 64 { "2" } else { "1" };
355 for name in [
356 "BOOL", "CHAR", "CHAR8_T", "CHAR16_T", "CHAR32_T", "WCHAR_T", "SHORT", "INT", "LONG",
357 "POINTER",
358 ] {
359 d.set(&format!("__GCC_ATOMIC_{name}_LOCK_FREE"), "2");
360 }
361 d.set("__GCC_ATOMIC_LLONG_LOCK_FREE", llong);
365 d.set("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
366 for width in [1, 2, 4, 8] {
370 d.flag(&format!("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_{width}"));
371 }
372 if target.tuple.arch() == tuple::Arch::X86_64 {
377 d.set("__ATOMIC_HLE_ACQUIRE", "65536");
378 d.set("__ATOMIC_HLE_RELEASE", "131072");
379 }
380}
381
382fn optimization(d: &mut Defs, opts: &Predef) {
384 d.flag_if(opts.opt_level.runs_optimizer(), "__OPTIMIZE__");
385 d.flag_if(opts.opt_level.is_size(), "__OPTIMIZE_SIZE__");
386 d.flag_if(!opts.opt_level.runs_optimizer(), "__NO_INLINE__");
389 d.set("__FINITE_MATH_ONLY__", "0");
395}
396
397fn platform(d: &mut Defs, target: &TargetInfo, opts: &Predef) {
399 let Some(triple) = Triple::from_tuple(target.tuple) else {
405 return;
406 };
407 match triple.arch {
408 Arch::X86_64 => {
409 d.flag("__x86_64__");
410 d.flag("__x86_64");
411 d.flag("__amd64__");
412 d.flag("__amd64");
413 d.flag("__SSE__");
414 d.flag("__SSE2__");
415 d.flag("__MMX__");
416 d.flag("__SSE_MATH__");
417 d.flag("__SSE2_MATH__");
418 d.flag("__k8");
419 d.flag("__k8__");
420 d.flag("__FXSR__");
423 d.flag("__code_model_small__");
424 d.flag("__MMX_WITH_SSE__");
429 }
430 Arch::Aarch64 => {
431 d.flag("__aarch64__");
432 d.flag("__AARCH64EL__");
433 d.set("__ARM_ARCH", "8");
434 d.set("__ARM_ARCH_PROFILE", "'A'");
435 d.set("__ARM_64BIT_STATE", "1");
436 d.set("__ARM_ALIGN_MAX_PWR", "28");
437 d.set("__ARM_FP", "0xe");
438 d.set("__ARM_NEON", "1");
439 d.set("__ARM_FEATURE_UNALIGNED", "1");
440 d.set("__ARM_PCS_AAPCS64", "1");
441 }
442 Arch::Riscv64 => {
443 d.flag("__riscv");
444 d.set("__riscv_xlen", "64");
445 d.set("__riscv_flen", "64");
446 d.flag("__riscv_float_abi_double");
447 d.flag("__riscv_muldiv");
448 d.flag("__riscv_atomic");
449 d.flag("__riscv_compressed");
450 d.set("__riscv_cmodel_medlow", "1");
451 }
452 }
453 match triple.os {
454 Os::Linux => {
455 d.flag("__linux__");
456 d.flag("__linux");
457 d.flag("__unix__");
458 d.flag("__unix");
459 d.flag("__gnu_linux__");
460 d.flag("__ELF__");
461 if opts.gnu_extensions {
464 d.flag("linux");
465 d.flag("unix");
466 }
467 }
468 Os::Darwin => {
469 d.flag("__APPLE__");
470 d.flag("__MACH__");
471 d.flag("__unix__");
472 d.flag("__unix");
473 d.set("__APPLE_CC__", "6000");
474 d.set("__DYNAMIC__", "1");
475 if triple.arch == Arch::Aarch64 {
476 d.flag("__arm64__");
481 d.flag("__arm64");
482 }
483 if opts.gnu_extensions {
484 d.flag("unix");
485 }
486 }
487 Os::Windows => {
488 d.flag("_WIN32");
489 d.flag("__WIN32__");
490 d.flag("_WIN64");
491 d.flag("__WIN64__");
492 d.flag("__MINGW32__");
493 }
494 Os::None => {
495 d.flag("__ELF__");
498 }
499 }
500 match triple.env {
501 Env::Musl => d.flag("__musl__"),
502 Env::Gnu | Env::None | Env::Msvc => {}
503 }
504 if target.long_width == 64 && target.pointer_width == 64 {
507 d.flag("__LP64__");
508 d.flag("_LP64");
509 }
510 d.set("__USER_LABEL_PREFIX__", if triple.os == Os::Darwin { "_" } else { "" });
517 d.set("__REGISTER_PREFIX__", "");
521
522 if !matches!(triple.os, Os::Windows) {
525 d.set("__PIC__", "2");
526 d.set("__pic__", "2");
527 }
528}
529
530fn sizes(d: &mut Defs, target: &TargetInfo) {
532 let pointer = target.pointer_width / 8;
533 d.set("__GCC_CONSTRUCTIVE_SIZE", "64");
538 d.set("__GCC_DESTRUCTIVE_SIZE", "64");
539 let long = target.long_width / 8;
540 let long_double = target.long_double_width / 8;
541 d.set("__CHAR_BIT__", "8");
542 d.set("__SIZEOF_SHORT__", "2");
543 d.set("__SIZEOF_INT__", "4");
544 d.set("__SIZEOF_LONG__", &long.to_string());
545 d.set("__SIZEOF_LONG_LONG__", "8");
546 d.set("__SIZEOF_INT128__", "16");
547 d.set("__SIZEOF_FLOAT__", "4");
548 d.set("__SIZEOF_DOUBLE__", "8");
549 d.set("__SIZEOF_LONG_DOUBLE__", &long_double.to_string());
550 d.set("__SIZEOF_POINTER__", &pointer.to_string());
551 d.set("__SIZEOF_SIZE_T__", &pointer.to_string());
552 d.set("__SIZEOF_PTRDIFF_T__", &pointer.to_string());
553 d.set("__SIZEOF_WCHAR_T__", &wchar(target).size.to_string());
554 d.set("__SIZEOF_WINT_T__", "4");
555 d.set("__BIGGEST_ALIGNMENT__", "16");
556 d.set("__ORDER_LITTLE_ENDIAN__", "1234");
560 d.set("__ORDER_BIG_ENDIAN__", "4321");
561 d.set("__ORDER_PDP_ENDIAN__", "3412");
562 let order =
563 if target.little_endian { "__ORDER_LITTLE_ENDIAN__" } else { "__ORDER_BIG_ENDIAN__" };
564 d.set("__BYTE_ORDER__", order);
565 d.set("__FLOAT_WORD_ORDER__", order);
566 d.flag_if(!target.char_is_signed, "__CHAR_UNSIGNED__");
567}
568
569struct Wchar {
571 spelling: &'static str,
573 size: u32,
575 max: &'static str,
577 min: &'static str,
579}
580
581fn wchar(target: &TargetInfo) -> Wchar {
591 match (target.wchar_width, target.wchar_is_signed) {
592 (16, false) => Wchar { spelling: "short unsigned int", size: 2, max: "0xffff", min: "0" },
593 (16, true) => Wchar { spelling: "short int", size: 2, max: "0x7fff", min: "(-32767 - 1)" },
594 (_, false) => Wchar { spelling: "unsigned int", size: 4, max: "0xffffffffU", min: "0U" },
595 (_, true) => {
596 Wchar { spelling: "int", size: 4, max: "0x7fffffff", min: "(-__WCHAR_MAX__ - 1)" }
597 }
598 }
599}
600
601struct Wint {
603 spelling: &'static str,
605 max: &'static str,
607 min: &'static str,
609 width: u32,
611}
612
613fn wint(target: &TargetInfo) -> Wint {
620 match target.tuple.os() {
621 tuple::Os::Windows => {
622 Wint { spelling: "short unsigned int", max: "0xffff", min: "0", width: 16 }
623 }
624 os if os.is_darwin() => {
625 Wint { spelling: "int", max: "0x7fffffff", min: "(-__WINT_MAX__ - 1)", width: 32 }
626 }
627 _ => Wint { spelling: "unsigned int", max: "0xffffffffU", min: "0U", width: 32 },
628 }
629}
630
631fn integers(d: &mut Defs, target: &TargetInfo) {
633 let lp64 = target.long_width == 64;
637 let wide = if lp64 { "long int" } else { "long long int" };
638 let wide_unsigned = if lp64 { "long unsigned int" } else { "long long unsigned int" };
639 let wide_suffix = if lp64 { "L" } else { "LL" };
640 let wide_max = format!("0x7fffffffffffffff{wide_suffix}");
641 let wide_umax = format!("0xffffffffffffffffU{wide_suffix}");
642
643 d.set("__SCHAR_MAX__", "0x7f");
644 d.set("__SHRT_MAX__", "0x7fff");
645 d.set("__INT_MAX__", "0x7fffffff");
646 d.set("__LONG_MAX__", if lp64 { "0x7fffffffffffffffL" } else { "0x7fffffffL" });
647 d.set("__LONG_LONG_MAX__", "0x7fffffffffffffffLL");
648 d.set("__INTMAX_MAX__", &wide_max);
649 d.set("__UINTMAX_MAX__", &wide_umax);
650 d.set("__SIZE_MAX__", &wide_umax);
651 d.set("__PTRDIFF_MAX__", &wide_max);
652 d.set("__INTPTR_MAX__", &wide_max);
653 d.set("__UINTPTR_MAX__", &wide_umax);
654 d.set("__SIG_ATOMIC_MAX__", "0x7fffffff");
655 d.set("__SIG_ATOMIC_MIN__", "(-__SIG_ATOMIC_MAX__ - 1)");
656 d.set("__BITINT_MAXWIDTH__", "128");
663
664 let wchar = wchar(target);
665 d.set("__WCHAR_TYPE__", wchar.spelling);
666 d.set("__WCHAR_MAX__", wchar.max);
667 d.set("__WCHAR_MIN__", wchar.min);
668 let wint = wint(target);
669 d.set("__WINT_TYPE__", wint.spelling);
670 d.set("__WINT_MAX__", wint.max);
671 d.set("__WINT_MIN__", wint.min);
672 d.set("__SIZE_TYPE__", wide_unsigned);
673 d.set("__PTRDIFF_TYPE__", wide);
674 d.set("__INTMAX_TYPE__", wide);
675 d.set("__UINTMAX_TYPE__", wide_unsigned);
676 d.set("__INTPTR_TYPE__", wide);
677 d.set("__UINTPTR_TYPE__", wide_unsigned);
678 d.set("__SIG_ATOMIC_TYPE__", "int");
679 d.set("__CHAR16_TYPE__", "short unsigned int");
680 d.set("__CHAR32_TYPE__", "unsigned int");
681 d.set("__INTMAX_C(c)", &format!("c ## {wide_suffix}"));
682 d.set("__UINTMAX_C(c)", &format!("c ## U{wide_suffix}"));
683
684 exact(d, 8, "signed char", "unsigned char", "0x7f", "0xff", "");
686 exact(d, 16, "short int", "short unsigned int", "0x7fff", "0xffff", "");
687 exact(d, 32, "int", "unsigned int", "0x7fffffff", "0xffffffffU", "");
690 exact(d, 64, wide, wide_unsigned, &wide_max, &wide_umax, wide_suffix);
691
692 let fast_is_wide = target.tuple.arch() == tuple::Arch::X86_64
703 && lp64
704 && target.tuple.env() != tuple::Env::Musl;
705 let fast_middle = if fast_is_wide { wide } else { "int" };
706 d.set("__INT_FAST8_TYPE__", "signed char");
707 d.set("__UINT_FAST8_TYPE__", "unsigned char");
708 d.set("__INT_FAST8_MAX__", "0x7f");
709 d.set("__UINT_FAST8_MAX__", "0xff");
710 for width in [16, 32] {
711 let unsigned = if fast_middle == "int" { "unsigned int" } else { wide_unsigned };
712 let max = if fast_middle == "int" { "0x7fffffff" } else { wide_max.as_str() };
713 let umax = if fast_middle == "int" { "0xffffffffU" } else { wide_umax.as_str() };
714 d.set(&format!("__INT_FAST{width}_TYPE__"), fast_middle);
715 d.set(&format!("__UINT_FAST{width}_TYPE__"), unsigned);
716 d.set(&format!("__INT_FAST{width}_MAX__"), max);
717 d.set(&format!("__UINT_FAST{width}_MAX__"), umax);
718 }
719 d.set("__INT_FAST64_TYPE__", wide);
720 d.set("__UINT_FAST64_TYPE__", wide_unsigned);
721 d.set("__INT_FAST64_MAX__", &wide_max);
722 d.set("__UINT_FAST64_MAX__", &wide_umax);
723
724 widths(d, target, &wchar, &wint, if fast_is_wide { 64 } else { 32 });
725}
726
727fn widths(d: &mut Defs, target: &TargetInfo, wchar: &Wchar, wint: &Wint, fast_middle: u32) {
738 let pointer = target.pointer_width;
739 d.set("__SCHAR_WIDTH__", "8");
740 d.set("__SHRT_WIDTH__", "16");
741 d.set("__INT_WIDTH__", "32");
742 d.set("__LONG_WIDTH__", &target.long_width.to_string());
743 d.set("__LONG_LONG_WIDTH__", "64");
744 d.set("__INTMAX_WIDTH__", "64");
745 d.set("__INTPTR_WIDTH__", &pointer.to_string());
746 d.set("__PTRDIFF_WIDTH__", &pointer.to_string());
747 d.set("__SIZE_WIDTH__", &pointer.to_string());
748 d.set("__SIG_ATOMIC_WIDTH__", "32");
749 d.set("__WCHAR_WIDTH__", &(wchar.size * 8).to_string());
750 d.set("__WINT_WIDTH__", &wint.width.to_string());
751 for width in [8, 16, 32, 64] {
752 d.set(&format!("__INT_LEAST{width}_WIDTH__"), &width.to_string());
753 }
754 d.set("__INT_FAST8_WIDTH__", "8");
755 d.set("__INT_FAST16_WIDTH__", &fast_middle.to_string());
756 d.set("__INT_FAST32_WIDTH__", &fast_middle.to_string());
757 d.set("__INT_FAST64_WIDTH__", "64");
758}
759
760fn exact(
762 d: &mut Defs,
763 width: u32,
764 signed: &str,
765 unsigned: &str,
766 max: &str,
767 umax: &str,
768 width_suffix: &str,
772) {
773 d.set(&format!("__INT{width}_TYPE__"), signed);
774 d.set(&format!("__UINT{width}_TYPE__"), unsigned);
775 d.set(&format!("__INT{width}_MAX__"), max);
776 d.set(&format!("__UINT{width}_MAX__"), umax);
777 d.set(&format!("__INT_LEAST{width}_TYPE__"), signed);
778 d.set(&format!("__UINT_LEAST{width}_TYPE__"), unsigned);
779 d.set(&format!("__INT_LEAST{width}_MAX__"), max);
780 d.set(&format!("__UINT_LEAST{width}_MAX__"), umax);
781 let unsigned_after_promotion = width >= 32;
791 let u = if unsigned_after_promotion { "U" } else { "" };
792 if width_suffix.is_empty() && u.is_empty() {
793 d.set(&format!("__INT{width}_C(c)"), "c");
794 d.set(&format!("__UINT{width}_C(c)"), "c");
795 } else if width_suffix.is_empty() {
796 d.set(&format!("__INT{width}_C(c)"), "c");
797 d.set(&format!("__UINT{width}_C(c)"), &format!("c ## {u}"));
798 } else {
799 d.set(&format!("__INT{width}_C(c)"), &format!("c ## {width_suffix}"));
800 d.set(&format!("__UINT{width}_C(c)"), &format!("c ## {u}{width_suffix}"));
801 }
802}
803
804struct Characteristics {
810 mant_dig: &'static str,
811 dig: &'static str,
812 min_exp: &'static str,
813 min_10_exp: &'static str,
814 max_exp: &'static str,
815 max_10_exp: &'static str,
816 decimal_dig: &'static str,
817 max: &'static str,
818 norm_max: &'static str,
827 min: &'static str,
828 epsilon: &'static str,
829 denorm_min: &'static str,
830 is_iec_60559: &'static str,
834}
835
836const HALF: Characteristics = Characteristics {
838 mant_dig: "11",
839 dig: "3",
840 min_exp: "(-13)",
841 min_10_exp: "(-4)",
842 max_exp: "16",
843 max_10_exp: "4",
844 decimal_dig: "5",
845 max: "6.55040000000000000000000000000000000e+4",
846 norm_max: "6.55040000000000000000000000000000000e+4",
847 min: "6.10351562500000000000000000000000000e-5",
848 epsilon: "9.76562500000000000000000000000000000e-4",
849 denorm_min: "5.96046447753906250000000000000000000e-8",
850 is_iec_60559: "1",
851};
852
853const BFLOAT16: Characteristics = Characteristics {
855 mant_dig: "8",
856 dig: "2",
857 min_exp: "(-125)",
858 min_10_exp: "(-37)",
859 max_exp: "128",
860 max_10_exp: "38",
861 decimal_dig: "4",
862 max: "3.38953138925153547590470800371487867e+38",
863 norm_max: "3.38953138925153547590470800371487867e+38",
864 min: "1.17549435082228750796873653722224568e-38",
865 epsilon: "7.81250000000000000000000000000000000e-3",
866 denorm_min: "9.18354961579912115600575419704879436e-41",
867 is_iec_60559: "0",
868};
869
870const SINGLE: Characteristics = Characteristics {
872 mant_dig: "24",
873 dig: "6",
874 min_exp: "(-125)",
875 min_10_exp: "(-37)",
876 max_exp: "128",
877 max_10_exp: "38",
878 decimal_dig: "9",
879 max: "3.40282346638528859811704183484516925e+38",
880 norm_max: "3.40282346638528859811704183484516925e+38",
881 min: "1.17549435082228750796873653722224568e-38",
882 epsilon: "1.19209289550781250000000000000000000e-7",
883 denorm_min: "1.40129846432481707092372958328991613e-45",
884 is_iec_60559: "1",
885};
886
887const DOUBLE: Characteristics = Characteristics {
890 mant_dig: "53",
891 dig: "15",
892 min_exp: "(-1021)",
893 min_10_exp: "(-307)",
894 max_exp: "1024",
895 max_10_exp: "308",
896 decimal_dig: "17",
897 max: "1.79769313486231570814527423731704357e+308",
898 norm_max: "1.79769313486231570814527423731704357e+308",
899 min: "2.22507385850720138309023271733240406e-308",
900 epsilon: "2.22044604925031308084726333618164062e-16",
901 denorm_min: "4.94065645841246544176568792868221372e-324",
902 is_iec_60559: "1",
903};
904
905const X87: Characteristics = Characteristics {
907 mant_dig: "64",
908 dig: "18",
909 min_exp: "(-16381)",
910 min_10_exp: "(-4931)",
911 max_exp: "16384",
912 max_10_exp: "4932",
913 decimal_dig: "21",
914 max: "1.18973149535723176502126385303097021e+4932",
915 norm_max: "1.18973149535723176502126385303097021e+4932",
916 min: "3.36210314311209350626267781732175260e-4932",
917 epsilon: "1.08420217248550443400745280086994171e-19",
918 denorm_min: "3.64519953188247460252840593361941982e-4951",
919 is_iec_60559: "1",
920};
921
922const QUAD: Characteristics = Characteristics {
925 mant_dig: "113",
926 dig: "33",
927 min_exp: "(-16381)",
928 min_10_exp: "(-4931)",
929 max_exp: "16384",
930 max_10_exp: "4932",
931 decimal_dig: "36",
932 max: "1.18973149535723176508575932662800702e+4932",
933 norm_max: "1.18973149535723176508575932662800702e+4932",
934 min: "3.36210314311209350626267781732175260e-4932",
935 epsilon: "1.92592994438723585305597794258492732e-34",
936 denorm_min: "6.47517511943802511092443895822764655e-4966",
937 is_iec_60559: "1",
938};
939
940const DOUBLE_DOUBLE: Characteristics = Characteristics {
956 mant_dig: "106",
957 dig: "31",
958 min_exp: "(-968)",
959 min_10_exp: "(-291)",
960 max_exp: "1024",
961 max_10_exp: "308",
962 decimal_dig: "33",
963 max: "1.79769313486231580793728971405301e+308",
964 norm_max: "8.98846567431157953864652595394501e+307",
965 min: "2.00416836000897277799610805135016e-292",
966 epsilon: "4.94065645841246544176568792868221e-324",
967 denorm_min: "4.94065645841246544176568792868221e-324",
968 is_iec_60559: "0",
969};
970
971const fn characteristics(format: Format) -> &'static Characteristics {
974 match format {
975 Format::Half => &HALF,
976 Format::BFloat16 => &BFLOAT16,
977 Format::Single => &SINGLE,
978 Format::Double => &DOUBLE,
979 Format::X87Extended => &X87,
980 Format::Quad => &QUAD,
981 Format::DoubleDouble => &DOUBLE_DOUBLE,
982 }
983}
984
985fn floats(d: &mut Defs, target: &TargetInfo) {
996 d.set("__FLT_RADIX__", "2");
997 d.set("__GCC_IEC_559", "2");
1002 d.set("__FLT_EVAL_METHOD__", "0");
1007 d.set("__FLT_EVAL_METHOD_C99__", "0");
1008 d.set("__FLT_EVAL_METHOD_TS_18661_3__", "0");
1009
1010 family(d, "FLT", &SINGLE, |value| format!("{value}F"));
1011 family(d, "DBL", &DOUBLE, |value| format!("((double){value}L)"));
1014 family(d, "LDBL", characteristics(target.long_double_format), |value| format!("{value}L"));
1015
1016 family(d, "FLT16", &HALF, |value| format!("{value}F16"));
1017 family(d, "FLT32", &SINGLE, |value| format!("{value}F32"));
1018 family(d, "FLT64", &DOUBLE, |value| format!("{value}F64"));
1019 family(d, "FLT128", &QUAD, |value| format!("{value}F128"));
1020 family(d, "FLT32X", &DOUBLE, |value| format!("{value}F32x"));
1021 if let Some(format) = target.float64x_format {
1025 family(d, "FLT64X", characteristics(format), |value| format!("{value}F64x"));
1026 }
1027
1028 d.set("__DECIMAL_DIG__", characteristics(target.long_double_format).decimal_dig);
1033}
1034
1035fn family(d: &mut Defs, prefix: &str, c: &Characteristics, write: impl Fn(&str) -> String) {
1040 d.set(&format!("__{prefix}_MANT_DIG__"), c.mant_dig);
1041 d.set(&format!("__{prefix}_DIG__"), c.dig);
1042 d.set(&format!("__{prefix}_MIN_EXP__"), c.min_exp);
1043 d.set(&format!("__{prefix}_MIN_10_EXP__"), c.min_10_exp);
1044 d.set(&format!("__{prefix}_MAX_EXP__"), c.max_exp);
1045 d.set(&format!("__{prefix}_MAX_10_EXP__"), c.max_10_exp);
1046 d.set(&format!("__{prefix}_DECIMAL_DIG__"), c.decimal_dig);
1047 d.set(&format!("__{prefix}_MAX__"), &write(c.max));
1048 d.set(&format!("__{prefix}_NORM_MAX__"), &write(c.norm_max));
1049 d.set(&format!("__{prefix}_MIN__"), &write(c.min));
1050 d.set(&format!("__{prefix}_EPSILON__"), &write(c.epsilon));
1051 d.set(&format!("__{prefix}_DENORM_MIN__"), &write(c.denorm_min));
1052 d.set(&format!("__{prefix}_IS_IEC_60559__"), c.is_iec_60559);
1053 d.set(&format!("__{prefix}_HAS_DENORM__"), "1");
1054 d.set(&format!("__{prefix}_HAS_INFINITY__"), "1");
1055 d.set(&format!("__{prefix}_HAS_QUIET_NAN__"), "1");
1056}
1057
1058#[cfg(test)]
1059mod tests {
1060 use rucc_target::Triple;
1061
1062 use super::*;
1063
1064 fn set_for(triple: &str) -> String {
1065 let triple: Triple = triple.parse().expect("a triple the compiler supports");
1066 built_in(&TargetInfo::new(triple), &Predef::new())
1067 }
1068
1069 fn has(text: &str, line: &str) -> bool {
1070 text.lines().any(|l| l == line)
1071 }
1072
1073 #[test]
1074 fn the_set_is_driven_by_the_target_rather_than_by_the_host() {
1075 let x86 = set_for("x86_64-unknown-linux-gnu");
1076 let arm = set_for("aarch64-unknown-linux-gnu");
1077 assert!(has(&x86, "#define __x86_64__ 1"));
1078 assert!(!has(&x86, "#define __aarch64__ 1"));
1079 assert!(has(&arm, "#define __aarch64__ 1"));
1080 assert!(!has(&arm, "#define __x86_64__ 1"));
1081 assert!(has(&x86, "#define __linux__ 1") && has(&arm, "#define __linux__ 1"));
1082 }
1083
1084 #[test]
1085 fn windows_is_the_target_that_makes_long_thirty_two_bits() {
1086 let windows = set_for("x86_64-pc-windows-msvc");
1087 let linux = set_for("x86_64-unknown-linux-gnu");
1088 assert!(has(&windows, "#define __SIZEOF_LONG__ 4"));
1089 assert!(has(&windows, "#define __SIZE_TYPE__ long long unsigned int"));
1090 assert!(has(&windows, "#define __INT64_TYPE__ long long int"));
1091 assert!(!has(&windows, "#define __LP64__ 1"));
1092 assert!(has(&linux, "#define __SIZEOF_LONG__ 8"));
1093 assert!(has(&linux, "#define __SIZE_TYPE__ long unsigned int"));
1094 assert!(has(&linux, "#define __INT64_TYPE__ long int"));
1095 assert!(has(&linux, "#define __LP64__ 1"));
1096 }
1097
1098 #[test]
1099 fn wchar_t_is_the_type_that_divides_the_targets() {
1100 assert!(has(&set_for("x86_64-unknown-linux-gnu"), "#define __WCHAR_TYPE__ int"));
1102 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __WCHAR_TYPE__ unsigned int"));
1103 let windows = set_for("x86_64-pc-windows-msvc");
1104 assert!(has(&windows, "#define __WCHAR_TYPE__ short unsigned int"));
1105 assert!(has(&windows, "#define __SIZEOF_WCHAR_T__ 2"));
1106 }
1107
1108 #[test]
1109 fn apple_spells_the_architecture_its_own_way_and_its_headers_only_know_that_spelling() {
1110 let darwin = set_for("aarch64-apple-darwin");
1113 assert!(has(&darwin, "#define __arm64__ 1"));
1114 assert!(has(&darwin, "#define __arm64 1"));
1115 assert!(has(&darwin, "#define __aarch64__ 1"), "the portable spelling stays too");
1116 let linux = set_for("aarch64-unknown-linux-gnu");
1117 assert!(!has(&linux, "#define __arm64__ 1"), "Apple's spelling is Apple's alone");
1118 assert!(!has(&set_for("x86_64-apple-darwin"), "#define __arm64__ 1"));
1119 }
1120
1121 #[test]
1122 fn every_limit_is_spelled_in_hexadecimal_the_way_gcc_spells_it() {
1123 let linux = set_for("x86_64-unknown-linux-gnu");
1130 for line in [
1131 "#define __SCHAR_MAX__ 0x7f",
1132 "#define __SHRT_MAX__ 0x7fff",
1133 "#define __INT_MAX__ 0x7fffffff",
1134 "#define __LONG_MAX__ 0x7fffffffffffffffL",
1135 "#define __LONG_LONG_MAX__ 0x7fffffffffffffffLL",
1136 "#define __INTMAX_MAX__ 0x7fffffffffffffffL",
1137 "#define __UINTMAX_MAX__ 0xffffffffffffffffUL",
1138 "#define __SIZE_MAX__ 0xffffffffffffffffUL",
1139 "#define __PTRDIFF_MAX__ 0x7fffffffffffffffL",
1140 "#define __SIG_ATOMIC_MAX__ 0x7fffffff",
1141 "#define __INT8_MAX__ 0x7f",
1142 "#define __UINT8_MAX__ 0xff",
1143 "#define __INT16_MAX__ 0x7fff",
1144 "#define __UINT16_MAX__ 0xffff",
1145 "#define __INT32_MAX__ 0x7fffffff",
1146 "#define __UINT32_MAX__ 0xffffffffU",
1147 "#define __INT64_MAX__ 0x7fffffffffffffffL",
1148 "#define __UINT64_MAX__ 0xffffffffffffffffUL",
1149 "#define __INT_FAST8_MAX__ 0x7f",
1150 "#define __UINT_FAST8_MAX__ 0xff",
1151 ] {
1152 assert!(has(&linux, line), "{line}");
1153 }
1154 let windows = set_for("x86_64-pc-windows-msvc");
1157 assert!(has(&windows, "#define __LONG_MAX__ 0x7fffffffL"));
1158 assert!(has(&windows, "#define __INTMAX_MAX__ 0x7fffffffffffffffLL"));
1159 assert!(has(&windows, "#define __UINTMAX_MAX__ 0xffffffffffffffffULL"));
1160 }
1161
1162 #[test]
1163 fn wint_t_does_not_follow_wchar_t() {
1164 let darwin = set_for("aarch64-apple-darwin");
1166 assert!(has(&darwin, "#define __WINT_TYPE__ int"));
1167 assert!(has(&darwin, "#define __WINT_MAX__ 0x7fffffff"));
1168 assert!(has(&darwin, "#define __WCHAR_TYPE__ int"));
1169 let linux = set_for("aarch64-unknown-linux-gnu");
1170 assert!(has(&linux, "#define __WINT_TYPE__ unsigned int"));
1171 assert!(has(&linux, "#define __WINT_MAX__ 0xffffffffU"));
1172 assert!(has(&linux, "#define __WCHAR_TYPE__ unsigned int"), "and wchar_t is its own");
1173 assert!(has(
1174 &set_for("x86_64-pc-windows-msvc"),
1175 "#define __WINT_TYPE__ short unsigned int"
1176 ));
1177 }
1178
1179 #[test]
1180 fn the_widths_say_what_the_type_holds_and_follow_the_target_that_changes_it() {
1181 let linux = set_for("x86_64-unknown-linux-gnu");
1185 assert_eq!(linux.lines().filter(|line| line.contains("_WIDTH__")).count(), 20);
1186 assert!(has(&linux, "#define __LONG_WIDTH__ 64"));
1187 assert!(has(&linux, "#define __SIZE_WIDTH__ 64"));
1188 assert!(has(&linux, "#define __WCHAR_WIDTH__ 32"));
1189 assert!(has(&linux, "#define __INT_LEAST16_WIDTH__ 16"));
1190 assert!(has(&linux, "#define __INT_FAST16_WIDTH__ 64"));
1193 assert!(has(&set_for("x86_64-unknown-linux-musl"), "#define __INT_FAST16_WIDTH__ 32"));
1194 let windows = set_for("x86_64-pc-windows-msvc");
1197 assert!(has(&windows, "#define __LONG_WIDTH__ 32"));
1198 assert!(has(&windows, "#define __WINT_WIDTH__ 16"));
1199 assert!(has(&windows, "#define __SIZE_WIDTH__ 64"));
1200 assert!(has(&windows, "#define __INTMAX_WIDTH__ 64"));
1201 }
1202
1203 #[test]
1204 fn a_constant_maker_gets_the_suffix_its_width_needs_and_no_other() {
1205 let linux = set_for("x86_64-unknown-linux-gnu");
1209 assert!(has(&linux, "#define __INT32_C(c) c"));
1210 assert!(has(&linux, "#define __UINT32_C(c) c ## U"));
1211 assert!(has(&linux, "#define __INT16_C(c) c"));
1212 assert!(has(&linux, "#define __UINT16_C(c) c"));
1215 assert!(has(&linux, "#define __UINT8_C(c) c"));
1216 assert!(has(&linux, "#define __INT64_C(c) c ## L"));
1218 assert!(has(&linux, "#define __UINT64_C(c) c ## UL"));
1219 let windows = set_for("x86_64-pc-windows-msvc");
1221 assert!(has(&windows, "#define __INT64_C(c) c ## LL"));
1222 assert!(has(&windows, "#define __UINT64_C(c) c ## ULL"));
1223 }
1224
1225 #[test]
1226 fn the_symbol_prefix_is_defined_everywhere_including_where_it_is_empty() {
1227 for triple in
1232 ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
1233 {
1234 assert!(has(&set_for(triple), "#define __USER_LABEL_PREFIX__ "), "{triple}");
1235 }
1236 assert!(has(&set_for("aarch64-apple-darwin"), "#define __USER_LABEL_PREFIX__ _"));
1238 }
1239
1240 #[test]
1244 fn the_toolchain_macros_gcc_defines_are_defined_with_gccs_values() {
1245 let linux = set_for("x86_64-unknown-linux-gnu");
1246 for line in [
1247 "#define __GNUC_EXECUTION_CHARSET_NAME \"UTF-8\"",
1248 "#define __GNUC_WIDE_EXECUTION_CHARSET_NAME \"UTF-32LE\"",
1249 "#define __GXX_ABI_VERSION 1021",
1250 "#define __REGISTER_PREFIX__ ",
1251 "#define __FINITE_MATH_ONLY__ 0",
1252 "#define __GCC_IEC_559 2",
1253 "#define __GCC_CONSTRUCTIVE_SIZE 64",
1254 "#define __GCC_DESTRUCTIVE_SIZE 64",
1255 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1",
1256 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1",
1257 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1",
1258 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1",
1259 "#define __ATOMIC_HLE_ACQUIRE 65536",
1260 "#define __ATOMIC_HLE_RELEASE 131072",
1261 "#define __FXSR__ 1",
1262 "#define __MMX_WITH_SSE__ 1",
1263 "#define __code_model_small__ 1",
1264 ] {
1265 assert!(has(&linux, line), "{line}");
1266 }
1267 assert!(!linux.contains("__GCC_IEC_559_COMPLEX"));
1270 let arm = set_for("aarch64-unknown-linux-gnu");
1272 for name in ["__ATOMIC_HLE_ACQUIRE", "__FXSR__", "__MMX_WITH_SSE__", "__code_model_small__"]
1273 {
1274 assert!(!arm.contains(name), "{name}");
1275 }
1276 assert!(has(&arm, "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1"));
1277 let windows = set_for("x86_64-pc-windows-msvc");
1279 assert!(has(&windows, "#define __GNUC_WIDE_EXECUTION_CHARSET_NAME \"UTF-16LE\""));
1280 }
1281
1282 #[test]
1283 fn the_memory_orders_are_there_even_without_atomics() {
1284 let linux = set_for("x86_64-unknown-linux-gnu");
1289 assert!(has(&linux, "#define __ATOMIC_RELAXED 0"));
1290 assert!(has(&linux, "#define __ATOMIC_SEQ_CST 5"));
1291 assert!(has(&linux, "#define __STDC_NO_ATOMICS__ 1"), "and we still have no _Atomic");
1292 assert!(has(&linux, "#define __GCC_ATOMIC_INT_LOCK_FREE 2"));
1293 assert!(has(&linux, "#define __GCC_ATOMIC_LLONG_LOCK_FREE 2"));
1294 assert!(has(&set_for("x86_64-pc-windows-msvc"), "#define __GCC_ATOMIC_LLONG_LOCK_FREE 2"));
1295 }
1296
1297 #[test]
1298 fn long_double_is_three_types_and_the_macros_say_which() {
1299 assert!(has(&set_for("x86_64-unknown-linux-gnu"), "#define __LDBL_MANT_DIG__ 64"));
1300 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __LDBL_MANT_DIG__ 113"));
1301 assert!(has(&set_for("aarch64-apple-darwin"), "#define __LDBL_MANT_DIG__ 53"));
1302 }
1303
1304 #[test]
1305 fn the_extended_floating_types_have_the_limits_their_formats_have() {
1306 let linux = set_for("x86_64-unknown-linux-gnu");
1309 assert!(has(&linux, "#define __FLT16_MANT_DIG__ 11"));
1310 assert!(has(&linux, "#define __FLT32_MANT_DIG__ 24"));
1311 assert!(has(&linux, "#define __FLT64_MANT_DIG__ 53"));
1312 assert!(has(&linux, "#define __FLT128_MANT_DIG__ 113"));
1313 assert!(has(&linux, "#define __FLT32X_MANT_DIG__ 53"));
1314 assert!(has(&linux, "#define __FLT16_MAX__ 6.55040000000000000000000000000000000e+4F16"));
1317 assert!(has(
1318 &linux,
1319 "#define __FLT32X_MIN__ 2.22507385850720138309023271733240406e-308F32x"
1320 ));
1321 assert!(!linux.contains("__FLT128X_"));
1324 }
1325
1326 #[test]
1327 fn float64x_keeps_the_width_that_long_double_loses_on_apple() {
1328 let linux = set_for("x86_64-unknown-linux-gnu");
1331 assert!(has(&linux, "#define __FLT64X_MANT_DIG__ 64"));
1332 assert!(has(&linux, "#define __LDBL_MANT_DIG__ 64"));
1333 let mac = set_for("aarch64-apple-darwin");
1334 assert!(has(&mac, "#define __FLT64X_MANT_DIG__ 113"));
1335 assert!(has(&mac, "#define __LDBL_MANT_DIG__ 53"));
1336 let windows = set_for("x86_64-pc-windows-msvc");
1337 assert!(has(&windows, "#define __FLT64X_MANT_DIG__ 64"));
1338 assert!(has(&windows, "#define __LDBL_MANT_DIG__ 53"));
1339 }
1340
1341 #[test]
1342 fn the_largest_value_of_an_ieee_format_is_also_its_largest_normal_one() {
1343 let linux = set_for("x86_64-unknown-linux-gnu");
1347 for prefix in ["FLT", "DBL", "LDBL", "FLT16", "FLT32", "FLT64", "FLT128", "FLT32X"] {
1348 let value = |suffix: &str| {
1349 let name = format!("#define __{prefix}_{suffix}__ ");
1350 let line = linux
1351 .lines()
1352 .find(|line| line.starts_with(&name))
1353 .unwrap_or_else(|| panic!("__{prefix}_{suffix}__ is defined"));
1354 line[name.len()..].to_owned()
1355 };
1356 assert_eq!(value("MAX"), value("NORM_MAX"), "__{prefix}_NORM_MAX__");
1357 }
1358 }
1359
1360 #[test]
1361 fn the_double_double_is_the_row_where_the_largest_value_is_not_the_largest_normal_one() {
1362 let c = characteristics(Format::DoubleDouble);
1367 assert_ne!(c.max, c.norm_max);
1368 assert!(c.norm_max.starts_with("8.98846567431157953864652595394501e+307"));
1372 assert!(c.max.starts_with("1.7976931348623158"));
1373 assert_eq!(c.epsilon, c.denorm_min);
1377 for format in [Format::Half, Format::Single, Format::Double, Format::X87Extended] {
1378 assert_ne!(characteristics(format).epsilon, characteristics(format).denorm_min);
1379 }
1380 assert_eq!(c.is_iec_60559, "0");
1382 }
1383
1384 #[test]
1385 fn the_widest_bit_int_is_said_in_every_dialect() {
1386 let mut opts = Predef::new();
1390 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1391 assert!(has(&built_in(&target, &opts), "#define __BITINT_MAXWIDTH__ 128"));
1392 opts.std = Std::C17;
1393 assert!(has(&built_in(&target, &opts), "#define __BITINT_MAXWIDTH__ 128"));
1394 }
1395
1396 #[test]
1397 fn char_signedness_is_recorded_only_when_it_is_unsigned() {
1398 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __CHAR_UNSIGNED__ 1"));
1400 assert!(!has(&set_for("x86_64-unknown-linux-gnu"), "#define __CHAR_UNSIGNED__ 1"));
1401 }
1402
1403 #[test]
1404 fn the_dialect_decides_the_standard_macros() {
1405 let mut opts = Predef::new();
1406 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1407 assert!(has(&built_in(&target, &opts), "#define __STDC_VERSION__ 202311L"));
1408 assert!(!has(&built_in(&target, &opts), "#define __STRICT_ANSI__ 1"));
1409 assert!(has(&built_in(&target, &opts), "#define linux 1"));
1410
1411 opts.gnu_extensions = false;
1412 assert!(has(&built_in(&target, &opts), "#define __STRICT_ANSI__ 1"));
1413 assert!(!has(&built_in(&target, &opts), "#define linux 1"), "not a reserved name");
1414
1415 opts.std = Std::C89;
1416 let c89 = built_in(&target, &opts);
1417 assert!(!c89.contains("__STDC_VERSION__"), "C89 does not define it at all");
1418 assert!(has(&c89, "#define __STDC__ 1"));
1419 }
1420
1421 #[test]
1424 fn the_only_things_claimed_missing_are_the_ones_that_are_missing() {
1425 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1426 let opts = Predef::new();
1427 let set = built_in(&target, &opts);
1428 assert!(has(&set, "#define __STDC_NO_ATOMICS__ 1"), "there is no stdatomic.h to include");
1429 assert!(has(&set, "#define __STDC_NO_THREADS__ 1"), "nor a threads.h");
1430 assert!(has(&set, "#define __STDC_NO_COMPLEX__ 1"), "the arithmetic is not lowered");
1431 assert!(!set.contains("__STDC_NO_VLA__"), "variable length arrays work");
1432 }
1433
1434 #[test]
1438 fn the_type_behind_char8_t_is_defined_in_c23_and_in_no_dialect_before_it() {
1439 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1440 let mut opts = Predef::new();
1441 assert!(has(&built_in(&target, &opts), "#define __CHAR8_TYPE__ unsigned char"));
1442
1443 for older in [Std::C17, Std::C11, Std::C99, Std::C89] {
1444 opts.std = older;
1445 assert!(!built_in(&target, &opts).contains("__CHAR8_TYPE__"), "{older:?}");
1446 }
1447 }
1448
1449 #[test]
1450 fn the_optimizer_level_is_visible_to_the_preprocessor() {
1451 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1452 let mut opts = Predef::new();
1453 assert!(has(&built_in(&target, &opts), "#define __NO_INLINE__ 1"));
1454 assert!(!built_in(&target, &opts).contains("__OPTIMIZE__"));
1455
1456 opts.opt_level = OptLevel::O2;
1457 assert!(has(&built_in(&target, &opts), "#define __OPTIMIZE__ 1"));
1458 assert!(!built_in(&target, &opts).contains("__OPTIMIZE_SIZE__"));
1459
1460 opts.opt_level = OptLevel::Os;
1461 assert!(has(&built_in(&target, &opts), "#define __OPTIMIZE_SIZE__ 1"));
1462 }
1463
1464 #[test]
1465 fn a_command_line_define_with_no_value_is_one() {
1466 let mut opts = Predef::new();
1467 opts.defines = vec!["FOO".to_owned(), "BAR=2".to_owned(), "F(x)=x + 1".to_owned()];
1468 opts.undefines = vec!["__linux__".to_owned()];
1469 let text = command_line(&opts);
1470 assert!(has(&text, "#define FOO 1"));
1471 assert!(has(&text, "#define BAR 2"));
1472 assert!(has(&text, "#define F(x) x + 1"));
1473 assert!(text.trim_end().ends_with("#undef __linux__"));
1475 }
1476
1477 #[test]
1478 fn no_command_line_macros_is_no_file_at_all() {
1479 assert!(command_line(&Predef::new()).is_empty());
1480 }
1481
1482 #[test]
1483 fn a_date_is_spelled_the_way_the_standard_fixes() {
1484 let epoch = Timestamp::from_unix(0);
1486 assert_eq!(epoch.date, "Jan 1 1970");
1487 assert_eq!(epoch.time, "00:00:00");
1488 let leap = Timestamp::from_unix(1_709_164_800);
1489 assert_eq!(leap.date, "Feb 29 2024", "2024 is a leap year");
1490 let late = Timestamp::from_unix(1_735_689_599);
1491 assert_eq!(late.date, "Dec 31 2024");
1492 assert_eq!(late.time, "23:59:59");
1493 }
1494
1495 #[test]
1496 fn a_date_before_the_epoch_still_comes_out_right() {
1497 assert_eq!(Timestamp::from_unix(-1).date, "Dec 31 1969");
1500 assert_eq!(Timestamp::from_unix(-1).time, "23:59:59");
1501 }
1502
1503 #[test]
1504 fn the_gnuc_version_is_a_knob_rather_than_a_constant() {
1505 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1506 let mut opts = Predef::new();
1507 assert!(has(&built_in(&target, &opts), "#define __GNUC__ 7"));
1508 opts.gnuc = GnucVersion { major: 15, minor: 1, patch: 0 };
1509 assert!(has(&built_in(&target, &opts), "#define __GNUC__ 15"));
1510 assert!(has(&built_in(&target, &opts), "#define __GNUC_MINOR__ 1"));
1511 }
1512
1513 #[test]
1520 fn one_of_the_two_inline_macros_is_defined_and_three_things_can_pick_which() {
1521 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1522 let gnu = "#define __GNUC_GNU_INLINE__ 1";
1523 let stdc = "#define __GNUC_STDC_INLINE__ 1";
1524
1525 let mut opts = Predef::new();
1526 assert!(has(&built_in(&target, &opts), stdc));
1527 assert!(!has(&built_in(&target, &opts), gnu));
1528
1529 opts.gnu89_inline = true;
1530 assert!(has(&built_in(&target, &opts), gnu));
1531 assert!(!has(&built_in(&target, &opts), stdc));
1532
1533 let mut opts = Predef::new();
1535 opts.std = Std::C89;
1536 assert!(has(&built_in(&target, &opts), gnu));
1537 assert!(!has(&built_in(&target, &opts), stdc));
1538 }
1539
1540 #[test]
1541 fn musl_and_glibc_disagree_about_the_fast_types_on_the_same_processor() {
1542 let gnu = set_for("x86_64-unknown-linux-gnu");
1547 let musl = set_for("x86_64-unknown-linux-musl");
1548 assert!(has(&gnu, "#define __INT_FAST16_TYPE__ long int"));
1549 assert!(has(&gnu, "#define __INT_FAST32_TYPE__ long int"));
1550 assert!(has(&gnu, "#define __UINT_FAST16_TYPE__ long unsigned int"));
1551 assert!(has(&musl, "#define __INT_FAST16_TYPE__ int"));
1552 assert!(has(&musl, "#define __INT_FAST32_TYPE__ int"));
1553 assert!(has(&musl, "#define __UINT_FAST16_TYPE__ unsigned int"));
1554 assert!(has(&gnu, "#define __INT_FAST16_MAX__ 0x7fffffffffffffffL"));
1557 assert!(has(&musl, "#define __INT_FAST16_MAX__ 0x7fffffff"));
1558 assert!(has(&musl, "#define __UINT_FAST16_MAX__ 0xffffffffU"));
1559 }
1560
1561 #[test]
1562 fn the_libc_only_moves_the_two_fast_types_it_is_allowed_to_move() {
1563 let gnu = set_for("x86_64-unknown-linux-gnu");
1566 let musl = set_for("x86_64-unknown-linux-musl");
1567 for line in [
1568 "#define __INT_FAST8_TYPE__ signed char",
1569 "#define __INT_FAST64_TYPE__ long int",
1570 "#define __INT64_TYPE__ long int",
1571 "#define __SIZE_TYPE__ long unsigned int",
1572 "#define __SIZEOF_LONG__ 8",
1573 "#define __LP64__ 1",
1574 ] {
1575 assert!(has(&gnu, line), "glibc lost {line}");
1576 assert!(has(&musl, line), "musl lost {line}");
1577 }
1578 }
1579
1580 #[test]
1581 fn a_non_x86_target_has_int_sized_fast_types_whatever_the_libc() {
1582 let arm_gnu = set_for("aarch64-unknown-linux-gnu");
1585 let arm_musl = set_for("aarch64-unknown-linux-musl");
1586 assert!(has(&arm_gnu, "#define __INT_FAST16_TYPE__ int"));
1587 assert!(has(&arm_musl, "#define __INT_FAST16_TYPE__ int"));
1588 }
1589}