Skip to contentSkip to conversation

64-bit LoongArch port of QEMU TCG

PATCH Superseded WANG Xuerui 5 participants 80 messages Sep 20, 2021 – Sep 21, 2021 Arm/Amlogic Meson SoC support

v1 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

cover 64-bit LoongArch port of QEMU TCG

Hi all,

This is a port of QEMU TCG to the brand-new CPU architecture LoongArch, introduced by Loongson with their 3A5000 chips. Test suite all passed except one timeout that is test-crypto-tlssession, but this particular case runs well when relatively few targets are enabled, so it may be just a case of low performance (4C4T 2.5GHz). I also boot-tested x86_64 (Debian and Gentoo installation CDs) and aarch64 (Debian netboot installer), and ran riscv64 linux-user emulation with a chroot; everything seems fine so far.

## About the series

Only the LP64 ABI is supported, as this is the only one fully implemented and supported by Loongson. 32-bit support is incomplete from outset, and removed from the very latest upstream submissions, so you can't even configure for that.

The architecture's documentation is already translated into English; it can be browsed at https://loongson.github.io/LoongArch-Documentation/.

In this series I made use of generated instruction encodings and emitters from https://github.com/loongson-community/loongarch-opcodes
(a community project started by myself, something I must admit), as the LoongArch encoding is highly irregular even for a fixed 32-bit ISA, and I want to minimize the maintenance burden for future collaboration. This is something not seen in any of the other TCG ports out there, so I'd like to see if this is acceptable practice (and also maybe bikeshed the file name).

Also, I'm not quite familiar with the DWARF spec, so maybe the debug frame information is not supplied correctly; beware (though all my tests run fine).

This series touches some of the same files as Song Gao's previous submission of LoongArch *target* support, which is a bit unfortunate; one of us will have to rebase after either series gets in. Actual conflict should only happen on build system bits and include/elf.h, though, as we're working on entirely different areas.

## How to build and test this

Upstream support for LoongArch is largely WIP for now, which means you must apply a lot of patches if you want to even cross-build for this arch. The main sources I used are as follows:

* binutils: https://github.com/xen0n/binutils-gdb/tree/for-gentoo-2.37-v2
based on https://github.com/loongson/binutils-gdb/tree/loongarch/upstream_v6_a1d65b3
* gcc: https://github.com/xen0n/gcc/tree/for-gentoo-gcc-12-v2
based on https://github.com/loongson/gcc/tree/loongarch_upstream
* glibc: https://github.com/xen0n/glibc/tree/for-gentoo-glibc-2.34
based on https://github.com/loongson/glibc/tree/loongarch_2_34_for_upstream
* Linux: https://github.com/xen0n/linux/tree/loongarch-playground
based on https://github.com/loongson/linux/tree/loongarch-next
* Gentoo overlay: https://github.com/xen0n/loongson-overlay

I have made ready-to-use Gentoo stage3 tarballs, but they're served with CDN off my personal cloud account, and I don't want the link to be exposed so that my bills skyrocket; you can reach me off-list to get the links if you're interested.

As for the hardware availability, the boards can already be bought in China on Taobao, and I think some people at Loongson might be able to arrange for testing environments, if testing on real hardware other than mine is required before merging; they have their in-house Debian spin-off from the early days of this architecture. Their kernel is ABI-incompatible with the version being upstreamed and used by me, but QEMU should work there regardless.

Lastly, I'm new to QEMU development and this is my first patch series here; apologizes if I get anything wrong, and any help or suggestion is certainly appreciated!

WANG Xuerui (30):
elf: Add machine type value for LoongArch
MAINTAINERS: Add tcg/loongarch entry with myself as maintainer tcg/loongarch: Add the tcg-target.h file
tcg/loongarch: Add generated instruction opcodes and encoding helpers tcg/loongarch: Add register names, allocation order and input/output
sets
tcg/loongarch: Define the operand constraints tcg/loongarch: Implement necessary relocation operations tcg/loongarch: Implement the memory barrier op tcg/loongarch: Implement tcg_out_mov and tcg_out_movi tcg/loongarch: Implement goto_ptr
tcg/loongarch: Implement sign-/zero-extension ops tcg/loongarch: Implement not/and/or/xor/nor/andc/orc ops tcg/loongarch: Implement deposit/extract ops tcg/loongarch: Implement bswap32_i32/bswap64_i64 tcg/loongarch: Implement clz/ctz ops
tcg/loongarch: Implement shl/shr/sar/rotl/rotr ops tcg/loongarch: Implement neg/add/sub ops
tcg/loongarch: Implement mul/mulsh/muluh/div/divu/rem/remu ops tcg/loongarch: Implement br/brcond ops
tcg/loongarch: Implement setcond ops
tcg/loongarch: Implement tcg_out_call
tcg/loongarch: Implement simple load/store ops tcg/loongarch: Add softmmu load/store helpers, implement
qemu_ld/qemu_st ops
tcg/loongarch: Implement tcg_target_qemu_prologue tcg/loongarch: Implement exit_tb/goto_tb
tcg/loongarch: Implement tcg_target_init
tcg/loongarch: Register the JIT
configure, meson.build: Mark support for 64-bit LoongArch hosts
linux-user: Add host dependency for 64-bit LoongArch accel/tcg/user-exec: Implement CPU-specific signal handler for
LoongArch hosts

 MAINTAINERS                           |    5 +
 accel/tcg/user-exec.c                 |   83 ++
 configure                             |    4 +-
 include/elf.h                         |    2 +
 linux-user/host/loongarch64/hostdep.h |   11 +
 meson.build                           |    4 +-
 tcg/loongarch/tcg-insn-defs.c.inc     | 1080 +++++++++++++++++
 tcg/loongarch/tcg-target-con-set.h    |   30 +
 tcg/loongarch/tcg-target-con-str.h    |   26 +
 tcg/loongarch/tcg-target.c.inc        | 1561 +++++++++++++++++++++++++
 tcg/loongarch/tcg-target.h            |  183 +++
 11 files changed, 2987 insertions(+), 2 deletions(-)
 create mode 100644 linux-user/host/loongarch64/hostdep.h
 create mode 100644 tcg/loongarch/tcg-insn-defs.c.inc
 create mode 100644 tcg/loongarch/tcg-target-con-set.h
 create mode 100644 tcg/loongarch/tcg-target-con-str.h
 create mode 100644 tcg/loongarch/tcg-target.c.inc
 create mode 100644 tcg/loongarch/tcg-target.h
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/30 elf: Add machine type value for LoongArch

---
 include/elf.h | 2 ++
 1 file changed, 2 insertions(+)
include/elf.h +2 -0
index 811bf4a1cb..3a4bcb646a 100644
@@ -182,6 +182,8 @@ typedef struct mips_elf_abiflags_v0 {
 
 #define EM_NANOMIPS     249     /* Wave Computing nanoMIPS */
 
+#define EM_LOONGARCH    258     /* LoongArch */
+
 /*
  * This is an interim value that we will use until the committee comes
  * up with a final number.
Signature · 1 line
2.33.0
#2 lore raw

PATCH 2/30 MAINTAINERS: Add tcg/loongarch entry with myself as maintainer

In reply to WANG Xuerui

I wrote the initial code, so I should maintain it of course.

---
 MAINTAINERS | 5 +++++
 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
index 6c20634d63..0e9942cc00 100644
@@ -3109,6 +3109,11 @@ S: Maintained
 F: tcg/i386/
 F: disas/i386.c
 
+LoongArch TCG target
+M: WANG Xuerui <[email protected]>
+S: Maintained
+F: tcg/loongarch/
+
 MIPS TCG target
 M: Philippe Mathieu-Daudé <[email protected]>
 R: Aurelien Jarno <[email protected]>
Signature · 1 line
2.33.0
#4 lore raw

PATCH 3/30 tcg/loongarch: Add the tcg-target.h file

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.h | 183 +++++++++++++++++++++++++++++++++++++
 1 file changed, 183 insertions(+)
 create mode 100644 tcg/loongarch/tcg-target.h
tcg/loongarch/tcg-target.h new file +183 -0
new file mode 100644
index 0000000000..b5e70e01b5
@@ -0,0 +1,183 @@
+/*
+ * Tiny Code Generator for QEMU
+ *
+ * Copyright (c) 2021 WANG Xuerui <[email protected]>
+ *
+ * Based on tcg/riscv/tcg-target.h
+ *
+ * Copyright (c) 2018 SiFive, Inc
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef LOONGARCH_TCG_TARGET_H
+#define LOONGARCH_TCG_TARGET_H
+
+/*
+ * Loongson removed the (incomplete) 32-bit support from kernel and toolchain
+ * for the initial upstreaming of this architecture, so don't bother and just
+ * support the LP64 ABI for now.
+ */
+#if defined(__loongarch64)
+# define TCG_TARGET_REG_BITS 64
+#else
+# error unsupported LoongArch bitness
+#endif
+
+#define TCG_TARGET_INSN_UNIT_SIZE 4
+#define TCG_TARGET_TLB_DISPLACEMENT_BITS 20
+#define TCG_TARGET_NB_REGS 32
+#define MAX_CODE_GEN_BUFFER_SIZE  ((size_t)-1)
+
+typedef enum {
+    TCG_REG_ZERO,
+    TCG_REG_RA,
+    TCG_REG_TP,
+    TCG_REG_SP,
+    TCG_REG_A0,
+    TCG_REG_A1,
+    TCG_REG_A2,
+    TCG_REG_A3,
+    TCG_REG_A4,
+    TCG_REG_A5,
+    TCG_REG_A6,
+    TCG_REG_A7,
+    TCG_REG_T0,
+    TCG_REG_T1,
+    TCG_REG_T2,
+    TCG_REG_T3,
+    TCG_REG_T4,
+    TCG_REG_T5,
+    TCG_REG_T6,
+    TCG_REG_T7,
+    TCG_REG_T8,
+    TCG_REG_RESERVED,
+    TCG_REG_S9,
+    TCG_REG_S0,
+    TCG_REG_S1,
+    TCG_REG_S2,
+    TCG_REG_S3,
+    TCG_REG_S4,
+    TCG_REG_S5,
+    TCG_REG_S6,
+    TCG_REG_S7,
+    TCG_REG_S8,
+
+    /* aliases */
+    TCG_AREG0          = TCG_REG_S0,
+    TCG_GUEST_BASE_REG = TCG_REG_S1,
+    TCG_REG_TMP0       = TCG_REG_T8,
+    TCG_REG_TMP1       = TCG_REG_T7,
+    TCG_REG_TMP2       = TCG_REG_T6,
+} TCGReg;
+
+/* used for function call generation */
+#define TCG_REG_CALL_STACK              TCG_REG_SP
+#define TCG_TARGET_STACK_ALIGN          16
+#define TCG_TARGET_CALL_ALIGN_ARGS      1
+#define TCG_TARGET_CALL_STACK_OFFSET    0
+
+/* optional instructions */
+#define TCG_TARGET_HAS_movcond_i32      0
+#define TCG_TARGET_HAS_div_i32          1
+#define TCG_TARGET_HAS_rem_i32          1
+#define TCG_TARGET_HAS_div2_i32         0
+#define TCG_TARGET_HAS_rot_i32          1
+#define TCG_TARGET_HAS_deposit_i32      1
+#define TCG_TARGET_HAS_extract_i32      1
+#define TCG_TARGET_HAS_sextract_i32     0
+#define TCG_TARGET_HAS_extract2_i32     0
+#define TCG_TARGET_HAS_add2_i32         0
+#define TCG_TARGET_HAS_sub2_i32         0
+#define TCG_TARGET_HAS_mulu2_i32        0
+#define TCG_TARGET_HAS_muls2_i32        0
+#define TCG_TARGET_HAS_muluh_i32        1
+#define TCG_TARGET_HAS_mulsh_i32        1
+#define TCG_TARGET_HAS_ext8s_i32        1
+#define TCG_TARGET_HAS_ext16s_i32       1
+#define TCG_TARGET_HAS_ext8u_i32        1
+#define TCG_TARGET_HAS_ext16u_i32       1
+#define TCG_TARGET_HAS_bswap16_i32      0
+#define TCG_TARGET_HAS_bswap32_i32      1
+#define TCG_TARGET_HAS_not_i32          1
+#define TCG_TARGET_HAS_neg_i32          1
+#define TCG_TARGET_HAS_andc_i32         1
+#define TCG_TARGET_HAS_orc_i32          1
+#define TCG_TARGET_HAS_eqv_i32          0
+#define TCG_TARGET_HAS_nand_i32         0
+#define TCG_TARGET_HAS_nor_i32          1
+#define TCG_TARGET_HAS_clz_i32          1
+#define TCG_TARGET_HAS_ctz_i32          1
+#define TCG_TARGET_HAS_ctpop_i32        0
+#define TCG_TARGET_HAS_direct_jump      0
+#define TCG_TARGET_HAS_brcond2          0
+#define TCG_TARGET_HAS_setcond2         0
+#define TCG_TARGET_HAS_qemu_st8_i32     0
+
+#if TCG_TARGET_REG_BITS == 64
+#define TCG_TARGET_HAS_movcond_i64      0
+#define TCG_TARGET_HAS_div_i64          1
+#define TCG_TARGET_HAS_rem_i64          1
+#define TCG_TARGET_HAS_div2_i64         0
+#define TCG_TARGET_HAS_rot_i64          1
+#define TCG_TARGET_HAS_deposit_i64      1
+#define TCG_TARGET_HAS_extract_i64      1
+#define TCG_TARGET_HAS_sextract_i64     0
+#define TCG_TARGET_HAS_extract2_i64     0
+#define TCG_TARGET_HAS_extrl_i64_i32    1
+#define TCG_TARGET_HAS_extrh_i64_i32    1
+#define TCG_TARGET_HAS_ext8s_i64        1
+#define TCG_TARGET_HAS_ext16s_i64       1
+#define TCG_TARGET_HAS_ext32s_i64       1
+#define TCG_TARGET_HAS_ext8u_i64        1
+#define TCG_TARGET_HAS_ext16u_i64       1
+#define TCG_TARGET_HAS_ext32u_i64       1
+#define TCG_TARGET_HAS_bswap16_i64      0
+#define TCG_TARGET_HAS_bswap32_i64      0
+#define TCG_TARGET_HAS_bswap64_i64      1
+#define TCG_TARGET_HAS_not_i64          1
+#define TCG_TARGET_HAS_neg_i64          1
+#define TCG_TARGET_HAS_andc_i64         1
+#define TCG_TARGET_HAS_orc_i64          1
+#define TCG_TARGET_HAS_eqv_i64          0
+#define TCG_TARGET_HAS_nand_i64         0
+#define TCG_TARGET_HAS_nor_i64          1
+#define TCG_TARGET_HAS_clz_i64          1
+#define TCG_TARGET_HAS_ctz_i64          1
+#define TCG_TARGET_HAS_ctpop_i64        0
+#define TCG_TARGET_HAS_add2_i64         0
+#define TCG_TARGET_HAS_sub2_i64         0
+#define TCG_TARGET_HAS_mulu2_i64        0
+#define TCG_TARGET_HAS_muls2_i64        0
+#define TCG_TARGET_HAS_muluh_i64        1
+#define TCG_TARGET_HAS_mulsh_i64        1
+#endif
+
+/* not defined -- call should be eliminated at compile time */
+void tb_target_set_jmp_target(uintptr_t, uintptr_t, uintptr_t, uintptr_t);
+
+#define TCG_TARGET_DEFAULT_MO (0)
+
+#ifdef CONFIG_SOFTMMU
+#define TCG_TARGET_NEED_LDST_LABELS
+#endif
+
+#define TCG_TARGET_HAS_MEMORY_BSWAP 0
+
+#endif /* LOONGARCH_TCG_TARGET_H */
Signature · 1 line
2.33.0
#5 lore raw

Re: [PATCH 03/30] tcg/loongarch: Add the tcg-target.h file

Quoted 12 lines · WANG Xuerui
---
  tcg/loongarch/tcg-target.h | 183 +++++++++++++++++++++++++++++++++++++
  1 file changed, 183 insertions(+)

create mode 100644 tcg/loongarch/tcg-target.h

tcg/loongarch/tcg-target.h new file +0 -0
new file mode 100644
index 0000000000..b5e70e01b5

tcg/loongarch/tcg-target.h line 8

Quoted 8 lines
tcg/loongarch/tcg-target.h new file +8 -0
@@ -0,0 +1,183 @@
+/*
+ * Tiny Code Generator for QEMU
+ *
+ * Copyright (c) 2021 WANG Xuerui <[email protected]>
+ *
+ * Based on tcg/riscv/tcg-target.h
+ *
+ * Copyright (c) 2018 SiFive, Inc

You may have copied too much from the riscv port? :-)

tcg/loongarch/tcg-target.h line 40

Quoted 9 lines
tcg/loongarch/tcg-target.h new file +9 -0
+/*
+ * Loongson removed the (incomplete) 32-bit support from kernel and toolchain
+ * for the initial upstreaming of this architecture, so don't bother and just
+ * support the LP64 ABI for now.
+ */
+#if defined(__loongarch64)
+# define TCG_TARGET_REG_BITS 64
+#else
+# error unsupported LoongArch bitness

s/bitness/register size/

tcg/loongarch/tcg-target.h line 44

Quoted 1 line
tcg/loongarch/tcg-target.h new file +1 -0
+#define TCG_TARGET_TLB_DISPLACEMENT_BITS 20

Hmm. I was about to say this is more copying from riscv, and should be X, but now I see that this is no longer used. You can omit it now; I'll remove the other instances myself.

Quoted 28 lines
tcg/loongarch/tcg-target.h new file +28 -0
+/* optional instructions */
+#define TCG_TARGET_HAS_movcond_i32      0
+#define TCG_TARGET_HAS_div_i32          1
+#define TCG_TARGET_HAS_rem_i32          1
+#define TCG_TARGET_HAS_div2_i32         0
+#define TCG_TARGET_HAS_rot_i32          1
+#define TCG_TARGET_HAS_deposit_i32      1
+#define TCG_TARGET_HAS_extract_i32      1
+#define TCG_TARGET_HAS_sextract_i32     0
+#define TCG_TARGET_HAS_extract2_i32     0
+#define TCG_TARGET_HAS_add2_i32         0
+#define TCG_TARGET_HAS_sub2_i32         0
+#define TCG_TARGET_HAS_mulu2_i32        0
+#define TCG_TARGET_HAS_muls2_i32        0
+#define TCG_TARGET_HAS_muluh_i32        1
+#define TCG_TARGET_HAS_mulsh_i32        1
+#define TCG_TARGET_HAS_ext8s_i32        1
+#define TCG_TARGET_HAS_ext16s_i32       1
+#define TCG_TARGET_HAS_ext8u_i32        1
+#define TCG_TARGET_HAS_ext16u_i32       1
+#define TCG_TARGET_HAS_bswap16_i32      0
+#define TCG_TARGET_HAS_bswap32_i32      1
+#define TCG_TARGET_HAS_not_i32          1
+#define TCG_TARGET_HAS_neg_i32          1
+#define TCG_TARGET_HAS_andc_i32         1
+#define TCG_TARGET_HAS_orc_i32          1
+#define TCG_TARGET_HAS_eqv_i32          0
+#define TCG_TARGET_HAS_nand_i32         0

tcg/loongarch/tcg-target.h line 133

Quoted 10 lines
tcg/loongarch/tcg-target.h new file +10 -0
+#define TCG_TARGET_HAS_nor_i32          1
+#define TCG_TARGET_HAS_clz_i32          1
+#define TCG_TARGET_HAS_ctz_i32          1
+#define TCG_TARGET_HAS_ctpop_i32        0
+#define TCG_TARGET_HAS_direct_jump      0
+#define TCG_TARGET_HAS_brcond2          0
+#define TCG_TARGET_HAS_setcond2         0
+#define TCG_TARGET_HAS_qemu_st8_i32     0
+
+#if TCG_TARGET_REG_BITS == 64

You don't need this conditional, since you've asserted it at the top (and unlike riscv, have no plans to add support for riscv32 at some future point).

#6 lore raw

Re: [PATCH 03/30] tcg/loongarch: Add the tcg-target.h file

Hi Richard,

Quoted 7 lines · Richard Henderson
Quoted 6 lines · WANG Xuerui

---
  tcg/loongarch/tcg-target.h | 183 +++++++++++++++++++++++++++++++++++++

  1 file changed, 183 insertions(+)

  create mode 100644 tcg/loongarch/tcg-target.h

Quoted 16 lines
Quoted 6 lines
tcg/loongarch/tcg-target.h new file +0 -0
new file mode 100644
index 0000000000..b5e70e01b5

tcg/loongarch/tcg-target.h line 8

Quoted 8 lines
tcg/loongarch/tcg-target.h new file +8 -0
@@ -0,0 +1,183 @@
+/*
+ * Tiny Code Generator for QEMU
+ *
+ * Copyright (c) 2021 WANG Xuerui <[email protected]>
+ *
+ * Based on tcg/riscv/tcg-target.h
+ *
+ * Copyright (c) 2018 SiFive, Inc

You may have copied too much from the riscv port?  :-)

First of all, thanks for the *extremely* quick review!

As for the copying, I admit that I thought the riscv port generally was doing things the recent and preferred way, so most of the logic are only lightly touched. However the LoongArch is substantially similar to riscv too, so much of the traits expressed here would be the same regardless.

But in such a case of outstanding similarity, should I just drop my "copyright" line? I'm actually okay with dropping if that's the best thing to do.

Quoted 14 lines

tcg/loongarch/tcg-target.h line 40

Quoted 12 lines
tcg/loongarch/tcg-target.h new file +2 -0
+/*
+ * Loongson removed the (incomplete) 32-bit support from kernel and 

toolchain
+ * for the initial upstreaming of this architecture, so don't bother and just
+ * support the LP64 ABI for now.
+ */

tcg/loongarch/tcg-target.h new file +4 -0
+#if defined(__loongarch64)
+# define TCG_TARGET_REG_BITS 64
+#else
+# error unsupported LoongArch bitness

s/bitness/register size/

Sure; will fix in v2.

Quoted 5 lines
Quoted 3 lines

+#define TCG_TARGET_TLB_DISPLACEMENT_BITS 20

Hmm.  I was about to say this is more copying from riscv, and should be X, but now I see that this is no longer used.  You can omit it now; I'll remove the other instances myself.

Thanks for the explanation, I'm only into qemu internals for 2 weeks and that's something I haven't read about yet! I'll try to remove irrelevant parts like this in v2.

Quoted 33 lines
Quoted 33 lines
tcg/loongarch/tcg-target.h new file +32 -0
+/* optional instructions */
+#define TCG_TARGET_HAS_movcond_i32      0
+#define TCG_TARGET_HAS_div_i32          1
+#define TCG_TARGET_HAS_rem_i32          1
+#define TCG_TARGET_HAS_div2_i32         0
+#define TCG_TARGET_HAS_rot_i32          1
+#define TCG_TARGET_HAS_deposit_i32      1
+#define TCG_TARGET_HAS_extract_i32      1
+#define TCG_TARGET_HAS_sextract_i32     0
+#define TCG_TARGET_HAS_extract2_i32     0
+#define TCG_TARGET_HAS_add2_i32         0
+#define TCG_TARGET_HAS_sub2_i32         0
+#define TCG_TARGET_HAS_mulu2_i32        0
+#define TCG_TARGET_HAS_muls2_i32        0
+#define TCG_TARGET_HAS_muluh_i32        1
+#define TCG_TARGET_HAS_mulsh_i32        1
+#define TCG_TARGET_HAS_ext8s_i32        1
+#define TCG_TARGET_HAS_ext16s_i32       1
+#define TCG_TARGET_HAS_ext8u_i32        1
+#define TCG_TARGET_HAS_ext16u_i32       1
+#define TCG_TARGET_HAS_bswap16_i32      0
+#define TCG_TARGET_HAS_bswap32_i32      1
+#define TCG_TARGET_HAS_not_i32          1
+#define TCG_TARGET_HAS_neg_i32          1
+#define TCG_TARGET_HAS_andc_i32         1
+#define TCG_TARGET_HAS_orc_i32          1
+#define TCG_TARGET_HAS_eqv_i32          0
+#define TCG_TARGET_HAS_nand_i32         0
+#define TCG_TARGET_HAS_nor_i32          1
+#define TCG_TARGET_HAS_clz_i32          1
+#define TCG_TARGET_HAS_ctz_i32          1
+#define TCG_TARGET_HAS_ctpop_i32        0
Quoted 8 lines

tcg/loongarch/tcg-target.h line 133

Quoted 6 lines
tcg/loongarch/tcg-target.h new file +6 -0
+#define TCG_TARGET_HAS_direct_jump      0
+#define TCG_TARGET_HAS_brcond2          0
+#define TCG_TARGET_HAS_setcond2         0
+#define TCG_TARGET_HAS_qemu_st8_i32     0
+
+#if TCG_TARGET_REG_BITS == 64

You don't need this conditional, since you've asserted it at the top (and unlike riscv, have no plans to add support for riscv32 at some future point).

OK, will remove all such conditionals in v2 too.

#7 lore raw

Re: [PATCH 03/30] tcg/loongarch: Add the tcg-target.h file

Quoted 9 lines · WANG Xuerui
Quoted 3 lines
Quoted 1 line

+ * Copyright (c) 2018 SiFive, Inc

You may have copied too much from the riscv port?  :-)

