diff --git a/src/IR/nodes/alloca.rs b/src/IR/nodes/alloca.rs index 5769f5b5..74d4e886 100644 --- a/src/IR/nodes/alloca.rs +++ b/src/IR/nodes/alloca.rs @@ -1,5 +1,5 @@ use crate::IR::{Function, TypeMetadata, Var}; -use crate::Support::ColorClass; +use crate::Support::{AsAny, ColorClass}; use super::{Alloca, EvalOptVisitor, Ir}; @@ -59,6 +59,21 @@ impl EvalOptVisitor for Alloca { } } +impl Alloca where + T: Clone + AsAny + 'static, + U: Clone + AsAny + 'static +{ + /// Returns the output variable + pub fn getOut(&self) -> Var { + self.inner1.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the type which is allocated + pub fn getTypeToAlloc(&self) -> TypeMetadata { + self.inner2.as_any().downcast_ref::().unwrap().to_owned() + } +} + impl Function { /// Builds an stack allocation (the out var is the pointer to the allocated stack region) pub fn BuildAlloca(&mut self, ty: TypeMetadata) -> Var { diff --git a/src/IR/nodes/assign.rs b/src/IR/nodes/assign.rs index 8ec5cf91..9e67bd64 100644 --- a/src/IR/nodes/assign.rs +++ b/src/IR/nodes/assign.rs @@ -1,3 +1,5 @@ +use std::any::TypeId; + use super::*; impl Ir for Assign { @@ -204,6 +206,40 @@ impl EvalOptVisitor for Assign { } } +impl Assign where + U: AsAny + 'static +{ + /// Returns the type of the assignment + pub fn getType(&self) -> TypeMetadata { + self.inner1.ty + } + + /// Returns the output variable + pub fn getOut(&self) -> Var { + self.inner1.to_owned() + } + + /// Returns if the operand is an constant + pub fn isOpConstVal(&self) -> bool { + self.inner2.type_id() == TypeId::of::() + } + + /// Returns if the operand is an constant ptr load + pub fn isOpConstAdr(&self) -> bool { + self.inner2.type_id() == TypeId::of::() + } + + /// Returns the operand as a constant value + pub fn getOpConstVal(&self) -> Type { + self.inner2.as_any().downcast_ref::().unwrap().clone() + } + + /// Returns the operand as a constant adr (Const data type) + pub fn getOpConstAdr(&self) -> Const { + self.inner2.as_any().downcast_ref::().unwrap().clone() + } +} + /// Trait used for overloading the BuildAssign function pub trait BuildAssign { /// builds an assignment diff --git a/src/IR/nodes/br.rs b/src/IR/nodes/br.rs index 9c20e827..50234877 100644 --- a/src/IR/nodes/br.rs +++ b/src/IR/nodes/br.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::Support::ColorClass; +use crate::Support::{AsAny, ColorClass}; use crate::IR::block::BlockId; use crate::IR::{Function, Type, Var}; @@ -72,6 +72,15 @@ impl EvalOptVisitor for Br { } } +impl Br where + T: Clone + AsAny + 'static +{ + /// Returns the block which the br branches to + pub fn getBlockToBranch(&self) -> BlockId { + self.inner1.as_any().downcast_ref::().unwrap().to_owned() + } +} + impl Ir for BrCond { fn dump(&self) -> String { format!("br cond {} {}, {}", self.inner1.name, self.inner2.name, self.inner3.name) @@ -150,6 +159,27 @@ impl EvalOptVisitor for BrCond { } } +impl BrCond where + T: Clone + AsAny + 'static, + U: Clone + AsAny + 'static, + Z: Clone + AsAny + 'static +{ + /// Returns the condition variable + pub fn getCondition(&self) -> Var { + self.inner1.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the true branch + pub fn getBranchTrue(&self) -> BlockId { + self.inner2.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the false branch + pub fn getBranchFalse(&self) -> BlockId { + self.inner2.as_any().downcast_ref::().unwrap().to_owned() + } +} + /// This trait is used for building br nodes pub trait BuildBr { /// Builds a br node diff --git a/src/IR/nodes/call.rs b/src/IR/nodes/call.rs index ba632084..42ea5025 100644 --- a/src/IR/nodes/call.rs +++ b/src/IR/nodes/call.rs @@ -95,6 +95,27 @@ impl Ir for Call, Var> { } } +impl Call where + T: Clone + AsAny + 'static, + U: Clone + AsAny + 'static, + Z: Clone + AsAny + 'static +{ + /// Returns the call target + pub fn getCallTarget(&self) -> FuncId { + self.inner1.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the arguments + pub fn getArgs(&self) -> Vec { + self.inner2.as_any().downcast_ref::>().unwrap().to_owned() + } + + /// Returns the variable which stores the result of the call + pub fn getOutputVar(&self) -> Var { + self.inner3.as_any().downcast_ref::().unwrap().to_owned() + } +} + impl EvalOptVisitor for Call, Var> { fn maybe_inline(&self, _: &HashMap) -> Option> { None diff --git a/src/IR/nodes/cast.rs b/src/IR/nodes/cast.rs index 96ba9993..b1c29b55 100644 --- a/src/IR/nodes/cast.rs +++ b/src/IR/nodes/cast.rs @@ -61,6 +61,32 @@ impl Ir for Cast { } } +impl Cast where + T: Clone + AsAny + 'static, + U: Clone + AsAny + 'static, + Z: Clone + AsAny + 'static +{ + /// Returns the input variable + pub fn getInput(&self) -> Var { + self.inner1.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the output variable + pub fn getOutput(&self) -> Var { + self.inner3.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the type to which we cast + pub fn getCastType(&self) -> TypeMetadata { + self.inner2.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the type from which we cast + pub fn getFromType(&self) -> TypeMetadata { + self.getInput().ty + } +} + impl EvalOptVisitor for Cast { fn maybe_inline(&self, vars: &HashMap) -> Option> { if let Some(var) = vars.get(&self.inner1.name) { diff --git a/src/IR/nodes/cmp.rs b/src/IR/nodes/cmp.rs index a9242629..3edce25f 100644 --- a/src/IR/nodes/cmp.rs +++ b/src/IR/nodes/cmp.rs @@ -123,6 +123,33 @@ impl IsNode for Cmp { } } +impl Cmp { + /// Returns the mode with which the node compares + pub fn getCmpMode(&self) -> CmpMode { + self.mode + } + + /// Returns the left side operand + pub fn getLsVar(&self) -> Var { + self.ls.to_owned() + } + + /// Returns the right side operand + pub fn getRsVar(&self) -> Var { + self.rs.to_owned() + } + + /// Returns the output variable + pub fn getOutput(&self) -> Var { + self.out.to_owned() + } + + /// Returns the type of the node + pub fn getType(&self) -> TypeMetadata { + self.out.ty + } +} + /// The trait `BuildCmp` is used to build the cmp node pub trait BuildCmp { /// builds the compare node diff --git a/src/IR/nodes/ret.rs b/src/IR/nodes/ret.rs index a2726774..d5f6de5b 100644 --- a/src/IR/nodes/ret.rs +++ b/src/IR/nodes/ret.rs @@ -1,3 +1,5 @@ +use std::any::TypeId; + use super::*; impl Ir for Return { @@ -133,6 +135,30 @@ impl EvalOptVisitor for Return { } } +impl Return where + T: Clone + AsAny + 'static +{ + /// Returns the node a constant type? + pub fn isRetConst(&self) -> bool { + self.inner1.type_id() == TypeId::of::() + } + + /// Returns the node a variable? + pub fn isRetVar(&self) -> bool { + self.inner1.type_id() == TypeId::of::() + } + + /// Returns the constant the node returns (else panics) + pub fn getRetConst(&self) -> Type { + self.inner1.as_any().downcast_ref::().unwrap().to_owned() + } + + /// Returns the variable the node returns (else panics) + pub fn getRetVar(&self) -> Var { + self.inner1.as_any().downcast_ref::().unwrap().to_owned() + } +} + /// Trait for the return instruction /// Used for overloading the BuildRet function pub trait BuildReturn {