[pypy-svn] rev 1504 - in pypy/trunk/src/pypy: tool translator translator/test
arigo at codespeak.net
arigo at codespeak.net
Wed Oct 1 17:44:48 CEST 2003
Author: arigo
Date: Wed Oct 1 17:44:47 2003
New Revision: 1504
Added:
pypy/trunk/src/pypy/translator/test/test_annotation.py (contents, props changed)
Modified:
pypy/trunk/src/pypy/tool/udir.py (props changed)
pypy/trunk/src/pypy/translator/annotation.py (contents, props changed)
Log:
All three tests pass for type inference
Modified: pypy/trunk/src/pypy/translator/annotation.py
==============================================================================
--- pypy/trunk/src/pypy/translator/annotation.py (original)
+++ pypy/trunk/src/pypy/translator/annotation.py Wed Oct 1 17:44:47 2003
@@ -7,10 +7,10 @@
if isinstance(w,Constant):
return type(w.value)
for ann in annotations:
- if ann.opname == 'type' and list(ann.args) == [var] and isinstance(ann.result,Constant):
+ if ann.opname == 'type' and list(ann.args) == [w] and isinstance(ann.result,Constant):
return ann.result.value
return None
-
+
def set_type(var,type,annotations):
ann = SpaceOperation("type",[var],Constant(type))
annotations.append(ann)
@@ -22,9 +22,15 @@
def build_annotations(self,input_annotations):
self.annotated = {}
+ self.endblock = BasicBlock([Variable('_return_value')], [], [], None)
self.flowin(self.flowgraph.startblock,input_annotations)
return self.annotated
+ def end_annotations(self):
+ "Returns (return_value_Variable(), annotations_list)."
+ # XXX what if self.endblock not in self.annotated?
+ return self.endblock.input_args[0], self.annotated[self.endblock]
+
def flownext(self,branch,curblock):
getattr(self,'flownext_'+branch.__class__.__name__)(branch,curblock)
@@ -32,15 +38,16 @@
if block not in self.annotated:
self.annotated[block] = annotations[:]
else:
- oldannotations = block.annotations
+ oldannotations = self.annotated[block]
newannotations = self.unify(oldannotations,annotations)
if newannotations == oldannotations:
return
self.annotated[block] = newannotations
for op in block.operations:
- self.consider_op(op,self.annotated[block])
- self.flownext(block.branch,block)
+ self.consider_op(op,self.annotated[block])
+ if block is not self.endblock:
+ self.flownext(block.branch,block)
def consider_op(self,op,annotations):
consider_meth = getattr(self,'consider_op_'+op.opname,None)
@@ -75,7 +82,7 @@
return w
if isinstance(w,GraphGlobalVariable):
return w
- else
+ else:
return renaming[w]
for ann in self.annotated[curblock]:
@@ -90,11 +97,12 @@
self.flowin(branch.target,newannotations)
def flownext_ConditionalBranch(self,branch,curblock):
- self.flownext(branch.ifbranch,block)
- self.flownext(branch.elsebranch,block)
+ self.flownext(branch.ifbranch,curblock)
+ self.flownext(branch.elsebranch,curblock)
def flownext_EndBranch(self,branch,curblock):
- pass # XXX
+ branch = Branch([branch.returnvalue], self.endblock)
+ self.flownext_Branch(branch,curblock)
def unify(self,oldannotations,annotations):
return [ ann for ann in oldannotations if ann in annotations]
Added: pypy/trunk/src/pypy/translator/test/test_annotation.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/translator/test/test_annotation.py Wed Oct 1 17:44:47 2003
@@ -0,0 +1,114 @@
+
+import autopath
+from pypy.tool import test
+from pypy.tool.udir import udir
+
+from pypy.translator.annotation import Annotator, set_type, get_type
+from pypy.translator.flowmodel import *
+
+class TestCase(test.IntTestCase):
+ def test_simple_func(self):
+ """
+ one test source:
+ def f(x):
+ return x+1
+ """
+ x = Variable("x")
+ result = Variable("result")
+ op = SpaceOperation("add", [x, Constant(1)], result)
+ endbranch = EndBranch(result)
+ block = BasicBlock([x], [x],
+ [op],
+ endbranch)
+ fun = FunctionGraph(block, "f")
+ a = Annotator(fun)
+ input_ann = []
+ set_type(fun.get_args()[0], int, input_ann)
+ a.build_annotations(input_ann)
+ end_var, end_ann = a.end_annotations()
+ self.assertEquals(get_type(end_var, end_ann), int)
+
+ def test_while(self):
+ """
+ one test source:
+ def f(i):
+ while i > 0:
+ i = i - 1
+ return i
+ """
+ i = Variable("i")
+ conditionres = Variable("conditionres")
+ conditionop = SpaceOperation("gt", [i, Constant(0)], conditionres)
+ decop = SpaceOperation("add", [i, Constant(-1)], i)
+
+ conditionbranch = ConditionalBranch()
+ headerbranch = Branch()
+ whileblock = BasicBlock([i], [i], [decop], headerbranch)
+ whilebranch = Branch([i], whileblock)
+ endbranch = EndBranch(i)
+ conditionbranch.set(conditionres, whilebranch, endbranch)
+
+ headerblock = BasicBlock([i], [i, conditionres],
+ [conditionop], conditionbranch)
+
+ headerbranch.set([i], headerblock)
+
+ startblock = BasicBlock([i], [i],
+ [], headerbranch)
+
+ fun = FunctionGraph(startblock, "f")
+
+ a = Annotator(fun)
+ input_ann = []
+ set_type(fun.get_args()[0], int, input_ann)
+ a.build_annotations(input_ann)
+ end_var, end_ann = a.end_annotations()
+ self.assertEquals(get_type(end_var, end_ann), int)
+
+ def test_while_sum(self):
+ """
+ one test source:
+ def f(i):
+ sum = 0
+ while i > 0:
+ sum = sum + i
+ i = i - 1
+ return sum
+ """
+ i = Variable("i")
+ sum = Variable("sum")
+
+ conditionres = Variable("conditionres")
+ conditionop = SpaceOperation("gt", [i, Constant(0)], conditionres)
+ decop = SpaceOperation("add", [i, Constant(-1)], i)
+ addop = SpaceOperation("add", [i, sum], sum)
+
+ conditionbranch = ConditionalBranch()
+ headerbranch = Branch()
+ headerbranch2 = Branch()
+ whileblock = BasicBlock([i, sum], [i, sum], [addop, decop], headerbranch2)
+ whilebranch = Branch([i, sum], whileblock)
+
+ endbranch = EndBranch(sum)
+ conditionbranch.set(conditionres, whilebranch, endbranch)
+
+ headerblock = BasicBlock([i, sum], [i, conditionres],
+ [conditionop], conditionbranch)
+
+ headerbranch.set([i, Constant(0)], headerblock)
+ headerbranch2.set([i, sum], headerblock)
+ startblock = BasicBlock([i], [i, sum],
+ [], headerbranch)
+
+ fun = FunctionGraph(startblock, "f")
+
+ a = Annotator(fun)
+ input_ann = []
+ set_type(fun.get_args()[0], int, input_ann)
+ import sys; print >> sys.stderr, a.build_annotations(input_ann)
+ end_var, end_ann = a.end_annotations()
+ self.assertEquals(get_type(end_var, end_ann), int)
+
+
+if __name__ == '__main__':
+ test.main()
More information about the Pypy-commit
mailing list