| 1 |
from rpython.jit.metainterp.history import AbstractDescr, getkind |
| 2 |
from rpython.jit.metainterp.support import adr2int, int2adr |
| 3 |
from rpython.jit.codewriter.flatten import Register, Label, TLabel, KINDS |
| 4 |
from rpython.jit.codewriter.flatten import ListOfKind, IndirectCallTargets |
| 5 |
from rpython.jit.codewriter.format import format_assembler |
| 6 |
from rpython.jit.codewriter.jitcode import SwitchDictDescr, JitCode |
| 7 |
from rpython.jit.codewriter import longlong |
| 8 |
from rpython.rlib.objectmodel import ComputedIntSymbolic |
| 9 |
from rpython.rlib.rarithmetic import r_int |
| 10 |
from rpython.flowspace.model import Constant |
| 11 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 12 |
from rpython.rtyper import rclass |
| 13 |
|
| 14 |
|
| 15 |
class AssemblerError(Exception): |
| 16 |
pass |
| 17 |
|
| 18 |
|
| 19 |
class Assembler(object): |
| 20 |
|
| 21 |
def __init__(self): |
| 22 |
self.insns = {} |
| 23 |
self.descrs = [] |
| 24 |
self.indirectcalltargets = set() |
| 25 |
self.list_of_addr2name = [] |
| 26 |
self._descr_dict = {} |
| 27 |
self._count_jitcodes = 0 |
| 28 |
self._seen_raw_objects = set() |
| 29 |
self.all_liveness = [] |
| 30 |
self.all_liveness_length = 0 |
| 31 |
self.all_liveness_positions = {} |
| 32 |
self.num_liveness_ops = 0 |
| 33 |
|
| 34 |
def assemble(self, ssarepr, jitcode=None, num_regs=None): |
| 35 |
"""Take the 'ssarepr' representation of the code and assemble |
| 36 |
it inside the 'jitcode'. If jitcode is None, make a new one. |
| 37 |
""" |
| 38 |
self.setup(ssarepr.name) |
| 39 |
if num_regs is not None: |
| 40 |
self.count_regs.update(num_regs) |
| 41 |
ssarepr._insns_pos = [] |
| 42 |
for insn in ssarepr.insns: |
| 43 |
ssarepr._insns_pos.append(len(self.code)) |
| 44 |
self.write_insn(insn) |
| 45 |
self.fix_labels() |
| 46 |
self.check_result() |
| 47 |
if jitcode is None: |
| 48 |
jitcode = JitCode(ssarepr.name) |
| 49 |
jitcode._ssarepr = ssarepr |
| 50 |
self.make_jitcode(jitcode) |
| 51 |
if self._count_jitcodes < 20: |
| 52 |
jitcode._dump = format_assembler(ssarepr) |
| 53 |
self._count_jitcodes += 1 |
| 54 |
return jitcode |
| 55 |
|
| 56 |
def setup(self, name): |
| 57 |
self.code = [] |
| 58 |
self.constants_dict = {} |
| 59 |
self.constants_i = [] |
| 60 |
self.constants_r = [] |
| 61 |
self.constants_f = [] |
| 62 |
self.label_positions = {} |
| 63 |
self.tlabel_positions = [] |
| 64 |
self.switchdictdescrs = [] |
| 65 |
self.count_regs = dict.fromkeys(KINDS, 0) |
| 66 |
self.liveness = {} |
| 67 |
self.startpoints = set() |
| 68 |
self.alllabels = set() |
| 69 |
self.resulttypes = {} |
| 70 |
self.ssareprname = name |
| 71 |
|
| 72 |
def emit_reg(self, reg): |
| 73 |
assert reg.index < self.count_regs[reg.kind] |
| 74 |
self.code.append(chr(reg.index)) |
| 75 |
|
| 76 |
def count_reg(self, reg): |
| 77 |
if reg.index >= self.count_regs[reg.kind]: |
| 78 |
self.count_regs[reg.kind] = reg.index + 1 |
| 79 |
|
| 80 |
def emit_const(self, const, kind, allow_short=False): |
| 81 |
value = const.value |
| 82 |
value_key = value |
| 83 |
if kind == 'int': |
| 84 |
TYPE = const.concretetype |
| 85 |
if isinstance(TYPE, lltype.Ptr): |
| 86 |
assert TYPE.TO._gckind == 'raw' |
| 87 |
self.see_raw_object(value) |
| 88 |
value = llmemory.cast_ptr_to_adr(value) |
| 89 |
TYPE = llmemory.Address |
| 90 |
if TYPE == llmemory.Address: |
| 91 |
value = adr2int(value) |
| 92 |
if TYPE is lltype.SingleFloat: |
| 93 |
value = longlong.singlefloat2int(value) |
| 94 |
if not isinstance(value, (llmemory.AddressAsInt, |
| 95 |
ComputedIntSymbolic)): |
| 96 |
value = lltype.cast_primitive(lltype.Signed, value) |
| 97 |
if type(value) is r_int: |
| 98 |
value = int(value) |
| 99 |
if allow_short: |
| 100 |
try: |
| 101 |
short_num = -128 <= value <= 127 |
| 102 |
except TypeError: |
| 103 |
short_num = False |
| 104 |
if short_num: |
| 105 |
|
| 106 |
self.code.append(chr(value & 0xFF)) |
| 107 |
return True |
| 108 |
constants = self.constants_i |
| 109 |
elif kind == 'ref': |
| 110 |
value = lltype.cast_opaque_ptr(llmemory.GCREF, value) |
| 111 |
constants = self.constants_r |
| 112 |
if not value: |
| 113 |
|
| 114 |
value_key = None |
| 115 |
else: |
| 116 |
value_key = value._obj.container |
| 117 |
elif kind == 'float': |
| 118 |
if const.concretetype == lltype.Float: |
| 119 |
value = longlong.getfloatstorage(value) |
| 120 |
else: |
| 121 |
assert longlong.is_longlong(const.concretetype) |
| 122 |
value = rffi.cast(lltype.SignedLongLong, value) |
| 123 |
constants = self.constants_f |
| 124 |
else: |
| 125 |
raise AssemblerError('unimplemented %r in %r' % |
| 126 |
(const, self.ssareprname)) |
| 127 |
key = (kind, Constant(value_key)) |
| 128 |
try: |
| 129 |
val = self.constants_dict[key] |
| 130 |
except KeyError: |
| 131 |
constants.append(value) |
| 132 |
val = self.count_regs[kind] + len(constants) - 1 |
| 133 |
assert 0 <= val < 256, "too many constants" |
| 134 |
self.constants_dict[key] = val |
| 135 |
|
| 136 |
|
| 137 |
self.code.append(chr(val)) |
| 138 |
return False |
| 139 |
|
| 140 |
def write_insn(self, insn): |
| 141 |
if insn[0] == '---': |
| 142 |
return |
| 143 |
if isinstance(insn[0], Label): |
| 144 |
self.label_positions[insn[0].name] = len(self.code) |
| 145 |
return |
| 146 |
if insn[0] == '-live-': |
| 147 |
key = len(self.code) |
| 148 |
self.startpoints.add(key) |
| 149 |
self.num_liveness_ops += 1 |
| 150 |
live_i = self.get_liveness_info(insn[1:], 'int') |
| 151 |
live_r = self.get_liveness_info(insn[1:], 'ref') |
| 152 |
live_f = self.get_liveness_info(insn[1:], 'float') |
| 153 |
assert key not in self.liveness |
| 154 |
self.liveness[key] = live_i, live_r, live_f |
| 155 |
num = self.insns.setdefault('live/', len(self.insns)) |
| 156 |
self.code.append(chr(num)) |
| 157 |
self._encode_liveness(live_i, live_r, live_f) |
| 158 |
return |
| 159 |
startposition = len(self.code) |
| 160 |
self.code.append("temporary placeholder") |
| 161 |
|
| 162 |
argcodes = [] |
| 163 |
allow_short = (insn[0] in USE_C_FORM) |
| 164 |
for x in insn[1:]: |
| 165 |
if isinstance(x, Register): |
| 166 |
self.emit_reg(x) |
| 167 |
argcodes.append(x.kind[0]) |
| 168 |
elif isinstance(x, Constant): |
| 169 |
kind = getkind(x.concretetype) |
| 170 |
is_short = self.emit_const(x, kind, allow_short=allow_short) |
| 171 |
if is_short: |
| 172 |
argcodes.append('c') |
| 173 |
else: |
| 174 |
argcodes.append(kind[0]) |
| 175 |
elif isinstance(x, TLabel): |
| 176 |
self.alllabels.add(len(self.code)) |
| 177 |
self.tlabel_positions.append((x.name, len(self.code))) |
| 178 |
self.code.append("temp 1") |
| 179 |
self.code.append("temp 2") |
| 180 |
argcodes.append('L') |
| 181 |
elif isinstance(x, ListOfKind): |
| 182 |
itemkind = x.kind |
| 183 |
lst = list(x) |
| 184 |
assert len(lst) <= 255, "list too long!" |
| 185 |
self.code.append(chr(len(lst))) |
| 186 |
for item in lst: |
| 187 |
if isinstance(item, Register): |
| 188 |
assert itemkind == item.kind |
| 189 |
self.emit_reg(item) |
| 190 |
elif isinstance(item, Constant): |
| 191 |
assert itemkind == getkind(item.concretetype) |
| 192 |
self.emit_const(item, itemkind) |
| 193 |
else: |
| 194 |
raise NotImplementedError("found in ListOfKind(): %r" |
| 195 |
% (item,)) |
| 196 |
argcodes.append(itemkind[0].upper()) |
| 197 |
elif isinstance(x, AbstractDescr): |
| 198 |
if x not in self._descr_dict: |
| 199 |
self._descr_dict[x] = len(self.descrs) |
| 200 |
self.descrs.append(x) |
| 201 |
if isinstance(x, SwitchDictDescr): |
| 202 |
self.switchdictdescrs.append(x) |
| 203 |
num = self._descr_dict[x] |
| 204 |
assert 0 <= num <= 0xFFFF, "too many AbstractDescrs!" |
| 205 |
self.code.append(chr(num & 0xFF)) |
| 206 |
self.code.append(chr(num >> 8)) |
| 207 |
argcodes.append('d') |
| 208 |
elif isinstance(x, IndirectCallTargets): |
| 209 |
self.indirectcalltargets.update(x.lst) |
| 210 |
elif x == '->': |
| 211 |
assert '>' not in argcodes |
| 212 |
argcodes.append('>') |
| 213 |
else: |
| 214 |
raise NotImplementedError(x) |
| 215 |
|
| 216 |
opname = insn[0] |
| 217 |
if '>' in argcodes: |
| 218 |
assert argcodes.index('>') == len(argcodes) - 2 |
| 219 |
self.resulttypes[len(self.code)] = argcodes[-1] |
| 220 |
key = opname + '/' + ''.join(argcodes) |
| 221 |
num = self.insns.setdefault(key, len(self.insns)) |
| 222 |
self.code[startposition] = chr(num) |
| 223 |
self.startpoints.add(startposition) |
| 224 |
|
| 225 |
def get_liveness_info(self, args, kind): |
| 226 |
"""Return a set whose characters are register numbers. |
| 227 |
""" |
| 228 |
lives = set() |
| 229 |
for reg in args: |
| 230 |
if isinstance(reg, Register) and reg.kind == kind: |
| 231 |
lives.add(chr(reg.index)) |
| 232 |
return lives |
| 233 |
|
| 234 |
def _encode_liveness(self, live_i, live_r, live_f): |
| 235 |
from rpython.jit.codewriter.liveness import encode_offset, encode_liveness |
| 236 |
key = (frozenset(live_i), frozenset(live_r), frozenset(live_f)) |
| 237 |
try: |
| 238 |
pos = self.all_liveness_positions[key] |
| 239 |
except KeyError: |
| 240 |
pos = self.all_liveness_positions[key] = self.all_liveness_length |
| 241 |
self.all_liveness.append(chr(len(live_i)) + chr(len(live_r)) + chr(len(live_f))) |
| 242 |
self.all_liveness_length += 3 |
| 243 |
for live in live_i, live_r, live_f: |
| 244 |
liveness = encode_liveness(live) |
| 245 |
if liveness: |
| 246 |
self.all_liveness.append(liveness) |
| 247 |
self.all_liveness_length += len(liveness) |
| 248 |
encode_offset(pos, self.code) |
| 249 |
|
| 250 |
def fix_labels(self): |
| 251 |
for name, pos in self.tlabel_positions: |
| 252 |
assert self.code[pos ] == "temp 1" |
| 253 |
assert self.code[pos+1] == "temp 2" |
| 254 |
target = self.label_positions[name] |
| 255 |
assert 0 <= target <= 0xFFFF |
| 256 |
self.code[pos ] = chr(target & 0xFF) |
| 257 |
self.code[pos+1] = chr(target >> 8) |
| 258 |
for descr in self.switchdictdescrs: |
| 259 |
as_dict = {} |
| 260 |
for key, switchlabel in descr._labels: |
| 261 |
target = self.label_positions[switchlabel.name] |
| 262 |
as_dict[key] = target |
| 263 |
descr.attach(as_dict) |
| 264 |
|
| 265 |
def check_result(self): |
| 266 |
|
| 267 |
assert self.count_regs['int'] + len(self.constants_i) <= 256 |
| 268 |
assert self.count_regs['ref'] + len(self.constants_r) <= 256 |
| 269 |
assert self.count_regs['float'] + len(self.constants_f) <= 256 |
| 270 |
|
| 271 |
def make_jitcode(self, jitcode): |
| 272 |
jitcode.setup(''.join(self.code), |
| 273 |
self.constants_i, |
| 274 |
self.constants_r, |
| 275 |
self.constants_f, |
| 276 |
self.count_regs['int'], |
| 277 |
self.count_regs['ref'], |
| 278 |
self.count_regs['float'], |
| 279 |
startpoints=self.startpoints, |
| 280 |
alllabels=self.alllabels, |
| 281 |
resulttypes=self.resulttypes) |
| 282 |
|
| 283 |
def see_raw_object(self, value): |
| 284 |
if value._obj not in self._seen_raw_objects: |
| 285 |
self._seen_raw_objects.add(value._obj) |
| 286 |
if not value: |
| 287 |
return |
| 288 |
TYPE = lltype.typeOf(value).TO |
| 289 |
if isinstance(TYPE, lltype.FuncType): |
| 290 |
name = value._obj._name |
| 291 |
elif TYPE == rclass.OBJECT_VTABLE: |
| 292 |
if not value.name: |
| 293 |
return |
| 294 |
name = ''.join(value.name.chars) |
| 295 |
else: |
| 296 |
return |
| 297 |
addr = llmemory.cast_ptr_to_adr(value) |
| 298 |
self.list_of_addr2name.append((addr, name)) |
| 299 |
|
| 300 |
def finished(self, callinfocollection): |
| 301 |
|
| 302 |
|
| 303 |
for func in callinfocollection.all_function_addresses_as_int(): |
| 304 |
func = int2adr(func) |
| 305 |
self.see_raw_object(func.ptr) |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
USE_C_FORM = set([ |
| 313 |
'copystrcontent', |
| 314 |
'getarrayitem_gc_pure_i', |
| 315 |
'getarrayitem_gc_pure_r', |
| 316 |
'getarrayitem_gc_i', |
| 317 |
'getarrayitem_gc_r', |
| 318 |
'goto_if_not_int_eq', |
| 319 |
'goto_if_not_int_ge', |
| 320 |
'goto_if_not_int_gt', |
| 321 |
'goto_if_not_int_le', |
| 322 |
'goto_if_not_int_lt', |
| 323 |
'goto_if_not_int_ne', |
| 324 |
'int_add', |
| 325 |
'int_and', |
| 326 |
'int_copy', |
| 327 |
'int_eq', |
| 328 |
'int_ge', |
| 329 |
'int_gt', |
| 330 |
'int_le', |
| 331 |
'int_lt', |
| 332 |
'int_ne', |
| 333 |
'int_return', |
| 334 |
'int_sub', |
| 335 |
'jit_merge_point', |
| 336 |
'new_array', |
| 337 |
'new_array_clear', |
| 338 |
'newstr', |
| 339 |
'setarrayitem_gc_i', |
| 340 |
'setarrayitem_gc_r', |
| 341 |
'setfield_gc_i', |
| 342 |
'strgetitem', |
| 343 |
'strsetitem', |
| 344 |
|
| 345 |
'foobar', 'baz', |
| 346 |
]) |