This patch adds `gcc_jit_block_get_address` and 
`gcc_jit_block_end_with_indirect_jump`
under ABI version LIBGCCJIT_ABI_39. This exposes GNU C labels-as-values 
(`&&label`)
and indirect jumps (`goto *expr;`) in libgccjit, enabling JIT-compiled 
interpreter
dispatch loops and state machines to implement direct-threaded code tables.

Tested on x86_64-pc-linux-gnu. All 30 iterations of jit.dg/test-indirect-jump.c
passed across -O0 to -O3. check_GNU_style.py reports 0 errors.

gcc/jit/ChangeLog:

        * docs/topics/compatibility.rst (LIBGCCJIT_ABI_39): Document.
        * docs/topics/functions.rst (gcc_jit_block_get_address): Document.
        (gcc_jit_block_end_with_indirect_jump): Document.
        * jit-playback.cc (playback::block::get_address): New method.
        (playback::block::add_indirect_jump): New method.
        * jit-playback.h (playback::block::get_address): New method.
        (playback::block::add_indirect_jump): New method.
        * jit-recording.cc (recording::block::get_address): New method.
        (recording::block::end_with_indirect_jump): New method.
        (recording::get_block_address::replay_into): New method.
        (recording::get_block_address::make_debug_string): New method.
        (recording::get_block_address::write_reproducer): New method.
        (recording::indirect_jump::replay_into): New method.
        (recording::indirect_jump::get_successor_blocks): New method.
        (recording::indirect_jump::make_debug_string): New method.
        (recording::indirect_jump::write_reproducer): New method.
        * jit-recording.h (class get_block_address): New class.
        (class indirect_jump): New class.
        (recording::block::get_address): New method.
        (recording::block::end_with_indirect_jump): New method.
        * libgccjit.cc (gcc_jit_block_get_address): New entrypoint.
        (gcc_jit_block_end_with_indirect_jump): New entrypoint.
        * libgccjit.exports: Add gcc_jit_block_get_address and
        gcc_jit_block_end_with_indirect_jump.
        * libgccjit.h (LIBGCCJIT_HAVE_gcc_jit_block_get_address): New macro.
        (LIBGCCJIT_HAVE_gcc_jit_block_end_with_indirect_jump): New macro.
        (gcc_jit_block_get_address): New declaration.
        (gcc_jit_block_end_with_indirect_jump): New declaration.
        * libgccjit.map (LIBGCCJIT_ABI_39): Add gcc_jit_block_get_address and
        gcc_jit_block_end_with_indirect_jump.

gcc/testsuite/ChangeLog:

        * jit.dg/all-non-failing-tests.h: Include test-indirect-jump.c.
        * jit.dg/test-error-block-address-global-init.c: New test.
        * jit.dg/test-error-indirect-jump.c: New test.
        * jit.dg/test-indirect-jump.c: New test.

Signed-off-by: Vincent Li <[email protected]>
---
v4 -> v5

Removed duplicate initializer flag check in 
gcc_jit_global_set_initializer_rvalue 
and redundant visit_children call in verify_valid_within_stmt

 gcc/jit/docs/topics/compatibility.rst         |   8 +
 gcc/jit/docs/topics/functions.rst             |  40 +++
 gcc/jit/jit-playback.cc                       |  43 +++
 gcc/jit/jit-playback.h                        |   7 +
 gcc/jit/jit-recording.cc                      | 306 +++++++++++++++---
 gcc/jit/jit-recording.h                       |  62 +++-
 gcc/jit/libgccjit.cc                          |  71 ++++
 gcc/jit/libgccjit.exports                     |   4 +
 gcc/jit/libgccjit.h                           |  45 ++-
 gcc/jit/libgccjit.map                         |   6 +
 gcc/testsuite/jit.dg/all-non-failing-tests.h  |  10 +
 .../test-error-block-address-global-init.c    |  79 +++++
 .../jit.dg/test-error-indirect-jump.c         | 112 +++++++
 gcc/testsuite/jit.dg/test-indirect-jump.c     | 109 +++++++
 14 files changed, 859 insertions(+), 43 deletions(-)
 create mode 100644 gcc/testsuite/jit.dg/test-error-block-address-global-init.c
 create mode 100644 gcc/testsuite/jit.dg/test-error-indirect-jump.c
 create mode 100644 gcc/testsuite/jit.dg/test-indirect-jump.c

diff --git a/gcc/jit/docs/topics/compatibility.rst 
b/gcc/jit/docs/topics/compatibility.rst
index e4a231e79..ca1a428f3 100644
--- a/gcc/jit/docs/topics/compatibility.rst
+++ b/gcc/jit/docs/topics/compatibility.rst
@@ -489,3 +489,11 @@ information:
 ``LIBGCCJIT_ABI_38`` covers the addition of
 
  * :func:`gcc_jit_type_is_floating_point`
+
+   .. _LIBGCCJIT_ABI_39:
+
+``LIBGCCJIT_ABI_39``
+-----------------------
+``LIBGCCJIT_ABI_39`` covers the addition of API entrypoints
+ * :func:`gcc_jit_block_get_address` 
+ * :func:`gcc_jit_block_end_with_indirect_jump`
diff --git a/gcc/jit/docs/topics/functions.rst 
b/gcc/jit/docs/topics/functions.rst
index 66d4b6b2f..67818881d 100644
--- a/gcc/jit/docs/topics/functions.rst
+++ b/gcc/jit/docs/topics/functions.rst
@@ -466,6 +466,46 @@ Statements
 
       goto target;
 
