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 Oct 31, 2024
2 parents d7152fd + 5ab4403 commit 7be9fdc
Show file tree
Hide file tree
Showing 27 changed files with 936 additions and 301 deletions.
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::CompilationHelper;
impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_load(&mut self, node: &Load<Var, Var, TypeMetadata>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let ptr = *self.vars.get(&node.inner2.name).expect("expected valid variable");
let ptr = *self.vars.get(&node.inner2.name).expect(&format!("expected valid variable {}", node.inner2.name));
let ptr = ptr.into();

let out = *self.vars.get(&node.inner1.name).unwrap();
Expand Down
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
10 changes: 5 additions & 5 deletions src/IR/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl FunctionType {
index += 1;
}

panic!("the func has {} but argument {} wants to get accesed", self.args.len(), num)
panic!("the func has {} args but args {} is accesed", self.args.len(), num)
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ impl Function {

for index in 0..self.ty.args.len() {
let arg = self.ty.arg(index);
fmt += &format!("{} %{}, ", arg.ty, arg.name);
fmt += &format!("{} {}, ", arg.ty, arg.name);
}

if self.ty.args.len() > 0 {
Expand All @@ -130,7 +130,7 @@ impl Function {

for index in 0..self.ty.args.len() {
let arg = self.ty.arg(index);
fmt += &format!("{} %{}, ", arg.ty, arg.name);
fmt += &format!("{} {}, ", arg.ty, arg.name);
}

if self.ty.args.len() > 0 {
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Function {
let arg = self.ty.arg(index);
fmt += &format!("{} {}, ",
profile.markup(&arg.ty.to_string(), ColorClass::Ty),
profile.markup(&format!("%{}", arg.name), ColorClass::Var)
profile.markup(&format!("{}", arg.name), ColorClass::Var)
);
}

Expand All @@ -189,7 +189,7 @@ impl Function {
let arg = self.ty.arg(index);
fmt += &format!("{} {}, ",
profile.markup(&arg.ty.to_string(), ColorClass::Ty),
profile.markup(&format!("%{}", arg.name), ColorClass::Var)
profile.markup(&format!("{}", arg.name), ColorClass::Var)
);
}

Expand Down
20 changes: 11 additions & 9 deletions src/IR/nodes/alloca.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::IR::{Function, TypeMetadata, Var};
use crate::Support::ColorClass;

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

impl Ir for Alloca<Var, TypeMetadata> {
fn dump(&self) -> String {
Expand Down Expand Up @@ -36,14 +36,6 @@ impl Ir for Alloca<Var, TypeMetadata> {
compiler.compile_alloca(&self, &block, module)
}

fn maybe_inline(&self, _: &std::collections::HashMap<String, crate::prelude::Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}

fn inputs(&self) -> Vec<Var> {
vec![]
}
Expand All @@ -53,6 +45,16 @@ impl Ir for Alloca<Var, TypeMetadata> {
}
}

impl EvalOptVisitor for Alloca<Var, TypeMetadata> {
fn maybe_inline(&self, _: &std::collections::HashMap<String, crate::prelude::Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}
}

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
52 changes: 30 additions & 22 deletions src/IR/nodes/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ impl Ir for Assign<Var, Type> {
compiler.compile_assign_var_type(&self, &block, module)
}

fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}

fn inputs(&self) -> Vec<Var> {
vec![]
Expand All @@ -63,6 +56,16 @@ impl Ir for Assign<Var, Type> {
}
}

impl EvalOptVisitor for Assign<Var, Type> {
fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}
}

impl Ir for Assign<Var, Var> {
fn dump(&self) -> String {
let meta: TypeMetadata = self.inner2.ty;
Expand Down Expand Up @@ -110,6 +113,16 @@ impl Ir for Assign<Var, Var> {
compiler.compile_assign_var_var(&self, &block, module)
}

fn inputs(&self) -> Vec<Var> {
vec![self.inner2.to_owned()]
}

fn output(&self) -> Option<Var> {
Some(self.inner1.to_owned())
}
}

impl EvalOptVisitor for Assign<Var, Var> {
fn maybe_inline(&self, values: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
if let Some(lhs) = values.get(&self.inner2.name) {
Some(Assign::new(self.inner1.to_owned(), *lhs))
Expand All @@ -119,14 +132,6 @@ impl Ir for Assign<Var, Var> {
fn eval(&self) -> Option<Box<dyn Ir>> {
None
}

fn inputs(&self) -> Vec<Var> {
vec![self.inner2.to_owned()]
}

fn output(&self) -> Option<Var> {
Some(self.inner1.to_owned())
}
}

impl Ir for Assign<Var, Const> {
Expand Down Expand Up @@ -167,13 +172,6 @@ impl Ir for Assign<Var, Const> {
compiler.compile_assign_var_const(&self, &block, module)
}

fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}

fn inputs(&self) -> Vec<Var> {
vec![]
Expand All @@ -184,6 +182,16 @@ impl Ir for Assign<Var, Const> {
}
}

impl EvalOptVisitor for Assign<Var, Const> {
fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}
}

/// Trait used for overloading the BuildAssign function
pub trait BuildAssign<T> {
/// builds an assignment
Expand Down
37 changes: 21 additions & 16 deletions src/IR/nodes/br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::Support::ColorClass;
use crate::IR::block::BlockId;
use crate::IR::{Function, Type, Var};

use super::{Br, BrCond, Ir};
use super::{Br, BrCond, EvalOptVisitor, Ir};

impl Ir for Br<BlockId> {
fn dump(&self) -> String {
Expand Down Expand Up @@ -48,13 +48,6 @@ impl Ir for Br<BlockId> {
compiler.compile_br(&self, &block, module)
}

fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}

fn inputs(&self) -> Vec<Var> {
vec![]
Expand All @@ -65,6 +58,16 @@ impl Ir for Br<BlockId> {
}
}

impl EvalOptVisitor for Br<BlockId> {
fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}
}

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 @@ -109,7 +112,17 @@ impl Ir for BrCond<Var, BlockId, BlockId> {
fn compile_dir(&self, compiler: &mut crate::CodeGen::IrCodeGenHelper, block: &crate::prelude::Block, module: &mut crate::prelude::Module) {
compiler.compile_br_cond(&self, &block, module)
}

fn inputs(&self) -> Vec<Var> {
vec![self.inner1.to_owned()]
}

fn output(&self) -> Option<Var> {
None
}
}

impl EvalOptVisitor for BrCond<Var, BlockId, BlockId> {
fn maybe_inline(&self, vars: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
if let Some(check) = vars.get(&self.inner1.name) {
let value = check.val() as i64;
Expand All @@ -127,14 +140,6 @@ impl Ir for BrCond<Var, BlockId, BlockId> {
Some(Br::new( self.inner3.to_owned() ))
} else { None }
}

fn inputs(&self) -> Vec<Var> {
vec![self.inner1.to_owned()]
}

fn output(&self) -> Option<Var> {
None
}
}

/// This trait is used for building br nodes
Expand Down
18 changes: 9 additions & 9 deletions src/IR/nodes/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ impl Ir for Call<FuncId, Vec<Var>, Var> {
compiler.compile_call(&self, &block, module)
}

fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}

fn inputs(&self) -> Vec<Var> {
self.inner2.to_owned()
}
Expand All @@ -99,7 +91,15 @@ impl Ir for Call<FuncId, Vec<Var>, Var> {
}
}


impl EvalOptVisitor for Call<FuncId, Vec<Var>, Var> {
fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}

fn eval(&self) -> Option<Box<dyn Ir>> {
None
}
}

/// Trait for the call instruction
/// Used for overloading the BuildCall function
Expand Down
19 changes: 11 additions & 8 deletions src/IR/nodes/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ impl Ir for Cast<Var, TypeMetadata, Var> {
compiler.compile_cast(&self, &block, module)
}


fn inputs(&self) -> Vec<Var> {
vec![self.inner1.to_owned()]
}

fn output(&self) -> Option<Var> {
Some(self.inner3.to_owned())
}
}

impl EvalOptVisitor for Cast<Var, TypeMetadata, Var> {
fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
}
Expand All @@ -56,14 +67,6 @@ impl Ir for Cast<Var, TypeMetadata, Var> {
Some(Assign::new(self.inner3.to_owned(), self.inner1.to_owned()))
} else { None }
}

fn inputs(&self) -> Vec<Var> {
vec![self.inner1.to_owned()]
}

fn output(&self) -> Option<Var> {
Some(self.inner3.to_owned())
}
}

/// Trait for the cast instruction
Expand Down
20 changes: 11 additions & 9 deletions src/IR/nodes/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
use crate::Support::ColorClass;
use crate::IR::{Function, Type, TypeMetadata, Var, VerifyError};

use super::{Assign, Cmp, Ir};
use super::{Assign, Cmp, EvalOptVisitor, Ir};

/// The "compare mode" (e.g: ls is equal to rs)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -81,6 +81,16 @@ impl Ir for Cmp {
compiler.compile_cmp(&self, &block, module)
}

fn inputs(&self) -> Vec<Var> {
vec![self.ls.to_owned(), self.rs.to_owned()]
}

fn output(&self) -> Option<Var> {
Some(self.out.to_owned())
}
}

impl EvalOptVisitor for Cmp {
fn maybe_inline(&self, _: &std::collections::HashMap<String, crate::prelude::Type>) -> Option<Box<dyn Ir>> {
None
}
Expand All @@ -101,14 +111,6 @@ impl Ir for Cmp {
)))
} else { None }
}

fn inputs(&self) -> Vec<Var> {
vec![self.ls.to_owned(), self.rs.to_owned()]
}

fn output(&self) -> Option<Var> {
Some(self.out.to_owned())
}
}

/// The trait `BuildCmp` is used to build the cmp node
Expand Down
Loading

0 comments on commit 7be9fdc

Please sign in to comment.