Skip to content

Commit

Permalink
serialize tables into json
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed Jul 18, 2022
1 parent 37f89ae commit 2236c75
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions specs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ edition = "2021"
[dependencies]
num-bigint = { version = "0.4", features = ["rand"] }
parity-wasm = { version = "0.42.0", default-features = false }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = "0.24.1"
strum_macros = "0.24.1"
10 changes: 9 additions & 1 deletion specs/src/etable.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
use serde::Serialize;

use crate::step::StepInfo;

use super::itable::InstructionTableEntry;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct EventTableEntry {
pub eid: u64,
pub sp: u64,
pub last_jump_eid: u64,
pub inst: InstructionTableEntry,
pub step_info: StepInfo,
}

impl EventTableEntry {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
9 changes: 9 additions & 0 deletions specs/src/imtable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use serde::Serialize;

#[derive(Serialize)]
pub struct InitMemoryTableEntry {
pub mmid: u64,
pub offset: u64,
pub value: u64,
}

impl InitMemoryTableEntry {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
13 changes: 9 additions & 4 deletions specs/src/itable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::mtable::VarType;
use crate::types::ValueType;
use num_bigint::BigUint;
use serde::Serialize;
use std::collections::HashSet;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -37,18 +38,18 @@ impl OpcodeClass {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum BinOp {
Add,
Or,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum RelOp {
Ne,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum Opcode {
LocalGet { vtype: VarType, offset: u64 },
Const { vtype: VarType, value: u64 },
Expand Down Expand Up @@ -130,7 +131,7 @@ impl Into<OpcodeClass> for Opcode {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct InstructionTableEntry {
pub moid: u16,
pub mmid: u16,
Expand All @@ -140,6 +141,10 @@ pub struct InstructionTableEntry {
}

impl InstructionTableEntry {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}

pub fn encode_instruction_address(&self) -> BigUint {
let mut bn = BigUint::from(0u64);
bn += self.moid;
Expand Down
10 changes: 7 additions & 3 deletions specs/src/jtable.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use num_bigint::BigUint;

use super::itable::InstructionTableEntry;
use num_bigint::BigUint;
use serde::Serialize;

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct JumpTableEntry {
// caller eid (unique)
pub eid: u64,
Expand All @@ -11,6 +11,10 @@ pub struct JumpTableEntry {
}

impl JumpTableEntry {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}

pub fn encode(&self) -> BigUint {
let mut bn = BigUint::from(self.eid);
bn = bn << 16;
Expand Down
43 changes: 41 additions & 2 deletions specs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{env, io::Write, path::PathBuf};

use mtable::MTable;
use serde::Serialize;

use self::{
etable::EventTableEntry, imtable::InitMemoryTableEntry, itable::InstructionTableEntry,
Expand All @@ -13,15 +16,51 @@ pub mod mtable;
pub mod step;
pub mod types;

#[derive(Default)]
#[derive(Default, Serialize)]
pub struct CompileTable {
pub itable: Vec<InstructionTableEntry>,
pub imtable: Vec<InitMemoryTableEntry>,
}

#[derive(Default)]
impl CompileTable {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}

#[derive(Default, Serialize)]
pub struct ExecutionTable {
pub etable: Vec<EventTableEntry>,
pub mtable: MTable,
pub jtable: Vec<JumpTableEntry>,
}

impl ExecutionTable {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}

fn write_file(folder: &PathBuf, filename: &str, buf: &String) {
let mut folder = folder.clone();
folder.push(filename);
let mut fd = std::fs::File::create(folder.as_path()).unwrap();
folder.pop();

fd.write(buf.as_bytes()).unwrap();
}

pub fn write_json(compile_table: &CompileTable, execution_table: &ExecutionTable) {
let itable = serde_json::to_string(&compile_table.itable).unwrap();
let imtable = serde_json::to_string(&compile_table.imtable).unwrap();
let etable = serde_json::to_string(&execution_table.etable).unwrap();
let mtable = serde_json::to_string(&execution_table.mtable).unwrap();
let jtable = serde_json::to_string(&execution_table.jtable).unwrap();

let dir = env::current_dir().unwrap();
write_file(&dir, "itable.data", &itable);
write_file(&dir, "imtable.data", &imtable);
write_file(&dir, "etable.data", &etable);
write_file(&dir, "mtable.data", &mtable);
write_file(&dir, "jtable.data", &jtable);
}
19 changes: 14 additions & 5 deletions specs/src/mtable.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use serde::Serialize;
use strum_macros::EnumIter;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub enum LocationType {
Heap = 0,
Stack = 1,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub enum AccessType {
Read = 1,
Write = 2,
Init = 3,
}

#[derive(Clone, Copy, Debug, PartialEq, EnumIter)]
#[derive(Clone, Copy, Debug, PartialEq, EnumIter, Serialize)]
pub enum VarType {
U8 = 1,
I8,
Expand Down Expand Up @@ -60,7 +61,7 @@ impl VarType {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct MemoryTableEntry {
pub eid: u64,
// emid is small memory id of eid,
Expand All @@ -77,15 +78,23 @@ pub struct MemoryTableEntry {
}

impl MemoryTableEntry {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}

pub fn is_same_location(&self, other: &MemoryTableEntry) -> bool {
self.mmid == other.mmid && self.offset == other.offset && self.ltype == other.ltype
}
}

#[derive(Default, Debug)]
#[derive(Default, Debug, Serialize)]
pub struct MTable(Vec<MemoryTableEntry>);

impl MTable {
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}

pub fn new(entries: Vec<MemoryTableEntry>) -> Self {
let mut mtable = MTable(entries);
mtable.sort();
Expand Down
3 changes: 2 additions & 1 deletion specs/src/step.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{mtable::VarType, types::ValueType};
use serde::Serialize;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub enum StepInfo {
BrIfNez {
value: i32,
Expand Down
4 changes: 3 additions & 1 deletion specs/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Clone, Copy, Debug)]
use serde::Serialize;

#[derive(Clone, Copy, Debug, Serialize)]
pub enum ValueType {
I32,
I64,
Expand Down

0 comments on commit 2236c75

Please sign in to comment.