Skip to contentSkip to conversation

target/xtensa: use libisa for instruction decoding

PATCH In review Max Filippov 2 participants 5 messages Jan 9, 2018 – Jan 18, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 target/xtensa: use libisa for instruction decoding

Replace manual opcode analysis with libisa-based code. This makes it possible to support variable-encoding instructions of the core ISA, like const16, and will allow to support advanced Xtensa features, like FLIX and TIE.

---
Changes v3->v4:
- Don't use g_malloc_n, use g_new instead.

Changes v2->v3:
- drop no longer used function option_bits_enabled.

 target/xtensa/cpu.h       |    3 +
 target/xtensa/helper.c    |   37 +
 target/xtensa/translate.c | 2228 ++-------------------------------------------
 3 files changed, 124 insertions(+), 2144 deletions(-)
target/xtensa/cpu.h +3 -0
index 9a34cc825e96..e93bbb3c6d1e 100644
@@ -392,6 +392,9 @@ struct XtensaConfig {
     uint32_t configid[2];
 
     void *isa_internal;
+    xtensa_isa isa;
+    XtensaOpcodeOps **opcode_ops;
+    const XtensaOpcodeTranslators **opcode_translators;
 
     uint32_t clock_freq_khz;
 
target/xtensa/helper.c +37 -0
index 216f1988aac5..5009fecedcb0 100644
@@ -52,10 +52,47 @@ static void xtensa_core_class_init(ObjectClass *oc, void *data)
     cc->gdb_num_core_regs = config->gdb_regmap.num_regs;
 }
 