First of all, thanks for the *extremely* quick review!

As for the copying, I admit that I thought the riscv port generally was doing things the recent and preferred way, so most of the logic are only lightly touched. However the LoongArch is substantially similar to riscv too, so much of the traits expressed here would be the same regardless.

But in such a case of outstanding similarity, should I just drop my "copyright" line? I'm actually okay with dropping if that's the best thing to do.

Yes, your own copyright is the correct thing in this case.

r~

#8 lore raw

PATCH 4/30 tcg/loongarch: Add generated instruction opcodes and encoding helpers

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-insn-defs.c.inc | 1080 +++++++++++++++++++++++++++++
 1 file changed, 1080 insertions(+)
 create mode 100644 tcg/loongarch/tcg-insn-defs.c.inc
tcg/loongarch/tcg-insn-defs.c.inc new file +1080 -0
new file mode 100644
index 0000000000..413f7ffc12
@@ -0,0 +1,1080 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * LoongArch instruction formats, opcodes, and encoders for TCG use.
+ *
+ * Code generated by genqemutcgdefs from
+ * https://github.com/loongson-community/loongarch-opcodes,
+ * from commit bb5234081663faaefb6b921a7848b18e19519890.
+ * DO NOT EDIT.
+ */
+
+typedef enum {
+    OPC_CLZ_W = 0x00001400,
+    OPC_CTZ_W = 0x00001c00,
+    OPC_CLZ_D = 0x00002400,
+    OPC_CTZ_D = 0x00002c00,
+    OPC_REVB_2H = 0x00003000,
+    OPC_REVB_D = 0x00003c00,
+    OPC_SEXT_H = 0x00005800,
+    OPC_SEXT_B = 0x00005c00,
+    OPC_ADD_W = 0x00100000,
+    OPC_ADD_D = 0x00108000,
+    OPC_SUB_W = 0x00110000,
+    OPC_SUB_D = 0x00118000,
+    OPC_SLT = 0x00120000,
+    OPC_SLTU = 0x00128000,
+    OPC_MASKEQZ = 0x00130000,
+    OPC_MASKNEZ = 0x00138000,
+    OPC_NOR = 0x00140000,
+    OPC_AND = 0x00148000,
+    OPC_OR = 0x00150000,
+    OPC_XOR = 0x00158000,
+    OPC_ORN = 0x00160000,
+    OPC_ANDN = 0x00168000,
+    OPC_SLL_W = 0x00170000,
+    OPC_SRL_W = 0x00178000,
+    OPC_SRA_W = 0x00180000,
+    OPC_SLL_D = 0x00188000,
+    OPC_SRL_D = 0x00190000,
+    OPC_SRA_D = 0x00198000,
+    OPC_ROTR_W = 0x001b0000,
+    OPC_ROTR_D = 0x001b8000,
+    OPC_MUL_W = 0x001c0000,
+    OPC_MULH_W = 0x001c8000,
+    OPC_MULH_WU = 0x001d0000,
+    OPC_MUL_D = 0x001d8000,
+    OPC_MULH_D = 0x001e0000,
+    OPC_MULH_DU = 0x001e8000,
+    OPC_DIV_W = 0x00200000,
+    OPC_MOD_W = 0x00208000,
+    OPC_DIV_WU = 0x00210000,
+    OPC_MOD_WU = 0x00218000,
+    OPC_DIV_D = 0x00220000,
+    OPC_MOD_D = 0x00228000,
+    OPC_DIV_DU = 0x00230000,
+    OPC_MOD_DU = 0x00238000,
+    OPC_SLLI_W = 0x00408000,
+    OPC_SLLI_D = 0x00410000,
+    OPC_SRLI_W = 0x00448000,
+    OPC_SRLI_D = 0x00450000,
+    OPC_SRAI_W = 0x00488000,
+    OPC_SRAI_D = 0x00490000,
+    OPC_ROTRI_W = 0x004c8000,
+    OPC_ROTRI_D = 0x004d0000,
+    OPC_BSTRINS_W = 0x00600000,
+    OPC_BSTRPICK_W = 0x00608000,
+    OPC_BSTRINS_D = 0x00800000,
+    OPC_BSTRPICK_D = 0x00c00000,
+    OPC_SLTI = 0x02000000,
+    OPC_SLTUI = 0x02400000,
+    OPC_ADDI_W = 0x02800000,
+    OPC_ADDI_D = 0x02c00000,
+    OPC_CU52I_D = 0x03000000,
+    OPC_ANDI = 0x03400000,
+    OPC_ORI = 0x03800000,
+    OPC_XORI = 0x03c00000,
+    OPC_LU12I_W = 0x14000000,
+    OPC_CU32I_D = 0x16000000,
+    OPC_PCADDU12I = 0x1c000000,
+    OPC_LD_B = 0x28000000,
+    OPC_LD_H = 0x28400000,
+    OPC_LD_W = 0x28800000,
+    OPC_LD_D = 0x28c00000,
+    OPC_ST_B = 0x29000000,
+    OPC_ST_H = 0x29400000,
+    OPC_ST_W = 0x29800000,
+    OPC_ST_D = 0x29c00000,
+    OPC_LD_BU = 0x2a000000,
+    OPC_LD_HU = 0x2a400000,
+    OPC_LD_WU = 0x2a800000,
+    OPC_DBAR = 0x38720000,
+    OPC_JIRL = 0x4c000000,
+    OPC_B = 0x50000000,
+    OPC_BL = 0x54000000,
+    OPC_BEQ = 0x58000000,
+    OPC_BNE = 0x5c000000,
+    OPC_BGT = 0x60000000,
+    OPC_BLE = 0x64000000,
+    OPC_BGTU = 0x68000000,
+    OPC_BLEU = 0x6c000000,
+} LoongArchInsn;
+
+static int32_t encode_d_slot(LoongArchInsn opc, uint32_t d)
+    __attribute__((unused));
+
+static int32_t encode_d_slot(LoongArchInsn opc, uint32_t d)
+{
+    return opc | d;
+}
+
+static int32_t encode_dj_slots(LoongArchInsn opc, uint32_t d, uint32_t j)
+    __attribute__((unused));
+
+static int32_t encode_dj_slots(LoongArchInsn opc, uint32_t d, uint32_t j)
+{
+    return opc | d | j << 5;
+}
+
+static int32_t encode_djk_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                uint32_t k) __attribute__((unused));
+
+static int32_t encode_djk_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                uint32_t k)
+{
+    return opc | d | j << 5 | k << 10;
+}
+
+static int32_t encode_djkm_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                 uint32_t k, uint32_t m)
+    __attribute__((unused));
+
+static int32_t encode_djkm_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                 uint32_t k, uint32_t m)
+{
+    return opc | d | j << 5 | k << 10 | m << 16;
+}
+
+static int32_t encode_dk_slots(LoongArchInsn opc, uint32_t d, uint32_t k)
+    __attribute__((unused));
+
+static int32_t encode_dk_slots(LoongArchInsn opc, uint32_t d, uint32_t k)
+{
+    return opc | d | k << 10;
+}
+
+static int32_t encode_dj_insn(LoongArchInsn opc, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static int32_t encode_dj_insn(LoongArchInsn opc, TCGReg d, TCGReg j)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    return encode_dj_slots(opc, d, j);
+}
+
+static int32_t encode_djk_insn(LoongArchInsn opc, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static int32_t encode_djk_insn(LoongArchInsn opc, TCGReg d, TCGReg j, TCGReg k)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    k &= 0x1f;
+    return encode_djk_slots(opc, d, j, k);
+}
+
+static int32_t encode_djsk12_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk12) __attribute__((unused));
+
+static int32_t encode_djsk12_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk12)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    sk12 &= 0xfff;
+    return encode_djk_slots(opc, d, j, sk12);
+}
+
+static int32_t encode_djsk16_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk16) __attribute__((unused));
+
+static int32_t encode_djsk16_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk16)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    sk16 &= 0xffff;
+    return encode_djk_slots(opc, d, j, sk16);
+}
+
+static int32_t encode_djuk12_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  uint32_t uk12) __attribute__((unused));
+
+static int32_t encode_djuk12_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  uint32_t uk12)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    uk12 &= 0xfff;
+    return encode_djk_slots(opc, d, j, uk12);
+}
+
+static int32_t encode_djuk5_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                 uint32_t uk5) __attribute__((unused));
+
+static int32_t encode_djuk5_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                 uint32_t uk5)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    uk5 &= 0x1f;
+    return encode_djk_slots(opc, d, j, uk5);
+}
+
+static int32_t encode_djuk5um5_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                    uint32_t uk5, uint32_t um5)
+    __attribute__((unused));
+
+static int32_t encode_djuk5um5_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                    uint32_t uk5, uint32_t um5)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    uk5 &= 0x1f;
+    um5 &= 0x1f;
+    return encode_djkm_slots(opc, d, j, uk5, um5);
+}
+
+static int32_t encode_djuk6_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                 uint32_t uk6) __attribute__((unused));
+
+static int32_t encode_djuk6_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                 uint32_t uk6)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    uk6 &= 0x3f;
+    return encode_djk_slots(opc, d, j, uk6);
+}
+
+static int32_t encode_djuk6um6_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                    uint32_t uk6, uint32_t um6)
+    __attribute__((unused));
+
+static int32_t encode_djuk6um6_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                    uint32_t uk6, uint32_t um6)
+{
+    d &= 0x1f;
+    j &= 0x1f;
+    uk6 &= 0x3f;
+    um6 &= 0x3f;
+    return encode_djkm_slots(opc, d, j, uk6, um6);
+}
+
+static int32_t encode_dsj20_insn(LoongArchInsn opc, TCGReg d, int32_t sj20)
+    __attribute__((unused));
+
+static int32_t encode_dsj20_insn(LoongArchInsn opc, TCGReg d, int32_t sj20)
+{
+    d &= 0x1f;
+    sj20 &= 0xfffff;
+    return encode_dj_slots(opc, d, sj20);
+}
+
+static int32_t encode_sd10k16_insn(LoongArchInsn opc, int32_t sd10k16)
+    __attribute__((unused));
+
+static int32_t encode_sd10k16_insn(LoongArchInsn opc, int32_t sd10k16)
+{
+    sd10k16 &= 0x3ffffff;
+    return encode_dk_slots(opc, (sd10k16 >> 16) & 0x3ff, sd10k16 & 0xffff);
+}
+
+static int32_t encode_ud15_insn(LoongArchInsn opc, uint32_t ud15)
+    __attribute__((unused));
+
+static int32_t encode_ud15_insn(LoongArchInsn opc, uint32_t ud15)
+{
+    ud15 &= 0x7fff;
+    return encode_d_slot(opc, ud15);
+}
+
+/* Emits the `clz.w d, j` instruction. */
+static void tcg_out_opc_clz_w(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_clz_w(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_CLZ_W, d, j));
+}
+
+/* Emits the `ctz.w d, j` instruction. */
+static void tcg_out_opc_ctz_w(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_ctz_w(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_CTZ_W, d, j));
+}
+
+/* Emits the `clz.d d, j` instruction. */
+static void tcg_out_opc_clz_d(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_clz_d(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_CLZ_D, d, j));
+}
+
+/* Emits the `ctz.d d, j` instruction. */
+static void tcg_out_opc_ctz_d(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_ctz_d(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_CTZ_D, d, j));
+}
+
+/* Emits the `revb.2h d, j` instruction. */
+static void tcg_out_opc_revb_2h(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_revb_2h(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_REVB_2H, d, j));
+}
+
+/* Emits the `revb.d d, j` instruction. */
+static void tcg_out_opc_revb_d(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_revb_d(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_REVB_D, d, j));
+}
+
+/* Emits the `sext.h d, j` instruction. */
+static void tcg_out_opc_sext_h(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_sext_h(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_SEXT_H, d, j));
+}
+
+/* Emits the `sext.b d, j` instruction. */
+static void tcg_out_opc_sext_b(TCGContext *s, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static void tcg_out_opc_sext_b(TCGContext *s, TCGReg d, TCGReg j)
+{
+    tcg_out32(s, encode_dj_insn(OPC_SEXT_B, d, j));
+}
+
+/* Emits the `add.w d, j, k` instruction. */
+static void tcg_out_opc_add_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_add_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_ADD_W, d, j, k));
+}
+
+/* Emits the `add.d d, j, k` instruction. */
+static void tcg_out_opc_add_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_add_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_ADD_D, d, j, k));
+}
+
+/* Emits the `sub.w d, j, k` instruction. */
+static void tcg_out_opc_sub_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_sub_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SUB_W, d, j, k));
+}
+
+/* Emits the `sub.d d, j, k` instruction. */
+static void tcg_out_opc_sub_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_sub_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SUB_D, d, j, k));
+}
+
+/* Emits the `slt d, j, k` instruction. */
+static void tcg_out_opc_slt(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_slt(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SLT, d, j, k));
+}
+
+/* Emits the `sltu d, j, k` instruction. */
+static void tcg_out_opc_sltu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_sltu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SLTU, d, j, k));
+}
+
+/* Emits the `maskeqz d, j, k` instruction. */
+static void tcg_out_opc_maskeqz(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_maskeqz(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MASKEQZ, d, j, k));
+}
+
+/* Emits the `masknez d, j, k` instruction. */
+static void tcg_out_opc_masknez(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_masknez(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MASKNEZ, d, j, k));
+}
+
+/* Emits the `nor d, j, k` instruction. */
+static void tcg_out_opc_nor(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_nor(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_NOR, d, j, k));
+}
+
+/* Emits the `and d, j, k` instruction. */
+static void tcg_out_opc_and(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_and(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_AND, d, j, k));
+}
+
+/* Emits the `or d, j, k` instruction. */
+static void tcg_out_opc_or(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_or(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_OR, d, j, k));
+}
+
+/* Emits the `xor d, j, k` instruction. */
+static void tcg_out_opc_xor(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_xor(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_XOR, d, j, k));
+}
+
+/* Emits the `orn d, j, k` instruction. */
+static void tcg_out_opc_orn(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_orn(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_ORN, d, j, k));
+}
+
+/* Emits the `andn d, j, k` instruction. */
+static void tcg_out_opc_andn(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_andn(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_ANDN, d, j, k));
+}
+
+/* Emits the `sll.w d, j, k` instruction. */
+static void tcg_out_opc_sll_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_sll_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SLL_W, d, j, k));
+}
+
+/* Emits the `srl.w d, j, k` instruction. */
+static void tcg_out_opc_srl_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_srl_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SRL_W, d, j, k));
+}
+
+/* Emits the `sra.w d, j, k` instruction. */
+static void tcg_out_opc_sra_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_sra_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SRA_W, d, j, k));
+}
+
+/* Emits the `sll.d d, j, k` instruction. */
+static void tcg_out_opc_sll_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_sll_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SLL_D, d, j, k));
+}
+
+/* Emits the `srl.d d, j, k` instruction. */
+static void tcg_out_opc_srl_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_srl_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SRL_D, d, j, k));
+}
+
+/* Emits the `sra.d d, j, k` instruction. */
+static void tcg_out_opc_sra_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_sra_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_SRA_D, d, j, k));
+}
+
+/* Emits the `rotr.w d, j, k` instruction. */
+static void tcg_out_opc_rotr_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_rotr_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_ROTR_W, d, j, k));
+}
+
+/* Emits the `rotr.d d, j, k` instruction. */
+static void tcg_out_opc_rotr_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_rotr_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_ROTR_D, d, j, k));
+}
+
+/* Emits the `mul.w d, j, k` instruction. */
+static void tcg_out_opc_mul_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mul_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MUL_W, d, j, k));
+}
+
+/* Emits the `mulh.w d, j, k` instruction. */
+static void tcg_out_opc_mulh_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mulh_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MULH_W, d, j, k));
+}
+
+/* Emits the `mulh.wu d, j, k` instruction. */
+static void tcg_out_opc_mulh_wu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mulh_wu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MULH_WU, d, j, k));
+}
+
+/* Emits the `mul.d d, j, k` instruction. */
+static void tcg_out_opc_mul_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mul_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MUL_D, d, j, k));
+}
+
+/* Emits the `mulh.d d, j, k` instruction. */
+static void tcg_out_opc_mulh_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mulh_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MULH_D, d, j, k));
+}
+
+/* Emits the `mulh.du d, j, k` instruction. */
+static void tcg_out_opc_mulh_du(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mulh_du(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MULH_DU, d, j, k));
+}
+
+/* Emits the `div.w d, j, k` instruction. */
+static void tcg_out_opc_div_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_div_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_DIV_W, d, j, k));
+}
+
+/* Emits the `mod.w d, j, k` instruction. */
+static void tcg_out_opc_mod_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mod_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MOD_W, d, j, k));
+}
+
+/* Emits the `div.wu d, j, k` instruction. */
+static void tcg_out_opc_div_wu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_div_wu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_DIV_WU, d, j, k));
+}
+
+/* Emits the `mod.wu d, j, k` instruction. */
+static void tcg_out_opc_mod_wu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mod_wu(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MOD_WU, d, j, k));
+}
+
+/* Emits the `div.d d, j, k` instruction. */
+static void tcg_out_opc_div_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_div_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_DIV_D, d, j, k));
+}
+
+/* Emits the `mod.d d, j, k` instruction. */
+static void tcg_out_opc_mod_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mod_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MOD_D, d, j, k));
+}
+
+/* Emits the `div.du d, j, k` instruction. */
+static void tcg_out_opc_div_du(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_div_du(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_DIV_DU, d, j, k));
+}
+
+/* Emits the `mod.du d, j, k` instruction. */
+static void tcg_out_opc_mod_du(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static void tcg_out_opc_mod_du(TCGContext *s, TCGReg d, TCGReg j, TCGReg k)
+{
+    tcg_out32(s, encode_djk_insn(OPC_MOD_DU, d, j, k));
+}
+
+/* Emits the `slli.w d, j, uk5` instruction. */
+static void tcg_out_opc_slli_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+    __attribute__((unused));
+
+static void tcg_out_opc_slli_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+{
+    tcg_out32(s, encode_djuk5_insn(OPC_SLLI_W, d, j, uk5));
+}
+
+/* Emits the `slli.d d, j, uk6` instruction. */
+static void tcg_out_opc_slli_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+    __attribute__((unused));
+
+static void tcg_out_opc_slli_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+{
+    tcg_out32(s, encode_djuk6_insn(OPC_SLLI_D, d, j, uk6));
+}
+
+/* Emits the `srli.w d, j, uk5` instruction. */
+static void tcg_out_opc_srli_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+    __attribute__((unused));
+
+static void tcg_out_opc_srli_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+{
+    tcg_out32(s, encode_djuk5_insn(OPC_SRLI_W, d, j, uk5));
+}
+
+/* Emits the `srli.d d, j, uk6` instruction. */
+static void tcg_out_opc_srli_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+    __attribute__((unused));
+
+static void tcg_out_opc_srli_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+{
+    tcg_out32(s, encode_djuk6_insn(OPC_SRLI_D, d, j, uk6));
+}
+
+/* Emits the `srai.w d, j, uk5` instruction. */
+static void tcg_out_opc_srai_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+    __attribute__((unused));
+
+static void tcg_out_opc_srai_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+{
+    tcg_out32(s, encode_djuk5_insn(OPC_SRAI_W, d, j, uk5));
+}
+
+/* Emits the `srai.d d, j, uk6` instruction. */
+static void tcg_out_opc_srai_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+    __attribute__((unused));
+
+static void tcg_out_opc_srai_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+{
+    tcg_out32(s, encode_djuk6_insn(OPC_SRAI_D, d, j, uk6));
+}
+
+/* Emits the `rotri.w d, j, uk5` instruction. */
+static void tcg_out_opc_rotri_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+    __attribute__((unused));
+
+static void tcg_out_opc_rotri_w(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk5)
+{
+    tcg_out32(s, encode_djuk5_insn(OPC_ROTRI_W, d, j, uk5));
+}
+
+/* Emits the `rotri.d d, j, uk6` instruction. */
+static void tcg_out_opc_rotri_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+    __attribute__((unused));
+
+static void tcg_out_opc_rotri_d(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk6)
+{
+    tcg_out32(s, encode_djuk6_insn(OPC_ROTRI_D, d, j, uk6));
+}
+
+/* Emits the `bstrins.w d, j, uk5, um5` instruction. */
+static void tcg_out_opc_bstrins_w(TCGContext *s, TCGReg d, TCGReg j,
+                                  uint32_t uk5, uint32_t um5)
+    __attribute__((unused));
+
+static void tcg_out_opc_bstrins_w(TCGContext *s, TCGReg d, TCGReg j,
+                                  uint32_t uk5, uint32_t um5)
+{
+    tcg_out32(s, encode_djuk5um5_insn(OPC_BSTRINS_W, d, j, uk5, um5));
+}
+
+/* Emits the `bstrpick.w d, j, uk5, um5` instruction. */
+static void tcg_out_opc_bstrpick_w(TCGContext *s, TCGReg d, TCGReg j,
+                                   uint32_t uk5, uint32_t um5)
+    __attribute__((unused));
+
+static void tcg_out_opc_bstrpick_w(TCGContext *s, TCGReg d, TCGReg j,
+                                   uint32_t uk5, uint32_t um5)
+{
+    tcg_out32(s, encode_djuk5um5_insn(OPC_BSTRPICK_W, d, j, uk5, um5));
+}
+
+/* Emits the `bstrins.d d, j, uk6, um6` instruction. */
+static void tcg_out_opc_bstrins_d(TCGContext *s, TCGReg d, TCGReg j,
+                                  uint32_t uk6, uint32_t um6)
+    __attribute__((unused));
+
+static void tcg_out_opc_bstrins_d(TCGContext *s, TCGReg d, TCGReg j,
+                                  uint32_t uk6, uint32_t um6)
+{
+    tcg_out32(s, encode_djuk6um6_insn(OPC_BSTRINS_D, d, j, uk6, um6));
+}
+
+/* Emits the `bstrpick.d d, j, uk6, um6` instruction. */
+static void tcg_out_opc_bstrpick_d(TCGContext *s, TCGReg d, TCGReg j,
+                                   uint32_t uk6, uint32_t um6)
+    __attribute__((unused));
+
+static void tcg_out_opc_bstrpick_d(TCGContext *s, TCGReg d, TCGReg j,
+                                   uint32_t uk6, uint32_t um6)
+{
+    tcg_out32(s, encode_djuk6um6_insn(OPC_BSTRPICK_D, d, j, uk6, um6));
+}
+
+/* Emits the `slti d, j, sk12` instruction. */
+static void tcg_out_opc_slti(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_slti(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_SLTI, d, j, sk12));
+}
+
+/* Emits the `sltui d, j, sk12` instruction. */
+static void tcg_out_opc_sltui(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_sltui(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_SLTUI, d, j, sk12));
+}
+
+/* Emits the `addi.w d, j, sk12` instruction. */
+static void tcg_out_opc_addi_w(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_addi_w(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_ADDI_W, d, j, sk12));
+}
+
+/* Emits the `addi.d d, j, sk12` instruction. */
+static void tcg_out_opc_addi_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_addi_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_ADDI_D, d, j, sk12));
+}
+
+/* Emits the `cu52i.d d, j, sk12` instruction. */
+static void tcg_out_opc_cu52i_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_cu52i_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_CU52I_D, d, j, sk12));
+}
+
+/* Emits the `andi d, j, uk12` instruction. */
+static void tcg_out_opc_andi(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_andi(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk12)
+{
+    tcg_out32(s, encode_djuk12_insn(OPC_ANDI, d, j, uk12));
+}
+
+/* Emits the `ori d, j, uk12` instruction. */
+static void tcg_out_opc_ori(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ori(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk12)
+{
+    tcg_out32(s, encode_djuk12_insn(OPC_ORI, d, j, uk12));
+}
+
+/* Emits the `xori d, j, uk12` instruction. */
+static void tcg_out_opc_xori(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_xori(TCGContext *s, TCGReg d, TCGReg j, uint32_t uk12)
+{
+    tcg_out32(s, encode_djuk12_insn(OPC_XORI, d, j, uk12));
+}
+
+/* Emits the `lu12i.w d, sj20` instruction. */
+static void tcg_out_opc_lu12i_w(TCGContext *s, TCGReg d, int32_t sj20)
+    __attribute__((unused));
+
+static void tcg_out_opc_lu12i_w(TCGContext *s, TCGReg d, int32_t sj20)
+{
+    tcg_out32(s, encode_dsj20_insn(OPC_LU12I_W, d, sj20));
+}
+
+/* Emits the `cu32i.d d, sj20` instruction. */
+static void tcg_out_opc_cu32i_d(TCGContext *s, TCGReg d, int32_t sj20)
+    __attribute__((unused));
+
+static void tcg_out_opc_cu32i_d(TCGContext *s, TCGReg d, int32_t sj20)
+{
+    tcg_out32(s, encode_dsj20_insn(OPC_CU32I_D, d, sj20));
+}
+
+/* Emits the `pcaddu12i d, sj20` instruction. */
+static void tcg_out_opc_pcaddu12i(TCGContext *s, TCGReg d, int32_t sj20)
+    __attribute__((unused));
+
+static void tcg_out_opc_pcaddu12i(TCGContext *s, TCGReg d, int32_t sj20)
+{
+    tcg_out32(s, encode_dsj20_insn(OPC_PCADDU12I, d, sj20));
+}
+
+/* Emits the `ld.b d, j, sk12` instruction. */
+static void tcg_out_opc_ld_b(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ld_b(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_LD_B, d, j, sk12));
+}
+
+/* Emits the `ld.h d, j, sk12` instruction. */
+static void tcg_out_opc_ld_h(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ld_h(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_LD_H, d, j, sk12));
+}
+
+/* Emits the `ld.w d, j, sk12` instruction. */
+static void tcg_out_opc_ld_w(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ld_w(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_LD_W, d, j, sk12));
+}
+
+/* Emits the `ld.d d, j, sk12` instruction. */
+static void tcg_out_opc_ld_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ld_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_LD_D, d, j, sk12));
+}
+
+/* Emits the `st.b d, j, sk12` instruction. */
+static void tcg_out_opc_st_b(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_st_b(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_ST_B, d, j, sk12));
+}
+
+/* Emits the `st.h d, j, sk12` instruction. */
+static void tcg_out_opc_st_h(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_st_h(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_ST_H, d, j, sk12));
+}
+
+/* Emits the `st.w d, j, sk12` instruction. */
+static void tcg_out_opc_st_w(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_st_w(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_ST_W, d, j, sk12));
+}
+
+/* Emits the `st.d d, j, sk12` instruction. */
+static void tcg_out_opc_st_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_st_d(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_ST_D, d, j, sk12));
+}
+
+/* Emits the `ld.bu d, j, sk12` instruction. */
+static void tcg_out_opc_ld_bu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ld_bu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_LD_BU, d, j, sk12));
+}
+
+/* Emits the `ld.hu d, j, sk12` instruction. */
+static void tcg_out_opc_ld_hu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ld_hu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_LD_HU, d, j, sk12));
+}
+
+/* Emits the `ld.wu d, j, sk12` instruction. */
+static void tcg_out_opc_ld_wu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+    __attribute__((unused));
+
+static void tcg_out_opc_ld_wu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk12)
+{
+    tcg_out32(s, encode_djsk12_insn(OPC_LD_WU, d, j, sk12));
+}
+
+/* Emits the `dbar ud15` instruction. */
+static void tcg_out_opc_dbar(TCGContext *s, uint32_t ud15)
+    __attribute__((unused));
+
+static void tcg_out_opc_dbar(TCGContext *s, uint32_t ud15)
+{
+    tcg_out32(s, encode_ud15_insn(OPC_DBAR, ud15));
+}
+
+/* Emits the `jirl d, j, sk16` instruction. */
+static void tcg_out_opc_jirl(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+    __attribute__((unused));
+
+static void tcg_out_opc_jirl(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+{
+    tcg_out32(s, encode_djsk16_insn(OPC_JIRL, d, j, sk16));
+}
+
+/* Emits the `b sd10k16` instruction. */
+static void tcg_out_opc_b(TCGContext *s, int32_t sd10k16)
+    __attribute__((unused));
+
+static void tcg_out_opc_b(TCGContext *s, int32_t sd10k16)
+{
+    tcg_out32(s, encode_sd10k16_insn(OPC_B, sd10k16));
+}
+
+/* Emits the `bl sd10k16` instruction. */
+static void tcg_out_opc_bl(TCGContext *s, int32_t sd10k16)
+    __attribute__((unused));
+
+static void tcg_out_opc_bl(TCGContext *s, int32_t sd10k16)
+{
+    tcg_out32(s, encode_sd10k16_insn(OPC_BL, sd10k16));
+}
+
+/* Emits the `beq d, j, sk16` instruction. */
+static void tcg_out_opc_beq(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+    __attribute__((unused));
+
+static void tcg_out_opc_beq(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+{
+    tcg_out32(s, encode_djsk16_insn(OPC_BEQ, d, j, sk16));
+}
+
+/* Emits the `bne d, j, sk16` instruction. */
+static void tcg_out_opc_bne(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+    __attribute__((unused));
+
+static void tcg_out_opc_bne(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+{
+    tcg_out32(s, encode_djsk16_insn(OPC_BNE, d, j, sk16));
+}
+
+/* Emits the `bgt d, j, sk16` instruction. */
+static void tcg_out_opc_bgt(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+    __attribute__((unused));
+
+static void tcg_out_opc_bgt(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+{
+    tcg_out32(s, encode_djsk16_insn(OPC_BGT, d, j, sk16));
+}
+
+/* Emits the `ble d, j, sk16` instruction. */
+static void tcg_out_opc_ble(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+    __attribute__((unused));
+
+static void tcg_out_opc_ble(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+{
+    tcg_out32(s, encode_djsk16_insn(OPC_BLE, d, j, sk16));
+}
+
+/* Emits the `bgtu d, j, sk16` instruction. */
+static void tcg_out_opc_bgtu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+    __attribute__((unused));
+
+static void tcg_out_opc_bgtu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+{
+    tcg_out32(s, encode_djsk16_insn(OPC_BGTU, d, j, sk16));
+}
+
+/* Emits the `bleu d, j, sk16` instruction. */
+static void tcg_out_opc_bleu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+    __attribute__((unused));
+
+static void tcg_out_opc_bleu(TCGContext *s, TCGReg d, TCGReg j, int32_t sk16)
+{
+    tcg_out32(s, encode_djsk16_insn(OPC_BLEU, d, j, sk16));
+}
Signature · 1 line
2.33.0
Acked-by on v1 4/30
#9 lore raw

Re: [PATCH 04/30] tcg/loongarch: Add generated instruction opcodes and encoding helpers

Quoted 12 lines · WANG Xuerui
---
  tcg/loongarch/tcg-insn-defs.c.inc | 1080 +++++++++++++++++++++++++++++
  1 file changed, 1080 insertions(+)

create mode 100644 tcg/loongarch/tcg-insn-defs.c.inc

tcg/loongarch/tcg-insn-defs.c.inc new file +0 -0
new file mode 100644
index 0000000000..413f7ffc12

tcg/loongarch/tcg-insn-defs.c.inc line 10

Quoted 10 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +10 -0
@@ -0,0 +1,1080 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * LoongArch instruction formats, opcodes, and encoders for TCG use.
+ *
+ * Code generated by genqemutcgdefs from
+ *https://github.com/loongson-community/loongarch-opcodes,
+ * from commit bb5234081663faaefb6b921a7848b18e19519890.
+ * DO NOT EDIT.
+ */
+

tcg/loongarch/tcg-insn-defs.c.inc line 105

Quoted 4 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +4 -0
+static int32_t encode_d_slot(LoongArchInsn opc, uint32_t d)
+    __attribute__((unused));
+
+static int32_t encode_d_slot(LoongArchInsn opc, uint32_t d)

Just an FYI: you can add the attribute directly to the function definition like so

static int32_t __attribute__((unused))
encode_d_slot(LoongArchInsn opc, uint32_t d)
{
...
}

r~

#10 lore raw

Re: [PATCH 04/30] tcg/loongarch: Add generated instruction opcodes and encoding helpers

Hi Richard,

Quoted 34 lines · Richard Henderson
Quoted 12 lines · WANG Xuerui

---
  tcg/loongarch/tcg-insn-defs.c.inc | 1080 +++++++++++++++++++++++++++++

  1 file changed, 1080 insertions(+)

  create mode 100644 tcg/loongarch/tcg-insn-defs.c.inc

tcg/loongarch/tcg-insn-defs.c.inc new file +0 -0
new file mode 100644
index 0000000000..413f7ffc12

tcg/loongarch/tcg-insn-defs.c.inc line 10

Quoted 10 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +10 -0
@@ -0,0 +1,1080 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * LoongArch instruction formats, opcodes, and encoders for TCG use.
+ *
+ * Code generated by genqemutcgdefs from
+ *https://github.com/loongson-community/loongarch-opcodes,
+ * from commit bb5234081663faaefb6b921a7848b18e19519890.
+ * DO NOT EDIT.
+ */
+

tcg/loongarch/tcg-insn-defs.c.inc line 105

Quoted 4 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +4 -0
+static int32_t encode_d_slot(LoongArchInsn opc, uint32_t d)
+    __attribute__((unused));
+
+static int32_t encode_d_slot(LoongArchInsn opc, uint32_t d)

Just an FYI: you can add the attribute directly to the function definition like so

Quoted 6 lines

static int32_t __attribute__((unused))
encode_d_slot(LoongArchInsn opc, uint32_t d)
{
   ...
}

Fine; I always struggle to remember the correct placement of attributes! I'll try to adjust that in loongarch-opcodes repo. If I can arrive at something that doesn't need prototypes and builds cleanly, I'll replace the code here and shave off maybe 100 lines (because currently we use 88 insns).

Quoted 2 lines

r~

#11 lore raw

Re: [PATCH 04/30] tcg/loongarch: Add generated instruction opcodes and encoding helpers

In reply to WANG Xuerui

Review

Quoted 8 lines · WANG Xuerui
---
  tcg/loongarch/tcg-insn-defs.c.inc | 1080 +++++++++++++++++++++++++++++
  1 file changed, 1080 insertions(+)

create mode 100644 tcg/loongarch/tcg-insn-defs.c.inc

(quoted diff) +2 -0
+static int32_t encode_dj_slots(LoongArchInsn opc, uint32_t d, uint32_t j)
+{

Can we move the range check to the callee and avoid masking the values in the caller?

       tcg_debug_assert(d < 0x20);
       tcg_debug_assert(j < 0x20);

tcg/loongarch/tcg-insn-defs.c.inc line 123

Quoted 9 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +9 -0
+    return opc | d | j << 5;
+}
+
+static int32_t encode_djk_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                uint32_t k) __attribute__((unused));
+
+static int32_t encode_djk_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                uint32_t k)
+{
       tcg_debug_assert(d < 0x20);
       tcg_debug_assert(j < 0x20);
Quoted 28 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +28 -0
+    return opc | d | j << 5 | k << 10;
+}
+
+static int32_t encode_djkm_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                 uint32_t k, uint32_t m)
+    __attribute__((unused));
+
+static int32_t encode_djkm_slots(LoongArchInsn opc, uint32_t d, uint32_t j,
+                                 uint32_t k, uint32_t m)
+{
+    return opc | d | j << 5 | k << 10 | m << 16;
+}
+
+static int32_t encode_dk_slots(LoongArchInsn opc, uint32_t d, uint32_t k)
+    __attribute__((unused));
+
+static int32_t encode_dk_slots(LoongArchInsn opc, uint32_t d, uint32_t k)
+{
+    return opc | d | k << 10;
+}
+
+static int32_t encode_dj_insn(LoongArchInsn opc, TCGReg d, TCGReg j)
+    __attribute__((unused));
+
+static int32_t encode_dj_insn(LoongArchInsn opc, TCGReg d, TCGReg j)
+{
+    d &= 0x1f;
+    j &= 0x1f;

tcg/loongarch/tcg-insn-defs.c.inc line 161

Quoted 10 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +10 -0
+    return encode_dj_slots(opc, d, j);
+}
+
+static int32_t encode_djk_insn(LoongArchInsn opc, TCGReg d, TCGReg j, TCGReg k)
+    __attribute__((unused));
+
+static int32_t encode_djk_insn(LoongArchInsn opc, TCGReg d, TCGReg j, TCGReg k)
+{
+    d &= 0x1f;
+    j &= 0x1f;

^ moved to encode_djk_slots()

Quoted 1 line

+ k &= 0x1f;

       tcg_debug_assert(k < 0x20);

tcg/loongarch/tcg-insn-defs.c.inc line 173

Quoted 11 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +11 -0
+    return encode_djk_slots(opc, d, j, k);
+}
+
+static int32_t encode_djsk12_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk12) __attribute__((unused));
+
+static int32_t encode_djsk12_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk12)
+{
+    d &= 0x1f;
+    j &= 0x1f;

^ moved to encode_djk_slots()

Quoted 1 line

+ sk12 &= 0xfff;

       tcg_debug_assert(sk12 < 0x1000);

tcg/loongarch/tcg-insn-defs.c.inc line 185

Quoted 11 lines
tcg/loongarch/tcg-insn-defs.c.inc new file +11 -0
+    return encode_djk_slots(opc, d, j, sk12);
+}
+
+static int32_t encode_djsk16_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk16) __attribute__((unused));
+
+static int32_t encode_djsk16_insn(LoongArchInsn opc, TCGReg d, TCGReg j,
+                                  int32_t sk16)
+{
+    d &= 0x1f;
+    j &= 0x1f;

^ moved to encode_djk_slots()

Quoted 1 line

+ sk16 &= 0xffff;

       tcg_debug_assert(sk16 < 0x10000);

Review

Quoted 2 lines
(quoted diff) +2 -0
+    return encode_djk_slots(opc, d, j, sk16);
+}

etc...

#12 lore raw

Re: [PATCH 04/30] tcg/loongarch: Add generated instruction opcodes and encoding helpers

Hi Philippe,

Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 5 lines · WANG Xuerui

---
  tcg/loongarch/tcg-insn-defs.c.inc | 1080 +++++++++++++++++++++++++++++

  1 file changed, 1080 insertions(+)

  create mode 100644 tcg/loongarch/tcg-insn-defs.c.inc

Quoted 8 lines
Quoted 3 lines

+static int32_t encode_dj_slots(LoongArchInsn opc, uint32_t d, uint32_t j)
+{

Can we move the range check to the callee and avoid masking the values in the caller?

       tcg_debug_assert(d < 0x20);
       tcg_debug_assert(j < 0x20);

Making use of tcg_debug_assert would be rather nice, but in fact different instructions could have differently sized fields start from the same offset. Take the "bstrpick.w" and "bstrpick.d" instructions, they belong to DJUk5Um5 and DJUk6Um6 formats respectively; the "Uk5" and "Uk6" fields both start from the 10th bit but have different value ranges. So the range checks necessarily live in encoders for the individual formats.

#13 lore raw

PATCH 5/30 tcg/loongarch: Add register names, allocation order and input/output sets

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 118 +++++++++++++++++++++++++++++++++
 1 file changed, 118 insertions(+)
 create mode 100644 tcg/loongarch/tcg-target.c.inc
tcg/loongarch/tcg-target.c.inc new file +118 -0
new file mode 100644
index 0000000000..f8c71bbaf4
@@ -0,0 +1,118 @@
+/*
+ * Tiny Code Generator for QEMU
+ *
+ * Copyright (c) 2021 WANG Xuerui <[email protected]>
+ *
+ * Based on tcg/riscv/tcg-target.c.inc
+ *
+ * Copyright (c) 2018 SiFive, Inc
+ * Copyright (c) 2008-2009 Arnaud Patard <[email protected]>
+ * Copyright (c) 2009 Aurelien Jarno <[email protected]>
+ * Copyright (c) 2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifdef CONFIG_DEBUG_TCG
+static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
+    "zero",
+    "ra",
+    "tp",
+    "sp",
+    "a0",
+    "a1",
+    "a2",
+    "a3",
+    "a4",
+    "a5",
+    "a6",
+    "a7",
+    "t0",
+    "t1",
+    "t2",
+    "t3",
+    "t4",
+    "t5",
+    "t6",
+    "t7",
+    "t8",
+    "r21", /* reserved in the LP64 ABI, hence no ABI name */
+    "s9",
+    "s0",
+    "s1",
+    "s2",
+    "s3",
+    "s4",
+    "s5",
+    "s6",
+    "s7",
+    "s8"
+};
+#endif
+
+static const int tcg_target_reg_alloc_order[] = {
+    /* Registers preserved across calls */
+    /* TCG_REG_S0 reserved for TCG_AREG0 */
+    TCG_REG_S1,
+    TCG_REG_S2,
+    TCG_REG_S3,
+    TCG_REG_S4,
+    TCG_REG_S5,
+    TCG_REG_S6,
+    TCG_REG_S7,
+    TCG_REG_S8,
+    TCG_REG_S9,
+
+    /* Registers (potentially) clobbered across calls */
+    TCG_REG_T0,
+    TCG_REG_T1,
+    TCG_REG_T2,
+    TCG_REG_T3,
+    TCG_REG_T4,
+    TCG_REG_T5,
+    TCG_REG_T6,
+    TCG_REG_T7,
+    TCG_REG_T8,
+
+    /* Argument registers */
+    TCG_REG_A0,
+    TCG_REG_A1,
+    TCG_REG_A2,
+    TCG_REG_A3,
+    TCG_REG_A4,
+    TCG_REG_A5,
+    TCG_REG_A6,
+    TCG_REG_A7,
+};
+
+static const int tcg_target_call_iarg_regs[] = {
+    TCG_REG_A0,
+    TCG_REG_A1,
+    TCG_REG_A2,
+    TCG_REG_A3,
+    TCG_REG_A4,
+    TCG_REG_A5,
+    TCG_REG_A6,
+    TCG_REG_A7,
+};
+
+static const int tcg_target_call_oarg_regs[] = {
+    TCG_REG_A0,
+    TCG_REG_A1,
+};
Signature · 1 line
2.33.0
Reviewed-by on v1 5/30
#14 lore raw

Re: [PATCH 05/30] tcg/loongarch: Add register names, allocation order and input/output sets

tcg/loongarch/tcg-target.c.inc line 102

Quoted 10 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc new file +10 -0
+    /* Argument registers */
+    TCG_REG_A0,
+    TCG_REG_A1,
+    TCG_REG_A2,
+    TCG_REG_A3,
+    TCG_REG_A4,
+    TCG_REG_A5,
+    TCG_REG_A6,
+    TCG_REG_A7,
+};

Generally I'd place the argument registers in reverse usage order. It means that we'll try to use A7 before A0, which may work to our favor if the called function has less than 8 arguments.

But otherwise,

r~

#15 lore raw

Re: [PATCH 05/30] tcg/loongarch: Add register names, allocation order and input/output sets

Hi Richard,

Quoted 12 lines · Richard Henderson
Quoted 10 lines · WANG Xuerui

+    /* Argument registers */
+    TCG_REG_A0,
+    TCG_REG_A1,
+    TCG_REG_A2,
+    TCG_REG_A3,
+    TCG_REG_A4,
+    TCG_REG_A5,
+    TCG_REG_A6,
+    TCG_REG_A7,
+};

Quoted 1 line

Generally I'd place the argument registers in reverse usage order.  It means that we'll try to use A7 before A0, which may work to our favor if the called function has less than 8 arguments.

Hmm, is that a trick already employed by other TCG ports? I'll check the code and adjust if that proves beneficial indeed.

Quoted 6 lines

But otherwise,

r~

#16 lore raw

PATCH 6/30 tcg/loongarch: Define the operand constraints

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-str.h | 26 ++++++++++++++++
 tcg/loongarch/tcg-target.c.inc     | 48 ++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100644 tcg/loongarch/tcg-target-con-str.h
tcg/loongarch/tcg-target-con-str.h new file +26 -0
new file mode 100644
index 0000000000..30b42d83a4
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Define LoongArch target-specific operand constraints.
+ *
+ * Copyright (c) 2021 WANG Xuerui <[email protected]>
+ *
+ * Based on tcg/riscv/tcg-target-con-str.h
+ *
+ * Copyright (c) 2021 Linaro
+ */
+
+/*
+ * Define constraint letters for register sets:
+ * REGS(letter, register_mask)
+ */
+REGS('r', ALL_GENERAL_REGS)
+REGS('L', ALL_GENERAL_REGS & ~SOFTMMU_RESERVE_REGS)
+
+/*
+ * Define constraint letters for constants:
+ * CONST(letter, TCG_CT_CONST_* bit set)
+ */
+CONST('I', TCG_CT_CONST_S12)
+CONST('N', TCG_CT_CONST_N12)
+CONST('U', TCG_CT_CONST_U12)
+CONST('Z', TCG_CT_CONST_ZERO)
tcg/loongarch/tcg-target.c.inc +48 -0

51 lines not on this page · raw message

Signature · 1 line
2.33.0
#17 lore raw

Re: [PATCH 06/30] tcg/loongarch: Define the operand constraints

tcg/loongarch/tcg-target.c.inc static const int tcg_target_call_oarg_regs[] = { · line 145

Quoted 8 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +8 -0
+static inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
+{
+    if (TCG_TARGET_REG_BITS == 32) {
+        return sextract32(val, pos, len);
+    } else {
+        return sextract64(val, pos, len);
+    }
+}

You don't need this conditional. Just use sextract64 directly.

r~

#18 lore raw

PATCH 7/30 tcg/loongarch: Implement necessary relocation operations

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 84 ++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
tcg/loongarch/tcg-target.c.inc +84 -0
index 594b434b47..8be34f8275 100644
@@ -164,3 +164,87 @@ static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
     }
     return 0;
 }
+
+/*
+ * Relocations
+ */
+
+/*
+ * Relocation records defined in LoongArch ELF psABI v1.00 is way too much
+ * complicated; a whopping stack machine is needed to stuff the fields, at
+ * the very least one SOP_PUSH and one SOP_POP (of the correct format) are
+ * needed.
+ *
+ * Hence, define our own simpler relocation types. Numbers are chosen as to
+ * not collide with potential future additions to the true ELF relocation
+ * type enum.
+ */
+
+/* Field Sk16; suitable for conditional jumps */
+#define R_LOONGARCH_SK16    256
+/* Field Sd10k16; suitable for B and BL */
+#define R_LOONGARCH_SD10K16 257
+
+static bool reloc_sk16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
+{
+    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
+    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
+
+    tcg_debug_assert((offset & 2) == 0);
+    offset >>= 2;
+    if (offset == sextreg(offset, 0, 16)) {
+        *src_rw |= (offset << 10) & 0x3fffc00;
+        return true;
+    }
+
+    return false;
+}
+
+static bool reloc_sd10k16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
+{
+    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
+    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
+
+    tcg_debug_assert((offset & 2) == 0);
+    offset >>= 2;
+    if (offset == sextreg(offset, 0, 26)) {
+        *src_rw |= (offset >> 16) & 0x3ff; /* slot d10 */
+        *src_rw |= ((offset & 0xffff) << 10) & 0x3fffc00; /* slot k16 */
+        return true;
+    }
+
+    return false;
+}
+
+static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
+{
+    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
+    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
+    int32_t lo = sextreg(offset, 0, 12);
+    int32_t hi = offset - lo;
+
+    tcg_debug_assert((offset & 2) == 0);
+    if (offset == hi + lo) {
+        hi >>= 12;
+        src_rw[0] |= (hi << 5) & 0x1ffffe0; /* pcaddu12i's Sj20 imm */
+        lo >>= 2;
+        src_rw[1] |= (lo << 10) & 0x3fffc00; /* jirl's Sk16 imm */
+        return true;
+    }
+
+    return false;
+}
+
+static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
+                        intptr_t value, intptr_t addend)
+{
+    tcg_debug_assert(addend == 0);
+    switch (type) {
+    case R_LOONGARCH_SK16:
+        return reloc_sk16(code_ptr, (tcg_insn_unit *)value);
+    case R_LOONGARCH_SD10K16:
+        return reloc_sd10k16(code_ptr, (tcg_insn_unit *)value);
+    default:
+        g_assert_not_reached();
+    }
+}
Signature · 1 line
2.33.0
#19 lore raw

