cranelift: Prevent overflow errors in interpreter for add,sub,mul

This commit is contained in:
Afonso Bordado
2021-06-29 10:12:28 +01:00
committed by Andrew Brown
parent c5609bc364
commit a4770a7e28
2 changed files with 315 additions and 4 deletions

View File

@@ -119,6 +119,12 @@ macro_rules! unary_match {
};
}
macro_rules! binary_match {
( $op:ident($arg1:expr, $arg2:expr); [ $( $data_value_ty:ident ),* ] ) => {
match ($arg1, $arg2) {
$( (DataValue::$data_value_ty(a), DataValue::$data_value_ty(b)) => { Ok(DataValue::$data_value_ty(a.$op(*b))) } )*
_ => unimplemented!()
}
};
( $op:tt($arg1:expr, $arg2:expr); [ $( $data_value_ty:ident ),* ] ) => {
match ($arg1, $arg2) {
$( (DataValue::$data_value_ty(a), DataValue::$data_value_ty(b)) => { Ok(DataValue::$data_value_ty(a $op b)) } )*
@@ -272,19 +278,19 @@ impl Value for DataValue {
}
fn add(self, other: Self) -> ValueResult<Self> {
binary_match!(+(&self, &other); [I8, I16, I32, I64]) // TODO: floats must handle NaNs, +/-0
binary_match!(wrapping_add(&self, &other); [I8, I16, I32, I64]) // TODO: floats must handle NaNs, +/-0
}
fn sub(self, other: Self) -> ValueResult<Self> {
binary_match!(-(&self, &other); [I8, I16, I32, I64]) // TODO: floats must handle NaNs, +/-0
binary_match!(wrapping_sub(&self, &other); [I8, I16, I32, I64]) // TODO: floats must handle NaNs, +/-0
}
fn mul(self, other: Self) -> ValueResult<Self> {
binary_match!(*(&self, &other); [I8, I16, I32, I64])
binary_match!(wrapping_mul(&self, &other); [I8, I16, I32, I64])
}
fn div(self, other: Self) -> ValueResult<Self> {
binary_match!(/(&self, &other); [I8, I16, I32, I64])
binary_match!(/(&self, &other); [I8, I16, I32, I64, U8, U16, U32, U64])
}
fn rem(self, other: Self) -> ValueResult<Self> {