2019-02-13 12:42:47 +00:00
|
|
|
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2019-08-07 13:10:30 +00:00
|
|
|
// All imports must be in all FFI patch files to not depend on the order
|
|
|
|
// the patches are applied.
|
2019-02-13 12:42:47 +00:00
|
|
|
import "dart:_internal" show patch;
|
2019-08-07 13:10:30 +00:00
|
|
|
import 'dart:typed_data' show TypedData;
|
2019-02-13 12:42:47 +00:00
|
|
|
|
|
|
|
@patch
|
|
|
|
int sizeOf<T extends NativeType>() native "Ffi_sizeOf";
|
|
|
|
|
2019-07-02 23:12:13 +00:00
|
|
|
Pointer<T> _allocate<T extends NativeType>(int count) native "Ffi_allocate";
|
|
|
|
|
|
|
|
Pointer<T> _fromAddress<T extends NativeType>(int ptr) native "Ffi_fromAddress";
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2019-07-05 21:59:27 +00:00
|
|
|
// The real implementation of this function (for interface calls) lives in
|
|
|
|
// BuildFfiAsFunctionCall in the Kernel frontend. No calls can actually reach
|
|
|
|
// this function.
|
|
|
|
DS _asFunctionInternal<DS extends Function, NS extends Function>(
|
|
|
|
Pointer<NativeFunction<NS>> ptr) native "Ffi_asFunctionInternal";
|
|
|
|
|
2019-08-07 13:10:30 +00:00
|
|
|
dynamic _asExternalTypedData(Pointer ptr, int count)
|
|
|
|
native "Ffi_asExternalTypedData";
|
|
|
|
|
2019-02-13 12:42:47 +00:00
|
|
|
@patch
|
|
|
|
@pragma("vm:entry-point")
|
|
|
|
class Pointer<T extends NativeType> {
|
2019-07-02 23:12:13 +00:00
|
|
|
@patch
|
|
|
|
factory Pointer.allocate({int count: 1}) => _allocate<T>(count);
|
|
|
|
|
|
|
|
@patch
|
|
|
|
factory Pointer.fromAddress(int ptr) => _fromAddress(ptr);
|
|
|
|
|
|
|
|
@patch
|
|
|
|
static Pointer<NativeFunction<T>> fromFunction<T extends Function>(
|
|
|
|
@DartRepresentationOf("T") Function f,
|
2019-09-12 00:10:09 +00:00
|
|
|
Object exceptionalReturn) native "Ffi_fromFunction";
|
2019-07-02 23:12:13 +00:00
|
|
|
|
|
|
|
// TODO(sjindel): When NNBD is available, we should change `value` to be
|
|
|
|
// non-null.
|
2019-02-13 12:42:47 +00:00
|
|
|
@patch
|
|
|
|
void store(Object value) native "Ffi_store";
|
|
|
|
|
|
|
|
@patch
|
|
|
|
R load<R>() native "Ffi_load";
|
|
|
|
|
|
|
|
@patch
|
|
|
|
int get address native "Ffi_address";
|
|
|
|
|
|
|
|
// Note this could also be implmented without an extra native as offsetBy
|
|
|
|
// (elementSize()*index). This would be 2 native calls rather than one. What
|
|
|
|
// would be better?
|
|
|
|
@patch
|
|
|
|
Pointer<T> elementAt(int index) native "Ffi_elementAt";
|
|
|
|
|
|
|
|
// Note this could also be implmented without an extra native as
|
|
|
|
// fromAddress(address). This would be 2 native calls rather than one.
|
|
|
|
// What would be better?
|
|
|
|
@patch
|
|
|
|
Pointer<T> offsetBy(int offsetInBytes) native "Ffi_offsetBy";
|
|
|
|
|
|
|
|
// Note this could also be implemented without an extra native as
|
|
|
|
// fromAddress(address). This would be 2 native calls rather than one.
|
|
|
|
// What would be better?
|
|
|
|
@patch
|
2019-07-02 23:12:13 +00:00
|
|
|
Pointer<U> cast<U extends NativeType>() native "Ffi_cast";
|
2019-02-13 12:42:47 +00:00
|
|
|
|
|
|
|
@patch
|
2019-07-05 21:59:27 +00:00
|
|
|
R asFunction<R extends Function>() {
|
|
|
|
throw UnsupportedError("Pointer.asFunction cannot be called dynamically.");
|
|
|
|
}
|
2019-02-13 12:42:47 +00:00
|
|
|
|
|
|
|
@patch
|
|
|
|
void free() native "Ffi_free";
|
2019-08-07 13:10:30 +00:00
|
|
|
|
|
|
|
@patch
|
|
|
|
TypedData asExternalTypedData({int count: 1}) =>
|
|
|
|
_asExternalTypedData(this, count);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
Reland "[vm/ffi] Support structs on 32bit architectures"
Fixes alignment checks on GCC <= 7 and Clang <= 7.
Fixes: https://github.com/dart-lang/sdk/issues/37559
Fixes: https://github.com/dart-lang/sdk/issues/36334
Original CL is in patchset 1.
Tested GCC 7 and Clang 7 behavior of alignof and offsetof manually (our CQ uses newer tooling).
Change-Id: I828b00ad3d36be68a0d28b0a710fea938ca17784
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try, app-kernel-linux-debug-x64-try, vm-kernel-linux-debug-simdbc64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-ffi-android-product-arm-try,vm-dartkb-linux-release-x64-abi-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112254
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2019-08-09 14:14:32 +00:00
|
|
|
|
|
|
|
// Returns the ABI used for size and alignment calculations.
|
|
|
|
// See pkg/vm/lib/transformations/ffi.dart.
|
|
|
|
@pragma('vm:prefer-inline')
|
|
|
|
int _abi()
|
|
|
|
native "Recognized method: method is directly interpreted by the bytecode interpreter or IR graph is built in the flow graph builder.";
|