Re: [PATCH 07/30] tcg/loongarch: Implement necessary relocation operations

Quoted 8 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +8 -0
+static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
+{
+    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
+    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
+    int32_t lo = sextreg(offset, 0, 12);
+    int32_t hi = offset - lo;
+
+    tcg_debug_assert((offset & 2) == 0);

tcg/loongarch/tcg-target.c.inc static bool tcg_target_const_match(int64_t val, TCGType type, int ct) · line 236

Quoted 10 lines
tcg/loongarch/tcg-target.c.inc +10 -0
+    if (offset == hi + lo) {
+        hi >>= 12;
+        src_rw[0] |= (hi << 5) & 0x1ffffe0; /* pcaddu12i's Sj20 imm */
+        lo >>= 2;
+        src_rw[1] |= (lo << 10) & 0x3fffc00; /* jirl's Sk16 imm */
+        return true;
+    }
+
+    return false;
+}

This doesn't seem to belong as a "reloc".
Certainly it doesn't seem like something that can simply be allowed to fail.

r~

#20 lore raw

Re: [PATCH 07/30] tcg/loongarch: Implement necessary relocation operations

Hi Richard,

Quoted 15 lines · Richard Henderson
Quoted 14 lines · WANG Xuerui

+static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target)

tcg/loongarch/tcg-target.c.inc +12 -0
+{
+    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
+    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
+    int32_t lo = sextreg(offset, 0, 12);
+    int32_t hi = offset - lo;
+
+    tcg_debug_assert((offset & 2) == 0);
+    if (offset == hi + lo) {
+        hi >>= 12;
+        src_rw[0] |= (hi << 5) & 0x1ffffe0; /* pcaddu12i's Sj20 imm */
+        lo >>= 2;
+        src_rw[1] |= (lo << 10) & 0x3fffc00; /* jirl's Sk16 imm */
Quoted 9 lines

tcg/loongarch/tcg-target.c.inc static bool tcg_target_const_match(int64_t val, TCGType type, int ct) · line 201

Quoted 5 lines

+        return true;
+    }

tcg/loongarch/tcg-target.c.inc +3 -0
+
+    return false;
+}

