1. Pypy
  2. Untitled project
  3. pypy

Commits

Armin Rigo  committed 98d5562 Merge

hg merge out-of-line-guards-2

  • Participants
  • Parent commits 9ccaeaf, 20aac7a
  • Branches default

Comments (0)

Files changed (79)

File pypy/annotation/description.py

View file
  • Ignore whitespace
 from pypy.interpreter.pycode import cpython_code_signature
 from pypy.interpreter.argument import rawshape
 from pypy.interpreter.argument import ArgErr
-from pypy.interpreter.function import Defaults
 from pypy.tool.sourcetools import valid_identifier
 from pypy.tool.pairtype import extendabletype
 
             for x in defaults:
                 defs_s.append(self.bookkeeper.immutablevalue(x))
         try:
-            inputcells = args.match_signature(signature, Defaults(defs_s))
+            inputcells = args.match_signature(signature, defs_s)
         except ArgErr, e:
             raise TypeError, "signature mismatch: %s" % e.getmsg(self.name)
         return inputcells
         return None
 
     def maybe_return_immutable_list(self, attr, s_result):
-        # hack: 'x.lst' where lst is listed in _immutable_fields_ as 'lst[*]'
+        # hack: 'x.lst' where lst is listed in _immutable_fields_ as
+        # either 'lst[*]' or 'lst?[*]'
         # should really return an immutable list as a result.  Implemented
         # by changing the result's annotation (but not, of course, doing an
         # actual copy in the rtyper).  Tested in pypy.rpython.test.test_rlist,
         # test_immutable_list_out_of_instance.
-        search = '%s[*]' % (attr,)
+        search1 = '%s[*]' % (attr,)
+        search2 = '%s?[*]' % (attr,)
         cdesc = self
         while cdesc is not None:
             if '_immutable_fields_' in cdesc.classdict:
-                if search in cdesc.classdict['_immutable_fields_'].value:
+                if (search1 in cdesc.classdict['_immutable_fields_'].value or
+                    search2 in cdesc.classdict['_immutable_fields_'].value):
                     s_result.listdef.never_resize()
                     s_copy = s_result.listdef.offspring()
                     s_copy.listdef.mark_as_immutable()

File pypy/annotation/test/test_annrpython.py

View file
  • Ignore whitespace
         s = a.build_types(f, [int])
         assert s.listdef.listitem.immutable
 
+    def test_return_immutable_list_quasiimmut_field(self):
+        class A:
+            _immutable_fields_ = 'lst?[*]'
+        def f(n):
+            a = A()
+            l1 = [n, 0]
+            l1[1] = n+1
+            a.lst = l1
+            return a.lst
+
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [int])
+        assert s.listdef.listitem.immutable
+
     def test_immutable_list_is_actually_resized(self):
         class A:
             _immutable_fields_ = 'lst[*]'

File pypy/interpreter/argument.py

View file
  • Ignore whitespace
 
     ###  Parsing for function calls  ###
 
-    def _match_signature(self, w_firstarg, scope_w, signature, defaults=None,
+    def _match_signature(self, w_firstarg, scope_w, signature, defaults_w=None,
                          blindargs=0):
         """Parse args and kwargs according to the signature of a code object,
         or raise an ArgErr in case of failure.
         """
         if jit.we_are_jitted() and self._dont_jit:
             return self._match_signature_jit_opaque(w_firstarg, scope_w,
-                                                    signature, defaults,
+                                                    signature, defaults_w,
                                                     blindargs)
         return self._really_match_signature(w_firstarg, scope_w, signature,
-                                            defaults, blindargs)
+                                            defaults_w, blindargs)
 
     @jit.dont_look_inside
     def _match_signature_jit_opaque(self, w_firstarg, scope_w, signature,
-                                    defaults, blindargs):
+                                    defaults_w, blindargs):
         return self._really_match_signature(w_firstarg, scope_w, signature,
-                                            defaults, blindargs)
+                                            defaults_w, blindargs)
 
     @jit.unroll_safe
-    def _really_match_signature(self, w_firstarg, scope_w, signature, defaults=None,
-                                blindargs=0):
+    def _really_match_signature(self, w_firstarg, scope_w, signature,
+                                defaults_w=None, blindargs=0):
         #
         #   args_w = list of the normal actual parameters, wrapped
         #   kwds_w = real dictionary {'keyword': wrapped parameter}
         elif avail > co_argcount:
             raise ArgErrCount(avail, num_kwds,
                               co_argcount, has_vararg, has_kwarg,
-                              defaults, 0)
+                              defaults_w, 0)
 
         # the code assumes that keywords can potentially be large, but that
         # argnames is typically not too large
                     num_remainingkwds -= 1
         missing = 0
         if input_argcount < co_argcount:
-            def_first = co_argcount - (0 if defaults is None else defaults.getlen())
+            def_first = co_argcount - (0 if defaults_w is None else len(defaults_w))
             for i in range(input_argcount, co_argcount):
                 if scope_w[i] is not None:
-                    pass
-                elif i >= def_first:
-                    scope_w[i] = defaults.getitem(i - def_first)
+                    continue
+                defnum = i - def_first
+                if defnum >= 0:
+                    scope_w[i] = defaults_w[defnum]
                 else:
                     # error: not enough arguments.  Don't signal it immediately
                     # because it might be related to a problem with */** or
             if co_argcount == 0:
                 raise ArgErrCount(avail, num_kwds,
                               co_argcount, has_vararg, has_kwarg,
-                              defaults, missing)
+                              defaults_w, missing)
             raise ArgErrUnknownKwds(num_remainingkwds, keywords, used_keywords)
 
         if missing:
             raise ArgErrCount(avail, num_kwds,
                               co_argcount, has_vararg, has_kwarg,
-                              defaults, missing)
+                              defaults_w, missing)
 
         return co_argcount + has_vararg + has_kwarg
 
 
 
     def parse_into_scope(self, w_firstarg,
-                         scope_w, fnname, signature, defaults=None):
+                         scope_w, fnname, signature, defaults_w=None):
         """Parse args and kwargs to initialize a frame
         according to the signature of code object.
         Store the argumentvalues into scope_w.
         """
         try:
             return self._match_signature(w_firstarg,
-                                         scope_w, signature, defaults, 0)
+                                         scope_w, signature, defaults_w, 0)
         except ArgErr, e:
             raise OperationError(self.space.w_TypeError,
                                  self.space.wrap(e.getmsg(fnname)))
 
-    def _parse(self, w_firstarg, signature, defaults, blindargs=0):
+    def _parse(self, w_firstarg, signature, defaults_w, blindargs=0):
         """Parse args and kwargs according to the signature of a code object,
         or raise an ArgErr in case of failure.
         """
         scopelen = signature.scope_length()
         scope_w = [None] * scopelen
-        self._match_signature(w_firstarg, scope_w, signature, defaults,
+        self._match_signature(w_firstarg, scope_w, signature, defaults_w,
                               blindargs)
         return scope_w
 
 
     def parse_obj(self, w_firstarg,
-                  fnname, signature, defaults=None, blindargs=0):
+                  fnname, signature, defaults_w=None, blindargs=0):
         """Parse args and kwargs to initialize a frame
         according to the signature of code object.
         """
         try:
-            return self._parse(w_firstarg, signature, defaults, blindargs)
+            return self._parse(w_firstarg, signature, defaults_w, blindargs)
         except ArgErr, e:
             raise OperationError(self.space.w_TypeError,
                                  self.space.wrap(e.getmsg(fnname)))
 
 
 
-    def _match_signature(self, w_firstarg, scope_w, signature, defaults=None,
+    def _match_signature(self, w_firstarg, scope_w, signature, defaults_w=None,
                          blindargs=0):
         self.combine_if_necessary()
         # _match_signature is destructive
         return Arguments._match_signature(
                self, w_firstarg, scope_w, signature,
-               defaults, blindargs)
+               defaults_w, blindargs)
 
     def unpack(self):
         self.combine_if_necessary()
         return Arguments.unpack(self)
 
-    def match_signature(self, signature, defaults):
+    def match_signature(self, signature, defaults_w):
         """Parse args and kwargs according to the signature of a code object,
         or raise an ArgErr in case of failure.
         """
-        return self._parse(None, signature, defaults)
+        return self._parse(None, signature, defaults_w)
 
     def unmatch_signature(self, signature, data_w):
         """kind of inverse of match_signature"""
 class ArgErrCount(ArgErr):
 
     def __init__(self, got_nargs, nkwds, expected_nargs, has_vararg, has_kwarg,
-                 defaults, missing_args):
+                 defaults_w, missing_args):
         self.expected_nargs = expected_nargs
         self.has_vararg = has_vararg
         self.has_kwarg = has_kwarg
 
