Skip to content

Commit 40548c1

Browse files
committed
libgccjit: Allow sending a const pointer as argument
gcc/jit/ChangeLog: * jit-recording.h: Remove memento_of_get_const::accepts_writes_from, memento_of_get_type::is_same_type_as: new method. gcc/testsuite/ChangeLog: * jit.dg/all-non-failing-tests.h: Add test-const-pointer-argument.c. * jit.dg/test-const-pointer-argument.c: New test.
1 parent ec8ae43 commit 40548c1

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

gcc/jit/jit-recording.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,21 @@ class memento_of_get_type : public type
730730
return type::accepts_writes_from (rtype);
731731
}
732732

733+
bool is_same_type_as (type *other) final override
734+
{
735+
if (m_kind == GCC_JIT_TYPE_VOID_PTR)
736+
{
737+
if (other->is_pointer ())
738+
{
739+
/* LHS (this) is type (void *), and the RHS is a pointer:
740+
accept it: */
741+
return true;
742+
}
743+
}
744+
745+
return type::is_same_type_as (other);
746+
}
747+
733748
bool is_int () const final override;
734749
bool is_float () const final override;
735750
bool is_bool () const final override;
@@ -823,12 +838,6 @@ class memento_of_get_const : public decorated_type
823838
memento_of_get_const (type *other_type)
824839
: decorated_type (other_type) {}
825840

826-
bool accepts_writes_from (type */*rtype*/) final override
827-
{
828-
/* Can't write to a "const". */
829-
return false;
830-
}
831-
832841
type* copy (context* ctxt) final override
833842
{
834843
type* result = new memento_of_get_const (m_other_type->copy (ctxt));

gcc/testsuite/jit.dg/all-non-failing-tests.h

+10
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@
155155
#undef create_code
156156
#undef verify_code
157157

158+
/* test-const-pointer-argument.c */
159+
#define create_code create_code_const_pointer_argument
160+
#define verify_code verify_code_const_pointer_argument
161+
#include "test-const-pointer-argument.c"
162+
#undef create_code
163+
#undef verify_code
164+
158165
/* test-debug-strings.c */
159166
#define create_code create_code_debug_strings
160167
#define verify_code verify_code_debug_strings
@@ -537,6 +544,9 @@ const struct testcase testcases[] = {
537544
{"convert_vector",
538545
create_code_convert_vector,
539546
verify_code_convert_vector},
547+
{"const_pointer_argument",
548+
create_code_const_pointer_argument,
549+
verify_code_const_pointer_argument},
540550
{"debug_strings",
541551
create_code_debug_strings,
542552
verify_code_debug_strings},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <time.h>
5+
6+
#include "libgccjit.h"
7+
8+
#include "harness.h"
9+
10+
void
11+
create_code (gcc_jit_context *ctxt, void *user_data)
12+
{
13+
/* Let's try to inject the equivalent of:
14+
15+
void test_ptr(const void* value)
16+
{
17+
return;
18+
}
19+
20+
void main (void)
21+
{
22+
const void *ptr;
23+
test_ptr (ptr);
24+
return;
25+
}
26+
*/
27+
gcc_jit_type *void_type =
28+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
29+
gcc_jit_type *real_void_ptr_type =
30+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
31+
gcc_jit_type *const_real_void_ptr_type =
32+
gcc_jit_type_get_const (real_void_ptr_type);
33+
34+
/* Build the test_ptr. */
35+
gcc_jit_param *param =
36+
gcc_jit_context_new_param (ctxt, NULL, const_real_void_ptr_type, "value");
37+
gcc_jit_function *test_ptr =
38+
gcc_jit_context_new_function (ctxt, NULL,
39+
GCC_JIT_FUNCTION_EXPORTED,
40+
void_type,
41+
"test_ptr",
42+
1, &param,
43+
0);
44+
gcc_jit_block *block = gcc_jit_function_new_block (test_ptr, NULL);
45+
gcc_jit_block_end_with_void_return (block, NULL);
46+
47+
/* Build main. */
48+
gcc_jit_function *main =
49+
gcc_jit_context_new_function (ctxt, NULL,
50+
GCC_JIT_FUNCTION_EXPORTED,
51+
void_type,
52+
"main",
53+
0, NULL,
54+
0);
55+
gcc_jit_block *main_block = gcc_jit_function_new_block (main, NULL);
56+
57+
gcc_jit_type *void_ptr_type =
58+
gcc_jit_type_get_pointer (void_type);
59+
gcc_jit_type *const_void_ptr_type =
60+
gcc_jit_type_get_const (void_ptr_type);
61+
62+
gcc_jit_lvalue *pointer =
63+
gcc_jit_function_new_local (main, NULL, const_void_ptr_type, "ptr");
64+
gcc_jit_rvalue *ptr_rvalue = gcc_jit_lvalue_as_rvalue (pointer);
65+
66+
gcc_jit_block_add_eval (main_block, NULL,
67+
gcc_jit_context_new_call (ctxt, NULL, test_ptr, 1, &ptr_rvalue));
68+
gcc_jit_block_end_with_void_return (main_block, NULL);
69+
}
70+
71+
void
72+
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
73+
{
74+
CHECK_NON_NULL (result);
75+
}

0 commit comments

Comments
 (0)