This doesn't seem to belong as a "reloc".
Certainly it doesn't seem like something that can simply be allowed to fail.

Yes, you're right on this; on closer look at the riscv port they actually reused this logic once (the riscv port drops large constants to pool, hence need some PC-relative hackery). For LoongArch the only usage of this code is for generating calls, so I'll just merge this into the commit doing tcg_out_call, and inline if the resulting code is still readable.

And it's 1 a.m. here in China, so I'll be processing the other review comments after getting some sleep. (Today's in the middle of the 3-day Mid-Autumn Festival holiday here, and that's why I can work on this hobby project like it's $DAY_JOB!) I'll send the v2 hopefully at afternoon local time (tomorrow in your timezone).

Quoted 2 lines

r~

#21 lore raw

PATCH 8/30 tcg/loongarch: Implement the memory barrier op

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
tcg/loongarch/tcg-target.c.inc +32 -0
index 8be34f8275..71564e3246 100644
@@ -248,3 +248,35 @@ static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
         g_assert_not_reached();
     }
 }
+
+#include "tcg-insn-defs.c.inc"
+
+/*
+ * TCG intrinsics
+ */
+
+static void tcg_out_mb(TCGContext *s, TCGArg a0)
+{
+    /* Baseline LoongArch only has the full barrier, unfortunately.  */
+    tcg_out_opc_dbar(s, 0);
+}
+
+/*
+ * Entry-points
+ */
+
+static void tcg_out_op(TCGContext *s, TCGOpcode opc,
+                       const TCGArg args[TCG_MAX_OP_ARGS],
+                       const int const_args[TCG_MAX_OP_ARGS])
+{
+    TCGArg a0 = args[0];
+
+    switch (opc) {
+    case INDEX_op_mb:
+        tcg_out_mb(s, a0);
+        break;
+
+    default:
+        g_assert_not_reached();
+    }
+}
Signature · 1 line
2.33.0
#22 lore raw

