1/* 2 * Tiny Code Generator for QEMU 3 * 4 * Copyright (c) 2008 Andrzej Zaborowski 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25#include "elf.h" 26 27int arm_arch = __ARM_ARCH; 28 29#ifndef use_idiv_instructions 30bool use_idiv_instructions; 31#endif 32#ifndef use_neon_instructions 33bool use_neon_instructions; 34#endif 35 36/* Used for function call generation. */ 37#define TCG_TARGET_STACK_ALIGN 8 38#define TCG_TARGET_CALL_STACK_OFFSET 0 39#define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_NORMAL 40#define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_EVEN 41#define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_EVEN 42#define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_BY_REF 43 44#ifdef CONFIG_DEBUG_TCG 45static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { 46 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7", 47 "%r8", "%r9", "%r10", "%r11", "%r12", "%sp", "%r14", "%pc", 48 "%q0", "%q1", "%q2", "%q3", "%q4", "%q5", "%q6", "%q7", 49 "%q8", "%q9", "%q10", "%q11", "%q12", "%q13", "%q14", "%q15", 50}; 51#endif 52 53static const int tcg_target_reg_alloc_order[] = { 54 TCG_REG_R4, 55 TCG_REG_R5, 56 TCG_REG_R6, 57 TCG_REG_R7, 58 TCG_REG_R8, 59 TCG_REG_R9, 60 TCG_REG_R10, 61 TCG_REG_R11, 62 TCG_REG_R13, 63 TCG_REG_R0, 64 TCG_REG_R1, 65 TCG_REG_R2, 66 TCG_REG_R3, 67 TCG_REG_R12, 68 TCG_REG_R14, 69 70 TCG_REG_Q0, 71 TCG_REG_Q1, 72 TCG_REG_Q2, 73 TCG_REG_Q3, 74 /* Q4 - Q7 are call-saved, and skipped. */ 75 TCG_REG_Q8, 76 TCG_REG_Q9, 77 TCG_REG_Q10, 78 TCG_REG_Q11, 79 TCG_REG_Q12, 80 TCG_REG_Q13, 81 TCG_REG_Q14, 82 TCG_REG_Q15, 83}; 84 85static const int tcg_target_call_iarg_regs[4] = { 86 TCG_REG_R0, TCG_REG_R1, TCG_REG_R2, TCG_REG_R3 87}; 88 89static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) 90{ 91 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL); 92 tcg_debug_assert(slot >= 0 && slot <= 3); 93 return TCG_REG_R0 + slot; 94} 95 96#define TCG_REG_TMP TCG_REG_R12 97#define TCG_VEC_TMP TCG_REG_Q15 98#define TCG_REG_GUEST_BASE TCG_REG_R11 99 100typedef enum { 101 COND_EQ = 0x0, 102 COND_NE = 0x1, 103 COND_CS = 0x2, /* Unsigned greater or equal */ 104 COND_CC = 0x3, /* Unsigned less than */ 105 COND_MI = 0x4, /* Negative */ 106 COND_PL = 0x5, /* Zero or greater */ 107 COND_VS = 0x6, /* Overflow */ 108 COND_VC = 0x7, /* No overflow */ 109 COND_HI = 0x8, /* Unsigned greater than */ 110 COND_LS = 0x9, /* Unsigned less or equal */ 111 COND_GE = 0xa, 112 COND_LT = 0xb, 113 COND_GT = 0xc, 114 COND_LE = 0xd, 115 COND_AL = 0xe, 116} ARMCond; 117 118#define TO_CPSR (1 << 20) 119 120#define SHIFT_IMM_LSL(im) (((im) << 7) | 0x00) 121#define SHIFT_IMM_LSR(im) (((im) << 7) | 0x20) 122#define SHIFT_IMM_ASR(im) (((im) << 7) | 0x40) 123#define SHIFT_IMM_ROR(im) (((im) << 7) | 0x60) 124#define SHIFT_REG_LSL(rs) (((rs) << 8) | 0x10) 125#define SHIFT_REG_LSR(rs) (((rs) << 8) | 0x30) 126#define SHIFT_REG_ASR(rs) (((rs) << 8) | 0x50) 127#define SHIFT_REG_ROR(rs) (((rs) << 8) | 0x70) 128 129typedef enum { 130 ARITH_AND = 0x0 << 21, 131 ARITH_EOR = 0x1 << 21, 132 ARITH_SUB = 0x2 << 21, 133 ARITH_RSB = 0x3 << 21, 134 ARITH_ADD = 0x4 << 21, 135 ARITH_ADC = 0x5 << 21, 136 ARITH_SBC = 0x6 << 21, 137 ARITH_RSC = 0x7 << 21, 138 ARITH_TST = 0x8 << 21 | TO_CPSR, 139 ARITH_CMP = 0xa << 21 | TO_CPSR, 140 ARITH_CMN = 0xb << 21 | TO_CPSR, 141 ARITH_ORR = 0xc << 21, 142 ARITH_MOV = 0xd << 21, 143 ARITH_BIC = 0xe << 21, 144 ARITH_MVN = 0xf << 21, 145 146 INSN_B = 0x0a000000, 147 148 INSN_CLZ = 0x016f0f10, 149 INSN_RBIT = 0x06ff0f30, 150 151 INSN_LDMIA = 0x08b00000, 152 INSN_STMDB = 0x09200000, 153 154 INSN_LDR_IMM = 0x04100000, 155 INSN_LDR_REG = 0x06100000, 156 INSN_STR_IMM = 0x04000000, 157 INSN_STR_REG = 0x06000000, 158 159 INSN_LDRH_IMM = 0x005000b0, 160 INSN_LDRH_REG = 0x001000b0, 161 INSN_LDRSH_IMM = 0x005000f0, 162 INSN_LDRSH_REG = 0x001000f0, 163 INSN_STRH_IMM = 0x004000b0, 164 INSN_STRH_REG = 0x000000b0, 165 166 INSN_LDRB_IMM = 0x04500000, 167 INSN_LDRB_REG = 0x06500000, 168 INSN_LDRSB_IMM = 0x005000d0, 169 INSN_LDRSB_REG = 0x001000d0, 170 INSN_STRB_IMM = 0x04400000, 171 INSN_STRB_REG = 0x06400000, 172 173 INSN_LDRD_IMM = 0x004000d0, 174 INSN_LDRD_REG = 0x000000d0, 175 INSN_STRD_IMM = 0x004000f0, 176 INSN_STRD_REG = 0x000000f0, 177 178 INSN_DMB_ISH = 0xf57ff05b, 179 INSN_DMB_MCR = 0xee070fba, 180 181 INSN_MSRI_CPSR = 0x0360f000, 182 183 /* Architected nop introduced in v6k. */ 184 /* ??? This is an MSR (imm) 0,0,0 insn. Anyone know if this 185 also Just So Happened to do nothing on pre-v6k so that we 186 don't need to conditionalize it? */ 187 INSN_NOP_v6k = 0xe320f000, 188 /* Otherwise the assembler uses mov r0,r0 */ 189 INSN_NOP_v4 = (COND_AL << 28) | ARITH_MOV, 190 191 INSN_VADD = 0xf2000800, 192 INSN_VAND = 0xf2000110, 193 INSN_VBIC = 0xf2100110, 194 INSN_VEOR = 0xf3000110, 195 INSN_VORN = 0xf2300110, 196 INSN_VORR = 0xf2200110, 197 INSN_VSUB = 0xf3000800, 198 INSN_VMUL = 0xf2000910, 199 INSN_VQADD = 0xf2000010, 200 INSN_VQADD_U = 0xf3000010, 201 INSN_VQSUB = 0xf2000210, 202 INSN_VQSUB_U = 0xf3000210, 203 INSN_VMAX = 0xf2000600, 204 INSN_VMAX_U = 0xf3000600, 205 INSN_VMIN = 0xf2000610, 206 INSN_VMIN_U = 0xf3000610, 207 208 INSN_VABS = 0xf3b10300, 209 INSN_VMVN = 0xf3b00580, 210 INSN_VNEG = 0xf3b10380, 211 212 INSN_VCEQ0 = 0xf3b10100, 213 INSN_VCGT0 = 0xf3b10000, 214 INSN_VCGE0 = 0xf3b10080, 215 INSN_VCLE0 = 0xf3b10180, 216 INSN_VCLT0 = 0xf3b10200, 217 218 INSN_VCEQ = 0xf3000810, 219 INSN_VCGE = 0xf2000310, 220 INSN_VCGT = 0xf2000300, 221 INSN_VCGE_U = 0xf3000310, 222 INSN_VCGT_U = 0xf3000300, 223 224 INSN_VSHLI = 0xf2800510, /* VSHL (immediate) */ 225 INSN_VSARI = 0xf2800010, /* VSHR.S */ 226 INSN_VSHRI = 0xf3800010, /* VSHR.U */ 227 INSN_VSLI = 0xf3800510, 228 INSN_VSHL_S = 0xf2000400, /* VSHL.S (register) */ 229 INSN_VSHL_U = 0xf3000400, /* VSHL.U (register) */ 230 231 INSN_VBSL = 0xf3100110, 232 INSN_VBIT = 0xf3200110, 233 INSN_VBIF = 0xf3300110, 234 235 INSN_VTST = 0xf2000810, 236 237 INSN_VDUP_G = 0xee800b10, /* VDUP (ARM core register) */ 238 INSN_VDUP_S = 0xf3b00c00, /* VDUP (scalar) */ 239 INSN_VLDR_D = 0xed100b00, /* VLDR.64 */ 240 INSN_VLD1 = 0xf4200000, /* VLD1 (multiple single elements) */ 241 INSN_VLD1R = 0xf4a00c00, /* VLD1 (single element to all lanes) */ 242 INSN_VST1 = 0xf4000000, /* VST1 (multiple single elements) */ 243 INSN_VMOVI = 0xf2800010, /* VMOV (immediate) */ 244} ARMInsn; 245 246#define INSN_NOP (use_armv7_instructions ? INSN_NOP_v6k : INSN_NOP_v4) 247 248static const uint8_t tcg_cond_to_arm_cond[] = { 249 [TCG_COND_EQ] = COND_EQ, 250 [TCG_COND_NE] = COND_NE, 251 [TCG_COND_LT] = COND_LT, 252 [TCG_COND_GE] = COND_GE, 253 [TCG_COND_LE] = COND_LE, 254 [TCG_COND_GT] = COND_GT, 255 /* unsigned */ 256 [TCG_COND_LTU] = COND_CC, 257 [TCG_COND_GEU] = COND_CS, 258 [TCG_COND_LEU] = COND_LS, 259 [TCG_COND_GTU] = COND_HI, 260}; 261 262static int encode_imm(uint32_t imm); 263 264/* TCG private relocation type: add with pc+imm8 */ 265#define R_ARM_PC8 11 266 267/* TCG private relocation type: vldr with imm8 << 2 */ 268#define R_ARM_PC11 12 269 270static bool reloc_pc24(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 271{ 272 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 273 ptrdiff_t offset = (tcg_ptr_byte_diff(target, src_rx) - 8) >> 2; 274 275 if (offset == sextract32(offset, 0, 24)) { 276 *src_rw = deposit32(*src_rw, 0, 24, offset); 277 return true; 278 } 279 return false; 280} 281 282static bool reloc_pc13(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 283{ 284 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 285 ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8; 286 287 if (offset >= -0xfff && offset <= 0xfff) { 288 tcg_insn_unit insn = *src_rw; 289 bool u = (offset >= 0); 290 if (!u) { 291 offset = -offset; 292 } 293 insn = deposit32(insn, 23, 1, u); 294 insn = deposit32(insn, 0, 12, offset); 295 *src_rw = insn; 296 return true; 297 } 298 return false; 299} 300 301static bool reloc_pc11(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 302{ 303 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 304 ptrdiff_t offset = (tcg_ptr_byte_diff(target, src_rx) - 8) / 4; 305 306 if (offset >= -0xff && offset <= 0xff) { 307 tcg_insn_unit insn = *src_rw; 308 bool u = (offset >= 0); 309 if (!u) { 310 offset = -offset; 311 } 312 insn = deposit32(insn, 23, 1, u); 313 insn = deposit32(insn, 0, 8, offset); 314 *src_rw = insn; 315 return true; 316 } 317 return false; 318} 319 320static bool reloc_pc8(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 321{ 322 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 323 ptrdiff_t offset = tcg_ptr_byte_diff(target, src_rx) - 8; 324 int imm12 = encode_imm(offset); 325 326 if (imm12 >= 0) { 327 *src_rw = deposit32(*src_rw, 0, 12, imm12); 328 return true; 329 } 330 return false; 331} 332 333static bool patch_reloc(tcg_insn_unit *code_ptr, int type, 334 intptr_t value, intptr_t addend) 335{ 336 tcg_debug_assert(addend == 0); 337 switch (type) { 338 case R_ARM_PC24: 339 return reloc_pc24(code_ptr, (const tcg_insn_unit *)value); 340 case R_ARM_PC13: 341 return reloc_pc13(code_ptr, (const tcg_insn_unit *)value); 342 case R_ARM_PC11: 343 return reloc_pc11(code_ptr, (const tcg_insn_unit *)value); 344 case R_ARM_PC8: 345 return reloc_pc8(code_ptr, (const tcg_insn_unit *)value); 346 default: 347 g_assert_not_reached(); 348 } 349} 350 351#define TCG_CT_CONST_ARM 0x100 352#define TCG_CT_CONST_INV 0x200 353#define TCG_CT_CONST_NEG 0x400 354#define TCG_CT_CONST_ZERO 0x800 355#define TCG_CT_CONST_ORRI 0x1000 356#define TCG_CT_CONST_ANDI 0x2000 357 358#define ALL_GENERAL_REGS 0xffffu 359#define ALL_VECTOR_REGS 0xffff0000u 360 361/* 362 * r0-r3 will be overwritten when reading the tlb entry (system-mode only); 363 * r14 will be overwritten by the BLNE branching to the slow path. 364 */ 365#define ALL_QLDST_REGS \ 366 (ALL_GENERAL_REGS & ~((tcg_use_softmmu ? 0xf : 0) | (1 << TCG_REG_R14))) 367 368/* 369 * ARM immediates for ALU instructions are made of an unsigned 8-bit 370 * right-rotated by an even amount between 0 and 30. 371 * 372 * Return < 0 if @imm cannot be encoded, else the entire imm12 field. 373 */ 374static int encode_imm(uint32_t imm) 375{ 376 uint32_t rot, imm8; 377 378 /* Simple case, no rotation required. */ 379 if ((imm & ~0xff) == 0) { 380 return imm; 381 } 382 383 /* Next, try a simple even shift. */ 384 rot = ctz32(imm) & ~1; 385 imm8 = imm >> rot; 386 rot = 32 - rot; 387 if ((imm8 & ~0xff) == 0) { 388 goto found; 389 } 390 391 /* 392 * Finally, try harder with rotations. 393 * The ctz test above will have taken care of rotates >= 8. 394 */ 395 for (rot = 2; rot < 8; rot += 2) { 396 imm8 = rol32(imm, rot); 397 if ((imm8 & ~0xff) == 0) { 398 goto found; 399 } 400 } 401 /* Fail: imm cannot be encoded. */ 402 return -1; 403 404 found: 405 /* Note that rot is even, and we discard bit 0 by shifting by 7. */ 406 return rot << 7 | imm8; 407} 408 409static int encode_imm_nofail(uint32_t imm) 410{ 411 int ret = encode_imm(imm); 412 tcg_debug_assert(ret >= 0); 413 return ret; 414} 415 416static bool check_fit_imm(uint32_t imm) 417{ 418 return encode_imm(imm) >= 0; 419} 420 421/* Return true if v16 is a valid 16-bit shifted immediate. */ 422static bool is_shimm16(uint16_t v16, int *cmode, int *imm8) 423{ 424 if (v16 == (v16 & 0xff)) { 425 *cmode = 0x8; 426 *imm8 = v16 & 0xff; 427 return true; 428 } else if (v16 == (v16 & 0xff00)) { 429 *cmode = 0xa; 430 *imm8 = v16 >> 8; 431 return true; 432 } 433 return false; 434} 435 436/* Return true if v32 is a valid 32-bit shifted immediate. */ 437static bool is_shimm32(uint32_t v32, int *cmode, int *imm8) 438{ 439 if (v32 == (v32 & 0xff)) { 440 *cmode = 0x0; 441 *imm8 = v32 & 0xff; 442 return true; 443 } else if (v32 == (v32 & 0xff00)) { 444 *cmode = 0x2; 445 *imm8 = (v32 >> 8) & 0xff; 446 return true; 447 } else if (v32 == (v32 & 0xff0000)) { 448 *cmode = 0x4; 449 *imm8 = (v32 >> 16) & 0xff; 450 return true; 451 } else if (v32 == (v32 & 0xff000000)) { 452 *cmode = 0x6; 453 *imm8 = v32 >> 24; 454 return true; 455 } 456 return false; 457} 458 459/* Return true if v32 is a valid 32-bit shifting ones immediate. */ 460static bool is_soimm32(uint32_t v32, int *cmode, int *imm8) 461{ 462 if ((v32 & 0xffff00ff) == 0xff) { 463 *cmode = 0xc; 464 *imm8 = (v32 >> 8) & 0xff; 465 return true; 466 } else if ((v32 & 0xff00ffff) == 0xffff) { 467 *cmode = 0xd; 468 *imm8 = (v32 >> 16) & 0xff; 469 return true; 470 } 471 return false; 472} 473 474/* 475 * Return non-zero if v32 can be formed by MOVI+ORR. 476 * Place the parameters for MOVI in (cmode, imm8). 477 * Return the cmode for ORR; the imm8 can be had via extraction from v32. 478 */ 479static int is_shimm32_pair(uint32_t v32, int *cmode, int *imm8) 480{ 481 int i; 482 483 for (i = 6; i > 0; i -= 2) { 484 /* Mask out one byte we can add with ORR. */ 485 uint32_t tmp = v32 & ~(0xffu << (i * 4)); 486 if (is_shimm32(tmp, cmode, imm8) || 487 is_soimm32(tmp, cmode, imm8)) { 488 break; 489 } 490 } 491 return i; 492} 493 494/* Return true if V is a valid 16-bit or 32-bit shifted immediate. */ 495static bool is_shimm1632(uint32_t v32, int *cmode, int *imm8) 496{ 497 if (v32 == deposit32(v32, 16, 16, v32)) { 498 return is_shimm16(v32, cmode, imm8); 499 } else { 500 return is_shimm32(v32, cmode, imm8); 501 } 502} 503 504/* Test if a constant matches the constraint. 505 * TODO: define constraints for: 506 * 507 * ldr/str offset: between -0xfff and 0xfff 508 * ldrh/strh offset: between -0xff and 0xff 509 * mov operand2: values represented with x << (2 * y), x < 0x100 510 * add, sub, eor...: ditto 511 */ 512static bool tcg_target_const_match(int64_t val, int ct, 513 TCGType type, TCGCond cond, int vece) 514{ 515 if (ct & TCG_CT_CONST) { 516 return 1; 517 } else if ((ct & TCG_CT_CONST_ARM) && check_fit_imm(val)) { 518 return 1; 519 } else if ((ct & TCG_CT_CONST_INV) && check_fit_imm(~val)) { 520 return 1; 521 } else if ((ct & TCG_CT_CONST_NEG) && check_fit_imm(-val)) { 522 return 1; 523 } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) { 524 return 1; 525 } 526 527 switch (ct & (TCG_CT_CONST_ORRI | TCG_CT_CONST_ANDI)) { 528 case 0: 529 break; 530 case TCG_CT_CONST_ANDI: 531 val = ~val; 532 /* fallthru */ 533 case TCG_CT_CONST_ORRI: 534 if (val == deposit64(val, 32, 32, val)) { 535 int cmode, imm8; 536 return is_shimm1632(val, &cmode, &imm8); 537 } 538 break; 539 default: 540 /* Both bits should not be set for the same insn. */ 541 g_assert_not_reached(); 542 } 543 544 return 0; 545} 546 547static void tcg_out_b_imm(TCGContext *s, ARMCond cond, int32_t offset) 548{ 549 tcg_out32(s, (cond << 28) | INSN_B | 550 (((offset - 8) >> 2) & 0x00ffffff)); 551} 552 553static void tcg_out_bl_imm(TCGContext *s, ARMCond cond, int32_t offset) 554{ 555 tcg_out32(s, (cond << 28) | 0x0b000000 | 556 (((offset - 8) >> 2) & 0x00ffffff)); 557} 558 559static void tcg_out_blx_reg(TCGContext *s, ARMCond cond, TCGReg rn) 560{ 561 tcg_out32(s, (cond << 28) | 0x012fff30 | rn); 562} 563 564static void tcg_out_blx_imm(TCGContext *s, int32_t offset) 565{ 566 tcg_out32(s, 0xfa000000 | ((offset & 2) << 23) | 567 (((offset - 8) >> 2) & 0x00ffffff)); 568} 569 570static void tcg_out_dat_reg(TCGContext *s, ARMCond cond, ARMInsn opc, 571 TCGReg rd, TCGReg rn, TCGReg rm, int shift) 572{ 573 tcg_out32(s, (cond << 28) | (0 << 25) | opc | 574 (rn << 16) | (rd << 12) | shift | rm); 575} 576 577static void tcg_out_mov_reg(TCGContext *s, ARMCond cond, TCGReg rd, TCGReg rm) 578{ 579 /* Simple reg-reg move, optimising out the 'do nothing' case */ 580 if (rd != rm) { 581 tcg_out_dat_reg(s, cond, ARITH_MOV, rd, 0, rm, SHIFT_IMM_LSL(0)); 582 } 583} 584 585static void tcg_out_bx_reg(TCGContext *s, ARMCond cond, TCGReg rn) 586{ 587 tcg_out32(s, (cond << 28) | 0x012fff10 | rn); 588} 589 590static void tcg_out_b_reg(TCGContext *s, ARMCond cond, TCGReg rn) 591{ 592 /* 593 * Unless the C portion of QEMU is compiled as thumb, we don't need 594 * true BX semantics; merely a branch to an address held in a register. 595 */ 596 tcg_out_bx_reg(s, cond, rn); 597} 598 599static void tcg_out_dat_imm(TCGContext *s, ARMCond cond, ARMInsn opc, 600 TCGReg rd, TCGReg rn, int im) 601{ 602 tcg_out32(s, (cond << 28) | (1 << 25) | opc | 603 (rn << 16) | (rd << 12) | im); 604} 605 606static void tcg_out_ldstm(TCGContext *s, ARMCond cond, ARMInsn opc, 607 TCGReg rn, uint16_t mask) 608{ 609 tcg_out32(s, (cond << 28) | opc | (rn << 16) | mask); 610} 611 612/* Note that this routine is used for both LDR and LDRH formats, so we do 613 not wish to include an immediate shift at this point. */ 614static void tcg_out_memop_r(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt, 615 TCGReg rn, TCGReg rm, bool u, bool p, bool w) 616{ 617 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) 618 | (w << 21) | (rn << 16) | (rt << 12) | rm); 619} 620 621static void tcg_out_memop_8(TCGContext *s, ARMCond cond, ARMInsn opc, TCGReg rt, 622 TCGReg rn, int imm8, bool p, bool w) 623{ 624 bool u = 1; 625 if (imm8 < 0) { 626 imm8 = -imm8; 627 u = 0; 628 } 629 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) | 630 (rn << 16) | (rt << 12) | ((imm8 & 0xf0) << 4) | (imm8 & 0xf)); 631} 632 633static void tcg_out_memop_12(TCGContext *s, ARMCond cond, ARMInsn opc, 634 TCGReg rt, TCGReg rn, int imm12, bool p, bool w) 635{ 636 bool u = 1; 637 if (imm12 < 0) { 638 imm12 = -imm12; 639 u = 0; 640 } 641 tcg_out32(s, (cond << 28) | opc | (u << 23) | (p << 24) | (w << 21) | 642 (rn << 16) | (rt << 12) | imm12); 643} 644 645static void tcg_out_ld32_12(TCGContext *s, ARMCond cond, TCGReg rt, 646 TCGReg rn, int imm12) 647{ 648 tcg_out_memop_12(s, cond, INSN_LDR_IMM, rt, rn, imm12, 1, 0); 649} 650 651static void tcg_out_st32_12(TCGContext *s, ARMCond cond, TCGReg rt, 652 TCGReg rn, int imm12) 653{ 654 tcg_out_memop_12(s, cond, INSN_STR_IMM, rt, rn, imm12, 1, 0); 655} 656 657static void tcg_out_ld32_r(TCGContext *s, ARMCond cond, TCGReg rt, 658 TCGReg rn, TCGReg rm) 659{ 660 tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 0); 661} 662 663static void tcg_out_st32_r(TCGContext *s, ARMCond cond, TCGReg rt, 664 TCGReg rn, TCGReg rm) 665{ 666 tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 0); 667} 668 669static void tcg_out_ldrd_8(TCGContext *s, ARMCond cond, TCGReg rt, 670 TCGReg rn, int imm8) 671{ 672 tcg_out_memop_8(s, cond, INSN_LDRD_IMM, rt, rn, imm8, 1, 0); 673} 674 675static void tcg_out_ldrd_r(TCGContext *s, ARMCond cond, TCGReg rt, 676 TCGReg rn, TCGReg rm) 677{ 678 tcg_out_memop_r(s, cond, INSN_LDRD_REG, rt, rn, rm, 1, 1, 0); 679} 680 681static void tcg_out_strd_8(TCGContext *s, ARMCond cond, TCGReg rt, 682 TCGReg rn, int imm8) 683{ 684 tcg_out_memop_8(s, cond, INSN_STRD_IMM, rt, rn, imm8, 1, 0); 685} 686 687static void tcg_out_strd_r(TCGContext *s, ARMCond cond, TCGReg rt, 688 TCGReg rn, TCGReg rm) 689{ 690 tcg_out_memop_r(s, cond, INSN_STRD_REG, rt, rn, rm, 1, 1, 0); 691} 692 693/* Register pre-increment with base writeback. */ 694static void tcg_out_ld32_rwb(TCGContext *s, ARMCond cond, TCGReg rt, 695 TCGReg rn, TCGReg rm) 696{ 697 tcg_out_memop_r(s, cond, INSN_LDR_REG, rt, rn, rm, 1, 1, 1); 698} 699 700static void tcg_out_st32_rwb(TCGContext *s, ARMCond cond, TCGReg rt, 701 TCGReg rn, TCGReg rm) 702{ 703 tcg_out_memop_r(s, cond, INSN_STR_REG, rt, rn, rm, 1, 1, 1); 704} 705 706static void tcg_out_ld16u_8(TCGContext *s, ARMCond cond, TCGReg rt, 707 TCGReg rn, int imm8) 708{ 709 tcg_out_memop_8(s, cond, INSN_LDRH_IMM, rt, rn, imm8, 1, 0); 710} 711 712static void tcg_out_st16_8(TCGContext *s, ARMCond cond, TCGReg rt, 713 TCGReg rn, int imm8) 714{ 715 tcg_out_memop_8(s, cond, INSN_STRH_IMM, rt, rn, imm8, 1, 0); 716} 717 718static void tcg_out_ld16u_r(TCGContext *s, ARMCond cond, TCGReg rt, 719 TCGReg rn, TCGReg rm) 720{ 721 tcg_out_memop_r(s, cond, INSN_LDRH_REG, rt, rn, rm, 1, 1, 0); 722} 723 724static void tcg_out_st16_r(TCGContext *s, ARMCond cond, TCGReg rt, 725 TCGReg rn, TCGReg rm) 726{ 727 tcg_out_memop_r(s, cond, INSN_STRH_REG, rt, rn, rm, 1, 1, 0); 728} 729 730static void tcg_out_ld16s_8(TCGContext *s, ARMCond cond, TCGReg rt, 731 TCGReg rn, int imm8) 732{ 733 tcg_out_memop_8(s, cond, INSN_LDRSH_IMM, rt, rn, imm8, 1, 0); 734} 735 736static void tcg_out_ld16s_r(TCGContext *s, ARMCond cond, TCGReg rt, 737 TCGReg rn, TCGReg rm) 738{ 739 tcg_out_memop_r(s, cond, INSN_LDRSH_REG, rt, rn, rm, 1, 1, 0); 740} 741 742static void tcg_out_ld8_12(TCGContext *s, ARMCond cond, TCGReg rt, 743 TCGReg rn, int imm12) 744{ 745 tcg_out_memop_12(s, cond, INSN_LDRB_IMM, rt, rn, imm12, 1, 0); 746} 747 748static void tcg_out_st8_12(TCGContext *s, ARMCond cond, TCGReg rt, 749 TCGReg rn, int imm12) 750{ 751 tcg_out_memop_12(s, cond, INSN_STRB_IMM, rt, rn, imm12, 1, 0); 752} 753 754static void tcg_out_ld8_r(TCGContext *s, ARMCond cond, TCGReg rt, 755 TCGReg rn, TCGReg rm) 756{ 757 tcg_out_memop_r(s, cond, INSN_LDRB_REG, rt, rn, rm, 1, 1, 0); 758} 759 760static void tcg_out_st8_r(TCGContext *s, ARMCond cond, TCGReg rt, 761 TCGReg rn, TCGReg rm) 762{ 763 tcg_out_memop_r(s, cond, INSN_STRB_REG, rt, rn, rm, 1, 1, 0); 764} 765 766static void tcg_out_ld8s_8(TCGContext *s, ARMCond cond, TCGReg rt, 767 TCGReg rn, int imm8) 768{ 769 tcg_out_memop_8(s, cond, INSN_LDRSB_IMM, rt, rn, imm8, 1, 0); 770} 771 772static void tcg_out_ld8s_r(TCGContext *s, ARMCond cond, TCGReg rt, 773 TCGReg rn, TCGReg rm) 774{ 775 tcg_out_memop_r(s, cond, INSN_LDRSB_REG, rt, rn, rm, 1, 1, 0); 776} 777 778static void tcg_out_movi_pool(TCGContext *s, ARMCond cond, 779 TCGReg rd, uint32_t arg) 780{ 781 new_pool_label(s, arg, R_ARM_PC13, s->code_ptr, 0); 782 tcg_out_ld32_12(s, cond, rd, TCG_REG_PC, 0); 783} 784 785static void tcg_out_movi32(TCGContext *s, ARMCond cond, 786 TCGReg rd, uint32_t arg) 787{ 788 int imm12, diff, opc, sh1, sh2; 789 uint32_t tt0, tt1, tt2; 790 791 /* Check a single MOV/MVN before anything else. */ 792 imm12 = encode_imm(arg); 793 if (imm12 >= 0) { 794 tcg_out_dat_imm(s, cond, ARITH_MOV, rd, 0, imm12); 795 return; 796 } 797 imm12 = encode_imm(~arg); 798 if (imm12 >= 0) { 799 tcg_out_dat_imm(s, cond, ARITH_MVN, rd, 0, imm12); 800 return; 801 } 802 803 /* Check for a pc-relative address. This will usually be the TB, 804 or within the TB, which is immediately before the code block. */ 805 diff = tcg_pcrel_diff(s, (void *)arg) - 8; 806 if (diff >= 0) { 807 imm12 = encode_imm(diff); 808 if (imm12 >= 0) { 809 tcg_out_dat_imm(s, cond, ARITH_ADD, rd, TCG_REG_PC, imm12); 810 return; 811 } 812 } else { 813 imm12 = encode_imm(-diff); 814 if (imm12 >= 0) { 815 tcg_out_dat_imm(s, cond, ARITH_SUB, rd, TCG_REG_PC, imm12); 816 return; 817 } 818 } 819 820 /* Use movw + movt. */ 821 if (use_armv7_instructions) { 822 /* movw */ 823 tcg_out32(s, (cond << 28) | 0x03000000 | (rd << 12) 824 | ((arg << 4) & 0x000f0000) | (arg & 0xfff)); 825 if (arg & 0xffff0000) { 826 /* movt */ 827 tcg_out32(s, (cond << 28) | 0x03400000 | (rd << 12) 828 | ((arg >> 12) & 0x000f0000) | ((arg >> 16) & 0xfff)); 829 } 830 return; 831 } 832 833 /* Look for sequences of two insns. If we have lots of 1's, we can 834 shorten the sequence by beginning with mvn and then clearing 835 higher bits with eor. */ 836 tt0 = arg; 837 opc = ARITH_MOV; 838 if (ctpop32(arg) > 16) { 839 tt0 = ~arg; 840 opc = ARITH_MVN; 841 } 842 sh1 = ctz32(tt0) & ~1; 843 tt1 = tt0 & ~(0xff << sh1); 844 sh2 = ctz32(tt1) & ~1; 845 tt2 = tt1 & ~(0xff << sh2); 846 if (tt2 == 0) { 847 int rot; 848 849 rot = ((32 - sh1) << 7) & 0xf00; 850 tcg_out_dat_imm(s, cond, opc, rd, 0, ((tt0 >> sh1) & 0xff) | rot); 851 rot = ((32 - sh2) << 7) & 0xf00; 852 tcg_out_dat_imm(s, cond, ARITH_EOR, rd, rd, 853 ((tt0 >> sh2) & 0xff) | rot); 854 return; 855 } 856 857 /* Otherwise, drop it into the constant pool. */ 858 tcg_out_movi_pool(s, cond, rd, arg); 859} 860 861/* 862 * Emit either the reg,imm or reg,reg form of a data-processing insn. 863 * rhs must satisfy the "rI" constraint. 864 */ 865static void tcg_out_dat_rI(TCGContext *s, ARMCond cond, ARMInsn opc, 866 TCGReg dst, TCGReg lhs, TCGArg rhs, int rhs_is_const) 867{ 868 if (rhs_is_const) { 869 tcg_out_dat_imm(s, cond, opc, dst, lhs, encode_imm_nofail(rhs)); 870 } else { 871 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0)); 872 } 873} 874 875/* 876 * Emit either the reg,imm or reg,reg form of a data-processing insn. 877 * rhs must satisfy the "rIK" constraint. 878 */ 879static void tcg_out_dat_IK(TCGContext *s, ARMCond cond, ARMInsn opc, 880 ARMInsn opinv, TCGReg dst, TCGReg lhs, TCGArg rhs) 881{ 882 int imm12 = encode_imm(rhs); 883 if (imm12 < 0) { 884 imm12 = encode_imm_nofail(~rhs); 885 opc = opinv; 886 } 887 tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12); 888} 889 890static void tcg_out_dat_rIK(TCGContext *s, ARMCond cond, ARMInsn opc, 891 ARMInsn opinv, TCGReg dst, TCGReg lhs, TCGArg rhs, 892 bool rhs_is_const) 893{ 894 if (rhs_is_const) { 895 tcg_out_dat_IK(s, cond, opc, opinv, dst, lhs, rhs); 896 } else { 897 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0)); 898 } 899} 900 901static void tcg_out_dat_IN(TCGContext *s, ARMCond cond, ARMInsn opc, 902 ARMInsn opneg, TCGReg dst, TCGReg lhs, TCGArg rhs) 903{ 904 int imm12 = encode_imm(rhs); 905 if (imm12 < 0) { 906 imm12 = encode_imm_nofail(-rhs); 907 opc = opneg; 908 } 909 tcg_out_dat_imm(s, cond, opc, dst, lhs, imm12); 910} 911 912static void tcg_out_dat_rIN(TCGContext *s, ARMCond cond, ARMInsn opc, 913 ARMInsn opneg, TCGReg dst, TCGReg lhs, TCGArg rhs, 914 bool rhs_is_const) 915{ 916 /* Emit either the reg,imm or reg,reg form of a data-processing insn. 917 * rhs must satisfy the "rIN" constraint. 918 */ 919 if (rhs_is_const) { 920 tcg_out_dat_IN(s, cond, opc, opneg, dst, lhs, rhs); 921 } else { 922 tcg_out_dat_reg(s, cond, opc, dst, lhs, rhs, SHIFT_IMM_LSL(0)); 923 } 924} 925 926static void tcg_out_ext8s(TCGContext *s, TCGType t, TCGReg rd, TCGReg rn) 927{ 928 /* sxtb */ 929 tcg_out32(s, 0x06af0070 | (COND_AL << 28) | (rd << 12) | rn); 930} 931 932static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rn) 933{ 934 tcg_out_dat_imm(s, COND_AL, ARITH_AND, rd, rn, 0xff); 935} 936 937static void tcg_out_ext16s(TCGContext *s, TCGType t, TCGReg rd, TCGReg rn) 938{ 939 /* sxth */ 940 tcg_out32(s, 0x06bf0070 | (COND_AL << 28) | (rd << 12) | rn); 941} 942 943static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rn) 944{ 945 /* uxth */ 946 tcg_out32(s, 0x06ff0070 | (COND_AL << 28) | (rd << 12) | rn); 947} 948 949static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rn) 950{ 951 g_assert_not_reached(); 952} 953 954static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rn) 955{ 956 g_assert_not_reached(); 957} 958 959static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rn) 960{ 961 g_assert_not_reached(); 962} 963 964static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rn) 965{ 966 g_assert_not_reached(); 967} 968 969static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rn) 970{ 971 g_assert_not_reached(); 972} 973 974static void tgen_deposit(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 975 TCGReg a2, unsigned ofs, unsigned len) 976{ 977 /* bfi/bfc */ 978 tcg_out32(s, 0x07c00010 | (COND_AL << 28) | (a0 << 12) | a1 979 | (ofs << 7) | ((ofs + len - 1) << 16)); 980} 981 982static void tgen_depositi(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 983 tcg_target_long a2, unsigned ofs, unsigned len) 984{ 985 /* bfi becomes bfc with rn == 15. */ 986 tgen_deposit(s, type, a0, a1, 15, ofs, len); 987} 988 989static const TCGOutOpDeposit outop_deposit = { 990 .base.static_constraint = C_O1_I2(r, 0, rZ), 991 .out_rrr = tgen_deposit, 992 .out_rri = tgen_depositi, 993}; 994 995static void tgen_extract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rn, 996 unsigned ofs, unsigned len) 997{ 998 /* According to gcc, AND can be faster. */ 999 if (ofs == 0 && len <= 8) { 1000 tcg_out_dat_imm(s, COND_AL, ARITH_AND, rd, rn, 1001 encode_imm_nofail((1 << len) - 1)); 1002 return; 1003 } 1004 1005 if (use_armv7_instructions) { 1006 /* ubfx */ 1007 tcg_out32(s, 0x07e00050 | (COND_AL << 28) | (rd << 12) | rn 1008 | (ofs << 7) | ((len - 1) << 16)); 1009 return; 1010 } 1011 1012 assert(ofs % 8 == 0); 1013 switch (len) { 1014 case 8: 1015 /* uxtb */ 1016 tcg_out32(s, 0x06ef0070 | (COND_AL << 28) | 1017 (rd << 12) | (ofs << 7) | rn); 1018 break; 1019 case 16: 1020 /* uxth */ 1021 tcg_out32(s, 0x06ff0070 | (COND_AL << 28) | 1022 (rd << 12) | (ofs << 7) | rn); 1023 break; 1024 default: 1025 g_assert_not_reached(); 1026 } 1027} 1028 1029static const TCGOutOpExtract outop_extract = { 1030 .base.static_constraint = C_O1_I1(r, r), 1031 .out_rr = tgen_extract, 1032}; 1033 1034static void tgen_sextract(TCGContext *s, TCGType type, TCGReg rd, TCGReg rn, 1035 unsigned ofs, unsigned len) 1036{ 1037 if (use_armv7_instructions) { 1038 /* sbfx */ 1039 tcg_out32(s, 0x07a00050 | (COND_AL << 28) | (rd << 12) | rn 1040 | (ofs << 7) | ((len - 1) << 16)); 1041 return; 1042 } 1043 1044 assert(ofs % 8 == 0); 1045 switch (len) { 1046 case 8: 1047 /* sxtb */ 1048 tcg_out32(s, 0x06af0070 | (COND_AL << 28) | 1049 (rd << 12) | (ofs << 7) | rn); 1050 break; 1051 case 16: 1052 /* sxth */ 1053 tcg_out32(s, 0x06bf0070 | (COND_AL << 28) | 1054 (rd << 12) | (ofs << 7) | rn); 1055 break; 1056 default: 1057 g_assert_not_reached(); 1058 } 1059} 1060 1061static const TCGOutOpExtract outop_sextract = { 1062 .base.static_constraint = C_O1_I1(r, r), 1063 .out_rr = tgen_sextract, 1064}; 1065 1066 1067static void tcg_out_ld32u(TCGContext *s, ARMCond cond, 1068 TCGReg rd, TCGReg rn, int32_t offset) 1069{ 1070 if (offset > 0xfff || offset < -0xfff) { 1071 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1072 tcg_out_ld32_r(s, cond, rd, rn, TCG_REG_TMP); 1073 } else 1074 tcg_out_ld32_12(s, cond, rd, rn, offset); 1075} 1076 1077static void tcg_out_st32(TCGContext *s, ARMCond cond, 1078 TCGReg rd, TCGReg rn, int32_t offset) 1079{ 1080 if (offset > 0xfff || offset < -0xfff) { 1081 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1082 tcg_out_st32_r(s, cond, rd, rn, TCG_REG_TMP); 1083 } else 1084 tcg_out_st32_12(s, cond, rd, rn, offset); 1085} 1086 1087/* 1088 * The _goto case is normally between TBs within the same code buffer, and 1089 * with the code buffer limited to 16MB we wouldn't need the long case. 1090 * But we also use it for the tail-call to the qemu_ld/st helpers, which does. 1091 */ 1092static void tcg_out_goto(TCGContext *s, ARMCond cond, const tcg_insn_unit *addr) 1093{ 1094 intptr_t addri = (intptr_t)addr; 1095 ptrdiff_t disp = tcg_pcrel_diff(s, addr); 1096 bool arm_mode = !(addri & 1); 1097 1098 if (arm_mode && disp - 8 < 0x01fffffd && disp - 8 > -0x01fffffd) { 1099 tcg_out_b_imm(s, cond, disp); 1100 return; 1101 } 1102 1103 /* LDR is interworking from v5t. */ 1104 tcg_out_movi_pool(s, cond, TCG_REG_PC, addri); 1105} 1106 1107/* 1108 * The call case is mostly used for helpers - so it's not unreasonable 1109 * for them to be beyond branch range. 1110 */ 1111static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *addr) 1112{ 1113 intptr_t addri = (intptr_t)addr; 1114 ptrdiff_t disp = tcg_pcrel_diff(s, addr); 1115 bool arm_mode = !(addri & 1); 1116 1117 if (disp - 8 < 0x02000000 && disp - 8 >= -0x02000000) { 1118 if (arm_mode) { 1119 tcg_out_bl_imm(s, COND_AL, disp); 1120 } else { 1121 tcg_out_blx_imm(s, disp); 1122 } 1123 return; 1124 } 1125 1126 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, addri); 1127 tcg_out_blx_reg(s, COND_AL, TCG_REG_TMP); 1128} 1129 1130static void tcg_out_call(TCGContext *s, const tcg_insn_unit *addr, 1131 const TCGHelperInfo *info) 1132{ 1133 tcg_out_call_int(s, addr); 1134} 1135 1136static void tcg_out_goto_label(TCGContext *s, ARMCond cond, TCGLabel *l) 1137{ 1138 if (l->has_value) { 1139 tcg_out_goto(s, cond, l->u.value_ptr); 1140 } else { 1141 tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, l, 0); 1142 tcg_out_b_imm(s, cond, 0); 1143 } 1144} 1145 1146static void tcg_out_br(TCGContext *s, TCGLabel *l) 1147{ 1148 tcg_out_goto_label(s, COND_AL, l); 1149} 1150 1151static void tcg_out_mb(TCGContext *s, unsigned a0) 1152{ 1153 if (use_armv7_instructions) { 1154 tcg_out32(s, INSN_DMB_ISH); 1155 } else { 1156 tcg_out32(s, INSN_DMB_MCR); 1157 } 1158} 1159 1160static TCGCond tgen_cmp(TCGContext *s, TCGCond cond, TCGReg a, TCGReg b) 1161{ 1162 if (is_tst_cond(cond)) { 1163 tcg_out_dat_reg(s, COND_AL, ARITH_TST, 0, a, b, SHIFT_IMM_LSL(0)); 1164 return tcg_tst_eqne_cond(cond); 1165 } 1166 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, a, b, SHIFT_IMM_LSL(0)); 1167 return cond; 1168} 1169 1170static TCGCond tgen_cmpi(TCGContext *s, TCGCond cond, TCGReg a, TCGArg b) 1171{ 1172 int imm12; 1173 1174 if (!is_tst_cond(cond)) { 1175 tcg_out_dat_IN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0, a, b); 1176 return cond; 1177 } 1178 1179 /* 1180 * The compare constraints allow rIN, but TST does not support N. 1181 * Be prepared to load the constant into a scratch register. 1182 */ 1183 imm12 = encode_imm(b); 1184 if (imm12 >= 0) { 1185 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, a, imm12); 1186 } else { 1187 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, b); 1188 tcg_out_dat_reg(s, COND_AL, ARITH_TST, 0, 1189 a, TCG_REG_TMP, SHIFT_IMM_LSL(0)); 1190 } 1191 return tcg_tst_eqne_cond(cond); 1192} 1193 1194static TCGCond tcg_out_cmp(TCGContext *s, TCGCond cond, TCGReg a, 1195 TCGArg b, int b_const) 1196{ 1197 if (b_const) { 1198 return tgen_cmpi(s, cond, a, b); 1199 } else { 1200 return tgen_cmp(s, cond, a, b); 1201 } 1202} 1203 1204static TCGCond tcg_out_cmp2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, 1205 TCGArg bl, bool const_bl, TCGArg bh, bool const_bh) 1206{ 1207 switch (cond) { 1208 case TCG_COND_EQ: 1209 case TCG_COND_NE: 1210 case TCG_COND_LTU: 1211 case TCG_COND_LEU: 1212 case TCG_COND_GTU: 1213 case TCG_COND_GEU: 1214 /* 1215 * We perform a conditional comparison. If the high half is 1216 * equal, then overwrite the flags with the comparison of the 1217 * low half. The resulting flags cover the whole. 1218 */ 1219 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, ah, bh, const_bh); 1220 tcg_out_dat_rI(s, COND_EQ, ARITH_CMP, 0, al, bl, const_bl); 1221 return cond; 1222 1223 case TCG_COND_TSTEQ: 1224 case TCG_COND_TSTNE: 1225 /* Similar, but with TST instead of CMP. */ 1226 tcg_out_dat_rI(s, COND_AL, ARITH_TST, 0, ah, bh, const_bh); 1227 tcg_out_dat_rI(s, COND_EQ, ARITH_TST, 0, al, bl, const_bl); 1228 return tcg_tst_eqne_cond(cond); 1229 1230 case TCG_COND_LT: 1231 case TCG_COND_GE: 1232 /* We perform a double-word subtraction and examine the result. 1233 We do not actually need the result of the subtract, so the 1234 low part "subtract" is a compare. For the high half we have 1235 no choice but to compute into a temporary. */ 1236 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, al, bl, const_bl); 1237 tcg_out_dat_rI(s, COND_AL, ARITH_SBC | TO_CPSR, 1238 TCG_REG_TMP, ah, bh, const_bh); 1239 return cond; 1240 1241 case TCG_COND_LE: 1242 case TCG_COND_GT: 1243 /* Similar, but with swapped arguments, via reversed subtract. */ 1244 tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR, 1245 TCG_REG_TMP, al, bl, const_bl); 1246 tcg_out_dat_rI(s, COND_AL, ARITH_RSC | TO_CPSR, 1247 TCG_REG_TMP, ah, bh, const_bh); 1248 return tcg_swap_cond(cond); 1249 1250 default: 1251 g_assert_not_reached(); 1252 } 1253} 1254 1255/* 1256 * Note that TCGReg references Q-registers. 1257 * Q-regno = 2 * D-regno, so shift left by 1 while inserting. 1258 */ 1259static uint32_t encode_vd(TCGReg rd) 1260{ 1261 tcg_debug_assert(rd >= TCG_REG_Q0); 1262 return (extract32(rd, 3, 1) << 22) | (extract32(rd, 0, 3) << 13); 1263} 1264 1265static uint32_t encode_vn(TCGReg rn) 1266{ 1267 tcg_debug_assert(rn >= TCG_REG_Q0); 1268 return (extract32(rn, 3, 1) << 7) | (extract32(rn, 0, 3) << 17); 1269} 1270 1271static uint32_t encode_vm(TCGReg rm) 1272{ 1273 tcg_debug_assert(rm >= TCG_REG_Q0); 1274 return (extract32(rm, 3, 1) << 5) | (extract32(rm, 0, 3) << 1); 1275} 1276 1277static void tcg_out_vreg2(TCGContext *s, ARMInsn insn, int q, int vece, 1278 TCGReg d, TCGReg m) 1279{ 1280 tcg_out32(s, insn | (vece << 18) | (q << 6) | 1281 encode_vd(d) | encode_vm(m)); 1282} 1283 1284static void tcg_out_vreg3(TCGContext *s, ARMInsn insn, int q, int vece, 1285 TCGReg d, TCGReg n, TCGReg m) 1286{ 1287 tcg_out32(s, insn | (vece << 20) | (q << 6) | 1288 encode_vd(d) | encode_vn(n) | encode_vm(m)); 1289} 1290 1291static void tcg_out_vmovi(TCGContext *s, TCGReg rd, 1292 int q, int op, int cmode, uint8_t imm8) 1293{ 1294 tcg_out32(s, INSN_VMOVI | encode_vd(rd) | (q << 6) | (op << 5) 1295 | (cmode << 8) | extract32(imm8, 0, 4) 1296 | (extract32(imm8, 4, 3) << 16) 1297 | (extract32(imm8, 7, 1) << 24)); 1298} 1299 1300static void tcg_out_vshifti(TCGContext *s, ARMInsn insn, int q, 1301 TCGReg rd, TCGReg rm, int l_imm6) 1302{ 1303 tcg_out32(s, insn | (q << 6) | encode_vd(rd) | encode_vm(rm) | 1304 (extract32(l_imm6, 6, 1) << 7) | 1305 (extract32(l_imm6, 0, 6) << 16)); 1306} 1307 1308static void tcg_out_vldst(TCGContext *s, ARMInsn insn, 1309 TCGReg rd, TCGReg rn, int offset) 1310{ 1311 if (offset != 0) { 1312 if (check_fit_imm(offset) || check_fit_imm(-offset)) { 1313 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB, 1314 TCG_REG_TMP, rn, offset, true); 1315 } else { 1316 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, offset); 1317 tcg_out_dat_reg(s, COND_AL, ARITH_ADD, 1318 TCG_REG_TMP, TCG_REG_TMP, rn, 0); 1319 } 1320 rn = TCG_REG_TMP; 1321 } 1322 tcg_out32(s, insn | (rn << 16) | encode_vd(rd) | 0xf); 1323} 1324 1325typedef struct { 1326 ARMCond cond; 1327 TCGReg base; 1328 int index; 1329 bool index_scratch; 1330 TCGAtomAlign aa; 1331} HostAddress; 1332 1333bool tcg_target_has_memory_bswap(MemOp memop) 1334{ 1335 return false; 1336} 1337 1338static TCGReg ldst_ra_gen(TCGContext *s, const TCGLabelQemuLdst *l, int arg) 1339{ 1340 /* We arrive at the slow path via "BLNE", so R14 contains l->raddr. */ 1341 return TCG_REG_R14; 1342} 1343 1344static const TCGLdstHelperParam ldst_helper_param = { 1345 .ra_gen = ldst_ra_gen, 1346 .ntmp = 1, 1347 .tmp = { TCG_REG_TMP }, 1348}; 1349 1350static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) 1351{ 1352 MemOp opc = get_memop(lb->oi); 1353 1354 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 1355 return false; 1356 } 1357 1358 tcg_out_ld_helper_args(s, lb, &ldst_helper_param); 1359 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE]); 1360 tcg_out_ld_helper_ret(s, lb, false, &ldst_helper_param); 1361 1362 tcg_out_goto(s, COND_AL, lb->raddr); 1363 return true; 1364} 1365 1366static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) 1367{ 1368 MemOp opc = get_memop(lb->oi); 1369 1370 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 1371 return false; 1372 } 1373 1374 tcg_out_st_helper_args(s, lb, &ldst_helper_param); 1375 1376 /* Tail-call to the helper, which will return to the fast path. */ 1377 tcg_out_goto(s, COND_AL, qemu_st_helpers[opc & MO_SIZE]); 1378 return true; 1379} 1380 1381/* We expect to use an 9-bit sign-magnitude negative offset from ENV. */ 1382#define MIN_TLB_MASK_TABLE_OFS -256 1383 1384static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h, 1385 TCGReg addr, MemOpIdx oi, bool is_ld) 1386{ 1387 TCGLabelQemuLdst *ldst = NULL; 1388 MemOp opc = get_memop(oi); 1389 unsigned a_mask; 1390 1391 if (tcg_use_softmmu) { 1392 *h = (HostAddress){ 1393 .cond = COND_AL, 1394 .base = addr, 1395 .index = TCG_REG_R1, 1396 .index_scratch = true, 1397 }; 1398 } else { 1399 *h = (HostAddress){ 1400 .cond = COND_AL, 1401 .base = addr, 1402 .index = guest_base ? TCG_REG_GUEST_BASE : -1, 1403 .index_scratch = false, 1404 }; 1405 } 1406 1407 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false); 1408 a_mask = (1 << h->aa.align) - 1; 1409 1410 if (tcg_use_softmmu) { 1411 int mem_index = get_mmuidx(oi); 1412 int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read) 1413 : offsetof(CPUTLBEntry, addr_write); 1414 int fast_off = tlb_mask_table_ofs(s, mem_index); 1415 unsigned s_mask = (1 << (opc & MO_SIZE)) - 1; 1416 TCGReg t_addr; 1417 1418 ldst = new_ldst_label(s); 1419 ldst->is_ld = is_ld; 1420 ldst->oi = oi; 1421 ldst->addr_reg = addr; 1422 1423 /* Load cpu->neg.tlb.f[mmu_idx].{mask,table} into {r0,r1}. */ 1424 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, mask) != 0); 1425 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, table) != 4); 1426 tcg_out_ldrd_8(s, COND_AL, TCG_REG_R0, TCG_AREG0, fast_off); 1427 1428 /* Extract the tlb index from the address into R0. */ 1429 tcg_out_dat_reg(s, COND_AL, ARITH_AND, TCG_REG_R0, TCG_REG_R0, addr, 1430 SHIFT_IMM_LSR(s->page_bits - CPU_TLB_ENTRY_BITS)); 1431 1432 /* 1433 * Add the tlb_table pointer, creating the CPUTLBEntry address in R1. 1434 * Load the tlb comparator into R2 and the fast path addend into R1. 1435 */ 1436 if (cmp_off == 0) { 1437 tcg_out_ld32_rwb(s, COND_AL, TCG_REG_R2, TCG_REG_R1, TCG_REG_R0); 1438 } else { 1439 tcg_out_dat_reg(s, COND_AL, ARITH_ADD, 1440 TCG_REG_R1, TCG_REG_R1, TCG_REG_R0, 0); 1441 tcg_out_ld32_12(s, COND_AL, TCG_REG_R2, TCG_REG_R1, cmp_off); 1442 } 1443 1444 /* Load the tlb addend. */ 1445 tcg_out_ld32_12(s, COND_AL, TCG_REG_R1, TCG_REG_R1, 1446 offsetof(CPUTLBEntry, addend)); 1447 1448 /* 1449 * Check alignment, check comparators. 1450 * Do this in 2-4 insns. Use MOVW for v7, if possible, 1451 * to reduce the number of sequential conditional instructions. 1452 * Almost all guests have at least 4k pages, which means that we need 1453 * to clear at least 9 bits even for an 8-byte memory, which means it 1454 * isn't worth checking for an immediate operand for BIC. 1455 * 1456 * For unaligned accesses, test the page of the last unit of alignment. 1457 * This leaves the least significant alignment bits unchanged, and of 1458 * course must be zero. 1459 */ 1460 t_addr = addr; 1461 if (a_mask < s_mask) { 1462 t_addr = TCG_REG_R0; 1463 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, t_addr, 1464 addr, s_mask - a_mask); 1465 } 1466 if (use_armv7_instructions && s->page_bits <= 16) { 1467 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, ~(s->page_mask | a_mask)); 1468 tcg_out_dat_reg(s, COND_AL, ARITH_BIC, TCG_REG_TMP, 1469 t_addr, TCG_REG_TMP, 0); 1470 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, 1471 TCG_REG_R2, TCG_REG_TMP, 0); 1472 } else { 1473 if (a_mask) { 1474 tcg_debug_assert(a_mask <= 0xff); 1475 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addr, a_mask); 1476 } 1477 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP, 0, t_addr, 1478 SHIFT_IMM_LSR(s->page_bits)); 1479 tcg_out_dat_reg(s, (a_mask ? COND_EQ : COND_AL), ARITH_CMP, 1480 0, TCG_REG_R2, TCG_REG_TMP, 1481 SHIFT_IMM_LSL(s->page_bits)); 1482 } 1483 } else if (a_mask) { 1484 ldst = new_ldst_label(s); 1485 ldst->is_ld = is_ld; 1486 ldst->oi = oi; 1487 ldst->addr_reg = addr; 1488 1489 /* We are expecting alignment to max out at 7 */ 1490 tcg_debug_assert(a_mask <= 0xff); 1491 /* tst addr, #mask */ 1492 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addr, a_mask); 1493 } 1494 1495 return ldst; 1496} 1497 1498static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg datalo, 1499 TCGReg datahi, HostAddress h) 1500{ 1501 TCGReg base; 1502 1503 /* Byte swapping is left to middle-end expansion. */ 1504 tcg_debug_assert((opc & MO_BSWAP) == 0); 1505 1506 switch (opc & MO_SSIZE) { 1507 case MO_UB: 1508 if (h.index < 0) { 1509 tcg_out_ld8_12(s, h.cond, datalo, h.base, 0); 1510 } else { 1511 tcg_out_ld8_r(s, h.cond, datalo, h.base, h.index); 1512 } 1513 break; 1514 case MO_SB: 1515 if (h.index < 0) { 1516 tcg_out_ld8s_8(s, h.cond, datalo, h.base, 0); 1517 } else { 1518 tcg_out_ld8s_r(s, h.cond, datalo, h.base, h.index); 1519 } 1520 break; 1521 case MO_UW: 1522 if (h.index < 0) { 1523 tcg_out_ld16u_8(s, h.cond, datalo, h.base, 0); 1524 } else { 1525 tcg_out_ld16u_r(s, h.cond, datalo, h.base, h.index); 1526 } 1527 break; 1528 case MO_SW: 1529 if (h.index < 0) { 1530 tcg_out_ld16s_8(s, h.cond, datalo, h.base, 0); 1531 } else { 1532 tcg_out_ld16s_r(s, h.cond, datalo, h.base, h.index); 1533 } 1534 break; 1535 case MO_UL: 1536 if (h.index < 0) { 1537 tcg_out_ld32_12(s, h.cond, datalo, h.base, 0); 1538 } else { 1539 tcg_out_ld32_r(s, h.cond, datalo, h.base, h.index); 1540 } 1541 break; 1542 case MO_UQ: 1543 /* We used pair allocation for datalo, so already should be aligned. */ 1544 tcg_debug_assert((datalo & 1) == 0); 1545 tcg_debug_assert(datahi == datalo + 1); 1546 /* LDRD requires alignment; double-check that. */ 1547 if (memop_alignment_bits(opc) >= MO_64) { 1548 if (h.index < 0) { 1549 tcg_out_ldrd_8(s, h.cond, datalo, h.base, 0); 1550 break; 1551 } 1552 /* 1553 * Rm (the second address op) must not overlap Rt or Rt + 1. 1554 * Since datalo is aligned, we can simplify the test via alignment. 1555 * Flip the two address arguments if that works. 1556 */ 1557 if ((h.index & ~1) != datalo) { 1558 tcg_out_ldrd_r(s, h.cond, datalo, h.base, h.index); 1559 break; 1560 } 1561 if ((h.base & ~1) != datalo) { 1562 tcg_out_ldrd_r(s, h.cond, datalo, h.index, h.base); 1563 break; 1564 } 1565 } 1566 if (h.index < 0) { 1567 base = h.base; 1568 if (datalo == h.base) { 1569 tcg_out_mov_reg(s, h.cond, TCG_REG_TMP, base); 1570 base = TCG_REG_TMP; 1571 } 1572 } else if (h.index_scratch) { 1573 tcg_out_ld32_rwb(s, h.cond, datalo, h.index, h.base); 1574 tcg_out_ld32_12(s, h.cond, datahi, h.index, 4); 1575 break; 1576 } else { 1577 tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP, 1578 h.base, h.index, SHIFT_IMM_LSL(0)); 1579 base = TCG_REG_TMP; 1580 } 1581 tcg_out_ld32_12(s, h.cond, datalo, base, 0); 1582 tcg_out_ld32_12(s, h.cond, datahi, base, 4); 1583 break; 1584 default: 1585 g_assert_not_reached(); 1586 } 1587} 1588 1589static void tcg_out_qemu_ld(TCGContext *s, TCGReg datalo, TCGReg datahi, 1590 TCGReg addr, MemOpIdx oi, TCGType data_type) 1591{ 1592 MemOp opc = get_memop(oi); 1593 TCGLabelQemuLdst *ldst; 1594 HostAddress h; 1595 1596 ldst = prepare_host_addr(s, &h, addr, oi, true); 1597 if (ldst) { 1598 ldst->type = data_type; 1599 ldst->datalo_reg = datalo; 1600 ldst->datahi_reg = datahi; 1601 1602 /* 1603 * This a conditional BL only to load a pointer within this 1604 * opcode into LR for the slow path. We will not be using 1605 * the value for a tail call. 1606 */ 1607 ldst->label_ptr[0] = s->code_ptr; 1608 tcg_out_bl_imm(s, COND_NE, 0); 1609 1610 tcg_out_qemu_ld_direct(s, opc, datalo, datahi, h); 1611 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1612 } else { 1613 tcg_out_qemu_ld_direct(s, opc, datalo, datahi, h); 1614 } 1615} 1616 1617static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg datalo, 1618 TCGReg datahi, HostAddress h) 1619{ 1620 /* Byte swapping is left to middle-end expansion. */ 1621 tcg_debug_assert((opc & MO_BSWAP) == 0); 1622 1623 switch (opc & MO_SIZE) { 1624 case MO_8: 1625 if (h.index < 0) { 1626 tcg_out_st8_12(s, h.cond, datalo, h.base, 0); 1627 } else { 1628 tcg_out_st8_r(s, h.cond, datalo, h.base, h.index); 1629 } 1630 break; 1631 case MO_16: 1632 if (h.index < 0) { 1633 tcg_out_st16_8(s, h.cond, datalo, h.base, 0); 1634 } else { 1635 tcg_out_st16_r(s, h.cond, datalo, h.base, h.index); 1636 } 1637 break; 1638 case MO_32: 1639 if (h.index < 0) { 1640 tcg_out_st32_12(s, h.cond, datalo, h.base, 0); 1641 } else { 1642 tcg_out_st32_r(s, h.cond, datalo, h.base, h.index); 1643 } 1644 break; 1645 case MO_64: 1646 /* We used pair allocation for datalo, so already should be aligned. */ 1647 tcg_debug_assert((datalo & 1) == 0); 1648 tcg_debug_assert(datahi == datalo + 1); 1649 /* STRD requires alignment; double-check that. */ 1650 if (memop_alignment_bits(opc) >= MO_64) { 1651 if (h.index < 0) { 1652 tcg_out_strd_8(s, h.cond, datalo, h.base, 0); 1653 } else { 1654 tcg_out_strd_r(s, h.cond, datalo, h.base, h.index); 1655 } 1656 } else if (h.index < 0) { 1657 tcg_out_st32_12(s, h.cond, datalo, h.base, 0); 1658 tcg_out_st32_12(s, h.cond, datahi, h.base, 4); 1659 } else if (h.index_scratch) { 1660 tcg_out_st32_rwb(s, h.cond, datalo, h.index, h.base); 1661 tcg_out_st32_12(s, h.cond, datahi, h.index, 4); 1662 } else { 1663 tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP, 1664 h.base, h.index, SHIFT_IMM_LSL(0)); 1665 tcg_out_st32_12(s, h.cond, datalo, TCG_REG_TMP, 0); 1666 tcg_out_st32_12(s, h.cond, datahi, TCG_REG_TMP, 4); 1667 } 1668 break; 1669 default: 1670 g_assert_not_reached(); 1671 } 1672} 1673 1674static void tcg_out_qemu_st(TCGContext *s, TCGReg datalo, TCGReg datahi, 1675 TCGReg addr, MemOpIdx oi, TCGType data_type) 1676{ 1677 MemOp opc = get_memop(oi); 1678 TCGLabelQemuLdst *ldst; 1679 HostAddress h; 1680 1681 ldst = prepare_host_addr(s, &h, addr, oi, false); 1682 if (ldst) { 1683 ldst->type = data_type; 1684 ldst->datalo_reg = datalo; 1685 ldst->datahi_reg = datahi; 1686 1687 h.cond = COND_EQ; 1688 tcg_out_qemu_st_direct(s, opc, datalo, datahi, h); 1689 1690 /* The conditional call is last, as we're going to return here. */ 1691 ldst->label_ptr[0] = s->code_ptr; 1692 tcg_out_bl_imm(s, COND_NE, 0); 1693 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1694 } else { 1695 tcg_out_qemu_st_direct(s, opc, datalo, datahi, h); 1696 } 1697} 1698 1699static void tcg_out_epilogue(TCGContext *s); 1700 1701static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) 1702{ 1703 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, arg); 1704 tcg_out_epilogue(s); 1705} 1706 1707static void tcg_out_goto_tb(TCGContext *s, int which) 1708{ 1709 uintptr_t i_addr; 1710 intptr_t i_disp; 1711 1712 /* Direct branch will be patched by tb_target_set_jmp_target. */ 1713 set_jmp_insn_offset(s, which); 1714 tcg_out32(s, INSN_NOP); 1715 1716 /* When branch is out of range, fall through to indirect. */ 1717 i_addr = get_jmp_target_addr(s, which); 1718 i_disp = tcg_pcrel_diff(s, (void *)i_addr) - 8; 1719 tcg_debug_assert(i_disp < 0); 1720 if (i_disp >= -0xfff) { 1721 tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, i_disp); 1722 } else { 1723 /* 1724 * The TB is close, but outside the 12 bits addressable by 1725 * the load. We can extend this to 20 bits with a sub of a 1726 * shifted immediate from pc. 1727 */ 1728 int h = -i_disp; 1729 int l = -(h & 0xfff); 1730 1731 h = encode_imm_nofail(h + l); 1732 tcg_out_dat_imm(s, COND_AL, ARITH_SUB, TCG_REG_R0, TCG_REG_PC, h); 1733 tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_R0, l); 1734 } 1735 set_jmp_reset_offset(s, which); 1736} 1737 1738static void tcg_out_goto_ptr(TCGContext *s, TCGReg a0) 1739{ 1740 tcg_out_b_reg(s, COND_AL, a0); 1741} 1742 1743void tb_target_set_jmp_target(const TranslationBlock *tb, int n, 1744 uintptr_t jmp_rx, uintptr_t jmp_rw) 1745{ 1746 uintptr_t addr = tb->jmp_target_addr[n]; 1747 ptrdiff_t offset = addr - (jmp_rx + 8); 1748 tcg_insn_unit insn; 1749 1750 /* Either directly branch, or fall through to indirect branch. */ 1751 if (offset == sextract64(offset, 0, 26)) { 1752 /* B <addr> */ 1753 insn = deposit32((COND_AL << 28) | INSN_B, 0, 24, offset >> 2); 1754 } else { 1755 insn = INSN_NOP; 1756 } 1757 1758 qatomic_set((uint32_t *)jmp_rw, insn); 1759 flush_idcache_range(jmp_rx, jmp_rw, 4); 1760} 1761 1762 1763static void tgen_add(TCGContext *s, TCGType type, 1764 TCGReg a0, TCGReg a1, TCGReg a2) 1765{ 1766 tcg_out_dat_reg(s, COND_AL, ARITH_ADD, a0, a1, a2, SHIFT_IMM_LSL(0)); 1767} 1768 1769static void tgen_addi(TCGContext *s, TCGType type, 1770 TCGReg a0, TCGReg a1, tcg_target_long a2) 1771{ 1772 tcg_out_dat_IN(s, COND_AL, ARITH_ADD, ARITH_SUB, a0, a1, a2); 1773} 1774 1775static const TCGOutOpBinary outop_add = { 1776 .base.static_constraint = C_O1_I2(r, r, rIN), 1777 .out_rrr = tgen_add, 1778 .out_rri = tgen_addi, 1779}; 1780 1781static void tgen_addco(TCGContext *s, TCGType type, 1782 TCGReg a0, TCGReg a1, TCGReg a2) 1783{ 1784 tcg_out_dat_reg(s, COND_AL, ARITH_ADD | TO_CPSR, 1785 a0, a1, a2, SHIFT_IMM_LSL(0)); 1786} 1787 1788static void tgen_addco_imm(TCGContext *s, TCGType type, 1789 TCGReg a0, TCGReg a1, tcg_target_long a2) 1790{ 1791 tcg_out_dat_IN(s, COND_AL, ARITH_ADD | TO_CPSR, ARITH_SUB | TO_CPSR, 1792 a0, a1, a2); 1793} 1794 1795static const TCGOutOpBinary outop_addco = { 1796 .base.static_constraint = C_O1_I2(r, r, rIN), 1797 .out_rrr = tgen_addco, 1798 .out_rri = tgen_addco_imm, 1799}; 1800 1801static void tgen_addci(TCGContext *s, TCGType type, 1802 TCGReg a0, TCGReg a1, TCGReg a2) 1803{ 1804 tcg_out_dat_reg(s, COND_AL, ARITH_ADC, a0, a1, a2, SHIFT_IMM_LSL(0)); 1805} 1806 1807static void tgen_addci_imm(TCGContext *s, TCGType type, 1808 TCGReg a0, TCGReg a1, tcg_target_long a2) 1809{ 1810 tcg_out_dat_IK(s, COND_AL, ARITH_ADC, ARITH_SBC, a0, a1, a2); 1811} 1812 1813static const TCGOutOpAddSubCarry outop_addci = { 1814 .base.static_constraint = C_O1_I2(r, r, rIK), 1815 .out_rrr = tgen_addci, 1816 .out_rri = tgen_addci_imm, 1817}; 1818 1819static void tgen_addcio(TCGContext *s, TCGType type, 1820 TCGReg a0, TCGReg a1, TCGReg a2) 1821{ 1822 tcg_out_dat_reg(s, COND_AL, ARITH_ADC | TO_CPSR, 1823 a0, a1, a2, SHIFT_IMM_LSL(0)); 1824} 1825 1826static void tgen_addcio_imm(TCGContext *s, TCGType type, 1827 TCGReg a0, TCGReg a1, tcg_target_long a2) 1828{ 1829 tcg_out_dat_IK(s, COND_AL, ARITH_ADC | TO_CPSR, ARITH_SBC | TO_CPSR, 1830 a0, a1, a2); 1831} 1832 1833static const TCGOutOpBinary outop_addcio = { 1834 .base.static_constraint = C_O1_I2(r, r, rIK), 1835 .out_rrr = tgen_addcio, 1836 .out_rri = tgen_addcio_imm, 1837}; 1838 1839/* Set C to @c; NZVQ all set to 0. */ 1840static void tcg_out_movi_apsr_c(TCGContext *s, bool c) 1841{ 1842 int imm12 = encode_imm_nofail(c << 29); 1843 tcg_out32(s, (COND_AL << 28) | INSN_MSRI_CPSR | 0x80000 | imm12); 1844} 1845 1846static void tcg_out_set_carry(TCGContext *s) 1847{ 1848 tcg_out_movi_apsr_c(s, 1); 1849} 1850 1851static void tgen_and(TCGContext *s, TCGType type, 1852 TCGReg a0, TCGReg a1, TCGReg a2) 1853{ 1854 tcg_out_dat_reg(s, COND_AL, ARITH_AND, a0, a1, a2, SHIFT_IMM_LSL(0)); 1855} 1856 1857static void tgen_andi(TCGContext *s, TCGType type, 1858 TCGReg a0, TCGReg a1, tcg_target_long a2) 1859{ 1860 tcg_out_dat_IK(s, COND_AL, ARITH_AND, ARITH_BIC, a0, a1, a2); 1861} 1862 1863static const TCGOutOpBinary outop_and = { 1864 .base.static_constraint = C_O1_I2(r, r, rIK), 1865 .out_rrr = tgen_and, 1866 .out_rri = tgen_andi, 1867}; 1868 1869static void tgen_andc(TCGContext *s, TCGType type, 1870 TCGReg a0, TCGReg a1, TCGReg a2) 1871{ 1872 tcg_out_dat_reg(s, COND_AL, ARITH_BIC, a0, a1, a2, SHIFT_IMM_LSL(0)); 1873} 1874 1875static const TCGOutOpBinary outop_andc = { 1876 .base.static_constraint = C_O1_I2(r, r, r), 1877 .out_rrr = tgen_andc, 1878}; 1879 1880static void tgen_clz(TCGContext *s, TCGType type, 1881 TCGReg a0, TCGReg a1, TCGReg a2) 1882{ 1883 tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0); 1884 tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0); 1885 tcg_out_mov_reg(s, COND_EQ, a0, a2); 1886} 1887 1888static void tgen_clzi(TCGContext *s, TCGType type, 1889 TCGReg a0, TCGReg a1, tcg_target_long a2) 1890{ 1891 if (a2 == 32) { 1892 tcg_out_dat_reg(s, COND_AL, INSN_CLZ, a0, 0, a1, 0); 1893 } else { 1894 tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0); 1895 tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0); 1896 tcg_out_movi32(s, COND_EQ, a0, a2); 1897 } 1898} 1899 1900static const TCGOutOpBinary outop_clz = { 1901 .base.static_constraint = C_O1_I2(r, r, rIK), 1902 .out_rrr = tgen_clz, 1903 .out_rri = tgen_clzi, 1904}; 1905 1906static const TCGOutOpUnary outop_ctpop = { 1907 .base.static_constraint = C_NotImplemented, 1908}; 1909 1910static void tgen_ctz(TCGContext *s, TCGType type, 1911 TCGReg a0, TCGReg a1, TCGReg a2) 1912{ 1913 tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, a1, 0); 1914 tgen_clz(s, TCG_TYPE_I32, a0, TCG_REG_TMP, a2); 1915} 1916 1917static void tgen_ctzi(TCGContext *s, TCGType type, 1918 TCGReg a0, TCGReg a1, tcg_target_long a2) 1919{ 1920 tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, a1, 0); 1921 tgen_clzi(s, TCG_TYPE_I32, a0, TCG_REG_TMP, a2); 1922} 1923 1924static TCGConstraintSetIndex cset_ctz(TCGType type, unsigned flags) 1925{ 1926 return use_armv7_instructions ? C_O1_I2(r, r, rIK) : C_NotImplemented; 1927} 1928 1929static const TCGOutOpBinary outop_ctz = { 1930 .base.static_constraint = C_Dynamic, 1931 .base.dynamic_constraint = cset_ctz, 1932 .out_rrr = tgen_ctz, 1933 .out_rri = tgen_ctzi, 1934}; 1935 1936static TCGConstraintSetIndex cset_idiv(TCGType type, unsigned flags) 1937{ 1938 return use_idiv_instructions ? C_O1_I2(r, r, r) : C_NotImplemented; 1939} 1940 1941static void tgen_divs(TCGContext *s, TCGType type, 1942 TCGReg a0, TCGReg a1, TCGReg a2) 1943{ 1944 /* sdiv */ 1945 tcg_out32(s, 0x0710f010 | (COND_AL << 28) | (a0 << 16) | a1 | (a2 << 8)); 1946} 1947 1948static const TCGOutOpBinary outop_divs = { 1949 .base.static_constraint = C_Dynamic, 1950 .base.dynamic_constraint = cset_idiv, 1951 .out_rrr = tgen_divs, 1952}; 1953 1954static const TCGOutOpDivRem outop_divs2 = { 1955 .base.static_constraint = C_NotImplemented, 1956}; 1957 1958static void tgen_divu(TCGContext *s, TCGType type, 1959 TCGReg a0, TCGReg a1, TCGReg a2) 1960{ 1961 /* udiv */ 1962 tcg_out32(s, 0x0730f010 | (COND_AL << 28) | (a0 << 16) | a1 | (a2 << 8)); 1963} 1964 1965static const TCGOutOpBinary outop_divu = { 1966 .base.static_constraint = C_Dynamic, 1967 .base.dynamic_constraint = cset_idiv, 1968 .out_rrr = tgen_divu, 1969}; 1970 1971static const TCGOutOpDivRem outop_divu2 = { 1972 .base.static_constraint = C_NotImplemented, 1973}; 1974 1975static const TCGOutOpBinary outop_eqv = { 1976 .base.static_constraint = C_NotImplemented, 1977}; 1978 1979static void tgen_mul(TCGContext *s, TCGType type, 1980 TCGReg a0, TCGReg a1, TCGReg a2) 1981{ 1982 /* mul */ 1983 tcg_out32(s, (COND_AL << 28) | 0x90 | (a0 << 16) | (a1 << 8) | a2); 1984} 1985 1986static const TCGOutOpBinary outop_mul = { 1987 .base.static_constraint = C_O1_I2(r, r, r), 1988 .out_rrr = tgen_mul, 1989}; 1990 1991static void tgen_muls2(TCGContext *s, TCGType type, 1992 TCGReg rd0, TCGReg rd1, TCGReg rn, TCGReg rm) 1993{ 1994 /* smull */ 1995 tcg_out32(s, (COND_AL << 28) | 0x00c00090 | 1996 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn); 1997} 1998 1999static const TCGOutOpMul2 outop_muls2 = { 2000 .base.static_constraint = C_O2_I2(r, r, r, r), 2001 .out_rrrr = tgen_muls2, 2002}; 2003 2004static const TCGOutOpBinary outop_mulsh = { 2005 .base.static_constraint = C_NotImplemented, 2006}; 2007 2008static void tgen_mulu2(TCGContext *s, TCGType type, 2009 TCGReg rd0, TCGReg rd1, TCGReg rn, TCGReg rm) 2010{ 2011 /* umull */ 2012 tcg_out32(s, (COND_AL << 28) | 0x00800090 | 2013 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn); 2014} 2015 2016static const TCGOutOpMul2 outop_mulu2 = { 2017 .base.static_constraint = C_O2_I2(r, r, r, r), 2018 .out_rrrr = tgen_mulu2, 2019}; 2020 2021static const TCGOutOpBinary outop_muluh = { 2022 .base.static_constraint = C_NotImplemented, 2023}; 2024 2025static const TCGOutOpBinary outop_nand = { 2026 .base.static_constraint = C_NotImplemented, 2027}; 2028 2029static const TCGOutOpBinary outop_nor = { 2030 .base.static_constraint = C_NotImplemented, 2031}; 2032 2033static void tgen_or(TCGContext *s, TCGType type, 2034 TCGReg a0, TCGReg a1, TCGReg a2) 2035{ 2036 tcg_out_dat_reg(s, COND_AL, ARITH_ORR, a0, a1, a2, SHIFT_IMM_LSL(0)); 2037} 2038 2039static void tgen_ori(TCGContext *s, TCGType type, 2040 TCGReg a0, TCGReg a1, tcg_target_long a2) 2041{ 2042 tcg_out_dat_imm(s, COND_AL, ARITH_ORR, a0, a1, encode_imm_nofail(a2)); 2043} 2044 2045static const TCGOutOpBinary outop_or = { 2046 .base.static_constraint = C_O1_I2(r, r, rI), 2047 .out_rrr = tgen_or, 2048 .out_rri = tgen_ori, 2049}; 2050 2051static const TCGOutOpBinary outop_orc = { 2052 .base.static_constraint = C_NotImplemented, 2053}; 2054 2055static const TCGOutOpBinary outop_rems = { 2056 .base.static_constraint = C_NotImplemented, 2057}; 2058 2059static const TCGOutOpBinary outop_remu = { 2060 .base.static_constraint = C_NotImplemented, 2061}; 2062 2063static const TCGOutOpBinary outop_rotl = { 2064 .base.static_constraint = C_NotImplemented, 2065}; 2066 2067static void tgen_rotr(TCGContext *s, TCGType type, 2068 TCGReg a0, TCGReg a1, TCGReg a2) 2069{ 2070 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_ROR(a2)); 2071} 2072 2073static void tgen_rotri(TCGContext *s, TCGType type, 2074 TCGReg a0, TCGReg a1, tcg_target_long a2) 2075{ 2076 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_IMM_ROR(a2 & 0x1f)); 2077} 2078 2079static const TCGOutOpBinary outop_rotr = { 2080 .base.static_constraint = C_O1_I2(r, r, ri), 2081 .out_rrr = tgen_rotr, 2082 .out_rri = tgen_rotri, 2083}; 2084 2085static void tgen_sar(TCGContext *s, TCGType type, 2086 TCGReg a0, TCGReg a1, TCGReg a2) 2087{ 2088 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_ASR(a2)); 2089} 2090 2091static void tgen_sari(TCGContext *s, TCGType type, 2092 TCGReg a0, TCGReg a1, tcg_target_long a2) 2093{ 2094 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, 2095 SHIFT_IMM_ASR(a2 & 0x1f)); 2096} 2097 2098static const TCGOutOpBinary outop_sar = { 2099 .base.static_constraint = C_O1_I2(r, r, ri), 2100 .out_rrr = tgen_sar, 2101 .out_rri = tgen_sari, 2102}; 2103 2104static void tgen_shl(TCGContext *s, TCGType type, 2105 TCGReg a0, TCGReg a1, TCGReg a2) 2106{ 2107 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_LSL(a2)); 2108} 2109 2110static void tgen_shli(TCGContext *s, TCGType type, 2111 TCGReg a0, TCGReg a1, tcg_target_long a2) 2112{ 2113 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, 2114 SHIFT_IMM_LSL(a2 & 0x1f)); 2115} 2116 2117static const TCGOutOpBinary outop_shl = { 2118 .base.static_constraint = C_O1_I2(r, r, ri), 2119 .out_rrr = tgen_shl, 2120 .out_rri = tgen_shli, 2121}; 2122 2123static void tgen_shr(TCGContext *s, TCGType type, 2124 TCGReg a0, TCGReg a1, TCGReg a2) 2125{ 2126 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_LSR(a2)); 2127} 2128 2129static void tgen_shri(TCGContext *s, TCGType type, 2130 TCGReg a0, TCGReg a1, tcg_target_long a2) 2131{ 2132 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, 2133 SHIFT_IMM_LSR(a2 & 0x1f)); 2134} 2135 2136static const TCGOutOpBinary outop_shr = { 2137 .base.static_constraint = C_O1_I2(r, r, ri), 2138 .out_rrr = tgen_shr, 2139 .out_rri = tgen_shri, 2140}; 2141 2142static void tgen_sub(TCGContext *s, TCGType type, 2143 TCGReg a0, TCGReg a1, TCGReg a2) 2144{ 2145 tcg_out_dat_reg(s, COND_AL, ARITH_SUB, a0, a1, a2, SHIFT_IMM_LSL(0)); 2146} 2147 2148static void tgen_subfi(TCGContext *s, TCGType type, 2149 TCGReg a0, tcg_target_long a1, TCGReg a2) 2150{ 2151 tcg_out_dat_imm(s, COND_AL, ARITH_RSB, a0, a2, encode_imm_nofail(a1)); 2152} 2153 2154static const TCGOutOpSubtract outop_sub = { 2155 .base.static_constraint = C_O1_I2(r, rI, r), 2156 .out_rrr = tgen_sub, 2157 .out_rir = tgen_subfi, 2158}; 2159 2160static void tgen_subbo_rrr(TCGContext *s, TCGType type, 2161 TCGReg a0, TCGReg a1, TCGReg a2) 2162{ 2163 tcg_out_dat_reg(s, COND_AL, ARITH_SUB | TO_CPSR, 2164 a0, a1, a2, SHIFT_IMM_LSL(0)); 2165} 2166 2167static void tgen_subbo_rri(TCGContext *s, TCGType type, 2168 TCGReg a0, TCGReg a1, tcg_target_long a2) 2169{ 2170 tcg_out_dat_IN(s, COND_AL, ARITH_SUB | TO_CPSR, ARITH_ADD | TO_CPSR, 2171 a0, a1, a2); 2172} 2173 2174static void tgen_subbo_rir(TCGContext *s, TCGType type, 2175 TCGReg a0, tcg_target_long a1, TCGReg a2) 2176{ 2177 tcg_out_dat_imm(s, COND_AL, ARITH_RSB | TO_CPSR, 2178 a0, a2, encode_imm_nofail(a1)); 2179} 2180 2181static void tgen_subbo_rii(TCGContext *s, TCGType type, 2182 TCGReg a0, tcg_target_long a1, tcg_target_long a2) 2183{ 2184 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2); 2185 tgen_subbo_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP); 2186} 2187 2188static const TCGOutOpAddSubCarry outop_subbo = { 2189 .base.static_constraint = C_O1_I2(r, rI, rIN), 2190 .out_rrr = tgen_subbo_rrr, 2191 .out_rri = tgen_subbo_rri, 2192 .out_rir = tgen_subbo_rir, 2193 .out_rii = tgen_subbo_rii, 2194}; 2195 2196static void tgen_subbi_rrr(TCGContext *s, TCGType type, 2197 TCGReg a0, TCGReg a1, TCGReg a2) 2198{ 2199 tcg_out_dat_reg(s, COND_AL, ARITH_SBC, 2200 a0, a1, a2, SHIFT_IMM_LSL(0)); 2201} 2202 2203static void tgen_subbi_rri(TCGContext *s, TCGType type, 2204 TCGReg a0, TCGReg a1, tcg_target_long a2) 2205{ 2206 tcg_out_dat_IK(s, COND_AL, ARITH_SBC, ARITH_ADC, a0, a1, a2); 2207} 2208 2209static void tgen_subbi_rir(TCGContext *s, TCGType type, 2210 TCGReg a0, tcg_target_long a1, TCGReg a2) 2211{ 2212 tcg_out_dat_imm(s, COND_AL, ARITH_RSC, a0, a2, encode_imm_nofail(a1)); 2213} 2214 2215static void tgen_subbi_rii(TCGContext *s, TCGType type, 2216 TCGReg a0, tcg_target_long a1, tcg_target_long a2) 2217{ 2218 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2); 2219 tgen_subbi_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP); 2220} 2221 2222static const TCGOutOpAddSubCarry outop_subbi = { 2223 .base.static_constraint = C_O1_I2(r, rI, rIK), 2224 .out_rrr = tgen_subbi_rrr, 2225 .out_rri = tgen_subbi_rri, 2226 .out_rir = tgen_subbi_rir, 2227 .out_rii = tgen_subbi_rii, 2228}; 2229 2230static void tgen_subbio_rrr(TCGContext *s, TCGType type, 2231 TCGReg a0, TCGReg a1, TCGReg a2) 2232{ 2233 tcg_out_dat_reg(s, COND_AL, ARITH_SBC | TO_CPSR, 2234 a0, a1, a2, SHIFT_IMM_LSL(0)); 2235} 2236 2237static void tgen_subbio_rri(TCGContext *s, TCGType type, 2238 TCGReg a0, TCGReg a1, tcg_target_long a2) 2239{ 2240 tcg_out_dat_IK(s, COND_AL, ARITH_SBC | TO_CPSR, ARITH_ADC | TO_CPSR, 2241 a0, a1, a2); 2242} 2243 2244static void tgen_subbio_rir(TCGContext *s, TCGType type, 2245 TCGReg a0, tcg_target_long a1, TCGReg a2) 2246{ 2247 tcg_out_dat_imm(s, COND_AL, ARITH_RSC | TO_CPSR, 2248 a0, a2, encode_imm_nofail(a1)); 2249} 2250 2251static void tgen_subbio_rii(TCGContext *s, TCGType type, 2252 TCGReg a0, tcg_target_long a1, tcg_target_long a2) 2253{ 2254 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2); 2255 tgen_subbio_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP); 2256} 2257 2258static const TCGOutOpAddSubCarry outop_subbio = { 2259 .base.static_constraint = C_O1_I2(r, rI, rIK), 2260 .out_rrr = tgen_subbio_rrr, 2261 .out_rri = tgen_subbio_rri, 2262 .out_rir = tgen_subbio_rir, 2263 .out_rii = tgen_subbio_rii, 2264}; 2265 2266static void tcg_out_set_borrow(TCGContext *s) 2267{ 2268 tcg_out_movi_apsr_c(s, 0); /* borrow = !carry */ 2269} 2270 2271static void tgen_xor(TCGContext *s, TCGType type, 2272 TCGReg a0, TCGReg a1, TCGReg a2) 2273{ 2274 tcg_out_dat_reg(s, COND_AL, ARITH_EOR, a0, a1, a2, SHIFT_IMM_LSL(0)); 2275} 2276 2277static void tgen_xori(TCGContext *s, TCGType type, 2278 TCGReg a0, TCGReg a1, tcg_target_long a2) 2279{ 2280 tcg_out_dat_imm(s, COND_AL, ARITH_EOR, a0, a1, encode_imm_nofail(a2)); 2281} 2282 2283static const TCGOutOpBinary outop_xor = { 2284 .base.static_constraint = C_O1_I2(r, r, rI), 2285 .out_rrr = tgen_xor, 2286 .out_rri = tgen_xori, 2287}; 2288 2289static void tgen_bswap16(TCGContext *s, TCGType type, 2290 TCGReg rd, TCGReg rn, unsigned flags) 2291{ 2292 if (flags & TCG_BSWAP_OS) { 2293 /* revsh */ 2294 tcg_out32(s, 0x06ff0fb0 | (COND_AL << 28) | (rd << 12) | rn); 2295 return; 2296 } 2297 2298 /* rev16 */ 2299 tcg_out32(s, 0x06bf0fb0 | (COND_AL << 28) | (rd << 12) | rn); 2300 if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) { 2301 tcg_out_ext16u(s, rd, rd); 2302 } 2303} 2304 2305static const TCGOutOpBswap outop_bswap16 = { 2306 .base.static_constraint = C_O1_I1(r, r), 2307 .out_rr = tgen_bswap16, 2308}; 2309 2310static void tgen_bswap32(TCGContext *s, TCGType type, 2311 TCGReg rd, TCGReg rn, unsigned flags) 2312{ 2313 /* rev */ 2314 tcg_out32(s, 0x06bf0f30 | (COND_AL << 28) | (rd << 12) | rn); 2315} 2316 2317static const TCGOutOpBswap outop_bswap32 = { 2318 .base.static_constraint = C_O1_I1(r, r), 2319 .out_rr = tgen_bswap32, 2320}; 2321 2322static const TCGOutOpUnary outop_bswap64 = { 2323 .base.static_constraint = C_NotImplemented, 2324}; 2325 2326static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 2327{ 2328 tgen_subfi(s, type, a0, 0, a1); 2329} 2330 2331static const TCGOutOpUnary outop_neg = { 2332 .base.static_constraint = C_O1_I1(r, r), 2333 .out_rr = tgen_neg, 2334}; 2335 2336static void tgen_not(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 2337{ 2338 tcg_out_dat_reg(s, COND_AL, ARITH_MVN, a0, 0, a1, SHIFT_IMM_LSL(0)); 2339} 2340 2341static const TCGOutOpUnary outop_not = { 2342 .base.static_constraint = C_O1_I1(r, r), 2343 .out_rr = tgen_not, 2344}; 2345 2346static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond, 2347 TCGReg a0, TCGReg a1, TCGLabel *l) 2348{ 2349 cond = tgen_cmp(s, cond, a0, a1); 2350 tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l); 2351} 2352 2353static void tgen_brcondi(TCGContext *s, TCGType type, TCGCond cond, 2354 TCGReg a0, tcg_target_long a1, TCGLabel *l) 2355{ 2356 cond = tgen_cmpi(s, cond, a0, a1); 2357 tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l); 2358} 2359 2360static const TCGOutOpBrcond outop_brcond = { 2361 .base.static_constraint = C_O0_I2(r, rIN), 2362 .out_rr = tgen_brcond, 2363 .out_ri = tgen_brcondi, 2364}; 2365 2366static void finish_setcond(TCGContext *s, TCGCond cond, TCGReg ret, bool neg) 2367{ 2368 tcg_out_movi32(s, tcg_cond_to_arm_cond[tcg_invert_cond(cond)], ret, 0); 2369 tcg_out_movi32(s, tcg_cond_to_arm_cond[cond], ret, neg ? -1 : 1); 2370} 2371 2372static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, 2373 TCGReg a0, TCGReg a1, TCGReg a2) 2374{ 2375 cond = tgen_cmp(s, cond, a1, a2); 2376 finish_setcond(s, cond, a0, false); 2377} 2378 2379static void tgen_setcondi(TCGContext *s, TCGType type, TCGCond cond, 2380 TCGReg a0, TCGReg a1, tcg_target_long a2) 2381{ 2382 cond = tgen_cmpi(s, cond, a1, a2); 2383 finish_setcond(s, cond, a0, false); 2384} 2385 2386static const TCGOutOpSetcond outop_setcond = { 2387 .base.static_constraint = C_O1_I2(r, r, rIN), 2388 .out_rrr = tgen_setcond, 2389 .out_rri = tgen_setcondi, 2390}; 2391 2392static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, 2393 TCGReg a0, TCGReg a1, TCGReg a2) 2394{ 2395 cond = tgen_cmp(s, cond, a1, a2); 2396 finish_setcond(s, cond, a0, true); 2397} 2398 2399static void tgen_negsetcondi(TCGContext *s, TCGType type, TCGCond cond, 2400 TCGReg a0, TCGReg a1, tcg_target_long a2) 2401{ 2402 cond = tgen_cmpi(s, cond, a1, a2); 2403 finish_setcond(s, cond, a0, true); 2404} 2405 2406static const TCGOutOpSetcond outop_negsetcond = { 2407 .base.static_constraint = C_O1_I2(r, r, rIN), 2408 .out_rrr = tgen_negsetcond, 2409 .out_rri = tgen_negsetcondi, 2410}; 2411 2412static void tgen_movcond(TCGContext *s, TCGType type, TCGCond cond, 2413 TCGReg ret, TCGReg c1, TCGArg c2, bool const_c2, 2414 TCGArg vt, bool const_vt, TCGArg vf, bool consf_vf) 2415{ 2416 cond = tcg_out_cmp(s, cond, c1, c2, const_c2); 2417 tcg_out_dat_rIK(s, tcg_cond_to_arm_cond[cond], ARITH_MOV, ARITH_MVN, 2418 ret, 0, vt, const_vt); 2419} 2420 2421static const TCGOutOpMovcond outop_movcond = { 2422 .base.static_constraint = C_O1_I4(r, r, rIN, rIK, 0), 2423 .out = tgen_movcond, 2424}; 2425 2426static void tgen_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, 2427 TCGArg bl, bool const_bl, TCGArg bh, bool const_bh, 2428 TCGLabel *l) 2429{ 2430 cond = tcg_out_cmp2(s, cond, al, ah, bl, const_bl, bh, const_bh); 2431 tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l); 2432} 2433 2434static const TCGOutOpBrcond2 outop_brcond2 = { 2435 .base.static_constraint = C_O0_I4(r, r, rI, rI), 2436 .out = tgen_brcond2, 2437}; 2438 2439static void tgen_setcond2(TCGContext *s, TCGCond cond, TCGReg ret, 2440 TCGReg al, TCGReg ah, 2441 TCGArg bl, bool const_bl, 2442 TCGArg bh, bool const_bh) 2443{ 2444 cond = tcg_out_cmp2(s, cond, al, ah, bl, const_bl, bh, const_bh); 2445 finish_setcond(s, cond, ret, false); 2446} 2447 2448static const TCGOutOpSetcond2 outop_setcond2 = { 2449 .base.static_constraint = C_O1_I4(r, r, r, rI, rI), 2450 .out = tgen_setcond2, 2451}; 2452 2453static void tgen_extract2(TCGContext *s, TCGType type, TCGReg a0, 2454 TCGReg a1, TCGReg a2, unsigned shr) 2455{ 2456 /* We can do extract2 in 2 insns, vs the 3 required otherwise. */ 2457 tgen_shli(s, TCG_TYPE_I32, TCG_REG_TMP, a2, 32 - shr); 2458 tcg_out_dat_reg(s, COND_AL, ARITH_ORR, a0, TCG_REG_TMP, 2459 a1, SHIFT_IMM_LSR(shr)); 2460} 2461 2462static const TCGOutOpExtract2 outop_extract2 = { 2463 .base.static_constraint = C_O1_I2(r, r, r), 2464 .out_rrr = tgen_extract2, 2465}; 2466 2467static void tgen_ld8u(TCGContext *s, TCGType type, TCGReg rd, 2468 TCGReg rn, ptrdiff_t offset) 2469{ 2470 if (offset > 0xfff || offset < -0xfff) { 2471 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset); 2472 tcg_out_ld8_r(s, COND_AL, rd, rn, TCG_REG_TMP); 2473 } else { 2474 tcg_out_ld8_12(s, COND_AL, rd, rn, offset); 2475 } 2476} 2477 2478static const TCGOutOpLoad outop_ld8u = { 2479 .base.static_constraint = C_O1_I1(r, r), 2480 .out = tgen_ld8u, 2481}; 2482 2483static void tgen_ld8s(TCGContext *s, TCGType type, TCGReg rd, 2484 TCGReg rn, ptrdiff_t offset) 2485{ 2486 if (offset > 0xff || offset < -0xff) { 2487 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset); 2488 tcg_out_ld8s_r(s, COND_AL, rd, rn, TCG_REG_TMP); 2489 } else { 2490 tcg_out_ld8s_8(s, COND_AL, rd, rn, offset); 2491 } 2492} 2493 2494static const TCGOutOpLoad outop_ld8s = { 2495 .base.static_constraint = C_O1_I1(r, r), 2496 .out = tgen_ld8s, 2497}; 2498 2499static void tgen_ld16u(TCGContext *s, TCGType type, TCGReg rd, 2500 TCGReg rn, ptrdiff_t offset) 2501{ 2502 if (offset > 0xff || offset < -0xff) { 2503 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset); 2504 tcg_out_ld16u_r(s, COND_AL, rd, rn, TCG_REG_TMP); 2505 } else { 2506 tcg_out_ld16u_8(s, COND_AL, rd, rn, offset); 2507 } 2508} 2509 2510static const TCGOutOpLoad outop_ld16u = { 2511 .base.static_constraint = C_O1_I1(r, r), 2512 .out = tgen_ld16u, 2513}; 2514 2515static void tgen_ld16s(TCGContext *s, TCGType type, TCGReg rd, 2516 TCGReg rn, ptrdiff_t offset) 2517{ 2518 if (offset > 0xff || offset < -0xff) { 2519 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset); 2520 tcg_out_ld16s_r(s, COND_AL, rd, rn, TCG_REG_TMP); 2521 } else { 2522 tcg_out_ld16s_8(s, COND_AL, rd, rn, offset); 2523 } 2524} 2525 2526static const TCGOutOpLoad outop_ld16s = { 2527 .base.static_constraint = C_O1_I1(r, r), 2528 .out = tgen_ld16s, 2529}; 2530 2531static void tgen_st8(TCGContext *s, TCGType type, TCGReg rd, 2532 TCGReg rn, ptrdiff_t offset) 2533{ 2534 if (offset > 0xfff || offset < -0xfff) { 2535 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset); 2536 tcg_out_st8_r(s, COND_AL, rd, rn, TCG_REG_TMP); 2537 } else { 2538 tcg_out_st8_12(s, COND_AL, rd, rn, offset); 2539 } 2540} 2541 2542static const TCGOutOpStore outop_st8 = { 2543 .base.static_constraint = C_O0_I2(r, r), 2544 .out_r = tgen_st8, 2545}; 2546 2547static void tgen_st16(TCGContext *s, TCGType type, TCGReg rd, 2548 TCGReg rn, ptrdiff_t offset) 2549{ 2550 if (offset > 0xff || offset < -0xff) { 2551 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, offset); 2552 tcg_out_st16_r(s, COND_AL, rd, rn, TCG_REG_TMP); 2553 } else { 2554 tcg_out_st16_8(s, COND_AL, rd, rn, offset); 2555 } 2556} 2557 2558static const TCGOutOpStore outop_st16 = { 2559 .base.static_constraint = C_O0_I2(r, r), 2560 .out_r = tgen_st16, 2561}; 2562 2563static const TCGOutOpStore outop_st = { 2564 .base.static_constraint = C_O0_I2(r, r), 2565 .out_r = tcg_out_st, 2566}; 2567 2568static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type, 2569 const TCGArg args[TCG_MAX_OP_ARGS], 2570 const int const_args[TCG_MAX_OP_ARGS]) 2571{ 2572 switch (opc) { 2573 case INDEX_op_qemu_ld_i32: 2574 tcg_out_qemu_ld(s, args[0], -1, args[1], args[2], TCG_TYPE_I32); 2575 break; 2576 case INDEX_op_qemu_ld_i64: 2577 tcg_out_qemu_ld(s, args[0], args[1], args[2], args[3], TCG_TYPE_I64); 2578 break; 2579 2580 case INDEX_op_qemu_st_i32: 2581 tcg_out_qemu_st(s, args[0], -1, args[1], args[2], TCG_TYPE_I32); 2582 break; 2583 case INDEX_op_qemu_st_i64: 2584 tcg_out_qemu_st(s, args[0], args[1], args[2], args[3], TCG_TYPE_I64); 2585 break; 2586 2587 case INDEX_op_call: /* Always emitted via tcg_out_call. */ 2588 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */ 2589 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */ 2590 default: 2591 g_assert_not_reached(); 2592 } 2593} 2594 2595static TCGConstraintSetIndex 2596tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) 2597{ 2598 switch (op) { 2599 case INDEX_op_qemu_ld_i32: 2600 return C_O1_I1(r, q); 2601 case INDEX_op_qemu_ld_i64: 2602 return C_O2_I1(e, p, q); 2603 case INDEX_op_qemu_st_i32: 2604 return C_O0_I2(q, q); 2605 case INDEX_op_qemu_st_i64: 2606 return C_O0_I3(Q, p, q); 2607 2608 case INDEX_op_st_vec: 2609 return C_O0_I2(w, r); 2610 case INDEX_op_ld_vec: 2611 case INDEX_op_dupm_vec: 2612 return C_O1_I1(w, r); 2613 case INDEX_op_dup_vec: 2614 return C_O1_I1(w, wr); 2615 case INDEX_op_abs_vec: 2616 case INDEX_op_neg_vec: 2617 case INDEX_op_not_vec: 2618 case INDEX_op_shli_vec: 2619 case INDEX_op_shri_vec: 2620 case INDEX_op_sari_vec: 2621 return C_O1_I1(w, w); 2622 case INDEX_op_dup2_vec: 2623 case INDEX_op_add_vec: 2624 case INDEX_op_mul_vec: 2625 case INDEX_op_smax_vec: 2626 case INDEX_op_smin_vec: 2627 case INDEX_op_ssadd_vec: 2628 case INDEX_op_sssub_vec: 2629 case INDEX_op_sub_vec: 2630 case INDEX_op_umax_vec: 2631 case INDEX_op_umin_vec: 2632 case INDEX_op_usadd_vec: 2633 case INDEX_op_ussub_vec: 2634 case INDEX_op_xor_vec: 2635 case INDEX_op_arm_sshl_vec: 2636 case INDEX_op_arm_ushl_vec: 2637 return C_O1_I2(w, w, w); 2638 case INDEX_op_arm_sli_vec: 2639 return C_O1_I2(w, 0, w); 2640 case INDEX_op_or_vec: 2641 case INDEX_op_andc_vec: 2642 return C_O1_I2(w, w, wO); 2643 case INDEX_op_and_vec: 2644 case INDEX_op_orc_vec: 2645 return C_O1_I2(w, w, wV); 2646 case INDEX_op_cmp_vec: 2647 return C_O1_I2(w, w, wZ); 2648 case INDEX_op_bitsel_vec: 2649 return C_O1_I3(w, w, w, w); 2650 default: 2651 return C_NotImplemented; 2652 } 2653} 2654 2655static void tcg_target_init(TCGContext *s) 2656{ 2657 /* 2658 * Only probe for the platform and capabilities if we haven't already 2659 * determined maximum values at compile time. 2660 */ 2661#if !defined(use_idiv_instructions) || !defined(use_neon_instructions) 2662 { 2663 unsigned long hwcap = qemu_getauxval(AT_HWCAP); 2664#ifndef use_idiv_instructions 2665 use_idiv_instructions = (hwcap & HWCAP_ARM_IDIVA) != 0; 2666#endif 2667#ifndef use_neon_instructions 2668 use_neon_instructions = (hwcap & HWCAP_ARM_NEON) != 0; 2669#endif 2670 } 2671#endif 2672 2673 if (__ARM_ARCH < 7) { 2674 const char *pl = (const char *)qemu_getauxval(AT_PLATFORM); 2675 if (pl != NULL && pl[0] == 'v' && pl[1] >= '4' && pl[1] <= '9') { 2676 arm_arch = pl[1] - '0'; 2677 } 2678 2679 if (arm_arch < 6) { 2680 error_report("TCG: ARMv%d is unsupported; exiting", arm_arch); 2681 exit(EXIT_FAILURE); 2682 } 2683 } 2684 2685 tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS; 2686 2687 tcg_target_call_clobber_regs = 0; 2688 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0); 2689 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1); 2690 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2); 2691 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3); 2692 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R12); 2693 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14); 2694 2695 if (use_neon_instructions) { 2696 tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS; 2697 tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS; 2698 2699 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q0); 2700 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q1); 2701 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q2); 2702 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q3); 2703 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q8); 2704 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q9); 2705 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q10); 2706 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q11); 2707 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q12); 2708 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q13); 2709 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q14); 2710 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_Q15); 2711 } 2712 2713 s->reserved_regs = 0; 2714 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK); 2715 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP); 2716 tcg_regset_set_reg(s->reserved_regs, TCG_REG_PC); 2717 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP); 2718} 2719 2720static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg, 2721 TCGReg arg1, intptr_t arg2) 2722{ 2723 switch (type) { 2724 case TCG_TYPE_I32: 2725 tcg_out_ld32u(s, COND_AL, arg, arg1, arg2); 2726 return; 2727 case TCG_TYPE_V64: 2728 /* regs 1; size 8; align 8 */ 2729 tcg_out_vldst(s, INSN_VLD1 | 0x7d0, arg, arg1, arg2); 2730 return; 2731 case TCG_TYPE_V128: 2732 /* 2733 * We have only 8-byte alignment for the stack per the ABI. 2734 * Rather than dynamically re-align the stack, it's easier 2735 * to simply not request alignment beyond that. So: 2736 * regs 2; size 8; align 8 2737 */ 2738 tcg_out_vldst(s, INSN_VLD1 | 0xad0, arg, arg1, arg2); 2739 return; 2740 default: 2741 g_assert_not_reached(); 2742 } 2743} 2744 2745static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, 2746 TCGReg arg1, intptr_t arg2) 2747{ 2748 switch (type) { 2749 case TCG_TYPE_I32: 2750 tcg_out_st32(s, COND_AL, arg, arg1, arg2); 2751 return; 2752 case TCG_TYPE_V64: 2753 /* regs 1; size 8; align 8 */ 2754 tcg_out_vldst(s, INSN_VST1 | 0x7d0, arg, arg1, arg2); 2755 return; 2756 case TCG_TYPE_V128: 2757 /* See tcg_out_ld re alignment: regs 2; size 8; align 8 */ 2758 tcg_out_vldst(s, INSN_VST1 | 0xad0, arg, arg1, arg2); 2759 return; 2760 default: 2761 g_assert_not_reached(); 2762 } 2763} 2764 2765static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 2766 TCGReg base, intptr_t ofs) 2767{ 2768 return false; 2769} 2770 2771static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 2772{ 2773 if (ret == arg) { 2774 return true; 2775 } 2776 switch (type) { 2777 case TCG_TYPE_I32: 2778 if (ret < TCG_REG_Q0 && arg < TCG_REG_Q0) { 2779 tcg_out_mov_reg(s, COND_AL, ret, arg); 2780 return true; 2781 } 2782 return false; 2783 2784 case TCG_TYPE_V64: 2785 case TCG_TYPE_V128: 2786 /* "VMOV D,N" is an alias for "VORR D,N,N". */ 2787 tcg_out_vreg3(s, INSN_VORR, type - TCG_TYPE_V64, 0, ret, arg, arg); 2788 return true; 2789 2790 default: 2791 g_assert_not_reached(); 2792 } 2793} 2794 2795static void tcg_out_movi(TCGContext *s, TCGType type, 2796 TCGReg ret, tcg_target_long arg) 2797{ 2798 tcg_debug_assert(type == TCG_TYPE_I32); 2799 tcg_debug_assert(ret < TCG_REG_Q0); 2800 tcg_out_movi32(s, COND_AL, ret, arg); 2801} 2802 2803static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) 2804{ 2805 return false; 2806} 2807 2808static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, 2809 tcg_target_long imm) 2810{ 2811 int enc, opc = ARITH_ADD; 2812 2813 /* All of the easiest immediates to encode are positive. */ 2814 if (imm < 0) { 2815 imm = -imm; 2816 opc = ARITH_SUB; 2817 } 2818 enc = encode_imm(imm); 2819 if (enc >= 0) { 2820 tcg_out_dat_imm(s, COND_AL, opc, rd, rs, enc); 2821 } else { 2822 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, imm); 2823 tcg_out_dat_reg(s, COND_AL, opc, rd, rs, 2824 TCG_REG_TMP, SHIFT_IMM_LSL(0)); 2825 } 2826} 2827 2828/* Type is always V128, with I64 elements. */ 2829static void tcg_out_dup2_vec(TCGContext *s, TCGReg rd, TCGReg rl, TCGReg rh) 2830{ 2831 /* Move high element into place first. */ 2832 /* VMOV Dd+1, Ds */ 2833 tcg_out_vreg3(s, INSN_VORR | (1 << 12), 0, 0, rd, rh, rh); 2834 /* Move low element into place; tcg_out_mov will check for nop. */ 2835 tcg_out_mov(s, TCG_TYPE_V64, rd, rl); 2836} 2837 2838static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, 2839 TCGReg rd, TCGReg rs) 2840{ 2841 int q = type - TCG_TYPE_V64; 2842 2843 if (vece == MO_64) { 2844 if (type == TCG_TYPE_V128) { 2845 tcg_out_dup2_vec(s, rd, rs, rs); 2846 } else { 2847 tcg_out_mov(s, TCG_TYPE_V64, rd, rs); 2848 } 2849 } else if (rs < TCG_REG_Q0) { 2850 int b = (vece == MO_8); 2851 int e = (vece == MO_16); 2852 tcg_out32(s, INSN_VDUP_G | (b << 22) | (q << 21) | (e << 5) | 2853 encode_vn(rd) | (rs << 12)); 2854 } else { 2855 int imm4 = 1 << vece; 2856 tcg_out32(s, INSN_VDUP_S | (imm4 << 16) | (q << 6) | 2857 encode_vd(rd) | encode_vm(rs)); 2858 } 2859 return true; 2860} 2861 2862static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, 2863 TCGReg rd, TCGReg base, intptr_t offset) 2864{ 2865 if (vece == MO_64) { 2866 tcg_out_ld(s, TCG_TYPE_V64, rd, base, offset); 2867 if (type == TCG_TYPE_V128) { 2868 tcg_out_dup2_vec(s, rd, rd, rd); 2869 } 2870 } else { 2871 int q = type - TCG_TYPE_V64; 2872 tcg_out_vldst(s, INSN_VLD1R | (vece << 6) | (q << 5), 2873 rd, base, offset); 2874 } 2875 return true; 2876} 2877 2878static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece, 2879 TCGReg rd, int64_t v64) 2880{ 2881 int q = type - TCG_TYPE_V64; 2882 int cmode, imm8, i; 2883 2884 /* Test all bytes equal first. */ 2885 if (vece == MO_8) { 2886 tcg_out_vmovi(s, rd, q, 0, 0xe, v64); 2887 return; 2888 } 2889 2890 /* 2891 * Test all bytes 0x00 or 0xff second. This can match cases that 2892 * might otherwise take 2 or 3 insns for MO_16 or MO_32 below. 2893 */ 2894 for (i = imm8 = 0; i < 8; i++) { 2895 uint8_t byte = v64 >> (i * 8); 2896 if (byte == 0xff) { 2897 imm8 |= 1 << i; 2898 } else if (byte != 0) { 2899 goto fail_bytes; 2900 } 2901 } 2902 tcg_out_vmovi(s, rd, q, 1, 0xe, imm8); 2903 return; 2904 fail_bytes: 2905 2906 /* 2907 * Tests for various replications. For each element width, if we 2908 * cannot find an expansion there's no point checking a larger 2909 * width because we already know by replication it cannot match. 2910 */ 2911 if (vece == MO_16) { 2912 uint16_t v16 = v64; 2913 2914 if (is_shimm16(v16, &cmode, &imm8)) { 2915 tcg_out_vmovi(s, rd, q, 0, cmode, imm8); 2916 return; 2917 } 2918 if (is_shimm16(~v16, &cmode, &imm8)) { 2919 tcg_out_vmovi(s, rd, q, 1, cmode, imm8); 2920 return; 2921 } 2922 2923 /* 2924 * Otherwise, all remaining constants can be loaded in two insns: 2925 * rd = v16 & 0xff, rd |= v16 & 0xff00. 2926 */ 2927 tcg_out_vmovi(s, rd, q, 0, 0x8, v16 & 0xff); 2928 tcg_out_vmovi(s, rd, q, 0, 0xb, v16 >> 8); /* VORRI */ 2929 return; 2930 } 2931 2932 if (vece == MO_32) { 2933 uint32_t v32 = v64; 2934 2935 if (is_shimm32(v32, &cmode, &imm8) || 2936 is_soimm32(v32, &cmode, &imm8)) { 2937 tcg_out_vmovi(s, rd, q, 0, cmode, imm8); 2938 return; 2939 } 2940 if (is_shimm32(~v32, &cmode, &imm8) || 2941 is_soimm32(~v32, &cmode, &imm8)) { 2942 tcg_out_vmovi(s, rd, q, 1, cmode, imm8); 2943 return; 2944 } 2945 2946 /* 2947 * Restrict the set of constants to those we can load with 2948 * two instructions. Others we load from the pool. 2949 */ 2950 i = is_shimm32_pair(v32, &cmode, &imm8); 2951 if (i) { 2952 tcg_out_vmovi(s, rd, q, 0, cmode, imm8); 2953 tcg_out_vmovi(s, rd, q, 0, i | 1, extract32(v32, i * 4, 8)); 2954 return; 2955 } 2956 i = is_shimm32_pair(~v32, &cmode, &imm8); 2957 if (i) { 2958 tcg_out_vmovi(s, rd, q, 1, cmode, imm8); 2959 tcg_out_vmovi(s, rd, q, 1, i | 1, extract32(~v32, i * 4, 8)); 2960 return; 2961 } 2962 } 2963 2964 /* 2965 * As a last resort, load from the constant pool. 2966 */ 2967 if (!q || vece == MO_64) { 2968 new_pool_l2(s, R_ARM_PC11, s->code_ptr, 0, v64, v64 >> 32); 2969 /* VLDR Dd, [pc + offset] */ 2970 tcg_out32(s, INSN_VLDR_D | encode_vd(rd) | (0xf << 16)); 2971 if (q) { 2972 tcg_out_dup2_vec(s, rd, rd, rd); 2973 } 2974 } else { 2975 new_pool_label(s, (uint32_t)v64, R_ARM_PC8, s->code_ptr, 0); 2976 /* add tmp, pc, offset */ 2977 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_TMP, TCG_REG_PC, 0); 2978 tcg_out_dupm_vec(s, type, MO_32, rd, TCG_REG_TMP, 0); 2979 } 2980} 2981 2982static const ARMInsn vec_cmp_insn[16] = { 2983 [TCG_COND_EQ] = INSN_VCEQ, 2984 [TCG_COND_GT] = INSN_VCGT, 2985 [TCG_COND_GE] = INSN_VCGE, 2986 [TCG_COND_GTU] = INSN_VCGT_U, 2987 [TCG_COND_GEU] = INSN_VCGE_U, 2988}; 2989 2990static const ARMInsn vec_cmp0_insn[16] = { 2991 [TCG_COND_EQ] = INSN_VCEQ0, 2992 [TCG_COND_GT] = INSN_VCGT0, 2993 [TCG_COND_GE] = INSN_VCGE0, 2994 [TCG_COND_LT] = INSN_VCLT0, 2995 [TCG_COND_LE] = INSN_VCLE0, 2996}; 2997 2998static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, 2999 unsigned vecl, unsigned vece, 3000 const TCGArg args[TCG_MAX_OP_ARGS], 3001 const int const_args[TCG_MAX_OP_ARGS]) 3002{ 3003 TCGType type = vecl + TCG_TYPE_V64; 3004 unsigned q = vecl; 3005 TCGArg a0, a1, a2, a3; 3006 int cmode, imm8; 3007 3008 a0 = args[0]; 3009 a1 = args[1]; 3010 a2 = args[2]; 3011 3012 switch (opc) { 3013 case INDEX_op_ld_vec: 3014 tcg_out_ld(s, type, a0, a1, a2); 3015 return; 3016 case INDEX_op_st_vec: 3017 tcg_out_st(s, type, a0, a1, a2); 3018 return; 3019 case INDEX_op_dupm_vec: 3020 tcg_out_dupm_vec(s, type, vece, a0, a1, a2); 3021 return; 3022 case INDEX_op_dup2_vec: 3023 tcg_out_dup2_vec(s, a0, a1, a2); 3024 return; 3025 case INDEX_op_abs_vec: 3026 tcg_out_vreg2(s, INSN_VABS, q, vece, a0, a1); 3027 return; 3028 case INDEX_op_neg_vec: 3029 tcg_out_vreg2(s, INSN_VNEG, q, vece, a0, a1); 3030 return; 3031 case INDEX_op_not_vec: 3032 tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a1); 3033 return; 3034 case INDEX_op_add_vec: 3035 tcg_out_vreg3(s, INSN_VADD, q, vece, a0, a1, a2); 3036 return; 3037 case INDEX_op_mul_vec: 3038 tcg_out_vreg3(s, INSN_VMUL, q, vece, a0, a1, a2); 3039 return; 3040 case INDEX_op_smax_vec: 3041 tcg_out_vreg3(s, INSN_VMAX, q, vece, a0, a1, a2); 3042 return; 3043 case INDEX_op_smin_vec: 3044 tcg_out_vreg3(s, INSN_VMIN, q, vece, a0, a1, a2); 3045 return; 3046 case INDEX_op_sub_vec: 3047 tcg_out_vreg3(s, INSN_VSUB, q, vece, a0, a1, a2); 3048 return; 3049 case INDEX_op_ssadd_vec: 3050 tcg_out_vreg3(s, INSN_VQADD, q, vece, a0, a1, a2); 3051 return; 3052 case INDEX_op_sssub_vec: 3053 tcg_out_vreg3(s, INSN_VQSUB, q, vece, a0, a1, a2); 3054 return; 3055 case INDEX_op_umax_vec: 3056 tcg_out_vreg3(s, INSN_VMAX_U, q, vece, a0, a1, a2); 3057 return; 3058 case INDEX_op_umin_vec: 3059 tcg_out_vreg3(s, INSN_VMIN_U, q, vece, a0, a1, a2); 3060 return; 3061 case INDEX_op_usadd_vec: 3062 tcg_out_vreg3(s, INSN_VQADD_U, q, vece, a0, a1, a2); 3063 return; 3064 case INDEX_op_ussub_vec: 3065 tcg_out_vreg3(s, INSN_VQSUB_U, q, vece, a0, a1, a2); 3066 return; 3067 case INDEX_op_xor_vec: 3068 tcg_out_vreg3(s, INSN_VEOR, q, 0, a0, a1, a2); 3069 return; 3070 case INDEX_op_arm_sshl_vec: 3071 /* 3072 * Note that Vm is the data and Vn is the shift count, 3073 * therefore the arguments appear reversed. 3074 */ 3075 tcg_out_vreg3(s, INSN_VSHL_S, q, vece, a0, a2, a1); 3076 return; 3077 case INDEX_op_arm_ushl_vec: 3078 /* See above. */ 3079 tcg_out_vreg3(s, INSN_VSHL_U, q, vece, a0, a2, a1); 3080 return; 3081 case INDEX_op_shli_vec: 3082 tcg_out_vshifti(s, INSN_VSHLI, q, a0, a1, a2 + (8 << vece)); 3083 return; 3084 case INDEX_op_shri_vec: 3085 tcg_out_vshifti(s, INSN_VSHRI, q, a0, a1, (16 << vece) - a2); 3086 return; 3087 case INDEX_op_sari_vec: 3088 tcg_out_vshifti(s, INSN_VSARI, q, a0, a1, (16 << vece) - a2); 3089 return; 3090 case INDEX_op_arm_sli_vec: 3091 tcg_out_vshifti(s, INSN_VSLI, q, a0, a2, args[3] + (8 << vece)); 3092 return; 3093 3094 case INDEX_op_andc_vec: 3095 if (!const_args[2]) { 3096 tcg_out_vreg3(s, INSN_VBIC, q, 0, a0, a1, a2); 3097 return; 3098 } 3099 a2 = ~a2; 3100 /* fall through */ 3101 case INDEX_op_and_vec: 3102 if (const_args[2]) { 3103 is_shimm1632(~a2, &cmode, &imm8); 3104 if (a0 == a1) { 3105 tcg_out_vmovi(s, a0, q, 1, cmode | 1, imm8); /* VBICI */ 3106 return; 3107 } 3108 tcg_out_vmovi(s, a0, q, 1, cmode, imm8); /* VMVNI */ 3109 a2 = a0; 3110 } 3111 tcg_out_vreg3(s, INSN_VAND, q, 0, a0, a1, a2); 3112 return; 3113 3114 case INDEX_op_orc_vec: 3115 if (!const_args[2]) { 3116 tcg_out_vreg3(s, INSN_VORN, q, 0, a0, a1, a2); 3117 return; 3118 } 3119 a2 = ~a2; 3120 /* fall through */ 3121 case INDEX_op_or_vec: 3122 if (const_args[2]) { 3123 is_shimm1632(a2, &cmode, &imm8); 3124 if (a0 == a1) { 3125 tcg_out_vmovi(s, a0, q, 0, cmode | 1, imm8); /* VORRI */ 3126 return; 3127 } 3128 tcg_out_vmovi(s, a0, q, 0, cmode, imm8); /* VMOVI */ 3129 a2 = a0; 3130 } 3131 tcg_out_vreg3(s, INSN_VORR, q, 0, a0, a1, a2); 3132 return; 3133 3134 case INDEX_op_cmp_vec: 3135 { 3136 TCGCond cond = args[3]; 3137 ARMInsn insn; 3138 3139 switch (cond) { 3140 case TCG_COND_NE: 3141 if (const_args[2]) { 3142 tcg_out_vreg3(s, INSN_VTST, q, vece, a0, a1, a1); 3143 } else { 3144 tcg_out_vreg3(s, INSN_VCEQ, q, vece, a0, a1, a2); 3145 tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a0); 3146 } 3147 break; 3148 3149 case TCG_COND_TSTNE: 3150 case TCG_COND_TSTEQ: 3151 if (const_args[2]) { 3152 /* (x & 0) == 0 */ 3153 tcg_out_dupi_vec(s, type, MO_8, a0, 3154 -(cond == TCG_COND_TSTEQ)); 3155 break; 3156 } 3157 tcg_out_vreg3(s, INSN_VTST, q, vece, a0, a1, a2); 3158 if (cond == TCG_COND_TSTEQ) { 3159 tcg_out_vreg2(s, INSN_VMVN, q, 0, a0, a0); 3160 } 3161 break; 3162 3163 default: 3164 if (const_args[2]) { 3165 insn = vec_cmp0_insn[cond]; 3166 if (insn) { 3167 tcg_out_vreg2(s, insn, q, vece, a0, a1); 3168 return; 3169 } 3170 tcg_out_dupi_vec(s, type, MO_8, TCG_VEC_TMP, 0); 3171 a2 = TCG_VEC_TMP; 3172 } 3173 insn = vec_cmp_insn[cond]; 3174 if (insn == 0) { 3175 TCGArg t; 3176 t = a1, a1 = a2, a2 = t; 3177 cond = tcg_swap_cond(cond); 3178 insn = vec_cmp_insn[cond]; 3179 tcg_debug_assert(insn != 0); 3180 } 3181 tcg_out_vreg3(s, insn, q, vece, a0, a1, a2); 3182 break; 3183 } 3184 } 3185 return; 3186 3187 case INDEX_op_bitsel_vec: 3188 a3 = args[3]; 3189 if (a0 == a3) { 3190 tcg_out_vreg3(s, INSN_VBIT, q, 0, a0, a2, a1); 3191 } else if (a0 == a2) { 3192 tcg_out_vreg3(s, INSN_VBIF, q, 0, a0, a3, a1); 3193 } else { 3194 tcg_out_mov(s, type, a0, a1); 3195 tcg_out_vreg3(s, INSN_VBSL, q, 0, a0, a2, a3); 3196 } 3197 return; 3198 3199 case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */ 3200 case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */ 3201 default: 3202 g_assert_not_reached(); 3203 } 3204} 3205 3206int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) 3207{ 3208 switch (opc) { 3209 case INDEX_op_add_vec: 3210 case INDEX_op_sub_vec: 3211 case INDEX_op_and_vec: 3212 case INDEX_op_andc_vec: 3213 case INDEX_op_or_vec: 3214 case INDEX_op_orc_vec: 3215 case INDEX_op_xor_vec: 3216 case INDEX_op_not_vec: 3217 case INDEX_op_shli_vec: 3218 case INDEX_op_shri_vec: 3219 case INDEX_op_sari_vec: 3220 case INDEX_op_ssadd_vec: 3221 case INDEX_op_sssub_vec: 3222 case INDEX_op_usadd_vec: 3223 case INDEX_op_ussub_vec: 3224 case INDEX_op_bitsel_vec: 3225 return 1; 3226 case INDEX_op_abs_vec: 3227 case INDEX_op_cmp_vec: 3228 case INDEX_op_mul_vec: 3229 case INDEX_op_neg_vec: 3230 case INDEX_op_smax_vec: 3231 case INDEX_op_smin_vec: 3232 case INDEX_op_umax_vec: 3233 case INDEX_op_umin_vec: 3234 return vece < MO_64; 3235 case INDEX_op_shlv_vec: 3236 case INDEX_op_shrv_vec: 3237 case INDEX_op_sarv_vec: 3238 case INDEX_op_rotli_vec: 3239 case INDEX_op_rotlv_vec: 3240 case INDEX_op_rotrv_vec: 3241 return -1; 3242 default: 3243 return 0; 3244 } 3245} 3246 3247void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, 3248 TCGArg a0, ...) 3249{ 3250 va_list va; 3251 TCGv_vec v0, v1, v2, t1, t2, c1; 3252 TCGArg a2; 3253 3254 va_start(va, a0); 3255 v0 = temp_tcgv_vec(arg_temp(a0)); 3256 v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg))); 3257 a2 = va_arg(va, TCGArg); 3258 va_end(va); 3259 3260 switch (opc) { 3261 case INDEX_op_shlv_vec: 3262 /* 3263 * Merely propagate shlv_vec to arm_ushl_vec. 3264 * In this way we don't set TCG_TARGET_HAS_shv_vec 3265 * because everything is done via expansion. 3266 */ 3267 v2 = temp_tcgv_vec(arg_temp(a2)); 3268 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0), 3269 tcgv_vec_arg(v1), tcgv_vec_arg(v2)); 3270 break; 3271 3272 case INDEX_op_shrv_vec: 3273 case INDEX_op_sarv_vec: 3274 /* Right shifts are negative left shifts for NEON. */ 3275 v2 = temp_tcgv_vec(arg_temp(a2)); 3276 t1 = tcg_temp_new_vec(type); 3277 tcg_gen_neg_vec(vece, t1, v2); 3278 if (opc == INDEX_op_shrv_vec) { 3279 opc = INDEX_op_arm_ushl_vec; 3280 } else { 3281 opc = INDEX_op_arm_sshl_vec; 3282 } 3283 vec_gen_3(opc, type, vece, tcgv_vec_arg(v0), 3284 tcgv_vec_arg(v1), tcgv_vec_arg(t1)); 3285 tcg_temp_free_vec(t1); 3286 break; 3287 3288 case INDEX_op_rotli_vec: 3289 t1 = tcg_temp_new_vec(type); 3290 tcg_gen_shri_vec(vece, t1, v1, -a2 & ((8 << vece) - 1)); 3291 vec_gen_4(INDEX_op_arm_sli_vec, type, vece, 3292 tcgv_vec_arg(v0), tcgv_vec_arg(t1), tcgv_vec_arg(v1), a2); 3293 tcg_temp_free_vec(t1); 3294 break; 3295 3296 case INDEX_op_rotlv_vec: 3297 v2 = temp_tcgv_vec(arg_temp(a2)); 3298 t1 = tcg_temp_new_vec(type); 3299 c1 = tcg_constant_vec(type, vece, 8 << vece); 3300 tcg_gen_sub_vec(vece, t1, v2, c1); 3301 /* Right shifts are negative left shifts for NEON. */ 3302 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1), 3303 tcgv_vec_arg(v1), tcgv_vec_arg(t1)); 3304 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(v0), 3305 tcgv_vec_arg(v1), tcgv_vec_arg(v2)); 3306 tcg_gen_or_vec(vece, v0, v0, t1); 3307 tcg_temp_free_vec(t1); 3308 break; 3309 3310 case INDEX_op_rotrv_vec: 3311 v2 = temp_tcgv_vec(arg_temp(a2)); 3312 t1 = tcg_temp_new_vec(type); 3313 t2 = tcg_temp_new_vec(type); 3314 c1 = tcg_constant_vec(type, vece, 8 << vece); 3315 tcg_gen_neg_vec(vece, t1, v2); 3316 tcg_gen_sub_vec(vece, t2, c1, v2); 3317 /* Right shifts are negative left shifts for NEON. */ 3318 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t1), 3319 tcgv_vec_arg(v1), tcgv_vec_arg(t1)); 3320 vec_gen_3(INDEX_op_arm_ushl_vec, type, vece, tcgv_vec_arg(t2), 3321 tcgv_vec_arg(v1), tcgv_vec_arg(t2)); 3322 tcg_gen_or_vec(vece, v0, t1, t2); 3323 tcg_temp_free_vec(t1); 3324 tcg_temp_free_vec(t2); 3325 break; 3326 3327 default: 3328 g_assert_not_reached(); 3329 } 3330} 3331 3332static void tcg_out_nop_fill(tcg_insn_unit *p, int count) 3333{ 3334 int i; 3335 for (i = 0; i < count; ++i) { 3336 p[i] = INSN_NOP; 3337 } 3338} 3339 3340/* Compute frame size via macros, to share between tcg_target_qemu_prologue 3341 and tcg_register_jit. */ 3342 3343#define PUSH_SIZE ((11 - 4 + 1 + 1) * sizeof(tcg_target_long)) 3344 3345#define FRAME_SIZE \ 3346 ((PUSH_SIZE \ 3347 + TCG_STATIC_CALL_ARGS_SIZE \ 3348 + CPU_TEMP_BUF_NLONGS * sizeof(long) \ 3349 + TCG_TARGET_STACK_ALIGN - 1) \ 3350 & -TCG_TARGET_STACK_ALIGN) 3351 3352#define STACK_ADDEND (FRAME_SIZE - PUSH_SIZE) 3353 3354static void tcg_target_qemu_prologue(TCGContext *s) 3355{ 3356 /* Calling convention requires us to save r4-r11 and lr. */ 3357 /* stmdb sp!, { r4 - r11, lr } */ 3358 tcg_out_ldstm(s, COND_AL, INSN_STMDB, TCG_REG_CALL_STACK, 3359 (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) | 3360 (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) | 3361 (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_R14)); 3362 3363 /* Reserve callee argument and tcg temp space. */ 3364 tcg_out_dat_rI(s, COND_AL, ARITH_SUB, TCG_REG_CALL_STACK, 3365 TCG_REG_CALL_STACK, STACK_ADDEND, 1); 3366 tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE, 3367 CPU_TEMP_BUF_NLONGS * sizeof(long)); 3368 3369 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]); 3370 3371 if (!tcg_use_softmmu && guest_base) { 3372 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_GUEST_BASE, guest_base); 3373 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GUEST_BASE); 3374 } 3375 3376 tcg_out_b_reg(s, COND_AL, tcg_target_call_iarg_regs[1]); 3377 3378 /* 3379 * Return path for goto_ptr. Set return value to 0, a-la exit_tb, 3380 * and fall through to the rest of the epilogue. 3381 */ 3382 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr); 3383 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, 0); 3384 tcg_out_epilogue(s); 3385} 3386 3387static void tcg_out_epilogue(TCGContext *s) 3388{ 3389 /* Release local stack frame. */ 3390 tcg_out_dat_rI(s, COND_AL, ARITH_ADD, TCG_REG_CALL_STACK, 3391 TCG_REG_CALL_STACK, STACK_ADDEND, 1); 3392 3393 /* ldmia sp!, { r4 - r11, pc } */ 3394 tcg_out_ldstm(s, COND_AL, INSN_LDMIA, TCG_REG_CALL_STACK, 3395 (1 << TCG_REG_R4) | (1 << TCG_REG_R5) | (1 << TCG_REG_R6) | 3396 (1 << TCG_REG_R7) | (1 << TCG_REG_R8) | (1 << TCG_REG_R9) | 3397 (1 << TCG_REG_R10) | (1 << TCG_REG_R11) | (1 << TCG_REG_PC)); 3398} 3399 3400static void tcg_out_tb_start(TCGContext *s) 3401{ 3402 /* nothing to do */ 3403} 3404 3405typedef struct { 3406 DebugFrameHeader h; 3407 uint8_t fde_def_cfa[4]; 3408 uint8_t fde_reg_ofs[18]; 3409} DebugFrame; 3410 3411#define ELF_HOST_MACHINE EM_ARM 3412 3413/* We're expecting a 2 byte uleb128 encoded value. */ 3414QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14)); 3415 3416static const DebugFrame debug_frame = { 3417 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */ 3418 .h.cie.id = -1, 3419 .h.cie.version = 1, 3420 .h.cie.code_align = 1, 3421 .h.cie.data_align = 0x7c, /* sleb128 -4 */ 3422 .h.cie.return_column = 14, 3423 3424 /* Total FDE size does not include the "len" member. */ 3425 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 3426 3427 .fde_def_cfa = { 3428 12, 13, /* DW_CFA_def_cfa sp, ... */ 3429 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 3430 (FRAME_SIZE >> 7) 3431 }, 3432 .fde_reg_ofs = { 3433 /* The following must match the stmdb in the prologue. */ 3434 0x8e, 1, /* DW_CFA_offset, lr, -4 */ 3435 0x8b, 2, /* DW_CFA_offset, r11, -8 */ 3436 0x8a, 3, /* DW_CFA_offset, r10, -12 */ 3437 0x89, 4, /* DW_CFA_offset, r9, -16 */ 3438 0x88, 5, /* DW_CFA_offset, r8, -20 */ 3439 0x87, 6, /* DW_CFA_offset, r7, -24 */ 3440 0x86, 7, /* DW_CFA_offset, r6, -28 */ 3441 0x85, 8, /* DW_CFA_offset, r5, -32 */ 3442 0x84, 9, /* DW_CFA_offset, r4, -36 */ 3443 } 3444}; 3445 3446void tcg_register_jit(const void *buf, size_t buf_size) 3447{ 3448 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame)); 3449} 3450