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 1087static void tcg_out_ld16u(TCGContext *s, ARMCond cond, 1088 TCGReg rd, TCGReg rn, int32_t offset) 1089{ 1090 if (offset > 0xff || offset < -0xff) { 1091 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1092 tcg_out_ld16u_r(s, cond, rd, rn, TCG_REG_TMP); 1093 } else 1094 tcg_out_ld16u_8(s, cond, rd, rn, offset); 1095} 1096 1097static void tcg_out_ld16s(TCGContext *s, ARMCond cond, 1098 TCGReg rd, TCGReg rn, int32_t offset) 1099{ 1100 if (offset > 0xff || offset < -0xff) { 1101 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1102 tcg_out_ld16s_r(s, cond, rd, rn, TCG_REG_TMP); 1103 } else 1104 tcg_out_ld16s_8(s, cond, rd, rn, offset); 1105} 1106 1107static void tcg_out_st16(TCGContext *s, ARMCond cond, 1108 TCGReg rd, TCGReg rn, int32_t offset) 1109{ 1110 if (offset > 0xff || offset < -0xff) { 1111 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1112 tcg_out_st16_r(s, cond, rd, rn, TCG_REG_TMP); 1113 } else 1114 tcg_out_st16_8(s, cond, rd, rn, offset); 1115} 1116 1117static void tcg_out_ld8u(TCGContext *s, ARMCond cond, 1118 TCGReg rd, TCGReg rn, int32_t offset) 1119{ 1120 if (offset > 0xfff || offset < -0xfff) { 1121 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1122 tcg_out_ld8_r(s, cond, rd, rn, TCG_REG_TMP); 1123 } else 1124 tcg_out_ld8_12(s, cond, rd, rn, offset); 1125} 1126 1127static void tcg_out_ld8s(TCGContext *s, ARMCond cond, 1128 TCGReg rd, TCGReg rn, int32_t offset) 1129{ 1130 if (offset > 0xff || offset < -0xff) { 1131 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1132 tcg_out_ld8s_r(s, cond, rd, rn, TCG_REG_TMP); 1133 } else 1134 tcg_out_ld8s_8(s, cond, rd, rn, offset); 1135} 1136 1137static void tcg_out_st8(TCGContext *s, ARMCond cond, 1138 TCGReg rd, TCGReg rn, int32_t offset) 1139{ 1140 if (offset > 0xfff || offset < -0xfff) { 1141 tcg_out_movi32(s, cond, TCG_REG_TMP, offset); 1142 tcg_out_st8_r(s, cond, rd, rn, TCG_REG_TMP); 1143 } else 1144 tcg_out_st8_12(s, cond, rd, rn, offset); 1145} 1146 1147/* 1148 * The _goto case is normally between TBs within the same code buffer, and 1149 * with the code buffer limited to 16MB we wouldn't need the long case. 1150 * But we also use it for the tail-call to the qemu_ld/st helpers, which does. 1151 */ 1152static void tcg_out_goto(TCGContext *s, ARMCond cond, const tcg_insn_unit *addr) 1153{ 1154 intptr_t addri = (intptr_t)addr; 1155 ptrdiff_t disp = tcg_pcrel_diff(s, addr); 1156 bool arm_mode = !(addri & 1); 1157 1158 if (arm_mode && disp - 8 < 0x01fffffd && disp - 8 > -0x01fffffd) { 1159 tcg_out_b_imm(s, cond, disp); 1160 return; 1161 } 1162 1163 /* LDR is interworking from v5t. */ 1164 tcg_out_movi_pool(s, cond, TCG_REG_PC, addri); 1165} 1166 1167/* 1168 * The call case is mostly used for helpers - so it's not unreasonable 1169 * for them to be beyond branch range. 1170 */ 1171static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *addr) 1172{ 1173 intptr_t addri = (intptr_t)addr; 1174 ptrdiff_t disp = tcg_pcrel_diff(s, addr); 1175 bool arm_mode = !(addri & 1); 1176 1177 if (disp - 8 < 0x02000000 && disp - 8 >= -0x02000000) { 1178 if (arm_mode) { 1179 tcg_out_bl_imm(s, COND_AL, disp); 1180 } else { 1181 tcg_out_blx_imm(s, disp); 1182 } 1183 return; 1184 } 1185 1186 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, addri); 1187 tcg_out_blx_reg(s, COND_AL, TCG_REG_TMP); 1188} 1189 1190static void tcg_out_call(TCGContext *s, const tcg_insn_unit *addr, 1191 const TCGHelperInfo *info) 1192{ 1193 tcg_out_call_int(s, addr); 1194} 1195 1196static void tcg_out_goto_label(TCGContext *s, ARMCond cond, TCGLabel *l) 1197{ 1198 if (l->has_value) { 1199 tcg_out_goto(s, cond, l->u.value_ptr); 1200 } else { 1201 tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, l, 0); 1202 tcg_out_b_imm(s, cond, 0); 1203 } 1204} 1205 1206static void tcg_out_mb(TCGContext *s, TCGArg a0) 1207{ 1208 if (use_armv7_instructions) { 1209 tcg_out32(s, INSN_DMB_ISH); 1210 } else { 1211 tcg_out32(s, INSN_DMB_MCR); 1212 } 1213} 1214 1215static TCGCond tgen_cmp(TCGContext *s, TCGCond cond, TCGReg a, TCGReg b) 1216{ 1217 if (is_tst_cond(cond)) { 1218 tcg_out_dat_reg(s, COND_AL, ARITH_TST, 0, a, b, SHIFT_IMM_LSL(0)); 1219 return tcg_tst_eqne_cond(cond); 1220 } 1221 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, a, b, SHIFT_IMM_LSL(0)); 1222 return cond; 1223} 1224 1225static TCGCond tgen_cmpi(TCGContext *s, TCGCond cond, TCGReg a, TCGArg b) 1226{ 1227 int imm12; 1228 1229 if (!is_tst_cond(cond)) { 1230 tcg_out_dat_IN(s, COND_AL, ARITH_CMP, ARITH_CMN, 0, a, b); 1231 return cond; 1232 } 1233 1234 /* 1235 * The compare constraints allow rIN, but TST does not support N. 1236 * Be prepared to load the constant into a scratch register. 1237 */ 1238 imm12 = encode_imm(b); 1239 if (imm12 >= 0) { 1240 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, a, imm12); 1241 } else { 1242 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, b); 1243 tcg_out_dat_reg(s, COND_AL, ARITH_TST, 0, 1244 a, TCG_REG_TMP, SHIFT_IMM_LSL(0)); 1245 } 1246 return tcg_tst_eqne_cond(cond); 1247} 1248 1249static TCGCond tcg_out_cmp(TCGContext *s, TCGCond cond, TCGReg a, 1250 TCGArg b, int b_const) 1251{ 1252 if (b_const) { 1253 return tgen_cmpi(s, cond, a, b); 1254 } else { 1255 return tgen_cmp(s, cond, a, b); 1256 } 1257} 1258 1259static TCGCond tcg_out_cmp2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, 1260 TCGArg bl, bool const_bl, TCGArg bh, bool const_bh) 1261{ 1262 switch (cond) { 1263 case TCG_COND_EQ: 1264 case TCG_COND_NE: 1265 case TCG_COND_LTU: 1266 case TCG_COND_LEU: 1267 case TCG_COND_GTU: 1268 case TCG_COND_GEU: 1269 /* 1270 * We perform a conditional comparison. If the high half is 1271 * equal, then overwrite the flags with the comparison of the 1272 * low half. The resulting flags cover the whole. 1273 */ 1274 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, ah, bh, const_bh); 1275 tcg_out_dat_rI(s, COND_EQ, ARITH_CMP, 0, al, bl, const_bl); 1276 return cond; 1277 1278 case TCG_COND_TSTEQ: 1279 case TCG_COND_TSTNE: 1280 /* Similar, but with TST instead of CMP. */ 1281 tcg_out_dat_rI(s, COND_AL, ARITH_TST, 0, ah, bh, const_bh); 1282 tcg_out_dat_rI(s, COND_EQ, ARITH_TST, 0, al, bl, const_bl); 1283 return tcg_tst_eqne_cond(cond); 1284 1285 case TCG_COND_LT: 1286 case TCG_COND_GE: 1287 /* We perform a double-word subtraction and examine the result. 1288 We do not actually need the result of the subtract, so the 1289 low part "subtract" is a compare. For the high half we have 1290 no choice but to compute into a temporary. */ 1291 tcg_out_dat_rI(s, COND_AL, ARITH_CMP, 0, al, bl, const_bl); 1292 tcg_out_dat_rI(s, COND_AL, ARITH_SBC | TO_CPSR, 1293 TCG_REG_TMP, ah, bh, const_bh); 1294 return cond; 1295 1296 case TCG_COND_LE: 1297 case TCG_COND_GT: 1298 /* Similar, but with swapped arguments, via reversed subtract. */ 1299 tcg_out_dat_rI(s, COND_AL, ARITH_RSB | TO_CPSR, 1300 TCG_REG_TMP, al, bl, const_bl); 1301 tcg_out_dat_rI(s, COND_AL, ARITH_RSC | TO_CPSR, 1302 TCG_REG_TMP, ah, bh, const_bh); 1303 return tcg_swap_cond(cond); 1304 1305 default: 1306 g_assert_not_reached(); 1307 } 1308} 1309 1310/* 1311 * Note that TCGReg references Q-registers. 1312 * Q-regno = 2 * D-regno, so shift left by 1 while inserting. 1313 */ 1314static uint32_t encode_vd(TCGReg rd) 1315{ 1316 tcg_debug_assert(rd >= TCG_REG_Q0); 1317 return (extract32(rd, 3, 1) << 22) | (extract32(rd, 0, 3) << 13); 1318} 1319 1320static uint32_t encode_vn(TCGReg rn) 1321{ 1322 tcg_debug_assert(rn >= TCG_REG_Q0); 1323 return (extract32(rn, 3, 1) << 7) | (extract32(rn, 0, 3) << 17); 1324} 1325 1326static uint32_t encode_vm(TCGReg rm) 1327{ 1328 tcg_debug_assert(rm >= TCG_REG_Q0); 1329 return (extract32(rm, 3, 1) << 5) | (extract32(rm, 0, 3) << 1); 1330} 1331 1332static void tcg_out_vreg2(TCGContext *s, ARMInsn insn, int q, int vece, 1333 TCGReg d, TCGReg m) 1334{ 1335 tcg_out32(s, insn | (vece << 18) | (q << 6) | 1336 encode_vd(d) | encode_vm(m)); 1337} 1338 1339static void tcg_out_vreg3(TCGContext *s, ARMInsn insn, int q, int vece, 1340 TCGReg d, TCGReg n, TCGReg m) 1341{ 1342 tcg_out32(s, insn | (vece << 20) | (q << 6) | 1343 encode_vd(d) | encode_vn(n) | encode_vm(m)); 1344} 1345 1346static void tcg_out_vmovi(TCGContext *s, TCGReg rd, 1347 int q, int op, int cmode, uint8_t imm8) 1348{ 1349 tcg_out32(s, INSN_VMOVI | encode_vd(rd) | (q << 6) | (op << 5) 1350 | (cmode << 8) | extract32(imm8, 0, 4) 1351 | (extract32(imm8, 4, 3) << 16) 1352 | (extract32(imm8, 7, 1) << 24)); 1353} 1354 1355static void tcg_out_vshifti(TCGContext *s, ARMInsn insn, int q, 1356 TCGReg rd, TCGReg rm, int l_imm6) 1357{ 1358 tcg_out32(s, insn | (q << 6) | encode_vd(rd) | encode_vm(rm) | 1359 (extract32(l_imm6, 6, 1) << 7) | 1360 (extract32(l_imm6, 0, 6) << 16)); 1361} 1362 1363static void tcg_out_vldst(TCGContext *s, ARMInsn insn, 1364 TCGReg rd, TCGReg rn, int offset) 1365{ 1366 if (offset != 0) { 1367 if (check_fit_imm(offset) || check_fit_imm(-offset)) { 1368 tcg_out_dat_rIN(s, COND_AL, ARITH_ADD, ARITH_SUB, 1369 TCG_REG_TMP, rn, offset, true); 1370 } else { 1371 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP, offset); 1372 tcg_out_dat_reg(s, COND_AL, ARITH_ADD, 1373 TCG_REG_TMP, TCG_REG_TMP, rn, 0); 1374 } 1375 rn = TCG_REG_TMP; 1376 } 1377 tcg_out32(s, insn | (rn << 16) | encode_vd(rd) | 0xf); 1378} 1379 1380typedef struct { 1381 ARMCond cond; 1382 TCGReg base; 1383 int index; 1384 bool index_scratch; 1385 TCGAtomAlign aa; 1386} HostAddress; 1387 1388bool tcg_target_has_memory_bswap(MemOp memop) 1389{ 1390 return false; 1391} 1392 1393static TCGReg ldst_ra_gen(TCGContext *s, const TCGLabelQemuLdst *l, int arg) 1394{ 1395 /* We arrive at the slow path via "BLNE", so R14 contains l->raddr. */ 1396 return TCG_REG_R14; 1397} 1398 1399static const TCGLdstHelperParam ldst_helper_param = { 1400 .ra_gen = ldst_ra_gen, 1401 .ntmp = 1, 1402 .tmp = { TCG_REG_TMP }, 1403}; 1404 1405static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) 1406{ 1407 MemOp opc = get_memop(lb->oi); 1408 1409 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 1410 return false; 1411 } 1412 1413 tcg_out_ld_helper_args(s, lb, &ldst_helper_param); 1414 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE]); 1415 tcg_out_ld_helper_ret(s, lb, false, &ldst_helper_param); 1416 1417 tcg_out_goto(s, COND_AL, lb->raddr); 1418 return true; 1419} 1420 1421static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) 1422{ 1423 MemOp opc = get_memop(lb->oi); 1424 1425 if (!reloc_pc24(lb->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 1426 return false; 1427 } 1428 1429 tcg_out_st_helper_args(s, lb, &ldst_helper_param); 1430 1431 /* Tail-call to the helper, which will return to the fast path. */ 1432 tcg_out_goto(s, COND_AL, qemu_st_helpers[opc & MO_SIZE]); 1433 return true; 1434} 1435 1436/* We expect to use an 9-bit sign-magnitude negative offset from ENV. */ 1437#define MIN_TLB_MASK_TABLE_OFS -256 1438 1439static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h, 1440 TCGReg addr, MemOpIdx oi, bool is_ld) 1441{ 1442 TCGLabelQemuLdst *ldst = NULL; 1443 MemOp opc = get_memop(oi); 1444 unsigned a_mask; 1445 1446 if (tcg_use_softmmu) { 1447 *h = (HostAddress){ 1448 .cond = COND_AL, 1449 .base = addr, 1450 .index = TCG_REG_R1, 1451 .index_scratch = true, 1452 }; 1453 } else { 1454 *h = (HostAddress){ 1455 .cond = COND_AL, 1456 .base = addr, 1457 .index = guest_base ? TCG_REG_GUEST_BASE : -1, 1458 .index_scratch = false, 1459 }; 1460 } 1461 1462 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false); 1463 a_mask = (1 << h->aa.align) - 1; 1464 1465 if (tcg_use_softmmu) { 1466 int mem_index = get_mmuidx(oi); 1467 int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read) 1468 : offsetof(CPUTLBEntry, addr_write); 1469 int fast_off = tlb_mask_table_ofs(s, mem_index); 1470 unsigned s_mask = (1 << (opc & MO_SIZE)) - 1; 1471 TCGReg t_addr; 1472 1473 ldst = new_ldst_label(s); 1474 ldst->is_ld = is_ld; 1475 ldst->oi = oi; 1476 ldst->addr_reg = addr; 1477 1478 /* Load cpu->neg.tlb.f[mmu_idx].{mask,table} into {r0,r1}. */ 1479 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, mask) != 0); 1480 QEMU_BUILD_BUG_ON(offsetof(CPUTLBDescFast, table) != 4); 1481 tcg_out_ldrd_8(s, COND_AL, TCG_REG_R0, TCG_AREG0, fast_off); 1482 1483 /* Extract the tlb index from the address into R0. */ 1484 tcg_out_dat_reg(s, COND_AL, ARITH_AND, TCG_REG_R0, TCG_REG_R0, addr, 1485 SHIFT_IMM_LSR(s->page_bits - CPU_TLB_ENTRY_BITS)); 1486 1487 /* 1488 * Add the tlb_table pointer, creating the CPUTLBEntry address in R1. 1489 * Load the tlb comparator into R2 and the fast path addend into R1. 1490 */ 1491 if (cmp_off == 0) { 1492 tcg_out_ld32_rwb(s, COND_AL, TCG_REG_R2, TCG_REG_R1, TCG_REG_R0); 1493 } else { 1494 tcg_out_dat_reg(s, COND_AL, ARITH_ADD, 1495 TCG_REG_R1, TCG_REG_R1, TCG_REG_R0, 0); 1496 tcg_out_ld32_12(s, COND_AL, TCG_REG_R2, TCG_REG_R1, cmp_off); 1497 } 1498 1499 /* Load the tlb addend. */ 1500 tcg_out_ld32_12(s, COND_AL, TCG_REG_R1, TCG_REG_R1, 1501 offsetof(CPUTLBEntry, addend)); 1502 1503 /* 1504 * Check alignment, check comparators. 1505 * Do this in 2-4 insns. Use MOVW for v7, if possible, 1506 * to reduce the number of sequential conditional instructions. 1507 * Almost all guests have at least 4k pages, which means that we need 1508 * to clear at least 9 bits even for an 8-byte memory, which means it 1509 * isn't worth checking for an immediate operand for BIC. 1510 * 1511 * For unaligned accesses, test the page of the last unit of alignment. 1512 * This leaves the least significant alignment bits unchanged, and of 1513 * course must be zero. 1514 */ 1515 t_addr = addr; 1516 if (a_mask < s_mask) { 1517 t_addr = TCG_REG_R0; 1518 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, t_addr, 1519 addr, s_mask - a_mask); 1520 } 1521 if (use_armv7_instructions && s->page_bits <= 16) { 1522 tcg_out_movi32(s, COND_AL, TCG_REG_TMP, ~(s->page_mask | a_mask)); 1523 tcg_out_dat_reg(s, COND_AL, ARITH_BIC, TCG_REG_TMP, 1524 t_addr, TCG_REG_TMP, 0); 1525 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, 1526 TCG_REG_R2, TCG_REG_TMP, 0); 1527 } else { 1528 if (a_mask) { 1529 tcg_debug_assert(a_mask <= 0xff); 1530 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addr, a_mask); 1531 } 1532 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, TCG_REG_TMP, 0, t_addr, 1533 SHIFT_IMM_LSR(s->page_bits)); 1534 tcg_out_dat_reg(s, (a_mask ? COND_EQ : COND_AL), ARITH_CMP, 1535 0, TCG_REG_R2, TCG_REG_TMP, 1536 SHIFT_IMM_LSL(s->page_bits)); 1537 } 1538 } else if (a_mask) { 1539 ldst = new_ldst_label(s); 1540 ldst->is_ld = is_ld; 1541 ldst->oi = oi; 1542 ldst->addr_reg = addr; 1543 1544 /* We are expecting alignment to max out at 7 */ 1545 tcg_debug_assert(a_mask <= 0xff); 1546 /* tst addr, #mask */ 1547 tcg_out_dat_imm(s, COND_AL, ARITH_TST, 0, addr, a_mask); 1548 } 1549 1550 return ldst; 1551} 1552 1553static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg datalo, 1554 TCGReg datahi, HostAddress h) 1555{ 1556 TCGReg base; 1557 1558 /* Byte swapping is left to middle-end expansion. */ 1559 tcg_debug_assert((opc & MO_BSWAP) == 0); 1560 1561 switch (opc & MO_SSIZE) { 1562 case MO_UB: 1563 if (h.index < 0) { 1564 tcg_out_ld8_12(s, h.cond, datalo, h.base, 0); 1565 } else { 1566 tcg_out_ld8_r(s, h.cond, datalo, h.base, h.index); 1567 } 1568 break; 1569 case MO_SB: 1570 if (h.index < 0) { 1571 tcg_out_ld8s_8(s, h.cond, datalo, h.base, 0); 1572 } else { 1573 tcg_out_ld8s_r(s, h.cond, datalo, h.base, h.index); 1574 } 1575 break; 1576 case MO_UW: 1577 if (h.index < 0) { 1578 tcg_out_ld16u_8(s, h.cond, datalo, h.base, 0); 1579 } else { 1580 tcg_out_ld16u_r(s, h.cond, datalo, h.base, h.index); 1581 } 1582 break; 1583 case MO_SW: 1584 if (h.index < 0) { 1585 tcg_out_ld16s_8(s, h.cond, datalo, h.base, 0); 1586 } else { 1587 tcg_out_ld16s_r(s, h.cond, datalo, h.base, h.index); 1588 } 1589 break; 1590 case MO_UL: 1591 if (h.index < 0) { 1592 tcg_out_ld32_12(s, h.cond, datalo, h.base, 0); 1593 } else { 1594 tcg_out_ld32_r(s, h.cond, datalo, h.base, h.index); 1595 } 1596 break; 1597 case MO_UQ: 1598 /* We used pair allocation for datalo, so already should be aligned. */ 1599 tcg_debug_assert((datalo & 1) == 0); 1600 tcg_debug_assert(datahi == datalo + 1); 1601 /* LDRD requires alignment; double-check that. */ 1602 if (memop_alignment_bits(opc) >= MO_64) { 1603 if (h.index < 0) { 1604 tcg_out_ldrd_8(s, h.cond, datalo, h.base, 0); 1605 break; 1606 } 1607 /* 1608 * Rm (the second address op) must not overlap Rt or Rt + 1. 1609 * Since datalo is aligned, we can simplify the test via alignment. 1610 * Flip the two address arguments if that works. 1611 */ 1612 if ((h.index & ~1) != datalo) { 1613 tcg_out_ldrd_r(s, h.cond, datalo, h.base, h.index); 1614 break; 1615 } 1616 if ((h.base & ~1) != datalo) { 1617 tcg_out_ldrd_r(s, h.cond, datalo, h.index, h.base); 1618 break; 1619 } 1620 } 1621 if (h.index < 0) { 1622 base = h.base; 1623 if (datalo == h.base) { 1624 tcg_out_mov_reg(s, h.cond, TCG_REG_TMP, base); 1625 base = TCG_REG_TMP; 1626 } 1627 } else if (h.index_scratch) { 1628 tcg_out_ld32_rwb(s, h.cond, datalo, h.index, h.base); 1629 tcg_out_ld32_12(s, h.cond, datahi, h.index, 4); 1630 break; 1631 } else { 1632 tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP, 1633 h.base, h.index, SHIFT_IMM_LSL(0)); 1634 base = TCG_REG_TMP; 1635 } 1636 tcg_out_ld32_12(s, h.cond, datalo, base, 0); 1637 tcg_out_ld32_12(s, h.cond, datahi, base, 4); 1638 break; 1639 default: 1640 g_assert_not_reached(); 1641 } 1642} 1643 1644static void tcg_out_qemu_ld(TCGContext *s, TCGReg datalo, TCGReg datahi, 1645 TCGReg addr, MemOpIdx oi, TCGType data_type) 1646{ 1647 MemOp opc = get_memop(oi); 1648 TCGLabelQemuLdst *ldst; 1649 HostAddress h; 1650 1651 ldst = prepare_host_addr(s, &h, addr, oi, true); 1652 if (ldst) { 1653 ldst->type = data_type; 1654 ldst->datalo_reg = datalo; 1655 ldst->datahi_reg = datahi; 1656 1657 /* 1658 * This a conditional BL only to load a pointer within this 1659 * opcode into LR for the slow path. We will not be using 1660 * the value for a tail call. 1661 */ 1662 ldst->label_ptr[0] = s->code_ptr; 1663 tcg_out_bl_imm(s, COND_NE, 0); 1664 1665 tcg_out_qemu_ld_direct(s, opc, datalo, datahi, h); 1666 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1667 } else { 1668 tcg_out_qemu_ld_direct(s, opc, datalo, datahi, h); 1669 } 1670} 1671 1672static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg datalo, 1673 TCGReg datahi, HostAddress h) 1674{ 1675 /* Byte swapping is left to middle-end expansion. */ 1676 tcg_debug_assert((opc & MO_BSWAP) == 0); 1677 1678 switch (opc & MO_SIZE) { 1679 case MO_8: 1680 if (h.index < 0) { 1681 tcg_out_st8_12(s, h.cond, datalo, h.base, 0); 1682 } else { 1683 tcg_out_st8_r(s, h.cond, datalo, h.base, h.index); 1684 } 1685 break; 1686 case MO_16: 1687 if (h.index < 0) { 1688 tcg_out_st16_8(s, h.cond, datalo, h.base, 0); 1689 } else { 1690 tcg_out_st16_r(s, h.cond, datalo, h.base, h.index); 1691 } 1692 break; 1693 case MO_32: 1694 if (h.index < 0) { 1695 tcg_out_st32_12(s, h.cond, datalo, h.base, 0); 1696 } else { 1697 tcg_out_st32_r(s, h.cond, datalo, h.base, h.index); 1698 } 1699 break; 1700 case MO_64: 1701 /* We used pair allocation for datalo, so already should be aligned. */ 1702 tcg_debug_assert((datalo & 1) == 0); 1703 tcg_debug_assert(datahi == datalo + 1); 1704 /* STRD requires alignment; double-check that. */ 1705 if (memop_alignment_bits(opc) >= MO_64) { 1706 if (h.index < 0) { 1707 tcg_out_strd_8(s, h.cond, datalo, h.base, 0); 1708 } else { 1709 tcg_out_strd_r(s, h.cond, datalo, h.base, h.index); 1710 } 1711 } else if (h.index < 0) { 1712 tcg_out_st32_12(s, h.cond, datalo, h.base, 0); 1713 tcg_out_st32_12(s, h.cond, datahi, h.base, 4); 1714 } else if (h.index_scratch) { 1715 tcg_out_st32_rwb(s, h.cond, datalo, h.index, h.base); 1716 tcg_out_st32_12(s, h.cond, datahi, h.index, 4); 1717 } else { 1718 tcg_out_dat_reg(s, h.cond, ARITH_ADD, TCG_REG_TMP, 1719 h.base, h.index, SHIFT_IMM_LSL(0)); 1720 tcg_out_st32_12(s, h.cond, datalo, TCG_REG_TMP, 0); 1721 tcg_out_st32_12(s, h.cond, datahi, TCG_REG_TMP, 4); 1722 } 1723 break; 1724 default: 1725 g_assert_not_reached(); 1726 } 1727} 1728 1729static void tcg_out_qemu_st(TCGContext *s, TCGReg datalo, TCGReg datahi, 1730 TCGReg addr, MemOpIdx oi, TCGType data_type) 1731{ 1732 MemOp opc = get_memop(oi); 1733 TCGLabelQemuLdst *ldst; 1734 HostAddress h; 1735 1736 ldst = prepare_host_addr(s, &h, addr, oi, false); 1737 if (ldst) { 1738 ldst->type = data_type; 1739 ldst->datalo_reg = datalo; 1740 ldst->datahi_reg = datahi; 1741 1742 h.cond = COND_EQ; 1743 tcg_out_qemu_st_direct(s, opc, datalo, datahi, h); 1744 1745 /* The conditional call is last, as we're going to return here. */ 1746 ldst->label_ptr[0] = s->code_ptr; 1747 tcg_out_bl_imm(s, COND_NE, 0); 1748 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1749 } else { 1750 tcg_out_qemu_st_direct(s, opc, datalo, datahi, h); 1751 } 1752} 1753 1754static void tcg_out_epilogue(TCGContext *s); 1755 1756static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) 1757{ 1758 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R0, arg); 1759 tcg_out_epilogue(s); 1760} 1761 1762static void tcg_out_goto_tb(TCGContext *s, int which) 1763{ 1764 uintptr_t i_addr; 1765 intptr_t i_disp; 1766 1767 /* Direct branch will be patched by tb_target_set_jmp_target. */ 1768 set_jmp_insn_offset(s, which); 1769 tcg_out32(s, INSN_NOP); 1770 1771 /* When branch is out of range, fall through to indirect. */ 1772 i_addr = get_jmp_target_addr(s, which); 1773 i_disp = tcg_pcrel_diff(s, (void *)i_addr) - 8; 1774 tcg_debug_assert(i_disp < 0); 1775 if (i_disp >= -0xfff) { 1776 tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, i_disp); 1777 } else { 1778 /* 1779 * The TB is close, but outside the 12 bits addressable by 1780 * the load. We can extend this to 20 bits with a sub of a 1781 * shifted immediate from pc. 1782 */ 1783 int h = -i_disp; 1784 int l = -(h & 0xfff); 1785 1786 h = encode_imm_nofail(h + l); 1787 tcg_out_dat_imm(s, COND_AL, ARITH_SUB, TCG_REG_R0, TCG_REG_PC, h); 1788 tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_R0, l); 1789 } 1790 set_jmp_reset_offset(s, which); 1791} 1792 1793void tb_target_set_jmp_target(const TranslationBlock *tb, int n, 1794 uintptr_t jmp_rx, uintptr_t jmp_rw) 1795{ 1796 uintptr_t addr = tb->jmp_target_addr[n]; 1797 ptrdiff_t offset = addr - (jmp_rx + 8); 1798 tcg_insn_unit insn; 1799 1800 /* Either directly branch, or fall through to indirect branch. */ 1801 if (offset == sextract64(offset, 0, 26)) { 1802 /* B <addr> */ 1803 insn = deposit32((COND_AL << 28) | INSN_B, 0, 24, offset >> 2); 1804 } else { 1805 insn = INSN_NOP; 1806 } 1807 1808 qatomic_set((uint32_t *)jmp_rw, insn); 1809 flush_idcache_range(jmp_rx, jmp_rw, 4); 1810} 1811 1812 1813static void tgen_add(TCGContext *s, TCGType type, 1814 TCGReg a0, TCGReg a1, TCGReg a2) 1815{ 1816 tcg_out_dat_reg(s, COND_AL, ARITH_ADD, a0, a1, a2, SHIFT_IMM_LSL(0)); 1817} 1818 1819static void tgen_addi(TCGContext *s, TCGType type, 1820 TCGReg a0, TCGReg a1, tcg_target_long a2) 1821{ 1822 tcg_out_dat_IN(s, COND_AL, ARITH_ADD, ARITH_SUB, a0, a1, a2); 1823} 1824 1825static const TCGOutOpBinary outop_add = { 1826 .base.static_constraint = C_O1_I2(r, r, rIN), 1827 .out_rrr = tgen_add, 1828 .out_rri = tgen_addi, 1829}; 1830 1831static void tgen_addco(TCGContext *s, TCGType type, 1832 TCGReg a0, TCGReg a1, TCGReg a2) 1833{ 1834 tcg_out_dat_reg(s, COND_AL, ARITH_ADD | TO_CPSR, 1835 a0, a1, a2, SHIFT_IMM_LSL(0)); 1836} 1837 1838static void tgen_addco_imm(TCGContext *s, TCGType type, 1839 TCGReg a0, TCGReg a1, tcg_target_long a2) 1840{ 1841 tcg_out_dat_IN(s, COND_AL, ARITH_ADD | TO_CPSR, ARITH_SUB | TO_CPSR, 1842 a0, a1, a2); 1843} 1844 1845static const TCGOutOpBinary outop_addco = { 1846 .base.static_constraint = C_O1_I2(r, r, rIN), 1847 .out_rrr = tgen_addco, 1848 .out_rri = tgen_addco_imm, 1849}; 1850 1851static void tgen_addci(TCGContext *s, TCGType type, 1852 TCGReg a0, TCGReg a1, TCGReg a2) 1853{ 1854 tcg_out_dat_reg(s, COND_AL, ARITH_ADC, a0, a1, a2, SHIFT_IMM_LSL(0)); 1855} 1856 1857static void tgen_addci_imm(TCGContext *s, TCGType type, 1858 TCGReg a0, TCGReg a1, tcg_target_long a2) 1859{ 1860 tcg_out_dat_IK(s, COND_AL, ARITH_ADC, ARITH_SBC, a0, a1, a2); 1861} 1862 1863static const TCGOutOpAddSubCarry outop_addci = { 1864 .base.static_constraint = C_O1_I2(r, r, rIK), 1865 .out_rrr = tgen_addci, 1866 .out_rri = tgen_addci_imm, 1867}; 1868 1869static void tgen_addcio(TCGContext *s, TCGType type, 1870 TCGReg a0, TCGReg a1, TCGReg a2) 1871{ 1872 tcg_out_dat_reg(s, COND_AL, ARITH_ADC | TO_CPSR, 1873 a0, a1, a2, SHIFT_IMM_LSL(0)); 1874} 1875 1876static void tgen_addcio_imm(TCGContext *s, TCGType type, 1877 TCGReg a0, TCGReg a1, tcg_target_long a2) 1878{ 1879 tcg_out_dat_IK(s, COND_AL, ARITH_ADC | TO_CPSR, ARITH_SBC | TO_CPSR, 1880 a0, a1, a2); 1881} 1882 1883static const TCGOutOpBinary outop_addcio = { 1884 .base.static_constraint = C_O1_I2(r, r, rIK), 1885 .out_rrr = tgen_addcio, 1886 .out_rri = tgen_addcio_imm, 1887}; 1888 1889/* Set C to @c; NZVQ all set to 0. */ 1890static void tcg_out_movi_apsr_c(TCGContext *s, bool c) 1891{ 1892 int imm12 = encode_imm_nofail(c << 29); 1893 tcg_out32(s, (COND_AL << 28) | INSN_MSRI_CPSR | 0x80000 | imm12); 1894} 1895 1896static void tcg_out_set_carry(TCGContext *s) 1897{ 1898 tcg_out_movi_apsr_c(s, 1); 1899} 1900 1901static void tgen_and(TCGContext *s, TCGType type, 1902 TCGReg a0, TCGReg a1, TCGReg a2) 1903{ 1904 tcg_out_dat_reg(s, COND_AL, ARITH_AND, a0, a1, a2, SHIFT_IMM_LSL(0)); 1905} 1906 1907static void tgen_andi(TCGContext *s, TCGType type, 1908 TCGReg a0, TCGReg a1, tcg_target_long a2) 1909{ 1910 tcg_out_dat_IK(s, COND_AL, ARITH_AND, ARITH_BIC, a0, a1, a2); 1911} 1912 1913static const TCGOutOpBinary outop_and = { 1914 .base.static_constraint = C_O1_I2(r, r, rIK), 1915 .out_rrr = tgen_and, 1916 .out_rri = tgen_andi, 1917}; 1918 1919static void tgen_andc(TCGContext *s, TCGType type, 1920 TCGReg a0, TCGReg a1, TCGReg a2) 1921{ 1922 tcg_out_dat_reg(s, COND_AL, ARITH_BIC, a0, a1, a2, SHIFT_IMM_LSL(0)); 1923} 1924 1925static const TCGOutOpBinary outop_andc = { 1926 .base.static_constraint = C_O1_I2(r, r, r), 1927 .out_rrr = tgen_andc, 1928}; 1929 1930static void tgen_clz(TCGContext *s, TCGType type, 1931 TCGReg a0, TCGReg a1, TCGReg a2) 1932{ 1933 tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0); 1934 tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0); 1935 tcg_out_mov_reg(s, COND_EQ, a0, a2); 1936} 1937 1938static void tgen_clzi(TCGContext *s, TCGType type, 1939 TCGReg a0, TCGReg a1, tcg_target_long a2) 1940{ 1941 if (a2 == 32) { 1942 tcg_out_dat_reg(s, COND_AL, INSN_CLZ, a0, 0, a1, 0); 1943 } else { 1944 tcg_out_dat_imm(s, COND_AL, ARITH_CMP, 0, a1, 0); 1945 tcg_out_dat_reg(s, COND_NE, INSN_CLZ, a0, 0, a1, 0); 1946 tcg_out_movi32(s, COND_EQ, a0, a2); 1947 } 1948} 1949 1950static const TCGOutOpBinary outop_clz = { 1951 .base.static_constraint = C_O1_I2(r, r, rIK), 1952 .out_rrr = tgen_clz, 1953 .out_rri = tgen_clzi, 1954}; 1955 1956static const TCGOutOpUnary outop_ctpop = { 1957 .base.static_constraint = C_NotImplemented, 1958}; 1959 1960static void tgen_ctz(TCGContext *s, TCGType type, 1961 TCGReg a0, TCGReg a1, TCGReg a2) 1962{ 1963 tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, a1, 0); 1964 tgen_clz(s, TCG_TYPE_I32, a0, TCG_REG_TMP, a2); 1965} 1966 1967static void tgen_ctzi(TCGContext *s, TCGType type, 1968 TCGReg a0, TCGReg a1, tcg_target_long a2) 1969{ 1970 tcg_out_dat_reg(s, COND_AL, INSN_RBIT, TCG_REG_TMP, 0, a1, 0); 1971 tgen_clzi(s, TCG_TYPE_I32, a0, TCG_REG_TMP, a2); 1972} 1973 1974static TCGConstraintSetIndex cset_ctz(TCGType type, unsigned flags) 1975{ 1976 return use_armv7_instructions ? C_O1_I2(r, r, rIK) : C_NotImplemented; 1977} 1978 1979static const TCGOutOpBinary outop_ctz = { 1980 .base.static_constraint = C_Dynamic, 1981 .base.dynamic_constraint = cset_ctz, 1982 .out_rrr = tgen_ctz, 1983 .out_rri = tgen_ctzi, 1984}; 1985 1986static TCGConstraintSetIndex cset_idiv(TCGType type, unsigned flags) 1987{ 1988 return use_idiv_instructions ? C_O1_I2(r, r, r) : C_NotImplemented; 1989} 1990 1991static void tgen_divs(TCGContext *s, TCGType type, 1992 TCGReg a0, TCGReg a1, TCGReg a2) 1993{ 1994 /* sdiv */ 1995 tcg_out32(s, 0x0710f010 | (COND_AL << 28) | (a0 << 16) | a1 | (a2 << 8)); 1996} 1997 1998static const TCGOutOpBinary outop_divs = { 1999 .base.static_constraint = C_Dynamic, 2000 .base.dynamic_constraint = cset_idiv, 2001 .out_rrr = tgen_divs, 2002}; 2003 2004static const TCGOutOpDivRem outop_divs2 = { 2005 .base.static_constraint = C_NotImplemented, 2006}; 2007 2008static void tgen_divu(TCGContext *s, TCGType type, 2009 TCGReg a0, TCGReg a1, TCGReg a2) 2010{ 2011 /* udiv */ 2012 tcg_out32(s, 0x0730f010 | (COND_AL << 28) | (a0 << 16) | a1 | (a2 << 8)); 2013} 2014 2015static const TCGOutOpBinary outop_divu = { 2016 .base.static_constraint = C_Dynamic, 2017 .base.dynamic_constraint = cset_idiv, 2018 .out_rrr = tgen_divu, 2019}; 2020 2021static const TCGOutOpDivRem outop_divu2 = { 2022 .base.static_constraint = C_NotImplemented, 2023}; 2024 2025static const TCGOutOpBinary outop_eqv = { 2026 .base.static_constraint = C_NotImplemented, 2027}; 2028 2029static void tgen_mul(TCGContext *s, TCGType type, 2030 TCGReg a0, TCGReg a1, TCGReg a2) 2031{ 2032 /* mul */ 2033 tcg_out32(s, (COND_AL << 28) | 0x90 | (a0 << 16) | (a1 << 8) | a2); 2034} 2035 2036static const TCGOutOpBinary outop_mul = { 2037 .base.static_constraint = C_O1_I2(r, r, r), 2038 .out_rrr = tgen_mul, 2039}; 2040 2041static void tgen_muls2(TCGContext *s, TCGType type, 2042 TCGReg rd0, TCGReg rd1, TCGReg rn, TCGReg rm) 2043{ 2044 /* smull */ 2045 tcg_out32(s, (COND_AL << 28) | 0x00c00090 | 2046 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn); 2047} 2048 2049static const TCGOutOpMul2 outop_muls2 = { 2050 .base.static_constraint = C_O2_I2(r, r, r, r), 2051 .out_rrrr = tgen_muls2, 2052}; 2053 2054static const TCGOutOpBinary outop_mulsh = { 2055 .base.static_constraint = C_NotImplemented, 2056}; 2057 2058static void tgen_mulu2(TCGContext *s, TCGType type, 2059 TCGReg rd0, TCGReg rd1, TCGReg rn, TCGReg rm) 2060{ 2061 /* umull */ 2062 tcg_out32(s, (COND_AL << 28) | 0x00800090 | 2063 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rn); 2064} 2065 2066static const TCGOutOpMul2 outop_mulu2 = { 2067 .base.static_constraint = C_O2_I2(r, r, r, r), 2068 .out_rrrr = tgen_mulu2, 2069}; 2070 2071static const TCGOutOpBinary outop_muluh = { 2072 .base.static_constraint = C_NotImplemented, 2073}; 2074 2075static const TCGOutOpBinary outop_nand = { 2076 .base.static_constraint = C_NotImplemented, 2077}; 2078 2079static const TCGOutOpBinary outop_nor = { 2080 .base.static_constraint = C_NotImplemented, 2081}; 2082 2083static void tgen_or(TCGContext *s, TCGType type, 2084 TCGReg a0, TCGReg a1, TCGReg a2) 2085{ 2086 tcg_out_dat_reg(s, COND_AL, ARITH_ORR, a0, a1, a2, SHIFT_IMM_LSL(0)); 2087} 2088 2089static void tgen_ori(TCGContext *s, TCGType type, 2090 TCGReg a0, TCGReg a1, tcg_target_long a2) 2091{ 2092 tcg_out_dat_imm(s, COND_AL, ARITH_ORR, a0, a1, encode_imm_nofail(a2)); 2093} 2094 2095static const TCGOutOpBinary outop_or = { 2096 .base.static_constraint = C_O1_I2(r, r, rI), 2097 .out_rrr = tgen_or, 2098 .out_rri = tgen_ori, 2099}; 2100 2101static const TCGOutOpBinary outop_orc = { 2102 .base.static_constraint = C_NotImplemented, 2103}; 2104 2105static const TCGOutOpBinary outop_rems = { 2106 .base.static_constraint = C_NotImplemented, 2107}; 2108 2109static const TCGOutOpBinary outop_remu = { 2110 .base.static_constraint = C_NotImplemented, 2111}; 2112 2113static const TCGOutOpBinary outop_rotl = { 2114 .base.static_constraint = C_NotImplemented, 2115}; 2116 2117static void tgen_rotr(TCGContext *s, TCGType type, 2118 TCGReg a0, TCGReg a1, TCGReg a2) 2119{ 2120 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_ROR(a2)); 2121} 2122 2123static void tgen_rotri(TCGContext *s, TCGType type, 2124 TCGReg a0, TCGReg a1, tcg_target_long a2) 2125{ 2126 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_IMM_ROR(a2 & 0x1f)); 2127} 2128 2129static const TCGOutOpBinary outop_rotr = { 2130 .base.static_constraint = C_O1_I2(r, r, ri), 2131 .out_rrr = tgen_rotr, 2132 .out_rri = tgen_rotri, 2133}; 2134 2135static void tgen_sar(TCGContext *s, TCGType type, 2136 TCGReg a0, TCGReg a1, TCGReg a2) 2137{ 2138 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_ASR(a2)); 2139} 2140 2141static void tgen_sari(TCGContext *s, TCGType type, 2142 TCGReg a0, TCGReg a1, tcg_target_long a2) 2143{ 2144 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, 2145 SHIFT_IMM_ASR(a2 & 0x1f)); 2146} 2147 2148static const TCGOutOpBinary outop_sar = { 2149 .base.static_constraint = C_O1_I2(r, r, ri), 2150 .out_rrr = tgen_sar, 2151 .out_rri = tgen_sari, 2152}; 2153 2154static void tgen_shl(TCGContext *s, TCGType type, 2155 TCGReg a0, TCGReg a1, TCGReg a2) 2156{ 2157 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_LSL(a2)); 2158} 2159 2160static void tgen_shli(TCGContext *s, TCGType type, 2161 TCGReg a0, TCGReg a1, tcg_target_long a2) 2162{ 2163 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, 2164 SHIFT_IMM_LSL(a2 & 0x1f)); 2165} 2166 2167static const TCGOutOpBinary outop_shl = { 2168 .base.static_constraint = C_O1_I2(r, r, ri), 2169 .out_rrr = tgen_shl, 2170 .out_rri = tgen_shli, 2171}; 2172 2173static void tgen_shr(TCGContext *s, TCGType type, 2174 TCGReg a0, TCGReg a1, TCGReg a2) 2175{ 2176 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, SHIFT_REG_LSR(a2)); 2177} 2178 2179static void tgen_shri(TCGContext *s, TCGType type, 2180 TCGReg a0, TCGReg a1, tcg_target_long a2) 2181{ 2182 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, a0, 0, a1, 2183 SHIFT_IMM_LSR(a2 & 0x1f)); 2184} 2185 2186static const TCGOutOpBinary outop_shr = { 2187 .base.static_constraint = C_O1_I2(r, r, ri), 2188 .out_rrr = tgen_shr, 2189 .out_rri = tgen_shri, 2190}; 2191 2192static void tgen_sub(TCGContext *s, TCGType type, 2193 TCGReg a0, TCGReg a1, TCGReg a2) 2194{ 2195 tcg_out_dat_reg(s, COND_AL, ARITH_SUB, a0, a1, a2, SHIFT_IMM_LSL(0)); 2196} 2197 2198static void tgen_subfi(TCGContext *s, TCGType type, 2199 TCGReg a0, tcg_target_long a1, TCGReg a2) 2200{ 2201 tcg_out_dat_imm(s, COND_AL, ARITH_RSB, a0, a2, encode_imm_nofail(a1)); 2202} 2203 2204static const TCGOutOpSubtract outop_sub = { 2205 .base.static_constraint = C_O1_I2(r, rI, r), 2206 .out_rrr = tgen_sub, 2207 .out_rir = tgen_subfi, 2208}; 2209 2210static void tgen_subbo_rrr(TCGContext *s, TCGType type, 2211 TCGReg a0, TCGReg a1, TCGReg a2) 2212{ 2213 tcg_out_dat_reg(s, COND_AL, ARITH_SUB | TO_CPSR, 2214 a0, a1, a2, SHIFT_IMM_LSL(0)); 2215} 2216 2217static void tgen_subbo_rri(TCGContext *s, TCGType type, 2218 TCGReg a0, TCGReg a1, tcg_target_long a2) 2219{ 2220 tcg_out_dat_IN(s, COND_AL, ARITH_SUB | TO_CPSR, ARITH_ADD | TO_CPSR, 2221 a0, a1, a2); 2222} 2223 2224static void tgen_subbo_rir(TCGContext *s, TCGType type, 2225 TCGReg a0, tcg_target_long a1, TCGReg a2) 2226{ 2227 tcg_out_dat_imm(s, COND_AL, ARITH_RSB | TO_CPSR, 2228 a0, a2, encode_imm_nofail(a1)); 2229} 2230 2231static void tgen_subbo_rii(TCGContext *s, TCGType type, 2232 TCGReg a0, tcg_target_long a1, tcg_target_long a2) 2233{ 2234 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2); 2235 tgen_subbo_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP); 2236} 2237 2238static const TCGOutOpAddSubCarry outop_subbo = { 2239 .base.static_constraint = C_O1_I2(r, rI, rIN), 2240 .out_rrr = tgen_subbo_rrr, 2241 .out_rri = tgen_subbo_rri, 2242 .out_rir = tgen_subbo_rir, 2243 .out_rii = tgen_subbo_rii, 2244}; 2245 2246static void tgen_subbi_rrr(TCGContext *s, TCGType type, 2247 TCGReg a0, TCGReg a1, TCGReg a2) 2248{ 2249 tcg_out_dat_reg(s, COND_AL, ARITH_SBC, 2250 a0, a1, a2, SHIFT_IMM_LSL(0)); 2251} 2252 2253static void tgen_subbi_rri(TCGContext *s, TCGType type, 2254 TCGReg a0, TCGReg a1, tcg_target_long a2) 2255{ 2256 tcg_out_dat_IK(s, COND_AL, ARITH_SBC, ARITH_ADC, a0, a1, a2); 2257} 2258 2259static void tgen_subbi_rir(TCGContext *s, TCGType type, 2260 TCGReg a0, tcg_target_long a1, TCGReg a2) 2261{ 2262 tcg_out_dat_imm(s, COND_AL, ARITH_RSC, a0, a2, encode_imm_nofail(a1)); 2263} 2264 2265static void tgen_subbi_rii(TCGContext *s, TCGType type, 2266 TCGReg a0, tcg_target_long a1, tcg_target_long a2) 2267{ 2268 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2); 2269 tgen_subbi_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP); 2270} 2271 2272static const TCGOutOpAddSubCarry outop_subbi = { 2273 .base.static_constraint = C_O1_I2(r, rI, rIK), 2274 .out_rrr = tgen_subbi_rrr, 2275 .out_rri = tgen_subbi_rri, 2276 .out_rir = tgen_subbi_rir, 2277 .out_rii = tgen_subbi_rii, 2278}; 2279 2280static void tgen_subbio_rrr(TCGContext *s, TCGType type, 2281 TCGReg a0, TCGReg a1, TCGReg a2) 2282{ 2283 tcg_out_dat_reg(s, COND_AL, ARITH_SBC | TO_CPSR, 2284 a0, a1, a2, SHIFT_IMM_LSL(0)); 2285} 2286 2287static void tgen_subbio_rri(TCGContext *s, TCGType type, 2288 TCGReg a0, TCGReg a1, tcg_target_long a2) 2289{ 2290 tcg_out_dat_IK(s, COND_AL, ARITH_SBC | TO_CPSR, ARITH_ADC | TO_CPSR, 2291 a0, a1, a2); 2292} 2293 2294static void tgen_subbio_rir(TCGContext *s, TCGType type, 2295 TCGReg a0, tcg_target_long a1, TCGReg a2) 2296{ 2297 tcg_out_dat_imm(s, COND_AL, ARITH_RSC | TO_CPSR, 2298 a0, a2, encode_imm_nofail(a1)); 2299} 2300 2301static void tgen_subbio_rii(TCGContext *s, TCGType type, 2302 TCGReg a0, tcg_target_long a1, tcg_target_long a2) 2303{ 2304 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP, a2); 2305 tgen_subbio_rir(s, TCG_TYPE_I32, a0, a1, TCG_REG_TMP); 2306} 2307 2308static const TCGOutOpAddSubCarry outop_subbio = { 2309 .base.static_constraint = C_O1_I2(r, rI, rIK), 2310 .out_rrr = tgen_subbio_rrr, 2311 .out_rri = tgen_subbio_rri, 2312 .out_rir = tgen_subbio_rir, 2313 .out_rii = tgen_subbio_rii, 2314}; 2315 2316static void tcg_out_set_borrow(TCGContext *s) 2317{ 2318 tcg_out_movi_apsr_c(s, 0); /* borrow = !carry */ 2319} 2320 2321static void tgen_xor(TCGContext *s, TCGType type, 2322 TCGReg a0, TCGReg a1, TCGReg a2) 2323{ 2324 tcg_out_dat_reg(s, COND_AL, ARITH_EOR, a0, a1, a2, SHIFT_IMM_LSL(0)); 2325} 2326 2327static void tgen_xori(TCGContext *s, TCGType type, 2328 TCGReg a0, TCGReg a1, tcg_target_long a2) 2329{ 2330 tcg_out_dat_imm(s, COND_AL, ARITH_EOR, a0, a1, encode_imm_nofail(a2)); 2331} 2332 2333static const TCGOutOpBinary outop_xor = { 2334 .base.static_constraint = C_O1_I2(r, r, rI), 2335 .out_rrr = tgen_xor, 2336 .out_rri = tgen_xori, 2337}; 2338 2339static void tgen_bswap16(TCGContext *s, TCGType type, 2340 TCGReg rd, TCGReg rn, unsigned flags) 2341{ 2342 if (flags & TCG_BSWAP_OS) { 2343 /* revsh */ 2344 tcg_out32(s, 0x06ff0fb0 | (COND_AL << 28) | (rd << 12) | rn); 2345 return; 2346 } 2347 2348 /* rev16 */ 2349 tcg_out32(s, 0x06bf0fb0 | (COND_AL << 28) | (rd << 12) | rn); 2350 if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) { 2351 tcg_out_ext16u(s, rd, rd); 2352 } 2353} 2354 2355static const TCGOutOpBswap outop_bswap16 = { 2356 .base.static_constraint = C_O1_I1(r, r), 2357 .out_rr = tgen_bswap16, 2358}; 2359 2360static void tgen_bswap32(TCGContext *s, TCGType type, 2361 TCGReg rd, TCGReg rn, unsigned flags) 2362{ 2363 /* rev */ 2364 tcg_out32(s, 0x06bf0f30 | (COND_AL << 28) | (rd << 12) | rn); 2365} 2366 2367static const TCGOutOpBswap outop_bswap32 = { 2368 .base.static_constraint = C_O1_I1(r, r), 2369 .out_rr = tgen_bswap32, 2370}; 2371 2372static const TCGOutOpUnary outop_bswap64 = { 2373 .base.static_constraint = C_NotImplemented, 2374}; 2375 2376static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 2377{ 2378 tgen_subfi(s, type, a0, 0, a1); 2379} 2380 2381static const TCGOutOpUnary outop_neg = { 2382 .base.static_constraint = C_O1_I1(r, r), 2383 .out_rr = tgen_neg, 2384}; 2385 2386static void tgen_not(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 2387{ 2388 tcg_out_dat_reg(s, COND_AL, ARITH_MVN, a0, 0, a1, SHIFT_IMM_LSL(0)); 2389} 2390 2391static const TCGOutOpUnary outop_not = { 2392 .base.static_constraint = C_O1_I1(r, r), 2393 .out_rr = tgen_not, 2394}; 2395 2396static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond, 2397 TCGReg a0, TCGReg a1, TCGLabel *l) 2398{ 2399 cond = tgen_cmp(s, cond, a0, a1); 2400 tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l); 2401} 2402 2403static void tgen_brcondi(TCGContext *s, TCGType type, TCGCond cond, 2404 TCGReg a0, tcg_target_long a1, TCGLabel *l) 2405{ 2406 cond = tgen_cmpi(s, cond, a0, a1); 2407 tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l); 2408} 2409 2410static const TCGOutOpBrcond outop_brcond = { 2411 .base.static_constraint = C_O0_I2(r, rIN), 2412 .out_rr = tgen_brcond, 2413 .out_ri = tgen_brcondi, 2414}; 2415 2416static void finish_setcond(TCGContext *s, TCGCond cond, TCGReg ret, bool neg) 2417{ 2418 tcg_out_movi32(s, tcg_cond_to_arm_cond[tcg_invert_cond(cond)], ret, 0); 2419 tcg_out_movi32(s, tcg_cond_to_arm_cond[cond], ret, neg ? -1 : 1); 2420} 2421 2422static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, 2423 TCGReg a0, TCGReg a1, TCGReg a2) 2424{ 2425 cond = tgen_cmp(s, cond, a1, a2); 2426 finish_setcond(s, cond, a0, false); 2427} 2428 2429static void tgen_setcondi(TCGContext *s, TCGType type, TCGCond cond, 2430 TCGReg a0, TCGReg a1, tcg_target_long a2) 2431{ 2432 cond = tgen_cmpi(s, cond, a1, a2); 2433 finish_setcond(s, cond, a0, false); 2434} 2435 2436static const TCGOutOpSetcond outop_setcond = { 2437 .base.static_constraint = C_O1_I2(r, r, rIN), 2438 .out_rrr = tgen_setcond, 2439 .out_rri = tgen_setcondi, 2440}; 2441 2442static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, 2443 TCGReg a0, TCGReg a1, TCGReg a2) 2444{ 2445 cond = tgen_cmp(s, cond, a1, a2); 2446 finish_setcond(s, cond, a0, true); 2447} 2448 2449static void tgen_negsetcondi(TCGContext *s, TCGType type, TCGCond cond, 2450 TCGReg a0, TCGReg a1, tcg_target_long a2) 2451{ 2452 cond = tgen_cmpi(s, cond, a1, a2); 2453 finish_setcond(s, cond, a0, true); 2454} 2455 2456static const TCGOutOpSetcond outop_negsetcond = { 2457 .base.static_constraint = C_O1_I2(r, r, rIN), 2458 .out_rrr = tgen_negsetcond, 2459 .out_rri = tgen_negsetcondi, 2460}; 2461 2462static void tgen_movcond(TCGContext *s, TCGType type, TCGCond cond, 2463 TCGReg ret, TCGReg c1, TCGArg c2, bool const_c2, 2464 TCGArg vt, bool const_vt, TCGArg vf, bool consf_vf) 2465{ 2466 cond = tcg_out_cmp(s, cond, c1, c2, const_c2); 2467 tcg_out_dat_rIK(s, tcg_cond_to_arm_cond[cond], ARITH_MOV, ARITH_MVN, 2468 ret, 0, vt, const_vt); 2469} 2470 2471static const TCGOutOpMovcond outop_movcond = { 2472 .base.static_constraint = C_O1_I4(r, r, rIN, rIK, 0), 2473 .out = tgen_movcond, 2474}; 2475 2476static void tgen_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, 2477 TCGArg bl, bool const_bl, TCGArg bh, bool const_bh, 2478 TCGLabel *l) 2479{ 2480 cond = tcg_out_cmp2(s, cond, al, ah, bl, const_bl, bh, const_bh); 2481 tcg_out_goto_label(s, tcg_cond_to_arm_cond[cond], l); 2482} 2483 2484static const TCGOutOpBrcond2 outop_brcond2 = { 2485 .base.static_constraint = C_O0_I4(r, r, rI, rI), 2486 .out = tgen_brcond2, 2487}; 2488 2489static void tgen_setcond2(TCGContext *s, TCGCond cond, TCGReg ret, 2490 TCGReg al, TCGReg ah, 2491 TCGArg bl, bool const_bl, 2492 TCGArg bh, bool const_bh) 2493{ 2494 cond = tcg_out_cmp2(s, cond, al, ah, bl, const_bl, bh, const_bh); 2495 finish_setcond(s, cond, ret, false); 2496} 2497 2498static const TCGOutOpSetcond2 outop_setcond2 = { 2499 .base.static_constraint = C_O1_I4(r, r, r, rI, rI), 2500 .out = tgen_setcond2, 2501}; 2502 2503static void tgen_extract2(TCGContext *s, TCGType type, TCGReg a0, 2504 TCGReg a1, TCGReg a2, unsigned shr) 2505{ 2506 /* We can do extract2 in 2 insns, vs the 3 required otherwise. */ 2507 tgen_shli(s, TCG_TYPE_I32, TCG_REG_TMP, a2, 32 - shr); 2508 tcg_out_dat_reg(s, COND_AL, ARITH_ORR, a0, TCG_REG_TMP, 2509 a1, SHIFT_IMM_LSR(shr)); 2510} 2511 2512static const TCGOutOpExtract2 outop_extract2 = { 2513 .base.static_constraint = C_O1_I2(r, r, r), 2514 .out_rrr = tgen_extract2, 2515}; 2516 2517static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type, 2518 const TCGArg args[TCG_MAX_OP_ARGS], 2519 const int const_args[TCG_MAX_OP_ARGS]) 2520{ 2521 switch (opc) { 2522 case INDEX_op_goto_ptr: 2523 tcg_out_b_reg(s, COND_AL, args[0]); 2524 break; 2525 case INDEX_op_br: 2526 tcg_out_goto_label(s, COND_AL, arg_label(args[0])); 2527 break; 2528 2529 case INDEX_op_ld8u_i32: 2530 tcg_out_ld8u(s, COND_AL, args[0], args[1], args[2]); 2531 break; 2532 case INDEX_op_ld8s_i32: 2533 tcg_out_ld8s(s, COND_AL, args[0], args[1], args[2]); 2534 break; 2535 case INDEX_op_ld16u_i32: 2536 tcg_out_ld16u(s, COND_AL, args[0], args[1], args[2]); 2537 break; 2538 case INDEX_op_ld16s_i32: 2539 tcg_out_ld16s(s, COND_AL, args[0], args[1], args[2]); 2540 break; 2541 case INDEX_op_ld_i32: 2542 tcg_out_ld32u(s, COND_AL, args[0], args[1], args[2]); 2543 break; 2544 case INDEX_op_st8_i32: 2545 tcg_out_st8(s, COND_AL, args[0], args[1], args[2]); 2546 break; 2547 case INDEX_op_st16_i32: 2548 tcg_out_st16(s, COND_AL, args[0], args[1], args[2]); 2549 break; 2550 case INDEX_op_st_i32: 2551 tcg_out_st32(s, COND_AL, args[0], args[1], args[2]); 2552 break; 2553 2554 case INDEX_op_qemu_ld_i32: 2555 tcg_out_qemu_ld(s, args[0], -1, args[1], args[2], TCG_TYPE_I32); 2556 break; 2557 case INDEX_op_qemu_ld_i64: 2558 tcg_out_qemu_ld(s, args[0], args[1], args[2], args[3], TCG_TYPE_I64); 2559 break; 2560 2561 case INDEX_op_qemu_st_i32: 2562 tcg_out_qemu_st(s, args[0], -1, args[1], args[2], TCG_TYPE_I32); 2563 break; 2564 case INDEX_op_qemu_st_i64: 2565 tcg_out_qemu_st(s, args[0], args[1], args[2], args[3], TCG_TYPE_I64); 2566 break; 2567 2568 case INDEX_op_mb: 2569 tcg_out_mb(s, args[0]); 2570 break; 2571 2572 case INDEX_op_call: /* Always emitted via tcg_out_call. */ 2573 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */ 2574 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */ 2575 default: 2576 g_assert_not_reached(); 2577 } 2578} 2579 2580static TCGConstraintSetIndex 2581tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) 2582{ 2583 switch (op) { 2584 case INDEX_op_goto_ptr: 2585 return C_O0_I1(r); 2586 2587 case INDEX_op_ld8u_i32: 2588 case INDEX_op_ld8s_i32: 2589 case INDEX_op_ld16u_i32: 2590 case INDEX_op_ld16s_i32: 2591 case INDEX_op_ld_i32: 2592 return C_O1_I1(r, r); 2593 2594 case INDEX_op_st8_i32: 2595 case INDEX_op_st16_i32: 2596 case INDEX_op_st_i32: 2597 return C_O0_I2(r, r); 2598 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