holey-bytes/depell/wasm-hbc/src/lib.rs

109 lines
3.3 KiB
Rust
Raw Normal View History

2024-10-10 12:01:12 -05:00
#![feature(alloc_error_handler)]
2024-10-12 06:07:49 -05:00
#![feature(slice_take)]
2024-10-10 12:01:12 -05:00
#![no_std]
use {
2024-10-12 06:07:49 -05:00
alloc::{string::String, vec::Vec},
2024-10-10 12:01:12 -05:00
hblang::{codegen::Codegen, parser::FileId},
};
extern crate alloc;
const ARENA_CAP: usize = 128 * 16 * 1024;
wasm_rt::decl_runtime!(ARENA_CAP, 1024 * 4);
2024-10-10 12:01:12 -05:00
2024-10-12 06:07:49 -05:00
const MAX_INPUT_SIZE: usize = 32 * 4 * 1024;
wasm_rt::decl_buffer!(MAX_INPUT_SIZE, MAX_INPUT, INPUT, INPUT_LEN);
2024-10-10 12:01:12 -05:00
#[no_mangle]
2024-10-12 06:07:49 -05:00
unsafe fn compile_and_run(mut fuel: usize) {
2024-10-10 12:01:12 -05:00
ALLOCATOR.reset();
2024-10-12 14:25:37 -05:00
_ = log::set_logger(&wasm_rt::Logger);
2024-10-12 06:07:49 -05:00
log::set_max_level(log::LevelFilter::Error);
2024-10-10 12:01:12 -05:00
struct File<'a> {
path: &'a str,
code: &'a mut str,
}
2024-10-10 12:01:12 -05:00
let mut root = 0;
2024-10-10 12:01:12 -05:00
let files = {
let mut input_bytes =
core::slice::from_raw_parts_mut(core::ptr::addr_of_mut!(INPUT).cast::<u8>(), INPUT_LEN);
let mut files = Vec::with_capacity(32);
while let Some((&mut path_len, rest)) = input_bytes.split_first_chunk_mut() {
let (path, rest) = rest.split_at_mut(u16::from_le_bytes(path_len) as usize);
let (&mut code_len, rest) = rest.split_first_chunk_mut().unwrap();
let (code, rest) = rest.split_at_mut(u16::from_le_bytes(code_len) as usize);
files.push(File {
path: core::str::from_utf8_unchecked(path),
code: core::str::from_utf8_unchecked_mut(code),
});
input_bytes = rest;
}
2024-10-12 06:07:49 -05:00
let root_path = files[root].path;
2024-10-12 06:07:49 -05:00
hblang::quad_sort(&mut files, |a, b| a.path.cmp(b.path));
root = files.binary_search_by_key(&root_path, |p| p.path).unwrap();
2024-10-10 12:01:12 -05:00
files
};
let files = {
let mut ctx = hblang::parser::Ctx::default();
2024-10-10 12:01:12 -05:00
let paths = files.iter().map(|f| f.path).collect::<Vec<_>>();
2024-10-13 08:22:16 -05:00
let mut loader = |path: &str, _: &str, kind| match kind {
hblang::parser::FileKind::Module => Ok(paths.binary_search(&path).unwrap() as FileId),
hblang::parser::FileKind::Embed => Err("embeds are not supported".into()),
};
2024-10-10 12:01:12 -05:00
files
.into_iter()
.map(|f| {
hblang::parser::Ast::new(
f.path,
// since 'free' does nothing this is fine
String::from_raw_parts(f.code.as_mut_ptr(), f.code.len(), f.code.len()),
&mut ctx,
&mut loader,
)
})
.collect::<Vec<_>>()
};
2024-10-12 06:07:49 -05:00
let mut ct = {
2024-10-10 12:01:12 -05:00
let mut c = Codegen::default();
c.files = files;
c.generate(root as FileId);
2024-10-12 06:07:49 -05:00
c.assemble_comptime()
2024-10-10 12:01:12 -05:00
};
2024-10-12 06:07:49 -05:00
while fuel != 0 {
match ct.vm.run() {
Ok(hbvm::VmRunOk::End) => {
2024-10-14 15:04:18 -05:00
log::error!("exit code: {}", ct.vm.read_reg(1).0 as i64);
2024-10-12 06:07:49 -05:00
break;
}
Ok(hbvm::VmRunOk::Ecall) => {
let unknown = ct.vm.read_reg(2).0;
log::error!("unknown ecall: {unknown}")
}
2024-10-12 14:25:37 -05:00
Ok(hbvm::VmRunOk::Timer) => {
fuel -= 1;
if fuel == 0 {
log::error!("program timed out");
}
}
2024-10-12 06:07:49 -05:00
Ok(hbvm::VmRunOk::Breakpoint) => todo!(),
Err(e) => {
log::error!("vm error: {e}");
break;
}
}
}
2024-10-13 13:01:18 -05:00
//log::error!("memory consumption: {}b / {}b", ALLOCATOR.used(), ARENA_CAP);
}