diff libinterp/corefcn/jit-typeinfo.cc @ 20654:b65888ec820e draft default tip gccjit

dmalcom gcc jit import
author Stefan Mahr <dac922@gmx.de>
date Fri, 27 Feb 2015 16:59:36 +0100
parents d35201e5ce5d
children
line wrap: on
line diff
--- a/libinterp/corefcn/jit-typeinfo.cc	Tue Oct 13 11:40:05 2015 +0100
+++ b/libinterp/corefcn/jit-typeinfo.cc	Fri Feb 27 16:59:36 2015 +0100
@@ -504,9 +504,22 @@
 }
 
 // -------------------- jit_type --------------------
-jit_type::jit_type (const std::string& aname, jit_type *aparent,
-                    llvm::Type *allvm_type, bool askip_paren, int aid) :
-  mname (aname), mparent (aparent), llvm_type (allvm_type), mid (aid),
+jit_type::jit_type (const std::string& aname, jit_type *aparent
+#ifdef HAVE_LLVM
+                    , llvm::Type *allvm_type
+#endif
+#ifdef HAVE_GCCJIT
+                    , gccjit::type agcc_type
+#endif
+                    , bool askip_paren, int aid) :
+  mname (aname), mparent (aparent)
+#ifdef HAVE_LLVM
+  , llvm_type (allvm_type)
+#endif
+#ifdef HAVE_GCCJIT
+  , gccjit_type (agcc_type)
+#endif
+  , mid (aid),
   mdepth (aparent ? aparent->mdepth + 1 : 0), mskip_paren (askip_paren)
 {
   std::memset (msret, 0, sizeof (msret));
@@ -525,14 +538,21 @@
 }
 
 // -------------------- jit_function --------------------
-jit_function::jit_function () : module (0), llvm_function (0), mresult (0),
+jit_function::jit_function () : module (0), llvm_function (0),
+#ifdef HAVE_GCCJIT
+                                gccjit_function (),
+#endif
+                                mresult (0),
                                 call_conv (jit_convention::length),
                                 mcan_error (false)
 {}
 
 jit_function::jit_function (llvm::Module *amodule,
+#ifdef HAVE_GCCJIT
+                            gccjit::context gccjit_ctxt,
+#endif
                             jit_convention::type acall_conv,
-                            const llvm::Twine& aname, jit_type *aresult,
+                            std::string aname, jit_type *aresult,
                             const std::vector<jit_type *>& aargs)
   : module (amodule), mresult (aresult), args (aargs), call_conv (acall_conv),
     mcan_error (false)
@@ -586,17 +606,75 @@
 #else
     llvm_function->addFnAttr (llvm::Attribute::AlwaysInline);
 #endif
+
+#ifdef HAVE_GCCJIT
+    if (gccjit_ctxt.get_inner_context ())
+      {
+        std::vector<gccjit::param> gccjit_params;
+        for (int i = 0; i < args.size (); i++)
+          {
+            jit_type *ty = args[i];
+            assert (ty);
+
+            gccjit::type argty = ty->to_gccjit ();
+            if (ty->pointer_arg (call_conv))
+              argty = argty.get_pointer ();
+
+            std::stringstream paramname;
+            paramname << "arg" << i;
+
+            gccjit::param param = gccjit_ctxt.new_param (argty,
+                                                         paramname.str ());
+            gccjit_params.push_back (param);
+          }
+        gccjit::type gccjit_return_type;
+        if (aresult)
+          gccjit_return_type = aresult->to_gccjit ();
+        else
+          gccjit_return_type = gccjit_ctxt.get_type (GCC_JIT_TYPE_VOID);
+
+        enum gcc_jit_function_kind kind;
+        if (acall_conv == jit_convention::external)
+          kind = GCC_JIT_FUNCTION_IMPORTED;
+        else
+          {
+            if (0)
+              /* Doing this is correct, but makes the dump a little harder
+                 to read.
+                 Also, not fully implemented yet in libgccjit.  */
+              kind = GCC_JIT_FUNCTION_ALWAYS_INLINE;
+            else
+              kind = GCC_JIT_FUNCTION_EXPORTED;
+          }
+        gccjit_function =
+          gccjit_ctxt.new_function (kind,
+                                    gccjit_return_type,
+                                    aname,
+                                    gccjit_params,
+                                    0);
+      }
+#endif
 }
 
 jit_function::jit_function (const jit_function& fn, jit_type *aresult,
                             const std::vector<jit_type *>& aargs)
-  : module (fn.module), llvm_function (fn.llvm_function), mresult (aresult),
+  : module (fn.module),
+    llvm_function (fn.llvm_function),
+#ifdef HAVE_GCCJIT
+    gccjit_function (fn.gccjit_function),
+#endif
+    mresult (aresult),
     args (aargs), call_conv (fn.call_conv), mcan_error (fn.mcan_error)
 {
 }
 
 jit_function::jit_function (const jit_function& fn)
-  : module (fn.module), llvm_function (fn.llvm_function), mresult (fn.mresult),
+  : module (fn.module),
+    llvm_function (fn.llvm_function),
+#ifdef HAVE_GCCJIT
+    gccjit_function (fn.gccjit_function),
+#endif
+    mresult (fn.mresult),
     args (fn.args), call_conv (fn.call_conv), mcan_error (fn.mcan_error)
 {}
 
@@ -754,8 +832,76 @@
 
   if (verify)
     llvm::verifyFunction (*llvm_function);
+
+  if (0)
+    {
+      std::cout << "-------------------- llvm ir (at do_return) --------------------";
+      std::cout << *llvm_function << std::endl;
+    }
 }
 
+#ifdef HAVE_GCCJIT
+#if 1
+gccjit::rvalue
+jit_function::call (gccjit::context ctxt,
+                    gccjit::block block,
+                    const std::vector<jit_value *>& in_args) const
+{
+  assert (in_args.size () == args.size ());
+  std::vector<gccjit::rvalue> gccjit_args (args.size ());
+  for (size_t i = 0; i < in_args.size (); ++i)
+    gccjit_args[i] = in_args[i]->as_rvalue ();
+  return call (ctxt, block, gccjit_args);
+}
+
+gccjit::rvalue
+jit_function::call (gccjit::context ctxt,
+                    gccjit::block block,
+                    std::vector<gccjit::rvalue>& in_args) const
+{
+  assert (in_args.size () == args.size ());
+
+#if 1
+  std::vector<gccjit::rvalue> packed_args (in_args.size ());
+
+  for (size_t i = 0; i < in_args.size (); ++i)
+    {
+      gccjit::rvalue arg = in_args[i];
+#if 0
+      jit_type::convert_fn convert = args[i]->pack (call_conv);
+      if (convert)
+        arg = convert (builder, arg);
+#endif
+
+      if (args[i]->pointer_arg (call_conv))
+        {
+          // The LLVM implementation takes a copy using alloca, passing
+          // a ptr to the copy to the fn.  Emulate this behavior.
+          gccjit::lvalue tmp =
+            block.get_function ().new_local (arg.get_type (), "tmp");
+          block.add_assignment (tmp, arg);
+          arg = tmp.get_address ();
+        }
+
+      packed_args[i] = arg;
+    }
+
+  return ctxt.new_call (gccjit_function,
+                        packed_args);
+#else
+  return ctxt.new_call (gccjit_function,
+                        in_args);
+#endif
+}
+#endif
+
+gccjit::lvalue
+jit_function::argument (size_t idx) const
+{
+  return gccjit_function.get_param (idx);
+}
+#endif
+
 void
 jit_function::do_add_mapping (llvm::ExecutionEngine *engine, void *fn)
 {
@@ -928,6 +1074,7 @@
   return 0;
 }
 
+#ifdef HAVE_LLVM
 llvm::Value *
 jit_index_operation::create_arg_array (llvm::IRBuilderD& builder,
                                        const jit_function &fn, size_t start_idx,
@@ -947,6 +1094,28 @@
   builder.CreateStore (array, array_mem);
   return builder.CreateBitCast (array_mem, scalar_t->getPointerTo ());
 }
