2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Andrzej Zaborowski
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
45 static const int tcg_target_reg_alloc_order[] = {
63 static const int tcg_target_call_iarg_regs[4] = {
64 TCG_REG_R0, TCG_REG_R1, TCG_REG_R2, TCG_REG_R3
66 static const int tcg_target_call_oarg_regs[2] = {
67 TCG_REG_R0, TCG_REG_R1
70 static void patch_reloc(uint8_t *code_ptr, int type,
71 tcg_target_long value, tcg_target_long addend)
75 *(uint32_t *) code_ptr = value;
84 *(uint32_t *) code_ptr = ((*(uint32_t *) code_ptr) & 0xff000000) |
85 (((value - ((tcg_target_long) code_ptr + 8)) >> 2) & 0xffffff);
90 /* maximum number of register used for input function arguments */
91 static inline int tcg_target_get_call_iarg_regs_count(int flags)
96 /* parse target specific constraints */
97 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
104 ct->ct |= TCG_CT_CONST_ARM;
108 #ifndef CONFIG_SOFTMMU
114 ct->ct |= TCG_CT_REG;
115 tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
118 #ifdef CONFIG_SOFTMMU
119 /* qemu_ld/st inputs (unless 'X', 'd' or 'D') */
121 ct->ct |= TCG_CT_REG;
122 tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
123 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
124 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
127 /* qemu_ld64 data_reg */
129 ct->ct |= TCG_CT_REG;
130 tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
131 /* r1 is still needed to load data_reg2, so don't use it. */
132 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
135 /* qemu_ld/st64 data_reg2 */
137 ct->ct |= TCG_CT_REG;
138 tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
139 /* r0, r1 and optionally r2 will be overwritten by the address
140 * and the low word of data, so don't use these. */
141 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
142 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
143 # if TARGET_LONG_BITS == 64
144 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R2);
148 # if TARGET_LONG_BITS == 64
149 /* qemu_ld/st addr_reg2 */
151 ct->ct |= TCG_CT_REG;
152 tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
153 /* r0 will be overwritten by the low word of base, so don't use it. */
154 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
155 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
161 ct->ct |= TCG_CT_REG;
162 tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
163 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
167 ct->ct |= TCG_CT_REG;
168 tcg_regset_set32(ct->u.regs, 0, (1 << TCG_TARGET_NB_REGS) - 1);
169 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
170 tcg_regset_reset_reg(ct->u.regs, TCG_REG_R1);
183 static inline int check_fit_imm(uint32_t imm)
185 /* XXX: use rotation */
186 return (imm & ~0xff) == 0;
189 /* Test if a constant matches the constraint.
190 * TODO: define constraints for:
192 * ldr/str offset: between -0xfff and 0xfff
193 * ldrh/strh offset: between -0xff and 0xff
194 * mov operand2: values represented with x << (2 * y), x < 0x100
195 * add, sub, eor...: ditto
197 static inline int tcg_target_const_match(tcg_target_long val,
198 const TCGArgConstraint *arg_ct)
202 if (ct & TCG_CT_CONST)
204 else if ((ct & TCG_CT_CONST_ARM) && check_fit_imm(val))
210 enum arm_data_opc_e {
228 #define TO_CPSR(opc) \
229 ((opc == ARITH_CMP || opc == ARITH_CMN || opc == ARITH_TST) << 20)
231 #define SHIFT_IMM_LSL(im) (((im) << 7) | 0x00)
232 #define SHIFT_IMM_LSR(im) (((im) << 7) | 0x20)
233 #define SHIFT_IMM_ASR(im) (((im) << 7) | 0x40)
234 #define SHIFT_IMM_ROR(im) (((im) << 7) | 0x60)
235 #define SHIFT_REG_LSL(rs) (((rs) << 8) | 0x10)
236 #define SHIFT_REG_LSR(rs) (((rs) << 8) | 0x30)
237 #define SHIFT_REG_ASR(rs) (((rs) << 8) | 0x50)
238 #define SHIFT_REG_ROR(rs) (((rs) << 8) | 0x70)
240 enum arm_cond_code_e {
243 COND_CS = 0x2, /* Unsigned greater or equal */
244 COND_CC = 0x3, /* Unsigned less than */
245 COND_MI = 0x4, /* Negative */
246 COND_PL = 0x5, /* Zero or greater */
247 COND_VS = 0x6, /* Overflow */
248 COND_VC = 0x7, /* No overflow */
249 COND_HI = 0x8, /* Unsigned greater than */
250 COND_LS = 0x9, /* Unsigned less or equal */
258 static const uint8_t tcg_cond_to_arm_cond[10] = {
259 [TCG_COND_EQ] = COND_EQ,
260 [TCG_COND_NE] = COND_NE,
261 [TCG_COND_LT] = COND_LT,
262 [TCG_COND_GE] = COND_GE,
263 [TCG_COND_LE] = COND_LE,
264 [TCG_COND_GT] = COND_GT,
266 [TCG_COND_LTU] = COND_CC,
267 [TCG_COND_GEU] = COND_CS,
268 [TCG_COND_LEU] = COND_LS,
269 [TCG_COND_GTU] = COND_HI,
272 static inline void tcg_out_bx(TCGContext *s, int cond, int rn)
274 tcg_out32(s, (cond << 28) | 0x012fff10 | rn);
277 static inline void tcg_out_b(TCGContext *s, int cond, int32_t offset)
279 tcg_out32(s, (cond << 28) | 0x0a000000 |
280 (((offset - 8) >> 2) & 0x00ffffff));
283 static inline void tcg_out_b_noaddr(TCGContext *s, int cond)
285 #ifdef HOST_WORDS_BIGENDIAN
286 tcg_out8(s, (cond << 4) | 0x0a);
290 tcg_out8(s, (cond << 4) | 0x0a);
294 static inline void tcg_out_bl(TCGContext *s, int cond, int32_t offset)
296 tcg_out32(s, (cond << 28) | 0x0b000000 |
297 (((offset - 8) >> 2) & 0x00ffffff));
300 static inline void tcg_out_dat_reg(TCGContext *s,
301 int cond, int opc, int rd, int rn, int rm, int shift)
303 tcg_out32(s, (cond << 28) | (0 << 25) | (opc << 21) | TO_CPSR(opc) |
304 (rn << 16) | (rd << 12) | shift | rm);
307 static inline void tcg_out_dat_reg2(TCGContext *s,
308 int cond, int opc0, int opc1, int rd0, int rd1,
309 int rn0, int rn1, int rm0, int rm1, int shift)
311 if (rd0 == rn1 || rd0 == rm1) {
312 tcg_out32(s, (cond << 28) | (0 << 25) | (opc0 << 21) | (1 << 20) |
313 (rn0 << 16) | (8 << 12) | shift | rm0);
314 tcg_out32(s, (cond << 28) | (0 << 25) | (opc1 << 21) |
315 (rn1 << 16) | (rd1 << 12) | shift | rm1);
316 tcg_out_dat_reg(s, cond, ARITH_MOV,
317 rd0, 0, TCG_REG_R8, SHIFT_IMM_LSL(0));
319 tcg_out32(s, (cond << 28) | (0 << 25) | (opc0 << 21) | (1 << 20) |
320 (rn0 << 16) | (rd0 << 12) | shift | rm0);
321 tcg_out32(s, (cond << 28) | (0 << 25) | (opc1 << 21) |
322 (rn1 << 16) | (rd1 << 12) | shift | rm1);
326 static inline void tcg_out_dat_imm(TCGContext *s,
327 int cond, int opc, int rd, int rn, int im)
329 tcg_out32(s, (cond << 28) | (1 << 25) | (opc << 21) | TO_CPSR(opc) |
330 (rn << 16) | (rd << 12) | im);
333 static inline void tcg_out_movi32(TCGContext *s,
334 int cond, int rd, int32_t arg)
336 int offset = (uint32_t) arg - ((uint32_t) s->code_ptr + 8);
338 /* TODO: This is very suboptimal, we can easily have a constant
339 * pool somewhere after all the instructions. */
341 if (arg < 0 && arg > -0x100)
342 return tcg_out_dat_imm(s, cond, ARITH_MVN, rd, 0, (~arg) & 0xff);
344 if (offset < 0x100 && offset > -0x100)
346 tcg_out_dat_imm(s, cond, ARITH_ADD, rd, 15, offset) :
347 tcg_out_dat_imm(s, cond, ARITH_SUB, rd, 15, -offset);
349 #ifdef __ARM_ARCH_7A__
352 tcg_out32(s, (cond << 28) | 0x03000000 | (rd << 12)
353 | ((arg << 4) & 0x000f0000) | (arg & 0xfff));
354 if (arg & 0xffff0000)
356 tcg_out32(s, (cond << 28) | 0x03400000 | (rd << 12)
357 | ((arg >> 12) & 0x000f0000) | ((arg >> 16) & 0xfff));
359 tcg_out_dat_imm(s, cond, ARITH_MOV, rd, 0, arg & 0xff);
360 if (arg & 0x0000ff00)
361 tcg_out_dat_imm(s, cond, ARITH_ORR, rd, rd,
362 ((arg >> 8) & 0xff) | 0xc00);
363 if (arg & 0x00ff0000)
364 tcg_out_dat_imm(s, cond, ARITH_ORR, rd, rd,
365 ((arg >> 16) & 0xff) | 0x800);
366 if (arg & 0xff000000)
367 tcg_out_dat_imm(s, cond, ARITH_ORR, rd, rd,
368 ((arg >> 24) & 0xff) | 0x400);
372 static inline void tcg_out_mul32(TCGContext *s,
373 int cond, int rd, int rs, int rm)
376 tcg_out32(s, (cond << 28) | (rd << 16) | (0 << 12) |
377 (rs << 8) | 0x90 | rm);
379 tcg_out32(s, (cond << 28) | (rd << 16) | (0 << 12) |
380 (rm << 8) | 0x90 | rs);
382 tcg_out32(s, (cond << 28) | ( 8 << 16) | (0 << 12) |
383 (rs << 8) | 0x90 | rm);
384 tcg_out_dat_reg(s, cond, ARITH_MOV,
385 rd, 0, 8, SHIFT_IMM_LSL(0));
389 static inline void tcg_out_umull32(TCGContext *s,
390 int cond, int rd0, int rd1, int rs, int rm)
392 if (rd0 != rm && rd1 != rm)
393 tcg_out32(s, (cond << 28) | 0x800090 |
394 (rd1 << 16) | (rd0 << 12) | (rs << 8) | rm);
395 else if (rd0 != rs && rd1 != rs)
396 tcg_out32(s, (cond << 28) | 0x800090 |
397 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rs);
399 tcg_out_dat_reg(s, cond, ARITH_MOV,
400 TCG_REG_R8, 0, rm, SHIFT_IMM_LSL(0));
401 tcg_out32(s, (cond << 28) | 0x800098 |
402 (rd1 << 16) | (rd0 << 12) | (rs << 8));
406 static inline void tcg_out_smull32(TCGContext *s,
407 int cond, int rd0, int rd1, int rs, int rm)
409 if (rd0 != rm && rd1 != rm)
410 tcg_out32(s, (cond << 28) | 0xc00090 |
411 (rd1 << 16) | (rd0 << 12) | (rs << 8) | rm);
412 else if (rd0 != rs && rd1 != rs)
413 tcg_out32(s, (cond << 28) | 0xc00090 |
414 (rd1 << 16) | (rd0 << 12) | (rm << 8) | rs);
416 tcg_out_dat_reg(s, cond, ARITH_MOV,
417 TCG_REG_R8, 0, rm, SHIFT_IMM_LSL(0));
418 tcg_out32(s, (cond << 28) | 0xc00098 |
419 (rd1 << 16) | (rd0 << 12) | (rs << 8));
423 static inline void tcg_out_ld32_12(TCGContext *s, int cond,
424 int rd, int rn, tcg_target_long im)
427 tcg_out32(s, (cond << 28) | 0x05900000 |
428 (rn << 16) | (rd << 12) | (im & 0xfff));
430 tcg_out32(s, (cond << 28) | 0x05100000 |
431 (rn << 16) | (rd << 12) | ((-im) & 0xfff));
434 static inline void tcg_out_st32_12(TCGContext *s, int cond,
435 int rd, int rn, tcg_target_long im)
438 tcg_out32(s, (cond << 28) | 0x05800000 |
439 (rn << 16) | (rd << 12) | (im & 0xfff));
441 tcg_out32(s, (cond << 28) | 0x05000000 |
442 (rn << 16) | (rd << 12) | ((-im) & 0xfff));
445 static inline void tcg_out_ld32_r(TCGContext *s, int cond,
446 int rd, int rn, int rm)
448 tcg_out32(s, (cond << 28) | 0x07900000 |
449 (rn << 16) | (rd << 12) | rm);
452 static inline void tcg_out_st32_r(TCGContext *s, int cond,
453 int rd, int rn, int rm)
455 tcg_out32(s, (cond << 28) | 0x07800000 |
456 (rn << 16) | (rd << 12) | rm);
459 /* Register pre-increment with base writeback. */
460 static inline void tcg_out_ld32_rwb(TCGContext *s, int cond,
461 int rd, int rn, int rm)
463 tcg_out32(s, (cond << 28) | 0x07b00000 |
464 (rn << 16) | (rd << 12) | rm);
467 static inline void tcg_out_st32_rwb(TCGContext *s, int cond,
468 int rd, int rn, int rm)
470 tcg_out32(s, (cond << 28) | 0x07a00000 |
471 (rn << 16) | (rd << 12) | rm);
474 static inline void tcg_out_ld16u_8(TCGContext *s, int cond,
475 int rd, int rn, tcg_target_long im)
478 tcg_out32(s, (cond << 28) | 0x01d000b0 |
479 (rn << 16) | (rd << 12) |
480 ((im & 0xf0) << 4) | (im & 0xf));
482 tcg_out32(s, (cond << 28) | 0x015000b0 |
483 (rn << 16) | (rd << 12) |
484 (((-im) & 0xf0) << 4) | ((-im) & 0xf));
487 static inline void tcg_out_st16u_8(TCGContext *s, int cond,
488 int rd, int rn, tcg_target_long im)
491 tcg_out32(s, (cond << 28) | 0x01c000b0 |
492 (rn << 16) | (rd << 12) |
493 ((im & 0xf0) << 4) | (im & 0xf));
495 tcg_out32(s, (cond << 28) | 0x014000b0 |
496 (rn << 16) | (rd << 12) |
497 (((-im) & 0xf0) << 4) | ((-im) & 0xf));
500 static inline void tcg_out_ld16u_r(TCGContext *s, int cond,
501 int rd, int rn, int rm)
503 tcg_out32(s, (cond << 28) | 0x019000b0 |
504 (rn << 16) | (rd << 12) | rm);
507 static inline void tcg_out_st16u_r(TCGContext *s, int cond,
508 int rd, int rn, int rm)
510 tcg_out32(s, (cond << 28) | 0x018000b0 |
511 (rn << 16) | (rd << 12) | rm);
514 static inline void tcg_out_ld16s_8(TCGContext *s, int cond,
515 int rd, int rn, tcg_target_long im)
518 tcg_out32(s, (cond << 28) | 0x01d000f0 |
519 (rn << 16) | (rd << 12) |
520 ((im & 0xf0) << 4) | (im & 0xf));
522 tcg_out32(s, (cond << 28) | 0x015000f0 |
523 (rn << 16) | (rd << 12) |
524 (((-im) & 0xf0) << 4) | ((-im) & 0xf));
527 static inline void tcg_out_st16s_8(TCGContext *s, int cond,
528 int rd, int rn, tcg_target_long im)
531 tcg_out32(s, (cond << 28) | 0x01c000f0 |
532 (rn << 16) | (rd << 12) |
533 ((im & 0xf0) << 4) | (im & 0xf));
535 tcg_out32(s, (cond << 28) | 0x014000f0 |
536 (rn << 16) | (rd << 12) |
537 (((-im) & 0xf0) << 4) | ((-im) & 0xf));
540 static inline void tcg_out_ld16s_r(TCGContext *s, int cond,
541 int rd, int rn, int rm)
543 tcg_out32(s, (cond << 28) | 0x019000f0 |
544 (rn << 16) | (rd << 12) | rm);
547 static inline void tcg_out_st16s_r(TCGContext *s, int cond,
548 int rd, int rn, int rm)
550 tcg_out32(s, (cond << 28) | 0x018000f0 |
551 (rn << 16) | (rd << 12) | rm);
554 static inline void tcg_out_ld8_12(TCGContext *s, int cond,
555 int rd, int rn, tcg_target_long im)
558 tcg_out32(s, (cond << 28) | 0x05d00000 |
559 (rn << 16) | (rd << 12) | (im & 0xfff));
561 tcg_out32(s, (cond << 28) | 0x05500000 |
562 (rn << 16) | (rd << 12) | ((-im) & 0xfff));
565 static inline void tcg_out_st8_12(TCGContext *s, int cond,
566 int rd, int rn, tcg_target_long im)
569 tcg_out32(s, (cond << 28) | 0x05c00000 |
570 (rn << 16) | (rd << 12) | (im & 0xfff));
572 tcg_out32(s, (cond << 28) | 0x05400000 |
573 (rn << 16) | (rd << 12) | ((-im) & 0xfff));
576 static inline void tcg_out_ld8_r(TCGContext *s, int cond,
577 int rd, int rn, int rm)
579 tcg_out32(s, (cond << 28) | 0x07d00000 |
580 (rn << 16) | (rd << 12) | rm);
583 static inline void tcg_out_st8_r(TCGContext *s, int cond,
584 int rd, int rn, int rm)
586 tcg_out32(s, (cond << 28) | 0x07c00000 |
587 (rn << 16) | (rd << 12) | rm);
590 static inline void tcg_out_ld8s_8(TCGContext *s, int cond,
591 int rd, int rn, tcg_target_long im)
594 tcg_out32(s, (cond << 28) | 0x01d000d0 |
595 (rn << 16) | (rd << 12) |
596 ((im & 0xf0) << 4) | (im & 0xf));
598 tcg_out32(s, (cond << 28) | 0x015000d0 |
599 (rn << 16) | (rd << 12) |
600 (((-im) & 0xf0) << 4) | ((-im) & 0xf));
603 static inline void tcg_out_st8s_8(TCGContext *s, int cond,
604 int rd, int rn, tcg_target_long im)
607 tcg_out32(s, (cond << 28) | 0x01c000d0 |
608 (rn << 16) | (rd << 12) |
609 ((im & 0xf0) << 4) | (im & 0xf));
611 tcg_out32(s, (cond << 28) | 0x014000d0 |
612 (rn << 16) | (rd << 12) |
613 (((-im) & 0xf0) << 4) | ((-im) & 0xf));
616 static inline void tcg_out_ld8s_r(TCGContext *s, int cond,
617 int rd, int rn, int rm)
619 tcg_out32(s, (cond << 28) | 0x019000d0 |
620 (rn << 16) | (rd << 12) | rm);
623 static inline void tcg_out_st8s_r(TCGContext *s, int cond,
624 int rd, int rn, int rm)
626 tcg_out32(s, (cond << 28) | 0x018000d0 |
627 (rn << 16) | (rd << 12) | rm);
630 static inline void tcg_out_ld32u(TCGContext *s, int cond,
631 int rd, int rn, int32_t offset)
633 if (offset > 0xfff || offset < -0xfff) {
634 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
635 tcg_out_ld32_r(s, cond, rd, rn, TCG_REG_R8);
637 tcg_out_ld32_12(s, cond, rd, rn, offset);
640 static inline void tcg_out_st32(TCGContext *s, int cond,
641 int rd, int rn, int32_t offset)
643 if (offset > 0xfff || offset < -0xfff) {
644 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
645 tcg_out_st32_r(s, cond, rd, rn, TCG_REG_R8);
647 tcg_out_st32_12(s, cond, rd, rn, offset);
650 static inline void tcg_out_ld16u(TCGContext *s, int cond,
651 int rd, int rn, int32_t offset)
653 if (offset > 0xff || offset < -0xff) {
654 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
655 tcg_out_ld16u_r(s, cond, rd, rn, TCG_REG_R8);
657 tcg_out_ld16u_8(s, cond, rd, rn, offset);
660 static inline void tcg_out_ld16s(TCGContext *s, int cond,
661 int rd, int rn, int32_t offset)
663 if (offset > 0xff || offset < -0xff) {
664 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
665 tcg_out_ld16s_r(s, cond, rd, rn, TCG_REG_R8);
667 tcg_out_ld16s_8(s, cond, rd, rn, offset);
670 static inline void tcg_out_st16u(TCGContext *s, int cond,
671 int rd, int rn, int32_t offset)
673 if (offset > 0xff || offset < -0xff) {
674 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
675 tcg_out_st16u_r(s, cond, rd, rn, TCG_REG_R8);
677 tcg_out_st16u_8(s, cond, rd, rn, offset);
680 static inline void tcg_out_ld8u(TCGContext *s, int cond,
681 int rd, int rn, int32_t offset)
683 if (offset > 0xfff || offset < -0xfff) {
684 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
685 tcg_out_ld8_r(s, cond, rd, rn, TCG_REG_R8);
687 tcg_out_ld8_12(s, cond, rd, rn, offset);
690 static inline void tcg_out_ld8s(TCGContext *s, int cond,
691 int rd, int rn, int32_t offset)
693 if (offset > 0xff || offset < -0xff) {
694 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
695 tcg_out_ld8s_r(s, cond, rd, rn, TCG_REG_R8);
697 tcg_out_ld8s_8(s, cond, rd, rn, offset);
700 static inline void tcg_out_st8u(TCGContext *s, int cond,
701 int rd, int rn, int32_t offset)
703 if (offset > 0xfff || offset < -0xfff) {
704 tcg_out_movi32(s, cond, TCG_REG_R8, offset);
705 tcg_out_st8_r(s, cond, rd, rn, TCG_REG_R8);
707 tcg_out_st8_12(s, cond, rd, rn, offset);
710 static inline void tcg_out_goto(TCGContext *s, int cond, uint32_t addr)
714 val = addr - (tcg_target_long) s->code_ptr;
715 if (val - 8 < 0x01fffffd && val - 8 > -0x01fffffd)
716 tcg_out_b(s, cond, val);
721 if (cond == COND_AL) {
722 tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
723 tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
725 tcg_out_movi32(s, cond, TCG_REG_R8, val - 8);
726 tcg_out_dat_reg(s, cond, ARITH_ADD,
727 15, 15, TCG_REG_R8, SHIFT_IMM_LSL(0));
733 static inline void tcg_out_call(TCGContext *s, int cond, uint32_t addr)
738 tcg_out_dat_reg(s, cond, ARITH_MOV, TCG_REG_R8, 0, 14, SHIFT_IMM_LSL(0));
741 val = addr - (tcg_target_long) s->code_ptr;
742 if (val < 0x01fffffd && val > -0x01fffffd)
743 tcg_out_bl(s, cond, val);
748 if (cond == COND_AL) {
749 tcg_out_dat_imm(s, cond, ARITH_ADD, 14, 15, 4);
750 tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
751 tcg_out32(s, addr); /* XXX: This is l->u.value, can we use it? */
753 tcg_out_movi32(s, cond, TCG_REG_R9, addr);
754 tcg_out_dat_imm(s, cond, ARITH_MOV, 14, 0, 15);
755 tcg_out_bx(s, cond, TCG_REG_R9);
761 tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, TCG_REG_R8, SHIFT_IMM_LSL(0));
765 static inline void tcg_out_callr(TCGContext *s, int cond, int arg)
768 tcg_out_dat_reg(s, cond, ARITH_MOV, TCG_REG_R8, 0, 14, SHIFT_IMM_LSL(0));
770 /* TODO: on ARMv5 and ARMv6 replace with tcg_out_blx(s, cond, arg); */
771 tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, 15, SHIFT_IMM_LSL(0));
772 tcg_out_bx(s, cond, arg);
774 tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, TCG_REG_R8, SHIFT_IMM_LSL(0));
778 static inline void tcg_out_goto_label(TCGContext *s, int cond, int label_index)
780 TCGLabel *l = &s->labels[label_index];
783 tcg_out_goto(s, cond, l->u.value);
784 else if (cond == COND_AL) {
785 tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
786 tcg_out_reloc(s, s->code_ptr, R_ARM_ABS32, label_index, 31337);
789 /* Probably this should be preferred even for COND_AL... */
790 tcg_out_reloc(s, s->code_ptr, R_ARM_PC24, label_index, 31337);
791 tcg_out_b_noaddr(s, cond);
795 static void tcg_out_div_helper(TCGContext *s, int cond, const TCGArg *args,
796 void *helper_div, void *helper_rem, int shift)
798 int div_reg = args[0];
799 int rem_reg = args[1];
801 /* stmdb sp!, { r0 - r3, ip, lr } */
802 /* (Note that we need an even number of registers as per EABI) */
803 tcg_out32(s, (cond << 28) | 0x092d500f);
805 tcg_out_dat_reg(s, cond, ARITH_MOV, 0, 0, args[2], SHIFT_IMM_LSL(0));
806 tcg_out_dat_reg(s, cond, ARITH_MOV, 1, 0, args[3], SHIFT_IMM_LSL(0));
807 tcg_out_dat_reg(s, cond, ARITH_MOV, 2, 0, args[4], SHIFT_IMM_LSL(0));
808 tcg_out_dat_reg(s, cond, ARITH_MOV, 3, 0, 2, shift);
810 tcg_out_call(s, cond, (uint32_t) helper_div);
811 tcg_out_dat_reg(s, cond, ARITH_MOV, 8, 0, 0, SHIFT_IMM_LSL(0));
813 /* ldmia sp, { r0 - r3, fp, lr } */
814 tcg_out32(s, (cond << 28) | 0x089d500f);
816 tcg_out_dat_reg(s, cond, ARITH_MOV, 0, 0, args[2], SHIFT_IMM_LSL(0));
817 tcg_out_dat_reg(s, cond, ARITH_MOV, 1, 0, args[3], SHIFT_IMM_LSL(0));
818 tcg_out_dat_reg(s, cond, ARITH_MOV, 2, 0, args[4], SHIFT_IMM_LSL(0));
819 tcg_out_dat_reg(s, cond, ARITH_MOV, 3, 0, 2, shift);
821 tcg_out_call(s, cond, (uint32_t) helper_rem);
823 tcg_out_dat_reg(s, cond, ARITH_MOV, rem_reg, 0, 0, SHIFT_IMM_LSL(0));
824 tcg_out_dat_reg(s, cond, ARITH_MOV, div_reg, 0, 8, SHIFT_IMM_LSL(0));
826 /* ldr r0, [sp], #4 */
827 if (rem_reg != 0 && div_reg != 0)
828 tcg_out32(s, (cond << 28) | 0x04bd0004);
829 /* ldr r1, [sp], #4 */
830 if (rem_reg != 1 && div_reg != 1)
831 tcg_out32(s, (cond << 28) | 0x04bd1004);
832 /* ldr r2, [sp], #4 */
833 if (rem_reg != 2 && div_reg != 2)
834 tcg_out32(s, (cond << 28) | 0x04bd2004);
835 /* ldr r3, [sp], #4 */
836 if (rem_reg != 3 && div_reg != 3)
837 tcg_out32(s, (cond << 28) | 0x04bd3004);
838 /* ldr ip, [sp], #4 */
839 if (rem_reg != 12 && div_reg != 12)
840 tcg_out32(s, (cond << 28) | 0x04bdc004);
841 /* ldr lr, [sp], #4 */
842 if (rem_reg != 14 && div_reg != 14)
843 tcg_out32(s, (cond << 28) | 0x04bde004);
846 #ifdef CONFIG_SOFTMMU
848 #include "../../softmmu_defs.h"
850 static void *qemu_ld_helpers[4] = {
857 static void *qemu_st_helpers[4] = {
865 #define TLB_SHIFT (CPU_TLB_ENTRY_BITS + CPU_TLB_BITS)
867 static inline void tcg_out_qemu_ld(TCGContext *s, int cond,
868 const TCGArg *args, int opc)
870 int addr_reg, data_reg, data_reg2;
871 #ifdef CONFIG_SOFTMMU
872 int mem_index, s_bits;
873 # if TARGET_LONG_BITS == 64
883 data_reg2 = 0; /* surpress warning */
885 #ifdef CONFIG_SOFTMMU
886 # if TARGET_LONG_BITS == 64
892 /* Should generate something like the following:
893 * shr r8, addr_reg, #TARGET_PAGE_BITS
894 * and r0, r8, #(CPU_TLB_SIZE - 1) @ Assumption: CPU_TLB_BITS <= 8
895 * add r0, env, r0 lsl #CPU_TLB_ENTRY_BITS
897 # if CPU_TLB_BITS > 8
900 tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
901 8, 0, addr_reg, SHIFT_IMM_LSR(TARGET_PAGE_BITS));
902 tcg_out_dat_imm(s, COND_AL, ARITH_AND,
903 0, 8, CPU_TLB_SIZE - 1);
904 tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
905 0, TCG_AREG0, 0, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS));
907 * ldr r1 [r0, #(offsetof(CPUState, tlb_table[mem_index][0].addr_read))]
908 * below, the offset is likely to exceed 12 bits if mem_index != 0 and
909 * not exceed otherwise, so use an
910 * add r0, r0, #(mem_index * sizeof *CPUState.tlb_table)
914 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 0, 0,
915 (mem_index << (TLB_SHIFT & 1)) |
916 ((16 - (TLB_SHIFT >> 1)) << 8));
917 tcg_out_ld32_12(s, COND_AL, 1, 0,
918 offsetof(CPUState, tlb_table[0][0].addr_read));
919 tcg_out_dat_reg(s, COND_AL, ARITH_CMP,
920 0, 1, 8, SHIFT_IMM_LSL(TARGET_PAGE_BITS));
921 /* Check alignment. */
923 tcg_out_dat_imm(s, COND_EQ, ARITH_TST,
924 0, addr_reg, (1 << s_bits) - 1);
925 # if TARGET_LONG_BITS == 64
926 /* XXX: possibly we could use a block data load or writeback in
927 * the first access. */
928 tcg_out_ld32_12(s, COND_EQ, 1, 0,
929 offsetof(CPUState, tlb_table[0][0].addr_read) + 4);
930 tcg_out_dat_reg(s, COND_EQ, ARITH_CMP,
931 0, 1, addr_reg2, SHIFT_IMM_LSL(0));
933 tcg_out_ld32_12(s, COND_EQ, 1, 0,
934 offsetof(CPUState, tlb_table[0][0].addend));
938 tcg_out_ld8_r(s, COND_EQ, data_reg, addr_reg, 1);
941 tcg_out_ld8s_r(s, COND_EQ, data_reg, addr_reg, 1);
944 tcg_out_ld16u_r(s, COND_EQ, data_reg, addr_reg, 1);
947 tcg_out_ld16s_r(s, COND_EQ, data_reg, addr_reg, 1);
951 tcg_out_ld32_r(s, COND_EQ, data_reg, addr_reg, 1);
954 tcg_out_ld32_rwb(s, COND_EQ, data_reg, 1, addr_reg);
955 tcg_out_ld32_12(s, COND_EQ, data_reg2, 1, 4);
959 label_ptr = (void *) s->code_ptr;
960 tcg_out_b(s, COND_EQ, 8);
963 tcg_out_dat_reg(s, cond, ARITH_MOV, 8, 0, 14, SHIFT_IMM_LSL(0));
966 /* TODO: move this code to where the constants pool will be */
968 tcg_out_dat_reg(s, cond, ARITH_MOV,
969 0, 0, addr_reg, SHIFT_IMM_LSL(0));
970 # if TARGET_LONG_BITS == 32
971 tcg_out_dat_imm(s, cond, ARITH_MOV, 1, 0, mem_index);
974 tcg_out_dat_reg(s, cond, ARITH_MOV,
975 1, 0, addr_reg2, SHIFT_IMM_LSL(0));
976 tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
978 tcg_out_bl(s, cond, (tcg_target_long) qemu_ld_helpers[s_bits] -
979 (tcg_target_long) s->code_ptr);
983 tcg_out_dat_reg(s, cond, ARITH_MOV,
984 0, 0, 0, SHIFT_IMM_LSL(24));
985 tcg_out_dat_reg(s, cond, ARITH_MOV,
986 data_reg, 0, 0, SHIFT_IMM_ASR(24));
989 tcg_out_dat_reg(s, cond, ARITH_MOV,
990 0, 0, 0, SHIFT_IMM_LSL(16));
991 tcg_out_dat_reg(s, cond, ARITH_MOV,
992 data_reg, 0, 0, SHIFT_IMM_ASR(16));
999 tcg_out_dat_reg(s, cond, ARITH_MOV,
1000 data_reg, 0, 0, SHIFT_IMM_LSL(0));
1004 tcg_out_dat_reg(s, cond, ARITH_MOV,
1005 data_reg, 0, 0, SHIFT_IMM_LSL(0));
1007 tcg_out_dat_reg(s, cond, ARITH_MOV,
1008 data_reg2, 0, 1, SHIFT_IMM_LSL(0));
1013 tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, 8, SHIFT_IMM_LSL(0));
1016 *label_ptr += ((void *) s->code_ptr - (void *) label_ptr - 8) >> 2;
1017 #else /* !CONFIG_SOFTMMU */
1019 uint32_t offset = GUEST_BASE;
1024 i = ctz32(offset) & ~1;
1025 rot = ((32 - i) << 7) & 0xf00;
1027 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 8, addr_reg,
1028 ((offset >> i) & 0xff) | rot);
1030 offset &= ~(0xff << i);
1035 tcg_out_ld8_12(s, COND_AL, data_reg, addr_reg, 0);
1038 tcg_out_ld8s_8(s, COND_AL, data_reg, addr_reg, 0);
1041 tcg_out_ld16u_8(s, COND_AL, data_reg, addr_reg, 0);
1044 tcg_out_ld16s_8(s, COND_AL, data_reg, addr_reg, 0);
1048 tcg_out_ld32_12(s, COND_AL, data_reg, addr_reg, 0);
1051 /* TODO: use block load -
1052 * check that data_reg2 > data_reg or the other way */
1053 if (data_reg == addr_reg) {
1054 tcg_out_ld32_12(s, COND_AL, data_reg2, addr_reg, 4);
1055 tcg_out_ld32_12(s, COND_AL, data_reg, addr_reg, 0);
1057 tcg_out_ld32_12(s, COND_AL, data_reg, addr_reg, 0);
1058 tcg_out_ld32_12(s, COND_AL, data_reg2, addr_reg, 4);
1065 static inline void tcg_out_qemu_st(TCGContext *s, int cond,
1066 const TCGArg *args, int opc)
1068 int addr_reg, data_reg, data_reg2;
1069 #ifdef CONFIG_SOFTMMU
1070 int mem_index, s_bits;
1071 # if TARGET_LONG_BITS == 64
1074 uint32_t *label_ptr;
1079 data_reg2 = *args++;
1081 data_reg2 = 0; /* surpress warning */
1083 #ifdef CONFIG_SOFTMMU
1084 # if TARGET_LONG_BITS == 64
1085 addr_reg2 = *args++;
1090 /* Should generate something like the following:
1091 * shr r8, addr_reg, #TARGET_PAGE_BITS
1092 * and r0, r8, #(CPU_TLB_SIZE - 1) @ Assumption: CPU_TLB_BITS <= 8
1093 * add r0, env, r0 lsl #CPU_TLB_ENTRY_BITS
1095 tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
1096 8, 0, addr_reg, SHIFT_IMM_LSR(TARGET_PAGE_BITS));
1097 tcg_out_dat_imm(s, COND_AL, ARITH_AND,
1098 0, 8, CPU_TLB_SIZE - 1);
1099 tcg_out_dat_reg(s, COND_AL, ARITH_ADD,
1100 0, TCG_AREG0, 0, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS));
1102 * ldr r1 [r0, #(offsetof(CPUState, tlb_table[mem_index][0].addr_write))]
1103 * below, the offset is likely to exceed 12 bits if mem_index != 0 and
1104 * not exceed otherwise, so use an
1105 * add r0, r0, #(mem_index * sizeof *CPUState.tlb_table)
1109 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 0, 0,
1110 (mem_index << (TLB_SHIFT & 1)) |
1111 ((16 - (TLB_SHIFT >> 1)) << 8));
1112 tcg_out_ld32_12(s, COND_AL, 1, 0,
1113 offsetof(CPUState, tlb_table[0][0].addr_write));
1114 tcg_out_dat_reg(s, COND_AL, ARITH_CMP,
1115 0, 1, 8, SHIFT_IMM_LSL(TARGET_PAGE_BITS));
1116 /* Check alignment. */
1118 tcg_out_dat_imm(s, COND_EQ, ARITH_TST,
1119 0, addr_reg, (1 << s_bits) - 1);
1120 # if TARGET_LONG_BITS == 64
1121 /* XXX: possibly we could use a block data load or writeback in
1122 * the first access. */
1123 tcg_out_ld32_12(s, COND_EQ, 1, 0,
1124 offsetof(CPUState, tlb_table[0][0].addr_write)
1126 tcg_out_dat_reg(s, COND_EQ, ARITH_CMP,
1127 0, 1, addr_reg2, SHIFT_IMM_LSL(0));
1129 tcg_out_ld32_12(s, COND_EQ, 1, 0,
1130 offsetof(CPUState, tlb_table[0][0].addend));
1134 tcg_out_st8_r(s, COND_EQ, data_reg, addr_reg, 1);
1137 tcg_out_st8s_r(s, COND_EQ, data_reg, addr_reg, 1);
1140 tcg_out_st16u_r(s, COND_EQ, data_reg, addr_reg, 1);
1143 tcg_out_st16s_r(s, COND_EQ, data_reg, addr_reg, 1);
1147 tcg_out_st32_r(s, COND_EQ, data_reg, addr_reg, 1);
1150 tcg_out_st32_rwb(s, COND_EQ, data_reg, 1, addr_reg);
1151 tcg_out_st32_12(s, COND_EQ, data_reg2, 1, 4);
1155 label_ptr = (void *) s->code_ptr;
1156 tcg_out_b(s, COND_EQ, 8);
1158 /* TODO: move this code to where the constants pool will be */
1160 tcg_out_dat_reg(s, cond, ARITH_MOV,
1161 0, 0, addr_reg, SHIFT_IMM_LSL(0));
1162 # if TARGET_LONG_BITS == 32
1165 tcg_out_dat_imm(s, cond, ARITH_AND, 1, data_reg, 0xff);
1166 tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
1169 tcg_out_dat_reg(s, cond, ARITH_MOV,
1170 1, 0, data_reg, SHIFT_IMM_LSL(16));
1171 tcg_out_dat_reg(s, cond, ARITH_MOV,
1172 1, 0, 1, SHIFT_IMM_LSR(16));
1173 tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
1177 tcg_out_dat_reg(s, cond, ARITH_MOV,
1178 1, 0, data_reg, SHIFT_IMM_LSL(0));
1179 tcg_out_dat_imm(s, cond, ARITH_MOV, 2, 0, mem_index);
1183 tcg_out_dat_reg(s, cond, ARITH_MOV,
1184 1, 0, data_reg, SHIFT_IMM_LSL(0));
1186 tcg_out_dat_reg(s, cond, ARITH_MOV,
1187 2, 0, data_reg2, SHIFT_IMM_LSL(0));
1188 tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
1193 tcg_out_dat_reg(s, cond, ARITH_MOV,
1194 1, 0, addr_reg2, SHIFT_IMM_LSL(0));
1197 tcg_out_dat_imm(s, cond, ARITH_AND, 2, data_reg, 0xff);
1198 tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
1201 tcg_out_dat_reg(s, cond, ARITH_MOV,
1202 2, 0, data_reg, SHIFT_IMM_LSL(16));
1203 tcg_out_dat_reg(s, cond, ARITH_MOV,
1204 2, 0, 2, SHIFT_IMM_LSR(16));
1205 tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
1209 tcg_out_dat_reg(s, cond, ARITH_MOV,
1210 2, 0, data_reg, SHIFT_IMM_LSL(0));
1211 tcg_out_dat_imm(s, cond, ARITH_MOV, 3, 0, mem_index);
1214 tcg_out_dat_imm(s, cond, ARITH_MOV, 8, 0, mem_index);
1215 tcg_out32(s, (cond << 28) | 0x052d8010); /* str r8, [sp, #-0x10]! */
1217 tcg_out_dat_reg(s, cond, ARITH_MOV,
1218 2, 0, data_reg, SHIFT_IMM_LSL(0));
1220 tcg_out_dat_reg(s, cond, ARITH_MOV,
1221 3, 0, data_reg2, SHIFT_IMM_LSL(0));
1227 tcg_out_dat_reg(s, cond, ARITH_MOV, 8, 0, 14, SHIFT_IMM_LSL(0));
1230 tcg_out_bl(s, cond, (tcg_target_long) qemu_st_helpers[s_bits] -
1231 (tcg_target_long) s->code_ptr);
1232 # if TARGET_LONG_BITS == 64
1234 tcg_out_dat_imm(s, cond, ARITH_ADD, 13, 13, 0x10);
1238 tcg_out_dat_reg(s, cond, ARITH_MOV, 14, 0, 8, SHIFT_IMM_LSL(0));
1241 *label_ptr += ((void *) s->code_ptr - (void *) label_ptr - 8) >> 2;
1242 #else /* !CONFIG_SOFTMMU */
1244 uint32_t offset = GUEST_BASE;
1249 i = ctz32(offset) & ~1;
1250 rot = ((32 - i) << 7) & 0xf00;
1252 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, 8, addr_reg,
1253 ((offset >> i) & 0xff) | rot);
1255 offset &= ~(0xff << i);
1260 tcg_out_st8_12(s, COND_AL, data_reg, addr_reg, 0);
1263 tcg_out_st8s_8(s, COND_AL, data_reg, addr_reg, 0);
1266 tcg_out_st16u_8(s, COND_AL, data_reg, addr_reg, 0);
1269 tcg_out_st16s_8(s, COND_AL, data_reg, addr_reg, 0);
1273 tcg_out_st32_12(s, COND_AL, data_reg, addr_reg, 0);
1276 /* TODO: use block store -
1277 * check that data_reg2 > data_reg or the other way */
1278 tcg_out_st32_12(s, COND_AL, data_reg, addr_reg, 0);
1279 tcg_out_st32_12(s, COND_AL, data_reg2, addr_reg, 4);
1285 static uint8_t *tb_ret_addr;
1287 static inline void tcg_out_op(TCGContext *s, int opc,
1288 const TCGArg *args, const int *const_args)
1293 case INDEX_op_exit_tb:
1296 tcg_out_ld32_12(s, COND_AL, TCG_REG_R0, 15, 0);
1298 tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R0, 0, args[0]);
1299 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, 15, 0, 14, SHIFT_IMM_LSL(0));
1301 tcg_out32(s, args[0]);
1304 uint8_t *ld_ptr = s->code_ptr;
1306 tcg_out_ld32_12(s, COND_AL, 0, 15, 0);
1308 tcg_out_dat_imm(s, COND_AL, ARITH_MOV, 0, 0, args[0]);
1309 tcg_out_goto(s, COND_AL, (tcg_target_ulong) tb_ret_addr);
1311 *ld_ptr = (uint8_t) (s->code_ptr - ld_ptr) - 8;
1312 tcg_out32(s, args[0]);
1317 case INDEX_op_goto_tb:
1318 if (s->tb_jmp_offset) {
1319 /* Direct jump method */
1320 #if defined(USE_DIRECT_JUMP)
1321 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1322 tcg_out_b(s, COND_AL, 8);
1324 tcg_out_ld32_12(s, COND_AL, 15, 15, -4);
1325 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1329 /* Indirect jump method */
1331 c = (int) (s->tb_next + args[0]) - ((int) s->code_ptr + 8);
1332 if (c > 0xfff || c < -0xfff) {
1333 tcg_out_movi32(s, COND_AL, TCG_REG_R0,
1334 (tcg_target_long) (s->tb_next + args[0]));
1335 tcg_out_ld32_12(s, COND_AL, 15, TCG_REG_R0, 0);
1337 tcg_out_ld32_12(s, COND_AL, 15, 15, c);
1339 tcg_out_ld32_12(s, COND_AL, TCG_REG_R0, 15, 0);
1340 tcg_out_ld32_12(s, COND_AL, 15, TCG_REG_R0, 0);
1341 tcg_out32(s, (tcg_target_long) (s->tb_next + args[0]));
1344 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1348 tcg_out_call(s, COND_AL, args[0]);
1350 tcg_out_callr(s, COND_AL, args[0]);
1354 tcg_out_goto(s, COND_AL, args[0]);
1356 tcg_out_bx(s, COND_AL, args[0]);
1359 tcg_out_goto_label(s, COND_AL, args[0]);
1362 case INDEX_op_ld8u_i32:
1363 tcg_out_ld8u(s, COND_AL, args[0], args[1], args[2]);
1365 case INDEX_op_ld8s_i32:
1366 tcg_out_ld8s(s, COND_AL, args[0], args[1], args[2]);
1368 case INDEX_op_ld16u_i32:
1369 tcg_out_ld16u(s, COND_AL, args[0], args[1], args[2]);
1371 case INDEX_op_ld16s_i32:
1372 tcg_out_ld16s(s, COND_AL, args[0], args[1], args[2]);
1374 case INDEX_op_ld_i32:
1375 tcg_out_ld32u(s, COND_AL, args[0], args[1], args[2]);
1377 case INDEX_op_st8_i32:
1378 tcg_out_st8u(s, COND_AL, args[0], args[1], args[2]);
1380 case INDEX_op_st16_i32:
1381 tcg_out_st16u(s, COND_AL, args[0], args[1], args[2]);
1383 case INDEX_op_st_i32:
1384 tcg_out_st32(s, COND_AL, args[0], args[1], args[2]);
1387 case INDEX_op_mov_i32:
1388 tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
1389 args[0], 0, args[1], SHIFT_IMM_LSL(0));
1391 case INDEX_op_movi_i32:
1392 tcg_out_movi32(s, COND_AL, args[0], args[1]);
1394 case INDEX_op_add_i32:
1397 case INDEX_op_sub_i32:
1400 case INDEX_op_and_i32:
1403 case INDEX_op_or_i32:
1406 case INDEX_op_xor_i32:
1411 tcg_out_dat_imm(s, COND_AL, c,
1412 args[0], args[1], args[2]);
1414 tcg_out_dat_reg(s, COND_AL, c,
1415 args[0], args[1], args[2], SHIFT_IMM_LSL(0));
1417 case INDEX_op_add2_i32:
1418 tcg_out_dat_reg2(s, COND_AL, ARITH_ADD, ARITH_ADC,
1419 args[0], args[1], args[2], args[3],
1420 args[4], args[5], SHIFT_IMM_LSL(0));
1422 case INDEX_op_sub2_i32:
1423 tcg_out_dat_reg2(s, COND_AL, ARITH_SUB, ARITH_SBC,
1424 args[0], args[1], args[2], args[3],
1425 args[4], args[5], SHIFT_IMM_LSL(0));
1427 case INDEX_op_neg_i32:
1428 tcg_out_dat_imm(s, COND_AL, ARITH_RSB, args[0], args[1], 0);
1430 case INDEX_op_not_i32:
1431 tcg_out_dat_reg(s, COND_AL,
1432 ARITH_MVN, args[0], 0, args[1], SHIFT_IMM_LSL(0));
1434 case INDEX_op_mul_i32:
1435 tcg_out_mul32(s, COND_AL, args[0], args[1], args[2]);
1437 case INDEX_op_mulu2_i32:
1438 tcg_out_umull32(s, COND_AL, args[0], args[1], args[2], args[3]);
1440 case INDEX_op_div2_i32:
1441 tcg_out_div_helper(s, COND_AL, args,
1442 tcg_helper_div_i64, tcg_helper_rem_i64,
1445 case INDEX_op_divu2_i32:
1446 tcg_out_div_helper(s, COND_AL, args,
1447 tcg_helper_divu_i64, tcg_helper_remu_i64,
1450 /* XXX: Perhaps args[2] & 0x1f is wrong */
1451 case INDEX_op_shl_i32:
1453 SHIFT_IMM_LSL(args[2] & 0x1f) : SHIFT_REG_LSL(args[2]);
1455 case INDEX_op_shr_i32:
1456 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_LSR(args[2] & 0x1f) :
1457 SHIFT_IMM_LSL(0) : SHIFT_REG_LSR(args[2]);
1459 case INDEX_op_sar_i32:
1460 c = const_args[2] ? (args[2] & 0x1f) ? SHIFT_IMM_ASR(args[2] & 0x1f) :
1461 SHIFT_IMM_LSL(0) : SHIFT_REG_ASR(args[2]);
1464 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, args[0], 0, args[1], c);
1467 case INDEX_op_brcond_i32:
1468 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0,
1469 args[0], args[1], SHIFT_IMM_LSL(0));
1470 tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[2]], args[3]);
1472 case INDEX_op_brcond2_i32:
1473 /* The resulting conditions are:
1474 * TCG_COND_EQ --> a0 == a2 && a1 == a3,
1475 * TCG_COND_NE --> (a0 != a2 && a1 == a3) || a1 != a3,
1476 * TCG_COND_LT(U) --> (a0 < a2 && a1 == a3) || a1 < a3,
1477 * TCG_COND_GE(U) --> (a0 >= a2 && a1 == a3) || (a1 >= a3 && a1 != a3),
1478 * TCG_COND_LE(U) --> (a0 <= a2 && a1 == a3) || (a1 <= a3 && a1 != a3),
1479 * TCG_COND_GT(U) --> (a0 > a2 && a1 == a3) || a1 > a3,
1481 tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0,
1482 args[1], args[3], SHIFT_IMM_LSL(0));
1483 tcg_out_dat_reg(s, COND_EQ, ARITH_CMP, 0,
1484 args[0], args[2], SHIFT_IMM_LSL(0));
1485 tcg_out_goto_label(s, tcg_cond_to_arm_cond[args[4]], args[5]);
1488 case INDEX_op_qemu_ld8u:
1489 tcg_out_qemu_ld(s, COND_AL, args, 0);
1491 case INDEX_op_qemu_ld8s:
1492 tcg_out_qemu_ld(s, COND_AL, args, 0 | 4);
1494 case INDEX_op_qemu_ld16u:
1495 tcg_out_qemu_ld(s, COND_AL, args, 1);
1497 case INDEX_op_qemu_ld16s:
1498 tcg_out_qemu_ld(s, COND_AL, args, 1 | 4);
1500 case INDEX_op_qemu_ld32u:
1501 tcg_out_qemu_ld(s, COND_AL, args, 2);
1503 case INDEX_op_qemu_ld64:
1504 tcg_out_qemu_ld(s, COND_AL, args, 3);
1507 case INDEX_op_qemu_st8:
1508 tcg_out_qemu_st(s, COND_AL, args, 0);
1510 case INDEX_op_qemu_st16:
1511 tcg_out_qemu_st(s, COND_AL, args, 1);
1513 case INDEX_op_qemu_st32:
1514 tcg_out_qemu_st(s, COND_AL, args, 2);
1516 case INDEX_op_qemu_st64:
1517 tcg_out_qemu_st(s, COND_AL, args, 3);
1520 case INDEX_op_ext8s_i32:
1521 tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
1522 args[0], 0, args[1], SHIFT_IMM_LSL(24));
1523 tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
1524 args[0], 0, args[0], SHIFT_IMM_ASR(24));
1526 case INDEX_op_ext16s_i32:
1527 tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
1528 args[0], 0, args[1], SHIFT_IMM_LSL(16));
1529 tcg_out_dat_reg(s, COND_AL, ARITH_MOV,
1530 args[0], 0, args[0], SHIFT_IMM_ASR(16));
1538 static const TCGTargetOpDef arm_op_defs[] = {
1539 { INDEX_op_exit_tb, { } },
1540 { INDEX_op_goto_tb, { } },
1541 { INDEX_op_call, { "ri" } },
1542 { INDEX_op_jmp, { "ri" } },
1543 { INDEX_op_br, { } },
1545 { INDEX_op_mov_i32, { "r", "r" } },
1546 { INDEX_op_movi_i32, { "r" } },
1548 { INDEX_op_ld8u_i32, { "r", "r" } },
1549 { INDEX_op_ld8s_i32, { "r", "r" } },
1550 { INDEX_op_ld16u_i32, { "r", "r" } },
1551 { INDEX_op_ld16s_i32, { "r", "r" } },
1552 { INDEX_op_ld_i32, { "r", "r" } },
1553 { INDEX_op_st8_i32, { "r", "r" } },
1554 { INDEX_op_st16_i32, { "r", "r" } },
1555 { INDEX_op_st_i32, { "r", "r" } },
1557 /* TODO: "r", "r", "ri" */
1558 { INDEX_op_add_i32, { "r", "r", "rI" } },
1559 { INDEX_op_sub_i32, { "r", "r", "rI" } },
1560 { INDEX_op_mul_i32, { "r", "r", "r" } },
1561 { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } },
1562 { INDEX_op_div2_i32, { "r", "r", "r", "1", "2" } },
1563 { INDEX_op_divu2_i32, { "r", "r", "r", "1", "2" } },
1564 { INDEX_op_and_i32, { "r", "r", "rI" } },
1565 { INDEX_op_or_i32, { "r", "r", "rI" } },
1566 { INDEX_op_xor_i32, { "r", "r", "rI" } },
1567 { INDEX_op_neg_i32, { "r", "r" } },
1568 { INDEX_op_not_i32, { "r", "r" } },
1570 { INDEX_op_shl_i32, { "r", "r", "ri" } },
1571 { INDEX_op_shr_i32, { "r", "r", "ri" } },
1572 { INDEX_op_sar_i32, { "r", "r", "ri" } },
1574 { INDEX_op_brcond_i32, { "r", "r" } },
1576 /* TODO: "r", "r", "r", "r", "ri", "ri" */
1577 { INDEX_op_add2_i32, { "r", "r", "r", "r", "r", "r" } },
1578 { INDEX_op_sub2_i32, { "r", "r", "r", "r", "r", "r" } },
1579 { INDEX_op_brcond2_i32, { "r", "r", "r", "r" } },
1581 { INDEX_op_qemu_ld8u, { "r", "x", "X" } },
1582 { INDEX_op_qemu_ld8s, { "r", "x", "X" } },
1583 { INDEX_op_qemu_ld16u, { "r", "x", "X" } },
1584 { INDEX_op_qemu_ld16s, { "r", "x", "X" } },
1585 { INDEX_op_qemu_ld32u, { "r", "x", "X" } },
1586 { INDEX_op_qemu_ld64, { "d", "r", "x", "X" } },
1588 { INDEX_op_qemu_st8, { "x", "x", "X" } },
1589 { INDEX_op_qemu_st16, { "x", "x", "X" } },
1590 { INDEX_op_qemu_st32, { "x", "x", "X" } },
1591 { INDEX_op_qemu_st64, { "x", "D", "x", "X" } },
1593 { INDEX_op_ext8s_i32, { "r", "r" } },
1594 { INDEX_op_ext16s_i32, { "r", "r" } },
1599 void tcg_target_init(TCGContext *s)
1602 if ((1 << CPU_TLB_ENTRY_BITS) != sizeof(CPUTLBEntry))
1605 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0,
1606 ((2 << TCG_REG_R14) - 1) & ~(1 << TCG_REG_R8));
1607 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1608 ((2 << TCG_REG_R3) - 1) |
1609 (1 << TCG_REG_R12) | (1 << TCG_REG_R14));
1611 tcg_regset_clear(s->reserved_regs);
1613 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R14);
1615 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
1616 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R8);
1618 tcg_add_target_add_op_defs(arm_op_defs);
1621 static inline void tcg_out_ld(TCGContext *s, TCGType type, int arg,
1622 int arg1, tcg_target_long arg2)
1624 tcg_out_ld32u(s, COND_AL, arg, arg1, arg2);
1627 static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
1628 int arg1, tcg_target_long arg2)
1630 tcg_out_st32(s, COND_AL, arg, arg1, arg2);
1633 static void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
1637 tcg_out_dat_imm(s, COND_AL, ARITH_ADD, reg, reg, val);
1642 tcg_out_dat_imm(s, COND_AL, ARITH_SUB, reg, reg, -val);
1648 static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
1650 tcg_out_dat_reg(s, COND_AL, ARITH_MOV, ret, 0, arg, SHIFT_IMM_LSL(0));
1653 static inline void tcg_out_movi(TCGContext *s, TCGType type,
1654 int ret, tcg_target_long arg)
1656 tcg_out_movi32(s, COND_AL, ret, arg);
1659 void tcg_target_qemu_prologue(TCGContext *s)
1661 /* stmdb sp!, { r9 - r11, lr } */
1662 tcg_out32(s, (COND_AL << 28) | 0x092d4e00);
1664 tcg_out_bx(s, COND_AL, TCG_REG_R0);
1665 tb_ret_addr = s->code_ptr;
1667 /* ldmia sp!, { r9 - r11, pc } */
1668 tcg_out32(s, (COND_AL << 28) | 0x08bd8e00);