{"id":49232,"date":"2025-07-30T16:46:41","date_gmt":"2025-07-30T08:46:41","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/49232.html"},"modified":"2025-07-30T16:46:41","modified_gmt":"2025-07-30T08:46:41","slug":"qemu-riscv-tcg-%e8%af%a6%e8%a7%a3%e5%8d%81%e4%b8%80-riscv-cpu-%e7%8a%b6%e6%80%81%e5%9c%a8tcg-variables%e4%b8%ad%e7%9a%84%e8%a1%a8%e7%a4%ba%ef%bc%88representation%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/49232.html","title":{"rendered":"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 -- RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09"},"content":{"rendered":"<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5728\u524d\u6587\u300aRISCV ISA -&gt; TCG Ops\u300b\u4e2d&#xff0c;TCG IR \u64cd\u4f5c\u5bf9\u8c61\u662f Variables&#xff0c;\u5176\u4e2d Fixed Global Variable&#xff08;TEMP_FIXED&#xff09;&#xff0c;\u4e0e Global Variable&#xff08;TEMP_GLOBAL&#xff09;\u6bd4\u8f83\u7279\u6b8a&#xff0c;\u662f\u7528\u4e8e\u6307\u4ee3 CPUState \u4ee5\u53ca \u7279\u5b9aCPU \u7684\u72b6\u6001\u7684\u3002\u672c\u6587\u5c31\u68b3\u7406\u6e05\u695a&#xff0c;\u5728 TCG IR \u4e2d&#xff0c;\u8fd9\u4e9b Variable \u5177\u4f53\u5b9a\u4e49\u53ca\u5176\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u9996\u5148&#xff0c;\u6574\u4e2a\u76ee\u6807CPU\u7684\u72b6\u6001\u5b58\u5728\u00a0\u00a0CPUState *cpu \u4e2d&#xff0c;\u4e5f\u5c31\u662f\u6267\u884c\u4e3b\u5faa\u73af\u4f20\u5165\u7684\u00a0cpu_exec_loop(CPUState *cpu, SyncClocks *sc)\u3002\u6700\u7ec8\u6eaf\u6e90\u5230\u5728\u521b\u5efa QEMU Machine \u7684\u65f6\u5019&#xff0c;\u521b\u5efa\u7684CPU&#xff0c;\u4e5f\u5c31\u662f RISCVCPU:CPUState:DeviceState\u3002\u5176\u5305\u542b\u4e86&#xff0c;CPURISCVState env&#xff0c;\u7ee7\u800c\u5305\u62ec\u4e86 RISCV CPU \u7684\u901a\u7528\u5bc4\u5b58\u5668&#xff08;General Purpose Registers \/ GPRs&#xff09;\u4ee5\u53ca\u72b6\u6001\u5bc4\u5b58\u5668&#xff08;Control and Status Registers \/\u00a0CSRs&#xff09;\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u90a3\u4e48&#xff0c;\u5728 TCG IR \u7684 Global Variables \u9700\u8981\u901a\u8fc7\u6307\u9488\u53bb\u6307\u5411 RISCV CPU \u7684 GPRs&#xff0c;\u800c CSRs\u7684\u8bfb\u5199\u5219\u901a\u8fc7 RISCV csr \u6307\u4ee4\u8fdb\u884c\u8bfb\u5199\u8bbf\u95ee&#xff0c;\u5728 TCG IR \u4e2d\u901a\u8fc7\u8c03\u7528 RISCV CPU \u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u5b9e\u73b0&#xff0c;\u8fd9\u4e2a\u5728\u540e\u7eed\u6587\u7ae0\u4f1a\u8fdb\u884c\u8be6\u7ec6\u8bb2\u89e3\u3002\u73b0\u5728&#xff0c;\u5c31\u53ea\u5173\u6ce8 GPR \u7b49\u5bc4\u5b58\u5668&#xff0c;\u80fd\u591f\u5728 TCG IR\u4e2d&#xff0c;\u76f4\u63a5\u901a\u8fc7 Global Variables \u6765\u8bbf\u95ee\u7684\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 Global Variables \u662f\u901a\u8fc7\u8bb0\u5f55\u4e86 RISCV CPU GPRs \u4e0e cpu_env \u7684\u504f\u79fb\u91cf&#xff08;offset&#xff09;\u6765\u8fdb\u884c\u5bfb\u5740\u7684\u3002\u56e0\u6b64&#xff0c;\u9996\u5148\u8981\u786e\u5b9a cpu_env \u662f\u600e\u4e48\u4ece\u00a0CPUState *cpu \u8fdb\u5165\u5230 TCG IR \u4e2d\u7684\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5728 TCG IR&#xff0c;cpu_env \u5373 \u4e0a\u8ff0\u7684\u00a0CPURISCVState env&#xff0c;\u7531\u00a0TCGv_env tcg_env \u6307\u4ee3&#xff0c;\u662f\u4e00\u4e2aC\u7684\u516c\u5171\u53d8\u91cf&#xff08;Global Variable&#xff09;&#xff0c;\u5176\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>TCGContext tcg_init_ctx;<br \/>\n__thread TCGContext *tcg_ctx;<br \/>\ntypedef TCGv_ptr TCGv_env;<br \/>\nTCGv_env tcg_env;<\/p>\n<p>static void tcg_context_init(unsigned max_threads){<br \/>\n    TCGContext *s &#061; &amp;tcg_init_ctx;<br \/>\n    TCGTemp *ts;<br \/>\n    &#8230;<br \/>\n    ts &#061; tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, &#034;env&#034;);<br \/>\n    tcg_env &#061; temp_tcgv_ptr(ts);}<br \/>\nstatic TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,<br \/>\n                                            TCGReg reg, const char *name){<br \/>\n    TCGTemp *ts;<br \/>\n    tcg_debug_assert(TCG_TARGET_REG_BITS &#061;&#061; 64 || type &#061;&#061; TCG_TYPE_I32);<br \/>\n    ts &#061; tcg_global_alloc(s);<br \/>\n    ts-&gt;base_type &#061; type;<br \/>\n    ts-&gt;type &#061; type;<br \/>\n    ts-&gt;kind &#061; TEMP_FIXED;<br \/>\n    ts-&gt;reg &#061; reg;<br \/>\n    ts-&gt;name &#061; name;<br \/>\n    tcg_regset_set_reg(s-&gt;reserved_regs, reg);<br \/>\n    return ts;}<br \/>\nstatic TCGTemp *tcg_global_alloc(TCGContext *s){<br \/>\n    TCGTemp *ts;<br \/>\n    &#8230;<br \/>\n    s-&gt;nb_globals&#043;&#043;;<br \/>\n    ts &#061; tcg_temp_alloc(s);<br \/>\n    ts-&gt;kind &#061; TEMP_GLOBAL;<br \/>\n    return ts;}<br \/>\nstatic inline TCGv_ptr temp_tcgv_ptr(TCGTemp *t){<br \/>\n    return (TCGv_ptr)temp_tcgv_i32(t);}<br \/>\nstatic inline TCGv_i32 temp_tcgv_i32(TCGTemp *t) {<br \/>\n    (void)temp_idx(t); \/* trigger embedded assert *\/<br \/>\n    return (TCGv_i32)((void *)t &#8211; (void *)tcg_ctx);}<br \/>\nstatic inline size_t temp_idx(TCGTemp *ts){<br \/>\n    return ts &#8211; tcg_ctx-&gt;temps;}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5c31\u662f\u8bf4&#xff0c;tcg_env \u5e76\u4e0d\u662f\u76f4\u63a5\u6307\u5411\u63cf\u8ff0 cpu_env \u7684 TCGTemp&#xff0c;\u8bb0\u4e3a TCGTemp cpu_env&#xff0c;\u800c\u662f\u8bb0\u5f55\u4e86\u5176\u4e0e tcg_ctx \u7684\u504f\u79fb\u91cf\u3002\u800c\u00a0TCGTemp cpu_env \u63cf\u8ff0\u4e86\u00a0TCG_AREG0 \u5bc4\u5b58\u5668\u6307\u5411\u4e86 CPURISCVState env\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5728TCG \u7684 X86\u540e\u7aef\u4e2d&#xff0c;\u5b9a\u4e49\u4e86\u00a0TCG_AREG0 \u7531 EBP \u6765\u5b58\u653e&#xff0c;\u5373&#xff1a;<\/p>\n<p>\/\/ qemu\/tcg\/i386\/tcg-target.h<br \/>\ntypedef enum {<br \/>\n&#8230;<br \/>\n    TCG_AREG0 &#061; TCG_REG_EBP,<br \/>\n    TCG_REG_CALL_STACK &#061; TCG_REG_ESP<br \/>\n} TCGReg;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5e76\u5728 X86 \u751f\u6210 TB \u4ee3\u7801\u65f6&#xff0c;\u5728 TB prologue \u4e2d&#xff0c;\u8bbe\u5b9a&#xff0c;\u5982\u4e0b&#xff1a;<\/p>\n<p>static int __attribute__((noinline))<br \/>\ncpu_exec_loop(CPUState *cpu, SyncClocks *sc){<br \/>\n    &#8230;<br \/>\n    cpu_loop_exec_tb(cpu, tb, s.pc, &amp;last_tb, &amp;tb_exit);<br \/>\n    &#8230;}<br \/>\nstatic inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,<br \/>\n                                    vaddr pc, TranslationBlock **last_tb,<br \/>\n                                    int *tb_exit){<br \/>\n    &#8230;<br \/>\n    tb &#061; cpu_tb_exec(cpu, tb, tb_exit);<br \/>\n    &#8230;}<br \/>\n\/* Execute a TB, and fix up the CPU state afterwards if necessary *\/<br \/>\n\/*<br \/>\n * Disable CFI checks.<br \/>\n * TCG creates binary blobs at runtime, with the transformed code.<br \/>\n * A TB is a blob of binary code, created at runtime and called with an<br \/>\n * indirect function call. Since such function did not exist at compile time,<br \/>\n * the CFI runtime has no way to verify its signature and would fail.<br \/>\n * TCG is not considered a security-sensitive part of QEMU so this does not<br \/>\n * affect the impact of CFI in environment with high security requirements<br \/>\n *\/<br \/>\nstatic inline TranslationBlock * QEMU_DISABLE_CFI<br \/>\ncpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit) {<br \/>\n    &#8230;<br \/>\n    ret &#061; tcg_qemu_tb_exec(cpu_env(cpu), tb_ptr);<br \/>\n    &#8230;}<br \/>\nvoid tcg_prologue_init(void) {<br \/>\n    &#8230;<br \/>\n    tcg_qemu_tb_exec &#061; (tcg_prologue_fn *)tcg_splitwx_to_rx(s-&gt;code_ptr);<br \/>\n    &#8230;<br \/>\n    \/* Generate the prologue.  *\/<br \/>\n    tcg_target_qemu_prologue(s);<br \/>\n    &#8230;}<br \/>\n\/* Generate global QEMU prologue and epilogue code *\/<br \/>\nstatic void tcg_target_qemu_prologue(TCGContext *s) {<br \/>\n    &#8230;<br \/>\n    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);<br \/>\n    tcg_out_addi(s, TCG_REG_ESP, -stack_addend);<br \/>\n    \/* jmp *tb.  *\/<br \/>\n    tcg_out_modrm(s, OPC_GRP5, EXT5_JMPN_Ev, tcg_target_call_iarg_regs[1]);<br \/>\n    &#8230;}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5bf9\u5e94\u7684\u8f93\u51fa\u6709&#xff1a;<\/p>\n<p>PROLOGUE: [size&#061;45]<br \/>\n&#8230;<br \/>\n\/\/ %rdi used to pass 1st argument to functions, store cpu_env(cpu)<br \/>\n\/\/ %rsi used to pass 2nd argument to functions, point to tb code buffer<br \/>\n0x7d0f441b300a:  48 8b ef                 movq     %rdi, %rbp<br \/>\n0x7d0f441b300d:  48 81 c4 78 fb ff ff     addq     $-0x488, %rsp<br \/>\n0x7d0f441b3014:  ff e6                    jmpq     *%rsi<br \/>\n&#8230;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5176\u4e2d\u00a0tcg_qemu_tb_exec(cpu_env(cpu), tb_ptr) \u5c31\u662f \u4f20\u5165\u4e86 cpu_env \u5230 %rbp \u4e86\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u90a3\u4e48&#xff0c;\u627e\u5230\u4e86 cpu_env \u540e&#xff0c;\u5176\u5b83 TCG IR \u76f4\u63a5\u4f7f\u7528\u7684\u5bc4\u5b58\u5668\u7684\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>\/* global register indices *\/<br \/>\nstatic TCGv cpu_gpr[32], cpu_gprh[32], cpu_pc, cpu_vl, cpu_vstart;<br \/>\nstatic TCGv_i64 cpu_fpr[32]; \/* assume F and D extensions *\/<br \/>\nstatic TCGv load_res;<br \/>\nstatic TCGv load_val;<br \/>\n&#8230;<br \/>\nvoid riscv_translate_init(void){<br \/>\n    int i;<br \/>\n    \/*<br \/>\n     * cpu_gpr[0] is a placeholder for the zero register. Do not use it.<br \/>\n     * Use the gen_set_gpr and get_gpr helper functions when accessing regs,<br \/>\n     * unless you specifically block reads\/writes to reg 0.<br \/>\n     *\/<br \/>\n    cpu_gpr[0] &#061; NULL;<br \/>\n    cpu_gprh[0] &#061; NULL;<br \/>\n    for (i &#061; 1; i &lt; 32; i&#043;&#043;) {<br \/>\n        cpu_gpr[i] &#061; tcg_global_mem_new(tcg_env,<br \/>\n            offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);<br \/>\n        cpu_gprh[i] &#061; tcg_global_mem_new(tcg_env,<br \/>\n            offsetof(CPURISCVState, gprh[i]), riscv_int_regnamesh[i]);<br \/>\n    }<br \/>\n    for (i &#061; 0; i &lt; 32; i&#043;&#043;) {<br \/>\n        cpu_fpr[i] &#061; tcg_global_mem_new_i64(tcg_env,<br \/>\n            offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);<br \/>\n    }<br \/>\n    cpu_pc &#061; tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, pc), &#034;pc&#034;);<br \/>\n    cpu_vl &#061; tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vl), &#034;vl&#034;);<br \/>\n    cpu_vstart &#061; tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vstart),<br \/>\n                            &#034;vstart&#034;);<br \/>\n    load_res &#061; tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_res),<br \/>\n                             &#034;load_res&#034;);<br \/>\n    load_val &#061; tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_val),<br \/>\n                             &#034;load_val&#034;);}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u90fd\u662f\u901a\u8fc7\u00a0tcg_global_mem_new() \u51fd\u6570\u6765\u5b9a\u4e49\u3002\u4ee5\u00a0static TCGv cpu_pc \u4e3a\u4f8b&#xff0c;\u6709&#xff1a;<\/p>\n<p>__thread TCGContext *tcg_ctx;<br \/>\ntypedef struct TCGv_i64_d *TCGv_i64;<br \/>\ntypedef TCGv_i64 TCGv;<br \/>\nstatic TCGv cpu_pc &#061; tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, pc), &#034;pc&#034;);<br \/>\nTCGv_i64 tcg_global_mem_new_i64(TCGv_ptr reg, intptr_t off, const char *name){<br \/>\n    TCGTemp *ts &#061; tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_I64);<br \/>\n    return temp_tcgv_i64(ts);}<br \/>\nstatic TCGTemp *tcg_global_mem_new_internal(TCGv_ptr base, intptr_t offset,<br \/>\n                                            const char *name, TCGType type) {<br \/>\n    TCGContext *s &#061; tcg_ctx;<br \/>\n    \/\/ In this case, base_ts &#061;&#061; TCGTemp* cpu_env.<br \/>\n    TCGTemp *base_ts &#061; tcgv_ptr_temp(base);<br \/>\n    TCGTemp *ts &#061; tcg_global_alloc(s);<br \/>\n    int indirect_reg &#061; 0;<br \/>\n    switch (base_ts-&gt;kind) {<br \/>\n    case TEMP_FIXED:<br \/>\n        break;<br \/>\n    &#8230;}<br \/>\n    if (TCG_TARGET_REG_BITS &#061;&#061; 32 &amp;&amp; type &#061;&#061; TCG_TYPE_I64) {<br \/>\n        &#8230;<br \/>\n    } else {<br \/>\n        ts-&gt;base_type &#061; type;<br \/>\n        ts-&gt;type &#061; type;<br \/>\n        ts-&gt;indirect_reg &#061; indirect_reg;<br \/>\n        ts-&gt;mem_allocated &#061; 1;<br \/>\n        ts-&gt;mem_base &#061; base_ts;<br \/>\n        ts-&gt;mem_offset &#061; offset;<br \/>\n        ts-&gt;name &#061; name;<br \/>\n    }<br \/>\n    return ts;<br \/>\n}<br \/>\nstatic inline TCGTemp *tcgv_ptr_temp(TCGv_ptr v){<br \/>\n    return tcgv_i32_temp((TCGv_i32)v);}<br \/>\nstatic inline TCGv_i64 temp_tcgv_i64(TCGTemp *t){<br \/>\n    return (TCGv_i64)temp_tcgv_i32(t);}<br \/>\n\/*<br \/>\n * Using the offset of a temporary, relative to TCGContext, rather than<br \/>\n * its index means that we don&#039;t use 0.  That leaves offset 0 free for<br \/>\n * a NULL representation without having to leave index 0 unused.<br \/>\n *\/<br \/>\nstatic inline TCGTemp *tcgv_i32_temp(TCGv_i32 v){<br \/>\n    return (void *)tcg_ctx &#043; (uintptr_t)v;}<br \/>\nstatic inline TCGv_i32 temp_tcgv_i32(TCGTemp *t){<br \/>\n    (void)temp_idx(t); \/* trigger embedded assert *\/<br \/>\n    return (TCGv_i32)((void *)t &#8211; (void *)tcg_ctx);}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u90a3\u4e48&#xff0c;\u8fd9\u91cc\u7684 static TCGv cpu_pc \u8bb0\u5f55\u4e86 TCGTemp* cpu_pc \u4e0e\u00a0__thread TCGContext *tcg_ctx \u7684\u504f\u79fb\u91cf&#xff08;offset&#xff09;\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e5f\u5c31\u662f TCGv_* \u4e0e TCGTemp* \u7684\u8f6c\u6362\u9700\u8981\u7ecf\u8fc7 \u5176\u4e0etcg_ctx\u7684\u504f\u79fb\u91cf&#xff0c;\u800c TCGv_* \u5c31\u662f\u8bb0\u5f55\u4e86\u5bf9\u5e94\u7684\u504f\u79fb\u91cf&#xff0c;\u800c\u975e TCGTemp* \u6307\u9488\u3002\u800c\u5bf9\u5e94\u7684\u00a0TCGTemp* cpu_pc \u5219\u8bb0\u5f55\u4e86 pc \u5bc4\u5b58\u5668\u4e0e cpu_env \u7684\u504f\u79fb\u91cf\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u90a3\u4e48&#xff0c;\u56de\u987e\u524d\u6587\u7684 auipc t0,0 \u7684\u8f6c\u8bd1&#xff0c;\u6709<\/p>\n<p>static bool trans_auipc(DisasContext *ctx, arg_auipc *a){<br \/>\n    \/\/ target_pc &lt;- a-&gt;rd<br \/>\n    TCGv target_pc &#061; dest_gpr(ctx, a-&gt;rd);<br \/>\n    \/\/ target_pc &lt;- pc &#043; a-&gt;imm<br \/>\n    gen_pc_plus_diff(target_pc, ctx, a-&gt;imm);<br \/>\n    \/\/ a-&gt;rd &lt;- targe_pc<br \/>\n    gen_set_gpr(ctx, a-&gt;rd, target_pc);<br \/>\n    return true;}<br \/>\nstatic TCGv dest_gpr(DisasContext *ctx, int reg_num){<br \/>\n    if (reg_num &#061;&#061; 0 || get_olen(ctx) &lt; TARGET_LONG_BITS) {<br \/>\n        return tcg_temp_new();<br \/>\n    }<br \/>\n    return cpu_gpr[reg_num];}<br \/>\nstatic void gen_pc_plus_diff(TCGv target, DisasContext *ctx,<br \/>\n                             target_long diff){<br \/>\n    target_ulong dest &#061; ctx-&gt;base.pc_next &#043; diff;<br \/>\n&#8230;<br \/>\n    if (tb_cflags(ctx-&gt;base.tb) &amp; CF_PCREL) {<br \/>\n        tcg_gen_addi_tl(target, cpu_pc, dest &#8211; ctx-&gt;pc_save);<br \/>\n    &#8230;} else {&#8230;}}<br \/>\nvoid tcg_gen_addi_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2){<br \/>\n    \/* some cases can be optimized here *\/<br \/>\n    if (arg2 &#061;&#061; 0) {<br \/>\n        tcg_gen_mov_i64(ret, arg1);<br \/>\n    } else if (TCG_TARGET_REG_BITS &#061;&#061; 64) {<br \/>\n        tcg_gen_add_i64(ret, arg1, tcg_constant_i64(arg2));<br \/>\n    } else {<br \/>\n        tcg_gen_add2_i32(TCGV_LOW(ret), TCGV_HIGH(ret),<br \/>\n                         TCGV_LOW(arg1), TCGV_HIGH(arg1),<br \/>\n                         tcg_constant_i32(arg2), tcg_constant_i32(arg2 &gt;&gt; 32));}}<br \/>\nvoid tcg_gen_mov_i64(TCGv_i64 ret, TCGv_i64 arg){<br \/>\n    if (ret &#061;&#061; arg) {return;}<br \/>\n    if (TCG_TARGET_REG_BITS &#061;&#061; 64) {<br \/>\n        tcg_gen_op2_i64(INDEX_op_mov, ret, arg);<br \/>\n    } else {&#8230;}}<br \/>\nstatic void DNI tcg_gen_op2_i64(TCGOpcode opc, TCGv_i64 a1, TCGv_i64 a2){<br \/>\n    tcg_gen_op2(opc, TCG_TYPE_I64, tcgv_i64_arg(a1), tcgv_i64_arg(a2));}<br \/>\nTCGOp * NI tcg_gen_op2(TCGOpcode opc, TCGType type, TCGArg a1, TCGArg a2){<br \/>\n    TCGOp *op &#061; tcg_emit_op(opc, 2);<br \/>\n    TCGOP_TYPE(op) &#061; type;<br \/>\n    op-&gt;args[0] &#061; a1;<br \/>\n    op-&gt;args[1] &#061; a2;<br \/>\n    return op;}<br \/>\nTCGOp *tcg_emit_op(TCGOpcode opc, unsigned nargs){<br \/>\n    TCGOp *op &#061; tcg_op_alloc(opc, nargs);<br \/>\n    if (tcg_ctx-&gt;emit_before_op) {<br \/>\n        QTAILQ_INSERT_BEFORE(tcg_ctx-&gt;emit_before_op, op, link);<br \/>\n    } else {<br \/>\n        QTAILQ_INSERT_TAIL(&amp;tcg_ctx-&gt;ops, op, link);<br \/>\n    }<br \/>\n    return op;}<br \/>\nstatic void gen_set_gpr(DisasContext *ctx, int reg_num, TCGv t){<br \/>\n    if (reg_num !&#061; 0) {<br \/>\n        switch (get_ol(ctx)) {<br \/>\n        &#8230;<br \/>\n        case MXL_RV64:<br \/>\n        case MXL_RV128:<br \/>\n            tcg_gen_mov_tl(cpu_gpr[reg_num], t);<br \/>\n            break;<br \/>\n        &#8230;}<br \/>\n    &#8230;}}<br \/>\nstatic inline TCGArg tcgv_i64_arg(TCGv_i64 v){<br \/>\n    return temp_arg(tcgv_i64_temp(v));}<br \/>\nstatic inline TCGArg temp_arg(TCGTemp *ts) {<br \/>\n    return (uintptr_t)ts;}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5728\u751f\u6210\u5bf9\u5e94\u7684 struct TCGOp\u00a0\u65f6&#xff0c;\u4f1a\u5c06 TCGv_* \u8f6c\u53d8\u4e3a TCGArg\u3002\u800c TCGArg \u5b9e\u9645\u4e0a\u5c31\u662f TCGTemp*\u3002\u56e0\u6b64&#xff0c;\u5728 TCGArg (struct TCGOp)::args \u5c31\u662f\u8bb0\u5f55\u4e86 TCGTemp*\u3002\u4e5f\u5c31\u662f&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u00a0 TCGArg (struct TCGOp)::args[index] \u6765\u8bbf\u95ee TCG IR \u64cd\u4f5c\u7684\u64cd\u4f5c\u5bf9\u8c61&#xff08;Operands&#xff09;&#xff0c;\u5982\u00a0TCGTemp* cpu_pc&#xff0c;TCGTemp* cpu_gpr_x5\/t0 \u7b49\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5176\u4e2dTCG IR \u4e2d\u7684\u4e00\u4e2a\u64cd\u4f5c\u7531\u00a0struct TCGOp \u5b9a\u4e49&#xff0c;\u5982\u4e0b&#xff1a;<\/p>\n<p>\/\/ TCG IR Operations<br \/>\nstruct TCGOp {<br \/>\n    TCGOpcode opc   : 8;<br \/>\n    unsigned nargs  : 8;<br \/>\n    \/* Parameters for this opcode.  See below.  *\/<br \/>\n    unsigned param1 : 8;<br \/>\n    unsigned param2 : 8;<br \/>\n    \/* Lifetime data of the operands.  *\/<br \/>\n    TCGLifeData life;<br \/>\n    \/* Next and previous opcodes.  *\/<br \/>\n    QTAILQ_ENTRY(TCGOp) link;<br \/>\n    \/* Register preferences for the output(s).  *\/<br \/>\n    TCGRegSet output_pref[2];<br \/>\n    \/* Arguments for the opcode.  *\/<br \/>\n    TCGArg args[];};<\/p>\n<p>typedef enum TCGOpcode {<br \/>\n#define DEF(name, oargs, iargs, cargs, flags) INDEX_op_ ## name,<br \/>\n#include &#034;tcg\/tcg-opc.h&#034;<br \/>\n#undef DEF<br \/>\n    NB_OPS,<br \/>\n} TCGOpcode;<\/p>\n<p>\/\/ qemu\/include\/tcg\/tcg-opc.h<br \/>\n&#8230;<br \/>\n\/*<br \/>\n * DEF(name, oargs, iargs, cargs, flags)<br \/>\n *\/<br \/>\n\/* predefined ops *\/<br \/>\nDEF(discard, 1, 0, 0, TCG_OPF_NOT_PRESENT)<br \/>\nDEF(set_label, 0, 0, 1, TCG_OPF_BB_END | TCG_OPF_NOT_PRESENT)<br \/>\n\/* variable number of parameters *\/<br \/>\nDEF(call, 0, 0, 3, TCG_OPF_CALL_CLOBBER | TCG_OPF_NOT_PRESENT)<br \/>\nDEF(br, 0, 0, 1, TCG_OPF_BB_END | TCG_OPF_NOT_PRESENT)<br \/>\nDEF(brcond, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_COND_BRANCH | TCG_OPF_INT)<br \/>\nDEF(mb, 0, 0, 1, TCG_OPF_NOT_PRESENT)<br \/>\nDEF(mov, 1, 1, 0, TCG_OPF_INT | TCG_OPF_NOT_PRESENT)<br \/>\n&#8230;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u7531\u6b64&#xff0c;\u53ef\u4ee5\u4e86\u89e3\u5230&#xff0c;\u5728 TCG IR \u4e2d&#xff0c;\u662f\u5982\u4f55\u5bf9 RISCV CPU \u7684\u5bc4\u5b58\u5668\u8fdb\u884c\u8bbf\u95ee\u3002\u5173\u952e\u70b9\u5728\u4e8e\u5bf9 struct ArchCPU* cpu&#xff0c;struct CPUArchState* env&#xff0c;TCGv_*&#xff0c;TCGTemp*&#xff0c;TCGArg \u7684\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb43\u6b21\u3002\u5728 TCG IR \u4e2d\uff0c\u662f\u5982\u4f55\u5bf9 RISCV CPU \u7684\u5bc4\u5b58\u5668\u8fdb\u884c\u8bbf\u95ee\u3002\u5173\u952e\u70b9\u5728\u4e8e\u5bf9 struct ArchCPU* cpu\uff0cstruct CPUArchState* env\uff0cTCGv_*\uff0cTCGTemp*\uff0cTCGArg \u7684\u7406\u89e3\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,81,4780,97,190,1917],"topic":[],"class_list":["post-49232","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-python","tag-qemu","tag-97","tag-190","tag-1917"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 - RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/49232.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 - RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb43\u6b21\u3002\u5728 TCG IR \u4e2d\uff0c\u662f\u5982\u4f55\u5bf9 RISCV CPU \u7684\u5bc4\u5b58\u5668\u8fdb\u884c\u8bbf\u95ee\u3002\u5173\u952e\u70b9\u5728\u4e8e\u5bf9 struct ArchCPU* cpu\uff0cstruct CPUArchState* env\uff0cTCGv_*\uff0cTCGTemp*\uff0cTCGArg \u7684\u7406\u89e3\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/49232.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T08:46:41+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49232.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/49232.html\",\"name\":\"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 - RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T08:46:41+00:00\",\"dateModified\":\"2025-07-30T08:46:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/49232.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/49232.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49232.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 -- RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 - RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/49232.html","og_locale":"zh_CN","og_type":"article","og_title":"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 - RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb43\u6b21\u3002\u5728 TCG IR \u4e2d\uff0c\u662f\u5982\u4f55\u5bf9 RISCV CPU \u7684\u5bc4\u5b58\u5668\u8fdb\u884c\u8bbf\u95ee\u3002\u5173\u952e\u70b9\u5728\u4e8e\u5bf9 struct ArchCPU* cpu\uff0cstruct CPUArchState* env\uff0cTCGv_*\uff0cTCGTemp*\uff0cTCGArg \u7684\u7406\u89e3\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/49232.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T08:46:41+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/49232.html","url":"https:\/\/www.wsisp.com\/helps\/49232.html","name":"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 - RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T08:46:41+00:00","dateModified":"2025-07-30T08:46:41+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/49232.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/49232.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/49232.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"QEMU RISCV TCG \u8be6\u89e3\u5341\u4e00 -- RISCV CPU \u72b6\u6001\u5728TCG Variables\u4e2d\u7684\u8868\u793a\uff08Representation\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=49232"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49232\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=49232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=49232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=49232"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=49232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}