+.. function:: gcc_jit_rvalue *\
+              gcc_jit_block_get_address (gcc_jit_block *block,\
+                                         gcc_jit_location *loc)
+
+   Get the address of a block as a ``void *`` rvalue, equivalent to GNU C's
+   ``&&label`` extension.
+
+   ``block`` must be non-NULL.
+
+   This API entrypoint was added in :ref:`LIBGCCJIT_ABI_39`; you can test
+   for its presence using
+
+   .. code-block:: c
+
+      #ifdef LIBGCCJIT_HAVE_gcc_jit_block_get_address
+
+.. function:: void\
+             gcc_jit_block_end_with_indirect_jump (\
+             gcc_jit_block *block,\
+             gcc_jit_location *loc,\
+             gcc_jit_rvalue *target_address,\
+             int num_destinations,\
+             gcc_jit_block **destinations)
+
+   Terminate a basic block by adding an indirect jump to ``target_address``,
+   analogous to GNU C's ``goto *target_address;``.
+
+   Here, ``target_address`` must be a pointer rvalue, and ``destinations``
+   must be an array of ``num_destinations`` candidate target blocks within
+   the same function.  Listing the candidate destination blocks is required
+   so that ``libgccjit`` can validate block reachability when checking the
+   function.
+
+   This API entrypoint was added in ``LIBGCCJIT_ABI_39``; you can test for its
+   presence using:
+
+   .. code-block:: c
+
+      #ifdef LIBGCCJIT_HAVE_gcc_jit_block_end_with_indirect_jump
+
 .. function:: void\
               gcc_jit_block_end_with_return (gcc_jit_block *block,\
                                              gcc_jit_location *loc,\
diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc
index 56c9413f8..c4c1ec8fd 100644
--- a/gcc/jit/jit-playback.cc
+++ b/gcc/jit/jit-playback.cc
@@ -2547,6 +2547,49 @@ c_finish_goto_label (location_t loc, tree label)
 
 }
 
+/* Take the address of a block as a void * rvalue (&&block).  */
+
+playback::rvalue *
+playback::block::
+get_address (location *loc)
+{
+  tree label = as_label_decl ();
+  TREE_USED (label) = 1;
+  /* Also set during gimplification by gimplify_addr_expr, but marked
+     explicitly here when constructing the ADDR_EXPR.  */
+  FORCED_LABEL (label) = 1;
+
+  tree addr = build1 (ADDR_EXPR, ptr_type_node, label);
+  if (loc)
+    set_tree_location (addr, loc);
+
+  return new rvalue (m_func->m_ctxt, addr);
+}
+
+/* Add an indirect jump statement (goto *target_address;).  */
+
+void
+playback::block::add_indirect_jump (location *loc,
+                                   rvalue *target_address,
+                                   auto_vec <block *> *destinations)
+{
+  unsigned i;
+  block *dest;
+  FOR_EACH_VEC_ELT (*destinations, i, dest)
+    {
+      tree label = dest->as_label_decl ();
+      TREE_USED (label) = 1;
+      FORCED_LABEL (label) = 1;
+    }
+
+  tree target = target_address->as_tree ();
+  target = fold_convert (ptr_type_node, target);
+  tree stmt = build1 (GOTO_EXPR, void_type_node, target);
+  if (loc)
+    set_tree_location (stmt, loc);
+  add_stmt (stmt);
+}
+
 /* Add a return statement to the function's statement list.  */
 
 void
diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h
index ad3e41f3d..d32ed4d49 100644
--- a/gcc/jit/jit-playback.h
+++ b/gcc/jit/jit-playback.h
@@ -692,6 +692,13 @@ public:
   add_jump (location *loc,
            block *target);
 
+  rvalue *get_address (location *loc);
+
+  void
+  add_indirect_jump (location *loc,
+                    rvalue *target_address,
+                    auto_vec <block *> *destinations);
+
   void
   add_return (location *loc,
              rvalue *rvalue);
diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc
index e28e5f67c..b6c88fd62 100644
--- a/gcc/jit/jit-recording.cc
+++ b/gcc/jit/jit-recording.cc
@@ -4103,25 +4103,36 @@ class rvalue_usage_validator : public 
recording::rvalue_visitor
  public:
   rvalue_usage_validator (const char *api_funcname,
                          recording::context *ctxt,
-                         recording::statement *stmt);
-
+                         recording::statement *stmt,
+                         recording::lvalue *global = NULL);
   void
   visit (recording::rvalue *rvalue) final override;
 
+  bool
+  has_errors () const
+  {
+    return m_has_errors;
+  }
+
  private:
   const char *m_api_funcname;
   recording::context *m_ctxt;
   recording::statement *m_stmt;
+  recording::lvalue *m_global;
+  bool m_has_errors;
 };
 
 /* The trivial constructor for rvalue_usage_validator.  */
 
 rvalue_usage_validator::rvalue_usage_validator (const char *api_funcname,
                                                recording::context *ctxt,
-                                               recording::statement *stmt)
+                                               recording::statement *stmt,
+                                               recording::lvalue *global)
   : m_api_funcname (api_funcname),
     m_ctxt (ctxt),
-    m_stmt (stmt)
+    m_stmt (stmt),
+    m_global (global),
+    m_has_errors (false)
 {
 }
 
