Implement OpenCL binary operations
authorTom Tromey <tom@tromey.com>
Mon, 8 Mar 2021 14:27:57 +0000 (07:27 -0700)
committerTom Tromey <tom@tromey.com>
Mon, 8 Mar 2021 14:28:31 +0000 (07:28 -0700)
This implements various OpenCL binary operations, adding a new
template class to do so.

gdb/ChangeLog
2021-03-08  Tom Tromey  <tom@tromey.com>

* opencl-lang.c (opencl_relop, eval_opencl_assign): No longer
static.  Change parameters.
(eval_opencl_assign): No longer static.  Add "op" parameter.
(evaluate_subexp_opencl): Update.
* c-exp.h (opencl_binop_operation): New template class.
(opencl_assign_operation, opencl_equal_operation)
(opencl_notequal_operation, opencl_less_operation)
(opencl_gtr_operation, opencl_geq_operation)
(opencl_leq_operation): New typedefs.

gdb/ChangeLog
gdb/c-exp.h
gdb/opencl-lang.c

index 5abe97e6907f2bf2bab0ef529ad3310456954ed2..f463829b0cc8813198946bad5d27310e8b92fddb 100644 (file)
@@ -1,3 +1,15 @@
+2021-03-08  Tom Tromey  <tom@tromey.com>
+
+       * opencl-lang.c (opencl_relop, eval_opencl_assign): No longer
+       static.  Change parameters.
+       (eval_opencl_assign): No longer static.  Add "op" parameter.
+       (evaluate_subexp_opencl): Update.
+       * c-exp.h (opencl_binop_operation): New template class.
+       (opencl_assign_operation, opencl_equal_operation)
+       (opencl_notequal_operation, opencl_less_operation)
+       (opencl_gtr_operation, opencl_geq_operation)
+       (opencl_leq_operation): New typedefs.
+
 2021-03-08  Tom Tromey  <tom@tromey.com>
 
        * opencl-lang.c (opencl_value_cast): No longer static.
index 68efa62c5a8e9ba9bee0411e51bc4efcae8fa06d..dc92e7a49a8a72ba7d80228f39cbf093f8e7b55a 100644 (file)
@@ -28,6 +28,16 @@ extern struct value *eval_op_objc_selector (struct type *expect_type,
                                            enum noside noside,
                                            const char *sel);
 extern struct value *opencl_value_cast (struct type *type, struct value *arg);
+extern struct value *eval_opencl_assign (struct type *expect_type,
+                                        struct expression *exp,
+                                        enum noside noside,
+                                        enum exp_opcode op,
+                                        struct value *arg1,
+                                        struct value *arg2);
+extern struct value *opencl_relop (struct type *expect_type,
+                                  struct expression *exp,
+                                  enum noside noside, enum exp_opcode op,
+                                  struct value *arg1, struct value *arg2);
 
 namespace expr
 {
@@ -110,6 +120,48 @@ public:
 using opencl_cast_type_operation = cxx_cast_operation<UNOP_CAST_TYPE,
                                                      opencl_value_cast>;
 
+/* Binary operations, as needed for OpenCL.  */
+template<enum exp_opcode OP, binary_ftype FUNC,
+        typename BASE = maybe_constant_operation<operation_up, operation_up>>
+class opencl_binop_operation
+  : public BASE
+{
+public:
+
+  using BASE::BASE;
+
+  value *evaluate (struct type *expect_type,
+                  struct expression *exp,
+                  enum noside noside) override
+  {
+    value *lhs
+      = std::get<0> (this->m_storage)->evaluate (nullptr, exp, noside);
+    value *rhs
+      = std::get<1> (this->m_storage)->evaluate (value_type (lhs), exp,
+                                                noside);
+    return FUNC (expect_type, exp, noside, OP, lhs, rhs);
+  }
+
+  enum exp_opcode opcode () const override
+  { return OP; }
+};
+
+using opencl_assign_operation = opencl_binop_operation<BINOP_ASSIGN,
+                                                      eval_opencl_assign,
+                                                      assign_operation>;
+using opencl_equal_operation = opencl_binop_operation<BINOP_EQUAL,
+                                                     opencl_relop>;
+using opencl_notequal_operation = opencl_binop_operation<BINOP_NOTEQUAL,
+                                                        opencl_relop>;
+using opencl_less_operation = opencl_binop_operation<BINOP_LESS,
+                                                    opencl_relop>;
+using opencl_gtr_operation = opencl_binop_operation<BINOP_GTR,
+                                                   opencl_relop>;
+using opencl_geq_operation = opencl_binop_operation<BINOP_GEQ,
+                                                   opencl_relop>;
+using opencl_leq_operation = opencl_binop_operation<BINOP_LEQ,
+                                                   opencl_relop>;
+
 }/* namespace expr */
 
 #endif /* C_EXP_H */
index c31eb51a3901c8b525fd49f8ade41efaf0ccd1fa..01456055015c53bbafcc0a7fb2446412f1c21260 100644 (file)
@@ -631,9 +631,10 @@ opencl_value_cast (struct type *type, struct value *arg)
 
 /* Perform a relational operation on two operands.  */
 
-static struct value *
-opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
-             enum exp_opcode op)
+struct value *
+opencl_relop (struct type *expect_type, struct expression *exp,
+             enum noside noside, enum exp_opcode op,
+             struct value *arg1, struct value *arg2)
 {
   struct value *val;
   struct type *type1 = check_typedef (value_type (arg1));
@@ -673,9 +674,9 @@ opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
 
 /* A helper function for BINOP_ASSIGN.  */
 
-static struct value *
+struct value *
 eval_opencl_assign (struct type *expect_type, struct expression *exp,
-                   enum noside noside,
+                   enum noside noside, enum exp_opcode op,
                    struct value *arg1, struct value *arg2)
 {
   if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
@@ -712,7 +713,7 @@ evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
       type1 = value_type (arg1);
       arg2 = evaluate_subexp (type1, exp, pos, noside);
 
-      return eval_opencl_assign (expect_type, exp, noside, arg1, arg2);
+      return eval_opencl_assign (expect_type, exp, noside, op, arg1, arg2);
 
     case UNOP_CAST:
       type1 = exp->elts[*pos + 1].type;
@@ -753,7 +754,7 @@ evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
        return value_from_longest (builtin_type (exp->gdbarch)->
                                   builtin_int, 1);
 
-      return opencl_relop (exp, arg1, arg2, op);
+      return opencl_relop (expect_type, exp, noside, op, arg1, arg2);
 
     /* Handle the logical unary operator not(!).  */
     case UNOP_LOGICAL_NOT:
@@ -798,7 +799,7 @@ evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
            {
              arg2 = evaluate_subexp (nullptr, exp, pos, noside);
 
-             return opencl_relop (exp, arg1, arg2, op);
+             return opencl_relop (nullptr, exp, noside, op, arg1, arg2);
            }
          else
            {