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, see <http://www.gnu.org/licenses/>.
27 #define NO_CPU_IO_DEFS
31 #include "qemu-timer.h"
33 /* code generation context */
36 void cpu_gen_init(void)
38 tcg_context_init(&tcg_ctx
);
41 /* return non zero if the very first instruction is invalid so that
42 the virtual CPU can trigger an exception.
44 '*gen_code_size_ptr' contains the size of the generated code (host
47 int cpu_gen_code(CPUArchState
*env
, TranslationBlock
*tb
, int *gen_code_size_ptr
)
49 TCGContext
*s
= &tcg_ctx
;
50 uint8_t *gen_code_buf
;
52 #ifdef CONFIG_PROFILER
56 #ifdef CONFIG_PROFILER
57 s
->tb_count1
++; /* includes aborted translations because of
59 ti
= profile_getclock();
63 gen_intermediate_code(env
, tb
);
65 /* generate machine code */
66 gen_code_buf
= tb
->tc_ptr
;
67 tb
->tb_next_offset
[0] = 0xffff;
68 tb
->tb_next_offset
[1] = 0xffff;
69 s
->tb_next_offset
= tb
->tb_next_offset
;
70 #ifdef USE_DIRECT_JUMP
71 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
74 s
->tb_jmp_offset
= NULL
;
75 s
->tb_next
= tb
->tb_next
;
78 #ifdef CONFIG_PROFILER
80 s
->interm_time
+= profile_getclock() - ti
;
81 s
->code_time
-= profile_getclock();
83 gen_code_size
= tcg_gen_code(s
, gen_code_buf
);
84 *gen_code_size_ptr
= gen_code_size
;
85 #ifdef CONFIG_PROFILER
86 s
->code_time
+= profile_getclock();
87 s
->code_in_len
+= tb
->size
;
88 s
->code_out_len
+= gen_code_size
;
92 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM
)) {
93 qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr
);
94 log_disas(tb
->tc_ptr
, *gen_code_size_ptr
);
102 /* The cpu state corresponding to 'searched_pc' is restored.
104 int cpu_restore_state(TranslationBlock
*tb
,
105 CPUArchState
*env
, uintptr_t searched_pc
)
107 TCGContext
*s
= &tcg_ctx
;
110 #ifdef CONFIG_PROFILER
114 #ifdef CONFIG_PROFILER
115 ti
= profile_getclock();
119 gen_intermediate_code_pc(env
, tb
);
122 /* Reset the cycle counter to the start of the block. */
123 env
->icount_decr
.u16
.low
+= tb
->icount
;
124 /* Clear the IO flag. */
128 /* find opc index corresponding to search_pc */
129 tc_ptr
= (uintptr_t)tb
->tc_ptr
;
130 if (searched_pc
< tc_ptr
)
133 s
->tb_next_offset
= tb
->tb_next_offset
;
134 #ifdef USE_DIRECT_JUMP
135 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
138 s
->tb_jmp_offset
= NULL
;
139 s
->tb_next
= tb
->tb_next
;
141 j
= tcg_gen_code_search_pc(s
, (uint8_t *)tc_ptr
, searched_pc
- tc_ptr
);
144 /* now find start of instruction before */
145 while (s
->gen_opc_instr_start
[j
] == 0) {
148 env
->icount_decr
.u16
.low
-= s
->gen_opc_icount
[j
];
150 restore_state_to_opc(env
, tb
, j
);
152 #ifdef CONFIG_PROFILER
153 s
->restore_time
+= profile_getclock() - ti
;