Add support for some serde serialization (#847)

* Add support for some serde serialization
This commit is contained in:
Artur Jamro
2019-07-12 15:30:50 -07:00
committed by Dan Gohman
parent 8edc40cb49
commit 9e884b4433
8 changed files with 33 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ failure_derive = { version = "0.1.1", default-features = false }
hashmap_core = { version = "0.1.9", optional = true } hashmap_core = { version = "0.1.9", optional = true }
target-lexicon = { version = "0.4.0", default-features = false } target-lexicon = { version = "0.4.0", default-features = false }
log = { version = "0.4.6", 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. # 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 # 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 # machine code. Integration tests that need external dependencies can be
@@ -58,6 +59,9 @@ arm32 = []
arm64 = [] arm64 = []
riscv = [] riscv = []
# For dependent crates that want to serialize some parts of cranelift
enable-serde = ["serde"]
[badges] [badges]
maintenance = { status = "experimental" } maintenance = { status = "experimental" }
travis-ci = { repository = "CraneStation/cranelift" } travis-ci = { repository = "CraneStation/cranelift" }

View File

@@ -14,6 +14,8 @@ pub use crate::regalloc::RegDiversions;
use crate::ir::{ExternalName, Function, Inst, JumpTable, SourceLoc, TrapCode}; use crate::ir::{ExternalName, Function, Inst, JumpTable, SourceLoc, TrapCode};
use core::fmt; use core::fmt;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
/// Offset in bytes from the beginning of the function. /// Offset in bytes from the beginning of the function.
/// ///
@@ -26,6 +28,7 @@ pub type Addend = i64;
/// Relocation kinds for every ISA /// Relocation kinds for every ISA
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum Reloc { pub enum Reloc {
/// absolute 4-byte /// absolute 4-byte
Abs4, Abs4,

View File

@@ -22,6 +22,8 @@
use crate::entity::entity_impl; use crate::entity::entity_impl;
use core::fmt; use core::fmt;
use core::u32; use core::u32;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
/// An opaque reference to an extended basic block in a function. /// An opaque reference to an extended basic block in a function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
@@ -103,6 +105,7 @@ impl GlobalValue {
/// An opaque reference to a jump table. /// An opaque reference to a jump table.
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct JumpTable(u32); pub struct JumpTable(u32);
entity_impl!(JumpTable, "jt"); entity_impl!(JumpTable, "jt");

View File

@@ -7,6 +7,8 @@ use crate::ir::{
use crate::isa::{CallConv, RegUnit, TargetIsa}; use crate::isa::{CallConv, RegUnit, TargetIsa};
use core::fmt; use core::fmt;
use core::str::FromStr; use core::str::FromStr;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
/// The name of a runtime library routine. /// The name of a runtime library routine.
/// ///
@@ -17,6 +19,7 @@ use core::str::FromStr;
/// ///
/// This list is likely to grow over time. /// This list is likely to grow over time.
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum LibCall { pub enum LibCall {
/// probe for stack overflow. These are emitted for functions which need /// probe for stack overflow. These are emitted for functions which need
/// when the `probestack_enabled` setting is true. /// when the `probestack_enabled` setting is true.

View File

@@ -4,6 +4,8 @@
//! location when instructions are transformed. //! location when instructions are transformed.
use core::fmt; use core::fmt;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
/// A source location. /// 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 /// 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. /// that can't be given a real source location.
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct SourceLoc(u32); pub struct SourceLoc(u32);
impl SourceLoc { impl SourceLoc {

View File

@@ -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. /// Get the element at `k` if it exists.
pub fn get(&self, k: K) -> Option<&V> { pub fn get(&self, k: K) -> Option<&V> {
self.elems.get(k.index()) self.elems.get(k.index())
} }
/// Get the default value.
pub fn get_default(&self) -> &V {
&self.default
}
/// Is this map completely empty? /// Is this map completely empty?
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.elems.is_empty() self.elems.is_empty()

View File

@@ -19,6 +19,7 @@ hashmap_core = { version = "0.1.9", optional = true }
failure = { version = "0.1.1", default-features = false, features = ["derive"] } failure = { version = "0.1.1", default-features = false, features = ["derive"] }
failure_derive = { version = "0.1.1", default-features = false } failure_derive = { version = "0.1.1", default-features = false }
log = { version = "0.4.6", default-features = false } log = { version = "0.4.6", default-features = false }
serde = { version = "1.0.94", features = ["derive"], optional = true }
[dev-dependencies] [dev-dependencies]
wabt = "0.7.0" wabt = "0.7.0"
@@ -28,6 +29,7 @@ target-lexicon = "0.4.0"
default = ["std"] default = ["std"]
std = ["cranelift-codegen/std", "cranelift-frontend/std", "wasmparser/std"] std = ["cranelift-codegen/std", "cranelift-frontend/std", "wasmparser/std"]
core = ["hashmap_core", "cranelift-codegen/core", "cranelift-frontend/core", "wasmparser/core"] core = ["hashmap_core", "cranelift-codegen/core", "cranelift-frontend/core", "wasmparser/core"]
enable-serde = ["serde"]
[badges] [badges]
maintenance = { status = "experimental" } maintenance = { status = "experimental" }

View File

@@ -3,10 +3,13 @@ use crate::environ::{WasmError, WasmResult};
use core::u32; use core::u32;
use cranelift_codegen::entity::entity_impl; use cranelift_codegen::entity::entity_impl;
use cranelift_codegen::ir; use cranelift_codegen::ir;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use wasmparser; use wasmparser;
/// Index type of a function (imported or defined) inside the WebAssembly module. /// Index type of a function (imported or defined) inside the WebAssembly module.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct FuncIndex(u32); pub struct FuncIndex(u32);
entity_impl!(FuncIndex); entity_impl!(FuncIndex);