-        self.num_defaults = 0 if defaults is None else defaults.getlen()
+        self.num_defaults = 0 if defaults_w is None else len(defaults_w)
         self.missing_args = missing_args
         self.num_args = got_nargs
         self.num_kwds = nkwds

File pypy/interpreter/function.py

View file
  • Ignore whitespace
     assert not func.can_change_code
     return func.code
 
-class Defaults(object):
-    _immutable_fields_ = ["items[*]", "promote"]
-
-    def __init__(self, items, promote=False):
-        self.items = items
-        self.promote = promote
-
-    def getitems(self):
-        # an idea - we want to promote only items that we know won't change
-        # too often. this is the case for builtin functions and functions
-        # with known constant defaults. Otherwise we don't want to promote
-        # this so lambda a=a won't create a new trace each time it's
-        # encountered
-        if self.promote:
-            return jit.hint(self, promote=True).items
-        return self.items
-
-    def getitem(self, idx):
-        return self.getitems()[idx]
-
-    def getlen(self):
-        return len(self.getitems())
 
 class Function(Wrappable):
     """A function is a code object captured with some environment:
     and an arbitrary 'closure' passed to the code object."""
 
     can_change_code = True
+    _immutable_fields_ = ['code?',
+                          'w_func_globals?',
+                          'closure?',
+                          'defs_w?[*]']
 
     def __init__(self, space, code, w_globals=None, defs_w=[], closure=None,
-                 forcename=None, promote_defs=False):
+                 forcename=None):
         self.space = space
         self.name = forcename or code.co_name
         self.w_doc = None   # lazily read from code.getdocstring()
         self.code = code       # Code instance
         self.w_func_globals = w_globals  # the globals dictionary
         self.closure   = closure    # normally, list of Cell instances or None
-        self.defs = Defaults(defs_w, promote=promote_defs)
-        # wrapper around list of w_default's
+        self.defs_w = defs_w
         self.w_func_dict = None # filled out below if needed
         self.w_module = None
 
             return self._flat_pycall(code, nargs, frame)
         elif fast_natural_arity & Code.FLATPYCALL:
             natural_arity = fast_natural_arity & 0xff
-            if natural_arity > nargs >= natural_arity - self.defs.getlen():
+            if natural_arity > nargs >= natural_arity - len(self.defs_w):
                 assert isinstance(code, PyCode)
                 return self._flat_pycall_defaults(code, nargs, frame,
                                                   natural_arity - nargs)
             w_arg = frame.peekvalue(nargs-1-i)
             new_frame.fastlocals_w[i] = w_arg
 
-        defs = self.defs
-        ndefs = defs.getlen()
+        ndefs = len(self.defs_w)
         start = ndefs - defs_to_load
         i = nargs
         for j in xrange(start, ndefs):
-            new_frame.fastlocals_w[i] = defs.getitem(j)
+            new_frame.fastlocals_w[i] = self.defs_w[j]
             i += 1
         return new_frame.run()
 
             w(self.code),
             w_func_globals,
             w_closure,
-            nt(self.defs.getitems()),
+            nt(self.defs_w),
             w_func_dict,
             self.w_module,
         ]
         if space.is_w(w_func_dict, space.w_None):
             w_func_dict = None
         self.w_func_dict = w_func_dict
-        self.defs = Defaults(space.fixedview(w_defs))
+        self.defs_w = space.fixedview(w_defs)
         self.w_module = w_module
 
     def fget_func_defaults(self, space):
-        values_w = self.defs.getitems()
+        values_w = self.defs_w
         # the `None in values_w` check here is to ensure that interp-level
         # functions with a default of NoneNotWrapped do not get their defaults
         # exposed at applevel
 
     def fset_func_defaults(self, space, w_defaults):
         if space.is_w(w_defaults, space.w_None):
-            self.defs = Defaults([])
+            self.defs_w = []
             return
         if not space.is_true(space.isinstance(w_defaults, space.w_tuple)):
             raise OperationError( space.w_TypeError, space.wrap("func_defaults must be set to a tuple object or None") )
-        self.defs = Defaults(space.fixedview(w_defaults))
+        self.defs_w = space.fixedview(w_defaults)
 
     def fdel_func_defaults(self, space):
-        self.defs = Defaults([])
+        self.defs_w = []
 
     def fget_func_doc(self, space):
         if self.w_doc is None:
 
 class Method(Wrappable):
     """A method is a function bound to a specific instance or class."""
+    _immutable_fields_ = ['w_function', 'w_instance', 'w_class']
 
     def __init__(self, space, w_function, w_instance, w_class):
         self.space = space
     def __init__(self, func):
         assert isinstance(func, Function)
         Function.__init__(self, func.space, func.code, func.w_func_globals,
-                          func.defs.getitems(), func.closure, func.name,
-                          promote_defs=True)
+                          func.defs_w, func.closure, func.name)
         self.w_doc = func.w_doc
         self.w_func_dict = func.w_func_dict
         self.w_module = func.w_module

File pypy/interpreter/gateway.py

View file
  • Ignore whitespace
         space = func.space
         activation = self.activation
         scope_w = args.parse_obj(w_obj, func.name, self.sig,
-                                 func.defs, self.minargs)
+                                 func.defs_w, self.minargs)
         try:
             w_result = activation._run(space, scope_w)
         except DescrMismatch:

File pypy/interpreter/generator.py

View file
  • Ignore whitespace
 
 class GeneratorIterator(Wrappable):
     "An iterator created by a generator."
+    _immutable_fields_ = ['pycode']
     
     def __init__(self, frame):
         self.space = frame.space

File pypy/interpreter/pycode.py

View file
  • Ignore whitespace
                                       fresh_virtualizable=True)
         args_matched = args.parse_into_scope(None, fresh_frame.fastlocals_w,
                                              func.name,
-                                             sig, func.defs)
+                                             sig, func.defs_w)
         fresh_frame.init_cells()
         return frame.run()
 
                                       fresh_virtualizable=True)
         args_matched = args.parse_into_scope(w_obj, fresh_frame.fastlocals_w,
                                              func.name,
-                                             sig, func.defs)
+                                             sig, func.defs_w)
         fresh_frame.init_cells()
         return frame.run()
 

File pypy/interpreter/pyopcode.py

