def __init__(self): super(SSARewrite, self).__init__() self.functions = [] self.assignments = Counter() self.block = blocks.BasicBlock() self.start_block = self.block self.functions.append( ('__module', btypes.void_type, [], self.start_block))
def visit_WhileStatement(self, node): whileblock = blocks.WhileBlock() self.block.next_block = whileblock self.block = whileblock # Evaluate the condition self.visit(node.condition) # Save the variable where the test value is stored whileblock.testvar = node.condition.ssa_name # Traverse the body whileblock.body = blocks.BasicBlock() self.block = whileblock.body self.visit(node.statements) # Create the terminating block self.block = blocks.BasicBlock() whileblock.next_block = self.block
def visit_IfElseStatement(self, node): ifblock = blocks.IfBlock() self.block.next_block = ifblock self.block = ifblock self.visit(node.condition) ifblock.testvar = node.condition.ssa_name ifblock.true_branch = blocks.BasicBlock() self.block = ifblock.true_branch self.visit(node.if_statements) if node.else_statements: ifblock.false_branch = blocks.BasicBlock() self.block = ifblock.false_branch self.visit(node.else_statements) self.block = blocks.BasicBlock() ifblock.next_block = self.block
def visit_ForStatement(self, node): forblock = blocks.ForBlock() self.block.next_block = forblock self.block = forblock self.visit(node.iter) # store the temporary variable locations for the range # parameter. forblock.start_var = node.iter.start.ssa_name forblock.stop_var = node.iter.stop.ssa_name forblock.var = node.var # Generate blocks for the body forblock.body = blocks.BasicBlock() self.block = forblock.body self.visit(node.body) # End the loop self.block = blocks.BasicBlock() forblock.next_block = self.block
def visit_FunctionDef(self, node): last_block = self.block # pop self.block = blocks.BasicBlock() retty = node.sig.typename.type argtys = [arg.type for arg in node.sig.parameters] fn = (node.sig.name, retty, argtys, self.block) self.functions.append(fn) for n, parm in enumerate(node.sig.parameters): inst = ('LOAD_ARGUMENT', parm.type, parm.name, n) self.block.append(inst) self.visit(node.statements) if node.sig.type == btypes.void_type: self.block.append(('RETURN', btypes.void_type)) self.block = last_block # push