Strongly type scratch registers and fixed an unwinding issue.

This commit is contained in:
losfair
2019-03-05 00:23:49 +08:00
parent 2962fe25b6
commit fa61b66516
2 changed files with 20 additions and 9 deletions

View File

@ -56,10 +56,19 @@ pub struct ValueInfo {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ValueLocation {
Register(u8),
Register(ScratchRegister),
Stack,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ScratchRegister(u8);
impl ScratchRegister {
pub fn raw_id(&self) -> u8 {
self.0
}
}
impl ValueLocation {
pub fn is_register(&self) -> bool {
if let ValueLocation::Register(_) = *self {
@ -69,7 +78,7 @@ impl ValueLocation {
}
}
pub fn get_register(&self) -> Result<u8, CodegenError> {
pub fn get_register(&self) -> Result<ScratchRegister, CodegenError> {
if let ValueLocation::Register(id) = *self {
Ok(id)
} else {
@ -90,11 +99,11 @@ impl ValueStack {
fn next_location(&self, loc: &ValueLocation) -> ValueLocation {
match *loc {
ValueLocation::Register(x) => {
ValueLocation::Register(ScratchRegister(x)) => {
if x >= self.num_regs - 1 {
ValueLocation::Stack
} else {
ValueLocation::Register(x + 1)
ValueLocation::Register(ScratchRegister(x + 1))
}
}
ValueLocation::Stack => ValueLocation::Stack,
@ -106,7 +115,7 @@ impl ValueStack {
.values
.last()
.map(|x| self.next_location(&x.location))
.unwrap_or(ValueLocation::Register(0));
.unwrap_or(ValueLocation::Register(ScratchRegister(0)));
self.values.push(ValueInfo {
ty: ty,
location: loc,