1 /* Implementation of the C API; all wrappers into the internal C++ API
2 Copyright (C) 2013-2014 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-recording.h"
31 /* The opaque types used by the public API are actually subclasses
32 of the gcc::jit::recording classes. */
34 struct gcc_jit_context
: public gcc::jit::recording::context
36 gcc_jit_context (gcc_jit_context
*parent_ctxt
) :
41 struct gcc_jit_result
: public gcc::jit::result
45 struct gcc_jit_object
: public gcc::jit::recording::memento
49 struct gcc_jit_location
: public gcc::jit::recording::location
53 struct gcc_jit_type
: public gcc::jit::recording::type
57 struct gcc_jit_struct
: public gcc::jit::recording::struct_
61 struct gcc_jit_field
: public gcc::jit::recording::field
65 struct gcc_jit_function
: public gcc::jit::recording::function
69 struct gcc_jit_block
: public gcc::jit::recording::block
73 struct gcc_jit_rvalue
: public gcc::jit::recording::rvalue
77 struct gcc_jit_lvalue
: public gcc::jit::recording::lvalue
81 struct gcc_jit_param
: public gcc::jit::recording::param
85 /**********************************************************************
88 We try to gracefully handle API usage errors by being defensive
90 **********************************************************************/
92 #define JIT_BEGIN_STMT do {
93 #define JIT_END_STMT } while(0)
95 /* Each of these error-handling macros determines if TEST_EXPR holds.
97 If TEXT_EXPR fails to hold we return from the enclosing function and
98 print an error, either via adding an error on the given context CTXT
99 if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
102 They have to be macros since they inject their "return" into the
103 function they are placed in.
105 The variant macros express:
107 (A) whether or not we need to return a value:
108 RETURN_VAL_IF_FAIL* vs
110 with the former returning RETURN_EXPR, and
112 for the common case where a NULL value is to be returned on
115 (B) whether the error message is to be directly printed:
117 or is a format string with some number of arguments:
118 RETURN_*IF_FAIL_PRINTF*
120 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
124 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
128 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
129 return (RETURN_EXPR); \
133 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
137 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
139 return (RETURN_EXPR); \
143 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
147 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
148 __func__, (A0), (A1)); \
149 return (RETURN_EXPR); \
153 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
157 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
158 __func__, (A0), (A1), (A2)); \
159 return (RETURN_EXPR); \
163 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
167 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
168 __func__, (A0), (A1), (A2), (A3)); \
169 return (RETURN_EXPR); \
173 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
177 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
178 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
179 return (RETURN_EXPR); \
183 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
184 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
186 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
187 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
189 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
190 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
192 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
193 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
195 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
196 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
198 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
199 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
201 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
205 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
210 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
214 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
220 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
224 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
225 __func__, (A0), (A1)); \
230 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
234 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
235 __func__, (A0), (A1), (A2), (A3)); \
240 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
241 it. This will fail if BLOCK has already been terminated by some
242 kind of jump or a return. */
243 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
245 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
246 RETURN_IF_FAIL_PRINTF2 ( \
247 !(BLOCK)->has_been_terminated (), \
248 (BLOCK)->get_context (), \
250 "adding to terminated block: %s (already terminated by: %s)", \
251 (BLOCK)->get_debug_string (), \
252 (BLOCK)->get_last_statement ()->get_debug_string ()); \
255 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
257 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
259 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
260 RETURN_NULL_IF_FAIL_PRINTF2 ( \
261 !(BLOCK)->has_been_terminated (), \
262 (BLOCK)->get_context (), \
264 "adding to terminated block: %s (already terminated by: %s)", \
265 (BLOCK)->get_debug_string (), \
266 (BLOCK)->get_last_statement ()->get_debug_string ()); \
269 /* Format the given string, and report it as an error, either on CTXT
270 if non-NULL, or by printing to stderr if we have a NULL context.
271 LOC gives the source location where the error occcurred, and can be
275 jit_error (gcc::jit::recording::context
*ctxt
,
276 gcc_jit_location
*loc
,
277 const char *fmt
, ...)
281 jit_error (gcc::jit::recording::context
*ctxt
,
282 gcc_jit_location
*loc
,
283 const char *fmt
, ...)
289 ctxt
->add_error_va (loc
, fmt
, ap
);
292 /* No context? Send to stderr. */
293 vfprintf (stderr
, fmt
, ap
);
294 fprintf (stderr
, "\n");
300 /* Determine whether or not we can write to lvalues of type LTYPE from
301 rvalues of type RTYPE, detecting type errors such as attempting to
302 write to an int with a string literal (without an explicit cast).
304 This is implemented by calling the
305 gcc::jit::recording::type::accepts_writes_from virtual function on
309 compatible_types (gcc::jit::recording::type
*ltype
,
310 gcc::jit::recording::type
*rtype
)
312 return ltype
->accepts_writes_from (rtype
);
315 /* Public entrypoint for acquiring a gcc_jit_context.
316 Note that this creates a new top-level context; contrast with
317 gcc_jit_context_new_child_context below.
319 The real work is done in the constructor for
320 gcc::jit::recording::context in jit-recording.c. */
323 gcc_jit_context_acquire (void)
325 return new gcc_jit_context (NULL
);
328 /* Public entrypoint for releasing a gcc_jit_context.
329 The real work is done in the destructor for
330 gcc::jit::recording::context in jit-recording.c. */
333 gcc_jit_context_release (gcc_jit_context
*ctxt
)
338 /* Public entrypoint for creating a child context within
339 PARENT_CTXT. See description in libgccjit.h.
341 The real work is done in the constructor for
342 gcc::jit::recording::context in jit-recording.c. */
345 gcc_jit_context_new_child_context (gcc_jit_context
*parent_ctxt
)
347 return new gcc_jit_context (parent_ctxt
);
350 /* Public entrypoint. See description in libgccjit.h.
352 After error-checking, the real work is done by the
353 gcc::jit::recording::context::new_location
354 method in jit-recording.c. */
357 gcc_jit_context_new_location (gcc_jit_context
*ctxt
,
358 const char *filename
,
362 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
364 return (gcc_jit_location
*)ctxt
->new_location (filename
, line
, column
);
367 /* Public entrypoint. See description in libgccjit.h.
369 After error-checking, this calls the trivial
370 gcc::jit::recording::memento::as_object method (a location is a
371 memento), in jit-recording.h. */
374 gcc_jit_location_as_object (gcc_jit_location
*loc
)
376 RETURN_NULL_IF_FAIL (loc
, NULL
, NULL
, "NULL location");
378 return static_cast <gcc_jit_object
*> (loc
->as_object ());
381 /* Public entrypoint. See description in libgccjit.h.
383 After error-checking, this calls the trivial
384 gcc::jit::recording::memento::as_object method (a type is a
385 memento), in jit-recording.h. */
388 gcc_jit_type_as_object (gcc_jit_type
*type
)
390 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
392 return static_cast <gcc_jit_object
*> (type
->as_object ());
395 /* Public entrypoint for getting a specific type from a context.
397 After error-checking, the real work is done by the
398 gcc::jit::recording::context::get_type method, in
402 gcc_jit_context_get_type (gcc_jit_context
*ctxt
,
403 enum gcc_jit_types type
)
405 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
406 RETURN_NULL_IF_FAIL_PRINTF1 (
407 (type
>= GCC_JIT_TYPE_VOID
408 && type
<= GCC_JIT_TYPE_FILE_PTR
),
410 "unrecognized value for enum gcc_jit_types: %i", type
);
412 return (gcc_jit_type
*)ctxt
->get_type (type
);
415 /* Public entrypoint for getting the integer type of the given size and
418 After error-checking, the real work is done by the
419 gcc::jit::recording::context::get_int_type method,
420 in jit-recording.c. */
423 gcc_jit_context_get_int_type (gcc_jit_context
*ctxt
,
424 int num_bytes
, int is_signed
)
426 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
427 RETURN_NULL_IF_FAIL (num_bytes
>= 0, ctxt
, NULL
, "negative size");
429 return (gcc_jit_type
*)ctxt
->get_int_type (num_bytes
, is_signed
);
432 /* Public entrypoint. See description in libgccjit.h.
434 After error-checking, the real work is done by the
435 gcc::jit::recording::type::get_pointer method, in
439 gcc_jit_type_get_pointer (gcc_jit_type
*type
)
441 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
443 return (gcc_jit_type
*)type
->get_pointer ();
446 /* Public entrypoint. See description in libgccjit.h.
448 After error-checking, the real work is done by the
449 gcc::jit::recording::type::get_const method, in
453 gcc_jit_type_get_const (gcc_jit_type
*type
)
455 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
457 return (gcc_jit_type
*)type
->get_const ();
460 /* Public entrypoint. See description in libgccjit.h.
462 After error-checking, the real work is done by the
463 gcc::jit::recording::type::get_volatile method, in
467 gcc_jit_type_get_volatile (gcc_jit_type
*type
)
469 RETURN_NULL_IF_FAIL (type
, NULL
, NULL
, "NULL type");
471 return (gcc_jit_type
*)type
->get_volatile ();
474 /* Public entrypoint. See description in libgccjit.h.
476 After error-checking, the real work is done by the
477 gcc::jit::recording::context::new_array_type method, in
481 gcc_jit_context_new_array_type (gcc_jit_context
*ctxt
,
482 gcc_jit_location
*loc
,
483 gcc_jit_type
*element_type
,
486 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
487 /* LOC can be NULL. */
488 RETURN_NULL_IF_FAIL (element_type
, ctxt
, loc
, "NULL type");
489 RETURN_NULL_IF_FAIL (num_elements
>= 0, ctxt
, NULL
, "negative size");
491 return (gcc_jit_type
*)ctxt
->new_array_type (loc
,
496 /* Public entrypoint. See description in libgccjit.h.
498 After error-checking, the real work is done by the
499 gcc::jit::recording::context::new_field method, in
503 gcc_jit_context_new_field (gcc_jit_context
*ctxt
,
504 gcc_jit_location
*loc
,
508 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
509 /* LOC can be NULL. */
510 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
511 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
513 return (gcc_jit_field
*)ctxt
->new_field (loc
, type
, name
);
516 /* Public entrypoint. See description in libgccjit.h.
518 After error-checking, this calls the trivial
519 gcc::jit::recording::memento::as_object method (a field is a
520 memento), in jit-recording.h. */
523 gcc_jit_field_as_object (gcc_jit_field
*field
)
525 RETURN_NULL_IF_FAIL (field
, NULL
, NULL
, "NULL field");
527 return static_cast <gcc_jit_object
*> (field
->as_object ());
530 /* Public entrypoint. See description in libgccjit.h.
532 After error-checking, the real work is done by the
533 gcc::jit::recording::context::new_struct_type method,
534 immediately followed by a "set_fields" call on the resulting
535 gcc::jit::recording::compound_type *, both in jit-recording.c */
538 gcc_jit_context_new_struct_type (gcc_jit_context
*ctxt
,
539 gcc_jit_location
*loc
,
542 gcc_jit_field
**fields
)
544 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
545 /* LOC can be NULL. */
546 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
548 RETURN_NULL_IF_FAIL (fields
, ctxt
, loc
, "NULL fields ptr");
549 for (int i
= 0; i
< num_fields
; i
++)
551 RETURN_NULL_IF_FAIL (fields
[i
], ctxt
, loc
, "NULL field ptr");
552 RETURN_NULL_IF_FAIL_PRINTF2 (
553 NULL
== fields
[i
]->get_container (),
555 "%s is already a field of %s",
556 fields
[i
]->get_debug_string (),
557 fields
[i
]->get_container ()->get_debug_string ());
560 gcc::jit::recording::struct_
*result
=
561 ctxt
->new_struct_type (loc
, name
);
562 result
->set_fields (loc
,
564 (gcc::jit::recording::field
**)fields
);
565 return static_cast<gcc_jit_struct
*> (result
);
568 /* Public entrypoint. See description in libgccjit.h.
570 After error-checking, the real work is done by the
571 gcc::jit::recording::context::new_struct_type method in
575 gcc_jit_context_new_opaque_struct (gcc_jit_context
*ctxt
,
576 gcc_jit_location
*loc
,
579 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
580 /* LOC can be NULL. */
581 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
583 return (gcc_jit_struct
*)ctxt
->new_struct_type (loc
, name
);
586 /* Public entrypoint. See description in libgccjit.h.
588 After error-checking, this calls the trivial
589 gcc::jit::recording::struct_::as_object method in
593 gcc_jit_struct_as_type (gcc_jit_struct
*struct_type
)
595 RETURN_NULL_IF_FAIL (struct_type
, NULL
, NULL
, "NULL struct_type");
597 return static_cast <gcc_jit_type
*> (struct_type
->as_type ());
600 /* Public entrypoint. See description in libgccjit.h.
602 After error-checking, the real work is done by the
603 gcc::jit::recording::compound_type::set_fields method in
607 gcc_jit_struct_set_fields (gcc_jit_struct
*struct_type
,
608 gcc_jit_location
*loc
,
610 gcc_jit_field
**fields
)
612 RETURN_IF_FAIL (struct_type
, NULL
, loc
, "NULL struct_type");
613 /* LOC can be NULL. */
614 gcc::jit::recording::context
*ctxt
= struct_type
->m_ctxt
;
615 RETURN_IF_FAIL_PRINTF1 (
616 NULL
== struct_type
->get_fields (), ctxt
, loc
,
617 "%s already has had fields set",
618 struct_type
->get_debug_string ());
620 RETURN_IF_FAIL (fields
, ctxt
, loc
, "NULL fields ptr");
621 for (int i
= 0; i
< num_fields
; i
++)
623 RETURN_IF_FAIL (fields
[i
], ctxt
, loc
, "NULL field ptr");
624 RETURN_IF_FAIL_PRINTF2 (
625 NULL
== fields
[i
]->get_container (),
627 "%s is already a field of %s",
628 fields
[i
]->get_debug_string (),
629 fields
[i
]->get_container ()->get_debug_string ());
632 struct_type
->set_fields (loc
, num_fields
,
633 (gcc::jit::recording::field
**)fields
);
636 /* Public entrypoint. See description in libgccjit.h.
638 After error-checking, the real work is done by the
639 gcc::jit::recording::context::new_union_type method,
640 immediately followed by a "set_fields" call on the resulting
641 gcc::jit::recording::compound_type *, both in jit-recording.c */
644 gcc_jit_context_new_union_type (gcc_jit_context
*ctxt
,
645 gcc_jit_location
*loc
,
648 gcc_jit_field
**fields
)
650 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
651 /* LOC can be NULL. */
652 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
654 RETURN_NULL_IF_FAIL (fields
, ctxt
, loc
, "NULL fields ptr");
655 for (int i
= 0; i
< num_fields
; i
++)
657 RETURN_NULL_IF_FAIL (fields
[i
], ctxt
, loc
, "NULL field ptr");
658 RETURN_NULL_IF_FAIL_PRINTF2 (
659 NULL
== fields
[i
]->get_container (),
661 "%s is already a field of %s",
662 fields
[i
]->get_debug_string (),
663 fields
[i
]->get_container ()->get_debug_string ());
666 gcc::jit::recording::union_
*result
=
667 ctxt
->new_union_type (loc
, name
);
668 result
->set_fields (loc
,
670 (gcc::jit::recording::field
**)fields
);
671 return (gcc_jit_type
*) (result
);
674 /* Public entrypoint. See description in libgccjit.h.
676 After error-checking, the real work is done by the
677 gcc::jit::recording::context::new_function_ptr_type method,
678 in jit-recording.c */
681 gcc_jit_context_new_function_ptr_type (gcc_jit_context
*ctxt
,
682 gcc_jit_location
*loc
,
683 gcc_jit_type
*return_type
,
685 gcc_jit_type
**param_types
,
688 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
689 /* LOC can be NULL. */
690 RETURN_NULL_IF_FAIL (return_type
, ctxt
, loc
, "NULL return_type");
691 RETURN_NULL_IF_FAIL (
692 (num_params
== 0) || param_types
,
694 "NULL param_types creating function pointer type");
695 for (int i
= 0; i
< num_params
; i
++)
696 RETURN_NULL_IF_FAIL_PRINTF1 (
699 "NULL parameter type %i creating function pointer type", i
);
701 return (gcc_jit_type
*)
702 ctxt
->new_function_ptr_type (loc
, return_type
,
704 (gcc::jit::recording::type
**)param_types
,
708 /* Constructing functions. */
710 /* Public entrypoint. See description in libgccjit.h.
712 After error-checking, the real work is done by the
713 gcc::jit::recording::context::new_param method, in jit-recording.c */
716 gcc_jit_context_new_param (gcc_jit_context
*ctxt
,
717 gcc_jit_location
*loc
,
721 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
722 /* LOC can be NULL. */
723 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
724 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
726 return (gcc_jit_param
*)ctxt
->new_param (loc
, type
, name
);
729 /* Public entrypoint. See description in libgccjit.h.
731 After error-checking, this calls the trivial
732 gcc::jit::recording::memento::as_object method (a param is a memento),
733 in jit-recording.h. */
736 gcc_jit_param_as_object (gcc_jit_param
*param
)
738 RETURN_NULL_IF_FAIL (param
, NULL
, NULL
, "NULL param");
740 return static_cast <gcc_jit_object
*> (param
->as_object ());
743 /* Public entrypoint. See description in libgccjit.h.
745 After error-checking, this calls the trivial
746 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
749 gcc_jit_param_as_lvalue (gcc_jit_param
*param
)
751 RETURN_NULL_IF_FAIL (param
, NULL
, NULL
, "NULL param");
753 return (gcc_jit_lvalue
*)param
->as_lvalue ();
756 /* Public entrypoint. See description in libgccjit.h.
758 After error-checking, this calls the trivial
759 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
760 in jit-recording.h. */
763 gcc_jit_param_as_rvalue (gcc_jit_param
*param
)
765 RETURN_NULL_IF_FAIL (param
, NULL
, NULL
, "NULL param");
767 return (gcc_jit_rvalue
*)param
->as_rvalue ();
770 /* Public entrypoint. See description in libgccjit.h.
772 After error-checking, the real work is done by the
773 gcc::jit::recording::context::new_function method, in
777 gcc_jit_context_new_function (gcc_jit_context
*ctxt
,
778 gcc_jit_location
*loc
,
779 enum gcc_jit_function_kind kind
,
780 gcc_jit_type
*return_type
,
783 gcc_jit_param
**params
,
786 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
787 /* LOC can be NULL. */
788 RETURN_NULL_IF_FAIL_PRINTF1 (
789 ((kind
>= GCC_JIT_FUNCTION_EXPORTED
)
790 && (kind
<= GCC_JIT_FUNCTION_ALWAYS_INLINE
)),
792 "unrecognized value for enum gcc_jit_function_kind: %i",
794 RETURN_NULL_IF_FAIL (return_type
, ctxt
, loc
, "NULL return_type");
795 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
796 /* The assembler can only handle certain names, so for now, enforce
797 C's rules for identiers upon the name, using ISALPHA and ISALNUM
798 from safe-ctype.h to ignore the current locale.
799 Eventually we'll need some way to interact with e.g. C++ name
804 RETURN_NULL_IF_FAIL_PRINTF2 (
805 ISALPHA (ch
) || ch
== '_',
807 "name \"%s\" contains invalid character: '%c'",
809 /* Subsequent chars: */
810 for (const char *ptr
= name
+ 1; (ch
= *ptr
); ptr
++)
812 RETURN_NULL_IF_FAIL_PRINTF2 (
813 ISALNUM (ch
) || ch
== '_',
815 "name \"%s\" contains invalid character: '%c'",
819 RETURN_NULL_IF_FAIL_PRINTF1 (
820 (num_params
== 0) || params
,
822 "NULL params creating function %s", name
);
823 for (int i
= 0; i
< num_params
; i
++)
824 RETURN_NULL_IF_FAIL_PRINTF2 (
827 "NULL parameter %i creating function %s", i
, name
);
829 return (gcc_jit_function
*)
830 ctxt
->new_function (loc
, kind
, return_type
, name
,
832 (gcc::jit::recording::param
**)params
,
837 /* Public entrypoint. See description in libgccjit.h.
839 After error-checking, the real work is done by the
840 gcc::jit::recording::context::get_builtin_function method, in
844 gcc_jit_context_get_builtin_function (gcc_jit_context
*ctxt
,
847 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
848 RETURN_NULL_IF_FAIL (name
, ctxt
, NULL
, "NULL name");
850 return static_cast <gcc_jit_function
*> (ctxt
->get_builtin_function (name
));
853 /* Public entrypoint. See description in libgccjit.h.
855 After error-checking, this calls the trivial
856 gcc::jit::recording::memento::as_object method (a function is a
857 memento), in jit-recording.h. */
860 gcc_jit_function_as_object (gcc_jit_function
*func
)
862 RETURN_NULL_IF_FAIL (func
, NULL
, NULL
, "NULL function");
864 return static_cast <gcc_jit_object
*> (func
->as_object ());
867 /* Public entrypoint. See description in libgccjit.h.
869 After error-checking, the real work is done by the
870 gcc::jit::recording::function::get_param method, in
874 gcc_jit_function_get_param (gcc_jit_function
*func
, int index
)
876 RETURN_NULL_IF_FAIL (func
, NULL
, NULL
, "NULL function");
877 gcc::jit::recording::context
*ctxt
= func
->m_ctxt
;
878 RETURN_NULL_IF_FAIL (index
>= 0, ctxt
, NULL
, "negative index");
879 int num_params
= func
->get_params ().length ();
880 RETURN_NULL_IF_FAIL_PRINTF3 (index
< num_params
,
882 "index of %d is too large (%s has %d params)",
884 func
->get_debug_string (),
887 return static_cast <gcc_jit_param
*> (func
->get_param (index
));
890 /* Public entrypoint. See description in libgccjit.h.
892 After error-checking, the real work is done by the
893 gcc::jit::recording::function::dump_to_dot method, in
897 gcc_jit_function_dump_to_dot (gcc_jit_function
*func
,
900 RETURN_IF_FAIL (func
, NULL
, NULL
, "NULL function");
901 gcc::jit::recording::context
*ctxt
= func
->m_ctxt
;
902 RETURN_IF_FAIL (path
, ctxt
, NULL
, "NULL path");
904 func
->dump_to_dot (path
);
907 /* Public entrypoint. See description in libgccjit.h.
909 After error-checking, the real work is done by the
910 gcc::jit::recording::function::new_block method, in
914 gcc_jit_function_new_block (gcc_jit_function
*func
,
917 RETURN_NULL_IF_FAIL (func
, NULL
, NULL
, "NULL function");
918 RETURN_NULL_IF_FAIL (func
->get_kind () != GCC_JIT_FUNCTION_IMPORTED
,
919 func
->get_context (), NULL
,
920 "cannot add block to an imported function");
921 /* name can be NULL. */
923 return (gcc_jit_block
*)func
->new_block (name
);
926 /* Public entrypoint. See description in libgccjit.h.
928 After error-checking, this calls the trivial
929 gcc::jit::recording::memento::as_object method (a block is a
930 memento), in jit-recording.h. */
933 gcc_jit_block_as_object (gcc_jit_block
*block
)
935 RETURN_NULL_IF_FAIL (block
, NULL
, NULL
, "NULL block");
937 return static_cast <gcc_jit_object
*> (block
->as_object ());
940 /* Public entrypoint. See description in libgccjit.h.
942 After error-checking, the real work is done by the
943 gcc::jit::recording::block::get_function method, in
947 gcc_jit_block_get_function (gcc_jit_block
*block
)
949 RETURN_NULL_IF_FAIL (block
, NULL
, NULL
, "NULL block");
951 return static_cast <gcc_jit_function
*> (block
->get_function ());
954 /* Public entrypoint. See description in libgccjit.h.
956 After error-checking, the real work is done by the
957 gcc::jit::recording::context::new_global method, in
961 gcc_jit_context_new_global (gcc_jit_context
*ctxt
,
962 gcc_jit_location
*loc
,
966 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
967 /* LOC can be NULL. */
968 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
969 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
971 return (gcc_jit_lvalue
*)ctxt
->new_global (loc
, type
, name
);
974 /* Public entrypoint. See description in libgccjit.h.
976 After error-checking, this calls the trivial
977 gcc::jit::recording::memento::as_object method (an lvalue is a
978 memento), in jit-recording.h. */
981 gcc_jit_lvalue_as_object (gcc_jit_lvalue
*lvalue
)
983 RETURN_NULL_IF_FAIL (lvalue
, NULL
, NULL
, "NULL lvalue");
985 return static_cast <gcc_jit_object
*> (lvalue
->as_object ());
988 /* Public entrypoint. See description in libgccjit.h.
990 After error-checking, this calls the trivial
991 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
994 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue
*lvalue
)
996 RETURN_NULL_IF_FAIL (lvalue
, NULL
, NULL
, "NULL lvalue");
998 return (gcc_jit_rvalue
*)lvalue
->as_rvalue ();
1001 /* Public entrypoint. See description in libgccjit.h.
1003 After error-checking, this calls the trivial
1004 gcc::jit::recording::memento::as_object method (an rvalue is a
1005 memento), in jit-recording.h. */
1008 gcc_jit_rvalue_as_object (gcc_jit_rvalue
*rvalue
)
1010 RETURN_NULL_IF_FAIL (rvalue
, NULL
, NULL
, "NULL rvalue");
1012 return static_cast <gcc_jit_object
*> (rvalue
->as_object ());
1015 /* Public entrypoint. See description in libgccjit.h.
1017 After error-checking, the real work is done by the
1018 gcc::jit::recording::rvalue::get_type method, in
1022 gcc_jit_rvalue_get_type (gcc_jit_rvalue
*rvalue
)
1024 RETURN_NULL_IF_FAIL (rvalue
, NULL
, NULL
, "NULL rvalue");
1026 return static_cast <gcc_jit_type
*> (rvalue
->get_type ());
1029 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1030 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1031 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1033 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1034 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1035 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1036 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1037 "not a numeric type: %s", \
1038 NUMERIC_TYPE->get_debug_string ());
1040 /* Public entrypoint. See description in libgccjit.h.
1042 After error-checking, the real work is done by the
1043 gcc::jit::recording::context::new_rvalue_from_int method in
1047 gcc_jit_context_new_rvalue_from_int (gcc_jit_context
*ctxt
,
1048 gcc_jit_type
*numeric_type
,
1051 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1052 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1054 return (gcc_jit_rvalue
*)ctxt
->new_rvalue_from_int (numeric_type
, value
);
1057 /* Public entrypoint. See description in libgccjit.h.
1059 This is essentially equivalent to:
1060 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1061 albeit with slightly different error messages if an error occurs. */
1064 gcc_jit_context_zero (gcc_jit_context
*ctxt
,
1065 gcc_jit_type
*numeric_type
)
1067 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1068 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1070 return gcc_jit_context_new_rvalue_from_int (ctxt
, numeric_type
, 0);
1073 /* Public entrypoint. See description in libgccjit.h.
1075 This is essentially equivalent to:
1076 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1077 albeit with slightly different error messages if an error occurs. */
1080 gcc_jit_context_one (gcc_jit_context
*ctxt
,
1081 gcc_jit_type
*numeric_type
)
1083 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1084 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1086 return gcc_jit_context_new_rvalue_from_int (ctxt
, numeric_type
, 1);
1089 /* Public entrypoint. See description in libgccjit.h.
1091 After error-checking, the real work is done by the
1092 gcc::jit::recording::context::new_rvalue_from_double method in
1096 gcc_jit_context_new_rvalue_from_double (gcc_jit_context
*ctxt
,
1097 gcc_jit_type
*numeric_type
,
1100 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1101 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt
, numeric_type
);
1103 return (gcc_jit_rvalue
*)ctxt
->new_rvalue_from_double (numeric_type
, value
);
1106 /* Public entrypoint. See description in libgccjit.h.
1108 After error-checking, the real work is done by the
1109 gcc::jit::recording::context::new_rvalue_from_ptr method in
1113 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context
*ctxt
,
1114 gcc_jit_type
*pointer_type
,
1117 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1118 RETURN_NULL_IF_FAIL (pointer_type
, ctxt
, NULL
, "NULL type");
1119 RETURN_NULL_IF_FAIL_PRINTF1 (
1120 pointer_type
->is_pointer (),
1122 "not a pointer type (type: %s)",
1123 pointer_type
->get_debug_string ());
1125 return (gcc_jit_rvalue
*)ctxt
->new_rvalue_from_ptr (pointer_type
, value
);
1128 /* Public entrypoint. See description in libgccjit.h.
1130 This is essentially equivalent to:
1131 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1132 albeit with slightly different error messages if an error occurs. */
1135 gcc_jit_context_null (gcc_jit_context
*ctxt
,
1136 gcc_jit_type
*pointer_type
)
1138 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1139 RETURN_NULL_IF_FAIL (pointer_type
, ctxt
, NULL
, "NULL type");
1140 RETURN_NULL_IF_FAIL_PRINTF1 (
1141 pointer_type
->is_pointer (),
1143 "not a pointer type (type: %s)",
1144 pointer_type
->get_debug_string ());
1146 return gcc_jit_context_new_rvalue_from_ptr (ctxt
, pointer_type
, NULL
);
1149 /* Public entrypoint. See description in libgccjit.h.
1151 After error-checking, the real work is done by the
1152 gcc::jit::recording::context::new_string_literal method in
1156 gcc_jit_context_new_string_literal (gcc_jit_context
*ctxt
,
1159 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1160 RETURN_NULL_IF_FAIL (value
, ctxt
, NULL
, "NULL value");
1162 return (gcc_jit_rvalue
*)ctxt
->new_string_literal (value
);
1165 /* Public entrypoint. See description in libgccjit.h.
1167 After error-checking, the real work is done by the
1168 gcc::jit::recording::context::new_unary_op method in
1172 gcc_jit_context_new_unary_op (gcc_jit_context
*ctxt
,
1173 gcc_jit_location
*loc
,
1174 enum gcc_jit_unary_op op
,
1175 gcc_jit_type
*result_type
,
1176 gcc_jit_rvalue
*rvalue
)
1178 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1179 /* LOC can be NULL. */
1180 RETURN_NULL_IF_FAIL_PRINTF1 (
1181 (op
>= GCC_JIT_UNARY_OP_MINUS
1182 && op
<= GCC_JIT_UNARY_OP_LOGICAL_NEGATE
),
1184 "unrecognized value for enum gcc_jit_unary_op: %i",
1186 RETURN_NULL_IF_FAIL (result_type
, ctxt
, loc
, "NULL result_type");
1187 RETURN_NULL_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1189 return (gcc_jit_rvalue
*)ctxt
->new_unary_op (loc
, op
, result_type
, rvalue
);
1192 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1193 For use by both gcc_jit_context_new_binary_op and
1194 gcc_jit_block_add_assignment_op. */
1197 valid_binary_op_p (enum gcc_jit_binary_op op
)
1199 return (op
>= GCC_JIT_BINARY_OP_PLUS
1200 && op
<= GCC_JIT_BINARY_OP_RSHIFT
);
1203 /* Public entrypoint. See description in libgccjit.h.
1205 After error-checking, the real work is done by the
1206 gcc::jit::recording::context::new_binary_op method in
1210 gcc_jit_context_new_binary_op (gcc_jit_context
*ctxt
,
1211 gcc_jit_location
*loc
,
1212 enum gcc_jit_binary_op op
,
1213 gcc_jit_type
*result_type
,
1214 gcc_jit_rvalue
*a
, gcc_jit_rvalue
*b
)
1216 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1217 /* LOC can be NULL. */
1218 RETURN_NULL_IF_FAIL_PRINTF1 (
1219 valid_binary_op_p (op
),
1221 "unrecognized value for enum gcc_jit_binary_op: %i",
1223 RETURN_NULL_IF_FAIL (result_type
, ctxt
, loc
, "NULL result_type");
1224 RETURN_NULL_IF_FAIL (a
, ctxt
, loc
, "NULL a");
1225 RETURN_NULL_IF_FAIL (b
, ctxt
, loc
, "NULL b");
1226 RETURN_NULL_IF_FAIL_PRINTF4 (
1227 a
->get_type () == b
->get_type (),
1229 "mismatching types for binary op:"
1230 " a: %s (type: %s) b: %s (type: %s)",
1231 a
->get_debug_string (),
1232 a
->get_type ()->get_debug_string (),
1233 b
->get_debug_string (),
1234 b
->get_type ()->get_debug_string ());
1236 return (gcc_jit_rvalue
*)ctxt
->new_binary_op (loc
, op
, result_type
, a
, b
);
1239 /* Public entrypoint. See description in libgccjit.h.
1241 After error-checking, the real work is done by the
1242 gcc::jit::recording::context::new_comparison method in
1246 gcc_jit_context_new_comparison (gcc_jit_context
*ctxt
,
1247 gcc_jit_location
*loc
,
1248 enum gcc_jit_comparison op
,
1249 gcc_jit_rvalue
*a
, gcc_jit_rvalue
*b
)
1251 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1252 /* LOC can be NULL. */
1253 RETURN_NULL_IF_FAIL_PRINTF1 (
1254 (op
>= GCC_JIT_COMPARISON_EQ
1255 && op
<= GCC_JIT_COMPARISON_GE
),
1257 "unrecognized value for enum gcc_jit_comparison: %i",
1259 RETURN_NULL_IF_FAIL (a
, ctxt
, loc
, "NULL a");
1260 RETURN_NULL_IF_FAIL (b
, ctxt
, loc
, "NULL b");
1261 RETURN_NULL_IF_FAIL_PRINTF4 (
1262 a
->get_type ()->unqualified () == b
->get_type ()->unqualified (),
1264 "mismatching types for comparison:"
1265 " a: %s (type: %s) b: %s (type: %s)",
1266 a
->get_debug_string (),
1267 a
->get_type ()->get_debug_string (),
1268 b
->get_debug_string (),
1269 b
->get_type ()->get_debug_string ());
1271 return (gcc_jit_rvalue
*)ctxt
->new_comparison (loc
, op
, a
, b
);
1274 /* Public entrypoint. See description in libgccjit.h.
1276 After error-checking, the real work is done by the
1277 gcc::jit::recording::context::new_call method in
1281 gcc_jit_context_new_call (gcc_jit_context
*ctxt
,
1282 gcc_jit_location
*loc
,
1283 gcc_jit_function
*func
,
1284 int numargs
, gcc_jit_rvalue
**args
)
1286 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1287 /* LOC can be NULL. */
1288 RETURN_NULL_IF_FAIL (func
, ctxt
, loc
, "NULL function");
1290 RETURN_NULL_IF_FAIL (args
, ctxt
, loc
, "NULL args");
1292 int min_num_params
= func
->get_params ().length ();
1293 bool is_variadic
= func
->is_variadic ();
1295 RETURN_NULL_IF_FAIL_PRINTF3 (
1296 numargs
>= min_num_params
,
1298 "not enough arguments to function \"%s\""
1299 " (got %i args, expected %i)",
1300 func
->get_name ()->c_str (),
1301 numargs
, min_num_params
);
1303 RETURN_NULL_IF_FAIL_PRINTF3 (
1304 (numargs
== min_num_params
|| is_variadic
),
1306 "too many arguments to function \"%s\""
1307 " (got %i args, expected %i)",
1308 func
->get_name ()->c_str (),
1309 numargs
, min_num_params
);
1311 for (int i
= 0; i
< min_num_params
; i
++)
1313 gcc::jit::recording::param
*param
= func
->get_param (i
);
1314 gcc_jit_rvalue
*arg
= args
[i
];
1316 RETURN_NULL_IF_FAIL_PRINTF4 (
1319 "NULL argument %i to function \"%s\":"
1320 " param %s (type: %s)",
1322 func
->get_name ()->c_str (),
1323 param
->get_debug_string (),
1324 param
->get_type ()->get_debug_string ());
1326 RETURN_NULL_IF_FAIL_PRINTF6 (
1327 compatible_types (param
->get_type (),
1330 "mismatching types for argument %d of function \"%s\":"
1331 " assignment to param %s (type: %s) from %s (type: %s)",
1333 func
->get_name ()->c_str (),
1334 param
->get_debug_string (),
1335 param
->get_type ()->get_debug_string (),
1336 arg
->get_debug_string (),
1337 arg
->get_type ()->get_debug_string ());
1340 return (gcc_jit_rvalue
*)ctxt
->new_call (loc
,
1343 (gcc::jit::recording::rvalue
**)args
);
1346 /* Public entrypoint. See description in libgccjit.h.
1348 After error-checking, the real work is done by the
1349 gcc::jit::recording::context::new_call_through_ptr method in
1353 gcc_jit_context_new_call_through_ptr (gcc_jit_context
*ctxt
,
1354 gcc_jit_location
*loc
,
1355 gcc_jit_rvalue
*fn_ptr
,
1356 int numargs
, gcc_jit_rvalue
**args
)
1358 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1359 /* LOC can be NULL. */
1360 RETURN_NULL_IF_FAIL (fn_ptr
, ctxt
, loc
, "NULL fn_ptr");
1362 RETURN_NULL_IF_FAIL (args
, ctxt
, loc
, "NULL args");
1364 gcc::jit::recording::type
*ptr_type
= fn_ptr
->get_type ()->dereference ();
1365 RETURN_NULL_IF_FAIL_PRINTF2 (
1366 ptr_type
, ctxt
, loc
,
1367 "fn_ptr is not a ptr: %s"
1369 fn_ptr
->get_debug_string (),
1370 fn_ptr
->get_type ()->get_debug_string ());
1372 gcc::jit::recording::function_type
*fn_type
=
1373 ptr_type
->dyn_cast_function_type();
1374 RETURN_NULL_IF_FAIL_PRINTF2 (
1376 "fn_ptr is not a function ptr: %s"
1378 fn_ptr
->get_debug_string (),
1379 fn_ptr
->get_type ()->get_debug_string ());
1381 int min_num_params
= fn_type
->get_param_types ().length ();
1382 bool is_variadic
= fn_type
->is_variadic ();
1384 RETURN_NULL_IF_FAIL_PRINTF3 (
1385 numargs
>= min_num_params
,
1387 "not enough arguments to fn_ptr: %s"
1388 " (got %i args, expected %i)",
1389 fn_ptr
->get_debug_string (),
1390 numargs
, min_num_params
);
1392 RETURN_NULL_IF_FAIL_PRINTF3 (
1393 (numargs
== min_num_params
|| is_variadic
),
1395 "too many arguments to fn_ptr: %s"
1396 " (got %i args, expected %i)",
1397 fn_ptr
->get_debug_string (),
1398 numargs
, min_num_params
);
1400 for (int i
= 0; i
< min_num_params
; i
++)
1402 gcc::jit::recording::type
*param_type
= fn_type
->get_param_types ()[i
];
1403 gcc_jit_rvalue
*arg
= args
[i
];
1405 RETURN_NULL_IF_FAIL_PRINTF3 (
1408 "NULL argument %i to fn_ptr: %s"
1411 fn_ptr
->get_debug_string (),
1412 param_type
->get_debug_string ());
1414 RETURN_NULL_IF_FAIL_PRINTF6 (
1415 compatible_types (param_type
,
1418 "mismatching types for argument %d of fn_ptr: %s:"
1419 " assignment to param %d (type: %s) from %s (type: %s)",
1421 fn_ptr
->get_debug_string (),
1423 param_type
->get_debug_string (),
1424 arg
->get_debug_string (),
1425 arg
->get_type ()->get_debug_string ());
1428 return (gcc_jit_rvalue
*)(
1429 ctxt
->new_call_through_ptr (loc
,
1432 (gcc::jit::recording::rvalue
**)args
));
1435 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1436 to DST_TYPE, for use by gcc_jit_context_new_cast.
1438 We only permit these kinds of cast:
1442 P* <-> Q* for pointer types P and Q. */
1445 is_valid_cast (gcc::jit::recording::type
*src_type
,
1446 gcc_jit_type
*dst_type
)
1448 bool src_is_int
= src_type
->is_int ();
1449 bool dst_is_int
= dst_type
->is_int ();
1450 bool src_is_float
= src_type
->is_float ();
1451 bool dst_is_float
= dst_type
->is_float ();
1452 bool src_is_bool
= src_type
->is_bool ();
1453 bool dst_is_bool
= dst_type
->is_bool ();
1456 if (dst_is_int
|| dst_is_float
|| dst_is_bool
)
1460 if (dst_is_int
|| dst_is_float
)
1464 if (dst_is_int
|| dst_is_bool
)
1467 /* Permit casts between pointer types. */
1468 gcc::jit::recording::type
*deref_src_type
= src_type
->is_pointer ();
1469 gcc::jit::recording::type
*deref_dst_type
= dst_type
->is_pointer ();
1470 if (deref_src_type
&& deref_dst_type
)
1476 /* Public entrypoint. See description in libgccjit.h.
1478 After error-checking, the real work is done by the
1479 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1482 gcc_jit_context_new_cast (gcc_jit_context
*ctxt
,
1483 gcc_jit_location
*loc
,
1484 gcc_jit_rvalue
*rvalue
,
1487 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1488 /* LOC can be NULL. */
1489 RETURN_NULL_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1490 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
1491 RETURN_NULL_IF_FAIL_PRINTF3 (
1492 is_valid_cast (rvalue
->get_type (), type
),
1494 "cannot cast %s from type: %s to type: %s",
1495 rvalue
->get_debug_string (),
1496 rvalue
->get_type ()->get_debug_string (),
1497 type
->get_debug_string ());
1499 return static_cast <gcc_jit_rvalue
*> (ctxt
->new_cast (loc
, rvalue
, type
));
1502 /* Public entrypoint. See description in libgccjit.h.
1504 After error-checking, the real work is done by the
1505 gcc::jit::recording::context::new_array_access method in
1508 extern gcc_jit_lvalue
*
1509 gcc_jit_context_new_array_access (gcc_jit_context
*ctxt
,
1510 gcc_jit_location
*loc
,
1511 gcc_jit_rvalue
*ptr
,
1512 gcc_jit_rvalue
*index
)
1514 RETURN_NULL_IF_FAIL (ctxt
, NULL
, loc
, "NULL context");
1515 /* LOC can be NULL. */
1516 RETURN_NULL_IF_FAIL (ptr
, ctxt
, loc
, "NULL ptr");
1517 RETURN_NULL_IF_FAIL (index
, ctxt
, loc
, "NULL index");
1518 RETURN_NULL_IF_FAIL_PRINTF2 (
1519 ptr
->get_type ()->dereference (),
1521 "ptr: %s (type: %s) is not a pointer or array",
1522 ptr
->get_debug_string (),
1523 ptr
->get_type ()->get_debug_string ());
1524 RETURN_NULL_IF_FAIL_PRINTF2 (
1525 index
->get_type ()->is_numeric (),
1527 "index: %s (type: %s) is not of numeric type",
1528 index
->get_debug_string (),
1529 index
->get_type ()->get_debug_string ());
1531 return (gcc_jit_lvalue
*)ctxt
->new_array_access (loc
, ptr
, index
);
1534 /* Public entrypoint. See description in libgccjit.h.
1536 After error-checking, the real work is done by the
1537 gcc::jit::recording::memento::get_context method in
1541 gcc_jit_object_get_context (gcc_jit_object
*obj
)
1543 RETURN_NULL_IF_FAIL (obj
, NULL
, NULL
, "NULL object");
1545 return static_cast <gcc_jit_context
*> (obj
->get_context ());
1548 /* Public entrypoint. See description in libgccjit.h.
1550 After error-checking, the real work is done by the
1551 gcc::jit::recording::memento::get_debug_string method in
1555 gcc_jit_object_get_debug_string (gcc_jit_object
*obj
)
1557 RETURN_NULL_IF_FAIL (obj
, NULL
, NULL
, "NULL object");
1559 return obj
->get_debug_string ();
1562 /* Public entrypoint. See description in libgccjit.h.
1564 After error-checking, the real work is done by the
1565 gcc::jit::recording::lvalue::access_field method in
1569 gcc_jit_lvalue_access_field (gcc_jit_lvalue
*struct_
,
1570 gcc_jit_location
*loc
,
1571 gcc_jit_field
*field
)
1573 RETURN_NULL_IF_FAIL (struct_
, NULL
, loc
, "NULL struct");
1574 /* LOC can be NULL. */
1575 gcc::jit::recording::context
*ctxt
= struct_
->m_ctxt
;
1576 RETURN_NULL_IF_FAIL (field
, ctxt
, loc
, "NULL field");
1577 RETURN_NULL_IF_FAIL_PRINTF1 (field
->get_container (), field
->m_ctxt
, loc
,
1578 "field %s has not been placed in a struct",
1579 field
->get_debug_string ());
1581 return (gcc_jit_lvalue
*)struct_
->access_field (loc
, field
);
1584 /* Public entrypoint. See description in libgccjit.h.
1586 After error-checking, the real work is done by the
1587 gcc::jit::recording::rvalue::access_field method in
1591 gcc_jit_rvalue_access_field (gcc_jit_rvalue
*struct_
,
1592 gcc_jit_location
*loc
,
1593 gcc_jit_field
*field
)
1595 RETURN_NULL_IF_FAIL (struct_
, NULL
, loc
, "NULL struct");
1596 /* LOC can be NULL. */
1597 gcc::jit::recording::context
*ctxt
= struct_
->m_ctxt
;
1598 RETURN_NULL_IF_FAIL (field
, ctxt
, loc
, "NULL field");
1599 RETURN_NULL_IF_FAIL_PRINTF1 (field
->get_container (), field
->m_ctxt
, loc
,
1600 "field %s has not been placed in a struct",
1601 field
->get_debug_string ());
1603 return (gcc_jit_rvalue
*)struct_
->access_field (loc
, field
);
1606 /* Public entrypoint. See description in libgccjit.h.
1608 After error-checking, the real work is done by the
1609 gcc::jit::recording::rvalue::deference_field method in
1613 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue
*ptr
,
1614 gcc_jit_location
*loc
,
1615 gcc_jit_field
*field
)
1617 RETURN_NULL_IF_FAIL (ptr
, NULL
, loc
, "NULL ptr");
1618 /* LOC can be NULL. */
1619 RETURN_NULL_IF_FAIL (field
, NULL
, loc
, "NULL field");
1620 gcc::jit::recording::type
*underlying_type
=
1621 ptr
->get_type ()->is_pointer ();
1622 RETURN_NULL_IF_FAIL_PRINTF1 (field
->get_container (), field
->m_ctxt
, loc
,
1623 "field %s has not been placed in a struct",
1624 field
->get_debug_string ());
1625 RETURN_NULL_IF_FAIL_PRINTF3 (
1628 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1629 ptr
->get_debug_string (),
1630 ptr
->get_type ()->get_debug_string (),
1631 field
->get_debug_string ());
1632 RETURN_NULL_IF_FAIL_PRINTF2 (
1633 (field
->get_container ()->unqualified ()
1634 == underlying_type
->unqualified ()),
1636 "%s is not a field of %s",
1637 field
->get_debug_string (),
1638 underlying_type
->get_debug_string ());
1640 return (gcc_jit_lvalue
*)ptr
->dereference_field (loc
, field
);
1643 /* Public entrypoint. See description in libgccjit.h.
1645 After error-checking, the real work is done by the
1646 gcc::jit::recording::rvalue::deference method in
1650 gcc_jit_rvalue_dereference (gcc_jit_rvalue
*rvalue
,
1651 gcc_jit_location
*loc
)
1653 RETURN_NULL_IF_FAIL (rvalue
, NULL
, loc
, "NULL rvalue");
1654 /* LOC can be NULL. */
1656 gcc::jit::recording::type
*underlying_type
=
1657 rvalue
->get_type ()->is_pointer ();
1659 RETURN_NULL_IF_FAIL_PRINTF2 (
1661 rvalue
->m_ctxt
, loc
,
1662 "dereference of non-pointer %s (type: %s)",
1663 rvalue
->get_debug_string (),
1664 rvalue
->get_type ()->get_debug_string ());
1666 return (gcc_jit_lvalue
*)rvalue
->dereference (loc
);
1669 /* Public entrypoint. See description in libgccjit.h.
1671 After error-checking, the real work is done by the
1672 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1675 gcc_jit_lvalue_get_address (gcc_jit_lvalue
*lvalue
,
1676 gcc_jit_location
*loc
)
1678 RETURN_NULL_IF_FAIL (lvalue
, NULL
, loc
, "NULL lvalue");
1679 /* LOC can be NULL. */
1681 return (gcc_jit_rvalue
*)lvalue
->get_address (loc
);
1684 /* Public entrypoint. See description in libgccjit.h.
1686 After error-checking, the real work is done by the
1687 gcc::jit::recording::function::new_local method in jit-recording.c. */
1690 gcc_jit_function_new_local (gcc_jit_function
*func
,
1691 gcc_jit_location
*loc
,
1695 RETURN_NULL_IF_FAIL (func
, NULL
, loc
, "NULL function");
1696 /* LOC can be NULL. */
1697 gcc::jit::recording::context
*ctxt
= func
->m_ctxt
;
1698 RETURN_NULL_IF_FAIL (func
->get_kind () != GCC_JIT_FUNCTION_IMPORTED
,
1700 "Cannot add locals to an imported function");
1701 RETURN_NULL_IF_FAIL (type
, ctxt
, loc
, "NULL type");
1702 RETURN_NULL_IF_FAIL (name
, ctxt
, loc
, "NULL name");
1704 return (gcc_jit_lvalue
*)func
->new_local (loc
, type
, name
);
1707 /* Public entrypoint. See description in libgccjit.h.
1709 After error-checking, the real work is done by the
1710 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1713 gcc_jit_block_add_eval (gcc_jit_block
*block
,
1714 gcc_jit_location
*loc
,
1715 gcc_jit_rvalue
*rvalue
)
1717 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1718 /* LOC can be NULL. */
1719 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1720 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1722 return block
->add_eval (loc
, rvalue
);
1725 /* Public entrypoint. See description in libgccjit.h.
1727 After error-checking, the real work is done by the
1728 gcc::jit::recording::block::add_assignment method in
1732 gcc_jit_block_add_assignment (gcc_jit_block
*block
,
1733 gcc_jit_location
*loc
,
1734 gcc_jit_lvalue
*lvalue
,
1735 gcc_jit_rvalue
*rvalue
)
1737 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1738 /* LOC can be NULL. */
1739 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1740 RETURN_IF_FAIL (lvalue
, ctxt
, loc
, "NULL lvalue");
1741 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1742 RETURN_IF_FAIL_PRINTF4 (
1743 compatible_types (lvalue
->get_type (),
1744 rvalue
->get_type ()),
1746 "mismatching types:"
1747 " assignment to %s (type: %s) from %s (type: %s)",
1748 lvalue
->get_debug_string (),
1749 lvalue
->get_type ()->get_debug_string (),
1750 rvalue
->get_debug_string (),
1751 rvalue
->get_type ()->get_debug_string ());
1753 return block
->add_assignment (loc
, lvalue
, rvalue
);
1756 /* Public entrypoint. See description in libgccjit.h.
1758 After error-checking, the real work is done by the
1759 gcc::jit::recording::block::add_assignment_op method in
1763 gcc_jit_block_add_assignment_op (gcc_jit_block
*block
,
1764 gcc_jit_location
*loc
,
1765 gcc_jit_lvalue
*lvalue
,
1766 enum gcc_jit_binary_op op
,
1767 gcc_jit_rvalue
*rvalue
)
1769 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1770 /* LOC can be NULL. */
1771 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1772 RETURN_IF_FAIL (lvalue
, ctxt
, loc
, "NULL lvalue");
1773 RETURN_IF_FAIL_PRINTF1 (
1774 valid_binary_op_p (op
),
1776 "unrecognized value for enum gcc_jit_binary_op: %i",
1778 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1780 return block
->add_assignment_op (loc
, lvalue
, op
, rvalue
);
1783 /* Internal helper function for determining if rvalue BOOLVAL is of
1784 boolean type. For use by gcc_jit_block_end_with_conditional. */
1787 is_bool (gcc_jit_rvalue
*boolval
)
1789 gcc::jit::recording::type
*actual_type
= boolval
->get_type ();
1790 gcc::jit::recording::type
*bool_type
=
1791 boolval
->m_ctxt
->get_type (GCC_JIT_TYPE_BOOL
);
1792 return actual_type
== bool_type
;
1795 /* Public entrypoint. See description in libgccjit.h.
1797 After error-checking, the real work is done by the
1798 gcc::jit::recording::block::end_with_conditional method in
1802 gcc_jit_block_end_with_conditional (gcc_jit_block
*block
,
1803 gcc_jit_location
*loc
,
1804 gcc_jit_rvalue
*boolval
,
1805 gcc_jit_block
*on_true
,
1806 gcc_jit_block
*on_false
)
1808 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1809 /* LOC can be NULL. */
1810 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1811 RETURN_IF_FAIL (boolval
, ctxt
, loc
, "NULL boolval");
1812 RETURN_IF_FAIL_PRINTF2 (
1813 is_bool (boolval
), ctxt
, loc
,
1814 "%s (type: %s) is not of boolean type ",
1815 boolval
->get_debug_string (),
1816 boolval
->get_type ()->get_debug_string ());
1817 RETURN_IF_FAIL (on_true
, ctxt
, loc
, "NULL on_true");
1818 RETURN_IF_FAIL (on_true
, ctxt
, loc
, "NULL on_false");
1819 RETURN_IF_FAIL_PRINTF4 (
1820 block
->get_function () == on_true
->get_function (),
1822 "\"on_true\" block is not in same function:"
1823 " source block %s is in function %s"
1824 " whereas target block %s is in function %s",
1825 block
->get_debug_string (),
1826 block
->get_function ()->get_debug_string (),
1827 on_true
->get_debug_string (),
1828 on_true
->get_function ()->get_debug_string ());
1829 RETURN_IF_FAIL_PRINTF4 (
1830 block
->get_function () == on_false
->get_function (),
1832 "\"on_false\" block is not in same function:"
1833 " source block %s is in function %s"
1834 " whereas target block %s is in function %s",
1835 block
->get_debug_string (),
1836 block
->get_function ()->get_debug_string (),
1837 on_false
->get_debug_string (),
1838 on_false
->get_function ()->get_debug_string ());
1840 return block
->end_with_conditional (loc
, boolval
, on_true
, on_false
);
1843 /* Public entrypoint. See description in libgccjit.h.
1845 After error-checking, the real work is done by the
1846 gcc::jit::recording::block::add_comment method in
1850 gcc_jit_block_add_comment (gcc_jit_block
*block
,
1851 gcc_jit_location
*loc
,
1854 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1855 /* LOC can be NULL. */
1856 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1857 RETURN_IF_FAIL (text
, ctxt
, loc
, "NULL text");
1859 block
->add_comment (loc
, text
);
1862 /* Public entrypoint. See description in libgccjit.h.
1864 After error-checking, the real work is done by the
1865 gcc::jit::recording::block::end_with_jump method in
1869 gcc_jit_block_end_with_jump (gcc_jit_block
*block
,
1870 gcc_jit_location
*loc
,
1871 gcc_jit_block
*target
)
1873 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1874 /* LOC can be NULL. */
1875 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1876 RETURN_IF_FAIL (target
, ctxt
, loc
, "NULL target");
1877 RETURN_IF_FAIL_PRINTF4 (
1878 block
->get_function () == target
->get_function (),
1880 "target block is not in same function:"
1881 " source block %s is in function %s"
1882 " whereas target block %s is in function %s",
1883 block
->get_debug_string (),
1884 block
->get_function ()->get_debug_string (),
1885 target
->get_debug_string (),
1886 target
->get_function ()->get_debug_string ());
1888 block
->end_with_jump (loc
, target
);
1891 /* Public entrypoint. See description in libgccjit.h.
1893 After error-checking, the real work is done by the
1894 gcc::jit::recording::block::end_with_return method in
1898 gcc_jit_block_end_with_return (gcc_jit_block
*block
,
1899 gcc_jit_location
*loc
,
1900 gcc_jit_rvalue
*rvalue
)
1902 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1903 /* LOC can be NULL. */
1904 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1905 gcc::jit::recording::function
*func
= block
->get_function ();
1906 RETURN_IF_FAIL (rvalue
, ctxt
, loc
, "NULL rvalue");
1907 RETURN_IF_FAIL_PRINTF4 (
1909 func
->get_return_type (),
1910 rvalue
->get_type ()),
1912 "mismatching types:"
1913 " return of %s (type: %s) in function %s (return type: %s)",
1914 rvalue
->get_debug_string (),
1915 rvalue
->get_type ()->get_debug_string (),
1916 func
->get_debug_string (),
1917 func
->get_return_type ()->get_debug_string ());
1919 return block
->end_with_return (loc
, rvalue
);
1922 /* Public entrypoint. See description in libgccjit.h.
1924 After error-checking, the real work is done by the
1925 gcc::jit::recording::block::end_with_return method in
1929 gcc_jit_block_end_with_void_return (gcc_jit_block
*block
,
1930 gcc_jit_location
*loc
)
1932 RETURN_IF_NOT_VALID_BLOCK (block
, loc
);
1933 /* LOC can be NULL. */
1934 gcc::jit::recording::context
*ctxt
= block
->get_context ();
1935 gcc::jit::recording::function
*func
= block
->get_function ();
1936 RETURN_IF_FAIL_PRINTF2 (
1937 func
->get_return_type () == ctxt
->get_type (GCC_JIT_TYPE_VOID
),
1939 "mismatching types:"
1940 " void return in function %s (return type: %s)",
1941 func
->get_debug_string (),
1942 func
->get_return_type ()->get_debug_string ());
1944 return block
->end_with_return (loc
, NULL
);
1947 /**********************************************************************
1949 **********************************************************************/
1951 /* Public entrypoint. See description in libgccjit.h.
1953 After error-checking, the real work is done by the
1954 gcc::jit::recording::context::set_str_option method in
1958 gcc_jit_context_set_str_option (gcc_jit_context
*ctxt
,
1959 enum gcc_jit_str_option opt
,
1962 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1963 /* opt is checked by the inner function.
1964 value can be NULL. */
1966 ctxt
->set_str_option (opt
, value
);
1969 /* Public entrypoint. See description in libgccjit.h.
1971 After error-checking, the real work is done by the
1972 gcc::jit::recording::context::set_int_option method in
1976 gcc_jit_context_set_int_option (gcc_jit_context
*ctxt
,
1977 enum gcc_jit_int_option opt
,
1980 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1981 /* opt is checked by the inner function. */
1983 ctxt
->set_int_option (opt
, value
);
1986 /* Public entrypoint. See description in libgccjit.h.
1988 After error-checking, the real work is done by the
1989 gcc::jit::recording::context::set_bool_option method in
1993 gcc_jit_context_set_bool_option (gcc_jit_context
*ctxt
,
1994 enum gcc_jit_bool_option opt
,
1997 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
1998 /* opt is checked by the inner function. */
2000 ctxt
->set_bool_option (opt
, value
);
2003 /* Public entrypoint. See description in libgccjit.h.
2005 After error-checking, the real work is done by the
2006 gcc::jit::recording::context::compile method in
2010 gcc_jit_context_compile (gcc_jit_context
*ctxt
)
2012 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2014 return (gcc_jit_result
*)ctxt
->compile ();
2017 /* Public entrypoint. See description in libgccjit.h.
2019 After error-checking, the real work is done by the
2020 gcc::jit::recording::context::dump_to_file method in
2024 gcc_jit_context_dump_to_file (gcc_jit_context
*ctxt
,
2026 int update_locations
)
2028 RETURN_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2029 RETURN_IF_FAIL (path
, ctxt
, NULL
, "NULL path");
2030 ctxt
->dump_to_file (path
, update_locations
);
2033 /* Public entrypoint. See description in libgccjit.h.
2035 After error-checking, the real work is done by the
2036 gcc::jit::recording::context::get_first_error method in
2040 gcc_jit_context_get_first_error (gcc_jit_context
*ctxt
)
2042 RETURN_NULL_IF_FAIL (ctxt
, NULL
, NULL
, "NULL context");
2044 return ctxt
->get_first_error ();
2047 /* Public entrypoint. See description in libgccjit.h.
2049 After error-checking, the real work is done by the
2050 gcc::jit::playback::result::get_code method in
2054 gcc_jit_result_get_code (gcc_jit_result
*result
,
2057 RETURN_NULL_IF_FAIL (result
, NULL
, NULL
, "NULL result");
2058 RETURN_NULL_IF_FAIL (fnname
, NULL
, NULL
, "NULL fnname");
2060 return result
->get_code (fnname
);
2063 /* Public entrypoint. See description in libgccjit.h.
2065 After error-checking, this is essentially a wrapper around the
2066 destructor for gcc::jit::playback::result in jit-playback.c. */
2069 gcc_jit_result_release (gcc_jit_result
*result
)
2071 RETURN_IF_FAIL (result
, NULL
, NULL
, "NULL result");