Merge pull request #3484 from Dmitry-Me/fixBrokenCondition
[platform/upstream/coreclr.git] / src / vm / fcall.h
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 // FCall.H
5 //
6
7 //
8 // FCall is a high-performance alternative to ECall. Unlike ECall, FCall
9 // methods do not necessarily create a frame.   Jitted code calls directly
10 // to the FCall entry point.   It is possible to do operations that need
11 // to have a frame within an FCall, you need to manually set up the frame
12 // before you do such operations.
13
14 // It is illegal to cause a GC or EH to happen in an FCALL before setting
15 // up a frame.  To prevent accidentally violating this rule, FCALLs turn
16 // on BEGINGCFORBID, which insures that these things can't happen in a 
17 // checked build without causing an ASSERTE.  Once you set up a frame,
18 // this state is turned off as long as the frame is active, and then is
19 // turned on again when the frame is torn down.   This mechanism should
20 // be sufficient to insure that the rules are followed.
21
22 // In general you set up a frame by using the following macros
23
24 //      HELPER_METHOD_FRAME_BEGIN_RET*()    // Use If the FCALL has a return value
25 //      HELPER_METHOD_FRAME_BEGIN*()        // Use If FCALL does not return a value
26 //      HELPER_METHOD_FRAME_END*()              
27
28 // These macros introduce a scope which is protected by an HelperMethodFrame.
29 // In this scope you can do EH or GC.   There are rules associated with 
30 // their use.  In particular
31
32 //      1) These macros can only be used in the body of a FCALL (that is
33 //         something using the FCIMPL* or HCIMPL* macros for their decaration.
34
35 //      2) You may not perform a 'return' within this scope..
36
37 // Compile time errors occur if you try to violate either of these rules.
38
39 // The frame that is set up does NOT protect any GC variables (in particular the
40 // arguments of the FCALL.  Thus you need to do an explicit GCPROTECT once the
41 // frame is established if you need to protect an argument.  There are flavors
42 // of HELPER_METHOD_FRAME that protect a certain number of GC variables.  For
43 // example
44
45 //      HELPER_METHOD_FRAME_BEGIN_RET_2(arg1, arg2)
46
47 // will protect the GC variables arg1, and arg2 as well as erecting the frame.
48
49 // Another invariant that you must be aware of is the need to poll to see if
50 // a GC is needed by some other thread.   Unless the FCALL is VERY short, 
51 // every code path through the FCALL must do such a poll.  The important 
52 // thing here is that a poll will cause a GC, and thus you can only do it
53 // when all you GC variables are protected.   To make things easier 
54 // HELPER_METHOD_FRAMES that protect things automatically do this poll.
55 // If you don't need to protect anything HELPER_METHOD_FRAME_BEGIN_0
56 // will also do the poll. 
57
58 // Sometimes it is convenient to do the poll a the end of the frame, you 
59 // can use HELPER_METHOD_FRAME_BEGIN_NOPOLL and HELPER_METHOD_FRAME_END_POLL
60 // to do the poll at the end.   If somewhere in the middle is the best
61 // place you can do that too with HELPER_METHOD_POLL()
62
63 // You don't need to erect a helper method frame to do a poll.  FC_GC_POLL
64 // can do this (remember all your GC refs will be trashed).  
65
66 // Finally if your method is VERY small, you can get away without a poll,
67 // you have to use FC_GC_POLL_NOT_NEEDED to mark this.
68 // Use sparingly!
69
70 // It is possible to set up the frame as the first operation in the FCALL and
71 // tear it down as the last operation before returning.  This works and is 
72 // reasonably efficient (as good as an ECall), however, if it is the case that
73 // you can defer the setup of the frame to an unlikely code path (exception path)
74 // that is much better.   
75
76 // If you defer setup of the frame, all codepaths leading to the frame setup
77 // must be wrapped with PERMIT_HELPER_METHOD_FRAME_BEGIN/END.  These block
78 // certain compiler optimizations that interfere with the delayed frame setup.
79 // These macros are automatically included in the HCIMPL, FCIMPL, and frame
80 // setup macros.
81
82 // <TODO>TODO: we should have a way of doing a trial allocation (an allocation that
83 // will fail if it would cause a GC).  That way even FCALLs that need to allocate
84 // would not necessarily need to set up a frame.  </TODO>
85
86 // It is common to only need to set up a frame in order to throw an exception.
87 // While this can be done by doing 
88
89 //      HELPER_METHOD_FRAME_BEGIN()         // Use if FCALL does not return a value
90 //      COMPlusThrow(execpt);
91 //      HELPER_METHOD_FRAME_END()           
92
93 // It is more efficient (in space) to use convenience macro FCTHROW that does 
94 // this for you (sets up a frame, and does the throw).
95
96 //      FCTHROW(except)
97
98 // Since FCALLS have to conform to the EE calling conventions and not to C
99 // calling conventions, FCALLS, need to be declared using special macros (FCIMPL*) 
100 // that implement the correct calling conventions.  There are variants of these
101 // macros depending on the number of args, and sometimes the types of the 
102 // arguments. 
103
104 //------------------------------------------------------------------------
105 //    A very simple example:
106 //
107 //      FCIMPL2(INT32, Div, INT32 x, INT32 y)
108 //      {
109 //          if (y == 0) 
110 //              FCThrow(kDivideByZeroException);
111 //          return x/y;
112 //      }
113 //      FCIMPLEND
114 //
115 //
116 // *** WATCH OUT FOR THESE GOTCHAS: ***
117 // ------------------------------------
118 //  - In your FCDECL & FCIMPL protos, don't declare a param as type OBJECTREF
119 //    or any of its deriveds. This will break on the checked build because
120 //    __fastcall doesn't enregister C++ objects (which OBJECTREF is).
121 //    Instead, you need to do something like;
122 //
123 //      FCIMPL(.., .., Object* pObject0)
124 //          OBJECTREF pObject = ObjectToOBJECTREF(pObject0);
125 //      FCIMPL
126 //
127 //    For similar reasons, use Object* rather than OBJECTREF as a return type.  
128 //    Consider either using ObjectToOBJECTREF or calling VALIDATEOBJECTREF
129 //    to make sure your Object* is valid.
130 //
131 //  - FCThrow() must be called directly from your FCall impl function: it
132 //    cannot be called from a subfunction. Calling from a subfunction breaks
133 //    the VC code parsing workaround that lets us recover the callee saved registers.
134 //    Fortunately, you'll get a compile error complaining about an
135 //    unknown variable "__me".
136 //
137 //  - If your FCall returns VOID, you must use FCThrowVoid() rather than
138 //    FCThrow(). This is because FCThrow() has to generate an unexecuted
139 //    "return" statement for the code parser.
140 //
141 //  - If first and/or second argument of your FCall is 64-bit value on x86
142 //    (ie INT64, UINT64 or DOUBLE), you must use "V" versions of FCDECL and 
143 //    FCIMPL macros to enregister arguments correctly. For example, FCDECL3_IVI 
144 //    must be used for FCalls that take 3 arguments and 2nd argument is INT64.
145 //
146 //  - You may use structs for protecting multiple OBJECTREF's simultaneously.
147 //    In these cases, you must use a variant of a helper method frame with PROTECT
148 //    in the name, to ensure all the OBJECTREF's in the struct get protected.
149 //    Also, initialize all the OBJECTREF's first.  Like this:
150 //    
151 //    FCIMPL4(Object*, COMNlsInfo::nativeChangeCaseString, LocaleIDObject* localeUNSAFE,
152 //            INT_PTR pNativeTextInfo, StringObject* pStringUNSAFE, CLR_BOOL bIsToUpper)
153 //    {
154 //      [ignoring CONTRACT for now]
155 //      struct _gc 
156 //      {
157 //          STRINGREF pResult;
158 //          STRINGREF pString;
159 //          LOCALEIDREF pLocale;
160 //      } gc;
161 //      gc.pResult = NULL;
162 //      gc.pString = ObjectToSTRINGREF(pStringUNSAFE);
163 //      gc.pLocale = (LOCALEIDREF)ObjectToOBJECTREF(localeUNSAFE);
164 //  
165 //      HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc)
166 //  
167 //    If you forgot the PROTECT part, the macro will only protect the first OBJECTREF, 
168 //    introducing a subtle GC hole in your code.  Fortunately, we now issue a 
169 //    compile-time error if you forget.
170
171 // How FCall works:
172 // ----------------
173 //   An FCall target uses __fastcall or some other calling convention to
174 //   match the IL calling convention exactly. Thus, a call to FCall is a direct
175 //   call to the target w/ no intervening stub or frame.
176 //
177 //   The tricky part is when FCThrow is called. FCThrow must generate
178 //   a proper method frame before allocating and throwing the exception.
179 //   To do this, it must recover several things:
180 //
181 //      - The location of the FCIMPL's return address (since that's
182 //        where the frame will be based.)
183 //
184 //      - The on-entry values of the callee-saved regs; which must
185 //        be recorded in the frame so that GC can update them.
186 //        Depending on how VC compiles your FCIMPL, those values are still
187 //        in the original registers or saved on the stack.
188 //
189 //        To figure out which, FCThrow() generates the code:
190 //
191 //              while (NULL == __FCThrow(__me, ...)) {};
192 //              return 0;
193 //
194 //        The "return" statement will never execute; but its presence guarantees
195 //        that VC will follow the __FCThrow() call with a VC epilog
196 //        that restores the callee-saved registers using a pretty small
197 //        and predictable set of Intel opcodes. __FCThrow() parses this
198 //        epilog and simulates its execution to recover the callee saved
199 //        registers.
200 //
201 //        The while loop is to prevent the compiler from doing tail call optimizations.
202 //        The helper frame interpretter needs the frame to be present.
203 //
204 //      - The MethodDesc* that this FCall implements. This MethodDesc*
205 //        is part of the frame and ensures that the FCall will appear
206 //        in the exception's stack trace. To get this, FCDECL declares
207 //        a static local __me, initialized to point to the FC target itself.
208 //        This address is exactly what's stored in the ECall lookup tables;
209 //        so __FCThrow() simply does a reverse lookup on that table to recover
210 //        the MethodDesc*.
211 //
212
213
214 #ifndef __FCall_h__
215 #define __FCall_h__
216
217 #include "gms.h"
218 #include "runtimeexceptionkind.h"
219 #include "debugreturn.h"
220 #include "stackprobe.h"
221
222 //==============================================================================================
223 // These macros defeat compiler optimizations that might mix nonvolatile
224 // register loads and stores with other code in the function body.  This
225 // creates problems for the frame setup code, which assumes that any
226 // nonvolatiles that are saved at the point of the frame setup will be
227 // re-loaded when the frame is popped.
228 //
229 // Currently this is only known to be an issue on AMD64.  It's uncertain
230 // whether it is an issue on x86.
231 //==============================================================================================
232
233 #if defined(_TARGET_AMD64_) && !defined(FEATURE_PAL)
234
235 //
236 // On AMD64 this is accomplished by including a setjmp anywhere in a function.
237 // Doesn't matter whether it is reachable or not, and in fact in optimized
238 // builds the setjmp is removed altogether.
239 //
240 #include <setjmp.h>
241
242 //
243 // Use of setjmp is temporary, we will eventually have compiler intrinsics to
244 // disable the optimizations.  Besides, we don't actually execute setjmp in
245 // these macros (or anywhere else in the VM on AMD64).
246 //
247 #pragma warning(disable:4611) // interaction between '_setjmp' and C++ object destruction is non-portable
248
249 #ifdef _DEBUG
250 //
251 // Linked list of unmanaged methods preceeding a HelperMethodFrame push.  This
252 // is linked onto the current Thread.  Each list entry is stack-allocated so it
253 // can be associated with an unmanaged frame.  Each unmanaged frame needs to be
254 // associated with at least one list entry.
255 //
256 struct HelperMethodFrameCallerList
257 {
258     HelperMethodFrameCallerList *pCaller;
259 };
260 #endif // _DEBUG
261
262 //
263 // Resets the Thread state at a new managed -> fcall transition.
264 //
265 class FCallTransitionState
266 {
267 public:
268
269     FCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
270     ~FCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
271
272 #ifdef _DEBUG
273 private:
274     Thread *m_pThread;
275     HelperMethodFrameCallerList *m_pPreviousHelperMethodFrameCallerList;
276 #endif // _DEBUG
277 };
278
279 //
280 // Pushes/pops state for each caller.
281 //
282 class PermitHelperMethodFrameState
283 {
284 public:
285
286     PermitHelperMethodFrameState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
287     ~PermitHelperMethodFrameState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
288
289     static VOID CheckHelperMethodFramePermitted () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
290
291 #ifdef _DEBUG
292 private:
293     Thread *m_pThread;
294     HelperMethodFrameCallerList m_ListEntry;
295 #endif // _DEBUG
296 };
297
298 //
299 // Resets the Thread state after the HelperMethodFrame is pushed.  At this
300 // point, the HelperMethodFrame is capable of unwinding to the managed code,
301 // so we can reset the Thread state for any nested fcalls.
302 //
303 class CompletedFCallTransitionState
304 {
305 public:
306
307     CompletedFCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
308     ~CompletedFCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
309
310 #ifdef _DEBUG
311 private:
312
313     HelperMethodFrameCallerList *m_pLastHelperMethodFrameCallerList;
314 #endif // _DEBUG
315 };
316
317 #define PERMIT_HELPER_METHOD_FRAME_BEGIN()                                  \
318         if (1)                                                              \
319         {                                                                   \
320             PermitHelperMethodFrameState ___PermitHelperMethodFrameState;
321
322 #define PERMIT_HELPER_METHOD_FRAME_END()    \
323         }                                   \
324         else                                \
325         {                                   \
326             jmp_buf ___jmpbuf;              \
327             setjmp(___jmpbuf);              \
328             __assume(0);                    \
329         }
330
331 #define FCALL_TRANSITION_BEGIN()                        \
332         FCallTransitionState ___FCallTransitionState;   \
333         PERMIT_HELPER_METHOD_FRAME_BEGIN();
334
335 #define FCALL_TRANSITION_END()              \
336         PERMIT_HELPER_METHOD_FRAME_END();
337
338 #define CHECK_HELPER_METHOD_FRAME_PERMITTED() \
339         PermitHelperMethodFrameState::CheckHelperMethodFramePermitted(); \
340         CompletedFCallTransitionState ___CompletedFCallTransitionState;
341
342 #else // unsupported processor
343
344 #define PERMIT_HELPER_METHOD_FRAME_BEGIN()
345 #define PERMIT_HELPER_METHOD_FRAME_END()
346 #define FCALL_TRANSITION_BEGIN()
347 #define FCALL_TRANSITION_END()
348 #define CHECK_HELPER_METHOD_FRAME_PERMITTED()
349
350 #endif // unsupported processor
351
352 //==============================================================================================
353 // This is where FCThrow ultimately ends up. Never call this directly.
354 // Use the FCThrow() macros. __FCThrowArgument is the helper to throw ArgumentExceptions
355 // with a resource taken from the managed resource manager.
356 //==============================================================================================
357 LPVOID __FCThrow(LPVOID me, enum RuntimeExceptionKind reKind, UINT resID, LPCWSTR arg1, LPCWSTR arg2, LPCWSTR arg3);
358 LPVOID __FCThrowArgument(LPVOID me, enum RuntimeExceptionKind reKind, LPCWSTR argumentName, LPCWSTR resourceName);
359
360 //==============================================================================================
361 // FDECLn: A set of macros for generating header declarations for FC targets.
362 // Use FIMPLn for the actual body.
363 //==============================================================================================
364
365 // Note: on the x86, these defs reverse all but the first two arguments
366 // (IL stack calling convention is reversed from __fastcall.)
367
368
369 // Calling convention for varargs
370 #define F_CALL_VA_CONV __cdecl
371
372
373 #ifdef _TARGET_X86_
374
375 // Choose the appropriate calling convention for FCALL helpers on the basis of the JIT calling convention
376 #ifdef __GNUC__
377 #define F_CALL_CONV __attribute__((stdcall, regparm(3)))
378 #else
379 #define F_CALL_CONV __fastcall
380 #endif
381
382 #if defined(__GNUC__)
383
384 // GCC fastcall convention is different from MSVC fastcall convention. GCC can use up to 3 registers to
385 // store parameters. The registers used are EAX, EDX, ECX. Dummy parameters and reordering of the 
386 // actual parameters in the FCALL signature is used to make the calling convention to look like in MSVC.
387
388 #define FCDECL0(rettype, funcname) rettype F_CALL_CONV funcname()
389 #define FCDECL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1)
390 #define FCDECL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a1)
391 #define FCDECL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1)
392 #define FCDECL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...)
393 #define FCDECL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a2, a1)
394 #define FCDECL2_VI(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a2, a1)
395 #define FCDECL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a2)
396 #define FCDECL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3)
397 #define FCDECL3_IIV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3)
398 #define FCDECL3_VII(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a2, a1)
399 #define FCDECL3_IVV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a3, a2)
400 #define FCDECL3_IVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a1, a2)
401 #define FCDECL3_VVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a3, a2, a1)
402 #define FCDECL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a4, a3)
403 #define FCDECL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a5, a4, a3)
404 #define FCDECL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a6, a5, a4, a3)
405 #define FCDECL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a7, a6, a5, a4, a3)
406 #define FCDECL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a8, a7, a6, a5, a4, a3)
407 #define FCDECL9(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a9, a8, a7, a6, a5, a4, a3)
408 #define FCDECL10(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a10, a9, a8, a7, a6, a5, a4, a3)
409 #define FCDECL11(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a11, a10, a9, a8, a7, a6, a5, a4, a3)
410 #define FCDECL12(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3)
411 #define FCDECL13(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3)
412 #define FCDECL14(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a14, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3)
413
414 #define FCDECL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a3, a1, a5, a4, a2)
415 #define FCDECL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a3, a2, a5, a4, a1)
416
417 #else // __GNUC__
418
419 #define FCDECL0(rettype, funcname) rettype F_CALL_CONV funcname()
420 #define FCDECL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1)
421 #define FCDECL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1)
422 #define FCDECL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2)
423 #define FCDECL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...)
424 #define FCDECL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a2, a1)
425 #define FCDECL2_VI(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a2, a1)
426 #define FCDECL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2)
427 #define FCDECL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3)
428 #define FCDECL3_IIV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3)
429 #define FCDECL3_VII(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a2, a3, a1)
430 #define FCDECL3_IVV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a3, a2)
431 #define FCDECL3_IVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a3, a2)
432 #define FCDECL3_VVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a2, a1, a3)
433 #define FCDECL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(a1, a2, a4, a3)
434 #define FCDECL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a2, a5, a4, a3)
435 #define FCDECL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype F_CALL_CONV funcname(a1, a2, a6, a5, a4, a3)
436 #define FCDECL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype F_CALL_CONV funcname(a1, a2, a7, a6, a5, a4, a3)
437 #define FCDECL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) rettype F_CALL_CONV funcname(a1, a2, a8, a7, a6, a5, a4, a3)
438 #define FCDECL9(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) rettype F_CALL_CONV funcname(a1, a2, a9, a8, a7, a6, a5, a4, a3)
439 #define FCDECL10(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) rettype F_CALL_CONV funcname(a1, a2, a10, a9, a8, a7, a6, a5, a4, a3)
440 #define FCDECL11(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) rettype F_CALL_CONV funcname(a1, a2, a11, a10, a9, a8, a7, a6, a5, a4, a3)
441 #define FCDECL12(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) rettype F_CALL_CONV funcname(a1, a2, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3)
442 #define FCDECL13(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) rettype F_CALL_CONV funcname(a1, a2, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3)
443 #define FCDECL14(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) rettype F_CALL_CONV funcname(a1, a2, a14, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3)
444
445 #define FCDECL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a3, a5, a4, a2)
446 #define FCDECL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a2, a3, a5, a4, a1)
447
448 #endif // __GNUC__
449
450 #if 0
451 //
452 // don't use something like this... directly calling an FCALL from within the runtime breaks stackwalking because
453 // the FCALL reverse mapping only gets established in ECall::GetFCallImpl and that codepath is circumvented by 
454 // directly calling and FCALL
455 // See below for usage of FC_CALL_INNER (used in SecurityStackWalk::Check presently)
456 //
457 #define FCCALL0(funcname) funcname()
458 #define FCCALL1(funcname, a1) funcname(a1)
459 #define FCCALL2(funcname, a1, a2) funcname(a1, a2)
460 #define FCCALL3(funcname, a1, a2, a3) funcname(a1, a2, a3)
461 #define FCCALL4(funcname, a1, a2, a3, a4) funcname(a1, a2, a4, a3)
462 #define FCCALL5(funcname, a1, a2, a3, a4, a5) funcname(a1, a2, a5, a4, a3)
463 #define FCCALL6(funcname, a1, a2, a3, a4, a5, a6) funcname(a1, a2, a6, a5, a4, a3)
464 #define FCCALL7(funcname, a1, a2, a3, a4, a5, a6, a7) funcname(a1, a2, a7, a6, a5, a4, a3)
465 #define FCCALL8(funcname, a1, a2, a3, a4, a5, a6, a7, a8) funcname(a1, a2, a8, a7, a6, a5, a4, a3)
466 #define FCCALL9(funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) funcname(a1, a2, a9, a8, a7, a6, a5, a4, a3)
467 #define FCCALL10(funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) funcname(a1, a2, a10, a9, a8, a7, a6, a5, a4, a3)
468 #define FCCALL11(funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) funcname(a1, a2, a11, a10, a9, a8, a7, a6, a5, a4, a3)
469 #define FCCALL12(funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) funcname(a1, a2, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3)
470 #endif // 0
471
472 #else // !_TARGET_X86
473
474 #define F_CALL_CONV
475
476 #define FCDECL0(rettype, funcname) rettype funcname()
477 #define FCDECL1(rettype, funcname, a1) rettype funcname(a1)
478 #define FCDECL1_V(rettype, funcname, a1) rettype funcname(a1)
479 #define FCDECL2(rettype, funcname, a1, a2) rettype funcname(a1, a2)
480 #define FCDECL2VA(rettype, funcname, a1, a2) rettype funcname(a1, a2, ...)
481 #define FCDECL2_VV(rettype, funcname, a1, a2) rettype funcname(a1, a2)
482 #define FCDECL2_VI(rettype, funcname, a1, a2) rettype funcname(a1, a2)
483 #define FCDECL2_IV(rettype, funcname, a1, a2) rettype funcname(a1, a2)
484 #define FCDECL3(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
485 #define FCDECL3_IIV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
486 #define FCDECL3_VII(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
487 #define FCDECL3_IVV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
488 #define FCDECL3_IVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
489 #define FCDECL3_VVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
490 #define FCDECL4(rettype, funcname, a1, a2, a3, a4) rettype funcname(a1, a2, a3, a4)
491 #define FCDECL5(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5)
492 #define FCDECL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype funcname(a1, a2, a3, a4, a5, a6)
493 #define FCDECL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype funcname(a1, a2, a3, a4, a5, a6, a7)
494 #define FCDECL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8)
495 #define FCDECL9(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9)
496 #define FCDECL10(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
497 #define FCDECL11(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)
498 #define FCDECL12(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
499 #define FCDECL13(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13)
500 #define FCDECL14(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14)
501
502 #define FCDECL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5)
503 #define FCDECL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5)
504
505 #endif // _TARGET_X86_ 
506
507 #define HELPER_FRAME_DECL(x) FrameWithCookie<HelperMethodFrame_##x##OBJ> __helperframe
508
509 // use the capture state machinery if the architecture has one
510 //
511 // For a normal build we create a loop (see explaination on RestoreState below)
512 // We don't want a loop here for PREFAST since that causes 
513 //   warning 263: Using _alloca in a loop
514 // And we can't use DEBUG_OK_TO_RETURN for PREFAST because the PREFAST version 
515 // requires that you already be in a DEBUG_ASSURE_NO_RETURN_BEGIN scope 
516
517 #define HelperMethodFrame_0OBJ      HelperMethodFrame
518 #define HELPER_FRAME_ARGS(attribs)  __me, attribs
519 #define FORLAZYMACHSTATE(x) x
520
521 #if defined(_PREFAST_)
522   #define FORLAZYMACHSTATE_BEGINLOOP(x) x
523   #define FORLAZYMACHSTATE_ENDLOOP(x)
524   #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_BEGIN
525   #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_END
526 #else
527   #define FORLAZYMACHSTATE_BEGINLOOP(x) x do
528   #define FORLAZYMACHSTATE_ENDLOOP(x) while(x)
529   #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_BEGIN  DEBUG_OK_TO_RETURN_BEGIN(LAZYMACHSTATE)
530   #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_END    DEBUG_OK_TO_RETURN_END(LAZYMACHSTATE)
531 #endif
532
533 // BEGIN: before gcpoll
534 //FCallGCCanTriggerNoDtor __fcallGcCanTrigger;        
535 //__fcallGcCanTrigger.Enter();                        
536
537 // END: after gcpoll
538 //__fcallGcCanTrigger.Leave(__FUNCTION__, __FILE__, __LINE__);    
539
540 // We have to put DEBUG_OK_TO_RETURN_BEGIN around the FORLAZYMACHSTATE
541 // to allow the HELPER_FRAME to be installed inside an SO_INTOLERANT region
542 // which does not allow a return.  The return is used by FORLAZYMACHSTATE
543 // to capture the state, but is not an actual return, so it is ok.
544 #define HELPER_METHOD_FRAME_BEGIN_EX_BODY(ret, helperFrame, gcpoll, allowGC)  \
545         FORLAZYMACHSTATE_BEGINLOOP(int alwaysZero = 0;)         \
546         {                                                       \
547             INDEBUG(static BOOL __haveCheckedRestoreState = FALSE;)     \
548             PERMIT_HELPER_METHOD_FRAME_BEGIN();                 \
549             CHECK_HELPER_METHOD_FRAME_PERMITTED();              \
550             helperFrame;                                        \
551             FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_BEGIN;          \
552             FORLAZYMACHSTATE(CAPTURE_STATE(__helperframe.MachineState(), ret);) \
553             FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_END;            \
554             INDEBUG(__helperframe.SetAddrOfHaveCheckedRestoreState(&__haveCheckedRestoreState)); \
555             DEBUG_ASSURE_NO_RETURN_BEGIN(HELPER_METHOD_FRAME);  \
556             INCONTRACT(FCallGCCanTrigger::Enter());             \
557             __helperframe.Push();                               \
558             MAKE_CURRENT_THREAD_AVAILABLE_EX(__helperframe.GetThread()); \
559
560 #define HELPER_METHOD_FRAME_BEGIN_EX(ret, helperFrame, gcpoll, allowGC)         \
561         HELPER_METHOD_FRAME_BEGIN_EX_BODY(ret, helperFrame, gcpoll, allowGC)    \
562             TESTHOOKCALL(AppDomainCanBeUnloaded(GET_THREAD()->GetDomain()->GetId().m_dwId,!allowGC)); \
563             /* <TODO>TODO TURN THIS ON!!!   </TODO> */                    \
564             /* gcpoll; */                                                       \
565             INSTALL_MANAGED_EXCEPTION_DISPATCHER;                               \
566             INSTALL_UNWIND_AND_CONTINUE_HANDLER_FOR_HMF(&__helperframe);
567
568 #define HELPER_METHOD_FRAME_BEGIN_EX_NOTHROW(ret, helperFrame, gcpoll, allowGC, probeFailExpr) \
569         HELPER_METHOD_FRAME_BEGIN_EX_BODY(ret, helperFrame, gcpoll, allowGC)    \
570             /* <TODO>TODO TURN THIS ON!!!   </TODO> */                    \
571             /* gcpoll; */                                                       \
572             BEGIN_SO_INTOLERANT_CODE_NOTHROW(GET_THREAD(), probeFailExpr);
573
574
575 // The while(__helperframe.RestoreState() needs a bit of explanation.
576 // The issue is insuring that the same machine state (which registers saved)
577 // exists when the machine state is probed (when the frame is created, and
578 // when it is actually used (when the frame is popped.  We do this by creating
579 // a flow of control from use to def.  Note that 'RestoreState' always returns false
580 // we never actually loop, but the compiler does not know that, and thus
581 // will be forced to make the keep the state of register spills the same at
582 // the two locations.
583
584 #define HELPER_METHOD_FRAME_END_EX_BODY(gcpoll,allowGC) \
585             /* <TODO>TODO TURN THIS ON!!!   </TODO> */                \
586             /* gcpoll; */                                                   \
587             __helperframe.Pop();                                            \
588             DEBUG_ASSURE_NO_RETURN_END(HELPER_METHOD_FRAME);                \
589             INCONTRACT(FCallGCCanTrigger::Leave(__FUNCTION__, __FILE__, __LINE__)); \
590             FORLAZYMACHSTATE(alwaysZero =                                   \
591             HelperMethodFrameRestoreState(INDEBUG_COMMA(&__helperframe)     \
592                                           __helperframe.MachineState());)   \
593             PERMIT_HELPER_METHOD_FRAME_END()                                \
594         } FORLAZYMACHSTATE_ENDLOOP(alwaysZero);
595
596 #define HELPER_METHOD_FRAME_END_EX(gcpoll,allowGC)                          \
597             UNINSTALL_UNWIND_AND_CONTINUE_HANDLER;                          \
598             UNINSTALL_MANAGED_EXCEPTION_DISPATCHER;                         \
599             TESTHOOKCALL(AppDomainCanBeUnloaded(GET_THREAD()->GetDomain()->GetId().m_dwId,!allowGC)); \
600         HELPER_METHOD_FRAME_END_EX_BODY(gcpoll,allowGC);
601
602 #define HELPER_METHOD_FRAME_END_EX_NOTHROW(gcpoll,allowGC)                  \
603             END_SO_INTOLERANT_CODE;                                         \
604         HELPER_METHOD_FRAME_END_EX_BODY(gcpoll,allowGC);
605
606 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB(attribs)                                       \
607         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
608             return,                                                                     \
609             HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)),                           \
610             HELPER_METHOD_POLL(),TRUE)
611
612 #define HELPER_METHOD_FRAME_BEGIN_0()                                                   \
613         HELPER_METHOD_FRAME_BEGIN_ATTRIB(Frame::FRAME_ATTR_NONE)
614
615 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL(attribs)                                \
616         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
617             return,                                                                     \
618             HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)),                           \
619             {},FALSE)
620
621 #define HELPER_METHOD_FRAME_BEGIN_NOPOLL() HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL(Frame::FRAME_ATTR_NONE)
622
623 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB_1(attribs, arg1)                               \
624         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
625         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
626             return,                                                                     \
627             HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(attribs),                            \
628                 (OBJECTREF*) &arg1),                                                    \
629             HELPER_METHOD_POLL(),TRUE)
630
631 #define HELPER_METHOD_FRAME_BEGIN_1(arg1)  HELPER_METHOD_FRAME_BEGIN_ATTRIB_1(Frame::FRAME_ATTR_NONE, arg1)
632
633 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB_2(attribs, arg1, arg2)                         \
634         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
635         static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
636         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
637             return,                                                                     \
638             HELPER_FRAME_DECL(2)(HELPER_FRAME_ARGS(attribs),                            \
639                 (OBJECTREF*) &arg1, (OBJECTREF*) &arg2),                                \
640             HELPER_METHOD_POLL(),TRUE)
641
642 #define HELPER_METHOD_FRAME_BEGIN_2(arg1, arg2) HELPER_METHOD_FRAME_BEGIN_ATTRIB_2(Frame::FRAME_ATTR_NONE, arg1, arg2)
643
644 #define HELPER_METHOD_FRAME_BEGIN_PROTECT(gc)                                           \
645         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
646             return,                                                                     \
647             HELPER_FRAME_DECL(PROTECT)(HELPER_FRAME_ARGS(Frame::FRAME_ATTR_NONE),       \
648                 (OBJECTREF*)&(gc), sizeof(gc)/sizeof(OBJECTREF)),                       \
649             HELPER_METHOD_POLL(),TRUE)
650
651 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_NOPOLL(attribs)                            \
652         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
653             return 0,                                                                   \
654             HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)),                           \
655             {},FALSE)
656
657 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_NOPOLL(attribs)                         \
658         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
659             FC_RETURN_VC(),                                                             \
660             HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)),                           \
661             {},FALSE)
662
663 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB(attribs)                                   \
664         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
665             return 0,                                                                   \
666             HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)),                           \
667             HELPER_METHOD_POLL(),TRUE)
668
669 #define HELPER_METHOD_FRAME_BEGIN_RET_0()                                               \
670         HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB(Frame::FRAME_ATTR_NONE)
671
672 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_0()                                            \
673         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
674             FC_RETURN_VC(),                                                             \
675             HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(Frame::FRAME_ATTR_NONE)),            \
676             HELPER_METHOD_POLL(),TRUE)
677
678 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(attribs, arg1)                           \
679         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
680         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
681             return 0,                                                                   \
682             HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(attribs),                            \
683                 (OBJECTREF*) &arg1),                                                    \
684             HELPER_METHOD_POLL(),TRUE)
685
686 #define HELPER_METHOD_FRAME_BEGIN_RET_NOTHROW_1(probeFailExpr, arg1)                    \
687         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
688         HELPER_METHOD_FRAME_BEGIN_EX_NOTHROW(                                           \
689             return 0,                                                                   \
690             HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(Frame::FRAME_ATTR_NO_THREAD_ABORT),  \
691                 (OBJECTREF*) &arg1),                                                    \
692             HELPER_METHOD_POLL(), TRUE, probeFailExpr)
693
694 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_1(attribs, arg1)                        \
695         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
696         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
697             FC_RETURN_VC(),                                                             \
698             HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(attribs),                            \
699                 (OBJECTREF*) &arg1),                                                    \
700             HELPER_METHOD_POLL(),TRUE)
701
702 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(attribs, arg1, arg2)                     \
703         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
704         static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
705         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
706             return 0,                                                                   \
707             HELPER_FRAME_DECL(2)(HELPER_FRAME_ARGS(attribs),                            \
708                 (OBJECTREF*) &arg1, (OBJECTREF*) &arg2),                                \
709             HELPER_METHOD_POLL(),TRUE)
710
711 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_2(attribs, arg1, arg2)                  \
712         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
713         static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
714         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
715             FC_RETURN_VC(),                                                             \
716             HELPER_FRAME_DECL(2)(HELPER_FRAME_ARGS(attribs),                            \
717                 (OBJECTREF*) &arg1, (OBJECTREF*) &arg2),                                \
718             HELPER_METHOD_POLL(),TRUE)
719
720 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_PROTECT(attribs, gc)                       \
721         HELPER_METHOD_FRAME_BEGIN_EX(                                                   \
722             return 0,                                                                   \
723             HELPER_FRAME_DECL(PROTECT)(HELPER_FRAME_ARGS(attribs),                      \
724                 (OBJECTREF*)&(gc), sizeof(gc)/sizeof(OBJECTREF)),                       \
725             HELPER_METHOD_POLL(),TRUE)
726
727 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_NOPOLL()                                       \
728         HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_NOPOLL(Frame::FRAME_ATTR_NONE)
729
730 #define HELPER_METHOD_FRAME_BEGIN_RET_NOPOLL()                                          \
731         HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_NOPOLL(Frame::FRAME_ATTR_NONE)
732
733 #define HELPER_METHOD_FRAME_BEGIN_RET_1(arg1)                                           \
734         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
735         HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_NONE, arg1)
736
737 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_1(arg1)                                        \
738         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
739         HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_1(Frame::FRAME_ATTR_NONE, arg1)
740
741 #define HELPER_METHOD_FRAME_BEGIN_RET_2(arg1, arg2)                                     \
742         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
743         static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
744         HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(Frame::FRAME_ATTR_NONE, arg1, arg2)
745
746 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_2(arg1, arg2)                                  \
747         static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
748         static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
749         HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_2(Frame::FRAME_ATTR_NONE, arg1, arg2)
750
751 #define HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc)                                       \
752         HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_PROTECT(Frame::FRAME_ATTR_NONE, gc)
753
754
755 #define HELPER_METHOD_FRAME_END()        HELPER_METHOD_FRAME_END_EX({},FALSE)  
756 #define HELPER_METHOD_FRAME_END_POLL()   HELPER_METHOD_FRAME_END_EX(HELPER_METHOD_POLL(),TRUE)  
757 #define HELPER_METHOD_FRAME_END_NOTHROW()HELPER_METHOD_FRAME_END_EX_NOTHROW({},FALSE)  
758
759 // This is the fastest way to do a GC poll if you have already erected a HelperMethodFrame
760 #define HELPER_METHOD_POLL()            { __helperframe.Poll(); INCONTRACT(__fCallCheck.SetDidPoll()); }
761
762 // The HelperMethodFrame knows how to get its return address.  Let other code get at it, too.
763 //  (Uses comma operator to call InsureInit & discard result.
764 #define HELPER_METHOD_FRAME_GET_RETURN_ADDRESS()                                        \
765     ( static_cast<UINT_PTR>( (__helperframe.InsureInit(false, NULL)), (__helperframe.MachineState()->GetRetAddr()) ) )
766
767     // Very short routines, or routines that are guarenteed to force GC or EH 
768     // don't need to poll the GC.  USE VERY SPARINGLY!!!
769 #define FC_GC_POLL_NOT_NEEDED()    INCONTRACT(__fCallCheck.SetNotNeeded()) 
770
771 Object* FC_GCPoll(void* me, Object* objToProtect = NULL);
772
773 #define FC_GC_POLL_EX(ret)                                  \
774     {                                                       \
775         INCONTRACT(Thread::TriggersGC(GetThread());)        \
776         INCONTRACT(__fCallCheck.SetDidPoll();)              \
777         if (g_TrapReturningThreads.LoadWithoutBarrier())    \
778         {                                                   \
779             if (FC_GCPoll(__me))                            \
780                 return ret;                                 \
781             while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */  \
782         }                                                   \
783     }
784
785 #define FC_GC_POLL()        FC_GC_POLL_EX(;)
786 #define FC_GC_POLL_RET()    FC_GC_POLL_EX(0)
787
788 #define FC_GC_POLL_AND_RETURN_OBJREF(obj)                   \
789     {                                                       \
790         INCONTRACT(__fCallCheck.SetDidPoll();)              \
791         Object* __temp = OBJECTREFToObject(obj);            \
792         if (g_TrapReturningThreads.LoadWithoutBarrier())    \
793         {                                                   \
794             __temp = FC_GCPoll(__me, __temp);               \
795             while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */  \
796         }                                                   \
797         return __temp;                                      \
798     }
799
800 #if defined(ENABLE_CONTRACTS)
801 #define FC_CAN_TRIGGER_GC()         FCallGCCanTrigger::Enter()
802 #define FC_CAN_TRIGGER_GC_END()     FCallGCCanTrigger::Leave(__FUNCTION__, __FILE__, __LINE__)
803
804 #define FC_CAN_TRIGGER_GC_HAVE_THREAD(thread)       FCallGCCanTrigger::Enter(thread)
805 #define FC_CAN_TRIGGER_GC_HAVE_THREADEND(thread)    FCallGCCanTrigger::Leave(thread, __FUNCTION__, __FILE__, __LINE__)
806
807     // turns on forbidGC for the lifetime of the instance
808 class ForbidGC {
809 protected:
810     Thread *m_pThread;
811 public:
812     ForbidGC(const char *szFile, int lineNum);
813     ~ForbidGC();
814 };
815
816     // this little helper class checks to make certain
817     // 1) ForbidGC is set throughout the routine.
818     // 2) Sometime during the routine, a GC poll is done
819
820 class FCallCheck : public ForbidGC {
821 public:
822     FCallCheck(const char *szFile, int lineNum);
823     ~FCallCheck();
824     void SetDidPoll()       {LIMITED_METHOD_CONTRACT;  didGCPoll = true; }
825     void SetNotNeeded()     {LIMITED_METHOD_CONTRACT;  notNeeded = true; }
826
827 private:
828 #ifdef _DEBUG
829     DWORD         unbreakableLockCount;
830 #endif
831     bool          didGCPoll;            // GC poll was done
832     bool          notNeeded;            // GC poll not needed
833     unsigned __int64 startTicks;        // tick count at begining of FCall
834 };
835
836         // FC_COMMON_PROLOG is used for both FCalls and HCalls
837 #define FC_COMMON_PROLOG(target, assertFn)      \
838         /* The following line has to be first.  We do not want to trash last error */ \
839         DWORD __lastError = ::GetLastError();   \
840         static void* __cache = 0;               \
841         assertFn(__cache, (LPVOID)target);      \
842         {                                       \
843             Thread *_pThread = GetThread();     \
844             Thread::ObjectRefFlush(_pThread);    \
845             /*_ASSERTE (_pThread->IsSOTolerant() ||*/ \
846             /*       _pThread->HasThreadStateNC(Thread::TSNC_DisableSOCheckInHCALL)); */    \
847         }                                       \
848         FCallCheck __fCallCheck(__FILE__, __LINE__); \
849         FCALL_TRANSITION_BEGIN(); \
850         ::SetLastError(__lastError);            \
851
852 void FCallAssert(void*& cache, void* target);       
853 void HCallAssert(void*& cache, void* target);
854
855 #else
856 #define FC_COMMON_PROLOG(target, assertFn) FCALL_TRANSITION_BEGIN()
857 #define FC_CAN_TRIGGER_GC() 
858 #define FC_CAN_TRIGGER_GC_END() 
859 #endif // ENABLE_CONTRACTS
860
861 // #FC_INNER
862 // Macros that allows fcall to be split into two function to avoid the helper frame overhead on common fast
863 // codepaths.
864 // 
865 // The helper routine needs to know the name of the routine that called it so that it can look up the name of
866 // the managed routine this code is associted with (for managed stack traces). This is passed with the
867 // FC_INNER_PROLOG macro.
868 // 
869 // The helper can set up a HELPER_METHOD_FRAME, but should pass the
870 // Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2 which indicates the exact number of
871 // unwinds to do to get back to managed code. Currently we only support depth 2 which means that the
872 // HELPER_METHOD_FRAME needs to be set up in the function directly called by the FCALL. The helper should
873 // use the NOINLINE macro to prevent the compiler from inlining it into the FCALL (which would obviously
874 // mess up the unwind count).
875 // 
876 // The other invarient that needs to hold is that the epilog walker needs to be able to get from the call to
877 // the helper routine to the end of the FCALL using trivial heurisitics.   The easiest (and only supported)
878 // way of doing this is to place your helper right before a return (eg at the end of the method).  Generally
879 // this is not a problem at all, since the FCALL itself will pick off some common case and then tail-call to
880 // the helper for everything else.  You must use the code:FC_INNER_RETURN macros to do the call, to insure
881 // that the C++ compiler does not tail-call optimize the call to the inner function and mess up the stack
882 // depth. 
883 // 
884 // see code:ObjectNative::GetClass for an example
885 //
886 #define FC_INNER_PROLOG(outerfuncname)                          \
887     LPVOID __me;                                                \
888     __me = GetEEFuncEntryPointMacro(outerfuncname);             \
889     FC_CAN_TRIGGER_GC();                                        \
890     INCONTRACT(FCallCheck __fCallCheck(__FILE__, __LINE__));
891
892 // This variant should be used for inner fcall functions that have the
893 // __me value passed as an argument to the function. This allows
894 // inner functions to be shared across multiple fcalls.
895 #define FC_INNER_PROLOG_NO_ME_SETUP()                           \
896     FC_CAN_TRIGGER_GC();                                        \
897     INCONTRACT(FCallCheck __fCallCheck(__FILE__, __LINE__));
898
899 #define FC_INNER_EPILOG()                                       \
900     FC_CAN_TRIGGER_GC_END(); 
901
902 // If you are using FC_INNER, and you are tail calling to the helper method (a common case), then you need
903 // to use the FC_INNER_RETURN macros (there is one for methods that return a value and another if the
904 // function returns void).  This macro's purpose is to inhibit any tail calll optimization the C++ compiler 
905 // might do, which would otherwise confuse the epilog walker.  
906 // 
907 // * See #FC_INNER for more
908 extern int FC_NO_TAILCALL;
909 #define FC_INNER_RETURN(type, expr)                                                        \
910     type __retVal = expr;                                                                  \
911     while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */            \
912     return(__retVal);
913  
914 #define FC_INNER_RETURN_VOID(stmt)                                                         \
915     stmt;                                                                                  \
916     while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */            \
917     return;
918
919 //==============================================================================================
920 // FIMPLn: A set of macros for generating the proto for the actual
921 // implementation (use FDECLN for header protos.)
922 //
923 // The hidden "__me" variable lets us recover the original MethodDesc*
924 // so any thrown exceptions will have the correct stack trace. FCThrow()
925 // passes this along to __FCThrowInternal(). 
926 //==============================================================================================
927
928 #define GetEEFuncEntryPointMacro(func)  ((LPVOID)(func))
929
930 #define FCIMPL_PROLOG(funcname)  \
931     LPVOID __me; \
932     __me = GetEEFuncEntryPointMacro(funcname); \
933     FC_COMMON_PROLOG(__me, FCallAssert)
934
935
936 #if defined(_DEBUG) && !defined(CROSSGEN_COMPILE)
937
938 // Build the list of all fcalls signatures. It is used in binder.cpp to verify 
939 // compatibility of managed and unmanaged fcall signatures. The check is currently done 
940 // for x86 only.
941
942 struct FCSigCheck {
943 public:
944     FCSigCheck(void* fnc, char* sig)
945     {
946         LIMITED_METHOD_CONTRACT;
947         func = fnc;
948         signature = sig;
949         next = g_pFCSigCheck;
950         g_pFCSigCheck = this;
951     }
952
953     FCSigCheck* next;
954     void* func;
955     char* signature;
956
957     static FCSigCheck* g_pFCSigCheck;
958 };
959
960 #define FCSIGCHECK(funcname, signature) \
961     static FCSigCheck UNIQUE_LABEL(FCSigCheck)(GetEEFuncEntryPointMacro(funcname), signature);
962
963 #else
964
965 #define FCSIGCHECK(funcname, signature)
966
967 #endif
968
969
970 #ifdef _TARGET_X86_
971
972 #if defined(__GNUC__)
973
974 #define FCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { FCIMPL_PROLOG(funcname)
975 #define FCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1) { FCIMPL_PROLOG(funcname)
976 #define FCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a1) { FCIMPL_PROLOG(funcname)
977 #define FCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1) { FCIMPL_PROLOG(funcname)
978 #define FCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a2, a1) { FCIMPL_PROLOG(funcname)
979 #define FCIMPL2_VI(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a2, a1) { FCIMPL_PROLOG(funcname)
980 #define FCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a2) { FCIMPL_PROLOG(funcname)
981 #define FCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3) { FCIMPL_PROLOG(funcname)
982 #define FCIMPL3_IIV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3) { FCIMPL_PROLOG(funcname)
983 #define FCIMPL3_VII(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a2, a1) { FCIMPL_PROLOG(funcname)
984 #define FCIMPL3_IVV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a3, a2) { FCIMPL_PROLOG(funcname)
985 #define FCIMPL3_IVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a1, a2) { FCIMPL_PROLOG(funcname)
986 #define FCIMPL3_VVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a3, a2, a1) {  FCIMPL_PROLOG(funcname)
987 #define FCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a4, a3) { FCIMPL_PROLOG(funcname)
988 #define FCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a5, a4, a3) { FCIMPL_PROLOG(funcname)
989 #define FCIMPL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
990 #define FCIMPL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
991 #define FCIMPL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
992 #define FCIMPL9(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
993 #define FCIMPL10(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
994 #define FCIMPL11(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
995 #define FCIMPL12(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
996 #define FCIMPL13(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
997 #define FCIMPL14(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a14, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
998
999 #define FCIMPL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a3, a1, a5, a4, a2) { FCIMPL_PROLOG(funcname)
1000 #define FCIMPL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a3, a2, a5, a4, a1) { FCIMPL_PROLOG(funcname)
1001
1002 #else // __GNUC__
1003
1004 #define FCIMPL0(rettype, funcname) FCSIGCHECK(funcname, #rettype) \
1005     rettype F_CALL_CONV funcname() { FCIMPL_PROLOG(funcname)
1006 #define FCIMPL1(rettype, funcname, a1) FCSIGCHECK(funcname, #rettype "," #a1) \
1007     rettype F_CALL_CONV funcname(a1) { FCIMPL_PROLOG(funcname)
1008 #define FCIMPL1_V(rettype, funcname, a1) FCSIGCHECK(funcname, #rettype "," "V" #a1) \
1009     rettype F_CALL_CONV funcname(a1) { FCIMPL_PROLOG(funcname)
1010 #define FCIMPL2(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2) \
1011     rettype F_CALL_CONV funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1012 #define FCIMPL2VA(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," "...") \
1013     rettype F_CALL_VA_CONV funcname(a1, a2, ...) { FCIMPL_PROLOG(funcname)
1014 #define FCIMPL2_VV(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," "V" #a2) \
1015     rettype F_CALL_CONV funcname(a2, a1) { FCIMPL_PROLOG(funcname)
1016 #define FCIMPL2_VI(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," #a2) \
1017     rettype F_CALL_CONV funcname(a2, a1) { FCIMPL_PROLOG(funcname)
1018 #define FCIMPL2_IV(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2) \
1019     rettype F_CALL_CONV funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1020 #define FCIMPL3(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3) \
1021     rettype F_CALL_CONV funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1022 #define FCIMPL3_IIV(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," "V" #a3) \
1023     rettype F_CALL_CONV funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1024 #define FCIMPL3_VII(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," #a2 "," #a3) \
1025     rettype F_CALL_CONV funcname(a2, a3, a1) { FCIMPL_PROLOG(funcname) 
1026 #define FCIMPL3_IVV(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2 "," "V" #a3) \
1027     rettype F_CALL_CONV funcname(a1, a3, a2) { FCIMPL_PROLOG(funcname)
1028 #define FCIMPL3_IVI(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2 "," #a3) \
1029     rettype F_CALL_CONV funcname(a1, a3, a2) { FCIMPL_PROLOG(funcname)
1030 #define FCIMPL3_VVI(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," "V" #a2 "," #a3) \
1031     rettype F_CALL_CONV funcname(a2, a1, a3) { FCIMPL_PROLOG(funcname)
1032 #define FCIMPL4(rettype, funcname, a1, a2, a3, a4) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4) \
1033     rettype F_CALL_CONV funcname(a1, a2, a4, a3) { FCIMPL_PROLOG(funcname)
1034 #define FCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5) \
1035     rettype F_CALL_CONV funcname(a1, a2, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1036 #define FCIMPL6(rettype, funcname, a1, a2, a3, a4, a5, a6) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6) \
1037     rettype F_CALL_CONV funcname(a1, a2, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1038 #define FCIMPL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7) \
1039     rettype F_CALL_CONV funcname(a1, a2, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1040 #define FCIMPL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8) \
1041     rettype F_CALL_CONV funcname(a1, a2, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1042 #define FCIMPL9(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8 "," #a9) \
1043     rettype F_CALL_CONV funcname(a1, a2, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1044 #define FCIMPL10(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8 "," #a9 "," #a10) \
1045     rettype F_CALL_CONV funcname(a1, a2, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1046 #define FCIMPL11(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8 "," #a9 "," #a10 "," #a11) \
1047     rettype F_CALL_CONV funcname(a1, a2, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1048 #define FCIMPL12(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8 "," #a9 "," #a10 "," #a11 "," #a12) \
1049     rettype F_CALL_CONV funcname(a1, a2, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1050 #define FCIMPL13(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8 "," #a9 "," #a10 "," #a11 "," #a12 "," #a13) \
1051     rettype F_CALL_CONV funcname(a1, a2, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1052 #define FCIMPL14(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8 "," #a9 "," #a10 "," #a11 "," #a12 "," #a13 "," #a14) \
1053     rettype F_CALL_CONV funcname(a1, a2, a14, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1054
1055 #define FCIMPL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2 "," #a3 "," #a4 "," #a5) \
1056     rettype F_CALL_CONV funcname(a1, a3, a5, a4, a2) { FCIMPL_PROLOG(funcname)
1057 #define FCIMPL5_VII(rettype, funcname, a1, a2, a3, a4, a5) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," #a2 "," #a3 "," #a4 "," #a5) \
1058     rettype F_CALL_CONV funcname(a2, a3, a5, a4, a1) { FCIMPL_PROLOG(funcname)
1059
1060 #endif // __GNUC__
1061
1062 #else // !_TARGET_X86_ 
1063 //
1064 // non-x86 platforms don't have messed-up calling convention swizzling 
1065 //
1066
1067 #define FCIMPL0(rettype, funcname) rettype funcname() { FCIMPL_PROLOG(funcname)
1068 #define FCIMPL1(rettype, funcname, a1) rettype funcname(a1) {  FCIMPL_PROLOG(funcname)
1069 #define FCIMPL1_V(rettype, funcname, a1) rettype funcname(a1) {  FCIMPL_PROLOG(funcname)
1070 #define FCIMPL2(rettype, funcname, a1, a2) rettype funcname(a1, a2) {  FCIMPL_PROLOG(funcname)
1071 #define FCIMPL2VA(rettype, funcname, a1, a2) rettype funcname(a1, a2, ...) {  FCIMPL_PROLOG(funcname)
1072 #define FCIMPL2_VV(rettype, funcname, a1, a2) rettype funcname(a1, a2) {  FCIMPL_PROLOG(funcname)
1073 #define FCIMPL2_VI(rettype, funcname, a1, a2) rettype funcname(a1, a2) {  FCIMPL_PROLOG(funcname)
1074 #define FCIMPL2_IV(rettype, funcname, a1, a2) rettype funcname(a1, a2) {  FCIMPL_PROLOG(funcname)
1075 #define FCIMPL3(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) {  FCIMPL_PROLOG(funcname)
1076 #define FCIMPL3_IIV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) {  FCIMPL_PROLOG(funcname)
1077 #define FCIMPL3_IVV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) {  FCIMPL_PROLOG(funcname)
1078 #define FCIMPL3_VII(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) {  FCIMPL_PROLOG(funcname)
1079 #define FCIMPL3_IVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) {  FCIMPL_PROLOG(funcname)
1080 #define FCIMPL3_VVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) {  FCIMPL_PROLOG(funcname)
1081 #define FCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype funcname(a1, a2, a3, a4) {  FCIMPL_PROLOG(funcname)
1082 #define FCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5) {  FCIMPL_PROLOG(funcname)
1083 #define FCIMPL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype funcname(a1, a2, a3, a4, a5, a6) {  FCIMPL_PROLOG(funcname)
1084 #define FCIMPL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype funcname(a1, a2, a3, a4, a5, a6, a7) {  FCIMPL_PROLOG(funcname)
1085 #define FCIMPL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8) {  FCIMPL_PROLOG(funcname)
1086 #define FCIMPL9(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9) {  FCIMPL_PROLOG(funcname)
1087 #define FCIMPL10(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) {  FCIMPL_PROLOG(funcname)
1088 #define FCIMPL11(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) {  FCIMPL_PROLOG(funcname)
1089 #define FCIMPL12(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) {  FCIMPL_PROLOG(funcname)
1090 #define FCIMPL13(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) {  FCIMPL_PROLOG(funcname)
1091 #define FCIMPL14(rettype,funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) {  FCIMPL_PROLOG(funcname)
1092
1093 #define FCIMPL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5) { FCIMPL_PROLOG(funcname)
1094 #define FCIMPL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5) { FCIMPL_PROLOG(funcname)
1095
1096 #endif
1097
1098 //==============================================================================================
1099 // Use this to terminte an FCIMPLEND.
1100 //==============================================================================================
1101
1102 #define FCIMPL_EPILOG()   FCALL_TRANSITION_END()
1103
1104 #define FCIMPLEND   FCIMPL_EPILOG(); }
1105
1106 #define HCIMPL_PROLOG(funcname) LPVOID __me; __me = 0; FC_COMMON_PROLOG(funcname, HCallAssert)
1107
1108     // HCIMPL macros are just like their FCIMPL counterparts, however
1109     // they do not remember the function they come from. Thus they will not
1110     // show up in a stack trace.  This is what you want for JIT helpers and the like
1111
1112 #ifdef _TARGET_X86_
1113
1114 #if defined(__GNUC__)
1115
1116 #define HCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { HCIMPL_PROLOG(funcname)
1117 #define HCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1) { HCIMPL_PROLOG(funcname)
1118 #define HCIMPL1_RAW(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1) {
1119 #define HCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a1) { HCIMPL_PROLOG(funcname)
1120 #define HCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1) { HCIMPL_PROLOG(funcname)
1121 #define HCIMPL2_RAW(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1) {
1122 #define HCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a2, a1) { HCIMPL_PROLOG(funcname)
1123 #define HCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a2) { HCIMPL_PROLOG(funcname)
1124 #define HCIMPL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...) { HCIMPL_PROLOG(funcname)
1125 #define HCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3) { HCIMPL_PROLOG(funcname)
1126 #define HCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a4, a3) { HCIMPL_PROLOG(funcname)
1127 #define HCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a5, a4, a3) { HCIMPL_PROLOG(funcname)
1128
1129 #define HCCALL1(funcname, a1)           funcname(0, 0, a1)
1130 #define HCCALL1_V(funcname, a1)         funcname(0, 0, 0, a1)
1131 #define HCCALL2(funcname, a1, a2)       funcname(0, a2, a1)
1132 #define HCCALL3(funcname, a1, a2, a3)   funcname(0, a2, a1, a3)
1133 #define HCCALL4(funcname, a1, a2, a3, a4)       funcname(0, a2, a1, a4, a3)
1134 #define HCCALL5(funcname, a1, a2, a3, a4, a5)   funcname(0, a2, a1, a5, a4, a3)
1135 #define HCCALL1_PTR(rettype, funcptr, a1)        rettype (F_CALL_CONV * funcptr)(int /* EAX */, int /* EDX */, a1)
1136 #define HCCALL2_PTR(rettype, funcptr, a1, a2)    rettype (F_CALL_CONV * funcptr)(int /* EAX */, a2, a1)
1137 #else
1138
1139 #define HCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { HCIMPL_PROLOG(funcname) 
1140 #define HCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1141 #define HCIMPL1_RAW(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) {
1142 #define HCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1143 #define HCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1144 #define HCIMPL2_RAW(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) {
1145 #define HCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a2, a1) { HCIMPL_PROLOG(funcname)
1146 #define HCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1147 #define HCIMPL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...) { HCIMPL_PROLOG(funcname)
1148 #define HCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3) { HCIMPL_PROLOG(funcname)
1149 #define HCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(a1, a2, a4, a3) { HCIMPL_PROLOG(funcname)
1150 #define HCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a2, a5, a4, a3) { HCIMPL_PROLOG(funcname)
1151
1152 #define HCCALL1(funcname, a1)           funcname(a1)
1153 #define HCCALL1_V(funcname, a1)         funcname(a1)
1154 #define HCCALL2(funcname, a1, a2)       funcname(a1, a2)
1155 #define HCCALL3(funcname, a1, a2, a3)   funcname(a1, a2, a3)
1156 #define HCCALL4(funcname, a1, a2, a3, a4)       funcname(a1, a2, a4, a3)
1157 #define HCCALL5(funcname, a1, a2, a3, a4, a5)   funcname(a1, a2, a5, a4, a3)
1158 #define HCCALL1_PTR(rettype, funcptr, a1)        rettype (F_CALL_CONV * funcptr)(a1)
1159 #define HCCALL2_PTR(rettype, funcptr, a1, a2)    rettype (F_CALL_CONV * funcptr)(a1, a2)
1160
1161 #endif
1162
1163 #else // !_TARGET_X86_ 
1164 //
1165 // non-x86 platforms don't have messed-up calling convention swizzling 
1166 //
1167
1168 #define HCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { HCIMPL_PROLOG(funcname) 
1169 #define HCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1170 #define HCIMPL1_RAW(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) {
1171 #define HCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1172 #define HCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1173 #define HCIMPL2_RAW(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) {
1174 #define HCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1175 #define HCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1176 #define HCIMPL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...) { HCIMPL_PROLOG(funcname)
1177 #define HCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3) { HCIMPL_PROLOG(funcname)
1178 #define HCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(a1, a2, a3, a4) { HCIMPL_PROLOG(funcname)
1179 #define HCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a2, a3, a4, a5) { HCIMPL_PROLOG(funcname)
1180
1181 #define HCCALL1(funcname, a1)           funcname(a1)
1182 #define HCCALL1_V(funcname, a1)         funcname(a1)
1183 #define HCCALL2(funcname, a1, a2)       funcname(a1, a2)
1184 #define HCCALL3(funcname, a1, a2, a3)   funcname(a1, a2, a3)
1185 #define HCCALL4(funcname, a1, a2, a3, a4)       funcname(a1, a2, a3, a4)
1186 #define HCCALL5(funcname, a1, a2, a3, a4, a5)   funcname(a1, a2, a3, a4, a5)
1187 #define HCCALL1_PTR(rettype, funcptr, a1)        rettype (F_CALL_CONV * funcptr)(a1)
1188 #define HCCALL2_PTR(rettype, funcptr, a1, a2)    rettype (F_CALL_CONV * funcptr)(a1, a2)
1189
1190 #endif
1191
1192 #define HCIMPLEND_RAW   }
1193 #define HCIMPLEND       FCALL_TRANSITION_END(); }
1194
1195
1196 //==============================================================================================
1197 // Throws an exception from an FCall. See rexcep.h for a list of valid
1198 // exception codes.
1199 //==============================================================================================
1200 #define FCThrow(reKind) FCThrowEx(reKind, 0, 0, 0, 0)
1201
1202 //==============================================================================================
1203 // This version lets you attach a message with inserts (similar to
1204 // COMPlusThrow()).
1205 //==============================================================================================
1206 #define FCThrowEx(reKind, resID, arg1, arg2, arg3)              \
1207     {                                                           \
1208         while (NULL ==                                          \
1209             __FCThrow(__me, reKind, resID, arg1, arg2, arg3)) {}; \
1210         return 0;                                               \
1211     }
1212
1213 //==============================================================================================
1214 // Like FCThrow but can be used for a VOID-returning FCall. The only
1215 // difference is in the "return" statement.
1216 //==============================================================================================
1217 #define FCThrowVoid(reKind) FCThrowExVoid(reKind, 0, 0, 0, 0)
1218
1219 //==============================================================================================
1220 // This version lets you attach a message with inserts (similar to
1221 // COMPlusThrow()).
1222 //==============================================================================================
1223 #define FCThrowExVoid(reKind, resID, arg1, arg2, arg3)          \
1224     {                                                           \
1225         while (NULL ==                                          \
1226             __FCThrow(__me, reKind, resID, arg1, arg2, arg3)) {}; \
1227         return;                                                 \
1228     }
1229
1230 // Use FCThrowRes to throw an exception with a localized error message from the
1231 // ResourceManager in managed code.
1232 #define FCThrowRes(reKind, resourceName) FCThrowArgumentEx(reKind, NULL, resourceName)
1233 #define FCThrowArgumentNull(argName) FCThrowArgumentEx(kArgumentNullException, argName, NULL)
1234 #define FCThrowArgumentOutOfRange(argName, message) FCThrowArgumentEx(kArgumentOutOfRangeException, argName, message)
1235 #define FCThrowArgument(argName, message) FCThrowArgumentEx(kArgumentException, argName, message)
1236
1237 #define FCThrowArgumentEx(reKind, argName, resourceName)        \
1238     {                                                           \
1239         while (NULL ==                                                  \
1240             __FCThrowArgument(__me, reKind, argName, resourceName)) {}; \
1241         return 0;                                               \
1242     }
1243
1244 // Use FCThrowRes to throw an exception with a localized error message from the
1245 // ResourceManager in managed code.
1246 #define FCThrowResVoid(reKind, resourceName) FCThrowArgumentVoidEx(reKind, NULL, resourceName)
1247 #define FCThrowArgumentNullVoid(argName) FCThrowArgumentVoidEx(kArgumentNullException, argName, NULL)
1248 #define FCThrowArgumentOutOfRangeVoid(argName, message) FCThrowArgumentVoidEx(kArgumentOutOfRangeException, argName, message)
1249 #define FCThrowArgumentVoid(argName, message) FCThrowArgumentVoidEx(kArgumentException, argName, message)
1250
1251 #define FCThrowArgumentVoidEx(reKind, argName, resourceName)    \
1252     {                                                           \
1253         while (NULL ==                                                  \
1254             __FCThrowArgument(__me, reKind, argName, resourceName)) {};  \
1255         return;                                                 \
1256     }
1257
1258
1259
1260 // The x86 JIT calling convention expects returned small types (e.g. bool) to be
1261 // widened on return. The C/C++ calling convention does not guarantee returned
1262 // small types to be widened. The small types has to be artifically widened on return
1263 // to fit x86 JIT calling convention. Thus fcalls returning small types has to
1264 // use the FC_XXX_RET types to force C/C++ compiler to do the widening.
1265 //
1266 // The most common small return type of FCALLs is bool. The widening of bool is
1267 // especially tricky since the value has to be also normalized. FC_BOOL_RET and 
1268 // FC_RETURN_BOOL macros are provided to make it fool-proof. FCALLs returning bool
1269 // should be implemented using following pattern:
1270
1271 // FCIMPL0(FC_BOOL_RET, Foo)    // the return type should be FC_BOOL_RET
1272 //      BOOL ret;
1273 //
1274 //      FC_RETURN_BOOL(ret);    // return statements should be FC_RETURN_BOOL
1275 // FCIMPLEND
1276
1277 // This rules are verified in binder.cpp if COMPlus_ConsistencyCheck is set.
1278
1279 #ifdef _PREFAST_
1280
1281 // Use prefast build to ensure that functions returning FC_BOOL_RET 
1282 // are using FC_RETURN_BOOL to return it. Missing FC_RETURN_BOOL will
1283 // result into type mismatch error in prefast builds. This will also
1284 // catch misuses of FC_BOOL_RET for other places (e.g. in FCALL parameters).
1285
1286 typedef LPVOID FC_BOOL_RET;
1287 #define FC_RETURN_BOOL(x) do { return (LPVOID)!!(x); } while(0)
1288
1289 #else
1290
1291 #if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
1292 // The return value is artifically widened on x86 and amd64
1293 typedef INT32 FC_BOOL_RET;
1294 #else
1295 typedef CLR_BOOL FC_BOOL_RET;
1296 #endif
1297
1298 #define FC_RETURN_BOOL(x)   do { return !!(x); } while(0)
1299
1300 #endif
1301
1302
1303 #if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
1304 // The return value is artifically widened on x86 and amd64
1305 typedef UINT32 FC_CHAR_RET;
1306 typedef INT32 FC_INT8_RET;
1307 typedef UINT32 FC_UINT8_RET;
1308 typedef INT32 FC_INT16_RET;
1309 typedef UINT32 FC_UINT16_RET;
1310 #else
1311 typedef CLR_CHAR FC_CHAR_RET;
1312 typedef INT8 FC_INT8_RET;
1313 typedef UINT8 FC_UINT8_RET;
1314 typedef INT16 FC_INT16_RET;
1315 typedef UINT16 FC_UINT16_RET;
1316 #endif
1317
1318
1319 // FC_TypedByRef should be used for TypedReferences in FCall signatures
1320 #if defined(UNIX_AMD64_ABI) && !defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
1321 // Explicitly pass the TypedReferences by reference 
1322 #define FC_TypedByRef   TypedByRef&
1323 #define FC_DECIMAL      DECIMAL&
1324 #else
1325 #define FC_TypedByRef   TypedByRef
1326 #define FC_DECIMAL      DECIMAL
1327 #endif
1328
1329
1330 // The fcall entrypoints has to be at unique addresses. Use this helper macro to make 
1331 // the code of the fcalls unique if you get assert in ecall.cpp that mentions it.
1332 // The parameter of the FCUnique macro is an arbitrary 32-bit random non-zero number.
1333 #define FCUnique(unique) { Volatile<int> u = (unique); while (u.LoadWithoutBarrier() == 0) { }; }
1334
1335
1336
1337
1338 // FCALL contracts come in two forms:
1339 //
1340 // Short form that should be used if the FCALL contract does not have any extras like preconditions, failure injection. Example:
1341 //
1342 // FCIMPL0(void, foo)
1343 // {
1344 //     FCALL_CONTRACT;
1345 //     ...
1346 //
1347 // Long form that should be used otherwise. Example:
1348 //
1349 // FCIMPL1(void, foo, void *p)
1350 // {
1351 //     CONTRACTL {
1352 //         FCALL_CHECK;
1353 //         PRECONDITION(CheckPointer(p));
1354 //     } CONTRACTL_END;
1355 //     ...
1356
1357
1358 //
1359 // FCALL_CHECK defines the actual contract conditions required for FCALLs
1360 //
1361 #define FCALL_CHECK \
1362         THROWS; \
1363         DISABLED(GC_TRIGGERS); /* FCALLS with HELPER frames have issues with GC_TRIGGERS */ \
1364         MODE_COOPERATIVE; \
1365         SO_TOLERANT
1366
1367 //
1368 // FCALL_CONTRACT should be the following shortcut:
1369 //
1370 // #define FCALL_CONTRACT   CONTRACTL { FCALL_CHECK; } CONTRACTL_END;
1371 //
1372 // Since there is very little value in having runtime contracts in FCalls, FCALL_CONTRACT is defined as static contract only for performance reasons.
1373 //
1374 #define FCALL_CONTRACT \
1375     STATIC_CONTRACT_SO_TOLERANT; \
1376     STATIC_CONTRACT_THROWS; \
1377     /* FCALLS are a special case contract wise, they are "NOTRIGGER, unless you setup a frame" */ \
1378     STATIC_CONTRACT_GC_NOTRIGGER; \
1379     STATIC_CONTRACT_MODE_COOPERATIVE
1380
1381 #endif //__FCall_h__