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