Skip to content
STIMSMITH

trans_addi TCG Translation Function

CodeArtifact WIKI v1 · 5/29/2026

`trans_addi` is a generated QEMU TCG translation function for the RISC-V 64 `ADDI` instruction. In the OpenVADL example, it lowers the instruction semantics `X(rd) := X(rs1) + immS` into 64-bit TCG operations by reading the source register, materializing the sign-extended immediate as a TCG constant, emitting `tcg_gen_add_i64`, moving the result to the destination register, and returning `true`.

Overview

trans_addi is presented as generated C code for a QEMU Tiny Code Generator (TCG) translation function for the RISC-V 64 ADDI instruction. The example comes from an OpenVADL-to-QEMU generation flow in which a VADL instruction-set specification is transformed through VIAM and lowered to TCG operations before C code is emitted for a QEMU frontend. [1]

Source instruction semantics

The source VADL example defines an RV64I-style ADDI instruction over an I-type format. Its semantic assignment is:

instruction ADDI : Itype = X(rd) := X(rs1) + immS

The immediate field immS is derived from the instruction immediate as a signed value, and the operation writes the sum of register X(rs1) and immS to register X(rd). [2]

Generated C translation function

The generated function shown for RISC-V 64 ADDI is:

static bool trans_addi(DisasContext *ctx, arg_addi *a) {
    TCGv_i64 reg_x_rd_dest = dest_x(ctx, a->rd);
    TCGv_i64 reg_x_rs1 = get_x(ctx, a->rs1);
    TCGv_i64 tmp_n4_0 = tcg_temp_new_i64();
    TCGv_i64 const_immS_n3 = tcg_constant_i64(a->immS);

    tcg_gen_add_i64(tmp_n4_0, reg_x_rs1, const_immS_n3);
    tcg_gen_mov_i64(reg_x_rd_dest, tmp_n4_0);

    return true;
}

The function takes a QEMU translation context (DisasContext *ctx) and decoded instruction arguments (arg_addi *a). It obtains a destination TCG value for rd, reads the source register rs1, creates a temporary 64-bit TCG value, materializes the immediate as a 64-bit TCG constant, emits a 64-bit add, moves the computed result into the destination register, and reports successful translation by returning true. [3]

Relationship to the lowering pipeline

The slides describe a generation pipeline in which a VADL specification is processed into VIAM, transformed into TCG-oriented form, lowered, and then emitted as C code for a QEMU frontend. The shown lowered VIAM for RISC-V 64 ADDI contains field accesses for rs1, immS, and rd, register and constant variables, a tcg_add, a destination, and a tcg_mov, matching the generated function’s tcg_gen_add_i64 followed by tcg_gen_mov_i64. [4]

Technical significance

Within the example, trans_addi illustrates how an instruction-level semantic assignment is represented as QEMU TCG IR-emitting C code. It is specifically a RISC-V 64 ADDI translation function, and it uses QEMU’s architecture-agnostic TCG mechanism rather than directly emitting host machine code. [1]

CITATIONS

4 sources
4 citations
[1] trans_addi is generated C code for a QEMU TCG translation function for RISC-V 64 ADDI in an OpenVADL-to-QEMU generation flow. Generation of a QEMU-Based Instruction Set Simulator from a Processor Description in OpenVADL
[2] The source ADDI semantics are X(rd) := X(rs1) + immS, with immS derived from the instruction immediate as a signed value. Generation of a QEMU-Based Instruction Set Simulator from a Processor Description in OpenVADL
[3] The generated trans_addi function creates TCG values for the destination register, source register, temporary, and immediate constant, emits tcg_gen_add_i64 and tcg_gen_mov_i64, and returns true. Generation of a QEMU-Based Instruction Set Simulator from a Processor Description in OpenVADL
[4] The generation pipeline lowers VIAM to TCG operations and then emits C code for a QEMU frontend; the lowered ADDI representation includes tcg_add and tcg_mov. Generation of a QEMU-Based Instruction Set Simulator from a Processor Description in OpenVADL