From e735281b2cd212b5f83b86ff787a76cae6d42593 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Fri, 10 Dec 2021 11:24:59 +0100 Subject: [PATCH] Thunks: Use libclang-based code generation for libxcb-present --- ThunkLibs/Generators/libxcb_present.py | 38 ------ ThunkLibs/GuestLibs/CMakeLists.txt | 4 +- ThunkLibs/HostLibs/CMakeLists.txt | 4 +- ThunkLibs/libxcb-present/Guest.cpp | 110 ++++++++++++++++ .../Host.cpp | 38 +++--- .../libxcb-present_interface.cpp | 40 ++++++ ThunkLibs/libxcb_present/Guest.cpp | 123 ------------------ 7 files changed, 170 insertions(+), 187 deletions(-) delete mode 100644 ThunkLibs/Generators/libxcb_present.py create mode 100644 ThunkLibs/libxcb-present/Guest.cpp rename ThunkLibs/{libxcb_present => libxcb-present}/Host.cpp (55%) create mode 100644 ThunkLibs/libxcb-present/libxcb-present_interface.cpp delete mode 100644 ThunkLibs/libxcb_present/Guest.cpp diff --git a/ThunkLibs/Generators/libxcb_present.py b/ThunkLibs/Generators/libxcb_present.py deleted file mode 100644 index 2cef5febf..000000000 --- a/ThunkLibs/Generators/libxcb_present.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/python3 -from ThunkHelpers import * - -lib_with_filename("libxcb_present", "0", "libxcb-present") -# FEX -fn("void FEX_xcb_present_init_extension(xcb_connection_t *, xcb_extension_t *)"); no_unpack() -fn("size_t FEX_usable_size(void*)"); no_unpack() -fn("void FEX_free_on_host(void*)"); no_unpack() - -fn("void xcb_present_notify_next(xcb_present_notify_iterator_t *)") -fn("xcb_generic_iterator_t xcb_present_notify_end(xcb_present_notify_iterator_t)") -fn("xcb_present_query_version_cookie_t xcb_present_query_version(xcb_connection_t *, uint32_t, uint32_t)"); no_pack() -fn("xcb_present_query_version_cookie_t xcb_present_query_version_unchecked(xcb_connection_t *, uint32_t, uint32_t)"); no_pack() -fn("xcb_present_query_version_reply_t * xcb_present_query_version_reply(xcb_connection_t *, xcb_present_query_version_cookie_t, xcb_generic_error_t **)"); no_pack() -fn("int xcb_present_pixmap_sizeof(const void *, uint32_t)") -fn("xcb_void_cookie_t xcb_present_pixmap_checked(xcb_connection_t *, xcb_window_t, xcb_pixmap_t, uint32_t, xcb_xfixes_region_t, xcb_xfixes_region_t, int16_t, int16_t, xcb_randr_crtc_t, xcb_sync_fence_t, xcb_sync_fence_t, uint32_t, uint64_t, uint64_t, uint64_t, uint32_t, const xcb_present_notify_t *)") -fn("xcb_void_cookie_t xcb_present_pixmap(xcb_connection_t *, xcb_window_t, xcb_pixmap_t, uint32_t, xcb_xfixes_region_t, xcb_xfixes_region_t, int16_t, int16_t, xcb_randr_crtc_t, xcb_sync_fence_t, xcb_sync_fence_t, uint32_t, uint64_t, uint64_t, uint64_t, uint32_t, const xcb_present_notify_t *)") -# ::Iterator:: -fn("xcb_present_notify_t * xcb_present_pixmap_notifies(const xcb_present_pixmap_request_t *)") -fn("int xcb_present_pixmap_notifies_length(const xcb_present_pixmap_request_t *)") -fn("xcb_present_notify_iterator_t xcb_present_pixmap_notifies_iterator(const xcb_present_pixmap_request_t *)") - -fn("xcb_void_cookie_t xcb_present_notify_msc_checked(xcb_connection_t *, xcb_window_t, uint32_t, uint64_t, uint64_t, uint64_t)") -fn("xcb_void_cookie_t xcb_present_notify_msc(xcb_connection_t *, xcb_window_t, uint32_t, uint64_t, uint64_t, uint64_t)") -fn("void xcb_present_event_next(xcb_present_event_iterator_t *)") -fn("xcb_generic_iterator_t xcb_present_event_end(xcb_present_event_iterator_t)") -fn("xcb_void_cookie_t xcb_present_select_input_checked(xcb_connection_t *, xcb_present_event_t, xcb_window_t, uint32_t)") -fn("xcb_void_cookie_t xcb_present_select_input(xcb_connection_t *, xcb_present_event_t, xcb_window_t, uint32_t)") -fn("xcb_present_query_capabilities_cookie_t xcb_present_query_capabilities(xcb_connection_t *, uint32_t)") -fn("xcb_present_query_capabilities_cookie_t xcb_present_query_capabilities_unchecked(xcb_connection_t *, uint32_t)") -fn("xcb_present_query_capabilities_reply_t * xcb_present_query_capabilities_reply(xcb_connection_t *, xcb_present_query_capabilities_cookie_t, xcb_generic_error_t **)"); no_pack(); -fn("int xcb_present_redirect_notify_sizeof(const void *, uint32_t)") -# ::Iterator:: -fn("xcb_present_notify_t * xcb_present_redirect_notify_notifies(const xcb_present_redirect_notify_event_t *)") -fn("int xcb_present_redirect_notify_notifies_length(const xcb_present_redirect_notify_event_t *)") -fn("xcb_present_notify_iterator_t xcb_present_redirect_notify_notifies_iterator(const xcb_present_redirect_notify_event_t *)") - -Generate() diff --git a/ThunkLibs/GuestLibs/CMakeLists.txt b/ThunkLibs/GuestLibs/CMakeLists.txt index a5cce3043..807fe815b 100644 --- a/ThunkLibs/GuestLibs/CMakeLists.txt +++ b/ThunkLibs/GuestLibs/CMakeLists.txt @@ -258,8 +258,8 @@ add_guest_lib(xcb_shm) generate(libxcb_sync thunks function_packs function_packs_public) add_guest_lib(xcb_sync) -generate(libxcb_present thunks function_packs function_packs_public) -add_guest_lib(xcb_present) +generate_new(libxcb-present ${CMAKE_CURRENT_SOURCE_DIR}/../libxcb-present/libxcb-present_interface.cpp thunks function_packs function_packs_public) +add_guest_lib(xcb-present) generate(libxcb_randr thunks function_packs function_packs_public) add_guest_lib(xcb_randr) diff --git a/ThunkLibs/HostLibs/CMakeLists.txt b/ThunkLibs/HostLibs/CMakeLists.txt index c00093be0..f5a93df92 100644 --- a/ThunkLibs/HostLibs/CMakeLists.txt +++ b/ThunkLibs/HostLibs/CMakeLists.txt @@ -179,8 +179,8 @@ add_host_lib(xcb_shm) generate(libxcb_sync function_unpacks tab_function_unpacks ldr ldr_ptrs) add_host_lib(xcb_sync) -generate(libxcb_present function_unpacks tab_function_unpacks ldr ldr_ptrs) -add_host_lib(xcb_present) +generate_new(libxcb-present ${CMAKE_CURRENT_SOURCE_DIR}/../libxcb-present/libxcb-present_interface.cpp function_unpacks tab_function_unpacks ldr ldr_ptrs) +add_host_lib(xcb-present) generate(libxcb_randr function_unpacks tab_function_unpacks ldr ldr_ptrs) add_host_lib(xcb_randr) diff --git a/ThunkLibs/libxcb-present/Guest.cpp b/ThunkLibs/libxcb-present/Guest.cpp new file mode 100644 index 000000000..a955f52a5 --- /dev/null +++ b/ThunkLibs/libxcb-present/Guest.cpp @@ -0,0 +1,110 @@ +/* +$info$ +tags: thunklibs|xcb-present +$end_info$ +*/ + +#include +#include + +#include +#include +#include +#include + +#include "common/Guest.h" +#include + +#include "thunks.inl" +#include "function_packs.inl" +#include "function_packs_public.inl" + +extern "C" { + xcb_extension_t xcb_present_id = { + .name = "Present", + .global_id = 0, + }; + + void FEX_malloc_free_on_host(void *Ptr) { + struct {void *p;} args; + args.p = Ptr; + fexthunks_libxcb_present_FEX_free_on_host(&args); + } + + size_t FEX_malloc_usable_size(void *Ptr) { + struct {void *p; size_t rv;} args; + args.p = Ptr; + fexthunks_libxcb_present_FEX_usable_size(&args); + return args.rv; + } + + static void InitializeExtensions(xcb_connection_t *c) { + FEX_xcb_present_init_extension(c, &xcb_present_id); + } + + xcb_present_query_version_cookie_t xcb_present_query_version(xcb_connection_t * a_0,uint32_t a_1,uint32_t a_2){ + auto ret = fexfn_pack_xcb_present_query_version(a_0, a_1, a_2); + InitializeExtensions(a_0); + return ret; + } + + xcb_present_query_version_cookie_t xcb_present_query_version_unchecked(xcb_connection_t * a_0,uint32_t a_1,uint32_t a_2){ + auto ret = fexfn_pack_xcb_present_query_version_unchecked(a_0, a_1, a_2); + InitializeExtensions(a_0); + return ret; + } + + xcb_present_query_version_reply_t * xcb_present_query_version_reply(xcb_connection_t * a_0,xcb_present_query_version_cookie_t a_1,xcb_generic_error_t ** a_2){ + auto ret = fexfn_pack_xcb_present_query_version_reply(a_0, a_1, a_2); + + // We now need to do some fixups here + if (a_2 && *a_2) { + // If the error code pointer exists then we need to copy the contents and free the host facing pointer + xcb_generic_error_t *NewError = (xcb_generic_error_t *)malloc(sizeof(xcb_generic_error_t)); + memcpy(NewError, *a_2, sizeof(xcb_generic_error_t)); + FEX_malloc_free_on_host(*a_2); + + // User is expected to free this + *a_2 = NewError; + } + + if (ret) { + constexpr size_t ResultSize = sizeof(std::remove_pointer::type); + void *NewPtr = malloc(ResultSize); + memcpy(NewPtr, ret, ResultSize); + + FEX_malloc_free_on_host(ret); + ret = (decltype(ret))NewPtr; + } + + return ret; + } + + xcb_present_query_capabilities_reply_t * xcb_present_query_capabilities_reply(xcb_connection_t * a_0,xcb_present_query_capabilities_cookie_t a_1,xcb_generic_error_t ** a_2){ + auto ret = fexfn_pack_xcb_present_query_capabilities_reply(a_0, a_1, a_2); + + // We now need to do some fixups here + if (a_2 && *a_2) { + // If the error code pointer exists then we need to copy the contents and free the host facing pointer + xcb_generic_error_t *NewError = (xcb_generic_error_t *)malloc(sizeof(xcb_generic_error_t)); + memcpy(NewError, *a_2, sizeof(xcb_generic_error_t)); + FEX_malloc_free_on_host(*a_2); + + // User is expected to free this + *a_2 = NewError; + } + + if (ret) { + constexpr size_t ResultSize = sizeof(std::remove_pointer::type); + void *NewPtr = malloc(ResultSize); + memcpy(NewPtr, ret, ResultSize); + + FEX_malloc_free_on_host(ret); + ret = (decltype(ret))NewPtr; + } + + return ret; + } +} + +LOAD_LIB(libxcb_present) diff --git a/ThunkLibs/libxcb_present/Host.cpp b/ThunkLibs/libxcb-present/Host.cpp similarity index 55% rename from ThunkLibs/libxcb_present/Host.cpp rename to ThunkLibs/libxcb-present/Host.cpp index b9589706a..d0b3b878a 100644 --- a/ThunkLibs/libxcb_present/Host.cpp +++ b/ThunkLibs/libxcb-present/Host.cpp @@ -15,23 +15,19 @@ $end_info$ #include #include "ldr_ptrs.inl" + +static void fexfn_impl_libxcb_present_FEX_xcb_present_init_extension(xcb_connection_t * a_0, xcb_extension_t * a_1); + +static size_t fexfn_impl_libxcb_present_FEX_usable_size(void *a_0){ + return malloc_usable_size(a_0); +} + +static void fexfn_impl_libxcb_present_FEX_free_on_host(void *a_0){ + free(a_0); +} + #include "function_unpacks.inl" - -void fexfn_unpack_libxcb_present_FEX_xcb_present_init_extension(void *argsv); - -void fexfn_unpack_libxcb_present_FEX_usable_size(void *argsv){ - struct arg_t {void* a_0;size_t rv;}; - auto args = (arg_t*)argsv; - args->rv = malloc_usable_size(args->a_0); -} - -void fexfn_unpack_libxcb_present_FEX_free_on_host(void *argsv){ - struct arg_t {void* a_0;}; - auto args = (arg_t*)argsv; - free(args->a_0); -} - static ExportEntry exports[] = { #include "tab_function_unpacks.inl" { nullptr, nullptr } @@ -39,15 +35,13 @@ static ExportEntry exports[] = { #include "ldr.inl" -void fexfn_unpack_libxcb_present_FEX_xcb_present_init_extension(void *argsv){ - struct arg_t {xcb_connection_t * a_0;xcb_extension_t * a_1;}; - auto args = (arg_t*)argsv; +static void fexfn_impl_libxcb_present_FEX_xcb_present_init_extension(xcb_connection_t * a_0, xcb_extension_t * a_1) { xcb_extension_t *ext{}; - if (strcmp(args->a_1->name, "Present") == 0) { + if (strcmp(a_1->name, "Present") == 0) { ext = (xcb_extension_t *)dlsym(fexldr_ptr_libxcb_present_so, "xcb_present_id"); } else { - fprintf(stderr, "Unknown xcb extension '%s'\n", args->a_1->name); + fprintf(stderr, "Unknown xcb extension '%s'\n", a_1->name); __builtin_trap(); return; } @@ -57,10 +51,10 @@ void fexfn_unpack_libxcb_present_FEX_xcb_present_init_extension(void *argsv){ fexldr_ptr_libxcb_xcb_get_extension_data = (fexldr_type_libxcb_xcb_get_extension_data*)dlsym(RTLD_DEFAULT, "xcb_get_extension_data"); - auto res = fexldr_ptr_libxcb_xcb_get_extension_data(args->a_0, ext); + auto res = fexldr_ptr_libxcb_xcb_get_extension_data(a_0, ext); // Copy over the global id - args->a_1->global_id = ext->global_id; + a_1->global_id = ext->global_id; } EXPORTS(libxcb_present) diff --git a/ThunkLibs/libxcb-present/libxcb-present_interface.cpp b/ThunkLibs/libxcb-present/libxcb-present_interface.cpp new file mode 100644 index 000000000..3e9310037 --- /dev/null +++ b/ThunkLibs/libxcb-present/libxcb-present_interface.cpp @@ -0,0 +1,40 @@ +#include + +#include + +template +struct fex_gen_config; + +void FEX_xcb_present_init_extension(xcb_connection_t*, xcb_extension_t*); +size_t FEX_usable_size(void*); +void FEX_free_on_host(void*); + +template<> struct fex_gen_config : fexgen::custom_host_impl {}; +template<> struct fex_gen_config : fexgen::custom_host_impl, fexgen::custom_guest_entrypoint {}; +template<> struct fex_gen_config : fexgen::custom_host_impl, fexgen::custom_guest_entrypoint {}; + +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config : fexgen::custom_guest_entrypoint {}; +template<> struct fex_gen_config : fexgen::custom_guest_entrypoint {}; +template<> struct fex_gen_config : fexgen::custom_guest_entrypoint {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; + +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config : fexgen::custom_guest_entrypoint {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; +template<> struct fex_gen_config {}; diff --git a/ThunkLibs/libxcb_present/Guest.cpp b/ThunkLibs/libxcb_present/Guest.cpp deleted file mode 100644 index 7baeb2ded..000000000 --- a/ThunkLibs/libxcb_present/Guest.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* -$info$ -tags: thunklibs|xcb-present -$end_info$ -*/ - -#include -#include - -#include -#include -#include -#include - -#include "common/Guest.h" -#include - -#include "thunks.inl" -#include "function_packs.inl" -#include "function_packs_public.inl" - -extern "C" { - xcb_extension_t xcb_present_id = { - .name = "Present", - .global_id = 0, - }; - - void FEX_malloc_free_on_host(void *Ptr) { - struct {void *p;} args; - args.p = Ptr; - fexthunks_libxcb_present_FEX_free_on_host(&args); - } - - size_t FEX_malloc_usable_size(void *Ptr) { - struct {void *p; size_t rv;} args; - args.p = Ptr; - fexthunks_libxcb_present_FEX_usable_size(&args); - return args.rv; - } - - static void InitializeExtensions(xcb_connection_t *c) { - FEX_xcb_present_init_extension(c, &xcb_present_id); - } - - static xcb_present_query_version_cookie_t fexfn_pack_xcb_present_query_version(xcb_connection_t * a_0,uint32_t a_1,uint32_t a_2){ - struct {xcb_connection_t * a_0;uint32_t a_1;uint32_t a_2;xcb_present_query_version_cookie_t rv;} args; - args.a_0 = a_0;args.a_1 = a_1;args.a_2 = a_2; - fexthunks_libxcb_present_xcb_present_query_version(&args); - InitializeExtensions(a_0); - return args.rv; - } - - static xcb_present_query_version_cookie_t fexfn_pack_xcb_present_query_version_unchecked(xcb_connection_t * a_0,uint32_t a_1,uint32_t a_2){ - struct {xcb_connection_t * a_0;uint32_t a_1;uint32_t a_2;xcb_present_query_version_cookie_t rv;} args; - args.a_0 = a_0;args.a_1 = a_1;args.a_2 = a_2; - fexthunks_libxcb_present_xcb_present_query_version_unchecked(&args); - InitializeExtensions(a_0); - return args.rv; - } - - static xcb_present_query_version_reply_t * fexfn_pack_xcb_present_query_version_reply(xcb_connection_t * a_0,xcb_present_query_version_cookie_t a_1,xcb_generic_error_t ** a_2){ - struct {xcb_connection_t * a_0;xcb_present_query_version_cookie_t a_1;xcb_generic_error_t ** a_2;xcb_present_query_version_reply_t * rv;} args; - args.a_0 = a_0;args.a_1 = a_1;args.a_2 = a_2; - fexthunks_libxcb_present_xcb_present_query_version_reply(&args); - - // We now need to do some fixups here - if (a_2 && *a_2) { - // If the error code pointer exists then we need to copy the contents and free the host facing pointer - xcb_generic_error_t *NewError = (xcb_generic_error_t *)malloc(sizeof(xcb_generic_error_t)); - memcpy(NewError, *a_2, sizeof(xcb_generic_error_t)); - FEX_malloc_free_on_host(*a_2); - - // User is expected to free this - *a_2 = NewError; - } - - if (args.rv) { - constexpr size_t ResultSize = sizeof(std::remove_pointer::type); - void *NewPtr = malloc(ResultSize); - memcpy(NewPtr, args.rv, ResultSize); - - FEX_malloc_free_on_host(args.rv); - args.rv = (decltype(args.rv))NewPtr; - } - - return args.rv; - } - - static xcb_present_query_capabilities_reply_t * fexfn_pack_xcb_present_query_capabilities_reply(xcb_connection_t * a_0,xcb_present_query_capabilities_cookie_t a_1,xcb_generic_error_t ** a_2){ - struct {xcb_connection_t * a_0;xcb_present_query_capabilities_cookie_t a_1;xcb_generic_error_t ** a_2;xcb_present_query_capabilities_reply_t * rv;} args; - args.a_0 = a_0;args.a_1 = a_1;args.a_2 = a_2; - fexthunks_libxcb_present_xcb_present_query_capabilities_reply(&args); - - // We now need to do some fixups here - if (a_2 && *a_2) { - // If the error code pointer exists then we need to copy the contents and free the host facing pointer - xcb_generic_error_t *NewError = (xcb_generic_error_t *)malloc(sizeof(xcb_generic_error_t)); - memcpy(NewError, *a_2, sizeof(xcb_generic_error_t)); - FEX_malloc_free_on_host(*a_2); - - // User is expected to free this - *a_2 = NewError; - } - - if (args.rv) { - constexpr size_t ResultSize = sizeof(std::remove_pointer::type); - void *NewPtr = malloc(ResultSize); - memcpy(NewPtr, args.rv, ResultSize); - - FEX_malloc_free_on_host(args.rv); - args.rv = (decltype(args.rv))NewPtr; - } - - return args.rv; - } - - xcb_present_query_version_cookie_t xcb_present_query_version(xcb_connection_t * a_0,uint32_t a_1,uint32_t a_2) __attribute__((alias("fexfn_pack_xcb_present_query_version"))); - xcb_present_query_version_cookie_t xcb_present_query_version_unchecked(xcb_connection_t * a_0,uint32_t a_1,uint32_t a_2) __attribute__((alias("fexfn_pack_xcb_present_query_version_unchecked"))); - xcb_present_query_version_reply_t * xcb_present_query_version_reply(xcb_connection_t * a_0,xcb_present_query_version_cookie_t a_1,xcb_generic_error_t ** a_2) __attribute__((alias("fexfn_pack_xcb_present_query_version_reply"))); - xcb_present_query_capabilities_reply_t * xcb_present_query_capabilities_reply(xcb_connection_t * a_0,xcb_present_query_capabilities_cookie_t a_1,xcb_generic_error_t ** a_2) __attribute__((alias("fexfn_pack_xcb_present_query_capabilities_reply"))); -} - -LOAD_LIB(libxcb_present)