Author: Ronan Lamy <[email protected]>
Branch: 
Changeset: r68986:f73987807040
Date: 2014-01-29 01:34 +0000
http://bitbucket.org/pypy/pypy/changeset/f73987807040/

Log:    move low-level stuff out of rpython.annotator.model

diff --git a/rpython/annotator/binaryop.py b/rpython/annotator/binaryop.py
--- a/rpython/annotator/binaryop.py
+++ b/rpython/annotator/binaryop.py
@@ -829,8 +829,8 @@
 
 # ____________________________________________________________
 # annotation of low-level types
-from rpython.rtyper.llannotation import SomePtr
-from rpython.annotator.model import ll_to_annotation, annotation_to_lltype
+from rpython.rtyper.llannotation import (
+    SomePtr, ll_to_annotation, annotation_to_lltype, lltype_to_annotation)
 
 class __extend__(pairtype(SomePtr, SomePtr)):
     def union((p1, p2)):
@@ -908,12 +908,10 @@
 
 class __extend__(pairtype(SomeTypedAddressAccess, SomeInteger)):
     def getitem((s_taa, s_int)):
-        from rpython.annotator.model import lltype_to_annotation
         return lltype_to_annotation(s_taa.type)
     getitem.can_only_throw = []
 
     def setitem((s_taa, s_int), s_value):
-        from rpython.annotator.model import annotation_to_lltype
         assert annotation_to_lltype(s_value) is s_taa.type
     setitem.can_only_throw = []
 
diff --git a/rpython/annotator/bookkeeper.py b/rpython/annotator/bookkeeper.py
--- a/rpython/annotator/bookkeeper.py
+++ b/rpython/annotator/bookkeeper.py
@@ -12,8 +12,9 @@
     SomeBuiltin, SomePBC, SomeInteger, TLS, SomeUnicodeCodePoint,
     s_None, s_ImpossibleValue, SomeBool, SomeTuple,
     SomeImpossibleValue, SomeUnicodeString, SomeList, HarmlesslyBlocked,
-    SomeWeakRef, lltype_to_annotation, SomeByteArray, SomeConstantType)
-from rpython.rtyper.llannotation import SomeAddress, SomePtr, SomeLLADTMeth
+    SomeWeakRef, SomeByteArray, SomeConstantType)
+from rpython.rtyper.llannotation import (
+    SomeAddress, SomePtr, SomeLLADTMeth, lltype_to_annotation)
 from rpython.annotator.classdef import InstanceSource, ClassDef
 from rpython.annotator.listdef import ListDef, ListItem
 from rpython.annotator.dictdef import DictDef
diff --git a/rpython/annotator/builtin.py b/rpython/annotator/builtin.py
--- a/rpython/annotator/builtin.py
+++ b/rpython/annotator/builtin.py
@@ -4,13 +4,12 @@
 import sys
 
 from rpython.annotator.model import (
-    SomeInteger, SomeObject, SomeChar, SomeBool, SomeString, SomeTuple, s_Bool,
+    SomeInteger, SomeObject, SomeChar, SomeBool, SomeString, SomeTuple,
     SomeUnicodeCodePoint, SomeFloat, unionof, SomeUnicodeString,
     SomePBC, SomeInstance, SomeDict, SomeList, SomeWeakRef, SomeIterator,
-    SomeOrderedDict,
-    SomeByteArray, annotation_to_lltype, lltype_to_annotation,
-    ll_to_annotation, add_knowntypedata, s_ImpossibleValue,)
-from rpython.rtyper.llannotation import SomeAddress
+    SomeOrderedDict, SomeByteArray, add_knowntypedata, s_ImpossibleValue,)
+from rpython.rtyper.llannotation import (
+    SomeAddress, annotation_to_lltype, lltype_to_annotation, ll_to_annotation)
 from rpython.annotator.bookkeeper import getbookkeeper
 from rpython.annotator import description
 from rpython.flowspace.model import Constant
diff --git a/rpython/annotator/model.py b/rpython/annotator/model.py
--- a/rpython/annotator/model.py
+++ b/rpython/annotator/model.py
@@ -568,86 +568,6 @@
         # 'classdef' is None for known-to-be-dead weakrefs.
         self.classdef = classdef
 
-#____________________________________________________________
-# annotation of low-level types
-
-from rpython.rtyper.lltypesystem import lltype
-
-
-
-from rpython.rtyper.llannotation import SomeAddress, SomePtr, SomeInteriorPtr
-from rpython.rtyper.lltypesystem import llmemory
-
-annotation_to_ll_map = [
-    (SomeSingleFloat(), lltype.SingleFloat),
-    (s_None, lltype.Void),   # also matches SomeImpossibleValue()
-    (s_Bool, lltype.Bool),
-    (SomeFloat(), lltype.Float),
-    (SomeLongFloat(), lltype.LongFloat),
-    (SomeChar(), lltype.Char),
-    (SomeUnicodeCodePoint(), lltype.UniChar),
-    (SomeAddress(), llmemory.Address),
-]
-
-
-def annotation_to_lltype(s_val, info=None):
-    if isinstance(s_val, SomeInteriorPtr):
-        p = s_val.ll_ptrtype
-        if 0 in p.offsets:
-            assert list(p.offsets).count(0) == 1
-            return 
lltype.Ptr(lltype.Ptr(p.PARENTTYPE)._interior_ptr_type_with_index(p.TO))
-        else:
-            return lltype.Ptr(p.PARENTTYPE)
-    if isinstance(s_val, SomePtr):
-        return s_val.ll_ptrtype
-    if type(s_val) is SomeInteger:
-        return lltype.build_number(None, s_val.knowntype)
-
-    for witness, T in annotation_to_ll_map:
-        if witness.contains(s_val):
-            return T
-    if info is None:
-        info = ''
-    else:
-        info = '%s: ' % info
-    raise ValueError("%sshould return a low-level type,\ngot instead %r" % (
-        info, s_val))
-
-ll_to_annotation_map = dict([(ll, ann) for ann, ll in annotation_to_ll_map])
-
-
-def lltype_to_annotation(T):
-    try:
-        s = ll_to_annotation_map.get(T)
-    except TypeError:
-        s = None    # unhashable T, e.g. a Ptr(GcForwardReference())
-    if s is None:
-        if isinstance(T, lltype.Typedef):
-            return lltype_to_annotation(T.OF)
-        if isinstance(T, lltype.Number):
-            return SomeInteger(knowntype=T._type)
-        elif isinstance(T, lltype.InteriorPtr):
-            return SomeInteriorPtr(T)
-        else:
-            return SomePtr(T)
-    else:
-        return s
-
-
-def ll_to_annotation(v):
-    if v is None:
-        # i think we can only get here in the case of void-returning
-        # functions
-        return s_None
-    if isinstance(v, lltype._interior_ptr):
-        ob = v._parent
-        if ob is None:
-            raise RuntimeError
-        T = lltype.InteriorPtr(lltype.typeOf(ob), v._T, v._offsets)
-        return SomeInteriorPtr(T)
-    return lltype_to_annotation(lltype.typeOf(v))
-
-
 # ____________________________________________________________
 
 
