gem5  v21.1.0.2
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 GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi);
47 namespace guest_abi
48 {
49 
50 /*
51  * These functions will likely be common among all ABIs and implement the
52  * mechanism of gathering arguments, calling the target function, and then
53  * storing the result. They might need to be overridden if, for instance,
54  * the location of arguments need to be determined in a different order.
55  * For example, there might be an ABI which gathers arguments starting
56  * from the last in the list instead of the first. This is unlikely but
57  * still possible to support by redefining these functions..
58  */
59 
60 template <typename ABI, typename Ret, bool store_ret, typename Target,
61  typename State, typename Args, std::size_t... I>
62 static inline typename std::enable_if_t<!store_ret, Ret>
63 callFromHelper(Target &target, ThreadContext *tc, State &state, Args &&args,
64  std::index_sequence<I...>)
65 {
66  return target(tc, std::get<I>(args)...);
67 }
68 
69 template <typename ABI, typename Ret, bool store_ret, typename Target,
70  typename State, typename Args, std::size_t... I>
71 static inline typename std::enable_if_t<store_ret, Ret>
72 callFromHelper(Target &target, ThreadContext *tc, State &state, Args &&args,
73  std::index_sequence<I...>)
74 {
75  Ret ret = target(tc, std::get<I>(args)...);
76  storeResult<ABI, Ret>(tc, ret, state);
77  return ret;
78 }
79 
80 template <typename ABI, typename Ret, bool store_ret, typename ...Args>
81 static inline Ret
82 callFrom(ThreadContext *tc, typename ABI::State &state,
83  std::function<Ret(ThreadContext *, Args...)> target)
84 {
85  // Extract all the arguments from the thread context. Braced initializers
86  // are evaluated from left to right.
87  auto args = std::tuple<Args...>{getArgument<ABI, Args>(tc, state)...};
88 
89  // Call the wrapper which will call target.
90  return callFromHelper<ABI, Ret, store_ret>(
91  target, tc, state, std::move(args),
92  std::make_index_sequence<sizeof...(Args)>{});
93 }
94 
95 
96 /*
97  * This function is like the ones above, except it prints the arguments
98  * a target function would be called with instead of actually calling it.
99  */
100 
101 template <typename ABI, typename Ret, typename ...Args>
102 static void
103 dumpArgsFrom(std::ostream &os, GEM5_VAR_USED ThreadContext *tc,
104  typename ABI::State &state)
105 {
106  int count = 0;
107  // Extract all the arguments from the thread context and print them,
108  // prefixed with either a ( or a , as appropriate.
109  GEM5_FOR_EACH_IN_PACK(os << (count++ ? ", " : "("),
110  os << getArgument<ABI, Args>(tc, state));
111  os << ")";
112 }
113 
114 } // namespace guest_abi
115 } // namespace gem5
116 
117 #endif // __SIM_GUEST_ABI_DISPATCH_HH__
definition.hh
gem5::guest_abi::dumpArgsFrom
static void dumpArgsFrom(std::ostream &os, GEM5_VAR_USED ThreadContext *tc, typename ABI::State &state)
Definition: dispatch.hh:103
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:63
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:93
gem5::X86ISA::count
count
Definition: misc.hh:709
compiler.hh
gem5::GEM5_DEPRECATED_NAMESPACE
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
gem5::guest_abi::callFrom
static Ret callFrom(ThreadContext *tc, typename ABI::State &state, std::function< Ret(ThreadContext *, Args...)> target)
Definition: dispatch.hh:82
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:809
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
layout.hh

Generated on Tue Sep 21 2021 12:25:48 for gem5 by doxygen 1.8.17