2018-02-09 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / gcc / testsuite / jit.dg / test-factorial-must-tail-call.c
blobc8626115708edad5b68faa3ea0ea546de126bb59
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include "libgccjit.h"
7 #include "harness.h"
9 void
10 create_code (gcc_jit_context *ctxt, void *user_data)
12 /* Let's try to inject the equivalent of:
14 int
15 my_factorial_must_tail_call (int x)
17 if (x < 2)
18 return x;
19 else
20 return x * my_factorial_must_tail_call (x - 1);
23 and mark the call as requiring tail-call-optimization.
25 gcc_jit_type *the_type =
26 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
27 gcc_jit_type *return_type = the_type;
29 gcc_jit_param *x =
30 gcc_jit_context_new_param (ctxt, NULL, the_type, "x");
31 gcc_jit_param *params[1] = {x};
32 gcc_jit_function *func =
33 gcc_jit_context_new_function (ctxt, NULL,
34 GCC_JIT_FUNCTION_EXPORTED,
35 return_type,
36 "my_factorial_must_tail_call",
37 1, params, 0);
39 gcc_jit_block *initial =
40 gcc_jit_function_new_block (func, "initial");
41 gcc_jit_block *on_true =
42 gcc_jit_function_new_block (func, "on_true");
43 gcc_jit_block *on_false =
44 gcc_jit_function_new_block (func, "on_false");
46 /* if (x < 2) */
47 gcc_jit_block_end_with_conditional (
48 initial, NULL,
49 gcc_jit_context_new_comparison (
50 ctxt, NULL,
51 GCC_JIT_COMPARISON_LT,
52 gcc_jit_param_as_rvalue (x),
53 gcc_jit_context_new_rvalue_from_int (
54 ctxt,
55 the_type,
56 2)),
57 on_true,
58 on_false);
60 /* true branch: */
61 /* return x */
62 gcc_jit_block_end_with_return (
63 on_true,
64 NULL,
65 gcc_jit_param_as_rvalue (x));
67 /* false branch: */
68 gcc_jit_rvalue *x_minus_1 =
69 gcc_jit_context_new_binary_op (
70 ctxt, NULL,
71 GCC_JIT_BINARY_OP_MINUS, the_type,
72 gcc_jit_param_as_rvalue (x),
73 gcc_jit_context_new_rvalue_from_int (
74 ctxt,
75 the_type,
76 1));
77 /* my_factorial_must_tail_call (x - 1) */
78 gcc_jit_rvalue *call =
79 gcc_jit_context_new_call (
80 ctxt, NULL,
81 func,
82 1, &x_minus_1);
84 /* Mark the call as requiring tail-call optimization. */
85 gcc_jit_rvalue_set_bool_require_tail_call (call, 1);
87 gcc_jit_block_end_with_return (
88 on_false,
89 NULL,
90 gcc_jit_context_new_binary_op (
91 ctxt, NULL,
92 GCC_JIT_BINARY_OP_MULT, the_type,
93 gcc_jit_param_as_rvalue (x),
94 call));
97 void
98 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
100 typedef int (*my_factorial_fn_type) (int);
101 CHECK_NON_NULL (result);
102 my_factorial_fn_type my_factorial_must_tail_call =
103 (my_factorial_fn_type)gcc_jit_result_get_code (result, "my_factorial_must_tail_call");
104 CHECK_NON_NULL (my_factorial_must_tail_call);
105 int val = my_factorial_must_tail_call (10);
106 note ("my_factorial_must_tail_call returned: %d", val);
107 CHECK_VALUE (val, 3628800);