+#endif // #ifdef HAVE_LLVM
+
+#ifdef HAVE_GCCJIT
+gccjit::rvalue
+jit_index_operation::create_arg_array (const jit_function &fn,
+                                       gccjit::block block,
+                                       size_t start_idx,
+                                       size_t end_idx) const
+{
+  size_t n = end_idx - start_idx;
+  gccjit::type scalar_t = jit_typeinfo::get_scalar_gccjit ();
+  gccjit::type array_t = block.get_context ().new_array_type (scalar_t, n);
+  gccjit::lvalue array =
+    block.get_function ().new_local (array_t, "tmp_array");
+  for (size_t i = start_idx; i < end_idx; ++i)
+    {
+      gccjit::rvalue idx = fn.argument (i);
+      block.add_assignment (array[i - start_idx], idx);
+    }
+  return array.get_address ().cast_to (scalar_t.get_pointer ());
+}
+#endif // #ifdef HAVE_GCCJIT
 
 // -------------------- jit_paren_subsref --------------------
 jit_function *
@@ -956,7 +1125,11 @@
   ss << "jit_paren_subsref_matrix_scalar" << (types.size () - 1);
 
   jit_type *scalar = jit_typeinfo::get_scalar ();
-  jit_function *fn = new jit_function (module, jit_convention::internal,
+  jit_function *fn = new jit_function (module,
+#ifdef HAVE_GCCJIT
+                                       gccjit_ctxt,
+#endif
+                                       jit_convention::internal,
                                        ss.str (), scalar, types);
   fn->mark_can_error ();
   llvm::BasicBlock *body = fn->new_block ();
@@ -969,6 +1142,26 @@
   llvm::Value *mat = fn->argument (builder, 0);
   llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem);
   fn->do_return (builder, ret);
+
+#ifdef HAVE_GCCJIT
+  // gcc implementation
+  {
+    gccjit::function gf = fn->gccjit_function;
+    gccjit::block body = gf.new_block ();
+    gccjit::rvalue array = create_arg_array (*fn, body, 1, types.size ());
+    jit_type *index = jit_typeinfo::get_index ();
+    gccjit::rvalue nelem = gccjit_ctxt.new_rvalue (index->to_gccjit (),
+                                                   (int)types.size () - 1);
+    gccjit::rvalue mat = gf.get_param (0);
+    std::vector<gccjit::rvalue> args (3);
+    args[0] = mat;
+    args[1] = array;
+    args[2] = nelem;
+    gccjit::rvalue ret = paren_scalar.call (gccjit_ctxt, body, args);
+    body.end_with_return (ret);
+  }
+#endif
+
   return fn;
 }
 
@@ -981,7 +1174,11 @@
   types[2] = jit_typeinfo::get_index ();
 
   jit_type *scalar = jit_typeinfo::get_scalar ();
-  paren_scalar = jit_function (module, jit_convention::external,
+  paren_scalar = jit_function (module,
+#ifdef HAVE_GCCJIT
+                               gccjit_ctxt,
+#endif
+                               jit_convention::external,
                                "octave_jit_paren_scalar", scalar, types);
   paren_scalar.add_mapping (engine, &octave_jit_paren_scalar);
   paren_scalar.mark_can_error ();
@@ -995,7 +1192,11 @@
   ss << "jit_paren_subsasgn_matrix_scalar" << (types.size () - 2);
 
   jit_type *matrix = jit_typeinfo::get_matrix ();
-  jit_function *fn = new jit_function (module, jit_convention::internal,
+  jit_function *fn = new jit_function (module,
+#ifdef HAVE_GCCJIT
+                                       gccjit_ctxt,
+#endif
+                                       jit_convention::internal,
                                        ss.str (), matrix, types);
   fn->mark_can_error ();
   llvm::BasicBlock *body = fn->new_block ();
@@ -1010,6 +1211,28 @@
   llvm::Value *value = fn->argument (builder, types.size () - 1);
   llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem, value);
   fn->do_return (builder, ret);
+
+#ifdef HAVE_GCCJIT
+  {
+    // FIXME: TODO
+    gccjit::function gf = fn->gccjit_function;
+    gccjit::block body = gf.new_block ();
+    gccjit::rvalue array = create_arg_array (*fn, body, 1, types.size () - 1);
+    jit_type *index = jit_typeinfo::get_index ();
+    gccjit::rvalue nelem = gccjit_ctxt.new_rvalue (index->to_gccjit (),
+                                                   (int)types.size () - 2);
+    gccjit::rvalue mat = gf.get_param (0);
+    gccjit::rvalue value = gf.get_param (types.size () - 1);
+    std::vector<gccjit::rvalue> args(4);
+    args[0] = mat;
+    args[1] = array;
+    args[2] = nelem;
+    args[3] = value;
+    gccjit::rvalue ret = paren_scalar.call (gccjit_ctxt, body, args);
+    body.end_with_return (ret);
+  }
+#endif
+
   return fn;
 }
 
@@ -1026,7 +1249,11 @@
   types[2] = jit_typeinfo::get_index ();
   types[3] = jit_typeinfo::get_scalar ();
 
