| 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 | #ifndef RUNTIME_VM_COMPILER_FFI_NATIVE_CALLING_CONVENTION_H_ |
| 6 | #define RUNTIME_VM_COMPILER_FFI_NATIVE_CALLING_CONVENTION_H_ |
| 7 | |
| 8 | #if defined(DART_PRECOMPILED_RUNTIME) |
| 9 | #error "AOT runtime should not use compiler sources (including header files)" |
| 10 | #endif // defined(DART_PRECOMPILED_RUNTIME) |
| 11 | |
| 12 | #include <platform/globals.h> |
| 13 | |
| 14 | #include "vm/compiler/backend/locations.h" |
| 15 | #include "vm/compiler/ffi/native_location.h" |
| 16 | #include "vm/compiler/ffi/native_type.h" |
| 17 | |
| 18 | namespace dart { |
| 19 | |
| 20 | namespace compiler { |
| 21 | |
| 22 | namespace ffi { |
| 23 | |
| 24 | using NativeLocations = ZoneGrowableArray<const NativeLocation*>; |
| 25 | |
| 26 | |
| 27 | // Values below 0 index result (result might be multiple if composite). |
| 28 | const intptr_t kResultIndex = -1; |
| 29 | |
| 30 | // Calculates native calling convention, is not aware of Dart calling |
| 31 | // convention constraints. |
| 32 | // |
| 33 | // This class is meant to be extended or embedded in a class that is aware |
| 34 | // of Dart calling convention constraints. |
| 35 | class NativeCallingConvention : public ZoneAllocated { |
| 36 | public: |
| 37 | NativeCallingConvention(Zone* zone, const Function& c_signature); |
| 38 | |
| 39 | // Excluding the #0 argument which is the function pointer. |
| 40 | intptr_t num_args() const; |
| 41 | |
| 42 | // The C Type (expressed in a Dart Type) of the argument at `arg_index`. |
| 43 | // |
| 44 | // Excluding the #0 argument which is the function pointer. |
| 45 | AbstractTypePtr CType(intptr_t arg_index) const; |
| 46 | |
| 47 | // The location of the argument at `arg_index`. |
| 48 | const NativeLocation& Location(intptr_t arg_index) const { |
| 49 | if (arg_index == kResultIndex) { |
| 50 | return result_loc_; |
| 51 | } |
| 52 | return *arg_locs_.At(arg_index); |
| 53 | } |
| 54 | |
| 55 | intptr_t StackTopInBytes() const; |
| 56 | |
| 57 | protected: |
| 58 | Zone* const zone_; |
| 59 | // Contains the function pointer as argument #0. |
| 60 | const Function& c_signature_; |
| 61 | const NativeLocations& arg_locs_; |
| 62 | const NativeLocation& result_loc_; |
| 63 | }; |
| 64 | |
| 65 | } // namespace ffi |
| 66 | |
| 67 | } // namespace compiler |
| 68 | |
| 69 | } // namespace dart |
| 70 | |
| 71 | #endif // RUNTIME_VM_COMPILER_FFI_NATIVE_CALLING_CONVENTION_H_ |
| 72 | |