Fix gnat.dg/opt39.adb on hppa.
[official-gcc.git] / gcc / jit / libgccjit.cc
blob2def58f6aa73e6816ed124d1691aa7711b4dda1c
1 /* Implementation of the C API; all wrappers into the internal C++ API
2 Copyright (C) 2013-2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #define INCLUDE_MUTEX
23 #include "system.h"
24 #include "coretypes.h"
25 #include "timevar.h"
26 #include "typed-splay-tree.h"
27 #include "cppbuiltin.h"
29 #include "libgccjit.h"
30 #include "jit-recording.h"
31 #include "jit-result.h"
33 /* The opaque types used by the public API are actually subclasses
34 of the gcc::jit::recording classes. */
36 struct gcc_jit_context : public gcc::jit::recording::context
38 gcc_jit_context (gcc_jit_context *parent_ctxt) :
39 context (parent_ctxt)
43 struct gcc_jit_result : public gcc::jit::result
47 struct gcc_jit_object : public gcc::jit::recording::memento
51 struct gcc_jit_location : public gcc::jit::recording::location
55 struct gcc_jit_type : public gcc::jit::recording::type
59 struct gcc_jit_struct : public gcc::jit::recording::struct_
63 struct gcc_jit_function_type : public gcc::jit::recording::function_type
67 struct gcc_jit_vector_type : public gcc::jit::recording::vector_type
71 struct gcc_jit_field : public gcc::jit::recording::field
75 struct gcc_jit_bitfield : public gcc::jit::recording::bitfield
79 struct gcc_jit_function : public gcc::jit::recording::function
83 struct gcc_jit_block : public gcc::jit::recording::block
87 struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
91 struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
95 struct gcc_jit_param : public gcc::jit::recording::param
99 struct gcc_jit_case : public gcc::jit::recording::case_
103 struct gcc_jit_timer : public timer
107 struct gcc_jit_extended_asm : public gcc::jit::recording::extended_asm
112 /**********************************************************************
113 Error-handling.
115 We try to gracefully handle API usage errors by being defensive
116 at the API boundary.
117 **********************************************************************/
119 #define JIT_BEGIN_STMT do {
120 #define JIT_END_STMT } while(0)
122 /* Each of these error-handling macros determines if TEST_EXPR holds.
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.
129 They have to be macros since they inject their "return" into the
130 function they are placed in.
132 The variant macros express:
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
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*
147 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
148 trailing semicolons.
151 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
152 JIT_BEGIN_STMT \
153 if (!(TEST_EXPR)) \
155 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
156 return (RETURN_EXPR); \
158 JIT_END_STMT
160 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
161 JIT_BEGIN_STMT \
162 if (!(TEST_EXPR)) \
164 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
165 __func__, (A0)); \
166 return (RETURN_EXPR); \
168 JIT_END_STMT
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)) \
174 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
175 __func__, (A0), (A1)); \
176 return (RETURN_EXPR); \
178 JIT_END_STMT
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)) \
184 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
185 __func__, (A0), (A1), (A2)); \
186 return (RETURN_EXPR); \
188 JIT_END_STMT
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)) \
194 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
195 __func__, (A0), (A1), (A2), (A3)); \
196 return (RETURN_EXPR); \
198 JIT_END_STMT
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)) \
204 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
205 __func__, (A0), (A1), (A2), (A3), (A4)); \
206 return (RETURN_EXPR); \
208 JIT_END_STMT
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)) \
214 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
215 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
216 return (RETURN_EXPR); \
218 JIT_END_STMT
220 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
221 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
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)
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)
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)
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)
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)
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)
241 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
242 JIT_BEGIN_STMT \
243 if (!(TEST_EXPR)) \
245 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
246 return; \
248 JIT_END_STMT
250 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
251 JIT_BEGIN_STMT \
252 if (!(TEST_EXPR)) \
254 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
255 __func__, (A0)); \
256 return; \
258 JIT_END_STMT
260 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
261 JIT_BEGIN_STMT \
262 if (!(TEST_EXPR)) \
264 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
265 __func__, (A0), (A1)); \
266 return; \
268 JIT_END_STMT
270 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
271 JIT_BEGIN_STMT \
272 if (!(TEST_EXPR)) \
274 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
275 __func__, (A0), (A1), (A2), (A3)); \
276 return; \
278 JIT_END_STMT
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
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
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. */
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);
320 static void
321 jit_error (gcc::jit::recording::context *ctxt,
322 gcc::jit::recording::location *loc,
323 const char *fmt, ...)
325 va_list ap;
326 va_start (ap, fmt);
328 if (ctxt)
329 ctxt->add_error_va (loc, fmt, ap);
330 else
332 /* No context? Send to stderr. */
333 vfprintf (stderr, fmt, ap);
334 fprintf (stderr, "\n");
337 va_end (ap);
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).
344 This is implemented by calling the
345 gcc::jit::recording::type::accepts_writes_from virtual function on
346 LTYPE. */
348 static bool
349 compatible_types (gcc::jit::recording::type *ltype,
350 gcc::jit::recording::type *rtype)
352 return ltype->accepts_writes_from (rtype);
355 /* Public entrypoint wrapping compatible_types. */
358 gcc_jit_compatible_types (gcc_jit_type *ltype,
359 gcc_jit_type *rtype)
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);
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.
370 The real work is done in the constructor for
371 gcc::jit::recording::context in jit-recording.cc. */
373 gcc_jit_context *
374 gcc_jit_context_acquire (void)
376 gcc_jit_context *ctxt = new gcc_jit_context (NULL);
377 ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
378 return ctxt;
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. */
385 void
386 gcc_jit_context_release (gcc_jit_context *ctxt)
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;
394 /* Public entrypoint for creating a child context within
395 PARENT_CTXT. See description in libgccjit.h.
397 The real work is done in the constructor for
398 gcc::jit::recording::context in jit-recording.cc. */
400 gcc_jit_context *
401 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
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;
411 /* Public entrypoint. See description in libgccjit.h.
413 After error-checking, the real work is done by the
414 gcc::jit::recording::context::new_location
415 method in jit-recording.cc. */
417 gcc_jit_location *
418 gcc_jit_context_new_location (gcc_jit_context *ctxt,
419 const char *filename,
420 int line,
421 int column)
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);
428 /* Public entrypoint. See description in libgccjit.h.
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. */
434 gcc_jit_object *
435 gcc_jit_location_as_object (gcc_jit_location *loc)
437 RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
439 return static_cast <gcc_jit_object *> (loc->as_object ());
442 /* Public entrypoint. See description in libgccjit.h.
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. */
448 gcc_jit_object *
449 gcc_jit_type_as_object (gcc_jit_type *type)
451 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
453 return static_cast <gcc_jit_object *> (type->as_object ());
456 /* Public entrypoint for getting a specific type from a context.
458 After error-checking, the real work is done by the
459 gcc::jit::recording::context::get_type method, in
460 jit-recording.cc */
462 gcc_jit_type *
463 gcc_jit_context_get_type (gcc_jit_context *ctxt,
464 enum gcc_jit_types type)
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);
474 return (gcc_jit_type *)ctxt->get_type (type);
477 /* Public entrypoint for getting the integer type of the given size and
478 signedness.
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. */
484 gcc_jit_type *
485 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
486 int num_bytes, int is_signed)
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");
492 return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
495 /* Public entrypoint. See description in libgccjit.h.
497 After error-checking, the real work is done by the
498 gcc::jit::recording::type::get_pointer method, in
499 jit-recording.cc */
501 gcc_jit_type *
502 gcc_jit_type_get_pointer (gcc_jit_type *type)
504 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
506 return (gcc_jit_type *)type->get_pointer ();
509 /* Public entrypoint. See description in libgccjit.h.
511 After error-checking, the real work is done by the
512 gcc::jit::recording::type::get_const method, in
513 jit-recording.cc. */
515 gcc_jit_type *
516 gcc_jit_type_get_const (gcc_jit_type *type)
518 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
520 return (gcc_jit_type *)type->get_const ();
523 /* Public entrypoint. See description in libgccjit.h.
525 After error-checking, the real work is done by the
526 gcc::jit::recording::type::get_volatile method, in
527 jit-recording.cc. */
529 gcc_jit_type *
530 gcc_jit_type_get_volatile (gcc_jit_type *type)
532 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
534 return (gcc_jit_type *)type->get_volatile ();
537 /* Public entrypoint. See description in libgccjit.h.
539 After error-checking, the real work is done by the
540 gcc::jit::recording::type::get_size method, in
541 jit-recording.cc. */
543 ssize_t
544 gcc_jit_type_get_size (gcc_jit_type *type)
546 RETURN_VAL_IF_FAIL (type, -1, NULL, NULL, "NULL type");
547 RETURN_VAL_IF_FAIL
548 (type->is_int () || type->is_float (), -1, NULL, NULL,
549 "only getting the size of integer or floating-point types is supported for now");
550 return type->get_size ();
553 /* Public entrypoint. See description in libgccjit.h.
555 After error-checking, the real work is done by the
556 gcc::jit::recording::type::is_array method, in
557 jit-recording.cc. */
559 gcc_jit_type *
560 gcc_jit_type_dyncast_array (gcc_jit_type *type)
562 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
564 return (gcc_jit_type *)type->is_array ();
567 /* Public entrypoint. See description in libgccjit.h.
569 After error-checking, the real work is done by the
570 gcc::jit::recording::type::is_bool method, in
571 jit-recording.cc. */
574 gcc_jit_type_is_bool (gcc_jit_type *type)
576 RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
578 return type->is_bool ();
581 /* Public entrypoint. See description in libgccjit.h.
583 After error-checking, the real work is done by the
584 gcc::jit::recording::type::is_pointer method, in
585 jit-recording.cc. */
587 gcc_jit_type *
588 gcc_jit_type_is_pointer (gcc_jit_type *type)
590 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
592 return (gcc_jit_type *)type->is_pointer ();
595 /* Public entrypoint. See description in libgccjit.h.
597 After error-checking, the real work is done by the
598 gcc::jit::recording::type::is_int method, in
599 jit-recording.cc. */
602 gcc_jit_type_is_integral (gcc_jit_type *type)
604 RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
606 return type->is_int ();
609 /* Public entrypoint. See description in libgccjit.h.
611 After error-checking, the real work is done by the
612 gcc::jit::recording::type::is_vector method, in
613 jit-recording.cc. */
615 gcc_jit_vector_type *
616 gcc_jit_type_dyncast_vector (gcc_jit_type *type)
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;
623 /* Public entrypoint. See description in libgccjit.h.
625 After error-checking, the real work is done by the
626 gcc::jit::recording::type::is_struct method, in
627 jit-recording.cc. */
629 gcc_jit_struct *
630 gcc_jit_type_is_struct (gcc_jit_type *type)
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;
637 /* Public entrypoint. See description in libgccjit.h.
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. */
643 size_t
644 gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
646 RETURN_VAL_IF_FAIL (vector_type, 0, NULL, NULL, "NULL vector_type");
647 return vector_type->get_num_units ();
650 /* Public entrypoint. See description in libgccjit.h.
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. */
656 gcc_jit_type *
657 gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type)
659 RETURN_NULL_IF_FAIL (vector_type, NULL, NULL, "NULL vector_type");
660 return (gcc_jit_type *)vector_type->get_element_type ();
663 /* Public entrypoint. See description in libgccjit.h.
665 After error-checking, the real work is done by the
666 gcc::jit::recording::type::unqualified method, in
667 jit-recording.cc. */
669 gcc_jit_type *
670 gcc_jit_type_unqualified (gcc_jit_type *type)
672 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
674 return (gcc_jit_type *)type->unqualified ();
677 /* Public entrypoint. See description in libgccjit.h.
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. */
683 gcc_jit_function_type *
684 gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type)
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)
690 return NULL;
693 return (gcc_jit_function_type *)func_ptr_type->dyn_cast_function_type ();
696 /* Public entrypoint. See description in libgccjit.h.
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. */
702 gcc_jit_type *
703 gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
705 RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL function_type");
706 return (gcc_jit_type *)function_type->get_return_type ();
709 /* Public entrypoint. See description in libgccjit.h.
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. */
715 size_t
716 gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
718 RETURN_VAL_IF_FAIL (function_type, 0, NULL, NULL, "NULL function_type");
719 return function_type->get_param_types ().length ();
722 /* Public entrypoint. See description in libgccjit.h.
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. */
728 gcc_jit_type *
729 gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
730 size_t index)
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];
744 /* Public entrypoint. See description in libgccjit.h.
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. */
750 gcc_jit_type *
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)
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");
764 return (gcc_jit_type *)ctxt->new_array_type (loc,
765 element_type,
766 num_elements);
769 /* Public entrypoint. See description in libgccjit.h.
771 After error-checking, the real work is done by the
772 gcc::jit::recording::context::new_field method, in
773 jit-recording.cc. */
775 gcc_jit_field *
776 gcc_jit_context_new_field (gcc_jit_context *ctxt,
777 gcc_jit_location *loc,
778 gcc_jit_type *type,
779 const char *name)
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);
798 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
801 /* Public entrypoint. See description in libgccjit.h.
803 After error-checking, the real work is done by the
804 gcc::jit::recording::context::new_bitfield method, in
805 jit-recording.cc. */
807 gcc_jit_field *
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)
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 ());
834 return (gcc_jit_field *)ctxt->new_bitfield (loc, type, width, name);
837 /* Public entrypoint. See description in libgccjit.h.
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. */
843 gcc_jit_object *
844 gcc_jit_field_as_object (gcc_jit_field *field)
846 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
848 return static_cast <gcc_jit_object *> (field->as_object ());
851 /* Public entrypoint. See description in libgccjit.h.
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 */
858 gcc_jit_struct *
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)
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++)
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 ());
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);
890 /* Public entrypoint. See description in libgccjit.h.
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. */
896 gcc_jit_struct *
897 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
898 gcc_jit_location *loc,
899 const char *name)
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");
906 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
909 /* Public entrypoint. See description in libgccjit.h.
911 After error-checking, this calls the trivial
912 gcc::jit::recording::struct_::as_object method in
913 jit-recording.h. */
915 gcc_jit_type *
916 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
918 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
920 return static_cast <gcc_jit_type *> (struct_type->as_type ());
923 /* Public entrypoint. See description in libgccjit.h.
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. */
929 void
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)
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++)
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 (),
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 ());
961 struct_type->set_fields (loc, num_fields,
962 (gcc::jit::recording::field **)fields);
966 /* Public entrypoint. See description in libgccjit.h.
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 *
972 gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
973 size_t index)
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);
988 /* Public entrypoint. See description in libgccjit.h.
990 After error-checking, this calls the trivial
991 gcc::jit::recording::struct_::get_fields method in
992 jit-recording.h. */
994 size_t
995 gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
997 RETURN_VAL_IF_FAIL (struct_type, 0, NULL, NULL, "NULL struct type");
998 return struct_type->get_fields ()->length ();
1001 /* Public entrypoint. See description in libgccjit.h.
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 */
1008 gcc_jit_type *
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)
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++)
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 ());
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);
1040 /* Public entrypoint. See description in libgccjit.h.
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 */
1046 gcc_jit_type *
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)
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++)
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);
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);
1080 /* Constructing functions. */
1082 /* Public entrypoint. See description in libgccjit.h.
1084 After error-checking, the real work is done by the
1085 gcc::jit::recording::context::new_param method, in jit-recording.cc */
1087 gcc_jit_param *
1088 gcc_jit_context_new_param (gcc_jit_context *ctxt,
1089 gcc_jit_location *loc,
1090 gcc_jit_type *type,
1091 const char *name)
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);
1102 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
1105 /* Public entrypoint. See description in libgccjit.h.
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. */
1111 gcc_jit_object *
1112 gcc_jit_param_as_object (gcc_jit_param *param)
1114 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
1116 return static_cast <gcc_jit_object *> (param->as_object ());
1119 /* Public entrypoint. See description in libgccjit.h.
1121 After error-checking, this calls the trivial
1122 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
1124 gcc_jit_lvalue *
1125 gcc_jit_param_as_lvalue (gcc_jit_param *param)
1127 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
1129 return (gcc_jit_lvalue *)param->as_lvalue ();
1132 /* Public entrypoint. See description in libgccjit.h.
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. */
1138 gcc_jit_rvalue *
1139 gcc_jit_param_as_rvalue (gcc_jit_param *param)
1141 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
1143 return (gcc_jit_rvalue *)param->as_rvalue ();
1146 /* Public entrypoint. See description in libgccjit.h.
1148 After error-checking, the real work is done by the
1149 gcc::jit::recording::context::new_function method, in
1150 jit-recording.cc. */
1152 gcc_jit_function *
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)
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. */
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++)
1189 RETURN_NULL_IF_FAIL_PRINTF2 (
1190 ISALNUM (ch) || ch == '_',
1191 ctxt, loc,
1192 "name \"%s\" contains invalid character: '%c'",
1193 name, ch);
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++)
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 ());
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);
1227 /* Public entrypoint. See description in libgccjit.h.
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. */
1233 gcc_jit_function *
1234 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
1235 const char *name)
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");
1241 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
1244 /* Public entrypoint. See description in libgccjit.h.
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. */
1250 gcc_jit_object *
1251 gcc_jit_function_as_object (gcc_jit_function *func)
1253 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
1255 return static_cast <gcc_jit_object *> (func->as_object ());
1258 /* Public entrypoint. See description in libgccjit.h.
1260 After error-checking, the real work is done by the
1261 gcc::jit::recording::function::get_param method, in
1262 jit-recording.h. */
1264 gcc_jit_param *
1265 gcc_jit_function_get_param (gcc_jit_function *func, int index)
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);
1279 return static_cast <gcc_jit_param *> (func->get_param (index));
1282 /* Public entrypoint. See description in libgccjit.h.
1284 After error-checking, the real work is done by the
1285 gcc::jit::recording::function::get_params method, in
1286 jit-recording.h.
1289 size_t
1290 gcc_jit_function_get_param_count (gcc_jit_function *func)
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 ();
1298 /* Public entrypoint. See description in libgccjit.h.
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. */
1304 gcc_jit_type *
1305 gcc_jit_function_get_return_type (gcc_jit_function *func)
1307 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function_type");
1308 return (gcc_jit_type *)func->get_return_type ();
1311 /* Public entrypoint. See description in libgccjit.h.
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. */
1317 void
1318 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
1319 const char *path)
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");
1326 func->dump_to_dot (path);
1329 /* Public entrypoint. See description in libgccjit.h.
1331 After error-checking, the real work is done by the
1332 gcc::jit::recording::function::new_block method, in
1333 jit-recording.cc. */
1335 gcc_jit_block*
1336 gcc_jit_function_new_block (gcc_jit_function *func,
1337 const char *name)
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. */
1346 return (gcc_jit_block *)func->new_block (name);
1349 /* Public entrypoint. See description in libgccjit.h.
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. */
1355 gcc_jit_object *
1356 gcc_jit_block_as_object (gcc_jit_block *block)
1358 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1360 return static_cast <gcc_jit_object *> (block->as_object ());
1363 /* Public entrypoint. See description in libgccjit.h.
1365 After error-checking, the real work is done by the
1366 gcc::jit::recording::block::get_function method, in
1367 jit-recording.h. */
1369 gcc_jit_function *
1370 gcc_jit_block_get_function (gcc_jit_block *block)
1372 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1374 return static_cast <gcc_jit_function *> (block->get_function ());
1377 /* Public entrypoint. See description in libgccjit.h.
1379 After error-checking, the real work is done by the
1380 gcc::jit::recording::context::new_global method, in
1381 jit-recording.cc. */
1383 gcc_jit_lvalue *
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)
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);
1413 return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
1416 extern gcc_jit_rvalue *
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)
1424 using namespace gcc::jit::recording;
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");
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 ());
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 ();
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 ());
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);
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);
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'");
1466 size_t idx = 0; /* Runner index for fields in the type object. */
1468 for (size_t i = 0; i < num_values; i++)
1470 gcc::jit::recording::rvalue *rv = values[i];
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;
1475 /* If fields are specified we need to check that they are in
1476 definition order. */
1477 if (fields)
1479 gcc::jit::recording::field *f = fields[i];
1481 RETURN_NULL_IF_FAIL_PRINTF1 (
1483 ctxt, loc,
1484 "NULL field in 'fields', at index %zu", i);
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",
1493 f->get_debug_string (),
1494 type->get_debug_string ());
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++)
1501 field *fs = fields_struct->get_field (j);
1502 if (fs == f)
1504 idx = j; /* Advance runner index for next iteration. */
1505 break;
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)",
1515 type->get_debug_string (),
1516 f->get_debug_string ());
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)",
1527 type->get_debug_string (),
1528 f->get_debug_string (),
1529 f->get_type ()->get_debug_string (),
1530 rv_type->get_debug_string ());
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)
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)",
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 ());
1551 if (rv)
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);
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));
1568 extern gcc_jit_rvalue *
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)
1575 using namespace gcc::jit::recording;
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");
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 ());
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 ();
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 ());
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);
1603 gcc::jit::recording::type *rv_type = value->get_type ();
1605 RETURN_NULL_IF_FAIL (
1606 !rv_type->is_void (),
1607 ctxt, loc,
1608 "can't construct the void type");
1610 if (field)
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 ());
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 ());
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 ());
1646 return (gcc_jit_rvalue *)ctxt->new_ctor (
1647 loc,
1648 type,
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));
1655 extern gcc_jit_rvalue *
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)
1662 using namespace gcc::jit::recording;
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");
1668 RETURN_NULL_IF_FAIL (type->is_array () != NULL,
1669 ctxt, loc,
1670 "constructor type not an array");
1672 if (!num_values)
1673 values = NULL;
1675 if (num_values)
1677 RETURN_NULL_IF_FAIL (
1678 values,
1679 ctxt, loc,
1680 "'values' NULL with non-zero 'num_values'");
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 ();
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);
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++)
1700 if (values[i])
1701 break;
1704 if (i < num_values) /* All values might be null and i == num_values. */
1705 type0 = values[i]->get_type ();
1707 /* If we got a type0, check that all other values have
1708 the same type. */
1709 for (; i < num_values; i++)
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)",
1719 type0->get_debug_string (),
1720 values[i]->get_type ()->get_debug_string ());
1723 /* Compare type0 with the element type specified in the
1724 type of the array. */
1725 if (type0)
1727 gcc::jit::recording::type *el_type =
1728 type->is_array ();
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 ());
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));
1748 /* Public entrypoint. See description in libgccjit.h. */
1750 extern gcc_jit_lvalue *
1751 gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue *global,
1752 gcc_jit_rvalue *init_rvalue)
1754 RETURN_NULL_IF_FAIL (global, NULL, NULL,"NULL global");
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");
1761 RETURN_NULL_IF_FAIL_PRINTF1 (global->is_global (),
1762 ctxt, NULL,
1763 "lvalue \"%s\" not a global",
1764 global->get_debug_string ());
1766 gcc::jit::recording::global *gbl =
1767 reinterpret_cast<gcc::jit::recording::global *> (global);
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 ());
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 ());
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 ());
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);
1798 ctxt->new_global_init_rvalue (global, init_rvalue);
1800 return global;
1803 /* Public entrypoint. See description in libgccjit.h.
1805 After error-checking, the real work is done by the
1806 gcc::jit::recording::global::set_initializer method, in
1807 jit-recording.cc. */
1809 extern gcc_jit_lvalue *
1810 gcc_jit_global_set_initializer (gcc_jit_lvalue *global,
1811 const void *blob,
1812 size_t num_bytes)
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 ());
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);
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 ());
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);
1853 return global;
1856 /* Public entrypoint. See description in libgccjit.h.
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. */
1862 gcc_jit_object *
1863 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1865 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1867 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1870 /* Public entrypoint. See description in libgccjit.h.
1872 After error-checking, this calls the trivial
1873 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1875 gcc_jit_rvalue *
1876 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1878 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1880 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1883 /* Public entrypoint. See description in libgccjit.h.
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. */
1889 gcc_jit_object *
1890 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1892 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1894 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1897 /* Public entrypoint. See description in libgccjit.h.
1899 After error-checking, the real work is done by the
1900 gcc::jit::recording::rvalue::get_type method, in
1901 jit-recording.h. */
1903 gcc_jit_type *
1904 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1906 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1908 return static_cast <gcc_jit_type *> (rvalue->get_type ());
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). */
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
1924 /* Public entrypoint. See description in libgccjit.h.
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. */
1930 gcc_jit_rvalue *
1931 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1932 gcc_jit_type *numeric_type,
1933 int value)
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);
1939 return ((gcc_jit_rvalue *)ctxt
1940 ->new_rvalue_from_const <int> (numeric_type, value));
1943 /* Public entrypoint. See description in libgccjit.h.
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. */
1949 gcc_jit_rvalue *
1950 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1951 gcc_jit_type *numeric_type,
1952 long value)
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);
1958 return ((gcc_jit_rvalue *)ctxt
1959 ->new_rvalue_from_const <long> (numeric_type, value));
1962 /* Public entrypoint. See description in libgccjit.h.
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. */
1968 gcc_jit_rvalue *
1969 gcc_jit_context_zero (gcc_jit_context *ctxt,
1970 gcc_jit_type *numeric_type)
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);
1976 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1979 /* Public entrypoint. See description in libgccjit.h.
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. */
1985 gcc_jit_rvalue *
1986 gcc_jit_context_one (gcc_jit_context *ctxt,
1987 gcc_jit_type *numeric_type)
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);
1993 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1996 /* Public entrypoint. See description in libgccjit.h.
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. */
2002 gcc_jit_rvalue *
2003 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
2004 gcc_jit_type *numeric_type,
2005 double value)
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);
2011 return ((gcc_jit_rvalue *)ctxt
2012 ->new_rvalue_from_const <double> (numeric_type, value));
2015 /* Public entrypoint. See description in libgccjit.h.
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. */
2021 gcc_jit_rvalue *
2022 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
2023 gcc_jit_type *pointer_type,
2024 void *value)
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 ());
2035 return ((gcc_jit_rvalue *)ctxt
2036 ->new_rvalue_from_const <void *> (pointer_type, value));
2039 /* Public entrypoint. See description in libgccjit.h.
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. */
2045 gcc_jit_rvalue *
2046 gcc_jit_context_null (gcc_jit_context *ctxt,
2047 gcc_jit_type *pointer_type)
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 ());
2058 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
2061 /* Public entrypoint. See description in libgccjit.h.
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. */
2067 gcc_jit_rvalue *
2068 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
2069 const char *value)
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");
2075 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
2078 /* Public entrypoint. See description in libgccjit.h.
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. */
2084 gcc_jit_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)
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");
2110 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
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. */
2117 static bool
2118 valid_binary_op_p (enum gcc_jit_binary_op op)
2120 return (op >= GCC_JIT_BINARY_OP_PLUS
2121 && op <= GCC_JIT_BINARY_OP_RSHIFT);
2124 /* Public entrypoint. See description in libgccjit.h.
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. */
2130 gcc_jit_rvalue *
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)
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 ());
2166 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
2169 /* Public entrypoint. See description in libgccjit.h.
2171 After error-checking, the real work is done by the
2172 gcc::jit::recording::context::new_comparison method in
2173 jit-recording.cc. */
2175 gcc_jit_rvalue *
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)
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 ());
2202 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
2205 /* Public entrypoint. See description in libgccjit.h.
2207 After error-checking, the real work is done by the
2208 gcc::jit::recording::context::new_call method in
2209 jit-recording.cc. */
2211 gcc_jit_rvalue *
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)
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");
2224 int min_num_params = func->get_params ().length ();
2225 bool is_variadic = func->is_variadic ();
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);
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);
2243 for (int i = 0; i < min_num_params; i++)
2245 gcc::jit::recording::param *param = func->get_param (i);
2246 gcc_jit_rvalue *arg = args[i];
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 ());
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 ());
2272 return (gcc_jit_rvalue *)ctxt->new_call (loc,
2273 func,
2274 numargs,
2275 (gcc::jit::recording::rvalue **)args);
2278 /* Public entrypoint. See description in libgccjit.h.
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. */
2284 gcc_jit_rvalue *
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)
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");
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 ());
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 ());
2314 int min_num_params = fn_type->get_param_types ().length ();
2315 bool is_variadic = fn_type->is_variadic ();
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);
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);
2333 for (int i = 0; i < min_num_params; i++)
2335 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
2336 gcc_jit_rvalue *arg = args[i];
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 ());
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 ());
2361 return (gcc_jit_rvalue *)(
2362 ctxt->new_call_through_ptr (loc,
2363 fn_ptr,
2364 numargs,
2365 (gcc::jit::recording::rvalue **)args));
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.
2371 We only permit these kinds of cast:
2373 int <-> float
2374 int <-> bool
2375 P* <-> Q* for pointer types P and Q. */
2377 static bool
2378 is_valid_cast (gcc::jit::recording::type *src_type,
2379 gcc_jit_type *dst_type)
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 ();
2388 if (src_is_int)
2389 if (dst_is_int || dst_is_float || dst_is_bool)
2390 return true;
2392 if (src_is_float)
2393 if (dst_is_int || dst_is_float)
2394 return true;
2396 if (src_is_bool)
2397 if (dst_is_int || dst_is_bool)
2398 return true;
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;
2406 return false;
2409 /* Public entrypoint. See description in libgccjit.h.
2411 After error-checking, the real work is done by the
2412 gcc::jit::recording::context::new_cast method in jit-recording.cc. */
2414 gcc_jit_rvalue *
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)
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 ());
2433 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
2436 /* Public entrypoint. See description in libgccjit.h.
2438 After error-checking, the real work is done by the
2439 gcc::jit::recording::context::new_bitcast method in jit-recording.c. */
2441 gcc_jit_rvalue *
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)
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. */
2455 return static_cast <gcc_jit_rvalue *> (ctxt->new_bitcast (loc, rvalue, type));
2458 /* Public entrypoint. See description in libgccjit.h.
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. */
2464 extern gcc_jit_lvalue *
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)
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 ());
2488 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
2491 /* Public entrypoint. See description in libgccjit.h.
2493 After error-checking, the real work is done by the
2494 gcc::jit::recording::memento::get_context method in
2495 jit-recording.h. */
2497 gcc_jit_context *
2498 gcc_jit_object_get_context (gcc_jit_object *obj)
2500 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
2502 return static_cast <gcc_jit_context *> (obj->get_context ());
2505 /* Public entrypoint. See description in libgccjit.h.
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. */
2511 const char *
2512 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
2514 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
2516 return obj->get_debug_string ();
2519 /* Public entrypoint. See description in libgccjit.h.
2521 After error-checking, the real work is done by the
2522 gcc::jit::recording::lvalue::access_field method in
2523 jit-recording.cc. */
2525 gcc_jit_lvalue *
2526 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
2527 gcc_jit_location *loc,
2528 gcc_jit_field *field)
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 ());
2548 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
2551 /* Public entrypoint. See description in libgccjit.h.
2553 After error-checking, the real work is done by the
2554 gcc::jit::recording::rvalue::access_field method in
2555 jit-recording.cc. */
2557 gcc_jit_rvalue *
2558 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
2559 gcc_jit_location *loc,
2560 gcc_jit_field *field)
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 ());
2580 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
2583 /* Public entrypoint. See description in libgccjit.h.
2585 After error-checking, the real work is done by the
2586 gcc::jit::recording::rvalue::deference_field method in
2587 jit-recording.cc. */
2589 gcc_jit_lvalue *
2590 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
2591 gcc_jit_location *loc,
2592 gcc_jit_field *field)
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 ());
2618 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
2621 /* Public entrypoint. See description in libgccjit.h.
2623 After error-checking, the real work is done by the
2624 gcc::jit::recording::rvalue::deference method in
2625 jit-recording.cc. */
2627 gcc_jit_lvalue *
2628 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
2629 gcc_jit_location *loc)
2631 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
2632 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
2633 /* LOC can be NULL. */
2635 gcc::jit::recording::type *underlying_type =
2636 rvalue->get_type ()->is_pointer ();
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 ());
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 ());
2652 return (gcc_jit_lvalue *)rvalue->dereference (loc);
2655 /* Public entrypoint. See description in libgccjit.h.
2657 After error-checking, the real work is done by the
2658 gcc::jit::recording::lvalue::get_address method in jit-recording.cc. */
2660 gcc_jit_rvalue *
2661 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
2662 gcc_jit_location *loc)
2664 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
2665 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
2666 /* LOC can be NULL. */
2668 return (gcc_jit_rvalue *)lvalue->get_address (loc);
2671 /* Public entrypoint. See description in libgccjit.h.
2673 After error-checking, the real work is done by the
2674 gcc::jit::recording::lvalue::set_tls_model method in jit-recording.cc. */
2676 void
2677 gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,
2678 enum gcc_jit_tls_model model)
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 ());
2686 lvalue->set_tls_model (model);
2689 /* Public entrypoint. See description in libgccjit.h.
2691 After error-checking, the real work is done by the
2692 gcc::jit::recording::lvalue::set_link_section method in jit-recording.cc. */
2694 void
2695 gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,
2696 const char *section_name)
2698 RETURN_IF_FAIL (section_name, NULL, NULL, "NULL section_name");
2699 lvalue->set_link_section (section_name);
2702 /* Public entrypoint. See description in libgccjit.h.
2704 After error-checking, the real work is done by the
2705 gcc::jit::recording::lvalue::get_alignment method in jit-recording.cc. */
2707 unsigned
2708 gcc_jit_lvalue_get_alignment (gcc_jit_lvalue *lvalue)
2710 RETURN_VAL_IF_FAIL (lvalue, 0, NULL, NULL, "NULL lvalue");
2711 return lvalue->get_alignment ();
2714 /* Public entrypoint. See description in libgccjit.h.
2716 After error-checking, the real work is done by the
2717 gcc::jit::recording::lvalue::set_alignment method in jit-recording.cc. */
2719 void
2720 gcc_jit_lvalue_set_alignment (gcc_jit_lvalue *lvalue,
2721 unsigned bytes)
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);
2729 /* Public entrypoint. See description in libgccjit.h.
2731 After error-checking, the real work is done by the
2732 gcc::jit::recording::lvalue::set_register_name method in jit-recording.cc. */
2734 void
2735 gcc_jit_lvalue_set_register_name (gcc_jit_lvalue *lvalue,
2736 const char *reg_name)
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);
2743 /* Public entrypoint. See description in libgccjit.h.
2745 After error-checking, the real work is done by the
2746 gcc::jit::recording::function::new_local method in jit-recording.cc. */
2748 gcc_jit_lvalue *
2749 gcc_jit_function_new_local (gcc_jit_function *func,
2750 gcc_jit_location *loc,
2751 gcc_jit_type *type,
2752 const char *name)
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);
2775 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
2778 /* Public entrypoint. See description in libgccjit.h.
2780 After error-checking, the real work is done by the
2781 gcc::jit::recording::block::add_eval method in jit-recording.cc. */
2783 void
2784 gcc_jit_block_add_eval (gcc_jit_block *block,
2785 gcc_jit_location *loc,
2786 gcc_jit_rvalue *rvalue)
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");
2794 gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
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);
2804 /* Public entrypoint. See description in libgccjit.h.
2806 After error-checking, the real work is done by the
2807 gcc::jit::recording::block::add_assignment method in
2808 jit-recording.cc. */
2810 void
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)
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 ());
2833 gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
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);
2844 /* Public entrypoint. See description in libgccjit.h.
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. */
2850 void
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)
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 ());
2879 gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
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);
2890 /* Internal helper function for determining if rvalue BOOLVAL is of
2891 boolean type. For use by gcc_jit_block_end_with_conditional. */
2893 static bool
2894 is_bool (gcc_jit_rvalue *boolval)
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;
2902 /* Public entrypoint. See description in libgccjit.h.
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. */
2908 void
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)
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 ());
2948 gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
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);
2958 /* Public entrypoint. See description in libgccjit.h.
2960 After error-checking, the real work is done by the
2961 gcc::jit::recording::block::add_comment method in
2962 jit-recording.cc. */
2964 void
2965 gcc_jit_block_add_comment (gcc_jit_block *block,
2966 gcc_jit_location *loc,
2967 const char *text)
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");
2975 block->add_comment (loc, text);
2978 /* Public entrypoint. See description in libgccjit.h.
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. */
2984 void
2985 gcc_jit_block_end_with_jump (gcc_jit_block *block,
2986 gcc_jit_location *loc,
2987 gcc_jit_block *target)
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 ());
3005 block->end_with_jump (loc, target);
3008 /* Public entrypoint. See description in libgccjit.h.
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. */
3014 void
3015 gcc_jit_block_end_with_return (gcc_jit_block *block,
3016 gcc_jit_location *loc,
3017 gcc_jit_rvalue *rvalue)
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 ());
3037 gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
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);
3047 /* Public entrypoint. See description in libgccjit.h.
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. */
3053 void
3054 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
3055 gcc_jit_location *loc)
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 ());
3070 block->end_with_return (loc, NULL);
3073 /* Public entrypoint. See description in libgccjit.h.
3075 After error-checking, the real work is done by the
3076 gcc::jit::recording::context::new_case method in
3077 jit-recording.cc. */
3079 gcc_jit_case *
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)
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");
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 ());
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);
3126 /* Public entrypoint. See description in libgccjit.h.
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. */
3132 gcc_jit_object *
3133 gcc_jit_case_as_object (gcc_jit_case *case_)
3135 RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case");
3137 return static_cast <gcc_jit_object *> (case_->as_object ());
3140 /* Helper function for gcc_jit_block_end_with_switch and
3141 valid_case_for_switch. */
3143 static bool
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)
3151 if (!dest_block)
3153 jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc);
3154 return false;
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)
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;
3173 return true;
3176 /* Helper function for gcc_jit_block_end_with_switch. */
3178 static bool
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)
3188 if (!case_)
3190 jit_error (ctxt, loc,
3191 "%s:"
3192 " NULL case %i",
3193 api_funcname,
3194 case_idx);
3195 return false;
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 ())
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;
3219 if (expr_type != case_->get_max_value ()->get_type ())
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;
3234 return true;
3237 /* A class for holding the data we need to perform error-checking
3238 on a libgccjit API call. */
3240 class api_call_validator
3242 public:
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)
3251 protected:
3252 gcc::jit::recording::context *m_ctxt;
3253 gcc_jit_location *m_loc;
3254 const char *m_funcname;
3257 /* A class for verifying that the ranges of cases within
3258 gcc_jit_block_end_with_switch don't overlap. */
3260 class case_range_validator : public api_call_validator
3262 public:
3263 case_range_validator (gcc::jit::recording::context *ctxt,
3264 gcc_jit_location *loc,
3265 const char *funcname);
3267 bool
3268 validate (gcc_jit_case *case_, int idx);
3270 private:
3271 static int
3272 case_compare (gcc::jit::recording::rvalue *k1,
3273 gcc::jit::recording::rvalue *k2);
3275 static wide_int
3276 get_wide_int (gcc::jit::recording::rvalue *k);
3278 private:
3279 typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases;
3282 /* case_range_validator's ctor. */
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)
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. */
3298 bool
3299 case_range_validator::validate (gcc_jit_case *case_,
3300 int case_idx)
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 ());
3306 /* If there was not an exact match, check for overlapping ranges. */
3307 if (!other)
3309 gcc_jit_case *pred;
3310 gcc_jit_case *succ;
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 ());
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)
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;
3332 if (!other && succ)
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;
3343 /* If there was an overlap, issue an error. */
3344 if (other)
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;
3356 /* Register this case label in the splay tree. */
3357 m_cases.insert (case_->get_min_value (),
3358 case_);
3359 return true;
3362 /* Compare with c-family/c-common.cc:case_compare, which acts on tree
3363 nodes, rather than rvalue *.
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.
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. */
3373 case_range_validator::case_compare (gcc::jit::recording::rvalue * k1,
3374 gcc::jit::recording::rvalue * k2)
3376 wide_int wi1 = get_wide_int (k1);
3377 wide_int wi2 = get_wide_int (k2);
3378 return wi::cmps(wi1, wi2);
3381 /* Given a const int rvalue K, get the underlying value as a wide_int. */
3383 wide_int
3384 case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k)
3386 wide_int wi;
3387 bool got_wi = k->get_wide_int (&wi);
3388 gcc_assert (got_wi);
3389 return wi;
3392 /* Public entrypoint. See description in libgccjit.h.
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. */
3398 void
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)
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++)
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,
3439 return;
3440 if (!crv.validate (cases[i], i))
3441 return;
3444 block->end_with_switch (loc, expr, default_block,
3445 num_cases,
3446 (gcc::jit::recording::case_ **)cases);
3449 /**********************************************************************
3450 Option-management
3451 **********************************************************************/
3453 /* Public entrypoint. See description in libgccjit.h.
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. */
3459 void
3460 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
3461 enum gcc_jit_str_option opt,
3462 const char *value)
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. */
3469 ctxt->set_str_option (opt, value);
3472 /* Public entrypoint. See description in libgccjit.h.
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. */
3478 void
3479 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
3480 enum gcc_jit_int_option opt,
3481 int value)
3483 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3484 JIT_LOG_FUNC (ctxt->get_logger ());
3485 /* opt is checked by the inner function. */
3487 ctxt->set_int_option (opt, value);
3490 /* Public entrypoint. See description in libgccjit.h.
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. */
3496 void
3497 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
3498 enum gcc_jit_bool_option opt,
3499 int value)
3501 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3502 JIT_LOG_FUNC (ctxt->get_logger ());
3503 /* opt is checked by the inner function. */
3505 ctxt->set_bool_option (opt, value);
3508 /* Public entrypoint. See description in libgccjit.h.
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. */
3514 void
3515 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
3516 int bool_value)
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);
3525 /* Public entrypoint. See description in libgccjit.h.
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. */
3531 void
3532 gcc_jit_context_set_bool_print_errors_to_stderr (gcc_jit_context *ctxt,
3533 int enabled)
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);
3542 /* Public entrypoint. See description in libgccjit.h.
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. */
3548 extern void
3549 gcc_jit_context_set_bool_use_external_driver (gcc_jit_context *ctxt,
3550 int bool_value)
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);
3559 /* Public entrypoint. See description in libgccjit.h.
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. */
3565 void
3566 gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
3567 const char *optname)
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);
3575 ctxt->add_command_line_option (optname);
3578 /* Public entrypoint. See description in libgccjit.h.
3580 The real work is done by the
3581 gcc::jit::recording::context::add_driver_option method in
3582 jit-recording.cc. */
3584 void
3585 gcc_jit_context_add_driver_option (gcc_jit_context *ctxt,
3586 const char *optname)
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);
3594 ctxt->add_driver_option (optname);
3597 /* Public entrypoint. See description in libgccjit.h.
3599 After error-checking, the real work is done by the
3600 gcc::jit::recording::context::enable_dump method in
3601 jit-recording.cc. */
3603 void
3604 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
3605 const char *dumpname,
3606 char **out_ptr)
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");
3613 ctxt->enable_dump (dumpname, out_ptr);
3616 /* Public entrypoint. See description in libgccjit.h.
3618 After error-checking, the real work is done by the
3619 gcc::jit::recording::context::compile method in
3620 jit-recording.cc. */
3622 gcc_jit_result *
3623 gcc_jit_context_compile (gcc_jit_context *ctxt)
3625 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3627 JIT_LOG_FUNC (ctxt->get_logger ());
3629 ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
3631 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
3633 ctxt->log ("%s: returning (gcc_jit_result *)%p",
3634 __func__, (void *)result);
3636 return result;
3639 /* Public entrypoint. See description in libgccjit.h.
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. */
3645 void
3646 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
3647 enum gcc_jit_output_kind output_kind,
3648 const char *output_path)
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");
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);
3664 ctxt->compile_to_file (output_kind, output_path);
3668 /* Public entrypoint. See description in libgccjit.h.
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. */
3674 void
3675 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
3676 const char *path,
3677 int update_locations)
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);
3685 /* Public entrypoint. See description in libgccjit.h. */
3687 void
3688 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
3689 FILE *logfile,
3690 int flags,
3691 int verbosity)
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");
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);
3706 /* Public entrypoint. See description in libgccjit.h.
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. */
3712 void
3713 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
3714 const char *path)
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);
3722 /* Public entrypoint. See description in libgccjit.h.
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. */
3728 const char *
3729 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
3731 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3732 JIT_LOG_FUNC (ctxt->get_logger ());
3734 return ctxt->get_first_error ();
3737 /* Public entrypoint. See description in libgccjit.h.
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. */
3743 const char *
3744 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
3746 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3748 return ctxt->get_last_error ();
3751 /* Public entrypoint. See description in libgccjit.h.
3753 After error-checking, the real work is done by the
3754 gcc::jit::result::get_code method in jit-result.cc. */
3756 void *
3757 gcc_jit_result_get_code (gcc_jit_result *result,
3758 const char *fnname)
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");
3764 result->log ("locating fnname: %s", fnname);
3765 void *code = result->get_code (fnname);
3766 result->log ("%s: returning (void *)%p", __func__, code);
3768 return code;
3771 /* Public entrypoint. See description in libgccjit.h.
3773 After error-checking, the real work is done by the
3774 gcc::jit::result::get_global method in jit-result.cc. */
3776 void *
3777 gcc_jit_result_get_global (gcc_jit_result *result,
3778 const char *name)
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");
3784 void *global = result->get_global (name);
3785 result->log ("%s: returning (void *)%p", __func__, global);
3787 return global;
3790 /* Public entrypoint. See description in libgccjit.h.
3792 After error-checking, this is essentially a wrapper around the
3793 destructor for gcc::jit::result in jit-result.cc. */
3795 void
3796 gcc_jit_result_release (gcc_jit_result *result)
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;
3804 /**********************************************************************
3805 Timing support.
3806 **********************************************************************/
3808 /* Create a gcc_jit_timer instance, and start timing. */
3810 gcc_jit_timer *
3811 gcc_jit_timer_new (void)
3813 gcc_jit_timer *timer = new gcc_jit_timer ();
3814 timer->start (TV_TOTAL);
3815 timer->push (TV_JIT_CLIENT_CODE);
3816 return timer;
3819 /* Release a gcc_jit_timer instance. */
3821 void
3822 gcc_jit_timer_release (gcc_jit_timer *timer)
3824 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
3826 delete timer;
3829 /* Associate a gcc_jit_timer instance with a context. */
3831 void
3832 gcc_jit_context_set_timer (gcc_jit_context *ctxt,
3833 gcc_jit_timer *timer)
3835 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
3836 RETURN_IF_FAIL (timer, ctxt, NULL, "NULL timer");
3838 ctxt->set_timer (timer);
3841 /* Get the timer associated with a context (if any). */
3843 gcc_jit_timer *
3844 gcc_jit_context_get_timer (gcc_jit_context *ctxt)
3846 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
3848 return (gcc_jit_timer *)ctxt->get_timer ();
3851 /* Push the given item onto the timing stack. */
3853 void
3854 gcc_jit_timer_push (gcc_jit_timer *timer,
3855 const char *item_name)
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);
3862 /* Pop the top item from the timing stack. */
3864 void
3865 gcc_jit_timer_pop (gcc_jit_timer *timer,
3866 const char *item_name)
3868 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
3870 if (item_name)
3872 const char *top_item_name = timer->get_topmost_item_name ();
3874 RETURN_IF_FAIL_PRINTF1
3875 (top_item_name, NULL, NULL,
3876 "pop of empty timing stack (attempting to pop: \"%s\")",
3877 item_name);
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);
3888 timer->pop_client_item ();
3891 /* Print timing information to the given stream about activity since
3892 the timer was started. */
3894 void
3895 gcc_jit_timer_print (gcc_jit_timer *timer,
3896 FILE *f_out)
3898 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
3899 RETURN_IF_FAIL (f_out, NULL, NULL, "NULL f_out");
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);
3908 /* Public entrypoint. See description in libgccjit.h.
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. */
3913 void
3914 gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *rvalue,
3915 int require_tail_call)
3917 RETURN_IF_FAIL (rvalue, NULL, NULL, "NULL call");
3918 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
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 ());
3925 call->set_require_tail_call (require_tail_call);
3928 /* Public entrypoint. See description in libgccjit.h.
3930 After error-checking, the real work is done by the
3931 gcc::jit::recording::type::get_aligned method, in
3932 jit-recording.cc. */
3934 gcc_jit_type *
3935 gcc_jit_type_get_aligned (gcc_jit_type *type,
3936 size_t alignment_in_bytes)
3938 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
3940 gcc::jit::recording::context *ctxt = type->m_ctxt;
3942 JIT_LOG_FUNC (ctxt->get_logger ());
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");
3950 return (gcc_jit_type *)type->get_aligned (alignment_in_bytes);
3953 /* Public entrypoint. See description in libgccjit.h.
3955 After error-checking, the real work is done by the
3956 gcc::jit::recording::type::get_vector method, in
3957 jit-recording.cc. */
3959 gcc_jit_type *
3960 gcc_jit_type_get_vector (gcc_jit_type *type, size_t num_units)
3962 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
3964 gcc::jit::recording::context *ctxt = type->m_ctxt;
3966 JIT_LOG_FUNC (ctxt->get_logger ());
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 ());
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);
3978 return (gcc_jit_type *)type->get_vector (num_units);
3981 /* Public entrypoint. See description in libgccjit.h.
3983 After error-checking, the real work is done by the
3984 gcc::jit::recording::function::get_address method, in
3985 jit-recording.cc. */
3987 gcc_jit_rvalue *
3988 gcc_jit_function_get_address (gcc_jit_function *fn,
3989 gcc_jit_location *loc)
3991 RETURN_NULL_IF_FAIL (fn, NULL, NULL, "NULL function");
3993 gcc::jit::recording::context *ctxt = fn->m_ctxt;
3995 JIT_LOG_FUNC (ctxt->get_logger ());
3996 /* LOC can be NULL. */
3998 return (gcc_jit_rvalue *)fn->get_address (loc);
4001 /* Public entrypoint. See description in libgccjit.h.
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. */
4007 extern gcc_jit_rvalue *
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)
4014 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL ctxt");
4015 JIT_LOG_FUNC (ctxt->get_logger ());
4017 /* LOC can be NULL. */
4018 RETURN_NULL_IF_FAIL (vec_type, ctxt, loc, "NULL vec_type");
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 ());
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 ());
4032 /* "elements must be non-NULL. */
4033 RETURN_NULL_IF_FAIL (elements, ctxt, loc, "NULL elements");
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++)
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)",
4048 element_type->get_debug_string (),
4049 elements[i]->get_debug_string (),
4050 elements[i]->get_type ()->get_debug_string ());
4053 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_vector
4054 (loc,
4055 as_vec_type,
4056 (gcc::jit::recording::rvalue **)elements);
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. */
4063 static std::mutex version_mutex;
4065 struct jit_version_info
4067 /* Default constructor. Populate via parse_basever,
4068 guarded by version_mutex. */
4069 jit_version_info ()
4071 std::lock_guard<std::mutex> g (version_mutex);
4072 parse_basever (&major, &minor, &patchlevel);
4075 int major;
4076 int minor;
4077 int patchlevel;
4081 extern int
4082 gcc_jit_version_major (void)
4084 jit_version_info vi;
4085 return vi.major;
4088 extern int
4089 gcc_jit_version_minor (void)
4091 jit_version_info vi;
4092 return vi.minor;
4095 extern int
4096 gcc_jit_version_patchlevel (void)
4098 jit_version_info vi;
4099 return vi.patchlevel;
4102 /**********************************************************************
4103 Asm support.
4104 **********************************************************************/
4106 /* Public entrypoint. See description in libgccjit.h.
4108 After error-checking, the real work is done by the
4109 gcc::jit::recording::block::add_extended_asm, in
4110 jit-recording.cc. */
4112 gcc_jit_extended_asm *
4113 gcc_jit_block_add_extended_asm (gcc_jit_block *block,
4114 gcc_jit_location *loc,
4115 const char *asm_template)
4117 RETURN_NULL_IF_NOT_VALID_BLOCK (block, loc);
4118 gcc::jit::recording::context *ctxt = block->get_context ();
4119 JIT_LOG_FUNC (ctxt->get_logger ());
4120 /* LOC can be NULL. */
4121 RETURN_NULL_IF_FAIL (asm_template, ctxt, loc, "NULL asm_template");
4123 return (gcc_jit_extended_asm *)block->add_extended_asm (loc, asm_template);
4126 /* Public entrypoint. See description in libgccjit.h.
4128 After error-checking, the real work is done by the
4129 gcc::jit::recording::block::end_with_extended_asm_goto, in
4130 jit-recording.cc. */
4132 gcc_jit_extended_asm *
4133 gcc_jit_block_end_with_extended_asm_goto (gcc_jit_block *block,
4134 gcc_jit_location *loc,
4135 const char *asm_template,
4136 int num_goto_blocks,
4137 gcc_jit_block **goto_blocks,
4138 gcc_jit_block *fallthrough_block)
4140 RETURN_NULL_IF_NOT_VALID_BLOCK (block, loc);
4141 gcc::jit::recording::context *ctxt = block->get_context ();
4142 JIT_LOG_FUNC (ctxt->get_logger ());
4143 /* LOC can be NULL. */
4144 RETURN_NULL_IF_FAIL (asm_template, ctxt, loc, "NULL asm_template");
4145 RETURN_NULL_IF_FAIL (num_goto_blocks >= 0, ctxt, loc, "num_goto_blocks < 0");
4146 for (int i = 0; i < num_goto_blocks; i++)
4147 RETURN_NULL_IF_FAIL_PRINTF1 (goto_blocks[i],
4148 ctxt, loc,
4149 "NULL goto_blocks[%i]", i);
4150 /* fallthrough_block can be NULL. */
4151 return (gcc_jit_extended_asm *)block->end_with_extended_asm_goto
4152 (loc, asm_template,
4153 num_goto_blocks, (gcc::jit::recording::block **)goto_blocks,
4154 fallthrough_block);
4157 /* Public entrypoint. See description in libgccjit.h.
4159 After error-checking, this calls the trivial
4160 gcc::jit::recording::memento::as_object method (an extended_asm is a
4161 memento), in jit-recording.h. */
4163 gcc_jit_object *
4164 gcc_jit_extended_asm_as_object (gcc_jit_extended_asm *ext_asm)
4166 RETURN_NULL_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4168 return static_cast <gcc_jit_object *> (ext_asm->as_object ());
4171 /* Public entrypoint. See description in libgccjit.h.
4173 After error-checking, the real work is done by the
4174 gcc::jit::recording::extended_asm::set_volatile_flag, in
4175 jit-recording.cc. */
4177 void
4178 gcc_jit_extended_asm_set_volatile_flag (gcc_jit_extended_asm *ext_asm,
4179 int flag)
4181 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4182 ext_asm->set_volatile_flag (flag);
4185 /* Public entrypoint. See description in libgccjit.h.
4187 After error-checking, the real work is done by the
4188 gcc::jit::recording::extended_asm::set_inline_flag, in
4189 jit-recording.cc. */
4191 void
4192 gcc_jit_extended_asm_set_inline_flag (gcc_jit_extended_asm *ext_asm,
4193 int flag)
4195 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4196 ext_asm->set_inline_flag (flag);
4199 /* Public entrypoint. See description in libgccjit.h.
4201 After error-checking, the real work is done by the
4202 gcc::jit::recording::extended_asm::add_output_operand, in
4203 jit-recording.cc. */
4205 void
4206 gcc_jit_extended_asm_add_output_operand (gcc_jit_extended_asm *ext_asm,
4207 const char *asm_symbolic_name,
4208 const char *constraint,
4209 gcc_jit_lvalue *dest)
4211 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4212 gcc::jit::recording::context *ctxt = ext_asm->get_context ();
4213 JIT_LOG_FUNC (ctxt->get_logger ());
4214 gcc::jit::recording::location *loc = ext_asm->get_loc ();
4215 /* asm_symbolic_name can be NULL. */
4216 RETURN_IF_FAIL (constraint, ctxt, loc, "NULL constraint");
4217 RETURN_IF_FAIL (dest, ctxt, loc, "NULL dest");
4218 RETURN_IF_FAIL (!ext_asm->is_goto (), ctxt, loc,
4219 "cannot add output operand to asm goto");
4220 ext_asm->add_output_operand (asm_symbolic_name, constraint, dest);
4223 /* Public entrypoint. See description in libgccjit.h.
4225 After error-checking, the real work is done by the
4226 gcc::jit::recording::extended_asm::add_input_operand, in
4227 jit-recording.cc. */
4229 extern void
4230 gcc_jit_extended_asm_add_input_operand (gcc_jit_extended_asm *ext_asm,
4231 const char *asm_symbolic_name,
4232 const char *constraint,
4233 gcc_jit_rvalue *src)
4235 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4236 gcc::jit::recording::context *ctxt = ext_asm->get_context ();
4237 JIT_LOG_FUNC (ctxt->get_logger ());
4238 gcc::jit::recording::location *loc = ext_asm->get_loc ();
4239 /* asm_symbolic_name can be NULL. */
4240 RETURN_IF_FAIL (constraint, ctxt, loc, "NULL constraint");
4241 RETURN_IF_FAIL (src, ctxt, loc, "NULL src");
4242 ext_asm->add_input_operand (asm_symbolic_name, constraint, src);
4245 /* Public entrypoint. See description in libgccjit.h.
4247 After error-checking, the real work is done by the
4248 gcc::jit::recording::extended_asm::add_clobber, in
4249 jit-recording.cc. */
4251 void
4252 gcc_jit_extended_asm_add_clobber (gcc_jit_extended_asm *ext_asm,
4253 const char *victim)
4255 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4256 gcc::jit::recording::context *ctxt = ext_asm->get_context ();
4257 JIT_LOG_FUNC (ctxt->get_logger ());
4258 gcc::jit::recording::location *loc = ext_asm->get_loc ();
4259 RETURN_IF_FAIL (victim, ctxt, loc, "NULL victim");
4260 ext_asm->add_clobber (victim);
4263 /* Public entrypoint. See description in libgccjit.h.
4265 After error-checking, the real work is done by the
4266 gcc::jit::recording::context::add_top_level_asm, in
4267 jit-recording.cc. */
4269 void
4270 gcc_jit_context_add_top_level_asm (gcc_jit_context *ctxt,
4271 gcc_jit_location *loc,
4272 const char *asm_stmts)
4274 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
4275 JIT_LOG_FUNC (ctxt->get_logger ());
4276 /* LOC can be NULL. */
4277 RETURN_IF_FAIL (asm_stmts, ctxt, NULL, "NULL asm_stmts");
4278 ctxt->add_top_level_asm (loc, asm_stmts);