From 2d44ecd4240c9ed22a815b41d934a4043fe3c988 Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Tue, 23 Jul 2024 21:15:20 -0700 Subject: [PATCH 1/2] refactor: Clean up Invoke calls in dart_entry This reduces the number of #define switches for this file. I also think the TESTING + DART_PRECOMPILED_RUNTIME block was never actually compiled, so this fixes the type conversions in that case if it ever is compiled. We did this upstream in Shorebird to make it easier to change the invoke syntax (to make the simulator dynamically switchable) but the clean-up seemed worth sending upstream too. --- runtime/vm/dart_entry.cc | 73 +++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/runtime/vm/dart_entry.cc b/runtime/vm/dart_entry.cc index 97313f5e5f8e..27e1bff98614 100644 --- a/runtime/vm/dart_entry.cc +++ b/runtime/vm/dart_entry.cc @@ -82,6 +82,31 @@ extern "C" typedef uword /*ObjectPtr*/ (*invokestub)( uword /*ArrayPtr*/ arguments, Thread* thread); +// Private helper for converting types and switching between Simulator +// and CPU invocations. +static ObjectPtr InvokeDartCode(uword entry_point, + const Array& arguments, + const Array& arguments_descriptor, + Thread* thread) { + DartEntryScope dart_entry_scope(thread); + + const uword stub = StubCode::InvokeDartCode().EntryPoint(); +#if defined(USING_SIMULATOR) + auto invoke = [&](uword entry_point, uword arguments_descriptor, + uword arguments, Thread* thread) -> uword { + return Simulator::Current()->Call(stub, entry_point, arguments_descriptor, + arguments, + reinterpret_cast(thread)); + }; +#else + auto invoke = reinterpret_cast(stub); +#endif + uword result = + invoke(entry_point, static_cast(arguments_descriptor.ptr()), + static_cast(arguments.ptr()), thread); + return static_cast(result); +} + ObjectPtr DartEntry::InvokeFunction(const Function& function, const Array& arguments, const Array& arguments_descriptor) { @@ -107,30 +132,13 @@ ObjectPtr DartEntry::InvokeFunction(const Function& function, ASSERT(function.HasCode()); - DartEntryScope dart_entry_scope(thread); - - const uword stub = StubCode::InvokeDartCode().EntryPoint(); -#if defined(USING_SIMULATOR) - return bit_copy(Simulator::Current()->Call( - static_cast(stub), -#if defined(DART_PRECOMPILED_RUNTIME) - static_cast(function.entry_point()), -#else - static_cast(function.CurrentCode()), -#endif - static_cast(arguments_descriptor.ptr()), - static_cast(arguments.ptr()), - reinterpret_cast(thread))); -#else // USING_SIMULATOR - return static_cast((reinterpret_cast(stub))( + return InvokeDartCode( #if defined(DART_PRECOMPILED_RUNTIME) function.entry_point(), #else static_cast(function.CurrentCode()), #endif - static_cast(arguments_descriptor.ptr()), - static_cast(arguments.ptr()), thread)); -#endif + arguments, arguments_descriptor, thread); } #if defined(TESTING) @@ -148,34 +156,13 @@ ObjectPtr DartEntry::InvokeCode(const Code& code, ASSERT(!code.IsNull()); ASSERT(thread->no_callback_scope_depth() == 0); - DartEntryScope dart_entry_scope(thread); - - const uword stub = StubCode::InvokeDartCode().EntryPoint(); + return InvokeDartCode( #if defined(DART_PRECOMPILED_RUNTIME) -#if defined(USING_SIMULATOR) - return bit_copy(Simulator::Current()->Call( - static_cast(stub), static_cast(code.EntryPoint()), - static_cast(arguments_descriptor.ptr()), - static_cast(arguments.ptr()), - reinterpret_cast(thread))); -#else - return static_cast((reinterpret_cast(stub))( - code.EntryPoint(), arguments_descriptor.ptr(), arguments.ptr(), thread)); -#endif + code.EntryPoint(), #else // defined(DART_PRECOMPILED_RUNTIME) -#if defined(USING_SIMULATOR) - return bit_copy(Simulator::Current()->Call( - static_cast(stub), static_cast(code.ptr()), - static_cast(arguments_descriptor.ptr()), - static_cast(arguments.ptr()), - reinterpret_cast(thread))); -#else - return static_cast((reinterpret_cast(stub))( static_cast(code.ptr()), - static_cast(arguments_descriptor.ptr()), - static_cast(arguments.ptr()), thread)); -#endif #endif + arguments_descriptor, arguments, thread); } #endif // defined(TESTING) From 131e7212ab202622a7dc9efc7a3e115d2f9ac4e0 Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Wed, 24 Jul 2024 20:03:09 -0700 Subject: [PATCH 2/2] fix swapped arguments in TESTING case --- runtime/vm/dart_entry.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runtime/vm/dart_entry.cc b/runtime/vm/dart_entry.cc index 27e1bff98614..dc0b69d2da5d 100644 --- a/runtime/vm/dart_entry.cc +++ b/runtime/vm/dart_entry.cc @@ -85,8 +85,8 @@ extern "C" typedef uword /*ObjectPtr*/ (*invokestub)( // Private helper for converting types and switching between Simulator // and CPU invocations. static ObjectPtr InvokeDartCode(uword entry_point, - const Array& arguments, const Array& arguments_descriptor, + const Array& arguments, Thread* thread) { DartEntryScope dart_entry_scope(thread); @@ -132,13 +132,15 @@ ObjectPtr DartEntry::InvokeFunction(const Function& function, ASSERT(function.HasCode()); + // Note: InvokeFunction takes Arguments then ArgumentsDescriptor, + // where as InvokeDartCode takes ArgumentsDescriptor then Arguments. return InvokeDartCode( #if defined(DART_PRECOMPILED_RUNTIME) function.entry_point(), #else static_cast(function.CurrentCode()), #endif - arguments, arguments_descriptor, thread); + arguments_descriptor, arguments, thread); } #if defined(TESTING)