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;
21 ErrorTrapParam() { jitInfo = NULL; }
24 extern LONG __EEfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam);
25 // Only catch JIT internal errors (will not catch EE generated Errors)
26 extern LONG __JITfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam);
28 #define setErrorTrap(compHnd, ParamType, paramDef, paramRef) \
29 struct __JITParam : ErrorTrapParam \
33 __JITparam.errc = CORJIT_INTERNALERROR; \
34 __JITparam.jitInfo = compHnd; \
35 __JITparam.param = paramRef; \
36 PAL_TRY(__JITParam *, __JITpParam, &__JITparam) \
38 ParamType paramDef = __JITpParam->param;
40 // Catch only JitGeneratedErrors
41 #define impJitErrorTrap() \
43 PAL_EXCEPT_FILTER(__JITfilter) \
45 int __errc = __JITparam.errc; (void) __errc;
47 // Catch all errors (including recoverable ones from the EE)
48 #define impErrorTrap() \
50 PAL_EXCEPT_FILTER(__EEfilter) \
52 __JITparam.jitInfo->HandleException(&__JITparam.exceptionPointers);
54 #define endErrorTrap() \
58 #define finallyErrorTrap() \
64 /*****************************************************************************/
66 extern void debugError(const char* msg, const char* file, unsigned line);
67 extern void DECLSPEC_NORETURN badCode();
68 extern void DECLSPEC_NORETURN badCode3(const char* msg, const char* msg2, int arg, __in_z const char* file, unsigned line);
69 extern void DECLSPEC_NORETURN noWay();
70 extern void DECLSPEC_NORETURN NOMEM();
71 extern void DECLSPEC_NORETURN fatal(int errCode);
73 extern void DECLSPEC_NORETURN noWayAssertBody();
74 extern void DECLSPEC_NORETURN noWayAssertBody(const char * cond, const char * file, unsigned line);
76 // Conditionally invoke the noway assert body. The conditional predicate is evaluated using a method on the tlsCompiler.
77 // If a noway_assert is hit, we ask the Compiler whether to raise an exception (i.e., conditionally raise exception.)
78 // To have backward compatibility between v4.5 and v4.0, in min-opts we take a shot at codegen rather than rethrow.
79 extern void noWayAssertBodyConditional(
80 #ifdef FEATURE_TRACELOGGING
81 const char * file, unsigned line
84 extern void noWayAssertBodyConditional(const char * cond, const char * file, unsigned line);
86 #if !defined(_TARGET_X86_) || !defined(LEGACY_BACKEND)
88 // This guy can return based on Config flag/Debugger
89 extern void notYetImplemented(const char * msg, const char * file, unsigned line);
90 #define NYI(msg) notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
91 #define NYI_IF(cond, msg) if (cond) notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
95 #define NYI_AMD64(msg) notYetImplemented("NYI_AMD64: " # msg, __FILE__, __LINE__)
96 #define NYI_X86(msg) do { } while (0)
97 #define NYI_ARM(msg) do { } while (0)
98 #define NYI_ARM64(msg) do { } while (0)
100 #elif defined(_TARGET_X86_)
102 #define NYI_AMD64(msg) do { } while (0)
103 #define NYI_X86(msg) notYetImplemented("NYI_X86: " # msg, __FILE__, __LINE__)
104 #define NYI_ARM(msg) do { } while (0)
105 #define NYI_ARM64(msg) do { } while (0)
107 #elif defined(_TARGET_ARM_)
109 #define NYI_AMD64(msg) do { } while (0)
110 #define NYI_X86(msg) do { } while (0)
111 #define NYI_ARM(msg) notYetImplemented("NYI_ARM: " # msg, __FILE__, __LINE__)
112 #define NYI_ARM64(msg) do { } while (0)
114 #elif defined(_TARGET_ARM64_)
116 #define NYI_AMD64(msg) do { } while (0)
117 #define NYI_X86(msg) do { } while (0)
118 #define NYI_ARM(msg) do { } while (0)
119 #define NYI_ARM64(msg) notYetImplemented("NYI_ARM64: " # msg, __FILE__, __LINE__)
123 #error "Unknown platform, not x86, ARM, or AMD64?"
127 #else // defined(_TARGET_X86_) && defined(LEGACY_BACKEND)
130 #define NYI(msg) assert(!msg)
131 #define NYI_AMD64(msg) do { } while (0)
132 #define NYI_ARM(msg) do { } while (0)
133 #define NYI_ARM64(msg) do { } while (0)
135 #endif // _TARGET_X86_
137 #if !defined(_TARGET_X86_) && !defined(FEATURE_STACK_FP_X87)
138 #define NYI_FLAT_FP_X87(msg) notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
139 #define NYI_FLAT_FP_X87_NC(msg) notYetImplemented("NYI: " #msg, __FILE__, __LINE__)
143 #define NYI_FLAT_FP_X87(msg) do { } while (0)
144 #define NYI_FLAT_FP_X87_NC(msg) do { } while (0)
146 #endif // !_TARGET_X86_ && !FEATURE_STACK_FP_X87
150 #define NO_WAY(msg) (debugError(msg, __FILE__, __LINE__), noWay())
151 // Used for fallback stress mode
152 #define NO_WAY_NOASSERT(msg) noWay()
153 #define BADCODE(msg) (debugError(msg, __FILE__, __LINE__), badCode())
154 #define BADCODE3(msg, msg2, arg) badCode3(msg, msg2, arg, __FILE__, __LINE__)
155 //Used for an assert that we want to convert into BADCODE to force minopts, or in minopts to force codegen.
156 #define noway_assert(cond) do { if (!(cond)) { noWayAssertBodyConditional( # cond , __FILE__, __LINE__); } } while (0)
157 #define unreached() noWayAssertBody("unreached", __FILE__, __LINE__)
161 #define NO_WAY(msg) noWay()
162 #define BADCODE(msg) badCode()
163 #define BADCODE3(msg, msg2, arg) badCode()
165 #ifdef FEATURE_TRACELOGGING
166 #define NOWAY_ASSERT_BODY_ARGUMENTS __FILE__, __LINE__
168 #define NOWAY_ASSERT_BODY_ARGUMENTS
171 #define noway_assert(cond) do { if (!(cond)) { noWayAssertBodyConditional(NOWAY_ASSERT_BODY_ARGUMENTS); } } while (0)
172 #define unreached() noWayAssertBody()
176 // IMPL_LIMITATION is called when we encounter valid IL that is not
177 // supported by our current implementation because of various
178 // limitations (that could be removed in the future)
179 #define IMPL_LIMITATION(msg) NO_WAY(msg)
182 #if defined(_HOST_X86_)
184 // While debugging in an Debugger, the "int 3" will cause the program to break
185 // Outside, the exception handler will just filter out the "int 3".
187 #define BreakIfDebuggerPresent() \
188 do { __try { __asm {int 3} } __except(EXCEPTION_EXECUTE_HANDLER) {} } \
192 #define BreakIfDebuggerPresent() \
193 do { if (IsDebuggerPresent()) DebugBreak(); } \
198 DWORD getBreakOnBadCode();
201 // For narrowing numeric conversions, the following two methods ensure that the
202 // source value fits in the destination type, using either "assert" or
203 // "noway_assert" to validate the conversion. Obviously, each returns the source value as
204 // the destination type.
206 // (There is an argument that these should be macros, to let the preprocessor capture
207 // a more useful file/line for the error message. But then we have to use comma expressions
208 // so that these can be used in expressions, etc., which is ugly. So I propose we rely on
209 // getting stack traces in other ways.)
210 template <typename Dst, typename Src>
211 inline Dst SafeCvtAssert(Src val)
213 assert(FitsIn<Dst>(val));
214 return static_cast<Dst>(val);
217 template <typename Dst, typename Src>
218 inline Dst SafeCvtNowayAssert(Src val)
220 noway_assert(FitsIn<Dst>(val));
221 return static_cast<Dst>(val);