1/* Usage example for libgccjit.so's C++ API
2   Copyright (C) 2014-2015 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 3, or (at your option)
9any later version.
10
11GCC is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
25void
26create_code (gccjit::context ctxt)
27{
28  /*
29    Simple sum-of-squares, to test conditionals and looping
30
31    int loop_test (int n)
32    {
33      int i;
34      int sum = 0;
35      for (i = 0; i < n ; i ++)
36      {
37	sum += i * i;
38      }
39      return sum;
40   */
41  gccjit::type the_type = ctxt.get_int_type <int> ();
42  gccjit::type return_type = the_type;
43
44  gccjit::param n = ctxt.new_param (the_type, "n");
45  std::vector<gccjit::param> params;
46  params.push_back (n);
47  gccjit::function func =
48    ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
49                       return_type,
50                       "loop_test",
51                       params, 0);
52
53  /* Build locals:  */
54  gccjit::lvalue i = func.new_local (the_type, "i");
55  gccjit::lvalue sum = func.new_local (the_type, "sum");
56
57  gccjit::block b_initial = func.new_block ("initial");
58  gccjit::block b_loop_cond = func.new_block ("loop_cond");
59  gccjit::block b_loop_body = func.new_block ("loop_body");
60  gccjit::block b_after_loop = func.new_block ("after_loop");
61
62  /* sum = 0; */
63  b_initial.add_assignment (sum, ctxt.zero (the_type));
64
65  /* i = 0; */
66  b_initial.add_assignment (i, ctxt.zero (the_type));
67
68  b_initial.end_with_jump (b_loop_cond);
69
70  /* if (i >= n) */
71  b_loop_cond.end_with_conditional (
72    i >= n,
73    b_after_loop,
74    b_loop_body);
75
76  /* sum += i * i */
77  b_loop_body.add_assignment_op (sum,
78                                 GCC_JIT_BINARY_OP_PLUS,
79                                 i * i);
80
81  /* i++ */
82  b_loop_body.add_assignment_op (i,
83                                GCC_JIT_BINARY_OP_PLUS,
84                                ctxt.one (the_type));
85
86  b_loop_body.end_with_jump (b_loop_cond);
87
88  /* return sum */
89  b_after_loop.end_with_return (sum);
90}
91
92int
93main (int argc, char **argv)
94{
95  gccjit::context ctxt;
96  gcc_jit_result *result = NULL;
97
98  /* Get a "context" object for working with the library.  */
99  ctxt = gccjit::context::acquire ();
100
101  /* Set some options on the context.
102     Turn this on to see the code being generated, in assembler form.  */
103  ctxt.set_bool_option (GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
104                        0);
105
106  /* Populate the context.  */
107  create_code (ctxt);
108
109  /* Compile the code.  */
110  result = ctxt.compile ();
111
112  ctxt.release ();
113
114  if (!result)
115    {
116      fprintf (stderr, "NULL result");
117      return 1;
118    }
119
120  /* Extract the generated code from "result".  */
121  typedef int (*loop_test_fn_type) (int);
122  loop_test_fn_type loop_test =
123    (loop_test_fn_type)gcc_jit_result_get_code (result, "loop_test");
124  if (!loop_test)
125    {
126      fprintf (stderr, "NULL loop_test");
127      gcc_jit_result_release (result);
128      return 1;
129    }
130
131  /* Run the generated code.  */
132  int val = loop_test (10);
133  printf("loop_test returned: %d\n", val);
134
135  gcc_jit_result_release (result);
136  return 0;
137}
138