-  paren_scalar = jit_function (module, jit_convention::external,
+  paren_scalar = jit_function (module,
+#ifdef HAVE_GCCJIT
+                               gccjit_ctxt,
+#endif
+                               jit_convention::external,
                                "octave_jit_paren_scalar", matrix, types);
   paren_scalar.add_mapping (engine, &octave_jit_paren_scalar_subsasgn);
   paren_scalar.mark_can_error ();
@@ -1048,7 +1275,12 @@
 {
   instance = this;
 
+#ifdef HAVE_GCCJIT
+  gccjit_ctxt = gccjit::context::acquire ();
+#endif
+
   // FIXME: We should be registering types like in octave_value_typeinfo
+#ifdef HAVE_LLVM
   llvm::Type *any_t = llvm::StructType::create (context, "octave_base_value");
   any_t = any_t->getPointerTo ();
 
@@ -1058,14 +1290,54 @@
   string_t = string_t->getPointerTo ();
   llvm::Type *index_t = llvm::Type::getIntNTy (context,
                                                sizeof(octave_idx_type) * 8);
-
+#endif
+
+#ifdef HAVE_GCCJIT
+  gccjit::type any_t_gcc =
+    gccjit_ctxt.new_opaque_struct_type ("octave_base_value");
+  any_t_gcc = any_t_gcc.get_pointer ();
+  gccjit::type scalar_t_gcc = gccjit_ctxt.get_type (GCC_JIT_TYPE_DOUBLE);
+  gccjit::type bool_t_gcc = gccjit_ctxt.get_type (GCC_JIT_TYPE_BOOL);
+  gccjit::type string_t_gcc =
+    gccjit_ctxt.get_type (GCC_JIT_TYPE_CHAR).get_pointer ();
+  gccjit::type index_t_gcc = gccjit_ctxt.get_int_type <octave_idx_type> ();
+  gccjit::type int_t_gcc = gccjit_ctxt.get_type (GCC_JIT_TYPE_INT);
+#endif
+
+#ifdef HAVE_LLVM
   llvm::StructType *range_t = llvm::StructType::create (context, "range");
   std::vector<llvm::Type *> range_contents (4, scalar_t);
   range_contents[3] = index_t;
   range_t->setBody (range_contents);
-
+#endif
+#ifdef HAVE_GCCJIT
+  field_rng_base = gccjit_ctxt.new_field (scalar_t_gcc, "rng_base");
+  field_rng_limit = gccjit_ctxt.new_field (scalar_t_gcc, "rng_limit");
+  field_rng_inc = gccjit_ctxt.new_field (scalar_t_gcc, "rng_inc");
+  field_rng_nelem = gccjit_ctxt.new_field (index_t_gcc, "rng_nelem");
+  /* FIXME: what about the "mutable Matrix cache;" */
+
+  std::vector<gccjit::field> range_fields (4);
+  range_fields [0] = field_rng_base;
+  range_fields [1] = field_rng_limit;
+  range_fields [2] = field_rng_inc;
+  range_fields [3] = field_rng_nelem;
+
+  gccjit::type range_t_gcc =
+    gccjit_ctxt.new_struct_type (
+      "range",
+      range_fields,
+      gccjit_ctxt.new_location ("liboctave/array/Range.h", 33, 0));
+#endif
+
+#ifdef HAVE_LLVM
   llvm::Type *refcount_t = llvm::Type::getIntNTy (context, sizeof(int) * 8);
-
+#endif
+#ifdef HAVE_GCCJIT
+  gccjit::type refcount_t_gcc = gccjit_ctxt.get_type (GCC_JIT_TYPE_INT);
+#endif
+
+#ifdef HAVE_LLVM
   llvm::StructType *matrix_t = llvm::StructType::create (context, "matrix");
   llvm::Type *matrix_contents[5];
   matrix_contents[0] = refcount_t->getPointerTo ();
@@ -1074,8 +1346,58 @@
   matrix_contents[3] = index_t->getPointerTo ();
   matrix_contents[4] = string_t;
   matrix_t->setBody (llvm::makeArrayRef (matrix_contents, 5));
-
+#endif
+#ifdef HAVE_GCCJIT
+  /*
+    jit-typeinfo.h has, somewhat ominously:
+       // jit_array is compatable with the llvm array/matrix structures
+     typedef jit_array<NDArray, double> jit_matrix;
+  */
+  gccjit::type matrix_t_gcc;
+  {
+    /* typedef jit_array<NDArray, double> jit_matrix; */
+    /* template <typename T, typename U> struct jit_array {...}; */
+    gccjit::type T = string_t_gcc; /* NDArray */
+    gccjit::type U = scalar_t_gcc; /* double */
+
+    /* int *ref_count; */
+    field_ref_count =
+      gccjit_ctxt.new_field (refcount_t_gcc.get_pointer (), "ref_count");
+
+    /* U *slice_data; */
+    field_slice_data =
+      gccjit_ctxt.new_field (U.get_pointer (), "slice_data");
+
+    /* octave_idx_type slice_len; */
+    field_slice_len = gccjit_ctxt.new_field (index_t_gcc, "slice_len");
+
+    /* octave_idx_type *dimensions; */
+    field_dimensions =
+      gccjit_ctxt.new_field (index_t_gcc.get_pointer (), "dimensions");
+
+    /* T *array; */
+    field_array = gccjit_ctxt.new_field (T.get_pointer (), "array");
+
+    std::vector<gccjit::field> matrix_fields (5);
+    matrix_fields[0] = field_ref_count;
+    matrix_fields[1] = field_slice_data;
+    matrix_fields[2] = field_slice_len;
+    matrix_fields[3] = field_dimensions;
+    matrix_fields[4] = field_array;
+
+    matrix_t_gcc = gccjit_ctxt.new_struct_type (
+      "jit_matrix",
+      matrix_fields,
+      gccjit_ctxt.new_location ("jit-typeinfo.h", 106, 0));
+  }
+#endif
+
+#ifdef HAVE_LLVM
   llvm::Type *complex_t = llvm::ArrayType::get (scalar_t, 2);
+#endif
+#ifdef HAVE_GCCJIT
+  gccjit::type complex_t_gcc = gccjit_ctxt.new_array_type (scalar_t_gcc, 2);
+#endif
 
   // complex_ret is what is passed to C functions in order to get calling
   // convention right
@@ -1089,6 +1411,20 @@
   }
 
   // create types
+#ifdef HAVE_GCCJIT
+  any = new_type ("any", 0, any_t, any_t_gcc);
+  matrix = new_type ("matrix", any, matrix_t, matrix_t_gcc);
+  complex = new_type ("complex", any, complex_t, complex_t_gcc);
+  scalar = new_type ("scalar", complex, scalar_t, scalar_t_gcc);
+  scalar_ptr = new_type ("scalar_ptr", 0, scalar_t->getPointerTo (),
+    scalar_t_gcc.get_pointer ());
+  any_ptr = new_type ("any_ptr", 0, any_t->getPointerTo (),
+    any_t_gcc.get_pointer());
+  range = new_type ("range", any, range_t, range_t_gcc);
+  string = new_type ("string", any, string_t, string_t_gcc);
+  boolean = new_type ("bool", any, bool_t, bool_t_gcc);
+  index = new_type ("index", any, index_t, index_t_gcc);
+#else
   any = new_type ("any", 0, any_t);
   matrix = new_type ("matrix", any, matrix_t);
   complex = new_type ("complex", any, complex_t);
@@ -1099,6 +1435,7 @@
   string = new_type ("string", any, string_t);
   boolean = new_type ("bool", any, bool_t);
   index = new_type ("index", any, index_t);
+#endif
 
   create_int (8);
   create_int (16);
@@ -1124,10 +1461,16 @@
   if (sizeof (void *) == 4)
     complex->mark_sret (jit_convention::external);
 
+#ifdef HAVE_GCCJIT
+  paren_subsref_fn.initialize (module, engine, gccjit_ctxt);
+  paren_subsasgn_fn.initialize (module, engine, gccjit_ctxt);
+#else
   paren_subsref_fn.initialize (module, engine);
   paren_subsasgn_fn.initialize (module, engine);
+#endif
 
   // bind global variables
+#ifdef HAVE_LLVM
   lerror_state = new llvm::GlobalVariable (*module, bool_t, false,
                                            llvm::GlobalValue::ExternalLinkage,
                                            0, "error_state");
@@ -1142,6 +1485,20 @@
                                 "octave_interrupt_state");
   engine->addGlobalMapping (loctave_interrupt_state,
                             reinterpret_cast<void *> (&octave_interrupt_state));
+#endif
+
+#ifdef HAVE_GCCJIT
+  // Access "error_state" (actually declared as an "int"), by
+  // taking its address and dereferencing:
+  error_state_gccjit =
+    *gccjit_ctxt.new_rvalue (int_t_gcc.get_pointer (),
+                             &error_state);
+  sig_atomic_type_gccjit = gccjit_ctxt.get_int_type <sig_atomic_t> ();
+  octave_interrupt_state_gccjit =
+    *gccjit_ctxt.new_rvalue (
+      sig_atomic_type_gccjit.get_volatile ().get_pointer (),
+        &octave_interrupt_state);
+#endif
 
   // generic call function
   {
@@ -1175,8 +1532,10 @@
 
   for (int op = 0; op < octave_value::num_binary_ops; ++op)
     {
-      llvm::Twine fn_name ("octave_jit_binary_any_any_");
-      fn_name = fn_name + llvm::Twine (op);
+      std::string fn_name ("octave_jit_binary_any_any_");
+      char buf[64];
+      snprintf (buf, sizeof(buf), "%i", op);
+      fn_name = fn_name + std::string (buf);
 
       fn = create_internal (fn_name, any, any, any);
       fn.mark_can_error ();
@@ -1189,6 +1548,20 @@
                                           fn.argument (builder, 0),
                                           fn.argument (builder, 1));
       fn.do_return (builder, ret);
+
+#ifdef HAVE_GCCJIT
+      // gccjit implementation of octave_jit_binary_any_any_*:
+      gccjit::function gf = fn.gccjit_function;
+      gccjit::block gccjit_block = gf.new_block ();
+      gccjit_block.end_with_return (
+        gccjit_block.add_call (
+          any_binary.gccjit_function,
+          gccjit_ctxt.new_rvalue (binary_op_type->to_gccjit (),
+                                  op),
+          gf.get_param (0),
+          gf.get_param (1)));
+#endif
+
       binary_ops[op].add_overload (fn);
     }
 
@@ -1220,9 +1593,40 @@
   destroy_fn.add_overload (create_identity(index));
   destroy_fn.add_overload (create_identity(complex));
 
-  // -------------------- scalar related operations --------------------
-
   // now for binary scalar operations