+static void init_libisa(XtensaConfig *config)
+{
+    unsigned i, j;
+    unsigned opcodes;
+
+    config->isa = xtensa_isa_init(config->isa_internal, NULL, NULL);
+    assert(xtensa_isa_maxlength(config->isa) <= MAX_INSN_LENGTH);
+    opcodes = xtensa_isa_num_opcodes(config->isa);
+    config->opcode_ops = g_new(XtensaOpcodeOps *, opcodes);
+
+    for (i = 0; i < opcodes; ++i) {
+        const char *opc_name = xtensa_opcode_name(config->isa, i);
+        XtensaOpcodeOps *ops = NULL;
+
+        assert(xtensa_opcode_num_operands(config->isa, i) <= MAX_OPCODE_ARGS);
+        if (!config->opcode_translators) {
+            ops = xtensa_find_opcode_ops(&xtensa_core_opcodes, opc_name);
+        } else {
+            for (j = 0; !ops && config->opcode_translators[j]; ++j) {
+                ops = xtensa_find_opcode_ops(config->opcode_translators[j],
+                                             opc_name);
+            }
+        }
+#ifdef DEBUG
+        if (ops == NULL) {
+            fprintf(stderr,
+                    "opcode translator not found for %s's opcode '%s'\n",
+                    config->name, opc_name);
+        }
+#endif
+        config->opcode_ops[i] = ops;
+    }
+}
+
 void xtensa_finalize_config(XtensaConfig *config)
 {
     unsigned i, n = 0;
 
+    if (config->isa_internal) {
+        init_libisa(config);
+    }
     if (config->gdb_regmap.num_regs) {
         return;
     }
target/xtensa/translate.c +84 -2144

2,298 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw
Quoted 6 lines · Max Filippov

Replace manual opcode analysis with libisa-based code. This makes it possible to support variable-encoding instructions of the core ISA, like const16, and will allow to support advanced Xtensa features, like FLIX and TIE.

Hi. Coverity (CID 1385146) complains about this bit of code:

target/xtensa/translate.c static TCGv_i32 gen_mac16_m(TCGv_i32 v, bool hi, bool is_unsigned) · line 1005

Quoted 5 lines
target/xtensa/translate.c +5 -0
+        opnds = xtensa_opcode_num_operands(isa, opc);
+
+        for (opnd = vopnd = 0; opnd < opnds; ++opnd) {
+            if (xtensa_operand_is_visible(isa, opc, opnd)) {
+                uint32_t v;

because xtensa_opcode_num_operands() can return -1 (if the CHECK_OPCODE() fails), and then we will try to use -1 as an upper bound for this loop and it will loop for a very long time.

CID 1385148 is similar but for the outer loop where we do "slots = xtensa_format_num_slots()" and then use slots as a loop bound without checking whether we got back -1.

thanks
-- PMM

#2 lore raw
Quoted 16 lines · Peter Maydell
Quoted 6 lines · Max Filippov

Replace manual opcode analysis with libisa-based code. This makes it possible to support variable-encoding instructions of the core ISA, like const16, and will allow to support advanced Xtensa features, like FLIX and TIE.

Hi. Coverity (CID 1385146) complains about this bit of code:

target/xtensa/translate.c static TCGv_i32 gen_mac16_m(TCGv_i32 v, bool hi, bool is_unsigned) · line 1005

Quoted 5 lines
target/xtensa/translate.c +5 -0
+        opnds = xtensa_opcode_num_operands(isa, opc);
+
+        for (opnd = vopnd = 0; opnd < opnds; ++opnd) {
+            if (xtensa_operand_is_visible(isa, opc, opnd)) {
+                uint32_t v;
Quoted 8 lines

because xtensa_opcode_num_operands() can return -1 (if the CHECK_OPCODE() fails), and then we will try to use -1 as an upper bound for this loop and it will loop for a very long time.

CID 1385148 is similar but for the outer loop where we do "slots = xtensa_format_num_slots()" and then use slots as a loop bound without checking whether we got back -1.

In both cases the check inside libisa should not fail, because the caller just made sure that opcode or format is valid. But I see, these checks are different. I guess the easiest fix for that is to make opnd, opnds, slot and slots signed.

Signature · 2 lines
Thanks.
-- Max
#3 lore raw
Quoted 14 lines · Max Filippov
Quoted 13 lines · Peter Maydell

Hi. Coverity (CID 1385146) complains about this bit of code:

target/xtensa/translate.c static TCGv_i32 gen_mac16_m(TCGv_i32 v, bool hi, bool is_unsigned) · line 1005

Quoted 5 lines
target/xtensa/translate.c +5 -0
+        opnds = xtensa_opcode_num_operands(isa, opc);
+
+        for (opnd = vopnd = 0; opnd < opnds; ++opnd) {
+            if (xtensa_operand_is_visible(isa, opc, opnd)) {
+                uint32_t v;

because xtensa_opcode_num_operands() can return -1 (if the CHECK_OPCODE() fails), and then we will try to use -1 as an upper bound for this loop and it will loop for a very long time.

Quoted 8 lines
Quoted 3 lines

CID 1385148 is similar but for the outer loop where we do "slots = xtensa_format_num_slots()" and then use slots as a loop bound without checking whether we got back -1.

In both cases the check inside libisa should not fail, because the caller just made sure that opcode or format is valid. But I see, these checks are different. I guess the easiest fix for that is to make opnd, opnds, slot and slots signed.

If you definitely know you won't get back -1 then I think putting "assert(opnds != XTENSA_UNDEFINED);" etc will express the intention and should make coverity happy.

thanks
-- PMM

#4 lore raw
Quoted 19 lines · Peter Maydell
Quoted 18 lines · Max Filippov
Quoted 16 lines · Peter Maydell

Hi. Coverity (CID 1385146) complains about this bit of code:

target/xtensa/translate.c static TCGv_i32 gen_mac16_m(TCGv_i32 v, bool hi, bool is_unsigned) · line 1005

Quoted 5 lines
target/xtensa/translate.c +5 -0
+        opnds = xtensa_opcode_num_operands(isa, opc);
+
+        for (opnd = vopnd = 0; opnd < opnds; ++opnd) {
+            if (xtensa_operand_is_visible(isa, opc, opnd)) {
+                uint32_t v;

because xtensa_opcode_num_operands() can return -1 (if the CHECK_OPCODE() fails), and then we will try to use -1 as an upper bound for this loop and it will loop for a very long time.

CID 1385148 is similar but for the outer loop where we do "slots = xtensa_format_num_slots()" and then use slots as a loop bound without checking whether we got back -1.

Quoted 8 lines
Quoted 4 lines

In both cases the check inside libisa should not fail, because the caller just made sure that opcode or format is valid. But I see, these checks are different. I guess the easiest fix for that is to make opnd, opnds, slot and slots signed.

If you definitely know you won't get back -1 then I think putting "assert(opnds != XTENSA_UNDEFINED);" etc will express the intention and should make coverity happy.

I'd rather use data types that match these function return types.

Signature · 2 lines
Thanks.
-- Max