Skip to contentSkip to conversation

Hexagon (target/hexagon) Additional instructions handled by idef-parser

PATCH In review Taylor Simpson 2 participants 3 messages May 1, 2023 – May 3, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Hexagon (target/hexagon) Additional instructions handled by idef-parser

**** Changes in v3 ****
Fix bugs exposed by dpmpyss_rnd_s0 instruction
Set correct size/signedness for constants
Test cases added to tests/tcg/hexagon/misc.c

**** Changes in v2 ****
Fix bug in imm_print identified in clang build

Currently, idef-parser skips all floating point instructions. However, there are some floating point instructions that can be handled.

The following instructions are now parsed

    F2_sfimm_p
    F2_sfimm_n
    F2_dfimm_p
    F2_dfimm_n
    F2_dfmpyll
    F2_dfmpylh

To make these instructions work, we fix some bugs in parser-helpers.c
gen_rvalue_extend
gen_cast_op
imm_print
lexer properly sets size/signedness of constants

Test cases added to tests/tcg/hexagon/fpstuff.c

---
 target/hexagon/idef-parser/parser-helpers.h |  2 +-
 target/hexagon/idef-parser/parser-helpers.c | 41 +++++++++++-----
 tests/tcg/hexagon/fpstuff.c                 | 54 +++++++++++++++++++++
 tests/tcg/hexagon/misc.c                    | 35 +++++++++++++
 target/hexagon/gen_idef_parser_funcs.py     | 10 +++-
 target/hexagon/idef-parser/idef-parser.lex  | 38 +++++++++++++--
 target/hexagon/idef-parser/idef-parser.y    |  2 -
 7 files changed, 162 insertions(+), 20 deletions(-)
target/hexagon/idef-parser/parser-helpers.h +1 -1
index 1239d23a6a..7c58087169 100644
@@ -80,7 +80,7 @@ void reg_compose(Context *c, YYLTYPE *locp, HexReg *reg, char reg_id[5]);
 
 void reg_print(Context *c, YYLTYPE *locp, HexReg *reg);
 
-void imm_print(Context *c, YYLTYPE *locp, HexImm *imm);
+void imm_print(Context *c, YYLTYPE *locp, HexValue *rvalue);
 
 void var_print(Context *c, YYLTYPE *locp, HexVar *var);
 
target/hexagon/idef-parser/parser-helpers.c +30 -11

74 lines not on this page · raw message

tests/tcg/hexagon/fpstuff.c +54 -0

72 lines not on this page · raw message

tests/tcg/hexagon/misc.c +35 -0
index e126751e3a..4c994c55bd 100644
@@ -385,6 +385,39 @@ void test_count_trailing_zeros_ones(void)
     check(ct1p(0xffffff0fffffffffULL), 36);
 }
 
+static inline int dpmpyss_rnd_s0(int x, int y)
+{
+    int res;
+    asm("%0 = mpy(%1, %2):rnd\n\t" : "=r"(res) : "r"(x), "r"(y));
+    return res;
+}
+
+void test_dpmpyss_rnd_s0(void)
+{
+    check(dpmpyss_rnd_s0(-1, 0x80000000), 1);
+    check(dpmpyss_rnd_s0(0, 0x80000000), 0);
+    check(dpmpyss_rnd_s0(1, 0x80000000), 0);
+    check(dpmpyss_rnd_s0(0x7fffffff, 0x80000000), 0xc0000001);
+    check(dpmpyss_rnd_s0(0x80000000, -1), 1);
+    check(dpmpyss_rnd_s0(-1, -1), 0);
+    check(dpmpyss_rnd_s0(0, -1), 0);
+    check(dpmpyss_rnd_s0(1, -1), 0);
+    check(dpmpyss_rnd_s0(0x7fffffff, -1), 0);
+    check(dpmpyss_rnd_s0(0x80000000, 0), 0);
+    check(dpmpyss_rnd_s0(-1, 0), 0);
+    check(dpmpyss_rnd_s0(0, 0), 0);
+    check(dpmpyss_rnd_s0(1, 0), 0);
+    check(dpmpyss_rnd_s0(-1, -1), 0);
+    check(dpmpyss_rnd_s0(0, -1), 0);
+    check(dpmpyss_rnd_s0(1, -1), 0);
+    check(dpmpyss_rnd_s0(0x7fffffff, 1), 0);
+    check(dpmpyss_rnd_s0(0x80000000, 0x7fffffff), 0xc0000001);
+    check(dpmpyss_rnd_s0(-1, 0x7fffffff), 0);
+    check(dpmpyss_rnd_s0(0, 0x7fffffff),  0);
+    check(dpmpyss_rnd_s0(1, 0x7fffffff),  0);
+    check(dpmpyss_rnd_s0(0x7fffffff, 0x7fffffff), 0x3fffffff);
+}
+
 int main()
 {
     int res;
@@ -522,6 +555,8 @@ int main()
 
     test_count_trailing_zeros_ones();
 
+    test_dpmpyss_rnd_s0();
+
     puts(err ? "FAIL" : "PASS");
     return err;
 }