+#ifdef HAVE_GCCJIT
+  add_binary_op (scalar, octave_value::op_add,
+                 llvm::Instruction::FAdd,
+                 GCC_JIT_BINARY_OP_PLUS);
+  add_binary_op (scalar, octave_value::op_sub,
+                 llvm::Instruction::FSub,
+                 GCC_JIT_BINARY_OP_MINUS);
+  add_binary_op (scalar, octave_value::op_mul,
+                 llvm::Instruction::FMul,
+                 GCC_JIT_BINARY_OP_MULT);
+  add_binary_op (scalar, octave_value::op_el_mul,
+                 llvm::Instruction::FMul,
+                 GCC_JIT_BINARY_OP_MULT);
+
+  add_binary_fcmp (scalar, octave_value::op_lt,
+                   llvm::CmpInst::FCMP_ULT,
+                   GCC_JIT_COMPARISON_LT);
+  add_binary_fcmp (scalar, octave_value::op_le,
+                   llvm::CmpInst::FCMP_ULE,
+                   GCC_JIT_COMPARISON_LE);
+  add_binary_fcmp (scalar, octave_value::op_eq,
+                   llvm::CmpInst::FCMP_UEQ,
+                   GCC_JIT_COMPARISON_EQ);
+  add_binary_fcmp (scalar, octave_value::op_ge,
+                   llvm::CmpInst::FCMP_UGE,
+                   GCC_JIT_COMPARISON_GE);
+  add_binary_fcmp (scalar, octave_value::op_gt,
+                   llvm::CmpInst::FCMP_UGT,
+                   GCC_JIT_COMPARISON_GT);
+  add_binary_fcmp (scalar, octave_value::op_ne,
+                   llvm::CmpInst::FCMP_UNE,
+                   GCC_JIT_COMPARISON_NE);
+#else
   add_binary_op (scalar, octave_value::op_add, llvm::Instruction::FAdd);
   add_binary_op (scalar, octave_value::op_sub, llvm::Instruction::FSub);
   add_binary_op (scalar, octave_value::op_mul, llvm::Instruction::FMul);
@@ -1234,6 +1638,7 @@
   add_binary_fcmp (scalar, octave_value::op_ge, llvm::CmpInst::FCMP_UGE);
   add_binary_fcmp (scalar, octave_value::op_gt, llvm::CmpInst::FCMP_UGT);
   add_binary_fcmp (scalar, octave_value::op_ne, llvm::CmpInst::FCMP_UNE);
+#endif
 
   jit_function gripe_div0 = create_external (JIT_FN (gripe_divide_by_zero), 0);
   gripe_div0.mark_can_error ();
@@ -1261,6 +1666,25 @@
                                            fn.argument (builder, 1));
     fn.do_return (builder, ret);
   }
+
+  // gccjit implementation of octave_jit_div_scalar_scalar:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block initial_block = gf.new_block ("initial");
+    gccjit::block warn_block = gf.new_block ("warn");
+    gccjit::block normal_block = gf.new_block ("normal");
+    initial_block.end_with_conditional (
+      gf.get_param (1) != scalar_t_gcc.zero (),
+      normal_block, // on_true
+      warn_block); // on_false
+
+    warn_block.add_call (gripe_div0.gccjit_function);
+    warn_block.end_with_jump (normal_block);
+
+    normal_block.end_with_return (gf.get_param (0) / gf.get_param (1));
+  }
+#endif
   binary_ops[octave_value::op_div].add_overload (fn);
   binary_ops[octave_value::op_el_div].add_overload (fn);
 
@@ -1279,7 +1703,7 @@
 
   // now for unary scalar operations
   // FIXME: Impelment not
-  fn = create_internal ("octave_jit_++", scalar, scalar);
+  fn = create_internal ("octave_jit_plusplus", scalar, scalar);
   body = fn.new_block ();
   builder.SetInsertPoint (body);
   {
@@ -1288,9 +1712,17 @@
     val = builder.CreateFAdd (val, one);
     fn.do_return (builder, val);
   }
+  // gccjit implementation of octave_jit_plusplus:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (gf.get_param (0) + scalar_t_gcc.one ());
+  }
+#endif
   unary_ops[octave_value::op_incr].add_overload (fn);
 
-  fn = create_internal ("octave_jit_--", scalar, scalar);
+  fn = create_internal ("octave_jit_minusminus", scalar, scalar);
   body = fn.new_block ();
   builder.SetInsertPoint (body);
   {
@@ -1298,7 +1730,15 @@
     llvm::Value *val = fn.argument (builder, 0);
     val = builder.CreateFSub (val, one);
     fn.do_return (builder, val);
+
+    // gccjit implementation of octave_jit_minusminus:
+#ifdef HAVE_GCCJIT
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (gf.get_param (0) - scalar_t_gcc.one ());
+#endif
   }
+
   unary_ops[octave_value::op_decr].add_overload (fn);
 
   fn = create_internal ("octave_jit_uminus", scalar, scalar);
@@ -1310,7 +1750,14 @@
     val = builder.CreateFMul (val, mone);
     fn.do_return (builder, val);
   }
-  unary_ops[octave_value::op_uminus].add_overload (fn);
+  // gccjit implementation of octave_jit_uminus:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (-gf.get_param (0));
+  }
+#endif
 
   fn = create_identity (scalar);
   unary_ops[octave_value::op_uplus].add_overload (fn);
@@ -1318,7 +1765,7 @@
   unary_ops[octave_value::op_hermitian].add_overload (fn);
 
   // now for binary complex operations
-  fn = create_internal ("octave_jit_+_complex_complex", complex, complex,
+  fn = create_internal ("octave_jit_plus_complex_complex", complex, complex,
                         complex);
   body = fn.new_block ();
   builder.SetInsertPoint (body);
@@ -1331,9 +1778,21 @@
                                             complex_imag (rhs));
     fn.do_return (builder, complex_new (real, imag));
   }
+  // gccjit implementation of octave_jit_plus_complex_complex:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    gccjit::rvalue lhs = gf.get_param (0);
+    gccjit::rvalue rhs = gf.get_param (1);
+    gccjit::rvalue real = complex_real (lhs) + complex_real (rhs);
+    gccjit::rvalue imag = complex_imag (lhs) + complex_imag (rhs);
+    b.end_with_return (complex_new (b, real, imag));
+  }
+#endif
   binary_ops[octave_value::op_add].add_overload (fn);
 
-  fn = create_internal ("octave_jit_-_complex_complex", complex, complex,
+  fn = create_internal ("octave_jit_minus_complex_complex", complex, complex,
                         complex);
   body = fn.new_block ();
   builder.SetInsertPoint (body);
@@ -1346,6 +1805,18 @@
                                             complex_imag (rhs));
     fn.do_return (builder, complex_new (real, imag));
   }
+  // gccjit implementation of octave_jit_minus_complex_complex:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    gccjit::rvalue lhs = gf.get_param (0);
+    gccjit::rvalue rhs = gf.get_param (1);
+    gccjit::rvalue real = complex_real (lhs) - complex_real (rhs);
+    gccjit::rvalue imag = complex_imag (lhs) - complex_imag (rhs);
+    b.end_with_return (complex_new (b, real, imag));
+  }
+#endif
   binary_ops[octave_value::op_sub].add_overload (fn);
 
   fn = create_external (JIT_FN (octave_jit_complex_mul),
@@ -1364,7 +1835,7 @@
   binary_ops[octave_value::op_pow].add_overload (fn);
   binary_ops[octave_value::op_el_pow].add_overload (fn);
 
-  fn = create_internal ("octave_jit_*_scalar_complex", complex, scalar,
+  fn = create_internal ("octave_jit_mult_scalar_complex", complex, scalar,
                         complex);
   jit_function mul_scalar_complex = fn;
   body = fn.new_block ();
@@ -1391,6 +1862,31 @@
                         builder.CreateFMul (lhs, complex_imag (rhs)));
     fn.do_return (builder, temp);
   }
