Ejemplo n.º 1
0
    def generic_visit(self, node):
        if isinstance(node, ast.expr) and node in self.constant_expressions:
            fake_node = ast.Expression(node)
            code = compile(ast.gast_to_ast(fake_node),
                           '<constant folding>', 'eval')
            try:
                value = eval(code, self.env)
                new_node = to_ast(value)
                if not ASTMatcher(node).match(new_node):
                    self.update = True
                    return new_node
            except DamnTooLongPattern as e:
                print("W: ", e, " Assume no update happened.")
            except ConversionError as e:
                print('error in constant folding: ', e)
                raise
            except ToNotEval:
                pass
            except AttributeError as e:
                # this may miss a few optimization
                logger.info('During constant folding, bailing out due to: ' +
                            e.args[0])
            except NameError as e:
                # FIXME dispatched function are not processed by constant
                # folding
                if "__dispatch__" in e.args[0]:
                    return Transformation.generic_visit(self, node)
                # this may miss a few optimization
                logger.info('During constant folding, bailing out due to: ' +
                            e.args[0])
            except Exception as e:
                raise PythranSyntaxError(str(e), node)

        return Transformation.generic_visit(self, node)
Ejemplo n.º 2
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, "<constant folding>", "eval")
             value = eval(code, self.env)
             new_node = self.to_ast(value)
             if isinstance(node, ast.Index) and not isinstance(new_node, ast.Index):
                 new_node = ast.Index(new_node)
             return new_node
         except ConstantFolding.ConversionError as e:
             print ast.dump(node)
             print "error in constant folding: ", e
             raise
         except ConstantFolding.ToNotEval:
             return Transformation.generic_visit(self, node)
         except AttributeError as e:
             # FIXME union_ function is not handle by constant folding
             if "union_" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
         except NameError as e:
             # FIXME dispatched function are not processed by constant
             # folding
             if "__dispatch__" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
     else:
         return Transformation.generic_visit(self, node)
Ejemplo n.º 3
0
 def __init__(self, pm, name, ctx, lambdas, imports, global_decls):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambdas
     self.imports = imports
     self.global_declarations = global_decls
Ejemplo n.º 4
0
 def __init__(self, pm, name, ctx, lambda_functions, imports):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambda_functions
     self.imports = imports
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
Ejemplo n.º 5
0
 def __init__(self, pm, name, ctx, lambda_functions, imports):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambda_functions
     self.imports = imports
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
Ejemplo n.º 6
0
 def __init__(self, pm, name, ctx, lambdas, imports, global_decls):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambdas
     self.imports = imports
     self.global_declarations = global_decls
Ejemplo n.º 7
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(
                 node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, '<constant folding>', 'eval')
             value = eval(code, self.env)
             new_node = self.to_ast(value)
             if (isinstance(node, ast.Index)
                     and not isinstance(new_node, ast.Index)):
                 new_node = ast.Index(new_node)
             return new_node
         except Exception:  # as e:
             #print ast.dump(node)
             #print 'error in constant folding: ', e
             return Transformation.generic_visit(self, node)
     else:
         return Transformation.generic_visit(self, node)
Ejemplo n.º 8
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(
                 node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, '<constant folding>', 'eval')
             value = eval(code, self.env)
             new_node = to_ast(value)
             if(isinstance(node, ast.Index) and
                not isinstance(new_node, ast.Index)):
                 new_node = ast.Index(new_node)
             try:
                 if not ASTMatcher(node).search(new_node):
                     self.update = True
             except DamnTooLongPattern as e:
                 print "W: ", e, " Assume no update happened."
             return new_node
         except ConversionError as e:
             print ast.dump(node)
             print 'error in constant folding: ', e
             raise
         except ToNotEval:
             return Transformation.generic_visit(self, node)
         except AttributeError as e:
             # FIXME union_ function is not handle by constant folding
             if "union_" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             elif "pythran" in e.args[0]:
                 # FIXME: Can be fix giving a Python implementation for
                 # these functions.
                 return Transformation.generic_visit(self, node)
             raise
         except NameError as e:
             # FIXME dispatched function are not processed by constant
             # folding
             if "__dispatch__" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
     else:
         return Transformation.generic_visit(self, node)
Ejemplo n.º 9
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(
                 node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, '<constant folding>', 'eval')
             value = eval(code, self.env)
             new_node = to_ast(value)
             if (isinstance(node, ast.Index)
                     and not isinstance(new_node, ast.Index)):
                 new_node = ast.Index(new_node)
             try:
                 if not ASTMatcher(node).search(new_node):
                     self.update = True
             except DamnTooLongPattern as e:
                 print "W: ", e, " Assume no update happened."
             return new_node
         except ConversionError as e:
             print ast.dump(node)
             print 'error in constant folding: ', e
             raise
         except ToNotEval:
             return Transformation.generic_visit(self, node)
         except AttributeError as e:
             # FIXME union_ function is not handle by constant folding
             if "union_" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             elif "pythran" in e.args[0]:
                 # FIXME: Can be fix giving a Python implementation for
                 # these functions.
                 return Transformation.generic_visit(self, node)
             raise
         except NameError as e:
             # FIXME dispatched function are not processed by constant
             # folding
             if "__dispatch__" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
     else:
         return Transformation.generic_visit(self, node)
Ejemplo n.º 10
0
 def __init__(self):
     self.count = 0
     Transformation.__init__(self)
