2015-07-14 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / jit / libgccjit.c
blobeee513f05c857a412e9d60e80c2134b75f3c064c
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");
546 RETURN_NULL_IF_FAIL_PRINTF2 (
547 type->has_known_size (),
548 ctxt, loc,
549 "unknown size for field \"%s\" (type: %s)",
550 name,
551 type->get_debug_string ());
553 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
556 /* Public entrypoint. See description in libgccjit.h.
558 After error-checking, this calls the trivial
559 gcc::jit::recording::memento::as_object method (a field is a
560 memento), in jit-recording.h. */
562 gcc_jit_object *
563 gcc_jit_field_as_object (gcc_jit_field *field)
565 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
567 return static_cast <gcc_jit_object *> (field->as_object ());
570 /* Public entrypoint. See description in libgccjit.h.
572 After error-checking, the real work is done by the
573 gcc::jit::recording::context::new_struct_type method,
574 immediately followed by a "set_fields" call on the resulting
575 gcc::jit::recording::compound_type *, both in jit-recording.c */
577 gcc_jit_struct *
578 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
579 gcc_jit_location *loc,
580 const char *name,
581 int num_fields,
582 gcc_jit_field **fields)
584 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
585 JIT_LOG_FUNC (ctxt->get_logger ());
586 /* LOC can be NULL. */
587 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
588 if (num_fields)
589 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
590 for (int i = 0; i < num_fields; i++)
592 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
593 RETURN_NULL_IF_FAIL_PRINTF2 (
594 NULL == fields[i]->get_container (),
595 ctxt, loc,
596 "%s is already a field of %s",
597 fields[i]->get_debug_string (),
598 fields[i]->get_container ()->get_debug_string ());
601 gcc::jit::recording::struct_ *result =
602 ctxt->new_struct_type (loc, name);
603 result->set_fields (loc,
604 num_fields,
605 (gcc::jit::recording::field **)fields);
606 return static_cast<gcc_jit_struct *> (result);
609 /* Public entrypoint. See description in libgccjit.h.
611 After error-checking, the real work is done by the
612 gcc::jit::recording::context::new_struct_type method in
613 jit-recording.c. */
615 gcc_jit_struct *
616 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
617 gcc_jit_location *loc,
618 const char *name)
620 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
621 JIT_LOG_FUNC (ctxt->get_logger ());
622 /* LOC can be NULL. */
623 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
625 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
628 /* Public entrypoint. See description in libgccjit.h.
630 After error-checking, this calls the trivial
631 gcc::jit::recording::struct_::as_object method in
632 jit-recording.h. */
634 gcc_jit_type *
635 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
637 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
639 return static_cast <gcc_jit_type *> (struct_type->as_type ());
642 /* Public entrypoint. See description in libgccjit.h.
644 After error-checking, the real work is done by the
645 gcc::jit::recording::compound_type::set_fields method in
646 jit-recording.c. */
648 void
649 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
650 gcc_jit_location *loc,
651 int num_fields,
652 gcc_jit_field **fields)
654 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
655 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
656 JIT_LOG_FUNC (ctxt->get_logger ());
657 /* LOC can be NULL. */
658 RETURN_IF_FAIL_PRINTF1 (
659 NULL == struct_type->get_fields (), ctxt, loc,
660 "%s already has had fields set",
661 struct_type->get_debug_string ());
662 if (num_fields)
663 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
664 for (int i = 0; i < num_fields; i++)
666 RETURN_IF_FAIL_PRINTF2 (
667 fields[i],
668 ctxt, loc,
669 "%s: NULL field ptr at index %i",
670 struct_type->get_debug_string (),
672 RETURN_IF_FAIL_PRINTF2 (
673 NULL == fields[i]->get_container (),
674 ctxt, loc,
675 "%s is already a field of %s",
676 fields[i]->get_debug_string (),
677 fields[i]->get_container ()->get_debug_string ());
680 struct_type->set_fields (loc, num_fields,
681 (gcc::jit::recording::field **)fields);
684 /* Public entrypoint. See description in libgccjit.h.
686 After error-checking, the real work is done by the
687 gcc::jit::recording::context::new_union_type method,
688 immediately followed by a "set_fields" call on the resulting
689 gcc::jit::recording::compound_type *, both in jit-recording.c */
691 gcc_jit_type *
692 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
693 gcc_jit_location *loc,
694 const char *name,
695 int num_fields,
696 gcc_jit_field **fields)
698 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
699 JIT_LOG_FUNC (ctxt->get_logger ());
700 /* LOC can be NULL. */
701 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
702 if (num_fields)
703 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
704 for (int i = 0; i < num_fields; i++)
706 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
707 RETURN_NULL_IF_FAIL_PRINTF2 (
708 NULL == fields[i]->get_container (),
709 ctxt, loc,
710 "%s is already a field of %s",
711 fields[i]->get_debug_string (),
712 fields[i]->get_container ()->get_debug_string ());
715 gcc::jit::recording::union_ *result =
716 ctxt->new_union_type (loc, name);
717 result->set_fields (loc,
718 num_fields,
719 (gcc::jit::recording::field **)fields);
720 return (gcc_jit_type *) (result);
723 /* Public entrypoint. See description in libgccjit.h.
725 After error-checking, the real work is done by the
726 gcc::jit::recording::context::new_function_ptr_type method,
727 in jit-recording.c */
729 gcc_jit_type *
730 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
731 gcc_jit_location *loc,
732 gcc_jit_type *return_type,
733 int num_params,
734 gcc_jit_type **param_types,
735 int is_variadic)
737 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
738 JIT_LOG_FUNC (ctxt->get_logger ());
739 /* LOC can be NULL. */
740 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
741 RETURN_NULL_IF_FAIL (
742 (num_params == 0) || param_types,
743 ctxt, loc,
744 "NULL param_types creating function pointer type");
745 for (int i = 0; i < num_params; i++)
746 RETURN_NULL_IF_FAIL_PRINTF1 (
747 param_types[i],
748 ctxt, loc,
749 "NULL parameter type %i creating function pointer type", i);
751 return (gcc_jit_type*)
752 ctxt->new_function_ptr_type (loc, return_type,
753 num_params,
754 (gcc::jit::recording::type **)param_types,
755 is_variadic);
758 /* Constructing functions. */
760 /* Public entrypoint. See description in libgccjit.h.
762 After error-checking, the real work is done by the
763 gcc::jit::recording::context::new_param method, in jit-recording.c */
765 gcc_jit_param *
766 gcc_jit_context_new_param (gcc_jit_context *ctxt,
767 gcc_jit_location *loc,
768 gcc_jit_type *type,
769 const char *name)
771 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
772 JIT_LOG_FUNC (ctxt->get_logger ());
773 /* LOC can be NULL. */
774 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
775 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
777 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
780 /* Public entrypoint. See description in libgccjit.h.
782 After error-checking, this calls the trivial
783 gcc::jit::recording::memento::as_object method (a param is a memento),
784 in jit-recording.h. */
786 gcc_jit_object *
787 gcc_jit_param_as_object (gcc_jit_param *param)
789 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
791 return static_cast <gcc_jit_object *> (param->as_object ());
794 /* Public entrypoint. See description in libgccjit.h.
796 After error-checking, this calls the trivial
797 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
799 gcc_jit_lvalue *
800 gcc_jit_param_as_lvalue (gcc_jit_param *param)
802 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
804 return (gcc_jit_lvalue *)param->as_lvalue ();
807 /* Public entrypoint. See description in libgccjit.h.
809 After error-checking, this calls the trivial
810 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
811 in jit-recording.h. */
813 gcc_jit_rvalue *
814 gcc_jit_param_as_rvalue (gcc_jit_param *param)
816 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
818 return (gcc_jit_rvalue *)param->as_rvalue ();
821 /* Public entrypoint. See description in libgccjit.h.
823 After error-checking, the real work is done by the
824 gcc::jit::recording::context::new_function method, in
825 jit-recording.c. */
827 gcc_jit_function *
828 gcc_jit_context_new_function (gcc_jit_context *ctxt,
829 gcc_jit_location *loc,
830 enum gcc_jit_function_kind kind,
831 gcc_jit_type *return_type,
832 const char *name,
833 int num_params,
834 gcc_jit_param **params,
835 int is_variadic)
837 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
838 JIT_LOG_FUNC (ctxt->get_logger ());
839 /* LOC can be NULL. */
840 RETURN_NULL_IF_FAIL_PRINTF1 (
841 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
842 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
843 ctxt, loc,
844 "unrecognized value for enum gcc_jit_function_kind: %i",
845 kind);
846 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
847 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
848 /* The assembler can only handle certain names, so for now, enforce
849 C's rules for identiers upon the name, using ISALPHA and ISALNUM
850 from safe-ctype.h to ignore the current locale.
851 Eventually we'll need some way to interact with e.g. C++ name
852 mangling. */
854 /* Leading char: */
855 char ch = *name;
856 RETURN_NULL_IF_FAIL_PRINTF2 (
857 ISALPHA (ch) || ch == '_',
858 ctxt, loc,
859 "name \"%s\" contains invalid character: '%c'",
860 name, ch);
861 /* Subsequent chars: */
862 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
864 RETURN_NULL_IF_FAIL_PRINTF2 (
865 ISALNUM (ch) || ch == '_',
866 ctxt, loc,
867 "name \"%s\" contains invalid character: '%c'",
868 name, ch);
871 RETURN_NULL_IF_FAIL_PRINTF1 (
872 (num_params == 0) || params,
873 ctxt, loc,
874 "NULL params creating function %s", name);
875 for (int i = 0; i < num_params; i++)
877 RETURN_NULL_IF_FAIL_PRINTF2 (
878 params[i],
879 ctxt, loc,
880 "NULL parameter %i creating function %s", i, name);
881 RETURN_NULL_IF_FAIL_PRINTF5 (
882 (NULL == params[i]->get_scope ()),
883 ctxt, loc,
884 "parameter %i \"%s\""
885 " (type: %s)"
886 " for function %s"
887 " was already used for function %s",
888 i, params[i]->get_debug_string (),
889 params[i]->get_type ()->get_debug_string (),
890 name,
891 params[i]->get_scope ()->get_debug_string ());
894 return (gcc_jit_function*)
895 ctxt->new_function (loc, kind, return_type, name,
896 num_params,
897 (gcc::jit::recording::param **)params,
898 is_variadic,
899 BUILT_IN_NONE);
902 /* Public entrypoint. See description in libgccjit.h.
904 After error-checking, the real work is done by the
905 gcc::jit::recording::context::get_builtin_function method, in
906 jit-recording.c. */
908 gcc_jit_function *
909 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
910 const char *name)
912 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
913 JIT_LOG_FUNC (ctxt->get_logger ());
914 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
916 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
919 /* Public entrypoint. See description in libgccjit.h.
921 After error-checking, this calls the trivial
922 gcc::jit::recording::memento::as_object method (a function is a
923 memento), in jit-recording.h. */
925 gcc_jit_object *
926 gcc_jit_function_as_object (gcc_jit_function *func)
928 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
930 return static_cast <gcc_jit_object *> (func->as_object ());
933 /* Public entrypoint. See description in libgccjit.h.
935 After error-checking, the real work is done by the
936 gcc::jit::recording::function::get_param method, in
937 jit-recording.h. */
939 gcc_jit_param *
940 gcc_jit_function_get_param (gcc_jit_function *func, int index)
942 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
943 gcc::jit::recording::context *ctxt = func->m_ctxt;
944 JIT_LOG_FUNC (ctxt->get_logger ());
945 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
946 int num_params = func->get_params ().length ();
947 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
948 ctxt, NULL,
949 "index of %d is too large (%s has %d params)",
950 index,
951 func->get_debug_string (),
952 num_params);
954 return static_cast <gcc_jit_param *> (func->get_param (index));
957 /* Public entrypoint. See description in libgccjit.h.
959 After error-checking, the real work is done by the
960 gcc::jit::recording::function::dump_to_dot method, in
961 jit-recording.c. */
963 void
964 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
965 const char *path)
967 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
968 gcc::jit::recording::context *ctxt = func->m_ctxt;
969 JIT_LOG_FUNC (ctxt->get_logger ());
970 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
972 func->dump_to_dot (path);
975 /* Public entrypoint. See description in libgccjit.h.
977 After error-checking, the real work is done by the
978 gcc::jit::recording::function::new_block method, in
979 jit-recording.c. */
981 gcc_jit_block*
982 gcc_jit_function_new_block (gcc_jit_function *func,
983 const char *name)
985 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
986 JIT_LOG_FUNC (func->get_context ()->get_logger ());
987 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
988 func->get_context (), NULL,
989 "cannot add block to an imported function");
990 /* name can be NULL. */
992 return (gcc_jit_block *)func->new_block (name);
995 /* Public entrypoint. See description in libgccjit.h.
997 After error-checking, this calls the trivial
998 gcc::jit::recording::memento::as_object method (a block is a
999 memento), in jit-recording.h. */
1001 gcc_jit_object *
1002 gcc_jit_block_as_object (gcc_jit_block *block)
1004 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1006 return static_cast <gcc_jit_object *> (block->as_object ());
1009 /* Public entrypoint. See description in libgccjit.h.
1011 After error-checking, the real work is done by the
1012 gcc::jit::recording::block::get_function method, in
1013 jit-recording.h. */
1015 gcc_jit_function *
1016 gcc_jit_block_get_function (gcc_jit_block *block)
1018 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
1020 return static_cast <gcc_jit_function *> (block->get_function ());
1023 /* Public entrypoint. See description in libgccjit.h.
1025 After error-checking, the real work is done by the
1026 gcc::jit::recording::context::new_global method, in
1027 jit-recording.c. */
1029 gcc_jit_lvalue *
1030 gcc_jit_context_new_global (gcc_jit_context *ctxt,
1031 gcc_jit_location *loc,
1032 enum gcc_jit_global_kind kind,
1033 gcc_jit_type *type,
1034 const char *name)
1036 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1037 JIT_LOG_FUNC (ctxt->get_logger ());
1038 /* LOC can be NULL. */
1039 RETURN_NULL_IF_FAIL_PRINTF1 (
1040 ((kind >= GCC_JIT_GLOBAL_EXPORTED)
1041 && (kind <= GCC_JIT_GLOBAL_IMPORTED)),
1042 ctxt, loc,
1043 "unrecognized value for enum gcc_jit_global_kind: %i",
1044 kind);
1045 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1046 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1047 RETURN_NULL_IF_FAIL_PRINTF2 (
1048 type->has_known_size (),
1049 ctxt, loc,
1050 "unknown size for global \"%s\" (type: %s)",
1051 name,
1052 type->get_debug_string ());
1054 return (gcc_jit_lvalue *)ctxt->new_global (loc, kind, type, name);
1057 /* Public entrypoint. See description in libgccjit.h.
1059 After error-checking, this calls the trivial
1060 gcc::jit::recording::memento::as_object method (an lvalue is a
1061 memento), in jit-recording.h. */
1063 gcc_jit_object *
1064 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
1066 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1068 return static_cast <gcc_jit_object *> (lvalue->as_object ());
1071 /* Public entrypoint. See description in libgccjit.h.
1073 After error-checking, this calls the trivial
1074 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
1076 gcc_jit_rvalue *
1077 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
1079 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
1081 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1084 /* Public entrypoint. See description in libgccjit.h.
1086 After error-checking, this calls the trivial
1087 gcc::jit::recording::memento::as_object method (an rvalue is a
1088 memento), in jit-recording.h. */
1090 gcc_jit_object *
1091 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1093 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1095 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1098 /* Public entrypoint. See description in libgccjit.h.
1100 After error-checking, the real work is done by the
1101 gcc::jit::recording::rvalue::get_type method, in
1102 jit-recording.h. */
1104 gcc_jit_type *
1105 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1107 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1109 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1112 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1113 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1114 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1116 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1117 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1118 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1119 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1120 "not a numeric type: %s", \
1121 NUMERIC_TYPE->get_debug_string ());
1123 /* Public entrypoint. See description in libgccjit.h.
1125 After error-checking, the real work is done by the
1126 gcc::jit::recording::context::new_rvalue_from_int method in
1127 jit-recording.c. */
1129 gcc_jit_rvalue *
1130 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1131 gcc_jit_type *numeric_type,
1132 int value)
1134 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1135 JIT_LOG_FUNC (ctxt->get_logger ());
1136 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1138 return ((gcc_jit_rvalue *)ctxt
1139 ->new_rvalue_from_const <int> (numeric_type, value));
1142 /* FIXME. */
1144 gcc_jit_rvalue *
1145 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1146 gcc_jit_type *numeric_type,
1147 long value)
1149 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1150 JIT_LOG_FUNC (ctxt->get_logger ());
1151 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1153 return ((gcc_jit_rvalue *)ctxt
1154 ->new_rvalue_from_const <long> (numeric_type, value));
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, 0);
1161 albeit with slightly different error messages if an error occurs. */
1163 gcc_jit_rvalue *
1164 gcc_jit_context_zero (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, 0);
1174 /* Public entrypoint. See description in libgccjit.h.
1176 This is essentially equivalent to:
1177 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1178 albeit with slightly different error messages if an error occurs. */
1180 gcc_jit_rvalue *
1181 gcc_jit_context_one (gcc_jit_context *ctxt,
1182 gcc_jit_type *numeric_type)
1184 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1185 JIT_LOG_FUNC (ctxt->get_logger ());
1186 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1188 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1191 /* Public entrypoint. See description in libgccjit.h.
1193 After error-checking, the real work is done by the
1194 gcc::jit::recording::context::new_rvalue_from_double method in
1195 jit-recording.c. */
1197 gcc_jit_rvalue *
1198 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1199 gcc_jit_type *numeric_type,
1200 double value)
1202 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1203 JIT_LOG_FUNC (ctxt->get_logger ());
1204 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1206 return ((gcc_jit_rvalue *)ctxt
1207 ->new_rvalue_from_const <double> (numeric_type, value));
1210 /* Public entrypoint. See description in libgccjit.h.
1212 After error-checking, the real work is done by the
1213 gcc::jit::recording::context::new_rvalue_from_ptr method in
1214 jit-recording.c. */
1216 gcc_jit_rvalue *
1217 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1218 gcc_jit_type *pointer_type,
1219 void *value)
1221 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1222 JIT_LOG_FUNC (ctxt->get_logger ());
1223 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1224 RETURN_NULL_IF_FAIL_PRINTF1 (
1225 pointer_type->is_pointer (),
1226 ctxt, NULL,
1227 "not a pointer type (type: %s)",
1228 pointer_type->get_debug_string ());
1230 return ((gcc_jit_rvalue *)ctxt
1231 ->new_rvalue_from_const <void *> (pointer_type, value));
1234 /* Public entrypoint. See description in libgccjit.h.
1236 This is essentially equivalent to:
1237 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1238 albeit with slightly different error messages if an error occurs. */
1240 gcc_jit_rvalue *
1241 gcc_jit_context_null (gcc_jit_context *ctxt,
1242 gcc_jit_type *pointer_type)
1244 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1245 JIT_LOG_FUNC (ctxt->get_logger ());
1246 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1247 RETURN_NULL_IF_FAIL_PRINTF1 (
1248 pointer_type->is_pointer (),
1249 ctxt, NULL,
1250 "not a pointer type (type: %s)",
1251 pointer_type->get_debug_string ());
1253 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
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_string_literal method in
1260 jit-recording.c. */
1262 gcc_jit_rvalue *
1263 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1264 const char *value)
1266 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1267 JIT_LOG_FUNC (ctxt->get_logger ());
1268 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1270 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1273 /* Public entrypoint. See description in libgccjit.h.
1275 After error-checking, the real work is done by the
1276 gcc::jit::recording::context::new_unary_op method in
1277 jit-recording.c. */
1279 gcc_jit_rvalue *
1280 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1281 gcc_jit_location *loc,
1282 enum gcc_jit_unary_op op,
1283 gcc_jit_type *result_type,
1284 gcc_jit_rvalue *rvalue)
1286 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1287 JIT_LOG_FUNC (ctxt->get_logger ());
1288 /* LOC can be NULL. */
1289 RETURN_NULL_IF_FAIL_PRINTF1 (
1290 (op >= GCC_JIT_UNARY_OP_MINUS
1291 && op <= GCC_JIT_UNARY_OP_ABS),
1292 ctxt, loc,
1293 "unrecognized value for enum gcc_jit_unary_op: %i",
1294 op);
1295 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1296 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1298 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1301 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1302 For use by both gcc_jit_context_new_binary_op and
1303 gcc_jit_block_add_assignment_op. */
1305 static bool
1306 valid_binary_op_p (enum gcc_jit_binary_op op)
1308 return (op >= GCC_JIT_BINARY_OP_PLUS
1309 && op <= GCC_JIT_BINARY_OP_RSHIFT);
1312 /* Public entrypoint. See description in libgccjit.h.
1314 After error-checking, the real work is done by the
1315 gcc::jit::recording::context::new_binary_op method in
1316 jit-recording.c. */
1318 gcc_jit_rvalue *
1319 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1320 gcc_jit_location *loc,
1321 enum gcc_jit_binary_op op,
1322 gcc_jit_type *result_type,
1323 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1325 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1326 JIT_LOG_FUNC (ctxt->get_logger ());
1327 /* LOC can be NULL. */
1328 RETURN_NULL_IF_FAIL_PRINTF1 (
1329 valid_binary_op_p (op),
1330 ctxt, loc,
1331 "unrecognized value for enum gcc_jit_binary_op: %i",
1332 op);
1333 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1334 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1335 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1336 RETURN_NULL_IF_FAIL_PRINTF4 (
1337 a->get_type () == b->get_type (),
1338 ctxt, loc,
1339 "mismatching types for binary op:"
1340 " a: %s (type: %s) b: %s (type: %s)",
1341 a->get_debug_string (),
1342 a->get_type ()->get_debug_string (),
1343 b->get_debug_string (),
1344 b->get_type ()->get_debug_string ());
1346 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1349 /* Public entrypoint. See description in libgccjit.h.
1351 After error-checking, the real work is done by the
1352 gcc::jit::recording::context::new_comparison method in
1353 jit-recording.c. */
1355 gcc_jit_rvalue *
1356 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1357 gcc_jit_location *loc,
1358 enum gcc_jit_comparison op,
1359 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1361 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1362 JIT_LOG_FUNC (ctxt->get_logger ());
1363 /* LOC can be NULL. */
1364 RETURN_NULL_IF_FAIL_PRINTF1 (
1365 (op >= GCC_JIT_COMPARISON_EQ
1366 && op <= GCC_JIT_COMPARISON_GE),
1367 ctxt, loc,
1368 "unrecognized value for enum gcc_jit_comparison: %i",
1369 op);
1370 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1371 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1372 RETURN_NULL_IF_FAIL_PRINTF4 (
1373 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1374 ctxt, loc,
1375 "mismatching types for comparison:"
1376 " a: %s (type: %s) b: %s (type: %s)",
1377 a->get_debug_string (),
1378 a->get_type ()->get_debug_string (),
1379 b->get_debug_string (),
1380 b->get_type ()->get_debug_string ());
1382 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1385 /* Public entrypoint. See description in libgccjit.h.
1387 After error-checking, the real work is done by the
1388 gcc::jit::recording::context::new_call method in
1389 jit-recording.c. */
1391 gcc_jit_rvalue *
1392 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1393 gcc_jit_location *loc,
1394 gcc_jit_function *func,
1395 int numargs , gcc_jit_rvalue **args)
1397 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1398 JIT_LOG_FUNC (ctxt->get_logger ());
1399 /* LOC can be NULL. */
1400 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1401 if (numargs)
1402 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1404 int min_num_params = func->get_params ().length ();
1405 bool is_variadic = func->is_variadic ();
1407 RETURN_NULL_IF_FAIL_PRINTF3 (
1408 numargs >= min_num_params,
1409 ctxt, loc,
1410 "not enough arguments to function \"%s\""
1411 " (got %i args, expected %i)",
1412 func->get_name ()->c_str (),
1413 numargs, min_num_params);
1415 RETURN_NULL_IF_FAIL_PRINTF3 (
1416 (numargs == min_num_params || is_variadic),
1417 ctxt, loc,
1418 "too many arguments to function \"%s\""
1419 " (got %i args, expected %i)",
1420 func->get_name ()->c_str (),
1421 numargs, min_num_params);
1423 for (int i = 0; i < min_num_params; i++)
1425 gcc::jit::recording::param *param = func->get_param (i);
1426 gcc_jit_rvalue *arg = args[i];
1428 RETURN_NULL_IF_FAIL_PRINTF4 (
1429 arg,
1430 ctxt, loc,
1431 "NULL argument %i to function \"%s\":"
1432 " param %s (type: %s)",
1433 i + 1,
1434 func->get_name ()->c_str (),
1435 param->get_debug_string (),
1436 param->get_type ()->get_debug_string ());
1438 RETURN_NULL_IF_FAIL_PRINTF6 (
1439 compatible_types (param->get_type (),
1440 arg->get_type ()),
1441 ctxt, loc,
1442 "mismatching types for argument %d of function \"%s\":"
1443 " assignment to param %s (type: %s) from %s (type: %s)",
1444 i + 1,
1445 func->get_name ()->c_str (),
1446 param->get_debug_string (),
1447 param->get_type ()->get_debug_string (),
1448 arg->get_debug_string (),
1449 arg->get_type ()->get_debug_string ());
1452 return (gcc_jit_rvalue *)ctxt->new_call (loc,
1453 func,
1454 numargs,
1455 (gcc::jit::recording::rvalue **)args);
1458 /* Public entrypoint. See description in libgccjit.h.
1460 After error-checking, the real work is done by the
1461 gcc::jit::recording::context::new_call_through_ptr method in
1462 jit-recording.c. */
1464 gcc_jit_rvalue *
1465 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1466 gcc_jit_location *loc,
1467 gcc_jit_rvalue *fn_ptr,
1468 int numargs, gcc_jit_rvalue **args)
1470 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1471 JIT_LOG_FUNC (ctxt->get_logger ());
1472 /* LOC can be NULL. */
1473 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1474 if (numargs)
1475 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1477 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1478 RETURN_NULL_IF_FAIL_PRINTF2 (
1479 ptr_type, ctxt, loc,
1480 "fn_ptr is not a ptr: %s"
1481 " type: %s",
1482 fn_ptr->get_debug_string (),
1483 fn_ptr->get_type ()->get_debug_string ());
1485 gcc::jit::recording::function_type *fn_type =
1486 ptr_type->dyn_cast_function_type();
1487 RETURN_NULL_IF_FAIL_PRINTF2 (
1488 fn_type, ctxt, loc,
1489 "fn_ptr is not a function ptr: %s"
1490 " type: %s",
1491 fn_ptr->get_debug_string (),
1492 fn_ptr->get_type ()->get_debug_string ());
1494 int min_num_params = fn_type->get_param_types ().length ();
1495 bool is_variadic = fn_type->is_variadic ();
1497 RETURN_NULL_IF_FAIL_PRINTF3 (
1498 numargs >= min_num_params,
1499 ctxt, loc,
1500 "not enough arguments to fn_ptr: %s"
1501 " (got %i args, expected %i)",
1502 fn_ptr->get_debug_string (),
1503 numargs, min_num_params);
1505 RETURN_NULL_IF_FAIL_PRINTF3 (
1506 (numargs == min_num_params || is_variadic),
1507 ctxt, loc,
1508 "too many arguments to fn_ptr: %s"
1509 " (got %i args, expected %i)",
1510 fn_ptr->get_debug_string (),
1511 numargs, min_num_params);
1513 for (int i = 0; i < min_num_params; i++)
1515 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1516 gcc_jit_rvalue *arg = args[i];
1518 RETURN_NULL_IF_FAIL_PRINTF3 (
1519 arg,
1520 ctxt, loc,
1521 "NULL argument %i to fn_ptr: %s"
1522 " (type: %s)",
1523 i + 1,
1524 fn_ptr->get_debug_string (),
1525 param_type->get_debug_string ());
1527 RETURN_NULL_IF_FAIL_PRINTF6 (
1528 compatible_types (param_type,
1529 arg->get_type ()),
1530 ctxt, loc,
1531 "mismatching types for argument %d of fn_ptr: %s:"
1532 " assignment to param %d (type: %s) from %s (type: %s)",
1533 i + 1,
1534 fn_ptr->get_debug_string (),
1535 i + 1,
1536 param_type->get_debug_string (),
1537 arg->get_debug_string (),
1538 arg->get_type ()->get_debug_string ());
1541 return (gcc_jit_rvalue *)(
1542 ctxt->new_call_through_ptr (loc,
1543 fn_ptr,
1544 numargs,
1545 (gcc::jit::recording::rvalue **)args));
1548 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1549 to DST_TYPE, for use by gcc_jit_context_new_cast.
1551 We only permit these kinds of cast:
1553 int <-> float
1554 int <-> bool
1555 P* <-> Q* for pointer types P and Q. */
1557 static bool
1558 is_valid_cast (gcc::jit::recording::type *src_type,
1559 gcc_jit_type *dst_type)
1561 bool src_is_int = src_type->is_int ();
1562 bool dst_is_int = dst_type->is_int ();
1563 bool src_is_float = src_type->is_float ();
1564 bool dst_is_float = dst_type->is_float ();
1565 bool src_is_bool = src_type->is_bool ();
1566 bool dst_is_bool = dst_type->is_bool ();
1568 if (src_is_int)
1569 if (dst_is_int || dst_is_float || dst_is_bool)
1570 return true;
1572 if (src_is_float)
1573 if (dst_is_int || dst_is_float)
1574 return true;
1576 if (src_is_bool)
1577 if (dst_is_int || dst_is_bool)
1578 return true;
1580 /* Permit casts between pointer types. */
1581 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1582 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1583 if (deref_src_type && deref_dst_type)
1584 return true;
1586 return false;
1589 /* Public entrypoint. See description in libgccjit.h.
1591 After error-checking, the real work is done by the
1592 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1594 gcc_jit_rvalue *
1595 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1596 gcc_jit_location *loc,
1597 gcc_jit_rvalue *rvalue,
1598 gcc_jit_type *type)
1600 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1601 JIT_LOG_FUNC (ctxt->get_logger ());
1602 /* LOC can be NULL. */
1603 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1604 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1605 RETURN_NULL_IF_FAIL_PRINTF3 (
1606 is_valid_cast (rvalue->get_type (), type),
1607 ctxt, loc,
1608 "cannot cast %s from type: %s to type: %s",
1609 rvalue->get_debug_string (),
1610 rvalue->get_type ()->get_debug_string (),
1611 type->get_debug_string ());
1613 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1616 /* Public entrypoint. See description in libgccjit.h.
1618 After error-checking, the real work is done by the
1619 gcc::jit::recording::context::new_array_access method in
1620 jit-recording.c. */
1622 extern gcc_jit_lvalue *
1623 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1624 gcc_jit_location *loc,
1625 gcc_jit_rvalue *ptr,
1626 gcc_jit_rvalue *index)
1628 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1629 JIT_LOG_FUNC (ctxt->get_logger ());
1630 /* LOC can be NULL. */
1631 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1632 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1633 RETURN_NULL_IF_FAIL_PRINTF2 (
1634 ptr->get_type ()->dereference (),
1635 ctxt, loc,
1636 "ptr: %s (type: %s) is not a pointer or array",
1637 ptr->get_debug_string (),
1638 ptr->get_type ()->get_debug_string ());
1639 RETURN_NULL_IF_FAIL_PRINTF2 (
1640 index->get_type ()->is_numeric (),
1641 ctxt, loc,
1642 "index: %s (type: %s) is not of numeric type",
1643 index->get_debug_string (),
1644 index->get_type ()->get_debug_string ());
1646 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1649 /* Public entrypoint. See description in libgccjit.h.
1651 After error-checking, the real work is done by the
1652 gcc::jit::recording::memento::get_context method in
1653 jit-recording.h. */
1655 gcc_jit_context *
1656 gcc_jit_object_get_context (gcc_jit_object *obj)
1658 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1660 return static_cast <gcc_jit_context *> (obj->get_context ());
1663 /* Public entrypoint. See description in libgccjit.h.
1665 After error-checking, the real work is done by the
1666 gcc::jit::recording::memento::get_debug_string method in
1667 jit-recording.c. */
1669 const char *
1670 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1672 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1674 return obj->get_debug_string ();
1677 /* Public entrypoint. See description in libgccjit.h.
1679 After error-checking, the real work is done by the
1680 gcc::jit::recording::lvalue::access_field method in
1681 jit-recording.c. */
1683 gcc_jit_lvalue *
1684 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1685 gcc_jit_location *loc,
1686 gcc_jit_field *field)
1688 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1689 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1690 JIT_LOG_FUNC (ctxt->get_logger ());
1691 /* LOC can be NULL. */
1692 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1693 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1694 "field %s has not been placed in a struct",
1695 field->get_debug_string ());
1696 gcc::jit::recording::type *underlying_type =
1697 struct_->get_type ();
1698 RETURN_NULL_IF_FAIL_PRINTF2 (
1699 (field->get_container ()->unqualified ()
1700 == underlying_type->unqualified ()),
1701 struct_->m_ctxt, loc,
1702 "%s is not a field of %s",
1703 field->get_debug_string (),
1704 underlying_type->get_debug_string ());
1706 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1709 /* Public entrypoint. See description in libgccjit.h.
1711 After error-checking, the real work is done by the
1712 gcc::jit::recording::rvalue::access_field method in
1713 jit-recording.c. */
1715 gcc_jit_rvalue *
1716 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1717 gcc_jit_location *loc,
1718 gcc_jit_field *field)
1720 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1721 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1722 JIT_LOG_FUNC (ctxt->get_logger ());
1723 /* LOC can be NULL. */
1724 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1725 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1726 "field %s has not been placed in a struct",
1727 field->get_debug_string ());
1728 gcc::jit::recording::type *underlying_type =
1729 struct_->get_type ();
1730 RETURN_NULL_IF_FAIL_PRINTF2 (
1731 (field->get_container ()->unqualified ()
1732 == underlying_type->unqualified ()),
1733 struct_->m_ctxt, loc,
1734 "%s is not a field of %s",
1735 field->get_debug_string (),
1736 underlying_type->get_debug_string ());
1738 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1741 /* Public entrypoint. See description in libgccjit.h.
1743 After error-checking, the real work is done by the
1744 gcc::jit::recording::rvalue::deference_field method in
1745 jit-recording.c. */
1747 gcc_jit_lvalue *
1748 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1749 gcc_jit_location *loc,
1750 gcc_jit_field *field)
1752 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1753 JIT_LOG_FUNC (ptr->get_context ()->get_logger ());
1754 /* LOC can be NULL. */
1755 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1756 gcc::jit::recording::type *underlying_type =
1757 ptr->get_type ()->is_pointer ();
1758 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1759 "field %s has not been placed in a struct",
1760 field->get_debug_string ());
1761 RETURN_NULL_IF_FAIL_PRINTF3 (
1762 underlying_type,
1763 ptr->m_ctxt, loc,
1764 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1765 ptr->get_debug_string (),
1766 ptr->get_type ()->get_debug_string (),
1767 field->get_debug_string ());
1768 RETURN_NULL_IF_FAIL_PRINTF2 (
1769 (field->get_container ()->unqualified ()
1770 == underlying_type->unqualified ()),
1771 ptr->m_ctxt, loc,
1772 "%s is not a field of %s",
1773 field->get_debug_string (),
1774 underlying_type->get_debug_string ());
1776 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1779 /* Public entrypoint. See description in libgccjit.h.
1781 After error-checking, the real work is done by the
1782 gcc::jit::recording::rvalue::deference method in
1783 jit-recording.c. */
1785 gcc_jit_lvalue *
1786 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1787 gcc_jit_location *loc)
1789 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1790 JIT_LOG_FUNC (rvalue->get_context ()->get_logger ());
1791 /* LOC can be NULL. */
1793 gcc::jit::recording::type *underlying_type =
1794 rvalue->get_type ()->is_pointer ();
1796 RETURN_NULL_IF_FAIL_PRINTF2 (
1797 underlying_type,
1798 rvalue->m_ctxt, loc,
1799 "dereference of non-pointer %s (type: %s)",
1800 rvalue->get_debug_string (),
1801 rvalue->get_type ()->get_debug_string ());
1803 RETURN_NULL_IF_FAIL_PRINTF2 (
1804 !underlying_type->is_void (),
1805 rvalue->m_ctxt, loc,
1806 "dereference of void pointer %s (type: %s)",
1807 rvalue->get_debug_string (),
1808 rvalue->get_type ()->get_debug_string ());
1810 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1813 /* Public entrypoint. See description in libgccjit.h.
1815 After error-checking, the real work is done by the
1816 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1818 gcc_jit_rvalue *
1819 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1820 gcc_jit_location *loc)
1822 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1823 JIT_LOG_FUNC (lvalue->get_context ()->get_logger ());
1824 /* LOC can be NULL. */
1826 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1829 /* Public entrypoint. See description in libgccjit.h.
1831 After error-checking, the real work is done by the
1832 gcc::jit::recording::function::new_local method in jit-recording.c. */
1834 gcc_jit_lvalue *
1835 gcc_jit_function_new_local (gcc_jit_function *func,
1836 gcc_jit_location *loc,
1837 gcc_jit_type *type,
1838 const char *name)
1840 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1841 gcc::jit::recording::context *ctxt = func->m_ctxt;
1842 JIT_LOG_FUNC (ctxt->get_logger ());
1843 /* LOC can be NULL. */
1844 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1845 ctxt, loc,
1846 "Cannot add locals to an imported function");
1847 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1848 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1849 RETURN_NULL_IF_FAIL_PRINTF2 (
1850 type->has_known_size (),
1851 ctxt, loc,
1852 "unknown size for local \"%s\" (type: %s)",
1853 name,
1854 type->get_debug_string ());
1856 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1859 /* Public entrypoint. See description in libgccjit.h.
1861 After error-checking, the real work is done by the
1862 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1864 void
1865 gcc_jit_block_add_eval (gcc_jit_block *block,
1866 gcc_jit_location *loc,
1867 gcc_jit_rvalue *rvalue)
1869 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1870 gcc::jit::recording::context *ctxt = block->get_context ();
1871 JIT_LOG_FUNC (ctxt->get_logger ());
1872 /* LOC can be NULL. */
1873 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1875 gcc::jit::recording::statement *stmt = block->add_eval (loc, rvalue);
1877 /* "stmt" should be good enough to be usable in error-messages,
1878 but might still not be compilable; perform some more
1879 error-checking here. We do this here so that the error messages
1880 can contain a stringified version of "stmt", whilst appearing
1881 as close as possible to the point of failure. */
1882 rvalue->verify_valid_within_stmt (__func__, stmt);
1885 /* Public entrypoint. See description in libgccjit.h.
1887 After error-checking, the real work is done by the
1888 gcc::jit::recording::block::add_assignment method in
1889 jit-recording.c. */
1891 void
1892 gcc_jit_block_add_assignment (gcc_jit_block *block,
1893 gcc_jit_location *loc,
1894 gcc_jit_lvalue *lvalue,
1895 gcc_jit_rvalue *rvalue)
1897 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1898 gcc::jit::recording::context *ctxt = block->get_context ();
1899 JIT_LOG_FUNC (ctxt->get_logger ());
1900 /* LOC can be NULL. */
1901 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1902 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1903 RETURN_IF_FAIL_PRINTF4 (
1904 compatible_types (lvalue->get_type (),
1905 rvalue->get_type ()),
1906 ctxt, loc,
1907 "mismatching types:"
1908 " assignment to %s (type: %s) from %s (type: %s)",
1909 lvalue->get_debug_string (),
1910 lvalue->get_type ()->get_debug_string (),
1911 rvalue->get_debug_string (),
1912 rvalue->get_type ()->get_debug_string ());
1914 gcc::jit::recording::statement *stmt = block->add_assignment (loc, lvalue, rvalue);
1916 /* "stmt" should be good enough to be usable in error-messages,
1917 but might still not be compilable; perform some more
1918 error-checking here. We do this here so that the error messages
1919 can contain a stringified version of "stmt", whilst appearing
1920 as close as possible to the point of failure. */
1921 lvalue->verify_valid_within_stmt (__func__, stmt);
1922 rvalue->verify_valid_within_stmt (__func__, stmt);
1925 /* Public entrypoint. See description in libgccjit.h.
1927 After error-checking, the real work is done by the
1928 gcc::jit::recording::block::add_assignment_op method in
1929 jit-recording.c. */
1931 void
1932 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1933 gcc_jit_location *loc,
1934 gcc_jit_lvalue *lvalue,
1935 enum gcc_jit_binary_op op,
1936 gcc_jit_rvalue *rvalue)
1938 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1939 gcc::jit::recording::context *ctxt = block->get_context ();
1940 JIT_LOG_FUNC (ctxt->get_logger ());
1941 /* LOC can be NULL. */
1942 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1943 RETURN_IF_FAIL_PRINTF1 (
1944 valid_binary_op_p (op),
1945 ctxt, loc,
1946 "unrecognized value for enum gcc_jit_binary_op: %i",
1947 op);
1948 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1949 RETURN_IF_FAIL_PRINTF4 (
1950 compatible_types (lvalue->get_type (),
1951 rvalue->get_type ()),
1952 ctxt, loc,
1953 "mismatching types:"
1954 " assignment to %s (type: %s) involving %s (type: %s)",
1955 lvalue->get_debug_string (),
1956 lvalue->get_type ()->get_debug_string (),
1957 rvalue->get_debug_string (),
1958 rvalue->get_type ()->get_debug_string ());
1960 gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
1962 /* "stmt" should be good enough to be usable in error-messages,
1963 but might still not be compilable; perform some more
1964 error-checking here. We do this here so that the error messages
1965 can contain a stringified version of "stmt", whilst appearing
1966 as close as possible to the point of failure. */
1967 lvalue->verify_valid_within_stmt (__func__, stmt);
1968 rvalue->verify_valid_within_stmt (__func__, stmt);
1971 /* Internal helper function for determining if rvalue BOOLVAL is of
1972 boolean type. For use by gcc_jit_block_end_with_conditional. */
1974 static bool
1975 is_bool (gcc_jit_rvalue *boolval)
1977 gcc::jit::recording::type *actual_type = boolval->get_type ();
1978 gcc::jit::recording::type *bool_type =
1979 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1980 return actual_type == bool_type;
1983 /* Public entrypoint. See description in libgccjit.h.
1985 After error-checking, the real work is done by the
1986 gcc::jit::recording::block::end_with_conditional method in
1987 jit-recording.c. */
1989 void
1990 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1991 gcc_jit_location *loc,
1992 gcc_jit_rvalue *boolval,
1993 gcc_jit_block *on_true,
1994 gcc_jit_block *on_false)
1996 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1997 gcc::jit::recording::context *ctxt = block->get_context ();
1998 JIT_LOG_FUNC (ctxt->get_logger ());
1999 /* LOC can be NULL. */
2000 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
2001 RETURN_IF_FAIL_PRINTF2 (
2002 is_bool (boolval), ctxt, loc,
2003 "%s (type: %s) is not of boolean type ",
2004 boolval->get_debug_string (),
2005 boolval->get_type ()->get_debug_string ());
2006 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
2007 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
2008 RETURN_IF_FAIL_PRINTF4 (
2009 block->get_function () == on_true->get_function (),
2010 ctxt, loc,
2011 "\"on_true\" block is not in same function:"
2012 " source block %s is in function %s"
2013 " whereas target block %s is in function %s",
2014 block->get_debug_string (),
2015 block->get_function ()->get_debug_string (),
2016 on_true->get_debug_string (),
2017 on_true->get_function ()->get_debug_string ());
2018 RETURN_IF_FAIL_PRINTF4 (
2019 block->get_function () == on_false->get_function (),
2020 ctxt, loc,
2021 "\"on_false\" block is not in same function:"
2022 " source block %s is in function %s"
2023 " whereas target block %s is in function %s",
2024 block->get_debug_string (),
2025 block->get_function ()->get_debug_string (),
2026 on_false->get_debug_string (),
2027 on_false->get_function ()->get_debug_string ());
2029 gcc::jit::recording::statement *stmt = block->end_with_conditional (loc, boolval, on_true, on_false);
2031 /* "stmt" should be good enough to be usable in error-messages,
2032 but might still not be compilable; perform some more
2033 error-checking here. We do this here so that the error messages
2034 can contain a stringified version of "stmt", whilst appearing
2035 as close as possible to the point of failure. */
2036 boolval->verify_valid_within_stmt (__func__, stmt);
2039 /* Public entrypoint. See description in libgccjit.h.
2041 After error-checking, the real work is done by the
2042 gcc::jit::recording::block::add_comment method in
2043 jit-recording.c. */
2045 void
2046 gcc_jit_block_add_comment (gcc_jit_block *block,
2047 gcc_jit_location *loc,
2048 const char *text)
2050 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2051 gcc::jit::recording::context *ctxt = block->get_context ();
2052 JIT_LOG_FUNC (ctxt->get_logger ());
2053 /* LOC can be NULL. */
2054 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
2056 block->add_comment (loc, text);
2059 /* Public entrypoint. See description in libgccjit.h.
2061 After error-checking, the real work is done by the
2062 gcc::jit::recording::block::end_with_jump method in
2063 jit-recording.c. */
2065 void
2066 gcc_jit_block_end_with_jump (gcc_jit_block *block,
2067 gcc_jit_location *loc,
2068 gcc_jit_block *target)
2070 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2071 gcc::jit::recording::context *ctxt = block->get_context ();
2072 JIT_LOG_FUNC (ctxt->get_logger ());
2073 /* LOC can be NULL. */
2074 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
2075 RETURN_IF_FAIL_PRINTF4 (
2076 block->get_function () == target->get_function (),
2077 ctxt, loc,
2078 "target block is not in same function:"
2079 " source block %s is in function %s"
2080 " whereas target block %s is in function %s",
2081 block->get_debug_string (),
2082 block->get_function ()->get_debug_string (),
2083 target->get_debug_string (),
2084 target->get_function ()->get_debug_string ());
2086 block->end_with_jump (loc, target);
2089 /* Public entrypoint. See description in libgccjit.h.
2091 After error-checking, the real work is done by the
2092 gcc::jit::recording::block::end_with_return method in
2093 jit-recording.c. */
2095 void
2096 gcc_jit_block_end_with_return (gcc_jit_block *block,
2097 gcc_jit_location *loc,
2098 gcc_jit_rvalue *rvalue)
2100 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2101 gcc::jit::recording::context *ctxt = block->get_context ();
2102 JIT_LOG_FUNC (ctxt->get_logger ());
2103 /* LOC can be NULL. */
2104 gcc::jit::recording::function *func = block->get_function ();
2105 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2106 RETURN_IF_FAIL_PRINTF4 (
2107 compatible_types (
2108 func->get_return_type (),
2109 rvalue->get_type ()),
2110 ctxt, loc,
2111 "mismatching types:"
2112 " return of %s (type: %s) in function %s (return type: %s)",
2113 rvalue->get_debug_string (),
2114 rvalue->get_type ()->get_debug_string (),
2115 func->get_debug_string (),
2116 func->get_return_type ()->get_debug_string ());
2118 gcc::jit::recording::statement *stmt = block->end_with_return (loc, rvalue);
2120 /* "stmt" should be good enough to be usable in error-messages,
2121 but might still not be compilable; perform some more
2122 error-checking here. We do this here so that the error messages
2123 can contain a stringified version of "stmt", whilst appearing
2124 as close as possible to the point of failure. */
2125 rvalue->verify_valid_within_stmt (__func__, stmt);
2128 /* Public entrypoint. See description in libgccjit.h.
2130 After error-checking, the real work is done by the
2131 gcc::jit::recording::block::end_with_return method in
2132 jit-recording.c. */
2134 void
2135 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
2136 gcc_jit_location *loc)
2138 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2139 gcc::jit::recording::context *ctxt = block->get_context ();
2140 JIT_LOG_FUNC (ctxt->get_logger ());
2141 /* LOC can be NULL. */
2142 gcc::jit::recording::function *func = block->get_function ();
2143 RETURN_IF_FAIL_PRINTF2 (
2144 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
2145 ctxt, loc,
2146 "mismatching types:"
2147 " void return in function %s (return type: %s)",
2148 func->get_debug_string (),
2149 func->get_return_type ()->get_debug_string ());
2151 block->end_with_return (loc, NULL);
2154 /* Public entrypoint. See description in libgccjit.h.
2156 After error-checking, the real work is done by the
2157 gcc::jit::recording::context::new_case method in
2158 jit-recording.c. */
2160 gcc_jit_case *
2161 gcc_jit_context_new_case (gcc_jit_context *ctxt,
2162 gcc_jit_rvalue *min_value,
2163 gcc_jit_rvalue *max_value,
2164 gcc_jit_block *block)
2166 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2167 JIT_LOG_FUNC (ctxt->get_logger ());
2168 RETURN_NULL_IF_FAIL (min_value, ctxt, NULL, "NULL min_value");
2169 RETURN_NULL_IF_FAIL (max_value, ctxt, NULL, "NULL max_value");
2170 RETURN_NULL_IF_FAIL (block, ctxt, NULL, "NULL block");
2172 RETURN_NULL_IF_FAIL_PRINTF1 (min_value->is_constant (), ctxt, NULL,
2173 "min_value is not a constant: %s",
2174 min_value->get_debug_string ());
2175 RETURN_NULL_IF_FAIL_PRINTF1 (max_value->is_constant (), ctxt, NULL,
2176 "max_value is not a constant: %s",
2177 max_value->get_debug_string ());
2178 RETURN_NULL_IF_FAIL_PRINTF2 (
2179 min_value->get_type ()->is_int (),
2180 ctxt, NULL,
2181 "min_value: %s (type: %s) is not of integer type",
2182 min_value->get_debug_string (),
2183 min_value->get_type ()->get_debug_string ());
2184 RETURN_NULL_IF_FAIL_PRINTF2 (
2185 max_value->get_type ()->is_int (),
2186 ctxt, NULL,
2187 "max_value: %s (type: %s) is not of integer type",
2188 max_value->get_debug_string (),
2189 max_value->get_type ()->get_debug_string ());
2191 wide_int wi_min, wi_max;
2192 if (!min_value->get_wide_int (&wi_min))
2193 gcc_unreachable ();
2194 if (!max_value->get_wide_int (&wi_max))
2195 gcc_unreachable ();
2196 RETURN_NULL_IF_FAIL_PRINTF2 (
2197 wi::les_p (wi_min, wi_max),
2198 ctxt, NULL,
2199 "min_value: %s > max_value: %s",
2200 min_value->get_debug_string (),
2201 max_value->get_debug_string ());
2202 return (gcc_jit_case *)ctxt->new_case (min_value,
2203 max_value,
2204 block);
2207 /* Public entrypoint. See description in libgccjit.h.
2209 After error-checking, this calls the trivial
2210 gcc::jit::recording::memento::as_object method (a case is a
2211 memento), in jit-recording.h. */
2213 gcc_jit_object *
2214 gcc_jit_case_as_object (gcc_jit_case *case_)
2216 RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case");
2218 return static_cast <gcc_jit_object *> (case_->as_object ());
2221 /* Helper function for gcc_jit_block_end_with_switch and
2222 valid_case_for_switch. */
2224 static bool
2225 valid_dest_for_switch (gcc::jit::recording::context *ctxt,
2226 gcc_jit_location *loc,
2227 const char *api_funcname,
2228 gcc::jit::recording::block *switch_block,
2229 gcc::jit::recording::block *dest_block,
2230 const char *dest_block_desc)
2232 if (!dest_block)
2234 jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc);
2235 return false;
2237 gcc::jit::recording::function *switch_fn = switch_block->get_function ();
2238 gcc::jit::recording::function *dest_fn = dest_block->get_function ();
2239 if (switch_fn != dest_fn)
2241 jit_error (ctxt, loc,
2242 "%s: %s is not in same function:"
2243 " switch block %s is in function %s"
2244 " whereas %s %s is in function %s",
2245 api_funcname,
2246 dest_block_desc,
2247 switch_block->get_debug_string (),
2248 switch_fn->get_debug_string (),
2249 dest_block_desc,
2250 dest_block->get_debug_string (),
2251 dest_fn->get_debug_string ());
2252 return false;
2254 return true;
2257 /* Helper function for gcc_jit_block_end_with_switch. */
2259 static bool
2260 valid_case_for_switch (gcc::jit::recording::context *ctxt,
2261 gcc_jit_location *loc,
2262 const char *api_funcname,
2263 gcc_jit_block *switch_block,
2264 gcc_jit_rvalue *expr,
2265 gcc_jit_case *case_,
2266 const char *case_desc,
2267 int case_idx)
2269 if (!case_)
2271 jit_error (ctxt, loc,
2272 "%s:"
2273 " NULL case %i",
2274 api_funcname,
2275 case_idx);
2276 return false;
2278 if (!valid_dest_for_switch (ctxt, loc,
2279 api_funcname,
2280 switch_block,
2281 case_->get_dest_block (),
2282 case_desc))
2283 return false;
2284 gcc::jit::recording::type *expr_type = expr->get_type ();
2285 if (expr_type != case_->get_min_value ()->get_type ())
2287 jit_error (ctxt, loc,
2288 "%s:"
2289 " mismatching types between case and expression:"
2290 " cases[%i]->min_value: %s (type: %s)"
2291 " expr: %s (type: %s)",
2292 api_funcname,
2293 case_idx,
2294 case_->get_min_value ()->get_debug_string (),
2295 case_->get_min_value ()->get_type ()->get_debug_string (),
2296 expr->get_debug_string (),
2297 expr_type->get_debug_string ());
2298 return false;
2300 if (expr_type != case_->get_max_value ()->get_type ())
2302 jit_error (ctxt, loc,
2303 "%s:"
2304 " mismatching types between case and expression:"
2305 " cases[%i]->max_value: %s (type: %s)"
2306 " expr: %s (type: %s)",
2307 api_funcname,
2308 case_idx,
2309 case_->get_max_value ()->get_debug_string (),
2310 case_->get_max_value ()->get_type ()->get_debug_string (),
2311 expr->get_debug_string (),
2312 expr_type->get_debug_string ());
2313 return false;
2315 return true;
2318 /* A class for holding the data we need to perform error-checking
2319 on a libgccjit API call. */
2321 class api_call_validator
2323 public:
2324 api_call_validator (gcc::jit::recording::context *ctxt,
2325 gcc_jit_location *loc,
2326 const char *funcname)
2327 : m_ctxt (ctxt),
2328 m_loc (loc),
2329 m_funcname (funcname)
2332 protected:
2333 gcc::jit::recording::context *m_ctxt;
2334 gcc_jit_location *m_loc;
2335 const char *m_funcname;
2338 /* A class for verifying that the ranges of cases within
2339 gcc_jit_block_end_with_switch don't overlap. */
2341 class case_range_validator : public api_call_validator
2343 public:
2344 case_range_validator (gcc::jit::recording::context *ctxt,
2345 gcc_jit_location *loc,
2346 const char *funcname);
2348 bool
2349 validate (gcc_jit_case *case_, int idx);
2351 private:
2352 static int
2353 case_compare (gcc::jit::recording::rvalue *k1,
2354 gcc::jit::recording::rvalue *k2);
2356 static wide_int
2357 get_wide_int (gcc::jit::recording::rvalue *k);
2359 private:
2360 typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases;
2363 /* case_range_validator's ctor. */
2365 case_range_validator::case_range_validator (gcc::jit::recording::context *ctxt,
2366 gcc_jit_location *loc,
2367 const char *funcname)
2368 : api_call_validator (ctxt, loc, funcname),
2369 m_cases (case_compare, NULL, NULL)
2373 /* Ensure that the range of CASE_ does not overlap with any of the
2374 ranges of cases we've already seen.
2375 Return true if everything is OK.
2376 Return false and emit an error if there is an overlap.
2377 Compare with c-family/c-common.c:c_add_case_label. */
2379 bool
2380 case_range_validator::validate (gcc_jit_case *case_,
2381 int case_idx)
2383 /* Look up the LOW_VALUE in the table of case labels we already
2384 have. */
2385 gcc_jit_case *other = m_cases.lookup (case_->get_min_value ());
2387 /* If there was not an exact match, check for overlapping ranges. */
2388 if (!other)
2390 gcc_jit_case *pred;
2391 gcc_jit_case *succ;
2393 /* Even though there wasn't an exact match, there might be an
2394 overlap between this case range and another case range.
2395 Since we've (inductively) not allowed any overlapping case
2396 ranges, we simply need to find the greatest low case label
2397 that is smaller that CASE_MIN_VALUE, and the smallest low case
2398 label that is greater than CASE_MAX_VALUE. If there is an overlap
2399 it will occur in one of these two ranges. */
2400 pred = m_cases.predecessor (case_->get_min_value ());
2401 succ = m_cases.successor (case_->get_max_value ());
2403 /* Check to see if the PRED overlaps. It is smaller than
2404 the LOW_VALUE, so we only need to check its max value. */
2405 if (pred)
2407 wide_int wi_case_min = get_wide_int (case_->get_min_value ());
2408 wide_int wi_pred_max = get_wide_int (pred->get_max_value ());
2409 if (wi::ges_p (wi_pred_max, wi_case_min))
2410 other = pred;
2413 if (!other && succ)
2415 /* Check to see if the SUCC overlaps. The low end of that
2416 range is bigger than the low end of the current range. */
2417 wide_int wi_case_max = get_wide_int (case_->get_max_value ());
2418 wide_int wi_succ_min = get_wide_int (succ->get_min_value ());
2419 if (wi::les_p (wi_succ_min, wi_case_max))
2420 other = succ;
2424 /* If there was an overlap, issue an error. */
2425 if (other)
2427 jit_error (m_ctxt, m_loc,
2428 "%s: duplicate (or overlapping) cases values:"
2429 " case %i: %s overlaps %s",
2430 m_funcname,
2431 case_idx,
2432 case_->get_debug_string (),
2433 other->get_debug_string ());
2434 return false;
2437 /* Register this case label in the splay tree. */
2438 m_cases.insert (case_->get_min_value (),
2439 case_);
2440 return true;
2443 /* Compare with c-family/c-common.c:case_compare, which acts on tree
2444 nodes, rather than rvalue *.
2446 Comparator for case label values. K1 and K2 must be constant integer
2447 values (anything else should have been rejected by
2448 gcc_jit_context_new_case.
2450 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
2451 K2, and 0 if K1 and K2 are equal. */
2454 case_range_validator::case_compare (gcc::jit::recording::rvalue * k1,
2455 gcc::jit::recording::rvalue * k2)
2457 wide_int wi1 = get_wide_int (k1);
2458 wide_int wi2 = get_wide_int (k2);
2459 return wi::cmps(wi1, wi2);
2462 /* Given a const int rvalue K, get the underlying value as a wide_int. */
2464 wide_int
2465 case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k)
2467 wide_int wi;
2468 bool got_wi = k->get_wide_int (&wi);
2469 gcc_assert (got_wi);
2470 return wi;
2473 /* Public entrypoint. See description in libgccjit.h.
2475 After error-checking, the real work is done by the
2476 gcc::jit::recording::block::end_with_switch method in
2477 jit-recording.c. */
2479 void
2480 gcc_jit_block_end_with_switch (gcc_jit_block *block,
2481 gcc_jit_location *loc,
2482 gcc_jit_rvalue *expr,
2483 gcc_jit_block *default_block,
2484 int num_cases,
2485 gcc_jit_case **cases)
2487 RETURN_IF_NOT_VALID_BLOCK (block, loc);
2488 gcc::jit::recording::context *ctxt = block->get_context ();
2489 JIT_LOG_FUNC (ctxt->get_logger ());
2490 /* LOC can be NULL. */
2491 RETURN_IF_FAIL (expr, ctxt, loc,
2492 "NULL expr");
2493 gcc::jit::recording::type *expr_type = expr->get_type ();
2494 RETURN_IF_FAIL_PRINTF2 (
2495 expr_type->is_int (),
2496 ctxt, loc,
2497 "expr: %s (type: %s) is not of integer type",
2498 expr->get_debug_string (),
2499 expr_type->get_debug_string ());
2500 if (!valid_dest_for_switch (ctxt, loc,
2501 __func__,
2502 block,
2503 default_block,
2504 "default_block"))
2505 return;
2506 RETURN_IF_FAIL (num_cases >= 0, ctxt, loc, "num_cases < 0");
2507 case_range_validator crv (ctxt, loc, __func__);
2508 for (int i = 0; i < num_cases; i++)
2510 char case_desc[32];
2511 snprintf (case_desc, sizeof (case_desc),
2512 "cases[%i]", i);
2513 if (!valid_case_for_switch (ctxt, loc,
2514 __func__,
2515 block,
2516 expr,
2517 cases[i],
2518 case_desc,
2520 return;
2521 if (!crv.validate (cases[i], i))
2522 return;
2525 block->end_with_switch (loc, expr, default_block,
2526 num_cases,
2527 (gcc::jit::recording::case_ **)cases);
2530 /**********************************************************************
2531 Option-management
2532 **********************************************************************/
2534 /* Public entrypoint. See description in libgccjit.h.
2536 After error-checking, the real work is done by the
2537 gcc::jit::recording::context::set_str_option method in
2538 jit-recording.c. */
2540 void
2541 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
2542 enum gcc_jit_str_option opt,
2543 const char *value)
2545 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2546 JIT_LOG_FUNC (ctxt->get_logger ());
2547 /* opt is checked by the inner function.
2548 value can be NULL. */
2550 ctxt->set_str_option (opt, value);
2553 /* Public entrypoint. See description in libgccjit.h.
2555 After error-checking, the real work is done by the
2556 gcc::jit::recording::context::set_int_option method in
2557 jit-recording.c. */
2559 void
2560 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
2561 enum gcc_jit_int_option opt,
2562 int value)
2564 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2565 JIT_LOG_FUNC (ctxt->get_logger ());
2566 /* opt is checked by the inner function. */
2568 ctxt->set_int_option (opt, value);
2571 /* Public entrypoint. See description in libgccjit.h.
2573 After error-checking, the real work is done by the
2574 gcc::jit::recording::context::set_bool_option method in
2575 jit-recording.c. */
2577 void
2578 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
2579 enum gcc_jit_bool_option opt,
2580 int value)
2582 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2583 JIT_LOG_FUNC (ctxt->get_logger ());
2584 /* opt is checked by the inner function. */
2586 ctxt->set_bool_option (opt, value);
2589 /* Public entrypoint. See description in libgccjit.h.
2591 After error-checking, the real work is done by the
2592 gcc::jit::recording::context::set_inner_bool_option method in
2593 jit-recording.c. */
2595 void
2596 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
2597 int bool_value)
2599 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2600 JIT_LOG_FUNC (ctxt->get_logger ());
2601 ctxt->set_inner_bool_option (
2602 gcc::jit::INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
2603 bool_value);
2606 /* Public entrypoint. See description in libgccjit.h.
2608 After error-checking, the real work is done by the
2609 gcc::jit::recording::context::add_command_line_option method in
2610 jit-recording.c. */
2612 void
2613 gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
2614 const char *optname)
2616 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2617 JIT_LOG_FUNC (ctxt->get_logger ());
2618 RETURN_IF_FAIL (optname, ctxt, NULL, "NULL optname");
2619 if (ctxt->get_logger ())
2620 ctxt->get_logger ()->log ("optname: %s", optname);
2622 ctxt->add_command_line_option (optname);
2625 /* Public entrypoint. See description in libgccjit.h.
2627 After error-checking, the real work is done by the
2628 gcc::jit::recording::context::enable_dump method in
2629 jit-recording.c. */
2631 void
2632 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2633 const char *dumpname,
2634 char **out_ptr)
2636 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2637 JIT_LOG_FUNC (ctxt->get_logger ());
2638 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2639 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2641 ctxt->enable_dump (dumpname, out_ptr);
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 method in
2648 jit-recording.c. */
2650 gcc_jit_result *
2651 gcc_jit_context_compile (gcc_jit_context *ctxt)
2653 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2655 JIT_LOG_FUNC (ctxt->get_logger ());
2657 ctxt->log ("in-memory compile of ctxt: %p", (void *)ctxt);
2659 gcc_jit_result *result = (gcc_jit_result *)ctxt->compile ();
2661 ctxt->log ("%s: returning (gcc_jit_result *)%p",
2662 __func__, (void *)result);
2664 return result;
2667 /* Public entrypoint. See description in libgccjit.h.
2669 After error-checking, the real work is done by the
2670 gcc::jit::recording::context::compile_to_file method in
2671 jit-recording.c. */
2673 void
2674 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
2675 enum gcc_jit_output_kind output_kind,
2676 const char *output_path)
2678 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2679 JIT_LOG_FUNC (ctxt->get_logger ());
2680 RETURN_IF_FAIL_PRINTF1 (
2681 ((output_kind >= GCC_JIT_OUTPUT_KIND_ASSEMBLER)
2682 && (output_kind <= GCC_JIT_OUTPUT_KIND_EXECUTABLE)),
2683 ctxt, NULL,
2684 "unrecognized output_kind: %i",
2685 output_kind);
2686 RETURN_IF_FAIL (output_path, ctxt, NULL, "NULL output_path");
2688 ctxt->log ("compile_to_file of ctxt: %p", (void *)ctxt);
2689 ctxt->log ("output_kind: %i", output_kind);
2690 ctxt->log ("output_path: %s", output_path);
2692 ctxt->compile_to_file (output_kind, output_path);
2696 /* Public entrypoint. See description in libgccjit.h.
2698 After error-checking, the real work is done by the
2699 gcc::jit::recording::context::dump_to_file method in
2700 jit-recording.c. */
2702 void
2703 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2704 const char *path,
2705 int update_locations)
2707 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2708 JIT_LOG_FUNC (ctxt->get_logger ());
2709 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2710 ctxt->dump_to_file (path, update_locations);
2713 /* Public entrypoint. See description in libgccjit.h. */
2715 void
2716 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
2717 FILE *logfile,
2718 int flags,
2719 int verbosity)
2721 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2722 JIT_LOG_FUNC (ctxt->get_logger ());
2723 RETURN_IF_FAIL ((flags == 0), ctxt, NULL, "flags must be 0 for now");
2724 RETURN_IF_FAIL ((verbosity == 0), ctxt, NULL, "verbosity must be 0 for now");
2726 gcc::jit::logger *logger;
2727 if (logfile)
2728 logger = new gcc::jit::logger (logfile, flags, verbosity);
2729 else
2730 logger = NULL;
2731 ctxt->set_logger (logger);
2734 /* Public entrypoint. See description in libgccjit.h.
2736 After error-checking, the real work is done by the
2737 gcc::jit::recording::context::dump_reproducer_to_file method in
2738 jit-recording.c. */
2740 void
2741 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
2742 const char *path)
2744 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2745 JIT_LOG_FUNC (ctxt->get_logger ());
2746 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2747 ctxt->dump_reproducer_to_file (path);
2750 /* Public entrypoint. See description in libgccjit.h.
2752 After error-checking, the real work is done by the
2753 gcc::jit::recording::context::get_first_error method in
2754 jit-recording.c. */
2756 const char *
2757 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2759 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2760 JIT_LOG_FUNC (ctxt->get_logger ());
2762 return ctxt->get_first_error ();
2765 /* Public entrypoint. See description in libgccjit.h.
2767 After error-checking, the real work is done by the
2768 gcc::jit::recording::context::get_last_error method in
2769 jit-recording.c. */
2771 const char *
2772 gcc_jit_context_get_last_error (gcc_jit_context *ctxt)
2774 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2776 return ctxt->get_last_error ();
2779 /* Public entrypoint. See description in libgccjit.h.
2781 After error-checking, the real work is done by the
2782 gcc::jit::result::get_code method in jit-result.c. */
2784 void *
2785 gcc_jit_result_get_code (gcc_jit_result *result,
2786 const char *fnname)
2788 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2789 JIT_LOG_FUNC (result->get_logger ());
2790 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2792 result->log ("locating fnname: %s", fnname);
2793 void *code = result->get_code (fnname);
2794 result->log ("%s: returning (void *)%p", __func__, code);
2796 return code;
2799 /* Public entrypoint. See description in libgccjit.h.
2801 After error-checking, the real work is done by the
2802 gcc::jit::result::get_global method in jit-result.c. */
2804 void *
2805 gcc_jit_result_get_global (gcc_jit_result *result,
2806 const char *name)
2808 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2809 JIT_LOG_FUNC (result->get_logger ());
2810 RETURN_NULL_IF_FAIL (name, NULL, NULL, "NULL name");
2812 void *global = result->get_global (name);
2813 result->log ("%s: returning (void *)%p", __func__, global);
2815 return global;
2818 /* Public entrypoint. See description in libgccjit.h.
2820 After error-checking, this is essentially a wrapper around the
2821 destructor for gcc::jit::result in jit-result.c. */
2823 void
2824 gcc_jit_result_release (gcc_jit_result *result)
2826 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2827 JIT_LOG_FUNC (result->get_logger ());
2828 result->log ("deleting result: %p", (void *)result);
2829 delete result;