From a87b472210aef5dfc46aed5928212482cf3f3c76 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Wed, 18 Dec 2024 09:57:09 +0100 Subject: [PATCH] Update --- xrcf/src/dialect/cf/op.rs | 26 ++++++++++---------------- xrcf/src/dialect/llvm/op.rs | 2 -- xrcf/src/targ3t/llvmir/op.rs | 22 +++++----------------- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/xrcf/src/dialect/cf/op.rs b/xrcf/src/dialect/cf/op.rs index f90d43e1..f44ea05c 100644 --- a/xrcf/src/dialect/cf/op.rs +++ b/xrcf/src/dialect/cf/op.rs @@ -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; @@ -23,15 +23,14 @@ const TOKEN_KIND: TokenKind = TokenKind::PercentIdentifier; /// ``` pub struct BranchOp { operation: Arc>, - dest: Option>>, } impl BranchOp { - pub fn dest(&self) -> Option>> { - self.dest.clone() + pub fn dest(&self) -> Option>> { + self.operation().operand(0) } - pub fn set_dest(&mut self, dest: Option>>) { - self.dest = dest; + pub fn set_dest(&mut self, dest: Arc>) { + self.operation().set_operand(0, dest); } } @@ -40,10 +39,7 @@ impl Op for BranchOp { OperationName::new("cf.br".to_string()) } fn new(operation: Arc>) -> Self { - BranchOp { - operation, - dest: None, - } + BranchOp { operation } } fn as_any(&self) -> &dyn std::any::Any { self @@ -54,12 +50,9 @@ impl Op for BranchOp { fn operation(&self) -> &Arc> { &self.operation } - fn block_destination(&self) -> Option>> { - 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(); @@ -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) } diff --git a/xrcf/src/dialect/llvm/op.rs b/xrcf/src/dialect/llvm/op.rs index 2dca9ac6..34973e52 100644 --- a/xrcf/src/dialect/llvm/op.rs +++ b/xrcf/src/dialect/llvm/op.rs @@ -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; @@ -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; diff --git a/xrcf/src/targ3t/llvmir/op.rs b/xrcf/src/targ3t/llvmir/op.rs index 13e287af..2811362f 100644 --- a/xrcf/src/targ3t/llvmir/op.rs +++ b/xrcf/src/targ3t/llvmir/op.rs @@ -247,15 +247,11 @@ 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>, - dest: Option>>, -} - -impl BranchOp { - pub fn set_dest(&mut self, dest: Arc>) { - self.dest = Some(dest); - } } impl Op for BranchOp { @@ -263,10 +259,7 @@ impl Op for BranchOp { OperationName::new("branch".to_string()) } fn new(operation: Arc>) -> Self { - BranchOp { - operation, - dest: None, - } + BranchOp { operation } } fn as_any(&self) -> &dyn std::any::Any { self @@ -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(()) } }