+  // gccjit implementation of octave_jit_mult_scalar_complex:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+
+    gccjit::rvalue fzero = scalar_t_gcc.zero ();
+    gccjit::rvalue lhs = gf.get_param (0);
+    gccjit::rvalue rhs = gf.get_param (1);
+
+    gccjit::block initial = gf.new_block ("initial");
+    gccjit::block complex_mul = gf.new_block ("complex_mul");
+    gccjit::block scalar_mul = gf.new_block ("scalar_mul");
+
+    initial.end_with_conditional (complex_imag (rhs) == fzero,
+                                  scalar_mul,
+                                  complex_mul);
+
+    scalar_mul.end_with_return (complex_new (scalar_mul,
+                                             lhs * complex_real (rhs),
+                                             fzero));
+    complex_mul.end_with_return (complex_new (complex_mul,
+                                              lhs * complex_real (rhs),
+                                              lhs * complex_imag (rhs)));
+  }
+#endif
   binary_ops[octave_value::op_mul].add_overload (fn);
   binary_ops[octave_value::op_el_mul].add_overload (fn);
 
@@ -1399,7 +1895,7 @@
   binary_ops[octave_value::op_mul].add_overload (fn);
   binary_ops[octave_value::op_el_mul].add_overload (fn);
 
-  fn = create_internal ("octave_jit_+_scalar_complex", complex, scalar,
+  fn = create_internal ("octave_jit_plus_scalar_complex", complex, scalar,
                         complex);
   body = fn.new_block ();
   builder.SetInsertPoint (body);
@@ -1409,12 +1905,23 @@
     llvm::Value *real = builder.CreateFAdd (lhs, complex_real (rhs));
     fn.do_return (builder, complex_real (rhs, real));
   }
+  // gccjit implementation of octave_jit_plus_scalar_complex:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    gccjit::rvalue lhs = gf.get_param (0);
+    gccjit::lvalue rhs = gf.get_param (1);
+    gccjit::rvalue real = lhs + complex_real (rhs);
+    b.end_with_return (complex_real (b, rhs, real));
+  }
+#endif
   binary_ops[octave_value::op_add].add_overload (fn);
 
   fn = mirror_binary (fn);
   binary_ops[octave_value::op_add].add_overload (fn);
 
-  fn = create_internal ("octave_jit_-_complex_scalar", complex, complex,
+  fn = create_internal ("octave_jit_minus_complex_scalar", complex, complex,
                         scalar);
   body = fn.new_block ();
   builder.SetInsertPoint (body);
@@ -1424,9 +1931,20 @@
     llvm::Value *real = builder.CreateFSub (complex_real (lhs), rhs);
     fn.do_return (builder, complex_real (lhs, real));
   }
+  // gccjit implementation of octave_jit_minus_complex_scalar:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    gccjit::lvalue lhs = gf.get_param (0);
+    gccjit::rvalue rhs = gf.get_param (1);
+    gccjit::rvalue real = complex_real (lhs) - rhs;
+    b.end_with_return (complex_real (b, lhs, real));
+  }
+#endif
   binary_ops[octave_value::op_sub].add_overload (fn);
 
-  fn = create_internal ("octave_jit_-_scalar_complex", complex, scalar,
+  fn = create_internal ("octave_jit_minus_scalar_complex", complex, scalar,
                         complex);
   body = fn.new_block ();
   builder.SetInsertPoint (body);
@@ -1436,6 +1954,17 @@
     llvm::Value *real = builder.CreateFSub (lhs, complex_real (rhs));
     fn.do_return (builder, complex_real (rhs, real));
   }
+  // gccjit implementation of octave_jit_minus_scalar_complex:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    gccjit::rvalue lhs = gf.get_param (0);
+    gccjit::lvalue rhs = gf.get_param (1);
+    gccjit::rvalue real = lhs - complex_real (rhs);
+    b.end_with_return (complex_real (b, rhs, real));
+  }
+#endif
   binary_ops[octave_value::op_sub].add_overload (fn);
 
   fn = create_external (JIT_FN (octave_jit_pow_scalar_complex), complex, scalar,
@@ -1449,11 +1978,25 @@
   binary_ops[octave_value::op_el_pow].add_overload (fn);
 
   // now for binary index operators
+#ifdef HAVE_GCCJIT
+  add_binary_op (index, octave_value::op_add,
+                 llvm::Instruction::Add,
+                 GCC_JIT_BINARY_OP_PLUS);
+
+  // and binary bool operators
+  add_binary_op (boolean, octave_value::op_el_or,
+                 llvm::Instruction::Or,
+                 GCC_JIT_BINARY_OP_LOGICAL_OR);
+  add_binary_op (boolean, octave_value::op_el_and,
+                 llvm::Instruction::And,
+                 GCC_JIT_BINARY_OP_LOGICAL_AND);
+#else
   add_binary_op (index, octave_value::op_add, llvm::Instruction::Add);
 
   // and binary bool operators
   add_binary_op (boolean, octave_value::op_el_or, llvm::Instruction::Or);
   add_binary_op (boolean, octave_value::op_el_and, llvm::Instruction::And);
+#endif
 
   // now for printing functions
   print_fn.stash_name ("print");
@@ -1470,6 +2013,13 @@
     llvm::Value *zero = llvm::ConstantInt::get (index_t, 0);
     fn.do_return (builder, zero);
   }
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (gccjit_ctxt.zero (index_t_gcc));
+  }
+#endif
   for_init_fn.add_overload (fn);
 
   // bounds check for for loop
@@ -1484,7 +2034,20 @@
     llvm::Value *idx = fn.argument (builder, 1);
     llvm::Value *ret = builder.CreateICmpULT (idx, nelem);
     fn.do_return (builder, ret);
+
   }
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    // FIXME: is "range" a (struct range) or a (struct range *) ?
+    gccjit::rvalue nelem
+      = gf.get_param (0).access_field (field_rng_nelem);
+    gccjit::rvalue idx = gf.get_param (1);
+    gccjit::rvalue ret = idx < nelem;
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (ret);
+  }
+#endif
   for_check_fn.add_overload (fn);
 
   // index variabe for for loop
@@ -1504,6 +2067,20 @@
     ret = builder.CreateFAdd (base, ret);
     fn.do_return (builder, ret);
   }
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    gccjit::rvalue idx = gf.get_param (1);
+    gccjit::rvalue didx = idx.cast_to (scalar_t_gcc);
+    gccjit::rvalue rng = gf.get_param (0);
+    gccjit::rvalue base = rng.access_field (field_rng_base);
+    gccjit::rvalue inc = rng.access_field (field_rng_inc);
+    gccjit::rvalue ret = didx * inc;
+    ret = base + ret;
+    b.end_with_return (ret);
+  }
+#endif
   for_index_fn.add_overload (fn);
 
   // logically true
@@ -1535,6 +2112,22 @@
     llvm::Value *ret = builder.CreateFCmpONE (fn.argument (builder, 0), zero);
     fn.do_return (builder, ret);
   }
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block initial = gf.new_block ("initial");
+    gccjit::block error = gf.new_block ("error");
+    gccjit::block normal = gf.new_block ("normal");
+    initial.add_comment ("check for NaN");
+    initial.end_with_conditional (gf.get_param (0) != gf.get_param (0),
+                                  error, normal);
+
+    error.add_call (gripe_nantl.gccjit_function);
+    error.end_with_jump (normal);
+    
+    normal.end_with_return ( gf.get_param (0) != scalar_t_gcc.zero ());
+  }
+#endif
   logically_true_fn.add_overload (fn);
 
   // logically_true boolean
@@ -1568,6 +2161,22 @@
     rng = builder.CreateInsertValue (rng, nelem, 3);
     fn.do_return (builder, rng);
   }
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::rvalue base = gf.get_param (0);
+    gccjit::rvalue limit = gf.get_param (1);
+    gccjit::rvalue inc = gf.get_param (2);
+    gccjit::block b = gf.new_block ();
+    gccjit::rvalue nelem = compute_nelem.gccjit_function (base, limit, inc);
+    gccjit::lvalue rng = gf.new_local (range_t_gcc, "rng");
+    b.add_assignment (rng.access_field (field_rng_base), base);
+    b.add_assignment (rng.access_field (field_rng_limit), limit);
+    b.add_assignment (rng.access_field (field_rng_inc), inc);
+    b.add_assignment (rng.access_field (field_rng_nelem), nelem);
+    b.end_with_return (rng);
+  }
+#endif
   make_range_fn.add_overload (fn);
 
   // paren_subsref
