Make the main frame "extendable" allowing us to not have locals until needed

This commit is contained in:
2019-05-15 14:59:19 +02:00
parent 1381ce7b48
commit 9740c0fc57
2 changed files with 26 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ pub struct Frame {
pub base: usize,
pub stack: Stack,
pub locals: Vec<i32>,
pub can_extend_locals: bool,
}
impl Frame {
@@ -17,6 +18,19 @@ impl Frame {
base,
stack: Stack::new(),
locals: vec![0; num_locals],
can_extend_locals: false,
}
}
pub fn new_extendable(base: usize) -> Frame {
if cfg!(feature = "debug:frame") {
println!("Initializing extendable frame");
}
Frame {
base,
stack: Stack::new(),
locals: Vec::new(),
can_extend_locals: true,
}
}
@@ -28,16 +42,24 @@ impl Frame {
self.locals.is_empty()
}
pub fn get(&self, offset: usize) -> Result<i32> {
pub fn get(&mut self, offset: usize) -> Result<i32> {
if offset >= self.locals.len() {
return Err("Local variable out of range");
if self.can_extend_locals {
self.locals.resize(offset + 1, 0);
} else {
return Err("Local variable out of range");
}
}
Ok(self.locals[offset])
}
pub fn set(&mut self, offset: usize, val: i32) -> Result<()> {
if offset >= self.locals.len() {
return Err("Local variable out of range");
if self.can_extend_locals {
self.locals.resize(offset + 1, 0);
} else {
return Err("Local variable out of range");
}
}
self.locals[offset] = val;
Ok(())

View File

@@ -16,7 +16,6 @@ use heap::Heap;
use netstack::NetStack;
pub const MAGIC_HEADER: u32 = 0x1DEA_DFAD;
const ANTI_BS_SIZE: usize = 0xFF;
pub struct Machine {
pub wide: bool,
@@ -41,7 +40,7 @@ impl Machine {
halted: false,
pool,
block,
frame: vec![Frame::new(ANTI_BS_SIZE, 0)],
frame: vec![Frame::new_extendable(0)],
stream_in: Box::new(::std::io::stdin()),
stream_out: Arc::new(Mutex::new(::std::io::stdout())),