Add more trap codes.

These are codes that come up naturally when translating WebAssembly and
legalizing the Cretonne instruction set.
This commit is contained in:
Jakob Stoklund Olesen
2017-09-22 08:51:55 -07:00
parent 7fbf1cb810
commit 2946cc54d3

View File

@@ -20,12 +20,24 @@ pub enum TrapCode {
/// pages. /// pages.
HeapOutOfBounds, HeapOutOfBounds,
/// Other bounds checking error.
OutOfBounds,
/// Indirect call to a null table entry.
IndirectCallToNull,
/// Signature mismatch on indirect call.
BadSignature,
/// An integer arithmetic operation caused an overflow. /// An integer arithmetic operation caused an overflow.
IntegerOverflow, IntegerOverflow,
/// An integer division by zero. /// An integer division by zero.
IntegerDivisionByZero, IntegerDivisionByZero,
/// Failed float-to-int conversion.
BadConversionToInteger,
/// A user-defined trap code. /// A user-defined trap code.
User(u16), User(u16),
} }
@@ -36,8 +48,12 @@ impl Display for TrapCode {
let identifier = match *self { let identifier = match *self {
StackOverflow => "stk_ovf", StackOverflow => "stk_ovf",
HeapOutOfBounds => "heap_oob", HeapOutOfBounds => "heap_oob",
OutOfBounds => "oob",
IndirectCallToNull => "icall_null",
BadSignature => "bad_sig",
IntegerOverflow => "int_ovf", IntegerOverflow => "int_ovf",
IntegerDivisionByZero => "int_divz", IntegerDivisionByZero => "int_divz",
BadConversionToInteger => "bad_toint",
User(x) => return write!(f, "user{}", x), User(x) => return write!(f, "user{}", x),
}; };
f.write_str(identifier) f.write_str(identifier)
@@ -52,8 +68,12 @@ impl FromStr for TrapCode {
match s { match s {
"stk_ovf" => Ok(StackOverflow), "stk_ovf" => Ok(StackOverflow),
"heap_oob" => Ok(HeapOutOfBounds), "heap_oob" => Ok(HeapOutOfBounds),
"oob" => Ok(OutOfBounds),
"icall_null" => Ok(IndirectCallToNull),
"bad_sig" => Ok(BadSignature),
"int_ovf" => Ok(IntegerOverflow), "int_ovf" => Ok(IntegerOverflow),
"int_divz" => Ok(IntegerDivisionByZero), "int_divz" => Ok(IntegerDivisionByZero),
"bad_toint" => Ok(BadConversionToInteger),
_ if s.starts_with("user") => s[4..].parse().map(User).map_err(|_| ()), _ if s.starts_with("user") => s[4..].parse().map(User).map_err(|_| ()),
_ => Err(()), _ => Err(()),
} }
@@ -65,11 +85,15 @@ mod tests {
use super::*; use super::*;
// Everything but user-defined codes. // Everything but user-defined codes.
const CODES: [TrapCode; 4] = [ const CODES: [TrapCode; 8] = [
TrapCode::StackOverflow, TrapCode::StackOverflow,
TrapCode::HeapOutOfBounds, TrapCode::HeapOutOfBounds,
TrapCode::OutOfBounds,
TrapCode::IndirectCallToNull,
TrapCode::BadSignature,
TrapCode::IntegerOverflow, TrapCode::IntegerOverflow,
TrapCode::IntegerDivisionByZero, TrapCode::IntegerDivisionByZero,
TrapCode::BadConversionToInteger,
]; ];
#[test] #[test]