1use rucc_base::float::Format;
23use rucc_session::{GnucVersion, OptLevel, Options, Std};
24use rucc_target::{Arch, Env, Os, TargetInfo};
25
26pub const BUILT_IN: &str = "<built-in>";
28
29pub const COMMAND_LINE: &str = "<command-line>";
31
32#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Timestamp {
39 pub date: String,
41 pub time: String,
43}
44
45impl Timestamp {
46 pub fn now() -> Timestamp {
51 let seconds = match std::env::var("SOURCE_DATE_EPOCH").ok().and_then(|v| v.parse().ok()) {
52 Some(fixed) => fixed,
53 None => std::time::SystemTime::now()
54 .duration_since(std::time::UNIX_EPOCH)
55 .map_or(0, |d| d.as_secs() as i64),
56 };
57 Timestamp::from_unix(seconds)
58 }
59
60 pub fn from_unix(seconds: i64) -> Timestamp {
65 let days = seconds.div_euclid(86_400);
66 let rest = seconds.rem_euclid(86_400);
67 let (year, month, day) = civil_from_days(days);
68 const MONTHS: [&str; 12] =
69 ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
70 let name = MONTHS[(month - 1) as usize];
71 Timestamp {
72 date: format!("{name} {day:2} {year}"),
73 time: format!("{:02}:{:02}:{:02}", rest / 3600, (rest / 60) % 60, rest % 60),
74 }
75 }
76}
77
78fn civil_from_days(days: i64) -> (i64, u32, u32) {
84 let shifted = days + 719_468;
87 let era = shifted.div_euclid(146_097);
88 let day_of_era = shifted.rem_euclid(146_097);
89 let year_of_era =
90 (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
91 let year = year_of_era + era * 400;
92 let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
93 let marched = (5 * day_of_year + 2) / 153;
94 let day = (day_of_year - (153 * marched + 2) / 5 + 1) as u32;
95 let month = if marched < 10 { marched + 3 } else { marched - 9 } as u32;
96 (year + i64::from(month <= 2), month, day)
97}
98
99#[derive(Debug, Clone, PartialEq, Eq)]
101pub struct Predef {
102 pub std: Std,
104 pub gnu_extensions: bool,
107 pub gnuc: GnucVersion,
109 pub opt_level: OptLevel,
111 pub hosted: bool,
113 pub timestamp: Timestamp,
115 pub defines: Vec<String>,
117 pub undefines: Vec<String>,
119}
120
121impl Predef {
122 pub fn new() -> Predef {
124 Predef {
125 std: Std::default(),
126 gnu_extensions: true,
127 gnuc: GnucVersion::default(),
128 opt_level: OptLevel::O0,
129 hosted: true,
130 timestamp: Timestamp::now(),
131 defines: Vec::new(),
132 undefines: Vec::new(),
133 }
134 }
135}
136
137impl Predef {
138 pub fn for_options(opts: &Options) -> Predef {
144 Predef {
145 std: opts.std,
146 gnu_extensions: opts.gnu_extensions,
147 gnuc: opts.gnuc,
148 opt_level: opts.opt_level,
149 hosted: opts.hosted,
150 timestamp: Timestamp::now(),
151 defines: opts.defines.clone(),
152 undefines: opts.undefines.clone(),
153 }
154 }
155}
156
157impl Default for Predef {
158 fn default() -> Predef {
159 Predef::new()
160 }
161}
162
163struct Defs {
165 text: String,
166}
167
168impl Defs {
169 fn new() -> Defs {
170 Defs { text: String::new() }
171 }
172
173 fn set(&mut self, name: &str, value: &str) {
175 self.text.push_str("#define ");
176 self.text.push_str(name);
177 self.text.push(' ');
178 self.text.push_str(value);
179 self.text.push('\n');
180 }
181
182 fn flag(&mut self, name: &str) {
184 self.set(name, "1");
185 }
186
187 fn set_if(&mut self, when: bool, name: &str, value: &str) {
188 if when {
189 self.set(name, value);
190 }
191 }
192
193 fn flag_if(&mut self, when: bool, name: &str) {
194 if when {
195 self.flag(name);
196 }
197 }
198}
199
200pub(crate) fn built_in(target: &TargetInfo, opts: &Predef) -> String {
202 let mut d = Defs::new();
203 identity(&mut d, target, opts);
204 d.set("__DATE__", &format!("\"{}\"", opts.timestamp.date));
208 d.set("__TIME__", &format!("\"{}\"", opts.timestamp.time));
209 dialect(&mut d, opts);
210 optimization(&mut d, opts);
211 platform(&mut d, target, opts);
212 sizes(&mut d, target);
213 integers(&mut d, target);
214 floats(&mut d, target);
215 atomics(&mut d, target);
216 d.text
217}
218
219pub(crate) fn command_line(opts: &Predef) -> String {
225 let mut d = Defs::new();
226 for define in &opts.defines {
227 match define.split_once('=') {
228 Some((name, value)) => d.set(name, value),
229 None => d.flag(define),
232 }
233 }
234 for name in &opts.undefines {
235 d.text.push_str("#undef ");
236 d.text.push_str(name);
237 d.text.push('\n');
238 }
239 d.text
240}
241
242fn identity(d: &mut Defs, target: &TargetInfo, opts: &Predef) {
244 d.flag("__rucc__");
245 d.set("__rucc_version__", "\"0.1.0\"");
246 d.set("__rucc_major__", "0");
247 d.set("__rucc_minor__", "1");
248 d.set("__rucc_patchlevel__", "0");
249 d.set("__GNUC__", &opts.gnuc.major.to_string());
251 d.set("__GNUC_MINOR__", &opts.gnuc.minor.to_string());
252 d.set("__GNUC_PATCHLEVEL__", &opts.gnuc.patch.to_string());
253 d.set("__VERSION__", "\"rucc 0.1.0\"");
254 d.flag_if(opts.std == Std::C89, "__GNUC_GNU_INLINE__");
263 d.flag_if(opts.std != Std::C89, "__GNUC_STDC_INLINE__");
264 d.set("__GNUC_EXECUTION_CHARSET_NAME", "\"UTF-8\"");
269 let wide = if target.wchar_width == 16 { "\"UTF-16LE\"" } else { "\"UTF-32LE\"" };
270 d.set("__GNUC_WIDE_EXECUTION_CHARSET_NAME", wide);
271 d.set("__GXX_ABI_VERSION", "1021");
276}
277
278fn dialect(d: &mut Defs, opts: &Predef) {
280 d.flag("__STDC__");
281 d.set_if(opts.hosted, "__STDC_HOSTED__", "1");
282 d.set_if(!opts.hosted, "__STDC_HOSTED__", "0");
283 if let Some(version) = opts.std.stdc_version() {
284 d.set("__STDC_VERSION__", version);
285 }
286 d.flag_if(!opts.gnu_extensions, "__STRICT_ANSI__");
289 d.flag("__STDC_UTF_16__");
290 d.flag("__STDC_UTF_32__");
291 d.flag("__STDC_IEC_559__");
292 d.flag("__STDC_IEC_559_COMPLEX__");
293 d.set_if(opts.std == Std::C23, "__STDC_IEC_60559_BFP__", "202311L");
294 d.set("__STDC_ISO_10646__", "201706L");
295 d.set_if(opts.std == Std::C23, "__CHAR8_TYPE__", "unsigned char");
301 if opts.std.has_c11() {
313 d.flag("__STDC_NO_ATOMICS__");
314 d.flag("__STDC_NO_THREADS__");
315 d.flag("__STDC_NO_COMPLEX__");
316 }
317 d.set("__STDC_EMBED_NOT_FOUND__", "0");
322 d.set("__STDC_EMBED_FOUND__", "1");
323 d.set("__STDC_EMBED_EMPTY__", "2");
324}
325
326fn atomics(d: &mut Defs, target: &TargetInfo) {
338 d.set("__ATOMIC_RELAXED", "0");
339 d.set("__ATOMIC_CONSUME", "1");
340 d.set("__ATOMIC_ACQUIRE", "2");
341 d.set("__ATOMIC_RELEASE", "3");
342 d.set("__ATOMIC_ACQ_REL", "4");
343 d.set("__ATOMIC_SEQ_CST", "5");
344 let llong = if target.pointer_width == 64 { "2" } else { "1" };
347 for name in [
348 "BOOL", "CHAR", "CHAR8_T", "CHAR16_T", "CHAR32_T", "WCHAR_T", "SHORT", "INT", "LONG",
349 "POINTER",
350 ] {
351 d.set(&format!("__GCC_ATOMIC_{name}_LOCK_FREE"), "2");
352 }
353 d.set("__GCC_ATOMIC_LLONG_LOCK_FREE", llong);
357 d.set("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
358 for width in [1, 2, 4, 8] {
362 d.flag(&format!("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_{width}"));
363 }
364 if target.triple.arch == Arch::X86_64 {
369 d.set("__ATOMIC_HLE_ACQUIRE", "65536");
370 d.set("__ATOMIC_HLE_RELEASE", "131072");
371 }
372}
373
374fn optimization(d: &mut Defs, opts: &Predef) {
376 d.flag_if(opts.opt_level.runs_optimizer(), "__OPTIMIZE__");
377 d.flag_if(opts.opt_level.is_size(), "__OPTIMIZE_SIZE__");
378 d.flag_if(!opts.opt_level.runs_optimizer(), "__NO_INLINE__");
381 d.set("__FINITE_MATH_ONLY__", "0");
387}
388
389fn platform(d: &mut Defs, target: &TargetInfo, opts: &Predef) {
391 let triple = target.triple;
392 match triple.arch {
393 Arch::X86_64 => {
394 d.flag("__x86_64__");
395 d.flag("__x86_64");
396 d.flag("__amd64__");
397 d.flag("__amd64");
398 d.flag("__SSE__");
399 d.flag("__SSE2__");
400 d.flag("__MMX__");
401 d.flag("__SSE_MATH__");
402 d.flag("__SSE2_MATH__");
403 d.flag("__k8");
404 d.flag("__k8__");
405 d.flag("__FXSR__");
408 d.flag("__code_model_small__");
409 d.flag("__MMX_WITH_SSE__");
414 }
415 Arch::Aarch64 => {
416 d.flag("__aarch64__");
417 d.flag("__AARCH64EL__");
418 d.set("__ARM_ARCH", "8");
419 d.set("__ARM_ARCH_PROFILE", "'A'");
420 d.set("__ARM_64BIT_STATE", "1");
421 d.set("__ARM_ALIGN_MAX_PWR", "28");
422 d.set("__ARM_FP", "0xe");
423 d.set("__ARM_NEON", "1");
424 d.set("__ARM_FEATURE_UNALIGNED", "1");
425 d.set("__ARM_PCS_AAPCS64", "1");
426 }
427 Arch::Riscv64 => {
428 d.flag("__riscv");
429 d.set("__riscv_xlen", "64");
430 d.set("__riscv_flen", "64");
431 d.flag("__riscv_float_abi_double");
432 d.flag("__riscv_muldiv");
433 d.flag("__riscv_atomic");
434 d.flag("__riscv_compressed");
435 d.set("__riscv_cmodel_medlow", "1");
436 }
437 }
438 match triple.os {
439 Os::Linux => {
440 d.flag("__linux__");
441 d.flag("__linux");
442 d.flag("__unix__");
443 d.flag("__unix");
444 d.flag("__gnu_linux__");
445 d.flag("__ELF__");
446 if opts.gnu_extensions {
449 d.flag("linux");
450 d.flag("unix");
451 }
452 }
453 Os::Darwin => {
454 d.flag("__APPLE__");
455 d.flag("__MACH__");
456 d.flag("__unix__");
457 d.flag("__unix");
458 d.set("__APPLE_CC__", "6000");
459 d.set("__DYNAMIC__", "1");
460 if triple.arch == Arch::Aarch64 {
461 d.flag("__arm64__");
466 d.flag("__arm64");
467 }
468 if opts.gnu_extensions {
469 d.flag("unix");
470 }
471 }
472 Os::Windows => {
473 d.flag("_WIN32");
474 d.flag("__WIN32__");
475 d.flag("_WIN64");
476 d.flag("__WIN64__");
477 d.flag("__MINGW32__");
478 }
479 Os::None => {
480 d.flag("__ELF__");
483 }
484 }
485 match triple.env {
486 Env::Musl => d.flag("__musl__"),
487 Env::Gnu | Env::None | Env::Msvc => {}
488 }
489 if target.long_width == 64 && target.pointer_width == 64 {
492 d.flag("__LP64__");
493 d.flag("_LP64");
494 }
495 d.set("__USER_LABEL_PREFIX__", if triple.os == Os::Darwin { "_" } else { "" });
502 d.set("__REGISTER_PREFIX__", "");
506
507 if !matches!(triple.os, Os::Windows) {
510 d.set("__PIC__", "2");
511 d.set("__pic__", "2");
512 }
513}
514
515fn sizes(d: &mut Defs, target: &TargetInfo) {
517 let pointer = target.pointer_width / 8;
518 d.set("__GCC_CONSTRUCTIVE_SIZE", "64");
523 d.set("__GCC_DESTRUCTIVE_SIZE", "64");
524 let long = target.long_width / 8;
525 let long_double = target.long_double_width / 8;
526 d.set("__CHAR_BIT__", "8");
527 d.set("__SIZEOF_SHORT__", "2");
528 d.set("__SIZEOF_INT__", "4");
529 d.set("__SIZEOF_LONG__", &long.to_string());
530 d.set("__SIZEOF_LONG_LONG__", "8");
531 d.set("__SIZEOF_INT128__", "16");
532 d.set("__SIZEOF_FLOAT__", "4");
533 d.set("__SIZEOF_DOUBLE__", "8");
534 d.set("__SIZEOF_LONG_DOUBLE__", &long_double.to_string());
535 d.set("__SIZEOF_POINTER__", &pointer.to_string());
536 d.set("__SIZEOF_SIZE_T__", &pointer.to_string());
537 d.set("__SIZEOF_PTRDIFF_T__", &pointer.to_string());
538 d.set("__SIZEOF_WCHAR_T__", &wchar(target).size.to_string());
539 d.set("__SIZEOF_WINT_T__", "4");
540 d.set("__BIGGEST_ALIGNMENT__", "16");
541 d.set("__ORDER_LITTLE_ENDIAN__", "1234");
545 d.set("__ORDER_BIG_ENDIAN__", "4321");
546 d.set("__ORDER_PDP_ENDIAN__", "3412");
547 let order =
548 if target.little_endian { "__ORDER_LITTLE_ENDIAN__" } else { "__ORDER_BIG_ENDIAN__" };
549 d.set("__BYTE_ORDER__", order);
550 d.set("__FLOAT_WORD_ORDER__", order);
551 d.flag_if(!target.char_is_signed, "__CHAR_UNSIGNED__");
552}
553
554struct Wchar {
556 spelling: &'static str,
558 size: u32,
560 max: &'static str,
562 min: &'static str,
564}
565
566fn wchar(target: &TargetInfo) -> Wchar {
576 match (target.wchar_width, target.wchar_is_signed) {
577 (16, false) => Wchar { spelling: "short unsigned int", size: 2, max: "0xffff", min: "0" },
578 (16, true) => Wchar { spelling: "short int", size: 2, max: "0x7fff", min: "(-32767 - 1)" },
579 (_, false) => Wchar { spelling: "unsigned int", size: 4, max: "0xffffffffU", min: "0U" },
580 (_, true) => {
581 Wchar { spelling: "int", size: 4, max: "0x7fffffff", min: "(-__WCHAR_MAX__ - 1)" }
582 }
583 }
584}
585
586struct Wint {
588 spelling: &'static str,
590 max: &'static str,
592 min: &'static str,
594 width: u32,
596}
597
598fn wint(target: &TargetInfo) -> Wint {
605 match target.triple.os {
606 Os::Windows => Wint { spelling: "short unsigned int", max: "0xffff", min: "0", width: 16 },
607 Os::Darwin => {
608 Wint { spelling: "int", max: "0x7fffffff", min: "(-__WINT_MAX__ - 1)", width: 32 }
609 }
610 _ => Wint { spelling: "unsigned int", max: "0xffffffffU", min: "0U", width: 32 },
611 }
612}
613
614fn integers(d: &mut Defs, target: &TargetInfo) {
616 let lp64 = target.long_width == 64;
620 let wide = if lp64 { "long int" } else { "long long int" };
621 let wide_unsigned = if lp64 { "long unsigned int" } else { "long long unsigned int" };
622 let wide_suffix = if lp64 { "L" } else { "LL" };
623 let wide_max = format!("0x7fffffffffffffff{wide_suffix}");
624 let wide_umax = format!("0xffffffffffffffffU{wide_suffix}");
625
626 d.set("__SCHAR_MAX__", "0x7f");
627 d.set("__SHRT_MAX__", "0x7fff");
628 d.set("__INT_MAX__", "0x7fffffff");
629 d.set("__LONG_MAX__", if lp64 { "0x7fffffffffffffffL" } else { "0x7fffffffL" });
630 d.set("__LONG_LONG_MAX__", "0x7fffffffffffffffLL");
631 d.set("__INTMAX_MAX__", &wide_max);
632 d.set("__UINTMAX_MAX__", &wide_umax);
633 d.set("__SIZE_MAX__", &wide_umax);
634 d.set("__PTRDIFF_MAX__", &wide_max);
635 d.set("__INTPTR_MAX__", &wide_max);
636 d.set("__UINTPTR_MAX__", &wide_umax);
637 d.set("__SIG_ATOMIC_MAX__", "0x7fffffff");
638 d.set("__SIG_ATOMIC_MIN__", "(-__SIG_ATOMIC_MAX__ - 1)");
639 d.set("__BITINT_MAXWIDTH__", "128");
646
647 let wchar = wchar(target);
648 d.set("__WCHAR_TYPE__", wchar.spelling);
649 d.set("__WCHAR_MAX__", wchar.max);
650 d.set("__WCHAR_MIN__", wchar.min);
651 let wint = wint(target);
652 d.set("__WINT_TYPE__", wint.spelling);
653 d.set("__WINT_MAX__", wint.max);
654 d.set("__WINT_MIN__", wint.min);
655 d.set("__SIZE_TYPE__", wide_unsigned);
656 d.set("__PTRDIFF_TYPE__", wide);
657 d.set("__INTMAX_TYPE__", wide);
658 d.set("__UINTMAX_TYPE__", wide_unsigned);
659 d.set("__INTPTR_TYPE__", wide);
660 d.set("__UINTPTR_TYPE__", wide_unsigned);
661 d.set("__SIG_ATOMIC_TYPE__", "int");
662 d.set("__CHAR16_TYPE__", "short unsigned int");
663 d.set("__CHAR32_TYPE__", "unsigned int");
664 d.set("__INTMAX_C(c)", &format!("c ## {wide_suffix}"));
665 d.set("__UINTMAX_C(c)", &format!("c ## U{wide_suffix}"));
666
667 exact(d, 8, "signed char", "unsigned char", "0x7f", "0xff", "");
669 exact(d, 16, "short int", "short unsigned int", "0x7fff", "0xffff", "");
670 exact(d, 32, "int", "unsigned int", "0x7fffffff", "0xffffffffU", "");
673 exact(d, 64, wide, wide_unsigned, &wide_max, &wide_umax, wide_suffix);
674
675 let fast_is_wide = target.triple.arch == Arch::X86_64 && lp64 && target.triple.env != Env::Musl;
686 let fast_middle = if fast_is_wide { wide } else { "int" };
687 d.set("__INT_FAST8_TYPE__", "signed char");
688 d.set("__UINT_FAST8_TYPE__", "unsigned char");
689 d.set("__INT_FAST8_MAX__", "0x7f");
690 d.set("__UINT_FAST8_MAX__", "0xff");
691 for width in [16, 32] {
692 let unsigned = if fast_middle == "int" { "unsigned int" } else { wide_unsigned };
693 let max = if fast_middle == "int" { "0x7fffffff" } else { wide_max.as_str() };
694 let umax = if fast_middle == "int" { "0xffffffffU" } else { wide_umax.as_str() };
695 d.set(&format!("__INT_FAST{width}_TYPE__"), fast_middle);
696 d.set(&format!("__UINT_FAST{width}_TYPE__"), unsigned);
697 d.set(&format!("__INT_FAST{width}_MAX__"), max);
698 d.set(&format!("__UINT_FAST{width}_MAX__"), umax);
699 }
700 d.set("__INT_FAST64_TYPE__", wide);
701 d.set("__UINT_FAST64_TYPE__", wide_unsigned);
702 d.set("__INT_FAST64_MAX__", &wide_max);
703 d.set("__UINT_FAST64_MAX__", &wide_umax);
704
705 widths(d, target, &wchar, &wint, if fast_is_wide { 64 } else { 32 });
706}
707
708fn widths(d: &mut Defs, target: &TargetInfo, wchar: &Wchar, wint: &Wint, fast_middle: u32) {
719 let pointer = target.pointer_width;
720 d.set("__SCHAR_WIDTH__", "8");
721 d.set("__SHRT_WIDTH__", "16");
722 d.set("__INT_WIDTH__", "32");
723 d.set("__LONG_WIDTH__", &target.long_width.to_string());
724 d.set("__LONG_LONG_WIDTH__", "64");
725 d.set("__INTMAX_WIDTH__", "64");
726 d.set("__INTPTR_WIDTH__", &pointer.to_string());
727 d.set("__PTRDIFF_WIDTH__", &pointer.to_string());
728 d.set("__SIZE_WIDTH__", &pointer.to_string());
729 d.set("__SIG_ATOMIC_WIDTH__", "32");
730 d.set("__WCHAR_WIDTH__", &(wchar.size * 8).to_string());
731 d.set("__WINT_WIDTH__", &wint.width.to_string());
732 for width in [8, 16, 32, 64] {
733 d.set(&format!("__INT_LEAST{width}_WIDTH__"), &width.to_string());
734 }
735 d.set("__INT_FAST8_WIDTH__", "8");
736 d.set("__INT_FAST16_WIDTH__", &fast_middle.to_string());
737 d.set("__INT_FAST32_WIDTH__", &fast_middle.to_string());
738 d.set("__INT_FAST64_WIDTH__", "64");
739}
740
741fn exact(
743 d: &mut Defs,
744 width: u32,
745 signed: &str,
746 unsigned: &str,
747 max: &str,
748 umax: &str,
749 width_suffix: &str,
753) {
754 d.set(&format!("__INT{width}_TYPE__"), signed);
755 d.set(&format!("__UINT{width}_TYPE__"), unsigned);
756 d.set(&format!("__INT{width}_MAX__"), max);
757 d.set(&format!("__UINT{width}_MAX__"), umax);
758 d.set(&format!("__INT_LEAST{width}_TYPE__"), signed);
759 d.set(&format!("__UINT_LEAST{width}_TYPE__"), unsigned);
760 d.set(&format!("__INT_LEAST{width}_MAX__"), max);
761 d.set(&format!("__UINT_LEAST{width}_MAX__"), umax);
762 let unsigned_after_promotion = width >= 32;
772 let u = if unsigned_after_promotion { "U" } else { "" };
773 if width_suffix.is_empty() && u.is_empty() {
774 d.set(&format!("__INT{width}_C(c)"), "c");
775 d.set(&format!("__UINT{width}_C(c)"), "c");
776 } else if width_suffix.is_empty() {
777 d.set(&format!("__INT{width}_C(c)"), "c");
778 d.set(&format!("__UINT{width}_C(c)"), &format!("c ## {u}"));
779 } else {
780 d.set(&format!("__INT{width}_C(c)"), &format!("c ## {width_suffix}"));
781 d.set(&format!("__UINT{width}_C(c)"), &format!("c ## {u}{width_suffix}"));
782 }
783}
784
785struct Characteristics {
791 mant_dig: &'static str,
792 dig: &'static str,
793 min_exp: &'static str,
794 min_10_exp: &'static str,
795 max_exp: &'static str,
796 max_10_exp: &'static str,
797 decimal_dig: &'static str,
798 max: &'static str,
799 min: &'static str,
800 epsilon: &'static str,
801 denorm_min: &'static str,
802 is_iec_60559: &'static str,
805}
806
807const HALF: Characteristics = Characteristics {
809 mant_dig: "11",
810 dig: "3",
811 min_exp: "(-13)",
812 min_10_exp: "(-4)",
813 max_exp: "16",
814 max_10_exp: "4",
815 decimal_dig: "5",
816 max: "6.55040000000000000000000000000000000e+4",
817 min: "6.10351562500000000000000000000000000e-5",
818 epsilon: "9.76562500000000000000000000000000000e-4",
819 denorm_min: "5.96046447753906250000000000000000000e-8",
820 is_iec_60559: "1",
821};
822
823const BFLOAT16: Characteristics = Characteristics {
825 mant_dig: "8",
826 dig: "2",
827 min_exp: "(-125)",
828 min_10_exp: "(-37)",
829 max_exp: "128",
830 max_10_exp: "38",
831 decimal_dig: "4",
832 max: "3.38953138925153547590470800371487867e+38",
833 min: "1.17549435082228750796873653722224568e-38",
834 epsilon: "7.81250000000000000000000000000000000e-3",
835 denorm_min: "9.18354961579912115600575419704879436e-41",
836 is_iec_60559: "0",
837};
838
839const SINGLE: Characteristics = Characteristics {
841 mant_dig: "24",
842 dig: "6",
843 min_exp: "(-125)",
844 min_10_exp: "(-37)",
845 max_exp: "128",
846 max_10_exp: "38",
847 decimal_dig: "9",
848 max: "3.40282346638528859811704183484516925e+38",
849 min: "1.17549435082228750796873653722224568e-38",
850 epsilon: "1.19209289550781250000000000000000000e-7",
851 denorm_min: "1.40129846432481707092372958328991613e-45",
852 is_iec_60559: "1",
853};
854
855const DOUBLE: Characteristics = Characteristics {
858 mant_dig: "53",
859 dig: "15",
860 min_exp: "(-1021)",
861 min_10_exp: "(-307)",
862 max_exp: "1024",
863 max_10_exp: "308",
864 decimal_dig: "17",
865 max: "1.79769313486231570814527423731704357e+308",
866 min: "2.22507385850720138309023271733240406e-308",
867 epsilon: "2.22044604925031308084726333618164062e-16",
868 denorm_min: "4.94065645841246544176568792868221372e-324",
869 is_iec_60559: "1",
870};
871
872const X87: Characteristics = Characteristics {
874 mant_dig: "64",
875 dig: "18",
876 min_exp: "(-16381)",
877 min_10_exp: "(-4931)",
878 max_exp: "16384",
879 max_10_exp: "4932",
880 decimal_dig: "21",
881 max: "1.18973149535723176502126385303097021e+4932",
882 min: "3.36210314311209350626267781732175260e-4932",
883 epsilon: "1.08420217248550443400745280086994171e-19",
884 denorm_min: "3.64519953188247460252840593361941982e-4951",
885 is_iec_60559: "1",
886};
887
888const QUAD: Characteristics = Characteristics {
891 mant_dig: "113",
892 dig: "33",
893 min_exp: "(-16381)",
894 min_10_exp: "(-4931)",
895 max_exp: "16384",
896 max_10_exp: "4932",
897 decimal_dig: "36",
898 max: "1.18973149535723176508575932662800702e+4932",
899 min: "3.36210314311209350626267781732175260e-4932",
900 epsilon: "1.92592994438723585305597794258492732e-34",
901 denorm_min: "6.47517511943802511092443895822764655e-4966",
902 is_iec_60559: "1",
903};
904
905const fn characteristics(format: Format) -> &'static Characteristics {
908 match format {
909 Format::Half => &HALF,
910 Format::BFloat16 => &BFLOAT16,
911 Format::Single => &SINGLE,
912 Format::Double => &DOUBLE,
913 Format::X87Extended => &X87,
914 Format::Quad => &QUAD,
915 }
916}
917
918fn floats(d: &mut Defs, target: &TargetInfo) {
929 d.set("__FLT_RADIX__", "2");
930 d.set("__GCC_IEC_559", "2");
935 d.set("__FLT_EVAL_METHOD__", "0");
940 d.set("__FLT_EVAL_METHOD_C99__", "0");
941 d.set("__FLT_EVAL_METHOD_TS_18661_3__", "0");
942
943 family(d, "FLT", &SINGLE, |value| format!("{value}F"));
944 family(d, "DBL", &DOUBLE, |value| format!("((double){value}L)"));
947 family(d, "LDBL", characteristics(target.long_double_format), |value| format!("{value}L"));
948
949 family(d, "FLT16", &HALF, |value| format!("{value}F16"));
950 family(d, "FLT32", &SINGLE, |value| format!("{value}F32"));
951 family(d, "FLT64", &DOUBLE, |value| format!("{value}F64"));
952 family(d, "FLT128", &QUAD, |value| format!("{value}F128"));
953 family(d, "FLT32X", &DOUBLE, |value| format!("{value}F32x"));
954 family(d, "FLT64X", characteristics(target.float64x_format), |value| format!("{value}F64x"));
955
956 d.set("__DECIMAL_DIG__", characteristics(target.long_double_format).decimal_dig);
961}
962
963fn family(d: &mut Defs, prefix: &str, c: &Characteristics, write: impl Fn(&str) -> String) {
969 d.set(&format!("__{prefix}_MANT_DIG__"), c.mant_dig);
970 d.set(&format!("__{prefix}_DIG__"), c.dig);
971 d.set(&format!("__{prefix}_MIN_EXP__"), c.min_exp);
972 d.set(&format!("__{prefix}_MIN_10_EXP__"), c.min_10_exp);
973 d.set(&format!("__{prefix}_MAX_EXP__"), c.max_exp);
974 d.set(&format!("__{prefix}_MAX_10_EXP__"), c.max_10_exp);
975 d.set(&format!("__{prefix}_DECIMAL_DIG__"), c.decimal_dig);
976 d.set(&format!("__{prefix}_MAX__"), &write(c.max));
977 d.set(&format!("__{prefix}_NORM_MAX__"), &write(c.max));
978 d.set(&format!("__{prefix}_MIN__"), &write(c.min));
979 d.set(&format!("__{prefix}_EPSILON__"), &write(c.epsilon));
980 d.set(&format!("__{prefix}_DENORM_MIN__"), &write(c.denorm_min));
981 d.set(&format!("__{prefix}_IS_IEC_60559__"), c.is_iec_60559);
982 d.set(&format!("__{prefix}_HAS_DENORM__"), "1");
983 d.set(&format!("__{prefix}_HAS_INFINITY__"), "1");
984 d.set(&format!("__{prefix}_HAS_QUIET_NAN__"), "1");
985}
986
987#[cfg(test)]
988mod tests {
989 use rucc_target::Triple;
990
991 use super::*;
992
993 fn set_for(triple: &str) -> String {
994 let triple: Triple = triple.parse().expect("a triple the compiler supports");
995 built_in(&TargetInfo::new(triple), &Predef::new())
996 }
997
998 fn has(text: &str, line: &str) -> bool {
999 text.lines().any(|l| l == line)
1000 }
1001
1002 #[test]
1003 fn the_set_is_driven_by_the_target_rather_than_by_the_host() {
1004 let x86 = set_for("x86_64-unknown-linux-gnu");
1005 let arm = set_for("aarch64-unknown-linux-gnu");
1006 assert!(has(&x86, "#define __x86_64__ 1"));
1007 assert!(!has(&x86, "#define __aarch64__ 1"));
1008 assert!(has(&arm, "#define __aarch64__ 1"));
1009 assert!(!has(&arm, "#define __x86_64__ 1"));
1010 assert!(has(&x86, "#define __linux__ 1") && has(&arm, "#define __linux__ 1"));
1011 }
1012
1013 #[test]
1014 fn windows_is_the_target_that_makes_long_thirty_two_bits() {
1015 let windows = set_for("x86_64-pc-windows-msvc");
1016 let linux = set_for("x86_64-unknown-linux-gnu");
1017 assert!(has(&windows, "#define __SIZEOF_LONG__ 4"));
1018 assert!(has(&windows, "#define __SIZE_TYPE__ long long unsigned int"));
1019 assert!(has(&windows, "#define __INT64_TYPE__ long long int"));
1020 assert!(!has(&windows, "#define __LP64__ 1"));
1021 assert!(has(&linux, "#define __SIZEOF_LONG__ 8"));
1022 assert!(has(&linux, "#define __SIZE_TYPE__ long unsigned int"));
1023 assert!(has(&linux, "#define __INT64_TYPE__ long int"));
1024 assert!(has(&linux, "#define __LP64__ 1"));
1025 }
1026
1027 #[test]
1028 fn wchar_t_is_the_type_that_divides_the_targets() {
1029 assert!(has(&set_for("x86_64-unknown-linux-gnu"), "#define __WCHAR_TYPE__ int"));
1031 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __WCHAR_TYPE__ unsigned int"));
1032 let windows = set_for("x86_64-pc-windows-msvc");
1033 assert!(has(&windows, "#define __WCHAR_TYPE__ short unsigned int"));
1034 assert!(has(&windows, "#define __SIZEOF_WCHAR_T__ 2"));
1035 }
1036
1037 #[test]
1038 fn apple_spells_the_architecture_its_own_way_and_its_headers_only_know_that_spelling() {
1039 let darwin = set_for("aarch64-apple-darwin");
1042 assert!(has(&darwin, "#define __arm64__ 1"));
1043 assert!(has(&darwin, "#define __arm64 1"));
1044 assert!(has(&darwin, "#define __aarch64__ 1"), "the portable spelling stays too");
1045 let linux = set_for("aarch64-unknown-linux-gnu");
1046 assert!(!has(&linux, "#define __arm64__ 1"), "Apple's spelling is Apple's alone");
1047 assert!(!has(&set_for("x86_64-apple-darwin"), "#define __arm64__ 1"));
1048 }
1049
1050 #[test]
1051 fn every_limit_is_spelled_in_hexadecimal_the_way_gcc_spells_it() {
1052 let linux = set_for("x86_64-unknown-linux-gnu");
1059 for line in [
1060 "#define __SCHAR_MAX__ 0x7f",
1061 "#define __SHRT_MAX__ 0x7fff",
1062 "#define __INT_MAX__ 0x7fffffff",
1063 "#define __LONG_MAX__ 0x7fffffffffffffffL",
1064 "#define __LONG_LONG_MAX__ 0x7fffffffffffffffLL",
1065 "#define __INTMAX_MAX__ 0x7fffffffffffffffL",
1066 "#define __UINTMAX_MAX__ 0xffffffffffffffffUL",
1067 "#define __SIZE_MAX__ 0xffffffffffffffffUL",
1068 "#define __PTRDIFF_MAX__ 0x7fffffffffffffffL",
1069 "#define __SIG_ATOMIC_MAX__ 0x7fffffff",
1070 "#define __INT8_MAX__ 0x7f",
1071 "#define __UINT8_MAX__ 0xff",
1072 "#define __INT16_MAX__ 0x7fff",
1073 "#define __UINT16_MAX__ 0xffff",
1074 "#define __INT32_MAX__ 0x7fffffff",
1075 "#define __UINT32_MAX__ 0xffffffffU",
1076 "#define __INT64_MAX__ 0x7fffffffffffffffL",
1077 "#define __UINT64_MAX__ 0xffffffffffffffffUL",
1078 "#define __INT_FAST8_MAX__ 0x7f",
1079 "#define __UINT_FAST8_MAX__ 0xff",
1080 ] {
1081 assert!(has(&linux, line), "{line}");
1082 }
1083 let windows = set_for("x86_64-pc-windows-msvc");
1086 assert!(has(&windows, "#define __LONG_MAX__ 0x7fffffffL"));
1087 assert!(has(&windows, "#define __INTMAX_MAX__ 0x7fffffffffffffffLL"));
1088 assert!(has(&windows, "#define __UINTMAX_MAX__ 0xffffffffffffffffULL"));
1089 }
1090
1091 #[test]
1092 fn wint_t_does_not_follow_wchar_t() {
1093 let darwin = set_for("aarch64-apple-darwin");
1095 assert!(has(&darwin, "#define __WINT_TYPE__ int"));
1096 assert!(has(&darwin, "#define __WINT_MAX__ 0x7fffffff"));
1097 assert!(has(&darwin, "#define __WCHAR_TYPE__ int"));
1098 let linux = set_for("aarch64-unknown-linux-gnu");
1099 assert!(has(&linux, "#define __WINT_TYPE__ unsigned int"));
1100 assert!(has(&linux, "#define __WINT_MAX__ 0xffffffffU"));
1101 assert!(has(&linux, "#define __WCHAR_TYPE__ unsigned int"), "and wchar_t is its own");
1102 assert!(has(
1103 &set_for("x86_64-pc-windows-msvc"),
1104 "#define __WINT_TYPE__ short unsigned int"
1105 ));
1106 }
1107
1108 #[test]
1109 fn the_widths_say_what_the_type_holds_and_follow_the_target_that_changes_it() {
1110 let linux = set_for("x86_64-unknown-linux-gnu");
1114 assert_eq!(linux.lines().filter(|line| line.contains("_WIDTH__")).count(), 20);
1115 assert!(has(&linux, "#define __LONG_WIDTH__ 64"));
1116 assert!(has(&linux, "#define __SIZE_WIDTH__ 64"));
1117 assert!(has(&linux, "#define __WCHAR_WIDTH__ 32"));
1118 assert!(has(&linux, "#define __INT_LEAST16_WIDTH__ 16"));
1119 assert!(has(&linux, "#define __INT_FAST16_WIDTH__ 64"));
1122 assert!(has(&set_for("x86_64-unknown-linux-musl"), "#define __INT_FAST16_WIDTH__ 32"));
1123 let windows = set_for("x86_64-pc-windows-msvc");
1126 assert!(has(&windows, "#define __LONG_WIDTH__ 32"));
1127 assert!(has(&windows, "#define __WINT_WIDTH__ 16"));
1128 assert!(has(&windows, "#define __SIZE_WIDTH__ 64"));
1129 assert!(has(&windows, "#define __INTMAX_WIDTH__ 64"));
1130 }
1131
1132 #[test]
1133 fn a_constant_maker_gets_the_suffix_its_width_needs_and_no_other() {
1134 let linux = set_for("x86_64-unknown-linux-gnu");
1138 assert!(has(&linux, "#define __INT32_C(c) c"));
1139 assert!(has(&linux, "#define __UINT32_C(c) c ## U"));
1140 assert!(has(&linux, "#define __INT16_C(c) c"));
1141 assert!(has(&linux, "#define __UINT16_C(c) c"));
1144 assert!(has(&linux, "#define __UINT8_C(c) c"));
1145 assert!(has(&linux, "#define __INT64_C(c) c ## L"));
1147 assert!(has(&linux, "#define __UINT64_C(c) c ## UL"));
1148 let windows = set_for("x86_64-pc-windows-msvc");
1150 assert!(has(&windows, "#define __INT64_C(c) c ## LL"));
1151 assert!(has(&windows, "#define __UINT64_C(c) c ## ULL"));
1152 }
1153
1154 #[test]
1155 fn the_symbol_prefix_is_defined_everywhere_including_where_it_is_empty() {
1156 for triple in
1161 ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
1162 {
1163 assert!(has(&set_for(triple), "#define __USER_LABEL_PREFIX__ "), "{triple}");
1164 }
1165 assert!(has(&set_for("aarch64-apple-darwin"), "#define __USER_LABEL_PREFIX__ _"));
1167 }
1168
1169 #[test]
1173 fn the_toolchain_macros_gcc_defines_are_defined_with_gccs_values() {
1174 let linux = set_for("x86_64-unknown-linux-gnu");
1175 for line in [
1176 "#define __GNUC_EXECUTION_CHARSET_NAME \"UTF-8\"",
1177 "#define __GNUC_WIDE_EXECUTION_CHARSET_NAME \"UTF-32LE\"",
1178 "#define __GXX_ABI_VERSION 1021",
1179 "#define __REGISTER_PREFIX__ ",
1180 "#define __FINITE_MATH_ONLY__ 0",
1181 "#define __GCC_IEC_559 2",
1182 "#define __GCC_CONSTRUCTIVE_SIZE 64",
1183 "#define __GCC_DESTRUCTIVE_SIZE 64",
1184 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1",
1185 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1",
1186 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1",
1187 "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1",
1188 "#define __ATOMIC_HLE_ACQUIRE 65536",
1189 "#define __ATOMIC_HLE_RELEASE 131072",
1190 "#define __FXSR__ 1",
1191 "#define __MMX_WITH_SSE__ 1",
1192 "#define __code_model_small__ 1",
1193 ] {
1194 assert!(has(&linux, line), "{line}");
1195 }
1196 assert!(!linux.contains("__GCC_IEC_559_COMPLEX"));
1199 let arm = set_for("aarch64-unknown-linux-gnu");
1201 for name in ["__ATOMIC_HLE_ACQUIRE", "__FXSR__", "__MMX_WITH_SSE__", "__code_model_small__"]
1202 {
1203 assert!(!arm.contains(name), "{name}");
1204 }
1205 assert!(has(&arm, "#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1"));
1206 let windows = set_for("x86_64-pc-windows-msvc");
1208 assert!(has(&windows, "#define __GNUC_WIDE_EXECUTION_CHARSET_NAME \"UTF-16LE\""));
1209 }
1210
1211 #[test]
1212 fn the_memory_orders_are_there_even_without_atomics() {
1213 let linux = set_for("x86_64-unknown-linux-gnu");
1218 assert!(has(&linux, "#define __ATOMIC_RELAXED 0"));
1219 assert!(has(&linux, "#define __ATOMIC_SEQ_CST 5"));
1220 assert!(has(&linux, "#define __STDC_NO_ATOMICS__ 1"), "and we still have no _Atomic");
1221 assert!(has(&linux, "#define __GCC_ATOMIC_INT_LOCK_FREE 2"));
1222 assert!(has(&linux, "#define __GCC_ATOMIC_LLONG_LOCK_FREE 2"));
1223 assert!(has(&set_for("x86_64-pc-windows-msvc"), "#define __GCC_ATOMIC_LLONG_LOCK_FREE 2"));
1224 }
1225
1226 #[test]
1227 fn long_double_is_three_types_and_the_macros_say_which() {
1228 assert!(has(&set_for("x86_64-unknown-linux-gnu"), "#define __LDBL_MANT_DIG__ 64"));
1229 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __LDBL_MANT_DIG__ 113"));
1230 assert!(has(&set_for("aarch64-apple-darwin"), "#define __LDBL_MANT_DIG__ 53"));
1231 }
1232
1233 #[test]
1234 fn the_extended_floating_types_have_the_limits_their_formats_have() {
1235 let linux = set_for("x86_64-unknown-linux-gnu");
1238 assert!(has(&linux, "#define __FLT16_MANT_DIG__ 11"));
1239 assert!(has(&linux, "#define __FLT32_MANT_DIG__ 24"));
1240 assert!(has(&linux, "#define __FLT64_MANT_DIG__ 53"));
1241 assert!(has(&linux, "#define __FLT128_MANT_DIG__ 113"));
1242 assert!(has(&linux, "#define __FLT32X_MANT_DIG__ 53"));
1243 assert!(has(&linux, "#define __FLT16_MAX__ 6.55040000000000000000000000000000000e+4F16"));
1246 assert!(has(
1247 &linux,
1248 "#define __FLT32X_MIN__ 2.22507385850720138309023271733240406e-308F32x"
1249 ));
1250 assert!(!linux.contains("__FLT128X_"));
1253 }
1254
1255 #[test]
1256 fn float64x_keeps_the_width_that_long_double_loses_on_apple() {
1257 let linux = set_for("x86_64-unknown-linux-gnu");
1260 assert!(has(&linux, "#define __FLT64X_MANT_DIG__ 64"));
1261 assert!(has(&linux, "#define __LDBL_MANT_DIG__ 64"));
1262 let mac = set_for("aarch64-apple-darwin");
1263 assert!(has(&mac, "#define __FLT64X_MANT_DIG__ 113"));
1264 assert!(has(&mac, "#define __LDBL_MANT_DIG__ 53"));
1265 let windows = set_for("x86_64-pc-windows-msvc");
1266 assert!(has(&windows, "#define __FLT64X_MANT_DIG__ 64"));
1267 assert!(has(&windows, "#define __LDBL_MANT_DIG__ 53"));
1268 }
1269
1270 #[test]
1271 fn the_largest_value_of_a_binary_format_is_also_its_largest_normal_one() {
1272 let linux = set_for("x86_64-unknown-linux-gnu");
1275 for prefix in ["FLT", "DBL", "LDBL", "FLT16", "FLT32", "FLT64", "FLT128", "FLT32X"] {
1276 let value = |suffix: &str| {
1277 let name = format!("#define __{prefix}_{suffix}__ ");
1278 let line = linux
1279 .lines()
1280 .find(|line| line.starts_with(&name))
1281 .unwrap_or_else(|| panic!("__{prefix}_{suffix}__ is defined"));
1282 line[name.len()..].to_owned()
1283 };
1284 assert_eq!(value("MAX"), value("NORM_MAX"), "__{prefix}_NORM_MAX__");
1285 }
1286 }
1287
1288 #[test]
1289 fn the_widest_bit_int_is_said_in_every_dialect() {
1290 let mut opts = Predef::new();
1294 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1295 assert!(has(&built_in(&target, &opts), "#define __BITINT_MAXWIDTH__ 128"));
1296 opts.std = Std::C17;
1297 assert!(has(&built_in(&target, &opts), "#define __BITINT_MAXWIDTH__ 128"));
1298 }
1299
1300 #[test]
1301 fn char_signedness_is_recorded_only_when_it_is_unsigned() {
1302 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __CHAR_UNSIGNED__ 1"));
1304 assert!(!has(&set_for("x86_64-unknown-linux-gnu"), "#define __CHAR_UNSIGNED__ 1"));
1305 }
1306
1307 #[test]
1308 fn the_dialect_decides_the_standard_macros() {
1309 let mut opts = Predef::new();
1310 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1311 assert!(has(&built_in(&target, &opts), "#define __STDC_VERSION__ 202311L"));
1312 assert!(!has(&built_in(&target, &opts), "#define __STRICT_ANSI__ 1"));
1313 assert!(has(&built_in(&target, &opts), "#define linux 1"));
1314
1315 opts.gnu_extensions = false;
1316 assert!(has(&built_in(&target, &opts), "#define __STRICT_ANSI__ 1"));
1317 assert!(!has(&built_in(&target, &opts), "#define linux 1"), "not a reserved name");
1318
1319 opts.std = Std::C89;
1320 let c89 = built_in(&target, &opts);
1321 assert!(!c89.contains("__STDC_VERSION__"), "C89 does not define it at all");
1322 assert!(has(&c89, "#define __STDC__ 1"));
1323 }
1324
1325 #[test]
1328 fn the_only_things_claimed_missing_are_the_ones_that_are_missing() {
1329 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1330 let opts = Predef::new();
1331 let set = built_in(&target, &opts);
1332 assert!(has(&set, "#define __STDC_NO_ATOMICS__ 1"), "there is no stdatomic.h to include");
1333 assert!(has(&set, "#define __STDC_NO_THREADS__ 1"), "nor a threads.h");
1334 assert!(has(&set, "#define __STDC_NO_COMPLEX__ 1"), "the arithmetic is not lowered");
1335 assert!(!set.contains("__STDC_NO_VLA__"), "variable length arrays work");
1336 }
1337
1338 #[test]
1342 fn the_type_behind_char8_t_is_defined_in_c23_and_in_no_dialect_before_it() {
1343 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1344 let mut opts = Predef::new();
1345 assert!(has(&built_in(&target, &opts), "#define __CHAR8_TYPE__ unsigned char"));
1346
1347 for older in [Std::C17, Std::C11, Std::C99, Std::C89] {
1348 opts.std = older;
1349 assert!(!built_in(&target, &opts).contains("__CHAR8_TYPE__"), "{older:?}");
1350 }
1351 }
1352
1353 #[test]
1354 fn the_optimizer_level_is_visible_to_the_preprocessor() {
1355 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1356 let mut opts = Predef::new();
1357 assert!(has(&built_in(&target, &opts), "#define __NO_INLINE__ 1"));
1358 assert!(!built_in(&target, &opts).contains("__OPTIMIZE__"));
1359
1360 opts.opt_level = OptLevel::O2;
1361 assert!(has(&built_in(&target, &opts), "#define __OPTIMIZE__ 1"));
1362 assert!(!built_in(&target, &opts).contains("__OPTIMIZE_SIZE__"));
1363
1364 opts.opt_level = OptLevel::Os;
1365 assert!(has(&built_in(&target, &opts), "#define __OPTIMIZE_SIZE__ 1"));
1366 }
1367
1368 #[test]
1369 fn a_command_line_define_with_no_value_is_one() {
1370 let mut opts = Predef::new();
1371 opts.defines = vec!["FOO".to_owned(), "BAR=2".to_owned(), "F(x)=x + 1".to_owned()];
1372 opts.undefines = vec!["__linux__".to_owned()];
1373 let text = command_line(&opts);
1374 assert!(has(&text, "#define FOO 1"));
1375 assert!(has(&text, "#define BAR 2"));
1376 assert!(has(&text, "#define F(x) x + 1"));
1377 assert!(text.trim_end().ends_with("#undef __linux__"));
1379 }
1380
1381 #[test]
1382 fn no_command_line_macros_is_no_file_at_all() {
1383 assert!(command_line(&Predef::new()).is_empty());
1384 }
1385
1386 #[test]
1387 fn a_date_is_spelled_the_way_the_standard_fixes() {
1388 let epoch = Timestamp::from_unix(0);
1390 assert_eq!(epoch.date, "Jan 1 1970");
1391 assert_eq!(epoch.time, "00:00:00");
1392 let leap = Timestamp::from_unix(1_709_164_800);
1393 assert_eq!(leap.date, "Feb 29 2024", "2024 is a leap year");
1394 let late = Timestamp::from_unix(1_735_689_599);
1395 assert_eq!(late.date, "Dec 31 2024");
1396 assert_eq!(late.time, "23:59:59");
1397 }
1398
1399 #[test]
1400 fn a_date_before_the_epoch_still_comes_out_right() {
1401 assert_eq!(Timestamp::from_unix(-1).date, "Dec 31 1969");
1404 assert_eq!(Timestamp::from_unix(-1).time, "23:59:59");
1405 }
1406
1407 #[test]
1408 fn the_gnuc_version_is_a_knob_rather_than_a_constant() {
1409 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1410 let mut opts = Predef::new();
1411 assert!(has(&built_in(&target, &opts), "#define __GNUC__ 7"));
1412 opts.gnuc = GnucVersion { major: 15, minor: 1, patch: 0 };
1413 assert!(has(&built_in(&target, &opts), "#define __GNUC__ 15"));
1414 assert!(has(&built_in(&target, &opts), "#define __GNUC_MINOR__ 1"));
1415 }
1416
1417 #[test]
1418 fn musl_and_glibc_disagree_about_the_fast_types_on_the_same_processor() {
1419 let gnu = set_for("x86_64-unknown-linux-gnu");
1424 let musl = set_for("x86_64-unknown-linux-musl");
1425 assert!(has(&gnu, "#define __INT_FAST16_TYPE__ long int"));
1426 assert!(has(&gnu, "#define __INT_FAST32_TYPE__ long int"));
1427 assert!(has(&gnu, "#define __UINT_FAST16_TYPE__ long unsigned int"));
1428 assert!(has(&musl, "#define __INT_FAST16_TYPE__ int"));
1429 assert!(has(&musl, "#define __INT_FAST32_TYPE__ int"));
1430 assert!(has(&musl, "#define __UINT_FAST16_TYPE__ unsigned int"));
1431 assert!(has(&gnu, "#define __INT_FAST16_MAX__ 0x7fffffffffffffffL"));
1434 assert!(has(&musl, "#define __INT_FAST16_MAX__ 0x7fffffff"));
1435 assert!(has(&musl, "#define __UINT_FAST16_MAX__ 0xffffffffU"));
1436 }
1437
1438 #[test]
1439 fn the_libc_only_moves_the_two_fast_types_it_is_allowed_to_move() {
1440 let gnu = set_for("x86_64-unknown-linux-gnu");
1443 let musl = set_for("x86_64-unknown-linux-musl");
1444 for line in [
1445 "#define __INT_FAST8_TYPE__ signed char",
1446 "#define __INT_FAST64_TYPE__ long int",
1447 "#define __INT64_TYPE__ long int",
1448 "#define __SIZE_TYPE__ long unsigned int",
1449 "#define __SIZEOF_LONG__ 8",
1450 "#define __LP64__ 1",
1451 ] {
1452 assert!(has(&gnu, line), "glibc lost {line}");
1453 assert!(has(&musl, line), "musl lost {line}");
1454 }
1455 }
1456
1457 #[test]
1458 fn a_non_x86_target_has_int_sized_fast_types_whatever_the_libc() {
1459 let arm_gnu = set_for("aarch64-unknown-linux-gnu");
1462 let arm_musl = set_for("aarch64-unknown-linux-musl");
1463 assert!(has(&arm_gnu, "#define __INT_FAST16_TYPE__ int"));
1464 assert!(has(&arm_musl, "#define __INT_FAST16_TYPE__ int"));
1465 }
1466}