Example #1
0
    def flatten (self, node):
        """Takes an AST as input, and then "flattens" the tree into a list of statements."""

        if isinstance(node, While):
            #statement
            flatbody = self.flatten(node.body)
            #expression
            # the first element in the tuple is an expression, so it needs flattened.
            var0, flattest0 = self.flatten(node.test[0]) 
            # the second selement is a stmt node.
            stmt = self.flatten(node.test[1])
            # the statements associated with the newly flattened expression (node.test[0]),
            # have to be run AFTER the statements in node.test[1]
            stmt.nodes = stmt.nodes + flattest0
            return [While((var0,stmt), flatbody, [], node.lineno)]   
        elif isinstance(node, If):
            # flatten the "test" expression
            vartes, test = self.flatten(node.tests[0][0])
            # flatten the "then" and "else" statements
            then = self.flatten(node.tests[0][1])
            else_ = self.flatten(node.else_)
            
            # NOTE: The If node has two attributes: "tests" and "else_".  The tests attribute is
            # a list of tuples, where the first element in the tuple is the test expression and the
            # second element in the tuple is a Stmt object.  Each tuple in the list corresponds to
            # an "if" or "elif" clause.  The else_ attribute is a Stmt object corresponding to the 
            # "else" clause.
            self.log.debug('then=%s' % then)
            self.log.debug('else_=%s' % then)
            return test + [If([(vartes, then)], else_)]

        else:
            return P2Flattener.flatten(self, node)
Example #2
0
    def flatten(self, node):
        """Takes an AST as input, and then "flattens" the tree into a list of statements."""

        if isinstance(node, While):
            #statement
            flatbody = self.flatten(node.body)
            #expression
            # the first element in the tuple is an expression, so it needs flattened.
            var0, flattest0 = self.flatten(node.test[0])
            # the second selement is a stmt node.
            stmt = self.flatten(node.test[1])
            # the statements associated with the newly flattened expression (node.test[0]),
            # have to be run AFTER the statements in node.test[1]
            stmt.nodes = stmt.nodes + flattest0
            return [While((var0, stmt), flatbody, [], node.lineno)]
        elif isinstance(node, If):
            # flatten the "test" expression
            vartes, test = self.flatten(node.tests[0][0])
            # flatten the "then" and "else" statements
            then = self.flatten(node.tests[0][1])
            else_ = self.flatten(node.else_)

            # NOTE: The If node has two attributes: "tests" and "else_".  The tests attribute is
            # a list of tuples, where the first element in the tuple is the test expression and the
            # second element in the tuple is a Stmt object.  Each tuple in the list corresponds to
            # an "if" or "elif" clause.  The else_ attribute is a Stmt object corresponding to the
            # "else" clause.
            self.log.debug('then=%s' % then)
            self.log.debug('else_=%s' % then)
            return test + [If([(vartes, then)], else_)]

        else:
            return P2Flattener.flatten(self, node)
Example #3
0
    from p2heapify import P2Heapify
    from p2closureconvert import P2ClosureConversion
    from p2flattener import P2Flattener
    from p2insselector import P2InstructionSelector
    from p2regallocator import P2RegAllocator
    from p2ifinsselector import P2IfInstructionSelector
    if len(sys.argv) < 2:
        sys.exit(1)
    testcases = sys.argv[1:]
    for testcase in testcases:
        varalloc = VariableAllocator()
        p2unique = P2UniquifyVars()
        p2explicator = P2Explicate(varalloc)
        p2heap = P2Heapify(p2explicator)
        p2closure = P2ClosureConversion(p2explicator, varalloc)
        p2flatten = P2Flattener(varalloc)
        p2insselector = P2InstructionSelector(varalloc)
        ifinsselector = P2IfInstructionSelector(varalloc,
                                                p2insselector.labelalloc)

        ast = compiler.parseFile(testcase)
        unique = p2unique.transform(ast)
        explicated = p2explicator.explicate(unique)
        heaped = p2heap.transform(explicated)
        astlist = p2closure.transform(heaped)
        for ast in astlist:
            ast = p2flatten.flatten(ast)
            program = p2insselector.transform(ast)
            p2regallocator = P2RegAllocator(program, varalloc)
            program = p2regallocator.substitute()
            program = ifinsselector.visit(program)
Example #4
0
 def __init__ (self, varalloc, validate=False):
     P2Flattener.__init__(self, varalloc)
     self.validate = validate
Example #5
0
 def __init__(self, varalloc, validate=False):
     P2Flattener.__init__(self, varalloc)
     self.validate = validate