PATCH 9/30 tcg/loongarch: Implement tcg_out_mov and tcg_out_movi

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 73 ++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
tcg/loongarch/tcg-target.c.inc +73 -0
index 71564e3246..60783d7ddc 100644
@@ -261,6 +261,77 @@ static void tcg_out_mb(TCGContext *s, TCGArg a0)
     tcg_out_opc_dbar(s, 0);
 }
 
+static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
+{
+    if (ret == arg) {
+        return true;
+    }
+    switch (type) {
+    case TCG_TYPE_I32:
+    case TCG_TYPE_I64:
+        /*
+         * Conventional register-register move used in LoongArch is
+         * `or dst, src, zero`.
+         */
+        tcg_out_opc_or(s, ret, arg, TCG_REG_ZERO);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+    return true;
+}
+
+static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
+                         tcg_target_long val)
+{
+    tcg_target_long low, upper, higher, top;
+
+    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
+        val = (int32_t)val;
+    }
+
+    /* Single-instruction cases.  */
+    low = sextreg(val, 0, 12);
+    if (low == val) {
+        /* val fits in simm12: addi.w rd, zero, val */
+        tcg_out_opc_addi_w(s, rd, TCG_REG_ZERO, val);
+        return;
+    }
+    if (0x800 <= val && val <= 0xfff) {
+        /* val fits in uimm12: ori rd, zero, val */
+        tcg_out_opc_ori(s, rd, TCG_REG_ZERO, val);
+        return;
+    }
+
+    /* Chop upper bits into 3 immediate-field-sized segments respectively.  */
+    upper = (val >> 12) & 0xfffff;
+    higher = (val >> 32) & 0xfffff;
+    top = val >> 52;
+
+    tcg_out_opc_lu12i_w(s, rd, upper);
+    if (low != 0) {
+        tcg_out_opc_ori(s, rd, rd, low);
+    }
+
+    if (sextreg(val, 0, 32) == val) {
+        /*
+         * Fits in 32-bits, upper bits are already properly sign-extended by
+         * lu12i.w.
+         */
+        return;
+    }
+    tcg_out_opc_cu32i_d(s, rd, higher);
+
+    if (sextreg(val, 0, 52) == val) {
+        /*
+         * Fits in 52-bits, upper bits are already properly sign-extended by
+         * cu32i.d.
+         */
+        return;
+    }
+    tcg_out_opc_cu52i_d(s, rd, rd, top);
+}
+
 /*
  * Entry-points
  */
@@ -276,6 +347,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_mb(s, a0);
         break;
 
+    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
+    case INDEX_op_mov_i64:
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.33.0
#23 lore raw

Re: [PATCH 09/30] tcg/loongarch: Implement tcg_out_mov and tcg_out_movi

Quoted 39 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +39 -0
+static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
+                         tcg_target_long val)
+{
+    tcg_target_long low, upper, higher, top;
+
+    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
+        val = (int32_t)val;
+    }
+
+    /* Single-instruction cases.  */
+    low = sextreg(val, 0, 12);
+    if (low == val) {
+        /* val fits in simm12: addi.w rd, zero, val */
+        tcg_out_opc_addi_w(s, rd, TCG_REG_ZERO, val);
+        return;
+    }
+    if (0x800 <= val && val <= 0xfff) {
+        /* val fits in uimm12: ori rd, zero, val */
+        tcg_out_opc_ori(s, rd, TCG_REG_ZERO, val);
+        return;
+    }
+
+    /* Chop upper bits into 3 immediate-field-sized segments respectively.  */
+    upper = (val >> 12) & 0xfffff;
+    higher = (val >> 32) & 0xfffff;
+    top = val >> 52;
+
+    tcg_out_opc_lu12i_w(s, rd, upper);
+    if (low != 0) {
+        tcg_out_opc_ori(s, rd, rd, low);
+    }
+
+    if (sextreg(val, 0, 32) == val) {
+        /*
+         * Fits in 32-bits, upper bits are already properly sign-extended by
+         * lu12i.w.
+         */
+        return;
+    }

tcg/loongarch/tcg-target.c.inc static void tcg_out_mb(TCGContext *s, TCGArg a0) · line 332

Quoted 10 lines
tcg/loongarch/tcg-target.c.inc +10 -0
+    tcg_out_opc_cu32i_d(s, rd, higher);
+
+    if (sextreg(val, 0, 52) == val) {
+        /*
+         * Fits in 52-bits, upper bits are already properly sign-extended by
+         * cu32i.d.
+         */
+        return;
+    }
+    tcg_out_opc_cu52i_d(s, rd, rd, top);

Looks ok.

You'll want to check for small to medium pc-relative addresses. Almost every TB will load the address of TB+C (0 <= C <= 3) at the end, and the TB structure immediately precedes the code. Because of the odd values, you'll sometimes need two instructions. But that will still be less than the 3-4 for a 52/64-bit address constant.

r~

#24 lore raw

PATCH 10/30 tcg/loongarch: Implement goto_ptr

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h | 17 +++++++++++++++++
 tcg/loongarch/tcg-target.c.inc     | 15 +++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100644 tcg/loongarch/tcg-target-con-set.h
tcg/loongarch/tcg-target-con-set.h new file +17 -0
new file mode 100644
index 0000000000..5cc4407367
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Define LoongArch target-specific constraint sets.
+ *
+ * Copyright (c) 2021 WANG Xuerui <[email protected]>
+ *
+ * Based on tcg/riscv/tcg-target-con-set.h
+ *
+ * Copyright (c) 2021 Linaro
+ */
+
+/*
+ * C_On_Im(...) defines a constraint set with <n> outputs and <m> inputs.
+ * Each operand should be a sequence of constraint letters as defined by
+ * tcg-target-con-str.h; the constraint combination is inclusive or.
+ */
+C_O0_I1(r)
tcg/loongarch/tcg-target.c.inc +15 -0
index 60783d7ddc..9d78146fb9 100644
@@ -347,9 +347,24 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_mb(s, a0);
         break;
 
+    case INDEX_op_goto_ptr:
+        tcg_out_opc_jirl(s, TCG_REG_ZERO, a0, 0);
+        break;
+
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     default:
         g_assert_not_reached();
     }
 }
+
+static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
+{
+    switch (op) {
+    case INDEX_op_goto_ptr:
+        return C_O0_I1(r);
+
+    default:
+        g_assert_not_reached();
+    }
+}
Signature · 1 line
2.33.0
Reviewed-by on v1 10/30
#25 lore raw

Re: [PATCH 10/30] tcg/loongarch: Implement goto_ptr

Quoted 7 lines

+ * Define LoongArch target-specific constraint sets.
+ *
+ * Copyright (c) 2021 WANG Xuerui <[email protected]>
+ *
+ * Based on tcg/riscv/tcg-target-con-set.h
+ *
+ * Copyright (c) 2021 Linaro

Too much cut and paste. Otherwise,

r~

#26 lore raw

PATCH 11/30 tcg/loongarch: Implement sign-/zero-extension ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  1 +
 tcg/loongarch/tcg-target.c.inc     | 82 ++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+)
tcg/loongarch/tcg-target-con-set.h +1 -0
index 5cc4407367..7e459490ea 100644
@@ -15,3 +15,4 @@
  * tcg-target-con-str.h; the constraint combination is inclusive or.
  */
 C_O0_I1(r)
+C_O1_I1(r, r)
tcg/loongarch/tcg-target.c.inc +82 -0

106 lines not on this page · raw message

Signature · 1 line
2.33.0
#28 lore raw

PATCH 12/30 tcg/loongarch: Implement not/and/or/xor/nor/andc/orc ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  2 +
 tcg/loongarch/tcg-target.c.inc     | 69 ++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
tcg/loongarch/tcg-target-con-set.h +2 -0
index 7e459490ea..385f503552 100644
@@ -16,3 +16,5 @@
  */
 C_O0_I1(r)
 C_O1_I1(r, r)
+C_O1_I2(r, r, r)
+C_O1_I2(r, r, rU)
tcg/loongarch/tcg-target.c.inc +69 -0

89 lines not on this page · raw message

Signature · 1 line
2.33.0
Reviewed-by on v1 12/30
#29 lore raw

Re: [PATCH 12/30] tcg/loongarch: Implement not/and/or/xor/nor/andc/orc ops

tcg/loongarch/tcg-target.c.inc static void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 435

Quoted 4 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +4 -0
+    case INDEX_op_andc_i32:
+    case INDEX_op_andc_i64:
+        tcg_out_opc_andn(s, a0, a1, a2);
+        break;

You may want to add the constant case here, implemented with andi, with the constant inverted, similarly to the negation of the N constraint. We do not (but probably should) canonicalize andc/orc/eqv constants to and/or/xor during optimization...

r~

#30 lore raw

PATCH 13/30 tcg/loongarch: Implement deposit/extract ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  1 +
 tcg/loongarch/tcg-target.c.inc     | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
tcg/loongarch/tcg-target-con-set.h +1 -0
index 385f503552..b0751c4bb0 100644
@@ -18,3 +18,4 @@ C_O0_I1(r)
 C_O1_I1(r, r)
 C_O1_I2(r, r, r)
 C_O1_I2(r, r, rU)
+C_O1_I2(r, 0, rZ)
tcg/loongarch/tcg-target.c.inc +21 -0
index e364b6c1da..e5356bdaf8 100644
@@ -466,6 +466,20 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         }
         break;
 
+    case INDEX_op_extract_i32:
+        tcg_out_opc_bstrpick_w(s, a0, a1, a2, a2 + args[3] - 1);
+        break;
+    case INDEX_op_extract_i64:
+        tcg_out_opc_bstrpick_d(s, a0, a1, a2, a2 + args[3] - 1);
+        break;
+
+    case INDEX_op_deposit_i32:
+        tcg_out_opc_bstrins_w(s, a0, a2, args[3], args[3] + args[4] - 1);
+        break;
+    case INDEX_op_deposit_i64:
+        tcg_out_opc_bstrins_d(s, a0, a2, args[3], args[3] + args[4] - 1);
+        break;
+
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     default:
@@ -495,6 +509,8 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_ext_i32_i64:
     case INDEX_op_not_i32:
     case INDEX_op_not_i64:
+    case INDEX_op_extract_i32:
+    case INDEX_op_extract_i64:
         return C_O1_I1(r, r);
 
     case INDEX_op_nor_i32:
@@ -515,6 +531,11 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
         /* LoongArch reg-imm bitops have their imms ZERO-extended */
         return C_O1_I2(r, r, rU);
 
+    case INDEX_op_deposit_i32:
+    case INDEX_op_deposit_i64:
+        /* Must deposit into the same register as input */
+        return C_O1_I2(r, 0, rZ);
+
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.33.0
#32 lore raw

PATCH 14/30 tcg/loongarch: Implement bswap32_i32/bswap64_i64

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 10 ++++++++++
 1 file changed, 10 insertions(+)
