gcc/
[official-gcc.git] / gcc / jit / libgccjit.c
blobdedf942ebaaa74b51178b538e12f5698adb510d4
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_PRINTF5(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
176 JIT_BEGIN_STMT \
177 if (!(TEST_EXPR)) \
179 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
180 __func__, (A0), (A1), (A2), (A3), (A4)); \
181 return (RETURN_EXPR); \
183 JIT_END_STMT
185 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
186 JIT_BEGIN_STMT \
187 if (!(TEST_EXPR)) \
189 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
190 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
191 return (RETURN_EXPR); \
193 JIT_END_STMT
195 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
196 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
198 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
199 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
201 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
202 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
204 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
205 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
207 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
208 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
210 #define RETURN_NULL_IF_FAIL_PRINTF5(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
211 RETURN_VAL_IF_FAIL_PRINTF5 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4)
213 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
214 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
216 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
217 JIT_BEGIN_STMT \
218 if (!(TEST_EXPR)) \
220 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
221 return; \
223 JIT_END_STMT
225 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
226 JIT_BEGIN_STMT \
227 if (!(TEST_EXPR)) \
229 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
230 __func__, (A0)); \
231 return; \
233 JIT_END_STMT
235 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
236 JIT_BEGIN_STMT \
237 if (!(TEST_EXPR)) \
239 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
240 __func__, (A0), (A1)); \
241 return; \
243 JIT_END_STMT
245 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
246 JIT_BEGIN_STMT \
247 if (!(TEST_EXPR)) \
249 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
250 __func__, (A0), (A1), (A2), (A3)); \
251 return; \
253 JIT_END_STMT
255 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
256 it. This will fail if BLOCK has already been terminated by some
257 kind of jump or a return. */
258 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
259 JIT_BEGIN_STMT \
260 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
261 RETURN_IF_FAIL_PRINTF2 ( \
262 !(BLOCK)->has_been_terminated (), \
263 (BLOCK)->get_context (), \
264 (LOC), \
265 "adding to terminated block: %s (already terminated by: %s)", \
266 (BLOCK)->get_debug_string (), \
267 (BLOCK)->get_last_statement ()->get_debug_string ()); \
268 JIT_END_STMT
270 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
271 fails. */
272 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
273 JIT_BEGIN_STMT \
274 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
275 RETURN_NULL_IF_FAIL_PRINTF2 ( \
276 !(BLOCK)->has_been_terminated (), \
277 (BLOCK)->get_context (), \
278 (LOC), \
279 "adding to terminated block: %s (already terminated by: %s)", \
280 (BLOCK)->get_debug_string (), \
281 (BLOCK)->get_last_statement ()->get_debug_string ()); \
282 JIT_END_STMT
284 /* Format the given string, and report it as an error, either on CTXT
285 if non-NULL, or by printing to stderr if we have a NULL context.
286 LOC gives the source location where the error occcurred, and can be
287 NULL. */
289 static void
290 jit_error (gcc::jit::recording::context *ctxt,
291 gcc_jit_location *loc,
292 const char *fmt, ...)
293 GNU_PRINTF(3, 4);
295 static void
296 jit_error (gcc::jit::recording::context *ctxt,
297 gcc_jit_location *loc,
298 const char *fmt, ...)
300 va_list ap;
301 va_start (ap, fmt);
303 if (ctxt)
304 ctxt->add_error_va (loc, fmt, ap);
305 else
307 /* No context? Send to stderr. */
308 vfprintf (stderr, fmt, ap);
309 fprintf (stderr, "\n");
312 va_end (ap);
315 /* Determine whether or not we can write to lvalues of type LTYPE from
316 rvalues of type RTYPE, detecting type errors such as attempting to
317 write to an int with a string literal (without an explicit cast).
319 This is implemented by calling the
320 gcc::jit::recording::type::accepts_writes_from virtual function on
321 LTYPE. */
323 static bool
324 compatible_types (gcc::jit::recording::type *ltype,
325 gcc::jit::recording::type *rtype)
327 return ltype->accepts_writes_from (rtype);
330 /* Public entrypoint for acquiring a gcc_jit_context.
331 Note that this creates a new top-level context; contrast with
332 gcc_jit_context_new_child_context below.
334 The real work is done in the constructor for
335 gcc::jit::recording::context in jit-recording.c. */
337 gcc_jit_context *
338 gcc_jit_context_acquire (void)
340 gcc_jit_context *ctxt = new gcc_jit_context (NULL);
341 ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
342 return ctxt;
345 /* Public entrypoint for releasing a gcc_jit_context.
346 The real work is done in the destructor for
347 gcc::jit::recording::context in jit-recording.c. */
349 void
350 gcc_jit_context_release (gcc_jit_context *ctxt)
352 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
353 JIT_LOG_FUNC (ctxt->get_logger ());
354 ctxt->log ("deleting ctxt: %p", (void *)ctxt);
355 delete ctxt;
358 /* Public entrypoint for creating a child context within
359 PARENT_CTXT. See description in libgccjit.h.
361 The real work is done in the constructor for
362 gcc::jit::recording::context in jit-recording.c. */
364 gcc_jit_context *
365 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
367 RETURN_NULL_IF_FAIL (parent_ctxt, NULL, NULL, "NULL parent ctxt");
368 JIT_LOG_FUNC (parent_ctxt->get_logger ());
369 parent_ctxt->log ("parent_ctxt: %p", (void *)parent_ctxt);
370 gcc_jit_context *child_ctxt = new gcc_jit_context (parent_ctxt);
371 child_ctxt->log ("new child_ctxt: %p", (void *)child_ctxt);
372 return child_ctxt;
375 /* Public entrypoint. See description in libgccjit.h.
377 After error-checking, the real work is done by the
378 gcc::jit::recording::context::new_location
379 method in jit-recording.c. */
381 gcc_jit_location *
382 gcc_jit_context_new_location (gcc_jit_context *ctxt,
383 const char *filename,
384 int line,
385 int column)
387 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
388 JIT_LOG_FUNC (ctxt->get_logger ());
389 return (gcc_jit_location *)ctxt->new_location (filename, line, column, true);
392 /* Public entrypoint. See description in libgccjit.h.
394 After error-checking, this calls the trivial
395 gcc::jit::recording::memento::as_object method (a location is a
396 memento), in jit-recording.h. */
398 gcc_jit_object *
399 gcc_jit_location_as_object (gcc_jit_location *loc)
401 RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
403 return static_cast <gcc_jit_object *> (loc->as_object ());
406 /* Public entrypoint. See description in libgccjit.h.
408 After error-checking, this calls the trivial
409 gcc::jit::recording::memento::as_object method (a type is a
410 memento), in jit-recording.h. */
412 gcc_jit_object *
413 gcc_jit_type_as_object (gcc_jit_type *type)
415 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
417 return static_cast <gcc_jit_object *> (type->as_object ());
420 /* Public entrypoint for getting a specific type from a context.
422 After error-checking, the real work is done by the
423 gcc::jit::recording::context::get_type method, in
424 jit-recording.c */
426 gcc_jit_type *
427 gcc_jit_context_get_type (gcc_jit_context *ctxt,
428 enum gcc_jit_types type)
430 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
431 JIT_LOG_FUNC (ctxt->get_logger ());
432 RETURN_NULL_IF_FAIL_PRINTF1 (
433 (type >= GCC_JIT_TYPE_VOID
434 && type <= GCC_JIT_TYPE_FILE_PTR),
435 ctxt, NULL,
436 "unrecognized value for enum gcc_jit_types: %i", type);
438 return (gcc_jit_type *)ctxt->get_type (type);
441 /* Public entrypoint for getting the integer type of the given size and
442 signedness.
444 After error-checking, the real work is done by the
445 gcc::jit::recording::context::get_int_type method,
446 in jit-recording.c. */
448 gcc_jit_type *
449 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
450 int num_bytes, int is_signed)
452 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
453 JIT_LOG_FUNC (ctxt->get_logger ());
454 RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
456 return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
459 /* Public entrypoint. See description in libgccjit.h.
461 After error-checking, the real work is done by the
462 gcc::jit::recording::type::get_pointer method, in
463 jit-recording.c */
465 gcc_jit_type *
466 gcc_jit_type_get_pointer (gcc_jit_type *type)
468 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
470 return (gcc_jit_type *)type->get_pointer ();
473 /* Public entrypoint. See description in libgccjit.h.
475 After error-checking, the real work is done by the
476 gcc::jit::recording::type::get_const method, in
477 jit-recording.c. */
479 gcc_jit_type *
480 gcc_jit_type_get_const (gcc_jit_type *type)
482 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
484 return (gcc_jit_type *)type->get_const ();
487 /* Public entrypoint. See description in libgccjit.h.
489 After error-checking, the real work is done by the
490 gcc::jit::recording::type::get_volatile method, in
491 jit-recording.c. */
493 gcc_jit_type *
494 gcc_jit_type_get_volatile (gcc_jit_type *type)
496 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
498 return (gcc_jit_type *)type->get_volatile ();
501 /* Public entrypoint. See description in libgccjit.h.
503 After error-checking, the real work is done by the
504 gcc::jit::recording::context::new_array_type method, in
505 jit-recording.c. */
507 gcc_jit_type *
508 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
509 gcc_jit_location *loc,
510 gcc_jit_type *element_type,
511 int num_elements)
513 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
514 JIT_LOG_FUNC (ctxt->get_logger ());
515 /* LOC can be NULL. */
516 RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
517 RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
519 return (gcc_jit_type *)ctxt->new_array_type (loc,
520 element_type,
521 num_elements);
524 /* Public entrypoint. See description in libgccjit.h.
526 After error-checking, the real work is done by the
527 gcc::jit::recording::context::new_field method, in
528 jit-recording.c. */
530 gcc_jit_field *
531 gcc_jit_context_new_field (gcc_jit_context *ctxt,
532 gcc_jit_location *loc,
533 gcc_jit_type *type,
534 const char *name)
536 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
537 JIT_LOG_FUNC (ctxt->get_logger ());
538 /* LOC can be NULL. */
539 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
540 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
542 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
545 /* Public entrypoint. See description in libgccjit.h.
547 After error-checking, this calls the trivial
548 gcc::jit::recording::memento::as_object method (a field is a
549 memento), in jit-recording.h. */
551 gcc_jit_object *
552 gcc_jit_field_as_object (gcc_jit_field *field)
554 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
556 return static_cast <gcc_jit_object *> (field->as_object ());
559 /* Public entrypoint. See description in libgccjit.h.
561 After error-checking, the real work is done by the
562 gcc::jit::recording::context::new_struct_type method,
563 immediately followed by a "set_fields" call on the resulting
564 gcc::jit::recording::compound_type *, both in jit-recording.c */
566 gcc_jit_struct *
567 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
568 gcc_jit_location *loc,
569 const char *name,
570 int num_fields,
571 gcc_jit_field **fields)
573 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
574 JIT_LOG_FUNC (ctxt->get_logger ());
575 /* LOC can be NULL. */
576 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
577 if (num_fields)
578 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
579 for (int i = 0; i < num_fields; i++)
581 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
582 RETURN_NULL_IF_FAIL_PRINTF2 (
583 NULL == fields[i]->get_container (),
584 ctxt, loc,
585 "%s is already a field of %s",
586 fields[i]->get_debug_string (),
587 fields[i]->get_container ()->get_debug_string ());
590 gcc::jit::recording::struct_ *result =
591 ctxt->new_struct_type (loc, name);
592 result->set_fields (loc,
593 num_fields,
594 (gcc::jit::recording::field **)fields);
595 return static_cast<gcc_jit_struct *> (result);
598 /* Public entrypoint. See description in libgccjit.h.
600 After error-checking, the real work is done by the
601 gcc::jit::recording::context::new_struct_type method in
602 jit-recording.c. */
604 gcc_jit_struct *
605 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
606 gcc_jit_location *loc,
607 const char *name)
609 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
610 JIT_LOG_FUNC (ctxt->get_logger ());
611 /* LOC can be NULL. */
612 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
614 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
617 /* Public entrypoint. See description in libgccjit.h.
619 After error-checking, this calls the trivial
620 gcc::jit::recording::struct_::as_object method in
621 jit-recording.h. */
623 gcc_jit_type *
624 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
626 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
628 return static_cast <gcc_jit_type *> (struct_type->as_type ());
631 /* Public entrypoint. See description in libgccjit.h.
633 After error-checking, the real work is done by the
634 gcc::jit::recording::compound_type::set_fields method in
635 jit-recording.c. */
637 void
638 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
639 gcc_jit_location *loc,
640 int num_fields,
641 gcc_jit_field **fields)
643 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
644 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
645 JIT_LOG_FUNC (ctxt->get_logger ());
646 /* LOC can be NULL. */
647 RETURN_IF_FAIL_PRINTF1 (
648 NULL == struct_type->get_fields (), ctxt, loc,
649 "%s already has had fields set",
650 struct_type->get_debug_string ());
651 if (num_fields)
652 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
653 for (int i = 0; i < num_fields; i++)
655 RETURN_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
656 RETURN_IF_FAIL_PRINTF2 (
657 NULL == fields[i]->get_container (),
658 ctxt, loc,
659 "%s is already a field of %s",
660 fields[i]->get_debug_string (),
661 fields[i]->get_container ()->get_debug_string ());
664 struct_type->set_fields (loc, num_fields,
665 (gcc::jit::recording::field **)fields);
668 /* Public entrypoint. See description in libgccjit.h.
670 After error-checking, the real work is done by the
671 gcc::jit::recording::context::new_union_type method,
672 immediately followed by a "set_fields" call on the resulting
673 gcc::jit::recording::compound_type *, both in jit-recording.c */
675 gcc_jit_type *
676 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
677 gcc_jit_location *loc,
678 const char *name,
679 int num_fields,
680 gcc_jit_field **fields)
682 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
683 JIT_LOG_FUNC (ctxt->get_logger ());
684 /* LOC can be NULL. */
685 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
686 if (num_fields)
687 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
688 for (int i = 0; i < num_fields; i++)
690 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
691 RETURN_NULL_IF_FAIL_PRINTF2 (
692 NULL == fields[i]->get_container (),
693 ctxt, loc,
694 "%s is already a field of %s",
695 fields[i]->get_debug_string (),
696 fields[i]->get_container ()->get_debug_string ());
699 gcc::jit::recording::union_ *result =
700 ctxt->new_union_type (loc, name);
701 result->set_fields (loc,
702 num_fields,
703 (gcc::jit::recording::field **)fields);
704 return (gcc_jit_type *) (result);
707 /* Public entrypoint. See description in libgccjit.h.
709 After error-checking, the real work is done by the
710 gcc::jit::recording::context::new_function_ptr_type method,
711 in jit-recording.c */
713 gcc_jit_type *
714 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
715 gcc_jit_location *loc,
716 gcc_jit_type *return_type,
717 int num_params,
718 gcc_jit_type **param_types,
719 int is_variadic)
721 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
722 JIT_LOG_FUNC (ctxt->get_logger ());
723 /* LOC can be NULL. */
724 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
725 RETURN_NULL_IF_FAIL (
726 (num_params == 0) || param_types,
727 ctxt, loc,
728 "NULL param_types creating function pointer type");
729 for (int i = 0; i < num_params; i++)
730 RETURN_NULL_IF_FAIL_PRINTF1 (
731 param_types[i],
732 ctxt, loc,
733 "NULL parameter type %i creating function pointer type", i);
735 return (gcc_jit_type*)
736 ctxt->new_function_ptr_type (loc, return_type,
737 num_params,
738 (gcc::jit::recording::type **)param_types,
739 is_variadic);
742 /* Constructing functions. */
744 /* Public entrypoint. See description in libgccjit.h.
746 After error-checking, the real work is done by the
747 gcc::jit::recording::context::new_param method, in jit-recording.c */
749 gcc_jit_param *
750 gcc_jit_context_new_param (gcc_jit_context *ctxt,
751 gcc_jit_location *loc,
752 gcc_jit_type *type,
753 const char *name)
755 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
756 JIT_LOG_FUNC (ctxt->get_logger ());
757 /* LOC can be NULL. */
758 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
759 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
761 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
764 /* Public entrypoint. See description in libgccjit.h.
766 After error-checking, this calls the trivial
767 gcc::jit::recording::memento::as_object method (a param is a memento),
768 in jit-recording.h. */
770 gcc_jit_object *
771 gcc_jit_param_as_object (gcc_jit_param *param)
773 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
775 return static_cast <gcc_jit_object *> (param->as_object ());
778 /* Public entrypoint. See description in libgccjit.h.
780 After error-checking, this calls the trivial
781 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
783 gcc_jit_lvalue *
784 gcc_jit_param_as_lvalue (gcc_jit_param *param)
786 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
788 return (gcc_jit_lvalue *)param->as_lvalue ();
791 /* Public entrypoint. See description in libgccjit.h.
793 After error-checking, this calls the trivial
794 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
795 in jit-recording.h. */
797 gcc_jit_rvalue *
798 gcc_jit_param_as_rvalue (gcc_jit_param *param)
800 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
802 return (gcc_jit_rvalue *)param->as_rvalue ();
805 /* Public entrypoint. See description in libgccjit.h.
807 After error-checking, the real work is done by the
808 gcc::jit::recording::context::new_function method, in
809 jit-recording.c. */
811 gcc_jit_function *
812 gcc_jit_context_new_function (gcc_jit_context *ctxt,
813 gcc_jit_location *loc,
814 enum gcc_jit_function_kind kind,
815 gcc_jit_type *return_type,
816 const char *name,
817 int num_params,
818 gcc_jit_param **params,
819 int is_variadic)
821 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
822 JIT_LOG_FUNC (ctxt->get_logger ());
823 /* LOC can be NULL. */
824 RETURN_NULL_IF_FAIL_PRINTF1 (
825 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
826 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
827 ctxt, loc,
828 "unrecognized value for enum gcc_jit_function_kind: %i",
829 kind);
830 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
831 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
832 /* The assembler can only handle certain names, so for now, enforce
833 C's rules for identiers upon the name, using ISALPHA and ISALNUM
834 from safe-ctype.h to ignore the current locale.
835 Eventually we'll need some way to interact with e.g. C++ name
836 mangling. */
838 /* Leading char: */
839 char ch = *name;
840 RETURN_NULL_IF_FAIL_PRINTF2 (
841 ISALPHA (ch) || ch == '_',
842 ctxt, loc,
843 "name \"%s\" contains invalid character: '%c'",
844 name, ch);
845 /* Subsequent chars: */
846 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
848 RETURN_NULL_IF_FAIL_PRINTF2 (
849 ISALNUM (ch) || ch == '_',
850 ctxt, loc,
851 "name \"%s\" contains invalid character: '%c'",
852 name, ch);
855 RETURN_NULL_IF_FAIL_PRINTF1 (
856 (num_params == 0) || params,
857 ctxt, loc,
858 "NULL params creating function %s", name);
859 for (int i = 0; i < num_params; i++)
861 RETURN_NULL_IF_FAIL_PRINTF2 (
862 params[i],
863 ctxt, loc,
864 "NULL parameter %i creating function %s", i, name);
865 RETURN_NULL_IF_FAIL_PRINTF5 (
866 (NULL == params[i]->get_scope ()),
867 ctxt, loc,
868 "parameter %i \"%s\""
869 " (type: %s)"
870 " for function %s"
871 " was already used for function %s",
872 i, params[i]->get_debug_string (),
873 params[i]->get_type ()->get_debug_string (),
874 name,
875 params[i]->get_scope ()->get_debug_string ());
878 return (gcc_jit_function*)
879 ctxt->new_function (loc, kind, return_type, name,
880 num_params,
881 (gcc::jit::recording::param **)params,
882 is_variadic,
883 BUILT_IN_NONE);
886 /* Public entrypoint. See description in libgccjit.h.
888 After error-checking, the real work is done by the
889 gcc::jit::recording::context::get_builtin_function method, in
890 jit-recording.c. */
892 gcc_jit_function *
893 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
894 const char *name)
896 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
897 JIT_LOG_FUNC (ctxt->get_logger ());
898 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
900 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
903 /* Public entrypoint. See description in libgccjit.h.
905 After error-checking, this calls the trivial
906 gcc::jit::recording::memento::as_object method (a function is a
907 memento), in jit-recording.h. */
909 gcc_jit_object *
910 gcc_jit_function_as_object (gcc_jit_function *func)
912 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
914 return static_cast <gcc_jit_object *> (func->as_object ());
917 /* Public entrypoint. See description in libgccjit.h.
919 After error-checking, the real work is done by the
920 gcc::jit::recording::function::get_param method, in
921 jit-recording.h. */
923 gcc_jit_param *
924 gcc_jit_function_get_param (gcc_jit_function *func, int index)
926 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
927 gcc::jit::recording::context *ctxt = func->m_ctxt;
928 JIT_LOG_FUNC (ctxt->get_logger ());
929 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
930 int num_params = func->get_params ().length ();
931 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
932 ctxt, NULL,
933 "index of %d is too large (%s has %d params)",
934 index,
935 func->get_debug_string (),
936 num_params);
938 return static_cast <gcc_jit_param *> (func->get_param (index));
941 /* Public entrypoint. See description in libgccjit.h.
943 After error-checking, the real work is done by the
944 gcc::jit::recording::function::dump_to_dot method, in
945 jit-recording.c. */
947 void
948 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
949 const char *path)
951 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
952 gcc::jit::recording::context *ctxt = func->m_ctxt;
953 JIT_LOG_FUNC (ctxt->get_logger ());
954 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
956 func->dump_to_dot (path);
959 /* Public entrypoint. See description in libgccjit.h.
961 After error-checking, the real work is done by the
962 gcc::jit::recording::function::new_block method, in
963 jit-recording.c. */
965 gcc_jit_block*
966 gcc_jit_function_new_block (gcc_jit_function *func,
967 const char *name)
969 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
970 JIT_LOG_FUNC (func->get_context ()->get_logger ());
971 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
972 func->get_context (), NULL,
973 "cannot add block to an imported function");
974 /* name can be NULL. */
976 return (gcc_jit_block *)func->new_block (name);
979 /* Public entrypoint. See description in libgccjit.h.
981 After error-checking, this calls the trivial
982 gcc::jit::recording::memento::as_object method (a block is a
983 memento), in jit-recording.h. */
985 gcc_jit_object *
986 gcc_jit_block_as_object (gcc_jit_block *block)
988 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
990 return static_cast <gcc_jit_object *> (block->as_object ());
993 /* Public entrypoint. See description in libgccjit.h.
995 After error-checking, the real work is done by the
996 gcc::jit::recording::block::get_function method, in
997 jit-recording.h. */
999 gcc_jit_function *
1000 gcc_jit_block_get_function (gcc_jit_block *block)
1002 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1004 return static_cast <gcc_jit_function *> (block->get_function ());
1007 /* Public entrypoint. See description in libgccjit.h.
1009 After error-checking, the real work is done by the
1010 gcc::jit::recording::context::new_global method, in
1011 jit-recording.c. */
1013 gcc_jit_lvalue *
1014 gcc_jit_context_new_global (gcc_jit_context *ctxt,
1015 gcc_jit_location *loc,
1016 enum gcc_jit_global_kind kind,
1017 gcc_jit_type *type,
1018 const char *name)
1020 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1021 JIT_LOG_FUNC (ctxt->get_logger ());
1022 /* LOC can be NULL. */
1023 RETURN_NULL_IF_FAIL_PRINTF1 (
1024 ((kind >= GCC_JIT_GLOBAL_EXPORTED)
1025 && (kind <= GCC_JIT_GLOBAL_IMPORTED)),
1026 ctxt, loc,
1027 "unrecognized value for enum gcc_jit_global_kind: %i",
1028 kind);
1029 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1030 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1032 return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
1035 /* Public entrypoint. See description in libgccjit.h.
1037 After error-checking, this calls the trivial
1038 gcc::jit::recording::memento::as_object method (an lvalue is a
1039 memento), in jit-recording.h. */
1041 gcc_jit_object *
1042 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1044 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1046 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1049 /* Public entrypoint. See description in libgccjit.h.
1051 After error-checking, this calls the trivial
1052 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1054 gcc_jit_rvalue *
1055 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1057 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1059 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1062 /* Public entrypoint. See description in libgccjit.h.
1064 After error-checking, this calls the trivial
1065 gcc::jit::recording::memento::as_object method (an rvalue is a
1066 memento), in jit-recording.h. */
1068 gcc_jit_object *
1069 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1071 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1073 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1076 /* Public entrypoint. See description in libgccjit.h.
1078 After error-checking, the real work is done by the
1079 gcc::jit::recording::rvalue::get_type method, in
1080 jit-recording.h. */
1082 gcc_jit_type *
1083 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1085 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1087 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1090 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1091 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1092 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1094 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1095 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1096 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1097 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1098 "not a numeric type: %s", \
1099 NUMERIC_TYPE->get_debug_string ());
1101 /* Public entrypoint. See description in libgccjit.h.
1103 After error-checking, the real work is done by the
1104 gcc::jit::recording::context::new_rvalue_from_int method in
1105 jit-recording.c. */
1107 gcc_jit_rvalue *
1108 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1109 gcc_jit_type *numeric_type,
1110 int value)
1112 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1113 JIT_LOG_FUNC (ctxt->get_logger ());
1114 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1116 return ((gcc_jit_rvalue *)ctxt
1117 ->new_rvalue_from_const <int> (numeric_type, value));
1120 /* FIXME. */
1122 gcc_jit_rvalue *
1123 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1124 gcc_jit_type *numeric_type,
1125 long value)
1127 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1128 JIT_LOG_FUNC (ctxt->get_logger ());
1129 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1131 return ((gcc_jit_rvalue *)ctxt
1132 ->new_rvalue_from_const <long> (numeric_type, value));
1135 /* Public entrypoint. See description in libgccjit.h.
1137 This is essentially equivalent to:
1138 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1139 albeit with slightly different error messages if an error occurs. */
1141 gcc_jit_rvalue *
1142 gcc_jit_context_zero (gcc_jit_context *ctxt,
1143 gcc_jit_type *numeric_type)
1145 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1146 JIT_LOG_FUNC (ctxt->get_logger ());
1147 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1149 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1152 /* Public entrypoint. See description in libgccjit.h.
1154 This is essentially equivalent to:
1155 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1156 albeit with slightly different error messages if an error occurs. */
1158 gcc_jit_rvalue *
1159 gcc_jit_context_one (gcc_jit_context *ctxt,
1160 gcc_jit_type *numeric_type)
1162 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1163 JIT_LOG_FUNC (ctxt->get_logger ());
1164 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1166 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1169 /* Public entrypoint. See description in libgccjit.h.
1171 After error-checking, the real work is done by the
1172 gcc::jit::recording::context::new_rvalue_from_double method in
1173 jit-recording.c. */
1175 gcc_jit_rvalue *
1176 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1177 gcc_jit_type *numeric_type,
1178 double value)
1180 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1181 JIT_LOG_FUNC (ctxt->get_logger ());
1182 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1184 return ((gcc_jit_rvalue *)ctxt
1185 ->new_rvalue_from_const <double> (numeric_type, value));
1188 /* Public entrypoint. See description in libgccjit.h.
1190 After error-checking, the real work is done by the
1191 gcc::jit::recording::context::new_rvalue_from_ptr method in
1192 jit-recording.c. */
1194 gcc_jit_rvalue *
1195 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1196 gcc_jit_type *pointer_type,
1197 void *value)
1199 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1200 JIT_LOG_FUNC (ctxt->get_logger ());
1201 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1202 RETURN_NULL_IF_FAIL_PRINTF1 (
1203 pointer_type->is_pointer (),
1204 ctxt, NULL,
1205 "not a pointer type (type: %s)",
1206 pointer_type->get_debug_string ());
1208 return ((gcc_jit_rvalue *)ctxt
1209 ->new_rvalue_from_const <void *> (pointer_type, value));
1212 /* Public entrypoint. See description in libgccjit.h.
1214 This is essentially equivalent to:
1215 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1216 albeit with slightly different error messages if an error occurs. */
1218 gcc_jit_rvalue *
1219 gcc_jit_context_null (gcc_jit_context *ctxt,
1220 gcc_jit_type *pointer_type)
1222 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1223 JIT_LOG_FUNC (ctxt->get_logger ());
1224 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1225 RETURN_NULL_IF_FAIL_PRINTF1 (
1226 pointer_type->is_pointer (),
1227 ctxt, NULL,
1228 "not a pointer type (type: %s)",
1229 pointer_type->get_debug_string ());
1231 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1234 /* Public entrypoint. See description in libgccjit.h.
1236 After error-checking, the real work is done by the
1237 gcc::jit::recording::context::new_string_literal method in
1238 jit-recording.c. */
1240 gcc_jit_rvalue *
1241 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1242 const char *value)
1244 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1245 JIT_LOG_FUNC (ctxt->get_logger ());
1246 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1248 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1251 /* Public entrypoint. See description in libgccjit.h.
1253 After error-checking, the real work is done by the
1254 gcc::jit::recording::context::new_unary_op method in
1255 jit-recording.c. */
1257 gcc_jit_rvalue *
1258 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1259 gcc_jit_location *loc,
1260 enum gcc_jit_unary_op op,
1261 gcc_jit_type *result_type,
1262 gcc_jit_rvalue *rvalue)
1264 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1265 JIT_LOG_FUNC (ctxt->get_logger ());
1266 /* LOC can be NULL. */
1267 RETURN_NULL_IF_FAIL_PRINTF1 (
1268 (op >= GCC_JIT_UNARY_OP_MINUS
1269 && op <= GCC_JIT_UNARY_OP_ABS),
1270 ctxt, loc,
1271 "unrecognized value for enum gcc_jit_unary_op: %i",
1272 op);
1273 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1274 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1276 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1279 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1280 For use by both gcc_jit_context_new_binary_op and
1281 gcc_jit_block_add_assignment_op. */
1283 static bool
1284 valid_binary_op_p (enum gcc_jit_binary_op op)
1286 return (op >= GCC_JIT_BINARY_OP_PLUS
1287 && op <= GCC_JIT_BINARY_OP_RSHIFT);
1290 /* Public entrypoint. See description in libgccjit.h.
1292 After error-checking, the real work is done by the
1293 gcc::jit::recording::context::new_binary_op method in
1294 jit-recording.c. */
1296 gcc_jit_rvalue *
1297 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1298 gcc_jit_location *loc,
1299 enum gcc_jit_binary_op op,
1300 gcc_jit_type *result_type,
1301 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1303 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1304 JIT_LOG_FUNC (ctxt->get_logger ());
1305 /* LOC can be NULL. */
1306 RETURN_NULL_IF_FAIL_PRINTF1 (
1307 valid_binary_op_p (op),
1308 ctxt, loc,
1309 "unrecognized value for enum gcc_jit_binary_op: %i",
1310 op);
1311 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1312 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1313 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1314 RETURN_NULL_IF_FAIL_PRINTF4 (
1315 a->get_type () == b->get_type (),
1316 ctxt, loc,
1317 "mismatching types for binary op:"
1318 " a: %s (type: %s) b: %s (type: %s)",
1319 a->get_debug_string (),
1320 a->get_type ()->get_debug_string (),
1321 b->get_debug_string (),
1322 b->get_type ()->get_debug_string ());
1324 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1327 /* Public entrypoint. See description in libgccjit.h.
1329 After error-checking, the real work is done by the
1330 gcc::jit::recording::context::new_comparison method in
1331 jit-recording.c. */
1333 gcc_jit_rvalue *
1334 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1335 gcc_jit_location *loc,
1336 enum gcc_jit_comparison op,
1337 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1339 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1340 JIT_LOG_FUNC (ctxt->get_logger ());
1341 /* LOC can be NULL. */
1342 RETURN_NULL_IF_FAIL_PRINTF1 (
1343 (op >= GCC_JIT_COMPARISON_EQ
1344 && op <= GCC_JIT_COMPARISON_GE),
1345 ctxt, loc,
1346 "unrecognized value for enum gcc_jit_comparison: %i",
1347 op);
1348 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1349 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1350 RETURN_NULL_IF_FAIL_PRINTF4 (
1351 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1352 ctxt, loc,
1353 "mismatching types for comparison:"
1354 " a: %s (type: %s) b: %s (type: %s)",
1355 a->get_debug_string (),
1356 a->get_type ()->get_debug_string (),
1357 b->get_debug_string (),
1358 b->get_type ()->get_debug_string ());
1360 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1363 /* Public entrypoint. See description in libgccjit.h.
1365 After error-checking, the real work is done by the
1366 gcc::jit::recording::context::new_call method in
1367 jit-recording.c. */
1369 gcc_jit_rvalue *
1370 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1371 gcc_jit_location *loc,
1372 gcc_jit_function *func,
1373 int numargs , gcc_jit_rvalue **args)
1375 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1376 JIT_LOG_FUNC (ctxt->get_logger ());
1377 /* LOC can be NULL. */
1378 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1379 if (numargs)
1380 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1382 int min_num_params = func->get_params ().length ();
1383 bool is_variadic = func->is_variadic ();
1385 RETURN_NULL_IF_FAIL_PRINTF3 (
1386 numargs >= min_num_params,
1387 ctxt, loc,
1388 "not enough arguments to function \"%s\""
1389 " (got %i args, expected %i)",
1390 func->get_name ()->c_str (),
1391 numargs, min_num_params);
1393 RETURN_NULL_IF_FAIL_PRINTF3 (
1394 (numargs == min_num_params || is_variadic),
1395 ctxt, loc,
1396 "too many arguments to function \"%s\""
1397 " (got %i args, expected %i)",
1398 func->get_name ()->c_str (),
1399 numargs, min_num_params);
1401 for (int i = 0; i < min_num_params; i++)
1403 gcc::jit::recording::param *param = func->get_param (i);
1404 gcc_jit_rvalue *arg = args[i];
1406 RETURN_NULL_IF_FAIL_PRINTF4 (
1407 arg,
1408 ctxt, loc,
1409 "NULL argument %i to function \"%s\":"
1410 " param %s (type: %s)",
1411 i + 1,
1412 func->get_name ()->c_str (),
1413 param->get_debug_string (),
1414 param->get_type ()->get_debug_string ());
1416 RETURN_NULL_IF_FAIL_PRINTF6 (
1417 compatible_types (param->get_type (),
1418 arg->get_type ()),
1419 ctxt, loc,
1420 "mismatching types for argument %d of function \"%s\":"
1421 " assignment to param %s (type: %s) from %s (type: %s)",
1422 i + 1,
1423 func->get_name ()->c_str (),
1424 param->get_debug_string (),
1425 param->get_type ()->get_debug_string (),
1426 arg->get_debug_string (),
1427 arg->get_type ()->get_debug_string ());
1430 return (gcc_jit_rvalue *)ctxt->new_call (loc,
1431 func,
1432 numargs,
1433 (gcc::jit::recording::rvalue **)args);
1436 /* Public entrypoint. See description in libgccjit.h.
1438 After error-checking, the real work is done by the
1439 gcc::jit::recording::context::new_call_through_ptr method in
1440 jit-recording.c. */
1442 gcc_jit_rvalue *
1443 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1444 gcc_jit_location *loc,
1445 gcc_jit_rvalue *fn_ptr,
1446 int numargs, gcc_jit_rvalue **args)
1448 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1449 JIT_LOG_FUNC (ctxt->get_logger ());
1450 /* LOC can be NULL. */
1451 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1452 if (numargs)
1453 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1455 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1456 RETURN_NULL_IF_FAIL_PRINTF2 (
1457 ptr_type, ctxt, loc,
1458 "fn_ptr is not a ptr: %s"
1459 " type: %s",
1460 fn_ptr->get_debug_string (),
1461 fn_ptr->get_type ()->get_debug_string ());
1463 gcc::jit::recording::function_type *fn_type =
1464 ptr_type->dyn_cast_function_type();
1465 RETURN_NULL_IF_FAIL_PRINTF2 (
1466 fn_type, ctxt, loc,
1467 "fn_ptr is not a function ptr: %s"
1468 " type: %s",
1469 fn_ptr->get_debug_string (),
1470 fn_ptr->get_type ()->get_debug_string ());
1472 int min_num_params = fn_type->get_param_types ().length ();
1473 bool is_variadic = fn_type->is_variadic ();
1475 RETURN_NULL_IF_FAIL_PRINTF3 (
1476 numargs >= min_num_params,
1477 ctxt, loc,
1478 "not enough arguments to fn_ptr: %s"
1479 " (got %i args, expected %i)",
1480 fn_ptr->get_debug_string (),
1481 numargs, min_num_params);
1483 RETURN_NULL_IF_FAIL_PRINTF3 (
1484 (numargs == min_num_params || is_variadic),
1485 ctxt, loc,
1486 "too many arguments to fn_ptr: %s"
1487 " (got %i args, expected %i)",
1488 fn_ptr->get_debug_string (),
1489 numargs, min_num_params);
1491 for (int i = 0; i < min_num_params; i++)
1493 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1494 gcc_jit_rvalue *arg = args[i];
1496 RETURN_NULL_IF_FAIL_PRINTF3 (
1497 arg,
1498 ctxt, loc,
1499 "NULL argument %i to fn_ptr: %s"
1500 " (type: %s)",
1501 i + 1,
1502 fn_ptr->get_debug_string (),
1503 param_type->get_debug_string ());
1505 RETURN_NULL_IF_FAIL_PRINTF6 (
1506 compatible_types (param_type,
1507 arg->get_type ()),
1508 ctxt, loc,
1509 "mismatching types for argument %d of fn_ptr: %s:"
1510 " assignment to param %d (type: %s) from %s (type: %s)",
1511 i + 1,
1512 fn_ptr->get_debug_string (),
1513 i + 1,
1514 param_type->get_debug_string (),
1515 arg->get_debug_string (),
1516 arg->get_type ()->get_debug_string ());
1519 return (gcc_jit_rvalue *)(
1520 ctxt->new_call_through_ptr (loc,
1521 fn_ptr,
1522 numargs,
1523 (gcc::jit::recording::rvalue **)args));
1526 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1527 to DST_TYPE, for use by gcc_jit_context_new_cast.
1529 We only permit these kinds of cast:
1531 int <-> float
1532 int <-> bool
1533 P* <-> Q* for pointer types P and Q. */
1535 static bool
1536 is_valid_cast (gcc::jit::recording::type *src_type,
1537 gcc_jit_type *dst_type)
1539 bool src_is_int = src_type->is_int ();
1540 bool dst_is_int = dst_type->is_int ();
1541 bool src_is_float = src_type->is_float ();
1542 bool dst_is_float = dst_type->is_float ();
1543 bool src_is_bool = src_type->is_bool ();
1544 bool dst_is_bool = dst_type->is_bool ();
1546 if (src_is_int)
1547 if (dst_is_int || dst_is_float || dst_is_bool)
1548 return true;
1550 if (src_is_float)
1551 if (dst_is_int || dst_is_float)
1552 return true;
1554 if (src_is_bool)
1555 if (dst_is_int || dst_is_bool)
1556 return true;
1558 /* Permit casts between pointer types. */
1559 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1560 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1561 if (deref_src_type && deref_dst_type)
1562 return true;
1564 return false;
1567 /* Public entrypoint. See description in libgccjit.h.
1569 After error-checking, the real work is done by the
1570 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1572 gcc_jit_rvalue *
1573 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1574 gcc_jit_location *loc,
1575 gcc_jit_rvalue *rvalue,
1576 gcc_jit_type *type)
1578 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1579 JIT_LOG_FUNC (ctxt->get_logger ());
1580 /* LOC can be NULL. */
1581 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1582 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1583 RETURN_NULL_IF_FAIL_PRINTF3 (
1584 is_valid_cast (rvalue->get_type (), type),
1585 ctxt, loc,
1586 "cannot cast %s from type: %s to type: %s",
1587 rvalue->get_debug_string (),
1588 rvalue->get_type ()->get_debug_string (),
1589 type->get_debug_string ());
1591 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1594 /* Public entrypoint. See description in libgccjit.h.
1596 After error-checking, the real work is done by the
1597 gcc::jit::recording::context::new_array_access method in
1598 jit-recording.c. */
1600 extern gcc_jit_lvalue *
1601 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1602 gcc_jit_location *loc,
1603 gcc_jit_rvalue *ptr,
1604 gcc_jit_rvalue *index)
1606 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1607 JIT_LOG_FUNC (ctxt->get_logger ());
1608 /* LOC can be NULL. */
1609 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1610 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1611 RETURN_NULL_IF_FAIL_PRINTF2 (
1612 ptr->get_type ()->dereference (),
1613 ctxt, loc,
1614 "ptr: %s (type: %s) is not a pointer or array",
1615 ptr->get_debug_string (),
1616 ptr->get_type ()->get_debug_string ());
1617 RETURN_NULL_IF_FAIL_PRINTF2 (
1618 index->get_type ()->is_numeric (),
1619 ctxt, loc,
1620 "index: %s (type: %s) is not of numeric type",
1621 index->get_debug_string (),
1622 index->get_type ()->get_debug_string ());
1624 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1627 /* Public entrypoint. See description in libgccjit.h.
1629 After error-checking, the real work is done by the
1630 gcc::jit::recording::memento::get_context method in
1631 jit-recording.h. */
1633 gcc_jit_context *
1634 gcc_jit_object_get_context (gcc_jit_object *obj)
1636 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1638 return static_cast <gcc_jit_context *> (obj->get_context ());
1641 /* Public entrypoint. See description in libgccjit.h.
1643 After error-checking, the real work is done by the
1644 gcc::jit::recording::memento::get_debug_string method in
1645 jit-recording.c. */
1647 const char *
1648 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1650 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1652 return obj->get_debug_string ();
1655 /* Public entrypoint. See description in libgccjit.h.
1657 After error-checking, the real work is done by the
1658 gcc::jit::recording::lvalue::access_field method in
1659 jit-recording.c. */
1661 gcc_jit_lvalue *
1662 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1663 gcc_jit_location *loc,
1664 gcc_jit_field *field)
1666 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1667 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1668 JIT_LOG_FUNC (ctxt->get_logger ());
1669 /* LOC can be NULL. */
1670 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1671 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1672 "field %s has not been placed in a struct",
1673 field->get_debug_string ());
1674 gcc::jit::recording::type *underlying_type =
1675 struct_->get_type ();
1676 RETURN_NULL_IF_FAIL_PRINTF2 (
1677 (field->get_container ()->unqualified ()
1678 == underlying_type->unqualified ()),
1679 struct_->m_ctxt, loc,
1680 "%s is not a field of %s",
1681 field->get_debug_string (),
1682 underlying_type->get_debug_string ());
1684 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1687 /* Public entrypoint. See description in libgccjit.h.
1689 After error-checking, the real work is done by the
1690 gcc::jit::recording::rvalue::access_field method in
1691 jit-recording.c. */
1693 gcc_jit_rvalue *
1694 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1695 gcc_jit_location *loc,
1696 gcc_jit_field *field)
1698 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1699 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1700 JIT_LOG_FUNC (ctxt->get_logger ());
1701 /* LOC can be NULL. */
1702 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1703 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1704 "field %s has not been placed in a struct",
1705 field->get_debug_string ());
1706 gcc::jit::recording::type *underlying_type =
1707 struct_->get_type ();
1708 RETURN_NULL_IF_FAIL_PRINTF2 (
1709 (field->get_container ()->unqualified ()
1710 == underlying_type->unqualified ()),
1711 struct_->m_ctxt, loc,
1712 "%s is not a field of %s",
1713 field->get_debug_string (),
1714 underlying_type->get_debug_string ());
1716 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1719 /* Public entrypoint. See description in libgccjit.h.
1721 After error-checking, the real work is done by the
1722 gcc::jit::recording::rvalue::deference_field method in
1723 jit-recording.c. */
1725 gcc_jit_lvalue *
1726 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1727 gcc_jit_location *loc,
1728 gcc_jit_field *field)
1730 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1731 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1732 /* LOC can be NULL. */
1733 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1734 gcc::jit::recording::type *underlying_type =
1735 ptr->get_type ()->is_pointer ();
1736 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1737 "field %s has not been placed in a struct",
1738 field->get_debug_string ());
1739 RETURN_NULL_IF_FAIL_PRINTF3 (
1740 underlying_type,
1741 ptr->m_ctxt, loc,
1742 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1743 ptr->get_debug_string (),
1744 ptr->get_type ()->get_debug_string (),
1745 field->get_debug_string ());
1746 RETURN_NULL_IF_FAIL_PRINTF2 (
1747 (field->get_container ()->unqualified ()
1748 == underlying_type->unqualified ()),
1749 ptr->m_ctxt, loc,
1750 "%s is not a field of %s",
1751 field->get_debug_string (),
1752 underlying_type->get_debug_string ());
1754 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1757 /* Public entrypoint. See description in libgccjit.h.
1759 After error-checking, the real work is done by the
1760 gcc::jit::recording::rvalue::deference method in
1761 jit-recording.c. */
1763 gcc_jit_lvalue *
1764 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1765 gcc_jit_location *loc)
1767 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1768 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1769 /* LOC can be NULL. */
1771 gcc::jit::recording::type *underlying_type =
1772 rvalue->get_type ()->is_pointer ();
1774 RETURN_NULL_IF_FAIL_PRINTF2 (
1775 underlying_type,
1776 rvalue->m_ctxt, loc,
1777 "dereference of non-pointer %s (type: %s)",
1778 rvalue->get_debug_string (),
1779 rvalue->get_type ()->get_debug_string ());
1781 RETURN_NULL_IF_FAIL_PRINTF2 (
1782 !underlying_type->is_void (),
1783 rvalue->m_ctxt, loc,
1784 "dereference of void pointer %s (type: %s)",
1785 rvalue->get_debug_string (),
1786 rvalue->get_type ()->get_debug_string ());
1788 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1791 /* Public entrypoint. See description in libgccjit.h.
1793 After error-checking, the real work is done by the
1794 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1796 gcc_jit_rvalue *
1797 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1798 gcc_jit_location *loc)
1800 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1801 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1802 /* LOC can be NULL. */
1804 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1807 /* Public entrypoint. See description in libgccjit.h.
1809 After error-checking, the real work is done by the
1810 gcc::jit::recording::function::new_local method in jit-recording.c. */
1812 gcc_jit_lvalue *
1813 gcc_jit_function_new_local (gcc_jit_function *func,
1814 gcc_jit_location *loc,
1815 gcc_jit_type *type,
1816 const char *name)
1818 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1819 gcc::jit::recording::context *ctxt = func->m_ctxt;
1820 JIT_LOG_FUNC (ctxt->get_logger ());
1821 /* LOC can be NULL. */
1822 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1823 ctxt, loc,
1824 "Cannot add locals to an imported function");
1825 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1826 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1828 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1831 /* Public entrypoint. See description in libgccjit.h.
1833 After error-checking, the real work is done by the
1834 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1836 void
1837 gcc_jit_block_add_eval (gcc_jit_block *block,
1838 gcc_jit_location *loc,
1839 gcc_jit_rvalue *rvalue)
1841 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1842 gcc::jit::recording::context *ctxt = block->get_context ();
1843 JIT_LOG_FUNC (ctxt->get_logger ());
1844 /* LOC can be NULL. */
1845 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1847 gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
1849 /* "stmt" should be good enough to be usable in error-messages,
1850 but might still not be compilable; perform some more
1851 error-checking here. We do this here so that the error messages
1852 can contain a stringified version of "stmt", whilst appearing
1853 as close as possible to the point of failure. */
1854 rvalue->verify_valid_within_stmt (__func__, stmt);
1857 /* Public entrypoint. See description in libgccjit.h.
1859 After error-checking, the real work is done by the
1860 gcc::jit::recording::block::add_assignment method in
1861 jit-recording.c. */
1863 void
1864 gcc_jit_block_add_assignment (gcc_jit_block *block,
1865 gcc_jit_location *loc,
1866 gcc_jit_lvalue *lvalue,
1867 gcc_jit_rvalue *rvalue)
1869 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1870 gcc::jit::recording::context *ctxt = block->get_context ();
1871 JIT_LOG_FUNC (ctxt->get_logger ());
1872 /* LOC can be NULL. */
1873 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1874 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1875 RETURN_IF_FAIL_PRINTF4 (
1876 compatible_types (lvalue->get_type (),
1877 rvalue->get_type ()),
1878 ctxt, loc,
1879 "mismatching types:"
1880 " assignment to %s (type: %s) from %s (type: %s)",
1881 lvalue->get_debug_string (),
1882 lvalue->get_type ()->get_debug_string (),
1883 rvalue->get_debug_string (),
1884 rvalue->get_type ()->get_debug_string ());
1886 gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
1888 /* "stmt" should be good enough to be usable in error-messages,
1889 but might still not be compilable; perform some more
1890 error-checking here. We do this here so that the error messages
1891 can contain a stringified version of "stmt", whilst appearing
1892 as close as possible to the point of failure. */
1893 lvalue->verify_valid_within_stmt (__func__, stmt);
1894 rvalue->verify_valid_within_stmt (__func__, stmt);
1897 /* Public entrypoint. See description in libgccjit.h.
1899 After error-checking, the real work is done by the
1900 gcc::jit::recording::block::add_assignment_op method in
1901 jit-recording.c. */
1903 void
1904 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1905 gcc_jit_location *loc,
1906 gcc_jit_lvalue *lvalue,
1907 enum gcc_jit_binary_op op,
1908 gcc_jit_rvalue *rvalue)
1910 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1911 gcc::jit::recording::context *ctxt = block->get_context ();
1912 JIT_LOG_FUNC (ctxt->get_logger ());
1913 /* LOC can be NULL. */
1914 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1915 RETURN_IF_FAIL_PRINTF1 (
1916 valid_binary_op_p (op),
1917 ctxt, loc,
1918 "unrecognized value for enum gcc_jit_binary_op: %i",
1919 op);
1920 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1921 RETURN_IF_FAIL_PRINTF4 (
1922 compatible_types (lvalue->get_type (),
1923 rvalue->get_type ()),
1924 ctxt, loc,
1925 "mismatching types:"
1926 " assignment to %s (type: %s) involving %s (type: %s)",
1927 lvalue->get_debug_string (),
1928 lvalue->get_type ()->get_debug_string (),
1929 rvalue->get_debug_string (),
1930 rvalue->get_type ()->get_debug_string ());
1932 gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
1934 /* "stmt" should be good enough to be usable in error-messages,
1935 but might still not be compilable; perform some more
1936 error-checking here. We do this here so that the error messages
1937 can contain a stringified version of "stmt", whilst appearing
1938 as close as possible to the point of failure. */
1939 lvalue->verify_valid_within_stmt (__func__, stmt);
1940 rvalue->verify_valid_within_stmt (__func__, stmt);
1943 /* Internal helper function for determining if rvalue BOOLVAL is of
1944 boolean type. For use by gcc_jit_block_end_with_conditional. */
1946 static bool
1947 is_bool (gcc_jit_rvalue *boolval)
1949 gcc::jit::recording::type *actual_type = boolval->get_type ();
1950 gcc::jit::recording::type *bool_type =
1951 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1952 return actual_type == bool_type;
1955 /* Public entrypoint. See description in libgccjit.h.
1957 After error-checking, the real work is done by the
1958 gcc::jit::recording::block::end_with_conditional method in
1959 jit-recording.c. */
1961 void
1962 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1963 gcc_jit_location *loc,
1964 gcc_jit_rvalue *boolval,
1965 gcc_jit_block *on_true,
1966 gcc_jit_block *on_false)
1968 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1969 gcc::jit::recording::context *ctxt = block->get_context ();
1970 JIT_LOG_FUNC (ctxt->get_logger ());
1971 /* LOC can be NULL. */
1972 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
1973 RETURN_IF_FAIL_PRINTF2 (
1974 is_bool (boolval), ctxt, loc,
1975 "%s (type: %s) is not of boolean type ",
1976 boolval->get_debug_string (),
1977 boolval->get_type ()->get_debug_string ());
1978 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
1979 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
1980 RETURN_IF_FAIL_PRINTF4 (
1981 block->get_function () == on_true->get_function (),
1982 ctxt, loc,
1983 "\"on_true\" block is not in same function:"
1984 " source block %s is in function %s"
1985 " whereas target block %s is in function %s",
1986 block->get_debug_string (),
1987 block->get_function ()->get_debug_string (),
1988 on_true->get_debug_string (),
1989 on_true->get_function ()->get_debug_string ());
1990 RETURN_IF_FAIL_PRINTF4 (
1991 block->get_function () == on_false->get_function (),
1992 ctxt, loc,
1993 "\"on_false\" block is not in same function:"
1994 " source block %s is in function %s"
1995 " whereas target block %s is in function %s",
1996 block->get_debug_string (),
1997 block->get_function ()->get_debug_string (),
1998 on_false->get_debug_string (),
1999 on_false->get_function ()->get_debug_string ());
2001 gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
2003 /* "stmt" should be good enough to be usable in error-messages,
2004 but might still not be compilable; perform some more
2005 error-checking here. We do this here so that the error messages
2006 can contain a stringified version of "stmt", whilst appearing
2007 as close as possible to the point of failure. */
2008 boolval->verify_valid_within_stmt (__func__, stmt);
2011 /* Public entrypoint. See description in libgccjit.h.
2013 After error-checking, the real work is done by the
2014 gcc::jit::recording::block::add_comment method in
2015 jit-recording.c. */
2017 void
2018 gcc_jit_block_add_comment (gcc_jit_block *block,
2019 gcc_jit_location *loc,
2020 const char *text)
2022 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2023 gcc::jit::recording::context *ctxt = block->get_context ();
2024 JIT_LOG_FUNC (ctxt->get_logger ());
2025 /* LOC can be NULL. */
2026 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
2028 block->add_comment (loc, text);
2031 /* Public entrypoint. See description in libgccjit.h.
2033 After error-checking, the real work is done by the
2034 gcc::jit::recording::block::end_with_jump method in
2035 jit-recording.c. */
2037 void
2038 gcc_jit_block_end_with_jump (gcc_jit_block *block,
2039 gcc_jit_location *loc,
2040 gcc_jit_block *target)
2042 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2043 gcc::jit::recording::context *ctxt = block->get_context ();
2044 JIT_LOG_FUNC (ctxt->get_logger ());
2045 /* LOC can be NULL. */
2046 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
2047 RETURN_IF_FAIL_PRINTF4 (
2048 block->get_function () == target->get_function (),
2049 ctxt, loc,
2050 "target block is not in same function:"
2051 " source block %s is in function %s"
2052 " whereas target block %s is in function %s",
2053 block->get_debug_string (),
2054 block->get_function ()->get_debug_string (),
2055 target->get_debug_string (),
2056 target->get_function ()->get_debug_string ());
2058 block->end_with_jump (loc, target);
2061 /* Public entrypoint. See description in libgccjit.h.
2063 After error-checking, the real work is done by the
2064 gcc::jit::recording::block::end_with_return method in
2065 jit-recording.c. */
2067 void
2068 gcc_jit_block_end_with_return (gcc_jit_block *block,
2069 gcc_jit_location *loc,
2070 gcc_jit_rvalue *rvalue)
2072 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2073 gcc::jit::recording::context *ctxt = block->get_context ();
2074 JIT_LOG_FUNC (ctxt->get_logger ());
2075 /* LOC can be NULL. */
2076 gcc::jit::recording::function *func = block->get_function ();
2077 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2078 RETURN_IF_FAIL_PRINTF4 (
2079 compatible_types (
2080 func->get_return_type (),
2081 rvalue->get_type ()),
2082 ctxt, loc,
2083 "mismatching types:"
2084 " return of %s (type: %s) in function %s (return type: %s)",
2085 rvalue->get_debug_string (),
2086 rvalue->get_type ()->get_debug_string (),
2087 func->get_debug_string (),
2088 func->get_return_type ()->get_debug_string ());
2090 gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
2092 /* "stmt" should be good enough to be usable in error-messages,
2093 but might still not be compilable; perform some more
2094 error-checking here. We do this here so that the error messages
2095 can contain a stringified version of "stmt", whilst appearing
2096 as close as possible to the point of failure. */
2097 rvalue->verify_valid_within_stmt (__func__, stmt);
2100 /* Public entrypoint. See description in libgccjit.h.
2102 After error-checking, the real work is done by the
2103 gcc::jit::recording::block::end_with_return method in
2104 jit-recording.c. */
2106 void
2107 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
2108 gcc_jit_location *loc)
2110 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2111 gcc::jit::recording::context *ctxt = block->get_context ();
2112 JIT_LOG_FUNC (ctxt->get_logger ());
2113 /* LOC can be NULL. */
2114 gcc::jit::recording::function *func = block->get_function ();
2115 RETURN_IF_FAIL_PRINTF2 (
2116 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2117 ctxt, loc,
2118 "mismatching types:"
2119 " void return in function %s (return type: %s)",
2120 func->get_debug_string (),
2121 func->get_return_type ()->get_debug_string ());
2123 block->end_with_return (loc, NULL);
2126 /**********************************************************************
2127 Option-management
2128 **********************************************************************/
2130 /* Public entrypoint. See description in libgccjit.h.
2132 After error-checking, the real work is done by the
2133 gcc::jit::recording::context::set_str_option method in
2134 jit-recording.c. */
2136 void
2137 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2138 enum gcc_jit_str_option opt,
2139 const char *value)
2141 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2142 JIT_LOG_FUNC (ctxt->get_logger ());
2143 /* opt is checked by the inner function.
2144 value can be NULL. */
2146 ctxt->set_str_option (opt, value);
2149 /* Public entrypoint. See description in libgccjit.h.
2151 After error-checking, the real work is done by the
2152 gcc::jit::recording::context::set_int_option method in
2153 jit-recording.c. */
2155 void
2156 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2157 enum gcc_jit_int_option opt,
2158 int value)
2160 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2161 JIT_LOG_FUNC (ctxt->get_logger ());
2162 /* opt is checked by the inner function. */
2164 ctxt->set_int_option (opt, value);
2167 /* Public entrypoint. See description in libgccjit.h.
2169 After error-checking, the real work is done by the
2170 gcc::jit::recording::context::set_bool_option method in
2171 jit-recording.c. */
2173 void
2174 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2175 enum gcc_jit_bool_option opt,
2176 int value)
2178 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2179 JIT_LOG_FUNC (ctxt->get_logger ());
2180 /* opt is checked by the inner function. */
2182 ctxt->set_bool_option (opt, value);
2185 /* Public entrypoint. See description in libgccjit.h.
2187 After error-checking, the real work is done by the
2188 gcc::jit::recording::context::enable_dump method in
2189 jit-recording.c. */
2191 void
2192 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2193 const char *dumpname,
2194 char **out_ptr)
2196 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2197 JIT_LOG_FUNC (ctxt->get_logger ());
2198 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2199 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2201 ctxt->enable_dump (dumpname, out_ptr);
2204 /* Public entrypoint. See description in libgccjit.h.
2206 After error-checking, the real work is done by the
2207 gcc::jit::recording::context::compile method in
2208 jit-recording.c. */
2210 gcc_jit_result *
2211 gcc_jit_context_compile (gcc_jit_context *ctxt)
2213 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2215 JIT_LOG_FUNC (ctxt->get_logger ());
2217 ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
2219 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2221 ctxt->log ("%s: returning (gcc_jit_result *)%p",
2222 __func__, (void *)result);
2224 return result;
2227 /* Public entrypoint. See description in libgccjit.h.
2229 After error-checking, the real work is done by the
2230 gcc::jit::recording::context::compile_to_file method in
2231 jit-recording.c. */
2233 void
2234 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
2235 enum gcc_jit_output_kind output_kind,
2236 const char *output_path)
2238 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2239 JIT_LOG_FUNC (ctxt->get_logger ());
2240 RETURN_IF_FAIL_PRINTF1 (
2241 ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
2242 && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
2243 ctxt, NULL,
2244 "unrecognized output_kind: %i",
2245 output_kind);
2246 RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
2248 ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
2249 ctxt->log ("output_kind: %i", output_kind);
2250 ctxt->log ("output_path: %s", output_path);
2252 ctxt->compile_to_file (output_kind, output_path);
2256 /* Public entrypoint. See description in libgccjit.h.
2258 After error-checking, the real work is done by the
2259 gcc::jit::recording::context::dump_to_file method in
2260 jit-recording.c. */
2262 void
2263 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2264 const char *path,
2265 int update_locations)
2267 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2268 JIT_LOG_FUNC (ctxt->get_logger ());
2269 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2270 ctxt->dump_to_file (path, update_locations);
2273 /* Public entrypoint. See description in libgccjit.h. */
2275 void
2276 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2277 FILE *logfile,
2278 int flags,
2279 int verbosity)
2281 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2282 JIT_LOG_FUNC (ctxt->get_logger ());
2283 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2284 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2286 gcc::jit::logger *logger;
2287 if (logfile)
2288 logger = new gcc::jit::logger (logfile, flags, verbosity);
2289 else
2290 logger = NULL;
2291 ctxt->set_logger (logger);
2294 /* Public entrypoint. See description in libgccjit.h.
2296 After error-checking, the real work is done by the
2297 gcc::jit::recording::context::dump_reproducer_to_file method in
2298 jit-recording.c. */
2300 void
2301 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
2302 const char *path)
2304 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2305 JIT_LOG_FUNC (ctxt->get_logger ());
2306 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2307 ctxt->dump_reproducer_to_file (path);
2310 /* Public entrypoint. See description in libgccjit.h.
2312 After error-checking, the real work is done by the
2313 gcc::jit::recording::context::get_first_error method in
2314 jit-recording.c. */
2316 const char *
2317 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2319 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2320 JIT_LOG_FUNC (ctxt->get_logger ());
2322 return ctxt->get_first_error ();
2325 /* Public entrypoint. See description in libgccjit.h.
2327 After error-checking, the real work is done by the
2328 gcc::jit::recording::context::get_last_error method in
2329 jit-recording.c. */
2331 const char *
2332 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2334 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2336 return ctxt->get_last_error ();
2339 /* Public entrypoint. See description in libgccjit.h.
2341 After error-checking, the real work is done by the
2342 gcc::jit::result::get_code method in jit-result.c. */
2344 void *
2345 gcc_jit_result_get_code (gcc_jit_result *result,
2346 const char *fnname)
2348 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2349 JIT_LOG_FUNC (result->get_logger ());
2350 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2352 result->log ("locating fnname: %s", fnname);
2353 void *code = result->get_code (fnname);
2354 result->log ("%s: returning (void *)%p", __func__, code);
2356 return code;
2359 /* Public entrypoint. See description in libgccjit.h.
2361 After error-checking, the real work is done by the
2362 gcc::jit::result::get_global method in jit-result.c. */
2364 void *
2365 gcc_jit_result_get_global (gcc_jit_result *result,
2366 const char *name)
2368 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2369 JIT_LOG_FUNC (result->get_logger ());
2370 RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
2372 void *global = result->get_global (name);
2373 result->log ("%s: returning (void *)%p", __func__, global);
2375 return global;
2378 /* Public entrypoint. See description in libgccjit.h.
2380 After error-checking, this is essentially a wrapper around the
2381 destructor for gcc::jit::result in jit-result.c. */
2383 void
2384 gcc_jit_result_release (gcc_jit_result *result)
2386 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2387 JIT_LOG_FUNC (result->get_logger ());
2388 result->log ("deleting result: %p", (void *)result);
2389 delete result;