View file
  • Ignore whitespace
                 WHY_CONTINUE,   SContinueLoop
                 WHY_YIELD       not needed
     """
+    _immutable_ = True
     def nomoreblocks(self):
         raise BytecodeCorruption("misplaced bytecode - should not return")
 
 class SReturnValue(SuspendedUnroller):
     """Signals a 'return' statement.
     Argument is the wrapped object to return."""
+    _immutable_ = True
     kind = 0x01
     def __init__(self, w_returnvalue):
         self.w_returnvalue = w_returnvalue
 class SApplicationException(SuspendedUnroller):
     """Signals an application-level exception
     (i.e. an OperationException)."""
+    _immutable_ = True
     kind = 0x02
     def __init__(self, operr):
         self.operr = operr
 
 class SBreakLoop(SuspendedUnroller):
     """Signals a 'break' statement."""
+    _immutable_ = True
     kind = 0x04
 
     def state_unpack_variables(self, space):
 class SContinueLoop(SuspendedUnroller):
     """Signals a 'continue' statement.
     Argument is the bytecode position of the beginning of the loop."""
+    _immutable_ = True
     kind = 0x08
     def __init__(self, jump_to):
         self.jump_to = jump_to
 
 
 class FrameBlock(object):
-
     """Abstract base class for frame blocks from the blockstack,
     used by the SETUP_XXX and POP_BLOCK opcodes."""
 
+    _immutable_ = True
+
     def __init__(self, frame, handlerposition):
         self.handlerposition = handlerposition
         self.valuestackdepth = frame.valuestackdepth
 class LoopBlock(FrameBlock):
     """A loop block.  Stores the end-of-loop pointer in case of 'break'."""
 
+    _immutable_ = True
     _opname = 'SETUP_LOOP'
     handling_mask = SBreakLoop.kind | SContinueLoop.kind
 
 class ExceptBlock(FrameBlock):
     """An try:except: block.  Stores the position of the exception handler."""
 
+    _immutable_ = True
     _opname = 'SETUP_EXCEPT'
     handling_mask = SApplicationException.kind
 
 class FinallyBlock(FrameBlock):
     """A try:finally: block.  Stores the position of the exception handler."""
 
+    _immutable_ = True
     _opname = 'SETUP_FINALLY'
     handling_mask = -1     # handles every kind of SuspendedUnroller
 
 
 class WithBlock(FinallyBlock):
 
+    _immutable_ = True
+
     def really_handle(self, frame, unroller):
         if (frame.space.full_exceptions and
             isinstance(unroller, SApplicationException)):

File pypy/interpreter/special.py

  • Ignore whitespace
File contents unchanged.

File pypy/interpreter/test/test_argument.py

View file
  • Ignore whitespace
     ArgErr, ArgErrUnknownKwds, ArgErrMultipleValues, ArgErrCount, rawshape,
     Signature)
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.function import Defaults
 
 
 class TestSignature(object):
         py.test.raises(ArgErr, args._match_signature, None, l, Signature(["a"], "*"))
         args = Arguments(space, [])
         l = [None]
-        args._match_signature(None, l, Signature(["a"]), defaults=Defaults([1]))
+        args._match_signature(None, l, Signature(["a"]), defaults_w=[1])
         assert l == [1]
         args = Arguments(space, [])
         l = [None]
                 py.test.raises(ArgErr, args._match_signature, firstarg, l, Signature(["a", "b", "c", "d", "e"], "*"))
                 l = [None, None, None, None, None]
                 args = Arguments(space, arglist, w_stararg=starargs)
-                args._match_signature(firstarg, l, Signature(["a", "b", "c", "d", "e"]), defaults=Defaults([1]))
+                args._match_signature(firstarg, l, Signature(["a", "b", "c", "d", "e"]), defaults_w=[1])
                 assert l == [4, 5, 6, 7, 1]
                 for j in range(len(values)):
                     l = [None] * (j + 1)
             assert len(keywords) == len(keywords_w)
             args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds)
             l = [None, None, None]
-            args._match_signature(None, l, Signature(["a", "b", "c"]), defaults=Defaults([4]))
+            args._match_signature(None, l, Signature(["a", "b", "c"]), defaults_w=[4])
             assert l == [1, 2, 3]
             args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds)
             l = [None, None, None, None]
-            args._match_signature(None, l, Signature(["a", "b", "b1", "c"]), defaults=Defaults([4, 5]))
+            args._match_signature(None, l, Signature(["a", "b", "b1", "c"]), defaults_w=[4, 5])
             assert l == [1, 2, 4, 3]
             args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds)
             l = [None, None, None, None]
-            args._match_signature(None, l, Signature(["a", "b", "c", "d"]), defaults=Defaults([4, 5]))
+            args._match_signature(None, l, Signature(["a", "b", "c", "d"]), defaults_w=[4, 5])
             assert l == [1, 2, 3, 5]
             args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds)
             l = [None, None, None, None]
             py.test.raises(ArgErr, args._match_signature, None, l,
-                           Signature(["c", "b", "a", "d"]), defaults=Defaults([4, 5]))
+                           Signature(["c", "b", "a", "d"]), defaults_w=[4, 5])
             args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds)
             l = [None, None, None, None]
             py.test.raises(ArgErr, args._match_signature, None, l,
-                           Signature(["a", "b", "c1", "d"]), defaults=Defaults([4, 5]))
+                           Signature(["a", "b", "c1", "d"]), defaults_w=[4, 5])
             args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds)
             l = [None, None, None]
             args._match_signature(None, l, Signature(["a", "b"], None, "**"))
         calls = []
 
         def _match_signature(w_firstarg, scope_w, signature,
-                             defaults=None, blindargs=0):
-            defaults = [] if defaults is None else defaults.getitems()
+                             defaults_w=None, blindargs=0):
+            defaults_w = [] if defaults_w is None else defaults_w
             calls.append((w_firstarg, scope_w, signature.argnames, signature.has_vararg(),
-                          signature.has_kwarg(), defaults, blindargs))
+                          signature.has_kwarg(), defaults_w, blindargs))
         args._match_signature = _match_signature
 
         scope_w = args.parse_obj(None, "foo", Signature(["a", "b"], None, None))
         calls = []
 
         scope_w = args.parse_obj(None, "foo", Signature(["a", "b"], "args", "kw"),
-                             defaults=Defaults(['x', 'y']))
+                             defaults_w=['x', 'y'])
         assert len(calls) == 1
         assert calls[0] == (None, [None, None, None, None], ["a", "b"],
                             True, True,
         calls = []
 
         scope_w = args.parse_obj("obj", "foo", Signature(["a", "b"], "args", "kw"),
-                             defaults=Defaults(['x', 'y']), blindargs=1)
+                             defaults_w=['x', 'y'], blindargs=1)
         assert len(calls) == 1
         assert calls[0] == ("obj", [None, None, None, None], ["a", "b"],
                             True, True,
         calls = []
 
         def _match_signature(w_firstarg, scope_w, signature,
-                             defaults=None, blindargs=0):
-            defaults = [] if defaults is None else defaults.getitems()
+                             defaults_w=None, blindargs=0):
+            defaults_w = [] if defaults_w is None else defaults_w
             calls.append((w_firstarg, scope_w, signature.argnames, signature.has_vararg(),
-                          signature.has_kwarg(), defaults, blindargs))
+                          signature.has_kwarg(), defaults_w, blindargs))
         args._match_signature = _match_signature
 
         scope_w = [None, None]
 
         scope_w = [None, None, None, None]
         args.parse_into_scope(None, scope_w, "foo", Signature(["a", "b"], "args", "kw"),
-                              defaults=Defaults(['x', 'y']))
+                              defaults_w=['x', 'y'])
         assert len(calls) == 1
         assert calls[0] == (None, scope_w, ["a", "b"],
                             True, True,
         scope_w = [None, None, None, None]
         args.parse_into_scope("obj", scope_w, "foo", Signature(["a", "b"],
                                                       "args", "kw"),
-                              defaults=Defaults(['x', 'y']))
+                              defaults_w=['x', 'y'])
         assert len(calls) == 1
         assert calls[0] == ("obj", scope_w, ["a", "b"],
                             True, True,
     def test_missing_args(self):
         # got_nargs, nkwds, expected_nargs, has_vararg, has_kwarg,
         # defaults_w, missing_args
-        err = ArgErrCount(1, 0, 0, False, False, Defaults([]), 0)
+        err = ArgErrCount(1, 0, 0, False, False, None, 0)
         s = err.getmsg('foo')
         assert s == "foo() takes no argument (1 given)"
-        err = ArgErrCount(0, 0, 1, False, False, Defaults([]), 1)
+        err = ArgErrCount(0, 0, 1, False, False, [], 1)
         s = err.getmsg('foo')
         assert s == "foo() takes exactly 1 argument (0 given)"
-        err = ArgErrCount(3, 0, 2, False, False, Defaults([]), 0)
+        err = ArgErrCount(3, 0, 2, False, False, [], 0)
         s = err.getmsg('foo')
         assert s == "foo() takes exactly 2 arguments (3 given)"
-        err = ArgErrCount(1, 0, 2, True, False, Defaults([]), 1)
+        err = ArgErrCount(1, 0, 2, True, False, [], 1)
         s = err.getmsg('foo')
         assert s == "foo() takes at least 2 arguments (1 given)"
-        err = ArgErrCount(3, 0, 2, True, False, Defaults(['a']), 0)
+        err = ArgErrCount(3, 0, 2, True, False, ['a'], 0)
         s = err.getmsg('foo')
         assert s == "foo() takes at most 2 arguments (3 given)"
-        err = ArgErrCount(0, 1, 2, True, False, Defaults(['a']), 1)
+        err = ArgErrCount(0, 1, 2, True, False, ['a'], 1)
         s = err.getmsg('foo')
         assert s == "foo() takes at least 1 argument (1 given)"
-        err = ArgErrCount(2, 1, 1, False, True, Defaults([]), 0)
+        err = ArgErrCount(2, 1, 1, False, True, [], 0)
         s = err.getmsg('foo')
         assert s == "foo() takes exactly 1 argument (3 given)"
 
 
         args = make_arguments_for_translation(space, [1])
         sig = Signature(['a', 'b', 'c'], None, None)
-        data = args.match_signature(sig, Defaults([2, 3]))
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
 
         args = make_arguments_for_translation(space, [1], {'c': 3, 'b': 2})
         sig = Signature(['a', 'b', 'c'], None, None)
-        data = args.match_signature(sig, Defaults([]))
+        data = args.match_signature(sig, [])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1], {'c': 5})
         sig = Signature(['a', 'b', 'c'], None, None)
-        data = args.match_signature(sig, Defaults([2, 3]))
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1], {'c': 5, 'd': 7})
         sig = Signature(['a', 'b', 'c'], None, 'kw')
-        data = args.match_signature(sig, Defaults([2, 3]))
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1,2,3,4,5], {'e': 5, 'd': 7})
         sig = Signature(['a', 'b', 'c'], 'r', 'kw')
-        data = args.match_signature(sig, Defaults([2, 3]))
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
                                        w_stararg=[1],
                                        w_starstararg={'c': 5, 'd': 7})
         sig = Signature(['a', 'b', 'c'], None, 'kw')
-        data = args.match_signature(sig, Defaults([2, 3]))
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
                                        w_stararg=[3,4,5],
                                        w_starstararg={'e': 5, 'd': 7})
         sig = Signature(['a', 'b', 'c'], 'r', 'kw')
-        data = args.match_signature(sig, Defaults([2, 3]))
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 

File pypy/interpreter/test/test_gateway.py

View file
  • Ignore whitespace
 from pypy.conftest import gettestobjspace
 from pypy.interpreter import gateway, argument
 from pypy.interpreter.gateway import ObjSpace, W_Root
-from pypy.interpreter.function import Defaults
 import py
 import sys
 
     def __init__(self, space, name):
         self.space = space
         self.name = name
-        self.defs = Defaults([])
+        self.defs_w = []
 
 class TestBuiltinCode:
     def test_signature(self):

File pypy/jit/backend/llgraph/llimpl.py

View file
  • Ignore whitespace
 
 class CompiledLoop(object):
     has_been_freed = False
+    invalid = False
 
     def __init__(self):
         self.inputargs = []
         if forced:
             raise GuardFailed
 
+    def op_guard_not_invalidated(self, descr):
+        if self.loop.invalid:
+            raise GuardFailed
 
 class OOFrame(Frame):
 

File pypy/jit/backend/llgraph/runner.py

View file
  • Ignore whitespace
             raise ValueError("CALL_ASSEMBLER not supported")
         llimpl.redirect_call_assembler(self, oldlooptoken, newlooptoken)
 
+    def invalidate_loop(self, looptoken):
+        for loop in looptoken.compiled_loop_token.loop_and_bridges:
+            loop._obj.externalobj.invalid = True
+
     # ----------
 
     def sizeof(self, S):

File pypy/jit/backend/model.py

View file
  • Ignore whitespace
         oldlooptoken so that from now own they will call newlooptoken."""
         raise NotImplementedError
 
