diff --git a/runtime/bin/ffi_test/ffi_test_functions.cc b/runtime/bin/ffi_test/ffi_test_functions.cc index 0f52f7c5b83..583c603bc89 100644 --- a/runtime/bin/ffi_test/ffi_test_functions.cc +++ b/runtime/bin/ffi_test/ffi_test_functions.cc @@ -186,6 +186,28 @@ DART_EXPORT intptr_t SumManyInts(intptr_t a, return retval; } +// Sums many ints. +// Used for testing calling conventions. With small integers on stack slots we +// test stack alignment. +DART_EXPORT int16_t SumManySmallInts(int8_t a, + int16_t b, + int8_t c, + int16_t d, + int8_t e, + int16_t f, + int8_t g, + int16_t h, + int8_t i, + int16_t j) { + std::cout << "SumManySmallInts(" << static_cast(a) << ", " << b << ", " + << static_cast(c) << ", " << d << ", " << static_cast(e) + << ", " << f << ", " << static_cast(g) << ", " << h << ", " + << static_cast(i) << ", " << j << ")\n"; + int16_t retval = a + b + c + d + e + f + g + h + i + j; + std::cout << "returning " << retval << "\n"; + return retval; +} + // Sums an odd number of ints. // Used for testing calling conventions. With so many arguments, and an odd // number of arguments, we are testing stack alignment on various architectures. diff --git a/samples/ffi/sample_ffi_functions.dart b/samples/ffi/sample_ffi_functions.dart index 72db3ebdf34..ed641e98216 100644 --- a/samples/ffi/sample_ffi_functions.dart +++ b/samples/ffi/sample_ffi_functions.dart @@ -20,6 +20,8 @@ typedef NativeDoubleUnaryOp = Double Function(Double); typedef NativeFloatUnaryOp = Float Function(Float); typedef NativeDecenaryOp = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr); +typedef NativeDecenaryOp2 = Int16 Function( + Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16); typedef NativeDoubleDecenaryOp = Double Function(Double, Double, Double, Double, Double, Double, Double, Double, Double, Double); typedef NativeVigesimalOp = Double Function( @@ -149,6 +151,15 @@ main() { print(result.runtimeType); } + { + // Function with many arguments: arguments get passed in registers and stack. + DecenaryOp sumManyInts = ffiTestFunctions + .lookupFunction("SumManySmallInts"); + var result = sumManyInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + print(result); + print(result.runtimeType); + } + { // Function with many double arguments. DoubleDecenaryOp sumManyDoubles = ffiTestFunctions.lookupFunction< diff --git a/tests/ffi/function_callbacks_test.dart b/tests/ffi/function_callbacks_test.dart index 33241f2f366..9b59fb4fd51 100644 --- a/tests/ffi/function_callbacks_test.dart +++ b/tests/ffi/function_callbacks_test.dart @@ -45,24 +45,40 @@ class Test { } typedef SimpleAdditionType = Int32 Function(Int32, Int32); -int simpleAddition(int x, int y) => x + y; +int simpleAddition(int x, int y) { + print("simpleAddition($x, $y)"); + return x + y; +} typedef IntComputationType = Int64 Function(Int8, Int16, Int32, Int64); -int intComputation(int a, int b, int c, int d) => d - c + b - a; +int intComputation(int a, int b, int c, int d) { + print("intComputation($a, $b, $c, $d)"); + return d - c + b - a; +} typedef UintComputationType = Uint64 Function(Uint8, Uint16, Uint32, Uint64); -int uintComputation(int a, int b, int c, int d) => d - c + b - a; +int uintComputation(int a, int b, int c, int d) { + print("uintComputation($a, $b, $c, $d)"); + return d - c + b - a; +} typedef SimpleMultiplyType = Double Function(Double); -double simpleMultiply(double x) => x * 1.337; +double simpleMultiply(double x) { + print("simpleMultiply($x)"); + return x * 1.337; +} typedef SimpleMultiplyFloatType = Float Function(Float); -double simpleMultiplyFloat(double x) => x * 1.337; +double simpleMultiplyFloat(double x) { + print("simpleMultiplyFloat($x)"); + return x * 1.337; +} typedef ManyIntsType = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr); int manyInts( int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) { + print("manyInts($a, $b, $c, $d, $e, $f, $g, $h, $i, $j"); return a + b + c + d + e + f + g + h + i + j; } @@ -70,6 +86,7 @@ typedef ManyDoublesType = Double Function(Double, Double, Double, Double, Double, Double, Double, Double, Double, Double); double manyDoubles(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j) { + print("manyDoubles($a, $b, $c, $d, $e, $f, $g, $h, $i, $j"); return a + b + c + d + e + f + g + h + i + j; } @@ -115,6 +132,8 @@ double manyArgs( double _18, int _19, double _20) { + print("manyArgs( $_1, $_2, $_3, $_4, $_5, $_6, $_7, $_8, $_9, $_10," + + "$_11, $_12, $_13, $_14, $_15, $_16, $_17, $_18, $_19, $_20)"); return _1 + _2 + _3 + diff --git a/tests/ffi/function_test.dart b/tests/ffi/function_test.dart index 5ed233d7c51..644605a70b2 100644 --- a/tests/ffi/function_test.dart +++ b/tests/ffi/function_test.dart @@ -33,6 +33,7 @@ void main() { testNativeFunctionManyArguments2(); testNativeFunctionManyArguments3(); testNativeFunctionManyArguments4(); + testNativeFunctionManyArguments5(); testNativeFunctionPointer(); testNullInt(); testNullDouble(); @@ -229,6 +230,8 @@ void testNativeFunctionFloats() { typedef NativeDecenaryOp = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr); +typedef NativeDecenaryOp2 = Int16 Function( + Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16); typedef DecenaryOp = int Function( int, int, int, int, int, int, int, int, int, int); @@ -239,6 +242,13 @@ void testNativeFunctionManyArguments1() { Expect.equals(55, sumManyInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); } +DecenaryOp sumManySmallInts = ffiTestFunctions + .lookupFunction("SumManySmallInts"); + +void testNativeFunctionManyArguments5() { + Expect.equals(55, sumManySmallInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); +} + typedef NativeUndenaryOp = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr); typedef UndenaryOp = int Function(