Skip to content

Commit

Permalink
[vm/ffi] Remove deprecated methods
Browse files Browse the repository at this point in the history
Removes deprecated `load`, `store`, `allocate`, `free`, and `asExternalTypedData` methods from Pointer because these have been replace by extension methods and `package:ffi`.

Closes: #38860

Change-Id: I76d21bcfd2fdd6ee504a51db821345125beb92d2
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-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-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-reload-mac-release-simdbc64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121708
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
  • Loading branch information
dcharkes authored and commit-bot@chromium.org committed Oct 16, 2019
1 parent f1657ab commit bdf0202
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 384 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ vars = {
"term_glyph_tag": "1.0.1",
"test_reflective_loader_tag": "0.1.9",
"test_tag": "test-v1.6.4",
"tflite_native_rev": "4e8216d8fd3d5ccc8a56a056ec43d44cf99bf5c0",
"tflite_native_rev": "ff18d205bb4eb4db98439658066e72a238044e28",
"typed_data_tag": "1.1.6",
"unittest_rev": "2b8375bc98bb9dc81c539c91aaea6adce12e1072",
"usage_tag": "3.4.0",
Expand Down
1 change: 0 additions & 1 deletion pkg/front_end/lib/src/api_unstable/vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export '../fasta/fasta_codes.dart'
templateFfiStructGeneric,
templateFfiTypeInvalid,
templateFfiTypeMismatch,
templateFfiTypeUnsized,
templateIllegalRecursiveType;

export '../fasta/hybrid_file_system.dart' show HybridFileSystem;
Expand Down
32 changes: 0 additions & 32 deletions pkg/front_end/lib/src/fasta/fasta_codes_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4060,38 +4060,6 @@ Message _withArgumentsFfiTypeMismatch(
arguments: {'type': _type, 'type2': _type2, 'type3': _type3});
}

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Template<
Message Function(
String name,
DartType
_type)> templateFfiTypeUnsized = const Template<
Message Function(String name, DartType _type)>(
messageTemplate:
r"""Method '#name' cannot be called on something of type '#type' as this type is unsized.""",
withArguments: _withArgumentsFfiTypeUnsized);

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Message Function(String name, DartType _type)> codeFfiTypeUnsized =
const Code<Message Function(String name, DartType _type)>(
"FfiTypeUnsized",
templateFfiTypeUnsized,
);

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
Message _withArgumentsFfiTypeUnsized(String name, DartType _type) {
if (name.isEmpty) throw 'No name provided';
name = demangleMixinApplicationName(name);
TypeLabeler labeler = new TypeLabeler();
List<Object> typeParts = labeler.labelType(_type);
String type = typeParts.join();
return new Message(codeFfiTypeUnsized,
message:
"""Method '${name}' cannot be called on something of type '${type}' as this type is unsized.""" +
labeler.originMessages,
arguments: {'name': name, 'type': _type});
}

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeFieldInitializedOutsideDeclaringClass =
messageFieldInitializedOutsideDeclaringClass;
Expand Down
1 change: 0 additions & 1 deletion pkg/front_end/messages.status
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ FfiNotStatic/analyzerCode: Fail
FfiStructAnnotation/analyzerCode: Fail
FfiTypeInvalid/analyzerCode: Fail
FfiTypeMismatch/analyzerCode: Fail
FfiTypeUnsized/analyzerCode: Fail
FfiDartTypeMismatch/analyzerCode: Fail
FfiExtendsOrImplementsSealedClass/analyzerCode: Fail
FfiStructGeneric/analyzerCode: Fail
Expand Down
5 changes: 0 additions & 5 deletions pkg/front_end/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3475,11 +3475,6 @@ FfiTypeInvalid:
template: "Expected type '#type' to be a valid and instantiated subtype of 'NativeType'."
external: test/ffi_test.dart

FfiTypeUnsized:
# Used by dart:ffi
template: "Method '#name' cannot be called on something of type '#type' as this type is unsized."
external: test/ffi_test.dart

