Ejemplo n.º 1
0
 def gettype(self, T, varlength=None, who_asks=None, argnames=[]):
     if isinstance(T, Primitive) or T == GCREF:
         return PrimitiveType[T]
     elif isinstance(T, Typedef):
         return '%s @' % T.c_name
     elif isinstance(T, Ptr):
         if (isinstance(T.TO, OpaqueType) and
             T.TO.hints.get('c_pointer_typedef') is not None):
             return '%s @' % T.TO.hints['c_pointer_typedef']
         try:
             node = self.gettypedefnode(T.TO)
         except NoCorrespondingNode:
             pass
         else:
             if hasattr(node, 'getptrtype'):
                 return node.getptrtype()   # special-casing because of C
         typename = self.gettype(T.TO)   # who_asks not propagated
         return typename.replace('@', '*@')
     elif isinstance(T, (Struct, Array, _WeakRefType)):
         node = self.gettypedefnode(T, varlength=varlength)
         if who_asks is not None:
             who_asks.dependencies.add(node)
         return node.gettype()
     elif isinstance(T, FuncType):
         resulttype = self.gettype(T.RESULT)
         argtypes = []
         for i in range(len(T.ARGS)):
             if T.ARGS[i] is not Void:
                 argtype = self.gettype(T.ARGS[i])
                 try:
                     argname = argnames[i]
                 except IndexError:
                     argname = ''
                 argtypes.append(cdecl(argtype, argname))
         argtypes = ', '.join(argtypes) or 'void'
         return resulttype.replace('@', '(@)(%s)' % argtypes)
     elif isinstance(T, OpaqueType):
         if T == RuntimeTypeInfo:
             return  self.gcpolicy.rtti_type()
         elif T.hints.get("render_structure", False):
             node = self.gettypedefnode(T, varlength=varlength)
             if who_asks is not None:
                 who_asks.dependencies.add(node)
             return 'struct %s @' % node.name
         elif T.hints.get('external', None) == 'C':
             return '%s @' % T.hints['c_name']
         else:
             #raise Exception("don't know about opaque type %r" % (T,))
             return 'struct %s @' % (
                 valid_identifier('pypy_opaque_' + T.tag),)
     elif isinstance(T, llgroup.GroupType):
         return "/*don't use me*/ void @"
     else:
         raise Exception("don't know about type %r" % (T,))
Ejemplo n.º 2
0
 def gettype(self, T, varlength=None, who_asks=None, argnames=[]):
     if isinstance(T, Primitive) or T == GCREF:
         return PrimitiveType[T]
     elif isinstance(T, Typedef):
         return '%s @' % T.c_name
     elif isinstance(T, Ptr):
         if (isinstance(T.TO, OpaqueType)
                 and T.TO.hints.get('c_pointer_typedef') is not None):
             return '%s @' % T.TO.hints['c_pointer_typedef']
         try:
             node = self.gettypedefnode(T.TO)
         except NoCorrespondingNode:
             pass
         else:
             if hasattr(node, 'getptrtype'):
                 return node.getptrtype()  # special-casing because of C
         typename = self.gettype(T.TO)  # who_asks not propagated
         return typename.replace('@', '*@')
     elif isinstance(T, (Struct, Array, _WeakRefType)):
         node = self.gettypedefnode(T, varlength=varlength)
         if who_asks is not None:
             who_asks.dependencies.add(node)
         return node.gettype()
     elif isinstance(T, FuncType):
         resulttype = self.gettype(T.RESULT)
         argtypes = []
         for i in range(len(T.ARGS)):
             if T.ARGS[i] is not Void:
                 argtype = self.gettype(T.ARGS[i])
                 try:
                     argname = argnames[i]
                 except IndexError:
                     argname = ''
                 argtypes.append(cdecl(argtype, argname))
         argtypes = ', '.join(argtypes) or 'void'
         return resulttype.replace('@', '(@)(%s)' % argtypes)
     elif isinstance(T, OpaqueType):
         if T == RuntimeTypeInfo:
             return self.gcpolicy.rtti_type()
         elif T.hints.get("render_structure", False):
             node = self.gettypedefnode(T, varlength=varlength)
             if who_asks is not None:
                 who_asks.dependencies.add(node)
             return 'struct %s @' % node.name
         elif T.hints.get('external', None) == 'C':
             return '%s @' % T.hints['c_name']
         else:
             #raise Exception("don't know about opaque type %r" % (T,))
             return 'struct %s @' % (valid_identifier('pypy_opaque_' +
                                                      T.tag), )
     elif isinstance(T, llgroup.GroupType):
         return "/*don't use me*/ void @"
     else:
         raise Exception("don't know about type %r" % (T, ))
