jit: add switch statements
[official-gcc.git] / gcc / jit / libgccjit.c
blob4d7dd8cf40f66bc642f98a5a33a2ca5145367768
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"
26 #include "typed-splay-tree.h"
28 #include "libgccjit.h"
29 #include "jit-common.h"
30 #include "jit-logging.h"
31 #include "jit-recording.h"
32 #include "jit-result.h"
34 /* The opaque types used by the public API are actually subclasses
35 of the gcc::jit::recording classes. */
37 struct gcc_jit_context : public gcc::jit::recording::context
39 gcc_jit_context (gcc_jit_context *parent_ctxt) :
40 context (parent_ctxt)
44 struct gcc_jit_result : public gcc::jit::result
48 struct gcc_jit_object : public gcc::jit::recording::memento
52 struct gcc_jit_location : public gcc::jit::recording::location
56 struct gcc_jit_type : public gcc::jit::recording::type
60 struct gcc_jit_struct : public gcc::jit::recording::struct_
64 struct gcc_jit_field : public gcc::jit::recording::field
68 struct gcc_jit_function : public gcc::jit::recording::function
72 struct gcc_jit_block : public gcc::jit::recording::block
76 struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
80 struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
84 struct gcc_jit_param : public gcc::jit::recording::param
88 struct gcc_jit_case : public gcc::jit::recording::case_
92 /**********************************************************************
93 Error-handling.
95 We try to gracefully handle API usage errors by being defensive
96 at the API boundary.
97 **********************************************************************/
99 #define JIT_BEGIN_STMT do {
100 #define JIT_END_STMT } while(0)
102 /* Each of these error-handling macros determines if TEST_EXPR holds.
104 If TEXT_EXPR fails to hold we return from the enclosing function and
105 print an error, either via adding an error on the given context CTXT
106 if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
107 is NULL.
109 They have to be macros since they inject their "return" into the
110 function they are placed in.
112 The variant macros express:
114 (A) whether or not we need to return a value:
115 RETURN_VAL_IF_FAIL* vs
116 RETURN_IF_FAIL*,
117 with the former returning RETURN_EXPR, and
118 RETURN_NULL_IF_FAIL*
119 for the common case where a NULL value is to be returned on
120 error, and
122 (B) whether the error message is to be directly printed:
123 RETURN_*IF_FAIL
124 or is a format string with some number of arguments:
125 RETURN_*IF_FAIL_PRINTF*
127 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
128 trailing semicolons.
131 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
132 JIT_BEGIN_STMT \
133 if (!(TEST_EXPR)) \
135 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
136 return (RETURN_EXPR); \
138 JIT_END_STMT
140 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
141 JIT_BEGIN_STMT \
142 if (!(TEST_EXPR)) \
144 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
145 __func__, (A0)); \
146 return (RETURN_EXPR); \
148 JIT_END_STMT
150 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
151 JIT_BEGIN_STMT \
152 if (!(TEST_EXPR)) \
154 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
155 __func__, (A0), (A1)); \
156 return (RETURN_EXPR); \
158 JIT_END_STMT
160 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
161 JIT_BEGIN_STMT \
162 if (!(TEST_EXPR)) \
164 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
165 __func__, (A0), (A1), (A2)); \
166 return (RETURN_EXPR); \
168 JIT_END_STMT
170 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
171 JIT_BEGIN_STMT \
172 if (!(TEST_EXPR)) \
174 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
175 __func__, (A0), (A1), (A2), (A3)); \
176 return (RETURN_EXPR); \
178 JIT_END_STMT
180 #define RETURN_VAL_IF_FAIL_PRINTF5(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
181 JIT_BEGIN_STMT \
182 if (!(TEST_EXPR)) \
184 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
185 __func__, (A0), (A1), (A2), (A3), (A4)); \
186 return (RETURN_EXPR); \
188 JIT_END_STMT
190 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
191 JIT_BEGIN_STMT \
192 if (!(TEST_EXPR)) \
194 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
195 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
196 return (RETURN_EXPR); \
198 JIT_END_STMT
200 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
201 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
203 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
204 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
206 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
207 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
209 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
210 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
212 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
213 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
215 #define RETURN_NULL_IF_FAIL_PRINTF5(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
216 RETURN_VAL_IF_FAIL_PRINTF5 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4)
218 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
219 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
221 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
222 JIT_BEGIN_STMT \
223 if (!(TEST_EXPR)) \
225 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
226 return; \
228 JIT_END_STMT
230 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
231 JIT_BEGIN_STMT \
232 if (!(TEST_EXPR)) \
234 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
235 __func__, (A0)); \
236 return; \
238 JIT_END_STMT
240 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
241 JIT_BEGIN_STMT \
242 if (!(TEST_EXPR)) \
244 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
245 __func__, (A0), (A1)); \
246 return; \
248 JIT_END_STMT
250 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
251 JIT_BEGIN_STMT \
252 if (!(TEST_EXPR)) \
254 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
255 __func__, (A0), (A1), (A2), (A3)); \
256 return; \
258 JIT_END_STMT
260 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
261 it. This will fail if BLOCK has already been terminated by some
262 kind of jump or a return. */
263 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
264 JIT_BEGIN_STMT \
265 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
266 RETURN_IF_FAIL_PRINTF2 ( \
267 !(BLOCK)->has_been_terminated (), \
268 (BLOCK)->get_context (), \
269 (LOC), \
270 "adding to terminated block: %s (already terminated by: %s)", \
271 (BLOCK)->get_debug_string (), \
272 (BLOCK)->get_last_statement ()->get_debug_string ()); \
273 JIT_END_STMT
275 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
276 fails. */
277 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
278 JIT_BEGIN_STMT \
279 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
280 RETURN_NULL_IF_FAIL_PRINTF2 ( \
281 !(BLOCK)->has_been_terminated (), \
282 (BLOCK)->get_context (), \
283 (LOC), \
284 "adding to terminated block: %s (already terminated by: %s)", \
285 (BLOCK)->get_debug_string (), \
286 (BLOCK)->get_last_statement ()->get_debug_string ()); \
287 JIT_END_STMT
289 /* Format the given string, and report it as an error, either on CTXT
290 if non-NULL, or by printing to stderr if we have a NULL context.
291 LOC gives the source location where the error occcurred, and can be
292 NULL. */
294 static void
295 jit_error (gcc::jit::recording::context *ctxt,
296 gcc_jit_location *loc,
297 const char *fmt, ...)
298 GNU_PRINTF(3, 4);
300 static void
301 jit_error (gcc::jit::recording::context *ctxt,
302 gcc_jit_location *loc,
303 const char *fmt, ...)
305 va_list ap;
306 va_start (ap, fmt);
308 if (ctxt)
309 ctxt->add_error_va (loc, fmt, ap);
310 else
312 /* No context? Send to stderr. */
313 vfprintf (stderr, fmt, ap);
314 fprintf (stderr, "\n");
317 va_end (ap);
320 /* Determine whether or not we can write to lvalues of type LTYPE from
321 rvalues of type RTYPE, detecting type errors such as attempting to
322 write to an int with a string literal (without an explicit cast).
324 This is implemented by calling the
325 gcc::jit::recording::type::accepts_writes_from virtual function on
326 LTYPE. */
328 static bool
329 compatible_types (gcc::jit::recording::type *ltype,
330 gcc::jit::recording::type *rtype)
332 return ltype->accepts_writes_from (rtype);
335 /* Public entrypoint for acquiring a gcc_jit_context.
336 Note that this creates a new top-level context; contrast with
337 gcc_jit_context_new_child_context below.
339 The real work is done in the constructor for
340 gcc::jit::recording::context in jit-recording.c. */
342 gcc_jit_context *
343 gcc_jit_context_acquire (void)
345 gcc_jit_context *ctxt = new gcc_jit_context (NULL);
346 ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
347 return ctxt;
350 /* Public entrypoint for releasing a gcc_jit_context.
351 The real work is done in the destructor for
352 gcc::jit::recording::context in jit-recording.c. */
354 void
355 gcc_jit_context_release (gcc_jit_context *ctxt)
357 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
358 JIT_LOG_FUNC (ctxt->get_logger ());
359 ctxt->log ("deleting ctxt: %p", (void *)ctxt);
360 delete ctxt;
363 /* Public entrypoint for creating a child context within
364 PARENT_CTXT. See description in libgccjit.h.
366 The real work is done in the constructor for
367 gcc::jit::recording::context in jit-recording.c. */
369 gcc_jit_context *
370 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
372 RETURN_NULL_IF_FAIL (parent_ctxt, NULL, NULL, "NULL parent ctxt");
373 JIT_LOG_FUNC (parent_ctxt->get_logger ());
374 parent_ctxt->log ("parent_ctxt: %p", (void *)parent_ctxt);
375 gcc_jit_context *child_ctxt = new gcc_jit_context (parent_ctxt);
376 child_ctxt->log ("new child_ctxt: %p", (void *)child_ctxt);
377 return child_ctxt;
380 /* Public entrypoint. See description in libgccjit.h.
382 After error-checking, the real work is done by the
383 gcc::jit::recording::context::new_location
384 method in jit-recording.c. */
386 gcc_jit_location *
387 gcc_jit_context_new_location (gcc_jit_context *ctxt,
388 const char *filename,
389 int line,
390 int column)
392 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
393 JIT_LOG_FUNC (ctxt->get_logger ());
394 return (gcc_jit_location *)ctxt->new_location (filename, line, column, true);
397 /* Public entrypoint. See description in libgccjit.h.
399 After error-checking, this calls the trivial
400 gcc::jit::recording::memento::as_object method (a location is a
401 memento), in jit-recording.h. */
403 gcc_jit_object *
404 gcc_jit_location_as_object (gcc_jit_location *loc)
406 RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
408 return static_cast <gcc_jit_object *> (loc->as_object ());
411 /* Public entrypoint. See description in libgccjit.h.
413 After error-checking, this calls the trivial
414 gcc::jit::recording::memento::as_object method (a type is a
415 memento), in jit-recording.h. */
417 gcc_jit_object *
418 gcc_jit_type_as_object (gcc_jit_type *type)
420 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
422 return static_cast <gcc_jit_object *> (type->as_object ());
425 /* Public entrypoint for getting a specific type from a context.
427 After error-checking, the real work is done by the
428 gcc::jit::recording::context::get_type method, in
429 jit-recording.c */
431 gcc_jit_type *
432 gcc_jit_context_get_type (gcc_jit_context *ctxt,
433 enum gcc_jit_types type)
435 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
436 JIT_LOG_FUNC (ctxt->get_logger ());
437 RETURN_NULL_IF_FAIL_PRINTF1 (
438 (type >= GCC_JIT_TYPE_VOID
439 && type <= GCC_JIT_TYPE_FILE_PTR),
440 ctxt, NULL,
441 "unrecognized value for enum gcc_jit_types: %i", type);
443 return (gcc_jit_type *)ctxt->get_type (type);
446 /* Public entrypoint for getting the integer type of the given size and
447 signedness.
449 After error-checking, the real work is done by the
450 gcc::jit::recording::context::get_int_type method,
451 in jit-recording.c. */
453 gcc_jit_type *
454 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
455 int num_bytes, int is_signed)
457 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
458 JIT_LOG_FUNC (ctxt->get_logger ());
459 RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
461 return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
464 /* Public entrypoint. See description in libgccjit.h.
466 After error-checking, the real work is done by the
467 gcc::jit::recording::type::get_pointer method, in
468 jit-recording.c */
470 gcc_jit_type *
471 gcc_jit_type_get_pointer (gcc_jit_type *type)
473 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
475 return (gcc_jit_type *)type->get_pointer ();
478 /* Public entrypoint. See description in libgccjit.h.
480 After error-checking, the real work is done by the
481 gcc::jit::recording::type::get_const method, in
482 jit-recording.c. */
484 gcc_jit_type *
485 gcc_jit_type_get_const (gcc_jit_type *type)
487 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
489 return (gcc_jit_type *)type->get_const ();
492 /* Public entrypoint. See description in libgccjit.h.
494 After error-checking, the real work is done by the
495 gcc::jit::recording::type::get_volatile method, in
496 jit-recording.c. */
498 gcc_jit_type *
499 gcc_jit_type_get_volatile (gcc_jit_type *type)
501 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
503 return (gcc_jit_type *)type->get_volatile ();
506 /* Public entrypoint. See description in libgccjit.h.
508 After error-checking, the real work is done by the
509 gcc::jit::recording::context::new_array_type method, in
510 jit-recording.c. */
512 gcc_jit_type *
513 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
514 gcc_jit_location *loc,
515 gcc_jit_type *element_type,
516 int num_elements)
518 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
519 JIT_LOG_FUNC (ctxt->get_logger ());
520 /* LOC can be NULL. */
521 RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
522 RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
524 return (gcc_jit_type *)ctxt->new_array_type (loc,
525 element_type,
526 num_elements);
529 /* Public entrypoint. See description in libgccjit.h.
531 After error-checking, the real work is done by the
532 gcc::jit::recording::context::new_field method, in
533 jit-recording.c. */
535 gcc_jit_field *
536 gcc_jit_context_new_field (gcc_jit_context *ctxt,
537 gcc_jit_location *loc,
538 gcc_jit_type *type,
539 const char *name)
541 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
542 JIT_LOG_FUNC (ctxt->get_logger ());
543 /* LOC can be NULL. */
544 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
545 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
547 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
550 /* Public entrypoint. See description in libgccjit.h.
552 After error-checking, this calls the trivial
553 gcc::jit::recording::memento::as_object method (a field is a
554 memento), in jit-recording.h. */
556 gcc_jit_object *
557 gcc_jit_field_as_object (gcc_jit_field *field)
559 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
561 return static_cast <gcc_jit_object *> (field->as_object ());
564 /* Public entrypoint. See description in libgccjit.h.
566 After error-checking, the real work is done by the
567 gcc::jit::recording::context::new_struct_type method,
568 immediately followed by a "set_fields" call on the resulting
569 gcc::jit::recording::compound_type *, both in jit-recording.c */
571 gcc_jit_struct *
572 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
573 gcc_jit_location *loc,
574 const char *name,
575 int num_fields,
576 gcc_jit_field **fields)
578 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
579 JIT_LOG_FUNC (ctxt->get_logger ());
580 /* LOC can be NULL. */
581 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
582 if (num_fields)
583 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
584 for (int i = 0; i < num_fields; i++)
586 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
587 RETURN_NULL_IF_FAIL_PRINTF2 (
588 NULL == fields[i]->get_container (),
589 ctxt, loc,
590 "%s is already a field of %s",
591 fields[i]->get_debug_string (),
592 fields[i]->get_container ()->get_debug_string ());
595 gcc::jit::recording::struct_ *result =
596 ctxt->new_struct_type (loc, name);
597 result->set_fields (loc,
598 num_fields,
599 (gcc::jit::recording::field **)fields);
600 return static_cast<gcc_jit_struct *> (result);
603 /* Public entrypoint. See description in libgccjit.h.
605 After error-checking, the real work is done by the
606 gcc::jit::recording::context::new_struct_type method in
607 jit-recording.c. */
609 gcc_jit_struct *
610 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
611 gcc_jit_location *loc,
612 const char *name)
614 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
615 JIT_LOG_FUNC (ctxt->get_logger ());
616 /* LOC can be NULL. */
617 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
619 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
622 /* Public entrypoint. See description in libgccjit.h.
624 After error-checking, this calls the trivial
625 gcc::jit::recording::struct_::as_object method in
626 jit-recording.h. */
628 gcc_jit_type *
629 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
631 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
633 return static_cast <gcc_jit_type *> (struct_type->as_type ());
636 /* Public entrypoint. See description in libgccjit.h.
638 After error-checking, the real work is done by the
639 gcc::jit::recording::compound_type::set_fields method in
640 jit-recording.c. */
642 void
643 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
644 gcc_jit_location *loc,
645 int num_fields,
646 gcc_jit_field **fields)
648 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
649 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
650 JIT_LOG_FUNC (ctxt->get_logger ());
651 /* LOC can be NULL. */
652 RETURN_IF_FAIL_PRINTF1 (
653 NULL == struct_type->get_fields (), ctxt, loc,
654 "%s already has had fields set",
655 struct_type->get_debug_string ());
656 if (num_fields)
657 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
658 for (int i = 0; i < num_fields; i++)
660 RETURN_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
661 RETURN_IF_FAIL_PRINTF2 (
662 NULL == fields[i]->get_container (),
663 ctxt, loc,
664 "%s is already a field of %s",
665 fields[i]->get_debug_string (),
666 fields[i]->get_container ()->get_debug_string ());
669 struct_type->set_fields (loc, num_fields,
670 (gcc::jit::recording::field **)fields);
673 /* Public entrypoint. See description in libgccjit.h.
675 After error-checking, the real work is done by the
676 gcc::jit::recording::context::new_union_type method,
677 immediately followed by a "set_fields" call on the resulting
678 gcc::jit::recording::compound_type *, both in jit-recording.c */
680 gcc_jit_type *
681 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
682 gcc_jit_location *loc,
683 const char *name,
684 int num_fields,
685 gcc_jit_field **fields)
687 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
688 JIT_LOG_FUNC (ctxt->get_logger ());
689 /* LOC can be NULL. */
690 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
691 if (num_fields)
692 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
693 for (int i = 0; i < num_fields; i++)
695 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
696 RETURN_NULL_IF_FAIL_PRINTF2 (
697 NULL == fields[i]->get_container (),
698 ctxt, loc,
699 "%s is already a field of %s",
700 fields[i]->get_debug_string (),
701 fields[i]->get_container ()->get_debug_string ());
704 gcc::jit::recording::union_ *result =
705 ctxt->new_union_type (loc, name);
706 result->set_fields (loc,
707 num_fields,
708 (gcc::jit::recording::field **)fields);
709 return (gcc_jit_type *) (result);
712 /* Public entrypoint. See description in libgccjit.h.
714 After error-checking, the real work is done by the
715 gcc::jit::recording::context::new_function_ptr_type method,
716 in jit-recording.c */
718 gcc_jit_type *
719 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
720 gcc_jit_location *loc,
721 gcc_jit_type *return_type,
722 int num_params,
723 gcc_jit_type **param_types,
724 int is_variadic)
726 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
727 JIT_LOG_FUNC (ctxt->get_logger ());
728 /* LOC can be NULL. */
729 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
730 RETURN_NULL_IF_FAIL (
731 (num_params == 0) || param_types,
732 ctxt, loc,
733 "NULL param_types creating function pointer type");
734 for (int i = 0; i < num_params; i++)
735 RETURN_NULL_IF_FAIL_PRINTF1 (
736 param_types[i],
737 ctxt, loc,
738 "NULL parameter type %i creating function pointer type", i);
740 return (gcc_jit_type*)
741 ctxt->new_function_ptr_type (loc, return_type,
742 num_params,
743 (gcc::jit::recording::type **)param_types,
744 is_variadic);
747 /* Constructing functions. */
749 /* Public entrypoint. See description in libgccjit.h.
751 After error-checking, the real work is done by the
752 gcc::jit::recording::context::new_param method, in jit-recording.c */
754 gcc_jit_param *
755 gcc_jit_context_new_param (gcc_jit_context *ctxt,
756 gcc_jit_location *loc,
757 gcc_jit_type *type,
758 const char *name)
760 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
761 JIT_LOG_FUNC (ctxt->get_logger ());
762 /* LOC can be NULL. */
763 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
764 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
766 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
769 /* Public entrypoint. See description in libgccjit.h.
771 After error-checking, this calls the trivial
772 gcc::jit::recording::memento::as_object method (a param is a memento),
773 in jit-recording.h. */
775 gcc_jit_object *
776 gcc_jit_param_as_object (gcc_jit_param *param)
778 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
780 return static_cast <gcc_jit_object *> (param->as_object ());
783 /* Public entrypoint. See description in libgccjit.h.
785 After error-checking, this calls the trivial
786 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
788 gcc_jit_lvalue *
789 gcc_jit_param_as_lvalue (gcc_jit_param *param)
791 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
793 return (gcc_jit_lvalue *)param->as_lvalue ();
796 /* Public entrypoint. See description in libgccjit.h.
798 After error-checking, this calls the trivial
799 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
800 in jit-recording.h. */
802 gcc_jit_rvalue *
803 gcc_jit_param_as_rvalue (gcc_jit_param *param)
805 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
807 return (gcc_jit_rvalue *)param->as_rvalue ();
810 /* Public entrypoint. See description in libgccjit.h.
812 After error-checking, the real work is done by the
813 gcc::jit::recording::context::new_function method, in
814 jit-recording.c. */
816 gcc_jit_function *
817 gcc_jit_context_new_function (gcc_jit_context *ctxt,
818 gcc_jit_location *loc,
819 enum gcc_jit_function_kind kind,
820 gcc_jit_type *return_type,
821 const char *name,
822 int num_params,
823 gcc_jit_param **params,
824 int is_variadic)
826 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
827 JIT_LOG_FUNC (ctxt->get_logger ());
828 /* LOC can be NULL. */
829 RETURN_NULL_IF_FAIL_PRINTF1 (
830 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
831 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
832 ctxt, loc,
833 "unrecognized value for enum gcc_jit_function_kind: %i",
834 kind);
835 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
836 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
837 /* The assembler can only handle certain names, so for now, enforce
838 C's rules for identiers upon the name, using ISALPHA and ISALNUM
839 from safe-ctype.h to ignore the current locale.
840 Eventually we'll need some way to interact with e.g. C++ name
841 mangling. */
843 /* Leading char: */
844 char ch = *name;
845 RETURN_NULL_IF_FAIL_PRINTF2 (
846 ISALPHA (ch) || ch == '_',
847 ctxt, loc,
848 "name \"%s\" contains invalid character: '%c'",
849 name, ch);
850 /* Subsequent chars: */
851 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
853 RETURN_NULL_IF_FAIL_PRINTF2 (
854 ISALNUM (ch) || ch == '_',
855 ctxt, loc,
856 "name \"%s\" contains invalid character: '%c'",
857 name, ch);
860 RETURN_NULL_IF_FAIL_PRINTF1 (
861 (num_params == 0) || params,
862 ctxt, loc,
863 "NULL params creating function %s", name);
864 for (int i = 0; i < num_params; i++)
866 RETURN_NULL_IF_FAIL_PRINTF2 (
867 params[i],
868 ctxt, loc,
869 "NULL parameter %i creating function %s", i, name);
870 RETURN_NULL_IF_FAIL_PRINTF5 (
871 (NULL == params[i]->get_scope ()),
872 ctxt, loc,
873 "parameter %i \"%s\""
874 " (type: %s)"
875 " for function %s"
876 " was already used for function %s",
877 i, params[i]->get_debug_string (),
878 params[i]->get_type ()->get_debug_string (),
879 name,
880 params[i]->get_scope ()->get_debug_string ());
883 return (gcc_jit_function*)
884 ctxt->new_function (loc, kind, return_type, name,
885 num_params,
886 (gcc::jit::recording::param **)params,
887 is_variadic,
888 BUILT_IN_NONE);
891 /* Public entrypoint. See description in libgccjit.h.
893 After error-checking, the real work is done by the
894 gcc::jit::recording::context::get_builtin_function method, in
895 jit-recording.c. */
897 gcc_jit_function *
898 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
899 const char *name)
901 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
902 JIT_LOG_FUNC (ctxt->get_logger ());
903 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
905 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
908 /* Public entrypoint. See description in libgccjit.h.
910 After error-checking, this calls the trivial
911 gcc::jit::recording::memento::as_object method (a function is a
912 memento), in jit-recording.h. */
914 gcc_jit_object *
915 gcc_jit_function_as_object (gcc_jit_function *func)
917 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
919 return static_cast <gcc_jit_object *> (func->as_object ());
922 /* Public entrypoint. See description in libgccjit.h.
924 After error-checking, the real work is done by the
925 gcc::jit::recording::function::get_param method, in
926 jit-recording.h. */
928 gcc_jit_param *
929 gcc_jit_function_get_param (gcc_jit_function *func, int index)
931 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
932 gcc::jit::recording::context *ctxt = func->m_ctxt;
933 JIT_LOG_FUNC (ctxt->get_logger ());
934 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
935 int num_params = func->get_params ().length ();
936 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
937 ctxt, NULL,
938 "index of %d is too large (%s has %d params)",
939 index,
940 func->get_debug_string (),
941 num_params);
943 return static_cast <gcc_jit_param *> (func->get_param (index));
946 /* Public entrypoint. See description in libgccjit.h.
948 After error-checking, the real work is done by the
949 gcc::jit::recording::function::dump_to_dot method, in
950 jit-recording.c. */
952 void
953 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
954 const char *path)
956 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
957 gcc::jit::recording::context *ctxt = func->m_ctxt;
958 JIT_LOG_FUNC (ctxt->get_logger ());
959 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
961 func->dump_to_dot (path);
964 /* Public entrypoint. See description in libgccjit.h.
966 After error-checking, the real work is done by the
967 gcc::jit::recording::function::new_block method, in
968 jit-recording.c. */
970 gcc_jit_block*
971 gcc_jit_function_new_block (gcc_jit_function *func,
972 const char *name)
974 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
975 JIT_LOG_FUNC (func->get_context ()->get_logger ());
976 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
977 func->get_context (), NULL,
978 "cannot add block to an imported function");
979 /* name can be NULL. */
981 return (gcc_jit_block *)func->new_block (name);
984 /* Public entrypoint. See description in libgccjit.h.
986 After error-checking, this calls the trivial
987 gcc::jit::recording::memento::as_object method (a block is a
988 memento), in jit-recording.h. */
990 gcc_jit_object *
991 gcc_jit_block_as_object (gcc_jit_block *block)
993 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
995 return static_cast <gcc_jit_object *> (block->as_object ());
998 /* Public entrypoint. See description in libgccjit.h.
1000 After error-checking, the real work is done by the
1001 gcc::jit::recording::block::get_function method, in
1002 jit-recording.h. */
1004 gcc_jit_function *
1005 gcc_jit_block_get_function (gcc_jit_block *block)
1007 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1009 return static_cast <gcc_jit_function *> (block->get_function ());
1012 /* Public entrypoint. See description in libgccjit.h.
1014 After error-checking, the real work is done by the
1015 gcc::jit::recording::context::new_global method, in
1016 jit-recording.c. */
1018 gcc_jit_lvalue *
1019 gcc_jit_context_new_global (gcc_jit_context *ctxt,
1020 gcc_jit_location *loc,
1021 enum gcc_jit_global_kind kind,
1022 gcc_jit_type *type,
1023 const char *name)
1025 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1026 JIT_LOG_FUNC (ctxt->get_logger ());
1027 /* LOC can be NULL. */
1028 RETURN_NULL_IF_FAIL_PRINTF1 (
1029 ((kind >= GCC_JIT_GLOBAL_EXPORTED)
1030 && (kind <= GCC_JIT_GLOBAL_IMPORTED)),
1031 ctxt, loc,
1032 "unrecognized value for enum gcc_jit_global_kind: %i",
1033 kind);
1034 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1035 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1037 return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
1040 /* Public entrypoint. See description in libgccjit.h.
1042 After error-checking, this calls the trivial
1043 gcc::jit::recording::memento::as_object method (an lvalue is a
1044 memento), in jit-recording.h. */
1046 gcc_jit_object *
1047 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1049 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1051 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1054 /* Public entrypoint. See description in libgccjit.h.
1056 After error-checking, this calls the trivial
1057 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1059 gcc_jit_rvalue *
1060 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1062 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1064 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1067 /* Public entrypoint. See description in libgccjit.h.
1069 After error-checking, this calls the trivial
1070 gcc::jit::recording::memento::as_object method (an rvalue is a
1071 memento), in jit-recording.h. */
1073 gcc_jit_object *
1074 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1076 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1078 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1081 /* Public entrypoint. See description in libgccjit.h.
1083 After error-checking, the real work is done by the
1084 gcc::jit::recording::rvalue::get_type method, in
1085 jit-recording.h. */
1087 gcc_jit_type *
1088 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1090 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1092 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1095 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1096 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1097 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1099 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1100 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1101 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1102 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1103 "not a numeric type: %s", \
1104 NUMERIC_TYPE->get_debug_string ());
1106 /* Public entrypoint. See description in libgccjit.h.
1108 After error-checking, the real work is done by the
1109 gcc::jit::recording::context::new_rvalue_from_int method in
1110 jit-recording.c. */
1112 gcc_jit_rvalue *
1113 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1114 gcc_jit_type *numeric_type,
1115 int value)
1117 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1118 JIT_LOG_FUNC (ctxt->get_logger ());
1119 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1121 return ((gcc_jit_rvalue *)ctxt
1122 ->new_rvalue_from_const <int> (numeric_type, value));
1125 /* FIXME. */
1127 gcc_jit_rvalue *
1128 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1129 gcc_jit_type *numeric_type,
1130 long value)
1132 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1133 JIT_LOG_FUNC (ctxt->get_logger ());
1134 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1136 return ((gcc_jit_rvalue *)ctxt
1137 ->new_rvalue_from_const <long> (numeric_type, value));
1140 /* Public entrypoint. See description in libgccjit.h.
1142 This is essentially equivalent to:
1143 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1144 albeit with slightly different error messages if an error occurs. */
1146 gcc_jit_rvalue *
1147 gcc_jit_context_zero (gcc_jit_context *ctxt,
1148 gcc_jit_type *numeric_type)
1150 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1151 JIT_LOG_FUNC (ctxt->get_logger ());
1152 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1154 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1157 /* Public entrypoint. See description in libgccjit.h.
1159 This is essentially equivalent to:
1160 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1161 albeit with slightly different error messages if an error occurs. */
1163 gcc_jit_rvalue *
1164 gcc_jit_context_one (gcc_jit_context *ctxt,
1165 gcc_jit_type *numeric_type)
1167 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1168 JIT_LOG_FUNC (ctxt->get_logger ());
1169 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1171 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1174 /* Public entrypoint. See description in libgccjit.h.
1176 After error-checking, the real work is done by the
1177 gcc::jit::recording::context::new_rvalue_from_double method in
1178 jit-recording.c. */
1180 gcc_jit_rvalue *
1181 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1182 gcc_jit_type *numeric_type,
1183 double value)
1185 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1186 JIT_LOG_FUNC (ctxt->get_logger ());
1187 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1189 return ((gcc_jit_rvalue *)ctxt
1190 ->new_rvalue_from_const <double> (numeric_type, value));
1193 /* Public entrypoint. See description in libgccjit.h.
1195 After error-checking, the real work is done by the
1196 gcc::jit::recording::context::new_rvalue_from_ptr method in
1197 jit-recording.c. */
1199 gcc_jit_rvalue *
1200 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1201 gcc_jit_type *pointer_type,
1202 void *value)
1204 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1205 JIT_LOG_FUNC (ctxt->get_logger ());
1206 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1207 RETURN_NULL_IF_FAIL_PRINTF1 (
1208 pointer_type->is_pointer (),
1209 ctxt, NULL,
1210 "not a pointer type (type: %s)",
1211 pointer_type->get_debug_string ());
1213 return ((gcc_jit_rvalue *)ctxt
1214 ->new_rvalue_from_const <void *> (pointer_type, value));
1217 /* Public entrypoint. See description in libgccjit.h.
1219 This is essentially equivalent to:
1220 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1221 albeit with slightly different error messages if an error occurs. */
1223 gcc_jit_rvalue *
1224 gcc_jit_context_null (gcc_jit_context *ctxt,
1225 gcc_jit_type *pointer_type)
1227 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1228 JIT_LOG_FUNC (ctxt->get_logger ());
1229 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1230 RETURN_NULL_IF_FAIL_PRINTF1 (
1231 pointer_type->is_pointer (),
1232 ctxt, NULL,
1233 "not a pointer type (type: %s)",
1234 pointer_type->get_debug_string ());
1236 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1239 /* Public entrypoint. See description in libgccjit.h.
1241 After error-checking, the real work is done by the
1242 gcc::jit::recording::context::new_string_literal method in
1243 jit-recording.c. */
1245 gcc_jit_rvalue *
1246 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1247 const char *value)
1249 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1250 JIT_LOG_FUNC (ctxt->get_logger ());
1251 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1253 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1256 /* Public entrypoint. See description in libgccjit.h.
1258 After error-checking, the real work is done by the
1259 gcc::jit::recording::context::new_unary_op method in
1260 jit-recording.c. */
1262 gcc_jit_rvalue *
1263 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1264 gcc_jit_location *loc,
1265 enum gcc_jit_unary_op op,
1266 gcc_jit_type *result_type,
1267 gcc_jit_rvalue *rvalue)
1269 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1270 JIT_LOG_FUNC (ctxt->get_logger ());
1271 /* LOC can be NULL. */
1272 RETURN_NULL_IF_FAIL_PRINTF1 (
1273 (op >= GCC_JIT_UNARY_OP_MINUS
1274 && op <= GCC_JIT_UNARY_OP_ABS),
1275 ctxt, loc,
1276 "unrecognized value for enum gcc_jit_unary_op: %i",
1277 op);
1278 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1279 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1281 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1284 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1285 For use by both gcc_jit_context_new_binary_op and
1286 gcc_jit_block_add_assignment_op. */
1288 static bool
1289 valid_binary_op_p (enum gcc_jit_binary_op op)
1291 return (op >= GCC_JIT_BINARY_OP_PLUS
1292 && op <= GCC_JIT_BINARY_OP_RSHIFT);
1295 /* Public entrypoint. See description in libgccjit.h.
1297 After error-checking, the real work is done by the
1298 gcc::jit::recording::context::new_binary_op method in
1299 jit-recording.c. */
1301 gcc_jit_rvalue *
1302 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1303 gcc_jit_location *loc,
1304 enum gcc_jit_binary_op op,
1305 gcc_jit_type *result_type,
1306 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1308 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1309 JIT_LOG_FUNC (ctxt->get_logger ());
1310 /* LOC can be NULL. */
1311 RETURN_NULL_IF_FAIL_PRINTF1 (
1312 valid_binary_op_p (op),
1313 ctxt, loc,
1314 "unrecognized value for enum gcc_jit_binary_op: %i",
1315 op);
1316 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1317 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1318 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1319 RETURN_NULL_IF_FAIL_PRINTF4 (
1320 a->get_type () == b->get_type (),
1321 ctxt, loc,
1322 "mismatching types for binary op:"
1323 " a: %s (type: %s) b: %s (type: %s)",
1324 a->get_debug_string (),
1325 a->get_type ()->get_debug_string (),
1326 b->get_debug_string (),
1327 b->get_type ()->get_debug_string ());
1329 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1332 /* Public entrypoint. See description in libgccjit.h.
1334 After error-checking, the real work is done by the
1335 gcc::jit::recording::context::new_comparison method in
1336 jit-recording.c. */
1338 gcc_jit_rvalue *
1339 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1340 gcc_jit_location *loc,
1341 enum gcc_jit_comparison op,
1342 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1344 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1345 JIT_LOG_FUNC (ctxt->get_logger ());
1346 /* LOC can be NULL. */
1347 RETURN_NULL_IF_FAIL_PRINTF1 (
1348 (op >= GCC_JIT_COMPARISON_EQ
1349 && op <= GCC_JIT_COMPARISON_GE),
1350 ctxt, loc,
1351 "unrecognized value for enum gcc_jit_comparison: %i",
1352 op);
1353 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1354 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1355 RETURN_NULL_IF_FAIL_PRINTF4 (
1356 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1357 ctxt, loc,
1358 "mismatching types for comparison:"
1359 " a: %s (type: %s) b: %s (type: %s)",
1360 a->get_debug_string (),
1361 a->get_type ()->get_debug_string (),
1362 b->get_debug_string (),
1363 b->get_type ()->get_debug_string ());
1365 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1368 /* Public entrypoint. See description in libgccjit.h.
1370 After error-checking, the real work is done by the
1371 gcc::jit::recording::context::new_call method in
1372 jit-recording.c. */
1374 gcc_jit_rvalue *
1375 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1376 gcc_jit_location *loc,
1377 gcc_jit_function *func,
1378 int numargs , gcc_jit_rvalue **args)
1380 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1381 JIT_LOG_FUNC (ctxt->get_logger ());
1382 /* LOC can be NULL. */
1383 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1384 if (numargs)
1385 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1387 int min_num_params = func->get_params ().length ();
1388 bool is_variadic = func->is_variadic ();
1390 RETURN_NULL_IF_FAIL_PRINTF3 (
1391 numargs >= min_num_params,
1392 ctxt, loc,
1393 "not enough arguments to function \"%s\""
1394 " (got %i args, expected %i)",
1395 func->get_name ()->c_str (),
1396 numargs, min_num_params);
1398 RETURN_NULL_IF_FAIL_PRINTF3 (
1399 (numargs == min_num_params || is_variadic),
1400 ctxt, loc,
1401 "too many arguments to function \"%s\""
1402 " (got %i args, expected %i)",
1403 func->get_name ()->c_str (),
1404 numargs, min_num_params);
1406 for (int i = 0; i < min_num_params; i++)
1408 gcc::jit::recording::param *param = func->get_param (i);
1409 gcc_jit_rvalue *arg = args[i];
1411 RETURN_NULL_IF_FAIL_PRINTF4 (
1412 arg,
1413 ctxt, loc,
1414 "NULL argument %i to function \"%s\":"
1415 " param %s (type: %s)",
1416 i + 1,
1417 func->get_name ()->c_str (),
1418 param->get_debug_string (),
1419 param->get_type ()->get_debug_string ());
1421 RETURN_NULL_IF_FAIL_PRINTF6 (
1422 compatible_types (param->get_type (),
1423 arg->get_type ()),
1424 ctxt, loc,
1425 "mismatching types for argument %d of function \"%s\":"
1426 " assignment to param %s (type: %s) from %s (type: %s)",
1427 i + 1,
1428 func->get_name ()->c_str (),
1429 param->get_debug_string (),
1430 param->get_type ()->get_debug_string (),
1431 arg->get_debug_string (),
1432 arg->get_type ()->get_debug_string ());
1435 return (gcc_jit_rvalue *)ctxt->new_call (loc,
1436 func,
1437 numargs,
1438 (gcc::jit::recording::rvalue **)args);
1441 /* Public entrypoint. See description in libgccjit.h.
1443 After error-checking, the real work is done by the
1444 gcc::jit::recording::context::new_call_through_ptr method in
1445 jit-recording.c. */
1447 gcc_jit_rvalue *
1448 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1449 gcc_jit_location *loc,
1450 gcc_jit_rvalue *fn_ptr,
1451 int numargs, gcc_jit_rvalue **args)
1453 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1454 JIT_LOG_FUNC (ctxt->get_logger ());
1455 /* LOC can be NULL. */
1456 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1457 if (numargs)
1458 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1460 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1461 RETURN_NULL_IF_FAIL_PRINTF2 (
1462 ptr_type, ctxt, loc,
1463 "fn_ptr is not a ptr: %s"
1464 " type: %s",
1465 fn_ptr->get_debug_string (),
1466 fn_ptr->get_type ()->get_debug_string ());
1468 gcc::jit::recording::function_type *fn_type =
1469 ptr_type->dyn_cast_function_type();
1470 RETURN_NULL_IF_FAIL_PRINTF2 (
1471 fn_type, ctxt, loc,
1472 "fn_ptr is not a function ptr: %s"
1473 " type: %s",
1474 fn_ptr->get_debug_string (),
1475 fn_ptr->get_type ()->get_debug_string ());
1477 int min_num_params = fn_type->get_param_types ().length ();
1478 bool is_variadic = fn_type->is_variadic ();
1480 RETURN_NULL_IF_FAIL_PRINTF3 (
1481 numargs >= min_num_params,
1482 ctxt, loc,
1483 "not enough arguments to fn_ptr: %s"
1484 " (got %i args, expected %i)",
1485 fn_ptr->get_debug_string (),
1486 numargs, min_num_params);
1488 RETURN_NULL_IF_FAIL_PRINTF3 (
1489 (numargs == min_num_params || is_variadic),
1490 ctxt, loc,
1491 "too many arguments to fn_ptr: %s"
1492 " (got %i args, expected %i)",
1493 fn_ptr->get_debug_string (),
1494 numargs, min_num_params);
1496 for (int i = 0; i < min_num_params; i++)
1498 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1499 gcc_jit_rvalue *arg = args[i];
1501 RETURN_NULL_IF_FAIL_PRINTF3 (
1502 arg,
1503 ctxt, loc,
1504 "NULL argument %i to fn_ptr: %s"
1505 " (type: %s)",
1506 i + 1,
1507 fn_ptr->get_debug_string (),
1508 param_type->get_debug_string ());
1510 RETURN_NULL_IF_FAIL_PRINTF6 (
1511 compatible_types (param_type,
1512 arg->get_type ()),
1513 ctxt, loc,
1514 "mismatching types for argument %d of fn_ptr: %s:"
1515 " assignment to param %d (type: %s) from %s (type: %s)",
1516 i + 1,
1517 fn_ptr->get_debug_string (),
1518 i + 1,
1519 param_type->get_debug_string (),
1520 arg->get_debug_string (),
1521 arg->get_type ()->get_debug_string ());
1524 return (gcc_jit_rvalue *)(
1525 ctxt->new_call_through_ptr (loc,
1526 fn_ptr,
1527 numargs,
1528 (gcc::jit::recording::rvalue **)args));
1531 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1532 to DST_TYPE, for use by gcc_jit_context_new_cast.
1534 We only permit these kinds of cast:
1536 int <-> float
1537 int <-> bool
1538 P* <-> Q* for pointer types P and Q. */
1540 static bool
1541 is_valid_cast (gcc::jit::recording::type *src_type,
1542 gcc_jit_type *dst_type)
1544 bool src_is_int = src_type->is_int ();
1545 bool dst_is_int = dst_type->is_int ();
1546 bool src_is_float = src_type->is_float ();
1547 bool dst_is_float = dst_type->is_float ();
1548 bool src_is_bool = src_type->is_bool ();
1549 bool dst_is_bool = dst_type->is_bool ();
1551 if (src_is_int)
1552 if (dst_is_int || dst_is_float || dst_is_bool)
1553 return true;
1555 if (src_is_float)
1556 if (dst_is_int || dst_is_float)
1557 return true;
1559 if (src_is_bool)
1560 if (dst_is_int || dst_is_bool)
1561 return true;
1563 /* Permit casts between pointer types. */
1564 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1565 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1566 if (deref_src_type && deref_dst_type)
1567 return true;
1569 return false;
1572 /* Public entrypoint. See description in libgccjit.h.
1574 After error-checking, the real work is done by the
1575 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1577 gcc_jit_rvalue *
1578 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1579 gcc_jit_location *loc,
1580 gcc_jit_rvalue *rvalue,
1581 gcc_jit_type *type)
1583 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1584 JIT_LOG_FUNC (ctxt->get_logger ());
1585 /* LOC can be NULL. */
1586 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1587 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1588 RETURN_NULL_IF_FAIL_PRINTF3 (
1589 is_valid_cast (rvalue->get_type (), type),
1590 ctxt, loc,
1591 "cannot cast %s from type: %s to type: %s",
1592 rvalue->get_debug_string (),
1593 rvalue->get_type ()->get_debug_string (),
1594 type->get_debug_string ());
1596 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1599 /* Public entrypoint. See description in libgccjit.h.
1601 After error-checking, the real work is done by the
1602 gcc::jit::recording::context::new_array_access method in
1603 jit-recording.c. */
1605 extern gcc_jit_lvalue *
1606 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1607 gcc_jit_location *loc,
1608 gcc_jit_rvalue *ptr,
1609 gcc_jit_rvalue *index)
1611 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1612 JIT_LOG_FUNC (ctxt->get_logger ());
1613 /* LOC can be NULL. */
1614 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1615 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1616 RETURN_NULL_IF_FAIL_PRINTF2 (
1617 ptr->get_type ()->dereference (),
1618 ctxt, loc,
1619 "ptr: %s (type: %s) is not a pointer or array",
1620 ptr->get_debug_string (),
1621 ptr->get_type ()->get_debug_string ());
1622 RETURN_NULL_IF_FAIL_PRINTF2 (
1623 index->get_type ()->is_numeric (),
1624 ctxt, loc,
1625 "index: %s (type: %s) is not of numeric type",
1626 index->get_debug_string (),
1627 index->get_type ()->get_debug_string ());
1629 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1632 /* Public entrypoint. See description in libgccjit.h.
1634 After error-checking, the real work is done by the
1635 gcc::jit::recording::memento::get_context method in
1636 jit-recording.h. */
1638 gcc_jit_context *
1639 gcc_jit_object_get_context (gcc_jit_object *obj)
1641 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1643 return static_cast <gcc_jit_context *> (obj->get_context ());
1646 /* Public entrypoint. See description in libgccjit.h.
1648 After error-checking, the real work is done by the
1649 gcc::jit::recording::memento::get_debug_string method in
1650 jit-recording.c. */
1652 const char *
1653 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1655 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1657 return obj->get_debug_string ();
1660 /* Public entrypoint. See description in libgccjit.h.
1662 After error-checking, the real work is done by the
1663 gcc::jit::recording::lvalue::access_field method in
1664 jit-recording.c. */
1666 gcc_jit_lvalue *
1667 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1668 gcc_jit_location *loc,
1669 gcc_jit_field *field)
1671 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1672 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1673 JIT_LOG_FUNC (ctxt->get_logger ());
1674 /* LOC can be NULL. */
1675 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1676 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1677 "field %s has not been placed in a struct",
1678 field->get_debug_string ());
1679 gcc::jit::recording::type *underlying_type =
1680 struct_->get_type ();
1681 RETURN_NULL_IF_FAIL_PRINTF2 (
1682 (field->get_container ()->unqualified ()
1683 == underlying_type->unqualified ()),
1684 struct_->m_ctxt, loc,
1685 "%s is not a field of %s",
1686 field->get_debug_string (),
1687 underlying_type->get_debug_string ());
1689 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1692 /* Public entrypoint. See description in libgccjit.h.
1694 After error-checking, the real work is done by the
1695 gcc::jit::recording::rvalue::access_field method in
1696 jit-recording.c. */
1698 gcc_jit_rvalue *
1699 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1700 gcc_jit_location *loc,
1701 gcc_jit_field *field)
1703 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1704 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1705 JIT_LOG_FUNC (ctxt->get_logger ());
1706 /* LOC can be NULL. */
1707 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1708 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1709 "field %s has not been placed in a struct",
1710 field->get_debug_string ());
1711 gcc::jit::recording::type *underlying_type =
1712 struct_->get_type ();
1713 RETURN_NULL_IF_FAIL_PRINTF2 (
1714 (field->get_container ()->unqualified ()
1715 == underlying_type->unqualified ()),
1716 struct_->m_ctxt, loc,
1717 "%s is not a field of %s",
1718 field->get_debug_string (),
1719 underlying_type->get_debug_string ());
1721 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1724 /* Public entrypoint. See description in libgccjit.h.
1726 After error-checking, the real work is done by the
1727 gcc::jit::recording::rvalue::deference_field method in
1728 jit-recording.c. */
1730 gcc_jit_lvalue *
1731 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1732 gcc_jit_location *loc,
1733 gcc_jit_field *field)
1735 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1736 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1737 /* LOC can be NULL. */
1738 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1739 gcc::jit::recording::type *underlying_type =
1740 ptr->get_type ()->is_pointer ();
1741 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1742 "field %s has not been placed in a struct",
1743 field->get_debug_string ());
1744 RETURN_NULL_IF_FAIL_PRINTF3 (
1745 underlying_type,
1746 ptr->m_ctxt, loc,
1747 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1748 ptr->get_debug_string (),
1749 ptr->get_type ()->get_debug_string (),
1750 field->get_debug_string ());
1751 RETURN_NULL_IF_FAIL_PRINTF2 (
1752 (field->get_container ()->unqualified ()
1753 == underlying_type->unqualified ()),
1754 ptr->m_ctxt, loc,
1755 "%s is not a field of %s",
1756 field->get_debug_string (),
1757 underlying_type->get_debug_string ());
1759 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1762 /* Public entrypoint. See description in libgccjit.h.
1764 After error-checking, the real work is done by the
1765 gcc::jit::recording::rvalue::deference method in
1766 jit-recording.c. */
1768 gcc_jit_lvalue *
1769 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1770 gcc_jit_location *loc)
1772 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1773 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1774 /* LOC can be NULL. */
1776 gcc::jit::recording::type *underlying_type =
1777 rvalue->get_type ()->is_pointer ();
1779 RETURN_NULL_IF_FAIL_PRINTF2 (
1780 underlying_type,
1781 rvalue->m_ctxt, loc,
1782 "dereference of non-pointer %s (type: %s)",
1783 rvalue->get_debug_string (),
1784 rvalue->get_type ()->get_debug_string ());
1786 RETURN_NULL_IF_FAIL_PRINTF2 (
1787 !underlying_type->is_void (),
1788 rvalue->m_ctxt, loc,
1789 "dereference of void pointer %s (type: %s)",
1790 rvalue->get_debug_string (),
1791 rvalue->get_type ()->get_debug_string ());
1793 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1796 /* Public entrypoint. See description in libgccjit.h.
1798 After error-checking, the real work is done by the
1799 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1801 gcc_jit_rvalue *
1802 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1803 gcc_jit_location *loc)
1805 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1806 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1807 /* LOC can be NULL. */
1809 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1812 /* Public entrypoint. See description in libgccjit.h.
1814 After error-checking, the real work is done by the
1815 gcc::jit::recording::function::new_local method in jit-recording.c. */
1817 gcc_jit_lvalue *
1818 gcc_jit_function_new_local (gcc_jit_function *func,
1819 gcc_jit_location *loc,
1820 gcc_jit_type *type,
1821 const char *name)
1823 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1824 gcc::jit::recording::context *ctxt = func->m_ctxt;
1825 JIT_LOG_FUNC (ctxt->get_logger ());
1826 /* LOC can be NULL. */
1827 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1828 ctxt, loc,
1829 "Cannot add locals to an imported function");
1830 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1831 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1833 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1836 /* Public entrypoint. See description in libgccjit.h.
1838 After error-checking, the real work is done by the
1839 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1841 void
1842 gcc_jit_block_add_eval (gcc_jit_block *block,
1843 gcc_jit_location *loc,
1844 gcc_jit_rvalue *rvalue)
1846 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1847 gcc::jit::recording::context *ctxt = block->get_context ();
1848 JIT_LOG_FUNC (ctxt->get_logger ());
1849 /* LOC can be NULL. */
1850 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1852 gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
1854 /* "stmt" should be good enough to be usable in error-messages,
1855 but might still not be compilable; perform some more
1856 error-checking here. We do this here so that the error messages
1857 can contain a stringified version of "stmt", whilst appearing
1858 as close as possible to the point of failure. */
1859 rvalue->verify_valid_within_stmt (__func__, stmt);
1862 /* Public entrypoint. See description in libgccjit.h.
1864 After error-checking, the real work is done by the
1865 gcc::jit::recording::block::add_assignment method in
1866 jit-recording.c. */
1868 void
1869 gcc_jit_block_add_assignment (gcc_jit_block *block,
1870 gcc_jit_location *loc,
1871 gcc_jit_lvalue *lvalue,
1872 gcc_jit_rvalue *rvalue)
1874 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1875 gcc::jit::recording::context *ctxt = block->get_context ();
1876 JIT_LOG_FUNC (ctxt->get_logger ());
1877 /* LOC can be NULL. */
1878 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1879 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1880 RETURN_IF_FAIL_PRINTF4 (
1881 compatible_types (lvalue->get_type (),
1882 rvalue->get_type ()),
1883 ctxt, loc,
1884 "mismatching types:"
1885 " assignment to %s (type: %s) from %s (type: %s)",
1886 lvalue->get_debug_string (),
1887 lvalue->get_type ()->get_debug_string (),
1888 rvalue->get_debug_string (),
1889 rvalue->get_type ()->get_debug_string ());
1891 gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
1893 /* "stmt" should be good enough to be usable in error-messages,
1894 but might still not be compilable; perform some more
1895 error-checking here. We do this here so that the error messages
1896 can contain a stringified version of "stmt", whilst appearing
1897 as close as possible to the point of failure. */
1898 lvalue->verify_valid_within_stmt (__func__, stmt);
1899 rvalue->verify_valid_within_stmt (__func__, stmt);
1902 /* Public entrypoint. See description in libgccjit.h.
1904 After error-checking, the real work is done by the
1905 gcc::jit::recording::block::add_assignment_op method in
1906 jit-recording.c. */
1908 void
1909 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1910 gcc_jit_location *loc,
1911 gcc_jit_lvalue *lvalue,
1912 enum gcc_jit_binary_op op,
1913 gcc_jit_rvalue *rvalue)
1915 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1916 gcc::jit::recording::context *ctxt = block->get_context ();
1917 JIT_LOG_FUNC (ctxt->get_logger ());
1918 /* LOC can be NULL. */
1919 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1920 RETURN_IF_FAIL_PRINTF1 (
1921 valid_binary_op_p (op),
1922 ctxt, loc,
1923 "unrecognized value for enum gcc_jit_binary_op: %i",
1924 op);
1925 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1926 RETURN_IF_FAIL_PRINTF4 (
1927 compatible_types (lvalue->get_type (),
1928 rvalue->get_type ()),
1929 ctxt, loc,
1930 "mismatching types:"
1931 " assignment to %s (type: %s) involving %s (type: %s)",
1932 lvalue->get_debug_string (),
1933 lvalue->get_type ()->get_debug_string (),
1934 rvalue->get_debug_string (),
1935 rvalue->get_type ()->get_debug_string ());
1937 gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
1939 /* "stmt" should be good enough to be usable in error-messages,
1940 but might still not be compilable; perform some more
1941 error-checking here. We do this here so that the error messages
1942 can contain a stringified version of "stmt", whilst appearing
1943 as close as possible to the point of failure. */
1944 lvalue->verify_valid_within_stmt (__func__, stmt);
1945 rvalue->verify_valid_within_stmt (__func__, stmt);
1948 /* Internal helper function for determining if rvalue BOOLVAL is of
1949 boolean type. For use by gcc_jit_block_end_with_conditional. */
1951 static bool
1952 is_bool (gcc_jit_rvalue *boolval)
1954 gcc::jit::recording::type *actual_type = boolval->get_type ();
1955 gcc::jit::recording::type *bool_type =
1956 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1957 return actual_type == bool_type;
1960 /* Public entrypoint. See description in libgccjit.h.
1962 After error-checking, the real work is done by the
1963 gcc::jit::recording::block::end_with_conditional method in
1964 jit-recording.c. */
1966 void
1967 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1968 gcc_jit_location *loc,
1969 gcc_jit_rvalue *boolval,
1970 gcc_jit_block *on_true,
1971 gcc_jit_block *on_false)
1973 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1974 gcc::jit::recording::context *ctxt = block->get_context ();
1975 JIT_LOG_FUNC (ctxt->get_logger ());
1976 /* LOC can be NULL. */
1977 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
1978 RETURN_IF_FAIL_PRINTF2 (
1979 is_bool (boolval), ctxt, loc,
1980 "%s (type: %s) is not of boolean type ",
1981 boolval->get_debug_string (),
1982 boolval->get_type ()->get_debug_string ());
1983 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
1984 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
1985 RETURN_IF_FAIL_PRINTF4 (
1986 block->get_function () == on_true->get_function (),
1987 ctxt, loc,
1988 "\"on_true\" block is not in same function:"
1989 " source block %s is in function %s"
1990 " whereas target block %s is in function %s",
1991 block->get_debug_string (),
1992 block->get_function ()->get_debug_string (),
1993 on_true->get_debug_string (),
1994 on_true->get_function ()->get_debug_string ());
1995 RETURN_IF_FAIL_PRINTF4 (
1996 block->get_function () == on_false->get_function (),
1997 ctxt, loc,
1998 "\"on_false\" block is not in same function:"
1999 " source block %s is in function %s"
2000 " whereas target block %s is in function %s",
2001 block->get_debug_string (),
2002 block->get_function ()->get_debug_string (),
2003 on_false->get_debug_string (),
2004 on_false->get_function ()->get_debug_string ());
2006 gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
2008 /* "stmt" should be good enough to be usable in error-messages,
2009 but might still not be compilable; perform some more
2010 error-checking here. We do this here so that the error messages
2011 can contain a stringified version of "stmt", whilst appearing
2012 as close as possible to the point of failure. */
2013 boolval->verify_valid_within_stmt (__func__, stmt);
2016 /* Public entrypoint. See description in libgccjit.h.
2018 After error-checking, the real work is done by the
2019 gcc::jit::recording::block::add_comment method in
2020 jit-recording.c. */
2022 void
2023 gcc_jit_block_add_comment (gcc_jit_block *block,
2024 gcc_jit_location *loc,
2025 const char *text)
2027 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2028 gcc::jit::recording::context *ctxt = block->get_context ();
2029 JIT_LOG_FUNC (ctxt->get_logger ());
2030 /* LOC can be NULL. */
2031 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
2033 block->add_comment (loc, text);
2036 /* Public entrypoint. See description in libgccjit.h.
2038 After error-checking, the real work is done by the
2039 gcc::jit::recording::block::end_with_jump method in
2040 jit-recording.c. */
2042 void
2043 gcc_jit_block_end_with_jump (gcc_jit_block *block,
2044 gcc_jit_location *loc,
2045 gcc_jit_block *target)
2047 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2048 gcc::jit::recording::context *ctxt = block->get_context ();
2049 JIT_LOG_FUNC (ctxt->get_logger ());
2050 /* LOC can be NULL. */
2051 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
2052 RETURN_IF_FAIL_PRINTF4 (
2053 block->get_function () == target->get_function (),
2054 ctxt, loc,
2055 "target block is not in same function:"
2056 " source block %s is in function %s"
2057 " whereas target block %s is in function %s",
2058 block->get_debug_string (),
2059 block->get_function ()->get_debug_string (),
2060 target->get_debug_string (),
2061 target->get_function ()->get_debug_string ());
2063 block->end_with_jump (loc, target);
2066 /* Public entrypoint. See description in libgccjit.h.
2068 After error-checking, the real work is done by the
2069 gcc::jit::recording::block::end_with_return method in
2070 jit-recording.c. */
2072 void
2073 gcc_jit_block_end_with_return (gcc_jit_block *block,
2074 gcc_jit_location *loc,
2075 gcc_jit_rvalue *rvalue)
2077 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2078 gcc::jit::recording::context *ctxt = block->get_context ();
2079 JIT_LOG_FUNC (ctxt->get_logger ());
2080 /* LOC can be NULL. */
2081 gcc::jit::recording::function *func = block->get_function ();
2082 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2083 RETURN_IF_FAIL_PRINTF4 (
2084 compatible_types (
2085 func->get_return_type (),
2086 rvalue->get_type ()),
2087 ctxt, loc,
2088 "mismatching types:"
2089 " return of %s (type: %s) in function %s (return type: %s)",
2090 rvalue->get_debug_string (),
2091 rvalue->get_type ()->get_debug_string (),
2092 func->get_debug_string (),
2093 func->get_return_type ()->get_debug_string ());
2095 gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
2097 /* "stmt" should be good enough to be usable in error-messages,
2098 but might still not be compilable; perform some more
2099 error-checking here. We do this here so that the error messages
2100 can contain a stringified version of "stmt", whilst appearing
2101 as close as possible to the point of failure. */
2102 rvalue->verify_valid_within_stmt (__func__, stmt);
2105 /* Public entrypoint. See description in libgccjit.h.
2107 After error-checking, the real work is done by the
2108 gcc::jit::recording::block::end_with_return method in
2109 jit-recording.c. */
2111 void
2112 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
2113 gcc_jit_location *loc)
2115 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2116 gcc::jit::recording::context *ctxt = block->get_context ();
2117 JIT_LOG_FUNC (ctxt->get_logger ());
2118 /* LOC can be NULL. */
2119 gcc::jit::recording::function *func = block->get_function ();
2120 RETURN_IF_FAIL_PRINTF2 (
2121 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2122 ctxt, loc,
2123 "mismatching types:"
2124 " void return in function %s (return type: %s)",
2125 func->get_debug_string (),
2126 func->get_return_type ()->get_debug_string ());
2128 block->end_with_return (loc, NULL);
2131 /* Public entrypoint. See description in libgccjit.h.
2133 After error-checking, the real work is done by the
2134 gcc::jit::recording::context::new_case method in
2135 jit-recording.c. */
2137 gcc_jit_case *
2138 gcc_jit_context_new_case (gcc_jit_context *ctxt,
2139 gcc_jit_rvalue *min_value,
2140 gcc_jit_rvalue *max_value,
2141 gcc_jit_block *block)
2143 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2144 JIT_LOG_FUNC (ctxt->get_logger ());
2145 RETURN_NULL_IF_FAIL (min_value, ctxt, NULL, "NULL min_value");
2146 RETURN_NULL_IF_FAIL (max_value, ctxt, NULL, "NULL max_value");
2147 RETURN_NULL_IF_FAIL (block, ctxt, NULL, "NULL block");
2149 RETURN_NULL_IF_FAIL_PRINTF1 (min_value->is_constant (), ctxt, NULL,
2150 "min_value is not a constant: %s",
2151 min_value->get_debug_string ());
2152 RETURN_NULL_IF_FAIL_PRINTF1 (max_value->is_constant (), ctxt, NULL,
2153 "max_value is not a constant: %s",
2154 max_value->get_debug_string ());
2155 RETURN_NULL_IF_FAIL_PRINTF2 (
2156 min_value->get_type ()->is_int (),
2157 ctxt, NULL,
2158 "min_value: %s (type: %s) is not of integer type",
2159 min_value->get_debug_string (),
2160 min_value->get_type ()->get_debug_string ());
2161 RETURN_NULL_IF_FAIL_PRINTF2 (
2162 max_value->get_type ()->is_int (),
2163 ctxt, NULL,
2164 "max_value: %s (type: %s) is not of integer type",
2165 max_value->get_debug_string (),
2166 max_value->get_type ()->get_debug_string ());
2168 wide_int wi_min, wi_max;
2169 if (!min_value->get_wide_int (&wi_min))
2170 gcc_unreachable ();
2171 if (!max_value->get_wide_int (&wi_max))
2172 gcc_unreachable ();
2173 RETURN_NULL_IF_FAIL_PRINTF2 (
2174 wi::les_p (wi_min, wi_max),
2175 ctxt, NULL,
2176 "min_value: %s > max_value: %s",
2177 min_value->get_debug_string (),
2178 max_value->get_debug_string ());
2179 return (gcc_jit_case *)ctxt->new_case (min_value,
2180 max_value,
2181 block);
2184 /* Public entrypoint. See description in libgccjit.h.
2186 After error-checking, this calls the trivial
2187 gcc::jit::recording::memento::as_object method (a case is a
2188 memento), in jit-recording.h. */
2190 gcc_jit_object *
2191 gcc_jit_case_as_object (gcc_jit_case *case_)
2193 RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case");
2195 return static_cast <gcc_jit_object *> (case_->as_object ());
2198 /* Helper function for gcc_jit_block_end_with_switch and
2199 valid_case_for_switch. */
2201 static bool
2202 valid_dest_for_switch (gcc::jit::recording::context *ctxt,
2203 gcc_jit_location *loc,
2204 const char *api_funcname,
2205 gcc::jit::recording::block *switch_block,
2206 gcc::jit::recording::block *dest_block,
2207 const char *dest_block_desc)
2209 if (!dest_block)
2211 jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc);
2212 return false;
2214 gcc::jit::recording::function *switch_fn = switch_block->get_function ();
2215 gcc::jit::recording::function *dest_fn = dest_block->get_function ();
2216 if (switch_fn != dest_fn)
2218 jit_error (ctxt, loc,
2219 "%s: %s is not in same function:"
2220 " switch block %s is in function %s"
2221 " whereas %s %s is in function %s",
2222 api_funcname,
2223 dest_block_desc,
2224 switch_block->get_debug_string (),
2225 switch_fn->get_debug_string (),
2226 dest_block_desc,
2227 dest_block->get_debug_string (),
2228 dest_fn->get_debug_string ());
2229 return false;
2231 return true;
2234 /* Helper function for gcc_jit_block_end_with_switch. */
2236 static bool
2237 valid_case_for_switch (gcc::jit::recording::context *ctxt,
2238 gcc_jit_location *loc,
2239 const char *api_funcname,
2240 gcc_jit_block *switch_block,
2241 gcc_jit_rvalue *expr,
2242 gcc_jit_case *case_,
2243 const char *case_desc,
2244 int case_idx)
2246 if (!case_)
2248 jit_error (ctxt, loc,
2249 "%s:"
2250 " NULL case %i",
2251 api_funcname,
2252 case_idx);
2253 return false;
2255 if (!valid_dest_for_switch (ctxt, loc,
2256 api_funcname,
2257 switch_block,
2258 case_->get_dest_block (),
2259 case_desc))
2260 return false;
2261 gcc::jit::recording::type *expr_type = expr->get_type ();
2262 if (expr_type != case_->get_min_value ()->get_type ())
2264 jit_error (ctxt, loc,
2265 "%s:"
2266 " mismatching types between case and expression:"
2267 " cases[%i]->min_value: %s (type: %s)"
2268 " expr: %s (type: %s)",
2269 api_funcname,
2270 case_idx,
2271 case_->get_min_value ()->get_debug_string (),
2272 case_->get_min_value ()->get_type ()->get_debug_string (),
2273 expr->get_debug_string (),
2274 expr_type->get_debug_string ());
2275 return false;
2277 if (expr_type != case_->get_max_value ()->get_type ())
2279 jit_error (ctxt, loc,
2280 "%s:"
2281 " mismatching types between case and expression:"
2282 " cases[%i]->max_value: %s (type: %s)"
2283 " expr: %s (type: %s)",
2284 api_funcname,
2285 case_idx,
2286 case_->get_max_value ()->get_debug_string (),
2287 case_->get_max_value ()->get_type ()->get_debug_string (),
2288 expr->get_debug_string (),
2289 expr_type->get_debug_string ());
2290 return false;
2292 return true;
2295 /* A class for holding the data we need to perform error-checking
2296 on a libgccjit API call. */
2298 class api_call_validator
2300 public:
2301 api_call_validator (gcc::jit::recording::context *ctxt,
2302 gcc_jit_location *loc,
2303 const char *funcname)
2304 : m_ctxt (ctxt),
2305 m_loc (loc),
2306 m_funcname (funcname)
2309 protected:
2310 gcc::jit::recording::context *m_ctxt;
2311 gcc_jit_location *m_loc;
2312 const char *m_funcname;
2315 /* A class for verifying that the ranges of cases within
2316 gcc_jit_block_end_with_switch don't overlap. */
2318 class case_range_validator : public api_call_validator
2320 public:
2321 case_range_validator (gcc::jit::recording::context *ctxt,
2322 gcc_jit_location *loc,
2323 const char *funcname);
2325 bool
2326 validate (gcc_jit_case *case_, int idx);
2328 private:
2329 static int
2330 case_compare (gcc::jit::recording::rvalue *k1,
2331 gcc::jit::recording::rvalue *k2);
2333 static wide_int
2334 get_wide_int (gcc::jit::recording::rvalue *k);
2336 private:
2337 typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases;
2340 /* case_range_validator's ctor. */
2342 case_range_validator::case_range_validator (gcc::jit::recording::context *ctxt,
2343 gcc_jit_location *loc,
2344 const char *funcname)
2345 : api_call_validator (ctxt, loc, funcname),
2346 m_cases (case_compare, NULL, NULL)
2350 /* Ensure that the range of CASE_ does not overlap with any of the
2351 ranges of cases we've already seen.
2352 Return true if everything is OK.
2353 Return false and emit an error if there is an overlap.
2354 Compare with c-family/c-common.c:c_add_case_label. */
2356 bool
2357 case_range_validator::validate (gcc_jit_case *case_,
2358 int case_idx)
2360 /* Look up the LOW_VALUE in the table of case labels we already
2361 have. */
2362 gcc_jit_case *other = m_cases.lookup (case_->get_min_value ());
2364 /* If there was not an exact match, check for overlapping ranges. */
2365 if (!other)
2367 gcc_jit_case *pred;
2368 gcc_jit_case *succ;
2370 /* Even though there wasn't an exact match, there might be an
2371 overlap between this case range and another case range.
2372 Since we've (inductively) not allowed any overlapping case
2373 ranges, we simply need to find the greatest low case label
2374 that is smaller that CASE_MIN_VALUE, and the smallest low case
2375 label that is greater than CASE_MAX_VALUE. If there is an overlap
2376 it will occur in one of these two ranges. */
2377 pred = m_cases.predecessor (case_->get_min_value ());
2378 succ = m_cases.successor (case_->get_max_value ());
2380 /* Check to see if the PRED overlaps. It is smaller than
2381 the LOW_VALUE, so we only need to check its max value. */
2382 if (pred)
2384 wide_int wi_case_min = get_wide_int (case_->get_min_value ());
2385 wide_int wi_pred_max = get_wide_int (pred->get_max_value ());
2386 if (wi::ges_p (wi_pred_max, wi_case_min))
2387 other = pred;
2390 if (!other && succ)
2392 /* Check to see if the SUCC overlaps. The low end of that
2393 range is bigger than the low end of the current range. */
2394 wide_int wi_case_max = get_wide_int (case_->get_max_value ());
2395 wide_int wi_succ_min = get_wide_int (succ->get_min_value ());
2396 if (wi::les_p (wi_succ_min, wi_case_max))
2397 other = succ;
2401 /* If there was an overlap, issue an error. */
2402 if (other)
2404 jit_error (m_ctxt, m_loc,
2405 "%s: duplicate (or overlapping) cases values:"
2406 " case %i: %s overlaps %s",
2407 m_funcname,
2408 case_idx,
2409 case_->get_debug_string (),
2410 other->get_debug_string ());
2411 return false;
2414 /* Register this case label in the splay tree. */
2415 m_cases.insert (case_->get_min_value (),
2416 case_);
2417 return true;
2420 /* Compare with c-family/c-common.c:case_compare, which acts on tree
2421 nodes, rather than rvalue *.
2423 Comparator for case label values. K1 and K2 must be constant integer
2424 values (anything else should have been rejected by
2425 gcc_jit_context_new_case.
2427 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
2428 K2, and 0 if K1 and K2 are equal. */
2431 case_range_validator::case_compare (gcc::jit::recording::rvalue * k1,
2432 gcc::jit::recording::rvalue * k2)
2434 wide_int wi1 = get_wide_int (k1);
2435 wide_int wi2 = get_wide_int (k2);
2436 return wi::cmps(wi1, wi2);
2439 /* Given a const int rvalue K, get the underlying value as a wide_int. */
2441 wide_int
2442 case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k)
2444 wide_int wi;
2445 bool got_wi = k->get_wide_int (&wi);
2446 gcc_assert (got_wi);
2447 return wi;
2450 /* Public entrypoint. See description in libgccjit.h.
2452 After error-checking, the real work is done by the
2453 gcc::jit::recording::block::end_with_switch method in
2454 jit-recording.c. */
2456 void
2457 gcc_jit_block_end_with_switch (gcc_jit_block *block,
2458 gcc_jit_location *loc,
2459 gcc_jit_rvalue *expr,
2460 gcc_jit_block *default_block,
2461 int num_cases,
2462 gcc_jit_case **cases)
2464 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2465 gcc::jit::recording::context *ctxt = block->get_context ();
2466 JIT_LOG_FUNC (ctxt->get_logger ());
2467 /* LOC can be NULL. */
2468 RETURN_IF_FAIL (expr, ctxt, loc,
2469 "NULL expr");
2470 gcc::jit::recording::type *expr_type = expr->get_type ();
2471 RETURN_IF_FAIL_PRINTF2 (
2472 expr_type->is_int (),
2473 ctxt, loc,
2474 "expr: %s (type: %s) is not of integer type",
2475 expr->get_debug_string (),
2476 expr_type->get_debug_string ());
2477 if (!valid_dest_for_switch (ctxt, loc,
2478 __func__,
2479 block,
2480 default_block,
2481 "default_block"))
2482 return;
2483 RETURN_IF_FAIL (num_cases >= 0, ctxt, loc, "num_cases < 0");
2484 case_range_validator crv (ctxt, loc, __func__);
2485 for (int i = 0; i < num_cases; i++)
2487 char case_desc[32];
2488 snprintf (case_desc, sizeof (case_desc),
2489 "cases[%i]", i);
2490 if (!valid_case_for_switch (ctxt, loc,
2491 __func__,
2492 block,
2493 expr,
2494 cases[i],
2495 case_desc,
2497 return;
2498 if (!crv.validate (cases[i], i))
2499 return;
2502 block->end_with_switch (loc, expr, default_block,
2503 num_cases,
2504 (gcc::jit::recording::case_ **)cases);
2507 /**********************************************************************
2508 Option-management
2509 **********************************************************************/
2511 /* Public entrypoint. See description in libgccjit.h.
2513 After error-checking, the real work is done by the
2514 gcc::jit::recording::context::set_str_option method in
2515 jit-recording.c. */
2517 void
2518 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2519 enum gcc_jit_str_option opt,
2520 const char *value)
2522 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2523 JIT_LOG_FUNC (ctxt->get_logger ());
2524 /* opt is checked by the inner function.
2525 value can be NULL. */
2527 ctxt->set_str_option (opt, value);
2530 /* Public entrypoint. See description in libgccjit.h.
2532 After error-checking, the real work is done by the
2533 gcc::jit::recording::context::set_int_option method in
2534 jit-recording.c. */
2536 void
2537 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2538 enum gcc_jit_int_option opt,
2539 int value)
2541 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2542 JIT_LOG_FUNC (ctxt->get_logger ());
2543 /* opt is checked by the inner function. */
2545 ctxt->set_int_option (opt, value);
2548 /* Public entrypoint. See description in libgccjit.h.
2550 After error-checking, the real work is done by the
2551 gcc::jit::recording::context::set_bool_option method in
2552 jit-recording.c. */
2554 void
2555 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2556 enum gcc_jit_bool_option opt,
2557 int value)
2559 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2560 JIT_LOG_FUNC (ctxt->get_logger ());
2561 /* opt is checked by the inner function. */
2563 ctxt->set_bool_option (opt, value);
2566 /* Public entrypoint. See description in libgccjit.h.
2568 After error-checking, the real work is done by the
2569 gcc::jit::recording::context::set_inner_bool_option method in
2570 jit-recording.c. */
2572 void
2573 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
2574 int bool_value)
2576 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2577 JIT_LOG_FUNC (ctxt->get_logger ());
2578 ctxt->set_inner_bool_option (
2579 gcc::jit::INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
2580 bool_value);
2583 /* Public entrypoint. See description in libgccjit.h.
2585 After error-checking, the real work is done by the
2586 gcc::jit::recording::context::add_command_line_option method in
2587 jit-recording.c. */
2589 void
2590 gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
2591 const char *optname)
2593 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2594 JIT_LOG_FUNC (ctxt->get_logger ());
2595 RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
2596 if (ctxt->get_logger ())
2597 ctxt->get_logger ()->log ("optname: %s", optname);
2599 ctxt->add_command_line_option (optname);
2602 /* Public entrypoint. See description in libgccjit.h.
2604 After error-checking, the real work is done by the
2605 gcc::jit::recording::context::enable_dump method in
2606 jit-recording.c. */
2608 void
2609 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2610 const char *dumpname,
2611 char **out_ptr)
2613 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2614 JIT_LOG_FUNC (ctxt->get_logger ());
2615 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2616 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2618 ctxt->enable_dump (dumpname, out_ptr);
2621 /* Public entrypoint. See description in libgccjit.h.
2623 After error-checking, the real work is done by the
2624 gcc::jit::recording::context::compile method in
2625 jit-recording.c. */
2627 gcc_jit_result *
2628 gcc_jit_context_compile (gcc_jit_context *ctxt)
2630 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2632 JIT_LOG_FUNC (ctxt->get_logger ());
2634 ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
2636 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2638 ctxt->log ("%s: returning (gcc_jit_result *)%p",
2639 __func__, (void *)result);
2641 return result;
2644 /* Public entrypoint. See description in libgccjit.h.
2646 After error-checking, the real work is done by the
2647 gcc::jit::recording::context::compile_to_file method in
2648 jit-recording.c. */
2650 void
2651 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
2652 enum gcc_jit_output_kind output_kind,
2653 const char *output_path)
2655 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2656 JIT_LOG_FUNC (ctxt->get_logger ());
2657 RETURN_IF_FAIL_PRINTF1 (
2658 ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
2659 && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
2660 ctxt, NULL,
2661 "unrecognized output_kind: %i",
2662 output_kind);
2663 RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
2665 ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
2666 ctxt->log ("output_kind: %i", output_kind);
2667 ctxt->log ("output_path: %s", output_path);
2669 ctxt->compile_to_file (output_kind, output_path);
2673 /* Public entrypoint. See description in libgccjit.h.
2675 After error-checking, the real work is done by the
2676 gcc::jit::recording::context::dump_to_file method in
2677 jit-recording.c. */
2679 void
2680 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2681 const char *path,
2682 int update_locations)
2684 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2685 JIT_LOG_FUNC (ctxt->get_logger ());
2686 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2687 ctxt->dump_to_file (path, update_locations);
2690 /* Public entrypoint. See description in libgccjit.h. */
2692 void
2693 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2694 FILE *logfile,
2695 int flags,
2696 int verbosity)
2698 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2699 JIT_LOG_FUNC (ctxt->get_logger ());
2700 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2701 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2703 gcc::jit::logger *logger;
2704 if (logfile)
2705 logger = new gcc::jit::logger (logfile, flags, verbosity);
2706 else
2707 logger = NULL;
2708 ctxt->set_logger (logger);
2711 /* Public entrypoint. See description in libgccjit.h.
2713 After error-checking, the real work is done by the
2714 gcc::jit::recording::context::dump_reproducer_to_file method in
2715 jit-recording.c. */
2717 void
2718 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
2719 const char *path)
2721 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2722 JIT_LOG_FUNC (ctxt->get_logger ());
2723 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2724 ctxt->dump_reproducer_to_file (path);
2727 /* Public entrypoint. See description in libgccjit.h.
2729 After error-checking, the real work is done by the
2730 gcc::jit::recording::context::get_first_error method in
2731 jit-recording.c. */
2733 const char *
2734 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2736 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2737 JIT_LOG_FUNC (ctxt->get_logger ());
2739 return ctxt->get_first_error ();
2742 /* Public entrypoint. See description in libgccjit.h.
2744 After error-checking, the real work is done by the
2745 gcc::jit::recording::context::get_last_error method in
2746 jit-recording.c. */
2748 const char *
2749 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2751 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2753 return ctxt->get_last_error ();
2756 /* Public entrypoint. See description in libgccjit.h.
2758 After error-checking, the real work is done by the
2759 gcc::jit::result::get_code method in jit-result.c. */
2761 void *
2762 gcc_jit_result_get_code (gcc_jit_result *result,
2763 const char *fnname)
2765 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2766 JIT_LOG_FUNC (result->get_logger ());
2767 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2769 result->log ("locating fnname: %s", fnname);
2770 void *code = result->get_code (fnname);
2771 result->log ("%s: returning (void *)%p", __func__, code);
2773 return code;
2776 /* Public entrypoint. See description in libgccjit.h.
2778 After error-checking, the real work is done by the
2779 gcc::jit::result::get_global method in jit-result.c. */
2781 void *
2782 gcc_jit_result_get_global (gcc_jit_result *result,
2783 const char *name)
2785 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2786 JIT_LOG_FUNC (result->get_logger ());
2787 RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
2789 void *global = result->get_global (name);
2790 result->log ("%s: returning (void *)%p", __func__, global);
2792 return global;
2795 /* Public entrypoint. See description in libgccjit.h.
2797 After error-checking, this is essentially a wrapper around the
2798 destructor for gcc::jit::result in jit-result.c. */
2800 void
2801 gcc_jit_result_release (gcc_jit_result *result)
2803 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2804 JIT_LOG_FUNC (result->get_logger ());
2805 result->log ("deleting result: %p", (void *)result);
2806 delete result;