[pypy-svn] r18467 - in pypy/branch/hl-backend/pypy/rpython: . ootype/test
boria at codespeak.net
boria at codespeak.net
Wed Oct 12 14:42:02 CEST 2005
Author: boria
Date: Wed Oct 12 14:41:59 2005
New Revision: 18467
Added:
pypy/branch/hl-backend/pypy/rpython/typesystem.py (contents, props changed)
Modified:
pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooclean.py
pypy/branch/hl-backend/pypy/rpython/rmodel.py
pypy/branch/hl-backend/pypy/rpython/rpbc.py
pypy/branch/hl-backend/pypy/rpython/rtyper.py
Log:
(Samuele, Boris)
* Decoupling dependecies of rtyper on lltype: checkpoint.
Modified: pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooclean.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooclean.py (original)
+++ pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooclean.py Wed Oct 12 14:41:59 2005
@@ -2,6 +2,14 @@
from pypy.rpython import lltype
from pypy.rpython.ootype import ootype
+def specialize(f, input_types):
+ t = Translator(f)
+ t.annotate(input_types)
+ t.specialize(type_system="ootype")
+
+ graph = t.flowgraphs[f]
+ check_only_ootype(graph)
+
def check_only_ootype(graph):
def check_ootype(v):
t = v.concretetype
@@ -16,12 +24,7 @@
def test_simple():
def f(a, b):
return a + b
- t = Translator(f)
- t.annotate([int, int])
- t.specialize(type_system="ootype")
-
- graph = t.flowgraphs[f]
- check_only_ootype(graph)
+ specialize(f, [int, int])
def test_simple_call():
def f(a, b):
@@ -29,11 +32,14 @@
def g():
return f(5, 3)
+ specialize(g, [])
- t = Translator(g)
- t.annotate([])
-
- t.specialize(type_system="ootype")
+# Adjusted from test_rclass.py
+class EmptyBase(object):
+ pass
- graph = t.flowgraphs[g]
- check_only_ootype(graph)
+def test_simple():
+ def dummyfn():
+ x = EmptyBase()
+ return x
+ specialize(dummyfn, [])
Modified: pypy/branch/hl-backend/pypy/rpython/rmodel.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/rmodel.py (original)
+++ pypy/branch/hl-backend/pypy/rpython/rmodel.py Wed Oct 12 14:41:59 2005
@@ -307,26 +307,8 @@
"""
# __________ utilities __________
-
-PyObjPtr = Ptr(PyObject)
-
-def getconcretetype(v):
- return getattr(v, 'concretetype', PyObjPtr)
-
-def getfunctionptr(translator, graphfunc, getconcretetype=getconcretetype):
- return getcallable(translator, graphfunc, getconcretetype, FuncType, functionptr)
-
-def getstaticmeth(translator, graphfunc, getconcretetype=getconcretetype):
- return getcallable(translator, graphfunc, getconcretetype, ootype.StaticMethod, ootype.static_meth)
-
-def getcallable(translator, graphfunc, getconcretetype, typ, constr):
- """Make a functionptr from the given Python function."""
- graph = translator.getflowgraph(graphfunc)
- llinputs = [getconcretetype(v) for v in graph.getargs()]
- lloutput = getconcretetype(graph.getreturnvar())
- FT = typ(llinputs, lloutput)
- _callable = getattr(graphfunc, '_specializedversionof_', graphfunc)
- return constr(FT, graphfunc.func_name, graph = graph, _callable = _callable)
+from pypy.rpython.typesystem import LowLevelTypeSystem
+getfunctionptr = LowLevelTypeSystem.instance.getcallable
def needsgc(classdef, nogc=False):
if classdef is None:
Modified: pypy/branch/hl-backend/pypy/rpython/rpbc.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/rpbc.py (original)
+++ pypy/branch/hl-backend/pypy/rpython/rpbc.py Wed Oct 12 14:41:59 2005
@@ -345,7 +345,7 @@
def getsignature(rtyper, func):
f = rtyper.getcallable(func)
- graph = rtyper.deref(f).graph
+ graph = rtyper.type_system_deref(f).graph
rinputs = [rtyper.bindingrepr(v) for v in graph.getargs()]
if graph.getreturnvar() in rtyper.annotator.bindings:
rresult = rtyper.bindingrepr(graph.getreturnvar())
@@ -391,7 +391,7 @@
def get_args_ret_s(self):
f, _, _ = self.get_signature()
- graph = self.rtyper.deref(f).graph
+ graph = self.rtyper.type_system_deref(f).graph
rtyper = self.rtyper
return [rtyper.binding(arg) for arg in graph.getargs()], rtyper.binding(graph.getreturnvar())
@@ -419,7 +419,7 @@
def rtype_simple_call(self, hop):
f, rinputs, rresult = self.function_signatures().itervalues().next()
- if getattr(self.rtyper.deref(f).graph, 'normalized_for_calls', False):
+ if getattr(self.rtyper.type_system_deref(f).graph, 'normalized_for_calls', False):
# should not have an argument count mismatch
assert len(rinputs) == hop.nb_args-1, "normalization bug"
vlist = hop.inputargs(self, *rinputs)
@@ -446,7 +446,7 @@
f, rinputs, rresult = self.function_signatures().itervalues().next()
# the function arguments may have been normalized by normalizecalls()
# already
- if getattr(self.rtyper.deref(f).graph, 'normalized_for_calls', False):
+ if getattr(self.rtyper.type_system_deref(f).graph, 'normalized_for_calls', False):
vlist = hop.inputargs(self, Void, *rinputs)
vlist = vlist[:1] + vlist[2:]
else:
Modified: pypy/branch/hl-backend/pypy/rpython/rtyper.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/rtyper.py (original)
+++ pypy/branch/hl-backend/pypy/rpython/rtyper.py Wed Oct 12 14:41:59 2005
@@ -27,12 +27,14 @@
from pypy.translator.unsimplify import insert_empty_block
from pypy.rpython.rmodel import Repr, inputconst
from pypy.rpython.rmodel import TyperError, BrokenReprTyperError
-from pypy.rpython.rmodel import getfunctionptr, getstaticmeth, warning
+from pypy.rpython.rmodel import warning
from pypy.rpython.normalizecalls import perform_normalizations
from pypy.rpython.annlowlevel import annotate_lowlevel_helper
from pypy.rpython.exceptiondata import ExceptionData
from pypy.rpython.rmodel import log
+from pypy.rpython.typesystem import LowLevelTypeSystem,\
+ ObjectOrientedTypeSystem
class RPythonTyper:
@@ -40,9 +42,22 @@
def __init__(self, annotator, type_system="lltype"):
self.annotator = annotator
- if type_system not in ("lltype", "ootype"):
+ if type_system == "lltype":
+ self.type_system = LowLevelTypeSystem.instance
+ elif type_system == "ootype":
+ self.type_system = ObjectOrientedTypeSystem.instance
+ else:
raise TyperError("Unknown type system %r!" % type_system)
- self.type_system = type_system
+ self.type_system_deref = self.type_system.deref
+
+ def getfunctionptr(graphfunc):
+ def getconcretetype(v):
+ return self.bindingrepr(v).lowleveltype
+
+ return LowLevelTypeSystem.instance.getcallable(
+ self.annotator.translator, graphfunc, getconcretetype)
+
+ self.getfunctionptr = getfunctionptr
self.reprs = {}
self._reprs_must_call_setup = []
@@ -502,32 +517,12 @@
def needs_hash_support(self, cls):
return cls in self.annotator.bookkeeper.needs_hash_support
- def deref(self, obj):
- """Derefernce obj if it is a pointer."""
- if self.type_system == "ootype":
- assert isinstance(typeOf(obj), ootype.OOType)
- return obj
- elif self.type_system == "lltype":
- assert isinstance(typeOf(obj), Ptr)
-
- return obj._obj
-
def getcallable(self, graphfunc):
- if self.type_system == "lltype":
- return self.getfunctionptr(graphfunc)
-
- elif self.type_system == "ootype":
- return self.getstaticmeth(graphfunc)
-
- def getstaticmeth(self, graphfunc):
def getconcretetype(v):
return self.bindingrepr(v).lowleveltype
- return getstaticmeth(self.annotator.translator, graphfunc, getconcretetype)
- def getfunctionptr(self, graphfunc):
- def getconcretetype(v):
- return self.bindingrepr(v).lowleveltype
- return getfunctionptr(self.annotator.translator, graphfunc, getconcretetype)
+ return self.type_system.getcallable(
+ self.annotator.translator, graphfunc, getconcretetype)
def annotate_helper(self, ll_function, arglltypes):
"""Annotate the given low-level helper function
Added: pypy/branch/hl-backend/pypy/rpython/typesystem.py
==============================================================================
--- (empty file)
+++ pypy/branch/hl-backend/pypy/rpython/typesystem.py Wed Oct 12 14:41:59 2005
@@ -0,0 +1,54 @@
+
+"""typesystem.py -- Typesystem-specific operations for RTyper."""
+
+from pypy.annotation.pairtype import extendabletype
+
+from pypy.rpython.ootype import ootype
+from pypy.rpython import lltype # FIXME lltype in separate directory
+
+class TypeSystem(object):
+ __metaclass__ = extendabletype
+
+ def deref(self, obj):
+ """Dereference `obj' to concrete object."""
+ raise NotImplementedError()
+
+ def getcallable(self, translator, graphfunc, getconcretetype=None):
+ """Return callable given a Python function."""
+ if getconcretetype is None:
+ getconcretetype = self.getconcretetype
+ graph = translator.getflowgraph(graphfunc)
+ llinputs = [getconcretetype(v) for v in graph.getargs()]
+ lloutput = getconcretetype(graph.getreturnvar())
+
+ typ, constr = self.callable_trait
+
+ FT = typ(llinputs, lloutput)
+ _callable = getattr(graphfunc, '_specializedversionof_', graphfunc)
+ return constr(FT, graphfunc.func_name, graph = graph, _callable = _callable)
+
+ def getconcretetype(self, v):
+ """Helper called by getcallable() to get the conrete type of a variable
+in a graph."""
+ raise NotImplementedError()
+
+class LowLevelTypeSystem(TypeSystem):
+ callable_trait = (lltype.FuncType, lltype.functionptr)
+
+ def deref(self, obj):
+ assert isinstance(lltype.typeOf(obj), lltype.Ptr)
+ return obj._obj
+
+ def getconcretetype(self, v):
+ return getattr(v, 'concretetype', lltype.Ptr(lltype.PyObject))
+
+class ObjectOrientedTypeSystem(TypeSystem):
+ callable_trait = (ootype.StaticMethod, ootype.static_meth)
+
+ def deref(self, obj):
+ assert isinstance(ootype.typeOf(obj), ootype.OOType)
+ return obj
+
+# All typesystems are singletons
+LowLevelTypeSystem.instance = LowLevelTypeSystem()
+ObjectOrientedTypeSystem.instance = ObjectOrientedTypeSystem()
More information about the Pypy-commit
mailing list