Ejemplo n.º 3
0
def create_instantiate_function(annotator, classdef):
    # build the graph of a function that looks like
    #
    # def my_instantiate():
    #     return instantiate(cls)
    #
    if hasattr(classdef, 'my_instantiate_graph'):
        return
    v = Variable()
    block = Block([])
    block.operations.append(SpaceOperation('instantiate1', [], v))
    name = valid_identifier('instantiate_' + classdef.name)
    graph = FunctionGraph(name, block)
    block.closeblock(Link([v], graph.returnblock))
    annotator.setbinding(v, annmodel.SomeInstance(classdef))
    annotator.annotated[block] = graph
    # force the result to be converted to a generic OBJECTPTR
    generalizedresult = annmodel.SomeInstance(classdef=None)
    annotator.setbinding(graph.getreturnvar(), generalizedresult)
    classdef.my_instantiate_graph = graph
    annotator.translator.graphs.append(graph)
Ejemplo n.º 4
0
def create_instantiate_function(annotator, classdef):
    # build the graph of a function that looks like
    #
    # def my_instantiate():
    #     return instantiate(cls)
    #
    if hasattr(classdef, 'my_instantiate_graph'):
        return
    v = Variable()
    block = Block([])
    block.operations.append(SpaceOperation('instantiate1', [], v))
    name = valid_identifier('instantiate_' + classdef.name)
    graph = FunctionGraph(name, block)
    block.closeblock(Link([v], graph.returnblock))
    annotator.setbinding(v, annmodel.SomeInstance(classdef))
    annotator.annotated[block] = graph
    # force the result to be converted to a generic OBJECTPTR
    generalizedresult = annmodel.SomeInstance(classdef=None)
    annotator.setbinding(graph.getreturnvar(), generalizedresult)
    classdef.my_instantiate_graph = graph
    annotator.translator.graphs.append(graph)
Ejemplo n.º 5
0
    def cachedgraph(self, key, alt_name=None, builder=None):
        try:
            return self._cache[key]
        except KeyError:
            def nameof(thing):
                if isinstance(thing, str):
                    return thing
                elif hasattr(thing, '__name__'):  # mostly types and functions
                    return thing.__name__
                elif hasattr(thing, 'name') and isinstance(thing.name, str):
                    return thing.name            # mostly ClassDescs
                elif isinstance(thing, tuple):
                    return '_'.join(map(nameof, thing))
                else:
                    return str(thing)[:30]

            if key is not None and alt_name is None:
                postfix = valid_identifier(nameof(key))
                alt_name = "%s__%s" % (self.name, postfix)
            graph = self.buildgraph(alt_name, builder)
            self._cache[key] = graph
            return graph
Ejemplo n.º 6
0
    def cachedgraph(self, key, alt_name=None, builder=None):
        try:
            return self._cache[key]
        except KeyError:
            def nameof(thing):
                if isinstance(thing, str):
                    return thing
                elif hasattr(thing, '__name__'):  # mostly types and functions
                    return thing.__name__
                elif hasattr(thing, 'name') and isinstance(thing.name, str):
                    return thing.name            # mostly ClassDescs
                elif isinstance(thing, tuple):
                    return '_'.join(map(nameof, thing))
                else:
                    return str(thing)[:30]

            if key is not None and alt_name is None:
                postfix = valid_identifier(nameof(key))
                alt_name = "%s__%s" % (self.name, postfix)
            graph = self.buildgraph(alt_name, builder)
            self._cache[key] = graph
            return graph
Ejemplo n.º 7
0
 def specialize__genconst(pol, funcdesc, args_s, i):
     # XXX this is specific to the JIT
     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))
Ejemplo n.º 8
0
 def specialize__arglltype(pol, funcdesc, args_s, i):
     key = pol.rtyper.getrepr(args_s[i]).lowleveltype
     alt_name = funcdesc.name+"__for_%sLlT" % key._short_name()
     return funcdesc.cachedgraph(key, alt_name=valid_identifier(alt_name))
Ejemplo n.º 9
0
 def specialize__genconst(self, funcdesc, args_s, i):
     # XXX this is specific to the JIT
     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))
Ejemplo n.º 10
0
 def specialize__arglltype(self, funcdesc, args_s, i):
     key = self.rtyper.getrepr(args_s[i]).lowleveltype
     alt_name = funcdesc.name + "__for_%sLlT" % key._short_name()
     return funcdesc.cachedgraph(key, alt_name=valid_identifier(alt_name))