Croissant Runtime
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
crsn/crsn_stacks/src/exec.rs

73 lines
2.2 KiB

use std::collections::{HashMap, VecDeque};
use crsn::asm::data::literal::Value;
use crsn::asm::instr::Cond;
use crsn::module::{CrsnExtension, EvalRes, OpTrait};
use crsn::runtime::fault::Fault;
use crsn::runtime::run_thread::{state::RunState, ThreadInfo};
use crate::defs::StackOp;
#[derive(Debug, Default)]
struct Stacks {
store: HashMap<Value, VecDeque<Value>>,
}
impl OpTrait for StackOp {
fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> {
let eres = EvalRes::default();
match self {
StackOp::NewStack { dst } => {
let id = info.uniq();
let stacks: &mut Stacks = state.ext_mut();
stacks.store.insert(id, VecDeque::new());
state.write(*dst, id)?;
}
StackOp::Push { obj, src } => {
state.clear_status();
let handle = state.read_obj(*obj)?;
let val = state.read(*src)?;
let stacks: &mut Stacks = state.ext_mut();
stacks.store.get_mut(&handle)
.ok_or_else(|| Fault::ObjectNotExist(handle))?
.push_back(val);
}
StackOp::Pop { dst, obj } => {
state.clear_status();
let handle = state.read_obj(*obj)?;
let stacks: &mut Stacks = state.ext_mut();
let stack = stacks.store.get_mut(&handle)
.ok_or_else(|| Fault::ObjectNotExist(handle))?;
let val = stack.pop_back();
if stack.is_empty() {
state.set_flag(Cond::Zero, true);
}
let val = match val {
None => {
state.set_flag(Cond::Overflow, true);
0
}
Some(val) => {
val
}
};
state.write(*dst, val)?;
}
}
Ok(eres)
}
//
}
pub(crate) fn drop_stack(state: &mut RunState, handle: Value) -> Result<Option<()>, Fault> {
let stacks: &mut Stacks = state.ext_mut();
Ok(stacks.store.remove(&handle).map(|_| ()))
}