Skip to content

Commit

Permalink
libgccjit: Allow sending a const pointer as argument
Browse files Browse the repository at this point in the history
gcc/jit/ChangeLog:

	* jit-recording.h: Remove memento_of_get_const::accepts_writes_from.

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.
  • Loading branch information
antoyo committed Oct 16, 2024
1 parent 0fa5017 commit 7a57f72
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
6 changes: 0 additions & 6 deletions gcc/jit/jit-recording.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,6 @@ class memento_of_get_const : public decorated_type
memento_of_get_const (type *other_type)
: decorated_type (other_type) {}

bool accepts_writes_from (type */*rtype*/) final override
{
/* Can't write to a "const". */
return false;
}

/* Strip off the "const", giving the underlying type. */
type *unqualified () final override { return m_other_type; }

Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/jit.dg/all-non-failing-tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@
/* test-const-attribute.c: This can't be in the testcases array as it needs
the `-O3` flag. */

/* test-const-pointer-argument.c */
#define create_code create_code_const_pointer_argument
#define verify_code verify_code_const_pointer_argument
#include "test-const-pointer-argument.c"
#undef create_code
#undef verify_code

/* test-debug-strings.c */
#define create_code create_code_debug_strings
#define verify_code verify_code_debug_strings
Expand Down Expand Up @@ -501,6 +508,9 @@ const struct testcase testcases[] = {
{"constants",
create_code_constants,
verify_code_constants},
{"const_pointer_argument",
create_code_const_pointer_argument,
verify_code_const_pointer_argument},
{"debug_strings",
create_code_debug_strings,
verify_code_debug_strings},
Expand Down
76 changes: 76 additions & 0 deletions gcc/testsuite/jit.dg/test-const-pointer-argument.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "libgccjit.h"

#include "harness.h"

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
/* Let's try to inject the equivalent of:
int test_ptr(const int* value)
{
return *foo;
}
int main (void)
{
int value = 10;
const int *ptr = &value;
int res = test_ptr (ptr);
return res;
}
*/
gcc_jit_type *int_type =
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
gcc_jit_type *int_ptr_type =
gcc_jit_type_get_pointer (int_type);
gcc_jit_type *const_ptr_type =
gcc_jit_type_get_const (int_ptr_type);

/* Build the test_ptr. */
gcc_jit_param *param =
gcc_jit_context_new_param (ctxt, NULL, const_ptr_type, "value");
gcc_jit_function *test_ptr =
gcc_jit_context_new_function (ctxt, NULL,
GCC_JIT_FUNCTION_EXPORTED,
int_type,
"test_ptr",
1, &param,
0);
gcc_jit_block *block = gcc_jit_function_new_block (test_ptr, NULL);
gcc_jit_block_end_with_return (block,
NULL,
gcc_jit_lvalue_as_rvalue (
gcc_jit_rvalue_dereference (gcc_jit_param_as_rvalue (param), NULL)));

/* Build main. */
gcc_jit_function *main =
gcc_jit_context_new_function (ctxt, NULL,
GCC_JIT_FUNCTION_EXPORTED,
int_type,
"main",
0, NULL,
0);
gcc_jit_block *main_block = gcc_jit_function_new_block (main, NULL);
gcc_jit_lvalue *variable =
gcc_jit_function_new_local (main, NULL, int_type, "value");
gcc_jit_lvalue *pointer =
gcc_jit_function_new_local (main, NULL, const_ptr_type, "ptr");
gcc_jit_block_add_assignment (main_block, NULL, pointer,
gcc_jit_lvalue_get_address (variable, NULL));
gcc_jit_rvalue *ptr_rvalue = gcc_jit_lvalue_as_rvalue (pointer);
gcc_jit_rvalue *res =
gcc_jit_context_new_call (ctxt, NULL, test_ptr, 1, &ptr_rvalue);
gcc_jit_block_end_with_return (main_block, NULL, res);
}

void
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
{
CHECK_NON_NULL (result);
}

0 comments on commit 7a57f72

Please sign in to comment.