+    def invalidate_loop(self, looptoken):
+        """Activate all GUARD_NOT_INVALIDATED in the loop and its attached
+        bridges.  Before this call, all GUARD_NOT_INVALIDATED do nothing;
+        after this call, they all fail.  Note that afterwards, if one such
+        guard fails often enough, it has a bridge attached to it; it is
+        possible then to re-call invalidate_loop() on the same looptoken,
+        which must invalidate all newer GUARD_NOT_INVALIDATED, but not the
+        old one that already has a bridge attached to it."""
+        raise NotImplementedError
+
     def free_loop_and_bridges(self, compiled_loop_token):
         """This method is called to free resources (machine code,
         references to resume guards, etc.) allocated by the compilation
         # that belong to this loop or to a bridge attached to it.
         # Filled by the frontend calling record_faildescr_index().
         self.faildescr_indices = []
+        self.invalidate_positions = []
         debug_start("jit-mem-looptoken-alloc")
         debug_print("allocating Loop #", self.number)
         debug_stop("jit-mem-looptoken-alloc")

File pypy/jit/backend/test/runner_test.py

View file
  • Ignore whitespace
         assert self.cpu.get_latest_value_int(2) == 10
         assert values == [1, 10]
 
+    def test_guard_not_invalidated(self):
+        cpu = self.cpu
+        i0 = BoxInt()
+        i1 = BoxInt()
+        faildescr = BasicFailDescr(1)
+        ops = [
+            ResOperation(rop.GUARD_NOT_INVALIDATED, [], None, descr=faildescr),
+            ResOperation(rop.FINISH, [i0], None, descr=BasicFailDescr(0))
+        ]
+        ops[0].setfailargs([i1])
+        looptoken = LoopToken()
+        self.cpu.compile_loop([i0, i1], ops, looptoken)
+
+        self.cpu.set_future_value_int(0, -42)
+        self.cpu.set_future_value_int(1, 9)
+        fail = self.cpu.execute_token(looptoken)
+        assert fail.identifier == 0
+        assert self.cpu.get_latest_value_int(0) == -42
+        print 'step 1 ok'
+        print '-'*79
+
+        # mark as failing
+        self.cpu.invalidate_loop(looptoken)
+
+        self.cpu.set_future_value_int(0, -42)
+        self.cpu.set_future_value_int(1, 9)
+        fail = self.cpu.execute_token(looptoken)
+        assert fail is faildescr
+        assert self.cpu.get_latest_value_int(0) == 9
+        print 'step 2 ok'
+        print '-'*79
+
+        # attach a bridge
+        i2 = BoxInt()
+        faildescr2 = BasicFailDescr(2)
+        ops = [
+            ResOperation(rop.GUARD_NOT_INVALIDATED, [],None, descr=faildescr2),
+            ResOperation(rop.FINISH, [i2], None, descr=BasicFailDescr(3))
+        ]
+        ops[0].setfailargs([])
+        self.cpu.compile_bridge(faildescr, [i2], ops, looptoken)
+
+        self.cpu.set_future_value_int(0, -42)
+        self.cpu.set_future_value_int(1, 9)
+        fail = self.cpu.execute_token(looptoken)
+        assert fail.identifier == 3
+        assert self.cpu.get_latest_value_int(0) == 9
+        print 'step 3 ok'
+        print '-'*79
+
+        # mark as failing again
+        self.cpu.invalidate_loop(looptoken)
+
+        self.cpu.set_future_value_int(0, -42)
+        self.cpu.set_future_value_int(1, 9)
+        fail = self.cpu.execute_token(looptoken)
+        assert fail is faildescr2
+        print 'step 4 ok'
+        print '-'*79
+
     # pure do_ / descr features
 
     def test_do_operations(self):

File pypy/jit/backend/x86/assembler.py

View file
  • Ignore whitespace
 
 
 class GuardToken(object):
-    def __init__(self, faildescr, failargs, fail_locs, exc):
+    def __init__(self, faildescr, failargs, fail_locs, exc,
+                 is_guard_not_invalidated):
         self.faildescr = faildescr
         self.failargs = failargs
         self.fail_locs = fail_locs
         self.exc = exc
+        self.is_guard_not_invalidated = is_guard_not_invalidated
 
 DEBUG_COUNTER = lltype.Struct('DEBUG_COUNTER', ('i', lltype.Signed))
 
         # tok.faildescr._x86_adr_jump_offset to contain the raw address of
         # the 4-byte target field in the JMP/Jcond instruction, and patch
         # the field in question to point (initially) to the recovery stub
+        clt = self.current_clt
         for tok in self.pending_guard_tokens:
             addr = rawstart + tok.pos_jump_offset
             tok.faildescr._x86_adr_jump_offset = addr
             relative_target = tok.pos_recovery_stub - (tok.pos_jump_offset + 4)
             assert rx86.fits_in_32bits(relative_target)
             #
-            mc = codebuf.MachineCodeBlockWrapper()
-            mc.writeimm32(relative_target)
-            mc.copy_to_raw_memory(addr)
+            if not tok.is_guard_not_invalidated:
+                mc = codebuf.MachineCodeBlockWrapper()
+                mc.writeimm32(relative_target)
+                mc.copy_to_raw_memory(addr)
+            else:
+                # GUARD_NOT_INVALIDATED, record an entry in
+                # clt.invalidate_positions of the form:
+                #     (addr-in-the-code-of-the-not-yet-written-jump-target,
+                #      relative-target-to-use)
+                relpos = tok.pos_jump_offset
+                clt.invalidate_positions.append((rawstart + relpos,
+                                                 relative_target))
+                # General idea: Although no code was generated by this
+                # guard, the code might be patched with a "JMP rel32" to
+                # the guard recovery code.  This recovery code is
+                # already generated, and looks like the recovery code
+                # for any guard, even if at first it has no jump to it.
+                # So we may later write 5 bytes overriding the existing
+                # instructions; this works because a CALL instruction
+                # would also take at least 5 bytes.  If it could take
+                # less, we would run into the issue that overwriting the
+                # 5 bytes here might get a few nonsense bytes at the
+                # return address of the following CALL.
 
     def get_asmmemmgr_blocks(self, looptoken):
         clt = looptoken.compiled_loop_token
         self.mc.CMP(heap(self.cpu.pos_exception()), imm0)
         self.implement_guard(guard_token, 'NZ')
 
+    def genop_guard_guard_not_invalidated(self, ign_1, guard_op, guard_token,
+                                     locs, ign_2):
+        pos = self.mc.get_relative_pos() + 1 # after potential jmp
+        guard_token.pos_jump_offset = pos
+        self.pending_guard_tokens.append(guard_token)
+
     def genop_guard_guard_exception(self, ign_1, guard_op, guard_token,
                                     locs, resloc):
         loc = locs[0]
         exc = (guard_opnum == rop.GUARD_EXCEPTION or
                guard_opnum == rop.GUARD_NO_EXCEPTION or
                guard_opnum == rop.GUARD_NOT_FORCED)
-        return GuardToken(faildescr, failargs, fail_locs, exc)
+        is_guard_not_invalidated = guard_opnum == rop.GUARD_NOT_INVALIDATED
+        return GuardToken(faildescr, failargs, fail_locs, exc,
+                          is_guard_not_invalidated)
 
     def generate_quick_failure(self, guardtok):
         """Generate the initial code for handling a failure.  We try to

