Merge pull request #14350 from CarolEidt/LsraInfoCleanup
[platform/upstream/coreclr.git] / src / jit / emit.cpp
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 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 XX                                                                           XX
8 XX                              emit.cpp                                     XX
9 XX                                                                           XX
10 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
11 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
12 */
13
14 #include "jitpch.h"
15 #ifdef _MSC_VER
16 #pragma hdrstop
17 #endif
18
19 #include "hostallocator.h"
20 #include "instr.h"
21 #include "emit.h"
22 #include "codegen.h"
23
24 /*****************************************************************************
25  *
26  *  Represent an emitter location.
27  */
28
29 void emitLocation::CaptureLocation(emitter* emit)
30 {
31     ig      = emit->emitCurIG;
32     codePos = emit->emitCurOffset();
33
34     assert(Valid());
35 }
36
37 bool emitLocation::IsCurrentLocation(emitter* emit) const
38 {
39     assert(Valid());
40     return (ig == emit->emitCurIG) && (codePos == emit->emitCurOffset());
41 }
42
43 UNATIVE_OFFSET emitLocation::CodeOffset(emitter* emit) const
44 {
45     assert(Valid());
46     return emit->emitCodeOffset(ig, codePos);
47 }
48
49 int emitLocation::GetInsNum() const
50 {
51     return emitGetInsNumFromCodePos(codePos);
52 }
53
54 #ifdef _TARGET_AMD64_
55 // Get the instruction offset in the current instruction group, which must be a funclet prolog group.
56 // This is used to find an instruction offset used in unwind data.
57 // TODO-AMD64-Bug?: We only support a single main function prolog group, but allow for multiple funclet prolog
58 // groups (not that we actually use that flexibility, since the funclet prolog will be small). How to
59 // handle that?
60 UNATIVE_OFFSET emitLocation::GetFuncletPrologOffset(emitter* emit) const
61 {
62     assert(ig->igFuncIdx != 0);
63     assert((ig->igFlags & IGF_FUNCLET_PROLOG) != 0);
64     assert(ig == emit->emitCurIG);
65
66     return emit->emitCurIGsize;
67 }
68 #endif // _TARGET_AMD64_
69
70 #ifdef DEBUG
71 void emitLocation::Print() const
72 {
73     unsigned insNum = emitGetInsNumFromCodePos(codePos);
74     unsigned insOfs = emitGetInsOfsFromCodePos(codePos);
75     printf("(G_M%03u_IG%02u,ins#%d,ofs#%d)", Compiler::s_compMethodsCount, ig->igNum, insNum, insOfs);
76 }
77 #endif // DEBUG
78
79 /*****************************************************************************
80  *
81  *  Return the name of an instruction format.
82  */
83
84 #if defined(DEBUG) || EMITTER_STATS
85
86 const char* emitter::emitIfName(unsigned f)
87 {
88     static const char* const ifNames[] = {
89 #define IF_DEF(en, op1, op2) "IF_" #en,
90 #include "emitfmts.h"
91     };
92
93     static char errBuff[32];
94
95     if (f < sizeof(ifNames) / sizeof(*ifNames))
96     {
97         return ifNames[f];
98     }
99
100     sprintf_s(errBuff, sizeof(errBuff), "??%u??", f);
101     return errBuff;
102 }
103
104 #endif
105
106 #ifdef TRANSLATE_PDB
107
108 /* these are protected */
109
110 AddrMap*  emitter::emitPDBOffsetTable = 0;
111 LocalMap* emitter::emitPDBLocalTable  = 0;
112 bool      emitter::emitIsPDBEnabled   = true;
113 BYTE*     emitter::emitILBaseOfCode   = 0;
114 BYTE*     emitter::emitILMethodBase   = 0;
115 BYTE*     emitter::emitILMethodStart  = 0;
116 BYTE*     emitter::emitImgBaseOfCode  = 0;
117
118 void emitter::MapCode(int ilOffset, BYTE* imgDest)
119 {
120     if (emitIsPDBEnabled)
121     {
122         emitPDBOffsetTable->MapSrcToDest(ilOffset, (int)(imgDest - emitImgBaseOfCode));
123     }
124 }
125
126 void emitter::MapFunc(int                imgOff,
127                       int                procLen,
128                       int                dbgStart,
129                       int                dbgEnd,
130                       short              frameReg,
131                       int                stkAdjust,
132                       int                lvaCount,
133                       OptJit::LclVarDsc* lvaTable,
134                       bool               framePtr)
135 {
136     if (emitIsPDBEnabled)
137     {
138         // this code stores information about local symbols for the PDB translation
139
140         assert(lvaCount >= 0); // don't allow a negative count
141
142         LvaDesc* rgLvaDesc = 0;
143
144         if (lvaCount > 0)
145         {
146             rgLvaDesc = new LvaDesc[lvaCount];
147
148             if (!rgLvaDesc)
149             {
150                 NOMEM();
151             }
152
153             LvaDesc*           pDst = rgLvaDesc;
154             OptJit::LclVarDsc* pSrc = lvaTable;
155             for (int i = 0; i < lvaCount; ++i, ++pDst, ++pSrc)
156             {
157                 pDst->slotNum = pSrc->lvSlotNum;
158                 pDst->isReg   = pSrc->lvRegister;
159                 pDst->reg     = (pSrc->lvRegister ? pSrc->lvRegNum : frameReg);
160                 pDst->off     = pSrc->lvStkOffs + stkAdjust;
161             }
162         }
163
164         emitPDBLocalTable->AddFunc((int)(emitILMethodBase - emitILBaseOfCode), imgOff - (int)emitImgBaseOfCode, procLen,
165                                    dbgStart - imgOff, dbgEnd - imgOff, lvaCount, rgLvaDesc, framePtr);
166         // do not delete rgLvaDesc here -- responsibility is now on emitPDBLocalTable destructor
167     }
168 }
169
170 /* these are public */
171
172 void emitter::SetILBaseOfCode(BYTE* pTextBase)
173 {
174     emitILBaseOfCode = pTextBase;
175 }
176
177 void emitter::SetILMethodBase(BYTE* pMethodEntry)
178 {
179     emitILMethodBase = pMethodEntry;
180 }
181
182 void emitter::SetILMethodStart(BYTE* pMethodCode)
183 {
184     emitILMethodStart = pMethodCode;
185 }
186
187 void emitter::SetImgBaseOfCode(BYTE* pTextBase)
188 {
189     emitImgBaseOfCode = pTextBase;
190 }
191
192 void emitter::SetIDBaseToProlog()
193 {
194     emitInstrDescILBase = (int)(emitILMethodBase - emitILBaseOfCode);
195 }
196
197 void emitter::SetIDBaseToOffset(int methodOffset)
198 {
199     emitInstrDescILBase = methodOffset + (int)(emitILMethodStart - emitILBaseOfCode);
200 }
201
202 void emitter::DisablePDBTranslation()
203 {
204     // this function should disable PDB translation code
205     emitIsPDBEnabled = false;
206 }
207
208 bool emitter::IsPDBEnabled()
209 {
210     return emitIsPDBEnabled;
211 }
212
213 void emitter::InitTranslationMaps(int ilCodeSize)
214 {
215     if (emitIsPDBEnabled)
216     {
217         emitPDBOffsetTable = AddrMap::Create(ilCodeSize);
218         emitPDBLocalTable  = LocalMap::Create();
219     }
220 }
221
222 void emitter::DeleteTranslationMaps()
223 {
224     if (emitPDBOffsetTable)
225     {
226         delete emitPDBOffsetTable;
227         emitPDBOffsetTable = 0;
228     }
229     if (emitPDBLocalTable)
230     {
231         delete emitPDBLocalTable;
232         emitPDBLocalTable = 0;
233     }
234 }
235
236 void emitter::InitTranslator(PDBRewriter* pPDB, int* rgSecMap, IMAGE_SECTION_HEADER** rgpHeader, int numSections)
237 {
238     if (emitIsPDBEnabled)
239     {
240         pPDB->InitMaps(rgSecMap,           // new PE section header order
241                        rgpHeader,          // array of section headers
242                        numSections,        // number of sections
243                        emitPDBOffsetTable, // code offset translation table
244                        emitPDBLocalTable); // slot variable translation table
245     }
246 }
247
248 #endif // TRANSLATE_PDB
249
250 /*****************************************************************************/
251
252 #if EMITTER_STATS
253
254 static unsigned totAllocdSize;
255 static unsigned totActualSize;
256
257 unsigned emitter::emitIFcounts[emitter::IF_COUNT];
258
259 static unsigned  emitSizeBuckets[] = {100, 1024 * 1, 1024 * 2, 1024 * 3, 1024 * 4, 1024 * 5, 1024 * 10, 0};
260 static Histogram emitSizeTable(HostAllocator::getHostAllocator(), emitSizeBuckets);
261
262 static unsigned  GCrefsBuckets[] = {0, 1, 2, 5, 10, 20, 50, 128, 256, 512, 1024, 0};
263 static Histogram GCrefsTable(HostAllocator::getHostAllocator(), GCrefsBuckets);
264
265 static unsigned  stkDepthBuckets[] = {0, 1, 2, 5, 10, 16, 32, 128, 1024, 0};
266 static Histogram stkDepthTable(HostAllocator::getHostAllocator(), stkDepthBuckets);
267
268 size_t emitter::emitSizeMethod;
269
270 size_t   emitter::emitTotMemAlloc;
271 unsigned emitter::emitTotalInsCnt;
272 unsigned emitter::emitTotalIGcnt;
273 unsigned emitter::emitTotalPhIGcnt;
274 unsigned emitter::emitTotalIGjmps;
275 unsigned emitter::emitTotalIGptrs;
276 unsigned emitter::emitTotalIGicnt;
277 size_t   emitter::emitTotalIGsize;
278 unsigned emitter::emitTotalIGmcnt;
279
280 unsigned emitter::emitSmallDspCnt;
281 unsigned emitter::emitLargeDspCnt;
282
283 unsigned emitter::emitSmallCnsCnt;
284 unsigned emitter::emitLargeCnsCnt;
285 unsigned emitter::emitSmallCns[SMALL_CNS_TSZ];
286
287 void emitterStaticStats(FILE* fout)
288 {
289     // insGroup members
290
291     fprintf(fout, "\n");
292     fprintf(fout, "insGroup:\n");
293     fprintf(fout, "Offset of igNext              = %2u\n", offsetof(insGroup, igNext));
294 #ifdef DEBUG
295     fprintf(fout, "Offset of igSelf              = %2u\n", offsetof(insGroup, igSelf));
296 #endif
297     fprintf(fout, "Offset of igNum               = %2u\n", offsetof(insGroup, igNum));
298     fprintf(fout, "Offset of igOffs              = %2u\n", offsetof(insGroup, igOffs));
299     fprintf(fout, "Offset of igFuncIdx           = %2u\n", offsetof(insGroup, igFuncIdx));
300     fprintf(fout, "Offset of igFlags             = %2u\n", offsetof(insGroup, igFlags));
301     fprintf(fout, "Offset of igSize              = %2u\n", offsetof(insGroup, igSize));
302     fprintf(fout, "Offset of igData              = %2u\n", offsetof(insGroup, igData));
303 #if EMIT_TRACK_STACK_DEPTH
304     fprintf(fout, "Offset of igStkLvl            = %2u\n", offsetof(insGroup, igStkLvl));
305 #endif
306     fprintf(fout, "Offset of igGCregs            = %2u\n", offsetof(insGroup, igGCregs));
307     fprintf(fout, "Offset of igInsCnt            = %2u\n", offsetof(insGroup, igInsCnt));
308     fprintf(fout, "Size   of insGroup            = %u\n", sizeof(insGroup));
309
310     // insPlaceholderGroupData members
311
312     fprintf(fout, "\n");
313     fprintf(fout, "insPlaceholderGroupData:\n");
314     fprintf(fout, "Offset of igPhNext                = %2u\n", offsetof(insPlaceholderGroupData, igPhNext));
315     fprintf(fout, "Offset of igPhBB                  = %2u\n", offsetof(insPlaceholderGroupData, igPhBB));
316     fprintf(fout, "Offset of igPhInitGCrefVars       = %2u\n", offsetof(insPlaceholderGroupData, igPhInitGCrefVars));
317     fprintf(fout, "Offset of igPhInitGCrefRegs       = %2u\n", offsetof(insPlaceholderGroupData, igPhInitGCrefRegs));
318     fprintf(fout, "Offset of igPhInitByrefRegs       = %2u\n", offsetof(insPlaceholderGroupData, igPhInitByrefRegs));
319     fprintf(fout, "Offset of igPhPrevGCrefVars       = %2u\n", offsetof(insPlaceholderGroupData, igPhPrevGCrefVars));
320     fprintf(fout, "Offset of igPhPrevGCrefRegs       = %2u\n", offsetof(insPlaceholderGroupData, igPhPrevGCrefRegs));
321     fprintf(fout, "Offset of igPhPrevByrefRegs       = %2u\n", offsetof(insPlaceholderGroupData, igPhPrevByrefRegs));
322     fprintf(fout, "Offset of igPhType                = %2u\n", offsetof(insPlaceholderGroupData, igPhType));
323     fprintf(fout, "Size   of insPlaceholderGroupData = %u\n", sizeof(insPlaceholderGroupData));
324
325     fprintf(fout, "\n");
326     fprintf(fout, "Size   of tinyID      = %2u\n", TINY_IDSC_SIZE);
327     fprintf(fout, "Size   of instrDesc   = %2u\n", sizeof(emitter::instrDesc));
328     // fprintf(fout, "Offset of _idIns      = %2u\n", offsetof(emitter::instrDesc, _idIns      ));
329     // fprintf(fout, "Offset of _idInsFmt   = %2u\n", offsetof(emitter::instrDesc, _idInsFmt   ));
330     // fprintf(fout, "Offset of _idOpSize   = %2u\n", offsetof(emitter::instrDesc, _idOpSize   ));
331     // fprintf(fout, "Offset of idSmallCns  = %2u\n", offsetof(emitter::instrDesc, idSmallCns  ));
332     // fprintf(fout, "Offset of _idAddrUnion= %2u\n", offsetof(emitter::instrDesc, _idAddrUnion));
333     // fprintf(fout, "\n");
334     // fprintf(fout, "Size   of _idAddrUnion= %2u\n", sizeof(((emitter::instrDesc*)0)->_idAddrUnion));
335
336     fprintf(fout, "\n");
337     fprintf(fout, "GCInfo::regPtrDsc:\n");
338     fprintf(fout, "Offset of rpdNext           = %2u\n", offsetof(GCInfo::regPtrDsc, rpdNext));
339     fprintf(fout, "Offset of rpdOffs           = %2u\n", offsetof(GCInfo::regPtrDsc, rpdOffs));
340     fprintf(fout, "Offset of <union>           = %2u\n", offsetof(GCInfo::regPtrDsc, rpdPtrArg));
341     fprintf(fout, "Size   of GCInfo::regPtrDsc = %2u\n", sizeof(GCInfo::regPtrDsc));
342
343     fprintf(fout, "\n");
344 }
345
346 void emitterStats(FILE* fout)
347 {
348     if (totAllocdSize > 0)
349     {
350         assert(totActualSize <= totAllocdSize);
351
352         fprintf(fout, "\nTotal allocated code size = %u\n", totAllocdSize);
353
354         if (totActualSize < totAllocdSize)
355         {
356             fprintf(fout, "Total generated code size = %u  ", totActualSize);
357
358             fprintf(fout, "(%4.3f%% waste)", 100 * ((totAllocdSize - totActualSize) / (double)totActualSize));
359             fprintf(fout, "\n");
360         }
361
362         assert(emitter::emitTotalInsCnt);
363
364         fprintf(fout, "Average of %4.2f bytes of code generated per instruction\n",
365                 (double)totActualSize / emitter::emitTotalInsCnt);
366     }
367
368     fprintf(fout, "\nInstruction format frequency table:\n\n");
369
370     unsigned f, ic = 0, dc = 0;
371
372     for (f = 0; f < emitter::IF_COUNT; f++)
373     {
374         ic += emitter::emitIFcounts[f];
375     }
376
377     for (f = 0; f < emitter::IF_COUNT; f++)
378     {
379         unsigned c = emitter::emitIFcounts[f];
380
381         if ((c > 0) && (1000 * c >= ic))
382         {
383             dc += c;
384             fprintf(fout, "          %-13s %8u (%5.2f%%)\n", emitter::emitIfName(f), c, 100.0 * c / ic);
385         }
386     }
387
388     fprintf(fout, "         --------------------------------\n");
389     fprintf(fout, "          %-13s %8u (%5.2f%%)\n", "Total shown", dc, 100.0 * dc / ic);
390
391     if (emitter::emitTotalIGmcnt)
392     {
393         fprintf(fout, "Total of %8u methods\n", emitter::emitTotalIGmcnt);
394         fprintf(fout, "Total of %8u insGroup\n", emitter::emitTotalIGcnt);
395         fprintf(fout, "Total of %8u insPlaceholderGroupData\n", emitter::emitTotalPhIGcnt);
396         fprintf(fout, "Total of %8u instructions\n", emitter::emitTotalIGicnt);
397         fprintf(fout, "Total of %8u jumps\n", emitter::emitTotalIGjmps);
398         fprintf(fout, "Total of %8u GC livesets\n", emitter::emitTotalIGptrs);
399         fprintf(fout, "\n");
400         fprintf(fout, "Average of %8.1lf insGroup     per method\n",
401                 (double)emitter::emitTotalIGcnt / emitter::emitTotalIGmcnt);
402         fprintf(fout, "Average of %8.1lf insPhGroup   per method\n",
403                 (double)emitter::emitTotalPhIGcnt / emitter::emitTotalIGmcnt);
404         fprintf(fout, "Average of %8.1lf instructions per method\n",
405                 (double)emitter::emitTotalIGicnt / emitter::emitTotalIGmcnt);
406         fprintf(fout, "Average of %8.1lf desc.  bytes per method\n",
407                 (double)emitter::emitTotalIGsize / emitter::emitTotalIGmcnt);
408         fprintf(fout, "Average of %8.1lf jumps        per method\n",
409                 (double)emitter::emitTotalIGjmps / emitter::emitTotalIGmcnt);
410         fprintf(fout, "Average of %8.1lf GC livesets  per method\n",
411                 (double)emitter::emitTotalIGptrs / emitter::emitTotalIGmcnt);
412         fprintf(fout, "\n");
413         fprintf(fout, "Average of %8.1lf instructions per group \n",
414                 (double)emitter::emitTotalIGicnt / emitter::emitTotalIGcnt);
415         fprintf(fout, "Average of %8.1lf desc.  bytes per group \n",
416                 (double)emitter::emitTotalIGsize / emitter::emitTotalIGcnt);
417         fprintf(fout, "Average of %8.1lf jumps        per group \n",
418                 (double)emitter::emitTotalIGjmps / emitter::emitTotalIGcnt);
419         fprintf(fout, "\n");
420         fprintf(fout, "Average of %8.1lf bytes        per instrDesc\n",
421                 (double)emitter::emitTotalIGsize / emitter::emitTotalIGicnt);
422         fprintf(fout, "\n");
423         fprintf(fout, "A total of %8u desc.  bytes\n", emitter::emitTotalIGsize);
424         fprintf(fout, "\n");
425     }
426
427     fprintf(fout, "Descriptor size distribution:\n");
428     emitSizeTable.dump(fout);
429     fprintf(fout, "\n");
430
431     fprintf(fout, "GC ref frame variable counts:\n");
432     GCrefsTable.dump(fout);
433     fprintf(fout, "\n");
434
435     fprintf(fout, "Max. stack depth distribution:\n");
436     stkDepthTable.dump(fout);
437     fprintf(fout, "\n");
438
439     int      i;
440     unsigned c;
441     unsigned m;
442
443     if (emitter::emitSmallCnsCnt || emitter::emitLargeCnsCnt)
444     {
445         fprintf(fout, "SmallCnsCnt = %6u\n", emitter::emitSmallCnsCnt);
446         fprintf(fout, "LargeCnsCnt = %6u (%3u %% of total)\n", emitter::emitLargeCnsCnt,
447                 100 * emitter::emitLargeCnsCnt / (emitter::emitLargeCnsCnt + emitter::emitSmallCnsCnt));
448     }
449
450 #if 0
451     // TODO-Cleanup: WHy is this in #if 0 - Is EMITTER_STATS ever used? Fix or delete this.
452     if  (emitter::emitSmallCnsCnt)
453     {
454         fprintf(fout, "\n");
455
456         m = emitter::emitSmallCnsCnt/1000 + 1;
457
458         for (i = ID_MIN_SMALL_CNS; i < ID_MAX_SMALL_CNS; i++)
459         {
460             c = emitter::emitSmallCns[i-ID_MIN_SMALL_CNS];
461             if  (c >= m)
462                 fprintf(fout, "cns[%4d] = %u\n", i, c);
463         }
464     }
465 #endif // 0
466
467     fprintf(fout, "%8u bytes allocated in the emitter\n", emitter::emitTotMemAlloc);
468 }
469
470 #endif // EMITTER_STATS
471
472 /*****************************************************************************/
473
474 const unsigned short emitTypeSizes[] = {
475 #define DEF_TP(tn, nm, jitType, verType, sz, sze, asze, st, al, tf, howUsed) sze,
476 #include "typelist.h"
477 #undef DEF_TP
478 };
479
480 const unsigned short emitTypeActSz[] = {
481 #define DEF_TP(tn, nm, jitType, verType, sz, sze, asze, st, al, tf, howUsed) asze,
482 #include "typelist.h"
483 #undef DEF_TP
484 };
485
486 /*****************************************************************************/
487 /*****************************************************************************
488  *
489  *  Initialize the emitter - called once, at DLL load time.
490  */
491
492 void emitter::emitInit()
493 {
494 }
495
496 /*****************************************************************************
497  *
498  *  Shut down the emitter - called once, at DLL exit time.
499  */
500
501 void emitter::emitDone()
502 {
503 }
504
505 /*****************************************************************************
506  *
507  *  Allocate memory.
508  */
509
510 void* emitter::emitGetMem(size_t sz)
511 {
512     assert(sz % sizeof(int) == 0);
513
514 #if EMITTER_STATS
515     emitTotMemAlloc += sz;
516 #endif
517
518     return emitComp->compGetMem(sz, CMK_InstDesc);
519 }
520
521 /*****************************************************************************
522  *
523  *  emitLclVarAddr support methods
524  */
525 void emitLclVarAddr::initLclVarAddr(int varNum, unsigned offset)
526 {
527     if (varNum < 32768)
528     {
529         if (varNum >= 0)
530         {
531             if (offset < 32768)
532             {
533                 _lvaTag    = LVA_STANDARD_ENCODING;
534                 _lvaExtra  = offset;           // offset known to be in [0..32767]
535                 _lvaVarNum = (unsigned)varNum; // varNum known to be in [0..32767]
536             }
537             else // offset >= 32768
538             {
539                 // We could support larger local offsets here at the cost of less varNums
540                 if (offset >= 65536)
541                 {
542                     IMPL_LIMITATION("JIT doesn't support offsets larger than 65535 into valuetypes\n");
543                 }
544
545                 _lvaTag    = LVA_LARGE_OFFSET;
546                 _lvaExtra  = (offset - 32768); // (offset-32768) is known to be in [0..32767]
547                 _lvaVarNum = (unsigned)varNum; // varNum known to be in [0..32767]
548             }
549         }
550         else // varNum < 0, These are used for Compiler spill temps
551         {
552             if (varNum < -32767)
553             {
554                 IMPL_LIMITATION("JIT doesn't support more than 32767 Compiler Spill temps\n");
555             }
556             if (offset > 32767)
557             {
558                 IMPL_LIMITATION(
559                     "JIT doesn't support offsets larger than 32767 into valuetypes for Compiler Spill temps\n");
560             }
561
562             _lvaTag    = LVA_COMPILER_TEMP;
563             _lvaExtra  = offset;              //  offset known to be in [0..32767]
564             _lvaVarNum = (unsigned)(-varNum); // -varNum known to be in [1..32767]
565         }
566     }
567     else // varNum >= 32768
568     {
569         if (offset >= 256)
570         {
571             IMPL_LIMITATION("JIT doesn't support offsets larger than 255 into valuetypes for local vars > 32767\n");
572         }
573         if (varNum >= 0x00400000)
574         { // 0x00400000 == 2^22
575             IMPL_LIMITATION("JIT doesn't support more than 2^22 variables\n");
576         }
577
578         _lvaTag    = LVA_LARGE_VARNUM;
579         _lvaVarNum = varNum & 0x00007FFF;         // varNum bits 14 to 0
580         _lvaExtra  = (varNum & 0x003F8000) >> 15; // varNum bits 21 to 15 in _lvaExtra bits  6 to 0, 7 bits total
581         _lvaExtra |= (offset << 7);               // offset bits  7 to 0  in _lvaExtra bits 14 to 7, 8 bits total
582     }
583 }
584
585 // Returns the variable to access. Note that it returns a negative number for compiler spill temps.
586 int emitLclVarAddr::lvaVarNum()
587 {
588     switch (_lvaTag)
589     {
590         case LVA_COMPILER_TEMP:
591             return -((int)_lvaVarNum);
592         case LVA_LARGE_VARNUM:
593             return (int)(((_lvaExtra & 0x007F) << 15) + _lvaVarNum);
594         default: // LVA_STANDARD_ENCODING or LVA_LARGE_OFFSET
595             assert((_lvaTag == LVA_STANDARD_ENCODING) || (_lvaTag == LVA_LARGE_OFFSET));
596             return (int)_lvaVarNum;
597     }
598 }
599
600 unsigned emitLclVarAddr::lvaOffset() // returns the offset into the variable to access
601 {
602     switch (_lvaTag)
603     {
604         case LVA_LARGE_OFFSET:
605             return (32768 + _lvaExtra);
606         case LVA_LARGE_VARNUM:
607             return (_lvaExtra & 0x7F80) >> 7;
608         default: // LVA_STANDARD_ENCODING or LVA_COMPILER_TEMP
609             assert((_lvaTag == LVA_STANDARD_ENCODING) || (_lvaTag == LVA_COMPILER_TEMP));
610             return _lvaExtra;
611     }
612 }
613
614 /*****************************************************************************
615  *
616  *  Record some info about the method about to be emitted.
617  */
618
619 void emitter::emitBegCG(Compiler* comp, COMP_HANDLE cmpHandle)
620 {
621     emitComp      = comp;
622     emitCmpHandle = cmpHandle;
623 }
624
625 void emitter::emitEndCG()
626 {
627 }
628
629 /*****************************************************************************
630  *
631  *  Prepare the given IG for emission of code.
632  */
633
634 void emitter::emitGenIG(insGroup* ig)
635 {
636     /* Set the "current IG" value */
637
638     emitCurIG = ig;
639
640 #if EMIT_TRACK_STACK_DEPTH
641
642     /* Record the stack level on entry to this group */
643
644     ig->igStkLvl = emitCurStackLvl;
645
646     // If we don't have enough bits in igStkLvl, refuse to compile
647
648     if (ig->igStkLvl != emitCurStackLvl)
649     {
650         IMPL_LIMITATION("Too many arguments pushed on stack");
651     }
652
653 //  printf("Start IG #%02u [stk=%02u]\n", ig->igNum, emitCurStackLvl);
654
655 #endif
656
657     if (emitNoGCIG)
658     {
659         ig->igFlags |= IGF_NOGCINTERRUPT;
660     }
661
662     /* Prepare to issue instructions */
663
664     emitCurIGinsCnt = 0;
665     emitCurIGsize   = 0;
666
667     assert(emitCurIGjmpList == nullptr);
668
669     /* Allocate the temp instruction buffer if we haven't done so */
670
671     if (emitCurIGfreeBase == nullptr)
672     {
673         emitIGbuffSize    = SC_IG_BUFFER_SIZE;
674         emitCurIGfreeBase = (BYTE*)emitGetMem(emitIGbuffSize);
675     }
676
677     emitCurIGfreeNext = emitCurIGfreeBase;
678     emitCurIGfreeEndp = emitCurIGfreeBase + emitIGbuffSize;
679 }
680
681 /*****************************************************************************
682  *
683  *  Finish and save the current IG.
684  */
685
686 insGroup* emitter::emitSavIG(bool emitAdd)
687 {
688     insGroup* ig;
689     BYTE*     id;
690
691     size_t sz;
692     size_t gs;
693
694     assert(emitCurIGfreeNext <= emitCurIGfreeEndp);
695
696     /* Get hold of the IG descriptor */
697
698     ig = emitCurIG;
699     assert(ig);
700
701     /* Compute how much code we've generated */
702
703     sz = emitCurIGfreeNext - emitCurIGfreeBase;
704
705     /* Compute the total size we need to allocate */
706
707     gs = roundUp(sz);
708
709     /* Do we need space for GC? */
710
711     if (!(ig->igFlags & IGF_EMIT_ADD))
712     {
713         /* Is the initial set of live GC vars different from the previous one? */
714
715         if (emitForceStoreGCState || !VarSetOps::Equal(emitComp, emitPrevGCrefVars, emitInitGCrefVars))
716         {
717             /* Remember that we will have a new set of live GC variables */
718
719             ig->igFlags |= IGF_GC_VARS;
720
721 #if EMITTER_STATS
722             emitTotalIGptrs++;
723 #endif
724
725             /* We'll allocate extra space to record the liveset */
726
727             gs += sizeof(VARSET_TP);
728         }
729
730         /* Is the initial set of live Byref regs different from the previous one? */
731
732         /* Remember that we will have a new set of live GC variables */
733
734         ig->igFlags |= IGF_BYREF_REGS;
735
736         /* We'll allocate extra space (DWORD aligned) to record the GC regs */
737
738         gs += sizeof(int);
739     }
740
741     /* Allocate space for the instructions and optional liveset */
742
743     id = (BYTE*)emitGetMem(gs);
744
745     /* Do we need to store the byref regs */
746
747     if (ig->igFlags & IGF_BYREF_REGS)
748     {
749         /* Record the byref regs in front the of the instructions */
750
751         *castto(id, unsigned*)++ = (unsigned)emitInitByrefRegs;
752     }
753
754     /* Do we need to store the liveset? */
755
756     if (ig->igFlags & IGF_GC_VARS)
757     {
758         /* Record the liveset in front the of the instructions */
759         VarSetOps::AssignNoCopy(emitComp, (*castto(id, VARSET_TP*)), VarSetOps::MakeEmpty(emitComp));
760         VarSetOps::Assign(emitComp, (*castto(id, VARSET_TP*)++), emitInitGCrefVars);
761     }
762
763     /* Record the collected instructions */
764
765     assert((ig->igFlags & IGF_PLACEHOLDER) == 0);
766     ig->igData = id;
767
768     memcpy(id, emitCurIGfreeBase, sz);
769
770 #ifdef DEBUG
771     if (false && emitComp->verbose) // this is not useful in normal dumps (hence it is normally under if (false)
772     {
773         // If there's an error during emission, we may want to connect the post-copy address
774         // of an instrDesc with the pre-copy address (the one that was originally created).  This
775         // printing enables that.
776         printf("copying instruction group from [0x%x..0x%x) to [0x%x..0x%x).\n", dspPtr(emitCurIGfreeBase),
777                dspPtr(emitCurIGfreeBase + sz), dspPtr(id), dspPtr(id + sz));
778     }
779 #endif
780
781     /* Record how many instructions and bytes of code this group contains */
782
783     noway_assert((BYTE)emitCurIGinsCnt == emitCurIGinsCnt);
784     noway_assert((unsigned short)emitCurIGsize == emitCurIGsize);
785
786     ig->igInsCnt = (BYTE)emitCurIGinsCnt;
787     ig->igSize   = (unsigned short)emitCurIGsize;
788     emitCurCodeOffset += emitCurIGsize;
789     assert(IsCodeAligned(emitCurCodeOffset));
790
791 #if EMITTER_STATS
792     emitTotalIGicnt += emitCurIGinsCnt;
793     emitTotalIGsize += sz;
794     emitSizeMethod += sz;
795 #endif
796
797     // printf("Group [%08X]%3u has %2u instructions (%4u bytes at %08X)\n", ig, ig->igNum, emitCurIGinsCnt, sz, id);
798
799     /* Record the live GC register set - if and only if it is not an emitter added block */
800
801     if (!(ig->igFlags & IGF_EMIT_ADD))
802     {
803         ig->igGCregs = (regMaskSmall)emitInitGCrefRegs;
804     }
805
806     if (!emitAdd)
807     {
808         /* Update the previous recorded live GC ref sets, but not if
809            if we are starting an "overflow" buffer. Note that this is
810            only used to determine whether we need to store or not store
811            the GC ref sets for the next IG, which is dependent on exactly
812            what the state of the emitter GC ref sets will be when the
813            next IG is processed in the emitter.
814          */
815
816         VarSetOps::Assign(emitComp, emitPrevGCrefVars, emitThisGCrefVars);
817         emitPrevGCrefRegs = emitThisGCrefRegs;
818         emitPrevByrefRegs = emitThisByrefRegs;
819
820         emitForceStoreGCState = false;
821     }
822
823 #ifdef DEBUG
824     if (emitComp->opts.dspCode)
825     {
826         printf("\n      G_M%03u_IG%02u:", Compiler::s_compMethodsCount, ig->igNum);
827         if (emitComp->verbose)
828         {
829             printf("        ; offs=%06XH, funclet=%02u", ig->igOffs, ig->igFuncIdx);
830         }
831         else
832         {
833             printf("        ; funclet=%02u", ig->igFuncIdx);
834         }
835         printf("\n");
836     }
837 #endif
838
839     /* Did we have any jumps in this group? */
840
841     if (emitCurIGjmpList)
842     {
843         instrDescJmp* list = nullptr;
844         instrDescJmp* last = nullptr;
845
846         /* Move jumps to the global list, update their 'next' links */
847
848         do
849         {
850             /* Grab the jump and remove it from the list */
851
852             instrDescJmp* oj = emitCurIGjmpList;
853             emitCurIGjmpList = oj->idjNext;
854
855             /* Figure out the address of where the jump got copied */
856
857             size_t        of = (BYTE*)oj - emitCurIGfreeBase;
858             instrDescJmp* nj = (instrDescJmp*)(ig->igData + of);
859
860             // printf("Jump moved from %08X to %08X\n", oj, nj);
861             // printf("jmp [%08X] at %08X + %03u\n", nj, ig, nj->idjOffs);
862
863             assert(nj->idjIG == ig);
864             assert(nj->idIns() == oj->idIns());
865             assert(nj->idjNext == oj->idjNext);
866
867             /* Make sure the jumps are correctly ordered */
868
869             assert(last == nullptr || last->idjOffs > nj->idjOffs);
870
871             if (ig->igFlags & IGF_FUNCLET_PROLOG)
872             {
873                 // Our funclet prologs have short jumps, if the prolog would ever have
874                 // long jumps, then we'd have to insert the list in sorted order than
875                 // just append to the emitJumpList.
876                 noway_assert(nj->idjShort);
877                 if (nj->idjShort)
878                 {
879                     continue;
880                 }
881             }
882
883             /* Append the new jump to the list */
884
885             nj->idjNext = list;
886             list        = nj;
887
888             if (last == nullptr)
889             {
890                 last = nj;
891             }
892         } while (emitCurIGjmpList);
893
894         if (last != nullptr)
895         {
896             /* Append the jump(s) from this IG to the global list */
897             bool prologJump = (ig == emitPrologIG);
898             if ((emitJumpList == nullptr) || prologJump)
899             {
900                 last->idjNext = emitJumpList;
901                 emitJumpList  = list;
902             }
903             else
904             {
905                 last->idjNext         = nullptr;
906                 emitJumpLast->idjNext = list;
907             }
908
909             if (!prologJump || (emitJumpLast == nullptr))
910             {
911                 emitJumpLast = last;
912             }
913         }
914     }
915
916     /* Fix the last instruction field */
917
918     if (sz != 0)
919     {
920         assert(emitLastIns != nullptr);
921         assert(emitCurIGfreeBase <= (BYTE*)emitLastIns);
922         assert((BYTE*)emitLastIns < emitCurIGfreeBase + sz);
923         emitLastIns = (instrDesc*)((BYTE*)id + ((BYTE*)emitLastIns - (BYTE*)emitCurIGfreeBase));
924     }
925
926     /* Reset the buffer free pointers */
927
928     emitCurIGfreeNext = emitCurIGfreeBase;
929
930     return ig;
931 }
932
933 #ifdef LEGACY_BACKEND
934 void emitter::emitTmpSizeChanged(unsigned tmpSize)
935 {
936     assert(emitGrowableMaxByteOffs <= SCHAR_MAX);
937
938 #ifdef DEBUG
939     // Workaround for FP code
940     bool bAssert = JitConfig.JitMaxTempAssert() ? true : false;
941
942     if (tmpSize > emitMaxTmpSize && bAssert)
943     {
944         // TODO-Review: We have a known issue involving floating point code and this assert.
945         // The generated code will be ok, This is only a warning.
946         // To not receive this assert again you can set the registry key: JITMaxTempAssert=0.
947         //
948         assert(!"Incorrect max tmp size set.");
949     }
950 #endif
951
952     if (tmpSize <= emitMaxTmpSize)
953         return;
954
955     unsigned change = tmpSize - emitMaxTmpSize;
956
957     /* If we have used a small offset to access a variable, growing the
958        temp size is a problem if we should have used a large offset instead.
959        Detect if such a situation happens and bail */
960
961     if (emitGrowableMaxByteOffs <= SCHAR_MAX && (emitGrowableMaxByteOffs + change) > SCHAR_MAX)
962     {
963 #ifdef DEBUG
964         if (emitComp->verbose)
965             printf("Under-estimated var offset encoding size for ins #%Xh\n", emitMaxByteOffsIdNum);
966 #endif
967         IMPL_LIMITATION("Should have used large offset to access var");
968     }
969
970     emitMaxTmpSize = tmpSize;
971     emitGrowableMaxByteOffs += change;
972 }
973 #endif // LEGACY_BACKEND
974
975 /*****************************************************************************
976  *
977  *  Start generating code to be scheduled; called once per method.
978  */
979
980 void emitter::emitBegFN(bool hasFramePtr
981 #if defined(DEBUG)
982                         ,
983                         bool chkAlign
984 #endif
985 #ifdef LEGACY_BACKEND
986                         ,
987                         unsigned lclSize
988 #endif // LEGACY_BACKEND
989                         ,
990                         unsigned maxTmpSize)
991 {
992     insGroup* ig;
993
994     /* Assume we won't need the temp instruction buffer */
995
996     emitCurIGfreeBase = nullptr;
997     emitIGbuffSize    = 0;
998
999     /* Record stack frame info (the temp size is just an estimate) */
1000
1001     emitHasFramePtr = hasFramePtr;
1002
1003     emitMaxTmpSize = maxTmpSize;
1004
1005 #ifdef LEGACY_BACKEND
1006     emitLclSize             = lclSize;
1007     emitGrowableMaxByteOffs = 0;
1008 #ifdef DEBUG
1009     emitMaxByteOffsIdNum = (unsigned)-1;
1010 #endif // DEBUG
1011 #endif // LEGACY_BACKEND
1012
1013 #ifdef DEBUG
1014     emitChkAlign = chkAlign;
1015 #endif
1016
1017     /* We have no epilogs yet */
1018
1019     emitEpilogSize = 0;
1020     emitEpilogCnt  = 0;
1021
1022 #ifdef _TARGET_XARCH_
1023     emitExitSeqBegLoc.Init();
1024     emitExitSeqSize = INT_MAX;
1025 #endif // _TARGET_XARCH_
1026
1027     emitPlaceholderList = emitPlaceholderLast = nullptr;
1028
1029 #ifdef JIT32_GCENCODER
1030     emitEpilogList = emitEpilogLast = nullptr;
1031 #endif // JIT32_GCENCODER
1032
1033     /* We don't have any jumps */
1034
1035     emitJumpList = emitJumpLast = nullptr;
1036     emitCurIGjmpList            = nullptr;
1037
1038     emitFwdJumps   = false;
1039     emitNoGCIG     = false;
1040     emitForceNewIG = false;
1041
1042     /* We have not recorded any live sets */
1043
1044     assert(VarSetOps::IsEmpty(emitComp, emitThisGCrefVars));
1045     assert(VarSetOps::IsEmpty(emitComp, emitInitGCrefVars));
1046     assert(VarSetOps::IsEmpty(emitComp, emitPrevGCrefVars));
1047     emitThisGCrefRegs = RBM_NONE;
1048     emitInitGCrefRegs = RBM_NONE;
1049     emitPrevGCrefRegs = RBM_NONE;
1050     emitThisByrefRegs = RBM_NONE;
1051     emitInitByrefRegs = RBM_NONE;
1052     emitPrevByrefRegs = RBM_NONE;
1053
1054     emitForceStoreGCState = false;
1055
1056 #ifdef DEBUG
1057
1058     emitIssuing = false;
1059
1060 #endif
1061
1062     /* Assume there will be no GC ref variables */
1063
1064     emitGCrFrameOffsMin = emitGCrFrameOffsMax = emitGCrFrameOffsCnt = 0;
1065 #ifdef DEBUG
1066     emitGCrFrameLiveTab = nullptr;
1067 #endif
1068
1069     /* We have no groups / code at this point */
1070
1071     emitIGlist = emitIGlast = nullptr;
1072
1073     emitCurCodeOffset = 0;
1074     emitFirstColdIG   = nullptr;
1075     emitTotalCodeSize = 0;
1076
1077 #if EMITTER_STATS
1078     emitTotalIGmcnt++;
1079     emitSizeMethod = 0;
1080 #endif
1081
1082     emitInsCount = 0;
1083
1084     /* The stack is empty now */
1085
1086     emitCurStackLvl = 0;
1087
1088 #if EMIT_TRACK_STACK_DEPTH
1089     emitMaxStackDepth = 0;
1090     emitCntStackDepth = sizeof(int);
1091 #endif
1092
1093     /* No data sections have been created */
1094
1095     emitDataSecCur = nullptr;
1096
1097     memset(&emitConsDsc, 0, sizeof(emitConsDsc));
1098
1099 #ifdef PSEUDORANDOM_NOP_INSERTION
1100     // for random NOP insertion
1101
1102     emitEnableRandomNops();
1103     emitComp->info.compRNG.Init(emitComp->info.compChecksum);
1104     emitNextNop           = emitNextRandomNop();
1105     emitInInstrumentation = false;
1106 #endif // PSEUDORANDOM_NOP_INSERTION
1107
1108     /* Create the first IG, it will be used for the prolog */
1109
1110     emitNxtIGnum = 1;
1111
1112     emitPrologIG = emitIGlist = emitIGlast = emitCurIG = ig = emitAllocIG();
1113
1114     emitLastIns = nullptr;
1115
1116     ig->igNext = nullptr;
1117
1118 #ifdef DEBUG
1119     emitScratchSigInfo = nullptr;
1120 #endif // DEBUG
1121
1122     /* Append another group, to start generating the method body */
1123
1124     emitNewIG();
1125 }
1126
1127 #ifdef PSEUDORANDOM_NOP_INSERTION
1128 int emitter::emitNextRandomNop()
1129 {
1130     return emitComp->info.compRNG.Next(1, 9);
1131 }
1132 #endif
1133
1134 /*****************************************************************************
1135  *
1136  *  Done generating code to be scheduled; called once per method.
1137  */
1138
1139 void emitter::emitEndFN()
1140 {
1141 }
1142
1143 // member function iiaIsJitDataOffset for idAddrUnion, defers to Compiler::eeIsJitDataOffs
1144 bool emitter::instrDesc::idAddrUnion::iiaIsJitDataOffset() const
1145 {
1146     return Compiler::eeIsJitDataOffs(iiaFieldHnd);
1147 }
1148
1149 // member function iiaGetJitDataOffset for idAddrUnion, defers to Compiler::eeGetJitDataOffs
1150 int emitter::instrDesc::idAddrUnion::iiaGetJitDataOffset() const
1151 {
1152     assert(iiaIsJitDataOffset());
1153     return Compiler::eeGetJitDataOffs(iiaFieldHnd);
1154 }
1155
1156 void emitter::dispIns(instrDesc* id)
1157 {
1158 #ifdef DEBUG
1159     emitInsSanityCheck(id);
1160
1161     if (emitComp->opts.dspCode)
1162     {
1163         emitDispIns(id, true, false, false);
1164     }
1165
1166 #if EMIT_TRACK_STACK_DEPTH
1167     assert((int)emitCurStackLvl >= 0);
1168 #endif
1169     size_t sz = emitSizeOfInsDsc(id);
1170     assert(id->idDebugOnlyInfo()->idSize == sz);
1171 #endif // DEBUG
1172
1173 #if EMITTER_STATS
1174     emitIFcounts[id->idInsFmt()]++;
1175 #endif
1176 }
1177
1178 void emitter::appendToCurIG(instrDesc* id)
1179 {
1180     emitCurIGsize += id->idCodeSize();
1181 }
1182
1183 /*****************************************************************************
1184  *
1185  *  Display (optionally) an instruction offset.
1186  */
1187
1188 #ifdef DEBUG
1189
1190 void emitter::emitDispInsOffs(unsigned offs, bool doffs)
1191 {
1192     if (doffs)
1193     {
1194         printf("%06X", offs);
1195     }
1196     else
1197     {
1198         printf("      ");
1199     }
1200 }
1201
1202 #endif // DEBUG
1203
1204 #ifdef JIT32_GCENCODER
1205
1206 /*****************************************************************************
1207  *
1208  *  Call the specified function pointer for each epilog block in the current
1209  *  method with the epilog's relative code offset. Returns the sum of the
1210  *  values returned by the callback.
1211  */
1212
1213 size_t emitter::emitGenEpilogLst(size_t (*fp)(void*, unsigned), void* cp)
1214 {
1215     EpilogList* el;
1216     size_t      sz;
1217
1218     for (el = emitEpilogList, sz = 0; el != nullptr; el = el->elNext)
1219     {
1220         assert(el->elLoc.GetIG()->igFlags & IGF_EPILOG);
1221
1222         // The epilog starts at the location recorded in the epilog list.
1223         sz += fp(cp, el->elLoc.CodeOffset(this));
1224     }
1225
1226     return sz;
1227 }
1228
1229 #endif // JIT32_GCENCODER
1230
1231 /*****************************************************************************
1232  *
1233  *  The following series of methods allocates instruction descriptors.
1234  */
1235
1236 void* emitter::emitAllocInstr(size_t sz, emitAttr opsz)
1237 {
1238     instrDesc* id;
1239
1240 #ifdef DEBUG
1241     // Under STRESS_EMITTER, put every instruction in its own instruction group.
1242     // We can't do this for a prolog, epilog, funclet prolog, or funclet epilog,
1243     // because those are generated out of order. We currently have a limitation
1244     // where the jump shortening pass uses the instruction group number to determine
1245     // if something is earlier or later in the code stream. This implies that
1246     // these groups cannot be more than a single instruction group. Note that
1247     // the prolog/epilog placeholder groups ARE generated in order, and are
1248     // re-used. But generating additional groups would not work.
1249     if (emitComp->compStressCompile(Compiler::STRESS_EMITTER, 1) && emitCurIGinsCnt && !emitIGisInProlog(emitCurIG) &&
1250         !emitIGisInEpilog(emitCurIG)
1251 #if FEATURE_EH_FUNCLETS
1252         && !emitIGisInFuncletProlog(emitCurIG) && !emitIGisInFuncletEpilog(emitCurIG)
1253 #endif // FEATURE_EH_FUNCLETS
1254             )
1255     {
1256         emitNxtIG(true);
1257     }
1258 #endif
1259
1260 #ifdef PSEUDORANDOM_NOP_INSERTION
1261     // TODO-ARM-Bug?: PSEUDORANDOM_NOP_INSERTION is not defined for _TARGET_ARM_
1262     //     ARM - This is currently broken on _TARGET_ARM_
1263     //     When nopSize is odd we misalign emitCurIGsize
1264     //
1265     if (!emitComp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_PREJIT) && !emitInInstrumentation &&
1266         !emitIGisInProlog(emitCurIG) && // don't do this in prolog or epilog
1267         !emitIGisInEpilog(emitCurIG) &&
1268         emitRandomNops // sometimes we turn off where exact codegen is needed (pinvoke inline)
1269         )
1270     {
1271         if (emitNextNop == 0)
1272         {
1273             int nopSize           = 4;
1274             emitInInstrumentation = true;
1275             instrDesc* idnop      = emitNewInstr();
1276             emitInInstrumentation = false;
1277             idnop->idInsFmt(IF_NONE);
1278             idnop->idIns(INS_nop);
1279 #if defined(_TARGET_XARCH_)
1280             idnop->idCodeSize(nopSize);
1281 #else
1282 #error "Undefined target for pseudorandom NOP insertion"
1283 #endif
1284
1285             emitCurIGsize += nopSize;
1286             emitNextNop = emitNextRandomNop();
1287         }
1288         else
1289             emitNextNop--;
1290     }
1291 #endif // PSEUDORANDOM_NOP_INSERTION
1292
1293     assert(IsCodeAligned(emitCurIGsize));
1294
1295     /* Make sure we have enough space for the new instruction */
1296
1297     if ((emitCurIGfreeNext + sz >= emitCurIGfreeEndp) || emitForceNewIG)
1298     {
1299         emitNxtIG(true);
1300     }
1301
1302     /* Grab the space for the instruction */
1303
1304     emitLastIns = id = (instrDesc*)emitCurIGfreeNext;
1305     emitCurIGfreeNext += sz;
1306
1307     assert(sz >= sizeof(void*));
1308     memset(id, 0, sz);
1309
1310     // These fields should have been zero-ed by the above
1311     assert(id->idReg1() == regNumber(0));
1312     assert(id->idReg2() == regNumber(0));
1313 #ifdef _TARGET_XARCH_
1314     assert(id->idCodeSize() == 0);
1315 #endif
1316
1317 #if HAS_TINY_DESC
1318     /* Is the second area to be cleared actually present? */
1319     if (sz >= SMALL_IDSC_SIZE)
1320     {
1321         /* Clear the second 4 bytes, or the 'SMALL' part */
1322         *(int*)((BYTE*)id + (SMALL_IDSC_SIZE - sizeof(int))) = 0;
1323
1324         // These fields should have been zero-ed by the above
1325         assert(id->idIsLargeCns() == false);
1326         assert(id->idIsLargeDsp() == false);
1327         assert(id->idIsLargeCall() == false);
1328     }
1329 #endif
1330
1331     // Make sure that idAddrUnion is just a union of various pointer sized things
1332     C_ASSERT(sizeof(CORINFO_FIELD_HANDLE) <= sizeof(void*));
1333     C_ASSERT(sizeof(CORINFO_METHOD_HANDLE) <= sizeof(void*));
1334     C_ASSERT(sizeof(emitter::emitAddrMode) <= sizeof(void*));
1335     C_ASSERT(sizeof(emitLclVarAddr) <= sizeof(void*));
1336     C_ASSERT(sizeof(emitter::instrDesc) == (SMALL_IDSC_SIZE + sizeof(void*)));
1337
1338     emitInsCount++;
1339
1340 #if defined(DEBUG) || defined(LATE_DISASM)
1341     /* In debug mode we clear/set some additional fields */
1342
1343     instrDescDebugInfo* info = (instrDescDebugInfo*)emitGetMem(sizeof(*info));
1344
1345     info->idNum        = emitInsCount;
1346     info->idSize       = sz;
1347     info->idVarRefOffs = 0;
1348     info->idMemCookie  = 0;
1349     info->idClsCookie  = nullptr;
1350 #ifdef TRANSLATE_PDB
1351     info->idilStart = emitInstrDescILBase;
1352 #endif
1353     info->idFinallyCall = false;
1354     info->idCatchRet    = false;
1355     info->idCallSig     = nullptr;
1356
1357     id->idDebugOnlyInfo(info);
1358
1359 #endif // defined(DEBUG) || defined(LATE_DISASM)
1360
1361     /* Store the size and handle the two special values
1362        that indicate GCref and ByRef */
1363
1364     if (EA_IS_GCREF(opsz))
1365     {
1366         /* A special value indicates a GCref pointer value */
1367
1368         id->idGCref(GCT_GCREF);
1369         id->idOpSize(EA_PTRSIZE);
1370     }
1371     else if (EA_IS_BYREF(opsz))
1372     {
1373         /* A special value indicates a Byref pointer value */
1374
1375         id->idGCref(GCT_BYREF);
1376         id->idOpSize(EA_PTRSIZE);
1377     }
1378     else
1379     {
1380         id->idGCref(GCT_NONE);
1381         id->idOpSize(EA_SIZE(opsz));
1382     }
1383
1384     // Amd64: ip-relative addressing is supported even when not generating relocatable ngen code
1385     if (EA_IS_DSP_RELOC(opsz)
1386 #ifndef _TARGET_AMD64_
1387         && emitComp->opts.compReloc
1388 #endif //_TARGET_AMD64_
1389         )
1390     {
1391         /* Mark idInfo()->idDspReloc to remember that the            */
1392         /* address mode has a displacement that is relocatable       */
1393         id->idSetIsDspReloc();
1394     }
1395
1396     if (EA_IS_CNS_RELOC(opsz) && emitComp->opts.compReloc)
1397     {
1398         /* Mark idInfo()->idCnsReloc to remember that the            */
1399         /* instruction has an immediate constant that is relocatable */
1400         id->idSetIsCnsReloc();
1401     }
1402
1403 #if EMITTER_STATS
1404     emitTotalInsCnt++;
1405 #endif
1406
1407     /* Update the instruction count */
1408
1409     emitCurIGinsCnt++;
1410
1411     return id;
1412 }
1413
1414 #ifdef DEBUG
1415
1416 /*****************************************************************************
1417  *
1418  *  Make sure the code offsets of all instruction groups look reasonable.
1419  */
1420 void emitter::emitCheckIGoffsets()
1421 {
1422     insGroup* tempIG;
1423     size_t    offsIG;
1424
1425     for (tempIG = emitIGlist, offsIG = 0; tempIG; tempIG = tempIG->igNext)
1426     {
1427         if (tempIG->igOffs != offsIG)
1428         {
1429             printf("Block #%u has offset %08X, expected %08X\n", tempIG->igNum, tempIG->igOffs, offsIG);
1430             assert(!"bad block offset");
1431         }
1432
1433         offsIG += tempIG->igSize;
1434     }
1435
1436     if (emitTotalCodeSize && emitTotalCodeSize != offsIG)
1437     {
1438         printf("Total code size is %08X, expected %08X\n", emitTotalCodeSize, offsIG);
1439
1440         assert(!"bad total code size");
1441     }
1442 }
1443
1444 #endif // DEBUG
1445
1446 /*****************************************************************************
1447  *
1448  *  Begin generating a method prolog.
1449  */
1450
1451 void emitter::emitBegProlog()
1452 {
1453     assert(emitComp->compGeneratingProlog);
1454
1455 #if EMIT_TRACK_STACK_DEPTH
1456
1457     /* Don't measure stack depth inside the prolog, it's misleading */
1458
1459     emitCntStackDepth = 0;
1460
1461     assert(emitCurStackLvl == 0);
1462
1463 #endif
1464
1465     emitNoGCIG     = true;
1466     emitForceNewIG = false;
1467
1468     /* Switch to the pre-allocated prolog IG */
1469
1470     emitGenIG(emitPrologIG);
1471
1472     /* Nothing is live on entry to the prolog */
1473
1474     // These were initialized to Empty at the start of compilation.
1475     VarSetOps::OldStyleClearD(emitComp, emitInitGCrefVars);
1476     VarSetOps::OldStyleClearD(emitComp, emitPrevGCrefVars);
1477     emitInitGCrefRegs = RBM_NONE;
1478     emitPrevGCrefRegs = RBM_NONE;
1479     emitInitByrefRegs = RBM_NONE;
1480     emitPrevByrefRegs = RBM_NONE;
1481 }
1482
1483 /*****************************************************************************
1484  *
1485  *  Return the code offset of the current location in the prolog.
1486  */
1487
1488 unsigned emitter::emitGetPrologOffsetEstimate()
1489 {
1490     /* For now only allow a single prolog ins group */
1491
1492     assert(emitPrologIG);
1493     assert(emitPrologIG == emitCurIG);
1494
1495     return emitCurIGsize;
1496 }
1497
1498 /*****************************************************************************
1499  *
1500  *  Mark the code offset of the current location as the end of the prolog,
1501  *  so it can be used later to compute the actual size of the prolog.
1502  */
1503
1504 void emitter::emitMarkPrologEnd()
1505 {
1506     assert(emitComp->compGeneratingProlog);
1507
1508     /* For now only allow a single prolog ins group */
1509
1510     assert(emitPrologIG);
1511     assert(emitPrologIG == emitCurIG);
1512
1513     emitPrologEndPos = emitCurOffset();
1514 }
1515
1516 /*****************************************************************************
1517  *
1518  *  Finish generating a method prolog.
1519  */
1520
1521 void emitter::emitEndProlog()
1522 {
1523     assert(emitComp->compGeneratingProlog);
1524
1525     size_t prolSz;
1526
1527     insGroup* tempIG;
1528
1529     emitNoGCIG = false;
1530
1531     /* Save the prolog IG if non-empty or if only one block */
1532
1533     if (emitCurIGnonEmpty() || emitCurIG == emitPrologIG)
1534     {
1535         emitSavIG();
1536     }
1537
1538 #if EMIT_TRACK_STACK_DEPTH
1539     /* Reset the stack depth values */
1540
1541     emitCurStackLvl   = 0;
1542     emitCntStackDepth = sizeof(int);
1543 #endif
1544 }
1545
1546 /*****************************************************************************
1547  *
1548  *  Create a placeholder instruction group to be used by a prolog or epilog,
1549  *  either for the main function, or a funclet.
1550  */
1551
1552 void emitter::emitCreatePlaceholderIG(insGroupPlaceholderType igType,
1553                                       BasicBlock*             igBB,
1554                                       VARSET_VALARG_TP        GCvars,
1555                                       regMaskTP               gcrefRegs,
1556                                       regMaskTP               byrefRegs,
1557                                       bool                    last)
1558 {
1559     assert(igBB != nullptr);
1560
1561     bool emitAdd = false;
1562
1563     if (igType == IGPT_EPILOG
1564 #if FEATURE_EH_FUNCLETS
1565         || igType == IGPT_FUNCLET_EPILOG
1566 #endif // FEATURE_EH_FUNCLETS
1567         )
1568     {
1569 #ifdef _TARGET_AMD64_
1570         emitOutputPreEpilogNOP();
1571 #endif // _TARGET_AMD64_
1572
1573         emitAdd = true;
1574     }
1575
1576     if (emitCurIGnonEmpty())
1577     {
1578         emitNxtIG(emitAdd);
1579     }
1580
1581     /* Update GC tracking for the beginning of the placeholder IG */
1582
1583     if (!emitAdd)
1584     {
1585         VarSetOps::Assign(emitComp, emitThisGCrefVars, GCvars);
1586         VarSetOps::Assign(emitComp, emitInitGCrefVars, GCvars);
1587         emitThisGCrefRegs = emitInitGCrefRegs = gcrefRegs;
1588         emitThisByrefRegs = emitInitByrefRegs = byrefRegs;
1589     }
1590
1591     /* Convert the group to a placeholder group */
1592
1593     insGroup* igPh = emitCurIG;
1594
1595     igPh->igFlags |= IGF_PLACEHOLDER;
1596
1597     /* Note that we might be re-using a previously created but empty IG. In this
1598      * case, we need to make sure any re-used fields, such as igFuncIdx, are correct.
1599      */
1600
1601     igPh->igFuncIdx = emitComp->compCurrFuncIdx;
1602
1603     /* Create a separate block of memory to store placeholder information.
1604      * We could use unions to put some of this into the insGroup itself, but we don't
1605      * want to grow the insGroup, and it's difficult to make sure the
1606      * insGroup fields are getting set and used elsewhere.
1607      */
1608
1609     igPh->igPhData = new (emitComp, CMK_InstDesc) insPlaceholderGroupData;
1610
1611     igPh->igPhData->igPhNext = nullptr;
1612     igPh->igPhData->igPhType = igType;
1613     igPh->igPhData->igPhBB   = igBB;
1614
1615     VarSetOps::AssignNoCopy(emitComp, igPh->igPhData->igPhPrevGCrefVars, VarSetOps::UninitVal());
1616     VarSetOps::Assign(emitComp, igPh->igPhData->igPhPrevGCrefVars, emitPrevGCrefVars);
1617     igPh->igPhData->igPhPrevGCrefRegs = emitPrevGCrefRegs;
1618     igPh->igPhData->igPhPrevByrefRegs = emitPrevByrefRegs;
1619
1620     VarSetOps::AssignNoCopy(emitComp, igPh->igPhData->igPhInitGCrefVars, VarSetOps::UninitVal());
1621     VarSetOps::Assign(emitComp, igPh->igPhData->igPhInitGCrefVars, emitInitGCrefVars);
1622     igPh->igPhData->igPhInitGCrefRegs = emitInitGCrefRegs;
1623     igPh->igPhData->igPhInitByrefRegs = emitInitByrefRegs;
1624
1625 #if EMITTER_STATS
1626     emitTotalPhIGcnt += 1;
1627 #endif
1628
1629     // Mark function prologs and epilogs properly in the igFlags bits. These bits
1630     // will get used and propagated when the placeholder is converted to a non-placeholder
1631     // during prolog/epilog generation.
1632
1633     if (igType == IGPT_EPILOG)
1634     {
1635         igPh->igFlags |= IGF_EPILOG;
1636     }
1637 #if FEATURE_EH_FUNCLETS
1638     else if (igType == IGPT_FUNCLET_PROLOG)
1639     {
1640         igPh->igFlags |= IGF_FUNCLET_PROLOG;
1641     }
1642     else if (igType == IGPT_FUNCLET_EPILOG)
1643     {
1644         igPh->igFlags |= IGF_FUNCLET_EPILOG;
1645     }
1646 #endif // FEATURE_EH_FUNCLETS
1647
1648     /* Link it into the placeholder list */
1649
1650     if (emitPlaceholderList)
1651     {
1652         emitPlaceholderLast->igPhData->igPhNext = igPh;
1653     }
1654     else
1655     {
1656         emitPlaceholderList = igPh;
1657     }
1658
1659     emitPlaceholderLast = igPh;
1660
1661     // Give an estimated size of this placeholder IG and
1662     // increment emitCurCodeOffset since we are not calling emitNewIG()
1663     //
1664     emitCurIGsize += MAX_PLACEHOLDER_IG_SIZE;
1665     emitCurCodeOffset += emitCurIGsize;
1666
1667 #if FEATURE_EH_FUNCLETS
1668     // Add the appropriate IP mapping debugging record for this placeholder
1669     // group. genExitCode() adds the mapping for main function epilogs.
1670     if (emitComp->opts.compDbgInfo)
1671     {
1672         if (igType == IGPT_FUNCLET_PROLOG)
1673         {
1674             codeGen->genIPmappingAdd((IL_OFFSETX)ICorDebugInfo::PROLOG, true);
1675         }
1676         else if (igType == IGPT_FUNCLET_EPILOG)
1677         {
1678             codeGen->genIPmappingAdd((IL_OFFSETX)ICorDebugInfo::EPILOG, true);
1679         }
1680     }
1681 #endif // FEATURE_EH_FUNCLETS
1682
1683     /* Start a new IG if more code follows */
1684
1685     if (last)
1686     {
1687         emitCurIG = nullptr;
1688     }
1689     else
1690     {
1691         if (igType == IGPT_EPILOG
1692 #if FEATURE_EH_FUNCLETS
1693             || igType == IGPT_FUNCLET_EPILOG
1694 #endif // FEATURE_EH_FUNCLETS
1695             )
1696         {
1697             // If this was an epilog, then assume this is the end of any currently in progress
1698             // no-GC region. If a block after the epilog needs to be no-GC, it needs to call
1699             // emitter::emitDisableGC() directly. This behavior is depended upon by the fast
1700             // tailcall implementation, which disables GC at the beginning of argument setup,
1701             // but assumes that after the epilog it will be re-enabled.
1702             emitNoGCIG = false;
1703         }
1704
1705         emitNewIG();
1706
1707         // We don't know what the GC ref state will be at the end of the placeholder
1708         // group. So, force the next IG to store all the GC ref state variables;
1709         // don't omit them because emitPrev* is the same as emitInit*, because emitPrev*
1710         // will be inaccurate. (Note that, currently, GCrefRegs and ByrefRegs are always
1711         // saved anyway.)
1712         //
1713         // There is no need to re-initialize the emitPrev* variables, as they won't be used
1714         // with emitForceStoreGCState==true, and will be re-initialized just before
1715         // emitForceStoreGCState is set to false;
1716
1717         emitForceStoreGCState = true;
1718
1719         /* The group after the placeholder group doesn't get the "propagate" flags */
1720
1721         emitCurIG->igFlags &= ~IGF_PROPAGATE_MASK;
1722     }
1723
1724 #ifdef DEBUG
1725     if (emitComp->verbose)
1726     {
1727         printf("*************** After placeholder IG creation\n");
1728         emitDispIGlist(false);
1729     }
1730 #endif
1731 }
1732
1733 /*****************************************************************************
1734  *
1735  *  Generate all prologs and epilogs
1736  */
1737
1738 void emitter::emitGeneratePrologEpilog()
1739 {
1740 #ifdef DEBUG
1741     unsigned prologCnt = 0;
1742     unsigned epilogCnt = 0;
1743 #if FEATURE_EH_FUNCLETS
1744     unsigned funcletPrologCnt = 0;
1745     unsigned funcletEpilogCnt = 0;
1746 #endif // FEATURE_EH_FUNCLETS
1747 #endif // DEBUG
1748
1749     insGroup* igPh;
1750     insGroup* igPhNext;
1751
1752     // Generating the prolog/epilog is going to destroy the placeholder group,
1753     // so save the "next" pointer before that happens.
1754
1755     for (igPh = emitPlaceholderList; igPh != nullptr; igPh = igPhNext)
1756     {
1757         assert(igPh->igFlags & IGF_PLACEHOLDER);
1758
1759         igPhNext = igPh->igPhData->igPhNext;
1760
1761         BasicBlock* igPhBB = igPh->igPhData->igPhBB;
1762
1763         switch (igPh->igPhData->igPhType)
1764         {
1765             case IGPT_PROLOG: // currently unused
1766                 INDEBUG(++prologCnt);
1767                 break;
1768
1769             case IGPT_EPILOG:
1770                 INDEBUG(++epilogCnt);
1771                 emitBegFnEpilog(igPh);
1772                 codeGen->genFnEpilog(igPhBB);
1773                 emitEndFnEpilog();
1774                 break;
1775
1776 #if FEATURE_EH_FUNCLETS
1777
1778             case IGPT_FUNCLET_PROLOG:
1779                 INDEBUG(++funcletPrologCnt);
1780                 emitBegFuncletProlog(igPh);
1781                 codeGen->genFuncletProlog(igPhBB);
1782                 emitEndFuncletProlog();
1783                 break;
1784
1785             case IGPT_FUNCLET_EPILOG:
1786                 INDEBUG(++funcletEpilogCnt);
1787                 emitBegFuncletEpilog(igPh);
1788                 codeGen->genFuncletEpilog();
1789                 emitEndFuncletEpilog();
1790                 break;
1791
1792 #endif // FEATURE_EH_FUNCLETS
1793
1794             default:
1795                 unreached();
1796         }
1797     }
1798
1799 #ifdef DEBUG
1800     if (emitComp->verbose)
1801     {
1802         printf("%d prologs, %d epilogs", prologCnt, epilogCnt);
1803 #if FEATURE_EH_FUNCLETS
1804         printf(", %d funclet prologs, %d funclet epilogs", funcletPrologCnt, funcletEpilogCnt);
1805 #endif // FEATURE_EH_FUNCLETS
1806         printf("\n");
1807
1808 // prolog/epilog code doesn't use this yet
1809 // noway_assert(prologCnt == 1);
1810 // noway_assert(epilogCnt == emitEpilogCnt); // Is this correct?
1811 #if FEATURE_EH_FUNCLETS
1812         assert(funcletPrologCnt == emitComp->ehFuncletCount());
1813 #endif // FEATURE_EH_FUNCLETS
1814     }
1815 #endif // DEBUG
1816 }
1817
1818 /*****************************************************************************
1819  *
1820  *  Begin all prolog and epilog generation
1821  */
1822
1823 void emitter::emitStartPrologEpilogGeneration()
1824 {
1825     /* Save the current IG if it's non-empty */
1826
1827     if (emitCurIGnonEmpty())
1828     {
1829         emitSavIG();
1830     }
1831     else
1832     {
1833         assert(emitCurIG == nullptr);
1834     }
1835 }
1836
1837 /*****************************************************************************
1838  *
1839  *  Finish all prolog and epilog generation
1840  */
1841
1842 void emitter::emitFinishPrologEpilogGeneration()
1843 {
1844     /* Update the offsets of all the blocks */
1845
1846     emitRecomputeIGoffsets();
1847
1848     /* We should not generate any more code after this */
1849
1850     emitCurIG = nullptr;
1851 }
1852
1853 /*****************************************************************************
1854  *
1855  *  Common code for prolog / epilog beginning. Convert the placeholder group to actual code IG,
1856  *  and set it as the current group.
1857  */
1858
1859 void emitter::emitBegPrologEpilog(insGroup* igPh)
1860 {
1861     assert(igPh->igFlags & IGF_PLACEHOLDER);
1862
1863     /* Save the current IG if it's non-empty */
1864
1865     if (emitCurIGnonEmpty())
1866     {
1867         emitSavIG();
1868     }
1869
1870     /* Convert the placeholder group to a normal group.
1871      * We need to be very careful to re-initialize the IG properly.
1872      * It turns out, this means we only need to clear the placeholder bit
1873      * and clear the igPhData field, and emitGenIG() will do the rest,
1874      * since in the placeholder IG we didn't touch anything that is set by emitAllocIG().
1875      */
1876
1877     igPh->igFlags &= ~IGF_PLACEHOLDER;
1878     emitNoGCIG     = true;
1879     emitForceNewIG = false;
1880
1881     /* Set up the GC info that we stored in the placeholder */
1882
1883     VarSetOps::Assign(emitComp, emitPrevGCrefVars, igPh->igPhData->igPhPrevGCrefVars);
1884     emitPrevGCrefRegs = igPh->igPhData->igPhPrevGCrefRegs;
1885     emitPrevByrefRegs = igPh->igPhData->igPhPrevByrefRegs;
1886
1887     VarSetOps::Assign(emitComp, emitThisGCrefVars, igPh->igPhData->igPhInitGCrefVars);
1888     VarSetOps::Assign(emitComp, emitInitGCrefVars, igPh->igPhData->igPhInitGCrefVars);
1889     emitThisGCrefRegs = emitInitGCrefRegs = igPh->igPhData->igPhInitGCrefRegs;
1890     emitThisByrefRegs = emitInitByrefRegs = igPh->igPhData->igPhInitByrefRegs;
1891
1892     igPh->igPhData = nullptr;
1893
1894     /* Create a non-placeholder group pointer that we'll now use */
1895
1896     insGroup* ig = igPh;
1897
1898     /* Set the current function using the function index we stored */
1899
1900     emitComp->funSetCurrentFunc(ig->igFuncIdx);
1901
1902     /* Set the new IG as the place to generate code */
1903
1904     emitGenIG(ig);
1905
1906 #if EMIT_TRACK_STACK_DEPTH
1907
1908     /* Don't measure stack depth inside the prolog / epilog, it's misleading */
1909
1910     emitCntStackDepth = 0;
1911
1912     assert(emitCurStackLvl == 0);
1913
1914 #endif
1915 }
1916
1917 /*****************************************************************************
1918  *
1919  *  Common code for end of prolog / epilog
1920  */
1921
1922 void emitter::emitEndPrologEpilog()
1923 {
1924     emitNoGCIG = false;
1925
1926     /* Save the IG if non-empty */
1927
1928     if (emitCurIGnonEmpty())
1929     {
1930         emitSavIG();
1931     }
1932
1933     assert(emitCurIGsize <= MAX_PLACEHOLDER_IG_SIZE);
1934
1935 #if EMIT_TRACK_STACK_DEPTH
1936     /* Reset the stack depth values */
1937
1938     emitCurStackLvl   = 0;
1939     emitCntStackDepth = sizeof(int);
1940 #endif
1941 }
1942
1943 /*****************************************************************************
1944  *
1945  *  Begin generating a main function epilog.
1946  */
1947
1948 void emitter::emitBegFnEpilog(insGroup* igPh)
1949 {
1950     emitEpilogCnt++;
1951
1952     emitBegPrologEpilog(igPh);
1953
1954 #ifdef JIT32_GCENCODER
1955
1956     EpilogList* el = new (emitComp, CMK_GC) EpilogList();
1957
1958     if (emitEpilogLast != nullptr)
1959     {
1960         emitEpilogLast->elNext = el;
1961     }
1962     else
1963     {
1964         emitEpilogList = el;
1965     }
1966
1967     emitEpilogLast = el;
1968
1969 #endif // JIT32_GCENCODER
1970 }
1971
1972 /*****************************************************************************
1973  *
1974  *  Finish generating a funclet epilog.
1975  */
1976
1977 void emitter::emitEndFnEpilog()
1978 {
1979     emitEndPrologEpilog();
1980
1981 #ifdef JIT32_GCENCODER
1982     assert(emitEpilogLast != nullptr);
1983
1984     UNATIVE_OFFSET epilogBegCodeOffset          = emitEpilogLast->elLoc.CodeOffset(this);
1985     UNATIVE_OFFSET epilogExitSeqStartCodeOffset = emitExitSeqBegLoc.CodeOffset(this);
1986     UNATIVE_OFFSET newSize                      = epilogExitSeqStartCodeOffset - epilogBegCodeOffset;
1987
1988     /* Compute total epilog size */
1989     assert(emitEpilogSize == 0 || emitEpilogSize == newSize); // All epilogs must be identical
1990     emitEpilogSize = newSize;
1991
1992     UNATIVE_OFFSET epilogEndCodeOffset = emitCodeOffset(emitCurIG, emitCurOffset());
1993     assert(epilogExitSeqStartCodeOffset != epilogEndCodeOffset);
1994
1995     newSize = epilogEndCodeOffset - epilogExitSeqStartCodeOffset;
1996     if (newSize < emitExitSeqSize)
1997     {
1998         // We expect either the epilog to be the same every time, or that
1999         // one will be a ret or a ret <n> and others will be a jmp addr or jmp [addr];
2000         // we make the epilogs the minimum of these.  Note that this ONLY works
2001         // because the only instruction is the last one and thus a slight
2002         // underestimation of the epilog size is harmless (since the EIP
2003         // can not be between instructions).
2004         assert(emitEpilogCnt == 1 ||
2005                (emitExitSeqSize - newSize) <= 5 // delta between size of various forms of jmp (size is either 6 or 5)
2006                                                 // and various forms of ret (size is either 1 or 3). The combination can
2007                                                 // be anything been 1 and 5.
2008                );
2009         emitExitSeqSize = newSize;
2010     }
2011 #endif // JIT32_GCENCODER
2012 }
2013
2014 #if FEATURE_EH_FUNCLETS
2015
2016 /*****************************************************************************
2017  *
2018  *  Begin generating a funclet prolog.
2019  */
2020
2021 void emitter::emitBegFuncletProlog(insGroup* igPh)
2022 {
2023     emitBegPrologEpilog(igPh);
2024 }
2025
2026 /*****************************************************************************
2027  *
2028  *  Finish generating a funclet prolog.
2029  */
2030
2031 void emitter::emitEndFuncletProlog()
2032 {
2033     emitEndPrologEpilog();
2034 }
2035
2036 /*****************************************************************************
2037  *
2038  *  Begin generating a funclet epilog.
2039  */
2040
2041 void emitter::emitBegFuncletEpilog(insGroup* igPh)
2042 {
2043     emitBegPrologEpilog(igPh);
2044 }
2045
2046 /*****************************************************************************
2047  *
2048  *  Finish generating a funclet epilog.
2049  */
2050
2051 void emitter::emitEndFuncletEpilog()
2052 {
2053     emitEndPrologEpilog();
2054 }
2055
2056 #endif // FEATURE_EH_FUNCLETS
2057
2058 #ifdef JIT32_GCENCODER
2059
2060 //
2061 // emitter::emitStartEpilog:
2062 //   Mark the current position so that we can later compute the total epilog size.
2063 //
2064 void emitter::emitStartEpilog()
2065 {
2066     assert(emitEpilogLast != nullptr);
2067     emitEpilogLast->elLoc.CaptureLocation(this);
2068 }
2069
2070 /*****************************************************************************
2071  *
2072  *  Return non-zero if the current method only has one epilog, which is
2073  *  at the very end of the method body.
2074  */
2075
2076 bool emitter::emitHasEpilogEnd()
2077 {
2078     if (emitEpilogCnt == 1 && (emitIGlast->igFlags & IGF_EPILOG)) // This wouldn't work for funclets
2079         return true;
2080     else
2081         return false;
2082 }
2083
2084 #endif // JIT32_GCENCODER
2085
2086 #ifdef _TARGET_XARCH_
2087
2088 /*****************************************************************************
2089  *
2090  *  Mark the beginning of the epilog exit sequence by remembering our position.
2091  */
2092
2093 void emitter::emitStartExitSeq()
2094 {
2095     assert(emitComp->compGeneratingEpilog);
2096
2097     emitExitSeqBegLoc.CaptureLocation(this);
2098 }
2099
2100 #endif // _TARGET_XARCH_
2101
2102 /*****************************************************************************
2103  *
2104  *  The code generator tells us the range of GC ref locals through this
2105  *  method. Needless to say, locals and temps should be allocated so that
2106  *  the size of the range is as small as possible.
2107  *
2108  * offsLo - The FP offset from which the GC pointer range starts.
2109  * offsHi - The FP offset at which the GC pointer region ends (exclusive).
2110  */
2111
2112 void emitter::emitSetFrameRangeGCRs(int offsLo, int offsHi)
2113 {
2114     assert(emitComp->compGeneratingProlog);
2115     assert(offsHi > offsLo);
2116
2117 #ifdef DEBUG
2118
2119     //  A total of    47254 methods compiled.
2120     //
2121     //  GC ref frame variable counts:
2122     //
2123     //      <=         0 ===>  43175 count ( 91% of total)
2124     //       1 ..      1 ===>   2367 count ( 96% of total)
2125     //       2 ..      2 ===>    887 count ( 98% of total)
2126     //       3 ..      5 ===>    579 count ( 99% of total)
2127     //       6 ..     10 ===>    141 count ( 99% of total)
2128     //      11 ..     20 ===>     40 count ( 99% of total)
2129     //      21 ..     50 ===>     42 count ( 99% of total)
2130     //      51 ..    128 ===>     15 count ( 99% of total)
2131     //     129 ..    256 ===>      4 count ( 99% of total)
2132     //     257 ..    512 ===>      4 count (100% of total)
2133     //     513 ..   1024 ===>      0 count (100% of total)
2134
2135     if (emitComp->verbose)
2136     {
2137         unsigned count = (offsHi - offsLo) / sizeof(void*);
2138         printf("%u tracked GC refs are at stack offsets ", count);
2139
2140         if (offsLo >= 0)
2141         {
2142             printf(" %04X ...  %04X\n", offsLo, offsHi);
2143             assert(offsHi >= 0);
2144         }
2145         else
2146 #if defined(_TARGET_ARM_) && defined(PROFILING_SUPPORTED)
2147             if (!emitComp->compIsProfilerHookNeeded())
2148 #endif
2149         {
2150 #ifdef _TARGET_AMD64_
2151             // doesn't have to be all negative on amd
2152             printf("-%04X ... %04X\n", -offsLo, offsHi);
2153 #else
2154             printf("-%04X ... -%04X\n", -offsLo, -offsHi);
2155             assert(offsHi <= 0);
2156 #endif
2157         }
2158 #if defined(_TARGET_ARM_) && defined(PROFILING_SUPPORTED)
2159         else
2160         {
2161             // Under profiler due to prespilling of arguments, offHi need not be < 0
2162             if (offsHi < 0)
2163                 printf("-%04X ... -%04X\n", -offsLo, -offsHi);
2164             else
2165                 printf("-%04X ... %04X\n", -offsLo, offsHi);
2166         }
2167 #endif
2168     }
2169
2170 #endif // DEBUG
2171
2172     assert(((offsHi - offsLo) % sizeof(void*)) == 0);
2173     assert((offsLo % sizeof(void*)) == 0);
2174     assert((offsHi % sizeof(void*)) == 0);
2175
2176     emitGCrFrameOffsMin = offsLo;
2177     emitGCrFrameOffsMax = offsHi;
2178     emitGCrFrameOffsCnt = (offsHi - offsLo) / sizeof(void*);
2179 }
2180
2181 /*****************************************************************************
2182  *
2183  *  The code generator tells us the range of local variables through this
2184  *  method.
2185  */
2186
2187 void emitter::emitSetFrameRangeLcls(int offsLo, int offsHi)
2188 {
2189 }
2190
2191 /*****************************************************************************
2192  *
2193  *  The code generator tells us the range of used arguments through this
2194  *  method.
2195  */
2196
2197 void emitter::emitSetFrameRangeArgs(int offsLo, int offsHi)
2198 {
2199 }
2200
2201 /*****************************************************************************
2202  *
2203  *  A conversion table used to map an operand size value (in bytes) into its
2204  *  small encoding (0 through 3), and vice versa.
2205  */
2206
2207 const emitter::opSize emitter::emitSizeEncode[] = {
2208     emitter::OPSZ1, emitter::OPSZ2,  OPSIZE_INVALID, emitter::OPSZ4,  OPSIZE_INVALID, OPSIZE_INVALID, OPSIZE_INVALID,
2209     emitter::OPSZ8, OPSIZE_INVALID,  OPSIZE_INVALID, OPSIZE_INVALID,  OPSIZE_INVALID, OPSIZE_INVALID, OPSIZE_INVALID,
2210     OPSIZE_INVALID, emitter::OPSZ16, OPSIZE_INVALID, OPSIZE_INVALID,  OPSIZE_INVALID, OPSIZE_INVALID, OPSIZE_INVALID,
2211     OPSIZE_INVALID, OPSIZE_INVALID,  OPSIZE_INVALID, OPSIZE_INVALID,  OPSIZE_INVALID, OPSIZE_INVALID, OPSIZE_INVALID,
2212     OPSIZE_INVALID, OPSIZE_INVALID,  OPSIZE_INVALID, emitter::OPSZ32,
2213 };
2214
2215 const emitAttr emitter::emitSizeDecode[emitter::OPSZ_COUNT] = {EA_1BYTE, EA_2BYTE,  EA_4BYTE,
2216                                                                EA_8BYTE, EA_16BYTE, EA_32BYTE};
2217
2218 /*****************************************************************************
2219  *
2220  *  Allocate an instruction descriptor for an instruction that uses both
2221  *  a displacement and a constant.
2222  */
2223
2224 emitter::instrDesc* emitter::emitNewInstrCnsDsp(emitAttr size, ssize_t cns, int dsp)
2225 {
2226     if (dsp == 0)
2227     {
2228         if (instrDesc::fitsInSmallCns(cns))
2229         {
2230             instrDesc* id = emitAllocInstr(size);
2231
2232             id->idSmallCns(cns);
2233
2234 #if EMITTER_STATS
2235             emitSmallCnsCnt++;
2236             emitSmallCns[cns - ID_MIN_SMALL_CNS]++;
2237             emitSmallDspCnt++;
2238 #endif
2239
2240             return id;
2241         }
2242         else
2243         {
2244             instrDescCns* id = emitAllocInstrCns(size);
2245
2246             id->idSetIsLargeCns();
2247             id->idcCnsVal = cns;
2248
2249 #if EMITTER_STATS
2250             emitLargeCnsCnt++;
2251             emitSmallDspCnt++;
2252 #endif
2253
2254             return id;
2255         }
2256     }
2257     else
2258     {
2259         if (instrDesc::fitsInSmallCns(cns))
2260         {
2261             instrDescDsp* id = emitAllocInstrDsp(size);
2262
2263             id->idSetIsLargeDsp();
2264             id->iddDspVal = dsp;
2265
2266             id->idSmallCns(cns);
2267
2268 #if EMITTER_STATS
2269             emitLargeDspCnt++;
2270             emitSmallCnsCnt++;
2271             emitSmallCns[cns - ID_MIN_SMALL_CNS]++;
2272 #endif
2273
2274             return id;
2275         }
2276         else
2277         {
2278             instrDescCnsDsp* id = emitAllocInstrCnsDsp(size);
2279
2280             id->idSetIsLargeCns();
2281             id->iddcCnsVal = cns;
2282
2283             id->idSetIsLargeDsp();
2284             id->iddcDspVal = dsp;
2285
2286 #if EMITTER_STATS
2287             emitLargeDspCnt++;
2288             emitLargeCnsCnt++;
2289 #endif
2290
2291             return id;
2292         }
2293     }
2294 }
2295
2296 /*****************************************************************************
2297  *
2298  *  Returns true if garbage-collection won't happen within the helper call.
2299  *  Don't need to record live pointers for such call sites.
2300  */
2301
2302 bool emitter::emitNoGChelper(unsigned IHX)
2303 {
2304     // TODO-Throughput: Make this faster (maybe via a simple table of bools?)
2305
2306     switch (IHX)
2307     {
2308         case CORINFO_HELP_UNDEF:
2309             return false;
2310
2311         case CORINFO_HELP_PROF_FCN_LEAVE:
2312         case CORINFO_HELP_PROF_FCN_ENTER:
2313 #if defined(_TARGET_AMD64_) || (defined(_TARGET_X86_) && !defined(LEGACY_BACKEND))
2314         case CORINFO_HELP_PROF_FCN_TAILCALL:
2315 #endif
2316         case CORINFO_HELP_LLSH:
2317         case CORINFO_HELP_LRSH:
2318         case CORINFO_HELP_LRSZ:
2319
2320 //  case CORINFO_HELP_LMUL:
2321 //  case CORINFO_HELP_LDIV:
2322 //  case CORINFO_HELP_LMOD:
2323 //  case CORINFO_HELP_ULDIV:
2324 //  case CORINFO_HELP_ULMOD:
2325
2326 #ifdef _TARGET_X86_
2327         case CORINFO_HELP_ASSIGN_REF_EAX:
2328         case CORINFO_HELP_ASSIGN_REF_ECX:
2329         case CORINFO_HELP_ASSIGN_REF_EBX:
2330         case CORINFO_HELP_ASSIGN_REF_EBP:
2331         case CORINFO_HELP_ASSIGN_REF_ESI:
2332         case CORINFO_HELP_ASSIGN_REF_EDI:
2333
2334         case CORINFO_HELP_CHECKED_ASSIGN_REF_EAX:
2335         case CORINFO_HELP_CHECKED_ASSIGN_REF_ECX:
2336         case CORINFO_HELP_CHECKED_ASSIGN_REF_EBX:
2337         case CORINFO_HELP_CHECKED_ASSIGN_REF_EBP:
2338         case CORINFO_HELP_CHECKED_ASSIGN_REF_ESI:
2339         case CORINFO_HELP_CHECKED_ASSIGN_REF_EDI:
2340 #endif
2341
2342         case CORINFO_HELP_ASSIGN_REF:
2343
2344         case CORINFO_HELP_CHECKED_ASSIGN_REF:
2345
2346         case CORINFO_HELP_GETSHARED_GCSTATIC_BASE_NOCTOR:
2347
2348         case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE_NOCTOR:
2349
2350         case CORINFO_HELP_ASSIGN_BYREF:
2351
2352         case CORINFO_HELP_INIT_PINVOKE_FRAME:
2353
2354             return true;
2355     }
2356
2357     return false;
2358 }
2359
2360 /*****************************************************************************
2361  *
2362  *  Mark the current spot as having a label.
2363  */
2364
2365 void* emitter::emitAddLabel(VARSET_VALARG_TP GCvars, regMaskTP gcrefRegs, regMaskTP byrefRegs, BOOL isFinallyTarget)
2366 {
2367     /* Create a new IG if the current one is non-empty */
2368
2369     if (emitCurIGnonEmpty())
2370     {
2371         emitNxtIG();
2372     }
2373
2374     VarSetOps::Assign(emitComp, emitThisGCrefVars, GCvars);
2375     VarSetOps::Assign(emitComp, emitInitGCrefVars, GCvars);
2376     emitThisGCrefRegs = emitInitGCrefRegs = gcrefRegs;
2377     emitThisByrefRegs = emitInitByrefRegs = byrefRegs;
2378
2379 #if FEATURE_EH_FUNCLETS && defined(_TARGET_ARM_)
2380     if (isFinallyTarget)
2381     {
2382         emitCurIG->igFlags |= IGF_FINALLY_TARGET;
2383     }
2384 #endif // FEATURE_EH_FUNCLETS && defined(_TARGET_ARM_)
2385
2386 #ifdef DEBUG
2387     if (EMIT_GC_VERBOSE)
2388     {
2389         printf("Label: IG%02u, GCvars=%s ", emitCurIG->igNum, VarSetOps::ToString(emitComp, GCvars));
2390         dumpConvertedVarSet(emitComp, GCvars);
2391         printf(", gcrefRegs=");
2392         printRegMaskInt(gcrefRegs);
2393         emitDispRegSet(gcrefRegs);
2394         printf(", byrefRegs=");
2395         printRegMaskInt(byrefRegs);
2396         emitDispRegSet(byrefRegs);
2397         printf("\n");
2398     }
2399 #endif
2400     return emitCurIG;
2401 }
2402
2403 #ifdef _TARGET_ARMARCH_
2404
2405 // Does the argument location point to an IG at the end of a function or funclet?
2406 // We can ignore the codePos part of the location, since it doesn't affect the
2407 // determination. If 'emitLocNextFragment' is non-NULL, it indicates the first
2408 // IG of the next fragment, so it represents a function end.
2409 bool emitter::emitIsFuncEnd(emitLocation* emitLoc, emitLocation* emitLocNextFragment /* = NULL */)
2410 {
2411     assert(emitLoc);
2412
2413     insGroup* ig = emitLoc->GetIG();
2414     assert(ig);
2415
2416     // Are we at the end of the IG list?
2417     if ((emitLocNextFragment != NULL) && (ig->igNext == emitLocNextFragment->GetIG()))
2418         return true;
2419
2420     // Safety check
2421     if (ig->igNext == NULL)
2422         return true;
2423
2424     // Is the next IG the start of a funclet prolog?
2425     if (ig->igNext->igFlags & IGF_FUNCLET_PROLOG)
2426         return true;
2427
2428 #if FEATURE_EH_FUNCLETS
2429
2430     // Is the next IG a placeholder group for a funclet prolog?
2431     if ((ig->igNext->igFlags & IGF_PLACEHOLDER) && (ig->igNext->igPhData->igPhType == IGPT_FUNCLET_PROLOG))
2432     {
2433         return true;
2434     }
2435
2436 #endif // FEATURE_EH_FUNCLETS
2437
2438     return false;
2439 }
2440
2441 /*****************************************************************************
2442  *
2443  * Split the region from 'startLoc' to 'endLoc' into fragments by calling
2444  * a callback function to indicate the beginning of a fragment. The initial code,
2445  * starting at 'startLoc', doesn't get a callback, but the first code fragment,
2446  * about 'maxSplitSize' bytes out does, as does the beginning of each fragment
2447  * after that. There is no callback for the end (only the beginning of the last
2448  * fragment gets a callback). A fragment must contain at least one instruction
2449  * group. It should be smaller than 'maxSplitSize', although it may be larger to
2450  * satisfy the "at least one instruction group" rule. Do not split prologs or
2451  * epilogs. (Currently, prologs exist in a single instruction group at the main
2452  * function beginning, so they aren't split. Funclets, however, might span IGs,
2453  * so we can't split in between them.)
2454  *
2455  * Note that the locations must be the start of instruction groups; the part of
2456  * the location indicating offset within a group must be zero.
2457  *
2458  * If 'startLoc' is NULL, it means the start of the code.
2459  * If 'endLoc'   is NULL, it means the end   of the code.
2460  */
2461
2462 void emitter::emitSplit(emitLocation*         startLoc,
2463                         emitLocation*         endLoc,
2464                         UNATIVE_OFFSET        maxSplitSize,
2465                         void*                 context,
2466                         emitSplitCallbackType callbackFunc)
2467 {
2468     insGroup*      igStart = (startLoc == NULL) ? emitIGlist : startLoc->GetIG();
2469     insGroup*      igEnd   = (endLoc == NULL) ? NULL : endLoc->GetIG();
2470     insGroup*      igPrev;
2471     insGroup*      ig;
2472     insGroup*      igLastReported;
2473     insGroup*      igLastCandidate;
2474     UNATIVE_OFFSET curSize;
2475     UNATIVE_OFFSET candidateSize;
2476
2477     for (igPrev = NULL, ig = igLastReported = igStart, igLastCandidate = NULL, candidateSize = 0, curSize = 0;
2478          ig != igEnd && ig != NULL; igPrev = ig, ig = ig->igNext)
2479     {
2480         // Keep looking until we've gone past the maximum split size
2481         if (curSize >= maxSplitSize)
2482         {
2483             bool reportCandidate = true;
2484
2485             // Is there a candidate?
2486             if (igLastCandidate == NULL)
2487             {
2488 #ifdef DEBUG
2489                 if (EMITVERBOSE)
2490                     printf("emitSplit: can't split at IG%02u; we don't have a candidate to report\n", ig->igNum);
2491 #endif
2492                 reportCandidate = false;
2493             }
2494
2495             // Don't report the same thing twice (this also happens for the first block, since igLastReported is
2496             // initialized to igStart).
2497             if (igLastCandidate == igLastReported)
2498             {
2499 #ifdef DEBUG
2500                 if (EMITVERBOSE)
2501                     printf("emitSplit: can't split at IG%02u; we already reported it\n", igLastCandidate->igNum);
2502 #endif
2503                 reportCandidate = false;
2504             }
2505
2506             // Report it!
2507             if (reportCandidate)
2508             {
2509 #ifdef DEBUG
2510                 if (EMITVERBOSE && (candidateSize >= maxSplitSize))
2511                     printf("emitSplit: split at IG%02u is size %d, larger than requested maximum size of %d\n",
2512                            igLastCandidate->igNum, candidateSize, maxSplitSize);
2513 #endif
2514
2515                 // hand memory ownership to the callback function
2516                 emitLocation* pEmitLoc = new (emitComp, CMK_Unknown) emitLocation(igLastCandidate);
2517                 callbackFunc(context, pEmitLoc);
2518                 igLastReported  = igLastCandidate;
2519                 igLastCandidate = NULL;
2520                 curSize -= candidateSize;
2521             }
2522         }
2523
2524         // Update the current candidate to be this block, if it isn't in the middle of a
2525         // prolog or epilog, which we can't split. All we know is that certain
2526         // IGs are marked as prolog or epilog. We don't actually know if two adjacent
2527         // IGs are part of the *same* prolog or epilog, so we have to assume they are.
2528
2529         if (igPrev && (((igPrev->igFlags & IGF_FUNCLET_PROLOG) && (ig->igFlags & IGF_FUNCLET_PROLOG)) ||
2530                        ((igPrev->igFlags & IGF_EPILOG) && (ig->igFlags & IGF_EPILOG))))
2531         {
2532             // We can't update the candidate
2533         }
2534         else
2535         {
2536             igLastCandidate = ig;
2537             candidateSize   = curSize;
2538         }
2539
2540         curSize += ig->igSize;
2541
2542     } // end for loop
2543 }
2544
2545 /*****************************************************************************
2546  *
2547  * Given an instruction group, find the array of instructions (instrDesc) and
2548  * number of instructions in the array. If the IG is the current IG, we assume
2549  * that igData does NOT hold the instructions; they are unsaved and pointed
2550  * to by emitCurIGfreeBase.
2551  *
2552  * This function can't be called for placeholder groups, which have no instrDescs.
2553  */
2554
2555 void emitter::emitGetInstrDescs(insGroup* ig, instrDesc** id, int* insCnt)
2556 {
2557     assert(!(ig->igFlags & IGF_PLACEHOLDER));
2558     if (ig == emitCurIG)
2559     {
2560         *id     = (instrDesc*)emitCurIGfreeBase;
2561         *insCnt = emitCurIGinsCnt;
2562     }
2563     else
2564     {
2565         *id     = (instrDesc*)ig->igData;
2566         *insCnt = ig->igInsCnt;
2567     }
2568
2569     assert(*id);
2570 }
2571
2572 /*****************************************************************************
2573  *
2574  * Given a location (an 'emitLocation'), find the instruction group (IG) and
2575  * instruction descriptor (instrDesc) corresponding to that location. Returns
2576  * 'true' if there is an instruction, 'false' if there is no instruction
2577  * (i.e., we're at the end of the instruction list). Also, optionally return
2578  * the number of instructions that follow that instruction in the IG (in *pinsRemaining,
2579  * if pinsRemaining is non-NULL), which can be used for iterating over the
2580  * remaining instrDescs in the IG.
2581  *
2582  * We assume that emitCurIG points to the end of the instructions we care about.
2583  * For the prologs or epilogs, it points to the last IG of the prolog or epilog
2584  * that is being generated. For body code gen, it points to the place we are currently
2585  * adding code, namely, the end of currently generated code.
2586  */
2587
2588 bool emitter::emitGetLocationInfo(emitLocation* emitLoc,
2589                                   insGroup**    pig,
2590                                   instrDesc**   pid,
2591                                   int*          pinsRemaining /* = NULL */)
2592 {
2593     assert(emitLoc != nullptr);
2594     assert(emitLoc->Valid());
2595     assert(emitLoc->GetIG() != nullptr);
2596     assert(pig != nullptr);
2597     assert(pid != nullptr);
2598
2599     insGroup*  ig = emitLoc->GetIG();
2600     instrDesc* id;
2601     int        insNum = emitLoc->GetInsNum();
2602     int        insCnt;
2603
2604     emitGetInstrDescs(ig, &id, &insCnt);
2605     assert(insNum <= insCnt);
2606
2607     // There is a special-case: if the insNum points to the end, then we "wrap" and
2608     // consider that the instruction it is pointing at is actually the first instruction
2609     // of the next non-empty IG (which has its own valid emitLocation). This handles the
2610     // case where you capture a location, then the next instruction creates a new IG.
2611
2612     if (insNum == insCnt)
2613     {
2614         if (ig == emitCurIG)
2615         {
2616             // No instructions beyond the current location.
2617             return false;
2618         }
2619
2620         for (ig = ig->igNext; ig; ig = ig->igNext)
2621         {
2622             emitGetInstrDescs(ig, &id, &insCnt);
2623
2624             if (insCnt > 0)
2625             {
2626                 insNum = 0; // Pretend the index is 0 -- the first instruction
2627                 break;
2628             }
2629
2630             if (ig == emitCurIG)
2631             {
2632                 // There aren't any instructions in the current IG, and this is
2633                 // the current location, so we're at the end.
2634                 return false;
2635             }
2636         }
2637
2638         if (ig == NULL)
2639         {
2640             // 'ig' can't be NULL, or we went past the current IG represented by 'emitCurIG'.
2641             // Perhaps 'loc' was corrupt coming in?
2642             noway_assert(!"corrupt emitter location");
2643             return false;
2644         }
2645     }
2646
2647     // Now find the instrDesc within this group that corresponds to the location
2648
2649     assert(insNum < insCnt);
2650
2651     int i;
2652     for (i = 0; i != insNum; ++i)
2653     {
2654         castto(id, BYTE*) += emitSizeOfInsDsc(id);
2655     }
2656
2657     // Return the info we found
2658
2659     *pig = ig;
2660     *pid = id;
2661
2662     if (pinsRemaining)
2663     {
2664         *pinsRemaining = insCnt - insNum - 1;
2665     }
2666
2667     return true;
2668 }
2669
2670 /*****************************************************************************
2671  *
2672  * Compute the next instrDesc, either in this IG, or in a subsequent IG. 'id'
2673  * will point to this instrDesc. 'ig' and 'insRemaining' will also be updated.
2674  * Returns true if there is an instruction, or false if we've iterated over all
2675  * the instructions up to the current instruction (based on 'emitCurIG').
2676  */
2677
2678 bool emitter::emitNextID(insGroup*& ig, instrDesc*& id, int& insRemaining)
2679 {
2680     if (insRemaining > 0)
2681     {
2682         castto(id, BYTE*) += emitSizeOfInsDsc(id);
2683         --insRemaining;
2684         return true;
2685     }
2686
2687     // We're out of instrDesc in 'ig'. Is this the current IG? If so, we're done.
2688
2689     if (ig == emitCurIG)
2690     {
2691         return false;
2692     }
2693
2694     for (ig = ig->igNext; ig; ig = ig->igNext)
2695     {
2696         int insCnt;
2697         emitGetInstrDescs(ig, &id, &insCnt);
2698
2699         if (insCnt > 0)
2700         {
2701             insRemaining = insCnt - 1;
2702             return true;
2703         }
2704
2705         if (ig == emitCurIG)
2706         {
2707             return false;
2708         }
2709     }
2710
2711     return false;
2712 }
2713
2714 /*****************************************************************************
2715  *
2716  * Walk instrDesc's from the location given by 'locFrom', up to the current location.
2717  * For each instruction, call the callback function 'processFunc'. 'context' is simply
2718  * passed through to the callback function.
2719  */
2720
2721 void emitter::emitWalkIDs(emitLocation* locFrom, emitProcessInstrFunc_t processFunc, void* context)
2722 {
2723     insGroup*  ig;
2724     instrDesc* id;
2725     int        insRemaining;
2726
2727     if (!emitGetLocationInfo(locFrom, &ig, &id, &insRemaining))
2728         return; // no instructions at the 'from' location
2729
2730     do
2731     {
2732         // process <<id>>
2733         (*processFunc)(id, context);
2734
2735     } while (emitNextID(ig, id, insRemaining));
2736 }
2737
2738 /*****************************************************************************
2739  *
2740  * A callback function for emitWalkIDs() that calls Compiler::unwindNop().
2741  */
2742
2743 void emitter::emitGenerateUnwindNop(instrDesc* id, void* context)
2744 {
2745     Compiler* comp = (Compiler*)context;
2746 #if defined(_TARGET_ARM_)
2747     comp->unwindNop(id->idCodeSize());
2748 #elif defined(_TARGET_ARM64_)
2749     comp->unwindNop();
2750 #endif // defined(_TARGET_ARM64_)
2751 }
2752
2753 /*****************************************************************************
2754  *
2755  * emitUnwindNopPadding: call unwindNop() for every instruction from a given
2756  * location 'emitLoc' up to the current location.
2757  */
2758
2759 void emitter::emitUnwindNopPadding(emitLocation* locFrom, Compiler* comp)
2760 {
2761     emitWalkIDs(locFrom, emitGenerateUnwindNop, comp);
2762 }
2763
2764 #endif // _TARGET_ARMARCH_
2765
2766 #if defined(_TARGET_ARM_)
2767
2768 /*****************************************************************************
2769  *
2770  * Return the instruction size in bytes for the instruction at the specified location.
2771  * This is used to assert that the unwind code being generated on ARM has the
2772  * same size as the instruction for which it is being generated (since on ARM
2773  * the unwind codes have a one-to-one relationship with instructions, and the
2774  * unwind codes have an implicit instruction size that must match the instruction size.)
2775  * An instruction must exist at the specified location.
2776  */
2777
2778 unsigned emitter::emitGetInstructionSize(emitLocation* emitLoc)
2779 {
2780     insGroup*  ig;
2781     instrDesc* id;
2782
2783     bool anyInstrs = emitGetLocationInfo(emitLoc, &ig, &id);
2784     assert(anyInstrs); // There better be an instruction at this location (otherwise, we're at the end of the
2785                        // instruction list)
2786     return id->idCodeSize();
2787 }
2788
2789 #endif // defined(_TARGET_ARM_)
2790
2791 /*****************************************************************************/
2792 #ifdef DEBUG
2793 /*****************************************************************************
2794  *
2795  *  Returns the name for the register to use to access frame based variables
2796  */
2797
2798 const char* emitter::emitGetFrameReg()
2799 {
2800     if (emitHasFramePtr)
2801     {
2802         return STR_FPBASE;
2803     }
2804     else
2805     {
2806         return STR_SPBASE;
2807     }
2808 }
2809
2810 /*****************************************************************************
2811  *
2812  *  Display a register set in a readable form.
2813  */
2814
2815 void emitter::emitDispRegSet(regMaskTP regs)
2816 {
2817     regNumber reg;
2818     bool      sp = false;
2819
2820     printf(" {");
2821
2822     for (reg = REG_FIRST; reg < ACTUAL_REG_COUNT; reg = REG_NEXT(reg))
2823     {
2824         if ((regs & genRegMask(reg)) == 0)
2825         {
2826             continue;
2827         }
2828
2829         if (sp)
2830         {
2831             printf(" ");
2832         }
2833         else
2834         {
2835             sp = true;
2836         }
2837
2838         printf("%s", emitRegName(reg));
2839     }
2840
2841     printf("}");
2842 }
2843
2844 /*****************************************************************************
2845  *
2846  *  Display the current GC ref variable set in a readable form.
2847  */
2848
2849 void emitter::emitDispVarSet()
2850 {
2851     unsigned vn;
2852     int      of;
2853     bool     sp = false;
2854
2855     for (vn = 0, of = emitGCrFrameOffsMin; vn < emitGCrFrameOffsCnt; vn += 1, of += sizeof(void*))
2856     {
2857         if (emitGCrFrameLiveTab[vn])
2858         {
2859             if (sp)
2860             {
2861                 printf(" ");
2862             }
2863             else
2864             {
2865                 sp = true;
2866             }
2867
2868             printf("[%s", emitGetFrameReg());
2869
2870             if (of < 0)
2871             {
2872                 printf("-%02XH", -of);
2873             }
2874             else if (of > 0)
2875             {
2876                 printf("+%02XH", +of);
2877             }
2878
2879             printf("]");
2880         }
2881     }
2882
2883     if (!sp)
2884     {
2885         printf("none");
2886     }
2887 }
2888
2889 /*****************************************************************************/
2890 #endif // DEBUG
2891
2892 #if MULTIREG_HAS_SECOND_GC_RET
2893 //------------------------------------------------------------------------
2894 // emitSetSecondRetRegGCType: Sets the GC type of the second return register for instrDescCGCA struct.
2895 //
2896 // Arguments:
2897 //    id            - The large call instr descriptor to set the second GC return register type on.
2898 //    secondRetSize - The EA_SIZE for second return register type.
2899 //
2900 // Return Value:
2901 //    None
2902 //
2903
2904 void emitter::emitSetSecondRetRegGCType(instrDescCGCA* id, emitAttr secondRetSize)
2905 {
2906     if (EA_IS_GCREF(secondRetSize))
2907     {
2908         id->idSecondGCref(GCT_GCREF);
2909     }
2910     else if (EA_IS_BYREF(secondRetSize))
2911     {
2912         id->idSecondGCref(GCT_BYREF);
2913     }
2914     else
2915     {
2916         id->idSecondGCref(GCT_NONE);
2917     }
2918 }
2919 #endif // MULTIREG_HAS_SECOND_GC_RET
2920
2921 /*****************************************************************************
2922  *
2923  *  Allocate an instruction descriptor for an indirect call.
2924  *
2925  *  We use two different descriptors to save space - the common case records
2926  *  no GC variables and has both a very small argument count and an address
2927  *  mode displacement; the other case records the current GC var set,
2928  *  the call scope, and an arbitrarily large argument count and the
2929  *  address mode displacement.
2930  */
2931
2932 emitter::instrDesc* emitter::emitNewInstrCallInd(int              argCnt,
2933                                                  ssize_t          disp,
2934                                                  VARSET_VALARG_TP GCvars,
2935                                                  regMaskTP        gcrefRegs,
2936                                                  regMaskTP        byrefRegs,
2937                                                  emitAttr         retSizeIn
2938                                                      MULTIREG_HAS_SECOND_GC_RET_ONLY_ARG(emitAttr secondRetSize))
2939 {
2940     emitAttr retSize = (retSizeIn != EA_UNKNOWN) ? retSizeIn : EA_PTRSIZE;
2941
2942     bool gcRefRegsInScratch = ((gcrefRegs & RBM_CALLEE_TRASH) != 0);
2943
2944     // Allocate a larger descriptor if any GC values need to be saved
2945     // or if we have an absurd number of arguments or a large address
2946     // mode displacement, or we have some byref registers
2947     //
2948     // On Amd64 System V OSs a larger descriptor is also needed if the
2949     // call returns a two-register-returned struct and the second
2950     // register (RDX) is a GCRef or ByRef pointer.
2951
2952     if (!VarSetOps::IsEmpty(emitComp, GCvars) || // any frame GCvars live
2953         (gcRefRegsInScratch) ||                  // any register gc refs live in scratch regs
2954         (byrefRegs != 0) ||                      // any register byrefs live
2955         (disp < AM_DISP_MIN) ||                  // displacement too negative
2956         (disp > AM_DISP_MAX) ||                  // displacement too positive
2957         (argCnt > ID_MAX_SMALL_CNS) ||           // too many args
2958         (argCnt < 0)                             // caller pops arguments
2959                                                  // There is a second ref/byref return register.
2960         MULTIREG_HAS_SECOND_GC_RET_ONLY(|| EA_IS_GCREF_OR_BYREF(secondRetSize)))
2961     {
2962         instrDescCGCA* id;
2963
2964         id = emitAllocInstrCGCA(retSize);
2965
2966         id->idSetIsLargeCall();
2967
2968         VarSetOps::Assign(emitComp, id->idcGCvars, GCvars);
2969         id->idcGcrefRegs = gcrefRegs;
2970         id->idcByrefRegs = byrefRegs;
2971         id->idcArgCnt    = argCnt;
2972         id->idcDisp      = disp;
2973
2974 #if MULTIREG_HAS_SECOND_GC_RET
2975         emitSetSecondRetRegGCType(id, secondRetSize);
2976 #endif // MULTIREG_HAS_SECOND_GC_RET
2977
2978         return id;
2979     }
2980     else
2981     {
2982         instrDesc* id;
2983
2984         id = emitNewInstrCns(retSize, argCnt);
2985
2986         /* Make sure we didn't waste space unexpectedly */
2987         assert(!id->idIsLargeCns());
2988
2989         /* Store the displacement and make sure the value fit */
2990         id->idAddr()->iiaAddrMode.amDisp = disp;
2991         assert(id->idAddr()->iiaAddrMode.amDisp == disp);
2992
2993         /* Save the the live GC registers in the unused register fields */
2994         emitEncodeCallGCregs(gcrefRegs, id);
2995
2996         return id;
2997     }
2998 }
2999
3000 /*****************************************************************************
3001  *
3002  *  Allocate an instruction descriptor for a direct call.
3003  *
3004  *  We use two different descriptors to save space - the common case records
3005  *  with no GC variables or byrefs and has a very small argument count, and no
3006  *  explicit scope;
3007  *  the other case records the current GC var set, the call scope,
3008  *  and an arbitrarily large argument count.
3009  */
3010
3011 emitter::instrDesc* emitter::emitNewInstrCallDir(int              argCnt,
3012                                                  VARSET_VALARG_TP GCvars,
3013                                                  regMaskTP        gcrefRegs,
3014                                                  regMaskTP        byrefRegs,
3015                                                  emitAttr         retSizeIn
3016                                                      MULTIREG_HAS_SECOND_GC_RET_ONLY_ARG(emitAttr secondRetSize))
3017 {
3018     emitAttr retSize = (retSizeIn != EA_UNKNOWN) ? retSizeIn : EA_PTRSIZE;
3019
3020     // Allocate a larger descriptor if new GC values need to be saved
3021     // or if we have an absurd number of arguments or if we need to
3022     // save the scope.
3023     //
3024     // On Amd64 System V OSs a larger descriptor is also needed if the
3025     // call returns a two-register-returned struct and the second
3026     // register (RDX) is a GCRef or ByRef pointer.
3027
3028     bool gcRefRegsInScratch = ((gcrefRegs & RBM_CALLEE_TRASH) != 0);
3029
3030     if (!VarSetOps::IsEmpty(emitComp, GCvars) || // any frame GCvars live
3031         gcRefRegsInScratch ||                    // any register gc refs live in scratch regs
3032         (byrefRegs != 0) ||                      // any register byrefs live
3033         (argCnt > ID_MAX_SMALL_CNS) ||           // too many args
3034         (argCnt < 0)                             // caller pops arguments
3035                                                  // There is a second ref/byref return register.
3036         MULTIREG_HAS_SECOND_GC_RET_ONLY(|| EA_IS_GCREF_OR_BYREF(secondRetSize)))
3037     {
3038         instrDescCGCA* id = emitAllocInstrCGCA(retSize);
3039
3040         // printf("Direct call with GC vars / big arg cnt / explicit scope\n");
3041
3042         id->idSetIsLargeCall();
3043
3044         VarSetOps::Assign(emitComp, id->idcGCvars, GCvars);
3045         id->idcGcrefRegs = gcrefRegs;
3046         id->idcByrefRegs = byrefRegs;
3047         id->idcDisp      = 0;
3048         id->idcArgCnt    = argCnt;
3049
3050 #if MULTIREG_HAS_SECOND_GC_RET
3051         emitSetSecondRetRegGCType(id, secondRetSize);
3052 #endif // MULTIREG_HAS_SECOND_GC_RET
3053
3054         return id;
3055     }
3056     else
3057     {
3058         instrDesc* id = emitNewInstrCns(retSize, argCnt);
3059
3060         // printf("Direct call w/o  GC vars / big arg cnt / explicit scope\n");
3061
3062         /* Make sure we didn't waste space unexpectedly */
3063         assert(!id->idIsLargeCns());
3064
3065         /* Save the the live GC registers in the unused register fields */
3066         emitEncodeCallGCregs(gcrefRegs, id);
3067
3068         return id;
3069     }
3070 }
3071
3072 /*****************************************************************************/
3073 #ifdef DEBUG
3074 /*****************************************************************************
3075  *
3076  *  Return a string with the name of the given class field (blank string (not
3077  *  NULL) is returned when the name isn't available).
3078  */
3079
3080 const char* emitter::emitFldName(CORINFO_FIELD_HANDLE fieldVal)
3081 {
3082     if (emitComp->opts.varNames)
3083     {
3084         const char* memberName;
3085         const char* className;
3086
3087         const int   TEMP_BUFFER_LEN = 1024;
3088         static char buff[TEMP_BUFFER_LEN];
3089
3090         memberName = emitComp->eeGetFieldName(fieldVal, &className);
3091
3092         sprintf_s(buff, TEMP_BUFFER_LEN, "'<%s>.%s'", className, memberName);
3093         return buff;
3094     }
3095     else
3096     {
3097         return "";
3098     }
3099 }
3100
3101 /*****************************************************************************
3102  *
3103  *  Return a string with the name of the given function (blank string (not
3104  *  NULL) is returned when the name isn't available).
3105  */
3106
3107 const char* emitter::emitFncName(CORINFO_METHOD_HANDLE methHnd)
3108 {
3109     return emitComp->eeGetMethodFullName(methHnd);
3110 }
3111
3112 #endif // DEBUG
3113
3114 /*****************************************************************************
3115  *
3116  *  Be very careful, some instruction descriptors are allocated as "tiny" and
3117  *  don't have some of the tail fields of instrDesc (in particular, "idInfo").
3118  */
3119
3120 const BYTE emitter::emitFmtToOps[] = {
3121 #define IF_DEF(en, op1, op2) ID_OP_##op2,
3122 #include "emitfmts.h"
3123 };
3124
3125 #ifdef DEBUG
3126 const unsigned emitter::emitFmtCount = sizeof(emitFmtToOps) / sizeof(emitFmtToOps[0]);
3127 #endif
3128
3129 /*****************************************************************************
3130  *
3131  *  Display the current instruction group list.
3132  */
3133
3134 #ifdef DEBUG
3135
3136 void emitter::emitDispIGflags(unsigned flags)
3137 {
3138     if (flags & IGF_GC_VARS)
3139     {
3140         printf(", gcvars");
3141     }
3142     if (flags & IGF_BYREF_REGS)
3143     {
3144         printf(", byref");
3145     }
3146 #if FEATURE_EH_FUNCLETS && defined(_TARGET_ARM_)
3147     if (flags & IGF_FINALLY_TARGET)
3148     {
3149         printf(", ftarget");
3150     }
3151 #endif // FEATURE_EH_FUNCLETS && defined(_TARGET_ARM_)
3152     if (flags & IGF_FUNCLET_PROLOG)
3153     {
3154         printf(", funclet prolog");
3155     }
3156     if (flags & IGF_FUNCLET_EPILOG)
3157     {
3158         printf(", funclet epilog");
3159     }
3160     if (flags & IGF_EPILOG)
3161     {
3162         printf(", epilog");
3163     }
3164     if (flags & IGF_NOGCINTERRUPT)
3165     {
3166         printf(", nogc");
3167     }
3168     if (flags & IGF_UPD_ISZ)
3169     {
3170         printf(", isz");
3171     }
3172     if (flags & IGF_EMIT_ADD)
3173     {
3174         printf(", emitadd");
3175     }
3176 }
3177
3178 void emitter::emitDispIG(insGroup* ig, insGroup* igPrev, bool verbose)
3179 {
3180     const int TEMP_BUFFER_LEN = 40;
3181     char      buff[TEMP_BUFFER_LEN];
3182
3183     sprintf_s(buff, TEMP_BUFFER_LEN, "G_M%03u_IG%02u:        ", Compiler::s_compMethodsCount, ig->igNum);
3184     printf("%s; ", buff);
3185     if ((igPrev == nullptr) || (igPrev->igFuncIdx != ig->igFuncIdx))
3186     {
3187         printf("func=%02u, ", ig->igFuncIdx);
3188     }
3189
3190     if (ig->igFlags & IGF_PLACEHOLDER)
3191     {
3192         insGroup* igPh = ig;
3193
3194         const char* pszType;
3195         switch (igPh->igPhData->igPhType)
3196         {
3197             case IGPT_PROLOG:
3198                 pszType = "prolog";
3199                 break;
3200             case IGPT_EPILOG:
3201                 pszType = "epilog";
3202                 break;
3203 #if FEATURE_EH_FUNCLETS
3204             case IGPT_FUNCLET_PROLOG:
3205                 pszType = "funclet prolog";
3206                 break;
3207             case IGPT_FUNCLET_EPILOG:
3208                 pszType = "funclet epilog";
3209                 break;
3210 #endif // FEATURE_EH_FUNCLETS
3211             default:
3212                 pszType = "UNKNOWN";
3213                 break;
3214         }
3215         printf("%s placeholder, next placeholder=", pszType);
3216         if (igPh->igPhData->igPhNext)
3217         {
3218             printf("IG%02u ", igPh->igPhData->igPhNext->igNum);
3219         }
3220         else
3221         {
3222             printf("<END>");
3223         }
3224
3225         if (igPh->igPhData->igPhBB != nullptr)
3226         {
3227             printf(", %s", igPh->igPhData->igPhBB->dspToString());
3228         }
3229
3230         emitDispIGflags(igPh->igFlags);
3231
3232         if (ig == emitCurIG)
3233         {
3234             printf(" <-- Current IG");
3235         }
3236         if (igPh == emitPlaceholderList)
3237         {
3238             printf(" <-- First placeholder");
3239         }
3240         if (igPh == emitPlaceholderLast)
3241         {
3242             printf(" <-- Last placeholder");
3243         }
3244         printf("\n");
3245
3246         printf("%*s;   PrevGCVars=%s ", strlen(buff), "",
3247                VarSetOps::ToString(emitComp, igPh->igPhData->igPhPrevGCrefVars));
3248         dumpConvertedVarSet(emitComp, igPh->igPhData->igPhPrevGCrefVars);
3249         printf(", PrevGCrefRegs=");
3250         printRegMaskInt(igPh->igPhData->igPhPrevGCrefRegs);
3251         emitDispRegSet(igPh->igPhData->igPhPrevGCrefRegs);
3252         printf(", PrevByrefRegs=");
3253         printRegMaskInt(igPh->igPhData->igPhPrevByrefRegs);
3254         emitDispRegSet(igPh->igPhData->igPhPrevByrefRegs);
3255         printf("\n");
3256
3257         printf("%*s;   InitGCVars=%s ", strlen(buff), "",
3258                VarSetOps::ToString(emitComp, igPh->igPhData->igPhInitGCrefVars));
3259         dumpConvertedVarSet(emitComp, igPh->igPhData->igPhInitGCrefVars);
3260         printf(", InitGCrefRegs=");
3261         printRegMaskInt(igPh->igPhData->igPhInitGCrefRegs);
3262         emitDispRegSet(igPh->igPhData->igPhInitGCrefRegs);
3263         printf(", InitByrefRegs=");
3264         printRegMaskInt(igPh->igPhData->igPhInitByrefRegs);
3265         emitDispRegSet(igPh->igPhData->igPhInitByrefRegs);
3266         printf("\n");
3267
3268         assert(!(ig->igFlags & IGF_GC_VARS));
3269         assert(!(ig->igFlags & IGF_BYREF_REGS));
3270     }
3271     else
3272     {
3273         printf("offs=%06XH, size=%04XH", ig->igOffs, ig->igSize);
3274
3275         if (ig->igFlags & IGF_GC_VARS)
3276         {
3277             printf(", gcVars=%s ", VarSetOps::ToString(emitComp, ig->igGCvars()));
3278             dumpConvertedVarSet(emitComp, ig->igGCvars());
3279         }
3280
3281         if (!(ig->igFlags & IGF_EMIT_ADD))
3282         {
3283             printf(", gcrefRegs=");
3284             printRegMaskInt(ig->igGCregs);
3285             emitDispRegSet(ig->igGCregs);
3286         }
3287
3288         if (ig->igFlags & IGF_BYREF_REGS)
3289         {
3290             printf(", byrefRegs=");
3291             printRegMaskInt(ig->igByrefRegs());
3292             emitDispRegSet(ig->igByrefRegs());
3293         }
3294
3295         emitDispIGflags(ig->igFlags);
3296
3297         if (ig == emitCurIG)
3298         {
3299             printf(" <-- Current IG");
3300         }
3301         if (ig == emitPrologIG)
3302         {
3303             printf(" <-- Prolog IG");
3304         }
3305         printf("\n");
3306
3307         if (verbose)
3308         {
3309             BYTE*          ins = ig->igData;
3310             UNATIVE_OFFSET ofs = ig->igOffs;
3311             unsigned       cnt = ig->igInsCnt;
3312
3313             if (cnt)
3314             {
3315                 printf("\n");
3316
3317                 do
3318                 {
3319                     instrDesc* id = (instrDesc*)ins;
3320
3321                     emitDispIns(id, false, true, false, ofs, nullptr, 0, ig);
3322
3323                     ins += emitSizeOfInsDsc(id);
3324                     ofs += emitInstCodeSz(id);
3325                 } while (--cnt);
3326
3327                 printf("\n");
3328             }
3329         }
3330     }
3331 }
3332
3333 void emitter::emitDispIGlist(bool verbose)
3334 {
3335     insGroup* ig;
3336     insGroup* igPrev;
3337
3338     for (igPrev = nullptr, ig = emitIGlist; ig; igPrev = ig, ig = ig->igNext)
3339     {
3340         emitDispIG(ig, igPrev, verbose);
3341     }
3342 }
3343
3344 void emitter::emitDispGCinfo()
3345 {
3346     printf("Emitter GC tracking info:");
3347     printf("\n  emitPrevGCrefVars ");
3348     dumpConvertedVarSet(emitComp, emitPrevGCrefVars);
3349     printf("\n  emitPrevGCrefRegs(0x%p)=", dspPtr(&emitPrevGCrefRegs));
3350     printRegMaskInt(emitPrevGCrefRegs);
3351     emitDispRegSet(emitPrevGCrefRegs);
3352     printf("\n  emitPrevByrefRegs(0x%p)=", dspPtr(&emitPrevByrefRegs));
3353     printRegMaskInt(emitPrevByrefRegs);
3354     emitDispRegSet(emitPrevByrefRegs);
3355     printf("\n  emitInitGCrefVars ");
3356     dumpConvertedVarSet(emitComp, emitInitGCrefVars);
3357     printf("\n  emitInitGCrefRegs(0x%p)=", dspPtr(&emitInitGCrefRegs));
3358     printRegMaskInt(emitInitGCrefRegs);
3359     emitDispRegSet(emitInitGCrefRegs);
3360     printf("\n  emitInitByrefRegs(0x%p)=", dspPtr(&emitInitByrefRegs));
3361     printRegMaskInt(emitInitByrefRegs);
3362     emitDispRegSet(emitInitByrefRegs);
3363     printf("\n  emitThisGCrefVars ");
3364     dumpConvertedVarSet(emitComp, emitThisGCrefVars);
3365     printf("\n  emitThisGCrefRegs(0x%p)=", dspPtr(&emitThisGCrefRegs));
3366     printRegMaskInt(emitThisGCrefRegs);
3367     emitDispRegSet(emitThisGCrefRegs);
3368     printf("\n  emitThisByrefRegs(0x%p)=", dspPtr(&emitThisByrefRegs));
3369     printRegMaskInt(emitThisByrefRegs);
3370     emitDispRegSet(emitThisByrefRegs);
3371     printf("\n\n");
3372 }
3373
3374 #endif // DEBUG
3375
3376 /*****************************************************************************
3377  *
3378  *  Issue the given instruction. Basically, this is just a thin wrapper around
3379  *  emitOutputInstr() that does a few debug checks.
3380  */
3381
3382 size_t emitter::emitIssue1Instr(insGroup* ig, instrDesc* id, BYTE** dp)
3383 {
3384     size_t is;
3385
3386     /* Record the beginning offset of the instruction */
3387
3388     BYTE* curInsAdr = *dp;
3389
3390     /* Issue the next instruction */
3391
3392     // printf("[S=%02u] " , emitCurStackLvl);
3393
3394     is = emitOutputInstr(ig, id, dp);
3395
3396 // printf("[S=%02u]\n", emitCurStackLvl);
3397
3398 #if EMIT_TRACK_STACK_DEPTH
3399
3400     /*
3401         If we're generating a full pointer map and the stack
3402         is empty, there better not be any "pending" argument
3403         push entries.
3404      */
3405
3406     assert(emitFullGCinfo == false || emitCurStackLvl != 0 || u2.emitGcArgTrackCnt == 0);
3407
3408 #endif
3409
3410     /* Did the size of the instruction match our expectations? */
3411
3412     UNATIVE_OFFSET csz = (UNATIVE_OFFSET)(*dp - curInsAdr);
3413
3414     if (csz != id->idCodeSize())
3415     {
3416         /* It is fatal to under-estimate the instruction size */
3417         noway_assert(emitInstCodeSz(id) >= csz);
3418
3419 #if DEBUG_EMIT
3420         if (EMITVERBOSE)
3421         {
3422             printf("Instruction predicted size = %u, actual = %u\n", emitInstCodeSz(id), csz);
3423         }
3424 #endif // DEBUG_EMIT
3425
3426         /* The instruction size estimate wasn't accurate; remember this */
3427
3428         ig->igFlags |= IGF_UPD_ISZ;
3429 #if defined(_TARGET_XARCH_)
3430         id->idCodeSize(csz);
3431 #elif defined(_TARGET_ARM_)
3432 // This is done as part of emitSetShortJump();
3433 // insSize isz = emitInsSize(id->idInsFmt());
3434 // id->idInsSize(isz);
3435 #else
3436         /* It is fatal to over-estimate the instruction size */
3437         IMPL_LIMITATION("Over-estimated instruction size");
3438 #endif
3439     }
3440
3441 #ifdef DEBUG
3442     /* Make sure the instruction descriptor size also matches our expectations */
3443     if (is != emitSizeOfInsDsc(id))
3444     {
3445         printf("%s at %u: Expected size = %u , actual size = %u\n", emitIfName(id->idInsFmt()),
3446                id->idDebugOnlyInfo()->idNum, is, emitSizeOfInsDsc(id));
3447         assert(is == emitSizeOfInsDsc(id));
3448     }
3449 #endif
3450
3451     return is;
3452 }
3453
3454 /*****************************************************************************
3455  *
3456  *  Update the offsets of all the instruction groups (note: please don't be
3457  *  lazy and call this routine frequently, it walks the list of instruction
3458  *  groups and thus it isn't cheap).
3459  */
3460
3461 void emitter::emitRecomputeIGoffsets()
3462 {
3463     UNATIVE_OFFSET offs;
3464     insGroup*      ig;
3465
3466     for (ig = emitIGlist, offs = 0; ig; ig = ig->igNext)
3467     {
3468         ig->igOffs = offs;
3469         assert(IsCodeAligned(ig->igOffs));
3470         offs += ig->igSize;
3471     }
3472
3473     /* Set the total code size */
3474
3475     emitTotalCodeSize = offs;
3476
3477 #ifdef DEBUG
3478     emitCheckIGoffsets();
3479 #endif
3480 }
3481
3482 /*****************************************************************************
3483  *  Bind targets of relative jumps to choose the smallest possible encoding.
3484  *  X86 and AMD64 have a small and large encoding.
3485  *  ARM has a small, medium, and large encoding. The large encoding is a pseudo-op
3486  *      to handle greater range than the conditional branch instructions can handle.
3487  *  ARM64 has a small and large encoding for both conditional branch and loading label addresses.
3488  *      The large encodings are pseudo-ops that represent a multiple instruction sequence, similar to ARM. (Currently
3489  *      NYI).
3490  */
3491
3492 void emitter::emitJumpDistBind()
3493 {
3494 #ifdef DEBUG
3495     if (emitComp->verbose)
3496     {
3497         printf("*************** In emitJumpDistBind()\n");
3498     }
3499     if (EMIT_INSTLIST_VERBOSE)
3500     {
3501         printf("\nInstruction list before jump distance binding:\n\n");
3502         emitDispIGlist(true);
3503     }
3504 #endif
3505
3506     instrDescJmp* jmp;
3507
3508     UNATIVE_OFFSET minShortExtra; // The smallest offset greater than that required for a jump to be converted
3509                                   // to a small jump. If it is small enough, we will iterate in hopes of
3510                                   // converting those jumps we missed converting the first (or second...) time.
3511
3512 #if defined(_TARGET_ARM_)
3513     UNATIVE_OFFSET minMediumExtra; // Same as 'minShortExtra', but for medium-sized jumps.
3514 #endif                             // _TARGET_ARM_
3515
3516     UNATIVE_OFFSET adjIG;
3517     UNATIVE_OFFSET adjLJ;
3518     insGroup*      lstIG;
3519 #ifdef DEBUG
3520     insGroup* prologIG = emitPrologIG;
3521 #endif // DEBUG
3522
3523     int jmp_iteration = 1;
3524
3525 /*****************************************************************************/
3526 /* If we iterate to look for more jumps to shorten, we start again here.     */
3527 /*****************************************************************************/
3528
3529 AGAIN:
3530
3531 #ifdef DEBUG
3532     emitCheckIGoffsets();
3533 #endif
3534
3535 /*
3536     In the following loop we convert all jump targets from "BasicBlock *"
3537     to "insGroup *" values. We also estimate which jumps will be short.
3538  */
3539
3540 #ifdef DEBUG
3541     insGroup*     lastIG = nullptr;
3542     instrDescJmp* lastLJ = nullptr;
3543 #endif
3544
3545     lstIG         = nullptr;
3546     adjLJ         = 0;
3547     adjIG         = 0;
3548     minShortExtra = (UNATIVE_OFFSET)-1;
3549
3550 #if defined(_TARGET_ARM_)
3551     minMediumExtra = (UNATIVE_OFFSET)-1;
3552 #endif // _TARGET_ARM_
3553
3554     for (jmp = emitJumpList; jmp; jmp = jmp->idjNext)
3555     {
3556         insGroup* jmpIG;
3557         insGroup* tgtIG;
3558
3559         UNATIVE_OFFSET jsz; // size of the jump instruction in bytes
3560
3561         UNATIVE_OFFSET ssz = 0; // small  jump size
3562         NATIVE_OFFSET  nsd = 0; // small  jump max. neg distance
3563         NATIVE_OFFSET  psd = 0; // small  jump max. pos distance
3564
3565 #if defined(_TARGET_ARM_)
3566         UNATIVE_OFFSET msz = 0; // medium jump size
3567         NATIVE_OFFSET  nmd = 0; // medium jump max. neg distance
3568         NATIVE_OFFSET  pmd = 0; // medium jump max. pos distance
3569         NATIVE_OFFSET  mextra;  // How far beyond the medium jump range is this jump offset?
3570 #endif                          // _TARGET_ARM_
3571
3572         NATIVE_OFFSET  extra;           // How far beyond the short jump range is this jump offset?
3573         UNATIVE_OFFSET srcInstrOffs;    // offset of the source instruction of the jump
3574         UNATIVE_OFFSET srcEncodingOffs; // offset of the source used by the instruction set to calculate the relative
3575                                         // offset of the jump
3576         UNATIVE_OFFSET dstOffs;
3577         NATIVE_OFFSET  jmpDist; // the relative jump distance, as it will be encoded
3578         UNATIVE_OFFSET oldSize;
3579         UNATIVE_OFFSET sizeDif;
3580
3581 #ifdef _TARGET_XARCH_
3582         assert(jmp->idInsFmt() == IF_LABEL || jmp->idInsFmt() == IF_RWR_LABEL || jmp->idInsFmt() == IF_SWR_LABEL);
3583
3584         /* Figure out the smallest size we can end up with */
3585
3586         if (jmp->idInsFmt() == IF_LABEL)
3587         {
3588             if (emitIsCondJump(jmp))
3589             {
3590                 ssz = JCC_SIZE_SMALL;
3591                 nsd = JCC_DIST_SMALL_MAX_NEG;
3592                 psd = JCC_DIST_SMALL_MAX_POS;
3593             }
3594             else
3595             {
3596                 ssz = JMP_SIZE_SMALL;
3597                 nsd = JMP_DIST_SMALL_MAX_NEG;
3598                 psd = JMP_DIST_SMALL_MAX_POS;
3599             }
3600         }
3601 #endif // _TARGET_XARCH_
3602
3603 #ifdef _TARGET_ARM_
3604         assert((jmp->idInsFmt() == IF_T2_J1) || (jmp->idInsFmt() == IF_T2_J2) || (jmp->idInsFmt() == IF_T1_I) ||
3605                (jmp->idInsFmt() == IF_T1_K) || (jmp->idInsFmt() == IF_T1_M) || (jmp->idInsFmt() == IF_T2_M1) ||
3606                (jmp->idInsFmt() == IF_T2_N1) || (jmp->idInsFmt() == IF_T1_J3) || (jmp->idInsFmt() == IF_LARGEJMP));
3607
3608         /* Figure out the smallest size we can end up with */
3609
3610         if (emitIsCondJump(jmp))
3611         {
3612             ssz = JCC_SIZE_SMALL;
3613             nsd = JCC_DIST_SMALL_MAX_NEG;
3614             psd = JCC_DIST_SMALL_MAX_POS;
3615
3616             msz = JCC_SIZE_MEDIUM;
3617             nmd = JCC_DIST_MEDIUM_MAX_NEG;
3618             pmd = JCC_DIST_MEDIUM_MAX_POS;
3619         }
3620         else if (emitIsCmpJump(jmp))
3621         {
3622             ssz = JMP_SIZE_SMALL;
3623             nsd = 0;
3624             psd = 126;
3625         }
3626         else if (emitIsUncondJump(jmp))
3627         {
3628             ssz = JMP_SIZE_SMALL;
3629             nsd = JMP_DIST_SMALL_MAX_NEG;
3630             psd = JMP_DIST_SMALL_MAX_POS;
3631         }
3632         else if (emitIsLoadLabel(jmp))
3633         {
3634             ssz = LBL_SIZE_SMALL;
3635             nsd = LBL_DIST_SMALL_MAX_NEG;
3636             psd = LBL_DIST_SMALL_MAX_POS;
3637         }
3638         else
3639         {
3640             assert(!"Unknown jump instruction");
3641         }
3642 #endif // _TARGET_ARM_
3643
3644 #ifdef _TARGET_ARM64_
3645         /* Figure out the smallest size we can end up with */
3646
3647         if (emitIsCondJump(jmp))
3648         {
3649             ssz         = JCC_SIZE_SMALL;
3650             bool isTest = (jmp->idIns() == INS_tbz) || (jmp->idIns() == INS_tbnz);
3651
3652             nsd = (isTest) ? TB_DIST_SMALL_MAX_NEG : JCC_DIST_SMALL_MAX_NEG;
3653             psd = (isTest) ? TB_DIST_SMALL_MAX_POS : JCC_DIST_SMALL_MAX_POS;
3654         }
3655         else if (emitIsUncondJump(jmp))
3656         {
3657             // Nothing to do; we don't shrink these.
3658             assert(jmp->idjShort);
3659             ssz = JMP_SIZE_SMALL;
3660         }
3661         else if (emitIsLoadLabel(jmp))
3662         {
3663             ssz = LBL_SIZE_SMALL;
3664             nsd = LBL_DIST_SMALL_MAX_NEG;
3665             psd = LBL_DIST_SMALL_MAX_POS;
3666         }
3667         else if (emitIsLoadConstant(jmp))
3668         {
3669             ssz = LDC_SIZE_SMALL;
3670             nsd = LDC_DIST_SMALL_MAX_NEG;
3671             psd = LDC_DIST_SMALL_MAX_POS;
3672         }
3673         else
3674         {
3675             assert(!"Unknown jump instruction");
3676         }
3677 #endif // _TARGET_ARM64_
3678
3679 /* Make sure the jumps are properly ordered */
3680
3681 #ifdef DEBUG
3682         assert(lastLJ == nullptr || lastIG != jmp->idjIG || lastLJ->idjOffs < jmp->idjOffs);
3683         lastLJ = (lastIG == jmp->idjIG) ? jmp : nullptr;
3684
3685         assert(lastIG == nullptr || lastIG->igNum <= jmp->idjIG->igNum || jmp->idjIG == prologIG ||
3686                emitNxtIGnum > unsigned(0xFFFF)); // igNum might overflow
3687         lastIG = jmp->idjIG;
3688 #endif // DEBUG
3689
3690         /* Get hold of the current jump size */
3691
3692         jsz = emitSizeOfJump(jmp);
3693
3694         /* Get the group the jump is in */
3695
3696         jmpIG = jmp->idjIG;
3697
3698         /* Are we in a group different from the previous jump? */
3699
3700         if (lstIG != jmpIG)
3701         {
3702             /* Were there any jumps before this one? */
3703
3704             if (lstIG)
3705             {
3706                 /* Adjust the offsets of the intervening blocks */
3707
3708                 do
3709                 {
3710                     lstIG = lstIG->igNext;
3711                     assert(lstIG);
3712                     // printf("Adjusted offset of block %02u from %04X to %04X\n", lstIG->igNum, lstIG->igOffs,
3713                     // lstIG->igOffs - adjIG);
3714                     lstIG->igOffs -= adjIG;
3715                     assert(IsCodeAligned(lstIG->igOffs));
3716                 } while (lstIG != jmpIG);
3717             }
3718
3719             /* We've got the first jump in a new group */
3720
3721             adjLJ = 0;
3722             lstIG = jmpIG;
3723         }
3724
3725         /* Apply any local size adjustment to the jump's relative offset */
3726
3727         jmp->idjOffs -= adjLJ;
3728
3729         // If this is a jump via register, the instruction size does not change, so we are done.
3730         CLANG_FORMAT_COMMENT_ANCHOR;
3731
3732 #if defined(_TARGET_ARM64_)
3733         // JIT code and data will be allocated together for arm64 so the relative offset to JIT data is known.
3734         // In case such offset can be encodeable for `ldr` (+-1MB), shorten it.
3735         if (jmp->idAddr()->iiaIsJitDataOffset())
3736         {
3737             // Reference to JIT data
3738             assert(jmp->idIsBound());
3739             UNATIVE_OFFSET srcOffs = jmpIG->igOffs + jmp->idjOffs;
3740
3741             int doff = jmp->idAddr()->iiaGetJitDataOffset();
3742             assert(doff >= 0);
3743             ssize_t imm = emitGetInsSC(jmp);
3744             assert((imm >= 0) && (imm < 0x1000)); // 0x1000 is arbitrary, currently 'imm' is always 0
3745
3746             unsigned dataOffs = (unsigned)(doff + imm);
3747             assert(dataOffs < emitDataSize());
3748
3749             // Conservately assume JIT data starts after the entire code size.
3750             // TODO-ARM64: we might consider only hot code size which will be computed later in emitComputeCodeSizes().
3751             assert(emitTotalCodeSize > 0);
3752             UNATIVE_OFFSET maxDstOffs = emitTotalCodeSize + dataOffs;
3753
3754             // Check if the distance is within the encoding length.
3755             jmpDist = maxDstOffs - srcOffs;
3756             extra   = jmpDist - psd;
3757             if (extra <= 0)
3758             {
3759                 goto SHORT_JMP;
3760             }
3761
3762             // Keep the large form.
3763             continue;
3764         }
3765 #endif
3766
3767         /* Have we bound this jump's target already? */
3768
3769         if (jmp->idIsBound())
3770         {
3771             /* Does the jump already have the smallest size? */
3772
3773             if (jmp->idjShort)
3774             {
3775                 assert(emitSizeOfJump(jmp) == ssz);
3776
3777                 // We should not be jumping/branching across funclets/functions
3778                 emitCheckFuncletBranch(jmp, jmpIG);
3779
3780                 continue;
3781             }
3782
3783             tgtIG = jmp->idAddr()->iiaIGlabel;
3784         }
3785         else
3786         {
3787             /* First time we've seen this label, convert its target */
3788             CLANG_FORMAT_COMMENT_ANCHOR;
3789
3790 #ifdef DEBUG
3791             if (EMITVERBOSE)
3792             {
3793                 printf("Binding: ");
3794                 emitDispIns(jmp, false, false, false);
3795                 printf("Binding L_M%03u_BB%02u ", Compiler::s_compMethodsCount, jmp->idAddr()->iiaBBlabel->bbNum);
3796             }
3797 #endif // DEBUG
3798
3799             tgtIG = (insGroup*)emitCodeGetCookie(jmp->idAddr()->iiaBBlabel);
3800
3801 #ifdef DEBUG
3802             if (EMITVERBOSE)
3803             {
3804                 if (tgtIG)
3805                 {
3806                     printf("to G_M%03u_IG%02u\n", Compiler::s_compMethodsCount, tgtIG->igNum);
3807                 }
3808                 else
3809                 {
3810                     printf("-- ERROR, no emitter cookie for BB%02u; it is probably missing BBF_JMP_TARGET or "
3811                            "BBF_HAS_LABEL.\n",
3812                            jmp->idAddr()->iiaBBlabel->bbNum);
3813                 }
3814             }
3815             assert(tgtIG);
3816 #endif // DEBUG
3817
3818             /* Record the bound target */
3819
3820             jmp->idAddr()->iiaIGlabel = tgtIG;
3821             jmp->idSetIsBound();
3822         }
3823
3824         // We should not be jumping/branching across funclets/functions
3825         emitCheckFuncletBranch(jmp, jmpIG);
3826
3827 #ifdef _TARGET_XARCH_
3828         /* Done if this is not a variable-sized jump */
3829
3830         if ((jmp->idIns() == INS_push) || (jmp->idIns() == INS_mov) || (jmp->idIns() == INS_call) ||
3831             (jmp->idIns() == INS_push_hide))
3832         {
3833             continue;
3834         }
3835 #endif
3836 #ifdef _TARGET_ARM_
3837         if ((jmp->idIns() == INS_push) || (jmp->idIns() == INS_mov) || (jmp->idIns() == INS_movt) ||
3838             (jmp->idIns() == INS_movw))
3839         {
3840             continue;
3841         }
3842 #endif
3843 #ifdef _TARGET_ARM64_
3844         // There is only one size of unconditional branch; we don't support functions larger than 2^28 bytes (our branch
3845         // range).
3846         if (emitIsUncondJump(jmp))
3847         {
3848             continue;
3849         }
3850 #endif
3851
3852         /*
3853             In the following distance calculations, if we're not actually
3854             scheduling the code (i.e. reordering instructions), we can
3855             use the actual offset of the jump (rather than the beg/end of
3856             the instruction group) since the jump will not be moved around
3857             and thus its offset is accurate.
3858
3859             First we need to figure out whether this jump is a forward or
3860             backward one; to do this we simply look at the ordinals of the
3861             group that contains the jump and the target.
3862          */
3863
3864         srcInstrOffs = jmpIG->igOffs + jmp->idjOffs;
3865
3866         /* Note that the destination is always the beginning of an IG, so no need for an offset inside it */
3867         dstOffs = tgtIG->igOffs;
3868
3869 #if defined(_TARGET_ARM_)
3870         srcEncodingOffs =
3871             srcInstrOffs + 4; // For relative branches, ARM PC is always considered to be the instruction address + 4
3872 #elif defined(_TARGET_ARM64_)
3873         srcEncodingOffs =
3874             srcInstrOffs; // For relative branches, ARM64 PC is always considered to be the instruction address
3875 #else
3876         srcEncodingOffs = srcInstrOffs + ssz; // Encoding offset of relative offset for small branch
3877 #endif
3878
3879         if (jmpIG->igNum < tgtIG->igNum)
3880         {
3881             /* Forward jump */
3882
3883             /* Adjust the target offset by the current delta. This is a worst-case estimate, as jumps between
3884                here and the target could be shortened, causing the actual distance to shrink.
3885              */
3886
3887             dstOffs -= adjIG;
3888
3889             /* Compute the distance estimate */
3890
3891             jmpDist = dstOffs - srcEncodingOffs;
3892
3893             /* How much beyond the max. short distance does the jump go? */
3894
3895             extra = jmpDist - psd;
3896
3897 #if DEBUG_EMIT
3898             assert(jmp->idDebugOnlyInfo() != nullptr);
3899             if (jmp->idDebugOnlyInfo()->idNum == (unsigned)INTERESTING_JUMP_NUM || INTERESTING_JUMP_NUM == 0)
3900             {
3901                 if (INTERESTING_JUMP_NUM == 0)
3902                 {
3903                     printf("[1] Jump %u:\n", jmp->idDebugOnlyInfo()->idNum);
3904                 }
3905                 printf("[1] Jump  block is at %08X\n", jmpIG->igOffs);
3906                 printf("[1] Jump reloffset is %04X\n", jmp->idjOffs);
3907                 printf("[1] Jump source is at %08X\n", srcEncodingOffs);
3908                 printf("[1] Label block is at %08X\n", dstOffs);
3909                 printf("[1] Jump  dist. is    %04X\n", jmpDist);
3910                 if (extra > 0)
3911                 {
3912                     printf("[1] Dist excess [S] = %d  \n", extra);
3913                 }
3914             }
3915             if (EMITVERBOSE)
3916             {
3917                 printf("Estimate of fwd jump [%08X/%03u]: %04X -> %04X = %04X\n", dspPtr(jmp),
3918                        jmp->idDebugOnlyInfo()->idNum, srcInstrOffs, dstOffs, jmpDist);
3919             }
3920 #endif // DEBUG_EMIT
3921
3922             if (extra <= 0)
3923             {
3924                 /* This jump will be a short one */
3925                 goto SHORT_JMP;
3926             }
3927         }
3928         else
3929         {
3930             /* Backward jump */
3931
3932             /* Compute the distance estimate */
3933
3934             jmpDist = srcEncodingOffs - dstOffs;
3935
3936             /* How much beyond the max. short distance does the jump go? */
3937
3938             extra = jmpDist + nsd;
3939
3940 #if DEBUG_EMIT
3941             assert(jmp->idDebugOnlyInfo() != nullptr);
3942             if (jmp->idDebugOnlyInfo()->idNum == (unsigned)INTERESTING_JUMP_NUM || INTERESTING_JUMP_NUM == 0)
3943             {
3944                 if (INTERESTING_JUMP_NUM == 0)
3945                 {
3946                     printf("[2] Jump %u:\n", jmp->idDebugOnlyInfo()->idNum);
3947                 }
3948                 printf("[2] Jump  block is at %08X\n", jmpIG->igOffs);
3949                 printf("[2] Jump reloffset is %04X\n", jmp->idjOffs);
3950                 printf("[2] Jump source is at %08X\n", srcEncodingOffs);
3951                 printf("[2] Label block is at %08X\n", dstOffs);
3952                 printf("[2] Jump  dist. is    %04X\n", jmpDist);
3953                 if (extra > 0)
3954                 {
3955                     printf("[2] Dist excess [S] = %d  \n", extra);
3956                 }
3957             }
3958             if (EMITVERBOSE)
3959             {
3960                 printf("Estimate of bwd jump [%08X/%03u]: %04X -> %04X = %04X\n", dspPtr(jmp),
3961                        jmp->idDebugOnlyInfo()->idNum, srcInstrOffs, dstOffs, jmpDist);
3962             }
3963 #endif // DEBUG_EMIT
3964
3965             if (extra <= 0)
3966             {
3967                 /* This jump will be a short one */
3968                 goto SHORT_JMP;
3969             }
3970         }
3971
3972         /* We arrive here if the jump couldn't be made short, at least for now */
3973
3974         /* We had better not have eagerly marked the jump as short
3975          * in emitIns_J(). If we did, then it has to be able to stay short
3976          * as emitIns_J() uses the worst case scenario, and blocks can
3977          * only move closer together after that.
3978          */
3979         assert(jmp->idjShort == 0);
3980
3981         /* Keep track of the closest distance we got */
3982
3983         if (minShortExtra > (unsigned)extra)
3984         {
3985             minShortExtra = (unsigned)extra;
3986         }
3987
3988 #if defined(_TARGET_ARM_)
3989
3990         // If we're here, we couldn't convert to a small jump.
3991         // Handle conversion to medium-sized conditional jumps.
3992         // 'srcInstrOffs', 'srcEncodingOffs', 'dstOffs', 'jmpDist' have already been computed
3993         // and don't need to be recomputed.
3994
3995         if (emitIsCondJump(jmp))
3996         {
3997             if (jmpIG->igNum < tgtIG->igNum)
3998             {
3999                 /* Forward jump */
4000
4001                 /* How much beyond the max. medium distance does the jump go? */
4002
4003                 mextra = jmpDist - pmd;
4004
4005 #if DEBUG_EMIT
4006                 assert(jmp->idDebugOnlyInfo() != NULL);
4007                 if (jmp->idDebugOnlyInfo()->idNum == (unsigned)INTERESTING_JUMP_NUM || INTERESTING_JUMP_NUM == 0)
4008                 {
4009                     if (mextra > 0)
4010                     {
4011                         if (INTERESTING_JUMP_NUM == 0)
4012                             printf("[6] Jump %u:\n", jmp->idDebugOnlyInfo()->idNum);
4013                         printf("[6] Dist excess [S] = %d  \n", mextra);
4014                     }
4015                 }
4016 #endif // DEBUG_EMIT
4017
4018                 if (mextra <= 0)
4019                 {
4020                     /* This jump will be a medium one */
4021                     goto MEDIUM_JMP;
4022                 }
4023             }
4024             else
4025             {
4026                 /* Backward jump */
4027
4028                 /* How much beyond the max. medium distance does the jump go? */
4029
4030                 mextra = jmpDist + nmd;
4031
4032 #if DEBUG_EMIT
4033                 assert(jmp->idDebugOnlyInfo() != NULL);
4034                 if (jmp->idDebugOnlyInfo()->idNum == (unsigned)INTERESTING_JUMP_NUM || INTERESTING_JUMP_NUM == 0)
4035                 {
4036                     if (mextra > 0)
4037                     {
4038                         if (INTERESTING_JUMP_NUM == 0)
4039                             printf("[7] Jump %u:\n", jmp->idDebugOnlyInfo()->idNum);
4040                         printf("[7] Dist excess [S] = %d  \n", mextra);
4041                     }
4042                 }
4043 #endif // DEBUG_EMIT
4044
4045                 if (mextra <= 0)
4046                 {
4047                     /* This jump will be a medium one */
4048                     goto MEDIUM_JMP;
4049                 }
4050             }
4051
4052             /* We arrive here if the jump couldn't be made medium, at least for now */
4053
4054             /* Keep track of the closest distance we got */
4055
4056             if (minMediumExtra > (unsigned)mextra)
4057                 minMediumExtra = (unsigned)mextra;
4058         }
4059
4060 #endif // _TARGET_ARM_
4061
4062         /*****************************************************************************
4063          * We arrive here if the jump must stay long, at least for now.
4064          * Go try the next one.
4065          */
4066
4067         continue;
4068
4069     /*****************************************************************************/
4070     /* Handle conversion to short jump                                           */
4071     /*****************************************************************************/
4072
4073     SHORT_JMP:
4074
4075         /* Try to make this jump a short one */
4076
4077         emitSetShortJump(jmp);
4078
4079         if (!jmp->idjShort)
4080         {
4081             continue; // This jump must be kept long
4082         }
4083
4084         /* This jump is becoming either short or medium */
4085
4086         oldSize = jsz;
4087         jsz     = ssz;
4088         assert(oldSize >= jsz);
4089         sizeDif = oldSize - jsz;
4090
4091 #if defined(_TARGET_XARCH_)
4092         jmp->idCodeSize(jsz);
4093 #elif defined(_TARGET_ARM_)
4094 #if 0
4095         // This is done as part of emitSetShortJump():
4096         insSize isz = emitInsSize(jmp->idInsFmt());
4097         jmp->idInsSize(isz);
4098 #endif
4099 #elif defined(_TARGET_ARM64_)
4100         // The size of IF_LARGEJMP/IF_LARGEADR/IF_LARGELDC are 8 or 12.
4101         // All other code size is 4.
4102         assert((sizeDif == 4) || (sizeDif == 8));
4103 #else
4104 #error Unsupported or unset target architecture
4105 #endif
4106
4107         goto NEXT_JMP;
4108
4109 #if defined(_TARGET_ARM_)
4110
4111     /*****************************************************************************/
4112     /* Handle conversion to medium jump                                          */
4113     /*****************************************************************************/
4114
4115     MEDIUM_JMP:
4116
4117         /* Try to make this jump a medium one */
4118
4119         emitSetMediumJump(jmp);
4120
4121         if (jmp->idCodeSize() > msz)
4122         {
4123             continue; // This jump wasn't shortened
4124         }
4125         assert(jmp->idCodeSize() == msz);
4126
4127         /* This jump is becoming medium */
4128
4129         oldSize = jsz;
4130         jsz     = msz;
4131         assert(oldSize >= jsz);
4132         sizeDif = oldSize - jsz;
4133
4134         goto NEXT_JMP;
4135
4136 #endif // _TARGET_ARM_
4137
4138     /*****************************************************************************/
4139
4140     NEXT_JMP:
4141
4142         /* Make sure the size of the jump is marked correctly */
4143
4144         assert((0 == (jsz | jmpDist)) || (jsz == emitSizeOfJump(jmp)));
4145
4146 #ifdef DEBUG
4147         if (EMITVERBOSE)
4148         {
4149             printf("Shrinking jump [%08X/%03u]\n", dspPtr(jmp), jmp->idDebugOnlyInfo()->idNum);
4150         }
4151 #endif
4152         noway_assert((unsigned short)sizeDif == sizeDif);
4153
4154         adjIG += sizeDif;
4155         adjLJ += sizeDif;
4156         jmpIG->igSize -= (unsigned short)sizeDif;
4157         emitTotalCodeSize -= sizeDif;
4158
4159         /* The jump size estimate wasn't accurate; flag its group */
4160
4161         jmpIG->igFlags |= IGF_UPD_ISZ;
4162
4163     } // end for each jump
4164
4165     /* Did we shorten any jumps? */
4166
4167     if (adjIG)
4168     {
4169         /* Adjust offsets of any remaining blocks */
4170
4171         assert(lstIG);
4172
4173         for (;;)
4174         {
4175             lstIG = lstIG->igNext;
4176             if (!lstIG)
4177             {
4178                 break;
4179             }
4180             // printf("Adjusted offset of block %02u from %04X to %04X\n", lstIG->igNum, lstIG->igOffs,
4181             // lstIG->igOffs - adjIG);
4182             lstIG->igOffs -= adjIG;
4183             assert(IsCodeAligned(lstIG->igOffs));
4184         }
4185
4186 #ifdef DEBUG
4187         emitCheckIGoffsets();
4188 #endif
4189
4190         /* Is there a chance of other jumps becoming short? */
4191         CLANG_FORMAT_COMMENT_ANCHOR;
4192 #ifdef DEBUG
4193 #if defined(_TARGET_ARM_)
4194         if (EMITVERBOSE)
4195             printf("Total shrinkage = %3u, min extra short jump size = %3u, min extra medium jump size = %u\n", adjIG,
4196                    minShortExtra, minMediumExtra);
4197 #else
4198         if (EMITVERBOSE)
4199         {
4200             printf("Total shrinkage = %3u, min extra jump size = %3u\n", adjIG, minShortExtra);
4201         }
4202 #endif
4203 #endif
4204
4205         if ((minShortExtra <= adjIG)
4206 #if defined(_TARGET_ARM_)
4207             || (minMediumExtra <= adjIG)
4208 #endif // _TARGET_ARM_
4209                 )
4210         {
4211             jmp_iteration++;
4212
4213 #ifdef DEBUG
4214             if (EMITVERBOSE)
4215             {
4216                 printf("Iterating branch shortening. Iteration = %d\n", jmp_iteration);
4217             }
4218 #endif
4219
4220             goto AGAIN;
4221         }
4222     }
4223 }
4224
4225 void emitter::emitCheckFuncletBranch(instrDesc* jmp, insGroup* jmpIG)
4226 {
4227 #ifdef DEBUG
4228     // We should not be jumping/branching across funclets/functions
4229     // Except possibly a 'call' to a finally funclet for a local unwind
4230     // or a 'return' from a catch handler (that can go just about anywhere)
4231     // This routine attempts to validate that any branches across funclets
4232     // meets one of those criteria...
4233     assert(jmp->idIsBound());
4234
4235 #ifdef _TARGET_XARCH_
4236     // An lea of a code address (for constant data stored with the code)
4237     // is treated like a jump for emission purposes but is not really a jump so
4238     // we don't have to check anything here.
4239     if (jmp->idIns() == INS_lea)
4240     {
4241         return;
4242     }
4243 #endif
4244
4245 #ifdef _TARGET_ARMARCH_
4246     if (jmp->idAddr()->iiaHasInstrCount())
4247     {
4248         // Too hard to figure out funclets from just an instruction count
4249         // You're on your own!
4250         return;
4251     }
4252 #endif // _TARGET_ARMARCH_
4253
4254 #ifdef _TARGET_ARM64_
4255     // No interest if it's not jmp.
4256     if (emitIsLoadLabel(jmp) || emitIsLoadConstant(jmp))
4257     {
4258         return;
4259     }
4260 #endif // _TARGET_ARM64_
4261
4262     insGroup* tgtIG = jmp->idAddr()->iiaIGlabel;
4263     assert(tgtIG);
4264     if (tgtIG->igFuncIdx != jmpIG->igFuncIdx)
4265     {
4266         if (jmp->idDebugOnlyInfo()->idFinallyCall)
4267         {
4268             // We don't record enough information to determine this accurately, so instead
4269             // we assume that any branch to the very start of a finally is OK.
4270
4271             // No branches back to the root method
4272             assert(tgtIG->igFuncIdx > 0);
4273             FuncInfoDsc* tgtFunc = emitComp->funGetFunc(tgtIG->igFuncIdx);
4274             assert(tgtFunc->funKind == FUNC_HANDLER);
4275             EHblkDsc* tgtEH = emitComp->ehGetDsc(tgtFunc->funEHIndex);
4276
4277             // Only branches to finallys (not faults, catches, filters, etc.)
4278             assert(tgtEH->HasFinallyHandler());
4279
4280             // Only to the first block of the finally (which is properly marked)
4281             BasicBlock* tgtBlk = tgtEH->ebdHndBeg;
4282             assert(tgtBlk->bbFlags & BBF_FUNCLET_BEG);
4283
4284             // And now we made it back to where we started
4285             assert(tgtIG == emitCodeGetCookie(tgtBlk));
4286             assert(tgtIG->igFuncIdx == emitComp->funGetFuncIdx(tgtBlk));
4287         }
4288         else if (jmp->idDebugOnlyInfo()->idCatchRet)
4289         {
4290             // Again there isn't enough information to prove this correct
4291             // so just allow a 'branch' to any other 'parent' funclet
4292
4293             FuncInfoDsc* jmpFunc = emitComp->funGetFunc(jmpIG->igFuncIdx);
4294             assert(jmpFunc->funKind == FUNC_HANDLER);
4295             EHblkDsc* jmpEH = emitComp->ehGetDsc(jmpFunc->funEHIndex);
4296
4297             // Only branches out of catches
4298             assert(jmpEH->HasCatchHandler());
4299
4300             FuncInfoDsc* tgtFunc = emitComp->funGetFunc(tgtIG->igFuncIdx);
4301             assert(tgtFunc);
4302             if (tgtFunc->funKind == FUNC_HANDLER)
4303             {
4304                 // An outward chain to the containing funclet/EH handler
4305                 // Note that it might be anywhere within nested try bodies
4306                 assert(jmpEH->ebdEnclosingHndIndex == tgtFunc->funEHIndex);
4307             }
4308             else
4309             {
4310                 // This funclet is 'top level' and so it is branching back to the
4311                 // root function, and should have no containing EH handlers
4312                 // but it could be nested within try bodies...
4313                 assert(tgtFunc->funKind == FUNC_ROOT);
4314                 assert(jmpEH->ebdEnclosingHndIndex == EHblkDsc::NO_ENCLOSING_INDEX);
4315             }
4316         }
4317         else
4318         {
4319             printf("Hit an illegal branch between funclets!");
4320             assert(tgtIG->igFuncIdx == jmpIG->igFuncIdx);
4321         }
4322     }
4323 #endif // DEBUG
4324 }
4325
4326 /*****************************************************************************
4327  *
4328  *  Compute the code sizes that we're going to use to allocate the code buffers.
4329  *
4330  *  This sets:
4331  *
4332  *      emitTotalHotCodeSize
4333  *      emitTotalColdCodeSize
4334  *      Compiler::info.compTotalHotCodeSize
4335  *      Compiler::info.compTotalColdCodeSize
4336  */
4337
4338 void emitter::emitComputeCodeSizes()
4339 {
4340     assert((emitComp->fgFirstColdBlock == nullptr) == (emitFirstColdIG == nullptr));
4341
4342     if (emitFirstColdIG)
4343     {
4344         emitTotalHotCodeSize  = emitFirstColdIG->igOffs;
4345         emitTotalColdCodeSize = emitTotalCodeSize - emitTotalHotCodeSize;
4346     }
4347     else
4348     {
4349         emitTotalHotCodeSize  = emitTotalCodeSize;
4350         emitTotalColdCodeSize = 0;
4351     }
4352
4353     emitComp->info.compTotalHotCodeSize  = emitTotalHotCodeSize;
4354     emitComp->info.compTotalColdCodeSize = emitTotalColdCodeSize;
4355
4356 #ifdef DEBUG
4357     if (emitComp->verbose)
4358     {
4359         printf("\nHot  code size = 0x%X bytes\n", emitTotalHotCodeSize);
4360         printf("Cold code size = 0x%X bytes\n", emitTotalColdCodeSize);
4361     }
4362 #endif
4363 }
4364
4365 /*****************************************************************************
4366  *
4367  *  Called at the end of code generation, this method creates the code, data
4368  *  and GC info blocks for the method.  Returns the size of the method (which must fit in an unsigned).
4369  */
4370
4371 unsigned emitter::emitEndCodeGen(Compiler* comp,
4372                                  bool      contTrkPtrLcls,
4373                                  bool      fullyInt,
4374                                  bool      fullPtrMap,
4375                                  bool      returnsGCr,
4376                                  unsigned  xcptnsCount,
4377                                  unsigned* prologSize,
4378                                  unsigned* epilogSize,
4379                                  void**    codeAddr,
4380                                  void**    coldCodeAddr,
4381                                  void**    consAddr)
4382 {
4383 #ifdef DEBUG
4384     if (emitComp->verbose)
4385     {
4386         printf("*************** In emitEndCodeGen()\n");
4387     }
4388 #endif
4389
4390     insGroup* ig;
4391
4392     BYTE* consBlock;
4393     BYTE* codeBlock;
4394     BYTE* coldCodeBlock;
4395     BYTE* cp;
4396
4397     assert(emitCurIG == nullptr);
4398
4399     emitCodeBlock = nullptr;
4400     emitConsBlock = nullptr;
4401
4402     /* Tell everyone whether we have fully interruptible code or not */
4403
4404     emitFullyInt   = fullyInt;
4405     emitFullGCinfo = fullPtrMap;
4406
4407 #ifndef UNIX_X86_ABI
4408     emitFullArgInfo = !emitHasFramePtr;
4409 #else
4410     emitFullArgInfo = fullPtrMap;
4411 #endif
4412
4413 #if EMITTER_STATS
4414     GCrefsTable.record(emitGCrFrameOffsCnt);
4415     emitSizeTable.record(static_cast<unsigned>(emitSizeMethod));
4416     stkDepthTable.record(emitMaxStackDepth);
4417 #endif // EMITTER_STATS
4418
4419     // Default values, correct even if EMIT_TRACK_STACK_DEPTH is 0.
4420     emitSimpleStkUsed         = true;
4421     u1.emitSimpleStkMask      = 0;
4422     u1.emitSimpleByrefStkMask = 0;
4423
4424 #if EMIT_TRACK_STACK_DEPTH
4425     /* Convert max. stack depth from # of bytes to # of entries */
4426
4427     unsigned maxStackDepthIn4ByteElements = emitMaxStackDepth / sizeof(int);
4428     JITDUMP("Converting emitMaxStackDepth from bytes (%d) to elements (%d)\n", emitMaxStackDepth,
4429             maxStackDepthIn4ByteElements);
4430     emitMaxStackDepth = maxStackDepthIn4ByteElements;
4431
4432     /* Should we use the simple stack */
4433
4434     if (emitMaxStackDepth > MAX_SIMPLE_STK_DEPTH || emitFullGCinfo)
4435     {
4436         /* We won't use the "simple" argument table */
4437
4438         emitSimpleStkUsed = false;
4439
4440         /* Allocate the argument tracking table */
4441
4442         if (emitMaxStackDepth <= sizeof(u2.emitArgTrackLcl))
4443         {
4444             u2.emitArgTrackTab = (BYTE*)u2.emitArgTrackLcl;
4445         }
4446         else
4447         {
4448             u2.emitArgTrackTab = (BYTE*)emitGetMem(roundUp(emitMaxStackDepth));
4449         }
4450
4451         u2.emitArgTrackTop   = u2.emitArgTrackTab;
4452         u2.emitGcArgTrackCnt = 0;
4453     }
4454 #endif
4455
4456     if (emitEpilogCnt == 0)
4457     {
4458         /* No epilogs, make sure the epilog size is set to 0 */
4459
4460         emitEpilogSize = 0;
4461
4462 #ifdef _TARGET_XARCH_
4463         emitExitSeqSize = 0;
4464 #endif // _TARGET_XARCH_
4465     }
4466
4467     /* Return the size of the epilog to the caller */
4468
4469     *epilogSize = emitEpilogSize;
4470
4471 #ifdef _TARGET_XARCH_
4472     *epilogSize += emitExitSeqSize;
4473 #endif // _TARGET_XARCH_
4474
4475 #ifdef DEBUG
4476     if (EMIT_INSTLIST_VERBOSE)
4477     {
4478         printf("\nInstruction list before instruction issue:\n\n");
4479         emitDispIGlist(true);
4480     }
4481
4482     emitCheckIGoffsets();
4483 #endif
4484
4485     /* Allocate the code block (and optionally the data blocks) */
4486
4487     // If we're doing procedure splitting and we found cold blocks, then
4488     // allocate hot and cold buffers.  Otherwise only allocate a hot
4489     // buffer.
4490
4491     coldCodeBlock = nullptr;
4492
4493     CorJitAllocMemFlag allocMemFlag = CORJIT_ALLOCMEM_DEFAULT_CODE_ALIGN;
4494
4495 #ifdef _TARGET_X86_
4496     //
4497     // These are the heuristics we use to decide whether or not to force the
4498     // code to be 16-byte aligned.
4499     //
4500     // 1. For ngen code with IBC data, use 16-byte alignment if the method
4501     //    has been called more than BB_VERY_HOT_WEIGHT times.
4502     // 2. For JITed code and ngen code without IBC data, use 16-byte alignment
4503     //    when the code is 16 bytes or smaller. We align small getters/setters
4504     //    because of they are penalized heavily on certain hardware when not 16-byte
4505     //    aligned (VSWhidbey #373938). To minimize size impact of this optimization,
4506     //    we do not align large methods because of the penalty is amortized for them.
4507     //
4508     if (emitComp->fgHaveProfileData())
4509     {
4510         if (emitComp->fgCalledCount > (BB_VERY_HOT_WEIGHT * emitComp->fgProfileRunsCount()))
4511         {
4512             allocMemFlag = CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN;
4513         }
4514     }
4515     else
4516     {
4517         if (emitTotalHotCodeSize <= 16)
4518         {
4519             allocMemFlag = CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN;
4520         }
4521     }
4522 #endif
4523
4524 #ifdef _TARGET_ARM64_
4525     // For arm64, we want to allocate JIT data always adjacent to code similar to what native compiler does.
4526     // This way allows us to use a single `ldr` to access such data like float constant/jmp table.
4527     if (emitTotalColdCodeSize > 0)
4528     {
4529         // JIT data might be far away from the cold code.
4530         NYI_ARM64("Need to handle fix-up to data from cold code.");
4531     }
4532
4533     UNATIVE_OFFSET roDataAlignmentDelta = 0;
4534     if (emitConsDsc.dsdOffs)
4535     {
4536         UNATIVE_OFFSET roDataAlignment = sizeof(void*); // 8 Byte align by default.
4537         roDataAlignmentDelta = (UNATIVE_OFFSET)ALIGN_UP(emitTotalHotCodeSize, roDataAlignment) - emitTotalHotCodeSize;
4538         assert((roDataAlignmentDelta == 0) || (roDataAlignmentDelta == 4));
4539     }
4540     emitCmpHandle->allocMem(emitTotalHotCodeSize + roDataAlignmentDelta + emitConsDsc.dsdOffs, emitTotalColdCodeSize, 0,
4541                             xcptnsCount, allocMemFlag, (void**)&codeBlock, (void**)&coldCodeBlock, (void**)&consBlock);
4542
4543     consBlock = codeBlock + emitTotalHotCodeSize + roDataAlignmentDelta;
4544
4545 #else
4546     emitCmpHandle->allocMem(emitTotalHotCodeSize, emitTotalColdCodeSize, emitConsDsc.dsdOffs, xcptnsCount, allocMemFlag,
4547                             (void**)&codeBlock, (void**)&coldCodeBlock, (void**)&consBlock);
4548 #endif
4549
4550     // if (emitConsDsc.dsdOffs)
4551     //     printf("Cons=%08X\n", consBlock);
4552
4553     /* Give the block addresses to the caller and other functions here */
4554
4555     *codeAddr = emitCodeBlock = codeBlock;
4556     *coldCodeAddr = emitColdCodeBlock = coldCodeBlock;
4557     *consAddr = emitConsBlock = consBlock;
4558
4559     /* Nothing has been pushed on the stack */
4560     CLANG_FORMAT_COMMENT_ANCHOR;
4561
4562 #if EMIT_TRACK_STACK_DEPTH
4563     emitCurStackLvl = 0;
4564 #endif
4565
4566     /* Assume no live GC ref variables on entry */
4567
4568     VarSetOps::OldStyleClearD(emitComp, emitThisGCrefVars); // This is initialized to Empty at the start of codegen.
4569     emitThisGCrefRegs = emitThisByrefRegs = RBM_NONE;
4570     emitThisGCrefVset                     = true;
4571
4572 #ifdef DEBUG
4573
4574     emitIssuing = true;
4575
4576     // We don't use these after this point
4577
4578     VarSetOps::AssignNoCopy(emitComp, emitPrevGCrefVars, VarSetOps::UninitVal());
4579     emitPrevGCrefRegs = emitPrevByrefRegs = 0xBAADFEED;
4580
4581     VarSetOps::AssignNoCopy(emitComp, emitInitGCrefVars, VarSetOps::UninitVal());
4582     emitInitGCrefRegs = emitInitByrefRegs = 0xBAADFEED;
4583
4584 #endif
4585
4586     /* Initialize the GC ref variable lifetime tracking logic */
4587
4588     codeGen->gcInfo.gcVarPtrSetInit();
4589
4590     emitSyncThisObjOffs = -1;     /* -1  means no offset set */
4591     emitSyncThisObjReg  = REG_NA; /* REG_NA  means not set */
4592
4593 #ifdef JIT32_GCENCODER
4594     if (emitComp->lvaKeepAliveAndReportThis())
4595     {
4596         assert(emitComp->lvaIsOriginalThisArg(0));
4597         LclVarDsc* thisDsc = &emitComp->lvaTable[0];
4598
4599         /* If "this" (which is passed in as a register argument in REG_ARG_0)
4600            is enregistered, we normally spot the "mov REG_ARG_0 -> thisReg"
4601            in the prolog and note the location of "this" at that point.
4602            However, if 'this' is enregistered into REG_ARG_0 itself, no code
4603            will be generated in the prolog, so we explicitly need to note
4604            the location of "this" here.
4605            NOTE that we can do this even if "this" is not enregistered in
4606            REG_ARG_0, and it will result in more accurate "this" info over the
4607            prolog. However, as methods are not interruptible over the prolog,
4608            we try to save space by avoiding that.
4609          */
4610
4611         if (thisDsc->lvRegister)
4612         {
4613             emitSyncThisObjReg = thisDsc->lvRegNum;
4614
4615             if (emitSyncThisObjReg == (int)REG_ARG_0 &&
4616                 (codeGen->intRegState.rsCalleeRegArgMaskLiveIn & genRegMask(REG_ARG_0)))
4617             {
4618                 if (emitFullGCinfo)
4619                 {
4620                     emitGCregLiveSet(GCT_GCREF, genRegMask(REG_ARG_0),
4621                                      emitCodeBlock, // from offset 0
4622                                      true);
4623                 }
4624                 else
4625                 {
4626                     /* If emitFullGCinfo==false, the we don't use any
4627                        regPtrDsc's and so explictly note the location
4628                        of "this" in GCEncode.cpp
4629                      */
4630                 }
4631             }
4632         }
4633     }
4634 #endif // JIT32_GCENCODER
4635
4636     emitContTrkPtrLcls = contTrkPtrLcls;
4637
4638     /* Are there any GC ref variables on the stack? */
4639
4640     if (emitGCrFrameOffsCnt)
4641     {
4642         size_t     siz;
4643         unsigned   cnt;
4644         unsigned   num;
4645         LclVarDsc* dsc;
4646         int*       tab;
4647
4648         /* Allocate and clear emitGCrFrameLiveTab[]. This is the table
4649            mapping "stkOffs -> varPtrDsc". It holds a pointer to
4650            the liveness descriptor that was created when the
4651            variable became alive. When the variable becomes dead, the
4652            descriptor will be appended to the liveness descriptor list, and
4653            the entry in emitGCrFrameLiveTab[] will be made NULL.
4654
4655            Note that if all GC refs are assigned consecutively,
4656            emitGCrFrameLiveTab[] can be only as big as the number of GC refs
4657            present, instead of lvaTrackedCount.
4658          */
4659
4660         siz                 = emitGCrFrameOffsCnt * sizeof(*emitGCrFrameLiveTab);
4661         emitGCrFrameLiveTab = (varPtrDsc**)emitGetMem(roundUp(siz));
4662         memset(emitGCrFrameLiveTab, 0, siz);
4663
4664         /* Allocate and fill in emitGCrFrameOffsTab[]. This is the table
4665            mapping "varIndex -> stkOffs".
4666            Non-ptrs or reg vars have entries of -1.
4667            Entries of Tracked stack byrefs have the lower bit set to 1.
4668         */
4669
4670         emitTrkVarCnt = cnt = emitComp->lvaTrackedCount;
4671         assert(cnt);
4672         emitGCrFrameOffsTab = tab = (int*)emitGetMem(cnt * sizeof(int));
4673
4674         memset(emitGCrFrameOffsTab, -1, cnt * sizeof(int));
4675
4676         /* Now fill in all the actual used entries */
4677
4678         for (num = 0, dsc = emitComp->lvaTable, cnt = emitComp->lvaCount; num < cnt; num++, dsc++)
4679         {
4680             if (!dsc->lvOnFrame || (dsc->lvIsParam && !dsc->lvIsRegArg))
4681             {
4682                 continue;
4683             }
4684
4685 #if FEATURE_FIXED_OUT_ARGS
4686             if (num == emitComp->lvaOutgoingArgSpaceVar)
4687             {
4688                 continue;
4689             }
4690 #endif // FEATURE_FIXED_OUT_ARGS
4691
4692             int offs = dsc->lvStkOffs;
4693
4694             /* Is it within the interesting range of offsets */
4695
4696             if (offs >= emitGCrFrameOffsMin && offs < emitGCrFrameOffsMax)
4697             {
4698                 /* Are tracked stack ptr locals laid out contiguously?
4699                    If not, skip non-ptrs. The emitter is optimized to work
4700                    with contiguous ptrs, but for EditNContinue, the variables
4701                    are laid out in the order they occur in the local-sig.
4702                  */
4703
4704                 if (!emitContTrkPtrLcls)
4705                 {
4706                     if (!emitComp->lvaIsGCTracked(dsc))
4707                     {
4708                         continue;
4709                     }
4710                 }
4711
4712                 unsigned indx = dsc->lvVarIndex;
4713
4714                 assert(!dsc->lvRegister);
4715                 assert(dsc->lvTracked);
4716                 assert(dsc->lvRefCnt != 0);
4717
4718                 assert(dsc->TypeGet() == TYP_REF || dsc->TypeGet() == TYP_BYREF);
4719
4720                 assert(indx < emitComp->lvaTrackedCount);
4721
4722 // printf("Variable #%2u/%2u is at stack offset %d\n", num, indx, offs);
4723
4724 #ifdef JIT32_GCENCODER
4725 #ifndef WIN64EXCEPTIONS
4726                 /* Remember the frame offset of the "this" argument for synchronized methods */
4727                 if (emitComp->lvaIsOriginalThisArg(num) && emitComp->lvaKeepAliveAndReportThis())
4728                 {
4729                     emitSyncThisObjOffs = offs;
4730                     offs |= this_OFFSET_FLAG;
4731                 }
4732 #endif
4733 #endif // JIT32_GCENCODER
4734
4735                 if (dsc->TypeGet() == TYP_BYREF)
4736                 {
4737                     offs |= byref_OFFSET_FLAG;
4738                 }
4739                 tab[indx] = offs;
4740             }
4741         }
4742     }
4743     else
4744     {
4745 #ifdef DEBUG
4746         emitTrkVarCnt       = 0;
4747         emitGCrFrameOffsTab = nullptr;
4748 #endif
4749     }
4750
4751 #ifdef DEBUG
4752     if (emitComp->verbose)
4753     {
4754         printf("\n***************************************************************************\n");
4755         printf("Instructions as they come out of the scheduler\n\n");
4756     }
4757 #endif
4758
4759     /* Issue all instruction groups in order */
4760     cp = codeBlock;
4761
4762 #define DEFAULT_CODE_BUFFER_INIT 0xcc
4763
4764     for (ig = emitIGlist; ig; ig = ig->igNext)
4765     {
4766         assert(!(ig->igFlags & IGF_PLACEHOLDER)); // There better not be any placeholder groups left
4767
4768         /* Is this the first cold block? */
4769         if (ig == emitFirstColdIG)
4770         {
4771             unsigned actualHotCodeSize = emitCurCodeOffs(cp);
4772
4773             /* Fill in eventual unused space */
4774             while (emitCurCodeOffs(cp) < emitTotalHotCodeSize)
4775             {
4776                 *cp++ = DEFAULT_CODE_BUFFER_INIT;
4777             }
4778
4779             assert(coldCodeBlock);
4780             cp = coldCodeBlock;
4781 #ifdef DEBUG
4782             if (emitComp->opts.disAsm || emitComp->opts.dspEmit || emitComp->verbose)
4783             {
4784                 printf("\n************** Beginning of cold code **************\n");
4785             }
4786 #endif
4787         }
4788
4789         /* Are we overflowing? */
4790         if (ig->igNext && ig->igNum + 1 != ig->igNext->igNum)
4791         {
4792             NO_WAY("Too many instruction groups");
4793         }
4794
4795         // If this instruction group is returned to from a funclet implementing a finally,
4796         // on architectures where it is necessary generate GC info for the current instruction as
4797         // if it were the instruction following a call.
4798         emitGenGCInfoIfFuncletRetTarget(ig, cp);
4799
4800         instrDesc* id = (instrDesc*)ig->igData;
4801
4802 #ifdef DEBUG
4803
4804         /* Print the IG label, but only if it is a branch label */
4805
4806         if (emitComp->opts.disAsm || emitComp->opts.dspEmit || emitComp->verbose)
4807         {
4808             if (emitComp->verbose)
4809             {
4810                 printf("\n");
4811                 emitDispIG(ig); // Display the flags, IG data, etc.
4812             }
4813             else
4814             {
4815                 printf("\nG_M%03u_IG%02u:\n", Compiler::s_compMethodsCount, ig->igNum);
4816             }
4817         }
4818
4819 #endif // DEBUG
4820
4821         BYTE* bp = cp;
4822
4823         /* Record the actual offset of the block, noting the difference */
4824
4825         emitOffsAdj = ig->igOffs - emitCurCodeOffs(cp);
4826         assert(emitOffsAdj >= 0);
4827
4828 #if DEBUG_EMIT
4829         if ((emitOffsAdj != 0) && emitComp->verbose)
4830         {
4831             printf("Block predicted offs = %08X, actual = %08X -> size adj = %d\n", ig->igOffs, emitCurCodeOffs(cp),
4832                    emitOffsAdj);
4833         }
4834 #endif // DEBUG_EMIT
4835
4836         ig->igOffs = emitCurCodeOffs(cp);
4837         assert(IsCodeAligned(ig->igOffs));
4838
4839 #if EMIT_TRACK_STACK_DEPTH
4840
4841         /* Set the proper stack level if appropriate */
4842
4843         if (ig->igStkLvl != emitCurStackLvl)
4844         {
4845             /* We are pushing stuff implicitly at this label */
4846
4847             assert((unsigned)ig->igStkLvl > (unsigned)emitCurStackLvl);
4848             emitStackPushN(cp, (ig->igStkLvl - (unsigned)emitCurStackLvl) / sizeof(int));
4849         }
4850
4851 #endif
4852
4853         /* Update current GC information for non-overflow IG (not added implicitly by the emitter) */
4854
4855         if (!(ig->igFlags & IGF_EMIT_ADD))
4856         {
4857             /* Is there a new set of live GC ref variables? */
4858
4859             if (ig->igFlags & IGF_GC_VARS)
4860             {
4861                 emitUpdateLiveGCvars(ig->igGCvars(), cp);
4862             }
4863             else if (!emitThisGCrefVset)
4864             {
4865                 emitUpdateLiveGCvars(emitThisGCrefVars, cp);
4866             }
4867
4868             /* Update the set of live GC ref registers */
4869
4870             {
4871                 regMaskTP GCregs = ig->igGCregs;
4872
4873                 if (GCregs != emitThisGCrefRegs)
4874                 {
4875                     emitUpdateLiveGCregs(GCT_GCREF, GCregs, cp);
4876                 }
4877             }
4878
4879             /* Is there a new set of live byref registers? */
4880
4881             if (ig->igFlags & IGF_BYREF_REGS)
4882             {
4883                 unsigned byrefRegs = ig->igByrefRegs();
4884
4885                 if (byrefRegs != emitThisByrefRegs)
4886                 {
4887                     emitUpdateLiveGCregs(GCT_BYREF, byrefRegs, cp);
4888                 }
4889             }
4890         }
4891         else
4892         {
4893             // These are not set for "overflow" groups
4894             assert(!(ig->igFlags & IGF_GC_VARS));
4895             assert(!(ig->igFlags & IGF_BYREF_REGS));
4896         }
4897
4898         /* Issue each instruction in order */
4899
4900         emitCurIG = ig;
4901
4902         for (unsigned cnt = ig->igInsCnt; cnt; cnt--)
4903         {
4904             castto(id, BYTE*) += emitIssue1Instr(ig, id, &cp);
4905         }
4906
4907         emitCurIG = nullptr;
4908
4909         assert(ig->igSize >= cp - bp);
4910         ig->igSize = (unsigned short)(cp - bp);
4911     }
4912
4913 #if EMIT_TRACK_STACK_DEPTH
4914     assert(emitCurStackLvl == 0);
4915 #endif
4916
4917     /* Output any initialized data we may have */
4918
4919     if (emitConsDsc.dsdOffs)
4920     {
4921         emitOutputDataSec(&emitConsDsc, consBlock);
4922     }
4923
4924     /* Make sure all GC ref variables are marked as dead */
4925
4926     if (emitGCrFrameOffsCnt)
4927     {
4928         unsigned    vn;
4929         int         of;
4930         varPtrDsc** dp;
4931
4932         for (vn = 0, of = emitGCrFrameOffsMin, dp = emitGCrFrameLiveTab; vn < emitGCrFrameOffsCnt;
4933              vn++, of += sizeof(void*), dp++)
4934         {
4935             if (*dp)
4936             {
4937                 emitGCvarDeadSet(of, cp, vn);
4938             }
4939         }
4940     }
4941
4942     /* No GC registers are live any more */
4943
4944     if (emitThisByrefRegs)
4945     {
4946         emitUpdateLiveGCregs(GCT_BYREF, RBM_NONE, cp);
4947     }
4948     if (emitThisGCrefRegs)
4949     {
4950         emitUpdateLiveGCregs(GCT_GCREF, RBM_NONE, cp);
4951     }
4952
4953     /* Patch any forward jumps */
4954
4955     if (emitFwdJumps)
4956     {
4957         instrDescJmp* jmp;
4958
4959         for (jmp = emitJumpList; jmp; jmp = jmp->idjNext)
4960         {
4961             insGroup* tgt;
4962 #ifdef _TARGET_XARCH_
4963             assert(jmp->idInsFmt() == IF_LABEL || jmp->idInsFmt() == IF_RWR_LABEL || jmp->idInsFmt() == IF_SWR_LABEL);
4964 #endif
4965             tgt = jmp->idAddr()->iiaIGlabel;
4966
4967             if (jmp->idjTemp.idjAddr == nullptr)
4968             {
4969                 continue;
4970             }
4971
4972             if (jmp->idjOffs != tgt->igOffs)
4973             {
4974                 BYTE* adr = jmp->idjTemp.idjAddr;
4975                 int   adj = jmp->idjOffs - tgt->igOffs;
4976 #ifdef _TARGET_ARM_
4977                 // On Arm, the offset is encoded in unit of 2 bytes.
4978                 adj >>= 1;
4979 #endif
4980
4981 #if DEBUG_EMIT
4982                 if (jmp->idDebugOnlyInfo()->idNum == (unsigned)INTERESTING_JUMP_NUM || INTERESTING_JUMP_NUM == 0)
4983                 {
4984 #ifdef _TARGET_ARM_
4985                     printf("[5] This output is broken for ARM, since it doesn't properly decode the jump offsets of "
4986                            "the instruction at adr\n");
4987 #endif
4988
4989                     if (INTERESTING_JUMP_NUM == 0)
4990                     {
4991                         printf("[5] Jump %u:\n", jmp->idDebugOnlyInfo()->idNum);
4992                     }
4993
4994                     if (jmp->idjShort)
4995                     {
4996                         printf("[5] Jump        is at %08X\n", (adr + 1 - emitCodeBlock));
4997                         printf("[5] Jump distance is  %02X - %02X = %02X\n", *(BYTE*)adr, adj, *(BYTE*)adr - adj);
4998                     }
4999                     else
5000                     {
5001                         printf("[5] Jump        is at %08X\n", (adr + 4 - emitCodeBlock));
5002                         printf("[5] Jump distance is  %08X - %02X = %08X\n", *(int*)adr, adj, *(int*)adr - adj);
5003                     }
5004                 }
5005 #endif // DEBUG_EMIT
5006
5007                 if (jmp->idjShort)
5008                 {
5009                     // Patch Forward Short Jump
5010                     CLANG_FORMAT_COMMENT_ANCHOR;
5011 #if defined(_TARGET_XARCH_)
5012                     *(BYTE*)adr -= (BYTE)adj;
5013 #elif defined(_TARGET_ARM_)
5014                     // The following works because the jump offset is in the low order bits of the instruction.
5015                     // Presumably we could also just call "emitOutputLJ(NULL, adr, jmp)", like for long jumps?
5016                     *(short int*)adr -= (short)adj;
5017 #elif defined(_TARGET_ARM64_)
5018                     assert(!jmp->idAddr()->iiaHasInstrCount());
5019                     emitOutputLJ(NULL, adr, jmp);
5020 #else
5021 #error Unsupported or unset target architecture
5022 #endif
5023                 }
5024                 else
5025                 {
5026                     // Patch Forward non-Short Jump
5027                     CLANG_FORMAT_COMMENT_ANCHOR;
5028 #if defined(_TARGET_XARCH_)
5029                     *(int*)adr -= adj;
5030 #elif defined(_TARGET_ARMARCH_)
5031                     assert(!jmp->idAddr()->iiaHasInstrCount());
5032                     emitOutputLJ(NULL, adr, jmp);
5033 #else
5034 #error Unsupported or unset target architecture
5035 #endif
5036                 }
5037             }
5038         }
5039     }
5040
5041 #ifdef DEBUG
5042     if (emitComp->opts.disAsm)
5043     {
5044         printf("\n");
5045     }
5046
5047     if (emitComp->verbose)
5048     {
5049         printf("Allocated method code size = %4u , actual size = %4u\n", emitTotalCodeSize, cp - codeBlock);
5050     }
5051 #endif
5052
5053     unsigned actualCodeSize = emitCurCodeOffs(cp);
5054
5055     /* Fill in eventual unused space */
5056     while (emitCurCodeOffs(cp) < emitTotalCodeSize)
5057     {
5058         *cp++ = DEFAULT_CODE_BUFFER_INIT;
5059     }
5060
5061 #if EMITTER_STATS
5062     totAllocdSize += emitTotalCodeSize;
5063     totActualSize += actualCodeSize;
5064 #endif
5065
5066 #ifdef DEBUG
5067
5068     // Make sure these didn't change during the "issuing" phase
5069
5070     assert(VarSetOps::MayBeUninit(emitPrevGCrefVars));
5071     assert(emitPrevGCrefRegs == 0xBAADFEED);
5072     assert(emitPrevByrefRegs == 0xBAADFEED);
5073
5074     assert(VarSetOps::MayBeUninit(emitInitGCrefVars));
5075     assert(emitInitGCrefRegs == 0xBAADFEED);
5076     assert(emitInitByrefRegs == 0xBAADFEED);
5077
5078 #endif
5079
5080     // Assign the real prolog size
5081     *prologSize = emitCodeOffset(emitPrologIG, emitPrologEndPos);
5082
5083     /* Return the amount of code we've generated */
5084
5085     return actualCodeSize;
5086 }
5087
5088 // See specification comment at the declaration.
5089 void emitter::emitGenGCInfoIfFuncletRetTarget(insGroup* ig, BYTE* cp)
5090 {
5091 #if FEATURE_EH_FUNCLETS && defined(_TARGET_ARM_)
5092     // We only emit this GC information on targets where finally's are implemented via funclets,
5093     // and the finally is invoked, during non-exceptional execution, via a branch with a predefined
5094     // link register, rather than a "true call" for which we would already generate GC info.  Currently,
5095     // this means precisely ARM.
5096     if (ig->igFlags & IGF_FINALLY_TARGET)
5097     {
5098         // We don't actually have a call instruction in this case, so we don't have
5099         // a real size for that instruction.  We'll use 1.
5100         emitStackPop(cp, /*isCall*/ true, /*callInstrSize*/ 1, /*args*/ 0);
5101
5102         /* Do we need to record a call location for GC purposes? */
5103         if (!emitFullGCinfo)
5104         {
5105             emitRecordGCcall(cp, /*callInstrSize*/ 1);
5106         }
5107     }
5108 #endif // FEATURE_EH_FUNCLETS && defined(_TARGET_ARM_)
5109 }
5110
5111 /*****************************************************************************
5112  *
5113  *  We have an instruction in an insGroup and we need to know the
5114  *  instruction number for this instruction
5115  */
5116
5117 unsigned emitter::emitFindInsNum(insGroup* ig, instrDesc* idMatch)
5118 {
5119     instrDesc* id = (instrDesc*)ig->igData;
5120
5121     // Check if we are the first instruction in the group
5122     if (id == idMatch)
5123     {
5124         return 0;
5125     }
5126
5127     /* Walk the list of instructions until we find a match */
5128     unsigned insNum       = 0;
5129     unsigned insRemaining = ig->igInsCnt;
5130
5131     while (insRemaining > 0)
5132     {
5133         castto(id, BYTE*) += emitSizeOfInsDsc(id);
5134         insNum++;
5135         insRemaining--;
5136
5137         if (id == idMatch)
5138         {
5139             return insNum;
5140         }
5141     }
5142     assert(!"emitFindInsNum failed");
5143     return -1;
5144 }
5145
5146 /*****************************************************************************
5147  *
5148  *  We've been asked for the code offset of an instruction but alas one or
5149  *  more instruction sizes in the block have been mis-predicted, so we have
5150  *  to find the true offset by looking for the instruction within the group.
5151  */
5152
5153 UNATIVE_OFFSET emitter::emitFindOffset(insGroup* ig, unsigned insNum)
5154 {
5155     instrDesc*     id = (instrDesc*)ig->igData;
5156     UNATIVE_OFFSET of = 0;
5157
5158 #ifdef DEBUG
5159     /* Make sure we were passed reasonable arguments */
5160     assert(ig && ig->igSelf == ig);
5161     assert(ig->igInsCnt >= insNum);
5162 #endif
5163
5164     /* Walk the instruction list until all are counted */
5165
5166     while (insNum > 0)
5167     {
5168         of += emitInstCodeSz(id);
5169
5170         castto(id, BYTE*) += emitSizeOfInsDsc(id);
5171
5172         insNum--;
5173     }
5174
5175     return of;
5176 }
5177
5178 /*****************************************************************************
5179  *
5180  *  Start generating a constant data section for the current
5181  *  function. Returns the offset of the section in the appropriate data
5182  *  block.
5183  */
5184
5185 UNATIVE_OFFSET emitter::emitDataGenBeg(UNATIVE_OFFSET size, bool dblAlign, bool codeLtab)
5186 {
5187     unsigned     secOffs;
5188     dataSection* secDesc;
5189
5190     assert(emitDataSecCur == nullptr);
5191
5192     /* The size better not be some kind of an odd thing */
5193
5194     assert(size && size % sizeof(int) == 0);
5195
5196     /* Get hold of the current offset */
5197
5198     secOffs = emitConsDsc.dsdOffs;
5199
5200     /* Are we require to align this request on an eight byte boundry? */
5201     if (dblAlign && (secOffs % sizeof(double) != 0))
5202     {
5203         /* Need to skip 4 bytes to honor dblAlign */
5204         /* Must allocate a dummy 4 byte integer */
5205         int zero = 0;
5206         emitDataGenBeg(4, false, false);
5207         emitDataGenData(0, &zero, 4);
5208         emitDataGenEnd();
5209
5210         /* Get the new secOffs */
5211         secOffs = emitConsDsc.dsdOffs;
5212         /* Now it should be a multiple of 8 */
5213         assert(secOffs % sizeof(double) == 0);
5214     }
5215
5216     /* Advance the current offset */
5217
5218     emitConsDsc.dsdOffs += size;
5219
5220     /* Allocate a data section descriptor and add it to the list */
5221
5222     secDesc = emitDataSecCur = (dataSection*)emitGetMem(roundUp(sizeof(*secDesc) + size));
5223
5224     secDesc->dsSize = size;
5225
5226     secDesc->dsType = dataSection::data;
5227
5228     secDesc->dsNext = nullptr;
5229
5230     if (emitConsDsc.dsdLast)
5231     {
5232         emitConsDsc.dsdLast->dsNext = secDesc;
5233     }
5234     else
5235     {
5236         emitConsDsc.dsdList = secDesc;
5237     }
5238     emitConsDsc.dsdLast = secDesc;
5239
5240     return secOffs;
5241 }
5242
5243 //  Start generating a constant data section for the current function
5244 //  populated with BasicBlock references.
5245 //  You can choose the references to be either absolute pointers, or
5246 //  4-byte relative addresses.
5247 //  Currently the relative references are relative to the start of the
5248 //  first block (this is somewhat arbitrary)
5249
5250 UNATIVE_OFFSET emitter::emitBBTableDataGenBeg(unsigned numEntries, bool relativeAddr)
5251 {
5252     unsigned     secOffs;
5253     dataSection* secDesc;
5254
5255     assert(emitDataSecCur == nullptr);
5256
5257     UNATIVE_OFFSET emittedSize;
5258
5259     if (relativeAddr)
5260     {
5261         emittedSize = numEntries * 4;
5262     }
5263     else
5264     {
5265         emittedSize = numEntries * TARGET_POINTER_SIZE;
5266     }
5267
5268     /* Get hold of the current offset */
5269
5270     secOffs = emitConsDsc.dsdOffs;
5271
5272     /* Advance the current offset */
5273
5274     emitConsDsc.dsdOffs += emittedSize;
5275
5276     /* Allocate a data section descriptor and add it to the list */
5277
5278     secDesc = emitDataSecCur = (dataSection*)emitGetMem(roundUp(sizeof(*secDesc) + numEntries * sizeof(BasicBlock*)));
5279
5280     secDesc->dsSize = emittedSize;
5281
5282     secDesc->dsType = relativeAddr ? dataSection::blockRelative32 : dataSection::blockAbsoluteAddr;
5283
5284     secDesc->dsNext = nullptr;
5285
5286     if (emitConsDsc.dsdLast)
5287     {
5288         emitConsDsc.dsdLast->dsNext = secDesc;
5289     }
5290     else
5291     {
5292         emitConsDsc.dsdList = secDesc;
5293     }
5294
5295     emitConsDsc.dsdLast = secDesc;
5296
5297     return secOffs;
5298 }
5299
5300 /*****************************************************************************
5301  *
5302  *  Emit the given block of bits into the current data section.
5303  */
5304
5305 void emitter::emitDataGenData(unsigned offs, const void* data, size_t size)
5306 {
5307     assert(emitDataSecCur && (emitDataSecCur->dsSize >= offs + size));
5308
5309     assert(emitDataSecCur->dsType == dataSection::data);
5310
5311     memcpy(emitDataSecCur->dsCont + offs, data, size);
5312 }
5313
5314 /*****************************************************************************
5315  *
5316  *  Emit the address of the given basic block into the current data section.
5317  */
5318
5319 void emitter::emitDataGenData(unsigned index, BasicBlock* label)
5320 {
5321     assert(emitDataSecCur != nullptr);
5322     assert(emitDataSecCur->dsType == dataSection::blockAbsoluteAddr ||
5323            emitDataSecCur->dsType == dataSection::blockRelative32);
5324
5325     unsigned emittedElemSize = emitDataSecCur->dsType == dataSection::blockAbsoluteAddr ? TARGET_POINTER_SIZE : 4;
5326
5327     assert(emitDataSecCur->dsSize >= emittedElemSize * (index + 1));
5328
5329     ((BasicBlock**)(emitDataSecCur->dsCont))[index] = label;
5330 }
5331
5332 /*****************************************************************************
5333  *
5334  *  We're done generating a data section.
5335  */
5336
5337 void emitter::emitDataGenEnd()
5338 {
5339
5340 #ifdef DEBUG
5341     assert(emitDataSecCur);
5342     emitDataSecCur = nullptr;
5343 #endif
5344 }
5345
5346 /********************************************************************************
5347  * Generates a data section constant
5348  *
5349  * Parameters:
5350  *     cnsAddr  - memory location containing constant value
5351  *     cnsSize  - size of constant in bytes
5352  *     dblAlign - whether to double align the data section constant
5353  *
5354  * Returns constant number as offset into data section.
5355  */
5356 UNATIVE_OFFSET emitter::emitDataConst(const void* cnsAddr, unsigned cnsSize, bool dblAlign)
5357 {
5358     // When generating SMALL_CODE, we don't bother with dblAlign
5359     if (dblAlign && (emitComp->compCodeOpt() == Compiler::SMALL_CODE))
5360     {
5361         dblAlign = false;
5362     }
5363
5364     UNATIVE_OFFSET cnum = emitDataGenBeg(cnsSize, dblAlign, false);
5365     emitDataGenData(0, cnsAddr, cnsSize);
5366     emitDataGenEnd();
5367
5368     return cnum;
5369 }
5370
5371 /*****************************************************************************
5372  *
5373  *  Output the given data section at the specified address.
5374  */
5375
5376 void emitter::emitOutputDataSec(dataSecDsc* sec, BYTE* dst)
5377 {
5378 #ifdef DEBUG
5379     if (EMITVERBOSE)
5380     {
5381         printf("\nEmitting data sections: %u total bytes\n", sec->dsdOffs);
5382     }
5383
5384     unsigned secNum = 0;
5385 #endif
5386
5387     assert(dst);
5388     assert(sec->dsdOffs);
5389     assert(sec->dsdList);
5390
5391     /* Walk and emit the contents of all the data blocks */
5392
5393     dataSection* dsc;
5394
5395     for (dsc = sec->dsdList; dsc; dsc = dsc->dsNext)
5396     {
5397         size_t dscSize = dsc->dsSize;
5398
5399         // absolute label table
5400         if (dsc->dsType == dataSection::blockAbsoluteAddr)
5401         {
5402             JITDUMP("  section %u, size %u, block absolute addr\n", secNum++, dscSize);
5403
5404             assert(dscSize && dscSize % sizeof(BasicBlock*) == 0);
5405             size_t numElems = dscSize / TARGET_POINTER_SIZE;
5406             BYTE** bDst     = (BYTE**)dst;
5407             for (unsigned i = 0; i < numElems; i++)
5408             {
5409                 BasicBlock* block = ((BasicBlock**)dsc->dsCont)[i];
5410
5411                 // Convert the BasicBlock* value to an IG address
5412                 insGroup* lab = (insGroup*)emitCodeGetCookie(block);
5413
5414                 // Append the appropriate address to the destination
5415                 BYTE* target = emitOffsetToPtr(lab->igOffs);
5416
5417 #ifdef _TARGET_ARM_
5418                 target = (BYTE*)((size_t)target | 1); // Or in thumb bit
5419 #endif
5420                 bDst[i] = target;
5421                 if (emitComp->opts.compReloc)
5422                 {
5423                     emitRecordRelocation(&(bDst[i]), target, IMAGE_REL_BASED_HIGHLOW);
5424                 }
5425
5426                 JITDUMP("  BB%02u: 0x%p\n", block->bbNum, bDst[i]);
5427             }
5428         }
5429         // relative label table
5430         else if (dsc->dsType == dataSection::blockRelative32)
5431         {
5432             JITDUMP("  section %u, size %u, block relative addr\n", secNum++, dscSize);
5433
5434             unsigned  elemSize = 4;
5435             size_t    numElems = dscSize / 4;
5436             unsigned* uDst     = (unsigned*)dst;
5437             insGroup* labFirst = (insGroup*)emitCodeGetCookie(emitComp->fgFirstBB);
5438
5439             for (unsigned i = 0; i < numElems; i++)
5440             {
5441                 BasicBlock* block = ((BasicBlock**)dsc->dsCont)[i];
5442
5443                 // Convert the BasicBlock* value to an IG address
5444                 insGroup* lab = (insGroup*)emitCodeGetCookie(block);
5445
5446                 assert(FitsIn<uint32_t>(lab->igOffs - labFirst->igOffs));
5447                 uDst[i] = lab->igOffs - labFirst->igOffs;
5448
5449                 JITDUMP("  BB%02u: 0x%x\n", block->bbNum, uDst[i]);
5450             }
5451         }
5452         else
5453         {
5454             JITDUMP("  section %u, size %u, raw data\n", secNum++, dscSize);
5455
5456             // Simple binary data: copy the bytes to the target
5457             assert(dsc->dsType == dataSection::data);
5458
5459             memcpy(dst, dsc->dsCont, dscSize);
5460
5461 #ifdef DEBUG
5462             if (EMITVERBOSE)
5463             {
5464                 printf("  ");
5465                 for (size_t i = 0; i < dscSize; i++)
5466                 {
5467                     printf("%02x ", dsc->dsCont[i]);
5468                     if ((((i + 1) % 16) == 0) && (i + 1 != dscSize))
5469                     {
5470                         printf("\n  ");
5471                     }
5472                 }
5473                 printf("\n");
5474             }
5475 #endif // DEBUG
5476         }
5477         dst += dscSize;
5478     }
5479 }
5480
5481 /*****************************************************************************/
5482 /*****************************************************************************
5483  *
5484  *  Record the fact that the given variable now contains a live GC ref.
5485  */
5486
5487 void emitter::emitGCvarLiveSet(int offs, GCtype gcType, BYTE* addr, ssize_t disp)
5488 {
5489     assert(emitIssuing);
5490
5491     varPtrDsc* desc;
5492
5493     assert((abs(offs) % sizeof(ssize_t)) == 0);
5494     assert(needsGC(gcType));
5495
5496     /* Compute the index into the GC frame table if the caller didn't do it */
5497
5498     if (disp == -1)
5499     {
5500         disp = (offs - emitGCrFrameOffsMin) / sizeof(void*);
5501     }
5502
5503     assert((size_t)disp < emitGCrFrameOffsCnt);
5504
5505     /* Allocate a lifetime record */
5506
5507     desc = new (emitComp, CMK_GC) varPtrDsc;
5508
5509     desc->vpdBegOfs = emitCurCodeOffs(addr);
5510 #ifdef DEBUG
5511     desc->vpdEndOfs = 0xFACEDEAD;
5512 #endif
5513
5514     desc->vpdVarNum = offs;
5515
5516     desc->vpdNext = nullptr;
5517
5518 #if !defined(JIT32_GCENCODER) || !defined(WIN64EXCEPTIONS)
5519     /* the lower 2 bits encode props about the stk ptr */
5520
5521     if (offs == emitSyncThisObjOffs)
5522     {
5523         desc->vpdVarNum |= this_OFFSET_FLAG;
5524     }
5525 #endif
5526
5527     if (gcType == GCT_BYREF)
5528     {
5529         desc->vpdVarNum |= byref_OFFSET_FLAG;
5530     }
5531
5532     /* Append the new entry to the end of the list */
5533     if (codeGen->gcInfo.gcVarPtrLast == nullptr)
5534     {
5535         assert(codeGen->gcInfo.gcVarPtrList == nullptr);
5536         codeGen->gcInfo.gcVarPtrList = codeGen->gcInfo.gcVarPtrLast = desc;
5537     }
5538     else
5539     {
5540         assert(codeGen->gcInfo.gcVarPtrList != nullptr);
5541         codeGen->gcInfo.gcVarPtrLast->vpdNext = desc;
5542         codeGen->gcInfo.gcVarPtrLast          = desc;
5543     }
5544
5545     /* Record the variable descriptor in the table */
5546
5547     assert(emitGCrFrameLiveTab[disp] == nullptr);
5548     emitGCrFrameLiveTab[disp] = desc;
5549
5550 #ifdef DEBUG
5551     if (EMITVERBOSE)
5552     {
5553         printf("[%08X] %s var born at [%s", dspPtr(desc), GCtypeStr(gcType), emitGetFrameReg());
5554
5555         if (offs < 0)
5556         {
5557             printf("-%02XH", -offs);
5558         }
5559         else if (offs > 0)
5560         {
5561             printf("+%02XH", +offs);
5562         }
5563
5564         printf("]\n");
5565     }
5566 #endif
5567
5568     /* The "global" live GC variable mask is no longer up-to-date */
5569
5570     emitThisGCrefVset = false;
5571 }
5572
5573 /*****************************************************************************
5574  *
5575  *  Record the fact that the given variable no longer contains a live GC ref.
5576  */
5577
5578 void emitter::emitGCvarDeadSet(int offs, BYTE* addr, ssize_t disp)
5579 {
5580     assert(emitIssuing);
5581
5582     varPtrDsc* desc;
5583
5584     assert(abs(offs) % sizeof(int) == 0);
5585
5586     /* Compute the index into the GC frame table if the caller didn't do it */
5587
5588     if (disp == -1)
5589     {
5590         disp = (offs - emitGCrFrameOffsMin) / sizeof(void*);
5591     }
5592
5593     assert((unsigned)disp < emitGCrFrameOffsCnt);
5594
5595     /* Get hold of the lifetime descriptor and clear the entry */
5596
5597     desc                      = emitGCrFrameLiveTab[disp];
5598     emitGCrFrameLiveTab[disp] = nullptr;
5599
5600     assert(desc);
5601     assert((desc->vpdVarNum & ~OFFSET_MASK) == (unsigned)offs);
5602
5603     /* Record the death code offset */
5604
5605     assert(desc->vpdEndOfs == 0xFACEDEAD);
5606     desc->vpdEndOfs = emitCurCodeOffs(addr);
5607
5608 #ifdef DEBUG
5609     if (EMITVERBOSE)
5610     {
5611         GCtype gcType = (desc->vpdVarNum & byref_OFFSET_FLAG) ? GCT_BYREF : GCT_GCREF;
5612 #if !defined(JIT32_GCENCODER) || !defined(WIN64EXCEPTIONS)
5613         bool isThis = (desc->vpdVarNum & this_OFFSET_FLAG) != 0;
5614
5615         printf("[%08X] %s%s var died at [%s", dspPtr(desc), GCtypeStr(gcType), isThis ? "this-ptr" : "",
5616                emitGetFrameReg());
5617 #else
5618         bool isPinned = (desc->vpdVarNum & pinned_OFFSET_FLAG) != 0;
5619
5620         printf("[%08X] %s%s var died at [%s", dspPtr(desc), GCtypeStr(gcType), isPinned ? "pinned" : "",
5621                emitGetFrameReg());
5622 #endif
5623
5624         if (offs < 0)
5625         {
5626             printf("-%02XH", -offs);
5627         }
5628         else if (offs > 0)
5629         {
5630             printf("+%02XH", +offs);
5631         }
5632
5633         printf("]\n");
5634     }
5635 #endif
5636
5637     /* The "global" live GC variable mask is no longer up-to-date */
5638
5639     emitThisGCrefVset = false;
5640 }
5641
5642 /*****************************************************************************
5643  *
5644  *  Record a new set of live GC ref variables.
5645  */
5646
5647 void emitter::emitUpdateLiveGCvars(VARSET_VALARG_TP vars, BYTE* addr)
5648 {
5649     assert(emitIssuing);
5650
5651     // Don't track GC changes in epilogs
5652     if (emitIGisInEpilog(emitCurIG))
5653     {
5654         return;
5655     }
5656
5657     /* Is the current set accurate and unchanged? */
5658
5659     if (emitThisGCrefVset && VarSetOps::Equal(emitComp, emitThisGCrefVars, vars))
5660     {
5661         return;
5662     }
5663
5664 #ifdef DEBUG
5665     if (EMIT_GC_VERBOSE)
5666     {
5667         printf("New GC ref live vars=%s ", VarSetOps::ToString(emitComp, vars));
5668         dumpConvertedVarSet(emitComp, vars);
5669         printf("\n");
5670     }
5671 #endif
5672
5673     VarSetOps::Assign(emitComp, emitThisGCrefVars, vars);
5674
5675     /* Are there any GC ref variables on the stack? */
5676
5677     if (emitGCrFrameOffsCnt)
5678     {
5679         int*     tab;
5680         unsigned cnt = emitTrkVarCnt;
5681         unsigned num;
5682
5683         /* Test all the tracked variable bits in the mask */
5684
5685         for (num = 0, tab = emitGCrFrameOffsTab; num < cnt; num++, tab++)
5686         {
5687             int val = *tab;
5688
5689             if (val != -1)
5690             {
5691                 // byref_OFFSET_FLAG and this_OFFSET_FLAG are set
5692                 //  in the table-offsets for byrefs and this-ptr
5693
5694                 int offs = val & ~OFFSET_MASK;
5695
5696                 // printf("var #%2u at %3d is now %s\n", num, offs, (vars & 1) ? "live" : "dead");
5697
5698                 if (VarSetOps::IsMember(emitComp, vars, num))
5699                 {
5700                     GCtype gcType = (val & byref_OFFSET_FLAG) ? GCT_BYREF : GCT_GCREF;
5701                     emitGCvarLiveUpd(offs, INT_MAX, gcType, addr);
5702                 }
5703                 else
5704                 {
5705                     emitGCvarDeadUpd(offs, addr);
5706                 }
5707             }
5708         }
5709     }
5710
5711     emitThisGCrefVset = true;
5712 }
5713
5714 /*****************************************************************************
5715  *
5716  *  Record a call location for GC purposes (we know that this is a method that
5717  *  will not be fully interruptible).
5718  */
5719
5720 void emitter::emitRecordGCcall(BYTE* codePos, unsigned char callInstrSize)
5721 {
5722     assert(emitIssuing);
5723     assert(!emitFullGCinfo);
5724
5725     unsigned offs = emitCurCodeOffs(codePos);
5726     unsigned regs = (emitThisGCrefRegs | emitThisByrefRegs) & ~RBM_INTRET;
5727     callDsc* call;
5728
5729 #ifdef JIT32_GCENCODER
5730     // The JIT32 GCInfo encoder allows us to (as the comment previously here said):
5731     // "Bail if this is a totally boring call", but the GCInfoEncoder/Decoder interface
5732     // requires a definition for every call site, so we skip these "early outs" when we're
5733     // using the general encoder.
5734     if (regs == 0)
5735     {
5736 #if EMIT_TRACK_STACK_DEPTH
5737         if (emitCurStackLvl == 0)
5738             return;
5739 #endif
5740         /* Nope, only interesting calls get recorded */
5741
5742         if (emitSimpleStkUsed)
5743         {
5744             if (!u1.emitSimpleStkMask)
5745                 return;
5746         }
5747         else
5748         {
5749             if (u2.emitGcArgTrackCnt == 0)
5750                 return;
5751         }
5752     }
5753 #endif // JIT32_GCENCODER
5754
5755 #ifdef DEBUG
5756
5757     if (EMIT_GC_VERBOSE)
5758     {
5759         printf("; Call at %04X [stk=%u], GCvars=", offs - callInstrSize, emitCurStackLvl);
5760         emitDispVarSet();
5761         printf(", gcrefRegs=");
5762         printRegMaskInt(emitThisGCrefRegs);
5763         emitDispRegSet(emitThisGCrefRegs);
5764         // printRegMaskInt(emitThisGCrefRegs & ~RBM_INTRET & RBM_CALLEE_SAVED);    // only display callee-saved
5765         // emitDispRegSet (emitThisGCrefRegs & ~RBM_INTRET & RBM_CALLEE_SAVED);    // only display callee-saved
5766         printf(", byrefRegs=");
5767         printRegMaskInt(emitThisByrefRegs);
5768         emitDispRegSet(emitThisByrefRegs);
5769         // printRegMaskInt(emitThisByrefRegs & ~RBM_INTRET & RBM_CALLEE_SAVED);    // only display callee-saved
5770         // emitDispRegSet (emitThisByrefRegs & ~RBM_INTRET & RBM_CALLEE_SAVED);    // only display callee-saved
5771         printf("\n");
5772     }
5773
5774 #endif
5775
5776     /* Allocate a 'call site' descriptor and start filling it in */
5777
5778     call = new (emitComp, CMK_GC) callDsc;
5779
5780     call->cdBlock = nullptr;
5781     call->cdOffs  = offs;
5782 #ifndef JIT32_GCENCODER
5783     call->cdCallInstrSize = callInstrSize;
5784 #endif
5785     call->cdNext = nullptr;
5786
5787     call->cdGCrefRegs = (regMaskSmall)emitThisGCrefRegs;
5788     call->cdByrefRegs = (regMaskSmall)emitThisByrefRegs;
5789
5790 #if EMIT_TRACK_STACK_DEPTH
5791 #ifndef FEATURE_UNIX_AMD64_STRUCT_PASSING
5792     noway_assert(FitsIn<USHORT>(emitCurStackLvl / ((unsigned)sizeof(unsigned))));
5793 #endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
5794 #endif
5795
5796     // Append the call descriptor to the list */
5797     if (codeGen->gcInfo.gcCallDescLast == nullptr)
5798     {
5799         assert(codeGen->gcInfo.gcCallDescList == nullptr);
5800         codeGen->gcInfo.gcCallDescList = codeGen->gcInfo.gcCallDescLast = call;
5801     }
5802     else
5803     {
5804         assert(codeGen->gcInfo.gcCallDescList != nullptr);
5805         codeGen->gcInfo.gcCallDescLast->cdNext = call;
5806         codeGen->gcInfo.gcCallDescLast         = call;
5807     }
5808
5809     /* Record the current "pending" argument list */
5810
5811     if (emitSimpleStkUsed)
5812     {
5813         /* The biggest call is less than MAX_SIMPLE_STK_DEPTH. So use
5814            small format */
5815
5816         call->u1.cdArgMask      = u1.emitSimpleStkMask;
5817         call->u1.cdByrefArgMask = u1.emitSimpleByrefStkMask;
5818         call->cdArgCnt          = 0;
5819     }
5820     else
5821     {
5822         /* The current call has too many arguments, so we need to report the
5823            offsets of each individual GC arg. */
5824
5825         call->cdArgCnt = u2.emitGcArgTrackCnt;
5826         if (call->cdArgCnt == 0)
5827         {
5828             call->u1.cdArgMask = call->u1.cdByrefArgMask = 0;
5829             return;
5830         }
5831
5832         call->cdArgTable = new (emitComp, CMK_GC) unsigned[u2.emitGcArgTrackCnt];
5833
5834         unsigned gcArgs = 0;
5835         unsigned stkLvl = emitCurStackLvl / sizeof(int);
5836
5837         for (unsigned i = 0; i < stkLvl; i++)
5838         {
5839             GCtype gcType = (GCtype)u2.emitArgTrackTab[stkLvl - i - 1];
5840
5841             if (needsGC(gcType))
5842             {
5843                 call->cdArgTable[gcArgs] = i * sizeof(void*);
5844
5845                 if (gcType == GCT_BYREF)
5846                 {
5847                     call->cdArgTable[gcArgs] |= byref_OFFSET_FLAG;
5848                 }
5849
5850                 gcArgs++;
5851             }
5852         }
5853
5854         assert(gcArgs == u2.emitGcArgTrackCnt);
5855     }
5856 }
5857
5858 /*****************************************************************************
5859  *
5860  *  Record a new set of live GC ref registers.
5861  */
5862
5863 void emitter::emitUpdateLiveGCregs(GCtype gcType, regMaskTP regs, BYTE* addr)
5864 {
5865     assert(emitIssuing);
5866
5867     // Don't track GC changes in epilogs
5868     if (emitIGisInEpilog(emitCurIG))
5869     {
5870         return;
5871     }
5872
5873     regMaskTP life;
5874     regMaskTP dead;
5875     regMaskTP chg;
5876
5877 #ifdef DEBUG
5878     if (EMIT_GC_VERBOSE)
5879     {
5880         printf("New %sReg live regs=", GCtypeStr(gcType));
5881         printRegMaskInt(regs);
5882         emitDispRegSet(regs);
5883         printf("\n");
5884     }
5885 #endif
5886
5887     assert(needsGC(gcType));
5888
5889     regMaskTP& emitThisXXrefRegs = (gcType == GCT_GCREF) ? emitThisGCrefRegs : emitThisByrefRegs;
5890     regMaskTP& emitThisYYrefRegs = (gcType == GCT_GCREF) ? emitThisByrefRegs : emitThisGCrefRegs;
5891     assert(emitThisXXrefRegs != regs);
5892
5893     if (emitFullGCinfo)
5894     {
5895         /* Figure out which GC registers are becoming live/dead at this point */
5896
5897         dead = (emitThisXXrefRegs & ~regs);
5898         life = (~emitThisXXrefRegs & regs);
5899
5900         /* Can't simultaneously become live and dead at the same time */
5901
5902         assert((dead | life) != 0);
5903         assert((dead & life) == 0);
5904
5905         /* Compute the 'changing state' mask */
5906
5907         chg = (dead | life);
5908
5909         do
5910         {
5911             regMaskTP bit = genFindLowestBit(chg);
5912             regNumber reg = genRegNumFromMask(bit);
5913
5914             if (life & bit)
5915             {
5916                 emitGCregLiveUpd(gcType, reg, addr);
5917             }
5918             else
5919             {
5920                 emitGCregDeadUpd(reg, addr);
5921             }
5922
5923             chg -= bit;
5924         } while (chg);
5925
5926         assert(emitThisXXrefRegs == regs);
5927     }
5928     else
5929     {
5930         emitThisYYrefRegs &= ~regs; // Kill the regs from the other GC type (if live)
5931         emitThisXXrefRegs = regs;   // Mark them as live in the requested GC type
5932     }
5933
5934     // The 2 GC reg masks can't be overlapping
5935
5936     assert((emitThisGCrefRegs & emitThisByrefRegs) == 0);
5937 }
5938
5939 /*****************************************************************************
5940  *
5941  *  Record the fact that the given register now contains a live GC ref.
5942  */
5943
5944 void emitter::emitGCregLiveSet(GCtype gcType, regMaskTP regMask, BYTE* addr, bool isThis)
5945 {
5946     assert(emitIssuing);
5947     assert(needsGC(gcType));
5948
5949     regPtrDsc* regPtrNext;
5950
5951     assert(!isThis || emitComp->lvaKeepAliveAndReportThis());
5952     // assert(emitFullyInt || isThis);
5953     assert(emitFullGCinfo);
5954
5955     assert(((emitThisGCrefRegs | emitThisByrefRegs) & regMask) == 0);
5956
5957     /* Allocate a new regptr entry and fill it in */
5958
5959     regPtrNext            = codeGen->gcInfo.gcRegPtrAllocDsc();
5960     regPtrNext->rpdGCtype = gcType;
5961
5962     regPtrNext->rpdOffs            = emitCurCodeOffs(addr);
5963     regPtrNext->rpdArg             = FALSE;
5964     regPtrNext->rpdCall            = FALSE;
5965     regPtrNext->rpdIsThis          = isThis;
5966     regPtrNext->rpdCompiler.rpdAdd = (regMaskSmall)regMask;
5967     regPtrNext->rpdCompiler.rpdDel = 0;
5968 }
5969
5970 /*****************************************************************************
5971  *
5972  *  Record the fact that the given register no longer contains a live GC ref.
5973  */
5974
5975 void emitter::emitGCregDeadSet(GCtype gcType, regMaskTP regMask, BYTE* addr)
5976 {
5977     assert(emitIssuing);
5978     assert(needsGC(gcType));
5979
5980     regPtrDsc* regPtrNext;
5981
5982     // assert(emitFullyInt);
5983     assert(emitFullGCinfo);
5984
5985     assert(((emitThisGCrefRegs | emitThisByrefRegs) & regMask) != 0);
5986
5987     /* Allocate a new regptr entry and fill it in */
5988
5989     regPtrNext            = codeGen->gcInfo.gcRegPtrAllocDsc();
5990     regPtrNext->rpdGCtype = gcType;
5991
5992     regPtrNext->rpdOffs            = emitCurCodeOffs(addr);
5993     regPtrNext->rpdCall            = FALSE;
5994     regPtrNext->rpdIsThis          = FALSE;
5995     regPtrNext->rpdArg             = FALSE;
5996     regPtrNext->rpdCompiler.rpdAdd = 0;
5997     regPtrNext->rpdCompiler.rpdDel = (regMaskSmall)regMask;
5998 }
5999
6000 /*****************************************************************************
6001  *
6002  *  Emit an 8-bit integer as code.
6003  */
6004
6005 unsigned char emitter::emitOutputByte(BYTE* dst, ssize_t val)
6006 {
6007     *castto(dst, unsigned char*) = (unsigned char)val;
6008
6009 #ifdef DEBUG
6010     if (emitComp->opts.dspEmit)
6011     {
6012         printf("; emit_byte 0%02XH\n", val & 0xFF);
6013     }
6014 #ifdef _TARGET_AMD64_
6015     // if we're emitting code bytes, ensure that we've already emitted the rex prefix!
6016     assert(((val & 0xFF00000000LL) == 0) || ((val & 0xFFFFFFFF00000000LL) == 0xFFFFFFFF00000000LL));
6017 #endif // _TARGET_AMD64_
6018 #endif
6019
6020     return sizeof(unsigned char);
6021 }
6022
6023 /*****************************************************************************
6024  *
6025  *  Emit a 16-bit integer as code.
6026  */
6027
6028 unsigned char emitter::emitOutputWord(BYTE* dst, ssize_t val)
6029 {
6030     MISALIGNED_WR_I2(dst, (short)val);
6031
6032 #ifdef DEBUG
6033     if (emitComp->opts.dspEmit)
6034     {
6035         printf("; emit_word 0%02XH,0%02XH\n", (val & 0xFF), (val >> 8) & 0xFF);
6036     }
6037 #ifdef _TARGET_AMD64_
6038     // if we're emitting code bytes, ensure that we've already emitted the rex prefix!
6039     assert(((val & 0xFF00000000LL) == 0) || ((val & 0xFFFFFFFF00000000LL) == 0xFFFFFFFF00000000LL));
6040 #endif // _TARGET_AMD64_
6041 #endif
6042
6043     return sizeof(short);
6044 }
6045
6046 /*****************************************************************************
6047  *
6048  *  Emit a 32-bit integer as code.
6049  */
6050
6051 unsigned char emitter::emitOutputLong(BYTE* dst, ssize_t val)
6052 {
6053     MISALIGNED_WR_I4(dst, (int)val);
6054
6055 #ifdef DEBUG
6056     if (emitComp->opts.dspEmit)
6057     {
6058         printf("; emit_long 0%08XH\n", (int)val);
6059     }
6060 #ifdef _TARGET_AMD64_
6061     // if we're emitting code bytes, ensure that we've already emitted the rex prefix!
6062     assert(((val & 0xFF00000000LL) == 0) || ((val & 0xFFFFFFFF00000000LL) == 0xFFFFFFFF00000000LL));
6063 #endif // _TARGET_AMD64_
6064 #endif
6065
6066     return sizeof(int);
6067 }
6068
6069 /*****************************************************************************
6070  *
6071  *  Emit a pointer-sized integer as code.
6072  */
6073
6074 unsigned char emitter::emitOutputSizeT(BYTE* dst, ssize_t val)
6075 {
6076     MISALIGNED_WR_ST(dst, val);
6077
6078 #ifdef DEBUG
6079     if (emitComp->opts.dspEmit)
6080     {
6081 #ifdef _TARGET_AMD64_
6082         printf("; emit_size_t 0%016llXH\n", val);
6083 #else  // _TARGET_AMD64_
6084         printf("; emit_size_t 0%08XH\n", val);
6085 #endif // _TARGET_AMD64_
6086     }
6087 #endif // DEBUG
6088
6089     return sizeof(size_t);
6090 }
6091
6092 //------------------------------------------------------------------------
6093 // Wrappers to emitOutputByte, emitOutputWord, emitOutputLong, emitOutputSizeT
6094 // that take unsigned __int64 or size_t type instead of ssize_t. Used on RyuJIT/x86.
6095 //
6096 // Arguments:
6097 //    dst - passed through
6098 //    val - passed through
6099 //
6100 // Return Value:
6101 //    Same as wrapped function.
6102 //
6103
6104 #if !defined(LEGACY_BACKEND) && defined(_TARGET_X86_)
6105 unsigned char emitter::emitOutputByte(BYTE* dst, size_t val)
6106 {
6107     return emitOutputByte(dst, (ssize_t)val);
6108 }
6109
6110 unsigned char emitter::emitOutputWord(BYTE* dst, size_t val)
6111 {
6112     return emitOutputWord(dst, (ssize_t)val);
6113 }
6114
6115 unsigned char emitter::emitOutputLong(BYTE* dst, size_t val)
6116 {
6117     return emitOutputLong(dst, (ssize_t)val);
6118 }
6119
6120 unsigned char emitter::emitOutputSizeT(BYTE* dst, size_t val)
6121 {
6122     return emitOutputSizeT(dst, (ssize_t)val);
6123 }
6124
6125 unsigned char emitter::emitOutputByte(BYTE* dst, unsigned __int64 val)
6126 {
6127     return emitOutputByte(dst, (ssize_t)val);
6128 }
6129
6130 unsigned char emitter::emitOutputWord(BYTE* dst, unsigned __int64 val)
6131 {
6132     return emitOutputWord(dst, (ssize_t)val);
6133 }
6134
6135 unsigned char emitter::emitOutputLong(BYTE* dst, unsigned __int64 val)
6136 {
6137     return emitOutputLong(dst, (ssize_t)val);
6138 }
6139
6140 unsigned char emitter::emitOutputSizeT(BYTE* dst, unsigned __int64 val)
6141 {
6142     return emitOutputSizeT(dst, (ssize_t)val);
6143 }
6144 #endif // !defined(LEGACY_BACKEND) && defined(_TARGET_X86_)
6145
6146 /*****************************************************************************
6147  *
6148  *  Given a block cookie and a code position, return the actual code offset;
6149  *  this can only be called at the end of code generation.
6150  */
6151
6152 UNATIVE_OFFSET emitter::emitCodeOffset(void* blockPtr, unsigned codePos)
6153 {
6154     insGroup* ig;
6155
6156     UNATIVE_OFFSET of;
6157     unsigned       no = emitGetInsNumFromCodePos(codePos);
6158
6159     /* Make sure we weren't passed some kind of a garbage thing */
6160
6161     ig = (insGroup*)blockPtr;
6162 #ifdef DEBUG
6163     assert(ig && ig->igSelf == ig);
6164 #endif
6165
6166     /* The first and last offsets are always easy */
6167
6168     if (no == 0)
6169     {
6170         of = 0;
6171     }
6172     else if (no == ig->igInsCnt)
6173     {
6174         of = ig->igSize;
6175     }
6176     else if (ig->igFlags & IGF_UPD_ISZ)
6177     {
6178         /*
6179             Some instruction sizes have changed, so we'll have to figure
6180             out the instruction offset "the hard way".
6181          */
6182
6183         of = emitFindOffset(ig, no);
6184     }
6185     else
6186     {
6187         /* All instructions correctly predicted, the offset stays the same */
6188
6189         of = emitGetInsOfsFromCodePos(codePos);
6190
6191         // printf("[IG=%02u;ID=%03u;OF=%04X] <= %08X\n", ig->igNum, emitGetInsNumFromCodePos(codePos), of, codePos);
6192
6193         /* Make sure the offset estimate is accurate */
6194
6195         assert(of == emitFindOffset(ig, emitGetInsNumFromCodePos(codePos)));
6196     }
6197
6198     return ig->igOffs + of;
6199 }
6200
6201 /*****************************************************************************
6202  *
6203  *  Record the fact that the given register now contains a live GC ref.
6204  */
6205
6206 void emitter::emitGCregLiveUpd(GCtype gcType, regNumber reg, BYTE* addr)
6207 {
6208     assert(emitIssuing);
6209
6210     // Don't track GC changes in epilogs
6211     if (emitIGisInEpilog(emitCurIG))
6212     {
6213         return;
6214     }
6215
6216     assert(needsGC(gcType));
6217
6218     regMaskTP regMask = genRegMask(reg);
6219
6220     regMaskTP& emitThisXXrefRegs = (gcType == GCT_GCREF) ? emitThisGCrefRegs : emitThisByrefRegs;
6221     regMaskTP& emitThisYYrefRegs = (gcType == GCT_GCREF) ? emitThisByrefRegs : emitThisGCrefRegs;
6222
6223     if ((emitThisXXrefRegs & regMask) == 0)
6224     {
6225         // If the register was holding the other GC type, that type should
6226         // go dead now
6227
6228         if (emitThisYYrefRegs & regMask)
6229         {
6230             emitGCregDeadUpd(reg, addr);
6231         }
6232
6233         // For synchronized methods, "this" is always alive and in the same register.
6234         // However, if we generate any code after the epilog block (where "this"
6235         // goes dead), "this" will come alive again. We need to notice that.
6236         // Note that we only expect isThis to be true at an insGroup boundary.
6237
6238         bool isThis = (reg == emitSyncThisObjReg) ? true : false;
6239
6240         if (emitFullGCinfo)
6241         {
6242             emitGCregLiveSet(gcType, regMask, addr, isThis);
6243         }
6244
6245         emitThisXXrefRegs |= regMask;
6246
6247 #ifdef DEBUG
6248         if (EMIT_GC_VERBOSE)
6249         {
6250             printf("%sReg +[%s]\n", GCtypeStr(gcType), emitRegName(reg));
6251         }
6252 #endif
6253     }
6254
6255     // The 2 GC reg masks can't be overlapping
6256
6257     assert((emitThisGCrefRegs & emitThisByrefRegs) == 0);
6258 }
6259
6260 /*****************************************************************************
6261  *
6262  *  Record the fact that the given set of registers no longer contain live GC refs.
6263  */
6264
6265 void emitter::emitGCregDeadUpdMask(regMaskTP regs, BYTE* addr)
6266 {
6267     assert(emitIssuing);
6268
6269     // Don't track GC changes in epilogs
6270     if (emitIGisInEpilog(emitCurIG))
6271     {
6272         return;
6273     }
6274
6275     // First, handle the gcref regs going dead
6276
6277     regMaskTP gcrefRegs = emitThisGCrefRegs & regs;
6278
6279     // "this" can never go dead in synchronized methods, except in the epilog
6280     // after the call to CORINFO_HELP_MON_EXIT.
6281     assert(emitSyncThisObjReg == REG_NA || (genRegMask(emitSyncThisObjReg) & regs) == 0);
6282
6283     if (gcrefRegs)
6284     {
6285         assert((emitThisByrefRegs & gcrefRegs) == 0);
6286
6287         if (emitFullGCinfo)
6288         {
6289             emitGCregDeadSet(GCT_GCREF, gcrefRegs, addr);
6290         }
6291
6292         emitThisGCrefRegs &= ~gcrefRegs;
6293
6294 #ifdef DEBUG
6295         if (EMIT_GC_VERBOSE)
6296         {
6297             printf("gcrReg ");
6298             printRegMaskInt(gcrefRegs);
6299             printf(" -");
6300             emitDispRegSet(gcrefRegs);
6301             printf("\n");
6302         }
6303 #endif
6304     }
6305
6306     // Second, handle the byref regs going dead
6307
6308     regMaskTP byrefRegs = emitThisByrefRegs & regs;
6309
6310     if (byrefRegs)
6311     {
6312         assert((emitThisGCrefRegs & byrefRegs) == 0);
6313
6314         if (emitFullGCinfo)
6315         {
6316             emitGCregDeadSet(GCT_BYREF, byrefRegs, addr);
6317         }
6318
6319         emitThisByrefRegs &= ~byrefRegs;
6320
6321 #ifdef DEBUG
6322         if (EMIT_GC_VERBOSE)
6323         {
6324             printf("byrReg ");
6325             printRegMaskInt(byrefRegs);
6326             printf(" -");
6327             emitDispRegSet(byrefRegs);
6328             printf("\n");
6329         }
6330 #endif
6331     }
6332 }
6333
6334 /*****************************************************************************
6335  *
6336  *  Record the fact that the given register no longer contains a live GC ref.
6337  */
6338
6339 void emitter::emitGCregDeadUpd(regNumber reg, BYTE* addr)
6340 {
6341     assert(emitIssuing);
6342
6343     // Don't track GC changes in epilogs
6344     if (emitIGisInEpilog(emitCurIG))
6345     {
6346         return;
6347     }
6348
6349     regMaskTP regMask = genRegMask(reg);
6350
6351     if ((emitThisGCrefRegs & regMask) != 0)
6352     {
6353         assert((emitThisByrefRegs & regMask) == 0);
6354
6355         if (emitFullGCinfo)
6356         {
6357             emitGCregDeadSet(GCT_GCREF, regMask, addr);
6358         }
6359
6360         emitThisGCrefRegs &= ~regMask;
6361
6362 #ifdef DEBUG
6363         if (EMIT_GC_VERBOSE)
6364         {
6365             printf("%s -[%s]\n", "gcrReg", emitRegName(reg));
6366         }
6367 #endif
6368     }
6369     else if ((emitThisByrefRegs & regMask) != 0)
6370     {
6371         if (emitFullGCinfo)
6372         {
6373             emitGCregDeadSet(GCT_BYREF, regMask, addr);
6374         }
6375
6376         emitThisByrefRegs &= ~regMask;
6377
6378 #ifdef DEBUG
6379         if (EMIT_GC_VERBOSE)
6380         {
6381             printf("%s -[%s]\n", "byrReg", emitRegName(reg));
6382         }
6383 #endif
6384     }
6385 }
6386
6387 /*****************************************************************************
6388  *
6389  *  Record the fact that the given variable now contains a live GC ref.
6390  *  varNum may be INT_MAX or negative (indicating a spill temp) only if
6391  *    offs is guaranteed to be the offset of a tracked GC ref. Else we
6392  *    need a valid value to check if the variable is tracked or not.
6393  */
6394
6395 void emitter::emitGCvarLiveUpd(int offs, int varNum, GCtype gcType, BYTE* addr)
6396 {
6397     assert(abs(offs) % sizeof(int) == 0);
6398     assert(needsGC(gcType));
6399
6400 #if FEATURE_FIXED_OUT_ARGS
6401     if ((unsigned)varNum == emitComp->lvaOutgoingArgSpaceVar)
6402     {
6403         if (emitFullGCinfo)
6404         {
6405             /* Append an "arg push" entry to track a GC written to the
6406                outgoing argument space.
6407                Allocate a new ptr arg entry and fill it in */
6408
6409             regPtrDsc* regPtrNext = gcInfo->gcRegPtrAllocDsc();
6410             regPtrNext->rpdGCtype = gcType;
6411             regPtrNext->rpdOffs   = emitCurCodeOffs(addr);
6412             regPtrNext->rpdArg    = TRUE;
6413             regPtrNext->rpdCall   = FALSE;
6414             noway_assert(FitsIn<unsigned short>(offs));
6415             regPtrNext->rpdPtrArg  = (unsigned short)offs;
6416             regPtrNext->rpdArgType = (unsigned short)GCInfo::rpdARG_PUSH;
6417             regPtrNext->rpdIsThis  = FALSE;
6418
6419 #ifdef DEBUG
6420             if (EMIT_GC_VERBOSE)
6421             {
6422                 printf("[%04X] %s arg write\n", offs, GCtypeStr(gcType));
6423             }
6424 #endif
6425         }
6426     }
6427     else
6428 #endif // FEATURE_FIXED_OUT_ARGS
6429     {
6430         /* Is the frame offset within the "interesting" range? */
6431
6432         if (offs >= emitGCrFrameOffsMin && offs < emitGCrFrameOffsMax)
6433         {
6434             /* Normally all variables in this range must be tracked stack
6435                pointers. However, for EnC, we relax this condition. So we
6436                must check if this is not such a variable.
6437                Note that varNum might be negative, indicating a spill temp.
6438             */
6439
6440             if (varNum != INT_MAX)
6441             {
6442                 bool isTracked = false;
6443                 if (varNum >= 0)
6444                 {
6445                     // This is NOT a spill temp
6446                     LclVarDsc* varDsc = &emitComp->lvaTable[varNum];
6447                     isTracked         = emitComp->lvaIsGCTracked(varDsc);
6448                 }
6449                 else
6450                 {
6451                     // Is it an untracked spill temp?
6452                     isTracked = TRACK_GC_TEMP_LIFETIMES;
6453                 }
6454                 if (!isTracked)
6455                 {
6456 #if DOUBLE_ALIGN
6457                     assert(!emitContTrkPtrLcls ||
6458                            // EBP based variables in the double-aligned frames are indeed input arguments.
6459                            // and we don't require them to fall into the "interesting" range.
6460                            ((emitComp->rpFrameType == FT_DOUBLE_ALIGN_FRAME) && (varNum >= 0) &&
6461                             (emitComp->lvaTable[varNum].lvFramePointerBased == 1)));
6462 #else
6463                     assert(!emitContTrkPtrLcls);
6464 #endif
6465                     return;
6466                 }
6467             }
6468
6469             size_t disp;
6470
6471             /* Compute the index into the GC frame table */
6472
6473             disp = (offs - emitGCrFrameOffsMin) / sizeof(void*);
6474             assert(disp < emitGCrFrameOffsCnt);
6475
6476             /* If the variable is currently dead, mark it as live */
6477
6478             if (emitGCrFrameLiveTab[disp] == nullptr)
6479             {
6480                 emitGCvarLiveSet(offs, gcType, addr, disp);
6481             }
6482         }
6483     }
6484 }
6485
6486 /*****************************************************************************
6487  *
6488  *  Record the fact that the given variable no longer contains a live GC ref.
6489  */
6490
6491 void emitter::emitGCvarDeadUpd(int offs, BYTE* addr)
6492 {
6493     assert(emitIssuing);
6494     assert(abs(offs) % sizeof(int) == 0);
6495
6496     /* Is the frame offset within the "interesting" range? */
6497
6498     if (offs >= emitGCrFrameOffsMin && offs < emitGCrFrameOffsMax)
6499     {
6500         size_t disp;
6501
6502         /* Compute the index into the GC frame table */
6503
6504         disp = (offs - emitGCrFrameOffsMin) / sizeof(void*);
6505         assert(disp < emitGCrFrameOffsCnt);
6506
6507         /* If the variable is currently live, mark it as dead */
6508
6509         if (emitGCrFrameLiveTab[disp] != nullptr)
6510         {
6511             emitGCvarDeadSet(offs, addr, disp);
6512         }
6513     }
6514 }
6515
6516 /*****************************************************************************
6517  *
6518  *  Allocate a new IG and link it in to the global list after the current IG
6519  */
6520
6521 insGroup* emitter::emitAllocAndLinkIG()
6522 {
6523     insGroup* ig = emitAllocIG();
6524
6525     assert(emitCurIG);
6526
6527     emitInsertIGAfter(emitCurIG, ig);
6528
6529     /* Propagate some IG flags from the current group to the new group */
6530
6531     ig->igFlags |= (emitCurIG->igFlags & IGF_PROPAGATE_MASK);
6532
6533     /* Set the new IG as the current IG */
6534
6535     emitCurIG = ig;
6536
6537     return ig;
6538 }
6539
6540 /*****************************************************************************
6541  *
6542  *  Allocate an instruction group descriptor and assign it the next index.
6543  */
6544
6545 insGroup* emitter::emitAllocIG()
6546 {
6547     insGroup* ig;
6548
6549     /* Allocate a group descriptor */
6550
6551     size_t sz = sizeof(insGroup);
6552     ig        = (insGroup*)emitGetMem(sz);
6553
6554 #ifdef DEBUG
6555     ig->igSelf = ig;
6556 #endif
6557
6558 #if EMITTER_STATS
6559     emitTotalIGcnt += 1;
6560     emitTotalIGsize += sz;
6561     emitSizeMethod += sz;
6562 #endif
6563
6564     /* Do basic initialization */
6565
6566     emitInitIG(ig);
6567
6568     return ig;
6569 }
6570
6571 /*****************************************************************************
6572  *
6573  *  Initialize an instruction group
6574  */
6575
6576 void emitter::emitInitIG(insGroup* ig)
6577 {
6578     /* Assign the next available index to the instruction group */
6579
6580     ig->igNum = emitNxtIGnum;
6581
6582     emitNxtIGnum++;
6583
6584     /* Record the (estimated) code offset of the group */
6585
6586     ig->igOffs = emitCurCodeOffset;
6587     assert(IsCodeAligned(ig->igOffs));
6588
6589     /* Set the current function index */
6590
6591     ig->igFuncIdx = emitComp->compCurrFuncIdx;
6592
6593     ig->igFlags = 0;
6594
6595     /* Zero out some fields to avoid printing garbage in JitDumps. These
6596        really only need to be set in DEBUG, but do it in all cases to make
6597        sure we act the same in non-DEBUG builds.
6598     */
6599
6600     ig->igSize   = 0;
6601     ig->igGCregs = RBM_NONE;
6602     ig->igInsCnt = 0;
6603 }
6604
6605 /*****************************************************************************
6606  *
6607  *  Insert instruction group 'ig' after 'igInsertAfterIG'
6608  */
6609
6610 void emitter::emitInsertIGAfter(insGroup* insertAfterIG, insGroup* ig)
6611 {
6612     assert(emitIGlist);
6613     assert(emitIGlast);
6614
6615     ig->igNext            = insertAfterIG->igNext;
6616     insertAfterIG->igNext = ig;
6617
6618     if (emitIGlast == insertAfterIG)
6619     {
6620         // If we are inserting at the end, then update the 'last' pointer
6621         emitIGlast = ig;
6622     }
6623 }
6624
6625 /*****************************************************************************
6626  *
6627  *  Save the current IG and start a new one.
6628  */
6629
6630 void emitter::emitNxtIG(bool emitAdd)
6631 {
6632     /* Right now we don't allow multi-IG prologs */
6633
6634     assert(emitCurIG != emitPrologIG);
6635
6636     /* First save the current group */
6637
6638     emitSavIG(emitAdd);
6639
6640     /* Update the GC live sets for the group's start
6641      * Do it only if not an emitter added block */
6642
6643     if (!emitAdd)
6644     {
6645         VarSetOps::Assign(emitComp, emitInitGCrefVars, emitThisGCrefVars);
6646         emitInitGCrefRegs = emitThisGCrefRegs;
6647         emitInitByrefRegs = emitThisByrefRegs;
6648     }
6649
6650     /* Start generating the new group */
6651
6652     emitNewIG();
6653
6654     /* If this is an emitter added block, flag it */
6655
6656     if (emitAdd)
6657     {
6658         emitCurIG->igFlags |= IGF_EMIT_ADD;
6659     }
6660
6661     // We've created a new IG; no need to force another one.
6662     emitForceNewIG = false;
6663 }
6664
6665 /*****************************************************************************
6666  *
6667  *  emitGetInsSC: Get the instruction's constant value.
6668  */
6669
6670 ssize_t emitter::emitGetInsSC(instrDesc* id)
6671 {
6672 #ifdef _TARGET_ARM_ // should it be _TARGET_ARMARCH_? Why do we need this? Note that on ARM64 we store scaled immediates
6673                     // for some formats
6674     if (id->idIsLclVar())
6675     {
6676         int varNum = id->idAddr()->iiaLclVar.lvaVarNum();
6677
6678         regNumber baseReg;
6679         int       offs = id->idAddr()->iiaLclVar.lvaOffset();
6680 #if defined(_TARGET_ARM_)
6681         int adr = emitComp->lvaFrameAddress(varNum, id->idIsLclFPBase(), &baseReg, offs);
6682         int dsp = adr + offs;
6683         if ((id->idIns() == INS_sub) || (id->idIns() == INS_subw))
6684             dsp = -dsp;
6685 #elif defined(_TARGET_ARM64_)
6686         // TODO-ARM64-Cleanup: this is currently unreachable. Do we need it?
6687         bool FPbased;
6688         int  adr = emitComp->lvaFrameAddress(varNum, &FPbased);
6689         int  dsp = adr + offs;
6690         if (id->idIns() == INS_sub)
6691             dsp = -dsp;
6692 #endif
6693         return dsp;
6694     }
6695     else
6696 #endif // _TARGET_ARM_
6697         if (id->idIsLargeCns())
6698     {
6699         return ((instrDescCns*)id)->idcCnsVal;
6700     }
6701     else
6702     {
6703         return id->idSmallCns();
6704     }
6705 }
6706
6707 /*****************************************************************************/
6708 #if EMIT_TRACK_STACK_DEPTH
6709 /*****************************************************************************
6710  *
6711  *  Record a push of a single dword on the stack.
6712  */
6713
6714 void emitter::emitStackPush(BYTE* addr, GCtype gcType)
6715 {
6716 #ifdef DEBUG
6717     assert(IsValidGCtype(gcType));
6718 #endif
6719
6720     if (emitSimpleStkUsed)
6721     {
6722         assert(!emitFullGCinfo); // Simple stk not used for emitFullGCinfo
6723         assert(emitCurStackLvl / sizeof(int) < MAX_SIMPLE_STK_DEPTH);
6724
6725         u1.emitSimpleStkMask <<= 1;
6726         u1.emitSimpleStkMask |= (unsigned)needsGC(gcType);
6727
6728         u1.emitSimpleByrefStkMask <<= 1;
6729         u1.emitSimpleByrefStkMask |= (gcType == GCT_BYREF);
6730
6731         assert((u1.emitSimpleStkMask & u1.emitSimpleByrefStkMask) == u1.emitSimpleByrefStkMask);
6732     }
6733     else
6734     {
6735         emitStackPushLargeStk(addr, gcType);
6736     }
6737
6738     emitCurStackLvl += sizeof(int);
6739 }
6740
6741 /*****************************************************************************
6742  *
6743  *  Record a push of a bunch of non-GC dwords on the stack.
6744  */
6745
6746 void emitter::emitStackPushN(BYTE* addr, unsigned count)
6747 {
6748     assert(count);
6749
6750     if (emitSimpleStkUsed)
6751     {
6752         assert(!emitFullGCinfo); // Simple stk not used for emitFullGCinfo
6753
6754         u1.emitSimpleStkMask <<= count;
6755         u1.emitSimpleByrefStkMask <<= count;
6756     }
6757     else
6758     {
6759         emitStackPushLargeStk(addr, GCT_NONE, count);
6760     }
6761
6762     emitCurStackLvl += count * sizeof(int);
6763 }
6764
6765 /*****************************************************************************
6766  *
6767  *  Record a pop of the given number of dwords from the stack.
6768  */
6769
6770 void emitter::emitStackPop(BYTE* addr, bool isCall, unsigned char callInstrSize, unsigned count)
6771 {
6772     assert(emitCurStackLvl / sizeof(int) >= count);
6773     assert(!isCall || callInstrSize > 0);
6774
6775     if (count)
6776     {
6777         if (emitSimpleStkUsed)
6778         {
6779             assert(!emitFullGCinfo); // Simple stk not used for emitFullGCinfo
6780
6781             unsigned cnt = count;
6782
6783             do
6784             {
6785                 u1.emitSimpleStkMask >>= 1;
6786                 u1.emitSimpleByrefStkMask >>= 1;
6787             } while (--cnt);
6788         }
6789         else
6790         {
6791             emitStackPopLargeStk(addr, isCall, callInstrSize, count);
6792         }
6793
6794         emitCurStackLvl -= count * sizeof(int);
6795     }
6796     else
6797     {
6798         assert(isCall);
6799
6800         // For the general encoder we do the call below always when it's a call, to ensure that the call is
6801         // recorded (when we're doing the ptr reg map for a non-fully-interruptible method).
6802         if (emitFullGCinfo
6803 #ifndef JIT32_GCENCODER
6804             || (emitComp->genFullPtrRegMap && (!emitComp->genInterruptible) && isCall)
6805 #endif // JIT32_GCENCODER
6806                 )
6807         {
6808             emitStackPopLargeStk(addr, isCall, callInstrSize, 0);
6809         }
6810     }
6811 }
6812
6813 /*****************************************************************************
6814  *
6815  *  Record a push of a single word on the stack for a full pointer map.
6816  */
6817
6818 void emitter::emitStackPushLargeStk(BYTE* addr, GCtype gcType, unsigned count)
6819 {
6820     S_UINT32 level(emitCurStackLvl / sizeof(int));
6821
6822     assert(IsValidGCtype(gcType));
6823     assert(count);
6824     assert(!emitSimpleStkUsed);
6825
6826     do
6827     {
6828         /* Push an entry for this argument on the tracking stack */
6829
6830         // printf("Pushed [%d] at lvl %2u [max=%u]\n", isGCref, emitArgTrackTop - emitArgTrackTab, emitMaxStackDepth);
6831
6832         assert(level.IsOverflow() || u2.emitArgTrackTop == u2.emitArgTrackTab + level.Value());
6833         *u2.emitArgTrackTop++ = (BYTE)gcType;
6834         assert(u2.emitArgTrackTop <= u2.emitArgTrackTab + emitMaxStackDepth);
6835
6836         if (emitFullArgInfo || needsGC(gcType))
6837         {
6838             if (emitFullGCinfo)
6839             {
6840                 /* Append an "arg push" entry if this is a GC ref or
6841                    FPO method. Allocate a new ptr arg entry and fill it in */
6842
6843                 regPtrDsc* regPtrNext = codeGen->gcInfo.gcRegPtrAllocDsc();
6844                 regPtrNext->rpdGCtype = gcType;
6845
6846                 regPtrNext->rpdOffs = emitCurCodeOffs(addr);
6847                 regPtrNext->rpdArg  = TRUE;
6848                 regPtrNext->rpdCall = FALSE;
6849                 if (level.IsOverflow() || !FitsIn<unsigned short>(level.Value()))
6850                 {
6851                     IMPL_LIMITATION("Too many/too big arguments to encode GC information");
6852                 }
6853                 regPtrNext->rpdPtrArg  = (unsigned short)level.Value();
6854                 regPtrNext->rpdArgType = (unsigned short)GCInfo::rpdARG_PUSH;
6855                 regPtrNext->rpdIsThis  = FALSE;
6856
6857 #ifdef DEBUG
6858                 if (EMIT_GC_VERBOSE)
6859                 {
6860                     printf("[%08X] %s arg push %u\n", dspPtr(regPtrNext), GCtypeStr(gcType), level.Value());
6861                 }
6862 #endif
6863             }
6864
6865             /* This is an "interesting" argument push */
6866
6867             u2.emitGcArgTrackCnt++;
6868         }
6869         level += 1;
6870         assert(!level.IsOverflow());
6871     } while (--count);
6872 }
6873
6874 /*****************************************************************************
6875  *
6876  *  Record a pop of the given number of words from the stack for a full ptr
6877  *  map.
6878  */
6879
6880 void emitter::emitStackPopLargeStk(BYTE* addr, bool isCall, unsigned char callInstrSize, unsigned count)
6881 {
6882     assert(emitIssuing);
6883
6884     unsigned argStkCnt;
6885     S_UINT16 argRecCnt(0); // arg count for ESP, ptr-arg count for EBP
6886     unsigned gcrefRegs, byrefRegs;
6887
6888 #ifdef JIT32_GCENCODER
6889     // For the general encoder, we always need to record calls, so we make this call
6890     // even when emitSimpleStkUsed is true.
6891     assert(!emitSimpleStkUsed);
6892 #endif
6893
6894     /* Count how many pointer records correspond to this "pop" */
6895
6896     for (argStkCnt = count; argStkCnt; argStkCnt--)
6897     {
6898         assert(u2.emitArgTrackTop > u2.emitArgTrackTab);
6899
6900         GCtype gcType = (GCtype)(*--u2.emitArgTrackTop);
6901
6902         assert(IsValidGCtype(gcType));
6903
6904         // printf("Popped [%d] at lvl %u\n", GCtypeStr(gcType), emitArgTrackTop - emitArgTrackTab);
6905
6906         // This is an "interesting" argument
6907
6908         if (emitFullArgInfo || needsGC(gcType))
6909         {
6910             argRecCnt += 1;
6911         }
6912     }
6913
6914     assert(u2.emitArgTrackTop >= u2.emitArgTrackTab);
6915     assert(u2.emitArgTrackTop == u2.emitArgTrackTab + emitCurStackLvl / sizeof(int) - count);
6916     noway_assert(!argRecCnt.IsOverflow());
6917
6918     /* We're about to pop the corresponding arg records */
6919
6920     u2.emitGcArgTrackCnt -= argRecCnt.Value();
6921
6922 #ifdef JIT32_GCENCODER
6923     // For the general encoder, we always have to record calls, so we don't take this early return.
6924     if (!emitFullGCinfo)
6925         return;
6926 #endif
6927
6928     // Do we have any interesting (i.e., callee-saved) registers live here?
6929
6930     gcrefRegs = byrefRegs = 0;
6931
6932     // We make a bitmask whose bits correspond to callee-saved register indices (in the sequence
6933     // of callee-saved registers only).
6934     for (unsigned calleeSavedRegIdx = 0; calleeSavedRegIdx < CNT_CALLEE_SAVED; calleeSavedRegIdx++)
6935     {
6936         regMaskTP calleeSavedRbm = raRbmCalleeSaveOrder[calleeSavedRegIdx];
6937         if (emitThisGCrefRegs & calleeSavedRbm)
6938         {
6939             gcrefRegs |= (1 << calleeSavedRegIdx);
6940         }
6941         if (emitThisByrefRegs & calleeSavedRbm)
6942         {
6943             byrefRegs |= (1 << calleeSavedRegIdx);
6944         }
6945     }
6946
6947 #ifdef JIT32_GCENCODER
6948     // For the general encoder, we always have to record calls, so we don't take this early return.    /* Are there any
6949     // args to pop at this call site?
6950
6951     if (argRecCnt.Value() == 0)
6952     {
6953         /*
6954             Or do we have a partially interruptible EBP-less frame, and any
6955             of EDI,ESI,EBX,EBP are live, or is there an outer/pending call?
6956          */
6957         CLANG_FORMAT_COMMENT_ANCHOR;
6958
6959 #if !FPO_INTERRUPTIBLE
6960         if (emitFullyInt || (gcrefRegs == 0 && byrefRegs == 0 && u2.emitGcArgTrackCnt == 0))
6961 #endif
6962             return;
6963     }
6964 #endif // JIT32_GCENCODER
6965
6966     /* Only calls may pop more than one value */
6967     // More detail:
6968     // _cdecl calls accomplish this popping via a post-call-instruction SP adjustment.
6969     // The "rpdCall" field below should be interpreted as "the instruction accomplishes
6970     // call-related popping, even if it's not itself a call".  Therefore, we don't just
6971     // use the "isCall" input argument, which means that the instruction actually is a call --
6972     // we use the OR of "isCall" or the "pops more than one value."
6973
6974     bool isCallRelatedPop = (argRecCnt.Value() > 1);
6975
6976     /* Allocate a new ptr arg entry and fill it in */
6977
6978     regPtrDsc* regPtrNext = codeGen->gcInfo.gcRegPtrAllocDsc();
6979     regPtrNext->rpdGCtype = GCT_GCREF; // Pops need a non-0 value (??)
6980
6981     regPtrNext->rpdOffs = emitCurCodeOffs(addr);
6982     regPtrNext->rpdCall = (isCall || isCallRelatedPop);
6983 #ifndef JIT32_GCENCODER
6984     if (regPtrNext->rpdCall)
6985     {
6986         assert(isCall || callInstrSize == 0);
6987         regPtrNext->rpdCallInstrSize = callInstrSize;
6988     }
6989 #endif
6990     regPtrNext->rpdCallGCrefRegs = gcrefRegs;
6991     regPtrNext->rpdCallByrefRegs = byrefRegs;
6992     regPtrNext->rpdArg           = TRUE;
6993     regPtrNext->rpdArgType       = (unsigned short)GCInfo::rpdARG_POP;
6994     regPtrNext->rpdPtrArg        = argRecCnt.Value();
6995
6996 #ifdef DEBUG
6997     if (EMIT_GC_VERBOSE)
6998     {
6999         printf("[%08X] ptr arg pop  %u\n", dspPtr(regPtrNext), count);
7000     }
7001 #endif
7002 }
7003
7004 /*****************************************************************************
7005  *  For caller-pop arguments, we report the arguments as pending arguments.
7006  *  However, any GC arguments are now dead, so we need to report them
7007  *  as non-GC.
7008  */
7009
7010 void emitter::emitStackKillArgs(BYTE* addr, unsigned count, unsigned char callInstrSize)
7011 {
7012     assert(count > 0);
7013
7014     if (emitSimpleStkUsed)
7015     {
7016         assert(!emitFullGCinfo); // Simple stk not used for emitFullGCInfo
7017
7018         /* We don't need to report this to the GC info, but we do need
7019            to kill mark the ptrs on the stack as non-GC */
7020
7021         assert(emitCurStackLvl / sizeof(int) >= count);
7022
7023         for (unsigned lvl = 0; lvl < count; lvl++)
7024         {
7025             u1.emitSimpleStkMask &= ~(1 << lvl);
7026             u1.emitSimpleByrefStkMask &= ~(1 << lvl);
7027         }
7028     }
7029     else
7030     {
7031         BYTE*    argTrackTop = u2.emitArgTrackTop;
7032         S_UINT16 gcCnt(0);
7033
7034         for (unsigned i = 0; i < count; i++)
7035         {
7036             assert(argTrackTop > u2.emitArgTrackTab);
7037
7038             --argTrackTop;
7039
7040             GCtype gcType = (GCtype)(*argTrackTop);
7041             assert(IsValidGCtype(gcType));
7042
7043             if (needsGC(gcType))
7044             {
7045                 // printf("Killed %s at lvl %u\n", GCtypeStr(gcType), argTrackTop - emitArgTrackTab);
7046
7047                 *argTrackTop = GCT_NONE;
7048                 gcCnt += 1;
7049             }
7050         }
7051
7052         noway_assert(!gcCnt.IsOverflow());
7053
7054         /* We're about to kill the corresponding (pointer) arg records */
7055
7056         if (!emitFullArgInfo)
7057         {
7058             u2.emitGcArgTrackCnt -= gcCnt.Value();
7059         }
7060
7061         if (!emitFullGCinfo)
7062         {
7063             return;
7064         }
7065
7066         /* Right after the call, the arguments are still sitting on the
7067            stack, but they are effectively dead. For fully-interruptible
7068            methods, we need to report that */
7069
7070         if (emitFullGCinfo && gcCnt.Value())
7071         {
7072             /* Allocate a new ptr arg entry and fill it in */
7073
7074             regPtrDsc* regPtrNext = codeGen->gcInfo.gcRegPtrAllocDsc();
7075             regPtrNext->rpdGCtype = GCT_GCREF; // Kills need a non-0 value (??)
7076
7077             regPtrNext->rpdOffs = emitCurCodeOffs(addr);
7078
7079             regPtrNext->rpdArg     = TRUE;
7080             regPtrNext->rpdArgType = (unsigned short)GCInfo::rpdARG_KILL;
7081             regPtrNext->rpdPtrArg  = gcCnt.Value();
7082
7083 #ifdef DEBUG
7084             if (EMIT_GC_VERBOSE)
7085             {
7086                 printf("[%08X] ptr arg kill %u\n", dspPtr(regPtrNext), count);
7087             }
7088 #endif
7089         }
7090
7091         /* Now that ptr args have been marked as non-ptrs, we need to record
7092            the call itself as one that has no arguments. */
7093
7094         emitStackPopLargeStk(addr, true, callInstrSize, 0);
7095     }
7096 }
7097
7098 /*****************************************************************************
7099  *  A helper for recording a relocation with the EE.
7100  */
7101 void emitter::emitRecordRelocation(void* location,            /* IN */
7102                                    void* target,              /* IN */
7103                                    WORD  fRelocType,          /* IN */
7104                                    WORD  slotNum /* = 0 */,   /* IN */
7105                                    INT32 addlDelta /* = 0 */) /* IN */
7106 {
7107     // If we're an unmatched altjit, don't tell the VM anything. We still record the relocation for
7108     // late disassembly; maybe we'll need it?
7109     if (emitComp->info.compMatchedVM)
7110     {
7111         emitCmpHandle->recordRelocation(location, target, fRelocType, slotNum, addlDelta);
7112     }
7113 #if defined(LATE_DISASM)
7114     codeGen->getDisAssembler().disRecordRelocation((size_t)location, (size_t)target);
7115 #endif // defined(LATE_DISASM)
7116 }
7117
7118 #ifdef _TARGET_ARM_
7119 /*****************************************************************************
7120  *  A helper for handling a Thumb-Mov32 of position-independent (PC-relative) value
7121  *
7122  *  This routine either records relocation for the location with the EE,
7123  *  or creates a virtual relocation entry to perform offset fixup during
7124  *  compilation without recording it with EE - depending on which of
7125  *  absolute/relocative relocations mode are used for code section.
7126  */
7127 void emitter::emitHandlePCRelativeMov32(void* location, /* IN */
7128                                         void* target)   /* IN */
7129 {
7130     if (emitComp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_RELATIVE_CODE_RELOCS))
7131     {
7132         emitRecordRelocation(location, target, IMAGE_REL_BASED_REL_THUMB_MOV32_PCREL);
7133     }
7134     else
7135     {
7136         emitRecordRelocation(location, target, IMAGE_REL_BASED_THUMB_MOV32);
7137     }
7138 }
7139 #endif // _TARGET_ARM_
7140
7141 /*****************************************************************************
7142  *  A helper for recording a call site with the EE.
7143  */
7144 void emitter::emitRecordCallSite(ULONG                 instrOffset,  /* IN */
7145                                  CORINFO_SIG_INFO*     callSig,      /* IN */
7146                                  CORINFO_METHOD_HANDLE methodHandle) /* IN */
7147 {
7148 #if defined(DEBUG)
7149     // Since CORINFO_SIG_INFO is a heavyweight structure, in most cases we can
7150     // lazily obtain it here using the given method handle (we only save the sig
7151     // info when we explicitly need it, i.e. for CALLI calls, vararg calls, and
7152     // tail calls).
7153     if (callSig == nullptr)
7154     {
7155         assert(methodHandle != nullptr);
7156
7157         if (Compiler::eeGetHelperNum(methodHandle) == CORINFO_HELP_UNDEF)
7158         {
7159             if (emitScratchSigInfo == nullptr)
7160             {
7161                 emitScratchSigInfo = new (emitComp, CMK_CorSig) CORINFO_SIG_INFO;
7162             }
7163
7164             emitComp->eeGetMethodSig(methodHandle, emitScratchSigInfo);
7165             callSig = emitScratchSigInfo;
7166         }
7167     }
7168
7169     emitCmpHandle->recordCallSite(instrOffset, callSig, methodHandle);
7170 #endif // defined(DEBUG)
7171 }
7172
7173 /*****************************************************************************/
7174 #endif // EMIT_TRACK_STACK_DEPTH
7175 /*****************************************************************************/
7176 /*****************************************************************************/
7177
7178 #ifdef DEBUG
7179
7180 /*****************************************************************************
7181  *  Given a code offset, return a string representing a label for that offset.
7182  *  If the code offset is just after the end of the code of the function, the
7183  *  label will be "END". If the code offset doesn't correspond to any known
7184  *  offset, the label will be "UNKNOWN". The strings are returned from static
7185  *  buffers. This function rotates amongst four such static buffers (there are
7186  *  cases where this function is called four times to provide data for a single
7187  *  printf()).
7188  */
7189
7190 const char* emitter::emitOffsetToLabel(unsigned offs)
7191 {
7192     const size_t    TEMP_BUFFER_LEN = 40;
7193     static unsigned curBuf          = 0;
7194     static char     buf[4][TEMP_BUFFER_LEN];
7195     char*           retbuf;
7196
7197     insGroup*      ig;
7198     UNATIVE_OFFSET of;
7199     UNATIVE_OFFSET nextof = 0;
7200
7201     for (ig = emitIGlist; ig != nullptr; ig = ig->igNext)
7202     {
7203         assert(nextof == ig->igOffs);
7204
7205         if (ig->igOffs == offs)
7206         {
7207             // Found it!
7208             sprintf_s(buf[curBuf], TEMP_BUFFER_LEN, "G_M%03u_IG%02u", Compiler::s_compMethodsCount, ig->igNum);
7209             retbuf = buf[curBuf];
7210             curBuf = (curBuf + 1) % 4;
7211             return retbuf;
7212         }
7213         else if (ig->igOffs > offs)
7214         {
7215             // We went past the requested offset but didn't find it.
7216             sprintf_s(buf[curBuf], TEMP_BUFFER_LEN, "UNKNOWN");
7217             retbuf = buf[curBuf];
7218             curBuf = (curBuf + 1) % 4;
7219             return retbuf;
7220         }
7221
7222         nextof = ig->igOffs + ig->igSize;
7223     }
7224
7225     if (nextof == offs)
7226     {
7227         // It's a pseudo-label to the end.
7228         sprintf_s(buf[curBuf], TEMP_BUFFER_LEN, "END");
7229         retbuf = buf[curBuf];
7230         curBuf = (curBuf + 1) % 4;
7231         return retbuf;
7232     }
7233     else
7234     {
7235         sprintf_s(buf[curBuf], TEMP_BUFFER_LEN, "UNKNOWN");
7236         retbuf = buf[curBuf];
7237         curBuf = (curBuf + 1) % 4;
7238         return retbuf;
7239     }
7240 }
7241
7242 #endif // DEBUG