Ejemplo n.º 11
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples,
                             OptimizableComprehension)
 def __init__(self):
     Transformation.__init__(self, OptimizableComprehension)
Ejemplo n.º 13
0
 def __init__(self):
     """ Basic initialiser. """
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Ejemplo n.º 14
0
 def __init__(self):
     Transformation.__init__(self)
Ejemplo n.º 15
0
 def __init__(self):
     Transformation.__init__(self, Identifiers)
Ejemplo n.º 16
0
 def __init__(self):
     Transformation.__init__(self, RangeValues)
Ejemplo n.º 17
0
 def __init__(self):
     """ Gather required information. """
     Transformation.__init__(self, PotentialIterator, Aliases)
Ejemplo n.º 18
0
 def __init__(self):
     """ Basic initialiser. """
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Ejemplo n.º 19
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples, PotentialIterator)
Ejemplo n.º 20
0
 def __init__(self):
     Transformation.__init__(self, PotentialIterator, Aliases)
Ejemplo n.º 21
0
 def __init__(self):
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Ejemplo n.º 22
0
 def __init__(self):
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Ejemplo n.º 23
0
 def __init__(self, pm, ctx, global_declarations):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = global_declarations
     self.identifiers = set(self.global_declarations.keys())
Ejemplo n.º 24
0
 def __init__(self):
     Transformation.__init__(self)
     # Remap self.visit_XXXX() to self.attach_data() generic method
     for s in GatherOMPData.statements:
         setattr(self, "visit_" + s, self.attach_data)
     self.current = list()
Ejemplo n.º 25
0
 def __init__(self, pm, ctx, global_declarations):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = global_declarations
Ejemplo n.º 26
0
 def __init__(self):
     self.renamings = dict()
     Transformation.__init__(self, Identifiers)
Ejemplo n.º 27
0
 def __init__(self, pm, ctx):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
Ejemplo n.º 28
0
 def __init__(self):
     Transformation.__init__(self)
     # Remap self.visit_XXXX() to self.attach_data() generic method
     for s in GatherOMPData.statements:
         setattr(self, "visit_" + s, self.attach_data)
     self.current = list()
Ejemplo n.º 29
0
 def __init__(self, pm, ctx, global_declarations):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = global_declarations
Ejemplo n.º 30
0
 def __init__(self):
     Transformation.__init__(self, OptimizableComprehension)
Ejemplo n.º 31
0
 def __init__(self):
     Transformation.__init__(self, UseDefChains, Ancestors, Aliases,
                             RangeValues, Identifiers)
     self.loops_mod = dict()
Ejemplo n.º 32
0
 def visit_Call(self, node):
     if isinstance(node.func, ast.Attribute):
         return self.generic_visit(node)
     else:
         return Transformation.generic_visit(self, node)
Ejemplo n.º 33
0
 def __init__(self):
     Transformation.__init__(self, UseDefChain, Ancestors, Aliases,
                             RangeValues, Identifiers)
     self.loops_mod = dict()
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = {'__builtin__': '__builtin__'}
     self.to_import = set()
Ejemplo n.º 35
0
 def __init__(self):
     Transformation.__init__(self, Globals, Ancestors)
     self.imports = {'builtins': 'builtins'}
     self.to_import = set()
Ejemplo n.º 36
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples,
                             OptimizableComprehension)
Ejemplo n.º 37
0
 def __init__(self):
     Transformation.__init__(self, TypeDependencies,
                             OrderedGlobalDeclarations)
Ejemplo n.º 38
0
 def __init__(self):
     Transformation.__init__(self, Locals, Globals)
Ejemplo n.º 39
0
 def __init__(self):
     Transformation.__init__(self, Identifiers)
Ejemplo n.º 40
0
 def __init__(self):
     Transformation.__init__(self)
Ejemplo n.º 41
0
 def __init__(self):
     Transformation.__init__(self, PotentialIterator)
Ejemplo n.º 42
0
 def __init__(self):
     """ Gather required information. """
     Transformation.__init__(self, PotentialIterator, Aliases)
Ejemplo n.º 43
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples,
                             PotentialIterator)
Ejemplo n.º 44
0
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = {'__builtin__': '__builtin__'}
     self.to_import = set()
Ejemplo n.º 45
0
 def __init__(self):
     Transformation.__init__(self, Aliases, ConstantExpressions,
                             PureExpressions)
Ejemplo n.º 46
0
 def __init__(self):
     Transformation.__init__(self, ConstantExpressions)
Ejemplo n.º 47
0
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = set()
     self.to_import = set()
Ejemplo n.º 48
0
 def __init__(self, pm, ctx):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
Ejemplo n.º 49
0
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = set()
     self.to_import = set()
Ejemplo n.º 50
0
 def __init__(self):
     self.renamings = dict()
     Transformation.__init__(self, Identifiers)
Ejemplo n.º 51
0
 def __init__(self):
     """Gather required information."""
     Transformation.__init__(self, PotentialIterator, Aliases)
     self.use_itertools = False
Ejemplo n.º 52
0
 def __init__(self):
     Transformation.__init__(self, ConstantExpressions)
 def __init__(self):
     Transformation.__init__(self, RangeValues)
Ejemplo n.º 54
0
 def __init__(self):
     self.count = 0
     Transformation.__init__(self)
Ejemplo n.º 55
0
 def __init__(self):
     Transformation.__init__(self, Aliases, PureExpressions)