tcg/loongarch/tcg-target.c.inc +10 -0
index e5356bdaf8..d617b833e5 100644
@@ -480,6 +480,14 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_opc_bstrins_d(s, a0, a2, args[3], args[3] + args[4] - 1);
         break;
 
+    case INDEX_op_bswap32_i32:
+        tcg_out_opc_revb_2h(s, a0, a1);
+        tcg_out_opc_rotri_w(s, a0, a0, 16);
+        break;
+    case INDEX_op_bswap64_i64:
+        tcg_out_opc_revb_d(s, a0, a1);
+        break;
+
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     default:
@@ -511,6 +519,8 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_not_i64:
     case INDEX_op_extract_i32:
     case INDEX_op_extract_i64:
+    case INDEX_op_bswap32_i32:
+    case INDEX_op_bswap64_i64:
         return C_O1_I1(r, r);
 
     case INDEX_op_nor_i32:
Signature · 1 line
2.33.0
#33 lore raw

Re: [PATCH 14/30] tcg/loongarch: Implement bswap32_i32/bswap64_i64

tcg/loongarch/tcg-target.c.inc static void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 489

Quoted 7 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +7 -0
+    case INDEX_op_bswap32_i32:
+        tcg_out_opc_revb_2h(s, a0, a1);
+        tcg_out_opc_rotri_w(s, a0, a0, 16);
+        break;
+    case INDEX_op_bswap64_i64:
+        tcg_out_opc_revb_d(s, a0, a1);
+        break;

You're missing INDEX_op_bswap32_i64, which in addition has a third argument consisting of TCG_BSWAP_* bits.

I would have expected revb_2w to be the preferred implementation of bswap32. I would expect something like

    case INDEX_op_bswap32_i32:
        /* All 32-bit values are computed sign-extended in the register. */
        a2 = TCG_BSWAP_OS;
        /* fall through */
    case INDEX_op_bswap32_i64:
        tcg_out_opc_revb_2w(s, a0, a1);
        if (a2 & TCG_BSWAP_OS) {
            tcg_out_ext32s(s, a0, a0);
        } else if (a2 & TCG_BSWAP_OZ) {
            tcg_out_ext32u(s, a0, a0);
        }
        break;

r~

#34 lore raw

Re: [PATCH 14/30] tcg/loongarch: Implement bswap32_i32/bswap64_i64

Quoted 3 lines · Richard Henderson

        } else if (a2 & TCG_BSWAP_OZ) {
            tcg_out_ext32u(s, a0, a0);
        }

Actually,

if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ)

If the input is zero-extended, the output of revb_2w will also be zero-extended already.

r~

#35 lore raw

Re: [PATCH 14/30] tcg/loongarch: Implement bswap32_i32/bswap64_i64

In reply to Richard Henderson

Hi Richard,

Quoted 14 lines · Richard Henderson
Quoted 7 lines · WANG Xuerui

+    case INDEX_op_bswap32_i32:
+        tcg_out_opc_revb_2h(s, a0, a1);
+        tcg_out_opc_rotri_w(s, a0, a0, 16);
+        break;
+    case INDEX_op_bswap64_i64:
+        tcg_out_opc_revb_d(s, a0, a1);
+        break;

You're missing INDEX_op_bswap32_i64, which in addition has a third argument consisting of TCG_BSWAP_* bits.

I would have expected revb_2w to be the preferred implementation of bswap32.  I would expect something like

Quoted 13 lines

    case INDEX_op_bswap32_i32:
        /* All 32-bit values are computed sign-extended in the register. */ a2 = TCG_BSWAP_OS;
        /* fall through */
    case INDEX_op_bswap32_i64:
        tcg_out_opc_revb_2w(s, a0, a1);
        if (a2 & TCG_BSWAP_OS) {
            tcg_out_ext32s(s, a0, a0);
        } else if (a2 & TCG_BSWAP_OZ) {
            tcg_out_ext32u(s, a0, a0);
        }
        break;

You're right when we're supporting only 64-bit hosts. While I was writing that code I hadn't decided whether to remove support for 32-bit hosts, so I didn't make use of 64-bit instructions for the 32-bit ops. I'll fix this in v2.

Quoted 2 lines

r~

#36 lore raw

PATCH 15/30 tcg/loongarch: Implement clz/ctz ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  1 +
 tcg/loongarch/tcg-target.c.inc     | 31 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
tcg/loongarch/tcg-target-con-set.h +1 -0
index b0751c4bb0..417c97549a 100644
@@ -18,4 +18,5 @@ C_O0_I1(r)
 C_O1_I1(r, r)
 C_O1_I2(r, r, r)
 C_O1_I2(r, r, rU)
+C_O1_I2(r, r, rZ)
 C_O1_I2(r, 0, rZ)
tcg/loongarch/tcg-target.c.inc +31 -0
index d617b833e5..e817964a7e 100644
@@ -362,6 +362,17 @@ static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
     tcg_out_opc_addi_w(s, ret, arg, 0);
 }
 
+static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc,
+                           TCGReg a0, TCGReg a1, TCGReg a2)
+{
+    /* all clz/ctz insns belong to DJ-format */
+    tcg_out32(s, encode_dj_insn(opc, TCG_REG_TMP0, a1));
+    /* a0 = a1 ? REG_TMP0 : a2 */
+    tcg_out_opc_maskeqz(s, TCG_REG_TMP0, TCG_REG_TMP0, a1);
+    tcg_out_opc_masknez(s, a0, a2, a1);
+    tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0);
+}
+
 /*
  * Entry-points
  */
@@ -488,6 +499,20 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_opc_revb_d(s, a0, a1);
         break;
 
+    case INDEX_op_clz_i32:
+        tcg_out_clzctz(s, OPC_CLZ_W, a0, a1, a2);
+        break;
+    case INDEX_op_clz_i64:
+        tcg_out_clzctz(s, OPC_CLZ_D, a0, a1, a2);
+        break;
+
+    case INDEX_op_ctz_i32:
+        tcg_out_clzctz(s, OPC_CTZ_W, a0, a1, a2);
+        break;
+    case INDEX_op_ctz_i64:
+        tcg_out_clzctz(s, OPC_CTZ_D, a0, a1, a2);
+        break;
+
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     default:
@@ -541,6 +566,12 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
         /* LoongArch reg-imm bitops have their imms ZERO-extended */
         return C_O1_I2(r, r, rU);
 
+    case INDEX_op_clz_i32:
+    case INDEX_op_clz_i64:
+    case INDEX_op_ctz_i32:
+    case INDEX_op_ctz_i64:
+        return C_O1_I2(r, r, rZ);
+
     case INDEX_op_deposit_i32:
     case INDEX_op_deposit_i64:
         /* Must deposit into the same register as input */
Signature · 1 line
2.33.0
Reviewed-by on v1 15/30
#37 lore raw

Re: [PATCH 15/30] tcg/loongarch: Implement clz/ctz ops

tcg/loongarch/tcg-target.c.inc static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg) · line 374

Quoted 10 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +10 -0
+static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc,
+                           TCGReg a0, TCGReg a1, TCGReg a2)
+{
+    /* all clz/ctz insns belong to DJ-format */
+    tcg_out32(s, encode_dj_insn(opc, TCG_REG_TMP0, a1));
+    /* a0 = a1 ? REG_TMP0 : a2 */
+    tcg_out_opc_maskeqz(s, TCG_REG_TMP0, TCG_REG_TMP0, a1);
+    tcg_out_opc_masknez(s, a0, a2, a1);
+    tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0);
+}

From Song Gao's translation, I believe that ctz(0) == 32 or 64, depending on the operation width. This is in fact the most common result, so it's worth specializing. See tcg/i386/tcg-target.c.inc, tcg_out_clz, have_lzcnt.

But what's here looks ok.

r~

#38 lore raw

PATCH 16/30 tcg/loongarch: Implement shl/shr/sar/rotl/rotr ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  1 +
 tcg/loongarch/tcg-target.c.inc     | 91 ++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
tcg/loongarch/tcg-target-con-set.h +1 -0
index 417c97549a..8630d1ee6e 100644
@@ -17,6 +17,7 @@
 C_O0_I1(r)
 C_O1_I1(r, r)
 C_O1_I2(r, r, r)
+C_O1_I2(r, r, ri)
 C_O1_I2(r, r, rU)
 C_O1_I2(r, r, rZ)
 C_O1_I2(r, 0, rZ)
tcg/loongarch/tcg-target.c.inc +91 -0

103 lines not on this page · raw message

Signature · 1 line
2.33.0
#39 lore raw

Re: [PATCH 16/30] tcg/loongarch: Implement shl/shr/sar/rotl/rotr ops

tcg/loongarch/tcg-target.c.inc static void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 567

Quoted 7 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +7 -0
+    case INDEX_op_rotl_i32:
+        /* transform into equivalent rotr_i32 */
+        if (c2) {
+            a2 = 32 - a2;
+        } else {
+            tcg_out_opc_sub_w(s, a2, TCG_REG_ZERO, a2);
+            tcg_out_opc_addi_w(s, a2, a2, 32);

You can't modify a2 here; need to use TCG_REG_TMP0.
You don't need the addi, because the negation is sufficient, mod 32.

Likewise for INDEX_op_rotl_i64.

r~

#40 lore raw

PATCH 17/30 tcg/loongarch: Implement neg/add/sub ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  2 ++
 tcg/loongarch/tcg-target.c.inc     | 47 ++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
tcg/loongarch/tcg-target-con-set.h +2 -0
index 8630d1ee6e..58b5c487e2 100644
@@ -18,6 +18,8 @@ C_O0_I1(r)
 C_O1_I1(r, r)
 C_O1_I2(r, r, r)
 C_O1_I2(r, r, ri)
+C_O1_I2(r, r, rI)
 C_O1_I2(r, r, rU)
 C_O1_I2(r, r, rZ)
 C_O1_I2(r, 0, rZ)
+C_O1_I2(r, rZ, rN)
tcg/loongarch/tcg-target.c.inc +47 -0

71 lines not on this page · raw message

Signature · 1 line
2.33.0
Reviewed-by on v1 17/30
#41 lore raw

Re: [PATCH 17/30] tcg/loongarch: Implement neg/add/sub ops

Quoted 5 lines · WANG Xuerui
---
  tcg/loongarch/tcg-target-con-set.h |  2 ++
  tcg/loongarch/tcg-target.c.inc     | 47 ++++++++++++++++++++++++++++++
  2 files changed, 49 insertions(+)

You shouldn't have needed to implement neg, since tcg should have figured out that it could use subtract from zero. But anyway,

r~

#42 lore raw

PATCH 18/30 tcg/loongarch: Implement mul/mulsh/muluh/div/divu/rem/remu ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  1 +
 tcg/loongarch/tcg-target.c.inc     | 65 ++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
tcg/loongarch/tcg-target-con-set.h +1 -0
index 58b5c487e2..57b2846d82 100644
@@ -23,3 +23,4 @@ C_O1_I2(r, r, rU)
 C_O1_I2(r, r, rZ)
 C_O1_I2(r, 0, rZ)
 C_O1_I2(r, rZ, rN)
+C_O1_I2(r, rZ, rZ)
tcg/loongarch/tcg-target.c.inc +65 -0

77 lines not on this page · raw message

Signature · 1 line
2.33.0
#44 lore raw

PATCH 19/30 tcg/loongarch: Implement br/brcond ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |  1 +
 tcg/loongarch/tcg-target.c.inc     | 52 ++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
tcg/loongarch/tcg-target-con-set.h +1 -0
index 57b2846d82..bcbf0780ff 100644
@@ -15,6 +15,7 @@
  * tcg-target-con-str.h; the constraint combination is inclusive or.
  */
 C_O0_I1(r)
+C_O0_I2(rZ, rZ)
 C_O1_I1(r, r)
 C_O1_I2(r, r, r)
 C_O1_I2(r, r, ri)
tcg/loongarch/tcg-target.c.inc +52 -0

70 lines not on this page · raw message

Signature · 1 line
2.33.0
#46 lore raw

PATCH 20/30 tcg/loongarch: Implement setcond ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 53 ++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
tcg/loongarch/tcg-target.c.inc +53 -0
index a533a5619d..fb0143474a 100644
@@ -373,6 +373,52 @@ static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc,
     tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0);
 }
 
+static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
+                            TCGReg arg1, TCGReg arg2)
+{
+    switch (cond) {
+    case TCG_COND_EQ:
+        tcg_out_opc_sub_d(s, ret, arg1, arg2);
+        tcg_out_opc_sltui(s, ret, ret, 1);
+        break;
+    case TCG_COND_NE:
+        tcg_out_opc_sub_d(s, ret, arg1, arg2);
+        tcg_out_opc_sltu(s, ret, TCG_REG_ZERO, ret);
+        break;
+    case TCG_COND_LT:
+        tcg_out_opc_slt(s, ret, arg1, arg2);
+        break;
+    case TCG_COND_GE:
+        tcg_out_opc_slt(s, ret, arg1, arg2);
+        tcg_out_opc_xori(s, ret, ret, 1);
+        break;
+    case TCG_COND_LE:
+        tcg_out_opc_slt(s, ret, arg2, arg1);
+        tcg_out_opc_xori(s, ret, ret, 1);
+        break;
+    case TCG_COND_GT:
+        tcg_out_opc_slt(s, ret, arg2, arg1);
+        break;
+    case TCG_COND_LTU:
+        tcg_out_opc_sltu(s, ret, arg1, arg2);
+        break;
+    case TCG_COND_GEU:
+        tcg_out_opc_sltu(s, ret, arg1, arg2);
+        tcg_out_opc_xori(s, ret, ret, 1);
+        break;
+    case TCG_COND_LEU:
+        tcg_out_opc_sltu(s, ret, arg2, arg1);
+        tcg_out_opc_xori(s, ret, ret, 1);
+        break;
+    case TCG_COND_GTU:
+        tcg_out_opc_sltu(s, ret, arg2, arg1);
+        break;
+    default:
+         g_assert_not_reached();
+         break;
+     }
+}
+
 /*
  * Branch helpers
  */
@@ -726,6 +772,11 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_opc_mod_du(s, a0, a1, a2);
         break;
 
+    case INDEX_op_setcond_i32:
+    case INDEX_op_setcond_i64:
+        tcg_out_setcond(s, args[3], a0, a1, a2);
+        break;
+
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     default:
@@ -830,6 +881,8 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_rem_i64:
     case INDEX_op_remu_i32:
     case INDEX_op_remu_i64:
+    case INDEX_op_setcond_i32:
+    case INDEX_op_setcond_i64:
         return C_O1_I2(r, rZ, rZ);
 
     default:
Signature · 1 line
2.33.0
#47 lore raw

Re: [PATCH 20/30] tcg/loongarch: Implement setcond ops

tcg/loongarch/tcg-target.c.inc static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc, · line 387

Quoted 9 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +9 -0
+    switch (cond) {
+    case TCG_COND_EQ:
+        tcg_out_opc_sub_d(s, ret, arg1, arg2);
+        tcg_out_opc_sltui(s, ret, ret, 1);
+        break;
+    case TCG_COND_NE:
+        tcg_out_opc_sub_d(s, ret, arg1, arg2);
+        tcg_out_opc_sltu(s, ret, TCG_REG_ZERO, ret);
+        break;

You accept zero as input; you'll want to skip the subtract in that case.

r~

#48 lore raw

PATCH 21/30 tcg/loongarch: Implement tcg_out_call

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 37 ++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
tcg/loongarch/tcg-target.c.inc +37 -0
index fb0143474a..01c6002fdb 100644
@@ -457,6 +457,42 @@ static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
     tcg_out32(s, encode_djsk16_insn(op, arg1, arg2, 0));
 }
 
+static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
+{
+    TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
+    ptrdiff_t offset = tcg_pcrel_diff(s, arg);
+    int ret;
+
+    tcg_debug_assert((offset & 2) == 0);
+    if (offset == sextreg(offset, 0, 28)) {
+        /* short jump: +/- 256MiB */
+        if (tail) {
+            tcg_out_opc_b(s, offset >> 2);
+        } else {
+            tcg_out_opc_bl(s, offset >> 2);
+        }
+    } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) {
+        /* long jump: +/- 2GiB */
+        tcg_out_opc_pcaddu12i(s, TCG_REG_TMP0, 0);
+        tcg_out_opc_jirl(s, link, TCG_REG_TMP0, 0);
+        ret = reloc_call(s->code_ptr - 2, arg);
+        tcg_debug_assert(ret == true);
+    } else if (TCG_TARGET_REG_BITS == 64) {
+        /* far jump: 64-bit */
+        tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12);
+        tcg_target_long base = (tcg_target_long)arg - imm;
+        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base);
+        tcg_out_opc_jirl(s, link, TCG_REG_TMP0, imm >> 2);
+    } else {
+        g_assert_not_reached();
+    }
+}
+
+static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
+{
+    tcg_out_call_int(s, arg, false);
+}
+
 /*
  * Entry-points
  */
@@ -779,6 +815,7 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
 
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
+    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.33.0
#49 lore raw

Re: [PATCH 21/30] tcg/loongarch: Implement tcg_out_call

tcg/loongarch/tcg-target.c.inc static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, · line 479

