xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/jit/docs/examples/tut02-square.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg /* Usage example for libgccjit.so
2*8feb0f0bSmrg    Copyright (C) 2014-2020 Free Software Foundation, Inc.
336ac495dSmrg 
436ac495dSmrg This file is part of GCC.
536ac495dSmrg 
636ac495dSmrg GCC is free software; you can redistribute it and/or modify it
736ac495dSmrg under the terms of the GNU General Public License as published by
836ac495dSmrg the Free Software Foundation; either version 3, or (at your option)
936ac495dSmrg any later version.
1036ac495dSmrg 
1136ac495dSmrg GCC is distributed in the hope that it will be useful, but
1236ac495dSmrg WITHOUT ANY WARRANTY; without even the implied warranty of
1336ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1436ac495dSmrg General Public License for more details.
1536ac495dSmrg 
1636ac495dSmrg You should have received a copy of the GNU General Public License
1736ac495dSmrg along with GCC; see the file COPYING3.  If not see
1836ac495dSmrg <http://www.gnu.org/licenses/>.  */
1936ac495dSmrg 
2036ac495dSmrg #include <libgccjit.h>
2136ac495dSmrg 
2236ac495dSmrg #include <stdlib.h>
2336ac495dSmrg #include <stdio.h>
2436ac495dSmrg 
2536ac495dSmrg void
create_code(gcc_jit_context * ctxt)2636ac495dSmrg create_code (gcc_jit_context *ctxt)
2736ac495dSmrg {
2836ac495dSmrg   /* Let's try to inject the equivalent of:
2936ac495dSmrg 
3036ac495dSmrg       int square (int i)
3136ac495dSmrg       {
3236ac495dSmrg         return i * i;
3336ac495dSmrg       }
3436ac495dSmrg   */
3536ac495dSmrg   gcc_jit_type *int_type =
3636ac495dSmrg     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
3736ac495dSmrg   gcc_jit_param *param_i =
3836ac495dSmrg     gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
3936ac495dSmrg   gcc_jit_function *func =
4036ac495dSmrg     gcc_jit_context_new_function (ctxt, NULL,
4136ac495dSmrg                                   GCC_JIT_FUNCTION_EXPORTED,
4236ac495dSmrg                                   int_type,
4336ac495dSmrg                                   "square",
4436ac495dSmrg                                   1, &param_i,
4536ac495dSmrg                                   0);
4636ac495dSmrg 
4736ac495dSmrg   gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
4836ac495dSmrg 
4936ac495dSmrg   gcc_jit_rvalue *expr =
5036ac495dSmrg     gcc_jit_context_new_binary_op (
5136ac495dSmrg       ctxt, NULL,
5236ac495dSmrg       GCC_JIT_BINARY_OP_MULT, int_type,
5336ac495dSmrg       gcc_jit_param_as_rvalue (param_i),
5436ac495dSmrg       gcc_jit_param_as_rvalue (param_i));
5536ac495dSmrg 
5636ac495dSmrg    gcc_jit_block_end_with_return (block, NULL, expr);
5736ac495dSmrg }
5836ac495dSmrg 
5936ac495dSmrg int
main(int argc,char ** argv)6036ac495dSmrg main (int argc, char **argv)
6136ac495dSmrg {
6236ac495dSmrg   gcc_jit_context *ctxt = NULL;
6336ac495dSmrg   gcc_jit_result *result = NULL;
6436ac495dSmrg 
6536ac495dSmrg   /* Get a "context" object for working with the library.  */
6636ac495dSmrg   ctxt = gcc_jit_context_acquire ();
6736ac495dSmrg   if (!ctxt)
6836ac495dSmrg     {
6936ac495dSmrg       fprintf (stderr, "NULL ctxt");
7036ac495dSmrg       goto error;
7136ac495dSmrg     }
7236ac495dSmrg 
7336ac495dSmrg   /* Set some options on the context.
7436ac495dSmrg      Let's see the code being generated, in assembler form.  */
7536ac495dSmrg   gcc_jit_context_set_bool_option (
7636ac495dSmrg     ctxt,
7736ac495dSmrg     GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
7836ac495dSmrg     0);
7936ac495dSmrg 
8036ac495dSmrg   /* Populate the context.  */
8136ac495dSmrg   create_code (ctxt);
8236ac495dSmrg 
8336ac495dSmrg   /* Compile the code.  */
8436ac495dSmrg   result = gcc_jit_context_compile (ctxt);
8536ac495dSmrg   if (!result)
8636ac495dSmrg     {
8736ac495dSmrg       fprintf (stderr, "NULL result");
8836ac495dSmrg       goto error;
8936ac495dSmrg     }
9036ac495dSmrg 
9136ac495dSmrg   /* We're done with the context; we can release it: */
9236ac495dSmrg   gcc_jit_context_release (ctxt);
9336ac495dSmrg   ctxt = NULL;
9436ac495dSmrg 
9536ac495dSmrg   /* Extract the generated code from "result".  */
9636ac495dSmrg   void *fn_ptr = gcc_jit_result_get_code (result, "square");
9736ac495dSmrg   if (!fn_ptr)
9836ac495dSmrg      {
9936ac495dSmrg        fprintf (stderr, "NULL fn_ptr");
10036ac495dSmrg        goto error;
10136ac495dSmrg      }
10236ac495dSmrg 
10336ac495dSmrg   typedef int (*fn_type) (int);
10436ac495dSmrg   fn_type square = (fn_type)fn_ptr;
10536ac495dSmrg   printf ("result: %d\n", square (5));
10636ac495dSmrg 
10736ac495dSmrg  error:
10836ac495dSmrg   if (ctxt)
10936ac495dSmrg     gcc_jit_context_release (ctxt);
11036ac495dSmrg   if (result)
11136ac495dSmrg     gcc_jit_result_release (result);
11236ac495dSmrg   return 0;
11336ac495dSmrg }
114