(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-volatile.c
       1  #include <stdlib.h>
       2  #include <stdio.h>
       3  
       4  #include "libgccjit.h"
       5  
       6  #include "harness.h"
       7  
       8  volatile int the_volatile;
       9  
      10  void
      11  create_code (gcc_jit_context *ctxt, void *user_data)
      12  {
      13    /* Let's try to inject the equivalent of:
      14       extern volatile int the_volatile;
      15  
      16       int
      17       test_using_volatile (void)
      18       {
      19  	return the_volatile;
      20       }
      21    */
      22    gcc_jit_type *int_type =
      23      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
      24    gcc_jit_type *volatile_int_type =
      25      gcc_jit_type_get_volatile (int_type);
      26  
      27    /* Build the test_fn.  */
      28    gcc_jit_function *test_fn =
      29      gcc_jit_context_new_function (ctxt, NULL,
      30  				  GCC_JIT_FUNCTION_EXPORTED,
      31  				  int_type,
      32  				  "test_using_volatile",
      33  				  0, NULL,
      34  				  0);
      35    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
      36    gcc_jit_lvalue *read_of_the_volatile =
      37      gcc_jit_rvalue_dereference (
      38        gcc_jit_context_new_rvalue_from_ptr (
      39          ctxt,
      40  	gcc_jit_type_get_pointer (volatile_int_type),
      41  	(void *)&the_volatile),
      42        NULL);
      43  
      44    gcc_jit_block_end_with_return (
      45      block, NULL,
      46      gcc_jit_lvalue_as_rvalue (read_of_the_volatile));
      47  }
      48  
      49  void
      50  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      51  {
      52    typedef int (*fn_type) (void);
      53    CHECK_NON_NULL (result);
      54  
      55    fn_type test_using_volatile =
      56      (fn_type)gcc_jit_result_get_code (result, "test_using_volatile");
      57    CHECK_NON_NULL (test_using_volatile);
      58  
      59    the_volatile = 42;
      60  
      61    /* Call the JIT-generated function.  */
      62    test_using_volatile ();
      63  
      64    CHECK_VALUE (test_using_volatile (), 42);
      65  }
      66