-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LUA add register manager for lua backend
- Loading branch information
Showing
6 changed files
with
177 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::collections::HashSet; | ||
|
||
pub type RegisterId = usize; | ||
|
||
const MAX_REGISTER_ID: usize = 255; | ||
|
||
pub struct RegisterManager { | ||
register_alloc_cursor: usize, | ||
used_registers: HashSet<usize>, | ||
} | ||
|
||
impl RegisterManager { | ||
#[inline] | ||
pub fn new() -> Self { | ||
Self { | ||
register_alloc_cursor: 0, | ||
used_registers: HashSet::with_capacity(MAX_REGISTER_ID), | ||
} | ||
} | ||
|
||
pub fn alloc(&mut self) -> RegisterId { | ||
// ensure has free register to allocate | ||
assert!(self.used_registers.len() <= MAX_REGISTER_ID); | ||
|
||
loop { | ||
if self.used_registers.insert(self.register_alloc_cursor) { | ||
return self.register_alloc_cursor; | ||
} | ||
|
||
self.register_alloc_cursor += 1; | ||
self.register_alloc_cursor %= MAX_REGISTER_ID; | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn free(&mut self, id: &RegisterId) { | ||
_ = self.used_registers.remove(id) | ||
} | ||
|
||
#[inline] | ||
pub fn used_count(&self) -> usize { | ||
self.used_registers.len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::parser::{BitValue, LiteralValue}; | ||
|
||
/// sBx use 17 Bits | ||
const SBX_BIT_SIZE: u32 = 17; | ||
/// Max value of sBx is 2^18 - 1 | ||
const SBX_MAX_VALUE: i32 = 2_i32.pow(SBX_BIT_SIZE + 1) - 1; | ||
/// Min value of sBx is -2^18 | ||
const SBX_MIN_VALUE: i32 = 0 - 2_i32.pow(SBX_BIT_SIZE); | ||
/// sBx Mask, lower 17 Bits is 1 | ||
const SBX_MASK: u32 = 0b0001_1111_1111_1111_1111; | ||
|
||
/// Returns true if literal can fit into sBx value | ||
pub fn try_fit_sbx(literal: &LiteralValue) -> Option<u32> { | ||
match literal { | ||
LiteralValue::Bit(BitValue::Zero) => Some(0), | ||
LiteralValue::Bit(BitValue::One) => Some(1), | ||
LiteralValue::Bool(false) => Some(0), | ||
LiteralValue::Bool(true) => Some(1), | ||
LiteralValue::Byte(v) => Some(*v as u32), | ||
LiteralValue::SInt(v) => Some((*v as i32) as u32 & SBX_MASK), | ||
LiteralValue::Int(v) => Some((*v as i32) as u32 & SBX_MASK), | ||
LiteralValue::UInt(v) => Some(*v as u32 & SBX_MASK), | ||
LiteralValue::DInt(v) => { | ||
if (SBX_MIN_VALUE..=SBX_MAX_VALUE).contains(v) { | ||
Some(*v as u32 & SBX_MASK) | ||
} else { | ||
None | ||
} | ||
} | ||
LiteralValue::UDInt(v) => { | ||
if *v <= SBX_MAX_VALUE as u32 { | ||
Some(v & SBX_MASK) | ||
} else { | ||
None | ||
} | ||
} | ||
LiteralValue::LInt(v) => { | ||
if (SBX_MIN_VALUE as i64..=SBX_MAX_VALUE as i64).contains(v) { | ||
Some(*v as u32 & SBX_MASK) | ||
} else { | ||
None | ||
} | ||
} | ||
LiteralValue::ULInt(v) => { | ||
if *v <= SBX_MAX_VALUE as u64 { | ||
Some(*v as u32 & SBX_MASK) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters