| 1 | // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "vm/compiler/ffi/abi.h" |
| 6 | |
| 7 | #include "vm/constants.h" |
| 8 | |
| 9 | namespace dart { |
| 10 | |
| 11 | namespace compiler { |
| 12 | |
| 13 | namespace ffi { |
| 14 | |
| 15 | // See pkg/vm/lib/transformations/ffi.dart, which makes these assumptions. |
| 16 | struct AbiAlignmentDouble { |
| 17 | int8_t use_one_byte; |
| 18 | double d; |
| 19 | }; |
| 20 | struct AbiAlignmentUint64 { |
| 21 | int8_t use_one_byte; |
| 22 | uint64_t i; |
| 23 | }; |
| 24 | |
| 25 | #if defined(HOST_ARCH_X64) || defined(HOST_ARCH_ARM64) |
| 26 | static_assert(offsetof(AbiAlignmentDouble, d) == 8, |
| 27 | "FFI transformation alignment" ); |
| 28 | static_assert(offsetof(AbiAlignmentUint64, i) == 8, |
| 29 | "FFI transformation alignment" ); |
| 30 | #elif (defined(HOST_ARCH_IA32) && /* NOLINT(whitespace/parens) */ \ |
| 31 | (defined(HOST_OS_LINUX) || defined(HOST_OS_MACOS) || \ |
| 32 | defined(HOST_OS_ANDROID))) || \ |
| 33 | (defined(HOST_ARCH_ARM) && defined(HOST_OS_IOS)) |
| 34 | static_assert(offsetof(AbiAlignmentDouble, d) == 4, |
| 35 | "FFI transformation alignment" ); |
| 36 | static_assert(offsetof(AbiAlignmentUint64, i) == 4, |
| 37 | "FFI transformation alignment" ); |
| 38 | #elif defined(HOST_ARCH_IA32) && defined(HOST_OS_WINDOWS) || \ |
| 39 | defined(HOST_ARCH_ARM) |
| 40 | static_assert(offsetof(AbiAlignmentDouble, d) == 8, |
| 41 | "FFI transformation alignment" ); |
| 42 | static_assert(offsetof(AbiAlignmentUint64, i) == 8, |
| 43 | "FFI transformation alignment" ); |
| 44 | #else |
| 45 | #error "Unknown platform. Please add alignment requirements for ABI." |
| 46 | #endif |
| 47 | |
| 48 | Abi TargetAbi() { |
| 49 | #if defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64) |
| 50 | return Abi::kWordSize64; |
| 51 | #elif (defined(TARGET_ARCH_IA32) && /* NOLINT(whitespace/parens) */ \ |
| 52 | (defined(TARGET_OS_LINUX) || defined(TARGET_OS_MACOS) || \ |
| 53 | defined(TARGET_OS_ANDROID))) || \ |
| 54 | (defined(TARGET_ARCH_ARM) && defined(TARGET_OS_IOS)) |
| 55 | return Abi::kWordSize32Align32; |
| 56 | #elif defined(TARGET_ARCH_IA32) && defined(TARGET_OS_WINDOWS) || \ |
| 57 | defined(TARGET_ARCH_ARM) |
| 58 | return Abi::kWordSize32Align64; |
| 59 | #else |
| 60 | #error "Unknown platform. Please add alignment requirements for ABI." |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | } // namespace ffi |
| 65 | |
| 66 | } // namespace compiler |
| 67 | |
| 68 | } // namespace dart |
| 69 | |