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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
28 #define NO_CPU_IO_DEFS
34 /* code generation context */
37 uint16_t gen_opc_buf
[OPC_BUF_SIZE
];
38 TCGArg gen_opparam_buf
[OPPARAM_BUF_SIZE
];
40 target_ulong gen_opc_pc
[OPC_BUF_SIZE
];
41 uint16_t gen_opc_icount
[OPC_BUF_SIZE
];
42 uint8_t gen_opc_instr_start
[OPC_BUF_SIZE
];
43 #if defined(TARGET_I386)
44 uint8_t gen_opc_cc_op
[OPC_BUF_SIZE
];
45 #elif defined(TARGET_SPARC)
46 target_ulong gen_opc_npc
[OPC_BUF_SIZE
];
47 target_ulong gen_opc_jump_pc
[2];
48 #elif defined(TARGET_MIPS) || defined(TARGET_SH4)
49 uint32_t gen_opc_hflags
[OPC_BUF_SIZE
];
52 /* XXX: suppress that */
53 unsigned long code_gen_max_block_size(void)
55 static unsigned long max
;
58 max
= TCG_MAX_OP_SIZE
;
59 #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
68 void cpu_gen_init(void)
70 tcg_context_init(&tcg_ctx
);
71 tcg_set_frame(&tcg_ctx
, TCG_AREG0
, offsetof(CPUState
, temp_buf
),
72 CPU_TEMP_BUF_NLONGS
* sizeof(long));
75 /* return non zero if the very first instruction is invalid so that
76 the virtual CPU can trigger an exception.
78 '*gen_code_size_ptr' contains the size of the generated code (host
81 int cpu_gen_code(CPUState
*env
, TranslationBlock
*tb
, int *gen_code_size_ptr
)
83 TCGContext
*s
= &tcg_ctx
;
84 uint8_t *gen_code_buf
;
86 #ifdef CONFIG_PROFILER
90 #ifdef CONFIG_PROFILER
91 s
->tb_count1
++; /* includes aborted translations because of
93 ti
= profile_getclock();
97 gen_intermediate_code(env
, tb
);
99 /* generate machine code */
100 gen_code_buf
= tb
->tc_ptr
;
101 tb
->tb_next_offset
[0] = 0xffff;
102 tb
->tb_next_offset
[1] = 0xffff;
103 s
->tb_next_offset
= tb
->tb_next_offset
;
104 #ifdef USE_DIRECT_JUMP
105 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
107 /* the following two entries are optional (only used for string ops) */
108 /* XXX: not used ? */
109 tb
->tb_jmp_offset
[2] = 0xffff;
110 tb
->tb_jmp_offset
[3] = 0xffff;
112 s
->tb_jmp_offset
= NULL
;
113 s
->tb_next
= tb
->tb_next
;
116 #ifdef CONFIG_PROFILER
118 s
->interm_time
+= profile_getclock() - ti
;
119 s
->code_time
-= profile_getclock();
121 gen_code_size
= tcg_gen_code(s
, gen_code_buf
);
122 *gen_code_size_ptr
= gen_code_size
;
123 #ifdef CONFIG_PROFILER
124 s
->code_time
+= profile_getclock();
125 s
->code_in_len
+= tb
->size
;
126 s
->code_out_len
+= gen_code_size
;
130 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM
)) {
131 qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr
);
132 log_disas(tb
->tc_ptr
, *gen_code_size_ptr
);
140 /* The cpu state corresponding to 'searched_pc' is restored.
142 int cpu_restore_state(TranslationBlock
*tb
,
143 CPUState
*env
, unsigned long searched_pc
,
146 TCGContext
*s
= &tcg_ctx
;
148 unsigned long tc_ptr
;
149 #ifdef CONFIG_PROFILER
153 #ifdef CONFIG_PROFILER
154 ti
= profile_getclock();
158 gen_intermediate_code_pc(env
, tb
);
161 /* Reset the cycle counter to the start of the block. */
162 env
->icount_decr
.u16
.low
+= tb
->icount
;
163 /* Clear the IO flag. */
167 /* find opc index corresponding to search_pc */
168 tc_ptr
= (unsigned long)tb
->tc_ptr
;
169 if (searched_pc
< tc_ptr
)
172 s
->tb_next_offset
= tb
->tb_next_offset
;
173 #ifdef USE_DIRECT_JUMP
174 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
177 s
->tb_jmp_offset
= NULL
;
178 s
->tb_next
= tb
->tb_next
;
180 j
= tcg_gen_code_search_pc(s
, (uint8_t *)tc_ptr
, searched_pc
- tc_ptr
);
183 /* now find start of instruction before */
184 while (gen_opc_instr_start
[j
] == 0)
186 env
->icount_decr
.u16
.low
-= gen_opc_icount
[j
];
188 gen_pc_load(env
, tb
, searched_pc
, j
, puc
);
190 #ifdef CONFIG_PROFILER
191 s
->restore_time
+= profile_getclock() - ti
;