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 <nikos.nikoleris@arm.com>
Maintainer: Gabe Black <gabeblack@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/sim/guest_abi.hh b/src/sim/guest_abi.hh
index 704155b..ea3325f 100644
--- a/src/sim/guest_abi.hh
+++ b/src/sim/guest_abi.hh
@@ -42,7 +42,7 @@
 // and write a result (if any) back to. For convenience, the wrapper also
 // returns the result of the wrapped function.
 
-template <typename ABI, typename Ret, typename ...Args>
+template <typename ABI, bool store_ret, typename Ret, typename ...Args>
 Ret
 invokeSimcall(ThreadContext *tc,
               std::function<Ret(ThreadContext *, Args...)> target)
@@ -51,15 +51,30 @@
     // types will be zero initialized.
     auto state = GuestABI::initializeState<ABI>(tc);
     GuestABI::prepareForFunction<ABI, Ret, Args...>(tc, state);
-    return GuestABI::callFrom<ABI, Ret, Args...>(tc, state, target);
+    return GuestABI::callFrom<ABI, store_ret, Ret, Args...>(tc, state, target);
+}
+
+template <typename ABI, typename Ret, typename ...Args>
+Ret
+invokeSimcall(ThreadContext *tc,
+              std::function<Ret(ThreadContext *, Args...)> target)
+{
+    return invokeSimcall<ABI, true>(tc, target);
+}
+
+template <typename ABI, bool store_ret, typename Ret, typename ...Args>
+Ret
+invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...))
+{
+    return invokeSimcall<ABI, store_ret>(
+            tc, std::function<Ret(ThreadContext *, Args...)>(target));
 }
 
 template <typename ABI, typename Ret, typename ...Args>
 Ret
 invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...))
 {
-    return invokeSimcall<ABI>(
-            tc, std::function<Ret(ThreadContext *, Args...)>(target));
+    return invokeSimcall<ABI, true>(tc, target);
 }
 
 template <typename ABI, typename ...Args>
diff --git a/src/sim/guest_abi.test.cc b/src/sim/guest_abi.test.cc
index 6b5d060..8edf5d3 100644
--- a/src/sim/guest_abi.test.cc
+++ b/src/sim/guest_abi.test.cc
@@ -345,6 +345,15 @@
         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<TestABI_1D, false>(&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 62cbf29..794fd62 100644
--- a/src/sim/guest_abi/dispatch.hh
+++ b/src/sim/guest_abi/dispatch.hh
@@ -52,8 +52,9 @@
 
 // With no arguments to gather, call the target function and store the
 // result.
-template <typename ABI, typename Ret>
-static typename std::enable_if<!std::is_void<Ret>::value, Ret>::type
+template <typename ABI, bool store_ret, typename Ret>
+static typename std::enable_if<!std::is_void<Ret>::value && store_ret,
+                Ret>::type
 callFrom(ThreadContext *tc, typename ABI::State &state,
         std::function<Ret(ThreadContext *)> target)
 {
@@ -62,6 +63,15 @@
     return ret;
 }
 
+template <typename ABI, bool store_ret, typename Ret>
+static typename std::enable_if<!std::is_void<Ret>::value && !store_ret,
+                Ret>::type
+callFrom(ThreadContext *tc, typename ABI::State &state,
+        std::function<Ret(ThreadContext *)> target)
+{
+    return target(tc);
+}
+
 // With no arguments to gather and nothing to return, call the target function.
 template <typename ABI>
 static void
@@ -73,7 +83,8 @@
 
 // Recursively gather arguments for target from tc until we get to the base
 // case above.
-template <typename ABI, typename Ret, typename NextArg, typename ...Args>
+template <typename ABI, bool store_ret, typename Ret,
+          typename NextArg, typename ...Args>
 static typename std::enable_if<!std::is_void<Ret>::value, Ret>::type
 callFrom(ThreadContext *tc, typename ABI::State &state,
         std::function<Ret(ThreadContext *, NextArg, Args...)> target)
@@ -88,7 +99,7 @@
         };
 
     // Recursively handle any remaining arguments.
-    return callFrom<ABI, Ret, Args...>(tc, state, partial);
+    return callFrom<ABI, store_ret, Ret, Args...>(tc, state, partial);
 }
 
 // Recursively gather arguments for target from tc until we get to the base