1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
//! Macros shared throughout the compiler-builtins implementation
/// Changes the visibility to `pub` if feature "public-test-deps" is set
#[cfg(not(feature = "public-test-deps"))]
macro_rules! public_test_dep {
($(#[$($meta:meta)*])* pub(crate) $ident:ident $($tokens:tt)*) => {
$(#[$($meta)*])* pub(crate) $ident $($tokens)*
};
}
/// Changes the visibility to `pub` if feature "public-test-deps" is set
#[cfg(feature = "public-test-deps")]
macro_rules! public_test_dep {
{$(#[$($meta:meta)*])* pub(crate) $ident:ident $($tokens:tt)*} => {
$(#[$($meta)*])* pub $ident $($tokens)*
};
}
/// The "main macro" used for defining intrinsics.
///
/// The compiler-builtins library is super platform-specific with tons of crazy
/// little tweaks for various platforms. As a result it *could* involve a lot of
/// #[cfg] and macro soup, but the intention is that this macro alleviates a lot
/// of that complexity. Ideally this macro has all the weird ABI things
/// platforms need and elsewhere in this library it just looks like normal Rust
/// code.
///
/// When the weak-intrinsics feature is enabled, all intrinsics functions are
/// marked with #[linkage = "weak"] so that they can be replaced by another
/// implementation at link time. This is particularly useful for mixed Rust/C++
/// binaries that want to use the C++ intrinsics, otherwise linking against the
/// Rust stdlib will replace those from the compiler-rt library.
///
/// This macro is structured to be invoked with a bunch of functions that looks
/// like:
/// ```ignore
/// intrinsics! {
/// pub extern "C" fn foo(a: i32) -> u32 {
/// // ...
/// }
///
/// #[nonstandard_attribute]
/// pub extern "C" fn bar(a: i32) -> u32 {
/// // ...
/// }
/// }
/// ```
///
/// Each function is defined in a manner that looks like a normal Rust function.
/// The macro then accepts a few nonstandard attributes that can decorate
/// various functions. Each of the attributes is documented below with what it
/// can do, and each of them slightly tweaks how further expansion happens.
///
/// A quick overview of attributes supported right now are:
///
/// * `weak` - indicates that the function should always be given weak linkage.
/// This attribute must come before other attributes, as the other attributes
/// will generate the final output function and need to have `weak` modify
/// them.
/// * `maybe_use_optimized_c_shim` - indicates that the Rust implementation is
/// ignored if an optimized C version was compiled.
/// * `aapcs_on_arm` - forces the ABI of the function to be `"aapcs"` on ARM and
/// the specified ABI everywhere else.
/// * `unadjusted_on_win64` - like `aapcs_on_arm` this switches to the
/// `"unadjusted"` abi on Win64 and the specified abi elsewhere.
/// * `win64_128bit_abi_hack` - this attribute is used for 128-bit integer
/// intrinsics where the ABI is slightly tweaked on Windows platforms, but
/// it's a normal ABI elsewhere for returning a 128 bit integer.
/// * `arm_aeabi_alias` - handles the "aliasing" of various intrinsics on ARM
/// their otherwise typical names to other prefixed ones.
macro_rules! intrinsics {
() => ();
// Support cfg_attr:
(
#[cfg_attr($e:meta, $($attr:tt)*)]
$(#[$($attrs:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg($e)]
intrinsics! {
#[$($attr)*]
$(#[$($attrs)*])*
pub extern $abi fn $name($($argname: $ty),*) $(-> $ret)? {
$($body)*
}
}
#[cfg(not($e))]
intrinsics! {
$(#[$($attrs)*])*
pub extern $abi fn $name($($argname: $ty),*) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// Same as above but for unsafe.
(
#[cfg_attr($e:meta, $($attr:tt)*)]
$(#[$($attrs:tt)*])*
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg($e)]
intrinsics! {
#[$($attr)*]
$(#[$($attrs)*])*
pub unsafe extern $abi fn $name($($argname: $ty),*) $(-> $ret)? {
$($body)*
}
}
#[cfg(not($e))]
intrinsics! {
$(#[$($attrs)*])*
pub unsafe extern $abi fn $name($($argname: $ty),*) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// Explicit weak linkage gets dropped when weak-intrinsics is on since it
// will be added unconditionally to all intrinsics and would conflict
// otherwise.
(
#[weak]
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg(feature = "weak-intrinsics")]
intrinsics! {
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
#[cfg(not(feature = "weak-intrinsics"))]
intrinsics! {
$(#[$($attr)*])*
#[linkage = "weak"]
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// Same as above but for unsafe.
(
#[weak]
$(#[$($attr:tt)*])*
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg(feature = "weak-intrinsics")]
intrinsics! {
$(#[$($attr)*])*
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
#[cfg(not(feature = "weak-intrinsics"))]
intrinsics! {
$(#[$($attr)*])*
#[linkage = "weak"]
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// Right now there's a bunch of architecture-optimized intrinsics in the
// stock compiler-rt implementation. Not all of these have been ported over
// to Rust yet so when the `c` feature of this crate is enabled we fall back
// to the architecture-specific versions which should be more optimized. The
// purpose of this macro is to easily allow specifying this.
//
// The `#[maybe_use_optimized_c_shim]` attribute indicates that this
// intrinsic may have an optimized C version. In these situations the build
// script, if the C code is enabled and compiled, will emit a cfg directive
// to get passed to rustc for our compilation. If that cfg is set we skip
// the Rust implementation, but if the attribute is not enabled then we
// compile in the Rust implementation.
(
#[maybe_use_optimized_c_shim]
$(#[$($attr:tt)*])*
pub $(unsafe $(@ $empty:tt)? )? extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg($name = "optimized-c")]
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub $(unsafe $($empty)? )? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
extern $abi {
fn $name($($argname: $ty),*) $(-> $ret)?;
}
unsafe {
$name($($argname),*)
}
}
#[cfg(not($name = "optimized-c"))]
intrinsics! {
$(#[$($attr)*])*
pub $(unsafe $($empty)? )? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// We recognize the `#[aapcs_on_arm]` attribute here and generate the
// same intrinsic but force it to have the `"aapcs"` calling convention on
// ARM and `"C"` elsewhere.
(
#[aapcs_on_arm]
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg(target_arch = "arm")]
intrinsics! {
$(#[$($attr)*])*
pub extern "aapcs" fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
#[cfg(not(target_arch = "arm"))]
intrinsics! {
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// Like aapcs above we recognize an attribute for the "unadjusted" abi on
// win64 for some methods.
(
#[unadjusted_on_win64]
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg(all(any(windows, all(target_os = "uefi", target_arch = "x86_64")), target_pointer_width = "64"))]
intrinsics! {
$(#[$($attr)*])*
pub extern "unadjusted" fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
#[cfg(not(all(any(windows, all(target_os = "uefi", target_arch = "x86_64")), target_pointer_width = "64")))]
intrinsics! {
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// Some intrinsics on win64 which return a 128-bit integer have an.. unusual
// calling convention. That's managed here with this "abi hack" which alters
// the generated symbol's ABI.
//
// This will still define a function in this crate with the given name and
// signature, but the actual symbol for the intrinsic may have a slightly
// different ABI on win64.
(
#[win64_128bit_abi_hack]
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg(all(any(windows, target_os = "uefi"), target_arch = "x86_64"))]
$(#[$($attr)*])*
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
#[cfg(all(any(windows, target_os = "uefi"), target_arch = "x86_64"))]
pub mod $name {
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub extern $abi fn $name( $($argname: $ty),* )
-> ::macros::win64_128bit_abi_hack::U64x2
{
let e: $($ret)? = super::$name($($argname),*);
::macros::win64_128bit_abi_hack::U64x2::from(e)
}
}
#[cfg(not(all(any(windows, target_os = "uefi"), target_arch = "x86_64")))]
intrinsics! {
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// A bunch of intrinsics on ARM are aliased in the standard compiler-rt
// build under `__aeabi_*` aliases, and LLVM will call these instead of the
// original function. The aliasing here is used to generate these symbols in
// the object file.
(
#[arm_aeabi_alias = $alias:ident]
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg(target_arch = "arm")]
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
#[cfg(target_arch = "arm")]
pub mod $name {
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
super::$name($($argname),*)
}
}
#[cfg(target_arch = "arm")]
pub mod $alias {
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg_attr(any(all(not(windows), not(target_vendor="apple"), feature = "weak-intrinsics")), linkage = "weak")]
pub extern "aapcs" fn $alias( $($argname: $ty),* ) $(-> $ret)? {
super::$name($($argname),*)
}
}
#[cfg(not(target_arch = "arm"))]
intrinsics! {
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// C mem* functions are only generated when the "mem" feature is enabled.
(
#[mem_builtin]
$(#[$($attr:tt)*])*
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
$(#[$($attr)*])*
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
#[cfg(feature = "mem")]
pub mod $name {
$(#[$($attr)*])*
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
super::$name($($argname),*)
}
}
intrinsics!($($rest)*);
);
// Naked functions are special: we can't generate wrappers for them since
// they use a custom calling convention.
(
#[naked]
$(#[$($attr:tt)*])*
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
pub mod $name {
#[naked]
$(#[$($attr)*])*
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// For some intrinsics, AVR uses a custom calling convention¹ that does not
// match our definitions here. Ideally we would just use hand-written naked
// functions, but that's quite a lot of code to port² - so for the time
// being we are just ignoring the problematic functions, letting avr-gcc
// (which is required to compile to AVR anyway) link them from libgcc.
//
// ¹ https://gcc.gnu.org/wiki/avr-gcc (see "Exceptions to the Calling
// Convention")
// ² https://github.com/gcc-mirror/gcc/blob/31048012db98f5ec9c2ba537bfd850374bdd771f/libgcc/config/avr/lib1funcs.S
(
#[avr_skip]
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
#[cfg(not(target_arch = "avr"))]
intrinsics! {
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
}
intrinsics!($($rest)*);
);
// This is the final catch-all rule. At this point we generate an
// intrinsic with a conditional `#[no_mangle]` directive to avoid
// interfering with duplicate symbols and whatnot during testing.
//
// The implementation is placed in a separate module, to take advantage
// of the fact that rustc partitions functions into code generation
// units based on module they are defined in. As a result we will have
// a separate object file for each intrinsic. For further details see
// corresponding PR in rustc https://github.com/rust-lang/rust/pull/70846
//
// After the intrinsic is defined we just continue with the rest of the
// input we were given.
(
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
pub mod $name {
$(#[$($attr)*])*
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
super::$name($($argname),*)
}
}
intrinsics!($($rest)*);
);
// Same as the above for unsafe functions.
(
$(#[$($attr:tt)*])*
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}
$($rest:tt)*
) => (
$(#[$($attr)*])*
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}
pub mod $name {
$(#[$($attr)*])*
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
super::$name($($argname),*)
}
}
intrinsics!($($rest)*);
);
}
// Hack for LLVM expectations for ABI on windows. This is used by the
// `#[win64_128bit_abi_hack]` attribute recognized above
#[cfg(all(any(windows, target_os = "uefi"), target_pointer_width = "64"))]
pub mod win64_128bit_abi_hack {
#[repr(simd)]
pub struct U64x2(u64, u64);
impl From<i128> for U64x2 {
fn from(i: i128) -> U64x2 {
use int::DInt;
let j = i as u128;
U64x2(j.lo(), j.hi())
}
}
impl From<u128> for U64x2 {
fn from(i: u128) -> U64x2 {
use int::DInt;
U64x2(i.lo(), i.hi())
}
}
}