2015-09-28 David Wohlferd <dw@LimeGreenSocks.com>
[official-gcc.git] / gcc / jit / libgccjit.c
blob55cda6bf890f3509b26572ca48d46b4b0ba26318
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"
27 #include "timevar.h"
29 #include "libgccjit.h"
30 #include "jit-common.h"
31 #include "jit-logging.h"
32 #include "jit-recording.h"
33 #include "jit-result.h"
35 /* The opaque types used by the public API are actually subclasses
36 of the gcc::jit::recording classes. */
38 struct gcc_jit_context : public gcc::jit::recording::context
40 gcc_jit_context (gcc_jit_context *parent_ctxt) :
41 context (parent_ctxt)
45 struct gcc_jit_result : public gcc::jit::result
49 struct gcc_jit_object : public gcc::jit::recording::memento
53 struct gcc_jit_location : public gcc::jit::recording::location
57 struct gcc_jit_type : public gcc::jit::recording::type
61 struct gcc_jit_struct : public gcc::jit::recording::struct_
65 struct gcc_jit_field : public gcc::jit::recording::field
69 struct gcc_jit_function : public gcc::jit::recording::function
73 struct gcc_jit_block : public gcc::jit::recording::block
77 struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
81 struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
85 struct gcc_jit_param : public gcc::jit::recording::param
89 struct gcc_jit_case : public gcc::jit::recording::case_
93 struct gcc_jit_timer : public timer
97 /**********************************************************************
98 Error-handling.
100 We try to gracefully handle API usage errors by being defensive
101 at the API boundary.
102 **********************************************************************/
104 #define JIT_BEGIN_STMT do {
105 #define JIT_END_STMT } while(0)
107 /* Each of these error-handling macros determines if TEST_EXPR holds.
109 If TEXT_EXPR fails to hold we return from the enclosing function and
110 print an error, either via adding an error on the given context CTXT
111 if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
112 is NULL.
114 They have to be macros since they inject their "return" into the
115 function they are placed in.
117 The variant macros express:
119 (A) whether or not we need to return a value:
120 RETURN_VAL_IF_FAIL* vs
121 RETURN_IF_FAIL*,
122 with the former returning RETURN_EXPR, and
123 RETURN_NULL_IF_FAIL*
124 for the common case where a NULL value is to be returned on
125 error, and
127 (B) whether the error message is to be directly printed:
128 RETURN_*IF_FAIL
129 or is a format string with some number of arguments:
130 RETURN_*IF_FAIL_PRINTF*
132 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
133 trailing semicolons.
136 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
137 JIT_BEGIN_STMT \
138 if (!(TEST_EXPR)) \
140 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
141 return (RETURN_EXPR); \
143 JIT_END_STMT
145 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
146 JIT_BEGIN_STMT \
147 if (!(TEST_EXPR)) \
149 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
150 __func__, (A0)); \
151 return (RETURN_EXPR); \
153 JIT_END_STMT
155 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
156 JIT_BEGIN_STMT \
157 if (!(TEST_EXPR)) \
159 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
160 __func__, (A0), (A1)); \
161 return (RETURN_EXPR); \
163 JIT_END_STMT
165 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
166 JIT_BEGIN_STMT \
167 if (!(TEST_EXPR)) \
169 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
170 __func__, (A0), (A1), (A2)); \
171 return (RETURN_EXPR); \
173 JIT_END_STMT
175 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
176 JIT_BEGIN_STMT \
177 if (!(TEST_EXPR)) \
179 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
180 __func__, (A0), (A1), (A2), (A3)); \
181 return (RETURN_EXPR); \
183 JIT_END_STMT
185 #define RETURN_VAL_IF_FAIL_PRINTF5(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
186 JIT_BEGIN_STMT \
187 if (!(TEST_EXPR)) \
189 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
190 __func__, (A0), (A1), (A2), (A3), (A4)); \
191 return (RETURN_EXPR); \
193 JIT_END_STMT
195 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
196 JIT_BEGIN_STMT \
197 if (!(TEST_EXPR)) \
199 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
200 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
201 return (RETURN_EXPR); \
203 JIT_END_STMT
205 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
206 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
208 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
209 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
211 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
212 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
214 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
215 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
217 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
218 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
220 #define RETURN_NULL_IF_FAIL_PRINTF5(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4) \
221 RETURN_VAL_IF_FAIL_PRINTF5 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4)
223 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
224 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
226 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
227 JIT_BEGIN_STMT \
228 if (!(TEST_EXPR)) \
230 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
231 return; \
233 JIT_END_STMT
235 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
236 JIT_BEGIN_STMT \
237 if (!(TEST_EXPR)) \
239 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
240 __func__, (A0)); \
241 return; \
243 JIT_END_STMT
245 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
246 JIT_BEGIN_STMT \
247 if (!(TEST_EXPR)) \
249 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
250 __func__, (A0), (A1)); \
251 return; \
253 JIT_END_STMT
255 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
256 JIT_BEGIN_STMT \
257 if (!(TEST_EXPR)) \
259 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
260 __func__, (A0), (A1), (A2), (A3)); \
261 return; \
263 JIT_END_STMT
265 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
266 it. This will fail if BLOCK has already been terminated by some
267 kind of jump or a return. */
268 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
269 JIT_BEGIN_STMT \
270 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
271 RETURN_IF_FAIL_PRINTF2 ( \
272 !(BLOCK)->has_been_terminated (), \
273 (BLOCK)->get_context (), \
274 (LOC), \
275 "adding to terminated block: %s (already terminated by: %s)", \
276 (BLOCK)->get_debug_string (), \
277 (BLOCK)->get_last_statement ()->get_debug_string ()); \
278 JIT_END_STMT
280 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
281 fails. */
282 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
283 JIT_BEGIN_STMT \
284 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
285 RETURN_NULL_IF_FAIL_PRINTF2 ( \
286 !(BLOCK)->has_been_terminated (), \
287 (BLOCK)->get_context (), \
288 (LOC), \
289 "adding to terminated block: %s (already terminated by: %s)", \
290 (BLOCK)->get_debug_string (), \
291 (BLOCK)->get_last_statement ()->get_debug_string ()); \
292 JIT_END_STMT
294 /* Format the given string, and report it as an error, either on CTXT
295 if non-NULL, or by printing to stderr if we have a NULL context.
296 LOC gives the source location where the error occcurred, and can be
297 NULL. */
299 static void
300 jit_error (gcc::jit::recording::context *ctxt,
301 gcc_jit_location *loc,
302 const char *fmt, ...)
303 GNU_PRINTF(3, 4);
305 static void
306 jit_error (gcc::jit::recording::context *ctxt,
307 gcc_jit_location *loc,
308 const char *fmt, ...)
310 va_list ap;
311 va_start (ap, fmt);
313 if (ctxt)
314 ctxt->add_error_va (loc, fmt, ap);
315 else
317 /* No context? Send to stderr. */
318 vfprintf (stderr, fmt, ap);
319 fprintf (stderr, "\n");
322 va_end (ap);
325 /* Determine whether or not we can write to lvalues of type LTYPE from
326 rvalues of type RTYPE, detecting type errors such as attempting to
327 write to an int with a string literal (without an explicit cast).
329 This is implemented by calling the
330 gcc::jit::recording::type::accepts_writes_from virtual function on
331 LTYPE. */
333 static bool
334 compatible_types (gcc::jit::recording::type *ltype,
335 gcc::jit::recording::type *rtype)
337 return ltype->accepts_writes_from (rtype);
340 /* Public entrypoint for acquiring a gcc_jit_context.
341 Note that this creates a new top-level context; contrast with
342 gcc_jit_context_new_child_context below.
344 The real work is done in the constructor for
345 gcc::jit::recording::context in jit-recording.c. */
347 gcc_jit_context *
348 gcc_jit_context_acquire (void)
350 gcc_jit_context *ctxt = new gcc_jit_context (NULL);
351 ctxt->log ("new top-level ctxt: %p", (void *)ctxt);
352 return ctxt;
355 /* Public entrypoint for releasing a gcc_jit_context.
356 The real work is done in the destructor for
357 gcc::jit::recording::context in jit-recording.c. */
359 void
360 gcc_jit_context_release (gcc_jit_context *ctxt)
362 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
363 JIT_LOG_FUNC (ctxt->get_logger ());
364 ctxt->log ("deleting ctxt: %p", (void *)ctxt);
365 delete ctxt;
368 /* Public entrypoint for creating a child context within
369 PARENT_CTXT. See description in libgccjit.h.
371 The real work is done in the constructor for
372 gcc::jit::recording::context in jit-recording.c. */
374 gcc_jit_context *
375 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
377 RETURN_NULL_IF_FAIL (parent_ctxt, NULL, NULL, "NULL parent ctxt");
378 JIT_LOG_FUNC (parent_ctxt->get_logger ());
379 parent_ctxt->log ("parent_ctxt: %p", (void *)parent_ctxt);
380 gcc_jit_context *child_ctxt = new gcc_jit_context (parent_ctxt);
381 child_ctxt->log ("new child_ctxt: %p", (void *)child_ctxt);
382 return child_ctxt;
385 /* Public entrypoint. See description in libgccjit.h.
387 After error-checking, the real work is done by the
388 gcc::jit::recording::context::new_location
389 method in jit-recording.c. */
391 gcc_jit_location *
392 gcc_jit_context_new_location (gcc_jit_context *ctxt,
393 const char *filename,
394 int line,
395 int column)
397 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
398 JIT_LOG_FUNC (ctxt->get_logger ());
399 return (gcc_jit_location *)ctxt->new_location (filename, line, column, true);
402 /* Public entrypoint. See description in libgccjit.h.
404 After error-checking, this calls the trivial
405 gcc::jit::recording::memento::as_object method (a location is a
406 memento), in jit-recording.h. */
408 gcc_jit_object *
409 gcc_jit_location_as_object (gcc_jit_location *loc)
411 RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
413 return static_cast <gcc_jit_object *> (loc->as_object ());
416 /* Public entrypoint. See description in libgccjit.h.
418 After error-checking, this calls the trivial
419 gcc::jit::recording::memento::as_object method (a type is a
420 memento), in jit-recording.h. */
422 gcc_jit_object *
423 gcc_jit_type_as_object (gcc_jit_type *type)
425 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
427 return static_cast <gcc_jit_object *> (type->as_object ());
430 /* Public entrypoint for getting a specific type from a context.
432 After error-checking, the real work is done by the
433 gcc::jit::recording::context::get_type method, in
434 jit-recording.c */
436 gcc_jit_type *
437 gcc_jit_context_get_type (gcc_jit_context *ctxt,
438 enum gcc_jit_types type)
440 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
441 JIT_LOG_FUNC (ctxt->get_logger ());
442 RETURN_NULL_IF_FAIL_PRINTF1 (
443 (type >= GCC_JIT_TYPE_VOID
444 && type <= GCC_JIT_TYPE_FILE_PTR),
445 ctxt, NULL,
446 "unrecognized value for enum gcc_jit_types: %i", type);
448 return (gcc_jit_type *)ctxt->get_type (type);
451 /* Public entrypoint for getting the integer type of the given size and
452 signedness.
454 After error-checking, the real work is done by the
455 gcc::jit::recording::context::get_int_type method,
456 in jit-recording.c. */
458 gcc_jit_type *
459 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
460 int num_bytes, int is_signed)
462 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
463 JIT_LOG_FUNC (ctxt->get_logger ());
464 RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
466 return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
469 /* Public entrypoint. See description in libgccjit.h.
471 After error-checking, the real work is done by the
472 gcc::jit::recording::type::get_pointer method, in
473 jit-recording.c */
475 gcc_jit_type *
476 gcc_jit_type_get_pointer (gcc_jit_type *type)
478 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
480 return (gcc_jit_type *)type->get_pointer ();
483 /* Public entrypoint. See description in libgccjit.h.
485 After error-checking, the real work is done by the
486 gcc::jit::recording::type::get_const method, in
487 jit-recording.c. */
489 gcc_jit_type *
490 gcc_jit_type_get_const (gcc_jit_type *type)
492 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
494 return (gcc_jit_type *)type->get_const ();
497 /* Public entrypoint. See description in libgccjit.h.
499 After error-checking, the real work is done by the
500 gcc::jit::recording::type::get_volatile method, in
501 jit-recording.c. */
503 gcc_jit_type *
504 gcc_jit_type_get_volatile (gcc_jit_type *type)
506 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
508 return (gcc_jit_type *)type->get_volatile ();
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_array_type method, in
515 jit-recording.c. */
517 gcc_jit_type *
518 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
519 gcc_jit_location *loc,
520 gcc_jit_type *element_type,
521 int num_elements)
523 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
524 JIT_LOG_FUNC (ctxt->get_logger ());
525 /* LOC can be NULL. */
526 RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
527 RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
529 return (gcc_jit_type *)ctxt->new_array_type (loc,
530 element_type,
531 num_elements);
534 /* Public entrypoint. See description in libgccjit.h.
536 After error-checking, the real work is done by the
537 gcc::jit::recording::context::new_field method, in
538 jit-recording.c. */
540 gcc_jit_field *
541 gcc_jit_context_new_field (gcc_jit_context *ctxt,
542 gcc_jit_location *loc,
543 gcc_jit_type *type,
544 const char *name)
546 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
547 JIT_LOG_FUNC (ctxt->get_logger ());
548 /* LOC can be NULL. */
549 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
550 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
551 RETURN_NULL_IF_FAIL_PRINTF2 (
552 type->has_known_size (),
553 ctxt, loc,
554 "unknown size for field \"%s\" (type: %s)",
555 name,
556 type->get_debug_string ());
558 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
561 /* Public entrypoint. See description in libgccjit.h.
563 After error-checking, this calls the trivial
564 gcc::jit::recording::memento::as_object method (a field is a
565 memento), in jit-recording.h. */
567 gcc_jit_object *
568 gcc_jit_field_as_object (gcc_jit_field *field)
570 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
572 return static_cast <gcc_jit_object *> (field->as_object ());
575 /* Public entrypoint. See description in libgccjit.h.
577 After error-checking, the real work is done by the
578 gcc::jit::recording::context::new_struct_type method,
579 immediately followed by a "set_fields" call on the resulting
580 gcc::jit::recording::compound_type *, both in jit-recording.c */
582 gcc_jit_struct *
583 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
584 gcc_jit_location *loc,
585 const char *name,
586 int num_fields,
587 gcc_jit_field **fields)
589 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
590 JIT_LOG_FUNC (ctxt->get_logger ());
591 /* LOC can be NULL. */
592 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
593 if (num_fields)
594 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
595 for (int i = 0; i < num_fields; i++)
597 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
598 RETURN_NULL_IF_FAIL_PRINTF2 (
599 NULL == fields[i]->get_container (),
600 ctxt, loc,
601 "%s is already a field of %s",
602 fields[i]->get_debug_string (),
603 fields[i]->get_container ()->get_debug_string ());
606 gcc::jit::recording::struct_ *result =
607 ctxt->new_struct_type (loc, name);
608 result->set_fields (loc,
609 num_fields,
610 (gcc::jit::recording::field **)fields);
611 return static_cast<gcc_jit_struct *> (result);
614 /* Public entrypoint. See description in libgccjit.h.
616 After error-checking, the real work is done by the
617 gcc::jit::recording::context::new_struct_type method in
618 jit-recording.c. */
620 gcc_jit_struct *
621 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
622 gcc_jit_location *loc,
623 const char *name)
625 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
626 JIT_LOG_FUNC (ctxt->get_logger ());
627 /* LOC can be NULL. */
628 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
630 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
633 /* Public entrypoint. See description in libgccjit.h.
635 After error-checking, this calls the trivial
636 gcc::jit::recording::struct_::as_object method in
637 jit-recording.h. */
639 gcc_jit_type *
640 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
642 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
644 return static_cast <gcc_jit_type *> (struct_type->as_type ());
647 /* Public entrypoint. See description in libgccjit.h.
649 After error-checking, the real work is done by the
650 gcc::jit::recording::compound_type::set_fields method in
651 jit-recording.c. */
653 void
654 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
655 gcc_jit_location *loc,
656 int num_fields,
657 gcc_jit_field **fields)
659 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
660 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
661 JIT_LOG_FUNC (ctxt->get_logger ());
662 /* LOC can be NULL. */
663 RETURN_IF_FAIL_PRINTF1 (
664 NULL == struct_type->get_fields (), ctxt, loc,
665 "%s already has had fields set",
666 struct_type->get_debug_string ());
667 if (num_fields)
668 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
669 for (int i = 0; i < num_fields; i++)
671 RETURN_IF_FAIL_PRINTF2 (
672 fields[i],
673 ctxt, loc,
674 "%s: NULL field ptr at index %i",
675 struct_type->get_debug_string (),
677 RETURN_IF_FAIL_PRINTF2 (
678 NULL == fields[i]->get_container (),
679 ctxt, loc,
680 "%s is already a field of %s",
681 fields[i]->get_debug_string (),
682 fields[i]->get_container ()->get_debug_string ());
685 struct_type->set_fields (loc, num_fields,
686 (gcc::jit::recording::field **)fields);
689 /* Public entrypoint. See description in libgccjit.h.
691 After error-checking, the real work is done by the
692 gcc::jit::recording::context::new_union_type method,
693 immediately followed by a "set_fields" call on the resulting
694 gcc::jit::recording::compound_type *, both in jit-recording.c */
696 gcc_jit_type *
697 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
698 gcc_jit_location *loc,
699 const char *name,
700 int num_fields,
701 gcc_jit_field **fields)
703 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
704 JIT_LOG_FUNC (ctxt->get_logger ());
705 /* LOC can be NULL. */
706 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
707 if (num_fields)
708 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
709 for (int i = 0; i < num_fields; i++)
711 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
712 RETURN_NULL_IF_FAIL_PRINTF2 (
713 NULL == fields[i]->get_container (),
714 ctxt, loc,
715 "%s is already a field of %s",
716 fields[i]->get_debug_string (),
717 fields[i]->get_container ()->get_debug_string ());
720 gcc::jit::recording::union_ *result =
721 ctxt->new_union_type (loc, name);
722 result->set_fields (loc,
723 num_fields,
724 (gcc::jit::recording::field **)fields);
725 return (gcc_jit_type *) (result);
728 /* Public entrypoint. See description in libgccjit.h.
730 After error-checking, the real work is done by the
731 gcc::jit::recording::context::new_function_ptr_type method,
732 in jit-recording.c */
734 gcc_jit_type *
735 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
736 gcc_jit_location *loc,
737 gcc_jit_type *return_type,
738 int num_params,
739 gcc_jit_type **param_types,
740 int is_variadic)
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 (return_type, ctxt, loc, "NULL return_type");
746 RETURN_NULL_IF_FAIL (
747 (num_params == 0) || param_types,
748 ctxt, loc,
749 "NULL param_types creating function pointer type");
750 for (int i = 0; i < num_params; i++)
751 RETURN_NULL_IF_FAIL_PRINTF1 (
752 param_types[i],
753 ctxt, loc,
754 "NULL parameter type %i creating function pointer type", i);
756 return (gcc_jit_type*)
757 ctxt->new_function_ptr_type (loc, return_type,
758 num_params,
759 (gcc::jit::recording::type **)param_types,
760 is_variadic);
763 /* Constructing functions. */
765 /* Public entrypoint. See description in libgccjit.h.
767 After error-checking, the real work is done by the
768 gcc::jit::recording::context::new_param method, in jit-recording.c */
770 gcc_jit_param *
771 gcc_jit_context_new_param (gcc_jit_context *ctxt,
772 gcc_jit_location *loc,
773 gcc_jit_type *type,
774 const char *name)
776 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
777 JIT_LOG_FUNC (ctxt->get_logger ());
778 /* LOC can be NULL. */
779 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
780 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
782 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
785 /* Public entrypoint. See description in libgccjit.h.
787 After error-checking, this calls the trivial
788 gcc::jit::recording::memento::as_object method (a param is a memento),
789 in jit-recording.h. */
791 gcc_jit_object *
792 gcc_jit_param_as_object (gcc_jit_param *param)
794 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
796 return static_cast <gcc_jit_object *> (param->as_object ());
799 /* Public entrypoint. See description in libgccjit.h.
801 After error-checking, this calls the trivial
802 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
804 gcc_jit_lvalue *
805 gcc_jit_param_as_lvalue (gcc_jit_param *param)
807 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
809 return (gcc_jit_lvalue *)param->as_lvalue ();
812 /* Public entrypoint. See description in libgccjit.h.
814 After error-checking, this calls the trivial
815 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
816 in jit-recording.h. */
818 gcc_jit_rvalue *
819 gcc_jit_param_as_rvalue (gcc_jit_param *param)
821 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
823 return (gcc_jit_rvalue *)param->as_rvalue ();
826 /* Public entrypoint. See description in libgccjit.h.
828 After error-checking, the real work is done by the
829 gcc::jit::recording::context::new_function method, in
830 jit-recording.c. */
832 gcc_jit_function *
833 gcc_jit_context_new_function (gcc_jit_context *ctxt,
834 gcc_jit_location *loc,
835 enum gcc_jit_function_kind kind,
836 gcc_jit_type *return_type,
837 const char *name,
838 int num_params,
839 gcc_jit_param **params,
840 int is_variadic)
842 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
843 JIT_LOG_FUNC (ctxt->get_logger ());
844 /* LOC can be NULL. */
845 RETURN_NULL_IF_FAIL_PRINTF1 (
846 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
847 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
848 ctxt, loc,
849 "unrecognized value for enum gcc_jit_function_kind: %i",
850 kind);
851 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
852 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
853 /* The assembler can only handle certain names, so for now, enforce
854 C's rules for identiers upon the name, using ISALPHA and ISALNUM
855 from safe-ctype.h to ignore the current locale.
856 Eventually we'll need some way to interact with e.g. C++ name
857 mangling. */
859 /* Leading char: */
860 char ch = *name;
861 RETURN_NULL_IF_FAIL_PRINTF2 (
862 ISALPHA (ch) || ch == '_',
863 ctxt, loc,
864 "name \"%s\" contains invalid character: '%c'",
865 name, ch);
866 /* Subsequent chars: */
867 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
869 RETURN_NULL_IF_FAIL_PRINTF2 (
870 ISALNUM (ch) || ch == '_',
871 ctxt, loc,
872 "name \"%s\" contains invalid character: '%c'",
873 name, ch);
876 RETURN_NULL_IF_FAIL_PRINTF1 (
877 (num_params == 0) || params,
878 ctxt, loc,
879 "NULL params creating function %s", name);
880 for (int i = 0; i < num_params; i++)
882 RETURN_NULL_IF_FAIL_PRINTF2 (
883 params[i],
884 ctxt, loc,
885 "NULL parameter %i creating function %s", i, name);
886 RETURN_NULL_IF_FAIL_PRINTF5 (
887 (NULL == params[i]->get_scope ()),
888 ctxt, loc,
889 "parameter %i \"%s\""
890 " (type: %s)"
891 " for function %s"
892 " was already used for function %s",
893 i, params[i]->get_debug_string (),
894 params[i]->get_type ()->get_debug_string (),
895 name,
896 params[i]->get_scope ()->get_debug_string ());
899 return (gcc_jit_function*)
900 ctxt->new_function (loc, kind, return_type, name,
901 num_params,
902 (gcc::jit::recording::param **)params,
903 is_variadic,
904 BUILT_IN_NONE);
907 /* Public entrypoint. See description in libgccjit.h.
909 After error-checking, the real work is done by the
910 gcc::jit::recording::context::get_builtin_function method, in
911 jit-recording.c. */
913 gcc_jit_function *
914 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
915 const char *name)
917 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
918 JIT_LOG_FUNC (ctxt->get_logger ());
919 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
921 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
924 /* Public entrypoint. See description in libgccjit.h.
926 After error-checking, this calls the trivial
927 gcc::jit::recording::memento::as_object method (a function is a
928 memento), in jit-recording.h. */
930 gcc_jit_object *
931 gcc_jit_function_as_object (gcc_jit_function *func)
933 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
935 return static_cast <gcc_jit_object *> (func->as_object ());
938 /* Public entrypoint. See description in libgccjit.h.
940 After error-checking, the real work is done by the
941 gcc::jit::recording::function::get_param method, in
942 jit-recording.h. */
944 gcc_jit_param *
945 gcc_jit_function_get_param (gcc_jit_function *func, int index)
947 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
948 gcc::jit::recording::context *ctxt = func->m_ctxt;
949 JIT_LOG_FUNC (ctxt->get_logger ());
950 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
951 int num_params = func->get_params ().length ();
952 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
953 ctxt, NULL,
954 "index of %d is too large (%s has %d params)",
955 index,
956 func->get_debug_string (),
957 num_params);
959 return static_cast <gcc_jit_param *> (func->get_param (index));
962 /* Public entrypoint. See description in libgccjit.h.
964 After error-checking, the real work is done by the
965 gcc::jit::recording::function::dump_to_dot method, in
966 jit-recording.c. */
968 void
969 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
970 const char *path)
972 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
973 gcc::jit::recording::context *ctxt = func->m_ctxt;
974 JIT_LOG_FUNC (ctxt->get_logger ());
975 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
977 func->dump_to_dot (path);
980 /* Public entrypoint. See description in libgccjit.h.
982 After error-checking, the real work is done by the
983 gcc::jit::recording::function::new_block method, in
984 jit-recording.c. */
986 gcc_jit_block*
987 gcc_jit_function_new_block (gcc_jit_function *func,
988 const char *name)
990 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
991 JIT_LOG_FUNC (func->get_context ()->get_logger ());
992 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
993 func->get_context (), NULL,
994 "cannot add block to an imported function");
995 /* name can be NULL. */
997 return (gcc_jit_block *)func->new_block (name);
1000 /* Public entrypoint. See description in libgccjit.h.
1002 After error-checking, this calls the trivial
1003 gcc::jit::recording::memento::as_object method (a block is a
1004 memento), in jit-recording.h. */
1006 gcc_jit_object *
1007 gcc_jit_block_as_object (gcc_jit_block *block)
1009 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1011 return static_cast <gcc_jit_object *> (block->as_object ());
1014 /* Public entrypoint. See description in libgccjit.h.
1016 After error-checking, the real work is done by the
1017 gcc::jit::recording::block::get_function method, in
1018 jit-recording.h. */
1020 gcc_jit_function *
1021 gcc_jit_block_get_function (gcc_jit_block *block)
1023 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1025 return static_cast <gcc_jit_function *> (block->get_function ());
1028 /* Public entrypoint. See description in libgccjit.h.
1030 After error-checking, the real work is done by the
1031 gcc::jit::recording::context::new_global method, in
1032 jit-recording.c. */
1034 gcc_jit_lvalue *
1035 gcc_jit_context_new_global (gcc_jit_context *ctxt,
1036 gcc_jit_location *loc,
1037 enum gcc_jit_global_kind kind,
1038 gcc_jit_type *type,
1039 const char *name)
1041 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1042 JIT_LOG_FUNC (ctxt->get_logger ());
1043 /* LOC can be NULL. */
1044 RETURN_NULL_IF_FAIL_PRINTF1 (
1045 ((kind >= GCC_JIT_GLOBAL_EXPORTED)
1046 && (kind <= GCC_JIT_GLOBAL_IMPORTED)),
1047 ctxt, loc,
1048 "unrecognized value for enum gcc_jit_global_kind: %i",
1049 kind);
1050 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1051 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1052 RETURN_NULL_IF_FAIL_PRINTF2 (
1053 type->has_known_size (),
1054 ctxt, loc,
1055 "unknown size for global \"%s\" (type: %s)",
1056 name,
1057 type->get_debug_string ());
1059 return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
1062 /* Public entrypoint. See description in libgccjit.h.
1064 After error-checking, this calls the trivial
1065 gcc::jit::recording::memento::as_object method (an lvalue is a
1066 memento), in jit-recording.h. */
1068 gcc_jit_object *
1069 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1071 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1073 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1076 /* Public entrypoint. See description in libgccjit.h.
1078 After error-checking, this calls the trivial
1079 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1081 gcc_jit_rvalue *
1082 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1084 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1086 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1089 /* Public entrypoint. See description in libgccjit.h.
1091 After error-checking, this calls the trivial
1092 gcc::jit::recording::memento::as_object method (an rvalue is a
1093 memento), in jit-recording.h. */
1095 gcc_jit_object *
1096 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1098 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1100 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1103 /* Public entrypoint. See description in libgccjit.h.
1105 After error-checking, the real work is done by the
1106 gcc::jit::recording::rvalue::get_type method, in
1107 jit-recording.h. */
1109 gcc_jit_type *
1110 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1112 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1114 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1117 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1118 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1119 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1121 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1122 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1123 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1124 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1125 "not a numeric type: %s", \
1126 NUMERIC_TYPE->get_debug_string ());
1128 /* Public entrypoint. See description in libgccjit.h.
1130 After error-checking, the real work is done by the
1131 gcc::jit::recording::context::new_rvalue_from_int method in
1132 jit-recording.c. */
1134 gcc_jit_rvalue *
1135 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1136 gcc_jit_type *numeric_type,
1137 int value)
1139 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1140 JIT_LOG_FUNC (ctxt->get_logger ());
1141 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1143 return ((gcc_jit_rvalue *)ctxt
1144 ->new_rvalue_from_const <int> (numeric_type, value));
1147 /* FIXME. */
1149 gcc_jit_rvalue *
1150 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1151 gcc_jit_type *numeric_type,
1152 long value)
1154 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1155 JIT_LOG_FUNC (ctxt->get_logger ());
1156 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1158 return ((gcc_jit_rvalue *)ctxt
1159 ->new_rvalue_from_const <long> (numeric_type, value));
1162 /* Public entrypoint. See description in libgccjit.h.
1164 This is essentially equivalent to:
1165 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1166 albeit with slightly different error messages if an error occurs. */
1168 gcc_jit_rvalue *
1169 gcc_jit_context_zero (gcc_jit_context *ctxt,
1170 gcc_jit_type *numeric_type)
1172 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1173 JIT_LOG_FUNC (ctxt->get_logger ());
1174 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1176 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1179 /* Public entrypoint. See description in libgccjit.h.
1181 This is essentially equivalent to:
1182 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1183 albeit with slightly different error messages if an error occurs. */
1185 gcc_jit_rvalue *
1186 gcc_jit_context_one (gcc_jit_context *ctxt,
1187 gcc_jit_type *numeric_type)
1189 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1190 JIT_LOG_FUNC (ctxt->get_logger ());
1191 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1193 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1196 /* Public entrypoint. See description in libgccjit.h.
1198 After error-checking, the real work is done by the
1199 gcc::jit::recording::context::new_rvalue_from_double method in
1200 jit-recording.c. */
1202 gcc_jit_rvalue *
1203 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1204 gcc_jit_type *numeric_type,
1205 double value)
1207 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1208 JIT_LOG_FUNC (ctxt->get_logger ());
1209 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1211 return ((gcc_jit_rvalue *)ctxt
1212 ->new_rvalue_from_const <double> (numeric_type, value));
1215 /* Public entrypoint. See description in libgccjit.h.
1217 After error-checking, the real work is done by the
1218 gcc::jit::recording::context::new_rvalue_from_ptr method in
1219 jit-recording.c. */
1221 gcc_jit_rvalue *
1222 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1223 gcc_jit_type *pointer_type,
1224 void *value)
1226 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1227 JIT_LOG_FUNC (ctxt->get_logger ());
1228 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1229 RETURN_NULL_IF_FAIL_PRINTF1 (
1230 pointer_type->is_pointer (),
1231 ctxt, NULL,
1232 "not a pointer type (type: %s)",
1233 pointer_type->get_debug_string ());
1235 return ((gcc_jit_rvalue *)ctxt
1236 ->new_rvalue_from_const <void *> (pointer_type, value));
1239 /* Public entrypoint. See description in libgccjit.h.
1241 This is essentially equivalent to:
1242 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1243 albeit with slightly different error messages if an error occurs. */
1245 gcc_jit_rvalue *
1246 gcc_jit_context_null (gcc_jit_context *ctxt,
1247 gcc_jit_type *pointer_type)
1249 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1250 JIT_LOG_FUNC (ctxt->get_logger ());
1251 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1252 RETURN_NULL_IF_FAIL_PRINTF1 (
1253 pointer_type->is_pointer (),
1254 ctxt, NULL,
1255 "not a pointer type (type: %s)",
1256 pointer_type->get_debug_string ());
1258 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1261 /* Public entrypoint. See description in libgccjit.h.
1263 After error-checking, the real work is done by the
1264 gcc::jit::recording::context::new_string_literal method in
1265 jit-recording.c. */
1267 gcc_jit_rvalue *
1268 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1269 const char *value)
1271 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1272 JIT_LOG_FUNC (ctxt->get_logger ());
1273 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1275 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1278 /* Public entrypoint. See description in libgccjit.h.
1280 After error-checking, the real work is done by the
1281 gcc::jit::recording::context::new_unary_op method in
1282 jit-recording.c. */
1284 gcc_jit_rvalue *
1285 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1286 gcc_jit_location *loc,
1287 enum gcc_jit_unary_op op,
1288 gcc_jit_type *result_type,
1289 gcc_jit_rvalue *rvalue)
1291 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1292 JIT_LOG_FUNC (ctxt->get_logger ());
1293 /* LOC can be NULL. */
1294 RETURN_NULL_IF_FAIL_PRINTF1 (
1295 (op >= GCC_JIT_UNARY_OP_MINUS
1296 && op <= GCC_JIT_UNARY_OP_ABS),
1297 ctxt, loc,
1298 "unrecognized value for enum gcc_jit_unary_op: %i",
1299 op);
1300 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1301 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1303 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1306 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1307 For use by both gcc_jit_context_new_binary_op and
1308 gcc_jit_block_add_assignment_op. */
1310 static bool
1311 valid_binary_op_p (enum gcc_jit_binary_op op)
1313 return (op >= GCC_JIT_BINARY_OP_PLUS
1314 && op <= GCC_JIT_BINARY_OP_RSHIFT);
1317 /* Public entrypoint. See description in libgccjit.h.
1319 After error-checking, the real work is done by the
1320 gcc::jit::recording::context::new_binary_op method in
1321 jit-recording.c. */
1323 gcc_jit_rvalue *
1324 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1325 gcc_jit_location *loc,
1326 enum gcc_jit_binary_op op,
1327 gcc_jit_type *result_type,
1328 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1330 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1331 JIT_LOG_FUNC (ctxt->get_logger ());
1332 /* LOC can be NULL. */
1333 RETURN_NULL_IF_FAIL_PRINTF1 (
1334 valid_binary_op_p (op),
1335 ctxt, loc,
1336 "unrecognized value for enum gcc_jit_binary_op: %i",
1337 op);
1338 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1339 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1340 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1341 RETURN_NULL_IF_FAIL_PRINTF4 (
1342 a->get_type () == b->get_type (),
1343 ctxt, loc,
1344 "mismatching types for binary op:"
1345 " a: %s (type: %s) b: %s (type: %s)",
1346 a->get_debug_string (),
1347 a->get_type ()->get_debug_string (),
1348 b->get_debug_string (),
1349 b->get_type ()->get_debug_string ());
1351 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1354 /* Public entrypoint. See description in libgccjit.h.
1356 After error-checking, the real work is done by the
1357 gcc::jit::recording::context::new_comparison method in
1358 jit-recording.c. */
1360 gcc_jit_rvalue *
1361 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1362 gcc_jit_location *loc,
1363 enum gcc_jit_comparison op,
1364 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1366 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1367 JIT_LOG_FUNC (ctxt->get_logger ());
1368 /* LOC can be NULL. */
1369 RETURN_NULL_IF_FAIL_PRINTF1 (
1370 (op >= GCC_JIT_COMPARISON_EQ
1371 && op <= GCC_JIT_COMPARISON_GE),
1372 ctxt, loc,
1373 "unrecognized value for enum gcc_jit_comparison: %i",
1374 op);
1375 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1376 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1377 RETURN_NULL_IF_FAIL_PRINTF4 (
1378 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1379 ctxt, loc,
1380 "mismatching types for comparison:"
1381 " a: %s (type: %s) b: %s (type: %s)",
1382 a->get_debug_string (),
1383 a->get_type ()->get_debug_string (),
1384 b->get_debug_string (),
1385 b->get_type ()->get_debug_string ());
1387 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1390 /* Public entrypoint. See description in libgccjit.h.
1392 After error-checking, the real work is done by the
1393 gcc::jit::recording::context::new_call method in
1394 jit-recording.c. */
1396 gcc_jit_rvalue *
1397 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1398 gcc_jit_location *loc,
1399 gcc_jit_function *func,
1400 int numargs , gcc_jit_rvalue **args)
1402 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1403 JIT_LOG_FUNC (ctxt->get_logger ());
1404 /* LOC can be NULL. */
1405 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1406 if (numargs)
1407 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1409 int min_num_params = func->get_params ().length ();
1410 bool is_variadic = func->is_variadic ();
1412 RETURN_NULL_IF_FAIL_PRINTF3 (
1413 numargs >= min_num_params,
1414 ctxt, loc,
1415 "not enough arguments to function \"%s\""
1416 " (got %i args, expected %i)",
1417 func->get_name ()->c_str (),
1418 numargs, min_num_params);
1420 RETURN_NULL_IF_FAIL_PRINTF3 (
1421 (numargs == min_num_params || is_variadic),
1422 ctxt, loc,
1423 "too many arguments to function \"%s\""
1424 " (got %i args, expected %i)",
1425 func->get_name ()->c_str (),
1426 numargs, min_num_params);
1428 for (int i = 0; i < min_num_params; i++)
1430 gcc::jit::recording::param *param = func->get_param (i);
1431 gcc_jit_rvalue *arg = args[i];
1433 RETURN_NULL_IF_FAIL_PRINTF4 (
1434 arg,
1435 ctxt, loc,
1436 "NULL argument %i to function \"%s\":"
1437 " param %s (type: %s)",
1438 i + 1,
1439 func->get_name ()->c_str (),
1440 param->get_debug_string (),
1441 param->get_type ()->get_debug_string ());
1443 RETURN_NULL_IF_FAIL_PRINTF6 (
1444 compatible_types (param->get_type (),
1445 arg->get_type ()),
1446 ctxt, loc,
1447 "mismatching types for argument %d of function \"%s\":"
1448 " assignment to param %s (type: %s) from %s (type: %s)",
1449 i + 1,
1450 func->get_name ()->c_str (),
1451 param->get_debug_string (),
1452 param->get_type ()->get_debug_string (),
1453 arg->get_debug_string (),
1454 arg->get_type ()->get_debug_string ());
1457 return (gcc_jit_rvalue *)ctxt->new_call (loc,
1458 func,
1459 numargs,
1460 (gcc::jit::recording::rvalue **)args);
1463 /* Public entrypoint. See description in libgccjit.h.
1465 After error-checking, the real work is done by the
1466 gcc::jit::recording::context::new_call_through_ptr method in
1467 jit-recording.c. */
1469 gcc_jit_rvalue *
1470 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1471 gcc_jit_location *loc,
1472 gcc_jit_rvalue *fn_ptr,
1473 int numargs, gcc_jit_rvalue **args)
1475 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1476 JIT_LOG_FUNC (ctxt->get_logger ());
1477 /* LOC can be NULL. */
1478 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1479 if (numargs)
1480 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1482 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1483 RETURN_NULL_IF_FAIL_PRINTF2 (
1484 ptr_type, ctxt, loc,
1485 "fn_ptr is not a ptr: %s"
1486 " type: %s",
1487 fn_ptr->get_debug_string (),
1488 fn_ptr->get_type ()->get_debug_string ());
1490 gcc::jit::recording::function_type *fn_type =
1491 ptr_type->dyn_cast_function_type();
1492 RETURN_NULL_IF_FAIL_PRINTF2 (
1493 fn_type, ctxt, loc,
1494 "fn_ptr is not a function ptr: %s"
1495 " type: %s",
1496 fn_ptr->get_debug_string (),
1497 fn_ptr->get_type ()->get_debug_string ());
1499 int min_num_params = fn_type->get_param_types ().length ();
1500 bool is_variadic = fn_type->is_variadic ();
1502 RETURN_NULL_IF_FAIL_PRINTF3 (
1503 numargs >= min_num_params,
1504 ctxt, loc,
1505 "not enough arguments to fn_ptr: %s"
1506 " (got %i args, expected %i)",
1507 fn_ptr->get_debug_string (),
1508 numargs, min_num_params);
1510 RETURN_NULL_IF_FAIL_PRINTF3 (
1511 (numargs == min_num_params || is_variadic),
1512 ctxt, loc,
1513 "too many arguments to fn_ptr: %s"
1514 " (got %i args, expected %i)",
1515 fn_ptr->get_debug_string (),
1516 numargs, min_num_params);
1518 for (int i = 0; i < min_num_params; i++)
1520 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1521 gcc_jit_rvalue *arg = args[i];
1523 RETURN_NULL_IF_FAIL_PRINTF3 (
1524 arg,
1525 ctxt, loc,
1526 "NULL argument %i to fn_ptr: %s"
1527 " (type: %s)",
1528 i + 1,
1529 fn_ptr->get_debug_string (),
1530 param_type->get_debug_string ());
1532 RETURN_NULL_IF_FAIL_PRINTF6 (
1533 compatible_types (param_type,
1534 arg->get_type ()),
1535 ctxt, loc,
1536 "mismatching types for argument %d of fn_ptr: %s:"
1537 " assignment to param %d (type: %s) from %s (type: %s)",
1538 i + 1,
1539 fn_ptr->get_debug_string (),
1540 i + 1,
1541 param_type->get_debug_string (),
1542 arg->get_debug_string (),
1543 arg->get_type ()->get_debug_string ());
1546 return (gcc_jit_rvalue *)(
1547 ctxt->new_call_through_ptr (loc,
1548 fn_ptr,
1549 numargs,
1550 (gcc::jit::recording::rvalue **)args));
1553 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1554 to DST_TYPE, for use by gcc_jit_context_new_cast.
1556 We only permit these kinds of cast:
1558 int <-> float
1559 int <-> bool
1560 P* <-> Q* for pointer types P and Q. */
1562 static bool
1563 is_valid_cast (gcc::jit::recording::type *src_type,
1564 gcc_jit_type *dst_type)
1566 bool src_is_int = src_type->is_int ();
1567 bool dst_is_int = dst_type->is_int ();
1568 bool src_is_float = src_type->is_float ();
1569 bool dst_is_float = dst_type->is_float ();
1570 bool src_is_bool = src_type->is_bool ();
1571 bool dst_is_bool = dst_type->is_bool ();
1573 if (src_is_int)
1574 if (dst_is_int || dst_is_float || dst_is_bool)
1575 return true;
1577 if (src_is_float)
1578 if (dst_is_int || dst_is_float)
1579 return true;
1581 if (src_is_bool)
1582 if (dst_is_int || dst_is_bool)
1583 return true;
1585 /* Permit casts between pointer types. */
1586 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1587 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1588 if (deref_src_type && deref_dst_type)
1589 return true;
1591 return false;
1594 /* Public entrypoint. See description in libgccjit.h.
1596 After error-checking, the real work is done by the
1597 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1599 gcc_jit_rvalue *
1600 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1601 gcc_jit_location *loc,
1602 gcc_jit_rvalue *rvalue,
1603 gcc_jit_type *type)
1605 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1606 JIT_LOG_FUNC (ctxt->get_logger ());
1607 /* LOC can be NULL. */
1608 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1609 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1610 RETURN_NULL_IF_FAIL_PRINTF3 (
1611 is_valid_cast (rvalue->get_type (), type),
1612 ctxt, loc,
1613 "cannot cast %s from type: %s to type: %s",
1614 rvalue->get_debug_string (),
1615 rvalue->get_type ()->get_debug_string (),
1616 type->get_debug_string ());
1618 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1621 /* Public entrypoint. See description in libgccjit.h.
1623 After error-checking, the real work is done by the
1624 gcc::jit::recording::context::new_array_access method in
1625 jit-recording.c. */
1627 extern gcc_jit_lvalue *
1628 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1629 gcc_jit_location *loc,
1630 gcc_jit_rvalue *ptr,
1631 gcc_jit_rvalue *index)
1633 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1634 JIT_LOG_FUNC (ctxt->get_logger ());
1635 /* LOC can be NULL. */
1636 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1637 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1638 RETURN_NULL_IF_FAIL_PRINTF2 (
1639 ptr->get_type ()->dereference (),
1640 ctxt, loc,
1641 "ptr: %s (type: %s) is not a pointer or array",
1642 ptr->get_debug_string (),
1643 ptr->get_type ()->get_debug_string ());
1644 RETURN_NULL_IF_FAIL_PRINTF2 (
1645 index->get_type ()->is_numeric (),
1646 ctxt, loc,
1647 "index: %s (type: %s) is not of numeric type",
1648 index->get_debug_string (),
1649 index->get_type ()->get_debug_string ());
1651 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1654 /* Public entrypoint. See description in libgccjit.h.
1656 After error-checking, the real work is done by the
1657 gcc::jit::recording::memento::get_context method in
1658 jit-recording.h. */
1660 gcc_jit_context *
1661 gcc_jit_object_get_context (gcc_jit_object *obj)
1663 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1665 return static_cast <gcc_jit_context *> (obj->get_context ());
1668 /* Public entrypoint. See description in libgccjit.h.
1670 After error-checking, the real work is done by the
1671 gcc::jit::recording::memento::get_debug_string method in
1672 jit-recording.c. */
1674 const char *
1675 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1677 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1679 return obj->get_debug_string ();
1682 /* Public entrypoint. See description in libgccjit.h.
1684 After error-checking, the real work is done by the
1685 gcc::jit::recording::lvalue::access_field method in
1686 jit-recording.c. */
1688 gcc_jit_lvalue *
1689 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1690 gcc_jit_location *loc,
1691 gcc_jit_field *field)
1693 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1694 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1695 JIT_LOG_FUNC (ctxt->get_logger ());
1696 /* LOC can be NULL. */
1697 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1698 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1699 "field %s has not been placed in a struct",
1700 field->get_debug_string ());
1701 gcc::jit::recording::type *underlying_type =
1702 struct_->get_type ();
1703 RETURN_NULL_IF_FAIL_PRINTF2 (
1704 (field->get_container ()->unqualified ()
1705 == underlying_type->unqualified ()),
1706 struct_->m_ctxt, loc,
1707 "%s is not a field of %s",
1708 field->get_debug_string (),
1709 underlying_type->get_debug_string ());
1711 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1714 /* Public entrypoint. See description in libgccjit.h.
1716 After error-checking, the real work is done by the
1717 gcc::jit::recording::rvalue::access_field method in
1718 jit-recording.c. */
1720 gcc_jit_rvalue *
1721 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1722 gcc_jit_location *loc,
1723 gcc_jit_field *field)
1725 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1726 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1727 JIT_LOG_FUNC (ctxt->get_logger ());
1728 /* LOC can be NULL. */
1729 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1730 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1731 "field %s has not been placed in a struct",
1732 field->get_debug_string ());
1733 gcc::jit::recording::type *underlying_type =
1734 struct_->get_type ();
1735 RETURN_NULL_IF_FAIL_PRINTF2 (
1736 (field->get_container ()->unqualified ()
1737 == underlying_type->unqualified ()),
1738 struct_->m_ctxt, loc,
1739 "%s is not a field of %s",
1740 field->get_debug_string (),
1741 underlying_type->get_debug_string ());
1743 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1746 /* Public entrypoint. See description in libgccjit.h.
1748 After error-checking, the real work is done by the
1749 gcc::jit::recording::rvalue::deference_field method in
1750 jit-recording.c. */
1752 gcc_jit_lvalue *
1753 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1754 gcc_jit_location *loc,
1755 gcc_jit_field *field)
1757 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1758 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1759 /* LOC can be NULL. */
1760 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1761 gcc::jit::recording::type *underlying_type =
1762 ptr->get_type ()->is_pointer ();
1763 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1764 "field %s has not been placed in a struct",
1765 field->get_debug_string ());
1766 RETURN_NULL_IF_FAIL_PRINTF3 (
1767 underlying_type,
1768 ptr->m_ctxt, loc,
1769 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1770 ptr->get_debug_string (),
1771 ptr->get_type ()->get_debug_string (),
1772 field->get_debug_string ());
1773 RETURN_NULL_IF_FAIL_PRINTF2 (
1774 (field->get_container ()->unqualified ()
1775 == underlying_type->unqualified ()),
1776 ptr->m_ctxt, loc,
1777 "%s is not a field of %s",
1778 field->get_debug_string (),
1779 underlying_type->get_debug_string ());
1781 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1784 /* Public entrypoint. See description in libgccjit.h.
1786 After error-checking, the real work is done by the
1787 gcc::jit::recording::rvalue::deference method in
1788 jit-recording.c. */
1790 gcc_jit_lvalue *
1791 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1792 gcc_jit_location *loc)
1794 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1795 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1796 /* LOC can be NULL. */
1798 gcc::jit::recording::type *underlying_type =
1799 rvalue->get_type ()->is_pointer ();
1801 RETURN_NULL_IF_FAIL_PRINTF2 (
1802 underlying_type,
1803 rvalue->m_ctxt, loc,
1804 "dereference of non-pointer %s (type: %s)",
1805 rvalue->get_debug_string (),
1806 rvalue->get_type ()->get_debug_string ());
1808 RETURN_NULL_IF_FAIL_PRINTF2 (
1809 !underlying_type->is_void (),
1810 rvalue->m_ctxt, loc,
1811 "dereference of void pointer %s (type: %s)",
1812 rvalue->get_debug_string (),
1813 rvalue->get_type ()->get_debug_string ());
1815 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1818 /* Public entrypoint. See description in libgccjit.h.
1820 After error-checking, the real work is done by the
1821 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1823 gcc_jit_rvalue *
1824 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1825 gcc_jit_location *loc)
1827 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1828 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1829 /* LOC can be NULL. */
1831 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1834 /* Public entrypoint. See description in libgccjit.h.
1836 After error-checking, the real work is done by the
1837 gcc::jit::recording::function::new_local method in jit-recording.c. */
1839 gcc_jit_lvalue *
1840 gcc_jit_function_new_local (gcc_jit_function *func,
1841 gcc_jit_location *loc,
1842 gcc_jit_type *type,
1843 const char *name)
1845 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1846 gcc::jit::recording::context *ctxt = func->m_ctxt;
1847 JIT_LOG_FUNC (ctxt->get_logger ());
1848 /* LOC can be NULL. */
1849 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1850 ctxt, loc,
1851 "Cannot add locals to an imported function");
1852 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1853 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1854 RETURN_NULL_IF_FAIL_PRINTF2 (
1855 type->has_known_size (),
1856 ctxt, loc,
1857 "unknown size for local \"%s\" (type: %s)",
1858 name,
1859 type->get_debug_string ());
1861 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1864 /* Public entrypoint. See description in libgccjit.h.
1866 After error-checking, the real work is done by the
1867 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1869 void
1870 gcc_jit_block_add_eval (gcc_jit_block *block,
1871 gcc_jit_location *loc,
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 (rvalue, ctxt, loc, "NULL rvalue");
1880 gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
1882 /* "stmt" should be good enough to be usable in error-messages,
1883 but might still not be compilable; perform some more
1884 error-checking here. We do this here so that the error messages
1885 can contain a stringified version of "stmt", whilst appearing
1886 as close as possible to the point of failure. */
1887 rvalue->verify_valid_within_stmt (__func__, stmt);
1890 /* Public entrypoint. See description in libgccjit.h.
1892 After error-checking, the real work is done by the
1893 gcc::jit::recording::block::add_assignment method in
1894 jit-recording.c. */
1896 void
1897 gcc_jit_block_add_assignment (gcc_jit_block *block,
1898 gcc_jit_location *loc,
1899 gcc_jit_lvalue *lvalue,
1900 gcc_jit_rvalue *rvalue)
1902 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1903 gcc::jit::recording::context *ctxt = block->get_context ();
1904 JIT_LOG_FUNC (ctxt->get_logger ());
1905 /* LOC can be NULL. */
1906 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1907 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1908 RETURN_IF_FAIL_PRINTF4 (
1909 compatible_types (lvalue->get_type (),
1910 rvalue->get_type ()),
1911 ctxt, loc,
1912 "mismatching types:"
1913 " assignment to %s (type: %s) from %s (type: %s)",
1914 lvalue->get_debug_string (),
1915 lvalue->get_type ()->get_debug_string (),
1916 rvalue->get_debug_string (),
1917 rvalue->get_type ()->get_debug_string ());
1919 gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
1921 /* "stmt" should be good enough to be usable in error-messages,
1922 but might still not be compilable; perform some more
1923 error-checking here. We do this here so that the error messages
1924 can contain a stringified version of "stmt", whilst appearing
1925 as close as possible to the point of failure. */
1926 lvalue->verify_valid_within_stmt (__func__, stmt);
1927 rvalue->verify_valid_within_stmt (__func__, stmt);
1930 /* Public entrypoint. See description in libgccjit.h.
1932 After error-checking, the real work is done by the
1933 gcc::jit::recording::block::add_assignment_op method in
1934 jit-recording.c. */
1936 void
1937 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1938 gcc_jit_location *loc,
1939 gcc_jit_lvalue *lvalue,
1940 enum gcc_jit_binary_op op,
1941 gcc_jit_rvalue *rvalue)
1943 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1944 gcc::jit::recording::context *ctxt = block->get_context ();
1945 JIT_LOG_FUNC (ctxt->get_logger ());
1946 /* LOC can be NULL. */
1947 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1948 RETURN_IF_FAIL_PRINTF1 (
1949 valid_binary_op_p (op),
1950 ctxt, loc,
1951 "unrecognized value for enum gcc_jit_binary_op: %i",
1952 op);
1953 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1954 RETURN_IF_FAIL_PRINTF4 (
1955 compatible_types (lvalue->get_type (),
1956 rvalue->get_type ()),
1957 ctxt, loc,
1958 "mismatching types:"
1959 " assignment to %s (type: %s) involving %s (type: %s)",
1960 lvalue->get_debug_string (),
1961 lvalue->get_type ()->get_debug_string (),
1962 rvalue->get_debug_string (),
1963 rvalue->get_type ()->get_debug_string ());
1965 gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
1967 /* "stmt" should be good enough to be usable in error-messages,
1968 but might still not be compilable; perform some more
1969 error-checking here. We do this here so that the error messages
1970 can contain a stringified version of "stmt", whilst appearing
1971 as close as possible to the point of failure. */
1972 lvalue->verify_valid_within_stmt (__func__, stmt);
1973 rvalue->verify_valid_within_stmt (__func__, stmt);
1976 /* Internal helper function for determining if rvalue BOOLVAL is of
1977 boolean type. For use by gcc_jit_block_end_with_conditional. */
1979 static bool
1980 is_bool (gcc_jit_rvalue *boolval)
1982 gcc::jit::recording::type *actual_type = boolval->get_type ();
1983 gcc::jit::recording::type *bool_type =
1984 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1985 return actual_type == bool_type;
1988 /* Public entrypoint. See description in libgccjit.h.
1990 After error-checking, the real work is done by the
1991 gcc::jit::recording::block::end_with_conditional method in
1992 jit-recording.c. */
1994 void
1995 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1996 gcc_jit_location *loc,
1997 gcc_jit_rvalue *boolval,
1998 gcc_jit_block *on_true,
1999 gcc_jit_block *on_false)
2001 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2002 gcc::jit::recording::context *ctxt = block->get_context ();
2003 JIT_LOG_FUNC (ctxt->get_logger ());
2004 /* LOC can be NULL. */
2005 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
2006 RETURN_IF_FAIL_PRINTF2 (
2007 is_bool (boolval), ctxt, loc,
2008 "%s (type: %s) is not of boolean type ",
2009 boolval->get_debug_string (),
2010 boolval->get_type ()->get_debug_string ());
2011 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
2012 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
2013 RETURN_IF_FAIL_PRINTF4 (
2014 block->get_function () == on_true->get_function (),
2015 ctxt, loc,
2016 "\"on_true\" block is not in same function:"
2017 " source block %s is in function %s"
2018 " whereas target block %s is in function %s",
2019 block->get_debug_string (),
2020 block->get_function ()->get_debug_string (),
2021 on_true->get_debug_string (),
2022 on_true->get_function ()->get_debug_string ());
2023 RETURN_IF_FAIL_PRINTF4 (
2024 block->get_function () == on_false->get_function (),
2025 ctxt, loc,
2026 "\"on_false\" block is not in same function:"
2027 " source block %s is in function %s"
2028 " whereas target block %s is in function %s",
2029 block->get_debug_string (),
2030 block->get_function ()->get_debug_string (),
2031 on_false->get_debug_string (),
2032 on_false->get_function ()->get_debug_string ());
2034 gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
2036 /* "stmt" should be good enough to be usable in error-messages,
2037 but might still not be compilable; perform some more
2038 error-checking here. We do this here so that the error messages
2039 can contain a stringified version of "stmt", whilst appearing
2040 as close as possible to the point of failure. */
2041 boolval->verify_valid_within_stmt (__func__, stmt);
2044 /* Public entrypoint. See description in libgccjit.h.
2046 After error-checking, the real work is done by the
2047 gcc::jit::recording::block::add_comment method in
2048 jit-recording.c. */
2050 void
2051 gcc_jit_block_add_comment (gcc_jit_block *block,
2052 gcc_jit_location *loc,
2053 const char *text)
2055 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2056 gcc::jit::recording::context *ctxt = block->get_context ();
2057 JIT_LOG_FUNC (ctxt->get_logger ());
2058 /* LOC can be NULL. */
2059 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
2061 block->add_comment (loc, text);
2064 /* Public entrypoint. See description in libgccjit.h.
2066 After error-checking, the real work is done by the
2067 gcc::jit::recording::block::end_with_jump method in
2068 jit-recording.c. */
2070 void
2071 gcc_jit_block_end_with_jump (gcc_jit_block *block,
2072 gcc_jit_location *loc,
2073 gcc_jit_block *target)
2075 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2076 gcc::jit::recording::context *ctxt = block->get_context ();
2077 JIT_LOG_FUNC (ctxt->get_logger ());
2078 /* LOC can be NULL. */
2079 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
2080 RETURN_IF_FAIL_PRINTF4 (
2081 block->get_function () == target->get_function (),
2082 ctxt, loc,
2083 "target block is not in same function:"
2084 " source block %s is in function %s"
2085 " whereas target block %s is in function %s",
2086 block->get_debug_string (),
2087 block->get_function ()->get_debug_string (),
2088 target->get_debug_string (),
2089 target->get_function ()->get_debug_string ());
2091 block->end_with_jump (loc, target);
2094 /* Public entrypoint. See description in libgccjit.h.
2096 After error-checking, the real work is done by the
2097 gcc::jit::recording::block::end_with_return method in
2098 jit-recording.c. */
2100 void
2101 gcc_jit_block_end_with_return (gcc_jit_block *block,
2102 gcc_jit_location *loc,
2103 gcc_jit_rvalue *rvalue)
2105 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2106 gcc::jit::recording::context *ctxt = block->get_context ();
2107 JIT_LOG_FUNC (ctxt->get_logger ());
2108 /* LOC can be NULL. */
2109 gcc::jit::recording::function *func = block->get_function ();
2110 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2111 RETURN_IF_FAIL_PRINTF4 (
2112 compatible_types (
2113 func->get_return_type (),
2114 rvalue->get_type ()),
2115 ctxt, loc,
2116 "mismatching types:"
2117 " return of %s (type: %s) in function %s (return type: %s)",
2118 rvalue->get_debug_string (),
2119 rvalue->get_type ()->get_debug_string (),
2120 func->get_debug_string (),
2121 func->get_return_type ()->get_debug_string ());
2123 gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
2125 /* "stmt" should be good enough to be usable in error-messages,
2126 but might still not be compilable; perform some more
2127 error-checking here. We do this here so that the error messages
2128 can contain a stringified version of "stmt", whilst appearing
2129 as close as possible to the point of failure. */
2130 rvalue->verify_valid_within_stmt (__func__, stmt);
2133 /* Public entrypoint. See description in libgccjit.h.
2135 After error-checking, the real work is done by the
2136 gcc::jit::recording::block::end_with_return method in
2137 jit-recording.c. */
2139 void
2140 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
2141 gcc_jit_location *loc)
2143 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2144 gcc::jit::recording::context *ctxt = block->get_context ();
2145 JIT_LOG_FUNC (ctxt->get_logger ());
2146 /* LOC can be NULL. */
2147 gcc::jit::recording::function *func = block->get_function ();
2148 RETURN_IF_FAIL_PRINTF2 (
2149 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2150 ctxt, loc,
2151 "mismatching types:"
2152 " void return in function %s (return type: %s)",
2153 func->get_debug_string (),
2154 func->get_return_type ()->get_debug_string ());
2156 block->end_with_return (loc, NULL);
2159 /* Public entrypoint. See description in libgccjit.h.
2161 After error-checking, the real work is done by the
2162 gcc::jit::recording::context::new_case method in
2163 jit-recording.c. */
2165 gcc_jit_case *
2166 gcc_jit_context_new_case (gcc_jit_context *ctxt,
2167 gcc_jit_rvalue *min_value,
2168 gcc_jit_rvalue *max_value,
2169 gcc_jit_block *block)
2171 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2172 JIT_LOG_FUNC (ctxt->get_logger ());
2173 RETURN_NULL_IF_FAIL (min_value, ctxt, NULL, "NULL min_value");
2174 RETURN_NULL_IF_FAIL (max_value, ctxt, NULL, "NULL max_value");
2175 RETURN_NULL_IF_FAIL (block, ctxt, NULL, "NULL block");
2177 RETURN_NULL_IF_FAIL_PRINTF1 (min_value->is_constant (), ctxt, NULL,
2178 "min_value is not a constant: %s",
2179 min_value->get_debug_string ());
2180 RETURN_NULL_IF_FAIL_PRINTF1 (max_value->is_constant (), ctxt, NULL,
2181 "max_value is not a constant: %s",
2182 max_value->get_debug_string ());
2183 RETURN_NULL_IF_FAIL_PRINTF2 (
2184 min_value->get_type ()->is_int (),
2185 ctxt, NULL,
2186 "min_value: %s (type: %s) is not of integer type",
2187 min_value->get_debug_string (),
2188 min_value->get_type ()->get_debug_string ());
2189 RETURN_NULL_IF_FAIL_PRINTF2 (
2190 max_value->get_type ()->is_int (),
2191 ctxt, NULL,
2192 "max_value: %s (type: %s) is not of integer type",
2193 max_value->get_debug_string (),
2194 max_value->get_type ()->get_debug_string ());
2196 wide_int wi_min, wi_max;
2197 if (!min_value->get_wide_int (&wi_min))
2198 gcc_unreachable ();
2199 if (!max_value->get_wide_int (&wi_max))
2200 gcc_unreachable ();
2201 RETURN_NULL_IF_FAIL_PRINTF2 (
2202 wi::les_p (wi_min, wi_max),
2203 ctxt, NULL,
2204 "min_value: %s > max_value: %s",
2205 min_value->get_debug_string (),
2206 max_value->get_debug_string ());
2207 return (gcc_jit_case *)ctxt->new_case (min_value,
2208 max_value,
2209 block);
2212 /* Public entrypoint. See description in libgccjit.h.
2214 After error-checking, this calls the trivial
2215 gcc::jit::recording::memento::as_object method (a case is a
2216 memento), in jit-recording.h. */
2218 gcc_jit_object *
2219 gcc_jit_case_as_object (gcc_jit_case *case_)
2221 RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case");
2223 return static_cast <gcc_jit_object *> (case_->as_object ());
2226 /* Helper function for gcc_jit_block_end_with_switch and
2227 valid_case_for_switch. */
2229 static bool
2230 valid_dest_for_switch (gcc::jit::recording::context *ctxt,
2231 gcc_jit_location *loc,
2232 const char *api_funcname,
2233 gcc::jit::recording::block *switch_block,
2234 gcc::jit::recording::block *dest_block,
2235 const char *dest_block_desc)
2237 if (!dest_block)
2239 jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc);
2240 return false;
2242 gcc::jit::recording::function *switch_fn = switch_block->get_function ();
2243 gcc::jit::recording::function *dest_fn = dest_block->get_function ();
2244 if (switch_fn != dest_fn)
2246 jit_error (ctxt, loc,
2247 "%s: %s is not in same function:"
2248 " switch block %s is in function %s"
2249 " whereas %s %s is in function %s",
2250 api_funcname,
2251 dest_block_desc,
2252 switch_block->get_debug_string (),
2253 switch_fn->get_debug_string (),
2254 dest_block_desc,
2255 dest_block->get_debug_string (),
2256 dest_fn->get_debug_string ());
2257 return false;
2259 return true;
2262 /* Helper function for gcc_jit_block_end_with_switch. */
2264 static bool
2265 valid_case_for_switch (gcc::jit::recording::context *ctxt,
2266 gcc_jit_location *loc,
2267 const char *api_funcname,
2268 gcc_jit_block *switch_block,
2269 gcc_jit_rvalue *expr,
2270 gcc_jit_case *case_,
2271 const char *case_desc,
2272 int case_idx)
2274 if (!case_)
2276 jit_error (ctxt, loc,
2277 "%s:"
2278 " NULL case %i",
2279 api_funcname,
2280 case_idx);
2281 return false;
2283 if (!valid_dest_for_switch (ctxt, loc,
2284 api_funcname,
2285 switch_block,
2286 case_->get_dest_block (),
2287 case_desc))
2288 return false;
2289 gcc::jit::recording::type *expr_type = expr->get_type ();
2290 if (expr_type != case_->get_min_value ()->get_type ())
2292 jit_error (ctxt, loc,
2293 "%s:"
2294 " mismatching types between case and expression:"
2295 " cases[%i]->min_value: %s (type: %s)"
2296 " expr: %s (type: %s)",
2297 api_funcname,
2298 case_idx,
2299 case_->get_min_value ()->get_debug_string (),
2300 case_->get_min_value ()->get_type ()->get_debug_string (),
2301 expr->get_debug_string (),
2302 expr_type->get_debug_string ());
2303 return false;
2305 if (expr_type != case_->get_max_value ()->get_type ())
2307 jit_error (ctxt, loc,
2308 "%s:"
2309 " mismatching types between case and expression:"
2310 " cases[%i]->max_value: %s (type: %s)"
2311 " expr: %s (type: %s)",
2312 api_funcname,
2313 case_idx,
2314 case_->get_max_value ()->get_debug_string (),
2315 case_->get_max_value ()->get_type ()->get_debug_string (),
2316 expr->get_debug_string (),
2317 expr_type->get_debug_string ());
2318 return false;
2320 return true;
2323 /* A class for holding the data we need to perform error-checking
2324 on a libgccjit API call. */
2326 class api_call_validator
2328 public:
2329 api_call_validator (gcc::jit::recording::context *ctxt,
2330 gcc_jit_location *loc,
2331 const char *funcname)
2332 : m_ctxt (ctxt),
2333 m_loc (loc),
2334 m_funcname (funcname)
2337 protected:
2338 gcc::jit::recording::context *m_ctxt;
2339 gcc_jit_location *m_loc;
2340 const char *m_funcname;
2343 /* A class for verifying that the ranges of cases within
2344 gcc_jit_block_end_with_switch don't overlap. */
2346 class case_range_validator : public api_call_validator
2348 public:
2349 case_range_validator (gcc::jit::recording::context *ctxt,
2350 gcc_jit_location *loc,
2351 const char *funcname);
2353 bool
2354 validate (gcc_jit_case *case_, int idx);
2356 private:
2357 static int
2358 case_compare (gcc::jit::recording::rvalue *k1,
2359 gcc::jit::recording::rvalue *k2);
2361 static wide_int
2362 get_wide_int (gcc::jit::recording::rvalue *k);
2364 private:
2365 typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases;
2368 /* case_range_validator's ctor. */
2370 case_range_validator::case_range_validator (gcc::jit::recording::context *ctxt,
2371 gcc_jit_location *loc,
2372 const char *funcname)
2373 : api_call_validator (ctxt, loc, funcname),
2374 m_cases (case_compare, NULL, NULL)
2378 /* Ensure that the range of CASE_ does not overlap with any of the
2379 ranges of cases we've already seen.
2380 Return true if everything is OK.
2381 Return false and emit an error if there is an overlap.
2382 Compare with c-family/c-common.c:c_add_case_label. */
2384 bool
2385 case_range_validator::validate (gcc_jit_case *case_,
2386 int case_idx)
2388 /* Look up the LOW_VALUE in the table of case labels we already
2389 have. */
2390 gcc_jit_case *other = m_cases.lookup (case_->get_min_value ());
2392 /* If there was not an exact match, check for overlapping ranges. */
2393 if (!other)
2395 gcc_jit_case *pred;
2396 gcc_jit_case *succ;
2398 /* Even though there wasn't an exact match, there might be an
2399 overlap between this case range and another case range.
2400 Since we've (inductively) not allowed any overlapping case
2401 ranges, we simply need to find the greatest low case label
2402 that is smaller that CASE_MIN_VALUE, and the smallest low case
2403 label that is greater than CASE_MAX_VALUE. If there is an overlap
2404 it will occur in one of these two ranges. */
2405 pred = m_cases.predecessor (case_->get_min_value ());
2406 succ = m_cases.successor (case_->get_max_value ());
2408 /* Check to see if the PRED overlaps. It is smaller than
2409 the LOW_VALUE, so we only need to check its max value. */
2410 if (pred)
2412 wide_int wi_case_min = get_wide_int (case_->get_min_value ());
2413 wide_int wi_pred_max = get_wide_int (pred->get_max_value ());
2414 if (wi::ges_p (wi_pred_max, wi_case_min))
2415 other = pred;
2418 if (!other && succ)
2420 /* Check to see if the SUCC overlaps. The low end of that
2421 range is bigger than the low end of the current range. */
2422 wide_int wi_case_max = get_wide_int (case_->get_max_value ());
2423 wide_int wi_succ_min = get_wide_int (succ->get_min_value ());
2424 if (wi::les_p (wi_succ_min, wi_case_max))
2425 other = succ;
2429 /* If there was an overlap, issue an error. */
2430 if (other)
2432 jit_error (m_ctxt, m_loc,
2433 "%s: duplicate (or overlapping) cases values:"
2434 " case %i: %s overlaps %s",
2435 m_funcname,
2436 case_idx,
2437 case_->get_debug_string (),
2438 other->get_debug_string ());
2439 return false;
2442 /* Register this case label in the splay tree. */
2443 m_cases.insert (case_->get_min_value (),
2444 case_);
2445 return true;
2448 /* Compare with c-family/c-common.c:case_compare, which acts on tree
2449 nodes, rather than rvalue *.
2451 Comparator for case label values. K1 and K2 must be constant integer
2452 values (anything else should have been rejected by
2453 gcc_jit_context_new_case.
2455 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
2456 K2, and 0 if K1 and K2 are equal. */
2459 case_range_validator::case_compare (gcc::jit::recording::rvalue * k1,
2460 gcc::jit::recording::rvalue * k2)
2462 wide_int wi1 = get_wide_int (k1);
2463 wide_int wi2 = get_wide_int (k2);
2464 return wi::cmps(wi1, wi2);
2467 /* Given a const int rvalue K, get the underlying value as a wide_int. */
2469 wide_int
2470 case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k)
2472 wide_int wi;
2473 bool got_wi = k->get_wide_int (&wi);
2474 gcc_assert (got_wi);
2475 return wi;
2478 /* Public entrypoint. See description in libgccjit.h.
2480 After error-checking, the real work is done by the
2481 gcc::jit::recording::block::end_with_switch method in
2482 jit-recording.c. */
2484 void
2485 gcc_jit_block_end_with_switch (gcc_jit_block *block,
2486 gcc_jit_location *loc,
2487 gcc_jit_rvalue *expr,
2488 gcc_jit_block *default_block,
2489 int num_cases,
2490 gcc_jit_case **cases)
2492 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2493 gcc::jit::recording::context *ctxt = block->get_context ();
2494 JIT_LOG_FUNC (ctxt->get_logger ());
2495 /* LOC can be NULL. */
2496 RETURN_IF_FAIL (expr, ctxt, loc,
2497 "NULL expr");
2498 gcc::jit::recording::type *expr_type = expr->get_type ();
2499 RETURN_IF_FAIL_PRINTF2 (
2500 expr_type->is_int (),
2501 ctxt, loc,
2502 "expr: %s (type: %s) is not of integer type",
2503 expr->get_debug_string (),
2504 expr_type->get_debug_string ());
2505 if (!valid_dest_for_switch (ctxt, loc,
2506 __func__,
2507 block,
2508 default_block,
2509 "default_block"))
2510 return;
2511 RETURN_IF_FAIL (num_cases >= 0, ctxt, loc, "num_cases < 0");
2512 case_range_validator crv (ctxt, loc, __func__);
2513 for (int i = 0; i < num_cases; i++)
2515 char case_desc[32];
2516 snprintf (case_desc, sizeof (case_desc),
2517 "cases[%i]", i);
2518 if (!valid_case_for_switch (ctxt, loc,
2519 __func__,
2520 block,
2521 expr,
2522 cases[i],
2523 case_desc,
2525 return;
2526 if (!crv.validate (cases[i], i))
2527 return;
2530 block->end_with_switch (loc, expr, default_block,
2531 num_cases,
2532 (gcc::jit::recording::case_ **)cases);
2535 /**********************************************************************
2536 Option-management
2537 **********************************************************************/
2539 /* Public entrypoint. See description in libgccjit.h.
2541 After error-checking, the real work is done by the
2542 gcc::jit::recording::context::set_str_option method in
2543 jit-recording.c. */
2545 void
2546 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2547 enum gcc_jit_str_option opt,
2548 const char *value)
2550 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2551 JIT_LOG_FUNC (ctxt->get_logger ());
2552 /* opt is checked by the inner function.
2553 value can be NULL. */
2555 ctxt->set_str_option (opt, value);
2558 /* Public entrypoint. See description in libgccjit.h.
2560 After error-checking, the real work is done by the
2561 gcc::jit::recording::context::set_int_option method in
2562 jit-recording.c. */
2564 void
2565 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2566 enum gcc_jit_int_option opt,
2567 int value)
2569 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2570 JIT_LOG_FUNC (ctxt->get_logger ());
2571 /* opt is checked by the inner function. */
2573 ctxt->set_int_option (opt, value);
2576 /* Public entrypoint. See description in libgccjit.h.
2578 After error-checking, the real work is done by the
2579 gcc::jit::recording::context::set_bool_option method in
2580 jit-recording.c. */
2582 void
2583 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2584 enum gcc_jit_bool_option opt,
2585 int value)
2587 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2588 JIT_LOG_FUNC (ctxt->get_logger ());
2589 /* opt is checked by the inner function. */
2591 ctxt->set_bool_option (opt, value);
2594 /* Public entrypoint. See description in libgccjit.h.
2596 After error-checking, the real work is done by the
2597 gcc::jit::recording::context::set_inner_bool_option method in
2598 jit-recording.c. */
2600 void
2601 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
2602 int bool_value)
2604 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2605 JIT_LOG_FUNC (ctxt->get_logger ());
2606 ctxt->set_inner_bool_option (
2607 gcc::jit::INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
2608 bool_value);
2611 /* Public entrypoint. See description in libgccjit.h.
2613 After error-checking, the real work is done by the
2614 gcc::jit::recording::context::set_inner_bool_option method in
2615 jit-recording.c. */
2617 extern void
2618 gcc_jit_context_set_bool_use_external_driver (gcc_jit_context *ctxt,
2619 int bool_value)
2621 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2622 JIT_LOG_FUNC (ctxt->get_logger ());
2623 ctxt->set_inner_bool_option (
2624 gcc::jit::INNER_BOOL_OPTION_USE_EXTERNAL_DRIVER,
2625 bool_value);
2628 /* Public entrypoint. See description in libgccjit.h.
2630 After error-checking, the real work is done by the
2631 gcc::jit::recording::context::add_command_line_option method in
2632 jit-recording.c. */
2634 void
2635 gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
2636 const char *optname)
2638 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2639 JIT_LOG_FUNC (ctxt->get_logger ());
2640 RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
2641 if (ctxt->get_logger ())
2642 ctxt->get_logger ()->log ("optname: %s", optname);
2644 ctxt->add_command_line_option (optname);
2647 /* Public entrypoint. See description in libgccjit.h.
2649 After error-checking, the real work is done by the
2650 gcc::jit::recording::context::enable_dump method in
2651 jit-recording.c. */
2653 void
2654 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2655 const char *dumpname,
2656 char **out_ptr)
2658 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2659 JIT_LOG_FUNC (ctxt->get_logger ());
2660 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2661 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2663 ctxt->enable_dump (dumpname, out_ptr);
2666 /* Public entrypoint. See description in libgccjit.h.
2668 After error-checking, the real work is done by the
2669 gcc::jit::recording::context::compile method in
2670 jit-recording.c. */
2672 gcc_jit_result *
2673 gcc_jit_context_compile (gcc_jit_context *ctxt)
2675 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2677 JIT_LOG_FUNC (ctxt->get_logger ());
2679 ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
2681 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2683 ctxt->log ("%s: returning (gcc_jit_result *)%p",
2684 __func__, (void *)result);
2686 return result;
2689 /* Public entrypoint. See description in libgccjit.h.
2691 After error-checking, the real work is done by the
2692 gcc::jit::recording::context::compile_to_file method in
2693 jit-recording.c. */
2695 void
2696 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
2697 enum gcc_jit_output_kind output_kind,
2698 const char *output_path)
2700 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2701 JIT_LOG_FUNC (ctxt->get_logger ());
2702 RETURN_IF_FAIL_PRINTF1 (
2703 ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
2704 && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
2705 ctxt, NULL,
2706 "unrecognized output_kind: %i",
2707 output_kind);
2708 RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
2710 ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
2711 ctxt->log ("output_kind: %i", output_kind);
2712 ctxt->log ("output_path: %s", output_path);
2714 ctxt->compile_to_file (output_kind, output_path);
2718 /* Public entrypoint. See description in libgccjit.h.
2720 After error-checking, the real work is done by the
2721 gcc::jit::recording::context::dump_to_file method in
2722 jit-recording.c. */
2724 void
2725 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2726 const char *path,
2727 int update_locations)
2729 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2730 JIT_LOG_FUNC (ctxt->get_logger ());
2731 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2732 ctxt->dump_to_file (path, update_locations);
2735 /* Public entrypoint. See description in libgccjit.h. */
2737 void
2738 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2739 FILE *logfile,
2740 int flags,
2741 int verbosity)
2743 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2744 JIT_LOG_FUNC (ctxt->get_logger ());
2745 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2746 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2748 gcc::jit::logger *logger;
2749 if (logfile)
2750 logger = new gcc::jit::logger (logfile, flags, verbosity);
2751 else
2752 logger = NULL;
2753 ctxt->set_logger (logger);
2756 /* Public entrypoint. See description in libgccjit.h.
2758 After error-checking, the real work is done by the
2759 gcc::jit::recording::context::dump_reproducer_to_file method in
2760 jit-recording.c. */
2762 void
2763 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
2764 const char *path)
2766 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2767 JIT_LOG_FUNC (ctxt->get_logger ());
2768 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2769 ctxt->dump_reproducer_to_file (path);
2772 /* Public entrypoint. See description in libgccjit.h.
2774 After error-checking, the real work is done by the
2775 gcc::jit::recording::context::get_first_error method in
2776 jit-recording.c. */
2778 const char *
2779 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2781 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2782 JIT_LOG_FUNC (ctxt->get_logger ());
2784 return ctxt->get_first_error ();
2787 /* Public entrypoint. See description in libgccjit.h.
2789 After error-checking, the real work is done by the
2790 gcc::jit::recording::context::get_last_error method in
2791 jit-recording.c. */
2793 const char *
2794 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2796 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2798 return ctxt->get_last_error ();
2801 /* Public entrypoint. See description in libgccjit.h.
2803 After error-checking, the real work is done by the
2804 gcc::jit::result::get_code method in jit-result.c. */
2806 void *
2807 gcc_jit_result_get_code (gcc_jit_result *result,
2808 const char *fnname)
2810 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2811 JIT_LOG_FUNC (result->get_logger ());
2812 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2814 result->log ("locating fnname: %s", fnname);
2815 void *code = result->get_code (fnname);
2816 result->log ("%s: returning (void *)%p", __func__, code);
2818 return code;
2821 /* Public entrypoint. See description in libgccjit.h.
2823 After error-checking, the real work is done by the
2824 gcc::jit::result::get_global method in jit-result.c. */
2826 void *
2827 gcc_jit_result_get_global (gcc_jit_result *result,
2828 const char *name)
2830 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2831 JIT_LOG_FUNC (result->get_logger ());
2832 RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
2834 void *global = result->get_global (name);
2835 result->log ("%s: returning (void *)%p", __func__, global);
2837 return global;
2840 /* Public entrypoint. See description in libgccjit.h.
2842 After error-checking, this is essentially a wrapper around the
2843 destructor for gcc::jit::result in jit-result.c. */
2845 void
2846 gcc_jit_result_release (gcc_jit_result *result)
2848 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2849 JIT_LOG_FUNC (result->get_logger ());
2850 result->log ("deleting result: %p", (void *)result);
2851 delete result;
2854 /**********************************************************************
2855 Timing support.
2856 **********************************************************************/
2858 /* Create a gcc_jit_timer instance, and start timing. */
2860 gcc_jit_timer *
2861 gcc_jit_timer_new (void)
2863 gcc_jit_timer *timer = new gcc_jit_timer ();
2864 timer->start (TV_TOTAL);
2865 timer->push (TV_JIT_CLIENT_CODE);
2866 return timer;
2869 /* Release a gcc_jit_timer instance. */
2871 void
2872 gcc_jit_timer_release (gcc_jit_timer *timer)
2874 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2876 delete timer;
2879 /* Associate a gcc_jit_timer instance with a context. */
2881 void
2882 gcc_jit_context_set_timer (gcc_jit_context *ctxt,
2883 gcc_jit_timer *timer)
2885 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
2886 RETURN_IF_FAIL (timer, ctxt, NULL, "NULL timer");
2888 ctxt->set_timer (timer);
2891 /* Get the timer associated with a context (if any). */
2893 gcc_jit_timer *
2894 gcc_jit_context_get_timer (gcc_jit_context *ctxt)
2896 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL ctxt");
2898 return (gcc_jit_timer *)ctxt->get_timer ();
2901 /* Push the given item onto the timing stack. */
2903 void
2904 gcc_jit_timer_push (gcc_jit_timer *timer,
2905 const char *item_name)
2907 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2908 RETURN_IF_FAIL (item_name, NULL, NULL, "NULL item_name");
2909 timer->push_client_item (item_name);
2912 /* Pop the top item from the timing stack. */
2914 void
2915 gcc_jit_timer_pop (gcc_jit_timer *timer,
2916 const char *item_name)
2918 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2920 if (item_name)
2922 const char *top_item_name = timer->get_topmost_item_name ();
2924 RETURN_IF_FAIL_PRINTF1
2925 (top_item_name, NULL, NULL,
2926 "pop of empty timing stack (attempting to pop: \"%s\")",
2927 item_name);
2929 RETURN_IF_FAIL_PRINTF2
2930 (0 == strcmp (item_name, top_item_name), NULL, NULL,
2931 "mismatching item_name:"
2932 " top of timing stack: \"%s\","
2933 " attempting to pop: \"%s\"",
2934 top_item_name,
2935 item_name);
2938 timer->pop_client_item ();
2941 /* Print timing information to the given stream about activity since
2942 the timer was started. */
2944 void
2945 gcc_jit_timer_print (gcc_jit_timer *timer,
2946 FILE *f_out)
2948 RETURN_IF_FAIL (timer, NULL, NULL, "NULL timer");
2949 RETURN_IF_FAIL (f_out, NULL, NULL, "NULL f_out");
2951 timer->pop (TV_JIT_CLIENT_CODE);
2952 timer->stop (TV_TOTAL);
2953 timer->print (f_out);
2954 timer->start (TV_TOTAL);
2955 timer->push (TV_JIT_CLIENT_CODE);