xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/jit/docs/examples/tut02-square.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1 /* Usage example for libgccjit.so
2    Copyright (C) 2014-2020 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 
11 GCC is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include <libgccjit.h>
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 
25 void
create_code(gcc_jit_context * ctxt)26 create_code (gcc_jit_context *ctxt)
27 {
28   /* Let's try to inject the equivalent of:
29 
30       int square (int i)
31       {
32         return i * i;
33       }
34   */
35   gcc_jit_type *int_type =
36     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
37   gcc_jit_param *param_i =
38     gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
39   gcc_jit_function *func =
40     gcc_jit_context_new_function (ctxt, NULL,
41                                   GCC_JIT_FUNCTION_EXPORTED,
42                                   int_type,
43                                   "square",
44                                   1, &param_i,
45                                   0);
46 
47   gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
48 
49   gcc_jit_rvalue *expr =
50     gcc_jit_context_new_binary_op (
51       ctxt, NULL,
52       GCC_JIT_BINARY_OP_MULT, int_type,
53       gcc_jit_param_as_rvalue (param_i),
54       gcc_jit_param_as_rvalue (param_i));
55 
56    gcc_jit_block_end_with_return (block, NULL, expr);
57 }
58 
59 int
main(int argc,char ** argv)60 main (int argc, char **argv)
61 {
62   gcc_jit_context *ctxt = NULL;
63   gcc_jit_result *result = NULL;
64 
65   /* Get a "context" object for working with the library.  */
66   ctxt = gcc_jit_context_acquire ();
67   if (!ctxt)
68     {
69       fprintf (stderr, "NULL ctxt");
70       goto error;
71     }
72 
73   /* Set some options on the context.
74      Let's see the code being generated, in assembler form.  */
75   gcc_jit_context_set_bool_option (
76     ctxt,
77     GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
78     0);
79 
80   /* Populate the context.  */
81   create_code (ctxt);
82 
83   /* Compile the code.  */
84   result = gcc_jit_context_compile (ctxt);
85   if (!result)
86     {
87       fprintf (stderr, "NULL result");
88       goto error;
89     }
90 
91   /* We're done with the context; we can release it: */
92   gcc_jit_context_release (ctxt);
93   ctxt = NULL;
94 
95   /* Extract the generated code from "result".  */
96   void *fn_ptr = gcc_jit_result_get_code (result, "square");
97   if (!fn_ptr)
98      {
99        fprintf (stderr, "NULL fn_ptr");
100        goto error;
101      }
102 
103   typedef int (*fn_type) (int);
104   fn_type square = (fn_type)fn_ptr;
105   printf ("result: %d\n", square (5));
106 
107  error:
108   if (ctxt)
109     gcc_jit_context_release (ctxt);
110   if (result)
111     gcc_jit_result_release (result);
112   return 0;
113 }
114