New jit API entrypoint: gcc_jit_context_new_rvalue_from_long
[official-gcc.git] / gcc / jit / libgccjit.c
blob62d3edf9e1c5e36d863e97b1ed47e4fa4dac9f46
1 /* Implementation of the C API; all wrappers into the internal C++ API
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "safe-ctype.h"
27 #include "libgccjit.h"
28 #include "jit-common.h"
29 #include "jit-logging.h"
30 #include "jit-recording.h"
31 #include "jit-result.h"
33 /* The opaque types used by the public API are actually subclasses
34 of the gcc::jit::recording classes. */
36 struct gcc_jit_context : public gcc::jit::recording::context
38 gcc_jit_context (gcc_jit_context *parent_ctxt) :
39 context (parent_ctxt)
43 struct gcc_jit_result : public gcc::jit::result
47 struct gcc_jit_object : public gcc::jit::recording::memento
51 struct gcc_jit_location : public gcc::jit::recording::location
55 struct gcc_jit_type : public gcc::jit::recording::type
59 struct gcc_jit_struct : public gcc::jit::recording::struct_
63 struct gcc_jit_field : public gcc::jit::recording::field
67 struct gcc_jit_function : public gcc::jit::recording::function
71 struct gcc_jit_block : public gcc::jit::recording::block
75 struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
79 struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
83 struct gcc_jit_param : public gcc::jit::recording::param
87 /**********************************************************************
88 Error-handling.
90 We try to gracefully handle API usage errors by being defensive
91 at the API boundary.
92 **********************************************************************/
94 #define JIT_BEGIN_STMT do {
95 #define JIT_END_STMT } while(0)
97 /* Each of these error-handling macros determines if TEST_EXPR holds.
99 If TEXT_EXPR fails to hold we return from the enclosing function and
100 print an error, either via adding an error on the given context CTXT
101 if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
102 is NULL.
104 They have to be macros since they inject their "return" into the
105 function they are placed in.
107 The variant macros express:
109 (A) whether or not we need to return a value:
110 RETURN_VAL_IF_FAIL* vs
111 RETURN_IF_FAIL*,
112 with the former returning RETURN_EXPR, and
113 RETURN_NULL_IF_FAIL*
114 for the common case where a NULL value is to be returned on
115 error, and
117 (B) whether the error message is to be directly printed:
118 RETURN_*IF_FAIL
119 or is a format string with some number of arguments:
120 RETURN_*IF_FAIL_PRINTF*
122 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
123 trailing semicolons.
126 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
127 JIT_BEGIN_STMT \
128 if (!(TEST_EXPR)) \
130 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
131 return (RETURN_EXPR); \
133 JIT_END_STMT
135 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
136 JIT_BEGIN_STMT \
137 if (!(TEST_EXPR)) \
139 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
140 __func__, (A0)); \
141 return (RETURN_EXPR); \
143 JIT_END_STMT
145 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
146 JIT_BEGIN_STMT \
147 if (!(TEST_EXPR)) \
149 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
150 __func__, (A0), (A1)); \
151 return (RETURN_EXPR); \
153 JIT_END_STMT
155 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
156 JIT_BEGIN_STMT \
157 if (!(TEST_EXPR)) \
159 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
160 __func__, (A0), (A1), (A2)); \
161 return (RETURN_EXPR); \
163 JIT_END_STMT
165 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
166 JIT_BEGIN_STMT \
167 if (!(TEST_EXPR)) \
169 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
170 __func__, (A0), (A1), (A2), (A3)); \
171 return (RETURN_EXPR); \
173 JIT_END_STMT
175 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
176 JIT_BEGIN_STMT \
177 if (!(TEST_EXPR)) \
179 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
180 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
181 return (RETURN_EXPR); \
183 JIT_END_STMT
185 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
186 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
188 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
189 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
191 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
192 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
194 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
195 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
197 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
198 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
200 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
201 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
203 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
204 JIT_BEGIN_STMT \
205 if (!(TEST_EXPR)) \
207 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
208 return; \
210 JIT_END_STMT
212 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
213 JIT_BEGIN_STMT \
214 if (!(TEST_EXPR)) \
216 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
217 __func__, (A0)); \
218 return; \
220 JIT_END_STMT
222 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
223 JIT_BEGIN_STMT \
224 if (!(TEST_EXPR)) \
226 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
227 __func__, (A0), (A1)); \
228 return; \
230 JIT_END_STMT
232 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
233 JIT_BEGIN_STMT \
234 if (!(TEST_EXPR)) \
236 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
237 __func__, (A0), (A1), (A2), (A3)); \
238 return; \
240 JIT_END_STMT
242 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
243 it. This will fail if BLOCK has already been terminated by some
244 kind of jump or a return. */
245 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
246 JIT_BEGIN_STMT \
247 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
248 RETURN_IF_FAIL_PRINTF2 ( \
249 !(BLOCK)->has_been_terminated (), \
250 (BLOCK)->get_context (), \
251 (LOC), \
252 "adding to terminated block: %s (already terminated by: %s)", \
253 (BLOCK)->get_debug_string (), \
254 (BLOCK)->get_last_statement ()->get_debug_string ()); \
255 JIT_END_STMT
257 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
258 fails. */
259 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
260 JIT_BEGIN_STMT \
261 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
262 RETURN_NULL_IF_FAIL_PRINTF2 ( \
263 !(BLOCK)->has_been_terminated (), \
264 (BLOCK)->get_context (), \
265 (LOC), \
266 "adding to terminated block: %s (already terminated by: %s)", \
267 (BLOCK)->get_debug_string (), \
268 (BLOCK)->get_last_statement ()->get_debug_string ()); \
269 JIT_END_STMT
271 /* Format the given string, and report it as an error, either on CTXT
272 if non-NULL, or by printing to stderr if we have a NULL context.
273 LOC gives the source location where the error occcurred, and can be
274 NULL. */
276 static void
277 jit_error (gcc::jit::recording::context *ctxt,
278 gcc_jit_location *loc,
279 const char *fmt, ...)
280 GNU_PRINTF(3, 4);
282 static void
283 jit_error (gcc::jit::recording::context *ctxt,
284 gcc_jit_location *loc,
285 const char *fmt, ...)
287 va_list ap;
288 va_start (ap, fmt);
290 if (ctxt)
291 ctxt->add_error_va (loc, fmt, ap);
292 else
294 /* No context? Send to stderr. */
295 vfprintf (stderr, fmt, ap);
296 fprintf (stderr, "\n");
299 va_end (ap);
302 /* Determine whether or not we can write to lvalues of type LTYPE from
303 rvalues of type RTYPE, detecting type errors such as attempting to
304 write to an int with a string literal (without an explicit cast).
306 This is implemented by calling the
307 gcc::jit::recording::type::accepts_writes_from virtual function on
308 LTYPE. */
310 static bool
311 compatible_types (gcc::jit::recording::type *ltype,
312 gcc::jit::recording::type *rtype)
314 return ltype->accepts_writes_from (rtype);
317 /* Public entrypoint for acquiring a gcc_jit_context.
318 Note that this creates a new top-level context; contrast with
319 gcc_jit_context_new_child_context below.
321 The real work is done in the constructor for
322 gcc::jit::recording::context in jit-recording.c. */
324 gcc_jit_context *
325 gcc_jit_context_acquire (void)
327 gcc_jit_context *ctxt = new gcc_jit_context (NULL);
328 ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
329 return ctxt;
332 /* Public entrypoint for releasing a gcc_jit_context.
333 The real work is done in the destructor for
334 gcc::jit::recording::context in jit-recording.c. */
336 void
337 gcc_jit_context_release (gcc_jit_context *ctxt)
339 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
340 JIT_LOG_FUNC (ctxt->get_logger ());
341 ctxt->log ("deleting ctxt: %p", (void *)ctxt);
342 delete ctxt;
345 /* Public entrypoint for creating a child context within
346 PARENT_CTXT. See description in libgccjit.h.
348 The real work is done in the constructor for
349 gcc::jit::recording::context in jit-recording.c. */
351 gcc_jit_context *
352 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
354 RETURN_NULL_IF_FAIL (parent_ctxt, NULL, NULL, "NULL parent ctxt");
355 JIT_LOG_FUNC (parent_ctxt->get_logger ());
356 parent_ctxt->log ("parent_ctxt: %p", (void *)parent_ctxt);
357 gcc_jit_context *child_ctxt = new gcc_jit_context (parent_ctxt);
358 child_ctxt->log ("new child_ctxt: %p", (void *)child_ctxt);
359 return child_ctxt;
362 /* Public entrypoint. See description in libgccjit.h.
364 After error-checking, the real work is done by the
365 gcc::jit::recording::context::new_location
366 method in jit-recording.c. */
368 gcc_jit_location *
369 gcc_jit_context_new_location (gcc_jit_context *ctxt,
370 const char *filename,
371 int line,
372 int column)
374 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
375 JIT_LOG_FUNC (ctxt->get_logger ());
376 return (gcc_jit_location *)ctxt->new_location (filename, line, column);
379 /* Public entrypoint. See description in libgccjit.h.
381 After error-checking, this calls the trivial
382 gcc::jit::recording::memento::as_object method (a location is a
383 memento), in jit-recording.h. */
385 gcc_jit_object *
386 gcc_jit_location_as_object (gcc_jit_location *loc)
388 RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
390 return static_cast <gcc_jit_object *> (loc->as_object ());
393 /* Public entrypoint. See description in libgccjit.h.
395 After error-checking, this calls the trivial
396 gcc::jit::recording::memento::as_object method (a type is a
397 memento), in jit-recording.h. */
399 gcc_jit_object *
400 gcc_jit_type_as_object (gcc_jit_type *type)
402 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
404 return static_cast <gcc_jit_object *> (type->as_object ());
407 /* Public entrypoint for getting a specific type from a context.
409 After error-checking, the real work is done by the
410 gcc::jit::recording::context::get_type method, in
411 jit-recording.c */
413 gcc_jit_type *
414 gcc_jit_context_get_type (gcc_jit_context *ctxt,
415 enum gcc_jit_types type)
417 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
418 JIT_LOG_FUNC (ctxt->get_logger ());
419 RETURN_NULL_IF_FAIL_PRINTF1 (
420 (type >= GCC_JIT_TYPE_VOID
421 && type <= GCC_JIT_TYPE_FILE_PTR),
422 ctxt, NULL,
423 "unrecognized value for enum gcc_jit_types: %i", type);
425 return (gcc_jit_type *)ctxt->get_type (type);
428 /* Public entrypoint for getting the integer type of the given size and
429 signedness.
431 After error-checking, the real work is done by the
432 gcc::jit::recording::context::get_int_type method,
433 in jit-recording.c. */
435 gcc_jit_type *
436 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
437 int num_bytes, int is_signed)
439 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
440 JIT_LOG_FUNC (ctxt->get_logger ());
441 RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
443 return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
446 /* Public entrypoint. See description in libgccjit.h.
448 After error-checking, the real work is done by the
449 gcc::jit::recording::type::get_pointer method, in
450 jit-recording.c */
452 gcc_jit_type *
453 gcc_jit_type_get_pointer (gcc_jit_type *type)
455 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
457 return (gcc_jit_type *)type->get_pointer ();
460 /* Public entrypoint. See description in libgccjit.h.
462 After error-checking, the real work is done by the
463 gcc::jit::recording::type::get_const method, in
464 jit-recording.c. */
466 gcc_jit_type *
467 gcc_jit_type_get_const (gcc_jit_type *type)
469 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
471 return (gcc_jit_type *)type->get_const ();
474 /* Public entrypoint. See description in libgccjit.h.
476 After error-checking, the real work is done by the
477 gcc::jit::recording::type::get_volatile method, in
478 jit-recording.c. */
480 gcc_jit_type *
481 gcc_jit_type_get_volatile (gcc_jit_type *type)
483 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
485 return (gcc_jit_type *)type->get_volatile ();
488 /* Public entrypoint. See description in libgccjit.h.
490 After error-checking, the real work is done by the
491 gcc::jit::recording::context::new_array_type method, in
492 jit-recording.c. */
494 gcc_jit_type *
495 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
496 gcc_jit_location *loc,
497 gcc_jit_type *element_type,
498 int num_elements)
500 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
501 JIT_LOG_FUNC (ctxt->get_logger ());
502 /* LOC can be NULL. */
503 RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
504 RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
506 return (gcc_jit_type *)ctxt->new_array_type (loc,
507 element_type,
508 num_elements);
511 /* Public entrypoint. See description in libgccjit.h.
513 After error-checking, the real work is done by the
514 gcc::jit::recording::context::new_field method, in
515 jit-recording.c. */
517 gcc_jit_field *
518 gcc_jit_context_new_field (gcc_jit_context *ctxt,
519 gcc_jit_location *loc,
520 gcc_jit_type *type,
521 const char *name)
523 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
524 JIT_LOG_FUNC (ctxt->get_logger ());
525 /* LOC can be NULL. */
526 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
527 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
529 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
532 /* Public entrypoint. See description in libgccjit.h.
534 After error-checking, this calls the trivial
535 gcc::jit::recording::memento::as_object method (a field is a
536 memento), in jit-recording.h. */
538 gcc_jit_object *
539 gcc_jit_field_as_object (gcc_jit_field *field)
541 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
543 return static_cast <gcc_jit_object *> (field->as_object ());
546 /* Public entrypoint. See description in libgccjit.h.
548 After error-checking, the real work is done by the
549 gcc::jit::recording::context::new_struct_type method,
550 immediately followed by a "set_fields" call on the resulting
551 gcc::jit::recording::compound_type *, both in jit-recording.c */
553 gcc_jit_struct *
554 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
555 gcc_jit_location *loc,
556 const char *name,
557 int num_fields,
558 gcc_jit_field **fields)
560 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
561 JIT_LOG_FUNC (ctxt->get_logger ());
562 /* LOC can be NULL. */
563 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
564 if (num_fields)
565 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
566 for (int i = 0; i < num_fields; i++)
568 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
569 RETURN_NULL_IF_FAIL_PRINTF2 (
570 NULL == fields[i]->get_container (),
571 ctxt, loc,
572 "%s is already a field of %s",
573 fields[i]->get_debug_string (),
574 fields[i]->get_container ()->get_debug_string ());
577 gcc::jit::recording::struct_ *result =
578 ctxt->new_struct_type (loc, name);
579 result->set_fields (loc,
580 num_fields,
581 (gcc::jit::recording::field **)fields);
582 return static_cast<gcc_jit_struct *> (result);
585 /* Public entrypoint. See description in libgccjit.h.
587 After error-checking, the real work is done by the
588 gcc::jit::recording::context::new_struct_type method in
589 jit-recording.c. */
591 gcc_jit_struct *
592 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
593 gcc_jit_location *loc,
594 const char *name)
596 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
597 JIT_LOG_FUNC (ctxt->get_logger ());
598 /* LOC can be NULL. */
599 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
601 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
604 /* Public entrypoint. See description in libgccjit.h.
606 After error-checking, this calls the trivial
607 gcc::jit::recording::struct_::as_object method in
608 jit-recording.h. */
610 gcc_jit_type *
611 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
613 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
615 return static_cast <gcc_jit_type *> (struct_type->as_type ());
618 /* Public entrypoint. See description in libgccjit.h.
620 After error-checking, the real work is done by the
621 gcc::jit::recording::compound_type::set_fields method in
622 jit-recording.c. */
624 void
625 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
626 gcc_jit_location *loc,
627 int num_fields,
628 gcc_jit_field **fields)
630 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
631 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
632 JIT_LOG_FUNC (ctxt->get_logger ());
633 /* LOC can be NULL. */
634 RETURN_IF_FAIL_PRINTF1 (
635 NULL == struct_type->get_fields (), ctxt, loc,
636 "%s already has had fields set",
637 struct_type->get_debug_string ());
638 if (num_fields)
639 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
640 for (int i = 0; i < num_fields; i++)
642 RETURN_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
643 RETURN_IF_FAIL_PRINTF2 (
644 NULL == fields[i]->get_container (),
645 ctxt, loc,
646 "%s is already a field of %s",
647 fields[i]->get_debug_string (),
648 fields[i]->get_container ()->get_debug_string ());
651 struct_type->set_fields (loc, num_fields,
652 (gcc::jit::recording::field **)fields);
655 /* Public entrypoint. See description in libgccjit.h.
657 After error-checking, the real work is done by the
658 gcc::jit::recording::context::new_union_type method,
659 immediately followed by a "set_fields" call on the resulting
660 gcc::jit::recording::compound_type *, both in jit-recording.c */
662 gcc_jit_type *
663 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
664 gcc_jit_location *loc,
665 const char *name,
666 int num_fields,
667 gcc_jit_field **fields)
669 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
670 JIT_LOG_FUNC (ctxt->get_logger ());
671 /* LOC can be NULL. */
672 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
673 if (num_fields)
674 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
675 for (int i = 0; i < num_fields; i++)
677 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
678 RETURN_NULL_IF_FAIL_PRINTF2 (
679 NULL == fields[i]->get_container (),
680 ctxt, loc,
681 "%s is already a field of %s",
682 fields[i]->get_debug_string (),
683 fields[i]->get_container ()->get_debug_string ());
686 gcc::jit::recording::union_ *result =
687 ctxt->new_union_type (loc, name);
688 result->set_fields (loc,
689 num_fields,
690 (gcc::jit::recording::field **)fields);
691 return (gcc_jit_type *) (result);
694 /* Public entrypoint. See description in libgccjit.h.
696 After error-checking, the real work is done by the
697 gcc::jit::recording::context::new_function_ptr_type method,
698 in jit-recording.c */
700 gcc_jit_type *
701 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
702 gcc_jit_location *loc,
703 gcc_jit_type *return_type,
704 int num_params,
705 gcc_jit_type **param_types,
706 int is_variadic)
708 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
709 JIT_LOG_FUNC (ctxt->get_logger ());
710 /* LOC can be NULL. */
711 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
712 RETURN_NULL_IF_FAIL (
713 (num_params == 0) || param_types,
714 ctxt, loc,
715 "NULL param_types creating function pointer type");
716 for (int i = 0; i < num_params; i++)
717 RETURN_NULL_IF_FAIL_PRINTF1 (
718 param_types[i],
719 ctxt, loc,
720 "NULL parameter type %i creating function pointer type", i);
722 return (gcc_jit_type*)
723 ctxt->new_function_ptr_type (loc, return_type,
724 num_params,
725 (gcc::jit::recording::type **)param_types,
726 is_variadic);
729 /* Constructing functions. */
731 /* Public entrypoint. See description in libgccjit.h.
733 After error-checking, the real work is done by the
734 gcc::jit::recording::context::new_param method, in jit-recording.c */
736 gcc_jit_param *
737 gcc_jit_context_new_param (gcc_jit_context *ctxt,
738 gcc_jit_location *loc,
739 gcc_jit_type *type,
740 const char *name)
742 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
743 JIT_LOG_FUNC (ctxt->get_logger ());
744 /* LOC can be NULL. */
745 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
746 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
748 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
751 /* Public entrypoint. See description in libgccjit.h.
753 After error-checking, this calls the trivial
754 gcc::jit::recording::memento::as_object method (a param is a memento),
755 in jit-recording.h. */
757 gcc_jit_object *
758 gcc_jit_param_as_object (gcc_jit_param *param)
760 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
762 return static_cast <gcc_jit_object *> (param->as_object ());
765 /* Public entrypoint. See description in libgccjit.h.
767 After error-checking, this calls the trivial
768 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
770 gcc_jit_lvalue *
771 gcc_jit_param_as_lvalue (gcc_jit_param *param)
773 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
775 return (gcc_jit_lvalue *)param->as_lvalue ();
778 /* Public entrypoint. See description in libgccjit.h.
780 After error-checking, this calls the trivial
781 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
782 in jit-recording.h. */
784 gcc_jit_rvalue *
785 gcc_jit_param_as_rvalue (gcc_jit_param *param)
787 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
789 return (gcc_jit_rvalue *)param->as_rvalue ();
792 /* Public entrypoint. See description in libgccjit.h.
794 After error-checking, the real work is done by the
795 gcc::jit::recording::context::new_function method, in
796 jit-recording.c. */
798 gcc_jit_function *
799 gcc_jit_context_new_function (gcc_jit_context *ctxt,
800 gcc_jit_location *loc,
801 enum gcc_jit_function_kind kind,
802 gcc_jit_type *return_type,
803 const char *name,
804 int num_params,
805 gcc_jit_param **params,
806 int is_variadic)
808 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
809 JIT_LOG_FUNC (ctxt->get_logger ());
810 /* LOC can be NULL. */
811 RETURN_NULL_IF_FAIL_PRINTF1 (
812 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
813 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
814 ctxt, loc,
815 "unrecognized value for enum gcc_jit_function_kind: %i",
816 kind);
817 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
818 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
819 /* The assembler can only handle certain names, so for now, enforce
820 C's rules for identiers upon the name, using ISALPHA and ISALNUM
821 from safe-ctype.h to ignore the current locale.
822 Eventually we'll need some way to interact with e.g. C++ name
823 mangling. */
825 /* Leading char: */
826 char ch = *name;
827 RETURN_NULL_IF_FAIL_PRINTF2 (
828 ISALPHA (ch) || ch == '_',
829 ctxt, loc,
830 "name \"%s\" contains invalid character: '%c'",
831 name, ch);
832 /* Subsequent chars: */
833 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
835 RETURN_NULL_IF_FAIL_PRINTF2 (
836 ISALNUM (ch) || ch == '_',
837 ctxt, loc,
838 "name \"%s\" contains invalid character: '%c'",
839 name, ch);
842 RETURN_NULL_IF_FAIL_PRINTF1 (
843 (num_params == 0) || params,
844 ctxt, loc,
845 "NULL params creating function %s", name);
846 for (int i = 0; i < num_params; i++)
847 RETURN_NULL_IF_FAIL_PRINTF2 (
848 params[i],
849 ctxt, loc,
850 "NULL parameter %i creating function %s", i, name);
852 return (gcc_jit_function*)
853 ctxt->new_function (loc, kind, return_type, name,
854 num_params,
855 (gcc::jit::recording::param **)params,
856 is_variadic,
857 BUILT_IN_NONE);
860 /* Public entrypoint. See description in libgccjit.h.
862 After error-checking, the real work is done by the
863 gcc::jit::recording::context::get_builtin_function method, in
864 jit-recording.c. */
866 gcc_jit_function *
867 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
868 const char *name)
870 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
871 JIT_LOG_FUNC (ctxt->get_logger ());
872 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
874 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
877 /* Public entrypoint. See description in libgccjit.h.
879 After error-checking, this calls the trivial
880 gcc::jit::recording::memento::as_object method (a function is a
881 memento), in jit-recording.h. */
883 gcc_jit_object *
884 gcc_jit_function_as_object (gcc_jit_function *func)
886 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
888 return static_cast <gcc_jit_object *> (func->as_object ());
891 /* Public entrypoint. See description in libgccjit.h.
893 After error-checking, the real work is done by the
894 gcc::jit::recording::function::get_param method, in
895 jit-recording.h. */
897 gcc_jit_param *
898 gcc_jit_function_get_param (gcc_jit_function *func, int index)
900 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
901 gcc::jit::recording::context *ctxt = func->m_ctxt;
902 JIT_LOG_FUNC (ctxt->get_logger ());
903 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
904 int num_params = func->get_params ().length ();
905 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
906 ctxt, NULL,
907 "index of %d is too large (%s has %d params)",
908 index,
909 func->get_debug_string (),
910 num_params);
912 return static_cast <gcc_jit_param *> (func->get_param (index));
915 /* Public entrypoint. See description in libgccjit.h.
917 After error-checking, the real work is done by the
918 gcc::jit::recording::function::dump_to_dot method, in
919 jit-recording.c. */
921 void
922 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
923 const char *path)
925 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
926 gcc::jit::recording::context *ctxt = func->m_ctxt;
927 JIT_LOG_FUNC (ctxt->get_logger ());
928 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
930 func->dump_to_dot (path);
933 /* Public entrypoint. See description in libgccjit.h.
935 After error-checking, the real work is done by the
936 gcc::jit::recording::function::new_block method, in
937 jit-recording.c. */
939 gcc_jit_block*
940 gcc_jit_function_new_block (gcc_jit_function *func,
941 const char *name)
943 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
944 JIT_LOG_FUNC (func->get_context ()->get_logger ());
945 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
946 func->get_context (), NULL,
947 "cannot add block to an imported function");
948 /* name can be NULL. */
950 return (gcc_jit_block *)func->new_block (name);
953 /* Public entrypoint. See description in libgccjit.h.
955 After error-checking, this calls the trivial
956 gcc::jit::recording::memento::as_object method (a block is a
957 memento), in jit-recording.h. */
959 gcc_jit_object *
960 gcc_jit_block_as_object (gcc_jit_block *block)
962 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
964 return static_cast <gcc_jit_object *> (block->as_object ());
967 /* Public entrypoint. See description in libgccjit.h.
969 After error-checking, the real work is done by the
970 gcc::jit::recording::block::get_function method, in
971 jit-recording.h. */
973 gcc_jit_function *
974 gcc_jit_block_get_function (gcc_jit_block *block)
976 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
978 return static_cast <gcc_jit_function *> (block->get_function ());
981 /* Public entrypoint. See description in libgccjit.h.
983 After error-checking, the real work is done by the
984 gcc::jit::recording::context::new_global method, in
985 jit-recording.c. */
987 gcc_jit_lvalue *
988 gcc_jit_context_new_global (gcc_jit_context *ctxt,
989 gcc_jit_location *loc,
990 gcc_jit_type *type,
991 const char *name)
993 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
994 JIT_LOG_FUNC (ctxt->get_logger ());
995 /* LOC can be NULL. */
996 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
997 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
999 return (gcc_jit_lvalue *)ctxt->new_global (loc, type, name);
1002 /* Public entrypoint. See description in libgccjit.h.
1004 After error-checking, this calls the trivial
1005 gcc::jit::recording::memento::as_object method (an lvalue is a
1006 memento), in jit-recording.h. */
1008 gcc_jit_object *
1009 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1011 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1013 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1016 /* Public entrypoint. See description in libgccjit.h.
1018 After error-checking, this calls the trivial
1019 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1021 gcc_jit_rvalue *
1022 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1024 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1026 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1029 /* Public entrypoint. See description in libgccjit.h.
1031 After error-checking, this calls the trivial
1032 gcc::jit::recording::memento::as_object method (an rvalue is a
1033 memento), in jit-recording.h. */
1035 gcc_jit_object *
1036 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1038 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1040 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1043 /* Public entrypoint. See description in libgccjit.h.
1045 After error-checking, the real work is done by the
1046 gcc::jit::recording::rvalue::get_type method, in
1047 jit-recording.h. */
1049 gcc_jit_type *
1050 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1052 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1054 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1057 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1058 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1059 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1061 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1062 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1063 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1064 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1065 "not a numeric type: %s", \
1066 NUMERIC_TYPE->get_debug_string ());
1068 /* Public entrypoint. See description in libgccjit.h.
1070 After error-checking, the real work is done by the
1071 gcc::jit::recording::context::new_rvalue_from_int method in
1072 jit-recording.c. */
1074 gcc_jit_rvalue *
1075 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1076 gcc_jit_type *numeric_type,
1077 int value)
1079 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1080 JIT_LOG_FUNC (ctxt->get_logger ());
1081 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1083 return ((gcc_jit_rvalue *)ctxt
1084 ->new_rvalue_from_const <int> (numeric_type, value));
1087 /* FIXME. */
1089 gcc_jit_rvalue *
1090 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1091 gcc_jit_type *numeric_type,
1092 long value)
1094 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1095 JIT_LOG_FUNC (ctxt->get_logger ());
1096 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1098 return ((gcc_jit_rvalue *)ctxt
1099 ->new_rvalue_from_const <long> (numeric_type, value));
1102 /* Public entrypoint. See description in libgccjit.h.
1104 This is essentially equivalent to:
1105 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1106 albeit with slightly different error messages if an error occurs. */
1108 gcc_jit_rvalue *
1109 gcc_jit_context_zero (gcc_jit_context *ctxt,
1110 gcc_jit_type *numeric_type)
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_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1119 /* Public entrypoint. See description in libgccjit.h.
1121 This is essentially equivalent to:
1122 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1123 albeit with slightly different error messages if an error occurs. */
1125 gcc_jit_rvalue *
1126 gcc_jit_context_one (gcc_jit_context *ctxt,
1127 gcc_jit_type *numeric_type)
1129 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1130 JIT_LOG_FUNC (ctxt->get_logger ());
1131 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1133 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1136 /* Public entrypoint. See description in libgccjit.h.
1138 After error-checking, the real work is done by the
1139 gcc::jit::recording::context::new_rvalue_from_double method in
1140 jit-recording.c. */
1142 gcc_jit_rvalue *
1143 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1144 gcc_jit_type *numeric_type,
1145 double value)
1147 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1148 JIT_LOG_FUNC (ctxt->get_logger ());
1149 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1151 return ((gcc_jit_rvalue *)ctxt
1152 ->new_rvalue_from_const <double> (numeric_type, value));
1155 /* Public entrypoint. See description in libgccjit.h.
1157 After error-checking, the real work is done by the
1158 gcc::jit::recording::context::new_rvalue_from_ptr method in
1159 jit-recording.c. */
1161 gcc_jit_rvalue *
1162 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1163 gcc_jit_type *pointer_type,
1164 void *value)
1166 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1167 JIT_LOG_FUNC (ctxt->get_logger ());
1168 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1169 RETURN_NULL_IF_FAIL_PRINTF1 (
1170 pointer_type->is_pointer (),
1171 ctxt, NULL,
1172 "not a pointer type (type: %s)",
1173 pointer_type->get_debug_string ());
1175 return ((gcc_jit_rvalue *)ctxt
1176 ->new_rvalue_from_const <void *> (pointer_type, value));
1179 /* Public entrypoint. See description in libgccjit.h.
1181 This is essentially equivalent to:
1182 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1183 albeit with slightly different error messages if an error occurs. */
1185 gcc_jit_rvalue *
1186 gcc_jit_context_null (gcc_jit_context *ctxt,
1187 gcc_jit_type *pointer_type)
1189 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1190 JIT_LOG_FUNC (ctxt->get_logger ());
1191 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1192 RETURN_NULL_IF_FAIL_PRINTF1 (
1193 pointer_type->is_pointer (),
1194 ctxt, NULL,
1195 "not a pointer type (type: %s)",
1196 pointer_type->get_debug_string ());
1198 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1201 /* Public entrypoint. See description in libgccjit.h.
1203 After error-checking, the real work is done by the
1204 gcc::jit::recording::context::new_string_literal method in
1205 jit-recording.c. */
1207 gcc_jit_rvalue *
1208 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1209 const char *value)
1211 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1212 JIT_LOG_FUNC (ctxt->get_logger ());
1213 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1215 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1218 /* Public entrypoint. See description in libgccjit.h.
1220 After error-checking, the real work is done by the
1221 gcc::jit::recording::context::new_unary_op method in
1222 jit-recording.c. */
1224 gcc_jit_rvalue *
1225 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1226 gcc_jit_location *loc,
1227 enum gcc_jit_unary_op op,
1228 gcc_jit_type *result_type,
1229 gcc_jit_rvalue *rvalue)
1231 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1232 JIT_LOG_FUNC (ctxt->get_logger ());
1233 /* LOC can be NULL. */
1234 RETURN_NULL_IF_FAIL_PRINTF1 (
1235 (op >= GCC_JIT_UNARY_OP_MINUS
1236 && op <= GCC_JIT_UNARY_OP_ABS),
1237 ctxt, loc,
1238 "unrecognized value for enum gcc_jit_unary_op: %i",
1239 op);
1240 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1241 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1243 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1246 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1247 For use by both gcc_jit_context_new_binary_op and
1248 gcc_jit_block_add_assignment_op. */
1250 static bool
1251 valid_binary_op_p (enum gcc_jit_binary_op op)
1253 return (op >= GCC_JIT_BINARY_OP_PLUS
1254 && op <= GCC_JIT_BINARY_OP_RSHIFT);
1257 /* Public entrypoint. See description in libgccjit.h.
1259 After error-checking, the real work is done by the
1260 gcc::jit::recording::context::new_binary_op method in
1261 jit-recording.c. */
1263 gcc_jit_rvalue *
1264 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1265 gcc_jit_location *loc,
1266 enum gcc_jit_binary_op op,
1267 gcc_jit_type *result_type,
1268 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1270 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1271 JIT_LOG_FUNC (ctxt->get_logger ());
1272 /* LOC can be NULL. */
1273 RETURN_NULL_IF_FAIL_PRINTF1 (
1274 valid_binary_op_p (op),
1275 ctxt, loc,
1276 "unrecognized value for enum gcc_jit_binary_op: %i",
1277 op);
1278 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1279 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1280 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1281 RETURN_NULL_IF_FAIL_PRINTF4 (
1282 a->get_type () == b->get_type (),
1283 ctxt, loc,
1284 "mismatching types for binary op:"
1285 " a: %s (type: %s) b: %s (type: %s)",
1286 a->get_debug_string (),
1287 a->get_type ()->get_debug_string (),
1288 b->get_debug_string (),
1289 b->get_type ()->get_debug_string ());
1291 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1294 /* Public entrypoint. See description in libgccjit.h.
1296 After error-checking, the real work is done by the
1297 gcc::jit::recording::context::new_comparison method in
1298 jit-recording.c. */
1300 gcc_jit_rvalue *
1301 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1302 gcc_jit_location *loc,
1303 enum gcc_jit_comparison op,
1304 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1306 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1307 JIT_LOG_FUNC (ctxt->get_logger ());
1308 /* LOC can be NULL. */
1309 RETURN_NULL_IF_FAIL_PRINTF1 (
1310 (op >= GCC_JIT_COMPARISON_EQ
1311 && op <= GCC_JIT_COMPARISON_GE),
1312 ctxt, loc,
1313 "unrecognized value for enum gcc_jit_comparison: %i",
1314 op);
1315 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1316 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1317 RETURN_NULL_IF_FAIL_PRINTF4 (
1318 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1319 ctxt, loc,
1320 "mismatching types for comparison:"
1321 " a: %s (type: %s) b: %s (type: %s)",
1322 a->get_debug_string (),
1323 a->get_type ()->get_debug_string (),
1324 b->get_debug_string (),
1325 b->get_type ()->get_debug_string ());
1327 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1330 /* Public entrypoint. See description in libgccjit.h.
1332 After error-checking, the real work is done by the
1333 gcc::jit::recording::context::new_call method in
1334 jit-recording.c. */
1336 gcc_jit_rvalue *
1337 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1338 gcc_jit_location *loc,
1339 gcc_jit_function *func,
1340 int numargs , gcc_jit_rvalue **args)
1342 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1343 JIT_LOG_FUNC (ctxt->get_logger ());
1344 /* LOC can be NULL. */
1345 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1346 if (numargs)
1347 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1349 int min_num_params = func->get_params ().length ();
1350 bool is_variadic = func->is_variadic ();
1352 RETURN_NULL_IF_FAIL_PRINTF3 (
1353 numargs >= min_num_params,
1354 ctxt, loc,
1355 "not enough arguments to function \"%s\""
1356 " (got %i args, expected %i)",
1357 func->get_name ()->c_str (),
1358 numargs, min_num_params);
1360 RETURN_NULL_IF_FAIL_PRINTF3 (
1361 (numargs == min_num_params || is_variadic),
1362 ctxt, loc,
1363 "too many arguments to function \"%s\""
1364 " (got %i args, expected %i)",
1365 func->get_name ()->c_str (),
1366 numargs, min_num_params);
1368 for (int i = 0; i < min_num_params; i++)
1370 gcc::jit::recording::param *param = func->get_param (i);
1371 gcc_jit_rvalue *arg = args[i];
1373 RETURN_NULL_IF_FAIL_PRINTF4 (
1374 arg,
1375 ctxt, loc,
1376 "NULL argument %i to function \"%s\":"
1377 " param %s (type: %s)",
1378 i + 1,
1379 func->get_name ()->c_str (),
1380 param->get_debug_string (),
1381 param->get_type ()->get_debug_string ());
1383 RETURN_NULL_IF_FAIL_PRINTF6 (
1384 compatible_types (param->get_type (),
1385 arg->get_type ()),
1386 ctxt, loc,
1387 "mismatching types for argument %d of function \"%s\":"
1388 " assignment to param %s (type: %s) from %s (type: %s)",
1389 i + 1,
1390 func->get_name ()->c_str (),
1391 param->get_debug_string (),
1392 param->get_type ()->get_debug_string (),
1393 arg->get_debug_string (),
1394 arg->get_type ()->get_debug_string ());
1397 return (gcc_jit_rvalue *)ctxt->new_call (loc,
1398 func,
1399 numargs,
1400 (gcc::jit::recording::rvalue **)args);
1403 /* Public entrypoint. See description in libgccjit.h.
1405 After error-checking, the real work is done by the
1406 gcc::jit::recording::context::new_call_through_ptr method in
1407 jit-recording.c. */
1409 gcc_jit_rvalue *
1410 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1411 gcc_jit_location *loc,
1412 gcc_jit_rvalue *fn_ptr,
1413 int numargs, gcc_jit_rvalue **args)
1415 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1416 JIT_LOG_FUNC (ctxt->get_logger ());
1417 /* LOC can be NULL. */
1418 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1419 if (numargs)
1420 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1422 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1423 RETURN_NULL_IF_FAIL_PRINTF2 (
1424 ptr_type, ctxt, loc,
1425 "fn_ptr is not a ptr: %s"
1426 " type: %s",
1427 fn_ptr->get_debug_string (),
1428 fn_ptr->get_type ()->get_debug_string ());
1430 gcc::jit::recording::function_type *fn_type =
1431 ptr_type->dyn_cast_function_type();
1432 RETURN_NULL_IF_FAIL_PRINTF2 (
1433 fn_type, ctxt, loc,
1434 "fn_ptr is not a function ptr: %s"
1435 " type: %s",
1436 fn_ptr->get_debug_string (),
1437 fn_ptr->get_type ()->get_debug_string ());
1439 int min_num_params = fn_type->get_param_types ().length ();
1440 bool is_variadic = fn_type->is_variadic ();
1442 RETURN_NULL_IF_FAIL_PRINTF3 (
1443 numargs >= min_num_params,
1444 ctxt, loc,
1445 "not enough arguments to fn_ptr: %s"
1446 " (got %i args, expected %i)",
1447 fn_ptr->get_debug_string (),
1448 numargs, min_num_params);
1450 RETURN_NULL_IF_FAIL_PRINTF3 (
1451 (numargs == min_num_params || is_variadic),
1452 ctxt, loc,
1453 "too many arguments to fn_ptr: %s"
1454 " (got %i args, expected %i)",
1455 fn_ptr->get_debug_string (),
1456 numargs, min_num_params);
1458 for (int i = 0; i < min_num_params; i++)
1460 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1461 gcc_jit_rvalue *arg = args[i];
1463 RETURN_NULL_IF_FAIL_PRINTF3 (
1464 arg,
1465 ctxt, loc,
1466 "NULL argument %i to fn_ptr: %s"
1467 " (type: %s)",
1468 i + 1,
1469 fn_ptr->get_debug_string (),
1470 param_type->get_debug_string ());
1472 RETURN_NULL_IF_FAIL_PRINTF6 (
1473 compatible_types (param_type,
1474 arg->get_type ()),
1475 ctxt, loc,
1476 "mismatching types for argument %d of fn_ptr: %s:"
1477 " assignment to param %d (type: %s) from %s (type: %s)",
1478 i + 1,
1479 fn_ptr->get_debug_string (),
1480 i + 1,
1481 param_type->get_debug_string (),
1482 arg->get_debug_string (),
1483 arg->get_type ()->get_debug_string ());
1486 return (gcc_jit_rvalue *)(
1487 ctxt->new_call_through_ptr (loc,
1488 fn_ptr,
1489 numargs,
1490 (gcc::jit::recording::rvalue **)args));
1493 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1494 to DST_TYPE, for use by gcc_jit_context_new_cast.
1496 We only permit these kinds of cast:
1498 int <-> float
1499 int <-> bool
1500 P* <-> Q* for pointer types P and Q. */
1502 static bool
1503 is_valid_cast (gcc::jit::recording::type *src_type,
1504 gcc_jit_type *dst_type)
1506 bool src_is_int = src_type->is_int ();
1507 bool dst_is_int = dst_type->is_int ();
1508 bool src_is_float = src_type->is_float ();
1509 bool dst_is_float = dst_type->is_float ();
1510 bool src_is_bool = src_type->is_bool ();
1511 bool dst_is_bool = dst_type->is_bool ();
1513 if (src_is_int)
1514 if (dst_is_int || dst_is_float || dst_is_bool)
1515 return true;
1517 if (src_is_float)
1518 if (dst_is_int || dst_is_float)
1519 return true;
1521 if (src_is_bool)
1522 if (dst_is_int || dst_is_bool)
1523 return true;
1525 /* Permit casts between pointer types. */
1526 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1527 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1528 if (deref_src_type && deref_dst_type)
1529 return true;
1531 return false;
1534 /* Public entrypoint. See description in libgccjit.h.
1536 After error-checking, the real work is done by the
1537 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1539 gcc_jit_rvalue *
1540 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1541 gcc_jit_location *loc,
1542 gcc_jit_rvalue *rvalue,
1543 gcc_jit_type *type)
1545 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1546 JIT_LOG_FUNC (ctxt->get_logger ());
1547 /* LOC can be NULL. */
1548 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1549 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1550 RETURN_NULL_IF_FAIL_PRINTF3 (
1551 is_valid_cast (rvalue->get_type (), type),
1552 ctxt, loc,
1553 "cannot cast %s from type: %s to type: %s",
1554 rvalue->get_debug_string (),
1555 rvalue->get_type ()->get_debug_string (),
1556 type->get_debug_string ());
1558 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1561 /* Public entrypoint. See description in libgccjit.h.
1563 After error-checking, the real work is done by the
1564 gcc::jit::recording::context::new_array_access method in
1565 jit-recording.c. */
1567 extern gcc_jit_lvalue *
1568 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1569 gcc_jit_location *loc,
1570 gcc_jit_rvalue *ptr,
1571 gcc_jit_rvalue *index)
1573 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1574 JIT_LOG_FUNC (ctxt->get_logger ());
1575 /* LOC can be NULL. */
1576 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1577 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1578 RETURN_NULL_IF_FAIL_PRINTF2 (
1579 ptr->get_type ()->dereference (),
1580 ctxt, loc,
1581 "ptr: %s (type: %s) is not a pointer or array",
1582 ptr->get_debug_string (),
1583 ptr->get_type ()->get_debug_string ());
1584 RETURN_NULL_IF_FAIL_PRINTF2 (
1585 index->get_type ()->is_numeric (),
1586 ctxt, loc,
1587 "index: %s (type: %s) is not of numeric type",
1588 index->get_debug_string (),
1589 index->get_type ()->get_debug_string ());
1591 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1594 /* Public entrypoint. See description in libgccjit.h.
1596 After error-checking, the real work is done by the
1597 gcc::jit::recording::memento::get_context method in
1598 jit-recording.h. */
1600 gcc_jit_context *
1601 gcc_jit_object_get_context (gcc_jit_object *obj)
1603 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1605 return static_cast <gcc_jit_context *> (obj->get_context ());
1608 /* Public entrypoint. See description in libgccjit.h.
1610 After error-checking, the real work is done by the
1611 gcc::jit::recording::memento::get_debug_string method in
1612 jit-recording.c. */
1614 const char *
1615 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1617 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1619 return obj->get_debug_string ();
1622 /* Public entrypoint. See description in libgccjit.h.
1624 After error-checking, the real work is done by the
1625 gcc::jit::recording::lvalue::access_field method in
1626 jit-recording.c. */
1628 gcc_jit_lvalue *
1629 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1630 gcc_jit_location *loc,
1631 gcc_jit_field *field)
1633 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1634 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1635 JIT_LOG_FUNC (ctxt->get_logger ());
1636 /* LOC can be NULL. */
1637 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1638 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1639 "field %s has not been placed in a struct",
1640 field->get_debug_string ());
1642 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1645 /* Public entrypoint. See description in libgccjit.h.
1647 After error-checking, the real work is done by the
1648 gcc::jit::recording::rvalue::access_field method in
1649 jit-recording.c. */
1651 gcc_jit_rvalue *
1652 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1653 gcc_jit_location *loc,
1654 gcc_jit_field *field)
1656 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1657 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1658 JIT_LOG_FUNC (ctxt->get_logger ());
1659 /* LOC can be NULL. */
1660 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1661 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1662 "field %s has not been placed in a struct",
1663 field->get_debug_string ());
1665 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1668 /* Public entrypoint. See description in libgccjit.h.
1670 After error-checking, the real work is done by the
1671 gcc::jit::recording::rvalue::deference_field method in
1672 jit-recording.c. */
1674 gcc_jit_lvalue *
1675 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1676 gcc_jit_location *loc,
1677 gcc_jit_field *field)
1679 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1680 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1681 /* LOC can be NULL. */
1682 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1683 gcc::jit::recording::type *underlying_type =
1684 ptr->get_type ()->is_pointer ();
1685 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1686 "field %s has not been placed in a struct",
1687 field->get_debug_string ());
1688 RETURN_NULL_IF_FAIL_PRINTF3 (
1689 underlying_type,
1690 ptr->m_ctxt, loc,
1691 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1692 ptr->get_debug_string (),
1693 ptr->get_type ()->get_debug_string (),
1694 field->get_debug_string ());
1695 RETURN_NULL_IF_FAIL_PRINTF2 (
1696 (field->get_container ()->unqualified ()
1697 == underlying_type->unqualified ()),
1698 ptr->m_ctxt, loc,
1699 "%s is not a field of %s",
1700 field->get_debug_string (),
1701 underlying_type->get_debug_string ());
1703 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1706 /* Public entrypoint. See description in libgccjit.h.
1708 After error-checking, the real work is done by the
1709 gcc::jit::recording::rvalue::deference method in
1710 jit-recording.c. */
1712 gcc_jit_lvalue *
1713 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1714 gcc_jit_location *loc)
1716 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1717 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1718 /* LOC can be NULL. */
1720 gcc::jit::recording::type *underlying_type =
1721 rvalue->get_type ()->is_pointer ();
1723 RETURN_NULL_IF_FAIL_PRINTF2 (
1724 underlying_type,
1725 rvalue->m_ctxt, loc,
1726 "dereference of non-pointer %s (type: %s)",
1727 rvalue->get_debug_string (),
1728 rvalue->get_type ()->get_debug_string ());
1730 RETURN_NULL_IF_FAIL_PRINTF2 (
1731 !underlying_type->is_void (),
1732 rvalue->m_ctxt, loc,
1733 "dereference of void pointer %s (type: %s)",
1734 rvalue->get_debug_string (),
1735 rvalue->get_type ()->get_debug_string ());
1737 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1740 /* Public entrypoint. See description in libgccjit.h.
1742 After error-checking, the real work is done by the
1743 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1745 gcc_jit_rvalue *
1746 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1747 gcc_jit_location *loc)
1749 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1750 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1751 /* LOC can be NULL. */
1753 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1756 /* Public entrypoint. See description in libgccjit.h.
1758 After error-checking, the real work is done by the
1759 gcc::jit::recording::function::new_local method in jit-recording.c. */
1761 gcc_jit_lvalue *
1762 gcc_jit_function_new_local (gcc_jit_function *func,
1763 gcc_jit_location *loc,
1764 gcc_jit_type *type,
1765 const char *name)
1767 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1768 gcc::jit::recording::context *ctxt = func->m_ctxt;
1769 JIT_LOG_FUNC (ctxt->get_logger ());
1770 /* LOC can be NULL. */
1771 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1772 ctxt, loc,
1773 "Cannot add locals to an imported function");
1774 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1775 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1777 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1780 /* Public entrypoint. See description in libgccjit.h.
1782 After error-checking, the real work is done by the
1783 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1785 void
1786 gcc_jit_block_add_eval (gcc_jit_block *block,
1787 gcc_jit_location *loc,
1788 gcc_jit_rvalue *rvalue)
1790 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1791 gcc::jit::recording::context *ctxt = block->get_context ();
1792 JIT_LOG_FUNC (ctxt->get_logger ());
1793 /* LOC can be NULL. */
1794 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1796 return block->add_eval (loc, rvalue);
1799 /* Public entrypoint. See description in libgccjit.h.
1801 After error-checking, the real work is done by the
1802 gcc::jit::recording::block::add_assignment method in
1803 jit-recording.c. */
1805 void
1806 gcc_jit_block_add_assignment (gcc_jit_block *block,
1807 gcc_jit_location *loc,
1808 gcc_jit_lvalue *lvalue,
1809 gcc_jit_rvalue *rvalue)
1811 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1812 gcc::jit::recording::context *ctxt = block->get_context ();
1813 JIT_LOG_FUNC (ctxt->get_logger ());
1814 /* LOC can be NULL. */
1815 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1816 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1817 RETURN_IF_FAIL_PRINTF4 (
1818 compatible_types (lvalue->get_type (),
1819 rvalue->get_type ()),
1820 ctxt, loc,
1821 "mismatching types:"
1822 " assignment to %s (type: %s) from %s (type: %s)",
1823 lvalue->get_debug_string (),
1824 lvalue->get_type ()->get_debug_string (),
1825 rvalue->get_debug_string (),
1826 rvalue->get_type ()->get_debug_string ());
1828 return block->add_assignment (loc, lvalue, rvalue);
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_assignment_op method in
1835 jit-recording.c. */
1837 void
1838 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1839 gcc_jit_location *loc,
1840 gcc_jit_lvalue *lvalue,
1841 enum gcc_jit_binary_op op,
1842 gcc_jit_rvalue *rvalue)
1844 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1845 gcc::jit::recording::context *ctxt = block->get_context ();
1846 JIT_LOG_FUNC (ctxt->get_logger ());
1847 /* LOC can be NULL. */
1848 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1849 RETURN_IF_FAIL_PRINTF1 (
1850 valid_binary_op_p (op),
1851 ctxt, loc,
1852 "unrecognized value for enum gcc_jit_binary_op: %i",
1853 op);
1854 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1856 return block->add_assignment_op (loc, lvalue, op, rvalue);
1859 /* Internal helper function for determining if rvalue BOOLVAL is of
1860 boolean type. For use by gcc_jit_block_end_with_conditional. */
1862 static bool
1863 is_bool (gcc_jit_rvalue *boolval)
1865 gcc::jit::recording::type *actual_type = boolval->get_type ();
1866 gcc::jit::recording::type *bool_type =
1867 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1868 return actual_type == bool_type;
1871 /* Public entrypoint. See description in libgccjit.h.
1873 After error-checking, the real work is done by the
1874 gcc::jit::recording::block::end_with_conditional method in
1875 jit-recording.c. */
1877 void
1878 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1879 gcc_jit_location *loc,
1880 gcc_jit_rvalue *boolval,
1881 gcc_jit_block *on_true,
1882 gcc_jit_block *on_false)
1884 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1885 gcc::jit::recording::context *ctxt = block->get_context ();
1886 JIT_LOG_FUNC (ctxt->get_logger ());
1887 /* LOC can be NULL. */
1888 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
1889 RETURN_IF_FAIL_PRINTF2 (
1890 is_bool (boolval), ctxt, loc,
1891 "%s (type: %s) is not of boolean type ",
1892 boolval->get_debug_string (),
1893 boolval->get_type ()->get_debug_string ());
1894 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
1895 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
1896 RETURN_IF_FAIL_PRINTF4 (
1897 block->get_function () == on_true->get_function (),
1898 ctxt, loc,
1899 "\"on_true\" block is not in same function:"
1900 " source block %s is in function %s"
1901 " whereas target block %s is in function %s",
1902 block->get_debug_string (),
1903 block->get_function ()->get_debug_string (),
1904 on_true->get_debug_string (),
1905 on_true->get_function ()->get_debug_string ());
1906 RETURN_IF_FAIL_PRINTF4 (
1907 block->get_function () == on_false->get_function (),
1908 ctxt, loc,
1909 "\"on_false\" block is not in same function:"
1910 " source block %s is in function %s"
1911 " whereas target block %s is in function %s",
1912 block->get_debug_string (),
1913 block->get_function ()->get_debug_string (),
1914 on_false->get_debug_string (),
1915 on_false->get_function ()->get_debug_string ());
1917 return block->end_with_conditional (loc, boolval, on_true, on_false);
1920 /* Public entrypoint. See description in libgccjit.h.
1922 After error-checking, the real work is done by the
1923 gcc::jit::recording::block::add_comment method in
1924 jit-recording.c. */
1926 void
1927 gcc_jit_block_add_comment (gcc_jit_block *block,
1928 gcc_jit_location *loc,
1929 const char *text)
1931 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1932 gcc::jit::recording::context *ctxt = block->get_context ();
1933 JIT_LOG_FUNC (ctxt->get_logger ());
1934 /* LOC can be NULL. */
1935 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
1937 block->add_comment (loc, text);
1940 /* Public entrypoint. See description in libgccjit.h.
1942 After error-checking, the real work is done by the
1943 gcc::jit::recording::block::end_with_jump method in
1944 jit-recording.c. */
1946 void
1947 gcc_jit_block_end_with_jump (gcc_jit_block *block,
1948 gcc_jit_location *loc,
1949 gcc_jit_block *target)
1951 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1952 gcc::jit::recording::context *ctxt = block->get_context ();
1953 JIT_LOG_FUNC (ctxt->get_logger ());
1954 /* LOC can be NULL. */
1955 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
1956 RETURN_IF_FAIL_PRINTF4 (
1957 block->get_function () == target->get_function (),
1958 ctxt, loc,
1959 "target block is not in same function:"
1960 " source block %s is in function %s"
1961 " whereas target block %s is in function %s",
1962 block->get_debug_string (),
1963 block->get_function ()->get_debug_string (),
1964 target->get_debug_string (),
1965 target->get_function ()->get_debug_string ());
1967 block->end_with_jump (loc, target);
1970 /* Public entrypoint. See description in libgccjit.h.
1972 After error-checking, the real work is done by the
1973 gcc::jit::recording::block::end_with_return method in
1974 jit-recording.c. */
1976 void
1977 gcc_jit_block_end_with_return (gcc_jit_block *block,
1978 gcc_jit_location *loc,
1979 gcc_jit_rvalue *rvalue)
1981 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1982 gcc::jit::recording::context *ctxt = block->get_context ();
1983 JIT_LOG_FUNC (ctxt->get_logger ());
1984 /* LOC can be NULL. */
1985 gcc::jit::recording::function *func = block->get_function ();
1986 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1987 RETURN_IF_FAIL_PRINTF4 (
1988 compatible_types (
1989 func->get_return_type (),
1990 rvalue->get_type ()),
1991 ctxt, loc,
1992 "mismatching types:"
1993 " return of %s (type: %s) in function %s (return type: %s)",
1994 rvalue->get_debug_string (),
1995 rvalue->get_type ()->get_debug_string (),
1996 func->get_debug_string (),
1997 func->get_return_type ()->get_debug_string ());
1999 return block->end_with_return (loc, rvalue);
2002 /* Public entrypoint. See description in libgccjit.h.
2004 After error-checking, the real work is done by the
2005 gcc::jit::recording::block::end_with_return method in
2006 jit-recording.c. */
2008 void
2009 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
2010 gcc_jit_location *loc)
2012 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2013 gcc::jit::recording::context *ctxt = block->get_context ();
2014 JIT_LOG_FUNC (ctxt->get_logger ());
2015 /* LOC can be NULL. */
2016 gcc::jit::recording::function *func = block->get_function ();
2017 RETURN_IF_FAIL_PRINTF2 (
2018 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2019 ctxt, loc,
2020 "mismatching types:"
2021 " void return in function %s (return type: %s)",
2022 func->get_debug_string (),
2023 func->get_return_type ()->get_debug_string ());
2025 return block->end_with_return (loc, NULL);
2028 /**********************************************************************
2029 Option-management
2030 **********************************************************************/
2032 /* Public entrypoint. See description in libgccjit.h.
2034 After error-checking, the real work is done by the
2035 gcc::jit::recording::context::set_str_option method in
2036 jit-recording.c. */
2038 void
2039 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2040 enum gcc_jit_str_option opt,
2041 const char *value)
2043 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2044 JIT_LOG_FUNC (ctxt->get_logger ());
2045 /* opt is checked by the inner function.
2046 value can be NULL. */
2048 ctxt->set_str_option (opt, value);
2051 /* Public entrypoint. See description in libgccjit.h.
2053 After error-checking, the real work is done by the
2054 gcc::jit::recording::context::set_int_option method in
2055 jit-recording.c. */
2057 void
2058 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2059 enum gcc_jit_int_option opt,
2060 int value)
2062 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2063 JIT_LOG_FUNC (ctxt->get_logger ());
2064 /* opt is checked by the inner function. */
2066 ctxt->set_int_option (opt, value);
2069 /* Public entrypoint. See description in libgccjit.h.
2071 After error-checking, the real work is done by the
2072 gcc::jit::recording::context::set_bool_option method in
2073 jit-recording.c. */
2075 void
2076 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2077 enum gcc_jit_bool_option opt,
2078 int value)
2080 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2081 JIT_LOG_FUNC (ctxt->get_logger ());
2082 /* opt is checked by the inner function. */
2084 ctxt->set_bool_option (opt, value);
2087 /* Public entrypoint. See description in libgccjit.h.
2089 After error-checking, the real work is done by the
2090 gcc::jit::recording::context::enable_dump method in
2091 jit-recording.c. */
2093 void
2094 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2095 const char *dumpname,
2096 char **out_ptr)
2098 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2099 JIT_LOG_FUNC (ctxt->get_logger ());
2100 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2101 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2103 ctxt->enable_dump (dumpname, out_ptr);
2106 /* Public entrypoint. See description in libgccjit.h.
2108 After error-checking, the real work is done by the
2109 gcc::jit::recording::context::compile method in
2110 jit-recording.c. */
2112 gcc_jit_result *
2113 gcc_jit_context_compile (gcc_jit_context *ctxt)
2115 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2117 JIT_LOG_FUNC (ctxt->get_logger ());
2119 ctxt->log ("compiling ctxt: %p", (void *)ctxt);
2121 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2123 ctxt->log ("%s: returning (gcc_jit_result *)%p",
2124 __func__, (void *)result);
2126 return result;
2129 /* Public entrypoint. See description in libgccjit.h.
2131 After error-checking, the real work is done by the
2132 gcc::jit::recording::context::dump_to_file method in
2133 jit-recording.c. */
2135 void
2136 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2137 const char *path,
2138 int update_locations)
2140 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2141 JIT_LOG_FUNC (ctxt->get_logger ());
2142 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2143 ctxt->dump_to_file (path, update_locations);
2146 /* Public entrypoint. See description in libgccjit.h. */
2148 void
2149 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2150 FILE *logfile,
2151 int flags,
2152 int verbosity)
2154 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2155 JIT_LOG_FUNC (ctxt->get_logger ());
2156 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2157 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2159 gcc::jit::logger *logger;
2160 if (logfile)
2161 logger = new gcc::jit::logger (logfile, flags, verbosity);
2162 else
2163 logger = NULL;
2164 ctxt->set_logger (logger);
2167 /* Public entrypoint. See description in libgccjit.h.
2169 After error-checking, the real work is done by the
2170 gcc::jit::recording::context::get_first_error method in
2171 jit-recording.c. */
2173 const char *
2174 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2176 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2177 JIT_LOG_FUNC (ctxt->get_logger ());
2179 return ctxt->get_first_error ();
2182 /* Public entrypoint. See description in libgccjit.h.
2184 After error-checking, the real work is done by the
2185 gcc::jit::recording::context::get_last_error method in
2186 jit-recording.c. */
2188 const char *
2189 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2191 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2193 return ctxt->get_last_error ();
2196 /* Public entrypoint. See description in libgccjit.h.
2198 After error-checking, the real work is done by the
2199 gcc::jit::result::get_code method in jit-result.c. */
2201 void *
2202 gcc_jit_result_get_code (gcc_jit_result *result,
2203 const char *fnname)
2205 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2206 JIT_LOG_FUNC (result->get_logger ());
2207 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2209 result->log ("locating fnname: %s", fnname);
2210 void *code = result->get_code (fnname);
2211 result->log ("%s: returning (void *)%p", __func__, code);
2213 return code;
2216 /* Public entrypoint. See description in libgccjit.h.
2218 After error-checking, this is essentially a wrapper around the
2219 destructor for gcc::jit::result in jit-result.c. */
2221 void
2222 gcc_jit_result_release (gcc_jit_result *result)
2224 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2225 JIT_LOG_FUNC (result->get_logger ());
2226 result->log ("deleting result: %p", (void *)result);
2227 delete result;