]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/jit.dg/test-array-as-pointer.c
Merger of dmalcolm/jit branch from git
[thirdparty/gcc.git] / gcc / testsuite / jit.dg / test-array-as-pointer.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stddef.h>
4
5 #include "libgccjit.h"
6
7 #include "harness.h"
8
9 #define BUFFER_SIZE (1024)
10
11 char test_buffer[1024];
12
13 void
14 create_code (gcc_jit_context *ctxt, void *user_data)
15 {
16 /* Let's try to inject the equivalent of:
17 void test_of_array_as_pointer (const char *name)
18 {
19 snprintf (test_buffer, sizeof (test_buffer),
20 "hello %s", name);
21 }
22 */
23 gcc_jit_type *void_type =
24 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
25 gcc_jit_type *const_char_ptr_type =
26 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
27 gcc_jit_type *char_type =
28 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR);
29 gcc_jit_type *char_ptr_type =
30 gcc_jit_type_get_pointer (char_type);
31 gcc_jit_type *int_type =
32 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
33 gcc_jit_type *size_t_type =
34 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIZE_T);
35 gcc_jit_type *buf_type =
36 gcc_jit_context_new_array_type (ctxt, NULL, char_type, BUFFER_SIZE);
37
38 /* extern int snprintf(char *str, size_t size, const char *format, ...); */
39 gcc_jit_param *param_s =
40 gcc_jit_context_new_param (ctxt, NULL, char_ptr_type, "s");
41 gcc_jit_param *param_n =
42 gcc_jit_context_new_param (ctxt, NULL, size_t_type, "n");
43 gcc_jit_param *param_format =
44 gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
45 gcc_jit_param *snprintf_params[3] = {param_s, param_n, param_format};
46 gcc_jit_function *snprintf =
47 gcc_jit_context_new_function (ctxt, NULL,
48 GCC_JIT_FUNCTION_IMPORTED,
49 int_type,
50 "snprintf",
51 3, snprintf_params,
52 1);
53
54 gcc_jit_param *param_name =
55 gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
56 gcc_jit_function *test_fn =
57 gcc_jit_context_new_function (ctxt, NULL,
58 GCC_JIT_FUNCTION_EXPORTED,
59 void_type,
60 "test_of_array_as_pointer",
61 1, &param_name,
62 0);
63
64 gcc_jit_lvalue *buffer =
65 gcc_jit_context_new_global (ctxt, NULL, buf_type, "test_buffer");
66
67 gcc_jit_block *block = gcc_jit_function_new_block(test_fn, "entry");
68
69 /* snprintf(buffer, sizeof(buffer), "hello %s", name); */
70 gcc_jit_rvalue *args[4];
71 args[0] = gcc_jit_context_new_cast (
72 ctxt, NULL,
73 /* Here's the difference with test-error-array-as-pointer.c: */
74 gcc_jit_lvalue_get_address (buffer,
75 NULL),
76 char_ptr_type);
77 args[1] = gcc_jit_context_new_rvalue_from_int (ctxt,
78 size_t_type,
79 BUFFER_SIZE);
80 args[2] = gcc_jit_context_new_string_literal (ctxt, "hello %s");
81 args[3] = gcc_jit_param_as_rvalue (param_name);
82
83 gcc_jit_block_add_eval (
84 block, NULL,
85 gcc_jit_context_new_call (ctxt, NULL, snprintf, 4, args));
86 gcc_jit_block_end_with_void_return (block, NULL);
87 }
88
89 void
90 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
91 {
92 CHECK_NON_NULL (result);
93
94 typedef void (*fn_type) (const char *);
95 fn_type test_of_array_as_pointer =
96 (fn_type)gcc_jit_result_get_code (result, "test_of_array_as_pointer");
97 CHECK_NON_NULL (test_of_array_as_pointer);
98
99 test_of_array_as_pointer ("world");
100 CHECK_STRING_VALUE (test_buffer, "hello world");
101 }