| 1 |
"""PyPy Translator Frontend |
| 2 |
|
| 3 |
The Translator is a glue class putting together the various pieces of the |
| 4 |
translation-related code. It can be used for interactive testing of the |
| 5 |
translator; see pypy/bin/translatorshell.py. |
| 6 |
""" |
| 7 |
from __future__ import print_function |
| 8 |
|
| 9 |
import sys |
| 10 |
import types |
| 11 |
|
| 12 |
from rpython.translator import simplify |
| 13 |
from rpython.flowspace.model import FunctionGraph, checkgraph, Block |
| 14 |
from rpython.flowspace.objspace import build_flow |
| 15 |
from rpython.tool.ansi_print import AnsiLogger |
| 16 |
from rpython.tool.sourcetools import nice_repr_for_func |
| 17 |
from rpython.config.translationoption import get_platform |
| 18 |
|
| 19 |
log = AnsiLogger("flowgraph") |
| 20 |
|
| 21 |
class TranslationContext(object): |
| 22 |
FLOWING_FLAGS = { |
| 23 |
'verbose': False, |
| 24 |
'list_comprehension_operations': False, |
| 25 |
} |
| 26 |
|
| 27 |
def __init__(self, config=None, **flowing_flags): |
| 28 |
if config is None: |
| 29 |
from rpython.config.translationoption import get_combined_translation_config |
| 30 |
config = get_combined_translation_config(translating=True) |
| 31 |
|
| 32 |
for attr in ['verbose', 'list_comprehension_operations']: |
| 33 |
if attr in flowing_flags: |
| 34 |
setattr(config.translation, attr, flowing_flags[attr]) |
| 35 |
self.config = config |
| 36 |
self.platform = get_platform(config) |
| 37 |
self.annotator = None |
| 38 |
self.rtyper = None |
| 39 |
self.exceptiontransformer = None |
| 40 |
self.graphs = [] |
| 41 |
self.callgraph = {} |
| 42 |
self._prebuilt_graphs = {} |
| 43 |
self._call_at_startup = [] |
| 44 |
|
| 45 |
def buildflowgraph(self, func, mute_dot=False): |
| 46 |
"""Get the flow graph for a function.""" |
| 47 |
if not isinstance(func, types.FunctionType): |
| 48 |
raise TypeError("buildflowgraph() expects a function, " |
| 49 |
"got %r" % (func,)) |
| 50 |
if func in self._prebuilt_graphs: |
| 51 |
graph = self._prebuilt_graphs.pop(func) |
| 52 |
else: |
| 53 |
if self.config.translation.verbose: |
| 54 |
log(nice_repr_for_func(func)) |
| 55 |
graph = build_flow(func) |
| 56 |
simplify.simplify_graph(graph) |
| 57 |
if self.config.translation.list_comprehension_operations: |
| 58 |
simplify.detect_list_comprehension(graph) |
| 59 |
if not self.config.translation.verbose and not mute_dot: |
| 60 |
log.dot() |
| 61 |
self.graphs.append(graph) |
| 62 |
return graph |
| 63 |
|
| 64 |
def update_call_graph(self, caller_graph, callee_graph, position_tag): |
| 65 |
|
| 66 |
key = caller_graph, callee_graph, position_tag |
| 67 |
self.callgraph[key] = caller_graph, callee_graph |
| 68 |
|
| 69 |
def buildannotator(self, policy=None): |
| 70 |
if self.annotator is not None: |
| 71 |
raise ValueError("we already have an annotator") |
| 72 |
from rpython.annotator.annrpython import RPythonAnnotator |
| 73 |
self.annotator = RPythonAnnotator( |
| 74 |
self, policy=policy, keepgoing=self.config.translation.keepgoing) |
| 75 |
return self.annotator |
| 76 |
|
| 77 |
def buildrtyper(self): |
| 78 |
if self.annotator is None: |
| 79 |
raise ValueError("no annotator") |
| 80 |
if self.rtyper is not None: |
| 81 |
raise ValueError("we already have an rtyper") |
| 82 |
from rpython.rtyper.rtyper import RPythonTyper |
| 83 |
self.rtyper = RPythonTyper(self.annotator) |
| 84 |
return self.rtyper |
| 85 |
|
| 86 |
def getexceptiontransformer(self): |
| 87 |
if self.rtyper is None: |
| 88 |
raise ValueError("no rtyper") |
| 89 |
if self.exceptiontransformer is not None: |
| 90 |
return self.exceptiontransformer |
| 91 |
from rpython.translator.exceptiontransform import ExceptionTransformer |
| 92 |
self.exceptiontransformer = ExceptionTransformer(self) |
| 93 |
return self.exceptiontransformer |
| 94 |
|
| 95 |
def checkgraphs(self): |
| 96 |
for graph in self.graphs: |
| 97 |
checkgraph(graph) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
def about(self, x, f=None): |
| 102 |
"""Interactive debugging helper """ |
| 103 |
if f is None: |
| 104 |
f = sys.stdout |
| 105 |
if isinstance(x, Block): |
| 106 |
for graph in self.graphs: |
| 107 |
if x in graph.iterblocks(): |
| 108 |
print('%s is a %s' % (x, x.__class__), file=f) |
| 109 |
print('in %s' % (graph,), file=f) |
| 110 |
break |
| 111 |
else: |
| 112 |
print('%s is a %s at some unknown location' % ( |
| 113 |
x, x.__class__.__name__), file=f) |
| 114 |
print('containing the following operations:', file=f) |
| 115 |
for op in x.operations: |
| 116 |
print(" ",op, file=f) |
| 117 |
print('--end--', file=f) |
| 118 |
return |
| 119 |
raise TypeError("don't know about %r" % x) |
| 120 |
|
| 121 |
|
| 122 |
def view(self): |
| 123 |
"""Shows the control flow graph with annotations if computed. |
| 124 |
Requires 'dot' and pygame.""" |
| 125 |
from rpython.translator.tool.graphpage import FlowGraphPage |
| 126 |
FlowGraphPage(self).display() |
| 127 |
|
| 128 |
show = view |
| 129 |
|
| 130 |
def viewcg(self, center_graph=None, huge=100): |
| 131 |
"""Shows the whole call graph and the class hierarchy, based on |
| 132 |
the computed annotations.""" |
| 133 |
from rpython.translator.tool.graphpage import TranslatorPage |
| 134 |
TranslatorPage(self, center_graph=center_graph, huge=huge).display() |
| 135 |
|
| 136 |
showcg = viewcg |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
def graphof(translator, func): |
| 143 |
if isinstance(func, FunctionGraph): |
| 144 |
return func |
| 145 |
result = [] |
| 146 |
if hasattr(func, 'im_func'): |
| 147 |
|
| 148 |
func = func.im_func |
| 149 |
for graph in translator.graphs: |
| 150 |
if getattr(graph, 'func', None) is func: |
| 151 |
result.append(graph) |
| 152 |
assert len(result) == 1 |
| 153 |
return result[0] |
| 154 |
|
| 155 |
TranslationContext._graphof = graphof |