Commits

Anonymous committed 78da7dd

I refactored the files, and put temporary classes to controlflow.py We won't need these classes anymore, when the
controlObjectSpace is ready. We need them for testing.

Comments (0)

Files changed (5)

pypy/translator/__init__.py

Empty file added.

pypy/translator/controlflow.py

+
+import autopath
+
+
+class BasicBlock:
+    def __init__(self, input_args, locals, operations, branch):
+        self.input_args = input_args
+        self.locals = locals
+        self.operations = operations
+        self.branch = branch
+
+class Variable:
+    def __init__(self, pseudoname):
+        self.pseudoname = pseudoname
+
+class Constant:
+    def __init__(self, value):
+        self.value = value
+
+class SpaceOperation:
+    def __init__(self, opname, args, result, branch):
+        self.opname = opname
+        self.args = args # list of variables
+        self.result = result # <Variable/Constant instance>
+        self.branch = branch # branch
+
+class Branch:
+    def __init__(self, args, target):
+        self.args = args     # list of variables
+        self.target = target # basic block instance
+
+class ConditionalBranch:
+    def __init__(self, condition, ifbranch, elsebranch):
+        self.condition = condition
+        self.ifbranch = ifbranch
+        self.elsebranch = elsebranch
+
+class EndBranch:
+    def __init__(self, returnvalue):
+        self.returnvalue = returnvalue
+
+class FunctionGraph:
+    def __init__(self, startblock, functionname):
+        self.startblock = startblock
+        self.functionname = functionname
+

pypy/translator/genpyrex.py

+
+import autopath
+from pypy.tool import test
+from pypy.interpreter.baseobjspace import ObjSpace
+from pypy.translater.controlflow import *
+
+class GenPyrex:
+    def __init__(self, functiongraph):
+        self.functiongraph = functiongraph
+        ops = {}
+        oparity = {}
+        for (opname, opsymbol, arity, _) in ObjSpace.MethodTable:
+            ops[opname] = opsymbol
+            oparity[opname] = arity
+        self.ops = ops  
+        self.oparity = oparity
+
+    def emitcode(self):
+        self.blockids = {}
+        self.lines = []
+        self.indent = 0
+        self.createCodeFromGraph()
+        return "\n".join(self.lines)
+
+    def putline(self, line):
+        self.lines.append("  " * self.indent + line)
+
+    def createCodeFromGraph(self):
+        fun = self.functiongraph
+        inputargnames = [ var.pseudoname for var in fun.startblock.input_args ]
+        params = ", ".join(inputargnames)
+        self.putline("def %s(%s):" % (fun.functionname, params))
+        self.indent += 1 
+        self.createCodeFromBasicBlock(fun.startblock)
+        self.indent -= 1
+
+    def _str(self, obj):
+        if isinstance(obj, Variable):
+            return obj.pseudoname
+        else:
+            return repr(obj.value)
+
+    def createCodeFromBasicBlock(self, block):
+        if self.blockids.has_key(block):
+            self.putline('cinline "goto Label%s;"' % self.blockids[block])
+            return 
+
+        blockids = self.blockids
+        blockids.setdefault(block, len(blockids))
+        
+        self.putline('cinline "Label%s:"' % blockids[block])
+        for op in block.operations:
+            opsymbol = self.ops[op.opname] 
+            arity = self.oparity[op.opname]
+            assert(arity == len(op.args))
+            argsnames = [self._str(arg) for arg in args]
+            if arity == 1 or arity == 3 or "a" <= opsymbol[0] <= "z":
+                
+                self.putline("%s = %s(%s)" % (result.pseudoname, opsymbol, ", ".join([argnames]))
+            else:
+                self.putline("%s = %s %s %s") % (result.pseudoname, argnames[0], opsymbol, argnames[1]))
+
+        self.dispatchBranch(block.branch)
+
+    def dispatchBranch(self, branch):
+        method = getattr(self, "createCodeFrom" + branch.__class__.__name__)
+        method(branch)
+
+    def createCodeFromBranch(self, branch):
+        _str = self._str
+        block = branch.target
+        sourceargs = [_str(arg) for arg in branch.args]       
+        targetargs = [arg.pseudoname for arg in block.input_args]
+        assert(len(sourceargs) == len(targetargs))
+        if sourceargs: 
+            self.putline("%s = %s" % (", ".join(targetargs), ", ".join(sourceargs))
+
+        self.createCodeFromBasicBlock(block)    
+
+    def createCodeFromEndBranch(self, branch):
+        self.putline("return %s" % self._str(branch.returnvalue))
+   
+ 
+    def createCodeFromConditionalBranch(self, branch):
+        self.putline("if %s:" % self._str(branch.condition))
+        self.indent += 1
+        self.dispatchBranch(ifbranch)
+        self.indent -= 1
+        self.putline("else:")
+        self.indent += 1
+        self.dispatchBranch(elsebranch)
+        self.indent -= 1

pypy/translator/test/__init__.py

Empty file added.

pypy/translator/test/test_sourcegen.py

 import autopath
 from pypy.tool import test
 
-from pypy.translator.genpyrex import genpyrex
+from pypy.translator.genpyrex import GenPyrex
+from pypy.translator.controlflow import *
 
-class BasicBlock:
-    def __init__(self, input_args, locals, operations, branch):
-        self.input_args = input_args
-        self.locals = locals
-        self.operations = operations
-        self.branch = branch
-
-class Variable:
-    def __init__(self, pseudoname):
-        self.pseudoname = pseudoname
-
-class Constant:
-    def __init__(self, value):
-        self.value = value
-
-class SpaceOperation:
-    def __init__(self, opname, args, result, branch):
-        self.opname = opname
-        self.args = args # list of variables
-        self.result = result # <Variable/Constant instance>
-        self.branch = branch # branch
-
-class Branch:
-    def __init__(self, args, target):
-        self.args = args     # list of variables
-        self.target = target # basic block instance
-
-class ConditionalBranch:
-    def __init__(self, condition, ifbranch, elsebranch):
-        self.condition = condition
-        self.ifbranch = ifbranch
-        self.elsebranch = elsebranch
-
-class EndBranch:
-    def __init__(self, returnvalue):
-        self.returnvalue = returnvalue
-
-class FunctionGraph:
-    def __init__(self, startblock, functionname):
-        self.startblock = startblock
-        self.functionname = functionname
 
 class TestCase(test.IntTestCase):
     def test_simple_func(self):
                            [conditionop],
                            conditionalbranch)
         fun = FunctionGraph(startblock, "f")
-        result = genpyrex(fun)
+        result = GenPyrex(fun).emitcode()
         self.assertEquals(result, """
 def f(i, j):
     conditionres = i < 0