From: Gabe Black Date: Wed, 29 Apr 2020 08:32:40 +0000 (-0700) Subject: sim: Add an option to suppress the return value in invokeSimcall. X-Git-Tag: v20.0.0.0~95 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=41c88839a9239e4f7eaf27b4ae149348b0959f4c;p=gem5.git sim: Add an option to suppress the return value in invokeSimcall. Sometimes when using the GuestABI mechanism, gem5 wants to know that a function was called and with what arguments to do its own processing, but doesn't want to return its own value since it will still let the simulated system execute its own function. There are also situations where gem5 wants to return a value, but not through the normal mechanism. That happens when, for instance, a gem5 op is triggered by a memory access, and that access is what should return the value, not a particular fixed register. This option is a template parameter rather than a function argument so that if it's not going to be used, no "Return" type needs to be defined since it's not present at all in the chain of functions invokeSimcall expands to. This will also make it easier to reuse generic ABIs in those situations without having to make custom wrappers. Change-Id: I969e78495c8f4e73f4de1a3dfb4d74c9b30f5af5 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/28288 Reviewed-by: Nikos Nikoleris Maintainer: Gabe Black Tested-by: kokoro --- diff --git a/src/sim/guest_abi.hh b/src/sim/guest_abi.hh index 704155b4e..ea3325f6a 100644 --- a/src/sim/guest_abi.hh +++ b/src/sim/guest_abi.hh @@ -42,7 +42,7 @@ class ThreadContext; // and write a result (if any) back to. For convenience, the wrapper also // returns the result of the wrapped function. -template +template Ret invokeSimcall(ThreadContext *tc, std::function target) @@ -51,17 +51,32 @@ invokeSimcall(ThreadContext *tc, // types will be zero initialized. auto state = GuestABI::initializeState(tc); GuestABI::prepareForFunction(tc, state); - return GuestABI::callFrom(tc, state, target); + return GuestABI::callFrom(tc, state, target); } template Ret +invokeSimcall(ThreadContext *tc, + std::function target) +{ + return invokeSimcall(tc, target); +} + +template +Ret invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...)) { - return invokeSimcall( + return invokeSimcall( tc, std::function(target)); } +template +Ret +invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...)) +{ + return invokeSimcall(tc, target); +} + template void invokeSimcall(ThreadContext *tc, diff --git a/src/sim/guest_abi.test.cc b/src/sim/guest_abi.test.cc index 6b5d0607b..8edf5d35a 100644 --- a/src/sim/guest_abi.test.cc +++ b/src/sim/guest_abi.test.cc @@ -345,6 +345,15 @@ TEST(GuestABI, ABI_returns) EXPECT_EQ(tc.intResult, tc.DefaultIntResult); EXPECT_EQ(tc.floatResult, DoubleRetValue + 1.0); } + { + // Disable storing the return value in the ThreadContext. + ThreadContext tc; + int ret = invokeSimcall(&tc, testIntRet); + EXPECT_EQ(ret, IntRetValue); + EXPECT_EQ(tc.intResult, tc.DefaultIntResult); + EXPECT_EQ(tc.floatResult, tc.DefaultFloatResult); + } + // 2D returns. { diff --git a/src/sim/guest_abi/dispatch.hh b/src/sim/guest_abi/dispatch.hh index 62cbf291d..794fd6280 100644 --- a/src/sim/guest_abi/dispatch.hh +++ b/src/sim/guest_abi/dispatch.hh @@ -52,8 +52,9 @@ namespace GuestABI // With no arguments to gather, call the target function and store the // result. -template -static typename std::enable_if::value, Ret>::type +template +static typename std::enable_if::value && store_ret, + Ret>::type callFrom(ThreadContext *tc, typename ABI::State &state, std::function target) { @@ -62,6 +63,15 @@ callFrom(ThreadContext *tc, typename ABI::State &state, return ret; } +template +static typename std::enable_if::value && !store_ret, + Ret>::type +callFrom(ThreadContext *tc, typename ABI::State &state, + std::function target) +{ + return target(tc); +} + // With no arguments to gather and nothing to return, call the target function. template static void @@ -73,7 +83,8 @@ callFrom(ThreadContext *tc, typename ABI::State &state, // Recursively gather arguments for target from tc until we get to the base // case above. -template +template static typename std::enable_if::value, Ret>::type callFrom(ThreadContext *tc, typename ABI::State &state, std::function target) @@ -88,7 +99,7 @@ callFrom(ThreadContext *tc, typename ABI::State &state, }; // Recursively handle any remaining arguments. - return callFrom(tc, state, partial); + return callFrom(tc, state, partial); } // Recursively gather arguments for target from tc until we get to the base