Make the main frame "extendable" allowing us to not have locals until needed
This commit is contained in:
24
src/frame.rs
24
src/frame.rs
@@ -6,6 +6,7 @@ pub struct Frame {
|
|||||||
pub base: usize,
|
pub base: usize,
|
||||||
pub stack: Stack,
|
pub stack: Stack,
|
||||||
pub locals: Vec<i32>,
|
pub locals: Vec<i32>,
|
||||||
|
pub can_extend_locals: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Frame {
|
impl Frame {
|
||||||
@@ -17,6 +18,19 @@ impl Frame {
|
|||||||
base,
|
base,
|
||||||
stack: Stack::new(),
|
stack: Stack::new(),
|
||||||
locals: vec![0; num_locals],
|
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,17 +42,25 @@ impl Frame {
|
|||||||
self.locals.is_empty()
|
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() {
|
if offset >= self.locals.len() {
|
||||||
|
if self.can_extend_locals {
|
||||||
|
self.locals.resize(offset + 1, 0);
|
||||||
|
} else {
|
||||||
return Err("Local variable out of range");
|
return Err("Local variable out of range");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Ok(self.locals[offset])
|
Ok(self.locals[offset])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set(&mut self, offset: usize, val: i32) -> Result<()> {
|
pub fn set(&mut self, offset: usize, val: i32) -> Result<()> {
|
||||||
if offset >= self.locals.len() {
|
if offset >= self.locals.len() {
|
||||||
|
if self.can_extend_locals {
|
||||||
|
self.locals.resize(offset + 1, 0);
|
||||||
|
} else {
|
||||||
return Err("Local variable out of range");
|
return Err("Local variable out of range");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.locals[offset] = val;
|
self.locals[offset] = val;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ use heap::Heap;
|
|||||||
use netstack::NetStack;
|
use netstack::NetStack;
|
||||||
|
|
||||||
pub const MAGIC_HEADER: u32 = 0x1DEA_DFAD;
|
pub const MAGIC_HEADER: u32 = 0x1DEA_DFAD;
|
||||||
const ANTI_BS_SIZE: usize = 0xFF;
|
|
||||||
|
|
||||||
pub struct Machine {
|
pub struct Machine {
|
||||||
pub wide: bool,
|
pub wide: bool,
|
||||||
@@ -41,7 +40,7 @@ impl Machine {
|
|||||||
halted: false,
|
halted: false,
|
||||||
pool,
|
pool,
|
||||||
block,
|
block,
|
||||||
frame: vec![Frame::new(ANTI_BS_SIZE, 0)],
|
frame: vec![Frame::new_extendable(0)],
|
||||||
stream_in: Box::new(::std::io::stdin()),
|
stream_in: Box::new(::std::io::stdin()),
|
||||||
stream_out: Arc::new(Mutex::new(::std::io::stdout())),
|
stream_out: Arc::new(Mutex::new(::std::io::stdout())),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user