Skip to content

Commit 63330f1

Browse files
the-ssdbjorn3
andauthored
cranelift-assembler-x64: no_std support (#12235)
* cranelift-assembler-x64: no_std support * cargo fmt * Update cranelift/assembler-x64/src/xmm.rs Co-authored-by: bjorn3 <[email protected]> * Update cranelift/assembler-x64/src/imm.rs Co-authored-by: bjorn3 <[email protected]> * Update cranelift/assembler-x64/src/api.rs Co-authored-by: bjorn3 <[email protected]> * Reorder import of alloc * #![no_std] * revert main.rs * cargo fmt * remove core feature * Fix CI --------- Co-authored-by: bjorn3 <[email protected]>
1 parent d175767 commit 63330f1

File tree

16 files changed

+53
-33
lines changed

16 files changed

+53
-33
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ jobs:
550550
cargo check -p wasmtime --no-default-features --features runtime,component-model &&
551551
cargo check -p wasmtime --no-default-features --features runtime,gc,component-model,async,debug-builtins &&
552552
cargo check -p cranelift-control --no-default-features &&
553+
cargo check -p cranelift-assembler-x64 --lib &&
553554
cargo check -p pulley-interpreter --features encode,decode,disas,interp &&
554555
cargo check -p wasmtime-wasi-io --no-default-features
555556
# Use `cross` for illumos to have a C compiler/linker available.

cranelift/assembler-x64/meta/src/dsl/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ impl Default for Mutability {
535535
}
536536

537537
impl core::fmt::Display for Mutability {
538-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
538+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
539539
match self {
540540
Self::Read => write!(f, "r"),
541541
Self::ReadWrite => write!(f, "rw"),
@@ -576,7 +576,7 @@ impl Default for Extension {
576576
}
577577

578578
impl core::fmt::Display for Extension {
579-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
579+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
580580
match self {
581581
Extension::None => write!(f, ""),
582582
Extension::SignExtendQuad => write!(f, "sxq"),
@@ -627,7 +627,7 @@ impl Default for Eflags {
627627
}
628628

629629
impl core::fmt::Display for Eflags {
630-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
630+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
631631
match self {
632632
Self::None => write!(f, ""),
633633
Self::R => write!(f, "r"),

cranelift/assembler-x64/meta/src/generate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ fn match_variants(f: &mut Formatter, insts: &[dsl::Inst], invoke: &str) {
5353
});
5454
}
5555

56-
/// `impl std::fmt::Display for Inst { ... }`
56+
/// `impl core::fmt::Display for Inst { ... }`
5757
fn generate_inst_display_impl(f: &mut Formatter, insts: &[dsl::Inst]) {
58-
f.add_block("impl<R: Registers> std::fmt::Display for Inst<R>", |f| {
58+
f.add_block("impl<R: Registers> core::fmt::Display for Inst<R>", |f| {
5959
f.add_block(
60-
"fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result",
60+
"fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result",
6161
|f| {
6262
match_variants(f, insts, "fmt(f)");
6363
},

cranelift/assembler-x64/meta/src/generate/inst.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ impl dsl::Inst {
107107
fmtln!(f, "#[must_use]");
108108
fmtln!(f, "#[inline]");
109109
f.add_block(
110-
&format!("pub fn mnemonic(&self) -> std::borrow::Cow<'static, str>"),
110+
&format!("pub fn mnemonic(&self) -> alloc::borrow::Cow<'static, str>"),
111111
|f| {
112112
if self.custom.contains(Mnemonic) {
113113
fmtln!(f, "crate::custom::mnemonic::{}(self)", self.name());
114114
} else {
115-
fmtln!(f, "std::borrow::Cow::Borrowed(\"{}\")", self.mnemonic);
115+
fmtln!(f, "alloc::borrow::Cow::Borrowed(\"{}\")", self.mnemonic);
116116
}
117117
},
118118
);
@@ -265,10 +265,10 @@ impl dsl::Inst {
265265
let impl_block = self.generate_impl_block_start();
266266
let struct_name = self.struct_name_with_generic();
267267
f.add_block(
268-
&format!("{impl_block} std::fmt::Display for {struct_name}"),
268+
&format!("{impl_block} core::fmt::Display for {struct_name}"),
269269
|f| {
270270
f.add_block(
271-
"fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result",
271+
"fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result",
272272
|f| {
273273
if self.custom.contains(Display) {
274274
fmtln!(f, "crate::custom::display::{}(f, self)", self.name());

cranelift/assembler-x64/src/api.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
use crate::gpr;
44
use crate::xmm;
55
use crate::{Amode, DeferredTarget, GprMem, XmmMem};
6-
use std::fmt;
7-
use std::{num::NonZeroU8, vec::Vec};
6+
use alloc::string::String;
7+
use alloc::vec::Vec;
8+
use core::fmt;
9+
use core::num::NonZeroU8;
810

911
/// Describe how an instruction is emitted into a code buffer.
1012
pub trait CodeSink {
@@ -113,7 +115,7 @@ pub trait Registers {
113115
}
114116

115117
/// Describe how to interact with an external register type.
116-
pub trait AsReg: Copy + Clone + std::fmt::Debug + PartialEq {
118+
pub trait AsReg: Copy + Clone + core::fmt::Debug + PartialEq {
117119
/// Create a register from its hardware encoding.
118120
///
119121
/// This is primarily useful for fuzzing, though it is also useful for

cranelift/assembler-x64/src/custom.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub mod encode {
7575
pub mod mnemonic {
7676
use crate::inst;
7777
use crate::{Registers, XmmMem};
78-
use std::borrow::Cow;
78+
use alloc::borrow::Cow;
7979

8080
macro_rules! lock {
8181
($name:tt => $mnemonic:expr) => {
@@ -191,7 +191,8 @@ pub mod mnemonic {
191191
pub mod display {
192192
use crate::inst;
193193
use crate::{Amode, Gpr, GprMem, Registers, Size};
194-
use std::fmt;
194+
use alloc::string::ToString;
195+
use core::fmt;
195196

196197
pub fn callq_d(f: &mut fmt::Formatter, inst: &inst::callq_d) -> fmt::Result {
197198
let inst::callq_d { imm32 } = inst;

cranelift/assembler-x64/src/features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! [`Inst::features`]: crate::inst::Inst::features
3333
3434
use crate::inst::for_each_feature;
35-
use std::fmt;
35+
use core::fmt;
3636

3737
// Helpfully generate `enum Feature`.
3838
macro_rules! create_feature_enum {

cranelift/assembler-x64/src/fixed.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Operands with fixed register encodings.
22
33
use crate::{AsReg, Size};
4+
use alloc::string::String;
45

56
/// A _fixed_ register.
67
///

cranelift/assembler-x64/src/fuzz.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
//! throughout this crate to avoid depending on the `arbitrary` crate
55
//! unconditionally (use the `fuzz` feature instead).
66
7+
use std::string::{String, ToString};
8+
use std::vec::Vec;
9+
use std::{format, println};
10+
711
use crate::{
812
AmodeOffset, AmodeOffsetPlusKnownOffset, AsReg, CodeSink, DeferredTarget, Fixed, Gpr, Inst,
913
KnownOffset, NonRspGpr, Registers, TrapCode, Xmm,
@@ -146,7 +150,7 @@ fn disassemble(assembled: &[u8], original: &Inst<FuzzRegs>) -> String {
146150
}
147151

148152
fn pretty_print_hexadecimal(hex: &[u8]) -> String {
149-
use std::fmt::Write;
153+
use core::fmt::Write;
150154
let mut s = String::with_capacity(hex.len() * 2);
151155
for b in hex {
152156
write!(&mut s, "{b:02X}").unwrap();
@@ -183,7 +187,7 @@ macro_rules! hex_print_signed_imm {
183187
/// - print negative values as `-0x...` (signed hex) instead of `0xff...`
184188
/// (normal hex)
185189
/// - print `mov` immediates as base-10 instead of base-16 (?!).
186-
fn replace_signed_immediates(dis: &str) -> std::borrow::Cow<'_, str> {
190+
fn replace_signed_immediates(dis: &str) -> alloc::borrow::Cow<'_, str> {
187191
match dis.find('$') {
188192
None => dis.into(),
189193
Some(idx) => {
@@ -259,7 +263,7 @@ fn remove_after_parenthesis_test() {
259263
}
260264

261265
/// Run some post-processing on the disassembly to make it match Capstone.
262-
fn fix_up(dis: &str) -> std::borrow::Cow<'_, str> {
266+
fn fix_up(dis: &str) -> alloc::borrow::Cow<'_, str> {
263267
let dis = remove_after_semicolon(dis);
264268
replace_signed_immediates(&dis)
265269
}

cranelift/assembler-x64/src/gpr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Pure register operands; see [`Gpr`].
22
33
use crate::AsReg;
4+
use alloc::string::String;
45

56
/// A general purpose x64 register (e.g., `%rax`).
67
///

0 commit comments

Comments
 (0)