PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/rpython/rbuiltin.py

http://github.com/pypy/pypy
Python | 689 lines | 546 code | 102 blank | 41 comment | 85 complexity | 3a3e15bd9160407aeee0dd3212689603 MD5 | raw file
  1. from pypy.tool.pairtype import pairtype
  2. from pypy.annotation import model as annmodel
  3. from pypy.objspace.flow.model import Constant
  4. from pypy.rpython.lltypesystem import lltype, rclass, llmemory
  5. from pypy.rpython import rint, raddress
  6. from pypy.rlib import rarithmetic, objectmodel
  7. from pypy.rpython.error import TyperError
  8. from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
  9. from pypy.rpython.rrange import rtype_builtin_range, rtype_builtin_xrange
  10. from pypy.rpython.rrange import rtype_builtin_enumerate
  11. from pypy.rpython import rstr
  12. from pypy.rpython import rptr
  13. from pypy.rpython.robject import pyobj_repr
  14. from pypy.tool import sourcetools
  15. from pypy.rpython import extregistry
  16. class __extend__(annmodel.SomeBuiltin):
  17. def rtyper_makerepr(self, rtyper):
  18. if self.s_self is None:
  19. # built-in function case
  20. if not self.is_constant():
  21. raise TyperError("non-constant built-in function!")
  22. return BuiltinFunctionRepr(self.const)
  23. else:
  24. # built-in method case
  25. assert self.methodname is not None
  26. result = BuiltinMethodRepr(rtyper, self.s_self, self.methodname)
  27. if result.self_repr == pyobj_repr:
  28. return pyobj_repr # special case: methods of 'PyObject*'
  29. else:
  30. return result
  31. def rtyper_makekey(self):
  32. if self.s_self is None:
  33. # built-in function case
  34. const = getattr(self, 'const', None)
  35. if extregistry.is_registered(const):
  36. const = extregistry.lookup(const)
  37. return self.__class__, const
  38. else:
  39. # built-in method case
  40. # NOTE: we hash by id of self.s_self here. This appears to be
  41. # necessary because it ends up in hop.args_s[0] in the method call,
  42. # and there is no telling what information the called
  43. # rtype_method_xxx() will read from that hop.args_s[0].
  44. # See test_method_join in test_rbuiltin.
  45. # There is no problem with self.s_self being garbage-collected and
  46. # its id reused, because the BuiltinMethodRepr keeps a reference
  47. # to it.
  48. return (self.__class__, self.methodname, id(self.s_self))
  49. def call_args_expand(hop, takes_kwds = True):
  50. hop = hop.copy()
  51. from pypy.interpreter.argument import ArgumentsForTranslation
  52. arguments = ArgumentsForTranslation.fromshape(
  53. None, hop.args_s[1].const, # shape
  54. range(hop.nb_args-2))
  55. if arguments.w_starstararg is not None:
  56. raise TyperError("**kwds call not implemented")
  57. if arguments.w_stararg is not None:
  58. # expand the *arg in-place -- it must be a tuple
  59. from pypy.rpython.rtuple import AbstractTupleRepr
  60. if arguments.w_stararg != hop.nb_args - 3:
  61. raise TyperError("call pattern too complex")
  62. hop.nb_args -= 1
  63. v_tuple = hop.args_v.pop()
  64. s_tuple = hop.args_s.pop()
  65. r_tuple = hop.args_r.pop()
  66. if not isinstance(r_tuple, AbstractTupleRepr):
  67. raise TyperError("*arg must be a tuple")
  68. for i in range(len(r_tuple.items_r)):
  69. v_item = r_tuple.getitem_internal(hop.llops, v_tuple, i)
  70. hop.nb_args += 1
  71. hop.args_v.append(v_item)
  72. hop.args_s.append(s_tuple.items[i])
  73. hop.args_r.append(r_tuple.items_r[i])
  74. keywords = arguments.keywords
  75. if not takes_kwds and keywords:
  76. raise TyperError("kwds args not supported")
  77. # prefix keyword arguments with 'i_'
  78. kwds_i = {}
  79. for i, key in enumerate(keywords):
  80. index = arguments.keywords_w[i]
  81. kwds_i['i_'+key] = index
  82. return hop, kwds_i
  83. class BuiltinFunctionRepr(Repr):
  84. lowleveltype = lltype.Void
  85. def __init__(self, builtinfunc):
  86. self.builtinfunc = builtinfunc
  87. def findbltintyper(self, rtyper):
  88. "Find the function to use to specialize calls to this built-in func."
  89. try:
  90. return BUILTIN_TYPER[self.builtinfunc]
  91. except (KeyError, TypeError):
  92. pass
  93. try:
  94. return rtyper.type_system.rbuiltin.BUILTIN_TYPER[self.builtinfunc]
  95. except (KeyError, TypeError):
  96. pass
  97. if extregistry.is_registered(self.builtinfunc):
  98. entry = extregistry.lookup(self.builtinfunc)
  99. return entry.specialize_call
  100. raise TyperError("don't know about built-in function %r" % (
  101. self.builtinfunc,))
  102. def _call(self, hop2, **kwds_i):
  103. bltintyper = self.findbltintyper(hop2.rtyper)
  104. hop2.llops._called_exception_is_here_or_cannot_occur = False
  105. v_result = bltintyper(hop2, **kwds_i)
  106. if not hop2.llops._called_exception_is_here_or_cannot_occur:
  107. raise TyperError("missing hop.exception_cannot_occur() or "
  108. "hop.exception_is_here() in %s" % bltintyper)
  109. return v_result
  110. def rtype_simple_call(self, hop):
  111. hop2 = hop.copy()
  112. hop2.r_s_popfirstarg()
  113. return self._call(hop2)
  114. def rtype_call_args(self, hop):
  115. # calling a built-in function with keyword arguments:
  116. # mostly for rpython.objectmodel.hint()
  117. hop, kwds_i = call_args_expand(hop)
  118. hop2 = hop.copy()
  119. hop2.r_s_popfirstarg()
  120. hop2.r_s_popfirstarg()
  121. # the RPython-level keyword args are passed with an 'i_' prefix and
  122. # the corresponding value is an *index* in the hop2 arguments,
  123. # to be used with hop.inputarg(arg=..)
  124. return self._call(hop2, **kwds_i)
  125. class BuiltinMethodRepr(Repr):
  126. def __init__(self, rtyper, s_self, methodname):
  127. self.s_self = s_self
  128. self.self_repr = rtyper.getrepr(s_self)
  129. self.methodname = methodname
  130. # methods of a known name are implemented as just their 'self'
  131. self.lowleveltype = self.self_repr.lowleveltype
  132. def convert_const(self, obj):
  133. return self.self_repr.convert_const(get_builtin_method_self(obj))
  134. def rtype_simple_call(self, hop):
  135. # methods: look up the rtype_method_xxx()
  136. name = 'rtype_method_' + self.methodname
  137. try:
  138. bltintyper = getattr(self.self_repr, name)
  139. except AttributeError:
  140. raise TyperError("missing %s.%s" % (
  141. self.self_repr.__class__.__name__, name))
  142. # hack based on the fact that 'lowleveltype == self_repr.lowleveltype'
  143. hop2 = hop.copy()
  144. assert hop2.args_r[0] is self
  145. if isinstance(hop2.args_v[0], Constant):
  146. c = hop2.args_v[0].value # get object from bound method
  147. c = get_builtin_method_self(c)
  148. hop2.args_v[0] = Constant(c)
  149. hop2.args_s[0] = self.s_self
  150. hop2.args_r[0] = self.self_repr
  151. return bltintyper(hop2)
  152. class __extend__(pairtype(BuiltinMethodRepr, BuiltinMethodRepr)):
  153. def convert_from_to((r_from, r_to), v, llops):
  154. # convert between two MethodReprs only if they are about the same
  155. # methodname. (Useful for the case r_from.s_self == r_to.s_self but
  156. # r_from is not r_to.) See test_rbuiltin.test_method_repr.
  157. if r_from.methodname != r_to.methodname:
  158. return NotImplemented
  159. return llops.convertvar(v, r_from.self_repr, r_to.self_repr)
  160. def parse_kwds(hop, *argspec_i_r):
  161. lst = [i for (i, r) in argspec_i_r if i is not None]
  162. lst.sort()
  163. if lst != range(hop.nb_args - len(lst), hop.nb_args):
  164. raise TyperError("keyword args are expected to be at the end of "
  165. "the 'hop' arg list")
  166. result = []
  167. for i, r in argspec_i_r:
  168. if i is not None:
  169. if r is None:
  170. r = hop.args_r[i]
  171. result.append(hop.inputarg(r, arg=i))
  172. else:
  173. result.append(None)
  174. hop.nb_args -= len(lst)
  175. return result
  176. def get_builtin_method_self(x):
  177. try:
  178. return x.__self__ # on top of CPython
  179. except AttributeError:
  180. return x.im_self # on top of PyPy
  181. # ____________________________________________________________
  182. def rtype_builtin_bool(hop):
  183. # not called any more?
  184. assert hop.nb_args == 1
  185. return hop.args_r[0].rtype_is_true(hop)
  186. def rtype_builtin_int(hop):
  187. if isinstance(hop.args_s[0], annmodel.SomeString):
  188. assert 1 <= hop.nb_args <= 2
  189. return hop.args_r[0].rtype_int(hop)
  190. assert hop.nb_args == 1
  191. return hop.args_r[0].rtype_int(hop)
  192. def rtype_builtin_float(hop):
  193. assert hop.nb_args == 1
  194. return hop.args_r[0].rtype_float(hop)
  195. def rtype_builtin_chr(hop):
  196. assert hop.nb_args == 1
  197. return hop.args_r[0].rtype_chr(hop)
  198. def rtype_builtin_unichr(hop):
  199. assert hop.nb_args == 1
  200. return hop.args_r[0].rtype_unichr(hop)
  201. def rtype_builtin_unicode(hop):
  202. return hop.args_r[0].rtype_unicode(hop)
  203. def rtype_builtin_list(hop):
  204. return hop.args_r[0].rtype_bltn_list(hop)
  205. #def rtype_builtin_range(hop): see rrange.py
  206. #def rtype_builtin_xrange(hop): see rrange.py
  207. #def rtype_builtin_enumerate(hop): see rrange.py
  208. #def rtype_r_dict(hop): see rdict.py
  209. def rtype_intmask(hop):
  210. hop.exception_cannot_occur()
  211. vlist = hop.inputargs(lltype.Signed)
  212. return vlist[0]
  213. def rtype_longlongmask(hop):
  214. hop.exception_cannot_occur()
  215. vlist = hop.inputargs(lltype.SignedLongLong)
  216. return vlist[0]
  217. def rtype_builtin_min(hop):
  218. v1, v2 = hop.inputargs(hop.r_result, hop.r_result)
  219. hop.exception_cannot_occur()
  220. return hop.gendirectcall(ll_min, v1, v2)
  221. def ll_min(i1, i2):
  222. if i1 < i2:
  223. return i1
  224. return i2
  225. def rtype_builtin_max(hop):
  226. v1, v2 = hop.inputargs(hop.r_result, hop.r_result)
  227. hop.exception_cannot_occur()
  228. return hop.gendirectcall(ll_max, v1, v2)
  229. def ll_max(i1, i2):
  230. if i1 > i2:
  231. return i1
  232. return i2
  233. def rtype_Exception__init__(hop):
  234. pass
  235. def rtype_object__init__(hop):
  236. pass
  237. def rtype_OSError__init__(hop):
  238. hop.exception_cannot_occur()
  239. if hop.nb_args == 2:
  240. raise TyperError("OSError() should not be called with "
  241. "a single argument")
  242. if hop.nb_args >= 3:
  243. v_self = hop.args_v[0]
  244. r_self = hop.args_r[0]
  245. v_errno = hop.inputarg(lltype.Signed, arg=1)
  246. r_self.setfield(v_self, 'errno', v_errno, hop.llops)
  247. def rtype_WindowsError__init__(hop):
  248. hop.exception_cannot_occur()
  249. if hop.nb_args == 2:
  250. raise TyperError("WindowsError() should not be called with "
  251. "a single argument")
  252. if hop.nb_args >= 3:
  253. v_self = hop.args_v[0]
  254. r_self = hop.args_r[0]
  255. v_error = hop.inputarg(lltype.Signed, arg=1)
  256. r_self.setfield(v_self, 'winerror', v_error, hop.llops)
  257. def rtype_hlinvoke(hop):
  258. _, s_repr = hop.r_s_popfirstarg()
  259. r_callable = s_repr.const
  260. r_func, nimplicitarg = r_callable.get_r_implfunc()
  261. s_callable = r_callable.get_s_callable()
  262. nbargs = len(hop.args_s) - 1 + nimplicitarg
  263. s_sigs = r_func.get_s_signatures((nbargs, (), False, False))
  264. if len(s_sigs) != 1:
  265. raise TyperError("cannot hlinvoke callable %r with not uniform"
  266. "annotations: %r" % (r_callable,
  267. s_sigs))
  268. args_s, s_ret = s_sigs[0]
  269. rinputs = [hop.rtyper.getrepr(s_obj) for s_obj in args_s]
  270. rresult = hop.rtyper.getrepr(s_ret)
  271. args_s = args_s[nimplicitarg:]
  272. rinputs = rinputs[nimplicitarg:]
  273. new_args_r = [r_callable] + rinputs
  274. for i in range(len(new_args_r)):
  275. assert hop.args_r[i].lowleveltype == new_args_r[i].lowleveltype
  276. hop.args_r = new_args_r
  277. hop.args_s = [s_callable] + args_s
  278. hop.s_result = s_ret
  279. assert hop.r_result.lowleveltype == rresult.lowleveltype
  280. hop.r_result = rresult
  281. return hop.dispatch()
  282. # collect all functions
  283. import __builtin__, exceptions
  284. BUILTIN_TYPER = {}
  285. for name, value in globals().items():
  286. if name.startswith('rtype_builtin_'):
  287. original = getattr(__builtin__, name[14:])
  288. BUILTIN_TYPER[original] = value
  289. BUILTIN_TYPER[getattr(OSError.__init__, 'im_func', OSError.__init__)] = (
  290. rtype_OSError__init__)
  291. try:
  292. WindowsError
  293. except NameError:
  294. pass
  295. else:
  296. BUILTIN_TYPER[
  297. getattr(WindowsError.__init__, 'im_func', WindowsError.__init__)] = (
  298. rtype_WindowsError__init__)
  299. BUILTIN_TYPER[object.__init__] = rtype_object__init__
  300. # annotation of low-level types
  301. def rtype_malloc(hop, i_flavor=None, i_zero=None, i_track_allocation=None,
  302. i_add_memory_pressure=None):
  303. assert hop.args_s[0].is_constant()
  304. vlist = [hop.inputarg(lltype.Void, arg=0)]
  305. opname = 'malloc'
  306. v_flavor, v_zero, v_track_allocation, v_add_memory_pressure = parse_kwds(
  307. hop,
  308. (i_flavor, lltype.Void),
  309. (i_zero, None),
  310. (i_track_allocation, None),
  311. (i_add_memory_pressure, None))
  312. flags = {'flavor': 'gc'}
  313. if v_flavor is not None:
  314. flags['flavor'] = v_flavor.value
  315. if i_zero is not None:
  316. flags['zero'] = v_zero.value
  317. if i_track_allocation is not None:
  318. flags['track_allocation'] = v_track_allocation.value
  319. if i_add_memory_pressure is not None:
  320. flags['add_memory_pressure'] = v_add_memory_pressure.value
  321. vlist.append(hop.inputconst(lltype.Void, flags))
  322. assert 1 <= hop.nb_args <= 2
  323. if hop.nb_args == 2:
  324. vlist.append(hop.inputarg(lltype.Signed, arg=1))
  325. opname += '_varsize'
  326. hop.has_implicit_exception(MemoryError) # record that we know about it
  327. hop.exception_is_here()
  328. return hop.genop(opname, vlist, resulttype = hop.r_result.lowleveltype)
  329. def rtype_free(hop, i_flavor, i_track_allocation=None):
  330. vlist = [hop.inputarg(hop.args_r[0], arg=0)]
  331. v_flavor, v_track_allocation = parse_kwds(hop,
  332. (i_flavor, lltype.Void),
  333. (i_track_allocation, None))
  334. #
  335. assert v_flavor is not None and v_flavor.value == 'raw'
  336. flags = {'flavor': 'raw'}
  337. if i_track_allocation is not None:
  338. flags['track_allocation'] = v_track_allocation.value
  339. vlist.append(hop.inputconst(lltype.Void, flags))
  340. #
  341. hop.exception_cannot_occur()
  342. hop.genop('free', vlist)
  343. def rtype_render_immortal(hop, i_track_allocation=None):
  344. vlist = [hop.inputarg(hop.args_r[0], arg=0)]
  345. v_track_allocation = parse_kwds(hop,
  346. (i_track_allocation, None))
  347. hop.exception_cannot_occur()
  348. if i_track_allocation is None or v_track_allocation.value:
  349. hop.genop('track_alloc_stop', vlist)
  350. def rtype_const_result(hop):
  351. hop.exception_cannot_occur()
  352. return hop.inputconst(hop.r_result.lowleveltype, hop.s_result.const)
  353. def rtype_cast_pointer(hop):
  354. assert hop.args_s[0].is_constant()
  355. assert isinstance(hop.args_r[1], rptr.PtrRepr)
  356. v_type, v_input = hop.inputargs(lltype.Void, hop.args_r[1])
  357. hop.exception_cannot_occur()
  358. return hop.genop('cast_pointer', [v_input], # v_type implicit in r_result
  359. resulttype = hop.r_result.lowleveltype)
  360. def rtype_cast_opaque_ptr(hop):
  361. assert hop.args_s[0].is_constant()
  362. assert isinstance(hop.args_r[1], rptr.PtrRepr)
  363. v_type, v_input = hop.inputargs(lltype.Void, hop.args_r[1])
  364. hop.exception_cannot_occur()
  365. return hop.genop('cast_opaque_ptr', [v_input], # v_type implicit in r_result
  366. resulttype = hop.r_result.lowleveltype)
  367. def rtype_direct_fieldptr(hop):
  368. assert isinstance(hop.args_r[0], rptr.PtrRepr)
  369. assert hop.args_s[1].is_constant()
  370. vlist = hop.inputargs(hop.args_r[0], lltype.Void)
  371. hop.exception_cannot_occur()
  372. return hop.genop('direct_fieldptr', vlist,
  373. resulttype=hop.r_result.lowleveltype)
  374. def rtype_direct_arrayitems(hop):
  375. assert isinstance(hop.args_r[0], rptr.PtrRepr)
  376. vlist = hop.inputargs(hop.args_r[0])
  377. hop.exception_cannot_occur()
  378. return hop.genop('direct_arrayitems', vlist,
  379. resulttype=hop.r_result.lowleveltype)
  380. def rtype_direct_ptradd(hop):
  381. assert isinstance(hop.args_r[0], rptr.PtrRepr)
  382. vlist = hop.inputargs(hop.args_r[0], lltype.Signed)
  383. hop.exception_cannot_occur()
  384. return hop.genop('direct_ptradd', vlist,
  385. resulttype=hop.r_result.lowleveltype)
  386. def rtype_cast_primitive(hop):
  387. assert hop.args_s[0].is_constant()
  388. TGT = hop.args_s[0].const
  389. v_type, v_value = hop.inputargs(lltype.Void, hop.args_r[1])
  390. hop.exception_cannot_occur()
  391. return gen_cast(hop.llops, TGT, v_value)
  392. _cast_to_Signed = {
  393. lltype.Signed: None,
  394. lltype.Bool: 'cast_bool_to_int',
  395. lltype.Char: 'cast_char_to_int',
  396. lltype.UniChar: 'cast_unichar_to_int',
  397. lltype.Float: 'cast_float_to_int',
  398. lltype.Unsigned: 'cast_uint_to_int',
  399. lltype.SignedLongLong: 'truncate_longlong_to_int',
  400. }
  401. _cast_from_Signed = {
  402. lltype.Signed: None,
  403. lltype.Bool: 'int_is_true',
  404. lltype.Char: 'cast_int_to_char',
  405. lltype.UniChar: 'cast_int_to_unichar',
  406. lltype.Float: 'cast_int_to_float',
  407. lltype.Unsigned: 'cast_int_to_uint',
  408. lltype.SignedLongLong: 'cast_int_to_longlong',
  409. }
  410. def gen_cast(llops, TGT, v_value):
  411. ORIG = v_value.concretetype
  412. if ORIG == TGT:
  413. return v_value
  414. if (isinstance(TGT, lltype.Primitive) and
  415. isinstance(ORIG, lltype.Primitive)):
  416. if ORIG in _cast_to_Signed and TGT in _cast_from_Signed:
  417. op = _cast_to_Signed[ORIG]
  418. if op:
  419. v_value = llops.genop(op, [v_value], resulttype=lltype.Signed)
  420. op = _cast_from_Signed[TGT]
  421. if op:
  422. v_value = llops.genop(op, [v_value], resulttype=TGT)
  423. return v_value
  424. else:
  425. # use the generic operation if there is no alternative
  426. return llops.genop('cast_primitive', [v_value], resulttype=TGT)
  427. elif isinstance(TGT, lltype.Ptr):
  428. if isinstance(ORIG, lltype.Ptr):
  429. if (isinstance(TGT.TO, lltype.OpaqueType) or
  430. isinstance(ORIG.TO, lltype.OpaqueType)):
  431. return llops.genop('cast_opaque_ptr', [v_value],
  432. resulttype = TGT)
  433. else:
  434. return llops.genop('cast_pointer', [v_value], resulttype = TGT)
  435. elif ORIG == llmemory.Address:
  436. return llops.genop('cast_adr_to_ptr', [v_value], resulttype = TGT)
  437. elif isinstance(ORIG, lltype.Primitive):
  438. v_value = gen_cast(llops, lltype.Signed, v_value)
  439. return llops.genop('cast_int_to_ptr', [v_value], resulttype=TGT)
  440. elif TGT == llmemory.Address and isinstance(ORIG, lltype.Ptr):
  441. return llops.genop('cast_ptr_to_adr', [v_value], resulttype = TGT)
  442. elif isinstance(TGT, lltype.Primitive):
  443. if isinstance(ORIG, lltype.Ptr):
  444. v_value = llops.genop('cast_ptr_to_int', [v_value],
  445. resulttype=lltype.Signed)
  446. elif ORIG == llmemory.Address:
  447. v_value = llops.genop('cast_adr_to_int', [v_value],
  448. resulttype=lltype.Signed)
  449. else:
  450. raise TypeError("don't know how to cast from %r to %r" % (ORIG,
  451. TGT))
  452. return gen_cast(llops, TGT, v_value)
  453. raise TypeError("don't know how to cast from %r to %r" % (ORIG, TGT))
  454. def rtype_cast_ptr_to_int(hop):
  455. assert isinstance(hop.args_r[0], rptr.PtrRepr)
  456. vlist = hop.inputargs(hop.args_r[0])
  457. hop.exception_cannot_occur()
  458. return hop.genop('cast_ptr_to_int', vlist,
  459. resulttype = lltype.Signed)
  460. def rtype_cast_int_to_ptr(hop):
  461. assert hop.args_s[0].is_constant()
  462. v_type, v_input = hop.inputargs(lltype.Void, lltype.Signed)
  463. hop.exception_cannot_occur()
  464. return hop.genop('cast_int_to_ptr', [v_input],
  465. resulttype = hop.r_result.lowleveltype)
  466. def rtype_identity_hash(hop):
  467. vlist = hop.inputargs(hop.args_r[0])
  468. hop.exception_cannot_occur()
  469. return hop.genop('gc_identityhash', vlist, resulttype=lltype.Signed)
  470. def rtype_runtime_type_info(hop):
  471. assert isinstance(hop.args_r[0], rptr.PtrRepr)
  472. vlist = hop.inputargs(hop.args_r[0])
  473. hop.exception_cannot_occur()
  474. return hop.genop('runtime_type_info', vlist,
  475. resulttype = hop.r_result.lowleveltype)
  476. BUILTIN_TYPER[lltype.malloc] = rtype_malloc
  477. BUILTIN_TYPER[lltype.free] = rtype_free
  478. BUILTIN_TYPER[lltype.render_immortal] = rtype_render_immortal
  479. BUILTIN_TYPER[lltype.cast_primitive] = rtype_cast_primitive
  480. BUILTIN_TYPER[lltype.cast_pointer] = rtype_cast_pointer
  481. BUILTIN_TYPER[lltype.cast_opaque_ptr] = rtype_cast_opaque_ptr
  482. BUILTIN_TYPER[lltype.direct_fieldptr] = rtype_direct_fieldptr
  483. BUILTIN_TYPER[lltype.direct_arrayitems] = rtype_direct_arrayitems
  484. BUILTIN_TYPER[lltype.direct_ptradd] = rtype_direct_ptradd
  485. BUILTIN_TYPER[lltype.cast_ptr_to_int] = rtype_cast_ptr_to_int
  486. BUILTIN_TYPER[lltype.cast_int_to_ptr] = rtype_cast_int_to_ptr
  487. BUILTIN_TYPER[lltype.typeOf] = rtype_const_result
  488. BUILTIN_TYPER[lltype.nullptr] = rtype_const_result
  489. BUILTIN_TYPER[lltype.identityhash] = rtype_identity_hash
  490. BUILTIN_TYPER[lltype.getRuntimeTypeInfo] = rtype_const_result
  491. BUILTIN_TYPER[lltype.Ptr] = rtype_const_result
  492. BUILTIN_TYPER[lltype.runtime_type_info] = rtype_runtime_type_info
  493. BUILTIN_TYPER[rarithmetic.intmask] = rtype_intmask
  494. BUILTIN_TYPER[rarithmetic.longlongmask] = rtype_longlongmask
  495. BUILTIN_TYPER[objectmodel.hlinvoke] = rtype_hlinvoke
  496. # _________________________________________________________________
  497. # memory addresses
  498. def rtype_raw_malloc(hop):
  499. v_size, = hop.inputargs(lltype.Signed)
  500. hop.exception_cannot_occur()
  501. return hop.genop('raw_malloc', [v_size], resulttype=llmemory.Address)
  502. def rtype_raw_malloc_usage(hop):
  503. v_size, = hop.inputargs(lltype.Signed)
  504. hop.exception_cannot_occur()
  505. return hop.genop('raw_malloc_usage', [v_size], resulttype=lltype.Signed)
  506. def rtype_raw_free(hop):
  507. s_addr = hop.args_s[0]
  508. if s_addr.is_null_address():
  509. raise TyperError("raw_free(x) where x is the constant NULL")
  510. v_addr, = hop.inputargs(llmemory.Address)
  511. hop.exception_cannot_occur()
  512. return hop.genop('raw_free', [v_addr])
  513. def rtype_raw_memcopy(hop):
  514. for s_addr in hop.args_s[:2]:
  515. if s_addr.is_null_address():
  516. raise TyperError("raw_memcopy() with a constant NULL")
  517. v_list = hop.inputargs(llmemory.Address, llmemory.Address, lltype.Signed)
  518. hop.exception_cannot_occur()
  519. return hop.genop('raw_memcopy', v_list)
  520. def rtype_raw_memclear(hop):
  521. s_addr = hop.args_s[0]
  522. if s_addr.is_null_address():
  523. raise TyperError("raw_memclear(x, n) where x is the constant NULL")
  524. v_list = hop.inputargs(llmemory.Address, lltype.Signed)
  525. hop.exception_cannot_occur()
  526. return hop.genop('raw_memclear', v_list)
  527. BUILTIN_TYPER[llmemory.raw_malloc] = rtype_raw_malloc
  528. BUILTIN_TYPER[llmemory.raw_malloc_usage] = rtype_raw_malloc_usage
  529. BUILTIN_TYPER[llmemory.raw_free] = rtype_raw_free
  530. BUILTIN_TYPER[llmemory.raw_memclear] = rtype_raw_memclear
  531. BUILTIN_TYPER[llmemory.raw_memcopy] = rtype_raw_memcopy
  532. def rtype_offsetof(hop):
  533. TYPE, field = hop.inputargs(lltype.Void, lltype.Void)
  534. hop.exception_cannot_occur()
  535. return hop.inputconst(lltype.Signed,
  536. llmemory.offsetof(TYPE.value, field.value))
  537. BUILTIN_TYPER[llmemory.offsetof] = rtype_offsetof
  538. # _________________________________________________________________
  539. # non-gc objects
  540. def rtype_free_non_gc_object(hop):
  541. hop.exception_cannot_occur()
  542. vinst, = hop.inputargs(hop.args_r[0])
  543. flavor = hop.args_r[0].gcflavor
  544. assert flavor != 'gc'
  545. flags = {'flavor': flavor}
  546. cflags = hop.inputconst(lltype.Void, flags)
  547. return hop.genop('free', [vinst, cflags])
  548. BUILTIN_TYPER[objectmodel.free_non_gc_object] = rtype_free_non_gc_object
  549. # keepalive_until_here
  550. def rtype_keepalive_until_here(hop):
  551. hop.exception_cannot_occur()
  552. for v in hop.args_v:
  553. hop.genop('keepalive', [v], resulttype=lltype.Void)
  554. return hop.inputconst(lltype.Void, None)
  555. BUILTIN_TYPER[objectmodel.keepalive_until_here] = rtype_keepalive_until_here
  556. def rtype_cast_ptr_to_adr(hop):
  557. vlist = hop.inputargs(hop.args_r[0])
  558. assert isinstance(vlist[0].concretetype, lltype.Ptr)
  559. hop.exception_cannot_occur()
  560. return hop.genop('cast_ptr_to_adr', vlist,
  561. resulttype = llmemory.Address)
  562. def rtype_cast_adr_to_ptr(hop):
  563. assert isinstance(hop.args_r[0], raddress.AddressRepr)
  564. adr, TYPE = hop.inputargs(hop.args_r[0], lltype.Void)
  565. hop.exception_cannot_occur()
  566. return hop.genop('cast_adr_to_ptr', [adr],
  567. resulttype = TYPE.value)
  568. def rtype_cast_adr_to_int(hop):
  569. assert isinstance(hop.args_r[0], raddress.AddressRepr)
  570. adr = hop.inputarg(hop.args_r[0], arg=0)
  571. if len(hop.args_s) == 1:
  572. mode = "emulated"
  573. else:
  574. mode = hop.args_s[1].const
  575. hop.exception_cannot_occur()
  576. return hop.genop('cast_adr_to_int',
  577. [adr, hop.inputconst(lltype.Void, mode)],
  578. resulttype = lltype.Signed)
  579. def rtype_cast_int_to_adr(hop):
  580. v_input, = hop.inputargs(lltype.Signed)
  581. hop.exception_cannot_occur()
  582. return hop.genop('cast_int_to_adr', [v_input],
  583. resulttype = llmemory.Address)
  584. BUILTIN_TYPER[llmemory.cast_ptr_to_adr] = rtype_cast_ptr_to_adr
  585. BUILTIN_TYPER[llmemory.cast_adr_to_ptr] = rtype_cast_adr_to_ptr
  586. BUILTIN_TYPER[llmemory.cast_adr_to_int] = rtype_cast_adr_to_int
  587. BUILTIN_TYPER[llmemory.cast_int_to_adr] = rtype_cast_int_to_adr