Use global_asm! instead of external assembly files (#4306)

* Use `global_asm!` instead of external assembly files

This commit moves the external assembly files of the `wasmtime-fiber`
crate into `global_asm!` blocks defined in Rust. The motivation for
doing this is not very strong at this time, but the points in favor of
this are:

* One less tool needed to cross-compile Wasmtime. A linker is still
  needed but perhaps one day that will improve as well.
* A "modern" assembler, built-in to LLVM, is used instead of whatever
  appears on the system.

The first point hasn't really cropped up that much and typically getting
an assembler is just as hard as getting a linker nowadays. The second
point though has us using `hint #xx` in aarch64 assembly instead of the
actual instructions for assembler compatibility, and I believe that's no
longer necessary because the LLVM assembler supports the modern
instruction names.

The translation of the x86/x86_64 assembly has been done to Intel
syntax as well as opposed to the old AT&T syntax since that's Rust's
default. Additionally s390x still remains in an external assembler file
because `global_asm!` is still unstable in Rust on that platform.

* Simplify alignment specification

* Temporarily disable fail-fast

* Add `.cfi_def_cfa_offset 0` to fix CI

* Turn off fail-fast

* Review comments
This commit is contained in:
Alex Crichton
2022-06-27 13:20:19 -05:00
committed by GitHub
parent 0ef873f1bd
commit 4543a07bb5
16 changed files with 631 additions and 603 deletions

View File

@@ -29,6 +29,8 @@
//! `suspend`, which has 0xB000 so it can find this, will read that and write
//! its own resumption information into this slot as well.
#![allow(unused_macros)]
use crate::RunResult;
use std::cell::Cell;
use std::io;
@@ -174,3 +176,77 @@ impl Suspend {
ret.cast()
}
}
// This macro itself generates a macro named `asm_func!` which is suitable for
// generating a single `global_asm!`-defined function. This takes care of
// platform-specific directives to get the symbol attributes correct (e.g. ELF
// symbols get a size and are flagged as a function) and additionally handles
// visibility across platforms. All symbols should be visible to Rust but not
// visible externally outside of a `*.so`.
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
macro_rules! asm_func {
($name:tt, $($body:tt)*) => {
std::arch::global_asm!(concat!(
".p2align 4\n",
".private_extern _", $name, "\n",
".global _", $name, "\n",
"_", $name, ":\n",
$($body)*
));
};
}
macro_rules! asm_sym {
($name:tt) => (concat!("_", $name))
}
} else {
// Note that for now this "else" clause just assumes that everything
// other than macOS is ELF and has the various directives here for
// that.
cfg_if::cfg_if! {
if #[cfg(target_arch = "arm")] {
macro_rules! elf_func_type_header {
($name:tt) => (concat!(".type ", $name, ",%function\n"))
}
} else {
macro_rules! elf_func_type_header {
($name:tt) => (concat!(".type ", $name, ",@function\n"))
}
}
}
macro_rules! asm_func {
($name:tt, $($body:tt)*) => {
std::arch::global_asm!(concat!(
".p2align 4\n",
".hidden ", $name, "\n",
".global ", $name, "\n",
elf_func_type_header!($name),
$name, ":\n",
$($body)*
".size ", $name, ",.-", $name,
));
};
}
macro_rules! asm_sym {
($name:tt) => ($name)
}
}
}
cfg_if::cfg_if! {
if #[cfg(target_arch = "aarch64")] {
mod aarch64;
} else if #[cfg(target_arch = "x86_64")] {
mod x86_64;
} else if #[cfg(target_arch = "x86")] {
mod x86;
} else if #[cfg(target_arch = "arm")] {
mod arm;
} else if #[cfg(target_arch = "s390x")] {
// currently `global_asm!` isn't stable on s390x so this is an external
// assembler file built with the `build.rs`.
} else {
compile_error!("fibers are not supported on this CPU architecture");
}
}