@@ -1579,7 +2188,7 @@
                                                0, jit_int, jit_int, index,
                                                index);
 
-  fn = create_internal ("()subsref", scalar, matrix, scalar);
+  fn = create_internal ("subsref", scalar, matrix, scalar);
   fn.mark_can_error ();
 
   body = fn.new_block ();
@@ -1638,10 +2247,55 @@
     merge->addIncoming (ret, success);
     fn.do_return (builder, merge);
   }
+  // gccjit implementation of subsref:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block initial = gf.new_block ("initial");
+
+    gccjit::rvalue one_idx = index_t_gcc.one ();
+    gccjit::rvalue one_int = jit_int->to_gccjit ().one ();
+
+    gccjit::rvalue mat = gf.get_param (0);
+    gccjit::rvalue idx = gf.get_param (1); // scalar
+
+    // convert index to scalar to integer, and check index >= 1
+    gccjit::rvalue int_idx = idx.cast_to (index_t_gcc);
+    gccjit::rvalue check_idx = int_idx.cast_to (scalar_t_gcc);
+    gccjit::rvalue cond0 = (idx != check_idx);
+    gccjit::rvalue cond1 = (int_idx < one_idx);
+
+    gccjit::block conv_error = gf.new_block ("conv_error");
+    gccjit::block normal = gf.new_block ("normal");
+    initial.end_with_conditional (cond0 || cond1, conv_error, normal);
+
+    // "conv_error" block:
+    conv_error.add_call (ginvalid_index.gccjit_function);
+    conv_error.end_with_return (scalar_t_gcc.zero ()); // dummy value
+
+    // "normal" block:
+    gccjit::rvalue len = mat.access_field (field_slice_len);
+    gccjit::rvalue cond = (int_idx > len);
+    gccjit::block bounds_error = gf.new_block ("bounds_error");
+    gccjit::block success = gf.new_block ("success");
+    normal.end_with_conditional (cond, bounds_error, success);
+
+    // "bounds_error" block:
+    bounds_error.add_call (gindex_range.gccjit_function,
+                           one_int, one_int, int_idx, len);
+    bounds_error.end_with_return (scalar_t_gcc.zero ()); // dummy value
+
+    // "success" block:
+    gccjit::rvalue data = mat.access_field (field_slice_data);
+    gccjit::rvalue gep = data[int_idx];
+    gccjit::rvalue ret = gep;
+    success.end_with_return (ret);
+  }
+#endif
   paren_subsref_fn.add_overload (fn);
 
   // paren subsasgn
-  paren_subsasgn_fn.stash_name ("()subsasgn");
+  paren_subsasgn_fn.stash_name ("subsasgn");
 
   jit_function resize_paren_subsasgn
     = create_external (JIT_FN (octave_jit_paren_subsasgn_impl), matrix, matrix,
@@ -1710,6 +2364,61 @@
     merge->addIncoming (mat, success);
     fn.do_return (builder, merge);
   }
+  // gccjit implementation of octave_jit_paren_subsasgn:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block initial = gf.new_block ("initial");
+    gccjit::rvalue one_idx = index_t_gcc.one ();
+    gccjit::rvalue one_int = jit_int->to_gccjit ().one ();
+
+    gccjit::lvalue mat = gf.get_param (0);
+    gccjit::rvalue idx = gf.get_param (1);
+    gccjit::rvalue value = gf.get_param (2);
+    gccjit::rvalue int_idx = idx.cast_to (index_t_gcc);
+    gccjit::rvalue check_idx = int_idx.cast_to (scalar_t_gcc);
+    gccjit::rvalue cond0 = (idx != check_idx);
+    gccjit::rvalue cond1 = (int_idx < one_idx);
+    gccjit::rvalue cond = (cond0 || cond1);
+
+    gccjit::block conv_error = gf.new_block ("conv_error");
+    gccjit::block normal = gf.new_block ("normal");
+    initial.end_with_conditional (cond, conv_error, normal);
+
+    // block: conv_error
+    conv_error.add_call (ginvalid_index.gccjit_function);
+    conv_error.end_with_return (mat);
+
+    // block: normal
+    gccjit::rvalue len = mat.access_field (field_slice_len);
+    cond0 = (int_idx > len);
+
+    gccjit::rvalue rcount = mat.access_field (field_ref_count);
+    rcount = rcount.dereference ();
+    cond1 = rcount > one_int;
+    cond = (cond0 || cond1);
+
+    gccjit::block bounds_error = gf.new_block ("bounds_error");
+    gccjit::block success = gf.new_block ("success");
+    normal.end_with_conditional (cond, bounds_error, success);
+
+    // block: bounds_error
+    // resize on out of bounds access
+    std::vector<gccjit::rvalue> args (3);
+    args[0] = mat;
+    args[1] = int_idx;
+    args[2] = value;
+    gccjit::rvalue resize_result =
+      resize_paren_subsasgn.call (gccjit_ctxt, bounds_error, args);
+    bounds_error.end_with_return (resize_result);
+
+    // block: success
+    gccjit::rvalue data = mat.access_field (field_slice_data);
+    gccjit::lvalue gep = data[int_idx];
+    success.add_assignment (gep, value);
+    success.end_with_return (mat);
+  }
+#endif
   paren_subsasgn_fn.add_overload (fn);
 
   fn = create_external (JIT_FN (octave_jit_paren_subsasgn_matrix_range), matrix,
@@ -1726,6 +2435,16 @@
     llvm::Value *ret = builder.CreateExtractValue (mat, 2);
     fn.do_return (builder, builder.CreateSIToFP (ret, scalar_t));
   }
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::rvalue mat = gf.get_param (0);
+    // FIXME: is this the right behavior?
+    gccjit::rvalue ret = mat.access_field (field_slice_len);
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (ret.cast_to (scalar_t_gcc));
+  }
+#endif
   end1_fn.add_overload (fn);
 
   end_fn.stash_name ("end");
@@ -1784,6 +2503,15 @@
     llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0);
     fn.do_return (builder, complex_new (fn.argument (builder, 0), zero));
   }
+  // gccjit implementation of octave_jit_cast_complex_scalar:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    gccjit::rvalue zero = scalar_t_gcc.zero ();
+    b.end_with_return (complex_new (b, gf.get_param (0), zero));
+  }
+#endif
   casts[complex->type_id ()].add_overload (fn);
 
   // cast scalar <- complex
@@ -1791,6 +2519,13 @@
   body = fn.new_block ();
   builder.SetInsertPoint (body);
   fn.do_return (builder, complex_real (fn.argument (builder, 0)));
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (complex_real (gf.get_param (0)));
+  }
+#endif
   casts[scalar->type_id ()].add_overload (fn);
 
   // cast any <- any
@@ -1818,7 +2553,9 @@
   register_generic ("cos", matrix, matrix);
 
   add_builtin ("exp");
-  register_intrinsic ("exp", llvm::Intrinsic::exp, scalar, scalar);
+  // FIXME: looks like a typo: "cos" here should be "exp":
+  // filed as http://savannah.gnu.org/bugs/index.php?41560
+  register_intrinsic ("exp", llvm::Intrinsic::cos, scalar, scalar);
   register_generic ("exp", matrix, matrix);
 
   add_builtin ("balance");
@@ -1833,10 +2570,13 @@
   add_builtin ("norm");
   register_generic ("norm", scalar, matrix);
 
+  //FIXME: gccjit can't yet cope with duplicate names
+#if !defined (HAVE_GCCJIT)
   add_builtin ("rand");
   register_generic ("rand", matrix, scalar);
   register_generic ("rand", matrix, std::vector<jit_type *> (2, scalar));
 
