3 You can see the full documentation about what GIMPLE is [here](https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html). In this document we will explain how to generate it.
5 First, we'll copy the content from `gcc/gcc/testsuite/jit.dg/test-const-attribute.c` into a
6 file named `local.c` and remove the content we're not interested into:
9 - /* { dg-do compile { target x86_64-*-* } } */
11 - /* We don't want set_options() in harness.h to set -O3 to see that the const
12 - attribute affects the optimizations. */
13 - #define TEST_ESCHEWS_SET_OPTIONS
14 - static void set_options (gcc_jit_context *ctxt, const char *argv0)
17 - gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
20 - #define TEST_COMPILING_TO_FILE
21 - #define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
22 - #define OUTPUT_FILENAME "output-of-test-const-attribute.c.s"
23 - #include "harness.h"
25 - /* { dg-final { jit-verify-output-file-was-created "" } } */
26 - /* Check that the loop was optimized away */
27 - /* { dg-final { jit-verify-assembler-output-not "jne" } } */
30 Then we'll add a `main` function which will call the `create_code` function but
31 also add the calls we need to generate the GIMPLE:
35 gcc_jit_context *ctxt = gcc_jit_context_acquire();
36 // To set `-O3`, update it depending on your needs.
37 gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
38 // Very important option to generate the gimple format.
39 gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, 1);
40 create_code(ctxt, NULL);
42 gcc_jit_context_compile(ctxt);
43 // If you want to compile to assembly (or any other format) directly, you can
44 // use the following call instead:
45 // gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_ASSEMBLER, "out.s");
51 Then we can compile it by using:
54 gcc local.c -I `pwd`/gcc/gcc/jit/ -L `pwd`/gcc-build/gcc -lgccjit -o out
57 And finally when you run it:
60 LD_LIBRARY_PATH=`pwd`/gcc-build/gcc LIBRARY_PATH=`pwd`/gcc-build/gcc ./out
66 __attribute__((const))
79 if (x != 0) goto after_loop; else goto loop_body;
91 An alternative way to generate the GIMPLE is to replace:
94 gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, 1);
100 gcc_jit_context_add_command_line_option(ctxt, "-fdump-tree-gimple");
103 (although you can have both at the same time too). Then you can compile it like previously. Only one difference: before executing it, I recommend to run:
106 rm -rf /tmp/libgccjit-*
109 to make it easier for you to know which folder to look into.
111 Once the execution is done, you should now have a file with path looking like `/tmp/libgccjit-9OFqkD/fake.c.006t.gimple` which contains the GIMPLE format.