From 0651175da61998dfe179c2d1148688ce809c49da Mon Sep 17 00:00:00 2001 From: augustin64 Date: Mon, 21 Oct 2024 13:45:24 +0200 Subject: [PATCH] TP04 CFG --- MiniC/TP04/BuildCFG.py | 18 +++++++++++++++--- MiniC/TP04/LinearizeCFG.py | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/MiniC/TP04/BuildCFG.py b/MiniC/TP04/BuildCFG.py index 851e094..7df44d2 100644 --- a/MiniC/TP04/BuildCFG.py +++ b/MiniC/TP04/BuildCFG.py @@ -20,7 +20,13 @@ def find_leaders(instructions: List[CodeStatement]) -> List[int]: last is len(instructions) """ leaders: List[int] = [0] - # TODO fill leaders + + for i, instr in enumerate(instructions): + if isinstance(instr, Label): + leaders.append(i) + elif isinstance(instr, AbsoluteJump) or isinstance(instr, ConditionalJump): + leaders.append(i+1) + # The final "ret" is also a form of jump leaders.append(len(instructions)) return leaders @@ -64,9 +70,15 @@ def prepare_chunk(pre_chunk: List[CodeStatement], fdata: FunctionData) -> tuple[ jump = None inner_statements: List[CodeStatement] = pre_chunk # Extract the first instruction from inner_statements if it is a label, or create a fresh one - raise NotImplementedError() # TODO + if isinstance(inner_statements[0], Label): + label = inner_statements[0] + inner_statements = inner_statements[1:] + else: + label = fdata.fresh_label(fdata._name) # Extract the last instruction from inner_statements if it is a jump, or do nothing - raise NotImplementedError() # TODO + if len(inner_statements) > 0 and (isinstance(inner_statements[-1], ConditionalJump) or isinstance(inner_statements[-1], AbsoluteJump)): + jump = inner_statements[-1] + inner_statements = inner_statements[:-1] # Check that there is no other label or jump left in inner_statements l: List[BlockInstr] = [] for i in inner_statements: diff --git a/MiniC/TP04/LinearizeCFG.py b/MiniC/TP04/LinearizeCFG.py index a3b6a37..1149d19 100644 --- a/MiniC/TP04/LinearizeCFG.py +++ b/MiniC/TP04/LinearizeCFG.py @@ -20,7 +20,6 @@ def linearize(cfg: CFG) -> List[Statement]: """ Linearize the given control flow graph as a list of instructions. """ - # TODO l: List[Statement] = [] # Linearized CFG blocks: List[Block] = ordered_blocks_list(cfg) # All blocks of the CFG for i, block in enumerate(blocks): @@ -34,7 +33,8 @@ def linearize(cfg: CFG) -> List[Statement]: l.append(ConditionalJump(j.cond, j.op1, j.op2, j.label_then)) l.append(AbsoluteJump(j.label_else)) case AbsoluteJump() as j: - l.append(AbsoluteJump(j.label)) + if i+1 == len(blocks) or j != blocks[i+1].get_label(): + l.append(AbsoluteJump(j.label)) case Return(): l.append(AbsoluteJump(cfg.get_end())) return l