|
@@ -46,7 +46,10 @@ class BasicBlock(object):
|
|
|
return self.definitions.remove(definition)
|
|
|
|
|
|
def __str__(self):
|
|
|
- return '\n'.join(map(str, self.definitions + [self.flow]))
|
|
|
+ prefix = '!%d(%s):' % (self.index, ', '.join(map(str, self.parameters)))
|
|
|
+ return '\n'.join(
|
|
|
+ [prefix] +
|
|
|
+ [' ' * 4 + str(item) for item in self.definitions + [self.flow]])
|
|
|
|
|
|
class Definition(object):
|
|
|
"""Maps a value to a variable."""
|
|
@@ -58,6 +61,10 @@ class Definition(object):
|
|
|
"""Tweaks this definition to take on the given new value."""
|
|
|
self.value = new_value
|
|
|
|
|
|
+ def ref_str(self):
|
|
|
+ """Gets a string that represents a reference to this definition."""
|
|
|
+ return '$%d' % self.index
|
|
|
+
|
|
|
def __str__(self):
|
|
|
return '$%d = %s' % (self.index, str(self.value))
|
|
|
|
|
@@ -86,6 +93,9 @@ class Branch(Instruction):
|
|
|
"""Gets all definitions and instructions on which this instruction depends."""
|
|
|
return self.arguments
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return '!%d(%s)' % (self.block.index, ', '.join(map(str, self.arguments)))
|
|
|
+
|
|
|
class FlowInstruction(Instruction):
|
|
|
"""Represents a control flow instruction which terminates a basic block."""
|
|
|
def branches(self):
|
|
@@ -106,6 +116,9 @@ class JumpFlow(FlowInstruction):
|
|
|
"""Gets a list of basic blocks targeted by this flow instruction."""
|
|
|
return [self.branch]
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return 'jump %s' % self.branch
|
|
|
+
|
|
|
class SelectFlow(FlowInstruction):
|
|
|
"""Represents a control flow instruction which jumps to one of two basic blocks depending
|
|
|
on whether a condition is truthy or not."""
|
|
@@ -123,6 +136,9 @@ class SelectFlow(FlowInstruction):
|
|
|
"""Gets a list of basic blocks targeted by this flow instruction."""
|
|
|
return [self.if_branch, self.else_branch]
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return 'select %s, %s, %s' % (self.condition.ref_str(), self.if_branch, self.else_branch)
|
|
|
+
|
|
|
class ReturnFlow(FlowInstruction):
|
|
|
"""Represents a control flow instruction which terminates the execution of the current
|
|
|
function and returns a value."""
|
|
@@ -138,6 +154,9 @@ class ReturnFlow(FlowInstruction):
|
|
|
"""Gets a list of basic blocks targeted by this flow instruction."""
|
|
|
return []
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return 'return %s' % self.value.ref_str()
|
|
|
+
|
|
|
class UnreachableFlow(FlowInstruction):
|
|
|
"""Represents a control flow instruction which is unreachable."""
|
|
|
def get_dependencies(self):
|
|
@@ -148,6 +167,9 @@ class UnreachableFlow(FlowInstruction):
|
|
|
"""Gets a list of basic blocks targeted by this flow instruction."""
|
|
|
return []
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return 'unreachable'
|
|
|
+
|
|
|
class Value(Instruction):
|
|
|
"""A value: an instruction that produces some result."""
|
|
|
def get_dependencies(self):
|
|
@@ -160,6 +182,9 @@ class BlockParameter(Value):
|
|
|
"""Gets all definitions and instructions on which this instruction depends."""
|
|
|
return []
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return 'block-parameter'
|
|
|
+
|
|
|
class FunctionParameter(Value):
|
|
|
"""A function parameter."""
|
|
|
def __init__(self, name):
|
|
@@ -170,6 +195,9 @@ class FunctionParameter(Value):
|
|
|
"""Gets all definitions and instructions on which this instruction depends."""
|
|
|
return []
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return 'func-parameter %s' % self.name
|
|
|
+
|
|
|
class Literal(Value):
|
|
|
"""A literal value."""
|
|
|
def __init__(self, literal):
|
|
@@ -179,3 +207,6 @@ class Literal(Value):
|
|
|
def get_dependencies(self):
|
|
|
"""Gets all definitions and instructions on which this instruction depends."""
|
|
|
return []
|
|
|
+
|
|
|
+ def __str__(self):
|
|
|
+ return 'literal %s' % self.literal
|