File pypy/jit/backend/x86/regalloc.py

View file
  • Ignore whitespace
     def consider_guard_no_exception(self, op):
         self.perform_guard(op, [], None)
 
+    consider_guard_not_invalidated = consider_guard_no_exception
+
     def consider_guard_exception(self, op):
         loc = self.rm.make_sure_var_in_reg(op.getarg(0))
         box = TempBox()

File pypy/jit/backend/x86/runner.py

View file
  • Ignore whitespace
     def redirect_call_assembler(self, oldlooptoken, newlooptoken):
         self.assembler.redirect_call_assembler(oldlooptoken, newlooptoken)
 
+    def invalidate_loop(self, looptoken):
+        from pypy.jit.backend.x86 import codebuf
+        
+        for addr, tgt in looptoken.compiled_loop_token.invalidate_positions:
+            mc = codebuf.MachineCodeBlockWrapper()
+            mc.JMP_l(tgt)
+            assert mc.get_relative_pos() == 5      # [JMP] [tgt 4 bytes]
+            mc.copy_to_raw_memory(addr - 1)
+        # positions invalidated
+        looptoken.compiled_loop_token.invalidate_positions = []
+
 class CPU386(AbstractX86CPU):
     WORD = 4
     NUM_REGS = 8

File pypy/jit/backend/x86/test/test_quasiimmut.py

View file
  • Ignore whitespace
+
+import py
+from pypy.jit.backend.x86.test.test_basic import Jit386Mixin
+from pypy.jit.metainterp.test import test_quasiimmut
+
+class TestLoopSpec(Jit386Mixin, test_quasiimmut.QuasiImmutTests):
+    # for the individual tests see
+    # ====> ../../../metainterp/test/test_loop.py
+    pass

File pypy/jit/codewriter/call.py

View file
  • Ignore whitespace
 from pypy.jit.codewriter import support
 from pypy.jit.codewriter.jitcode import JitCode
 from pypy.jit.codewriter.effectinfo import VirtualizableAnalyzer
+from pypy.jit.codewriter.effectinfo import QuasiImmutAnalyzer
 from pypy.jit.codewriter.effectinfo import effectinfo_from_writeanalyze
 from pypy.jit.codewriter.effectinfo import EffectInfo, CallInfoCollection
 from pypy.translator.simplify import get_funcobj, get_functype
             self.raise_analyzer = RaiseAnalyzer(translator)
             self.readwrite_analyzer = ReadWriteAnalyzer(translator)
             self.virtualizable_analyzer = VirtualizableAnalyzer(translator)
+            self.quasiimmut_analyzer = QuasiImmutAnalyzer(translator)
         #
         for index, jd in enumerate(jitdrivers_sd):
             jd.index = index
         if extraeffect is None:
             if self.virtualizable_analyzer.analyze(op):
                 extraeffect = EffectInfo.EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE
+            elif self.quasiimmut_analyzer.analyze(op):
+                extraeffect = EffectInfo.EF_CAN_INVALIDATE
             elif loopinvariant:
                 extraeffect = EffectInfo.EF_LOOPINVARIANT
             elif pure:
         if pure or loopinvariant:
             assert effectinfo is not None
             assert extraeffect != EffectInfo.EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE
+            assert extraeffect != EffectInfo.EF_CAN_INVALIDATE
         #
         return self.cpu.calldescrof(FUNC, tuple(NON_VOID_ARGS), RESULT,
                                     effectinfo)

File pypy/jit/codewriter/codewriter.py

View file
  • Ignore whitespace
 
 class CodeWriter(object):
     callcontrol = None    # for tests
+    debug = False
 
-    def __init__(self, cpu=None, jitdrivers_sd=[], debug=False):
+    def __init__(self, cpu=None, jitdrivers_sd=[]):
         self.cpu = cpu
         self.assembler = Assembler()
         self.callcontrol = CallControl(cpu, jitdrivers_sd)
         self._seen_files = set()
-        self.debug = debug
 
     def transform_func_to_jitcode(self, func, values, type_system='lltype'):
         """For testing."""

File pypy/jit/codewriter/effectinfo.py

View file
  • Ignore whitespace
     EF_LOOPINVARIANT                   = 1 #special: call it only once per loop
     EF_CANNOT_RAISE                    = 2 #a function which cannot raise
     EF_CAN_RAISE                       = 3 #normal function (can raise)
-    EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE = 4 #can raise and force virtualizables
+    EF_CAN_INVALIDATE                  = 4 #can force all GUARD_NOT_INVALIDATED
+    EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE = 5 #can raise and force virtualizables
 
     # the 'oopspecindex' field is one of the following values:
     OS_NONE                     = 0    # normal case, no oopspec
         cls._cache[key] = result
         return result
 
+    def check_can_invalidate(self):
+        return self.extraeffect >= self.EF_CAN_INVALIDATE
+
     def check_forces_virtual_or_virtualizable(self):
         return self.extraeffect >= self.EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE
 
         return op.opname in ('jit_force_virtualizable',
                              'jit_force_virtual')
 
+class QuasiImmutAnalyzer(BoolGraphAnalyzer):
+    def analyze_simple_operation(self, op, graphinfo):
+        return op.opname == 'jit_force_quasi_immutable'
+
 # ____________________________________________________________
 
 class CallInfoCollection(object):

File pypy/jit/codewriter/jtransform.py

View file
  • Ignore whitespace
 from pypy.jit.codewriter.effectinfo import EffectInfo
 from pypy.jit.codewriter.policy import log
 from pypy.jit.metainterp.typesystem import deref, arrayItem
+from pypy.jit.metainterp import quasiimmut
+from pypy.rpython.rclass import IR_QUASIIMMUTABLE, IR_QUASIIMMUTABLE_ARRAY
 from pypy.rlib import objectmodel
 from pypy.rlib.jit import _we_are_jitted
 from pypy.translator.simplify import get_funcobj
                     "known non-negative, or catching IndexError, or\n"
                     "not inlining at all (for tests: use listops=True).\n"
                     "Occurred in: %r" % self.graph)
-            # extra expanation: with the way things are organized in
+            # extra explanation: with the way things are organized in
             # rpython/rlist.py, the ll_getitem becomes a function call
             # that is typically meant to be inlined by the JIT, but
             # this does not work with vable arrays because
         # If the resulting op1 is still a direct_call, turn it into a
         # residual_call.
         if isinstance(op1, SpaceOperation) and op1.opname == 'direct_call':
-            op1 = self.handle_residual_call(op1 or op)
+            op1 = self.handle_residual_call(op1)
         return op1
 
     def handle_recursive_call(self, op):
                                                 arraydescr)
             return []
         # check for _immutable_fields_ hints
-        if v_inst.concretetype.TO._immutable_field(c_fieldname.value):
+        immut = v_inst.concretetype.TO._immutable_field(c_fieldname.value)
+        if immut:
             if (self.callcontrol is not None and
                 self.callcontrol.could_be_green_field(v_inst.concretetype.TO,
                                                       c_fieldname.value)):
         descr = self.cpu.fielddescrof(v_inst.concretetype.TO,
                                       c_fieldname.value)
         kind = getkind(RESULT)[0]
