diff --git a/cranelift/codegen/Cargo.toml b/cranelift/codegen/Cargo.toml index 5cde9d47df..e088e6f84a 100644 --- a/cranelift/codegen/Cargo.toml +++ b/cranelift/codegen/Cargo.toml @@ -20,6 +20,7 @@ failure_derive = { version = "0.1.1", default-features = false } hashmap_core = { version = "0.1.9", optional = true } target-lexicon = { version = "0.4.0", default-features = false } log = { version = "0.4.6", default-features = false } +serde = { version = "1.0.94", features = ["derive"], optional = true } # It is a goal of the cranelift-codegen crate to have minimal external dependencies. # Please don't add any unless they are essential to the task of creating binary # machine code. Integration tests that need external dependencies can be @@ -58,6 +59,9 @@ arm32 = [] arm64 = [] riscv = [] +# For dependent crates that want to serialize some parts of cranelift +enable-serde = ["serde"] + [badges] maintenance = { status = "experimental" } travis-ci = { repository = "CraneStation/cranelift" } diff --git a/cranelift/codegen/src/binemit/mod.rs b/cranelift/codegen/src/binemit/mod.rs index 21b07587ed..b0408e97e7 100644 --- a/cranelift/codegen/src/binemit/mod.rs +++ b/cranelift/codegen/src/binemit/mod.rs @@ -14,6 +14,8 @@ pub use crate::regalloc::RegDiversions; use crate::ir::{ExternalName, Function, Inst, JumpTable, SourceLoc, TrapCode}; use core::fmt; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// Offset in bytes from the beginning of the function. /// @@ -26,6 +28,7 @@ pub type Addend = i64; /// Relocation kinds for every ISA #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub enum Reloc { /// absolute 4-byte Abs4, diff --git a/cranelift/codegen/src/ir/entities.rs b/cranelift/codegen/src/ir/entities.rs index 2c05fbc84b..a2a405bc46 100644 --- a/cranelift/codegen/src/ir/entities.rs +++ b/cranelift/codegen/src/ir/entities.rs @@ -22,6 +22,8 @@ use crate::entity::entity_impl; use core::fmt; use core::u32; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// An opaque reference to an extended basic block in a function. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -103,6 +105,7 @@ impl GlobalValue { /// An opaque reference to a jump table. #[derive(Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub struct JumpTable(u32); entity_impl!(JumpTable, "jt"); diff --git a/cranelift/codegen/src/ir/libcall.rs b/cranelift/codegen/src/ir/libcall.rs index 275e22a91f..f544dd5732 100644 --- a/cranelift/codegen/src/ir/libcall.rs +++ b/cranelift/codegen/src/ir/libcall.rs @@ -7,6 +7,8 @@ use crate::ir::{ use crate::isa::{CallConv, RegUnit, TargetIsa}; use core::fmt; use core::str::FromStr; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// The name of a runtime library routine. /// @@ -17,6 +19,7 @@ use core::str::FromStr; /// /// This list is likely to grow over time. #[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub enum LibCall { /// probe for stack overflow. These are emitted for functions which need /// when the `probestack_enabled` setting is true. diff --git a/cranelift/codegen/src/ir/sourceloc.rs b/cranelift/codegen/src/ir/sourceloc.rs index ab5722a57d..a0d051d3aa 100644 --- a/cranelift/codegen/src/ir/sourceloc.rs +++ b/cranelift/codegen/src/ir/sourceloc.rs @@ -4,6 +4,8 @@ //! location when instructions are transformed. use core::fmt; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// A source location. /// @@ -13,6 +15,7 @@ use core::fmt; /// The default source location uses the all-ones bit pattern `!0`. It is used for instructions /// that can't be given a real source location. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub struct SourceLoc(u32); impl SourceLoc { diff --git a/cranelift/entity/src/map.rs b/cranelift/entity/src/map.rs index 9410cc2f90..a0d31a4309 100644 --- a/cranelift/entity/src/map.rs +++ b/cranelift/entity/src/map.rs @@ -56,11 +56,23 @@ where } } + /// Returns the number of elements in the underlying vector. + /// + /// The number is not necessarily the same as the length of the corresponding PrimaryMap. + pub fn len(&self) -> usize { + self.elems.len() + } + /// Get the element at `k` if it exists. pub fn get(&self, k: K) -> Option<&V> { self.elems.get(k.index()) } + /// Get the default value. + pub fn get_default(&self) -> &V { + &self.default + } + /// Is this map completely empty? pub fn is_empty(&self) -> bool { self.elems.is_empty() diff --git a/cranelift/wasm/Cargo.toml b/cranelift/wasm/Cargo.toml index 53a64e7896..5f9bfc4737 100644 --- a/cranelift/wasm/Cargo.toml +++ b/cranelift/wasm/Cargo.toml @@ -19,6 +19,7 @@ hashmap_core = { version = "0.1.9", optional = true } failure = { version = "0.1.1", default-features = false, features = ["derive"] } failure_derive = { version = "0.1.1", default-features = false } log = { version = "0.4.6", default-features = false } +serde = { version = "1.0.94", features = ["derive"], optional = true } [dev-dependencies] wabt = "0.7.0" @@ -28,6 +29,7 @@ target-lexicon = "0.4.0" default = ["std"] std = ["cranelift-codegen/std", "cranelift-frontend/std", "wasmparser/std"] core = ["hashmap_core", "cranelift-codegen/core", "cranelift-frontend/core", "wasmparser/core"] +enable-serde = ["serde"] [badges] maintenance = { status = "experimental" } diff --git a/cranelift/wasm/src/translation_utils.rs b/cranelift/wasm/src/translation_utils.rs index c221f751ff..759e4ab4ff 100644 --- a/cranelift/wasm/src/translation_utils.rs +++ b/cranelift/wasm/src/translation_utils.rs @@ -3,10 +3,13 @@ use crate::environ::{WasmError, WasmResult}; use core::u32; use cranelift_codegen::entity::entity_impl; use cranelift_codegen::ir; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; use wasmparser; /// Index type of a function (imported or defined) inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] +#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub struct FuncIndex(u32); entity_impl!(FuncIndex);