Daily bump.
[official-gcc.git] / gcc / jit / libgccjit.cc
blobeb38da91df1ec9f46b00be68b2b11863a64468bc
1 /* Implementation of the C API; all wrappers into the internal C++ API
2 Copyright (C) 2013-2024 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_restrict method, in
541 jit-recording.cc. */
543 gcc_jit_type *
544 gcc_jit_type_get_restrict (gcc_jit_type *type)
546 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
547 RETURN_NULL_IF_FAIL (type->is_pointer (), NULL, NULL, "not a pointer type");
549 return (gcc_jit_type *)type->get_restrict ();
552 /* Public entrypoint. See description in libgccjit.h.
554 After error-checking, the real work is done by the
555 gcc::jit::recording::type::get_size method, in
556 jit-recording.cc. */
558 ssize_t
559 gcc_jit_type_get_size (gcc_jit_type *type)
561 RETURN_VAL_IF_FAIL (type, -1, NULL, NULL, "NULL type");
562 RETURN_VAL_IF_FAIL
563 (type->is_int () || type->is_float () || type->is_pointer (), -1, NULL, NULL,
564 "only getting the size of integer or floating-point or pointer types is supported for now");
565 return type->get_size ();
568 /* Public entrypoint. See description in libgccjit.h.
570 After error-checking, the real work is done by the
571 gcc::jit::recording::type::is_array method, in
572 jit-recording.cc. */
574 gcc_jit_type *
575 gcc_jit_type_dyncast_array (gcc_jit_type *type)
577 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
579 return (gcc_jit_type *)type->is_array ();
582 /* Public entrypoint. See description in libgccjit.h.
584 After error-checking, the real work is done by the
585 gcc::jit::recording::type::is_bool method, in
586 jit-recording.cc. */
589 gcc_jit_type_is_bool (gcc_jit_type *type)
591 RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
593 return type->is_bool ();
596 /* Public entrypoint. See description in libgccjit.h.
598 After error-checking, the real work is done by the
599 gcc::jit::recording::type::is_pointer method, in
600 jit-recording.cc. */
602 gcc_jit_type *
603 gcc_jit_type_is_pointer (gcc_jit_type *type)
605 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
607 return (gcc_jit_type *)type->is_pointer ();
610 /* Public entrypoint. See description in libgccjit.h.
612 After error-checking, the real work is done by the
613 gcc::jit::recording::type::is_int method, in
614 jit-recording.cc. */
617 gcc_jit_type_is_integral (gcc_jit_type *type)
619 RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
621 return type->is_int ();
624 /* Public entrypoint. See description in libgccjit.h.
626 After error-checking, the real work is done by the
627 gcc::jit::recording::type::is_vector method, in
628 jit-recording.cc. */
630 gcc_jit_vector_type *
631 gcc_jit_type_dyncast_vector (gcc_jit_type *type)
633 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
634 gcc::jit::recording::vector_type *vector_type = type->is_vector ();
635 return (gcc_jit_vector_type *)vector_type;
638 /* Public entrypoint. See description in libgccjit.h.
640 After error-checking, the real work is done by the
641 gcc::jit::recording::type::is_struct method, in
642 jit-recording.cc. */
644 gcc_jit_struct *
645 gcc_jit_type_is_struct (gcc_jit_type *type)
647 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
648 gcc::jit::recording::struct_ *struct_type = type->is_struct ();
649 return (gcc_jit_struct *)struct_type;
652 /* Public entrypoint. See description in libgccjit.h.
654 After error-checking, the real work is done by the
655 gcc::jit::recording::vector_type::get_num_units method, in
656 jit-recording.cc. */
658 size_t
659 gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
661 RETURN_VAL_IF_FAIL (vector_type, 0, NULL, NULL, "NULL vector_type");
662 return vector_type->get_num_units ();
665 /* Public entrypoint. See description in libgccjit.h.
667 After error-checking, the real work is done by the
668 gcc::jit::recording::vector_type::get_element_type method, in
669 jit-recording.cc. */
671 gcc_jit_type *
672 gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type)
674 RETURN_NULL_IF_FAIL (vector_type, NULL, NULL, "NULL vector_type");
675 return (gcc_jit_type *)vector_type->get_element_type ();
678 /* Public entrypoint. See description in libgccjit.h.
680 After error-checking, the real work is done by the
681 gcc::jit::recording::type::unqualified method, in
682 jit-recording.cc. */
684 gcc_jit_type *
685 gcc_jit_type_unqualified (gcc_jit_type *type)
687 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
689 return (gcc_jit_type *)type->unqualified ();
692 /* Public entrypoint. See description in libgccjit.h.
694 After error-checking, the real work is done by the
695 gcc::jit::recording::type::dyn_cast_function_type method, in
696 jit-recording.cc. */
698 gcc_jit_function_type *
699 gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type)
701 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
702 gcc::jit::recording::type *func_ptr_type = type->dereference ();
703 if (!func_ptr_type)
705 return NULL;
708 return (gcc_jit_function_type *)func_ptr_type->dyn_cast_function_type ();
711 /* Public entrypoint. See description in libgccjit.h.
713 After error-checking, the real work is done by the
714 gcc::jit::recording::function_type::get_return_type method, in
715 jit-recording.cc. */
717 gcc_jit_type *
718 gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
720 RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL function_type");
721 return (gcc_jit_type *)function_type->get_return_type ();
724 /* Public entrypoint. See description in libgccjit.h.
726 After error-checking, the real work is done by the
727 gcc::jit::recording::function_type::get_param_types method, in
728 jit-recording.cc. */
730 size_t
731 gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
733 RETURN_VAL_IF_FAIL (function_type, 0, NULL, NULL, "NULL function_type");
734 return function_type->get_param_types ().length ();
737 /* Public entrypoint. See description in libgccjit.h.
739 After error-checking, the real work is done by the
740 gcc::jit::recording::function_type::get_param_types method, in
741 jit-recording.cc. */
743 gcc_jit_type *
744 gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
745 size_t index)
747 RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL function_type");
748 size_t num_params = function_type->get_param_types ().length ();
749 gcc::jit::recording::context *ctxt = function_type->m_ctxt;
750 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
751 ctxt, NULL,
752 "index of %zu is too large (%s has %zu params)",
753 index,
754 function_type->get_debug_string (),
755 num_params);
756 return (gcc_jit_type *)function_type->get_param_types ()[index];
759 /* Public entrypoint. See description in libgccjit.h.
761 After error-checking, the real work is done by the
762 gcc::jit::recording::context::new_array_type method, in
763 jit-recording.cc. */
765 gcc_jit_type *
766 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
767 gcc_jit_location *loc,
768 gcc_jit_type *element_type,
769 int num_elements)
771 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
772 JIT_LOG_FUNC (ctxt->get_logger ());
773 /* LOC can be NULL. */
774 RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
775 RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
776 RETURN_NULL_IF_FAIL (!element_type->is_void (), ctxt, loc,
777 "void type for elements");
779 return (gcc_jit_type *)ctxt->new_array_type (loc,
780 element_type,
781 num_elements);
784 /* Public entrypoint. See description in libgccjit.h.
786 After error-checking, the real work is done by the
787 gcc::jit::recording::context::new_field method, in
788 jit-recording.cc. */
790 gcc_jit_field *
791 gcc_jit_context_new_field (gcc_jit_context *ctxt,
792 gcc_jit_location *loc,
793 gcc_jit_type *type,
794 const char *name)
796 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
797 JIT_LOG_FUNC (ctxt->get_logger ());
798 /* LOC can be NULL. */
799 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
800 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
801 RETURN_NULL_IF_FAIL_PRINTF2 (
802 type->has_known_size (),
803 ctxt, loc,
804 "unknown size for field \"%s\" (type: %s)",
805 name,
806 type->get_debug_string ());
807 RETURN_NULL_IF_FAIL_PRINTF1 (
808 !type->is_void (),
809 ctxt, loc,
810 "void type for field \"%s\"",
811 name);
813 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
816 /* Public entrypoint. See description in libgccjit.h.
818 After error-checking, the real work is done by the
819 gcc::jit::recording::context::new_bitfield method, in
820 jit-recording.cc. */
822 gcc_jit_field *
823 gcc_jit_context_new_bitfield (gcc_jit_context *ctxt,
824 gcc_jit_location *loc,
825 gcc_jit_type *type,
826 int width,
827 const char *name)
829 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
830 JIT_LOG_FUNC (ctxt->get_logger ());
831 /* LOC can be NULL. */
832 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
833 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
834 RETURN_NULL_IF_FAIL_PRINTF2 (type->is_int () || type->is_bool (),
835 ctxt, loc,
836 "bit-field %s has non integral type %s",
837 name, type->get_debug_string ());
838 RETURN_NULL_IF_FAIL_PRINTF2 (
839 width > 0, ctxt, loc,
840 "invalid width %d for bitfield \"%s\" (must be > 0)",
841 width, name);
842 RETURN_NULL_IF_FAIL_PRINTF2 (
843 type->has_known_size (),
844 ctxt, loc,
845 "unknown size for field \"%s\" (type: %s)",
846 name,
847 type->get_debug_string ());
849 return (gcc_jit_field *)ctxt->new_bitfield (loc, type, width, name);
852 /* Public entrypoint. See description in libgccjit.h.
854 After error-checking, this calls the trivial
855 gcc::jit::recording::memento::as_object method (a field is a
856 memento), in jit-recording.h. */
858 gcc_jit_object *
859 gcc_jit_field_as_object (gcc_jit_field *field)
861 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
863 return static_cast <gcc_jit_object *> (field->as_object ());
866 /* Public entrypoint. See description in libgccjit.h.
868 After error-checking, the real work is done by the
869 gcc::jit::recording::context::new_struct_type method,
870 immediately followed by a "set_fields" call on the resulting
871 gcc::jit::recording::compound_type *, both in jit-recording.cc */
873 gcc_jit_struct *
874 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
875 gcc_jit_location *loc,
876 const char *name,
877 int num_fields,
878 gcc_jit_field **fields)
880 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
881 JIT_LOG_FUNC (ctxt->get_logger ());
882 /* LOC can be NULL. */
883 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
884 if (num_fields)
885 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
886 for (int i = 0; i < num_fields; i++)
888 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
889 RETURN_NULL_IF_FAIL_PRINTF2 (
890 fields[i]->get_container () == NULL,
891 ctxt, loc,
892 "%s is already a field of %s",
893 fields[i]->get_debug_string (),
894 fields[i]->get_container ()->get_debug_string ());
897 gcc::jit::recording::struct_ *result =
898 ctxt->new_struct_type (loc, name);
899 result->set_fields (loc,
900 num_fields,
901 (gcc::jit::recording::field **)fields);
902 return static_cast<gcc_jit_struct *> (result);
905 /* Public entrypoint. See description in libgccjit.h.
907 After error-checking, the real work is done by the
908 gcc::jit::recording::context::new_struct_type method in
909 jit-recording.cc. */
911 gcc_jit_struct *
912 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
913 gcc_jit_location *loc,
914 const char *name)
916 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
917 JIT_LOG_FUNC (ctxt->get_logger ());
918 /* LOC can be NULL. */
919 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
921 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
924 /* Public entrypoint. See description in libgccjit.h.
926 After error-checking, this calls the trivial
927 gcc::jit::recording::struct_::as_object method in
928 jit-recording.h. */
930 gcc_jit_type *
931 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
933 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
935 return static_cast <gcc_jit_type *> (struct_type->as_type ());
938 /* Public entrypoint. See description in libgccjit.h.
940 After error-checking, the real work is done by the
941 gcc::jit::recording::compound_type::set_fields method in
942 jit-recording.cc. */
944 void
945 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
946 gcc_jit_location *loc,
947 int num_fields,
948 gcc_jit_field **fields)
950 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
951 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
952 JIT_LOG_FUNC (ctxt->get_logger ());
953 /* LOC can be NULL. */
954 RETURN_IF_FAIL_PRINTF1 (
955 struct_type->get_fields () == NULL, ctxt, loc,
956 "%s already has had fields set",
957 struct_type->get_debug_string ());
958 if (num_fields)
959 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
960 for (int i = 0; i < num_fields; i++)
962 RETURN_IF_FAIL_PRINTF2 (
963 fields[i],
964 ctxt, loc,
965 "%s: NULL field ptr at index %i",
966 struct_type->get_debug_string (),
968 RETURN_IF_FAIL_PRINTF2 (
969 fields[i]->get_container () == NULL,
970 ctxt, loc,
971 "%s is already a field of %s",
972 fields[i]->get_debug_string (),
973 fields[i]->get_container ()->get_debug_string ());
976 struct_type->set_fields (loc, num_fields,
977 (gcc::jit::recording::field **)fields);
981 /* Public entrypoint. See description in libgccjit.h.
983 After error-checking, the real work is done by the
984 gcc::jit::recording::fields::get_field method in
985 jit-recording.cc. */
986 extern gcc_jit_field *
987 gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
988 size_t index)
990 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct type");
991 RETURN_NULL_IF_FAIL (struct_type->get_fields (), NULL, NULL,
992 "NULL struct fields");
993 size_t num_fields = struct_type->get_fields ()->length ();
994 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_fields,
995 NULL, NULL,
996 "index of %zu is too large (%s has %zu fields)",
997 index,
998 struct_type->get_debug_string (),
999 num_fields);
1000 return (gcc_jit_field *)struct_type->get_fields ()->get_field (index);
1003 /* Public entrypoint. See description in libgccjit.h.
1005 After error-checking, this calls the trivial
1006 gcc::jit::recording::struct_::get_fields method in
1007 jit-recording.h. */
1009 size_t
1010 gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
1012 RETURN_VAL_IF_FAIL (struct_type, 0, NULL, NULL, "NULL struct type");
1013 return struct_type->get_fields ()->length ();
1016 /* Public entrypoint. See description in libgccjit.h.
1018 After error-checking, the real work is done by the
1019 gcc::jit::recording::context::new_union_type method,
1020 immediately followed by a "set_fields" call on the resulting
1021 gcc::jit::recording::compound_type *, both in jit-recording.cc */
1023 gcc_jit_type *
1024 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
1025 gcc_jit_location *loc,
1026 const char *name,
1027 int num_fields,
1028 gcc_jit_field **fields)
1030 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1031 JIT_LOG_FUNC (ctxt->get_logger ());
1032 /* LOC can be NULL. */
1033 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1034 if (num_fields)
1035 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
1036 for (int i = 0; i < num_fields; i++)
1038 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
1039 RETURN_NULL_IF_FAIL_PRINTF2 (
1040 fields[i]->get_container () == NULL,
1041 ctxt, loc,
1042 "%s is already a field of %s",
1043 fields[i]->get_debug_string (),
1044 fields[i]->get_container ()->get_debug_string ());
1047 gcc::jit::recording::union_ *result =
1048 ctxt->new_union_type (loc, name);
1049 result->set_fields (loc,
1050 num_fields,
1051 (gcc::jit::recording::field **)fields);
1052 return (gcc_jit_type *) (result);
1055 /* Public entrypoint. See description in libgccjit.h.
1057 After error-checking, the real work is done by the
1058 gcc::jit::recording::context::new_function_ptr_type method,
1059 in jit-recording.cc */
1061 gcc_jit_type *
1062 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
1063 gcc_jit_location *loc,
1064 gcc_jit_type *return_type,
1065 int num_params,
1066 gcc_jit_type **param_types,
1067 int is_variadic)
1069 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1070 JIT_LOG_FUNC (ctxt->get_logger ());
1071 /* LOC can be NULL. */
1072 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
1073 RETURN_NULL_IF_FAIL (
1074 (num_params == 0) || param_types,
1075 ctxt, loc,
1076 "NULL param_types creating function pointer type");
1077 for (int i = 0; i < num_params; i++)
1079 RETURN_NULL_IF_FAIL_PRINTF1 (param_types[i],
1080 ctxt, loc,
1081 "NULL parameter type %i"
1082 " creating function pointer type", i);
1083 RETURN_NULL_IF_FAIL_PRINTF1 (!param_types[i]->is_void (),
1084 ctxt, loc,
1085 "void type for param %i", i);
1088 return (gcc_jit_type*)
1089 ctxt->new_function_ptr_type (loc, return_type,
1090 num_params,
1091 (gcc::jit::recording::type **)param_types,
1092 is_variadic);
1095 /* Constructing functions. */
1097 /* Public entrypoint. See description in libgccjit.h.
1099 After error-checking, the real work is done by the
1100 gcc::jit::recording::context::new_param method, in jit-recording.cc */
1102 gcc_jit_param *
1103 gcc_jit_context_new_param (gcc_jit_context *ctxt,
1104 gcc_jit_location *loc,
1105 gcc_jit_type *type,
1106 const char *name)
1108 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1109 JIT_LOG_FUNC (ctxt->get_logger ());
1110 /* LOC can be NULL. */
1111 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1112 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1113 RETURN_NULL_IF_FAIL_PRINTF1 (!type->is_void (),
1114 ctxt, loc,
1115 "void type for param \"%s\"", name);
1117 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
1120 /* Public entrypoint. See description in libgccjit.h.
1122 After error-checking, this calls the trivial
1123 gcc::jit::recording::memento::as_object method (a param is a memento),
1124 in jit-recording.h. */
1126 gcc_jit_object *
1127 gcc_jit_param_as_object (gcc_jit_param *param)
1129 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
1131 return static_cast <gcc_jit_object *> (param->as_object ());
1134 /* Public entrypoint. See description in libgccjit.h.
1136 After error-checking, this calls the trivial
1137 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
1139 gcc_jit_lvalue *
1140 gcc_jit_param_as_lvalue (gcc_jit_param *param)
1142 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
1144 return (gcc_jit_lvalue *)param->as_lvalue ();
1147 /* Public entrypoint. See description in libgccjit.h.
1149 After error-checking, this calls the trivial
1150 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
1151 in jit-recording.h. */
1153 gcc_jit_rvalue *
1154 gcc_jit_param_as_rvalue (gcc_jit_param *param)
1156 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
1158 return (gcc_jit_rvalue *)param->as_rvalue ();
1161 /* Public entrypoint. See description in libgccjit.h.
1163 After error-checking, the real work is done by the
1164 gcc::jit::recording::context::new_function method, in
1165 jit-recording.cc. */
1167 gcc_jit_function *
1168 gcc_jit_context_new_function (gcc_jit_context *ctxt,
1169 gcc_jit_location *loc,
1170 enum gcc_jit_function_kind kind,
1171 gcc_jit_type *return_type,
1172 const char *name,
1173 int num_params,
1174 gcc_jit_param **params,
1175 int is_variadic)
1177 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1178 JIT_LOG_FUNC (ctxt->get_logger ());
1179 /* LOC can be NULL. */
1180 RETURN_NULL_IF_FAIL_PRINTF1 (
1181 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
1182 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
1183 ctxt, loc,
1184 "unrecognized value for enum gcc_jit_function_kind: %i",
1185 kind);
1186 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
1187 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1188 /* The assembler can only handle certain names, so for now, enforce
1189 C's rules for identifiers upon the name, using ISALPHA and ISALNUM
1190 from safe-ctype.h to ignore the current locale.
1191 Eventually we'll need some way to interact with e.g. C++ name
1192 mangling. */
1194 /* Leading char: */
1195 char ch = *name;
1196 RETURN_NULL_IF_FAIL_PRINTF2 (
1197 ISALPHA (ch) || ch == '_',
1198 ctxt, loc,
1199 "name \"%s\" contains invalid character: '%c'",
1200 name, ch);
1201 /* Subsequent chars: */
1202 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
1204 RETURN_NULL_IF_FAIL_PRINTF2 (
1205 ISALNUM (ch) || ch == '_',
1206 ctxt, loc,
1207 "name \"%s\" contains invalid character: '%c'",
1208 name, ch);
1211 RETURN_NULL_IF_FAIL_PRINTF1 (
1212 (num_params == 0) || params,
1213 ctxt, loc,
1214 "NULL params creating function %s", name);
1215 for (int i = 0; i < num_params; i++)
1217 RETURN_NULL_IF_FAIL_PRINTF2 (
1218 params[i],
1219 ctxt, loc,
1220 "NULL parameter %i creating function %s", i, name);
1221 RETURN_NULL_IF_FAIL_PRINTF5 (
1222 params[i]->get_scope () == NULL,
1223 ctxt, loc,
1224 "parameter %i \"%s\""
1225 " (type: %s)"
1226 " for function %s"
1227 " was already used for function %s",
1228 i, params[i]->get_debug_string (),
1229 params[i]->get_type ()->get_debug_string (),
1230 name,
1231 params[i]->get_scope ()->get_debug_string ());
1234 return (gcc_jit_function*)
1235 ctxt->new_function (loc, kind, return_type, name,
1236 num_params,
1237 (gcc::jit::recording::param **)params,
1238 is_variadic,
1239 BUILT_IN_NONE);
1242 /* Public entrypoint. See description in libgccjit.h.
1244 After error-checking, the real work is done by the
1245 gcc::jit::recording::context::get_builtin_function method, in
1246 jit-recording.cc. */
1248 gcc_jit_function *
1249 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
1250 const char *name)
1252 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1253 JIT_LOG_FUNC (ctxt->get_logger ());
1254 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
1256 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
1259 /* Public entrypoint. See description in libgccjit.h.
1261 After error-checking, this calls the trivial
1262 gcc::jit::recording::memento::as_object method (a function is a
1263 memento), in jit-recording.h. */
1265 gcc_jit_object *
1266 gcc_jit_function_as_object (gcc_jit_function *func)
1268 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
1270 return static_cast <gcc_jit_object *> (func->as_object ());
1273 /* Public entrypoint. See description in libgccjit.h.
1275 After error-checking, the real work is done by the
1276 gcc::jit::recording::function::get_param method, in
1277 jit-recording.h. */
1279 gcc_jit_param *
1280 gcc_jit_function_get_param (gcc_jit_function *func, int index)
1282 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
1283 gcc::jit::recording::context *ctxt = func->m_ctxt;
1284 JIT_LOG_FUNC (ctxt->get_logger ());
1285 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
1286 int num_params = func->get_params ().length ();
1287 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
1288 ctxt, NULL,
1289 "index of %d is too large (%s has %d params)",
1290 index,
1291 func->get_debug_string (),
1292 num_params);
1294 return static_cast <gcc_jit_param *> (func->get_param (index));
1297 /* Public entrypoint. See description in libgccjit.h.
1299 After error-checking, the real work is done by the
1300 gcc::jit::recording::function::get_params method, in
1301 jit-recording.h.
1304 size_t
1305 gcc_jit_function_get_param_count (gcc_jit_function *func)
1307 RETURN_VAL_IF_FAIL (func, 0, NULL, NULL, "NULL function");
1308 gcc::jit::recording::context *ctxt = func->m_ctxt;
1309 JIT_LOG_FUNC (ctxt->get_logger ());
1310 return func->get_params ().length ();
1313 /* Public entrypoint. See description in libgccjit.h.
1315 After error-checking, the real work is done by the
1316 gcc::jit::recording::function::get_return_type method, in
1317 jit-recording.h. */
1319 gcc_jit_type *
1320 gcc_jit_function_get_return_type (gcc_jit_function *func)
1322 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function_type");
1323 return (gcc_jit_type *)func->get_return_type ();
1326 /* Public entrypoint. See description in libgccjit.h.
1328 After error-checking, the real work is done by the
1329 gcc::jit::recording::function::dump_to_dot method, in
1330 jit-recording.cc. */
1332 void
1333 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
1334 const char *path)
1336 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
1337 gcc::jit::recording::context *ctxt = func->m_ctxt;
1338 JIT_LOG_FUNC (ctxt->get_logger ());
1339 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
1341 func->dump_to_dot (path);
1344 /* Public entrypoint. See description in libgccjit.h.
1346 After error-checking, the real work is done by the
1347 gcc::jit::recording::function::new_block method, in
1348 jit-recording.cc. */
1350 gcc_jit_block*
1351 gcc_jit_function_new_block (gcc_jit_function *func,
1352 const char *name)
1354 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
1355 JIT_LOG_FUNC (func->get_context ()->get_logger ());
1356 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1357 func->get_context (), NULL,
1358 "cannot add block to an imported function");
1359 /* name can be NULL. */
1361 return (gcc_jit_block *)func->new_block (name);
1364 /* Public entrypoint. See description in libgccjit.h.
1366 After error-checking, this calls the trivial
1367 gcc::jit::recording::memento::as_object method (a block is a
1368 memento), in jit-recording.h. */
1370 gcc_jit_object *
1371 gcc_jit_block_as_object (gcc_jit_block *block)
1373 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1375 return static_cast <gcc_jit_object *> (block->as_object ());
1378 /* Public entrypoint. See description in libgccjit.h.
1380 After error-checking, the real work is done by the
1381 gcc::jit::recording::block::get_function method, in
1382 jit-recording.h. */
1384 gcc_jit_function *
1385 gcc_jit_block_get_function (gcc_jit_block *block)
1387 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1389 return static_cast <gcc_jit_function *> (block->get_function ());
1392 /* Public entrypoint. See description in libgccjit.h.
1394 After error-checking, the real work is done by the
1395 gcc::jit::recording::context::new_global method, in
1396 jit-recording.cc. */
1398 gcc_jit_lvalue *
1399 gcc_jit_context_new_global (gcc_jit_context *ctxt,
1400 gcc_jit_location *loc,
1401 enum gcc_jit_global_kind kind,
1402 gcc_jit_type *type,
1403 const char *name)
1405 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1406 JIT_LOG_FUNC (ctxt->get_logger ());
1407 /* LOC can be NULL. */
1408 RETURN_NULL_IF_FAIL_PRINTF1 (
1409 ((kind >= GCC_JIT_GLOBAL_EXPORTED)
1410 && (kind <= GCC_JIT_GLOBAL_IMPORTED)),
1411 ctxt, loc,
1412 "unrecognized value for enum gcc_jit_global_kind: %i",
1413 kind);
1414 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1415 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1416 RETURN_NULL_IF_FAIL_PRINTF2 (
1417 type->has_known_size (),
1418 ctxt, loc,
1419 "unknown size for global \"%s\" (type: %s)",
1420 name,
1421 type->get_debug_string ());
1422 RETURN_NULL_IF_FAIL_PRINTF1 (
1423 !type->is_void (),
1424 ctxt, loc,
1425 "void type for global \"%s\"",
1426 name);
1428 return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
1431 extern gcc_jit_rvalue *
1432 gcc_jit_context_new_struct_constructor (gcc_jit_context *ctxt,
1433 gcc_jit_location *loc,
1434 gcc_jit_type *type,
1435 size_t num_values,
1436 gcc_jit_field **fields,
1437 gcc_jit_rvalue **values)
1439 using namespace gcc::jit::recording;
1441 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1442 JIT_LOG_FUNC (ctxt->get_logger ());
1443 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1445 RETURN_NULL_IF_FAIL_PRINTF1 (type->is_struct (),
1446 ctxt, loc,
1447 "constructor type is not a struct: %s",
1448 type->get_debug_string ());
1450 compound_type *ct = reinterpret_cast<compound_type *>(type);
1451 gcc::jit::recording::fields *fields_struct = ct->get_fields ();
1452 size_t n_fields = fields_struct->length ();
1454 RETURN_NULL_IF_FAIL_PRINTF1 (ct->has_known_size (),
1455 ctxt, loc,
1456 "struct can't be opaque: %s",
1457 type->get_debug_string ());
1458 RETURN_NULL_IF_FAIL_PRINTF1 (n_fields,
1459 ctxt, loc,
1460 "no fields in struct: %s",
1461 type->get_debug_string ());
1463 /* If there is no array input we just short circuit to zero the struct. */
1464 if (!num_values)
1465 return (gcc_jit_rvalue *)ctxt->new_ctor (loc, type, 0, NULL, NULL);
1467 RETURN_NULL_IF_FAIL_PRINTF3 (n_fields >= num_values,
1468 ctxt, loc,
1469 "more values in constructor (n=%zu) than fields"
1470 " in target %s (n=%zu)",
1471 num_values,
1472 type->get_debug_string (),
1473 n_fields);
1475 /* It is OK if fields are null here, indicating definiton order,
1476 but there has to be a values array. */
1477 RETURN_NULL_IF_FAIL (values,
1478 ctxt, loc,
1479 "'values' NULL with non-zero 'num_values'");
1481 size_t idx = 0; /* Runner index for fields in the type object. */
1483 for (size_t i = 0; i < num_values; i++)
1485 gcc::jit::recording::rvalue *rv = values[i];
1487 /* rv kan be NULL, which would indicate zero init for the field. */
1488 gcc::jit::recording::type *rv_type = rv ? rv->get_type () : nullptr;
1490 /* If fields are specified we need to check that they are in
1491 definition order. */
1492 if (fields)
1494 gcc::jit::recording::field *f = fields[i];
1496 RETURN_NULL_IF_FAIL_PRINTF1 (
1498 ctxt, loc,
1499 "NULL field in 'fields', at index %zu", i);
1501 RETURN_NULL_IF_FAIL_PRINTF3 (
1502 f->get_container () ==
1503 static_cast<gcc::jit::recording::type*>(type),
1504 ctxt, loc,
1505 "field object at index %zu (%s), was not used when creating "
1506 "the %s",
1508 f->get_debug_string (),
1509 type->get_debug_string ());
1511 /* Fields in the constructor need to be in struct definition
1512 order, but there can be gaps. */
1513 size_t j;
1514 for (j = idx; j < n_fields; j++)
1516 field *fs = fields_struct->get_field (j);
1517 if (fs == f)
1519 idx = j; /* Advance runner index for next iteration. */
1520 break;
1524 RETURN_NULL_IF_FAIL_PRINTF3 (
1525 j != n_fields,
1526 ctxt, loc,
1527 "field at index %zu in 'fields' is not in definition order "
1528 "(struct: %s) (ctor field: %s)",
1530 type->get_debug_string (),
1531 f->get_debug_string ());
1533 /* Check that the specified field has the same type as the
1534 value, unless the value is null (a zero value init). */
1535 RETURN_NULL_IF_FAIL_PRINTF5 (
1536 !rv || gcc::jit::types_kinda_same (rv_type,
1537 f->get_type ()),
1538 ctxt, loc,
1539 "value and field not the same unqualified type, at index %zu"
1540 " (%s.%s: %s)(value type: %s)",
1542 type->get_debug_string (),
1543 f->get_debug_string (),
1544 f->get_type ()->get_debug_string (),
1545 rv_type->get_debug_string ());
1548 /* If no fields are specified, check that the value has the same type
1549 as the field in the definition of the struct. */
1550 if (rv && !fields)
1552 RETURN_NULL_IF_FAIL_PRINTF5 (
1553 gcc::jit::types_kinda_same (rv_type,
1554 fields_struct->
1555 get_field (i)->get_type ()),
1556 ctxt, loc,
1557 "value and field not the same unqualified type, at index %zu"
1558 " (%s.%s: %s)(value type: %s)",
1560 type->get_debug_string (),
1561 fields_struct->get_field (i)->get_debug_string (),
1562 fields_struct->get_field (i)->get_type ()->get_debug_string (),
1563 rv_type->get_debug_string ());
1566 if (rv)
1568 RETURN_NULL_IF_FAIL_PRINTF1 (
1569 !rv_type->is_void (),
1570 ctxt, loc,
1571 "can't construct the void type, at index %zu", i);
1575 return (gcc_jit_rvalue *)ctxt->new_ctor (
1576 loc,
1577 type,
1578 num_values,
1579 reinterpret_cast<gcc::jit::recording::field**>(fields),
1580 reinterpret_cast<gcc::jit::recording::rvalue**>(values));
1583 extern gcc_jit_rvalue *
1584 gcc_jit_context_new_union_constructor (gcc_jit_context *ctxt,
1585 gcc_jit_location *loc,
1586 gcc_jit_type *type,
1587 gcc_jit_field *field,
1588 gcc_jit_rvalue *value)
1590 using namespace gcc::jit::recording;
1592 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1593 JIT_LOG_FUNC (ctxt->get_logger ());
1594 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1596 RETURN_NULL_IF_FAIL_PRINTF1 (type->is_union (),
1597 ctxt, loc,
1598 "constructor type is not an union: %s",
1599 type->get_debug_string ());
1601 compound_type *ct = reinterpret_cast<compound_type *>(type);
1602 gcc::jit::recording::fields *fields_union = ct->get_fields ();
1603 size_t n_fields = fields_union->length ();
1605 RETURN_NULL_IF_FAIL_PRINTF1 (ct->has_known_size (),
1606 ctxt, loc,
1607 "union can't be opaque: %s",
1608 type->get_debug_string ());
1609 RETURN_NULL_IF_FAIL_PRINTF1 (n_fields,
1610 ctxt, loc,
1611 "no fields in union: %s",
1612 type->get_debug_string ());
1614 /* If value is NULL we are just supposed to zero the whole union. */
1615 if (!value)
1616 return (gcc_jit_rvalue *)ctxt->new_ctor (loc, type, 0, NULL, NULL);
1618 gcc::jit::recording::type *rv_type = value->get_type ();
1620 RETURN_NULL_IF_FAIL (
1621 !rv_type->is_void (),
1622 ctxt, loc,
1623 "can't construct the void type");
1625 if (field)
1627 RETURN_NULL_IF_FAIL_PRINTF2 (
1628 field->get_container () ==
1629 static_cast<gcc::jit::recording::type*>(type),
1630 ctxt, loc,
1631 "field object (%s) was not used when creating "
1632 "the type %s",
1633 field->get_debug_string (),
1634 type->get_debug_string ());
1636 RETURN_NULL_IF_FAIL_PRINTF4 (
1637 gcc::jit::types_kinda_same (rv_type,
1638 field->get_type ()),
1639 ctxt, loc,
1640 "value and field are not the same unqualified type"
1641 " (%s.%s: %s)(value type: %s)",
1642 type->get_debug_string (),
1643 field->get_debug_string (),
1644 field->get_type ()->get_debug_string (),
1645 rv_type->get_debug_string ());
1647 /* If no field is specified, check that the value has the same type
1648 as the first field in the definition of the union. */
1649 if (!field)
1650 RETURN_NULL_IF_FAIL_PRINTF2 (
1651 gcc::jit::types_kinda_same (rv_type,
1652 fields_union->
1653 get_field (0)->get_type ()),
1654 ctxt, loc,
1655 "value and first union field not the same unqualified type"
1656 " (field type: %s)(value type: %s)",
1657 fields_union->get_field (0)->get_type ()->get_debug_string (),
1658 rv_type->get_debug_string ());
1661 return (gcc_jit_rvalue *)ctxt->new_ctor (
1662 loc,
1663 type,
1665 /* A NULL fields array tells new_ctor to take fields from the type obj. */
1666 reinterpret_cast<gcc::jit::recording::field**>(field ? &field : NULL),
1667 reinterpret_cast<gcc::jit::recording::rvalue**>(&value));
1670 extern gcc_jit_rvalue *
1671 gcc_jit_context_new_array_constructor (gcc_jit_context *ctxt,
1672 gcc_jit_location *loc,
1673 gcc_jit_type *type,
1674 size_t num_values,
1675 gcc_jit_rvalue **values)
1677 using namespace gcc::jit::recording;
1679 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1680 JIT_LOG_FUNC (ctxt->get_logger ());
1681 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1683 RETURN_NULL_IF_FAIL (type->is_array () != NULL,
1684 ctxt, loc,
1685 "constructor type not an array");
1687 if (!num_values)
1688 values = NULL;
1690 if (num_values)
1692 RETURN_NULL_IF_FAIL (
1693 values,
1694 ctxt, loc,
1695 "'values' NULL with non-zero 'num_values'");
1697 gcc::jit::recording::array_type *arr_type =
1698 reinterpret_cast<gcc::jit::recording::array_type*>(type);
1699 size_t n_el = arr_type->num_elements ();
1701 RETURN_NULL_IF_FAIL_PRINTF2 (
1702 n_el >= num_values,
1703 ctxt, loc,
1704 "array constructor has more values than the array type's length"
1705 " (array type length: %zu, constructor length: %zu)",
1706 n_el,
1707 num_values);
1709 /* For arrays, all values need to be the same base type. */
1710 gcc::jit::recording::type *type0 = NULL;
1711 size_t i = 0;
1712 /* Find first non-null value. */
1713 for (;i < num_values; i++)
1715 if (values[i])
1716 break;
1719 if (i < num_values) /* All values might be null and i == num_values. */
1720 type0 = values[i]->get_type ();
1722 /* If we got a type0, check that all other values have
1723 the same type. */
1724 for (; i < num_values; i++)
1726 if (values[i])
1727 RETURN_NULL_IF_FAIL_PRINTF3 (
1728 gcc::jit::types_kinda_same (type0,
1729 values[i]->get_type ()),
1730 ctxt, loc,
1731 "value type at index %zu differ from first value type"
1732 " (first type: %s)(different type: %s)",
1734 type0->get_debug_string (),
1735 values[i]->get_type ()->get_debug_string ());
1738 /* Compare type0 with the element type specified in the
1739 type of the array. */
1740 if (type0)
1742 gcc::jit::recording::type *el_type =
1743 type->is_array ();
1745 RETURN_NULL_IF_FAIL_PRINTF2 (
1746 gcc::jit::types_kinda_same (type0, el_type),
1747 ctxt, loc,
1748 "array element value types differ from types in 'values'"
1749 " (element type: %s)('values' type: %s)",
1750 el_type->get_debug_string (),
1751 type0->get_debug_string ());
1755 return (gcc_jit_rvalue *)ctxt->new_ctor (
1756 loc,
1757 type,
1758 num_values,
1759 NULL,
1760 reinterpret_cast<gcc::jit::recording::rvalue**>(values));
1763 /* Public entrypoint. See description in libgccjit.h. */
1765 extern gcc_jit_lvalue *
1766 gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue *global,
1767 gcc_jit_rvalue *init_rvalue)
1769 RETURN_NULL_IF_FAIL (global, NULL, NULL,"NULL global");
1771 gcc::jit::recording::context *ctxt = global->get_context ();
1772 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL,"NULL context");
1773 JIT_LOG_FUNC (ctxt->get_logger ());
1774 RETURN_NULL_IF_FAIL (init_rvalue, ctxt, NULL,"NULL init_rvalue");
1776 RETURN_NULL_IF_FAIL_PRINTF1 (global->is_global (),
1777 ctxt, NULL,
1778 "lvalue \"%s\" not a global",
1779 global->get_debug_string ());
1781 gcc::jit::recording::global *gbl =
1782 reinterpret_cast<gcc::jit::recording::global *> (global);
1784 RETURN_NULL_IF_FAIL_PRINTF1 (gbl->get_kind () !=
1785 GCC_JIT_GLOBAL_IMPORTED,
1786 ctxt, NULL,
1787 "can't initialize \"%s\", it is imported",
1788 global->get_debug_string ());
1790 RETURN_NULL_IF_FAIL_PRINTF4 (gcc::jit::types_kinda_same (
1791 global->get_type (),
1792 init_rvalue->get_type ()),
1793 ctxt, NULL,
1794 "mismatching types:"
1795 " initializing %s (type: %s) with %s (type: %s)",
1796 global->get_debug_string (),
1797 global->get_type ()->get_debug_string (),
1798 init_rvalue->get_debug_string (),
1799 init_rvalue->get_type ()->get_debug_string ());
1801 /* Check that there are no initializers set for the global yet. */
1802 RETURN_NULL_IF_FAIL_PRINTF1 (!gbl->test_flags_anyof (
1803 gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT |
1804 gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_BLOB_INIT),
1805 ctxt, NULL,
1806 "global variable already initialized: %s",
1807 global->get_debug_string ());
1809 /* The global need to know during playback that it will be
1810 initialized. */
1811 gbl->set_flags (gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT);
1813 ctxt->new_global_init_rvalue (global, init_rvalue);
1815 return global;
1818 /* Public entrypoint. See description in libgccjit.h.
1820 After error-checking, the real work is done by the
1821 gcc::jit::recording::global::set_initializer method, in
1822 jit-recording.cc. */
1824 extern gcc_jit_lvalue *
1825 gcc_jit_global_set_initializer (gcc_jit_lvalue *global,
1826 const void *blob,
1827 size_t num_bytes)
1829 RETURN_NULL_IF_FAIL (global, NULL, NULL, "NULL global");
1830 RETURN_NULL_IF_FAIL (blob, NULL, NULL, "NULL blob");
1831 RETURN_NULL_IF_FAIL_PRINTF1 (global->is_global (), NULL, NULL,
1832 "lvalue \"%s\" not a global",
1833 global->get_debug_string ());
1835 gcc::jit::recording::type *lval_type = global->get_type ();
1836 RETURN_NULL_IF_FAIL_PRINTF1 (lval_type->is_array (), NULL, NULL,
1837 "global \"%s\" is not an array",
1838 global->get_debug_string ());
1839 RETURN_NULL_IF_FAIL_PRINTF1 (lval_type->dereference ()->is_int (), NULL, NULL,
1840 "global \"%s\" is not an array of integral type",
1841 global->get_debug_string ());
1842 size_t lvalue_size =
1843 lval_type->dereference ()->get_size ()
1844 * static_cast <gcc::jit::recording::array_type *> (lval_type)->num_elements ();
1845 RETURN_NULL_IF_FAIL_PRINTF3 (
1846 lvalue_size == num_bytes, NULL, NULL,
1847 "mismatching sizes:"
1848 " global \"%s\" has size %zu whereas initializer has size %zu",
1849 global->get_debug_string (), lvalue_size, num_bytes);
1851 /* Check that the rvalue initializer is not set for this global.
1852 Note that we do not check if this blob type initializer is
1853 already set, since that check was not present when the entrypoint
1854 was initially written. */
1855 gcc::jit::recording::global *gbl =
1856 reinterpret_cast<gcc::jit::recording::global *> (global);
1857 RETURN_NULL_IF_FAIL_PRINTF1 (!gbl->test_flags_anyof (
1858 gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT),
1859 NULL, NULL,
1860 "global variable already initialized: %s",
1861 global->get_debug_string ());
1863 gbl->set_initializer (blob, num_bytes);
1864 /* The global need to know during playback that it will be
1865 initialized. */
1866 gbl->set_flags (gcc::jit::GLOBAL_VAR_FLAGS_WILL_BE_BLOB_INIT);
1868 return global;
1871 /* Public entrypoint. See description in libgccjit.h.
1873 After error-checking, this calls the trivial
1874 gcc::jit::recording::memento::as_object method (an lvalue is a
1875 memento), in jit-recording.h. */
1877 gcc_jit_object *
1878 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1880 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1882 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1885 /* Public entrypoint. See description in libgccjit.h.
1887 After error-checking, this calls the trivial
1888 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1890 gcc_jit_rvalue *
1891 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1893 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1895 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1898 /* Public entrypoint. See description in libgccjit.h.
1900 After error-checking, this calls the trivial
1901 gcc::jit::recording::memento::as_object method (an rvalue is a
1902 memento), in jit-recording.h. */
1904 gcc_jit_object *
1905 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1907 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1909 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1912 /* Public entrypoint. See description in libgccjit.h.
1914 After error-checking, the real work is done by the
1915 gcc::jit::recording::rvalue::get_type method, in
1916 jit-recording.h. */
1918 gcc_jit_type *
1919 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1921 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1923 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1926 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1927 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1928 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1930 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1931 JIT_BEGIN_STMT \
1932 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1933 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1934 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1935 "not a numeric type: %s", \
1936 NUMERIC_TYPE->get_debug_string ()); \
1937 JIT_END_STMT
1939 /* Public entrypoint. See description in libgccjit.h.
1941 After error-checking, the real work is done by the
1942 gcc::jit::recording::context::new_rvalue_from_const <int> method in
1943 jit-recording.cc. */
1945 gcc_jit_rvalue *
1946 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1947 gcc_jit_type *numeric_type,
1948 int value)
1950 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1951 JIT_LOG_FUNC (ctxt->get_logger ());
1952 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1954 return ((gcc_jit_rvalue *)ctxt
1955 ->new_rvalue_from_const <int> (numeric_type, value));
1958 /* Public entrypoint. See description in libgccjit.h.
1960 After error-checking, the real work is done by the
1961 gcc::jit::recording::context::new_rvalue_from_const <long> method
1962 in jit-recording.cc. */
1964 gcc_jit_rvalue *
1965 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1966 gcc_jit_type *numeric_type,
1967 long value)
1969 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1970 JIT_LOG_FUNC (ctxt->get_logger ());
1971 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1973 return ((gcc_jit_rvalue *)ctxt
1974 ->new_rvalue_from_const <long> (numeric_type, value));
1977 /* Public entrypoint. See description in libgccjit.h.
1979 This is essentially equivalent to:
1980 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1981 albeit with slightly different error messages if an error occurs. */
1983 gcc_jit_rvalue *
1984 gcc_jit_context_zero (gcc_jit_context *ctxt,
1985 gcc_jit_type *numeric_type)
1987 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1988 JIT_LOG_FUNC (ctxt->get_logger ());
1989 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1991 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1994 /* Public entrypoint. See description in libgccjit.h.
1996 This is essentially equivalent to:
1997 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1998 albeit with slightly different error messages if an error occurs. */
2000 gcc_jit_rvalue *
2001 gcc_jit_context_one (gcc_jit_context *ctxt,
2002 gcc_jit_type *numeric_type)
2004 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2005 JIT_LOG_FUNC (ctxt->get_logger ());
2006 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
2008 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
2011 /* Public entrypoint. See description in libgccjit.h.
2013 After error-checking, the real work is done by the
2014 gcc::jit::recording::context::new_rvalue_from_const <double> method in
2015 jit-recording.cc. */
2017 gcc_jit_rvalue *
2018 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
2019 gcc_jit_type *numeric_type,
2020 double value)
2022 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2023 JIT_LOG_FUNC (ctxt->get_logger ());
2024 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
2026 return ((gcc_jit_rvalue *)ctxt
2027 ->new_rvalue_from_const <double> (numeric_type, value));
2030 /* Public entrypoint. See description in libgccjit.h.
2032 After error-checking, the real work is done by the
2033 gcc::jit::recording::context::new_rvalue_from_const <void *> method
2034 in jit-recording.cc. */
2036 gcc_jit_rvalue *
2037 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
2038 gcc_jit_type *pointer_type,
2039 void *value)
2041 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2042 JIT_LOG_FUNC (ctxt->get_logger ());
2043 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
2044 RETURN_NULL_IF_FAIL_PRINTF1 (
2045 pointer_type->is_pointer (),
2046 ctxt, NULL,
2047 "not a pointer type (type: %s)",
2048 pointer_type->get_debug_string ());
2050 return ((gcc_jit_rvalue *)ctxt
2051 ->new_rvalue_from_const <void *> (pointer_type, value));
2054 /* Public entrypoint. See description in libgccjit.h.
2056 This is essentially equivalent to:
2057 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
2058 albeit with slightly different error messages if an error occurs. */
2060 gcc_jit_rvalue *
2061 gcc_jit_context_null (gcc_jit_context *ctxt,
2062 gcc_jit_type *pointer_type)
2064 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2065 JIT_LOG_FUNC (ctxt->get_logger ());
2066 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
2067 RETURN_NULL_IF_FAIL_PRINTF1 (
2068 pointer_type->is_pointer (),
2069 ctxt, NULL,
2070 "not a pointer type (type: %s)",
2071 pointer_type->get_debug_string ());
2073 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
2076 /* Public entrypoint. See description in libgccjit.h.
2078 After error-checking, the real work is done by the
2079 gcc::jit::recording::context::new_sizeof method in
2080 jit-recording.cc. */
2082 gcc_jit_rvalue *
2083 gcc_jit_context_new_sizeof (gcc_jit_context *ctxt,
2084 gcc_jit_type *type)
2086 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2087 RETURN_NULL_IF_FAIL (type, ctxt, NULL, "NULL type");
2088 JIT_LOG_FUNC (ctxt->get_logger ());
2090 return ((gcc_jit_rvalue *)ctxt
2091 ->new_sizeof (type));
2094 /* Public entrypoint. See description in libgccjit.h.
2096 After error-checking, the real work is done by the
2097 gcc::jit::recording::context::new_alignof method in
2098 jit-recording.cc. */
2100 gcc_jit_rvalue *
2101 gcc_jit_context_new_alignof (gcc_jit_context *ctxt,
2102 gcc_jit_type *type)
2104 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2105 RETURN_NULL_IF_FAIL (type, ctxt, NULL, "NULL type");
2106 JIT_LOG_FUNC (ctxt->get_logger ());
2108 return ((gcc_jit_rvalue *)ctxt
2109 ->new_alignof (type));
2112 /* Public entrypoint. See description in libgccjit.h.
2114 After error-checking, the real work is done by the
2115 gcc::jit::recording::context::new_string_literal method in
2116 jit-recording.cc. */
2118 gcc_jit_rvalue *
2119 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
2120 const char *value)
2122 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2123 JIT_LOG_FUNC (ctxt->get_logger ());
2124 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
2126 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
2129 /* Public entrypoint. See description in libgccjit.h.
2131 After error-checking, the real work is done by the
2132 gcc::jit::recording::context::new_unary_op method in
2133 jit-recording.cc. */
2135 gcc_jit_rvalue *
2136 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
2137 gcc_jit_location *loc,
2138 enum gcc_jit_unary_op op,
2139 gcc_jit_type *result_type,
2140 gcc_jit_rvalue *rvalue)
2142 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2143 JIT_LOG_FUNC (ctxt->get_logger ());
2144 /* LOC can be NULL. */
2145 RETURN_NULL_IF_FAIL_PRINTF1 (
2146 (op >= GCC_JIT_UNARY_OP_MINUS
2147 && op <= GCC_JIT_UNARY_OP_ABS),
2148 ctxt, loc,
2149 "unrecognized value for enum gcc_jit_unary_op: %i",
2150 op);
2151 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
2152 RETURN_NULL_IF_FAIL_PRINTF3 (
2153 result_type->is_numeric () || result_type->is_numeric_vector (), ctxt, loc,
2154 "gcc_jit_unary_op %s with operand %s "
2155 "has non-numeric result_type: %s",
2156 gcc::jit::unary_op_reproducer_strings[op],
2157 rvalue->get_debug_string (),
2158 result_type->get_debug_string ());
2159 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2161 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
2164 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
2165 For use by both gcc_jit_context_new_binary_op and
2166 gcc_jit_block_add_assignment_op. */
2168 static bool
2169 valid_binary_op_p (enum gcc_jit_binary_op op)
2171 return (op >= GCC_JIT_BINARY_OP_PLUS
2172 && op <= GCC_JIT_BINARY_OP_RSHIFT);
2175 /* Public entrypoint. See description in libgccjit.h.
2177 After error-checking, the real work is done by the
2178 gcc::jit::recording::context::new_binary_op method in
2179 jit-recording.cc. */
2181 gcc_jit_rvalue *
2182 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
2183 gcc_jit_location *loc,
2184 enum gcc_jit_binary_op op,
2185 gcc_jit_type *result_type,
2186 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
2188 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2189 JIT_LOG_FUNC (ctxt->get_logger ());
2190 /* LOC can be NULL. */
2191 RETURN_NULL_IF_FAIL_PRINTF1 (
2192 valid_binary_op_p (op),
2193 ctxt, loc,
2194 "unrecognized value for enum gcc_jit_binary_op: %i",
2195 op);
2196 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
2197 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
2198 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
2199 RETURN_NULL_IF_FAIL_PRINTF4 (
2200 compatible_types (a->get_type ()->unqualified (),
2201 b->get_type ()->unqualified ()),
2202 ctxt, loc,
2203 "mismatching types for binary op:"
2204 " a: %s (type: %s) b: %s (type: %s)",
2205 a->get_debug_string (),
2206 a->get_type ()->get_debug_string (),
2207 b->get_debug_string (),
2208 b->get_type ()->get_debug_string ());
2209 RETURN_NULL_IF_FAIL_PRINTF4 (
2210 result_type->is_numeric () || result_type->is_numeric_vector (), ctxt, loc,
2211 "gcc_jit_binary_op %s with operands a: %s b: %s "
2212 "has non-numeric result_type: %s",
2213 gcc::jit::binary_op_reproducer_strings[op],
2214 a->get_debug_string (), b->get_debug_string (),
2215 result_type->get_debug_string ());
2217 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
2220 /* Public entrypoint. See description in libgccjit.h.
2222 After error-checking, the real work is done by the
2223 gcc::jit::recording::context::new_comparison method in
2224 jit-recording.cc. */
2226 gcc_jit_rvalue *
2227 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
2228 gcc_jit_location *loc,
2229 enum gcc_jit_comparison op,
2230 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
2232 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2233 JIT_LOG_FUNC (ctxt->get_logger ());
2234 /* LOC can be NULL. */
2235 RETURN_NULL_IF_FAIL_PRINTF1 (
2236 (op >= GCC_JIT_COMPARISON_EQ
2237 && op <= GCC_JIT_COMPARISON_GE),
2238 ctxt, loc,
2239 "unrecognized value for enum gcc_jit_comparison: %i",
2240 op);
2241 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
2242 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
2243 RETURN_NULL_IF_FAIL_PRINTF4 (
2244 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
2245 ctxt, loc,
2246 "mismatching types for comparison:"
2247 " a: %s (type: %s) b: %s (type: %s)",
2248 a->get_debug_string (),
2249 a->get_type ()->get_debug_string (),
2250 b->get_debug_string (),
2251 b->get_type ()->get_debug_string ());
2253 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
2256 /* Public entrypoint. See description in libgccjit.h.
2258 After error-checking, the real work is done by the
2259 gcc::jit::recording::context::new_call method in
2260 jit-recording.cc. */
2262 gcc_jit_rvalue *
2263 gcc_jit_context_new_call (gcc_jit_context *ctxt,
2264 gcc_jit_location *loc,
2265 gcc_jit_function *func,
2266 int numargs , gcc_jit_rvalue **args)
2268 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2269 JIT_LOG_FUNC (ctxt->get_logger ());
2270 /* LOC can be NULL. */
2271 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
2272 if (numargs)
2273 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
2275 int min_num_params = func->get_params ().length ();
2276 bool is_variadic = func->is_variadic ();
2278 RETURN_NULL_IF_FAIL_PRINTF3 (
2279 numargs >= min_num_params,
2280 ctxt, loc,
2281 "not enough arguments to function \"%s\""
2282 " (got %i args, expected %i)",
2283 func->get_name ()->c_str (),
2284 numargs, min_num_params);
2286 RETURN_NULL_IF_FAIL_PRINTF3 (
2287 (numargs == min_num_params || is_variadic),
2288 ctxt, loc,
2289 "too many arguments to function \"%s\""
2290 " (got %i args, expected %i)",
2291 func->get_name ()->c_str (),
2292 numargs, min_num_params);
2294 for (int i = 0; i < min_num_params; i++)
2296 gcc::jit::recording::param *param = func->get_param (i);
2297 gcc_jit_rvalue *arg = args[i];
2299 RETURN_NULL_IF_FAIL_PRINTF4 (
2300 arg,
2301 ctxt, loc,
2302 "NULL argument %i to function \"%s\":"
2303 " param %s (type: %s)",
2304 i + 1,
2305 func->get_name ()->c_str (),
2306 param->get_debug_string (),
2307 param->get_type ()->get_debug_string ());
2309 RETURN_NULL_IF_FAIL_PRINTF6 (
2310 compatible_types (param->get_type (),
2311 arg->get_type ()),
2312 ctxt, loc,
2313 "mismatching types for argument %d of function \"%s\":"
2314 " assignment to param %s (type: %s) from %s (type: %s)",
2315 i + 1,
2316 func->get_name ()->c_str (),
2317 param->get_debug_string (),
2318 param->get_type ()->get_debug_string (),
2319 arg->get_debug_string (),
2320 arg->get_type ()->get_debug_string ());
2323 return (gcc_jit_rvalue *)ctxt->new_call (loc,
2324 func,
2325 numargs,
2326 (gcc::jit::recording::rvalue **)args);
2329 /* Public entrypoint. See description in libgccjit.h.
2331 After error-checking, the real work is done by the
2332 gcc::jit::recording::context::new_call_through_ptr method in
2333 jit-recording.cc. */
2335 gcc_jit_rvalue *
2336 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
2337 gcc_jit_location *loc,
2338 gcc_jit_rvalue *fn_ptr,
2339 int numargs, gcc_jit_rvalue **args)
2341 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2342 JIT_LOG_FUNC (ctxt->get_logger ());
2343 /* LOC can be NULL. */
2344 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
2345 if (numargs)
2346 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
2348 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
2349 RETURN_NULL_IF_FAIL_PRINTF2 (
2350 ptr_type, ctxt, loc,
2351 "fn_ptr is not a ptr: %s"
2352 " type: %s",
2353 fn_ptr->get_debug_string (),
2354 fn_ptr->get_type ()->get_debug_string ());
2356 gcc::jit::recording::function_type *fn_type =
2357 ptr_type->dyn_cast_function_type();
2358 RETURN_NULL_IF_FAIL_PRINTF2 (
2359 fn_type, ctxt, loc,
2360 "fn_ptr is not a function ptr: %s"
2361 " type: %s",
2362 fn_ptr->get_debug_string (),
2363 fn_ptr->get_type ()->get_debug_string ());
2365 int min_num_params = fn_type->get_param_types ().length ();
2366 bool is_variadic = fn_type->is_variadic ();
2368 RETURN_NULL_IF_FAIL_PRINTF3 (
2369 numargs >= min_num_params,
2370 ctxt, loc,
2371 "not enough arguments to fn_ptr: %s"
2372 " (got %i args, expected %i)",
2373 fn_ptr->get_debug_string (),
2374 numargs, min_num_params);
2376 RETURN_NULL_IF_FAIL_PRINTF3 (
2377 (numargs == min_num_params || is_variadic),
2378 ctxt, loc,
2379 "too many arguments to fn_ptr: %s"
2380 " (got %i args, expected %i)",
2381 fn_ptr->get_debug_string (),
2382 numargs, min_num_params);
2384 for (int i = 0; i < min_num_params; i++)
2386 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
2387 gcc_jit_rvalue *arg = args[i];
2389 RETURN_NULL_IF_FAIL_PRINTF3 (
2390 arg,
2391 ctxt, loc,
2392 "NULL argument %i to fn_ptr: %s"
2393 " (type: %s)",
2394 i + 1,
2395 fn_ptr->get_debug_string (),
2396 param_type->get_debug_string ());
2398 RETURN_NULL_IF_FAIL_PRINTF6 (
2399 compatible_types (param_type,
2400 arg->get_type ()),
2401 ctxt, loc,
2402 "mismatching types for argument %d of fn_ptr: %s:"
2403 " assignment to param %d (type: %s) from %s (type: %s)",
2404 i + 1,
2405 fn_ptr->get_debug_string (),
2406 i + 1,
2407 param_type->get_debug_string (),
2408 arg->get_debug_string (),
2409 arg->get_type ()->get_debug_string ());
2412 return (gcc_jit_rvalue *)(
2413 ctxt->new_call_through_ptr (loc,
2414 fn_ptr,
2415 numargs,
2416 (gcc::jit::recording::rvalue **)args));
2419 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
2420 to DST_TYPE, for use by gcc_jit_context_new_cast.
2422 We only permit these kinds of cast:
2424 int <-> float
2425 int <-> bool
2426 P* <-> Q* for pointer types P and Q. */
2428 static bool
2429 is_valid_cast (gcc::jit::recording::type *src_type,
2430 gcc_jit_type *dst_type)
2432 bool src_is_int = src_type->is_int ();
2433 bool dst_is_int = dst_type->is_int ();
2434 bool src_is_float = src_type->is_float ();
2435 bool dst_is_float = dst_type->is_float ();
2436 bool src_is_bool = src_type->is_bool ();
2437 bool dst_is_bool = dst_type->is_bool ();
2439 if (src_is_int)
2440 if (dst_is_int || dst_is_float || dst_is_bool)
2441 return true;
2443 if (src_is_float)
2444 if (dst_is_int || dst_is_float)
2445 return true;
2447 if (src_is_bool)
2448 if (dst_is_int || dst_is_bool)
2449 return true;
2451 /* Permit casts between pointer types. */
2452 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
2453 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
2454 if (deref_src_type && deref_dst_type)
2455 return true;
2457 return false;
2460 /* Public entrypoint. See description in libgccjit.h.
2462 After error-checking, the real work is done by the
2463 gcc::jit::recording::context::new_cast method in jit-recording.cc. */
2465 gcc_jit_rvalue *
2466 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
2467 gcc_jit_location *loc,
2468 gcc_jit_rvalue *rvalue,
2469 gcc_jit_type *type)
2471 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2472 JIT_LOG_FUNC (ctxt->get_logger ());
2473 /* LOC can be NULL. */
2474 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2475 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
2476 RETURN_NULL_IF_FAIL_PRINTF3 (
2477 is_valid_cast (rvalue->get_type (), type),
2478 ctxt, loc,
2479 "cannot cast %s from type: %s to type: %s",
2480 rvalue->get_debug_string (),
2481 rvalue->get_type ()->get_debug_string (),
2482 type->get_debug_string ());
2484 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
2487 /* Public entrypoint. See description in libgccjit.h.
2489 After error-checking, the real work is done by the
2490 gcc::jit::recording::context::new_bitcast method in jit-recording.c. */
2492 gcc_jit_rvalue *
2493 gcc_jit_context_new_bitcast (gcc_jit_context *ctxt,
2494 gcc_jit_location *loc,
2495 gcc_jit_rvalue *rvalue,
2496 gcc_jit_type *type)
2498 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2499 JIT_LOG_FUNC (ctxt->get_logger ());
2500 /* LOC can be NULL. */
2501 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2502 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
2503 /* We cannot check if the size of rvalue matches the size of type here, so
2504 we'll do it at playback. */
2506 return static_cast <gcc_jit_rvalue *> (ctxt->new_bitcast (loc, rvalue, type));
2509 /* Public entrypoint. See description in libgccjit.h.
2511 After error-checking, the real work is done by the
2512 gcc::jit::recording::context::new_array_access method in
2513 jit-recording.cc. */
2515 extern gcc_jit_lvalue *
2516 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
2517 gcc_jit_location *loc,
2518 gcc_jit_rvalue *ptr,
2519 gcc_jit_rvalue *index)
2521 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2522 JIT_LOG_FUNC (ctxt->get_logger ());
2523 /* LOC can be NULL. */
2524 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
2525 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
2526 RETURN_NULL_IF_FAIL_PRINTF2 (
2527 ptr->get_type ()->dereference (),
2528 ctxt, loc,
2529 "ptr: %s (type: %s) is not a pointer or array",
2530 ptr->get_debug_string (),
2531 ptr->get_type ()->get_debug_string ());
2532 RETURN_NULL_IF_FAIL_PRINTF2 (
2533 index->get_type ()->is_numeric (),
2534 ctxt, loc,
2535 "index: %s (type: %s) is not of numeric type",
2536 index->get_debug_string (),
2537 index->get_type ()->get_debug_string ());
2539 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
2542 /* Public entrypoint. See description in libgccjit.h.
2544 After error-checking, the real work is done by the
2545 gcc::jit::recording::memento::get_context method in
2546 jit-recording.h. */
2548 gcc_jit_context *
2549 gcc_jit_object_get_context (gcc_jit_object *obj)
2551 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
2553 return static_cast <gcc_jit_context *> (obj->get_context ());
2556 /* Public entrypoint. See description in libgccjit.h.
2558 After error-checking, the real work is done by the
2559 gcc::jit::recording::memento::get_debug_string method in
2560 jit-recording.cc. */
2562 const char *
2563 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
2565 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
2567 return obj->get_debug_string ();
2570 /* Public entrypoint. See description in libgccjit.h.
2572 After error-checking, the real work is done by the
2573 gcc::jit::recording::lvalue::access_field method in
2574 jit-recording.cc. */
2576 gcc_jit_lvalue *
2577 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
2578 gcc_jit_location *loc,
2579 gcc_jit_field *field)
2581 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
2582 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
2583 JIT_LOG_FUNC (ctxt->get_logger ());
2584 /* LOC can be NULL. */
2585 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
2586 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
2587 "field %s has not been placed in a struct",
2588 field->get_debug_string ());
2589 gcc::jit::recording::type *underlying_type =
2590 struct_->get_type ();
2591 RETURN_NULL_IF_FAIL_PRINTF2 (
2592 (field->get_container ()->unqualified ()
2593 == underlying_type->unqualified ()),
2594 struct_->m_ctxt, loc,
2595 "%s is not a field of %s",
2596 field->get_debug_string (),
2597 underlying_type->get_debug_string ());
2599 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
2602 /* Public entrypoint. See description in libgccjit.h.
2604 After error-checking, the real work is done by the
2605 gcc::jit::recording::rvalue::access_field method in
2606 jit-recording.cc. */
2608 gcc_jit_rvalue *
2609 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
2610 gcc_jit_location *loc,
2611 gcc_jit_field *field)
2613 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
2614 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
2615 JIT_LOG_FUNC (ctxt->get_logger ());
2616 /* LOC can be NULL. */
2617 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
2618 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
2619 "field %s has not been placed in a struct",
2620 field->get_debug_string ());
2621 gcc::jit::recording::type *underlying_type =
2622 struct_->get_type ();
2623 RETURN_NULL_IF_FAIL_PRINTF2 (
2624 (field->get_container ()->unqualified ()
2625 == underlying_type->unqualified ()),
2626 struct_->m_ctxt, loc,
2627 "%s is not a field of %s",
2628 field->get_debug_string (),
2629 underlying_type->get_debug_string ());
2631 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
2634 /* Public entrypoint. See description in libgccjit.h.
2636 After error-checking, the real work is done by the
2637 gcc::jit::recording::rvalue::deference_field method in
2638 jit-recording.cc. */
2640 gcc_jit_lvalue *
2641 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
2642 gcc_jit_location *loc,
2643 gcc_jit_field *field)
2645 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
2646 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
2647 /* LOC can be NULL. */
2648 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
2649 gcc::jit::recording::type *underlying_type =
2650 ptr->get_type ()->is_pointer ();
2651 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
2652 "field %s has not been placed in a struct",
2653 field->get_debug_string ());
2654 RETURN_NULL_IF_FAIL_PRINTF3 (
2655 underlying_type,
2656 ptr->m_ctxt, loc,
2657 "dereference of non-pointer %s (type: %s) when accessing ->%s",
2658 ptr->get_debug_string (),
2659 ptr->get_type ()->get_debug_string (),
2660 field->get_debug_string ());
2661 RETURN_NULL_IF_FAIL_PRINTF2 (
2662 (field->get_container ()->unqualified ()
2663 == underlying_type->unqualified ()),
2664 ptr->m_ctxt, loc,
2665 "%s is not a field of %s",
2666 field->get_debug_string (),
2667 underlying_type->get_debug_string ());
2669 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
2672 /* Public entrypoint. See description in libgccjit.h.
2674 After error-checking, the real work is done by the
2675 gcc::jit::recording::rvalue::deference method in
2676 jit-recording.cc. */
2678 gcc_jit_lvalue *
2679 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
2680 gcc_jit_location *loc)
2682 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
2683 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
2684 /* LOC can be NULL. */
2686 gcc::jit::recording::type *underlying_type =
2687 rvalue->get_type ()->is_pointer ();
2689 RETURN_NULL_IF_FAIL_PRINTF2 (
2690 underlying_type,
2691 rvalue->m_ctxt, loc,
2692 "dereference of non-pointer %s (type: %s)",
2693 rvalue->get_debug_string (),
2694 rvalue->get_type ()->get_debug_string ());
2696 RETURN_NULL_IF_FAIL_PRINTF2 (
2697 !underlying_type->is_void (),
2698 rvalue->m_ctxt, loc,
2699 "dereference of void pointer %s (type: %s)",
2700 rvalue->get_debug_string (),
2701 rvalue->get_type ()->get_debug_string ());
2703 return (gcc_jit_lvalue *)rvalue->dereference (loc);
2706 /* Public entrypoint. See description in libgccjit.h.
2708 After error-checking, the real work is done by the
2709 gcc::jit::recording::lvalue::get_address method in jit-recording.cc. */
2711 gcc_jit_rvalue *
2712 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
2713 gcc_jit_location *loc)
2715 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
2716 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
2717 /* LOC can be NULL. */
2719 return (gcc_jit_rvalue *)lvalue->get_address (loc);
2722 /* Public entrypoint. See description in libgccjit.h.
2724 After error-checking, the real work is done by the
2725 gcc::jit::recording::lvalue::set_tls_model method in jit-recording.cc. */
2727 void
2728 gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,
2729 enum gcc_jit_tls_model model)
2731 RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
2732 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
2733 RETURN_IF_FAIL_PRINTF1 (lvalue->is_global (), lvalue->get_context (), NULL,
2734 "lvalue \"%s\" not a global",
2735 lvalue->get_debug_string ());
2737 lvalue->set_tls_model (model);
2740 /* Public entrypoint. See description in libgccjit.h.
2742 After error-checking, the real work is done by the
2743 gcc::jit::recording::lvalue::set_link_section method in jit-recording.cc. */
2745 void
2746 gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,
2747 const char *section_name)
2749 RETURN_IF_FAIL (section_name, NULL, NULL, "NULL section_name");
2750 lvalue->set_link_section (section_name);
2753 /* Public entrypoint. See description in libgccjit.h.
2755 After error-checking, the real work is done by the
2756 gcc::jit::recording::lvalue::get_alignment method in jit-recording.cc. */
2758 unsigned
2759 gcc_jit_lvalue_get_alignment (gcc_jit_lvalue *lvalue)
2761 RETURN_VAL_IF_FAIL (lvalue, 0, NULL, NULL, "NULL lvalue");
2762 return lvalue->get_alignment ();
2765 /* Public entrypoint. See description in libgccjit.h.
2767 After error-checking, the real work is done by the
2768 gcc::jit::recording::lvalue::set_alignment method in jit-recording.cc. */
2770 void
2771 gcc_jit_lvalue_set_alignment (gcc_jit_lvalue *lvalue,
2772 unsigned bytes)
2774 RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
2775 RETURN_IF_FAIL ((bytes & (bytes - 1)) == 0, NULL, NULL,
2776 "alignment is not a power of 2");
2777 lvalue->set_alignment (bytes);
2780 /* Public entrypoint. See description in libgccjit.h.
2782 After error-checking, the real work is done by the
2783 gcc::jit::recording::lvalue::set_register_name method in jit-recording.cc. */
2785 void
2786 gcc_jit_lvalue_set_register_name (gcc_jit_lvalue *lvalue,
2787 const char *reg_name)
2789 RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
2790 RETURN_IF_FAIL (reg_name, NULL, NULL, "NULL reg_name");
2791 lvalue->set_register_name (reg_name);
2794 /* Public entrypoint. See description in libgccjit.h.
2796 After error-checking, the real work is done by the
2797 gcc::jit::recording::function::new_local method in jit-recording.cc. */
2799 gcc_jit_lvalue *
2800 gcc_jit_function_new_local (gcc_jit_function *func,
2801 gcc_jit_location *loc,
2802 gcc_jit_type *type,
2803 const char *name)
2805 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
2806 gcc::jit::recording::context *ctxt = func->m_ctxt;
2807 JIT_LOG_FUNC (ctxt->get_logger ());
2808 /* LOC can be NULL. */
2809 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
2810 ctxt, loc,
2811 "Cannot add locals to an imported function");
2812 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
2813 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
2814 RETURN_NULL_IF_FAIL_PRINTF2 (
2815 type->has_known_size (),
2816 ctxt, loc,
2817 "unknown size for local \"%s\" (type: %s)",
2818 name,
2819 type->get_debug_string ());
2820 RETURN_NULL_IF_FAIL_PRINTF1 (
2821 !type->is_void (),
2822 ctxt, loc,
2823 "void type for local \"%s\"",
2824 name);
2826 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
2829 /* Public entrypoint. See description in libgccjit.h.
2831 After error-checking, the real work is done by the
2832 gcc::jit::recording::block::add_eval method in jit-recording.cc. */
2834 void
2835 gcc_jit_block_add_eval (gcc_jit_block *block,
2836 gcc_jit_location *loc,
2837 gcc_jit_rvalue *rvalue)
2839 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2840 gcc::jit::recording::context *ctxt = block->get_context ();
2841 JIT_LOG_FUNC (ctxt->get_logger ());
2842 /* LOC can be NULL. */
2843 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2845 gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
2847 /* "stmt" should be good enough to be usable in error-messages,
2848 but might still not be compilable; perform some more
2849 error-checking here. We do this here so that the error messages
2850 can contain a stringified version of "stmt", whilst appearing
2851 as close as possible to the point of failure. */
2852 rvalue->verify_valid_within_stmt (__func__, stmt);
2855 /* Public entrypoint. See description in libgccjit.h.
2857 After error-checking, the real work is done by the
2858 gcc::jit::recording::block::add_assignment method in
2859 jit-recording.cc. */
2861 void
2862 gcc_jit_block_add_assignment (gcc_jit_block *block,
2863 gcc_jit_location *loc,
2864 gcc_jit_lvalue *lvalue,
2865 gcc_jit_rvalue *rvalue)
2867 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2868 gcc::jit::recording::context *ctxt = block->get_context ();
2869 JIT_LOG_FUNC (ctxt->get_logger ());
2870 /* LOC can be NULL. */
2871 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
2872 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2873 RETURN_IF_FAIL_PRINTF4 (
2874 compatible_types (lvalue->get_type (),
2875 rvalue->get_type ()),
2876 ctxt, loc,
2877 "mismatching types:"
2878 " assignment to %s (type: %s) from %s (type: %s)",
2879 lvalue->get_debug_string (),
2880 lvalue->get_type ()->get_debug_string (),
2881 rvalue->get_debug_string (),
2882 rvalue->get_type ()->get_debug_string ());
2884 gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
2886 /* "stmt" should be good enough to be usable in error-messages,
2887 but might still not be compilable; perform some more
2888 error-checking here. We do this here so that the error messages
2889 can contain a stringified version of "stmt", whilst appearing
2890 as close as possible to the point of failure. */
2891 lvalue->verify_valid_within_stmt (__func__, stmt);
2892 rvalue->verify_valid_within_stmt (__func__, stmt);
2895 /* Public entrypoint. See description in libgccjit.h.
2897 After error-checking, the real work is done by the
2898 gcc::jit::recording::block::add_assignment_op method in
2899 jit-recording.cc. */
2901 void
2902 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
2903 gcc_jit_location *loc,
2904 gcc_jit_lvalue *lvalue,
2905 enum gcc_jit_binary_op op,
2906 gcc_jit_rvalue *rvalue)
2908 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2909 gcc::jit::recording::context *ctxt = block->get_context ();
2910 JIT_LOG_FUNC (ctxt->get_logger ());
2911 /* LOC can be NULL. */
2912 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
2913 RETURN_IF_FAIL_PRINTF1 (
2914 valid_binary_op_p (op),
2915 ctxt, loc,
2916 "unrecognized value for enum gcc_jit_binary_op: %i",
2917 op);
2918 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2919 RETURN_IF_FAIL_PRINTF4 (
2920 compatible_types (lvalue->get_type (),
2921 rvalue->get_type ()),
2922 ctxt, loc,
2923 "mismatching types:"
2924 " assignment to %s (type: %s) involving %s (type: %s)",
2925 lvalue->get_debug_string (),
2926 lvalue->get_type ()->get_debug_string (),
2927 rvalue->get_debug_string (),
2928 rvalue->get_type ()->get_debug_string ());
2930 gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
2932 /* "stmt" should be good enough to be usable in error-messages,
2933 but might still not be compilable; perform some more
2934 error-checking here. We do this here so that the error messages
2935 can contain a stringified version of "stmt", whilst appearing
2936 as close as possible to the point of failure. */
2937 lvalue->verify_valid_within_stmt (__func__, stmt);
2938 rvalue->verify_valid_within_stmt (__func__, stmt);
2941 /* Internal helper function for determining if rvalue BOOLVAL is of
2942 boolean type. For use by gcc_jit_block_end_with_conditional. */
2944 static bool
2945 is_bool (gcc_jit_rvalue *boolval)
2947 gcc::jit::recording::type *actual_type = boolval->get_type ();
2948 gcc::jit::recording::type *bool_type =
2949 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
2950 return actual_type == bool_type;
2953 /* Public entrypoint. See description in libgccjit.h.
2955 After error-checking, the real work is done by the
2956 gcc::jit::recording::block::end_with_conditional method in
2957 jit-recording.cc. */
2959 void
2960 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
2961 gcc_jit_location *loc,
2962 gcc_jit_rvalue *boolval,
2963 gcc_jit_block *on_true,
2964 gcc_jit_block *on_false)
2966 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2967 gcc::jit::recording::context *ctxt = block->get_context ();
2968 JIT_LOG_FUNC (ctxt->get_logger ());
2969 /* LOC can be NULL. */
2970 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
2971 RETURN_IF_FAIL_PRINTF2 (
2972 is_bool (boolval), ctxt, loc,
2973 "%s (type: %s) is not of boolean type ",
2974 boolval->get_debug_string (),
2975 boolval->get_type ()->get_debug_string ());
2976 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
2977 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
2978 RETURN_IF_FAIL_PRINTF4 (
2979 block->get_function () == on_true->get_function (),
2980 ctxt, loc,
2981 "\"on_true\" block is not in same function:"
2982 " source block %s is in function %s"
2983 " whereas target block %s is in function %s",
2984 block->get_debug_string (),
2985 block->get_function ()->get_debug_string (),
2986 on_true->get_debug_string (),
2987 on_true->get_function ()->get_debug_string ());
2988 RETURN_IF_FAIL_PRINTF4 (
2989 block->get_function () == on_false->get_function (),
2990 ctxt, loc,
2991 "\"on_false\" block is not in same function:"
2992 " source block %s is in function %s"
2993 " whereas target block %s is in function %s",
2994 block->get_debug_string (),
2995 block->get_function ()->get_debug_string (),
2996 on_false->get_debug_string (),
2997 on_false->get_function ()->get_debug_string ());
2999 gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
3001 /* "stmt" should be good enough to be usable in error-messages,
3002 but might still not be compilable; perform some more
3003 error-checking here. We do this here so that the error messages
3004 can contain a stringified version of "stmt", whilst appearing
3005 as close as possible to the point of failure. */
3006 boolval->verify_valid_within_stmt (__func__, stmt);
3009 /* Public entrypoint. See description in libgccjit.h.
3011 After error-checking, the real work is done by the
3012 gcc::jit::recording::block::add_comment method in
3013 jit-recording.cc. */
3015 void
3016 gcc_jit_block_add_comment (gcc_jit_block *block,
3017 gcc_jit_location *loc,
3018 const char *text)
3020 RETURN_IF_NOT_VALID_BLOCK (block, loc);
3021 gcc::jit::recording::context *ctxt = block->get_context ();
3022 JIT_LOG_FUNC (ctxt->get_logger ());
3023 /* LOC can be NULL. */
3024 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
3026 block->add_comment (loc, text);
3029 /* Public entrypoint. See description in libgccjit.h.
3031 After error-checking, the real work is done by the
3032 gcc::jit::recording::block::end_with_jump method in
3033 jit-recording.cc. */
3035 void
3036 gcc_jit_block_end_with_jump (gcc_jit_block *block,
3037 gcc_jit_location *loc,
3038 gcc_jit_block *target)
3040 RETURN_IF_NOT_VALID_BLOCK (block, loc);
3041 gcc::jit::recording::context *ctxt = block->get_context ();
3042 JIT_LOG_FUNC (ctxt->get_logger ());
3043 /* LOC can be NULL. */
3044 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
3045 RETURN_IF_FAIL_PRINTF4 (
3046 block->get_function () == target->get_function (),
3047 ctxt, loc,
3048 "target block is not in same function:"
3049 " source block %s is in function %s"
3050 " whereas target block %s is in function %s",
3051 block->get_debug_string (),
3052 block->get_function ()->get_debug_string (),
3053 target->get_debug_string (),
3054 target->get_function ()->get_debug_string ());
3056 block->end_with_jump (loc, target);
3059 /* Public entrypoint. See description in libgccjit.h.
3061 After error-checking, the real work is done by the
3062 gcc::jit::recording::block::end_with_return method in
3063 jit-recording.cc. */
3065 void
3066 gcc_jit_block_end_with_return (gcc_jit_block *block,
3067 gcc_jit_location *loc,
3068 gcc_jit_rvalue *rvalue)
3070 RETURN_IF_NOT_VALID_BLOCK (block, loc);
3071 gcc::jit::recording::context *ctxt = block->get_context ();
3072 JIT_LOG_FUNC (ctxt->get_logger ());
3073 /* LOC can be NULL. */
3074 gcc::jit::recording::function *func = block->get_function ();
3075 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
3076 RETURN_IF_FAIL_PRINTF4 (
3077 compatible_types (
3078 func->get_return_type (),
3079 rvalue->get_type ()),
3080 ctxt, loc,
3081 "mismatching types:"
3082 " return of %s (type: %s) in function %s (return type: %s)",
3083 rvalue->get_debug_string (),
3084 rvalue->get_type ()->get_debug_string (),
3085 func->get_debug_string (),
3086 func->get_return_type ()->get_debug_string ());
3088 gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
3090 /* "stmt" should be good enough to be usable in error-messages,
3091 but might still not be compilable; perform some more
3092 error-checking here. We do this here so that the error messages
3093 can contain a stringified version of "stmt", whilst appearing
3094 as close as possible to the point of failure. */
3095 rvalue->verify_valid_within_stmt (__func__, stmt);
3098 /* Public entrypoint. See description in libgccjit.h.
3100 After error-checking, the real work is done by the
3101 gcc::jit::recording::block::end_with_return method in
3102 jit-recording.cc. */
3104 void
3105 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
3106 gcc_jit_location *loc)
3108 RETURN_IF_NOT_VALID_BLOCK (block, loc);
3109 gcc::jit::recording::context *ctxt = block->get_context ();
3110 JIT_LOG_FUNC (ctxt->get_logger ());
3111 /* LOC can be NULL. */
3112 gcc::jit::recording::function *func = block->get_function ();
3113 RETURN_IF_FAIL_PRINTF2 (
3114 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
3115 ctxt, loc,
3116 "mismatching types:"
3117 " void return in function %s (return type: %s)",
3118 func->get_debug_string (),
3119 func->get_return_type ()->get_debug_string ());
3121 block->end_with_return (loc, NULL);
3124 /* Public entrypoint. See description in libgccjit.h.
3126 After error-checking, the real work is done by the
3127 gcc::jit::recording::context::new_case method in
3128 jit-recording.cc. */
3130 gcc_jit_case *
3131 gcc_jit_context_new_case (gcc_jit_context *ctxt,
3132 gcc_jit_rvalue *min_value,
3133 gcc_jit_rvalue *max_value,
3134 gcc_jit_block *block)
3136 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3137 JIT_LOG_FUNC (ctxt->get_logger ());
3138 RETURN_NULL_IF_FAIL (min_value, ctxt, NULL, "NULL min_value");
3139 RETURN_NULL_IF_FAIL (max_value, ctxt, NULL, "NULL max_value");
3140 RETURN_NULL_IF_FAIL (block, ctxt, NULL, "NULL block");
3142 RETURN_NULL_IF_FAIL_PRINTF1 (min_value->is_constant (), ctxt, NULL,
3143 "min_value is not a constant: %s",
3144 min_value->get_debug_string ());
3145 RETURN_NULL_IF_FAIL_PRINTF1 (max_value->is_constant (), ctxt, NULL,
3146 "max_value is not a constant: %s",
3147 max_value->get_debug_string ());
3148 RETURN_NULL_IF_FAIL_PRINTF2 (
3149 min_value->get_type ()->is_int (),
3150 ctxt, NULL,
3151 "min_value: %s (type: %s) is not of integer type",
3152 min_value->get_debug_string (),
3153 min_value->get_type ()->get_debug_string ());
3154 RETURN_NULL_IF_FAIL_PRINTF2 (
3155 max_value->get_type ()->is_int (),
3156 ctxt, NULL,
3157 "max_value: %s (type: %s) is not of integer type",
3158 max_value->get_debug_string (),
3159 max_value->get_type ()->get_debug_string ());
3161 wide_int wi_min, wi_max;
3162 if (!min_value->get_wide_int (&wi_min))
3163 gcc_unreachable ();
3164 if (!max_value->get_wide_int (&wi_max))
3165 gcc_unreachable ();
3166 RETURN_NULL_IF_FAIL_PRINTF2 (
3167 wi::les_p (wi_min, wi_max),
3168 ctxt, NULL,
3169 "min_value: %s > max_value: %s",
3170 min_value->get_debug_string (),
3171 max_value->get_debug_string ());
3172 return (gcc_jit_case *)ctxt->new_case (min_value,
3173 max_value,
3174 block);
3177 /* Public entrypoint. See description in libgccjit.h.
3179 After error-checking, this calls the trivial
3180 gcc::jit::recording::memento::as_object method (a case is a
3181 memento), in jit-recording.h. */
3183 gcc_jit_object *
3184 gcc_jit_case_as_object (gcc_jit_case *case_)
3186 RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case");
3188 return static_cast <gcc_jit_object *> (case_->as_object ());
3191 /* Helper function for gcc_jit_block_end_with_switch and
3192 valid_case_for_switch. */
3194 static bool
3195 valid_dest_for_switch (gcc::jit::recording::context *ctxt,
3196 gcc_jit_location *loc,
3197 const char *api_funcname,
3198 gcc::jit::recording::block *switch_block,
3199 gcc::jit::recording::block *dest_block,
3200 const char *dest_block_desc)
3202 if (!dest_block)
3204 jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc);
3205 return false;
3207 gcc::jit::recording::function *switch_fn = switch_block->get_function ();
3208 gcc::jit::recording::function *dest_fn = dest_block->get_function ();
3209 if (switch_fn != dest_fn)
3211 jit_error (ctxt, loc,
3212 "%s: %s is not in same function:"
3213 " switch block %s is in function %s"
3214 " whereas %s %s is in function %s",
3215 api_funcname,
3216 dest_block_desc,
3217 switch_block->get_debug_string (),
3218 switch_fn->get_debug_string (),
3219 dest_block_desc,
3220 dest_block->get_debug_string (),
3221 dest_fn->get_debug_string ());
3222 return false;
3224 return true;
3227 /* Helper function for gcc_jit_block_end_with_switch. */
3229 static bool
3230 valid_case_for_switch (gcc::jit::recording::context *ctxt,
3231 gcc_jit_location *loc,
3232 const char *api_funcname,
3233 gcc_jit_block *switch_block,
3234 gcc_jit_rvalue *expr,
3235 gcc_jit_case *case_,
3236 const char *case_desc,
3237 int case_idx)
3239 if (!case_)
3241 jit_error (ctxt, loc,
3242 "%s:"
3243 " NULL case %i",
3244 api_funcname,
3245 case_idx);
3246 return false;
3248 if (!valid_dest_for_switch (ctxt, loc,
3249 api_funcname,
3250 switch_block,
3251 case_->get_dest_block (),
3252 case_desc))
3253 return false;
3254 gcc::jit::recording::type *expr_type = expr->get_type ();
3255 if (expr_type != case_->get_min_value ()->get_type ())
3257 jit_error (ctxt, loc,
3258 "%s:"
3259 " mismatching types between case and expression:"
3260 " cases[%i]->min_value: %s (type: %s)"
3261 " expr: %s (type: %s)",
3262 api_funcname,
3263 case_idx,
3264 case_->get_min_value ()->get_debug_string (),
3265 case_->get_min_value ()->get_type ()->get_debug_string (),
3266 expr->get_debug_string (),
3267 expr_type->get_debug_string ());
3268 return false;
3270 if (expr_type != case_->get_max_value ()->get_type ())
3272 jit_error (ctxt, loc,
3273 "%s:"
3274 " mismatching types between case and expression:"
3275 " cases[%i]->max_value: %s (type: %s)"
3276 " expr: %s (type: %s)",
3277 api_funcname,
3278 case_idx,
3279 case_->get_max_value ()->get_debug_string (),
3280 case_->get_max_value ()->get_type ()->get_debug_string (),
3281 expr->get_debug_string (),
3282 expr_type->get_debug_string ());
3283 return false;
3285 return true;
3288 /* A class for holding the data we need to perform error-checking
3289 on a libgccjit API call. */
3291 class api_call_validator
3293 public:
3294 api_call_validator (gcc::jit::recording::context *ctxt,
3295 gcc_jit_location *loc,
3296 const char *funcname)
3297 : m_ctxt (ctxt),
3298 m_loc (loc),
3299 m_funcname (funcname)
3302 protected:
3303 gcc::jit::recording::context *m_ctxt;
3304 gcc_jit_location *m_loc;
3305 const char *m_funcname;
3308 /* A class for verifying that the ranges of cases within
3309 gcc_jit_block_end_with_switch don't overlap. */
3311 class case_range_validator : public api_call_validator
3313 public:
3314 case_range_validator (gcc::jit::recording::context *ctxt,
3315 gcc_jit_location *loc,
3316 const char *funcname);
3318 bool
3319 validate (gcc_jit_case *case_, int idx);
3321 private:
3322 static int
3323 case_compare (gcc::jit::recording::rvalue *k1,
3324 gcc::jit::recording::rvalue *k2);
3326 static wide_int
3327 get_wide_int (gcc::jit::recording::rvalue *k);
3329 private:
3330 typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases;
3333 /* case_range_validator's ctor. */
3335 case_range_validator::case_range_validator (gcc::jit::recording::context *ctxt,
3336 gcc_jit_location *loc,
3337 const char *funcname)
3338 : api_call_validator (ctxt, loc, funcname),
3339 m_cases (case_compare, NULL, NULL)
3343 /* Ensure that the range of CASE_ does not overlap with any of the
3344 ranges of cases we've already seen.
3345 Return true if everything is OK.
3346 Return false and emit an error if there is an overlap.
3347 Compare with c-family/c-common.cc:c_add_case_label. */
3349 bool
3350 case_range_validator::validate (gcc_jit_case *case_,
3351 int case_idx)
3353 /* Look up the LOW_VALUE in the table of case labels we already
3354 have. */
3355 gcc_jit_case *other = m_cases.lookup (case_->get_min_value ());
3357 /* If there was not an exact match, check for overlapping ranges. */
3358 if (!other)
3360 gcc_jit_case *pred;
3361 gcc_jit_case *succ;
3363 /* Even though there wasn't an exact match, there might be an
3364 overlap between this case range and another case range.
3365 Since we've (inductively) not allowed any overlapping case
3366 ranges, we simply need to find the greatest low case label
3367 that is smaller that CASE_MIN_VALUE, and the smallest low case
3368 label that is greater than CASE_MAX_VALUE. If there is an overlap
3369 it will occur in one of these two ranges. */
3370 pred = m_cases.predecessor (case_->get_min_value ());
3371 succ = m_cases.successor (case_->get_max_value ());
3373 /* Check to see if the PRED overlaps. It is smaller than
3374 the LOW_VALUE, so we only need to check its max value. */
3375 if (pred)
3377 wide_int wi_case_min = get_wide_int (case_->get_min_value ());
3378 wide_int wi_pred_max = get_wide_int (pred->get_max_value ());
3379 if (wi::ges_p (wi_pred_max, wi_case_min))
3380 other = pred;
3383 if (!other && succ)
3385 /* Check to see if the SUCC overlaps. The low end of that
3386 range is bigger than the low end of the current range. */
3387 wide_int wi_case_max = get_wide_int (case_->get_max_value ());
3388 wide_int wi_succ_min = get_wide_int (succ->get_min_value ());
3389 if (wi::les_p (wi_succ_min, wi_case_max))
3390 other = succ;
3394 /* If there was an overlap, issue an error. */
3395 if (other)
3397 jit_error (m_ctxt, m_loc,
3398 "%s: duplicate (or overlapping) cases values:"
3399 " case %i: %s overlaps %s",
3400 m_funcname,
3401 case_idx,
3402 case_->get_debug_string (),
3403 other->get_debug_string ());
3404 return false;
3407 /* Register this case label in the splay tree. */
3408 m_cases.insert (case_->get_min_value (),
3409 case_);
3410 return true;
3413 /* Compare with c-family/c-common.cc:case_compare, which acts on tree
3414 nodes, rather than rvalue *.
3416 Comparator for case label values. K1 and K2 must be constant integer
3417 values (anything else should have been rejected by
3418 gcc_jit_context_new_case.
3420 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3421 K2, and 0 if K1 and K2 are equal. */
3424 case_range_validator::case_compare (gcc::jit::recording::rvalue * k1,
3425 gcc::jit::recording::rvalue * k2)
3427 wide_int wi1 = get_wide_int (k1);
3428 wide_int wi2 = get_wide_int (k2);
3429 return wi::cmps(wi1, wi2);
3432 /* Given a const int rvalue K, get the underlying value as a wide_int. */
3434 wide_int
3435 case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k)
3437 wide_int wi;
3438 bool got_wi = k->get_wide_int (&wi);
3439 gcc_assert (got_wi);
3440 return wi;
3443 /* Public entrypoint. See description in libgccjit.h.
3445 After error-checking, the real work is done by the
3446 gcc::jit::recording::block::end_with_switch method in
3447 jit-recording.cc. */
3449 void
3450 gcc_jit_block_end_with_switch (gcc_jit_block *block,
3451 gcc_jit_location *loc,
3452 gcc_jit_rvalue *expr,
3453 gcc_jit_block *default_block,
3454 int num_cases,
3455 gcc_jit_case **cases)
3457 RETURN_IF_NOT_VALID_BLOCK (block, loc);
3458 gcc::jit::recording::context *ctxt = block->get_context ();
3459 JIT_LOG_FUNC (ctxt->get_logger ());
3460 /* LOC can be NULL. */
3461 RETURN_IF_FAIL (expr, ctxt, loc,
3462 "NULL expr");
3463 gcc::jit::recording::type *expr_type = expr->get_type ();
3464 RETURN_IF_FAIL_PRINTF2 (
3465 expr_type->is_int (),
3466 ctxt, loc,
3467 "expr: %s (type: %s) is not of integer type",
3468 expr->get_debug_string (),
3469 expr_type->get_debug_string ());
3470 if (!valid_dest_for_switch (ctxt, loc,
3471 __func__,
3472 block,
3473 default_block,
3474 "default_block"))
3475 return;
3476 RETURN_IF_FAIL (num_cases >= 0, ctxt, loc, "num_cases < 0");
3477 case_range_validator crv (ctxt, loc, __func__);
3478 for (int i = 0; i < num_cases; i++)
3480 char case_desc[32];
3481 snprintf (case_desc, sizeof (case_desc),
3482 "cases[%i]", i);
3483 if (!valid_case_for_switch (ctxt, loc,
3484 __func__,
3485 block,
3486 expr,
3487 cases[i],
3488 case_desc,
3490 return;
3491 if (!crv.validate (cases[i], i))
3492 return;
3495 block->end_with_switch (loc, expr, default_block,
3496 num_cases,
3497 (gcc::jit::recording::case_ **)cases);
3500 /**********************************************************************
3501 Option-management
3502 **********************************************************************/
3504 /* Public entrypoint. See description in libgccjit.h.
3506 After error-checking, the real work is done by the
3507 gcc::jit::recording::context::set_str_option method in
3508 jit-recording.cc. */
3510 void
3511 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
3512 enum gcc_jit_str_option opt,
3513 const char *value)
3515 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3516 JIT_LOG_FUNC (ctxt->get_logger ());
3517 /* opt is checked by the inner function.
3518 value can be NULL. */
3520 ctxt->set_str_option (opt, value);
3523 /* Public entrypoint. See description in libgccjit.h.
3525 After error-checking, the real work is done by the
3526 gcc::jit::recording::context::set_int_option method in
3527 jit-recording.cc. */
3529 void
3530 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
3531 enum gcc_jit_int_option opt,
3532 int value)
3534 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3535 JIT_LOG_FUNC (ctxt->get_logger ());
3536 /* opt is checked by the inner function. */
3538 ctxt->set_int_option (opt, value);
3541 /* Public entrypoint. See description in libgccjit.h.
3543 After error-checking, the real work is done by the
3544 gcc::jit::recording::context::set_bool_option method in
3545 jit-recording.cc. */
3547 void
3548 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
3549 enum gcc_jit_bool_option opt,
3550 int value)
3552 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3553 JIT_LOG_FUNC (ctxt->get_logger ());
3554 /* opt is checked by the inner function. */
3556 ctxt->set_bool_option (opt, 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::set_inner_bool_option method in
3563 jit-recording.cc. */
3565 void
3566 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
3567 int bool_value)
3569 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3570 JIT_LOG_FUNC (ctxt->get_logger ());
3571 ctxt->set_inner_bool_option (
3572 gcc::jit::INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
3573 bool_value);
3576 /* Public entrypoint. See description in libgccjit.h.
3578 After error-checking, the real work is done by the
3579 gcc::jit::recording::context::set_inner_bool_option method in
3580 jit-recording.cc. */
3582 void
3583 gcc_jit_context_set_bool_print_errors_to_stderr (gcc_jit_context *ctxt,
3584 int enabled)
3586 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3587 JIT_LOG_FUNC (ctxt->get_logger ());
3588 ctxt->set_inner_bool_option (
3589 gcc::jit::INNER_BOOL_OPTION_PRINT_ERRORS_TO_STDERR,
3590 enabled);
3593 /* Public entrypoint. See description in libgccjit.h.
3595 After error-checking, the real work is done by the
3596 gcc::jit::recording::context::set_inner_bool_option method in
3597 jit-recording.cc. */
3599 extern void
3600 gcc_jit_context_set_bool_use_external_driver (gcc_jit_context *ctxt,
3601 int bool_value)
3603 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3604 JIT_LOG_FUNC (ctxt->get_logger ());
3605 ctxt->set_inner_bool_option (
3606 gcc::jit::INNER_BOOL_OPTION_USE_EXTERNAL_DRIVER,
3607 bool_value);
3610 /* Public entrypoint. See description in libgccjit.h.
3612 After error-checking, the real work is done by the
3613 gcc::jit::recording::context::add_command_line_option method in
3614 jit-recording.cc. */
3616 void
3617 gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
3618 const char *optname)
3620 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3621 JIT_LOG_FUNC (ctxt->get_logger ());
3622 RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
3623 if (ctxt->get_logger ())
3624 ctxt->get_logger ()->log ("optname: %s", optname);
3626 ctxt->add_command_line_option (optname);
3629 /* Public entrypoint. See description in libgccjit.h.
3631 The real work is done by the
3632 gcc::jit::recording::context::add_driver_option method in
3633 jit-recording.cc. */
3635 void
3636 gcc_jit_context_add_driver_option (gcc_jit_context *ctxt,
3637 const char *optname)
3639 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3640 JIT_LOG_FUNC (ctxt->get_logger ());
3641 RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
3642 if (ctxt->get_logger ())
3643 ctxt->get_logger ()->log ("optname: %s", optname);
3645 ctxt->add_driver_option (optname);
3648 /* Public entrypoint. See description in libgccjit.h.
3650 After error-checking, the real work is done by the
3651 gcc::jit::recording::context::enable_dump method in
3652 jit-recording.cc. */
3654 void
3655 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
3656 const char *dumpname,
3657 char **out_ptr)
3659 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3660 JIT_LOG_FUNC (ctxt->get_logger ());
3661 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
3662 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
3664 ctxt->enable_dump (dumpname, out_ptr);
3667 /* Public entrypoint. See description in libgccjit.h.
3669 After error-checking, the real work is done by the
3670 gcc::jit::recording::context::compile method in
3671 jit-recording.cc. */
3673 gcc_jit_result *
3674 gcc_jit_context_compile (gcc_jit_context *ctxt)
3676 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3678 JIT_LOG_FUNC (ctxt->get_logger ());
3680 ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
3682 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
3684 ctxt->log ("%s: returning (gcc_jit_result *)%p",
3685 __func__, (void *)result);
3687 return result;
3690 /* Public entrypoint. See description in libgccjit.h.
3692 After error-checking, the real work is done by the
3693 gcc::jit::recording::context::compile_to_file method in
3694 jit-recording.cc. */
3696 void
3697 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
3698 enum gcc_jit_output_kind output_kind,
3699 const char *output_path)
3701 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3702 JIT_LOG_FUNC (ctxt->get_logger ());
3703 RETURN_IF_FAIL_PRINTF1 (
3704 ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
3705 && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
3706 ctxt, NULL,
3707 "unrecognized output_kind: %i",
3708 output_kind);
3709 RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
3711 ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
3712 ctxt->log ("output_kind: %i", output_kind);
3713 ctxt->log ("output_path: %s", output_path);
3715 ctxt->compile_to_file (output_kind, output_path);
3719 /* Public entrypoint. See description in libgccjit.h.
3721 After error-checking, the real work is done by the
3722 gcc::jit::recording::context::dump_to_file method in
3723 jit-recording.cc. */
3725 void
3726 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
3727 const char *path,
3728 int update_locations)
3730 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3731 JIT_LOG_FUNC (ctxt->get_logger ());
3732 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
3733 ctxt->dump_to_file (path, update_locations);
3736 /* Public entrypoint. See description in libgccjit.h. */
3738 void
3739 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
3740 FILE *logfile,
3741 int flags,
3742 int verbosity)
3744 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3745 JIT_LOG_FUNC (ctxt->get_logger ());
3746 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
3747 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
3749 gcc::jit::logger *logger;
3750 if (logfile)
3751 logger = new gcc::jit::logger (logfile, flags, verbosity);
3752 else
3753 logger = NULL;
3754 ctxt->set_logger (logger);
3757 /* Public entrypoint. See description in libgccjit.h.
3759 After error-checking, the real work is done by the
3760 gcc::jit::recording::context::dump_reproducer_to_file method in
3761 jit-recording.cc. */
3763 void
3764 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
3765 const char *path)
3767 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3768 JIT_LOG_FUNC (ctxt->get_logger ());
3769 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
3770 ctxt->dump_reproducer_to_file (path);
3773 /* Public entrypoint. See description in libgccjit.h.
3775 After error-checking, the real work is done by the
3776 gcc::jit::recording::context::get_first_error method in
3777 jit-recording.cc. */
3779 const char *
3780 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
3782 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3783 JIT_LOG_FUNC (ctxt->get_logger ());
3785 return ctxt->get_first_error ();
3788 /* Public entrypoint. See description in libgccjit.h.
3790 After error-checking, the real work is done by the
3791 gcc::jit::recording::context::get_last_error method in
3792 jit-recording.cc. */
3794 const char *
3795 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
3797 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3799 return ctxt->get_last_error ();
3802 /* Public entrypoint. See description in libgccjit.h.
3804 After error-checking, the real work is done by the
3805 gcc::jit::result::get_code method in jit-result.cc. */
3807 void *
3808 gcc_jit_result_get_code (gcc_jit_result *result,
3809 const char *fnname)
3811 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
3812 JIT_LOG_FUNC (result->get_logger ());
3813 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
3815 result->log ("locating fnname: %s", fnname);
3816 void *code = result->get_code (fnname);
3817 result->log ("%s: returning (void *)%p", __func__, code);
3819 return code;
3822 /* Public entrypoint. See description in libgccjit.h.
3824 After error-checking, the real work is done by the
3825 gcc::jit::result::get_global method in jit-result.cc. */
3827 void *
3828 gcc_jit_result_get_global (gcc_jit_result *result,
3829 const char *name)
3831 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
3832 JIT_LOG_FUNC (result->get_logger ());
3833 RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
3835 void *global = result->get_global (name);
3836 result->log ("%s: returning (void *)%p", __func__, global);
3838 return global;
3841 /* Public entrypoint. See description in libgccjit.h.
3843 After error-checking, this is essentially a wrapper around the
3844 destructor for gcc::jit::result in jit-result.cc. */
3846 void
3847 gcc_jit_result_release (gcc_jit_result *result)
3849 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
3850 JIT_LOG_FUNC (result->get_logger ());
3851 result->log ("deleting result: %p", (void *)result);
3852 delete result;
3855 /**********************************************************************
3856 Timing support.
3857 **********************************************************************/
3859 /* Create a gcc_jit_timer instance, and start timing. */
3861 gcc_jit_timer *
3862 gcc_jit_timer_new (void)
3864 gcc_jit_timer *timer = new gcc_jit_timer ();
3865 timer->start (TV_TOTAL);
3866 timer->push (TV_JIT_CLIENT_CODE);
3867 return timer;
3870 /* Release a gcc_jit_timer instance. */
3872 void
3873 gcc_jit_timer_release (gcc_jit_timer *timer)
3875 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
3877 delete timer;
3880 /* Associate a gcc_jit_timer instance with a context. */
3882 void
3883 gcc_jit_context_set_timer (gcc_jit_context *ctxt,
3884 gcc_jit_timer *timer)
3886 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
3887 RETURN_IF_FAIL (timer, ctxt, NULL, "NULL timer");
3889 ctxt->set_timer (timer);
3892 /* Get the timer associated with a context (if any). */
3894 gcc_jit_timer *
3895 gcc_jit_context_get_timer (gcc_jit_context *ctxt)
3897 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
3899 return (gcc_jit_timer *)ctxt->get_timer ();
3902 /* Push the given item onto the timing stack. */
3904 void
3905 gcc_jit_timer_push (gcc_jit_timer *timer,
3906 const char *item_name)
3908 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
3909 RETURN_IF_FAIL (item_name, NULL, NULL, "NULL item_name");
3910 timer->push_client_item (item_name);
3913 /* Pop the top item from the timing stack. */
3915 void
3916 gcc_jit_timer_pop (gcc_jit_timer *timer,
3917 const char *item_name)
3919 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
3921 if (item_name)
3923 const char *top_item_name = timer->get_topmost_item_name ();
3925 RETURN_IF_FAIL_PRINTF1
3926 (top_item_name, NULL, NULL,
3927 "pop of empty timing stack (attempting to pop: \"%s\")",
3928 item_name);
3930 RETURN_IF_FAIL_PRINTF2
3931 (strcmp (item_name, top_item_name) == 0, NULL, NULL,
3932 "mismatching item_name:"
3933 " top of timing stack: \"%s\","
3934 " attempting to pop: \"%s\"",
3935 top_item_name,
3936 item_name);
3939 timer->pop_client_item ();
3942 /* Print timing information to the given stream about activity since
3943 the timer was started. */
3945 void
3946 gcc_jit_timer_print (gcc_jit_timer *timer,
3947 FILE *f_out)
3949 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
3950 RETURN_IF_FAIL (f_out, NULL, NULL, "NULL f_out");
3952 timer->pop (TV_JIT_CLIENT_CODE);
3953 timer->stop (TV_TOTAL);
3954 timer->print (f_out);
3955 timer->start (TV_TOTAL);
3956 timer->push (TV_JIT_CLIENT_CODE);
3959 /* Public entrypoint. See description in libgccjit.h.
3961 After error-checking, the real work is effectively done by the
3962 gcc::jit::base_call::set_require_tail_call setter in jit-recording.h. */
3964 void
3965 gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *rvalue,
3966 int require_tail_call)
3968 RETURN_IF_FAIL (rvalue, NULL, NULL, "NULL call");
3969 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
3971 /* Verify that it's a call. */
3972 gcc::jit::recording::base_call *call = rvalue->dyn_cast_base_call ();
3973 RETURN_IF_FAIL_PRINTF1 (call, NULL, NULL, "not a call: %s",
3974 rvalue->get_debug_string ());
3976 call->set_require_tail_call (require_tail_call);
3979 /* Public entrypoint. See description in libgccjit.h.
3981 After error-checking, the real work is done by the
3982 gcc::jit::recording::type::get_aligned method, in
3983 jit-recording.cc. */
3985 gcc_jit_type *
3986 gcc_jit_type_get_aligned (gcc_jit_type *type,
3987 size_t alignment_in_bytes)
3989 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
3991 gcc::jit::recording::context *ctxt = type->m_ctxt;
3993 JIT_LOG_FUNC (ctxt->get_logger ());
3995 RETURN_NULL_IF_FAIL_PRINTF1
3996 (pow2_or_zerop (alignment_in_bytes), ctxt, NULL,
3997 "alignment not a power of two: %zi",
3998 alignment_in_bytes);
3999 RETURN_NULL_IF_FAIL (!type->is_void (), ctxt, NULL, "void type");
4001 return (gcc_jit_type *)type->get_aligned (alignment_in_bytes);
4004 void
4005 gcc_jit_function_add_attribute (gcc_jit_function *func,
4006 gcc_jit_fn_attribute attribute)
4008 RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4009 RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_FN_ATTRIBUTE_MAX),
4010 NULL,
4011 NULL,
4012 "attribute should be a `gcc_jit_fn_attribute` enum value");
4014 func->add_attribute (attribute);
4017 void
4018 gcc_jit_function_add_string_attribute (gcc_jit_function *func,
4019 gcc_jit_fn_attribute attribute,
4020 const char* value)
4022 RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4023 RETURN_IF_FAIL (value, NULL, NULL, "NULL value");
4024 RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_FN_ATTRIBUTE_MAX),
4025 NULL,
4026 NULL,
4027 "attribute should be a `gcc_jit_fn_attribute` enum value");
4029 func->add_string_attribute (attribute, value);
4032 /* This function adds an attribute with multiple integer values. For example
4033 `nonnull(1, 2)`. The numbers in `values` are supposed to map how they
4034 should be written in C code. So for `nonnull(1, 2)`, you should pass `1`
4035 and `2` in `values` (and set `length` to `2`). */
4036 void
4037 gcc_jit_function_add_integer_array_attribute (gcc_jit_function *func,
4038 gcc_jit_fn_attribute attribute,
4039 const int* values,
4040 size_t length)
4042 RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4043 RETURN_IF_FAIL (values, NULL, NULL, "NULL values");
4044 RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_FN_ATTRIBUTE_MAX),
4045 NULL,
4046 NULL,
4047 "attribute should be a `gcc_jit_fn_attribute` enum value");
4049 func->add_integer_array_attribute (attribute, values, length);
4052 void
4053 gcc_jit_lvalue_add_string_attribute (gcc_jit_lvalue *variable,
4054 gcc_jit_variable_attribute attribute,
4055 const char* value)
4057 RETURN_IF_FAIL (variable, NULL, NULL, "NULL variable");
4058 RETURN_IF_FAIL (value, NULL, NULL, "NULL value");
4059 RETURN_IF_FAIL (variable->is_global () || variable->is_local (),
4060 NULL,
4061 NULL,
4062 "variable should be a variable");
4063 RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_VARIABLE_ATTRIBUTE_MAX),
4064 NULL,
4065 NULL,
4066 "attribute should be a `gcc_jit_variable_attribute` enum value");
4068 variable->add_string_attribute (attribute, value);
4071 /* Public entrypoint. See description in libgccjit.h.
4073 After error-checking, the real work is done by the
4074 gcc::jit::recording::type::get_vector method, in
4075 jit-recording.cc. */
4077 gcc_jit_type *
4078 gcc_jit_type_get_vector (gcc_jit_type *type, size_t num_units)
4080 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
4082 gcc::jit::recording::context *ctxt = type->m_ctxt;
4084 JIT_LOG_FUNC (ctxt->get_logger ());
4086 RETURN_NULL_IF_FAIL_PRINTF1
4087 (type->is_int () || type->is_float (), ctxt, NULL,
4088 "type is not integral or floating point: %s",
4089 type->get_debug_string ());
4091 RETURN_NULL_IF_FAIL_PRINTF1
4092 (pow2_or_zerop (num_units), ctxt, NULL,
4093 "num_units not a power of two: %zi",
4094 num_units);
4096 return (gcc_jit_type *)type->get_vector (num_units);
4099 /* Public entrypoint. See description in libgccjit.h.
4101 After error-checking, the real work is done by the
4102 gcc::jit::recording::function::get_address method, in
4103 jit-recording.cc. */
4105 gcc_jit_rvalue *
4106 gcc_jit_function_get_address (gcc_jit_function *fn,
4107 gcc_jit_location *loc)
4109 RETURN_NULL_IF_FAIL (fn, NULL, NULL, "NULL function");
4111 gcc::jit::recording::context *ctxt = fn->m_ctxt;
4113 JIT_LOG_FUNC (ctxt->get_logger ());
4114 /* LOC can be NULL. */
4116 return (gcc_jit_rvalue *)fn->get_address (loc);
4119 /* Public entrypoint. See description in libgccjit.h.
4121 After error-checking, the real work is done by the
4122 gcc::jit::recording::context::new_rvalue_from_vector method, in
4123 jit-recording.cc. */
4125 extern gcc_jit_rvalue *
4126 gcc_jit_context_new_rvalue_from_vector (gcc_jit_context *ctxt,
4127 gcc_jit_location *loc,
4128 gcc_jit_type *vec_type,
4129 size_t num_elements,
4130 gcc_jit_rvalue **elements)
4132 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL ctxt");
4133 JIT_LOG_FUNC (ctxt->get_logger ());
4135 /* LOC can be NULL. */
4136 RETURN_NULL_IF_FAIL (vec_type, ctxt, loc, "NULL vec_type");
4138 /* "vec_type" must be a vector type. */
4139 gcc::jit::recording::vector_type *as_vec_type
4140 = vec_type->dyn_cast_vector_type ();
4141 RETURN_NULL_IF_FAIL_PRINTF1 (as_vec_type, ctxt, loc,
4142 "%s is not a vector type",
4143 vec_type->get_debug_string ());
4145 /* "num_elements" must match. */
4146 RETURN_NULL_IF_FAIL_PRINTF1 (
4147 num_elements == as_vec_type->get_num_units (), ctxt, loc,
4148 "num_elements != %zi", as_vec_type->get_num_units ());
4150 /* "elements must be non-NULL. */
4151 RETURN_NULL_IF_FAIL (elements, ctxt, loc, "NULL elements");
4153 /* Each of "elements" must be non-NULL and of the correct type. */
4154 gcc::jit::recording::type *element_type
4155 = as_vec_type->get_element_type ();
4156 for (size_t i = 0; i < num_elements; i++)
4158 RETURN_NULL_IF_FAIL_PRINTF1 (
4159 elements[i], ctxt, loc, "NULL elements[%zi]", i);
4160 RETURN_NULL_IF_FAIL_PRINTF4 (
4161 compatible_types (element_type,
4162 elements[i]->get_type ()),
4163 ctxt, loc,
4164 "mismatching type for element[%zi] (expected type: %s): %s (type: %s)",
4166 element_type->get_debug_string (),
4167 elements[i]->get_debug_string (),
4168 elements[i]->get_type ()->get_debug_string ());
4171 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_vector
4172 (loc,
4173 as_vec_type,
4174 (gcc::jit::recording::rvalue **)elements);
4177 /* A mutex around the cached state in parse_basever.
4178 Ideally this would be within parse_basever, but the mutex is only needed
4179 by libgccjit. */
4181 static std::mutex version_mutex;
4183 struct jit_version_info
4185 /* Default constructor. Populate via parse_basever,
4186 guarded by version_mutex. */
4187 jit_version_info ()
4189 std::lock_guard<std::mutex> g (version_mutex);
4190 parse_basever (&major, &minor, &patchlevel);
4193 int major;
4194 int minor;
4195 int patchlevel;
4199 extern int
4200 gcc_jit_version_major (void)
4202 jit_version_info vi;
4203 return vi.major;
4206 extern int
4207 gcc_jit_version_minor (void)
4209 jit_version_info vi;
4210 return vi.minor;
4213 extern int
4214 gcc_jit_version_patchlevel (void)
4216 jit_version_info vi;
4217 return vi.patchlevel;
4220 /**********************************************************************
4221 Asm support.
4222 **********************************************************************/
4224 /* Public entrypoint. See description in libgccjit.h.
4226 After error-checking, the real work is done by the
4227 gcc::jit::recording::block::add_extended_asm, in
4228 jit-recording.cc. */
4230 gcc_jit_extended_asm *
4231 gcc_jit_block_add_extended_asm (gcc_jit_block *block,
4232 gcc_jit_location *loc,
4233 const char *asm_template)
4235 RETURN_NULL_IF_NOT_VALID_BLOCK (block, loc);
4236 gcc::jit::recording::context *ctxt = block->get_context ();
4237 JIT_LOG_FUNC (ctxt->get_logger ());
4238 /* LOC can be NULL. */
4239 RETURN_NULL_IF_FAIL (asm_template, ctxt, loc, "NULL asm_template");
4241 return (gcc_jit_extended_asm *)block->add_extended_asm (loc, asm_template);
4244 /* Public entrypoint. See description in libgccjit.h.
4246 After error-checking, the real work is done by the
4247 gcc::jit::recording::block::end_with_extended_asm_goto, in
4248 jit-recording.cc. */
4250 gcc_jit_extended_asm *
4251 gcc_jit_block_end_with_extended_asm_goto (gcc_jit_block *block,
4252 gcc_jit_location *loc,
4253 const char *asm_template,
4254 int num_goto_blocks,
4255 gcc_jit_block **goto_blocks,
4256 gcc_jit_block *fallthrough_block)
4258 RETURN_NULL_IF_NOT_VALID_BLOCK (block, loc);
4259 gcc::jit::recording::context *ctxt = block->get_context ();
4260 JIT_LOG_FUNC (ctxt->get_logger ());
4261 /* LOC can be NULL. */
4262 RETURN_NULL_IF_FAIL (asm_template, ctxt, loc, "NULL asm_template");
4263 RETURN_NULL_IF_FAIL (num_goto_blocks >= 0, ctxt, loc, "num_goto_blocks < 0");
4264 for (int i = 0; i < num_goto_blocks; i++)
4265 RETURN_NULL_IF_FAIL_PRINTF1 (goto_blocks[i],
4266 ctxt, loc,
4267 "NULL goto_blocks[%i]", i);
4268 /* fallthrough_block can be NULL. */
4269 return (gcc_jit_extended_asm *)block->end_with_extended_asm_goto
4270 (loc, asm_template,
4271 num_goto_blocks, (gcc::jit::recording::block **)goto_blocks,
4272 fallthrough_block);
4275 /* Public entrypoint. See description in libgccjit.h.
4277 After error-checking, this calls the trivial
4278 gcc::jit::recording::memento::as_object method (an extended_asm is a
4279 memento), in jit-recording.h. */
4281 gcc_jit_object *
4282 gcc_jit_extended_asm_as_object (gcc_jit_extended_asm *ext_asm)
4284 RETURN_NULL_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4286 return static_cast <gcc_jit_object *> (ext_asm->as_object ());
4289 /* Public entrypoint. See description in libgccjit.h.
4291 After error-checking, the real work is done by the
4292 gcc::jit::recording::extended_asm::set_volatile_flag, in
4293 jit-recording.cc. */
4295 void
4296 gcc_jit_extended_asm_set_volatile_flag (gcc_jit_extended_asm *ext_asm,
4297 int flag)
4299 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4300 ext_asm->set_volatile_flag (flag);
4303 /* Public entrypoint. See description in libgccjit.h.
4305 After error-checking, the real work is done by the
4306 gcc::jit::recording::extended_asm::set_inline_flag, in
4307 jit-recording.cc. */
4309 void
4310 gcc_jit_extended_asm_set_inline_flag (gcc_jit_extended_asm *ext_asm,
4311 int flag)
4313 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4314 ext_asm->set_inline_flag (flag);
4317 /* Public entrypoint. See description in libgccjit.h.
4319 After error-checking, the real work is done by the
4320 gcc::jit::recording::extended_asm::add_output_operand, in
4321 jit-recording.cc. */
4323 void
4324 gcc_jit_extended_asm_add_output_operand (gcc_jit_extended_asm *ext_asm,
4325 const char *asm_symbolic_name,
4326 const char *constraint,
4327 gcc_jit_lvalue *dest)
4329 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4330 gcc::jit::recording::context *ctxt = ext_asm->get_context ();
4331 JIT_LOG_FUNC (ctxt->get_logger ());
4332 gcc::jit::recording::location *loc = ext_asm->get_loc ();
4333 /* asm_symbolic_name can be NULL. */
4334 RETURN_IF_FAIL (constraint, ctxt, loc, "NULL constraint");
4335 RETURN_IF_FAIL (dest, ctxt, loc, "NULL dest");
4336 RETURN_IF_FAIL (!ext_asm->is_goto (), ctxt, loc,
4337 "cannot add output operand to asm goto");
4338 ext_asm->add_output_operand (asm_symbolic_name, constraint, dest);
4341 /* Public entrypoint. See description in libgccjit.h.
4343 After error-checking, the real work is done by the
4344 gcc::jit::recording::extended_asm::add_input_operand, in
4345 jit-recording.cc. */
4347 extern void
4348 gcc_jit_extended_asm_add_input_operand (gcc_jit_extended_asm *ext_asm,
4349 const char *asm_symbolic_name,
4350 const char *constraint,
4351 gcc_jit_rvalue *src)
4353 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4354 gcc::jit::recording::context *ctxt = ext_asm->get_context ();
4355 JIT_LOG_FUNC (ctxt->get_logger ());
4356 gcc::jit::recording::location *loc = ext_asm->get_loc ();
4357 /* asm_symbolic_name can be NULL. */
4358 RETURN_IF_FAIL (constraint, ctxt, loc, "NULL constraint");
4359 RETURN_IF_FAIL (src, ctxt, loc, "NULL src");
4360 ext_asm->add_input_operand (asm_symbolic_name, constraint, src);
4363 /* Public entrypoint. See description in libgccjit.h.
4365 After error-checking, the real work is done by the
4366 gcc::jit::recording::extended_asm::add_clobber, in
4367 jit-recording.cc. */
4369 void
4370 gcc_jit_extended_asm_add_clobber (gcc_jit_extended_asm *ext_asm,
4371 const char *victim)
4373 RETURN_IF_FAIL (ext_asm, NULL, NULL, "NULL ext_asm");
4374 gcc::jit::recording::context *ctxt = ext_asm->get_context ();
4375 JIT_LOG_FUNC (ctxt->get_logger ());
4376 gcc::jit::recording::location *loc = ext_asm->get_loc ();
4377 RETURN_IF_FAIL (victim, ctxt, loc, "NULL victim");
4378 ext_asm->add_clobber (victim);
4381 /* Public entrypoint. See description in libgccjit.h.
4383 After error-checking, the real work is done by the
4384 gcc::jit::recording::context::add_top_level_asm, in
4385 jit-recording.cc. */
4387 void
4388 gcc_jit_context_add_top_level_asm (gcc_jit_context *ctxt,
4389 gcc_jit_location *loc,
4390 const char *asm_stmts)
4392 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
4393 JIT_LOG_FUNC (ctxt->get_logger ());
4394 /* LOC can be NULL. */
4395 RETURN_IF_FAIL (asm_stmts, ctxt, NULL, "NULL asm_stmts");
4396 ctxt->add_top_level_asm (loc, asm_stmts);