]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
iropt-test: Refactor code
authorFlorian Krohm <flo2030@eich-krohm.de>
Fri, 25 Jul 2025 21:11:57 +0000 (21:11 +0000)
committerFlorian Krohm <flo2030@eich-krohm.de>
Fri, 25 Jul 2025 21:11:57 +0000 (21:11 +0000)
Refactor code in preparation for running each testcase twice: once
with constant folding and once without.
- remove function print_opnd
- remove function complain
- factor out function get_expected_value
- checking the result moved to valgrind_execute_test
- make IRICB a static global in valgrind.c
- new_iricb now returns a pointer to it

none/tests/iropt-test/binary.c
none/tests/iropt-test/main.c
none/tests/iropt-test/unary.c
none/tests/iropt-test/util.c
none/tests/iropt-test/valgrind.c
none/tests/iropt-test/vtest.h

index 62c9faeb35f4626b1ad1eb72a2d77a3eb8490dc1..0d40a4da3e8845776f5d1084e3cc50bcc433b4cb 100644 (file)
@@ -26,7 +26,7 @@
 #include <stdint.h>     // UINT64_MAX
 #include "vtest.h"
 
-static void check_result(const irop_t *, const test_data_t *);
+static uint64_t get_expected_value(const irop_t *, const test_data_t *);
 static void run_tests(const irop_t *, test_data_t *);
 static void run_shift_tests(const irop_t *, test_data_t *);
 static int  is_shift_op(IROp);
@@ -59,8 +59,7 @@ run_selected_tests(const irop_t *op, test_data_t *data)
 
          if (is_division_op(op->op) && opnd_r->value == 0) continue;
 
-         valgrind_execute_test(op, data);
-         check_result(op, data);
+         valgrind_execute_test(op, data, get_expected_value(op, data));
       }
    }
 }
@@ -79,8 +78,7 @@ run_random_tests(const irop_t *op, test_data_t *data)
 
       if (is_division_op(op->op) && opnd_r->value == 0) continue;
 
-      valgrind_execute_test(op, data);
-      check_result(op, data);
+      valgrind_execute_test(op, data, get_expected_value(op, data));
    }
 }
 
@@ -101,8 +99,7 @@ run_shift_tests(const irop_t *op, test_data_t *data)
       for (unsigned j = 0; j < max_shift_amount; ++j) {
          opnd_r->value = j;
 
-         valgrind_execute_test(op, data);
-         check_result(op, data);
+         valgrind_execute_test(op, data, get_expected_value(op, data));
       }
    }
 
@@ -111,8 +108,7 @@ run_shift_tests(const irop_t *op, test_data_t *data)
       opnd_l->value = get_random_value(opnd_l->type);
       opnd_r->value = get_random_value(opnd_r->type) & max_shift_amount;
 
-      valgrind_execute_test(op, data);
-      check_result(op, data);
+      valgrind_execute_test(op, data, get_expected_value(op, data));
    }
 }
 
@@ -125,11 +121,10 @@ run_tests(const irop_t *op, test_data_t *data)
 }
 
 
-/* Check the result of a binary operation. */
-static void
-check_result(const irop_t *op, const test_data_t *data)
+/* Compute the expected result of a binary operation. */
+static uint64_t
+get_expected_value(const irop_t *op, const test_data_t *data)
 {
-   uint64_t result = data->result.value;
    uint64_t opnd_l = data->opnds[0].value;
    uint64_t opnd_r = data->opnds[1].value;
    uint64_t expected;
@@ -384,25 +379,7 @@ check_result(const irop_t *op, const test_data_t *data)
       panic(__func__);
    }
 
-   if (verbose > 1) {
-      printf("expected:  value = ");
-      print_value(stdout, expected, bitsof_irtype(data->result.type));
-      printf("\n");
-   }
-
-   int ok = 1;
-   switch (data->result.type) {
-   case Ity_I1:  ok = result == expected; break;
-   case Ity_I8:  ok = result == expected; break;
-   case Ity_I16: ok = result == expected; break;
-   case Ity_I32: ok = result == expected; break;
-   case Ity_I64: ok = result == expected; break;
-   default:
-      panic(__func__);
-   }
-
-   if (! ok)
-      complain(op, data, expected);
+   return expected;
 }
 
 
index 9f1cc5083af0055e05d4aef8660cdf470d3821e7..8fcf9fb45333a93d49f6e6336a18cfe759b1ea6b 100644 (file)
@@ -81,9 +81,9 @@ main(int argc, char *argv[])
 
       test_data_t *data = new_test_data(op);
 
