| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
from rpython.jit.metainterp.history import AbstractDescr, ConstInt |
| 4 |
from rpython.jit.metainterp.support import adr2int |
| 5 |
from rpython.rlib.objectmodel import we_are_translated, specialize |
| 6 |
from rpython.rlib.rarithmetic import base_int |
| 7 |
|
| 8 |
|
| 9 |
class JitCode(AbstractDescr): |
| 10 |
_empty_i = [] |
| 11 |
_empty_r = [] |
| 12 |
_empty_f = [] |
| 13 |
|
| 14 |
def __init__(self, name, fnaddr=None, calldescr=None, called_from=None): |
| 15 |
self.name = name |
| 16 |
self.fnaddr = fnaddr |
| 17 |
self.calldescr = calldescr |
| 18 |
self.jitdriver_sd = None |
| 19 |
self._called_from = called_from |
| 20 |
self._ssarepr = None |
| 21 |
|
| 22 |
def setup(self, code='', constants_i=[], constants_r=[], constants_f=[], |
| 23 |
num_regs_i=255, num_regs_r=255, num_regs_f=255, |
| 24 |
startpoints=None, alllabels=None, |
| 25 |
resulttypes=None): |
| 26 |
self.code = code |
| 27 |
for x in constants_i: |
| 28 |
assert not isinstance(x, base_int), ( |
| 29 |
"found constant %r of type %r, must not appear in " |
| 30 |
"JitCode.constants_i" % (x, type(x))) |
| 31 |
|
| 32 |
self.constants_i = constants_i or self._empty_i |
| 33 |
self.constants_r = constants_r or self._empty_r |
| 34 |
self.constants_f = constants_f or self._empty_f |
| 35 |
|
| 36 |
assert num_regs_i < 256 and num_regs_r < 256 and num_regs_f < 256 |
| 37 |
self.c_num_regs_i = chr(num_regs_i) |
| 38 |
self.c_num_regs_r = chr(num_regs_r) |
| 39 |
self.c_num_regs_f = chr(num_regs_f) |
| 40 |
self._startpoints = startpoints |
| 41 |
self._alllabels = alllabels |
| 42 |
self._resulttypes = resulttypes |
| 43 |
|
| 44 |
def get_fnaddr_as_int(self): |
| 45 |
return adr2int(self.fnaddr) |
| 46 |
|
| 47 |
def num_regs_i(self): |
| 48 |
return ord(self.c_num_regs_i) |
| 49 |
|
| 50 |
def num_regs_r(self): |
| 51 |
return ord(self.c_num_regs_r) |
| 52 |
|
| 53 |
def num_regs_f(self): |
| 54 |
return ord(self.c_num_regs_f) |
| 55 |
|
| 56 |
def num_regs_and_consts_i(self): |
| 57 |
return ord(self.c_num_regs_i) + len(self.constants_i) |
| 58 |
|
| 59 |
def num_regs_and_consts_r(self): |
| 60 |
return ord(self.c_num_regs_r) + len(self.constants_r) |
| 61 |
|
| 62 |
def num_regs_and_consts_f(self): |
| 63 |
return ord(self.c_num_regs_f) + len(self.constants_f) |
| 64 |
|
| 65 |
|
| 66 |
def _live_vars(self, pc, all_liveness, op_live): |
| 67 |
from rpython.jit.codewriter.liveness import LivenessIterator |
| 68 |
|
| 69 |
if ord(self.code[pc]) != op_live: |
| 70 |
self._missing_liveness(pc) |
| 71 |
offset = self.get_live_vars_info(pc, op_live) |
| 72 |
lst_i = [] |
| 73 |
lst_r = [] |
| 74 |
lst_f = [] |
| 75 |
enumerate_vars(offset, all_liveness, |
| 76 |
lambda index: lst_i.append("%%i%d" % (index, )), |
| 77 |
lambda index: lst_r.append("%%r%d" % (index, )), |
| 78 |
lambda index: lst_f.append("%%f%d" % (index, )), |
| 79 |
None) |
| 80 |
return ' '.join(lst_i + lst_r + lst_f) |
| 81 |
|
| 82 |
def get_live_vars_info(self, pc, op_live): |
| 83 |
from rpython.jit.codewriter.liveness import decode_offset, OFFSET_SIZE |
| 84 |
|
| 85 |
if not we_are_translated(): |
| 86 |
assert pc in self._startpoints |
| 87 |
if ord(self.code[pc]) != op_live: |
| 88 |
pc -= OFFSET_SIZE + 1 |
| 89 |
if not we_are_translated(): |
| 90 |
assert pc in self._startpoints |
| 91 |
if ord(self.code[pc]) != op_live: |
| 92 |
self._missing_liveness(pc) |
| 93 |
return decode_offset(self.code, pc + 1) |
| 94 |
|
| 95 |
def _missing_liveness(self, pc): |
| 96 |
msg = "missing liveness[%d] in %s" % (pc, self.name) |
| 97 |
if we_are_translated(): |
| 98 |
print(msg) |
| 99 |
raise AssertionError |
| 100 |
raise MissingLiveness("%s\n%s" % (msg, self.dump())) |
| 101 |
|
| 102 |
def follow_jump(self, position): |
| 103 |
"""Assuming that 'position' points just after a bytecode |
| 104 |
instruction that ends with a label, follow that label.""" |
| 105 |
code = self.code |
| 106 |
position -= 2 |
| 107 |
assert position >= 0 |
| 108 |
if not we_are_translated(): |
| 109 |
assert position in self._alllabels |
| 110 |
labelvalue = ord(code[position]) | (ord(code[position+1])<<8) |
| 111 |
assert labelvalue < len(code) |
| 112 |
return labelvalue |
| 113 |
|
| 114 |
def dump(self): |
| 115 |
if self._ssarepr is None: |
| 116 |
return '<no dump available for %r>' % (self.name,) |
| 117 |
else: |
| 118 |
from rpython.jit.codewriter.format import format_assembler |
| 119 |
return format_assembler(self._ssarepr) |
| 120 |
|
| 121 |
def __repr__(self): |
| 122 |
return '<JitCode %r>' % self.name |
| 123 |
|
| 124 |
def _clone_if_mutable(self): |
| 125 |
raise NotImplementedError |
| 126 |
|
| 127 |
class MissingLiveness(Exception): |
| 128 |
pass |
| 129 |
|
| 130 |
|
| 131 |
class SwitchDictDescr(AbstractDescr): |
| 132 |
"Get a 'dict' attribute mapping integer values to bytecode positions." |
| 133 |
|
| 134 |
def attach(self, as_dict): |
| 135 |
self.dict = as_dict |
| 136 |
self.const_keys_in_order = map(ConstInt, sorted(as_dict.keys())) |
| 137 |
|
| 138 |
def __repr__(self): |
| 139 |
dict = getattr(self, 'dict', '?') |
| 140 |
return '<SwitchDictDescr %s>' % (dict,) |
| 141 |
|
| 142 |
def _clone_if_mutable(self): |
| 143 |
raise NotImplementedError |
| 144 |
|
| 145 |
|
| 146 |
@specialize.arg(5) |
| 147 |
def enumerate_vars(offset, all_liveness, callback_i, callback_r, callback_f, spec): |
| 148 |
from rpython.jit.codewriter.liveness import LivenessIterator |
| 149 |
length_i = ord(all_liveness[offset]) |
| 150 |
length_r = ord(all_liveness[offset + 1]) |
| 151 |
length_f = ord(all_liveness[offset + 2]) |
| 152 |
offset += 3 |
| 153 |
if length_i: |
| 154 |
it = LivenessIterator(offset, length_i, all_liveness) |
| 155 |
for index in it: |
| 156 |
callback_i(index) |
| 157 |
offset = it.offset |
| 158 |
if length_r: |
| 159 |
it = LivenessIterator(offset, length_r, all_liveness) |
| 160 |
for index in it: |
| 161 |
callback_r(index) |
| 162 |
offset = it.offset |
| 163 |
if length_f: |
| 164 |
it = LivenessIterator(offset, length_f, all_liveness) |
| 165 |
for index in it: |
| 166 |
callback_f(index) |