Allocates a new temporary register.
(&mut self)
| 522 | |
| 523 | /// Allocates a new temporary register. |
| 524 | pub(crate) fn alloc(&mut self) -> Result<Register> { |
| 525 | let temp; |
| 526 | let new_next_temp; |
| 527 | { |
| 528 | let mut next_temp = self.next_temp.borrow_mut(); |
| 529 | temp = *next_temp; |
| 530 | self.ntemps += 1; |
| 531 | new_next_temp = match next_temp.checked_add(1) { |
| 532 | Some(reg) => reg, |
| 533 | None => return Err(Error::OutOfRegisters(RegisterScope::Temp)), |
| 534 | }; |
| 535 | *next_temp = new_next_temp; |
| 536 | } |
| 537 | |
| 538 | { |
| 539 | let mut count_temps = self.count_temps.borrow_mut(); |
| 540 | *count_temps = max(*count_temps, new_next_temp); |
| 541 | } |
| 542 | |
| 543 | match u8::try_from(usize::from(self.nlocals) + usize::from(temp)) { |
| 544 | Ok(reg) => { |
| 545 | Ok(Register::local(reg).map_err(|_| Error::OutOfRegisters(RegisterScope::Temp))?) |
| 546 | } |
| 547 | Err(_) => Err(Error::OutOfRegisters(RegisterScope::Temp)), |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | #[cfg(test)] |
no outgoing calls
no test coverage detected