@@ -4130,43 +4141,88 @@ rvalue_usage_validator::rvalue_usage_validator (const 
char *api_funcname,
 void
 rvalue_usage_validator::visit (recording::rvalue *rvalue)
 {
-  gcc_assert (m_stmt->get_block ());
-  recording::function *stmt_scope = m_stmt->get_block ()->get_function ();
-
-  /* Most rvalues don't have a scope (only locals and params).  */
-  if (rvalue->get_scope ())
+  if (!m_stmt)
     {
-      if (rvalue->get_scope () != stmt_scope)
-       m_ctxt->add_error
-         (rvalue->get_loc (),
-          "%s:"
-          " rvalue %s (type: %s)"
-          " has scope limited to function %s"
-          " but was used within function %s"
-          " (in statement: %s)",
-          m_api_funcname,
-          rvalue->get_debug_string (),
-          rvalue->get_type ()->get_debug_string (),
-          rvalue->get_scope ()->get_debug_string (),
-          stmt_scope->get_debug_string (),
-          m_stmt->get_debug_string ());
+      gcc_assert (m_global);
+      if (rvalue->get_scope ())
+       {
+         m_ctxt->add_error
+           (rvalue->get_loc (),
+            "%s:"
+            " rvalue %s (type: %s)"
+            " has scope limited to function %s"
+            " but was used in initializer for global %s",
+            m_api_funcname,
+            rvalue->get_debug_string (),
+            rvalue->get_type ()->get_debug_string (),
+            rvalue->get_scope ()->get_debug_string (),
+            m_global->get_debug_string ());
+         m_has_errors = true;
+       }
+      else if (rvalue->dyn_cast_param ())
+       {
+         m_ctxt->add_error
+           (rvalue->get_loc (),
+            "%s:"
+            " param %s (type: %s)"
+            " was used in initializer for global %s"
+            " but is not associated with any function",
+            m_api_funcname,
+            rvalue->get_debug_string (),
+            rvalue->get_type ()->get_debug_string (),
+            m_global->get_debug_string ());
+         m_has_errors = true;
+       }
     }
   else
     {
-      if (rvalue->dyn_cast_param ())
-       m_ctxt->add_error
-         (rvalue->get_loc (),
-          "%s:"
-          " param %s (type: %s)"
-          " was used within function %s"
-          " (in statement: %s)"
-          " but is not associated with any function",
-          m_api_funcname,
-          rvalue->get_debug_string (),
-          rvalue->get_type ()->get_debug_string (),
-          stmt_scope->get_debug_string (),
-          m_stmt->get_debug_string ());
+      gcc_assert (m_stmt->get_block ());
+      recording::function *stmt_scope = m_stmt->get_block ()->get_function ();
+
+      /* Most rvalues don't have a scope (only locals, params, and
+        block addresses).  */
+      if (rvalue->get_scope ())
+       {
+         if (rvalue->get_scope () != stmt_scope)
+           {
+             m_ctxt->add_error
+               (rvalue->get_loc (),
+                "%s:"
+                " rvalue %s (type: %s)"
+                " has scope limited to function %s"
+                " but was used within function %s"
+                " (in statement: %s)",
+                m_api_funcname,
+                rvalue->get_debug_string (),
+                rvalue->get_type ()->get_debug_string (),
+                rvalue->get_scope ()->get_debug_string (),
+                stmt_scope->get_debug_string (),
+                m_stmt->get_debug_string ());
+             m_has_errors = true;
+           }
+       }
+      else
+       {
+         if (rvalue->dyn_cast_param ())
+           {
+             m_ctxt->add_error
+               (rvalue->get_loc (),
+                "%s:"
+                " param %s (type: %s)"
+                " was used within function %s"
+                " (in statement: %s)"
+                " but is not associated with any function",
+                m_api_funcname,
+                rvalue->get_debug_string (),
+                rvalue->get_type ()->get_debug_string (),
+                stmt_scope->get_debug_string (),
+                m_stmt->get_debug_string ());
+             m_has_errors = true;
+           }
+       }
     }
+
+  rvalue->visit_children (this);
 }
 
 /* Verify that it's valid to use this rvalue (and all expressions
@@ -4185,10 +4241,22 @@ recording::rvalue::verify_valid_within_stmt (const char 
*api_funcname, statement
 
   /* Verify that it's OK to use this rvalue within s.  */
   v.visit (this);
+}
+
+/* Verify that it's valid to use this rvalue (and all expressions
+   in the tree below it) as the initializer for GLOBAL.  */
 
