Implement JitDefaultFill
[platform/upstream/coreclr.git] / src / jit / jit.h
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 /*****************************************************************************/
6 #ifndef _JIT_H_
7 #define _JIT_H_
8 /*****************************************************************************/
9
10 //
11 // clr.sln only defines _DEBUG
12 // The jit uses DEBUG rather than _DEBUG
13 // So we make sure that _DEBUG implies DEBUG
14 //
15 #ifdef _DEBUG
16 #ifndef DEBUG
17 #define DEBUG 1
18 #endif
19 #endif
20
21 // Clang-format messes with the indentation of comments if they directly precede an
22 // ifdef. This macro allows us to anchor the comments to the regular flow of code.
23 #define CLANG_FORMAT_COMMENT_ANCHOR ;
24
25 // Clang-tidy replaces 0 with nullptr in some templated functions, causing a build
26 // break. Replacing those instances with ZERO avoids this change
27 #define ZERO 0
28
29 #ifdef _MSC_VER
30 // These don't seem useful, so turning them off is no big deal
31 #pragma warning(disable : 4065) // "switch statement contains 'default' but no 'case' labels" (happens due to #ifdefs)
32 #pragma warning(disable : 4510) // can't generate default constructor
33 #pragma warning(disable : 4511) // can't generate copy constructor
34 #pragma warning(disable : 4512) // can't generate assignment constructor
35 #pragma warning(disable : 4610) // user defined constructor required
36 #pragma warning(disable : 4211) // nonstandard extention used (char name[0] in structs)
37 #pragma warning(disable : 4127) // conditional expression constant
38 #pragma warning(disable : 4201) // "nonstandard extension used : nameless struct/union"
39
40 // Depending on the code base, you may want to not disable these
41 #pragma warning(disable : 4245) // assigning signed / unsigned
42 #pragma warning(disable : 4146) // unary minus applied to unsigned
43
44 #pragma warning(disable : 4100) // unreferenced formal parameter
45 #pragma warning(disable : 4291) // new operator without delete (only in emitX86.cpp)
46 #endif
47
48 #ifdef _MSC_VER
49 #define CHECK_STRUCT_PADDING 0 // Set this to '1' to enable warning C4820 "'bytes' bytes padding added after
50                                // construct 'member_name'" on interesting structs/classes
51 #else
52 #define CHECK_STRUCT_PADDING 0 // Never enable it for non-MSFT compilers
53 #endif
54
55 #if defined(_X86_)
56 #if defined(_ARM_)
57 #error Cannot define both _X86_ and _ARM_
58 #endif
59 #if defined(_AMD64_)
60 #error Cannot define both _X86_ and _AMD64_
61 #endif
62 #if defined(_ARM64_)
63 #error Cannot define both _X86_ and _ARM64_
64 #endif
65 #define _HOST_X86_
66 #elif defined(_AMD64_)
67 #if defined(_X86_)
68 #error Cannot define both _AMD64_ and _X86_
69 #endif
70 #if defined(_ARM_)
71 #error Cannot define both _AMD64_ and _ARM_
72 #endif
73 #if defined(_ARM64_)
74 #error Cannot define both _AMD64_ and _ARM64_
75 #endif
76 #define _HOST_AMD64_
77 #elif defined(_ARM_)
78 #if defined(_X86_)
79 #error Cannot define both _ARM_ and _X86_
80 #endif
81 #if defined(_AMD64_)
82 #error Cannot define both _ARM_ and _AMD64_
83 #endif
84 #if defined(_ARM64_)
85 #error Cannot define both _ARM_ and _ARM64_
86 #endif
87 #define _HOST_ARM_
88 #elif defined(_ARM64_)
89 #if defined(_X86_)
90 #error Cannot define both _ARM64_ and _X86_
91 #endif
92 #if defined(_AMD64_)
93 #error Cannot define both _ARM64_ and _AMD64_
94 #endif
95 #if defined(_ARM_)
96 #error Cannot define both _ARM64_ and _ARM_
97 #endif
98 #define _HOST_ARM64_
99 #else
100 #error Unsupported or unset host architecture
101 #endif
102
103 #if defined(_HOST_AMD64_) || defined(_HOST_ARM64_)
104 #define _HOST_64BIT_
105 #endif
106
107 #if defined(_TARGET_X86_)
108 #if defined(_TARGET_ARM_)
109 #error Cannot define both _TARGET_X86_ and _TARGET_ARM_
110 #endif
111 #if defined(_TARGET_AMD64_)
112 #error Cannot define both _TARGET_X86_ and _TARGET_AMD64_
113 #endif
114 #if defined(_TARGET_ARM64_)
115 #error Cannot define both _TARGET_X86_ and _TARGET_ARM64_
116 #endif
117 #if !defined(_HOST_X86_)
118 #define _CROSS_COMPILER_
119 #endif
120 #elif defined(_TARGET_AMD64_)
121 #if defined(_TARGET_X86_)
122 #error Cannot define both _TARGET_AMD64_ and _TARGET_X86_
123 #endif
124 #if defined(_TARGET_ARM_)
125 #error Cannot define both _TARGET_AMD64_ and _TARGET_ARM_
126 #endif
127 #if defined(_TARGET_ARM64_)
128 #error Cannot define both _TARGET_AMD64_ and _TARGET_ARM64_
129 #endif
130 #if !defined(_HOST_AMD64_)
131 #define _CROSS_COMPILER_
132 #endif
133 #elif defined(_TARGET_ARM_)
134 #if defined(_TARGET_X86_)
135 #error Cannot define both _TARGET_ARM_ and _TARGET_X86_
136 #endif
137 #if defined(_TARGET_AMD64_)
138 #error Cannot define both _TARGET_ARM_ and _TARGET_AMD64_
139 #endif
140 #if defined(_TARGET_ARM64_)
141 #error Cannot define both _TARGET_ARM_ and _TARGET_ARM64_
142 #endif
143 #if !defined(_HOST_ARM_)
144 #define _CROSS_COMPILER_
145 #endif
146 #elif defined(_TARGET_ARM64_)
147 #if defined(_TARGET_X86_)
148 #error Cannot define both _TARGET_ARM64_ and _TARGET_X86_
149 #endif
150 #if defined(_TARGET_AMD64_)
151 #error Cannot define both _TARGET_ARM64_ and _TARGET_AMD64_
152 #endif
153 #if defined(_TARGET_ARM_)
154 #error Cannot define both _TARGET_ARM64_ and _TARGET_ARM_
155 #endif
156 #if !defined(_HOST_ARM64_)
157 #define _CROSS_COMPILER_
158 #endif
159 #else
160 #error Unsupported or unset target architecture
161 #endif
162
163 #if defined(_TARGET_AMD64_) || defined(_TARGET_ARM64_)
164 #define _TARGET_64BIT_
165 #endif
166
167 #if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
168 #define _TARGET_XARCH_
169 #endif
170
171 #if defined(_TARGET_ARM_) || defined(_TARGET_ARM64_)
172 #define _TARGET_ARMARCH_
173 #endif
174
175 // If the UNIX_AMD64_ABI is defined make sure that _TARGET_AMD64_ is also defined.
176 #if defined(UNIX_AMD64_ABI)
177 #if !defined(_TARGET_AMD64_)
178 #error When UNIX_AMD64_ABI is defined you must define _TARGET_AMD64_ defined as well.
179 #endif
180 #endif
181
182 // If the UNIX_X86_ABI is defined make sure that _TARGET_X86_ is also defined.
183 #if defined(UNIX_X86_ABI)
184 #if !defined(_TARGET_X86_)
185 #error When UNIX_X86_ABI is defined you must define _TARGET_X86_ defined as well.
186 #endif
187 #endif
188
189 #if defined(PLATFORM_UNIX)
190 #define _HOST_UNIX_
191 #endif
192
193 // Are we generating code to target Unix? This is true if we will run on Unix (_HOST_UNIX_ is defined).
194 // It's also true if we are building an altjit targetting Unix, which we determine by checking if either
195 // UNIX_AMD64_ABI or UNIX_X86_ABI is defined.
196 #if defined(_HOST_UNIX_) || ((defined(UNIX_AMD64_ABI) || defined(UNIX_X86_ABI)) && defined(ALT_JIT))
197 #define _TARGET_UNIX_
198 #endif
199
200 // --------------------------------------------------------------------------------
201 // IMAGE_FILE_MACHINE_TARGET
202 // --------------------------------------------------------------------------------
203
204 #if defined(_TARGET_X86_)
205 #define IMAGE_FILE_MACHINE_TARGET IMAGE_FILE_MACHINE_I386
206 #elif defined(_TARGET_AMD64_)
207 #define IMAGE_FILE_MACHINE_TARGET IMAGE_FILE_MACHINE_AMD64
208 #elif defined(_TARGET_ARM_)
209 #define IMAGE_FILE_MACHINE_TARGET IMAGE_FILE_MACHINE_ARMNT
210 #elif defined(_TARGET_ARM64_)
211 #define IMAGE_FILE_MACHINE_TARGET IMAGE_FILE_MACHINE_ARM64 // 0xAA64
212 #else
213 #error Unsupported or unset target architecture
214 #endif
215
216 // Include the AMD64 unwind codes when appropriate.
217 #if defined(_TARGET_AMD64_)
218 // We need to temporarily set PLATFORM_UNIX, if necessary, to get the Unix-specific unwind codes.
219 #if defined(_TARGET_UNIX_) && !defined(_HOST_UNIX_)
220 #define PLATFORM_UNIX
221 #endif
222 #include "win64unwind.h"
223 #if defined(_TARGET_UNIX_) && !defined(_HOST_UNIX_)
224 #undef PLATFORM_UNIX
225 #endif
226 #endif
227
228 #include "corhdr.h"
229 #include "corjit.h"
230 #include "jitee.h"
231
232 #define __OPERATOR_NEW_INLINE 1 // indicate that I will define these
233 #define __PLACEMENT_NEW_INLINE  // don't bring in the global placement new, it is easy to make a mistake
234                                 // with our new(compiler*) pattern.
235
236 #include "utilcode.h" // this defines assert as _ASSERTE
237 #include "host.h"     // this redefines assert for the JIT to use assertAbort
238 #include "utils.h"
239
240 #ifdef DEBUG
241 #define INDEBUG(x) x
242 #define INDEBUG_COMMA(x) x,
243 #define DEBUGARG(x) , x
244 #else
245 #define INDEBUG(x)
246 #define INDEBUG_COMMA(x)
247 #define DEBUGARG(x)
248 #endif
249
250 #if defined(DEBUG) || defined(LATE_DISASM)
251 #define INDEBUG_LDISASM_COMMA(x) x,
252 #else
253 #define INDEBUG_LDISASM_COMMA(x)
254 #endif
255
256 #if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
257 #define FEATURE_UNIX_AMD64_STRUCT_PASSING_ONLY_ARG(x) , x
258 #define FEATURE_UNIX_AMD64_STRUCT_PASSING_ONLY(x) x
259 #else // !defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
260 #define FEATURE_UNIX_AMD64_STRUCT_PASSING_ONLY_ARG(x)
261 #define FEATURE_UNIX_AMD64_STRUCT_PASSING_ONLY(x)
262 #endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
263
264 #if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING) || (!defined(_TARGET_64BIT_) && !defined(LEGACY_BACKEND))
265 #define FEATURE_PUT_STRUCT_ARG_STK 1
266 #define PUT_STRUCT_ARG_STK_ONLY_ARG(x) , x
267 #define PUT_STRUCT_ARG_STK_ONLY(x) x
268 #else // !(defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)|| (!defined(_TARGET_64BIT_) && !defined(LEGACY_BACKEND)))
269 #define PUT_STRUCT_ARG_STK_ONLY_ARG(x)
270 #define PUT_STRUCT_ARG_STK_ONLY(x)
271 #endif // !(defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)|| (!defined(_TARGET_64BIT_) && !defined(LEGACY_BACKEND)))
272
273 #if defined(UNIX_AMD64_ABI)
274 #define UNIX_AMD64_ABI_ONLY_ARG(x) , x
275 #define UNIX_AMD64_ABI_ONLY(x) x
276 #else // !defined(UNIX_AMD64_ABI)
277 #define UNIX_AMD64_ABI_ONLY_ARG(x)
278 #define UNIX_AMD64_ABI_ONLY(x)
279 #endif // defined(UNIX_AMD64_ABI)
280
281 #if defined(UNIX_AMD64_ABI) || defined(_TARGET_ARM64_)
282 #define MULTIREG_HAS_SECOND_GC_RET 1
283 #define MULTIREG_HAS_SECOND_GC_RET_ONLY_ARG(x) , x
284 #define MULTIREG_HAS_SECOND_GC_RET_ONLY(x) x
285 #else // !defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
286 #define MULTIREG_HAS_SECOND_GC_RET 0
287 #define MULTIREG_HAS_SECOND_GC_RET_ONLY_ARG(x)
288 #define MULTIREG_HAS_SECOND_GC_RET_ONLY(x)
289 #endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
290
291 // To get rid of warning 4701 : local variable may be used without being initialized
292 #define DUMMY_INIT(x) (x)
293
294 #define REGEN_SHORTCUTS 0
295 #define REGEN_CALLPAT 0
296
297 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
298 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
299 XX                                                                           XX
300 XX                          jit.h                                            XX
301 XX                                                                           XX
302 XX   Interface of the JIT with jit.cpp                                       XX
303 XX                                                                           XX
304 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
305 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
306 */
307
308 /*****************************************************************************/
309 #if defined(DEBUG)
310 #include "log.h"
311
312 #define INFO6 LL_INFO10000   // Did Jit or Inline succeeded?
313 #define INFO7 LL_INFO100000  // NYI stuff
314 #define INFO8 LL_INFO1000000 // Weird failures
315 #define INFO9 LL_EVERYTHING  // Info about incoming settings
316 #define INFO10 LL_EVERYTHING // Totally verbose
317
318 #endif // DEBUG
319
320 typedef class ICorJitInfo* COMP_HANDLE;
321
322 const CORINFO_CLASS_HANDLE NO_CLASS_HANDLE = (CORINFO_CLASS_HANDLE) nullptr;
323
324 /*****************************************************************************/
325
326 inline bool False()
327 {
328     return false;
329 } // Use to disable code while keeping prefast happy
330
331 // We define two IL offset types, as follows:
332 //
333 // IL_OFFSET:  either a distinguished value, or an IL offset.
334 // IL_OFFSETX: either a distinguished value, or the top two bits are a flags, and the remaining bottom
335 //             bits are a IL offset.
336 //
337 // In both cases, the set of legal distinguished values is:
338 //     BAD_IL_OFFSET             -- A unique illegal IL offset number. Note that it must be different from
339 //                                  the ICorDebugInfo values, below, and must also not be a legal IL offset.
340 //     ICorDebugInfo::NO_MAPPING -- The IL offset corresponds to no source code (such as EH step blocks).
341 //     ICorDebugInfo::PROLOG     -- The IL offset indicates a prolog
342 //     ICorDebugInfo::EPILOG     -- The IL offset indicates an epilog
343 //
344 // The IL offset must be in the range [0 .. 0x3fffffff]. This is because we steal
345 // the top two bits in IL_OFFSETX for flags, but we want the maximum range to be the same
346 // for both types. The IL value can't be larger than the maximum IL offset of the function
347 // being compiled.
348 //
349 // Blocks and statements never store one of the ICorDebugInfo values, even for IL_OFFSETX types. These are
350 // only stored in the IPmappingDsc struct, ipmdILoffsx field.
351
352 typedef unsigned IL_OFFSET;
353
354 const IL_OFFSET BAD_IL_OFFSET = 0x80000000;
355 const IL_OFFSET MAX_IL_OFFSET = 0x3fffffff;
356
357 typedef unsigned IL_OFFSETX;                                 // IL_OFFSET with stack-empty or call-instruction bit
358 const IL_OFFSETX IL_OFFSETX_STKBIT             = 0x80000000; // Note: this bit is set when the stack is NOT empty!
359 const IL_OFFSETX IL_OFFSETX_CALLINSTRUCTIONBIT = 0x40000000; // Set when the IL offset is for a call instruction.
360 const IL_OFFSETX IL_OFFSETX_BITS               = IL_OFFSETX_STKBIT | IL_OFFSETX_CALLINSTRUCTIONBIT;
361
362 IL_OFFSET jitGetILoffs(IL_OFFSETX offsx);
363 IL_OFFSET jitGetILoffsAny(IL_OFFSETX offsx);
364 bool jitIsStackEmpty(IL_OFFSETX offsx);
365 bool jitIsCallInstruction(IL_OFFSETX offsx);
366
367 const unsigned BAD_VAR_NUM = UINT_MAX;
368
369 // Code can't be more than 2^31 in any direction.  This is signed, so it should be used for anything that is
370 // relative to something else.
371 typedef int NATIVE_OFFSET;
372
373 // This is the same as the above, but it's used in absolute contexts (i.e. offset from the start).  Also,
374 // this is used for native code sizes.
375 typedef unsigned UNATIVE_OFFSET;
376
377 typedef ptrdiff_t ssize_t;
378
379 // For the following specially handled FIELD_HANDLES we need
380 //   values that are negative and have the low two bits zero
381 // See eeFindJitDataOffs and eeGetJitDataOffs in Compiler.hpp
382 #define FLD_GLOBAL_DS ((CORINFO_FIELD_HANDLE)-4)
383 #define FLD_GLOBAL_FS ((CORINFO_FIELD_HANDLE)-8)
384
385 /*****************************************************************************/
386
387 #include "vartype.h"
388
389 /*****************************************************************************/
390
391 // Late disassembly is OFF by default. Can be turned ON by
392 // adding /DLATE_DISASM=1 on the command line.
393 // Always OFF in the non-debug version
394
395 #if defined(LATE_DISASM) && (LATE_DISASM == 0)
396 #undef LATE_DISASM
397 #endif
398
399 /*****************************************************************************/
400
401 /*****************************************************************************/
402
403 #define FEATURE_VALNUM_CSE 1 // enable the Value Number CSE optimization logic
404
405 // true if Value Number CSE is enabled
406 #define FEATURE_ANYCSE FEATURE_VALNUM_CSE
407
408 #define CSE_INTO_HANDLERS 0
409
410 #define LARGE_EXPSET 1   // Track 64 or 32 assertions/copies/consts/rangechecks
411 #define ASSERTION_PROP 1 // Enable value/assertion propagation
412
413 #define LOCAL_ASSERTION_PROP ASSERTION_PROP // Enable local assertion propagation
414
415 //=============================================================================
416
417 #define OPT_BOOL_OPS 1 // optimize boolean operations
418
419 //=============================================================================
420
421 #define REDUNDANT_LOAD 1      // track locals in regs, suppress loads
422 #define STACK_PROBES 0        // Support for stack probes
423 #define DUMP_FLOWGRAPHS DEBUG // Support for creating Xml Flowgraph reports in *.fgx files
424
425 #define HANDLER_ENTRY_MUST_BE_IN_HOT_SECTION 1 // if 1 we must have all handler entry points in the Hot code section
426
427 /*****************************************************************************/
428
429 #define VPTR_OFFS 0 // offset of vtable pointer from obj ptr
430
431 /*****************************************************************************/
432
433 #define DUMP_GC_TABLES DEBUG
434 #define VERIFY_GC_TABLES 0
435 #define REARRANGE_ADDS 1
436
437 #define FUNC_INFO_LOGGING 1 // Support dumping function info to a file. In retail, only NYIs, with no function name,
438                             // are dumped.
439
440 /*****************************************************************************/
441 /*****************************************************************************/
442 /* Set these to 1 to collect and output various statistics about the JIT */
443
444 #define CALL_ARG_STATS 0      // Collect stats about calls and call arguments.
445 #define COUNT_BASIC_BLOCKS 0  // Create a histogram of basic block sizes, and a histogram of IL sizes in the simple
446                               // case of single block methods.
447 #define COUNT_LOOPS 0         // Collect stats about loops, such as the total number of natural loops, a histogram of
448                               // the number of loop exits, etc.
449 #define COUNT_RANGECHECKS 0   // Count range checks removed (in lexical CSE?).
450 #define DATAFLOW_ITER 0       // Count iterations in lexical CSE and constant folding dataflow.
451 #define DISPLAY_SIZES 0       // Display generated code, data, and GC information sizes.
452 #define MEASURE_BLOCK_SIZE 0  // Collect stats about basic block and flowList node sizes and memory allocations.
453 #define MEASURE_FATAL 0       // Count the number of calls to fatal(), including NYIs and noway_asserts.
454 #define MEASURE_NODE_SIZE 0   // Collect stats about GenTree node allocations.
455 #define MEASURE_PTRTAB_SIZE 0 // Collect stats about GC pointer table allocations.
456 #define EMITTER_STATS 0       // Collect stats on the emitter.
457 #define NODEBASH_STATS 0      // Collect stats on changed gtOper values in GenTree's.
458 #define COUNT_AST_OPERS 0     // Display use counts for GenTree operators.
459
460 #define VERBOSE_SIZES 0  // Always display GC info sizes. If set, DISPLAY_SIZES must also be set.
461 #define VERBOSE_VERIFY 0 // Dump additional information when verifying code. Useful to debug verification bugs.
462
463 #ifdef DEBUG
464 #define MEASURE_MEM_ALLOC 1 // Collect memory allocation stats.
465 #define LOOP_HOIST_STATS 1  // Collect loop hoisting stats.
466 #define TRACK_LSRA_STATS 1  // Collect LSRA stats
467 #else
468 #define MEASURE_MEM_ALLOC 0 // You can set this to 1 to get memory stats in retail, as well
469 #define LOOP_HOIST_STATS 0  // You can set this to 1 to get loop hoist stats in retail, as well
470 #define TRACK_LSRA_STATS 0  // You can set this to 1 to get LSRA stats in retail, as well
471 #endif
472
473 // Timing calls to clr.dll is only available under certain conditions.
474 #ifndef FEATURE_JIT_METHOD_PERF
475 #define MEASURE_CLRAPI_CALLS 0 // Can't time these calls without METHOD_PERF.
476 #endif
477 #ifdef DEBUG
478 #define MEASURE_CLRAPI_CALLS 0 // No point in measuring DEBUG code.
479 #endif
480 #if !defined(_HOST_X86_) && !defined(_HOST_AMD64_)
481 #define MEASURE_CLRAPI_CALLS 0 // Cycle counters only hooked up on x86/x64.
482 #endif
483 #if !defined(_MSC_VER) && !defined(__clang__)
484 #define MEASURE_CLRAPI_CALLS 0 // Only know how to do this with VC and Clang.
485 #endif
486
487 // If none of the above set the flag to 0, it's available.
488 #ifndef MEASURE_CLRAPI_CALLS
489 #define MEASURE_CLRAPI_CALLS 0 // Set to 1 to measure time in ICorJitInfo calls.
490 #endif
491
492 /*****************************************************************************/
493 /* Portability Defines */
494 /*****************************************************************************/
495 #ifdef _TARGET_X86_
496 #define JIT32_GCENCODER
497 #endif
498
499 /*****************************************************************************/
500 #ifdef DEBUG
501 /*****************************************************************************/
502
503 #define DUMPER
504
505 #else // !DEBUG
506
507 #if DUMP_GC_TABLES
508 #pragma message("NOTE: this non-debug build has GC ptr table dumping always enabled!")
509 const bool dspGCtbls = true;
510 #endif
511
512 /*****************************************************************************/
513 #endif // !DEBUG
514
515 #ifdef DEBUG
516 #define JITDUMP(...)                                                                                                   \
517     {                                                                                                                  \
518         if (JitTls::GetCompiler()->verbose)                                                                            \
519             logf(__VA_ARGS__);                                                                                         \
520     }
521 #define JITLOG(x)                                                                                                      \
522     {                                                                                                                  \
523         JitLogEE x;                                                                                                    \
524     }
525 #define JITLOG_THIS(t, x)                                                                                              \
526     {                                                                                                                  \
527         (t)->JitLogEE x;                                                                                               \
528     }
529 #define DBEXEC(flg, expr)                                                                                              \
530     if (flg)                                                                                                           \
531     {                                                                                                                  \
532         expr;                                                                                                          \
533     }
534 #define DISPNODE(t)                                                                                                    \
535     if (JitTls::GetCompiler()->verbose)                                                                                \
536         JitTls::GetCompiler()->gtDispTree(t, nullptr, nullptr, true);
537 #define DISPTREE(t)                                                                                                    \
538     if (JitTls::GetCompiler()->verbose)                                                                                \
539         JitTls::GetCompiler()->gtDispTree(t);
540 #define DISPRANGE(range)                                                                                               \
541     if (JitTls::GetCompiler()->verbose)                                                                                \
542         JitTls::GetCompiler()->gtDispRange(range);
543 #define DISPTREERANGE(range, t)                                                                                        \
544     if (JitTls::GetCompiler()->verbose)                                                                                \
545         JitTls::GetCompiler()->gtDispTreeRange(range, t);
546 #define VERBOSE JitTls::GetCompiler()->verbose
547 #else // !DEBUG
548 #define JITDUMP(...)
549 #define JITLOG(x)
550 #define JITLOG_THIS(t, x)
551 #define DBEXEC(flg, expr)
552 #define DISPNODE(t)
553 #define DISPTREE(t)
554 #define DISPRANGE(range)
555 #define DISPTREERANGE(range, t)
556 #define VERBOSE 0
557 #endif // !DEBUG
558
559 /*****************************************************************************
560  *
561  * Double alignment. This aligns ESP to 0 mod 8 in function prolog, then uses ESP
562  * to reference locals, EBP to reference parameters.
563  * It only makes sense if frameless method support is on.
564  * (frameless method support is now always on)
565  */
566
567 #ifdef _TARGET_X86_
568 #define DOUBLE_ALIGN 1 // permit the double alignment of ESP in prolog,
569                        //  and permit the double alignment of local offsets
570 #else
571 #define DOUBLE_ALIGN 0 // no special handling for double alignment
572 #endif
573 /*****************************************************************************/
574 #ifdef DEBUG
575 extern void _cdecl debugStop(const char* why, ...);
576 #endif
577 /*****************************************************************************/
578
579 #ifdef DEBUG
580
581 struct JitOptions
582 {
583     const char* methodName; // Method to display output for
584     const char* className;  // Class  to display output for
585
586     double   CGknob;   // Tweakable knob for testing
587     unsigned testMask; // Tweakable mask for testing
588
589     JitOptions* lastDummyField; // Ensures instantiation uses right order of arguments
590 };
591
592 extern JitOptions jitOpts;
593
594 // Forward declarations for UninitializedWord and IsUninitialized are needed by alloc.h
595 template <typename T>
596 inline T UninitializedWord(Compiler* comp);
597
598 template <typename T>
599 inline bool IsUninitialized(T data);
600
601 #endif // DEBUG
602
603 /*****************************************************************************/
604
605 enum accessLevel
606 {
607     ACL_NONE,
608     ACL_PRIVATE,
609     ACL_DEFAULT,
610     ACL_PROTECTED,
611     ACL_PUBLIC,
612 };
613
614 /*****************************************************************************/
615
616 #define castto(var, typ) (*(typ*)&var)
617
618 #define sizeto(typ, mem) (offsetof(typ, mem) + sizeof(((typ*)0)->mem))
619
620 /*****************************************************************************/
621
622 #ifdef NO_MISALIGNED_ACCESS
623
624 #define MISALIGNED_RD_I2(src) (*castto(src, char*) | *castto(src + 1, char*) << 8)
625
626 #define MISALIGNED_RD_U2(src) (*castto(src, char*) | *castto(src + 1, char*) << 8)
627
628 #define MISALIGNED_WR_I2(dst, val)                                                                                     \
629     *castto(dst, char*)     = val;                                                                                     \
630     *castto(dst + 1, char*) = val >> 8;
631
632 #define MISALIGNED_WR_I4(dst, val)                                                                                     \
633     *castto(dst, char*)     = val;                                                                                     \
634     *castto(dst + 1, char*) = val >> 8;                                                                                \
635     *castto(dst + 2, char*) = val >> 16;                                                                               \
636     *castto(dst + 3, char*) = val >> 24;
637
638 #else
639
640 #define MISALIGNED_RD_I2(src) (*castto(src, short*))
641 #define MISALIGNED_RD_U2(src) (*castto(src, unsigned short*))
642
643 #define MISALIGNED_WR_I2(dst, val) *castto(dst, short*) = val;
644 #define MISALIGNED_WR_I4(dst, val) *castto(dst, int*)   = val;
645
646 #define MISALIGNED_WR_ST(dst, val) *castto(dst, ssize_t*) = val;
647
648 #endif
649
650 /*****************************************************************************/
651
652 inline size_t roundUp(size_t size, size_t mult = sizeof(size_t))
653 {
654     assert(mult && ((mult & (mult - 1)) == 0)); // power of two test
655
656     return (size + (mult - 1)) & ~(mult - 1);
657 }
658
659 inline size_t roundDn(size_t size, size_t mult = sizeof(size_t))
660 {
661     assert(mult && ((mult & (mult - 1)) == 0)); // power of two test
662
663     return (size) & ~(mult - 1);
664 }
665
666 inline unsigned int unsigned_abs(int x)
667 {
668     return ((unsigned int)abs(x));
669 }
670
671 #ifdef _TARGET_64BIT_
672 inline size_t unsigned_abs(ssize_t x)
673 {
674     return ((size_t)abs(x));
675 }
676 #endif // _TARGET_64BIT_
677
678 /*****************************************************************************/
679
680 #if CALL_ARG_STATS || COUNT_BASIC_BLOCKS || COUNT_LOOPS || EMITTER_STATS || MEASURE_NODE_SIZE || MEASURE_MEM_ALLOC
681
682 #define HISTOGRAM_MAX_SIZE_COUNT 64
683
684 class Histogram
685 {
686 public:
687     Histogram(const unsigned* const sizeTable);
688
689     void dump(FILE* output);
690     void record(unsigned size);
691
692 private:
693     void ensureAllocated();
694
695     unsigned              m_sizeCount;
696     const unsigned* const m_sizeTable;
697     unsigned              m_counts[HISTOGRAM_MAX_SIZE_COUNT];
698 };
699
700 #endif // CALL_ARG_STATS || COUNT_BASIC_BLOCKS || COUNT_LOOPS || EMITTER_STATS || MEASURE_NODE_SIZE
701
702 /*****************************************************************************/
703 #ifdef ICECAP
704 #include "icapexp.h"
705 #include "icapctrl.h"
706 #endif
707
708 /*****************************************************************************/
709
710 #include "error.h"
711
712 /*****************************************************************************/
713
714 #if CHECK_STRUCT_PADDING
715 #pragma warning(push)
716 #pragma warning(default : 4820) // 'bytes' bytes padding added after construct 'member_name'
717 #endif                          // CHECK_STRUCT_PADDING
718 #include "alloc.h"
719 #include "target.h"
720
721 #if FEATURE_TAILCALL_OPT
722
723 #ifdef FEATURE_CORECLR
724 // CoreCLR - enable tail call opt for the following IL pattern
725 //
726 //     call someFunc
727 //     jmp/jcc RetBlock
728 //     ...
729 //  RetBlock:
730 //     ret
731 #define FEATURE_TAILCALL_OPT_SHARED_RETURN 1
732 #else
733 // Desktop: Keep this to zero as one of app-compat apps that is using GetCallingAssembly()
734 // has an issue turning this ON.
735 //
736 // Refer to TF: Bug: 824625 and its associated regression TF Bug: 1113265
737 #define FEATURE_TAILCALL_OPT_SHARED_RETURN 0
738 #endif // FEATURE_CORECLR
739
740 #else // !FEATURE_TAILCALL_OPT
741 #define FEATURE_TAILCALL_OPT_SHARED_RETURN 0
742 #endif // !FEATURE_TAILCALL_OPT
743
744 #define CLFLG_CODESIZE 0x00001
745 #define CLFLG_CODESPEED 0x00002
746 #define CLFLG_CSE 0x00004
747 #define CLFLG_REGVAR 0x00008
748 #define CLFLG_RNGCHKOPT 0x00010
749 #define CLFLG_DEADASGN 0x00020
750 #define CLFLG_CODEMOTION 0x00040
751 #define CLFLG_QMARK 0x00080
752 #define CLFLG_TREETRANS 0x00100
753 #define CLFLG_INLINING 0x00200
754 #define CLFLG_CONSTANTFOLD 0x00800
755
756 #if FEATURE_STRUCTPROMOTE
757 #define CLFLG_STRUCTPROMOTE 0x00400
758 #else
759 #define CLFLG_STRUCTPROMOTE 0x00000
760 #endif
761
762 #define CLFLG_MAXOPT                                                                                                   \
763     (CLFLG_CSE | CLFLG_REGVAR | CLFLG_RNGCHKOPT | CLFLG_DEADASGN | CLFLG_CODEMOTION | CLFLG_QMARK | CLFLG_TREETRANS |  \
764      CLFLG_INLINING | CLFLG_STRUCTPROMOTE | CLFLG_CONSTANTFOLD)
765
766 #define CLFLG_MINOPT (CLFLG_TREETRANS)
767
768 #define JIT_RESERVED_STACK 64 // Reserved for arguments of calls and hidden
769                               // pushes for finallys so that we don't
770                               // probe on every call site. See comment in
771                               // for CORINFO_STACKPROBE_DEPTH in corjit.h
772
773 /*****************************************************************************/
774
775 extern void dumpILBytes(const BYTE* const codeAddr, unsigned codeSize, unsigned alignSize);
776
777 extern unsigned dumpSingleInstr(const BYTE* const codeAddr, IL_OFFSET offs, const char* prefix = nullptr);
778
779 extern void dumpILRange(const BYTE* const codeAddr, unsigned codeSize); // in bytes
780
781 /*****************************************************************************/
782
783 extern int jitNativeCode(CORINFO_METHOD_HANDLE methodHnd,
784                          CORINFO_MODULE_HANDLE classHnd,
785                          COMP_HANDLE           compHnd,
786                          CORINFO_METHOD_INFO*  methodInfo,
787                          void**                methodCodePtr,
788                          ULONG*                methodCodeSize,
789                          JitFlags*             compileFlags,
790                          void*                 inlineInfoPtr);
791
792 #ifdef _HOST_64BIT_
793 const size_t INVALID_POINTER_VALUE = 0xFEEDFACEABADF00D;
794 #else
795 const size_t INVALID_POINTER_VALUE = 0xFEEDFACE;
796 #endif
797
798 // Constants for making sure size_t fit into smaller types.
799 const size_t MAX_USHORT_SIZE_T   = static_cast<size_t>(static_cast<unsigned short>(-1));
800 const size_t MAX_UNSIGNED_SIZE_T = static_cast<size_t>(static_cast<unsigned>(-1));
801
802 // These assume 2's complement...
803 const int MAX_SHORT_AS_INT = 32767;
804 const int MIN_SHORT_AS_INT = -32768;
805
806 /*****************************************************************************/
807
808 // CompMemKind values are used to tag memory allocations performed via
809 // the compiler's allocator so that the memory usage of various compiler
810 // components can be tracked separately (when MEASURE_MEM_ALLOC is defined).
811
812 enum CompMemKind
813 {
814 #define CompMemKindMacro(kind) CMK_##kind,
815 #include "compmemkind.h"
816     CMK_Count
817 };
818
819 class Compiler;
820
821 // Allows general purpose code (e.g. collection classes) to allocate memory
822 // of a pre-determined kind via the compiler's allocator.
823
824 class CompAllocator
825 {
826     Compiler* const m_comp;
827 #if MEASURE_MEM_ALLOC
828     CompMemKind const m_cmk;
829 #endif
830 public:
831     CompAllocator(Compiler* comp, CompMemKind cmk)
832         : m_comp(comp)
833 #if MEASURE_MEM_ALLOC
834         , m_cmk(cmk)
835 #endif
836     {
837     }
838
839     // Allocates a block of memory at least `sz` in size.
840     // Zero-length allocation are not allowed.
841     inline void* Alloc(size_t sz);
842
843     // Allocates a block of memory at least `elems * elemSize` in size.
844     // Zero-length allocation are not allowed.
845     inline void* ArrayAlloc(size_t elems, size_t elemSize);
846
847     // For the compiler's ArenaAllocator, free operations are no-ops.
848     void Free(void* p)
849     {
850     }
851 };
852
853 // Global operator new overloads that work with CompAllocator
854
855 inline void* __cdecl operator new(size_t n, CompAllocator* alloc)
856 {
857     return alloc->Alloc(n);
858 }
859
860 inline void* __cdecl operator new[](size_t n, CompAllocator* alloc)
861 {
862     return alloc->Alloc(n);
863 }
864
865 // A CompAllocator wrapper that implements IAllocator and allows zero-length
866 // memory allocations (the compiler's ArenAllocator does not support zero-length
867 // allocation).
868
869 class CompIAllocator : public IAllocator
870 {
871     CompAllocator* const m_alloc;
872     char                 m_zeroLenAllocTarg;
873
874 public:
875     CompIAllocator(CompAllocator* alloc) : m_alloc(alloc)
876     {
877     }
878
879     // Allocates a block of memory at least `sz` in size.
880     virtual void* Alloc(size_t sz) override
881     {
882         if (sz == 0)
883         {
884             return &m_zeroLenAllocTarg;
885         }
886         else
887         {
888             return m_alloc->Alloc(sz);
889         }
890     }
891
892     // Allocates a block of memory at least `elems * elemSize` in size.
893     virtual void* ArrayAlloc(size_t elemSize, size_t numElems) override
894     {
895         if ((elemSize == 0) || (numElems == 0))
896         {
897             return &m_zeroLenAllocTarg;
898         }
899         else
900         {
901             return m_alloc->ArrayAlloc(elemSize, numElems);
902         }
903     }
904
905     // Frees the block of memory pointed to by p.
906     virtual void Free(void* p) override
907     {
908         m_alloc->Free(p);
909     }
910 };
911
912 class JitTls
913 {
914 #ifdef DEBUG
915     Compiler* m_compiler;
916     LogEnv    m_logEnv;
917     JitTls*   m_next;
918 #endif
919
920 public:
921     JitTls(ICorJitInfo* jitInfo);
922     ~JitTls();
923
924 #ifdef DEBUG
925     static LogEnv* GetLogEnv();
926 #endif
927
928     static Compiler* GetCompiler();
929     static void SetCompiler(Compiler* compiler);
930 };
931
932 #if defined(DEBUG)
933 //  Include the definition of Compiler for use by these template functions
934 //
935 #include "compiler.h"
936
937 //****************************************************************************
938 //
939 //  Returns a word filled with the JITs allocator default fill value.
940 //
941 template <typename T>
942 inline T UninitializedWord(Compiler* comp)
943 {
944     unsigned char defaultFill = 0xdd;
945     if (comp == nullptr)
946     {
947         comp = JitTls::GetCompiler();
948     }
949     defaultFill = comp->compGetJitDefaultFill();
950     assert(defaultFill <= 0xff);
951     __int64 word = 0x0101010101010101LL * defaultFill;
952     return (T)word;
953 }
954
955 //****************************************************************************
956 //
957 //  Tries to determine if this value is coming from uninitialized JIT memory
958 //    - Returns true if the value matches what we initialized the memory to.
959 //
960 //  Notes:
961 //    - Asserts that use this are assuming that the UninitializedWord value
962 //      isn't a legal value for 'data'.  Thus using a default fill value of
963 //      0x00 will often trigger such asserts.
964 //
965 template <typename T>
966 inline bool IsUninitialized(T data)
967 {
968     return data == UninitializedWord<T>(JitTls::GetCompiler());
969 }
970
971 //****************************************************************************
972 //
973 //  Debug template definitions for dspPtr, dspOffset
974 //    - Used to format pointer/offset values for diffable Disasm
975 //
976 template <typename T>
977 T dspPtr(T p)
978 {
979     return (p == ZERO) ? ZERO : (JitTls::GetCompiler()->opts.dspDiffable ? T(0xD1FFAB1E) : p);
980 }
981
982 template <typename T>
983 T dspOffset(T o)
984 {
985     return (o == ZERO) ? ZERO : (JitTls::GetCompiler()->opts.dspDiffable ? T(0xD1FFAB1E) : o);
986 }
987
988 #else // !defined(DEBUG)
989
990 //****************************************************************************
991 //
992 //  Non-Debug template definitions for dspPtr, dspOffset
993 //    - This is a nop in non-Debug builds
994 //
995 template <typename T>
996 T dspPtr(T p)
997 {
998     return p;
999 }
1000
1001 template <typename T>
1002 T dspOffset(T o)
1003 {
1004     return o;
1005 }
1006
1007 #endif // !defined(DEBUG)
1008
1009 /*****************************************************************************/
1010 #endif //_JIT_H_
1011 /*****************************************************************************/