+
   add_builtin ("magic");
   register_generic ("magic", matrix, scalar);
   register_generic ("magic", matrix, std::vector<jit_type *> (2, scalar));
@@ -1844,6 +2584,7 @@
   add_builtin ("eye");
   register_generic ("eye", matrix, scalar);
   register_generic ("eye", matrix, std::vector<jit_type *> (2, scalar));
+#endif
 
   add_builtin ("mod");
   register_generic ("mod", scalar, std::vector<jit_type *> (2, scalar));
@@ -1870,6 +2611,9 @@
       casts[btype->type_id ()].add_overload (jit_function (any_id, btype,
                                                            args));
     }
+#ifdef HAVE_GCCJIT
+  gccjit_ctxt.dump_to_file ("/tmp/jit-typeinfo-dump.c", true);
+#endif
 }
 
 const jit_function&
@@ -1883,10 +2627,23 @@
 }
 
 jit_type*
-jit_typeinfo::new_type (const std::string& name, jit_type *parent,
-                        llvm::Type *llvm_type, bool skip_paren)
+jit_typeinfo::new_type (const std::string& name, jit_type *parent
+#ifdef HAVE_LLVM
+                        , llvm::Type *llvm_type
+#endif
+#ifdef HAVE_GCCJIT
+                        , gccjit::type gccjit_type
+#endif
+                        , bool skip_paren)
 {
-  jit_type *ret = new jit_type (name, parent, llvm_type, skip_paren, next_id++);
+  jit_type *ret = new jit_type (name, parent
+#ifdef HAVE_LLVM
+                        , llvm_type
+#endif
+#ifdef HAVE_GCCJIT
+                        , gccjit_type
+#endif
+                                , skip_paren, next_id++);
   id_to_type.push_back (ret);
   return ret;
 }
@@ -1903,14 +2660,21 @@
 
 // FIXME: cp between add_binary_op, add_binary_icmp, and add_binary_fcmp
 void
-jit_typeinfo::add_binary_op (jit_type *ty, int op, int llvm_op)
+jit_typeinfo::add_binary_op (jit_type *ty, int op
+                             , int llvm_op
+#ifdef HAVE_GCCJIT
+                             , enum gcc_jit_binary_op gccjit_op
+#endif
+                            )
 {
   std::stringstream fname;
   octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op);
-  fname << "octave_jit_" << octave_value::binary_op_as_string (ov_op)
+  fname << "octave_jit_" << octave_value::binary_op_fcn_name (ov_op)
         << "_" << ty->name ();
 
   jit_function fn = create_internal (fname.str (), ty, ty, ty);
+
+  // LLVM implementation:
   llvm::BasicBlock *block = fn.new_block ();
   builder.SetInsertPoint (block);
   llvm::Instruction::BinaryOps temp
@@ -1919,18 +2683,37 @@
   llvm::Value *ret = builder.CreateBinOp (temp, fn.argument (builder, 0),
                                           fn.argument (builder, 1));
   fn.do_return (builder, ret);
+
+  // gccjit implementation:
+#ifdef HAVE_GCCJIT
+  gccjit::function gf = fn.gccjit_function;
+  gccjit::block b = gf.new_block ();
+  b.end_with_return (
+    gf.get_context ().new_binary_op (
+      gccjit_op,
+      ty->to_gccjit (),
+      gf.get_param (0),
+      gf.get_param (1)));                              
+#endif
   binary_ops[op].add_overload (fn);
 }
 
 void
-jit_typeinfo::add_binary_icmp (jit_type *ty, int op, int llvm_op)
+jit_typeinfo::add_binary_icmp (jit_type *ty, int op
+                               , int llvm_op
+#ifdef HAVE_GCCJIT
+                               , enum gcc_jit_comparison gccjit_op
+#endif
+                              )
 {
   std::stringstream fname;
   octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op);
-  fname << "octave_jit" << octave_value::binary_op_as_string (ov_op)
+  fname << "octave_jit_" << octave_value::binary_op_fcn_name (ov_op)
         << "_" << ty->name ();
 
   jit_function fn = create_internal (fname.str (), boolean, ty, ty);
+
+  // LLVM implementation:
   llvm::BasicBlock *block = fn.new_block ();
   builder.SetInsertPoint (block);
   llvm::CmpInst::Predicate temp
@@ -1938,18 +2721,36 @@
   llvm::Value *ret = builder.CreateICmp (temp, fn.argument (builder, 0),
                                          fn.argument (builder, 1));
   fn.do_return (builder, ret);
+
+  // gccjit implementation:
+#ifdef HAVE_GCCJIT
+  gccjit::function gf = fn.gccjit_function;
+  gccjit::block b = gf.new_block ();
+  b.end_with_return (
+    gf.get_context ().new_comparison (
+      gccjit_op,
+      gf.get_param (0),
+      gf.get_param (1)));                              
+#endif
   binary_ops[op].add_overload (fn);
 }
 
 void
-jit_typeinfo::add_binary_fcmp (jit_type *ty, int op, int llvm_op)
+jit_typeinfo::add_binary_fcmp (jit_type *ty, int op
+                               , int llvm_op
+#ifdef HAVE_GCCJIT
+                               , enum gcc_jit_comparison gccjit_op
+#endif
+                              )
 {
   std::stringstream fname;
   octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op);
-  fname << "octave_jit" << octave_value::binary_op_as_string (ov_op)
+  fname << "octave_jit_" << octave_value::binary_op_fcn_name (ov_op)
         << "_" << ty->name ();
 
   jit_function fn = create_internal (fname.str (), boolean, ty, ty);
+
+  // LLVM implementation:
   llvm::BasicBlock *block = fn.new_block ();
   builder.SetInsertPoint (block);
   llvm::CmpInst::Predicate temp
@@ -1957,15 +2758,30 @@
   llvm::Value *ret = builder.CreateFCmp (temp, fn.argument (builder, 0),
                                          fn.argument (builder, 1));
   fn.do_return (builder, ret);
+
+  // gccjit implementation:
+#ifdef HAVE_GCCJIT
+  gccjit::function gf = fn.gccjit_function;
+  gccjit::block b = gf.new_block ();
+  b.end_with_return (
+    gf.get_context ().new_comparison (
+      gccjit_op,
+      gf.get_param (0),
+      gf.get_param (1)));                              
+#endif
   binary_ops[op].add_overload (fn);
 }
 
 jit_function
-jit_typeinfo::create_function (jit_convention::type cc, const llvm::Twine& name,
+jit_typeinfo::create_function (jit_convention::type cc, std::string name,
                                jit_type *ret,
                                const std::vector<jit_type *>& args)
 {
-  jit_function result (module, cc, name, ret, args);
+  jit_function result (module,
+#ifdef HAVE_GCCJIT
+                       gccjit_ctxt,
+#endif
+                       cc, name, ret, args);
   return result;
 }
 
@@ -1982,15 +2798,26 @@
       name << "id_" << type->name ();
 
       jit_function fn = create_internal (name.str (), type, type);
+
+      // LLVM implementation:
       llvm::BasicBlock *body = fn.new_block ();
       builder.SetInsertPoint (body);
       fn.do_return (builder, fn.argument (builder, 0));
+
+       // gccjit implementation:
+#ifdef HAVE_GCCJIT
+      gccjit::function gf = fn.gccjit_function;
+      gccjit::block b = gf.new_block ();
+      b.end_with_return (gf.get_param (0));
+#endif
+
       return identities[id] = fn;
     }
 
   return identities[id];
 }
 
+#ifdef HAVE_LLVM
 llvm::Value *
 jit_typeinfo::do_insert_error_check (llvm::IRBuilderD& abuilder)
 {
@@ -2004,11 +2831,31 @@
   val->setVolatile (true);
   return abuilder.CreateICmpSGT (val, abuilder.getInt32 (0));
 }
