Home | History | Annotate | Line # | Download | only in examples
      1  1.1  mrg /* Smoketest example for libgccjit.so
      2  1.7  mrg    Copyright (C) 2014-2022 Free Software Foundation, Inc.
      3  1.1  mrg 
      4  1.1  mrg This file is part of GCC.
      5  1.1  mrg 
      6  1.1  mrg GCC is free software; you can redistribute it and/or modify it
      7  1.1  mrg under the terms of the GNU General Public License as published by
      8  1.1  mrg the Free Software Foundation; either version 3, or (at your option)
      9  1.1  mrg any later version.
     10  1.1  mrg 
     11  1.1  mrg GCC is distributed in the hope that it will be useful, but
     12  1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  1.1  mrg General Public License for more details.
     15  1.1  mrg 
     16  1.1  mrg You should have received a copy of the GNU General Public License
     17  1.1  mrg along with GCC; see the file COPYING3.  If not see
     18  1.1  mrg <http://www.gnu.org/licenses/>.  */
     19  1.1  mrg 
     20  1.1  mrg #include <libgccjit.h>
     21  1.1  mrg 
     22  1.1  mrg #include <stdlib.h>
     23  1.1  mrg #include <stdio.h>
     24  1.1  mrg 
     25  1.1  mrg static void
     26  1.1  mrg create_code (gcc_jit_context *ctxt)
     27  1.1  mrg {
     28  1.1  mrg   /* Let's try to inject the equivalent of:
     29  1.1  mrg      void
     30  1.1  mrg      greet (const char *name)
     31  1.1  mrg      {
     32  1.1  mrg         printf ("hello %s\n", name);
     33  1.1  mrg      }
     34  1.1  mrg   */
     35  1.1  mrg   gcc_jit_type *void_type =
     36  1.1  mrg     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
     37  1.1  mrg   gcc_jit_type *const_char_ptr_type =
     38  1.1  mrg     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
     39  1.1  mrg   gcc_jit_param *param_name =
     40  1.1  mrg     gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
     41  1.1  mrg   gcc_jit_function *func =
     42  1.1  mrg     gcc_jit_context_new_function (ctxt, NULL,
     43  1.1  mrg                                   GCC_JIT_FUNCTION_EXPORTED,
     44  1.1  mrg                                   void_type,
     45  1.1  mrg                                   "greet",
     46  1.1  mrg                                   1, &param_name,
     47  1.1  mrg                                   0);
     48  1.1  mrg 
     49  1.1  mrg   gcc_jit_param *param_format =
     50  1.1  mrg     gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
     51  1.1  mrg   gcc_jit_function *printf_func =
     52  1.1  mrg     gcc_jit_context_new_function (ctxt, NULL,
     53  1.1  mrg 				  GCC_JIT_FUNCTION_IMPORTED,
     54  1.1  mrg 				  gcc_jit_context_get_type (
     55  1.1  mrg 				     ctxt, GCC_JIT_TYPE_INT),
     56  1.1  mrg 				  "printf",
     57  1.1  mrg 				  1, &param_format,
     58  1.1  mrg 				  1);
     59  1.1  mrg   gcc_jit_rvalue *args[2];
     60  1.1  mrg   args[0] = gcc_jit_context_new_string_literal (ctxt, "hello %s\n");
     61  1.1  mrg   args[1] = gcc_jit_param_as_rvalue (param_name);
     62  1.1  mrg 
     63  1.1  mrg   gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
     64  1.1  mrg 
     65  1.1  mrg   gcc_jit_block_add_eval (
     66  1.1  mrg     block, NULL,
     67  1.1  mrg     gcc_jit_context_new_call (ctxt,
     68  1.1  mrg                               NULL,
     69  1.1  mrg                               printf_func,
     70  1.1  mrg                               2, args));
     71  1.1  mrg   gcc_jit_block_end_with_void_return (block, NULL);
     72  1.1  mrg }
     73  1.1  mrg 
     74  1.1  mrg int
     75  1.1  mrg main (int argc, char **argv)
     76  1.1  mrg {
     77  1.1  mrg   gcc_jit_context *ctxt;
     78  1.1  mrg   gcc_jit_result *result;
     79  1.1  mrg 
     80  1.1  mrg   /* Get a "context" object for working with the library.  */
     81  1.1  mrg   ctxt = gcc_jit_context_acquire ();
     82  1.1  mrg   if (!ctxt)
     83  1.1  mrg     {
     84  1.1  mrg       fprintf (stderr, "NULL ctxt");
     85  1.1  mrg       exit (1);
     86  1.1  mrg     }
     87  1.1  mrg 
     88  1.1  mrg   /* Set some options on the context.
     89  1.1  mrg      Let's see the code being generated, in assembler form.  */
     90  1.1  mrg   gcc_jit_context_set_bool_option (
     91  1.1  mrg     ctxt,
     92  1.1  mrg     GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
     93  1.1  mrg     0);
     94  1.1  mrg 
     95  1.1  mrg   /* Populate the context.  */
     96  1.1  mrg   create_code (ctxt);
     97  1.1  mrg 
     98  1.1  mrg   /* Compile the code.  */
     99  1.1  mrg   result = gcc_jit_context_compile (ctxt);
    100  1.1  mrg   if (!result)
    101  1.1  mrg     {
    102  1.1  mrg       fprintf (stderr, "NULL result");
    103  1.1  mrg       exit (1);
    104  1.1  mrg     }
    105  1.1  mrg 
    106  1.1  mrg   /* Extract the generated code from "result".  */
    107  1.1  mrg   typedef void (*fn_type) (const char *);
    108  1.1  mrg   fn_type greet =
    109  1.1  mrg     (fn_type)gcc_jit_result_get_code (result, "greet");
    110  1.1  mrg   if (!greet)
    111  1.1  mrg     {
    112  1.1  mrg       fprintf (stderr, "NULL greet");
    113  1.1  mrg       exit (1);
    114  1.1  mrg     }
    115  1.1  mrg 
    116  1.1  mrg   /* Now call the generated function: */
    117  1.1  mrg   greet ("world");
    118  1.1  mrg   fflush (stdout);
    119  1.1  mrg 
    120  1.1  mrg   gcc_jit_context_release (ctxt);
    121  1.1  mrg   gcc_jit_result_release (result);
    122  1.1  mrg   return 0;
    123  1.1  mrg }
    124