From 871398ff821255debdbd9452e46ca55a05e8cb92 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Fri, 1 Dec 2023 14:00:46 +0100 Subject: feat: wip: seperate parser and types crates Signed-off-by: Henry Gressmann --- crates/types/src/instructions.rs | 209 +++++++++++++++++++++++++++++++++++++++ crates/types/src/lib.rs | 99 +++++++++++++++++++ 2 files changed, 308 insertions(+) create mode 100644 crates/types/src/instructions.rs create mode 100644 crates/types/src/lib.rs (limited to 'crates/types/src') diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs new file mode 100644 index 0000000..993b260 --- /dev/null +++ b/crates/types/src/instructions.rs @@ -0,0 +1,209 @@ +use super::{FuncAddr, GlobalAddr, LabelAddr, LocalAddr, TableAddr, TypeAddr, ValType}; +use alloc::{format, vec::Vec}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum BlockArgs { + Empty, + Type(ValType), + FuncType(u32), +} + +/// Represents a memory immediate in a WebAssembly memory instruction. +#[derive(Debug, Copy, Clone)] +pub struct MemArg { + pub align: u8, + pub offset: u64, +} + +/// A WebAssembly Instruction +/// See https://webassembly.github.io/spec/core/binary/instructions.html +/// Currently includes all instructions from the MVP (1.0) spec +pub enum Instruction { + // Control Instructions + // See https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions + Unreachable, + Nop, + Block(BlockArgs), + Loop(BlockArgs), + If(BlockArgs), + Else, + End, + Br(LabelAddr), + BrIf(LabelAddr), + BrTable(Vec, LabelAddr), // not to spec, instead of a vector of labels, we have a label and a count + Return, + Call(FuncAddr), + CallIndirect(TypeAddr, TableAddr), + + // Parametric Instructions + // See https://webassembly.github.io/spec/core/binary/instructions.html#parametric-instructions + Drop, + Select, + + // Variable Instructions + // See https://webassembly.github.io/spec/core/binary/instructions.html#variable-instructions + LocalGet(LocalAddr), + LocalSet(LocalAddr), + LocalTee(LocalAddr), + GlobalGet(GlobalAddr), + GlobalSet(GlobalAddr), + + // Memory Instructions + I32Load(MemArg), + I64Load(MemArg), + F32Load(MemArg), + F64Load(MemArg), + I32Load8S(MemArg), + I32Load8U(MemArg), + I32Load16S(MemArg), + I32Load16U(MemArg), + I64Load8S(MemArg), + I64Load8U(MemArg), + I64Load16S(MemArg), + I64Load16U(MemArg), + I64Load32S(MemArg), + I64Load32U(MemArg), + I32Store(MemArg), + I64Store(MemArg), + F32Store(MemArg), + F64Store(MemArg), + I32Store8(MemArg), + I32Store16(MemArg), + I64Store8(MemArg), + I64Store16(MemArg), + I64Store32(MemArg), + MemorySize, + MemoryGrow, + + // Constants + I32Const(i32), + I64Const(i64), + F32Const(f32), + F64Const(f64), + + // Numeric Instructions + // See https://webassembly.github.io/spec/core/binary/instructions.html#numeric-instructions + I32Eqz, + I32Eq, + I32Ne, + I32LtS, + I32LtU, + I32GtS, + I32GtU, + I32LeS, + I32LeU, + I32GeS, + I32GeU, + I64Eqz, + I64Eq, + I64Ne, + I64LtS, + I64LtU, + I64GtS, + I64GtU, + I64LeS, + I64LeU, + I64GeS, + I64GeU, + F32Eq, + F32Ne, + F32Lt, + F32Gt, + F32Le, + F32Ge, + F64Eq, + F64Ne, + F64Lt, + F64Gt, + F64Le, + F64Ge, + I32Clz, + I32Ctz, + I32Popcnt, + I32Add, + I32Sub, + I32Mul, + I32DivS, + I32DivU, + I32RemS, + I32RemU, + I32And, + I32Or, + I32Xor, + I32Shl, + I32ShrS, + I32ShrU, + I32Rotl, + I32Rotr, + I64Clz, + I64Ctz, + I64Popcnt, + I64Add, + I64Sub, + I64Mul, + I64DivS, + I64DivU, + I64RemS, + I64RemU, + I64And, + I64Or, + I64Xor, + I64Shl, + I64ShrS, + I64ShrU, + I64Rotl, + I64Rotr, + F32Abs, + F32Neg, + F32Ceil, + F32Floor, + F32Trunc, + F32Nearest, + F32Sqrt, + F32Add, + F32Sub, + F32Mul, + F32Div, + F32Min, + F32Max, + F32Copysign, + F64Abs, + F64Neg, + F64Ceil, + F64Floor, + F64Trunc, + F64Nearest, + F64Sqrt, + F64Add, + F64Sub, + F64Mul, + F64Div, + F64Min, + F64Max, + F64Copysign, + I32WrapI64, + I32TruncF32S, + I32TruncF32U, + I32TruncF64S, + I32TruncF64U, + I64ExtendI32S, + I64ExtendI32U, + I64TruncF32S, + I64TruncF32U, + I64TruncF64S, + I64TruncF64U, + F32ConvertI32S, + F32ConvertI32U, + F32ConvertI64S, + F32ConvertI64U, + F32DemoteF64, + F64ConvertI32S, + F64ConvertI32U, + F64ConvertI64S, + F64ConvertI64U, + F64PromoteF32, + I32ReinterpretF32, + I64ReinterpretF64, + F32ReinterpretI32, + F64ReinterpretI64, +} diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs new file mode 100644 index 0000000..e68efe7 --- /dev/null +++ b/crates/types/src/lib.rs @@ -0,0 +1,99 @@ +extern crate alloc; + +mod instructions; +pub use instructions::*; + +/// A WebAssembly value. +/// See https://webassembly.github.io/spec/core/syntax/types.html#value-types +#[derive(Debug, Clone, PartialEq)] +pub enum WasmValue { + // Num types + I32(i32), + I64(i64), + F32(f32), + F64(f64), + + // Vec types + V128(i128), +} + +/// Type of a WebAssembly value. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ValType { + I32, + I64, + F32, + F64, + V128, + FuncRef, + ExternRef, +} + +/// A WebAssembly External Kind. +/// See https://webassembly.github.io/spec/core/syntax/types.html#external-types +#[derive(Debug, Clone, PartialEq)] +pub enum ExternalKind { + Func, + Table, + Memory, + Global, +} + +/// A WebAssembly Address. +/// These are indexes into the respective stores. +/// See https://webassembly.github.io/spec/core/exec/runtime.html#addresses +pub type Addr = u32; +pub type FuncAddr = Addr; +pub type TableAddr = Addr; +pub type MemAddr = Addr; +pub type GlobalAddr = Addr; +pub type ElmAddr = Addr; +pub type DataAddr = Addr; +pub type ExternAddr = Addr; +// additional internal addresses +pub type TypeAddr = Addr; +pub type LocalAddr = Addr; +pub type LabelAddr = Addr; + +/// A WebAssembly Export Instance. +/// https://webassembly.github.io/spec/core/exec/runtime.html#export-instances +#[derive(Debug)] +pub struct ExportInst { + pub name: String, + pub value: ExternVal, +} + +/// A WebAssembly External Value. +/// https://webassembly.github.io/spec/core/exec/runtime.html#external-values +#[derive(Debug)] +pub enum ExternVal { + Func(FuncAddr), + Table(TableAddr), + Mem(MemAddr), + Global(GlobalAddr), +} + +/// The type of a WebAssembly Function. +/// See https://webassembly.github.io/spec/core/syntax/types.html#function-types +#[derive(Debug, Clone, PartialEq)] +pub struct FuncType { + pub params: Box<[ValType]>, + pub results: Box<[ValType]>, +} + +/// A WebAssembly Function +pub struct Function { + pub locals: Box<[ValType]>, + pub body: Box<[Instruction]>, +} + +/// A WebAssembly Module Export +#[derive(Debug)] +pub struct Export { + /// The name of the export. + pub name: Box, + /// The kind of the export. + pub kind: ExternalKind, + /// The index of the exported item. + pub index: u32, +} -- cgit v1.3.1