return result;
}
+/* Build an array of type names for use by get_string_for_type_id. */
+
+static const char * const type_names[] = {
+#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) #ENUM,
+#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) #ENUM,
+#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) #ENUM,
+#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
+#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
+#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
+#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) #ENUM,
+#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6, ARG7) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6, ARG7, ARG8) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6, ARG7, ARG8, ARG9) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6, ARG7, ARG8, ARG9, ARG10) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6) \
+ #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+ ARG6, ARG7) \
+ #ENUM,
+#define DEF_POINTER_TYPE(ENUM, TYPE) #ENUM,
+
+#include "builtin-types.def"
+
+#undef DEF_PRIMITIVE_TYPE
+#undef DEF_FUNCTION_TYPE_0
+#undef DEF_FUNCTION_TYPE_1
+#undef DEF_FUNCTION_TYPE_2
+#undef DEF_FUNCTION_TYPE_3
+#undef DEF_FUNCTION_TYPE_4
+#undef DEF_FUNCTION_TYPE_5
+#undef DEF_FUNCTION_TYPE_6
+#undef DEF_FUNCTION_TYPE_7
+#undef DEF_FUNCTION_TYPE_8
+#undef DEF_FUNCTION_TYPE_9
+#undef DEF_FUNCTION_TYPE_10
+#undef DEF_FUNCTION_TYPE_11
+#undef DEF_FUNCTION_TYPE_VAR_0
+#undef DEF_FUNCTION_TYPE_VAR_1
+#undef DEF_FUNCTION_TYPE_VAR_2
+#undef DEF_FUNCTION_TYPE_VAR_3
+#undef DEF_FUNCTION_TYPE_VAR_4
+#undef DEF_FUNCTION_TYPE_VAR_5
+#undef DEF_FUNCTION_TYPE_VAR_6
+#undef DEF_FUNCTION_TYPE_VAR_7
+#undef DEF_POINTER_TYPE
+};
+
+/* Get a string for TYPE_ID suitable for use in logs and error messages
+ (e.g. "BT_PID"). */
+
+static const char *
+get_string_for_type_id (enum jit_builtin_type type_id)
+{
+ gcc_assert (type_id < sizeof (type_names)/sizeof(type_names[0]));
+ return type_names[type_id];
+}
+
/* Get the recording::type for a given type of builtin function,
by ID, creating it if it doesn't already exist. */
default:
// only some of these types are implemented so far:
m_ctxt->add_error (NULL,
- "unimplemented primitive type for builtin: %d", type_id);
+ "unimplemented primitive type for builtin (type: %s)",
+ get_string_for_type_id (type_id));
return NULL;
case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
case BT_ULONGLONG:
return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
- // case BT_INT128:
- // case BT_UINT128:
// case BT_INTMAX:
// case BT_UINTMAX:
+ case BT_INT8: return m_ctxt->get_int_type (1, true);
+ case BT_INT16: return m_ctxt->get_int_type (2, true);
+ case BT_UINT8: return m_ctxt->get_int_type (1, false);
case BT_UINT16: return m_ctxt->get_int_type (2, false);
case BT_UINT32: return m_ctxt->get_int_type (4, false);
case BT_UINT64: return m_ctxt->get_int_type (8, false);
case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
+ // case BT_FLOAT16:
+ // case BT_FLOAT32:
+ // case BT_FLOAT64:
+ // case BT_FLOAT128:
+ // case BT_FLOAT32X:
+ // case BT_FLOAT64X:
+ // case BT_FLOAT128X:
case BT_COMPLEX_FLOAT:
return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
case BT_COMPLEX_DOUBLE:
return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
- // case BT_CONST:
- // case BT_VOLATILE_PTR:
+ // case BT_CONST_TM_PTR:
+ // case BT_FENV_T_PTR:
+ // case BT_CONST_FENV_T_PTR:
+ // case BT_FEXCEPT_T_PTR:
+ // case BT_CONST_FEXCEPT_T_PTR:
+ case BT_CONST_PTR:
+ return m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()->get_pointer ();
+ case BT_VOLATILE_PTR:
+ return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_volatile ()
+ ->get_pointer ());
// case BT_CONST_VOLATILE_PTR:
// case BT_PTRMODE:
- // case BT_INT_PTR:
- // case BT_FLOAT_PTR:
+ case BT_INT_PTR:
+ return m_ctxt->get_type (GCC_JIT_TYPE_INT)->get_pointer ();
+ case BT_FLOAT_PTR:
+ return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT)->get_pointer ();
case BT_DOUBLE_PTR:
return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
- // case BT_CONST_DOUBLE_PTR:
+ case BT_CONST_DOUBLE_PTR:
+ return (m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_const ()
+ ->get_pointer ());
// case BT_LONGDOUBLE_PTR:
// case BT_PID:
- // case BT_SIZE:
+ case BT_SIZE:
+ return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
+ case BT_CONST_SIZE:
+ return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T)->get_const ();
// case BT_SSIZE:
// case BT_WINT:
// case BT_STRING:
// case BT_I4:
// case BT_I8:
// case BT_I16:
+ // case BT_PTR_CONST_STRING:
}
}
#undef create_code
#undef verify_code
+/* test-builtin-memcpy.c */
+#define create_code create_code_builtin_memcpy
+#define verify_code verify_code_builtin_memcpy
+#include "test-builtin-memcpy.c"
+#undef create_code
+#undef verify_code
+
/* test-calling-external-function.c */
#define create_code create_code_calling_external_function
#define verify_code verify_code_calling_external_function
#undef create_code
#undef verify_code
+/* test-pr95306-builtin-types.c. */
+#define create_code create_code_pr95306_builtin_types
+#define verify_code verify_code_pr95306_builtin_types
+#include "test-pr95306-builtin-types.c"
+#undef create_code
+#undef verify_code
+
/* test-reading-struct.c */
#define create_code create_code_reading_struct
#define verify_code verify_code_reading_struct
{"autovectorize",
create_code_autovectorize,
verify_code_autovectorize},
+ {"builtin-memcpy",
+ create_code_builtin_memcpy,
+ verify_code_builtin_memcpy},
{"calling_external_function",
create_code_calling_external_function,
verify_code_calling_external_function},
{"pr66779",
create_code_pr66779,
verify_code_pr66779},
+ {"pr95306_builtin_types",
+ create_code_pr95306_builtin_types,
+ verify_code_pr95306_builtin_types},
{"reading_struct ",
create_code_reading_struct ,
verify_code_reading_struct },
--- /dev/null
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.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:
+ void *
+ test_memcpy (void *dest, const void *src, size_t n)
+ {
+ return __builtin_memcpy (dest, src, n);
+ }
+ */
+ gcc_jit_type *t_void_ptr
+ = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
+ gcc_jit_type *t_const_void_ptr
+ = gcc_jit_type_get_pointer (gcc_jit_type_get_const
+ (gcc_jit_context_get_type
+ (ctxt, GCC_JIT_TYPE_VOID)));
+ gcc_jit_type *t_size_t
+ = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIZE_T);
+
+ gcc_jit_param *dest
+ = gcc_jit_context_new_param (ctxt, NULL, t_void_ptr, "dest");
+ gcc_jit_param *src
+ = gcc_jit_context_new_param (ctxt, NULL, t_const_void_ptr, "src");
+ gcc_jit_param *n = gcc_jit_context_new_param (ctxt, NULL, t_size_t, "n");
+ gcc_jit_param *params[3] = {dest, src, n};
+ gcc_jit_function *func =
+ gcc_jit_context_new_function (ctxt, NULL,
+ GCC_JIT_FUNCTION_EXPORTED,
+ t_void_ptr,
+ "test_memcpy",
+ 3, params, 0);
+
+ gcc_jit_function *jit_memcpy
+ = gcc_jit_context_get_builtin_function (ctxt, "__builtin_memcpy");
+
+
+ gcc_jit_block *b_initial
+ = gcc_jit_function_new_block (func, "initial");
+ gcc_jit_rvalue *args[3] = {gcc_jit_param_as_rvalue (dest),
+ gcc_jit_param_as_rvalue (src),
+ gcc_jit_param_as_rvalue (n)};
+ gcc_jit_rvalue *call
+ = gcc_jit_context_new_call (ctxt, NULL, jit_memcpy, 3, args);
+ gcc_jit_block_end_with_return (b_initial, NULL, call);
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+ typedef void *(*test_memcpy_type) (void *, const void *, size_t);
+ CHECK_NON_NULL (result);
+ test_memcpy_type test_memcpy =
+ (test_memcpy_type)gcc_jit_result_get_code (result, "test_memcpy");
+ CHECK_NON_NULL (test_memcpy);
+
+ int dst = 13;
+ int src = 42;
+ void *out = test_memcpy (&dst, &src, sizeof(int));
+ CHECK_VALUE(out, &dst);
+ CHECK_VALUE(dst, 42);
+}