Debian Codegraph @

rpython/rtyper/rbuiltin.py

raw symbols SCIP
1 from collections import OrderedDict
2
3 from rpython.annotator import model as annmodel
4 from rpython.flowspace.model import Constant
5 from rpython.rlib import rarithmetic, objectmodel
6 from rpython.rtyper import raddress, rptr, extregistry, rrange
7 from rpython.rtyper.error import TyperError
8 from rpython.rtyper.lltypesystem import lltype, llmemory, rstr
9 from rpython.rtyper import rclass
10 from rpython.rtyper.rmodel import Repr
11 from rpython.tool.pairtype import pairtype
12
13
14 BUILTIN_TYPER = {}
15
16 def typer_for(func):
17 def wrapped(rtyper_func):
18 BUILTIN_TYPER[func] = rtyper_func
19 return rtyper_func
20 return wrapped
21
22
23 class __extend__(annmodel.SomeBuiltin):
24 def rtyper_makerepr(self, rtyper):
25 if not self.is_constant():
26 raise TyperError("non-constant built-in function!")
27 return BuiltinFunctionRepr(self.const)
28
29 def rtyper_makekey(self):
30 const = getattr(self, 'const', None)
31 if extregistry.is_registered(const):
32 const = extregistry.lookup(const)
33 return self.__class__, const
34
35 class __extend__(annmodel.SomeBuiltinMethod):
36 def rtyper_makerepr(self, rtyper):
37 assert self.methodname is not None
38 result = BuiltinMethodRepr(rtyper, self.s_self, self.methodname)
39 return result
40
41 def rtyper_makekey(self):
42 # NOTE: we hash by id of self.s_self here. This appears to be
43 # necessary because it ends up in hop.args_s[0] in the method call,
44 # and there is no telling what information the called
45 # rtype_method_xxx() will read from that hop.args_s[0].
46 # See test_method_join in test_rbuiltin.
47 # There is no problem with self.s_self being garbage-collected and
48 # its id reused, because the BuiltinMethodRepr keeps a reference
49 # to it.
50 return (self.__class__, self.methodname, id(self.s_self))
51
52 def call_args_expand(hop):
53 hop = hop.copy()
54 from rpython.annotator.argument import ArgumentsForTranslation
55 arguments = ArgumentsForTranslation.fromshape(
56 hop.args_s[1].const, # shape
57 range(hop.nb_args-2))
58 assert arguments.w_stararg is None
59 keywords = arguments.keywords
60 # prefix keyword arguments with 'i_'
61 kwds_i = {}
62 for key in keywords:
63 kwds_i['i_' + key] = keywords[key]
64 return hop, kwds_i
65
66
67 class BuiltinFunctionRepr(Repr):
68 lowleveltype = lltype.Void
69
70 def __init__(self, builtinfunc):
71 self.builtinfunc = builtinfunc
72
73 def findbltintyper(self, rtyper):
74 "Find the function to use to specialize calls to this built-in func."
75 try:
76 return BUILTIN_TYPER[self.builtinfunc]
77 except (KeyError, TypeError):
78 pass
79 if extregistry.is_registered(self.builtinfunc):
80 entry = extregistry.lookup(self.builtinfunc)
81 return entry.specialize_call
82 raise TyperError("don't know about built-in function %r" % (
83 self.builtinfunc,))
84
85 def _call(self, hop2, **kwds_i):
86 bltintyper = self.findbltintyper(hop2.rtyper)
87 hop2.llops._called_exception_is_here_or_cannot_occur = False
88 v_result = bltintyper(hop2, **kwds_i)
89 if not hop2.llops._called_exception_is_here_or_cannot_occur:
90 raise TyperError("missing hop.exception_cannot_occur() or "
91 "hop.exception_is_here() in %s" % bltintyper)
92 return v_result
93
94 def rtype_simple_call(self, hop):
95 hop2 = hop.copy()
96 hop2.r_s_popfirstarg()
97 return self._call(hop2)
98
99 def rtype_call_args(self, hop):
100 # calling a built-in function with keyword arguments:
101 # mostly for rpython.objectmodel.hint()
102 hop, kwds_i = call_args_expand(hop)
103
104 hop2 = hop.copy()
105 hop2.r_s_popfirstarg()
106 hop2.r_s_popfirstarg()
107 # the RPython-level keyword args are passed with an 'i_' prefix and
108 # the corresponding value is an *index* in the hop2 arguments,
109 # to be used with hop.inputarg(arg=..)
110 return self._call(hop2, **kwds_i)
111
112
113 class BuiltinMethodRepr(Repr):
114
115 def __init__(self, rtyper, s_self, methodname):
116 self.s_self = s_self
117 self.self_repr = rtyper.getrepr(s_self)
118 self.methodname = methodname
119 # methods of a known name are implemented as just their 'self'
120 self.lowleveltype = self.self_repr.lowleveltype
121
122 def convert_const(self, obj):
123 return self.self_repr.convert_const(obj.__self__)
124
125 def rtype_simple_call(self, hop):
126 # methods: look up the rtype_method_xxx()
127 name = 'rtype_method_' + self.methodname
128 try:
129 bltintyper = getattr(self.self_repr, name)
130 except AttributeError:
131 raise TyperError("missing %s.%s" % (
132 self.self_repr.__class__.__name__, name))
133 # hack based on the fact that 'lowleveltype == self_repr.lowleveltype'
134 hop2 = hop.copy()
135 assert hop2.args_r[0] is self
136 if isinstance(hop2.args_v[0], Constant):
137 c = hop2.args_v[0].value # get object from bound method
138 c = c.__self__
139 hop2.args_v[0] = Constant(c)
140 hop2.args_s[0] = self.s_self
141 hop2.args_r[0] = self.self_repr
142 return bltintyper(hop2)
143
144 class __extend__(pairtype(BuiltinMethodRepr, BuiltinMethodRepr)):
145 def convert_from_to((r_from, r_to), v, llops):
146 # convert between two MethodReprs only if they are about the same
147 # methodname. (Useful for the case r_from.s_self == r_to.s_self but
148 # r_from is not r_to.) See test_rbuiltin.test_method_repr.
149 if r_from.methodname != r_to.methodname:
150 return NotImplemented
151 return llops.convertvar(v, r_from.self_repr, r_to.self_repr)
152
153 def parse_kwds(hop, *argspec_i_r):
154 lst = [i for (i, r) in argspec_i_r if i is not None]
155 lst.sort()
156 if lst != range(hop.nb_args - len(lst), hop.nb_args):
157 raise TyperError("keyword args are expected to be at the end of "
158 "the 'hop' arg list")
159 result = []
160 for i, r in argspec_i_r:
161 if i is not None:
162 if r is None:
163 r = hop.args_r[i]
164 result.append(hop.inputarg(r, arg=i))
165 else:
166 result.append(None)
167 del hop.args_v[hop.nb_args - len(lst):]
168 return result
169
170 # ____________________________________________________________
171
172 @typer_for(bool)
173 def rtype_builtin_bool(hop):
174 # not called any more?
175 assert hop.nb_args == 1
176 return hop.args_r[0].rtype_bool(hop)
177
178 @typer_for(int)
179 def rtype_builtin_int(hop):
180 if isinstance(hop.args_s[0], annmodel.SomeString):
181 assert 1 <= hop.nb_args <= 2
182 return hop.args_r[0].rtype_int(hop)
183 assert hop.nb_args == 1
184 return hop.args_r[0].rtype_int(hop)
185
186 @typer_for(float)
187 def rtype_builtin_float(hop):
188 assert hop.nb_args == 1
189 return hop.args_r[0].rtype_float(hop)
190
191 @typer_for(chr)
192 def rtype_builtin_chr(hop):
193 assert hop.nb_args == 1
194 return hop.args_r[0].rtype_chr(hop)
195
196 @typer_for(unichr)
197 def rtype_builtin_unichr(hop):
198 assert hop.nb_args == 1
199 return hop.args_r[0].rtype_unichr(hop)
200
201 @typer_for(unicode)
202 def rtype_builtin_unicode(hop):
203 return hop.args_r[0].rtype_unicode(hop)
204
205 @typer_for(bytearray)
206 def rtype_builtin_bytearray(hop):
207 return hop.args_r[0].rtype_bytearray(hop)
208
209 @typer_for(list)
210 def rtype_builtin_list(hop):
211 return hop.args_r[0].rtype_bltn_list(hop)
212
213 #def rtype_builtin_range(hop): see rrange.py
214
215 #def rtype_builtin_xrange(hop): see rrange.py
216
217 #def rtype_builtin_enumerate(hop): see rrange.py
218
219 #def rtype_r_dict(hop): see rdict.py
220
221 @typer_for(rarithmetic.intmask)
222 def rtype_intmask(hop):
223 hop.exception_cannot_occur()
224 vlist = hop.inputargs(lltype.Signed)
225 return vlist[0]
226
227 @typer_for(rarithmetic.longlongmask)
228 def rtype_longlongmask(hop):
229 hop.exception_cannot_occur()
230 vlist = hop.inputargs(lltype.SignedLongLong)
231 return vlist[0]
232
233
234 @typer_for(min)
235 def rtype_builtin_min(hop):
236 v1, v2 = hop.inputargs(hop.r_result, hop.r_result)
237 hop.exception_cannot_occur()
238 return hop.gendirectcall(ll_min, v1, v2)
239
240 def ll_min(i1, i2):
241 if i1 < i2:
242 return i1
243 return i2
244
245
246 @typer_for(max)
247 def rtype_builtin_max(hop):
248 v1, v2 = hop.inputargs(hop.r_result, hop.r_result)
249 hop.exception_cannot_occur()
250 return hop.gendirectcall(ll_max, v1, v2)
251
252 def ll_max(i1, i2):
253 if i1 > i2:
254 return i1
255 return i2
256
257
258 @typer_for(reversed)
259 def rtype_builtin_reversed(hop):
260 hop.exception_cannot_occur()
261 return hop.r_result.newiter(hop)
262
263
264 @typer_for(getattr(object.__init__, 'im_func', object.__init__))
265 def rtype_object__init__(hop):
266 hop.exception_cannot_occur()
267
268
269 @typer_for(getattr(EnvironmentError.__init__, 'im_func',
270 EnvironmentError.__init__))
271 def rtype_EnvironmentError__init__(hop):
272 hop.exception_cannot_occur()
273 v_self = hop.args_v[0]
274 r_self = hop.args_r[0]
275 if hop.nb_args <= 2:
276 v_errno = hop.inputconst(lltype.Signed, 0)
277 if hop.nb_args == 2:
278 v_strerror = hop.inputarg(rstr.string_repr, arg=1)
279 r_self.setfield(v_self, 'strerror', v_strerror, hop.llops)
280 else:
281 v_errno = hop.inputarg(lltype.Signed, arg=1)
282 v_strerror = hop.inputarg(rstr.string_repr, arg=2)
283 r_self.setfield(v_self, 'strerror', v_strerror, hop.llops)
284 if hop.nb_args >= 4:
285 v_filename = hop.inputarg(rstr.string_repr, arg=3)
286 r_self.setfield(v_self, 'filename', v_filename, hop.llops)
287 r_self.setfield(v_self, 'errno', v_errno, hop.llops)
288
289 try:
290 WindowsError
291 except NameError:
292 pass
293 else:
294 @typer_for(
295 getattr(WindowsError.__init__, 'im_func', WindowsError.__init__))
296 def rtype_WindowsError__init__(hop):
297 hop.exception_cannot_occur()
298 if hop.nb_args == 2:
299 raise TyperError("WindowsError() should not be called with "
300 "a single argument")
301 if hop.nb_args >= 3:
302 v_self = hop.args_v[0]
303 r_self = hop.args_r[0]
304 v_error = hop.inputarg(lltype.Signed, arg=1)
305 r_self.setfield(v_self, 'winerror', v_error, hop.llops)
306
307 @typer_for(objectmodel.hlinvoke)
308 def rtype_hlinvoke(hop):
309 _, s_repr = hop.r_s_popfirstarg()
310 r_callable = s_repr.const
311
312 r_func, nimplicitarg = r_callable.get_r_implfunc()
313 s_callable = r_callable.get_s_callable()
314
315 nbargs = len(hop.args_s) - 1 + nimplicitarg
316 s_sigs = r_func.get_s_signatures((nbargs, (), False))
317 if len(s_sigs) != 1:
318 raise TyperError("cannot hlinvoke callable %r with not uniform"
319 "annotations: %r" % (r_callable,
320 s_sigs))
321 args_s, s_ret = s_sigs[0]
322 rinputs = [hop.rtyper.getrepr(s_obj) for s_obj in args_s]
323 rresult = hop.rtyper.getrepr(s_ret)
324
325 args_s = args_s[nimplicitarg:]
326 rinputs = rinputs[nimplicitarg:]
327
328 new_args_r = [r_callable] + rinputs
329
330 for i in range(len(new_args_r)):
331 assert hop.args_r[i].lowleveltype == new_args_r[i].lowleveltype
332
333 hop.args_r = new_args_r
334 hop.args_s = [s_callable] + args_s
335
336 hop.s_result = s_ret
337 assert hop.r_result.lowleveltype == rresult.lowleveltype
338 hop.r_result = rresult
339
340 return hop.dispatch()
341
342 typer_for(range)(rrange.rtype_builtin_range)
343 typer_for(xrange)(rrange.rtype_builtin_xrange)
344 typer_for(enumerate)(rrange.rtype_builtin_enumerate)
345
346
347 # annotation of low-level types
348
349 @typer_for(lltype.malloc)
350 def rtype_malloc(hop, i_flavor=None, i_immortal=None, i_zero=None,
351 i_track_allocation=None, i_add_memory_pressure=None, i_nonmovable=None):
352 assert hop.args_s[0].is_constant()
353 vlist = [hop.inputarg(lltype.Void, arg=0)]
354 opname = 'malloc'
355 kwds_v = parse_kwds(
356 hop,
357 (i_flavor, lltype.Void),
358 (i_immortal, None),
359 (i_zero, None),
360 (i_track_allocation, None),
361 (i_add_memory_pressure, None),
362 (i_nonmovable, None))
363 (v_flavor, v_immortal, v_zero, v_track_allocation,
364 v_add_memory_pressure, v_nonmovable) = kwds_v
365 flags = {'flavor': 'gc'}
366 if v_flavor is not None:
367 flags['flavor'] = v_flavor.value
368 if i_zero is not None:
369 flags['zero'] = v_zero.value
370 if i_track_allocation is not None:
371 flags['track_allocation'] = v_track_allocation.value
372 if i_add_memory_pressure is not None:
373 flags['add_memory_pressure'] = v_add_memory_pressure.value
374 if i_nonmovable is not None:
375 flags['nonmovable'] = v_nonmovable
376 vlist.append(hop.inputconst(lltype.Void, flags))
377
378 assert 1 <= hop.nb_args <= 2
379 if hop.nb_args == 2:
380 vlist.append(hop.inputarg(lltype.Signed, arg=1))
381 opname += '_varsize'
382
383 hop.has_implicit_exception(MemoryError) # record that we know about it
384 hop.exception_is_here()
385 return hop.genop(opname, vlist, resulttype=hop.r_result.lowleveltype)
386
387 @typer_for(lltype.free)
388 def rtype_free(hop, i_flavor, i_track_allocation=None):
389 vlist = [hop.inputarg(hop.args_r[0], arg=0)]
390 v_flavor, v_track_allocation = parse_kwds(hop,
391 (i_flavor, lltype.Void),
392 (i_track_allocation, None))
393 #
394 assert v_flavor is not None and v_flavor.value == 'raw'
395 flags = {'flavor': 'raw'}
396 if i_track_allocation is not None:
397 flags['track_allocation'] = v_track_allocation.value
398 vlist.append(hop.inputconst(lltype.Void, flags))
399 #
400 hop.exception_cannot_occur()
401 hop.genop('free', vlist)
402
403 @typer_for(lltype.render_immortal)
404 def rtype_render_immortal(hop, i_track_allocation=None):
405 vlist = [hop.inputarg(hop.args_r[0], arg=0)]
406 v_track_allocation = parse_kwds(hop,
407 (i_track_allocation, None))
408 hop.exception_cannot_occur()
409 if i_track_allocation is None or v_track_allocation.value:
410 hop.genop('track_alloc_stop', vlist)
411
412 @typer_for(lltype.typeOf)
413 @typer_for(lltype.nullptr)
414 @typer_for(lltype.getRuntimeTypeInfo)
415 @typer_for(lltype.Ptr)
416 def rtype_const_result(hop):
417 hop.exception_cannot_occur()
418 return hop.inputconst(hop.r_result.lowleveltype, hop.s_result.const)
419
420 @typer_for(lltype.cast_pointer)
421 def rtype_cast_pointer(hop):
422 assert hop.args_s[0].is_constant()
423 assert isinstance(hop.args_r[1], rptr.PtrRepr)
424 v_type, v_input = hop.inputargs(lltype.Void, hop.args_r[1])
425 hop.exception_cannot_occur()
426 return hop.genop('cast_pointer', [v_input], # v_type implicit in r_result
427 resulttype = hop.r_result.lowleveltype)
428
429 @typer_for(lltype.cast_opaque_ptr)
430 def rtype_cast_opaque_ptr(hop):
431 assert hop.args_s[0].is_constant()
432 assert isinstance(hop.args_r[1], rptr.PtrRepr)
433 v_type, v_input = hop.inputargs(lltype.Void, hop.args_r[1])
434 hop.exception_cannot_occur()
435 return hop.genop('cast_opaque_ptr', [v_input], # v_type implicit in r_result
436 resulttype = hop.r_result.lowleveltype)
437
438 @typer_for(lltype.length_of_simple_gcarray_from_opaque)
439 def rtype_length_of_simple_gcarray_from_opaque(hop):
440 assert isinstance(hop.args_r[0], rptr.PtrRepr)
441 v_opaque_ptr, = hop.inputargs(hop.args_r[0])
442 hop.exception_cannot_occur()
443 return hop.genop('length_of_simple_gcarray_from_opaque', [v_opaque_ptr],
444 resulttype = hop.r_result.lowleveltype)
445
446 @typer_for(lltype.direct_fieldptr)
447 def rtype_direct_fieldptr(hop):
448 assert isinstance(hop.args_r[0], rptr.PtrRepr)
449 assert hop.args_s[1].is_constant()
450 vlist = hop.inputargs(hop.args_r[0], lltype.Void)
451 hop.exception_cannot_occur()
452 return hop.genop('direct_fieldptr', vlist,
453 resulttype=hop.r_result.lowleveltype)
454
455 @typer_for(lltype.direct_arrayitems)
456 def rtype_direct_arrayitems(hop):
457 assert isinstance(hop.args_r[0], rptr.PtrRepr)
458 vlist = hop.inputargs(hop.args_r[0])
459 hop.exception_cannot_occur()
460 return hop.genop('direct_arrayitems', vlist,
461 resulttype=hop.r_result.lowleveltype)
462
463 @typer_for(lltype.direct_ptradd)
464 def rtype_direct_ptradd(hop):
465 assert isinstance(hop.args_r[0], rptr.PtrRepr)
466 vlist = hop.inputargs(hop.args_r[0], lltype.Signed)
467 hop.exception_cannot_occur()
468 return hop.genop('direct_ptradd', vlist,
469 resulttype=hop.r_result.lowleveltype)
470
471 @typer_for(lltype.cast_primitive)
472 def rtype_cast_primitive(hop):
473 assert hop.args_s[0].is_constant()
474 TGT = hop.args_s[0].const
475 v_type, v_value = hop.inputargs(lltype.Void, hop.args_r[1])
476 hop.exception_cannot_occur()
477 return gen_cast(hop.llops, TGT, v_value)
478
479 _cast_to_Signed = {
480 lltype.Signed: None,
481 lltype.Bool: 'cast_bool_to_int',
482 lltype.Char: 'cast_char_to_int',
483 lltype.UniChar: 'cast_unichar_to_int',
484 lltype.Float: 'cast_float_to_int',
485 lltype.Unsigned: 'cast_uint_to_int',
486 lltype.SignedLongLong: 'truncate_longlong_to_int',
487 }
488 _cast_from_Signed = {
489 lltype.Signed: None,
490 lltype.Char: 'cast_int_to_char',
491 lltype.UniChar: 'cast_int_to_unichar',
492 lltype.Float: 'cast_int_to_float',
493 lltype.Unsigned: 'cast_int_to_uint',
494 lltype.SignedLongLong: 'cast_int_to_longlong',
495 }
496
497 def gen_cast(llops, TGT, v_value):
498 ORIG = v_value.concretetype
499 if ORIG == TGT:
500 return v_value
501 if (isinstance(TGT, lltype.Primitive) and
502 isinstance(ORIG, lltype.Primitive)):
503 if ORIG in _cast_to_Signed and TGT in _cast_from_Signed:
504 op = _cast_to_Signed[ORIG]
505 if op:
506 v_value = llops.genop(op, [v_value], resulttype=lltype.Signed)
507 op = _cast_from_Signed[TGT]
508 if op:
509 v_value = llops.genop(op, [v_value], resulttype=TGT)
510 return v_value
511 elif ORIG is lltype.Signed and TGT is lltype.Bool:
512 return llops.genop('int_is_true', [v_value], resulttype=lltype.Bool)
513 else:
514 # use the generic operation if there is no alternative
515 return llops.genop('cast_primitive', [v_value], resulttype=TGT)
516 elif isinstance(TGT, lltype.Ptr):
517 if isinstance(ORIG, lltype.Ptr):
518 if (isinstance(TGT.TO, lltype.OpaqueType) or
519 isinstance(ORIG.TO, lltype.OpaqueType)):
520 return llops.genop('cast_opaque_ptr', [v_value], resulttype=TGT)
521 else:
522 return llops.genop('cast_pointer', [v_value], resulttype=TGT)
523 elif ORIG == llmemory.Address:
524 return llops.genop('cast_adr_to_ptr', [v_value], resulttype=TGT)
525 elif isinstance(ORIG, lltype.Primitive):
526 v_value = gen_cast(llops, lltype.Signed, v_value)
527 return llops.genop('cast_int_to_ptr', [v_value], resulttype=TGT)
528 elif TGT == llmemory.Address and isinstance(ORIG, lltype.Ptr):
529 return llops.genop('cast_ptr_to_adr', [v_value], resulttype=TGT)
530 elif isinstance(TGT, lltype.Primitive):
531 if isinstance(ORIG, lltype.Ptr):
532 v_value = llops.genop('cast_ptr_to_int', [v_value],
533 resulttype=lltype.Signed)
534 elif ORIG == llmemory.Address:
535 v_value = llops.genop('cast_adr_to_int', [v_value],
536 resulttype=lltype.Signed)
537 else:
538 raise TypeError("don't know how to cast from %r to %r" % (ORIG,
539 TGT))
540 return gen_cast(llops, TGT, v_value)
541 raise TypeError("don't know how to cast from %r to %r" % (ORIG, TGT))
542
543 @typer_for(lltype.cast_ptr_to_int)
544 def rtype_cast_ptr_to_int(hop):
545 assert isinstance(hop.args_r[0], rptr.PtrRepr)
546 vlist = hop.inputargs(hop.args_r[0])
547 hop.exception_cannot_occur()
548 return hop.genop('cast_ptr_to_int', vlist,
549 resulttype=lltype.Signed)
550
551 @typer_for(lltype.cast_int_to_ptr)
552 def rtype_cast_int_to_ptr(hop):
553 assert hop.args_s[0].is_constant()
554 v_type, v_input = hop.inputargs(lltype.Void, lltype.Signed)
555 hop.exception_cannot_occur()
556 return hop.genop('cast_int_to_ptr', [v_input],
557 resulttype=hop.r_result.lowleveltype)
558
559 @typer_for(lltype.identityhash)
560 def rtype_identity_hash(hop):
561 vlist = hop.inputargs(hop.args_r[0])
562 hop.exception_cannot_occur()
563 return hop.genop('gc_identityhash', vlist, resulttype=lltype.Signed)
564
565 @typer_for(lltype.runtime_type_info)
566 def rtype_runtime_type_info(hop):
567 assert isinstance(hop.args_r[0], rptr.PtrRepr)
568 vlist = hop.inputargs(hop.args_r[0])
569 hop.exception_cannot_occur()
570 return hop.genop('runtime_type_info', vlist,
571 resulttype=hop.r_result.lowleveltype)
572
573
574 # _________________________________________________________________
575 # memory addresses
576
577 @typer_for(llmemory.raw_malloc)
578 def rtype_raw_malloc(hop, i_zero=None):
579 v_size = hop.inputarg(lltype.Signed, arg=0)
580 v_zero, = parse_kwds(hop, (i_zero, None))
581 if v_zero is None:
582 v_zero = hop.inputconst(lltype.Bool, False)
583 hop.exception_cannot_occur()
584 return hop.genop('raw_malloc', [v_size, v_zero],
585 resulttype=llmemory.Address)
586
587 @typer_for(llmemory.raw_malloc_usage)
588 def rtype_raw_malloc_usage(hop):
589 v_size, = hop.inputargs(lltype.Signed)
590 hop.exception_cannot_occur()
591 return hop.genop('raw_malloc_usage', [v_size], resulttype=lltype.Signed)
592
593 @typer_for(llmemory.raw_free)
594 def rtype_raw_free(hop):
595 s_addr = hop.args_s[0]
596 if s_addr.is_null_address():
597 raise TyperError("raw_free(x) where x is the constant NULL")
598 v_addr, = hop.inputargs(llmemory.Address)
599 hop.exception_cannot_occur()
600 return hop.genop('raw_free', [v_addr])
601
602 @typer_for(llmemory.raw_memcopy)
603 def rtype_raw_memcopy(hop):
604 for s_addr in hop.args_s[:2]:
605 if s_addr.is_null_address():
606 raise TyperError("raw_memcopy() with a constant NULL")
607 v_list = hop.inputargs(llmemory.Address, llmemory.Address, lltype.Signed)
608 hop.exception_cannot_occur()
609 return hop.genop('raw_memcopy', v_list)
610
611 @typer_for(llmemory.raw_memclear)
612 def rtype_raw_memclear(hop):
613 s_addr = hop.args_s[0]
614 if s_addr.is_null_address():
615 raise TyperError("raw_memclear(x, n) where x is the constant NULL")
616 v_list = hop.inputargs(llmemory.Address, lltype.Signed)
617 hop.exception_cannot_occur()
618 return hop.genop('raw_memclear', v_list)
619
620
621 @typer_for(llmemory.offsetof)
622 def rtype_offsetof(hop):
623 TYPE, field = hop.inputargs(lltype.Void, lltype.Void)
624 hop.exception_cannot_occur()
625 return hop.inputconst(lltype.Signed,
626 llmemory.offsetof(TYPE.value, field.value))
627
628
629 # _________________________________________________________________
630 # non-gc objects
631
632 @typer_for(objectmodel.free_non_gc_object)
633 def rtype_free_non_gc_object(hop):
634 hop.exception_cannot_occur()
635 vinst, = hop.inputargs(hop.args_r[0])
636 flavor = hop.args_r[0].gcflavor
637 assert flavor != 'gc'
638 flags = {'flavor': flavor}
639 cflags = hop.inputconst(lltype.Void, flags)
640 return hop.genop('free', [vinst, cflags])
641
642
643 @typer_for(objectmodel.keepalive_until_here)
644 def rtype_keepalive_until_here(hop):
645 hop.exception_cannot_occur()
646 for v in hop.args_v:
647 hop.genop('keepalive', [v], resulttype=lltype.Void)
648 return hop.inputconst(lltype.Void, None)
649
650
651 @typer_for(llmemory.cast_ptr_to_adr)
652 def rtype_cast_ptr_to_adr(hop):
653 vlist = hop.inputargs(hop.args_r[0])
654 assert isinstance(vlist[0].concretetype, lltype.Ptr)
655 hop.exception_cannot_occur()
656 return hop.genop('cast_ptr_to_adr', vlist,
657 resulttype=llmemory.Address)
658
659 @typer_for(llmemory.cast_adr_to_ptr)
660 def rtype_cast_adr_to_ptr(hop):
661 assert isinstance(hop.args_r[0], raddress.AddressRepr)
662 adr, TYPE = hop.inputargs(hop.args_r[0], lltype.Void)
663 hop.exception_cannot_occur()
664 return hop.genop('cast_adr_to_ptr', [adr],
665 resulttype=TYPE.value)
666
667 @typer_for(llmemory.cast_adr_to_int)
668 def rtype_cast_adr_to_int(hop):
669 assert isinstance(hop.args_r[0], raddress.AddressRepr)
670 adr = hop.inputarg(hop.args_r[0], arg=0)
671 if len(hop.args_s) == 1:
672 mode = "emulated"
673 else:
674 mode = hop.args_s[1].const
675 hop.exception_cannot_occur()
676 return hop.genop('cast_adr_to_int',
677 [adr, hop.inputconst(lltype.Void, mode)],
678 resulttype=lltype.Signed)
679
680 @typer_for(llmemory.cast_int_to_adr)
681 def rtype_cast_int_to_adr(hop):
682 v_input, = hop.inputargs(lltype.Signed)
683 hop.exception_cannot_occur()
684 return hop.genop('cast_int_to_adr', [v_input],
685 resulttype=llmemory.Address)
686
687
688 @typer_for(objectmodel.instantiate)
689 def rtype_instantiate(hop, i_nonmovable=None):
690 hop.exception_cannot_occur()
691 s_class = hop.args_s[0]
692 assert isinstance(s_class, annmodel.SomePBC)
693 v_nonmovable, = parse_kwds(hop, (i_nonmovable, None))
694 nonmovable = (i_nonmovable is not None and v_nonmovable.value)
695 if len(s_class.descriptions) != 1:
696 # instantiate() on a variable class
697 if nonmovable:
698 raise TyperError("instantiate(x, nonmovable=True) cannot be used "
699 "if x is not a constant class")
700 vtypeptr, = hop.inputargs(rclass.get_type_repr(hop.rtyper))
701 r_class = hop.args_r[0]
702 return r_class._instantiate_runtime_class(hop, vtypeptr,
703 hop.r_result.lowleveltype)
704 classdef = s_class.any_description().getuniqueclassdef()
705 return rclass.rtype_new_instance(hop.rtyper, classdef, hop.llops,
706 nonmovable=nonmovable)
707
708
709 @typer_for(hasattr)
710 def rtype_builtin_hasattr(hop):
711 hop.exception_cannot_occur()
712 if hop.s_result.is_constant():
713 return hop.inputconst(lltype.Bool, hop.s_result.const)
714
715 raise TyperError("hasattr is only suported on a constant")
716
717 @typer_for(OrderedDict)
718 @typer_for(objectmodel.r_dict)
719 @typer_for(objectmodel.r_ordereddict)
720 def rtype_dict_constructor(hop, i_force_non_null=None, i_simple_hash_eq=None):
721 # 'i_force_non_null' and 'i_simple_hash_eq' are ignored here; if they have any
722 # effect, it has already been applied to 'hop.r_result'
723 hop.exception_cannot_occur()
724 r_dict = hop.r_result
725 cDICT = hop.inputconst(lltype.Void, r_dict.DICT)
726 v_result = hop.gendirectcall(r_dict.ll_newdict, cDICT)
727 if r_dict.custom_eq_hash:
728 v_eqfn = hop.inputarg(r_dict.r_rdict_eqfn, arg=0)
729 v_hashfn = hop.inputarg(r_dict.r_rdict_hashfn, arg=1)
730 if r_dict.r_rdict_eqfn.lowleveltype != lltype.Void:
731 cname = hop.inputconst(lltype.Void, 'fnkeyeq')
732 hop.genop('setfield', [v_result, cname, v_eqfn])
733 if r_dict.r_rdict_hashfn.lowleveltype != lltype.Void:
734 cname = hop.inputconst(lltype.Void, 'fnkeyhash')
735 hop.genop('setfield', [v_result, cname, v_hashfn])
736 return v_result
737
738 # _________________________________________________________________
739 # weakrefs
740
741 import weakref
742 from rpython.rtyper.lltypesystem import llmemory
743
744 @typer_for(llmemory.weakref_create)
745 @typer_for(weakref.ref)
746 def rtype_weakref_create(hop):
747 from rpython.rtyper.rweakref import BaseWeakRefRepr
748
749 v_inst, = hop.inputargs(hop.args_r[0])
750 hop.exception_cannot_occur()
751 if isinstance(hop.r_result, BaseWeakRefRepr):
752 return hop.r_result._weakref_create(hop, v_inst)
753 else:
754 # low-level <PtrRepr * WeakRef>
755 assert hop.rtyper.getconfig().translation.rweakref
756 return hop.genop('weakref_create', [v_inst],
757 resulttype=llmemory.WeakRefPtr)
758
759 @typer_for(llmemory.weakref_deref)
760 def rtype_weakref_deref(hop):
761 assert hop.rtyper.getconfig().translation.rweakref
762 c_ptrtype, v_wref = hop.inputargs(lltype.Void, hop.args_r[1])
763 assert v_wref.concretetype == llmemory.WeakRefPtr
764 hop.exception_cannot_occur()
765 return hop.genop('weakref_deref', [v_wref], resulttype=c_ptrtype.value)
766
767 @typer_for(llmemory.cast_ptr_to_weakrefptr)
768 def rtype_cast_ptr_to_weakrefptr(hop):
769 assert hop.rtyper.getconfig().translation.rweakref
770 vlist = hop.inputargs(hop.args_r[0])
771 hop.exception_cannot_occur()
772 return hop.genop('cast_ptr_to_weakrefptr', vlist,
773 resulttype=llmemory.WeakRefPtr)
774
775 @typer_for(llmemory.cast_weakrefptr_to_ptr)
776 def rtype_cast_weakrefptr_to_ptr(hop):
777 assert hop.rtyper.getconfig().translation.rweakref
778 c_ptrtype, v_wref = hop.inputargs(lltype.Void, hop.args_r[1])
779 assert v_wref.concretetype == llmemory.WeakRefPtr
780 hop.exception_cannot_occur()
781 return hop.genop('cast_weakrefptr_to_ptr', [v_wref],
782 resulttype=c_ptrtype.value)