gem5  v20.1.0.0
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 <type_traits>
34 
36 #include "sim/guest_abi/layout.hh"
37 
38 class ThreadContext;
39 
40 namespace GuestABI
41 {
42 
43 /*
44  * These functions will likely be common among all ABIs and implement the
45  * mechanism of gathering arguments, calling the target function, and then
46  * storing the result. They might need to be overridden if, for instance,
47  * the location of arguments need to be determined in a different order.
48  * For example, there might be an ABI which gathers arguments starting
49  * from the last in the list instead of the first. This is unlikely but
50  * still possible to support by redefining these functions..
51  */
52 
53 // With no arguments to gather, call the target function and store the
54 // result.
55 template <typename ABI, bool store_ret, typename Ret>
56 static typename std::enable_if<!std::is_void<Ret>::value && store_ret,
57  Ret>::type
58 callFrom(ThreadContext *tc, typename ABI::State &state,
59  std::function<Ret(ThreadContext *)> target)
60 {
61  Ret ret = target(tc);
62  storeResult<ABI, Ret>(tc, ret, state);
63  return ret;
64 }
65 
66 template <typename ABI, bool store_ret, typename Ret>
67 static typename std::enable_if<!std::is_void<Ret>::value && !store_ret,
68  Ret>::type
69 callFrom(ThreadContext *tc, typename ABI::State &state,
70  std::function<Ret(ThreadContext *)> target)
71 {
72  return target(tc);
73 }
74 
75 // With no arguments to gather and nothing to return, call the target function.
76 template <typename ABI>
77 static void
78 callFrom(ThreadContext *tc, typename ABI::State &state,
79  std::function<void(ThreadContext *)> target)
80 {
81  target(tc);
82 }
83 
84 // Recursively gather arguments for target from tc until we get to the base
85 // case above.
86 template <typename ABI, bool store_ret, typename Ret,
87  typename NextArg, typename ...Args>
88 static typename std::enable_if<!std::is_void<Ret>::value, Ret>::type
89 callFrom(ThreadContext *tc, typename ABI::State &state,
90  std::function<Ret(ThreadContext *, NextArg, Args...)> target)
91 {
92  // Extract the next argument from the thread context.
93  NextArg next = getArgument<ABI, NextArg>(tc, state);
94 
95  // Build a partial function which adds the next argument to the call.
96  std::function<Ret(ThreadContext *, Args...)> partial =
97  [target,next](ThreadContext *_tc, Args... args) {
98  return target(_tc, next, args...);
99  };
100 
101  // Recursively handle any remaining arguments.
102  return callFrom<ABI, store_ret, Ret, Args...>(tc, state, partial);
103 }
104 
105 // Recursively gather arguments for target from tc until we get to the base
106 // case above. This version is for functions that don't return anything.
107 template <typename ABI, typename NextArg, typename ...Args>
108 static void
109 callFrom(ThreadContext *tc, typename ABI::State &state,
110  std::function<void(ThreadContext *, NextArg, Args...)> target)
111 {
112  // Extract the next argument from the thread context.
113  NextArg next = getArgument<ABI, NextArg>(tc, state);
114 
115  // Build a partial function which adds the next argument to the call.
116  std::function<void(ThreadContext *, Args...)> partial =
117  [target,next](ThreadContext *_tc, Args... args) {
118  target(_tc, next, args...);
119  };
120 
121  // Recursively handle any remaining arguments.
122  callFrom<ABI, Args...>(tc, state, partial);
123 }
124 
125 
126 
127 /*
128  * These functions are like the ones above, except they print the arguments
129  * a target function would be called with instead of actually calling it.
130  */
131 
132 // With no arguments to print, add the closing parenthesis and return.
133 template <typename ABI, typename Ret>
134 static void
135 dumpArgsFrom(int count, std::ostream &os, ThreadContext *tc,
136  typename ABI::State &state)
137 {
138  os << ")";
139 }
140 
141 // Recursively gather arguments for target from tc until we get to the base
142 // case above, and append those arguments to the string stream being
143 // constructed.
144 template <typename ABI, typename Ret, typename NextArg, typename ...Args>
145 static void
146 dumpArgsFrom(int count, std::ostream &os, ThreadContext *tc,
147  typename ABI::State &state)
148 {
149  // Either open the parenthesis or add a comma, depending on where we are
150  // in the argument list.
151  os << (count ? ", " : "(");
152 
153  // Extract the next argument from the thread context.
154  NextArg next = getArgument<ABI, NextArg>(tc, state);
155 
156  // Add this argument to the list.
157  os << next;
158 
159  // Recursively handle any remaining arguments.
160  dumpArgsFrom<ABI, Ret, Args...>(count + 1, os, tc, state);
161 }
162 
163 } // namespace GuestABI
164 
165 #endif // __SIM_GUEST_ABI_DISPATCH_HH__
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
definition.hh
type
uint8_t type
Definition: inet.hh:421
X86ISA::count
count
Definition: misc.hh:703
GuestABI::callFrom
static std::enable_if<!std::is_void< Ret >::value &&store_ret, Ret >::type callFrom(ThreadContext *tc, typename ABI::State &state, std::function< Ret(ThreadContext *)> target)
Definition: dispatch.hh:58
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
GuestABI
Definition: aapcs32.hh:66
GuestABI::dumpArgsFrom
static void dumpArgsFrom(int count, std::ostream &os, ThreadContext *tc, typename ABI::State &state)
Definition: dispatch.hh:135
layout.hh

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17