Skip to content

Commit

Permalink
[CODE] improving code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Oct 30, 2024
1 parent d642342 commit 8339c83
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/CodeGen/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub enum MachineMnemonic {

impl MachineMnemonic {
/// Returns the name of the mnemonic
pub fn name(&self) -> String {
pub fn name(&self) -> &'static str {
match self {
MachineMnemonic::Move => "move",
MachineMnemonic::Add => "add",
Expand Down Expand Up @@ -339,7 +339,7 @@ impl MachineMnemonic {
MachineMnemonic::FShr => "fshr",
MachineMnemonic::FCompare(_) => "fcompare",
MachineMnemonic::FCast(_) => "fcast",
}.to_string()
}
}
}

Expand All @@ -348,7 +348,7 @@ impl Display for MachineMnemonic {
write!(f, "{}", match self {
MachineMnemonic::Call(target) => format!("{} {}", self.name(), target),
MachineMnemonic::AdressLoad(adr) => format!("{} {}", self.name(), adr),
_ => self.name()
_ => self.name().to_string()
})
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/Jit/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use crate::Obj::Link;

use super::{JitFunction, JitLinker};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SymbolType {
Data,
Func
}

/// A jit map is a structure which is used to easily map multiple symbols into an jit function (uses the jit linker)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JitMap {
symbols: HashMap</*name*/String, Vec<u8>>,
symbol_types: HashMap</*name*/String, /*true = func; false = data*/bool>,
symbols: HashMap<String, Vec<u8>>,
symbol_types: HashMap<String, SymbolType>,
entry_symbol: String,
relocs: Vec<Link>,

Expand All @@ -33,13 +39,13 @@ impl JitMap {
/// adds a function to the map
pub fn define_func(&mut self, name: &String, data: Vec<u8>) {
self.symbols.insert(name.to_owned(), data);
self.symbol_types.insert(name.to_owned(), true);
self.symbol_types.insert(name.to_owned(), SymbolType::Func);
}

/// adds a data to the map
pub fn define_data(&mut self, name: &String, data: Vec<u8>) {
self.symbols.insert(name.to_owned(), data);
self.symbol_types.insert(name.to_owned(), false);
self.symbol_types.insert(name.to_owned(), SymbolType::Data);
}

/// adds a relocation to the map
Expand All @@ -61,9 +67,9 @@ impl JitMap {
let mut linker = JitLinker::new();

for (name, data) in &self.symbols {
let isfunc = *self.symbol_types.get(name).unwrap();
let symbol_type = *self.symbol_types.get(name).unwrap();

if isfunc {
if symbol_type == SymbolType::Func {
let entry = name == &self.entry_symbol;

linker.add_func(&name, data.to_owned(), entry);
Expand Down
24 changes: 10 additions & 14 deletions src/Target/whitelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::CodeGen::{MachineInstr, MachineMnemonic};
/// Stores allowed instructions
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WhiteList {
instrs: HashMap<String, AllowmentOption>,
instrs: HashMap<&'static str, bool>,
}

impl WhiteList {
Expand All @@ -18,35 +18,31 @@ impl WhiteList {

/// Allowes a specifc mnemonic
pub fn allow(&mut self, mnemonic: MachineMnemonic) {
if let Some(option) = self.instrs.get_mut(&mnemonic.name()) {
*option = AllowmentOption::Allowed;
if let Some(option) = self.instrs.get_mut(mnemonic.name()) {
*option = true;
} else {
self.instrs.insert(mnemonic.name(), AllowmentOption::Allowed);
self.instrs.insert(mnemonic.name(), true);
}
}

/// Forbids a specfic mnemonic
pub fn forbid(&mut self, mnemonic: MachineMnemonic) {
if let Some(option) = self.instrs.get_mut(&mnemonic.name()) {
*option = AllowmentOption::NotAllowed;
if let Some(option) = self.instrs.get_mut(mnemonic.name()) {
*option = false;
} else {
self.instrs.insert(mnemonic.name(), AllowmentOption::NotAllowed);
self.instrs.insert(mnemonic.name(), false);
}
}

/// Checks if the mnemonic is allowed
pub fn is_allowed(&self, mnemonic: MachineMnemonic) -> AllowmentOption {
if let Some(option) = self.instrs.get(&mnemonic.name()) {
*option
} else {
AllowmentOption::Unknown
}
pub fn is_allowed(&self, mnemonic: MachineMnemonic) -> bool {
*self.instrs.get(mnemonic.name()).unwrap_or(&false)
}

/// 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 {
if !self.is_allowed(instr.mnemonic.clone()) {
Err(WhiteListError::NotAllowed(instr.mnemonic.clone()))?
}
}
Expand Down

0 comments on commit 8339c83

Please sign in to comment.