target/hexagon/gen_idef_parser_funcs.py +9 -1
index afe68bdb6f..b766798ad5 100644
@@ -103,7 +103,15 @@ def main():
                 continue
             if tag.startswith("V6_"):
                 continue
-            if tag.startswith("F"):
+            if ( tag.startswith("F") and
+                 tag not in {
+                     "F2_sfimm_p",
+                     "F2_sfimm_n",
+                     "F2_dfimm_p",
+                     "F2_dfimm_n",
+                     "F2_dfmpyll",
+                     "F2_dfmpylh"
+                 }):
                 continue
             if tag.endswith("_locked"):
                 continue
target/hexagon/idef-parser/idef-parser.lex +33 -5
index 5eb8ac5a80..4b81e63a02 100644
@@ -401,12 +401,40 @@ STRING_LIT               \"(\\.|[^"\\])*\"
                            }
                            return SIGN;
                          }
-"0x"{HEX_DIGIT}+         |
-{DIGIT}+                 { yylval->rvalue.type = IMMEDIATE;
-                           yylval->rvalue.bit_width = 32;
-                           yylval->rvalue.signedness = SIGNED;
+"0x"{HEX_DIGIT}+         { uint64_t value = strtoull(yytext, NULL, 0);
+                           yylval->rvalue.type = IMMEDIATE;
                            yylval->rvalue.imm.type = VALUE;
-                           yylval->rvalue.imm.value = strtoull(yytext, NULL, 0);
+                           yylval->rvalue.imm.value = value;
+                           if (value <= INT_MAX) {
+                               yylval->rvalue.bit_width = sizeof(int) * 8;
+                               yylval->rvalue.signedness = SIGNED;
+                           } else if (value <= UINT_MAX) {
+                               yylval->rvalue.bit_width = sizeof(unsigned int) * 8;
+                               yylval->rvalue.signedness = UNSIGNED;
+                           } else if (value <= LONG_MAX) {
+                               yylval->rvalue.bit_width = sizeof(long) * 8;
+                               yylval->rvalue.signedness = SIGNED;
+                           } else if (value <= ULONG_MAX) {
+                               yylval->rvalue.bit_width = sizeof(unsigned long) * 8;
+                               yylval->rvalue.signedness = UNSIGNED;
+                           } else {
+                               g_assert_not_reached();
+                           }
+                           return IMM; }
+{DIGIT}+                 { int64_t value = strtoll(yytext, NULL, 0);
+                           yylval->rvalue.type = IMMEDIATE;
+                           yylval->rvalue.imm.type = VALUE;
+                           yylval->rvalue.imm.value = value;
+                           if (value >= INT_MIN && value <= INT_MAX) {
+                               yylval->rvalue.bit_width = sizeof(int) * 8;
+                               yylval->rvalue.signedness = SIGNED;
+                           } else if (value >= LONG_MIN && value <= LONG_MAX) {
+                               yylval->rvalue.bit_width = sizeof(long) * 8;
+                               yylval->rvalue.signedness = SIGNED;
+                           } else {
+                              printf("value = 0x%lx\n", value);
+                              g_assert_not_reached();
+                           }
                            return IMM; }
 "0x"{HEX_DIGIT}+"ULL"    |
 {DIGIT}+"ULL"            { yylval->rvalue.type = IMMEDIATE;
target/hexagon/idef-parser/idef-parser.y +0 -2
index 5444fd4749..5f3907eb28 100644
@@ -594,8 +594,6 @@ rvalue : FAIL
        | CAST rvalue
          {
              @1.last_column = @2.last_column;
-             /* Assign target signedness */
-             $2.signedness = $1.signedness;
              $$ = gen_cast_op(c, &@1, &$2, $1.bit_width, $1.signedness);
          }
        | rvalue EQ rvalue
Signature · 1 line
2.25.1
Tested-by Anton Johansson Reviewed-by Anton Johansson on v3 1/1
#1 lore raw
Quoted 124 lines · Taylor Simpson

**** Changes in v3 ****
Fix bugs exposed by dpmpyss_rnd_s0 instruction
Set correct size/signedness for constants
Test cases added to tests/tcg/hexagon/misc.c

**** Changes in v2 ****
Fix bug in imm_print identified in clang build

Currently, idef-parser skips all floating point instructions. However, there are some floating point instructions that can be handled.

The following instructions are now parsed

     F2_sfimm_p
     F2_sfimm_n
     F2_dfimm_p
     F2_dfimm_n
     F2_dfmpyll
     F2_dfmpylh

To make these instructions work, we fix some bugs in parser-helpers.c
gen_rvalue_extend
gen_cast_op
imm_print
lexer properly sets size/signedness of constants

Test cases added to tests/tcg/hexagon/fpstuff.c

---
  target/hexagon/idef-parser/parser-helpers.h |  2 +-
  target/hexagon/idef-parser/parser-helpers.c | 41 +++++++++++-----
  tests/tcg/hexagon/fpstuff.c                 | 54 +++++++++++++++++++++
  tests/tcg/hexagon/misc.c                    | 35 +++++++++++++
  target/hexagon/gen_idef_parser_funcs.py     | 10 +++-
  target/hexagon/idef-parser/idef-parser.lex  | 38 +++++++++++++--
  target/hexagon/idef-parser/idef-parser.y    |  2 -
  7 files changed, 162 insertions(+), 20 deletions(-)
target/hexagon/idef-parser/parser-helpers.h +1 -1
index 1239d23a6a..7c58087169 100644
@@ -80,7 +80,7 @@ void reg_compose(Context *c, YYLTYPE *locp, HexReg *reg, char reg_id[5]);
 
  void reg_print(Context *c, YYLTYPE *locp, HexReg *reg);
 
-void imm_print(Context *c, YYLTYPE *locp, HexImm *imm);
+void imm_print(Context *c, YYLTYPE *locp, HexValue *rvalue);
 
  void var_print(Context *c, YYLTYPE *locp, HexVar *var);
 
target/hexagon/idef-parser/parser-helpers.c +23 -11
index 86511efb62..0ad917f591 100644
@@ -167,8 +167,9 @@ void reg_print(Context *c, YYLTYPE *locp, HexReg *reg)
      EMIT(c, "hex_gpr[%u]", reg->id);
  }
 
-void imm_print(Context *c, YYLTYPE *locp, HexImm *imm)
+void imm_print(Context *c, YYLTYPE *locp, HexValue *rvalue)
  {
+    HexImm *imm = &rvalue->imm;
      switch (imm->type) {
      case I:
          EMIT(c, "i");
@@ -177,7 +178,21 @@ void imm_print(Context *c, YYLTYPE *locp, HexImm *imm)
          EMIT(c, "%ciV", imm->id);
          break;
      case VALUE:
-        EMIT(c, "((int64_t) %" PRIu64 "ULL)", (int64_t) imm->value);
+        if (rvalue->bit_width == 32) {
+            if (rvalue->signedness == UNSIGNED) {
+                EMIT(c, "((uint32_t) 0x%" PRIx32 ")", (uint32_t) imm->value);
+            }  else {
+                EMIT(c, "((int32_t) 0x%" PRIx32 ")", (int32_t) imm->value);
+            }
+        } else if (rvalue->bit_width == 64) {
+            if (rvalue->signedness == UNSIGNED) {
+                EMIT(c, "((uint64_t) 0x%" PRIx64 "ULL)", (uint64_t) imm->value);
+            } else {
+                EMIT(c, "((int64_t) 0x%" PRIx64 "LL)", (int64_t) imm->value);
+            }
+        } else {
+            g_assert_not_reached();
+        }
          break;
      case QEMU_TMP:
          EMIT(c, "qemu_tmp_%" PRIu64, imm->index);
@@ -213,7 +228,7 @@ void rvalue_print(Context *c, YYLTYPE *locp, void *pointer)
        tmp_print(c, locp, &rvalue->tmp);
        break;
    case IMMEDIATE:
-      imm_print(c, locp, &rvalue->imm);
+      imm_print(c, locp, rvalue);
        break;
    case VARID:
        var_print(c, locp, &rvalue->var);
@@ -386,13 +401,10 @@ HexValue gen_rvalue_extend(Context *c, YYLTYPE *locp, HexValue *rvalue)
 
      if (rvalue->type == IMMEDIATE) {
          HexValue res = gen_imm_qemu_tmp(c, locp, 64, rvalue->signedness);
-        bool is_unsigned = (rvalue->signedness == UNSIGNED);
-        const char *sign_suffix = is_unsigned ? "u" : "";
          gen_c_int_type(c, locp, 64, rvalue->signedness);
-        OUT(c, locp, " ", &res, " = ");
-        OUT(c, locp, "(", sign_suffix, "int64_t) ");
-        OUT(c, locp, "(", sign_suffix, "int32_t) ");
-        OUT(c, locp, rvalue, ";\n");
+        OUT(c, locp, " ", &res, " = (");
+        gen_c_int_type(c, locp, 64, rvalue->signedness);
+        OUT(c, locp, ")", rvalue, ";\n");
          return res;
      } else {
          HexValue res = gen_tmp(c, locp, 64, rvalue->signedness);
@@ -961,9 +973,16 @@ HexValue gen_cast_op(Context *c,
  {
      assert_signedness(c, locp, src->signedness);
      if (src->bit_width == target_width) {
-        return *src;
-    } else if (src->type == IMMEDIATE) {
          HexValue res = *src;
+        res.signedness = signedness;
+        return res;

target/hexagon/idef-parser/parser-helpers.c HexValue gen_cast_op(Context *c, · line 988

Quoted 10 lines
target/hexagon/idef-parser/parser-helpers.c +7 -0
+    } else if (src->type == IMMEDIATE) {
+        HexValue res;
+        if (src->bit_width < target_width) {
+            res = gen_rvalue_extend(c, locp, src);
+        } else {
+            res = *src;
+        }
          res.bit_width = target_width;
          res.signedness = signedness;
          return res;

Ah, gen_cast_op() can be simplified a great deal here to

    HexValue gen_cast_op(Context *c,
                         YYLTYPE *locp, HexValue *src, unsigned target_width, HexSignedness signedness) {
        HexValue res;
        assert_signedness(c, locp, src->signedness); if (src->bit_width == target_width) { res = *src;
        } else if (src->bit_width < target_width) { res = gen_rvalue_extend(c, locp, src); } else {
            res = gen_rvalue_truncate(c, locp, src); }
        res.signedness = signedness;
        return res;
    }

Other than that this patch looks good:

#2 lore raw
Quoted 50 lines

-----Original Message-----
From: Anton Johansson <[email protected]>
Sent: Tuesday, May 2, 2023 6:12 AM
To: Taylor Simpson <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Brian Cain <[email protected]>; Matheus Bernardino (QUIC) <[email protected]>
Subject: Re: [PATCH v3] Hexagon (target/hexagon) Additional instructions handled by idef-parser

Quoted 14 lines · Taylor Simpson
target/hexagon/idef-parser/parser-helpers.c +2 -3
index 86511efb62..0ad917f591 100644
-961,9 +973,16 @@ HexValue gen_cast_op(Context *c,
  {
      assert_signedness(c, locp, src->signedness);
      if (src->bit_width == target_width) {
-        return *src;
-    } else if (src->type == IMMEDIATE) {
          HexValue res = *src;
+        res.signedness = signedness;
+        return res;

target/hexagon/idef-parser/parser-helpers.c HexValue gen_cast_op(Context *c, · line 988

Quoted 10 lines
target/hexagon/idef-parser/parser-helpers.c +7 -0
+    } else if (src->type == IMMEDIATE) {
+        HexValue res;
+        if (src->bit_width < target_width) {
+            res = gen_rvalue_extend(c, locp, src);
+        } else {
+            res = *src;
+        }
          res.bit_width = target_width;
          res.signedness = signedness;
          return res;

Ah, gen_cast_op() can be simplified a great deal here to

     HexValue gen_cast_op(Context *c,
                          YYLTYPE *locp,
                          HexValue *src,
                          unsigned target_width,
                          HexSignedness signedness)
     {
         HexValue res;
         assert_signedness(c, locp, src->signedness);
         if (src->bit_width == target_width) {
             res = *src;
Quoted 8 lines
         } else if (src->bit_width < target_width) {
             res = gen_rvalue_extend(c, locp, src);
         } else {
             res = gen_rvalue_truncate(c, locp, src);
         }
         res.signedness = signedness;
         return res;
     }

Great suggestion, thanks!

Quoted 6 lines

Other than that this patch looks good: