(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-using-global.c
       1  #include <stdlib.h>
       2  #include <stdio.h>
       3  
       4  #include "libgccjit.h"
       5  
       6  #include "harness.h"
       7  
       8  #ifdef __cplusplus
       9  extern "C" {
      10  #endif
      11  
      12    extern int imported_global;
      13  
      14  #ifdef __cplusplus
      15  }
      16  #endif
      17  
      18  void
      19  create_code (gcc_jit_context *ctxt, void *user_data)
      20  {
      21    /* Let's try to inject the equivalent of:
      22  
      23       int exported_global;
      24       extern int imported_global;
      25       static int internal_global;
      26  
      27       int
      28       test_using_global (void)
      29       {
      30  	exported_global += 1;
      31  	imported_global += 1;
      32  	internal_global += 1;
      33  	return internal_global;
      34       }
      35    */
      36    gcc_jit_type *int_type =
      37      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
      38  
      39    gcc_jit_lvalue *exported_global =
      40      gcc_jit_context_new_global (ctxt,
      41  				NULL,
      42  				GCC_JIT_GLOBAL_EXPORTED,
      43  				int_type,
      44  				"exported_global");
      45    gcc_jit_lvalue *imported_global =
      46      gcc_jit_context_new_global (ctxt,
      47  				NULL,
      48  				GCC_JIT_GLOBAL_IMPORTED,
      49  				int_type,
      50  				"imported_global");
      51    gcc_jit_lvalue *internal_global =
      52      gcc_jit_context_new_global (ctxt,
      53  				NULL,
      54  				GCC_JIT_GLOBAL_INTERNAL,
      55  				int_type,
      56  				"internal_global");
      57  
      58    /* Build the test_fn.  */
      59    gcc_jit_function *test_fn =
      60      gcc_jit_context_new_function (ctxt, NULL,
      61  				  GCC_JIT_FUNCTION_EXPORTED,
      62  				  int_type,
      63  				  "test_using_global",
      64  				  0, NULL,
      65  				  0);
      66    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
      67  
      68    gcc_jit_block_add_assignment_op (
      69      block, NULL,
      70      exported_global,
      71      GCC_JIT_BINARY_OP_PLUS,
      72      gcc_jit_context_one (ctxt, int_type));
      73    gcc_jit_block_add_assignment_op (
      74      block, NULL,
      75      imported_global,
      76      GCC_JIT_BINARY_OP_PLUS,
      77      gcc_jit_context_one (ctxt, int_type));
      78    gcc_jit_block_add_assignment_op (
      79      block, NULL,
      80      internal_global,
      81      GCC_JIT_BINARY_OP_PLUS,
      82      gcc_jit_context_one (ctxt, int_type));
      83    gcc_jit_block_end_with_return (block,
      84  				 NULL,
      85  				 gcc_jit_lvalue_as_rvalue (internal_global));
      86  }
      87  
      88  int imported_global;
      89  
      90  void
      91  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      92  {
      93    typedef int (*fn_type) (void);
      94    CHECK_NON_NULL (result);
      95  
      96    fn_type test_using_global =
      97      (fn_type)gcc_jit_result_get_code (result, "test_using_global");
      98    CHECK_NON_NULL (test_using_global);
      99  
     100    /* The exported global should be visible.  */
     101    int *exported_global = (int *)gcc_jit_result_get_global (result, "exported_global");
     102    CHECK_NON_NULL (exported_global);
     103    /* ...and should be zero-initialized.  */
     104    CHECK_VALUE (*exported_global, 0);
     105  
     106    /* Set some nonzero values.  */
     107    *exported_global = 11;
     108    imported_global = 42;
     109  
     110    /* The internal global shouldn't be visible.  */
     111    int *internal_global = (int *)gcc_jit_result_get_global (result, "internal_global");
     112    CHECK_VALUE (internal_global, NULL);
     113  
     114    /* Call the JIT-generated function.  */
     115    int call_count = test_using_global ();
     116  
     117    /* Verify that it correctly modified imported_global and exported_global.  */
     118    CHECK_VALUE (*exported_global, 12);
     119    CHECK_VALUE (imported_global, 43);
     120    CHECK_VALUE (call_count, 1);
     121  
     122    /* Try calling it again.  */
     123    call_count = test_using_global ();
     124  
     125    /* Verify the new values.  */
     126    CHECK_VALUE (*exported_global, 13);
     127    CHECK_VALUE (imported_global, 44);
     128    CHECK_VALUE (call_count, 2);
     129  }
     130