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 uint16_t gen_opc_buf
[OPC_BUF_SIZE
];
37 TCGArg gen_opparam_buf
[OPPARAM_BUF_SIZE
];
39 target_ulong gen_opc_pc
[OPC_BUF_SIZE
];
40 uint16_t gen_opc_icount
[OPC_BUF_SIZE
];
41 uint8_t gen_opc_instr_start
[OPC_BUF_SIZE
];
43 void cpu_gen_init(void)
45 tcg_context_init(&tcg_ctx
);
48 /* return non zero if the very first instruction is invalid so that
49 the virtual CPU can trigger an exception.
51 '*gen_code_size_ptr' contains the size of the generated code (host
54 int cpu_gen_code(CPUArchState
*env
, TranslationBlock
*tb
, int *gen_code_size_ptr
)
56 TCGContext
*s
= &tcg_ctx
;
57 uint8_t *gen_code_buf
;
59 #ifdef CONFIG_PROFILER
63 #ifdef CONFIG_PROFILER
64 s
->tb_count1
++; /* includes aborted translations because of
66 ti
= profile_getclock();
70 gen_intermediate_code(env
, tb
);
72 /* generate machine code */
73 gen_code_buf
= tb
->tc_ptr
;
74 tb
->tb_next_offset
[0] = 0xffff;
75 tb
->tb_next_offset
[1] = 0xffff;
76 s
->tb_next_offset
= tb
->tb_next_offset
;
77 #ifdef USE_DIRECT_JUMP
78 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
81 s
->tb_jmp_offset
= NULL
;
82 s
->tb_next
= tb
->tb_next
;
85 #ifdef CONFIG_PROFILER
87 s
->interm_time
+= profile_getclock() - ti
;
88 s
->code_time
-= profile_getclock();
90 gen_code_size
= tcg_gen_code(s
, gen_code_buf
);
91 *gen_code_size_ptr
= gen_code_size
;
92 #ifdef CONFIG_PROFILER
93 s
->code_time
+= profile_getclock();
94 s
->code_in_len
+= tb
->size
;
95 s
->code_out_len
+= gen_code_size
;
99 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM
)) {
100 qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr
);
101 log_disas(tb
->tc_ptr
, *gen_code_size_ptr
);
109 /* The cpu state corresponding to 'searched_pc' is restored.
111 int cpu_restore_state(TranslationBlock
*tb
,
112 CPUArchState
*env
, uintptr_t searched_pc
)
114 TCGContext
*s
= &tcg_ctx
;
117 #ifdef CONFIG_PROFILER
121 #ifdef CONFIG_PROFILER
122 ti
= profile_getclock();
126 gen_intermediate_code_pc(env
, tb
);
129 /* Reset the cycle counter to the start of the block. */
130 env
->icount_decr
.u16
.low
+= tb
->icount
;
131 /* Clear the IO flag. */
135 /* find opc index corresponding to search_pc */
136 tc_ptr
= (uintptr_t)tb
->tc_ptr
;
137 if (searched_pc
< tc_ptr
)
140 s
->tb_next_offset
= tb
->tb_next_offset
;
141 #ifdef USE_DIRECT_JUMP
142 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
145 s
->tb_jmp_offset
= NULL
;
146 s
->tb_next
= tb
->tb_next
;
148 j
= tcg_gen_code_search_pc(s
, (uint8_t *)tc_ptr
, searched_pc
- tc_ptr
);
151 /* now find start of instruction before */
152 while (gen_opc_instr_start
[j
] == 0)
154 env
->icount_decr
.u16
.low
-= gen_opc_icount
[j
];
156 restore_state_to_opc(env
, tb
, j
);
158 #ifdef CONFIG_PROFILER
159 s
->restore_time
+= profile_getclock() - ti
;