2 * Generic intermediate code generation.
4 * Copyright (C) 2016-2017 LluĂs Vilanova <vilanova@ac.upc.edu>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qemu/error-report.h"
13 #include "tcg/tcg-op.h"
14 #include "exec/exec-all.h"
15 #include "exec/gen-icount.h"
17 #include "exec/translator.h"
18 #include "exec/plugin-gen.h"
19 #include "sysemu/replay.h"
21 /* Pairs with tcg_clear_temp_count.
22 To be called by #TranslatorOps.{translate_insn,tb_stop} if
23 (1) the target is sufficiently clean to support reporting,
24 (2) as and when all temporaries are known to be consumed.
25 For most targets, (2) is at the end of translate_insn. */
26 void translator_loop_temp_check(DisasContextBase
*db
)
28 if (tcg_check_temp_count()) {
29 qemu_log("warning: TCG temporary leaks before "
30 TARGET_FMT_lx
"\n", db
->pc_next
);
34 void translator_loop(const TranslatorOps
*ops
, DisasContextBase
*db
,
35 CPUState
*cpu
, TranslationBlock
*tb
, int max_insns
)
40 /* Initialize DisasContext */
42 db
->pc_first
= tb
->pc
;
43 db
->pc_next
= db
->pc_first
;
44 db
->is_jmp
= DISAS_NEXT
;
46 db
->max_insns
= max_insns
;
47 db
->singlestep_enabled
= cpu
->singlestep_enabled
;
49 ops
->init_disas_context(db
, cpu
);
50 tcg_debug_assert(db
->is_jmp
== DISAS_NEXT
); /* no early exit */
52 /* Reset the temp count so that we can identify leaks */
53 tcg_clear_temp_count();
55 /* Start translating. */
57 ops
->tb_start(db
, cpu
);
58 tcg_debug_assert(db
->is_jmp
== DISAS_NEXT
); /* no early exit */
60 plugin_enabled
= plugin_gen_tb_start(cpu
, tb
,
61 tb_cflags(db
->tb
) & CF_MEMI_ONLY
);
65 ops
->insn_start(db
, cpu
);
66 tcg_debug_assert(db
->is_jmp
== DISAS_NEXT
); /* no early exit */
69 plugin_gen_insn_start(cpu
, db
);
72 /* Pass breakpoint hits to target for further processing */
73 if (!db
->singlestep_enabled
74 && unlikely(!QTAILQ_EMPTY(&cpu
->breakpoints
))) {
76 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
77 if (bp
->pc
== db
->pc_next
) {
78 if (ops
->breakpoint_check(db
, cpu
, bp
)) {
84 /* The breakpoint_check hook may use DISAS_TOO_MANY to indicate
85 that only one more instruction is to be executed. Otherwise
86 it should use DISAS_NORETURN when generating an exception,
87 but may use a DISAS_TARGET_* value for Something Else. */
88 if (db
->is_jmp
> DISAS_TOO_MANY
) {
93 /* Disassemble one instruction. The translate_insn hook should
94 update db->pc_next and db->is_jmp to indicate what should be
95 done next -- either exiting this loop or locate the start of
96 the next instruction. */
97 if (db
->num_insns
== db
->max_insns
98 && (tb_cflags(db
->tb
) & CF_LAST_IO
)) {
99 /* Accept I/O on the last instruction. */
101 ops
->translate_insn(db
, cpu
);
103 /* we should only see CF_MEMI_ONLY for io_recompile */
104 tcg_debug_assert(!(tb_cflags(db
->tb
) & CF_MEMI_ONLY
));
105 ops
->translate_insn(db
, cpu
);
108 /* Stop translation if translate_insn so indicated. */
109 if (db
->is_jmp
!= DISAS_NEXT
) {
114 * We can't instrument after instructions that change control
115 * flow although this only really affects post-load operations.
117 if (plugin_enabled
) {
118 plugin_gen_insn_end();
121 /* Stop translation if the output buffer is full,
122 or we have executed all of the allowed instructions. */
123 if (tcg_op_buf_full() || db
->num_insns
>= db
->max_insns
) {
124 db
->is_jmp
= DISAS_TOO_MANY
;
129 /* Emit code to exit the TB, as indicated by db->is_jmp. */
130 ops
->tb_stop(db
, cpu
);
131 gen_tb_end(db
->tb
, db
->num_insns
- bp_insn
);
133 if (plugin_enabled
) {
134 plugin_gen_tb_end(cpu
);
137 /* The disas_log hook may use these values rather than recompute. */
138 tb
->size
= db
->pc_next
- db
->pc_first
;
139 tb
->icount
= db
->num_insns
;
142 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)
143 && qemu_log_in_addr_range(db
->pc_first
)) {
144 FILE *logfile
= qemu_log_lock();
145 qemu_log("----------------\n");
146 ops
->disas_log(db
, cpu
);
148 qemu_log_unlock(logfile
);