Committed to branch dmalcolm/jit:

Add some more methods to the C++ wrapper API to reduce the amount of
typing needed when performing specific operations, albeit it without going
all the way to using operator overloading, since the latter is often too
"magical" for some people's taste, and may make it impossible to specify
the context within which the resulting object lives.

We retain the more general wrapper APIs, since having both generic and
specialized methods seems to be useful (based on my ongoing experiments
porting GNU Octave's existing JIT compiler to libgccjit).

gcc/jit/
        * libgccjit++.h (gccjit::context::new_minus): New method,
        providing a way to do a specific unary op with less typing.
        (gccjit::context::new_bitwise_negate): Likewise.
        (gccjit::context::new_logical_negate): Likewise.

        (gccjit::context::new_plus): Likewise, for binary op.
        (gccjit::context::new_minus): Likewise.
        (gccjit::context::new_mult): Likewise.
        (gccjit::context::new_divide): Likewise.
        (gccjit::context::new_modulo): Likewise.
        (gccjit::context::new_bitwise_and): Likewise.
        (gccjit::context::new_bitwise_xor): Likewise.
        (gccjit::context::new_bitwise_or): Likewise.
        (gccjit::context::new_logical_and): Likewise.
        (gccjit::context::new_logical_or): Likewise.

        (gccjit::context::new_eq): Likewise, for comparison.
        (gccjit::context::new_ne): Likewise.
        (gccjit::context::new_lt): Likewise.
        (gccjit::context::new_le): Likewise.
        (gccjit::context::gccjit::context::new_gt): Likewise.
        (gccjit::context::gccjit::context::new_ge): Likewise.

        (gccjit::context::new_call): Add a series of overloaded methods
        for specific numbers of args (from 0 - 6), to avoid the need for
        client code to manually build a std::vector (or requiring C++11).

gcc/testsuite/
        * jit.dg/test-quadratic.cc (make_calc_discriminant): Make use of
        new methods of the C++ wrapper API to shorten the example code.
        (make_test_quadratic): Likewise.
---
 gcc/jit/ChangeLog.jit                  |  29 +++
 gcc/jit/libgccjit++.h                  | 316 +++++++++++++++++++++++++++++++++
 gcc/testsuite/ChangeLog.jit            |   6 +
 gcc/testsuite/jit.dg/test-quadratic.cc |  51 ++----
 4 files changed, 366 insertions(+), 36 deletions(-)

diff --git a/gcc/jit/ChangeLog.jit b/gcc/jit/ChangeLog.jit
index 8f1083a..bd209ed 100644
--- a/gcc/jit/ChangeLog.jit
+++ b/gcc/jit/ChangeLog.jit
@@ -1,5 +1,34 @@
 2014-02-10  David Malcolm  <dmalc...@redhat.com>
 
+       * libgccjit++.h (gccjit::context::new_minus): New method,
+       providing a way to do a specific unary op with less typing.
+       (gccjit::context::new_bitwise_negate): Likewise.
+       (gccjit::context::new_logical_negate): Likewise.
+
+       (gccjit::context::new_plus): Likewise, for binary op.
+       (gccjit::context::new_minus): Likewise.
+       (gccjit::context::new_mult): Likewise.
+       (gccjit::context::new_divide): Likewise.
+       (gccjit::context::new_modulo): Likewise.
+       (gccjit::context::new_bitwise_and): Likewise.
+       (gccjit::context::new_bitwise_xor): Likewise.
+       (gccjit::context::new_bitwise_or): Likewise.
+       (gccjit::context::new_logical_and): Likewise.
+       (gccjit::context::new_logical_or): Likewise.
+
+       (gccjit::context::new_eq): Likewise, for comparison.
+       (gccjit::context::new_ne): Likewise.
+       (gccjit::context::new_lt): Likewise.
+       (gccjit::context::new_le): Likewise.
+       (gccjit::context::gccjit::context::new_gt): Likewise.
+       (gccjit::context::gccjit::context::new_ge): Likewise.
+
+       (gccjit::context::new_call): Add a series of overloaded methods
+       for specific numbers of args (from 0 - 6), to avoid the need for
+       client code to manually build a std::vector (or requiring C++11).
+
+2014-02-10  David Malcolm  <dmalc...@redhat.com>
+
        * libgccjit++.h (gccjit::context::new_struct_type): Pass std::vector
        "fields" argument by reference rather than by value.
        (gccjit::context::new_function): Likewise, for "params" arg.
diff --git a/gcc/jit/libgccjit++.h b/gcc/jit/libgccjit++.h
index ae55058..ff22d1e 100644
--- a/gcc/jit/libgccjit++.h
+++ b/gcc/jit/libgccjit++.h
@@ -112,24 +112,114 @@ namespace gccjit
                       void *value);
     rvalue new_rvalue (const std::string &value);
 
