(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-compile-to-dynamic-library.c
       1  #include <stdlib.h>
       2  #include <stdio.h>
       3  
       4  #include "libgccjit.h"
       5  
       6  #define TEST_COMPILING_TO_FILE
       7  #define OUTPUT_KIND      GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY
       8  #define OUTPUT_FILENAME  "output-of-test-compile-to-dynamic-library.c.so"
       9  #include "harness.h"
      10  
      11  void
      12  create_code (gcc_jit_context *ctxt, void *user_data)
      13  {
      14    /* Let's try to inject the equivalent of:
      15       void
      16       hello_world (const char *name)
      17       {
      18         // a test comment
      19         printf ("hello from %s\n", name);
      20       }
      21    */
      22    gcc_jit_type *void_type =
      23      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
      24    gcc_jit_type *const_char_ptr_type =
      25      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
      26    gcc_jit_param *param_name =
      27      gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
      28    gcc_jit_function *func =
      29      gcc_jit_context_new_function (ctxt, NULL,
      30  				  GCC_JIT_FUNCTION_EXPORTED,
      31  				  void_type,
      32  				  "hello_world",
      33  				  1, &param_name,
      34  				  0);
      35  
      36    gcc_jit_param *param_format =
      37      gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
      38    gcc_jit_function *printf_func =
      39      gcc_jit_context_new_function (ctxt, NULL,
      40  				  GCC_JIT_FUNCTION_IMPORTED,
      41  				  gcc_jit_context_get_type (
      42  				     ctxt, GCC_JIT_TYPE_INT),
      43  				  "printf",
      44  				  1, &param_format,
      45  				  1);
      46    gcc_jit_rvalue *args[2];
      47    args[0] = gcc_jit_context_new_string_literal (ctxt, "hello from %s\n");
      48    args[1] = gcc_jit_param_as_rvalue (param_name);
      49  
      50    gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
      51  
      52    gcc_jit_block_add_comment (
      53      block, NULL,
      54      "a test comment");
      55  
      56    gcc_jit_block_add_eval (
      57      block, NULL,
      58      gcc_jit_context_new_call (ctxt,
      59  			      NULL,
      60  			      printf_func,
      61  			      2, args));
      62    gcc_jit_block_end_with_void_return (block, NULL);
      63  }
      64  
      65  /* { dg-final { jit-verify-output-file-was-created "" } } */
      66  /* { dg-final { jit-verify-dynamic-library "hello from ./verify-dynamic-library.c.exe" } } */