136ac495dSmrg /* Smoketest example for libgccjit.so 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 static 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 void
3036ac495dSmrg greet (const char *name)
3136ac495dSmrg {
3236ac495dSmrg printf ("hello %s\n", name);
3336ac495dSmrg }
3436ac495dSmrg */
3536ac495dSmrg gccjit::type void_type = ctxt.get_type (GCC_JIT_TYPE_VOID);
3636ac495dSmrg gccjit::type const_char_ptr_type =
3736ac495dSmrg ctxt.get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
3836ac495dSmrg gccjit::param param_name =
3936ac495dSmrg ctxt.new_param (const_char_ptr_type, "name");
4036ac495dSmrg std::vector<gccjit::param> func_params;
4136ac495dSmrg func_params.push_back (param_name);
4236ac495dSmrg gccjit::function func =
4336ac495dSmrg ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
4436ac495dSmrg void_type,
4536ac495dSmrg "greet",
4636ac495dSmrg func_params, 0);
4736ac495dSmrg
4836ac495dSmrg gccjit::param param_format =
4936ac495dSmrg ctxt.new_param (const_char_ptr_type, "format");
5036ac495dSmrg std::vector<gccjit::param> printf_params;
5136ac495dSmrg printf_params.push_back (param_format);
5236ac495dSmrg gccjit::function printf_func =
5336ac495dSmrg ctxt.new_function (GCC_JIT_FUNCTION_IMPORTED,
5436ac495dSmrg ctxt.get_type (GCC_JIT_TYPE_INT),
5536ac495dSmrg "printf",
5636ac495dSmrg printf_params, 1);
5736ac495dSmrg
5836ac495dSmrg gccjit::block block = func.new_block ();
5936ac495dSmrg block.add_eval (ctxt.new_call (printf_func,
6036ac495dSmrg ctxt.new_rvalue ("hello %s\n"),
6136ac495dSmrg param_name));
6236ac495dSmrg block.end_with_return ();
6336ac495dSmrg }
6436ac495dSmrg
6536ac495dSmrg int
main(int argc,char ** argv)6636ac495dSmrg main (int argc, char **argv)
6736ac495dSmrg {
6836ac495dSmrg gccjit::context ctxt;
6936ac495dSmrg gcc_jit_result *result;
7036ac495dSmrg
7136ac495dSmrg /* Get a "context" object for working with the library. */
7236ac495dSmrg ctxt = gccjit::context::acquire ();
7336ac495dSmrg
7436ac495dSmrg /* Set some options on the context.
7536ac495dSmrg Turn this on to see the code being generated, in assembler form. */
7636ac495dSmrg ctxt.set_bool_option (GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE, 0);
7736ac495dSmrg
7836ac495dSmrg /* Populate the context. */
7936ac495dSmrg create_code (ctxt);
8036ac495dSmrg
8136ac495dSmrg /* Compile the code. */
8236ac495dSmrg result = ctxt.compile ();
8336ac495dSmrg if (!result)
8436ac495dSmrg {
8536ac495dSmrg fprintf (stderr, "NULL result");
8636ac495dSmrg exit (1);
8736ac495dSmrg }
8836ac495dSmrg
8936ac495dSmrg ctxt.release ();
9036ac495dSmrg
9136ac495dSmrg /* Extract the generated code from "result". */
9236ac495dSmrg typedef void (*fn_type) (const char *);
9336ac495dSmrg fn_type greet =
9436ac495dSmrg (fn_type)gcc_jit_result_get_code (result, "greet");
9536ac495dSmrg if (!greet)
9636ac495dSmrg {
9736ac495dSmrg fprintf (stderr, "NULL greet");
9836ac495dSmrg exit (1);
9936ac495dSmrg }
10036ac495dSmrg
10136ac495dSmrg /* Now call the generated function: */
10236ac495dSmrg greet ("world");
10336ac495dSmrg fflush (stdout);
10436ac495dSmrg
10536ac495dSmrg gcc_jit_result_release (result);
10636ac495dSmrg return 0;
10736ac495dSmrg }
108