1 /* Implementation of the C API; all wrappers into the internal C++ API
2 Copyright (C) 2013-2015 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)
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/>. */
23 #include "coretypes.h"
25 #include "safe-ctype.h"
27 #include "libgccjit.h"
28 #include "jit-common.h"
29 #include "jit-logging.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
) :
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_field
: public gcc::jit::recording::field
67 struct gcc_jit_function
: public gcc::jit::recording::function
71 struct gcc_jit_block
: public gcc::jit::recording::block
75 struct gcc_jit_rvalue
: public gcc::jit::recording::rvalue
79 struct gcc_jit_lvalue
: public gcc::jit::recording::lvalue
83 struct gcc_jit_param
: public gcc::jit::recording::param
87 /**********************************************************************
90 We try to gracefully handle API usage errors by being defensive
92 **********************************************************************/
94 #define JIT_BEGIN_STMT do {
95 #define JIT_END_STMT } while(0)
97 /* Each of these error-handling macros determines if TEST_EXPR holds.
99 If TEXT_EXPR fails to hold we return from the enclosing function and
100 print an error, either via adding an error on the given context CTXT
101 if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
104 They have to be macros since they inject their "return" into the
105 function they are placed in.
107 The variant macros express:
109 (A) whether or not we need to return a value:
110 RETURN_VAL_IF_FAIL* vs
112 with the former returning RETURN_EXPR, and
114 for the common case where a NULL value is to be returned on
117 (B) whether the error message is to be directly printed:
119 or is a format string with some number of arguments:
120 RETURN_*IF_FAIL_PRINTF*
122 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
126 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
130 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
131 return (RETURN_EXPR); \
135 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
139 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
141 return (RETURN_EXPR); \
145 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
149 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
150 __func__, (A0), (A1)); \
151 return (RETURN_EXPR); \
155 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
159 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
160 __func__, (A0), (A1), (A2)); \
161 return (RETURN_EXPR); \
165 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
169 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
170 __func__, (A0), (A1), (A2), (A3)); \
171 return (RETURN_EXPR); \
175 #define RETURN_VAL_IF_FAIL_PRINTF5(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
179 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
180 __func__, (A0), (A1), (A2), (A3), (A4)); \
181 return (RETURN_EXPR); \
185 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
189 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
190 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
191 return (RETURN_EXPR); \
195 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
196 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
198 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
199 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
201 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
202 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
204 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
205 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
207 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
208 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
210 #define RETURN_NULL_IF_FAIL_PRINTF5(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
211 RETURN_VAL_IF_FAIL_PRINTF5 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4)
213 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
214 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
216 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
220 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
225 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
229 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
235 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
239 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
240 __func__, (A0), (A1)); \
245 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
249 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
250 __func__, (A0), (A1), (A2), (A3)); \
255 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
256 it. This will fail if BLOCK has already been terminated by some
257 kind of jump or a return. */
258 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
260 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
261 RETURN_IF_FAIL_PRINTF2 ( \
262 !(BLOCK)->has_been_terminated (), \
263 (BLOCK)->get_context (), \
265 "adding to terminated block: %s (already terminated by: %s)", \
266 (BLOCK)->get_debug_string (), \
267 (BLOCK)->get_last_statement ()->get_debug_string ()); \
270 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
272 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
274 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
275 RETURN_NULL_IF_FAIL_PRINTF2 ( \
276 !(BLOCK)->has_been_terminated (), \
277 (BLOCK)->get_context (), \
279 "adding to terminated block: %s (already terminated by: %s)", \
280 (BLOCK)->get_debug_string (), \
281 (BLOCK)->get_last_statement ()->get_debug_string ()); \
284 /* Format the given string, and report it as an error, either on CTXT
285 if non-NULL, or by printing to stderr if we have a NULL context.
286 LOC gives the source location where the error occcurred, and can be
290 jit_error (gcc::jit::recording::context
*ctxt
,
291 gcc_jit_location
*loc
,
292 const char *fmt
, ...)
296 jit_error (gcc::jit::recording::context
*ctxt
,
297 gcc_jit_location
*loc
,
298 const char *fmt
, ...)
304 ctxt
->add_error_va (loc
, fmt
, ap
);
307 /* No context? Send to stderr. */
308 vfprintf (stderr
, fmt
, ap
);
309 fprintf (stderr
, "\n");
315 /* Determine whether or not we can write to lvalues of type LTYPE from
316 rvalues of type RTYPE, detecting type errors such as attempting to
317 write to an int with a string literal (without an explicit cast).
319 This is implemented by calling the
320 gcc::jit::recording::type::accepts_writes_from virtual function on
324 compatible_types (gcc::jit::recording::type
*ltype
,
325 gcc::jit::recording::type
*rtype
)
327 return ltype
->accepts_writes_from (rtype
);
330 /* Public entrypoint for acquiring a gcc_jit_context.
331 Note that this creates a new top-level context; contrast with
332 gcc_jit_context_new_child_context below.
334 The real work is done in the constructor for
335 gcc::jit::recording::context in jit-recording.c. */
338 gcc_jit_context_acquire (void)
340 gcc_jit_context
*ctxt
= new gcc_jit_context (NULL
);
341 ctxt
->log ("new top-level ctxt: %p", (void *)ctxt
);
345 /* Public entrypoint for releasing a gcc_jit_context.
346 The real work is done in the destructor for
347 gcc::jit::recording::context in jit-recording.c. */
350 gcc_jit_context_release (gcc_jit_context
*ctxt
)
352 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL ctxt");
353 JIT_LOG_FUNC (ctxt
->get_logger ());
354 ctxt
->log ("deleting ctxt: %p", (void *)ctxt
);
358 /* Public entrypoint for creating a child context within
359 PARENT_CTXT. See description in libgccjit.h.
361 The real work is done in the constructor for
362 gcc::jit::recording::context in jit-recording.c. */
365 gcc_jit_context_new_child_context (gcc_jit_context
*parent_ctxt
)
367 RETURN_NULL_IF_FAIL (parent_ctxt
, NULL
, NULL
, "NULL parent ctxt");
368 JIT_LOG_FUNC (parent_ctxt
->get_logger ());
369 parent_ctxt
->log ("parent_ctxt: %p", (void *)parent_ctxt
);
370 gcc_jit_context
*child_ctxt
= new gcc_jit_context (parent_ctxt
);
371 child_ctxt
->log ("new child_ctxt: %p", (void *)child_ctxt
);
375 /* Public entrypoint. See description in libgccjit.h.
377 After error-checking, the real work is done by the
378 gcc::jit::recording::context::new_location
379 method in jit-recording.c. */
382 gcc_jit_context_new_location (gcc_jit_context
*ctxt
,
383 const char *filename
,
387 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
388 JIT_LOG_FUNC (ctxt
->get_logger ());
389 return (gcc_jit_location
*)ctxt
->new_location (filename
, line
, column
, true);
392 /* Public entrypoint. See description in libgccjit.h.
394 After error-checking, this calls the trivial
395 gcc::jit::recording::memento::as_object method (a location is a
396 memento), in jit-recording.h. */
399 gcc_jit_location_as_object (gcc_jit_location
*loc
)
401 RETURN_NULL_IF_FAIL (loc
, NULL
, NULL
, "NULL location");
403 return static_cast <gcc_jit_object
*> (loc
->as_object ());
406 /* Public entrypoint. See description in libgccjit.h.
408 After error-checking, this calls the trivial
409 gcc::jit::recording::memento::as_object method (a type is a
410 memento), in jit-recording.h. */
413 gcc_jit_type_as_object (gcc_jit_type
*type
)
415 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
417 return static_cast <gcc_jit_object
*> (type
->as_object ());
420 /* Public entrypoint for getting a specific type from a context.
422 After error-checking, the real work is done by the
423 gcc::jit::recording::context::get_type method, in
427 gcc_jit_context_get_type (gcc_jit_context
*ctxt
,
428 enum gcc_jit_types type
)
430 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
431 JIT_LOG_FUNC (ctxt
->get_logger ());
432 RETURN_NULL_IF_FAIL_PRINTF1 (
433 (type
>= GCC_JIT_TYPE_VOID
434 && type
<= GCC_JIT_TYPE_FILE_PTR
),
436 "unrecognized value for enum gcc_jit_types: %i", type
);
438 return (gcc_jit_type
*)ctxt
->get_type (type
);
441 /* Public entrypoint for getting the integer type of the given size and
444 After error-checking, the real work is done by the
445 gcc::jit::recording::context::get_int_type method,
446 in jit-recording.c. */
449 gcc_jit_context_get_int_type (gcc_jit_context
*ctxt
,
450 int num_bytes
, int is_signed
)
452 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
453 JIT_LOG_FUNC (ctxt
->get_logger ());
454 RETURN_NULL_IF_FAIL (num_bytes
>= 0, ctxt
, NULL
, "negative size");
456 return (gcc_jit_type
*)ctxt
->get_int_type (num_bytes
, is_signed
);
459 /* Public entrypoint. See description in libgccjit.h.
461 After error-checking, the real work is done by the
462 gcc::jit::recording::type::get_pointer method, in
466 gcc_jit_type_get_pointer (gcc_jit_type
*type
)
468 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
470 return (gcc_jit_type
*)type
->get_pointer ();
473 /* Public entrypoint. See description in libgccjit.h.
475 After error-checking, the real work is done by the
476 gcc::jit::recording::type::get_const method, in
480 gcc_jit_type_get_const (gcc_jit_type
*type
)
482 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
484 return (gcc_jit_type
*)type
->get_const ();
487 /* Public entrypoint. See description in libgccjit.h.
489 After error-checking, the real work is done by the
490 gcc::jit::recording::type::get_volatile method, in
494 gcc_jit_type_get_volatile (gcc_jit_type
*type
)
496 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
498 return (gcc_jit_type
*)type
->get_volatile ();
501 /* Public entrypoint. See description in libgccjit.h.
503 After error-checking, the real work is done by the
504 gcc::jit::recording::context::new_array_type method, in
508 gcc_jit_context_new_array_type (gcc_jit_context
*ctxt
,
509 gcc_jit_location
*loc
,
510 gcc_jit_type
*element_type
,
513 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
514 JIT_LOG_FUNC (ctxt
->get_logger ());
515 /* LOC can be NULL. */
516 RETURN_NULL_IF_FAIL (element_type
, ctxt
, loc
, "NULL type");
517 RETURN_NULL_IF_FAIL (num_elements
>= 0, ctxt
, NULL
, "negative size");
519 return (gcc_jit_type
*)ctxt
->new_array_type (loc
,
524 /* Public entrypoint. See description in libgccjit.h.
526 After error-checking, the real work is done by the
527 gcc::jit::recording::context::new_field method, in
531 gcc_jit_context_new_field (gcc_jit_context
*ctxt
,
532 gcc_jit_location
*loc
,
536 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
537 JIT_LOG_FUNC (ctxt
->get_logger ());
538 /* LOC can be NULL. */
539 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
540 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
542 return (gcc_jit_field
*)ctxt
->new_field (loc
, type
, name
);
545 /* Public entrypoint. See description in libgccjit.h.
547 After error-checking, this calls the trivial
548 gcc::jit::recording::memento::as_object method (a field is a
549 memento), in jit-recording.h. */
552 gcc_jit_field_as_object (gcc_jit_field
*field
)
554 RETURN_NULL_IF_FAIL (field
, NULL
, NULL
, "NULL field");
556 return static_cast <gcc_jit_object
*> (field
->as_object ());
559 /* Public entrypoint. See description in libgccjit.h.
561 After error-checking, the real work is done by the
562 gcc::jit::recording::context::new_struct_type method,
563 immediately followed by a "set_fields" call on the resulting
564 gcc::jit::recording::compound_type *, both in jit-recording.c */
567 gcc_jit_context_new_struct_type (gcc_jit_context
*ctxt
,
568 gcc_jit_location
*loc
,
571 gcc_jit_field
**fields
)
573 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
574 JIT_LOG_FUNC (ctxt
->get_logger ());
575 /* LOC can be NULL. */
576 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
578 RETURN_NULL_IF_FAIL (fields
, ctxt
, loc
, "NULL fields ptr");
579 for (int i
= 0; i
< num_fields
; i
++)
581 RETURN_NULL_IF_FAIL (fields
[i
], ctxt
, loc
, "NULL field ptr");
582 RETURN_NULL_IF_FAIL_PRINTF2 (
583 NULL
== fields
[i
]->get_container (),
585 "%s is already a field of %s",
586 fields
[i
]->get_debug_string (),
587 fields
[i
]->get_container ()->get_debug_string ());
590 gcc::jit::recording::struct_
*result
=
591 ctxt
->new_struct_type (loc
, name
);
592 result
->set_fields (loc
,
594 (gcc::jit::recording::field
**)fields
);
595 return static_cast<gcc_jit_struct
*> (result
);
598 /* Public entrypoint. See description in libgccjit.h.
600 After error-checking, the real work is done by the
601 gcc::jit::recording::context::new_struct_type method in
605 gcc_jit_context_new_opaque_struct (gcc_jit_context
*ctxt
,
606 gcc_jit_location
*loc
,
609 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
610 JIT_LOG_FUNC (ctxt
->get_logger ());
611 /* LOC can be NULL. */
612 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
614 return (gcc_jit_struct
*)ctxt
->new_struct_type (loc
, name
);
617 /* Public entrypoint. See description in libgccjit.h.
619 After error-checking, this calls the trivial
620 gcc::jit::recording::struct_::as_object method in
624 gcc_jit_struct_as_type (gcc_jit_struct
*struct_type
)
626 RETURN_NULL_IF_FAIL (struct_type
, NULL
, NULL
, "NULL struct_type");
628 return static_cast <gcc_jit_type
*> (struct_type
->as_type ());
631 /* Public entrypoint. See description in libgccjit.h.
633 After error-checking, the real work is done by the
634 gcc::jit::recording::compound_type::set_fields method in
638 gcc_jit_struct_set_fields (gcc_jit_struct
*struct_type
,
639 gcc_jit_location
*loc
,
641 gcc_jit_field
**fields
)
643 RETURN_IF_FAIL (struct_type
, NULL
, loc
, "NULL struct_type");
644 gcc::jit::recording::context
*ctxt
= struct_type
->m_ctxt
;
645 JIT_LOG_FUNC (ctxt
->get_logger ());
646 /* LOC can be NULL. */
647 RETURN_IF_FAIL_PRINTF1 (
648 NULL
== struct_type
->get_fields (), ctxt
, loc
,
649 "%s already has had fields set",
650 struct_type
->get_debug_string ());
652 RETURN_IF_FAIL (fields
, ctxt
, loc
, "NULL fields ptr");
653 for (int i
= 0; i
< num_fields
; i
++)
655 RETURN_IF_FAIL (fields
[i
], ctxt
, loc
, "NULL field ptr");
656 RETURN_IF_FAIL_PRINTF2 (
657 NULL
== fields
[i
]->get_container (),
659 "%s is already a field of %s",
660 fields
[i
]->get_debug_string (),
661 fields
[i
]->get_container ()->get_debug_string ());
664 struct_type
->set_fields (loc
, num_fields
,
665 (gcc::jit::recording::field
**)fields
);
668 /* Public entrypoint. See description in libgccjit.h.
670 After error-checking, the real work is done by the
671 gcc::jit::recording::context::new_union_type method,
672 immediately followed by a "set_fields" call on the resulting
673 gcc::jit::recording::compound_type *, both in jit-recording.c */
676 gcc_jit_context_new_union_type (gcc_jit_context
*ctxt
,
677 gcc_jit_location
*loc
,
680 gcc_jit_field
**fields
)
682 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
683 JIT_LOG_FUNC (ctxt
->get_logger ());
684 /* LOC can be NULL. */
685 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
687 RETURN_NULL_IF_FAIL (fields
, ctxt
, loc
, "NULL fields ptr");
688 for (int i
= 0; i
< num_fields
; i
++)
690 RETURN_NULL_IF_FAIL (fields
[i
], ctxt
, loc
, "NULL field ptr");
691 RETURN_NULL_IF_FAIL_PRINTF2 (
692 NULL
== fields
[i
]->get_container (),
694 "%s is already a field of %s",
695 fields
[i
]->get_debug_string (),
696 fields
[i
]->get_container ()->get_debug_string ());
699 gcc::jit::recording::union_
*result
=
700 ctxt
->new_union_type (loc
, name
);
701 result
->set_fields (loc
,
703 (gcc::jit::recording::field
**)fields
);
704 return (gcc_jit_type
*) (result
);
707 /* Public entrypoint. See description in libgccjit.h.
709 After error-checking, the real work is done by the
710 gcc::jit::recording::context::new_function_ptr_type method,
711 in jit-recording.c */
714 gcc_jit_context_new_function_ptr_type (gcc_jit_context
*ctxt
,
715 gcc_jit_location
*loc
,
716 gcc_jit_type
*return_type
,
718 gcc_jit_type
**param_types
,
721 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
722 JIT_LOG_FUNC (ctxt
->get_logger ());
723 /* LOC can be NULL. */
724 RETURN_NULL_IF_FAIL (return_type
, ctxt
, loc
, "NULL return_type");
725 RETURN_NULL_IF_FAIL (
726 (num_params
== 0) || param_types
,
728 "NULL param_types creating function pointer type");
729 for (int i
= 0; i
< num_params
; i
++)
730 RETURN_NULL_IF_FAIL_PRINTF1 (
733 "NULL parameter type %i creating function pointer type", i
);
735 return (gcc_jit_type
*)
736 ctxt
->new_function_ptr_type (loc
, return_type
,
738 (gcc::jit::recording::type
**)param_types
,
742 /* Constructing functions. */
744 /* Public entrypoint. See description in libgccjit.h.
746 After error-checking, the real work is done by the
747 gcc::jit::recording::context::new_param method, in jit-recording.c */
750 gcc_jit_context_new_param (gcc_jit_context
*ctxt
,
751 gcc_jit_location
*loc
,
755 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
756 JIT_LOG_FUNC (ctxt
->get_logger ());
757 /* LOC can be NULL. */
758 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
759 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
761 return (gcc_jit_param
*)ctxt
->new_param (loc
, type
, name
);
764 /* Public entrypoint. See description in libgccjit.h.
766 After error-checking, this calls the trivial
767 gcc::jit::recording::memento::as_object method (a param is a memento),
768 in jit-recording.h. */
771 gcc_jit_param_as_object (gcc_jit_param
*param
)
773 RETURN_NULL_IF_FAIL (param
, NULL
, NULL
, "NULL param");
775 return static_cast <gcc_jit_object
*> (param
->as_object ());
778 /* Public entrypoint. See description in libgccjit.h.
780 After error-checking, this calls the trivial
781 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
784 gcc_jit_param_as_lvalue (gcc_jit_param
*param
)
786 RETURN_NULL_IF_FAIL (param
, NULL
, NULL
, "NULL param");
788 return (gcc_jit_lvalue
*)param
->as_lvalue ();
791 /* Public entrypoint. See description in libgccjit.h.
793 After error-checking, this calls the trivial
794 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
795 in jit-recording.h. */
798 gcc_jit_param_as_rvalue (gcc_jit_param
*param
)
800 RETURN_NULL_IF_FAIL (param
, NULL
, NULL
, "NULL param");
802 return (gcc_jit_rvalue
*)param
->as_rvalue ();
805 /* Public entrypoint. See description in libgccjit.h.
807 After error-checking, the real work is done by the
808 gcc::jit::recording::context::new_function method, in
812 gcc_jit_context_new_function (gcc_jit_context
*ctxt
,
813 gcc_jit_location
*loc
,
814 enum gcc_jit_function_kind kind
,
815 gcc_jit_type
*return_type
,
818 gcc_jit_param
**params
,
821 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
822 JIT_LOG_FUNC (ctxt
->get_logger ());
823 /* LOC can be NULL. */
824 RETURN_NULL_IF_FAIL_PRINTF1 (
825 ((kind
>= GCC_JIT_FUNCTION_EXPORTED
)
826 && (kind
<= GCC_JIT_FUNCTION_ALWAYS_INLINE
)),
828 "unrecognized value for enum gcc_jit_function_kind: %i",
830 RETURN_NULL_IF_FAIL (return_type
, ctxt
, loc
, "NULL return_type");
831 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
832 /* The assembler can only handle certain names, so for now, enforce
833 C's rules for identiers upon the name, using ISALPHA and ISALNUM
834 from safe-ctype.h to ignore the current locale.
835 Eventually we'll need some way to interact with e.g. C++ name
840 RETURN_NULL_IF_FAIL_PRINTF2 (
841 ISALPHA (ch
) || ch
== '_',
843 "name \"%s\" contains invalid character: '%c'",
845 /* Subsequent chars: */
846 for (const char *ptr
= name
+ 1; (ch
= *ptr
); ptr
++)
848 RETURN_NULL_IF_FAIL_PRINTF2 (
849 ISALNUM (ch
) || ch
== '_',
851 "name \"%s\" contains invalid character: '%c'",
855 RETURN_NULL_IF_FAIL_PRINTF1 (
856 (num_params
== 0) || params
,
858 "NULL params creating function %s", name
);
859 for (int i
= 0; i
< num_params
; i
++)
861 RETURN_NULL_IF_FAIL_PRINTF2 (
864 "NULL parameter %i creating function %s", i
, name
);
865 RETURN_NULL_IF_FAIL_PRINTF5 (
866 (NULL
== params
[i
]->get_scope ()),
868 "parameter %i \"%s\""
871 " was already used for function %s",
872 i
, params
[i
]->get_debug_string (),
873 params
[i
]->get_type ()->get_debug_string (),
875 params
[i
]->get_scope ()->get_debug_string ());
878 return (gcc_jit_function
*)
879 ctxt
->new_function (loc
, kind
, return_type
, name
,
881 (gcc::jit::recording::param
**)params
,
886 /* Public entrypoint. See description in libgccjit.h.
888 After error-checking, the real work is done by the
889 gcc::jit::recording::context::get_builtin_function method, in
893 gcc_jit_context_get_builtin_function (gcc_jit_context
*ctxt
,
896 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
897 JIT_LOG_FUNC (ctxt
->get_logger ());
898 RETURN_NULL_IF_FAIL (name
, ctxt
, NULL
, "NULL name");
900 return static_cast <gcc_jit_function
*> (ctxt
->get_builtin_function (name
));
903 /* Public entrypoint. See description in libgccjit.h.
905 After error-checking, this calls the trivial
906 gcc::jit::recording::memento::as_object method (a function is a
907 memento), in jit-recording.h. */
910 gcc_jit_function_as_object (gcc_jit_function
*func
)
912 RETURN_NULL_IF_FAIL (func
, NULL
, NULL
, "NULL function");
914 return static_cast <gcc_jit_object
*> (func
->as_object ());
917 /* Public entrypoint. See description in libgccjit.h.
919 After error-checking, the real work is done by the
920 gcc::jit::recording::function::get_param method, in
924 gcc_jit_function_get_param (gcc_jit_function
*func
, int index
)
926 RETURN_NULL_IF_FAIL (func
, NULL
, NULL
, "NULL function");
927 gcc::jit::recording::context
*ctxt
= func
->m_ctxt
;
928 JIT_LOG_FUNC (ctxt
->get_logger ());
929 RETURN_NULL_IF_FAIL (index
>= 0, ctxt
, NULL
, "negative index");
930 int num_params
= func
->get_params ().length ();
931 RETURN_NULL_IF_FAIL_PRINTF3 (index
< num_params
,
933 "index of %d is too large (%s has %d params)",
935 func
->get_debug_string (),
938 return static_cast <gcc_jit_param
*> (func
->get_param (index
));
941 /* Public entrypoint. See description in libgccjit.h.
943 After error-checking, the real work is done by the
944 gcc::jit::recording::function::dump_to_dot method, in
948 gcc_jit_function_dump_to_dot (gcc_jit_function
*func
,
951 RETURN_IF_FAIL (func
, NULL
, NULL
, "NULL function");
952 gcc::jit::recording::context
*ctxt
= func
->m_ctxt
;
953 JIT_LOG_FUNC (ctxt
->get_logger ());
954 RETURN_IF_FAIL (path
, ctxt
, NULL
, "NULL path");
956 func
->dump_to_dot (path
);
959 /* Public entrypoint. See description in libgccjit.h.
961 After error-checking, the real work is done by the
962 gcc::jit::recording::function::new_block method, in
966 gcc_jit_function_new_block (gcc_jit_function
*func
,
969 RETURN_NULL_IF_FAIL (func
, NULL
, NULL
, "NULL function");
970 JIT_LOG_FUNC (func
->get_context ()->get_logger ());
971 RETURN_NULL_IF_FAIL (func
->get_kind () != GCC_JIT_FUNCTION_IMPORTED
,
972 func
->get_context (), NULL
,
973 "cannot add block to an imported function");
974 /* name can be NULL. */
976 return (gcc_jit_block
*)func
->new_block (name
);
979 /* Public entrypoint. See description in libgccjit.h.
981 After error-checking, this calls the trivial
982 gcc::jit::recording::memento::as_object method (a block is a
983 memento), in jit-recording.h. */
986 gcc_jit_block_as_object (gcc_jit_block
*block
)
988 RETURN_NULL_IF_FAIL (block
, NULL
, NULL
, "NULL block");
990 return static_cast <gcc_jit_object
*> (block
->as_object ());
993 /* Public entrypoint. See description in libgccjit.h.
995 After error-checking, the real work is done by the
996 gcc::jit::recording::block::get_function method, in
1000 gcc_jit_block_get_function (gcc_jit_block
*block
)
1002 RETURN_NULL_IF_FAIL (block
, NULL
, NULL
, "NULL block");
1004 return static_cast <gcc_jit_function
*> (block
->get_function ());
1007 /* Public entrypoint. See description in libgccjit.h.
1009 After error-checking, the real work is done by the
1010 gcc::jit::recording::context::new_global method, in
1014 gcc_jit_context_new_global (gcc_jit_context
*ctxt
,
1015 gcc_jit_location
*loc
,
1016 enum gcc_jit_global_kind kind
,
1020 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1021 JIT_LOG_FUNC (ctxt
->get_logger ());
1022 /* LOC can be NULL. */
1023 RETURN_NULL_IF_FAIL_PRINTF1 (
1024 ((kind
>= GCC_JIT_GLOBAL_EXPORTED
)
1025 && (kind
<= GCC_JIT_GLOBAL_IMPORTED
)),
1027 "unrecognized value for enum gcc_jit_global_kind: %i",
1029 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
1030 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
1032 return (gcc_jit_lvalue
*)ctxt
->new_global (loc
, kind
, type
, name
);
1035 /* Public entrypoint. See description in libgccjit.h.
1037 After error-checking, this calls the trivial
1038 gcc::jit::recording::memento::as_object method (an lvalue is a
1039 memento), in jit-recording.h. */
1042 gcc_jit_lvalue_as_object (gcc_jit_lvalue
*lvalue
)
1044 RETURN_NULL_IF_FAIL (lvalue
, NULL
, NULL
, "NULL lvalue");
1046 return static_cast <gcc_jit_object
*> (lvalue
->as_object ());
1049 /* Public entrypoint. See description in libgccjit.h.
1051 After error-checking, this calls the trivial
1052 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1055 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue
*lvalue
)
1057 RETURN_NULL_IF_FAIL (lvalue
, NULL
, NULL
, "NULL lvalue");
1059 return (gcc_jit_rvalue
*)lvalue
->as_rvalue ();
1062 /* Public entrypoint. See description in libgccjit.h.
1064 After error-checking, this calls the trivial
1065 gcc::jit::recording::memento::as_object method (an rvalue is a
1066 memento), in jit-recording.h. */
1069 gcc_jit_rvalue_as_object (gcc_jit_rvalue
*rvalue
)
1071 RETURN_NULL_IF_FAIL (rvalue
, NULL
, NULL
, "NULL rvalue");
1073 return static_cast <gcc_jit_object
*> (rvalue
->as_object ());
1076 /* Public entrypoint. See description in libgccjit.h.
1078 After error-checking, the real work is done by the
1079 gcc::jit::recording::rvalue::get_type method, in
1083 gcc_jit_rvalue_get_type (gcc_jit_rvalue
*rvalue
)
1085 RETURN_NULL_IF_FAIL (rvalue
, NULL
, NULL
, "NULL rvalue");
1087 return static_cast <gcc_jit_type
*> (rvalue
->get_type ());
1090 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1091 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1092 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1094 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1095 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1096 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1097 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1098 "not a numeric type: %s", \
1099 NUMERIC_TYPE->get_debug_string ());
1101 /* Public entrypoint. See description in libgccjit.h.
1103 After error-checking, the real work is done by the
1104 gcc::jit::recording::context::new_rvalue_from_int method in
1108 gcc_jit_context_new_rvalue_from_int (gcc_jit_context
*ctxt
,
1109 gcc_jit_type
*numeric_type
,
1112 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1113 JIT_LOG_FUNC (ctxt
->get_logger ());
1114 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1116 return ((gcc_jit_rvalue
*)ctxt
1117 ->new_rvalue_from_const
<int> (numeric_type
, value
));
1123 gcc_jit_context_new_rvalue_from_long (gcc_jit_context
*ctxt
,
1124 gcc_jit_type
*numeric_type
,
1127 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1128 JIT_LOG_FUNC (ctxt
->get_logger ());
1129 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1131 return ((gcc_jit_rvalue
*)ctxt
1132 ->new_rvalue_from_const
<long> (numeric_type
, value
));
1135 /* Public entrypoint. See description in libgccjit.h.
1137 This is essentially equivalent to:
1138 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1139 albeit with slightly different error messages if an error occurs. */
1142 gcc_jit_context_zero (gcc_jit_context
*ctxt
,
1143 gcc_jit_type
*numeric_type
)
1145 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1146 JIT_LOG_FUNC (ctxt
->get_logger ());
1147 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1149 return gcc_jit_context_new_rvalue_from_int (ctxt
, numeric_type
, 0);
1152 /* Public entrypoint. See description in libgccjit.h.
1154 This is essentially equivalent to:
1155 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1156 albeit with slightly different error messages if an error occurs. */
1159 gcc_jit_context_one (gcc_jit_context
*ctxt
,
1160 gcc_jit_type
*numeric_type
)
1162 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1163 JIT_LOG_FUNC (ctxt
->get_logger ());
1164 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1166 return gcc_jit_context_new_rvalue_from_int (ctxt
, numeric_type
, 1);
1169 /* Public entrypoint. See description in libgccjit.h.
1171 After error-checking, the real work is done by the
1172 gcc::jit::recording::context::new_rvalue_from_double method in
1176 gcc_jit_context_new_rvalue_from_double (gcc_jit_context
*ctxt
,
1177 gcc_jit_type
*numeric_type
,
1180 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1181 JIT_LOG_FUNC (ctxt
->get_logger ());
1182 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1184 return ((gcc_jit_rvalue
*)ctxt
1185 ->new_rvalue_from_const
<double> (numeric_type
, value
));
1188 /* Public entrypoint. See description in libgccjit.h.
1190 After error-checking, the real work is done by the
1191 gcc::jit::recording::context::new_rvalue_from_ptr method in
1195 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context
*ctxt
,
1196 gcc_jit_type
*pointer_type
,
1199 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1200 JIT_LOG_FUNC (ctxt
->get_logger ());
1201 RETURN_NULL_IF_FAIL (pointer_type
, ctxt
, NULL
, "NULL type");
1202 RETURN_NULL_IF_FAIL_PRINTF1 (
1203 pointer_type
->is_pointer (),
1205 "not a pointer type (type: %s)",
1206 pointer_type
->get_debug_string ());
1208 return ((gcc_jit_rvalue
*)ctxt
1209 ->new_rvalue_from_const
<void *> (pointer_type
, value
));
1212 /* Public entrypoint. See description in libgccjit.h.
1214 This is essentially equivalent to:
1215 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1216 albeit with slightly different error messages if an error occurs. */
1219 gcc_jit_context_null (gcc_jit_context
*ctxt
,
1220 gcc_jit_type
*pointer_type
)
1222 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1223 JIT_LOG_FUNC (ctxt
->get_logger ());
1224 RETURN_NULL_IF_FAIL (pointer_type
, ctxt
, NULL
, "NULL type");
1225 RETURN_NULL_IF_FAIL_PRINTF1 (
1226 pointer_type
->is_pointer (),
1228 "not a pointer type (type: %s)",
1229 pointer_type
->get_debug_string ());
1231 return gcc_jit_context_new_rvalue_from_ptr (ctxt
, pointer_type
, NULL
);
1234 /* Public entrypoint. See description in libgccjit.h.
1236 After error-checking, the real work is done by the
1237 gcc::jit::recording::context::new_string_literal method in
1241 gcc_jit_context_new_string_literal (gcc_jit_context
*ctxt
,
1244 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1245 JIT_LOG_FUNC (ctxt
->get_logger ());
1246 RETURN_NULL_IF_FAIL (value
, ctxt
, NULL
, "NULL value");
1248 return (gcc_jit_rvalue
*)ctxt
->new_string_literal (value
);
1251 /* Public entrypoint. See description in libgccjit.h.
1253 After error-checking, the real work is done by the
1254 gcc::jit::recording::context::new_unary_op method in
1258 gcc_jit_context_new_unary_op (gcc_jit_context
*ctxt
,
1259 gcc_jit_location
*loc
,
1260 enum gcc_jit_unary_op op
,
1261 gcc_jit_type
*result_type
,
1262 gcc_jit_rvalue
*rvalue
)
1264 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1265 JIT_LOG_FUNC (ctxt
->get_logger ());
1266 /* LOC can be NULL. */
1267 RETURN_NULL_IF_FAIL_PRINTF1 (
1268 (op
>= GCC_JIT_UNARY_OP_MINUS
1269 && op
<= GCC_JIT_UNARY_OP_ABS
),
1271 "unrecognized value for enum gcc_jit_unary_op: %i",
1273 RETURN_NULL_IF_FAIL (result_type
, ctxt
, loc
, "NULL result_type");
1274 RETURN_NULL_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1276 return (gcc_jit_rvalue
*)ctxt
->new_unary_op (loc
, op
, result_type
, rvalue
);
1279 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1280 For use by both gcc_jit_context_new_binary_op and
1281 gcc_jit_block_add_assignment_op. */
1284 valid_binary_op_p (enum gcc_jit_binary_op op
)
1286 return (op
>= GCC_JIT_BINARY_OP_PLUS
1287 && op
<= GCC_JIT_BINARY_OP_RSHIFT
);
1290 /* Public entrypoint. See description in libgccjit.h.
1292 After error-checking, the real work is done by the
1293 gcc::jit::recording::context::new_binary_op method in
1297 gcc_jit_context_new_binary_op (gcc_jit_context
*ctxt
,
1298 gcc_jit_location
*loc
,
1299 enum gcc_jit_binary_op op
,
1300 gcc_jit_type
*result_type
,
1301 gcc_jit_rvalue
*a
, gcc_jit_rvalue
*b
)
1303 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1304 JIT_LOG_FUNC (ctxt
->get_logger ());
1305 /* LOC can be NULL. */
1306 RETURN_NULL_IF_FAIL_PRINTF1 (
1307 valid_binary_op_p (op
),
1309 "unrecognized value for enum gcc_jit_binary_op: %i",
1311 RETURN_NULL_IF_FAIL (result_type
, ctxt
, loc
, "NULL result_type");
1312 RETURN_NULL_IF_FAIL (a
, ctxt
, loc
, "NULL a");
1313 RETURN_NULL_IF_FAIL (b
, ctxt
, loc
, "NULL b");
1314 RETURN_NULL_IF_FAIL_PRINTF4 (
1315 a
->get_type () == b
->get_type (),
1317 "mismatching types for binary op:"
1318 " a: %s (type: %s) b: %s (type: %s)",
1319 a
->get_debug_string (),
1320 a
->get_type ()->get_debug_string (),
1321 b
->get_debug_string (),
1322 b
->get_type ()->get_debug_string ());
1324 return (gcc_jit_rvalue
*)ctxt
->new_binary_op (loc
, op
, result_type
, a
, b
);
1327 /* Public entrypoint. See description in libgccjit.h.
1329 After error-checking, the real work is done by the
1330 gcc::jit::recording::context::new_comparison method in
1334 gcc_jit_context_new_comparison (gcc_jit_context
*ctxt
,
1335 gcc_jit_location
*loc
,
1336 enum gcc_jit_comparison op
,
1337 gcc_jit_rvalue
*a
, gcc_jit_rvalue
*b
)
1339 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1340 JIT_LOG_FUNC (ctxt
->get_logger ());
1341 /* LOC can be NULL. */
1342 RETURN_NULL_IF_FAIL_PRINTF1 (
1343 (op
>= GCC_JIT_COMPARISON_EQ
1344 && op
<= GCC_JIT_COMPARISON_GE
),
1346 "unrecognized value for enum gcc_jit_comparison: %i",
1348 RETURN_NULL_IF_FAIL (a
, ctxt
, loc
, "NULL a");
1349 RETURN_NULL_IF_FAIL (b
, ctxt
, loc
, "NULL b");
1350 RETURN_NULL_IF_FAIL_PRINTF4 (
1351 a
->get_type ()->unqualified () == b
->get_type ()->unqualified (),
1353 "mismatching types for comparison:"
1354 " a: %s (type: %s) b: %s (type: %s)",
1355 a
->get_debug_string (),
1356 a
->get_type ()->get_debug_string (),
1357 b
->get_debug_string (),
1358 b
->get_type ()->get_debug_string ());
1360 return (gcc_jit_rvalue
*)ctxt
->new_comparison (loc
, op
, a
, b
);
1363 /* Public entrypoint. See description in libgccjit.h.
1365 After error-checking, the real work is done by the
1366 gcc::jit::recording::context::new_call method in
1370 gcc_jit_context_new_call (gcc_jit_context
*ctxt
,
1371 gcc_jit_location
*loc
,
1372 gcc_jit_function
*func
,
1373 int numargs
, gcc_jit_rvalue
**args
)
1375 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1376 JIT_LOG_FUNC (ctxt
->get_logger ());
1377 /* LOC can be NULL. */
1378 RETURN_NULL_IF_FAIL (func
, ctxt
, loc
, "NULL function");
1380 RETURN_NULL_IF_FAIL (args
, ctxt
, loc
, "NULL args");
1382 int min_num_params
= func
->get_params ().length ();
1383 bool is_variadic
= func
->is_variadic ();
1385 RETURN_NULL_IF_FAIL_PRINTF3 (
1386 numargs
>= min_num_params
,
1388 "not enough arguments to function \"%s\""
1389 " (got %i args, expected %i)",
1390 func
->get_name ()->c_str (),
1391 numargs
, min_num_params
);
1393 RETURN_NULL_IF_FAIL_PRINTF3 (
1394 (numargs
== min_num_params
|| is_variadic
),
1396 "too many arguments to function \"%s\""
1397 " (got %i args, expected %i)",
1398 func
->get_name ()->c_str (),
1399 numargs
, min_num_params
);
1401 for (int i
= 0; i
< min_num_params
; i
++)
1403 gcc::jit::recording::param
*param
= func
->get_param (i
);
1404 gcc_jit_rvalue
*arg
= args
[i
];
1406 RETURN_NULL_IF_FAIL_PRINTF4 (
1409 "NULL argument %i to function \"%s\":"
1410 " param %s (type: %s)",
1412 func
->get_name ()->c_str (),
1413 param
->get_debug_string (),
1414 param
->get_type ()->get_debug_string ());
1416 RETURN_NULL_IF_FAIL_PRINTF6 (
1417 compatible_types (param
->get_type (),
1420 "mismatching types for argument %d of function \"%s\":"
1421 " assignment to param %s (type: %s) from %s (type: %s)",
1423 func
->get_name ()->c_str (),
1424 param
->get_debug_string (),
1425 param
->get_type ()->get_debug_string (),
1426 arg
->get_debug_string (),
1427 arg
->get_type ()->get_debug_string ());
1430 return (gcc_jit_rvalue
*)ctxt
->new_call (loc
,
1433 (gcc::jit::recording::rvalue
**)args
);
1436 /* Public entrypoint. See description in libgccjit.h.
1438 After error-checking, the real work is done by the
1439 gcc::jit::recording::context::new_call_through_ptr method in
1443 gcc_jit_context_new_call_through_ptr (gcc_jit_context
*ctxt
,
1444 gcc_jit_location
*loc
,
1445 gcc_jit_rvalue
*fn_ptr
,
1446 int numargs
, gcc_jit_rvalue
**args
)
1448 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1449 JIT_LOG_FUNC (ctxt
->get_logger ());
1450 /* LOC can be NULL. */
1451 RETURN_NULL_IF_FAIL (fn_ptr
, ctxt
, loc
, "NULL fn_ptr");
1453 RETURN_NULL_IF_FAIL (args
, ctxt
, loc
, "NULL args");
1455 gcc::jit::recording::type
*ptr_type
= fn_ptr
->get_type ()->dereference ();
1456 RETURN_NULL_IF_FAIL_PRINTF2 (
1457 ptr_type
, ctxt
, loc
,
1458 "fn_ptr is not a ptr: %s"
1460 fn_ptr
->get_debug_string (),
1461 fn_ptr
->get_type ()->get_debug_string ());
1463 gcc::jit::recording::function_type
*fn_type
=
1464 ptr_type
->dyn_cast_function_type();
1465 RETURN_NULL_IF_FAIL_PRINTF2 (
1467 "fn_ptr is not a function ptr: %s"
1469 fn_ptr
->get_debug_string (),
1470 fn_ptr
->get_type ()->get_debug_string ());
1472 int min_num_params
= fn_type
->get_param_types ().length ();
1473 bool is_variadic
= fn_type
->is_variadic ();
1475 RETURN_NULL_IF_FAIL_PRINTF3 (
1476 numargs
>= min_num_params
,
1478 "not enough arguments to fn_ptr: %s"
1479 " (got %i args, expected %i)",
1480 fn_ptr
->get_debug_string (),
1481 numargs
, min_num_params
);
1483 RETURN_NULL_IF_FAIL_PRINTF3 (
1484 (numargs
== min_num_params
|| is_variadic
),
1486 "too many arguments to fn_ptr: %s"
1487 " (got %i args, expected %i)",
1488 fn_ptr
->get_debug_string (),
1489 numargs
, min_num_params
);
1491 for (int i
= 0; i
< min_num_params
; i
++)
1493 gcc::jit::recording::type
*param_type
= fn_type
->get_param_types ()[i
];
1494 gcc_jit_rvalue
*arg
= args
[i
];
1496 RETURN_NULL_IF_FAIL_PRINTF3 (
1499 "NULL argument %i to fn_ptr: %s"
1502 fn_ptr
->get_debug_string (),
1503 param_type
->get_debug_string ());
1505 RETURN_NULL_IF_FAIL_PRINTF6 (
1506 compatible_types (param_type
,
1509 "mismatching types for argument %d of fn_ptr: %s:"
1510 " assignment to param %d (type: %s) from %s (type: %s)",
1512 fn_ptr
->get_debug_string (),
1514 param_type
->get_debug_string (),
1515 arg
->get_debug_string (),
1516 arg
->get_type ()->get_debug_string ());
1519 return (gcc_jit_rvalue
*)(
1520 ctxt
->new_call_through_ptr (loc
,
1523 (gcc::jit::recording::rvalue
**)args
));
1526 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1527 to DST_TYPE, for use by gcc_jit_context_new_cast.
1529 We only permit these kinds of cast:
1533 P* <-> Q* for pointer types P and Q. */
1536 is_valid_cast (gcc::jit::recording::type
*src_type
,
1537 gcc_jit_type
*dst_type
)
1539 bool src_is_int
= src_type
->is_int ();
1540 bool dst_is_int
= dst_type
->is_int ();
1541 bool src_is_float
= src_type
->is_float ();
1542 bool dst_is_float
= dst_type
->is_float ();
1543 bool src_is_bool
= src_type
->is_bool ();
1544 bool dst_is_bool
= dst_type
->is_bool ();
1547 if (dst_is_int
|| dst_is_float
|| dst_is_bool
)
1551 if (dst_is_int
|| dst_is_float
)
1555 if (dst_is_int
|| dst_is_bool
)
1558 /* Permit casts between pointer types. */
1559 gcc::jit::recording::type
*deref_src_type
= src_type
->is_pointer ();
1560 gcc::jit::recording::type
*deref_dst_type
= dst_type
->is_pointer ();
1561 if (deref_src_type
&& deref_dst_type
)
1567 /* Public entrypoint. See description in libgccjit.h.
1569 After error-checking, the real work is done by the
1570 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1573 gcc_jit_context_new_cast (gcc_jit_context
*ctxt
,
1574 gcc_jit_location
*loc
,
1575 gcc_jit_rvalue
*rvalue
,
1578 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1579 JIT_LOG_FUNC (ctxt
->get_logger ());
1580 /* LOC can be NULL. */
1581 RETURN_NULL_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1582 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
1583 RETURN_NULL_IF_FAIL_PRINTF3 (
1584 is_valid_cast (rvalue
->get_type (), type
),
1586 "cannot cast %s from type: %s to type: %s",
1587 rvalue
->get_debug_string (),
1588 rvalue
->get_type ()->get_debug_string (),
1589 type
->get_debug_string ());
1591 return static_cast <gcc_jit_rvalue
*> (ctxt
->new_cast (loc
, rvalue
, type
));
1594 /* Public entrypoint. See description in libgccjit.h.
1596 After error-checking, the real work is done by the
1597 gcc::jit::recording::context::new_array_access method in
1600 extern gcc_jit_lvalue
*
1601 gcc_jit_context_new_array_access (gcc_jit_context
*ctxt
,
1602 gcc_jit_location
*loc
,
1603 gcc_jit_rvalue
*ptr
,
1604 gcc_jit_rvalue
*index
)
1606 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1607 JIT_LOG_FUNC (ctxt
->get_logger ());
1608 /* LOC can be NULL. */
1609 RETURN_NULL_IF_FAIL (ptr
, ctxt
, loc
, "NULL ptr");
1610 RETURN_NULL_IF_FAIL (index
, ctxt
, loc
, "NULL index");
1611 RETURN_NULL_IF_FAIL_PRINTF2 (
1612 ptr
->get_type ()->dereference (),
1614 "ptr: %s (type: %s) is not a pointer or array",
1615 ptr
->get_debug_string (),
1616 ptr
->get_type ()->get_debug_string ());
1617 RETURN_NULL_IF_FAIL_PRINTF2 (
1618 index
->get_type ()->is_numeric (),
1620 "index: %s (type: %s) is not of numeric type",
1621 index
->get_debug_string (),
1622 index
->get_type ()->get_debug_string ());
1624 return (gcc_jit_lvalue
*)ctxt
->new_array_access (loc
, ptr
, index
);
1627 /* Public entrypoint. See description in libgccjit.h.
1629 After error-checking, the real work is done by the
1630 gcc::jit::recording::memento::get_context method in
1634 gcc_jit_object_get_context (gcc_jit_object
*obj
)
1636 RETURN_NULL_IF_FAIL (obj
, NULL
, NULL
, "NULL object");
1638 return static_cast <gcc_jit_context
*> (obj
->get_context ());
1641 /* Public entrypoint. See description in libgccjit.h.
1643 After error-checking, the real work is done by the
1644 gcc::jit::recording::memento::get_debug_string method in
1648 gcc_jit_object_get_debug_string (gcc_jit_object
*obj
)
1650 RETURN_NULL_IF_FAIL (obj
, NULL
, NULL
, "NULL object");
1652 return obj
->get_debug_string ();
1655 /* Public entrypoint. See description in libgccjit.h.
1657 After error-checking, the real work is done by the
1658 gcc::jit::recording::lvalue::access_field method in
1662 gcc_jit_lvalue_access_field (gcc_jit_lvalue
*struct_
,
1663 gcc_jit_location
*loc
,
1664 gcc_jit_field
*field
)
1666 RETURN_NULL_IF_FAIL (struct_
, NULL
, loc
, "NULL struct");
1667 gcc::jit::recording::context
*ctxt
= struct_
->m_ctxt
;
1668 JIT_LOG_FUNC (ctxt
->get_logger ());
1669 /* LOC can be NULL. */
1670 RETURN_NULL_IF_FAIL (field
, ctxt
, loc
, "NULL field");
1671 RETURN_NULL_IF_FAIL_PRINTF1 (field
->get_container (), field
->m_ctxt
, loc
,
1672 "field %s has not been placed in a struct",
1673 field
->get_debug_string ());
1674 gcc::jit::recording::type
*underlying_type
=
1675 struct_
->get_type ();
1676 RETURN_NULL_IF_FAIL_PRINTF2 (
1677 (field
->get_container ()->unqualified ()
1678 == underlying_type
->unqualified ()),
1679 struct_
->m_ctxt
, loc
,
1680 "%s is not a field of %s",
1681 field
->get_debug_string (),
1682 underlying_type
->get_debug_string ());
1684 return (gcc_jit_lvalue
*)struct_
->access_field (loc
, field
);
1687 /* Public entrypoint. See description in libgccjit.h.
1689 After error-checking, the real work is done by the
1690 gcc::jit::recording::rvalue::access_field method in
1694 gcc_jit_rvalue_access_field (gcc_jit_rvalue
*struct_
,
1695 gcc_jit_location
*loc
,
1696 gcc_jit_field
*field
)
1698 RETURN_NULL_IF_FAIL (struct_
, NULL
, loc
, "NULL struct");
1699 gcc::jit::recording::context
*ctxt
= struct_
->m_ctxt
;
1700 JIT_LOG_FUNC (ctxt
->get_logger ());
1701 /* LOC can be NULL. */
1702 RETURN_NULL_IF_FAIL (field
, ctxt
, loc
, "NULL field");
1703 RETURN_NULL_IF_FAIL_PRINTF1 (field
->get_container (), field
->m_ctxt
, loc
,
1704 "field %s has not been placed in a struct",
1705 field
->get_debug_string ());
1706 gcc::jit::recording::type
*underlying_type
=
1707 struct_
->get_type ();
1708 RETURN_NULL_IF_FAIL_PRINTF2 (
1709 (field
->get_container ()->unqualified ()
1710 == underlying_type
->unqualified ()),
1711 struct_
->m_ctxt
, loc
,
1712 "%s is not a field of %s",
1713 field
->get_debug_string (),
1714 underlying_type
->get_debug_string ());
1716 return (gcc_jit_rvalue
*)struct_
->access_field (loc
, field
);
1719 /* Public entrypoint. See description in libgccjit.h.
1721 After error-checking, the real work is done by the
1722 gcc::jit::recording::rvalue::deference_field method in
1726 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue
*ptr
,
1727 gcc_jit_location
*loc
,
1728 gcc_jit_field
*field
)
1730 RETURN_NULL_IF_FAIL (ptr
, NULL
, loc
, "NULL ptr");
1731 JIT_LOG_FUNC (ptr
->get_context ()->get_logger ());
1732 /* LOC can be NULL. */
1733 RETURN_NULL_IF_FAIL (field
, NULL
, loc
, "NULL field");
1734 gcc::jit::recording::type
*underlying_type
=
1735 ptr
->get_type ()->is_pointer ();
1736 RETURN_NULL_IF_FAIL_PRINTF1 (field
->get_container (), field
->m_ctxt
, loc
,
1737 "field %s has not been placed in a struct",
1738 field
->get_debug_string ());
1739 RETURN_NULL_IF_FAIL_PRINTF3 (
1742 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1743 ptr
->get_debug_string (),
1744 ptr
->get_type ()->get_debug_string (),
1745 field
->get_debug_string ());
1746 RETURN_NULL_IF_FAIL_PRINTF2 (
1747 (field
->get_container ()->unqualified ()
1748 == underlying_type
->unqualified ()),
1750 "%s is not a field of %s",
1751 field
->get_debug_string (),
1752 underlying_type
->get_debug_string ());
1754 return (gcc_jit_lvalue
*)ptr
->dereference_field (loc
, field
);
1757 /* Public entrypoint. See description in libgccjit.h.
1759 After error-checking, the real work is done by the
1760 gcc::jit::recording::rvalue::deference method in
1764 gcc_jit_rvalue_dereference (gcc_jit_rvalue
*rvalue
,
1765 gcc_jit_location
*loc
)
1767 RETURN_NULL_IF_FAIL (rvalue
, NULL
, loc
, "NULL rvalue");
1768 JIT_LOG_FUNC (rvalue
->get_context ()->get_logger ());
1769 /* LOC can be NULL. */
1771 gcc::jit::recording::type
*underlying_type
=
1772 rvalue
->get_type ()->is_pointer ();
1774 RETURN_NULL_IF_FAIL_PRINTF2 (
1776 rvalue
->m_ctxt
, loc
,
1777 "dereference of non-pointer %s (type: %s)",
1778 rvalue
->get_debug_string (),
1779 rvalue
->get_type ()->get_debug_string ());
1781 RETURN_NULL_IF_FAIL_PRINTF2 (
1782 !underlying_type
->is_void (),
1783 rvalue
->m_ctxt
, loc
,
1784 "dereference of void pointer %s (type: %s)",
1785 rvalue
->get_debug_string (),
1786 rvalue
->get_type ()->get_debug_string ());
1788 return (gcc_jit_lvalue
*)rvalue
->dereference (loc
);
1791 /* Public entrypoint. See description in libgccjit.h.
1793 After error-checking, the real work is done by the
1794 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1797 gcc_jit_lvalue_get_address (gcc_jit_lvalue
*lvalue
,
1798 gcc_jit_location
*loc
)
1800 RETURN_NULL_IF_FAIL (lvalue
, NULL
, loc
, "NULL lvalue");
1801 JIT_LOG_FUNC (lvalue
->get_context ()->get_logger ());
1802 /* LOC can be NULL. */
1804 return (gcc_jit_rvalue
*)lvalue
->get_address (loc
);
1807 /* Public entrypoint. See description in libgccjit.h.
1809 After error-checking, the real work is done by the
1810 gcc::jit::recording::function::new_local method in jit-recording.c. */
1813 gcc_jit_function_new_local (gcc_jit_function
*func
,
1814 gcc_jit_location
*loc
,
1818 RETURN_NULL_IF_FAIL (func
, NULL
, loc
, "NULL function");
1819 gcc::jit::recording::context
*ctxt
= func
->m_ctxt
;
1820 JIT_LOG_FUNC (ctxt
->get_logger ());
1821 /* LOC can be NULL. */
1822 RETURN_NULL_IF_FAIL (func
->get_kind () != GCC_JIT_FUNCTION_IMPORTED
,
1824 "Cannot add locals to an imported function");
1825 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
1826 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
1828 return (gcc_jit_lvalue
*)func
->new_local (loc
, type
, name
);
1831 /* Public entrypoint. See description in libgccjit.h.
1833 After error-checking, the real work is done by the
1834 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1837 gcc_jit_block_add_eval (gcc_jit_block
*block
,
1838 gcc_jit_location
*loc
,
1839 gcc_jit_rvalue
*rvalue
)
1841 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1842 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1843 JIT_LOG_FUNC (ctxt
->get_logger ());
1844 /* LOC can be NULL. */
1845 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1847 gcc::jit::recording::statement
*stmt
= block
->add_eval (loc
, rvalue
);
1849 /* "stmt" should be good enough to be usable in error-messages,
1850 but might still not be compilable; perform some more
1851 error-checking here. We do this here so that the error messages
1852 can contain a stringified version of "stmt", whilst appearing
1853 as close as possible to the point of failure. */
1854 rvalue
->verify_valid_within_stmt (__func__
, stmt
);
1857 /* Public entrypoint. See description in libgccjit.h.
1859 After error-checking, the real work is done by the
1860 gcc::jit::recording::block::add_assignment method in
1864 gcc_jit_block_add_assignment (gcc_jit_block
*block
,
1865 gcc_jit_location
*loc
,
1866 gcc_jit_lvalue
*lvalue
,
1867 gcc_jit_rvalue
*rvalue
)
1869 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1870 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1871 JIT_LOG_FUNC (ctxt
->get_logger ());
1872 /* LOC can be NULL. */
1873 RETURN_IF_FAIL (lvalue
, ctxt
, loc
, "NULL lvalue");
1874 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1875 RETURN_IF_FAIL_PRINTF4 (
1876 compatible_types (lvalue
->get_type (),
1877 rvalue
->get_type ()),
1879 "mismatching types:"
1880 " assignment to %s (type: %s) from %s (type: %s)",
1881 lvalue
->get_debug_string (),
1882 lvalue
->get_type ()->get_debug_string (),
1883 rvalue
->get_debug_string (),
1884 rvalue
->get_type ()->get_debug_string ());
1886 gcc::jit::recording::statement
*stmt
= block
->add_assignment (loc
, lvalue
, rvalue
);
1888 /* "stmt" should be good enough to be usable in error-messages,
1889 but might still not be compilable; perform some more
1890 error-checking here. We do this here so that the error messages
1891 can contain a stringified version of "stmt", whilst appearing
1892 as close as possible to the point of failure. */
1893 lvalue
->verify_valid_within_stmt (__func__
, stmt
);
1894 rvalue
->verify_valid_within_stmt (__func__
, stmt
);
1897 /* Public entrypoint. See description in libgccjit.h.
1899 After error-checking, the real work is done by the
1900 gcc::jit::recording::block::add_assignment_op method in
1904 gcc_jit_block_add_assignment_op (gcc_jit_block
*block
,
1905 gcc_jit_location
*loc
,
1906 gcc_jit_lvalue
*lvalue
,
1907 enum gcc_jit_binary_op op
,
1908 gcc_jit_rvalue
*rvalue
)
1910 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1911 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1912 JIT_LOG_FUNC (ctxt
->get_logger ());
1913 /* LOC can be NULL. */
1914 RETURN_IF_FAIL (lvalue
, ctxt
, loc
, "NULL lvalue");
1915 RETURN_IF_FAIL_PRINTF1 (
1916 valid_binary_op_p (op
),
1918 "unrecognized value for enum gcc_jit_binary_op: %i",
1920 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1921 RETURN_IF_FAIL_PRINTF4 (
1922 compatible_types (lvalue
->get_type (),
1923 rvalue
->get_type ()),
1925 "mismatching types:"
1926 " assignment to %s (type: %s) involving %s (type: %s)",
1927 lvalue
->get_debug_string (),
1928 lvalue
->get_type ()->get_debug_string (),
1929 rvalue
->get_debug_string (),
1930 rvalue
->get_type ()->get_debug_string ());
1932 gcc::jit::recording::statement
*stmt
= block
->add_assignment_op (loc
, lvalue
, op
, rvalue
);
1934 /* "stmt" should be good enough to be usable in error-messages,
1935 but might still not be compilable; perform some more
1936 error-checking here. We do this here so that the error messages
1937 can contain a stringified version of "stmt", whilst appearing
1938 as close as possible to the point of failure. */
1939 lvalue
->verify_valid_within_stmt (__func__
, stmt
);
1940 rvalue
->verify_valid_within_stmt (__func__
, stmt
);
1943 /* Internal helper function for determining if rvalue BOOLVAL is of
1944 boolean type. For use by gcc_jit_block_end_with_conditional. */
1947 is_bool (gcc_jit_rvalue
*boolval
)
1949 gcc::jit::recording::type
*actual_type
= boolval
->get_type ();
1950 gcc::jit::recording::type
*bool_type
=
1951 boolval
->m_ctxt
->get_type (GCC_JIT_TYPE_BOOL
);
1952 return actual_type
== bool_type
;
1955 /* Public entrypoint. See description in libgccjit.h.
1957 After error-checking, the real work is done by the
1958 gcc::jit::recording::block::end_with_conditional method in
1962 gcc_jit_block_end_with_conditional (gcc_jit_block
*block
,
1963 gcc_jit_location
*loc
,
1964 gcc_jit_rvalue
*boolval
,
1965 gcc_jit_block
*on_true
,
1966 gcc_jit_block
*on_false
)
1968 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1969 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1970 JIT_LOG_FUNC (ctxt
->get_logger ());
1971 /* LOC can be NULL. */
1972 RETURN_IF_FAIL (boolval
, ctxt
, loc
, "NULL boolval");
1973 RETURN_IF_FAIL_PRINTF2 (
1974 is_bool (boolval
), ctxt
, loc
,
1975 "%s (type: %s) is not of boolean type ",
1976 boolval
->get_debug_string (),
1977 boolval
->get_type ()->get_debug_string ());
1978 RETURN_IF_FAIL (on_true
, ctxt
, loc
, "NULL on_true");
1979 RETURN_IF_FAIL (on_true
, ctxt
, loc
, "NULL on_false");
1980 RETURN_IF_FAIL_PRINTF4 (
1981 block
->get_function () == on_true
->get_function (),
1983 "\"on_true\" block is not in same function:"
1984 " source block %s is in function %s"
1985 " whereas target block %s is in function %s",
1986 block
->get_debug_string (),
1987 block
->get_function ()->get_debug_string (),
1988 on_true
->get_debug_string (),
1989 on_true
->get_function ()->get_debug_string ());
1990 RETURN_IF_FAIL_PRINTF4 (
1991 block
->get_function () == on_false
->get_function (),
1993 "\"on_false\" block is not in same function:"
1994 " source block %s is in function %s"
1995 " whereas target block %s is in function %s",
1996 block
->get_debug_string (),
1997 block
->get_function ()->get_debug_string (),
1998 on_false
->get_debug_string (),
1999 on_false
->get_function ()->get_debug_string ());
2001 gcc::jit::recording::statement
*stmt
= block
->end_with_conditional (loc
, boolval
, on_true
, on_false
);
2003 /* "stmt" should be good enough to be usable in error-messages,
2004 but might still not be compilable; perform some more
2005 error-checking here. We do this here so that the error messages
2006 can contain a stringified version of "stmt", whilst appearing
2007 as close as possible to the point of failure. */
2008 boolval
->verify_valid_within_stmt (__func__
, stmt
);
2011 /* Public entrypoint. See description in libgccjit.h.
2013 After error-checking, the real work is done by the
2014 gcc::jit::recording::block::add_comment method in
2018 gcc_jit_block_add_comment (gcc_jit_block
*block
,
2019 gcc_jit_location
*loc
,
2022 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
2023 gcc::jit::recording::context
*ctxt
= block
->get_context ();
2024 JIT_LOG_FUNC (ctxt
->get_logger ());
2025 /* LOC can be NULL. */
2026 RETURN_IF_FAIL (text
, ctxt
, loc
, "NULL text");
2028 block
->add_comment (loc
, text
);
2031 /* Public entrypoint. See description in libgccjit.h.
2033 After error-checking, the real work is done by the
2034 gcc::jit::recording::block::end_with_jump method in
2038 gcc_jit_block_end_with_jump (gcc_jit_block
*block
,
2039 gcc_jit_location
*loc
,
2040 gcc_jit_block
*target
)
2042 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
2043 gcc::jit::recording::context
*ctxt
= block
->get_context ();
2044 JIT_LOG_FUNC (ctxt
->get_logger ());
2045 /* LOC can be NULL. */
2046 RETURN_IF_FAIL (target
, ctxt
, loc
, "NULL target");
2047 RETURN_IF_FAIL_PRINTF4 (
2048 block
->get_function () == target
->get_function (),
2050 "target block is not in same function:"
2051 " source block %s is in function %s"
2052 " whereas target block %s is in function %s",
2053 block
->get_debug_string (),
2054 block
->get_function ()->get_debug_string (),
2055 target
->get_debug_string (),
2056 target
->get_function ()->get_debug_string ());
2058 block
->end_with_jump (loc
, target
);
2061 /* Public entrypoint. See description in libgccjit.h.
2063 After error-checking, the real work is done by the
2064 gcc::jit::recording::block::end_with_return method in
2068 gcc_jit_block_end_with_return (gcc_jit_block
*block
,
2069 gcc_jit_location
*loc
,
2070 gcc_jit_rvalue
*rvalue
)
2072 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
2073 gcc::jit::recording::context
*ctxt
= block
->get_context ();
2074 JIT_LOG_FUNC (ctxt
->get_logger ());
2075 /* LOC can be NULL. */
2076 gcc::jit::recording::function
*func
= block
->get_function ();
2077 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
2078 RETURN_IF_FAIL_PRINTF4 (
2080 func
->get_return_type (),
2081 rvalue
->get_type ()),
2083 "mismatching types:"
2084 " return of %s (type: %s) in function %s (return type: %s)",
2085 rvalue
->get_debug_string (),
2086 rvalue
->get_type ()->get_debug_string (),
2087 func
->get_debug_string (),
2088 func
->get_return_type ()->get_debug_string ());
2090 gcc::jit::recording::statement
*stmt
= block
->end_with_return (loc
, rvalue
);
2092 /* "stmt" should be good enough to be usable in error-messages,
2093 but might still not be compilable; perform some more
2094 error-checking here. We do this here so that the error messages
2095 can contain a stringified version of "stmt", whilst appearing
2096 as close as possible to the point of failure. */
2097 rvalue
->verify_valid_within_stmt (__func__
, stmt
);
2100 /* Public entrypoint. See description in libgccjit.h.
2102 After error-checking, the real work is done by the
2103 gcc::jit::recording::block::end_with_return method in
2107 gcc_jit_block_end_with_void_return (gcc_jit_block
*block
,
2108 gcc_jit_location
*loc
)
2110 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
2111 gcc::jit::recording::context
*ctxt
= block
->get_context ();
2112 JIT_LOG_FUNC (ctxt
->get_logger ());
2113 /* LOC can be NULL. */
2114 gcc::jit::recording::function
*func
= block
->get_function ();
2115 RETURN_IF_FAIL_PRINTF2 (
2116 func
->get_return_type () == ctxt
->get_type (GCC_JIT_TYPE_VOID
),
2118 "mismatching types:"
2119 " void return in function %s (return type: %s)",
2120 func
->get_debug_string (),
2121 func
->get_return_type ()->get_debug_string ());
2123 block
->end_with_return (loc
, NULL
);
2126 /**********************************************************************
2128 **********************************************************************/
2130 /* Public entrypoint. See description in libgccjit.h.
2132 After error-checking, the real work is done by the
2133 gcc::jit::recording::context::set_str_option method in
2137 gcc_jit_context_set_str_option (gcc_jit_context
*ctxt
,
2138 enum gcc_jit_str_option opt
,
2141 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2142 JIT_LOG_FUNC (ctxt
->get_logger ());
2143 /* opt is checked by the inner function.
2144 value can be NULL. */
2146 ctxt
->set_str_option (opt
, value
);
2149 /* Public entrypoint. See description in libgccjit.h.
2151 After error-checking, the real work is done by the
2152 gcc::jit::recording::context::set_int_option method in
2156 gcc_jit_context_set_int_option (gcc_jit_context
*ctxt
,
2157 enum gcc_jit_int_option opt
,
2160 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2161 JIT_LOG_FUNC (ctxt
->get_logger ());
2162 /* opt is checked by the inner function. */
2164 ctxt
->set_int_option (opt
, value
);
2167 /* Public entrypoint. See description in libgccjit.h.
2169 After error-checking, the real work is done by the
2170 gcc::jit::recording::context::set_bool_option method in
2174 gcc_jit_context_set_bool_option (gcc_jit_context
*ctxt
,
2175 enum gcc_jit_bool_option opt
,
2178 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2179 JIT_LOG_FUNC (ctxt
->get_logger ());
2180 /* opt is checked by the inner function. */
2182 ctxt
->set_bool_option (opt
, value
);
2185 /* Public entrypoint. See description in libgccjit.h.
2187 After error-checking, the real work is done by the
2188 gcc::jit::recording::context::set_inner_bool_option method in
2192 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context
*ctxt
,
2195 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2196 JIT_LOG_FUNC (ctxt
->get_logger ());
2197 ctxt
->set_inner_bool_option (
2198 gcc::jit::INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS
,
2202 /* Public entrypoint. See description in libgccjit.h.
2204 After error-checking, the real work is done by the
2205 gcc::jit::recording::context::add_command_line_option method in
2209 gcc_jit_context_add_command_line_option (gcc_jit_context
*ctxt
,
2210 const char *optname
)
2212 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2213 JIT_LOG_FUNC (ctxt
->get_logger ());
2214 RETURN_IF_FAIL (optname
, ctxt
, NULL
, "NULL optname");
2215 if (ctxt
->get_logger ())
2216 ctxt
->get_logger ()->log ("optname: %s", optname
);
2218 ctxt
->add_command_line_option (optname
);
2221 /* Public entrypoint. See description in libgccjit.h.
2223 After error-checking, the real work is done by the
2224 gcc::jit::recording::context::enable_dump method in
2228 gcc_jit_context_enable_dump (gcc_jit_context
*ctxt
,
2229 const char *dumpname
,
2232 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2233 JIT_LOG_FUNC (ctxt
->get_logger ());
2234 RETURN_IF_FAIL (dumpname
, ctxt
, NULL
, "NULL dumpname");
2235 RETURN_IF_FAIL (out_ptr
, ctxt
, NULL
, "NULL out_ptr");
2237 ctxt
->enable_dump (dumpname
, out_ptr
);
2240 /* Public entrypoint. See description in libgccjit.h.
2242 After error-checking, the real work is done by the
2243 gcc::jit::recording::context::compile method in
2247 gcc_jit_context_compile (gcc_jit_context
*ctxt
)
2249 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2251 JIT_LOG_FUNC (ctxt
->get_logger ());
2253 ctxt
->log ("in-memory compile of ctxt: %p", (void *)ctxt
);
2255 gcc_jit_result
*result
= (gcc_jit_result
*)ctxt
->compile ();
2257 ctxt
->log ("%s: returning (gcc_jit_result *)%p",
2258 __func__
, (void *)result
);
2263 /* Public entrypoint. See description in libgccjit.h.
2265 After error-checking, the real work is done by the
2266 gcc::jit::recording::context::compile_to_file method in
2270 gcc_jit_context_compile_to_file (gcc_jit_context
*ctxt
,
2271 enum gcc_jit_output_kind output_kind
,
2272 const char *output_path
)
2274 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2275 JIT_LOG_FUNC (ctxt
->get_logger ());
2276 RETURN_IF_FAIL_PRINTF1 (
2277 ((output_kind
>= GCC_JIT_OUTPUT_KIND_ASSEMBLER
)
2278 && (output_kind
<= GCC_JIT_OUTPUT_KIND_EXECUTABLE
)),
2280 "unrecognized output_kind: %i",
2282 RETURN_IF_FAIL (output_path
, ctxt
, NULL
, "NULL output_path");
2284 ctxt
->log ("compile_to_file of ctxt: %p", (void *)ctxt
);
2285 ctxt
->log ("output_kind: %i", output_kind
);
2286 ctxt
->log ("output_path: %s", output_path
);
2288 ctxt
->compile_to_file (output_kind
, output_path
);
2292 /* Public entrypoint. See description in libgccjit.h.
2294 After error-checking, the real work is done by the
2295 gcc::jit::recording::context::dump_to_file method in
2299 gcc_jit_context_dump_to_file (gcc_jit_context
*ctxt
,
2301 int update_locations
)
2303 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2304 JIT_LOG_FUNC (ctxt
->get_logger ());
2305 RETURN_IF_FAIL (path
, ctxt
, NULL
, "NULL path");
2306 ctxt
->dump_to_file (path
, update_locations
);
2309 /* Public entrypoint. See description in libgccjit.h. */
2312 gcc_jit_context_set_logfile (gcc_jit_context
*ctxt
,
2317 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2318 JIT_LOG_FUNC (ctxt
->get_logger ());
2319 RETURN_IF_FAIL ((flags
== 0), ctxt
, NULL
, "flags must be 0 for now");
2320 RETURN_IF_FAIL ((verbosity
== 0), ctxt
, NULL
, "verbosity must be 0 for now");
2322 gcc::jit::logger
*logger
;
2324 logger
= new gcc::jit::logger (logfile
, flags
, verbosity
);
2327 ctxt
->set_logger (logger
);
2330 /* Public entrypoint. See description in libgccjit.h.
2332 After error-checking, the real work is done by the
2333 gcc::jit::recording::context::dump_reproducer_to_file method in
2337 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context
*ctxt
,
2340 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2341 JIT_LOG_FUNC (ctxt
->get_logger ());
2342 RETURN_IF_FAIL (path
, ctxt
, NULL
, "NULL path");
2343 ctxt
->dump_reproducer_to_file (path
);
2346 /* Public entrypoint. See description in libgccjit.h.
2348 After error-checking, the real work is done by the
2349 gcc::jit::recording::context::get_first_error method in
2353 gcc_jit_context_get_first_error (gcc_jit_context
*ctxt
)
2355 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2356 JIT_LOG_FUNC (ctxt
->get_logger ());
2358 return ctxt
->get_first_error ();
2361 /* Public entrypoint. See description in libgccjit.h.
2363 After error-checking, the real work is done by the
2364 gcc::jit::recording::context::get_last_error method in
2368 gcc_jit_context_get_last_error (gcc_jit_context
*ctxt
)
2370 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2372 return ctxt
->get_last_error ();
2375 /* Public entrypoint. See description in libgccjit.h.
2377 After error-checking, the real work is done by the
2378 gcc::jit::result::get_code method in jit-result.c. */
2381 gcc_jit_result_get_code (gcc_jit_result
*result
,
2384 RETURN_NULL_IF_FAIL (result
, NULL
, NULL
, "NULL result");
2385 JIT_LOG_FUNC (result
->get_logger ());
2386 RETURN_NULL_IF_FAIL (fnname
, NULL
, NULL
, "NULL fnname");
2388 result
->log ("locating fnname: %s", fnname
);
2389 void *code
= result
->get_code (fnname
);
2390 result
->log ("%s: returning (void *)%p", __func__
, code
);
2395 /* Public entrypoint. See description in libgccjit.h.
2397 After error-checking, the real work is done by the
2398 gcc::jit::result::get_global method in jit-result.c. */
2401 gcc_jit_result_get_global (gcc_jit_result
*result
,
2404 RETURN_NULL_IF_FAIL (result
, NULL
, NULL
, "NULL result");
2405 JIT_LOG_FUNC (result
->get_logger ());
2406 RETURN_NULL_IF_FAIL (name
, NULL
, NULL
, "NULL name");
2408 void *global
= result
->get_global (name
);
2409 result
->log ("%s: returning (void *)%p", __func__
, global
);
2414 /* Public entrypoint. See description in libgccjit.h.
2416 After error-checking, this is essentially a wrapper around the
2417 destructor for gcc::jit::result in jit-result.c. */
2420 gcc_jit_result_release (gcc_jit_result
*result
)
2422 RETURN_IF_FAIL (result
, NULL
, NULL
, "NULL result");
2423 JIT_LOG_FUNC (result
->get_logger ());
2424 result
->log ("deleting result: %p", (void *)result
);