Quoted 6 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +6 -0
+    } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) {
+        /* long jump: +/- 2GiB */
+        tcg_out_opc_pcaddu12i(s, TCG_REG_TMP0, 0);
+        tcg_out_opc_jirl(s, link, TCG_REG_TMP0, 0);
+        ret = reloc_call(s->code_ptr - 2, arg);
+        tcg_debug_assert(ret == true);

Just inline reloc_call here, so that you can provide the correct offsets to the pcadd and jirl instructions directly. The assert will vanish, because you've already done the range check with "offset == (int32_t)offset".

r~

#50 lore raw

Re: [PATCH 21/30] tcg/loongarch: Implement tcg_out_call

Quoted 9 lines · Richard Henderson
Quoted 6 lines · WANG Xuerui

+    } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) { +        /* long jump: +/- 2GiB */
+        tcg_out_opc_pcaddu12i(s, TCG_REG_TMP0, 0);
+        tcg_out_opc_jirl(s, link, TCG_REG_TMP0, 0);
+        ret = reloc_call(s->code_ptr - 2, arg);
+        tcg_debug_assert(ret == true);

Just inline reloc_call here, so that you can provide the correct offsets to the pcadd and jirl instructions directly.  The assert will vanish, because you've already done the range check with "offset == (int32_t)offset".

Actually, don't you want offset == sextract64(offset, 0, 34), and use pcaddu18i? Depending on the memory map of qemu, those extra bits could make the difference in directly reaching the main executable.

r~

#51 lore raw

Re: [PATCH 21/30] tcg/loongarch: Implement tcg_out_call

Hi Richard,

Quoted 10 lines · Richard Henderson
Quoted 9 lines · Richard Henderson
Quoted 7 lines · WANG Xuerui

+    } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) {
+        /* long jump: +/- 2GiB */
+        tcg_out_opc_pcaddu12i(s, TCG_REG_TMP0, 0); +        tcg_out_opc_jirl(s, link, TCG_REG_TMP0, 0); +        ret = reloc_call(s->code_ptr - 2, arg); +        tcg_debug_assert(ret == true);

Quoted 4 lines
Quoted 1 line

Just inline reloc_call here, so that you can provide the correct offsets to the pcadd and jirl instructions directly.  The assert will vanish, because you've already done the range check with "offset == (int32_t)offset".

Actually, don't you want offset == sextract64(offset, 0, 34), and use pcaddu18i? Depending on the memory map of qemu, those extra bits could make the difference in directly reaching the main executable.

Whoa, silly me, I actually didn't realize a single expected use case of pcaddu18i until I read this, the low 2 bits are always clear so 18 is exactly the amount of shift needed when paired with jirl!

I'll of course rework this to use pcaddu18i+jirl instead.

Quoted 2 lines

r~

#52 lore raw

PATCH 22/30 tcg/loongarch: Implement simple load/store ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |   1 +
 tcg/loongarch/tcg-target.c.inc     | 131 +++++++++++++++++++++++++++++
 2 files changed, 132 insertions(+)
tcg/loongarch/tcg-target-con-set.h +1 -0
index bcbf0780ff..cdbfe9cd8d 100644
@@ -15,6 +15,7 @@
  * tcg-target-con-str.h; the constraint combination is inclusive or.
  */
 C_O0_I1(r)
+C_O0_I2(rZ, r)
 C_O0_I2(rZ, rZ)
 C_O1_I1(r, r)
 C_O1_I2(r, r, r)
tcg/loongarch/tcg-target.c.inc +131 -0

155 lines not on this page · raw message

Signature · 1 line
2.33.0
#54 lore raw

PATCH 23/30 tcg/loongarch: Add softmmu load/store helpers, implement qemu_ld/qemu_st ops

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target-con-set.h |   2 +
 tcg/loongarch/tcg-target.c.inc     | 344 +++++++++++++++++++++++++++++
 2 files changed, 346 insertions(+)
tcg/loongarch/tcg-target-con-set.h +2 -0
index cdbfe9cd8d..b990da6b26 100644
@@ -17,7 +17,9 @@
 C_O0_I1(r)
 C_O0_I2(rZ, r)
 C_O0_I2(rZ, rZ)
+C_O0_I2(LZ, L)
 C_O1_I1(r, r)
+C_O1_I1(r, L)
 C_O1_I2(r, r, r)
 C_O1_I2(r, r, ri)
 C_O1_I2(r, r, rI)
tcg/loongarch/tcg-target.c.inc +344 -0

368 lines not on this page · raw message

Signature · 1 line
2.33.0
#55 lore raw

Re: [PATCH 23/30] tcg/loongarch: Add softmmu load/store helpers, implement qemu_ld/qemu_st ops

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 610

Quoted 6 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +6 -0
+static void tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
+{
+    tcg_out_opc_b(s, 0);
+    bool ok = reloc_sd10k16(s->code_ptr - 1, target);
+    tcg_debug_assert(ok);
+}

Hmm. This is an existing bug in tcg/riscv/. We should have no asserts on relocations being in range. We should always be able to tell our caller that the relocation failed, and we'll try again with a smaller TB.

In this case, return the result of reloc_sd10k16 and ...

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 713

Quoted 4 lines
tcg/loongarch/tcg-target.c.inc +4 -0
+
+    tcg_out_goto(s, l->raddr);
+    return true;
+}

... return the result of tcg_out_goto.

Quoted 5 lines
tcg/loongarch/tcg-target.c.inc +5 -0
+static void tcg_out_tlb_load(TCGContext *s, TCGReg addrl, TCGMemOpIdx oi,
+                             tcg_insn_unit **label_ptr, bool is_load)
+{
+    MemOp opc = get_memop(oi);
+    unsigned s_bits = opc & MO_SIZE;

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 626

Quoted 10 lines
tcg/loongarch/tcg-target.c.inc +10 -0
+    unsigned a_bits = get_alignment_bits(opc);
+    tcg_target_long compare_mask;
+    int mem_index = get_mmuidx(oi);
+    int fast_ofs = TLB_MASK_TABLE_OFS(mem_index);
+    int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
+    int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
+    TCGReg mask_base = TCG_AREG0, table_base = TCG_AREG0;
+
+    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, mask_base, mask_ofs);
+    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, table_base, table_ofs);

I think we can eliminate the mask_base and table_base variables now. This dates from the TCG_TARGET_TLB_DISPLACEMENT_BITS thing, where we would need to compute an intermediate offset, and adjust these base registers.

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 647

Quoted 4 lines
tcg/loongarch/tcg-target.c.inc +4 -0
+    /* Clear the non-page, non-alignment bits from the address.  */
+    compare_mask = (tcg_target_long)TARGET_PAGE_MASK | ((1 << a_bits) - 1);
+    if (compare_mask == sextreg(compare_mask, 0, 12)) {
+        tcg_out_opc_andi(s, TCG_REG_TMP1, addrl, compare_mask);

LoongArch uses an unsigned mask for andi, not signed. The immediate case will never match for LoongArch.

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 690

Quoted 8 lines
tcg/loongarch/tcg-target.c.inc +8 -0
+static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
+{
+    TCGMemOpIdx oi = l->oi;
+    MemOp opc = get_memop(oi);
+    TCGReg a0 = tcg_target_call_iarg_regs[0];
+    TCGReg a1 = tcg_target_call_iarg_regs[1];
+    TCGReg a2 = tcg_target_call_iarg_regs[2];
+    TCGReg a3 = tcg_target_call_iarg_regs[3];

Drop these, since you've already named TCG_REG_A0 etc.

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 695

Quoted 5 lines
tcg/loongarch/tcg-target.c.inc +5 -0
+
+    /* We don't support oversize guests */
+    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
+        g_assert_not_reached();
+    }

This is redundant with TCG_TARGET_REG_BITS == 64.

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 709

Quoted 2 lines
tcg/loongarch/tcg-target.c.inc +2 -0
+    tcg_out_call(s, qemu_ld_helpers[opc & MO_SSIZE]);
+    tcg_out_mov(s, (opc & MO_SIZE) == MO_64, l->datalo_reg, a0);

Because you have single-insn sign-extend instructions, it's better to always call the unsigned load function, and then sign-extend here. See the aarch64 version.

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 749

Quoted 11 lines
tcg/loongarch/tcg-target.c.inc +11 -0
+    tcg_out_mov(s, TCG_TYPE_PTR, a2, l->datalo_reg);
+    switch (s_bits) {
+    case MO_8:
+        tcg_out_ext8u(s, a2, a2);
+        break;
+    case MO_16:
+        tcg_out_ext16u(s, a2, a2);
+        break;
+    default:
+        break;
+    }

Do you have a pointer to the LoongArch ABI? Do 32-bit values need to be sign- or zero-extended in the call arguments?

Anyway, merge the move and extend.

Quoted 1 line

+ tcg_out_movi(s, TCG_TYPE_PTR, a4, (tcg_target_long)l->raddr);

Oh, just FYI, this is another case where movi wants to handle pc-relative addresses.

Review

Quoted 2 lines
(quoted diff) +2 -0
+    if (guest_base == 0) {
+        tcg_out_opc_add_d(s, base, addr_regl, TCG_REG_ZERO);

Don't add zero. RISCV bug that's been fixed recently.

tcg/loongarch/tcg-target.c.inc static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, · line 835

Quoted 7 lines
tcg/loongarch/tcg-target.c.inc +7 -0
+static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo,
+                                   TCGReg base, MemOp opc)
+{
+    /* Byte swapping is left to middle-end expansion.  */
+    tcg_debug_assert((opc & MO_BSWAP) == 0);
+
+    switch (opc & (MO_SSIZE)) {

MO_SIZE here.

Quoted 1 line

+static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)

Shouldn't need is_64 argument for store. It's only relevant to load so that TCG_TYPE_I32 values are always sign-extended in the host register.

r~

#56 lore raw

PATCH 24/30 tcg/loongarch: Implement tcg_target_qemu_prologue

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 66 ++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
tcg/loongarch/tcg-target.c.inc +66 -0
index 0b6f16bde0..10df007087 100644
@@ -887,6 +887,8 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
  * Entry-points
  */
 
+static const tcg_insn_unit *tb_ret_addr;
+
 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
                        const TCGArg args[TCG_MAX_OP_ARGS],
                        const int const_args[TCG_MAX_OP_ARGS])
@@ -1401,3 +1403,67 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
         g_assert_not_reached();
     }
 }
+
+static const int tcg_target_callee_save_regs[] = {
+    TCG_REG_S0,     /* used for the global env (TCG_AREG0) */
+    TCG_REG_S1,
+    TCG_REG_S2,
+    TCG_REG_S3,
+    TCG_REG_S4,
+    TCG_REG_S5,
+    TCG_REG_S6,
+    TCG_REG_S7,
+    TCG_REG_S8,
+    TCG_REG_S9,
+    TCG_REG_RA,     /* should be last for ABI compliance */
+};
+
+/* Stack frame parameters.  */
+#define REG_SIZE   (TCG_TARGET_REG_BITS / 8)
+#define SAVE_SIZE  ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
+#define TEMP_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
+#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
+                     + TCG_TARGET_STACK_ALIGN - 1) \
+                    & -TCG_TARGET_STACK_ALIGN)
+#define SAVE_OFS   (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
+
+/* We're expecting to be able to use an immediate for frame allocation.  */
+QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
+
+/* Generate global QEMU prologue and epilogue code */
+static void tcg_target_qemu_prologue(TCGContext *s)
+{
+    int i;
+
+    tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
+
+    /* TB prologue */
+    tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
+    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
+        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
+                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
+    }
+
+#if !defined(CONFIG_SOFTMMU)
+    tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
+    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
+#endif
+
+    /* Call generated code */
+    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
+    tcg_out_opc_jirl(s, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
+
+    /* Return path for goto_ptr. Set return value to 0 */
+    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
+    tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
+
+    /* TB epilogue */
+    tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
+    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
+        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
+                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
+    }
+
+    tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
+    tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_RA, 0);
+}
Signature · 1 line
2.33.0
Reviewed-by on v1 24/30
#57 lore raw

Re: [PATCH 24/30] tcg/loongarch: Implement tcg_target_qemu_prologue

tcg/loongarch/tcg-target.c.inc static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) · line 1450

Quoted 4 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +4 -0
+#if !defined(CONFIG_SOFTMMU)
+    tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
+    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
+#endif

Should test for guest_base == 0. This is common for a 64-bit guest.

Otherwise,

r~

#58 lore raw

PATCH 25/30 tcg/loongarch: Implement exit_tb/goto_tb

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tcg/loongarch/tcg-target.c.inc +19 -0
index 10df007087..585bf8dba0 100644
@@ -899,6 +899,25 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
     int c2 = const_args[2];
 
     switch (opc) {
+    case INDEX_op_exit_tb:
+        /* Reuse the zeroing that exists for goto_ptr.  */
+        if (a0 == 0) {
+            tcg_out_call_int(s, tcg_code_gen_epilogue, true);
+        } else {
+            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
+            tcg_out_call_int(s, tb_ret_addr, true);
+        }
+        break;
+
+    case INDEX_op_goto_tb:
+        assert(s->tb_jmp_insn_offset == 0);
+        /* indirect jump method */
+        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO,
+                   (uintptr_t)(s->tb_jmp_target_addr + a0));
+        tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0);
+        set_jmp_reset_offset(s, a0);
+        break;
+
     case INDEX_op_mb:
         tcg_out_mb(s, a0);
         break;
Signature · 1 line
2.33.0
#60 lore raw

PATCH 26/30 tcg/loongarch: Implement tcg_target_init

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
tcg/loongarch/tcg-target.c.inc +29 -0
index 585bf8dba0..107682e1fa 100644
@@ -1486,3 +1486,32 @@ static void tcg_target_qemu_prologue(TCGContext *s)
     tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
     tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_RA, 0);
 }
+
+static void tcg_target_init(TCGContext *s)
+{
+    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
+    if (TCG_TARGET_REG_BITS == 64) {
+        tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
+    }
+
+    tcg_target_call_clobber_regs = -1u;
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
+    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
+
+    s->reserved_regs = 0;
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
+    tcg_regset_set_reg(s->reserved_regs, TCG_REG_RESERVED);
+}
Signature · 1 line
2.33.0
Reviewed-by on v1 26/30
#61 lore raw

Re: [PATCH 26/30] tcg/loongarch: Implement tcg_target_init

tcg/loongarch/tcg-target.c.inc static void tcg_target_qemu_prologue(TCGContext *s) · line 1497

Quoted 8 lines · WANG Xuerui
tcg/loongarch/tcg-target.c.inc +8 -0
+static void tcg_target_init(TCGContext *s)
+{
+    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
+    if (TCG_TARGET_REG_BITS == 64) {
+        tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
+    }
+
+    tcg_target_call_clobber_regs = -1u;

In all 3 places, use your ALL_GENERAL_REGS constant.

Otherwise,

r~

#62 lore raw

PATCH 27/30 tcg/loongarch: Register the JIT

In reply to WANG Xuerui
---
 tcg/loongarch/tcg-target.c.inc | 44 ++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
tcg/loongarch/tcg-target.c.inc +44 -0
index 107682e1fa..59adc92d26 100644
@@ -1515,3 +1515,47 @@ static void tcg_target_init(TCGContext *s)
     tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
     tcg_regset_set_reg(s->reserved_regs, TCG_REG_RESERVED);
 }
+
+typedef struct {
+    DebugFrameHeader h;
+    uint8_t fde_def_cfa[4];
+    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
+} DebugFrame;
+
+#define ELF_HOST_MACHINE EM_LOONGARCH
+
+static const DebugFrame debug_frame = {
+    .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
+    .h.cie.id = -1,
+    .h.cie.version = 1,
+    .h.cie.code_align = 1,
+    .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
+    .h.cie.return_column = TCG_REG_RA,
+
+    /* Total FDE size does not include the "len" member.  */
+    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
+
+    .fde_def_cfa = {
+        12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ...  */
+        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
+        (FRAME_SIZE >> 7)
+    },
+    .fde_reg_ofs = {
+        0x80 + 23, 11,                  /* DW_CFA_offset, s0,  -88 */
+        0x80 + 24, 10,                  /* DW_CFA_offset, s1,  -80 */
+        0x80 + 25, 9,                   /* DW_CFA_offset, s2,  -72 */
+        0x80 + 26, 8,                   /* DW_CFA_offset, s3,  -64 */
+        0x80 + 27, 7,                   /* DW_CFA_offset, s4,  -56 */
+        0x80 + 28, 6,                   /* DW_CFA_offset, s5,  -48 */
+        0x80 + 29, 5,                   /* DW_CFA_offset, s6,  -40 */
+        0x80 + 30, 4,                   /* DW_CFA_offset, s7,  -32 */
+        0x80 + 31, 3,                   /* DW_CFA_offset, s8,  -24 */
+        0x80 + 22, 2,                   /* DW_CFA_offset, s9,  -16 */
+        0x80 + 1 , 1,                   /* DW_CFA_offset, ra,  -8 */
+    }
+};
+
+void tcg_register_jit(const void *buf, size_t buf_size)
+{
+    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
+}
Signature · 1 line
2.33.0
#64 lore raw

PATCH 28/30 configure, meson.build: Mark support for 64-bit LoongArch hosts

In reply to WANG Xuerui
---
 configure   | 4 +++-
 meson.build | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
configure +3 -1
index 1043ccce4f..f1bc85e71b 100755
@@ -659,6 +659,8 @@ elif check_define __arm__ ; then
   cpu="arm"
 elif check_define __aarch64__ ; then
   cpu="aarch64"
+elif check_define __loongarch64 ; then
+  cpu="loongarch64"
 else
   cpu=$(uname -m)
 fi
@@ -667,7 +669,7 @@ ARCH=
 # Normalise host CPU name and set ARCH.
 # Note that this case should only have supported host CPUs, not guests.
 case "$cpu" in
-  ppc|ppc64|s390x|sparc64|x32|riscv32|riscv64)
+  ppc|ppc64|s390x|sparc64|x32|riscv32|riscv64|loongarch64)
   ;;
   ppc64le)
     ARCH="ppc64"
meson.build +3 -1
index 2711cbb789..fb3befead5 100644
@@ -57,7 +57,7 @@ python = import('python').find_installation()
 
 supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux']
 supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64',
-  'arm', 'aarch64', 'mips', 'mips64', 'sparc', 'sparc64']
+  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc', 'sparc64']
 
 cpu = host_machine.cpu_family()
 targetos = host_machine.system()
