| 1 |
|
| 2 |
#ifdef HAVE_CONFIG_H |
| 3 |
#include "config.h" |
| 4 |
#endif |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
#include <sys/types.h> |
| 11 |
|
| 12 |
#include <orc/orcdebug.h> |
| 13 |
#include <orc/orcprogram.h> |
| 14 |
#include <orc/orcutils.h> |
| 15 |
#include <orc/orcinternal.h> |
| 16 |
#include <orc/orcarm.h> |
| 17 |
|
| 18 |
#if defined(HAVE_ARM) || defined(HAVE_AARCH64) |
| 19 |
#if defined(__APPLE__) |
| 20 |
#include <libkern/OSCacheControl.h> |
| 21 |
#endif |
| 22 |
#if defined (_WIN32) |
| 23 |
#define WIN32_LEAN_AND_MEAN |
| 24 |
#include <windows.h> |
| 25 |
#endif |
| 26 |
#endif |
| 27 |
|
| 28 |
/** |
| 29 |
* SECTION:orcarm |
| 30 |
* @title: ARM |
| 31 |
* @short_description: code generation for ARM |
| 32 |
*/ |
| 33 |
|
| 34 |
const char * |
| 35 |
orc_arm_cond_name (OrcArmCond cond) |
| 36 |
{ |
| 37 |
static const char *cond_names[] = { |
| 38 |
"eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", |
| 39 |
"hi", "ls", "ge", "lt", "gt", "le", "", "" |
| 40 |
}; |
| 41 |
if ((int)cond < 0 || (int)cond >= 16) { |
| 42 |
return "ERROR"; |
| 43 |
} |
| 44 |
return cond_names[cond&0xf]; |
| 45 |
} |
| 46 |
|
| 47 |
const char * |
| 48 |
orc_arm_reg_name (int reg) |
| 49 |
{ |
| 50 |
#if 0 |
| 51 |
static const char *gp_regs[] = { |
| 52 |
"a1", "a2", "a3", "a4", |
| 53 |
"v1", "v2", "v3", "v4", |
| 54 |
"v5", "v6", "v7", "v8", |
| 55 |
"ip", "sp", "lr", "pc" }; |
| 56 |
#else |
| 57 |
static const char *gp_regs[] = { |
| 58 |
"r0", "r1", "r2", "r3", |
| 59 |
"r4", "r5", "r6", "r7", |
| 60 |
"r8", "r9", "r10", "r11", |
| 61 |
"ip", "sp", "lr", "pc" }; |
| 62 |
/* "r12", "r13", "r14", "r15" }; */ |
| 63 |
#endif |
| 64 |
|
| 65 |
if (reg < ORC_GP_REG_BASE || reg >= ORC_GP_REG_BASE+16) { |
| 66 |
return "ERROR"; |
| 67 |
} |
| 68 |
|
| 69 |
return gp_regs[reg&0xf]; |
| 70 |
} |
| 71 |
|
| 72 |
const char * |
| 73 |
orc_arm64_reg_name (int reg, OrcArm64RegBits bits) |
| 74 |
{ |
| 75 |
static const char *gp_regs64[] = { |
| 76 |
"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", |
| 77 |
"x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", |
| 78 |
"x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x29", |
| 79 |
"x30", "sp" |
| 80 |
}; |
| 81 |
static const char *gp_regs32[] = { |
| 82 |
"w0", "w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", |
| 83 |
"w10", "w11", "w12", "w13", "w14", "w15", "w16", "w17", "w18", "w19", |
| 84 |
"w20", "w21", "w22", "w23", "w24", "w25", "w26", "w27", "w28", "w29", |
| 85 |
"w30", "sp" |
| 86 |
}; |
| 87 |
|
| 88 |
if (reg < ORC_GP_REG_BASE || reg >= ORC_GP_REG_BASE+32) { |
| 89 |
return "ERROR"; |
| 90 |
} |
| 91 |
|
| 92 |
return bits == ORC_ARM64_REG_64 ? gp_regs64[reg&0x1f] : gp_regs32[reg&0x1f]; |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
orc_arm_emit (OrcCompiler *compiler, orc_uint32 insn) |
| 97 |
{ |
| 98 |
ORC_WRITE_UINT32_LE (compiler->codeptr, insn); |
| 99 |
compiler->codeptr+=4; |
| 100 |
} |
| 101 |
|
| 102 |
void |
| 103 |
orc_arm_emit_bx_lr (OrcCompiler *compiler) |
| 104 |
{ |
| 105 |
if (compiler->is_64bit) { |
| 106 |
orc_arm64_emit_ret (compiler, ORC_ARM64_LR); |
| 107 |
} else { |
| 108 |
ORC_ASM_CODE(compiler," bx lr\n"); |
| 109 |
orc_arm_emit (compiler, 0xe12fff1e); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static int |
| 114 |
count_reg_ones (int regs) |
| 115 |
{ |
| 116 |
int count = 0; |
| 117 |
|
| 118 |
while (regs) { |
| 119 |
count += regs & 1; |
| 120 |
regs >>= 1; |
| 121 |
} |
| 122 |
|
| 123 |
return count; |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
orc_arm_emit_push (OrcCompiler *compiler, int regs, orc_uint32 vregs) |
| 128 |
{ |
| 129 |
int i; |
| 130 |
|
| 131 |
if (regs) { |
| 132 |
int x = 0; |
| 133 |
|
| 134 |
if (compiler->is_64bit) { |
| 135 |
int count, stores; |
| 136 |
int stack_increased = 0; |
| 137 |
/** a number of 1s in regs */ |
| 138 |
count = count_reg_ones (regs); |
| 139 |
/** AArch64 requires a 16-byte aligned stack pointer */ |
| 140 |
stores = (count-1)/2+1; |
| 141 |
x = -1; |
| 142 |
for(i=0;i<32;i++){ |
| 143 |
if (stores == 0) break; |
| 144 |
if (regs & (1<<i)) { |
| 145 |
if (stack_increased == 0) { |
| 146 |
/** increase stack area & store registers */ |
| 147 |
if (count % 2 == 1) { |
| 148 |
orc_arm64_emit_store_pre (compiler, 64, ORC_GP_REG_BASE+i, |
| 149 |
ORC_ARM64_SP, (stores--) * -16); |
| 150 |
stack_increased = 1; |
| 151 |
continue; |
| 152 |
} else if (x != -1) { |
| 153 |
orc_arm64_emit_store_pair_pre (compiler, 64, ORC_GP_REG_BASE+x, |
| 154 |
ORC_GP_REG_BASE+i, ORC_ARM64_SP, (stores--) * -16); |
| 155 |
stack_increased = 1; |
| 156 |
x = -1; |
| 157 |
continue; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (x != -1) { |
| 162 |
/** store registers */ |
| 163 |
orc_arm64_emit_store_pair_reg (compiler, 64, ORC_GP_REG_BASE+x, |
| 164 |
ORC_GP_REG_BASE+i, ORC_ARM64_SP, (stores--) * 16); |
| 165 |
x = -1; |
| 166 |
} else { |
| 167 |
x = i; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} else { |
| 172 |
ORC_ASM_CODE(compiler," push {"); |
| 173 |
for(i=0;i<16;i++){ |
| 174 |
if (regs & (1<<i)) { |
| 175 |
x |= (1<<i); |
| 176 |
ORC_ASM_CODE(compiler,"r%d", i); |
| 177 |
if (x != regs) { |
| 178 |
ORC_ASM_CODE(compiler,", "); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
ORC_ASM_CODE(compiler,"}\n"); |
| 183 |
|
| 184 |
orc_arm_emit (compiler, 0xe92d0000 | regs); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (vregs) { |
| 189 |
int first = -1, last = -1, nregs; |
| 190 |
|
| 191 |
ORC_ASM_CODE(compiler, " vpush {"); |
| 192 |
for(i=0; i<32; ++i) { |
| 193 |
if (vregs & (1U << i)) { |
| 194 |
if (first==-1) { |
| 195 |
ORC_ASM_CODE(compiler, "d%d", i); |
| 196 |
first = i; |
| 197 |
} |
| 198 |
last = i; |
| 199 |
} |
| 200 |
} |
| 201 |
/* What's the deal with even/odd registers ? */ |
| 202 |
ORC_ASM_CODE(compiler, "-d%d}\n", last+1); |
| 203 |
|
| 204 |
nregs = last + 1 - first + 1; |
| 205 |
orc_arm_emit (compiler, 0xed2d0b00 | (((first & 0x10) >> 4) << 22) | ((first & 0x0f) << 12) | (nregs << 1)); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void |
| 210 |
orc_arm_emit_pop (OrcCompiler *compiler, int regs, orc_uint32 vregs) |
| 211 |
{ |
| 212 |
int i; |
| 213 |
|
| 214 |
|
| 215 |
if (vregs) { |
| 216 |
int first = -1, last = -1, nregs; |
| 217 |
|
| 218 |
ORC_ASM_CODE(compiler, " vpop {"); |
| 219 |
for(i=0; i<32; ++i) { |
| 220 |
if (vregs & (1U << i)) { |
| 221 |
if (first==-1) { |
| 222 |
ORC_ASM_CODE(compiler, "d%d", i); |
| 223 |
first = i; |
| 224 |
} |
| 225 |
last = i; |
| 226 |
} |
| 227 |
} |
| 228 |
ORC_ASM_CODE(compiler, "-d%d}\n", last+1); |
| 229 |
|
| 230 |
nregs = last + 1 - first + 1; |
| 231 |
orc_arm_emit (compiler, 0xecbd0b00 | (((first & 0x10) >> 4) << 22) | ((first & 0x0f) << 12) | (nregs << 1)); |
| 232 |
} |
| 233 |
|
| 234 |
if (regs) { |
| 235 |
int x = 0; |
| 236 |
|
| 237 |
if (compiler->is_64bit) { |
| 238 |
int count, loads, tmp_loads; |
| 239 |
/** a number of 1s in regs */ |
| 240 |
count = count_reg_ones (regs); |
| 241 |
/** AArch64 requires a 16-byte aligned stack pointer */ |
| 242 |
loads = (count-1)/2+1; |
| 243 |
tmp_loads = loads; |
| 244 |
x = -1; |
| 245 |
for(i=31;i>=0;i--){ |
| 246 |
if (regs & (1<<i)) { |
| 247 |
if (x != -1) { |
| 248 |
if (tmp_loads == 1) break; |
| 249 |
/** load registers */ |
| 250 |
orc_arm64_emit_load_pair_reg (compiler, 64, ORC_GP_REG_BASE+i, |
| 251 |
ORC_GP_REG_BASE+x, ORC_ARM64_SP, (loads - (--tmp_loads)) * 16); |
| 252 |
x = -1; |
| 253 |
} else { |
| 254 |
x = i; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
/** decrease stack area & load registers */ |
| 259 |
if (count % 2 == 1) { |
| 260 |
orc_arm64_emit_load_post (compiler, 64, ORC_GP_REG_BASE+x, |
| 261 |
ORC_ARM64_SP, loads * 16); |
| 262 |
} else { |
| 263 |
orc_arm64_emit_load_pair_post (compiler, 64, ORC_GP_REG_BASE+i, |
| 264 |
ORC_GP_REG_BASE+x, ORC_ARM64_SP, loads * 16); |
| 265 |
} |
| 266 |
} else { |
| 267 |
ORC_ASM_CODE(compiler," pop {"); |
| 268 |
for(i=0;i<16;i++){ |
| 269 |
if (regs & (1<<i)) { |
| 270 |
x |= (1<<i); |
| 271 |
ORC_ASM_CODE(compiler,"r%d", i); |
| 272 |
if (x != regs) { |
| 273 |
ORC_ASM_CODE(compiler,", "); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
ORC_ASM_CODE(compiler,"}\n"); |
| 278 |
|
| 279 |
orc_arm_emit (compiler, 0xe8bd0000 | regs); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void |
| 285 |
orc_arm_emit_label (OrcCompiler *compiler, int label) |
| 286 |
{ |
| 287 |
ORC_ASSERT (label < ORC_N_LABELS); |
| 288 |
|
| 289 |
ORC_ASM_CODE(compiler,".L%d:\n", label); |
| 290 |
|
| 291 |
compiler->labels[label] = compiler->codeptr; |
| 292 |
} |
| 293 |
|
| 294 |
void |
| 295 |
orc_arm_add_fixup (OrcCompiler *compiler, int label, int type) |
| 296 |
{ |
| 297 |
ORC_ASSERT (compiler->n_fixups < ORC_N_FIXUPS); |
| 298 |
|
| 299 |
compiler->fixups[compiler->n_fixups].ptr = compiler->codeptr; |
| 300 |
compiler->fixups[compiler->n_fixups].label = label; |
| 301 |
compiler->fixups[compiler->n_fixups].type = type; |
| 302 |
compiler->n_fixups++; |
| 303 |
} |
| 304 |
|
| 305 |
void |
| 306 |
orc_arm_do_fixups (OrcCompiler *compiler) |
| 307 |
{ |
| 308 |
int i; |
| 309 |
for(i=0;i<compiler->n_fixups;i++){ |
| 310 |
unsigned char *label = compiler->labels[compiler->fixups[i].label]; |
| 311 |
unsigned char *ptr = compiler->fixups[i].ptr; |
| 312 |
orc_uint32 code; |
| 313 |
int diff; |
| 314 |
|
| 315 |
if (compiler->fixups[i].type == 0) { |
| 316 |
code = ORC_READ_UINT32_LE (ptr); |
| 317 |
diff = code; |
| 318 |
if (compiler->is_64bit) { |
| 319 |
diff = ((label - ptr) >> 2); /** time 4 */ |
| 320 |
if (diff != (diff << 6)>>6) { |
| 321 |
ORC_COMPILER_ERROR(compiler, "fixup out of range"); |
| 322 |
} |
| 323 |
/** check whether it's conditional or unconditioanl */ |
| 324 |
if ((code >> 30) & 1) { |
| 325 |
diff <<= 5; /** for cond */ |
| 326 |
ORC_WRITE_UINT32_LE(ptr, (code&0xff00001f) | (diff&0x00ffffe0)); |
| 327 |
} else { |
| 328 |
ORC_WRITE_UINT32_LE(ptr, (code&0xfc000000) | (diff&0x03ffffff)); |
| 329 |
} |
| 330 |
} else { |
| 331 |
diff = (diff << 8) >> 8; |
| 332 |
diff += ((label - ptr) >> 2); |
| 333 |
if (diff != (diff << 8)>>8) { |
| 334 |
ORC_COMPILER_ERROR(compiler, "fixup out of range"); |
| 335 |
} |
| 336 |
ORC_WRITE_UINT32_LE(ptr, (code&0xff000000) | (diff&0x00ffffff)); |
| 337 |
} |
| 338 |
} else { |
| 339 |
code = ORC_READ_UINT32_LE (ptr); |
| 340 |
diff = code; |
| 341 |
/* We store the offset in the code as signed, but the CPU considers |
| 342 |
* it unsigned */ |
| 343 |
diff = (diff << 24) >> 24; |
| 344 |
diff += ((label - ptr) >> 2); |
| 345 |
if (diff != (diff & 0xff)) { |
| 346 |
ORC_COMPILER_ERROR(compiler, "fixup out of range (%d > 255)", diff); |
| 347 |
} |
| 348 |
ORC_WRITE_UINT32_LE(ptr, (code&0xffffff00) | (diff&0x000000ff)); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
void |
| 354 |
orc_arm_emit_align (OrcCompiler *compiler, int align_shift) |
| 355 |
{ |
| 356 |
int diff; |
| 357 |
|
| 358 |
diff = (compiler->code - compiler->codeptr)&((1<<align_shift) - 1); |
| 359 |
while (diff) { |
| 360 |
orc_arm_emit_nop (compiler); |
| 361 |
diff-=4; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
void |
| 366 |
orc_arm_emit_nop (OrcCompiler *compiler) |
| 367 |
{ |
| 368 |
ORC_ASM_CODE(compiler," nop\n"); |
| 369 |
if (compiler->is_64bit) |
| 370 |
orc_arm_emit (compiler, 0xd503201f); |
| 371 |
else |
| 372 |
orc_arm_emit (compiler, 0xe1a00000); |
| 373 |
} |
| 374 |
|
| 375 |
void |
| 376 |
orc_arm_emit_branch (OrcCompiler *compiler, int cond, int label) |
| 377 |
{ |
| 378 |
orc_uint32 code; |
| 379 |
|
| 380 |
if (compiler->is_64bit) { |
| 381 |
/** B.cond |
| 382 |
* 3 2 1 |
| 383 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 384 |
* +---------------------------------------------------------------+ |
| 385 |
* |0 1 0 1 0 1 0|0| imm19 |0| cond | |
| 386 |
* +---------------------------------------------------------------+ |
| 387 |
* |
| 388 |
* B |
| 389 |
* 3 2 1 |
| 390 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 391 |
* +---------------------------------------------------------------+ |
| 392 |
* |0 0 0 1 0 1| imm26 | |
| 393 |
* +---------------------------------------------------------------+ |
| 394 |
* |
| 395 |
* Note that we don't know exact imm19/imm26 yet, so need fixup codes. |
| 396 |
*/ |
| 397 |
if (cond < ORC_ARM_COND_AL) { |
| 398 |
code = 0x54000000; |
| 399 |
code |= cond&0xf; |
| 400 |
|
| 401 |
ORC_ASM_CODE(compiler," b.%s .L%d\n", orc_arm_cond_name(cond), label); |
| 402 |
} else { |
| 403 |
code = 0x14000000; |
| 404 |
|
| 405 |
ORC_ASM_CODE(compiler," b .L%d\n", label); |
| 406 |
} |
| 407 |
} else { |
| 408 |
code = 0x0afffffe; |
| 409 |
code |= (cond&0xf) << 28; |
| 410 |
|
| 411 |
ORC_ASM_CODE(compiler," b%s .L%d\n", orc_arm_cond_name(cond), label); |
| 412 |
} |
| 413 |
orc_arm_add_fixup (compiler, label, 0); |
| 414 |
orc_arm_emit (compiler, code); |
| 415 |
} |
| 416 |
|
| 417 |
void |
| 418 |
orc_arm_emit_load_imm (OrcCompiler *compiler, int dest, int imm) |
| 419 |
{ |
| 420 |
orc_uint32 code; |
| 421 |
int shift2; |
| 422 |
unsigned int x; |
| 423 |
|
| 424 |
if ((imm & 0xff) == imm) { |
| 425 |
shift2 = 0; |
| 426 |
x = imm; |
| 427 |
} else { |
| 428 |
shift2 = 0; |
| 429 |
x = imm & 0xffffffff; |
| 430 |
while ((x & 3) == 0) { |
| 431 |
x >>= 2; |
| 432 |
shift2++; |
| 433 |
} |
| 434 |
if (x > 0xff) { |
| 435 |
ORC_PROGRAM_ERROR(compiler, "bad immediate value"); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
code = 0xe3a00000; |
| 440 |
code |= (dest&0xf) << 12; |
| 441 |
code |= (((16-shift2)&0xf) << 8); |
| 442 |
code |= (x&0xff); |
| 443 |
|
| 444 |
ORC_ASM_CODE(compiler," mov %s, #0x%08x\n", orc_arm_reg_name (dest), imm); |
| 445 |
orc_arm_emit (compiler, code); |
| 446 |
} |
| 447 |
|
| 448 |
void |
| 449 |
orc_arm_emit_add_imm (OrcCompiler *compiler, int dest, int src1, int imm) |
| 450 |
{ |
| 451 |
orc_uint32 code; |
| 452 |
int shift2; |
| 453 |
unsigned int x; |
| 454 |
|
| 455 |
if ((imm & 0xff) == imm) { |
| 456 |
shift2 = 0; |
| 457 |
x = imm; |
| 458 |
} else { |
| 459 |
shift2 = 0; |
| 460 |
x = imm & 0xffffffff; |
| 461 |
while ((x & 3) == 0) { |
| 462 |
x >>= 2; |
| 463 |
shift2++; |
| 464 |
} |
| 465 |
if (x > 0xff) { |
| 466 |
ORC_PROGRAM_ERROR(compiler, "bad immediate value"); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
code = 0xe2800000; |
| 471 |
code |= (src1&0xf) << 16; |
| 472 |
code |= (dest&0xf) << 12; |
| 473 |
code |= (((16-shift2)&0xf) << 8); |
| 474 |
code |= (x&0xff); |
| 475 |
|
| 476 |
ORC_ASM_CODE(compiler," add %s, %s, #0x%08x\n", orc_arm_reg_name (dest), |
| 477 |
orc_arm_reg_name(src1), imm); |
| 478 |
orc_arm_emit (compiler, code); |
| 479 |
} |
| 480 |
|
| 481 |
void |
| 482 |
orc_arm_emit_and_imm (OrcCompiler *compiler, int dest, int src1, int value) |
| 483 |
{ |
| 484 |
orc_uint32 code; |
| 485 |
|
| 486 |
code = 0xe2000000; |
| 487 |
code |= (src1&0xf) << 16; |
| 488 |
code |= (dest&0xf) << 12; |
| 489 |
code |= (value) << 0; |
| 490 |
|
| 491 |
ORC_ASM_CODE(compiler," and %s, %s, #%d\n", |
| 492 |
orc_arm_reg_name (dest), |
| 493 |
orc_arm_reg_name (src1), |
| 494 |
value); |
| 495 |
orc_arm_emit (compiler, code); |
| 496 |
} |
| 497 |
|
| 498 |
void |
| 499 |
orc_arm_emit_cmp (OrcCompiler *compiler, int src1, int src2) |
| 500 |
{ |
| 501 |
orc_uint32 code; |
| 502 |
|
| 503 |
code = 0xe1500000; |
| 504 |
code |= (src1&0xf) << 16; |
| 505 |
code |= (src2&0xf) << 0; |
| 506 |
|
| 507 |
ORC_ASM_CODE(compiler," cmp %s, %s\n", |
| 508 |
orc_arm_reg_name (src1), |
| 509 |
orc_arm_reg_name (src2)); |
| 510 |
orc_arm_emit (compiler, code); |
| 511 |
} |
| 512 |
|
| 513 |
void |
| 514 |
orc_arm_emit_asr_imm (OrcCompiler *compiler, int dest, int src1, int value) |
| 515 |
{ |
| 516 |
orc_uint32 code; |
| 517 |
|
| 518 |
if (value == 0) { |
| 519 |
ORC_ERROR("bad immediate value"); |
| 520 |
} |
| 521 |
code = 0xe1a00040; |
| 522 |
code |= (src1&0xf) << 0; |
| 523 |
code |= (dest&0xf) << 12; |
| 524 |
code |= (value) << 7; |
| 525 |
|
| 526 |
ORC_ASM_CODE(compiler," asr %s, %s, #%d\n", |
| 527 |
orc_arm_reg_name (dest), |
| 528 |
orc_arm_reg_name (src1), |
| 529 |
value); |
| 530 |
orc_arm_emit (compiler, code); |
| 531 |
} |
| 532 |
|
| 533 |
void |
| 534 |
orc_arm_emit_lsl_imm (OrcCompiler *compiler, int dest, int src1, int value) |
| 535 |
{ |
| 536 |
orc_uint32 code; |
| 537 |
|
| 538 |
if (value == 0) { |
| 539 |
ORC_ERROR("bad immediate value"); |
| 540 |
} |
| 541 |
code = 0xe1a00000; |
| 542 |
code |= (src1&0xf) << 0; |
| 543 |
code |= (dest&0xf) << 12; |
| 544 |
code |= (value) << 7; |
| 545 |
|
| 546 |
ORC_ASM_CODE(compiler," lsl %s, %s, #%d\n", |
| 547 |
orc_arm_reg_name (dest), |
| 548 |
orc_arm_reg_name (src1), |
| 549 |
value); |
| 550 |
orc_arm_emit (compiler, code); |
| 551 |
} |
| 552 |
|
| 553 |
void |
| 554 |
orc_arm_emit_load_reg (OrcCompiler *compiler, int dest, int src1, int offset) |
| 555 |
{ |
| 556 |
orc_uint32 code; |
| 557 |
|
| 558 |
code = 0xe5900000; |
| 559 |
code |= (src1&0xf) << 16; |
| 560 |
code |= (dest&0xf) << 12; |
| 561 |
code |= offset&0xfff; |
| 562 |
|
| 563 |
ORC_ASM_CODE(compiler," ldr %s, [%s, #%d]\n", |
| 564 |
orc_arm_reg_name (dest), |
| 565 |
orc_arm_reg_name (src1), offset); |
| 566 |
orc_arm_emit (compiler, code); |
| 567 |
} |
| 568 |
|
| 569 |
void |
| 570 |
orc_arm_emit_store_reg (OrcCompiler *compiler, int src1, int dest, int offset) |
| 571 |
{ |
| 572 |
orc_uint32 code; |
| 573 |
|
| 574 |
code = 0xe5800000; |
| 575 |
code |= (dest&0xf) << 16; |
| 576 |
code |= (src1&0xf) << 12; |
| 577 |
code |= offset&0xfff; |
| 578 |
|
| 579 |
ORC_ASM_CODE(compiler," str %s, [%s, #%d]\n", |
| 580 |
orc_arm_reg_name (src1), |
| 581 |
orc_arm_reg_name (dest), offset); |
| 582 |
orc_arm_emit (compiler, code); |
| 583 |
} |
| 584 |
|
| 585 |
void |
| 586 |
orc_arm_emit_mov (OrcCompiler *compiler, int dest, int src) |
| 587 |
{ |
| 588 |
if (dest == src) return; |
| 589 |
orc_arm_emit_mov_r(compiler, ORC_ARM_COND_AL, 0, dest, src); |
| 590 |
} |
| 591 |
|
| 592 |
void |
| 593 |
orc_arm_emit_sub (OrcCompiler *compiler, int dest, int src1, int src2) |
| 594 |
{ |
| 595 |
orc_arm_emit_sub_r (compiler, ORC_ARM_COND_AL, 0, dest, src1, src2); |
| 596 |
} |
| 597 |
|
| 598 |
void |
| 599 |
orc_arm_emit_sub_imm (OrcCompiler *compiler, int dest, int src1, int value, |
| 600 |
int record) |
| 601 |
{ |
| 602 |
orc_arm_emit_sub_i (compiler, ORC_ARM_COND_AL, record, dest, src1, value); |
| 603 |
} |
| 604 |
|
| 605 |
void |
| 606 |
orc_arm_emit_add (OrcCompiler *compiler, int dest, int src1, int src2) |
| 607 |
{ |
| 608 |
orc_arm_emit_add_r (compiler, ORC_ARM_COND_AL, 0, dest, src1, src2); |
| 609 |
} |
| 610 |
|
| 611 |
void |
| 612 |
orc_arm_emit_cmp_imm (OrcCompiler *compiler, int src1, int value) |
| 613 |
{ |
| 614 |
orc_arm_emit_cmp_i (compiler, ORC_ARM_COND_AL, src1, value); |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
/* shifter operands */ |
| 619 |
/* 1 |
| 620 |
* 1 0 9 8 7 6 5 4 3 2 1 0 |
| 621 |
* +-+-+-+-+-+-+-+-+-+-+-+-+ |
| 622 |
* |rotimm | immed_8 | |
| 623 |
* +-+-+-+-+-+-+-+-+-+-+-+-+ |
| 624 |
*/ |
| 625 |
#define arm_so_i(rot,imm) ((((rot)&15)<<8)|((imm)&255)) |
| 626 |
/* 1 |
| 627 |
* 1 0 9 8 7 6 5 4 3 2 1 0 |
| 628 |
* +-+-+-+-+-+-+-+-+-+-+-+-+ |
| 629 |
* | Si | St|0| Rm | |
| 630 |
* +-+-+-+-+-+-+-+-+-+-+-+-+ |
| 631 |
*/ |
| 632 |
#define arm_so_rsi(Si,St,Rm) ((((Si)&31)<<7)|(((St)&3)<<5)|((Rm)&15)) |
| 633 |
#define arm_so_rrx(Rm) arm_so_rsi(0,ORC_ARM_ROR,Rm) |
| 634 |
#define arm_so_r(Rm) ((Rm)&15) |
| 635 |
/* 1 |
| 636 |
* 1 0 9 8 7 6 5 4 3 2 1 0 |
| 637 |
* +-+-+-+-+-+-+-+-+-+-+-+-+ |
| 638 |
* | Rs |0| St|1| Rm | |
| 639 |
* +-+-+-+-+-+-+-+-+-+-+-+-+ |
| 640 |
*/ |
| 641 |
#define arm_so_rsr(Rs,St,Rm) (0x010|(((Rs)&15)<<8)|(((St)&3)<<5)|((Rm)&15)) |
| 642 |
|
| 643 |
/* data processing instructions */ |
| 644 |
/* 3 2 2 2 2 2 2 2 1 1 1 1 1 |
| 645 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 646 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 647 |
* | cond |0 0|I| opcode|S| Rn | Rd | shifter_operand | |
| 648 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 649 |
*/ |
| 650 |
#define arm_code_dp(cond,I,opcode,S,Rn,Rd,So) ((((cond)&15)<<28) | (((I)&1)<<25) | \ |
| 651 |
(((opcode)&15)<<21) | (((S)&1)<<20) | \ |
| 652 |
(((Rn)&15)<<16) | (((Rd)&15)<<12) | \ |
| 653 |
((So)&0xfff)) |
| 654 |
|
| 655 |
/* |
| 656 |
* type 0: <op>{<cond>}{s} {<Rd>}, <Rn>, #imm (imm = (val>>(shift*2))|(val<<(32-(shift*2)))) |
| 657 |
* type 1: <op>{<cond>}{s} {<Rd>}, <Rn>, <Rm> |
| 658 |
* type 2: <op>{<cond>}{s} {<Rd>}, <Rn>, <Rm>, [LSL|LSR|ASR] #imm |
| 659 |
* type 3: <op>{<cond>}{s} {<Rd>}, <Rn>, <Rm>, [LSL|LSR|ASR] <Rs> |
| 660 |
* type 4: <op>{<cond>}{s} {<Rd>,} <Rn>, <Rm>, RRX |
| 661 |
*/ |
| 662 |
void |
| 663 |
orc_arm_emit_dp (OrcCompiler *p, int type, OrcArmCond cond, OrcArmDP opcode, |
| 664 |
int S, int Rd, int Rn, int Rm, int shift, orc_uint32 val) |
| 665 |
{ |
| 666 |
orc_uint32 code; |
| 667 |
int I = 0; |
| 668 |
int shifter_op; |
| 669 |
char shifter[64]; |
| 670 |
orc_uint32 imm; |
| 671 |
static const char *shift_names[] = { |
| 672 |
"LSL", "LSR", "ASR", "ROR" |
| 673 |
}; |
| 674 |
/* opcodes with Rd */ |
| 675 |
static const int op_Rd[] = { |
| 676 |
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 |
| 677 |
}; |
| 678 |
/* opcodes using Rn */ |
| 679 |
static const int op_Rn[] = { |
| 680 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0 |
| 681 |
}; |
| 682 |
static const char *dp_insn_names[] = { |
| 683 |
"and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc", |
| 684 |
"tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn" |
| 685 |
}; |
| 686 |
|
| 687 |
switch (type) { |
| 688 |
case 0: |
| 689 |
/* #imm */ |
| 690 |
imm = (orc_uint32) val; |
| 691 |
/* if imm <= 0xff we're done. It's recommanded that we choose the |
| 692 |
* smallest shifter value. Impossible values will overflow the shifter. */ |
| 693 |
while (imm > 0xff && shift < 16) { |
| 694 |
imm = (imm << 2) | (imm >> 30); |
| 695 |
shift++; |
| 696 |
} |
| 697 |
if (shift > 15) { |
| 698 |
ORC_COMPILER_ERROR(p,"invalid ARM immediate %08x", val); |
| 699 |
return; |
| 700 |
} |
| 701 |
shifter_op = arm_so_i (shift, imm); |
| 702 |
sprintf (shifter, "#0x%08x", val); |
| 703 |
I = 1; |
| 704 |
break; |
| 705 |
case 1: |
| 706 |
/* <Rm> */ |
| 707 |
shifter_op = arm_so_r (Rm); |
| 708 |
sprintf (shifter, "%s", orc_arm_reg_name (Rm)); |
| 709 |
break; |
| 710 |
case 2: |
| 711 |
/* <Rm>, [LSL|LSR|ASR] #imm */ |
| 712 |
shifter_op = arm_so_rsi (val,shift,Rm); |
| 713 |
sprintf (shifter, "%s, %s #%d", |
| 714 |
orc_arm_reg_name (Rm), shift_names[shift], val); |
| 715 |
break; |
| 716 |
case 3: |
| 717 |
/* <Rm>, [LSL|LSR|ASR] <Rs> */ |
| 718 |
shifter_op = arm_so_rsr (val,shift,Rm); |
| 719 |
sprintf (shifter, "%s, %s %s", |
| 720 |
orc_arm_reg_name (Rm), shift_names[shift], orc_arm_reg_name (val)); |
| 721 |
break; |
| 722 |
case 4: |
| 723 |
/* <Rm>, RRX */ |
| 724 |
shifter_op = arm_so_rrx (Rm); |
| 725 |
sprintf (shifter, "%s, RRX", |
| 726 |
orc_arm_reg_name (Rm)); |
| 727 |
break; |
| 728 |
default: |
| 729 |
ORC_COMPILER_ERROR(p,"unknown data processing type %d", type); |
| 730 |
return; |
| 731 |
} |
| 732 |
|
| 733 |
if (op_Rd[opcode]) { |
| 734 |
if (op_Rn[opcode]) { |
| 735 |
/* opcode using Rn */ |
| 736 |
code = arm_code_dp (cond, I, opcode, S, Rn, Rd, shifter_op); |
| 737 |
ORC_ASM_CODE(p," %s%s%s %s, %s, %s\n", |
| 738 |
dp_insn_names[opcode], orc_arm_cond_name(cond), (S ? "s" : ""), |
| 739 |
orc_arm_reg_name (Rd), orc_arm_reg_name (Rn), shifter); |
| 740 |
} else { |
| 741 |
/* opcode using Rd and val (mov, mvn) */ |
| 742 |
code = arm_code_dp (cond, I, opcode, S, Rn, Rd, shifter_op); |
| 743 |
ORC_ASM_CODE(p," %s%s%s %s, %s\n", |
| 744 |
dp_insn_names[opcode], orc_arm_cond_name(cond), (S ? "s" : ""), |
| 745 |
orc_arm_reg_name (Rd), shifter); |
| 746 |
} |
| 747 |
} else { |
| 748 |
/* opcode does not change Rd, change status register (cmp, tst, ..) */ |
| 749 |
code = arm_code_dp (cond, I, opcode, 1, Rn, 0, shifter_op); |
| 750 |
ORC_ASM_CODE(p," %s%s %s, %s\n", |
| 751 |
dp_insn_names[opcode], orc_arm_cond_name(cond), orc_arm_reg_name (Rn), shifter); |
| 752 |
} |
| 753 |
orc_arm_emit (p, code); |
| 754 |
} |
| 755 |
|
| 756 |
/* parallel instructions */ |
| 757 |
/* 3 2 2 2 2 2 2 2 1 1 1 1 1 |
| 758 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 759 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 760 |
* | cond | mode | Rn | Rd |0 0 0 0| op | Rm | |
| 761 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 762 |
*/ |
| 763 |
#define arm_code_par(cond,mode,Rn,Rd,op,Rm) (((cond)<<28)|((mode)<<20)|(((Rn)&0xf)<<16)|(((Rd)&0xf)<<12)|((op)<<4)|((Rm)&0xf)|0xf00) |
| 764 |
|
| 765 |
void |
| 766 |
orc_arm_emit_par (OrcCompiler *p, int op, int mode, OrcArmCond cond, |
| 767 |
int Rd, int Rn, int Rm) |
| 768 |
{ |
| 769 |
orc_uint32 code; |
| 770 |
static const int par_op[] = { |
| 771 |
1, 3, 5, 7, 9, 15, 11, 5, 5 |
| 772 |
}; |
| 773 |
static const char *par_op_names[] = { |
| 774 |
"add16", "addsubx", "subaddx", "sub16", "add8", "sub8", "sel", "add", "sub" |
| 775 |
}; |
| 776 |
static const int par_mode[] = { |
| 777 |
0x61, 0x62, 0x63, 0x65, 0x66, 0x67, 0x68, 0x10, 0x12, 0x14, 0x16 |
| 778 |
}; |
| 779 |
static const char *par_mode_names[] = { |
| 780 |
"s", "q", "sh", "u", "uq", "uh", "", "q", "q", "qd", "qd" |
| 781 |
}; |
| 782 |
|
| 783 |
code = arm_code_par (cond, par_mode[mode], Rn, Rd, par_op[op], Rm); |
| 784 |
if (op == 7) { |
| 785 |
int tmp; |
| 786 |
/* gas does something screwy here */ |
| 787 |
code &= ~0xf00; |
| 788 |
tmp = Rn; |
| 789 |
Rn = Rm; |
| 790 |
Rm = tmp; |
| 791 |
} |
| 792 |
ORC_ASM_CODE(p," %s%s%s %s, %s, %s\n", |
| 793 |
par_mode_names[mode], par_op_names[op], orc_arm_cond_name(cond), |
| 794 |
orc_arm_reg_name (Rd), |
| 795 |
orc_arm_reg_name (Rn), |
| 796 |
orc_arm_reg_name (Rm)); |
| 797 |
orc_arm_emit (p, code); |
| 798 |
} |
| 799 |
|
| 800 |
/* extend instructions */ |
| 801 |
/* 3 2 2 2 2 2 2 2 1 1 1 1 1 1 |
| 802 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 803 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 804 |
* | cond |0 1 1 0 0 0 0 0| Rn | Rd |rot|0 0|0 1 1 1| Rm | |
| 805 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 806 |
*/ |
| 807 |
#define arm_code_xt(op,cond,Rn,Rd,r8,Rm) (op|((cond)<<28)|(((Rn)&0xf)<<16)|(((Rd)&0xf)<<12)|((((r8)&0xf)&0x18)<<7)|((Rm)&0xf)) |
| 808 |
|
| 809 |
void |
| 810 |
orc_arm_emit_xt (OrcCompiler *p, int op, OrcArmCond cond, |
| 811 |
int Rd, int Rn, int Rm, int r8) |
| 812 |
{ |
| 813 |
orc_uint32 code; |
| 814 |
char shifter[64]; |
| 815 |
static const orc_uint32 xt_opcodes[] = { |
| 816 |
0x06800070, 0x06a00070, 0x06b00070, 0x06c00070, 0x06e00070, 0x06f00070 |
| 817 |
}; |
| 818 |
static const char *xt_insn_names[] = { |
| 819 |
"sxtb16", "sxtb", "sxth", "uxtb16", "uxtb", "uxth", |
| 820 |
"sxtab16", "sxtab", "sxtah", "uxtab16", "uxtab", "uxtah", |
| 821 |
}; |
| 822 |
|
| 823 |
if (r8 & 0x18) |
| 824 |
sprintf (shifter, ", ROR #%d", r8 & 0x18); |
| 825 |
else |
| 826 |
shifter[0] = '\0'; |
| 827 |
|
| 828 |
code = arm_code_xt (xt_opcodes[op], cond, Rn, Rd, r8, Rm); |
| 829 |
if (Rn < 15) { |
| 830 |
/* with Rn */ |
| 831 |
ORC_ASM_CODE(p," %s%s %s, %s, %s%s\n", |
| 832 |
xt_insn_names[op], orc_arm_cond_name(cond), |
| 833 |
orc_arm_reg_name (Rd), |
| 834 |
orc_arm_reg_name (Rn), |
| 835 |
orc_arm_reg_name (Rm), |
| 836 |
shifter); |
| 837 |
} else { |
| 838 |
ORC_ASM_CODE(p," %s%s %s, %s%s\n", |
| 839 |
xt_insn_names[op], orc_arm_cond_name(cond), |
| 840 |
orc_arm_reg_name (Rd), |
| 841 |
orc_arm_reg_name (Rm), |
| 842 |
shifter); |
| 843 |
} |
| 844 |
orc_arm_emit (p, code); |
| 845 |
} |
| 846 |
|
| 847 |
#define arm_code_pkh(op,cond,Rn,Rd,sh,Rm) (op|((cond)<<28)|(((Rn)&0xf)<<16)|(((Rd)&0xf)<<12)|((sh)<<7)|((Rm)&0xf)) |
| 848 |
void |
| 849 |
orc_arm_emit_pkh (OrcCompiler *p, int op, OrcArmCond cond, |
| 850 |
int Rd, int Rn, int Rm, int sh) |
| 851 |
{ |
| 852 |
orc_uint32 code; |
| 853 |
char shifter[64]; |
| 854 |
static const orc_uint32 pkh_opcodes[] = { 0x06800010, 0x06800050 }; |
| 855 |
static const char *pkh_insn_names[] = { "pkhbt", "pkhtb" }; |
| 856 |
|
| 857 |
if (sh > 0) { |
| 858 |
sprintf (shifter, ", %s #%d", |
| 859 |
(op == 0 ? "LSL" : "ASR"), sh); |
| 860 |
} else { |
| 861 |
shifter[0] = '\0'; |
| 862 |
} |
| 863 |
|
| 864 |
code = arm_code_pkh (pkh_opcodes[op], cond, Rn, Rd, sh, Rm); |
| 865 |
ORC_ASM_CODE(p," %s%s %s, %s, %s%s\n", |
| 866 |
pkh_insn_names[op], orc_arm_cond_name(cond), |
| 867 |
orc_arm_reg_name (Rd), |
| 868 |
orc_arm_reg_name (Rn), |
| 869 |
orc_arm_reg_name (Rm), |
| 870 |
shifter); |
| 871 |
orc_arm_emit (p, code); |
| 872 |
} |
| 873 |
|
| 874 |
/* extend instructions */ |
| 875 |
/* 3 2 2 2 2 1 1 1 1 1 1 |
| 876 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 877 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 878 |
* | cond |0 1 1 0|x x x| sat | Rd | sh |a|0 1| Rm | |
| 879 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 880 |
*/ |
| 881 |
#define arm_code_sat(op,cond,sat,Rd,sh,a,Rm) (op|(((cond)&15)<<28)|(((sat)&31)<<16)|\ |
| 882 |
(((Rd)&15)<<12)|(((sh)&31)<<7)|(((a)&1)<<6)|\ |
| 883 |
((Rm)&15)) |
| 884 |
void |
| 885 |
orc_arm_emit_sat (OrcCompiler *p, int op, OrcArmCond cond, |
| 886 |
int Rd, int sat, int Rm, int sh, int asr) |
| 887 |
{ |
| 888 |
orc_uint32 code; |
| 889 |
char shifter[64]; |
| 890 |
static const orc_uint32 sat_opcodes[] = { 0x06a00010, 0x06e00010, 0, 0 }; |
| 891 |
static const char *sat_insn_names[] = { "ssat", "usat", "ssat16", "usat16" }; |
| 892 |
static const int par_mode[] = { 0, 0, 0x6a, 0x6e }; |
| 893 |
static const int par_op[] = { 0, 0, 3, 3 }; |
| 894 |
|
| 895 |
if (sh > 0) { |
| 896 |
sprintf (shifter, ", %s #%d", |
| 897 |
(asr&1 ? "ASR" : "LSL"), sh); |
| 898 |
} else { |
| 899 |
shifter[0] = '\0'; |
| 900 |
} |
| 901 |
|
| 902 |
if (op < 2) { |
| 903 |
code = arm_code_sat (sat_opcodes[op], cond, sat, Rd, sh, asr, Rm); |
| 904 |
} else { |
| 905 |
if (op == 3) { |
| 906 |
code = arm_code_par (cond, par_mode[op], sat, Rd, par_op[op], Rm); |
| 907 |
} else { |
| 908 |
code = arm_code_par (cond, par_mode[op], sat - 1, Rd, par_op[op], Rm); |
| 909 |
} |
| 910 |
} |
| 911 |
ORC_ASM_CODE(p," %s%s %s, #%d, %s%s\n", |
| 912 |
sat_insn_names[op], orc_arm_cond_name(cond), |
| 913 |
orc_arm_reg_name (Rd), |
| 914 |
sat, |
| 915 |
orc_arm_reg_name (Rm), |
| 916 |
shifter); |
| 917 |
orc_arm_emit (p, code); |
| 918 |
} |
| 919 |
|
| 920 |
#define arm_code_rv(op,cond,Rd,Rm) (op|(((cond)&15)<<28)|(((Rd)&15)<<12)|((Rm)&15)) |
| 921 |
void |
| 922 |
orc_arm_emit_rv (OrcCompiler *p, int op, OrcArmCond cond, |
| 923 |
int Rd, int Rm) |
| 924 |
{ |
| 925 |
orc_uint32 code; |
| 926 |
static const orc_uint32 rv_opcodes[] = { 0x06bf0f30, 0x06bf0fb0 }; |
| 927 |
static const char *rv_insn_names[] = { "rev", "rev16" }; |
| 928 |
|
| 929 |
code = arm_code_rv (rv_opcodes[op], cond, Rd, Rm); |
| 930 |
ORC_ASM_CODE(p," %s%s %s, %s\n", |
| 931 |
rv_insn_names[op], orc_arm_cond_name(cond), |
| 932 |
orc_arm_reg_name (Rd), orc_arm_reg_name (Rm)); |
| 933 |
orc_arm_emit (p, code); |
| 934 |
} |
| 935 |
|
| 936 |
void |
| 937 |
orc_arm_flush_cache (OrcCode *code) |
| 938 |
{ |
| 939 |
#if defined (HAVE_ARM) || defined (HAVE_AARCH64) |
| 940 |
#ifdef __APPLE__ |
| 941 |
sys_dcache_flush(code->code, code->code_size); |
| 942 |
sys_icache_invalidate(code->exec, code->code_size); |
| 943 |
#elif defined (_WIN32) |
| 944 |
HANDLE h_proc = GetCurrentProcess(); |
| 945 |
|
| 946 |
FlushInstructionCache(h_proc, code->code, code->code_size); |
| 947 |
|
| 948 |
if ((void *) code->exec != (void *) code->code) |
| 949 |
FlushInstructionCache(h_proc, code->exec, code->code_size); |
| 950 |
#else |
| 951 |
__clear_cache (code->code, code->code + code->code_size); |
| 952 |
if ((void *) code->exec != (void *) code->code) |
| 953 |
__clear_cache (code->exec, code->exec + code->code_size); |
| 954 |
#endif |
| 955 |
#endif |
| 956 |
} |
| 957 |
|
| 958 |
void |
| 959 |
orc_arm_emit_data (OrcCompiler *compiler, orc_uint32 data) |
| 960 |
{ |
| 961 |
if (compiler->target_flags & ORC_TARGET_CLEAN_COMPILE) { |
| 962 |
orc_arm_emit_nop (compiler); |
| 963 |
} else { |
| 964 |
ORC_ASM_CODE(compiler," .word 0x%08x\n", data); |
| 965 |
orc_arm_emit (compiler, data); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
void |
| 970 |
orc_arm_loadw (OrcCompiler *compiler, int dest, int src1, int offset) |
| 971 |
{ |
| 972 |
orc_uint32 code; |
| 973 |
|
| 974 |
code = 0xe1d000b0; |
| 975 |
code |= (src1&0xf) << 16; |
| 976 |
code |= (dest&0xf) << 12; |
| 977 |
code |= (offset&0xf0) << 4; |
| 978 |
code |= offset&0x0f; |
| 979 |
|
| 980 |
ORC_ASM_CODE(compiler," ldrh %s, [%s, #%d]\n", |
| 981 |
orc_arm_reg_name (dest), |
| 982 |
orc_arm_reg_name (src1), offset); |
| 983 |
orc_arm_emit (compiler, code); |
| 984 |
} |
| 985 |
|
| 986 |
/** AArch64 instructions */ |
| 987 |
|
| 988 |
#define ARM64_MAX_OP_LEN 64 |
| 989 |
|
| 990 |
/** data processing instructions: Arithmetic |
| 991 |
* |
| 992 |
* Immediate |
| 993 |
* 3 2 1 |
| 994 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 995 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 996 |
* |b|op |1 0 0 0 1|sft| imm12 | Rn | Rd | |
| 997 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 998 |
* |
| 999 |
* Shifted register |
| 1000 |
* 3 2 1 |
| 1001 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1002 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1003 |
* |b|op |0 1 0 1 1|sft|0| Rm | imm6 | Rn | Rd | |
| 1004 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1005 |
* |
| 1006 |
* Extended register |
| 1007 |
* 3 2 1 |
| 1008 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1009 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1010 |
* |b|op |0 1 0 1 1|0 0|1| Rm | opt |imm3 | Rn | Rd | |
| 1011 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1012 |
*/ |
| 1013 |
|
| 1014 |
#define arm64_code_arith_imm(b,op,sft,imm,Rn,Rd) (0x11000000 | \ |
| 1015 |
((((b)==64)&0x1)<<31) | (((op)&0x3)<<29) | (((sft)&0x3)<<22) | \ |
| 1016 |
(((imm)&0xfff)<<10) | (((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1017 |
|
| 1018 |
#define arm64_code_arith_reg(b,op,sft,Rm,imm,Rn,Rd) (0x0b000000 | \ |
| 1019 |
((((b)==64)&0x1)<<31) | (((op)&0x3)<<29) | (((sft)&0x3)<<22) | \ |
| 1020 |
(((Rm)&0x1f)<<16) | (((imm)&0x3f)<<10) | (((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1021 |
|
| 1022 |
#define arm64_code_arith_ext(b,op,Rm,opt,imm,Rn,Rd) (0x0b200000 | \ |
| 1023 |
((((b)==64)&0x1)<<31) | (((op)&0x3)<<29) | (((Rm)&0x1f)<<16) | \ |
| 1024 |
(((opt)&0x7)<<13) | (((imm)&0x7)<<10) | (((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1025 |
|
| 1026 |
void |
| 1027 |
orc_arm64_emit_am (OrcCompiler *p, OrcArm64RegBits bits, OrcArm64DP opcode, |
| 1028 |
OrcArm64Type type, int opt, int Rd, int Rn, int Rm, orc_uint64 val) |
| 1029 |
{ |
| 1030 |
orc_uint32 code; |
| 1031 |
orc_uint32 imm; |
| 1032 |
|
| 1033 |
int shift, extend; |
| 1034 |
|
| 1035 |
static const char *insn_names[] = { |
| 1036 |
"add", "adds", "sub", "subs", |
| 1037 |
}; |
| 1038 |
static const char *insn_alias[] = { |
| 1039 |
"ERROR", "cmn", "ERROR", "cmp", |
| 1040 |
}; |
| 1041 |
static const char *shift_names[] = { |
| 1042 |
"lsl", "lsr", "asr", "ror" |
| 1043 |
}; |
| 1044 |
static const char *extend_names[] = { |
| 1045 |
"uxtb", "uxth", "uxtw", "uxtx", |
| 1046 |
"sxtb", "sxth", "sxtw", "sxtx" |
| 1047 |
}; |
| 1048 |
|
| 1049 |
int alias_rd; |
| 1050 |
char opt_rm[ARM64_MAX_OP_LEN]; |
| 1051 |
|
| 1052 |
opcode -= ORC_ARM64_DP_ADD; |
| 1053 |
|
| 1054 |
if (opcode >= sizeof(insn_names)/sizeof(insn_names[0])) { |
| 1055 |
ORC_COMPILER_ERROR(p, "unsupported opcode %d", opcode); |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/** if a reg is not specified, it's regarded as alias; set it to SP (== 0b11111) */ |
| 1060 |
alias_rd = 0; |
| 1061 |
if (Rd == 0) { |
| 1062 |
Rd = ORC_ARM64_SP; |
| 1063 |
alias_rd = 1; |
| 1064 |
} |
| 1065 |
|
| 1066 |
memset (opt_rm, '\x00', ARM64_MAX_OP_LEN); |
| 1067 |
|
| 1068 |
switch (type) { |
| 1069 |
case ORC_ARM64_TYPE_IMM: /** immediate */ |
| 1070 |
/**, #imm */ |
| 1071 |
shift = 0; |
| 1072 |
imm = (orc_uint32) val; |
| 1073 |
|
| 1074 |
/** Support up to 24-bit immediate value, explicitly shifted left (LSL) by 0 or 12 */ |
| 1075 |
if (val > 0xfff) { |
| 1076 |
if (val > 0xffffff) { |
| 1077 |
ORC_COMPILER_ERROR(p, "imm is out-of-range %llx", val); |
| 1078 |
return; |
| 1079 |
} |
| 1080 |
if (val & 0xfff) { |
| 1081 |
ORC_WARNING("offset is truncated %llx", val); |
| 1082 |
} |
| 1083 |
imm >>= 12; |
| 1084 |
shift = 1; |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (shift) |
| 1088 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", #%u, lsl #12", imm); |
| 1089 |
else |
| 1090 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", #%u", imm); |
| 1091 |
|
| 1092 |
code = arm64_code_arith_imm (bits, opcode, shift, imm, Rn, Rd); |
| 1093 |
break; |
| 1094 |
case ORC_ARM64_TYPE_REG: /** shifted register */ |
| 1095 |
/**, <Rm>, shift #amount */ |
| 1096 |
shift = opt; |
| 1097 |
imm = (orc_uint32) val; |
| 1098 |
|
| 1099 |
if (shift >= sizeof(shift_names)/sizeof(shift_names[0])) { |
| 1100 |
ORC_COMPILER_ERROR(p, "unsupported shift %d", shift); |
| 1101 |
return; |
| 1102 |
} |
| 1103 |
|
| 1104 |
if (val > 0) { |
| 1105 |
if (val > 63) { |
| 1106 |
ORC_COMPILER_ERROR(p, "shift is out-of-range %llx", val); |
| 1107 |
return; |
| 1108 |
} |
| 1109 |
|
| 1110 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s, %s #%u", |
| 1111 |
orc_arm64_reg_name (Rm, bits), shift_names[shift], imm); |
| 1112 |
} else |
| 1113 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s", orc_arm64_reg_name (Rm, bits)); |
| 1114 |
|
| 1115 |
code = arm64_code_arith_reg (bits, opcode, shift, Rm, imm, Rn, Rd); |
| 1116 |
break; |
| 1117 |
case ORC_ARM64_TYPE_EXT: /** extended register */ |
| 1118 |
/**, <Rm>, extend #amount */ |
| 1119 |
extend = opt; |
| 1120 |
imm = (orc_uint32) val; |
| 1121 |
|
| 1122 |
if (extend >= sizeof(extend_names)/sizeof(extend_names[0])) { |
| 1123 |
ORC_COMPILER_ERROR(p, "unsupported extend %d", extend); |
| 1124 |
return; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (val > 0) { |
| 1128 |
if (val > 4) { |
| 1129 |
ORC_COMPILER_ERROR(p, "shift is out-of-range %llx\n", val); |
| 1130 |
return; |
| 1131 |
} |
| 1132 |
/** its width is determined by extend; '0bx11' ==> 64-bit reg */ |
| 1133 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s, %s #%u", |
| 1134 |
orc_arm64_reg_name (Rm, extend & 0x3 ? ORC_ARM64_REG_64 : ORC_ARM64_REG_32), |
| 1135 |
extend_names[extend], imm); |
| 1136 |
} else |
| 1137 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s", orc_arm64_reg_name (Rm, bits)); |
| 1138 |
|
| 1139 |
code = arm64_code_arith_ext(bits, opcode, Rm, extend, imm, Rn, Rd); |
| 1140 |
break; |
| 1141 |
default: |
| 1142 |
ORC_COMPILER_ERROR(p, "unknown data processing type %d", type); |
| 1143 |
return; |
| 1144 |
} |
| 1145 |
|
| 1146 |
/** it's preferred to use alias names if exists */ |
| 1147 |
if (alias_rd) { |
| 1148 |
ORC_ASM_CODE(p, " %s %s%s\n", insn_alias[opcode], |
| 1149 |
orc_arm64_reg_name(Rn, bits), opt_rm); |
| 1150 |
} else { |
| 1151 |
ORC_ASM_CODE(p, " %s %s, %s%s\n", insn_names[opcode], |
| 1152 |
orc_arm64_reg_name(Rd, bits), orc_arm64_reg_name(Rn, bits), opt_rm); |
| 1153 |
} |
| 1154 |
|
| 1155 |
orc_arm_emit (p, code); |
| 1156 |
} |
| 1157 |
|
| 1158 |
/** data processing instructions: Logical |
| 1159 |
* |
| 1160 |
* Immediate |
| 1161 |
* 3 2 1 |
| 1162 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1163 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1164 |
* |b|op |1 0 0 1 0 0| imm13 | Rn | Rd | |
| 1165 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1166 |
* |
| 1167 |
* Shifted register |
| 1168 |
* 3 2 1 |
| 1169 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1170 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1171 |
* |b|op |0 1 0 1 0|sft|0| Rm | imm6 | Rn | Rd | |
| 1172 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1173 |
*/ |
| 1174 |
|
| 1175 |
#define arm64_code_logical_imm(b,op,imm,Rn,Rd) (0x12000000 | \ |
| 1176 |
((((b)==64)&0x1)<<31) | (((op)&0x3)<<29) | (((imm)&0x1fff)<<10) | \ |
| 1177 |
(((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1178 |
|
| 1179 |
#define arm64_code_logical_reg(b,op,sft,Rm,imm,Rn,Rd) (0x0a000000 | \ |
| 1180 |
((((b)==64)&0x1)<<31) | (((op)&0x3)<<29) | (((sft)&0x3)<<22) | \ |
| 1181 |
(((Rm)&0x1f)<<16) | (((imm)&0x3f)<<10) | (((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1182 |
|
| 1183 |
/** |
| 1184 |
* check if it's a non-empty sequence of ones starting at the LSB with the remainder zero |
| 1185 |
* e.g., mask_ones(0x0000FFFF) == true |
| 1186 |
*/ |
| 1187 |
#define mask_ones(val) ((val) && (((val) + 1) & (val)) == 0) |
| 1188 |
/** |
| 1189 |
* check if it contains a non-empty sequence of ones with the remainder zero |
| 1190 |
* e.g., mask_shifted_ones(0x0000FF00) == true |
| 1191 |
*/ |
| 1192 |
#define mask_shifted_ones(val) ((val) && mask_ones(((val) - 1) | (val))) |
| 1193 |
|
| 1194 |
#if defined(__GNUC__) |
| 1195 |
#define count_trailing_zeros(val) __builtin_ctzll(val) |
| 1196 |
#define count_leading_zeros(val) __builtin_clzll(val) |
| 1197 |
#else |
| 1198 |
static int |
| 1199 |
count_trailing_zeros (orc_uint64 val) |
| 1200 |
{ |
| 1201 |
int i, count = 0; |
| 1202 |
|
| 1203 |
for (i = 0; i < 64; i++) { |
| 1204 |
if ((val >> i) & 1) |
| 1205 |
break; |
| 1206 |
count++; |
| 1207 |
} |
| 1208 |
|
| 1209 |
return count; |
| 1210 |
} |
| 1211 |
static int |
| 1212 |
count_leading_zeros (orc_uint64 val) |
| 1213 |
{ |
| 1214 |
int i, count = 0; |
| 1215 |
|
| 1216 |
for (i = 63; i >= 0; i--) { |
| 1217 |
if ((val >> i) & 1) |
| 1218 |
break; |
| 1219 |
count++; |
| 1220 |
} |
| 1221 |
|
| 1222 |
return count; |
| 1223 |
} |
| 1224 |
#endif |
| 1225 |
#define count_trailing_ones(val) count_trailing_zeros(~val) |
| 1226 |
#define count_leading_ones(val) count_leading_zeros(~val) |
| 1227 |
|
| 1228 |
/** |
| 1229 |
* Encode a logical immediate value (code reference: LLVM) |
| 1230 |
* 32-bit variant: immr:imms |
| 1231 |
* 64-bit variant: N:immr:imms |
| 1232 |
*/ |
| 1233 |
static int |
| 1234 |
encode_logical_imm (unsigned int size, orc_uint64 val, orc_uint32 *encoded) |
| 1235 |
{ |
| 1236 |
orc_uint64 mask; |
| 1237 |
orc_uint32 I, CTO, CLO; |
| 1238 |
orc_uint32 immr, imms, N; |
| 1239 |
|
| 1240 |
if (size > 64) |
| 1241 |
return -1; |
| 1242 |
|
| 1243 |
/** |
| 1244 |
* immediate values of all-zero and all-cones are not encoded. |
| 1245 |
* Requires at least one non-zero bit, and one zero bit. |
| 1246 |
*/ |
| 1247 |
if (val == 0ULL || val == ~0ULL || |
| 1248 |
(size != 64 && (val >> size != 0 || val == (~0ULL >> (64 - size))))) |
| 1249 |
return -2; |
| 1250 |
|
| 1251 |
/** decide the element size (i.e., 2, 4, 8, 16, 32 or 64 bits) */ |
| 1252 |
do { |
| 1253 |
size /= 2; |
| 1254 |
mask = (1ULL << size) - 1; |
| 1255 |
if ((val & mask) != ((val >> size) & mask)) { |
| 1256 |
size *= 2; |
| 1257 |
break; |
| 1258 |
} |
| 1259 |
} while (size > 2); |
| 1260 |
|
| 1261 |
/** decide the rotations to make the element be: 0^m 1^n */ |
| 1262 |
mask = ((orc_uint64)~0ULL) >> (64 - size); |
| 1263 |
val &= mask; |
| 1264 |
|
| 1265 |
if (mask_shifted_ones (val)) { |
| 1266 |
I = count_trailing_zeros (val); |
| 1267 |
CTO = count_trailing_ones (val >> I); |
| 1268 |
} else { |
| 1269 |
val |= ~mask; |
| 1270 |
if (!mask_shifted_ones (~val)) |
| 1271 |
return 0; |
| 1272 |
CLO = count_leading_ones (val); |
| 1273 |
I = 64 - CLO; |
| 1274 |
CTO = CLO + count_trailing_ones (val) - (64 - size); |
| 1275 |
} |
| 1276 |
|
| 1277 |
/** encode N:immr:imms */ |
| 1278 |
immr = (size - I) & (size - 1); |
| 1279 |
imms = ~(size-1) << 1; |
| 1280 |
imms |= (CTO-1); |
| 1281 |
N = ((imms >> 6) & 1) ^ 1; |
| 1282 |
|
| 1283 |
*encoded = (N << 12) | (immr << 6) | (imms & 0x3f); |
| 1284 |
|
| 1285 |
return 0; |
| 1286 |
} |
| 1287 |
|
| 1288 |
void |
| 1289 |
orc_arm64_emit_lg (OrcCompiler *p, OrcArm64RegBits bits, OrcArm64DP opcode, |
| 1290 |
OrcArm64Type type, int opt, int Rd, int Rn, int Rm, orc_uint64 val) |
| 1291 |
{ |
| 1292 |
orc_uint32 code; |
| 1293 |
orc_uint32 imm = 0xffffffff; |
| 1294 |
|
| 1295 |
int shift, ret; |
| 1296 |
|
| 1297 |
static const char *insn_names[] = { |
| 1298 |
"and", "orr", "eor", "ands" |
| 1299 |
}; |
| 1300 |
static const char *insn_alias[] = { |
| 1301 |
"ERROR", "mov", "ERROR", "tst" |
| 1302 |
}; |
| 1303 |
static const char *shift_names[] = { |
| 1304 |
"lsl", "lsr", "asr", "ror" |
| 1305 |
}; |
| 1306 |
|
| 1307 |
int alias_rd, alias_rn; |
| 1308 |
char opt_rm[ARM64_MAX_OP_LEN]; |
| 1309 |
|
| 1310 |
opcode -= ORC_ARM64_DP_AND; |
| 1311 |
|
| 1312 |
if (opcode >= sizeof(insn_names)/sizeof(insn_names[0])) { |
| 1313 |
ORC_COMPILER_ERROR(p, "unsupported opcode %d", opcode); |
| 1314 |
return; |
| 1315 |
} |
| 1316 |
|
| 1317 |
/** if a reg is not specified, it's regarded as alias; set it to SP (== 0b11111) */ |
| 1318 |
alias_rd = alias_rn = 0; |
| 1319 |
if (Rd == 0) { |
| 1320 |
Rd = ORC_ARM64_SP; |
| 1321 |
alias_rd = 1; |
| 1322 |
} |
| 1323 |
if (Rn == 0) { |
| 1324 |
Rn = ORC_ARM64_SP; |
| 1325 |
alias_rn = 1; |
| 1326 |
} |
| 1327 |
|
| 1328 |
memset (opt_rm, '\x00', ARM64_MAX_OP_LEN); |
| 1329 |
|
| 1330 |
switch (type) { |
| 1331 |
case ORC_ARM64_TYPE_IMM: /** immediate */ |
| 1332 |
/**, #imm */ |
| 1333 |
if (val == 0) { |
| 1334 |
ORC_COMPILER_ERROR(p, "zero imm is not supported"); |
| 1335 |
return; |
| 1336 |
} |
| 1337 |
|
| 1338 |
ret = encode_logical_imm (bits, val, &imm); |
| 1339 |
if (ret) { |
| 1340 |
ORC_COMPILER_ERROR(p, "wrong immediate value %llx", val); |
| 1341 |
return; |
| 1342 |
} |
| 1343 |
|
| 1344 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", #0x%08x", (orc_uint32) val); |
| 1345 |
|
| 1346 |
code = arm64_code_logical_imm (bits, opcode, imm, Rn, Rd); |
| 1347 |
break; |
| 1348 |
case ORC_ARM64_TYPE_REG: /** shifted register */ |
| 1349 |
/**, <Rm>, shift #amount */ |
| 1350 |
shift = opt; |
| 1351 |
imm = (orc_uint32) val; |
| 1352 |
|
| 1353 |
if (shift >= sizeof(shift_names)/sizeof(shift_names[0])) { |
| 1354 |
ORC_COMPILER_ERROR(p, "unsupported shift %d", shift); |
| 1355 |
return; |
| 1356 |
} |
| 1357 |
|
| 1358 |
if (val > 0) { |
| 1359 |
if (val > 63) { |
| 1360 |
ORC_COMPILER_ERROR(p, "shift is out-of-range %llx", val); |
| 1361 |
return; |
| 1362 |
} |
| 1363 |
|
| 1364 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s, %s #%u", |
| 1365 |
orc_arm64_reg_name (Rm, bits), shift_names[shift], imm); |
| 1366 |
} else |
| 1367 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s", orc_arm64_reg_name (Rm, bits)); |
| 1368 |
|
| 1369 |
code = arm64_code_logical_reg (bits, opcode, shift, Rm, imm, Rn, Rd); |
| 1370 |
break; |
| 1371 |
default: |
| 1372 |
ORC_COMPILER_ERROR(p, "unknown data processing type %d", type); |
| 1373 |
return; |
| 1374 |
} |
| 1375 |
|
| 1376 |
/** it's preferred to use alias names if exists */ |
| 1377 |
if (alias_rd) { |
| 1378 |
ORC_ASM_CODE(p, " %s %s%s\n", insn_alias[opcode], |
| 1379 |
orc_arm64_reg_name(Rn, bits), opt_rm); |
| 1380 |
} else if (alias_rn) { |
| 1381 |
ORC_ASM_CODE(p, " %s %s%s\n", insn_alias[opcode], |
| 1382 |
orc_arm64_reg_name(Rd, bits), opt_rm); |
| 1383 |
} else { |
| 1384 |
ORC_ASM_CODE(p, " %s %s, %s%s\n", insn_names[opcode], |
| 1385 |
orc_arm64_reg_name(Rd, bits), orc_arm64_reg_name(Rn, bits), opt_rm); |
| 1386 |
} |
| 1387 |
|
| 1388 |
orc_arm_emit (p, code); |
| 1389 |
} |
| 1390 |
|
| 1391 |
/** data processing instructions: move wide |
| 1392 |
* |
| 1393 |
* General formats |
| 1394 |
* 3 2 1 |
| 1395 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1396 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1397 |
* |b|op |1 0 0 1 0 1|hw | imm16 | Rd | |
| 1398 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1399 |
*/ |
| 1400 |
|
| 1401 |
#define arm64_code_mov_wide(b,op,hw,imm,Rd) (0x12800000 | ((((b)==64)&0x1)<<31) | \ |
| 1402 |
(((op)&0x3)<<29) | (((hw)&0x3)<<21) | (((imm)&0xffff)<<5) | ((Rd)&0x1f)) |
| 1403 |
|
| 1404 |
void |
| 1405 |
orc_arm64_emit_mov_wide (OrcCompiler *p, OrcArm64RegBits bits, int mov_op, int hw, |
| 1406 |
int Rd, orc_uint64 val) |
| 1407 |
{ |
| 1408 |
orc_uint16 imm; |
| 1409 |
orc_uint32 code; |
| 1410 |
|
| 1411 |
static const char *mov_names[] = { |
| 1412 |
"movn", "ERROR", "movz", "movk" |
| 1413 |
}; |
| 1414 |
char opt_lsl[ARM64_MAX_OP_LEN]; |
| 1415 |
|
| 1416 |
if (mov_op >= sizeof(mov_names)/sizeof(mov_names[0])) { |
| 1417 |
ORC_COMPILER_ERROR(p, "unsupported mov opcode %d", mov_op); |
| 1418 |
return; |
| 1419 |
} |
| 1420 |
|
| 1421 |
if (val > 65535) { |
| 1422 |
ORC_COMPILER_ERROR(p, "unsupported amount of shift %llu", val); |
| 1423 |
return; |
| 1424 |
} |
| 1425 |
|
| 1426 |
if (bits == ORC_ARM64_REG_64) { |
| 1427 |
if (!(hw == 0 || hw == 16 || hw == 32 || hw == 48)) { |
| 1428 |
ORC_COMPILER_ERROR(p, "unsupported hw shift %d", hw); |
| 1429 |
return; |
| 1430 |
} |
| 1431 |
} else { |
| 1432 |
if (!(hw == 0 || hw == 16)) { |
| 1433 |
ORC_COMPILER_ERROR(p, "unsupported hw shift %d", hw); |
| 1434 |
return; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
memset (opt_lsl, '\x00', ARM64_MAX_OP_LEN); |
| 1439 |
if (hw > 0) { |
| 1440 |
snprintf (opt_lsl, ARM64_MAX_OP_LEN, ", lsl #%d", hw); |
| 1441 |
} |
| 1442 |
|
| 1443 |
hw /= 16; |
| 1444 |
imm = val; |
| 1445 |
code = arm64_code_mov_wide(bits, mov_op, hw, imm, Rd); |
| 1446 |
|
| 1447 |
ORC_ASM_CODE(p, " %s %s, #%u%s\n", |
| 1448 |
/** movz has one alias condition */ |
| 1449 |
(mov_op == 2) && !(imm == 0 && hw != 0) ? "mov" : mov_names[mov_op], |
| 1450 |
orc_arm64_reg_name(Rd, bits), |
| 1451 |
imm, opt_lsl); |
| 1452 |
|
| 1453 |
orc_arm_emit (p, code); |
| 1454 |
} |
| 1455 |
|
| 1456 |
/** data processing instructions: Shift (reg) |
| 1457 |
* |
| 1458 |
* General formats |
| 1459 |
* 3 2 1 |
| 1460 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1461 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1462 |
* |b|0 0|1 1 0 1 0 1 1 0| Rm |0 0 1 0|sft| Rn | Rd | |
| 1463 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1464 |
*/ |
| 1465 |
|
| 1466 |
#define arm64_code_shift(b,Rm,sft,Rn,Rd) (0x1ac02000 | ((((b)==64)&0x1)<<31) | \ |
| 1467 |
(((Rm)&0x1f)<<16) | (((sft)&0x3)<<10) | (((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1468 |
|
| 1469 |
void |
| 1470 |
orc_arm64_emit_sft (OrcCompiler *p, OrcArm64RegBits bits, OrcArmShift shift, |
| 1471 |
int Rd, int Rn, int Rm) |
| 1472 |
{ |
| 1473 |
orc_uint32 code; |
| 1474 |
|
| 1475 |
static const char *shift_names[] = { |
| 1476 |
"lsl", "lsr", "asr", "ror" |
| 1477 |
}; |
| 1478 |
|
| 1479 |
if (shift >= sizeof(shift_names)/sizeof(shift_names[0])) { |
| 1480 |
ORC_COMPILER_ERROR(p, "unsupported shift %d", shift); |
| 1481 |
return; |
| 1482 |
} |
| 1483 |
|
| 1484 |
code = arm64_code_shift(bits, Rm, shift, Rn, Rd); |
| 1485 |
|
| 1486 |
ORC_ASM_CODE(p, " %s %s, %s, %s\n", |
| 1487 |
shift_names[shift], |
| 1488 |
orc_arm64_reg_name(Rd, bits), |
| 1489 |
orc_arm64_reg_name(Rn, bits), |
| 1490 |
orc_arm64_reg_name(Rm, bits)); |
| 1491 |
|
| 1492 |
orc_arm_emit (p, code); |
| 1493 |
} |
| 1494 |
|
| 1495 |
/** data processing instructions: Bitfield Move |
| 1496 |
* |
| 1497 |
* General formats |
| 1498 |
* 3 2 1 |
| 1499 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1500 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1501 |
* |b|op |1 0 0 1 1 0|N| immr | imms | Rn | Rd | |
| 1502 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1503 |
* |
| 1504 |
* Note that Bitfiled Move is usually accessed via one of its aliases |
| 1505 |
*/ |
| 1506 |
|
| 1507 |
#define arm64_code_bfm(b,opcode,immr,imms,Rn,Rd) (0x13000000 | ((((b)==64)&0x1)<<31) | \ |
| 1508 |
(((opcode)&0x3)<<29) | ((((b)==64)&0x1)<<22) | (((immr)&0x3f)<<16) | (((imms)&0x3f)<<10) | \ |
| 1509 |
(((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1510 |
|
| 1511 |
/** Return 1 if ubfx or sbfx is preferred. Must exclude more specific aliases */ |
| 1512 |
static int bfx_preferred (OrcArm64RegBits bits, int is_unsigned, |
| 1513 |
orc_uint32 imms, orc_uint32 immr) |
| 1514 |
{ |
| 1515 |
if (imms < immr) |
| 1516 |
return 0; |
| 1517 |
|
| 1518 |
/** must not match LSR/ASR/LSL alias */ |
| 1519 |
if (imms == 0x1f) |
| 1520 |
return 0; |
| 1521 |
|
| 1522 |
/** must not match UXTx/SXTx alias */ |
| 1523 |
if (immr == 0) { |
| 1524 |
if (bits == ORC_ARM64_REG_32) { |
| 1525 |
if (imms == 0x7 || imms == 0xf) |
| 1526 |
return 0; |
| 1527 |
} else if (is_unsigned) { |
| 1528 |
if (imms == 0x7 || imms == 0xf || imms == 0x1f) |
| 1529 |
return 0; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
return 1; |
| 1534 |
} |
| 1535 |
|
| 1536 |
void |
| 1537 |
orc_arm64_emit_bfm (OrcCompiler *p, OrcArm64RegBits bits, OrcArm64DP opcode, |
| 1538 |
int Rd, int Rn, orc_uint32 immr, orc_uint32 imms) |
| 1539 |
{ |
| 1540 |
orc_uint32 code; |
| 1541 |
int alias = -1; |
| 1542 |
|
| 1543 |
static const char *insn_names[3] = { |
| 1544 |
"sbfm", "bfm", "ubfm" |
| 1545 |
}; |
| 1546 |
static const char *insn_alias[3][6] = { |
| 1547 |
{"asr","sbfiz","sbfx","sxtb","sxth","sxtw"}, |
| 1548 |
{"bfc","bfi","bfxil", "ERROR","ERROR","ERROR"}, |
| 1549 |
{"lsl","lsr","ubfiz","ubfx","uxtb","uxth"} |
| 1550 |
}; |
| 1551 |
|
| 1552 |
char opt_immr[ARM64_MAX_OP_LEN]; |
| 1553 |
char opt_imms[ARM64_MAX_OP_LEN]; |
| 1554 |
|
| 1555 |
memset (opt_immr, '\x00', ARM64_MAX_OP_LEN); |
| 1556 |
memset (opt_imms, '\x00', ARM64_MAX_OP_LEN); |
| 1557 |
|
| 1558 |
/** find its alias */ |
| 1559 |
switch (opcode) { |
| 1560 |
case ORC_ARM64_DP_SBFM: |
| 1561 |
if ((imms == 0x1f && bits == ORC_ARM64_REG_32) || |
| 1562 |
(imms == 0x3f && bits == ORC_ARM64_REG_64)) { |
| 1563 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1564 |
alias = 0; |
| 1565 |
} else if (imms < immr) { |
| 1566 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1567 |
snprintf (opt_imms, ARM64_MAX_OP_LEN, ", #%u", imms); |
| 1568 |
alias = 1; |
| 1569 |
} else if (bfx_preferred(bits, 0, imms, immr)) { |
| 1570 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1571 |
snprintf (opt_imms, ARM64_MAX_OP_LEN, ", #%u", imms); |
| 1572 |
alias = 2; |
| 1573 |
} else if (immr == 0) { |
| 1574 |
if (imms == 0x7) |
| 1575 |
alias = 3; |
| 1576 |
else if (imms == 0xf) |
| 1577 |
alias = 4; |
| 1578 |
else if (imms == 0x1f) |
| 1579 |
alias = 5; |
| 1580 |
} |
| 1581 |
break; |
| 1582 |
case ORC_ARM64_DP_BFM: |
| 1583 |
if (imms < immr) { |
| 1584 |
if (Rn == 0x1f) { |
| 1585 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1586 |
alias = 0; |
| 1587 |
} else { |
| 1588 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1589 |
snprintf (opt_imms, ARM64_MAX_OP_LEN, ", #%u", imms); |
| 1590 |
alias = 1; |
| 1591 |
} |
| 1592 |
} else { |
| 1593 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1594 |
snprintf (opt_imms, ARM64_MAX_OP_LEN, ", #%u", imms); |
| 1595 |
alias = 2; |
| 1596 |
} |
| 1597 |
break; |
| 1598 |
case ORC_ARM64_DP_UBFM: |
| 1599 |
if ((imms == 0x1f && bits == ORC_ARM64_REG_32) || |
| 1600 |
(imms == 0x3f && bits == ORC_ARM64_REG_64)) { |
| 1601 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1602 |
alias = 1; |
| 1603 |
} else if (imms + 1 == immr) { |
| 1604 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1605 |
alias = 0; |
| 1606 |
} else if (imms < immr) { |
| 1607 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1608 |
snprintf (opt_imms, ARM64_MAX_OP_LEN, ", #%u", imms); |
| 1609 |
alias = 2; |
| 1610 |
} else if (bfx_preferred(bits, 1, imms, immr)) { |
| 1611 |
snprintf (opt_immr, ARM64_MAX_OP_LEN, ", #%u", immr); |
| 1612 |
snprintf (opt_imms, ARM64_MAX_OP_LEN, ", #%u", imms); |
| 1613 |
alias = 3; |
| 1614 |
} else if (immr == 0) { |
| 1615 |
if (imms == 0x7) |
| 1616 |
alias = 4; |
| 1617 |
else if (imms == 0xf) |
| 1618 |
alias = 5; |
| 1619 |
} |
| 1620 |
break; |
| 1621 |
default: |
| 1622 |
ORC_COMPILER_ERROR(p, "unknown opcode %d", opcode); |
| 1623 |
return; |
| 1624 |
} |
| 1625 |
|
| 1626 |
opcode -= ORC_ARM64_DP_SBFM; |
| 1627 |
|
| 1628 |
code = arm64_code_bfm(bits, opcode, immr, imms, Rn, Rd); |
| 1629 |
|
| 1630 |
if (alias != -1) { |
| 1631 |
ORC_ASM_CODE(p, " %s %s, %s%s%s\n", |
| 1632 |
insn_alias[opcode][alias], |
| 1633 |
orc_arm64_reg_name(Rd, bits), |
| 1634 |
orc_arm64_reg_name(Rn, bits), |
| 1635 |
opt_immr, opt_imms); |
| 1636 |
} else { |
| 1637 |
ORC_ASM_CODE(p, " %s %s, %s, #%u, #%u\n", |
| 1638 |
insn_names[opcode], |
| 1639 |
orc_arm64_reg_name(Rd, bits), |
| 1640 |
orc_arm64_reg_name(Rn, bits), |
| 1641 |
immr, imms); |
| 1642 |
} |
| 1643 |
|
| 1644 |
orc_arm_emit (p, code); |
| 1645 |
} |
| 1646 |
|
| 1647 |
/** data processing instructions: Extract, alias of ROR (imm) |
| 1648 |
* |
| 1649 |
* 3 2 1 |
| 1650 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1651 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1652 |
* |b|0 0|1 0 0 1 1 1|N|0| Rm | imms | Rn | Rd | |
| 1653 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1654 |
*/ |
| 1655 |
|
| 1656 |
#define arm64_code_extr(b,Rm,imms,Rn,Rd) (0x13800000 | ((((b)==64)&0x1)<<31) | \ |
| 1657 |
((((b)==64)&0x1)<<22) | (((Rm)&0x1f)<<16) | (((imms)&0x3f)<<10) | \ |
| 1658 |
(((Rn)&0x1f)<<5) | ((Rd)&0x1f)) |
| 1659 |
|
| 1660 |
void |
| 1661 |
orc_arm64_emit_extr (OrcCompiler *p, OrcArm64RegBits bits, |
| 1662 |
int Rd, int Rn, int Rm, orc_uint32 imm) |
| 1663 |
{ |
| 1664 |
orc_uint32 code; |
| 1665 |
|
| 1666 |
code = arm64_code_extr (bits, Rm, imm, Rn, Rd); |
| 1667 |
|
| 1668 |
if (Rn == Rm) { /** ROR (imm) */ |
| 1669 |
if (bits == ORC_ARM64_REG_32 && (imm & 0x20)) { |
| 1670 |
ORC_COMPILER_ERROR(p, "invalid immediate value 0x%08x", imm); |
| 1671 |
return; |
| 1672 |
} |
| 1673 |
|
| 1674 |
ORC_ASM_CODE(p, " ror %s, %s, #%u\n", |
| 1675 |
orc_arm64_reg_name(Rd, bits), |
| 1676 |
orc_arm64_reg_name(Rn, bits), |
| 1677 |
imm); |
| 1678 |
} else { |
| 1679 |
ORC_ASM_CODE(p, " extr %s, %s, %s, #%u\n", |
| 1680 |
orc_arm64_reg_name(Rd, bits), |
| 1681 |
orc_arm64_reg_name(Rn, bits), |
| 1682 |
orc_arm64_reg_name(Rm, bits), |
| 1683 |
imm); |
| 1684 |
} |
| 1685 |
|
| 1686 |
orc_arm_emit (p, code); |
| 1687 |
} |
| 1688 |
|
| 1689 |
/** memory access instructions |
| 1690 |
* |
| 1691 |
* Literal (LDR only) |
| 1692 |
* 3 2 1 |
| 1693 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1694 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1695 |
* |0 b|0 1 1|0|0 0| imm19 | Rt | |
| 1696 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1697 |
* |
| 1698 |
* Register Signed offset (Post-/Pre-index) |
| 1699 |
* 3 2 1 |
| 1700 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1701 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1702 |
* |1 b|1 1 1|0|0 0|op |0| imm9 |P 1| Rn | Rt | |
| 1703 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1704 |
* |
| 1705 |
* Register Unsigned offset |
| 1706 |
* 3 2 1 |
| 1707 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1708 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1709 |
* |1 b|1 1 1|0|0 1|op | imm12 | Rn | Rt | |
| 1710 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1711 |
* |
| 1712 |
* Register Extended |
| 1713 |
* 3 2 1 |
| 1714 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1715 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1716 |
* |1 b|1 1 1|0|0 0|op |1| Rm | opt |S|1 0| Rn | Rt | |
| 1717 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1718 |
* |
| 1719 |
*/ |
| 1720 |
|
| 1721 |
#define arm64_code_mem_literal(b,imm,Rt) (0x18000000 | ((((b)==64)&0x1)<<30) | \ |
| 1722 |
(((imm)&0x7ffff)<<5) | ((Rt)&0x1f)) |
| 1723 |
|
| 1724 |
#define arm64_code_mem_signed(b,op,imm,P,Rn,Rt) (0xb8000400 | ((((b)==64)&0x1)<<30) | \ |
| 1725 |
(((op)&0x3)<<22) | (((imm)&0x3f)<<12) | (((P)&0x1)<<11) | (((Rn)&0x1f)<<5) | ((Rt)&0x1f)) |
| 1726 |
|
| 1727 |
#define arm64_code_mem_unsigned(b,op,imm,Rn,Rt) (0xb9000000 | ((((b)==64)&0x1)<<30) | \ |
| 1728 |
(((op)&0x3)<<22) | (((imm)&0xfff)<<10) | (((Rn)&0x1f)<<5) | ((Rt)&0x1f)) |
| 1729 |
|
| 1730 |
#define arm64_code_mem_extended(b,op,Rm,opt,S,Rn,Rt) (0xb8200800 | ((((b)==64)&0x1)<<30) | \ |
| 1731 |
(((op)&0x3)<<22) | (((Rm)&0x1f)<<16) | (((opt)&0x7)<<13) | (((S)&0x1)<<12) | \ |
| 1732 |
(((Rn)&0x1f)<<5) | ((Rt)&0x1f)) |
| 1733 |
|
| 1734 |
void |
| 1735 |
orc_arm64_emit_mem (OrcCompiler *p, OrcArm64RegBits bits, OrcArm64Mem opcode, |
| 1736 |
OrcArm64Type type, int opt, int Rt, int Rn, int Rm, orc_uint32 val) |
| 1737 |
{ |
| 1738 |
orc_uint32 code; |
| 1739 |
orc_uint32 amount; |
| 1740 |
orc_int32 imm; |
| 1741 |
|
| 1742 |
int is_signed, extend, S; |
| 1743 |
|
| 1744 |
static const char *insn_names[3] = { |
| 1745 |
"str", "ldr" |
| 1746 |
}; |
| 1747 |
static const char *extend_names[] = { |
| 1748 |
"ERROR", "ERROR", "uxtw", "lsl", |
| 1749 |
"ERROR", "ERROR", "sxtw", "sxtx" |
| 1750 |
}; |
| 1751 |
|
| 1752 |
char opt_rn[ARM64_MAX_OP_LEN]; |
| 1753 |
char opt_rm[ARM64_MAX_OP_LEN]; |
| 1754 |
|
| 1755 |
if (opcode >= sizeof(insn_names)/sizeof(insn_names[0])) { |
| 1756 |
ORC_COMPILER_ERROR(p, "unsupported opcode %d", opcode); |
| 1757 |
return; |
| 1758 |
} |
| 1759 |
|
| 1760 |
memset (opt_rn, '\x00', ARM64_MAX_OP_LEN); |
| 1761 |
memset (opt_rm, '\x00', ARM64_MAX_OP_LEN); |
| 1762 |
|
| 1763 |
switch (type) { |
| 1764 |
case ORC_ARM64_TYPE_IMM: |
| 1765 |
if (opcode != ORC_ARM64_MEM_LDR) { |
| 1766 |
ORC_COMPILER_ERROR(p, "str with immediate is not permitted\n"); |
| 1767 |
return; |
| 1768 |
} |
| 1769 |
|
| 1770 |
if (bits == ORC_ARM64_REG_64) { |
| 1771 |
imm = val/8; |
| 1772 |
} else { |
| 1773 |
imm = val/4; |
| 1774 |
} |
| 1775 |
|
| 1776 |
if (imm > 4095) { |
| 1777 |
ORC_COMPILER_ERROR(p, "out-of-range immediate 0x%lx\n", val); |
| 1778 |
return; |
| 1779 |
} |
| 1780 |
|
| 1781 |
if (imm == 0) { |
| 1782 |
int label = opt; |
| 1783 |
/** resolve the actual address diff in fixup code */ |
| 1784 |
orc_arm_add_fixup (p, label, 2); |
| 1785 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", .L%d", label); |
| 1786 |
} else { |
| 1787 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", 0x%08x", val); |
| 1788 |
} |
| 1789 |
|
| 1790 |
code = arm64_code_mem_literal (bits, imm, Rt); |
| 1791 |
break; |
| 1792 |
case ORC_ARM64_TYPE_REG: |
| 1793 |
imm = val; |
| 1794 |
is_signed = opt; |
| 1795 |
|
| 1796 |
if (is_signed) { /** signed offset */ |
| 1797 |
if (imm > 0xff || imm < -0xff) { |
| 1798 |
ORC_COMPILER_ERROR(p, "simm is out-of-range %x\n", imm); |
| 1799 |
return; |
| 1800 |
} |
| 1801 |
|
| 1802 |
if (is_signed == 1) { /** pre index */ |
| 1803 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s", orc_arm64_reg_name(Rn, bits)); |
| 1804 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", #%d]!", imm); |
| 1805 |
|
| 1806 |
code = arm64_code_mem_signed (bits, opcode, imm, 1, Rn, Rt); |
| 1807 |
} else { /** post index */ |
| 1808 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s]", orc_arm64_reg_name(Rn, bits)); |
| 1809 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", #%d", imm); |
| 1810 |
|
| 1811 |
code = arm64_code_mem_signed (bits, opcode, imm, 0, Rn, Rt); |
| 1812 |
} |
| 1813 |
} else { /** unsigned offset */ |
| 1814 |
if (imm != 0) { |
| 1815 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s", orc_arm64_reg_name(Rn, bits)); |
| 1816 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", #%d]", imm); |
| 1817 |
} else { |
| 1818 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s]", orc_arm64_reg_name(Rn, bits)); |
| 1819 |
} |
| 1820 |
|
| 1821 |
if (bits == ORC_ARM64_REG_64) { |
| 1822 |
if (imm < 0 || imm > 0xfff * 8) |
| 1823 |
ORC_COMPILER_ERROR(p, "imm is Out-of-range %x\n", imm); |
| 1824 |
imm /= 8; |
| 1825 |
} else { |
| 1826 |
if (imm < 0 || imm > 0xfff * 4) |
| 1827 |
ORC_COMPILER_ERROR(p, "imm is Out-of-range %x\n", imm); |
| 1828 |
imm /= 4; |
| 1829 |
} |
| 1830 |
|
| 1831 |
code = arm64_code_mem_unsigned (bits, opcode, imm, Rn, Rt); |
| 1832 |
} |
| 1833 |
break; |
| 1834 |
case ORC_ARM64_TYPE_EXT: |
| 1835 |
extend = opt; |
| 1836 |
|
| 1837 |
if ((extend >= sizeof(extend_names)/sizeof(extend_names[0])) || |
| 1838 |
!strncmp (extend_names[extend], "ERROR", 5)) { |
| 1839 |
ORC_COMPILER_ERROR(p, "unsupported extend %d\n", extend); |
| 1840 |
return; |
| 1841 |
} |
| 1842 |
|
| 1843 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s", orc_arm64_reg_name(Rn, bits)); |
| 1844 |
|
| 1845 |
amount = val; |
| 1846 |
if (amount > 0) { |
| 1847 |
if (bits == ORC_ARM64_REG_64 && amount != 3) { |
| 1848 |
ORC_COMPILER_ERROR(p, "unsupported amount %d\n", amount); |
| 1849 |
return; |
| 1850 |
} |
| 1851 |
if (bits == ORC_ARM64_REG_32 && amount != 2) { |
| 1852 |
ORC_COMPILER_ERROR(p, "unsupported amount %d\n", amount); |
| 1853 |
return; |
| 1854 |
} |
| 1855 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s, %s #%u]", |
| 1856 |
orc_arm64_reg_name(Rm, bits), extend_names[extend], amount); |
| 1857 |
|
| 1858 |
S = 1; |
| 1859 |
} else { |
| 1860 |
if (extend == 3) { /** LSL */ |
| 1861 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s]", orc_arm64_reg_name(Rm, bits)); |
| 1862 |
} else { |
| 1863 |
snprintf (opt_rm, ARM64_MAX_OP_LEN, ", %s, %s]", |
| 1864 |
orc_arm64_reg_name(Rm, bits), extend_names[extend]); |
| 1865 |
} |
| 1866 |
|
| 1867 |
S = 0; |
| 1868 |
} |
| 1869 |
|
| 1870 |
code = arm64_code_mem_extended (bits, opcode, Rm, extend, S, Rn, Rt); |
| 1871 |
break; |
| 1872 |
default: |
| 1873 |
ORC_COMPILER_ERROR(p, "unsupported type %d\n", type); |
| 1874 |
return; |
| 1875 |
} |
| 1876 |
|
| 1877 |
ORC_ASM_CODE(p, " %s %s%s%s\n", |
| 1878 |
insn_names[opcode], |
| 1879 |
orc_arm64_reg_name(Rt, bits), |
| 1880 |
opt_rn, opt_rm); |
| 1881 |
|
| 1882 |
orc_arm_emit (p, code); |
| 1883 |
} |
| 1884 |
|
| 1885 |
/** memory access instructions (pair) |
| 1886 |
* |
| 1887 |
* 3 2 1 |
| 1888 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 1889 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1890 |
* |b 0|1 0 1|0| opt |L| imm7 | Rt2 | Rn | Rt | |
| 1891 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1892 |
*/ |
| 1893 |
|
| 1894 |
#define arm64_code_mem_pair(b,opt,L,imm,Rt2,Rn,Rt) (0x40000000 | ((((b)==64)&0x1)<<31) | \ |
| 1895 |
(((opt)&0x7)<<23) | (((L)&0x1)<<22) | (((imm)&0x7f)<<15) | (((Rt2)&0x1f)<<10) | \ |
| 1896 |
(((Rn)&0x1f)<<5) | ((Rt)&0x1f)) |
| 1897 |
|
| 1898 |
void |
| 1899 |
orc_arm64_emit_mem_pair (OrcCompiler *p, OrcArm64RegBits bits, OrcArm64Mem opcode, |
| 1900 |
int opt, int Rt, int Rt2, int Rn, orc_int32 imm) |
| 1901 |
{ |
| 1902 |
orc_uint32 code; |
| 1903 |
|
| 1904 |
static const char *insn_names[3] = { |
| 1905 |
"stp", "ldp" |
| 1906 |
}; |
| 1907 |
|
| 1908 |
char opt_rn[ARM64_MAX_OP_LEN]; |
| 1909 |
|
| 1910 |
if (opcode >= sizeof(insn_names)/sizeof(insn_names[0])) { |
| 1911 |
ORC_COMPILER_ERROR(p, "unsupported opcode %d", opcode); |
| 1912 |
return; |
| 1913 |
} |
| 1914 |
|
| 1915 |
memset (opt_rn, '\x00', ARM64_MAX_OP_LEN); |
| 1916 |
|
| 1917 |
switch (opt) { |
| 1918 |
case 1: /** post-index */ |
| 1919 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s], #%d", orc_arm64_reg_name(Rn, bits), imm); |
| 1920 |
break; |
| 1921 |
case 2: /** signed offset */ |
| 1922 |
if (imm) { |
| 1923 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s, #%d]", orc_arm64_reg_name(Rn, bits), imm); |
| 1924 |
} else { |
| 1925 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s]", orc_arm64_reg_name(Rn, bits)); |
| 1926 |
} |
| 1927 |
break; |
| 1928 |
case 3: /** pre-index */ |
| 1929 |
snprintf (opt_rn, ARM64_MAX_OP_LEN, ", [%s, #%d]!", orc_arm64_reg_name(Rn, bits), imm); |
| 1930 |
break; |
| 1931 |
default: |
| 1932 |
ORC_COMPILER_ERROR(p, "unsupported variant %d\n", opt); |
| 1933 |
return; |
| 1934 |
} |
| 1935 |
|
| 1936 |
if (bits == ORC_ARM64_REG_64) { |
| 1937 |
imm /= 8; |
| 1938 |
} else { |
| 1939 |
imm /= 4; |
| 1940 |
} |
| 1941 |
|
| 1942 |
if (imm < -64 || imm > 63) { |
| 1943 |
ORC_COMPILER_ERROR(p, "imm is Out-of-range\n"); |
| 1944 |
return; |
| 1945 |
} |
| 1946 |
|
| 1947 |
code = arm64_code_mem_pair (bits, opt, opcode, imm, Rt2, Rn, Rt); |
| 1948 |
|
| 1949 |
ORC_ASM_CODE(p, " %s %s, %s%s\n", |
| 1950 |
insn_names[opcode], |
| 1951 |
orc_arm64_reg_name(Rt, bits), |
| 1952 |
orc_arm64_reg_name(Rt2, bits), |
| 1953 |
opt_rn); |
| 1954 |
|
| 1955 |
orc_arm_emit (p, code); |
| 1956 |
} |
| 1957 |
|
| 1958 |
/** Return from subroutine */ |
| 1959 |
|
| 1960 |
void |
| 1961 |
orc_arm64_emit_ret (OrcCompiler *p, int Rn) |
| 1962 |
{ |
| 1963 |
orc_uint32 code; |
| 1964 |
|
| 1965 |
code = 0xd65f0000 | ((Rn & 0x1f) << 5); |
| 1966 |
|
| 1967 |
ORC_ASM_CODE (p, " ret %s\n", |
| 1968 |
Rn == ORC_ARM64_LR ? "" : orc_arm64_reg_name (Rn, ORC_ARM64_REG_64)); |
| 1969 |
|
| 1970 |
orc_arm_emit (p, code); |
| 1971 |
} |