Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Cr0a3/ygen
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Nov 3, 2024
2 parents 626624c + 25c2f7c commit 2ff6af6
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/IR/nodes/alloca.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::IR::{Function, TypeMetadata, Var};
use crate::Support::ColorClass;
use crate::Support::{AsAny, ColorClass};

use super::{Alloca, EvalOptVisitor, Ir};

Expand Down Expand Up @@ -59,6 +59,21 @@ impl EvalOptVisitor for Alloca<Var, TypeMetadata> {
}
}

impl<T, U> Alloca<T, U> where
T: Clone + AsAny + 'static,
U: Clone + AsAny + 'static
{
/// Returns the output variable
pub fn getOut(&self) -> Var {
self.inner1.as_any().downcast_ref::<Var>().unwrap().to_owned()
}

/// Returns the type which is allocated
pub fn getTypeToAlloc(&self) -> TypeMetadata {
self.inner2.as_any().downcast_ref::<TypeMetadata>().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 {
Expand Down
36 changes: 36 additions & 0 deletions src/IR/nodes/assign.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::any::TypeId;

use super::*;

impl Ir for Assign<Var, Type> {
Expand Down Expand Up @@ -204,6 +206,40 @@ impl EvalOptVisitor for Assign<Var, Const> {
}
}

impl<U> Assign<Var, U> 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::<Type>()
}

/// Returns if the operand is an constant ptr load
pub fn isOpConstAdr(&self) -> bool {
self.inner2.type_id() == TypeId::of::<Const>()
}

/// Returns the operand as a constant value
pub fn getOpConstVal(&self) -> Type {
self.inner2.as_any().downcast_ref::<Type>().unwrap().clone()
}

/// Returns the operand as a constant adr (Const data type)
pub fn getOpConstAdr(&self) -> Const {
self.inner2.as_any().downcast_ref::<Const>().unwrap().clone()
}
}

/// Trait used for overloading the BuildAssign function
pub trait BuildAssign<T> {
/// builds an assignment
Expand Down
32 changes: 31 additions & 1 deletion src/IR/nodes/br.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -72,6 +72,15 @@ impl EvalOptVisitor for Br<BlockId> {
}
}

impl<T> Br<T> where
T: Clone + AsAny + 'static
{
/// Returns the block which the br branches to
pub fn getBlockToBranch(&self) -> BlockId {
self.inner1.as_any().downcast_ref::<BlockId>().unwrap().to_owned()
}
}

impl Ir for BrCond<Var, BlockId, BlockId> {
fn dump(&self) -> String {
format!("br cond {} {}, {}", self.inner1.name, self.inner2.name, self.inner3.name)
Expand Down Expand Up @@ -150,6 +159,27 @@ impl EvalOptVisitor for BrCond<Var, BlockId, BlockId> {
}
}

impl<T, U, Z> BrCond<T, U, Z> 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::<Var>().unwrap().to_owned()
}

/// Returns the true branch
pub fn getBranchTrue(&self) -> BlockId {
self.inner2.as_any().downcast_ref::<BlockId>().unwrap().to_owned()
}

/// Returns the false branch
pub fn getBranchFalse(&self) -> BlockId {
self.inner2.as_any().downcast_ref::<BlockId>().unwrap().to_owned()
}
}

/// This trait is used for building br nodes
pub trait BuildBr<T> {
/// Builds a br node
Expand Down
21 changes: 21 additions & 0 deletions src/IR/nodes/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ impl Ir for Call<FuncId, Vec<Var>, Var> {
}
}

impl<T, U, Z> Call<T, U, Z> 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::<FuncId>().unwrap().to_owned()
}

/// Returns the arguments
pub fn getArgs(&self) -> Vec<Var> {
self.inner2.as_any().downcast_ref::<Vec<Var>>().unwrap().to_owned()
}

/// Returns the variable which stores the result of the call
pub fn getOutputVar(&self) -> Var {
self.inner3.as_any().downcast_ref::<Var>().unwrap().to_owned()
}
}

impl EvalOptVisitor for Call<FuncId, Vec<Var>, Var> {
fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
Expand Down
26 changes: 26 additions & 0 deletions src/IR/nodes/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ impl Ir for Cast<Var, TypeMetadata, Var> {
}
}

impl<T, U, Z> Cast<T, U, Z> 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::<Var>().unwrap().to_owned()
}

/// Returns the output variable
pub fn getOutput(&self) -> Var {
self.inner3.as_any().downcast_ref::<Var>().unwrap().to_owned()
}

/// Returns the type to which we cast
pub fn getCastType(&self) -> TypeMetadata {
self.inner2.as_any().downcast_ref::<TypeMetadata>().unwrap().to_owned()
}

/// Returns the type from which we cast
pub fn getFromType(&self) -> TypeMetadata {
self.getInput().ty
}
}

impl EvalOptVisitor for Cast<Var, TypeMetadata, Var> {
fn maybe_inline(&self, vars: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
if let Some(var) = vars.get(&self.inner1.name) {
Expand Down
27 changes: 27 additions & 0 deletions src/IR/nodes/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/IR/nodes/ret.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::any::TypeId;

use super::*;

impl Ir for Return<Type> {
Expand Down Expand Up @@ -133,6 +135,30 @@ impl EvalOptVisitor for Return<Var> {
}
}

impl<T> Return<T> where
T: Clone + AsAny + 'static
{
/// Returns the node a constant type?
pub fn isRetConst(&self) -> bool {
self.inner1.type_id() == TypeId::of::<Type>()
}

/// Returns the node a variable?
pub fn isRetVar(&self) -> bool {
self.inner1.type_id() == TypeId::of::<Var>()
}

/// Returns the constant the node returns (else panics)
pub fn getRetConst(&self) -> Type {
self.inner1.as_any().downcast_ref::<Type>().unwrap().to_owned()
}

/// Returns the variable the node returns (else panics)
pub fn getRetVar(&self) -> Var {
self.inner1.as_any().downcast_ref::<Var>().unwrap().to_owned()
}
}

/// Trait for the return instruction
/// Used for overloading the BuildRet function
pub trait BuildReturn<T> {
Expand Down

0 comments on commit 2ff6af6

Please sign in to comment.