-      IRICB iricb = new_iricb(op, data);
+      IRICB *iricb = new_iricb(op, data);
 
-      valgrind_vex_init_for_iri(&iricb);
+      valgrind_vex_init_for_iri(iricb);
 
       switch (op->num_opnds) {
       case 1:
index e6554cef60b20952f592fb759bf18773429d5b72..fa961f28a7b3d5514ef0f12449d8d45ddfeffed6 100644 (file)
@@ -27,9 +27,9 @@
 #include <stdint.h>     // UINT64_MAX
 #include "vtest.h"
 
-static void check_result(const irop_t *, const test_data_t *);
 static void run_selected_tests(const irop_t *, test_data_t *);
 static void run_random_tests(const irop_t *, test_data_t *);
+static uint64_t get_expected_value(const irop_t *, const test_data_t *);
 static uint64_t left(uint64_t, unsigned);
 static uint32_t popcount(uint64_t);
 static uint32_t clz(uint64_t, unsigned);
@@ -49,8 +49,7 @@ test_unary_op(const irop_t *op, test_data_t *data)
       for (unsigned i = 0; i <= max; ++i) {
          opnd->value = i;
 
-         valgrind_execute_test(op, data);
-         check_result(op, data);
+         valgrind_execute_test(op, data, get_expected_value(op, data));
       }
       break;
    }
@@ -78,8 +77,7 @@ run_selected_tests(const irop_t *op, test_data_t *data)
    for (unsigned i = 0; i < num_val; ++i) {
       opnd->value = values[i];
 
-      valgrind_execute_test(op, data);
-      check_result(op, data);
+      valgrind_execute_test(op, data, get_expected_value(op, data));
    }
 }
 
@@ -93,17 +91,15 @@ run_random_tests(const irop_t *op, test_data_t *data)
    for (unsigned i = 0; i < num_random_tests; ++i) {
       opnd->value = get_random_value(opnd->type);
 
-      valgrind_execute_test(op, data);
-      check_result(op, data);
+      valgrind_execute_test(op, data, get_expected_value(op, data));
    }
 }
 
 
-/* Check the result of a unary operation. */
-static void
-check_result(const irop_t *op, const test_data_t *data)
+/* Compute the expected result of a unary operation. */
+static uint64_t
+get_expected_value(const irop_t *op, const test_data_t *data)
 {
-   uint64_t result = data->result.value;
    uint64_t opnd   = data->opnds[0].value;
    uint64_t expected;
 
@@ -209,25 +205,7 @@ check_result(const irop_t *op, const test_data_t *data)
       panic("%s: operator %s not handled\n", __func__, op->name);
    }
 
-   if (verbose > 1) {
-      printf("expected:  value = ");
-      print_value(stdout, expected, bitsof_irtype(data->result.type));
-      printf("\n");
-   }
-
-   int ok = 1;
-   switch (data->result.type) {
-   case Ity_I1:  ok = result == expected; break;
-   case Ity_I8:  ok = result == expected; break;
-   case Ity_I16: ok = result == expected; break;
-   case Ity_I32: ok = result == expected; break;
-   case Ity_I64: ok = result == expected; break;
-   default:
-      panic(__func__);
-   }
-
-   if (! ok)
-      complain(op, data, expected);
+   return expected;
 }
 
 
index 18b671114ce26701b17d3f403280406ef4738963..f34936611e9829bf26d70b7a9cbb6dbcad037847 100644 (file)
@@ -44,27 +44,6 @@ panic(const char *fmt, ...)
 }
 
 
-/* Issue a complaint because the result of an operation differs from what
-   was expected. */
-void
-complain(const irop_t *op, const test_data_t *data, uint64_t expected)
-{
-   fprintf(stderr, "*** Incorrect result for operator %s\n", op->name);
-
-   for (unsigned i = 0; i < op->num_opnds; ++i) {
-      fprintf(stderr, "    opnd %u:  ", i);
-      print_opnd(stderr, &data->opnds[i]);
-      fprintf(stderr, "\n");
-   }
-   fprintf(stderr, "    result:  ");
-   print_opnd(stderr, &data->result);
-   fprintf(stderr, "\n");
-   fprintf(stderr, "    expect:  ");
-   print_value(stderr, expected, bitsof_irtype(op->result_type));
-   fprintf(stderr, "\n");
-}
-
-
 void
 print_value(FILE *fp, uint64_t val, unsigned num_bits)
 {
@@ -83,14 +62,6 @@ print_value(FILE *fp, uint64_t val, unsigned num_bits)
 }
 
 
