stack: Helper functions and unified return types

This commit is contained in:
losfair
2019-02-13 20:03:54 +08:00
parent 4ebb22f8bc
commit a69c5b4a14

View File

@ -1,3 +1,4 @@
use crate::codegen::CodegenError;
use dynasmrt::DynamicLabel; use dynasmrt::DynamicLabel;
use wasmparser::Type as WpType; use wasmparser::Type as WpType;
@ -51,6 +52,26 @@ pub enum ValueLocation {
Stack, Stack,
} }
impl ValueLocation {
pub fn is_register(&self) -> bool {
if let ValueLocation::Register(_) = *self {
true
} else {
false
}
}
pub fn get_register(&self) -> Result<u8, CodegenError> {
if let ValueLocation::Register(id) = *self {
Ok(id)
} else {
Err(CodegenError {
message: "not a register location"
})
}
}
}
impl ValueStack { impl ValueStack {
pub fn new(num_regs: u8) -> ValueStack { pub fn new(num_regs: u8) -> ValueStack {
ValueStack { ValueStack {
@ -85,22 +106,34 @@ impl ValueStack {
loc loc
} }
pub fn pop(&mut self) -> Option<ValueInfo> { pub fn pop(&mut self) -> Result<ValueInfo, CodegenError> {
self.values.pop() match self.values.pop() {
} Some(x) => Ok(x),
None => Err(CodegenError {
pub fn pop2(&mut self) -> Option<(ValueInfo, ValueInfo)> { message: "no value on top of stack",
if self.values.len() < 2 { }),
None
} else {
let v2 = self.values.pop().unwrap();
let v1 = self.values.pop().unwrap();
Some((v1, v2))
} }
} }
pub fn peek(&self) -> Option<ValueInfo> { pub fn pop2(&mut self) -> Result<(ValueInfo, ValueInfo), CodegenError> {
self.values.last().cloned() if self.values.len() < 2 {
Err(CodegenError {
message: "less than 2 values on top of stack",
})
} else {
let v2 = self.values.pop().unwrap();
let v1 = self.values.pop().unwrap();
Ok((v1, v2))
}
}
pub fn peek(&self) -> Result<ValueInfo, CodegenError> {
match self.values.last().cloned() {
Some(x) => Ok(x),
None => Err(CodegenError {
message: "no value on top of stack",
}),
}
} }
pub fn reset_depth(&mut self, target_depth: usize) { pub fn reset_depth(&mut self, target_depth: usize) {