-  /* Traverse the expression tree below "this", verifying all rvalues
-     within it.  */
-  visit_children (&v);
+bool
+recording::rvalue::verify_valid_within_global_init (const char *api_funcname,
+                                                   lvalue *global)
+{
+  rvalue_usage_validator v (api_funcname,
+                           global->get_context (),
+                           NULL,
+                           global);
+
+  v.visit (this);
+  return !v.has_errors ();
 }
 
 /* Set the scope of this rvalue to be the given function.  This can only
@@ -5072,6 +5140,26 @@ recording::block::end_with_jump (recording::location 
*loc,
   return result;
 }
 
+/* Create a recording::end_with_indirect_jump instance and add
+   it to the block's context's list of mementos, and to the
+   block's list of statements.
+
+   Implements the heart of gcc_jit_block_end_with_indirect_jump.  */
+
+recording::statement *
+recording::block::end_with_indirect_jump (location *loc,
+                                         rvalue *target_address,
+                                         int num_destinations,
+                                         block **destinations)
+{
+  statement *result = new indirect_jump (this, loc, target_address,
+                                        num_destinations, destinations);
+  m_ctxt->record (result);
+  m_statements.safe_push (result);
+  m_has_been_terminated = true;
+  return result;
+}
+
 /* Create a recording::end_with_return instance and add it to
    the block's context's list of mementos, and to the block's
    list of statements.
@@ -5197,6 +5285,20 @@ recording::block::get_loc () const
     return NULL;
 }
 
+/* Create a recording::get_address instance and add it to
+   the block's context's list of mementos, and to the block's
+   list of statements.
+
+   Implements the heart of gcc_jit_block_get_address.  */
+
+recording::rvalue *
+recording::block::get_address (location *loc)
+{
+  recording::rvalue *result = new get_block_address (m_ctxt, loc, this);
+  m_ctxt->record (result);
+  return result;
+}
+
 /* Get the first statement within a block, if any.  */
 
 recording::statement *
@@ -6099,7 +6201,8 @@ void
 recording::ctor::visit_children (rvalue_visitor *v)
 {
   for (unsigned int i = 0; i < m_values.length (); i++)
-    v->visit (m_values[i]);
+    if (m_values[i])
+      v->visit (m_values[i]);
 }
 
 recording::string *
@@ -7332,6 +7435,45 @@ recording::get_address_of_lvalue::write_reproducer 
(reproducer &r)
           r.get_identifier (m_loc));
 }
 
