[pypy-svn] rev 1449 - in pypy/trunk/src/pypy/translator: . test
tomek at codespeak.net
tomek at codespeak.net
Mon Sep 29 18:33:26 CEST 2003
Author: tomek
Date: Mon Sep 29 18:33:26 2003
New Revision: 1449
Added:
pypy/trunk/src/pypy/translator/__init__.py
pypy/trunk/src/pypy/translator/controlflow.py
pypy/trunk/src/pypy/translator/genpyrex.py
pypy/trunk/src/pypy/translator/test/__init__.py
Modified:
pypy/trunk/src/pypy/translator/test/test_sourcegen.py
Log:
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.
Added: pypy/trunk/src/pypy/translator/__init__.py
==============================================================================
Added: pypy/trunk/src/pypy/translator/controlflow.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/translator/controlflow.py Mon Sep 29 18:33:26 2003
@@ -0,0 +1,46 @@
+
+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
+
Added: pypy/trunk/src/pypy/translator/genpyrex.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/translator/genpyrex.py Mon Sep 29 18:33:26 2003
@@ -0,0 +1,92 @@
+
+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
Added: pypy/trunk/src/pypy/translator/test/__init__.py
==============================================================================
Modified: pypy/trunk/src/pypy/translator/test/test_sourcegen.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_sourcegen.py (original)
+++ pypy/trunk/src/pypy/translator/test/test_sourcegen.py Mon Sep 29 18:33:26 2003
@@ -2,49 +2,9 @@
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):
@@ -91,7 +51,7 @@
[conditionop],
conditionalbranch)
fun = FunctionGraph(startblock, "f")
- result = genpyrex(fun)
+ result = GenPyrex(fun).emitcode()
self.assertEquals(result, """
def f(i, j):
conditionres = i < 0
More information about the Pypy-commit
mailing list