gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dispatch.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef __SIM_GUEST_ABI_DISPATCH_HH__
29 #define __SIM_GUEST_ABI_DISPATCH_HH__
30 
31 #include <functional>
32 #include <sstream>
33 #include <tuple>
34 #include <type_traits>
35 #include <utility>
36 
37 #include "base/compiler.hh"
39 #include "sim/guest_abi/layout.hh"
40 
41 namespace gem5
42 {
43 
44 class ThreadContext;
45 
46 namespace guest_abi
47 {
48 
49 /*
50  * These functions will likely be common among all ABIs and implement the
51  * mechanism of gathering arguments, calling the target function, and then
52  * storing the result. They might need to be overridden if, for instance,
53  * the location of arguments need to be determined in a different order.
54  * For example, there might be an ABI which gathers arguments starting
55  * from the last in the list instead of the first. This is unlikely but
56  * still possible to support by redefining these functions..
57  */
58 
59 template <typename ABI, typename Ret, bool store_ret, typename Target,
60  typename State, typename Args, std::size_t... I>
61 static inline typename std::enable_if_t<!store_ret, Ret>
62 callFromHelper(Target &target, ThreadContext *tc, State &state, Args &&args,
63  std::index_sequence<I...>)
64 {
65  return target(tc, std::get<I>(args)...);
66 }
67 
68 template <typename ABI, typename Ret, bool store_ret, typename Target,
69  typename State, typename Args, std::size_t... I>
70 static inline typename std::enable_if_t<store_ret, Ret>
71 callFromHelper(Target &target, ThreadContext *tc, State &state, Args &&args,
72  std::index_sequence<I...>)
73 {
74  Ret ret = target(tc, std::get<I>(args)...);
75  storeResult<ABI, Ret>(tc, ret, state);
76  return ret;
77 }
78 
79 template <typename ABI, typename Ret, bool store_ret, typename ...Args>
80 static inline Ret
81 callFrom(ThreadContext *tc, typename ABI::State &state,
82  std::function<Ret(ThreadContext *, Args...)> target)
83 {
84  // Extract all the arguments from the thread context. Braced initializers
85  // are evaluated from left to right.
86  auto args = std::tuple<Args...>{getArgument<ABI, Args>(tc, state)...};
87 
88  // Call the wrapper which will call target.
89  return callFromHelper<ABI, Ret, store_ret>(
90  target, tc, state, std::move(args),
91  std::make_index_sequence<sizeof...(Args)>{});
92 }
93 
94 
95 /*
96  * This function is like the ones above, except it prints the arguments
97  * a target function would be called with instead of actually calling it.
98  */
99 
100 template <typename ABI, typename Ret, typename ...Args>
101 static void
102 dumpArgsFrom(std::ostream &os, [[maybe_unused]] ThreadContext *tc,
103  typename ABI::State &state)
104 {
105  int count = 0;
106  // Extract all the arguments from the thread context and print them,
107  // prefixed with either a ( or a , as appropriate.
108  GEM5_FOR_EACH_IN_PACK(os << (count++ ? ", " : "("),
109  os << getArgument<ABI, Args>(tc, state));
110  os << ")";
111 }
112 
113 } // namespace guest_abi
114 } // namespace gem5
115 
116 #endif // __SIM_GUEST_ABI_DISPATCH_HH__
definition.hh
gem5::guest_abi::callFromHelper
static std::enable_if_t<!store_ret, Ret > callFromHelper(Target &target, ThreadContext *tc, State &state, Args &&args, std::index_sequence< I... >)
Definition: dispatch.hh:62
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
gem5::guest_abi::dumpArgsFrom
static void dumpArgsFrom(std::ostream &os, [[maybe_unused]] ThreadContext *tc, typename ABI::State &state)
Definition: dispatch.hh:102
gem5::X86ISA::count
count
Definition: misc.hh:710
compiler.hh
gem5::guest_abi::callFrom
static Ret callFrom(ThreadContext *tc, typename ABI::State &state, std::function< Ret(ThreadContext *, Args...)> target)
Definition: dispatch.hh:81
state
atomic_var_t state
Definition: helpers.cc:188
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:810
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
layout.hh

Generated on Sun Jul 30 2023 01:56:59 for gem5 by doxygen 1.8.17