PageRenderTime 81ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/rlib/objectmodel.py

http://github.com/pypy/pypy
Python | 636 lines | 579 code | 7 blank | 50 comment | 4 complexity | fea989728145853cde19e20009af3a09 MD5 | raw file
  1. """
  2. This file defines utilities for manipulating objects in an
  3. RPython-compliant way.
  4. """
  5. import sys
  6. import types
  7. import math
  8. # specialize is a decorator factory for attaching _annspecialcase_
  9. # attributes to functions: for example
  10. #
  11. # f._annspecialcase_ = 'specialize:memo' can be expressed with:
  12. # @specialize.memo()
  13. # def f(...
  14. #
  15. # f._annspecialcase_ = 'specialize:arg(0)' can be expressed with:
  16. # @specialize.arg(0)
  17. # def f(...
  18. #
  19. from pypy.rpython.extregistry import ExtRegistryEntry
  20. class _Specialize(object):
  21. def memo(self):
  22. """ Specialize the function based on argument values. All arguments
  23. have to be either constants or PBCs (i.e. instances of classes with a
  24. _freeze_ method returning True). The function call is replaced by
  25. just its result, or in case several PBCs are used, by some fast
  26. look-up of the result.
  27. """
  28. def decorated_func(func):
  29. func._annspecialcase_ = 'specialize:memo'
  30. return func
  31. return decorated_func
  32. def arg(self, *args):
  33. """ Specialize the function based on the values of given positions
  34. of arguments. They must be compile-time constants in order to work.
  35. There will be a copy of provided function for each combination
  36. of given arguments on positions in args (that can lead to
  37. exponential behavior!).
  38. """
  39. def decorated_func(func):
  40. func._annspecialcase_ = 'specialize:arg' + self._wrap(args)
  41. return func
  42. return decorated_func
  43. def arg_or_var(self, *args):
  44. """ Same as arg, but additionally allow for a 'variable' annotation,
  45. that would simply be a situation where designated arg is not
  46. a constant
  47. """
  48. def decorated_func(func):
  49. func._annspecialcase_ = 'specialize:arg_or_var' + self._wrap(args)
  50. return func
  51. return decorated_func
  52. def argtype(self, *args):
  53. """ Specialize function based on types of arguments on given positions.
  54. There will be a copy of provided function for each combination
  55. of given arguments on positions in args (that can lead to
  56. exponential behavior!).
  57. """
  58. def decorated_func(func):
  59. func._annspecialcase_ = 'specialize:argtype' + self._wrap(args)
  60. return func
  61. return decorated_func
  62. def ll(self):
  63. """ This is version of argtypes that cares about low-level types
  64. (so it'll get additional copies for two different types of pointers
  65. for example). Same warnings about exponential behavior apply.
  66. """
  67. def decorated_func(func):
  68. func._annspecialcase_ = 'specialize:ll'
  69. return func
  70. return decorated_func
  71. def ll_and_arg(self, *args):
  72. """ This is like ll(), and additionally like arg(...).
  73. """
  74. def decorated_func(func):
  75. func._annspecialcase_ = 'specialize:ll_and_arg' + self._wrap(args)
  76. return func
  77. return decorated_func
  78. def call_location(self):
  79. """ Specializes the function for each call site.
  80. """
  81. def decorated_func(func):
  82. func._annspecialcase_ = "specialize:call_location"
  83. return func
  84. return decorated_func
  85. def _wrap(self, args):
  86. return "("+','.join([repr(arg) for arg in args]) +")"
  87. specialize = _Specialize()
  88. def enforceargs(*args):
  89. """ Decorate a function with forcing of RPython-level types on arguments.
  90. None means no enforcing.
  91. XXX shouldn't we also add asserts in function body?
  92. """
  93. def decorator(f):
  94. f._annenforceargs_ = args
  95. return f
  96. return decorator
  97. # ____________________________________________________________
  98. class Symbolic(object):
  99. def annotation(self):
  100. return None
  101. def lltype(self):
  102. return None
  103. def __cmp__(self, other):
  104. if self is other:
  105. return 0
  106. else:
  107. raise TypeError("Symbolics cannot be compared! (%r, %r)"
  108. % (self, other))
  109. def __hash__(self):
  110. raise TypeError("Symbolics are not hashable! %r" % (self,))
  111. def __nonzero__(self):
  112. raise TypeError("Symbolics are not comparable! %r" % (self,))
  113. class ComputedIntSymbolic(Symbolic):
  114. def __init__(self, compute_fn):
  115. self.compute_fn = compute_fn
  116. def __repr__(self):
  117. # repr(self.compute_fn) can arrive back here in an
  118. # infinite recursion
  119. try:
  120. name = self.compute_fn.__name__
  121. except (AttributeError, TypeError):
  122. name = hex(id(self.compute_fn))
  123. return '%s(%r)' % (self.__class__.__name__, name)
  124. def annotation(self):
  125. from pypy.annotation import model
  126. return model.SomeInteger()
  127. def lltype(self):
  128. from pypy.rpython.lltypesystem import lltype
  129. return lltype.Signed
  130. class CDefinedIntSymbolic(Symbolic):
  131. def __init__(self, expr, default=0):
  132. self.expr = expr
  133. self.default = default
  134. def __repr__(self):
  135. return '%s(%r)' % (self.__class__.__name__, self.expr)
  136. def annotation(self):
  137. from pypy.annotation import model
  138. return model.SomeInteger()
  139. def lltype(self):
  140. from pypy.rpython.lltypesystem import lltype
  141. return lltype.Signed
  142. malloc_zero_filled = CDefinedIntSymbolic('MALLOC_ZERO_FILLED', default=0)
  143. running_on_llinterp = CDefinedIntSymbolic('RUNNING_ON_LLINTERP', default=1)
  144. # running_on_llinterp is meant to have the value 0 in all backends
  145. # ____________________________________________________________
  146. def instantiate(cls):
  147. "Create an empty instance of 'cls'."
  148. if isinstance(cls, type):
  149. return cls.__new__(cls)
  150. else:
  151. return types.InstanceType(cls)
  152. def we_are_translated():
  153. return False
  154. # annotation -> True (replaced by the flow objspace)
  155. def keepalive_until_here(*values):
  156. pass
  157. def is_annotation_constant(thing):
  158. """ Returns whether the annotator can prove that the argument is constant.
  159. For advanced usage only."""
  160. return True
  161. class Entry(ExtRegistryEntry):
  162. _about_ = is_annotation_constant
  163. def compute_result_annotation(self, s_arg):
  164. from pypy.annotation import model
  165. r = model.SomeBool()
  166. r.const = s_arg.is_constant()
  167. return r
  168. def specialize_call(self, hop):
  169. from pypy.rpython.lltypesystem import lltype
  170. hop.exception_cannot_occur()
  171. return hop.inputconst(lltype.Bool, hop.s_result.const)
  172. # ____________________________________________________________
  173. class FREED_OBJECT(object):
  174. def __getattribute__(self, attr):
  175. raise RuntimeError("trying to access freed object")
  176. def __setattr__(self, attr, value):
  177. raise RuntimeError("trying to access freed object")
  178. def free_non_gc_object(obj):
  179. assert not getattr(obj.__class__, "_alloc_flavor_", 'gc').startswith('gc'), "trying to free gc object"
  180. obj.__dict__ = {}
  181. obj.__class__ = FREED_OBJECT
  182. # ____________________________________________________________
  183. def newlist_hint(sizehint=0):
  184. """ Create a new list, but pass a hint how big the size should be
  185. preallocated
  186. """
  187. return []
  188. class Entry(ExtRegistryEntry):
  189. _about_ = newlist_hint
  190. def compute_result_annotation(self, s_sizehint):
  191. from pypy.annotation.model import SomeInteger
  192. assert isinstance(s_sizehint, SomeInteger)
  193. s_l = self.bookkeeper.newlist()
  194. s_l.listdef.listitem.resize()
  195. return s_l
  196. def specialize_call(self, orig_hop, i_sizehint=None):
  197. from pypy.rpython.rlist import rtype_newlist
  198. # fish a bit hop
  199. hop = orig_hop.copy()
  200. v = hop.args_v[0]
  201. r, s = hop.r_s_popfirstarg()
  202. if s.is_constant():
  203. v = hop.inputconst(r, s.const)
  204. hop.exception_is_here()
  205. return rtype_newlist(hop, v_sizehint=v)
  206. # ____________________________________________________________
  207. #
  208. # id-like functions. The idea is that calling hash() or id() is not
  209. # allowed in RPython. You have to call one of the following more
  210. # precise functions.
  211. def compute_hash(x):
  212. """RPython equivalent of hash(x), where 'x' is an immutable
  213. RPython-level. For strings or unicodes it computes the hash as
  214. in Python. For tuples it calls compute_hash() recursively.
  215. For instances it uses compute_identity_hash().
  216. Note that this can return 0 or -1 too.
  217. Behavior across translation:
  218. * on lltypesystem, it always returns the same number, both
  219. before and after translation. Dictionaries don't need to
  220. be rehashed after translation.
  221. * on ootypesystem, the value changes because of translation.
  222. Dictionaries need to be rehashed.
  223. """
  224. if isinstance(x, (str, unicode)):
  225. return _hash_string(x)
  226. if isinstance(x, int):
  227. return x
  228. if isinstance(x, float):
  229. return _hash_float(x)
  230. if isinstance(x, tuple):
  231. return _hash_tuple(x)
  232. if x is None:
  233. return 0
  234. return compute_identity_hash(x)
  235. def compute_identity_hash(x):
  236. """RPython equivalent of object.__hash__(x). This returns the
  237. so-called 'identity hash', which is the non-overridable default hash
  238. of Python. Can be called for any RPython-level object that turns
  239. into a GC object, but not NULL. The value is not guaranteed to be the
  240. same before and after translation, except for RPython instances on the
  241. lltypesystem.
  242. """
  243. assert x is not None
  244. result = object.__hash__(x)
  245. try:
  246. x.__dict__['__precomputed_identity_hash'] = result
  247. except (TypeError, AttributeError):
  248. pass
  249. return result
  250. def compute_unique_id(x):
  251. """RPython equivalent of id(x). The 'x' must be an RPython-level
  252. object that turns into a GC object. This operation can be very
  253. costly depending on the garbage collector. To remind you of this
  254. fact, we don't support id(x) directly.
  255. (XXX not implemented on ootype, falls back to compute_identity_hash)
  256. """
  257. return id(x) # XXX need to return r_longlong on some platforms
  258. def current_object_addr_as_int(x):
  259. """A cheap version of id(x). The current memory location of an
  260. object can change over time for moving GCs. Also note that on
  261. ootypesystem this typically doesn't return the real address but
  262. just the same as compute_hash(x).
  263. """
  264. from pypy.rlib.rarithmetic import intmask
  265. return intmask(id(x))
  266. # ----------
  267. def _hash_string(s):
  268. """The algorithm behind compute_hash() for a string or a unicode."""
  269. from pypy.rlib.rarithmetic import intmask
  270. length = len(s)
  271. if length == 0:
  272. return -1
  273. x = ord(s[0]) << 7
  274. i = 0
  275. while i < length:
  276. x = intmask((1000003*x) ^ ord(s[i]))
  277. i += 1
  278. x ^= length
  279. return intmask(x)
  280. def _hash_float(f):
  281. """The algorithm behind compute_hash() for a float.
  282. This implementation is identical to the CPython implementation,
  283. except the fact that the integer case is not treated specially.
  284. In RPython, floats cannot be used with ints in dicts, anyway.
  285. """
  286. from pypy.rlib.rarithmetic import intmask
  287. from pypy.rlib.rfloat import isfinite, isinf
  288. if not isfinite(f):
  289. if isinf(f):
  290. if f < 0.0:
  291. return -271828
  292. else:
  293. return 314159
  294. else: #isnan(f):
  295. return 0
  296. v, expo = math.frexp(f)
  297. v *= TAKE_NEXT
  298. hipart = int(v)
  299. v = (v - float(hipart)) * TAKE_NEXT
  300. x = hipart + int(v) + (expo << 15)
  301. return intmask(x)
  302. TAKE_NEXT = float(2**31)
  303. def _hash_tuple(t):
  304. """NOT_RPYTHON. The algorithm behind compute_hash() for a tuple.
  305. It is modelled after the old algorithm of Python 2.3, which is
  306. a bit faster than the one introduced by Python 2.4. We assume
  307. that nested tuples are very uncommon in RPython, making the bad
  308. case unlikely.
  309. """
  310. from pypy.rlib.rarithmetic import intmask
  311. x = 0x345678
  312. for item in t:
  313. y = compute_hash(item)
  314. x = intmask((1000003 * x) ^ y)
  315. return x
  316. # ----------
  317. class Entry(ExtRegistryEntry):
  318. _about_ = compute_hash
  319. def compute_result_annotation(self, s_x):
  320. from pypy.annotation import model as annmodel
  321. return annmodel.SomeInteger()
  322. def specialize_call(self, hop):
  323. r_obj, = hop.args_r
  324. v_obj, = hop.inputargs(r_obj)
  325. ll_fn = r_obj.get_ll_hash_function()
  326. hop.exception_is_here()
  327. return hop.gendirectcall(ll_fn, v_obj)
  328. class Entry(ExtRegistryEntry):
  329. _about_ = compute_identity_hash
  330. def compute_result_annotation(self, s_x):
  331. from pypy.annotation import model as annmodel
  332. return annmodel.SomeInteger()
  333. def specialize_call(self, hop):
  334. from pypy.rpython.lltypesystem import lltype
  335. vobj, = hop.inputargs(hop.args_r[0])
  336. if hop.rtyper.type_system.name == 'lltypesystem':
  337. ok = (isinstance(vobj.concretetype, lltype.Ptr) and
  338. vobj.concretetype.TO._gckind == 'gc')
  339. else:
  340. from pypy.rpython.ootypesystem import ootype
  341. ok = isinstance(vobj.concretetype, ootype.OOType)
  342. if not ok:
  343. from pypy.rpython.error import TyperError
  344. raise TyperError("compute_identity_hash() cannot be applied to"
  345. " %r" % (vobj.concretetype,))
  346. hop.exception_cannot_occur()
  347. return hop.genop('gc_identityhash', [vobj], resulttype=lltype.Signed)
  348. class Entry(ExtRegistryEntry):
  349. _about_ = compute_unique_id
  350. def compute_result_annotation(self, s_x):
  351. from pypy.annotation import model as annmodel
  352. return annmodel.SomeInteger()
  353. def specialize_call(self, hop):
  354. from pypy.rpython.lltypesystem import lltype
  355. vobj, = hop.inputargs(hop.args_r[0])
  356. if hop.rtyper.type_system.name == 'lltypesystem':
  357. ok = (isinstance(vobj.concretetype, lltype.Ptr) and
  358. vobj.concretetype.TO._gckind == 'gc')
  359. else:
  360. from pypy.rpython.ootypesystem import ootype
  361. ok = isinstance(vobj.concretetype, (ootype.Instance, ootype.BuiltinType))
  362. if not ok:
  363. from pypy.rpython.error import TyperError
  364. raise TyperError("compute_unique_id() cannot be applied to"
  365. " %r" % (vobj.concretetype,))
  366. hop.exception_cannot_occur()
  367. return hop.genop('gc_id', [vobj], resulttype=lltype.Signed)
  368. class Entry(ExtRegistryEntry):
  369. _about_ = current_object_addr_as_int
  370. def compute_result_annotation(self, s_x):
  371. from pypy.annotation import model as annmodel
  372. return annmodel.SomeInteger()
  373. def specialize_call(self, hop):
  374. vobj, = hop.inputargs(hop.args_r[0])
  375. hop.exception_cannot_occur()
  376. if hop.rtyper.type_system.name == 'lltypesystem':
  377. from pypy.rpython.lltypesystem import lltype
  378. if isinstance(vobj.concretetype, lltype.Ptr):
  379. return hop.genop('cast_ptr_to_int', [vobj],
  380. resulttype = lltype.Signed)
  381. elif hop.rtyper.type_system.name == 'ootypesystem':
  382. from pypy.rpython.ootypesystem import ootype
  383. if isinstance(vobj.concretetype, ootype.Instance):
  384. return hop.genop('gc_identityhash', [vobj],
  385. resulttype = ootype.Signed)
  386. from pypy.rpython.error import TyperError
  387. raise TyperError("current_object_addr_as_int() cannot be applied to"
  388. " %r" % (vobj.concretetype,))
  389. # ____________________________________________________________
  390. def hlinvoke(repr, llcallable, *args):
  391. raise TypeError, "hlinvoke is meant to be rtyped and not called direclty"
  392. def invoke_around_extcall(before, after):
  393. """Call before() before any external function call, and after() after.
  394. At the moment only one pair before()/after() can be registered at a time.
  395. """
  396. # NOTE: the hooks are cleared during translation! To be effective
  397. # in a compiled program they must be set at run-time.
  398. from pypy.rpython.lltypesystem import rffi
  399. rffi.aroundstate.before = before
  400. rffi.aroundstate.after = after
  401. # the 'aroundstate' contains regular function and not ll pointers to them,
  402. # but let's call llhelper() anyway to force their annotation
  403. from pypy.rpython.annlowlevel import llhelper
  404. llhelper(rffi.AroundFnPtr, before)
  405. llhelper(rffi.AroundFnPtr, after)
  406. def is_in_callback():
  407. from pypy.rpython.lltypesystem import rffi
  408. return rffi.stackcounter.stacks_counter > 1
  409. class UnboxedValue(object):
  410. """A mixin class to use for classes that have exactly one field which
  411. is an integer. They are represented as a tagged pointer, if the
  412. translation.taggedpointers config option is used."""
  413. _mixin_ = True
  414. def __new__(cls, value):
  415. assert '__init__' not in cls.__dict__ # won't be called anyway
  416. assert isinstance(cls.__slots__, str) or len(cls.__slots__) == 1
  417. return super(UnboxedValue, cls).__new__(cls)
  418. def __init__(self, value):
  419. # this funtion is annotated but not included in the translated program
  420. int_as_pointer = value * 2 + 1 # XXX for now
  421. if -sys.maxint-1 <= int_as_pointer <= sys.maxint:
  422. if isinstance(self.__class__.__slots__, str):
  423. setattr(self, self.__class__.__slots__, value)
  424. else:
  425. setattr(self, self.__class__.__slots__[0], value)
  426. else:
  427. raise OverflowError("UnboxedValue: argument out of range")
  428. def __repr__(self):
  429. return '<unboxed %d>' % (self.get_untagged_value(),)
  430. def get_untagged_value(self): # helper, equivalent to reading the custom field
  431. if isinstance(self.__class__.__slots__, str):
  432. return getattr(self, self.__class__.__slots__)
  433. else:
  434. return getattr(self, self.__class__.__slots__[0])
  435. # ____________________________________________________________
  436. class r_dict(object):
  437. """An RPython dict-like object.
  438. Only provides the interface supported by RPython.
  439. The functions key_eq() and key_hash() are used by the key comparison
  440. algorithm."""
  441. def __init__(self, key_eq, key_hash, force_non_null=False):
  442. self._dict = {}
  443. self.key_eq = key_eq
  444. self.key_hash = key_hash
  445. self.force_non_null = force_non_null
  446. def __getitem__(self, key):
  447. return self._dict[_r_dictkey(self, key)]
  448. def __setitem__(self, key, value):
  449. self._dict[_r_dictkey(self, key)] = value
  450. def __delitem__(self, key):
  451. del self._dict[_r_dictkey(self, key)]
  452. def __len__(self):
  453. return len(self._dict)
  454. def __iter__(self):
  455. for dk in self._dict:
  456. yield dk.key
  457. def __contains__(self, key):
  458. return _r_dictkey(self, key) in self._dict
  459. def get(self, key, default):
  460. return self._dict.get(_r_dictkey(self, key), default)
  461. def setdefault(self, key, default):
  462. return self._dict.setdefault(_r_dictkey(self, key), default)
  463. def popitem(self):
  464. dk, value = self._dict.popitem()
  465. return dk.key, value
  466. def copy(self):
  467. result = r_dict(self.key_eq, self.key_hash)
  468. result.update(self)
  469. return result
  470. def update(self, other):
  471. for key, value in other.items():
  472. self[key] = value
  473. def keys(self):
  474. return [dk.key for dk in self._dict]
  475. def values(self):
  476. return self._dict.values()
  477. def items(self):
  478. return [(dk.key, value) for dk, value in self._dict.items()]
  479. iterkeys = __iter__
  480. def itervalues(self):
  481. return self._dict.itervalues()
  482. def iteritems(self):
  483. for dk, value in self._dict.items():
  484. yield dk.key, value
  485. def clear(self):
  486. self._dict.clear()
  487. def __repr__(self):
  488. "Representation for debugging purposes."
  489. return 'r_dict(%r)' % (self._dict,)
  490. def __hash__(self):
  491. raise TypeError("cannot hash r_dict instances")
  492. class _r_dictkey(object):
  493. __slots__ = ['dic', 'key', 'hash']
  494. def __init__(self, dic, key):
  495. self.dic = dic
  496. self.key = key
  497. self.hash = dic.key_hash(key)
  498. def __eq__(self, other):
  499. if not isinstance(other, _r_dictkey):
  500. return NotImplemented
  501. return self.dic.key_eq(self.key, other.key)
  502. def __ne__(self, other):
  503. if not isinstance(other, _r_dictkey):
  504. return NotImplemented
  505. return not self.dic.key_eq(self.key, other.key)
  506. def __hash__(self):
  507. return self.hash
  508. def __repr__(self):
  509. return repr(self.key)
  510. class _r_dictkey_with_hash(_r_dictkey):
  511. def __init__(self, dic, key, hash):
  512. self.dic = dic
  513. self.key = key
  514. self.hash = hash