Mon commit avec mes modifs à moi ! Na
This commit is contained in:
+19
-9
@@ -6,7 +6,7 @@ Functions to convert a CFG into SSA Form.
|
||||
from typing import List, Dict, Set
|
||||
from Lib.CFG import Block, CFG
|
||||
from Lib.Operands import Renamer
|
||||
from Lib.Statement import Instruction
|
||||
from Lib.Statement import Instruction,Label,Operand
|
||||
from Lib.PhiNode import PhiNode
|
||||
from Lib.Dominators import computeDom, computeDT, computeDF
|
||||
|
||||
@@ -25,8 +25,13 @@ def insertPhis(cfg: CFG, DF: Dict[Block, Set[Block]]) -> None:
|
||||
d = queue.pop(0)
|
||||
for b in DF[d]:
|
||||
if b not in has_phi:
|
||||
# TODO add a phi node in block `b` (Lab 5a, Exercise 4)
|
||||
raise NotImplementedError("insertPhis")
|
||||
params : Dict[Label,Operand] = {}
|
||||
for bi in (set(b.get_in())):
|
||||
params[bi.get_label()] = var
|
||||
phi = PhiNode(var,params)
|
||||
b._phis.append(phi)
|
||||
queue.append(b)
|
||||
has_phi.add(b)
|
||||
|
||||
|
||||
def rename_block(cfg: CFG, DT: Dict[Block, Set[Block]], renamer: Renamer, b: Block) -> None:
|
||||
@@ -43,18 +48,19 @@ def rename_block(cfg: CFG, DT: Dict[Block, Set[Block]], renamer: Renamer, b: Blo
|
||||
for i in succ._phis:
|
||||
assert (isinstance(i, PhiNode))
|
||||
i.rename_from(renamer, b.get_label())
|
||||
# TODO recursive call(s) of rename_block (Lab 5a, Exercise 5)
|
||||
|
||||
for dtsucc in DT[b]:
|
||||
rename_block(cfg,DT,renamer, dtsucc)
|
||||
|
||||
def rename_variables(cfg: CFG, DT: Dict[Block, Set[Block]]) -> None:
|
||||
"""
|
||||
Rename variables in the CFG, to transform `temp_x = φ(temp_x, ..., temp_x)`
|
||||
into `temp_x = φ(temp_0, ... temp_n)`.
|
||||
into `temp_x = φ(temp_0, ..., temp_n)`.
|
||||
|
||||
This is an helper function called during SSA entry.
|
||||
"""
|
||||
renamer = Renamer(cfg.fdata._pool)
|
||||
# TODO initial call(s) to rename_block (Lab 5a, Exercise 5)
|
||||
for etr in cfg.get_entries():
|
||||
rename_block(cfg,DT,renamer,etr)
|
||||
|
||||
|
||||
def enter_ssa(cfg: CFG, dom_graphs=False, basename="prog") -> None:
|
||||
@@ -66,5 +72,9 @@ def enter_ssa(cfg: CFG, dom_graphs=False, basename="prog") -> None:
|
||||
`dom_graphs` indicates if we have to print the domination graphs.
|
||||
`basename` is used for the names of the produced graphs.
|
||||
"""
|
||||
# TODO implement this function (Lab 5a, Exercise 2)
|
||||
raise NotImplementedError("enter_ssa")
|
||||
# Compute the DF
|
||||
dom = computeDom(cfg)
|
||||
dt = computeDT(cfg,dom,dom_graphs,basename)
|
||||
df = computeDF(cfg,dom,dt,dom_graphs, basename)
|
||||
insertPhis(cfg,df)
|
||||
rename_variables(cfg,dt)
|
||||
|
||||
+34
-8
@@ -4,6 +4,7 @@ Functions to convert a CFG out of SSA Form.
|
||||
"""
|
||||
|
||||
from typing import cast, List, Set, Tuple
|
||||
from Lib.Errors import MiniCInternalError
|
||||
from Lib import RiscV
|
||||
from Lib.Graphes import DiGraph
|
||||
from Lib.CFG import Block, BlockInstr, CFG
|
||||
@@ -24,8 +25,10 @@ def generate_moves_from_phis(phis: List[PhiNode], parent: Block) -> List[BlockIn
|
||||
This is an helper function called during SSA exit.
|
||||
"""
|
||||
moves: List[BlockInstr] = []
|
||||
# TODO compute 'moves', a list of 'mv' instructions to insert under parent
|
||||
# (Lab 5a, Exercise 6)
|
||||
plabel = parent.get_label()
|
||||
for phi in phis:
|
||||
if(plabel in phi.used()):
|
||||
moves.append(RiscV.mv(phi.var,phi.used()[plabel]))
|
||||
return moves
|
||||
|
||||
|
||||
@@ -38,11 +41,34 @@ def exit_ssa(cfg: CFG, is_smart: bool) -> None:
|
||||
for b in cfg.get_blocks():
|
||||
phis = cast(List[PhiNode], b._phis) # Use cast for Pyright
|
||||
b._phis = [] # Remove all phi nodes in the block
|
||||
blabel = b.get_label()
|
||||
parents: List[Block] = b.get_in().copy() # Copy as we modify it by adding blocks
|
||||
for parent in parents:
|
||||
moves = generate_moves_from_phis(phis, parent)
|
||||
# TODO Add the block containing 'moves' to 'cfg'
|
||||
# and update edges and jumps accordingly (Lab 5a, Exercise 6)
|
||||
raise NotImplementedError("exit_ssa")
|
||||
|
||||
for p in parents:
|
||||
moves = generate_moves_from_phis(phis, p)
|
||||
if(len(moves)==0):
|
||||
continue
|
||||
# Creating the block
|
||||
ilabel = cfg.fdata.fresh_label(p.get_label().name+"_to_"+b.get_label().name)
|
||||
i = Block(ilabel,moves,AbsoluteJump(blabel))
|
||||
# Add the block
|
||||
cfg.add_block(i)
|
||||
# Changing the jumps
|
||||
ot = p.get_terminator()
|
||||
if(isinstance(ot,BranchingTerminator)):
|
||||
if(ot.label_then == blabel):
|
||||
ot.label_then = ilabel
|
||||
if(ot.label_else == blabel):
|
||||
ot.label_else = ilabel
|
||||
elif(isinstance(ot,AbsoluteJump)):
|
||||
assert(ot.label == blabel)
|
||||
ot = AbsoluteJump(ilabel)
|
||||
elif(isinstance(ot,Return)):
|
||||
raise MiniCInternalError("Malformed CFG, cannot have a return in a parent block")
|
||||
else:
|
||||
raise MiniCInternalError("I don't know of this terminator type:",type(ot))
|
||||
p.set_terminator(ot) # This instruction might be useless
|
||||
# Moving from p -> b to p -> i -> b
|
||||
cfg.remove_edge(p,b)
|
||||
cfg.add_edge(p,i)
|
||||
cfg.add_edge(i,b)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user