diff --git a/rpython/annotator/signature.py b/rpython/annotator/signature.py
--- a/rpython/annotator/signature.py
+++ b/rpython/annotator/signature.py
@@ -2,10 +2,11 @@
 from __future__ import absolute_import
 
 import types
-from rpython.annotator.model import SomeBool, SomeInteger, SomeString,\
-     SomeFloat, SomeList, SomeDict, s_None, \
-     SomeObject, SomeInstance, SomeTuple, lltype_to_annotation,\
-     unionof, SomeUnicodeString, SomeType, AnnotatorError
+from rpython.annotator.model import (
+    SomeBool, SomeInteger, SomeString, SomeFloat, SomeList, SomeDict, s_None,
+    SomeObject, SomeInstance, SomeTuple, unionof, SomeUnicodeString, SomeType,
+    AnnotatorError)
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.annotator.listdef import ListDef
 from rpython.annotator.dictdef import DictDef
 
diff --git a/rpython/annotator/test/test_model.py 
b/rpython/annotator/test/test_model.py
--- a/rpython/annotator/test/test_model.py
+++ b/rpython/annotator/test/test_model.py
@@ -1,9 +1,11 @@
 import py
 
 from rpython.annotator.model import *
-from rpython.rtyper.llannotation import SomePtr
+from rpython.rtyper.llannotation import (
+    SomePtr, annotation_to_lltype, ll_to_annotation)
 from rpython.annotator.listdef import ListDef
 from rpython.translator.translator import TranslationContext
+from rpython.rtyper.typesystem import lltype
 
 
 listdef1 = ListDef(None, SomeTuple([SomeInteger(nonneg=True), SomeString()]))
diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -759,8 +759,9 @@
             raise AnnotatorError("Cannot call len on a pbc")
 
 # annotation of low-level types
-from rpython.rtyper.llannotation import SomePtr, SomeLLADTMeth
-from rpython.annotator.model import ll_to_annotation, lltype_to_annotation, 
annotation_to_lltype
+from rpython.rtyper.llannotation import (
+    SomePtr, SomeLLADTMeth, ll_to_annotation, lltype_to_annotation,
+    annotation_to_lltype)
 
 class __extend__(SomePtr):
 
diff --git a/rpython/jit/backend/llsupport/llmodel.py 
b/rpython/jit/backend/llsupport/llmodel.py
--- a/rpython/jit/backend/llsupport/llmodel.py
+++ b/rpython/jit/backend/llsupport/llmodel.py
@@ -2,6 +2,7 @@
 from rpython.rtyper.lltypesystem.lloperation import llop
 from rpython.rtyper.llinterp import LLInterpreter
 from rpython.rtyper.annlowlevel import llhelper, MixLevelHelperAnnotator
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rlib.objectmodel import we_are_translated, specialize
 from rpython.jit.metainterp import history
 from rpython.jit.codewriter import heaptracker, longlong
@@ -14,7 +15,6 @@
     FieldDescr, ArrayDescr, CallDescr, InteriorFieldDescr,
     FLAG_POINTER, FLAG_FLOAT)
 from rpython.jit.backend.llsupport.asmmemmgr import AsmMemoryManager
-from rpython.annotator import model as annmodel
 from rpython.rlib.unroll import unrolling_iterable
 
 
@@ -111,8 +111,8 @@
             fptr = llhelper(FUNC_TP, realloc_frame)
         else:
             FUNC = FUNC_TP.TO
-            args_s = [annmodel.lltype_to_annotation(ARG) for ARG in FUNC.ARGS]
-            s_result = annmodel.lltype_to_annotation(FUNC.RESULT)
+            args_s = [lltype_to_annotation(ARG) for ARG in FUNC.ARGS]
+            s_result = lltype_to_annotation(FUNC.RESULT)
             mixlevelann = MixLevelHelperAnnotator(self.rtyper)
             graph = mixlevelann.getgraph(realloc_frame, args_s, s_result)
             fptr = mixlevelann.graph2delayed(graph, FUNC)
@@ -123,8 +123,8 @@
             fptr = llhelper(FUNC_TP, realloc_frame_crash)
         else:
             FUNC = FUNC_TP.TO
-            args_s = [annmodel.lltype_to_annotation(ARG) for ARG in FUNC.ARGS]
-            s_result = annmodel.lltype_to_annotation(FUNC.RESULT)
+            args_s = [lltype_to_annotation(ARG) for ARG in FUNC.ARGS]
+            s_result = lltype_to_annotation(FUNC.RESULT)
             mixlevelann = MixLevelHelperAnnotator(self.rtyper)
             graph = mixlevelann.getgraph(realloc_frame_crash, args_s, s_result)
             fptr = mixlevelann.graph2delayed(graph, FUNC)
diff --git a/rpython/jit/codewriter/support.py 
b/rpython/jit/codewriter/support.py
--- a/rpython/jit/codewriter/support.py
+++ b/rpython/jit/codewriter/support.py
@@ -1,6 +1,7 @@
 import sys
 
 from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.annotator.policy import AnnotatorPolicy
 from rpython.flowspace.model import Variable, Constant
 from rpython.jit.metainterp.typesystem import deref