-        return SpaceOperation('getfield_%s_%s%s' % (argname, kind, pure),
-                              [v_inst, descr], op.result)
+        op1 = SpaceOperation('getfield_%s_%s%s' % (argname, kind, pure),
+                             [v_inst, descr], op.result)
+        #
+        if immut in (IR_QUASIIMMUTABLE, IR_QUASIIMMUTABLE_ARRAY):
+            descr1 = self.cpu.fielddescrof(
+                v_inst.concretetype.TO,
+                quasiimmut.get_mutate_field_name(c_fieldname.value))
+            op1 = [SpaceOperation('-live-', [], None),
+                   SpaceOperation('record_quasiimmut_field',
+                                  [v_inst, descr, descr1], None),
+                   op1]
+        return op1
 
     def rewrite_op_setfield(self, op):
         if self.is_typeptr_getset(op):
         return self._handle_oopspec_call(op, args, EffectInfo.OS_MATH_SQRT,
                                          EffectInfo.EF_PURE)
 
+    def rewrite_op_jit_force_quasi_immutable(self, op):
+        v_inst, c_fieldname = op.args
+        descr1 = self.cpu.fielddescrof(v_inst.concretetype.TO,
+                                       c_fieldname.value)
+        op0 = SpaceOperation('-live-', [], None)
+        op1 = SpaceOperation('jit_force_quasi_immutable', [v_inst, descr1],
+                             None)
+        return [op0, op1]
+
 # ____________________________________________________________
 
 class NotSupported(Exception):

File pypy/jit/codewriter/policy.py

  • Ignore whitespace
File contents unchanged.

File pypy/jit/codewriter/test/test_jtransform.py

View file
  • Ignore whitespace
     assert op1.args[3] == ListOfKind("ref", [])
     assert op1.args[4] == ListOfKind('float', [v1])
     assert op1.result == v2
+
+def test_quasi_immutable():
+    from pypy.rpython.rclass import FieldListAccessor, IR_QUASIIMMUTABLE
+    accessor = FieldListAccessor()
+    accessor.initialize(None, {'inst_x': IR_QUASIIMMUTABLE})
+    v2 = varoftype(lltype.Signed)
+    STRUCT = lltype.GcStruct('struct', ('inst_x', lltype.Signed),
+                             ('mutate_x', rclass.OBJECTPTR),
+                             hints={'immutable_fields': accessor})
+    for v_x in [const(lltype.malloc(STRUCT)), varoftype(lltype.Ptr(STRUCT))]:
+        op = SpaceOperation('getfield', [v_x, Constant('inst_x', lltype.Void)],
+                            v2)
+        tr = Transformer(FakeCPU())
+        [_, op1, op2] = tr.rewrite_operation(op)
+        assert op1.opname == 'record_quasiimmut_field'
+        assert len(op1.args) == 3
+        assert op1.args[0] == v_x
+        assert op1.args[1] == ('fielddescr', STRUCT, 'inst_x')
+        assert op1.args[2] == ('fielddescr', STRUCT, 'mutate_x')
+        assert op1.result is None
+        assert op2.opname == 'getfield_gc_i'
+        assert len(op2.args) == 2
+        assert op2.args[0] == v_x
+        assert op2.args[1] == ('fielddescr', STRUCT, 'inst_x')
+        assert op2.result is op.result
+
+def test_quasi_immutable_setfield():
+    from pypy.rpython.rclass import FieldListAccessor, IR_QUASIIMMUTABLE
+    accessor = FieldListAccessor()
+    accessor.initialize(None, {'inst_x': IR_QUASIIMMUTABLE})
+    v1 = varoftype(lltype.Signed)
+    STRUCT = lltype.GcStruct('struct', ('inst_x', lltype.Signed),
+                             ('mutate_x', rclass.OBJECTPTR),
+                             hints={'immutable_fields': accessor})
+    for v_x in [const(lltype.malloc(STRUCT)), varoftype(lltype.Ptr(STRUCT))]:
+        op = SpaceOperation('jit_force_quasi_immutable',
+                            [v_x, Constant('mutate_x', lltype.Void)],
+                            varoftype(lltype.Void))
+        tr = Transformer(FakeCPU(), FakeRegularCallControl())
+        tr.graph = 'currentgraph'
+        op0, op1 = tr.rewrite_operation(op)
+        assert op0.opname == '-live-'
+        assert op1.opname == 'jit_force_quasi_immutable'
+        assert op1.args[0] == v_x
+        assert op1.args[1] == ('fielddescr', STRUCT, 'mutate_x')

File pypy/jit/metainterp/blackhole.py

View file
  • Ignore whitespace
     def bhimpl_setfield_raw_f(cpu, struct, fielddescr, newvalue):
         cpu.bh_setfield_raw_f(struct, fielddescr, newvalue)
 
+    @arguments("r", "d", "d")
+    def bhimpl_record_quasiimmut_field(struct, fielddescr, mutatefielddescr):
+        pass
+
+    @arguments("cpu", "r", "d")
+    def bhimpl_jit_force_quasi_immutable(cpu, struct, mutatefielddescr):
+        from pypy.jit.metainterp import quasiimmut
+        quasiimmut.do_force_quasi_immutable(cpu, struct, mutatefielddescr)
+
     @arguments("cpu", "d", returns="r")
     def bhimpl_new(cpu, descr):
         return cpu.bh_new(descr)
             # We get here because it used to overflow, but now it no longer
             # does.
             pass
+        elif opnum == rop.GUARD_NOT_INVALIDATED:
+            pass
         else:
             from pypy.jit.metainterp.resoperation import opname
             raise NotImplementedError(opname[opnum])

File pypy/jit/metainterp/compile.py

View file
  • Ignore whitespace
             op.setdescr(None)    # clear reference, mostly for tests
             if not we_are_translated():
                 op._jumptarget_number = descr.number
+    # record this looptoken on the QuasiImmut used in the code
+    if loop.quasi_immutable_deps is not None:
+        for qmut in loop.quasi_immutable_deps:
+            qmut.register_loop_token(wref)
+        # XXX maybe we should clear the dictionary here
     # mostly for tests: make sure we don't keep a reference to the LoopToken
     loop.token = None
     if not we_are_translated():
         self.copy_all_attributes_into(res)
         return res
 
+class ResumeGuardNotInvalidated(ResumeGuardDescr):
+    def _clone_if_mutable(self):
+        res = ResumeGuardNotInvalidated()
+        self.copy_all_attributes_into(res)
+        return res
+
 class ResumeAtPositionDescr(ResumeGuardDescr):
     def _clone_if_mutable(self):
         res = ResumeAtPositionDescr()

File pypy/jit/metainterp/executor.py

View file
  • Ignore whitespace
                          rop.DEBUG_MERGE_POINT,
                          rop.JIT_DEBUG,
                          rop.SETARRAYITEM_RAW,
+                         rop.QUASIIMMUT_FIELD,
                          ):      # list of opcodes never executed by pyjitpl
                 continue
             raise AssertionError("missing %r" % (key,))

File pypy/jit/metainterp/history.py

View file
  • Ignore whitespace
     operations = None
     token = None
     call_pure_results = None
+    quasi_immutable_deps = None
 
     def __init__(self, name):
         self.name = name

File pypy/jit/metainterp/jitprof.py

View file
  • Ignore whitespace
 ABORT_BRIDGE
 ABORT_ESCAPE
 ABORT_BAD_LOOP
+ABORT_FORCE_QUASIIMMUT
 NVIRTUALS
 NVHOLES
 NVREUSED
         self._print_intline("abort: compiling", cnt[ABORT_BRIDGE])
         self._print_intline("abort: vable escape", cnt[ABORT_ESCAPE])
         self._print_intline("abort: bad loop", cnt[ABORT_BAD_LOOP])
+        self._print_intline("abort: force quasi-immut",
+                                               cnt[ABORT_FORCE_QUASIIMMUT])
         self._print_intline("nvirtuals", cnt[NVIRTUALS])
         self._print_intline("nvholes", cnt[NVHOLES])
         self._print_intline("nvreused", cnt[NVREUSED])

File pypy/jit/metainterp/optimizeopt/__init__.py

View file
  • Ignore whitespace
                 # during preamble but to keep it during the loop
                 optimizations.append(o)
 
-    if 'rewrite' not in enable_opts or 'virtualize' not in enable_opts:
+    if ('rewrite' not in enable_opts or 'virtualize' not in enable_opts
+        or 'heap' not in enable_opts):
         optimizations.append(OptSimplify())
 
     if inline_short_preamble:

File pypy/jit/metainterp/optimizeopt/heap.py

View file
  • Ignore whitespace
         self._lazy_setfields = []
         # cached array items:  {descr: CachedArrayItems}
         self.cached_arrayitems = {}
