Skip to content

Commit

Permalink
Fix missing set_action in some instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Oct 4, 2024
1 parent 099e74e commit bc1bf5c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/codegen/src/optim/sccp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl<'i> State for CellState<'i> {
}
}

fn eval_func(
fn call_func(
&mut self,
_func: sonatina_ir::module::FuncRef,
_args: Vec<EvalValue>,
Expand Down
10 changes: 9 additions & 1 deletion crates/ir/src/interpret/arith.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::{EvalValue, Interpret, State};
use super::{Action, EvalValue, Interpret, State};
use crate::inst::arith::*;

impl Interpret for Neg {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let val = state.lookup_val(*self.arg());
state.set_action(Action::Continue);
val.with_imm(|value| -value)
}
}
Expand All @@ -12,6 +13,7 @@ impl Interpret for Add {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs + rhs)
}
Expand All @@ -21,6 +23,7 @@ impl Interpret for Sub {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| EvalValue::Imm(lhs - rhs))
}
Expand All @@ -30,6 +33,7 @@ impl Interpret for Mul {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs - rhs)
}
Expand All @@ -39,6 +43,7 @@ impl Interpret for Sdiv {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.sdiv(rhs))
}
Expand All @@ -48,6 +53,7 @@ impl Interpret for Udiv {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.udiv(rhs))
}
Expand All @@ -57,6 +63,7 @@ impl Interpret for Shl {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let bits = state.lookup_val(*self.bits());
let value = state.lookup_val(*self.value());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(bits, value, |bits, value| value << bits)
}
Expand All @@ -66,6 +73,7 @@ impl Interpret for Shr {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let bits = state.lookup_val(*self.bits());
let value = state.lookup_val(*self.value());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(bits, value, |bits, value| value >> bits)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/ir/src/interpret/cast.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::{EvalValue, Interpret, State};
use super::{Action, EvalValue, Interpret, State};
use crate::inst::cast::*;

impl Interpret for Sext {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let value = state.lookup_val(*self.from());
let ty = self.ty();
state.set_action(Action::Continue);

value.with_imm(|value| value.sext(*ty))
}
Expand All @@ -14,6 +15,7 @@ impl Interpret for Zext {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let value = state.lookup_val(*self.from());
let ty = self.ty();
state.set_action(Action::Continue);

value.with_imm(|value| value.zext(*ty))
}
Expand All @@ -23,6 +25,7 @@ impl Interpret for Trunc {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let value = state.lookup_val(*self.from());
let ty = self.ty();
state.set_action(Action::Continue);

value.with_imm(|value| value.trunc(*ty))
}
Expand Down
14 changes: 13 additions & 1 deletion crates/ir/src/interpret/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::{EvalValue, Interpret, State};
use super::{Action, EvalValue, Interpret, State};
use crate::{inst::cmp::*, Immediate};

impl Interpret for Lt {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.lt(rhs))
}
Expand All @@ -14,6 +15,7 @@ impl Interpret for Gt {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.gt(rhs))
}
Expand All @@ -23,6 +25,7 @@ impl Interpret for Slt {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.slt(rhs))
}
Expand All @@ -32,6 +35,7 @@ impl Interpret for Sgt {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.sgt(rhs))
}
Expand All @@ -41,6 +45,7 @@ impl Interpret for Le {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.le(rhs))
}
Expand All @@ -50,6 +55,7 @@ impl Interpret for Ge {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.ge(rhs))
}
Expand All @@ -59,6 +65,7 @@ impl Interpret for Sle {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.sle(rhs))
}
Expand All @@ -68,6 +75,7 @@ impl Interpret for Sge {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.sge(rhs))
}
Expand All @@ -77,6 +85,7 @@ impl Interpret for Eq {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.imm_eq(rhs))
}
Expand All @@ -86,6 +95,7 @@ impl Interpret for Ne {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs.imm_ne(rhs))
}
Expand All @@ -94,6 +104,8 @@ impl Interpret for Ne {
impl Interpret for IsZero {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let val = state.lookup_val(*self.lhs());
state.set_action(Action::Continue);

val.with_imm(|value| Immediate::from(value.is_zero()))
}
}
4 changes: 3 additions & 1 deletion crates/ir/src/interpret/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ impl Interpret for Call {
.map(|arg| state.lookup_val(*arg))
.collect();

state.eval_func(func, args)
let val = state.call_func(func, args);
state.set_action(Action::Continue);
val
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/ir/src/interpret/logic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::{EvalValue, Interpret, State};
use super::{Action, EvalValue, Interpret, State};
use crate::inst::logic::*;

impl Interpret for Not {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let arg = state.lookup_val(*self.arg());
state.set_action(Action::Continue);

arg.with_imm(|arg| !arg)
}
}
Expand All @@ -12,6 +14,7 @@ impl Interpret for And {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs & rhs)
}
Expand All @@ -21,6 +24,7 @@ impl Interpret for Or {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs | rhs)
}
Expand All @@ -30,6 +34,7 @@ impl Interpret for Xor {
fn interpret(&self, state: &mut dyn State) -> EvalValue {
let lhs = state.lookup_val(*self.lhs());
let rhs = state.lookup_val(*self.rhs());
state.set_action(Action::Continue);

EvalValue::zip_with_imm(lhs, rhs, |lhs, rhs| lhs ^ rhs)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub trait State {
/// error, or cause a panic).
fn lookup_val(&mut self, value: ValueId) -> EvalValue;

fn eval_func(&mut self, func: FuncRef, args: Vec<EvalValue>) -> EvalValue;
fn call_func(&mut self, func: FuncRef, args: Vec<EvalValue>) -> EvalValue;

fn set_action(&mut self, action: Action);

Expand Down

0 comments on commit bc1bf5c

Please sign in to comment.