[vm/ffi] Refactor a few duplicated AST patterns in FFI transformer
This change is a pure refactoring. It extracts 3 duplicated code
snippets into helper methods. This is needed to reduce number of
places where PropertyGet and MethodInvocation nodes are created, as
these nodes are going to be replaced with InstanceGet and
InstanceInvocation nodes soon.
Issue: https://github.com/dart-lang/sdk/issues/45340
Change-Id: I694805a3761fd389ac8ee005d12ffb9bb9543ea7
TEST=ci
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/198581
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
diff --git a/pkg/vm/lib/transformations/ffi.dart b/pkg/vm/lib/transformations/ffi.dart
index 558f4da..a4c6fd4 100644
--- a/pkg/vm/lib/transformations/ffi.dart
+++ b/pkg/vm/lib/transformations/ffi.dart
@@ -784,6 +784,25 @@
InterfaceType(compoundClass, Nullability.legacy),
SubtypeCheckMode.ignoringNullabilities);
}
+
+ Expression getCompoundTypedDataBaseField(
+ Expression receiver, int fileOffset) {
+ return PropertyGet(
+ receiver, compoundTypedDataBaseField.name, compoundTypedDataBaseField)
+ ..fileOffset = fileOffset;
+ }
+
+ Expression getArrayTypedDataBaseField(Expression receiver,
+ [int fileOffset = TreeNode.noOffset]) {
+ return PropertyGet(
+ receiver, arrayTypedDataBaseField.name, arrayTypedDataBaseField)
+ ..fileOffset = fileOffset;
+ }
+
+ Expression multiply(Expression a, Expression b) {
+ return MethodInvocation(
+ a, numMultiplication.name, Arguments([b]), numMultiplication);
+ }
}
/// Checks if any library depends on dart:ffi.
diff --git a/pkg/vm/lib/transformations/ffi_definitions.dart b/pkg/vm/lib/transformations/ffi_definitions.dart
index e69b8a1..f841be3 100644
--- a/pkg/vm/lib/transformations/ffi_definitions.dart
+++ b/pkg/vm/lib/transformations/ffi_definitions.dart
@@ -1033,11 +1033,8 @@
? transformer.loadUnalignedMethods
: transformer.loadMethods)[nativeType],
Arguments([
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets)
]))
..fileOffset = fileOffset);
@@ -1060,11 +1057,8 @@
? transformer.storeUnalignedMethods
: transformer.storeMethods)[nativeType],
Arguments([
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets),
VariableGet(argument)
]))
@@ -1103,11 +1097,8 @@
StaticInvocation(
transformer.loadMethods[NativeType.kIntptr],
Arguments([
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets)
]))
..fileOffset = fileOffset
@@ -1132,11 +1123,8 @@
ReturnStatement(StaticInvocation(
transformer.storeMethods[NativeType.kIntptr],
Arguments([
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets),
PropertyGet(VariableGet(argument), transformer.addressGetter.name,
transformer.addressGetter)
@@ -1183,11 +1171,8 @@
constructor,
Arguments([
transformer.typedDataBaseOffset(
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets),
transformer.runtimeBranchOnLayout(size),
dartType,
@@ -1212,17 +1197,11 @@
ReturnStatement(StaticInvocation(
transformer.memCopy,
Arguments([
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets),
- PropertyGet(
- VariableGet(argument),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ VariableGet(argument), fileOffset),
ConstantExpression(IntConstant(0)),
transformer.runtimeBranchOnLayout(size),
]))
@@ -1367,11 +1346,8 @@
transformer.arrayConstructor,
Arguments([
transformer.typedDataBaseOffset(
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets),
transformer.runtimeBranchOnLayout(size),
typeArgument,
@@ -1400,17 +1376,11 @@
ReturnStatement(StaticInvocation(
transformer.memCopy,
Arguments([
- PropertyGet(
- ThisExpression(),
- transformer.compoundTypedDataBaseField.name,
- transformer.compoundTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getCompoundTypedDataBaseField(
+ ThisExpression(), fileOffset),
transformer.runtimeBranchOnLayout(offsets),
- PropertyGet(
- VariableGet(argument),
- transformer.arrayTypedDataBaseField.name,
- transformer.arrayTypedDataBaseField)
- ..fileOffset = fileOffset,
+ transformer.getArrayTypedDataBaseField(
+ VariableGet(argument), fileOffset),
ConstantExpression(IntConstant(0)),
transformer.runtimeBranchOnLayout(size),
]))
diff --git a/pkg/vm/lib/transformations/ffi_use_sites.dart b/pkg/vm/lib/transformations/ffi_use_sites.dart
index 8f9b4a3..24e4b89 100644
--- a/pkg/vm/lib/transformations/ffi_use_sites.dart
+++ b/pkg/vm/lib/transformations/ffi_use_sites.dart
@@ -327,11 +327,7 @@
Expression sizeInBytes = _inlineSizeOf(nativeType);
if (sizeInBytes != null) {
if (node.arguments.positional.length == 2) {
- sizeInBytes = MethodInvocation(
- node.arguments.positional[1],
- numMultiplication.name,
- Arguments([sizeInBytes]),
- numMultiplication);
+ sizeInBytes = multiply(node.arguments.positional[1], sizeInBytes);
}
return MethodInvocation(
node.arguments.positional[0],
@@ -485,11 +481,7 @@
pointer,
offsetByMethod.name,
Arguments([
- MethodInvocation(
- node.arguments.positional[1],
- numMultiplication.name,
- Arguments([_inlineSizeOf(dartType)]),
- numMultiplication)
+ multiply(node.arguments.positional[1], _inlineSizeOf(dartType))
]),
offsetByMethod);
}
@@ -503,10 +495,8 @@
.firstWhere((c) => c.name == Name("#fromTypedDataBase"));
final typedDataBasePrime = typedDataBaseOffset(
- PropertyGet(NullCheck(node.arguments.positional[0]),
- arrayTypedDataBaseField.name, arrayTypedDataBaseField),
- MethodInvocation(node.arguments.positional[1], numMultiplication.name,
- Arguments([_inlineSizeOf(dartType)]), numMultiplication),
+ getArrayTypedDataBaseField(NullCheck(node.arguments.positional[0])),
+ multiply(node.arguments.positional[1], _inlineSizeOf(dartType)),
_inlineSizeOf(dartType),
dartType,
node.fileOffset);
@@ -574,26 +564,17 @@
type: coreTypes.intNonNullableRawType)
..fileOffset = node.fileOffset;
final elementSizeVar = VariableDeclaration("#elementSize",
- initializer: MethodInvocation(
+ initializer: multiply(
VariableGet(singleElementSizeVar),
- numMultiplication.name,
- Arguments([
- PropertyGet(
- VariableGet(arrayVar),
- arrayNestedDimensionsFlattened.name,
- arrayNestedDimensionsFlattened)
- ]),
- numMultiplication),
+ PropertyGet(
+ VariableGet(arrayVar),
+ arrayNestedDimensionsFlattened.name,
+ arrayNestedDimensionsFlattened)),
type: coreTypes.intNonNullableRawType)
..fileOffset = node.fileOffset;
final offsetVar = VariableDeclaration("#offset",
- initializer: MethodInvocation(
- VariableGet(elementSizeVar),
- numMultiplication.name,
- Arguments([
- VariableGet(indexVar),
- ]),
- numMultiplication),
+ initializer:
+ multiply(VariableGet(elementSizeVar), VariableGet(indexVar)),
type: coreTypes.intNonNullableRawType)
..fileOffset = node.fileOffset;
@@ -618,8 +599,7 @@
arrayConstructor,
Arguments([
typedDataBaseOffset(
- PropertyGet(VariableGet(arrayVar),
- arrayTypedDataBaseField.name, arrayTypedDataBaseField),
+ getArrayTypedDataBaseField(VariableGet(arrayVar)),
VariableGet(offsetVar),
VariableGet(elementSizeVar),
dartType,
@@ -641,13 +621,11 @@
StaticInvocation(
memCopy,
Arguments([
- PropertyGet(VariableGet(arrayVar), arrayTypedDataBaseField.name,
- arrayTypedDataBaseField)
- ..fileOffset = node.fileOffset,
+ getArrayTypedDataBaseField(
+ VariableGet(arrayVar), node.fileOffset),
VariableGet(offsetVar),
- PropertyGet(node.arguments.positional[2],
- arrayTypedDataBaseField.name, arrayTypedDataBaseField)
- ..fileOffset = node.fileOffset,
+ getArrayTypedDataBaseField(
+ node.arguments.positional[2], node.fileOffset),
ConstantExpression(IntConstant(0)),
VariableGet(elementSizeVar),
]))
@@ -673,13 +651,8 @@
return MethodInvocation(
node.receiver,
offsetByMethod.name,
- Arguments([
- MethodInvocation(
- node.arguments.positional.single,
- numMultiplication.name,
- Arguments([inlineSizeOf]),
- numMultiplication)
- ]),
+ Arguments(
+ [multiply(node.arguments.positional.single, inlineSizeOf)]),
offsetByMethod);
}
}
@@ -711,13 +684,8 @@
return MethodInvocation(
node.receiver,
offsetByMethod.name,
- Arguments([
- MethodInvocation(
- node.arguments.positional.single,
- numMultiplication.name,
- Arguments([inlineSizeOf]),
- numMultiplication)
- ]),
+ Arguments(
+ [multiply(node.arguments.positional.single, inlineSizeOf)]),
offsetByMethod);
}
}