+    /* Generic unary operations...  */
     rvalue new_unary_op (enum gcc_jit_unary_op op,
                         type result_type,
                         rvalue a,
                         location loc = location ());
 
+    /* ...and shorter ways to spell the various specific kinds of
+       unary op.  */
+    rvalue new_minus (type result_type,
+                     rvalue a,
+                     location loc = location ());
+    rvalue new_bitwise_negate (type result_type,
+                              rvalue a,
+                              location loc = location ());
+    rvalue new_logical_negate (type result_type,
+                              rvalue a,
+                              location loc = location ());
+
+    /* Generic binary operations...  */
     rvalue new_binary_op (enum gcc_jit_binary_op op,
                          type result_type,
                          rvalue a, rvalue b,
                          location loc = location ());
 
+    /* ...and shorter ways to spell the various specific kinds of
+       binary op.  */
+    rvalue new_plus (type result_type,
+                    rvalue a, rvalue b,
+                    location loc = location ());
+    rvalue new_minus (type result_type,
+                     rvalue a, rvalue b,
+                     location loc = location ());
+    rvalue new_mult (type result_type,
+                    rvalue a, rvalue b,
+                    location loc = location ());
+    rvalue new_divide (type result_type,
+                      rvalue a, rvalue b,
+                      location loc = location ());
+    rvalue new_modulo (type result_type,
+                      rvalue a, rvalue b,
+                      location loc = location ());
+    rvalue new_bitwise_and (type result_type,
+                           rvalue a, rvalue b,
+                           location loc = location ());
+    rvalue new_bitwise_xor (type result_type,
+                           rvalue a, rvalue b,
+                           location loc = location ());
+    rvalue new_bitwise_or (type result_type,
+                          rvalue a, rvalue b,
+                          location loc = location ());
+    rvalue new_logical_and (type result_type,
+                           rvalue a, rvalue b,
+                           location loc = location ());
+    rvalue new_logical_or (type result_type,
+                          rvalue a, rvalue b,
+                          location loc = location ());
+
+    /* Generic comparisons...  */
     rvalue new_comparison (enum gcc_jit_comparison op,
                           rvalue a, rvalue b,
                           location loc = location ());
+    /* ...and shorter ways to spell the various specific kinds of
+       comparison.  */
+    rvalue new_eq (rvalue a, rvalue b,
+                  location loc = location ());
+    rvalue new_ne (rvalue a, rvalue b,
+                  location loc = location ());
+    rvalue new_lt (rvalue a, rvalue b,
+                  location loc = location ());
+    rvalue new_le (rvalue a, rvalue b,
+                  location loc = location ());
+    rvalue new_gt (rvalue a, rvalue b,
+                  location loc = location ());
+    rvalue new_ge (rvalue a, rvalue b,
+                  location loc = location ());
 
+    /* The most general way of creating a function call.  */
     rvalue new_call (function func,
                     std::vector<rvalue> &args,
                     location loc = location ());
 
+    /* In addition, we provide a series of overloaded "new_call" methods
+       for specific numbers of args (from 0 - 6), to avoid the need for
+       client code to manually build a vector.  */
+    rvalue new_call (function func,
+                    location loc = location ());
+    rvalue new_call (function func,
+                    rvalue arg0,
+                    location loc = location ());
+    rvalue new_call (function func,
+                    rvalue arg0, rvalue arg1,
+                    location loc = location ());
+    rvalue new_call (function func,
+                    rvalue arg0, rvalue arg1, rvalue arg2,
+                    location loc = location ());
+    rvalue new_call (function func,
+                    rvalue arg0, rvalue arg1, rvalue arg2,
+                    rvalue arg3,
+                    location loc = location ());
+    rvalue new_call (function func,
+                    rvalue arg0, rvalue arg1, rvalue arg2,
+                    rvalue arg3, rvalue arg4,
+                    location loc = location ());
+    rvalue new_call (function func,
+                    rvalue arg0, rvalue arg1, rvalue arg2,
+                    rvalue arg3, rvalue arg4, rvalue arg5,
+                    location loc = location ());
+
     lvalue new_array_access (rvalue ptr,
                             rvalue index,
                             location loc = location ());