+#endif /* #ifdef HAVE_LLVM */
+
+#ifdef HAVE_GCCJIT
+gccjit::rvalue
+jit_typeinfo::do_insert_error_check (gccjit::function func)
+{
+  return error_state_gccjit.cast_to (gccjit_ctxt.get_type (GCC_JIT_TYPE_BOOL));
+}
+
+gccjit::rvalue
+jit_typeinfo::do_insert_interrupt_check (gccjit::function func)
+{
+  return octave_interrupt_state_gccjit > sig_atomic_type_gccjit.zero ();
+}
+#endif /* #ifdef HAVE_GCCJIT */
+
 
 void
 jit_typeinfo::add_builtin (const std::string& name)
 {
-  jit_type *btype = new_type (name, any, any->to_llvm (), true);
+  jit_type *btype = new_type (name, any, any->to_llvm (),
+#ifdef HAVE_GCCJIT
+                              any->to_gccjit (),
+#endif
+                              true);
   builtins[name] = btype;
 
   octave_builtin *ov_builtin = find_builtin (name);
@@ -2050,6 +2897,21 @@
 
   llvm::Value *ret = builder.CreateCall (ifun, fargs);
   fn.do_return (builder, ret);
+
+  // gcc implementation
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::function builtin = gccjit_ctxt.get_builtin_function (name);
+    std::vector<gccjit::rvalue> gccjit_args (nargs);
+    for (size_t i = 0; i < nargs; ++i)
+      gccjit_args[i] = gf.get_param (i + 1);
+    gccjit::rvalue ret = gccjit_ctxt.new_call (builtin, gccjit_args);
+    gccjit::block b = gf.new_block ();
+    b.end_with_return (ret);
+  }
+#endif
+
   paren_subsref_fn.add_overload (fn);
 }
 
@@ -2075,6 +2937,8 @@
   std::copy (args.begin (), args.end (), fn_args.begin () + 1);
   jit_function fn = create_internal (name, result, fn_args);
   fn.mark_can_error ();
+
+  // LLVM implementation:
   llvm::BasicBlock *block = fn.new_block ();
   builder.SetInsertPoint (block);
   llvm::Type *any_t = any->to_llvm ();
@@ -2105,6 +2969,55 @@
 
   jit_function cast_result = cast (result, any);
   fn.do_return (builder, cast_result.call (builder, ret));
+
+  // gccjit implementation:
+#ifdef HAVE_GCCJIT
+  {
+    gccjit::function gf = fn.gccjit_function;
+    gccjit::block b = gf.new_block ("body of register_generic");
+    b.add_comment ("TODO: register_generic");
+
+    gccjit::type array_t = gccjit_ctxt.new_array_type (any->to_gccjit (),
+                                                       args.size ());
+#if 1
+    gccjit::lvalue array = gf.new_local (array_t, "tmp_array");
+    for (size_t i = 0; i < args.size (); ++i)
+      {
+        gccjit::lvalue arg = gf.get_param (i + 1);
+        jit_function agrab = get_grab (args[i]);
+        if (agrab.valid ())
+          {
+            std::vector<gccjit::rvalue> grab_args (1, arg);
+            b.add_assignment (arg, agrab.call (gccjit_ctxt, b, grab_args));
+          }
+        jit_function acast = cast (any, args[i]);
+        std::vector<gccjit::rvalue> cast_args (1, arg);
+        b.add_assignment (array[i],
+                          acast.call (gccjit_ctxt, b, cast_args));
+      }
+#endif
+
+    gccjit::type int_t = intN (sizeof (octave_builtin::fcn) * 8)->to_gccjit ();
+    size_t fcn_int = reinterpret_cast<size_t> (builtin->function ());
+    gccjit::rvalue fcn = gccjit_ctxt.new_rvalue (int_t, (int)fcn_int);
+    gccjit::rvalue nargin = gccjit_ctxt.new_rvalue (int_t, (int)args.size ());
+    size_t result_int = reinterpret_cast<size_t> (result);
+    gccjit::rvalue res_gcc = gccjit_ctxt.new_rvalue (int_t, (int)result_int);
+    std::vector<gccjit::rvalue> call_args (4);
+    call_args[0] = fcn;
+    call_args[1] = nargin;
+    call_args[2] = array;
+    call_args[3] = res_gcc;
+    gccjit::rvalue ret = any_call.call (gccjit_ctxt, b, call_args);
+    
+    jit_function cast_result = cast (result, any);
+    std::vector<gccjit::rvalue> final_cast_args (1);
+    final_cast_args[0] = ret;
+    gccjit::rvalue final_result = cast_result.call (gccjit_ctxt, b, final_cast_args);
+    b.end_with_return (final_result);
+  }
+#endif
+
   paren_subsref_fn.add_overload (fn);
 }
 
@@ -2117,6 +3030,7 @@
   if (fn.can_error ())
     ret.mark_can_error ();
 
+  // LLVM implementation:
   llvm::BasicBlock *body = ret.new_block ();
   builder.SetInsertPoint (body);
   llvm::Value *result = fn.call (builder, ret.argument (builder, 1),
@@ -2126,9 +3040,27 @@
   else
     ret.do_return (builder);
 
+  // gccjit implementation:
+#ifdef HAVE_GCCJIT
+  gccjit::function gf = ret.gccjit_function;
+  gccjit::block b = gf.new_block ();
+  b.add_comment ("built by mirror_binary");
+  //std::vector<gccjit::rvalue> gccjit_args (2);
+  //gccjit_args[0] = gf.get_param (1);
+  //gccjit_args[1] = gf.get_param (0);
+  if (ret.result ())
+    b.end_with_return (
+      gccjit_ctxt.new_call (fn.gccjit_function,
+                            gf.get_param (1),
+                            gf.get_param (0)));
+  else
+    b.end_with_return ();
+#endif
+
   return ret;
 }
 
+#ifdef HAVE_LLVM
 llvm::Value *
 jit_typeinfo::pack_complex (llvm::IRBuilderD& bld, llvm::Value *cplx)
 {
@@ -2189,14 +3121,65 @@
   ret = complex_real (ret, real);
   return complex_imag (ret, imag);
 }
+#endif // #ifdef HAVE_LLVM
+
+#ifdef HAVE_GCCJIT
+gccjit::rvalue
+jit_typeinfo::complex_real (gccjit::rvalue cx)
+{
+  return cx[0];
+}
+
+gccjit::rvalue
+jit_typeinfo::complex_real (gccjit::block block,
+                            gccjit::lvalue cx,
+                            gccjit::rvalue real)
+{
+  block.add_assignment (cx[0], real);
+  return cx;
+}
+
+gccjit::rvalue
+jit_typeinfo::complex_imag (gccjit::rvalue cx)
+{
+  return cx[1];
+}
+
+gccjit::rvalue
+jit_typeinfo::complex_imag (gccjit::block block,
+                            gccjit::lvalue cx,
+                            gccjit::rvalue imag)
+{
+  block.add_assignment (cx[1], imag);
+  return cx;
+}
+
+gccjit::rvalue
+jit_typeinfo::complex_new (gccjit::block block,
+                           gccjit::rvalue real,
+                           gccjit::rvalue imag)
+{
+  gccjit::rvalue tmp =
+    block.get_function ().new_local (complex->to_gccjit (),
+                                     "complex_new");
+  block.add_assignment (tmp[0], real);
+  block.add_assignment (tmp[1], imag);
+  return tmp;
+}
+#endif // #ifdef HAVE_GCCJIT
 
 void
 jit_typeinfo::create_int (size_t nbits)
 {
   std::stringstream tname;
   tname << "int" << nbits;
-  ints[nbits] = new_type (tname.str (), any, llvm::Type::getIntNTy (context,
-                                                                    nbits));
+  ints[nbits] = new_type (tname.str (), any
+                          , llvm::Type::getIntNTy (context,
+                                                   nbits)
+#ifdef HAVE_GCCJIT
+                          , gccjit_ctxt.get_int_type (nbits / 8, 1)
+#endif
+                          );
 }
 
 jit_type *