Skip to content

Commit

Permalink
[IR] switching to IROperand system for select
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Nov 9, 2024
1 parent 3906187 commit ac82de7
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 554 deletions.
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl crate::IR::ir::IROperand {
fn into_mi(&self, compiler: &mut CompilationHelper) -> MachineOperand {
match self {
crate::prelude::IROperand::Type(ty) => MachineOperand::Imm(ty.val()),
crate::prelude::IROperand::Var(var) => (*compiler.vars.get(&var.name).unwrap()).into(),
crate::prelude::IROperand::Var(var) => (*compiler.vars.get(&var.name).expect(&format!("unknown variable: {}", var.name))).into(),
}
}
}
3 changes: 2 additions & 1 deletion src/CodeGen/compilation/ret.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{prelude::{IROperand, Return}, CodeGen::{MachineInstr, MachineMnemonic, MachineOperand}, IR::{Block, Type, Var}};
use crate::prelude::{IROperand, Return, Block};
use crate::CodeGen::{MachineInstr, MachineMnemonic};

use super::CompilationHelper;

Expand Down
119 changes: 8 additions & 111 deletions src/CodeGen/compilation/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,22 @@ use crate::prelude::*;

impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_select_tt(&mut self, node: &Select<Type, Type>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let out = self.vars.get(&node.out.name).expect("expected valid variable");
let cond = self.vars.get(&node.cond.name).expect("expected valid variable");

let out = (*out).into();
let cond = (*cond).into();

let yes = MachineOperand::Imm(node.yes.val());
let no = MachineOperand::Imm(node.no.val());

let mut yes_instr = MachineInstr::new(MachineMnemonic::MovIfZero);

yes_instr.set_out(out);
yes_instr.add_operand(cond);
yes_instr.add_operand(no);

yes_instr.meta = node.yes.into();

mc_sink.push(yes_instr);

let mut no_instr = MachineInstr::new(MachineMnemonic::MovIfNotZero);

no_instr.set_out(out);
no_instr.add_operand(cond);
no_instr.add_operand(yes);
pub fn compile_select(&mut self, node: &Select<IROperand, IROperand>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let out = (*self.vars.get(&node.out.name).expect("expected valid variable")).into();
let cond = (*self.vars.get(&node.cond.name).expect("expected valid variable")).into();

no_instr.meta = node.no.into();
let yes = node.yes.into_mi(self);
let no = node.no.into_mi(self);

mc_sink.push(no_instr);
}

#[allow(missing_docs)]
pub fn compile_select_tv(&mut self, node: &Select<Type, Var>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let out = self.vars.get(&node.out.name).expect("expected valid variable");
let cond = self.vars.get(&node.cond.name).expect("expected valid variable");

let no = self.vars.get(&node.no.name).expect("expected valid variable");

let out = (*out).into();
let cond = (*cond).into();
let no = (*no).into();

let yes = MachineOperand::Imm(node.yes.val());
let ty = node.yes.get_ty();

let mut yes_instr = MachineInstr::new(MachineMnemonic::MovIfZero);

yes_instr.set_out(out);
yes_instr.add_operand(cond);
yes_instr.add_operand(yes);

yes_instr.meta = node.yes.into();
yes_instr.meta = ty;

mc_sink.push(yes_instr);

Expand All @@ -64,75 +29,7 @@ impl CompilationHelper {
no_instr.add_operand(cond);
no_instr.add_operand(no);

no_instr.meta = node.no.ty;

mc_sink.push(no_instr);
}

#[allow(missing_docs)]
pub fn compile_select_vt(&mut self, node: &Select<Var, Type>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let out = self.vars.get(&node.out.name).expect("expected valid variable");
let cond = self.vars.get(&node.cond.name).expect("expected valid variable");

let yes = self.vars.get(&node.yes.name).expect("expected valid variable");

let out = (*out).into();
let cond = (*cond).into();
let yes = (*yes).into();

let no = MachineOperand::Imm(node.no.val());

let mut yes_instr = MachineInstr::new(MachineMnemonic::MovIfZero);

yes_instr.set_out(out);
yes_instr.add_operand(cond);
yes_instr.add_operand(no);

yes_instr.meta = node.yes.ty;

mc_sink.push(yes_instr);

let mut no_instr = MachineInstr::new(MachineMnemonic::MovIfNotZero);

no_instr.set_out(out);
no_instr.add_operand(cond);
no_instr.add_operand(yes);

no_instr.meta = node.no.into();

mc_sink.push(no_instr);
}

#[allow(missing_docs)]
pub fn compile_select_vv(&mut self, node: &Select<Var, Var>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let out = self.vars.get(&node.out.name).expect("expected valid variable");
let cond = self.vars.get(&node.cond.name).expect("expected valid variable");

let yes = self.vars.get(&node.yes.name).expect("expected valid variable");
let no = self.vars.get(&node.no.name).expect("expected valid variable");

let out = (*out).into();
let cond = (*cond).into();
let yes = (*yes).into();
let no = (*no).into();

let mut yes_instr = MachineInstr::new(MachineMnemonic::MovIfZero);

yes_instr.set_out(out);
yes_instr.add_operand(cond);
yes_instr.add_operand(no);

yes_instr.meta = node.yes.ty;

mc_sink.push(yes_instr);

let mut no_instr = MachineInstr::new(MachineMnemonic::MovIfNotZero);

no_instr.set_out(out);
no_instr.add_operand(cond);
no_instr.add_operand(yes);

no_instr.meta = node.no.ty;
no_instr.meta = ty;

mc_sink.push(no_instr);
}
Expand Down
19 changes: 2 additions & 17 deletions src/CodeGen/ir_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,9 @@ impl IrCodeGenHelper {
ir::Neg<Var, Var>
);
ir_codegen_wrap!(
compile_select_tt,
compile_select,
"Loweres the select ty ty node",
ir::Select<Type, Type>
);
ir_codegen_wrap!(
compile_select_vt,
"Loweres the select var ty node",
ir::Select<Var, Type>
);
ir_codegen_wrap!(
compile_select_tv,
"Loweres the select ty var node",
ir::Select<Type, Var>
);
ir_codegen_wrap!(
compile_select_vv,
"Loweres the select var var node",
ir::Select<Var, Var>
ir::Select<ir::IROperand, ir::IROperand>
);
ir_codegen_wrap!(
compile_getelemptr,
Expand Down
20 changes: 20 additions & 0 deletions src/IR/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ impl IROperand {
IROperand::Var(var) => var.ty,
}
}

#[inline]
/// Returns the unwraped inner type value (else it panics)
pub fn get_typeconst(&self) -> Type {
let IROperand::Type(ret) = self else { panic!(); };
return *ret;
}

#[inline]
/// Returns the unwraped inner var value (else it panics)
pub fn get_var(&self) -> Var {
let IROperand::Var(ret) = self else { panic!(); };
return ret.to_owned();
}
}

impl std::fmt::Display for IROperand {
Expand All @@ -388,4 +402,10 @@ impl std::fmt::Display for IROperand {
IROperand::Var(var) => var.name.to_string(),
})
}
}

