| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
from rpython.jit.backend.arm import conditions as cond |
| 4 |
from rpython.jit.backend.arm import instructions |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
def define_load_store_func(name, table): |
| 9 |
n = (0x1 << 26 |
| 10 |
| (table['A'] & 0x1) << 25 |
| 11 |
| (table['op1'] & 0x1F) << 20) |
| 12 |
if 'B' in table: |
| 13 |
b_zero = True |
| 14 |
else: |
| 15 |
b_zero = False |
| 16 |
op1cond = table['op1not'] |
| 17 |
rncond = ('rn' in table and table['rn'] == '!0xF') |
| 18 |
if table['imm']: |
| 19 |
assert not b_zero |
| 20 |
|
| 21 |
def f(self, rt, rn, imm=0, cond=cond.AL): |
| 22 |
assert not (rncond and rn == 0xF) |
| 23 |
p = 1 |
| 24 |
w = 0 |
| 25 |
u, imm = self._encode_imm(imm) |
| 26 |
instr = (n |
| 27 |
| cond << 28 |
| 28 |
| (p & 0x1) << 24 |
| 29 |
| (u & 0x1) << 23 |
| 30 |
| (w & 0x1) << 21 |
| 31 |
| imm_operation(rt, rn, imm)) |
| 32 |
assert instr & 0x1F00000 != op1cond |
| 33 |
self.write32(instr) |
| 34 |
else: |
| 35 |
def f(self, rt, rn, rm, imm=0, cond=cond.AL, s=0, shifttype=0): |
| 36 |
assert not (rncond and rn == 0xF) |
| 37 |
p = 1 |
| 38 |
w = 0 |
| 39 |
u, imm = self._encode_imm(imm) |
| 40 |
instr = (n |
| 41 |
| cond << 28 |
| 42 |
| (p & 0x1) << 24 |
| 43 |
| (u & 0x1) << 23 |
| 44 |
| (w & 0x1) << 21 |
| 45 |
| reg_operation(rt, rn, rm, imm, s, shifttype)) |
| 46 |
if b_zero: |
| 47 |
assert instr & 0x10 == 0, 'bit 4 should be zero' |
| 48 |
assert instr & 0x1F00000 != op1cond |
| 49 |
self.write32(instr) |
| 50 |
return f |
| 51 |
|
| 52 |
|
| 53 |
def define_extra_load_store_func(name, table): |
| 54 |
def check_registers(r1, r2): |
| 55 |
assert r1 % 2 == 0 |
| 56 |
assert r1 + 1 == r2 |
| 57 |
assert r1 != 14 |
| 58 |
|
| 59 |
n = ((table['op1'] & 0x1F) << 20 |
| 60 |
| 0x1 << 7 |
| 61 |
| (table['op2'] & 0x3) << 5 |
| 62 |
| 0x1 << 4) |
| 63 |
p = 1 |
| 64 |
w = 0 |
| 65 |
rncond = ('rn' in table and table['rn'] == '!0xF') |
| 66 |
dual = (name[-4] == 'D') |
| 67 |
|
| 68 |
if dual: |
| 69 |
if name[-2:] == 'rr': |
| 70 |
def f(self, rt, rt2, rn, rm, cond=cond.AL): |
| 71 |
check_registers(rt, rt2) |
| 72 |
assert not (rncond and rn == 0xF) |
| 73 |
self.write32(n |
| 74 |
| cond << 28 |
| 75 |
| (p & 0x1) << 24 |
| 76 |
| (1 & 0x1) << 23 |
| 77 |
| (w & 0x1) << 21 |
| 78 |
| (rn & 0xF) << 16 |
| 79 |
| (rt & 0xF) << 12 |
| 80 |
| (rm & 0xF)) |
| 81 |
else: |
| 82 |
def f(self, rt, rt2, rn, imm=0, cond=cond.AL): |
| 83 |
check_registers(rt, rt2) |
| 84 |
assert not (rncond and rn == 0xF) |
| 85 |
u, imm = self._encode_imm(imm) |
| 86 |
self.write32(n |
| 87 |
| cond << 28 |
| 88 |
| (p & 0x1) << 24 |
| 89 |
| (u & 0x1) << 23 |
| 90 |
| (w & 0x1) << 21 |
| 91 |
| (rn & 0xF) << 16 |
| 92 |
| (rt & 0xF) << 12 |
| 93 |
| ((imm >> 0x4) & 0xF) << 8 |
| 94 |
| (imm & 0xF)) |
| 95 |
|
| 96 |
else: |
| 97 |
if name[-2:] == 'rr': |
| 98 |
def f(self, rt, rn, rm, cond=cond.AL): |
| 99 |
assert not (rncond and rn == 0xF) |
| 100 |
self.write32(n |
| 101 |
| cond << 28 |
| 102 |
| (p & 0x1) << 24 |
| 103 |
| (1 & 0x1) << 23 |
| 104 |
| (w & 0x1) << 21 |
| 105 |
| (rn & 0xF) << 16 |
| 106 |
| (rt & 0xF) << 12 |
| 107 |
| (rm & 0xF)) |
| 108 |
else: |
| 109 |
def f(self, rt, rn, imm=0, cond=cond.AL): |
| 110 |
assert not (rncond and rn == 0xF) |
| 111 |
u, imm = self._encode_imm(imm) |
| 112 |
self.write32(n |
| 113 |
| cond << 28 |
| 114 |
| (p & 0x1) << 24 |
| 115 |
| (u & 0x1) << 23 |
| 116 |
| (w & 0x1) << 21 |
| 117 |
| (rn & 0xF) << 16 |
| 118 |
| (rt & 0xF) << 12 |
| 119 |
| ((imm >> 0x4) & 0xF) << 8 |
| 120 |
| (imm & 0xF)) |
| 121 |
return f |
| 122 |
|
| 123 |
|
| 124 |
def define_data_proc_imm_func(name, table): |
| 125 |
n = (0x1 << 25 |
| 126 |
| (table['op'] & 0x1F) << 20) |
| 127 |
if table['result'] and table['base']: |
| 128 |
def imm_func(self, rd, rn, imm=0, cond=cond.AL, s=0): |
| 129 |
if imm < 0: |
| 130 |
raise ValueError |
| 131 |
self.write32(n |
| 132 |
| cond << 28 |
| 133 |
| s << 20 |
| 134 |
| imm_operation(rd, rn, imm)) |
| 135 |
elif not table['base']: |
| 136 |
def imm_func(self, rd, imm=0, cond=cond.AL, s=0): |
| 137 |
self.write32(n |
| 138 |
| cond << 28 |
| 139 |
| s << 20 |
| 140 |
| imm_operation(rd, 0, imm)) |
| 141 |
else: |
| 142 |
def imm_func(self, rn, imm=0, cond=cond.AL, s=0): |
| 143 |
self.write32(n |
| 144 |
| cond << 28 |
| 145 |
| s << 20 |
| 146 |
| imm_operation(0, rn, imm)) |
| 147 |
return imm_func |
| 148 |
|
| 149 |
|
| 150 |
def define_data_proc_func(name, table): |
| 151 |
n = ((table['op1'] & 0x1F) << 20 |
| 152 |
| (table['op2'] & 0x1F) << 7 |
| 153 |
| (table['op3'] & 0x3) << 5) |
| 154 |
if name[-2:] == 'ri': |
| 155 |
def f(self, rd, rm, imm=0, cond=cond.AL, s=0): |
| 156 |
if table['op2cond'] == '!0': |
| 157 |
assert imm != 0 |
| 158 |
elif table['op2cond'] == '0': |
| 159 |
assert imm == 0 |
| 160 |
self.write32(n |
| 161 |
| cond << 28 |
| 162 |
| (s & 0x1) << 20 |
| 163 |
| (rd & 0xFF) << 12 |
| 164 |
| (imm & 0x1F) << 7 |
| 165 |
| (rm & 0xFF)) |
| 166 |
|
| 167 |
elif not table['result']: |
| 168 |
|
| 169 |
def f(self, rn, rm, imm=0, cond=cond.AL, s=0, shifttype=0): |
| 170 |
self.write32(n |
| 171 |
| cond << 28 |
| 172 |
| reg_operation(0, rn, rm, imm, s, shifttype)) |
| 173 |
elif not table['base']: |
| 174 |
|
| 175 |
def f(self, rd, rm, imm=0, cond=cond.AL, s=0, shifttype=0): |
| 176 |
self.write32(n |
| 177 |
| cond << 28 |
| 178 |
| reg_operation(rd, 0, rm, imm, s, shifttype)) |
| 179 |
else: |
| 180 |
def f(self, rd, rn, rm, imm=0, cond=cond.AL, s=0, shifttype=0): |
| 181 |
self.write32(n |
| 182 |
| cond << 28 |
| 183 |
| reg_operation(rd, rn, rm, imm, s, shifttype)) |
| 184 |
return f |
| 185 |
|
| 186 |
|
| 187 |
def define_data_proc_reg_shift_reg_func(name, table): |
| 188 |
n = ((0x1 << 4) | (table['op1'] & 0x1F) << 20 | (table['op2'] & 0x3) << 5) |
| 189 |
if 'result' in table and not table['result']: |
| 190 |
result = False |
| 191 |
else: |
| 192 |
result = True |
| 193 |
if name[-2:] == 'sr': |
| 194 |
if result: |
| 195 |
def f(self, rd, rn, rm, rs, cond=cond.AL, s=0, shifttype=0): |
| 196 |
self.write32(n |
| 197 |
| cond << 28 |
| 198 |
| (s & 0x1) << 20 |
| 199 |
| (rn & 0xF) << 16 |
| 200 |
| (rd & 0xF) << 12 |
| 201 |
| (rs & 0xF) << 8 |
| 202 |
| (shifttype & 0x3) << 5 |
| 203 |
| (rm & 0xF)) |
| 204 |
else: |
| 205 |
def f(self, rn, rm, rs, cond=cond.AL, s=0, shifttype=0): |
| 206 |
self.write32(n |
| 207 |
| cond << 28 |
| 208 |
| (s & 0x1) << 20 |
| 209 |
| (rn & 0xF) << 16 |
| 210 |
| (rs & 0xF) << 8 |
| 211 |
| (shifttype & 0x3) << 5 |
| 212 |
| (rm & 0xF)) |
| 213 |
else: |
| 214 |
def f(self, rd, rn, rm, cond=cond.AL, s=0): |
| 215 |
self.write32(n |
| 216 |
| cond << 28 |
| 217 |
| (s & 0x1) << 20 |
| 218 |
| (rd & 0xF) << 12 |
| 219 |
| (rm & 0xF) << 8 |
| 220 |
| (rn & 0xF)) |
| 221 |
return f |
| 222 |
|
| 223 |
|
| 224 |
def define_supervisor_and_coproc_func(name, table): |
| 225 |
n = (0x3 << 26 | (table['op1'] & 0x3F) << 20 | (table['op'] & 0x1) << 4) |
| 226 |
|
| 227 |
def f(self, coproc, opc1, rt, crn, crm, opc2=0, cond=cond.AL): |
| 228 |
assert coproc & 0xE != 0xA |
| 229 |
self.write32(n |
| 230 |
| cond << 28 |
| 231 |
| (opc1 & 0x7) << 21 |
| 232 |
| (crn & 0xF) << 16 |
| 233 |
| (rt & 0xF) << 12 |
| 234 |
| (coproc & 0xF) << 8 |
| 235 |
| (opc2 & 0x7) << 5 |
| 236 |
| (crm & 0xF)) |
| 237 |
return f |
| 238 |
|
| 239 |
|
| 240 |
def define_multiply_func(name, table): |
| 241 |
n = (table['op'] & 0xF) << 20 | 0x9 << 4 |
| 242 |
if 'acc' in table and table['acc']: |
| 243 |
if 'update_flags' in table and table['update_flags']: |
| 244 |
def f(self, rd, rn, rm, ra, cond=cond.AL, s=0): |
| 245 |
self.write32(n |
| 246 |
| cond << 28 |
| 247 |
| (s & 0x1) << 20 |
| 248 |
| (rd & 0xF) << 16 |
| 249 |
| (ra & 0xF) << 12 |
| 250 |
| (rm & 0xF) << 8 |
| 251 |
| (rn & 0xF)) |
| 252 |
else: |
| 253 |
def f(self, rd, rn, rm, ra, cond=cond.AL): |
| 254 |
self.write32(n |
| 255 |
| cond << 28 |
| 256 |
| (rd & 0xF) << 16 |
| 257 |
| (ra & 0xF) << 12 |
| 258 |
| (rm & 0xF) << 8 |
| 259 |
| (rn & 0xF)) |
| 260 |
|
| 261 |
elif 'long' in table and table['long']: |
| 262 |
def f(self, rdlo, rdhi, rn, rm, cond=cond.AL): |
| 263 |
assert rdhi != rdlo |
| 264 |
self.write32(n |
| 265 |
| cond << 28 |
| 266 |
| (rdhi & 0xF) << 16 |
| 267 |
| (rdlo & 0xF) << 12 |
| 268 |
| (rm & 0xF) << 8 |
| 269 |
| (rn & 0xF)) |
| 270 |
else: |
| 271 |
def f(self, rd, rn, rm, cond=cond.AL, s=0): |
| 272 |
self.write32(n |
| 273 |
| cond << 28 |
| 274 |
| (s & 0x1) << 20 |
| 275 |
| (rd & 0xF) << 16 |
| 276 |
| (rm & 0xF) << 8 |
| 277 |
| (rn & 0xF)) |
| 278 |
|
| 279 |
return f |
| 280 |
|
| 281 |
|
| 282 |
def define_block_data_func(name, table): |
| 283 |
n = (table['op'] & 0x3F) << 20 |
| 284 |
|
| 285 |
def f(self, rn, regs, w=0, cond=cond.AL): |
| 286 |
|
| 287 |
instr = (n |
| 288 |
| cond << 28 |
| 289 |
| 0x1 << 27 |
| 290 |
| (w & 0x1) << 21 |
| 291 |
| (rn & 0xF) << 16) |
| 292 |
instr = self._encode_reg_list(instr, regs) |
| 293 |
self.write32(instr) |
| 294 |
|
| 295 |
return f |
| 296 |
|
| 297 |
|
| 298 |
def define_float_load_store_func(name, table): |
| 299 |
n = (0x3 << 26 |
| 300 |
| (table['opcode'] & 0x1F) << 20 |
| 301 |
| 0x5 << 0x9 |
| 302 |
| 0x1 << 0x8) |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
def f(self, dd, rn, imm=0, cond=cond.AL): |
| 307 |
assert imm % 4 == 0 |
| 308 |
imm = imm / 4 |
| 309 |
u, imm = self._encode_imm(imm) |
| 310 |
instr = (n |
| 311 |
| (cond & 0xF) << 28 |
| 312 |
| (u & 0x1) << 23 |
| 313 |
| (rn & 0xF) << 16 |
| 314 |
| (dd & 0xF) << 12 |
| 315 |
| (imm & 0xFF)) |
| 316 |
self.write32(instr) |
| 317 |
return f |
| 318 |
|
| 319 |
|
| 320 |
def define_float64_data_proc_instructions_func(name, table): |
| 321 |
n = (0xE << 24 |
| 322 |
| 0x5 << 9 |
| 323 |
| 0x1 << 8 |
| 324 |
| (table['opc3'] & 0x3) << 6) |
| 325 |
|
| 326 |
if 'opc1' in table: |
| 327 |
n |= (table['opc1'] & 0xF) << 20 |
| 328 |
if 'opc2' in table: |
| 329 |
n |= (table['opc2'] & 0xF) << 16 |
| 330 |
|
| 331 |
if 'result' in table and not table['result']: |
| 332 |
def f(self, dd, dm, cond=cond.AL): |
| 333 |
instr = (n |
| 334 |
| (cond & 0xF) << 28 |
| 335 |
| 0x4 << 16 |
| 336 |
| (dd & 0xF) << 12 |
| 337 |
| (dm & 0xF)) |
| 338 |
self.write32(instr) |
| 339 |
elif 'base' in table and not table['base']: |
| 340 |
def f(self, dd, dm, cond=cond.AL): |
| 341 |
instr = (n |
| 342 |
| (cond & 0xF) << 28 |
| 343 |
| (dd & 0xF) << 12 |
| 344 |
| (dm & 0xF)) |
| 345 |
self.write32(instr) |
| 346 |
else: |
| 347 |
def f(self, dd, dn, dm, cond=cond.AL): |
| 348 |
instr = (n |
| 349 |
| (cond & 0xF) << 28 |
| 350 |
| (dn & 0xF) << 16 |
| 351 |
| (dd & 0xF) << 12 |
| 352 |
| (dm & 0xF)) |
| 353 |
self.write32(instr) |
| 354 |
return f |
| 355 |
|
| 356 |
def define_simd_instructions_3regs_func(name, table): |
| 357 |
n = 0 |
| 358 |
if 'A' in table: |
| 359 |
n |= (table['A'] & 0xF) << 8 |
| 360 |
if 'B' in table: |
| 361 |
n |= (table['B'] & 0x1) << 4 |
| 362 |
if 'U' in table: |
| 363 |
n |= (table['U'] & 0x1) << 24 |
| 364 |
if 'C' in table: |
| 365 |
n |= (table['C'] & 0x3) << 20 |
| 366 |
if name == 'VADD_i64' or name == 'VSUB_i64': |
| 367 |
size = 0x3 << 20 |
| 368 |
n |= size |
| 369 |
def f(self, dd, dn, dm): |
| 370 |
base = 0x79 |
| 371 |
N = (dn >> 4) & 0x1 |
| 372 |
M = (dm >> 4) & 0x1 |
| 373 |
D = (dd >> 4) & 0x1 |
| 374 |
Q = 0 |
| 375 |
instr = (n |
| 376 |
| base << 25 |
| 377 |
| D << 22 |
| 378 |
| (dn & 0xf) << 16 |
| 379 |
| (dd & 0xf) << 12 |
| 380 |
| N << 7 |
| 381 |
| Q << 6 |
| 382 |
| M << 5 |
| 383 |
| (dm & 0xf)) |
| 384 |
self.write32(instr) |
| 385 |
return f |
| 386 |
|
| 387 |
|
| 388 |
def imm_operation(rt, rn, imm): |
| 389 |
return ((rn & 0xFF) << 16 |
| 390 |
| (rt & 0xFF) << 12 |
| 391 |
| (imm & 0xFFF)) |
| 392 |
|
| 393 |
|
| 394 |
def reg_operation(rt, rn, rm, imm, s, shifttype): |
| 395 |
return ((s & 0x1) << 20 |
| 396 |
| (rn & 0xF) << 16 |
| 397 |
| (rt & 0xF) << 12 |
| 398 |
| (imm & 0x1F) << 7 |
| 399 |
| (shifttype & 0x3) << 5 |
| 400 |
| (rm & 0xF)) |
| 401 |
|
| 402 |
|
| 403 |
def define_instruction(builder, key, val, target): |
| 404 |
f = builder(key, val) |
| 405 |
f.__name__ = key |
| 406 |
setattr(target, key, f) |
| 407 |
|
| 408 |
|
| 409 |
def define_instructions(target): |
| 410 |
inss = [k for k in instructions.__dict__.keys() if not k.startswith('__')] |
| 411 |
for name in inss: |
| 412 |
if name == 'branch': |
| 413 |
continue |
| 414 |
try: |
| 415 |
func = globals()['define_%s_func' % name] |
| 416 |
except KeyError: |
| 417 |
print('No instr generator for %s instructions' % name) |
| 418 |
continue |
| 419 |
for key, value in getattr(instructions, name).iteritems(): |
| 420 |
define_instruction(func, key, value, target) |