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 /*****************************************************************************/
63 extern void debugError(const char* msg, const char* file, unsigned line);
64 extern void DECLSPEC_NORETURN badCode();
65 extern void DECLSPEC_NORETURN badCode3(const char* msg, const char* msg2, int arg, __in_z const char* file, unsigned line);
66 extern void DECLSPEC_NORETURN noWay();
67 extern void DECLSPEC_NORETURN NOMEM();
68 extern void DECLSPEC_NORETURN fatal(int errCode);
70 extern void DECLSPEC_NORETURN noWayAssertBody();
71 extern void DECLSPEC_NORETURN noWayAssertBody(const char* cond, const char* file, unsigned line);
73 // Conditionally invoke the noway assert body. The conditional predicate is evaluated using a method on the tlsCompiler.
74 // If a noway_assert is hit, we ask the Compiler whether to raise an exception (i.e., conditionally raise exception.)
75 // To have backward compatibility between v4.5 and v4.0, in min-opts we take a shot at codegen rather than rethrow.
76 extern void noWayAssertBodyConditional(
77 #ifdef FEATURE_TRACELOGGING
78 const char* file, unsigned line
81 extern void noWayAssertBodyConditional(const char* cond, const char* file, unsigned line);
83 // Define MEASURE_NOWAY to 1 to enable code to count and rank individual noway_assert calls by occurrence.
84 // These asserts would be dynamically executed, but not necessarily fail. The provides some insight into
85 // the dynamic prevalence of these (if not a direct measure of their cost), which exist in non-DEBUG as
86 // well as DEBUG builds.
88 #define MEASURE_NOWAY 1
90 #define MEASURE_NOWAY 0
94 extern void RecordNowayAssertGlobal(const char* filename, unsigned line, const char* condStr);
95 #define RECORD_NOWAY_ASSERT(condStr) RecordNowayAssertGlobal(__FILE__, __LINE__, condStr);
97 #define RECORD_NOWAY_ASSERT(condStr)
102 #define NO_WAY(msg) (debugError(msg, __FILE__, __LINE__), noWay())
103 // Used for fallback stress mode
104 #define NO_WAY_NOASSERT(msg) noWay()
105 #define BADCODE(msg) (debugError(msg, __FILE__, __LINE__), badCode())
106 #define BADCODE3(msg, msg2, arg) badCode3(msg, msg2, arg, __FILE__, __LINE__)
107 // Used for an assert that we want to convert into BADCODE to force minopts, or in minopts to force codegen.
108 #define noway_assert(cond) \
111 RECORD_NOWAY_ASSERT(#cond) \
114 noWayAssertBodyConditional(#cond, __FILE__, __LINE__); \
117 #define unreached() noWayAssertBody("unreached", __FILE__, __LINE__)
119 #define NOWAY_MSG(msg) noWayAssertBodyConditional(msg, __FILE__, __LINE__)
123 #define NO_WAY(msg) noWay()
124 #define BADCODE(msg) badCode()
125 #define BADCODE3(msg, msg2, arg) badCode()
127 #ifdef FEATURE_TRACELOGGING
128 #define NOWAY_ASSERT_BODY_ARGUMENTS __FILE__, __LINE__
130 #define NOWAY_ASSERT_BODY_ARGUMENTS
133 #define noway_assert(cond) \
136 RECORD_NOWAY_ASSERT(#cond) \
139 noWayAssertBodyConditional(NOWAY_ASSERT_BODY_ARGUMENTS); \
142 #define unreached() noWayAssertBody()
144 #define NOWAY_MSG(msg) noWayAssertBodyConditional(NOWAY_ASSERT_BODY_ARGUMENTS)
148 // IMPL_LIMITATION is called when we encounter valid IL that is not
149 // supported by our current implementation because of various
150 // limitations (that could be removed in the future)
151 #define IMPL_LIMITATION(msg) NO_WAY(msg)
153 #if !defined(_TARGET_X86_) || !defined(LEGACY_BACKEND)
157 // This guy can return based on Config flag/Debugger
158 extern void notYetImplemented(const char* msg, const char* file, unsigned line);
159 #define NYIRAW(msg) notYetImplemented(msg, __FILE__, __LINE__)
161 #else // !defined(ALT_JIT)
163 #define NYIRAW(msg) NOWAY_MSG(msg)
165 #endif // !defined(ALT_JIT)
167 #define NYI(msg) NYIRAW("NYI: " msg)
168 #define NYI_IF(cond, msg) if (cond) NYIRAW("NYI: " msg)
170 #ifdef _TARGET_AMD64_
172 #define NYI_AMD64(msg) NYIRAW("NYI_AMD64: " msg)
173 #define NYI_X86(msg) do { } while (0)
174 #define NYI_ARM(msg) do { } while (0)
175 #define NYI_ARM64(msg) do { } while (0)
177 #elif defined(_TARGET_X86_)
179 #define NYI_AMD64(msg) do { } while (0)
180 #define NYI_X86(msg) NYIRAW("NYI_X86: " msg)
181 #define NYI_ARM(msg) do { } while (0)
182 #define NYI_ARM64(msg) do { } while (0)
184 #elif defined(_TARGET_ARM_)
186 #define NYI_AMD64(msg) do { } while (0)
187 #define NYI_X86(msg) do { } while (0)
188 #define NYI_ARM(msg) NYIRAW("NYI_ARM: " msg)
189 #define NYI_ARM64(msg) do { } while (0)
191 #elif defined(_TARGET_ARM64_)
193 #define NYI_AMD64(msg) do { } while (0)
194 #define NYI_X86(msg) do { } while (0)
195 #define NYI_ARM(msg) do { } while (0)
196 #define NYI_ARM64(msg) NYIRAW("NYI_ARM64: " msg)
200 #error "Unknown platform, not x86, ARM, or AMD64?"
204 #else // NYI not available; make it an assert.
206 #define NYI(msg) assert(!msg)
207 #define NYI_AMD64(msg) do { } while (0)
208 #define NYI_ARM(msg) do { } while (0)
209 #define NYI_ARM64(msg) do { } while (0)
211 #endif // NYI not available
213 #if !defined(_TARGET_X86_) && !defined(FEATURE_STACK_FP_X87)
215 #define NYI_FLAT_FP_X87(msg) NYI(msg)
216 #define NYI_FLAT_FP_X87_NC(msg) NYI(msg)
220 #define NYI_FLAT_FP_X87(msg) do { } while (0)
221 #define NYI_FLAT_FP_X87_NC(msg) do { } while (0)
223 #endif // !_TARGET_X86_ && !FEATURE_STACK_FP_X87
227 #if defined(_HOST_X86_) && !defined(FEATURE_PAL)
229 // While debugging in an Debugger, the "int 3" will cause the program to break
230 // Outside, the exception handler will just filter out the "int 3".
232 #define BreakIfDebuggerPresent() \
239 __except (EXCEPTION_EXECUTE_HANDLER) \
245 #define BreakIfDebuggerPresent() \
248 if (IsDebuggerPresent()) \
254 DWORD getBreakOnBadCode();
257 // For narrowing numeric conversions, the following two methods ensure that the
258 // source value fits in the destination type, using either "assert" or
259 // "noway_assert" to validate the conversion. Obviously, each returns the source value as
260 // the destination type.
262 // (There is an argument that these should be macros, to let the preprocessor capture
263 // a more useful file/line for the error message. But then we have to use comma expressions
264 // so that these can be used in expressions, etc., which is ugly. So I propose we rely on
265 // getting stack traces in other ways.)
266 template <typename Dst, typename Src>
267 inline Dst SafeCvtAssert(Src val)
269 assert(FitsIn<Dst>(val));
270 return static_cast<Dst>(val);
273 template <typename Dst, typename Src>
274 inline Dst SafeCvtNowayAssert(Src val)
276 noway_assert(FitsIn<Dst>(val));
277 return static_cast<Dst>(val);