Skip to content

Commit

Permalink
feat: raw wasm tips
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Sep 12, 2023
1 parent eeeb2c2 commit dd022f9
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 22 deletions.
73 changes: 71 additions & 2 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ pub enum ProgramStmt {
stmt: Stmt,
span: Span,
},
Tip {
tip: Tip,
span: Span,
},
}

impl ProgramStmt {
Expand All @@ -355,6 +359,7 @@ impl ProgramStmt {
ProgramStmt::StructDecl { span, .. } => span.clone(),

Check warning on line 359 in ast/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `Span` which implements the `Copy` trait

warning: using `clone` on type `Span` which implements the `Copy` trait --> ast/src/lib.rs:359:47 | 359 | ProgramStmt::StructDecl { span, .. } => span.clone(), | ^^^^^^^^^^^^ help: try dereferencing it: `*span` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
ProgramStmt::TypeDecl { span, .. } => span.clone(),

Check warning on line 360 in ast/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `Span` which implements the `Copy` trait

warning: using `clone` on type `Span` which implements the `Copy` trait --> ast/src/lib.rs:360:45 | 360 | ProgramStmt::TypeDecl { span, .. } => span.clone(), | ^^^^^^^^^^^^ help: try dereferencing it: `*span` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
ProgramStmt::Stmt { span, .. } => span.clone(),

Check warning on line 361 in ast/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `Span` which implements the `Copy` trait

warning: using `clone` on type `Span` which implements the `Copy` trait --> ast/src/lib.rs:361:41 | 361 | ProgramStmt::Stmt { span, .. } => span.clone(), | ^^^^^^^^^^^^ help: try dereferencing it: `*span` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
ProgramStmt::Tip { span, .. } => span.clone(),

Check warning on line 362 in ast/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `Span` which implements the `Copy` trait

warning: using `clone` on type `Span` which implements the `Copy` trait --> ast/src/lib.rs:362:40 | 362 | ProgramStmt::Tip { span, .. } => span.clone(), | ^^^^^^^^^^^^ help: try dereferencing it: `*span` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ whistle_common = { path = "../common/" }
whistle_parser = { path = "../parser/" }
whistle_ast = { path = "../ast/" }
wasm-encoder = "0.32.0"
wast = "64.0.0"
wasmprinter = "0.2.64"
wasmparser = "0.112.0"
byteorder = "1.4.3"
15 changes: 8 additions & 7 deletions compiler/src/compilers/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ pub fn compile_program(compiler: &mut Compiler, program: ProgramStmt) {
imp_type: _imp_type,
..
} => {}
_ => {
compiler
_ => compiler
.handler
.throw(CompilerErrorKind::Unimplemented, program.span())},
.throw(CompilerErrorKind::Unimplemented, program.span()),
}
}

Expand All @@ -61,22 +60,24 @@ pub fn compile_fn(
stmts: Vec<Stmt>,
) {
// TODO: Inline functions, would be done with a new field in the Compiler struct
let sym = compiler.get_sym(&ident).unwrap().clone();
compiler.scope.enter_curr_scope();

let mut types = Vec::new();
for param in params {
types.push(ident_type_to_val_type(param.type_ident.to_type()));
}

let encoded_ret_type = if let IdentType::Primitive { .. } = ret_type {
let ret_type = ret_type.to_type();

let encoded_ret_type = if ret_type == Type::Primitive(whistle_common::Primitive::None) {
vec![]
} else {
vec![ident_type_to_val_type(ret_type.to_type())]
vec![ident_type_to_val_type(ret_type)]
};

compiler.module.types.function(types, encoded_ret_type);
let sym = compiler.get_sym(&ident).unwrap().clone();
compiler.module.fns.function(sym.0);
compiler.module.types.function(types, encoded_ret_type);
if export {
compiler.module.exports.export(
if &ident == "main" { "_start" } else { &ident },
Expand Down
33 changes: 27 additions & 6 deletions compiler/src/compilers/tip.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
use whistle_common::{Tip, Span};
use whistle_common::Tip;

use crate::{Compiler, Function};

pub fn compile_tip_wasm_bytes(
_compiler: &mut Compiler,
function: &mut Function,
tip: Tip,
_span: whistle_common::Span,
) {
let raw_data = tip
.value
.split(",")
.map(|s| s.trim())
.collect::<Vec<&str>>();
let data = raw_data
.iter()
.map(|s| s.parse::<u8>().unwrap())
.collect::<Vec<u8>>();
function.raw(data);
}

pub fn compile_tip_wasm_bytes(_compiler: &mut Compiler, function: &mut Function, tip: Tip, _span: Span) {
let raw_data = tip.value.split(",").map(|s| s.trim()).collect::<Vec<&str>>();
let data = raw_data.iter().map(|s| s.parse::<u8>().unwrap()).collect::<Vec<u8>>();
function.raw(data);
}
pub fn compile_tip_wast(compiler: &mut Compiler, tip: Tip, _span: whistle_common::Span) {
let lexer = wast::lexer::Lexer::new(tip.value.as_str());
let buf = wast::parser::ParseBuffer::new_with_lexer(lexer).unwrap();
let ast = wast::parser::parse::<wast::Wat>(&buf)
.unwrap()
.encode()
.unwrap();
compiler.module.code.raw(&ast);
}
6 changes: 1 addition & 5 deletions examples/add.whi
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
fn _add(a: u32, b: u32): u32 {
export fn add(a: i32, b: i32): i32 {
return a + b
}

export fn add(a: u32, b: u32): u32 {
return _add(a, b)
}
7 changes: 5 additions & 2 deletions examples/tip.whi
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export fn test(x: i32) {
#(wasm_bytes) 0,15,11
export fn test() {
#(wasm_bytes) {
0,15,11
}

}

0 comments on commit dd022f9

Please sign in to comment.