Skip to content

Commit

Permalink
[TYPE] using more safe if let Some(...) instead of .unwrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Aug 23, 2024
1 parent 4e5f823 commit 5c841c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 80 deletions.
57 changes: 7 additions & 50 deletions src/IR/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ IrTypeWith3!(And, T, U, Z);
use crate::Support::{ColorClass, ColorProfile};

macro_rules! MathIrNode {
($name:ident, $compileFuncVarVar:ident, $compileFuncVarTy:ident, $compileFuncTyTy:ident, $buildTraitName:ident, $buildFuncName:ident, $dump:expr, $variantVarVar:expr, $variantVarType:expr, $variantTypeType:expr) => {
($name:ident, $compileFuncVarVar:ident, $compileFuncVarTy:ident, $compileFuncTyTy:ident, $buildTraitName:ident, $buildFuncName:ident, $dump:expr) => {
/// Used for overloading the build function
pub trait $buildTraitName<T, U> {
/// Xors values
Expand Down Expand Up @@ -151,10 +151,6 @@ macro_rules! MathIrNode {
Box::new(self.clone())
}

fn name(&self) -> String {
$variantTypeType.into()
}

fn dump(&self) -> String {
format!("{} = {} {} {}, {}", $dump, self.inner3.name, self.inner3.ty, self.inner1.val(), self.inner2.val())
}
Expand Down Expand Up @@ -205,11 +201,7 @@ macro_rules! MathIrNode {
fn clone_box(&self) -> Box<dyn Ir> {
Box::new(self.clone())
}

fn name(&self) -> String {
$variantVarVar.into()
}


fn dump(&self) -> String {
format!("{} = {} {} {}, {}", $dump, self.inner3.name, self.inner3.ty, self.inner1.name, self.inner2.name)
}
Expand Down Expand Up @@ -261,10 +253,6 @@ macro_rules! MathIrNode {
Box::new(self.clone())
}

fn name(&self) -> String {
$variantVarType.into()
}

fn dump(&self) -> String {
format!("{} = {} {} {}, {}", $dump, self.inner3.name, self.inner1.ty, self.inner1.name, self.inner2.val())
}
Expand Down Expand Up @@ -314,22 +302,18 @@ macro_rules! MathIrNode {
};
}

MathIrNode!(Add, getCompileFuncForAddVarVar, getCompileFuncForAddVarType, getCompileFuncForAddTypeType, BuildAdd, BuildAdd, "add", "AddVarVar", "AddVarType", "AddTypeType");
MathIrNode!(Sub, getCompileFuncForSubVarVar, getCompileFuncForSubVarType, getCompileFuncForSubTypeType, BuildSub, BuildSub, "sub", "SubVarVar", "SubVarType", "SubTypeType");
MathIrNode!(Xor, getCompileFuncForXorVarVar, getCompileFuncForXorVarType, getCompileFuncForXorTypeType, BuildXor, BuildXor, "xor", "XorVarVar", "XorVarType", "XorTypeType");
MathIrNode!(Or, getCompileFuncForOrVarVar, getCompileFuncForOrVarType, getCompileFuncForOrTypeType, BuildOr, BuildOr, "or", "OrVarVar", "OrVarType", "OrTypeType");
MathIrNode!(And, getCompileFuncForAndVarVar, getCompileFuncForAndVarType, getCompileFuncForAndTypeType, BuildAnd, BuildAnd, "and", "AndVarVar", "AndVarType", "AndTypeType");
MathIrNode!(Add, getCompileFuncForAddVarVar, getCompileFuncForAddVarType, getCompileFuncForAddTypeType, BuildAdd, BuildAdd, "add");
MathIrNode!(Sub, getCompileFuncForSubVarVar, getCompileFuncForSubVarType, getCompileFuncForSubTypeType, BuildSub, BuildSub, "sub");
MathIrNode!(Xor, getCompileFuncForXorVarVar, getCompileFuncForXorVarType, getCompileFuncForXorTypeType, BuildXor, BuildXor, "xor");
MathIrNode!(Or, getCompileFuncForOrVarVar, getCompileFuncForOrVarType, getCompileFuncForOrTypeType, BuildOr, BuildOr, "or");
MathIrNode!(And, getCompileFuncForAndVarVar, getCompileFuncForAndVarType, getCompileFuncForAndTypeType, BuildAnd, BuildAnd, "and");


impl Ir for Return<Type> {
fn clone_box(&self) -> Box<dyn Ir> {
Box::new(self.clone())
}

fn name(&self) -> String {
"RetType".into()
}

fn dump(&self) -> String {
let metadata: TypeMetadata = self.inner1.into();
format!("ret {} {}", metadata, self.inner1.val())
Expand Down Expand Up @@ -368,10 +352,6 @@ impl Ir for Return<Var> {
Box::new(self.clone())
}

fn name(&self) -> String {
"RetVar".into()
}

fn dump(&self) -> String {
format!("ret {} {}", self.inner1.ty, self.inner1.name)
}
Expand Down Expand Up @@ -423,10 +403,6 @@ impl Ir for ConstAssign<Var, Type> {
)
}

fn name(&self) -> String {
"AssignVarType".into()
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down Expand Up @@ -470,10 +446,6 @@ impl Ir for ConstAssign<Var, Var> {
)
}

fn name(&self) -> String {
"AssignVarVar".into()
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down Expand Up @@ -516,10 +488,6 @@ impl Ir for ConstAssign<Var, Const> {
)
}

fn name(&self) -> String {
"AssignVarConst".into()
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down Expand Up @@ -557,10 +525,6 @@ impl Ir for Cast<Var, TypeMetadata, Var> {
)
}

fn name(&self) -> String {
"CastTyVar".into()
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down Expand Up @@ -617,10 +581,6 @@ impl Ir for Call<Function, Vec<Var>, Var> {
)
}

fn name(&self) -> String {
"Call".to_string()
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down Expand Up @@ -781,9 +741,6 @@ pub(crate) trait Ir: Debug + Any {
/// Returns the ir node as his textual representation with colors
fn dumpColored(&self, profile: ColorProfile) -> String;

/// Returns the name of the ir expr
fn name(&self) -> String;

/// Turns the ir node to an any
fn as_any(&self) -> &dyn Any;

Expand Down
54 changes: 24 additions & 30 deletions src/Optimizations/Passes/MathEval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ pub fn PreComputeValue() -> Box<PreComputeValue> {
impl Pass for PreComputeValue {
fn run(&self, block: &mut crate::prelude::Block) {
for node in block.nodes.iter_mut() {
if &node.name() == "AddTypeType" {
let any = node.as_any();
let add_node = any.downcast_ref::<Add<Type, Type, Var>>().unwrap().clone();
let res = add_node.inner1.val() + add_node.inner2.val();
node.replace(ConstAssign::new(add_node.inner3.clone(), {
match add_node.inner3.ty {
if let Some(or) = node.as_any().downcast_ref::<Add<Type, Type, Var>>() {
let res = or.inner1.val() + or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
TypeMetadata::u64 => Type::u64(res as u64),
Expand All @@ -28,12 +26,11 @@ impl Pass for PreComputeValue {
TypeMetadata::Void => Type::Void,
}
}))
} else if &node.name() == "SubTypeType" {
let any = node.as_any();
let sub_node = any.downcast_ref::<Sub<Type, Type, Var>>().unwrap().clone();
let res = sub_node.inner1.val() - sub_node.inner2.val();
node.replace(ConstAssign::new(sub_node.inner3.clone(), {
match sub_node.inner3.ty {
}
if let Some(or) = node.as_any().downcast_ref::<Sub<Type, Type, Var>>() {
let res = or.inner1.val() - or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
TypeMetadata::u64 => Type::u64(res as u64),
Expand All @@ -44,12 +41,11 @@ impl Pass for PreComputeValue {
TypeMetadata::Void => Type::Void,
}
}))
} else if &node.name() == "XorTypeType" {
let any = node.as_any();
let xor_node = any.downcast_ref::<Xor<Type, Type, Var>>().unwrap().clone();
let res = xor_node.inner1.val() ^ xor_node.inner2.val();
node.replace(ConstAssign::new(xor_node.inner3.clone(), {
match xor_node.inner3.ty {
}
if let Some(or) = node.as_any().downcast_ref::<And<Type, Type, Var>>() {
let res = or.inner1.val() & or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
TypeMetadata::u64 => Type::u64(res as u64),
Expand All @@ -60,12 +56,11 @@ impl Pass for PreComputeValue {
TypeMetadata::Void => Type::Void,
}
}))
} else if &node.name() == "AndTypeType" {
let any = node.as_any();
let and_node = any.downcast_ref::<And<Type, Type, Var>>().unwrap().clone();
let res = and_node.inner1.val() & and_node.inner2.val();
node.replace(ConstAssign::new(and_node.inner3.clone(), {
match and_node.inner3.ty {
}
if let Some(or) = node.as_any().downcast_ref::<Xor<Type, Type, Var>>() {
let res = or.inner1.val() ^ or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
TypeMetadata::u64 => Type::u64(res as u64),
Expand All @@ -76,12 +71,11 @@ impl Pass for PreComputeValue {
TypeMetadata::Void => Type::Void,
}
}))
} else if &node.name() == "OrTypeType" {
let any = node.as_any();
let or_node = any.downcast_ref::<Or<Type, Type, Var>>().unwrap().clone();
let res = or_node.inner1.val() | or_node.inner2.val();
node.replace(ConstAssign::new(or_node.inner3.clone(), {
match or_node.inner3.ty {
}
if let Some(or) = node.as_any().downcast_ref::<Or<Type, Type, Var>>() {
let res = or.inner1.val() | or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
TypeMetadata::u64 => Type::u64(res as u64),
Expand Down

0 comments on commit 5c841c3

Please sign in to comment.