-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
133 additions
and
25 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
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,81 @@ | ||
use std::{collections::HashMap, error::Error, fmt::Display}; | ||
|
||
use crate::CodeGen::{MachineInstr, MachineMnemonic}; | ||
|
||
/// Stores allowed instructions | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct WhiteList { | ||
instrs: HashMap<MachineMnemonic, AllowmentOption>, | ||
} | ||
|
||
impl WhiteList { | ||
/// Creates a new instruction whitelist | ||
pub fn new() -> Self { | ||
Self { | ||
instrs: HashMap::new() | ||
} | ||
} | ||
|
||
/// Allowes a specifc mnemonic | ||
pub fn allow(&mut self, mnemonic: MachineMnemonic) { | ||
if let Some(option) = self.instrs.get_mut(&mnemonic) { | ||
*option = AllowmentOption::Allowed; | ||
} else { | ||
self.instrs.insert(mnemonic, AllowmentOption::Allowed); | ||
} | ||
} | ||
|
||
/// Forbids a specfic mnemonic | ||
pub fn forbid(&mut self, mnemonic: MachineMnemonic) { | ||
if let Some(option) = self.instrs.get_mut(&mnemonic) { | ||
*option = AllowmentOption::NotAllowed; | ||
} else { | ||
self.instrs.insert(mnemonic, AllowmentOption::NotAllowed); | ||
} | ||
} | ||
|
||
/// Checks if the mnemonic is allowed | ||
pub fn is_allowed(&self, mnemonic: MachineMnemonic) -> AllowmentOption { | ||
if let Some(option) = self.instrs.get(&mnemonic) { | ||
*option | ||
} else { | ||
AllowmentOption::Unknown | ||
} | ||
} | ||
|
||
/// Checks for forbidden mnemonics | ||
pub fn check_for_forbidden_mnemonics(&self, vec: &Vec<MachineInstr>) -> Result<(), WhiteListError> { | ||
for instr in vec { | ||
if self.is_allowed(instr.mnemonic.clone()) == AllowmentOption::NotAllowed { | ||
Err(WhiteListError::NotAllowed(instr.mnemonic.clone()))? | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// how strong allowed the object is | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
#[allow(missing_docs)] | ||
pub enum AllowmentOption { | ||
Allowed, | ||
NotAllowed, | ||
Unknown, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[allow(missing_docs)] | ||
pub enum WhiteListError { | ||
NotAllowed(MachineMnemonic) | ||
} | ||
|
||
impl Display for WhiteListError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", match self { | ||
WhiteListError::NotAllowed(mne) => format!("the instruction {} is not allowed but was suppyled", mne), | ||
}) | ||
} | ||
} | ||
|
||
impl Error for WhiteListError {} |
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 |
---|---|---|
|
@@ -8,4 +8,4 @@ mod optimizer; | |
|
||
pub use lexer::*; | ||
pub use parser::*; | ||
pub use optimizer::*; | ||
//pub(crate) use optimizer::*; |
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