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