2015-03-02 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / jit / libgccjit.c
blob7eb66bdae506ddb4be89d16d3d9824f5929c5ef9
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 ());
1675 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1678 /* Public entrypoint. See description in libgccjit.h.
1680 After error-checking, the real work is done by the
1681 gcc::jit::recording::rvalue::access_field method in
1682 jit-recording.c. */
1684 gcc_jit_rvalue *
1685 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1686 gcc_jit_location *loc,
1687 gcc_jit_field *field)
1689 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1690 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1691 JIT_LOG_FUNC (ctxt->get_logger ());
1692 /* LOC can be NULL. */
1693 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1694 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1695 "field %s has not been placed in a struct",
1696 field->get_debug_string ());
1698 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1701 /* Public entrypoint. See description in libgccjit.h.
1703 After error-checking, the real work is done by the
1704 gcc::jit::recording::rvalue::deference_field method in
1705 jit-recording.c. */
1707 gcc_jit_lvalue *
1708 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1709 gcc_jit_location *loc,
1710 gcc_jit_field *field)
1712 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1713 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1714 /* LOC can be NULL. */
1715 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1716 gcc::jit::recording::type *underlying_type =
1717 ptr->get_type ()->is_pointer ();
1718 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1719 "field %s has not been placed in a struct",
1720 field->get_debug_string ());
1721 RETURN_NULL_IF_FAIL_PRINTF3 (
1722 underlying_type,
1723 ptr->m_ctxt, loc,
1724 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1725 ptr->get_debug_string (),
1726 ptr->get_type ()->get_debug_string (),
1727 field->get_debug_string ());
1728 RETURN_NULL_IF_FAIL_PRINTF2 (
1729 (field->get_container ()->unqualified ()
1730 == underlying_type->unqualified ()),
1731 ptr->m_ctxt, loc,
1732 "%s is not a field of %s",
1733 field->get_debug_string (),
1734 underlying_type->get_debug_string ());
1736 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1739 /* Public entrypoint. See description in libgccjit.h.
1741 After error-checking, the real work is done by the
1742 gcc::jit::recording::rvalue::deference method in
1743 jit-recording.c. */
1745 gcc_jit_lvalue *
1746 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1747 gcc_jit_location *loc)
1749 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1750 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1751 /* LOC can be NULL. */
1753 gcc::jit::recording::type *underlying_type =
1754 rvalue->get_type ()->is_pointer ();
1756 RETURN_NULL_IF_FAIL_PRINTF2 (
1757 underlying_type,
1758 rvalue->m_ctxt, loc,
1759 "dereference of non-pointer %s (type: %s)",
1760 rvalue->get_debug_string (),
1761 rvalue->get_type ()->get_debug_string ());
1763 RETURN_NULL_IF_FAIL_PRINTF2 (
1764 !underlying_type->is_void (),
1765 rvalue->m_ctxt, loc,
1766 "dereference of void pointer %s (type: %s)",
1767 rvalue->get_debug_string (),
1768 rvalue->get_type ()->get_debug_string ());
1770 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1773 /* Public entrypoint. See description in libgccjit.h.
1775 After error-checking, the real work is done by the
1776 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1778 gcc_jit_rvalue *
1779 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1780 gcc_jit_location *loc)
1782 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1783 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1784 /* LOC can be NULL. */
1786 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1789 /* Public entrypoint. See description in libgccjit.h.
1791 After error-checking, the real work is done by the
1792 gcc::jit::recording::function::new_local method in jit-recording.c. */
1794 gcc_jit_lvalue *
1795 gcc_jit_function_new_local (gcc_jit_function *func,
1796 gcc_jit_location *loc,
1797 gcc_jit_type *type,
1798 const char *name)
1800 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1801 gcc::jit::recording::context *ctxt = func->m_ctxt;
1802 JIT_LOG_FUNC (ctxt->get_logger ());
1803 /* LOC can be NULL. */
1804 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1805 ctxt, loc,
1806 "Cannot add locals to an imported function");
1807 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1808 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1810 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
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_eval method in jit-recording.c. */
1818 void
1819 gcc_jit_block_add_eval (gcc_jit_block *block,
1820 gcc_jit_location *loc,
1821 gcc_jit_rvalue *rvalue)
1823 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1824 gcc::jit::recording::context *ctxt = block->get_context ();
1825 JIT_LOG_FUNC (ctxt->get_logger ());
1826 /* LOC can be NULL. */
1827 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1829 gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
1831 /* "stmt" should be good enough to be usable in error-messages,
1832 but might still not be compilable; perform some more
1833 error-checking here. We do this here so that the error messages
1834 can contain a stringified version of "stmt", whilst appearing
1835 as close as possible to the point of failure. */
1836 rvalue->verify_valid_within_stmt (__func__, stmt);
1839 /* Public entrypoint. See description in libgccjit.h.
1841 After error-checking, the real work is done by the
1842 gcc::jit::recording::block::add_assignment method in
1843 jit-recording.c. */
1845 void
1846 gcc_jit_block_add_assignment (gcc_jit_block *block,
1847 gcc_jit_location *loc,
1848 gcc_jit_lvalue *lvalue,
1849 gcc_jit_rvalue *rvalue)
1851 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1852 gcc::jit::recording::context *ctxt = block->get_context ();
1853 JIT_LOG_FUNC (ctxt->get_logger ());
1854 /* LOC can be NULL. */
1855 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1856 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1857 RETURN_IF_FAIL_PRINTF4 (
1858 compatible_types (lvalue->get_type (),
1859 rvalue->get_type ()),
1860 ctxt, loc,
1861 "mismatching types:"
1862 " assignment to %s (type: %s) from %s (type: %s)",
1863 lvalue->get_debug_string (),
1864 lvalue->get_type ()->get_debug_string (),
1865 rvalue->get_debug_string (),
1866 rvalue->get_type ()->get_debug_string ());
1868 gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
1870 /* "stmt" should be good enough to be usable in error-messages,
1871 but might still not be compilable; perform some more
1872 error-checking here. We do this here so that the error messages
1873 can contain a stringified version of "stmt", whilst appearing
1874 as close as possible to the point of failure. */
1875 lvalue->verify_valid_within_stmt (__func__, stmt);
1876 rvalue->verify_valid_within_stmt (__func__, stmt);
1879 /* Public entrypoint. See description in libgccjit.h.
1881 After error-checking, the real work is done by the
1882 gcc::jit::recording::block::add_assignment_op method in
1883 jit-recording.c. */
1885 void
1886 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1887 gcc_jit_location *loc,
1888 gcc_jit_lvalue *lvalue,
1889 enum gcc_jit_binary_op op,
1890 gcc_jit_rvalue *rvalue)
1892 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1893 gcc::jit::recording::context *ctxt = block->get_context ();
1894 JIT_LOG_FUNC (ctxt->get_logger ());
1895 /* LOC can be NULL. */
1896 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1897 RETURN_IF_FAIL_PRINTF1 (
1898 valid_binary_op_p (op),
1899 ctxt, loc,
1900 "unrecognized value for enum gcc_jit_binary_op: %i",
1901 op);
1902 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1903 RETURN_IF_FAIL_PRINTF4 (
1904 compatible_types (lvalue->get_type (),
1905 rvalue->get_type ()),
1906 ctxt, loc,
1907 "mismatching types:"
1908 " assignment to %s (type: %s) involving %s (type: %s)",
1909 lvalue->get_debug_string (),
1910 lvalue->get_type ()->get_debug_string (),
1911 rvalue->get_debug_string (),
1912 rvalue->get_type ()->get_debug_string ());
1914 gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
1916 /* "stmt" should be good enough to be usable in error-messages,
1917 but might still not be compilable; perform some more
1918 error-checking here. We do this here so that the error messages
1919 can contain a stringified version of "stmt", whilst appearing
1920 as close as possible to the point of failure. */
1921 lvalue->verify_valid_within_stmt (__func__, stmt);
1922 rvalue->verify_valid_within_stmt (__func__, stmt);
1925 /* Internal helper function for determining if rvalue BOOLVAL is of
1926 boolean type. For use by gcc_jit_block_end_with_conditional. */
1928 static bool
1929 is_bool (gcc_jit_rvalue *boolval)
1931 gcc::jit::recording::type *actual_type = boolval->get_type ();
1932 gcc::jit::recording::type *bool_type =
1933 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1934 return actual_type == bool_type;
1937 /* Public entrypoint. See description in libgccjit.h.
1939 After error-checking, the real work is done by the
1940 gcc::jit::recording::block::end_with_conditional method in
1941 jit-recording.c. */
1943 void
1944 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1945 gcc_jit_location *loc,
1946 gcc_jit_rvalue *boolval,
1947 gcc_jit_block *on_true,
1948 gcc_jit_block *on_false)
1950 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1951 gcc::jit::recording::context *ctxt = block->get_context ();
1952 JIT_LOG_FUNC (ctxt->get_logger ());
1953 /* LOC can be NULL. */
1954 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
1955 RETURN_IF_FAIL_PRINTF2 (
1956 is_bool (boolval), ctxt, loc,
1957 "%s (type: %s) is not of boolean type ",
1958 boolval->get_debug_string (),
1959 boolval->get_type ()->get_debug_string ());
1960 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
1961 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
1962 RETURN_IF_FAIL_PRINTF4 (
1963 block->get_function () == on_true->get_function (),
1964 ctxt, loc,
1965 "\"on_true\" block is not in same function:"
1966 " source block %s is in function %s"
1967 " whereas target block %s is in function %s",
1968 block->get_debug_string (),
1969 block->get_function ()->get_debug_string (),
1970 on_true->get_debug_string (),
1971 on_true->get_function ()->get_debug_string ());
1972 RETURN_IF_FAIL_PRINTF4 (
1973 block->get_function () == on_false->get_function (),
1974 ctxt, loc,
1975 "\"on_false\" block is not in same function:"
1976 " source block %s is in function %s"
1977 " whereas target block %s is in function %s",
1978 block->get_debug_string (),
1979 block->get_function ()->get_debug_string (),
1980 on_false->get_debug_string (),
1981 on_false->get_function ()->get_debug_string ());
1983 gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
1985 /* "stmt" should be good enough to be usable in error-messages,
1986 but might still not be compilable; perform some more
1987 error-checking here. We do this here so that the error messages
1988 can contain a stringified version of "stmt", whilst appearing
1989 as close as possible to the point of failure. */
1990 boolval->verify_valid_within_stmt (__func__, stmt);
1993 /* Public entrypoint. See description in libgccjit.h.
1995 After error-checking, the real work is done by the
1996 gcc::jit::recording::block::add_comment method in
1997 jit-recording.c. */
1999 void
2000 gcc_jit_block_add_comment (gcc_jit_block *block,
2001 gcc_jit_location *loc,
2002 const char *text)
2004 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2005 gcc::jit::recording::context *ctxt = block->get_context ();
2006 JIT_LOG_FUNC (ctxt->get_logger ());
2007 /* LOC can be NULL. */
2008 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
2010 block->add_comment (loc, text);
2013 /* Public entrypoint. See description in libgccjit.h.
2015 After error-checking, the real work is done by the
2016 gcc::jit::recording::block::end_with_jump method in
2017 jit-recording.c. */
2019 void
2020 gcc_jit_block_end_with_jump (gcc_jit_block *block,
2021 gcc_jit_location *loc,
2022 gcc_jit_block *target)
2024 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2025 gcc::jit::recording::context *ctxt = block->get_context ();
2026 JIT_LOG_FUNC (ctxt->get_logger ());
2027 /* LOC can be NULL. */
2028 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
2029 RETURN_IF_FAIL_PRINTF4 (
2030 block->get_function () == target->get_function (),
2031 ctxt, loc,
2032 "target block is not in same function:"
2033 " source block %s is in function %s"
2034 " whereas target block %s is in function %s",
2035 block->get_debug_string (),
2036 block->get_function ()->get_debug_string (),
2037 target->get_debug_string (),
2038 target->get_function ()->get_debug_string ());
2040 block->end_with_jump (loc, target);
2043 /* Public entrypoint. See description in libgccjit.h.
2045 After error-checking, the real work is done by the
2046 gcc::jit::recording::block::end_with_return method in
2047 jit-recording.c. */
2049 void
2050 gcc_jit_block_end_with_return (gcc_jit_block *block,
2051 gcc_jit_location *loc,
2052 gcc_jit_rvalue *rvalue)
2054 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2055 gcc::jit::recording::context *ctxt = block->get_context ();
2056 JIT_LOG_FUNC (ctxt->get_logger ());
2057 /* LOC can be NULL. */
2058 gcc::jit::recording::function *func = block->get_function ();
2059 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2060 RETURN_IF_FAIL_PRINTF4 (
2061 compatible_types (
2062 func->get_return_type (),
2063 rvalue->get_type ()),
2064 ctxt, loc,
2065 "mismatching types:"
2066 " return of %s (type: %s) in function %s (return type: %s)",
2067 rvalue->get_debug_string (),
2068 rvalue->get_type ()->get_debug_string (),
2069 func->get_debug_string (),
2070 func->get_return_type ()->get_debug_string ());
2072 gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
2074 /* "stmt" should be good enough to be usable in error-messages,
2075 but might still not be compilable; perform some more
2076 error-checking here. We do this here so that the error messages
2077 can contain a stringified version of "stmt", whilst appearing
2078 as close as possible to the point of failure. */
2079 rvalue->verify_valid_within_stmt (__func__, stmt);
2082 /* Public entrypoint. See description in libgccjit.h.
2084 After error-checking, the real work is done by the
2085 gcc::jit::recording::block::end_with_return method in
2086 jit-recording.c. */
2088 void
2089 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
2090 gcc_jit_location *loc)
2092 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2093 gcc::jit::recording::context *ctxt = block->get_context ();
2094 JIT_LOG_FUNC (ctxt->get_logger ());
2095 /* LOC can be NULL. */
2096 gcc::jit::recording::function *func = block->get_function ();
2097 RETURN_IF_FAIL_PRINTF2 (
2098 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2099 ctxt, loc,
2100 "mismatching types:"
2101 " void return in function %s (return type: %s)",
2102 func->get_debug_string (),
2103 func->get_return_type ()->get_debug_string ());
2105 block->end_with_return (loc, NULL);
2108 /**********************************************************************
2109 Option-management
2110 **********************************************************************/
2112 /* Public entrypoint. See description in libgccjit.h.
2114 After error-checking, the real work is done by the
2115 gcc::jit::recording::context::set_str_option method in
2116 jit-recording.c. */
2118 void
2119 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2120 enum gcc_jit_str_option opt,
2121 const char *value)
2123 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2124 JIT_LOG_FUNC (ctxt->get_logger ());
2125 /* opt is checked by the inner function.
2126 value can be NULL. */
2128 ctxt->set_str_option (opt, value);
2131 /* Public entrypoint. See description in libgccjit.h.
2133 After error-checking, the real work is done by the
2134 gcc::jit::recording::context::set_int_option method in
2135 jit-recording.c. */
2137 void
2138 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2139 enum gcc_jit_int_option opt,
2140 int value)
2142 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2143 JIT_LOG_FUNC (ctxt->get_logger ());
2144 /* opt is checked by the inner function. */
2146 ctxt->set_int_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_bool_option method in
2153 jit-recording.c. */
2155 void
2156 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2157 enum gcc_jit_bool_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_bool_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::enable_dump method in
2171 jit-recording.c. */
2173 void
2174 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2175 const char *dumpname,
2176 char **out_ptr)
2178 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2179 JIT_LOG_FUNC (ctxt->get_logger ());
2180 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2181 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2183 ctxt->enable_dump (dumpname, out_ptr);
2186 /* Public entrypoint. See description in libgccjit.h.
2188 After error-checking, the real work is done by the
2189 gcc::jit::recording::context::compile method in
2190 jit-recording.c. */
2192 gcc_jit_result *
2193 gcc_jit_context_compile (gcc_jit_context *ctxt)
2195 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2197 JIT_LOG_FUNC (ctxt->get_logger ());
2199 ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
2201 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2203 ctxt->log ("%s: returning (gcc_jit_result *)%p",
2204 __func__, (void *)result);
2206 return result;
2209 /* Public entrypoint. See description in libgccjit.h.
2211 After error-checking, the real work is done by the
2212 gcc::jit::recording::context::compile_to_file method in
2213 jit-recording.c. */
2215 void
2216 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
2217 enum gcc_jit_output_kind output_kind,
2218 const char *output_path)
2220 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2221 JIT_LOG_FUNC (ctxt->get_logger ());
2222 RETURN_IF_FAIL_PRINTF1 (
2223 ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
2224 && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
2225 ctxt, NULL,
2226 "unrecognized output_kind: %i",
2227 output_kind);
2228 RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
2230 ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
2231 ctxt->log ("output_kind: %i", output_kind);
2232 ctxt->log ("output_path: %s", output_path);
2234 ctxt->compile_to_file (output_kind, output_path);
2238 /* Public entrypoint. See description in libgccjit.h.
2240 After error-checking, the real work is done by the
2241 gcc::jit::recording::context::dump_to_file method in
2242 jit-recording.c. */
2244 void
2245 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2246 const char *path,
2247 int update_locations)
2249 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2250 JIT_LOG_FUNC (ctxt->get_logger ());
2251 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2252 ctxt->dump_to_file (path, update_locations);
2255 /* Public entrypoint. See description in libgccjit.h. */
2257 void
2258 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2259 FILE *logfile,
2260 int flags,
2261 int verbosity)
2263 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2264 JIT_LOG_FUNC (ctxt->get_logger ());
2265 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2266 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2268 gcc::jit::logger *logger;
2269 if (logfile)
2270 logger = new gcc::jit::logger (logfile, flags, verbosity);
2271 else
2272 logger = NULL;
2273 ctxt->set_logger (logger);
2276 /* Public entrypoint. See description in libgccjit.h.
2278 After error-checking, the real work is done by the
2279 gcc::jit::recording::context::dump_reproducer_to_file method in
2280 jit-recording.c. */
2282 void
2283 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
2284 const char *path)
2286 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2287 JIT_LOG_FUNC (ctxt->get_logger ());
2288 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2289 ctxt->dump_reproducer_to_file (path);
2292 /* Public entrypoint. See description in libgccjit.h.
2294 After error-checking, the real work is done by the
2295 gcc::jit::recording::context::get_first_error method in
2296 jit-recording.c. */
2298 const char *
2299 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2301 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2302 JIT_LOG_FUNC (ctxt->get_logger ());
2304 return ctxt->get_first_error ();
2307 /* Public entrypoint. See description in libgccjit.h.
2309 After error-checking, the real work is done by the
2310 gcc::jit::recording::context::get_last_error method in
2311 jit-recording.c. */
2313 const char *
2314 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2316 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2318 return ctxt->get_last_error ();
2321 /* Public entrypoint. See description in libgccjit.h.
2323 After error-checking, the real work is done by the
2324 gcc::jit::result::get_code method in jit-result.c. */
2326 void *
2327 gcc_jit_result_get_code (gcc_jit_result *result,
2328 const char *fnname)
2330 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2331 JIT_LOG_FUNC (result->get_logger ());
2332 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2334 result->log ("locating fnname: %s", fnname);
2335 void *code = result->get_code (fnname);
2336 result->log ("%s: returning (void *)%p", __func__, code);
2338 return code;
2341 /* Public entrypoint. See description in libgccjit.h.
2343 After error-checking, the real work is done by the
2344 gcc::jit::result::get_global method in jit-result.c. */
2346 void *
2347 gcc_jit_result_get_global (gcc_jit_result *result,
2348 const char *name)
2350 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2351 JIT_LOG_FUNC (result->get_logger ());
2352 RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
2354 void *global = result->get_global (name);
2355 result->log ("%s: returning (void *)%p", __func__, global);
2357 return global;
2360 /* Public entrypoint. See description in libgccjit.h.
2362 After error-checking, this is essentially a wrapper around the
2363 destructor for gcc::jit::result in jit-result.c. */
2365 void
2366 gcc_jit_result_release (gcc_jit_result *result)
2368 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2369 JIT_LOG_FUNC (result->get_logger ());
2370 result->log ("deleting result: %p", (void *)result);
2371 delete result;