xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/jit/docs/examples/tut02-square.cc (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg /* Usage example for libgccjit.so's C++ API
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(gccjit::context ctxt)2636ac495dSmrg create_code (gccjit::context ctxt)
2736ac495dSmrg {
2836ac495dSmrg   /* Let's try to inject the equivalent of this C code:
2936ac495dSmrg 
3036ac495dSmrg       int square (int i)
3136ac495dSmrg       {
3236ac495dSmrg         return i * i;
3336ac495dSmrg       }
3436ac495dSmrg   */
3536ac495dSmrg   gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT);
3636ac495dSmrg   gccjit::param param_i = ctxt.new_param (int_type, "i");
3736ac495dSmrg   std::vector<gccjit::param> params;
3836ac495dSmrg   params.push_back (param_i);
3936ac495dSmrg   gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
4036ac495dSmrg                                              int_type,
4136ac495dSmrg                                              "square",
4236ac495dSmrg                                              params, 0);
4336ac495dSmrg 
4436ac495dSmrg   gccjit::block block = func.new_block ();
4536ac495dSmrg 
4636ac495dSmrg   gccjit::rvalue expr =
4736ac495dSmrg     ctxt.new_binary_op (GCC_JIT_BINARY_OP_MULT, int_type,
4836ac495dSmrg                         param_i, param_i);
4936ac495dSmrg 
5036ac495dSmrg   block.end_with_return (expr);
5136ac495dSmrg }
5236ac495dSmrg 
5336ac495dSmrg int
main(int argc,char ** argv)5436ac495dSmrg main (int argc, char **argv)
5536ac495dSmrg {
5636ac495dSmrg   /* Get a "context" object for working with the library.  */
5736ac495dSmrg   gccjit::context ctxt = gccjit::context::acquire ();
5836ac495dSmrg 
5936ac495dSmrg   /* Set some options on the context.
6036ac495dSmrg      Turn this on to see the code being generated, in assembler form.  */
6136ac495dSmrg   ctxt.set_bool_option (
6236ac495dSmrg     GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
6336ac495dSmrg     0);
6436ac495dSmrg 
6536ac495dSmrg   /* Populate the context.  */
6636ac495dSmrg   create_code (ctxt);
6736ac495dSmrg 
6836ac495dSmrg   /* Compile the code.  */
6936ac495dSmrg   gcc_jit_result *result = ctxt.compile ();
7036ac495dSmrg 
7136ac495dSmrg   /* We're done with the context; we can release it: */
7236ac495dSmrg   ctxt.release ();
7336ac495dSmrg 
7436ac495dSmrg   if (!result)
7536ac495dSmrg     {
7636ac495dSmrg       fprintf (stderr, "NULL result");
7736ac495dSmrg       return 1;
7836ac495dSmrg     }
7936ac495dSmrg 
8036ac495dSmrg   /* Extract the generated code from "result".  */
8136ac495dSmrg   void *fn_ptr = gcc_jit_result_get_code (result, "square");
8236ac495dSmrg   if (!fn_ptr)
8336ac495dSmrg      {
8436ac495dSmrg        fprintf (stderr, "NULL fn_ptr");
8536ac495dSmrg        gcc_jit_result_release (result);
8636ac495dSmrg        return 1;
8736ac495dSmrg      }
8836ac495dSmrg 
8936ac495dSmrg   typedef int (*fn_type) (int);
9036ac495dSmrg   fn_type square = (fn_type)fn_ptr;
9136ac495dSmrg   printf ("result: %d\n", square (5));
9236ac495dSmrg 
9336ac495dSmrg   gcc_jit_result_release (result);
9436ac495dSmrg   return 0;
9536ac495dSmrg }
96