+        self._remove_guard_not_invalidated = False
+        self._seen_guard_not_invalidated = False
 
     def reconstruct_for_next_iteration(self, optimizer, valuemap):
         new = OptHeap()
                 effectinfo = None
             else:
                 effectinfo = op.getdescr().get_extra_info()
+            if effectinfo is None or effectinfo.check_can_invalidate():
+                self._seen_guard_not_invalidated = False
             if effectinfo is not None:
                 # XXX we can get the wrong complexity here, if the lists
                 # XXX stored on effectinfo are large
         self.cache_arrayitem_value(op.getdescr(), value, indexvalue, fieldvalue,
                                    write=True)
 
+    def optimize_QUASIIMMUT_FIELD(self, op):
+        # Pattern: QUASIIMMUT_FIELD(s, descr=QuasiImmutDescr)
+        #          x = GETFIELD_GC(s, descr='inst_x')
+        # If 's' is a constant (after optimizations), then we make 's.inst_x'
+        # a constant too, and we rely on the rest of the optimizations to
+        # constant-fold the following getfield_gc.
+        structvalue = self.getvalue(op.getarg(0))
+        if not structvalue.is_constant():
+            self._remove_guard_not_invalidated = True
+            return    # not a constant at all; ignore QUASIIMMUT_FIELD
+        #
+        from pypy.jit.metainterp.quasiimmut import QuasiImmutDescr
+        qmutdescr = op.getdescr()
+        assert isinstance(qmutdescr, QuasiImmutDescr)
+        # check that the value is still correct; it could have changed
+        # already between the tracing and now.  In this case, we are
+        # simply ignoring the QUASIIMMUT_FIELD hint and compiling it
+        # as a regular getfield.
+        if not qmutdescr.is_still_valid():
+            self._remove_guard_not_invalidated = True
+            return
+        # record as an out-of-line guard
+        if self.optimizer.quasi_immutable_deps is None:
+            self.optimizer.quasi_immutable_deps = {}
+        self.optimizer.quasi_immutable_deps[qmutdescr.qmut] = None
+        # perform the replacement in the list of operations
+        fieldvalue = self.getvalue(qmutdescr.constantfieldbox)
+        cf = self.field_cache(qmutdescr.fielddescr)
+        cf.force_lazy_setfield(self)
+        cf.remember_field_value(structvalue, fieldvalue)
+        self._remove_guard_not_invalidated = False
+
+    def optimize_GUARD_NOT_INVALIDATED(self, op):
+        if self._remove_guard_not_invalidated:
+            return
+        if self._seen_guard_not_invalidated:
+            return
+        self._seen_guard_not_invalidated = True
+        self.emit_operation(op)
+
     def propagate_forward(self, op):
         opnum = op.getopnum()
         for value, func in optimize_ops:

File pypy/jit/metainterp/optimizeopt/optimizer.py

View file
  • Ignore whitespace
         self.pendingfields = []
         self.posponedop = None
         self.exception_might_have_happened = False
+        self.quasi_immutable_deps = None
         self.newoperations = []
         if loop is not None:
             self.call_pure_results = loop.call_pure_results
         new.pure_operations = self.pure_operations
         new.producer = self.producer
         assert self.posponedop is None
+        new.quasi_immutable_deps = self.quasi_immutable_deps
 
         return new
 
             self.first_optimization.propagate_forward(op)
             self.i += 1
         self.loop.operations = self.newoperations
+        self.loop.quasi_immutable_deps = self.quasi_immutable_deps
         # accumulate counters
         self.resumedata_memo.update_counters(self.metainterp_sd.profiler)
 

File pypy/jit/metainterp/optimizeopt/simplify.py

View file
  • Ignore whitespace
         op = ResOperation(rop.SAME_AS, [op.getarg(0)], op.result)
         self.emit_operation(op)
 
+    def optimize_QUASIIMMUT_FIELD(self, op):
+        # xxx ideally we could also kill the following GUARD_NOT_INVALIDATED
+        #     but it's a bit hard to implement robustly if heap.py is also run
+        pass
+
     def propagate_forward(self, op):
         opnum = op.getopnum()
         for value, func in optimize_ops:

File pypy/jit/metainterp/optimizeopt/unroll.py

View file
  • Ignore whitespace
             virtual_state = modifier.get_virtual_state(jump_args)
 
             loop.preamble.operations = self.optimizer.newoperations
+            loop.preamble.quasi_immutable_deps = (
+                self.optimizer.quasi_immutable_deps)
             self.optimizer = self.optimizer.reconstruct_for_next_iteration()
             inputargs = self.inline(self.cloned_operations,
                                     loop.inputargs, jump_args)
             loop.preamble.operations.append(jmp)
 
             loop.operations = self.optimizer.newoperations
+            loop.quasi_immutable_deps = self.optimizer.quasi_immutable_deps
 
             start_resumedescr = loop.preamble.start_resumedescr.clone_if_mutable()
             assert isinstance(start_resumedescr, ResumeGuardDescr)

File pypy/jit/metainterp/pyjitpl.py

View file
  • Ignore whitespace
 from pypy.jit.metainterp.jitprof import EmptyProfiler
 from pypy.jit.metainterp.jitprof import GUARDS, RECORDED_OPS, ABORT_ESCAPE
 from pypy.jit.metainterp.jitprof import ABORT_TOO_LONG, ABORT_BRIDGE, \
-                                        ABORT_BAD_LOOP
+                                        ABORT_BAD_LOOP, ABORT_FORCE_QUASIIMMUT
 from pypy.jit.metainterp.jitexc import JitException, get_llexception
 from pypy.rlib.rarithmetic import intmask
 from pypy.rlib.objectmodel import specialize
     opimpl_setfield_raw_r = _opimpl_setfield_raw_any
     opimpl_setfield_raw_f = _opimpl_setfield_raw_any
 
+    @arguments("box", "descr", "descr", "orgpc")
+    def opimpl_record_quasiimmut_field(self, box, fielddescr,
+                                       mutatefielddescr, orgpc):
+        from pypy.jit.metainterp.quasiimmut import QuasiImmutDescr
+        cpu = self.metainterp.cpu
+        descr = QuasiImmutDescr(cpu, box, fielddescr, mutatefielddescr)
+        self.metainterp.history.record(rop.QUASIIMMUT_FIELD, [box],
+                                       None, descr=descr)
+        self.generate_guard(rop.GUARD_NOT_INVALIDATED, resumepc=orgpc)
+
+    @arguments("box", "descr", "orgpc")
+    def opimpl_jit_force_quasi_immutable(self, box, mutatefielddescr, orgpc):
+        # During tracing, a 'jit_force_quasi_immutable' usually turns into
+        # the operations that check that the content of 'mutate_xxx' is null.
+        # If it is actually not null already now, then we abort tracing.
+        # The idea is that if we use 'jit_force_quasi_immutable' on a freshly
+        # allocated object, then the GETFIELD_GC will know that the answer is
+        # null, and the guard will be removed.  So the fact that the field is
+        # quasi-immutable will have no effect, and instead it will work as a
+        # regular, probably virtual, structure.
+        mutatebox = self.execute_with_descr(rop.GETFIELD_GC,
+                                            mutatefielddescr, box)
+        if mutatebox.nonnull():
+            from pypy.jit.metainterp.quasiimmut import do_force_quasi_immutable
+            do_force_quasi_immutable(self.metainterp.cpu, box.getref_base(),
+                                     mutatefielddescr)
+            raise SwitchToBlackhole(ABORT_FORCE_QUASIIMMUT)
+        self.generate_guard(rop.GUARD_ISNULL, mutatebox, resumepc=orgpc)
+
     def _nonstandard_virtualizable(self, pc, box):
         # returns True if 'box' is actually not the "standard" virtualizable
         # that is stored in metainterp.virtualizable_boxes[-1]
         if opnum == rop.GUARD_NOT_FORCED:
             resumedescr = compile.ResumeGuardForcedDescr(metainterp_sd,
                                                    metainterp.jitdriver_sd)
