LCOV - code coverage report
Current view: top level - gcc/jit - libgccjit.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.6 % 1338 1292
Test Date: 2026-12-05 16:10:02 Functions: 95.0 % 181 172
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Implementation of the C API; all wrappers into the internal C++ API
       2              :    Copyright (C) 2013-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it
       8              : under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but
      13              : WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              : General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #define INCLUDE_MUTEX
      23              : #define INCLUDE_STRING
      24              : #include "system.h"
      25              : #include "coretypes.h"
      26              : #include "timevar.h"
      27              : #include "typed-splay-tree.h"
      28              : #include "cppbuiltin.h"
      29              : 
      30              : #include "libgccjit.h"
      31              : #include "jit-recording.h"
      32              : #include "jit-result.h"
      33              : #include "jit-target.h"
      34              : 
      35              : /* The opaque types used by the public API are actually subclasses
      36              :    of the gcc::jit::recording classes.  */
      37              : 
      38         3742 : struct gcc_jit_context : public gcc::jit::recording::context
      39              : {
      40         1918 :   gcc_jit_context (gcc_jit_context *parent_ctxt) :
      41         1918 :     context (parent_ctxt)
      42              :   {}
      43              : };
      44              : 
      45              : struct gcc_jit_result : public gcc::jit::result
      46              : {
      47              : };
      48              : 
      49            4 : struct gcc_jit_target_info : public target_info
      50              : {
      51              : };
      52              : 
      53              : struct gcc_jit_object : public gcc::jit::recording::memento
      54              : {
      55              : };
      56              : 
      57              : struct gcc_jit_location : public gcc::jit::recording::location
      58              : {
      59              : };
      60              : 
      61              : struct gcc_jit_type : public gcc::jit::recording::type
      62              : {
      63              : };
      64              : 
      65              : struct gcc_jit_struct : public gcc::jit::recording::struct_
      66              : {
      67              : };
      68              : 
      69              : struct gcc_jit_function_type : public gcc::jit::recording::function_type
      70              : {
      71              : };
      72              : 
      73              : struct gcc_jit_vector_type : public gcc::jit::recording::vector_type
      74              : {
      75              : };
      76              : 
      77              : struct gcc_jit_field : public gcc::jit::recording::field
      78              : {
      79              : };
      80              : 
      81              : struct gcc_jit_bitfield : public gcc::jit::recording::bitfield
      82              : {
      83              : };
      84              : 
      85              : struct gcc_jit_function : public gcc::jit::recording::function
      86              : {
      87              : };
      88              : 
      89              : struct gcc_jit_block : public gcc::jit::recording::block
      90              : {
      91              : };
      92              : 
      93              : struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
      94              : {
      95              : };
      96              : 
      97              : struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
      98              : {
      99              : };
     100              : 
     101              : struct gcc_jit_param : public gcc::jit::recording::param
     102              : {
     103              : };
     104              : 
     105              : struct gcc_jit_case : public gcc::jit::recording::case_
     106              : {
     107              : };
     108              : 
     109           42 : struct gcc_jit_timer : public timer
     110              : {
     111              : };
     112              : 
     113              : struct gcc_jit_extended_asm : public gcc::jit::recording::extended_asm
     114              : {
     115              : };
     116              : 
     117              : 
     118              : /**********************************************************************
     119              :  Error-handling.
     120              : 
     121              :  We try to gracefully handle API usage errors by being defensive
     122              :  at the API boundary.
     123              :  **********************************************************************/
     124              : 
     125              : #define JIT_BEGIN_STMT do {
     126              : #define JIT_END_STMT   } while(0)
     127              : 
     128              : /* Each of these error-handling macros determines if TEST_EXPR holds.
     129              : 
     130              :    If TEXT_EXPR fails to hold we return from the enclosing function and
     131              :    print an error, either via adding an error on the given context CTXT
     132              :    if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
     133              :    is NULL.
     134              : 
     135              :    They have to be macros since they inject their "return" into the
     136              :    function they are placed in.
     137              : 
     138              :    The variant macros express:
     139              : 
     140              :      (A) whether or not we need to return a value:
     141              :             RETURN_VAL_IF_FAIL* vs
     142              :             RETURN_IF_FAIL*,
     143              :          with the former returning RETURN_EXPR, and
     144              :             RETURN_NULL_IF_FAIL*
     145              :          for the common case where a NULL value is to be returned on
     146              :          error, and
     147              : 
     148              :      (B) whether the error message is to be directly printed:
     149              :            RETURN_*IF_FAIL
     150              :          or is a format string with some number of arguments:
     151              :            RETURN_*IF_FAIL_PRINTF*
     152              : 
     153              :    They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
     154              :    trailing semicolons.
     155              : */
     156              : 
     157              : #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG)  \
     158              :   JIT_BEGIN_STMT                                                        \
     159              :     if (!(TEST_EXPR))                                                   \
     160              :       {                                                         \
     161              :         jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG));     \
     162              :         return (RETURN_EXPR);                                           \
     163              :       }                                                         \
     164              :   JIT_END_STMT
     165              : 
     166              : #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
     167              :   JIT_BEGIN_STMT                                                        \
     168              :     if (!(TEST_EXPR))                                                   \
     169              :       {                                                         \
     170              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                     \
     171              :                    __func__, (A0));                             \
     172              :         return (RETURN_EXPR);                                           \
     173              :       }                                                         \
     174              :   JIT_END_STMT
     175              : 
     176              : #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
     177              :   JIT_BEGIN_STMT                                                        \
     178              :     if (!(TEST_EXPR))                                                   \
     179              :       {                                                         \
     180              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     181              :                    __func__, (A0), (A1));                               \
     182              :         return (RETURN_EXPR);                                           \
     183              :       }                                                         \
     184              :   JIT_END_STMT
     185              : 
     186              : #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
     187              :   JIT_BEGIN_STMT                                                        \
     188              :     if (!(TEST_EXPR))                                                   \
     189              :       {                                                         \
     190              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     191              :                    __func__, (A0), (A1), (A2));                 \
     192              :         return (RETURN_EXPR);                                           \
     193              :       }                                                         \
     194              :   JIT_END_STMT
     195              : 
     196              : #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
     197              :   JIT_BEGIN_STMT                                                        \
     198              :     if (!(TEST_EXPR))                                                   \
     199              :       {                                                         \
     200              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     201              :                    __func__, (A0), (A1), (A2), (A3));                   \
     202              :         return (RETURN_EXPR);                                           \
     203              :       }                                                         \
     204              :   JIT_END_STMT
     205              : 
     206              : #define RETURN_VAL_IF_FAIL_PRINTF5(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
     207              :   JIT_BEGIN_STMT                                                        \
     208              :     if (!(TEST_EXPR))                                                   \
     209              :       {                                                         \
     210              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     211              :                    __func__, (A0), (A1), (A2), (A3), (A4));     \
     212              :         return (RETURN_EXPR);                                           \
     213              :       }                                                         \
     214              :   JIT_END_STMT
     215              : 
     216              : #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
     217              :   JIT_BEGIN_STMT                                                        \
     218              :     if (!(TEST_EXPR))                                                   \
     219              :       {                                                         \
     220              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     221              :                    __func__, (A0), (A1), (A2), (A3), (A4), (A5));       \
     222              :         return (RETURN_EXPR);                                           \
     223              :       }                                                         \
     224              :   JIT_END_STMT
     225              : 
     226              : #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
     227              :   RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
     228              : 
     229              : #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
     230              :   RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
     231              : 
     232              : #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
     233              :   RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
     234              : 
     235              : #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
     236              :   RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
     237              : 
     238              : #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
     239              :   RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
     240              : 
     241              : #define RETURN_NULL_IF_FAIL_PRINTF5(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
     242              :   RETURN_VAL_IF_FAIL_PRINTF5 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4)
     243              : 
     244              : #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
     245              :   RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
     246              : 
     247              : #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG)                   \
     248              :   JIT_BEGIN_STMT                                                        \
     249              :     if (!(TEST_EXPR))                                                   \
     250              :       {                                                         \
     251              :         jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG));             \
     252              :         return;                                                 \
     253              :       }                                                         \
     254              :   JIT_END_STMT
     255              : 
     256              : #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
     257              :   JIT_BEGIN_STMT                                                        \
     258              :     if (!(TEST_EXPR))                                                   \
     259              :       {                                                         \
     260              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     261              :                    __func__, (A0));                                     \
     262              :         return;                                                 \
     263              :       }                                                         \
     264              :   JIT_END_STMT
     265              : 
     266              : #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
     267              :   JIT_BEGIN_STMT                                                        \
     268              :     if (!(TEST_EXPR))                                                   \
     269              :       {                                                         \
     270              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     271              :                    __func__, (A0), (A1));                               \
     272              :         return;                                                 \
     273              :       }                                                         \
     274              :   JIT_END_STMT
     275              : 
     276              : #define RETURN_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
     277              :   JIT_BEGIN_STMT                                                        \
     278              :     if (!(TEST_EXPR))                                                   \
     279              :       {                                                         \
     280              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                     \
     281              :                    __func__, (A0), (A1), (A2));                 \
     282              :         return;                                                 \
     283              :       }                                                         \
     284              :   JIT_END_STMT
     285              : 
     286              : #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
     287              :   JIT_BEGIN_STMT                                                        \
     288              :     if (!(TEST_EXPR))                                                   \
     289              :       {                                                         \
     290              :         jit_error ((CTXT), (LOC), "%s: " ERR_FMT,                             \
     291              :                    __func__, (A0), (A1), (A2), (A3));                   \
     292              :         return;                                                 \
     293              :       }                                                         \
     294              :   JIT_END_STMT
     295              : 
     296              : /* Check that BLOCK is non-NULL, and that it's OK to add statements to
     297              :    it.  This will fail if BLOCK has already been terminated by some
     298              :    kind of jump or a return.  */
     299              : #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC)                           \
     300              :   JIT_BEGIN_STMT                                                        \
     301              :     RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block");              \
     302              :     RETURN_IF_FAIL_PRINTF2 (                                            \
     303              :       !(BLOCK)->has_been_terminated (),                              \
     304              :       (BLOCK)->get_context (),                                               \
     305              :       (LOC),                                                            \
     306              :       "adding to terminated block: %s (already terminated by: %s)",   \
     307              :       (BLOCK)->get_debug_string (),                                  \
     308              :       (BLOCK)->get_last_statement ()->get_debug_string ());               \
     309              :   JIT_END_STMT
     310              : 
     311              : /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
     312              :    fails.  */
     313              : #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC)                      \
     314              :   JIT_BEGIN_STMT                                                        \
     315              :     RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block");         \
     316              :     RETURN_NULL_IF_FAIL_PRINTF2 (                                       \
     317              :       !(BLOCK)->has_been_terminated (),                              \
     318              :       (BLOCK)->get_context (),                                               \
     319              :       (LOC),                                                            \
     320              :       "adding to terminated block: %s (already terminated by: %s)",   \
     321              :       (BLOCK)->get_debug_string (),                                  \
     322              :       (BLOCK)->get_last_statement ()->get_debug_string ());               \
     323              :   JIT_END_STMT
     324              : 
     325              : /* Format the given string, and report it as an error, either on CTXT
     326              :    if non-NULL, or by printing to stderr if we have a NULL context.
     327              :    LOC gives the source location where the error occcurred, and can be
     328              :    NULL.  */
     329              : 
     330              : static void
     331              : jit_error (gcc::jit::recording::context *ctxt,
     332              :            gcc::jit::recording::location *loc,
     333              :            const char *fmt, ...)
     334              :   GNU_PRINTF(3, 4);
     335              : 
     336              : static void
     337        10173 : jit_error (gcc::jit::recording::context *ctxt,
     338              :            gcc::jit::recording::location *loc,
     339              :            const char *fmt, ...)
     340              : {
     341        10173 :   va_list ap;
     342        10173 :   va_start (ap, fmt);
     343              : 
     344        10173 :   if (ctxt)
     345         4864 :     ctxt->add_error_va (loc, diagnostics::kind::error, fmt, ap);
     346              :   else
     347              :     {
     348              :       /* No context?  Send to stderr.  */
     349         5309 :       vfprintf (stderr, fmt, ap);
     350         5309 :       fprintf (stderr, "\n");
     351              :     }
     352              : 
     353        10173 :   va_end (ap);
     354        10173 : }
     355              : 
     356              : /* Determine whether or not we can write to lvalues of type LTYPE from
     357              :    rvalues of type RTYPE, detecting type errors such as attempting to
     358              :    write to an int with a string literal (without an explicit cast).
     359              : 
     360              :    This is implemented by calling the
     361              :    gcc::jit::recording::type::accepts_writes_from virtual function on
     362              :    LTYPE.  */
     363              : 
     364              : static bool
     365        16696 : compatible_types (gcc::jit::recording::type *ltype,
     366              :                   gcc::jit::recording::type *rtype)
     367              : {
     368        11744 :   return ltype->accepts_writes_from (rtype);
     369              : }
     370              : 
     371              : /* Public entrypoint wrapping compatible_types.  */
     372              : 
     373              : int
     374           75 : gcc_jit_compatible_types (gcc_jit_type *ltype,
     375              :                           gcc_jit_type *rtype)
     376              : {
     377           75 :   RETURN_VAL_IF_FAIL (ltype, 0, NULL, NULL, "NULL ltype");
     378           75 :   RETURN_VAL_IF_FAIL (rtype, 0, NULL, NULL, "NULL rtype");
     379           75 :   return compatible_types (ltype, rtype);
     380              : }
     381              : 
     382              : /* Public entrypoint for acquiring a gcc_jit_context.
     383              :    Note that this creates a new top-level context; contrast with
     384              :    gcc_jit_context_new_child_context below.
     385              : 
     386              :    The real work is done in the constructor for
     387              :    gcc::jit::recording::context in jit-recording.cc. */
     388              : 
     389              : gcc_jit_context *
     390         1906 : gcc_jit_context_acquire (void)
     391              : {
     392         1906 :   gcc_jit_context *ctxt = new gcc_jit_context (NULL);
     393         1906 :   ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
     394         1906 :   return ctxt;
     395              : }
     396              : 
     397              : /* Public entrypoint for releasing a gcc_jit_context.
     398              :    The real work is done in the destructor for
     399              :    gcc::jit::recording::context in jit-recording.cc.  */
     400              : 
     401              : void
     402         1871 : gcc_jit_context_release (gcc_jit_context *ctxt)
     403              : {
     404         1871 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
     405         1871 :   JIT_LOG_FUNC (ctxt->get_logger ());
     406         1871 :   ctxt->log ("deleting ctxt: %p", (void *)ctxt);
     407         1871 :   delete ctxt;
     408         1871 : }
     409              : 
     410              : /* Public entrypoint for creating a child context within
     411              :    PARENT_CTXT.  See description in libgccjit.h.
     412              : 
     413              :    The real work is done in the constructor for
     414              :    gcc::jit::recording::context in jit-recording.cc. */
     415              : 
     416              : gcc_jit_context *
     417           12 : gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
     418              : {
     419           12 :   RETURN_NULL_IF_FAIL (parent_ctxt, NULL, NULL, "NULL parent ctxt");
     420           12 :   JIT_LOG_FUNC (parent_ctxt->get_logger ());
     421           12 :   parent_ctxt->log ("parent_ctxt: %p", (void *)parent_ctxt);
     422           12 :   gcc_jit_context *child_ctxt = new gcc_jit_context (parent_ctxt);
     423           12 :   child_ctxt->log ("new child_ctxt: %p", (void *)child_ctxt);
     424           12 :   return child_ctxt;
     425           12 : }
     426              : 
     427              : /* Public entrypoint.  See description in libgccjit.h.
     428              : 
     429              :    After error-checking, the real work is done by the
     430              :      gcc::jit::recording::context::new_location
     431              :    method in jit-recording.cc.  */
     432              : 
     433              : gcc_jit_location *
     434         9717 : gcc_jit_context_new_location (gcc_jit_context *ctxt,
     435              :                               const char *filename,
     436              :                               int line,
     437              :                               int column)
     438              : {
     439         9717 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
     440         9717 :   JIT_LOG_FUNC (ctxt->get_logger ());
     441         9717 :   return (gcc_jit_location *)ctxt->new_location (filename, line, column, true);
     442         9717 : }
     443              : 
     444              : /* Public entrypoint.  See description in libgccjit.h.
     445              : 
     446              :    After error-checking, this calls the trivial
     447              :    gcc::jit::recording::memento::as_object method (a location is a
     448              :    memento), in jit-recording.h.  */
     449              : 
     450              : gcc_jit_object *
     451           24 : gcc_jit_location_as_object (gcc_jit_location *loc)
     452              : {
     453           24 :   RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
     454              : 
     455           24 :   return static_cast <gcc_jit_object *> (loc->as_object ());
     456              : }
     457              : 
     458              : /* Public entrypoint.  See description in libgccjit.h.
     459              : 
     460              :    After error-checking, this calls the trivial
     461              :    gcc::jit::recording::memento::as_object method (a type is a
     462              :    memento), in jit-recording.h.  */
     463              : 
     464              : gcc_jit_object *
     465          427 : gcc_jit_type_as_object (gcc_jit_type *type)
     466              : {
     467          427 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     468              : 
     469          427 :   return static_cast <gcc_jit_object *> (type->as_object ());
     470              : }
     471              : 
     472              : /* Public entrypoint for getting a specific type from a context.
     473              : 
     474              :    After error-checking, the real work is done by the
     475              :    gcc::jit::recording::context::get_type method, in
     476              :    jit-recording.cc  */
     477              : 
     478              : gcc_jit_type *
     479        16539 : gcc_jit_context_get_type (gcc_jit_context *ctxt,
     480              :                           enum gcc_jit_types type)
     481              : {
     482        16539 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
     483        16539 :   JIT_LOG_FUNC (ctxt->get_logger ());
     484        16539 :   RETURN_NULL_IF_FAIL_PRINTF1 (
     485              :     (type >= GCC_JIT_TYPE_VOID
     486              :      && type < NUM_GCC_JIT_TYPES),
     487              :     ctxt, NULL,
     488              :     "unrecognized value for enum gcc_jit_types: %i", type);
     489              : 
     490        16534 :   return (gcc_jit_type *)ctxt->get_type (type);
     491        16539 : }
     492              : 
     493              : /* Public entrypoint for getting the integer type of the given size and
     494              :    signedness.
     495              : 
     496              :    After error-checking, the real work is done by the
     497              :    gcc::jit::recording::context::get_int_type method,
     498              :    in jit-recording.cc.  */
     499              : 
     500              : gcc_jit_type *
     501          166 : gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
     502              :                               int num_bytes, int is_signed)
     503              : {
     504          166 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
     505          166 :   JIT_LOG_FUNC (ctxt->get_logger ());
     506          166 :   RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
     507              : 
     508          166 :   return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
     509          166 : }
     510              : 
     511              : /* Public entrypoint.  See description in libgccjit.h.
     512              : 
     513              :    After error-checking, the real work is done by the
     514              :    gcc::jit::recording::type::get_pointer method, in
     515              :    jit-recording.cc  */
     516              : 
     517              : gcc_jit_type *
     518         1510 : gcc_jit_type_get_pointer (gcc_jit_type *type)
     519              : {
     520         1510 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     521              : 
     522         1504 :   return (gcc_jit_type *)type->get_pointer ();
     523              : }
     524              : 
     525              : /* Public entrypoint.  See description in libgccjit.h.
     526              : 
     527              :    After error-checking, the real work is done by the
     528              :    gcc::jit::recording::type::get_const method, in
     529              :    jit-recording.cc.  */
     530              : 
     531              : gcc_jit_type *
     532          648 : gcc_jit_type_get_const (gcc_jit_type *type)
     533              : {
     534          648 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     535              : 
     536          646 :   return (gcc_jit_type *)type->get_const ();
     537              : }
     538              : 
     539              : /* Public entrypoint.  See description in libgccjit.h.
     540              : 
     541              :    After error-checking, the real work is done by the
     542              :    gcc::jit::recording::type::get_volatile method, in
     543              :    jit-recording.cc.  */
     544              : 
     545              : gcc_jit_type *
     546          257 : gcc_jit_type_get_volatile (gcc_jit_type *type)
     547              : {
     548          257 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     549              : 
     550          251 :   return (gcc_jit_type *)type->get_volatile ();
     551              : }
     552              : 
     553              : /* Public entrypoint.  See description in libgccjit.h.
     554              : 
     555              :    After error-checking, the real work is done by the
     556              :    gcc::jit::recording::type::get_restrict method, in
     557              :    jit-recording.cc.  */
     558              : 
     559              : gcc_jit_type *
     560           10 : gcc_jit_type_get_restrict (gcc_jit_type *type)
     561              : {
     562           10 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     563           10 :   RETURN_NULL_IF_FAIL (type->is_pointer (), NULL, NULL, "not a pointer type");
     564              : 
     565           10 :   return (gcc_jit_type *)type->get_restrict ();
     566              : }
     567              : 
     568              : /* Public entrypoint.  See description in libgccjit.h.
     569              : 
     570              :    After error-checking, the real work is done by the
     571              :    gcc::jit::recording::type::get_size method, in
     572              :    jit-recording.cc.  */
     573              : 
     574              : ssize_t
     575          100 : gcc_jit_type_get_size (gcc_jit_type *type)
     576              : {
     577          100 :   RETURN_VAL_IF_FAIL (type, -1, NULL, NULL, "NULL type");
     578          100 :   RETURN_VAL_IF_FAIL
     579              :     (type->is_int () || type->is_float () || type->is_pointer (), -1, NULL, NULL,
     580              :      "only getting the size of integer or floating-point or pointer types is supported for now");
     581          100 :   return type->get_size ();
     582              : }
     583              : 
     584              : /* Public entrypoint.  See description in libgccjit.h.
     585              : 
     586              :    After error-checking, the real work is done by the
     587              :    gcc::jit::recording::type::is_array method, in
     588              :    jit-recording.cc.  */
     589              : 
     590              : gcc_jit_type *
     591           30 : gcc_jit_type_dyncast_array (gcc_jit_type *type)
     592              : {
     593           30 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     594              : 
     595           30 :   return (gcc_jit_type *)type->is_array ();
     596              : }
     597              : 
     598              : /* Public entrypoint.  See description in libgccjit.h.
     599              : 
     600              :    After error-checking, the real work is done by the
     601              :    gcc::jit::recording::type::is_bool method, in
     602              :    jit-recording.cc.  */
     603              : 
     604              : int
     605          105 : gcc_jit_type_is_bool (gcc_jit_type *type)
     606              : {
     607          105 :   RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
     608              : 
     609          105 :   return type->is_bool ();
     610              : }
     611              : 
     612              : /* Public entrypoint.  See description in libgccjit.h.
     613              : 
     614              :    After error-checking, the real work is done by the
     615              :    gcc::jit::recording::type::is_pointer method, in
     616              :    jit-recording.cc.  */
     617              : 
     618              : gcc_jit_type *
     619           30 : gcc_jit_type_is_pointer (gcc_jit_type *type)
     620              : {
     621           30 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     622              : 
     623           30 :   return (gcc_jit_type *)type->is_pointer ();
     624              : }
     625              : 
     626              : /* Public entrypoint.  See description in libgccjit.h.
     627              : 
     628              :    After error-checking, the real work is done by the
     629              :    gcc::jit::recording::type::is_int method, in
     630              :    jit-recording.cc.  */
     631              : 
     632              : int
     633          180 : gcc_jit_type_is_integral (gcc_jit_type *type)
     634              : {
     635          180 :   RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
     636              : 
     637          180 :   return type->is_int ();
     638              : }
     639              : 
     640              : /* Public entrypoint.  See description in libgccjit.h.
     641              : 
     642              :    After error-checking, the real work is done by the
     643              :    gcc::jit::recording::type::is_float method, in
     644              :    jit-recording.cc.  */
     645              : 
     646              : int
     647          165 : gcc_jit_type_is_floating_point (gcc_jit_type *type)
     648              : {
     649          165 :   RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
     650              : 
     651          165 :   if (type->is_vector ())
     652              :     return false;
     653              : 
     654          165 :   return type->is_float ();
     655              : }
     656              : 
     657              : /* Public entrypoint.  See description in libgccjit.h.
     658              : 
     659              :    After error-checking, the real work is done by the
     660              :    gcc::jit::recording::type::is_vector method, in
     661              :    jit-recording.cc.  */
     662              : 
     663              : gcc_jit_vector_type *
     664           30 : gcc_jit_type_dyncast_vector (gcc_jit_type *type)
     665              : {
     666           30 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     667           30 :   gcc::jit::recording::vector_type *vector_type = type->is_vector ();
     668           30 :   return (gcc_jit_vector_type *)vector_type;
     669              : }
     670              : 
     671              : /* Public entrypoint.  See description in libgccjit.h.
     672              : 
     673              :    After error-checking, the real work is done by the
     674              :    gcc::jit::recording::type::is_struct method, in
     675              :    jit-recording.cc.  */
     676              : 
     677              : gcc_jit_struct *
     678           30 : gcc_jit_type_is_struct (gcc_jit_type *type)
     679              : {
     680           30 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     681           30 :   gcc::jit::recording::struct_ *struct_type = type->is_struct ();
     682           30 :   return (gcc_jit_struct *)struct_type;
     683              : }
     684              : 
     685              : /* Public entrypoint.  See description in libgccjit.h.
     686              : 
     687              :    After error-checking, the real work is done by the
     688              :    gcc::jit::recording::vector_type::get_num_units method, in
     689              :    jit-recording.cc.  */
     690              : 
     691              : size_t
     692           15 : gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
     693              : {
     694           15 :   RETURN_VAL_IF_FAIL (vector_type, 0, NULL, NULL, "NULL vector_type");
     695           15 :   return vector_type->get_num_units ();
     696              : }
     697              : 
     698              : /* Public entrypoint.  See description in libgccjit.h.
     699              : 
     700              :    After error-checking, the real work is done by the
     701              :    gcc::jit::recording::vector_type::get_element_type method, in
     702              :    jit-recording.cc.  */
     703              : 
     704              : gcc_jit_type *
     705           15 : gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type)
     706              : {
     707           15 :   RETURN_NULL_IF_FAIL (vector_type, NULL, NULL, "NULL vector_type");
     708           15 :   return (gcc_jit_type *)vector_type->get_element_type ();
     709              : }
     710              : 
     711              : /* Public entrypoint.  See description in libgccjit.h.
     712              : 
     713              :    After error-checking, the real work is done by the
     714              :    gcc::jit::recording::type::unqualified method, in
     715              :    jit-recording.cc.  */
     716              : 
     717              : gcc_jit_type *
     718           45 : gcc_jit_type_unqualified (gcc_jit_type *type)
     719              : {
     720           45 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     721              : 
     722           45 :   return (gcc_jit_type *)type->unqualified ();
     723              : }
     724              : 
     725              : /* Public entrypoint.  See description in libgccjit.h.
     726              : 
     727              :    After error-checking, the real work is done by the
     728              :    gcc::jit::recording::type::dyn_cast_function_type method, in
     729              :    jit-recording.cc.  */
     730              : 
     731              : gcc_jit_function_type *
     732           30 : gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type)
     733              : {
     734           30 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
     735           30 :   gcc::jit::recording::type *func_ptr_type = type->dereference ();
     736           30 :   if (!func_ptr_type)
     737              :   {
     738              :     return NULL;
     739              :   }
     740              : 
     741           15 :   return (gcc_jit_function_type *)func_ptr_type->dyn_cast_function_type ();
     742              : }
     743              : 
     744              : /* Public entrypoint.  See description in libgccjit.h.
     745              : 
     746              :    After error-checking, the real work is done by the
     747              :    gcc::jit::recording::function_type::get_return_type method, in
     748              :    jit-recording.cc.  */
     749              : 
     750              : gcc_jit_type *
     751           15 : gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
     752              : {
     753           15 :   RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL function_type");
     754           15 :   return (gcc_jit_type *)function_type->get_return_type ();
     755              : }
     756              : 
     757              : /* Public entrypoint.  See description in libgccjit.h.
     758              : 
     759              :    After error-checking, the real work is done by the
     760              :    gcc::jit::recording::function_type::get_param_types method, in
     761              :    jit-recording.cc.  */
     762              : 
     763              : size_t
     764           15 : gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
     765              : {
     766           15 :   RETURN_VAL_IF_FAIL (function_type, 0, NULL, NULL, "NULL function_type");
     767           30 :   return function_type->get_param_types ().length ();
     768              : }
     769              : 
     770              : /* Public entrypoint.  See description in libgccjit.h.
     771              : 
     772              :    After error-checking, the real work is done by the
     773              :    gcc::jit::recording::function_type::get_param_types method, in
     774              :    jit-recording.cc.  */
     775              : 
     776              : gcc_jit_type *
     777           30 : gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
     778              :                                 size_t index)
     779              : {
     780           30 :   RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL function_type");
     781           30 :   size_t num_params = function_type->get_param_types ().length ();
     782           30 :   gcc::jit::recording::context *ctxt = function_type->m_ctxt;
     783           30 :   RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
     784              :                                ctxt, NULL,
     785              :                                "index of %zu is too large (%s has %zu params)",
     786              :                                index,
     787              :                                function_type->get_debug_string (),
     788              :                                num_params);
     789           30 :   return (gcc_jit_type *)function_type->get_param_types ()[index];
     790              : }
     791              : 
     792              : /* Public entrypoint.  See description in libgccjit.h.
     793              : 
     794              :    After error-checking, the real work is done by the
     795              :    gcc::jit::recording::context::new_array_type method, in
     796              :    jit-recording.cc.  */
     797              : 
     798              : gcc_jit_type *
     799          395 : gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
     800              :                                 gcc_jit_location *loc,
     801              :                                 gcc_jit_type *element_type,
     802              :                                 int num_elements)
     803              : {
     804          395 :   RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
     805          395 :   return gcc_jit_context_new_array_type_u64 (ctxt, loc, element_type,
     806          395 :     (uint64_t) num_elements);
     807              : }
     808              : 
     809              : gcc_jit_type *
     810          410 : gcc_jit_context_new_array_type_u64 (gcc_jit_context *ctxt,
     811              :                                               gcc_jit_location *loc,
     812              :                                               gcc_jit_type *element_type,
     813              :                                               uint64_t num_elements)
     814              : {
     815          410 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
     816          410 :   JIT_LOG_FUNC (ctxt->get_logger ());
     817              :   /* LOC can be NULL.  */
     818          410 :   RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
     819          410 :   RETURN_NULL_IF_FAIL (!element_type->is_void (), ctxt, loc,
     820              :                        "void type for elements");
     821              : 
     822          410 :   return (gcc_jit_type *)ctxt->new_array_type (loc,
     823              :                                                element_type,
     824          410 :                                                num_elements);
     825          410 : }
     826              : 
     827              : /* Public entrypoint.  See description in libgccjit.h.
     828              : 
     829              :    After error-checking, the real work is done by the
     830              :    gcc::jit::recording::context::new_field method, in
     831              :    jit-recording.cc.  */
     832              : 
     833              : gcc_jit_field *
     834         4727 : gcc_jit_context_new_field (gcc_jit_context *ctxt,
     835              :                            gcc_jit_location *loc,
     836              :                            gcc_jit_type *type,
     837              :                            const char *name)
     838              : {
     839         4727 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
     840         4727 :   JIT_LOG_FUNC (ctxt->get_logger ());
     841              :   /* LOC can be NULL.  */
     842         4727 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
     843         4661 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
     844         4661 :   RETURN_NULL_IF_FAIL_PRINTF2 (
     845              :     type->has_known_size (),
     846              :     ctxt, loc,
     847              :     "unknown size for field \"%s\" (type: %s)",
     848              :     name,
     849              :     type->get_debug_string ());
     850         4656 :   RETURN_NULL_IF_FAIL_PRINTF1 (
     851              :     !type->is_void (),
     852              :     ctxt, loc,
     853              :     "void type for field \"%s\"",
     854              :     name);
     855              : 
     856         4520 :   return (gcc_jit_field *)ctxt->new_field (loc, type, name);
     857         4727 : }
     858              : 
     859              : /* Public entrypoint.  See description in libgccjit.h.
     860              : 
     861              :    After error-checking, the real work is done by the
     862              :    gcc::jit::recording::context::new_bitfield method, in
     863              :    jit-recording.cc.  */
     864              : 
     865              : gcc_jit_field *
     866          115 : gcc_jit_context_new_bitfield (gcc_jit_context *ctxt,
     867              :                               gcc_jit_location *loc,
     868              :                               gcc_jit_type *type,
     869              :                               int width,
     870              :                               const char *name)
     871              : {
     872          115 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
     873          115 :   JIT_LOG_FUNC (ctxt->get_logger ());
     874              :   /* LOC can be NULL.  */
     875          115 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
     876          115 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
     877          115 :   RETURN_NULL_IF_FAIL_PRINTF2 (type->is_int () || type->is_bool (),
     878              :                                ctxt, loc,
     879              :                                "bit-field %s has non integral type %s",
     880              :                                name, type->get_debug_string ());
     881          110 :   RETURN_NULL_IF_FAIL_PRINTF2 (
     882              :     width > 0, ctxt, loc,
     883              :     "invalid width %d for bitfield \"%s\" (must be > 0)",
     884              :     width, name);
     885          110 :   RETURN_NULL_IF_FAIL_PRINTF2 (
     886              :     type->has_known_size (),
     887              :     ctxt, loc,
     888              :     "unknown size for field \"%s\" (type: %s)",
     889              :     name,
     890              :     type->get_debug_string ());
     891              : 
     892          110 :   return (gcc_jit_field *)ctxt->new_bitfield (loc, type, width, name);
     893          115 : }
     894              : 
     895              : /* Public entrypoint.  See description in libgccjit.h.
     896              : 
     897              :    After error-checking, this calls the trivial
     898              :    gcc::jit::recording::memento::as_object method (a field is a
     899              :    memento), in jit-recording.h.  */
     900              : 
     901              : gcc_jit_object *
     902          110 : gcc_jit_field_as_object (gcc_jit_field *field)
     903              : {
     904          110 :   RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
     905              : 
     906          110 :   return static_cast <gcc_jit_object *> (field->as_object ());
     907              : }
     908              : 
     909              : /* Public entrypoint.  See description in libgccjit.h.
     910              : 
     911              :    After error-checking, the real work is done by the
     912              :    gcc::jit::recording::context::new_struct_type method,
     913              :    immediately followed by a "set_fields" call on the resulting
     914              :    gcc::jit::recording::compound_type *, both in jit-recording.cc  */
     915              : 
     916              : gcc_jit_struct *
     917         1042 : gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
     918              :                                  gcc_jit_location *loc,
     919              :                                  const char *name,
     920              :                                  int num_fields,
     921              :                                  gcc_jit_field **fields)
     922              : {
     923         1042 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
     924         1042 :   JIT_LOG_FUNC (ctxt->get_logger ());
     925              :   /* LOC can be NULL.  */
     926         1042 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
     927         1042 :   if (num_fields)
     928          984 :     RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
     929         4776 :   for (int i = 0; i < num_fields; i++)
     930              :     {
     931         3914 :       RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
     932         3734 :       RETURN_NULL_IF_FAIL_PRINTF2 (
     933              :         fields[i]->get_container () == NULL,
     934              :         ctxt, loc,
     935              :         "%s is already a field of %s",
     936              :         fields[i]->get_debug_string (),
     937              :         fields[i]->get_container ()->get_debug_string ());
     938              :     }
     939              : 
     940          862 :   gcc::jit::recording::struct_ *result =
     941          862 :     ctxt->new_struct_type (loc, name);
     942          862 :   result->set_fields (loc,
     943              :                       num_fields,
     944              :                       (gcc::jit::recording::field **)fields);
     945          862 :   return static_cast<gcc_jit_struct *> (result);
     946         1042 : }
     947              : 
     948              : /* Public entrypoint.  See description in libgccjit.h.
     949              : 
     950              :    After error-checking, the real work is done by the
     951              :    gcc::jit::recording::context::new_struct_type method in
     952              :    jit-recording.cc.  */
     953              : 
     954              : gcc_jit_struct *
     955          120 : gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
     956              :                                    gcc_jit_location *loc,
     957              :                                    const char *name)
     958              : {
     959          120 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
     960          120 :   JIT_LOG_FUNC (ctxt->get_logger ());
     961              :   /* LOC can be NULL.  */
     962          120 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
     963              : 
     964          120 :   return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
     965          120 : }
     966              : 
     967              : /* Public entrypoint.  See description in libgccjit.h.
     968              : 
     969              :    After error-checking, this calls the trivial
     970              :    gcc::jit::recording::struct_::as_object method in
     971              :    jit-recording.h.  */
     972              : 
     973              : gcc_jit_type *
     974         1207 : gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
     975              : {
     976         1207 :   RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
     977              : 
     978         1027 :   return static_cast <gcc_jit_type *> (struct_type->as_type ());
     979              : }
     980              : 
     981              : /* Public entrypoint.  See description in libgccjit.h.
     982              : 
     983              :    After error-checking, the real work is done by the
     984              :    gcc::jit::recording::compound_type::set_fields method in
     985              :    jit-recording.cc.  */
     986              : 
     987              : void
     988           90 : gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
     989              :                            gcc_jit_location *loc,
     990              :                            int num_fields,
     991              :                            gcc_jit_field **fields)
     992              : {
     993           90 :   RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
     994           90 :   gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
     995           90 :   JIT_LOG_FUNC (ctxt->get_logger ());
     996              :   /* LOC can be NULL.  */
     997           90 :   RETURN_IF_FAIL_PRINTF1 (
     998              :     struct_type->get_fields () == NULL, ctxt, loc,
     999              :     "%s already has had fields set",
    1000              :     struct_type->get_debug_string ());
    1001           90 :   if (num_fields)
    1002           90 :     RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
    1003          255 :   for (int i = 0; i < num_fields; i++)
    1004              :     {
    1005          165 :       RETURN_IF_FAIL_PRINTF2 (
    1006              :         fields[i],
    1007              :         ctxt, loc,
    1008              :         "%s: NULL field ptr at index %i",
    1009              :         struct_type->get_debug_string (),
    1010              :         i);
    1011          165 :       RETURN_IF_FAIL_PRINTF2 (
    1012              :         fields[i]->get_container () == NULL,
    1013              :         ctxt, loc,
    1014              :         "%s is already a field of %s",
    1015              :         fields[i]->get_debug_string (),
    1016              :         fields[i]->get_container ()->get_debug_string ());
    1017              :     }
    1018              : 
    1019           90 :   struct_type->set_fields (loc, num_fields,
    1020              :                            (gcc::jit::recording::field **)fields);
    1021           90 : }
    1022              : 
    1023              : 
    1024              : /* Public entrypoint.  See description in libgccjit.h.
    1025              : 
    1026              :    After error-checking, the real work is done by the
    1027              :    gcc::jit::recording::fields::get_field method in
    1028              :    jit-recording.cc.  */
    1029              : extern gcc_jit_field *
    1030           30 : gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
    1031              :                            size_t index)
    1032              : {
    1033           30 :   RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct type");
    1034           30 :   RETURN_NULL_IF_FAIL (struct_type->get_fields (), NULL, NULL,
    1035              :                                 "NULL struct fields");
    1036           30 :   size_t num_fields = struct_type->get_fields ()->length ();
    1037           30 :   RETURN_NULL_IF_FAIL_PRINTF3 (index < num_fields,
    1038              :                                NULL, NULL,
    1039              :                                "index of %zu is too large (%s has %zu fields)",
    1040              :                                index,
    1041              :                                struct_type->get_debug_string (),
    1042              :                                num_fields);
    1043           30 :   return (gcc_jit_field *)struct_type->get_fields ()->get_field (index);
    1044              : }
    1045              : 
    1046              : /* Public entrypoint.  See description in libgccjit.h.
    1047              : 
    1048              :    After error-checking, this calls the trivial
    1049              :    gcc::jit::recording::struct_::get_fields method in
    1050              :    jit-recording.h.  */
    1051              : 
    1052              : size_t
    1053           15 : gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
    1054              : {
    1055           15 :   RETURN_VAL_IF_FAIL (struct_type, 0, NULL, NULL, "NULL struct type");
    1056           30 :   return struct_type->get_fields ()->length ();
    1057              : }
    1058              : 
    1059              : /* Public entrypoint.  See description in libgccjit.h.
    1060              : 
    1061              :    After error-checking, the real work is done by the
    1062              :    gcc::jit::recording::context::new_union_type method,
    1063              :    immediately followed by a "set_fields" call on the resulting
    1064              :    gcc::jit::recording::compound_type *, both in jit-recording.cc  */
    1065              : 
    1066              : gcc_jit_type *
    1067           85 : gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
    1068              :                                 gcc_jit_location *loc,
    1069              :                                 const char *name,
    1070              :                                 int num_fields,
    1071              :                                 gcc_jit_field **fields)
    1072              : {
    1073           85 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    1074           85 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1075              :   /* LOC can be NULL.  */
    1076           85 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
    1077           85 :   if (num_fields)
    1078           85 :     RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
    1079          275 :   for (int i = 0; i < num_fields; i++)
    1080              :     {
    1081          190 :       RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
    1082          190 :       RETURN_NULL_IF_FAIL_PRINTF2 (
    1083              :         fields[i]->get_container () == NULL,
    1084              :         ctxt, loc,
    1085              :         "%s is already a field of %s",
    1086              :         fields[i]->get_debug_string (),
    1087              :         fields[i]->get_container ()->get_debug_string ());
    1088              :     }
    1089              : 
    1090           85 :   gcc::jit::recording::union_ *result =
    1091           85 :     ctxt->new_union_type (loc, name);
    1092           85 :   result->set_fields (loc,
    1093              :                       num_fields,
    1094              :                       (gcc::jit::recording::field **)fields);
    1095           85 :   return (gcc_jit_type *) (result);
    1096           85 : }
    1097              : 
    1098              : /* Public entrypoint.  See description in libgccjit.h.
    1099              : 
    1100              :    After error-checking, the real work is done by the
    1101              :    gcc::jit::recording::context::new_function_ptr_type method,
    1102              :    in jit-recording.cc  */
    1103              : 
    1104              : gcc_jit_type *
    1105           70 : gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
    1106              :                                        gcc_jit_location *loc,
    1107              :                                        gcc_jit_type *return_type,
    1108              :                                        int num_params,
    1109              :                                        gcc_jit_type **param_types,
    1110              :                                        int is_variadic)
    1111              : {
    1112           70 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    1113           70 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1114              :   /* LOC can be NULL.  */
    1115           70 :   RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
    1116           70 :   RETURN_NULL_IF_FAIL (
    1117              :     (num_params == 0) || param_types,
    1118              :     ctxt, loc,
    1119              :     "NULL param_types creating function pointer type");
    1120          200 :   for (int i = 0; i < num_params; i++)
    1121              :     {
    1122          130 :       RETURN_NULL_IF_FAIL_PRINTF1 (param_types[i],
    1123              :                                    ctxt, loc,
    1124              :                                    "NULL parameter type %i"
    1125              :                                    " creating function pointer type", i);
    1126          130 :       RETURN_NULL_IF_FAIL_PRINTF1 (!param_types[i]->is_void (),
    1127              :                                    ctxt, loc,
    1128              :                                    "void type for param %i", i);
    1129              :     }
    1130              : 
    1131           70 :   return (gcc_jit_type*)
    1132           70 :     ctxt->new_function_ptr_type (loc, return_type,
    1133              :                                  num_params,
    1134              :                                  (gcc::jit::recording::type **)param_types,
    1135           70 :                                  is_variadic);
    1136           70 : }
    1137              : 
    1138              : /* Constructing functions.  */
    1139              : 
    1140              : /* Public entrypoint.  See description in libgccjit.h.
    1141              : 
    1142              :    After error-checking, the real work is done by the
    1143              :    gcc::jit::recording::context::new_param method, in jit-recording.cc  */
    1144              : 
    1145              : gcc_jit_param *
    1146         8667 : gcc_jit_context_new_param (gcc_jit_context *ctxt,
    1147              :                            gcc_jit_location *loc,
    1148              :                            gcc_jit_type *type,
    1149              :                            const char *name)
    1150              : {
    1151         8667 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    1152         8667 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1153              :   /* LOC can be NULL.  */
    1154         8667 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    1155         8497 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
    1156         8497 :   RETURN_NULL_IF_FAIL_PRINTF1 (!type->is_void (),
    1157              :                                ctxt, loc,
    1158              :                                "void type for param \"%s\"", name);
    1159              : 
    1160         8365 :   return (gcc_jit_param *)ctxt->new_param (loc, type, name);
    1161         8667 : }
    1162              : 
    1163              : /* Public entrypoint.  See description in libgccjit.h.
    1164              : 
    1165              :    After error-checking, this calls the trivial
    1166              :    gcc::jit::recording::memento::as_object method (a param is a memento),
    1167              :    in jit-recording.h.  */
    1168              : 
    1169              : gcc_jit_object *
    1170            0 : gcc_jit_param_as_object (gcc_jit_param *param)
    1171              : {
    1172            0 :   RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
    1173              : 
    1174            0 :   return static_cast <gcc_jit_object *> (param->as_object ());
    1175              : }
    1176              : 
    1177              : /* Public entrypoint.  See description in libgccjit.h.
    1178              : 
    1179              :    After error-checking, this calls the trivial
    1180              :    gcc::jit::recording::param::as_lvalue method in jit-recording.h.  */
    1181              : 
    1182              : gcc_jit_lvalue *
    1183         1934 : gcc_jit_param_as_lvalue (gcc_jit_param *param)
    1184              : {
    1185         1934 :   RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
    1186              : 
    1187         1758 :   return (gcc_jit_lvalue *)param->as_lvalue ();
    1188              : }
    1189              : 
    1190              : /* Public entrypoint.  See description in libgccjit.h.
    1191              : 
    1192              :    After error-checking, this calls the trivial
    1193              :    gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
    1194              :    in jit-recording.h.  */
    1195              : 
    1196              : gcc_jit_rvalue *
    1197         5560 : gcc_jit_param_as_rvalue (gcc_jit_param *param)
    1198              : {
    1199         5560 :   RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
    1200              : 
    1201         5560 :   return (gcc_jit_rvalue *)param->as_rvalue ();
    1202              : }
    1203              : 
    1204              : /* Public entrypoint.  See description in libgccjit.h.
    1205              : 
    1206              :    After error-checking, the real work is done by the
    1207              :    gcc::jit::recording::context::new_function method, in
    1208              :    jit-recording.cc.  */
    1209              : 
    1210              : gcc_jit_function *
    1211         4720 : gcc_jit_context_new_function (gcc_jit_context *ctxt,
    1212              :                               gcc_jit_location *loc,
    1213              :                               enum gcc_jit_function_kind kind,
    1214              :                               gcc_jit_type *return_type,
    1215              :                               const char *name,
    1216              :                               int num_params,
    1217              :                               gcc_jit_param **params,
    1218              :                               int is_variadic)
    1219              : {
    1220         4720 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    1221         4720 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1222              :   /* LOC can be NULL.  */
    1223         4720 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    1224              :     ((kind >= GCC_JIT_FUNCTION_EXPORTED)
    1225              :      && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
    1226              :     ctxt, loc,
    1227              :     "unrecognized value for enum gcc_jit_function_kind: %i",
    1228              :     kind);
    1229         4715 :   RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
    1230         4674 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
    1231              :   /* The assembler can only handle certain names, so for now, enforce
    1232              :      C's rules for identifiers upon the name, using ISALPHA and ISALNUM
    1233              :      from safe-ctype.h to ignore the current locale.
    1234              :      Eventually we'll need some way to interact with e.g. C++ name
    1235              :      mangling.  */
    1236         4674 :   {
    1237         4674 :     const char* special_chars_allowed
    1238         4674 :       = ctxt->get_str_option (GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES);
    1239              :     /* Leading char: */
    1240         4674 :     char ch = *name;
    1241         4674 :     RETURN_NULL_IF_FAIL_PRINTF2 (
    1242              :         ISALPHA (ch) || ch == '_' || (special_chars_allowed
    1243              :           && strchr (special_chars_allowed, ch)),
    1244              :         ctxt, loc,
    1245              :         "name \"%s\" contains invalid character: '%c'",
    1246              :         name, ch);
    1247              :     /* Subsequent chars: */
    1248      1064844 :     for (const char *ptr = name + 1; (ch = *ptr); ptr++)
    1249              :       {
    1250      1060170 :         RETURN_NULL_IF_FAIL_PRINTF2 (
    1251              :           ISALNUM (ch) || ch == '_' || (special_chars_allowed
    1252              :             && strchr (special_chars_allowed, ch)),
    1253              :           ctxt, loc,
    1254              :           "name \"%s\" contains invalid character: '%c'",
    1255              :           name, ch);
    1256              :       }
    1257              :   }
    1258         4674 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    1259              :     (num_params == 0) || params,
    1260              :     ctxt, loc,
    1261              :     "NULL params creating function %s", name);
    1262        12283 :   for (int i = 0; i < num_params; i++)
    1263              :     {
    1264         7838 :       RETURN_NULL_IF_FAIL_PRINTF2 (
    1265              :         params[i],
    1266              :         ctxt, loc,
    1267              :         "NULL parameter %i creating function %s", i, name);
    1268         7614 :       RETURN_NULL_IF_FAIL_PRINTF5 (
    1269              :         params[i]->get_scope () == NULL,
    1270              :         ctxt, loc,
    1271              :         "parameter %i \"%s\""
    1272              :         " (type: %s)"
    1273              :         " for function %s"
    1274              :         " was already used for function %s",
    1275              :         i, params[i]->get_debug_string (),
    1276              :         params[i]->get_type ()->get_debug_string (),
    1277              :         name,
    1278              :         params[i]->get_scope ()->get_debug_string ());
    1279              :     }
    1280              : 
    1281         4445 :   return (gcc_jit_function*)
    1282         4445 :     ctxt->new_function (loc, kind, return_type, name,
    1283              :                         num_params,
    1284              :                         (gcc::jit::recording::param **)params,
    1285              :                         is_variadic,
    1286         4445 :                         BUILT_IN_NONE);
    1287         4720 : }
    1288              : 
    1289              : /* Public entrypoint.  See description in libgccjit.h.
    1290              : 
    1291              :    After error-checking, the real work is done by the
    1292              :    gcc::jit::recording::context::get_builtin_function method, in
    1293              :    jit-recording.cc.  */
    1294              : 
    1295              : gcc_jit_function *
    1296          255 : gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
    1297              :                                       const char *name)
    1298              : {
    1299          255 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    1300          255 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1301          255 :   RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
    1302              : 
    1303          255 :   return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
    1304          255 : }
    1305              : 
    1306              : /* Public entrypoint.  See description in libgccjit.h.
    1307              : 
    1308              :    After error-checking, this calls the trivial
    1309              :    gcc::jit::recording::memento::as_object method (a function is a
    1310              :    memento), in jit-recording.h.  */
    1311              : 
    1312              : gcc_jit_object *
    1313          151 : gcc_jit_function_as_object (gcc_jit_function *func)
    1314              : {
    1315          151 :   RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
    1316              : 
    1317          151 :   return static_cast <gcc_jit_object *> (func->as_object ());
    1318              : }
    1319              : 
    1320              : /* Public entrypoint.  See description in libgccjit.h.
    1321              : 
    1322              :    After error-checking, the real work is done by the
    1323              :    gcc::jit::recording::function::get_param method, in
    1324              :    jit-recording.h.  */
    1325              : 
    1326              : gcc_jit_param *
    1327           30 : gcc_jit_function_get_param (gcc_jit_function *func, int index)
    1328              : {
    1329           30 :   RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
    1330           30 :   gcc::jit::recording::context *ctxt = func->m_ctxt;
    1331           30 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1332           30 :   RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
    1333           30 :   int num_params = func->get_params ().length ();
    1334           30 :   RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
    1335              :                                ctxt, NULL,
    1336              :                                "index of %d is too large (%s has %d params)",
    1337              :                                index,
    1338              :                                func->get_debug_string (),
    1339              :                                num_params);
    1340              : 
    1341           30 :   return static_cast <gcc_jit_param *> (func->get_param (index));
    1342           30 : }
    1343              : 
    1344              : /* Public entrypoint.  See description in libgccjit.h.
    1345              : 
    1346              :    After error-checking, the real work is done by the
    1347              :    gcc::jit::recording::function::get_params method, in
    1348              :    jit-recording.h.
    1349              :   */
    1350              : 
    1351              : size_t
    1352           15 : gcc_jit_function_get_param_count (gcc_jit_function *func)
    1353              : {
    1354           15 :   RETURN_VAL_IF_FAIL (func, 0, NULL, NULL, "NULL function");
    1355           15 :   gcc::jit::recording::context *ctxt = func->m_ctxt;
    1356           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1357           15 :   return func->get_params ().length ();
    1358           15 : }
    1359              : 
    1360              : /* Public entrypoint.  See description in libgccjit.h.
    1361              : 
    1362              :    After error-checking, the real work is done by the
    1363              :    gcc::jit::recording::function::get_return_type method, in
    1364              :    jit-recording.h.  */
    1365              : 
    1366              : gcc_jit_type *
    1367           15 : gcc_jit_function_get_return_type (gcc_jit_function *func)
    1368              : {
    1369           15 :     RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function_type");
    1370           15 :     return (gcc_jit_type *)func->get_return_type ();
    1371              : }
    1372              : 
    1373              : /* Public entrypoint.  See description in libgccjit.h.
    1374              : 
    1375              :    After error-checking, the real work is done by the
    1376              :    gcc::jit::recording::function::dump_to_dot method, in
    1377              :    jit-recording.cc.  */
    1378              : 
    1379              : void
    1380            0 : gcc_jit_function_dump_to_dot (gcc_jit_function *func,
    1381              :                               const char *path)
    1382              : {
    1383            0 :   RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
    1384            0 :   gcc::jit::recording::context *ctxt = func->m_ctxt;
    1385            0 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1386            0 :   RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
    1387              : 
    1388            0 :   func->dump_to_dot (path);
    1389            0 : }
    1390              : 
    1391              : /* Public entrypoint.  See description in libgccjit.h.
    1392              : 
    1393              :    After error-checking, the real work is done by the
    1394              :    gcc::jit::recording::function::new_block method, in
    1395              :    jit-recording.cc.  */
    1396              : 
    1397              : gcc_jit_block*
    1398         7091 : gcc_jit_function_new_block (gcc_jit_function *func,
    1399              :                             const char *name)
    1400              : {
    1401         7091 :   RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
    1402         6831 :   JIT_LOG_FUNC (func->get_context ()->get_logger ());
    1403         6830 :   RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
    1404              :                        func->get_context (), NULL,
    1405              :                        "cannot add block to an imported function");
    1406              :   /* name can be NULL.  */
    1407              : 
    1408         6608 :   return (gcc_jit_block *)func->new_block (name);
    1409         6831 : }
    1410              : 
    1411              : /* Public entrypoint.  See description in libgccjit.h.
    1412              : 
    1413              :    After error-checking, this calls the trivial
    1414              :    gcc::jit::recording::memento::as_object method (a block is a
    1415              :    memento), in jit-recording.h.  */
    1416              : 
    1417              : gcc_jit_object *
    1418          237 : gcc_jit_block_as_object (gcc_jit_block *block)
    1419              : {
    1420          237 :   RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
    1421              : 
    1422          237 :   return static_cast <gcc_jit_object *> (block->as_object ());
    1423              : }
    1424              : 
    1425              : /* Public entrypoint.  See description in libgccjit.h.
    1426              : 
    1427              :    After error-checking, the real work is done by the
    1428              :    gcc::jit::recording::block::get_function method, in
    1429              :    jit-recording.h.  */
    1430              : 
    1431              : gcc_jit_function *
    1432            0 : gcc_jit_block_get_function (gcc_jit_block *block)
    1433              : {
    1434            0 :   RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
    1435              : 
    1436            0 :   return static_cast <gcc_jit_function *> (block->get_function ());
    1437              : }
    1438              : 
    1439              : /* Public entrypoint.  See description in libgccjit.h.
    1440              : 
    1441              :    After error-checking, the real work is done by the
    1442              :    gcc::jit::recording::context::new_global method, in
    1443              :    jit-recording.cc.  */
    1444              : 
    1445              : gcc_jit_lvalue *
    1446         2595 : gcc_jit_context_new_global (gcc_jit_context *ctxt,
    1447              :                             gcc_jit_location *loc,
    1448              :                             enum gcc_jit_global_kind kind,
    1449              :                             gcc_jit_type *type,
    1450              :                             const char *name)
    1451              : {
    1452         2595 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    1453         2595 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1454              :   /* LOC can be NULL.  */
    1455         2595 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    1456              :     ((kind >= GCC_JIT_GLOBAL_EXPORTED)
    1457              :      && (kind <= GCC_JIT_GLOBAL_IMPORTED)),
    1458              :     ctxt, loc,
    1459              :     "unrecognized value for enum gcc_jit_global_kind: %i",
    1460              :     kind);
    1461         2595 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    1462         2543 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
    1463         2543 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    1464              :     type->has_known_size (),
    1465              :     ctxt, loc,
    1466              :     "unknown size for global \"%s\" (type: %s)",
    1467              :     name,
    1468              :     type->get_debug_string ());
    1469         2538 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    1470              :     !type->is_void (),
    1471              :     ctxt, loc,
    1472              :     "void type for global \"%s\"",
    1473              :     name);
    1474              : 
    1475         2479 :   return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
    1476         2595 : }
    1477              : 
    1478              : extern gcc_jit_rvalue *
    1479          365 : gcc_jit_context_new_struct_constructor (gcc_jit_context *ctxt,
    1480              :                                         gcc_jit_location *loc,
    1481              :                                         gcc_jit_type *type,
    1482              :                                         size_t num_values,
    1483              :                                         gcc_jit_field **fields,
    1484              :                                         gcc_jit_rvalue **values)
    1485              : {
    1486          365 :   using namespace gcc::jit::recording;
    1487              : 
    1488          365 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    1489          365 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1490          365 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    1491              : 
    1492          365 :   RETURN_NULL_IF_FAIL_PRINTF1 (type->is_struct (),
    1493              :                                ctxt, loc,
    1494              :                                "constructor type is not a struct: %s",
    1495              :                                type->get_debug_string ());
    1496              : 
    1497          365 :   compound_type *ct = reinterpret_cast<compound_type *>(type);
    1498          365 :   gcc::jit::recording::fields *fields_struct = ct->get_fields ();
    1499          365 :   size_t n_fields = fields_struct->length ();
    1500              : 
    1501          365 :   RETURN_NULL_IF_FAIL_PRINTF1 (ct->has_known_size (),
    1502              :                                ctxt, loc,
    1503              :                                "struct can't be opaque: %s",
    1504              :                                type->get_debug_string ());
    1505          365 :   RETURN_NULL_IF_FAIL_PRINTF1 (n_fields,
    1506              :                                ctxt, loc,
    1507              :                                "no fields in struct: %s",
    1508              :                                type->get_debug_string ());
    1509              : 
    1510              :   /* If there is no array input we just short circuit to zero the struct.  */
    1511          365 :   if (!num_values)
    1512           30 :     return (gcc_jit_rvalue *)ctxt->new_ctor (loc, type, 0, NULL, NULL);
    1513              : 
    1514          335 :   RETURN_NULL_IF_FAIL_PRINTF3 (n_fields >= num_values,
    1515              :                                ctxt, loc,
    1516              :                                "more values in constructor (n=%zu) than fields"
    1517              :                                " in target %s (n=%zu)",
    1518              :                                num_values,
    1519              :                                type->get_debug_string (),
    1520              :                                n_fields);
    1521              : 
    1522              :   /* It is OK if fields are null here, indicating definition order,
    1523              :      but there has to be a values array.  */
    1524          330 :   RETURN_NULL_IF_FAIL (values,
    1525              :                        ctxt, loc,
    1526              :                        "'values' NULL with non-zero 'num_values'");
    1527              : 
    1528              :   size_t idx = 0; /* Runner index for fields in the type object.  */
    1529              : 
    1530          895 :   for (size_t i = 0; i < num_values; i++)
    1531              :     {
    1532          580 :       gcc::jit::recording::rvalue *rv = values[i];
    1533              : 
    1534              :       /* rv kan be NULL, which would indicate zero init for the field.  */
    1535          580 :       gcc::jit::recording::type *rv_type = rv ? rv->get_type () : nullptr;
    1536              : 
    1537              :       /* If fields are specified we need to check that they are in
    1538              :          definition order.  */
    1539          580 :       if (fields)
    1540              :         {
    1541          365 :           gcc::jit::recording::field *f = fields[i];
    1542              : 
    1543          365 :           RETURN_NULL_IF_FAIL_PRINTF1 (
    1544              :             f,
    1545              :             ctxt, loc,
    1546              :             "NULL field in 'fields', at index %zu", i);
    1547              : 
    1548          365 :           RETURN_NULL_IF_FAIL_PRINTF3 (
    1549              :             f->get_container () ==
    1550              :             static_cast<gcc::jit::recording::type*>(type),
    1551              :             ctxt, loc,
    1552              :             "field object at index %zu (%s), was not used when creating "
    1553              :             "the %s",
    1554              :             i,
    1555              :             f->get_debug_string (),
    1556              :             type->get_debug_string ());
    1557              : 
    1558              :           /* Fields in the constructor need to be in struct definition
    1559              :              order, but there can be gaps.  */
    1560              :           size_t j;
    1561          565 :           for (j = idx; j < n_fields; j++)
    1562              :             {
    1563          565 :               field *fs = fields_struct->get_field (j);
    1564          565 :               if (fs == f)
    1565              :                 {
    1566              :                   idx = j; /* Advance runner index for next iteration.  */
    1567              :                   break;
    1568              :                 }
    1569              :             }
    1570              : 
    1571          360 :           RETURN_NULL_IF_FAIL_PRINTF3 (
    1572              :             j != n_fields,
    1573              :             ctxt, loc,
    1574              :             "field at index %zu in 'fields' is not in definition order "
    1575              :             "(struct: %s) (ctor field: %s)",
    1576              :             i,
    1577              :             type->get_debug_string (),
    1578              :             f->get_debug_string ());
    1579              : 
    1580              :           /* Check that the specified field has the same type as the
    1581              :              value, unless the value is null (a zero value init).  */
    1582          360 :           RETURN_NULL_IF_FAIL_PRINTF5 (
    1583              :             !rv || gcc::jit::types_kinda_same (rv_type,
    1584              :                                                f->get_type ()),
    1585              :             ctxt, loc,
    1586              :             "value and field not the same unqualified type, at index %zu"
    1587              :             " (%s.%s: %s)(value type: %s)",
    1588              :             i,
    1589              :             type->get_debug_string (),
    1590              :             f->get_debug_string (),
    1591              :             f->get_type ()->get_debug_string (),
    1592              :             rv_type->get_debug_string ());
    1593              :         }
    1594              : 
    1595              :       /* If no fields are specified, check that the value has the same type
    1596              :          as the field in the definition of the struct.  */
    1597          570 :       if (rv && !fields)
    1598              :         {
    1599          170 :           RETURN_NULL_IF_FAIL_PRINTF5 (
    1600              :             gcc::jit::types_kinda_same (rv_type,
    1601              :                                         fields_struct->
    1602              :                                           get_field (i)->get_type ()),
    1603              :             ctxt, loc,
    1604              :             "value and field not the same unqualified type, at index %zu"
    1605              :             " (%s.%s: %s)(value type: %s)",
    1606              :             i,
    1607              :             type->get_debug_string (),
    1608              :             fields_struct->get_field (i)->get_debug_string (),
    1609              :             fields_struct->get_field (i)->get_type ()->get_debug_string (),
    1610              :             rv_type->get_debug_string ());
    1611              :         }
    1612              : 
    1613          565 :       if (rv)
    1614              :         {
    1615          465 :           RETURN_NULL_IF_FAIL_PRINTF1 (
    1616              :             !rv_type->is_void (),
    1617              :             ctxt, loc,
    1618              :             "can't construct the void type, at index %zu", i);
    1619              :         }
    1620              :     }
    1621              : 
    1622          315 :   return (gcc_jit_rvalue *)ctxt->new_ctor (
    1623              :     loc,
    1624              :     type,
    1625              :     num_values,
    1626              :     reinterpret_cast<gcc::jit::recording::field**>(fields),
    1627          315 :     reinterpret_cast<gcc::jit::recording::rvalue**>(values));
    1628          365 : }
    1629              : 
    1630              : extern gcc_jit_rvalue *
    1631           80 : gcc_jit_context_new_union_constructor (gcc_jit_context *ctxt,
    1632              :                                        gcc_jit_location *loc,
    1633              :                                        gcc_jit_type *type,
    1634              :                                        gcc_jit_field *field,
    1635              :                                        gcc_jit_rvalue *value)
    1636              : {
    1637           80 :   using namespace gcc::jit::recording;
    1638              : 
    1639           80 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    1640           80 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1641           80 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    1642              : 
    1643           80 :   RETURN_NULL_IF_FAIL_PRINTF1 (type->is_union (),
    1644              :                                ctxt, loc,
    1645              :                                "constructor type is not an union: %s",
    1646              :                                type->get_debug_string ());
    1647              : 
    1648           80 :   compound_type *ct = reinterpret_cast<compound_type *>(type);
    1649           80 :   gcc::jit::recording::fields *fields_union = ct->get_fields ();
    1650           80 :   size_t n_fields = fields_union->length ();
    1651              : 
    1652           80 :   RETURN_NULL_IF_FAIL_PRINTF1 (ct->has_known_size (),
    1653              :                                ctxt, loc,
    1654              :                                "union can't be opaque: %s",
    1655              :                                type->get_debug_string ());
    1656           80 :   RETURN_NULL_IF_FAIL_PRINTF1 (n_fields,
    1657              :                                ctxt, loc,
    1658              :                                "no fields in union: %s",
    1659              :                                type->get_debug_string ());
    1660              : 
    1661              :   /* If value is NULL we are just supposed to zero the whole union.  */
    1662           80 :   if (!value)
    1663           15 :     return (gcc_jit_rvalue *)ctxt->new_ctor (loc, type, 0, NULL, NULL);
    1664              : 
    1665           65 :   gcc::jit::recording::type *rv_type = value->get_type ();
    1666              : 
    1667           65 :   RETURN_NULL_IF_FAIL (
    1668              :     !rv_type->is_void (),
    1669              :     ctxt, loc,
    1670              :     "can't construct the void type");
    1671              : 
    1672           65 :   if (field)
    1673              :     {
    1674           50 :       RETURN_NULL_IF_FAIL_PRINTF2 (
    1675              :         field->get_container () ==
    1676              :         static_cast<gcc::jit::recording::type*>(type),
    1677              :         ctxt, loc,
    1678              :         "field object (%s) was not used when creating "
    1679              :         "the type %s",
    1680              :         field->get_debug_string (),
    1681              :         type->get_debug_string ());
    1682              : 
    1683           45 :       RETURN_NULL_IF_FAIL_PRINTF4 (
    1684              :         gcc::jit::types_kinda_same (rv_type,
    1685              :                                     field->get_type ()),
    1686              :         ctxt, loc,
    1687              :         "value and field are not the same unqualified type"
    1688              :         " (%s.%s: %s)(value type: %s)",
    1689              :         type->get_debug_string (),
    1690              :         field->get_debug_string (),
    1691              :         field->get_type ()->get_debug_string (),
    1692              :         rv_type->get_debug_string ());
    1693              :     }
    1694              :   /* If no field is specified, check that the value has the same type
    1695              :      as the first field in the definition of the union.  */
    1696           60 :   if (!field)
    1697           15 :     RETURN_NULL_IF_FAIL_PRINTF2 (
    1698              :       gcc::jit::types_kinda_same (rv_type,
    1699              :                                   fields_union->
    1700              :                                     get_field (0)->get_type ()),
    1701              :       ctxt, loc,
    1702              :       "value and first union field not the same unqualified type"
    1703              :       " (field type: %s)(value type: %s)",
    1704              :       fields_union->get_field (0)->get_type ()->get_debug_string (),
    1705              :       rv_type->get_debug_string ());
    1706              : 
    1707              : 
    1708           60 :   return (gcc_jit_rvalue *)ctxt->new_ctor (
    1709              :     loc,
    1710              :     type,
    1711              :     1,
    1712              :     /* A NULL fields array tells new_ctor to take fields from the type obj.  */
    1713           60 :     reinterpret_cast<gcc::jit::recording::field**>(field ? &field : NULL),
    1714           60 :     reinterpret_cast<gcc::jit::recording::rvalue**>(&value));
    1715           80 : }
    1716              : 
    1717              : extern gcc_jit_rvalue *
    1718          270 : gcc_jit_context_new_array_constructor (gcc_jit_context *ctxt,
    1719              :                                        gcc_jit_location *loc,
    1720              :                                        gcc_jit_type *type,
    1721              :                                        size_t num_values,
    1722              :                                        gcc_jit_rvalue **values)
    1723              : {
    1724          270 :   using namespace gcc::jit::recording;
    1725              : 
    1726          270 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    1727          270 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1728          270 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    1729              : 
    1730          270 :   RETURN_NULL_IF_FAIL (type->is_array () != NULL,
    1731              :                        ctxt, loc,
    1732              :                        "constructor type not an array");
    1733              : 
    1734          270 :   if (!num_values)
    1735              :     values = NULL;
    1736              : 
    1737          255 :   if (num_values)
    1738              :     {
    1739          255 :       RETURN_NULL_IF_FAIL (
    1740              :         values,
    1741              :         ctxt, loc,
    1742              :         "'values' NULL with non-zero 'num_values'");
    1743              : 
    1744          255 :       gcc::jit::recording::array_type *arr_type =
    1745              :         reinterpret_cast<gcc::jit::recording::array_type*>(type);
    1746          255 :       size_t n_el = arr_type->num_elements ();
    1747              : 
    1748          255 :       RETURN_NULL_IF_FAIL_PRINTF2 (
    1749              :         n_el >= num_values,
    1750              :         ctxt, loc,
    1751              :         "array constructor has more values than the array type's length"
    1752              :         " (array type length: %zu, constructor length: %zu)",
    1753              :         n_el,
    1754              :         num_values);
    1755              : 
    1756              :       /* For arrays, all values need to be the same base type.  */
    1757          295 :       gcc::jit::recording::type *type0 = NULL;
    1758              :       size_t i = 0;
    1759              :       /* Find first non-null value.  */
    1760          295 :       for (;i < num_values; i++)
    1761              :         {
    1762          295 :           if (values[i])
    1763              :             break;
    1764              :         }
    1765              : 
    1766          250 :       if (i < num_values) /* All values might be null and i == num_values.  */
    1767          250 :         type0 = values[i]->get_type ();
    1768              : 
    1769              :       /* If we got a type0, check that all other values have
    1770              :          the same type.  */
    1771          995 :       for (; i < num_values; i++)
    1772              :         {
    1773          745 :           if (values[i])
    1774          715 :             RETURN_NULL_IF_FAIL_PRINTF3 (
    1775              :               gcc::jit::types_kinda_same (type0,
    1776              :                                           values[i]->get_type ()),
    1777              :               ctxt, loc,
    1778              :               "value type at index %zu differ from first value type"
    1779              :               " (first type: %s)(different type: %s)",
    1780              :               i,
    1781              :               type0->get_debug_string (),
    1782              :               values[i]->get_type ()->get_debug_string ());
    1783              :         }
    1784              : 
    1785              :       /* Compare type0 with the element type specified in the
    1786              :          type of the array.  */
    1787          250 :       if (type0)
    1788              :         {
    1789          250 :           gcc::jit::recording::type *el_type =
    1790          250 :             type->is_array ();
    1791              : 
    1792          250 :           RETURN_NULL_IF_FAIL_PRINTF2 (
    1793              :             gcc::jit::types_kinda_same (type0, el_type),
    1794              :             ctxt, loc,
    1795              :             "array element value types differ from types in 'values'"
    1796              :             " (element type: %s)('values' type: %s)",
    1797              :             el_type->get_debug_string (),
    1798              :             type0->get_debug_string ());
    1799              :         }
    1800              :     }
    1801              : 
    1802          260 :   return (gcc_jit_rvalue *)ctxt->new_ctor (
    1803              :     loc,
    1804              :     type,
    1805              :     num_values,
    1806              :     NULL,
    1807          260 :     reinterpret_cast<gcc::jit::recording::rvalue**>(values));
    1808          270 : }
    1809              : 
    1810              : /* Public entrypoint.  See description in libgccjit.h.
    1811              : 
    1812              :    After error-checking, the real work is done by the
    1813              :    gcc::jit::recording::context::get_target_builtin_function method, in
    1814              :    jit-recording.c.  */
    1815              : 
    1816              : gcc_jit_function *
    1817           10 : gcc_jit_context_get_target_builtin_function (gcc_jit_context *ctxt,
    1818              :                                              const char *name)
    1819              : {
    1820           10 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    1821           10 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1822           10 :   RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
    1823              : 
    1824           10 :   return static_cast <gcc_jit_function *> (
    1825           10 :     ctxt->get_target_builtin_function (name));
    1826           10 : }
    1827              : 
    1828              : /* Public entrypoint.  See description in libgccjit.h.  */
    1829              : 
    1830              : extern gcc_jit_lvalue *
    1831          875 : gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue *global,
    1832              :                                        gcc_jit_rvalue *init_rvalue)
    1833              : {
    1834          875 :   RETURN_NULL_IF_FAIL (global, NULL, NULL,"NULL global");
    1835              : 
    1836          875 :   gcc::jit::recording::context *ctxt = global->get_context ();
    1837          875 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL,"NULL context");
    1838          875 :   JIT_LOG_FUNC (ctxt->get_logger ());
    1839          875 :   RETURN_NULL_IF_FAIL (init_rvalue, ctxt, NULL,"NULL init_rvalue");
    1840              : 
    1841          875 :   RETURN_NULL_IF_FAIL_PRINTF1 (global->is_global (),
    1842              :                                ctxt, NULL,
    1843              :                                "lvalue \"%s\" not a global",
    1844              :                                global->get_debug_string ());
    1845              : 
    1846          875 :   gcc::jit::recording::global *gbl =
    1847              :     reinterpret_cast<gcc::jit::recording::global *> (global);
    1848              : 
    1849          875 :   RETURN_NULL_IF_FAIL_PRINTF1 (gbl->get_kind () !=
    1850              :                                GCC_JIT_GLOBAL_IMPORTED,
    1851              :                                ctxt, NULL,
    1852              :                                "can't initialize \"%s\", it is imported",
    1853              :                                global->get_debug_string ());
    1854              : 
    1855          875 :   RETURN_NULL_IF_FAIL_PRINTF4 (gcc::jit::types_kinda_same (
    1856              :                                  global->get_type (),
    1857              :                                  init_rvalue->get_type ()),
    1858              :                                ctxt, NULL,
    1859              :                                "mismatching types:"
    1860              :                                " initializing %s (type: %s) with %s (type: %s)",
    1861              :                                global->get_debug_string (),
    1862              :                                global->get_type ()->get_debug_string (),
    1863              :                                init_rvalue->get_debug_string (),
    1864              :                                init_rvalue->get_type ()->get_debug_string ());
    1865              : 
    1866              :   /* Check that there are no initializers set for the global yet.  */
    1867          875 :   RETURN_NULL_IF_FAIL_PRINTF1 (!gbl->test_flags_anyof (
    1868              :                                   gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT |
    1869              :                                   gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_BLOB_INIT),
    1870              :                                ctxt, NULL,
    1871              :                                "global variable already initialized: %s",
    1872              :                                global->get_debug_string ());
    1873              : 
    1874          870 :   if (!init_rvalue->verify_valid_within_global_init (__func__, global))
    1875              :     return NULL;
    1876              : 
    1877              :   /* The global need to know during playback that it will be
    1878              :      initialized.  */
    1879          860 :   gbl->set_flags (gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT);
    1880              : 
    1881          860 :   ctxt->new_global_init_rvalue (global, init_rvalue);
    1882              : 
    1883          860 :   return global;
    1884          875 : }
    1885              : 
    1886              : /* Public entrypoint.  See description in libgccjit.h.
    1887              : 
    1888              :    After error-checking, the real work is done by the
    1889              :    gcc::jit::recording::global::set_initializer method, in
    1890              :    jit-recording.cc.  */
    1891              : 
    1892              : extern gcc_jit_lvalue *
    1893           15 : gcc_jit_global_set_initializer (gcc_jit_lvalue *global,
    1894              :                                 const void *blob,
    1895              :                                 size_t num_bytes)
    1896              : {
    1897           15 :   RETURN_NULL_IF_FAIL (global, NULL, NULL, "NULL global");
    1898           15 :   RETURN_NULL_IF_FAIL (blob, NULL, NULL, "NULL blob");
    1899           15 :   RETURN_NULL_IF_FAIL_PRINTF1 (global->is_global (), NULL, NULL,
    1900              :                                "lvalue \"%s\" not a global",
    1901              :                                global->get_debug_string ());
    1902              : 
    1903           15 :   gcc::jit::recording::type *lval_type = global->get_type ();
    1904           15 :   RETURN_NULL_IF_FAIL_PRINTF1 (lval_type->is_array (), NULL, NULL,
    1905              :                                "global \"%s\" is not an array",
    1906              :                                global->get_debug_string ());
    1907           15 :   RETURN_NULL_IF_FAIL_PRINTF1 (lval_type->dereference ()->is_int (), NULL, NULL,
    1908              :                                "global \"%s\" is not an array of integral type",
    1909              :                                global->get_debug_string ());
    1910           15 :   size_t lvalue_size =
    1911           15 :     lval_type->dereference ()->get_size ()
    1912           15 :     * static_cast <gcc::jit::recording::array_type *> (lval_type)->num_elements ();
    1913           15 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    1914              :     lvalue_size == num_bytes, NULL, NULL,
    1915              :     "mismatching sizes:"
    1916              :     " global \"%s\" has size %zu whereas initializer has size %zu",
    1917              :     global->get_debug_string (), lvalue_size, num_bytes);
    1918              : 
    1919              :   /* Check that the rvalue initializer is not set for this global.
    1920              :      Note that we do not check if this blob type initializer is
    1921              :      already set, since that check was not present when the entrypoint
    1922              :      was initially written.  */
    1923           15 :   gcc::jit::recording::global *gbl =
    1924              :     reinterpret_cast<gcc::jit::recording::global *> (global);
    1925           15 :   RETURN_NULL_IF_FAIL_PRINTF1 (!gbl->test_flags_anyof (
    1926              :                                   gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT),
    1927              :                                NULL, NULL,
    1928              :                                "global variable already initialized: %s",
    1929              :                                global->get_debug_string ());
    1930              : 
    1931           15 :   gbl->set_initializer (blob, num_bytes);
    1932              :   /* The global need to know during playback that it will be
    1933              :      initialized.  */
    1934           15 :   gbl->set_flags (gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_BLOB_INIT);
    1935              : 
    1936           15 :   return global;
    1937              : }
    1938              : 
    1939              : /* Public entrypoint.  See description in libgccjit.h.
    1940              : 
    1941              :    After error-checking, the real work is done by the
    1942              :    gcc::jit::recording::global::set_readonly method, in
    1943              :    jit-recording.cc.  */
    1944              : 
    1945              : extern void
    1946           10 : gcc_jit_global_set_readonly (gcc_jit_lvalue *global)
    1947              : {
    1948           10 :   RETURN_IF_FAIL (global, NULL, NULL, "NULL global");
    1949           10 :   RETURN_IF_FAIL_PRINTF1 (global->is_global (), NULL, NULL,
    1950              :                                "lvalue \"%s\" not a global",
    1951              :                                global->get_debug_string ());
    1952              : 
    1953           10 :   global->set_readonly ();
    1954              : }
    1955              : 
    1956              : /* Public entrypoint.  See description in libgccjit.h.
    1957              : 
    1958              :    After error-checking, this calls the trivial
    1959              :    gcc::jit::recording::memento::as_object method (an lvalue is a
    1960              :    memento), in jit-recording.h.  */
    1961              : 
    1962              : gcc_jit_object *
    1963           30 : gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
    1964              : {
    1965           30 :   RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
    1966              : 
    1967           30 :   return static_cast <gcc_jit_object *> (lvalue->as_object ());
    1968              : }
    1969              : 
    1970              : /* Public entrypoint.  See description in libgccjit.h.
    1971              : 
    1972              :    After error-checking, this calls the trivial
    1973              :    gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h.  */
    1974              : 
    1975              : gcc_jit_rvalue *
    1976         9647 : gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
    1977              : {
    1978         9647 :   RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
    1979              : 
    1980         9098 :   return (gcc_jit_rvalue *)lvalue->as_rvalue ();
    1981              : }
    1982              : 
    1983              : /* Public entrypoint.  See description in libgccjit.h.
    1984              : 
    1985              :    After error-checking, this calls the trivial
    1986              :    gcc::jit::recording::memento::as_object method (an rvalue is a
    1987              :    memento), in jit-recording.h.  */
    1988              : 
    1989              : gcc_jit_object *
    1990         2116 : gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
    1991              : {
    1992         2116 :   RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
    1993              : 
    1994         2116 :   return static_cast <gcc_jit_object *> (rvalue->as_object ());
    1995              : }
    1996              : 
    1997              : /* Public entrypoint.  See description in libgccjit.h.
    1998              : 
    1999              :    After error-checking, the real work is done by the
    2000              :    gcc::jit::recording::rvalue::get_type method, in
    2001              :    jit-recording.h.  */
    2002              : 
    2003              : gcc_jit_type *
    2004          366 : gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
    2005              : {
    2006          366 :   RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
    2007              : 
    2008          366 :   return static_cast <gcc_jit_type *> (rvalue->get_type ());
    2009              : }
    2010              : 
    2011              : /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
    2012              :    type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
    2013              :    result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT).  */
    2014              : 
    2015              : #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
    2016              :   JIT_BEGIN_STMT                                                     \
    2017              :   RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
    2018              :   RETURN_NULL_IF_FAIL_PRINTF1 (                                \
    2019              :     NUMERIC_TYPE->is_numeric (), ctxt, NULL,                   \
    2020              :     "not a numeric type: %s",                                  \
    2021              :     NUMERIC_TYPE->get_debug_string ()); \
    2022              :   JIT_END_STMT
    2023              : 
    2024              : /* Public entrypoint.  See description in libgccjit.h.
    2025              : 
    2026              :    After error-checking, the real work is done by the
    2027              :    gcc::jit::recording::context::new_rvalue_from_const <int> method in
    2028              :    jit-recording.cc.  */
    2029              : 
    2030              : gcc_jit_rvalue *
    2031         8098 : gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
    2032              :                                      gcc_jit_type *numeric_type,
    2033              :                                      int value)
    2034              : {
    2035         8098 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2036         8098 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2037         8098 :   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
    2038              : 
    2039         7478 :   return ((gcc_jit_rvalue *)ctxt
    2040         7478 :           ->new_rvalue_from_const <int> (numeric_type, value));
    2041         8098 : }
    2042              : 
    2043              : /* Public entrypoint.  See description in libgccjit.h.
    2044              : 
    2045              :    After error-checking, the real work is done by the
    2046              :    gcc::jit::recording::context::new_rvalue_from_const <long> method
    2047              :    in jit-recording.cc.  */
    2048              : 
    2049              : gcc_jit_rvalue *
    2050           80 : gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
    2051              :                                       gcc_jit_type *numeric_type,
    2052              :                                       long value)
    2053              : {
    2054           80 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2055           80 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2056           80 :   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
    2057              : 
    2058           80 :   return ((gcc_jit_rvalue *)ctxt
    2059           80 :           ->new_rvalue_from_const <long> (numeric_type, value));
    2060           80 : }
    2061              : 
    2062              : /* Public entrypoint.  See description in libgccjit.h.
    2063              : 
    2064              :    This is essentially equivalent to:
    2065              :       gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
    2066              :    albeit with slightly different error messages if an error occurs.  */
    2067              : 
    2068              : gcc_jit_rvalue *
    2069          430 : gcc_jit_context_zero (gcc_jit_context *ctxt,
    2070              :                       gcc_jit_type *numeric_type)
    2071              : {
    2072          430 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2073          430 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2074          430 :   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
    2075              : 
    2076          430 :   return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
    2077          430 : }
    2078              : 
    2079              : /* Public entrypoint.  See description in libgccjit.h.
    2080              : 
    2081              :    This is essentially equivalent to:
    2082              :       gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
    2083              :    albeit with slightly different error messages if an error occurs.  */
    2084              : 
    2085              : gcc_jit_rvalue *
    2086          246 : gcc_jit_context_one (gcc_jit_context *ctxt,
    2087              :                      gcc_jit_type *numeric_type)
    2088              : {
    2089          246 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2090          246 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2091          246 :   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
    2092              : 
    2093          246 :   return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
    2094          246 : }
    2095              : 
    2096              : /* Public entrypoint.  See description in libgccjit.h.
    2097              : 
    2098              :    After error-checking, the real work is done by the
    2099              :    gcc::jit::recording::context::new_rvalue_from_const <double> method in
    2100              :    jit-recording.cc.  */
    2101              : 
    2102              : gcc_jit_rvalue *
    2103          240 : gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
    2104              :                                         gcc_jit_type *numeric_type,
    2105              :                                         double value)
    2106              : {
    2107          240 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2108          240 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2109          240 :   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
    2110              : 
    2111          235 :   return ((gcc_jit_rvalue *)ctxt
    2112          235 :           ->new_rvalue_from_const <double> (numeric_type, value));
    2113          240 : }
    2114              : 
    2115              : /* Public entrypoint.  See description in libgccjit.h.
    2116              : 
    2117              :    After error-checking, the real work is done by the
    2118              :    gcc::jit::recording::context::new_rvalue_from_const <void *> method
    2119              :    in jit-recording.cc.  */
    2120              : 
    2121              : gcc_jit_rvalue *
    2122          150 : gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
    2123              :                                      gcc_jit_type *pointer_type,
    2124              :                                      void *value)
    2125              : {
    2126          150 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2127          150 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2128          150 :   RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
    2129          150 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    2130              :     pointer_type->is_pointer (),
    2131              :     ctxt, NULL,
    2132              :     "not a pointer type (type: %s)",
    2133              :     pointer_type->get_debug_string ());
    2134              : 
    2135          150 :   return ((gcc_jit_rvalue *)ctxt
    2136          150 :           ->new_rvalue_from_const <void *> (pointer_type, value));
    2137          150 : }
    2138              : 
    2139              : /* Public entrypoint.  See description in libgccjit.h.
    2140              : 
    2141              :    This is essentially equivalent to:
    2142              :       gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
    2143              :    albeit with slightly different error messages if an error occurs.  */
    2144              : 
    2145              : gcc_jit_rvalue *
    2146           55 : gcc_jit_context_null (gcc_jit_context *ctxt,
    2147              :                       gcc_jit_type *pointer_type)
    2148              : {
    2149           55 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2150           55 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2151           55 :   RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
    2152           55 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    2153              :     pointer_type->is_pointer (),
    2154              :     ctxt, NULL,
    2155              :     "not a pointer type (type: %s)",
    2156              :     pointer_type->get_debug_string ());
    2157              : 
    2158           55 :   return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
    2159           55 : }
    2160              : 
    2161              : /* Public entrypoint.  See description in libgccjit.h.
    2162              : 
    2163              :    After error-checking, the real work is done by the
    2164              :    gcc::jit::recording::context::new_sizeof method in
    2165              :    jit-recording.cc.  */
    2166              : 
    2167              : gcc_jit_rvalue *
    2168           15 : gcc_jit_context_new_sizeof (gcc_jit_context *ctxt,
    2169              :                             gcc_jit_type *type)
    2170              : {
    2171           15 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2172           15 :   RETURN_NULL_IF_FAIL (type, ctxt, NULL, "NULL type");
    2173           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2174              : 
    2175           15 :   return ((gcc_jit_rvalue *)ctxt
    2176           15 :           ->new_sizeof (type));
    2177           15 : }
    2178              : 
    2179              : /* Public entrypoint.  See description in libgccjit.h.
    2180              : 
    2181              :    After error-checking, the real work is done by the
    2182              :    gcc::jit::recording::context::new_alignof method in
    2183              :    jit-recording.cc.  */
    2184              : 
    2185              : gcc_jit_rvalue *
    2186           15 : gcc_jit_context_new_alignof (gcc_jit_context *ctxt,
    2187              :                              gcc_jit_type *type)
    2188              : {
    2189           15 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2190           15 :   RETURN_NULL_IF_FAIL (type, ctxt, NULL, "NULL type");
    2191           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2192              : 
    2193           15 :   return ((gcc_jit_rvalue *)ctxt
    2194           15 :           ->new_alignof (type));
    2195           15 : }
    2196              : 
    2197              : /* Public entrypoint.  See description in libgccjit.h.
    2198              : 
    2199              :    After error-checking, the real work is done by the
    2200              :    gcc::jit::recording::context::new_string_literal method in
    2201              :    jit-recording.cc.  */
    2202              : 
    2203              : gcc_jit_rvalue *
    2204         1963 : gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
    2205              :                                     const char *value)
    2206              : {
    2207         1963 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    2208         1963 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2209         1963 :   RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
    2210              : 
    2211         1963 :   return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
    2212         1963 : }
    2213              : 
    2214              : /* Public entrypoint.  See description in libgccjit.h.
    2215              : 
    2216              :    After error-checking, the real work is done by the
    2217              :    gcc::jit::recording::context::new_unary_op method in
    2218              :    jit-recording.cc.  */
    2219              : 
    2220              : gcc_jit_rvalue *
    2221          138 : gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
    2222              :                               gcc_jit_location *loc,
    2223              :                               enum gcc_jit_unary_op op,
    2224              :                               gcc_jit_type *result_type,
    2225              :                               gcc_jit_rvalue *rvalue)
    2226              : {
    2227          138 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2228          138 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2229              :   /* LOC can be NULL.  */
    2230          138 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    2231              :     (op >= GCC_JIT_UNARY_OP_MINUS
    2232              :      && op <= GCC_JIT_UNARY_OP_ABS),
    2233              :     ctxt, loc,
    2234              :     "unrecognized value for enum gcc_jit_unary_op: %i",
    2235              :     op);
    2236          133 :   RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
    2237          133 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    2238              :     result_type->is_numeric () || result_type->is_numeric_vector (), ctxt, loc,
    2239              :     "gcc_jit_unary_op %s with operand %s "
    2240              :     "has non-numeric result_type: %s",
    2241              :     gcc::jit::unary_op_reproducer_strings[op],
    2242              :     rvalue->get_debug_string (),
    2243              :     result_type->get_debug_string ());
    2244          128 :   RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
    2245              : 
    2246          128 :   return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
    2247          138 : }
    2248              : 
    2249              : /* Determine if OP is a valid value for enum gcc_jit_binary_op.
    2250              :    For use by both gcc_jit_context_new_binary_op and
    2251              :    gcc_jit_block_add_assignment_op.  */
    2252              : 
    2253              : static bool
    2254            0 : valid_binary_op_p (enum gcc_jit_binary_op op)
    2255              : {
    2256            0 :   return (op >= GCC_JIT_BINARY_OP_PLUS
    2257            0 :           && op <= GCC_JIT_BINARY_OP_RSHIFT);
    2258              : }
    2259              : 
    2260              : /* Public entrypoint.  See description in libgccjit.h.
    2261              : 
    2262              :    After error-checking, the real work is done by the
    2263              :    gcc::jit::recording::context::new_binary_op method in
    2264              :    jit-recording.cc.  */
    2265              : 
    2266              : gcc_jit_rvalue *
    2267         3343 : gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
    2268              :                                gcc_jit_location *loc,
    2269              :                                enum gcc_jit_binary_op op,
    2270              :                                gcc_jit_type *result_type,
    2271              :                                gcc_jit_rvalue *a, gcc_jit_rvalue *b)
    2272              : {
    2273         3343 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2274         3343 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2275              :   /* LOC can be NULL.  */
    2276         3343 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    2277              :     valid_binary_op_p (op),
    2278              :     ctxt, loc,
    2279              :     "unrecognized value for enum gcc_jit_binary_op: %i",
    2280              :     op);
    2281         3338 :   RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
    2282         3338 :   RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
    2283         3338 :   RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
    2284         3338 :   RETURN_NULL_IF_FAIL_PRINTF4 (
    2285              :     compatible_types (a->get_type ()->unqualified (),
    2286              :                       b->get_type ()->unqualified ()),
    2287              :     ctxt, loc,
    2288              :     "mismatching types for binary op:"
    2289              :     " a: %s (type: %s) b: %s (type: %s)",
    2290              :     a->get_debug_string (),
    2291              :     a->get_type ()->get_debug_string (),
    2292              :     b->get_debug_string (),
    2293              :     b->get_type ()->get_debug_string ());
    2294         3337 :   RETURN_NULL_IF_FAIL_PRINTF4 (
    2295              :     result_type->is_numeric () || result_type->is_numeric_vector (), ctxt, loc,
    2296              :     "gcc_jit_binary_op %s with operands a: %s b: %s "
    2297              :     "has non-numeric result_type: %s",
    2298              :     gcc::jit::binary_op_reproducer_strings[op],
    2299              :     a->get_debug_string (), b->get_debug_string (),
    2300              :     result_type->get_debug_string ());
    2301              : 
    2302         3333 :   return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
    2303         3343 : }
    2304              : 
    2305              : /* Public entrypoint.  See description in libgccjit.h.
    2306              : 
    2307              :    After error-checking, the real work is done by the
    2308              :    gcc::jit::recording::context::new_comparison method in
    2309              :    jit-recording.cc.  */
    2310              : 
    2311              : gcc_jit_rvalue *
    2312         1524 : gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
    2313              :                                 gcc_jit_location *loc,
    2314              :                                 enum gcc_jit_comparison op,
    2315              :                                 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
    2316              : {
    2317         1524 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2318         1524 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2319              :   /* LOC can be NULL.  */
    2320         1524 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    2321              :     (op >= GCC_JIT_COMPARISON_EQ
    2322              :      && op <= GCC_JIT_COMPARISON_GE),
    2323              :     ctxt, loc,
    2324              :     "unrecognized value for enum gcc_jit_comparison: %i",
    2325              :     op);
    2326         1524 :   RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
    2327         1524 :   RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
    2328         1524 :   RETURN_NULL_IF_FAIL_PRINTF4 (
    2329              :     compatible_types (a->get_type ()->unqualified (),
    2330              :                       b->get_type ()->unqualified ()),
    2331              :     ctxt, loc,
    2332              :     "mismatching types for comparison:"
    2333              :     " a: %s (type: %s) b: %s (type: %s)",
    2334              :     a->get_debug_string (),
    2335              :     a->get_type ()->get_debug_string (),
    2336              :     b->get_debug_string (),
    2337              :     b->get_type ()->get_debug_string ());
    2338              : 
    2339         1524 :   return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
    2340         1524 : }
    2341              : 
    2342              : /* Public entrypoint.  See description in libgccjit.h.
    2343              : 
    2344              :    After error-checking, the real work is done by the
    2345              :    gcc::jit::recording::context::new_call method in
    2346              :    jit-recording.cc.  */
    2347              : 
    2348              : gcc_jit_rvalue *
    2349          830 : gcc_jit_context_new_call (gcc_jit_context *ctxt,
    2350              :                           gcc_jit_location *loc,
    2351              :                           gcc_jit_function *func,
    2352              :                           int numargs , gcc_jit_rvalue **args)
    2353              : {
    2354          830 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2355          830 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2356              :   /* LOC can be NULL.  */
    2357          830 :   RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
    2358          830 :   if (numargs)
    2359          730 :     RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
    2360              : 
    2361          830 :   int min_num_params = func->get_params ().length ();
    2362          830 :   bool is_variadic = func->is_variadic ();
    2363              : 
    2364          830 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    2365              :     numargs >= min_num_params,
    2366              :     ctxt, loc,
    2367              :     "not enough arguments to function \"%s\""
    2368              :     " (got %i args, expected %i)",
    2369              :     func->get_name ()->c_str (),
    2370              :     numargs, min_num_params);
    2371              : 
    2372          825 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    2373              :     (numargs == min_num_params || is_variadic),
    2374              :     ctxt, loc,
    2375              :     "too many arguments to function \"%s\""
    2376              :     " (got %i args, expected %i)",
    2377              :     func->get_name ()->c_str (),
    2378              :     numargs, min_num_params);
    2379              : 
    2380         1620 :   for (int i = 0; i < min_num_params; i++)
    2381              :     {
    2382          815 :       gcc::jit::recording::param *param = func->get_param (i);
    2383          815 :       gcc_jit_rvalue *arg = args[i];
    2384              : 
    2385          815 :       RETURN_NULL_IF_FAIL_PRINTF4 (
    2386              :         arg,
    2387              :         ctxt, loc,
    2388              :         "NULL argument %i to function \"%s\":"
    2389              :         " param %s (type: %s)",
    2390              :         i + 1,
    2391              :         func->get_name ()->c_str (),
    2392              :         param->get_debug_string (),
    2393              :         param->get_type ()->get_debug_string ());
    2394              : 
    2395          810 :       RETURN_NULL_IF_FAIL_PRINTF6 (
    2396              :         compatible_types (param->get_type (),
    2397              :                           arg->get_type ()),
    2398              :         ctxt, loc,
    2399              :         "mismatching types for argument %d of function \"%s\":"
    2400              :         " assignment to param %s (type: %s) from %s (type: %s)",
    2401              :         i + 1,
    2402              :         func->get_name ()->c_str (),
    2403              :         param->get_debug_string (),
    2404              :         param->get_type ()->get_debug_string (),
    2405              :         arg->get_debug_string (),
    2406              :         arg->get_type ()->get_debug_string ());
    2407              :     }
    2408              : 
    2409          805 :   return (gcc_jit_rvalue *)ctxt->new_call (loc,
    2410              :                                            func,
    2411              :                                            numargs,
    2412          805 :                                            (gcc::jit::recording::rvalue **)args);
    2413          830 : }
    2414              : 
    2415              : /* Public entrypoint.  See description in libgccjit.h.
    2416              : 
    2417              :    After error-checking, the real work is done by the
    2418              :    gcc::jit::recording::context::new_call_through_ptr method in
    2419              :    jit-recording.cc.  */
    2420              : 
    2421              : gcc_jit_rvalue *
    2422           40 : gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
    2423              :                                       gcc_jit_location *loc,
    2424              :                                       gcc_jit_rvalue *fn_ptr,
    2425              :                                       int numargs, gcc_jit_rvalue **args)
    2426              : {
    2427           40 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2428           40 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2429              :   /* LOC can be NULL.  */
    2430           40 :   RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
    2431           40 :   if (numargs)
    2432           35 :     RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
    2433              : 
    2434           40 :   gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
    2435           40 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2436              :     ptr_type, ctxt, loc,
    2437              :     "fn_ptr is not a ptr: %s"
    2438              :     " type: %s",
    2439              :     fn_ptr->get_debug_string (),
    2440              :     fn_ptr->get_type ()->get_debug_string ());
    2441              : 
    2442           35 :   gcc::jit::recording::function_type *fn_type =
    2443           35 :     ptr_type->dyn_cast_function_type();
    2444           35 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2445              :     fn_type, ctxt, loc,
    2446              :     "fn_ptr is not a function ptr: %s"
    2447              :     " type: %s",
    2448              :     fn_ptr->get_debug_string (),
    2449              :     fn_ptr->get_type ()->get_debug_string ());
    2450              : 
    2451           30 :   int min_num_params = fn_type->get_param_types ().length ();
    2452           30 :   bool is_variadic = fn_type->is_variadic ();
    2453              : 
    2454           30 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    2455              :     numargs >= min_num_params,
    2456              :     ctxt, loc,
    2457              :     "not enough arguments to fn_ptr: %s"
    2458              :     " (got %i args, expected %i)",
    2459              :     fn_ptr->get_debug_string (),
    2460              :     numargs, min_num_params);
    2461              : 
    2462           25 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    2463              :     (numargs == min_num_params || is_variadic),
    2464              :     ctxt, loc,
    2465              :     "too many arguments to fn_ptr: %s"
    2466              :     " (got %i args, expected %i)",
    2467              :     fn_ptr->get_debug_string (),
    2468              :     numargs, min_num_params);
    2469              : 
    2470           65 :   for (int i = 0; i < min_num_params; i++)
    2471              :     {
    2472           50 :       gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
    2473           50 :       gcc_jit_rvalue *arg = args[i];
    2474              : 
    2475           50 :       RETURN_NULL_IF_FAIL_PRINTF3 (
    2476              :         arg,
    2477              :         ctxt, loc,
    2478              :         "NULL argument %i to fn_ptr: %s"
    2479              :         " (type: %s)",
    2480              :         i + 1,
    2481              :         fn_ptr->get_debug_string (),
    2482              :         param_type->get_debug_string ());
    2483              : 
    2484           50 :       RETURN_NULL_IF_FAIL_PRINTF6 (
    2485              :         compatible_types (param_type,
    2486              :                           arg->get_type ()),
    2487              :         ctxt, loc,
    2488              :         "mismatching types for argument %d of fn_ptr: %s:"
    2489              :         " assignment to param %d (type: %s) from %s (type: %s)",
    2490              :         i + 1,
    2491              :         fn_ptr->get_debug_string (),
    2492              :         i + 1,
    2493              :         param_type->get_debug_string (),
    2494              :         arg->get_debug_string (),
    2495              :         arg->get_type ()->get_debug_string ());
    2496              :     }
    2497              : 
    2498           15 :   return (gcc_jit_rvalue *)(
    2499           15 :             ctxt->new_call_through_ptr (loc,
    2500              :                                         fn_ptr,
    2501              :                                         numargs,
    2502           15 :                                         (gcc::jit::recording::rvalue **)args));
    2503           40 : }
    2504              : 
    2505              : /* Helper function for determining if we can cast an rvalue from SRC_TYPE
    2506              :    to DST_TYPE, for use by gcc_jit_context_new_cast.
    2507              : 
    2508              :    We only permit these kinds of cast:
    2509              : 
    2510              :      int <-> float
    2511              :      int <-> bool
    2512              :      P*  <-> Q*   for pointer types P and Q.  */
    2513              : 
    2514              : static bool
    2515          404 : is_valid_cast (gcc::jit::recording::type *src_type,
    2516              :                gcc_jit_type *dst_type)
    2517              : {
    2518          404 :   bool src_is_int = src_type->is_int ();
    2519          404 :   bool dst_is_int = dst_type->is_int ();
    2520          404 :   bool src_is_float = src_type->is_float ();
    2521          404 :   bool dst_is_float = dst_type->is_float ();
    2522          404 :   bool src_is_bool = src_type->is_bool ();
    2523          404 :   bool dst_is_bool = dst_type->is_bool ();
    2524              : 
    2525          404 :   if (src_is_int)
    2526          155 :     if (dst_is_int || dst_is_float || dst_is_bool)
    2527              :       return true;
    2528              : 
    2529          249 :   if (src_is_float)
    2530           15 :     if (dst_is_int || dst_is_float)
    2531              :       return true;
    2532              : 
    2533          234 :   if (src_is_bool)
    2534           34 :     if (dst_is_int || dst_is_bool)
    2535              :       return true;
    2536              : 
    2537              :   /* Permit casts between pointer types and integers and pointers.  */
    2538          200 :   gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
    2539          200 :   gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
    2540          200 :   if ((deref_src_type || src_is_int) && (deref_dst_type || dst_is_int))
    2541              :     return true;
    2542              : 
    2543              :   return false;
    2544              : }
    2545              : 
    2546              : /* Public entrypoint.  See description in libgccjit.h.
    2547              : 
    2548              :    After error-checking, the real work is done by the
    2549              :    gcc::jit::recording::context::new_cast method in jit-recording.cc.  */
    2550              : 
    2551              : gcc_jit_rvalue *
    2552          404 : gcc_jit_context_new_cast (gcc_jit_context *ctxt,
    2553              :                           gcc_jit_location *loc,
    2554              :                           gcc_jit_rvalue *rvalue,
    2555              :                           gcc_jit_type *type)
    2556              : {
    2557          404 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2558          404 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2559              :   /* LOC can be NULL.  */
    2560          404 :   RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
    2561          404 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    2562          404 :   gcc::jit::recording::vector_type *vector_type = type->dyn_cast_vector_type ();
    2563          404 :   RETURN_NULL_IF_FAIL (vector_type == NULL, ctxt, loc,
    2564              :                        "cannot cast vector types");
    2565          404 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    2566              :     is_valid_cast (rvalue->get_type (), type),
    2567              :     ctxt, loc,
    2568              :     "cannot cast %s from type: %s to type: %s",
    2569              :     rvalue->get_debug_string (),
    2570              :     rvalue->get_type ()->get_debug_string (),
    2571              :     type->get_debug_string ());
    2572              : 
    2573          394 :   return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
    2574          404 : }
    2575              : 
    2576              : /* Public entrypoint.  See description in libgccjit.h.
    2577              : 
    2578              :    After error-checking, the real work is done by the
    2579              :    gcc::jit::recording::context::new_bitcast method in jit-recording.c.  */
    2580              : 
    2581              : gcc_jit_rvalue *
    2582           25 : gcc_jit_context_new_bitcast (gcc_jit_context *ctxt,
    2583              :                              gcc_jit_location *loc,
    2584              :                              gcc_jit_rvalue *rvalue,
    2585              :                              gcc_jit_type *type)
    2586              : {
    2587           25 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2588           25 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2589              :   /* LOC can be NULL.  */
    2590           25 :   RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
    2591           25 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    2592              :   /* We cannot check if the size of rvalue matches the size of type here, so
    2593              :    we'll do it at playback. */
    2594              : 
    2595           25 :   return static_cast <gcc_jit_rvalue *> (ctxt->new_bitcast (loc, rvalue, type));
    2596           25 : }
    2597              : 
    2598              : /* Public entrypoint.  See description in libgccjit.h.
    2599              : 
    2600              :    After error-checking, the real work is done by the
    2601              :    gcc::jit::recording::context::new_array_access method in
    2602              :    jit-recording.cc.  */
    2603              : 
    2604              : extern gcc_jit_lvalue *
    2605          539 : gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
    2606              :                                   gcc_jit_location *loc,
    2607              :                                   gcc_jit_rvalue *ptr,
    2608              :                                   gcc_jit_rvalue *index)
    2609              : {
    2610          539 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2611          539 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2612              :   /* LOC can be NULL.  */
    2613          539 :   RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
    2614          539 :   RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
    2615          539 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2616              :     ptr->get_type ()->dereference (),
    2617              :     ctxt, loc,
    2618              :     "ptr: %s (type: %s) is not a pointer or array",
    2619              :     ptr->get_debug_string (),
    2620              :     ptr->get_type ()->get_debug_string ());
    2621          539 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2622              :     index->get_type ()->is_numeric (),
    2623              :     ctxt, loc,
    2624              :     "index: %s (type: %s) is not of numeric type",
    2625              :     index->get_debug_string (),
    2626              :     index->get_type ()->get_debug_string ());
    2627              : 
    2628          534 :   return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
    2629          539 : }
    2630              : 
    2631              : /* Public entrypoint.  See description in libgccjit.h.
    2632              : 
    2633              :    After error-checking, the real work is done by the
    2634              :    gcc::jit::recording::context::new_convert_vector method in
    2635              :    jit-recording.cc.  */
    2636              : 
    2637              : gcc_jit_rvalue *
    2638           15 : gcc_jit_context_convert_vector (gcc_jit_context *ctxt,
    2639              :                                 gcc_jit_location *loc,
    2640              :                                 gcc_jit_rvalue *vector,
    2641              :                                 gcc_jit_type *type)
    2642              : {
    2643           15 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2644           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2645              :   /* LOC can be NULL.  */
    2646           15 :   RETURN_NULL_IF_FAIL (vector, ctxt, loc, "NULL vector");
    2647           15 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    2648              : 
    2649           15 :   gcc::jit::recording::vector_type *value_vec_type
    2650           15 :     = vector->get_type ()->dyn_cast_vector_type ();
    2651           15 :   RETURN_NULL_IF_FAIL_PRINTF1 (value_vec_type, ctxt, loc,
    2652              :                                "%s is not a value of a vector type",
    2653              :                                vector->get_debug_string ());
    2654           15 :   gcc::jit::recording::vector_type *as_vec_type
    2655           15 :     = type->dyn_cast_vector_type ();
    2656           15 :   RETURN_NULL_IF_FAIL_PRINTF1 (as_vec_type, ctxt, loc,
    2657              :                                "%s is not a vector type",
    2658              :                                type->get_debug_string ());
    2659           15 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2660              :     as_vec_type->get_num_units () == value_vec_type->get_num_units (), ctxt,
    2661              :     loc, "%s should contain the same number of elements as %s",
    2662              :     vector->get_debug_string (), type->get_debug_string ());
    2663              : 
    2664           15 :   return (gcc_jit_rvalue *)ctxt->new_convert_vector (loc, vector, type);
    2665           15 : }
    2666              : 
    2667              : /* Public entrypoint.  See description in libgccjit.h.
    2668              : 
    2669              :    After error-checking, the real work is done by the
    2670              :    gcc::jit::recording::context::new_vector_access method in
    2671              :    jit-recording.cc.  */
    2672              : 
    2673              : extern gcc_jit_lvalue *
    2674           15 : gcc_jit_context_new_vector_access (gcc_jit_context *ctxt,
    2675              :                                    gcc_jit_location *loc,
    2676              :                                    gcc_jit_rvalue *vector,
    2677              :                                    gcc_jit_rvalue *index)
    2678              : {
    2679           15 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
    2680           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2681              :   /* LOC can be NULL.  */
    2682           15 :   RETURN_NULL_IF_FAIL (vector, ctxt, loc, "NULL vector");
    2683           15 :   RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
    2684           15 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2685              :     vector->get_type ()->dyn_cast_vector_type (),
    2686              :     ctxt, loc,
    2687              :     "vector: %s (type: %s) is not a vector",
    2688              :     vector->get_debug_string (),
    2689              :     vector->get_type ()->get_debug_string ());
    2690           15 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2691              :     index->get_type ()->is_numeric (),
    2692              :     ctxt, loc,
    2693              :     "index: %s (type: %s) is not of numeric type",
    2694              :     index->get_debug_string (),
    2695              :     index->get_type ()->get_debug_string ());
    2696              : 
    2697           15 :   return (gcc_jit_lvalue *)ctxt->new_vector_access (loc, vector, index);
    2698           15 : }
    2699              : 
    2700              : /* Public entrypoint.  See description in libgccjit.h.
    2701              : 
    2702              :    After error-checking, the real work is done by the
    2703              :    gcc::jit::recording::memento::get_context method in
    2704              :    jit-recording.h.  */
    2705              : 
    2706              : gcc_jit_context *
    2707          132 : gcc_jit_object_get_context (gcc_jit_object *obj)
    2708              : {
    2709          132 :   RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
    2710              : 
    2711          132 :   return static_cast <gcc_jit_context *> (obj->get_context ());
    2712              : }
    2713              : 
    2714              : /* Public entrypoint.  See description in libgccjit.h.
    2715              : 
    2716              :    After error-checking, the real work is done by the
    2717              :    gcc::jit::recording::memento::get_debug_string method in
    2718              :    jit-recording.cc.  */
    2719              : 
    2720              : const char *
    2721         1240 : gcc_jit_object_get_debug_string (gcc_jit_object *obj)
    2722              : {
    2723         1240 :   RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
    2724              : 
    2725         1240 :   return obj->get_debug_string ();
    2726              : }
    2727              : 
    2728              : /* Public entrypoint.  See description in libgccjit.h.
    2729              : 
    2730              :    After error-checking, the real work is done by the
    2731              :    gcc::jit::recording::lvalue::access_field method in
    2732              :    jit-recording.cc.  */
    2733              : 
    2734              : gcc_jit_lvalue *
    2735          224 : gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
    2736              :                              gcc_jit_location *loc,
    2737              :                              gcc_jit_field *field)
    2738              : {
    2739          224 :   RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
    2740          224 :   gcc::jit::recording::context *ctxt = struct_->m_ctxt;
    2741          224 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2742              :   /* LOC can be NULL.  */
    2743          224 :   RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
    2744          224 :   RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
    2745              :                                "field %s has not been placed in a struct",
    2746              :                                field->get_debug_string ());
    2747          224 :   gcc::jit::recording::type *underlying_type =
    2748          224 :     struct_->get_type ();
    2749          224 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2750              :     (field->get_container ()->unqualified ()
    2751              :      == underlying_type->unqualified ()),
    2752              :     struct_->m_ctxt, loc,
    2753              :     "%s is not a field of %s",
    2754              :     field->get_debug_string (),
    2755              :     underlying_type->get_debug_string ());
    2756              : 
    2757          219 :   return (gcc_jit_lvalue *)struct_->access_field (loc, field);
    2758          224 : }
    2759              : 
    2760              : /* Public entrypoint.  See description in libgccjit.h.
    2761              : 
    2762              :    After error-checking, the real work is done by the
    2763              :    gcc::jit::recording::rvalue::access_field method in
    2764              :    jit-recording.cc.  */
    2765              : 
    2766              : gcc_jit_rvalue *
    2767          144 : gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
    2768              :                              gcc_jit_location *loc,
    2769              :                              gcc_jit_field *field)
    2770              : {
    2771          144 :   RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
    2772          144 :   gcc::jit::recording::context *ctxt = struct_->m_ctxt;
    2773          144 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2774              :   /* LOC can be NULL.  */
    2775          144 :   RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
    2776          144 :   RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
    2777              :                                "field %s has not been placed in a struct",
    2778              :                                field->get_debug_string ());
    2779          144 :   gcc::jit::recording::type *underlying_type =
    2780          144 :     struct_->get_type ();
    2781          144 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2782              :     (field->get_container ()->unqualified ()
    2783              :      == underlying_type->unqualified ()),
    2784              :     struct_->m_ctxt, loc,
    2785              :     "%s is not a field of %s",
    2786              :     field->get_debug_string (),
    2787              :     underlying_type->get_debug_string ());
    2788              : 
    2789          139 :   return (gcc_jit_rvalue *)struct_->access_field (loc, field);
    2790          144 : }
    2791              : 
    2792              : /* Public entrypoint.  See description in libgccjit.h.
    2793              : 
    2794              :    After error-checking, the real work is done by the
    2795              :    gcc::jit::recording::rvalue::deference_field method in
    2796              :    jit-recording.cc.  */
    2797              : 
    2798              : gcc_jit_lvalue *
    2799         1551 : gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
    2800              :                                   gcc_jit_location *loc,
    2801              :                                   gcc_jit_field *field)
    2802              : {
    2803         1551 :   RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
    2804         1551 :   JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
    2805              :   /* LOC can be NULL.  */
    2806         1551 :   RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
    2807         1551 :   gcc::jit::recording::type *underlying_type =
    2808         1551 :     ptr->get_type ()->is_pointer ();
    2809         1551 :   RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
    2810              :                                "field %s has not been placed in a struct",
    2811              :                                field->get_debug_string ());
    2812         1551 :   RETURN_NULL_IF_FAIL_PRINTF3 (
    2813              :     underlying_type,
    2814              :     ptr->m_ctxt, loc,
    2815              :     "dereference of non-pointer %s (type: %s) when accessing ->%s",
    2816              :     ptr->get_debug_string (),
    2817              :     ptr->get_type ()->get_debug_string (),
    2818              :     field->get_debug_string ());
    2819         1541 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2820              :     (field->get_container ()->unqualified ()
    2821              :      == underlying_type->unqualified ()),
    2822              :     ptr->m_ctxt, loc,
    2823              :     "%s is not a field of %s",
    2824              :     field->get_debug_string (),
    2825              :     underlying_type->get_debug_string ());
    2826              : 
    2827         1531 :   return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
    2828         1551 : }
    2829              : 
    2830              : /* Public entrypoint.  See description in libgccjit.h.
    2831              : 
    2832              :    After error-checking, the real work is done by the
    2833              :    gcc::jit::recording::rvalue::deference method in
    2834              :    jit-recording.cc.  */
    2835              : 
    2836              : gcc_jit_lvalue *
    2837          694 : gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
    2838              :                             gcc_jit_location *loc)
    2839              : {
    2840          694 :   RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
    2841          694 :   JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
    2842              :   /* LOC can be NULL.  */
    2843              : 
    2844          694 :   gcc::jit::recording::type *underlying_type =
    2845          694 :     rvalue->get_type ()->is_pointer ();
    2846              : 
    2847          694 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2848              :     underlying_type,
    2849              :     rvalue->m_ctxt, loc,
    2850              :     "dereference of non-pointer %s (type: %s)",
    2851              :     rvalue->get_debug_string (),
    2852              :     rvalue->get_type ()->get_debug_string ());
    2853              : 
    2854          689 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2855              :     !underlying_type->is_void (),
    2856              :     rvalue->m_ctxt, loc,
    2857              :     "dereference of void pointer %s (type: %s)",
    2858              :     rvalue->get_debug_string (),
    2859              :     rvalue->get_type ()->get_debug_string ());
    2860              : 
    2861          684 :   return (gcc_jit_lvalue *)rvalue->dereference (loc);
    2862          694 : }
    2863              : 
    2864              : /* Public entrypoint.  See description in libgccjit.h.
    2865              : 
    2866              :    After error-checking, the real work is done by the
    2867              :    gcc::jit::recording::lvalue::get_address method in jit-recording.cc.  */
    2868              : 
    2869              : gcc_jit_rvalue *
    2870          508 : gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
    2871              :                             gcc_jit_location *loc)
    2872              : {
    2873          508 :   RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
    2874          508 :   JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
    2875              :   /* LOC can be NULL.  */
    2876              : 
    2877          508 :   return (gcc_jit_rvalue *)lvalue->get_address (loc);
    2878          508 : }
    2879              : 
    2880              : /* Public entrypoint.  See description in libgccjit.h.
    2881              : 
    2882              :    After error-checking, the real work is done by the
    2883              :    gcc::jit::recording::block::get_address method in
    2884              :    jit-recording.cc.  */
    2885              : 
    2886              : gcc_jit_rvalue *
    2887           55 : gcc_jit_block_get_address (gcc_jit_block *block,
    2888              :                            gcc_jit_location *loc)
    2889              : {
    2890           55 :   RETURN_NULL_IF_FAIL (block, NULL, loc, "NULL block");
    2891           55 :   JIT_LOG_FUNC (block->get_context ()->get_logger ());
    2892              :   /* LOC can be NULL.  */
    2893              : 
    2894           55 :   return (gcc_jit_rvalue *)block->get_address (loc);
    2895           55 : }
    2896              : 
    2897              : /* Public entrypoint.  See description in libgccjit.h.
    2898              : 
    2899              :    After error-checking, the real work is done by the
    2900              :    gcc::jit::recording::lvalue::set_tls_model method in jit-recording.cc.  */
    2901              : 
    2902              : void
    2903           15 : gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,
    2904              :                             enum gcc_jit_tls_model model)
    2905              : {
    2906           15 :   RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
    2907           15 :   JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
    2908           15 :   RETURN_IF_FAIL_PRINTF1 (lvalue->is_global (), lvalue->get_context (), NULL,
    2909              :                                "lvalue \"%s\" not a global",
    2910              :                                lvalue->get_debug_string ());
    2911              : 
    2912           15 :   lvalue->set_tls_model (model);
    2913           15 : }
    2914              : 
    2915              : /* Public entrypoint.  See description in libgccjit.h.
    2916              : 
    2917              :    After error-checking, the real work is done by the
    2918              :    gcc::jit::recording::lvalue::set_link_section method in jit-recording.cc.  */
    2919              : 
    2920              : void
    2921            5 : gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,
    2922              :                             const char *section_name)
    2923              : {
    2924            5 :   RETURN_IF_FAIL (section_name, NULL, NULL, "NULL section_name");
    2925            5 :   lvalue->set_link_section (section_name);
    2926              : }
    2927              : 
    2928              : /* Public entrypoint.  See description in libgccjit.h.
    2929              : 
    2930              :    After error-checking, the real work is done by the
    2931              :    gcc::jit::recording::lvalue::get_alignment method in jit-recording.cc.  */
    2932              : 
    2933              : unsigned
    2934           10 : gcc_jit_lvalue_get_alignment (gcc_jit_lvalue *lvalue)
    2935              : {
    2936           10 :   RETURN_VAL_IF_FAIL (lvalue, 0, NULL, NULL, "NULL lvalue");
    2937           10 :   return lvalue->get_alignment ();
    2938              : }
    2939              : 
    2940              : /* Public entrypoint.  See description in libgccjit.h.
    2941              : 
    2942              :    After error-checking, the real work is done by the
    2943              :    gcc::jit::recording::lvalue::set_alignment method in jit-recording.cc.  */
    2944              : 
    2945              : void
    2946           10 : gcc_jit_lvalue_set_alignment (gcc_jit_lvalue *lvalue,
    2947              :                               unsigned bytes)
    2948              : {
    2949           10 :   RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
    2950           10 :   RETURN_IF_FAIL ((bytes & (bytes - 1)) == 0, NULL, NULL,
    2951              :                   "alignment is not a power of 2");
    2952           10 :   lvalue->set_alignment (bytes);
    2953              : }
    2954              : 
    2955              : /* Public entrypoint.  See description in libgccjit.h.
    2956              : 
    2957              :    After error-checking, the real work is done by the
    2958              :    gcc::jit::recording::lvalue::set_register_name method in jit-recording.cc.  */
    2959              : 
    2960              : void
    2961           20 : gcc_jit_lvalue_set_register_name (gcc_jit_lvalue *lvalue,
    2962              :                                   const char *reg_name)
    2963              : {
    2964           20 :   RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
    2965           20 :   RETURN_IF_FAIL (reg_name, NULL, NULL, "NULL reg_name");
    2966           20 :   lvalue->set_register_name (reg_name);
    2967              : }
    2968              : 
    2969              : /* Public entrypoint.  See description in libgccjit.h.
    2970              : 
    2971              :    After error-checking, the real work is done by the
    2972              :    gcc::jit::recording::function::new_local method in jit-recording.cc.  */
    2973              : 
    2974              : gcc_jit_lvalue *
    2975         5086 : gcc_jit_function_new_local (gcc_jit_function *func,
    2976              :                             gcc_jit_location *loc,
    2977              :                             gcc_jit_type *type,
    2978              :                             const char *name)
    2979              : {
    2980         5086 :   RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
    2981         4156 :   gcc::jit::recording::context *ctxt = func->m_ctxt;
    2982         4156 :   JIT_LOG_FUNC (ctxt->get_logger ());
    2983              :   /* LOC can be NULL.  */
    2984         4156 :   RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
    2985              :                        ctxt, loc,
    2986              :                        "Cannot add locals to an imported function");
    2987         4156 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    2988         4092 :   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
    2989         4092 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    2990              :     type->has_known_size (),
    2991              :     ctxt, loc,
    2992              :     "unknown size for local \"%s\" (type: %s)",
    2993              :     name,
    2994              :     type->get_debug_string ());
    2995         4087 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    2996              :     !type->is_void (),
    2997              :     ctxt, loc,
    2998              :     "void type for local \"%s\"",
    2999              :     name);
    3000              : 
    3001         3990 :   return (gcc_jit_lvalue *)func->new_local (loc, type, name);
    3002         4156 : }
    3003              : 
    3004              : /* Public entrypoint.  See description in libgccjit.h.
    3005              : 
    3006              :    After error-checking, the real work is done by the
    3007              :    gcc::jit::recording::function::new_temp method in jit-recording.cc.  */
    3008              : 
    3009              : gcc_jit_lvalue *
    3010            5 : gcc_jit_function_new_temp (gcc_jit_function *func,
    3011              :                            gcc_jit_location *loc,
    3012              :                            gcc_jit_type *type)
    3013              : {
    3014            5 :   RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
    3015            5 :   gcc::jit::recording::context *ctxt = func->m_ctxt;
    3016            5 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3017              :   /* LOC can be NULL.  */
    3018            5 :   RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
    3019              :                        ctxt, loc,
    3020              :                        "Cannot add temps to an imported function");
    3021            5 :   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
    3022            5 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    3023              :     type->has_known_size (),
    3024              :     ctxt, loc,
    3025              :     "unknown size for temp (type: %s)",
    3026              :     type->get_debug_string ());
    3027            5 :   RETURN_NULL_IF_FAIL (
    3028              :     !type->is_void (),
    3029              :     ctxt, loc,
    3030              :     "void type for temp");
    3031              : 
    3032            5 :   return (gcc_jit_lvalue *)func->new_temp (loc, type);
    3033            5 : }
    3034              : 
    3035              : /* Public entrypoint.  See description in libgccjit.h.
    3036              : 
    3037              :    After error-checking, the real work is done by the
    3038              :    gcc::jit::recording::block::add_eval method in jit-recording.cc.  */
    3039              : 
    3040              : void
    3041         3317 : gcc_jit_block_add_eval (gcc_jit_block *block,
    3042              :                         gcc_jit_location *loc,
    3043              :                         gcc_jit_rvalue *rvalue)
    3044              : {
    3045         3561 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3046         2271 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3047         2271 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3048              :   /* LOC can be NULL.  */
    3049         2271 :   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
    3050              : 
    3051         2027 :   gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
    3052              : 
    3053              :   /* "stmt" should be good enough to be usable in error-messages,
    3054              :      but might still not be compilable; perform some more
    3055              :      error-checking here.  We do this here so that the error messages
    3056              :      can contain a stringified version of "stmt", whilst appearing
    3057              :      as close as possible to the point of failure.  */
    3058         2027 :   rvalue->verify_valid_within_stmt (__func__, stmt);
    3059         2271 : }
    3060              : 
    3061              : /* Public entrypoint.  See description in libgccjit.h.
    3062              : 
    3063              :    After error-checking, the real work is done by the
    3064              :    gcc::jit::recording::block::add_assignment method in
    3065              :    jit-recording.cc.  */
    3066              : 
    3067              : void
    3068         7558 : gcc_jit_block_add_assignment (gcc_jit_block *block,
    3069              :                               gcc_jit_location *loc,
    3070              :                               gcc_jit_lvalue *lvalue,
    3071              :                               gcc_jit_rvalue *rvalue)
    3072              : {
    3073         9367 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3074         6512 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3075         6512 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3076              :   /* LOC can be NULL.  */
    3077         6512 :   RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
    3078         6375 :   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
    3079         6154 :   RETURN_IF_FAIL_PRINTF4 (
    3080              :     compatible_types (lvalue->get_type (),
    3081              :                       rvalue->get_type ()),
    3082              :     ctxt, loc,
    3083              :     "mismatching types:"
    3084              :     " assignment to %s (type: %s) from %s (type: %s)",
    3085              :     lvalue->get_debug_string (),
    3086              :     lvalue->get_type ()->get_debug_string (),
    3087              :     rvalue->get_debug_string (),
    3088              :     rvalue->get_type ()->get_debug_string ());
    3089         4708 :   RETURN_IF_FAIL_PRINTF1 (
    3090              :     !lvalue->get_readonly (),
    3091              :     ctxt, loc,
    3092              :     "cannot assign to readonly variable: %s",
    3093              :     lvalue->get_debug_string ());
    3094              : 
    3095         4703 :   gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
    3096              : 
    3097              :   /* "stmt" should be good enough to be usable in error-messages,
    3098              :      but might still not be compilable; perform some more
    3099              :      error-checking here.  We do this here so that the error messages
    3100              :      can contain a stringified version of "stmt", whilst appearing
    3101              :      as close as possible to the point of failure.  */
    3102         4703 :   lvalue->verify_valid_within_stmt (__func__, stmt);
    3103         4703 :   rvalue->verify_valid_within_stmt (__func__, stmt);
    3104         6512 : }
    3105              : 
    3106              : /* Public entrypoint.  See description in libgccjit.h.
    3107              : 
    3108              :    After error-checking, the real work is done by the
    3109              :    gcc::jit::recording::block::add_assignment_op method in
    3110              :    jit-recording.cc.  */
    3111              : 
    3112              : void
    3113          499 : gcc_jit_block_add_assignment_op (gcc_jit_block *block,
    3114              :                                  gcc_jit_location *loc,
    3115              :                                  gcc_jit_lvalue *lvalue,
    3116              :                                  enum gcc_jit_binary_op op,
    3117              :                                  gcc_jit_rvalue *rvalue)
    3118              : {
    3119          514 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3120          499 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3121          499 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3122              :   /* LOC can be NULL.  */
    3123          499 :   RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
    3124          494 :   RETURN_IF_FAIL_PRINTF1 (
    3125              :     valid_binary_op_p (op),
    3126              :     ctxt, loc,
    3127              :     "unrecognized value for enum gcc_jit_binary_op: %i",
    3128              :     op);
    3129          494 :   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
    3130          494 :   RETURN_IF_FAIL_PRINTF4 (
    3131              :     compatible_types (lvalue->get_type (),
    3132              :                       rvalue->get_type ()),
    3133              :     ctxt, loc,
    3134              :     "mismatching types:"
    3135              :     " assignment to %s (type: %s) involving %s (type: %s)",
    3136              :     lvalue->get_debug_string (),
    3137              :     lvalue->get_type ()->get_debug_string (),
    3138              :     rvalue->get_debug_string (),
    3139              :     rvalue->get_type ()->get_debug_string ());
    3140              :   // TODO: check if it is a numeric vector?
    3141          489 :   RETURN_IF_FAIL_PRINTF3 (
    3142              :     lvalue->get_type ()->is_numeric (), ctxt, loc,
    3143              :     "gcc_jit_block_add_assignment_op %s has non-numeric lvalue %s (type: %s)",
    3144              :     gcc::jit::binary_op_reproducer_strings[op],
    3145              :     lvalue->get_debug_string (), lvalue->get_type ()->get_debug_string ());
    3146          484 :   RETURN_IF_FAIL_PRINTF3 (
    3147              :     rvalue->get_type ()->is_numeric (), ctxt, loc,
    3148              :     "gcc_jit_block_add_assignment_op %s has non-numeric rvalue %s (type: %s)",
    3149              :     gcc::jit::binary_op_reproducer_strings[op],
    3150              :     rvalue->get_debug_string (), rvalue->get_type ()->get_debug_string ());
    3151              : 
    3152          484 :   gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
    3153              : 
    3154              :   /* "stmt" should be good enough to be usable in error-messages,
    3155              :      but might still not be compilable; perform some more
    3156              :      error-checking here.  We do this here so that the error messages
    3157              :      can contain a stringified version of "stmt", whilst appearing
    3158              :      as close as possible to the point of failure.  */
    3159          484 :   lvalue->verify_valid_within_stmt (__func__, stmt);
    3160          484 :   rvalue->verify_valid_within_stmt (__func__, stmt);
    3161          499 : }
    3162              : 
    3163              : /* Internal helper function for determining if rvalue BOOLVAL is of
    3164              :    boolean type.  For use by gcc_jit_block_end_with_conditional.  */
    3165              : 
    3166              : static bool
    3167          894 : is_bool (gcc_jit_rvalue *boolval)
    3168              : {
    3169            0 :   gcc::jit::recording::type *actual_type = boolval->get_type ();
    3170          894 :   gcc::jit::recording::type *bool_type =
    3171          894 :     boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
    3172          894 :   return actual_type == bool_type;
    3173              : }
    3174              : 
    3175              : /* Public entrypoint.  See description in libgccjit.h.
    3176              : 
    3177              :    After error-checking, the real work is done by the
    3178              :    gcc::jit::recording::block::end_with_conditional method in
    3179              :    jit-recording.cc.  */
    3180              : 
    3181              : void
    3182          894 : gcc_jit_block_end_with_conditional (gcc_jit_block *block,
    3183              :                                     gcc_jit_location *loc,
    3184              :                                     gcc_jit_rvalue *boolval,
    3185              :                                     gcc_jit_block *on_true,
    3186              :                                     gcc_jit_block *on_false)
    3187              : {
    3188          894 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3189          894 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3190          894 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3191              :   /* LOC can be NULL.  */
    3192          894 :   RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
    3193          894 :   RETURN_IF_FAIL_PRINTF2 (
    3194              :    is_bool (boolval), ctxt, loc,
    3195              :    "%s (type: %s) is not of boolean type ",
    3196              :    boolval->get_debug_string (),
    3197              :    boolval->get_type ()->get_debug_string ());
    3198          894 :   RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
    3199          894 :   RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
    3200          894 :   RETURN_IF_FAIL_PRINTF4 (
    3201              :     block->get_function () == on_true->get_function (),
    3202              :     ctxt, loc,
    3203              :     "\"on_true\" block is not in same function:"
    3204              :     " source block %s is in function %s"
    3205              :     " whereas target block %s is in function %s",
    3206              :     block->get_debug_string (),
    3207              :     block->get_function ()->get_debug_string (),
    3208              :     on_true->get_debug_string (),
    3209              :     on_true->get_function ()->get_debug_string ());
    3210          894 :   RETURN_IF_FAIL_PRINTF4 (
    3211              :     block->get_function () == on_false->get_function (),
    3212              :     ctxt, loc,
    3213              :     "\"on_false\" block is not in same function:"
    3214              :     " source block %s is in function %s"
    3215              :     " whereas target block %s is in function %s",
    3216              :     block->get_debug_string (),
    3217              :     block->get_function ()->get_debug_string (),
    3218              :     on_false->get_debug_string (),
    3219              :     on_false->get_function ()->get_debug_string ());
    3220              : 
    3221          894 :   gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
    3222              : 
    3223              :   /* "stmt" should be good enough to be usable in error-messages,
    3224              :      but might still not be compilable; perform some more
    3225              :      error-checking here.  We do this here so that the error messages
    3226              :      can contain a stringified version of "stmt", whilst appearing
    3227              :      as close as possible to the point of failure.  */
    3228          894 :   boolval->verify_valid_within_stmt (__func__, stmt);
    3229          894 : }
    3230              : 
    3231              : /* Public entrypoint.  See description in libgccjit.h.
    3232              : 
    3233              :    After error-checking, the real work is done by the
    3234              :    gcc::jit::recording::block::add_comment method in
    3235              :    jit-recording.cc.  */
    3236              : 
    3237              : void
    3238          450 : gcc_jit_block_add_comment (gcc_jit_block *block,
    3239              :                            gcc_jit_location *loc,
    3240              :                            const char *text)
    3241              : {
    3242          450 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3243          450 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3244          450 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3245              :   /* LOC can be NULL.  */
    3246          450 :   RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
    3247              : 
    3248          450 :   block->add_comment (loc, text);
    3249          450 : }
    3250              : 
    3251              : /* Public entrypoint.  See description in libgccjit.h.
    3252              : 
    3253              :    After error-checking, the real work is done by the
    3254              :    gcc::jit::recording::block::end_with_jump method in
    3255              :    jit-recording.cc.  */
    3256              : 
    3257              : void
    3258         1207 : gcc_jit_block_end_with_jump (gcc_jit_block *block,
    3259              :                              gcc_jit_location *loc,
    3260              :                              gcc_jit_block *target)
    3261              : {
    3262         1212 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3263         1207 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3264         1207 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3265              :   /* LOC can be NULL.  */
    3266         1207 :   RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
    3267         1207 :   RETURN_IF_FAIL_PRINTF4 (
    3268              :     block->get_function () == target->get_function (),
    3269              :     ctxt, loc,
    3270              :     "target block is not in same function:"
    3271              :     " source block %s is in function %s"
    3272              :     " whereas target block %s is in function %s",
    3273              :     block->get_debug_string (),
    3274              :     block->get_function ()->get_debug_string (),
    3275              :     target->get_debug_string (),
    3276              :     target->get_function ()->get_debug_string ());
    3277              : 
    3278         1202 :   block->end_with_jump (loc, target);
    3279         1207 : }
    3280              : 
    3281              : /* Public entrypoint.  See description in libgccjit.h.
    3282              : 
    3283              :    After error-checking, the real work is done by the
    3284              :    gcc::jit::recording::block::end_with_indirect_jump method in
    3285              :    jit-recording.cc.  */
    3286              : 
    3287              : void
    3288           45 : gcc_jit_block_end_with_indirect_jump (gcc_jit_block *block,
    3289              :                                       gcc_jit_location *loc,
    3290              :                                       gcc_jit_rvalue *target_address,
    3291              :                                       int num_destinations,
    3292              :                                       gcc_jit_block **destinations)
    3293              : {
    3294           70 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3295           45 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3296           45 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3297              :   /* LOC can be NULL.  */
    3298           45 :   RETURN_IF_FAIL (target_address, ctxt, loc, "NULL target_address");
    3299           45 :   RETURN_IF_FAIL_PRINTF1 (
    3300              :     target_address->get_type ()->is_pointer (),
    3301              :     ctxt, loc,
    3302              :     "target_address: %s is not of a pointer type",
    3303              :     target_address->get_debug_string ());
    3304           40 :   RETURN_IF_FAIL (num_destinations >= 0, ctxt, loc, "num_destinations < 0");
    3305           35 :   if (num_destinations > 0)
    3306           35 :     RETURN_IF_FAIL (destinations, ctxt, loc, "NULL destinations");
    3307              : 
    3308           80 :   for (int i = 0; i < num_destinations; i++)
    3309              :     {
    3310           60 :       RETURN_IF_FAIL_PRINTF1 (destinations[i], ctxt, loc,
    3311              :                               "NULL destination block at index %d", i);
    3312           55 :       RETURN_IF_FAIL_PRINTF4 (
    3313              :         block->get_function () == destinations[i]->get_function (),
    3314              :         ctxt, loc,
    3315              :         "destination block %d (%s) is not in the same function"
    3316              :         " (function %s) as source block %s",
    3317              :         i,
    3318              :         destinations[i]->get_debug_string (),
    3319              :         destinations[i]->get_function ()->get_debug_string (),
    3320              :         block->get_debug_string ());
    3321              :     }
    3322              : 
    3323           20 :   gcc::jit::recording::statement *stmt =
    3324           20 :     block->end_with_indirect_jump (loc, target_address,
    3325              :                                    num_destinations,
    3326              :                                    (gcc::jit::recording::block **)destinations);
    3327              : 
    3328              :   /* target_address must be valid to use within STMT.  */
    3329           20 :   target_address->verify_valid_within_stmt (__func__, stmt);
    3330           45 : }
    3331              : 
    3332              : /* Public entrypoint.  See description in libgccjit.h.
    3333              : 
    3334              :    After error-checking, the real work is done by the
    3335              :    gcc::jit::recording::block::end_with_return method in
    3336              :    jit-recording.cc.  */
    3337              : 
    3338              : void
    3339         4234 : gcc_jit_block_end_with_return (gcc_jit_block *block,
    3340              :                                gcc_jit_location *loc,
    3341              :                                gcc_jit_rvalue *rvalue)
    3342              : {
    3343         4607 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3344         3752 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3345         3752 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3346              :   /* LOC can be NULL.  */
    3347         3752 :   gcc::jit::recording::function *func = block->get_function ();
    3348         3752 :   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
    3349         3696 :   RETURN_IF_FAIL_PRINTF4 (
    3350              :     compatible_types (
    3351              :       func->get_return_type (),
    3352              :       rvalue->get_type ()),
    3353              :     ctxt, loc,
    3354              :     "mismatching types:"
    3355              :     " return of %s (type: %s) in function %s (return type: %s)",
    3356              :     rvalue->get_debug_string (),
    3357              :     rvalue->get_type ()->get_debug_string (),
    3358              :     func->get_debug_string (),
    3359              :     func->get_return_type ()->get_debug_string ());
    3360              : 
    3361         3379 :   gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
    3362              : 
    3363              :   /* "stmt" should be good enough to be usable in error-messages,
    3364              :      but might still not be compilable; perform some more
    3365              :      error-checking here.  We do this here so that the error messages
    3366              :      can contain a stringified version of "stmt", whilst appearing
    3367              :      as close as possible to the point of failure.  */
    3368         3379 :   rvalue->verify_valid_within_stmt (__func__, stmt);
    3369         3752 : }
    3370              : 
    3371              : /* Public entrypoint.  See description in libgccjit.h.
    3372              : 
    3373              :    After error-checking, the real work is done by the
    3374              :    gcc::jit::recording::block::end_with_return method in
    3375              :    jit-recording.cc.  */
    3376              : 
    3377              : void
    3378          656 : gcc_jit_block_end_with_void_return (gcc_jit_block *block,
    3379              :                                     gcc_jit_location *loc)
    3380              : {
    3381          656 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3382          651 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3383          651 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3384              :   /* LOC can be NULL.  */
    3385          651 :   gcc::jit::recording::function *func = block->get_function ();
    3386          651 :   RETURN_IF_FAIL_PRINTF2 (
    3387              :     func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
    3388              :     ctxt, loc,
    3389              :     "mismatching types:"
    3390              :     " void return in function %s (return type: %s)",
    3391              :     func->get_debug_string (),
    3392              :     func->get_return_type ()->get_debug_string ());
    3393              : 
    3394          651 :   block->end_with_return (loc, NULL);
    3395          651 : }
    3396              : 
    3397              : /* Public entrypoint.  See description in libgccjit.h.
    3398              : 
    3399              :    After error-checking, the real work is done by the
    3400              :    gcc::jit::recording::context::new_case method in
    3401              :    jit-recording.cc.  */
    3402              : 
    3403              : gcc_jit_case *
    3404          110 : gcc_jit_context_new_case (gcc_jit_context *ctxt,
    3405              :                           gcc_jit_rvalue *min_value,
    3406              :                           gcc_jit_rvalue *max_value,
    3407              :                           gcc_jit_block *block)
    3408              : {
    3409          110 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3410          110 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3411          110 :   RETURN_NULL_IF_FAIL (min_value, ctxt, NULL, "NULL min_value");
    3412          110 :   RETURN_NULL_IF_FAIL (max_value, ctxt, NULL, "NULL max_value");
    3413          110 :   RETURN_NULL_IF_FAIL (block, ctxt, NULL, "NULL block");
    3414              : 
    3415          110 :   RETURN_NULL_IF_FAIL_PRINTF1 (min_value->is_constant (), ctxt, NULL,
    3416              :                                "min_value is not a constant: %s",
    3417              :                                min_value->get_debug_string ());
    3418          105 :   RETURN_NULL_IF_FAIL_PRINTF1 (max_value->is_constant (), ctxt, NULL,
    3419              :                                "max_value is not a constant: %s",
    3420              :                                max_value->get_debug_string ());
    3421          105 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    3422              :     min_value->get_type ()->is_int (),
    3423              :     ctxt, NULL,
    3424              :     "min_value: %s (type: %s) is not of integer type",
    3425              :     min_value->get_debug_string (),
    3426              :     min_value->get_type ()->get_debug_string ());
    3427          100 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    3428              :     max_value->get_type ()->is_int (),
    3429              :     ctxt, NULL,
    3430              :     "max_value: %s (type: %s) is not of integer type",
    3431              :     max_value->get_debug_string (),
    3432              :     max_value->get_type ()->get_debug_string ());
    3433              : 
    3434          100 :   wide_int wi_min, wi_max;
    3435          100 :   if (!min_value->get_wide_int (&wi_min))
    3436            0 :     gcc_unreachable ();
    3437          100 :   if (!max_value->get_wide_int (&wi_max))
    3438            0 :     gcc_unreachable ();
    3439          100 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    3440              :     wi::les_p (wi_min, wi_max),
    3441              :     ctxt, NULL,
    3442              :     "min_value: %s > max_value: %s",
    3443              :     min_value->get_debug_string (),
    3444              :     max_value->get_debug_string ());
    3445           95 :   return (gcc_jit_case *)ctxt->new_case (min_value,
    3446              :                                          max_value,
    3447           95 :                                          block);
    3448          110 : }
    3449              : 
    3450              : /* Public entrypoint.  See description in libgccjit.h.
    3451              : 
    3452              :    After error-checking, this calls the trivial
    3453              :    gcc::jit::recording::memento::as_object method (a case is a
    3454              :    memento), in jit-recording.h.  */
    3455              : 
    3456              : gcc_jit_object *
    3457           20 : gcc_jit_case_as_object (gcc_jit_case *case_)
    3458              : {
    3459           20 :   RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case");
    3460              : 
    3461           20 :   return static_cast <gcc_jit_object *> (case_->as_object ());
    3462              : }
    3463              : 
    3464              : /* Helper function for gcc_jit_block_end_with_switch and
    3465              :    valid_case_for_switch.  */
    3466              : 
    3467              : static bool
    3468          145 : valid_dest_for_switch (gcc::jit::recording::context *ctxt,
    3469              :                        gcc_jit_location *loc,
    3470              :                        const char *api_funcname,
    3471              :                        gcc::jit::recording::block *switch_block,
    3472              :                        gcc::jit::recording::block *dest_block,
    3473              :                        const char *dest_block_desc)
    3474              : {
    3475          145 :   if (!dest_block)
    3476              :     {
    3477            0 :       jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc);
    3478            0 :       return false;
    3479              :     }
    3480          145 :   gcc::jit::recording::function *switch_fn = switch_block->get_function ();
    3481          145 :   gcc::jit::recording::function *dest_fn = dest_block->get_function ();
    3482          145 :   if (switch_fn != dest_fn)
    3483              :     {
    3484            0 :       jit_error (ctxt, loc,
    3485              :                  "%s: %s is not in same function:"
    3486              :                  " switch block %s is in function %s"
    3487              :                  " whereas %s %s is in function %s",
    3488              :                  api_funcname,
    3489              :                  dest_block_desc,
    3490              :                  switch_block->get_debug_string (),
    3491              :                  switch_fn->get_debug_string (),
    3492              :                  dest_block_desc,
    3493              :                  dest_block->get_debug_string (),
    3494              :                  dest_fn->get_debug_string ());
    3495            0 :       return false;
    3496              :     }
    3497              :   return true;
    3498              : }
    3499              : 
    3500              : /* Helper function for gcc_jit_block_end_with_switch.  */
    3501              : 
    3502              : static bool
    3503          115 : valid_case_for_switch (gcc::jit::recording::context *ctxt,
    3504              :                        gcc_jit_location *loc,
    3505              :                        const char *api_funcname,
    3506              :                        gcc_jit_block *switch_block,
    3507              :                        gcc_jit_rvalue *expr,
    3508              :                        gcc_jit_case *case_,
    3509              :                        const char *case_desc,
    3510              :                        int case_idx)
    3511              : {
    3512          115 :   if (!case_)
    3513              :     {
    3514           20 :       jit_error (ctxt, loc,
    3515              :                  "%s:"
    3516              :                  " NULL case %i",
    3517              :                  api_funcname,
    3518              :                  case_idx);
    3519           20 :       return false;
    3520              :     }
    3521           95 :   if (!valid_dest_for_switch (ctxt, loc,
    3522              :                               api_funcname,
    3523              :                               switch_block,
    3524              :                               case_->get_dest_block (),
    3525              :                               case_desc))
    3526              :     return false;
    3527           95 :   gcc::jit::recording::type *expr_type = expr->get_type ();
    3528           95 :   if (expr_type != case_->get_min_value ()->get_type ())
    3529              :     {
    3530            5 :       jit_error (ctxt, loc,
    3531              :                  "%s:"
    3532              :                  " mismatching types between case and expression:"
    3533              :                  " cases[%i]->min_value: %s (type: %s)"
    3534              :                  " expr: %s (type: %s)",
    3535              :                  api_funcname,
    3536              :                  case_idx,
    3537            5 :                  case_->get_min_value ()->get_debug_string (),
    3538            5 :                  case_->get_min_value ()->get_type ()->get_debug_string (),
    3539              :                  expr->get_debug_string (),
    3540              :                  expr_type->get_debug_string ());
    3541            5 :       return false;
    3542              :     }
    3543           90 :   if (expr_type != case_->get_max_value ()->get_type ())
    3544              :     {
    3545            0 :       jit_error (ctxt, loc,
    3546              :                  "%s:"
    3547              :                  " mismatching types between case and expression:"
    3548              :                  " cases[%i]->max_value: %s (type: %s)"
    3549              :                  " expr: %s (type: %s)",
    3550              :                  api_funcname,
    3551              :                  case_idx,
    3552            0 :                  case_->get_max_value ()->get_debug_string (),
    3553            0 :                  case_->get_max_value ()->get_type ()->get_debug_string (),
    3554              :                  expr->get_debug_string (),
    3555              :                  expr_type->get_debug_string ());
    3556            0 :       return false;
    3557              :     }
    3558              :   return true;
    3559              : }
    3560              : 
    3561              : /* A class for holding the data we need to perform error-checking
    3562              :    on a libgccjit API call.  */
    3563              : 
    3564              : class api_call_validator
    3565              : {
    3566              :  public:
    3567           50 :   api_call_validator (gcc::jit::recording::context *ctxt,
    3568              :                       gcc_jit_location *loc,
    3569              :                       const char *funcname)
    3570           50 :   : m_ctxt (ctxt),
    3571           50 :     m_loc (loc),
    3572           50 :     m_funcname (funcname)
    3573              :   {}
    3574              : 
    3575              :  protected:
    3576              :   gcc::jit::recording::context *m_ctxt;
    3577              :   gcc_jit_location *m_loc;
    3578              :   const char *m_funcname;
    3579              : };
    3580              : 
    3581              : /* A class for verifying that the ranges of cases within
    3582              :    gcc_jit_block_end_with_switch don't overlap.  */
    3583              : 
    3584          100 : class case_range_validator : public api_call_validator
    3585              : {
    3586              :  public:
    3587              :   case_range_validator (gcc::jit::recording::context *ctxt,
    3588              :                         gcc_jit_location *loc,
    3589              :                         const char *funcname);
    3590              : 
    3591              :   bool
    3592              :   validate (gcc_jit_case *case_, int idx);
    3593              : 
    3594              :  private:
    3595              :   static int
    3596              :   case_compare (gcc::jit::recording::rvalue *k1,
    3597              :                 gcc::jit::recording::rvalue *k2);
    3598              : 
    3599              :   static wide_int
    3600              :   get_wide_int (gcc::jit::recording::rvalue *k);
    3601              : 
    3602              :  private:
    3603              :   typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases;
    3604              : };
    3605              : 
    3606              : /* case_range_validator's ctor.  */
    3607              : 
    3608           50 : case_range_validator::case_range_validator (gcc::jit::recording::context *ctxt,
    3609              :                                             gcc_jit_location *loc,
    3610              :                                             const char *funcname)
    3611              : : api_call_validator (ctxt, loc, funcname),
    3612           50 :   m_cases (case_compare, NULL, NULL)
    3613              : {
    3614           50 : }
    3615              : 
    3616              : /* Ensure that the range of CASE_ does not overlap with any of the
    3617              :    ranges of cases we've already seen.
    3618              :    Return true if everything is OK.
    3619              :    Return false and emit an error if there is an overlap.
    3620              :    Compare with c-family/c-common.cc:c_add_case_label.  */
    3621              : 
    3622              : bool
    3623           90 : case_range_validator::validate (gcc_jit_case *case_,
    3624              :                                 int case_idx)
    3625              : {
    3626              :   /* Look up the LOW_VALUE in the table of case labels we already
    3627              :      have.  */
    3628           90 :   gcc_jit_case *other = m_cases.lookup (case_->get_min_value ());
    3629              : 
    3630              :   /* If there was not an exact match, check for overlapping ranges.  */
    3631            0 :   if (!other)
    3632              :     {
    3633           90 :       gcc_jit_case *pred;
    3634           90 :       gcc_jit_case *succ;
    3635              : 
    3636              :       /* Even though there wasn't an exact match, there might be an
    3637              :          overlap between this case range and another case range.
    3638              :          Since we've (inductively) not allowed any overlapping case
    3639              :          ranges, we simply need to find the greatest low case label
    3640              :          that is smaller that CASE_MIN_VALUE, and the smallest low case
    3641              :          label that is greater than CASE_MAX_VALUE.  If there is an overlap
    3642              :          it will occur in one of these two ranges.  */
    3643           90 :       pred = m_cases.predecessor (case_->get_min_value ());
    3644           90 :       succ = m_cases.successor (case_->get_max_value ());
    3645              : 
    3646              :       /* Check to see if the PRED overlaps.  It is smaller than
    3647              :          the LOW_VALUE, so we only need to check its max value.  */
    3648           90 :       if (pred)
    3649              :         {
    3650           45 :           wide_int wi_case_min = get_wide_int (case_->get_min_value ());
    3651           45 :           wide_int wi_pred_max = get_wide_int (pred->get_max_value ());
    3652           45 :           if (wi::ges_p (wi_pred_max, wi_case_min))
    3653            5 :             other = pred;
    3654           45 :         }
    3655              : 
    3656           90 :       if (!other && succ)
    3657              :         {
    3658              :           /* Check to see if the SUCC overlaps.  The low end of that
    3659              :              range is bigger than the low end of the current range.  */
    3660           20 :           wide_int wi_case_max = get_wide_int (case_->get_max_value ());
    3661           20 :           wide_int wi_succ_min = get_wide_int (succ->get_min_value ());
    3662           20 :           if (wi::les_p (wi_succ_min, wi_case_max))
    3663            0 :             other = succ;
    3664           20 :         }
    3665              :     }
    3666              : 
    3667              :   /* If there was an overlap, issue an error.  */
    3668           90 :   if (other)
    3669              :     {
    3670            5 :       jit_error (m_ctxt, m_loc,
    3671              :                  "%s: duplicate (or overlapping) cases values:"
    3672              :                  " case %i: %s overlaps %s",
    3673              :                  m_funcname,
    3674              :                  case_idx,
    3675              :                  case_->get_debug_string (),
    3676              :                  other->get_debug_string ());
    3677            5 :       return false;
    3678              :     }
    3679              : 
    3680              :   /* Register this case label in the splay tree.  */
    3681           85 :   m_cases.insert (case_->get_min_value (),
    3682              :                   case_);
    3683           85 :   return true;
    3684              : }
    3685              : 
    3686              : /* Compare with c-family/c-common.cc:case_compare, which acts on tree
    3687              :    nodes, rather than rvalue *.
    3688              : 
    3689              :    Comparator for case label values.  K1 and K2 must be constant integer
    3690              :    values (anything else should have been rejected by
    3691              :    gcc_jit_context_new_case.
    3692              : 
    3693              :    Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
    3694              :    K2, and 0 if K1 and K2 are equal.  */
    3695              : 
    3696              : int
    3697          570 : case_range_validator::case_compare (gcc::jit::recording::rvalue * k1,
    3698              :                                     gcc::jit::recording::rvalue * k2)
    3699              : {
    3700          570 :   wide_int wi1 = get_wide_int (k1);
    3701          570 :   wide_int wi2 = get_wide_int (k2);
    3702          570 :   return wi::cmps(wi1, wi2);
    3703          570 : }
    3704              : 
    3705              : /* Given a const int rvalue K, get the underlying value as a wide_int.  */
    3706              : 
    3707              : wide_int
    3708         1270 : case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k)
    3709              : {
    3710         1270 :   wide_int wi;
    3711         1270 :   bool got_wi = k->get_wide_int (&wi);
    3712         1270 :   gcc_assert (got_wi);
    3713         1270 :   return wi;
    3714              : }
    3715              : 
    3716              : /* Public entrypoint.  See description in libgccjit.h.
    3717              : 
    3718              :    After error-checking, the real work is done by the
    3719              :    gcc::jit::recording::block::end_with_switch method in
    3720              :    jit-recording.cc.  */
    3721              : 
    3722              : void
    3723           50 : gcc_jit_block_end_with_switch (gcc_jit_block *block,
    3724              :                                gcc_jit_location *loc,
    3725              :                                gcc_jit_rvalue *expr,
    3726              :                                gcc_jit_block *default_block,
    3727              :                                int num_cases,
    3728              :                                gcc_jit_case **cases)
    3729              : {
    3730           80 :   RETURN_IF_NOT_VALID_BLOCK (block, loc);
    3731           50 :   gcc::jit::recording::context *ctxt = block->get_context ();
    3732           50 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3733              :   /* LOC can be NULL.  */
    3734           50 :   RETURN_IF_FAIL (expr, ctxt, loc,
    3735              :                   "NULL expr");
    3736           50 :   gcc::jit::recording::type *expr_type = expr->get_type ();
    3737           50 :   RETURN_IF_FAIL_PRINTF2 (
    3738              :     expr_type->is_int (),
    3739              :     ctxt, loc,
    3740              :     "expr: %s (type: %s) is not of integer type",
    3741              :     expr->get_debug_string (),
    3742              :     expr_type->get_debug_string ());
    3743           50 :   if (!valid_dest_for_switch (ctxt, loc,
    3744              :                               __func__,
    3745              :                               block,
    3746              :                               default_block,
    3747              :                               "default_block"))
    3748              :     return;
    3749           50 :   RETURN_IF_FAIL (num_cases >= 0, ctxt, loc, "num_cases < 0");
    3750           50 :   case_range_validator crv (ctxt, loc, __func__);
    3751          185 :   for (int i = 0; i < num_cases; i++)
    3752              :     {
    3753          115 :       char case_desc[32];
    3754          115 :       snprintf (case_desc, sizeof (case_desc),
    3755              :                 "cases[%i]", i);
    3756          115 :       if (!valid_case_for_switch (ctxt, loc,
    3757              :                                   __func__,
    3758              :                                   block,
    3759              :                                   expr,
    3760          115 :                                   cases[i],
    3761              :                                   case_desc,
    3762              :                                   i))
    3763           30 :         return;
    3764           90 :       if (!crv.validate (cases[i], i))
    3765              :         return;
    3766              :     }
    3767              : 
    3768           20 :   block->end_with_switch (loc, expr, default_block,
    3769              :                           num_cases,
    3770              :                           (gcc::jit::recording::case_ **)cases);
    3771           50 : }
    3772              : 
    3773              : /**********************************************************************
    3774              :  Option-management
    3775              :  **********************************************************************/
    3776              : 
    3777              : /* Public entrypoint.  See description in libgccjit.h.
    3778              : 
    3779              :    After error-checking, the real work is done by the
    3780              :    gcc::jit::recording::context::set_str_option method in
    3781              :    jit-recording.cc.  */
    3782              : 
    3783              : void
    3784         1787 : gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
    3785              :                                 enum gcc_jit_str_option opt,
    3786              :                                 const char *value)
    3787              : {
    3788         1787 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3789         1787 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3790              :   /* opt is checked by the inner function.
    3791              :      value can be NULL.  */
    3792              : 
    3793         1786 :   ctxt->set_str_option (opt, value);
    3794         1787 : }
    3795              : 
    3796              : /* Public entrypoint.  See description in libgccjit.h.
    3797              : 
    3798              :    After error-checking, the real work is done by the
    3799              :    gcc::jit::recording::context::set_int_option method in
    3800              :    jit-recording.cc.  */
    3801              : 
    3802              : void
    3803         1832 : gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
    3804              :                                 enum gcc_jit_int_option opt,
    3805              :                                 int value)
    3806              : {
    3807         1832 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3808         1832 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3809              :   /* opt is checked by the inner function.  */
    3810              : 
    3811         1832 :   ctxt->set_int_option (opt, value);
    3812         1832 : }
    3813              : 
    3814              : /* Public entrypoint.  See description in libgccjit.h.
    3815              : 
    3816              :    After error-checking, the real work is done by the
    3817              :    gcc::jit::recording::context::set_bool_option method in
    3818              :    jit-recording.cc.  */
    3819              : 
    3820              : void
    3821         7717 : gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
    3822              :                                  enum gcc_jit_bool_option opt,
    3823              :                                  int value)
    3824              : {
    3825         7717 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3826         7717 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3827              :   /* opt is checked by the inner function.  */
    3828              : 
    3829         7717 :   ctxt->set_bool_option (opt, value);
    3830         7715 : }
    3831              : 
    3832              : /* Public entrypoint.  See description in libgccjit.h.
    3833              : 
    3834              :    After error-checking, the real work is done by the
    3835              :    gcc::jit::recording::context::set_inner_bool_option method in
    3836              :    jit-recording.cc.  */
    3837              : 
    3838              : void
    3839            5 : gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
    3840              :                                                    int bool_value)
    3841              : {
    3842            5 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3843            5 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3844            5 :   ctxt->set_inner_bool_option (
    3845              :     gcc::jit::INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
    3846              :     bool_value);
    3847            5 : }
    3848              : 
    3849              : /* Public entrypoint.  See description in libgccjit.h.
    3850              : 
    3851              :    After error-checking, the real work is done by the
    3852              :    gcc::jit::recording::context::set_inner_bool_option method in
    3853              :    jit-recording.cc.  */
    3854              : 
    3855              : void
    3856            0 : gcc_jit_context_set_bool_print_errors_to_stderr (gcc_jit_context *ctxt,
    3857              :                                                  int enabled)
    3858              : {
    3859            0 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3860            0 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3861            0 :   ctxt->set_inner_bool_option (
    3862              :     gcc::jit::INNER_BOOL_OPTION_PRINT_ERRORS_TO_STDERR,
    3863              :     enabled);
    3864            0 : }
    3865              : 
    3866              : /* Public entrypoint.  See description in libgccjit.h.
    3867              : 
    3868              :    After error-checking, the real work is done by the
    3869              :    gcc::jit::recording::context::set_inner_bool_option method in
    3870              :    jit-recording.cc.  */
    3871              : 
    3872              : extern void
    3873            5 : gcc_jit_context_set_bool_use_external_driver (gcc_jit_context *ctxt,
    3874              :                                               int bool_value)
    3875              : {
    3876            5 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3877            5 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3878            5 :   ctxt->set_inner_bool_option (
    3879              :     gcc::jit::INNER_BOOL_OPTION_USE_EXTERNAL_DRIVER,
    3880              :     bool_value);
    3881            5 : }
    3882              : 
    3883              : /* Public entrypoint.  See description in libgccjit.h.
    3884              : 
    3885              :    After error-checking, the real work is done by the
    3886              :    gcc::jit::recording::context::add_command_line_option method in
    3887              :    jit-recording.cc.  */
    3888              : 
    3889              : void
    3890         1417 : gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
    3891              :                                          const char *optname)
    3892              : {
    3893         1417 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3894         1417 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3895         1417 :   RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
    3896         1417 :   if (ctxt->get_logger ())
    3897         1127 :     ctxt->get_logger ()->log ("optname: %s", optname);
    3898              : 
    3899         1417 :   ctxt->add_command_line_option (optname);
    3900         1417 : }
    3901              : 
    3902              : /* Public entrypoint.  See description in libgccjit.h.
    3903              : 
    3904              :    The real work is done by the
    3905              :    gcc::jit::recording::context::add_driver_option method in
    3906              :    jit-recording.cc.  */
    3907              : 
    3908              : void
    3909           15 : gcc_jit_context_add_driver_option (gcc_jit_context *ctxt,
    3910              :                                    const char *optname)
    3911              : {
    3912           15 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3913           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3914           15 :   RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
    3915           15 :   if (ctxt->get_logger ())
    3916           15 :     ctxt->get_logger ()->log ("optname: %s", optname);
    3917              : 
    3918           15 :   ctxt->add_driver_option (optname);
    3919           15 : }
    3920              : 
    3921              : /* Public entrypoint.  See description in libgccjit.h.
    3922              : 
    3923              :    After error-checking, the real work is done by the
    3924              :    gcc::jit::recording::context::enable_dump method in
    3925              :    jit-recording.cc.  */
    3926              : 
    3927              : void
    3928           50 : gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
    3929              :                              const char *dumpname,
    3930              :                              char **out_ptr)
    3931              : {
    3932           50 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3933           50 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3934           50 :   RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
    3935           50 :   RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
    3936              : 
    3937           50 :   ctxt->enable_dump (dumpname, out_ptr);
    3938           50 : }
    3939              : 
    3940              : /* Public entrypoint.  See description in libgccjit.h.
    3941              : 
    3942              :    After error-checking, the real work is done by the
    3943              :    gcc::jit::recording::context::compile method in
    3944              :    jit-recording.cc.  */
    3945              : 
    3946              : gcc_jit_result *
    3947         1744 : gcc_jit_context_compile (gcc_jit_context *ctxt)
    3948              : {
    3949         1744 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3950              : 
    3951         1744 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3952              : 
    3953         1744 :   ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
    3954              : 
    3955         1744 :   gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
    3956              : 
    3957         1744 :   ctxt->log ("%s: returning (gcc_jit_result *)%p",
    3958              :              __func__, (void *)result);
    3959              : 
    3960         1744 :   return result;
    3961         1744 : }
    3962              : 
    3963              : /* Public entrypoint.  See description in libgccjit.h.
    3964              : 
    3965              :    After error-checking, the real work is done by the
    3966              :    gcc::jit::recording::context::compile_to_file method in
    3967              :    jit-recording.cc.  */
    3968              : 
    3969              : void
    3970          126 : gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
    3971              :                                  enum gcc_jit_output_kind output_kind,
    3972              :                                  const char *output_path)
    3973              : {
    3974          126 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    3975          126 :   JIT_LOG_FUNC (ctxt->get_logger ());
    3976          126 :   RETURN_IF_FAIL_PRINTF1 (
    3977              :     ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
    3978              :      && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
    3979              :     ctxt, NULL,
    3980              :     "unrecognized output_kind: %i",
    3981              :     output_kind);
    3982          126 :   RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
    3983              : 
    3984          126 :   ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
    3985          126 :   ctxt->log ("output_kind: %i", output_kind);
    3986          126 :   ctxt->log ("output_path: %s", output_path);
    3987              : 
    3988          126 :   ctxt->compile_to_file (output_kind, output_path);
    3989          126 : }
    3990              : 
    3991              : /* Public entrypoint.  See description in libgccjit.h.
    3992              : 
    3993              :    After error-checking, the real work is done by the
    3994              :    gcc::jit::recording::context::set_str_option method in
    3995              :    jit-recording.cc.  */
    3996              : 
    3997              : void
    3998            5 : gcc_jit_context_set_output_ident (gcc_jit_context *ctxt,
    3999              :                                   const char* output_ident)
    4000              : {
    4001            5 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4002            5 :   RETURN_IF_FAIL (output_ident, ctxt, NULL, "NULL output_ident");
    4003            5 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4004              : 
    4005            5 :   ctxt->set_output_ident (output_ident);
    4006            5 : }
    4007              : 
    4008              : gcc_jit_target_info *
    4009           62 : gcc_jit_context_get_target_info (gcc_jit_context *ctxt)
    4010              : {
    4011           62 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4012           62 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4013              : 
    4014           62 :   ctxt->log ("populate_target_info of ctxt: %p", (void *)ctxt);
    4015              : 
    4016           62 :   ctxt->populate_target_info ();
    4017              : 
    4018           62 :   return (gcc_jit_target_info*) ctxt->move_target_info ();
    4019           62 : }
    4020              : 
    4021              : void
    4022            2 : gcc_jit_target_info_release (gcc_jit_target_info *info)
    4023              : {
    4024            2 :   RETURN_IF_FAIL (info, NULL, NULL, "NULL info");
    4025            2 :   delete info;
    4026              : }
    4027              : 
    4028              : int
    4029            1 : gcc_jit_target_info_cpu_supports (gcc_jit_target_info *info,
    4030              :                                   const char *feature)
    4031              : {
    4032            1 :   RETURN_VAL_IF_FAIL (info, 0, NULL, NULL, "NULL info");
    4033            1 :   RETURN_VAL_IF_FAIL (feature, 0, NULL, NULL, "NULL feature");
    4034            1 :   return info->has_target_value ("target_feature", feature);
    4035              : }
    4036              : 
    4037              : const char *
    4038            0 : gcc_jit_target_info_arch (gcc_jit_target_info *info)
    4039              : {
    4040            0 :   RETURN_NULL_IF_FAIL (info, NULL, NULL, "NULL info");
    4041            0 :   return info->m_arch.c_str ();
    4042              : }
    4043              : 
    4044              : int
    4045          256 : gcc_jit_target_info_supports_target_dependent_type (gcc_jit_target_info *info,
    4046              :                                                     enum gcc_jit_types type)
    4047              : {
    4048          256 :   RETURN_VAL_IF_FAIL (info, 0, NULL, NULL, "NULL info");
    4049          256 :   return info->m_supported_target_dependent_types.find (type)
    4050          512 :     != info->m_supported_target_dependent_types.end ();
    4051              : }
    4052              : 
    4053              : void
    4054            0 : gcc_jit_context_set_abort_on_unsupported_target_builtin (gcc_jit_context *ctxt)
    4055              : {
    4056            0 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4057              : 
    4058            0 :   ctxt->set_abort_on_unsupported_target_builtin ();
    4059              : }
    4060              : 
    4061              : /* Public entrypoint.  See description in libgccjit.h.
    4062              : 
    4063              :    After error-checking, the real work is done by the
    4064              :    gcc::jit::recording::context::dump_to_file method in
    4065              :    jit-recording.cc.  */
    4066              : 
    4067              : void
    4068           14 : gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
    4069              :                               const char *path,
    4070              :                               int update_locations)
    4071              : {
    4072           14 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4073           14 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4074           14 :   RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
    4075           14 :   ctxt->dump_to_file (path, update_locations);
    4076           14 : }
    4077              : 
    4078              : /* Public entrypoint.  See description in libgccjit.h.  */
    4079              : 
    4080              : void
    4081         1157 : gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
    4082              :                              FILE *logfile,
    4083              :                              int flags,
    4084              :                              int verbosity)
    4085              : {
    4086         1157 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4087         1157 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4088         1157 :   RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
    4089         1157 :   RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
    4090              : 
    4091         1157 :   gcc::jit::logger *logger;
    4092         1157 :   if (logfile)
    4093         1157 :     logger = new gcc::jit::logger (logfile, flags, verbosity);
    4094              :   else
    4095              :     logger = NULL;
    4096         1157 :   ctxt->set_logger (logger);
    4097         1157 : }
    4098              : 
    4099              : /* Public entrypoint.  See description in libgccjit.h.
    4100              : 
    4101              :    After error-checking, the real work is done by the
    4102              :    gcc::jit::recording::context::dump_reproducer_to_file method in
    4103              :    jit-recording.cc.  */
    4104              : 
    4105              : void
    4106         1163 : gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
    4107              :                                          const char *path)
    4108              : {
    4109         1163 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4110         1163 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4111         1163 :   RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
    4112         1163 :   ctxt->dump_reproducer_to_file (path);
    4113         1163 : }
    4114              : 
    4115              : /* Public entrypoint.  See description in libgccjit.h.
    4116              : 
    4117              :    After error-checking, the real work is done by the
    4118              :    gcc::jit::recording::context::get_first_error method in
    4119              :    jit-recording.cc.  */
    4120              : 
    4121              : const char *
    4122          540 : gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
    4123              : {
    4124          540 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4125          540 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4126              : 
    4127          540 :   return ctxt->get_first_error ();
    4128          540 : }
    4129              : 
    4130              : /* Public entrypoint.  See description in libgccjit.h.
    4131              : 
    4132              :    After error-checking, the real work is done by the
    4133              :    gcc::jit::recording::context::get_last_error method in
    4134              :    jit-recording.cc.  */
    4135              : 
    4136              : const char *
    4137          126 : gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
    4138              : {
    4139          126 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
    4140              : 
    4141          126 :   return ctxt->get_last_error ();
    4142              : }
    4143              : 
    4144              : /* Public entrypoint.  See description in libgccjit.h.
    4145              : 
    4146              :    After error-checking, the real work is done by the
    4147              :    gcc::jit::result::get_code method in jit-result.cc.  */
    4148              : 
    4149              : void *
    4150         2897 : gcc_jit_result_get_code (gcc_jit_result *result,
    4151              :                          const char *fnname)
    4152              : {
    4153         2897 :   RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
    4154         2897 :   JIT_LOG_FUNC (result->get_logger ());
    4155         2897 :   RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
    4156              : 
    4157         2897 :   result->log ("locating fnname: %s", fnname);
    4158         2897 :   void *code = result->get_code (fnname);
    4159         2897 :   result->log ("%s: returning (void *)%p", __func__, code);
    4160              : 
    4161         2897 :   return code;
    4162         2897 : }
    4163              : 
    4164              : /* Public entrypoint.  See description in libgccjit.h.
    4165              : 
    4166              :    After error-checking, the real work is done by the
    4167              :    gcc::jit::result::get_global method in jit-result.cc.  */
    4168              : 
    4169              : void *
    4170          695 : gcc_jit_result_get_global (gcc_jit_result *result,
    4171              :                            const char *name)
    4172              : {
    4173          695 :   RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
    4174          695 :   JIT_LOG_FUNC (result->get_logger ());
    4175          695 :   RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
    4176              : 
    4177          695 :   void *global = result->get_global (name);
    4178          695 :   result->log ("%s: returning (void *)%p", __func__, global);
    4179              : 
    4180          695 :   return global;
    4181          695 : }
    4182              : 
    4183              : /* Public entrypoint.  See description in libgccjit.h.
    4184              : 
    4185              :    After error-checking, this is essentially a wrapper around the
    4186              :    destructor for gcc::jit::result in jit-result.cc.  */
    4187              : 
    4188              : void
    4189         1743 : gcc_jit_result_release (gcc_jit_result *result)
    4190              : {
    4191         1743 :   RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
    4192         1133 :   JIT_LOG_FUNC (result->get_logger ());
    4193         1133 :   result->log ("deleting result: %p", (void *)result);
    4194         1133 :   delete result;
    4195         1133 : }
    4196              : 
    4197              : /**********************************************************************
    4198              :  Timing support.
    4199              :  **********************************************************************/
    4200              : 
    4201              : /* Create a gcc_jit_timer instance, and start timing.  */
    4202              : 
    4203              : gcc_jit_timer *
    4204           14 : gcc_jit_timer_new (void)
    4205              : {
    4206           14 :   gcc_jit_timer *timer = new gcc_jit_timer ();
    4207           14 :   timer->start (TV_TOTAL);
    4208           14 :   timer->push (TV_JIT_CLIENT_CODE);
    4209           14 :   return timer;
    4210              : }
    4211              : 
    4212              : /* Release a gcc_jit_timer instance.  */
    4213              : 
    4214              : void
    4215           14 : gcc_jit_timer_release (gcc_jit_timer *timer)
    4216              : {
    4217           14 :   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
    4218              : 
    4219           14 :   delete timer;
    4220              : }
    4221              : 
    4222              : /* Associate a gcc_jit_timer instance with a context.  */
    4223              : 
    4224              : void
    4225          400 : gcc_jit_context_set_timer (gcc_jit_context *ctxt,
    4226              :                            gcc_jit_timer *timer)
    4227              : {
    4228          400 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
    4229          400 :   RETURN_IF_FAIL (timer, ctxt, NULL, "NULL timer");
    4230              : 
    4231          400 :   ctxt->set_timer (timer);
    4232              : }
    4233              : 
    4234              : /* Get the timer associated with a context (if any).  */
    4235              : 
    4236              : gcc_jit_timer *
    4237            0 : gcc_jit_context_get_timer (gcc_jit_context *ctxt)
    4238              : {
    4239            0 :   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
    4240              : 
    4241            0 :   return (gcc_jit_timer *)ctxt->get_timer ();
    4242              : }
    4243              : 
    4244              : /* Push the given item onto the timing stack.  */
    4245              : 
    4246              : void
    4247         1605 : gcc_jit_timer_push (gcc_jit_timer *timer,
    4248              :                     const char *item_name)
    4249              : {
    4250         1605 :   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
    4251         1605 :   RETURN_IF_FAIL (item_name, NULL, NULL, "NULL item_name");
    4252         1605 :   timer->push_client_item (item_name);
    4253              : }
    4254              : 
    4255              : /* Pop the top item from the timing stack.  */
    4256              : 
    4257              : void
    4258         1610 : gcc_jit_timer_pop (gcc_jit_timer *timer,
    4259              :                    const char *item_name)
    4260              : {
    4261         1610 :   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
    4262              : 
    4263         1610 :   if (item_name)
    4264              :     {
    4265         1610 :       const char *top_item_name = timer->get_topmost_item_name ();
    4266              : 
    4267         1610 :       RETURN_IF_FAIL_PRINTF1
    4268              :         (top_item_name, NULL, NULL,
    4269              :          "pop of empty timing stack (attempting to pop: \"%s\")",
    4270              :          item_name);
    4271              : 
    4272         1610 :       RETURN_IF_FAIL_PRINTF2
    4273              :         (strcmp (item_name, top_item_name) == 0, NULL, NULL,
    4274              :          "mismatching item_name:"
    4275              :          " top of timing stack: \"%s\","
    4276              :          " attempting to pop: \"%s\"",
    4277              :          top_item_name,
    4278              :          item_name);
    4279              :     }
    4280              : 
    4281         1600 :   timer->pop_client_item ();
    4282              : }
    4283              : 
    4284              : /* Print timing information to the given stream about activity since
    4285              :    the timer was started.  */
    4286              : 
    4287              : void
    4288            4 : gcc_jit_timer_print (gcc_jit_timer *timer,
    4289              :                      FILE *f_out)
    4290              : {
    4291            4 :   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
    4292            4 :   RETURN_IF_FAIL (f_out, NULL, NULL, "NULL f_out");
    4293              : 
    4294            4 :   timer->pop (TV_JIT_CLIENT_CODE);
    4295            4 :   timer->stop (TV_TOTAL);
    4296            4 :   timer->print (f_out);
    4297            4 :   timer->start (TV_TOTAL);
    4298            4 :   timer->push (TV_JIT_CLIENT_CODE);
    4299              : }
    4300              : 
    4301              : /* Public entrypoint.  See description in libgccjit.h.
    4302              : 
    4303              :    After error-checking, the real work is effectively done by the
    4304              :    gcc::jit::base_call::set_require_tail_call setter in jit-recording.h.  */
    4305              : 
    4306              : void
    4307           20 : gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *rvalue,
    4308              :                                            int require_tail_call)
    4309              : {
    4310           20 :   RETURN_IF_FAIL (rvalue, NULL, NULL, "NULL call");
    4311           20 :   JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
    4312              : 
    4313              :   /* Verify that it's a call.  */
    4314           20 :   gcc::jit::recording::base_call *call = rvalue->dyn_cast_base_call ();
    4315           20 :   RETURN_IF_FAIL_PRINTF1 (call, NULL, NULL, "not a call: %s",
    4316              :                           rvalue->get_debug_string ());
    4317              : 
    4318           20 :   call->set_require_tail_call (require_tail_call);
    4319           20 : }
    4320              : 
    4321              : /* Public entrypoint.  See description in libgccjit.h.
    4322              : 
    4323              :    After error-checking, the real work is done by the
    4324              :    gcc::jit::recording::type::get_aligned method, in
    4325              :    jit-recording.cc.  */
    4326              : 
    4327              : gcc_jit_type *
    4328          431 : gcc_jit_type_get_aligned (gcc_jit_type *type,
    4329              :                           size_t alignment_in_bytes)
    4330              : {
    4331          431 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
    4332              : 
    4333          429 :   gcc::jit::recording::context *ctxt = type->m_ctxt;
    4334              : 
    4335          429 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4336              : 
    4337          429 :   RETURN_NULL_IF_FAIL_PRINTF1
    4338              :     (pow2_or_zerop (alignment_in_bytes), ctxt, NULL,
    4339              :      "alignment not a power of two: %zi",
    4340              :      alignment_in_bytes);
    4341          424 :   RETURN_NULL_IF_FAIL (!type->is_void (), ctxt, NULL, "void type");
    4342              : 
    4343          414 :   return (gcc_jit_type *)type->get_aligned (alignment_in_bytes);
    4344          429 : }
    4345              : 
    4346              : void
    4347           45 : gcc_jit_function_add_attribute (gcc_jit_function *func,
    4348              :                                 gcc_jit_fn_attribute attribute)
    4349              : {
    4350           45 :   RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
    4351           45 :   RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_FN_ATTRIBUTE_MAX),
    4352              :                   NULL,
    4353              :                   NULL,
    4354              :                   "attribute should be a `gcc_jit_fn_attribute` enum value");
    4355              : 
    4356           45 :   func->add_attribute (attribute);
    4357              : }
    4358              : 
    4359              : void
    4360            5 : gcc_jit_function_add_string_attribute (gcc_jit_function *func,
    4361              :                                        gcc_jit_fn_attribute attribute,
    4362              :                                        const char* value)
    4363              : {
    4364            5 :   RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
    4365            5 :   RETURN_IF_FAIL (value, NULL, NULL, "NULL value");
    4366            5 :   RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_FN_ATTRIBUTE_MAX),
    4367              :                   NULL,
    4368              :                   NULL,
    4369              :                   "attribute should be a `gcc_jit_fn_attribute` enum value");
    4370              : 
    4371            5 :   func->add_string_attribute (attribute, value);
    4372              : }
    4373              : 
    4374              : /* This function adds an attribute with multiple integer values.  For example
    4375              :    `nonnull(1, 2)`.  The numbers in `values` are supposed to map how they
    4376              :    should be written in C code.  So for `nonnull(1, 2)`, you should pass `1`
    4377              :    and `2` in `values` (and set `length` to `2`). */
    4378              : void
    4379            5 : gcc_jit_function_add_integer_array_attribute (gcc_jit_function *func,
    4380              :                                               gcc_jit_fn_attribute attribute,
    4381              :                                               const int* values,
    4382              :                                               size_t length)
    4383              : {
    4384            5 :   RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
    4385            5 :   RETURN_IF_FAIL (values, NULL, NULL, "NULL values");
    4386            5 :   RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_FN_ATTRIBUTE_MAX),
    4387              :                   NULL,
    4388              :                   NULL,
    4389              :                   "attribute should be a `gcc_jit_fn_attribute` enum value");
    4390              : 
    4391            5 :   func->add_integer_array_attribute (attribute, values, length);
    4392              : }
    4393              : 
    4394              : void
    4395            5 : gcc_jit_lvalue_add_string_attribute (gcc_jit_lvalue *variable,
    4396              :                                      gcc_jit_variable_attribute attribute,
    4397              :                                      const char* value)
    4398              : {
    4399            5 :   RETURN_IF_FAIL (variable, NULL, NULL, "NULL variable");
    4400            5 :   RETURN_IF_FAIL (value, NULL, NULL, "NULL value");
    4401            5 :   RETURN_IF_FAIL (variable->is_global () || variable->is_local (),
    4402              :                   NULL,
    4403              :                   NULL,
    4404              :                   "variable should be a variable");
    4405            5 :   RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_VARIABLE_ATTRIBUTE_MAX),
    4406              :                   NULL,
    4407              :                   NULL,
    4408              :                   "attribute should be a `gcc_jit_variable_attribute` enum value");
    4409              : 
    4410            5 :   variable->add_string_attribute (attribute, value);
    4411              : }
    4412              : 
    4413              : /* Public entrypoint.  See description in libgccjit.h.
    4414              : 
    4415              :    After error-checking, the real work is done by the
    4416              :    gcc::jit::recording::type::get_vector method, in
    4417              :    jit-recording.cc.  */
    4418              : 
    4419              : gcc_jit_type *
    4420          525 : gcc_jit_type_get_vector (gcc_jit_type *type, size_t num_units)
    4421              : {
    4422          525 :   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
    4423              : 
    4424          521 :   gcc::jit::recording::context *ctxt = type->m_ctxt;
    4425              : 
    4426          521 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4427              : 
    4428          521 :   RETURN_NULL_IF_FAIL_PRINTF1
    4429              :     (type->is_int () || type->is_float (), ctxt, NULL,
    4430              :      "type is not integral or floating point: %s",
    4431              :      type->get_debug_string ());
    4432              : 
    4433          466 :   RETURN_NULL_IF_FAIL_PRINTF1
    4434              :     (pow2_or_zerop (num_units), ctxt, NULL,
    4435              :      "num_units not a power of two: %zi",
    4436              :      num_units);
    4437              : 
    4438          461 :   return (gcc_jit_type *)type->get_vector (num_units);
    4439          521 : }
    4440              : 
    4441              : /* Public entrypoint.  See description in libgccjit.h.
    4442              : 
    4443              :    After error-checking, the real work is done by the
    4444              :    gcc::jit::recording::function::get_address method, in
    4445              :    jit-recording.cc.  */
    4446              : 
    4447              : gcc_jit_rvalue *
    4448           15 : gcc_jit_function_get_address (gcc_jit_function *fn,
    4449              :                               gcc_jit_location *loc)
    4450              : {
    4451           15 :   RETURN_NULL_IF_FAIL (fn, NULL, NULL, "NULL function");
    4452              : 
    4453           15 :   gcc::jit::recording::context *ctxt = fn->m_ctxt;
    4454              : 
    4455           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4456              :   /* LOC can be NULL.  */
    4457              : 
    4458           15 :   return (gcc_jit_rvalue *)fn->get_address (loc);
    4459           15 : }
    4460              : 
    4461              : /* Public entrypoint.  See description in libgccjit.h.
    4462              : 
    4463              :    After error-checking, the real work is done by the
    4464              :    gcc::jit::recording::context::new_rvalue_from_vector method, in
    4465              :    jit-recording.cc.  */
    4466              : 
    4467              : extern gcc_jit_rvalue *
    4468          135 : gcc_jit_context_new_rvalue_from_vector (gcc_jit_context *ctxt,
    4469              :                                         gcc_jit_location *loc,
    4470              :                                         gcc_jit_type *vec_type,
    4471              :                                         size_t num_elements,
    4472              :                                         gcc_jit_rvalue **elements)
    4473              : {
    4474          135 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL ctxt");
    4475          135 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4476              : 
    4477              :   /* LOC can be NULL.  */
    4478          135 :   RETURN_NULL_IF_FAIL (vec_type, ctxt, loc, "NULL vec_type");
    4479              : 
    4480              :   /* "vec_type" must be a vector type.  */
    4481          135 :   gcc::jit::recording::vector_type *as_vec_type
    4482          135 :     = vec_type->dyn_cast_vector_type ();
    4483          135 :   RETURN_NULL_IF_FAIL_PRINTF1 (as_vec_type, ctxt, loc,
    4484              :                                "%s is not a vector type",
    4485              :                                vec_type->get_debug_string ());
    4486              : 
    4487              :   /* "num_elements" must match.  */
    4488          135 :   RETURN_NULL_IF_FAIL_PRINTF1 (
    4489              :     num_elements == as_vec_type->get_num_units (), ctxt, loc,
    4490              :     "num_elements != %zi", as_vec_type->get_num_units ());
    4491              : 
    4492              :   /* "elements must be non-NULL.  */
    4493          135 :   RETURN_NULL_IF_FAIL (elements, ctxt, loc, "NULL elements");
    4494              : 
    4495              :   /* Each of "elements" must be non-NULL and of the correct type.  */
    4496          135 :   gcc::jit::recording::type *element_type
    4497          135 :     = as_vec_type->get_element_type ();
    4498          675 :   for (size_t i = 0; i < num_elements; i++)
    4499              :     {
    4500          540 :       RETURN_NULL_IF_FAIL_PRINTF1 (
    4501              :         elements[i], ctxt, loc, "NULL elements[%zi]", i);
    4502          540 :       RETURN_NULL_IF_FAIL_PRINTF4 (
    4503              :         compatible_types (element_type,
    4504              :                           elements[i]->get_type ()),
    4505              :         ctxt, loc,
    4506              :         "mismatching type for element[%zi] (expected type: %s): %s (type: %s)",
    4507              :         i,
    4508              :         element_type->get_debug_string (),
    4509              :         elements[i]->get_debug_string (),
    4510              :         elements[i]->get_type ()->get_debug_string ());
    4511              :     }
    4512              : 
    4513          135 :   return (gcc_jit_rvalue *)ctxt->new_rvalue_from_vector
    4514          135 :     (loc,
    4515              :      as_vec_type,
    4516          135 :      (gcc::jit::recording::rvalue **)elements);
    4517          135 : }
    4518              : 
    4519              : /* Public entrypoint.  See description in libgccjit.h.
    4520              : 
    4521              :    After error-checking, the real work is done by the
    4522              :    gcc::jit::recording::context::new_rvalue_vector_perm method, in
    4523              :    jit-recording.cc.  */
    4524              : 
    4525              : gcc_jit_rvalue *
    4526           15 : gcc_jit_context_new_rvalue_vector_perm (gcc_jit_context *ctxt,
    4527              :                                         gcc_jit_location *loc,
    4528              :                                         gcc_jit_rvalue *elements1,
    4529              :                                         gcc_jit_rvalue *elements2,
    4530              :                                         gcc_jit_rvalue *mask)
    4531              : {
    4532           15 :   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL ctxt");
    4533           15 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4534           15 :   RETURN_NULL_IF_FAIL (elements1, ctxt, loc, "NULL elements1");
    4535           15 :   RETURN_NULL_IF_FAIL (elements2, ctxt, loc, "NULL elements2");
    4536           15 :   RETURN_NULL_IF_FAIL (mask, ctxt, loc, "NULL mask");
    4537              : 
    4538              :   /* LOC can be NULL.  */
    4539              : 
    4540           15 :   gcc::jit::recording::type *elements1_type = elements1->get_type ();
    4541           15 :   gcc::jit::recording::type *elements2_type = elements2->get_type ();
    4542           15 :   RETURN_NULL_IF_FAIL_PRINTF4 (
    4543              :     compatible_types (elements1->get_type ()->unqualified (),
    4544              :                       elements2->get_type ()->unqualified ()),
    4545              :     ctxt, loc,
    4546              :     "mismatching types for vector perm:"
    4547              :     " elements1: %s (type: %s) elements2: %s (type: %s)",
    4548              :     elements1->get_debug_string (),
    4549              :     elements1_type->get_debug_string (),
    4550              :     elements2->get_debug_string (),
    4551              :     elements2_type->get_debug_string ());
    4552              : 
    4553           15 :   gcc::jit::recording::type *mask_type = mask->get_type ();
    4554           15 :   gcc::jit::recording::vector_type *mask_vector_type =
    4555           15 :     mask_type->dyn_cast_vector_type ();
    4556           15 :   gcc::jit::recording::vector_type *elements1_vector_type =
    4557           15 :     elements1_type->dyn_cast_vector_type ();
    4558              : 
    4559           15 :   size_t mask_len = mask_vector_type->get_num_units ();
    4560           15 :   size_t elements1_len = elements1_vector_type->get_num_units ();
    4561              : 
    4562           15 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    4563              :     mask_len == elements1_len,
    4564              :     ctxt, loc,
    4565              :     "mismatching length for mask:"
    4566              :     " elements1 length: %ld mask length: %ld",
    4567              :     mask_len,
    4568              :     elements1_len);
    4569              : 
    4570           15 :   gcc::jit::recording::type *mask_element_type =
    4571           15 :     mask_vector_type->get_element_type ();
    4572              : 
    4573           15 :   RETURN_NULL_IF_FAIL (
    4574              :     mask_element_type->is_int (),
    4575              :     ctxt, loc,
    4576              :     "elements of mask must be of an integer type");
    4577              : 
    4578           15 :   gcc::jit::recording::type *elements1_element_type =
    4579           15 :     elements1_vector_type->get_element_type ();
    4580           15 :   size_t mask_element_size = mask_element_type->get_size ();
    4581           15 :   size_t elements1_element_size = elements1_element_type->get_size ();
    4582              : 
    4583           15 :   RETURN_NULL_IF_FAIL_PRINTF2 (
    4584              :     mask_element_size == elements1_element_size,
    4585              :     ctxt, loc,
    4586              :     "mismatching size for mask element type:"
    4587              :     " elements1 element type: %ld mask element type: %ld",
    4588              :     mask_element_size,
    4589              :     elements1_element_size);
    4590              : 
    4591           15 :   return (gcc_jit_rvalue *)ctxt->new_rvalue_vector_perm (loc, elements1,
    4592           15 :                                                          elements2, mask);
    4593           15 : }
    4594              : 
    4595              : /* A mutex around the cached state in parse_basever.
    4596              :    Ideally this would be within parse_basever, but the mutex is only needed
    4597              :    by libgccjit.  */
    4598              : 
    4599              : static std::mutex version_mutex;
    4600              : 
    4601              : struct jit_version_info
    4602              : {
    4603              :   /* Default constructor.  Populate via parse_basever,
    4604              :      guarded by version_mutex.  */
    4605           45 :   jit_version_info ()
    4606              :   {
    4607           45 :     std::lock_guard<std::mutex> g (version_mutex);
    4608           45 :     parse_basever (&major, &minor, &patchlevel);
    4609           45 :   }
    4610              : 
    4611              :   int major;
    4612              :   int minor;
    4613              :   int patchlevel;
    4614              : };
    4615              : 
    4616              : 
    4617              : extern int
    4618           15 : gcc_jit_version_major (void)
    4619              : {
    4620           15 :   jit_version_info vi;
    4621           15 :   return vi.major;
    4622              : }
    4623              : 
    4624              : extern int
    4625           15 : gcc_jit_version_minor (void)
    4626              : {
    4627           15 :   jit_version_info vi;
    4628           15 :   return vi.minor;
    4629              : }
    4630              : 
    4631              : extern int
    4632           15 : gcc_jit_version_patchlevel (void)
    4633              : {
    4634           15 :   jit_version_info vi;
    4635           15 :   return vi.patchlevel;
    4636              : }
    4637              : 
    4638              : /**********************************************************************
    4639              :  Asm support.
    4640              :  **********************************************************************/
    4641              : 
    4642              : /* Public entrypoint.  See description in libgccjit.h.
    4643              : 
    4644              :    After error-checking, the real work is done by the
    4645              :    gcc::jit::recording::block::add_extended_asm, in
    4646              :    jit-recording.cc.  */
    4647              : 
    4648              : gcc_jit_extended_asm *
    4649           50 : gcc_jit_block_add_extended_asm (gcc_jit_block *block,
    4650              :                                 gcc_jit_location *loc,
    4651              :                                 const char *asm_template)
    4652              : {
    4653           50 :   RETURN_NULL_IF_NOT_VALID_BLOCK (block, loc);
    4654           50 :   gcc::jit::recording::context *ctxt = block->get_context ();
    4655           50 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4656              :   /* LOC can be NULL.  */
    4657           50 :   RETURN_NULL_IF_FAIL (asm_template, ctxt, loc, "NULL asm_template");
    4658              : 
    4659           50 :   return (gcc_jit_extended_asm *)block->add_extended_asm (loc, asm_template);
    4660           50 : }
    4661              : 
    4662              : /* Public entrypoint.  See description in libgccjit.h.
    4663              : 
    4664              :    After error-checking, the real work is done by the
    4665              :    gcc::jit::recording::block::end_with_extended_asm_goto, in
    4666              :    jit-recording.cc.  */
    4667              : 
    4668              : gcc_jit_extended_asm *
    4669           20 : gcc_jit_block_end_with_extended_asm_goto (gcc_jit_block *block,
    4670              :                                           gcc_jit_location *loc,
    4671              :                                           const char *asm_template,
    4672              :                                           int num_goto_blocks,
    4673              :                                           gcc_jit_block **goto_blocks,
    4674              :                                           gcc_jit_block *fallthrough_block)
    4675              : {
    4676           20 :   RETURN_NULL_IF_NOT_VALID_BLOCK (block, loc);
    4677           20 :   gcc::jit::recording::context *ctxt = block->get_context ();
    4678           20 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4679              :   /* LOC can be NULL.  */
    4680           20 :   RETURN_NULL_IF_FAIL (asm_template, ctxt, loc, "NULL asm_template");
    4681           20 :   RETURN_NULL_IF_FAIL (num_goto_blocks >= 0, ctxt, loc, "num_goto_blocks < 0");
    4682           40 :   for (int i = 0; i < num_goto_blocks; i++)
    4683           20 :     RETURN_NULL_IF_FAIL_PRINTF1 (goto_blocks[i],
    4684              :                                  ctxt, loc,
    4685              :                                  "NULL goto_blocks[%i]", i);
    4686              :   /* fallthrough_block can be NULL.  */
    4687           20 :   return (gcc_jit_extended_asm *)block->end_with_extended_asm_goto
    4688           20 :     (loc, asm_template,
    4689              :      num_goto_blocks, (gcc::jit::recording::block **)goto_blocks,
    4690           20 :      fallthrough_block);
    4691           20 : }
    4692              : 
    4693              : /* Public entrypoint.  See description in libgccjit.h.
    4694              : 
    4695              :    After error-checking, this calls the trivial
    4696              :    gcc::jit::recording::memento::as_object method (an extended_asm is a
    4697              :    memento), in jit-recording.h.  */
    4698              : 
    4699              : gcc_jit_object *
    4700           60 : gcc_jit_extended_asm_as_object (gcc_jit_extended_asm *ext_asm)
    4701              : {
    4702           60 :   RETURN_NULL_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
    4703              : 
    4704           60 :   return static_cast <gcc_jit_object *> (ext_asm->as_object ());
    4705              : }
    4706              : 
    4707              : /* Public entrypoint.  See description in libgccjit.h.
    4708              : 
    4709              :    After error-checking, the real work is done by the
    4710              :    gcc::jit::recording::extended_asm::set_volatile_flag, in
    4711              :    jit-recording.cc.  */
    4712              : 
    4713              : void
    4714           20 : gcc_jit_extended_asm_set_volatile_flag (gcc_jit_extended_asm *ext_asm,
    4715              :                                         int flag)
    4716              : {
    4717           20 :   RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
    4718           20 :   ext_asm->set_volatile_flag (flag);
    4719              : }
    4720              : 
    4721              : /* Public entrypoint.  See description in libgccjit.h.
    4722              : 
    4723              :    After error-checking, the real work is done by the
    4724              :    gcc::jit::recording::extended_asm::set_inline_flag, in
    4725              :    jit-recording.cc.  */
    4726              : 
    4727              : void
    4728            0 : gcc_jit_extended_asm_set_inline_flag (gcc_jit_extended_asm *ext_asm,
    4729              :                                       int flag)
    4730              : {
    4731            0 :   RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
    4732            0 :   ext_asm->set_inline_flag (flag);
    4733              : }
    4734              : 
    4735              : /* Public entrypoint.  See description in libgccjit.h.
    4736              : 
    4737              :    After error-checking, the real work is done by the
    4738              :    gcc::jit::recording::extended_asm::add_output_operand, in
    4739              :    jit-recording.cc.  */
    4740              : 
    4741              : void
    4742           40 : gcc_jit_extended_asm_add_output_operand (gcc_jit_extended_asm *ext_asm,
    4743              :                                          const char *asm_symbolic_name,
    4744              :                                          const char *constraint,
    4745              :                                          gcc_jit_lvalue *dest)
    4746              : {
    4747           40 :   RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
    4748           40 :   gcc::jit::recording::context *ctxt = ext_asm->get_context ();
    4749           40 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4750           40 :   gcc::jit::recording::location *loc = ext_asm->get_loc ();
    4751              :   /* asm_symbolic_name can be NULL.  */
    4752           40 :   RETURN_IF_FAIL (constraint, ctxt, loc, "NULL constraint");
    4753           40 :   RETURN_IF_FAIL (dest, ctxt, loc, "NULL dest");
    4754           40 :   RETURN_IF_FAIL (!ext_asm->is_goto (), ctxt, loc,
    4755              :                   "cannot add output operand to asm goto");
    4756           40 :   ext_asm->add_output_operand (asm_symbolic_name, constraint, dest);
    4757           40 : }
    4758              : 
    4759              : /* Public entrypoint.  See description in libgccjit.h.
    4760              : 
    4761              :    After error-checking, the real work is done by the
    4762              :    gcc::jit::recording::extended_asm::add_input_operand, in
    4763              :    jit-recording.cc.  */
    4764              : 
    4765              : extern void
    4766           60 : gcc_jit_extended_asm_add_input_operand (gcc_jit_extended_asm *ext_asm,
    4767              :                                         const char *asm_symbolic_name,
    4768              :                                         const char *constraint,
    4769              :                                         gcc_jit_rvalue *src)
    4770              : {
    4771           60 :   RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
    4772           60 :   gcc::jit::recording::context *ctxt = ext_asm->get_context ();
    4773           60 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4774           60 :   gcc::jit::recording::location *loc = ext_asm->get_loc ();
    4775              :   /* asm_symbolic_name can be NULL.  */
    4776           60 :   RETURN_IF_FAIL (constraint, ctxt, loc, "NULL constraint");
    4777           60 :   RETURN_IF_FAIL (src, ctxt, loc, "NULL src");
    4778           60 :   ext_asm->add_input_operand (asm_symbolic_name, constraint, src);
    4779           60 : }
    4780              : 
    4781              : /* Public entrypoint.  See description in libgccjit.h.
    4782              : 
    4783              :    After error-checking, the real work is done by the
    4784              :    gcc::jit::recording::extended_asm::add_clobber, in
    4785              :    jit-recording.cc.  */
    4786              : 
    4787              : void
    4788           50 : gcc_jit_extended_asm_add_clobber (gcc_jit_extended_asm *ext_asm,
    4789              :                                   const char *victim)
    4790              : {
    4791           50 :   RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
    4792           50 :   gcc::jit::recording::context *ctxt = ext_asm->get_context ();
    4793           50 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4794           50 :   gcc::jit::recording::location *loc = ext_asm->get_loc ();
    4795           50 :   RETURN_IF_FAIL (victim, ctxt, loc, "NULL victim");
    4796           50 :   ext_asm->add_clobber (victim);
    4797           50 : }
    4798              : 
    4799              : /* Public entrypoint.  See description in libgccjit.h.
    4800              : 
    4801              :    After error-checking, the real work is done by the
    4802              :    gcc::jit::recording::context::add_top_level_asm, in
    4803              :    jit-recording.cc.  */
    4804              : 
    4805              : void
    4806           10 : gcc_jit_context_add_top_level_asm (gcc_jit_context *ctxt,
    4807              :                                    gcc_jit_location *loc,
    4808              :                                    const char *asm_stmts)
    4809              : {
    4810           10 :   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
    4811           10 :   JIT_LOG_FUNC (ctxt->get_logger ());
    4812              :   /* LOC can be NULL.  */
    4813           10 :   RETURN_IF_FAIL (asm_stmts, ctxt, NULL, "NULL asm_stmts");
    4814           10 :   ctxt->add_top_level_asm (loc, asm_stmts);
    4815           10 : }
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.