4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /* internal defines */
31 typedef struct DisasContext {
34 struct TranslationBlock *tb;
37 #define DISAS_JUMP_NEXT 4
39 /* XXX: move that elsewhere */
40 static uint16_t *gen_opc_ptr;
41 static uint32_t *gen_opparam_ptr;
46 #define DEF(s, n, copy_size) INDEX_op_ ## s,
54 static GenOpFunc2 *gen_test_cc[14] = {
71 const uint8_t table_logic_cc[16] = {
90 static GenOpFunc1 *gen_shift_T1_im[4] = {
97 static GenOpFunc1 *gen_shift_T2_im[4] = {
104 static GenOpFunc1 *gen_shift_T1_im_cc[4] = {
105 gen_op_shll_T1_im_cc,
106 gen_op_shrl_T1_im_cc,
107 gen_op_sarl_T1_im_cc,
108 gen_op_rorl_T1_im_cc,
111 static GenOpFunc *gen_shift_T1_T0[4] = {
118 static GenOpFunc *gen_shift_T1_T0_cc[4] = {
119 gen_op_shll_T1_T0_cc,
120 gen_op_shrl_T1_T0_cc,
121 gen_op_sarl_T1_T0_cc,
122 gen_op_rorl_T1_T0_cc,
125 static GenOpFunc *gen_op_movl_TN_reg[3][16] = {
182 static GenOpFunc *gen_op_movl_reg_TN[2][16] = {
221 static GenOpFunc1 *gen_op_movl_TN_im[3] = {
227 static inline void gen_movl_TN_reg(DisasContext *s, int reg, int t)
232 /* normaly, since we updated PC, we need only to add 4 */
233 val = (long)s->pc + 4;
234 gen_op_movl_TN_im[t](val);
236 gen_op_movl_TN_reg[t][reg]();
240 static inline void gen_movl_T0_reg(DisasContext *s, int reg)
242 gen_movl_TN_reg(s, reg, 0);
245 static inline void gen_movl_T1_reg(DisasContext *s, int reg)
247 gen_movl_TN_reg(s, reg, 1);
250 static inline void gen_movl_T2_reg(DisasContext *s, int reg)
252 gen_movl_TN_reg(s, reg, 2);
255 static inline void gen_movl_reg_TN(DisasContext *s, int reg, int t)
257 gen_op_movl_reg_TN[t][reg]();
259 s->is_jmp = DISAS_JUMP;
263 static inline void gen_movl_reg_T0(DisasContext *s, int reg)
265 gen_movl_reg_TN(s, reg, 0);
268 static inline void gen_movl_reg_T1(DisasContext *s, int reg)
270 gen_movl_reg_TN(s, reg, 1);
273 static inline void gen_add_data_offset(DisasContext *s, unsigned int insn)
277 if (!(insn & (1 << 25))) {
280 if (!(insn & (1 << 23)))
282 gen_op_addl_T1_im(val);
286 shift = (insn >> 7) & 0x1f;
287 gen_movl_T2_reg(s, rm);
289 gen_shift_T2_im[(insn >> 5) & 3](shift);
291 if (!(insn & (1 << 23)))
298 static inline void gen_add_datah_offset(DisasContext *s, unsigned int insn)
302 if (insn & (1 << 22)) {
304 val = (insn & 0xf) | ((insn >> 4) & 0xf0);
305 if (!(insn & (1 << 23)))
307 gen_op_addl_T1_im(val);
311 gen_movl_T2_reg(s, rm);
312 if (!(insn & (1 << 23)))
319 static void disas_arm_insn(DisasContext *s)
321 unsigned int cond, insn, val, op1, i, shift, rm, rs, rn, rd, sh;
330 /* if not always execute, we generate a conditional jump to
332 gen_test_cc[cond ^ 1]((long)s->tb, (long)s->pc);
333 s->is_jmp = DISAS_JUMP_NEXT;
335 if (((insn & 0x0e000000) == 0 &&
336 (insn & 0x00000090) != 0x90) ||
337 ((insn & 0x0e000000) == (1 << 25))) {
338 int set_cc, logic_cc, shiftop;
340 op1 = (insn >> 21) & 0xf;
341 set_cc = (insn >> 20) & 1;
342 logic_cc = table_logic_cc[op1] & set_cc;
344 /* data processing instruction */
345 if (insn & (1 << 25)) {
346 /* immediate operand */
348 shift = ((insn >> 8) & 0xf) * 2;
350 val = (val >> shift) | (val << (32 - shift));
351 gen_op_movl_T1_im(val);
352 /* XXX: is CF modified ? */
356 gen_movl_T1_reg(s, rm);
357 shiftop = (insn >> 5) & 3;
358 if (!(insn & (1 << 4))) {
359 shift = (insn >> 7) & 0x1f;
362 gen_shift_T1_im_cc[shiftop](shift);
364 gen_shift_T1_im[shiftop](shift);
368 rs = (insn >> 8) & 0xf;
369 gen_movl_T0_reg(s, rs);
371 gen_shift_T1_T0_cc[shiftop]();
373 gen_shift_T1_T0[shiftop]();
377 if (op1 != 0x0f && op1 != 0x0d) {
378 rn = (insn >> 16) & 0xf;
379 gen_movl_T0_reg(s, rn);
381 rd = (insn >> 12) & 0xf;
385 gen_movl_reg_T0(s, rd);
387 gen_op_logic_T0_cc();
391 gen_movl_reg_T0(s, rd);
393 gen_op_logic_T0_cc();
397 gen_op_subl_T0_T1_cc();
400 gen_movl_reg_T0(s, rd);
404 gen_op_rsbl_T0_T1_cc();
407 gen_movl_reg_T0(s, rd);
411 gen_op_addl_T0_T1_cc();
414 gen_movl_reg_T0(s, rd);
418 gen_op_adcl_T0_T1_cc();
421 gen_movl_reg_T0(s, rd);
425 gen_op_sbcl_T0_T1_cc();
428 gen_movl_reg_T0(s, rd);
432 gen_op_rscl_T0_T1_cc();
435 gen_movl_reg_T0(s, rd);
440 gen_op_logic_T0_cc();
446 gen_op_logic_T0_cc();
451 gen_op_subl_T0_T1_cc();
456 gen_op_addl_T0_T1_cc();
461 gen_movl_reg_T0(s, rd);
463 gen_op_logic_T0_cc();
466 gen_movl_reg_T1(s, rd);
468 gen_op_logic_T1_cc();
472 gen_movl_reg_T0(s, rd);
474 gen_op_logic_T0_cc();
479 gen_movl_reg_T1(s, rd);
481 gen_op_logic_T1_cc();
485 /* other instructions */
486 op1 = (insn >> 24) & 0xf;
490 sh = (insn >> 5) & 3;
493 rd = (insn >> 16) & 0xf;
494 rn = (insn >> 12) & 0xf;
495 rs = (insn >> 8) & 0xf;
497 if (!(insn & (1 << 23))) {
499 gen_movl_T0_reg(s, rs);
500 gen_movl_T1_reg(s, rm);
502 if (insn & (1 << 21)) {
503 gen_movl_T1_reg(s, rn);
506 if (insn & (1 << 20))
507 gen_op_logic_T0_cc();
508 gen_movl_reg_T0(s, rd);
511 gen_movl_T0_reg(s, rs);
512 gen_movl_T1_reg(s, rm);
513 if (insn & (1 << 22))
514 gen_op_imull_T0_T1();
517 if (insn & (1 << 21))
518 gen_op_addq_T0_T1(rn, rd);
519 if (insn & (1 << 20))
521 gen_movl_reg_T0(s, rn);
522 gen_movl_reg_T1(s, rd);
525 /* SWP instruction */
526 rn = (insn >> 16) & 0xf;
527 rd = (insn >> 12) & 0xf;
530 gen_movl_T0_reg(s, rm);
531 gen_movl_T1_reg(s, rn);
532 if (insn & (1 << 22)) {
537 gen_movl_reg_T0(s, rd);
540 /* load/store half word */
541 rn = (insn >> 16) & 0xf;
542 rd = (insn >> 12) & 0xf;
543 gen_movl_T1_reg(s, rn);
544 gen_add_datah_offset(s, insn);
545 if (insn & (1 << 20)) {
559 gen_movl_reg_T0(s, rd);
562 gen_movl_T0_reg(s, rd);
565 if (!(insn & (1 << 24))) {
566 gen_add_datah_offset(s, insn);
567 gen_movl_reg_T1(s, rn);
568 } else if (insn & (1 << 21)) {
569 gen_movl_reg_T1(s, rn);
577 /* load/store byte/word */
578 rn = (insn >> 16) & 0xf;
579 rd = (insn >> 12) & 0xf;
580 gen_movl_T1_reg(s, rn);
581 if (insn & (1 << 24))
582 gen_add_data_offset(s, insn);
583 if (insn & (1 << 20)) {
585 if (insn & (1 << 22))
589 gen_movl_reg_T0(s, rd);
592 gen_movl_T0_reg(s, rd);
593 if (insn & (1 << 22))
598 if (!(insn & (1 << 24))) {
599 gen_add_data_offset(s, insn);
600 gen_movl_reg_T1(s, rn);
601 } else if (insn & (1 << 21))
602 gen_movl_reg_T1(s, rn); {
609 /* load/store multiple words */
610 /* XXX: store correct base if write back */
611 if (insn & (1 << 22))
612 goto illegal_op; /* only usable in supervisor mode */
613 rn = (insn >> 16) & 0xf;
614 gen_movl_T1_reg(s, rn);
616 /* compute total size */
622 /* XXX: test invalid n == 0 case ? */
623 if (insn & (1 << 23)) {
624 if (insn & (1 << 24)) {
626 gen_op_addl_T1_im(4);
631 if (insn & (1 << 24)) {
633 gen_op_addl_T1_im(-(n * 4));
637 gen_op_addl_T1_im(-((n - 1) * 4));
642 if (insn & (1 << i)) {
643 if (insn & (1 << 20)) {
646 gen_movl_reg_T0(s, i);
650 /* special case: r15 = PC + 12 */
651 val = (long)s->pc + 8;
652 gen_op_movl_TN_im[0](val);
654 gen_movl_T0_reg(s, i);
659 /* no need to add after the last transfer */
661 gen_op_addl_T1_im(4);
664 if (insn & (1 << 21)) {
666 if (insn & (1 << 23)) {
667 if (insn & (1 << 24)) {
671 gen_op_addl_T1_im(4);
674 if (insn & (1 << 24)) {
677 gen_op_addl_T1_im(-((n - 1) * 4));
680 gen_op_addl_T1_im(-(n * 4));
683 gen_movl_reg_T1(s, rn);
692 /* branch (and link) */
694 if (insn & (1 << 24)) {
695 gen_op_movl_T0_im(val);
696 gen_op_movl_reg_TN[0][14]();
698 offset = (((int)insn << 8) >> 8);
699 val += (offset << 2) + 4;
700 gen_op_jmp((long)s->tb, val);
701 s->is_jmp = DISAS_TB_JUMP;
706 gen_op_movl_T0_im((long)s->pc);
707 gen_op_movl_reg_TN[0][15]();
709 s->is_jmp = DISAS_JUMP;
713 rd = (insn >> 12) & 0x7;
714 rn = (insn >> 16) & 0xf;
715 gen_movl_T1_reg(s, rn);
717 if (!(insn & (1 << 23)))
719 switch((insn >> 8) & 0xf) {
722 if ((insn & (1 << 24)))
723 gen_op_addl_T1_im(val);
725 if (!(insn & (1 << 24)))
726 gen_op_addl_T1_im(val);
727 if (insn & (1 << 21))
728 gen_movl_reg_T1(s, rn);
733 /* load store multiple */
734 if ((insn & (1 << 24)))
735 gen_op_addl_T1_im(val);
736 switch(insn & 0x00408000) {
737 case 0x00008000: n = 1; break;
738 case 0x00400000: n = 2; break;
739 case 0x00408000: n = 3; break;
740 default: n = 4; break;
742 for(i = 0;i < n; i++) {
745 if (!(insn & (1 << 24)))
746 gen_op_addl_T1_im(val);
747 if (insn & (1 << 21))
748 gen_movl_reg_T1(s, rn);
758 switch((insn >> 20) & 0xf) {
773 gen_op_movl_T0_im((long)s->pc - 4);
774 gen_op_movl_reg_TN[0][15]();
776 s->is_jmp = DISAS_JUMP;
782 /* generate intermediate code in gen_opc_buf and gen_opparam_buf for
783 basic block 'tb'. If search_pc is TRUE, also generate PC
784 information for each intermediate instruction. */
785 static inline int gen_intermediate_code_internal(CPUState *env,
786 TranslationBlock *tb,
789 DisasContext dc1, *dc = &dc1;
790 uint16_t *gen_opc_end;
794 /* generate intermediate code */
795 pc_start = (uint8_t *)tb->pc;
799 gen_opc_ptr = gen_opc_buf;
800 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
801 gen_opparam_ptr = gen_opparam_buf;
803 dc->is_jmp = DISAS_NEXT;
808 j = gen_opc_ptr - gen_opc_buf;
812 gen_opc_instr_start[lj++] = 0;
814 gen_opc_pc[lj] = (uint32_t)dc->pc;
815 gen_opc_instr_start[lj] = 1;
818 } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
819 (dc->pc - pc_start) < (TARGET_PAGE_SIZE - 32));
821 case DISAS_JUMP_NEXT:
823 gen_op_jmp((long)dc->tb, (long)dc->pc);
827 /* indicate that the hash table must be used to find the next TB */
832 /* nothing more to generate */
835 *gen_opc_ptr = INDEX_op_end;
839 fprintf(logfile, "----------------\n");
840 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
841 disas(logfile, pc_start, dc->pc - pc_start, 0, 0);
842 fprintf(logfile, "\n");
844 fprintf(logfile, "OP:\n");
845 dump_ops(gen_opc_buf, gen_opparam_buf);
846 fprintf(logfile, "\n");
850 tb->size = dc->pc - pc_start;
854 int gen_intermediate_code(CPUState *env, TranslationBlock *tb)
856 return gen_intermediate_code_internal(env, tb, 0);
859 int gen_intermediate_code_pc(CPUState *env, TranslationBlock *tb)
861 return gen_intermediate_code_internal(env, tb, 1);
864 CPUARMState *cpu_arm_init(void)
870 env = malloc(sizeof(CPUARMState));
873 memset(env, 0, sizeof(CPUARMState));
877 void cpu_arm_close(CPUARMState *env)
882 void cpu_arm_dump_state(CPUARMState *env, FILE *f, int flags)
887 fprintf(f, "R%02d=%08x", i, env->regs[i]);
893 fprintf(f, "PSR=%08x %c%c%c%c\n",
895 env->cpsr & (1 << 31) ? 'N' : '-',
896 env->cpsr & (1 << 30) ? 'Z' : '-',
897 env->cpsr & (1 << 29) ? 'C' : '-',
898 env->cpsr & (1 << 28) ? 'V' : '-');