From 01494e44a43dfc7006f7318b5a94197c48c6d5bb Mon Sep 17 00:00:00 2001 From: Akhil Guruprasad Date: Mon, 29 Apr 2024 14:28:33 +0530 Subject: [PATCH 1/2] post review refactoring --- dncil/cil/body/__init__.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/dncil/cil/body/__init__.py b/dncil/cil/body/__init__.py index a0708f6..a71e4ab 100644 --- a/dncil/cil/body/__init__.py +++ b/dncil/cil/body/__init__.py @@ -216,16 +216,13 @@ def parse_basic_blocks(self): # 3. Instructions that immediately follow unconditional or conditional jump/goto statements are considered leaders # https://www.geeksforgeeks.org/basic-blocks-in-compiler-design/ - leaders: Set[int] = set() + # add #1: first instruction is a leader + leaders: Set[int] = set(self.instructions[:1]) if self.instructions else set() for idx, insn in enumerate(self.instructions): - if idx == 0: - # add #1 - leaders.add(insn.offset) - if any((insn.is_br(), insn.is_cond_br(), insn.is_leave())): - # add #2 + # add #2: targets of branches are leaders leaders.add(cast(int, insn.operand)) - # add #3 + # add #3: instructions immediately following unconditional or conditional branches are leaders try: leaders.add(self.instructions[idx + 1].offset) except IndexError: @@ -239,10 +236,9 @@ def parse_basic_blocks(self): # new leader, new basic block bb_curr = BasicBlock(instructions=[insn]) self.basic_blocks.append(bb_curr) - continue - - assert bb_curr is not None - bb_curr.instructions.append(insn) + else: + assert bb_curr is not None + bb_curr.instructions.append(insn) # create mapping of first instruction to basic block bb_map: Dict[int, BasicBlock] = {} @@ -265,11 +261,14 @@ def parse_basic_blocks(self): # no fallthrough continue - # connect fallthrough + # connect fallthrough: + # if a basic block has a fallthrough successor, it will be the next basic block in our list + # since instructions, leaders, and basic blocks are processed sequentially try: bb_next: BasicBlock = self.basic_blocks[idx + 1] - bb.succs.append(bb_next) - bb_next.preds.append(bb) except IndexError: # end of method continue + else: + bb.succs.append(bb_next) + bb_next.preds.append(bb) From 8a757942d02c47cde4a0b3226a69440f8ff4895b Mon Sep 17 00:00:00 2001 From: Akhil G <46602926+akhilguruprasad22@users.noreply.github.com> Date: Wed, 1 May 2024 13:32:31 +0530 Subject: [PATCH 2/2] Update dncil/cil/body/__init__.py Co-authored-by: Mike Hunhoff --- dncil/cil/body/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dncil/cil/body/__init__.py b/dncil/cil/body/__init__.py index a71e4ab..f480bd8 100644 --- a/dncil/cil/body/__init__.py +++ b/dncil/cil/body/__init__.py @@ -217,7 +217,9 @@ def parse_basic_blocks(self): # https://www.geeksforgeeks.org/basic-blocks-in-compiler-design/ # add #1: first instruction is a leader - leaders: Set[int] = set(self.instructions[:1]) if self.instructions else set() + if not self.instructions: + return + leaders: Set[int] = set([self.instructions[0].offset]) for idx, insn in enumerate(self.instructions): if any((insn.is_br(), insn.is_cond_br(), insn.is_leave())): # add #2: targets of branches are leaders