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 #ifndef EXEC__TRANSLATOR_H
11 #define EXEC__TRANSLATOR_H
14 * Include this header from a target-specific file, and add a
16 * DisasContextBase base;
18 * member in your target-specific DisasContext.
22 #include "qemu/bswap.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
25 #include "exec/plugin-gen.h"
31 * @DISAS_NEXT: Next instruction in program order.
32 * @DISAS_TOO_MANY: Too many instructions translated.
33 * @DISAS_NORETURN: Following code is dead.
34 * @DISAS_TARGET_*: Start of target-specific conditions.
36 * What instruction to disassemble next.
38 typedef enum DisasJumpType
{
58 * @tb: Translation block for this disassembly.
59 * @pc_first: Address of first guest instruction in this TB.
60 * @pc_next: Address of next guest instruction in this TB (current during
62 * @is_jmp: What instruction to disassemble next.
63 * @num_insns: Number of translated instructions (including current).
64 * @max_insns: Maximum number of instructions to be translated in this TB.
65 * @singlestep_enabled: "Hardware" single stepping enabled.
67 * Architecture-agnostic disassembly context.
69 typedef struct DisasContextBase
{
70 const TranslationBlock
*tb
;
71 target_ulong pc_first
;
76 bool singlestep_enabled
;
81 * @init_disas_context:
82 * Initialize the target-specific portions of DisasContext struct.
83 * The generic DisasContextBase has already been initialized.
86 * Emit any code required before the start of the main loop,
87 * after the generic gen_tb_start().
90 * Emit the tcg_gen_insn_start opcode.
93 * Disassemble one instruction and set db->pc_next for the start
94 * of the following instruction. Set db->is_jmp as necessary to
95 * terminate the main loop.
98 * Emit any opcodes required to exit the TB, based on db->is_jmp.
101 * Print instruction disassembly to log.
103 typedef struct TranslatorOps
{
104 void (*init_disas_context
)(DisasContextBase
*db
, CPUState
*cpu
);
105 void (*tb_start
)(DisasContextBase
*db
, CPUState
*cpu
);
106 void (*insn_start
)(DisasContextBase
*db
, CPUState
*cpu
);
107 void (*translate_insn
)(DisasContextBase
*db
, CPUState
*cpu
);
108 void (*tb_stop
)(DisasContextBase
*db
, CPUState
*cpu
);
109 void (*disas_log
)(const DisasContextBase
*db
, CPUState
*cpu
);
114 * @ops: Target-specific operations.
115 * @db: Disassembly context.
117 * @tb: Translation block.
118 * @max_insns: Maximum number of insns to translate.
120 * Generic translator loop.
122 * Translation will stop in the following cases (in order):
123 * - When is_jmp set by #TranslatorOps::breakpoint_check.
124 * - set to DISAS_TOO_MANY exits after translating one more insn
125 * - set to any other value than DISAS_NEXT exits immediately.
126 * - When is_jmp set by #TranslatorOps::translate_insn.
127 * - set to any value other than DISAS_NEXT exits immediately.
128 * - When the TCG operation buffer is full.
129 * - When single-stepping is enabled (system-wide or on the current vCPU).
130 * - When too many instructions have been translated.
132 void translator_loop(const TranslatorOps
*ops
, DisasContextBase
*db
,
133 CPUState
*cpu
, TranslationBlock
*tb
, int max_insns
);
135 void translator_loop_temp_check(DisasContextBase
*db
);
138 * translator_use_goto_tb
139 * @db: Disassembly context
140 * @dest: target pc of the goto
142 * Return true if goto_tb is allowed between the current TB
143 * and the destination PC.
145 bool translator_use_goto_tb(DisasContextBase
*db
, target_ulong dest
);
148 * Translator Load Functions
150 * These are intended to replace the direct usage of the cpu_ld*_code
151 * functions and are mandatory for front-ends that have been migrated
152 * to the common translator_loop. These functions are only intended
153 * to be called from the translation stage and should not be called
154 * from helper functions. Those functions should be converted to encode
155 * the relevant information at translation time.
158 #define GEN_TRANSLATOR_LD(fullname, type, load_fn, swap_fn) \
160 fullname ## _swap(CPUArchState *env, abi_ptr pc, bool do_swap) \
162 type ret = load_fn(env, pc); \
164 ret = swap_fn(ret); \
166 plugin_insn_append(&ret, sizeof(ret)); \
170 static inline type fullname(CPUArchState *env, abi_ptr pc) \
172 return fullname ## _swap(env, pc, false); \
175 GEN_TRANSLATOR_LD(translator_ldub
, uint8_t, cpu_ldub_code
, /* no swap */)
176 GEN_TRANSLATOR_LD(translator_ldsw
, int16_t, cpu_ldsw_code
, bswap16
)
177 GEN_TRANSLATOR_LD(translator_lduw
, uint16_t, cpu_lduw_code
, bswap16
)
178 GEN_TRANSLATOR_LD(translator_ldl
, uint32_t, cpu_ldl_code
, bswap32
)
179 GEN_TRANSLATOR_LD(translator_ldq
, uint64_t, cpu_ldq_code
, bswap64
)
180 #undef GEN_TRANSLATOR_LD
182 #endif /* EXEC__TRANSLATOR_H */