mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 16:21:11 +00:00
Bugfixes: generate valid wasm for gen_add_i64
Still need to create test_wrapper
This commit is contained in:
parent
08e242967a
commit
13a969a238
4 changed files with 46 additions and 21 deletions
3
compiler/gen_wasm/.gitignore
vendored
Normal file
3
compiler/gen_wasm/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
*.wasm
|
||||||
|
*.wat
|
||||||
|
/notes.md
|
|
@ -12,14 +12,16 @@ use roc_mono::layout::{Builtin, Layout};
|
||||||
// Follow Emscripten's example by using 1kB (4 bytes would probably do)
|
// Follow Emscripten's example by using 1kB (4 bytes would probably do)
|
||||||
const UNUSED_DATA_SECTION_BYTES: u32 = 1024;
|
const UNUSED_DATA_SECTION_BYTES: u32 = 1024;
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct LocalId(u32);
|
struct LocalId(u32);
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct LabelId(u32);
|
struct LabelId(u32);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct SymbolStorage(LocalId, WasmLayout);
|
struct SymbolStorage(LocalId, WasmLayout);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct WasmLayout {
|
struct WasmLayout {
|
||||||
value_type: ValueType,
|
value_type: ValueType,
|
||||||
stack_memory: u32,
|
stack_memory: u32,
|
||||||
|
@ -154,7 +156,7 @@ impl<'a> WasmBackend<'a> {
|
||||||
fn get_symbol_storage(&self, sym: &Symbol) -> Result<&SymbolStorage, String> {
|
fn get_symbol_storage(&self, sym: &Symbol) -> Result<&SymbolStorage, String> {
|
||||||
self.symbol_storage_map
|
self.symbol_storage_map
|
||||||
.get(sym)
|
.get(sym)
|
||||||
.ok_or(format!("Symbol not found in function scope {:?}", sym))
|
.ok_or_else(|| format!("Symbol {:?} not found in function scope:\n{:?}", sym, self.symbol_storage_map))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_from_symbol(&mut self, sym: &Symbol) -> Result<(), String> {
|
fn load_from_symbol(&mut self, sym: &Symbol) -> Result<(), String> {
|
||||||
|
@ -164,21 +166,13 @@ impl<'a> WasmBackend<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store whatever value is on top of the VM's stack
|
|
||||||
fn store_to_symbol(&mut self, sym: &Symbol) -> Result<(), String> {
|
|
||||||
let SymbolStorage(LocalId(local_id), _) = self.get_symbol_storage(sym)?;
|
|
||||||
let id: u32 = *local_id;
|
|
||||||
self.instructions.push(SetLocal(id));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) -> Result<(), String> {
|
fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) -> Result<(), String> {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::Let(sym, expr, layout, following) => {
|
Stmt::Let(sym, expr, layout, following) => {
|
||||||
self.build_expr(sym, expr, layout)?;
|
|
||||||
|
|
||||||
let wasm_layout = WasmLayout::new(layout)?;
|
let wasm_layout = WasmLayout::new(layout)?;
|
||||||
let local_id = self.insert_local(wasm_layout, *sym);
|
let local_id = self.insert_local(wasm_layout, *sym);
|
||||||
|
|
||||||
|
self.build_expr(sym, expr, layout)?;
|
||||||
self.instructions.push(SetLocal(local_id.0));
|
self.instructions.push(SetLocal(local_id.0));
|
||||||
|
|
||||||
self.build_stmt(following, ret_layout)?;
|
self.build_stmt(following, ret_layout)?;
|
||||||
|
@ -222,12 +216,11 @@ impl<'a> WasmBackend<'a> {
|
||||||
func_sym, sym
|
func_sym, sym
|
||||||
))?;
|
))?;
|
||||||
self.instructions.push(Call(function_location.body));
|
self.instructions.push(Call(function_location.body));
|
||||||
self.store_to_symbol(sym)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
CallType::LowLevel { op: lowlevel, .. } => {
|
CallType::LowLevel { op: lowlevel, .. } => {
|
||||||
self.build_call_low_level(sym, lowlevel, arguments, layout)
|
self.build_call_low_level(lowlevel, arguments, layout)
|
||||||
}
|
}
|
||||||
x => Err(format!("the call type, {:?}, is not yet implemented", x)),
|
x => Err(format!("the call type, {:?}, is not yet implemented", x)),
|
||||||
},
|
},
|
||||||
|
@ -253,7 +246,6 @@ impl<'a> WasmBackend<'a> {
|
||||||
|
|
||||||
fn build_call_low_level(
|
fn build_call_low_level(
|
||||||
&mut self,
|
&mut self,
|
||||||
sym: &Symbol,
|
|
||||||
lowlevel: &LowLevel,
|
lowlevel: &LowLevel,
|
||||||
args: &'a [Symbol],
|
args: &'a [Symbol],
|
||||||
layout: &Layout<'a>,
|
layout: &Layout<'a>,
|
||||||
|
@ -263,7 +255,6 @@ impl<'a> WasmBackend<'a> {
|
||||||
}
|
}
|
||||||
let wasm_layout = WasmLayout::new(layout)?;
|
let wasm_layout = WasmLayout::new(layout)?;
|
||||||
self.build_instructions_lowlevel(lowlevel, wasm_layout.value_type)?;
|
self.build_instructions_lowlevel(lowlevel, wasm_layout.value_type)?;
|
||||||
self.store_to_symbol(sym)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,6 @@ pub fn build_module<'a>(
|
||||||
let mut backend = WasmBackend::new();
|
let mut backend = WasmBackend::new();
|
||||||
let mut layout_ids = LayoutIds::default();
|
let mut layout_ids = LayoutIds::default();
|
||||||
|
|
||||||
let mut exports = std::vec::Vec::new();
|
|
||||||
for ((sym, layout), proc) in procedures {
|
for ((sym, layout), proc) in procedures {
|
||||||
let function_index = backend.build_proc(proc, sym)?;
|
let function_index = backend.build_proc(proc, sym)?;
|
||||||
if env.exposed_to_host.contains(&sym) {
|
if env.exposed_to_host.contains(&sym) {
|
||||||
|
@ -37,9 +36,24 @@ pub fn build_module<'a>(
|
||||||
.with_internal(Internal::Function(function_index))
|
.with_internal(Internal::Function(function_index))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
exports.push(export);
|
backend.builder.push_export(export);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MIN_MEMORY_SIZE_KB: u32 = 1024;
|
||||||
|
const PAGE_SIZE_KB: u32 = 64;
|
||||||
|
|
||||||
|
let memory = builder::MemoryBuilder::new()
|
||||||
|
.with_min(MIN_MEMORY_SIZE_KB / PAGE_SIZE_KB)
|
||||||
|
.build();
|
||||||
|
backend.builder.push_memory(memory);
|
||||||
|
|
||||||
|
let memory_export = builder::export()
|
||||||
|
.field("memory")
|
||||||
|
.with_internal(Internal::Memory(0))
|
||||||
|
.build();
|
||||||
|
backend.builder.push_export(memory_export);
|
||||||
|
|
||||||
let module = backend.builder.build();
|
let module = backend.builder.build();
|
||||||
module
|
module
|
||||||
.to_bytes()
|
.to_bytes()
|
||||||
|
|
|
@ -67,6 +67,22 @@ pub fn helper_wasm<'a>(
|
||||||
procedures.insert(key, proc);
|
procedures.insert(key, proc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// You can comment and uncomment this block out to get more useful information
|
||||||
|
// while you're working on the wasm backend!
|
||||||
|
// {
|
||||||
|
// println!("=========== Procedures ==========");
|
||||||
|
// println!("{:?}", procedures);
|
||||||
|
// println!("=================================\n");
|
||||||
|
|
||||||
|
// println!("=========== Interns ==========");
|
||||||
|
// println!("{:?}", interns);
|
||||||
|
// println!("=================================\n");
|
||||||
|
|
||||||
|
// println!("=========== Exposed ==========");
|
||||||
|
// println!("{:?}", exposed_to_host);
|
||||||
|
// println!("=================================\n");
|
||||||
|
// }
|
||||||
|
|
||||||
let exposed_to_host = exposed_to_host.keys().copied().collect::<MutSet<_>>();
|
let exposed_to_host = exposed_to_host.keys().copied().collect::<MutSet<_>>();
|
||||||
|
|
||||||
let env = gen_wasm::Env {
|
let env = gen_wasm::Env {
|
||||||
|
@ -80,7 +96,8 @@ pub fn helper_wasm<'a>(
|
||||||
// for debugging (e.g. with wasm2wat)
|
// for debugging (e.g. with wasm2wat)
|
||||||
if false {
|
if false {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
let mut file = std::fs::File::create("/home/folkertdev/roc/wasm/manual.wasm").unwrap();
|
let mut file =
|
||||||
|
std::fs::File::create("/home/brian/Documents/roc/compiler/gen_wasm/debug.wasm").unwrap();
|
||||||
file.write_all(&module_bytes).unwrap();
|
file.write_all(&module_bytes).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +107,7 @@ pub fn helper_wasm<'a>(
|
||||||
|
|
||||||
let store = Store::default();
|
let store = Store::default();
|
||||||
// let module = Module::from_file(&store, &test_wasm_path).unwrap();
|
// let module = Module::from_file(&store, &test_wasm_path).unwrap();
|
||||||
let module = Module::new(&store, &module_bytes).unwrap();
|
let module = Module::from_binary(&store, &module_bytes).unwrap();
|
||||||
|
|
||||||
// First, we create the `WasiEnv`
|
// First, we create the `WasiEnv`
|
||||||
use wasmer_wasi::WasiState;
|
use wasmer_wasi::WasiState;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue