TP04 CFG
This commit is contained in:
parent
7479ee19d8
commit
0651175da6
@ -20,7 +20,13 @@ def find_leaders(instructions: List[CodeStatement]) -> List[int]:
|
|||||||
last is len(instructions)
|
last is len(instructions)
|
||||||
"""
|
"""
|
||||||
leaders: List[int] = [0]
|
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
|
# The final "ret" is also a form of jump
|
||||||
leaders.append(len(instructions))
|
leaders.append(len(instructions))
|
||||||
return leaders
|
return leaders
|
||||||
@ -64,9 +70,15 @@ def prepare_chunk(pre_chunk: List[CodeStatement], fdata: FunctionData) -> tuple[
|
|||||||
jump = None
|
jump = None
|
||||||
inner_statements: List[CodeStatement] = pre_chunk
|
inner_statements: List[CodeStatement] = pre_chunk
|
||||||
# Extract the first instruction from inner_statements if it is a label, or create a fresh one
|
# 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
|
# 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
|
# Check that there is no other label or jump left in inner_statements
|
||||||
l: List[BlockInstr] = []
|
l: List[BlockInstr] = []
|
||||||
for i in inner_statements:
|
for i in inner_statements:
|
||||||
|
@ -20,7 +20,6 @@ def linearize(cfg: CFG) -> List[Statement]:
|
|||||||
"""
|
"""
|
||||||
Linearize the given control flow graph as a list of instructions.
|
Linearize the given control flow graph as a list of instructions.
|
||||||
"""
|
"""
|
||||||
# TODO
|
|
||||||
l: List[Statement] = [] # Linearized CFG
|
l: List[Statement] = [] # Linearized CFG
|
||||||
blocks: List[Block] = ordered_blocks_list(cfg) # All blocks of the CFG
|
blocks: List[Block] = ordered_blocks_list(cfg) # All blocks of the CFG
|
||||||
for i, block in enumerate(blocks):
|
for i, block in enumerate(blocks):
|
||||||
@ -34,6 +33,7 @@ def linearize(cfg: CFG) -> List[Statement]:
|
|||||||
l.append(ConditionalJump(j.cond, j.op1, j.op2, j.label_then))
|
l.append(ConditionalJump(j.cond, j.op1, j.op2, j.label_then))
|
||||||
l.append(AbsoluteJump(j.label_else))
|
l.append(AbsoluteJump(j.label_else))
|
||||||
case AbsoluteJump() as j:
|
case AbsoluteJump() as j:
|
||||||
|
if i+1 == len(blocks) or j != blocks[i+1].get_label():
|
||||||
l.append(AbsoluteJump(j.label))
|
l.append(AbsoluteJump(j.label))
|
||||||
case Return():
|
case Return():
|
||||||
l.append(AbsoluteJump(cfg.get_end()))
|
l.append(AbsoluteJump(cfg.get_end()))
|
||||||
|
Loading…
Reference in New Issue
Block a user