2014-11-20 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / jit / libgccjit.c
blob7bc9209139b000a96e2b0f334ec1ac11560e3889
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)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.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) :
37 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 /**********************************************************************
86 Error-handling.
88 We try to gracefully handle API usage errors by being defensive
89 at the API boundary.
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
100 is NULL.
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
109 RETURN_IF_FAIL*,
110 with the former returning RETURN_EXPR, and
111 RETURN_NULL_IF_FAIL*
112 for the common case where a NULL value is to be returned on
113 error, and
115 (B) whether the error message is to be directly printed:
116 RETURN_*IF_FAIL
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
121 trailing semicolons.
124 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
125 JIT_BEGIN_STMT \
126 if (!(TEST_EXPR)) \
128 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
129 return (RETURN_EXPR); \
131 JIT_END_STMT
133 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
134 JIT_BEGIN_STMT \
135 if (!(TEST_EXPR)) \
137 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
138 __func__, (A0)); \
139 return (RETURN_EXPR); \
141 JIT_END_STMT
143 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
144 JIT_BEGIN_STMT \
145 if (!(TEST_EXPR)) \
147 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
148 __func__, (A0), (A1)); \
149 return (RETURN_EXPR); \
151 JIT_END_STMT
153 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
154 JIT_BEGIN_STMT \
155 if (!(TEST_EXPR)) \
157 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
158 __func__, (A0), (A1), (A2)); \
159 return (RETURN_EXPR); \
161 JIT_END_STMT
163 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
164 JIT_BEGIN_STMT \
165 if (!(TEST_EXPR)) \
167 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
168 __func__, (A0), (A1), (A2), (A3)); \
169 return (RETURN_EXPR); \
171 JIT_END_STMT
173 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
174 JIT_BEGIN_STMT \
175 if (!(TEST_EXPR)) \
177 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
178 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
179 return (RETURN_EXPR); \
181 JIT_END_STMT
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) \
202 JIT_BEGIN_STMT \
203 if (!(TEST_EXPR)) \
205 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
206 return; \
208 JIT_END_STMT
210 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
211 JIT_BEGIN_STMT \
212 if (!(TEST_EXPR)) \
214 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
215 __func__, (A0)); \
216 return; \
218 JIT_END_STMT
220 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
221 JIT_BEGIN_STMT \
222 if (!(TEST_EXPR)) \
224 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
225 __func__, (A0), (A1)); \
226 return; \
228 JIT_END_STMT
230 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
231 JIT_BEGIN_STMT \
232 if (!(TEST_EXPR)) \
234 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
235 __func__, (A0), (A1), (A2), (A3)); \
236 return; \
238 JIT_END_STMT
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) \
244 JIT_BEGIN_STMT \
245 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
246 RETURN_IF_FAIL_PRINTF2 ( \
247 !(BLOCK)->has_been_terminated (), \
248 (BLOCK)->get_context (), \
249 (LOC), \
250 "adding to terminated block: %s (already terminated by: %s)", \
251 (BLOCK)->get_debug_string (), \
252 (BLOCK)->get_last_statement ()->get_debug_string ()); \
253 JIT_END_STMT
255 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
256 fails. */
257 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
258 JIT_BEGIN_STMT \
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 (), \
263 (LOC), \
264 "adding to terminated block: %s (already terminated by: %s)", \
265 (BLOCK)->get_debug_string (), \
266 (BLOCK)->get_last_statement ()->get_debug_string ()); \
267 JIT_END_STMT
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
272 NULL. */
274 static void
275 jit_error (gcc::jit::recording::context *ctxt,
276 gcc_jit_location *loc,
277 const char *fmt, ...)
278 GNU_PRINTF(3, 4);
280 static void
281 jit_error (gcc::jit::recording::context *ctxt,
282 gcc_jit_location *loc,
283 const char *fmt, ...)
285 va_list ap;
286 va_start (ap, fmt);
288 if (ctxt)
289 ctxt->add_error_va (loc, fmt, ap);
290 else
292 /* No context? Send to stderr. */
293 vfprintf (stderr, fmt, ap);
294 fprintf (stderr, "\n");
297 va_end (ap);
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
306 LTYPE. */
308 static bool
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. */
322 gcc_jit_context *
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. */
332 void
333 gcc_jit_context_release (gcc_jit_context *ctxt)
335 delete 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. */
344 gcc_jit_context *
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. */
356 gcc_jit_location *
357 gcc_jit_context_new_location (gcc_jit_context *ctxt,
358 const char *filename,
359 int line,
360 int column)
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. */
373 gcc_jit_object *
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. */
387 gcc_jit_object *
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
399 jit-recording.c */
401 gcc_jit_type *
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),
409 ctxt, NULL,
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
416 signedness.
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. */
422 gcc_jit_type *
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
436 jit-recording.c */
438 gcc_jit_type *
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
450 jit-recording.c. */
452 gcc_jit_type *
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
464 jit-recording.c. */
466 gcc_jit_type *
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
478 jit-recording.c. */
480 gcc_jit_type *
481 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
482 gcc_jit_location *loc,
483 gcc_jit_type *element_type,
484 int num_elements)
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,
492 element_type,
493 num_elements);
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
500 jit-recording.c. */
502 gcc_jit_field *
503 gcc_jit_context_new_field (gcc_jit_context *ctxt,
504 gcc_jit_location *loc,
505 gcc_jit_type *type,
506 const char *name)
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. */
522 gcc_jit_object *
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 */
537 gcc_jit_struct *
538 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
539 gcc_jit_location *loc,
540 const char *name,
541 int num_fields,
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");
547 if (num_fields)
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 (),
554 ctxt, loc,
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,
563 num_fields,
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
572 jit-recording.c. */
574 gcc_jit_struct *
575 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
576 gcc_jit_location *loc,
577 const char *name)
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
590 jit-recording.h. */
592 gcc_jit_type *
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
604 jit-recording.c. */
606 void
607 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
608 gcc_jit_location *loc,
609 int num_fields,
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 ());
619 if (num_fields)
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 (),
626 ctxt, loc,
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 */
643 gcc_jit_type *
644 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
645 gcc_jit_location *loc,
646 const char *name,
647 int num_fields,
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");
653 if (num_fields)
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 (),
660 ctxt, loc,
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,
669 num_fields,
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 */
680 gcc_jit_type *
681 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
682 gcc_jit_location *loc,
683 gcc_jit_type *return_type,
684 int num_params,
685 gcc_jit_type **param_types,
686 int is_variadic)
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,
693 ctxt, loc,
694 "NULL param_types creating function pointer type");
695 for (int i = 0; i < num_params; i++)
696 RETURN_NULL_IF_FAIL_PRINTF1 (
697 param_types[i],
698 ctxt, loc,
699 "NULL parameter type %i creating function pointer type", i);
701 return (gcc_jit_type*)
702 ctxt->new_function_ptr_type (loc, return_type,
703 num_params,
704 (gcc::jit::recording::type **)param_types,
705 is_variadic);
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 */
715 gcc_jit_param *
716 gcc_jit_context_new_param (gcc_jit_context *ctxt,
717 gcc_jit_location *loc,
718 gcc_jit_type *type,
719 const char *name)
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. */
735 gcc_jit_object *
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. */
748 gcc_jit_lvalue *
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. */
762 gcc_jit_rvalue *
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
774 jit-recording.c. */
776 gcc_jit_function *
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,
781 const char *name,
782 int num_params,
783 gcc_jit_param **params,
784 int is_variadic)
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)),
791 ctxt, loc,
792 "unrecognized value for enum gcc_jit_function_kind: %i",
793 kind);
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
800 mangling. */
802 /* Leading char: */
803 char ch = *name;
804 RETURN_NULL_IF_FAIL_PRINTF2 (
805 ISALPHA (ch) || ch == '_',
806 ctxt, loc,
807 "name \"%s\" contains invalid character: '%c'",
808 name, ch);
809 /* Subsequent chars: */
810 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
812 RETURN_NULL_IF_FAIL_PRINTF2 (
813 ISALNUM (ch) || ch == '_',
814 ctxt, loc,
815 "name \"%s\" contains invalid character: '%c'",
816 name, ch);
819 RETURN_NULL_IF_FAIL_PRINTF1 (
820 (num_params == 0) || params,
821 ctxt, loc,
822 "NULL params creating function %s", name);
823 for (int i = 0; i < num_params; i++)
824 RETURN_NULL_IF_FAIL_PRINTF2 (
825 params[i],
826 ctxt, loc,
827 "NULL parameter %i creating function %s", i, name);
829 return (gcc_jit_function*)
830 ctxt->new_function (loc, kind, return_type, name,
831 num_params,
832 (gcc::jit::recording::param **)params,
833 is_variadic,
834 BUILT_IN_NONE);
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
841 jit-recording.c. */
843 gcc_jit_function *
844 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
845 const char *name)
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. */
859 gcc_jit_object *
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
871 jit-recording.h. */
873 gcc_jit_param *
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,
881 ctxt, NULL,
882 "index of %d is too large (%s has %d params)",
883 index,
884 func->get_debug_string (),
885 num_params);
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
894 jit-recording.c. */
896 void
897 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
898 const char *path)
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
911 jit-recording.c. */
913 gcc_jit_block*
914 gcc_jit_function_new_block (gcc_jit_function *func,
915 const char *name)
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. */
932 gcc_jit_object *
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
944 jit-recording.h. */
946 gcc_jit_function *
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
958 jit-recording.c. */
960 gcc_jit_lvalue *
961 gcc_jit_context_new_global (gcc_jit_context *ctxt,
962 gcc_jit_location *loc,
963 gcc_jit_type *type,
964 const char *name)
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. */
980 gcc_jit_object *
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. */
993 gcc_jit_rvalue *
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. */
1007 gcc_jit_object *
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
1019 jit-recording.h. */
1021 gcc_jit_type *
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
1044 jit-recording.c. */
1046 gcc_jit_rvalue *
1047 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1048 gcc_jit_type *numeric_type,
1049 int value)
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. */
1063 gcc_jit_rvalue *
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. */
1079 gcc_jit_rvalue *
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
1093 jit-recording.c. */
1095 gcc_jit_rvalue *
1096 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1097 gcc_jit_type *numeric_type,
1098 double value)
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
1110 jit-recording.c. */
1112 gcc_jit_rvalue *
1113 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1114 gcc_jit_type *pointer_type,
1115 void *value)
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 (),
1121 ctxt, NULL,
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. */
1134 gcc_jit_rvalue *
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 (),
1142 ctxt, NULL,
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
1153 jit-recording.c. */
1155 gcc_jit_rvalue *
1156 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1157 const char *value)
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
1169 jit-recording.c. */
1171 gcc_jit_rvalue *
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),
1183 ctxt, loc,
1184 "unrecognized value for enum gcc_jit_unary_op: %i",
1185 op);
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. */
1196 static bool
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
1207 jit-recording.c. */
1209 gcc_jit_rvalue *
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),
1220 ctxt, loc,
1221 "unrecognized value for enum gcc_jit_binary_op: %i",
1222 op);
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 (),
1228 ctxt, loc,
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
1243 jit-recording.c. */
1245 gcc_jit_rvalue *
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),
1256 ctxt, loc,
1257 "unrecognized value for enum gcc_jit_comparison: %i",
1258 op);
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 (),
1263 ctxt, loc,
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
1278 jit-recording.c. */
1280 gcc_jit_rvalue *
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");
1289 if (numargs)
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,
1297 ctxt, loc,
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),
1305 ctxt, loc,
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 (
1317 arg,
1318 ctxt, loc,
1319 "NULL argument %i to function \"%s\":"
1320 " param %s (type: %s)",
1321 i + 1,
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 (),
1328 arg->get_type ()),
1329 ctxt, loc,
1330 "mismatching types for argument %d of function \"%s\":"
1331 " assignment to param %s (type: %s) from %s (type: %s)",
1332 i + 1,
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,
1341 func,
1342 numargs,
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
1350 jit-recording.c. */
1352 gcc_jit_rvalue *
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");
1361 if (numargs)
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"
1368 " type: %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 (
1375 fn_type, ctxt, loc,
1376 "fn_ptr is not a function ptr: %s"
1377 " type: %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,
1386 ctxt, loc,
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),
1394 ctxt, loc,
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 (
1406 arg,
1407 ctxt, loc,
1408 "NULL argument %i to fn_ptr: %s"
1409 " (type: %s)",
1410 i + 1,
1411 fn_ptr->get_debug_string (),
1412 param_type->get_debug_string ());
1414 RETURN_NULL_IF_FAIL_PRINTF6 (
1415 compatible_types (param_type,
1416 arg->get_type ()),
1417 ctxt, loc,
1418 "mismatching types for argument %d of fn_ptr: %s:"
1419 " assignment to param %d (type: %s) from %s (type: %s)",
1420 i + 1,
1421 fn_ptr->get_debug_string (),
1422 i + 1,
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,
1430 fn_ptr,
1431 numargs,
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:
1440 int <-> float
1441 int <-> bool
1442 P* <-> Q* for pointer types P and Q. */
1444 static bool
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 ();
1455 if (src_is_int)
1456 if (dst_is_int || dst_is_float || dst_is_bool)
1457 return true;
1459 if (src_is_float)
1460 if (dst_is_int || dst_is_float)
1461 return true;
1463 if (src_is_bool)
1464 if (dst_is_int || dst_is_bool)
1465 return true;
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)
1471 return true;
1473 return false;
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. */
1481 gcc_jit_rvalue *
1482 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1483 gcc_jit_location *loc,
1484 gcc_jit_rvalue *rvalue,
1485 gcc_jit_type *type)
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),
1493 ctxt, loc,
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
1506 jit-recording.c. */
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 (),
1520 ctxt, loc,
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 (),
1526 ctxt, loc,
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
1538 jit-recording.h. */
1540 gcc_jit_context *
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
1552 jit-recording.c. */
1554 const char *
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
1566 jit-recording.c. */
1568 gcc_jit_lvalue *
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
1588 jit-recording.c. */
1590 gcc_jit_rvalue *
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
1610 jit-recording.c. */
1612 gcc_jit_lvalue *
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 (
1626 underlying_type,
1627 ptr->m_ctxt, loc,
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 ()),
1635 ptr->m_ctxt, loc,
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
1647 jit-recording.c. */
1649 gcc_jit_lvalue *
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 (
1660 underlying_type,
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. */
1674 gcc_jit_rvalue *
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. */
1689 gcc_jit_lvalue *
1690 gcc_jit_function_new_local (gcc_jit_function *func,
1691 gcc_jit_location *loc,
1692 gcc_jit_type *type,
1693 const char *name)
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,
1699 ctxt, loc,
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. */
1712 void
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
1729 jit-recording.c. */
1731 void
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 ()),
1745 ctxt, loc,
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
1760 jit-recording.c. */
1762 void
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),
1775 ctxt, loc,
1776 "unrecognized value for enum gcc_jit_binary_op: %i",
1777 op);
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. */
1786 static bool
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
1799 jit-recording.c. */
1801 void
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 (),
1821 ctxt, loc,
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 (),
1831 ctxt, loc,
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
1847 jit-recording.c. */
1849 void
1850 gcc_jit_block_add_comment (gcc_jit_block *block,
1851 gcc_jit_location *loc,
1852 const char *text)
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
1866 jit-recording.c. */
1868 void
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 (),
1879 ctxt, loc,
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
1895 jit-recording.c. */
1897 void
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 (
1908 compatible_types (
1909 func->get_return_type (),
1910 rvalue->get_type ()),
1911 ctxt, loc,
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
1926 jit-recording.c. */
1928 void
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),
1938 ctxt, loc,
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 /**********************************************************************
1948 Option-management
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
1955 jit-recording.c. */
1957 void
1958 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
1959 enum gcc_jit_str_option opt,
1960 const char *value)
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
1973 jit-recording.c. */
1975 void
1976 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
1977 enum gcc_jit_int_option opt,
1978 int value)
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
1990 jit-recording.c. */
1992 void
1993 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
1994 enum gcc_jit_bool_option opt,
1995 int value)
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
2007 jit-recording.c. */
2009 gcc_jit_result *
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
2021 jit-recording.c. */
2023 void
2024 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2025 const char *path,
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
2037 jit-recording.c. */
2039 const char *
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
2051 jit-playback.c. */
2053 void *
2054 gcc_jit_result_get_code (gcc_jit_result *result,
2055 const char *fnname)
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. */
2068 void
2069 gcc_jit_result_release (gcc_jit_result *result)
2071 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2073 delete result;