jit: New API entrypoint: gcc_jit_context_get_last_error
[official-gcc.git] / gcc / jit / libgccjit.c
blob34f201ef035752f3de6491c5bd0ed6c88a1c0fc4
1 /* Implementation of the C API; all wrappers into the internal C++ API
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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-logging.h"
30 #include "jit-recording.h"
31 #include "jit-result.h"
33 /* The opaque types used by the public API are actually subclasses
34 of the gcc::jit::recording classes. */
36 struct gcc_jit_context : public gcc::jit::recording::context
38 gcc_jit_context (gcc_jit_context *parent_ctxt) :
39 context (parent_ctxt)
43 struct gcc_jit_result : public gcc::jit::result
47 struct gcc_jit_object : public gcc::jit::recording::memento
51 struct gcc_jit_location : public gcc::jit::recording::location
55 struct gcc_jit_type : public gcc::jit::recording::type
59 struct gcc_jit_struct : public gcc::jit::recording::struct_
63 struct gcc_jit_field : public gcc::jit::recording::field
67 struct gcc_jit_function : public gcc::jit::recording::function
71 struct gcc_jit_block : public gcc::jit::recording::block
75 struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
79 struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
83 struct gcc_jit_param : public gcc::jit::recording::param
87 /**********************************************************************
88 Error-handling.
90 We try to gracefully handle API usage errors by being defensive
91 at the API boundary.
92 **********************************************************************/
94 #define JIT_BEGIN_STMT do {
95 #define JIT_END_STMT } while(0)
97 /* Each of these error-handling macros determines if TEST_EXPR holds.
99 If TEXT_EXPR fails to hold we return from the enclosing function and
100 print an error, either via adding an error on the given context CTXT
101 if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
102 is NULL.
104 They have to be macros since they inject their "return" into the
105 function they are placed in.
107 The variant macros express:
109 (A) whether or not we need to return a value:
110 RETURN_VAL_IF_FAIL* vs
111 RETURN_IF_FAIL*,
112 with the former returning RETURN_EXPR, and
113 RETURN_NULL_IF_FAIL*
114 for the common case where a NULL value is to be returned on
115 error, and
117 (B) whether the error message is to be directly printed:
118 RETURN_*IF_FAIL
119 or is a format string with some number of arguments:
120 RETURN_*IF_FAIL_PRINTF*
122 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
123 trailing semicolons.
126 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
127 JIT_BEGIN_STMT \
128 if (!(TEST_EXPR)) \
130 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
131 return (RETURN_EXPR); \
133 JIT_END_STMT
135 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
136 JIT_BEGIN_STMT \
137 if (!(TEST_EXPR)) \
139 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
140 __func__, (A0)); \
141 return (RETURN_EXPR); \
143 JIT_END_STMT
145 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
146 JIT_BEGIN_STMT \
147 if (!(TEST_EXPR)) \
149 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
150 __func__, (A0), (A1)); \
151 return (RETURN_EXPR); \
153 JIT_END_STMT
155 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
156 JIT_BEGIN_STMT \
157 if (!(TEST_EXPR)) \
159 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
160 __func__, (A0), (A1), (A2)); \
161 return (RETURN_EXPR); \
163 JIT_END_STMT
165 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
166 JIT_BEGIN_STMT \
167 if (!(TEST_EXPR)) \
169 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
170 __func__, (A0), (A1), (A2), (A3)); \
171 return (RETURN_EXPR); \
173 JIT_END_STMT
175 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
176 JIT_BEGIN_STMT \
177 if (!(TEST_EXPR)) \
179 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
180 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
181 return (RETURN_EXPR); \
183 JIT_END_STMT
185 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
186 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
188 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
189 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
191 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
192 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
194 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
195 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
197 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
198 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
200 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
201 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
203 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
204 JIT_BEGIN_STMT \
205 if (!(TEST_EXPR)) \
207 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
208 return; \
210 JIT_END_STMT
212 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
213 JIT_BEGIN_STMT \
214 if (!(TEST_EXPR)) \
216 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
217 __func__, (A0)); \
218 return; \
220 JIT_END_STMT
222 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
223 JIT_BEGIN_STMT \
224 if (!(TEST_EXPR)) \
226 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
227 __func__, (A0), (A1)); \
228 return; \
230 JIT_END_STMT
232 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
233 JIT_BEGIN_STMT \
234 if (!(TEST_EXPR)) \
236 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
237 __func__, (A0), (A1), (A2), (A3)); \
238 return; \
240 JIT_END_STMT
242 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
243 it. This will fail if BLOCK has already been terminated by some
244 kind of jump or a return. */
245 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
246 JIT_BEGIN_STMT \
247 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
248 RETURN_IF_FAIL_PRINTF2 ( \
249 !(BLOCK)->has_been_terminated (), \
250 (BLOCK)->get_context (), \
251 (LOC), \
252 "adding to terminated block: %s (already terminated by: %s)", \
253 (BLOCK)->get_debug_string (), \
254 (BLOCK)->get_last_statement ()->get_debug_string ()); \
255 JIT_END_STMT
257 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
258 fails. */
259 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
260 JIT_BEGIN_STMT \
261 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
262 RETURN_NULL_IF_FAIL_PRINTF2 ( \
263 !(BLOCK)->has_been_terminated (), \
264 (BLOCK)->get_context (), \
265 (LOC), \
266 "adding to terminated block: %s (already terminated by: %s)", \
267 (BLOCK)->get_debug_string (), \
268 (BLOCK)->get_last_statement ()->get_debug_string ()); \
269 JIT_END_STMT
271 /* Format the given string, and report it as an error, either on CTXT
272 if non-NULL, or by printing to stderr if we have a NULL context.
273 LOC gives the source location where the error occcurred, and can be
274 NULL. */
276 static void
277 jit_error (gcc::jit::recording::context *ctxt,
278 gcc_jit_location *loc,
279 const char *fmt, ...)
280 GNU_PRINTF(3, 4);
282 static void
283 jit_error (gcc::jit::recording::context *ctxt,
284 gcc_jit_location *loc,
285 const char *fmt, ...)
287 va_list ap;
288 va_start (ap, fmt);
290 if (ctxt)
291 ctxt->add_error_va (loc, fmt, ap);
292 else
294 /* No context? Send to stderr. */
295 vfprintf (stderr, fmt, ap);
296 fprintf (stderr, "\n");
299 va_end (ap);
302 /* Determine whether or not we can write to lvalues of type LTYPE from
303 rvalues of type RTYPE, detecting type errors such as attempting to
304 write to an int with a string literal (without an explicit cast).
306 This is implemented by calling the
307 gcc::jit::recording::type::accepts_writes_from virtual function on
308 LTYPE. */
310 static bool
311 compatible_types (gcc::jit::recording::type *ltype,
312 gcc::jit::recording::type *rtype)
314 return ltype->accepts_writes_from (rtype);
317 /* Public entrypoint for acquiring a gcc_jit_context.
318 Note that this creates a new top-level context; contrast with
319 gcc_jit_context_new_child_context below.
321 The real work is done in the constructor for
322 gcc::jit::recording::context in jit-recording.c. */
324 gcc_jit_context *
325 gcc_jit_context_acquire (void)
327 gcc_jit_context *ctxt = new gcc_jit_context (NULL);
328 ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
329 return ctxt;
332 /* Public entrypoint for releasing a gcc_jit_context.
333 The real work is done in the destructor for
334 gcc::jit::recording::context in jit-recording.c. */
336 void
337 gcc_jit_context_release (gcc_jit_context *ctxt)
339 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
340 JIT_LOG_FUNC (ctxt->get_logger ());
341 ctxt->log ("deleting ctxt: %p", (void *)ctxt);
342 delete ctxt;
345 /* Public entrypoint for creating a child context within
346 PARENT_CTXT. See description in libgccjit.h.
348 The real work is done in the constructor for
349 gcc::jit::recording::context in jit-recording.c. */
351 gcc_jit_context *
352 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
354 RETURN_NULL_IF_FAIL (parent_ctxt, NULL, NULL, "NULL parent ctxt");
355 JIT_LOG_FUNC (parent_ctxt->get_logger ());
356 parent_ctxt->log ("parent_ctxt: %p", (void *)parent_ctxt);
357 gcc_jit_context *child_ctxt = new gcc_jit_context (parent_ctxt);
358 child_ctxt->log ("new child_ctxt: %p", (void *)child_ctxt);
359 return child_ctxt;
362 /* Public entrypoint. See description in libgccjit.h.
364 After error-checking, the real work is done by the
365 gcc::jit::recording::context::new_location
366 method in jit-recording.c. */
368 gcc_jit_location *
369 gcc_jit_context_new_location (gcc_jit_context *ctxt,
370 const char *filename,
371 int line,
372 int column)
374 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
375 JIT_LOG_FUNC (ctxt->get_logger ());
376 return (gcc_jit_location *)ctxt->new_location (filename, line, column);
379 /* Public entrypoint. See description in libgccjit.h.
381 After error-checking, this calls the trivial
382 gcc::jit::recording::memento::as_object method (a location is a
383 memento), in jit-recording.h. */
385 gcc_jit_object *
386 gcc_jit_location_as_object (gcc_jit_location *loc)
388 RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
390 return static_cast <gcc_jit_object *> (loc->as_object ());
393 /* Public entrypoint. See description in libgccjit.h.
395 After error-checking, this calls the trivial
396 gcc::jit::recording::memento::as_object method (a type is a
397 memento), in jit-recording.h. */
399 gcc_jit_object *
400 gcc_jit_type_as_object (gcc_jit_type *type)
402 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
404 return static_cast <gcc_jit_object *> (type->as_object ());
407 /* Public entrypoint for getting a specific type from a context.
409 After error-checking, the real work is done by the
410 gcc::jit::recording::context::get_type method, in
411 jit-recording.c */
413 gcc_jit_type *
414 gcc_jit_context_get_type (gcc_jit_context *ctxt,
415 enum gcc_jit_types type)
417 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
418 JIT_LOG_FUNC (ctxt->get_logger ());
419 RETURN_NULL_IF_FAIL_PRINTF1 (
420 (type >= GCC_JIT_TYPE_VOID
421 && type <= GCC_JIT_TYPE_FILE_PTR),
422 ctxt, NULL,
423 "unrecognized value for enum gcc_jit_types: %i", type);
425 return (gcc_jit_type *)ctxt->get_type (type);
428 /* Public entrypoint for getting the integer type of the given size and
429 signedness.
431 After error-checking, the real work is done by the
432 gcc::jit::recording::context::get_int_type method,
433 in jit-recording.c. */
435 gcc_jit_type *
436 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
437 int num_bytes, int is_signed)
439 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
440 JIT_LOG_FUNC (ctxt->get_logger ());
441 RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
443 return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
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_pointer method, in
450 jit-recording.c */
452 gcc_jit_type *
453 gcc_jit_type_get_pointer (gcc_jit_type *type)
455 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
457 return (gcc_jit_type *)type->get_pointer ();
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_const method, in
464 jit-recording.c. */
466 gcc_jit_type *
467 gcc_jit_type_get_const (gcc_jit_type *type)
469 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
471 return (gcc_jit_type *)type->get_const ();
474 /* Public entrypoint. See description in libgccjit.h.
476 After error-checking, the real work is done by the
477 gcc::jit::recording::type::get_volatile method, in
478 jit-recording.c. */
480 gcc_jit_type *
481 gcc_jit_type_get_volatile (gcc_jit_type *type)
483 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
485 return (gcc_jit_type *)type->get_volatile ();
488 /* Public entrypoint. See description in libgccjit.h.
490 After error-checking, the real work is done by the
491 gcc::jit::recording::context::new_array_type method, in
492 jit-recording.c. */
494 gcc_jit_type *
495 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
496 gcc_jit_location *loc,
497 gcc_jit_type *element_type,
498 int num_elements)
500 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
501 JIT_LOG_FUNC (ctxt->get_logger ());
502 /* LOC can be NULL. */
503 RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
504 RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
506 return (gcc_jit_type *)ctxt->new_array_type (loc,
507 element_type,
508 num_elements);
511 /* Public entrypoint. See description in libgccjit.h.
513 After error-checking, the real work is done by the
514 gcc::jit::recording::context::new_field method, in
515 jit-recording.c. */
517 gcc_jit_field *
518 gcc_jit_context_new_field (gcc_jit_context *ctxt,
519 gcc_jit_location *loc,
520 gcc_jit_type *type,
521 const char *name)
523 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
524 JIT_LOG_FUNC (ctxt->get_logger ());
525 /* LOC can be NULL. */
526 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
527 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
529 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
532 /* Public entrypoint. See description in libgccjit.h.
534 After error-checking, this calls the trivial
535 gcc::jit::recording::memento::as_object method (a field is a
536 memento), in jit-recording.h. */
538 gcc_jit_object *
539 gcc_jit_field_as_object (gcc_jit_field *field)
541 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
543 return static_cast <gcc_jit_object *> (field->as_object ());
546 /* Public entrypoint. See description in libgccjit.h.
548 After error-checking, the real work is done by the
549 gcc::jit::recording::context::new_struct_type method,
550 immediately followed by a "set_fields" call on the resulting
551 gcc::jit::recording::compound_type *, both in jit-recording.c */
553 gcc_jit_struct *
554 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
555 gcc_jit_location *loc,
556 const char *name,
557 int num_fields,
558 gcc_jit_field **fields)
560 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
561 JIT_LOG_FUNC (ctxt->get_logger ());
562 /* LOC can be NULL. */
563 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
564 if (num_fields)
565 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
566 for (int i = 0; i < num_fields; i++)
568 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
569 RETURN_NULL_IF_FAIL_PRINTF2 (
570 NULL == fields[i]->get_container (),
571 ctxt, loc,
572 "%s is already a field of %s",
573 fields[i]->get_debug_string (),
574 fields[i]->get_container ()->get_debug_string ());
577 gcc::jit::recording::struct_ *result =
578 ctxt->new_struct_type (loc, name);
579 result->set_fields (loc,
580 num_fields,
581 (gcc::jit::recording::field **)fields);
582 return static_cast<gcc_jit_struct *> (result);
585 /* Public entrypoint. See description in libgccjit.h.
587 After error-checking, the real work is done by the
588 gcc::jit::recording::context::new_struct_type method in
589 jit-recording.c. */
591 gcc_jit_struct *
592 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
593 gcc_jit_location *loc,
594 const char *name)
596 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
597 JIT_LOG_FUNC (ctxt->get_logger ());
598 /* LOC can be NULL. */
599 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
601 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
604 /* Public entrypoint. See description in libgccjit.h.
606 After error-checking, this calls the trivial
607 gcc::jit::recording::struct_::as_object method in
608 jit-recording.h. */
610 gcc_jit_type *
611 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
613 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
615 return static_cast <gcc_jit_type *> (struct_type->as_type ());
618 /* Public entrypoint. See description in libgccjit.h.
620 After error-checking, the real work is done by the
621 gcc::jit::recording::compound_type::set_fields method in
622 jit-recording.c. */
624 void
625 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
626 gcc_jit_location *loc,
627 int num_fields,
628 gcc_jit_field **fields)
630 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
631 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
632 JIT_LOG_FUNC (ctxt->get_logger ());
633 /* LOC can be NULL. */
634 RETURN_IF_FAIL_PRINTF1 (
635 NULL == struct_type->get_fields (), ctxt, loc,
636 "%s already has had fields set",
637 struct_type->get_debug_string ());
638 if (num_fields)
639 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
640 for (int i = 0; i < num_fields; i++)
642 RETURN_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
643 RETURN_IF_FAIL_PRINTF2 (
644 NULL == fields[i]->get_container (),
645 ctxt, loc,
646 "%s is already a field of %s",
647 fields[i]->get_debug_string (),
648 fields[i]->get_container ()->get_debug_string ());
651 struct_type->set_fields (loc, num_fields,
652 (gcc::jit::recording::field **)fields);
655 /* Public entrypoint. See description in libgccjit.h.
657 After error-checking, the real work is done by the
658 gcc::jit::recording::context::new_union_type method,
659 immediately followed by a "set_fields" call on the resulting
660 gcc::jit::recording::compound_type *, both in jit-recording.c */
662 gcc_jit_type *
663 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
664 gcc_jit_location *loc,
665 const char *name,
666 int num_fields,
667 gcc_jit_field **fields)
669 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
670 JIT_LOG_FUNC (ctxt->get_logger ());
671 /* LOC can be NULL. */
672 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
673 if (num_fields)
674 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
675 for (int i = 0; i < num_fields; i++)
677 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
678 RETURN_NULL_IF_FAIL_PRINTF2 (
679 NULL == fields[i]->get_container (),
680 ctxt, loc,
681 "%s is already a field of %s",
682 fields[i]->get_debug_string (),
683 fields[i]->get_container ()->get_debug_string ());
686 gcc::jit::recording::union_ *result =
687 ctxt->new_union_type (loc, name);
688 result->set_fields (loc,
689 num_fields,
690 (gcc::jit::recording::field **)fields);
691 return (gcc_jit_type *) (result);
694 /* Public entrypoint. See description in libgccjit.h.
696 After error-checking, the real work is done by the
697 gcc::jit::recording::context::new_function_ptr_type method,
698 in jit-recording.c */
700 gcc_jit_type *
701 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
702 gcc_jit_location *loc,
703 gcc_jit_type *return_type,
704 int num_params,
705 gcc_jit_type **param_types,
706 int is_variadic)
708 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
709 JIT_LOG_FUNC (ctxt->get_logger ());
710 /* LOC can be NULL. */
711 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
712 RETURN_NULL_IF_FAIL (
713 (num_params == 0) || param_types,
714 ctxt, loc,
715 "NULL param_types creating function pointer type");
716 for (int i = 0; i < num_params; i++)
717 RETURN_NULL_IF_FAIL_PRINTF1 (
718 param_types[i],
719 ctxt, loc,
720 "NULL parameter type %i creating function pointer type", i);
722 return (gcc_jit_type*)
723 ctxt->new_function_ptr_type (loc, return_type,
724 num_params,
725 (gcc::jit::recording::type **)param_types,
726 is_variadic);
729 /* Constructing functions. */
731 /* Public entrypoint. See description in libgccjit.h.
733 After error-checking, the real work is done by the
734 gcc::jit::recording::context::new_param method, in jit-recording.c */
736 gcc_jit_param *
737 gcc_jit_context_new_param (gcc_jit_context *ctxt,
738 gcc_jit_location *loc,
739 gcc_jit_type *type,
740 const char *name)
742 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
743 JIT_LOG_FUNC (ctxt->get_logger ());
744 /* LOC can be NULL. */
745 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
746 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
748 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
751 /* Public entrypoint. See description in libgccjit.h.
753 After error-checking, this calls the trivial
754 gcc::jit::recording::memento::as_object method (a param is a memento),
755 in jit-recording.h. */
757 gcc_jit_object *
758 gcc_jit_param_as_object (gcc_jit_param *param)
760 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
762 return static_cast <gcc_jit_object *> (param->as_object ());
765 /* Public entrypoint. See description in libgccjit.h.
767 After error-checking, this calls the trivial
768 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
770 gcc_jit_lvalue *
771 gcc_jit_param_as_lvalue (gcc_jit_param *param)
773 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
775 return (gcc_jit_lvalue *)param->as_lvalue ();
778 /* Public entrypoint. See description in libgccjit.h.
780 After error-checking, this calls the trivial
781 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
782 in jit-recording.h. */
784 gcc_jit_rvalue *
785 gcc_jit_param_as_rvalue (gcc_jit_param *param)
787 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
789 return (gcc_jit_rvalue *)param->as_rvalue ();
792 /* Public entrypoint. See description in libgccjit.h.
794 After error-checking, the real work is done by the
795 gcc::jit::recording::context::new_function method, in
796 jit-recording.c. */
798 gcc_jit_function *
799 gcc_jit_context_new_function (gcc_jit_context *ctxt,
800 gcc_jit_location *loc,
801 enum gcc_jit_function_kind kind,
802 gcc_jit_type *return_type,
803 const char *name,
804 int num_params,
805 gcc_jit_param **params,
806 int is_variadic)
808 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
809 JIT_LOG_FUNC (ctxt->get_logger ());
810 /* LOC can be NULL. */
811 RETURN_NULL_IF_FAIL_PRINTF1 (
812 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
813 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
814 ctxt, loc,
815 "unrecognized value for enum gcc_jit_function_kind: %i",
816 kind);
817 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
818 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
819 /* The assembler can only handle certain names, so for now, enforce
820 C's rules for identiers upon the name, using ISALPHA and ISALNUM
821 from safe-ctype.h to ignore the current locale.
822 Eventually we'll need some way to interact with e.g. C++ name
823 mangling. */
825 /* Leading char: */
826 char ch = *name;
827 RETURN_NULL_IF_FAIL_PRINTF2 (
828 ISALPHA (ch) || ch == '_',
829 ctxt, loc,
830 "name \"%s\" contains invalid character: '%c'",
831 name, ch);
832 /* Subsequent chars: */
833 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
835 RETURN_NULL_IF_FAIL_PRINTF2 (
836 ISALNUM (ch) || ch == '_',
837 ctxt, loc,
838 "name \"%s\" contains invalid character: '%c'",
839 name, ch);
842 RETURN_NULL_IF_FAIL_PRINTF1 (
843 (num_params == 0) || params,
844 ctxt, loc,
845 "NULL params creating function %s", name);
846 for (int i = 0; i < num_params; i++)
847 RETURN_NULL_IF_FAIL_PRINTF2 (
848 params[i],
849 ctxt, loc,
850 "NULL parameter %i creating function %s", i, name);
852 return (gcc_jit_function*)
853 ctxt->new_function (loc, kind, return_type, name,
854 num_params,
855 (gcc::jit::recording::param **)params,
856 is_variadic,
857 BUILT_IN_NONE);
860 /* Public entrypoint. See description in libgccjit.h.
862 After error-checking, the real work is done by the
863 gcc::jit::recording::context::get_builtin_function method, in
864 jit-recording.c. */
866 gcc_jit_function *
867 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
868 const char *name)
870 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
871 JIT_LOG_FUNC (ctxt->get_logger ());
872 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
874 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
877 /* Public entrypoint. See description in libgccjit.h.
879 After error-checking, this calls the trivial
880 gcc::jit::recording::memento::as_object method (a function is a
881 memento), in jit-recording.h. */
883 gcc_jit_object *
884 gcc_jit_function_as_object (gcc_jit_function *func)
886 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
888 return static_cast <gcc_jit_object *> (func->as_object ());
891 /* Public entrypoint. See description in libgccjit.h.
893 After error-checking, the real work is done by the
894 gcc::jit::recording::function::get_param method, in
895 jit-recording.h. */
897 gcc_jit_param *
898 gcc_jit_function_get_param (gcc_jit_function *func, int index)
900 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
901 gcc::jit::recording::context *ctxt = func->m_ctxt;
902 JIT_LOG_FUNC (ctxt->get_logger ());
903 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
904 int num_params = func->get_params ().length ();
905 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
906 ctxt, NULL,
907 "index of %d is too large (%s has %d params)",
908 index,
909 func->get_debug_string (),
910 num_params);
912 return static_cast <gcc_jit_param *> (func->get_param (index));
915 /* Public entrypoint. See description in libgccjit.h.
917 After error-checking, the real work is done by the
918 gcc::jit::recording::function::dump_to_dot method, in
919 jit-recording.c. */
921 void
922 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
923 const char *path)
925 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
926 gcc::jit::recording::context *ctxt = func->m_ctxt;
927 JIT_LOG_FUNC (ctxt->get_logger ());
928 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
930 func->dump_to_dot (path);
933 /* Public entrypoint. See description in libgccjit.h.
935 After error-checking, the real work is done by the
936 gcc::jit::recording::function::new_block method, in
937 jit-recording.c. */
939 gcc_jit_block*
940 gcc_jit_function_new_block (gcc_jit_function *func,
941 const char *name)
943 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
944 JIT_LOG_FUNC (func->get_context ()->get_logger ());
945 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
946 func->get_context (), NULL,
947 "cannot add block to an imported function");
948 /* name can be NULL. */
950 return (gcc_jit_block *)func->new_block (name);
953 /* Public entrypoint. See description in libgccjit.h.
955 After error-checking, this calls the trivial
956 gcc::jit::recording::memento::as_object method (a block is a
957 memento), in jit-recording.h. */
959 gcc_jit_object *
960 gcc_jit_block_as_object (gcc_jit_block *block)
962 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
964 return static_cast <gcc_jit_object *> (block->as_object ());
967 /* Public entrypoint. See description in libgccjit.h.
969 After error-checking, the real work is done by the
970 gcc::jit::recording::block::get_function method, in
971 jit-recording.h. */
973 gcc_jit_function *
974 gcc_jit_block_get_function (gcc_jit_block *block)
976 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
978 return static_cast <gcc_jit_function *> (block->get_function ());
981 /* Public entrypoint. See description in libgccjit.h.
983 After error-checking, the real work is done by the
984 gcc::jit::recording::context::new_global method, in
985 jit-recording.c. */
987 gcc_jit_lvalue *
988 gcc_jit_context_new_global (gcc_jit_context *ctxt,
989 gcc_jit_location *loc,
990 gcc_jit_type *type,
991 const char *name)
993 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
994 JIT_LOG_FUNC (ctxt->get_logger ());
995 /* LOC can be NULL. */
996 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
997 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
999 return (gcc_jit_lvalue *)ctxt->new_global (loc, type, name);
1002 /* Public entrypoint. See description in libgccjit.h.
1004 After error-checking, this calls the trivial
1005 gcc::jit::recording::memento::as_object method (an lvalue is a
1006 memento), in jit-recording.h. */
1008 gcc_jit_object *
1009 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1011 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1013 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1016 /* Public entrypoint. See description in libgccjit.h.
1018 After error-checking, this calls the trivial
1019 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1021 gcc_jit_rvalue *
1022 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1024 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1026 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1029 /* Public entrypoint. See description in libgccjit.h.
1031 After error-checking, this calls the trivial
1032 gcc::jit::recording::memento::as_object method (an rvalue is a
1033 memento), in jit-recording.h. */
1035 gcc_jit_object *
1036 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1038 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1040 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1043 /* Public entrypoint. See description in libgccjit.h.
1045 After error-checking, the real work is done by the
1046 gcc::jit::recording::rvalue::get_type method, in
1047 jit-recording.h. */
1049 gcc_jit_type *
1050 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1052 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1054 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1057 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1058 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1059 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1061 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1062 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1063 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1064 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1065 "not a numeric type: %s", \
1066 NUMERIC_TYPE->get_debug_string ());
1068 /* Public entrypoint. See description in libgccjit.h.
1070 After error-checking, the real work is done by the
1071 gcc::jit::recording::context::new_rvalue_from_int method in
1072 jit-recording.c. */
1074 gcc_jit_rvalue *
1075 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1076 gcc_jit_type *numeric_type,
1077 int value)
1079 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1080 JIT_LOG_FUNC (ctxt->get_logger ());
1081 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1083 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_int (numeric_type, value);
1086 /* Public entrypoint. See description in libgccjit.h.
1088 This is essentially equivalent to:
1089 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1090 albeit with slightly different error messages if an error occurs. */
1092 gcc_jit_rvalue *
1093 gcc_jit_context_zero (gcc_jit_context *ctxt,
1094 gcc_jit_type *numeric_type)
1096 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1097 JIT_LOG_FUNC (ctxt->get_logger ());
1098 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1100 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1103 /* Public entrypoint. See description in libgccjit.h.
1105 This is essentially equivalent to:
1106 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1107 albeit with slightly different error messages if an error occurs. */
1109 gcc_jit_rvalue *
1110 gcc_jit_context_one (gcc_jit_context *ctxt,
1111 gcc_jit_type *numeric_type)
1113 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1114 JIT_LOG_FUNC (ctxt->get_logger ());
1115 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1117 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1120 /* Public entrypoint. See description in libgccjit.h.
1122 After error-checking, the real work is done by the
1123 gcc::jit::recording::context::new_rvalue_from_double method in
1124 jit-recording.c. */
1126 gcc_jit_rvalue *
1127 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1128 gcc_jit_type *numeric_type,
1129 double value)
1131 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1132 JIT_LOG_FUNC (ctxt->get_logger ());
1133 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1135 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_double (numeric_type, value);
1138 /* Public entrypoint. See description in libgccjit.h.
1140 After error-checking, the real work is done by the
1141 gcc::jit::recording::context::new_rvalue_from_ptr method in
1142 jit-recording.c. */
1144 gcc_jit_rvalue *
1145 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1146 gcc_jit_type *pointer_type,
1147 void *value)
1149 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1150 JIT_LOG_FUNC (ctxt->get_logger ());
1151 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1152 RETURN_NULL_IF_FAIL_PRINTF1 (
1153 pointer_type->is_pointer (),
1154 ctxt, NULL,
1155 "not a pointer type (type: %s)",
1156 pointer_type->get_debug_string ());
1158 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_ptr (pointer_type, value);
1161 /* Public entrypoint. See description in libgccjit.h.
1163 This is essentially equivalent to:
1164 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1165 albeit with slightly different error messages if an error occurs. */
1167 gcc_jit_rvalue *
1168 gcc_jit_context_null (gcc_jit_context *ctxt,
1169 gcc_jit_type *pointer_type)
1171 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1172 JIT_LOG_FUNC (ctxt->get_logger ());
1173 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1174 RETURN_NULL_IF_FAIL_PRINTF1 (
1175 pointer_type->is_pointer (),
1176 ctxt, NULL,
1177 "not a pointer type (type: %s)",
1178 pointer_type->get_debug_string ());
1180 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1183 /* Public entrypoint. See description in libgccjit.h.
1185 After error-checking, the real work is done by the
1186 gcc::jit::recording::context::new_string_literal method in
1187 jit-recording.c. */
1189 gcc_jit_rvalue *
1190 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1191 const char *value)
1193 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1194 JIT_LOG_FUNC (ctxt->get_logger ());
1195 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1197 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1200 /* Public entrypoint. See description in libgccjit.h.
1202 After error-checking, the real work is done by the
1203 gcc::jit::recording::context::new_unary_op method in
1204 jit-recording.c. */
1206 gcc_jit_rvalue *
1207 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1208 gcc_jit_location *loc,
1209 enum gcc_jit_unary_op op,
1210 gcc_jit_type *result_type,
1211 gcc_jit_rvalue *rvalue)
1213 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1214 JIT_LOG_FUNC (ctxt->get_logger ());
1215 /* LOC can be NULL. */
1216 RETURN_NULL_IF_FAIL_PRINTF1 (
1217 (op >= GCC_JIT_UNARY_OP_MINUS
1218 && op <= GCC_JIT_UNARY_OP_ABS),
1219 ctxt, loc,
1220 "unrecognized value for enum gcc_jit_unary_op: %i",
1221 op);
1222 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1223 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1225 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1228 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1229 For use by both gcc_jit_context_new_binary_op and
1230 gcc_jit_block_add_assignment_op. */
1232 static bool
1233 valid_binary_op_p (enum gcc_jit_binary_op op)
1235 return (op >= GCC_JIT_BINARY_OP_PLUS
1236 && op <= GCC_JIT_BINARY_OP_RSHIFT);
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_binary_op method in
1243 jit-recording.c. */
1245 gcc_jit_rvalue *
1246 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1247 gcc_jit_location *loc,
1248 enum gcc_jit_binary_op op,
1249 gcc_jit_type *result_type,
1250 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1252 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1253 JIT_LOG_FUNC (ctxt->get_logger ());
1254 /* LOC can be NULL. */
1255 RETURN_NULL_IF_FAIL_PRINTF1 (
1256 valid_binary_op_p (op),
1257 ctxt, loc,
1258 "unrecognized value for enum gcc_jit_binary_op: %i",
1259 op);
1260 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1261 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1262 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1263 RETURN_NULL_IF_FAIL_PRINTF4 (
1264 a->get_type () == b->get_type (),
1265 ctxt, loc,
1266 "mismatching types for binary op:"
1267 " a: %s (type: %s) b: %s (type: %s)",
1268 a->get_debug_string (),
1269 a->get_type ()->get_debug_string (),
1270 b->get_debug_string (),
1271 b->get_type ()->get_debug_string ());
1273 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1276 /* Public entrypoint. See description in libgccjit.h.
1278 After error-checking, the real work is done by the
1279 gcc::jit::recording::context::new_comparison method in
1280 jit-recording.c. */
1282 gcc_jit_rvalue *
1283 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1284 gcc_jit_location *loc,
1285 enum gcc_jit_comparison op,
1286 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1288 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1289 JIT_LOG_FUNC (ctxt->get_logger ());
1290 /* LOC can be NULL. */
1291 RETURN_NULL_IF_FAIL_PRINTF1 (
1292 (op >= GCC_JIT_COMPARISON_EQ
1293 && op <= GCC_JIT_COMPARISON_GE),
1294 ctxt, loc,
1295 "unrecognized value for enum gcc_jit_comparison: %i",
1296 op);
1297 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1298 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1299 RETURN_NULL_IF_FAIL_PRINTF4 (
1300 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1301 ctxt, loc,
1302 "mismatching types for comparison:"
1303 " a: %s (type: %s) b: %s (type: %s)",
1304 a->get_debug_string (),
1305 a->get_type ()->get_debug_string (),
1306 b->get_debug_string (),
1307 b->get_type ()->get_debug_string ());
1309 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1312 /* Public entrypoint. See description in libgccjit.h.
1314 After error-checking, the real work is done by the
1315 gcc::jit::recording::context::new_call method in
1316 jit-recording.c. */
1318 gcc_jit_rvalue *
1319 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1320 gcc_jit_location *loc,
1321 gcc_jit_function *func,
1322 int numargs , gcc_jit_rvalue **args)
1324 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1325 JIT_LOG_FUNC (ctxt->get_logger ());
1326 /* LOC can be NULL. */
1327 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1328 if (numargs)
1329 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1331 int min_num_params = func->get_params ().length ();
1332 bool is_variadic = func->is_variadic ();
1334 RETURN_NULL_IF_FAIL_PRINTF3 (
1335 numargs >= min_num_params,
1336 ctxt, loc,
1337 "not enough arguments to function \"%s\""
1338 " (got %i args, expected %i)",
1339 func->get_name ()->c_str (),
1340 numargs, min_num_params);
1342 RETURN_NULL_IF_FAIL_PRINTF3 (
1343 (numargs == min_num_params || is_variadic),
1344 ctxt, loc,
1345 "too many arguments to function \"%s\""
1346 " (got %i args, expected %i)",
1347 func->get_name ()->c_str (),
1348 numargs, min_num_params);
1350 for (int i = 0; i < min_num_params; i++)
1352 gcc::jit::recording::param *param = func->get_param (i);
1353 gcc_jit_rvalue *arg = args[i];
1355 RETURN_NULL_IF_FAIL_PRINTF4 (
1356 arg,
1357 ctxt, loc,
1358 "NULL argument %i to function \"%s\":"
1359 " param %s (type: %s)",
1360 i + 1,
1361 func->get_name ()->c_str (),
1362 param->get_debug_string (),
1363 param->get_type ()->get_debug_string ());
1365 RETURN_NULL_IF_FAIL_PRINTF6 (
1366 compatible_types (param->get_type (),
1367 arg->get_type ()),
1368 ctxt, loc,
1369 "mismatching types for argument %d of function \"%s\":"
1370 " assignment to param %s (type: %s) from %s (type: %s)",
1371 i + 1,
1372 func->get_name ()->c_str (),
1373 param->get_debug_string (),
1374 param->get_type ()->get_debug_string (),
1375 arg->get_debug_string (),
1376 arg->get_type ()->get_debug_string ());
1379 return (gcc_jit_rvalue *)ctxt->new_call (loc,
1380 func,
1381 numargs,
1382 (gcc::jit::recording::rvalue **)args);
1385 /* Public entrypoint. See description in libgccjit.h.
1387 After error-checking, the real work is done by the
1388 gcc::jit::recording::context::new_call_through_ptr method in
1389 jit-recording.c. */
1391 gcc_jit_rvalue *
1392 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1393 gcc_jit_location *loc,
1394 gcc_jit_rvalue *fn_ptr,
1395 int numargs, gcc_jit_rvalue **args)
1397 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1398 JIT_LOG_FUNC (ctxt->get_logger ());
1399 /* LOC can be NULL. */
1400 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1401 if (numargs)
1402 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1404 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1405 RETURN_NULL_IF_FAIL_PRINTF2 (
1406 ptr_type, ctxt, loc,
1407 "fn_ptr is not a ptr: %s"
1408 " type: %s",
1409 fn_ptr->get_debug_string (),
1410 fn_ptr->get_type ()->get_debug_string ());
1412 gcc::jit::recording::function_type *fn_type =
1413 ptr_type->dyn_cast_function_type();
1414 RETURN_NULL_IF_FAIL_PRINTF2 (
1415 fn_type, ctxt, loc,
1416 "fn_ptr is not a function ptr: %s"
1417 " type: %s",
1418 fn_ptr->get_debug_string (),
1419 fn_ptr->get_type ()->get_debug_string ());
1421 int min_num_params = fn_type->get_param_types ().length ();
1422 bool is_variadic = fn_type->is_variadic ();
1424 RETURN_NULL_IF_FAIL_PRINTF3 (
1425 numargs >= min_num_params,
1426 ctxt, loc,
1427 "not enough arguments to fn_ptr: %s"
1428 " (got %i args, expected %i)",
1429 fn_ptr->get_debug_string (),
1430 numargs, min_num_params);
1432 RETURN_NULL_IF_FAIL_PRINTF3 (
1433 (numargs == min_num_params || is_variadic),
1434 ctxt, loc,
1435 "too many arguments to fn_ptr: %s"
1436 " (got %i args, expected %i)",
1437 fn_ptr->get_debug_string (),
1438 numargs, min_num_params);
1440 for (int i = 0; i < min_num_params; i++)
1442 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1443 gcc_jit_rvalue *arg = args[i];
1445 RETURN_NULL_IF_FAIL_PRINTF3 (
1446 arg,
1447 ctxt, loc,
1448 "NULL argument %i to fn_ptr: %s"
1449 " (type: %s)",
1450 i + 1,
1451 fn_ptr->get_debug_string (),
1452 param_type->get_debug_string ());
1454 RETURN_NULL_IF_FAIL_PRINTF6 (
1455 compatible_types (param_type,
1456 arg->get_type ()),
1457 ctxt, loc,
1458 "mismatching types for argument %d of fn_ptr: %s:"
1459 " assignment to param %d (type: %s) from %s (type: %s)",
1460 i + 1,
1461 fn_ptr->get_debug_string (),
1462 i + 1,
1463 param_type->get_debug_string (),
1464 arg->get_debug_string (),
1465 arg->get_type ()->get_debug_string ());
1468 return (gcc_jit_rvalue *)(
1469 ctxt->new_call_through_ptr (loc,
1470 fn_ptr,
1471 numargs,
1472 (gcc::jit::recording::rvalue **)args));
1475 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1476 to DST_TYPE, for use by gcc_jit_context_new_cast.
1478 We only permit these kinds of cast:
1480 int <-> float
1481 int <-> bool
1482 P* <-> Q* for pointer types P and Q. */
1484 static bool
1485 is_valid_cast (gcc::jit::recording::type *src_type,
1486 gcc_jit_type *dst_type)
1488 bool src_is_int = src_type->is_int ();
1489 bool dst_is_int = dst_type->is_int ();
1490 bool src_is_float = src_type->is_float ();
1491 bool dst_is_float = dst_type->is_float ();
1492 bool src_is_bool = src_type->is_bool ();
1493 bool dst_is_bool = dst_type->is_bool ();
1495 if (src_is_int)
1496 if (dst_is_int || dst_is_float || dst_is_bool)
1497 return true;
1499 if (src_is_float)
1500 if (dst_is_int || dst_is_float)
1501 return true;
1503 if (src_is_bool)
1504 if (dst_is_int || dst_is_bool)
1505 return true;
1507 /* Permit casts between pointer types. */
1508 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1509 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1510 if (deref_src_type && deref_dst_type)
1511 return true;
1513 return false;
1516 /* Public entrypoint. See description in libgccjit.h.
1518 After error-checking, the real work is done by the
1519 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1521 gcc_jit_rvalue *
1522 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1523 gcc_jit_location *loc,
1524 gcc_jit_rvalue *rvalue,
1525 gcc_jit_type *type)
1527 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1528 JIT_LOG_FUNC (ctxt->get_logger ());
1529 /* LOC can be NULL. */
1530 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1531 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1532 RETURN_NULL_IF_FAIL_PRINTF3 (
1533 is_valid_cast (rvalue->get_type (), type),
1534 ctxt, loc,
1535 "cannot cast %s from type: %s to type: %s",
1536 rvalue->get_debug_string (),
1537 rvalue->get_type ()->get_debug_string (),
1538 type->get_debug_string ());
1540 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1543 /* Public entrypoint. See description in libgccjit.h.
1545 After error-checking, the real work is done by the
1546 gcc::jit::recording::context::new_array_access method in
1547 jit-recording.c. */
1549 extern gcc_jit_lvalue *
1550 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1551 gcc_jit_location *loc,
1552 gcc_jit_rvalue *ptr,
1553 gcc_jit_rvalue *index)
1555 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1556 JIT_LOG_FUNC (ctxt->get_logger ());
1557 /* LOC can be NULL. */
1558 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1559 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1560 RETURN_NULL_IF_FAIL_PRINTF2 (
1561 ptr->get_type ()->dereference (),
1562 ctxt, loc,
1563 "ptr: %s (type: %s) is not a pointer or array",
1564 ptr->get_debug_string (),
1565 ptr->get_type ()->get_debug_string ());
1566 RETURN_NULL_IF_FAIL_PRINTF2 (
1567 index->get_type ()->is_numeric (),
1568 ctxt, loc,
1569 "index: %s (type: %s) is not of numeric type",
1570 index->get_debug_string (),
1571 index->get_type ()->get_debug_string ());
1573 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1576 /* Public entrypoint. See description in libgccjit.h.
1578 After error-checking, the real work is done by the
1579 gcc::jit::recording::memento::get_context method in
1580 jit-recording.h. */
1582 gcc_jit_context *
1583 gcc_jit_object_get_context (gcc_jit_object *obj)
1585 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1587 return static_cast <gcc_jit_context *> (obj->get_context ());
1590 /* Public entrypoint. See description in libgccjit.h.
1592 After error-checking, the real work is done by the
1593 gcc::jit::recording::memento::get_debug_string method in
1594 jit-recording.c. */
1596 const char *
1597 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1599 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1601 return obj->get_debug_string ();
1604 /* Public entrypoint. See description in libgccjit.h.
1606 After error-checking, the real work is done by the
1607 gcc::jit::recording::lvalue::access_field method in
1608 jit-recording.c. */
1610 gcc_jit_lvalue *
1611 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1612 gcc_jit_location *loc,
1613 gcc_jit_field *field)
1615 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1616 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1617 JIT_LOG_FUNC (ctxt->get_logger ());
1618 /* LOC can be NULL. */
1619 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1620 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1621 "field %s has not been placed in a struct",
1622 field->get_debug_string ());
1624 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1627 /* Public entrypoint. See description in libgccjit.h.
1629 After error-checking, the real work is done by the
1630 gcc::jit::recording::rvalue::access_field method in
1631 jit-recording.c. */
1633 gcc_jit_rvalue *
1634 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1635 gcc_jit_location *loc,
1636 gcc_jit_field *field)
1638 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1639 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1640 JIT_LOG_FUNC (ctxt->get_logger ());
1641 /* LOC can be NULL. */
1642 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1643 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1644 "field %s has not been placed in a struct",
1645 field->get_debug_string ());
1647 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1650 /* Public entrypoint. See description in libgccjit.h.
1652 After error-checking, the real work is done by the
1653 gcc::jit::recording::rvalue::deference_field method in
1654 jit-recording.c. */
1656 gcc_jit_lvalue *
1657 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1658 gcc_jit_location *loc,
1659 gcc_jit_field *field)
1661 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1662 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1663 /* LOC can be NULL. */
1664 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1665 gcc::jit::recording::type *underlying_type =
1666 ptr->get_type ()->is_pointer ();
1667 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1668 "field %s has not been placed in a struct",
1669 field->get_debug_string ());
1670 RETURN_NULL_IF_FAIL_PRINTF3 (
1671 underlying_type,
1672 ptr->m_ctxt, loc,
1673 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1674 ptr->get_debug_string (),
1675 ptr->get_type ()->get_debug_string (),
1676 field->get_debug_string ());
1677 RETURN_NULL_IF_FAIL_PRINTF2 (
1678 (field->get_container ()->unqualified ()
1679 == underlying_type->unqualified ()),
1680 ptr->m_ctxt, loc,
1681 "%s is not a field of %s",
1682 field->get_debug_string (),
1683 underlying_type->get_debug_string ());
1685 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1688 /* Public entrypoint. See description in libgccjit.h.
1690 After error-checking, the real work is done by the
1691 gcc::jit::recording::rvalue::deference method in
1692 jit-recording.c. */
1694 gcc_jit_lvalue *
1695 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1696 gcc_jit_location *loc)
1698 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1699 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1700 /* LOC can be NULL. */
1702 gcc::jit::recording::type *underlying_type =
1703 rvalue->get_type ()->is_pointer ();
1705 RETURN_NULL_IF_FAIL_PRINTF2 (
1706 underlying_type,
1707 rvalue->m_ctxt, loc,
1708 "dereference of non-pointer %s (type: %s)",
1709 rvalue->get_debug_string (),
1710 rvalue->get_type ()->get_debug_string ());
1712 RETURN_NULL_IF_FAIL_PRINTF2 (
1713 !underlying_type->is_void (),
1714 rvalue->m_ctxt, loc,
1715 "dereference of void pointer %s (type: %s)",
1716 rvalue->get_debug_string (),
1717 rvalue->get_type ()->get_debug_string ());
1719 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1722 /* Public entrypoint. See description in libgccjit.h.
1724 After error-checking, the real work is done by the
1725 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1727 gcc_jit_rvalue *
1728 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1729 gcc_jit_location *loc)
1731 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1732 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1733 /* LOC can be NULL. */
1735 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1738 /* Public entrypoint. See description in libgccjit.h.
1740 After error-checking, the real work is done by the
1741 gcc::jit::recording::function::new_local method in jit-recording.c. */
1743 gcc_jit_lvalue *
1744 gcc_jit_function_new_local (gcc_jit_function *func,
1745 gcc_jit_location *loc,
1746 gcc_jit_type *type,
1747 const char *name)
1749 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1750 gcc::jit::recording::context *ctxt = func->m_ctxt;
1751 JIT_LOG_FUNC (ctxt->get_logger ());
1752 /* LOC can be NULL. */
1753 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1754 ctxt, loc,
1755 "Cannot add locals to an imported function");
1756 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1757 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1759 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1762 /* Public entrypoint. See description in libgccjit.h.
1764 After error-checking, the real work is done by the
1765 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1767 void
1768 gcc_jit_block_add_eval (gcc_jit_block *block,
1769 gcc_jit_location *loc,
1770 gcc_jit_rvalue *rvalue)
1772 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1773 gcc::jit::recording::context *ctxt = block->get_context ();
1774 JIT_LOG_FUNC (ctxt->get_logger ());
1775 /* LOC can be NULL. */
1776 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1778 return block->add_eval (loc, rvalue);
1781 /* Public entrypoint. See description in libgccjit.h.
1783 After error-checking, the real work is done by the
1784 gcc::jit::recording::block::add_assignment method in
1785 jit-recording.c. */
1787 void
1788 gcc_jit_block_add_assignment (gcc_jit_block *block,
1789 gcc_jit_location *loc,
1790 gcc_jit_lvalue *lvalue,
1791 gcc_jit_rvalue *rvalue)
1793 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1794 gcc::jit::recording::context *ctxt = block->get_context ();
1795 JIT_LOG_FUNC (ctxt->get_logger ());
1796 /* LOC can be NULL. */
1797 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1798 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1799 RETURN_IF_FAIL_PRINTF4 (
1800 compatible_types (lvalue->get_type (),
1801 rvalue->get_type ()),
1802 ctxt, loc,
1803 "mismatching types:"
1804 " assignment to %s (type: %s) from %s (type: %s)",
1805 lvalue->get_debug_string (),
1806 lvalue->get_type ()->get_debug_string (),
1807 rvalue->get_debug_string (),
1808 rvalue->get_type ()->get_debug_string ());
1810 return block->add_assignment (loc, lvalue, rvalue);
1813 /* Public entrypoint. See description in libgccjit.h.
1815 After error-checking, the real work is done by the
1816 gcc::jit::recording::block::add_assignment_op method in
1817 jit-recording.c. */
1819 void
1820 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1821 gcc_jit_location *loc,
1822 gcc_jit_lvalue *lvalue,
1823 enum gcc_jit_binary_op op,
1824 gcc_jit_rvalue *rvalue)
1826 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1827 gcc::jit::recording::context *ctxt = block->get_context ();
1828 JIT_LOG_FUNC (ctxt->get_logger ());
1829 /* LOC can be NULL. */
1830 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1831 RETURN_IF_FAIL_PRINTF1 (
1832 valid_binary_op_p (op),
1833 ctxt, loc,
1834 "unrecognized value for enum gcc_jit_binary_op: %i",
1835 op);
1836 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1838 return block->add_assignment_op (loc, lvalue, op, rvalue);
1841 /* Internal helper function for determining if rvalue BOOLVAL is of
1842 boolean type. For use by gcc_jit_block_end_with_conditional. */
1844 static bool
1845 is_bool (gcc_jit_rvalue *boolval)
1847 gcc::jit::recording::type *actual_type = boolval->get_type ();
1848 gcc::jit::recording::type *bool_type =
1849 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1850 return actual_type == bool_type;
1853 /* Public entrypoint. See description in libgccjit.h.
1855 After error-checking, the real work is done by the
1856 gcc::jit::recording::block::end_with_conditional method in
1857 jit-recording.c. */
1859 void
1860 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1861 gcc_jit_location *loc,
1862 gcc_jit_rvalue *boolval,
1863 gcc_jit_block *on_true,
1864 gcc_jit_block *on_false)
1866 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1867 gcc::jit::recording::context *ctxt = block->get_context ();
1868 JIT_LOG_FUNC (ctxt->get_logger ());
1869 /* LOC can be NULL. */
1870 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
1871 RETURN_IF_FAIL_PRINTF2 (
1872 is_bool (boolval), ctxt, loc,
1873 "%s (type: %s) is not of boolean type ",
1874 boolval->get_debug_string (),
1875 boolval->get_type ()->get_debug_string ());
1876 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
1877 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
1878 RETURN_IF_FAIL_PRINTF4 (
1879 block->get_function () == on_true->get_function (),
1880 ctxt, loc,
1881 "\"on_true\" block is not in same function:"
1882 " source block %s is in function %s"
1883 " whereas target block %s is in function %s",
1884 block->get_debug_string (),
1885 block->get_function ()->get_debug_string (),
1886 on_true->get_debug_string (),
1887 on_true->get_function ()->get_debug_string ());
1888 RETURN_IF_FAIL_PRINTF4 (
1889 block->get_function () == on_false->get_function (),
1890 ctxt, loc,
1891 "\"on_false\" block is not in same function:"
1892 " source block %s is in function %s"
1893 " whereas target block %s is in function %s",
1894 block->get_debug_string (),
1895 block->get_function ()->get_debug_string (),
1896 on_false->get_debug_string (),
1897 on_false->get_function ()->get_debug_string ());
1899 return block->end_with_conditional (loc, boolval, on_true, on_false);
1902 /* Public entrypoint. See description in libgccjit.h.
1904 After error-checking, the real work is done by the
1905 gcc::jit::recording::block::add_comment method in
1906 jit-recording.c. */
1908 void
1909 gcc_jit_block_add_comment (gcc_jit_block *block,
1910 gcc_jit_location *loc,
1911 const char *text)
1913 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1914 gcc::jit::recording::context *ctxt = block->get_context ();
1915 JIT_LOG_FUNC (ctxt->get_logger ());
1916 /* LOC can be NULL. */
1917 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
1919 block->add_comment (loc, text);
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_jump method in
1926 jit-recording.c. */
1928 void
1929 gcc_jit_block_end_with_jump (gcc_jit_block *block,
1930 gcc_jit_location *loc,
1931 gcc_jit_block *target)
1933 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1934 gcc::jit::recording::context *ctxt = block->get_context ();
1935 JIT_LOG_FUNC (ctxt->get_logger ());
1936 /* LOC can be NULL. */
1937 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
1938 RETURN_IF_FAIL_PRINTF4 (
1939 block->get_function () == target->get_function (),
1940 ctxt, loc,
1941 "target block is not in same function:"
1942 " source block %s is in function %s"
1943 " whereas target block %s is in function %s",
1944 block->get_debug_string (),
1945 block->get_function ()->get_debug_string (),
1946 target->get_debug_string (),
1947 target->get_function ()->get_debug_string ());
1949 block->end_with_jump (loc, target);
1952 /* Public entrypoint. See description in libgccjit.h.
1954 After error-checking, the real work is done by the
1955 gcc::jit::recording::block::end_with_return method in
1956 jit-recording.c. */
1958 void
1959 gcc_jit_block_end_with_return (gcc_jit_block *block,
1960 gcc_jit_location *loc,
1961 gcc_jit_rvalue *rvalue)
1963 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1964 gcc::jit::recording::context *ctxt = block->get_context ();
1965 JIT_LOG_FUNC (ctxt->get_logger ());
1966 /* LOC can be NULL. */
1967 gcc::jit::recording::function *func = block->get_function ();
1968 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1969 RETURN_IF_FAIL_PRINTF4 (
1970 compatible_types (
1971 func->get_return_type (),
1972 rvalue->get_type ()),
1973 ctxt, loc,
1974 "mismatching types:"
1975 " return of %s (type: %s) in function %s (return type: %s)",
1976 rvalue->get_debug_string (),
1977 rvalue->get_type ()->get_debug_string (),
1978 func->get_debug_string (),
1979 func->get_return_type ()->get_debug_string ());
1981 return block->end_with_return (loc, rvalue);
1984 /* Public entrypoint. See description in libgccjit.h.
1986 After error-checking, the real work is done by the
1987 gcc::jit::recording::block::end_with_return method in
1988 jit-recording.c. */
1990 void
1991 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
1992 gcc_jit_location *loc)
1994 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1995 gcc::jit::recording::context *ctxt = block->get_context ();
1996 JIT_LOG_FUNC (ctxt->get_logger ());
1997 /* LOC can be NULL. */
1998 gcc::jit::recording::function *func = block->get_function ();
1999 RETURN_IF_FAIL_PRINTF2 (
2000 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2001 ctxt, loc,
2002 "mismatching types:"
2003 " void return in function %s (return type: %s)",
2004 func->get_debug_string (),
2005 func->get_return_type ()->get_debug_string ());
2007 return block->end_with_return (loc, NULL);
2010 /**********************************************************************
2011 Option-management
2012 **********************************************************************/
2014 /* Public entrypoint. See description in libgccjit.h.
2016 After error-checking, the real work is done by the
2017 gcc::jit::recording::context::set_str_option method in
2018 jit-recording.c. */
2020 void
2021 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2022 enum gcc_jit_str_option opt,
2023 const char *value)
2025 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2026 JIT_LOG_FUNC (ctxt->get_logger ());
2027 /* opt is checked by the inner function.
2028 value can be NULL. */
2030 ctxt->set_str_option (opt, value);
2033 /* Public entrypoint. See description in libgccjit.h.
2035 After error-checking, the real work is done by the
2036 gcc::jit::recording::context::set_int_option method in
2037 jit-recording.c. */
2039 void
2040 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2041 enum gcc_jit_int_option opt,
2042 int value)
2044 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2045 JIT_LOG_FUNC (ctxt->get_logger ());
2046 /* opt is checked by the inner function. */
2048 ctxt->set_int_option (opt, value);
2051 /* Public entrypoint. See description in libgccjit.h.
2053 After error-checking, the real work is done by the
2054 gcc::jit::recording::context::set_bool_option method in
2055 jit-recording.c. */
2057 void
2058 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2059 enum gcc_jit_bool_option opt,
2060 int value)
2062 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2063 JIT_LOG_FUNC (ctxt->get_logger ());
2064 /* opt is checked by the inner function. */
2066 ctxt->set_bool_option (opt, value);
2069 /* Public entrypoint. See description in libgccjit.h.
2071 After error-checking, the real work is done by the
2072 gcc::jit::recording::context::enable_dump method in
2073 jit-recording.c. */
2075 void
2076 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2077 const char *dumpname,
2078 char **out_ptr)
2080 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2081 JIT_LOG_FUNC (ctxt->get_logger ());
2082 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2083 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2085 ctxt->enable_dump (dumpname, out_ptr);
2088 /* Public entrypoint. See description in libgccjit.h.
2090 After error-checking, the real work is done by the
2091 gcc::jit::recording::context::compile method in
2092 jit-recording.c. */
2094 gcc_jit_result *
2095 gcc_jit_context_compile (gcc_jit_context *ctxt)
2097 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2099 JIT_LOG_FUNC (ctxt->get_logger ());
2101 ctxt->log ("compiling ctxt: %p", (void *)ctxt);
2103 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2105 ctxt->log ("%s: returning (gcc_jit_result *)%p",
2106 __func__, (void *)result);
2108 return result;
2111 /* Public entrypoint. See description in libgccjit.h.
2113 After error-checking, the real work is done by the
2114 gcc::jit::recording::context::dump_to_file method in
2115 jit-recording.c. */
2117 void
2118 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2119 const char *path,
2120 int update_locations)
2122 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2123 JIT_LOG_FUNC (ctxt->get_logger ());
2124 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2125 ctxt->dump_to_file (path, update_locations);
2128 /* Public entrypoint. See description in libgccjit.h. */
2130 void
2131 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2132 FILE *logfile,
2133 int flags,
2134 int verbosity)
2136 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2137 JIT_LOG_FUNC (ctxt->get_logger ());
2138 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2139 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2141 gcc::jit::logger *logger;
2142 if (logfile)
2143 logger = new gcc::jit::logger (logfile, flags, verbosity);
2144 else
2145 logger = NULL;
2146 ctxt->set_logger (logger);
2149 /* Public entrypoint. See description in libgccjit.h.
2151 After error-checking, the real work is done by the
2152 gcc::jit::recording::context::get_first_error method in
2153 jit-recording.c. */
2155 const char *
2156 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2158 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2159 JIT_LOG_FUNC (ctxt->get_logger ());
2161 return ctxt->get_first_error ();
2164 /* Public entrypoint. See description in libgccjit.h.
2166 After error-checking, the real work is done by the
2167 gcc::jit::recording::context::get_last_error method in
2168 jit-recording.c. */
2170 const char *
2171 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2173 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2175 return ctxt->get_last_error ();
2178 /* Public entrypoint. See description in libgccjit.h.
2180 After error-checking, the real work is done by the
2181 gcc::jit::result::get_code method in jit-result.c. */
2183 void *
2184 gcc_jit_result_get_code (gcc_jit_result *result,
2185 const char *fnname)
2187 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2188 JIT_LOG_FUNC (result->get_logger ());
2189 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2191 result->log ("locating fnname: %s", fnname);
2192 void *code = result->get_code (fnname);
2193 result->log ("%s: returning (void *)%p", __func__, code);
2195 return code;
2198 /* Public entrypoint. See description in libgccjit.h.
2200 After error-checking, this is essentially a wrapper around the
2201 destructor for gcc::jit::result in jit-result.c. */
2203 void
2204 gcc_jit_result_release (gcc_jit_result *result)
2206 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2207 JIT_LOG_FUNC (result->get_logger ());
2208 result->log ("deleting result: %p", (void *)result);
2209 delete result;