@@ -461,6 +551,30 @@ context::new_unary_op (enum gcc_jit_unary_op op,
                                               result_type.get_inner_type (),
                                               a.get_inner_rvalue ()));
 }
+inline rvalue
+context::new_minus (type result_type,
+                   rvalue a,
+                   location loc)
+{
+  return rvalue (new_unary_op (GCC_JIT_UNARY_OP_MINUS,
+                              result_type, a, loc));
+}
+inline rvalue
+context::new_bitwise_negate (type result_type,
+                            rvalue a,
+                            location loc)
+{
+  return rvalue (new_unary_op (GCC_JIT_UNARY_OP_BITWISE_NEGATE,
+                              result_type, a, loc));
+}
+inline rvalue
+context::new_logical_negate (type result_type,
+                            rvalue a,
+                            location loc)
+{
+  return rvalue (new_unary_op (GCC_JIT_UNARY_OP_LOGICAL_NEGATE,
+                              result_type, a, loc));
+}
 
 inline rvalue
 context::new_binary_op (enum gcc_jit_binary_op op,
@@ -475,6 +589,86 @@ context::new_binary_op (enum gcc_jit_binary_op op,
                                                a.get_inner_rvalue (),
                                                b.get_inner_rvalue ()));
 }
+inline rvalue
+context::new_plus (type result_type,
+                  rvalue a, rvalue b,
+                  location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_PLUS,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_minus (type result_type,
+                   rvalue a, rvalue b,
+                   location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_MINUS,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_mult (type result_type,
+                  rvalue a, rvalue b,
+                  location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_MULT,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_divide (type result_type,
+                    rvalue a, rvalue b,
+                    location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_DIVIDE,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_modulo (type result_type,
+                    rvalue a, rvalue b,
+                    location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_MODULO,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_bitwise_and (type result_type,
+                         rvalue a, rvalue b,
+                         location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_BITWISE_AND,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_bitwise_xor (type result_type,
+                         rvalue a, rvalue b,
+                         location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_BITWISE_XOR,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_bitwise_or (type result_type,
+                        rvalue a, rvalue b,
+                        location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_BITWISE_OR,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_logical_and (type result_type,
+                         rvalue a, rvalue b,
+                         location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_LOGICAL_AND,
+                       result_type, a, b, loc);
+}
+inline rvalue
+context::new_logical_or (type result_type,
+                        rvalue a, rvalue b,
+                        location loc)
+{
+  return new_binary_op (GCC_JIT_BINARY_OP_LOGICAL_OR,
+                       result_type, a, b, loc);
+}
 
 inline rvalue
 context::new_comparison (enum gcc_jit_comparison op,
@@ -487,6 +681,48 @@ context::new_comparison (enum gcc_jit_comparison op,
                                                 a.get_inner_rvalue (),
                                                 b.get_inner_rvalue ()));
 }
+inline rvalue
+context::new_eq (rvalue a, rvalue b,
+                location loc)
+{
+  return new_comparison (GCC_JIT_COMPARISON_EQ,
+                        a, b, loc);
+}
+inline rvalue
+context::new_ne (rvalue a, rvalue b,
+                location loc)
+{
+  return new_comparison (GCC_JIT_COMPARISON_NE,
+                        a, b, loc);
+}
+inline rvalue
+context::new_lt (rvalue a, rvalue b,
+                location loc)
+{
+  return new_comparison (GCC_JIT_COMPARISON_LT,
+                        a, b, loc);
+}
+inline rvalue
+context::new_le (rvalue a, rvalue b,
+                location loc)
+{
+  return new_comparison (GCC_JIT_COMPARISON_LE,
+                        a, b, loc);
+}
+inline rvalue
+context::new_gt (rvalue a, rvalue b,
+                location loc)
+{
+  return new_comparison (GCC_JIT_COMPARISON_GT,
+                        a, b, loc);
+}
+inline rvalue
+context::new_ge (rvalue a, rvalue b,
+                location loc)
+{
+  return new_comparison (GCC_JIT_COMPARISON_GE,
+                        a, b, loc);
+}
 
 inline rvalue
 context::new_call (function func,
@@ -506,6 +742,86 @@ context::new_call (function func,
                                   args.size (),
                                   as_array_of_ptrs);
 }
+inline rvalue
+context::new_call (function func,
+                  location loc)
+{
+  std::vector<rvalue> args;
+  return new_call (func, args, loc);
+}
+
+inline rvalue
+context::new_call (function func,
+                  rvalue arg0,
+                  location loc)
+{
+  std::vector<rvalue> args(1);
+  args[0] = arg0;
+  return new_call (func, args, loc);
+}
+inline rvalue
+context::new_call (function func,
+                  rvalue arg0, rvalue arg1,
+                  location loc)
+{
+  std::vector<rvalue> args(2);
+  args[0] = arg0;
+  args[1] = arg1;
+  return new_call (func, args, loc);
+}
+inline rvalue
+context::new_call (function func,
+                  rvalue arg0, rvalue arg1, rvalue arg2,
+                  location loc)
+{
+  std::vector<rvalue> args(3);
+  args[0] = arg0;
+  args[1] = arg1;
+  args[2] = arg2;
+  return new_call (func, args, loc);
+}
+inline rvalue
+context::new_call (function func,
+                  rvalue arg0, rvalue arg1, rvalue arg2,
+                  rvalue arg3,
+                  location loc)
+{
+  std::vector<rvalue> args(4);
+  args[0] = arg0;
+  args[1] = arg1;
+  args[2] = arg2;
+  args[3] = arg3;
+  return new_call (func, args, loc);
+}
+inline rvalue
+context::new_call (function func,
+                  rvalue arg0, rvalue arg1, rvalue arg2,
+                  rvalue arg3, rvalue arg4,
+                  location loc)
+{
+  std::vector<rvalue> args(5);
+  args[0] = arg0;
+  args[1] = arg1;
+  args[2] = arg2;
+  args[3] = arg3;
+  args[4] = arg4;
+  return new_call (func, args, loc);
+}
+inline rvalue
+context::new_call (function func,
+                  rvalue arg0, rvalue arg1, rvalue arg2,
+                  rvalue arg3, rvalue arg4, rvalue arg5,
+                  location loc)
+{
+  std::vector<rvalue> args(6);
+  args[0] = arg0;
+  args[1] = arg1;
+  args[2] = arg2;
+  args[3] = arg3;
+  args[4] = arg4;
+  args[5] = arg5;
+  return new_call (func, args, loc);
+}
 
 inline lvalue
 context::new_array_access (rvalue ptr,
diff --git a/gcc/testsuite/ChangeLog.jit b/gcc/testsuite/ChangeLog.jit
index 79d2096..ee2dc40 100644
--- a/gcc/testsuite/ChangeLog.jit
+++ b/gcc/testsuite/ChangeLog.jit
@@ -1,5 +1,11 @@
 2014-02-10  David Malcolm  <dmalc...@redhat.com>
 
+       * jit.dg/test-quadratic.cc (make_calc_discriminant): Make use of
+       new methods of the C++ wrapper API to shorten the example code.
+       (make_test_quadratic): Likewise.
+
+2014-02-10  David Malcolm  <dmalc...@redhat.com>
+
        * jit.dg/test-quadratic.cc (make_test_quadratic): Update for
        change to gccjit::context::new_call to pass args by reference
        rather than by value.
diff --git a/gcc/testsuite/jit.dg/test-quadratic.cc 
b/gcc/testsuite/jit.dg/test-quadratic.cc
index 75725c0..daeaee7 100644
--- a/gcc/testsuite/jit.dg/test-quadratic.cc
+++ b/gcc/testsuite/jit.dg/test-quadratic.cc
@@ -151,29 +151,24 @@ make_calc_discriminant (quadratic_test &testcase)
   testcase.calc_discriminant.add_assignment (
     /* q->discriminant =...  */
     param_q.dereference_field (testcase.discriminant),
-
     /* (q->b * q->b) - (4 * q->a * q->c) */
-    testcase.ctxt.new_binary_op (
-      GCC_JIT_BINARY_OP_MINUS,
+    testcase.ctxt.new_minus (
       testcase.numeric_type,
 
       /* (q->b * q->b) */
-      testcase.ctxt.new_binary_op (
-       GCC_JIT_BINARY_OP_MULT,
+      testcase.ctxt.new_mult (
        testcase.numeric_type,
        q_b, q_b),
 
       /* (4 * (q->a * q->c)) */
-      testcase.ctxt.new_binary_op (
-       GCC_JIT_BINARY_OP_MULT,
+      testcase.ctxt.new_mult (
        testcase.numeric_type,
        /* 4.0 */
        testcase.ctxt.new_rvalue (
          testcase.numeric_type,
          4),
        /* (q->a * q->c) */
-       testcase.ctxt.new_binary_op (
-         GCC_JIT_BINARY_OP_MULT,
+       testcase.ctxt.new_mult (
          testcase.numeric_type,
          q_a, q_c)))); /* end of add_assignment call.  */
 }
@@ -213,12 +208,8 @@ make_test_quadratic (quadratic_test &testcase)
   test_quadratic.add_assignment (q.access_field (testcase.c), c);
   /* calc_discriminant (&q); */
   gccjit::rvalue address_of_q = q.get_address ();
-  std::vector<gccjit::rvalue> args (1);
-  args[0] = address_of_q;
   test_quadratic.add_eval (
-    testcase.ctxt.new_call (
-      testcase.calc_discriminant,
-      args));
+    testcase.ctxt.new_call (testcase.calc_discriminant, address_of_q));
 
   gccjit::label on_positive_discriminant
     = test_quadratic.new_forward_label ("positive_discriminant");
@@ -236,8 +227,7 @@ make_test_quadratic (quadratic_test &testcase)
 
   test_quadratic.add_comment ("if (q.discriminant > 0)");
   test_quadratic.add_conditional (
-    testcase.ctxt.new_comparison (
-      GCC_JIT_COMPARISON_GT,
+    testcase.ctxt.new_gt (
       q.access_field (testcase.discriminant),
       testcase.zero),
     on_positive_discriminant,
@@ -247,21 +237,16 @@ make_test_quadratic (quadratic_test &testcase)
   /* double s = sqrt (q.discriminant); */
   gccjit::lvalue s = test_quadratic.new_local (testcase.numeric_type, "s");
   gccjit::rvalue discriminant_of_q = q.access_field (testcase.discriminant);
-  std::vector<gccjit::rvalue> args_to_sqrt_call (1, discriminant_of_q);
   test_quadratic.add_assignment (
     s,
-    testcase.ctxt.new_call (
-      testcase.sqrt,
-      args_to_sqrt_call));
+    testcase.ctxt.new_call (testcase.sqrt, discriminant_of_q));
 
   gccjit::rvalue minus_b =
-    testcase.ctxt.new_unary_op (
-      GCC_JIT_UNARY_OP_MINUS,
+    testcase.ctxt.new_minus (
       testcase.numeric_type,
       b);
   gccjit::rvalue two_a =
-    testcase.ctxt.new_binary_op (
-      GCC_JIT_BINARY_OP_MULT,
+    testcase.ctxt.new_mult (
       testcase.numeric_type,
       testcase.ctxt.new_rvalue (testcase.numeric_type, 2),
       a);
@@ -274,11 +259,9 @@ make_test_quadratic (quadratic_test &testcase)
     r1.dereference (),
 
     /* (-b + s) / (2 * a) */
-    testcase.ctxt.new_binary_op (
-      GCC_JIT_BINARY_OP_DIVIDE,
+    testcase.ctxt.new_divide (
       testcase.numeric_type,
-      testcase.ctxt.new_binary_op (
-       GCC_JIT_BINARY_OP_PLUS,
+      testcase.ctxt.new_plus (
        testcase.numeric_type,
        minus_b,
        s),
@@ -290,11 +273,9 @@ make_test_quadratic (quadratic_test &testcase)
     r2.dereference (),
 
     /* (-b - s) / (2 * a) */
-    testcase.ctxt.new_binary_op (
-      GCC_JIT_BINARY_OP_DIVIDE,
+    testcase.ctxt.new_divide (
       testcase.numeric_type,
-      testcase.ctxt.new_binary_op (
-       GCC_JIT_BINARY_OP_MINUS,
+      testcase.ctxt.new_minus (
        testcase.numeric_type,
        minus_b,
        s),
@@ -307,8 +288,7 @@ make_test_quadratic (quadratic_test &testcase)
   test_quadratic.place_forward_label (on_nonpositive_discriminant);
   test_quadratic.add_comment ("else if (q.discriminant == 0)");
   test_quadratic.add_conditional (
-    testcase.ctxt.new_comparison (
-      GCC_JIT_COMPARISON_EQ,
+    testcase.ctxt.new_eq (
       q.access_field (testcase.discriminant),
       testcase.zero),
     on_zero_discriminant,
@@ -323,8 +303,7 @@ make_test_quadratic (quadratic_test &testcase)
     r1.dereference (),
 
     /* -b / (2 * a) */
-    testcase.ctxt.new_binary_op (
-      GCC_JIT_BINARY_OP_DIVIDE,
+    testcase.ctxt.new_divide (
       testcase.numeric_type,
       minus_b,
       two_a));
-- 
1.7.11.7

Reply via email to