compiler.ir: print basic blocks in reverse postorder for readability.

This commit is contained in:
whitequark 2015-11-23 21:44:38 +08:00
parent c73b2c1a78
commit d92b3434a0
1 changed files with 12 additions and 1 deletions

View File

@ -463,12 +463,23 @@ class Function:
yield from iter(basic_block.instructions) yield from iter(basic_block.instructions)
def as_entity(self, type_printer): def as_entity(self, type_printer):
postorder = []
visited = set()
def visit(block):
visited.add(block)
for next_block in block.successors():
if next_block not in visited:
visit(next_block)
postorder.append(block)
visit(self.entry())
lines = [] lines = []
lines.append("{} {}({}) {{ ; type: {}".format( lines.append("{} {}({}) {{ ; type: {}".format(
type_printer.name(self.type.ret), self.name, type_printer.name(self.type.ret), self.name,
", ".join([arg.as_operand(type_printer) for arg in self.arguments]), ", ".join([arg.as_operand(type_printer) for arg in self.arguments]),
type_printer.name(self.type))) type_printer.name(self.type)))
for block in self.basic_blocks: for block in reversed(postorder):
lines.append(block.as_entity(type_printer)) lines.append(block.as_entity(type_printer))
lines.append("}") lines.append("}")
return "\n".join(lines) return "\n".join(lines)