Files
gem5/src/sim/guest_abi.hh
Gabe Black ec5a24fba4 sim: Rename GuestABI's Position to State.
This type can hold any generic state related to locating return types
and arguments in addition to simple position information. To make that
clearer, this change renames the Position type to State.

Change-Id: I50ff2ec61c3eba0e9505c66ce32e27b515bd4b27
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/24107
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu>
Maintainer: Gabe Black <gabeblack@google.com>
2020-03-12 11:14:09 +00:00

115 lines
4.3 KiB
C++

/*
* Copyright 2019 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __SIM_GUEST_ABI_HH__
#define __SIM_GUEST_ABI_HH__
#include <functional>
#include "sim/guest_abi/definition.hh"
#include "sim/guest_abi/dispatch.hh"
#include "sim/guest_abi/layout.hh"
#include "sim/guest_abi/varargs.hh"
class ThreadContext;
// These functions wrap a simulator level function with the given signature.
// The wrapper takes one argument, a thread context to extract arguments from
// 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>
Ret
invokeSimcall(ThreadContext *tc,
std::function<Ret(ThreadContext *, Args...)> target)
{
// Default construct a State to track consumed resources. Built in
// 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);
}
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));
}
template <typename ABI, typename ...Args>
void
invokeSimcall(ThreadContext *tc,
std::function<void(ThreadContext *, Args...)> target)
{
// Default construct a State to track consumed resources. Built in
// types will be zero initialized.
auto state = GuestABI::initializeState<ABI>(tc);
GuestABI::prepareForArguments<ABI, Args...>(tc, state);
GuestABI::callFrom<ABI, Args...>(tc, state, target);
}
template <typename ABI, typename ...Args>
void
invokeSimcall(ThreadContext *tc, void (*target)(ThreadContext *, Args...))
{
invokeSimcall<ABI>(
tc, std::function<void(ThreadContext *, Args...)>(target));
}
// These functions also wrap a simulator level function. Instead of running the
// function, they return a string which shows what arguments the function would
// be invoked with if it were called from the given context.
template <typename ABI, typename Ret, typename ...Args>
std::string
dumpSimcall(std::string name, ThreadContext *tc,
std::function<Ret(ThreadContext *, Args...)> target=
std::function<Ret(ThreadContext *, Args...)>())
{
auto state = GuestABI::initializeState<ABI>(tc);
std::ostringstream ss;
GuestABI::prepareForFunction<ABI, Ret, Args...>(tc, state);
ss << name;
GuestABI::dumpArgsFrom<ABI, Ret, Args...>(0, ss, tc, state);
return ss.str();
}
template <typename ABI, typename Ret, typename ...Args>
std::string
dumpSimcall(std::string name, ThreadContext *tc,
Ret (*target)(ThreadContext *, Args...))
{
return dumpSimcall<ABI>(
name, tc, std::function<Ret(ThreadContext *, Args...)>(target));
}
#endif // __SIM_GUEST_ABI_HH__