1  #include <stdlib.h>
       2  #include <stdio.h>
       3  #include <string.h>
       4  #include <time.h>
       5  
       6  #include "libgccjit.h"
       7  
       8  #include "harness.h"
       9  
      10  void
      11  create_code (gcc_jit_context *ctxt, void *user_data)
      12  {
      13    /* Let's try to inject the equivalent of:
      14  
      15       _Thread_local int foo;
      16  
      17       int test_using_tls()
      18       {
      19        foo = 42;
      20        return foo;
      21       }
      22    */
      23    gcc_jit_type *int_type =
      24      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
      25  
      26    gcc_jit_lvalue *foo =
      27      gcc_jit_context_new_global (
      28        ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, int_type, "foo");
      29    gcc_jit_lvalue_set_tls_model (foo, GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC);
      30  
      31    /* Build the test_fn.  */
      32    gcc_jit_function *test_fn =
      33      gcc_jit_context_new_function (ctxt, NULL,
      34  				  GCC_JIT_FUNCTION_EXPORTED,
      35  				  int_type,
      36  				  "test_using_tls",
      37  				  0, NULL,
      38  				  0);
      39    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
      40    gcc_jit_block_add_assignment (
      41      block, NULL,
      42      foo,
      43      gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42));
      44    gcc_jit_block_end_with_return (block,
      45  				 NULL,
      46  				 gcc_jit_lvalue_as_rvalue (foo));
      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_tls =
      56      (fn_type)gcc_jit_result_get_code (result, "test_using_tls");
      57    CHECK_NON_NULL (test_using_tls);
      58    int return_value = test_using_tls();
      59    CHECK_VALUE (return_value, 42);
      60  
      61    int *foo = (int *)gcc_jit_result_get_global (result, "foo");
      62    CHECK_NON_NULL (foo);
      63    CHECK_VALUE (*foo, 42);
      64  }