+/* The implementation of class gcc::jit::recording::get_block_address.  */
+
+/* Implementation of pure virtual hook recording::memento::replay_into
+   for recording::get_block_address.  */
+
+void
+recording::get_block_address::replay_into (replayer *r)
+{
+  set_playback_obj (
+    m_block->playback_block ()->
+      get_address (playback_location (r, m_loc)));
+}
+
+/* Implementation of recording::memento::make_debug_string for
+   getting the address of a block.  */
+
+recording::string *
+recording::get_block_address::make_debug_string ()
+{
+  return string::from_printf (m_ctxt,
+                             "&&%s",
+                             m_block->get_debug_string ());
+}
+
+/* Implementation of recording::memento::write_reproducer for
+   get_block_address.  */
+
+void
+recording::get_block_address::write_reproducer (reproducer &r)
+{
+  const char *id = r.make_identifier (this, "address_of");
+  r.write ("  gcc_jit_rvalue *%s =\n"
+          "    gcc_jit_block_get_address (%s,\n"
+          "                               %s);\n",
+          id,
+          r.get_identifier (m_block),
+          r.get_identifier (m_loc));
+}
+
 /* The implementation of class gcc::jit::recording::function_pointer.  */
 
 /* Implementation of pure virtual hook recording::memento::replay_into
@@ -7765,6 +7907,90 @@ recording::jump::write_reproducer (reproducer &r)
           r.get_identifier (m_target));
 }
 
+/* The implementation of class gcc::jit::recording::indirect_jump.  */
+
+/* gcc::jit::recording::indirect_jump's constructor.  */
+
+recording::indirect_jump::indirect_jump (block *b,
+                                        location *loc,
+                                        rvalue *target_address,
+                                        int num_destinations,
+                                        block **destinations)
+: statement (b, loc),
+  m_target_address (target_address)
+{
+  m_destinations.reserve_exact (num_destinations);
+  for (int i = 0; i < num_destinations; i++)
+    m_destinations.quick_push (destinations[i]);
+}
+
+/* Implementation of pure virtual hook recording::memento::replay_into
+   for recording::indirect_jump.  */
+
+void
+recording::indirect_jump::replay_into (replayer *r)
+{
+  auto_vec <playback::block *> pb_dests;
+  pb_dests.reserve_exact (m_destinations.length ());
+  for (unsigned i = 0; i < m_destinations.length (); i++)
+    pb_dests.quick_push (m_destinations[i]->playback_block ());
+
+  playback_block (get_block ())
+    ->add_indirect_jump (playback_location (r),
+                        m_target_address->playback_rvalue (),
+                        &pb_dests);
+}
+
+/* Override the poisoned default implementation of
+   gcc::jit::recording::statement::get_successor_blocks.  */
+
+vec <recording::block *>
+recording::indirect_jump::get_successor_blocks () const
+{
+  vec <block *> result;
+  result.create (m_destinations.length ());
+  for (unsigned i = 0; i < m_destinations.length (); i++)
+    result.quick_push (m_destinations[i]);
+  return result;
+}
+
+/* Implementation of recording::memento::make_debug_string for
+   an indirect jump statement.  */
+
+recording::string *
+recording::indirect_jump::make_debug_string ()
+{
+  return string::from_printf (m_ctxt,
+                             "goto *%s;",
+                             m_target_address->get_debug_string_parens 
(PRECEDENCE_UNARY));
+}
+
+/* Implementation of recording::memento::write_reproducer for
+   indirect_jump statements.  */
+
+void
+recording::indirect_jump::write_reproducer (reproducer &r)
+{
+  r.make_identifier (this, "indirect_jump");
+  const char *dests_id = r.make_tmp_identifier ("destinations_for", this);
+  r.write ("  gcc_jit_block *%s[%i] = {\n",
+          dests_id,
+          m_destinations.length ());
+  for (unsigned i = 0; i < m_destinations.length (); i++)
+    r.write ("    %s,\n", r.get_identifier (m_destinations[i]));
+  r.write ("  };\n");
+  r.write ("  gcc_jit_block_end_with_indirect_jump (%s,\n"
+          "                                        %s,\n"
+          "                                        %s,\n"
+          "                                        %i,\n"
+          "                                        %s);\n",
+          r.get_identifier (get_block ()),
+          r.get_identifier (get_loc ()),
+          r.get_identifier_as_rvalue (m_target_address),
+          m_destinations.length (),
+          dests_id);
+}
+
 /* The implementation of class gcc::jit::recording::return_.  */
 
 /* Implementation of pure virtual hook recording::memento::replay_into
diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index 2a5f23a05..cbcbdf216 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -966,7 +966,7 @@ public:
   type *unqualified () final override { return m_other_type; }
 
   void replay_into (replayer *) final override;
-  memento_of_get_aligned *dyn_cast_aligned_type () final override 
+  memento_of_get_aligned *dyn_cast_aligned_type () final override
   {
     return this;
   }
@@ -1413,6 +1413,9 @@ public:
   void
   verify_valid_within_stmt (const char *api_funcname, statement *s);
 
+  bool verify_valid_within_global_init (const char *api_funcname,
+                                       lvalue *global);
+
   virtual void visit_children (rvalue_visitor *v) = 0;
 
   void set_scope (function *scope);
@@ -1682,6 +1685,13 @@ public:
   end_with_jump (location *loc,
                 block *target);
 
+  rvalue * get_address (location *loc);
+
+  statement * end_with_indirect_jump (location *loc,
+                                    rvalue *target_address,
+                                    int num_destinations,
+                                    block **destinations);
+
   statement *
   end_with_return (location *loc,
                   rvalue *rvalue);
@@ -2427,6 +2437,34 @@ private:
   lvalue *m_lvalue;
 };
 
+class get_block_address : public rvalue
+{
+public:
+  get_block_address (context *ctxt,
+                location *loc,
+                block *b)
+  : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_VOID_PTR)),
+    m_block (b)
+  {
+    set_scope (b->get_function ());
+  }
+
+  void replay_into (replayer *r) final override;
+
+  void visit_children (rvalue_visitor *) final override {}
+
+private:
+  string * make_debug_string () final override;
+  void write_reproducer (reproducer &r) final override;
+  enum precedence get_precedence () const final override
+  {
+    return PRECEDENCE_UNARY;
+  }
+
+private:
+  block *m_block;
+};
+
 class function_pointer : public rvalue
 {
 public:
@@ -2650,6 +2688,28 @@ private:
   block *m_target;
 };
 
+class indirect_jump : public statement
+{
+public:
+  indirect_jump (block *b,
+                location *loc,
+                rvalue *target_address,
+                int num_destinations,
+                block **destinations);
+
+  void replay_into (replayer *r) final override;
+
+  vec <block *> get_successor_blocks () const final override;
+
+private:
+  string * make_debug_string () final override;
+  void write_reproducer (reproducer &r) final override;
+
+private:
+  rvalue *m_target_address;
+  auto_vec <block *> m_destinations;
+};
+
 class return_ : public statement
 {
 public:
diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc
index 5a874fc48..b38f8961e 100644
--- a/gcc/jit/libgccjit.cc
+++ b/gcc/jit/libgccjit.cc
@@ -1871,6 +1871,9 @@ gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue 
*global,
                               "global variable already initialized: %s",
                               global->get_debug_string ());
 
+  if (!init_rvalue->verify_valid_within_global_init (__func__, global))
+    return NULL;
+
   /* The global need to know during playback that it will be
      initialized.  */
   gbl->set_flags (gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT);
