Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer committed Dec 18, 2024
1 parent 4390682 commit a87b472
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
26 changes: 10 additions & 16 deletions xrcf/src/dialect/cf/op.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ir::Block;
use crate::ir::BlockDest;
use crate::ir::GuardedOperation;
use crate::ir::Op;
use crate::ir::OpOperand;
use crate::ir::Operation;
use crate::ir::OperationName;
use crate::parser::Parse;
Expand All @@ -23,15 +23,14 @@ const TOKEN_KIND: TokenKind = TokenKind::PercentIdentifier;
/// ```
pub struct BranchOp {
operation: Arc<RwLock<Operation>>,
dest: Option<Arc<RwLock<BlockDest>>>,
}

impl BranchOp {
pub fn dest(&self) -> Option<Arc<RwLock<BlockDest>>> {
self.dest.clone()
pub fn dest(&self) -> Option<Arc<RwLock<OpOperand>>> {
self.operation().operand(0)
}
pub fn set_dest(&mut self, dest: Option<Arc<RwLock<BlockDest>>>) {
self.dest = dest;
pub fn set_dest(&mut self, dest: Arc<RwLock<OpOperand>>) {
self.operation().set_operand(0, dest);
}
}

Expand All @@ -40,10 +39,7 @@ impl Op for BranchOp {
OperationName::new("cf.br".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
BranchOp {
operation,
dest: None,
}
BranchOp { operation }
}
fn as_any(&self) -> &dyn std::any::Any {
self
Expand All @@ -54,12 +50,9 @@ impl Op for BranchOp {
fn operation(&self) -> &Arc<RwLock<Operation>> {
&self.operation
}
fn block_destination(&self) -> Option<Arc<RwLock<BlockDest>>> {
self.dest.clone()
}
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
write!(f, "{} ", self.operation.name())?;
let dest = self.dest.as_ref().expect("Dest not set");
let dest = self.dest().expect("Dest not set");
let dest = dest.try_read().unwrap();
write!(f, "{}", dest)?;
let operands = self.operation().operands();
Expand Down Expand Up @@ -99,9 +92,10 @@ impl Parse for BranchOp {
}
parser.expect(TokenKind::RParen)?;
}
let dest = Some(Arc::new(RwLock::new(dest)));

let op = BranchOp { operation, dest };
let mut op = BranchOp { operation };
let dest = Arc::new(RwLock::new(dest));
op.set_dest(dest);
let op = Arc::new(RwLock::new(op));
Ok(op)
}
Expand Down
2 changes: 0 additions & 2 deletions xrcf/src/dialect/llvm/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::ir::AnyAttr;
use crate::ir::Attribute;
use crate::ir::Attributes;
use crate::ir::Block;
use crate::ir::BlockDest;
use crate::ir::GuardedOpOperand;
use crate::ir::GuardedOperation;
use crate::ir::Op;
Expand All @@ -16,7 +15,6 @@ use crate::ir::Operation;
use crate::ir::OperationName;
use crate::ir::StringAttr;
use crate::ir::Type;
use crate::ir::Value;
use crate::parser::Parse;
use crate::parser::Parser;
use crate::parser::ParserDispatch;
Expand Down
22 changes: 5 additions & 17 deletions xrcf/src/targ3t/llvmir/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,26 +247,19 @@ impl Display for CallOp {
}

/// `br`
///
/// Can be a conditional as well as an unconditional branch. Branch targets
/// are stored as operands.
pub struct BranchOp {
operation: Arc<RwLock<Operation>>,
dest: Option<Arc<RwLock<BlockDest>>>,
}

impl BranchOp {
pub fn set_dest(&mut self, dest: Arc<RwLock<BlockDest>>) {
self.dest = Some(dest);
}
}

impl Op for BranchOp {
fn operation_name() -> OperationName {
OperationName::new("branch".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
BranchOp {
operation,
dest: None,
}
BranchOp { operation }
}
fn as_any(&self) -> &dyn std::any::Any {
self
Expand All @@ -276,12 +269,7 @@ impl Op for BranchOp {
}
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
write!(f, "br ")?;
if let Some(dest) = &self.dest {
write!(f, "label {}", dest.try_read().unwrap())?;
} else {
// Conditional branch (e.g., `br i1 %cond, label %then, label %else`).
display_operands(f, &self.operation().operands())?;
}
display_operands(f, &self.operation().operands())?;
Ok(())
}
}
Expand Down

0 comments on commit a87b472

Please sign in to comment.