diff options
author | David Malcolm <dmalcolm@redhat.com> | 2015-06-30 20:39:50 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2015-06-30 20:39:50 +0000 |
commit | ec5d0088148d1d6218f5f503d1c8e67a529dfacd (patch) | |
tree | c7cf3f33890bd8579a8afa8a850a326bc0b1fd58 /gcc | |
parent | 6a3603e356e14096e9554ceef57916fd03d9072b (diff) | |
download | gcc-ec5d0088148d1d6218f5f503d1c8e67a529dfacd.zip gcc-ec5d0088148d1d6218f5f503d1c8e67a529dfacd.tar.gz gcc-ec5d0088148d1d6218f5f503d1c8e67a529dfacd.tar.bz2 |
jit: add switch statements
gcc/ChangeLog:
* typed-splay-tree.h: New file.
gcc/jit/ChangeLog:
* docs/cp/topics/functions.rst (Blocks): Add switch statements to
list of ways to terminate a block.
(gccjit::block::end_with_switch): Add function description.
(gccjit::case_): Add class.
(gccjit::context::new_case): Add function description.
* docs/cp/topics/objects.rst: Add "case_" to class hierarchy.
* docs/topics/compatibility.rst (LIBGCCJIT_ABI_3): New.
* docs/topics/functions.rst (Blocks): Add switch statements to
list of ways to terminate a block.
(gcc_jit_block_end_with_switch): Add function description.
(gcc_jit_case): Add type.
(gcc_jit_context_new_case): Add function description.
(gcc_jit_case_as_object): Add function description.
* docs/topics/objects.rst: Add gcc_jit_case to class hierarchy.
* docs/_build/texinfo/libgccjit.texi: Regenerate.
* jit-common.h (gcc::jit::recording::case_): Add forward decl.
(gcc::jit::playback::case_): Add forward decl.
* jit-playback.c (add_case): New function.
(gcc::jit::playback::block::add_switch): New function.
* jit-playback.h (gcc::jit::playback::case_): New struct.
(gcc::jit::playback::block::get_function): New method.
(gcc::jit::playback::block::add_switch): New method.
* jit-recording.c: Within namespace gcc::jit...
(recording::context::new_case): New method.
(recording::function::validate): Update for change to
get_successor_blocks.
(recording::block::end_with_switch): New method.
(recording::block::get_successor_blocks): Update to support an
arbitrary number of successor blocks.
(recording::block::dump_edges_to_dot): Likewise.
(memento_of_new_rvalue_from_const <int>::get_wide_int): New.
(memento_of_new_rvalue_from_const <long>::get_wide_int): New.
(memento_of_new_rvalue_from_const <double>::get_wide_int): New.
(memento_of_new_rvalue_from_const <void *>::get_wide_int): New.
(recording::statement::get_successor_blocks): Update to support an
arbitrary number of successor blocks.
(recording::conditional::get_successor_blocks): Likewise.
(recording::jump::get_successor_blocks): Likewise.
(recording::return_::get_successor_blocks): Likewise.
(recording::case_::write_reproducer): New.
(recording::case_::make_debug_string): New.
(recording::switch_::switch_): New.
(recording::switch_::replay_into): New.
(recording::switch_::get_successor_blocks): New.
(recording::switch_::make_debug_string): New.
(recording::switch_::write_reproducer): New.
* jit-recording.h: Within namespace gcc::jit::recording...
(context::new_case): New.
(rvalue::is_constant): New.
(rvalue::get_wide_int): New.
(block::end_with_switch): New.
(block::get_successor_blocks): Update to support an arbitrary
number of successor blocks.
(memento_of_new_rvalue_from_const::is_constant): New.
(memento_of_new_rvalue_from_const::get_wide_int): New.
(statement::get_successor_blocks): Update to support an arbitrary
number of successor blocks.
(conditional::get_successor_blocks): Likewise.
(jump::get_successor_blocks): Likewise.
(return_::get_successor_blocks): Likewise.
(case_): New subclass of memento.
(switch_): New subclass of statement.
* libgccjit++.h (gccjit::case_): New subclass of gccjit::object.
(gccjit::context::new_case): New method.
(gccjit::block::end_with_switch): New method.
(gccjit::case_::case): New ctors.
(gccjit::case_::get_inner_case): New method.
* libgccjit.c: Include "typed-splay-tree.h"
(struct gcc_jit_case): New.
(gcc_jit_context_new_case): New function.
(gcc_jit_case_as_object): New function.
(valid_dest_for_switch): New function.
(valid_case_for_switch): New function.
(class api_call_validator): New class.
(class case_range_validator): New class.
(case_range_validator::case_range_validator): New.
(case_range_validator::validate): New.
(case_range_validator::case_compare): New.
(case_range_validator::get_wide_int): new.
(gcc_jit_block_end_with_switch): New.
* libgccjit.h: Add gcc_jit_case to class hierarchy comment.
(gcc_jit_case): New typedef.
(gcc_jit_context_new_case): New function.
(gcc_jit_case_as_object): New function.
(gcc_jit_block_end_with_switch): New function.
(LIBGCCJIT_HAVE_SWITCH_STATEMENTS): New.
* libgccjit.map: Add gcc_jit_block_end_with_switch,
gcc_jit_case_as_object and gcc_jit_context_new_case.
gcc/testsuite/ChangeLog:
* jit.dg/all-non-failing-tests.h: Add test-switch.c.
* jit.dg/test-error-gcc_jit_block_end_with_switch-NULL-case.c: New
testcase.
* jit.dg/test-error-gcc_jit_block_end_with_switch-mismatching-case-type.c:
New testcase.
* jit.dg/test-error-gcc_jit_block_end_with_switch-overlapping-ranges.c:
New testcase.
* jit.dg/test-error-gcc_jit_context_new_case-non-const-label.c:
New testcase.
* jit.dg/test-error-gcc_jit_context_new_case-non-integer-type.c:
New testcase.
* jit.dg/test-error-gcc_jit_context_new_case-reversed-endpoints.c:
New testcase.
* jit.dg/test-switch.c: New testcase.
* jit.dg/test-switch.cc: New testcase.
From-SVN: r225207
Diffstat (limited to 'gcc')
28 files changed, 2867 insertions, 337 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 83f5986..6772150 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2015-06-30 David Malcolm <dmalcolm@redhat.com> + + * typed-splay-tree.h: New file. + 2015-06-30 Vladimir Makarov <vmakarov@redhat.com> PR debug/66691 diff --git a/gcc/jit/ChangeLog b/gcc/jit/ChangeLog index 439f248..325f3b8 100644 --- a/gcc/jit/ChangeLog +++ b/gcc/jit/ChangeLog @@ -1,5 +1,96 @@ 2015-06-30 David Malcolm <dmalcolm@redhat.com> + * docs/cp/topics/functions.rst (Blocks): Add switch statements to + list of ways to terminate a block. + (gccjit::block::end_with_switch): Add function description. + (gccjit::case_): Add class. + (gccjit::context::new_case): Add function description. + * docs/cp/topics/objects.rst: Add "case_" to class hierarchy. + * docs/topics/compatibility.rst (LIBGCCJIT_ABI_3): New. + * docs/topics/functions.rst (Blocks): Add switch statements to + list of ways to terminate a block. + (gcc_jit_block_end_with_switch): Add function description. + (gcc_jit_case): Add type. + (gcc_jit_context_new_case): Add function description. + (gcc_jit_case_as_object): Add function description. + * docs/topics/objects.rst: Add gcc_jit_case to class hierarchy. + * docs/_build/texinfo/libgccjit.texi: Regenerate. + * jit-common.h (gcc::jit::recording::case_): Add forward decl. + (gcc::jit::playback::case_): Add forward decl. + * jit-playback.c (add_case): New function. + (gcc::jit::playback::block::add_switch): New function. + * jit-playback.h (gcc::jit::playback::case_): New struct. + (gcc::jit::playback::block::get_function): New method. + (gcc::jit::playback::block::add_switch): New method. + * jit-recording.c: Within namespace gcc::jit... + (recording::context::new_case): New method. + (recording::function::validate): Update for change to + get_successor_blocks. + (recording::block::end_with_switch): New method. + (recording::block::get_successor_blocks): Update to support an + arbitrary number of successor blocks. + (recording::block::dump_edges_to_dot): Likewise. + (memento_of_new_rvalue_from_const <int>::get_wide_int): New. + (memento_of_new_rvalue_from_const <long>::get_wide_int): New. + (memento_of_new_rvalue_from_const <double>::get_wide_int): New. + (memento_of_new_rvalue_from_const <void *>::get_wide_int): New. + (recording::statement::get_successor_blocks): Update to support an + arbitrary number of successor blocks. + (recording::conditional::get_successor_blocks): Likewise. + (recording::jump::get_successor_blocks): Likewise. + (recording::return_::get_successor_blocks): Likewise. + (recording::case_::write_reproducer): New. + (recording::case_::make_debug_string): New. + (recording::switch_::switch_): New. + (recording::switch_::replay_into): New. + (recording::switch_::get_successor_blocks): New. + (recording::switch_::make_debug_string): New. + (recording::switch_::write_reproducer): New. + * jit-recording.h: Within namespace gcc::jit::recording... + (context::new_case): New. + (rvalue::is_constant): New. + (rvalue::get_wide_int): New. + (block::end_with_switch): New. + (block::get_successor_blocks): Update to support an arbitrary + number of successor blocks. + (memento_of_new_rvalue_from_const::is_constant): New. + (memento_of_new_rvalue_from_const::get_wide_int): New. + (statement::get_successor_blocks): Update to support an arbitrary + number of successor blocks. + (conditional::get_successor_blocks): Likewise. + (jump::get_successor_blocks): Likewise. + (return_::get_successor_blocks): Likewise. + (case_): New subclass of memento. + (switch_): New subclass of statement. + * libgccjit++.h (gccjit::case_): New subclass of gccjit::object. + (gccjit::context::new_case): New method. + (gccjit::block::end_with_switch): New method. + (gccjit::case_::case): New ctors. + (gccjit::case_::get_inner_case): New method. + * libgccjit.c: Include "typed-splay-tree.h" + (struct gcc_jit_case): New. + (gcc_jit_context_new_case): New function. + (gcc_jit_case_as_object): New function. + (valid_dest_for_switch): New function. + (valid_case_for_switch): New function. + (class api_call_validator): New class. + (class case_range_validator): New class. + (case_range_validator::case_range_validator): New. + (case_range_validator::validate): New. + (case_range_validator::case_compare): New. + (case_range_validator::get_wide_int): new. + (gcc_jit_block_end_with_switch): New. + * libgccjit.h: Add gcc_jit_case to class hierarchy comment. + (gcc_jit_case): New typedef. + (gcc_jit_context_new_case): New function. + (gcc_jit_case_as_object): New function. + (gcc_jit_block_end_with_switch): New function. + (LIBGCCJIT_HAVE_SWITCH_STATEMENTS): New. + * libgccjit.map: Add gcc_jit_block_end_with_switch, + gcc_jit_case_as_object and gcc_jit_context_new_case. + +2015-06-30 David Malcolm <dmalcolm@redhat.com> + PR jit/66546 * docs/cp/topics/contexts.rst (gccjit::context::set_bool_allow_unreachable_blocks): New. diff --git a/gcc/jit/docs/_build/texinfo/libgccjit.texi b/gcc/jit/docs/_build/texinfo/libgccjit.texi index 522ce76..212df10 100644 --- a/gcc/jit/docs/_build/texinfo/libgccjit.texi +++ b/gcc/jit/docs/_build/texinfo/libgccjit.texi @@ -235,6 +235,7 @@ ABI symbol tags * LIBGCCJIT_ABI_0:: * LIBGCCJIT_ABI_1:: * LIBGCCJIT_ABI_2:: +* LIBGCCJIT_ABI_3:: C++ bindings for libgccjit @@ -4916,6 +4917,7 @@ ABI symbol tags * LIBGCCJIT_ABI_0:: * LIBGCCJIT_ABI_1:: * LIBGCCJIT_ABI_2:: +* LIBGCCJIT_ABI_3:: @end menu @@ -5619,6 +5621,7 @@ looks like this: +- gcc_jit_rvalue +- gcc_jit_lvalue +- gcc_jit_param + +- gcc_jit_case @end example @noindent @@ -7299,7 +7302,8 @@ The first basic block that you create within a function will be the entrypoint. Each basic block that you create within a function must be -terminated, either with a conditional, a jump, or a return. +terminated, either with a conditional, a jump, a return, or a +switch. It's legal to have multiple basic blocks that return within one function. @@ -7476,6 +7480,217 @@ return; @noindent @end deffn +@geindex gcc_jit_block_end_with_switch (C function) +@anchor{topics/functions gcc_jit_block_end_with_switch}@anchor{d4} +@deffn {C Function} void gcc_jit_block_end_with_switch (gcc_jit_block@w{ }*block, gcc_jit_location@w{ }*loc, gcc_jit_rvalue@w{ }*expr, gcc_jit_block@w{ }*default_block, int@w{ }num_cases, gcc_jit_case@w{ }**cases) + +Terminate a block by adding evalation of an rvalue, then performing +a multiway branch. + +This is roughly equivalent to this C code: + +@example +switch (expr) + @{ + default: + goto default_block; + + case C0.min_value ... C0.max_value: + goto C0.dest_block; + + case C1.min_value ... C1.max_value: + goto C1.dest_block; + + ...etc... + + case C[N - 1].min_value ... C[N - 1].max_value: + goto C[N - 1].dest_block; +@} +@end example + +@noindent + +@code{block}, @code{expr}, @code{default_block} and @code{cases} must all be +non-NULL. + +@code{expr} must be of the same integer type as all of the @code{min_value} +and @code{max_value} within the cases. + +@code{num_cases} must be >= 0. + +The ranges of the cases must not overlap (or have duplicate +values). + +The API entrypoints relating to switch statements and cases: + +@quotation + + +@itemize * + +@item +@pxref{d4,,gcc_jit_block_end_with_switch()} + +@item +@pxref{d5,,gcc_jit_case_as_object()} + +@item +@pxref{d6,,gcc_jit_context_new_case()} +@end itemize +@end quotation + +were added in @pxref{d7,,LIBGCCJIT_ABI_3}; you can test for their presence +using + +@example +#ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS +@end example + +@noindent + +@geindex gcc_jit_case (C type) +@anchor{topics/functions gcc_jit_case}@anchor{d8} +@deffn {C Type} gcc_jit_case +@end deffn + +A @cite{gcc_jit_case} represents a case within a switch statement, and +is created within a particular @pxref{8,,gcc_jit_context} using +@pxref{d6,,gcc_jit_context_new_case()}. + +Each case expresses a multivalued range of integer values. You +can express single-valued cases by passing in the same value for +both @cite{min_value} and @cite{max_value}. + +@geindex gcc_jit_context_new_case (C function) +@anchor{topics/functions gcc_jit_context_new_case}@anchor{d6} +@deffn {C Function} gcc_jit_case * gcc_jit_context_new_case (gcc_jit_context@w{ }*ctxt, gcc_jit_rvalue@w{ }*min_value, gcc_jit_rvalue@w{ }*max_value, gcc_jit_block@w{ }*dest_block) + +Create a new gcc_jit_case instance for use in a switch statement. +@cite{min_value} and @cite{max_value} must be constants of an integer type, +which must match that of the expression of the switch statement. + +@cite{dest_block} must be within the same function as the switch +statement. +@end deffn + +@geindex gcc_jit_case_as_object (C function) +@anchor{topics/functions gcc_jit_case_as_object}@anchor{d5} +@deffn {C Function} gcc_jit_object * gcc_jit_case_as_object (gcc_jit_case@w{ }*case_) + +Upcast from a case to an object. +@end deffn + +Here's an example of creating a switch statement: + +@quotation + +@example + +void +create_code (gcc_jit_context *ctxt, void *user_data) +@{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + @{ + switch (x) + @{ + case 0 ... 5: + return 3; + + case 25 ... 27: + return 4; + + case -42 ... -17: + return 83; + + case 40: + return 8; + + default: + return 10; + @} + @} + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = @{x@}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + gcc_jit_block *b_case_0_5 = + gcc_jit_function_new_block (func, "case_0_5"); + gcc_jit_block *b_case_25_27 = + gcc_jit_function_new_block (func, "case_25_27"); + gcc_jit_block *b_case_m42_m17 = + gcc_jit_function_new_block (func, "case_m42_m17"); + gcc_jit_block *b_case_40 = + gcc_jit_function_new_block (func, "case_40"); + + gcc_jit_case *cases[4] = @{ + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 0), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5), + b_case_0_5), + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 25), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 27), + b_case_25_27), + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, -42), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, -17), + b_case_m42_m17), + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 40), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 40), + b_case_40) + @}; + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 4, cases); + + gcc_jit_block_end_with_return ( + b_case_0_5, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3)); + gcc_jit_block_end_with_return ( + b_case_25_27, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 4)); + gcc_jit_block_end_with_return ( + b_case_m42_m17, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 83)); + gcc_jit_block_end_with_return ( + b_case_40, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 8)); + gcc_jit_block_end_with_return ( + b_default, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10)); +@} + + +@end example + +@noindent +@end quotation +@end deffn + @c Copyright (C) 2014-2015 Free Software Foundation, Inc. @c Originally contributed by David Malcolm <dmalcolm@redhat.com> @c @@ -7494,7 +7709,7 @@ return; @c <http://www.gnu.org/licenses/>. @node Source Locations,Compiling a context,Creating and using functions,Topic Reference -@anchor{topics/locations source-locations}@anchor{d4}@anchor{topics/locations doc}@anchor{d5} +@anchor{topics/locations source-locations}@anchor{d9}@anchor{topics/locations doc}@anchor{da} @section Source Locations @@ -7540,7 +7755,7 @@ location. @end menu @node Faking it,,,Source Locations -@anchor{topics/locations faking-it}@anchor{d6} +@anchor{topics/locations faking-it}@anchor{db} @subsection Faking it @@ -7578,7 +7793,7 @@ file, giving you @emph{something} you can step through in the debugger. @c <http://www.gnu.org/licenses/>. @node Compiling a context,ABI and API compatibility,Source Locations,Topic Reference -@anchor{topics/compilation compiling-a-context}@anchor{d7}@anchor{topics/compilation doc}@anchor{d8} +@anchor{topics/compilation compiling-a-context}@anchor{dc}@anchor{topics/compilation doc}@anchor{dd} @section Compiling a context @@ -7597,7 +7812,7 @@ prevent any future compilation of that context. @end menu @node In-memory compilation,Ahead-of-time compilation,,Compiling a context -@anchor{topics/compilation in-memory-compilation}@anchor{d9} +@anchor{topics/compilation in-memory-compilation}@anchor{de} @subsection In-memory compilation @@ -7723,7 +7938,7 @@ by calling @pxref{17,,gcc_jit_result_get_code()} or @end deffn @node Ahead-of-time compilation,,In-memory compilation,Compiling a context -@anchor{topics/compilation ahead-of-time-compilation}@anchor{da} +@anchor{topics/compilation ahead-of-time-compilation}@anchor{df} @subsection Ahead-of-time compilation @@ -7752,7 +7967,7 @@ suffix of the output file when determining what to do. @end cartouche @geindex gcc_jit_output_kind (C type) -@anchor{topics/compilation gcc_jit_output_kind}@anchor{db} +@anchor{topics/compilation gcc_jit_output_kind}@anchor{e0} @deffn {C Type} enum gcc_jit_output_kind @end deffn @@ -7770,7 +7985,7 @@ Typical suffix @item -@pxref{dc,,GCC_JIT_OUTPUT_KIND_ASSEMBLER} +@pxref{e1,,GCC_JIT_OUTPUT_KIND_ASSEMBLER} @tab @@ -7778,7 +7993,7 @@ Typical suffix @item -@pxref{dd,,GCC_JIT_OUTPUT_KIND_OBJECT_FILE} +@pxref{e2,,GCC_JIT_OUTPUT_KIND_OBJECT_FILE} @tab @@ -7786,7 +8001,7 @@ Typical suffix @item -@pxref{de,,GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY} +@pxref{e3,,GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY} @tab @@ -7794,7 +8009,7 @@ Typical suffix @item -@pxref{df,,GCC_JIT_OUTPUT_KIND_EXECUTABLE} +@pxref{e4,,GCC_JIT_OUTPUT_KIND_EXECUTABLE} @tab @@ -7804,21 +8019,21 @@ None, or .exe @geindex GCC_JIT_OUTPUT_KIND_ASSEMBLER (C macro) -@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_ASSEMBLER}@anchor{dc} +@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_ASSEMBLER}@anchor{e1} @deffn {C Macro} GCC_JIT_OUTPUT_KIND_ASSEMBLER Compile the context to an assembler file. @end deffn @geindex GCC_JIT_OUTPUT_KIND_OBJECT_FILE (C macro) -@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_OBJECT_FILE}@anchor{dd} +@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_OBJECT_FILE}@anchor{e2} @deffn {C Macro} GCC_JIT_OUTPUT_KIND_OBJECT_FILE Compile the context to an object file. @end deffn @geindex GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY (C macro) -@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY}@anchor{de} +@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY}@anchor{e3} @deffn {C Macro} GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY Compile the context to a dynamic library. @@ -7828,7 +8043,7 @@ against. @end deffn @geindex GCC_JIT_OUTPUT_KIND_EXECUTABLE (C macro) -@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_EXECUTABLE}@anchor{df} +@anchor{topics/compilation GCC_JIT_OUTPUT_KIND_EXECUTABLE}@anchor{e4} @deffn {C Macro} GCC_JIT_OUTPUT_KIND_EXECUTABLE Compile the context to an executable. @@ -7855,7 +8070,7 @@ against. @c <http://www.gnu.org/licenses/>. @node ABI and API compatibility,,Compiling a context,Topic Reference -@anchor{topics/compatibility abi-and-api-compatibility}@anchor{e0}@anchor{topics/compatibility doc}@anchor{e1} +@anchor{topics/compatibility abi-and-api-compatibility}@anchor{e5}@anchor{topics/compatibility doc}@anchor{e6} @section ABI and API compatibility @@ -7889,7 +8104,6 @@ Version References: 0x00824161 0x00 04 LIBGCCJIT_ABI_1 0x00824160 0x00 03 LIBGCCJIT_ABI_0 required from libc.so.6: - 0x09691a75 0x00 02 GLIBC_2.2.5 @end example @noindent @@ -7917,12 +8131,13 @@ ABI symbol tags * LIBGCCJIT_ABI_0:: * LIBGCCJIT_ABI_1:: * LIBGCCJIT_ABI_2:: +* LIBGCCJIT_ABI_3:: @end menu @node ABI symbol tags,,,ABI and API compatibility -@anchor{topics/compatibility abi-symbol-tags}@anchor{e2} +@anchor{topics/compatibility abi-symbol-tags}@anchor{e7} @subsection ABI symbol tags @@ -7934,11 +8149,12 @@ Newer releases use the following tags. * LIBGCCJIT_ABI_0:: * LIBGCCJIT_ABI_1:: * LIBGCCJIT_ABI_2:: +* LIBGCCJIT_ABI_3:: @end menu @node LIBGCCJIT_ABI_0,LIBGCCJIT_ABI_1,,ABI symbol tags -@anchor{topics/compatibility libgccjit-abi-0}@anchor{e3}@anchor{topics/compatibility id1}@anchor{e4} +@anchor{topics/compatibility libgccjit-abi-0}@anchor{e8}@anchor{topics/compatibility id1}@anchor{e9} @subsubsection @code{LIBGCCJIT_ABI_0} @@ -7950,21 +8166,45 @@ continue to work, with this being handled transparently by the linker (see this post@footnote{https://gcc.gnu.org/ml/gcc-patches/2015-06/msg02126.html}) @node LIBGCCJIT_ABI_1,LIBGCCJIT_ABI_2,LIBGCCJIT_ABI_0,ABI symbol tags -@anchor{topics/compatibility libgccjit-abi-1}@anchor{71}@anchor{topics/compatibility id2}@anchor{e5} +@anchor{topics/compatibility libgccjit-abi-1}@anchor{71}@anchor{topics/compatibility id2}@anchor{ea} @subsubsection @code{LIBGCCJIT_ABI_1} @code{LIBGCCJIT_ABI_1} covers the addition of @pxref{70,,gcc_jit_context_add_command_line_option()} -@node LIBGCCJIT_ABI_2,,LIBGCCJIT_ABI_1,ABI symbol tags -@anchor{topics/compatibility libgccjit-abi-2}@anchor{6c}@anchor{topics/compatibility id3}@anchor{e6} +@node LIBGCCJIT_ABI_2,LIBGCCJIT_ABI_3,LIBGCCJIT_ABI_1,ABI symbol tags +@anchor{topics/compatibility libgccjit-abi-2}@anchor{6c}@anchor{topics/compatibility id3}@anchor{eb} @subsubsection @code{LIBGCCJIT_ABI_2} @code{LIBGCCJIT_ABI_2} covers the addition of @pxref{6b,,gcc_jit_context_set_bool_allow_unreachable_blocks()} +@node LIBGCCJIT_ABI_3,,LIBGCCJIT_ABI_2,ABI symbol tags +@anchor{topics/compatibility libgccjit-abi-3}@anchor{d7}@anchor{topics/compatibility id4}@anchor{ec} +@subsubsection @code{LIBGCCJIT_ABI_3} + + +@code{LIBGCCJIT_ABI_3} covers the addition of switch statements via API +entrypoints: + +@quotation + + +@itemize * + +@item +@pxref{d4,,gcc_jit_block_end_with_switch()} + +@item +@pxref{d5,,gcc_jit_case_as_object()} + +@item +@pxref{d6,,gcc_jit_context_new_case()} +@end itemize +@end quotation + @c Copyright (C) 2014-2015 Free Software Foundation, Inc. @c Originally contributed by David Malcolm <dmalcolm@redhat.com> @c @@ -7983,7 +8223,7 @@ continue to work, with this being handled transparently by the linker @c <http://www.gnu.org/licenses/>. @node C++ bindings for libgccjit,Internals,Topic Reference,Top -@anchor{cp/index c-bindings-for-libgccjit}@anchor{e7}@anchor{cp/index doc}@anchor{e8} +@anchor{cp/index c-bindings-for-libgccjit}@anchor{ed}@anchor{cp/index doc}@anchor{ee} @chapter C++ bindings for libgccjit @@ -8131,7 +8371,7 @@ Compiling a context @node Tutorial<2>,Topic Reference<2>,,C++ bindings for libgccjit -@anchor{cp/intro/index doc}@anchor{e9}@anchor{cp/intro/index tutorial}@anchor{ea} +@anchor{cp/intro/index doc}@anchor{ef}@anchor{cp/intro/index tutorial}@anchor{f0} @section Tutorial @@ -8161,7 +8401,7 @@ Compiling a context @end menu @node Tutorial part 1 "Hello world"<2>,Tutorial part 2 Creating a trivial machine code function<2>,,Tutorial<2> -@anchor{cp/intro/tutorial01 doc}@anchor{eb}@anchor{cp/intro/tutorial01 tutorial-part-1-hello-world}@anchor{ec} +@anchor{cp/intro/tutorial01 doc}@anchor{f1}@anchor{cp/intro/tutorial01 tutorial-part-1-hello-world}@anchor{f2} @subsection Tutorial part 1: "Hello world" @@ -8331,7 +8571,7 @@ hello world @c <http://www.gnu.org/licenses/>. @node Tutorial part 2 Creating a trivial machine code function<2>,Tutorial part 3 Loops and variables<2>,Tutorial part 1 "Hello world"<2>,Tutorial<2> -@anchor{cp/intro/tutorial02 doc}@anchor{ed}@anchor{cp/intro/tutorial02 tutorial-part-2-creating-a-trivial-machine-code-function}@anchor{ee} +@anchor{cp/intro/tutorial02 doc}@anchor{f3}@anchor{cp/intro/tutorial02 tutorial-part-2-creating-a-trivial-machine-code-function}@anchor{f4} @subsection Tutorial part 2: Creating a trivial machine code function @@ -8360,7 +8600,7 @@ All state associated with compilation is associated with a @code{gccjit::context}, which is a thin C++ wrapper around the C API's @pxref{8,,gcc_jit_context *}. -Create one using @pxref{ef,,gccjit;;context;;acquire()}: +Create one using @pxref{f5,,gccjit;;context;;acquire()}: @example gccjit::context ctxt; @@ -8373,7 +8613,7 @@ The JIT library has a system of types. It is statically-typed: every expression is of a specific type, fixed at compile-time. In our example, all of the expressions are of the C @cite{int} type, so let's obtain this from the context, as a @code{gccjit::type}, using -@pxref{f0,,gccjit;;context;;get_type()}: +@pxref{f6,,gccjit;;context;;get_type()}: @example gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT); @@ -8386,7 +8626,7 @@ entity in the API is associated with a @code{gccjit::context}. Memory management is easy: all such "contextual" objects are automatically cleaned up for you when the context is released, using -@pxref{f1,,gccjit;;context;;release()}: +@pxref{f7,,gccjit;;context;;release()}: @example ctxt.release (); @@ -8419,7 +8659,7 @@ The C++ class hierarchy within the @code{gccjit} namespace looks like this: One thing you can do with a @code{gccjit::object} is to ask it for a human-readable description as a @code{std::string}, using -@pxref{f2,,gccjit;;object;;get_debug_string()}: +@pxref{f8,,gccjit;;object;;get_debug_string()}: @example printf ("obj: %s\n", obj.get_debug_string ().c_str ()); @@ -8439,7 +8679,7 @@ This is invaluable when debugging. Let's create the function. To do so, we first need to construct its single parameter, specifying its type and giving it a name, -using @pxref{f3,,gccjit;;context;;new_param()}: +using @pxref{f9,,gccjit;;context;;new_param()}: @example gccjit::param param_i = ctxt.new_param (int_type, "i"); @@ -8488,7 +8728,7 @@ gccjit::block block = func.new_block (); Our basic block is relatively simple: it immediately terminates by returning the value of an expression. -We can build the expression using @pxref{f4,,gccjit;;context;;new_binary_op()}: +We can build the expression using @pxref{fa,,gccjit;;context;;new_binary_op()}: @example gccjit::rvalue expr = @@ -8501,7 +8741,7 @@ gccjit::rvalue expr = A @code{gccjit::rvalue} is another example of a @code{gccjit::object} subclass. As before, we can print it with -@pxref{f2,,gccjit;;object;;get_debug_string()}. +@pxref{f8,,gccjit;;object;;get_debug_string()}. @example printf ("expr: %s\n", expr.get_debug_string ().c_str ()); @@ -8538,7 +8778,7 @@ block.end_with_return (expr); @noindent OK, we've populated the context. We can now compile it using -@pxref{f5,,gccjit;;context;;compile()}: +@pxref{fb,,gccjit;;context;;compile()}: @example gcc_jit_result *result; @@ -8588,12 +8828,12 @@ result: 25 @end menu @node Options<3>,Full example<3>,,Tutorial part 2 Creating a trivial machine code function<2> -@anchor{cp/intro/tutorial02 options}@anchor{f6} +@anchor{cp/intro/tutorial02 options}@anchor{fc} @subsubsection Options To get more information on what's going on, you can set debugging flags -on the context using @pxref{f7,,gccjit;;context;;set_bool_option()}. +on the context using @pxref{fd,,gccjit;;context;;set_bool_option()}. @c (I'm deliberately not mentioning @c :c:macro:`GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE` here since I think @@ -8665,7 +8905,7 @@ square: By default, no optimizations are performed, the equivalent of GCC's @cite{-O0} option. We can turn things up to e.g. @cite{-O3} by calling -@pxref{f8,,gccjit;;context;;set_int_option()} with +@pxref{fe,,gccjit;;context;;set_int_option()} with @pxref{1f,,GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL}: @example @@ -8699,7 +8939,7 @@ square: Naturally this has only a small effect on such a trivial function. @node Full example<3>,,Options<3>,Tutorial part 2 Creating a trivial machine code function<2> -@anchor{cp/intro/tutorial02 full-example}@anchor{f9} +@anchor{cp/intro/tutorial02 full-example}@anchor{ff} @subsubsection Full example @@ -8842,7 +9082,7 @@ result: 25 @c <http://www.gnu.org/licenses/>. @node Tutorial part 3 Loops and variables<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2>,Tutorial part 2 Creating a trivial machine code function<2>,Tutorial<2> -@anchor{cp/intro/tutorial03 tutorial-part-3-loops-and-variables}@anchor{fa}@anchor{cp/intro/tutorial03 doc}@anchor{fb} +@anchor{cp/intro/tutorial03 tutorial-part-3-loops-and-variables}@anchor{100}@anchor{cp/intro/tutorial03 doc}@anchor{101} @subsection Tutorial part 3: Loops and variables @@ -8966,7 +9206,7 @@ gccjit::function func = @end menu @node Expressions lvalues and rvalues<2>,Control flow<2>,,Tutorial part 3 Loops and variables<2> -@anchor{cp/intro/tutorial03 expressions-lvalues-and-rvalues}@anchor{fc} +@anchor{cp/intro/tutorial03 expressions-lvalues-and-rvalues}@anchor{102} @subsubsection Expressions: lvalues and rvalues @@ -9039,7 +9279,7 @@ body of a function. Our new example has a new kind of expression: we have two local variables. We create them by calling -@pxref{fd,,gccjit;;function;;new_local()}, supplying a type and a name: +@pxref{103,,gccjit;;function;;new_local()}, supplying a type and a name: @example /* Build locals: */ @@ -9065,7 +9305,7 @@ Instead, having added the local to the function, we have to separately add an assignment of @cite{0} to @cite{local_i} at the beginning of the function. @node Control flow<2>,Visualizing the control flow graph<2>,Expressions lvalues and rvalues<2>,Tutorial part 3 Loops and variables<2> -@anchor{cp/intro/tutorial03 control-flow}@anchor{fe} +@anchor{cp/intro/tutorial03 control-flow}@anchor{104} @subsubsection Control flow @@ -9104,8 +9344,8 @@ We now populate each block with statements. The entry block @cite{b_initial} consists of initializations followed by a jump to the conditional. We assign @cite{0} to @cite{i} and to @cite{sum}, using -@pxref{ff,,gccjit;;block;;add_assignment()} to add -an assignment statement, and using @pxref{100,,gccjit;;context;;zero()} to get +@pxref{105,,gccjit;;block;;add_assignment()} to add +an assignment statement, and using @pxref{106,,gccjit;;context;;zero()} to get the constant value @cite{0} for the relevant type for the right-hand side of the assignment: @@ -9132,7 +9372,7 @@ C example. It contains a single statement: a conditional, which jumps to one of two destination blocks depending on a boolean @code{gccjit::rvalue}, in this case the comparison of @cite{i} and @cite{n}. -We could build the comparison using @pxref{101,,gccjit;;context;;new_comparison()}: +We could build the comparison using @pxref{107,,gccjit;;context;;new_comparison()}: @example gccjit::rvalue guard = @@ -9143,7 +9383,7 @@ gccjit::rvalue guard = @noindent and can then use this to add @cite{b_loop_cond}'s sole statement, via -@pxref{102,,gccjit;;block;;end_with_conditional()}: +@pxref{108,,gccjit;;block;;end_with_conditional()}: @example b_loop_cond.end_with_conditional (guard, @@ -9177,7 +9417,7 @@ Next, we populate the body of the loop. The C statement @cite{sum += i * i;} is an assignment operation, where an lvalue is modified "in-place". We use -@pxref{103,,gccjit;;block;;add_assignment_op()} to handle these operations: +@pxref{109,,gccjit;;block;;add_assignment_op()} to handle these operations: @example /* sum += i * i */ @@ -9205,7 +9445,7 @@ b_loop_body.add_assignment_op (i, @cartouche @quotation Note For numeric constants other than 0 or 1, we could use -@pxref{104,,gccjit;;context;;new_rvalue()}, which has overloads +@pxref{10a,,gccjit;;context;;new_rvalue()}, which has overloads for both @code{int} and @code{double}. @end quotation @end cartouche @@ -9281,12 +9521,12 @@ result: 285 @noindent @node Visualizing the control flow graph<2>,Full example<4>,Control flow<2>,Tutorial part 3 Loops and variables<2> -@anchor{cp/intro/tutorial03 visualizing-the-control-flow-graph}@anchor{105} +@anchor{cp/intro/tutorial03 visualizing-the-control-flow-graph}@anchor{10b} @subsubsection Visualizing the control flow graph You can see the control flow graph of a function using -@pxref{106,,gccjit;;function;;dump_to_dot()}: +@pxref{10c,,gccjit;;function;;dump_to_dot()}: @example func.dump_to_dot ("/tmp/sum-of-squares.dot"); @@ -9320,7 +9560,7 @@ install it with @cite{yum install python-xdot}): @end quotation @node Full example<4>,,Visualizing the control flow graph<2>,Tutorial part 3 Loops and variables<2> -@anchor{cp/intro/tutorial03 full-example}@anchor{107} +@anchor{cp/intro/tutorial03 full-example}@anchor{10d} @subsubsection Full example @@ -9503,7 +9743,7 @@ loop_test returned: 285 @c <http://www.gnu.org/licenses/>. @node Tutorial part 4 Adding JIT-compilation to a toy interpreter<2>,,Tutorial part 3 Loops and variables<2>,Tutorial<2> -@anchor{cp/intro/tutorial04 tutorial-part-4-adding-jit-compilation-to-a-toy-interpreter}@anchor{108}@anchor{cp/intro/tutorial04 doc}@anchor{109} +@anchor{cp/intro/tutorial04 tutorial-part-4-adding-jit-compilation-to-a-toy-interpreter}@anchor{10e}@anchor{cp/intro/tutorial04 doc}@anchor{10f} @subsection Tutorial part 4: Adding JIT-compilation to a toy interpreter @@ -9525,7 +9765,7 @@ to it. @end menu @node Our toy interpreter<2>,Compiling to machine code<2>,,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 our-toy-interpreter}@anchor{10a} +@anchor{cp/intro/tutorial04 our-toy-interpreter}@anchor{110} @subsubsection Our toy interpreter @@ -9933,7 +10173,7 @@ toyvm_function::interpret (int arg, FILE *trace) @end quotation @node Compiling to machine code<2>,Setting things up<2>,Our toy interpreter<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 compiling-to-machine-code}@anchor{10b} +@anchor{cp/intro/tutorial04 compiling-to-machine-code}@anchor{111} @subsubsection Compiling to machine code @@ -10013,7 +10253,7 @@ This means our compiler has the following state: @end quotation @node Setting things up<2>,Populating the function<2>,Compiling to machine code<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 setting-things-up}@anchor{10c} +@anchor{cp/intro/tutorial04 setting-things-up}@anchor{112} @subsubsection Setting things up @@ -10181,7 +10421,7 @@ We create the locals within the function. @end quotation @node Populating the function<2>,Verifying the control flow graph<2>,Setting things up<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 populating-the-function}@anchor{10d} +@anchor{cp/intro/tutorial04 populating-the-function}@anchor{113} @subsubsection Populating the function @@ -10309,7 +10549,7 @@ stack into @code{y} instead erroneously assigned it to @code{x}, leaving @code{y uninitialized. To track this kind of thing down, we can use -@pxref{10e,,gccjit;;block;;add_comment()} to add descriptive comments +@pxref{114,,gccjit;;block;;add_comment()} to add descriptive comments to the internal representation. This is invaluable when looking through the generated IR for, say @code{factorial}: @@ -10458,14 +10698,14 @@ to the next block. This is analogous to simply incrementing the program counter. @node Verifying the control flow graph<2>,Compiling the context<2>,Populating the function<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 verifying-the-control-flow-graph}@anchor{10f} +@anchor{cp/intro/tutorial04 verifying-the-control-flow-graph}@anchor{115} @subsubsection Verifying the control flow graph Having finished looping over the blocks, the context is complete. As before, we can verify that the control flow and statements are sane by -using @pxref{106,,gccjit;;function;;dump_to_dot()}: +using @pxref{10c,,gccjit;;function;;dump_to_dot()}: @example fn.dump_to_dot ("/tmp/factorial.dot"); @@ -10489,7 +10729,7 @@ errors in our compiler. @end quotation @node Compiling the context<2>,Single-stepping through the generated code<2>,Verifying the control flow graph<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 compiling-the-context}@anchor{110} +@anchor{cp/intro/tutorial04 compiling-the-context}@anchor{116} @subsubsection Compiling the context @@ -10526,7 +10766,7 @@ We can now run the result: @end quotation @node Single-stepping through the generated code<2>,Examining the generated code<2>,Compiling the context<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 single-stepping-through-the-generated-code}@anchor{111} +@anchor{cp/intro/tutorial04 single-stepping-through-the-generated-code}@anchor{117} @subsubsection Single-stepping through the generated code @@ -10540,14 +10780,14 @@ It's possible to debug the generated code. To do this we need to both: @item Set up source code locations for our statements, so that we can meaningfully step through the code. We did this above by -calling @pxref{112,,gccjit;;context;;new_location()} and using the +calling @pxref{118,,gccjit;;context;;new_location()} and using the results. @item Enable the generation of debugging information, by setting @pxref{42,,GCC_JIT_BOOL_OPTION_DEBUGINFO} on the @code{gccjit::context} via -@pxref{f7,,gccjit;;context;;set_bool_option()}: +@pxref{fd,,gccjit;;context;;set_bool_option()}: @example ctxt.set_bool_option (GCC_JIT_BOOL_OPTION_DEBUGINFO, 1); @@ -10619,14 +10859,14 @@ optimization level in a regular compiler. @end cartouche @node Examining the generated code<2>,Putting it all together<2>,Single-stepping through the generated code<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 examining-the-generated-code}@anchor{113} +@anchor{cp/intro/tutorial04 examining-the-generated-code}@anchor{119} @subsubsection Examining the generated code How good is the optimized code? We can turn up optimizations, by calling -@pxref{f8,,gccjit;;context;;set_int_option()} with +@pxref{fe,,gccjit;;context;;set_int_option()} with @pxref{1f,,GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL}: @example @@ -10808,7 +11048,7 @@ Note that the stack pushing and popping have been eliminated, as has the recursive call (in favor of an iteration). @node Putting it all together<2>,Behind the curtain How does our code get optimized?<2>,Examining the generated code<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 putting-it-all-together}@anchor{114} +@anchor{cp/intro/tutorial04 putting-it-all-together}@anchor{11a} @subsubsection Putting it all together @@ -10841,7 +11081,7 @@ compiler result: 55 @noindent @node Behind the curtain How does our code get optimized?<2>,,Putting it all together<2>,Tutorial part 4 Adding JIT-compilation to a toy interpreter<2> -@anchor{cp/intro/tutorial04 behind-the-curtain-how-does-our-code-get-optimized}@anchor{115} +@anchor{cp/intro/tutorial04 behind-the-curtain-how-does-our-code-get-optimized}@anchor{11b} @subsubsection Behind the curtain: How does our code get optimized? @@ -11042,7 +11282,7 @@ representation: @code{initial}, @code{instr4} and @code{instr9}. @end menu @node Optimizing away stack manipulation<2>,Elimination of tail recursion<2>,,Behind the curtain How does our code get optimized?<2> -@anchor{cp/intro/tutorial04 optimizing-away-stack-manipulation}@anchor{116} +@anchor{cp/intro/tutorial04 optimizing-away-stack-manipulation}@anchor{11c} @subsubsection Optimizing away stack manipulation @@ -11322,7 +11562,7 @@ instr9: @noindent @node Elimination of tail recursion<2>,,Optimizing away stack manipulation<2>,Behind the curtain How does our code get optimized?<2> -@anchor{cp/intro/tutorial04 elimination-of-tail-recursion}@anchor{117} +@anchor{cp/intro/tutorial04 elimination-of-tail-recursion}@anchor{11d} @subsubsection Elimination of tail recursion @@ -11409,7 +11649,7 @@ instr9: @c <http://www.gnu.org/licenses/>. @node Topic Reference<2>,,Tutorial<2>,C++ bindings for libgccjit -@anchor{cp/topics/index doc}@anchor{118}@anchor{cp/topics/index topic-reference}@anchor{119} +@anchor{cp/topics/index doc}@anchor{11e}@anchor{cp/topics/index topic-reference}@anchor{11f} @section Topic Reference @@ -11499,22 +11739,22 @@ Compiling a context @node Compilation contexts<2>,Objects<2>,,Topic Reference<2> -@anchor{cp/topics/contexts compilation-contexts}@anchor{11a}@anchor{cp/topics/contexts doc}@anchor{11b} +@anchor{cp/topics/contexts compilation-contexts}@anchor{120}@anchor{cp/topics/contexts doc}@anchor{121} @subsection Compilation contexts @geindex gccjit;;context (C++ class) -@anchor{cp/topics/contexts gccjit context}@anchor{11c} +@anchor{cp/topics/contexts gccjit context}@anchor{122} @deffn {C++ Class} gccjit::context @end deffn -The top-level of the C++ API is the @pxref{11c,,gccjit;;context} type. +The top-level of the C++ API is the @pxref{122,,gccjit;;context} type. -A @pxref{11c,,gccjit;;context} instance encapsulates the state of a +A @pxref{122,,gccjit;;context} instance encapsulates the state of a compilation. You can set up options on it, and add types, functions and code. -Invoking @pxref{f5,,gccjit;;context;;compile()} on it gives you a +Invoking @pxref{fb,,gccjit;;context;;compile()} on it gives you a @pxref{16,,gcc_jit_result *}. It is a thin wrapper around the C API's @pxref{8,,gcc_jit_context *}. @@ -11529,7 +11769,7 @@ It is a thin wrapper around the C API's @pxref{8,,gcc_jit_context *}. @end menu @node Lifetime-management<2>,Thread-safety<2>,,Compilation contexts<2> -@anchor{cp/topics/contexts lifetime-management}@anchor{11d} +@anchor{cp/topics/contexts lifetime-management}@anchor{123} @subsubsection Lifetime-management @@ -11538,16 +11778,16 @@ have their lifetime bounded by the context they are created within, and cleanup of such objects is done for you when the context is released. @geindex gccjit;;context;;acquire (C++ function) -@anchor{cp/topics/contexts gccjit context acquire}@anchor{ef} +@anchor{cp/topics/contexts gccjit context acquire}@anchor{f5} @deffn {C++ Function} gccjit::context gccjit::context::acquire () -This function acquires a new @pxref{11c,,gccjit;;context} instance, +This function acquires a new @pxref{122,,gccjit;;context} instance, which is independent of any others that may be present within this process. @end deffn @geindex gccjit;;context;;release (C++ function) -@anchor{cp/topics/contexts gccjit context release}@anchor{f1} +@anchor{cp/topics/contexts gccjit context release}@anchor{f7} @deffn {C++ Function} void gccjit::context::release () This function releases all resources associated with the given context. @@ -11566,7 +11806,7 @@ ctxt.release (); @end deffn @geindex gccjit;;context;;new_child_context (C++ function) -@anchor{cp/topics/contexts gccjit context new_child_context}@anchor{11e} +@anchor{cp/topics/contexts gccjit context new_child_context}@anchor{124} @deffn {C++ Function} gccjit::context gccjit::context::new_child_context () Given an existing JIT context, create a child context. @@ -11598,16 +11838,16 @@ there will likely be a performance hit for such nesting. @end deffn @node Thread-safety<2>,Error-handling<3>,Lifetime-management<2>,Compilation contexts<2> -@anchor{cp/topics/contexts thread-safety}@anchor{11f} +@anchor{cp/topics/contexts thread-safety}@anchor{125} @subsubsection Thread-safety -Instances of @pxref{11c,,gccjit;;context} created via -@pxref{ef,,gccjit;;context;;acquire()} are independent from each other: +Instances of @pxref{122,,gccjit;;context} created via +@pxref{f5,,gccjit;;context;;acquire()} are independent from each other: only one thread may use a given context at once, but multiple threads could each have their own contexts without needing locks. -Contexts created via @pxref{11e,,gccjit;;context;;new_child_context()} are +Contexts created via @pxref{124,,gccjit;;context;;new_child_context()} are related to their parent context. They can be partitioned by their ultimate ancestor into independent "family trees". Only one thread within a process may use a given "family tree" of such contexts at once, @@ -11615,7 +11855,7 @@ and if you're using multiple threads you should provide your own locking around entire such context partitions. @node Error-handling<3>,Debugging<2>,Thread-safety<2>,Compilation contexts<2> -@anchor{cp/topics/contexts error-handling}@anchor{120} +@anchor{cp/topics/contexts error-handling}@anchor{126} @subsubsection Error-handling @@ -11628,10 +11868,10 @@ NULL. You don't have to check everywhere for NULL results, since the API gracefully handles a NULL being passed in for any argument. Errors are printed on stderr and can be queried using -@pxref{121,,gccjit;;context;;get_first_error()}. +@pxref{127,,gccjit;;context;;get_first_error()}. @geindex gccjit;;context;;get_first_error (C++ function) -@anchor{cp/topics/contexts gccjit context get_first_error__gccjit contextP}@anchor{121} +@anchor{cp/topics/contexts gccjit context get_first_error__gccjit contextP}@anchor{127} @deffn {C++ Function} const char* gccjit::context::get_first_error (gccjit::context* ctxt) Returns the first error message that occurred on the context. @@ -11643,18 +11883,18 @@ If no errors occurred, this will be NULL. @end deffn @node Debugging<2>,Options<4>,Error-handling<3>,Compilation contexts<2> -@anchor{cp/topics/contexts debugging}@anchor{122} +@anchor{cp/topics/contexts debugging}@anchor{128} @subsubsection Debugging @geindex gccjit;;context;;dump_to_file (C++ function) -@anchor{cp/topics/contexts gccjit context dump_to_file__ssCR i}@anchor{123} +@anchor{cp/topics/contexts gccjit context dump_to_file__ssCR i}@anchor{129} @deffn {C++ Function} void gccjit::context::dump_to_file (const std::string& path, int update_locations) To help with debugging: dump a C-like representation to the given path, describing what's been set up on the context. -If "update_locations" is true, then also set up @pxref{124,,gccjit;;location} +If "update_locations" is true, then also set up @pxref{12a,,gccjit;;location} information throughout the context, pointing at the dump file as if it were a source file. This may be of use in conjunction with @code{GCCJIT::BOOL_OPTION_DEBUGINFO} to allow stepping through the @@ -11662,7 +11902,7 @@ code in a debugger. @end deffn @geindex gccjit;;context;;dump_reproducer_to_file (C++ function) -@anchor{cp/topics/contexts gccjit context dump_reproducer_to_file__gcc_jit_contextP cCP}@anchor{125} +@anchor{cp/topics/contexts gccjit context dump_reproducer_to_file__gcc_jit_contextP cCP}@anchor{12b} @deffn {C++ Function} void gccjit::context::dump_reproducer_to_file (gcc_jit_context* ctxt, const char* path) This is a thin wrapper around the C API @@ -11674,7 +11914,7 @@ for seeing what the C++ bindings are doing at the C level. @end deffn @node Options<4>,,Debugging<2>,Compilation contexts<2> -@anchor{cp/topics/contexts options}@anchor{126} +@anchor{cp/topics/contexts options}@anchor{12c} @subsubsection Options @@ -11687,12 +11927,12 @@ for seeing what the C++ bindings are doing at the C level. @end menu @node String Options<2>,Boolean options<2>,,Options<4> -@anchor{cp/topics/contexts string-options}@anchor{127} +@anchor{cp/topics/contexts string-options}@anchor{12d} @subsubsection String Options @geindex gccjit;;context;;set_str_option (C++ function) -@anchor{cp/topics/contexts gccjit context set_str_option__enum cCP}@anchor{128} +@anchor{cp/topics/contexts gccjit context set_str_option__enum cCP}@anchor{12e} @deffn {C++ Function} void gccjit::context::set_str_option (enum gcc_jit_str_option, const char* value) Set a string option of the context. @@ -11703,12 +11943,12 @@ meaning. @end deffn @node Boolean options<2>,Integer options<2>,String Options<2>,Options<4> -@anchor{cp/topics/contexts boolean-options}@anchor{129} +@anchor{cp/topics/contexts boolean-options}@anchor{12f} @subsubsection Boolean options @geindex gccjit;;context;;set_bool_option (C++ function) -@anchor{cp/topics/contexts gccjit context set_bool_option__enum i}@anchor{f7} +@anchor{cp/topics/contexts gccjit context set_bool_option__enum i}@anchor{fd} @deffn {C++ Function} void gccjit::context::set_bool_option (enum gcc_jit_bool_option, int value) Set a boolean option of the context. @@ -11719,7 +11959,7 @@ meaning. @end deffn @geindex gccjit;;context;;set_bool_allow_unreachable_blocks (C++ function) -@anchor{cp/topics/contexts gccjit context set_bool_allow_unreachable_blocks__i}@anchor{12a} +@anchor{cp/topics/contexts gccjit context set_bool_allow_unreachable_blocks__i}@anchor{130} @deffn {C++ Function} void gccjit::context::set_bool_allow_unreachable_blocks (int bool_value) By default, libgccjit will issue an error about unreachable blocks @@ -11740,12 +11980,12 @@ its presence using @end deffn @node Integer options<2>,Additional command-line options<2>,Boolean options<2>,Options<4> -@anchor{cp/topics/contexts integer-options}@anchor{12b} +@anchor{cp/topics/contexts integer-options}@anchor{131} @subsubsection Integer options @geindex gccjit;;context;;set_int_option (C++ function) -@anchor{cp/topics/contexts gccjit context set_int_option__enum i}@anchor{f8} +@anchor{cp/topics/contexts gccjit context set_int_option__enum i}@anchor{fe} @deffn {C++ Function} void gccjit::context::set_int_option (enum gcc_jit_int_option, int value) Set an integer option of the context. @@ -11756,12 +11996,12 @@ meaning. @end deffn @node Additional command-line options<2>,,Integer options<2>,Options<4> -@anchor{cp/topics/contexts additional-command-line-options}@anchor{12c} +@anchor{cp/topics/contexts additional-command-line-options}@anchor{132} @subsubsection Additional command-line options @geindex gccjit;;context;;add_command_line_option (C++ function) -@anchor{cp/topics/contexts gccjit context add_command_line_option__cCP}@anchor{12d} +@anchor{cp/topics/contexts gccjit context add_command_line_option__cCP}@anchor{133} @deffn {C++ Function} void gccjit::context::add_command_line_option (const char* optname) Add an arbitrary gcc command-line option to the context for use @@ -11798,18 +12038,18 @@ its presence using @c <http://www.gnu.org/licenses/>. @node Objects<2>,Types<2>,Compilation contexts<2>,Topic Reference<2> -@anchor{cp/topics/objects objects}@anchor{12e}@anchor{cp/topics/objects doc}@anchor{12f} +@anchor{cp/topics/objects objects}@anchor{134}@anchor{cp/topics/objects doc}@anchor{135} @subsection Objects @geindex gccjit;;object (C++ class) -@anchor{cp/topics/objects gccjit object}@anchor{130} +@anchor{cp/topics/objects gccjit object}@anchor{136} @deffn {C++ Class} gccjit::object @end deffn Almost every entity in the API (with the exception of -@pxref{11c,,gccjit;;context} and @pxref{16,,gcc_jit_result *}) is a -"contextual" object, a @pxref{130,,gccjit;;object}. +@pxref{122,,gccjit;;context} and @pxref{16,,gcc_jit_result *}) is a +"contextual" object, a @pxref{136,,gccjit;;object}. A JIT object: @@ -11819,7 +12059,7 @@ A JIT object: @itemize * @item -is associated with a @pxref{11c,,gccjit;;context}. +is associated with a @pxref{122,,gccjit;;context}. @item is automatically cleaned up for you when its context is released so @@ -11841,21 +12081,22 @@ The C++ class hierarchy within the @code{gccjit} namespace looks like this: +- rvalue +- lvalue +- param + +- case_ @end example @noindent -The @pxref{130,,gccjit;;object} base class has the following operations: +The @pxref{136,,gccjit;;object} base class has the following operations: @geindex gccjit;;object;;get_context (C++ function) -@anchor{cp/topics/objects gccjit object get_contextC}@anchor{131} +@anchor{cp/topics/objects gccjit object get_contextC}@anchor{137} @deffn {C++ Function} gccjit::context gccjit::object::get_context () const Which context is the obj within? @end deffn @geindex gccjit;;object;;get_debug_string (C++ function) -@anchor{cp/topics/objects gccjit object get_debug_stringC}@anchor{f2} +@anchor{cp/topics/objects gccjit object get_debug_stringC}@anchor{f8} @deffn {C++ Function} std::string gccjit::object::get_debug_string () const Generate a human-readable description for the given object. @@ -11895,16 +12136,16 @@ obj: 4.0 * (float)i @c <http://www.gnu.org/licenses/>. @node Types<2>,Expressions<2>,Objects<2>,Topic Reference<2> -@anchor{cp/topics/types doc}@anchor{132}@anchor{cp/topics/types types}@anchor{133} +@anchor{cp/topics/types doc}@anchor{138}@anchor{cp/topics/types types}@anchor{139} @subsection Types @geindex gccjit;;type (C++ class) -@anchor{cp/topics/types gccjit type}@anchor{134} +@anchor{cp/topics/types gccjit type}@anchor{13a} @deffn {C++ Class} gccjit::type gccjit::type represents a type within the library. It is a subclass -of @pxref{130,,gccjit;;object}. +of @pxref{136,,gccjit;;object}. @end deffn Types can be created in several ways: @@ -11914,7 +12155,7 @@ Types can be created in several ways: @item fundamental types can be accessed using -@pxref{f0,,gccjit;;context;;get_type()}: +@pxref{f6,,gccjit;;context;;get_type()}: @example gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT); @@ -11934,7 +12175,7 @@ See @pxref{b,,gcc_jit_context_get_type()} for the available types. @item derived types can be accessed by using functions such as -@pxref{135,,gccjit;;type;;get_pointer()} and @pxref{136,,gccjit;;type;;get_const()}: +@pxref{13b,,gccjit;;type;;get_pointer()} and @pxref{13c,,gccjit;;type;;get_const()}: @example gccjit::type const_int_star = int_type.get_const ().get_pointer (); @@ -11955,12 +12196,12 @@ by creating structures (see below). @end menu @node Standard types<2>,Pointers const and volatile<2>,,Types<2> -@anchor{cp/topics/types standard-types}@anchor{137} +@anchor{cp/topics/types standard-types}@anchor{13d} @subsubsection Standard types @geindex gccjit;;context;;get_type (C++ function) -@anchor{cp/topics/types gccjit context get_type__enum}@anchor{f0} +@anchor{cp/topics/types gccjit context get_type__enum}@anchor{f6} @deffn {C++ Function} gccjit::type gccjit::context::get_type (enum gcc_jit_types) Access a specific type. This is a thin wrapper around @@ -11968,14 +12209,14 @@ Access a specific type. This is a thin wrapper around @end deffn @geindex gccjit;;context;;get_int_type (C++ function) -@anchor{cp/topics/types gccjit context get_int_type__s i}@anchor{138} +@anchor{cp/topics/types gccjit context get_int_type__s i}@anchor{13e} @deffn {C++ Function} gccjit::type gccjit::context::get_int_type (size_t num_bytes, int is_signed) Access the integer type of the given size. @end deffn @geindex gccjit;;context;;get_int_type<T> (C++ function) -@anchor{cp/topics/types gccjit context get_int_type T}@anchor{139} +@anchor{cp/topics/types gccjit context get_int_type T}@anchor{13f} @deffn {C++ Function} gccjit::type gccjit::context::get_int_type<T> () Access the given integer type. For example, you could map the @@ -11989,12 +12230,12 @@ gccjit::type t = ctxt.get_int_type <unsigned short> (); @end deffn @node Pointers const and volatile<2>,Structures and unions<2>,Standard types<2>,Types<2> -@anchor{cp/topics/types pointers-const-and-volatile}@anchor{13a} +@anchor{cp/topics/types pointers-const-and-volatile}@anchor{140} @subsubsection Pointers, @cite{const}, and @cite{volatile} @geindex gccjit;;type;;get_pointer (C++ function) -@anchor{cp/topics/types gccjit type get_pointer}@anchor{135} +@anchor{cp/topics/types gccjit type get_pointer}@anchor{13b} @deffn {C++ Function} gccjit::type gccjit::type::get_pointer () Given type "T", get type "T*". @@ -12003,21 +12244,21 @@ Given type "T", get type "T*". @c FIXME: get_const doesn't seem to exist @geindex gccjit;;type;;get_const (C++ function) -@anchor{cp/topics/types gccjit type get_const}@anchor{136} +@anchor{cp/topics/types gccjit type get_const}@anchor{13c} @deffn {C++ Function} gccjit::type gccjit::type::get_const () Given type "T", get type "const T". @end deffn @geindex gccjit;;type;;get_volatile (C++ function) -@anchor{cp/topics/types gccjit type get_volatile}@anchor{13b} +@anchor{cp/topics/types gccjit type get_volatile}@anchor{141} @deffn {C++ Function} gccjit::type gccjit::type::get_volatile () Given type "T", get type "volatile T". @end deffn @geindex gccjit;;context;;new_array_type (C++ function) -@anchor{cp/topics/types gccjit context new_array_type__gccjit type i gccjit location}@anchor{13c} +@anchor{cp/topics/types gccjit context new_array_type__gccjit type i gccjit location}@anchor{142} @deffn {C++ Function} gccjit::type gccjit::context::new_array_type (gccjit::type element_type, int num_elements, gccjit::location loc) Given type "T", get type "T[N]" (for a constant N). @@ -12025,31 +12266,31 @@ Param "loc" is optional. @end deffn @node Structures and unions<2>,,Pointers const and volatile<2>,Types<2> -@anchor{cp/topics/types structures-and-unions}@anchor{13d} +@anchor{cp/topics/types structures-and-unions}@anchor{143} @subsubsection Structures and unions @geindex gccjit;;struct_ (C++ class) -@anchor{cp/topics/types gccjit struct_}@anchor{13e} +@anchor{cp/topics/types gccjit struct_}@anchor{144} @deffn {C++ Class} gccjit::struct_ @end deffn A compound type analagous to a C @cite{struct}. -@pxref{13e,,gccjit;;struct_} is a subclass of @pxref{134,,gccjit;;type} (and thus -of @pxref{130,,gccjit;;object} in turn). +@pxref{144,,gccjit;;struct_} is a subclass of @pxref{13a,,gccjit;;type} (and thus +of @pxref{136,,gccjit;;object} in turn). @geindex gccjit;;field (C++ class) -@anchor{cp/topics/types gccjit field}@anchor{13f} +@anchor{cp/topics/types gccjit field}@anchor{145} @deffn {C++ Class} gccjit::field @end deffn -A field within a @pxref{13e,,gccjit;;struct_}. +A field within a @pxref{144,,gccjit;;struct_}. -@pxref{13f,,gccjit;;field} is a subclass of @pxref{130,,gccjit;;object}. +@pxref{145,,gccjit;;field} is a subclass of @pxref{136,,gccjit;;object}. -You can model C @cite{struct} types by creating @pxref{13e,,gccjit;;struct_} and -@pxref{13f,,gccjit;;field} instances, in either order: +You can model C @cite{struct} types by creating @pxref{144,,gccjit;;struct_} and +@pxref{145,,gccjit;;field} instances, in either order: @itemize * @@ -12105,14 +12346,14 @@ node.set_fields (fields); @c FIXME: the above API doesn't seem to exist yet @geindex gccjit;;context;;new_field (C++ function) -@anchor{cp/topics/types gccjit context new_field__gccjit type cCP gccjit location}@anchor{140} +@anchor{cp/topics/types gccjit context new_field__gccjit type cCP gccjit location}@anchor{146} @deffn {C++ Function} gccjit::field gccjit::context::new_field (gccjit::type type, const char* name, gccjit::location loc) Construct a new field, with the given type and name. @end deffn @geindex gccjit;;context;;new_struct_type (C++ function) -@anchor{cp/topics/types gccjit context new_struct_type__ssCR std vector field R gccjit location}@anchor{141} +@anchor{cp/topics/types gccjit context new_struct_type__ssCR std vector field R gccjit location}@anchor{147} @deffn {C++ Function} gccjit::struct_ gccjit::context::new_struct_type (const std::string& name, std::vector<field>& fields, gccjit::location loc) @quotation @@ -12122,7 +12363,7 @@ Construct a new struct type, with the given name and fields. @end deffn @geindex gccjit;;context;;new_opaque_struct (C++ function) -@anchor{cp/topics/types gccjit context new_opaque_struct__ssCR gccjit location}@anchor{142} +@anchor{cp/topics/types gccjit context new_opaque_struct__ssCR gccjit location}@anchor{148} @deffn {C++ Function} gccjit::struct_ gccjit::context::new_opaque_struct (const std::string& name, gccjit::location loc) Construct a new struct type, with the given name, but without @@ -12149,7 +12390,7 @@ size of the struct is not known), or later specified using @c <http://www.gnu.org/licenses/>. @node Expressions<2>,Creating and using functions<2>,Types<2>,Topic Reference<2> -@anchor{cp/topics/expressions expressions}@anchor{143}@anchor{cp/topics/expressions doc}@anchor{144} +@anchor{cp/topics/expressions expressions}@anchor{149}@anchor{cp/topics/expressions doc}@anchor{14a} @subsection Expressions @@ -12175,17 +12416,17 @@ Lvalues @node Rvalues<2>,Lvalues<2>,,Expressions<2> -@anchor{cp/topics/expressions rvalues}@anchor{145} +@anchor{cp/topics/expressions rvalues}@anchor{14b} @subsubsection Rvalues @geindex gccjit;;rvalue (C++ class) -@anchor{cp/topics/expressions gccjit rvalue}@anchor{146} +@anchor{cp/topics/expressions gccjit rvalue}@anchor{14c} @deffn {C++ Class} gccjit::rvalue @end deffn -A @pxref{146,,gccjit;;rvalue} is an expression that can be computed. It is a -subclass of @pxref{130,,gccjit;;object}, and is a thin wrapper around +A @pxref{14c,,gccjit;;rvalue} is an expression that can be computed. It is a +subclass of @pxref{136,,gccjit;;object}, and is a thin wrapper around @pxref{13,,gcc_jit_rvalue *} from the C API. It can be simple, e.g.: @@ -12231,7 +12472,7 @@ Every rvalue has an associated type, and the API will check to ensure that types match up correctly (otherwise the context will emit an error). @geindex gccjit;;rvalue;;get_type (C++ function) -@anchor{cp/topics/expressions gccjit rvalue get_type}@anchor{147} +@anchor{cp/topics/expressions gccjit rvalue get_type}@anchor{14d} @deffn {C++ Function} gccjit::type gccjit::rvalue::get_type () Get the type of this rvalue. @@ -12248,12 +12489,12 @@ Get the type of this rvalue. @end menu @node Simple expressions<2>,Unary Operations<2>,,Rvalues<2> -@anchor{cp/topics/expressions simple-expressions}@anchor{148} +@anchor{cp/topics/expressions simple-expressions}@anchor{14e} @subsubsection Simple expressions @geindex gccjit;;context;;new_rvalue (C++ function) -@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type iC}@anchor{104} +@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type iC}@anchor{10a} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_rvalue (gccjit::type numeric_type, int value) const Given a numeric type (integer or floating point), build an rvalue for @@ -12261,7 +12502,7 @@ the given constant @code{int} value. @end deffn @geindex gccjit;;context;;new_rvalue (C++ function) -@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type lC}@anchor{149} +@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type lC}@anchor{14f} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_rvalue (gccjit::type numeric_type, long value) const Given a numeric type (integer or floating point), build an rvalue for @@ -12269,7 +12510,7 @@ the given constant @code{long} value. @end deffn @geindex gccjit;;context;;zero (C++ function) -@anchor{cp/topics/expressions gccjit context zero__gccjit typeC}@anchor{100} +@anchor{cp/topics/expressions gccjit context zero__gccjit typeC}@anchor{106} @deffn {C++ Function} gccjit::rvalue gccjit::context::zero (gccjit::type numeric_type) const Given a numeric type (integer or floating point), get the rvalue for @@ -12283,7 +12524,7 @@ ctxt.new_rvalue (numeric_type, 0) @end deffn @geindex gccjit;;context;;one (C++ function) -@anchor{cp/topics/expressions gccjit context one__gccjit typeC}@anchor{14a} +@anchor{cp/topics/expressions gccjit context one__gccjit typeC}@anchor{150} @deffn {C++ Function} gccjit::rvalue gccjit::context::one (gccjit::type numeric_type) const Given a numeric type (integer or floating point), get the rvalue for @@ -12297,7 +12538,7 @@ ctxt.new_rvalue (numeric_type, 1) @end deffn @geindex gccjit;;context;;new_rvalue (C++ function) -@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type doubleC}@anchor{14b} +@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type doubleC}@anchor{151} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_rvalue (gccjit::type numeric_type, double value) const Given a numeric type (integer or floating point), build an rvalue for @@ -12305,14 +12546,14 @@ the given constant @code{double} value. @end deffn @geindex gccjit;;context;;new_rvalue (C++ function) -@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type voidPC}@anchor{14c} +@anchor{cp/topics/expressions gccjit context new_rvalue__gccjit type voidPC}@anchor{152} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_rvalue (gccjit::type pointer_type, void* value) const Given a pointer type, build an rvalue for the given address. @end deffn @geindex gccjit;;context;;new_rvalue (C++ function) -@anchor{cp/topics/expressions gccjit context new_rvalue__ssCRC}@anchor{14d} +@anchor{cp/topics/expressions gccjit context new_rvalue__ssCRC}@anchor{153} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_rvalue (const std::string& value) const Generate an rvalue of type @code{GCC_JIT_TYPE_CONST_CHAR_PTR} for @@ -12320,12 +12561,12 @@ the given string. This is akin to a string literal. @end deffn @node Unary Operations<2>,Binary Operations<2>,Simple expressions<2>,Rvalues<2> -@anchor{cp/topics/expressions unary-operations}@anchor{14e} +@anchor{cp/topics/expressions unary-operations}@anchor{154} @subsubsection Unary Operations @geindex gccjit;;context;;new_unary_op (C++ function) -@anchor{cp/topics/expressions gccjit context new_unary_op__enum gccjit type gccjit rvalue gccjit location}@anchor{14f} +@anchor{cp/topics/expressions gccjit context new_unary_op__enum gccjit type gccjit rvalue gccjit location}@anchor{155} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_unary_op (enum gcc_jit_unary_op, gccjit::type result_type, gccjit::rvalue rvalue, gccjit::location loc) Build a unary operation out of an input rvalue. @@ -12341,7 +12582,7 @@ There are shorter ways to spell the various specific kinds of unary operation: @geindex gccjit;;context;;new_minus (C++ function) -@anchor{cp/topics/expressions gccjit context new_minus__gccjit type gccjit rvalue gccjit location}@anchor{150} +@anchor{cp/topics/expressions gccjit context new_minus__gccjit type gccjit rvalue gccjit location}@anchor{156} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_minus (gccjit::type result_type, gccjit::rvalue a, gccjit::location loc) Negate an arithmetic value; for example: @@ -12362,7 +12603,7 @@ builds the equivalent of this C expression: @end deffn @geindex new_bitwise_negate (C++ function) -@anchor{cp/topics/expressions new_bitwise_negate__gccjit type gccjit rvalue gccjit location}@anchor{151} +@anchor{cp/topics/expressions new_bitwise_negate__gccjit type gccjit rvalue gccjit location}@anchor{157} @deffn {C++ Function} gccjit::rvalue new_bitwise_negate (gccjit::type result_type, gccjit::rvalue a, gccjit::location loc) Bitwise negation of an integer value (one's complement); for example: @@ -12383,7 +12624,7 @@ builds the equivalent of this C expression: @end deffn @geindex new_logical_negate (C++ function) -@anchor{cp/topics/expressions new_logical_negate__gccjit type gccjit rvalue gccjit location}@anchor{152} +@anchor{cp/topics/expressions new_logical_negate__gccjit type gccjit rvalue gccjit location}@anchor{158} @deffn {C++ Function} gccjit::rvalue new_logical_negate (gccjit::type result_type, gccjit::rvalue a, gccjit::location loc) Logical negation of an arithmetic or pointer value; for example: @@ -12406,7 +12647,7 @@ builds the equivalent of this C expression: The most concise way to spell them is with overloaded operators: @geindex operator- (C++ function) -@anchor{cp/topics/expressions sub-operator__gccjit rvalue}@anchor{153} +@anchor{cp/topics/expressions sub-operator__gccjit rvalue}@anchor{159} @deffn {C++ Function} gccjit::rvalue operator- (gccjit::rvalue a) @example @@ -12417,7 +12658,7 @@ gccjit::rvalue negpi = -pi; @end deffn @geindex operator~ (C++ function) -@anchor{cp/topics/expressions inv-operator__gccjit rvalue}@anchor{154} +@anchor{cp/topics/expressions inv-operator__gccjit rvalue}@anchor{15a} @deffn {C++ Function} gccjit::rvalue operator~ (gccjit::rvalue a) @example @@ -12428,7 +12669,7 @@ gccjit::rvalue mask = ~a; @end deffn @geindex operator! (C++ function) -@anchor{cp/topics/expressions not-operator__gccjit rvalue}@anchor{155} +@anchor{cp/topics/expressions not-operator__gccjit rvalue}@anchor{15b} @deffn {C++ Function} gccjit::rvalue operator! (gccjit::rvalue a) @example @@ -12439,12 +12680,12 @@ gccjit::rvalue guard = !cond; @end deffn @node Binary Operations<2>,Comparisons<2>,Unary Operations<2>,Rvalues<2> -@anchor{cp/topics/expressions binary-operations}@anchor{156} +@anchor{cp/topics/expressions binary-operations}@anchor{15c} @subsubsection Binary Operations @geindex gccjit;;context;;new_binary_op (C++ function) -@anchor{cp/topics/expressions gccjit context new_binary_op__enum gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{f4} +@anchor{cp/topics/expressions gccjit context new_binary_op__enum gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{fa} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_binary_op (enum gcc_jit_binary_op, gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) Build a binary operation out of two constituent rvalues. @@ -12460,59 +12701,59 @@ There are shorter ways to spell the various specific kinds of binary operation: @geindex gccjit;;context;;new_plus (C++ function) -@anchor{cp/topics/expressions gccjit context new_plus__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{157} +@anchor{cp/topics/expressions gccjit context new_plus__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15d} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_plus (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_minus (C++ function) -@anchor{cp/topics/expressions gccjit context new_minus__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{158} +@anchor{cp/topics/expressions gccjit context new_minus__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15e} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_minus (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_mult (C++ function) -@anchor{cp/topics/expressions gccjit context new_mult__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{159} +@anchor{cp/topics/expressions gccjit context new_mult__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15f} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_mult (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_divide (C++ function) -@anchor{cp/topics/expressions gccjit context new_divide__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15a} +@anchor{cp/topics/expressions gccjit context new_divide__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{160} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_divide (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_modulo (C++ function) -@anchor{cp/topics/expressions gccjit context new_modulo__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15b} +@anchor{cp/topics/expressions gccjit context new_modulo__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{161} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_modulo (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_bitwise_and (C++ function) -@anchor{cp/topics/expressions gccjit context new_bitwise_and__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15c} +@anchor{cp/topics/expressions gccjit context new_bitwise_and__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{162} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_bitwise_and (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_bitwise_xor (C++ function) -@anchor{cp/topics/expressions gccjit context new_bitwise_xor__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15d} +@anchor{cp/topics/expressions gccjit context new_bitwise_xor__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{163} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_bitwise_xor (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_bitwise_or (C++ function) -@anchor{cp/topics/expressions gccjit context new_bitwise_or__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15e} +@anchor{cp/topics/expressions gccjit context new_bitwise_or__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{164} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_bitwise_or (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_logical_and (C++ function) -@anchor{cp/topics/expressions gccjit context new_logical_and__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{15f} +@anchor{cp/topics/expressions gccjit context new_logical_and__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{165} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_logical_and (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_logical_or (C++ function) -@anchor{cp/topics/expressions gccjit context new_logical_or__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{160} +@anchor{cp/topics/expressions gccjit context new_logical_or__gccjit type gccjit rvalue gccjit rvalue gccjit location}@anchor{166} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_logical_or (gccjit::type result_type, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn The most concise way to spell them is with overloaded operators: @geindex operator+ (C++ function) -@anchor{cp/topics/expressions add-operator__gccjit rvalue gccjit rvalue}@anchor{161} +@anchor{cp/topics/expressions add-operator__gccjit rvalue gccjit rvalue}@anchor{167} @deffn {C++ Function} gccjit::rvalue operator+ (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12523,7 +12764,7 @@ gccjit::rvalue sum = a + b; @end deffn @geindex operator- (C++ function) -@anchor{cp/topics/expressions sub-operator__gccjit rvalue gccjit rvalue}@anchor{162} +@anchor{cp/topics/expressions sub-operator__gccjit rvalue gccjit rvalue}@anchor{168} @deffn {C++ Function} gccjit::rvalue operator- (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12534,7 +12775,7 @@ gccjit::rvalue diff = a - b; @end deffn @geindex operator* (C++ function) -@anchor{cp/topics/expressions mul-operator__gccjit rvalue gccjit rvalue}@anchor{163} +@anchor{cp/topics/expressions mul-operator__gccjit rvalue gccjit rvalue}@anchor{169} @deffn {C++ Function} gccjit::rvalue operator* (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12545,7 +12786,7 @@ gccjit::rvalue prod = a * b; @end deffn @geindex operator/ (C++ function) -@anchor{cp/topics/expressions div-operator__gccjit rvalue gccjit rvalue}@anchor{164} +@anchor{cp/topics/expressions div-operator__gccjit rvalue gccjit rvalue}@anchor{16a} @deffn {C++ Function} gccjit::rvalue operator/ (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12556,7 +12797,7 @@ gccjit::rvalue result = a / b; @end deffn @geindex operator% (C++ function) -@anchor{cp/topics/expressions mod-operator__gccjit rvalue gccjit rvalue}@anchor{165} +@anchor{cp/topics/expressions mod-operator__gccjit rvalue gccjit rvalue}@anchor{16b} @deffn {C++ Function} gccjit::rvalue operator% (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12567,7 +12808,7 @@ gccjit::rvalue mod = a % b; @end deffn @geindex operator& (C++ function) -@anchor{cp/topics/expressions and-operator__gccjit rvalue gccjit rvalue}@anchor{166} +@anchor{cp/topics/expressions and-operator__gccjit rvalue gccjit rvalue}@anchor{16c} @deffn {C++ Function} gccjit::rvalue operator& (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12578,7 +12819,7 @@ gccjit::rvalue x = a & b; @end deffn @geindex operator^ (C++ function) -@anchor{cp/topics/expressions xor-operator__gccjit rvalue gccjit rvalue}@anchor{167} +@anchor{cp/topics/expressions xor-operator__gccjit rvalue gccjit rvalue}@anchor{16d} @deffn {C++ Function} gccjit::rvalue operator^ (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12589,7 +12830,7 @@ gccjit::rvalue x = a ^ b; @end deffn @geindex operator| (C++ function) -@anchor{cp/topics/expressions or-operator__gccjit rvalue gccjit rvalue}@anchor{168} +@anchor{cp/topics/expressions or-operator__gccjit rvalue gccjit rvalue}@anchor{16e} @deffn {C++ Function} gccjit::rvalue operator| (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12600,7 +12841,7 @@ gccjit::rvalue x = a | b; @end deffn @geindex operator&& (C++ function) -@anchor{cp/topics/expressions sand-operator__gccjit rvalue gccjit rvalue}@anchor{169} +@anchor{cp/topics/expressions sand-operator__gccjit rvalue gccjit rvalue}@anchor{16f} @deffn {C++ Function} gccjit::rvalue operator&& (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12611,7 +12852,7 @@ gccjit::rvalue cond = a && b; @end deffn @geindex operator|| (C++ function) -@anchor{cp/topics/expressions sor-operator__gccjit rvalue gccjit rvalue}@anchor{16a} +@anchor{cp/topics/expressions sor-operator__gccjit rvalue gccjit rvalue}@anchor{170} @deffn {C++ Function} gccjit::rvalue operator|| (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12634,12 +12875,12 @@ gccjit::rvalue discriminant = (b * b) - (four * a * c); @end quotation @node Comparisons<2>,Function calls<2>,Binary Operations<2>,Rvalues<2> -@anchor{cp/topics/expressions comparisons}@anchor{16b} +@anchor{cp/topics/expressions comparisons}@anchor{171} @subsubsection Comparisons @geindex gccjit;;context;;new_comparison (C++ function) -@anchor{cp/topics/expressions gccjit context new_comparison__enum gccjit rvalue gccjit rvalue gccjit location}@anchor{101} +@anchor{cp/topics/expressions gccjit context new_comparison__enum gccjit rvalue gccjit rvalue gccjit location}@anchor{107} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_comparison (enum gcc_jit_comparison, gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) Build a boolean rvalue out of the comparison of two other rvalues. @@ -12655,39 +12896,39 @@ There are shorter ways to spell the various specific kinds of binary operation: @geindex gccjit;;context;;new_eq (C++ function) -@anchor{cp/topics/expressions gccjit context new_eq__gccjit rvalue gccjit rvalue gccjit location}@anchor{16c} +@anchor{cp/topics/expressions gccjit context new_eq__gccjit rvalue gccjit rvalue gccjit location}@anchor{172} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_eq (gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_ne (C++ function) -@anchor{cp/topics/expressions gccjit context new_ne__gccjit rvalue gccjit rvalue gccjit location}@anchor{16d} +@anchor{cp/topics/expressions gccjit context new_ne__gccjit rvalue gccjit rvalue gccjit location}@anchor{173} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_ne (gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_lt (C++ function) -@anchor{cp/topics/expressions gccjit context new_lt__gccjit rvalue gccjit rvalue gccjit location}@anchor{16e} +@anchor{cp/topics/expressions gccjit context new_lt__gccjit rvalue gccjit rvalue gccjit location}@anchor{174} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_lt (gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_le (C++ function) -@anchor{cp/topics/expressions gccjit context new_le__gccjit rvalue gccjit rvalue gccjit location}@anchor{16f} +@anchor{cp/topics/expressions gccjit context new_le__gccjit rvalue gccjit rvalue gccjit location}@anchor{175} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_le (gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_gt (C++ function) -@anchor{cp/topics/expressions gccjit context new_gt__gccjit rvalue gccjit rvalue gccjit location}@anchor{170} +@anchor{cp/topics/expressions gccjit context new_gt__gccjit rvalue gccjit rvalue gccjit location}@anchor{176} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_gt (gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn @geindex gccjit;;context;;new_ge (C++ function) -@anchor{cp/topics/expressions gccjit context new_ge__gccjit rvalue gccjit rvalue gccjit location}@anchor{171} +@anchor{cp/topics/expressions gccjit context new_ge__gccjit rvalue gccjit rvalue gccjit location}@anchor{177} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_ge (gccjit::rvalue a, gccjit::rvalue b, gccjit::location loc) @end deffn The most concise way to spell them is with overloaded operators: @geindex operator== (C++ function) -@anchor{cp/topics/expressions eq-operator__gccjit rvalue gccjit rvalue}@anchor{172} +@anchor{cp/topics/expressions eq-operator__gccjit rvalue gccjit rvalue}@anchor{178} @deffn {C++ Function} gccjit::rvalue operator== (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12698,7 +12939,7 @@ gccjit::rvalue cond = (a == ctxt.zero (t_int)); @end deffn @geindex operator!= (C++ function) -@anchor{cp/topics/expressions neq-operator__gccjit rvalue gccjit rvalue}@anchor{173} +@anchor{cp/topics/expressions neq-operator__gccjit rvalue gccjit rvalue}@anchor{179} @deffn {C++ Function} gccjit::rvalue operator!= (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12709,7 +12950,7 @@ gccjit::rvalue cond = (i != j); @end deffn @geindex operator< (C++ function) -@anchor{cp/topics/expressions lt-operator__gccjit rvalue gccjit rvalue}@anchor{174} +@anchor{cp/topics/expressions lt-operator__gccjit rvalue gccjit rvalue}@anchor{17a} @deffn {C++ Function} gccjit::rvalue operator< (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12720,7 +12961,7 @@ gccjit::rvalue cond = i < n; @end deffn @geindex operator<= (C++ function) -@anchor{cp/topics/expressions lte-operator__gccjit rvalue gccjit rvalue}@anchor{175} +@anchor{cp/topics/expressions lte-operator__gccjit rvalue gccjit rvalue}@anchor{17b} @deffn {C++ Function} gccjit::rvalue operator<= (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12731,7 +12972,7 @@ gccjit::rvalue cond = i <= n; @end deffn @geindex operator> (C++ function) -@anchor{cp/topics/expressions gt-operator__gccjit rvalue gccjit rvalue}@anchor{176} +@anchor{cp/topics/expressions gt-operator__gccjit rvalue gccjit rvalue}@anchor{17c} @deffn {C++ Function} gccjit::rvalue operator> (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12742,7 +12983,7 @@ gccjit::rvalue cond = (ch > limit); @end deffn @geindex operator>= (C++ function) -@anchor{cp/topics/expressions gte-operator__gccjit rvalue gccjit rvalue}@anchor{177} +@anchor{cp/topics/expressions gte-operator__gccjit rvalue gccjit rvalue}@anchor{17d} @deffn {C++ Function} gccjit::rvalue operator>= (gccjit::rvalue a, gccjit::rvalue b) @example @@ -12755,12 +12996,12 @@ gccjit::rvalue cond = (score >= ctxt.new_rvalue (t_int, 100)); @c TODO: beyond this point @node Function calls<2>,Type-coercion<2>,Comparisons<2>,Rvalues<2> -@anchor{cp/topics/expressions function-calls}@anchor{178} +@anchor{cp/topics/expressions function-calls}@anchor{17e} @subsubsection Function calls @geindex gcc_jit_context_new_call (C++ function) -@anchor{cp/topics/expressions gcc_jit_context_new_call__gcc_jit_contextP gcc_jit_locationP gcc_jit_functionP i gcc_jit_rvaluePP}@anchor{179} +@anchor{cp/topics/expressions gcc_jit_context_new_call__gcc_jit_contextP gcc_jit_locationP gcc_jit_functionP i gcc_jit_rvaluePP}@anchor{17f} @deffn {C++ Function} gcc_jit_rvalue* gcc_jit_context_new_call (gcc_jit_context* ctxt, gcc_jit_location* loc, gcc_jit_function* func, int numargs, gcc_jit_rvalue** args) Given a function and the given table of argument rvalues, construct a @@ -12769,14 +13010,14 @@ call to the function, with the result as an rvalue. @cartouche @quotation Note @code{gccjit::context::new_call()} merely builds a -@pxref{146,,gccjit;;rvalue} i.e. an expression that can be evaluated, +@pxref{14c,,gccjit;;rvalue} i.e. an expression that can be evaluated, perhaps as part of a more complicated expression. The call @emph{won't} happen unless you add a statement to a function that evaluates the expression. For example, if you want to call a function and discard the result (or to call a function with @code{void} return type), use -@pxref{17a,,gccjit;;block;;add_eval()}: +@pxref{180,,gccjit;;block;;add_eval()}: @example /* Add "(void)printf (arg0, arg1);". */ @@ -12789,12 +13030,12 @@ block.add_eval (ctxt.new_call (printf_func, arg0, arg1)); @end deffn @node Type-coercion<2>,,Function calls<2>,Rvalues<2> -@anchor{cp/topics/expressions type-coercion}@anchor{17b} +@anchor{cp/topics/expressions type-coercion}@anchor{181} @subsubsection Type-coercion @geindex gccjit;;context;;new_cast (C++ function) -@anchor{cp/topics/expressions gccjit context new_cast__gccjit rvalue gccjit type gccjit location}@anchor{17c} +@anchor{cp/topics/expressions gccjit context new_cast__gccjit rvalue gccjit type gccjit location}@anchor{182} @deffn {C++ Function} gccjit::rvalue gccjit::context::new_cast (gccjit::rvalue rvalue, gccjit::type type, gccjit::location loc) Given an rvalue of T, construct another rvalue of another type. @@ -12819,24 +13060,24 @@ P* <-> Q*, for pointer types P and Q @end deffn @node Lvalues<2>,Working with pointers structs and unions<2>,Rvalues<2>,Expressions<2> -@anchor{cp/topics/expressions lvalues}@anchor{17d} +@anchor{cp/topics/expressions lvalues}@anchor{183} @subsubsection Lvalues @geindex gccjit;;lvalue (C++ class) -@anchor{cp/topics/expressions gccjit lvalue}@anchor{17e} +@anchor{cp/topics/expressions gccjit lvalue}@anchor{184} @deffn {C++ Class} gccjit::lvalue @end deffn An lvalue is something that can of the @emph{left}-hand side of an assignment: a storage area (such as a variable). It is a subclass of -@pxref{146,,gccjit;;rvalue}, where the rvalue is computed by reading from the +@pxref{14c,,gccjit;;rvalue}, where the rvalue is computed by reading from the storage area. It iss a thin wrapper around @pxref{24,,gcc_jit_lvalue *} from the C API. @geindex gccjit;;lvalue;;get_address (C++ function) -@anchor{cp/topics/expressions gccjit lvalue get_address__gccjit location}@anchor{17f} +@anchor{cp/topics/expressions gccjit lvalue get_address__gccjit location}@anchor{185} @deffn {C++ Function} gccjit::rvalue gccjit::lvalue::get_address (gccjit::location loc) Take the address of an lvalue; analogous to: @@ -12858,12 +13099,12 @@ Parameter "loc" is optional. @end menu @node Global variables<2>,,,Lvalues<2> -@anchor{cp/topics/expressions global-variables}@anchor{180} +@anchor{cp/topics/expressions global-variables}@anchor{186} @subsubsection Global variables @geindex gccjit;;context;;new_global (C++ function) -@anchor{cp/topics/expressions gccjit context new_global__enum gccjit type cCP gccjit location}@anchor{181} +@anchor{cp/topics/expressions gccjit context new_global__enum gccjit type cCP gccjit location}@anchor{187} @deffn {C++ Function} gccjit::lvalue gccjit::context::new_global (enum gcc_jit_global_kind, gccjit::type type, const char* name, gccjit::location loc) Add a new global variable of the given type and name to the context. @@ -12873,12 +13114,12 @@ the C API; the "kind" parameter has the same meaning as there. @end deffn @node Working with pointers structs and unions<2>,,Lvalues<2>,Expressions<2> -@anchor{cp/topics/expressions working-with-pointers-structs-and-unions}@anchor{182} +@anchor{cp/topics/expressions working-with-pointers-structs-and-unions}@anchor{188} @subsubsection Working with pointers, structs and unions @geindex gccjit;;rvalue;;dereference (C++ function) -@anchor{cp/topics/expressions gccjit rvalue dereference__gccjit location}@anchor{183} +@anchor{cp/topics/expressions gccjit rvalue dereference__gccjit location}@anchor{189} @deffn {C++ Function} gccjit::lvalue gccjit::rvalue::dereference (gccjit::location loc) Given an rvalue of pointer type @code{T *}, dereferencing the pointer, @@ -12899,7 +13140,7 @@ If you don't need to specify the location, this can also be expressed using an overloaded operator: @geindex gccjit;;rvalue;;operator* (C++ function) -@anchor{cp/topics/expressions gccjit rvalue mul-operator}@anchor{184} +@anchor{cp/topics/expressions gccjit rvalue mul-operator}@anchor{18a} @deffn {C++ Function} gccjit::lvalue gccjit::rvalue::operator* () @example @@ -12912,7 +13153,7 @@ gccjit::lvalue content = *ptr; Field access is provided separately for both lvalues and rvalues: @geindex gccjit;;lvalue;;access_field (C++ function) -@anchor{cp/topics/expressions gccjit lvalue access_field__gccjit field gccjit location}@anchor{185} +@anchor{cp/topics/expressions gccjit lvalue access_field__gccjit field gccjit location}@anchor{18b} @deffn {C++ Function} gccjit::lvalue gccjit::lvalue::access_field (gccjit::field field, gccjit::location loc) Given an lvalue of struct or union type, access the given field, @@ -12928,7 +13169,7 @@ in C. @end deffn @geindex gccjit;;rvalue;;access_field (C++ function) -@anchor{cp/topics/expressions gccjit rvalue access_field__gccjit field gccjit location}@anchor{186} +@anchor{cp/topics/expressions gccjit rvalue access_field__gccjit field gccjit location}@anchor{18c} @deffn {C++ Function} gccjit::rvalue gccjit::rvalue::access_field (gccjit::field field, gccjit::location loc) Given an rvalue of struct or union type, access the given field @@ -12944,7 +13185,7 @@ in C. @end deffn @geindex gccjit;;rvalue;;dereference_field (C++ function) -@anchor{cp/topics/expressions gccjit rvalue dereference_field__gccjit field gccjit location}@anchor{187} +@anchor{cp/topics/expressions gccjit rvalue dereference_field__gccjit field gccjit location}@anchor{18d} @deffn {C++ Function} gccjit::lvalue gccjit::rvalue::dereference_field (gccjit::field field, gccjit::location loc) Given an rvalue of pointer type @code{T *} where T is of struct or union @@ -12960,7 +13201,7 @@ in C, itself equivalent to @code{(*EXPR).FIELD}. @end deffn @geindex gccjit;;context;;new_array_access (C++ function) -@anchor{cp/topics/expressions gccjit context new_array_access__gccjit rvalue gccjit rvalue gccjit location}@anchor{188} +@anchor{cp/topics/expressions gccjit context new_array_access__gccjit rvalue gccjit rvalue gccjit location}@anchor{18e} @deffn {C++ Function} gccjit::lvalue gccjit::context::new_array_access (gccjit::rvalue ptr, gccjit::rvalue index, gccjit::location loc) Given an rvalue of pointer type @code{T *}, get at the element @cite{T} at @@ -12979,7 +13220,7 @@ in C (or, indeed, to @code{PTR + INDEX}). Parameter "loc" is optional. @end deffn -For array accesses where you don't need to specify a @pxref{124,,gccjit;;location}, +For array accesses where you don't need to specify a @pxref{12a,,gccjit;;location}, two overloaded operators are available: @quotation @@ -13019,7 +13260,7 @@ gccjit::lvalue element = array[0]; @c <http://www.gnu.org/licenses/>. @node Creating and using functions<2>,Source Locations<2>,Expressions<2>,Topic Reference<2> -@anchor{cp/topics/functions doc}@anchor{189}@anchor{cp/topics/functions creating-and-using-functions}@anchor{18a} +@anchor{cp/topics/functions doc}@anchor{18f}@anchor{cp/topics/functions creating-and-using-functions}@anchor{190} @subsection Creating and using functions @@ -13032,36 +13273,36 @@ gccjit::lvalue element = array[0]; @end menu @node Params<2>,Functions<2>,,Creating and using functions<2> -@anchor{cp/topics/functions params}@anchor{18b} +@anchor{cp/topics/functions params}@anchor{191} @subsubsection Params @geindex gccjit;;param (C++ class) -@anchor{cp/topics/functions gccjit param}@anchor{18c} +@anchor{cp/topics/functions gccjit param}@anchor{192} @deffn {C++ Class} gccjit::param A @cite{gccjit::param} represents a parameter to a function. @end deffn @geindex gccjit;;context;;new_param (C++ function) -@anchor{cp/topics/functions gccjit context new_param__gccjit type cCP gccjit location}@anchor{f3} +@anchor{cp/topics/functions gccjit context new_param__gccjit type cCP gccjit location}@anchor{f9} @deffn {C++ Function} gccjit::param gccjit::context::new_param (gccjit::type type, const char* name, gccjit::location loc) In preparation for creating a function, create a new parameter of the given type and name. @end deffn -@pxref{18c,,gccjit;;param} is a subclass of @pxref{17e,,gccjit;;lvalue} (and thus -of @pxref{146,,gccjit;;rvalue} and @pxref{130,,gccjit;;object}). It is a thin +@pxref{192,,gccjit;;param} is a subclass of @pxref{184,,gccjit;;lvalue} (and thus +of @pxref{14c,,gccjit;;rvalue} and @pxref{136,,gccjit;;object}). It is a thin wrapper around the C API's @pxref{25,,gcc_jit_param *}. @node Functions<2>,Blocks<2>,Params<2>,Creating and using functions<2> -@anchor{cp/topics/functions functions}@anchor{18d} +@anchor{cp/topics/functions functions}@anchor{193} @subsubsection Functions @geindex gccjit;;function (C++ class) -@anchor{cp/topics/functions gccjit function}@anchor{18e} +@anchor{cp/topics/functions gccjit function}@anchor{194} @deffn {C++ Class} gccjit::function A @cite{gccjit::function} represents a function - either one that we're @@ -13069,7 +13310,7 @@ creating ourselves, or one that we're referencing. @end deffn @geindex gccjit;;context;;new_function (C++ function) -@anchor{cp/topics/functions gccjit context new_function__enum gccjit type cCP std vector param R i gccjit location}@anchor{18f} +@anchor{cp/topics/functions gccjit context new_function__enum gccjit type cCP std vector param R i gccjit location}@anchor{195} @deffn {C++ Function} gccjit::function gccjit::context::new_function (enum gcc_jit_function_kind, gccjit::type return_type, const char* name, std::vector<param>& params, int is_variadic, gccjit::location loc) Create a gcc_jit_function with the given name and parameters. @@ -13080,7 +13321,7 @@ This is a wrapper around the C API's @pxref{11,,gcc_jit_context_new_function()}. @end deffn @geindex gccjit;;context;;get_builtin_function (C++ function) -@anchor{cp/topics/functions gccjit context get_builtin_function__cCP}@anchor{190} +@anchor{cp/topics/functions gccjit context get_builtin_function__cCP}@anchor{196} @deffn {C++ Function} gccjit::function gccjit::context::get_builtin_function (const char* name) This is a wrapper around the C API's @@ -13088,21 +13329,21 @@ This is a wrapper around the C API's @end deffn @geindex gccjit;;function;;get_param (C++ function) -@anchor{cp/topics/functions gccjit function get_param__iC}@anchor{191} +@anchor{cp/topics/functions gccjit function get_param__iC}@anchor{197} @deffn {C++ Function} gccjit::param gccjit::function::get_param (int index) const Get the param of the given index (0-based). @end deffn @geindex gccjit;;function;;dump_to_dot (C++ function) -@anchor{cp/topics/functions gccjit function dump_to_dot__cCP}@anchor{106} +@anchor{cp/topics/functions gccjit function dump_to_dot__cCP}@anchor{10c} @deffn {C++ Function} void gccjit::function::dump_to_dot (const char* path) Emit the function in graphviz format to the given path. @end deffn @geindex gccjit;;function;;new_local (C++ function) -@anchor{cp/topics/functions gccjit function new_local__gccjit type cCP gccjit location}@anchor{fd} +@anchor{cp/topics/functions gccjit function new_local__gccjit type cCP gccjit location}@anchor{103} @deffn {C++ Function} gccjit::lvalue gccjit::function::new_local (gccjit::type type, const char* name, gccjit::location loc) Create a new local variable within the function, of the given type and @@ -13110,32 +13351,33 @@ name. @end deffn @node Blocks<2>,Statements<2>,Functions<2>,Creating and using functions<2> -@anchor{cp/topics/functions blocks}@anchor{192} +@anchor{cp/topics/functions blocks}@anchor{198} @subsubsection Blocks @geindex gccjit;;block (C++ class) -@anchor{cp/topics/functions gccjit block}@anchor{193} +@anchor{cp/topics/functions gccjit block}@anchor{199} @deffn {C++ Class} gccjit::block A @cite{gccjit::block} represents a basic block within a function i.e. a sequence of statements with a single entry point and a single exit point. -@pxref{193,,gccjit;;block} is a subclass of @pxref{130,,gccjit;;object}. +@pxref{199,,gccjit;;block} is a subclass of @pxref{136,,gccjit;;object}. The first basic block that you create within a function will be the entrypoint. Each basic block that you create within a function must be -terminated, either with a conditional, a jump, or a return. +terminated, either with a conditional, a jump, a return, or +a switch. It's legal to have multiple basic blocks that return within one function. @end deffn @geindex gccjit;;function;;new_block (C++ function) -@anchor{cp/topics/functions gccjit function new_block__cCP}@anchor{194} +@anchor{cp/topics/functions gccjit function new_block__cCP}@anchor{19a} @deffn {C++ Function} gccjit::block gccjit::function::new_block (const char* name) Create a basic block of the given name. The name may be NULL, but @@ -13145,12 +13387,12 @@ messages. @end deffn @node Statements<2>,,Blocks<2>,Creating and using functions<2> -@anchor{cp/topics/functions statements}@anchor{195} +@anchor{cp/topics/functions statements}@anchor{19b} @subsubsection Statements @geindex gccjit;;block;;add_eval (C++ function) -@anchor{cp/topics/functions gccjit block add_eval__gccjit rvalue gccjit location}@anchor{17a} +@anchor{cp/topics/functions gccjit block add_eval__gccjit rvalue gccjit location}@anchor{180} @deffn {C++ Function} void gccjit::block::add_eval (gccjit::rvalue rvalue, gccjit::location loc) Add evaluation of an rvalue, discarding the result @@ -13166,7 +13408,7 @@ This is equivalent to this C code: @end deffn @geindex gccjit;;block;;add_assignment (C++ function) -@anchor{cp/topics/functions gccjit block add_assignment__gccjit lvalue gccjit rvalue gccjit location}@anchor{ff} +@anchor{cp/topics/functions gccjit block add_assignment__gccjit lvalue gccjit rvalue gccjit location}@anchor{105} @deffn {C++ Function} void gccjit::block::add_assignment (gccjit::lvalue lvalue, gccjit::rvalue rvalue, gccjit::location loc) Add evaluation of an rvalue, assigning the result to the given @@ -13182,7 +13424,7 @@ lvalue = rvalue; @end deffn @geindex gccjit;;block;;add_assignment_op (C++ function) -@anchor{cp/topics/functions gccjit block add_assignment_op__gccjit lvalue enum gccjit rvalue gccjit location}@anchor{103} +@anchor{cp/topics/functions gccjit block add_assignment_op__gccjit lvalue enum gccjit rvalue gccjit location}@anchor{109} @deffn {C++ Function} void gccjit::block::add_assignment_op (gccjit::lvalue lvalue, enum gcc_jit_binary_op, gccjit::rvalue rvalue, gccjit::location loc) Add evaluation of an rvalue, using the result to modify an @@ -13212,7 +13454,7 @@ loop_body.add_assignment_op ( @end deffn @geindex gccjit;;block;;add_comment (C++ function) -@anchor{cp/topics/functions gccjit block add_comment__cCP gccjit location}@anchor{10e} +@anchor{cp/topics/functions gccjit block add_comment__cCP gccjit location}@anchor{114} @deffn {C++ Function} void gccjit::block::add_comment (const char* text, gccjit::location loc) Add a no-op textual comment to the internal representation of the @@ -13226,7 +13468,7 @@ Parameter "loc" is optional. @end deffn @geindex gccjit;;block;;end_with_conditional (C++ function) -@anchor{cp/topics/functions gccjit block end_with_conditional__gccjit rvalue gccjit block gccjit block gccjit location}@anchor{102} +@anchor{cp/topics/functions gccjit block end_with_conditional__gccjit rvalue gccjit block gccjit block gccjit location}@anchor{108} @deffn {C++ Function} void gccjit::block::end_with_conditional (gccjit::rvalue boolval, gccjit::block on_true, gccjit::block on_false, gccjit::location loc) Terminate a block by adding evaluation of an rvalue, branching on the @@ -13247,7 +13489,7 @@ block, boolval, on_true, and on_false must be non-NULL. @end deffn @geindex gccjit;;block;;end_with_jump (C++ function) -@anchor{cp/topics/functions gccjit block end_with_jump__gccjit block gccjit location}@anchor{196} +@anchor{cp/topics/functions gccjit block end_with_jump__gccjit block gccjit location}@anchor{19c} @deffn {C++ Function} void gccjit::block::end_with_jump (gccjit::block target, gccjit::location loc) Terminate a block by adding a jump to the given target block. @@ -13262,7 +13504,7 @@ goto target; @end deffn @geindex gccjit;;block;;end_with_return (C++ function) -@anchor{cp/topics/functions gccjit block end_with_return__gccjit rvalue gccjit location}@anchor{197} +@anchor{cp/topics/functions gccjit block end_with_return__gccjit rvalue gccjit location}@anchor{19d} @deffn {C++ Function} void gccjit::block::end_with_return (gccjit::rvalue rvalue, gccjit::location loc) Terminate a block. @@ -13295,6 +13537,174 @@ return; @noindent @end deffn +@geindex gccjit;;block;;end_with_switch (C++ function) +@anchor{cp/topics/functions gccjit block end_with_switch__gccjit rvalue gccjit block std vector gccjit case_ gccjit location}@anchor{19e} +@deffn {C++ Function} void gccjit::block::end_with_switch (gccjit::rvalue expr, gccjit::block default_block, std::vector<gccjit::case_> cases, gccjit::location loc) + +Terminate a block by adding evalation of an rvalue, then performing +a multiway branch. + +This is roughly equivalent to this C code: + +@example +switch (expr) + @{ + default: + goto default_block; + + case C0.min_value ... C0.max_value: + goto C0.dest_block; + + case C1.min_value ... C1.max_value: + goto C1.dest_block; + + ...etc... + + case C[N - 1].min_value ... C[N - 1].max_value: + goto C[N - 1].dest_block; +@} +@end example + +@noindent + +@code{expr} must be of the same integer type as all of the @code{min_value} +and @code{max_value} within the cases. + +The ranges of the cases must not overlap (or have duplicate +values). + +The API entrypoints relating to switch statements and cases: + +@quotation + + +@itemize * + +@item +@pxref{19e,,gccjit;;block;;end_with_switch()} + +@item +@pxref{19f,,gccjit;;context;;new_case()} +@end itemize +@end quotation + +were added in @pxref{d7,,LIBGCCJIT_ABI_3}; you can test for their presence +using + +@example +#ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS +@end example + +@noindent + +@geindex gccjit;;block;;end_with_switch;;gccjit;;case_ (C++ class) +@anchor{cp/topics/functions gccjit block end_with_switch gccjit case_}@anchor{1a0} +@deffn {C++ Class} gccjit::case_ +@end deffn + +A @cite{gccjit::case_} represents a case within a switch statement, and +is created within a particular @pxref{122,,gccjit;;context} using +@pxref{19f,,gccjit;;context;;new_case()}. It is a subclass of +@pxref{136,,gccjit;;object}. + +Each case expresses a multivalued range of integer values. You +can express single-valued cases by passing in the same value for +both @cite{min_value} and @cite{max_value}. + +@geindex gccjit;;block;;end_with_switch;;gccjit;;context;;new_case (C++ function) +@anchor{cp/topics/functions gccjit block end_with_switch gccjit context new_case__gccjit rvalue gccjit rvalue gccjit block}@anchor{19f} +@deffn {C++ Function} gccjit::case_* gccjit::context::new_case (gccjit::rvalue min_value, gccjit::rvalue max_value, gccjit::block dest_block) + +Create a new gccjit::case for use in a switch statement. +@cite{min_value} and @cite{max_value} must be constants of an integer type, +which must match that of the expression of the switch statement. + +@cite{dest_block} must be within the same function as the switch +statement. +@end deffn + +Here's an example of creating a switch statement: + +@quotation + +@example + +void +create_code (gcc_jit_context *c_ctxt, void *user_data) +@{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + @{ + switch (x) + @{ + case 0 ... 5: + return 3; + + case 25 ... 27: + return 4; + + case -42 ... -17: + return 83; + + case 40: + return 8; + + default: + return 10; + @} + @} + */ + gccjit::context ctxt (c_ctxt); + gccjit::type t_int = ctxt.get_type (GCC_JIT_TYPE_INT); + gccjit::type return_type = t_int; + gccjit::param x = ctxt.new_param (t_int, "x"); + std::vector <gccjit::param> params; + params.push_back (x); + gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + params, 0); + + gccjit::block b_initial = func.new_block ("initial"); + + gccjit::block b_default = func.new_block ("default"); + gccjit::block b_case_0_5 = func.new_block ("case_0_5"); + gccjit::block b_case_25_27 = func.new_block ("case_25_27"); + gccjit::block b_case_m42_m17 = func.new_block ("case_m42_m17"); + gccjit::block b_case_40 = func.new_block ("case_40"); + + std::vector <gccjit::case_> cases; + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 0), + ctxt.new_rvalue (t_int, 5), + b_case_0_5)); + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 25), + ctxt.new_rvalue (t_int, 27), + b_case_25_27)); + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, -42), + ctxt.new_rvalue (t_int, -17), + b_case_m42_m17)); + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 40), + ctxt.new_rvalue (t_int, 40), + b_case_40)); + b_initial.end_with_switch (x, + b_default, + cases); + + b_case_0_5.end_with_return (ctxt.new_rvalue (t_int, 3)); + b_case_25_27.end_with_return (ctxt.new_rvalue (t_int, 4)); + b_case_m42_m17.end_with_return (ctxt.new_rvalue (t_int, 83)); + b_case_40.end_with_return (ctxt.new_rvalue (t_int, 8)); + b_default.end_with_return (ctxt.new_rvalue (t_int, 10)); +@} + + +@end example + +@noindent +@end quotation +@end deffn + @c Copyright (C) 2014-2015 Free Software Foundation, Inc. @c Originally contributed by David Malcolm <dmalcolm@redhat.com> @c @@ -13313,12 +13723,12 @@ return; @c <http://www.gnu.org/licenses/>. @node Source Locations<2>,Compiling a context<2>,Creating and using functions<2>,Topic Reference<2> -@anchor{cp/topics/locations source-locations}@anchor{198}@anchor{cp/topics/locations doc}@anchor{199} +@anchor{cp/topics/locations source-locations}@anchor{1a1}@anchor{cp/topics/locations doc}@anchor{1a2} @subsection Source Locations @geindex gccjit;;location (C++ class) -@anchor{cp/topics/locations gccjit location}@anchor{124} +@anchor{cp/topics/locations gccjit location}@anchor{12a} @deffn {C++ Class} gccjit::location A @cite{gccjit::location} encapsulates a source code location, so that @@ -13329,10 +13739,10 @@ single-step through your language. @cite{gccjit::location} instances are optional: you can always omit them from any C++ API entrypoint accepting one. -You can construct them using @pxref{112,,gccjit;;context;;new_location()}. +You can construct them using @pxref{118,,gccjit;;context;;new_location()}. You need to enable @pxref{42,,GCC_JIT_BOOL_OPTION_DEBUGINFO} on the -@pxref{11c,,gccjit;;context} for these locations to actually be usable by +@pxref{122,,gccjit;;context} for these locations to actually be usable by the debugger: @example @@ -13343,7 +13753,7 @@ ctxt.set_bool_option (GCC_JIT_BOOL_OPTION_DEBUGINFO, 1); @end deffn @geindex gccjit;;context;;new_location (C++ function) -@anchor{cp/topics/locations gccjit context new_location__cCP i i}@anchor{112} +@anchor{cp/topics/locations gccjit context new_location__cCP i i}@anchor{118} @deffn {C++ Function} gccjit::location gccjit::context::new_location (const char* filename, int line, int column) Create a @cite{gccjit::location} instance representing the given source @@ -13356,13 +13766,13 @@ location. @end menu @node Faking it<2>,,,Source Locations<2> -@anchor{cp/topics/locations faking-it}@anchor{19a} +@anchor{cp/topics/locations faking-it}@anchor{1a3} @subsubsection Faking it If you don't have source code for your internal representation, but need to debug, you can generate a C-like representation of the functions in -your context using @pxref{123,,gccjit;;context;;dump_to_file()}: +your context using @pxref{129,,gccjit;;context;;dump_to_file()}: @example ctxt.dump_to_file ("/tmp/something.c", @@ -13394,13 +13804,13 @@ file, giving you @emph{something} you can step through in the debugger. @c <http://www.gnu.org/licenses/>. @node Compiling a context<2>,,Source Locations<2>,Topic Reference<2> -@anchor{cp/topics/compilation compiling-a-context}@anchor{19b}@anchor{cp/topics/compilation doc}@anchor{19c} +@anchor{cp/topics/compilation compiling-a-context}@anchor{1a4}@anchor{cp/topics/compilation doc}@anchor{1a5} @subsection Compiling a context -Once populated, a @pxref{11c,,gccjit;;context} can be compiled to -machine code, either in-memory via @pxref{f5,,gccjit;;context;;compile()} or -to disk via @pxref{19d,,gccjit;;context;;compile_to_file()}. +Once populated, a @pxref{122,,gccjit;;context} can be compiled to +machine code, either in-memory via @pxref{fb,,gccjit;;context;;compile()} or +to disk via @pxref{1a6,,gccjit;;context;;compile_to_file()}. You can compile a context multiple times (using either form of compilation), although any errors that occur on the context will @@ -13413,12 +13823,12 @@ prevent any future compilation of that context. @end menu @node In-memory compilation<2>,Ahead-of-time compilation<2>,,Compiling a context<2> -@anchor{cp/topics/compilation in-memory-compilation}@anchor{19e} +@anchor{cp/topics/compilation in-memory-compilation}@anchor{1a7} @subsubsection In-memory compilation @geindex gccjit;;context;;compile (C++ function) -@anchor{cp/topics/compilation gccjit context compile}@anchor{f5} +@anchor{cp/topics/compilation gccjit context compile}@anchor{fb} @deffn {C++ Function} gcc_jit_result* gccjit::context::compile () This calls into GCC and builds the code, returning a @@ -13429,19 +13839,19 @@ This is a thin wrapper around the @end deffn @node Ahead-of-time compilation<2>,,In-memory compilation<2>,Compiling a context<2> -@anchor{cp/topics/compilation ahead-of-time-compilation}@anchor{19f} +@anchor{cp/topics/compilation ahead-of-time-compilation}@anchor{1a8} @subsubsection Ahead-of-time compilation Although libgccjit is primarily aimed at just-in-time compilation, it can also be used for implementing more traditional ahead-of-time -compilers, via the @pxref{19d,,gccjit;;context;;compile_to_file()} method. +compilers, via the @pxref{1a6,,gccjit;;context;;compile_to_file()} method. @geindex gccjit;;context;;compile_to_file (C++ function) -@anchor{cp/topics/compilation gccjit context compile_to_file__enum cCP}@anchor{19d} +@anchor{cp/topics/compilation gccjit context compile_to_file__enum cCP}@anchor{1a6} @deffn {C++ Function} void gccjit::context::compile_to_file (enum gcc_jit_output_kind, const char* output_path) -Compile the @pxref{11c,,gccjit;;context} to a file of the given +Compile the @pxref{122,,gccjit;;context} to a file of the given kind. This is a thin wrapper around the @@ -13466,7 +13876,7 @@ This is a thin wrapper around the @c <http://www.gnu.org/licenses/>. @node Internals,Indices and tables,C++ bindings for libgccjit,Top -@anchor{internals/index internals}@anchor{1a0}@anchor{internals/index doc}@anchor{1a1} +@anchor{internals/index internals}@anchor{1a9}@anchor{internals/index doc}@anchor{1aa} @chapter Internals @@ -13481,7 +13891,7 @@ This is a thin wrapper around the @end menu @node Working on the JIT library,Running the test suite,,Internals -@anchor{internals/index working-on-the-jit-library}@anchor{1a2} +@anchor{internals/index working-on-the-jit-library}@anchor{1ab} @section Working on the JIT library @@ -13518,7 +13928,7 @@ gcc/libgccjit.so.0.0.1: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), Here's what those configuration options mean: @geindex command line option; --enable-host-shared -@anchor{internals/index cmdoption--enable-host-shared}@anchor{1a3} +@anchor{internals/index cmdoption--enable-host-shared}@anchor{1ac} @deffn {Option} --enable-host-shared Configuring with this option means that the compiler is built as @@ -13527,7 +13937,7 @@ but it necessary for a shared library. @end deffn @geindex command line option; --enable-languages=jit@comma{}c++ -@anchor{internals/index cmdoption--enable-languages}@anchor{1a4} +@anchor{internals/index cmdoption--enable-languages}@anchor{1ad} @deffn {Option} --enable-languages=jit,c++ This specifies which frontends to build. The JIT library looks like @@ -13546,7 +13956,7 @@ c++: error trying to exec 'cc1plus': execvp: No such file or directory @end deffn @geindex command line option; --disable-bootstrap -@anchor{internals/index cmdoption--disable-bootstrap}@anchor{1a5} +@anchor{internals/index cmdoption--disable-bootstrap}@anchor{1ae} @deffn {Option} --disable-bootstrap For hacking on the "jit" subdirectory, performing a full @@ -13556,7 +13966,7 @@ the compiler can still bootstrap itself. @end deffn @geindex command line option; --enable-checking=release -@anchor{internals/index cmdoption--enable-checking}@anchor{1a6} +@anchor{internals/index cmdoption--enable-checking}@anchor{1af} @deffn {Option} --enable-checking=release The compile can perform extensive self-checking as it runs, useful when @@ -13567,7 +13977,7 @@ disable this self-checking. @end deffn @node Running the test suite,Environment variables,Working on the JIT library,Internals -@anchor{internals/index running-the-test-suite}@anchor{1a7} +@anchor{internals/index running-the-test-suite}@anchor{1b0} @section Running the test suite @@ -13630,7 +14040,7 @@ and once a test has been compiled, you can debug it directly: @end menu @node Running under valgrind,,,Running the test suite -@anchor{internals/index running-under-valgrind}@anchor{1a8} +@anchor{internals/index running-under-valgrind}@anchor{1b1} @subsection Running under valgrind @@ -13678,7 +14088,7 @@ When running under valgrind, it's best to have configured gcc with various known false positives. @node Environment variables,Packaging notes,Running the test suite,Internals -@anchor{internals/index environment-variables}@anchor{1a9} +@anchor{internals/index environment-variables}@anchor{1b2} @section Environment variables @@ -13686,7 +14096,7 @@ When running client code against a locally-built libgccjit, three environment variables need to be set up: @geindex environment variable; LD_LIBRARY_PATH -@anchor{internals/index envvar-LD_LIBRARY_PATH}@anchor{1aa} +@anchor{internals/index envvar-LD_LIBRARY_PATH}@anchor{1b3} @deffn {Environment Variable} LD_LIBRARY_PATH @quotation @@ -13708,7 +14118,7 @@ libgccjit.so.0.0.1: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), @end deffn @geindex environment variable; PATH -@anchor{internals/index envvar-PATH}@anchor{1ab} +@anchor{internals/index envvar-PATH}@anchor{1b4} @deffn {Environment Variable} PATH The library uses a driver executable for converting from .s assembler @@ -13727,7 +14137,7 @@ of development. @end deffn @geindex environment variable; LIBRARY_PATH -@anchor{internals/index envvar-LIBRARY_PATH}@anchor{1ac} +@anchor{internals/index envvar-LIBRARY_PATH}@anchor{1b5} @deffn {Environment Variable} LIBRARY_PATH The driver executable invokes the linker, and the latter needs to locate @@ -13763,11 +14173,11 @@ hello world @noindent @node Packaging notes,Overview of code structure,Environment variables,Internals -@anchor{internals/index packaging-notes}@anchor{1ad} +@anchor{internals/index packaging-notes}@anchor{1b6} @section Packaging notes -The configure-time option @pxref{1a3,,--enable-host-shared} is needed when +The configure-time option @pxref{1ac,,--enable-host-shared} is needed when building the jit in order to get position-independent code. This will slow down the regular compiler by a few percent. Hence when packaging gcc with libgccjit, please configure and build twice: @@ -13778,10 +14188,10 @@ with libgccjit, please configure and build twice: @itemize * @item -once without @pxref{1a3,,--enable-host-shared} for most languages, and +once without @pxref{1ac,,--enable-host-shared} for most languages, and @item -once with @pxref{1a3,,--enable-host-shared} for the jit +once with @pxref{1ac,,--enable-host-shared} for the jit @end itemize @end quotation @@ -13825,7 +14235,7 @@ popd @noindent @node Overview of code structure,Design notes,Packaging notes,Internals -@anchor{internals/index overview-of-code-structure}@anchor{1ae} +@anchor{internals/index overview-of-code-structure}@anchor{1b7} @section Overview of code structure @@ -13865,6 +14275,7 @@ The gcc::jit::recording classes (within @code{jit-recording.c} and class global; class param; class statement; + class case_; @end example @@ -13895,6 +14306,7 @@ within langhook:parse_file: class source_file; class source_line; class location; + class case_; @end example @@ -14289,7 +14701,7 @@ JIT: gcc::jit::logger::~logger() @noindent @node Design notes,,Overview of code structure,Internals -@anchor{internals/index design-notes}@anchor{1af} +@anchor{internals/index design-notes}@anchor{1b8} @section Design notes @@ -14302,7 +14714,7 @@ close as possible to the error; failing that, a good place is within @code{recording::context::validate ()} in jit-recording.c. @node Indices and tables,Index,Internals,Top -@anchor{index indices-and-tables}@anchor{1b0} +@anchor{index indices-and-tables}@anchor{1b9} @unnumbered Indices and tables diff --git a/gcc/jit/docs/cp/topics/functions.rst b/gcc/jit/docs/cp/topics/functions.rst index de3570a..57b6298 100644 --- a/gcc/jit/docs/cp/topics/functions.rst +++ b/gcc/jit/docs/cp/topics/functions.rst @@ -98,7 +98,8 @@ Blocks be the entrypoint. Each basic block that you create within a function must be - terminated, either with a conditional, a jump, or a return. + terminated, either with a conditional, a jump, a return, or + a switch. It's legal to have multiple basic blocks that return within one function. @@ -241,3 +242,82 @@ Statements .. code-block:: c return; + +.. function:: void\ + gccjit::block::end_with_switch (gccjit::rvalue expr,\ + gccjit::block default_block,\ + std::vector <gccjit::case_> cases,\ + gccjit::location loc) + + Terminate a block by adding evalation of an rvalue, then performing + a multiway branch. + + This is roughly equivalent to this C code: + + .. code-block:: c + + switch (expr) + { + default: + goto default_block; + + case C0.min_value ... C0.max_value: + goto C0.dest_block; + + case C1.min_value ... C1.max_value: + goto C1.dest_block; + + ...etc... + + case C[N - 1].min_value ... C[N - 1].max_value: + goto C[N - 1].dest_block; + } + + ``expr`` must be of the same integer type as all of the ``min_value`` + and ``max_value`` within the cases. + + The ranges of the cases must not overlap (or have duplicate + values). + + The API entrypoints relating to switch statements and cases: + + * :func:`gccjit::block::end_with_switch` + + * :func:`gccjit::context::new_case` + + were added in :ref:`LIBGCCJIT_ABI_3`; you can test for their presence + using + + .. code-block:: c + + #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS + + .. class:: gccjit::case_ + + A `gccjit::case_` represents a case within a switch statement, and + is created within a particular :class:`gccjit::context` using + :func:`gccjit::context::new_case`. It is a subclass of + :class:`gccjit::object`. + + Each case expresses a multivalued range of integer values. You + can express single-valued cases by passing in the same value for + both `min_value` and `max_value`. + + .. function:: gccjit::case_ *\ + gccjit::context::new_case (gccjit::rvalue min_value,\ + gccjit::rvalue max_value,\ + gccjit::block dest_block) + + Create a new gccjit::case for use in a switch statement. + `min_value` and `max_value` must be constants of an integer type, + which must match that of the expression of the switch statement. + + `dest_block` must be within the same function as the switch + statement. + + Here's an example of creating a switch statement: + + .. literalinclude:: ../../../../testsuite/jit.dg/test-switch.cc + :start-after: /* Quote from here in docs/cp/topics/functions.rst. */ + :end-before: /* Quote up to here in docs/cp/topics/functions.rst. */ + :language: c++ diff --git a/gcc/jit/docs/cp/topics/objects.rst b/gcc/jit/docs/cp/topics/objects.rst index 714b645..8d99bd4 100644 --- a/gcc/jit/docs/cp/topics/objects.rst +++ b/gcc/jit/docs/cp/topics/objects.rst @@ -46,6 +46,7 @@ The C++ class hierarchy within the ``gccjit`` namespace looks like this:: +- rvalue +- lvalue +- param + +- case_ The :class:`gccjit::object` base class has the following operations: diff --git a/gcc/jit/docs/topics/compatibility.rst b/gcc/jit/docs/topics/compatibility.rst index 91bbb05..37e2866 100644 --- a/gcc/jit/docs/topics/compatibility.rst +++ b/gcc/jit/docs/topics/compatibility.rst @@ -47,7 +47,6 @@ from metadata by using ``objdump``: 0x00824161 0x00 04 LIBGCCJIT_ABI_1 0x00824160 0x00 03 LIBGCCJIT_ABI_0 required from libc.so.6: - 0x09691a75 0x00 02 GLIBC_2.2.5 You can see the symbol tags provided by libgccjit.so using ``objdump``: @@ -95,3 +94,16 @@ continue to work, with this being handled transparently by the linker ------------------- ``LIBGCCJIT_ABI_2`` covers the addition of :func:`gcc_jit_context_set_bool_allow_unreachable_blocks` + +.. _LIBGCCJIT_ABI_3: + +``LIBGCCJIT_ABI_3`` +------------------- +``LIBGCCJIT_ABI_3`` covers the addition of switch statements via API +entrypoints: + + * :func:`gcc_jit_block_end_with_switch` + + * :func:`gcc_jit_case_as_object` + + * :func:`gcc_jit_context_new_case` diff --git a/gcc/jit/docs/topics/functions.rst b/gcc/jit/docs/topics/functions.rst index 774a556..94db471 100644 --- a/gcc/jit/docs/topics/functions.rst +++ b/gcc/jit/docs/topics/functions.rst @@ -153,7 +153,8 @@ Blocks be the entrypoint. Each basic block that you create within a function must be - terminated, either with a conditional, a jump, or a return. + terminated, either with a conditional, a jump, a return, or a + switch. It's legal to have multiple basic blocks that return within one function. @@ -313,3 +314,96 @@ Statements .. code-block:: c return; + +.. function:: void\ + gcc_jit_block_end_with_switch (gcc_jit_block *block,\ + gcc_jit_location *loc,\ + gcc_jit_rvalue *expr,\ + gcc_jit_block *default_block,\ + int num_cases,\ + gcc_jit_case **cases) + + Terminate a block by adding evalation of an rvalue, then performing + a multiway branch. + + This is roughly equivalent to this C code: + + .. code-block:: c + + switch (expr) + { + default: + goto default_block; + + case C0.min_value ... C0.max_value: + goto C0.dest_block; + + case C1.min_value ... C1.max_value: + goto C1.dest_block; + + ...etc... + + case C[N - 1].min_value ... C[N - 1].max_value: + goto C[N - 1].dest_block; + } + + ``block``, ``expr``, ``default_block`` and ``cases`` must all be + non-NULL. + + ``expr`` must be of the same integer type as all of the ``min_value`` + and ``max_value`` within the cases. + + ``num_cases`` must be >= 0. + + The ranges of the cases must not overlap (or have duplicate + values). + + The API entrypoints relating to switch statements and cases: + + * :c:func:`gcc_jit_block_end_with_switch` + + * :c:func:`gcc_jit_case_as_object` + + * :c:func:`gcc_jit_context_new_case` + + were added in :ref:`LIBGCCJIT_ABI_3`; you can test for their presence + using + + .. code-block:: c + + #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS + + .. type:: gcc_jit_case + + A `gcc_jit_case` represents a case within a switch statement, and + is created within a particular :c:type:`gcc_jit_context` using + :c:func:`gcc_jit_context_new_case`. + + Each case expresses a multivalued range of integer values. You + can express single-valued cases by passing in the same value for + both `min_value` and `max_value`. + + .. function:: gcc_jit_case *\ + gcc_jit_context_new_case (gcc_jit_context *ctxt,\ + gcc_jit_rvalue *min_value,\ + gcc_jit_rvalue *max_value,\ + gcc_jit_block *dest_block) + + Create a new gcc_jit_case instance for use in a switch statement. + `min_value` and `max_value` must be constants of an integer type, + which must match that of the expression of the switch statement. + + `dest_block` must be within the same function as the switch + statement. + + .. function:: gcc_jit_object *\ + gcc_jit_case_as_object (gcc_jit_case *case_) + + Upcast from a case to an object. + + Here's an example of creating a switch statement: + + .. literalinclude:: ../../../testsuite/jit.dg/test-switch.c + :start-after: /* Quote from here in docs/topics/functions.rst. */ + :end-before: /* Quote up to here in docs/topics/functions.rst. */ + :language: c diff --git a/gcc/jit/docs/topics/objects.rst b/gcc/jit/docs/topics/objects.rst index 3ae6309..85c783c 100644 --- a/gcc/jit/docs/topics/objects.rst +++ b/gcc/jit/docs/topics/objects.rst @@ -47,6 +47,7 @@ looks like this:: +- gcc_jit_rvalue +- gcc_jit_lvalue +- gcc_jit_param + +- gcc_jit_case There are casting methods for upcasting from subclasses to parent classes. For example, :c:func:`gcc_jit_type_as_object`: diff --git a/gcc/jit/jit-common.h b/gcc/jit/jit-common.h index edf8d2c..3397215 100644 --- a/gcc/jit/jit-common.h +++ b/gcc/jit/jit-common.h @@ -129,6 +129,7 @@ namespace recording { class global; class param; class statement; + class case_; /* End of recording types. */ } @@ -150,6 +151,7 @@ namespace playback { class source_file; class source_line; class location; + class case_; /* End of playback types. */ } diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c index 9d7f8d6..c9d7c8c 100644 --- a/gcc/jit/jit-playback.c +++ b/gcc/jit/jit-playback.c @@ -1576,6 +1576,80 @@ add_return (location *loc, add_stmt (return_stmt); } +/* Helper function for playback::block::add_switch. + Construct a case label for the given range, followed by a goto stmt + to the given block, appending them to stmt list *ptr_t_switch_body. */ + +static void +add_case (tree *ptr_t_switch_body, + tree t_low_value, + tree t_high_value, + playback::block *dest_block) +{ + tree t_label = create_artificial_label (UNKNOWN_LOCATION); + DECL_CONTEXT (t_label) = dest_block->get_function ()->as_fndecl (); + + tree t_case_label = + build_case_label (t_low_value, t_high_value, t_label); + append_to_statement_list (t_case_label, ptr_t_switch_body); + + tree t_goto_stmt = + build1 (GOTO_EXPR, void_type_node, dest_block->as_label_decl ()); + append_to_statement_list (t_goto_stmt, ptr_t_switch_body); +} + +/* Add a switch statement to the function's statement list. + + My initial attempt at implementing this constructed a TREE_VEC + of the cases and set it as SWITCH_LABELS (switch_expr). However, + gimplify.c:gimplify_switch_expr is set up to deal with SWITCH_BODY, and + doesn't have any logic for gimplifying SWITCH_LABELS. + + Hence we create a switch body, and populate it with case labels, each + followed by a goto to the desired block. */ + +void +playback::block:: +add_switch (location *loc, + rvalue *expr, + block *default_block, + const auto_vec <case_> *cases) +{ + /* Compare with: + - c/c-typeck.c: c_start_case + - c-family/c-common.c:c_add_case_label + - java/expr.c:expand_java_switch and expand_java_add_case + We've already rejected overlaps and duplicates in + libgccjit.c:case_range_validator::validate. */ + + tree t_expr = expr->as_tree (); + tree t_type = TREE_TYPE (t_expr); + + tree t_switch_body = alloc_stmt_list (); + + int i; + case_ *c; + FOR_EACH_VEC_ELT (*cases, i, c) + { + tree t_low_value = c->m_min_value->as_tree (); + tree t_high_value = c->m_max_value->as_tree (); + add_case (&t_switch_body, + t_low_value, + t_high_value, + c->m_dest_block); + } + /* Default label. */ + add_case (&t_switch_body, + NULL_TREE, NULL_TREE, + default_block); + + tree switch_stmt = build3 (SWITCH_EXPR, t_type, t_expr, + t_switch_body, NULL_TREE); + if (loc) + set_tree_location (switch_stmt, loc); + add_stmt (switch_stmt); +} + /* Constructor for gcc::jit::playback::block. */ playback::block:: diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h index a6de566..13cc748 100644 --- a/gcc/jit/jit-playback.h +++ b/gcc/jit/jit-playback.h @@ -437,6 +437,19 @@ private: vec<block *> m_blocks; }; +struct case_ +{ + case_ (rvalue *min_value, rvalue *max_value, block *dest_block) + : m_min_value (min_value), + m_max_value (max_value), + m_dest_block (dest_block) + {} + + rvalue *m_min_value; + rvalue *m_max_value; + block *m_dest_block; +}; + class block : public wrapper { public: @@ -447,6 +460,8 @@ public: tree as_label_decl () const { return m_label_decl; } + function *get_function () const { return m_func; } + void add_eval (location *loc, rvalue *rvalue); @@ -478,6 +493,12 @@ public: add_return (location *loc, rvalue *rvalue); + void + add_switch (location *loc, + rvalue *expr, + block *default_block, + const auto_vec <case_> *cases); + private: void set_tree_location (tree t, location *loc) diff --git a/gcc/jit/jit-recording.c b/gcc/jit/jit-recording.c index ad13aaa..a653205 100644 --- a/gcc/jit/jit-recording.c +++ b/gcc/jit/jit-recording.c @@ -1084,6 +1084,22 @@ recording::context::new_array_access (recording::location *loc, return result; } +/* Create a recording::case_ instance and add it to this context's list + of mementos. + + Implements the post-error-checking part of + gcc_jit_context_new_case. */ + +recording::case_ * +recording::context::new_case (recording::rvalue *min_value, + recording::rvalue *max_value, + recording::block *block) +{ + recording::case_ *result = new case_ (this, min_value, max_value, block); + record (result); + return result; +} + /* Set the given string option for this context, or add an error if it's not recognized. @@ -3505,23 +3521,13 @@ recording::function::validate () /* Add successor blocks that aren't yet marked to the worklist. */ /* We checked that each block has a terminating statement above . */ - block *next1, *next2; - int n = b->get_successor_blocks (&next1, &next2); - switch (n) - { - default: - gcc_unreachable (); - case 2: - if (!next2->m_is_reachable) - worklist.safe_push (next2); - /* fallthrough */ - case 1: - if (!next1->m_is_reachable) - worklist.safe_push (next1); - break; - case 0: - break; - } + vec <block *> successors = b->get_successor_blocks (); + int i; + block *succ; + FOR_EACH_VEC_ELT (successors, i, succ) + if (!succ->m_is_reachable) + worklist.safe_push (succ); + successors.release (); } /* Now complain about any blocks that haven't been marked. */ @@ -3769,6 +3775,30 @@ recording::block::end_with_return (recording::location *loc, return result; } +/* Create a recording::switch_ instance and add it to + the block's context's list of mementos, and to the block's + list of statements. + + Implements the heart of gcc_jit_block_end_with_switch. */ + +recording::statement * +recording::block::end_with_switch (recording::location *loc, + recording::rvalue *expr, + recording::block *default_block, + int num_cases, + recording::case_ **cases) +{ + statement *result = new switch_ (this, loc, + expr, + default_block, + num_cases, + cases); + m_ctxt->record (result); + m_statements.safe_push (result); + m_has_been_terminated = true; + return result; +} + /* Override the default implementation of recording::memento::write_to_dump for blocks by writing an unindented block name as a label, followed by the indented @@ -3846,24 +3876,20 @@ recording::block::get_last_statement () const return NULL; } -/* Assuming that this block has been terminated, get the number of - successor blocks, which will be 0, 1 or 2, for return, unconditional - jump, and conditional jump respectively. - NEXT1 and NEXT2 must be non-NULL. The first successor block (if any) - is written to NEXT1, and the second (if any) to NEXT2. +/* Assuming that this block has been terminated, get the successor blocks + as a vector. Ownership of the vector transfers to the caller, which + must call its release () method. Used when validating functions, and when dumping dot representations of them. */ -int -recording::block::get_successor_blocks (block **next1, block **next2) const +vec <recording::block *> +recording::block::get_successor_blocks () const { gcc_assert (m_has_been_terminated); - gcc_assert (next1); - gcc_assert (next2); statement *last_statement = get_last_statement (); gcc_assert (last_statement); - return last_statement->get_successor_blocks (next1, next2); + return last_statement->get_successor_blocks (); } /* Implementation of pure virtual hook recording::memento::replay_into @@ -3941,12 +3967,14 @@ recording::block::dump_to_dot (pretty_printer *pp) void recording::block::dump_edges_to_dot (pretty_printer *pp) { - block *next[2]; - int num_succs = get_successor_blocks (&next[0], &next[1]); - for (int i = 0; i < num_succs; i++) + vec <block *> successors = get_successor_blocks (); + int i; + block *succ; + FOR_EACH_VEC_ELT (successors, i, succ) pp_printf (pp, "\tblock_%d:s -> block_%d:n;\n", - m_index, next[i]->m_index); + m_index, succ->m_index); + successors.release (); } /* The implementation of class gcc::jit::recording::global. */ @@ -4091,6 +4119,16 @@ memento_of_new_rvalue_from_const <int>::make_debug_string () m_value); } +/* The get_wide_int specialization for <int>. */ + +template <> +bool +memento_of_new_rvalue_from_const <int>::get_wide_int (wide_int *out) const +{ + *out = wi::shwi (m_value, sizeof (m_value) * 8); + return true; +} + /* The write_reproducer specialization for <int>. */ template <> @@ -4123,6 +4161,16 @@ memento_of_new_rvalue_from_const <long>::make_debug_string () m_value); } +/* The get_wide_int specialization for <long>. */ + +template <> +bool +memento_of_new_rvalue_from_const <long>::get_wide_int (wide_int *out) const +{ + *out = wi::shwi (m_value, sizeof (m_value) * 8); + return true; +} + /* The write_reproducer specialization for <long>. */ template <> @@ -4176,6 +4224,15 @@ memento_of_new_rvalue_from_const <double>::make_debug_string () m_value); } +/* The get_wide_int specialization for <double>. */ + +template <> +bool +memento_of_new_rvalue_from_const <double>::get_wide_int (wide_int *) const +{ + return false; +} + /* The write_reproducer specialization for <double>. */ template <> @@ -4215,6 +4272,15 @@ memento_of_new_rvalue_from_const <void *>::make_debug_string () m_type->get_debug_string ()); } +/* The get_wide_int specialization for <void *>. */ + +template <> +bool +memento_of_new_rvalue_from_const <void *>::get_wide_int (wide_int *) const +{ + return false; +} + /* Implementation of recording::memento::write_reproducer for <void *> values. */ @@ -5213,14 +5279,15 @@ recording::local::write_reproducer (reproducer &r) since this vfunc must only ever be called on terminator statements. */ -int -recording::statement::get_successor_blocks (block **/*out_next1*/, - block **/*out_next2*/) const +vec <recording::block *> +recording::statement::get_successor_blocks () const { /* The base class implementation is for non-terminating statements, and thus should never be called. */ gcc_unreachable (); - return 0; + vec <block *> result; + result.create (0); + return result; } /* Extend the default implementation of @@ -5429,13 +5496,14 @@ recording::conditional::replay_into (replayer *r) A conditional jump has 2 successor blocks. */ -int -recording::conditional::get_successor_blocks (block **out_next1, - block **out_next2) const +vec <recording::block *> +recording::conditional::get_successor_blocks () const { - *out_next1 = m_on_true; - *out_next2 = m_on_false; - return 2; + vec <block *> result; + result.create (2); + result.quick_push (m_on_true); + result.quick_push (m_on_false); + return result; } /* Implementation of recording::memento::make_debug_string for @@ -5493,12 +5561,13 @@ recording::jump::replay_into (replayer *r) An unconditional jump has 1 successor block. */ -int -recording::jump::get_successor_blocks (block **out_next1, - block **/*out_next2*/) const +vec <recording::block *> +recording::jump::get_successor_blocks () const { - *out_next1 = m_target; - return 1; + vec <block *> result; + result.create (1); + result.quick_push (m_target); + return result; } /* Implementation of recording::memento::make_debug_string for @@ -5544,11 +5613,12 @@ recording::return_::replay_into (replayer *r) A return statement has no successor block. */ -int -recording::return_::get_successor_blocks (block **/*out_next1*/, - block **/*out_next2*/) const +vec <recording::block *> +recording::return_::get_successor_blocks () const { - return 0; + vec <block *> result; + result.create (0); + return result; } /* Implementation of recording::memento::make_debug_string for @@ -5586,6 +5656,158 @@ recording::return_::write_reproducer (reproducer &r) r.get_identifier (get_loc ())); } +/* The implementation of class gcc::jit::recording::case_. */ + +void +recording::case_::write_reproducer (reproducer &r) +{ + const char *id = r.make_identifier (this, "case"); + const char *fmt = + " gcc_jit_case *%s = \n" + " gcc_jit_context_new_case (%s, /*gcc_jit_context *ctxt */\n" + " %s, /* gcc_jit_rvalue *min_value */\n" + " %s, /* gcc_jit_rvalue *max_value */\n" + " %s); /* gcc_jit_block *dest_block */\n"; + r.write (fmt, + id, + r.get_identifier (get_context ()), + r.get_identifier_as_rvalue (m_min_value), + r.get_identifier_as_rvalue (m_max_value), + r.get_identifier (m_dest_block)); +} + +recording::string * +recording::case_::make_debug_string () +{ + return string::from_printf (get_context (), + "case %s ... %s: goto %s;", + m_min_value->get_debug_string (), + m_max_value->get_debug_string (), + m_dest_block->get_debug_string ()); +} + +/* The implementation of class gcc::jit::recording::switch_. */ + +/* gcc::jit::recording::switch_'s constructor. */ + +recording::switch_::switch_ (block *b, + location *loc, + rvalue *expr, + block *default_block, + int num_cases, + case_ **cases) +: statement (b, loc), + m_expr (expr), + m_default_block (default_block) +{ + m_cases.reserve_exact (num_cases); + for (int i = 0; i< num_cases; i++) + m_cases.quick_push (cases[i]); +} + +/* Implementation of pure virtual hook recording::memento::replay_into + for recording::switch_. */ + +void +recording::switch_::replay_into (replayer *r) +{ + auto_vec <playback::case_> pcases; + int i; + recording::case_ *rcase; + pcases.reserve_exact (m_cases.length ()); + FOR_EACH_VEC_ELT (m_cases, i, rcase) + { + playback::case_ pcase (rcase->get_min_value ()->playback_rvalue (), + rcase->get_max_value ()->playback_rvalue (), + rcase->get_dest_block ()->playback_block ()); + pcases.safe_push (pcase); + } + playback_block (get_block ()) + ->add_switch (playback_location (r), + m_expr->playback_rvalue (), + m_default_block->playback_block (), + &pcases); +} + +/* Override the poisoned default implementation of + gcc::jit::recording::statement::get_successor_blocks + + A switch statement has (NUM_CASES + 1) successor blocks. */ + +vec <recording::block *> +recording::switch_::get_successor_blocks () const +{ + vec <block *> result; + result.create (m_cases.length () + 1); + result.quick_push (m_default_block); + int i; + case_ *c; + FOR_EACH_VEC_ELT (m_cases, i, c) + result.quick_push (c->get_dest_block ()); + return result; +} + +/* Implementation of recording::memento::make_debug_string for + a switch statement. */ + +recording::string * +recording::switch_::make_debug_string () +{ + auto_vec <char> cases_str; + int i; + case_ *c; + FOR_EACH_VEC_ELT (m_cases, i, c) + { + size_t len = strlen (c->get_debug_string ()); + unsigned idx = cases_str.length (); + cases_str.safe_grow (idx + 1 + len); + cases_str[idx] = ' '; + memcpy (&(cases_str[idx + 1]), + c->get_debug_string (), + len); + } + cases_str.safe_push ('\0'); + + return string::from_printf (m_ctxt, + "switch (%s) {default: goto %s;%s}", + m_expr->get_debug_string (), + m_default_block->get_debug_string (), + &cases_str[0]); +} + +/* Implementation of recording::memento::write_reproducer for + switch statements. */ + +void +recording::switch_::write_reproducer (reproducer &r) +{ + r.make_identifier (this, "switch"); + int i; + case_ *c; + const char *cases_id = + r.make_tmp_identifier ("cases_for", this); + r.write (" gcc_jit_case *%s[%i] = {\n", + cases_id, + m_cases.length ()); + FOR_EACH_VEC_ELT (m_cases, i, c) + r.write (" %s,\n", r.get_identifier (c)); + r.write (" };\n"); + const char *fmt = + " gcc_jit_block_end_with_switch (%s, /*gcc_jit_block *block */\n" + " %s, /* gcc_jit_location *loc */\n" + " %s, /* gcc_jit_rvalue *expr */\n" + " %s, /* gcc_jit_block *default_block */\n" + " %i, /* int num_cases */\n" + " %s); /* gcc_jit_case **cases */\n"; + r.write (fmt, + r.get_identifier (get_block ()), + r.get_identifier (get_loc ()), + r.get_identifier_as_rvalue (m_expr), + r.get_identifier (m_default_block), + m_cases.length (), + cases_id); +} + } // namespace gcc::jit } // namespace gcc diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h index a9bcbb5..acd69e9 100644 --- a/gcc/jit/jit-recording.h +++ b/gcc/jit/jit-recording.h @@ -183,6 +183,11 @@ public: rvalue *ptr, rvalue *index); + case_ * + new_case (rvalue *min_value, + rvalue *max_value, + block *block); + void set_str_option (enum gcc_jit_str_option opt, const char *value); @@ -954,6 +959,9 @@ public: const char * get_debug_string_parens (enum precedence outer_prec); + virtual bool is_constant () const { return false; } + virtual bool get_wide_int (wide_int *) const { return false; } + private: virtual enum precedence get_precedence () const = 0; @@ -1152,6 +1160,13 @@ public: end_with_return (location *loc, rvalue *rvalue); + statement * + end_with_switch (location *loc, + rvalue *expr, + block *default_block, + int num_cases, + case_ **cases); + playback::block * playback_block () const { @@ -1167,7 +1182,7 @@ public: statement *get_first_statement () const; statement *get_last_statement () const; - int get_successor_blocks (block **next1, block **next2) const; + vec <block *> get_successor_blocks () const; private: string * make_debug_string (); @@ -1233,6 +1248,10 @@ public: void visit_children (rvalue_visitor *) {} + bool is_constant () const { return true; } + + bool get_wide_int (wide_int *out) const; + private: string * make_debug_string (); void write_reproducer (reproducer &r); @@ -1596,8 +1615,7 @@ private: class statement : public memento { public: - virtual int get_successor_blocks (block **out_next1, - block **out_next2) const; + virtual vec <block *> get_successor_blocks () const; void write_to_dump (dump &d); @@ -1721,8 +1739,7 @@ public: void replay_into (replayer *r); - int get_successor_blocks (block **out_next1, - block **out_next2) const; + vec <block *> get_successor_blocks () const; private: string * make_debug_string (); @@ -1745,8 +1762,7 @@ public: void replay_into (replayer *r); - int get_successor_blocks (block **out_next1, - block **out_next2) const; + vec <block *> get_successor_blocks () const; private: string * make_debug_string (); @@ -1767,8 +1783,7 @@ public: void replay_into (replayer *r); - int get_successor_blocks (block **out_next1, - block **out_next2) const; + vec <block *> get_successor_blocks () const; private: string * make_debug_string (); @@ -1778,6 +1793,60 @@ private: rvalue *m_rvalue; }; +class case_ : public memento +{ + public: + case_ (context *ctxt, + rvalue *min_value, + rvalue *max_value, + block *dest_block) + : memento (ctxt), + m_min_value (min_value), + m_max_value (max_value), + m_dest_block (dest_block) + {} + + rvalue *get_min_value () const { return m_min_value; } + rvalue *get_max_value () const { return m_max_value; } + block *get_dest_block () const { return m_dest_block; } + + void replay_into (replayer *) { /* empty */ } + + void write_reproducer (reproducer &r); + +private: + string * make_debug_string (); + + private: + rvalue *m_min_value; + rvalue *m_max_value; + block *m_dest_block; +}; + +class switch_ : public statement +{ +public: + switch_ (block *b, + location *loc, + rvalue *expr, + block *default_block, + int num_cases, + case_ **cases); + + void replay_into (replayer *r); + + vec <block *> get_successor_blocks () const; + +private: + string * make_debug_string (); + void write_reproducer (reproducer &r); + +private: + rvalue *m_expr; + block *m_default_block; + auto_vec <case_ *> m_cases; +}; + } // namespace gcc::jit::recording /* Create a recording::memento_of_new_rvalue_from_const instance and add diff --git a/gcc/jit/libgccjit++.h b/gcc/jit/libgccjit++.h index cbdc96f..01579bd7 100644 --- a/gcc/jit/libgccjit++.h +++ b/gcc/jit/libgccjit++.h @@ -45,6 +45,7 @@ namespace gccjit class rvalue; class lvalue; class param; + class case_; /* Errors within the API become C++ exceptions of this class. */ class error @@ -297,6 +298,10 @@ namespace gccjit rvalue index, location loc = location ()); + case_ new_case (rvalue min_value, + rvalue max_value, + block dest_block); + private: gcc_jit_context *m_inner_ctxt; }; @@ -421,6 +426,10 @@ namespace gccjit location loc = location ()); void end_with_return (location loc = location ()); + void end_with_switch (rvalue expr, + block default_block, + std::vector <case_> cases, + location loc = location ()); }; class rvalue : public object @@ -471,6 +480,14 @@ namespace gccjit gcc_jit_param *get_inner_param () const; }; + class case_ : public object + { + public: + case_ (); + case_ (gcc_jit_case *inner); + + gcc_jit_case *get_inner_case () const; + }; /* Overloaded operators, for those who want the most terse API (at the possible risk of being a little too magical). @@ -1124,6 +1141,17 @@ context::new_array_access (rvalue ptr, index.get_inner_rvalue ())); } +inline case_ +context::new_case (rvalue min_value, + rvalue max_value, + block dest_block) +{ + return case_ (gcc_jit_context_new_case (m_inner_ctxt, + min_value.get_inner_rvalue (), + max_value.get_inner_rvalue (), + dest_block.get_inner_block ())); +} + // class object inline context object::get_context () const @@ -1371,6 +1399,27 @@ block::end_with_return (location loc) loc.get_inner_location ()); } +inline void +block::end_with_switch (rvalue expr, + block default_block, + std::vector <case_> cases, + location loc) +{ + /* Treat std::vector as an array, relying on it not being resized: */ + case_ *as_array_of_wrappers = &cases[0]; + + /* Treat the array as being of the underlying pointers, relying on + the wrapper type being such a pointer internally. */ + gcc_jit_case **as_array_of_ptrs = + reinterpret_cast<gcc_jit_case **> (as_array_of_wrappers); + gcc_jit_block_end_with_switch (get_inner_block (), + loc.get_inner_location (), + expr.get_inner_rvalue (), + default_block.get_inner_block (), + cases.size (), + as_array_of_ptrs); +} + inline rvalue block::add_call (function other, location loc) @@ -1561,6 +1610,20 @@ inline param::param (gcc_jit_param *inner) : lvalue (gcc_jit_param_as_lvalue (inner)) {} +// class case_ : public object +inline case_::case_ () : object () {} +inline case_::case_ (gcc_jit_case *inner) + : object (gcc_jit_case_as_object (inner)) +{ +} + +inline gcc_jit_case * +case_::get_inner_case () const +{ + /* Manual downcast: */ + return reinterpret_cast<gcc_jit_case *> (get_inner_object ()); +} + /* Overloaded operators. */ // Unary operators inline rvalue operator- (rvalue a) diff --git a/gcc/jit/libgccjit.c b/gcc/jit/libgccjit.c index 7e0bfa6..4d7dd8c 100644 --- a/gcc/jit/libgccjit.c +++ b/gcc/jit/libgccjit.c @@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see #include "coretypes.h" #include "opts.h" #include "safe-ctype.h" +#include "typed-splay-tree.h" #include "libgccjit.h" #include "jit-common.h" @@ -84,6 +85,10 @@ struct gcc_jit_param : public gcc::jit::recording::param { }; +struct gcc_jit_case : public gcc::jit::recording::case_ +{ +}; + /********************************************************************** Error-handling. @@ -2123,6 +2128,382 @@ gcc_jit_block_end_with_void_return (gcc_jit_block *block, block->end_with_return (loc, NULL); } +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, the real work is done by the + gcc::jit::recording::context::new_case method in + jit-recording.c. */ + +gcc_jit_case * +gcc_jit_context_new_case (gcc_jit_context *ctxt, + gcc_jit_rvalue *min_value, + gcc_jit_rvalue *max_value, + gcc_jit_block *block) +{ + RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context"); + JIT_LOG_FUNC (ctxt->get_logger ()); + RETURN_NULL_IF_FAIL (min_value, ctxt, NULL, "NULL min_value"); + RETURN_NULL_IF_FAIL (max_value, ctxt, NULL, "NULL max_value"); + RETURN_NULL_IF_FAIL (block, ctxt, NULL, "NULL block"); + + RETURN_NULL_IF_FAIL_PRINTF1 (min_value->is_constant (), ctxt, NULL, + "min_value is not a constant: %s", + min_value->get_debug_string ()); + RETURN_NULL_IF_FAIL_PRINTF1 (max_value->is_constant (), ctxt, NULL, + "max_value is not a constant: %s", + max_value->get_debug_string ()); + RETURN_NULL_IF_FAIL_PRINTF2 ( + min_value->get_type ()->is_int (), + ctxt, NULL, + "min_value: %s (type: %s) is not of integer type", + min_value->get_debug_string (), + min_value->get_type ()->get_debug_string ()); + RETURN_NULL_IF_FAIL_PRINTF2 ( + max_value->get_type ()->is_int (), + ctxt, NULL, + "max_value: %s (type: %s) is not of integer type", + max_value->get_debug_string (), + max_value->get_type ()->get_debug_string ()); + + wide_int wi_min, wi_max; + if (!min_value->get_wide_int (&wi_min)) + gcc_unreachable (); + if (!max_value->get_wide_int (&wi_max)) + gcc_unreachable (); + RETURN_NULL_IF_FAIL_PRINTF2 ( + wi::les_p (wi_min, wi_max), + ctxt, NULL, + "min_value: %s > max_value: %s", + min_value->get_debug_string (), + max_value->get_debug_string ()); + return (gcc_jit_case *)ctxt->new_case (min_value, + max_value, + block); +} + +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, this calls the trivial + gcc::jit::recording::memento::as_object method (a case is a + memento), in jit-recording.h. */ + +gcc_jit_object * +gcc_jit_case_as_object (gcc_jit_case *case_) +{ + RETURN_NULL_IF_FAIL (case_, NULL, NULL, "NULL case"); + + return static_cast <gcc_jit_object *> (case_->as_object ()); +} + +/* Helper function for gcc_jit_block_end_with_switch and + valid_case_for_switch. */ + +static bool +valid_dest_for_switch (gcc::jit::recording::context *ctxt, + gcc_jit_location *loc, + const char *api_funcname, + gcc::jit::recording::block *switch_block, + gcc::jit::recording::block *dest_block, + const char *dest_block_desc) +{ + if (!dest_block) + { + jit_error (ctxt, loc, "%s: NULL %s", api_funcname, dest_block_desc); + return false; + } + gcc::jit::recording::function *switch_fn = switch_block->get_function (); + gcc::jit::recording::function *dest_fn = dest_block->get_function (); + if (switch_fn != dest_fn) + { + jit_error (ctxt, loc, + "%s: %s is not in same function:" + " switch block %s is in function %s" + " whereas %s %s is in function %s", + api_funcname, + dest_block_desc, + switch_block->get_debug_string (), + switch_fn->get_debug_string (), + dest_block_desc, + dest_block->get_debug_string (), + dest_fn->get_debug_string ()); + return false; + } + return true; +} + +/* Helper function for gcc_jit_block_end_with_switch. */ + +static bool +valid_case_for_switch (gcc::jit::recording::context *ctxt, + gcc_jit_location *loc, + const char *api_funcname, + gcc_jit_block *switch_block, + gcc_jit_rvalue *expr, + gcc_jit_case *case_, + const char *case_desc, + int case_idx) +{ + if (!case_) + { + jit_error (ctxt, loc, + "%s:" + " NULL case %i", + api_funcname, + case_idx); + return false; + } + if (!valid_dest_for_switch (ctxt, loc, + api_funcname, + switch_block, + case_->get_dest_block (), + case_desc)) + return false; + gcc::jit::recording::type *expr_type = expr->get_type (); + if (expr_type != case_->get_min_value ()->get_type ()) + { + jit_error (ctxt, loc, + "%s:" + " mismatching types between case and expression:" + " cases[%i]->min_value: %s (type: %s)" + " expr: %s (type: %s)", + api_funcname, + case_idx, + case_->get_min_value ()->get_debug_string (), + case_->get_min_value ()->get_type ()->get_debug_string (), + expr->get_debug_string (), + expr_type->get_debug_string ()); + return false; + } + if (expr_type != case_->get_max_value ()->get_type ()) + { + jit_error (ctxt, loc, + "%s:" + " mismatching types between case and expression:" + " cases[%i]->max_value: %s (type: %s)" + " expr: %s (type: %s)", + api_funcname, + case_idx, + case_->get_max_value ()->get_debug_string (), + case_->get_max_value ()->get_type ()->get_debug_string (), + expr->get_debug_string (), + expr_type->get_debug_string ()); + return false; + } + return true; +} + +/* A class for holding the data we need to perform error-checking + on a libgccjit API call. */ + +class api_call_validator +{ + public: + api_call_validator (gcc::jit::recording::context *ctxt, + gcc_jit_location *loc, + const char *funcname) + : m_ctxt (ctxt), + m_loc (loc), + m_funcname (funcname) + {} + + protected: + gcc::jit::recording::context *m_ctxt; + gcc_jit_location *m_loc; + const char *m_funcname; +}; + +/* A class for verifying that the ranges of cases within + gcc_jit_block_end_with_switch don't overlap. */ + +class case_range_validator : public api_call_validator +{ + public: + case_range_validator (gcc::jit::recording::context *ctxt, + gcc_jit_location *loc, + const char *funcname); + + bool + validate (gcc_jit_case *case_, int idx); + + private: + static int + case_compare (gcc::jit::recording::rvalue *k1, + gcc::jit::recording::rvalue *k2); + + static wide_int + get_wide_int (gcc::jit::recording::rvalue *k); + + private: + typed_splay_tree <gcc::jit::recording::rvalue *, gcc_jit_case *> m_cases; +}; + +/* case_range_validator's ctor. */ + +case_range_validator::case_range_validator (gcc::jit::recording::context *ctxt, + gcc_jit_location *loc, + const char *funcname) +: api_call_validator (ctxt, loc, funcname), + m_cases (case_compare, NULL, NULL) +{ +} + +/* Ensure that the range of CASE_ does not overlap with any of the + ranges of cases we've already seen. + Return true if everything is OK. + Return false and emit an error if there is an overlap. + Compare with c-family/c-common.c:c_add_case_label. */ + +bool +case_range_validator::validate (gcc_jit_case *case_, + int case_idx) +{ + /* Look up the LOW_VALUE in the table of case labels we already + have. */ + gcc_jit_case *other = m_cases.lookup (case_->get_min_value ()); + + /* If there was not an exact match, check for overlapping ranges. */ + if (!other) + { + gcc_jit_case *pred; + gcc_jit_case *succ; + + /* Even though there wasn't an exact match, there might be an + overlap between this case range and another case range. + Since we've (inductively) not allowed any overlapping case + ranges, we simply need to find the greatest low case label + that is smaller that CASE_MIN_VALUE, and the smallest low case + label that is greater than CASE_MAX_VALUE. If there is an overlap + it will occur in one of these two ranges. */ + pred = m_cases.predecessor (case_->get_min_value ()); + succ = m_cases.successor (case_->get_max_value ()); + + /* Check to see if the PRED overlaps. It is smaller than + the LOW_VALUE, so we only need to check its max value. */ + if (pred) + { + wide_int wi_case_min = get_wide_int (case_->get_min_value ()); + wide_int wi_pred_max = get_wide_int (pred->get_max_value ()); + if (wi::ges_p (wi_pred_max, wi_case_min)) + other = pred; + } + + if (!other && succ) + { + /* Check to see if the SUCC overlaps. The low end of that + range is bigger than the low end of the current range. */ + wide_int wi_case_max = get_wide_int (case_->get_max_value ()); + wide_int wi_succ_min = get_wide_int (succ->get_min_value ()); + if (wi::les_p (wi_succ_min, wi_case_max)) + other = succ; + } + } + + /* If there was an overlap, issue an error. */ + if (other) + { + jit_error (m_ctxt, m_loc, + "%s: duplicate (or overlapping) cases values:" + " case %i: %s overlaps %s", + m_funcname, + case_idx, + case_->get_debug_string (), + other->get_debug_string ()); + return false; + } + + /* Register this case label in the splay tree. */ + m_cases.insert (case_->get_min_value (), + case_); + return true; +} + +/* Compare with c-family/c-common.c:case_compare, which acts on tree + nodes, rather than rvalue *. + + Comparator for case label values. K1 and K2 must be constant integer + values (anything else should have been rejected by + gcc_jit_context_new_case. + + Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after + K2, and 0 if K1 and K2 are equal. */ + +int +case_range_validator::case_compare (gcc::jit::recording::rvalue * k1, + gcc::jit::recording::rvalue * k2) +{ + wide_int wi1 = get_wide_int (k1); + wide_int wi2 = get_wide_int (k2); + return wi::cmps(wi1, wi2); +} + +/* Given a const int rvalue K, get the underlying value as a wide_int. */ + +wide_int +case_range_validator::get_wide_int (gcc::jit::recording::rvalue *k) +{ + wide_int wi; + bool got_wi = k->get_wide_int (&wi); + gcc_assert (got_wi); + return wi; +} + +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, the real work is done by the + gcc::jit::recording::block::end_with_switch method in + jit-recording.c. */ + +void +gcc_jit_block_end_with_switch (gcc_jit_block *block, + gcc_jit_location *loc, + gcc_jit_rvalue *expr, + gcc_jit_block *default_block, + int num_cases, + gcc_jit_case **cases) +{ + RETURN_IF_NOT_VALID_BLOCK (block, loc); + gcc::jit::recording::context *ctxt = block->get_context (); + JIT_LOG_FUNC (ctxt->get_logger ()); + /* LOC can be NULL. */ + RETURN_IF_FAIL (expr, ctxt, loc, + "NULL expr"); + gcc::jit::recording::type *expr_type = expr->get_type (); + RETURN_IF_FAIL_PRINTF2 ( + expr_type->is_int (), + ctxt, loc, + "expr: %s (type: %s) is not of integer type", + expr->get_debug_string (), + expr_type->get_debug_string ()); + if (!valid_dest_for_switch (ctxt, loc, + __func__, + block, + default_block, + "default_block")) + return; + RETURN_IF_FAIL (num_cases >= 0, ctxt, loc, "num_cases < 0"); + case_range_validator crv (ctxt, loc, __func__); + for (int i = 0; i < num_cases; i++) + { + char case_desc[32]; + snprintf (case_desc, sizeof (case_desc), + "cases[%i]", i); + if (!valid_case_for_switch (ctxt, loc, + __func__, + block, + expr, + cases[i], + case_desc, + i)) + return; + if (!crv.validate (cases[i], i)) + return; + } + + block->end_with_switch (loc, expr, default_block, + num_cases, + (gcc::jit::recording::case_ **)cases); +} + /********************************************************************** Option-management **********************************************************************/ diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h index e33900c..32f2a5d 100644 --- a/gcc/jit/libgccjit.h +++ b/gcc/jit/libgccjit.h @@ -67,6 +67,7 @@ typedef struct gcc_jit_result gcc_jit_result; +- gcc_jit_rvalue +- gcc_jit_lvalue +- gcc_jit_param + +- gcc_jit_case */ typedef struct gcc_jit_object gcc_jit_object; @@ -131,6 +132,12 @@ typedef struct gcc_jit_lvalue gcc_jit_lvalue; rvalue); use gcc_jit_param_as_lvalue to convert. */ typedef struct gcc_jit_param gcc_jit_param; +/* A gcc_jit_case is for use when building multiway branches via + gcc_jit_block_end_with_switch and represents a range of integer + values (or an individual integer value) together with an associated + destination block. */ +typedef struct gcc_jit_case gcc_jit_case; + /* Acquire a JIT-compilation context. */ extern gcc_jit_context * gcc_jit_context_acquire (void); @@ -1097,6 +1104,81 @@ extern void gcc_jit_block_end_with_void_return (gcc_jit_block *block, gcc_jit_location *loc); +/* Create a new gcc_jit_case instance for use in a switch statement. + min_value and max_value must be constants of integer type. + + This API entrypoint was added in LIBGCCJIT_ABI_3; you can test for its + presence using + #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS +*/ +extern gcc_jit_case * +gcc_jit_context_new_case (gcc_jit_context *ctxt, + gcc_jit_rvalue *min_value, + gcc_jit_rvalue *max_value, + gcc_jit_block *dest_block); + +/* Upcasting from case to object. + + This API entrypoint was added in LIBGCCJIT_ABI_3; you can test for its + presence using + #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS +*/ + +extern gcc_jit_object * +gcc_jit_case_as_object (gcc_jit_case *case_); + +/* Terminate a block by adding evalation of an rvalue, then performing + a multiway branch. + + This is roughly equivalent to this C code: + + switch (expr) + { + default: + goto default_block; + + case C0.min_value ... C0.max_value: + goto C0.dest_block; + + case C1.min_value ... C1.max_value: + goto C1.dest_block; + + ...etc... + + case C[N - 1].min_value ... C[N - 1].max_value: + goto C[N - 1].dest_block; + } + + block, expr, default_block and cases must all be non-NULL. + + expr must be of the same integer type as all of the min_value + and max_value within the cases. + + num_cases must be >= 0. + + The ranges of the cases must not overlap (or have duplicate + values). + + This API entrypoint was added in LIBGCCJIT_ABI_3; you can test for its + presence using + #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS +*/ + +extern void +gcc_jit_block_end_with_switch (gcc_jit_block *block, + gcc_jit_location *loc, + gcc_jit_rvalue *expr, + gcc_jit_block *default_block, + int num_cases, + gcc_jit_case **cases); + +/* Pre-canned feature macro to indicate the presence of + gcc_jit_block_end_with_switch, gcc_jit_case_as_object, and + gcc_jit_context_new_case. + + This can be tested for with #ifdef. */ +#define LIBGCCJIT_HAVE_SWITCH_STATEMENTS + /********************************************************************** Nested contexts. **********************************************************************/ diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map index b55df1e..3c0a0c1 100644 --- a/gcc/jit/libgccjit.map +++ b/gcc/jit/libgccjit.map @@ -120,3 +120,11 @@ LIBGCCJIT_ABI_2 { global: gcc_jit_context_set_bool_allow_unreachable_blocks; } LIBGCCJIT_ABI_1; + +# Add support for switch statements. +LIBGCCJIT_ABI_3 { + global: + gcc_jit_block_end_with_switch; + gcc_jit_case_as_object; + gcc_jit_context_new_case; +} LIBGCCJIT_ABI_2; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3b4879d..80be58e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,23 @@ 2015-06-30 David Malcolm <dmalcolm@redhat.com> + * jit.dg/all-non-failing-tests.h: Add test-switch.c. + * jit.dg/test-error-gcc_jit_block_end_with_switch-NULL-case.c: New + testcase. + * jit.dg/test-error-gcc_jit_block_end_with_switch-mismatching-case-type.c: + New testcase. + * jit.dg/test-error-gcc_jit_block_end_with_switch-overlapping-ranges.c: + New testcase. + * jit.dg/test-error-gcc_jit_context_new_case-non-const-label.c: + New testcase. + * jit.dg/test-error-gcc_jit_context_new_case-non-integer-type.c: + New testcase. + * jit.dg/test-error-gcc_jit_context_new_case-reversed-endpoints.c: + New testcase. + * jit.dg/test-switch.c: New testcase. + * jit.dg/test-switch.cc: New testcase. + +2015-06-30 David Malcolm <dmalcolm@redhat.com> + PR jit/66546 * jit.dg/all-non-failing-tests.h: Add note about test-validly-unreachable-block.c. diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h index 67a16de..36a6160 100644 --- a/gcc/testsuite/jit.dg/all-non-failing-tests.h +++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h @@ -175,6 +175,13 @@ #undef create_code #undef verify_code +/* test-switch.c */ +#define create_code create_code_switch +#define verify_code verify_code_switch +#include "test-switch.c" +#undef create_code +#undef verify_code + /* test-types.c */ #define create_code create_code_types #define verify_code verify_code_types @@ -281,6 +288,9 @@ const struct testcase testcases[] = { {"sum_of_squares", create_code_sum_of_squares, verify_code_sum_of_squares}, + {"switch", + create_code_switch, + verify_code_switch}, {"types", create_code_types, verify_code_types}, diff --git a/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-NULL-case.c b/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-NULL-case.c new file mode 100644 index 0000000..07a9848 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-NULL-case.c @@ -0,0 +1,66 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case x: + return 3; + + default: + return 10; + } + } + and verify that we get a sane error about the non-const + case. + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + + gcc_jit_case *cases[1] = { + NULL + }; + + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 1, + cases); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_VALUE (result, NULL); + + CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), + "gcc_jit_block_end_with_switch: NULL case 0"); +} diff --git a/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-mismatching-case-type.c b/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-mismatching-case-type.c new file mode 100644 index 0000000..cc907ce --- /dev/null +++ b/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-mismatching-case-type.c @@ -0,0 +1,83 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case (long long)0 ... (long long)5: + return 3; + default: + return 10; + } + } + and verify that the floating-point case is an error. + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *t_long_long = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + gcc_jit_block *b_case_0 = + gcc_jit_function_new_block (func, "case_0"); + + /* Note the erroneous use of "t_float" here. */ + gcc_jit_case *cases[1] = { + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_long_long, 0), + gcc_jit_context_new_rvalue_from_int (ctxt, t_long_long, 5), + b_case_0) + }; + + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 1, + cases); + + gcc_jit_block_end_with_return ( + b_case_0, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3)); + gcc_jit_block_end_with_return ( + b_default, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10)); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_VALUE (result, NULL); + + CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), + "gcc_jit_block_end_with_switch:" + " mismatching types between case and expression:" + " cases[0]->min_value: (long long)0 (type: long long)" + " expr: x (type: int)"); +} diff --git a/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-overlapping-ranges.c b/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-overlapping-ranges.c new file mode 100644 index 0000000..40655c2 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-error-gcc_jit_block_end_with_switch-overlapping-ranges.c @@ -0,0 +1,95 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case 0 ... 5: + return 3; + + case 5 ... 10: + return 4; + + default: + return 10; + } + } + and verify that we get an error about the overlapping + ranges. + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + gcc_jit_block *b_case_0_5 = + gcc_jit_function_new_block (func, "case_0_5"); + gcc_jit_block *b_case_5_10 = + gcc_jit_function_new_block (func, "case_5_10"); + + gcc_jit_case *cases[2] = { + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 0), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5), + b_case_0_5), + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10), + b_case_5_10) + }; + + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 2, + cases); + + gcc_jit_block_end_with_return ( + b_case_0_5, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3)); + gcc_jit_block_end_with_return ( + b_case_5_10, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 4)); + gcc_jit_block_end_with_return ( + b_default, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10)); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_VALUE (result, NULL); + + CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), + "gcc_jit_block_end_with_switch:" + " duplicate (or overlapping) cases values:" + " case 1: case (int)5 ... (int)10: goto case_5_10;" + " overlaps case (int)0 ... (int)5: goto case_0_5;"); +} diff --git a/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-non-const-label.c b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-non-const-label.c new file mode 100644 index 0000000..3953818 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-non-const-label.c @@ -0,0 +1,80 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case x: + return 3; + + default: + return 10; + } + } + and verify that we get a sane error about the non-const + case. + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + gcc_jit_block *b_case_x = + gcc_jit_function_new_block (func, "case_x"); + + /* Erroneous use of non-const x for a case. */ + gcc_jit_case *cases[1] = { + gcc_jit_context_new_case ( + ctxt, + gcc_jit_param_as_rvalue (x), + gcc_jit_param_as_rvalue (x), + b_case_x) + }; + + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 1, + cases); + gcc_jit_block_end_with_return ( + b_case_x, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3)); + gcc_jit_block_end_with_return ( + b_default, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10)); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_VALUE (result, NULL); + + CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), + "gcc_jit_context_new_case:" + " min_value is not a constant: x"); +} diff --git a/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-non-integer-type.c b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-non-integer-type.c new file mode 100644 index 0000000..5d44286 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-non-integer-type.c @@ -0,0 +1,81 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case 0.f ... 5.f: + return 3; + default: + return 10; + } + } + and verify that the floating-point case is an error. + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *t_float = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + gcc_jit_block *b_case_0 = + gcc_jit_function_new_block (func, "case_0"); + + /* Note the erroneous use of "t_float" here. */ + gcc_jit_case *cases[1] = { + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_float, 0), + gcc_jit_context_new_rvalue_from_int (ctxt, t_float, 5), + b_case_0) + }; + + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 1, + cases); + + gcc_jit_block_end_with_return ( + b_case_0, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3)); + gcc_jit_block_end_with_return ( + b_default, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10)); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_VALUE (result, NULL); + + CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), + "gcc_jit_context_new_case:" + " min_value: (float)0 (type: float) is not of integer type"); +} diff --git a/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-reversed-endpoints.c b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-reversed-endpoints.c new file mode 100644 index 0000000..a84d9f3 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_new_case-reversed-endpoints.c @@ -0,0 +1,80 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case 5 ... 0: + return 3; + + default: + return 10; + } + } + and verify that we get an error about the reversed endpoints + in the range. + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + gcc_jit_block *b_case_5_0 = + gcc_jit_function_new_block (func, "case_5_0"); + + gcc_jit_case *cases[1] = { + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 0), + b_case_5_0) + }; + + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 1, + cases); + + gcc_jit_block_end_with_return ( + b_case_5_0, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3)); + gcc_jit_block_end_with_return ( + b_default, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10)); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_VALUE (result, NULL); + + CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), + "gcc_jit_context_new_case:" + " min_value: (int)5 > max_value: (int)0"); +} diff --git a/gcc/testsuite/jit.dg/test-switch.c b/gcc/testsuite/jit.dg/test-switch.c new file mode 100644 index 0000000..74088c8 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-switch.c @@ -0,0 +1,147 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit.h" + +#include "harness.h" + +/* Quote from here in docs/topics/functions.rst. */ + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case 0 ... 5: + return 3; + + case 25 ... 27: + return 4; + + case -42 ... -17: + return 83; + + case 40: + return 8; + + default: + return 10; + } + } + */ + gcc_jit_type *t_int = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *return_type = t_int; + gcc_jit_param *x = + gcc_jit_context_new_param (ctxt, NULL, t_int, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + 1, params, 0); + + gcc_jit_block *b_initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block *b_default = + gcc_jit_function_new_block (func, "default"); + gcc_jit_block *b_case_0_5 = + gcc_jit_function_new_block (func, "case_0_5"); + gcc_jit_block *b_case_25_27 = + gcc_jit_function_new_block (func, "case_25_27"); + gcc_jit_block *b_case_m42_m17 = + gcc_jit_function_new_block (func, "case_m42_m17"); + gcc_jit_block *b_case_40 = + gcc_jit_function_new_block (func, "case_40"); + + gcc_jit_case *cases[4] = { + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 0), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5), + b_case_0_5), + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 25), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 27), + b_case_25_27), + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, -42), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, -17), + b_case_m42_m17), + gcc_jit_context_new_case ( + ctxt, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 40), + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 40), + b_case_40) + }; + gcc_jit_block_end_with_switch ( + b_initial, NULL, + gcc_jit_param_as_rvalue (x), + b_default, + 4, cases); + + gcc_jit_block_end_with_return ( + b_case_0_5, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3)); + gcc_jit_block_end_with_return ( + b_case_25_27, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 4)); + gcc_jit_block_end_with_return ( + b_case_m42_m17, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 83)); + gcc_jit_block_end_with_return ( + b_case_40, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 8)); + gcc_jit_block_end_with_return ( + b_default, NULL, + gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10)); +} + +/* Quote up to here in docs/topics/functions.rst. */ + +static int +c_test_switch (int x) +{ + switch (x) + { + case 0 ... 5: + return 3; + case 25 ... 27: + return 4; + case -42 ... -17: + return 83; + case 40: + return 8; + default: + return 10; + } +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + typedef int (*test_switch_type) (int); + CHECK_NON_NULL (result); + test_switch_type test_switch = + (test_switch_type)gcc_jit_result_get_code (result, "test_switch"); + CHECK_NON_NULL (test_switch); + + int i; + + for (i = -255; i < 255; i++) + { + int val = test_switch (i); + int exp = c_test_switch (i); + if (val != exp) + fail ("test_switch (%i) returned: %i; expected; %i", i, val, exp); + } +} diff --git a/gcc/testsuite/jit.dg/test-switch.cc b/gcc/testsuite/jit.dg/test-switch.cc new file mode 100644 index 0000000..862f7a8 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-switch.cc @@ -0,0 +1,118 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "libgccjit++.h" + +#include "harness.h" + +/* Quote from here in docs/cp/topics/functions.rst. */ + +void +create_code (gcc_jit_context *c_ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + int + test_switch (int x) + { + switch (x) + { + case 0 ... 5: + return 3; + + case 25 ... 27: + return 4; + + case -42 ... -17: + return 83; + + case 40: + return 8; + + default: + return 10; + } + } + */ + gccjit::context ctxt (c_ctxt); + gccjit::type t_int = ctxt.get_type (GCC_JIT_TYPE_INT); + gccjit::type return_type = t_int; + gccjit::param x = ctxt.new_param (t_int, "x"); + std::vector <gccjit::param> params; + params.push_back (x); + gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED, + return_type, + "test_switch", + params, 0); + + gccjit::block b_initial = func.new_block ("initial"); + + gccjit::block b_default = func.new_block ("default"); + gccjit::block b_case_0_5 = func.new_block ("case_0_5"); + gccjit::block b_case_25_27 = func.new_block ("case_25_27"); + gccjit::block b_case_m42_m17 = func.new_block ("case_m42_m17"); + gccjit::block b_case_40 = func.new_block ("case_40"); + + std::vector <gccjit::case_> cases; + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 0), + ctxt.new_rvalue (t_int, 5), + b_case_0_5)); + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 25), + ctxt.new_rvalue (t_int, 27), + b_case_25_27)); + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, -42), + ctxt.new_rvalue (t_int, -17), + b_case_m42_m17)); + cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 40), + ctxt.new_rvalue (t_int, 40), + b_case_40)); + b_initial.end_with_switch (x, + b_default, + cases); + + b_case_0_5.end_with_return (ctxt.new_rvalue (t_int, 3)); + b_case_25_27.end_with_return (ctxt.new_rvalue (t_int, 4)); + b_case_m42_m17.end_with_return (ctxt.new_rvalue (t_int, 83)); + b_case_40.end_with_return (ctxt.new_rvalue (t_int, 8)); + b_default.end_with_return (ctxt.new_rvalue (t_int, 10)); +} + +/* Quote up to here in docs/cp/topics/functions.rst. */ + +static int +c_test_switch (int x) +{ + switch (x) + { + case 0 ... 5: + return 3; + case 25 ... 27: + return 4; + case -42 ... -17: + return 83; + case 40: + return 8; + default: + return 10; + } +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + typedef int (*test_switch_type) (int); + CHECK_NON_NULL (result); + test_switch_type test_switch = + (test_switch_type)gcc_jit_result_get_code (result, "test_switch"); + CHECK_NON_NULL (test_switch); + + int i; + + for (i = -255; i < 255; i++) + { + int val = test_switch (i); + int exp = c_test_switch (i); + if (val != exp) + fail ("test_switch (%i) returned: %i; expected; %i", i, val, exp); + } +} diff --git a/gcc/typed-splay-tree.h b/gcc/typed-splay-tree.h new file mode 100644 index 0000000..7849862 --- /dev/null +++ b/gcc/typed-splay-tree.h @@ -0,0 +1,135 @@ +/* A typesafe wrapper around libiberty's splay-tree.h. + Copyright (C) 2015 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#ifndef GCC_TYPED_SPLAY_TREE_H +#define GCC_TYPED_SPLAY_TREE_H + +#include "splay-tree.h" + +/* Typesafe wrapper around libiberty's splay-tree.h. */ +template <typename KEY_TYPE, typename VALUE_TYPE> +class typed_splay_tree +{ + public: + typedef KEY_TYPE key_type; + typedef VALUE_TYPE value_type; + + typedef int (*compare_fn) (key_type, key_type); + typedef void (*delete_key_fn) (key_type); + typedef void (*delete_value_fn) (value_type); + + typed_splay_tree (compare_fn, + delete_key_fn, + delete_value_fn); + ~typed_splay_tree (); + + value_type lookup (key_type k); + value_type predecessor (key_type k); + value_type successor (key_type k); + void insert (key_type k, value_type v); + + private: + static value_type node_to_value (splay_tree_node node); + + private: + ::splay_tree m_inner; +}; + +/* Constructor for typed_splay_tree <K, V>. */ + +template <typename KEY_TYPE, typename VALUE_TYPE> +inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>:: + typed_splay_tree (compare_fn compare_fn, + delete_key_fn delete_key_fn, + delete_value_fn delete_value_fn) +{ + m_inner = splay_tree_new ((splay_tree_compare_fn)compare_fn, + (splay_tree_delete_key_fn)delete_key_fn, + (splay_tree_delete_value_fn)delete_value_fn); +} + +/* Destructor for typed_splay_tree <K, V>. */ + +template <typename KEY_TYPE, typename VALUE_TYPE> +inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>:: + ~typed_splay_tree () +{ + splay_tree_delete (m_inner); +} + +/* Lookup KEY, returning a value if present, and NULL + otherwise. */ + +template <typename KEY_TYPE, typename VALUE_TYPE> +inline VALUE_TYPE +typed_splay_tree<KEY_TYPE, VALUE_TYPE>::lookup (key_type key) +{ + splay_tree_node node = splay_tree_lookup (m_inner, (splay_tree_key)key); + return node_to_value (node); +} + +/* Return the immediate predecessor of KEY, or NULL if there is no + predecessor. KEY need not be present in the tree. */ + +template <typename KEY_TYPE, typename VALUE_TYPE> +inline VALUE_TYPE +typed_splay_tree<KEY_TYPE, VALUE_TYPE>::predecessor (key_type key) +{ + splay_tree_node node = splay_tree_predecessor (m_inner, (splay_tree_key)key); + return node_to_value (node); +} + +/* Return the immediate successor of KEY, or NULL if there is no + successor. KEY need not be present in the tree. */ + +template <typename KEY_TYPE, typename VALUE_TYPE> +inline VALUE_TYPE +typed_splay_tree<KEY_TYPE, VALUE_TYPE>::successor (key_type k) +{ + splay_tree_node node = splay_tree_successor (m_inner, (splay_tree_key)k); + return node_to_value (node); +} + +/* Insert a new node (associating KEY with VALUE). If a + previous node with the indicated KEY exists, its data is replaced + with the new value. */ + +template <typename KEY_TYPE, typename VALUE_TYPE> +inline void +typed_splay_tree<KEY_TYPE, VALUE_TYPE>::insert (key_type key, + value_type value) +{ + splay_tree_insert (m_inner, + (splay_tree_key)key, + (splay_tree_value)value); +} + +/* Internal function for converting from splay_tree_node to + VALUE_TYPE. */ +template <typename KEY_TYPE, typename VALUE_TYPE> +inline VALUE_TYPE +typed_splay_tree<KEY_TYPE, VALUE_TYPE>::node_to_value (splay_tree_node node) +{ + if (node) + return (value_type)node->value; + else + return 0; +} + +#endif /* GCC_TYPED_SPLAY_TREE_H */ |