Skip to content

Commit

Permalink
indexing branch
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Oct 7, 2024
1 parent 9cb0fd5 commit b45e79a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions crates/dash_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ impl<'interner> FunctionCompiler<'interner> {
let root_function = self.scopes[root.id].expect_function();
let locals = root_function.locals.len();

// Throughout the vm we make the assumption that the instruction pointer is always a u32
assert!(
root.buf.len() <= u32::MAX as usize,
"bytecode for root function exceeds the 4 GB limit"
);

Ok(CompileResult {
instructions: root.buf,
cp: root.cp,
Expand Down Expand Up @@ -1767,6 +1773,12 @@ impl<'interner> Visitor<Result<(), Error>> for FunctionCompiler<'interner> {
res?; // Cannot early return error in the loop as we need to pop the function state in any case
let locals = ib.scopes[id].expect_function().locals.len();

assert!(
cmp.buf.len() <= u32::MAX as usize,
"bytecode for function {} exceeds 4 GB limit",
name.map(|b| ib.interner.resolve(b.ident)).unwrap_or("<anon>")
);

let function = Function {
buffer: Buffer(Cell::new(cmp.buf.into())),
constants: cmp.cp,
Expand Down
5 changes: 4 additions & 1 deletion crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,10 @@ impl Vm {
pub(crate) fn fetchw_and_inc_ip(&mut self) -> u16 {
let frame = self.active_frame_mut();
let value: [u8; 2] = frame.function.buffer.with(|buf| {
buf[frame.ip..frame.ip + 2]
// This "no op" cast usize->u32->usize is intentional
// as it elides the end > start indexing branch
let ip = frame.ip as u32 as usize;
buf[ip..ip + 2]
.try_into()
.expect("Failed to get wide instruction")
});
Expand Down

0 comments on commit b45e79a

Please sign in to comment.