This commit is contained in:
augustin64 2024-10-21 13:45:24 +02:00
parent 7479ee19d8
commit 0651175da6
2 changed files with 17 additions and 5 deletions

View File

@ -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:

View File

@ -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