Debian Codegraph @

rpython/jit/metainterp/optimizeopt/virtualstate.py

raw symbols SCIP
1 from rpython.jit.metainterp.walkvirtual import VirtualVisitor
2 from rpython.jit.metainterp.history import ConstInt, ConstPtr, ConstFloat
3 from rpython.jit.metainterp.optimizeopt.info import ArrayPtrInfo,\
4 ArrayStructInfo, AbstractStructPtrInfo
5 from rpython.jit.metainterp.optimizeopt.intutils import \
6 IntBound
7 from rpython.jit.metainterp.resoperation import rop, ResOperation, \
8 InputArgInt, InputArgRef, InputArgFloat
9 from .info import getptrinfo
10 from .util import get_box_replacement
11 from rpython.rlib.debug import debug_print
12
13 LEVEL_UNKNOWN = '\x00'
14 LEVEL_NONNULL = '\x01'
15 LEVEL_KNOWNCLASS = '\x02'
16 LEVEL_CONSTANT = '\x03'
17
18 class VirtualStatesCantMatch(Exception):
19 def __init__(self, msg='?', state=None):
20 self.msg = msg
21 self.state = state
22
23
24 class GenerateGuardState(object):
25 def __init__(self, optimizer=None, guards=None, renum=None, bad=None, force_boxes=False):
26 self.optimizer = optimizer
27 self.cpu = optimizer.cpu
28 if guards is None:
29 guards = []
30 self.extra_guards = guards
31 if renum is None:
32 renum = {}
33 self.renum = renum
34 if bad is None:
35 bad = {}
36 self.bad = bad
37 self.force_boxes = force_boxes
38
39 def get_runtime_item(self, box, descr, i):
40 array = box.getref_base()
41 if descr.is_array_of_pointers():
42 return InputArgRef(self.cpu.bh_getarrayitem_gc_r(array, i, descr))
43 elif descr.is_array_of_floats():
44 return InputArgFloat(self.cpu.bh_getarrayitem_gc_f(array, i, descr))
45 else:
46 return InputArgInt(self.cpu.bh_getarrayitem_gc_i(array, i, descr))
47
48 def get_runtime_field(self, box, descr):
49 struct = box.getref_base()
50 if descr.is_pointer_field():
51 return InputArgRef(self.cpu.bh_getfield_gc_r(struct, descr))
52 elif descr.is_float_field():
53 return InputArgFloat(self.cpu.bh_getfield_gc_f(struct, descr))
54 else:
55 return InputArgInt(self.cpu.bh_getfield_gc_i(struct, descr))
56
57 def get_runtime_interiorfield(self, box, descr, i):
58 struct = box.getref_base()
59 if descr.is_pointer_field():
60 return InputArgRef(self.cpu.bh_getinteriorfield_gc_r(struct, i,
61 descr))
62 elif descr.is_float_field():
63 return InputArgFloat(self.cpu.bh_getinteriorfield_gc_f(struct, i,
64 descr))
65 else:
66 return InputArgInt(self.cpu.bh_getinteriorfield_gc_i(struct, i,
67 descr))
68
69 class AbstractVirtualStateInfo(object):
70 position = -1
71
72 def generate_guards(self, other, op, runtime_op, state):
73 """ generate guards (output in the list extra_guards) that make runtime
74 values of the shape other match the shape of self. if that's not
75 possible, VirtualStatesCantMatch is thrown and bad gets keys set which
76 parts of the state are the problem.
77
78 the function can peek into the information about the op, as well
79 as runtime value (passed in runtime_op)
80 as a guiding heuristic whether making such guards makes
81 sense. if None is passed in for op, no guard is ever generated, and
82 this function degenerates to a generalization check."""
83 assert self.position != -1
84 if self.position in state.renum:
85 if state.renum[self.position] != other.position:
86 state.bad[self] = state.bad[other] = None
87 raise VirtualStatesCantMatch(
88 'The numbering of the virtual states does not ' +
89 'match. This means that two virtual fields ' +
90 'have been set to the same Box in one of the ' +
91 'virtual states but not in the other.',
92 state)
93 else:
94 state.renum[self.position] = other.position
95 try:
96 self._generate_guards(other, op, runtime_op, state)
97 except VirtualStatesCantMatch as e:
98 state.bad[self] = state.bad[other] = None
99 if e.state is None:
100 e.state = state
101 raise e
102
103 def _generate_guards(self, other, box, runtime_box, state):
104 raise VirtualStatesCantMatch(
105 'Generating guards for making the VirtualStates ' +
106 'at hand match have not been implemented')
107
108 def enum_forced_boxes(self, boxes, box, optimizer, force_boxes=False):
109 raise NotImplementedError
110
111 def enum(self, virtual_state):
112 if self.position != -1:
113 return
114 virtual_state.info_counter += 1
115 self.position = virtual_state.info_counter
116 self._enum(virtual_state)
117
118 def _enum(self, virtual_state):
119 raise NotImplementedError
120
121 def debug_print(self, indent, seen, bad, metainterp_sd):
122 mark = ''
123 if self in bad:
124 mark = '*'
125 self.debug_header(indent + mark)
126 if self not in seen:
127 seen[self] = True
128 for s in self.fieldstate:
129 s.debug_print(indent + " ", seen, bad, metainterp_sd)
130 else:
131 debug_print(indent + " ...")
132
133 def debug_header(self, indent):
134 raise NotImplementedError
135
136
137 class AbstractVirtualStructStateInfo(AbstractVirtualStateInfo):
138 def __init__(self, fielddescrs):
139 self.fielddescrs = fielddescrs
140
141 def _generate_guards(self, other, box, runtime_box, state):
142 if not self._generalization_of_structpart(other):
143 raise VirtualStatesCantMatch("different kinds of structs")
144
145 assert isinstance(other, AbstractVirtualStructStateInfo)
146 assert len(self.fielddescrs) == len(self.fieldstate)
147 assert len(other.fielddescrs) == len(other.fieldstate)
148 if runtime_box is not None:
149 opinfo = getptrinfo(box)
150 assert opinfo.is_virtual()
151 assert isinstance(opinfo, AbstractStructPtrInfo)
152 else:
153 opinfo = None
154
155 if len(self.fielddescrs) != len(other.fielddescrs):
156 raise VirtualStatesCantMatch("field descrs don't match")
157
158 for i in range(len(self.fielddescrs)):
159 if other.fielddescrs[i] is not self.fielddescrs[i]:
160 raise VirtualStatesCantMatch("field descrs don't match")
161 if runtime_box is not None and opinfo is not None:
162 fieldbox = opinfo._fields[self.fielddescrs[i].get_index()]
163 if fieldbox is not None:
164 fieldbox_runtime = state.get_runtime_field(runtime_box,
165 self.fielddescrs[i])
166 else:
167 fieldbox_runtime = None
168 else:
169 fieldbox = None
170 fieldbox_runtime = None
171 if self.fieldstate[i] is not None:
172 if other.fieldstate[i] is None:
173 raise VirtualStatesCantMatch
174 self.fieldstate[i].generate_guards(other.fieldstate[i],
175 fieldbox,
176 fieldbox_runtime, state)
177
178
179 def _generalization_of_structpart(self, other):
180 raise NotImplementedError
181
182 def enum_forced_boxes(self, boxes, box, optimizer, force_boxes=False):
183 box = get_box_replacement(box)
184 info = getptrinfo(box)
185 if info is None or not info.is_virtual():
186 raise VirtualStatesCantMatch()
187 else:
188 assert isinstance(info, AbstractStructPtrInfo)
189
190 # The min operation ensures we don't wander off either array, as not all
191 # to make_inputargs have validated their inputs with generate_guards.
192 for i in range(min(len(self.fielddescrs), len(info._fields))):
193 state = self.fieldstate[i]
194 if not state:
195 continue
196 if state.position > self.position:
197 fieldbox = info._fields[i]
198 state.enum_forced_boxes(boxes, fieldbox, optimizer, force_boxes)
199
200 def _enum(self, virtual_state):
201 for s in self.fieldstate:
202 if s:
203 s.enum(virtual_state)
204
205
206 class VirtualStateInfo(AbstractVirtualStructStateInfo):
207 def is_virtual(self):
208 return True
209
210 def __init__(self, known_class, fielddescrs):
211 AbstractVirtualStructStateInfo.__init__(self, fielddescrs)
212 self.known_class = known_class
213
214 def _generalization_of_structpart(self, other):
215 return (isinstance(other, VirtualStateInfo) and
216 self.known_class.same_constant(other.known_class))
217
218
219 def debug_header(self, indent):
220 debug_print(indent + 'VirtualStateInfo(%d):' % self.position)
221
222
223 class VStructStateInfo(AbstractVirtualStructStateInfo):
224 def __init__(self, typedescr, fielddescrs):
225 AbstractVirtualStructStateInfo.__init__(self, fielddescrs)
226 self.typedescr = typedescr
227
228 def _generalization_of_structpart(self, other):
229 return (isinstance(other, VStructStateInfo) and
230 self.typedescr is other.typedescr)
231
232 def debug_header(self, indent):
233 debug_print(indent + 'VStructStateInfo(%d):' % self.position)
234
235
236 class VArrayStateInfo(AbstractVirtualStateInfo):
237
238 def __init__(self, arraydescr):
239 self.arraydescr = arraydescr
240
241 def _generate_guards(self, other, box, runtime_box, state):
242 if not isinstance(other, VArrayStateInfo):
243 raise VirtualStatesCantMatch("other is not an array")
244 if self.arraydescr is not other.arraydescr:
245 raise VirtualStatesCantMatch("other is a different kind of array")
246 if len(self.fieldstate) != len(other.fieldstate):
247 raise VirtualStatesCantMatch("other has a different length")
248 fieldbox = None
249 fieldbox_runtime = None
250 for i in range(len(self.fieldstate)):
251 if runtime_box is not None:
252 opinfo = getptrinfo(box)
253 assert isinstance(opinfo, ArrayPtrInfo)
254 fieldbox = opinfo._items[i]
255 fieldbox_runtime = state.get_runtime_item(runtime_box,
256 self.arraydescr, i)
257 if self.fieldstate[i] is not None:
258 if other.fieldstate[i] is None:
259 raise VirtualStatesCantMatch
260 self.fieldstate[i].generate_guards(other.fieldstate[i],
261 fieldbox, fieldbox_runtime, state)
262
263 def enum_forced_boxes(self, boxes, box, optimizer, force_boxes=False):
264 box = get_box_replacement(box)
265 info = getptrinfo(box)
266 if info is None or not info.is_virtual():
267 raise VirtualStatesCantMatch()
268 if len(self.fieldstate) > info.getlength():
269 raise VirtualStatesCantMatch
270 for i in range(len(self.fieldstate)):
271 fieldbox = info.getitem(self.arraydescr, i)
272 s = self.fieldstate[i]
273 if s is not None:
274 if s.position > self.position:
275 s.enum_forced_boxes(boxes, fieldbox, optimizer, force_boxes)
276
277 def _enum(self, virtual_state):
278 for s in self.fieldstate:
279 if s:
280 s.enum(virtual_state)
281
282 def debug_header(self, indent):
283 debug_print(indent + 'VArrayStateInfo(%d):' % self.position)
284
285
286 class VArrayStructStateInfo(AbstractVirtualStateInfo):
287 def __init__(self, arraydescr, fielddescrs, length):
288 self.arraydescr = arraydescr
289 self.fielddescrs = fielddescrs
290 self.length = length
291
292 def _generate_guards(self, other, box, runtime_box, state):
293 if not isinstance(other, VArrayStructStateInfo):
294 raise VirtualStatesCantMatch("other is not an VArrayStructStateInfo")
295 if self.arraydescr is not other.arraydescr:
296 raise VirtualStatesCantMatch("other is a different kind of array")
297
298 if len(self.fielddescrs) != len(other.fielddescrs):
299 raise VirtualStatesCantMatch("other has a different length")
300
301 if len(self.fielddescrs) != len(other.fielddescrs):
302 raise VirtualStatesCantMatch("other has a different length")
303 for j, descr in enumerate(self.fielddescrs):
304 if descr is not other.fielddescrs[j]:
305 raise VirtualStatesCantMatch("other is a different kind of array")
306 fieldbox = None
307 fieldbox_runtime = None
308 if box is not None:
309 opinfo = getptrinfo(box)
310 assert isinstance(opinfo, ArrayPtrInfo)
311 else:
312 opinfo = None
313 for i in range(self.length):
314 for descr in self.fielddescrs:
315 index = i * len(self.fielddescrs) + descr.get_index()
316 fieldstate = self.fieldstate[index]
317 if fieldstate is None:
318 continue
319 if other.fieldstate[index] is None:
320 raise VirtualStatesCantMatch
321 if box is not None and opinfo is not None:
322 fieldbox = opinfo._items[index]
323 fieldbox_runtime = state.get_runtime_interiorfield(
324 runtime_box, descr, i)
325 self.fieldstate[index].generate_guards(other.fieldstate[index],
326 fieldbox, fieldbox_runtime, state)
327
328 def _enum(self, virtual_state):
329 for s in self.fieldstate:
330 if s is not None:
331 s.enum(virtual_state)
332
333 def enum_forced_boxes(self, boxes, box, optimizer, force_boxes=False):
334 opinfo = getptrinfo(box)
335 if not isinstance(opinfo, ArrayStructInfo):
336 raise VirtualStatesCantMatch
337 if not opinfo.is_virtual():
338 raise VirtualStatesCantMatch
339 #if len(self.fielddescrs) > len(value._items):
340 # raise VirtualStatesCantMatch
341 for i in range(self.length):
342 for descr in self.fielddescrs:
343 index = i * len(self.fielddescrs) + descr.get_index()
344 fieldstate = self.fieldstate[index]
345 itembox = opinfo._items[i * len(self.fielddescrs) +
346 descr.get_index()]
347 if fieldstate is None:
348 if itembox is not None:
349 raise VirtualStatesCantMatch
350 continue
351 # I think itembox must be present here
352 if fieldstate.position > self.position:
353 fieldstate.enum_forced_boxes(boxes, itembox, optimizer,
354 force_boxes)
355
356 def debug_header(self, indent):
357 debug_print(indent + 'VArrayStructStateInfo(%d):' % self.position)
358
359
360 def not_virtual(cpu, type, info):
361 if type == 'i':
362 return NotVirtualStateInfoInt(cpu, type, info)
363 if type == 'r':
364 return NotVirtualStateInfoPtr(cpu, type, info)
365 return NotVirtualStateInfo(cpu, type, info)
366
367
368 class NotVirtualStateInfo(AbstractVirtualStateInfo):
369 level = LEVEL_UNKNOWN
370 constbox = None
371
372 def __init__(self, cpu, type, info):
373 if info and info.is_constant():
374 self.level = LEVEL_CONSTANT
375 self.constbox = info.getconst()
376
377 def is_const(self):
378 return self.constbox is not None
379
380 def is_virtual(self):
381 return False
382
383 def _generate_guards(self, other, box, runtime_box, state):
384 # XXX This will always retrace instead of forcing anything which
385 # might be what we want sometimes?
386 extra_guards = state.extra_guards
387 if self.level == LEVEL_UNKNOWN:
388 return self._generate_guards_unkown(other, box, runtime_box,
389 extra_guards,
390 state)
391 else:
392 if not isinstance(other, NotVirtualStateInfo):
393 raise VirtualStatesCantMatch(
394 'comparing a constant against something that is a virtual')
395 assert self.level == LEVEL_CONSTANT
396 if other.level == LEVEL_CONSTANT:
397 if self.constbox.same_constant(other.constbox):
398 return
399 raise VirtualStatesCantMatch("different constants")
400 if runtime_box is not None and self.constbox.same_constant(runtime_box.constbox()):
401 op = ResOperation(rop.GUARD_VALUE, [box, self.constbox])
402 extra_guards.append(op)
403 return
404 else:
405 raise VirtualStatesCantMatch("other not constant")
406 assert 0, "unreachable"
407
408 def _generate_guards_unkown(self, other, box, runtime_box, extra_guards,
409 state):
410 return
411
412 def enum_forced_boxes(self, boxes, box, optimizer, force_boxes=False):
413 if self.level == LEVEL_CONSTANT:
414 return
415 assert 0 <= self.position_in_notvirtuals
416 assert optimizer is not None
417 box = get_box_replacement(box)
418 if box.type == 'r':
419 info = getptrinfo(box)
420 if info and info.is_virtual():
421 if force_boxes:
422 info.force_box(box, optimizer)
423 else:
424 raise VirtualStatesCantMatch
425 boxes[self.position_in_notvirtuals] = box
426
427 def _enum(self, virtual_state):
428 if self.level == LEVEL_CONSTANT:
429 return
430 self.position_in_notvirtuals = virtual_state.numnotvirtuals
431 virtual_state.numnotvirtuals += 1
432
433 def debug_print(self, indent, seen, bad, metainterp_sd=None):
434 mark = ''
435 if self in bad:
436 mark = '*'
437 if self.level == LEVEL_UNKNOWN:
438 l = "Unknown"
439 elif self.level == LEVEL_NONNULL:
440 l = "NonNull"
441 elif self.level == LEVEL_KNOWNCLASS:
442 addr = self.known_class.getaddr()
443 if metainterp_sd:
444 name = metainterp_sd.get_name_from_address(addr)
445 else:
446 name = "?"
447 l = "KnownClass(%s)" % name
448 else:
449 assert self.level == LEVEL_CONSTANT
450 const = self.constbox
451 if isinstance(const, ConstInt):
452 l = "ConstInt(%s)" % (const.value, )
453 elif isinstance(const, ConstPtr):
454 if const.value:
455 l = "ConstPtr"
456 else:
457 l = "ConstPtr(null)"
458 else:
459 assert isinstance(const, ConstFloat)
460 l = "ConstFloat(%s)" % const.getfloat()
461
462 lb = ''
463 if self.lenbound:
464 lb = ', ' + self.lenbound.bound.__repr__()
465
466 result = indent + mark + 'NotVirtualStateInfo(%d' % self.position + ', ' + l
467 extra = self._extra_repr()
468 if extra:
469 result += ', ' + extra
470 result += lb + ')'
471 debug_print(result)
472
473 class NotVirtualStateInfoInt(NotVirtualStateInfo):
474 intbound = None
475
476 def __init__(self, cpu, type, info):
477 NotVirtualStateInfo.__init__(self, cpu, type, info)
478 assert type == 'i'
479 if isinstance(info, IntBound):
480 info.widen_update()
481 self.intbound = info
482
483 def _generate_guards_unkown(self, other, box, runtime_box, extra_guards,
484 state):
485 other_intbound = None
486 if isinstance(other, NotVirtualStateInfoInt):
487 other_intbound = other.intbound
488 if self.intbound is None:
489 return
490 assert self.intbound._are_knownbits_implied()
491 if other_intbound.is_within_range(self.intbound.lower, self.intbound.upper):
492 return
493 if (runtime_box is not None and
494 self.intbound.contains(runtime_box.getint())):
495 # this may generate a few more guards than needed, but they are
496 # optimized away when emitting them
497 self.intbound.make_guards(box, extra_guards, state.optimizer)
498 return
499 raise VirtualStatesCantMatch("intbounds don't match")
500
501 def _extra_repr(self):
502 return self.intbound.__repr__()
503
504
505 class NotVirtualStateInfoPtr(NotVirtualStateInfo):
506 lenbound = None
507 known_class = None
508
509 def __init__(self, cpu, type, info):
510 if info:
511 self.known_class = info.get_known_class(cpu)
512 if self.known_class:
513 self.level = LEVEL_KNOWNCLASS
514 elif info.is_nonnull():
515 self.level = LEVEL_NONNULL
516 self.lenbound = info.getlenbound(None)
517 if self.lenbound is not None:
518 self.lenbound = self.lenbound.widen()
519 # might set it to LEVEL_CONSTANT
520 NotVirtualStateInfo.__init__(self, cpu, type, info)
521
522 def _generate_guards(self, other, box, runtime_box, state):
523 if state.force_boxes and isinstance(other, VirtualStateInfo):
524 return self._generate_virtual_guards(other, box, runtime_box, state)
525 if not isinstance(other, NotVirtualStateInfoPtr):
526 raise VirtualStatesCantMatch(
527 'The VirtualStates does not match as a ' +
528 'virtual appears where a pointer is needed ' +
529 'and it is too late to force it.')
530 extra_guards = state.extra_guards
531 if self.lenbound:
532 if other.lenbound is None:
533 other_bound = IntBound.nonnegative()
534 else:
535 other_bound = other.lenbound
536 assert self.lenbound._are_knownbits_implied()
537 if not other_bound.is_within_range(self.lenbound.lower, self.lenbound.upper):
538 raise VirtualStatesCantMatch("length bound does not match")
539 if self.level == LEVEL_NONNULL:
540 return self._generate_guards_nonnull(other, box, runtime_box,
541 extra_guards,
542 state)
543 elif self.level == LEVEL_KNOWNCLASS:
544 return self._generate_guards_knownclass(other, box, runtime_box,
545 extra_guards,
546 state)
547 return NotVirtualStateInfo._generate_guards(self, other, box,
548 runtime_box, state)
549
550
551 # the following methods often peek into the runtime value that the
552 # box had when tracing. This value is only used as an educated guess.
553 # It is used here to choose between either emitting a guard and jumping
554 # to an existing compiled loop or retracing the loop. Both alternatives
555 # will always generate correct behaviour, but performance will differ.
556
557 def _generate_virtual_guards(self, other, box, runtime_box, state):
558 """
559 Generate the guards and add state information for unifying a virtual
560 object with a non-virtual. This involves forcing the object in the
561 event that unification can succeed. Since virtual objects cannot be null,
562 this method need only check that the virtual object has the expected type.
563 """
564 assert state.force_boxes and isinstance(other, VirtualStateInfo)
565
566 if self.level == LEVEL_CONSTANT:
567 raise VirtualStatesCantMatch(
568 "cannot unify a constant value with a virtual object")
569
570 if self.level == LEVEL_KNOWNCLASS:
571 if not self.known_class.same_constant(other.known_class):
572 raise VirtualStatesCantMatch("classes don't match")
573
574 def _generate_guards_nonnull(self, other, box, runtime_box, extra_guards,
575 state):
576 if not isinstance(other, NotVirtualStateInfoPtr):
577 raise VirtualStatesCantMatch('trying to match ptr with non-ptr??!')
578 if other.level == LEVEL_UNKNOWN:
579 if runtime_box is not None and runtime_box.nonnull():
580 op = ResOperation(rop.GUARD_NONNULL, [box])
581 extra_guards.append(op)
582 return
583 else:
584 raise VirtualStatesCantMatch("other not known to be nonnull")
585 elif other.level == LEVEL_NONNULL:
586 pass
587 elif other.level == LEVEL_KNOWNCLASS:
588 pass # implies nonnull
589 else:
590 assert other.level == LEVEL_CONSTANT
591 assert other.constbox
592 if not other.constbox.nonnull():
593 raise VirtualStatesCantMatch("constant is null")
594
595 def _generate_guards_knownclass(self, other, box, runtime_box, extra_guards,
596 state):
597 cpu = state.cpu
598 if not isinstance(other, NotVirtualStateInfoPtr):
599 raise VirtualStatesCantMatch('trying to match ptr with non-ptr??!')
600 if other.level == LEVEL_UNKNOWN:
601 if (runtime_box and runtime_box.nonnull() and
602 self.known_class.same_constant(cpu.cls_of_box(runtime_box))):
603 op = ResOperation(rop.GUARD_NONNULL_CLASS, [box, self.known_class])
604 extra_guards.append(op)
605 else:
606 raise VirtualStatesCantMatch("other's class is unknown")
607 elif other.level == LEVEL_NONNULL:
608 if (runtime_box and self.known_class.same_constant(
609 cpu.cls_of_box(runtime_box))):
610 op = ResOperation(rop.GUARD_CLASS, [box, self.known_class])
611 extra_guards.append(op)
612 else:
613 raise VirtualStatesCantMatch("other's class is unknown")
614 elif other.level == LEVEL_KNOWNCLASS:
615 if self.known_class.same_constant(other.known_class):
616 return
617 raise VirtualStatesCantMatch("classes don't match")
618 else:
619 assert other.level == LEVEL_CONSTANT
620 if (other.constbox.nonnull() and
621 self.known_class.same_constant(cpu.cls_of_box(other.constbox))):
622 pass
623 else:
624 raise VirtualStatesCantMatch("classes don't match")
625
626
627 class VirtualState(object):
628 def __init__(self, state):
629 self.state = state
630 self.info_counter = -1
631 self.numnotvirtuals = 0
632 for s in state:
633 if s:
634 s.enum(self)
635
636 def generalization_of(self, other, optimizer):
637 state = GenerateGuardState(optimizer)
638 assert len(self.state) == len(other.state)
639 try:
640 for i in range(len(self.state)):
641 self.state[i].generate_guards(other.state[i], None, None, state)
642 except VirtualStatesCantMatch:
643 return False
644 return True
645
646 def generate_guards(self, other, boxes, runtime_boxes, optimizer, force_boxes=False):
647 assert (len(self.state) == len(other.state) == len(boxes) ==
648 len(runtime_boxes))
649 state = GenerateGuardState(optimizer, force_boxes=force_boxes)
650 for i in range(len(self.state)):
651 self.state[i].generate_guards(other.state[i], boxes[i],
652 runtime_boxes[i], state)
653 return state
654
655 def make_inputargs(self, inputargs, optimizer, force_boxes=False):
656 if optimizer.optearlyforce:
657 optimizer = optimizer.optearlyforce
658 assert len(inputargs) == len(self.state)
659 boxes = [None] * self.numnotvirtuals
660
661 # We try twice. The first time around we allow boxes to be forced
662 # which might change the virtual state if the box appear in more
663 # than one place among the inputargs.
664 if force_boxes:
665 for i in range(len(inputargs)):
666 self.state[i].enum_forced_boxes(boxes, inputargs[i], optimizer,
667 True)
668 for i in range(len(inputargs)):
669 self.state[i].enum_forced_boxes(boxes, inputargs[i], optimizer)
670
671 return boxes
672
673 def make_inputargs_and_virtuals(self, inputargs, optimizer, force_boxes=False):
674 inpargs = self.make_inputargs(inputargs, optimizer, force_boxes)
675 # we append the virtuals here in case some stuff is proven
676 # to be not a virtual and there are getfields in the short preamble
677 # that will read items out of there
678 virtuals = []
679 for i in range(len(inputargs)):
680 if not isinstance(self.state[i], NotVirtualStateInfo):
681 virtuals.append(inputargs[i])
682
683 return inpargs, virtuals
684
685 def debug_print(self, hdr='', bad=None, metainterp_sd=None):
686 if bad is None:
687 bad = {}
688 debug_print(hdr + "VirtualState():")
689 seen = {}
690 for s in self.state:
691 s.debug_print(" ", seen, bad, metainterp_sd)
692
693
694 class VirtualStateConstructor(VirtualVisitor):
695
696 def __init__(self, optimizer):
697 self.fieldboxes = {}
698 self.optimizer = optimizer
699 self.info = {}
700
701 def register_virtual_fields(self, keybox, fieldboxes):
702 self.fieldboxes[keybox] = fieldboxes
703
704 def already_seen_virtual(self, keybox):
705 return keybox in self.fieldboxes
706
707 def create_state_or_none(self, box):
708 if box is None:
709 return None
710 return self.create_state(box)
711
712 def create_state(self, box):
713 box = get_box_replacement(box)
714 try:
715 return self.info[box]
716 except KeyError:
717 pass
718 if box.type == 'r':
719 info = getptrinfo(box)
720 if info is not None and info.is_virtual():
721 result = info.visitor_dispatch_virtual_type(self)
722 self.info[box] = result
723 info.visitor_walk_recursive(box, self)
724 result.fieldstate = [self.create_state_or_none(b)
725 for b in self.fieldboxes[box]]
726 else:
727 result = self.visit_not_virtual(box)
728 self.info[box] = result
729 elif box.type == 'i' or box.type == 'f':
730 result = self.visit_not_virtual(box)
731 self.info[box] = result
732 else:
733 assert False
734 return result
735
736 def get_virtual_state(self, jump_args):
737 state = []
738 self.info = {}
739 for box in jump_args:
740 state.append(self.create_state(box))
741 return VirtualState(state)
742
743 def visit_not_virtual(self, box):
744 return not_virtual(self.optimizer.cpu, box.type,
745 self.optimizer.getinfo(box))
746
747 def visit_virtual(self, descr, fielddescrs):
748 known_class = ConstInt(descr.get_vtable())
749 return VirtualStateInfo(known_class, fielddescrs)
750
751 def visit_vstruct(self, typedescr, fielddescrs):
752 return VStructStateInfo(typedescr, fielddescrs)
753
754 def visit_varray(self, arraydescr, clear):
755 # 'clear' is ignored here. I *think* it is correct, because so
756 # far in force_at_end_of_preamble() we force all array values
757 # to be non-None, so clearing is not important any more
758 return VArrayStateInfo(arraydescr)
759
760 def visit_varraystruct(self, arraydescr, length, fielddescrs):
761 return VArrayStructStateInfo(arraydescr, fielddescrs, length)