FfiFieldAnnotation:
# Used by dart:ffi
template: "Field '#name' requires exactly one annotation to declare its native type, which cannot be Void. dart:ffi Structs cannot have regular Dart fields."
Expand Down
4 changes: 0 additions & 4 deletions pkg/vm/lib/transformations/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ class FfiTransformer extends Transformer {
final Class pointerClass;
final Class structClass;
final Procedure castMethod;
final Procedure loadMethod;
final Procedure storeMethod;
final Procedure offsetByMethod;
final Procedure elementAtMethod;
final Procedure asFunctionMethod;
Expand Down Expand Up @@ -227,8 +225,6 @@ class FfiTransformer extends Transformer {
pointerClass = index.getClass('dart:ffi', 'Pointer'),
structClass = index.getClass('dart:ffi', 'Struct'),
castMethod = index.getMember('dart:ffi', 'Pointer', 'cast'),
loadMethod = index.getMember('dart:ffi', 'Pointer', 'load'),
storeMethod = index.getMember('dart:ffi', 'Pointer', 'store'),
offsetByMethod = index.getMember('dart:ffi', 'Pointer', '_offsetBy'),
elementAtMethod = index.getMember('dart:ffi', 'Pointer', 'elementAt'),
addressOfField = index.getMember('dart:ffi', 'Struct', '_addressOf'),
Expand Down
38 changes: 26 additions & 12 deletions pkg/vm/lib/transformations/ffi_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ class _FfiDefinitionTransformer extends FfiTransformer {
pointer = MethodInvocation(pointer, offsetByMethod.name,
Arguments([_runtimeBranchOnLayout(offsets)]), offsetByMethod);
}

final Class nativeClass = (nativeType as InterfaceType).classNode;
final NativeType nt = getType(nativeClass);
final typeArguments = [
if (nt == NativeType.kPointer && pointerType is InterfaceType)
pointerType.typeArguments[0]
];

final Procedure pointerGetter = Procedure(
pointerName,
ProcedureKind.Getter,
Expand All @@ -307,20 +315,24 @@ class _FfiDefinitionTransformer extends FfiTransformer {
returnType: pointerType));

// Sample output:
// double get x => _xPtr.load<double>();
// double get x => _xPtr.value;
final loadMethod =
optimizedTypes.contains(nt) ? loadMethods[nt] : loadStructMethod;
final Procedure getter = Procedure(
field.name,
ProcedureKind.Getter,
FunctionNode(
ReturnStatement(MethodInvocation(
PropertyGet(ThisExpression(), pointerName, pointerGetter),
loadMethod.name,
Arguments([], types: [field.type]),
loadMethod)),
ReturnStatement(StaticInvocation(
loadMethod,
Arguments([
PropertyGet(ThisExpression(), pointerName, pointerGetter),
ConstantExpression(IntConstant(0))
], types: typeArguments))),
returnType: field.type));

// Sample output:
// set x(double v) => _xPtr.store(v);
// set x(double v) { _xPtr.value = v; };
final storeMethod = storeMethods[nt];
Procedure setter = null;
if (!field.isFinal) {
final VariableDeclaration argument =
Expand All @@ -329,11 +341,13 @@ class _FfiDefinitionTransformer extends FfiTransformer {
field.name,
ProcedureKind.Setter,
FunctionNode(
ReturnStatement(MethodInvocation(
PropertyGet(ThisExpression(), pointerName, pointerGetter),
storeMethod.name,
Arguments([VariableGet(argument)]),
storeMethod)),
ReturnStatement(StaticInvocation(
storeMethod,
Arguments([
PropertyGet(ThisExpression(), pointerName, pointerGetter),
ConstantExpression(IntConstant(0)),
VariableGet(argument)
], types: typeArguments))),
returnType: VoidType(),
positionalParameters: [argument]));
}
Expand Down
102 changes: 2 additions & 100 deletions pkg/vm/lib/transformations/ffi_use_sites.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import 'package:front_end/src/api_unstable/vm.dart'
templateFfiExtendsOrImplementsSealedClass,
templateFfiNotStatic,
templateFfiTypeInvalid,
templateFfiTypeMismatch,
templateFfiTypeUnsized;
templateFfiTypeMismatch;

import 'package:kernel/ast.dart';
import 'package:kernel/class_hierarchy.dart' show ClassHierarchy;
Expand All @@ -25,13 +24,7 @@ import 'package:kernel/target/targets.dart' show DiagnosticReporter;
import 'package:kernel/type_environment.dart';

import 'ffi.dart'
show
ReplacedMembers,
NativeType,
kNativeTypeIntStart,
kNativeTypeIntEnd,
FfiTransformer,
optimizedTypes;
show ReplacedMembers, NativeType, FfiTransformer, optimizedTypes;

/// Checks and replaces calls to dart:ffi struct fields and methods.
void transformLibraries(
Expand Down Expand Up @@ -316,53 +309,6 @@ class _FfiUseSiteTransformer extends FfiTransformer {
(nativeType as InterfaceType).typeArguments[0];
return StaticInvocation(asFunctionInternal,
Arguments([node.receiver], types: [dartType, nativeSignature]));
} else if (target == loadMethod) {
final DartType dartType = node.arguments.types[0];
final DartType pointerType = node.receiver.getStaticType(env);
final DartType nativeType = _pointerTypeGetTypeArg(pointerType);

_ensureNativeTypeValid(pointerType, node);
_ensureNativeTypeValid(nativeType, node, allowStructs: true);
_ensureNativeTypeSized(nativeType, node, target.name);
_ensureNativeTypeToDartType(nativeType, dartType, node,
allowStructs: true);

// TODO(37773): When moving to extension methods we can get rid of
// this rewiring.
final Class nativeClass = (nativeType as InterfaceType).classNode;
final NativeType nt = getType(nativeClass);
final typeArguments = [
if (nt == NativeType.kPointer) _pointerTypeGetTypeArg(nativeType)
];
return StaticInvocation(
optimizedTypes.contains(nt) ? loadMethods[nt] : loadStructMethod,
Arguments([node.receiver, ConstantExpression(IntConstant(0))],
types: typeArguments));
} else if (target == storeMethod) {
final Expression storeValue = node.arguments.positional.single;
final DartType dartType = storeValue.getStaticType(env);
final DartType pointerType = node.receiver.getStaticType(env);
final DartType nativeType = _pointerTypeGetTypeArg(pointerType);

// TODO(36730): Allow storing an entire struct to memory.
// TODO(36780): Emit a better error message for the struct case.
_ensureNativeTypeValid(pointerType, node);
_ensureNativeTypeValid(nativeType, node);
_ensureNativeTypeSized(nativeType, node, target.name);
_ensureNativeTypeToDartType(nativeType, dartType, node);

// TODO(37773): When moving to extension methods we can get rid of
// this rewiring.
final Class nativeClass = (nativeType as InterfaceType).classNode;
final NativeType nt = getType(nativeClass);
final typeArguments = [
if (nt == NativeType.kPointer) _pointerTypeGetTypeArg(nativeType)
];
return StaticInvocation(
storeMethods[nt],
Arguments(
[node.receiver, ConstantExpression(IntConstant(0)), storeValue],
types: typeArguments));
} else if (target == elementAtMethod) {
// TODO(37773): When moving to extension methods we can get rid of
// this rewiring.
Expand Down Expand Up @@ -433,50 +379,6 @@ class _FfiUseSiteTransformer extends FfiTransformer {
return convertNativeTypeToDartType(nativeType, allowStructs) != null;
}

void _ensureNativeTypeSized(
DartType nativeType, Expression node, Name targetName) {
if (!_nativeTypeSized(nativeType)) {
diagnosticReporter.report(
templateFfiTypeUnsized.withArguments(targetName.name, nativeType),
node.fileOffset,
1,
node.location.file);
throw _FfiStaticTypeError();
}
}

/// Unsized NativeTypes do not support [sizeOf] because their size is unknown.
/// Consequently, [allocate], [Pointer.load], [Pointer.store], and
/// [Pointer.elementAt] are not available.
bool _nativeTypeSized(DartType nativeType) {
if (nativeType is! InterfaceType) {
return false;
}
final Class nativeClass = (nativeType as InterfaceType).classNode;
if (env.isSubtypeOf(InterfaceType(nativeClass), InterfaceType(pointerClass),
SubtypeCheckMode.ignoringNullabilities)) {
return true;
}
if (hierarchy.isSubclassOf(nativeClass, structClass)) {
return true;
}
final NativeType nativeType_ = getType(nativeClass);
if (nativeType_ == null) {
return false;
}
if (kNativeTypeIntStart.index <= nativeType_.index &&
nativeType_.index <= kNativeTypeIntEnd.index) {
return true;
}
if (nativeType_ == NativeType.kFloat || nativeType_ == NativeType.kDouble) {
return true;
}
if (nativeType_ == NativeType.kPointer) {
return true;
}
return false;
}

void _ensureIsStaticFunction(Expression node) {
if ((node is StaticGet && node.target is Procedure) ||
(node is ConstantExpression && node.constant is TearOffConstant)) {
Expand Down
28 changes: 0 additions & 28 deletions runtime/lib/ffi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,12 @@ static size_t SizeOf(const AbstractType& type) {

// The remainder of this file implements the dart:ffi native methods.

DEFINE_NATIVE_ENTRY(Ffi_allocate, 1, 1) {
GET_NATIVE_TYPE_ARGUMENT(type_arg, arguments->NativeTypeArgAt(0));

CheckSized(type_arg);
size_t element_size = SizeOf(type_arg);

GET_NON_NULL_NATIVE_ARGUMENT(Integer, argCount, arguments->NativeArgAt(0));
int64_t count = argCount.AsInt64Value();
size_t size = element_size * count; // Truncates overflow.
size_t memory = reinterpret_cast<size_t>(malloc(size));
if (memory == 0) {
const String& error = String::Handle(String::NewFormatted(
"allocating (%" Pd ") bytes of memory failed", size));
Exceptions::ThrowArgumentError(error);
}

RawPointer* result = Pointer::New(type_arg, memory);
return result;
}

DEFINE_NATIVE_ENTRY(Ffi_fromAddress, 1, 1) {
GET_NATIVE_TYPE_ARGUMENT(type_arg, arguments->NativeTypeArgAt(0));
GET_NON_NULL_NATIVE_ARGUMENT(Integer, arg_ptr, arguments->NativeArgAt(0));
return Pointer::New(type_arg, arg_ptr.AsInt64Value());
}

DEFINE_NATIVE_ENTRY(Ffi_free, 0, 1) {
GET_NON_NULL_NATIVE_ARGUMENT(Pointer, pointer, arguments->NativeArgAt(0));

free(reinterpret_cast<void*>(pointer.NativeAddress()));

return Object::null();
}

DEFINE_NATIVE_ENTRY(Ffi_address, 0, 1) {
GET_NON_NULL_NATIVE_ARGUMENT(Pointer, pointer, arguments->NativeArgAt(0));
return Integer::New(pointer.NativeAddress());
Expand Down
2 changes: 0 additions & 2 deletions runtime/vm/bootstrap_natives.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,6 @@ namespace dart {
V(VMService_RequestAssets, 0) \
V(VMService_DecodeAssets, 1) \
V(VMService_spawnUriNotify, 2) \
V(Ffi_allocate, 1) \
V(Ffi_free, 1) \
V(Ffi_loadInt8, 2) \
V(Ffi_loadInt16, 2) \
V(Ffi_loadInt32, 2) \
Expand Down
24 changes: 0 additions & 24 deletions sdk/lib/_internal/vm/lib/ffi_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ int sizeOf<T extends NativeType>() {

int _sizeOf<T extends NativeType>() native "Ffi_sizeOf";

Pointer<T> _allocate<T extends NativeType>(int count) native "Ffi_allocate";

// Implemented in the method recognizer, bytecode interpreter uses runtime.
Pointer<T> _fromAddress<T extends NativeType>(int ptr) native "Ffi_fromAddress";

Expand Down Expand Up @@ -73,9 +71,6 @@ Pointer<NS> _pointerFromFunction<NS extends NativeFunction>(Object function)
@patch
@pragma("vm:entry-point")
class Pointer<T extends NativeType> {
@patch
factory Pointer.allocate({int count: 1}) => _allocate<T>(count);

@patch
factory Pointer.fromAddress(int ptr) => _fromAddress(ptr);

Expand All @@ -92,18 +87,6 @@ class Pointer<T extends NativeType> {
"Pointer.fromFunction cannot be called dynamically.");
}

// TODO(sjindel): When NNBD is available, we should change `value` to be
// non-null.
// For statically known types, this is rewired.
@patch
void store(Object value) =>
throw UnsupportedError("Pointer.store cannot be called dynamically.");

// For statically known types, this is rewired.
@patch
R load<R>() =>
throw UnsupportedError("Pointer.load cannot be called dynamically.");

// Implemented in the method recognizer, bytecode interpreter uses runtime.
@patch
int get address native "Ffi_address";
Expand All @@ -125,13 +108,6 @@ class Pointer<T extends NativeType> {
R asFunction<R extends Function>() {
throw UnsupportedError("Pointer.asFunction cannot be called dynamically.");
}

@patch
void free() native "Ffi_free";

@patch
TypedData asExternalTypedData({int count: 1}) =>
_asExternalTypedData(this, count);
}

/// Returns an integer encoding the ABI used for size and alignment
Expand Down
Loading

0 comments on commit bdf0202

Please sign in to comment.