xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/jit/libgccjit.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /* Implementation of the C API; all wrappers into the internal C++ API
2    Copyright (C) 2013-2017 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 #include "system.h"
23 #include "coretypes.h"
24 #include "timevar.h"
25 #include "typed-splay-tree.h"
26 
27 #include "libgccjit.h"
28 #include "jit-recording.h"
29 #include "jit-result.h"
30 
31 /* The opaque types used by the public API are actually subclasses
32    of the gcc::jit::recording classes.  */
33 
34 struct gcc_jit_context : public gcc::jit::recording::context
35 {
36   gcc_jit_context (gcc_jit_context *parent_ctxt) :
37     context (parent_ctxt)
38   {}
39 };
40 
41 struct gcc_jit_result : public gcc::jit::result
42 {
43 };
44 
45 struct gcc_jit_object : public gcc::jit::recording::memento
46 {
47 };
48 
49 struct gcc_jit_location : public gcc::jit::recording::location
50 {
51 };
52 
53 struct gcc_jit_type : public gcc::jit::recording::type
54 {
55 };
56 
57 struct gcc_jit_struct : public gcc::jit::recording::struct_
58 {
59 };
60 
61 struct gcc_jit_field : public gcc::jit::recording::field
62 {
63 };
64 
65 struct gcc_jit_function : public gcc::jit::recording::function
66 {
67 };
68 
69 struct gcc_jit_block : public gcc::jit::recording::block
70 {
71 };
72 
73 struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
74 {
75 };
76 
77 struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
78 {
79 };
80 
81 struct gcc_jit_param : public gcc::jit::recording::param
82 {
83 };
84 
85 struct gcc_jit_case : public gcc::jit::recording::case_
86 {
87 };
88 
89 struct gcc_jit_timer : public timer
90 {
91 };
92 
93 /**********************************************************************
94  Error-handling.
95 
96  We try to gracefully handle API usage errors by being defensive
97  at the API boundary.
98  **********************************************************************/
99 
100 #define JIT_BEGIN_STMT do {
101 #define JIT_END_STMT   } while(0)
102 
103 /* Each of these error-handling macros determines if TEST_EXPR holds.
104 
105    If TEXT_EXPR fails to hold we return from the enclosing function and
106    print an error, either via adding an error on the given context CTXT
107    if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
108    is NULL.
109 
110    They have to be macros since they inject their "return" into the
111    function they are placed in.
112 
113    The variant macros express:
114 
115      (A) whether or not we need to return a value:
116 	    RETURN_VAL_IF_FAIL* vs
117 	    RETURN_IF_FAIL*,
118 	 with the former returning RETURN_EXPR, and
119 	    RETURN_NULL_IF_FAIL*
120 	 for the common case where a NULL value is to be returned on
121 	 error, and
122 
123      (B) whether the error message is to be directly printed:
124 	   RETURN_*IF_FAIL
125 	 or is a format string with some number of arguments:
126 	   RETURN_*IF_FAIL_PRINTF*
127 
128    They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
129    trailing semicolons.
130 */
131 
132 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG)	\
133   JIT_BEGIN_STMT							\
134     if (!(TEST_EXPR))							\
135       {								\
136 	jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG));	\
137 	return (RETURN_EXPR);						\
138       }								\
139   JIT_END_STMT
140 
141 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
142   JIT_BEGIN_STMT							\
143     if (!(TEST_EXPR))							\
144       {								\
145 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,			\
146 		   __func__, (A0));				\
147 	return (RETURN_EXPR);						\
148       }								\
149   JIT_END_STMT
150 
151 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
152   JIT_BEGIN_STMT							\
153     if (!(TEST_EXPR))							\
154       {								\
155 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
156 		   __func__, (A0), (A1));				\
157 	return (RETURN_EXPR);						\
158       }								\
159   JIT_END_STMT
160 
161 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
162   JIT_BEGIN_STMT							\
163     if (!(TEST_EXPR))							\
164       {								\
165 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
166 		   __func__, (A0), (A1), (A2));			\
167 	return (RETURN_EXPR);						\
168       }								\
169   JIT_END_STMT
170 
171 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
172   JIT_BEGIN_STMT							\
173     if (!(TEST_EXPR))							\
174       {								\
175 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
176 		   __func__, (A0), (A1), (A2), (A3));			\
177 	return (RETURN_EXPR);						\
178       }								\
179   JIT_END_STMT
180 
181 #define RETURN_VAL_IF_FAIL_PRINTF5(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
182   JIT_BEGIN_STMT							\
183     if (!(TEST_EXPR))							\
184       {								\
185 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
186 		   __func__, (A0), (A1), (A2), (A3), (A4));	\
187 	return (RETURN_EXPR);						\
188       }								\
189   JIT_END_STMT
190 
191 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
192   JIT_BEGIN_STMT							\
193     if (!(TEST_EXPR))							\
194       {								\
195 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
196 		   __func__, (A0), (A1), (A2), (A3), (A4), (A5));	\
197 	return (RETURN_EXPR);						\
198       }								\
199   JIT_END_STMT
200 
201 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
202   RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
203 
204 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
205   RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
206 
207 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
208   RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
209 
210 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
211   RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
212 
213 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
214   RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
215 
216 #define RETURN_NULL_IF_FAIL_PRINTF5(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
217   RETURN_VAL_IF_FAIL_PRINTF5 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4)
218 
219 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
220   RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
221 
222 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG)			\
223   JIT_BEGIN_STMT							\
224     if (!(TEST_EXPR))							\
225       {								\
226 	jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG));		\
227 	return;							\
228       }								\
229   JIT_END_STMT
230 
231 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
232   JIT_BEGIN_STMT							\
233     if (!(TEST_EXPR))							\
234       {								\
235 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
236 		   __func__, (A0));					\
237 	return;							\
238       }								\
239   JIT_END_STMT
240 
241 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
242   JIT_BEGIN_STMT							\
243     if (!(TEST_EXPR))							\
244       {								\
245 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
246 		   __func__, (A0), (A1));				\
247 	return;							\
248       }								\
249   JIT_END_STMT
250 
251 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
252   JIT_BEGIN_STMT							\
253     if (!(TEST_EXPR))							\
254       {								\
255 	jit_error ((CTXT), (LOC), "%s: " ERR_FMT,				\
256 		   __func__, (A0), (A1), (A2), (A3));			\
257 	return;							\
258       }								\
259   JIT_END_STMT
260 
261 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
262    it.  This will fail if BLOCK has already been terminated by some
263    kind of jump or a return.  */
264 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC)				\
265   JIT_BEGIN_STMT							\
266     RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block");		\
267     RETURN_IF_FAIL_PRINTF2 (						\
268       !(BLOCK)->has_been_terminated (),				\
269       (BLOCK)->get_context (),						\
270       (LOC),								\
271       "adding to terminated block: %s (already terminated by: %s)",	\
272       (BLOCK)->get_debug_string (),					\
273       (BLOCK)->get_last_statement ()->get_debug_string ());		\
274   JIT_END_STMT
275 
276 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
277    fails.  */
278 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC)			\
279   JIT_BEGIN_STMT							\
280     RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block");		\
281     RETURN_NULL_IF_FAIL_PRINTF2 (					\
282       !(BLOCK)->has_been_terminated (),				\
283       (BLOCK)->get_context (),						\
284       (LOC),								\
285       "adding to terminated block: %s (already terminated by: %s)",	\
286       (BLOCK)->get_debug_string (),					\
287       (BLOCK)->get_last_statement ()->get_debug_string ());		\
288   JIT_END_STMT
289 
290 /* Format the given string, and report it as an error, either on CTXT
291    if non-NULL, or by printing to stderr if we have a NULL context.
292    LOC gives the source location where the error occcurred, and can be
293    NULL.  */
294 
295 static void
296 jit_error (gcc::jit::recording::context *ctxt,
297 	   gcc_jit_location *loc,
298 	   const char *fmt, ...)
299   GNU_PRINTF(3, 4);
300 
301 static void
302 jit_error (gcc::jit::recording::context *ctxt,
303 	   gcc_jit_location *loc,
304 	   const char *fmt, ...)
305 {
306   va_list ap;
307   va_start (ap, fmt);
308 
309   if (ctxt)
310     ctxt->add_error_va (loc, fmt, ap);
311   else
312     {
313       /* No context?  Send to stderr.  */
314       vfprintf (stderr, fmt, ap);
315       fprintf (stderr, "\n");
316     }
317 
318   va_end (ap);
319 }
320 
321 /* Determine whether or not we can write to lvalues of type LTYPE from
322    rvalues of type RTYPE, detecting type errors such as attempting to
323    write to an int with a string literal (without an explicit cast).
324 
325    This is implemented by calling the
326    gcc::jit::recording::type::accepts_writes_from virtual function on
327    LTYPE.  */
328 
329 static bool
330 compatible_types (gcc::jit::recording::type *ltype,
331 		  gcc::jit::recording::type *rtype)
332 {
333   return ltype->accepts_writes_from (rtype);
334 }
335 
336 /* Public entrypoint for acquiring a gcc_jit_context.
337    Note that this creates a new top-level context; contrast with
338    gcc_jit_context_new_child_context below.
339 
340    The real work is done in the constructor for
341    gcc::jit::recording::context in jit-recording.c. */
342 
343 gcc_jit_context *
344 gcc_jit_context_acquire (void)
345 {
346   gcc_jit_context *ctxt = new gcc_jit_context (NULL);
347   ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
348   return ctxt;
349 }
350 
351 /* Public entrypoint for releasing a gcc_jit_context.
352    The real work is done in the destructor for
353    gcc::jit::recording::context in jit-recording.c.  */
354 
355 void
356 gcc_jit_context_release (gcc_jit_context *ctxt)
357 {
358   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
359   JIT_LOG_FUNC (ctxt->get_logger ());
360   ctxt->log ("deleting ctxt: %p", (void *)ctxt);
361   delete ctxt;
362 }
363 
364 /* Public entrypoint for creating a child context within
365    PARENT_CTXT.  See description in libgccjit.h.
366 
367    The real work is done in the constructor for
368    gcc::jit::recording::context in jit-recording.c. */
369 
370 gcc_jit_context *
371 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
372 {
373   RETURN_NULL_IF_FAIL (parent_ctxt, NULL, NULL, "NULL parent ctxt");
374   JIT_LOG_FUNC (parent_ctxt->get_logger ());
375   parent_ctxt->log ("parent_ctxt: %p", (void *)parent_ctxt);
376   gcc_jit_context *child_ctxt = new gcc_jit_context (parent_ctxt);
377   child_ctxt->log ("new child_ctxt: %p", (void *)child_ctxt);
378   return child_ctxt;
379 }
380 
381 /* Public entrypoint.  See description in libgccjit.h.
382 
383    After error-checking, the real work is done by the
384      gcc::jit::recording::context::new_location
385    method in jit-recording.c.  */
386 
387 gcc_jit_location *
388 gcc_jit_context_new_location (gcc_jit_context *ctxt,
389 			      const char *filename,
390 			      int line,
391 			      int column)
392 {
393   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
394   JIT_LOG_FUNC (ctxt->get_logger ());
395   return (gcc_jit_location *)ctxt->new_location (filename, line, column, true);
396 }
397 
398 /* Public entrypoint.  See description in libgccjit.h.
399 
400    After error-checking, this calls the trivial
401    gcc::jit::recording::memento::as_object method (a location is a
402    memento), in jit-recording.h.  */
403 
404 gcc_jit_object *
405 gcc_jit_location_as_object (gcc_jit_location *loc)
406 {
407   RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
408 
409   return static_cast <gcc_jit_object *> (loc->as_object ());
410 }
411 
412 /* Public entrypoint.  See description in libgccjit.h.
413 
414    After error-checking, this calls the trivial
415    gcc::jit::recording::memento::as_object method (a type is a
416    memento), in jit-recording.h.  */
417 
418 gcc_jit_object *
419 gcc_jit_type_as_object (gcc_jit_type *type)
420 {
421   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
422 
423   return static_cast <gcc_jit_object *> (type->as_object ());
424 }
425 
426 /* Public entrypoint for getting a specific type from a context.
427 
428    After error-checking, the real work is done by the
429    gcc::jit::recording::context::get_type method, in
430    jit-recording.c  */
431 
432 gcc_jit_type *
433 gcc_jit_context_get_type (gcc_jit_context *ctxt,
434 			  enum gcc_jit_types type)
435 {
436   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
437   JIT_LOG_FUNC (ctxt->get_logger ());
438   RETURN_NULL_IF_FAIL_PRINTF1 (
439     (type >= GCC_JIT_TYPE_VOID
440      && type <= GCC_JIT_TYPE_FILE_PTR),
441     ctxt, NULL,
442     "unrecognized value for enum gcc_jit_types: %i", type);
443 
444   return (gcc_jit_type *)ctxt->get_type (type);
445 }
446 
447 /* Public entrypoint for getting the integer type of the given size and
448    signedness.
449 
450    After error-checking, the real work is done by the
451    gcc::jit::recording::context::get_int_type method,
452    in jit-recording.c.  */
453 
454 gcc_jit_type *
455 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
456 			      int num_bytes, int is_signed)
457 {
458   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
459   JIT_LOG_FUNC (ctxt->get_logger ());
460   RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
461 
462   return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
463 }
464 
465 /* Public entrypoint.  See description in libgccjit.h.
466 
467    After error-checking, the real work is done by the
468    gcc::jit::recording::type::get_pointer method, in
469    jit-recording.c  */
470 
471 gcc_jit_type *
472 gcc_jit_type_get_pointer (gcc_jit_type *type)
473 {
474   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
475 
476   return (gcc_jit_type *)type->get_pointer ();
477 }
478 
479 /* Public entrypoint.  See description in libgccjit.h.
480 
481    After error-checking, the real work is done by the
482    gcc::jit::recording::type::get_const method, in
483    jit-recording.c.  */
484 
485 gcc_jit_type *
486 gcc_jit_type_get_const (gcc_jit_type *type)
487 {
488   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
489 
490   return (gcc_jit_type *)type->get_const ();
491 }
492 
493 /* Public entrypoint.  See description in libgccjit.h.
494 
495    After error-checking, the real work is done by the
496    gcc::jit::recording::type::get_volatile method, in
497    jit-recording.c.  */
498 
499 gcc_jit_type *
500 gcc_jit_type_get_volatile (gcc_jit_type *type)
501 {
502   RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
503 
504   return (gcc_jit_type *)type->get_volatile ();
505 }
506 
507 /* Public entrypoint.  See description in libgccjit.h.
508 
509    After error-checking, the real work is done by the
510    gcc::jit::recording::context::new_array_type method, in
511    jit-recording.c.  */
512 
513 gcc_jit_type *
514 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
515 				gcc_jit_location *loc,
516 				gcc_jit_type *element_type,
517 				int num_elements)
518 {
519   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
520   JIT_LOG_FUNC (ctxt->get_logger ());
521   /* LOC can be NULL.  */
522   RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
523   RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
524 
525   return (gcc_jit_type *)ctxt->new_array_type (loc,
526 					       element_type,
527 					       num_elements);
528 }
529 
530 /* Public entrypoint.  See description in libgccjit.h.
531 
532    After error-checking, the real work is done by the
533    gcc::jit::recording::context::new_field method, in
534    jit-recording.c.  */
535 
536 gcc_jit_field *
537 gcc_jit_context_new_field (gcc_jit_context *ctxt,
538 			   gcc_jit_location *loc,
539 			   gcc_jit_type *type,
540 			   const char *name)
541 {
542   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
543   JIT_LOG_FUNC (ctxt->get_logger ());
544   /* LOC can be NULL.  */
545   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
546   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
547   RETURN_NULL_IF_FAIL_PRINTF2 (
548     type->has_known_size (),
549     ctxt, loc,
550     "unknown size for field \"%s\" (type: %s)",
551     name,
552     type->get_debug_string ());
553 
554   return (gcc_jit_field *)ctxt->new_field (loc, type, name);
555 }
556 
557 /* Public entrypoint.  See description in libgccjit.h.
558 
559    After error-checking, this calls the trivial
560    gcc::jit::recording::memento::as_object method (a field is a
561    memento), in jit-recording.h.  */
562 
563 gcc_jit_object *
564 gcc_jit_field_as_object (gcc_jit_field *field)
565 {
566   RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
567 
568   return static_cast <gcc_jit_object *> (field->as_object ());
569 }
570 
571 /* Public entrypoint.  See description in libgccjit.h.
572 
573    After error-checking, the real work is done by the
574    gcc::jit::recording::context::new_struct_type method,
575    immediately followed by a "set_fields" call on the resulting
576    gcc::jit::recording::compound_type *, both in jit-recording.c  */
577 
578 gcc_jit_struct *
579 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
580 				 gcc_jit_location *loc,
581 				 const char *name,
582 				 int num_fields,
583 				 gcc_jit_field **fields)
584 {
585   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
586   JIT_LOG_FUNC (ctxt->get_logger ());
587   /* LOC can be NULL.  */
588   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
589   if (num_fields)
590     RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
591   for (int i = 0; i < num_fields; i++)
592     {
593       RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
594       RETURN_NULL_IF_FAIL_PRINTF2 (
595 	NULL == fields[i]->get_container (),
596 	ctxt, loc,
597 	"%s is already a field of %s",
598 	fields[i]->get_debug_string (),
599 	fields[i]->get_container ()->get_debug_string ());
600     }
601 
602   gcc::jit::recording::struct_ *result =
603     ctxt->new_struct_type (loc, name);
604   result->set_fields (loc,
605 		      num_fields,
606 		      (gcc::jit::recording::field **)fields);
607   return static_cast<gcc_jit_struct *> (result);
608 }
609 
610 /* Public entrypoint.  See description in libgccjit.h.
611 
612    After error-checking, the real work is done by the
613    gcc::jit::recording::context::new_struct_type method in
614    jit-recording.c.  */
615 
616 gcc_jit_struct *
617 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
618 				   gcc_jit_location *loc,
619 				   const char *name)
620 {
621   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
622   JIT_LOG_FUNC (ctxt->get_logger ());
623   /* LOC can be NULL.  */
624   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
625 
626   return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
627 }
628 
629 /* Public entrypoint.  See description in libgccjit.h.
630 
631    After error-checking, this calls the trivial
632    gcc::jit::recording::struct_::as_object method in
633    jit-recording.h.  */
634 
635 gcc_jit_type *
636 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
637 {
638   RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
639 
640   return static_cast <gcc_jit_type *> (struct_type->as_type ());
641 }
642 
643 /* Public entrypoint.  See description in libgccjit.h.
644 
645    After error-checking, the real work is done by the
646    gcc::jit::recording::compound_type::set_fields method in
647    jit-recording.c.  */
648 
649 void
650 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
651 			   gcc_jit_location *loc,
652 			   int num_fields,
653 			   gcc_jit_field **fields)
654 {
655   RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
656   gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
657   JIT_LOG_FUNC (ctxt->get_logger ());
658   /* LOC can be NULL.  */
659   RETURN_IF_FAIL_PRINTF1 (
660     NULL == struct_type->get_fields (), ctxt, loc,
661     "%s already has had fields set",
662     struct_type->get_debug_string ());
663   if (num_fields)
664     RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
665   for (int i = 0; i < num_fields; i++)
666     {
667       RETURN_IF_FAIL_PRINTF2 (
668 	fields[i],
669 	ctxt, loc,
670 	"%s: NULL field ptr at index %i",
671 	struct_type->get_debug_string (),
672 	i);
673       RETURN_IF_FAIL_PRINTF2 (
674 	NULL == fields[i]->get_container (),
675 	ctxt, loc,
676 	"%s is already a field of %s",
677 	fields[i]->get_debug_string (),
678 	fields[i]->get_container ()->get_debug_string ());
679     }
680 
681   struct_type->set_fields (loc, num_fields,
682 			   (gcc::jit::recording::field **)fields);
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::context::new_union_type method,
689    immediately followed by a "set_fields" call on the resulting
690    gcc::jit::recording::compound_type *, both in jit-recording.c  */
691 
692 gcc_jit_type *
693 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
694 				gcc_jit_location *loc,
695 				const char *name,
696 				int num_fields,
697 				gcc_jit_field **fields)
698 {
699   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
700   JIT_LOG_FUNC (ctxt->get_logger ());
701   /* LOC can be NULL.  */
702   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
703   if (num_fields)
704     RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
705   for (int i = 0; i < num_fields; i++)
706     {
707       RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
708       RETURN_NULL_IF_FAIL_PRINTF2 (
709 	NULL == fields[i]->get_container (),
710 	ctxt, loc,
711 	"%s is already a field of %s",
712 	fields[i]->get_debug_string (),
713 	fields[i]->get_container ()->get_debug_string ());
714     }
715 
716   gcc::jit::recording::union_ *result =
717     ctxt->new_union_type (loc, name);
718   result->set_fields (loc,
719 		      num_fields,
720 		      (gcc::jit::recording::field **)fields);
721   return (gcc_jit_type *) (result);
722 }
723 
724 /* Public entrypoint.  See description in libgccjit.h.
725 
726    After error-checking, the real work is done by the
727    gcc::jit::recording::context::new_function_ptr_type method,
728    in jit-recording.c  */
729 
730 gcc_jit_type *
731 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
732 				       gcc_jit_location *loc,
733 				       gcc_jit_type *return_type,
734 				       int num_params,
735 				       gcc_jit_type **param_types,
736 				       int is_variadic)
737 {
738   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
739   JIT_LOG_FUNC (ctxt->get_logger ());
740   /* LOC can be NULL.  */
741   RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
742   RETURN_NULL_IF_FAIL (
743     (num_params == 0) || param_types,
744     ctxt, loc,
745     "NULL param_types creating function pointer type");
746   for (int i = 0; i < num_params; i++)
747     RETURN_NULL_IF_FAIL_PRINTF1 (
748       param_types[i],
749       ctxt, loc,
750       "NULL parameter type %i creating function pointer type", i);
751 
752   return (gcc_jit_type*)
753     ctxt->new_function_ptr_type (loc, return_type,
754 				 num_params,
755 				 (gcc::jit::recording::type **)param_types,
756 				 is_variadic);
757 }
758 
759 /* Constructing functions.  */
760 
761 /* Public entrypoint.  See description in libgccjit.h.
762 
763    After error-checking, the real work is done by the
764    gcc::jit::recording::context::new_param method, in jit-recording.c  */
765 
766 gcc_jit_param *
767 gcc_jit_context_new_param (gcc_jit_context *ctxt,
768 			   gcc_jit_location *loc,
769 			   gcc_jit_type *type,
770 			   const char *name)
771 {
772   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
773   JIT_LOG_FUNC (ctxt->get_logger ());
774   /* LOC can be NULL.  */
775   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
776   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
777 
778   return (gcc_jit_param *)ctxt->new_param (loc, type, name);
779 }
780 
781 /* Public entrypoint.  See description in libgccjit.h.
782 
783    After error-checking, this calls the trivial
784    gcc::jit::recording::memento::as_object method (a param is a memento),
785    in jit-recording.h.  */
786 
787 gcc_jit_object *
788 gcc_jit_param_as_object (gcc_jit_param *param)
789 {
790   RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
791 
792   return static_cast <gcc_jit_object *> (param->as_object ());
793 }
794 
795 /* Public entrypoint.  See description in libgccjit.h.
796 
797    After error-checking, this calls the trivial
798    gcc::jit::recording::param::as_lvalue method in jit-recording.h.  */
799 
800 gcc_jit_lvalue *
801 gcc_jit_param_as_lvalue (gcc_jit_param *param)
802 {
803   RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
804 
805   return (gcc_jit_lvalue *)param->as_lvalue ();
806 }
807 
808 /* Public entrypoint.  See description in libgccjit.h.
809 
810    After error-checking, this calls the trivial
811    gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
812    in jit-recording.h.  */
813 
814 gcc_jit_rvalue *
815 gcc_jit_param_as_rvalue (gcc_jit_param *param)
816 {
817   RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
818 
819   return (gcc_jit_rvalue *)param->as_rvalue ();
820 }
821 
822 /* Public entrypoint.  See description in libgccjit.h.
823 
824    After error-checking, the real work is done by the
825    gcc::jit::recording::context::new_function method, in
826    jit-recording.c.  */
827 
828 gcc_jit_function *
829 gcc_jit_context_new_function (gcc_jit_context *ctxt,
830 			      gcc_jit_location *loc,
831 			      enum gcc_jit_function_kind kind,
832 			      gcc_jit_type *return_type,
833 			      const char *name,
834 			      int num_params,
835 			      gcc_jit_param **params,
836 			      int is_variadic)
837 {
838   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
839   JIT_LOG_FUNC (ctxt->get_logger ());
840   /* LOC can be NULL.  */
841   RETURN_NULL_IF_FAIL_PRINTF1 (
842     ((kind >= GCC_JIT_FUNCTION_EXPORTED)
843      && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
844     ctxt, loc,
845     "unrecognized value for enum gcc_jit_function_kind: %i",
846     kind);
847   RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
848   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
849   /* The assembler can only handle certain names, so for now, enforce
850      C's rules for identiers upon the name, using ISALPHA and ISALNUM
851      from safe-ctype.h to ignore the current locale.
852      Eventually we'll need some way to interact with e.g. C++ name
853      mangling.  */
854   {
855     /* Leading char: */
856     char ch = *name;
857     RETURN_NULL_IF_FAIL_PRINTF2 (
858 	ISALPHA (ch) || ch == '_',
859 	ctxt, loc,
860 	"name \"%s\" contains invalid character: '%c'",
861 	name, ch);
862     /* Subsequent chars: */
863     for (const char *ptr = name + 1; (ch = *ptr); ptr++)
864       {
865 	RETURN_NULL_IF_FAIL_PRINTF2 (
866 	  ISALNUM (ch) || ch == '_',
867 	  ctxt, loc,
868 	  "name \"%s\" contains invalid character: '%c'",
869 	  name, ch);
870       }
871   }
872   RETURN_NULL_IF_FAIL_PRINTF1 (
873     (num_params == 0) || params,
874     ctxt, loc,
875     "NULL params creating function %s", name);
876   for (int i = 0; i < num_params; i++)
877     {
878       RETURN_NULL_IF_FAIL_PRINTF2 (
879 	params[i],
880 	ctxt, loc,
881 	"NULL parameter %i creating function %s", i, name);
882       RETURN_NULL_IF_FAIL_PRINTF5 (
883 	(NULL == params[i]->get_scope ()),
884 	ctxt, loc,
885 	"parameter %i \"%s\""
886 	" (type: %s)"
887 	" for function %s"
888 	" was already used for function %s",
889 	i, params[i]->get_debug_string (),
890 	params[i]->get_type ()->get_debug_string (),
891 	name,
892 	params[i]->get_scope ()->get_debug_string ());
893     }
894 
895   return (gcc_jit_function*)
896     ctxt->new_function (loc, kind, return_type, name,
897 			num_params,
898 			(gcc::jit::recording::param **)params,
899 			is_variadic,
900 			BUILT_IN_NONE);
901 }
902 
903 /* Public entrypoint.  See description in libgccjit.h.
904 
905    After error-checking, the real work is done by the
906    gcc::jit::recording::context::get_builtin_function method, in
907    jit-recording.c.  */
908 
909 gcc_jit_function *
910 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
911 				      const char *name)
912 {
913   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
914   JIT_LOG_FUNC (ctxt->get_logger ());
915   RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
916 
917   return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
918 }
919 
920 /* Public entrypoint.  See description in libgccjit.h.
921 
922    After error-checking, this calls the trivial
923    gcc::jit::recording::memento::as_object method (a function is a
924    memento), in jit-recording.h.  */
925 
926 gcc_jit_object *
927 gcc_jit_function_as_object (gcc_jit_function *func)
928 {
929   RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
930 
931   return static_cast <gcc_jit_object *> (func->as_object ());
932 }
933 
934 /* Public entrypoint.  See description in libgccjit.h.
935 
936    After error-checking, the real work is done by the
937    gcc::jit::recording::function::get_param method, in
938    jit-recording.h.  */
939 
940 gcc_jit_param *
941 gcc_jit_function_get_param (gcc_jit_function *func, int index)
942 {
943   RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
944   gcc::jit::recording::context *ctxt = func->m_ctxt;
945   JIT_LOG_FUNC (ctxt->get_logger ());
946   RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
947   int num_params = func->get_params ().length ();
948   RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
949 			       ctxt, NULL,
950 			       "index of %d is too large (%s has %d params)",
951 			       index,
952 			       func->get_debug_string (),
953 			       num_params);
954 
955   return static_cast <gcc_jit_param *> (func->get_param (index));
956 }
957 
958 /* Public entrypoint.  See description in libgccjit.h.
959 
960    After error-checking, the real work is done by the
961    gcc::jit::recording::function::dump_to_dot method, in
962    jit-recording.c.  */
963 
964 void
965 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
966 			      const char *path)
967 {
968   RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
969   gcc::jit::recording::context *ctxt = func->m_ctxt;
970   JIT_LOG_FUNC (ctxt->get_logger ());
971   RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
972 
973   func->dump_to_dot (path);
974 }
975 
976 /* Public entrypoint.  See description in libgccjit.h.
977 
978    After error-checking, the real work is done by the
979    gcc::jit::recording::function::new_block method, in
980    jit-recording.c.  */
981 
982 gcc_jit_block*
983 gcc_jit_function_new_block (gcc_jit_function *func,
984 			    const char *name)
985 {
986   RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
987   JIT_LOG_FUNC (func->get_context ()->get_logger ());
988   RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
989 		       func->get_context (), NULL,
990 		       "cannot add block to an imported function");
991   /* name can be NULL.  */
992 
993   return (gcc_jit_block *)func->new_block (name);
994 }
995 
996 /* Public entrypoint.  See description in libgccjit.h.
997 
998    After error-checking, this calls the trivial
999    gcc::jit::recording::memento::as_object method (a block is a
1000    memento), in jit-recording.h.  */
1001 
1002 gcc_jit_object *
1003 gcc_jit_block_as_object (gcc_jit_block *block)
1004 {
1005   RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1006 
1007   return static_cast <gcc_jit_object *> (block->as_object ());
1008 }
1009 
1010 /* Public entrypoint.  See description in libgccjit.h.
1011 
1012    After error-checking, the real work is done by the
1013    gcc::jit::recording::block::get_function method, in
1014    jit-recording.h.  */
1015 
1016 gcc_jit_function *
1017 gcc_jit_block_get_function (gcc_jit_block *block)
1018 {
1019   RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1020 
1021   return static_cast <gcc_jit_function *> (block->get_function ());
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::context::new_global method, in
1028    jit-recording.c.  */
1029 
1030 gcc_jit_lvalue *
1031 gcc_jit_context_new_global (gcc_jit_context *ctxt,
1032 			    gcc_jit_location *loc,
1033 			    enum gcc_jit_global_kind kind,
1034 			    gcc_jit_type *type,
1035 			    const char *name)
1036 {
1037   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1038   JIT_LOG_FUNC (ctxt->get_logger ());
1039   /* LOC can be NULL.  */
1040   RETURN_NULL_IF_FAIL_PRINTF1 (
1041     ((kind >= GCC_JIT_GLOBAL_EXPORTED)
1042      && (kind <= GCC_JIT_GLOBAL_IMPORTED)),
1043     ctxt, loc,
1044     "unrecognized value for enum gcc_jit_global_kind: %i",
1045     kind);
1046   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1047   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1048   RETURN_NULL_IF_FAIL_PRINTF2 (
1049     type->has_known_size (),
1050     ctxt, loc,
1051     "unknown size for global \"%s\" (type: %s)",
1052     name,
1053     type->get_debug_string ());
1054 
1055   return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
1056 }
1057 
1058 /* Public entrypoint.  See description in libgccjit.h.
1059 
1060    After error-checking, this calls the trivial
1061    gcc::jit::recording::memento::as_object method (an lvalue is a
1062    memento), in jit-recording.h.  */
1063 
1064 gcc_jit_object *
1065 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1066 {
1067   RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1068 
1069   return static_cast <gcc_jit_object *> (lvalue->as_object ());
1070 }
1071 
1072 /* Public entrypoint.  See description in libgccjit.h.
1073 
1074    After error-checking, this calls the trivial
1075    gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h.  */
1076 
1077 gcc_jit_rvalue *
1078 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1079 {
1080   RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1081 
1082   return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1083 }
1084 
1085 /* Public entrypoint.  See description in libgccjit.h.
1086 
1087    After error-checking, this calls the trivial
1088    gcc::jit::recording::memento::as_object method (an rvalue is a
1089    memento), in jit-recording.h.  */
1090 
1091 gcc_jit_object *
1092 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1093 {
1094   RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1095 
1096   return static_cast <gcc_jit_object *> (rvalue->as_object ());
1097 }
1098 
1099 /* Public entrypoint.  See description in libgccjit.h.
1100 
1101    After error-checking, the real work is done by the
1102    gcc::jit::recording::rvalue::get_type method, in
1103    jit-recording.h.  */
1104 
1105 gcc_jit_type *
1106 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1107 {
1108   RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1109 
1110   return static_cast <gcc_jit_type *> (rvalue->get_type ());
1111 }
1112 
1113 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1114    type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1115    result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT).  */
1116 
1117 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1118   RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1119   RETURN_NULL_IF_FAIL_PRINTF1 (                                \
1120     NUMERIC_TYPE->is_numeric (), ctxt, NULL,                   \
1121     "not a numeric type: %s",                                  \
1122     NUMERIC_TYPE->get_debug_string ());
1123 
1124 /* Public entrypoint.  See description in libgccjit.h.
1125 
1126    After error-checking, the real work is done by the
1127    gcc::jit::recording::context::new_rvalue_from_int method in
1128    jit-recording.c.  */
1129 
1130 gcc_jit_rvalue *
1131 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1132 				     gcc_jit_type *numeric_type,
1133 				     int value)
1134 {
1135   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1136   JIT_LOG_FUNC (ctxt->get_logger ());
1137   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1138 
1139   return ((gcc_jit_rvalue *)ctxt
1140 	  ->new_rvalue_from_const <int> (numeric_type, value));
1141 }
1142 
1143 /* FIXME. */
1144 
1145 gcc_jit_rvalue *
1146 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1147 				      gcc_jit_type *numeric_type,
1148 				      long value)
1149 {
1150   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1151   JIT_LOG_FUNC (ctxt->get_logger ());
1152   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1153 
1154   return ((gcc_jit_rvalue *)ctxt
1155 	  ->new_rvalue_from_const <long> (numeric_type, value));
1156 }
1157 
1158 /* Public entrypoint.  See description in libgccjit.h.
1159 
1160    This is essentially equivalent to:
1161       gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1162    albeit with slightly different error messages if an error occurs.  */
1163 
1164 gcc_jit_rvalue *
1165 gcc_jit_context_zero (gcc_jit_context *ctxt,
1166 		      gcc_jit_type *numeric_type)
1167 {
1168   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1169   JIT_LOG_FUNC (ctxt->get_logger ());
1170   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1171 
1172   return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1173 }
1174 
1175 /* Public entrypoint.  See description in libgccjit.h.
1176 
1177    This is essentially equivalent to:
1178       gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1179    albeit with slightly different error messages if an error occurs.  */
1180 
1181 gcc_jit_rvalue *
1182 gcc_jit_context_one (gcc_jit_context *ctxt,
1183 		     gcc_jit_type *numeric_type)
1184 {
1185   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1186   JIT_LOG_FUNC (ctxt->get_logger ());
1187   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1188 
1189   return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1190 }
1191 
1192 /* Public entrypoint.  See description in libgccjit.h.
1193 
1194    After error-checking, the real work is done by the
1195    gcc::jit::recording::context::new_rvalue_from_double method in
1196    jit-recording.c.  */
1197 
1198 gcc_jit_rvalue *
1199 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1200 					gcc_jit_type *numeric_type,
1201 					double value)
1202 {
1203   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1204   JIT_LOG_FUNC (ctxt->get_logger ());
1205   RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1206 
1207   return ((gcc_jit_rvalue *)ctxt
1208 	  ->new_rvalue_from_const <double> (numeric_type, value));
1209 }
1210 
1211 /* Public entrypoint.  See description in libgccjit.h.
1212 
1213    After error-checking, the real work is done by the
1214    gcc::jit::recording::context::new_rvalue_from_ptr method in
1215    jit-recording.c.  */
1216 
1217 gcc_jit_rvalue *
1218 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1219 				     gcc_jit_type *pointer_type,
1220 				     void *value)
1221 {
1222   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1223   JIT_LOG_FUNC (ctxt->get_logger ());
1224   RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1225   RETURN_NULL_IF_FAIL_PRINTF1 (
1226     pointer_type->is_pointer (),
1227     ctxt, NULL,
1228     "not a pointer type (type: %s)",
1229     pointer_type->get_debug_string ());
1230 
1231   return ((gcc_jit_rvalue *)ctxt
1232 	  ->new_rvalue_from_const <void *> (pointer_type, value));
1233 }
1234 
1235 /* Public entrypoint.  See description in libgccjit.h.
1236 
1237    This is essentially equivalent to:
1238       gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1239    albeit with slightly different error messages if an error occurs.  */
1240 
1241 gcc_jit_rvalue *
1242 gcc_jit_context_null (gcc_jit_context *ctxt,
1243 		      gcc_jit_type *pointer_type)
1244 {
1245   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1246   JIT_LOG_FUNC (ctxt->get_logger ());
1247   RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1248   RETURN_NULL_IF_FAIL_PRINTF1 (
1249     pointer_type->is_pointer (),
1250     ctxt, NULL,
1251     "not a pointer type (type: %s)",
1252     pointer_type->get_debug_string ());
1253 
1254   return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1255 }
1256 
1257 /* Public entrypoint.  See description in libgccjit.h.
1258 
1259    After error-checking, the real work is done by the
1260    gcc::jit::recording::context::new_string_literal method in
1261    jit-recording.c.  */
1262 
1263 gcc_jit_rvalue *
1264 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1265 				    const char *value)
1266 {
1267   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1268   JIT_LOG_FUNC (ctxt->get_logger ());
1269   RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1270 
1271   return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1272 }
1273 
1274 /* Public entrypoint.  See description in libgccjit.h.
1275 
1276    After error-checking, the real work is done by the
1277    gcc::jit::recording::context::new_unary_op method in
1278    jit-recording.c.  */
1279 
1280 gcc_jit_rvalue *
1281 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1282 			      gcc_jit_location *loc,
1283 			      enum gcc_jit_unary_op op,
1284 			      gcc_jit_type *result_type,
1285 			      gcc_jit_rvalue *rvalue)
1286 {
1287   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1288   JIT_LOG_FUNC (ctxt->get_logger ());
1289   /* LOC can be NULL.  */
1290   RETURN_NULL_IF_FAIL_PRINTF1 (
1291     (op >= GCC_JIT_UNARY_OP_MINUS
1292      && op <= GCC_JIT_UNARY_OP_ABS),
1293     ctxt, loc,
1294     "unrecognized value for enum gcc_jit_unary_op: %i",
1295     op);
1296   RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1297   RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1298 
1299   return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1300 }
1301 
1302 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1303    For use by both gcc_jit_context_new_binary_op and
1304    gcc_jit_block_add_assignment_op.  */
1305 
1306 static bool
1307 valid_binary_op_p (enum gcc_jit_binary_op op)
1308 {
1309   return (op >= GCC_JIT_BINARY_OP_PLUS
1310 	  && op <= GCC_JIT_BINARY_OP_RSHIFT);
1311 }
1312 
1313 /* Public entrypoint.  See description in libgccjit.h.
1314 
1315    After error-checking, the real work is done by the
1316    gcc::jit::recording::context::new_binary_op method in
1317    jit-recording.c.  */
1318 
1319 gcc_jit_rvalue *
1320 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1321 			       gcc_jit_location *loc,
1322 			       enum gcc_jit_binary_op op,
1323 			       gcc_jit_type *result_type,
1324 			       gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1325 {
1326   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1327   JIT_LOG_FUNC (ctxt->get_logger ());
1328   /* LOC can be NULL.  */
1329   RETURN_NULL_IF_FAIL_PRINTF1 (
1330     valid_binary_op_p (op),
1331     ctxt, loc,
1332     "unrecognized value for enum gcc_jit_binary_op: %i",
1333     op);
1334   RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1335   RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1336   RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1337   RETURN_NULL_IF_FAIL_PRINTF4 (
1338     a->get_type () == b->get_type (),
1339     ctxt, loc,
1340     "mismatching types for binary op:"
1341     " a: %s (type: %s) b: %s (type: %s)",
1342     a->get_debug_string (),
1343     a->get_type ()->get_debug_string (),
1344     b->get_debug_string (),
1345     b->get_type ()->get_debug_string ());
1346 
1347   return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1348 }
1349 
1350 /* Public entrypoint.  See description in libgccjit.h.
1351 
1352    After error-checking, the real work is done by the
1353    gcc::jit::recording::context::new_comparison method in
1354    jit-recording.c.  */
1355 
1356 gcc_jit_rvalue *
1357 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1358 				gcc_jit_location *loc,
1359 				enum gcc_jit_comparison op,
1360 				gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1361 {
1362   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1363   JIT_LOG_FUNC (ctxt->get_logger ());
1364   /* LOC can be NULL.  */
1365   RETURN_NULL_IF_FAIL_PRINTF1 (
1366     (op >= GCC_JIT_COMPARISON_EQ
1367      && op <= GCC_JIT_COMPARISON_GE),
1368     ctxt, loc,
1369     "unrecognized value for enum gcc_jit_comparison: %i",
1370     op);
1371   RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1372   RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1373   RETURN_NULL_IF_FAIL_PRINTF4 (
1374     a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1375     ctxt, loc,
1376     "mismatching types for comparison:"
1377     " a: %s (type: %s) b: %s (type: %s)",
1378     a->get_debug_string (),
1379     a->get_type ()->get_debug_string (),
1380     b->get_debug_string (),
1381     b->get_type ()->get_debug_string ());
1382 
1383   return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1384 }
1385 
1386 /* Public entrypoint.  See description in libgccjit.h.
1387 
1388    After error-checking, the real work is done by the
1389    gcc::jit::recording::context::new_call method in
1390    jit-recording.c.  */
1391 
1392 gcc_jit_rvalue *
1393 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1394 			  gcc_jit_location *loc,
1395 			  gcc_jit_function *func,
1396 			  int numargs , gcc_jit_rvalue **args)
1397 {
1398   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1399   JIT_LOG_FUNC (ctxt->get_logger ());
1400   /* LOC can be NULL.  */
1401   RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1402   if (numargs)
1403     RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1404 
1405   int min_num_params = func->get_params ().length ();
1406   bool is_variadic = func->is_variadic ();
1407 
1408   RETURN_NULL_IF_FAIL_PRINTF3 (
1409     numargs >= min_num_params,
1410     ctxt, loc,
1411     "not enough arguments to function \"%s\""
1412     " (got %i args, expected %i)",
1413     func->get_name ()->c_str (),
1414     numargs, min_num_params);
1415 
1416   RETURN_NULL_IF_FAIL_PRINTF3 (
1417     (numargs == min_num_params || is_variadic),
1418     ctxt, loc,
1419     "too many arguments to function \"%s\""
1420     " (got %i args, expected %i)",
1421     func->get_name ()->c_str (),
1422     numargs, min_num_params);
1423 
1424   for (int i = 0; i < min_num_params; i++)
1425     {
1426       gcc::jit::recording::param *param = func->get_param (i);
1427       gcc_jit_rvalue *arg = args[i];
1428 
1429       RETURN_NULL_IF_FAIL_PRINTF4 (
1430 	arg,
1431 	ctxt, loc,
1432 	"NULL argument %i to function \"%s\":"
1433 	" param %s (type: %s)",
1434 	i + 1,
1435 	func->get_name ()->c_str (),
1436 	param->get_debug_string (),
1437 	param->get_type ()->get_debug_string ());
1438 
1439       RETURN_NULL_IF_FAIL_PRINTF6 (
1440 	compatible_types (param->get_type (),
1441 			  arg->get_type ()),
1442 	ctxt, loc,
1443 	"mismatching types for argument %d of function \"%s\":"
1444 	" assignment to param %s (type: %s) from %s (type: %s)",
1445 	i + 1,
1446 	func->get_name ()->c_str (),
1447 	param->get_debug_string (),
1448 	param->get_type ()->get_debug_string (),
1449 	arg->get_debug_string (),
1450 	arg->get_type ()->get_debug_string ());
1451     }
1452 
1453   return (gcc_jit_rvalue *)ctxt->new_call (loc,
1454 					   func,
1455 					   numargs,
1456 					   (gcc::jit::recording::rvalue **)args);
1457 }
1458 
1459 /* Public entrypoint.  See description in libgccjit.h.
1460 
1461    After error-checking, the real work is done by the
1462    gcc::jit::recording::context::new_call_through_ptr method in
1463    jit-recording.c.  */
1464 
1465 gcc_jit_rvalue *
1466 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1467 				      gcc_jit_location *loc,
1468 				      gcc_jit_rvalue *fn_ptr,
1469 				      int numargs, gcc_jit_rvalue **args)
1470 {
1471   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1472   JIT_LOG_FUNC (ctxt->get_logger ());
1473   /* LOC can be NULL.  */
1474   RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1475   if (numargs)
1476     RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1477 
1478   gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1479   RETURN_NULL_IF_FAIL_PRINTF2 (
1480     ptr_type, ctxt, loc,
1481     "fn_ptr is not a ptr: %s"
1482     " type: %s",
1483     fn_ptr->get_debug_string (),
1484     fn_ptr->get_type ()->get_debug_string ());
1485 
1486   gcc::jit::recording::function_type *fn_type =
1487     ptr_type->dyn_cast_function_type();
1488   RETURN_NULL_IF_FAIL_PRINTF2 (
1489     fn_type, ctxt, loc,
1490     "fn_ptr is not a function ptr: %s"
1491     " type: %s",
1492     fn_ptr->get_debug_string (),
1493     fn_ptr->get_type ()->get_debug_string ());
1494 
1495   int min_num_params = fn_type->get_param_types ().length ();
1496   bool is_variadic = fn_type->is_variadic ();
1497 
1498   RETURN_NULL_IF_FAIL_PRINTF3 (
1499     numargs >= min_num_params,
1500     ctxt, loc,
1501     "not enough arguments to fn_ptr: %s"
1502     " (got %i args, expected %i)",
1503     fn_ptr->get_debug_string (),
1504     numargs, min_num_params);
1505 
1506   RETURN_NULL_IF_FAIL_PRINTF3 (
1507     (numargs == min_num_params || is_variadic),
1508     ctxt, loc,
1509     "too many arguments to fn_ptr: %s"
1510     " (got %i args, expected %i)",
1511     fn_ptr->get_debug_string (),
1512     numargs, min_num_params);
1513 
1514   for (int i = 0; i < min_num_params; i++)
1515     {
1516       gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1517       gcc_jit_rvalue *arg = args[i];
1518 
1519       RETURN_NULL_IF_FAIL_PRINTF3 (
1520 	arg,
1521 	ctxt, loc,
1522 	"NULL argument %i to fn_ptr: %s"
1523 	" (type: %s)",
1524 	i + 1,
1525 	fn_ptr->get_debug_string (),
1526 	param_type->get_debug_string ());
1527 
1528       RETURN_NULL_IF_FAIL_PRINTF6 (
1529 	compatible_types (param_type,
1530 			  arg->get_type ()),
1531 	ctxt, loc,
1532 	"mismatching types for argument %d of fn_ptr: %s:"
1533 	" assignment to param %d (type: %s) from %s (type: %s)",
1534 	i + 1,
1535 	fn_ptr->get_debug_string (),
1536 	i + 1,
1537 	param_type->get_debug_string (),
1538 	arg->get_debug_string (),
1539 	arg->get_type ()->get_debug_string ());
1540     }
1541 
1542   return (gcc_jit_rvalue *)(
1543 	    ctxt->new_call_through_ptr (loc,
1544 					fn_ptr,
1545 					numargs,
1546 					(gcc::jit::recording::rvalue **)args));
1547 }
1548 
1549 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1550    to DST_TYPE, for use by gcc_jit_context_new_cast.
1551 
1552    We only permit these kinds of cast:
1553 
1554      int <-> float
1555      int <-> bool
1556      P*  <-> Q*   for pointer types P and Q.  */
1557 
1558 static bool
1559 is_valid_cast (gcc::jit::recording::type *src_type,
1560 	       gcc_jit_type *dst_type)
1561 {
1562   bool src_is_int = src_type->is_int ();
1563   bool dst_is_int = dst_type->is_int ();
1564   bool src_is_float = src_type->is_float ();
1565   bool dst_is_float = dst_type->is_float ();
1566   bool src_is_bool = src_type->is_bool ();
1567   bool dst_is_bool = dst_type->is_bool ();
1568 
1569   if (src_is_int)
1570     if (dst_is_int || dst_is_float || dst_is_bool)
1571       return true;
1572 
1573   if (src_is_float)
1574     if (dst_is_int || dst_is_float)
1575       return true;
1576 
1577   if (src_is_bool)
1578     if (dst_is_int || dst_is_bool)
1579       return true;
1580 
1581   /* Permit casts between pointer types.  */
1582   gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1583   gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1584   if (deref_src_type && deref_dst_type)
1585     return true;
1586 
1587   return false;
1588 }
1589 
1590 /* Public entrypoint.  See description in libgccjit.h.
1591 
1592    After error-checking, the real work is done by the
1593    gcc::jit::recording::context::new_cast method in jit-recording.c.  */
1594 
1595 gcc_jit_rvalue *
1596 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1597 			  gcc_jit_location *loc,
1598 			  gcc_jit_rvalue *rvalue,
1599 			  gcc_jit_type *type)
1600 {
1601   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1602   JIT_LOG_FUNC (ctxt->get_logger ());
1603   /* LOC can be NULL.  */
1604   RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1605   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1606   RETURN_NULL_IF_FAIL_PRINTF3 (
1607     is_valid_cast (rvalue->get_type (), type),
1608     ctxt, loc,
1609     "cannot cast %s from type: %s to type: %s",
1610     rvalue->get_debug_string (),
1611     rvalue->get_type ()->get_debug_string (),
1612     type->get_debug_string ());
1613 
1614   return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1615 }
1616 
1617 /* Public entrypoint.  See description in libgccjit.h.
1618 
1619    After error-checking, the real work is done by the
1620    gcc::jit::recording::context::new_array_access method in
1621    jit-recording.c.  */
1622 
1623 extern gcc_jit_lvalue *
1624 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1625 				  gcc_jit_location *loc,
1626 				  gcc_jit_rvalue *ptr,
1627 				  gcc_jit_rvalue *index)
1628 {
1629   RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1630   JIT_LOG_FUNC (ctxt->get_logger ());
1631   /* LOC can be NULL.  */
1632   RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1633   RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1634   RETURN_NULL_IF_FAIL_PRINTF2 (
1635     ptr->get_type ()->dereference (),
1636     ctxt, loc,
1637     "ptr: %s (type: %s) is not a pointer or array",
1638     ptr->get_debug_string (),
1639     ptr->get_type ()->get_debug_string ());
1640   RETURN_NULL_IF_FAIL_PRINTF2 (
1641     index->get_type ()->is_numeric (),
1642     ctxt, loc,
1643     "index: %s (type: %s) is not of numeric type",
1644     index->get_debug_string (),
1645     index->get_type ()->get_debug_string ());
1646 
1647   return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1648 }
1649 
1650 /* Public entrypoint.  See description in libgccjit.h.
1651 
1652    After error-checking, the real work is done by the
1653    gcc::jit::recording::memento::get_context method in
1654    jit-recording.h.  */
1655 
1656 gcc_jit_context *
1657 gcc_jit_object_get_context (gcc_jit_object *obj)
1658 {
1659   RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1660 
1661   return static_cast <gcc_jit_context *> (obj->get_context ());
1662 }
1663 
1664 /* Public entrypoint.  See description in libgccjit.h.
1665 
1666    After error-checking, the real work is done by the
1667    gcc::jit::recording::memento::get_debug_string method in
1668    jit-recording.c.  */
1669 
1670 const char *
1671 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1672 {
1673   RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1674 
1675   return obj->get_debug_string ();
1676 }
1677 
1678 /* Public entrypoint.  See description in libgccjit.h.
1679 
1680    After error-checking, the real work is done by the
1681    gcc::jit::recording::lvalue::access_field method in
1682    jit-recording.c.  */
1683 
1684 gcc_jit_lvalue *
1685 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1686 			     gcc_jit_location *loc,
1687 			     gcc_jit_field *field)
1688 {
1689   RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1690   gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1691   JIT_LOG_FUNC (ctxt->get_logger ());
1692   /* LOC can be NULL.  */
1693   RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1694   RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1695 			       "field %s has not been placed in a struct",
1696 			       field->get_debug_string ());
1697   gcc::jit::recording::type *underlying_type =
1698     struct_->get_type ();
1699   RETURN_NULL_IF_FAIL_PRINTF2 (
1700     (field->get_container ()->unqualified ()
1701      == underlying_type->unqualified ()),
1702     struct_->m_ctxt, loc,
1703     "%s is not a field of %s",
1704     field->get_debug_string (),
1705     underlying_type->get_debug_string ());
1706 
1707   return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1708 }
1709 
1710 /* Public entrypoint.  See description in libgccjit.h.
1711 
1712    After error-checking, the real work is done by the
1713    gcc::jit::recording::rvalue::access_field method in
1714    jit-recording.c.  */
1715 
1716 gcc_jit_rvalue *
1717 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1718 			     gcc_jit_location *loc,
1719 			     gcc_jit_field *field)
1720 {
1721   RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1722   gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1723   JIT_LOG_FUNC (ctxt->get_logger ());
1724   /* LOC can be NULL.  */
1725   RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1726   RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1727 			       "field %s has not been placed in a struct",
1728 			       field->get_debug_string ());
1729   gcc::jit::recording::type *underlying_type =
1730     struct_->get_type ();
1731   RETURN_NULL_IF_FAIL_PRINTF2 (
1732     (field->get_container ()->unqualified ()
1733      == underlying_type->unqualified ()),
1734     struct_->m_ctxt, loc,
1735     "%s is not a field of %s",
1736     field->get_debug_string (),
1737     underlying_type->get_debug_string ());
1738 
1739   return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1740 }
1741 
1742 /* Public entrypoint.  See description in libgccjit.h.
1743 
1744    After error-checking, the real work is done by the
1745    gcc::jit::recording::rvalue::deference_field method in
1746    jit-recording.c.  */
1747 
1748 gcc_jit_lvalue *
1749 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1750 				  gcc_jit_location *loc,
1751 				  gcc_jit_field *field)
1752 {
1753   RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1754   JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1755   /* LOC can be NULL.  */
1756   RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1757   gcc::jit::recording::type *underlying_type =
1758     ptr->get_type ()->is_pointer ();
1759   RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1760 			       "field %s has not been placed in a struct",
1761 			       field->get_debug_string ());
1762   RETURN_NULL_IF_FAIL_PRINTF3 (
1763     underlying_type,
1764     ptr->m_ctxt, loc,
1765     "dereference of non-pointer %s (type: %s) when accessing ->%s",
1766     ptr->get_debug_string (),
1767     ptr->get_type ()->get_debug_string (),
1768     field->get_debug_string ());
1769   RETURN_NULL_IF_FAIL_PRINTF2 (
1770     (field->get_container ()->unqualified ()
1771      == underlying_type->unqualified ()),
1772     ptr->m_ctxt, loc,
1773     "%s is not a field of %s",
1774     field->get_debug_string (),
1775     underlying_type->get_debug_string ());
1776 
1777   return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1778 }
1779 
1780 /* Public entrypoint.  See description in libgccjit.h.
1781 
1782    After error-checking, the real work is done by the
1783    gcc::jit::recording::rvalue::deference method in
1784    jit-recording.c.  */
1785 
1786 gcc_jit_lvalue *
1787 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1788 			    gcc_jit_location *loc)
1789 {
1790   RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1791   JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1792   /* LOC can be NULL.  */
1793 
1794   gcc::jit::recording::type *underlying_type =
1795     rvalue->get_type ()->is_pointer ();
1796 
1797   RETURN_NULL_IF_FAIL_PRINTF2 (
1798     underlying_type,
1799     rvalue->m_ctxt, loc,
1800     "dereference of non-pointer %s (type: %s)",
1801     rvalue->get_debug_string (),
1802     rvalue->get_type ()->get_debug_string ());
1803 
1804   RETURN_NULL_IF_FAIL_PRINTF2 (
1805     !underlying_type->is_void (),
1806     rvalue->m_ctxt, loc,
1807     "dereference of void pointer %s (type: %s)",
1808     rvalue->get_debug_string (),
1809     rvalue->get_type ()->get_debug_string ());
1810 
1811   return (gcc_jit_lvalue *)rvalue->dereference (loc);
1812 }
1813 
1814 /* Public entrypoint.  See description in libgccjit.h.
1815 
1816    After error-checking, the real work is done by the
1817    gcc::jit::recording::lvalue::get_address method in jit-recording.c.  */
1818 
1819 gcc_jit_rvalue *
1820 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1821 			    gcc_jit_location *loc)
1822 {
1823   RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1824   JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1825   /* LOC can be NULL.  */
1826 
1827   return (gcc_jit_rvalue *)lvalue->get_address (loc);
1828 }
1829 
1830 /* Public entrypoint.  See description in libgccjit.h.
1831 
1832    After error-checking, the real work is done by the
1833    gcc::jit::recording::function::new_local method in jit-recording.c.  */
1834 
1835 gcc_jit_lvalue *
1836 gcc_jit_function_new_local (gcc_jit_function *func,
1837 			    gcc_jit_location *loc,
1838 			    gcc_jit_type *type,
1839 			    const char *name)
1840 {
1841   RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1842   gcc::jit::recording::context *ctxt = func->m_ctxt;
1843   JIT_LOG_FUNC (ctxt->get_logger ());
1844   /* LOC can be NULL.  */
1845   RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1846 		       ctxt, loc,
1847 		       "Cannot add locals to an imported function");
1848   RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1849   RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1850   RETURN_NULL_IF_FAIL_PRINTF2 (
1851     type->has_known_size (),
1852     ctxt, loc,
1853     "unknown size for local \"%s\" (type: %s)",
1854     name,
1855     type->get_debug_string ());
1856 
1857   return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1858 }
1859 
1860 /* Public entrypoint.  See description in libgccjit.h.
1861 
1862    After error-checking, the real work is done by the
1863    gcc::jit::recording::block::add_eval method in jit-recording.c.  */
1864 
1865 void
1866 gcc_jit_block_add_eval (gcc_jit_block *block,
1867 			gcc_jit_location *loc,
1868 			gcc_jit_rvalue *rvalue)
1869 {
1870   RETURN_IF_NOT_VALID_BLOCK (block, loc);
1871   gcc::jit::recording::context *ctxt = block->get_context ();
1872   JIT_LOG_FUNC (ctxt->get_logger ());
1873   /* LOC can be NULL.  */
1874   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1875 
1876   gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
1877 
1878   /* "stmt" should be good enough to be usable in error-messages,
1879      but might still not be compilable; perform some more
1880      error-checking here.  We do this here so that the error messages
1881      can contain a stringified version of "stmt", whilst appearing
1882      as close as possible to the point of failure.  */
1883   rvalue->verify_valid_within_stmt (__func__, stmt);
1884 }
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::block::add_assignment method in
1890    jit-recording.c.  */
1891 
1892 void
1893 gcc_jit_block_add_assignment (gcc_jit_block *block,
1894 			      gcc_jit_location *loc,
1895 			      gcc_jit_lvalue *lvalue,
1896 			      gcc_jit_rvalue *rvalue)
1897 {
1898   RETURN_IF_NOT_VALID_BLOCK (block, loc);
1899   gcc::jit::recording::context *ctxt = block->get_context ();
1900   JIT_LOG_FUNC (ctxt->get_logger ());
1901   /* LOC can be NULL.  */
1902   RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1903   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1904   RETURN_IF_FAIL_PRINTF4 (
1905     compatible_types (lvalue->get_type (),
1906 		      rvalue->get_type ()),
1907     ctxt, loc,
1908     "mismatching types:"
1909     " assignment to %s (type: %s) from %s (type: %s)",
1910     lvalue->get_debug_string (),
1911     lvalue->get_type ()->get_debug_string (),
1912     rvalue->get_debug_string (),
1913     rvalue->get_type ()->get_debug_string ());
1914 
1915   gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
1916 
1917   /* "stmt" should be good enough to be usable in error-messages,
1918      but might still not be compilable; perform some more
1919      error-checking here.  We do this here so that the error messages
1920      can contain a stringified version of "stmt", whilst appearing
1921      as close as possible to the point of failure.  */
1922   lvalue->verify_valid_within_stmt (__func__, stmt);
1923   rvalue->verify_valid_within_stmt (__func__, stmt);
1924 }
1925 
1926 /* Public entrypoint.  See description in libgccjit.h.
1927 
1928    After error-checking, the real work is done by the
1929    gcc::jit::recording::block::add_assignment_op method in
1930    jit-recording.c.  */
1931 
1932 void
1933 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1934 				 gcc_jit_location *loc,
1935 				 gcc_jit_lvalue *lvalue,
1936 				 enum gcc_jit_binary_op op,
1937 				 gcc_jit_rvalue *rvalue)
1938 {
1939   RETURN_IF_NOT_VALID_BLOCK (block, loc);
1940   gcc::jit::recording::context *ctxt = block->get_context ();
1941   JIT_LOG_FUNC (ctxt->get_logger ());
1942   /* LOC can be NULL.  */
1943   RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1944   RETURN_IF_FAIL_PRINTF1 (
1945     valid_binary_op_p (op),
1946     ctxt, loc,
1947     "unrecognized value for enum gcc_jit_binary_op: %i",
1948     op);
1949   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1950   RETURN_IF_FAIL_PRINTF4 (
1951     compatible_types (lvalue->get_type (),
1952 		      rvalue->get_type ()),
1953     ctxt, loc,
1954     "mismatching types:"
1955     " assignment to %s (type: %s) involving %s (type: %s)",
1956     lvalue->get_debug_string (),
1957     lvalue->get_type ()->get_debug_string (),
1958     rvalue->get_debug_string (),
1959     rvalue->get_type ()->get_debug_string ());
1960 
1961   gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
1962 
1963   /* "stmt" should be good enough to be usable in error-messages,
1964      but might still not be compilable; perform some more
1965      error-checking here.  We do this here so that the error messages
1966      can contain a stringified version of "stmt", whilst appearing
1967      as close as possible to the point of failure.  */
1968   lvalue->verify_valid_within_stmt (__func__, stmt);
1969   rvalue->verify_valid_within_stmt (__func__, stmt);
1970 }
1971 
1972 /* Internal helper function for determining if rvalue BOOLVAL is of
1973    boolean type.  For use by gcc_jit_block_end_with_conditional.  */
1974 
1975 static bool
1976 is_bool (gcc_jit_rvalue *boolval)
1977 {
1978   gcc::jit::recording::type *actual_type = boolval->get_type ();
1979   gcc::jit::recording::type *bool_type =
1980     boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1981   return actual_type == bool_type;
1982 }
1983 
1984 /* Public entrypoint.  See description in libgccjit.h.
1985 
1986    After error-checking, the real work is done by the
1987    gcc::jit::recording::block::end_with_conditional method in
1988    jit-recording.c.  */
1989 
1990 void
1991 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1992 				    gcc_jit_location *loc,
1993 				    gcc_jit_rvalue *boolval,
1994 				    gcc_jit_block *on_true,
1995 				    gcc_jit_block *on_false)
1996 {
1997   RETURN_IF_NOT_VALID_BLOCK (block, loc);
1998   gcc::jit::recording::context *ctxt = block->get_context ();
1999   JIT_LOG_FUNC (ctxt->get_logger ());
2000   /* LOC can be NULL.  */
2001   RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
2002   RETURN_IF_FAIL_PRINTF2 (
2003    is_bool (boolval), ctxt, loc,
2004    "%s (type: %s) is not of boolean type ",
2005    boolval->get_debug_string (),
2006    boolval->get_type ()->get_debug_string ());
2007   RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
2008   RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
2009   RETURN_IF_FAIL_PRINTF4 (
2010     block->get_function () == on_true->get_function (),
2011     ctxt, loc,
2012     "\"on_true\" block is not in same function:"
2013     " source block %s is in function %s"
2014     " whereas target block %s is in function %s",
2015     block->get_debug_string (),
2016     block->get_function ()->get_debug_string (),
2017     on_true->get_debug_string (),
2018     on_true->get_function ()->get_debug_string ());
2019   RETURN_IF_FAIL_PRINTF4 (
2020     block->get_function () == on_false->get_function (),
2021     ctxt, loc,
2022     "\"on_false\" block is not in same function:"
2023     " source block %s is in function %s"
2024     " whereas target block %s is in function %s",
2025     block->get_debug_string (),
2026     block->get_function ()->get_debug_string (),
2027     on_false->get_debug_string (),
2028     on_false->get_function ()->get_debug_string ());
2029 
2030   gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
2031 
2032   /* "stmt" should be good enough to be usable in error-messages,
2033      but might still not be compilable; perform some more
2034      error-checking here.  We do this here so that the error messages
2035      can contain a stringified version of "stmt", whilst appearing
2036      as close as possible to the point of failure.  */
2037   boolval->verify_valid_within_stmt (__func__, stmt);
2038 }
2039 
2040 /* Public entrypoint.  See description in libgccjit.h.
2041 
2042    After error-checking, the real work is done by the
2043    gcc::jit::recording::block::add_comment method in
2044    jit-recording.c.  */
2045 
2046 void
2047 gcc_jit_block_add_comment (gcc_jit_block *block,
2048 			   gcc_jit_location *loc,
2049 			   const char *text)
2050 {
2051   RETURN_IF_NOT_VALID_BLOCK (block, loc);
2052   gcc::jit::recording::context *ctxt = block->get_context ();
2053   JIT_LOG_FUNC (ctxt->get_logger ());
2054   /* LOC can be NULL.  */
2055   RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
2056 
2057   block->add_comment (loc, text);
2058 }
2059 
2060 /* Public entrypoint.  See description in libgccjit.h.
2061 
2062    After error-checking, the real work is done by the
2063    gcc::jit::recording::block::end_with_jump method in
2064    jit-recording.c.  */
2065 
2066 void
2067 gcc_jit_block_end_with_jump (gcc_jit_block *block,
2068 			     gcc_jit_location *loc,
2069 			     gcc_jit_block *target)
2070 {
2071   RETURN_IF_NOT_VALID_BLOCK (block, loc);
2072   gcc::jit::recording::context *ctxt = block->get_context ();
2073   JIT_LOG_FUNC (ctxt->get_logger ());
2074   /* LOC can be NULL.  */
2075   RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
2076   RETURN_IF_FAIL_PRINTF4 (
2077     block->get_function () == target->get_function (),
2078     ctxt, loc,
2079     "target block is not in same function:"
2080     " source block %s is in function %s"
2081     " whereas target block %s is in function %s",
2082     block->get_debug_string (),
2083     block->get_function ()->get_debug_string (),
2084     target->get_debug_string (),
2085     target->get_function ()->get_debug_string ());
2086 
2087   block->end_with_jump (loc, target);
2088 }
2089 
2090 /* Public entrypoint.  See description in libgccjit.h.
2091 
2092    After error-checking, the real work is done by the
2093    gcc::jit::recording::block::end_with_return method in
2094    jit-recording.c.  */
2095 
2096 void
2097 gcc_jit_block_end_with_return (gcc_jit_block *block,
2098 			       gcc_jit_location *loc,
2099 			       gcc_jit_rvalue *rvalue)
2100 {
2101   RETURN_IF_NOT_VALID_BLOCK (block, loc);
2102   gcc::jit::recording::context *ctxt = block->get_context ();
2103   JIT_LOG_FUNC (ctxt->get_logger ());
2104   /* LOC can be NULL.  */
2105   gcc::jit::recording::function *func = block->get_function ();
2106   RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2107   RETURN_IF_FAIL_PRINTF4 (
2108     compatible_types (
2109       func->get_return_type (),
2110       rvalue->get_type ()),
2111     ctxt, loc,
2112     "mismatching types:"
2113     " return of %s (type: %s) in function %s (return type: %s)",
2114     rvalue->get_debug_string (),
2115     rvalue->get_type ()->get_debug_string (),
2116     func->get_debug_string (),
2117     func->get_return_type ()->get_debug_string ());
2118 
2119   gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
2120 
2121   /* "stmt" should be good enough to be usable in error-messages,
2122      but might still not be compilable; perform some more
2123      error-checking here.  We do this here so that the error messages
2124      can contain a stringified version of "stmt", whilst appearing
2125      as close as possible to the point of failure.  */
2126   rvalue->verify_valid_within_stmt (__func__, stmt);
2127 }
2128 
2129 /* Public entrypoint.  See description in libgccjit.h.
2130 
2131    After error-checking, the real work is done by the
2132    gcc::jit::recording::block::end_with_return method in
2133    jit-recording.c.  */
2134 
2135 void
2136 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
2137 				    gcc_jit_location *loc)
2138 {
2139   RETURN_IF_NOT_VALID_BLOCK (block, loc);
2140   gcc::jit::recording::context *ctxt = block->get_context ();
2141   JIT_LOG_FUNC (ctxt->get_logger ());
2142   /* LOC can be NULL.  */
2143   gcc::jit::recording::function *func = block->get_function ();
2144   RETURN_IF_FAIL_PRINTF2 (
2145     func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2146     ctxt, loc,
2147     "mismatching types:"
2148     " void return in function %s (return type: %s)",
2149     func->get_debug_string (),
2150     func->get_return_type ()->get_debug_string ());
2151 
2152   block->end_with_return (loc, NULL);
2153 }
2154 
2155 /* Public entrypoint.  See description in libgccjit.h.
2156 
2157    After error-checking, the real work is done by the
2158    gcc::jit::recording::context::new_case method in
2159    jit-recording.c.  */
2160 
2161 gcc_jit_case *
2162 gcc_jit_context_new_case (gcc_jit_context *ctxt,
2163 			  gcc_jit_rvalue *min_value,
2164 			  gcc_jit_rvalue *max_value,
2165 			  gcc_jit_block *block)
2166 {
2167   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2168   JIT_LOG_FUNC (ctxt->get_logger ());
2169   RETURN_NULL_IF_FAIL (min_value, ctxt, NULL, "NULL min_value");
2170   RETURN_NULL_IF_FAIL (max_value, ctxt, NULL, "NULL max_value");
2171   RETURN_NULL_IF_FAIL (block, ctxt, NULL, "NULL block");
2172 
2173   RETURN_NULL_IF_FAIL_PRINTF1 (min_value->is_constant (), ctxt, NULL,
2174 			       "min_value is not a constant: %s",
2175 			       min_value->get_debug_string ());
2176   RETURN_NULL_IF_FAIL_PRINTF1 (max_value->is_constant (), ctxt, NULL,
2177 			       "max_value is not a constant: %s",
2178 			       max_value->get_debug_string ());
2179   RETURN_NULL_IF_FAIL_PRINTF2 (
2180     min_value->get_type ()->is_int (),
2181     ctxt, NULL,
2182     "min_value: %s (type: %s) is not of integer type",
2183     min_value->get_debug_string (),
2184     min_value->get_type ()->get_debug_string ());
2185   RETURN_NULL_IF_FAIL_PRINTF2 (
2186     max_value->get_type ()->is_int (),
2187     ctxt, NULL,
2188     "max_value: %s (type: %s) is not of integer type",
2189     max_value->get_debug_string (),
2190     max_value->get_type ()->get_debug_string ());
2191 
2192   wide_int wi_min, wi_max;
2193   if (!min_value->get_wide_int (&wi_min))
2194     gcc_unreachable ();
2195   if (!max_value->get_wide_int (&wi_max))
2196     gcc_unreachable ();
2197   RETURN_NULL_IF_FAIL_PRINTF2 (
2198     wi::les_p (wi_min, wi_max),
2199     ctxt, NULL,
2200     "min_value: %s > max_value: %s",
2201     min_value->get_debug_string (),
2202     max_value->get_debug_string ());
2203   return (gcc_jit_case *)ctxt->new_case (min_value,
2204 					 max_value,
2205 					 block);
2206 }
2207 
2208 /* Public entrypoint.  See description in libgccjit.h.
2209 
2210    After error-checking, this calls the trivial
2211    gcc::jit::recording::memento::as_object method (a case is a
2212    memento), in jit-recording.h.  */
2213 
2214 gcc_jit_object *
2215 gcc_jit_case_as_object (gcc_jit_case *case_)
2216 {
2217   RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case");
2218 
2219   return static_cast <gcc_jit_object *> (case_->as_object ());
2220 }
2221 
2222 /* Helper function for gcc_jit_block_end_with_switch and
2223    valid_case_for_switch.  */
2224 
2225 static bool
2226 valid_dest_for_switch (gcc::jit::recording::context *ctxt,
2227 		       gcc_jit_location *loc,
2228 		       const char *api_funcname,
2229 		       gcc::jit::recording::block *switch_block,
2230 		       gcc::jit::recording::block *dest_block,
2231 		       const char *dest_block_desc)
2232 {
2233   if (!dest_block)
2234     {
2235       jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc);
2236       return false;
2237     }
2238   gcc::jit::recording::function *switch_fn = switch_block->get_function ();
2239   gcc::jit::recording::function *dest_fn = dest_block->get_function ();
2240   if (switch_fn != dest_fn)
2241     {
2242       jit_error (ctxt, loc,
2243 		 "%s: %s is not in same function:"
2244 		 " switch block %s is in function %s"
2245 		 " whereas %s %s is in function %s",
2246 		 api_funcname,
2247 		 dest_block_desc,
2248 		 switch_block->get_debug_string (),
2249 		 switch_fn->get_debug_string (),
2250 		 dest_block_desc,
2251 		 dest_block->get_debug_string (),
2252 		 dest_fn->get_debug_string ());
2253       return false;
2254     }
2255   return true;
2256 }
2257 
2258 /* Helper function for gcc_jit_block_end_with_switch.  */
2259 
2260 static bool
2261 valid_case_for_switch (gcc::jit::recording::context *ctxt,
2262 		       gcc_jit_location *loc,
2263 		       const char *api_funcname,
2264 		       gcc_jit_block *switch_block,
2265 		       gcc_jit_rvalue *expr,
2266 		       gcc_jit_case *case_,
2267 		       const char *case_desc,
2268 		       int case_idx)
2269 {
2270   if (!case_)
2271     {
2272       jit_error (ctxt, loc,
2273 		 "%s:"
2274 		 " NULL case %i",
2275 		 api_funcname,
2276 		 case_idx);
2277       return false;
2278     }
2279   if (!valid_dest_for_switch (ctxt, loc,
2280 			      api_funcname,
2281 			      switch_block,
2282 			      case_->get_dest_block (),
2283 			      case_desc))
2284     return false;
2285   gcc::jit::recording::type *expr_type = expr->get_type ();
2286   if (expr_type != case_->get_min_value ()->get_type ())
2287     {
2288       jit_error (ctxt, loc,
2289 		 "%s:"
2290 		 " mismatching types between case and expression:"
2291 		 " cases[%i]->min_value: %s (type: %s)"
2292 		 " expr: %s (type: %s)",
2293 		 api_funcname,
2294 		 case_idx,
2295 		 case_->get_min_value ()->get_debug_string (),
2296 		 case_->get_min_value ()->get_type ()->get_debug_string (),
2297 		 expr->get_debug_string (),
2298 		 expr_type->get_debug_string ());
2299       return false;
2300     }
2301   if (expr_type != case_->get_max_value ()->get_type ())
2302     {
2303       jit_error (ctxt, loc,
2304 		 "%s:"
2305 		 " mismatching types between case and expression:"
2306 		 " cases[%i]->max_value: %s (type: %s)"
2307 		 " expr: %s (type: %s)",
2308 		 api_funcname,
2309 		 case_idx,
2310 		 case_->get_max_value ()->get_debug_string (),
2311 		 case_->get_max_value ()->get_type ()->get_debug_string (),
2312 		 expr->get_debug_string (),
2313 		 expr_type->get_debug_string ());
2314       return false;
2315     }
2316   return true;
2317 }
2318 
2319 /* A class for holding the data we need to perform error-checking
2320    on a libgccjit API call.  */
2321 
2322 class api_call_validator
2323 {
2324  public:
2325   api_call_validator (gcc::jit::recording::context *ctxt,
2326 		      gcc_jit_location *loc,
2327 		      const char *funcname)
2328   : m_ctxt (ctxt),
2329     m_loc (loc),
2330     m_funcname (funcname)
2331   {}
2332 
2333  protected:
2334   gcc::jit::recording::context *m_ctxt;
2335   gcc_jit_location *m_loc;
2336   const char *m_funcname;
2337 };
2338 
2339 /* A class for verifying that the ranges of cases within
2340    gcc_jit_block_end_with_switch don't overlap.  */
2341 
2342 class case_range_validator : public api_call_validator
2343 {
2344  public:
2345   case_range_validator (gcc::jit::recording::context *ctxt,
2346 			gcc_jit_location *loc,
2347 			const char *funcname);
2348 
2349   bool
2350   validate (gcc_jit_case *case_, int idx);
2351 
2352  private:
2353   static int
2354   case_compare (gcc::jit::recording::rvalue *k1,
2355 		gcc::jit::recording::rvalue *k2);
2356 
2357   static wide_int
2358   get_wide_int (gcc::jit::recording::rvalue *k);
2359 
2360  private:
2361   typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases;
2362 };
2363 
2364 /* case_range_validator's ctor.  */
2365 
2366 case_range_validator::case_range_validator (gcc::jit::recording::context *ctxt,
2367 					    gcc_jit_location *loc,
2368 					    const char *funcname)
2369 : api_call_validator (ctxt, loc, funcname),
2370   m_cases (case_compare, NULL, NULL)
2371 {
2372 }
2373 
2374 /* Ensure that the range of CASE_ does not overlap with any of the
2375    ranges of cases we've already seen.
2376    Return true if everything is OK.
2377    Return false and emit an error if there is an overlap.
2378    Compare with c-family/c-common.c:c_add_case_label.  */
2379 
2380 bool
2381 case_range_validator::validate (gcc_jit_case *case_,
2382 				int case_idx)
2383 {
2384   /* Look up the LOW_VALUE in the table of case labels we already
2385      have.  */
2386   gcc_jit_case *other = m_cases.lookup (case_->get_min_value ());
2387 
2388   /* If there was not an exact match, check for overlapping ranges.  */
2389   if (!other)
2390     {
2391       gcc_jit_case *pred;
2392       gcc_jit_case *succ;
2393 
2394       /* Even though there wasn't an exact match, there might be an
2395 	 overlap between this case range and another case range.
2396 	 Since we've (inductively) not allowed any overlapping case
2397 	 ranges, we simply need to find the greatest low case label
2398 	 that is smaller that CASE_MIN_VALUE, and the smallest low case
2399 	 label that is greater than CASE_MAX_VALUE.  If there is an overlap
2400 	 it will occur in one of these two ranges.  */
2401       pred = m_cases.predecessor (case_->get_min_value ());
2402       succ = m_cases.successor (case_->get_max_value ());
2403 
2404       /* Check to see if the PRED overlaps.  It is smaller than
2405 	 the LOW_VALUE, so we only need to check its max value.  */
2406       if (pred)
2407 	{
2408 	  wide_int wi_case_min = get_wide_int (case_->get_min_value ());
2409 	  wide_int wi_pred_max = get_wide_int (pred->get_max_value ());
2410 	  if (wi::ges_p (wi_pred_max, wi_case_min))
2411 	    other = pred;
2412 	}
2413 
2414       if (!other && succ)
2415 	{
2416 	  /* Check to see if the SUCC overlaps.  The low end of that
2417 	     range is bigger than the low end of the current range.  */
2418 	  wide_int wi_case_max = get_wide_int (case_->get_max_value ());
2419 	  wide_int wi_succ_min = get_wide_int (succ->get_min_value ());
2420 	  if (wi::les_p (wi_succ_min, wi_case_max))
2421 	    other = succ;
2422 	}
2423     }
2424 
2425   /* If there was an overlap, issue an error.  */
2426   if (other)
2427     {
2428       jit_error (m_ctxt, m_loc,
2429 		 "%s: duplicate (or overlapping) cases values:"
2430 		 " case %i: %s overlaps %s",
2431 		 m_funcname,
2432 		 case_idx,
2433 		 case_->get_debug_string (),
2434 		 other->get_debug_string ());
2435       return false;
2436     }
2437 
2438   /* Register this case label in the splay tree.  */
2439   m_cases.insert (case_->get_min_value (),
2440 		  case_);
2441   return true;
2442 }
2443 
2444 /* Compare with c-family/c-common.c:case_compare, which acts on tree
2445    nodes, rather than rvalue *.
2446 
2447    Comparator for case label values.  K1 and K2 must be constant integer
2448    values (anything else should have been rejected by
2449    gcc_jit_context_new_case.
2450 
2451    Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
2452    K2, and 0 if K1 and K2 are equal.  */
2453 
2454 int
2455 case_range_validator::case_compare (gcc::jit::recording::rvalue * k1,
2456 				    gcc::jit::recording::rvalue * k2)
2457 {
2458   wide_int wi1 = get_wide_int (k1);
2459   wide_int wi2 = get_wide_int (k2);
2460   return wi::cmps(wi1, wi2);
2461 }
2462 
2463 /* Given a const int rvalue K, get the underlying value as a wide_int.  */
2464 
2465 wide_int
2466 case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k)
2467 {
2468   wide_int wi;
2469   bool got_wi = k->get_wide_int (&wi);
2470   gcc_assert (got_wi);
2471   return wi;
2472 }
2473 
2474 /* Public entrypoint.  See description in libgccjit.h.
2475 
2476    After error-checking, the real work is done by the
2477    gcc::jit::recording::block::end_with_switch method in
2478    jit-recording.c.  */
2479 
2480 void
2481 gcc_jit_block_end_with_switch (gcc_jit_block *block,
2482 			       gcc_jit_location *loc,
2483 			       gcc_jit_rvalue *expr,
2484 			       gcc_jit_block *default_block,
2485 			       int num_cases,
2486 			       gcc_jit_case **cases)
2487 {
2488   RETURN_IF_NOT_VALID_BLOCK (block, loc);
2489   gcc::jit::recording::context *ctxt = block->get_context ();
2490   JIT_LOG_FUNC (ctxt->get_logger ());
2491   /* LOC can be NULL.  */
2492   RETURN_IF_FAIL (expr, ctxt, loc,
2493 		  "NULL expr");
2494   gcc::jit::recording::type *expr_type = expr->get_type ();
2495   RETURN_IF_FAIL_PRINTF2 (
2496     expr_type->is_int (),
2497     ctxt, loc,
2498     "expr: %s (type: %s) is not of integer type",
2499     expr->get_debug_string (),
2500     expr_type->get_debug_string ());
2501   if (!valid_dest_for_switch (ctxt, loc,
2502 			      __func__,
2503 			      block,
2504 			      default_block,
2505 			      "default_block"))
2506     return;
2507   RETURN_IF_FAIL (num_cases >= 0, ctxt, loc, "num_cases < 0");
2508   case_range_validator crv (ctxt, loc, __func__);
2509   for (int i = 0; i < num_cases; i++)
2510     {
2511       char case_desc[32];
2512       snprintf (case_desc, sizeof (case_desc),
2513 		"cases[%i]", i);
2514       if (!valid_case_for_switch (ctxt, loc,
2515 				  __func__,
2516 				  block,
2517 				  expr,
2518 				  cases[i],
2519 				  case_desc,
2520 				  i))
2521 	return;
2522       if (!crv.validate (cases[i], i))
2523 	return;
2524     }
2525 
2526   block->end_with_switch (loc, expr, default_block,
2527 			  num_cases,
2528 			  (gcc::jit::recording::case_ **)cases);
2529 }
2530 
2531 /**********************************************************************
2532  Option-management
2533  **********************************************************************/
2534 
2535 /* Public entrypoint.  See description in libgccjit.h.
2536 
2537    After error-checking, the real work is done by the
2538    gcc::jit::recording::context::set_str_option method in
2539    jit-recording.c.  */
2540 
2541 void
2542 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2543 				enum gcc_jit_str_option opt,
2544 				const char *value)
2545 {
2546   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2547   JIT_LOG_FUNC (ctxt->get_logger ());
2548   /* opt is checked by the inner function.
2549      value can be NULL.  */
2550 
2551   ctxt->set_str_option (opt, value);
2552 }
2553 
2554 /* Public entrypoint.  See description in libgccjit.h.
2555 
2556    After error-checking, the real work is done by the
2557    gcc::jit::recording::context::set_int_option method in
2558    jit-recording.c.  */
2559 
2560 void
2561 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2562 				enum gcc_jit_int_option opt,
2563 				int value)
2564 {
2565   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2566   JIT_LOG_FUNC (ctxt->get_logger ());
2567   /* opt is checked by the inner function.  */
2568 
2569   ctxt->set_int_option (opt, value);
2570 }
2571 
2572 /* Public entrypoint.  See description in libgccjit.h.
2573 
2574    After error-checking, the real work is done by the
2575    gcc::jit::recording::context::set_bool_option method in
2576    jit-recording.c.  */
2577 
2578 void
2579 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2580 				 enum gcc_jit_bool_option opt,
2581 				 int value)
2582 {
2583   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2584   JIT_LOG_FUNC (ctxt->get_logger ());
2585   /* opt is checked by the inner function.  */
2586 
2587   ctxt->set_bool_option (opt, value);
2588 }
2589 
2590 /* Public entrypoint.  See description in libgccjit.h.
2591 
2592    After error-checking, the real work is done by the
2593    gcc::jit::recording::context::set_inner_bool_option method in
2594    jit-recording.c.  */
2595 
2596 void
2597 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
2598 						   int bool_value)
2599 {
2600   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2601   JIT_LOG_FUNC (ctxt->get_logger ());
2602   ctxt->set_inner_bool_option (
2603     gcc::jit::INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
2604     bool_value);
2605 }
2606 
2607 /* Public entrypoint.  See description in libgccjit.h.
2608 
2609    After error-checking, the real work is done by the
2610    gcc::jit::recording::context::set_inner_bool_option method in
2611    jit-recording.c.  */
2612 
2613 extern void
2614 gcc_jit_context_set_bool_use_external_driver (gcc_jit_context *ctxt,
2615 					      int bool_value)
2616 {
2617   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2618   JIT_LOG_FUNC (ctxt->get_logger ());
2619   ctxt->set_inner_bool_option (
2620     gcc::jit::INNER_BOOL_OPTION_USE_EXTERNAL_DRIVER,
2621     bool_value);
2622 }
2623 
2624 /* Public entrypoint.  See description in libgccjit.h.
2625 
2626    After error-checking, the real work is done by the
2627    gcc::jit::recording::context::add_command_line_option method in
2628    jit-recording.c.  */
2629 
2630 void
2631 gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
2632 					 const char *optname)
2633 {
2634   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2635   JIT_LOG_FUNC (ctxt->get_logger ());
2636   RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
2637   if (ctxt->get_logger ())
2638     ctxt->get_logger ()->log ("optname: %s", optname);
2639 
2640   ctxt->add_command_line_option (optname);
2641 }
2642 
2643 /* Public entrypoint.  See description in libgccjit.h.
2644 
2645    After error-checking, the real work is done by the
2646    gcc::jit::recording::context::enable_dump method in
2647    jit-recording.c.  */
2648 
2649 void
2650 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2651 			     const char *dumpname,
2652 			     char **out_ptr)
2653 {
2654   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2655   JIT_LOG_FUNC (ctxt->get_logger ());
2656   RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2657   RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2658 
2659   ctxt->enable_dump (dumpname, out_ptr);
2660 }
2661 
2662 /* Public entrypoint.  See description in libgccjit.h.
2663 
2664    After error-checking, the real work is done by the
2665    gcc::jit::recording::context::compile method in
2666    jit-recording.c.  */
2667 
2668 gcc_jit_result *
2669 gcc_jit_context_compile (gcc_jit_context *ctxt)
2670 {
2671   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2672 
2673   JIT_LOG_FUNC (ctxt->get_logger ());
2674 
2675   ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
2676 
2677   gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2678 
2679   ctxt->log ("%s: returning (gcc_jit_result *)%p",
2680 	     __func__, (void *)result);
2681 
2682   return result;
2683 }
2684 
2685 /* Public entrypoint.  See description in libgccjit.h.
2686 
2687    After error-checking, the real work is done by the
2688    gcc::jit::recording::context::compile_to_file method in
2689    jit-recording.c.  */
2690 
2691 void
2692 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
2693 				 enum gcc_jit_output_kind output_kind,
2694 				 const char *output_path)
2695 {
2696   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2697   JIT_LOG_FUNC (ctxt->get_logger ());
2698   RETURN_IF_FAIL_PRINTF1 (
2699     ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
2700      && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
2701     ctxt, NULL,
2702     "unrecognized output_kind: %i",
2703     output_kind);
2704   RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
2705 
2706   ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
2707   ctxt->log ("output_kind: %i", output_kind);
2708   ctxt->log ("output_path: %s", output_path);
2709 
2710   ctxt->compile_to_file (output_kind, output_path);
2711 }
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::context::dump_to_file method in
2718    jit-recording.c.  */
2719 
2720 void
2721 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2722 			      const char *path,
2723 			      int update_locations)
2724 {
2725   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2726   JIT_LOG_FUNC (ctxt->get_logger ());
2727   RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2728   ctxt->dump_to_file (path, update_locations);
2729 }
2730 
2731 /* Public entrypoint.  See description in libgccjit.h.  */
2732 
2733 void
2734 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2735 			     FILE *logfile,
2736 			     int flags,
2737 			     int verbosity)
2738 {
2739   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2740   JIT_LOG_FUNC (ctxt->get_logger ());
2741   RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2742   RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2743 
2744   gcc::jit::logger *logger;
2745   if (logfile)
2746     logger = new gcc::jit::logger (logfile, flags, verbosity);
2747   else
2748     logger = NULL;
2749   ctxt->set_logger (logger);
2750 }
2751 
2752 /* Public entrypoint.  See description in libgccjit.h.
2753 
2754    After error-checking, the real work is done by the
2755    gcc::jit::recording::context::dump_reproducer_to_file method in
2756    jit-recording.c.  */
2757 
2758 void
2759 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
2760 					 const char *path)
2761 {
2762   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2763   JIT_LOG_FUNC (ctxt->get_logger ());
2764   RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2765   ctxt->dump_reproducer_to_file (path);
2766 }
2767 
2768 /* Public entrypoint.  See description in libgccjit.h.
2769 
2770    After error-checking, the real work is done by the
2771    gcc::jit::recording::context::get_first_error method in
2772    jit-recording.c.  */
2773 
2774 const char *
2775 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2776 {
2777   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2778   JIT_LOG_FUNC (ctxt->get_logger ());
2779 
2780   return ctxt->get_first_error ();
2781 }
2782 
2783 /* Public entrypoint.  See description in libgccjit.h.
2784 
2785    After error-checking, the real work is done by the
2786    gcc::jit::recording::context::get_last_error method in
2787    jit-recording.c.  */
2788 
2789 const char *
2790 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2791 {
2792   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2793 
2794   return ctxt->get_last_error ();
2795 }
2796 
2797 /* Public entrypoint.  See description in libgccjit.h.
2798 
2799    After error-checking, the real work is done by the
2800    gcc::jit::result::get_code method in jit-result.c.  */
2801 
2802 void *
2803 gcc_jit_result_get_code (gcc_jit_result *result,
2804 			 const char *fnname)
2805 {
2806   RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2807   JIT_LOG_FUNC (result->get_logger ());
2808   RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2809 
2810   result->log ("locating fnname: %s", fnname);
2811   void *code = result->get_code (fnname);
2812   result->log ("%s: returning (void *)%p", __func__, code);
2813 
2814   return code;
2815 }
2816 
2817 /* Public entrypoint.  See description in libgccjit.h.
2818 
2819    After error-checking, the real work is done by the
2820    gcc::jit::result::get_global method in jit-result.c.  */
2821 
2822 void *
2823 gcc_jit_result_get_global (gcc_jit_result *result,
2824 			   const char *name)
2825 {
2826   RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2827   JIT_LOG_FUNC (result->get_logger ());
2828   RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
2829 
2830   void *global = result->get_global (name);
2831   result->log ("%s: returning (void *)%p", __func__, global);
2832 
2833   return global;
2834 }
2835 
2836 /* Public entrypoint.  See description in libgccjit.h.
2837 
2838    After error-checking, this is essentially a wrapper around the
2839    destructor for gcc::jit::result in jit-result.c.  */
2840 
2841 void
2842 gcc_jit_result_release (gcc_jit_result *result)
2843 {
2844   RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2845   JIT_LOG_FUNC (result->get_logger ());
2846   result->log ("deleting result: %p", (void *)result);
2847   delete result;
2848 }
2849 
2850 /**********************************************************************
2851  Timing support.
2852  **********************************************************************/
2853 
2854 /* Create a gcc_jit_timer instance, and start timing.  */
2855 
2856 gcc_jit_timer *
2857 gcc_jit_timer_new (void)
2858 {
2859   gcc_jit_timer *timer = new gcc_jit_timer ();
2860   timer->start (TV_TOTAL);
2861   timer->push (TV_JIT_CLIENT_CODE);
2862   return timer;
2863 }
2864 
2865 /* Release a gcc_jit_timer instance.  */
2866 
2867 void
2868 gcc_jit_timer_release (gcc_jit_timer *timer)
2869 {
2870   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2871 
2872   delete timer;
2873 }
2874 
2875 /* Associate a gcc_jit_timer instance with a context.  */
2876 
2877 void
2878 gcc_jit_context_set_timer (gcc_jit_context *ctxt,
2879 			   gcc_jit_timer *timer)
2880 {
2881   RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
2882   RETURN_IF_FAIL (timer, ctxt, NULL, "NULL timer");
2883 
2884   ctxt->set_timer (timer);
2885 }
2886 
2887 /* Get the timer associated with a context (if any).  */
2888 
2889 gcc_jit_timer *
2890 gcc_jit_context_get_timer (gcc_jit_context *ctxt)
2891 {
2892   RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
2893 
2894   return (gcc_jit_timer *)ctxt->get_timer ();
2895 }
2896 
2897 /* Push the given item onto the timing stack.  */
2898 
2899 void
2900 gcc_jit_timer_push (gcc_jit_timer *timer,
2901 		    const char *item_name)
2902 {
2903   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2904   RETURN_IF_FAIL (item_name, NULL, NULL, "NULL item_name");
2905   timer->push_client_item (item_name);
2906 }
2907 
2908 /* Pop the top item from the timing stack.  */
2909 
2910 void
2911 gcc_jit_timer_pop (gcc_jit_timer *timer,
2912 		   const char *item_name)
2913 {
2914   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2915 
2916   if (item_name)
2917     {
2918       const char *top_item_name = timer->get_topmost_item_name ();
2919 
2920       RETURN_IF_FAIL_PRINTF1
2921 	(top_item_name, NULL, NULL,
2922 	 "pop of empty timing stack (attempting to pop: \"%s\")",
2923 	 item_name);
2924 
2925       RETURN_IF_FAIL_PRINTF2
2926 	(0 == strcmp (item_name, top_item_name), NULL, NULL,
2927 	 "mismatching item_name:"
2928 	 " top of timing stack: \"%s\","
2929 	 " attempting to pop: \"%s\"",
2930 	 top_item_name,
2931 	 item_name);
2932     }
2933 
2934   timer->pop_client_item ();
2935 }
2936 
2937 /* Print timing information to the given stream about activity since
2938    the timer was started.  */
2939 
2940 void
2941 gcc_jit_timer_print (gcc_jit_timer *timer,
2942 		     FILE *f_out)
2943 {
2944   RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2945   RETURN_IF_FAIL (f_out, NULL, NULL, "NULL f_out");
2946 
2947   timer->pop (TV_JIT_CLIENT_CODE);
2948   timer->stop (TV_TOTAL);
2949   timer->print (f_out);
2950   timer->start (TV_TOTAL);
2951   timer->push (TV_JIT_CLIENT_CODE);
2952 }
2953 
2954 /* Public entrypoint.  See description in libgccjit.h.
2955 
2956    After error-checking, the real work is effectively done by the
2957    gcc::jit::base_call::set_require_tail_call setter in jit-recording.h.  */
2958 
2959 void
2960 gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *rvalue,
2961 					   int require_tail_call)
2962 {
2963   RETURN_IF_FAIL (rvalue, NULL, NULL, "NULL call");
2964   JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
2965 
2966   /* Verify that it's a call.  */
2967   gcc::jit::recording::base_call *call = rvalue->dyn_cast_base_call ();
2968   RETURN_IF_FAIL_PRINTF1 (call, NULL, NULL, "not a call: %s",
2969 			  rvalue->get_debug_string ());
2970 
2971   call->set_require_tail_call (require_tail_call);
2972 }
2973