(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-pr103562.c
       1  #include <libgccjit.h>
       2  #include "harness.h"
       3  
       4  struct my_struct { long a; long b; long c; };
       5  
       6  void create_code (gcc_jit_context *ctxt, void *user_data)
       7  {
       8  	/* Create the equivalent of:
       9  		struct my_struct { long a; long b; long c; };
      10  		static struct my_struct deref(struct my_struct *ptr) { return *ptr; } 
      11  		long get_a(struct my_struct *s) { return deref(s).a; }
      12  	   and compile it at -O1.  */
      13  	gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 1);
      14  
      15  	gcc_jit_type *long_type = gcc_jit_context_get_type(ctxt, GCC_JIT_TYPE_LONG);
      16  	gcc_jit_field* fields[3] = {
      17  		gcc_jit_context_new_field(ctxt, NULL, long_type, "a"),
      18  		gcc_jit_context_new_field(ctxt, NULL, long_type, "b"),
      19  		gcc_jit_context_new_field(ctxt, NULL, long_type, "c"),
      20  	};
      21  	gcc_jit_struct *my_struct =
      22  		gcc_jit_context_new_struct_type(ctxt, NULL, "my_struct", 3, fields);
      23  	gcc_jit_type *my_struct_type = gcc_jit_struct_as_type(my_struct);
      24  	gcc_jit_type *my_struct_ptr_type = gcc_jit_type_get_pointer(my_struct_type);
      25  
      26  	/* struct my_struct deref(struct my_struct *ptr) { return *ptr; } */
      27  	gcc_jit_param *param_deref =
      28  		gcc_jit_context_new_param(ctxt, NULL, my_struct_ptr_type, "ptr");
      29  	gcc_jit_function *func_deref = gcc_jit_context_new_function(
      30  		ctxt, NULL, GCC_JIT_FUNCTION_INTERNAL,
      31  		my_struct_type, "deref",
      32  		1, &param_deref,
      33  		0);
      34  	gcc_jit_block *blockDeref = gcc_jit_function_new_block(func_deref, NULL);
      35  	gcc_jit_block_end_with_return(
      36  		blockDeref, NULL,
      37  		gcc_jit_lvalue_as_rvalue(gcc_jit_rvalue_dereference(gcc_jit_param_as_rvalue(param_deref), NULL)));
      38  
      39  	/* long get_a(struct my_struct *s) { return deref(s).a; } */
      40  	gcc_jit_param *param_get_a = gcc_jit_context_new_param(ctxt, NULL, my_struct_ptr_type, "s");
      41  	gcc_jit_function *func_get_a = gcc_jit_context_new_function(
      42  		ctxt, NULL, GCC_JIT_FUNCTION_EXPORTED,
      43  		long_type, "get_a",
      44  		1, &param_get_a,
      45  		0);
      46  	gcc_jit_block *block_get_a = gcc_jit_function_new_block(func_get_a, NULL);
      47  	gcc_jit_rvalue *argsForDeref[1] = {gcc_jit_param_as_rvalue(param_get_a)};
      48  	gcc_jit_rvalue *callDeref = gcc_jit_context_new_call(ctxt, NULL, func_deref, 1, argsForDeref);
      49  	gcc_jit_block_end_with_return(
      50  		block_get_a, NULL,
      51  		gcc_jit_rvalue_access_field(callDeref, NULL, fields[0]));
      52  }
      53  
      54  void
      55  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      56  {
      57    typedef long(*fn_type) (struct my_struct*);
      58    fn_type get_a = (fn_type) gcc_jit_result_get_code(result, "get_a");
      59  
      60    struct my_struct s = {1, 2, 3};
      61    CHECK_VALUE (get_a(&s), 1);
      62  }