PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/rpython/rtyper/rmodel.py

https://bitbucket.org/pypy/pypy/
Python | 465 lines | 440 code | 12 blank | 13 comment | 12 complexity | 5e65e2246258c7320ef13eac90506d0f MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.annotator import model as annmodel, unaryop, binaryop, description
  2. from rpython.flowspace.model import Constant
  3. from rpython.rtyper.error import TyperError, MissingRTypeOperation
  4. from rpython.rtyper.lltypesystem import lltype
  5. from rpython.rtyper.lltypesystem.lltype import Void, Bool, LowLevelType, Ptr
  6. from rpython.tool.pairtype import pairtype, extendabletype, pair
  7. # initialization states for Repr instances
  8. class setupstate(object):
  9. NOTINITIALIZED = 0
  10. INPROGRESS = 1
  11. BROKEN = 2
  12. FINISHED = 3
  13. DELAYED = 4
  14. class Repr(object):
  15. """ An instance of Repr is associated with each instance of SomeXxx.
  16. It defines the chosen representation for the SomeXxx. The Repr subclasses
  17. generally follows the SomeXxx subclass hierarchy, but there are numerous
  18. exceptions. For example, the annotator uses SomeIter for any iterator, but
  19. we need different representations according to the type of container we are
  20. iterating over.
  21. """
  22. __metaclass__ = extendabletype
  23. _initialized = setupstate.NOTINITIALIZED
  24. __NOT_RPYTHON__ = True
  25. def __repr__(self):
  26. return '<%s %s>' % (self.__class__.__name__, self.lowleveltype)
  27. def compact_repr(self):
  28. return '%s %s' % (self.__class__.__name__.replace('Repr','R'), self.lowleveltype._short_name())
  29. def setup(self):
  30. """ call _setup_repr() and keep track of the initializiation
  31. status to e.g. detect recursive _setup_repr invocations.
  32. the '_initialized' attr has four states:
  33. """
  34. if self._initialized == setupstate.FINISHED:
  35. return
  36. elif self._initialized == setupstate.BROKEN:
  37. raise BrokenReprTyperError(
  38. "cannot setup already failed Repr: %r" %(self,))
  39. elif self._initialized == setupstate.INPROGRESS:
  40. raise AssertionError(
  41. "recursive invocation of Repr setup(): %r" %(self,))
  42. elif self._initialized == setupstate.DELAYED:
  43. raise AssertionError(
  44. "Repr setup() is delayed and cannot be called yet: %r" %(self,))
  45. assert self._initialized == setupstate.NOTINITIALIZED
  46. self._initialized = setupstate.INPROGRESS
  47. try:
  48. self._setup_repr()
  49. except TyperError:
  50. self._initialized = setupstate.BROKEN
  51. raise
  52. else:
  53. self._initialized = setupstate.FINISHED
  54. def _setup_repr(self):
  55. "For recursive data structure, which must be initialized in two steps."
  56. def setup_final(self):
  57. """Same as setup(), called a bit later, for effects that are only
  58. needed after the typer finished (as opposed to needed for other parts
  59. of the typer itself)."""
  60. if self._initialized == setupstate.BROKEN:
  61. raise BrokenReprTyperError("cannot perform setup_final_touch "
  62. "on failed Repr: %r" %(self,))
  63. assert self._initialized == setupstate.FINISHED, (
  64. "setup_final() on repr with state %s: %r" %
  65. (self._initialized, self))
  66. self._setup_repr_final()
  67. def _setup_repr_final(self):
  68. pass
  69. def is_setup_delayed(self):
  70. return self._initialized == setupstate.DELAYED
  71. def set_setup_delayed(self, flag):
  72. assert self._initialized in (setupstate.NOTINITIALIZED,
  73. setupstate.DELAYED)
  74. if flag:
  75. self._initialized = setupstate.DELAYED
  76. else:
  77. self._initialized = setupstate.NOTINITIALIZED
  78. def set_setup_maybe_delayed(self):
  79. if self._initialized == setupstate.NOTINITIALIZED:
  80. self._initialized = setupstate.DELAYED
  81. return self._initialized == setupstate.DELAYED
  82. def __getattr__(self, name):
  83. # Assume that when an attribute is missing, it's because setup() needs
  84. # to be called
  85. if not (name[:2] == '__' == name[-2:]):
  86. if self._initialized == setupstate.NOTINITIALIZED:
  87. self.setup()
  88. try:
  89. return self.__dict__[name]
  90. except KeyError:
  91. pass
  92. raise AttributeError("%s instance has no attribute %s" % (
  93. self.__class__.__name__, name))
  94. def _freeze_(self):
  95. return True
  96. def convert_desc_or_const(self, desc_or_const):
  97. if isinstance(desc_or_const, description.Desc):
  98. return self.convert_desc(desc_or_const)
  99. elif isinstance(desc_or_const, Constant):
  100. return self.convert_const(desc_or_const.value)
  101. else:
  102. raise TyperError("convert_desc_or_const expects a Desc"
  103. "or Constant: %r" % desc_or_const)
  104. def convert_const(self, value):
  105. "Convert the given constant value to the low-level repr of 'self'."
  106. if not self.lowleveltype._contains_value(value):
  107. raise TyperError("convert_const(self = %r, value = %r)" % (
  108. self, value))
  109. return value
  110. def special_uninitialized_value(self):
  111. return None
  112. def get_ll_eq_function(self):
  113. """Return an eq(x,y) function to use to compare two low-level
  114. values of this Repr.
  115. This can return None to mean that simply using '==' is fine.
  116. """
  117. raise TyperError('no equality function for %r' % self)
  118. def get_ll_hash_function(self):
  119. """Return a hash(x) function for low-level values of this Repr.
  120. """
  121. raise TyperError('no hashing function for %r' % self)
  122. def get_ll_fasthash_function(self):
  123. """Return a 'fast' hash(x) function for low-level values of this
  124. Repr. The function can assume that 'x' is already stored as a
  125. key in a dict. get_ll_fasthash_function() should return None if
  126. the hash should rather be cached in the dict entry.
  127. """
  128. return None
  129. def can_ll_be_null(self, s_value):
  130. """Check if the low-level repr can take the value 0/NULL.
  131. The annotation s_value is provided as a hint because it may
  132. contain more information than the Repr.
  133. """
  134. return True # conservative
  135. def get_ll_dummyval_obj(self, rtyper, s_value):
  136. """A dummy value is a special low-level value, not otherwise
  137. used. It should not be the NULL value even if it is special.
  138. This returns either None, or a hashable object that has a
  139. (possibly lazy) attribute 'll_dummy_value'.
  140. The annotation s_value is provided as a hint because it may
  141. contain more information than the Repr.
  142. """
  143. T = self.lowleveltype
  144. if (isinstance(T, lltype.Ptr) and
  145. isinstance(T.TO, (lltype.Struct,
  146. lltype.Array,
  147. lltype.ForwardReference))):
  148. return DummyValueBuilder(rtyper, T.TO)
  149. else:
  150. return None
  151. def rtype_bltn_list(self, hop):
  152. raise TyperError('no list() support for %r' % self)
  153. def rtype_unichr(self, hop):
  154. raise TyperError('no unichr() support for %r' % self)
  155. # default implementation of some operations
  156. def rtype_getattr(self, hop):
  157. s_attr = hop.args_s[1]
  158. if s_attr.is_constant() and isinstance(s_attr.const, str):
  159. attr = s_attr.const
  160. s_obj = hop.args_s[0]
  161. if s_obj.find_method(attr) is None:
  162. raise TyperError("no method %s on %r" % (attr, s_obj))
  163. else:
  164. # implement methods (of a known name) as just their 'self'
  165. return hop.inputarg(self, arg=0)
  166. else:
  167. raise TyperError("getattr() with a non-constant attribute name")
  168. def rtype_str(self, hop):
  169. [v_self] = hop.inputargs(self)
  170. return hop.gendirectcall(self.ll_str, v_self)
  171. def rtype_bool(self, hop):
  172. try:
  173. vlen = self.rtype_len(hop)
  174. except MissingRTypeOperation:
  175. if not hop.s_result.is_constant():
  176. raise TyperError("rtype_bool(%r) not implemented" % (self,))
  177. return hop.inputconst(Bool, hop.s_result.const)
  178. else:
  179. return hop.genop('int_is_true', [vlen], resulttype=Bool)
  180. def rtype_isinstance(self, hop):
  181. hop.exception_cannot_occur()
  182. if hop.s_result.is_constant():
  183. return hop.inputconst(lltype.Bool, hop.s_result.const)
  184. if hop.args_s[1].is_constant() and hop.args_s[1].const in (str, list, unicode):
  185. if hop.args_s[0].knowntype not in (str, list, unicode):
  186. raise TyperError("isinstance(x, str/list/unicode) expects x to be known"
  187. " statically to be a str/list/unicode or None")
  188. rstrlist = hop.args_r[0]
  189. vstrlist = hop.inputarg(rstrlist, arg=0)
  190. cnone = hop.inputconst(rstrlist, None)
  191. return hop.genop('ptr_ne', [vstrlist, cnone], resulttype=lltype.Bool)
  192. raise TyperError
  193. def rtype_hash(self, hop):
  194. ll_hash = self.get_ll_hash_function()
  195. v, = hop.inputargs(self)
  196. return hop.gendirectcall(ll_hash, v)
  197. def rtype_iter(self, hop):
  198. r_iter = self.make_iterator_repr()
  199. return r_iter.newiter(hop)
  200. def make_iterator_repr(self, *variant):
  201. raise TyperError("%s is not iterable" % (self,))
  202. def rtype_hint(self, hop):
  203. return hop.inputarg(hop.r_result, arg=0)
  204. # hlinvoke helpers
  205. def get_r_implfunc(self):
  206. raise TyperError("%s has no corresponding implementation function representation" % (self,))
  207. def get_s_callable(self):
  208. raise TyperError("%s is not callable or cannot reconstruct a pbc annotation for itself" % (self,))
  209. def ll_hash_void(v):
  210. return 0
  211. class CanBeNull(object):
  212. """A mix-in base class for subclasses of Repr that represent None as
  213. 'null' and true values as non-'null'.
  214. """
  215. def rtype_bool(self, hop):
  216. if hop.s_result.is_constant():
  217. return hop.inputconst(Bool, hop.s_result.const)
  218. else:
  219. vlist = hop.inputargs(self)
  220. return hop.genop('ptr_nonzero', vlist, resulttype=Bool)
  221. class IteratorRepr(Repr):
  222. """Base class of Reprs of any kind of iterator."""
  223. def rtype_iter(self, hop): # iter(iter(x)) <==> iter(x)
  224. v_iter, = hop.inputargs(self)
  225. return v_iter
  226. def rtype_method_next(self, hop):
  227. return self.rtype_next(hop)
  228. class __extend__(annmodel.SomeIterator):
  229. # NOTE: SomeIterator is for iterators over any container, not just list
  230. def rtyper_makerepr(self, rtyper):
  231. r_container = rtyper.getrepr(self.s_container)
  232. if self.variant == ("enumerate",):
  233. from rpython.rtyper.rrange import EnumerateIteratorRepr
  234. r_baseiter = r_container.make_iterator_repr()
  235. return EnumerateIteratorRepr(r_baseiter)
  236. return r_container.make_iterator_repr(*self.variant)
  237. def rtyper_makekey(self):
  238. return self.__class__, self.s_container.rtyper_makekey(), self.variant
  239. class __extend__(annmodel.SomeImpossibleValue):
  240. def rtyper_makerepr(self, rtyper):
  241. return impossible_repr
  242. def rtyper_makekey(self):
  243. return self.__class__,
  244. # ____ generic binary operations _____________________________
  245. class __extend__(pairtype(Repr, Repr)):
  246. def rtype_is_((robj1, robj2), hop):
  247. if hop.s_result.is_constant():
  248. return inputconst(Bool, hop.s_result.const)
  249. roriginal1 = robj1
  250. roriginal2 = robj2
  251. if robj1.lowleveltype is Void:
  252. robj1 = robj2
  253. elif robj2.lowleveltype is Void:
  254. robj2 = robj1
  255. if (not isinstance(robj1.lowleveltype, Ptr) or
  256. not isinstance(robj2.lowleveltype, Ptr)):
  257. raise TyperError('is of instances of the non-pointers: %r, %r' % (
  258. roriginal1, roriginal2))
  259. if robj1.lowleveltype != robj2.lowleveltype:
  260. raise TyperError('is of instances of different pointer types: %r, %r' % (
  261. roriginal1, roriginal2))
  262. v_list = hop.inputargs(robj1, robj2)
  263. return hop.genop('ptr_eq', v_list, resulttype=Bool)
  264. # default implementation for checked getitems
  265. def rtype_getitem_idx((r_c1, r_o1), hop):
  266. return pair(r_c1, r_o1).rtype_getitem(hop)
  267. # ____________________________________________________________
  268. def make_missing_op(rcls, opname):
  269. attr = 'rtype_' + opname
  270. if not hasattr(rcls, attr):
  271. def missing_rtype_operation(self, hop):
  272. raise MissingRTypeOperation("unimplemented operation: "
  273. "'%s' on %r" % (opname, self))
  274. setattr(rcls, attr, missing_rtype_operation)
  275. for opname in unaryop.UNARY_OPERATIONS:
  276. make_missing_op(Repr, opname)
  277. for opname in binaryop.BINARY_OPERATIONS:
  278. make_missing_op(pairtype(Repr, Repr), opname)
  279. # not in BINARY_OPERATIONS
  280. make_missing_op(pairtype(Repr, Repr), 'contains')
  281. class __extend__(pairtype(Repr, Repr)):
  282. def convert_from_to((r_from, r_to), v, llops):
  283. return NotImplemented
  284. # ____________________________________________________________
  285. class VoidRepr(Repr):
  286. lowleveltype = Void
  287. def get_ll_eq_function(self): return None
  288. def get_ll_hash_function(self): return ll_hash_void
  289. get_ll_fasthash_function = get_ll_hash_function
  290. def ll_str(self, nothing): raise AssertionError("unreachable code")
  291. impossible_repr = VoidRepr()
  292. class SimplePointerRepr(Repr):
  293. "Convenience Repr for simple ll pointer types with no operation on them."
  294. def __init__(self, lowleveltype):
  295. self.lowleveltype = lowleveltype
  296. def convert_const(self, value):
  297. if value is not None:
  298. raise TyperError("%r only supports None as prebuilt constant, "
  299. "got %r" % (self, value))
  300. return lltype.nullptr(self.lowleveltype.TO)
  301. # ____________________________________________________________
  302. def inputconst(reqtype, value):
  303. """Return a Constant with the given value, of the requested type,
  304. which can be a Repr instance or a low-level type.
  305. """
  306. if isinstance(reqtype, Repr):
  307. value = reqtype.convert_const(value)
  308. lltype = reqtype.lowleveltype
  309. elif isinstance(reqtype, LowLevelType):
  310. lltype = reqtype
  311. else:
  312. raise TypeError(repr(reqtype))
  313. if not lltype._contains_value(value):
  314. raise TyperError("inputconst(): expected a %r, got %r" %
  315. (lltype, value))
  316. c = Constant(value)
  317. c.concretetype = lltype
  318. return c
  319. class BrokenReprTyperError(TyperError):
  320. """ raised when trying to setup a Repr whose setup
  321. has failed already.
  322. """
  323. def mangle(prefix, name):
  324. """Make a unique identifier from the prefix and the name. The name
  325. is allowed to start with $."""
  326. if name.startswith('$'):
  327. return '%sinternal_%s' % (prefix, name[1:])
  328. else:
  329. return '%s_%s' % (prefix, name)
  330. # __________ utilities __________
  331. def getgcflavor(classdef):
  332. classdesc = classdef.classdesc
  333. alloc_flavor = classdesc.get_param('_alloc_flavor_', default='gc')
  334. return alloc_flavor
  335. def externalvsinternal(rtyper, item_repr): # -> external_item_repr, (internal_)item_repr
  336. from rpython.rtyper import rclass
  337. if (isinstance(item_repr, rclass.InstanceRepr) and
  338. getattr(item_repr, 'gcflavor', 'gc') == 'gc'):
  339. return item_repr, rclass.getinstancerepr(rtyper, None)
  340. else:
  341. return item_repr, item_repr
  342. class DummyValueBuilder(object):
  343. def __init__(self, rtyper, TYPE):
  344. self.rtyper = rtyper
  345. self.TYPE = TYPE
  346. def _freeze_(self):
  347. return True
  348. def __hash__(self):
  349. return hash(self.TYPE)
  350. def __eq__(self, other):
  351. return (isinstance(other, DummyValueBuilder) and
  352. self.rtyper is other.rtyper and
  353. self.TYPE == other.TYPE)
  354. def __ne__(self, other):
  355. return not (self == other)
  356. @property
  357. def ll_dummy_value(self):
  358. TYPE = self.TYPE
  359. try:
  360. return self.rtyper.cache_dummy_values[TYPE]
  361. except KeyError:
  362. # generate a dummy ptr to an immortal placeholder struct/array
  363. if TYPE._is_varsize():
  364. p = lltype.malloc(TYPE, 1, immortal=True)
  365. else:
  366. p = lltype.malloc(TYPE, immortal=True)
  367. self.rtyper.cache_dummy_values[TYPE] = p
  368. return p
  369. # logging/warning
  370. from rpython.tool.ansi_print import AnsiLogger
  371. log = AnsiLogger("rtyper")
  372. def warning(msg):
  373. log.WARNING(msg)