@@ -269,6 +269,8 @@ if not get_option('tcg').disabled()
     tcg_arch = 's390'
   elif config_host['ARCH'] in ['x86_64', 'x32']
     tcg_arch = 'i386'
+  elif config_host['ARCH'] == 'loongarch64'
+    tcg_arch = 'loongarch'
   elif config_host['ARCH'] == 'ppc64'
     tcg_arch = 'ppc'
   elif config_host['ARCH'] in ['riscv32', 'riscv64']
Signature · 1 line
2.33.0
#65 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Quoted 42 lines · WANG Xuerui
---
  configure   | 4 +++-
  meson.build | 4 +++-
  2 files changed, 6 insertions(+), 2 deletions(-)
configure +3 -1
index 1043ccce4f..f1bc85e71b 100755
@@ -659,6 +659,8 @@ elif check_define __arm__ ; then
    cpu="arm"
  elif check_define __aarch64__ ; then
    cpu="aarch64"
+elif check_define __loongarch64 ; then
+  cpu="loongarch64"
  else
    cpu=$(uname -m)
  fi
@@ -667,7 +669,7 @@ ARCH=
  # Normalise host CPU name and set ARCH.
  # Note that this case should only have supported host CPUs, not guests.
  case "$cpu" in
-  ppc|ppc64|s390x|sparc64|x32|riscv32|riscv64)
+  ppc|ppc64|s390x|sparc64|x32|riscv32|riscv64|loongarch64)
    ;;
    ppc64le)
      ARCH="ppc64"
meson.build +1 -1
index 2711cbb789..fb3befead5 100644
@@ -57,7 +57,7 @@ python = import('python').find_installation()
 
  supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux']
  supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64',
-  'arm', 'aarch64', 'mips', 'mips64', 'sparc', 'sparc64']
+  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc', 'sparc64']
 
  cpu = host_machine.cpu_family()
  targetos = host_machine.system()

meson.build if not get_option('tcg').disabled() · line 273

Quoted 5 lines
meson.build +2 -0
@@ -269,6 +269,8 @@ if not get_option('tcg').disabled()
      tcg_arch = 's390'
    elif config_host['ARCH'] in ['x86_64', 'x32']
      tcg_arch = 'i386'
+  elif config_host['ARCH'] == 'loongarch64'
+    tcg_arch = 'loongarch'

Be consistent with loongarch or loongarch64 everywhere.

If there's no loongarch32, and never will be, then there's probably no point in keeping the '64' suffix.

r~

#66 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Hi Richard,

Quoted 50 lines · Richard Henderson
Quoted 41 lines · WANG Xuerui

---
  configure   | 4 +++-
  meson.build | 4 +++-

  2 files changed, 6 insertions(+), 2 deletions(-)
configure +0 -0
index 1043ccce4f..f1bc85e71b 100755
@@ -659,6 +659,8 @@ elif check_define __arm__ ; then

    cpu="arm"
  elif check_define __aarch64__ ; then
    cpu="aarch64"
+elif check_define __loongarch64 ; then
+  cpu="loongarch64"
  else
    cpu=$(uname -m)
  fi

configure +0 -0
@@ -667,7 +669,7 @@ ARCH=
   # Normalise host CPU name and set ARCH.
   # Note that this case should only have supported host CPUs, not guests.

  case "$cpu" in

-  ppc|ppc64|s390x|sparc64|x32|riscv32|riscv64)
+  ppc|ppc64|s390x|sparc64|x32|riscv32|riscv64|loongarch64)

    ;;
    ppc64le)
      ARCH="ppc64"

meson.build +0 -0
index 2711cbb789..fb3befead5 100644
@@ -57,7 +57,7 @@ python = import('python').find_installation()

    supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux'] supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64', -  'arm', 'aarch64', 'mips', 'mips64', 'sparc', 'sparc64']
+  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc',
'sparc64']
    cpu = host_machine.cpu_family()
  targetos = host_machine.system()

meson.build +0 -0
@@ -269,6 +269,8 @@ if not get_option('tcg').disabled()
Quoted 5 lines

      tcg_arch = 's390'
    elif config_host['ARCH'] in ['x86_64', 'x32']
      tcg_arch = 'i386'
+  elif config_host['ARCH'] == 'loongarch64'
+    tcg_arch = 'loongarch'

Be consistent with loongarch or loongarch64 everywhere.

Quoted 1 line

If there's no loongarch32, and never will be, then there's probably no point in keeping the '64' suffix.

The loongarch32 tuple will most certainly come into existence some time in the future, but probably bare-metal-only and without a Linux port AFAIK. That's a point the Loongson people and I didn't communicate well, apologizes for that. (While we're at it, the reserved "loongarchx32" which is x32/n32-like, most likely will never exist.)

So should I drop the explicit probing for __loongarch64, instead just probe for __loongarch__ and later #error out the non-__loongarch64 cases individually?

Quoted 3 lines

r~

#67 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Quoted 11 lines · WANG Xuerui
Quoted 8 lines · Richard Henderson
Quoted 5 lines · WANG Xuerui

---
  configure   | 4 +++-
  meson.build | 4 +++-

  2 files changed, 6 insertions(+), 2 deletions(-)

If there's no loongarch32, and never will be, then there's probably no point in keeping the '64' suffix.

The loongarch32 tuple will most certainly come into existence some time in the future, but probably bare-metal-only and without a Linux port AFAIK. That's a point the Loongson people and I didn't communicate well, apologizes for that. (While we're at it, the reserved "loongarchx32" which is x32/n32-like, most likely will never exist.)

Are you trying to beat MIPS at their ABI complexity? /s

#68 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Hi Philippe,

Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 7 lines · WANG Xuerui
Quoted 6 lines · Richard Henderson
Quoted 5 lines · WANG Xuerui

---
  configure   | 4 +++-
  meson.build | 4 +++-

  2 files changed, 6 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

If there's no loongarch32, and never will be, then there's probably no point in keeping the '64' suffix.

The loongarch32 tuple will most certainly come into existence some time in the future, but probably bare-metal-only and without a Linux port AFAIK. That's a point the Loongson people and I didn't communicate well, apologizes for that. (While we're at it, the reserved "loongarchx32" which is x32/n32-like, most likely will never exist.)

Are you trying to beat MIPS at their ABI complexity? /s

Hah, I'm not Loongson employee so maybe I'm not in the best position to answer this ;-)

But from an outsider's perspective, the Loongson people obviously reserved things upfront like a multi-millionaire, then suddenly realized they only have ~500 people on board, developers even less; so they did the Right Thing(TM), only later, to drop x32 altogether and focus their energy on bare-metal use cases for their 32-bit-only chips.

Plus, LoongArch is strictly little-endian, and only one baseline ISA revision is published so far, so IMO it can never beat MIPS in terms of combinatorial ABI possibilities. Maybe RISC-V have a chance? ;-)

#69 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

In reply to WANG Xuerui
Quoted 1 line · WANG Xuerui

The loongarch32 tuple will most certainly come into existence some time in the future, but probably bare-metal-only and without a Linux port AFAIK.

Ok, I'll bear that in mind when considering target/loongarch/.

Quoted 1 line

So should I drop the explicit probing for __loongarch64, instead just probe for __loongarch__ and later #error out the non-__loongarch64 cases individually?

I'm ok with checking the __loongarch64 define, but I thing ARCH=loongarch is sufficient. That name will apply to linux-user/host/$ARCH/ and tcg/$ARCH/.

r~

#70 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Hi Richard,

Quoted 6 lines · Richard Henderson
Quoted 1 line · WANG Xuerui

So should I drop the explicit probing for __loongarch64, instead just probe for __loongarch__ and later #error out the non-__loongarch64 cases individually?

I'm ok with checking the __loongarch64 define, but I thing ARCH=loongarch is sufficient. That name will apply to linux-user/host/$ARCH/ and tcg/$ARCH/.

I just dug deeper into this while waiting for compilations; indeed the cpu variable must be "loongarch64" but ARCH could be just "loongarch". The $cpu is shoved directly into the meson cross file as CPU family name, for which only "loongarch64" is valid [1]. I'll keep probing for __loongarch64 but just transform the ARCH value.

[1]: https://mesonbuild.com/Reference-tables.html#cpu-families

#71 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Quoted 11 lines · WANG Xuerui

Hi Richard,

Quoted 6 lines · Richard Henderson
Quoted 1 line · WANG Xuerui

So should I drop the explicit probing for __loongarch64, instead just probe for __loongarch__ and later #error out the non-__loongarch64 cases individually?

I'm ok with checking the __loongarch64 define, but I thing ARCH=loongarch is sufficient. That name will apply to linux-user/host/$ARCH/ and tcg/$ARCH/.

I just dug deeper into this while waiting for compilations; indeed the cpu variable must be "loongarch64" but ARCH could be just "loongarch". The $cpu is shoved directly into the meson cross file as CPU family name, for which only "loongarch64" is valid [1]. I'll keep probing for __loongarch64 but just transform the ARCH value.

Ah wait, it seems the used value is $ARCH... But some changes around the $cpu/$ARCH handling are necessary anyway. Sorry for the noise!

Quoted 4 lines

[1]: https://mesonbuild.com/Reference-tables.html#cpu-families

#72 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

In reply to Richard Henderson
Quoted 8 lines · Richard Henderson
Quoted 1 line · WANG Xuerui

Be consistent with loongarch or loongarch64 everywhere.

If there's no loongarch32, and never will be, then there's probably no point in keeping the '64' suffix.

What does Linux 'uname -m' call the architecture, and what is the name in the gcc triplet? Generally I think we should prefer to follow those precedents (which hopefully don't point in different directions) rather than making up our own architecture names.

thanks
-- PMM

#73 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Quoted 6 lines · Peter Maydell
Quoted 3 lines · Richard Henderson
Quoted 1 line · WANG Xuerui
Quoted 7 lines
Quoted 4 lines

Be consistent with loongarch or loongarch64 everywhere.

If there's no loongarch32, and never will be, then there's probably no point in keeping the '64' suffix.

What does Linux 'uname -m' call the architecture, and what is the name in the gcc triplet?

The kernel will report

arch/loongarch/Makefile:UTS_MACHINE := loongarch64

and it appears that the toolchain is using loongarch64 as well.

So, Xuerui, I think there's your answer...

r~

#74 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

In reply to Peter Maydell

Hi Peter,

Quoted 4 lines · Peter Maydell
Quoted 2 lines · Richard Henderson
Quoted 1 line · WANG Xuerui
Quoted 8 lines
Quoted 4 lines

Be consistent with loongarch or loongarch64 everywhere.

If there's no loongarch32, and never will be, then there's probably no point in keeping the '64' suffix.

What does Linux 'uname -m' call the architecture, and what is the name in the gcc triplet? Generally I think we should prefer to follow those precedents (which hopefully don't point in different directions) rather than making up our own architecture names.

uname -m says "loongarch64", the GNU triple arch name is also "loongarch64". I'd say it's similar to the situation of RISC-V or MIPS; except that a Linux port to the 32-bit variant of LoongArch might not happen, precluding a QEMU port.

I think cpu=loongarch64 but ARCH=loongarch should be okay; at least it's better than, say, the Go language or Gentoo, where this architecture is named "loong64" and "loong"; or the binutils internals where it's "larch".

Quoted 3 lines

thanks
-- PMM

#75 lore raw

Re: [PATCH 28/30] configure, meson.build: Mark support for 64-bit LoongArch hosts

Quoted 1 line · WANG Xuerui

I think cpu=loongarch64 but ARCH=loongarch should be okay...

Make it easier on yourself and keep them the same.

r~

#76 lore raw

PATCH 29/30 linux-user: Add host dependency for 64-bit LoongArch

In reply to WANG Xuerui

Currently nothing special is needed for LoongArch hosts to work, so only leave a placeholder there.

---
 linux-user/host/loongarch64/hostdep.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 linux-user/host/loongarch64/hostdep.h
linux-user/host/loongarch64/hostdep.h new file +11 -0
new file mode 100644
index 0000000000..4e55695155
@@ -0,0 +1,11 @@
+/*
+ * hostdep.h : things which are dependent on the host architecture
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef LOONGARCH64_HOSTDEP_H
+#define LOONGARCH64_HOSTDEP_H
+
+#endif
Signature · 1 line
2.33.0
#77 lore raw

Re: [PATCH 29/30] linux-user: Add host dependency for 64-bit LoongArch

Quoted 17 lines · WANG Xuerui

Currently nothing special is needed for LoongArch hosts to work, so only leave a placeholder there.

---
  linux-user/host/loongarch64/hostdep.h | 11 +++++++++++
  1 file changed, 11 insertions(+)

create mode 100644 linux-user/host/loongarch64/hostdep.h

linux-user/host/loongarch64/hostdep.h new file +1 -0
new file mode 100644
index 0000000000..4e55695155
@@ -0,0 +1,11 @@
+/*

linux-user/host/loongarch64/hostdep.h line 11

Quoted 10 lines
linux-user/host/loongarch64/hostdep.h new file +10 -0
+ * hostdep.h : things which are dependent on the host architecture
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef LOONGARCH64_HOSTDEP_H
+#define LOONGARCH64_HOSTDEP_H
+
+#endif

This is not true. You'll need to write safe-syscall.inc.S for loongarch. Currently we have a fallback, in linux-user/safe-syscall.h, but this is clearly marked as a bug. I plan to drop this fallback very shortly, as all supported hosts have now filled this in.

r~

#78 lore raw

PATCH 30/30 accel/tcg/user-exec: Implement CPU-specific signal handler for LoongArch hosts

In reply to WANG Xuerui
---
 accel/tcg/user-exec.c | 83 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
accel/tcg/user-exec.c +83 -0
index 8fed542622..0f85062e61 100644
@@ -878,6 +878,89 @@ int cpu_signal_handler(int host_signum, void *pinfo,
     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
 }
 
+#elif defined(__loongarch__)
+
+/*
+ * This logic is bitness-agnostic, so the generic __loongarch__ guard is used
+ * instead of explicit ones like __loongarch64.
+ */
+
+int cpu_signal_handler(int host_signum, void *pinfo,
+                       void *puc)
+{
+    siginfo_t *info = pinfo;
+    ucontext_t *uc = puc;
+    greg_t pc = uc->uc_mcontext.__pc;
+    uint32_t insn = *(uint32_t *)pc;
+    int is_write = 0;
+
+    /* Detect store by reading the instruction at the program counter.  */
+    switch ((insn >> 26) & 0b111111) {
+    case 0b001000: /* {ll,sc}.[wd] */
+        switch ((insn >> 24) & 0b11) {
+        case 0b01: /* sc.w */
+        case 0b11: /* sc.d */
+            is_write = 1;
+            break;
+        }
+        break;
+    case 0b001001: /* {ld,st}ox4.[wd] ({ld,st}ptr.[wd]) */
+        switch ((insn >> 24) & 0b11) {
+        case 0b01: /* stox4.w (stptr.w) */
+        case 0b11: /* stox4.d (stptr.d) */
+            is_write = 1;
+            break;
+        }
+        break;
+    case 0b001010: /* {ld,st}.* family */
+        switch ((insn >> 22) & 0b1111) {
+        case 0b0100: /* st.b */
+        case 0b0101: /* st.h */
+        case 0b0110: /* st.w */
+        case 0b0111: /* st.d */
+        case 0b1101: /* fst.s */
+        case 0b1111: /* fst.d */
+            is_write = 1;
+            break;
+        }
+        break;
+    case 0b001110: /* indexed, atomic, bounds-checking memory operations */
+        uint32_t sel = (insn >> 15) & 0b11111111111;
+
+        switch (sel) {
+        case 0b00000100000: /* stx.b */
+        case 0b00000101000: /* stx.h */
+        case 0b00000110000: /* stx.w */
+        case 0b00000111000: /* stx.d */
+        case 0b00001110000: /* fstx.s */
+        case 0b00001111000: /* fstx.d */
+        case 0b00011101100: /* fstgt.s */
+        case 0b00011101101: /* fstgt.d */
+        case 0b00011101110: /* fstle.s */
+        case 0b00011101111: /* fstle.d */
+        case 0b00011111000: /* stgt.b */
+        case 0b00011111001: /* stgt.h */
+        case 0b00011111010: /* stgt.w */
+        case 0b00011111011: /* stgt.d */
+        case 0b00011111100: /* stle.b */
+        case 0b00011111101: /* stle.h */
+        case 0b00011111110: /* stle.w */
+        case 0b00011111111: /* stle.d */
+            is_write = 1;
+            break;
+        default:
+            /* test for am* instruction range */
+            if (0b00011000000 <= sel && sel <= 0b00011100011) {
+                is_write = 1;
+            }
+            break;
+        }
+        break;
+    }
+
+    return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
+}
+
 #else
 
 #error host CPU specific signal handler needed
Signature · 1 line
2.33.0
#79 lore raw

Re: [PATCH 30/30] accel/tcg/user-exec: Implement CPU-specific signal handler for LoongArch hosts

Quoted 12 lines · WANG Xuerui
---
  accel/tcg/user-exec.c | 83 +++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 83 insertions(+)
accel/tcg/user-exec.c +0 -0
index 8fed542622..0f85062e61 100644
@@ -878,6 +878,89 @@ int cpu_signal_handler(int host_signum, void *pinfo,
      return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
  }

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 889

Quoted 10 lines
accel/tcg/user-exec.c +9 -0
 
+#elif defined(__loongarch__)
+
+/*
+ * This logic is bitness-agnostic, so the generic __loongarch__ guard is used
+ * instead of explicit ones like __loongarch64.
+ */
+
+int cpu_signal_handler(int host_signum, void *pinfo,
+                       void *puc)

Looks ok, as far as it goes. Similar comments about loongarch64 vs loongarch32 vs loongarch.

Also have a look at

https://lore.kernel.org/qemu-devel/[email protected]/

r~