Debian Codegraph @

rpython/jit/metainterp/blackhole.py

raw symbols SCIP
1 from __future__ import print_function
2
3 from rpython.jit.codewriter import longlong
4 from rpython.jit.codewriter.jitcode import JitCode, SwitchDictDescr
5 from rpython.jit.codewriter.liveness import OFFSET_SIZE
6 from rpython.jit.metainterp.compile import ResumeAtPositionDescr
7 from rpython.jit.metainterp.jitexc import get_llexception, reraise
8 from rpython.jit.metainterp import jitexc
9 from rpython.jit.metainterp.history import MissingValue
10 from rpython.jit.metainterp.support import (
11 adr2int, int2adr, ptr2int, int_signext)
12 from rpython.rlib import longlong2float
13 from rpython.rlib.debug import ll_assert, make_sure_not_resized
14 from rpython.rlib.debug import check_annotation
15 from rpython.rlib.objectmodel import we_are_translated, specialize
16 from rpython.rlib.rarithmetic import (intmask, LONG_BIT, r_uint, ovfcheck,
17 uint_mul_high)
18 from rpython.rlib.unroll import unrolling_iterable
19 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
20 from rpython.rtyper import rclass
21 from rpython.rtyper.lltypesystem.lloperation import llop
22 from rpython.rlib.debug import debug_start, debug_stop, debug_print
23 from rpython.rlib.jit_libffi import CIF_DESCRIPTION_P
24
25 SIZE_LIVE_OP = OFFSET_SIZE + 1
26
27 def arguments(*argtypes, **kwds):
28 resulttype = kwds.pop('returns', None)
29 assert not kwds
30 def decorate(function):
31 function.argtypes = argtypes
32 function.resulttype = resulttype
33 return function
34 return decorate
35
36 LONGLONG_TYPECODE = 'i' if longlong.is_64_bit else 'f'
37
38
39 class LeaveFrame(jitexc.JitException):
40 pass
41
42 def signedord(c):
43 value = ord(c)
44 value = intmask(value << (LONG_BIT-8)) >> (LONG_BIT-8)
45 return value
46
47 NULL = lltype.nullptr(llmemory.GCREF.TO)
48
49 # ____________________________________________________________
50
51
52 class BlackholeInterpBuilder(object):
53 verbose = True
54
55 def __init__(self, codewriter, metainterp_sd=None):
56 self.cpu = codewriter.cpu
57 asm = codewriter.assembler
58 self.setup_insns(asm.insns)
59 self.setup_descrs(asm.descrs)
60 self.metainterp_sd = metainterp_sd
61 self.blackholeinterps = None
62
63 def _cleanup_(self):
64 self.blackholeinterps = None
65
66 def setup_insns(self, insns):
67 assert len(insns) <= 256, "too many instructions!"
68 self._insns = [None] * len(insns)
69 for key, value in insns.items():
70 assert self._insns[value] is None
71 self._insns[value] = key
72 self.op_live = insns.get('live/', -1)
73 self.op_catch_exception = insns.get('catch_exception/L', -1)
74 self.op_rvmprof_code = insns.get('rvmprof_code/ii', -1)
75 #
76 all_funcs = []
77 for key in self._insns:
78 assert key.count('/') == 1, "bad key: %r" % (key,)
79 name, argcodes = key.split('/')
80 all_funcs.append(self._get_method(name, argcodes))
81 all_funcs = unrolling_iterable(enumerate(all_funcs))
82 #
83 def dispatch_loop(self, code, position):
84 assert position >= 0
85 while True:
86 if (not we_are_translated()
87 and self.jitcode._startpoints is not None):
88 assert position in self.jitcode._startpoints, (
89 "the current position %d is in the middle of "
90 "an instruction!" % position)
91 opcode = ord(code[position])
92 position += 1
93 for i, func in all_funcs:
94 if opcode == i:
95 position = func(self, code, position)
96 break
97 else:
98 raise AssertionError("bad opcode")
99 dispatch_loop._dont_inline_ = True
100 self.dispatch_loop = dispatch_loop
101
102 def setup_descrs(self, descrs):
103 self.descrs = descrs
104
105 def _get_method(self, name, argcodes):
106 #
107 def handler(self, code, position):
108 assert position >= 0
109 args = ()
110 next_argcode = 0
111 for argtype in argtypes:
112 if argtype == 'i' or argtype == 'r' or argtype == 'f':
113 # if argtype is 'i', then argcode can be 'i' or 'c';
114 # 'c' stands for a single signed byte that gives the
115 # value of a small constant.
116 argcode = argcodes[next_argcode]
117 next_argcode = next_argcode + 1
118 if argcode == 'i':
119 assert argtype == 'i'
120 value = self.registers_i[ord(code[position])]
121 elif argcode == 'c':
122 assert argtype == 'i'
123 value = signedord(code[position])
124 elif argcode == 'r':
125 assert argtype == 'r'
126 value = self.registers_r[ord(code[position])]
127 elif argcode == 'f':
128 assert argtype == 'f'
129 value = self.registers_f[ord(code[position])]
130 else:
131 raise AssertionError("bad argcode")
132 position += 1
133 elif argtype == 'L':
134 # argcode should be 'L' too
135 assert argcodes[next_argcode] == 'L'
136 next_argcode = next_argcode + 1
137 value = ord(code[position]) | (ord(code[position+1])<<8)
138 position += 2
139 elif argtype == 'I' or argtype == 'R' or argtype == 'F':
140 assert argcodes[next_argcode] == argtype
141 next_argcode = next_argcode + 1
142 value = self._get_list_of_values(code, position, argtype)
143 position += 1 + len(value)
144 elif argtype == 'self':
145 value = self
146 elif argtype == 'cpu':
147 value = self.cpu
148 elif argtype == 'pc':
149 value = position
150 elif argtype == 'd' or argtype == 'j':
151 assert argcodes[next_argcode] == 'd'
152 next_argcode = next_argcode + 1
153 index = ord(code[position]) | (ord(code[position+1])<<8)
154 value = self.descrs[index]
155 if argtype == 'j':
156 assert isinstance(value, JitCode)
157 position += 2
158 else:
159 raise AssertionError("bad argtype: %r" % (argtype,))
160 if not we_are_translated():
161 assert not isinstance(value, MissingValue), (
162 name, self.jitcode, position)
163 args = args + (value,)
164
165 if verbose and not we_are_translated():
166 print('\tbh:', name, list(args), end=" ")
167
168 # call the method bhimpl_xxx()
169 try:
170 result = unboundmethod(*args)
171 except Exception as e:
172 if verbose and not we_are_translated():
173 print('-> %s!' % (e.__class__.__name__,))
174 if resulttype == 'i' or resulttype == 'r' or resulttype == 'f':
175 position += 1
176 self.position = position
177 raise
178
179 if verbose and not we_are_translated():
180 if result is None:
181 print()
182 else:
183 print('->', result)
184
185 if resulttype == 'i':
186 # argcode should be 'i' too
187 assert argcodes[next_argcode] == '>'
188 assert argcodes[next_argcode + 1] == 'i'
189 next_argcode = next_argcode + 2
190 if lltype.typeOf(result) is lltype.Bool:
191 result = int(result)
192 assert lltype.typeOf(result) is lltype.Signed
193 self.registers_i[ord(code[position])] = plain_int(result)
194 position += 1
195 elif resulttype == 'r':
196 # argcode should be 'r' too
197 assert argcodes[next_argcode] == '>'
198 assert argcodes[next_argcode + 1] == 'r'
199 next_argcode = next_argcode + 2
200 assert lltype.typeOf(result) == llmemory.GCREF
201 self.registers_r[ord(code[position])] = result
202 position += 1
203 elif resulttype == 'f':
204 # argcode should be 'f' too
205 assert argcodes[next_argcode] == '>'
206 assert argcodes[next_argcode + 1] == 'f'
207 next_argcode = next_argcode + 2
208 assert lltype.typeOf(result) is longlong.FLOATSTORAGE
209 self.registers_f[ord(code[position])] = result
210 position += 1
211 elif resulttype == "iL":
212 result, new_position = result
213 if new_position != -1:
214 position = new_position
215 next_argcode = next_argcode + 2
216 else:
217 assert argcodes[next_argcode] == '>'
218 assert argcodes[next_argcode + 1] == 'i'
219 next_argcode = next_argcode + 2
220 if lltype.typeOf(result) is lltype.Bool:
221 result = int(result)
222 assert lltype.typeOf(result) is lltype.Signed
223 self.registers_i[ord(code[position])] = plain_int(result)
224 position += 1
225 elif resulttype == 'L':
226 assert result >= 0
227 position = result
228 else:
229 assert resulttype is None
230 assert result is None
231 assert next_argcode == len(argcodes)
232 return position
233 #
234 # Get the bhimpl_xxx method. If we get an AttributeError here,
235 # it means that either the implementation is missing, or that it
236 # should not appear here at all but instead be transformed away
237 # by codewriter/jtransform.py.
238 unboundmethod = getattr(BlackholeInterpreter, 'bhimpl_' + name).im_func
239 verbose = self.verbose
240 argtypes = unrolling_iterable(unboundmethod.argtypes)
241 resulttype = unboundmethod.resulttype
242 handler.__name__ = 'handler_' + name
243 return handler
244
245 def acquire_interp(self):
246 res = self.blackholeinterps
247 if res is not None:
248 self.blackholeinterps = res.back
249 return res
250 else:
251 return BlackholeInterpreter(self)
252
253 def release_interp(self, interp):
254 interp.cleanup_registers()
255 interp.back = self.blackholeinterps
256 self.blackholeinterps = interp
257
258 def check_shift_count(b):
259 if not we_are_translated():
260 if b < 0 or b >= LONG_BIT:
261 raise ValueError("Shift count, %d, not in valid range, 0 .. %d." % (b, LONG_BIT-1))
262
263 def check_list_of_plain_integers(s_arg, bookkeeper):
264 """Check that 'BlackhopeInterpreter.registers_i' is annotated as a
265 non-resizable list of plain integers (and not r_int's for example)."""
266 from rpython.annotator import model as annmodel
267 assert isinstance(s_arg, annmodel.SomeList)
268 s_arg.listdef.never_resize()
269 s_value = s_arg.listdef.listitem.s_value
270 assert s_value.knowntype is int or isinstance(s_value, annmodel.SomeImpossibleValue)
271
272 def _check_int(s_arg, bookkeeper):
273 assert s_arg.knowntype is int
274
275 def plain_int(x):
276 """Check that 'x' is annotated as a plain integer (and not r_int)"""
277 check_annotation(x, _check_int)
278 return x
279
280 EMPTY_LIST_I = [] # shared
281
282 class BlackholeInterpreter(object):
283
284 def __init__(self, builder):
285 self.builder = builder
286 self.cpu = builder.cpu
287 self.dispatch_loop = builder.dispatch_loop
288 self.descrs = builder.descrs
289 self.op_catch_exception = builder.op_catch_exception
290 self.op_rvmprof_code = builder.op_rvmprof_code
291 #
292 if we_are_translated():
293 default_i = 0
294 default_r = NULL
295 default_f = longlong.ZEROF
296 else:
297 default_i = MissingValue()
298 default_r = MissingValue()
299 default_f = MissingValue()
300 self.registers_i = EMPTY_LIST_I
301 self.registers_r = None
302 self.registers_f = None
303 self.tmpreg_i = default_i
304 self.tmpreg_r = default_r
305 self.tmpreg_f = default_f
306 self.jitcode = None
307 check_annotation(self.registers_i, check_list_of_plain_integers)
308
309 def __repr__(self):
310 return '<BHInterp %s>' % self.jitcode
311
312 def setposition(self, jitcode, position):
313 num_regs_and_consts_i = jitcode.num_regs_and_consts_i()
314 num_regs_and_consts_r = jitcode.num_regs_and_consts_r()
315 num_regs_and_consts_f = jitcode.num_regs_and_consts_f()
316 if we_are_translated():
317 default_i = 0
318 default_r = NULL
319 default_f = longlong.ZEROF
320 else:
321 default_i = MissingValue()
322 default_r = MissingValue()
323 default_f = MissingValue()
324 if num_regs_and_consts_i:
325 if self.registers_i is None or len(self.registers_i) < num_regs_and_consts_i:
326 self.registers_i = [default_i] * num_regs_and_consts_i
327 self.copy_constants(self.registers_i, jitcode.constants_i, jitcode.num_regs_i())
328 if num_regs_and_consts_r:
329 if self.registers_r is None or len(self.registers_r) < num_regs_and_consts_r:
330 self.registers_r = [default_r] * num_regs_and_consts_r
331 self.copy_constants(self.registers_r, jitcode.constants_r, jitcode.num_regs_r())
332 if num_regs_and_consts_f:
333 if self.registers_f is None or len(self.registers_f) < num_regs_and_consts_f:
334 self.registers_f = [default_f] * num_regs_and_consts_f
335 self.copy_constants(self.registers_f, jitcode.constants_f, jitcode.num_regs_f())
336 self.jitcode = jitcode
337 self.position = position
338
339 def setarg_i(self, index, value):
340 assert lltype.typeOf(value) is lltype.Signed
341 self.registers_i[index] = plain_int(value)
342
343 def setarg_r(self, index, value):
344 assert lltype.typeOf(value) == llmemory.GCREF
345 self.registers_r[index] = value
346
347 def setarg_f(self, index, value):
348 assert lltype.typeOf(value) is longlong.FLOATSTORAGE
349 self.registers_f[index] = value
350
351 def run(self):
352 while True:
353 try:
354 self.dispatch_loop(self, self.jitcode.code, self.position)
355 except LeaveFrame:
356 break
357 except jitexc.JitException:
358 raise # go through
359 except Exception as e:
360 lle = get_llexception(self.cpu, e)
361 self.handle_exception_in_frame(lle)
362
363 def get_tmpreg_i(self):
364 return self.tmpreg_i
365
366 def get_tmpreg_r(self):
367 result = self.tmpreg_r
368 if we_are_translated():
369 self.tmpreg_r = NULL
370 else:
371 del self.tmpreg_r
372 return result
373
374 def get_tmpreg_f(self):
375 return self.tmpreg_f
376
377 def _final_result_anytype(self):
378 "NOT_RPYTHON"
379 if self._return_type == 'i': return self.get_tmpreg_i()
380 if self._return_type == 'r': return self.get_tmpreg_r()
381 if self._return_type == 'f': return self.get_tmpreg_f()
382 if self._return_type == 'v': return None
383 raise ValueError(self._return_type)
384
385 def cleanup_registers(self):
386 # To avoid keeping references alive, this cleans up the registers_r.
387 # It does not clear the references set by copy_constants(), but
388 # these are all prebuilt constants anyway.
389 for i in range(self.jitcode.num_regs_r()):
390 self.registers_r[i] = NULL
391 self.exception_last_value = lltype.nullptr(rclass.OBJECT)
392
393 def get_current_position_info(self):
394 return self.jitcode.get_live_vars_info(self.position, self.builder.op_live)
395
396 def handle_exception_in_frame(self, e):
397 # This frame raises an exception. First try to see if
398 # the exception is handled in the frame itself.
399 code = self.jitcode.code
400 position = self.position
401 if position < len(code):
402 opcode = ord(code[position])
403 if opcode == self.builder.op_live: # live, skip it
404 position += SIZE_LIVE_OP
405 opcode = ord(code[position])
406 if opcode == self.op_catch_exception:
407 # store the exception on 'self', and jump to the handler
408 self.exception_last_value = e
409 target = ord(code[position+1]) | (ord(code[position+2])<<8)
410 self.position = target
411 return
412 if opcode == self.op_rvmprof_code:
413 # call the 'jit_rvmprof_code(1)' for rvmprof, but then
414 # continue popping frames. Decode the 'rvmprof_code' insn
415 # manually here.
416 from rpython.rlib.rvmprof import cintf
417 arg1 = self.registers_i[ord(code[position + 1])]
418 arg2 = self.registers_i[ord(code[position + 2])]
419 assert arg1 == 1
420 cintf.jit_rvmprof_code(arg1, arg2)
421 # no 'catch_exception' insn follows: just reraise
422 reraise(e)
423
424 def handle_rvmprof_enter(self):
425 code = self.jitcode.code
426 position = self.position
427 opcode = ord(code[position])
428 if opcode == self.builder.op_live:
429 position += SIZE_LIVE_OP
430 opcode = ord(code[position])
431 if opcode == self.op_rvmprof_code:
432 arg1 = self.registers_i[ord(code[position + 1])]
433 arg2 = self.registers_i[ord(code[position + 2])]
434 if arg1 == 1:
435 # we are resuming at a position that will do a
436 # jit_rvmprof_code(1), when really executed. That's a
437 # hint for the need for a jit_rvmprof_code(0).
438 from rpython.rlib.rvmprof import cintf
439 cintf.jit_rvmprof_code(0, arg2)
440
441 def copy_constants(self, registers, constants, targetindex):
442 """Copy jitcode.constants[0] to registers[255],
443 jitcode.constants[1] to registers[254],
444 jitcode.constants[2] to registers[253], etc."""
445 make_sure_not_resized(registers)
446 make_sure_not_resized(constants)
447 for i in range(len(constants)):
448 registers[targetindex] = constants[i]
449 targetindex += 1
450 copy_constants._annspecialcase_ = 'specialize:arglistitemtype(1)'
451
452 # ----------
453
454 @arguments("i", returns="i")
455 def bhimpl_int_same_as(a):
456 return a
457
458 @arguments("i", "i", returns="i")
459 def bhimpl_int_add(a, b):
460 return intmask(a + b)
461
462 @arguments("i", "i", returns="i")
463 def bhimpl_int_sub(a, b):
464 return intmask(a - b)
465
466 @arguments("i", "i", returns="i")
467 def bhimpl_int_mul(a, b):
468 return intmask(a * b)
469
470 @arguments("i", "i", returns="i")
471 def bhimpl_uint_mul_high(a, b):
472 from rpython.jit.metainterp.optimizeopt import intdiv
473 a = r_uint(a)
474 b = r_uint(b)
475 c = uint_mul_high(a, b)
476 return intmask(c)
477
478 @arguments("L", "i", "i", returns="iL")
479 def bhimpl_int_add_jump_if_ovf(label, a, b):
480 try:
481 return ovfcheck(a + b), -1
482 except OverflowError:
483 return 0, label
484
485 @arguments("L", "i", "i", returns="iL")
486 def bhimpl_int_sub_jump_if_ovf(label, a, b):
487 try:
488 return ovfcheck(a - b), -1
489 except OverflowError:
490 return 0, label
491
492 @arguments("L", "i", "i", returns="iL")
493 def bhimpl_int_mul_jump_if_ovf(label, a, b):
494 try:
495 return ovfcheck(a * b), -1
496 except OverflowError:
497 return 0, label
498
499 @arguments("i", "i", returns="i")
500 def bhimpl_int_and(a, b):
501 return a & b
502
503 @arguments("i", "i", returns="i")
504 def bhimpl_int_or(a, b):
505 return a | b
506
507 @arguments("i", "i", returns="i")
508 def bhimpl_int_xor(a, b):
509 return a ^ b
510
511 @arguments("i", "i", returns="i")
512 def bhimpl_int_rshift(a, b):
513 check_shift_count(b)
514 return a >> b
515
516 @arguments("i", "i", returns="i")
517 def bhimpl_int_lshift(a, b):
518 check_shift_count(b)
519 return intmask(a << b)
520
521 @arguments("i", "i", returns="i")
522 def bhimpl_uint_rshift(a, b):
523 check_shift_count(b)
524 c = r_uint(a) >> r_uint(b)
525 return intmask(c)
526
527 @arguments("i", returns="i")
528 def bhimpl_int_neg(a):
529 return intmask(-a)
530
531 @arguments("i", returns="i")
532 def bhimpl_int_invert(a):
533 return intmask(~a)
534
535 @arguments("i", "i", returns="i")
536 def bhimpl_int_lt(a, b):
537 return a < b
538 @arguments("i", "i", returns="i")
539 def bhimpl_int_le(a, b):
540 return a <= b
541 @arguments("i", "i", returns="i")
542 def bhimpl_int_eq(a, b):
543 return a == b
544 @arguments("i", "i", returns="i")
545 def bhimpl_int_ne(a, b):
546 return a != b
547 @arguments("i", "i", returns="i")
548 def bhimpl_int_gt(a, b):
549 return a > b
550 @arguments("i", "i", returns="i")
551 def bhimpl_int_ge(a, b):
552 return a >= b
553 @arguments("i", returns="i")
554 def bhimpl_int_is_zero(a):
555 return not a
556 @arguments("i", returns="i")
557 def bhimpl_int_is_true(a):
558 return bool(a)
559 @arguments("i", "i", "i", returns="i")
560 def bhimpl_int_between(a, b, c):
561 return a <= b < c
562 @arguments("i", returns="i")
563 def bhimpl_int_force_ge_zero(i):
564 if i < 0:
565 return 0
566 return i
567 @arguments("i", "i", returns="i")
568 def bhimpl_int_signext(a, b):
569 return int_signext(a, b)
570
571 @arguments("i", "i", returns="i")
572 def bhimpl_uint_lt(a, b):
573 return r_uint(a) < r_uint(b)
574 @arguments("i", "i", returns="i")
575 def bhimpl_uint_le(a, b):
576 return r_uint(a) <= r_uint(b)
577 @arguments("i", "i", returns="i")
578 def bhimpl_uint_gt(a, b):
579 return r_uint(a) > r_uint(b)
580 @arguments("i", "i", returns="i")
581 def bhimpl_uint_ge(a, b):
582 return r_uint(a) >= r_uint(b)
583
584 @arguments("r", "r", returns="i")
585 def bhimpl_ptr_eq(a, b):
586 return a == b
587 @arguments("r", "r", returns="i")
588 def bhimpl_ptr_ne(a, b):
589 return a != b
590 @arguments("r", returns="i")
591 def bhimpl_ptr_iszero(a):
592 return not a
593 @arguments("r", returns="i")
594 def bhimpl_ptr_nonzero(a):
595 return bool(a)
596 @arguments("r", "r", returns="i")
597 def bhimpl_instance_ptr_eq(a, b):
598 return a == b
599 @arguments("r", "r", returns="i")
600 def bhimpl_instance_ptr_ne(a, b):
601 return a != b
602 @arguments("r", returns="i")
603 def bhimpl_cast_ptr_to_int(a):
604 i = lltype.cast_ptr_to_int(a)
605 ll_assert((i & 1) == 1, "bhimpl_cast_ptr_to_int: not an odd int")
606 return i
607 @arguments("i", returns="r")
608 def bhimpl_cast_int_to_ptr(i):
609 ll_assert((i & 1) == 1, "bhimpl_cast_int_to_ptr: not an odd int")
610 return lltype.cast_int_to_ptr(llmemory.GCREF, i)
611
612 @arguments("r")
613 def bhimpl_assert_not_none(a):
614 assert a
615
616 @arguments("r", "i")
617 def bhimpl_record_exact_class(a, b):
618 pass
619
620 @arguments("cpu", "i", "i", "I", "R", "d")
621 def bhimpl_record_known_result_i_ir_v(cpu, res, func, args_i, args_r,
622 calldescr):
623 pass
624
625 @arguments("cpu", "r", "i", "I", "R", "d")
626 def bhimpl_record_known_result_r_ir_v(cpu, res, func, args_i, args_r,
627 calldescr):
628 pass
629
630 @arguments("r", "r")
631 def bhimpl_record_exact_value_r(a, b):
632 pass
633
634 @arguments("i", "i")
635 def bhimpl_record_exact_value_i(a, b):
636 pass
637
638 @arguments("i", returns="i")
639 def bhimpl_int_copy(a):
640 return a
641 @arguments("r", returns="r")
642 def bhimpl_ref_copy(a):
643 return a
644 @arguments("f", returns="f")
645 def bhimpl_float_copy(a):
646 return a
647
648 @arguments("i")
649 def bhimpl_int_guard_value(a):
650 pass
651 @arguments("r")
652 def bhimpl_ref_guard_value(a):
653 pass
654 @arguments("f")
655 def bhimpl_float_guard_value(a):
656 pass
657 @arguments("r", "i", "d", returns="r")
658 def bhimpl_str_guard_value(a, i, d):
659 return a
660
661 @arguments("self", "i")
662 def bhimpl_int_push(self, a):
663 self.tmpreg_i = a
664 @arguments("self", "r")
665 def bhimpl_ref_push(self, a):
666 self.tmpreg_r = a
667 @arguments("self", "f")
668 def bhimpl_float_push(self, a):
669 self.tmpreg_f = a
670
671 @arguments("self", returns="i")
672 def bhimpl_int_pop(self):
673 return self.get_tmpreg_i()
674 @arguments("self", returns="r")
675 def bhimpl_ref_pop(self):
676 return self.get_tmpreg_r()
677 @arguments("self", returns="f")
678 def bhimpl_float_pop(self):
679 return self.get_tmpreg_f()
680
681 # ----------
682 # float operations
683
684 @arguments("f", returns="f")
685 def bhimpl_float_neg(a):
686 a = longlong.getrealfloat(a)
687 x = -a
688 return longlong.getfloatstorage(x)
689 @arguments("f", returns="f")
690 def bhimpl_float_abs(a):
691 a = longlong.getrealfloat(a)
692 x = abs(a)
693 return longlong.getfloatstorage(x)
694
695 @arguments("f", "f", returns="f")
696 def bhimpl_float_add(a, b):
697 a = longlong.getrealfloat(a)
698 b = longlong.getrealfloat(b)
699 x = a + b
700 return longlong.getfloatstorage(x)
701 @arguments("f", "f", returns="f")
702 def bhimpl_float_sub(a, b):
703 a = longlong.getrealfloat(a)
704 b = longlong.getrealfloat(b)
705 x = a - b
706 return longlong.getfloatstorage(x)
707 @arguments("f", "f", returns="f")
708 def bhimpl_float_mul(a, b):
709 a = longlong.getrealfloat(a)
710 b = longlong.getrealfloat(b)
711 x = a * b
712 return longlong.getfloatstorage(x)
713 @arguments("f", "f", returns="f")
714 def bhimpl_float_truediv(a, b):
715 a = longlong.getrealfloat(a)
716 b = longlong.getrealfloat(b)
717 x = a / b
718 return longlong.getfloatstorage(x)
719
720 @arguments("f", "f", returns="i")
721 def bhimpl_float_lt(a, b):
722 a = longlong.getrealfloat(a)
723 b = longlong.getrealfloat(b)
724 return a < b
725 @arguments("f", "f", returns="i")
726 def bhimpl_float_le(a, b):
727 a = longlong.getrealfloat(a)
728 b = longlong.getrealfloat(b)
729 return a <= b
730 @arguments("f", "f", returns="i")
731 def bhimpl_float_eq(a, b):
732 a = longlong.getrealfloat(a)
733 b = longlong.getrealfloat(b)
734 return a == b
735 @arguments("f", "f", returns="i")
736 def bhimpl_float_ne(a, b):
737 a = longlong.getrealfloat(a)
738 b = longlong.getrealfloat(b)
739 return a != b
740 @arguments("f", "f", returns="i")
741 def bhimpl_float_gt(a, b):
742 a = longlong.getrealfloat(a)
743 b = longlong.getrealfloat(b)
744 return a > b
745 @arguments("f", "f", returns="i")
746 def bhimpl_float_ge(a, b):
747 a = longlong.getrealfloat(a)
748 b = longlong.getrealfloat(b)
749 return a >= b
750
751 @arguments("f", "f", "L", "pc", returns="L")
752 def bhimpl_goto_if_not_float_lt(a, b, target, pc):
753 a = longlong.getrealfloat(a)
754 b = longlong.getrealfloat(b)
755 if a < b:
756 return pc
757 else:
758 return target
759 @arguments("f", "f", "L", "pc", returns="L")
760 def bhimpl_goto_if_not_float_le(a, b, target, pc):
761 a = longlong.getrealfloat(a)
762 b = longlong.getrealfloat(b)
763 if a <= b:
764 return pc
765 else:
766 return target
767 @arguments("f", "f", "L", "pc", returns="L")
768 def bhimpl_goto_if_not_float_eq(a, b, target, pc):
769 a = longlong.getrealfloat(a)
770 b = longlong.getrealfloat(b)
771 if a == b:
772 return pc
773 else:
774 return target
775 @arguments("f", "f", "L", "pc", returns="L")
776 def bhimpl_goto_if_not_float_ne(a, b, target, pc):
777 a = longlong.getrealfloat(a)
778 b = longlong.getrealfloat(b)
779 if a != b:
780 return pc
781 else:
782 return target
783 @arguments("f", "f", "L", "pc", returns="L")
784 def bhimpl_goto_if_not_float_gt(a, b, target, pc):
785 a = longlong.getrealfloat(a)
786 b = longlong.getrealfloat(b)
787 if a > b:
788 return pc
789 else:
790 return target
791 @arguments("f", "f", "L", "pc", returns="L")
792 def bhimpl_goto_if_not_float_ge(a, b, target, pc):
793 a = longlong.getrealfloat(a)
794 b = longlong.getrealfloat(b)
795 if a >= b:
796 return pc
797 else:
798 return target
799
800 @arguments("f", returns="i")
801 def bhimpl_cast_float_to_int(a):
802 a = longlong.getrealfloat(a)
803 # note: we need to call int() twice to care for the fact that
804 # int(-2147483648.0) returns a long :-(
805 # we could also call intmask() instead of the outermost int(), but
806 # it's probably better to explicitly crash (by getting a long) if a
807 # non-translated version tries to cast a too large float to an int.
808 return int(int(a))
809
810 @arguments("i", returns="f")
811 def bhimpl_cast_int_to_float(a):
812 x = float(a)
813 return longlong.getfloatstorage(x)
814
815 @arguments("f", returns="i")
816 def bhimpl_cast_float_to_singlefloat(a):
817 from rpython.rlib.rarithmetic import r_singlefloat
818 a = longlong.getrealfloat(a)
819 a = r_singlefloat(a)
820 return longlong.singlefloat2int(a)
821
822 @arguments("i", returns="f")
823 def bhimpl_cast_singlefloat_to_float(a):
824 a = longlong.int2singlefloat(a)
825 a = float(a)
826 return longlong.getfloatstorage(a)
827
828 @arguments("f", returns=LONGLONG_TYPECODE)
829 def bhimpl_convert_float_bytes_to_longlong(a):
830 a = longlong.getrealfloat(a)
831 return longlong2float.float2longlong(a)
832
833 @arguments(LONGLONG_TYPECODE, returns="f")
834 def bhimpl_convert_longlong_bytes_to_float(a):
835 a = longlong2float.longlong2float(a)
836 return longlong.getfloatstorage(a)
837
838 # ----------
839 # control flow operations
840
841 @arguments("self", "i")
842 def bhimpl_int_return(self, a):
843 self.tmpreg_i = a
844 self._return_type = 'i'
845 raise LeaveFrame
846
847 @arguments("self", "r")
848 def bhimpl_ref_return(self, a):
849 self.tmpreg_r = a
850 self._return_type = 'r'
851 raise LeaveFrame
852
853 @arguments("self", "f")
854 def bhimpl_float_return(self, a):
855 self.tmpreg_f = a
856 self._return_type = 'f'
857 raise LeaveFrame
858
859 @arguments("self")
860 def bhimpl_void_return(self):
861 self._return_type = 'v'
862 raise LeaveFrame
863
864 @arguments("i", "L", "pc", returns="L")
865 def bhimpl_goto_if_not(a, target, pc):
866 if a:
867 return pc
868 else:
869 return target
870
871 @arguments("i", "i", "L", "pc", returns="L")
872 def bhimpl_goto_if_not_int_lt(a, b, target, pc):
873 if a < b:
874 return pc
875 else:
876 return target
877
878 @arguments("i", "i", "L", "pc", returns="L")
879 def bhimpl_goto_if_not_int_le(a, b, target, pc):
880 if a <= b:
881 return pc
882 else:
883 return target
884
885 @arguments("i", "i", "L", "pc", returns="L")
886 def bhimpl_goto_if_not_int_eq(a, b, target, pc):
887 if a == b:
888 return pc
889 else:
890 return target
891
892 @arguments("i", "i", "L", "pc", returns="L")
893 def bhimpl_goto_if_not_int_ne(a, b, target, pc):
894 if a != b:
895 return pc
896 else:
897 return target
898
899 @arguments("i", "i", "L", "pc", returns="L")
900 def bhimpl_goto_if_not_int_gt(a, b, target, pc):
901 if a > b:
902 return pc
903 else:
904 return target
905
906 @arguments("i", "i", "L", "pc", returns="L")
907 def bhimpl_goto_if_not_int_ge(a, b, target, pc):
908 if a >= b:
909 return pc
910 else:
911 return target
912
913 bhimpl_goto_if_not_int_is_true = bhimpl_goto_if_not
914
915 @arguments("i", "L", "pc", returns="L")
916 def bhimpl_goto_if_not_int_is_zero(a, target, pc):
917 if not a:
918 return pc
919 else:
920 return target
921
922 @arguments("r", "r", "L", "pc", returns="L")
923 def bhimpl_goto_if_not_ptr_eq(a, b, target, pc):
924 if a == b:
925 return pc
926 else:
927 return target
928
929 @arguments("r", "r", "L", "pc", returns="L")
930 def bhimpl_goto_if_not_ptr_ne(a, b, target, pc):
931 if a != b:
932 return pc
933 else:
934 return target
935
936 @arguments("r", "L", "pc", returns="L")
937 def bhimpl_goto_if_not_ptr_iszero(a, target, pc):
938 if not a:
939 return pc
940 else:
941 return target
942
943 @arguments("r", "L", "pc", returns="L")
944 def bhimpl_goto_if_not_ptr_nonzero(a, target, pc):
945 if a:
946 return pc
947 else:
948 return target
949
950 @arguments("L", returns="L")
951 def bhimpl_goto(target):
952 return target
953
954 @arguments("i", "d", "pc", returns="L")
955 def bhimpl_switch(switchvalue, switchdict, pc):
956 assert isinstance(switchdict, SwitchDictDescr)
957 try:
958 return switchdict.dict[switchvalue]
959 except KeyError:
960 return pc
961
962 @arguments()
963 def bhimpl_unreachable():
964 raise AssertionError("unreachable")
965
966 # ----------
967 # exception handling operations
968
969 @arguments("L")
970 def bhimpl_catch_exception(target):
971 """This is a no-op when run normally. When an exception occurs
972 and the instruction that raised is immediately followed by a
973 catch_exception, then the code in handle_exception_in_frame()
974 will capture the exception and jump to 'target'."""
975
976 @arguments("self", "i", "L", "pc", returns="L")
977 def bhimpl_goto_if_exception_mismatch(self, vtable, target, pc):
978 adr = int2adr(vtable)
979 bounding_class = llmemory.cast_adr_to_ptr(adr, rclass.CLASSTYPE)
980 real_instance = self.exception_last_value
981 assert real_instance
982 if rclass.ll_issubclass(real_instance.typeptr, bounding_class):
983 return pc
984 else:
985 return target
986
987 @arguments("self", returns="i")
988 def bhimpl_last_exception(self):
989 real_instance = self.exception_last_value
990 assert real_instance
991 return ptr2int(real_instance.typeptr)
992
993 @arguments("self", returns="r")
994 def bhimpl_last_exc_value(self):
995 real_instance = self.exception_last_value
996 assert real_instance
997 return lltype.cast_opaque_ptr(llmemory.GCREF, real_instance)
998
999 @arguments("self", "r")
1000 def bhimpl_raise(self, excvalue):
1001 e = lltype.cast_opaque_ptr(rclass.OBJECTPTR, excvalue)
1002 assert e
1003 reraise(e)
1004
1005 @arguments("self")
1006 def bhimpl_reraise(self):
1007 e = self.exception_last_value
1008 assert e
1009 reraise(e)
1010
1011 @arguments("r")
1012 def bhimpl_debug_fatalerror(msg):
1013 from rpython.rtyper.lltypesystem import rstr
1014 msg = lltype.cast_opaque_ptr(lltype.Ptr(rstr.STR), msg)
1015 llop.debug_fatalerror(lltype.Void, msg)
1016
1017 @arguments("r", "i", "i", "i", "i")
1018 def bhimpl_jit_debug(string, arg1=0, arg2=0, arg3=0, arg4=0):
1019 pass
1020
1021 @arguments("i")
1022 def bhimpl_jit_enter_portal_frame(x):
1023 pass
1024
1025 @arguments()
1026 def bhimpl_jit_leave_portal_frame():
1027 pass
1028
1029 @arguments("i")
1030 def bhimpl_int_assert_green(x):
1031 pass
1032 @arguments("r")
1033 def bhimpl_ref_assert_green(x):
1034 pass
1035 @arguments("f")
1036 def bhimpl_float_assert_green(x):
1037 pass
1038
1039 @arguments(returns="i")
1040 def bhimpl_current_trace_length():
1041 return -1
1042
1043 @arguments("i", returns="i")
1044 def bhimpl_int_isconstant(x):
1045 return False
1046
1047 @arguments("f", returns="i")
1048 def bhimpl_float_isconstant(x):
1049 return False
1050
1051 @arguments("r", returns="i")
1052 def bhimpl_ref_isconstant(x):
1053 return False
1054
1055 @arguments("r", returns="i")
1056 def bhimpl_ref_isvirtual(x):
1057 return False
1058
1059 # ----------
1060 # the main hints and recursive calls
1061
1062 @arguments("i")
1063 def bhimpl_loop_header(jdindex):
1064 pass
1065
1066 @arguments("self", "i", "I", "R", "F", "I", "R", "F")
1067 def bhimpl_jit_merge_point(self, jdindex, *args):
1068 if self.nextblackholeinterp is None: # we are the last level
1069 raise jitexc.ContinueRunningNormally(*args)
1070 # Note that the case above is an optimization: the case
1071 # below would work too. But it keeps unnecessary stuff on
1072 # the stack; the solution above first gets rid of the blackhole
1073 # interpreter completely.
1074 else:
1075 # This occurs when we reach 'jit_merge_point' in the portal
1076 # function called by recursion. In this case, we can directly
1077 # call the interpreter main loop from here, and just return its
1078 # result.
1079 sd = self.builder.metainterp_sd
1080 result_type = sd.jitdrivers_sd[jdindex].result_type
1081 if result_type == 'v':
1082 self.bhimpl_recursive_call_v(jdindex, *args)
1083 self.bhimpl_void_return()
1084 elif result_type == 'i':
1085 x = self.bhimpl_recursive_call_i(jdindex, *args)
1086 self.bhimpl_int_return(x)
1087 elif result_type == 'r':
1088 x = self.bhimpl_recursive_call_r(jdindex, *args)
1089 self.bhimpl_ref_return(x)
1090 elif result_type == 'f':
1091 x = self.bhimpl_recursive_call_f(jdindex, *args)
1092 self.bhimpl_float_return(x)
1093 assert False
1094
1095 def get_portal_runner(self, jdindex):
1096 jitdriver_sd = self.builder.metainterp_sd.jitdrivers_sd[jdindex]
1097 fnptr = adr2int(jitdriver_sd.portal_runner_adr)
1098 calldescr = jitdriver_sd.mainjitcode.calldescr
1099 return fnptr, calldescr
1100
1101 @arguments("self", "i", "I", "R", "F", "I", "R", "F", returns="i")
1102 def bhimpl_recursive_call_i(self, jdindex, greens_i, greens_r, greens_f,
1103 reds_i, reds_r, reds_f):
1104 fnptr, calldescr = self.get_portal_runner(jdindex)
1105 return self.cpu.bh_call_i(fnptr,
1106 greens_i + reds_i,
1107 greens_r + reds_r,
1108 greens_f + reds_f, calldescr)
1109 @arguments("self", "i", "I", "R", "F", "I", "R", "F", returns="r")
1110 def bhimpl_recursive_call_r(self, jdindex, greens_i, greens_r, greens_f,
1111 reds_i, reds_r, reds_f):
1112 fnptr, calldescr = self.get_portal_runner(jdindex)
1113 return self.cpu.bh_call_r(fnptr,
1114 greens_i + reds_i,
1115 greens_r + reds_r,
1116 greens_f + reds_f, calldescr)
1117 @arguments("self", "i", "I", "R", "F", "I", "R", "F", returns="f")
1118 def bhimpl_recursive_call_f(self, jdindex, greens_i, greens_r, greens_f,
1119 reds_i, reds_r, reds_f):
1120 fnptr, calldescr = self.get_portal_runner(jdindex)
1121 return self.cpu.bh_call_f(fnptr,
1122 greens_i + reds_i,
1123 greens_r + reds_r,
1124 greens_f + reds_f, calldescr)
1125 @arguments("self", "i", "I", "R", "F", "I", "R", "F")
1126 def bhimpl_recursive_call_v(self, jdindex, greens_i, greens_r, greens_f,
1127 reds_i, reds_r, reds_f):
1128 fnptr, calldescr = self.get_portal_runner(jdindex)
1129 return self.cpu.bh_call_v(fnptr,
1130 greens_i + reds_i,
1131 greens_r + reds_r,
1132 greens_f + reds_f, calldescr)
1133
1134 # ----------
1135 # virtual refs
1136
1137 @arguments("r", returns="r")
1138 def bhimpl_virtual_ref(a):
1139 return a
1140
1141 @arguments("r")
1142 def bhimpl_virtual_ref_finish(a):
1143 pass
1144
1145 # ----------
1146 # list operations
1147
1148 @arguments("cpu", "r", "i", "d", returns="i")
1149 def bhimpl_check_neg_index(cpu, array, index, arraydescr):
1150 if index < 0:
1151 index += cpu.bh_arraylen_gc(array, arraydescr)
1152 return index
1153
1154 @arguments("cpu", "r", "i", "d", returns="i")
1155 def bhimpl_check_resizable_neg_index(cpu, lst, index, lengthdescr):
1156 if index < 0:
1157 index += cpu.bh_getfield_gc_i(lst, lengthdescr)
1158 return index
1159
1160 @arguments("cpu", "i", "d", "d", "d", "d", returns="r")
1161 def bhimpl_newlist(cpu, length, structdescr, lengthdescr,
1162 itemsdescr, arraydescr):
1163 result = cpu.bh_new(structdescr)
1164 cpu.bh_setfield_gc_i(result, length, lengthdescr)
1165 if (arraydescr.is_array_of_structs() or
1166 arraydescr.is_array_of_pointers()):
1167 items = cpu.bh_new_array_clear(length, arraydescr)
1168 else:
1169 items = cpu.bh_new_array(length, arraydescr)
1170 cpu.bh_setfield_gc_r(result, items, itemsdescr)
1171 return result
1172
1173 @arguments("cpu", "i", "d", "d", "d", "d", returns="r")
1174 def bhimpl_newlist_clear(cpu, length, structdescr, lengthdescr,
1175 itemsdescr, arraydescr):
1176 result = cpu.bh_new(structdescr)
1177 cpu.bh_setfield_gc_i(result, length, lengthdescr)
1178 items = cpu.bh_new_array_clear(length, arraydescr)
1179 cpu.bh_setfield_gc_r(result, items, itemsdescr)
1180 return result
1181
1182 @arguments("cpu", "i", "d", "d", "d", "d", returns="r")
1183 def bhimpl_newlist_hint(cpu, lengthhint, structdescr, lengthdescr,
1184 itemsdescr, arraydescr):
1185 result = cpu.bh_new(structdescr)
1186 cpu.bh_setfield_gc_i(result, 0, lengthdescr)
1187 if (arraydescr.is_array_of_structs() or
1188 arraydescr.is_array_of_pointers()):
1189 items = cpu.bh_new_array_clear(lengthhint, arraydescr)
1190 else:
1191 items = cpu.bh_new_array(lengthhint, arraydescr)
1192 cpu.bh_setfield_gc_r(result, items, itemsdescr)
1193 return result
1194
1195 @arguments("cpu", "r", "i", "d", "d", returns="i")
1196 def bhimpl_getlistitem_gc_i(cpu, lst, index, itemsdescr, arraydescr):
1197 items = cpu.bh_getfield_gc_r(lst, itemsdescr)
1198 return cpu.bh_getarrayitem_gc_i(items, index, arraydescr)
1199 @arguments("cpu", "r", "i", "d", "d", returns="r")
1200 def bhimpl_getlistitem_gc_r(cpu, lst, index, itemsdescr, arraydescr):
1201 items = cpu.bh_getfield_gc_r(lst, itemsdescr)
1202 return cpu.bh_getarrayitem_gc_r(items, index, arraydescr)
1203 @arguments("cpu", "r", "i", "d", "d", returns="f")
1204 def bhimpl_getlistitem_gc_f(cpu, lst, index, itemsdescr, arraydescr):
1205 items = cpu.bh_getfield_gc_r(lst, itemsdescr)
1206 return cpu.bh_getarrayitem_gc_f(items, index, arraydescr)
1207
1208 @arguments("cpu", "r", "i", "i", "d", "d")
1209 def bhimpl_setlistitem_gc_i(cpu, lst, index, nval, itemsdescr, arraydescr):
1210 items = cpu.bh_getfield_gc_r(lst, itemsdescr)
1211 cpu.bh_setarrayitem_gc_i(items, index, nval, arraydescr)
1212 @arguments("cpu", "r", "i", "r", "d", "d")
1213 def bhimpl_setlistitem_gc_r(cpu, lst, index, nval, itemsdescr, arraydescr):
1214 items = cpu.bh_getfield_gc_r(lst, itemsdescr)
1215 cpu.bh_setarrayitem_gc_r(items, index, nval, arraydescr)
1216 @arguments("cpu", "r", "i", "f", "d", "d")
1217 def bhimpl_setlistitem_gc_f(cpu, lst, index, nval, itemsdescr, arraydescr):
1218 items = cpu.bh_getfield_gc_r(lst, itemsdescr)
1219 cpu.bh_setarrayitem_gc_f(items, index, nval, arraydescr)
1220
1221 # ----------
1222 # the following operations are directly implemented by the backend
1223
1224 @arguments("cpu", "i", "R", "d", returns="i")
1225 def bhimpl_residual_call_r_i(cpu, func, args_r, calldescr):
1226 return cpu.bh_call_i(func, None, args_r, None, calldescr)
1227 @arguments("cpu", "i", "R", "d", returns="r")
1228 def bhimpl_residual_call_r_r(cpu, func, args_r, calldescr):
1229 return cpu.bh_call_r(func, None, args_r, None, calldescr)
1230 @arguments("cpu", "i", "R", "d")
1231 def bhimpl_residual_call_r_v(cpu, func, args_r, calldescr):
1232 return cpu.bh_call_v(func, None, args_r, None, calldescr)
1233
1234 @arguments("cpu", "i", "I", "R", "d", returns="i")
1235 def bhimpl_residual_call_ir_i(cpu, func, args_i, args_r, calldescr):
1236 return cpu.bh_call_i(func, args_i, args_r, None, calldescr)
1237 @arguments("cpu", "i", "I", "R", "d", returns="r")
1238 def bhimpl_residual_call_ir_r(cpu, func, args_i, args_r, calldescr):
1239 return cpu.bh_call_r(func, args_i, args_r, None, calldescr)
1240 @arguments("cpu", "i", "I", "R", "d")
1241 def bhimpl_residual_call_ir_v(cpu, func, args_i, args_r, calldescr):
1242 return cpu.bh_call_v(func, args_i, args_r, None, calldescr)
1243
1244 @arguments("cpu", "i", "I", "R", "F", "d", returns="i")
1245 def bhimpl_residual_call_irf_i(cpu, func, args_i,args_r,args_f,calldescr):
1246 return cpu.bh_call_i(func, args_i, args_r, args_f, calldescr)
1247 @arguments("cpu", "i", "I", "R", "F", "d", returns="r")
1248 def bhimpl_residual_call_irf_r(cpu, func, args_i,args_r,args_f,calldescr):
1249 return cpu.bh_call_r(func, args_i, args_r, args_f, calldescr)
1250 @arguments("cpu", "i", "I", "R", "F", "d", returns="f")
1251 def bhimpl_residual_call_irf_f(cpu, func, args_i,args_r,args_f,calldescr):
1252 return cpu.bh_call_f(func, args_i, args_r, args_f, calldescr)
1253 @arguments("cpu", "i", "I", "R", "F", "d")
1254 def bhimpl_residual_call_irf_v(cpu, func, args_i,args_r,args_f,calldescr):
1255 return cpu.bh_call_v(func, args_i, args_r, args_f, calldescr)
1256
1257 @arguments("cpu", "i", "i", "I", "R", "d")
1258 def bhimpl_conditional_call_ir_v(cpu, condition, func, args_i, args_r,
1259 calldescr):
1260 # conditional calls - condition is a flag, and they cannot return stuff
1261 if condition:
1262 cpu.bh_call_v(func, args_i, args_r, None, calldescr)
1263
1264 @arguments("cpu", "i", "i", "I", "R", "d", returns="i")
1265 def bhimpl_conditional_call_value_ir_i(cpu, value, func, args_i, args_r,
1266 calldescr):
1267 if value == 0:
1268 value = cpu.bh_call_i(func, args_i, args_r, None, calldescr)
1269 return value
1270
1271 @arguments("cpu", "r", "i", "I", "R", "d", returns="r")
1272 def bhimpl_conditional_call_value_ir_r(cpu, value, func, args_i, args_r,
1273 calldescr):
1274 if not value:
1275 value = cpu.bh_call_r(func, args_i, args_r, None, calldescr)
1276 return value
1277
1278 @arguments("cpu", "j", "R", returns="i")
1279 def bhimpl_inline_call_r_i(cpu, jitcode, args_r):
1280 return cpu.bh_call_i(adr2int(jitcode.fnaddr),
1281 None, args_r, None, jitcode.calldescr)
1282 @arguments("cpu", "j", "R", returns="r")
1283 def bhimpl_inline_call_r_r(cpu, jitcode, args_r):
1284 return cpu.bh_call_r(adr2int(jitcode.fnaddr),
1285 None, args_r, None, jitcode.calldescr)
1286 @arguments("cpu", "j", "R")
1287 def bhimpl_inline_call_r_v(cpu, jitcode, args_r):
1288 return cpu.bh_call_v(adr2int(jitcode.fnaddr),
1289 None, args_r, None, jitcode.calldescr)
1290
1291 @arguments("cpu", "j", "I", "R", returns="i")
1292 def bhimpl_inline_call_ir_i(cpu, jitcode, args_i, args_r):
1293 return cpu.bh_call_i(adr2int(jitcode.fnaddr),
1294 args_i, args_r, None, jitcode.calldescr)
1295 @arguments("cpu", "j", "I", "R", returns="r")
1296 def bhimpl_inline_call_ir_r(cpu, jitcode, args_i, args_r):
1297 return cpu.bh_call_r(adr2int(jitcode.fnaddr),
1298 args_i, args_r, None, jitcode.calldescr)
1299 @arguments("cpu", "j", "I", "R")
1300 def bhimpl_inline_call_ir_v(cpu, jitcode, args_i, args_r):
1301 return cpu.bh_call_v(adr2int(jitcode.fnaddr),
1302 args_i, args_r, None, jitcode.calldescr)
1303
1304 @arguments("cpu", "j", "I", "R", "F", returns="i")
1305 def bhimpl_inline_call_irf_i(cpu, jitcode, args_i, args_r, args_f):
1306 return cpu.bh_call_i(adr2int(jitcode.fnaddr),
1307 args_i, args_r, args_f, jitcode.calldescr)
1308 @arguments("cpu", "j", "I", "R", "F", returns="r")
1309 def bhimpl_inline_call_irf_r(cpu, jitcode, args_i, args_r, args_f):
1310 return cpu.bh_call_r(adr2int(jitcode.fnaddr),
1311 args_i, args_r, args_f, jitcode.calldescr)
1312 @arguments("cpu", "j", "I", "R", "F", returns="f")
1313 def bhimpl_inline_call_irf_f(cpu, jitcode, args_i, args_r, args_f):
1314 return cpu.bh_call_f(adr2int(jitcode.fnaddr),
1315 args_i, args_r, args_f, jitcode.calldescr)
1316 @arguments("cpu", "j", "I", "R", "F")
1317 def bhimpl_inline_call_irf_v(cpu, jitcode, args_i, args_r, args_f):
1318 return cpu.bh_call_v(adr2int(jitcode.fnaddr),
1319 args_i, args_r, args_f, jitcode.calldescr)
1320
1321 @arguments("cpu", "i", "d", returns="r")
1322 def bhimpl_new_array(cpu, length, arraydescr):
1323 return cpu.bh_new_array(length, arraydescr)
1324
1325 @arguments("cpu", "i", "d", returns="r")
1326 def bhimpl_new_array_clear(cpu, length, arraydescr):
1327 return cpu.bh_new_array_clear(length, arraydescr)
1328
1329 @arguments("cpu", "r", "i", "d", returns="i")
1330 def bhimpl_getarrayitem_gc_i(cpu, array, index, arraydescr):
1331 return cpu.bh_getarrayitem_gc_i(array, index, arraydescr)
1332 @arguments("cpu", "r", "i", "d", returns="r")
1333 def bhimpl_getarrayitem_gc_r(cpu, array, index, arraydescr):
1334 return cpu.bh_getarrayitem_gc_r(array, index, arraydescr)
1335 @arguments("cpu", "r", "i", "d", returns="f")
1336 def bhimpl_getarrayitem_gc_f(cpu, array, index, arraydescr):
1337 return cpu.bh_getarrayitem_gc_f(array, index, arraydescr)
1338
1339 bhimpl_getarrayitem_gc_i_pure = bhimpl_getarrayitem_gc_i
1340 bhimpl_getarrayitem_gc_r_pure = bhimpl_getarrayitem_gc_r
1341 bhimpl_getarrayitem_gc_f_pure = bhimpl_getarrayitem_gc_f
1342
1343 @arguments("cpu", "i", "i", "d", returns="i")
1344 def bhimpl_getarrayitem_raw_i(cpu, array, index, arraydescr):
1345 return cpu.bh_getarrayitem_raw_i(array, index, arraydescr)
1346 @arguments("cpu", "i", "i", "d", returns="f")
1347 def bhimpl_getarrayitem_raw_f(cpu, array, index, arraydescr):
1348 return cpu.bh_getarrayitem_raw_f(array, index, arraydescr)
1349
1350 @arguments("cpu", "r", "i", "i", "d")
1351 def bhimpl_setarrayitem_gc_i(cpu, array, index, newvalue, arraydescr):
1352 cpu.bh_setarrayitem_gc_i(array, index, newvalue, arraydescr)
1353 @arguments("cpu", "r", "i", "r", "d")
1354 def bhimpl_setarrayitem_gc_r(cpu, array, index, newvalue, arraydescr):
1355 cpu.bh_setarrayitem_gc_r(array, index, newvalue, arraydescr)
1356 @arguments("cpu", "r", "i", "f", "d")
1357 def bhimpl_setarrayitem_gc_f(cpu, array, index, newvalue, arraydescr):
1358 cpu.bh_setarrayitem_gc_f(array, index, newvalue, arraydescr)
1359
1360 @arguments("cpu", "i", "i", "i", "d")
1361 def bhimpl_setarrayitem_raw_i(cpu, array, index, newvalue, arraydescr):
1362 cpu.bh_setarrayitem_raw_i(array, index, newvalue, arraydescr)
1363 @arguments("cpu", "i", "i", "f", "d")
1364 def bhimpl_setarrayitem_raw_f(cpu, array, index, newvalue, arraydescr):
1365 cpu.bh_setarrayitem_raw_f(array, index, newvalue, arraydescr)
1366
1367 # note, there is no 'r' here, since it can't happen
1368
1369 @arguments("cpu", "r", "d", returns="i")
1370 def bhimpl_arraylen_gc(cpu, array, arraydescr):
1371 return cpu.bh_arraylen_gc(array, arraydescr)
1372
1373 @arguments("cpu", "r", "i", "d", "d", returns="i")
1374 def bhimpl_getarrayitem_vable_i(cpu, vable, index, fielddescr, arraydescr):
1375 fielddescr.get_vinfo().clear_vable_token(vable)
1376 array = cpu.bh_getfield_gc_r(vable, fielddescr)
1377 return cpu.bh_getarrayitem_gc_i(array, index, arraydescr)
1378 @arguments("cpu", "r", "i", "d", "d", returns="r")
1379 def bhimpl_getarrayitem_vable_r(cpu, vable, index, fielddescr, arraydescr):
1380 fielddescr.get_vinfo().clear_vable_token(vable)
1381 array = cpu.bh_getfield_gc_r(vable, fielddescr)
1382 return cpu.bh_getarrayitem_gc_r(array, index, arraydescr)
1383 @arguments("cpu", "r", "i", "d", "d", returns="f")
1384 def bhimpl_getarrayitem_vable_f(cpu, vable, index, fielddescr, arraydescr):
1385 fielddescr.get_vinfo().clear_vable_token(vable)
1386 array = cpu.bh_getfield_gc_r(vable, fielddescr)
1387 return cpu.bh_getarrayitem_gc_f(array, index, arraydescr)
1388
1389 @arguments("cpu", "r", "i", "i", "d", "d")
1390 def bhimpl_setarrayitem_vable_i(cpu, vable, index, newval, fdescr, adescr):
1391 fdescr.get_vinfo().clear_vable_token(vable)
1392 array = cpu.bh_getfield_gc_r(vable, fdescr)
1393 cpu.bh_setarrayitem_gc_i(array, index, newval, adescr)
1394 @arguments("cpu", "r", "i", "r", "d", "d")
1395 def bhimpl_setarrayitem_vable_r(cpu, vable, index, newval, fdescr, adescr):
1396 fdescr.get_vinfo().clear_vable_token(vable)
1397 array = cpu.bh_getfield_gc_r(vable, fdescr)
1398 cpu.bh_setarrayitem_gc_r(array, index, newval, adescr)
1399 @arguments("cpu", "r", "i", "f", "d", "d")
1400 def bhimpl_setarrayitem_vable_f(cpu, vable, index, newval, fdescr, adescr):
1401 fdescr.get_vinfo().clear_vable_token(vable)
1402 array = cpu.bh_getfield_gc_r(vable, fdescr)
1403 cpu.bh_setarrayitem_gc_f(array, index, newval, adescr)
1404
1405 @arguments("cpu", "r", "d", "d", returns="i")
1406 def bhimpl_arraylen_vable(cpu, vable, fdescr, adescr):
1407 fdescr.get_vinfo().clear_vable_token(vable)
1408 array = cpu.bh_getfield_gc_r(vable, fdescr)
1409 return cpu.bh_arraylen_gc(array, adescr)
1410
1411 @arguments("cpu", "r", "i", "d", returns="i")
1412 def bhimpl_getinteriorfield_gc_i(cpu, array, index, descr):
1413 return cpu.bh_getinteriorfield_gc_i(array, index, descr)
1414 @arguments("cpu", "r", "i", "d", returns="r")
1415 def bhimpl_getinteriorfield_gc_r(cpu, array, index, descr):
1416 return cpu.bh_getinteriorfield_gc_r(array, index, descr)
1417 @arguments("cpu", "r", "i", "d", returns="f")
1418 def bhimpl_getinteriorfield_gc_f(cpu, array, index, descr):
1419 return cpu.bh_getinteriorfield_gc_f(array, index, descr)
1420
1421 @arguments("cpu", "r", "i", "i", "d")
1422 def bhimpl_setinteriorfield_gc_i(cpu, array, index, value, descr):
1423 cpu.bh_setinteriorfield_gc_i(array, index, value, descr)
1424 @arguments("cpu", "r", "i", "r", "d")
1425 def bhimpl_setinteriorfield_gc_r(cpu, array, index, value, descr):
1426 cpu.bh_setinteriorfield_gc_r(array, index, value, descr)
1427 @arguments("cpu", "r", "i", "f", "d")
1428 def bhimpl_setinteriorfield_gc_f(cpu, array, index, value, descr):
1429 cpu.bh_setinteriorfield_gc_f(array, index, value, descr)
1430
1431 @arguments("cpu", "r", "d", returns="i")
1432 def bhimpl_getfield_gc_i(cpu, struct, fielddescr):
1433 return cpu.bh_getfield_gc_i(struct, fielddescr)
1434 @arguments("cpu", "r", "d", returns="r")
1435 def bhimpl_getfield_gc_r(cpu, struct, fielddescr):
1436 return cpu.bh_getfield_gc_r(struct, fielddescr)
1437 @arguments("cpu", "r", "d", returns="f")
1438 def bhimpl_getfield_gc_f(cpu, struct, fielddescr):
1439 return cpu.bh_getfield_gc_f(struct, fielddescr)
1440
1441 bhimpl_getfield_gc_i_pure = bhimpl_getfield_gc_i
1442 bhimpl_getfield_gc_r_pure = bhimpl_getfield_gc_r
1443 bhimpl_getfield_gc_f_pure = bhimpl_getfield_gc_f
1444
1445 @arguments("cpu", "r", "d", returns="i")
1446 def bhimpl_getfield_vable_i(cpu, struct, fielddescr):
1447 fielddescr.get_vinfo().clear_vable_token(struct)
1448 return cpu.bh_getfield_gc_i(struct, fielddescr)
1449
1450 @arguments("cpu", "r", "d", returns="r")
1451 def bhimpl_getfield_vable_r(cpu, struct, fielddescr):
1452 fielddescr.get_vinfo().clear_vable_token(struct)
1453 return cpu.bh_getfield_gc_r(struct, fielddescr)
1454
1455 @arguments("cpu", "r", "d", returns="f")
1456 def bhimpl_getfield_vable_f(cpu, struct, fielddescr):
1457 fielddescr.get_vinfo().clear_vable_token(struct)
1458 return cpu.bh_getfield_gc_f(struct, fielddescr)
1459
1460 bhimpl_getfield_gc_i_greenfield = bhimpl_getfield_gc_i
1461 bhimpl_getfield_gc_r_greenfield = bhimpl_getfield_gc_r
1462 bhimpl_getfield_gc_f_greenfield = bhimpl_getfield_gc_f
1463
1464 @arguments("cpu", "i", "d", returns="i")
1465 def bhimpl_getfield_raw_i(cpu, struct, fielddescr):
1466 return cpu.bh_getfield_raw_i(struct, fielddescr)
1467 @arguments("cpu", "i", "d", returns="r")
1468 def bhimpl_getfield_raw_r(cpu, struct, fielddescr): # for pure only
1469 return cpu.bh_getfield_raw_r(struct, fielddescr)
1470 @arguments("cpu", "i", "d", returns="f")
1471 def bhimpl_getfield_raw_f(cpu, struct, fielddescr):
1472 return cpu.bh_getfield_raw_f(struct, fielddescr)
1473
1474 @arguments("cpu", "r", "i", "d")
1475 def bhimpl_setfield_gc_i(cpu, struct, newvalue, fielddescr):
1476 cpu.bh_setfield_gc_i(struct, newvalue, fielddescr)
1477 @arguments("cpu", "r", "r", "d")
1478 def bhimpl_setfield_gc_r(cpu, struct, newvalue, fielddescr):
1479 cpu.bh_setfield_gc_r(struct, newvalue, fielddescr)
1480 @arguments("cpu", "r", "f", "d")
1481 def bhimpl_setfield_gc_f(cpu, struct, newvalue, fielddescr):
1482 cpu.bh_setfield_gc_f(struct, newvalue, fielddescr)
1483
1484 @arguments("cpu", "r", "i", "d")
1485 def bhimpl_setfield_vable_i(cpu, struct, newvalue, fielddescr):
1486 fielddescr.get_vinfo().clear_vable_token(struct)
1487 cpu.bh_setfield_gc_i(struct, newvalue, fielddescr)
1488 @arguments("cpu", "r", "r", "d")
1489 def bhimpl_setfield_vable_r(cpu, struct, newvalue, fielddescr):
1490 fielddescr.get_vinfo().clear_vable_token(struct)
1491 cpu.bh_setfield_gc_r(struct, newvalue, fielddescr)
1492 @arguments("cpu", "r", "f", "d")
1493 def bhimpl_setfield_vable_f(cpu, struct, newvalue, fielddescr):
1494 fielddescr.get_vinfo().clear_vable_token(struct)
1495 cpu.bh_setfield_gc_f(struct, newvalue, fielddescr)
1496
1497 @arguments("cpu", "i", "i", "d")
1498 def bhimpl_setfield_raw_i(cpu, struct, newvalue, fielddescr):
1499 cpu.bh_setfield_raw_i(struct, newvalue, fielddescr)
1500 @arguments("cpu", "i", "f", "d")
1501 def bhimpl_setfield_raw_f(cpu, struct, newvalue, fielddescr):
1502 cpu.bh_setfield_raw_f(struct, newvalue, fielddescr)
1503
1504 @arguments("cpu", "i", "i", "i", "d")
1505 def bhimpl_raw_store_i(cpu, addr, offset, newvalue, arraydescr):
1506 cpu.bh_raw_store_i(addr, offset, newvalue, arraydescr)
1507 @arguments("cpu", "i", "i", "f", "d")
1508 def bhimpl_raw_store_f(cpu, addr, offset, newvalue, arraydescr):
1509 cpu.bh_raw_store_f(addr, offset, newvalue, arraydescr)
1510
1511 @arguments("cpu", "i", "i", "d", returns="i")
1512 def bhimpl_raw_load_i(cpu, addr, offset, arraydescr):
1513 return cpu.bh_raw_load_i(addr, offset, arraydescr)
1514 @arguments("cpu", "i", "i", "d", returns="f")
1515 def bhimpl_raw_load_f(cpu, addr, offset, arraydescr):
1516 return cpu.bh_raw_load_f(addr, offset, arraydescr)
1517
1518 @arguments("cpu", "r", "i", "i", "i", "i", returns="i")
1519 def bhimpl_gc_load_indexed_i(cpu, addr, index, scale, base_ofs, bytes):
1520 return cpu.bh_gc_load_indexed_i(addr, index,scale,base_ofs, bytes)
1521 @arguments("cpu", "r", "i", "i", "i", "i", returns="f")
1522 def bhimpl_gc_load_indexed_f(cpu, addr, index, scale, base_ofs, bytes):
1523 return cpu.bh_gc_load_indexed_f(addr, index,scale,base_ofs, bytes)
1524
1525 @arguments("cpu", "r", "i", "i", "i", "i", "i", "d")
1526 def bhimpl_gc_store_indexed_i(cpu, addr, index, val, scale, base_ofs, bytes,
1527 arraydescr):
1528 return cpu.bh_gc_store_indexed_i(addr, index, val, scale,base_ofs, bytes,
1529 arraydescr)
1530
1531 @arguments("cpu", "r", "i", "f", "i", "i", "i", "d")
1532 def bhimpl_gc_store_indexed_f(cpu, addr, index, val, scale, base_ofs, bytes,
1533 arraydescr):
1534 return cpu.bh_gc_store_indexed_f(addr, index, val, scale,base_ofs, bytes,
1535 arraydescr)
1536
1537 @arguments("r", "d", "d")
1538 def bhimpl_record_quasiimmut_field(struct, fielddescr, mutatefielddescr):
1539 pass
1540
1541 @arguments("cpu", "r", "d")
1542 def bhimpl_jit_force_quasi_immutable(cpu, struct, mutatefielddescr):
1543 from rpython.jit.metainterp import quasiimmut
1544 quasiimmut.do_force_quasi_immutable(cpu, struct, mutatefielddescr)
1545
1546 @arguments("r")
1547 def bhimpl_hint_force_virtualizable(r):
1548 pass
1549
1550 @arguments("cpu", "d", returns="r")
1551 def bhimpl_new(cpu, descr):
1552 return cpu.bh_new(descr)
1553
1554 @arguments("cpu", "d", returns="r")
1555 def bhimpl_new_with_vtable(cpu, descr):
1556 return cpu.bh_new_with_vtable(descr)
1557
1558 @arguments("cpu", "r", returns="i")
1559 def bhimpl_guard_class(cpu, struct):
1560 return cpu.bh_classof(struct)
1561
1562 @arguments("cpu", "i", returns="r")
1563 def bhimpl_newstr(cpu, length):
1564 return cpu.bh_newstr(length)
1565 @arguments("cpu", "r", returns="i")
1566 def bhimpl_strlen(cpu, string):
1567 return cpu.bh_strlen(string)
1568 @arguments("cpu", "r", "i", returns="i")
1569 def bhimpl_strgetitem(cpu, string, index):
1570 return cpu.bh_strgetitem(string, index)
1571 @arguments("cpu", "r", "i", "i")
1572 def bhimpl_strsetitem(cpu, string, index, newchr):
1573 cpu.bh_strsetitem(string, index, newchr)
1574 @arguments("cpu", "r", "r", "i", "i", "i")
1575 def bhimpl_copystrcontent(cpu, src, dst, srcstart, dststart, length):
1576 cpu.bh_copystrcontent(src, dst, srcstart, dststart, length)
1577 @arguments("cpu", "r", returns="i")
1578 def bhimpl_strhash(cpu, string):
1579 return cpu.bh_strhash(string)
1580
1581 @arguments("cpu", "i", returns="r")
1582 def bhimpl_newunicode(cpu, length):
1583 return cpu.bh_newunicode(length)
1584 @arguments("cpu", "r", returns="i")
1585 def bhimpl_unicodelen(cpu, unicode):
1586 return cpu.bh_unicodelen(unicode)
1587 @arguments("cpu", "r", "i", returns="i")
1588 def bhimpl_unicodegetitem(cpu, unicode, index):
1589 return cpu.bh_unicodegetitem(unicode, index)
1590 @arguments("cpu", "r", "i", "i")
1591 def bhimpl_unicodesetitem(cpu, unicode, index, newchr):
1592 cpu.bh_unicodesetitem(unicode, index, newchr)
1593 @arguments("cpu", "r", "r", "i", "i", "i")
1594 def bhimpl_copyunicodecontent(cpu, src, dst, srcstart, dststart, length):
1595 cpu.bh_copyunicodecontent(src, dst, srcstart, dststart, length)
1596 @arguments("cpu", "r", returns="i")
1597 def bhimpl_unicodehash(cpu, unicode):
1598 return cpu.bh_unicodehash(unicode)
1599
1600 @arguments("i", "i")
1601 def bhimpl_rvmprof_code(leaving, unique_id):
1602 from rpython.rlib.rvmprof import cintf
1603 cintf.jit_rvmprof_code(leaving, unique_id)
1604
1605 @arguments("pc", returns="L")
1606 def bhimpl_live(pc):
1607 return pc + OFFSET_SIZE
1608
1609 # ----------
1610 # helpers to resume running in blackhole mode when a guard failed
1611
1612 def _resume_mainloop(self, current_exc):
1613 assert lltype.typeOf(current_exc) == rclass.OBJECTPTR
1614 try:
1615 # if there is a current exception, raise it now
1616 # (it may be caught by a catch_operation in this frame)
1617 if current_exc:
1618 self.handle_exception_in_frame(current_exc)
1619 # unless the call above raised again the exception,
1620 # we now proceed to interpret the bytecode in this frame
1621 self.run()
1622 #
1623 except jitexc.JitException as e:
1624 raise # go through
1625 except Exception as e:
1626 # if we get an exception, return it to the caller frame
1627 current_exc = get_llexception(self.cpu, e)
1628 if not self.nextblackholeinterp:
1629 self._exit_frame_with_exception(current_exc)
1630 return current_exc
1631 #
1632 # pass the frame's return value to the caller
1633 caller = self.nextblackholeinterp
1634 if not caller:
1635 self._done_with_this_frame()
1636 kind = self._return_type
1637 if kind == 'i':
1638 caller._setup_return_value_i(self.get_tmpreg_i())
1639 elif kind == 'r':
1640 caller._setup_return_value_r(self.get_tmpreg_r())
1641 elif kind == 'f':
1642 caller._setup_return_value_f(self.get_tmpreg_f())
1643 else:
1644 assert kind == 'v'
1645 return lltype.nullptr(rclass.OBJECTPTR.TO)
1646
1647 def _prepare_resume_from_failure(self, deadframe):
1648 return lltype.cast_opaque_ptr(rclass.OBJECTPTR,
1649 self.cpu.grab_exc_value(deadframe))
1650
1651 # connect the return of values from the called frame to the
1652 # 'xxx_call_yyy' instructions from the caller frame
1653 def _setup_return_value_i(self, result):
1654 assert lltype.typeOf(result) is lltype.Signed
1655 self.registers_i[ord(self.jitcode.code[self.position-1])] = plain_int(
1656 result)
1657 def _setup_return_value_r(self, result):
1658 assert lltype.typeOf(result) == llmemory.GCREF
1659 self.registers_r[ord(self.jitcode.code[self.position-1])] = result
1660 def _setup_return_value_f(self, result):
1661 assert lltype.typeOf(result) is longlong.FLOATSTORAGE
1662 self.registers_f[ord(self.jitcode.code[self.position-1])] = result
1663
1664 def _done_with_this_frame(self):
1665 # rare case: we only get there if the blackhole interps all returned
1666 # normally (in general we get a ContinueRunningNormally exception).
1667 kind = self._return_type
1668 if kind == 'v':
1669 raise jitexc.DoneWithThisFrameVoid()
1670 elif kind == 'i':
1671 raise jitexc.DoneWithThisFrameInt(self.get_tmpreg_i())
1672 elif kind == 'r':
1673 raise jitexc.DoneWithThisFrameRef(self.get_tmpreg_r())
1674 elif kind == 'f':
1675 raise jitexc.DoneWithThisFrameFloat(self.get_tmpreg_f())
1676 else:
1677 assert False
1678
1679 def _exit_frame_with_exception(self, e):
1680 sd = self.builder.metainterp_sd
1681 e = lltype.cast_opaque_ptr(llmemory.GCREF, e)
1682 raise jitexc.ExitFrameWithExceptionRef(e)
1683
1684 def _handle_jitexception_in_portal(self, e):
1685 # This case is really rare, but can occur if
1686 # convert_and_run_from_pyjitpl() gets called in this situation:
1687 #
1688 # [function 1] <---- top BlackholeInterpreter()
1689 # [recursive portal jit code]
1690 # ...
1691 # [bottom portal jit code] <---- bottom BlackholeInterpreter()
1692 #
1693 # and then "function 1" contains a call to "function 2", which
1694 # calls "can_enter_jit". The latter can terminate by raising a
1695 # JitException. In that case, the JitException is not supposed
1696 # to fall through the whole chain of BlackholeInterpreters, but
1697 # be caught and handled just below the level "recursive portal
1698 # jit code". The present function is called to handle the case
1699 # of recursive portal jit codes.
1700 for jd in self.builder.metainterp_sd.jitdrivers_sd:
1701 if jd.mainjitcode is self.jitcode:
1702 break
1703 else:
1704 assert 0, "portal jitcode not found??"
1705 # call the helper in warmspot.py. It might either raise a
1706 # regular exception (which should then be propagated outside
1707 # of 'self', not caught inside), or return (the return value
1708 # gets stored in nextblackholeinterp).
1709 jd.handle_jitexc_from_bh(self.nextblackholeinterp, e)
1710
1711 def _copy_data_from_miframe(self, miframe):
1712 self.setposition(miframe.jitcode, miframe.pc)
1713 for i in range(self.jitcode.num_regs_i()):
1714 box = miframe.registers_i[i]
1715 if not we_are_translated() and isinstance(box, MissingValue):
1716 continue
1717 if box is not None:
1718 self.setarg_i(i, box.getint())
1719 for i in range(self.jitcode.num_regs_r()):
1720 box = miframe.registers_r[i]
1721 if not we_are_translated() and isinstance(box, MissingValue):
1722 continue
1723 if box is not None:
1724 self.setarg_r(i, box.getref_base())
1725 for i in range(self.jitcode.num_regs_f()):
1726 box = miframe.registers_f[i]
1727 if not we_are_translated() and isinstance(box, MissingValue):
1728 continue
1729 if box is not None:
1730 self.setarg_f(i, box.getfloatstorage())
1731
1732 @specialize.arg(3)
1733 def _get_list_of_values(self, code, position, argtype):
1734 length = ord(code[position])
1735 position += 1
1736 value = []
1737 for i in range(length):
1738 index = ord(code[position+i])
1739 if argtype == 'I': reg = self.registers_i[index]
1740 elif argtype == 'R': reg = self.registers_r[index]
1741 elif argtype == 'F': reg = self.registers_f[index]
1742 else: assert 0
1743 if not we_are_translated():
1744 assert not isinstance(reg, MissingValue), (
1745 name, self.jitcode, position)
1746 value.append(reg)
1747 make_sure_not_resized(value)
1748 return value
1749
1750 # ____________________________________________________________
1751
1752 def _run_forever(blackholeinterp, current_exc):
1753 while True:
1754 try:
1755 current_exc = blackholeinterp._resume_mainloop(current_exc)
1756 except jitexc.JitException as e:
1757 blackholeinterp, current_exc = _handle_jitexception(
1758 blackholeinterp, e)
1759 blackholeinterp.builder.release_interp(blackholeinterp)
1760 blackholeinterp = blackholeinterp.nextblackholeinterp
1761
1762 def _handle_jitexception(blackholeinterp, exc):
1763 # See comments in _handle_jitexception_in_portal().
1764 while blackholeinterp.jitcode.jitdriver_sd is None:
1765 blackholeinterp.builder.release_interp(blackholeinterp)
1766 blackholeinterp = blackholeinterp.nextblackholeinterp
1767 if blackholeinterp.nextblackholeinterp is None:
1768 blackholeinterp.builder.release_interp(blackholeinterp)
1769 raise exc # bottommost entry: go through
1770 # We have reached a recursive portal level.
1771 try:
1772 blackholeinterp._handle_jitexception_in_portal(exc)
1773 except Exception as e:
1774 # It raised a general exception (it should not be a JitException here).
1775 lle = get_llexception(blackholeinterp.cpu, e)
1776 else:
1777 # It set up the nextblackholeinterp to contain the return value.
1778 lle = lltype.nullptr(rclass.OBJECTPTR.TO)
1779 # We will continue to loop in _run_forever() from the parent level.
1780 return blackholeinterp, lle
1781
1782 def resume_in_blackhole(metainterp_sd, jitdriver_sd, resumedescr, deadframe,
1783 all_virtuals=None):
1784 from rpython.jit.metainterp.resume import blackhole_from_resumedata
1785 #debug_start('jit-blackhole')
1786 blackholeinterp = blackhole_from_resumedata(
1787 metainterp_sd.blackholeinterpbuilder,
1788 metainterp_sd.jitcodes,
1789 jitdriver_sd,
1790 resumedescr,
1791 deadframe,
1792 all_virtuals)
1793
1794 current_exc = blackholeinterp._prepare_resume_from_failure(deadframe)
1795
1796 _run_forever(blackholeinterp, current_exc)
1797 resume_in_blackhole._dont_inline_ = True
1798
1799 def convert_and_run_from_pyjitpl(metainterp, raising_exception=False):
1800 # Get a chain of blackhole interpreters and fill them by copying
1801 # 'metainterp.framestack'.
1802 #debug_start('jit-blackhole')
1803 metainterp_sd = metainterp.staticdata
1804 nextbh = None
1805 for frame in metainterp.framestack:
1806 curbh = metainterp_sd.blackholeinterpbuilder.acquire_interp()
1807 curbh._copy_data_from_miframe(frame)
1808 curbh.nextblackholeinterp = nextbh
1809 nextbh = curbh
1810 firstbh = nextbh
1811 #
1812 if metainterp.last_exc_value:
1813 current_exc = metainterp.last_exc_value
1814 else:
1815 current_exc = lltype.nullptr(rclass.OBJECTPTR.TO)
1816 if not raising_exception:
1817 firstbh.exception_last_value = current_exc
1818 current_exc = lltype.nullptr(rclass.OBJECTPTR.TO)
1819 #
1820 _run_forever(firstbh, current_exc)
1821 convert_and_run_from_pyjitpl._dont_inline_ = True