xref: /netbsd-src/external/gpl3/gcc/dist/gcc/jit/docs/examples/tut01-hello-world.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14d5abbe8Smrg /* Smoketest example for libgccjit.so
2*b1e83836Smrg    Copyright (C) 2014-2022 Free Software Foundation, Inc.
34d5abbe8Smrg 
44d5abbe8Smrg This file is part of GCC.
54d5abbe8Smrg 
64d5abbe8Smrg GCC is free software; you can redistribute it and/or modify it
74d5abbe8Smrg under the terms of the GNU General Public License as published by
84d5abbe8Smrg the Free Software Foundation; either version 3, or (at your option)
94d5abbe8Smrg any later version.
104d5abbe8Smrg 
114d5abbe8Smrg GCC is distributed in the hope that it will be useful, but
124d5abbe8Smrg WITHOUT ANY WARRANTY; without even the implied warranty of
134d5abbe8Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
144d5abbe8Smrg General Public License for more details.
154d5abbe8Smrg 
164d5abbe8Smrg You should have received a copy of the GNU General Public License
174d5abbe8Smrg along with GCC; see the file COPYING3.  If not see
184d5abbe8Smrg <http://www.gnu.org/licenses/>.  */
194d5abbe8Smrg 
204d5abbe8Smrg #include <libgccjit.h>
214d5abbe8Smrg 
224d5abbe8Smrg #include <stdlib.h>
234d5abbe8Smrg #include <stdio.h>
244d5abbe8Smrg 
254d5abbe8Smrg static void
create_code(gcc_jit_context * ctxt)264d5abbe8Smrg create_code (gcc_jit_context *ctxt)
274d5abbe8Smrg {
284d5abbe8Smrg   /* Let's try to inject the equivalent of:
294d5abbe8Smrg      void
304d5abbe8Smrg      greet (const char *name)
314d5abbe8Smrg      {
324d5abbe8Smrg         printf ("hello %s\n", name);
334d5abbe8Smrg      }
344d5abbe8Smrg   */
354d5abbe8Smrg   gcc_jit_type *void_type =
364d5abbe8Smrg     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
374d5abbe8Smrg   gcc_jit_type *const_char_ptr_type =
384d5abbe8Smrg     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
394d5abbe8Smrg   gcc_jit_param *param_name =
404d5abbe8Smrg     gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
414d5abbe8Smrg   gcc_jit_function *func =
424d5abbe8Smrg     gcc_jit_context_new_function (ctxt, NULL,
434d5abbe8Smrg                                   GCC_JIT_FUNCTION_EXPORTED,
444d5abbe8Smrg                                   void_type,
454d5abbe8Smrg                                   "greet",
464d5abbe8Smrg                                   1, &param_name,
474d5abbe8Smrg                                   0);
484d5abbe8Smrg 
494d5abbe8Smrg   gcc_jit_param *param_format =
504d5abbe8Smrg     gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
514d5abbe8Smrg   gcc_jit_function *printf_func =
524d5abbe8Smrg     gcc_jit_context_new_function (ctxt, NULL,
534d5abbe8Smrg 				  GCC_JIT_FUNCTION_IMPORTED,
544d5abbe8Smrg 				  gcc_jit_context_get_type (
554d5abbe8Smrg 				     ctxt, GCC_JIT_TYPE_INT),
564d5abbe8Smrg 				  "printf",
574d5abbe8Smrg 				  1, &param_format,
584d5abbe8Smrg 				  1);
594d5abbe8Smrg   gcc_jit_rvalue *args[2];
604d5abbe8Smrg   args[0] = gcc_jit_context_new_string_literal (ctxt, "hello %s\n");
614d5abbe8Smrg   args[1] = gcc_jit_param_as_rvalue (param_name);
624d5abbe8Smrg 
634d5abbe8Smrg   gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
644d5abbe8Smrg 
654d5abbe8Smrg   gcc_jit_block_add_eval (
664d5abbe8Smrg     block, NULL,
674d5abbe8Smrg     gcc_jit_context_new_call (ctxt,
684d5abbe8Smrg                               NULL,
694d5abbe8Smrg                               printf_func,
704d5abbe8Smrg                               2, args));
714d5abbe8Smrg   gcc_jit_block_end_with_void_return (block, NULL);
724d5abbe8Smrg }
734d5abbe8Smrg 
744d5abbe8Smrg int
main(int argc,char ** argv)754d5abbe8Smrg main (int argc, char **argv)
764d5abbe8Smrg {
774d5abbe8Smrg   gcc_jit_context *ctxt;
784d5abbe8Smrg   gcc_jit_result *result;
794d5abbe8Smrg 
804d5abbe8Smrg   /* Get a "context" object for working with the library.  */
814d5abbe8Smrg   ctxt = gcc_jit_context_acquire ();
824d5abbe8Smrg   if (!ctxt)
834d5abbe8Smrg     {
844d5abbe8Smrg       fprintf (stderr, "NULL ctxt");
854d5abbe8Smrg       exit (1);
864d5abbe8Smrg     }
874d5abbe8Smrg 
884d5abbe8Smrg   /* Set some options on the context.
894d5abbe8Smrg      Let's see the code being generated, in assembler form.  */
904d5abbe8Smrg   gcc_jit_context_set_bool_option (
914d5abbe8Smrg     ctxt,
924d5abbe8Smrg     GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
934d5abbe8Smrg     0);
944d5abbe8Smrg 
954d5abbe8Smrg   /* Populate the context.  */
964d5abbe8Smrg   create_code (ctxt);
974d5abbe8Smrg 
984d5abbe8Smrg   /* Compile the code.  */
994d5abbe8Smrg   result = gcc_jit_context_compile (ctxt);
1004d5abbe8Smrg   if (!result)
1014d5abbe8Smrg     {
1024d5abbe8Smrg       fprintf (stderr, "NULL result");
1034d5abbe8Smrg       exit (1);
1044d5abbe8Smrg     }
1054d5abbe8Smrg 
1064d5abbe8Smrg   /* Extract the generated code from "result".  */
1074d5abbe8Smrg   typedef void (*fn_type) (const char *);
1084d5abbe8Smrg   fn_type greet =
1094d5abbe8Smrg     (fn_type)gcc_jit_result_get_code (result, "greet");
1104d5abbe8Smrg   if (!greet)
1114d5abbe8Smrg     {
1124d5abbe8Smrg       fprintf (stderr, "NULL greet");
1134d5abbe8Smrg       exit (1);
1144d5abbe8Smrg     }
1154d5abbe8Smrg 
1164d5abbe8Smrg   /* Now call the generated function: */
1174d5abbe8Smrg   greet ("world");
1184d5abbe8Smrg   fflush (stdout);
1194d5abbe8Smrg 
1204d5abbe8Smrg   gcc_jit_context_release (ctxt);
1214d5abbe8Smrg   gcc_jit_result_release (result);
1224d5abbe8Smrg   return 0;
1234d5abbe8Smrg }
124