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