| 1 | //===--- generic-elf-64bit/dynamic_ffi/ffi.cpp -------------------- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Provides a mirror to the parts of the FFI interface that the plugins require. |
| 10 | // |
| 11 | // libffi |
| 12 | // - Copyright (c) 2011, 2014, 2019, 2021, 2022 Anthony Green |
| 13 | // - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef DYNAMIC_FFI_FFI_H |
| 18 | #define DYNAMIC_FFI_FFI_H |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #define USES_DYNAMIC_FFI |
| 24 | |
| 25 | uint32_t ffi_init(); |
| 26 | |
| 27 | typedef struct _ffi_type { |
| 28 | size_t size; |
| 29 | unsigned short alignment; |
| 30 | unsigned short type; |
| 31 | struct _ffi_type **elements; |
| 32 | } ffi_type; |
| 33 | |
| 34 | typedef enum { |
| 35 | FFI_OK = 0, |
| 36 | FFI_BAD_TYPEDEF, |
| 37 | FFI_BAD_ABI, |
| 38 | FFI_BAD_ARGTYPE |
| 39 | } ffi_status; |
| 40 | |
| 41 | // These are target dependent so we set them manually for each ABI by |
| 42 | // referencing the FFI source. |
| 43 | typedef enum ffi_abi { |
| 44 | #if (defined(_M_X64) || defined(__x86_64__)) |
| 45 | FFI_DEFAULT_ABI = 2, // FFI_UNIX64. |
| 46 | #elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || \ |
| 47 | defined(__riscv) || defined(__loongarch__) |
| 48 | FFI_DEFAULT_ABI = 1, // FFI_SYSV. |
| 49 | #elif defined(__powerpc64__) |
| 50 | FFI_DEFAULT_ABI = 8, // FFI_LINUX. |
| 51 | #elif defined(__s390x__) |
| 52 | FFI_DEFAULT_ABI = 1, // FFI_SYSV. |
| 53 | #else |
| 54 | #error "Unknown ABI" |
| 55 | #endif |
| 56 | } ffi_abi; |
| 57 | |
| 58 | typedef struct { |
| 59 | ffi_abi abi; |
| 60 | unsigned nargs; |
| 61 | ffi_type **arg_types; |
| 62 | ffi_type *rtype; |
| 63 | unsigned bytes; |
| 64 | unsigned flags; |
| 65 | long long ; // Longest extra field defined in the FFI sources |
| 66 | } ffi_cif; |
| 67 | |
| 68 | #ifdef __cplusplus |
| 69 | extern "C" { |
| 70 | #endif |
| 71 | |
| 72 | #define FFI_EXTERN extern |
| 73 | #define FFI_API |
| 74 | |
| 75 | FFI_EXTERN ffi_type ffi_type_void; |
| 76 | FFI_EXTERN ffi_type ffi_type_pointer; |
| 77 | |
| 78 | FFI_API |
| 79 | void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue); |
| 80 | |
| 81 | FFI_API |
| 82 | ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs, |
| 83 | ffi_type *rtype, ffi_type **atypes); |
| 84 | |
| 85 | #ifdef __cplusplus |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | #endif // DYNAMIC_FFI_FFI_H |
| 90 | |