@@ -32,7 +33,7 @@
     if T == lltype.Ptr(ll_rstr.STR):
         t = str
     else:
-        t = annmodel.lltype_to_annotation(T)
+        t = lltype_to_annotation(T)
     return a.typeannotation(t)
 
 def annotate(func, values, inline=None, backendoptimize=True,
@@ -814,12 +815,12 @@
         return rtyper._builtin_func_for_spec_cache[key]
     except (KeyError, AttributeError):
         pass
-    args_s = [annmodel.lltype_to_annotation(v) for v in ll_args]
+    args_s = [lltype_to_annotation(v) for v in ll_args]
     if '.' not in oopspec_name:    # 'newxxx' operations
         LIST_OR_DICT = ll_res
     else:
         LIST_OR_DICT = ll_args[0]
-    s_result = annmodel.lltype_to_annotation(ll_res)
+    s_result = lltype_to_annotation(ll_res)
     impl = setup_extra_builtin(rtyper, oopspec_name, len(args_s), extra)
     if getattr(impl, 'need_result_type', False):
         bk = rtyper.annotator.bookkeeper
diff --git a/rpython/jit/metainterp/test/test_virtualizable.py 
b/rpython/jit/metainterp/test/test_virtualizable.py
--- a/rpython/jit/metainterp/test/test_virtualizable.py
+++ b/rpython/jit/metainterp/test/test_virtualizable.py
@@ -9,6 +9,7 @@
 from rpython.rlib.jit import JitDriver, hint, dont_look_inside, promote, 
virtual_ref
 from rpython.rlib.rarithmetic import intmask
 from rpython.rtyper.annlowlevel import hlstr
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rtyper.extregistry import ExtRegistryEntry
 from rpython.rtyper.lltypesystem import lltype, lloperation, rclass, llmemory
 from rpython.rtyper.rclass import IR_IMMUTABLE, IR_IMMUTABLE_ARRAY, 
FieldListAccessor
@@ -23,7 +24,6 @@
     _about_ = promote_virtualizable
 
     def compute_result_annotation(self, *args):
-        from rpython.annotator.model import lltype_to_annotation
         return lltype_to_annotation(lltype.Void)
 
     def specialize_call(self, hop):
diff --git a/rpython/jit/metainterp/warmspot.py 
b/rpython/jit/metainterp/warmspot.py
--- a/rpython/jit/metainterp/warmspot.py
+++ b/rpython/jit/metainterp/warmspot.py
@@ -4,6 +4,7 @@
 from rpython.rtyper.lltypesystem import lltype, llmemory
 from rpython.rtyper.annlowlevel import (llhelper, MixLevelHelperAnnotator,
     cast_base_ptr_to_instance, hlstr)
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.annotator import model as annmodel
 from rpython.rtyper.llinterp import LLException
 from rpython.rtyper.test.test_llinterp import get_interpreter, clear_tcache
@@ -662,8 +663,8 @@
         if not self.cpu.translate_support_code:
             return llhelper(FUNCPTR, func)
         FUNC = FUNCPTR.TO
-        args_s = [annmodel.lltype_to_annotation(ARG) for ARG in FUNC.ARGS]
-        s_result = annmodel.lltype_to_annotation(FUNC.RESULT)
+        args_s = [lltype_to_annotation(ARG) for ARG in FUNC.ARGS]
+        s_result = lltype_to_annotation(FUNC.RESULT)
         graph = self.annhelper.getgraph(func, args_s, s_result)
         return self.annhelper.graph2delayed(graph, FUNC)
 
diff --git a/rpython/memory/gctransform/transform.py 
b/rpython/memory/gctransform/transform.py
--- a/rpython/memory/gctransform/transform.py
+++ b/rpython/memory/gctransform/transform.py
@@ -9,7 +9,7 @@
 from rpython.translator.backendopt.canraise import RaiseAnalyzer
 from rpython.translator.backendopt.ssa import DataFlowFamilyBuilder
 from rpython.translator.backendopt.constfold import constant_fold_graph
-from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rtyper import rmodel
 from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator
 from rpython.rtyper.rtyper import LowLevelOpList
@@ -259,8 +259,8 @@
 
     def annotate_helper(self, ll_helper, ll_args, ll_result, inline=False):
         assert not self.finished_helpers
-        args_s = map(annmodel.lltype_to_annotation, ll_args)
-        s_result = annmodel.lltype_to_annotation(ll_result)
+        args_s = map(lltype_to_annotation, ll_args)
+        s_result = lltype_to_annotation(ll_result)
         graph = self.mixlevelannotator.getgraph(ll_helper, args_s, s_result)
         # the produced graphs does not need to be fully transformed
         self.need_minimal_transform(graph)
diff --git a/rpython/rlib/_stacklet_asmgcc.py b/rpython/rlib/_stacklet_asmgcc.py
--- a/rpython/rlib/_stacklet_asmgcc.py
+++ b/rpython/rlib/_stacklet_asmgcc.py
@@ -3,6 +3,7 @@
 from rpython.rtyper.lltypesystem.lloperation import llop
 from rpython.rtyper.annlowlevel import llhelper, MixLevelHelperAnnotator
 from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rlib import _rffi_stacklet as _c
 
 
@@ -145,7 +146,7 @@
 def complete_destrptr(gctransformer):
     translator = gctransformer.translator
     mixlevelannotator = MixLevelHelperAnnotator(translator.rtyper)
-    args_s = [annmodel.lltype_to_annotation(lltype.Ptr(SUSPSTACK))]
+    args_s = [lltype_to_annotation(lltype.Ptr(SUSPSTACK))]
     s_result = annmodel.s_None
     destrptr = mixlevelannotator.delayedfunction(suspstack_destructor,
                                                  args_s, s_result)
diff --git a/rpython/rlib/debug.py b/rpython/rlib/debug.py
--- a/rpython/rlib/debug.py
+++ b/rpython/rlib/debug.py
@@ -229,12 +229,13 @@
 
     def compute_result_annotation(self, s_RESTYPE, s_pythonfunction, *args_s):
         from rpython.annotator import model as annmodel
+        from rpython.rtyper.llannotation import lltype_to_annotation
         from rpython.rtyper.lltypesystem import lltype
         assert s_RESTYPE.is_constant()
         assert s_pythonfunction.is_constant()
         s_result = s_RESTYPE.const
         if isinstance(s_result, lltype.LowLevelType):
-            s_result = annmodel.lltype_to_annotation(s_result)
+            s_result = lltype_to_annotation(s_result)
         assert isinstance(s_result, annmodel.SomeObject)
         return s_result
 
diff --git a/rpython/rlib/jit_hooks.py b/rpython/rlib/jit_hooks.py
--- a/rpython/rlib/jit_hooks.py
+++ b/rpython/rlib/jit_hooks.py
@@ -1,5 +1,5 @@
 from rpython.annotator import model as annmodel
-from rpython.rtyper.llannotation import SomePtr
+from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation
 from rpython.rlib.objectmodel import specialize
 from rpython.rtyper.annlowlevel import (cast_instance_to_base_ptr,
     cast_base_ptr_to_instance, llstr)
@@ -16,7 +16,7 @@
                 if (isinstance(s_result, annmodel.SomeObject) or
                     s_result is None):
                     return s_result
-                return annmodel.lltype_to_annotation(s_result)
+                return lltype_to_annotation(s_result)
 
             def specialize_call(self, hop):
                 from rpython.rtyper.lltypesystem import lltype
diff --git a/rpython/rlib/rawstorage.py b/rpython/rlib/rawstorage.py
--- a/rpython/rlib/rawstorage.py
+++ b/rpython/rlib/rawstorage.py
@@ -2,6 +2,7 @@
 from rpython.rtyper.extregistry import ExtRegistryEntry
 from rpython.rtyper.lltypesystem import lltype, rffi, llmemory
 from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rlib.rgc import lltype_is_gc
 from rpython.rlib.objectmodel import specialize
 
@@ -33,7 +34,7 @@
 
     def compute_result_annotation(self, s_TP, s_storage, s_index):
         assert s_TP.is_constant()
-        return annmodel.lltype_to_annotation(s_TP.const)
+        return lltype_to_annotation(s_TP.const)
 
     def specialize_call(self, hop):
         assert hop.args_r[1].lowleveltype == RAW_STORAGE_PTR
diff --git a/rpython/rlib/rrawarray.py b/rpython/rlib/rrawarray.py
--- a/rpython/rlib/rrawarray.py
+++ b/rpython/rlib/rrawarray.py
@@ -1,4 +1,4 @@
-from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rlib.objectmodel import specialize
 from rpython.rlib import jit
 from rpython.rtyper.lltypesystem import lltype, llmemory
@@ -30,7 +30,7 @@
     _about_ = populate_list_from_raw_array
 
     def compute_result_annotation(self, s_list, s_array, s_length):
-        s_item = annmodel.lltype_to_annotation(s_array.ll_ptrtype.TO.OF)
+        s_item = lltype_to_annotation(s_array.ll_ptrtype.TO.OF)
         s_newlist = self.bookkeeper.newlist(s_item)
         s_newlist.listdef.resize()
         pair(s_list, s_newlist).union()
diff --git a/rpython/rlib/types.py b/rpython/rlib/types.py
--- a/rpython/rlib/types.py
+++ b/rpython/rlib/types.py
@@ -57,7 +57,8 @@
 
 def ptr(ll_type):
     from rpython.rtyper.lltypesystem.lltype import Ptr
-    return model.SomePtr(Ptr(ll_type))
+    from rpython.rtyper.llannotation import SomePtr
+    return SomePtr(Ptr(ll_type))
 
 
 def list(element):
diff --git a/rpython/rtyper/annlowlevel.py b/rpython/rtyper/annlowlevel.py
--- a/rpython/rtyper/annlowlevel.py
+++ b/rpython/rtyper/annlowlevel.py
@@ -7,7 +7,8 @@
 from rpython.annotator.policy import AnnotatorPolicy
 from rpython.annotator.signature import Sig
 from rpython.annotator.specialize import flatten_star_args
-from rpython.rtyper.llannotation import SomePtr
+from rpython.rtyper.llannotation import (
+    SomePtr, annotation_to_lltype, lltype_to_annotation)
 from rpython.rtyper.normalizecalls import perform_normalizations
 from rpython.rtyper.lltypesystem import lltype, llmemory
 from rpython.flowspace.model import Constant
@@ -58,7 +59,7 @@
             else:
                 new_args_s.append(annmodel.not_const(s_obj))
                 try:
-                    key.append(annmodel.annotation_to_lltype(s_obj))
+                    key.append(annotation_to_lltype(s_obj))
                 except ValueError:
                     # passing non-low-level types to a ll_* function is allowed
                     # for module/ll_*
@@ -76,8 +77,8 @@
     default_specialize = staticmethod(default_specialize)
 
     def specialize__semierased(funcdesc, args_s):
-        a2l = annmodel.annotation_to_lltype
-        l2a = annmodel.lltype_to_annotation
+        a2l = annotation_to_lltype
+        l2a = lltype_to_annotation
         args_s[:] = [l2a(a2l(s)) for s in args_s]
         return LowLevelAnnotatorPolicy.default_specialize(funcdesc, args_s)
     specialize__semierased = staticmethod(specialize__semierased)
@@ -121,8 +122,8 @@
 
     def specialize__genconst(pol, funcdesc, args_s, i):
         # XXX this is specific to the JIT
-        TYPE = annmodel.annotation_to_lltype(args_s[i], 'genconst')
-        args_s[i] = annmodel.lltype_to_annotation(TYPE)
+        TYPE = annotation_to_lltype(args_s[i], 'genconst')
+        args_s[i] = lltype_to_annotation(TYPE)
         alt_name = funcdesc.name + "__%s" % (TYPE._short_name(),)
         return funcdesc.cachedgraph(TYPE, alt_name=valid_identifier(alt_name))
 
@@ -356,10 +357,10 @@
         assert s_callable.is_constant()
         F = s_F.const
         FUNC = F.TO
-        args_s = [annmodel.lltype_to_annotation(T) for T in FUNC.ARGS]
+        args_s = [lltype_to_annotation(T) for T in FUNC.ARGS]
         key = (llhelper, s_callable.const)
         s_res = self.bookkeeper.emulate_pbc_call(key, s_callable, args_s)
-        assert annmodel.lltype_to_annotation(FUNC.RESULT).contains(s_res)
+        assert lltype_to_annotation(FUNC.RESULT).contains(s_res)
         return SomePtr(F)
 
     def specialize_call(self, hop):
@@ -419,9 +420,9 @@
         def compute_result_annotation(self, s_str):
             from rpython.rtyper.lltypesystem.rstr import STR, UNICODE
             if strtype is str:
-                return annmodel.lltype_to_annotation(lltype.Ptr(STR))
+                return lltype_to_annotation(lltype.Ptr(STR))
             else:
-                return annmodel.lltype_to_annotation(lltype.Ptr(UNICODE))
+                return lltype_to_annotation(lltype.Ptr(UNICODE))
 
         def specialize_call(self, hop):
             hop.exception_cannot_occur()
diff --git a/rpython/rtyper/llannotation.py b/rpython/rtyper/llannotation.py
--- a/rpython/rtyper/llannotation.py
+++ b/rpython/rtyper/llannotation.py
@@ -1,8 +1,10 @@
 """
 Code for annotating low-level thingies.
 """
-from rpython.annotator.model import SomeObject
-from rpython.rtyper.lltypesystem import lltype
+from rpython.annotator.model import (
+    SomeObject, SomeSingleFloat, SomeFloat, SomeLongFloat, SomeChar,
+    SomeUnicodeCodePoint, SomeInteger, s_None, s_Bool)
+from rpython.rtyper.lltypesystem import lltype, llmemory
 
 class SomeAddress(SomeObject):
     immutable = True
@@ -52,3 +54,72 @@
 
     def can_be_none(self):
         return False
+
+
+annotation_to_ll_map = [
+    (SomeSingleFloat(), lltype.SingleFloat),
+    (s_None, lltype.Void),   # also matches SomeImpossibleValue()
+    (s_Bool, lltype.Bool),
+    (SomeFloat(), lltype.Float),
+    (SomeLongFloat(), lltype.LongFloat),
+    (SomeChar(), lltype.Char),
+    (SomeUnicodeCodePoint(), lltype.UniChar),
+    (SomeAddress(), llmemory.Address),
+]
+
+
+def annotation_to_lltype(s_val, info=None):
+    if isinstance(s_val, SomeInteriorPtr):
+        p = s_val.ll_ptrtype
+        if 0 in p.offsets:
+            assert list(p.offsets).count(0) == 1
+            return 
lltype.Ptr(lltype.Ptr(p.PARENTTYPE)._interior_ptr_type_with_index(p.TO))
+        else:
+            return lltype.Ptr(p.PARENTTYPE)
+    if isinstance(s_val, SomePtr):
+        return s_val.ll_ptrtype
+    if type(s_val) is SomeInteger:
+        return lltype.build_number(None, s_val.knowntype)
+
+    for witness, T in annotation_to_ll_map:
+        if witness.contains(s_val):
+            return T
+    if info is None:
+        info = ''
+    else:
+        info = '%s: ' % info
+    raise ValueError("%sshould return a low-level type,\ngot instead %r" % (
+        info, s_val))
+
+ll_to_annotation_map = dict([(ll, ann) for ann, ll in annotation_to_ll_map])
+
+def lltype_to_annotation(T):
+    try:
+        s = ll_to_annotation_map.get(T)
+    except TypeError:
+        s = None    # unhashable T, e.g. a Ptr(GcForwardReference())
+    if s is None:
+        if isinstance(T, lltype.Typedef):
+            return lltype_to_annotation(T.OF)
+        if isinstance(T, lltype.Number):
+            return SomeInteger(knowntype=T._type)
+        elif isinstance(T, lltype.InteriorPtr):
+            return SomeInteriorPtr(T)
+        else:
+            return SomePtr(T)
+    else:
+        return s
+
+
+def ll_to_annotation(v):
+    if v is None:
+        # i think we can only get here in the case of void-returning
+        # functions
+        return s_None
+    if isinstance(v, lltype._interior_ptr):
+        ob = v._parent
+        if ob is None:
+            raise RuntimeError
+        T = lltype.InteriorPtr(lltype.typeOf(ob), v._T, v._offsets)
+        return SomeInteriorPtr(T)
+    return lltype_to_annotation(lltype.typeOf(v))
diff --git a/rpython/rtyper/lltypesystem/ll2ctypes.py 
b/rpython/rtyper/lltypesystem/ll2ctypes.py
--- a/rpython/rtyper/lltypesystem/ll2ctypes.py
+++ b/rpython/rtyper/lltypesystem/ll2ctypes.py
@@ -22,6 +22,7 @@
 from rpython.rlib.rarithmetic import r_singlefloat, r_longfloat, base_int, 
intmask
 from rpython.rlib.rarithmetic import is_emulated_long, maxint
 from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rtyper.llannotation import SomePtr
 from rpython.rtyper.llinterp import LLInterpreter, LLException
 from rpython.rtyper.lltypesystem.rclass import OBJECT, OBJECT_VTABLE
@@ -1303,7 +1304,7 @@
     def compute_result_annotation(self, s_RESTYPE, s_value):
         assert s_RESTYPE.is_constant()
         RESTYPE = s_RESTYPE.const
-        return annmodel.lltype_to_annotation(RESTYPE)
+        return lltype_to_annotation(RESTYPE)
 
     def specialize_call(self, hop):
         hop.exception_cannot_occur()
@@ -1342,7 +1343,7 @@
         assert isinstance(s_n, annmodel.SomeInteger)
         assert isinstance(s_ptr, SomePtr)
         typecheck_ptradd(s_ptr.ll_ptrtype)
-        return annmodel.lltype_to_annotation(s_ptr.ll_ptrtype)
+        return lltype_to_annotation(s_ptr.ll_ptrtype)
 
     def specialize_call(self, hop):
         hop.exception_cannot_occur()
diff --git a/rpython/rtyper/lltypesystem/lloperation.py 
b/rpython/rtyper/lltypesystem/lloperation.py
--- a/rpython/rtyper/lltypesystem/lloperation.py
+++ b/rpython/rtyper/lltypesystem/lloperation.py
@@ -141,7 +141,7 @@
     _type_ = LLOp
 
     def compute_result_annotation(self, RESULTTYPE, *args):
-        from rpython.annotator.model import lltype_to_annotation
+        from rpython.rtyper.llannotation import lltype_to_annotation
         assert RESULTTYPE.is_constant()
         return lltype_to_annotation(RESULTTYPE.const)
 
diff --git a/rpython/rtyper/lltypesystem/rffi.py 
b/rpython/rtyper/lltypesystem/rffi.py
--- a/rpython/rtyper/lltypesystem/rffi.py
+++ b/rpython/rtyper/lltypesystem/rffi.py
@@ -5,7 +5,7 @@
 from rpython.rtyper.lltypesystem import ll2ctypes
 from rpython.rtyper.lltypesystem.llmemory import cast_ptr_to_adr
 from rpython.rtyper.lltypesystem.llmemory import itemoffsetof, raw_memcopy
-from rpython.annotator.model import lltype_to_annotation
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.tool.sourcetools import func_with_new_name
 from rpython.rlib.objectmodel import Symbolic
 from rpython.rlib.objectmodel import keepalive_until_here, enforceargs
diff --git a/rpython/rtyper/module/ll_os_stat.py 
b/rpython/rtyper/module/ll_os_stat.py
--- a/rpython/rtyper/module/ll_os_stat.py
+++ b/rpython/rtyper/module/ll_os_stat.py
@@ -7,6 +7,7 @@
 import sys
 
 from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rlib import rposix
 from rpython.rlib.rarithmetic import intmask
 from rpython.rtyper import extregistry
@@ -87,7 +88,7 @@
         assert s_attr.is_constant(), "non-constant attr name in getattr()"
         attrname = s_attr.const
         TYPE = STAT_FIELD_TYPES[attrname]
-        return annmodel.lltype_to_annotation(TYPE)
+        return lltype_to_annotation(TYPE)
 
     def _get_rmarshall_support_(self):     # for rlib.rmarshal
         # reduce and recreate stat_result objects from 10-tuples
@@ -98,7 +99,7 @@
 
         def stat_result_recreate(tup):
             return make_stat_result(tup + extra_zeroes)
-        s_reduced = annmodel.SomeTuple([annmodel.lltype_to_annotation(TYPE)
+        s_reduced = annmodel.SomeTuple([lltype_to_annotation(TYPE)
                                        for name, TYPE in PORTABLE_STAT_FIELDS])
         extra_zeroes = (0,) * (len(STAT_FIELDS) - len(PORTABLE_STAT_FIELDS))
         return s_reduced, stat_result_reduce, stat_result_recreate
@@ -120,7 +121,7 @@
     def getattr(self, s_attr):
         assert s_attr.is_constant()
         TYPE = STATVFS_FIELD_TYPES[s_attr.const]
-        return annmodel.lltype_to_annotation(TYPE)
+        return lltype_to_annotation(TYPE)
 
 
 class __extend__(pairtype(SomeStatResult, annmodel.SomeInteger)):
@@ -129,14 +130,14 @@
         index = s_int.const
         assert 0 <= index < N_INDEXABLE_FIELDS, "os.stat()[index] out of range"
         name, TYPE = STAT_FIELDS[index]
-        return annmodel.lltype_to_annotation(TYPE)
+        return lltype_to_annotation(TYPE)
 
 
 class __extend__(pairtype(SomeStatvfsResult, annmodel.SomeInteger)):
     def getitem((s_stat, s_int)):
         assert s_int.is_constant()
         name, TYPE = STATVFS_FIELDS[s_int.const]
-        return annmodel.lltype_to_annotation(TYPE)
+        return lltype_to_annotation(TYPE)
 
 
 s_StatResult = SomeStatResult()
diff --git a/rpython/rtyper/module/r_os_stat.py 
b/rpython/rtyper/module/r_os_stat.py
--- a/rpython/rtyper/module/r_os_stat.py
+++ b/rpython/rtyper/module/r_os_stat.py
@@ -8,6 +8,7 @@
 the tuples returned by os.stat().
 """
 from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.flowspace.model import Constant
 from rpython.tool.pairtype import pairtype
 from rpython.rtyper.rmodel import Repr, IntegerRepr
@@ -25,7 +26,7 @@
         for i, (name, TYPE) in enumerate(self.stat_fields):
             self.stat_field_indexes[name] = i
 
-        self.s_tuple = annmodel.SomeTuple([annmodel.lltype_to_annotation(TYPE)
+        self.s_tuple = annmodel.SomeTuple([lltype_to_annotation(TYPE)
                                            for name, TYPE in self.stat_fields])
         self.r_tuple = rtyper.getrepr(self.s_tuple)
         self.lowleveltype = self.r_tuple.lowleveltype
@@ -76,7 +77,7 @@
         for i, (name, TYPE) in enumerate(self.statvfs_fields):
             self.statvfs_field_indexes[name] = i
 
-        self.s_tuple = annmodel.SomeTuple([annmodel.lltype_to_annotation(TYPE)
+        self.s_tuple = annmodel.SomeTuple([lltype_to_annotation(TYPE)
                                            for name, TYPE in 
self.statvfs_fields])
         self.r_tuple = rtyper.getrepr(self.s_tuple)
         self.lowleveltype = self.r_tuple.lowleveltype
diff --git a/rpython/rtyper/rptr.py b/rpython/rtyper/rptr.py
--- a/rpython/rtyper/rptr.py
+++ b/rpython/rtyper/rptr.py
@@ -1,6 +1,6 @@
 from rpython.annotator import model as annmodel
 from rpython.rtyper.llannotation import (
-    SomePtr, SomeInteriorPtr, SomeLLADTMeth)
+    SomePtr, SomeInteriorPtr, SomeLLADTMeth, lltype_to_annotation)
 from rpython.flowspace import model as flowmodel
 from rpython.rlib.rarithmetic import r_uint
 from rpython.rtyper.error import TyperError
@@ -193,7 +193,7 @@
         self.func = adtmeth.func
         self.lowleveltype = adtmeth.ll_ptrtype
         self.ll_ptrtype = adtmeth.ll_ptrtype
-        self.lowleveltype = 
rtyper.getrepr(annmodel.lltype_to_annotation(adtmeth.ll_ptrtype)).lowleveltype
+        self.lowleveltype = 
rtyper.getrepr(lltype_to_annotation(adtmeth.ll_ptrtype)).lowleveltype
 
     def rtype_simple_call(self, hop):
         hop2 = hop.copy()
@@ -201,8 +201,7 @@
         s_func = hop.rtyper.annotator.bookkeeper.immutablevalue(func)
         v_ptr = hop2.args_v[0]
         hop2.r_s_popfirstarg()
-        hop2.v_s_insertfirstarg(
-            v_ptr, annmodel.lltype_to_annotation(self.ll_ptrtype))
+        hop2.v_s_insertfirstarg(v_ptr, lltype_to_annotation(self.ll_ptrtype))
         hop2.v_s_insertfirstarg(flowmodel.Constant(func), s_func)
         return hop2.dispatch()
 
diff --git a/rpython/rtyper/rtyper.py b/rpython/rtyper/rtyper.py
--- a/rpython/rtyper/rtyper.py
+++ b/rpython/rtyper/rtyper.py
@@ -16,7 +16,7 @@
 import py
 
 from rpython.annotator import model as annmodel, unaryop, binaryop
-from rpython.rtyper.llannotation import SomePtr
+from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation
 from rpython.annotator.annrpython import FAIL
 from rpython.flowspace.model import Variable, Constant, SpaceOperation, 
c_last_exception
 from rpython.rtyper.annlowlevel import annotate_lowlevel_helper, 
LowLevelAnnotatorPolicy
@@ -77,7 +77,7 @@
         except KeyError:
             pass
         if isinstance(lltype, Primitive):
-            repr = self.primitive_to_repr[lltype] = 
self.getrepr(annmodel.lltype_to_annotation(lltype))
+            repr = self.primitive_to_repr[lltype] = 
self.getrepr(lltype_to_annotation(lltype))
             return repr
         raise TyperError('There is no primitive repr for %r' % (lltype,))
 
@@ -616,7 +616,7 @@
         for s in argtypes:
             # assume 's' is a low-level type, unless it is already an 
annotation
             if not isinstance(s, annmodel.SomeObject):
-                s = annmodel.lltype_to_annotation(s)
+                s = lltype_to_annotation(s)
             args_s.append(s)
         # hack for bound methods
         if hasattr(ll_function, 'im_func'):
@@ -903,7 +903,7 @@
                     raise TyperError("non-PBC Void argument: %r", (s_value,))
                 args_s.append(s_value)
             else:
-                args_s.append(annmodel.lltype_to_annotation(v.concretetype))
+                args_s.append(lltype_to_annotation(v.concretetype))
             newargs_v.append(v)
 
         self.rtyper.call_all_setups()  # compute ForwardReferences now
diff --git a/rpython/rtyper/test/test_llann.py 
b/rpython/rtyper/test/test_llann.py
--- a/rpython/rtyper/test/test_llann.py
+++ b/rpython/rtyper/test/test_llann.py
@@ -1,7 +1,7 @@
 import py
 
 from rpython.annotator import model as annmodel
-from rpython.rtyper.llannotation import SomePtr
+from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation
 from rpython.conftest import option
 from rpython.rtyper.annlowlevel import (annotate_lowlevel_helper,
     MixLevelHelperAnnotator, PseudoHighLevelCallable, llhelper,
@@ -202,7 +202,7 @@
                     assert a.binding(vT) == a.bookkeeper.immutablevalue(T)
                     assert a.binding(vi).knowntype == int
                     assert a.binding(vp).ll_ptrtype.TO == T
-                    assert a.binding(rv) == annmodel.lltype_to_annotation(T.OF)
+                    assert a.binding(rv) == lltype_to_annotation(T.OF)
                 elif func is ll_make:
                     vT, vn = args
                     assert a.binding(vT) == a.bookkeeper.immutablevalue(T)
@@ -281,7 +281,7 @@
                     vp, vi = args
                     assert a.binding(vi).knowntype == int
                     assert a.binding(vp).ll_ptrtype == T
-                    assert a.binding(rv) == annmodel.lltype_to_annotation(
+                    assert a.binding(rv) == lltype_to_annotation(
                         T.TO.OF)
                 else:
                     assert False, func
diff --git a/rpython/rtyper/test/test_llinterp.py 
b/rpython/rtyper/test/test_llinterp.py
--- a/rpython/rtyper/test/test_llinterp.py
+++ b/rpython/rtyper/test/test_llinterp.py
@@ -9,7 +9,7 @@
 from rpython.translator.translator import TranslationContext, graphof
 from rpython.rtyper.lltypesystem import lltype
 from rpython.annotator import model as annmodel
-from rpython.annotator.model import lltype_to_annotation
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rlib.rarithmetic import r_uint, ovfcheck
 from rpython.tool import leakfinder
 from rpython.conftest import option
diff --git a/rpython/rtyper/test/test_rpbc.py b/rpython/rtyper/test/test_rpbc.py
--- a/rpython/rtyper/test/test_rpbc.py
+++ b/rpython/rtyper/test/test_rpbc.py
@@ -1,9 +1,10 @@
 import py
 
+from rpython.annotator import model as annmodel
 from rpython.annotator import policy, specialize
 from rpython.rtyper.lltypesystem.lltype import typeOf
 from rpython.rtyper.test.tool import BaseRtypingTest
-from rpython.rtyper.llannotation import SomePtr
+from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation
 
 
 class MyBase:
@@ -1702,7 +1703,6 @@
     from rpython.translator import translator
     from rpython.annotator import annrpython
     a = annrpython.RPythonAnnotator()
-    from rpython.annotator import model as annmodel
 
     s_f = a.bookkeeper.immutablevalue(f)
     a.bookkeeper.emulate_pbc_call('f', s_f, [annmodel.SomeInteger(), 
annmodel.SomeInteger()])
@@ -1721,7 +1721,7 @@
     r_f = rt.getrepr(s_f)
 
     s_R = a.bookkeeper.immutablevalue(r_f)
-    s_ll_f = annmodel.lltype_to_annotation(r_f.lowleveltype)
+    s_ll_f = lltype_to_annotation(r_f.lowleveltype)
     ll_h_graph = annlowlevel.annotate_lowlevel_helper(a, ll_h, [s_R, s_ll_f, 
annmodel.SomeInteger()])
     assert a.binding(ll_h_graph.getreturnvar()).knowntype == int
     rt.specialize_more_blocks()
@@ -1740,7 +1740,6 @@
         return a - b
     from rpython.annotator import annrpython
     a = annrpython.RPythonAnnotator()
-    from rpython.annotator import model as annmodel
 
     def g(i):
         if i:
@@ -1769,7 +1768,7 @@
     r_f = rt.getrepr(s_f)
 
     s_R = a.bookkeeper.immutablevalue(r_f)
-    s_ll_f = annmodel.lltype_to_annotation(r_f.lowleveltype)
+    s_ll_f = lltype_to_annotation(r_f.lowleveltype)
     ll_h_graph= annlowlevel.annotate_lowlevel_helper(a, ll_h, [s_R, s_ll_f, 
annmodel.SomeInteger()])
     assert a.binding(ll_h_graph.getreturnvar()).knowntype == int
     rt.specialize_more_blocks()
@@ -1792,7 +1791,6 @@
 
     from rpython.annotator import annrpython
     a = annrpython.RPythonAnnotator()
-    from rpython.annotator import model as annmodel
 
     def g():
         a = A(None)
@@ -1815,7 +1813,7 @@
     r_f = rt.getrepr(s_f)
 
     s_R = a.bookkeeper.immutablevalue(r_f)
-    s_ll_f = annmodel.lltype_to_annotation(r_f.lowleveltype)
+    s_ll_f = lltype_to_annotation(r_f.lowleveltype)
     A_repr = rclass.getinstancerepr(rt, a.bookkeeper.getdesc(A).
                                     getuniqueclassdef())
     ll_h_graph = annlowlevel.annotate_lowlevel_helper(
@@ -1842,7 +1840,6 @@
 
     from rpython.annotator import annrpython
     a = annrpython.RPythonAnnotator()
-    from rpython.annotator import model as annmodel
 
     def g():
         a = A(None)
@@ -1872,7 +1869,7 @@
     r_f = rt.getrepr(s_f)
 
     s_R = a.bookkeeper.immutablevalue(r_f)
-    s_ll_f = annmodel.lltype_to_annotation(r_f.lowleveltype)
+    s_ll_f = lltype_to_annotation(r_f.lowleveltype)
     A_repr = rclass.getinstancerepr(rt, a.bookkeeper.getdesc(A).
                                     getuniqueclassdef())
     ll_h_graph = annlowlevel.annotate_lowlevel_helper(
@@ -1903,7 +1900,6 @@
 
     from rpython.annotator import annrpython
     a = annrpython.RPythonAnnotator()
-    from rpython.annotator import model as annmodel
 
     i = Impl()
 
@@ -1928,7 +1924,7 @@
     r_f = rt.getrepr(s_f)
 
     s_R = a.bookkeeper.immutablevalue(r_f)
-    s_ll_f = annmodel.lltype_to_annotation(r_f.lowleveltype)
+    s_ll_f = lltype_to_annotation(r_f.lowleveltype)
 
     A_repr = rclass.getinstancerepr(rt, a.bookkeeper.getdesc(A).
                                     getuniqueclassdef())
diff --git a/rpython/translator/exceptiontransform.py 
b/rpython/translator/exceptiontransform.py
--- a/rpython/translator/exceptiontransform.py
+++ b/rpython/translator/exceptiontransform.py
@@ -12,7 +12,7 @@
 from rpython.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
 from rpython.rlib.rarithmetic import r_singlefloat, r_longfloat
 from rpython.rlib.debug import ll_assert
-from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator
 from rpython.tool.sourcetools import func_with_new_name
 
@@ -164,7 +164,7 @@
         return fn
 
     def build_func(self, name, fn, inputtypes, rettype, **kwds):
-        l2a = annmodel.lltype_to_annotation
+        l2a = lltype_to_annotation
         graph = self.mixlevelannotator.getgraph(fn, map(l2a, inputtypes), 
l2a(rettype))
         return self.constant_func(name, inputtypes, rettype, graph,
                                   exception_policy="exc_helper", **kwds)
diff --git a/rpython/translator/sandbox/rsandbox.py 
b/rpython/translator/sandbox/rsandbox.py
--- a/rpython/translator/sandbox/rsandbox.py
+++ b/rpython/translator/sandbox/rsandbox.py
@@ -14,7 +14,7 @@
 #
 
 from rpython.rtyper.lltypesystem import lltype, rffi
-from rpython.annotator import model as annmodel
+from rpython.rtyper.llannotation import lltype_to_annotation
 from rpython.tool.sourcetools import func_with_new_name
 from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator
 from rpython.tool.ansi_print import ansi_log
@@ -126,8 +126,8 @@
         # pure external function - fall back to the annotations
         # corresponding to the ll types
         FUNCTYPE = lltype.typeOf(fnobj)
-        args_s = [annmodel.lltype_to_annotation(ARG) for ARG in FUNCTYPE.ARGS]
-        s_result = annmodel.lltype_to_annotation(FUNCTYPE.RESULT)
+        args_s = [lltype_to_annotation(ARG) for ARG in FUNCTYPE.ARGS]
+        s_result = lltype_to_annotation(FUNCTYPE.RESULT)
 
     try:
         if force_stub:   # old case - don't try to support suggested_primitive
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to