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>
This commit is contained in:
Gabe Black
2020-04-29 01:32:40 -07:00
parent 4773c6faff
commit 41c88839a9
3 changed files with 43 additions and 8 deletions

View File

@@ -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 <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 @@ invokeSimcall(ThreadContext *tc,
// 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>

View File

@@ -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<TestABI_1D, false>(&tc, testIntRet);
EXPECT_EQ(ret, IntRetValue);
EXPECT_EQ(tc.intResult, tc.DefaultIntResult);
EXPECT_EQ(tc.floatResult, tc.DefaultFloatResult);
}
// 2D returns.
{

View File

@@ -52,8 +52,9 @@ namespace GuestABI
// 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 @@ callFrom(ThreadContext *tc, typename ABI::State &state,
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 @@ callFrom(ThreadContext *tc, typename ABI::State &state,
// 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 @@ callFrom(ThreadContext *tc, typename ABI::State &state,
};
// 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