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 /*****************************************************************************/
8 /*****************************************************************************/
10 #include <corjit.h> // for CORJIT_INTERNALERROR
11 #include <safemath.h> // For FitsIn, used by SafeCvt methods.
13 #define FATAL_JIT_EXCEPTION 0x02345678
20 EXCEPTION_POINTERS exceptionPointers;
27 // Only catch JIT internal errors (will not catch EE generated Errors)
28 extern LONG __JITfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam);
30 #define setErrorTrap(compHnd, ParamType, paramDef, paramRef) \
31 struct __JITParam : ErrorTrapParam \
35 __JITparam.errc = CORJIT_INTERNALERROR; \
36 __JITparam.jitInfo = compHnd; \
37 __JITparam.param = paramRef; \
38 PAL_TRY(__JITParam*, __JITpParam, &__JITparam) \
40 ParamType paramDef = __JITpParam->param;
42 // Only catch JIT internal errors (will not catch EE generated Errors)
43 #define impJitErrorTrap() \
45 PAL_EXCEPT_FILTER(__JITfilter) \
47 int __errc = __JITparam.errc; \
50 #define endErrorTrap() \
54 #define finallyErrorTrap() \
59 /*****************************************************************************/
61 extern void debugError(const char* msg, const char* file, unsigned line);
62 extern void DECLSPEC_NORETURN badCode();
63 extern void DECLSPEC_NORETURN
64 badCode3(const char* msg, const char* msg2, int arg, __in_z const char* file, unsigned line);
65 extern void DECLSPEC_NORETURN noWay();
66 extern void DECLSPEC_NORETURN NOMEM();
67 extern void DECLSPEC_NORETURN fatal(int errCode);
69 extern void DECLSPEC_NORETURN noWayAssertBody();
70 extern void DECLSPEC_NORETURN noWayAssertBody(const char* cond, const char* file, unsigned line);
72 // Conditionally invoke the noway assert body. The conditional predicate is evaluated using a method on the tlsCompiler.
73 // If a noway_assert is hit, we ask the Compiler whether to raise an exception (i.e., conditionally raise exception.)
74 // To have backward compatibility between v4.5 and v4.0, in min-opts we take a shot at codegen rather than rethrow.
75 extern void noWayAssertBodyConditional(
76 #ifdef FEATURE_TRACELOGGING
77 const char* file, unsigned line
80 extern void noWayAssertBodyConditional(const char* cond, const char* file, unsigned line);
82 #if !defined(_TARGET_X86_) || !defined(LEGACY_BACKEND)
84 // This guy can return based on Config flag/Debugger
85 extern void notYetImplemented(const char* msg, const char* file, unsigned line);
86 #define NYI(msg) notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
87 #define NYI_IF(cond, msg) \
89 notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
93 #define NYI_AMD64(msg) notYetImplemented("NYI_AMD64: " #msg, __FILE__, __LINE__)
94 #define NYI_X86(msg) \
98 #define NYI_ARM(msg) \
102 #define NYI_ARM64(msg) \
107 #elif defined(_TARGET_X86_)
109 #define NYI_AMD64(msg) \
113 #define NYI_X86(msg) notYetImplemented("NYI_X86: " #msg, __FILE__, __LINE__)
114 #define NYI_ARM(msg) \
118 #define NYI_ARM64(msg) \
123 #elif defined(_TARGET_ARM_)
125 #define NYI_AMD64(msg) \
129 #define NYI_X86(msg) \
133 #define NYI_ARM(msg) notYetImplemented("NYI_ARM: " #msg, __FILE__, __LINE__)
134 #define NYI_ARM64(msg) \
139 #elif defined(_TARGET_ARM64_)
141 #define NYI_AMD64(msg) \
145 #define NYI_X86(msg) \
149 #define NYI_ARM(msg) \
153 #define NYI_ARM64(msg) notYetImplemented("NYI_ARM64: " #msg, __FILE__, __LINE__)
157 #error "Unknown platform, not x86, ARM, or AMD64?"
161 #else // defined(_TARGET_X86_) && defined(LEGACY_BACKEND)
163 #define NYI(msg) assert(!msg)
164 #define NYI_AMD64(msg) \
168 #define NYI_ARM(msg) \
172 #define NYI_ARM64(msg) \
177 #endif // _TARGET_X86_
179 #if !defined(_TARGET_X86_) && !defined(FEATURE_STACK_FP_X87)
180 #define NYI_FLAT_FP_X87(msg) notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
181 #define NYI_FLAT_FP_X87_NC(msg) notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
185 #define NYI_FLAT_FP_X87(msg) \
189 #define NYI_FLAT_FP_X87_NC(msg) \
194 #endif // !_TARGET_X86_ && !FEATURE_STACK_FP_X87
197 #define NO_WAY(msg) (debugError(msg, __FILE__, __LINE__), noWay())
198 // Used for fallback stress mode
199 #define NO_WAY_NOASSERT(msg) noWay()
200 #define BADCODE(msg) (debugError(msg, __FILE__, __LINE__), badCode())
201 #define BADCODE3(msg, msg2, arg) badCode3(msg, msg2, arg, __FILE__, __LINE__)
202 // Used for an assert that we want to convert into BADCODE to force minopts, or in minopts to force codegen.
203 #define noway_assert(cond) \
208 noWayAssertBodyConditional(#cond, __FILE__, __LINE__); \
211 #define unreached() noWayAssertBody("unreached", __FILE__, __LINE__)
215 #define NO_WAY(msg) noWay()
216 #define BADCODE(msg) badCode()
217 #define BADCODE3(msg, msg2, arg) badCode()
219 #ifdef FEATURE_TRACELOGGING
220 #define NOWAY_ASSERT_BODY_ARGUMENTS __FILE__, __LINE__
222 #define NOWAY_ASSERT_BODY_ARGUMENTS
225 #define noway_assert(cond) \
230 noWayAssertBodyConditional(NOWAY_ASSERT_BODY_ARGUMENTS); \
233 #define unreached() noWayAssertBody()
237 // IMPL_LIMITATION is called when we encounter valid IL that is not
238 // supported by our current implementation because of various
239 // limitations (that could be removed in the future)
240 #define IMPL_LIMITATION(msg) NO_WAY(msg)
242 #if defined(_HOST_X86_)
244 // While debugging in an Debugger, the "int 3" will cause the program to break
245 // Outside, the exception handler will just filter out the "int 3".
247 #define BreakIfDebuggerPresent() \
254 __except (EXCEPTION_EXECUTE_HANDLER) \
260 #define BreakIfDebuggerPresent() \
263 if (IsDebuggerPresent()) \
269 DWORD getBreakOnBadCode();
272 // For narrowing numeric conversions, the following two methods ensure that the
273 // source value fits in the destination type, using either "assert" or
274 // "noway_assert" to validate the conversion. Obviously, each returns the source value as
275 // the destination type.
277 // (There is an argument that these should be macros, to let the preprocessor capture
278 // a more useful file/line for the error message. But then we have to use comma expressions
279 // so that these can be used in expressions, etc., which is ugly. So I propose we rely on
280 // getting stack traces in other ways.)
281 template <typename Dst, typename Src>
282 inline Dst SafeCvtAssert(Src val)
284 assert(FitsIn<Dst>(val));
285 return static_cast<Dst>(val);
288 template <typename Dst, typename Src>
289 inline Dst SafeCvtNowayAssert(Src val)
291 noway_assert(FitsIn<Dst>(val));
292 return static_cast<Dst>(val);