@@ -2874,6 +2877,23 @@ gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
   return (gcc_jit_rvalue *)lvalue->get_address (loc);
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::block::get_address method in
+   jit-recording.cc.  */
+
+gcc_jit_rvalue *
+gcc_jit_block_get_address (gcc_jit_block *block,
+                          gcc_jit_location *loc)
+{
+  RETURN_NULL_IF_FAIL (block, NULL, loc, "NULL block");
+  JIT_LOG_FUNC (block->get_context ()->get_logger ());
+  /* LOC can be NULL.  */
+
+  return (gcc_jit_rvalue *)block->get_address (loc);
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
@@ -3258,6 +3278,57 @@ gcc_jit_block_end_with_jump (gcc_jit_block *block,
   block->end_with_jump (loc, target);
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::block::end_with_indirect_jump method in
+   jit-recording.cc.  */
+
+void
+gcc_jit_block_end_with_indirect_jump (gcc_jit_block *block,
+                                     gcc_jit_location *loc,
+                                     gcc_jit_rvalue *target_address,
+                                     int num_destinations,
+                                     gcc_jit_block **destinations)
+{
+  RETURN_IF_NOT_VALID_BLOCK (block, loc);
+  gcc::jit::recording::context *ctxt = block->get_context ();
+  JIT_LOG_FUNC (ctxt->get_logger ());
+  /* LOC can be NULL.  */
+  RETURN_IF_FAIL (target_address, ctxt, loc, "NULL target_address");
+  RETURN_IF_FAIL_PRINTF1 (
+    target_address->get_type ()->is_pointer (),
+    ctxt, loc,
+    "target_address: %s is not of a pointer type",
+    target_address->get_debug_string ());
+  RETURN_IF_FAIL (num_destinations >= 0, ctxt, loc, "num_destinations < 0");
+  if (num_destinations > 0)
+    RETURN_IF_FAIL (destinations, ctxt, loc, "NULL destinations");
+
+  for (int i = 0; i < num_destinations; i++)
+    {
+      RETURN_IF_FAIL_PRINTF1 (destinations[i], ctxt, loc,
+                             "NULL destination block at index %d", i);
+      RETURN_IF_FAIL_PRINTF4 (
+       block->get_function () == destinations[i]->get_function (),
+       ctxt, loc,
+       "destination block %d (%s) is not in the same function"
+       " (function %s) as source block %s",
+       i,
+       destinations[i]->get_debug_string (),
+       destinations[i]->get_function ()->get_debug_string (),
+       block->get_debug_string ());
+    }
+
+  gcc::jit::recording::statement *stmt =
+    block->end_with_indirect_jump (loc, target_address,
+                                  num_destinations,
+                                  (gcc::jit::recording::block **)destinations);
+
+  /* target_address must be valid to use within STMT.  */
+  target_address->verify_valid_within_stmt (__func__, stmt);
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
diff --git a/gcc/jit/libgccjit.exports b/gcc/jit/libgccjit.exports
index fae095192..751918c45 100644
--- a/gcc/jit/libgccjit.exports
+++ b/gcc/jit/libgccjit.exports
@@ -267,3 +267,7 @@ _gcc_jit_context_new_array_type_u64
 
 # LIBGCCJIT_ABI_38
 _gcc_jit_type_is_floating_point
+
+# LIBGCCJIT_ABI_39
+_gcc_jit_block_get_address
+_gcc_jit_block_end_with_indirect_jump
diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h
index 2247c99de..272085986 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -1455,16 +1455,31 @@ extern gcc_jit_rvalue *
 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
                            gcc_jit_location *loc);
 
+#define LIBGCCJIT_HAVE_gcc_jit_block_get_address
+
+/* Taking the address of a block; analogous to:
+     &&block
+   in C.
+
+   This API entrypoint was added in LIBGCCJIT_ABI_39; you can test for its
+   presence using
+      #ifdef LIBGCCJIT_HAVE_gcc_jit_block_get_address
+*/
+extern gcc_jit_rvalue *
+gcc_jit_block_get_address (gcc_jit_block *block,
+                          gcc_jit_location *loc);
+
 #define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model
 
 /* Set the thread-local storage model of a global variable
 
    This API entrypoint was added in LIBGCCJIT_ABI_17; you can test for its
    presence using
-     #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model  */
+     #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model
+*/
 extern void
 gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,
-                           enum gcc_jit_tls_model model);
+                             enum gcc_jit_tls_model model);
 
 #define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
 
@@ -1592,6 +1607,32 @@ gcc_jit_block_end_with_jump (gcc_jit_block *block,
                             gcc_jit_location *loc,
                             gcc_jit_block *target);
 
+#define LIBGCCJIT_HAVE_gcc_jit_block_end_with_indirect_jump
+
+/* Terminate a block by adding an indirect jump to target_address.
+
+   All possible indirect jump targets must be constrained to a fixed array.
+
+   The candidate destination blocks must be listed in DESTINATIONS
+
+   (of length NUM_DESTINATIONS) so that libgccjit can validate block
+
+   reachability within the function.
+
+   This is roughly equivalent to this C code:
+
+      goto *target_address;
+
+   This API entrypoint was added in LIBGCCJIT_ABI_39; you can test for its
+   presence using
+      #ifdef LIBGCCJIT_HAVE_gcc_jit_block_end_with_indirect_jump.  */
+extern void
+gcc_jit_block_end_with_indirect_jump (gcc_jit_block *block,
+                                     gcc_jit_location *loc,
+                                     gcc_jit_rvalue *target_address,
+                                     int num_destinations,
+                                     gcc_jit_block **destinations);
+
 /* Terminate a block by adding evaluation of an rvalue, returning the value.
 
    This is roughly equivalent to this C code:
diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map
index 78b6be9ff..9412700a2 100644
--- a/gcc/jit/libgccjit.map
+++ b/gcc/jit/libgccjit.map
@@ -349,3 +349,9 @@ LIBGCCJIT_ABI_38 {
   global:
     gcc_jit_type_is_floating_point;
 } LIBGCCJIT_ABI_37;
+
+LIBGCCJIT_ABI_39 {
+  global:
+    gcc_jit_block_get_address;
+    gcc_jit_block_end_with_indirect_jump;
+} LIBGCCJIT_ABI_38;
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h 
b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index e8f8463e8..f8202635e 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -269,6 +269,13 @@
 #undef create_code
 #undef verify_code
 
+/* test-indirect-jump.c */
+#define create_code create_code_indirect_jump
+#define verify_code verify_code_indirect_jump
+#include "test-indirect-jump.c"
+#undef create_code
+#undef verify_code
+
 /* test-link-section-assembler.c: This can't be in the testcases array as it
    doesn't have a verify_code implementation.  */
 