+        elif opnum == rop.GUARD_NOT_INVALIDATED:
+            resumedescr = compile.ResumeGuardNotInvalidated()
         else:
             resumedescr = compile.ResumeGuardDescr()
         guard_op = metainterp.history.record(opnum, moreargs, None,
                 self.handle_possible_exception()
             except ChangeFrame:
                 pass
+        elif opnum == rop.GUARD_NOT_INVALIDATED:
+            pass # XXX we want to do something special in resume descr,
+                 # but not now
         elif opnum == rop.GUARD_NO_OVERFLOW:   # an overflow now detected
             self.execute_raised(OverflowError(), constant=True)
             try:

File pypy/jit/metainterp/quasiimmut.py

View file
  • Ignore whitespace
+import weakref
+from pypy.rpython.lltypesystem import lltype, rclass
+from pypy.rpython.annlowlevel import cast_base_ptr_to_instance
+from pypy.jit.metainterp.history import AbstractDescr
+
+
+def get_mutate_field_name(fieldname):
+    if fieldname.startswith('inst_'):    # lltype
+        return 'mutate_' + fieldname[5:]
+    elif fieldname.startswith('o'):      # ootype
+        return 'mutate_' + fieldname[1:]
+    else:
+        raise AssertionError(fieldname)
+
+def get_current_qmut_instance(cpu, gcref, mutatefielddescr):
+    """Returns the current QuasiImmut instance in the field,
+    possibly creating one.
+    """
+    qmut_gcref = cpu.bh_getfield_gc_r(gcref, mutatefielddescr)
+    if qmut_gcref:
+        qmut = QuasiImmut.show(cpu, qmut_gcref)
+    else:
+        qmut = QuasiImmut(cpu)
+        cpu.bh_setfield_gc_r(gcref, mutatefielddescr, qmut.hide())
+    return qmut
+
+def make_invalidation_function(STRUCT, mutatefieldname):
+    #
+    def _invalidate_now(p):
+        qmut_ptr = getattr(p, mutatefieldname)
+        setattr(p, mutatefieldname, lltype.nullptr(rclass.OBJECT))
+        qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr)
+        qmut.invalidate()
+    _invalidate_now._dont_inline_ = True
+    #
+    def invalidation(p):
+        if getattr(p, mutatefieldname):
+            _invalidate_now(p)
+    #
+    return invalidation
+
+def do_force_quasi_immutable(cpu, p, mutatefielddescr):
+    qmut_ref = cpu.bh_getfield_gc_r(p, mutatefielddescr)
+    if qmut_ref:
+        cpu.bh_setfield_gc_r(p, mutatefielddescr, cpu.ts.NULLREF)
+        qmut_ptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, qmut_ref)
+        qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr)
+        qmut.invalidate()
+
+
+class QuasiImmut(object):
+    llopaque = True
+    
+    def __init__(self, cpu):
+        self.cpu = cpu
+        # list of weakrefs to the LoopTokens that must be invalidated if
+        # this value ever changes
+        self.looptokens_wrefs = []
+        self.compress_limit = 30
+
+    def hide(self):
+        qmut_ptr = self.cpu.ts.cast_instance_to_base_ref(self)
+        return self.cpu.ts.cast_to_ref(qmut_ptr)
+
+    @staticmethod
+    def show(cpu, qmut_gcref):
+        qmut_ptr = cpu.ts.cast_to_baseclass(qmut_gcref)
+        return cast_base_ptr_to_instance(QuasiImmut, qmut_ptr)
+
+    def register_loop_token(self, wref_looptoken):
+        if len(self.looptokens_wrefs) > self.compress_limit:
+            self.compress_looptokens_list()
+        self.looptokens_wrefs.append(wref_looptoken)
+
+    def compress_looptokens_list(self):
+        self.looptokens_wrefs = [wref for wref in self.looptokens_wrefs
+                                      if wref() is not None]
+        self.compress_limit = (len(self.looptokens_wrefs) + 15) * 2
+
+    def invalidate(self):
+        # When this is called, all the loops that we record become
+        # invalid: all GUARD_NOT_INVALIDATED in these loops (and
+        # in attached bridges) must now fail.
+        wrefs = self.looptokens_wrefs
+        self.looptokens_wrefs = []
+        for wref in wrefs:
+            looptoken = wref()
+            if looptoken is not None:
+                self.cpu.invalidate_loop(looptoken)
+
+
+class QuasiImmutDescr(AbstractDescr):
+    structbox = None
+
+    def __init__(self, cpu, structbox, fielddescr, mutatefielddescr):
+        self.cpu = cpu
+        self.structbox = structbox
+        self.fielddescr = fielddescr
+        self.mutatefielddescr = mutatefielddescr
+        gcref = structbox.getref_base()
+        self.qmut = get_current_qmut_instance(cpu, gcref, mutatefielddescr)
+        self.constantfieldbox = self.get_current_constant_fieldvalue()
+
+    def get_current_constant_fieldvalue(self):
+        from pypy.jit.metainterp import executor
+        from pypy.jit.metainterp.resoperation import rop
+        fieldbox = executor.execute(self.cpu, None, rop.GETFIELD_GC,
+                                    self.fielddescr, self.structbox)
+        return fieldbox.constbox()
+
+    def is_still_valid(self):
+        assert self.structbox is not None
+        cpu = self.cpu
+        gcref = self.structbox.getref_base()
+        qmut = get_current_qmut_instance(cpu, gcref, self.mutatefielddescr)
+        if qmut is not self.qmut:
+            return False
+        else:
+            currentbox = self.get_current_constant_fieldvalue()
+            assert self.constantfieldbox.same_constant(currentbox)
+            return True

File pypy/jit/metainterp/resoperation.py

View file
  • Ignore whitespace
     'GUARD_NO_OVERFLOW/0d',
     'GUARD_OVERFLOW/0d',
     'GUARD_NOT_FORCED/0d',
+    'GUARD_NOT_INVALIDATED/0d',
     '_GUARD_LAST', # ----- end of guard operations -----
 
     '_NOSIDEEFFECT_FIRST', # ----- start of no_side_effect operations -----
     'VIRTUAL_REF_FINISH/2',   # removed before it's passed to the backend
     'COPYSTRCONTENT/5',       # src, dst, srcstart, dststart, length
     'COPYUNICODECONTENT/5',
+    'QUASIIMMUT_FIELD/1d',    # [objptr], descr=SlowMutateDescr
 
     '_CANRAISE_FIRST', # ----- start of can_raise operations -----
     '_CALL_FIRST',

File pypy/jit/metainterp/test/support.py

View file
  • Ignore whitespace
 from pypy.jit.metainterp import pyjitpl, history
 from pypy.jit.metainterp.warmstate import set_future_value
 from pypy.jit.codewriter.policy import JitPolicy
-from pypy.jit.codewriter import longlong
-from pypy.rlib.rfloat import isinf, isnan
+from pypy.jit.codewriter import codewriter, longlong
+from pypy.rlib.rfloat import isnan
 
 def _get_jitcodes(testself, CPUClass, func, values, type_system,
                   supports_longlong=False, **kwds):
-    from pypy.jit.codewriter import support, codewriter
+    from pypy.jit.codewriter import support
 
     class FakeJitCell:
         __compiled_merge_points = []
     stats = history.Stats()
     cpu = CPUClass(rtyper, stats, None, False)
     cw = codewriter.CodeWriter(cpu, [FakeJitDriverSD()])
+    cw.debug = True
     testself.cw = cw
     policy = JitPolicy()
     policy.set_supports_floats(True)
         kwds['type_system'] = self.type_system
         if "backendopt" not in kwds:
             kwds["backendopt"] = False
-        return ll_meta_interp(*args, **kwds)
+        old = codewriter.CodeWriter.debug
+        try:
+            codewriter.CodeWriter.debug = True
+            return ll_meta_interp(*args, **kwds)
+        finally:
+            codewriter.CodeWriter.debug = old
 
     def interp_operations(self, f, args, **kwds):
         # get the JitCodes for the function f

File pypy/jit/metainterp/test/test_jitprof.py

View file
  • Ignore whitespace
             ]
         assert profiler.events == expected
         assert profiler.times == [3, 2, 1, 1]
-        assert profiler.counters == [1, 2, 1, 1, 3, 3, 1, 13, 2, 0, 0, 0,
+        assert profiler.counters == [1, 2, 1, 1, 3, 3, 1, 13, 2, 0, 0, 0, 0,
                                      0, 0, 0, 0, 0]
 
     def test_simple_loop_with_call(self):

File pypy/jit/metainterp/test/test_optimizeopt.py

View file
  • Ignore whitespace
         # not obvious, because of the exception UnicodeDecodeError that
         # can be raised by ll_str2unicode()
 
-
-
-
-##class TestOOtype(OptimizeOptTest, OOtypeMixin):
-
-##    def test_instanceof(self):
-##        ops = """
-##        [i0]
-##        p0 = new_with_vtable(ConstClass(node_vtable))
-##        i1 = instanceof(p0, descr=nodesize)
-##        jump(i1)
-##        """
-##        expected = """
-##        [i0]
-##        jump(1)
-##        """
-##        self.optimize_loop(ops, expected)