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