OSDN Git Service

libitm: Configure for gas cfi pseudo ops.
[pf3gnuchains/gcc-fork.git] / libitm / dispatch.h
1 /* Copyright (C) 2011 Free Software Foundation, Inc.
2    Contributed by Torvald Riegel <triegel@redhat.com>.
3
4    This file is part of the GNU Transactional Memory Library (libitm).
5
6    Libitm is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14    more details.
15
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24
25 #ifndef DISPATCH_H
26 #define DISPATCH_H 1
27
28 #include "libitm.h"
29 #include "common.h"
30
31 // Creates ABI load/store methods (can be made virtual or static using M,
32 // use M2 to create separate methods names for virtual and static)
33 // The _PV variants are for the pure-virtual methods in the base class.
34 #define ITM_READ_M(T, LSMOD, M, M2)                                         \
35   M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
36   {                                                                         \
37     return load(ptr, abi_dispatch::LSMOD);                                  \
38   }
39
40 #define ITM_READ_M_PV(T, LSMOD, M, M2)                                      \
41   M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
42   = 0;
43
44 #define ITM_WRITE_M(T, LSMOD, M, M2)                         \
45   M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
46                                          _ITM_TYPE_##T val)  \
47   {                                                          \
48     store(ptr, val, abi_dispatch::LSMOD);                    \
49   }
50
51 #define ITM_WRITE_M_PV(T, LSMOD, M, M2)                      \
52   M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
53                                          _ITM_TYPE_##T val)  \
54   = 0;
55
56 // Creates ABI load/store methods for all load/store modifiers for a particular
57 // type.
58 #define CREATE_DISPATCH_METHODS_T(T, M, M2) \
59   ITM_READ_M(T, R, M, M2)                \
60   ITM_READ_M(T, RaR, M, M2)              \
61   ITM_READ_M(T, RaW, M, M2)              \
62   ITM_READ_M(T, RfW, M, M2)              \
63   ITM_WRITE_M(T, W, M, M2)               \
64   ITM_WRITE_M(T, WaR, M, M2)             \
65   ITM_WRITE_M(T, WaW, M, M2)
66 #define CREATE_DISPATCH_METHODS_T_PV(T, M, M2) \
67   ITM_READ_M_PV(T, R, M, M2)                \
68   ITM_READ_M_PV(T, RaR, M, M2)              \
69   ITM_READ_M_PV(T, RaW, M, M2)              \
70   ITM_READ_M_PV(T, RfW, M, M2)              \
71   ITM_WRITE_M_PV(T, W, M, M2)               \
72   ITM_WRITE_M_PV(T, WaR, M, M2)             \
73   ITM_WRITE_M_PV(T, WaW, M, M2)
74
75 // Creates ABI load/store methods for all types.
76 // See CREATE_DISPATCH_FUNCTIONS for comments.
77 #define CREATE_DISPATCH_METHODS(M, M2)  \
78   CREATE_DISPATCH_METHODS_T (U1, M, M2) \
79   CREATE_DISPATCH_METHODS_T (U2, M, M2) \
80   CREATE_DISPATCH_METHODS_T (U4, M, M2) \
81   CREATE_DISPATCH_METHODS_T (U8, M, M2) \
82   CREATE_DISPATCH_METHODS_T (F, M, M2)  \
83   CREATE_DISPATCH_METHODS_T (D, M, M2)  \
84   CREATE_DISPATCH_METHODS_T (E, M, M2)  \
85   CREATE_DISPATCH_METHODS_T (CF, M, M2) \
86   CREATE_DISPATCH_METHODS_T (CD, M, M2) \
87   CREATE_DISPATCH_METHODS_T (CE, M, M2)
88 #define CREATE_DISPATCH_METHODS_PV(M, M2)  \
89   CREATE_DISPATCH_METHODS_T_PV (U1, M, M2) \
90   CREATE_DISPATCH_METHODS_T_PV (U2, M, M2) \
91   CREATE_DISPATCH_METHODS_T_PV (U4, M, M2) \
92   CREATE_DISPATCH_METHODS_T_PV (U8, M, M2) \
93   CREATE_DISPATCH_METHODS_T_PV (F, M, M2)  \
94   CREATE_DISPATCH_METHODS_T_PV (D, M, M2)  \
95   CREATE_DISPATCH_METHODS_T_PV (E, M, M2)  \
96   CREATE_DISPATCH_METHODS_T_PV (CF, M, M2) \
97   CREATE_DISPATCH_METHODS_T_PV (CD, M, M2) \
98   CREATE_DISPATCH_METHODS_T_PV (CE, M, M2)
99
100 // Creates memcpy/memmove/memset methods.
101 #define CREATE_DISPATCH_METHODS_MEM()  \
102 virtual void memtransfer(void *dst, const void* src, size_t size,    \
103     bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod)       \
104 {                                                                     \
105   memtransfer_static(dst, src, size, may_overlap, dst_mod, src_mod); \
106 }                                                                     \
107 virtual void memset(void *dst, int c, size_t size, ls_modifier mod)  \
108 {                                                                     \
109   memset_static(dst, c, size, mod);                                  \
110 }
111
112 #define CREATE_DISPATCH_METHODS_MEM_PV()  \
113 virtual void memtransfer(void *dst, const void* src, size_t size,       \
114     bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) = 0;     \
115 virtual void memset(void *dst, int c, size_t size, ls_modifier mod) = 0;
116
117
118 // Creates ABI load/store functions that can target either a class or an
119 // object.
120 #define ITM_READ(T, LSMOD, TARGET, M2)                                 \
121   _ITM_TYPE_##T ITM_REGPARM _ITM_##LSMOD##T (const _ITM_TYPE_##T *ptr) \
122   {                                                                    \
123     return TARGET ITM_##LSMOD##T##M2(ptr);                            \
124   }
125
126 #define ITM_WRITE(T, LSMOD, TARGET, M2)                                    \
127   void ITM_REGPARM _ITM_##LSMOD##T (_ITM_TYPE_##T *ptr, _ITM_TYPE_##T val) \
128   {                                                                        \
129     TARGET ITM_##LSMOD##T##M2(ptr, val);                                  \
130   }
131
132 // Creates ABI load/store functions for all load/store modifiers for a
133 // particular type.
134 #define CREATE_DISPATCH_FUNCTIONS_T(T, TARGET, M2) \
135   ITM_READ(T, R, TARGET, M2)                \
136   ITM_READ(T, RaR, TARGET, M2)              \
137   ITM_READ(T, RaW, TARGET, M2)              \
138   ITM_READ(T, RfW, TARGET, M2)              \
139   ITM_WRITE(T, W, TARGET, M2)               \
140   ITM_WRITE(T, WaR, TARGET, M2)             \
141   ITM_WRITE(T, WaW, TARGET, M2)
142
143 // Creates ABI memcpy/memmove/memset functions.
144 #define ITM_MEMTRANSFER_DEF(TARGET, M2, NAME, READ, WRITE) \
145 void ITM_REGPARM _ITM_memcpy##NAME(void *dst, const void *src, size_t size)  \
146 {                                                                            \
147   TARGET memtransfer##M2 (dst, src, size,                                   \
148              false, GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ);      \
149 }                                                                            \
150 void ITM_REGPARM _ITM_memmove##NAME(void *dst, const void *src, size_t size) \
151 {                                                                            \
152   TARGET memtransfer##M2 (dst, src, size,                                   \
153       GTM::abi_dispatch::memmove_overlap_check(dst, src, size,               \
154           GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ),                \
155       GTM::abi_dispatch::WRITE, GTM::abi_dispatch::READ);                    \
156 }
157
158 #define ITM_MEMSET_DEF(TARGET, M2, WRITE) \
159 void ITM_REGPARM _ITM_memset##WRITE(void *dst, int c, size_t size) \
160 {                                                                  \
161   TARGET memset##M2 (dst, c, size, GTM::abi_dispatch::WRITE);     \
162 }                                                                  \
163
164
165 // ??? The number of virtual methods is large (7*4 for integers, 7*6 for FP,
166 // 7*3 for vectors). Is the cache footprint so costly that we should go for
167 // a small table instead (i.e., only have two virtual load/store methods for
168 // each supported type)? Note that this doesn't affect custom code paths at
169 // all because these use only direct calls.
170 // A large cache footprint could especially decrease HTM performance (due
171 // to HTM capacity). We could add the modifier (RaR etc.) as parameter, which
172 // would give us just 4*2+6*2+3*2 functions (so we'd just need one line for
173 // the integer loads/stores), but then the modifier can be checked only at
174 // runtime.
175 // For memcpy/memmove/memset, we just have two virtual methods (memtransfer
176 // and memset).
177 #define CREATE_DISPATCH_FUNCTIONS(TARGET, M2)  \
178   CREATE_DISPATCH_FUNCTIONS_T (U1, TARGET, M2) \
179   CREATE_DISPATCH_FUNCTIONS_T (U2, TARGET, M2) \
180   CREATE_DISPATCH_FUNCTIONS_T (U4, TARGET, M2) \
181   CREATE_DISPATCH_FUNCTIONS_T (U8, TARGET, M2) \
182   CREATE_DISPATCH_FUNCTIONS_T (F, TARGET, M2)  \
183   CREATE_DISPATCH_FUNCTIONS_T (D, TARGET, M2)  \
184   CREATE_DISPATCH_FUNCTIONS_T (E, TARGET, M2)  \
185   CREATE_DISPATCH_FUNCTIONS_T (CF, TARGET, M2) \
186   CREATE_DISPATCH_FUNCTIONS_T (CD, TARGET, M2) \
187   CREATE_DISPATCH_FUNCTIONS_T (CE, TARGET, M2) \
188   ITM_MEMTRANSFER_DEF(TARGET, M2, RnWt,     NONTXNAL, W)      \
189   ITM_MEMTRANSFER_DEF(TARGET, M2, RnWtaR,   NONTXNAL, WaR)    \
190   ITM_MEMTRANSFER_DEF(TARGET, M2, RnWtaW,   NONTXNAL, WaW)    \
191   ITM_MEMTRANSFER_DEF(TARGET, M2, RtWn,     R,      NONTXNAL) \
192   ITM_MEMTRANSFER_DEF(TARGET, M2, RtWt,     R,      W)        \
193   ITM_MEMTRANSFER_DEF(TARGET, M2, RtWtaR,   R,      WaR)      \
194   ITM_MEMTRANSFER_DEF(TARGET, M2, RtWtaW,   R,      WaW)      \
195   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWn,   RaR,    NONTXNAL) \
196   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWt,   RaR,    W)        \
197   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWtaR, RaR,    WaR)      \
198   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaRWtaW, RaR,    WaW)      \
199   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWn,   RaW,    NONTXNAL) \
200   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWt,   RaW,    W)        \
201   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWtaR, RaW,    WaR)      \
202   ITM_MEMTRANSFER_DEF(TARGET, M2, RtaWWtaW, RaW,    WaW)      \
203   ITM_MEMSET_DEF(TARGET, M2, W)   \
204   ITM_MEMSET_DEF(TARGET, M2, WaR) \
205   ITM_MEMSET_DEF(TARGET, M2, WaW)
206
207
208 // Creates ABI load/store functions that delegate to a transactional memcpy.
209 #define ITM_READ_MEMCPY(T, LSMOD, TARGET, M2)                         \
210   _ITM_TYPE_##T ITM_REGPARM _ITM_##LSMOD##T (const _ITM_TYPE_##T *ptr)\
211   {                                                                   \
212     _ITM_TYPE_##T v;                                                  \
213     TARGET memtransfer##M2(&v, ptr, sizeof(_ITM_TYPE_##T), false,    \
214         GTM::abi_dispatch::NONTXNAL, GTM::abi_dispatch::LSMOD);       \
215     return v;                                                         \
216   }
217
218 #define ITM_WRITE_MEMCPY(T, LSMOD, TARGET, M2)                            \
219   void ITM_REGPARM _ITM_##LSMOD##T (_ITM_TYPE_##T *ptr, _ITM_TYPE_##T val)\
220   {                                                                       \
221     TARGET memtransfer##M2(ptr, &val, sizeof(_ITM_TYPE_##T), false,      \
222         GTM::abi_dispatch::LSMOD, GTM::abi_dispatch::NONTXNAL);           \
223   }
224
225 #define CREATE_DISPATCH_FUNCTIONS_T_MEMCPY(T, TARGET, M2) \
226   ITM_READ_MEMCPY(T, R, TARGET, M2)                \
227   ITM_READ_MEMCPY(T, RaR, TARGET, M2)              \
228   ITM_READ_MEMCPY(T, RaW, TARGET, M2)              \
229   ITM_READ_MEMCPY(T, RfW, TARGET, M2)              \
230   ITM_WRITE_MEMCPY(T, W, TARGET, M2)               \
231   ITM_WRITE_MEMCPY(T, WaR, TARGET, M2)             \
232   ITM_WRITE_MEMCPY(T, WaW, TARGET, M2)
233
234
235 namespace GTM HIDDEN {
236
237 struct gtm_transaction_cp;
238
239 struct method_group
240 {
241   // Start using a TM method from this group. This constructs required meta
242   // data on demand when this method group is actually used. Will be called
243   // either on first use or after a previous call to fini().
244   virtual void init() = 0;
245   // Stop using any method from this group for now. This can be used to
246   // destruct meta data as soon as this method group is not used anymore.
247   virtual void fini() = 0;
248 };
249
250
251 // This is the base interface that all TM methods have to implement.
252 struct abi_dispatch
253 {
254 public:
255   enum ls_modifier { NONTXNAL, R, RaR, RaW, RfW, W, WaR, WaW };
256
257 private:
258   // Disallow copies
259   abi_dispatch(const abi_dispatch &) = delete;
260   abi_dispatch& operator=(const abi_dispatch &) = delete;
261
262 public:
263   // Starts or restarts a transaction. Is called right before executing the
264   // transactional application code (by either returning from
265   // gtm_thread::begin_transaction or doing the longjmp when restarting).
266   // Returns NO_RESTART if the transaction started successfully. Returns
267   // a real restart reason if it couldn't start and does need to abort. This
268   // allows TM methods to just give up and delegate ensuring progress to the
269   // restart mechanism. If it returns a restart reason, this call must be
270   // idempotent because it will trigger the restart mechanism, which could
271   // switch to a different TM method.
272   virtual gtm_restart_reason begin_or_restart() = 0;
273   // Tries to commit the transaction. Iff this returns true, the transaction
274   // got committed and all per-transaction data will have been reset.
275   // Currently, this is called only for the commit of the outermost
276   // transaction, or when switching to serial mode (which can happen in a
277   // nested transaction).
278   // If privatization safety must be ensured in a quiescence-based way, set
279   // priv_time to a value different to 0. Nontransactional code will not be
280   // executed after this commit until all registered threads' shared_state is
281   // larger than or equal to this value.
282   virtual bool trycommit(gtm_word& priv_time) = 0;
283   // Rolls back a transaction. Called on abort or after trycommit() returned
284   // false.
285   virtual void rollback(gtm_transaction_cp *cp = 0) = 0;
286
287   // Return an alternative method that is compatible with the current
288   // method but supports closed nesting. Return zero if there is none.
289   // Note that too be compatible, it must be possible to switch to this other
290   // method on begin of a nested transaction without committing or restarting
291   // the parent method.
292   virtual abi_dispatch* closed_nesting_alternative() { return 0; }
293
294   bool read_only () const { return m_read_only; }
295   bool write_through() const { return m_write_through; }
296   bool can_run_uninstrumented_code() const
297   {
298     return m_can_run_uninstrumented_code;
299   }
300   // Returns true iff this TM method supports closed nesting.
301   bool closed_nesting() const { return m_closed_nesting; }
302   method_group* get_method_group() const { return m_method_group; }
303
304   static void *operator new(size_t s) { return xmalloc (s); }
305   static void operator delete(void *p) { free (p); }
306
307 public:
308   static bool memmove_overlap_check(void *dst, const void *src, size_t size,
309       ls_modifier dst_mod, ls_modifier src_mod);
310
311   // Creates the ABI dispatch methods for loads and stores.
312   // ??? Should the dispatch table instead be embedded in the dispatch object
313   // to avoid the indirect lookup in the vtable?
314   CREATE_DISPATCH_METHODS_PV(virtual, )
315   // Creates the ABI dispatch methods for memcpy/memmove/memset.
316   CREATE_DISPATCH_METHODS_MEM_PV()
317
318 protected:
319   const bool m_read_only;
320   const bool m_write_through;
321   const bool m_can_run_uninstrumented_code;
322   const bool m_closed_nesting;
323   method_group* const m_method_group;
324   abi_dispatch(bool ro, bool wt, bool uninstrumented, bool closed_nesting,
325       method_group* mg) :
326     m_read_only(ro), m_write_through(wt),
327     m_can_run_uninstrumented_code(uninstrumented),
328     m_closed_nesting(closed_nesting), m_method_group(mg)
329   { }
330 };
331
332 }
333
334 #endif // DISPATCH_H