master
able 2022-07-25 07:01:51 -05:00
commit 847ba30761
6 changed files with 120 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

25
Cargo.lock generated Normal file
View File

@ -0,0 +1,25 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "wat2wasm"
version = "0.1.0"
dependencies = [
"log",
]

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "wat2wasm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4.17"

10
assets/example.wasm.txt Normal file
View File

@ -0,0 +1,10 @@
00000000: 0061 736d
00000004: 0100 0000
00000008: 0105 0160
0000000c: 0001 7f03
00000010: 0201 0007
00000014: 0801 046d
00000018: 6169 6e00
0000001c: 000a 0701
00000020: 0500 412a
00000024: 0f0b

5
assets/example.wat Normal file
View File

@ -0,0 +1,5 @@
(module
(func (export "main")
(result i32)
i32.const 42
return))

70
src/main.rs Normal file
View File

@ -0,0 +1,70 @@
#![warn(missing_docs)]
//! A compliant wat compiler
//!
//! # Refs
//! https://webassembly.github.io/spec/core/
//! https://blog.ttulka.com/learning-webassembly-2-wasm-binary-format
use log::*;
pub const WASM_MAGIC: &[u8; 4] = b"\0asm";
pub const WASM_VERSION: &[u8; 4] = b"1000";
#[non_exhaustive]
pub enum WasmTypes {
/// https://webassembly.github.io/spec/core/syntax/types.html#number-types
Number32(i32),
Number64(i64),
Float32(f32),
Float64(f64),
/// https://webassembly.github.io/spec/core/syntax/types.html#vector-types
Vector128,
}
#[non_exhaustive]
pub struct FunctionSignature {
pub parameters: Vec<WasmTypes>,
pub return_count: Vec<WasmTypes>,
}
pub enum SectionType {
Type,
Function(FunctionSignature),
Export,
}
pub struct Section {
stype: SectionType,
size: u64,
}
pub struct Program {
pub sections: Vec<Section>,
}
impl Program {
pub fn to_binary(&self) -> Vec<u8> {
let mut bin: Vec<u8> = vec![];
for byte in WASM_MAGIC {
bin.push(*byte);
}
trace!("Added WASM magic value: {:?}", WASM_MAGIC);
for byte in WASM_VERSION {
bin.push(*byte);
}
trace!("Added WASM version value: {:?}", WASM_VERSION);
bin
}
}
fn main() {
let program = Program {
// NOTE: If wasm ever gets a v2 this should probably be handled properly
sections: vec![],
};
let pbin = program.to_binary();
println!("{:?}", pbin);
}