diff mbox series

[RFC,v1,1/1] riscv: Add helper_print() for printing intermediate results of IR

Message ID b516765922b56fcfd5a16ceaf46428dbdfe0e41f.1732376265.git.chao.liu@yeah.net (mailing list archive)
State New
Headers show
Series Add helper_print functions for printing intermediate results of complex instructions in RISC-V target | expand

Commit Message

Chao Liu Nov. 23, 2024, 5:23 p.m. UTC
Signed-off-by: Chao Liu <chao.liu@yeah.net>
---
 target/riscv/helper.h    | 13 ++++++++++++
 target/riscv/op_helper.c | 46 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

Comments

Chao Liu Nov. 23, 2024, 6:35 p.m. UTC | #1
For example:

    static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop)
    {
        TCGv src1;

        decode_save_opc(ctx, 0);
        src1 = get_address(ctx, a->rs1, 0);
        gen_helper_print("src1 %x\n", src1);
        ...
        return true;
    }

When the TCG executes instructions containing this function, the terminal will print:

    src1 0x...

However, currently the parameter type for `helper_print` is uniformly set to `tl`,
so when passing parameters of other types, they need to be cast to the `TCGv` type.
Chao Liu Nov. 23, 2024, 7:08 p.m. UTC | #2
Currently, the v1-patch is not yet capable of achieving the desired effect.
It still requires passing the number of variable arguments to the helper_print()
function:

     static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop)
     {
         TCGv src1;
 
         decode_save_opc(ctx, 0);
         src1 = get_address(ctx, a->rs1, 0);
         gen_helper_print(2, "src1 %x\n", src1);
         ...
         return true;
     }

Regards,
Chao
diff mbox series

Patch

diff --git a/target/riscv/helper.h b/target/riscv/helper.h
index 451261ce5a..667da16988 100644
--- a/target/riscv/helper.h
+++ b/target/riscv/helper.h
@@ -1281,3 +1281,16 @@  DEF_HELPER_4(vgmul_vv, void, ptr, ptr, env, i32)
 DEF_HELPER_5(vsm4k_vi, void, ptr, ptr, i32, env, i32)
 DEF_HELPER_4(vsm4r_vv, void, ptr, ptr, env, i32)
 DEF_HELPER_4(vsm4r_vs, void, ptr, ptr, env, i32)
+
+DEF_HELPER_1(print1, void, ptr)
+DEF_HELPER_2(print2, void, ptr, tl)
+DEF_HELPER_3(print3, void, ptr, tl, tl)
+DEF_HELPER_4(print4, void, ptr, tl, tl, tl)
+DEF_HELPER_5(print5, void, ptr, tl, tl, tl, tl)
+DEF_HELPER_6(print6, void, ptr, tl, tl, tl, tl, tl)
+DEF_HELPER_7(print7, void, ptr, tl, tl, tl, tl, tl, tl)
+
+#define gen_helper_print(num, fmt, ...) do { \
+    assert(num >= 1 && num <= 7); \
+    gen_helper_print##num(tcg_constant_ptr((uintptr_t)fmt), __VA_ARGS__); \
+} while(0)
diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c
index eddedacf4b..f45e1b9a50 100644
--- a/target/riscv/op_helper.c
+++ b/target/riscv/op_helper.c
@@ -564,3 +564,49 @@  target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr)
 }
 
 #endif /* !CONFIG_USER_ONLY */
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-security"
+
+void helper_print1(void *fmt)
+{
+    printf((const char *)fmt);
+}
+
+void helper_print2(void *fmt, target_ulong arg2)
+{
+   printf((const char *)fmt, arg2);
+}
+
+void helper_print3(void *fmt, target_ulong arg2, target_ulong arg3)
+{
+     printf((const char *)fmt, arg2, arg3);
+}
+
+void helper_print4(void *fmt, target_ulong arg2, target_ulong arg3,
+                   target_ulong arg4)
+{
+    printf((const char *)fmt, arg2, arg3, arg4);
+}
+
+void helper_print5(void *fmt, target_ulong arg2, target_ulong arg3,
+                   target_ulong arg4, target_ulong arg5)
+{
+    printf((const char *)fmt, arg2, arg3, arg4, arg5);
+}
+
+void helper_print6(void *fmt, target_ulong arg2, target_ulong arg3,
+                   target_ulong arg4, target_ulong arg5, target_ulong arg6)
+{
+    printf((const char *)fmt, arg2, arg3, arg4, arg5, arg6);
+}
+
+void helper_print7(void *fmt, target_ulong arg2, target_ulong arg3,
+                   target_ulong arg4, target_ulong arg5, target_ulong arg6,
+                   target_ulong arg7)
+{
+    printf((const char *)fmt, arg2, arg3, arg4, arg5, arg6, arg7);
+}
+
+#pragma GCC diagnostic pop
+