-void
-print_opnd(FILE *fp, const opnd_t *opnd)
-{
-   fprintf(fp, "value = ");
-   print_value(fp, opnd->value, bitsof_irtype(opnd->type));
-}
-
-
 unsigned
 bitsof_irtype(IRType ty)
 {
index e482909b1653693db3768619a05d6eab5bb60a30..004c8a09a40ee3bea18556f2a7aec24550dd06ab 100644 (file)
 #include "valgrind.h"  // VALGRIND_VEX_INJECT_IR
 #include "vtest.h"
 
+static IRICB iricb;
 
 /* Return a completely initialised control block */
-IRICB
+IRICB *
 new_iricb(const irop_t *op, test_data_t *data)
 {
    IRICB_iropt_payload cb;
@@ -45,7 +46,10 @@ new_iricb(const irop_t *op, test_data_t *data)
 
    cb.num_operands = op->num_opnds;
 
-   return (IRICB) { .kind = IRICB_iropt, .iropt = cb };
+   iricb.kind  = IRICB_iropt;
+   iricb.iropt = cb;
+
+   return &iricb;
 }
 
 
@@ -69,24 +73,47 @@ valgrind_vex_inject_ir(void)
 /* Execute the test under valgrind. Well, yes, we're not really executing
    it here, just preparing for it... */
 void
-valgrind_execute_test(const irop_t *op, test_data_t *data)
+valgrind_execute_test(const irop_t *op, test_data_t *data, uint64_t expected)
 {
-   if (verbose > 1)
+   if (verbose > 1) {
       printf("---------- Running a test\n");
 
-   for (unsigned i = 0; i < op->num_opnds; ++i) {
-      if (verbose > 1) {
-         printf("opnd #%u:   ", i);
-         print_opnd(stdout, &data->opnds[i]);
+      for (unsigned i = 0; i < op->num_opnds; ++i) {
+         const opnd_t *opnd = data->opnds + i;
+         printf("opnd %u:    value = ", i);
+         print_value(stdout, opnd->value, bitsof_irtype(opnd->type));
          printf("\n");
       }
    }
 
    valgrind_vex_inject_ir();
+   uint64_t result = data->result.value;
 
+   unsigned num_result_bits = bitsof_irtype(data->result.type);
    if (verbose > 1) {
-      printf("result:    ");
-      print_opnd(stdout, &data->result);
+      printf("result:    value = ");
+      print_value(stdout, result, num_result_bits);
       printf("\n");
+      printf("expected:  value = ");
+      print_value(stdout, expected, num_result_bits);
+      printf("\n");
+   }
+
+   /* Check result */
+   if (result != expected) {
+      fprintf(stderr, "*** Incorrect result for operator %s\n", op->name);
+
+      for (unsigned i = 0; i < op->num_opnds; ++i) {
+         const opnd_t *opnd = data->opnds + i;
+         fprintf(stderr, "    opnd %u:  ", i);
+         print_value(stderr, opnd->value, bitsof_irtype(opnd->type));
+         fprintf(stderr, "\n");
+      }
+      fprintf(stderr, "    result:  ");
+      print_value(stderr, result, num_result_bits);
+      fprintf(stderr, "\n");
+      fprintf(stderr, "    expect:  ");
+      print_value(stderr, expected, num_result_bits);
+      fprintf(stderr, "\n");
    }
 }
index 47b397f6819277de7aad4e28716f52eeb140631d..f31f3c3ce4b1a6c17ad24ef266918c1212547b7e 100644 (file)
@@ -69,19 +69,17 @@ typedef struct {
 
 
 /* Function prototypes */
-void print_opnd(FILE *, const opnd_t *);
 void print_value(FILE *, uint64_t, unsigned);
 
 void test_unary_op(const irop_t *, test_data_t *);
 void test_binary_op(const irop_t *, test_data_t *);
 
 void valgrind_vex_init_for_iri(IRICB *);
-void valgrind_execute_test(const irop_t *, test_data_t *);
+void valgrind_execute_test(const irop_t *, test_data_t *, uint64_t);
 
-IRICB new_iricb(const irop_t *, test_data_t *);
+IRICB *new_iricb(const irop_t *, test_data_t *);
 
 void panic(const char *, ...) __attribute__((noreturn));
-void complain(const irop_t *, const test_data_t *, uint64_t expected);
 
 unsigned bitsof_irtype(IRType);
 uint64_t get_random_value(IRType);