impl AsAny for IROperand {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
21 changes: 14 additions & 7 deletions src/IR/nodes/ret.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::any::TypeId;

use super::*;

impl Ir for Return<IROperand> {
Expand Down Expand Up @@ -45,7 +43,8 @@ impl Ir for Return<IROperand> {
}

fn inputs(&self) -> Vec<Var> {
vec![]
if let IROperand::Var(ret) = &self.inner1 { vec![ret.to_owned()] }
else { vec![] }
}

fn inputs_mut(&mut self) -> Vec<&mut Var> {
Expand Down Expand Up @@ -78,22 +77,30 @@ impl<T> Return<T> where
{
/// Returns the node a constant type?
pub fn isRetConst(&self) -> bool {
self.inner1.type_id() == TypeId::of::<Type>()
if let Some(op) = self.inner1.as_any().downcast_ref::<IROperand>() {
op.is_type()
} else { panic!() }
}

/// Returns the node a variable?
pub fn isRetVar(&self) -> bool {
self.inner1.type_id() == TypeId::of::<Var>()
if let Some(op) = self.inner1.as_any().downcast_ref::<IROperand>() {
op.is_var()
} else { panic!() }
}

/// Returns the constant the node returns (else panics)
pub fn getRetConst(&self) -> Type {
self.inner1.as_any().downcast_ref::<Type>().unwrap().to_owned()
if let Some(op) = self.inner1.as_any().downcast_ref::<IROperand>() {
op.get_typeconst()
} else { panic!() }
}

/// Returns the variable the node returns (else panics)
pub fn getRetVar(&self) -> Var {
self.inner1.as_any().downcast_ref::<Var>().unwrap().to_owned()
if let Some(op) = self.inner1.as_any().downcast_ref::<IROperand>() {
op.get_var()
} else { panic!() }
}
}

Expand Down
Loading

0 comments on commit ac82de7

Please sign in to comment.