From 659856ce28a9f31a8727c40b60d24bb56d49888d Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Sat, 7 Feb 2015 00:11:27 +0000 Subject: [PATCH] Add new files erroneously omitted from r220494. From-SVN: r220496 --- .../create-code-for-hello-world-executable.h | 101 ++++++++++++++++++ gcc/testsuite/jit.dg/verify-dynamic-library.c | 41 +++++++ 2 files changed, 142 insertions(+) create mode 100644 gcc/testsuite/jit.dg/create-code-for-hello-world-executable.h create mode 100644 gcc/testsuite/jit.dg/verify-dynamic-library.c diff --git a/gcc/testsuite/jit.dg/create-code-for-hello-world-executable.h b/gcc/testsuite/jit.dg/create-code-for-hello-world-executable.h new file mode 100644 index 00000000000..363171f6764 --- /dev/null +++ b/gcc/testsuite/jit.dg/create-code-for-hello-world-executable.h @@ -0,0 +1,101 @@ +/* This code is shared by various test-compile-to-*.c test cases + that ultimately generate a standalone executable + (all of them apart from test-compile-to-dynamic-library.c). */ + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + static void + hello_world (const char *name) + { + // a test comment + printf ("hello from %s\n", name); + } + + extern int + main (int argc, char **argv) + { + hello_world (argv[0]); + return 0; + } + */ + gcc_jit_type *void_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); + gcc_jit_type *const_char_ptr_type = + 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 = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_INTERNAL, + void_type, + "hello_world", + 1, ¶m_name, + 0); + + gcc_jit_param *param_format = + 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_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 from %s\n"); + args[1] = gcc_jit_param_as_rvalue (param_name); + + gcc_jit_block *block = gcc_jit_function_new_block (func, NULL); + + gcc_jit_block_add_comment ( + block, NULL, + "a test comment"); + + gcc_jit_block_add_eval ( + block, NULL, + gcc_jit_context_new_call (ctxt, + NULL, + printf_func, + 2, args)); + gcc_jit_block_end_with_void_return (block, NULL); + + gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_param *param_argc = + gcc_jit_context_new_param (ctxt, NULL, int_type, "argc"); + gcc_jit_type *char_ptr_ptr_type = + gcc_jit_type_get_pointer ( + gcc_jit_type_get_pointer ( + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR))); + gcc_jit_param *param_argv = + gcc_jit_context_new_param (ctxt, NULL, char_ptr_ptr_type, "argv"); + gcc_jit_param *params[2] = {param_argc, param_argv}; + gcc_jit_function *func_main = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + int_type, + "main", + 2, params, + 0); + block = gcc_jit_function_new_block (func_main, NULL); + gcc_jit_rvalue *zero = gcc_jit_context_zero (ctxt, int_type); + args[0] = gcc_jit_context_new_cast ( + ctxt, + NULL, + gcc_jit_lvalue_as_rvalue ( + gcc_jit_context_new_array_access ( + ctxt, + NULL, + gcc_jit_param_as_rvalue (param_argv), + zero)), + const_char_ptr_type); + gcc_jit_block_add_eval ( + block, NULL, + gcc_jit_context_new_call (ctxt, + NULL, + func, + 1, args)); + gcc_jit_block_end_with_return (block, NULL, zero); +} diff --git a/gcc/testsuite/jit.dg/verify-dynamic-library.c b/gcc/testsuite/jit.dg/verify-dynamic-library.c new file mode 100644 index 00000000000..99f9128bc66 --- /dev/null +++ b/gcc/testsuite/jit.dg/verify-dynamic-library.c @@ -0,0 +1,41 @@ +/* For use by jit-verify-dynamic-library, used by + test-compile-to-dynamic-library.c. */ +#include +#include +#include + +int +main (int argc, char **argv) +{ + void *handle; + void (*hello_world) (const char *name); + char *error; + + handle = dlopen ("./output-of-test-compile-to-dynamic-library.c.so", + RTLD_NOW | RTLD_LOCAL); + if (!handle) + { + fprintf (stderr, "dlopen failed: %s\n", dlerror()); + exit (1); + } + + /* Clear any existing error */ + dlerror (); + + /* This symbol is from the DSO built by + test-compile-to-dynamic-library.c. */ + *(void **) (&hello_world) = dlsym (handle, "hello_world"); + + if ((error = dlerror()) != NULL) + { + fprintf (stderr, "dlsym failed: %s\n", error); + exit (2); + } + + /* Call the function from the generated DSO. */ + hello_world (argv[0]); + + dlclose (handle); + + return 0; +} -- 2.30.2