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.
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.
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.
22 // In general you set up a frame by using the following macros
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*()
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
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.
35 // 2) You may not perform a 'return' within this scope..
37 // Compile time errors occur if you try to violate either of these rules.
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
45 // HELPER_METHOD_FRAME_BEGIN_RET_2(arg1, arg2)
47 // will protect the GC variables arg1, and arg2 as well as erecting the frame.
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.
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()
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).
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.
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.
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
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>
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
89 // HELPER_METHOD_FRAME_BEGIN() // Use if FCALL does not return a value
90 // COMPlusThrow(execpt);
91 // HELPER_METHOD_FRAME_END()
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).
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
104 //------------------------------------------------------------------------
105 // A very simple example:
107 // FCIMPL2(INT32, Div, INT32 x, INT32 y)
110 // FCThrow(kDivideByZeroException);
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;
123 // FCIMPL(.., .., Object* pObject0)
124 // OBJECTREF pObject = ObjectToOBJECTREF(pObject0);
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.
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".
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.
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.
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:
154 // FCIMPL4(Object*, COMNlsInfo::nativeChangeCaseString, LocaleIDObject* localeUNSAFE,
155 // INT_PTR pNativeTextInfo, StringObject* pStringUNSAFE, CLR_BOOL bIsToUpper)
157 // [ignoring CONTRACT for now]
160 // STRINGREF pResult;
161 // STRINGREF pString;
162 // LOCALEIDREF pLocale;
164 // gc.pResult = NULL;
165 // gc.pString = ObjectToSTRINGREF(pStringUNSAFE);
166 // gc.pLocale = (LOCALEIDREF)ObjectToOBJECTREF(localeUNSAFE);
168 // HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc)
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.
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.
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:
184 // - The location of the FCIMPL's return address (since that's
185 // where the frame will be based.)
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.
192 // To figure out which, FCThrow() generates the code:
194 // while (NULL == __FCThrow(__me, ...)) {};
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
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.
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
221 #include "runtimeexceptionkind.h"
222 #include "debugreturn.h"
223 #include "stackprobe.h"
225 //==============================================================================================
226 // These macros defeat compiler optimizations that might mix nonvolatile
227 // register loads and stores with other code in the function body. This
228 // creates problems for the frame setup code, which assumes that any
229 // nonvolatiles that are saved at the point of the frame setup will be
230 // re-loaded when the frame is popped.
232 // Currently this is only known to be an issue on AMD64. It's uncertain
233 // whether it is an issue on x86.
234 //==============================================================================================
236 #if defined(_TARGET_AMD64_) && !defined(FEATURE_PAL)
239 // On AMD64 this is accomplished by including a setjmp anywhere in a function.
240 // Doesn't matter whether it is reachable or not, and in fact in optimized
241 // builds the setjmp is removed altogether.
246 // Use of setjmp is temporary, we will eventually have compiler intrinsics to
247 // disable the optimizations. Besides, we don't actually execute setjmp in
248 // these macros (or anywhere else in the VM on AMD64).
250 #pragma warning(disable:4611) // interaction between '_setjmp' and C++ object destruction is non-portable
254 // Linked list of unmanaged methods preceeding a HelperMethodFrame push. This
255 // is linked onto the current Thread. Each list entry is stack-allocated so it
256 // can be associated with an unmanaged frame. Each unmanaged frame needs to be
257 // associated with at least one list entry.
259 struct HelperMethodFrameCallerList
261 HelperMethodFrameCallerList *pCaller;
266 // Resets the Thread state at a new managed -> fcall transition.
268 class FCallTransitionState
272 FCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
273 ~FCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
278 HelperMethodFrameCallerList *m_pPreviousHelperMethodFrameCallerList;
283 // Pushes/pops state for each caller.
285 class PermitHelperMethodFrameState
289 PermitHelperMethodFrameState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
290 ~PermitHelperMethodFrameState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
292 static VOID CheckHelperMethodFramePermitted () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
297 HelperMethodFrameCallerList m_ListEntry;
302 // Resets the Thread state after the HelperMethodFrame is pushed. At this
303 // point, the HelperMethodFrame is capable of unwinding to the managed code,
304 // so we can reset the Thread state for any nested fcalls.
306 class CompletedFCallTransitionState
310 CompletedFCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
311 ~CompletedFCallTransitionState () NOT_DEBUG({ LIMITED_METHOD_CONTRACT; });
316 HelperMethodFrameCallerList *m_pLastHelperMethodFrameCallerList;
320 #define PERMIT_HELPER_METHOD_FRAME_BEGIN() \
323 PermitHelperMethodFrameState ___PermitHelperMethodFrameState;
325 #define PERMIT_HELPER_METHOD_FRAME_END() \
334 #define FCALL_TRANSITION_BEGIN() \
335 FCallTransitionState ___FCallTransitionState; \
336 PERMIT_HELPER_METHOD_FRAME_BEGIN();
338 #define FCALL_TRANSITION_END() \
339 PERMIT_HELPER_METHOD_FRAME_END();
341 #define CHECK_HELPER_METHOD_FRAME_PERMITTED() \
342 PermitHelperMethodFrameState::CheckHelperMethodFramePermitted(); \
343 CompletedFCallTransitionState ___CompletedFCallTransitionState;
345 #else // unsupported processor
347 #define PERMIT_HELPER_METHOD_FRAME_BEGIN()
348 #define PERMIT_HELPER_METHOD_FRAME_END()
349 #define FCALL_TRANSITION_BEGIN()
350 #define FCALL_TRANSITION_END()
351 #define CHECK_HELPER_METHOD_FRAME_PERMITTED()
353 #endif // unsupported processor
355 //==============================================================================================
356 // This is where FCThrow ultimately ends up. Never call this directly.
357 // Use the FCThrow() macros. __FCThrowArgument is the helper to throw ArgumentExceptions
358 // with a resource taken from the managed resource manager.
359 //==============================================================================================
360 LPVOID __FCThrow(LPVOID me, enum RuntimeExceptionKind reKind, UINT resID, LPCWSTR arg1, LPCWSTR arg2, LPCWSTR arg3);
361 LPVOID __FCThrowArgument(LPVOID me, enum RuntimeExceptionKind reKind, LPCWSTR argumentName, LPCWSTR resourceName);
363 //==============================================================================================
364 // FDECLn: A set of macros for generating header declarations for FC targets.
365 // Use FIMPLn for the actual body.
366 //==============================================================================================
368 // Note: on the x86, these defs reverse all but the first two arguments
369 // (IL stack calling convention is reversed from __fastcall.)
372 // Calling convention for varargs
373 #define F_CALL_VA_CONV __cdecl
378 // Choose the appropriate calling convention for FCALL helpers on the basis of the JIT calling convention
380 #define F_CALL_CONV __attribute__((stdcall, regparm(3)))
382 #define F_CALL_CONV __fastcall
385 #if defined(__GNUC__)
387 // GCC fastcall convention is different from MSVC fastcall convention. GCC can use up to 3 registers to
388 // store parameters. The registers used are EAX, EDX, ECX. Dummy parameters and reordering of the
389 // actual parameters in the FCALL signature is used to make the calling convention to look like in MSVC.
391 #define FCDECL0(rettype, funcname) rettype F_CALL_CONV funcname()
392 #define FCDECL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1)
393 #define FCDECL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a1)
394 #define FCDECL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1)
395 #define FCDECL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...)
396 #define FCDECL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a2, a1)
397 #define FCDECL2_VI(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a2, a1)
398 #define FCDECL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a2)
399 #define FCDECL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3)
400 #define FCDECL3_IIV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3)
401 #define FCDECL3_VII(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a2, a1)
402 #define FCDECL3_IVV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a3, a2)
403 #define FCDECL3_IVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a1, a2)
404 #define FCDECL3_VVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a3, a2, a1)
405 #define FCDECL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a4, a3)
406 #define FCDECL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a5, a4, a3)
407 #define FCDECL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a6, a5, a4, a3)
408 #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)
409 #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)
410 #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)
411 #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)
412 #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)
413 #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)
414 #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)
415 #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)
417 #define FCDECL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a3, a1, a5, a4, a2)
418 #define FCDECL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a3, a2, a5, a4, a1)
422 #define FCDECL0(rettype, funcname) rettype F_CALL_CONV funcname()
423 #define FCDECL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1)
424 #define FCDECL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1)
425 #define FCDECL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2)
426 #define FCDECL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...)
427 #define FCDECL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a2, a1)
428 #define FCDECL2_VI(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a2, a1)
429 #define FCDECL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2)
430 #define FCDECL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3)
431 #define FCDECL3_IIV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3)
432 #define FCDECL3_VII(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a2, a3, a1)
433 #define FCDECL3_IVV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a3, a2)
434 #define FCDECL3_IVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a3, a2)
435 #define FCDECL3_VVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a2, a1, a3)
436 #define FCDECL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(a1, a2, a4, a3)
437 #define FCDECL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a2, a5, a4, a3)
438 #define FCDECL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype F_CALL_CONV funcname(a1, a2, a6, a5, a4, a3)
439 #define FCDECL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype F_CALL_CONV funcname(a1, a2, a7, a6, a5, a4, a3)
440 #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)
441 #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)
442 #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)
443 #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)
444 #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)
445 #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)
446 #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)
448 #define FCDECL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a3, a5, a4, a2)
449 #define FCDECL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a2, a3, a5, a4, a1)
455 // don't use something like this... directly calling an FCALL from within the runtime breaks stackwalking because
456 // the FCALL reverse mapping only gets established in ECall::GetFCallImpl and that codepath is circumvented by
457 // directly calling and FCALL
458 // See below for usage of FC_CALL_INNER (used in SecurityStackWalk::Check presently)
460 #define FCCALL0(funcname) funcname()
461 #define FCCALL1(funcname, a1) funcname(a1)
462 #define FCCALL2(funcname, a1, a2) funcname(a1, a2)
463 #define FCCALL3(funcname, a1, a2, a3) funcname(a1, a2, a3)
464 #define FCCALL4(funcname, a1, a2, a3, a4) funcname(a1, a2, a4, a3)
465 #define FCCALL5(funcname, a1, a2, a3, a4, a5) funcname(a1, a2, a5, a4, a3)
466 #define FCCALL6(funcname, a1, a2, a3, a4, a5, a6) funcname(a1, a2, a6, a5, a4, a3)
467 #define FCCALL7(funcname, a1, a2, a3, a4, a5, a6, a7) funcname(a1, a2, a7, a6, a5, a4, a3)
468 #define FCCALL8(funcname, a1, a2, a3, a4, a5, a6, a7, a8) funcname(a1, a2, a8, a7, a6, a5, a4, a3)
469 #define FCCALL9(funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) funcname(a1, a2, a9, a8, a7, a6, a5, a4, a3)
470 #define FCCALL10(funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) funcname(a1, a2, a10, a9, a8, a7, a6, a5, a4, a3)
471 #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)
472 #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)
475 #else // !_TARGET_X86
479 #define FCDECL0(rettype, funcname) rettype funcname()
480 #define FCDECL1(rettype, funcname, a1) rettype funcname(a1)
481 #define FCDECL1_V(rettype, funcname, a1) rettype funcname(a1)
482 #define FCDECL2(rettype, funcname, a1, a2) rettype funcname(a1, a2)
483 #define FCDECL2VA(rettype, funcname, a1, a2) rettype funcname(a1, a2, ...)
484 #define FCDECL2_VV(rettype, funcname, a1, a2) rettype funcname(a1, a2)
485 #define FCDECL2_VI(rettype, funcname, a1, a2) rettype funcname(a1, a2)
486 #define FCDECL2_IV(rettype, funcname, a1, a2) rettype funcname(a1, a2)
487 #define FCDECL3(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
488 #define FCDECL3_IIV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
489 #define FCDECL3_VII(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
490 #define FCDECL3_IVV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
491 #define FCDECL3_IVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
492 #define FCDECL3_VVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3)
493 #define FCDECL4(rettype, funcname, a1, a2, a3, a4) rettype funcname(a1, a2, a3, a4)
494 #define FCDECL5(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5)
495 #define FCDECL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype funcname(a1, a2, a3, a4, a5, a6)
496 #define FCDECL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype funcname(a1, a2, a3, a4, a5, a6, a7)
497 #define FCDECL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8)
498 #define FCDECL9(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8, a9) rettype funcname(a1, a2, a3, a4, a5, a6, a7, a8, a9)
499 #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)
500 #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)
501 #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)
502 #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)
503 #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)
505 #define FCDECL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5)
506 #define FCDECL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5)
508 #endif // _TARGET_X86_
510 #define HELPER_FRAME_DECL(x) FrameWithCookie<HelperMethodFrame_##x##OBJ> __helperframe
512 // use the capture state machinery if the architecture has one
514 // For a normal build we create a loop (see explaination on RestoreState below)
515 // We don't want a loop here for PREFAST since that causes
516 // warning 263: Using _alloca in a loop
517 // And we can't use DEBUG_OK_TO_RETURN for PREFAST because the PREFAST version
518 // requires that you already be in a DEBUG_ASSURE_NO_RETURN_BEGIN scope
520 #define HelperMethodFrame_0OBJ HelperMethodFrame
521 #define HELPER_FRAME_ARGS(attribs) __me, attribs
522 #define FORLAZYMACHSTATE(x) x
524 #if defined(_PREFAST_)
525 #define FORLAZYMACHSTATE_BEGINLOOP(x) x
526 #define FORLAZYMACHSTATE_ENDLOOP(x)
527 #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_BEGIN
528 #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_END
530 #define FORLAZYMACHSTATE_BEGINLOOP(x) x do
531 #define FORLAZYMACHSTATE_ENDLOOP(x) while(x)
532 #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_BEGIN DEBUG_OK_TO_RETURN_BEGIN(LAZYMACHSTATE)
533 #define FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_END DEBUG_OK_TO_RETURN_END(LAZYMACHSTATE)
536 // BEGIN: before gcpoll
537 //FCallGCCanTriggerNoDtor __fcallGcCanTrigger;
538 //__fcallGcCanTrigger.Enter();
541 //__fcallGcCanTrigger.Leave(__FUNCTION__, __FILE__, __LINE__);
543 // We have to put DEBUG_OK_TO_RETURN_BEGIN around the FORLAZYMACHSTATE
544 // to allow the HELPER_FRAME to be installed inside an SO_INTOLERANT region
545 // which does not allow a return. The return is used by FORLAZYMACHSTATE
546 // to capture the state, but is not an actual return, so it is ok.
547 #define HELPER_METHOD_FRAME_BEGIN_EX_BODY(ret, helperFrame, gcpoll, allowGC) \
548 FORLAZYMACHSTATE_BEGINLOOP(int alwaysZero = 0;) \
550 INDEBUG(static BOOL __haveCheckedRestoreState = FALSE;) \
551 PERMIT_HELPER_METHOD_FRAME_BEGIN(); \
552 CHECK_HELPER_METHOD_FRAME_PERMITTED(); \
554 FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_BEGIN; \
555 FORLAZYMACHSTATE(CAPTURE_STATE(__helperframe.MachineState(), ret);) \
556 FORLAZYMACHSTATE_DEBUG_OK_TO_RETURN_END; \
557 INDEBUG(__helperframe.SetAddrOfHaveCheckedRestoreState(&__haveCheckedRestoreState)); \
558 DEBUG_ASSURE_NO_RETURN_BEGIN(HELPER_METHOD_FRAME); \
559 INCONTRACT(FCallGCCanTrigger::Enter()); \
560 __helperframe.Push(); \
561 MAKE_CURRENT_THREAD_AVAILABLE_EX(__helperframe.GetThread()); \
563 #define HELPER_METHOD_FRAME_BEGIN_EX(ret, helperFrame, gcpoll, allowGC) \
564 HELPER_METHOD_FRAME_BEGIN_EX_BODY(ret, helperFrame, gcpoll, allowGC) \
565 TESTHOOKCALL(AppDomainCanBeUnloaded(GET_THREAD()->GetDomain()->GetId().m_dwId,!allowGC)); \
566 /* <TODO>TODO TURN THIS ON!!! </TODO> */ \
568 INSTALL_MANAGED_EXCEPTION_DISPATCHER; \
569 INSTALL_UNWIND_AND_CONTINUE_HANDLER_FOR_HMF(&__helperframe);
571 #define HELPER_METHOD_FRAME_BEGIN_EX_NOTHROW(ret, helperFrame, gcpoll, allowGC, probeFailExpr) \
572 HELPER_METHOD_FRAME_BEGIN_EX_BODY(ret, helperFrame, gcpoll, allowGC) \
573 /* <TODO>TODO TURN THIS ON!!! </TODO> */ \
575 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GET_THREAD(), probeFailExpr);
578 // The while(__helperframe.RestoreState() needs a bit of explanation.
579 // The issue is insuring that the same machine state (which registers saved)
580 // exists when the machine state is probed (when the frame is created, and
581 // when it is actually used (when the frame is popped. We do this by creating
582 // a flow of control from use to def. Note that 'RestoreState' always returns false
583 // we never actually loop, but the compiler does not know that, and thus
584 // will be forced to make the keep the state of register spills the same at
585 // the two locations.
587 #define HELPER_METHOD_FRAME_END_EX_BODY(gcpoll,allowGC) \
588 /* <TODO>TODO TURN THIS ON!!! </TODO> */ \
590 __helperframe.Pop(); \
591 DEBUG_ASSURE_NO_RETURN_END(HELPER_METHOD_FRAME); \
592 INCONTRACT(FCallGCCanTrigger::Leave(__FUNCTION__, __FILE__, __LINE__)); \
593 FORLAZYMACHSTATE(alwaysZero = \
594 HelperMethodFrameRestoreState(INDEBUG_COMMA(&__helperframe) \
595 __helperframe.MachineState());) \
596 PERMIT_HELPER_METHOD_FRAME_END() \
597 } FORLAZYMACHSTATE_ENDLOOP(alwaysZero);
599 #define HELPER_METHOD_FRAME_END_EX(gcpoll,allowGC) \
600 UNINSTALL_UNWIND_AND_CONTINUE_HANDLER; \
601 UNINSTALL_MANAGED_EXCEPTION_DISPATCHER; \
602 TESTHOOKCALL(AppDomainCanBeUnloaded(GET_THREAD()->GetDomain()->GetId().m_dwId,!allowGC)); \
603 HELPER_METHOD_FRAME_END_EX_BODY(gcpoll,allowGC);
605 #define HELPER_METHOD_FRAME_END_EX_NOTHROW(gcpoll,allowGC) \
606 END_SO_INTOLERANT_CODE; \
607 HELPER_METHOD_FRAME_END_EX_BODY(gcpoll,allowGC);
609 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB(attribs) \
610 HELPER_METHOD_FRAME_BEGIN_EX( \
612 HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)), \
613 HELPER_METHOD_POLL(),TRUE)
615 #define HELPER_METHOD_FRAME_BEGIN_0() \
616 HELPER_METHOD_FRAME_BEGIN_ATTRIB(Frame::FRAME_ATTR_NONE)
618 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL(attribs) \
619 HELPER_METHOD_FRAME_BEGIN_EX( \
621 HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)), \
624 #define HELPER_METHOD_FRAME_BEGIN_NOPOLL() HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL(Frame::FRAME_ATTR_NONE)
626 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB_1(attribs, arg1) \
627 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
628 HELPER_METHOD_FRAME_BEGIN_EX( \
630 HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(attribs), \
631 (OBJECTREF*) &arg1), \
632 HELPER_METHOD_POLL(),TRUE)
634 #define HELPER_METHOD_FRAME_BEGIN_1(arg1) HELPER_METHOD_FRAME_BEGIN_ATTRIB_1(Frame::FRAME_ATTR_NONE, arg1)
636 #define HELPER_METHOD_FRAME_BEGIN_ATTRIB_2(attribs, arg1, arg2) \
637 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
638 static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
639 HELPER_METHOD_FRAME_BEGIN_EX( \
641 HELPER_FRAME_DECL(2)(HELPER_FRAME_ARGS(attribs), \
642 (OBJECTREF*) &arg1, (OBJECTREF*) &arg2), \
643 HELPER_METHOD_POLL(),TRUE)
645 #define HELPER_METHOD_FRAME_BEGIN_2(arg1, arg2) HELPER_METHOD_FRAME_BEGIN_ATTRIB_2(Frame::FRAME_ATTR_NONE, arg1, arg2)
647 #define HELPER_METHOD_FRAME_BEGIN_PROTECT(gc) \
648 HELPER_METHOD_FRAME_BEGIN_EX( \
650 HELPER_FRAME_DECL(PROTECT)(HELPER_FRAME_ARGS(Frame::FRAME_ATTR_NONE), \
651 (OBJECTREF*)&(gc), sizeof(gc)/sizeof(OBJECTREF)), \
652 HELPER_METHOD_POLL(),TRUE)
654 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_NOPOLL(attribs) \
655 HELPER_METHOD_FRAME_BEGIN_EX( \
657 HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)), \
660 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_NOPOLL(attribs) \
661 HELPER_METHOD_FRAME_BEGIN_EX( \
663 HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)), \
666 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB(attribs) \
667 HELPER_METHOD_FRAME_BEGIN_EX( \
669 HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(attribs)), \
670 HELPER_METHOD_POLL(),TRUE)
672 #define HELPER_METHOD_FRAME_BEGIN_RET_0() \
673 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB(Frame::FRAME_ATTR_NONE)
675 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_0() \
676 HELPER_METHOD_FRAME_BEGIN_EX( \
678 HELPER_FRAME_DECL(0)(HELPER_FRAME_ARGS(Frame::FRAME_ATTR_NONE)), \
679 HELPER_METHOD_POLL(),TRUE)
681 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(attribs, arg1) \
682 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
683 HELPER_METHOD_FRAME_BEGIN_EX( \
685 HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(attribs), \
686 (OBJECTREF*) &arg1), \
687 HELPER_METHOD_POLL(),TRUE)
689 #define HELPER_METHOD_FRAME_BEGIN_RET_NOTHROW_1(probeFailExpr, arg1) \
690 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
691 HELPER_METHOD_FRAME_BEGIN_EX_NOTHROW( \
693 HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(Frame::FRAME_ATTR_NO_THREAD_ABORT), \
694 (OBJECTREF*) &arg1), \
695 HELPER_METHOD_POLL(), TRUE, probeFailExpr)
697 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_1(attribs, arg1) \
698 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
699 HELPER_METHOD_FRAME_BEGIN_EX( \
701 HELPER_FRAME_DECL(1)(HELPER_FRAME_ARGS(attribs), \
702 (OBJECTREF*) &arg1), \
703 HELPER_METHOD_POLL(),TRUE)
705 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(attribs, arg1, arg2) \
706 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
707 static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
708 HELPER_METHOD_FRAME_BEGIN_EX( \
710 HELPER_FRAME_DECL(2)(HELPER_FRAME_ARGS(attribs), \
711 (OBJECTREF*) &arg1, (OBJECTREF*) &arg2), \
712 HELPER_METHOD_POLL(),TRUE)
714 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_2(attribs, arg1, arg2) \
715 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
716 static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
717 HELPER_METHOD_FRAME_BEGIN_EX( \
719 HELPER_FRAME_DECL(2)(HELPER_FRAME_ARGS(attribs), \
720 (OBJECTREF*) &arg1, (OBJECTREF*) &arg2), \
721 HELPER_METHOD_POLL(),TRUE)
723 #define HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_PROTECT(attribs, gc) \
724 HELPER_METHOD_FRAME_BEGIN_EX( \
726 HELPER_FRAME_DECL(PROTECT)(HELPER_FRAME_ARGS(attribs), \
727 (OBJECTREF*)&(gc), sizeof(gc)/sizeof(OBJECTREF)), \
728 HELPER_METHOD_POLL(),TRUE)
730 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_NOPOLL() \
731 HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_NOPOLL(Frame::FRAME_ATTR_NONE)
733 #define HELPER_METHOD_FRAME_BEGIN_RET_NOPOLL() \
734 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_NOPOLL(Frame::FRAME_ATTR_NONE)
736 #define HELPER_METHOD_FRAME_BEGIN_RET_1(arg1) \
737 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
738 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_NONE, arg1)
740 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_1(arg1) \
741 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
742 HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_1(Frame::FRAME_ATTR_NONE, arg1)
744 #define HELPER_METHOD_FRAME_BEGIN_RET_2(arg1, arg2) \
745 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
746 static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
747 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(Frame::FRAME_ATTR_NONE, arg1, arg2)
749 #define HELPER_METHOD_FRAME_BEGIN_RET_VC_2(arg1, arg2) \
750 static_assert(sizeof(arg1) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
751 static_assert(sizeof(arg2) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");\
752 HELPER_METHOD_FRAME_BEGIN_RET_VC_ATTRIB_2(Frame::FRAME_ATTR_NONE, arg1, arg2)
754 #define HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc) \
755 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_PROTECT(Frame::FRAME_ATTR_NONE, gc)
758 #define HELPER_METHOD_FRAME_END() HELPER_METHOD_FRAME_END_EX({},FALSE)
759 #define HELPER_METHOD_FRAME_END_POLL() HELPER_METHOD_FRAME_END_EX(HELPER_METHOD_POLL(),TRUE)
760 #define HELPER_METHOD_FRAME_END_NOTHROW()HELPER_METHOD_FRAME_END_EX_NOTHROW({},FALSE)
762 // This is the fastest way to do a GC poll if you have already erected a HelperMethodFrame
763 #define HELPER_METHOD_POLL() { __helperframe.Poll(); INCONTRACT(__fCallCheck.SetDidPoll()); }
765 // The HelperMethodFrame knows how to get its return address. Let other code get at it, too.
766 // (Uses comma operator to call InsureInit & discard result.
767 #define HELPER_METHOD_FRAME_GET_RETURN_ADDRESS() \
768 ( static_cast<UINT_PTR>( (__helperframe.InsureInit(false, NULL)), (__helperframe.MachineState()->GetRetAddr()) ) )
770 // Very short routines, or routines that are guarenteed to force GC or EH
771 // don't need to poll the GC. USE VERY SPARINGLY!!!
772 #define FC_GC_POLL_NOT_NEEDED() INCONTRACT(__fCallCheck.SetNotNeeded())
774 Object* FC_GCPoll(void* me, Object* objToProtect = NULL);
776 #define FC_GC_POLL_EX(ret) \
778 INCONTRACT(Thread::TriggersGC(GetThread());) \
779 INCONTRACT(__fCallCheck.SetDidPoll();) \
780 if (g_TrapReturningThreads.LoadWithoutBarrier()) \
782 if (FC_GCPoll(__me)) \
784 while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */ \
788 #define FC_GC_POLL() FC_GC_POLL_EX(;)
789 #define FC_GC_POLL_RET() FC_GC_POLL_EX(0)
791 #define FC_GC_POLL_AND_RETURN_OBJREF(obj) \
793 INCONTRACT(__fCallCheck.SetDidPoll();) \
794 Object* __temp = OBJECTREFToObject(obj); \
795 if (g_TrapReturningThreads.LoadWithoutBarrier()) \
797 __temp = FC_GCPoll(__me, __temp); \
798 while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */ \
803 #if defined(ENABLE_CONTRACTS)
804 #define FC_CAN_TRIGGER_GC() FCallGCCanTrigger::Enter()
805 #define FC_CAN_TRIGGER_GC_END() FCallGCCanTrigger::Leave(__FUNCTION__, __FILE__, __LINE__)
807 #define FC_CAN_TRIGGER_GC_HAVE_THREAD(thread) FCallGCCanTrigger::Enter(thread)
808 #define FC_CAN_TRIGGER_GC_HAVE_THREADEND(thread) FCallGCCanTrigger::Leave(thread, __FUNCTION__, __FILE__, __LINE__)
810 // turns on forbidGC for the lifetime of the instance
815 ForbidGC(const char *szFile, int lineNum);
819 // this little helper class checks to make certain
820 // 1) ForbidGC is set throughout the routine.
821 // 2) Sometime during the routine, a GC poll is done
823 class FCallCheck : public ForbidGC {
825 FCallCheck(const char *szFile, int lineNum);
827 void SetDidPoll() {LIMITED_METHOD_CONTRACT; didGCPoll = true; }
828 void SetNotNeeded() {LIMITED_METHOD_CONTRACT; notNeeded = true; }
832 DWORD unbreakableLockCount;
834 bool didGCPoll; // GC poll was done
835 bool notNeeded; // GC poll not needed
836 unsigned __int64 startTicks; // tick count at begining of FCall
839 // FC_COMMON_PROLOG is used for both FCalls and HCalls
840 #define FC_COMMON_PROLOG(target, assertFn) \
841 /* The following line has to be first. We do not want to trash last error */ \
842 DWORD __lastError = ::GetLastError(); \
843 static void* __cache = 0; \
844 assertFn(__cache, (LPVOID)target); \
846 Thread *_pThread = GetThread(); \
847 Thread::ObjectRefFlush(_pThread); \
848 /*_ASSERTE (_pThread->IsSOTolerant() ||*/ \
849 /* _pThread->HasThreadStateNC(Thread::TSNC_DisableSOCheckInHCALL)); */ \
851 FCallCheck __fCallCheck(__FILE__, __LINE__); \
852 FCALL_TRANSITION_BEGIN(); \
853 ::SetLastError(__lastError); \
855 void FCallAssert(void*& cache, void* target);
856 void HCallAssert(void*& cache, void* target);
859 #define FC_COMMON_PROLOG(target, assertFn) FCALL_TRANSITION_BEGIN()
860 #define FC_CAN_TRIGGER_GC()
861 #define FC_CAN_TRIGGER_GC_END()
862 #endif // ENABLE_CONTRACTS
865 // Macros that allows fcall to be split into two function to avoid the helper frame overhead on common fast
868 // The helper routine needs to know the name of the routine that called it so that it can look up the name of
869 // the managed routine this code is associted with (for managed stack traces). This is passed with the
870 // FC_INNER_PROLOG macro.
872 // The helper can set up a HELPER_METHOD_FRAME, but should pass the
873 // Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2 which indicates the exact number of
874 // unwinds to do to get back to managed code. Currently we only support depth 2 which means that the
875 // HELPER_METHOD_FRAME needs to be set up in the function directly called by the FCALL. The helper should
876 // use the NOINLINE macro to prevent the compiler from inlining it into the FCALL (which would obviously
877 // mess up the unwind count).
879 // The other invarient that needs to hold is that the epilog walker needs to be able to get from the call to
880 // the helper routine to the end of the FCALL using trivial heurisitics. The easiest (and only supported)
881 // way of doing this is to place your helper right before a return (eg at the end of the method). Generally
882 // this is not a problem at all, since the FCALL itself will pick off some common case and then tail-call to
883 // the helper for everything else. You must use the code:FC_INNER_RETURN macros to do the call, to insure
884 // that the C++ compiler does not tail-call optimize the call to the inner function and mess up the stack
887 // see code:ObjectNative::GetClass for an example
889 #define FC_INNER_PROLOG(outerfuncname) \
891 __me = GetEEFuncEntryPointMacro(outerfuncname); \
892 FC_CAN_TRIGGER_GC(); \
893 INCONTRACT(FCallCheck __fCallCheck(__FILE__, __LINE__));
895 // This variant should be used for inner fcall functions that have the
896 // __me value passed as an argument to the function. This allows
897 // inner functions to be shared across multiple fcalls.
898 #define FC_INNER_PROLOG_NO_ME_SETUP() \
899 FC_CAN_TRIGGER_GC(); \
900 INCONTRACT(FCallCheck __fCallCheck(__FILE__, __LINE__));
902 #define FC_INNER_EPILOG() \
903 FC_CAN_TRIGGER_GC_END();
905 // If you are using FC_INNER, and you are tail calling to the helper method (a common case), then you need
906 // to use the FC_INNER_RETURN macros (there is one for methods that return a value and another if the
907 // function returns void). This macro's purpose is to inhibit any tail calll optimization the C++ compiler
908 // might do, which would otherwise confuse the epilog walker.
910 // * See #FC_INNER for more
911 extern int FC_NO_TAILCALL;
912 #define FC_INNER_RETURN(type, expr) \
913 type __retVal = expr; \
914 while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */ \
917 #define FC_INNER_RETURN_VOID(stmt) \
919 while (0 == FC_NO_TAILCALL) { }; /* side effect the compile can't remove */ \
922 //==============================================================================================
923 // FIMPLn: A set of macros for generating the proto for the actual
924 // implementation (use FDECLN for header protos.)
926 // The hidden "__me" variable lets us recover the original MethodDesc*
927 // so any thrown exceptions will have the correct stack trace. FCThrow()
928 // passes this along to __FCThrowInternal().
929 //==============================================================================================
931 #define GetEEFuncEntryPointMacro(func) ((LPVOID)(func))
933 #define FCIMPL_PROLOG(funcname) \
935 __me = GetEEFuncEntryPointMacro(funcname); \
936 FC_COMMON_PROLOG(__me, FCallAssert)
939 #if defined(_DEBUG) && !defined(CROSSGEN_COMPILE)
941 // Build the list of all fcalls signatures. It is used in binder.cpp to verify
942 // compatibility of managed and unmanaged fcall signatures. The check is currently done
947 FCSigCheck(void* fnc, char* sig)
949 LIMITED_METHOD_CONTRACT;
952 next = g_pFCSigCheck;
953 g_pFCSigCheck = this;
960 static FCSigCheck* g_pFCSigCheck;
963 #define FCSIGCHECK(funcname, signature) \
964 static FCSigCheck UNIQUE_LABEL(FCSigCheck)(GetEEFuncEntryPointMacro(funcname), signature);
968 #define FCSIGCHECK(funcname, signature)
975 #if defined(__GNUC__)
977 #define FCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { FCIMPL_PROLOG(funcname)
978 #define FCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1) { FCIMPL_PROLOG(funcname)
979 #define FCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a1) { FCIMPL_PROLOG(funcname)
980 #define FCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1) { FCIMPL_PROLOG(funcname)
981 #define FCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a2, a1) { FCIMPL_PROLOG(funcname)
982 #define FCIMPL2_VI(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a2, a1) { FCIMPL_PROLOG(funcname)
983 #define FCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a2) { FCIMPL_PROLOG(funcname)
984 #define FCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3) { FCIMPL_PROLOG(funcname)
985 #define FCIMPL3_IIV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3) { FCIMPL_PROLOG(funcname)
986 #define FCIMPL3_VII(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a2, a1) { FCIMPL_PROLOG(funcname)
987 #define FCIMPL3_IVV(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a3, a2) { FCIMPL_PROLOG(funcname)
988 #define FCIMPL3_IVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a3, a1, a2) { FCIMPL_PROLOG(funcname)
989 #define FCIMPL3_VVI(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a3, a2, a1) { FCIMPL_PROLOG(funcname)
990 #define FCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a4, a3) { FCIMPL_PROLOG(funcname)
991 #define FCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a5, a4, a3) { FCIMPL_PROLOG(funcname)
992 #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)
993 #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)
994 #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)
995 #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)
996 #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)
997 #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)
998 #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)
999 #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)
1000 #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)
1002 #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)
1003 #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)
1007 #define FCIMPL0(rettype, funcname) FCSIGCHECK(funcname, #rettype) \
1008 rettype F_CALL_CONV funcname() { FCIMPL_PROLOG(funcname)
1009 #define FCIMPL1(rettype, funcname, a1) FCSIGCHECK(funcname, #rettype "," #a1) \
1010 rettype F_CALL_CONV funcname(a1) { FCIMPL_PROLOG(funcname)
1011 #define FCIMPL1_V(rettype, funcname, a1) FCSIGCHECK(funcname, #rettype "," "V" #a1) \
1012 rettype F_CALL_CONV funcname(a1) { FCIMPL_PROLOG(funcname)
1013 #define FCIMPL2(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2) \
1014 rettype F_CALL_CONV funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1015 #define FCIMPL2VA(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," "...") \
1016 rettype F_CALL_VA_CONV funcname(a1, a2, ...) { FCIMPL_PROLOG(funcname)
1017 #define FCIMPL2_VV(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," "V" #a2) \
1018 rettype F_CALL_CONV funcname(a2, a1) { FCIMPL_PROLOG(funcname)
1019 #define FCIMPL2_VI(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," #a2) \
1020 rettype F_CALL_CONV funcname(a2, a1) { FCIMPL_PROLOG(funcname)
1021 #define FCIMPL2_IV(rettype, funcname, a1, a2) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2) \
1022 rettype F_CALL_CONV funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1023 #define FCIMPL3(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3) \
1024 rettype F_CALL_CONV funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1025 #define FCIMPL3_IIV(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," "V" #a3) \
1026 rettype F_CALL_CONV funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1027 #define FCIMPL3_VII(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," #a2 "," #a3) \
1028 rettype F_CALL_CONV funcname(a2, a3, a1) { FCIMPL_PROLOG(funcname)
1029 #define FCIMPL3_IVV(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2 "," "V" #a3) \
1030 rettype F_CALL_CONV funcname(a1, a3, a2) { FCIMPL_PROLOG(funcname)
1031 #define FCIMPL3_IVI(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2 "," #a3) \
1032 rettype F_CALL_CONV funcname(a1, a3, a2) { FCIMPL_PROLOG(funcname)
1033 #define FCIMPL3_VVI(rettype, funcname, a1, a2, a3) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," "V" #a2 "," #a3) \
1034 rettype F_CALL_CONV funcname(a2, a1, a3) { FCIMPL_PROLOG(funcname)
1035 #define FCIMPL4(rettype, funcname, a1, a2, a3, a4) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4) \
1036 rettype F_CALL_CONV funcname(a1, a2, a4, a3) { FCIMPL_PROLOG(funcname)
1037 #define FCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5) \
1038 rettype F_CALL_CONV funcname(a1, a2, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1039 #define FCIMPL6(rettype, funcname, a1, a2, a3, a4, a5, a6) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6) \
1040 rettype F_CALL_CONV funcname(a1, a2, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1041 #define FCIMPL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7) \
1042 rettype F_CALL_CONV funcname(a1, a2, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1043 #define FCIMPL8(rettype, funcname, a1, a2, a3, a4, a5, a6, a7, a8) FCSIGCHECK(funcname, #rettype "," #a1 "," #a2 "," #a3 "," #a4 "," #a5 "," #a6 "," #a7 "," #a8) \
1044 rettype F_CALL_CONV funcname(a1, a2, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1045 #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) \
1046 rettype F_CALL_CONV funcname(a1, a2, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1047 #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) \
1048 rettype F_CALL_CONV funcname(a1, a2, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1049 #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) \
1050 rettype F_CALL_CONV funcname(a1, a2, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1051 #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) \
1052 rettype F_CALL_CONV funcname(a1, a2, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1053 #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) \
1054 rettype F_CALL_CONV funcname(a1, a2, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1055 #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) \
1056 rettype F_CALL_CONV funcname(a1, a2, a14, a13, a12, a11, a10, a9, a8, a7, a6, a5, a4, a3) { FCIMPL_PROLOG(funcname)
1058 #define FCIMPL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) FCSIGCHECK(funcname, #rettype "," #a1 "," "V" #a2 "," #a3 "," #a4 "," #a5) \
1059 rettype F_CALL_CONV funcname(a1, a3, a5, a4, a2) { FCIMPL_PROLOG(funcname)
1060 #define FCIMPL5_VII(rettype, funcname, a1, a2, a3, a4, a5) FCSIGCHECK(funcname, #rettype "," "V" #a1 "," #a2 "," #a3 "," #a4 "," #a5) \
1061 rettype F_CALL_CONV funcname(a2, a3, a5, a4, a1) { FCIMPL_PROLOG(funcname)
1065 #else // !_TARGET_X86_
1067 // non-x86 platforms don't have messed-up calling convention swizzling
1070 #define FCIMPL0(rettype, funcname) rettype funcname() { FCIMPL_PROLOG(funcname)
1071 #define FCIMPL1(rettype, funcname, a1) rettype funcname(a1) { FCIMPL_PROLOG(funcname)
1072 #define FCIMPL1_V(rettype, funcname, a1) rettype funcname(a1) { FCIMPL_PROLOG(funcname)
1073 #define FCIMPL2(rettype, funcname, a1, a2) rettype funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1074 #define FCIMPL2VA(rettype, funcname, a1, a2) rettype funcname(a1, a2, ...) { FCIMPL_PROLOG(funcname)
1075 #define FCIMPL2_VV(rettype, funcname, a1, a2) rettype funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1076 #define FCIMPL2_VI(rettype, funcname, a1, a2) rettype funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1077 #define FCIMPL2_IV(rettype, funcname, a1, a2) rettype funcname(a1, a2) { FCIMPL_PROLOG(funcname)
1078 #define FCIMPL3(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1079 #define FCIMPL3_IIV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1080 #define FCIMPL3_IVV(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1081 #define FCIMPL3_VII(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1082 #define FCIMPL3_IVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1083 #define FCIMPL3_VVI(rettype, funcname, a1, a2, a3) rettype funcname(a1, a2, a3) { FCIMPL_PROLOG(funcname)
1084 #define FCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype funcname(a1, a2, a3, a4) { FCIMPL_PROLOG(funcname)
1085 #define FCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5) { FCIMPL_PROLOG(funcname)
1086 #define FCIMPL6(rettype, funcname, a1, a2, a3, a4, a5, a6) rettype funcname(a1, a2, a3, a4, a5, a6) { FCIMPL_PROLOG(funcname)
1087 #define FCIMPL7(rettype, funcname, a1, a2, a3, a4, a5, a6, a7) rettype funcname(a1, a2, a3, a4, a5, a6, a7) { FCIMPL_PROLOG(funcname)
1088 #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)
1089 #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)
1090 #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)
1091 #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)
1092 #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)
1093 #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)
1094 #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)
1096 #define FCIMPL5_IVI(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5) { FCIMPL_PROLOG(funcname)
1097 #define FCIMPL5_VII(rettype, funcname, a1, a2, a3, a4, a5) rettype funcname(a1, a2, a3, a4, a5) { FCIMPL_PROLOG(funcname)
1101 //==============================================================================================
1102 // Use this to terminte an FCIMPLEND.
1103 //==============================================================================================
1105 #define FCIMPL_EPILOG() FCALL_TRANSITION_END()
1107 #define FCIMPLEND FCIMPL_EPILOG(); }
1109 #define HCIMPL_PROLOG(funcname) LPVOID __me; __me = 0; FC_COMMON_PROLOG(funcname, HCallAssert)
1111 // HCIMPL macros are just like their FCIMPL counterparts, however
1112 // they do not remember the function they come from. Thus they will not
1113 // show up in a stack trace. This is what you want for JIT helpers and the like
1117 #if defined(__GNUC__)
1119 #define HCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { HCIMPL_PROLOG(funcname)
1120 #define HCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1) { HCIMPL_PROLOG(funcname)
1121 #define HCIMPL1_RAW(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1) {
1122 #define HCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a1) { HCIMPL_PROLOG(funcname)
1123 #define HCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1) { HCIMPL_PROLOG(funcname)
1124 #define HCIMPL2_RAW(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1) {
1125 #define HCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, int /* ECX */, a2, a1) { HCIMPL_PROLOG(funcname)
1126 #define HCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(int /* EAX */, int /* EDX */, a1, a2) { HCIMPL_PROLOG(funcname)
1127 #define HCIMPL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...) { HCIMPL_PROLOG(funcname)
1128 #define HCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a3) { HCIMPL_PROLOG(funcname)
1129 #define HCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a4, a3) { HCIMPL_PROLOG(funcname)
1130 #define HCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(int /* EAX */, a2, a1, a5, a4, a3) { HCIMPL_PROLOG(funcname)
1132 #define HCCALL1(funcname, a1) funcname(0, 0, a1)
1133 #define HCCALL1_V(funcname, a1) funcname(0, 0, 0, a1)
1134 #define HCCALL2(funcname, a1, a2) funcname(0, a2, a1)
1135 #define HCCALL3(funcname, a1, a2, a3) funcname(0, a2, a1, a3)
1136 #define HCCALL4(funcname, a1, a2, a3, a4) funcname(0, a2, a1, a4, a3)
1137 #define HCCALL5(funcname, a1, a2, a3, a4, a5) funcname(0, a2, a1, a5, a4, a3)
1138 #define HCCALL1_PTR(rettype, funcptr, a1) rettype (F_CALL_CONV * funcptr)(int /* EAX */, int /* EDX */, a1)
1139 #define HCCALL2_PTR(rettype, funcptr, a1, a2) rettype (F_CALL_CONV * funcptr)(int /* EAX */, a2, a1)
1142 #define HCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { HCIMPL_PROLOG(funcname)
1143 #define HCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1144 #define HCIMPL1_RAW(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) {
1145 #define HCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1146 #define HCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1147 #define HCIMPL2_RAW(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) {
1148 #define HCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a2, a1) { HCIMPL_PROLOG(funcname)
1149 #define HCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1150 #define HCIMPL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...) { HCIMPL_PROLOG(funcname)
1151 #define HCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3) { HCIMPL_PROLOG(funcname)
1152 #define HCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(a1, a2, a4, a3) { HCIMPL_PROLOG(funcname)
1153 #define HCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a2, a5, a4, a3) { HCIMPL_PROLOG(funcname)
1155 #define HCCALL1(funcname, a1) funcname(a1)
1156 #define HCCALL1_V(funcname, a1) funcname(a1)
1157 #define HCCALL2(funcname, a1, a2) funcname(a1, a2)
1158 #define HCCALL3(funcname, a1, a2, a3) funcname(a1, a2, a3)
1159 #define HCCALL4(funcname, a1, a2, a3, a4) funcname(a1, a2, a4, a3)
1160 #define HCCALL5(funcname, a1, a2, a3, a4, a5) funcname(a1, a2, a5, a4, a3)
1161 #define HCCALL1_PTR(rettype, funcptr, a1) rettype (F_CALL_CONV * funcptr)(a1)
1162 #define HCCALL2_PTR(rettype, funcptr, a1, a2) rettype (F_CALL_CONV * funcptr)(a1, a2)
1166 #else // !_TARGET_X86_
1168 // non-x86 platforms don't have messed-up calling convention swizzling
1171 #define HCIMPL0(rettype, funcname) rettype F_CALL_CONV funcname() { HCIMPL_PROLOG(funcname)
1172 #define HCIMPL1(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1173 #define HCIMPL1_RAW(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) {
1174 #define HCIMPL1_V(rettype, funcname, a1) rettype F_CALL_CONV funcname(a1) { HCIMPL_PROLOG(funcname)
1175 #define HCIMPL2(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1176 #define HCIMPL2_RAW(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) {
1177 #define HCIMPL2_VV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1178 #define HCIMPL2_IV(rettype, funcname, a1, a2) rettype F_CALL_CONV funcname(a1, a2) { HCIMPL_PROLOG(funcname)
1179 #define HCIMPL2VA(rettype, funcname, a1, a2) rettype F_CALL_VA_CONV funcname(a1, a2, ...) { HCIMPL_PROLOG(funcname)
1180 #define HCIMPL3(rettype, funcname, a1, a2, a3) rettype F_CALL_CONV funcname(a1, a2, a3) { HCIMPL_PROLOG(funcname)
1181 #define HCIMPL4(rettype, funcname, a1, a2, a3, a4) rettype F_CALL_CONV funcname(a1, a2, a3, a4) { HCIMPL_PROLOG(funcname)
1182 #define HCIMPL5(rettype, funcname, a1, a2, a3, a4, a5) rettype F_CALL_CONV funcname(a1, a2, a3, a4, a5) { HCIMPL_PROLOG(funcname)
1184 #define HCCALL1(funcname, a1) funcname(a1)
1185 #define HCCALL1_V(funcname, a1) funcname(a1)
1186 #define HCCALL2(funcname, a1, a2) funcname(a1, a2)
1187 #define HCCALL3(funcname, a1, a2, a3) funcname(a1, a2, a3)
1188 #define HCCALL4(funcname, a1, a2, a3, a4) funcname(a1, a2, a3, a4)
1189 #define HCCALL5(funcname, a1, a2, a3, a4, a5) funcname(a1, a2, a3, a4, a5)
1190 #define HCCALL1_PTR(rettype, funcptr, a1) rettype (F_CALL_CONV * funcptr)(a1)
1191 #define HCCALL2_PTR(rettype, funcptr, a1, a2) rettype (F_CALL_CONV * funcptr)(a1, a2)
1195 #define HCIMPLEND_RAW }
1196 #define HCIMPLEND FCALL_TRANSITION_END(); }
1199 //==============================================================================================
1200 // Throws an exception from an FCall. See rexcep.h for a list of valid
1202 //==============================================================================================
1203 #define FCThrow(reKind) FCThrowEx(reKind, 0, 0, 0, 0)
1205 //==============================================================================================
1206 // This version lets you attach a message with inserts (similar to
1208 //==============================================================================================
1209 #define FCThrowEx(reKind, resID, arg1, arg2, arg3) \
1212 __FCThrow(__me, reKind, resID, arg1, arg2, arg3)) {}; \
1216 //==============================================================================================
1217 // Like FCThrow but can be used for a VOID-returning FCall. The only
1218 // difference is in the "return" statement.
1219 //==============================================================================================
1220 #define FCThrowVoid(reKind) FCThrowExVoid(reKind, 0, 0, 0, 0)
1222 //==============================================================================================
1223 // This version lets you attach a message with inserts (similar to
1225 //==============================================================================================
1226 #define FCThrowExVoid(reKind, resID, arg1, arg2, arg3) \
1229 __FCThrow(__me, reKind, resID, arg1, arg2, arg3)) {}; \
1233 // Use FCThrowRes to throw an exception with a localized error message from the
1234 // ResourceManager in managed code.
1235 #define FCThrowRes(reKind, resourceName) FCThrowArgumentEx(reKind, NULL, resourceName)
1236 #define FCThrowArgumentNull(argName) FCThrowArgumentEx(kArgumentNullException, argName, NULL)
1237 #define FCThrowArgumentOutOfRange(argName, message) FCThrowArgumentEx(kArgumentOutOfRangeException, argName, message)
1238 #define FCThrowArgument(argName, message) FCThrowArgumentEx(kArgumentException, argName, message)
1240 #define FCThrowArgumentEx(reKind, argName, resourceName) \
1243 __FCThrowArgument(__me, reKind, argName, resourceName)) {}; \
1247 // Use FCThrowRes to throw an exception with a localized error message from the
1248 // ResourceManager in managed code.
1249 #define FCThrowResVoid(reKind, resourceName) FCThrowArgumentVoidEx(reKind, NULL, resourceName)
1250 #define FCThrowArgumentNullVoid(argName) FCThrowArgumentVoidEx(kArgumentNullException, argName, NULL)
1251 #define FCThrowArgumentOutOfRangeVoid(argName, message) FCThrowArgumentVoidEx(kArgumentOutOfRangeException, argName, message)
1252 #define FCThrowArgumentVoid(argName, message) FCThrowArgumentVoidEx(kArgumentException, argName, message)
1254 #define FCThrowArgumentVoidEx(reKind, argName, resourceName) \
1257 __FCThrowArgument(__me, reKind, argName, resourceName)) {}; \
1263 // The x86 JIT calling convention expects returned small types (e.g. bool) to be
1264 // widened on return. The C/C++ calling convention does not guarantee returned
1265 // small types to be widened. The small types has to be artifically widened on return
1266 // to fit x86 JIT calling convention. Thus fcalls returning small types has to
1267 // use the FC_XXX_RET types to force C/C++ compiler to do the widening.
1269 // The most common small return type of FCALLs is bool. The widening of bool is
1270 // especially tricky since the value has to be also normalized. FC_BOOL_RET and
1271 // FC_RETURN_BOOL macros are provided to make it fool-proof. FCALLs returning bool
1272 // should be implemented using following pattern:
1274 // FCIMPL0(FC_BOOL_RET, Foo) // the return type should be FC_BOOL_RET
1277 // FC_RETURN_BOOL(ret); // return statements should be FC_RETURN_BOOL
1280 // This rules are verified in binder.cpp if COMPlus_ConsistencyCheck is set.
1284 // Use prefast build to ensure that functions returning FC_BOOL_RET
1285 // are using FC_RETURN_BOOL to return it. Missing FC_RETURN_BOOL will
1286 // result into type mismatch error in prefast builds. This will also
1287 // catch misuses of FC_BOOL_RET for other places (e.g. in FCALL parameters).
1289 typedef LPVOID FC_BOOL_RET;
1290 #define FC_RETURN_BOOL(x) do { return (LPVOID)!!(x); } while(0)
1294 #if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
1295 // The return value is artifically widened on x86 and amd64
1296 typedef INT32 FC_BOOL_RET;
1298 typedef CLR_BOOL FC_BOOL_RET;
1301 #define FC_RETURN_BOOL(x) do { return !!(x); } while(0)
1306 #if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
1307 // The return value is artifically widened on x86 and amd64
1308 typedef UINT32 FC_CHAR_RET;
1309 typedef INT32 FC_INT8_RET;
1310 typedef UINT32 FC_UINT8_RET;
1311 typedef INT32 FC_INT16_RET;
1312 typedef UINT32 FC_UINT16_RET;
1314 typedef CLR_CHAR FC_CHAR_RET;
1315 typedef INT8 FC_INT8_RET;
1316 typedef UINT8 FC_UINT8_RET;
1317 typedef INT16 FC_INT16_RET;
1318 typedef UINT16 FC_UINT16_RET;
1322 // FC_TypedByRef should be used for TypedReferences in FCall signatures
1323 #if defined(UNIX_AMD64_ABI) && !defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
1324 // Explicitly pass the TypedReferences by reference
1325 #define FC_TypedByRef TypedByRef&
1326 #define FC_DECIMAL DECIMAL&
1328 #define FC_TypedByRef TypedByRef
1329 #define FC_DECIMAL DECIMAL
1333 // The fcall entrypoints has to be at unique addresses. Use this helper macro to make
1334 // the code of the fcalls unique if you get assert in ecall.cpp that mentions it.
1335 // The parameter of the FCUnique macro is an arbitrary 32-bit random non-zero number.
1336 #define FCUnique(unique) { Volatile<int> u = (unique); while (u.LoadWithoutBarrier() == 0) { }; }
1341 // FCALL contracts come in two forms:
1343 // Short form that should be used if the FCALL contract does not have any extras like preconditions, failure injection. Example:
1345 // FCIMPL0(void, foo)
1350 // Long form that should be used otherwise. Example:
1352 // FCIMPL1(void, foo, void *p)
1356 // PRECONDITION(CheckPointer(p));
1362 // FCALL_CHECK defines the actual contract conditions required for FCALLs
1364 #define FCALL_CHECK \
1366 DISABLED(GC_TRIGGERS); /* FCALLS with HELPER frames have issues with GC_TRIGGERS */ \
1371 // FCALL_CONTRACT should be the following shortcut:
1373 // #define FCALL_CONTRACT CONTRACTL { FCALL_CHECK; } CONTRACTL_END;
1375 // Since there is very little value in having runtime contracts in FCalls, FCALL_CONTRACT is defined as static contract only for performance reasons.
1377 #define FCALL_CONTRACT \
1378 STATIC_CONTRACT_SO_TOLERANT; \
1379 STATIC_CONTRACT_THROWS; \
1380 /* FCALLS are a special case contract wise, they are "NOTRIGGER, unless you setup a frame" */ \
1381 STATIC_CONTRACT_GC_NOTRIGGER; \
1382 STATIC_CONTRACT_MODE_COOPERATIVE
1384 #endif //__FCall_h__