@@ -688,6 +695,9 @@ const struct testcase testcases[] = {
   {"vector_perm",
    create_code_vector_perm,
    verify_code_vector_perm},
+  {"indirect_jump",
+   create_code_indirect_jump,
+   verify_code_indirect_jump},
 };
 
 const int num_testcases = (sizeof (testcases) / sizeof (testcases[0]));
diff --git a/gcc/testsuite/jit.dg/test-error-block-address-global-init.c 
b/gcc/testsuite/jit.dg/test-error-block-address-global-init.c
new file mode 100644
index 000000000..1df8988ff
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-error-block-address-global-init.c
@@ -0,0 +1,79 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libgccjit.h"
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  gcc_jit_type *void_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
+  gcc_jit_type *void_ptr_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
+
+  gcc_jit_function *fn =
+    gcc_jit_context_new_function (ctxt, NULL,
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 void_type,
+                                 "my_fn",
+                                 0, NULL,
+                                 0);
+  gcc_jit_block *block =
+    gcc_jit_function_new_block (fn, "target_block");
+
+  gcc_jit_rvalue *block_addr =
+    gcc_jit_block_get_address (block, NULL);
+
+  /* 1. Direct global initializer (should fail validation).  */
+  gcc_jit_lvalue *global_ptr =
+    gcc_jit_context_new_global (ctxt, NULL,
+                               GCC_JIT_GLOBAL_EXPORTED,
+                               void_ptr_type,
+                               "global_ptr");
+  CHECK_VALUE (
+    gcc_jit_global_set_initializer_rvalue (global_ptr, block_addr),
+    NULL);
+
+  /* 2. Nested inside an array constructor (should fail validation).  */
+  gcc_jit_type *array_type =
+    gcc_jit_context_new_array_type (ctxt, NULL, void_ptr_type, 1);
+  gcc_jit_lvalue *global_table =
+    gcc_jit_context_new_global (ctxt, NULL,
+                               GCC_JIT_GLOBAL_EXPORTED,
+                               array_type,
+                               "global_table");
+  gcc_jit_rvalue *ctor =
+    gcc_jit_context_new_array_constructor (ctxt, NULL,
+                                          array_type,
+                                          1, &block_addr);
+  CHECK_VALUE (
+    gcc_jit_global_set_initializer_rvalue (global_table, ctor),
+    NULL);
+
+  /* Consume ctor (and transitively block_addr) within my_fn so the
+     generated reproducer reads the variables and avoids
+     -Werror=unused-but-set-variable.  */
+  gcc_jit_block_add_eval (block, NULL, ctor);
+  gcc_jit_block_end_with_void_return (block, NULL);
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_VALUE (result, NULL);
+
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_first_error (ctxt),
+    "gcc_jit_global_set_initializer_rvalue:"
+    " rvalue &&target_block (type: void *)"
+    " has scope limited to function my_fn"
+    " but was used in initializer for global global_ptr");
+
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_global_set_initializer_rvalue:"
+    " rvalue &&target_block (type: void *)"
+    " has scope limited to function my_fn"
+    " but was used in initializer for global global_table");
+}
diff --git a/gcc/testsuite/jit.dg/test-error-indirect-jump.c 
b/gcc/testsuite/jit.dg/test-error-indirect-jump.c
new file mode 100644
index 000000000..8f40a3738
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-error-indirect-jump.c
@@ -0,0 +1,112 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libgccjit.h"
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Let's try to trigger every validation error on
+     gcc_jit_block_get_address and gcc_jit_block_end_with_indirect_jump.  */
+  gcc_jit_type *void_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+
+  gcc_jit_function *fn1 =
+    gcc_jit_context_new_function (ctxt, NULL,
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 void_type,
+                                 "fn1",
+                                 0, NULL,
+                                 0);
+  gcc_jit_block *b_fn1 =
+    gcc_jit_function_new_block (fn1, "b_fn1");
+
+  gcc_jit_function *fn2 =
+    gcc_jit_context_new_function (ctxt, NULL,
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 void_type,
+                                 "fn2",
+                                 0, NULL,
+                                 0);
+  gcc_jit_block *b_fn2 =
+    gcc_jit_function_new_block (fn2, "b_fn2");
+
+  /* 1. Non-pointer target_address.  */
+  gcc_jit_rvalue * non_ptr =
+    gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42);
+  gcc_jit_block_end_with_indirect_jump (b_fn1, NULL, non_ptr, 0, NULL);
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump:"
+    " target_address: (int)42 is not of a pointer type");
+
+  /* 2. Negative num_destinations.  */
+  gcc_jit_rvalue *addr_fn1 = gcc_jit_block_get_address (b_fn1, NULL);
+  gcc_jit_block_end_with_indirect_jump (b_fn1, NULL, addr_fn1, -1, NULL);
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump: num_destinations < 0");
+
+  /* 3. NULL destinations array when num_destinations > 0.  */
+  gcc_jit_block_end_with_indirect_jump (b_fn1, NULL, addr_fn1, 1, NULL);
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump: NULL destinations");
+
+  /* 4. NULL element in destinations array.  */
+  gcc_jit_block *null_dests[1] = { NULL };
+  gcc_jit_block_end_with_indirect_jump (b_fn1, NULL, addr_fn1, 1, null_dests);
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump:"
+    " NULL destination block at index 0");
+
+  /* 5. Destination block in wrong function.  */
+  gcc_jit_block *wrong_fn_dests[1] = { b_fn2 };
+  gcc_jit_block_end_with_indirect_jump (b_fn1, NULL, addr_fn1, 1,
+                                       wrong_fn_dests);
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump:"
+    " destination block 0 (b_fn2) is not in the same function"
+    " (function fn2) as source block b_fn1");
+
+  /* Terminate b_fn1 cleanly so function validation doesn't overwrite
+     last_error with an "unterminated block" error during compile.  */
+  gcc_jit_block_end_with_void_return (b_fn1, NULL);
+
+  /* 6. Cross-function block address (addr_fn1 used inside fn2).  */
+  gcc_jit_block *fn2_dests[1] = { b_fn2 };
+  gcc_jit_block_end_with_indirect_jump (b_fn2, NULL, addr_fn1, 1,
+                                       fn2_dests);
+
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump:"
+    " rvalue &&b_fn1 (type: void *) has scope limited to function fn1"
+    " but was used within function fn2"
+    " (in statement: goto *&&b_fn1;)");
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_VALUE (result, NULL);
+
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_first_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump:"
+    " target_address: (int)42 is not of a pointer type");
+
+  /* Verify that using a block address from fn1 inside fn2 was caught
+     by verify_valid_within_stmt.  */
+  CHECK_STRING_VALUE (
+    gcc_jit_context_get_last_error (ctxt),
+    "gcc_jit_block_end_with_indirect_jump:"
+    " rvalue &&b_fn1 (type: void *) has scope limited to function fn1"
+    " but was used within function fn2"
+    " (in statement: goto *&&b_fn1;)");
+}
diff --git a/gcc/testsuite/jit.dg/test-indirect-jump.c 
b/gcc/testsuite/jit.dg/test-indirect-jump.c
new file mode 100644
index 000000000..f464a6edb
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-indirect-jump.c
@@ -0,0 +1,109 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libgccjit.h"
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Let's try to inject the equivalent of:
+     int
+     test_indirect_jump (int idx)
+     {
+       void *targets[3];
+       targets[0] = &&b_zero;
+       targets[1] = &&b_one;
+       targets[2] = &&b_two;
+       goto *targets[idx];
+
+     b_zero:
+       return 10;
+     b_one:
+       return 20;
+     b_two:
+       return 30;
+     }
+  */
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+  gcc_jit_type *void_ptr_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
+  gcc_jit_type *array_type =
+    gcc_jit_context_new_array_type (ctxt, NULL, void_ptr_type, 3);
+
+  gcc_jit_param *param_idx =
+    gcc_jit_context_new_param (ctxt, NULL, int_type, "idx");
+  gcc_jit_function *func =
+    gcc_jit_context_new_function (ctxt, NULL,
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 int_type,
+                                 "test_indirect_jump",
+                                 1, &param_idx,
+                                 0);
+
+  gcc_jit_block *b_entry = gcc_jit_function_new_block (func, "entry");
+  gcc_jit_block *b_zero = gcc_jit_function_new_block (func, "b_zero");
+  gcc_jit_block *b_one = gcc_jit_function_new_block (func, "b_one");
+  gcc_jit_block *b_two = gcc_jit_function_new_block (func, "b_two");
+
+  gcc_jit_lvalue *targets =
+    gcc_jit_function_new_local (func, NULL, array_type, "targets");
+
+  gcc_jit_block *blocks[3] = { b_zero, b_one, b_two };
+  for (int i = 0; i < 3; i++)
+    {
+      gcc_jit_rvalue *idx_val =
+       gcc_jit_context_new_rvalue_from_int (ctxt, int_type, i);
+      gcc_jit_lvalue *elem =
+       gcc_jit_context_new_array_access (ctxt, NULL,
+                                         gcc_jit_lvalue_as_rvalue (targets),
+                                         idx_val);
+      gcc_jit_rvalue *blk_addr =
+       gcc_jit_block_get_address (blocks[i], NULL);
+      gcc_jit_block_add_assignment (b_entry, NULL, elem, blk_addr);
+    }
+
+  /* void *target_addr = targets[idx]; */
+  gcc_jit_lvalue *selected_elem =
+    gcc_jit_context_new_array_access (ctxt, NULL,
+                                     gcc_jit_lvalue_as_rvalue (targets),
+                                     gcc_jit_param_as_rvalue (param_idx));
+
+  /* goto *targets[idx]; */
+  gcc_jit_block_end_with_indirect_jump (
+    b_entry, NULL,
+    gcc_jit_lvalue_as_rvalue (selected_elem),
+    3, blocks);
+
+  /* b_zero: return 10; */
+  gcc_jit_block_end_with_return (
+    b_zero, NULL,
+    gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10));
+
+  /* b_one: return 20; */
+  gcc_jit_block_end_with_return (
+    b_one, NULL,
+    gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 20));
+
+  /* b_two: return 30; */
+  gcc_jit_block_end_with_return (
+    b_two, NULL,
+    gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 30));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+
+{
+  typedef int (*fn_type) (int);
+  CHECK_NON_NULL (result);
+
+  fn_type test_indirect_jump =
+    (fn_type)gcc_jit_result_get_code (result, "test_indirect_jump");
+  CHECK_NON_NULL (test_indirect_jump);
+
+  CHECK_VALUE (test_indirect_jump (0), 10);
+  CHECK_VALUE (test_indirect_jump (1), 20);
+  CHECK_VALUE (test_indirect_jump (2), 30);
+}
-- 
2.56.0

Reply via email to