I've committed this to dmalcolm/jit: Replace various accessors to primitive types (gcc_jit_context_get_char_type etc) with a single entrypoint gcc_jit_context_get_type that takes an enum.
Add two other new entrypoints: /* Floating-point constants. */ extern gcc_jit_rvalue * gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt, gcc_jit_type *type, double value); /* Pointers. */ extern gcc_jit_rvalue * gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt, gcc_jit_type *type, void *value); Add test-types.c to exercise the above. gcc/jit/ * internal-api.c (gcc::jit::context::get_void_type): Remove. (gcc::jit::context::get_char_type): Remove. (gcc::jit::context::get_int_type): Remove. (gcc::jit::context::get_float_type): Remove. (gcc::jit::context::get_double_type): Remove. (get_tree_node_for_type): New. (gcc::jit::context::get_type): New. (gcc::jit::context::new_rvalue_from_double): New. (gcc::jit::context::new_rvalue_from_ptr): New. * internal-api.h (gcc::jit::context::get_void_type): Remove. (gcc::jit::context::get_char_type): Remove. (gcc::jit::context::get_int_type): Remove. (gcc::jit::context::get_float_type): Remove. (gcc::jit::context::get_double_type): Remove. (gcc::jit::context::get_type): New. (gcc::jit::context::new_rvalue_from_double): New. (gcc::jit::context::new_rvalue_from_ptr): New. * libgccjit.c (gcc_jit_context_get_void_type): Remove. (gcc_jit_context_get_char_type): Remove. (gcc_jit_context_get_int_type): Remove. (gcc_jit_context_get_float_type): Remove. (gcc_jit_context_get_double_type): Remove. (gcc_jit_context_get_type): New. (gcc_jit_context_new_rvalue_from_double): New. (gcc_jit_context_new_rvalue_from_ptr): New. * libgccjit.h (gcc_jit_context_get_void_type): Remove. (gcc_jit_context_get_char_type): Remove. (gcc_jit_context_get_int_type): Remove. (gcc_jit_context_get_float_type): Remove. (gcc_jit_context_get_double_type): Remove. (enum gcc_jit_types): New. (gcc_jit_context_get_type): New. (gcc_jit_context_new_rvalue_from_double): New. (gcc_jit_context_new_rvalue_from_ptr): New. * libgccjit.map (gcc_jit_context_get_void_type): Remove. (gcc_jit_context_get_char_type): Remove. (gcc_jit_context_get_int_type): Remove. (gcc_jit_context_get_float_type): Remove. (gcc_jit_context_get_double_type): Remove. (enum gcc_jit_types): New. (gcc_jit_context_get_type): New. (gcc_jit_context_new_rvalue_from_double): New. (gcc_jit_context_new_rvalue_from_ptr): New. * TODO.rst ("access to more primitive types"): Remove completed item. gcc/testsuite/ * jit.dg/test-accessing-struct.c (code_making_callback): Update for changes to type API. * jit.dg/test-calling-external-function.c (code_making_callback): Likewise. * jit.dg/test-dot-product.c (code_making_callback): Likewise. * jit.dg/test-factorial.c (code_making_callback): Likewise. * jit.dg/test-fibonacci.c (code_making_callback): Likewise. * jit.dg/test-hello-world.c (code_making_callback): Likewise. * jit.dg/test-string-literal.c (code_making_callback): Likewise. * jit.dg/test-sum-of-squares.c (code_making_callback): Likewise. * jit.dg/test-using-globals.c (code_making_callback): Likewise. * jit.dg/test-types.c: New. * jit.dg/test-combination.c (code_making_callback): Use code from test-types.c. (verify_code): ...and verify it. --- gcc/jit/ChangeLog.jit | 48 ++++ gcc/jit/TODO.rst | 11 - gcc/jit/internal-api.c | 130 ++++++++-- gcc/jit/internal-api.h | 22 +- gcc/jit/libgccjit.c | 49 ++-- gcc/jit/libgccjit.h | 67 ++++- gcc/jit/libgccjit.map | 8 +- gcc/testsuite/ChangeLog.jit | 18 ++ gcc/testsuite/jit.dg/test-accessing-struct.c | 6 +- .../jit.dg/test-calling-external-function.c | 6 +- gcc/testsuite/jit.dg/test-combination.c | 9 + gcc/testsuite/jit.dg/test-dot-product.c | 6 +- gcc/testsuite/jit.dg/test-factorial.c | 3 +- gcc/testsuite/jit.dg/test-fibonacci.c | 3 +- gcc/testsuite/jit.dg/test-hello-world.c | 17 +- gcc/testsuite/jit.dg/test-string-literal.c | 3 +- gcc/testsuite/jit.dg/test-sum-of-squares.c | 3 +- gcc/testsuite/jit.dg/test-types.c | 287 +++++++++++++++++++++ gcc/testsuite/jit.dg/test-using-global.c | 6 +- 19 files changed, 588 insertions(+), 114 deletions(-) create mode 100644 gcc/testsuite/jit.dg/test-types.c diff --git a/gcc/jit/ChangeLog.jit b/gcc/jit/ChangeLog.jit index d0c76fc..78c1b65 100644 --- a/gcc/jit/ChangeLog.jit +++ b/gcc/jit/ChangeLog.jit @@ -1,5 +1,53 @@ 2013-10-17 David Malcolm <dmalc...@redhat.com> + * internal-api.c (gcc::jit::context::get_void_type): Remove. + (gcc::jit::context::get_char_type): Remove. + (gcc::jit::context::get_int_type): Remove. + (gcc::jit::context::get_float_type): Remove. + (gcc::jit::context::get_double_type): Remove. + (get_tree_node_for_type): New. + (gcc::jit::context::get_type): New. + (gcc::jit::context::new_rvalue_from_double): New. + (gcc::jit::context::new_rvalue_from_ptr): New. + * internal-api.h (gcc::jit::context::get_void_type): Remove. + (gcc::jit::context::get_char_type): Remove. + (gcc::jit::context::get_int_type): Remove. + (gcc::jit::context::get_float_type): Remove. + (gcc::jit::context::get_double_type): Remove. + (gcc::jit::context::get_type): New. + (gcc::jit::context::new_rvalue_from_double): New. + (gcc::jit::context::new_rvalue_from_ptr): New. + * libgccjit.c (gcc_jit_context_get_void_type): Remove. + (gcc_jit_context_get_char_type): Remove. + (gcc_jit_context_get_int_type): Remove. + (gcc_jit_context_get_float_type): Remove. + (gcc_jit_context_get_double_type): Remove. + (gcc_jit_context_get_type): New. + (gcc_jit_context_new_rvalue_from_double): New. + (gcc_jit_context_new_rvalue_from_ptr): New. + * libgccjit.h (gcc_jit_context_get_void_type): Remove. + (gcc_jit_context_get_char_type): Remove. + (gcc_jit_context_get_int_type): Remove. + (gcc_jit_context_get_float_type): Remove. + (gcc_jit_context_get_double_type): Remove. + (enum gcc_jit_types): New. + (gcc_jit_context_get_type): New. + (gcc_jit_context_new_rvalue_from_double): New. + (gcc_jit_context_new_rvalue_from_ptr): New. + * libgccjit.map (gcc_jit_context_get_void_type): Remove. + (gcc_jit_context_get_char_type): Remove. + (gcc_jit_context_get_int_type): Remove. + (gcc_jit_context_get_float_type): Remove. + (gcc_jit_context_get_double_type): Remove. + (enum gcc_jit_types): New. + (gcc_jit_context_get_type): New. + (gcc_jit_context_new_rvalue_from_double): New. + (gcc_jit_context_new_rvalue_from_ptr): New. + * TODO.rst ("access to more primitive types"): Remove + completed item. + +2013-10-17 David Malcolm <dmalc...@redhat.com> + * libgccjit.h: Add and reword comments throughout. 2013-10-17 David Malcolm <dmalc...@redhat.com> diff --git a/gcc/jit/TODO.rst b/gcc/jit/TODO.rst index f126fcc..ffe4c37 100644 --- a/gcc/jit/TODO.rst +++ b/gcc/jit/TODO.rst @@ -44,17 +44,6 @@ Initial Release e.g. (void*) to (struct foo*) -* access to more primitive types:: - - enum gcc_jit_types { - GCC_JIT_TYPE_SIGNED_INT, - // etc - }; - - extern gcc_jit_type * - gcc_jit_context_get_type (gcc_jit_context *, - enum gcc_jit_types type); - * support more arithmetic ops and comparison modes * access to a function by address:: diff --git a/gcc/jit/internal-api.c b/gcc/jit/internal-api.c index c48257f..9232215 100644 --- a/gcc/jit/internal-api.c +++ b/gcc/jit/internal-api.c @@ -67,39 +67,80 @@ set_code_factory (gcc_jit_code_callback cb, m_user_data = user_data; } -gcc::jit::type * -gcc::jit::context:: -get_void_type () -{ - return new type (void_type_node); -} - -gcc::jit::type * -gcc::jit::context:: -get_char_type () +static tree +get_tree_node_for_type (enum gcc_jit_types type_) { - return new type (char_type_node); -} + switch (type_) + { + case GCC_JIT_TYPE_VOID: + return void_type_node; + + case GCC_JIT_TYPE_VOID_PTR: + return ptr_type_node; + + case GCC_JIT_TYPE_CHAR: + return char_type_node; + case GCC_JIT_TYPE_SIGNED_CHAR: + return signed_char_type_node; + case GCC_JIT_TYPE_UNSIGNED_CHAR: + return unsigned_char_type_node; + + case GCC_JIT_TYPE_SHORT: + return short_integer_type_node; + case GCC_JIT_TYPE_UNSIGNED_SHORT: + return short_unsigned_type_node; + + case GCC_JIT_TYPE_CONST_CHAR_PTR: + { + tree const_char = build_qualified_type (char_type_node, + TYPE_QUAL_CONST); + return build_pointer_type (const_char); + } + + case GCC_JIT_TYPE_INT: + return integer_type_node; + case GCC_JIT_TYPE_UNSIGNED_INT: + return unsigned_type_node; + + case GCC_JIT_TYPE_LONG: + return long_integer_type_node; + case GCC_JIT_TYPE_UNSIGNED_LONG: + return long_unsigned_type_node; + + case GCC_JIT_TYPE_LONG_LONG: + return long_long_integer_type_node; + case GCC_JIT_TYPE_UNSIGNED_LONG_LONG: + return long_long_unsigned_type_node; + + case GCC_JIT_TYPE_FLOAT: + return float_type_node; + case GCC_JIT_TYPE_DOUBLE: + return double_type_node; + case GCC_JIT_TYPE_LONG_DOUBLE: + return long_double_type_node; + + case GCC_JIT_TYPE_SIZE_T: + return size_type_node; + + case GCC_JIT_TYPE_FILE_PTR: + return fileptr_type_node; + } -gcc::jit::type * -gcc::jit::context:: -get_int_type () -{ - return new type (intSI_type_node); + return NULL; } gcc::jit::type * gcc::jit::context:: -get_float_type () +get_type (enum gcc_jit_types type_) { - return new type (float_type_node); -} + tree type_node = get_tree_node_for_type (type_); + if (NULL == type_node) + { + add_error ("unrecognized (enum gcc_jit_types) value"); + return NULL; + } -gcc::jit::type * -gcc::jit::context:: -get_double_type () -{ - return new type (double_type_node); + return new type (type_node); } gcc::jit::field * @@ -283,6 +324,45 @@ new_rvalue_from_int (type *type, gcc::jit::rvalue * gcc::jit::context:: +new_rvalue_from_double (type *type, + double value) +{ + // FIXME: type-checking, or coercion? + tree inner_type = type->as_tree (); + + /* We have a "double", we want a REAL_VALUE_TYPE. + + real.c:real_from_target appears to require the representation to be + split into 32-bit values, and then sent as an pair of host long + ints. */ + REAL_VALUE_TYPE real_value; + union + { + double as_double; + uint32_t as_uint32s[2]; + } u; + u.as_double = value; + long int as_long_ints[2]; + as_long_ints[0] = u.as_uint32s[0]; + as_long_ints[1] = u.as_uint32s[1]; + real_from_target (&real_value, as_long_ints, DFmode); + tree inner = build_real (inner_type, real_value); + return new rvalue (inner); +} + +gcc::jit::rvalue * +gcc::jit::context:: +new_rvalue_from_ptr (type *type, + void *value) +{ + tree inner_type = type->as_tree (); + /* FIXME: how to ensure we have a wide enough type? */ + tree inner = build_int_cstu (inner_type, (unsigned HOST_WIDE_INT)value); + return new rvalue (inner); +} + +gcc::jit::rvalue * +gcc::jit::context:: new_string_literal (const char *value) { tree t_str = build_string (strlen (value), value); diff --git a/gcc/jit/internal-api.h b/gcc/jit/internal-api.h index e3650fc..b145cb3 100644 --- a/gcc/jit/internal-api.h +++ b/gcc/jit/internal-api.h @@ -40,19 +40,7 @@ public: int column); type * - get_void_type (); - - type * - get_char_type (); - - type * - get_int_type (); - - type * - get_float_type (); - - type * - get_double_type (); + get_type (enum gcc_jit_types type); field * new_field (location *loc, @@ -90,6 +78,14 @@ public: int value); rvalue * + new_rvalue_from_double (type *type, + double value); + + rvalue * + new_rvalue_from_ptr (type *type, + void *value); + + rvalue * new_string_literal (const char *value); rvalue * diff --git a/gcc/jit/libgccjit.c b/gcc/jit/libgccjit.c index ec854a1..5da2889 100644 --- a/gcc/jit/libgccjit.c +++ b/gcc/jit/libgccjit.c @@ -97,38 +97,11 @@ gcc_jit_context_new_location (gcc_jit_context *ctxt, } gcc_jit_type * -gcc_jit_context_get_void_type (gcc_jit_context *ctxt) +gcc_jit_context_get_type (gcc_jit_context *ctxt, + enum gcc_jit_types type) { ASSERT_WITHIN_CALLBACK (ctxt); - return (gcc_jit_type *)ctxt->get_void_type (); -} - -gcc_jit_type * -gcc_jit_context_get_char_type (gcc_jit_context *ctxt) -{ - ASSERT_WITHIN_CALLBACK (ctxt); - return (gcc_jit_type *)ctxt->get_char_type (); -} - -gcc_jit_type * -gcc_jit_context_get_int_type (gcc_jit_context *ctxt) -{ - ASSERT_WITHIN_CALLBACK (ctxt); - return (gcc_jit_type *)ctxt->get_int_type (); -} - -gcc_jit_type * -gcc_jit_context_get_float_type (gcc_jit_context *ctxt) -{ - ASSERT_WITHIN_CALLBACK (ctxt); - return (gcc_jit_type *)ctxt->get_float_type (); -} - -gcc_jit_type * -gcc_jit_context_get_double_type (gcc_jit_context *ctxt) -{ - ASSERT_WITHIN_CALLBACK (ctxt); - return (gcc_jit_type *)ctxt->get_double_type (); + return (gcc_jit_type *)ctxt->get_type (type); } gcc_jit_type * @@ -254,6 +227,22 @@ gcc_jit_context_one (gcc_jit_context *ctxt, } gcc_jit_rvalue * +gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt, + gcc_jit_type *type, + double value) +{ + return (gcc_jit_rvalue *)ctxt->new_rvalue_from_double (type, value); +} + +gcc_jit_rvalue * +gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt, + gcc_jit_type *type, + void *value) +{ + return (gcc_jit_rvalue *)ctxt->new_rvalue_from_ptr (type, value); +} + +gcc_jit_rvalue * gcc_jit_context_new_string_literal (gcc_jit_context *ctxt, const char *value) { diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h index e4b8de6..a3da478 100644 --- a/gcc/jit/libgccjit.h +++ b/gcc/jit/libgccjit.h @@ -267,20 +267,57 @@ gcc_jit_context_new_location (gcc_jit_context *ctxt, **********************************************************************/ /* Access to specific types. */ -extern gcc_jit_type * -gcc_jit_context_get_void_type (gcc_jit_context *ctxt); +enum gcc_jit_types +{ + /* C's "void" type. */ + GCC_JIT_TYPE_VOID, -extern gcc_jit_type * -gcc_jit_context_get_char_type (gcc_jit_context *ctxt); + /* "void *". */ + GCC_JIT_TYPE_VOID_PTR, -extern gcc_jit_type * -gcc_jit_context_get_int_type (gcc_jit_context *ctxt); + /* Various integer types. */ -extern gcc_jit_type * -gcc_jit_context_get_float_type (gcc_jit_context *ctxt); + /* C's "char" (of some signedness) and the variants where the + signedness is specified. */ + GCC_JIT_TYPE_CHAR, + GCC_JIT_TYPE_SIGNED_CHAR, + GCC_JIT_TYPE_UNSIGNED_CHAR, + + /* C's "short" and "unsigned short". */ + GCC_JIT_TYPE_SHORT, /* signed */ + GCC_JIT_TYPE_UNSIGNED_SHORT, + + /* C's "int" and "unsigned int". */ + GCC_JIT_TYPE_INT, /* signed */ + GCC_JIT_TYPE_UNSIGNED_INT, + + /* C's "long" and "unsigned long". */ + GCC_JIT_TYPE_LONG, /* signed */ + GCC_JIT_TYPE_UNSIGNED_LONG, + + /* C99's "long long" and "unsigned long long". */ + GCC_JIT_TYPE_LONG_LONG, /* signed */ + GCC_JIT_TYPE_UNSIGNED_LONG_LONG, + + /* Floating-point types */ + + GCC_JIT_TYPE_FLOAT, + GCC_JIT_TYPE_DOUBLE, + GCC_JIT_TYPE_LONG_DOUBLE, + + /* C type: (const char *). */ + GCC_JIT_TYPE_CONST_CHAR_PTR, + + /* The C "size_t" type. */ + GCC_JIT_TYPE_SIZE_T, + + /* C type: (FILE *) */ + GCC_JIT_TYPE_FILE_PTR +}; extern gcc_jit_type * -gcc_jit_context_get_double_type (gcc_jit_context *ctxt); +gcc_jit_context_get_type (gcc_jit_context *ctxt, + enum gcc_jit_types type_); /* Constructing new types. */ @@ -380,6 +417,18 @@ extern gcc_jit_rvalue * gcc_jit_context_one (gcc_jit_context *ctxt, gcc_jit_type *type); +/* Floating-point constants. */ +extern gcc_jit_rvalue * +gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt, + gcc_jit_type *type, + double value); + +/* Pointers. */ +extern gcc_jit_rvalue * +gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt, + gcc_jit_type *type, + void *value); + /* String literals. */ extern gcc_jit_rvalue * gcc_jit_context_new_string_literal (gcc_jit_context *ctxt, diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map index a8b91e6..beaf0de 100644 --- a/gcc/jit/libgccjit.map +++ b/gcc/jit/libgccjit.map @@ -4,11 +4,7 @@ # Keep this list sorted alphabetically: gcc_jit_context_acquire; gcc_jit_context_compile; - gcc_jit_context_get_char_type; - gcc_jit_context_get_double_type; - gcc_jit_context_get_float_type; - gcc_jit_context_get_int_type; - gcc_jit_context_get_void_type; + gcc_jit_context_get_type; gcc_jit_context_new_array_lookup; gcc_jit_context_new_binary_op; gcc_jit_context_new_call; @@ -19,7 +15,9 @@ gcc_jit_context_new_global; gcc_jit_context_new_location; gcc_jit_context_new_param; + gcc_jit_context_new_rvalue_from_double; gcc_jit_context_new_rvalue_from_int; + gcc_jit_context_new_rvalue_from_ptr; gcc_jit_context_new_string_literal; gcc_jit_context_new_struct_type; gcc_jit_context_one; diff --git a/gcc/testsuite/ChangeLog.jit b/gcc/testsuite/ChangeLog.jit index 7cc8fa1..87cb958 100644 --- a/gcc/testsuite/ChangeLog.jit +++ b/gcc/testsuite/ChangeLog.jit @@ -1,3 +1,21 @@ +2013-10-17 David Malcolm <dmalc...@redhat.com> + + * jit.dg/test-accessing-struct.c (code_making_callback): Update + for changes to type API. + * jit.dg/test-calling-external-function.c (code_making_callback): + Likewise. + * jit.dg/test-dot-product.c (code_making_callback): Likewise. + * jit.dg/test-factorial.c (code_making_callback): Likewise. + * jit.dg/test-fibonacci.c (code_making_callback): Likewise. + * jit.dg/test-hello-world.c (code_making_callback): Likewise. + * jit.dg/test-string-literal.c (code_making_callback): Likewise. + * jit.dg/test-sum-of-squares.c (code_making_callback): Likewise. + * jit.dg/test-using-globals.c (code_making_callback): Likewise. + * jit.dg/test-types.c: New. + * jit.dg/test-combination.c (code_making_callback): Use code + from test-types.c. + (verify_code): ...and verify it. + 2013-10-16 David Malcolm <dmalc...@redhat.com> * jit.dg/test-dot-product.c (code_making_callback): Update for diff --git a/gcc/testsuite/jit.dg/test-accessing-struct.c b/gcc/testsuite/jit.dg/test-accessing-struct.c index 223b679..46f112a 100644 --- a/gcc/testsuite/jit.dg/test-accessing-struct.c +++ b/gcc/testsuite/jit.dg/test-accessing-struct.c @@ -22,8 +22,10 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) f->z = f->x * f->y; } */ - gcc_jit_type *void_type = gcc_jit_context_get_void_type (ctxt); - gcc_jit_type *int_type = gcc_jit_context_get_int_type (ctxt); + gcc_jit_type *void_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); gcc_jit_field *x = gcc_jit_context_new_field (ctxt, NULL, diff --git a/gcc/testsuite/jit.dg/test-calling-external-function.c b/gcc/testsuite/jit.dg/test-calling-external-function.c index 0e2a0a9..6e5dd7d 100644 --- a/gcc/testsuite/jit.dg/test-calling-external-function.c +++ b/gcc/testsuite/jit.dg/test-calling-external-function.c @@ -29,8 +29,10 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) } */ int i; - gcc_jit_type *void_type = gcc_jit_context_get_void_type (ctxt); - gcc_jit_type *int_type = gcc_jit_context_get_int_type (ctxt); + gcc_jit_type *void_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); /* Declare the imported function. */ gcc_jit_param *params[3]; diff --git a/gcc/testsuite/jit.dg/test-combination.c b/gcc/testsuite/jit.dg/test-combination.c index 6eb8dbe..2bf7124 100644 --- a/gcc/testsuite/jit.dg/test-combination.c +++ b/gcc/testsuite/jit.dg/test-combination.c @@ -66,6 +66,13 @@ #undef code_making_callback #undef verify_code +/* test-types.c */ +#define code_making_callback code_making_callback_types +#define verify_code verify_code_types +#include "test-types.c" +#undef code_making_callback +#undef verify_code + /* test-using-global.c */ #define code_making_callback code_making_callback_using_global #define verify_code verify_code_using_global @@ -93,6 +100,7 @@ code_making_callback (gcc_jit_context *ctxt, void * user_data) errors += code_making_callback_hello_world (ctxt, user_data); errors += code_making_callback_string_literal (ctxt, user_data); errors += code_making_callback_sum_of_squares (ctxt, user_data); + errors += code_making_callback_types (ctxt, user_data); errors += code_making_callback_using_global (ctxt, user_data); return errors; @@ -109,5 +117,6 @@ verify_code (gcc_jit_result *result) verify_code_hello_world (result); verify_code_string_literal (result); verify_code_sum_of_squares (result); + verify_code_types (result); verify_code_using_global (result); } diff --git a/gcc/testsuite/jit.dg/test-dot-product.c b/gcc/testsuite/jit.dg/test-dot-product.c index d01e495..3a8a59a 100644 --- a/gcc/testsuite/jit.dg/test-dot-product.c +++ b/gcc/testsuite/jit.dg/test-dot-product.c @@ -21,9 +21,11 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) } and see what the optimizer can do. */ - gcc_jit_type *val_type = gcc_jit_context_get_double_type (ctxt); + gcc_jit_type *val_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE); gcc_jit_type *ptr_type = gcc_jit_type_get_pointer (val_type); - gcc_jit_type *int_type = gcc_jit_context_get_int_type (ctxt); + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); gcc_jit_type *return_type = val_type; gcc_jit_param *param_n = diff --git a/gcc/testsuite/jit.dg/test-factorial.c b/gcc/testsuite/jit.dg/test-factorial.c index 90b4fbe..5c3b237 100644 --- a/gcc/testsuite/jit.dg/test-factorial.c +++ b/gcc/testsuite/jit.dg/test-factorial.c @@ -22,7 +22,8 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) and see if the optimizer eliminates the recursion (it does). */ - gcc_jit_type *the_type = gcc_jit_context_get_int_type (ctxt); + gcc_jit_type *the_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); gcc_jit_type *return_type = the_type; gcc_jit_param *x = diff --git a/gcc/testsuite/jit.dg/test-fibonacci.c b/gcc/testsuite/jit.dg/test-fibonacci.c index 215546c..16d8a46 100644 --- a/gcc/testsuite/jit.dg/test-fibonacci.c +++ b/gcc/testsuite/jit.dg/test-fibonacci.c @@ -30,7 +30,8 @@ FIRST_LINE + 7: } in the debugger, stepping through the above commented-out code fragement. */ - gcc_jit_type *the_type = gcc_jit_context_get_int_type (ctxt); + gcc_jit_type *the_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); gcc_jit_type *return_type = the_type; gcc_jit_param *x = diff --git a/gcc/testsuite/jit.dg/test-hello-world.c b/gcc/testsuite/jit.dg/test-hello-world.c index e7856c6..0e3af81 100644 --- a/gcc/testsuite/jit.dg/test-hello-world.c +++ b/gcc/testsuite/jit.dg/test-hello-world.c @@ -15,10 +15,10 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) printf ("hello %s\n", name); } */ - gcc_jit_type *void_type = gcc_jit_context_get_void_type (ctxt); + gcc_jit_type *void_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); gcc_jit_type *const_char_ptr_type = - gcc_jit_type_get_pointer ( - gcc_jit_type_get_const (gcc_jit_context_get_char_type (ctxt))); + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR); gcc_jit_param *param_name = gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name"); gcc_jit_function *func = @@ -33,11 +33,12 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format"); gcc_jit_function *printf_func = gcc_jit_context_new_function (ctxt, NULL, - GCC_JIT_FUNCTION_IMPORTED, - gcc_jit_context_get_int_type (ctxt), - "printf", - 1, ¶m_format, - 1); + GCC_JIT_FUNCTION_IMPORTED, + gcc_jit_context_get_type ( + ctxt, GCC_JIT_TYPE_INT), + "printf", + 1, ¶m_format, + 1); gcc_jit_rvalue *args[2]; args[0] = gcc_jit_context_new_string_literal (ctxt, "hello %s\n"); args[1] = gcc_jit_param_as_rvalue (param_name); diff --git a/gcc/testsuite/jit.dg/test-string-literal.c b/gcc/testsuite/jit.dg/test-string-literal.c index 0eb08d5..cd373ad 100644 --- a/gcc/testsuite/jit.dg/test-string-literal.c +++ b/gcc/testsuite/jit.dg/test-string-literal.c @@ -18,8 +18,7 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) } */ gcc_jit_type *const_char_ptr_type = - gcc_jit_type_get_pointer ( - gcc_jit_type_get_const (gcc_jit_context_get_char_type (ctxt))); + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR); /* Build the test_fn. */ gcc_jit_function *test_fn = diff --git a/gcc/testsuite/jit.dg/test-sum-of-squares.c b/gcc/testsuite/jit.dg/test-sum-of-squares.c index c8bb16a..48ac93f 100644 --- a/gcc/testsuite/jit.dg/test-sum-of-squares.c +++ b/gcc/testsuite/jit.dg/test-sum-of-squares.c @@ -22,7 +22,8 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) } return sum; */ - gcc_jit_type *the_type = gcc_jit_context_get_int_type (ctxt); + gcc_jit_type *the_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); gcc_jit_type *return_type = the_type; gcc_jit_param *n = diff --git a/gcc/testsuite/jit.dg/test-types.c b/gcc/testsuite/jit.dg/test-types.c new file mode 100644 index 0000000..4c30547 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-types.c @@ -0,0 +1,287 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stddef.h> + +#include "libgccjit.h" + +#include "harness.h" + +struct zoo +{ + void *m_void_ptr; + + char m_char; + signed char m_signed_char; + unsigned char m_unsigned_char; + + short m_short; + unsigned short m_unsigned_short; + + int m_int; + unsigned int m_unsigned_int; + + long m_long; + unsigned long m_unsigned_long; + + long long m_long_long; + unsigned long long m_unsigned_long_long; + + float m_float; + double m_double; + long double m_long_double; + + const char *m_const_char_ptr; + + size_t m_size_t; + + FILE *m_FILE_ptr; +}; + +/* Now describe the struct in libgccjit terms. */ +struct field +{ + enum gcc_jit_types type_; + const char *name; +}; + +const struct field fields[] = { + {GCC_JIT_TYPE_VOID_PTR, "m_void_ptr"}, + + {GCC_JIT_TYPE_CHAR, "m_char"}, + {GCC_JIT_TYPE_SIGNED_CHAR, "m_signed_char"}, + {GCC_JIT_TYPE_UNSIGNED_CHAR, "m_unsigned_char"}, + + {GCC_JIT_TYPE_SHORT, "m_short"}, + {GCC_JIT_TYPE_UNSIGNED_SHORT, "m_unsigned_short"}, + + {GCC_JIT_TYPE_INT, "m_int"}, + {GCC_JIT_TYPE_UNSIGNED_INT, "m_unsigned_int"}, + + {GCC_JIT_TYPE_LONG, "m_long"}, + {GCC_JIT_TYPE_UNSIGNED_LONG, "m_unsigned_long"}, + + {GCC_JIT_TYPE_LONG_LONG, "m_long_long"}, + {GCC_JIT_TYPE_UNSIGNED_LONG_LONG, "m_unsigned_long_long"}, + + {GCC_JIT_TYPE_FLOAT, "m_float"}, + {GCC_JIT_TYPE_DOUBLE, "m_double"}, + {GCC_JIT_TYPE_LONG_DOUBLE, "m_long_double"}, + + {GCC_JIT_TYPE_CONST_CHAR_PTR, "m_const_char_ptr"}, + + {GCC_JIT_TYPE_SIZE_T, "m_size_t"}, + + {GCC_JIT_TYPE_FILE_PTR, "m_FILE_ptr"}, + +}; + +#define NUM_FIELDS (sizeof(fields)/sizeof(fields[0])) + +int test_int = 42; +int *test_ptr = &test_int; + +const char *test_string = "test_string"; + +int +code_making_callback (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + + void + test_caller (struct zoo *z) + { + for each fields "m_field": + z->m_field = ...some data; + } + */ + int i; + gcc_jit_type *void_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); + gcc_jit_field *zoo_fields[NUM_FIELDS]; + for (i = 0; i < NUM_FIELDS; i++) + zoo_fields[i] = + gcc_jit_context_new_field ( + ctxt, NULL, + gcc_jit_context_get_type (ctxt, fields[i].type_), + fields[i].name); + gcc_jit_type *zoo_type = + gcc_jit_context_new_struct_type (ctxt, + NULL, + "zoo", + NUM_FIELDS, + zoo_fields); + gcc_jit_type *zoo_ptr_type = + gcc_jit_type_get_pointer (zoo_type); + + /* Build the test_fn. */ + gcc_jit_param *param_z = + gcc_jit_context_new_param (ctxt, NULL, zoo_ptr_type, "z"); + gcc_jit_function *test_fn = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + void_type, + "test_types", + 1, ¶m_z, + 0); + + /* Write to the various fields of param "z". */ +#define ASSIGN(FIELDNAME, EXPR) \ + gcc_jit_function_add_assignment ( \ + test_fn, NULL, \ + gcc_jit_context_new_field_access ( \ + ctxt, NULL, \ + gcc_jit_param_as_rvalue (param_z), \ + (FIELDNAME)), \ + (EXPR)); + + ASSIGN( + "m_void_ptr", + gcc_jit_context_new_rvalue_from_ptr ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR), + test_ptr)) + + ASSIGN("m_char", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR), + 'V')) + ASSIGN("m_signed_char", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIGNED_CHAR), + -37)) + ASSIGN("m_unsigned_char", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_CHAR), + 200)) + + ASSIGN("m_short", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SHORT), + -900)) + ASSIGN("m_unsigned_short", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_SHORT), + 0x3000)) + + ASSIGN("m_int", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT), + -0x2000)) + ASSIGN("m_unsigned_int", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT), + 1234567)) + + ASSIGN("m_long", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG), + -5)) + ASSIGN("m_unsigned_long", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG), + 12345678)) + + ASSIGN("m_long_long", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG), + -42)) + ASSIGN("m_unsigned_long_long", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG_LONG), + 123456789)) + + ASSIGN("m_float", + gcc_jit_context_new_rvalue_from_double ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT), + 3.141)) + ASSIGN("m_double", + gcc_jit_context_new_rvalue_from_double ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE), + 3.141)) + ASSIGN("m_long_double", + gcc_jit_context_new_rvalue_from_double ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_DOUBLE), + 3.141)) + + ASSIGN("m_const_char_ptr", + gcc_jit_context_new_rvalue_from_ptr ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR), + (char *)test_string)) + + ASSIGN("m_size_t", + gcc_jit_context_new_rvalue_from_int ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIZE_T), + sizeof (struct zoo))) + + ASSIGN("m_FILE_ptr", + gcc_jit_context_new_rvalue_from_ptr ( + ctxt, + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FILE_PTR), + stderr)) + +#undef ASSIGN + + return 0; +} + +void +verify_code (gcc_jit_result *result) +{ + typedef void (*fn_type) (struct zoo *); + CHECK_NON_NULL (result); + + fn_type test_types = + (fn_type)gcc_jit_result_get_code (result, "test_types"); + CHECK_NON_NULL (test_types); + + struct zoo z; + memset (&z, 0xf0, sizeof (z)); + + /* Call the JIT-generated function. */ + test_types (&z); + + /* Verify that it correctly wrote to the various fields. */ + CHECK_VALUE (z.m_void_ptr, test_ptr); + + CHECK_VALUE (z.m_char, 'V'); + CHECK_VALUE (z.m_signed_char, -37); + CHECK_VALUE (z.m_unsigned_char, 200); + + CHECK_VALUE (z.m_short, -900); + CHECK_VALUE (z.m_unsigned_short, 0x3000); + + CHECK_VALUE (z.m_int, -0x2000); + CHECK_VALUE (z.m_unsigned_int, 1234567); + + CHECK_VALUE (z.m_long, -5); + CHECK_VALUE (z.m_unsigned_long, 12345678); + + CHECK_VALUE (z.m_long_long, -42); + CHECK_VALUE (z.m_unsigned_long_long, 123456789); + + CHECK_VALUE (z.m_float, 3.141f); + CHECK_VALUE (z.m_double, 3.141); + CHECK_VALUE (z.m_long_double, 3.141); + + CHECK_VALUE (z.m_const_char_ptr, test_string); + + CHECK_VALUE (z.m_size_t, sizeof (struct zoo)); + + CHECK_VALUE (z.m_FILE_ptr, stderr); +} diff --git a/gcc/testsuite/jit.dg/test-using-global.c b/gcc/testsuite/jit.dg/test-using-global.c index 9e3caaf..8eabc31 100644 --- a/gcc/testsuite/jit.dg/test-using-global.c +++ b/gcc/testsuite/jit.dg/test-using-global.c @@ -27,8 +27,10 @@ code_making_callback (gcc_jit_context *ctxt, void *user_data) the_global += 1; } */ - gcc_jit_type *void_type = gcc_jit_context_get_void_type (ctxt); - gcc_jit_type *int_type = gcc_jit_context_get_int_type (ctxt); + gcc_jit_type *void_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); /* Build the test_fn. */ gcc_jit_function *test_fn = -- 1.7.11.7