From 8db8a0234782a612db040282dda2ee177ff36f28 Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Sun, 7 Jul 2024 14:59:55 -0400 Subject: [PATCH] serde --- Cargo.toml | 1 + src/entity.rs | 6 +++--- src/ir.rs | 2 +- src/ir/debug.rs | 6 +++--- src/ir/func.rs | 12 ++++++------ src/ir/module.rs | 20 ++++++++++---------- src/ir/value.rs | 2 +- src/ops.rs | 4 ++-- src/pool.rs | 4 ++-- 9 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c6332a0..8293c1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ indexmap = "2.2.2" stacker = "0.1.15" wasm-smith = { version = "0.202", optional = true } paste = "1.0.15" +serde = { version = "1.0.204", features = ["derive"] } [features] default = [] diff --git a/src/entity.rs b/src/entity.rs index 565b56d..99a8669 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -22,7 +22,7 @@ pub trait EntityRef: Clone + Copy + PartialEq + Eq + PartialOrd + Ord + Hash { #[macro_export] macro_rules! declare_entity { ($name:tt, $prefix:tt) => { - #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub struct $name(u32); impl $crate::entity::EntityRef for $name { @@ -73,7 +73,7 @@ macro_rules! declare_entity { }; } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct EntityVec(Vec, PhantomData); impl std::default::Default for EntityVec { @@ -151,7 +151,7 @@ impl IndexMut for EntityVec { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct PerEntity(Vec, PhantomData, T); impl Index for PerEntity { diff --git a/src/ir.rs b/src/ir.rs index bd7a78f..b322507 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -2,7 +2,7 @@ use crate::{declare_entity, entity::EntityRef}; -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub enum Type { I32, I64, diff --git a/src/ir/debug.rs b/src/ir/debug.rs index b19b484..33e1542 100644 --- a/src/ir/debug.rs +++ b/src/ir/debug.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; declare_entity!(SourceFile, "file"); declare_entity!(SourceLoc, "loc"); -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Debug { pub source_files: EntityVec, source_file_dedup: HashMap, @@ -17,7 +17,7 @@ pub struct Debug { source_loc_dedup: HashMap, } -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub struct SourceLocData { pub file: SourceFile, pub line: u32, @@ -46,7 +46,7 @@ impl Debug { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct DebugMap { /// Offset of code section relative to the Wasm file start. pub code_offset: u32, diff --git a/src/ir/func.rs b/src/ir/func.rs index 61ba209..89ec83f 100644 --- a/src/ir/func.rs +++ b/src/ir/func.rs @@ -13,12 +13,12 @@ use std::collections::HashSet; /// A declaration of a function: there is one `FuncDecl` per `Func` /// index. -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub enum FuncDecl<'a> { /// An imported function. Import(Signature, String), /// An un-expanded body that can be lazily expanded if needed. - Lazy(Signature, String, wasmparser::FunctionBody<'a>), + #[serde(skip)]Lazy(Signature, String, wasmparser::FunctionBody<'a>), /// A modified or new function body that requires compilation. Body(Signature, String, FunctionBody), /// A compiled function body (was IR, has been collapsed back to bytecode). @@ -113,7 +113,7 @@ impl<'a> FuncDecl<'a> { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct FunctionBody { /// How many parameters the function has. (Their types are the /// first `n_params` values in `locals`.) @@ -481,7 +481,7 @@ impl FunctionBody { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct BlockDef { /// Instructions in this block. pub insts: Vec, @@ -501,7 +501,7 @@ pub struct BlockDef { pub desc: String, } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct BlockTarget { pub block: Block, pub args: Vec, @@ -518,7 +518,7 @@ impl std::fmt::Display for BlockTarget { } } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum Terminator { Br { target: BlockTarget, diff --git a/src/ir/module.rs b/src/ir/module.rs index dc7c37a..6af9fe1 100644 --- a/src/ir/module.rs +++ b/src/ir/module.rs @@ -7,7 +7,7 @@ use indexmap::IndexMap; pub use crate::frontend::FrontendOptions; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Module<'a> { pub orig_bytes: &'a [u8], pub funcs: EntityVec>, @@ -23,13 +23,13 @@ pub struct Module<'a> { pub custom_sections: IndexMap>, } -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub struct SignatureData { pub params: Vec, pub returns: Vec, } -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub struct MemoryData { pub initial_pages: usize, pub maximum_pages: Option, @@ -38,13 +38,13 @@ pub struct MemoryData { pub shared: bool, } -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub struct MemorySegment { pub offset: usize, pub data: Vec, } -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub struct TableData { pub ty: Type, pub initial: u64, @@ -52,7 +52,7 @@ pub struct TableData { pub func_elements: Option>, } -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] pub struct GlobalData { pub ty: Type, pub value: Option, @@ -81,14 +81,14 @@ impl From for SignatureData { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Import { pub module: String, pub name: String, pub kind: ImportKind, } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum ImportKind { Table(Table), Func(Func), @@ -108,13 +108,13 @@ impl std::fmt::Display for ImportKind { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Export { pub name: String, pub kind: ExportKind, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub enum ExportKind { Table(Table), Func(Func), diff --git a/src/ir/value.rs b/src/ir/value.rs index 67072fe..ef6d5bf 100644 --- a/src/ir/value.rs +++ b/src/ir/value.rs @@ -2,7 +2,7 @@ use super::{Block, Type, Value}; use crate::pool::{ListPool, ListRef}; use crate::Operator; -#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum ValueDef { BlockParam(Block, u32, Type), Operator(Operator, ListRef, ListRef), diff --git a/src/ops.rs b/src/ops.rs index dd1bc86..2dbfcba 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -5,7 +5,7 @@ use anyhow::Context; use std::convert::TryFrom; pub use wasmparser::{Ieee32, Ieee64}; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub struct MemoryArg { pub align: u32, pub offset: u64 , @@ -23,7 +23,7 @@ impl std::fmt::Display for MemoryArg { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum Operator { Unreachable, Nop, diff --git a/src/pool.rs b/src/pool.rs index cc60d80..387f1a1 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -5,7 +5,7 @@ use std::fmt::Debug; use std::marker::PhantomData; use std::ops::{Index, IndexMut}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct ListPool { pub storage: Vec, } @@ -16,7 +16,7 @@ impl Default for ListPool { } } -#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, serde::Serialize, serde::Deserialize)] pub struct ListRef(u32, u32, PhantomData); impl Default for ListRef {