-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathgcencode.cpp
4862 lines (4047 loc) · 166 KB
/
gcencode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX GCEncode XX
XX XX
XX Logic to encode the JIT method header and GC pointer tables XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "gcinfotypes.h"
#include "patchpointinfo.h"
ReturnKind VarTypeToReturnKind(var_types type)
{
switch (type)
{
case TYP_REF:
return RT_Object;
case TYP_BYREF:
return RT_ByRef;
#ifdef TARGET_X86
case TYP_FLOAT:
case TYP_DOUBLE:
return RT_Float;
#endif // TARGET_X86
default:
return RT_Scalar;
}
}
ReturnKind GCInfo::getReturnKind()
{
// Note the GCInfo representation only supports structs with up to 2 GC pointers.
ReturnTypeDesc retTypeDesc = compiler->compRetTypeDesc;
const unsigned regCount = retTypeDesc.GetReturnRegCount();
switch (regCount)
{
case 1:
return VarTypeToReturnKind(retTypeDesc.GetReturnRegType(0));
case 2:
return GetStructReturnKind(VarTypeToReturnKind(retTypeDesc.GetReturnRegType(0)),
VarTypeToReturnKind(retTypeDesc.GetReturnRegType(1)));
default:
#ifdef DEBUG
for (unsigned i = 0; i < regCount; i++)
{
assert(!varTypeIsGC(retTypeDesc.GetReturnRegType(i)));
}
#endif // DEBUG
return RT_Scalar;
}
}
// gcMarkFilterVarsPinned - Walk all lifetimes and make it so that anything
// live in a filter is marked as pinned (often by splitting the lifetime
// so that *only* the filter region is pinned). This should only be
// called once (after generating all lifetimes, but before slot ids are
// finalized.
//
// DevDiv 376329 - The VM has to double report filters and their parent frame
// because they occur during the 1st pass and the parent frame doesn't go dead
// until we start unwinding in the 2nd pass.
//
// Untracked locals will only be reported in non-filter funclets and the
// parent.
// Registers can't be double reported by 2 frames since they're different.
// That just leaves stack variables which might be double reported.
//
// Technically double reporting is only a problem when the GC has to relocate a
// reference. So we avoid that problem by marking all live tracked stack
// variables as pinned inside the filter. Thus if they are double reported, it
// won't be a problem since they won't be double relocated.
//
void GCInfo::gcMarkFilterVarsPinned()
{
assert(compiler->UsesFunclets());
assert(compiler->ehAnyFunclets());
for (EHblkDsc* const HBtab : EHClauses(compiler))
{
if (HBtab->HasFilter())
{
const UNATIVE_OFFSET filterBeg = compiler->ehCodeOffset(HBtab->ebdFilter);
const UNATIVE_OFFSET filterEnd = compiler->ehCodeOffset(HBtab->ebdHndBeg);
for (varPtrDsc* varTmp = gcVarPtrList; varTmp != nullptr; varTmp = varTmp->vpdNext)
{
// Get hold of the variable's flags.
const unsigned lowBits = varTmp->vpdVarNum & OFFSET_MASK;
// Compute the actual lifetime offsets.
const unsigned begOffs = varTmp->vpdBegOfs;
const unsigned endOffs = varTmp->vpdEndOfs;
// Special case: skip any 0-length lifetimes.
if (endOffs == begOffs)
{
continue;
}
// Skip lifetimes with no overlap with the filter
if ((endOffs <= filterBeg) || (begOffs >= filterEnd))
{
continue;
}
#ifndef JIT32_GCENCODER
// Because there is no nesting within filters, nothing
// should be already pinned.
// For JIT32_GCENCODER, we should not do this check as gcVarPtrList are always sorted by vpdBegOfs
// which means that we could see some varPtrDsc that were already pinned by previous splitting.
assert((lowBits & pinned_OFFSET_FLAG) == 0);
#endif // JIT32_GCENCODER
if (begOffs < filterBeg)
{
if (endOffs > filterEnd)
{
// The variable lifetime is starts before AND ends after
// the filter, so we need to create 2 new lifetimes:
// (1) a pinned one for the filter
// (2) a regular one for after the filter
// and then adjust the original lifetime to end before
// the filter.
#ifdef DEBUG
if (compiler->verbose)
{
printf("Splitting lifetime for filter: [%04X, %04X).\nOld: ", filterBeg, filterEnd);
gcDumpVarPtrDsc(varTmp);
}
#endif // DEBUG
varPtrDsc* desc1 = new (compiler, CMK_GC) varPtrDsc;
desc1->vpdVarNum = varTmp->vpdVarNum | pinned_OFFSET_FLAG;
desc1->vpdBegOfs = filterBeg;
desc1->vpdEndOfs = filterEnd;
varPtrDsc* desc2 = new (compiler, CMK_GC) varPtrDsc;
desc2->vpdVarNum = varTmp->vpdVarNum;
desc2->vpdBegOfs = filterEnd;
desc2->vpdEndOfs = endOffs;
varTmp->vpdEndOfs = filterBeg;
gcInsertVarPtrDscSplit(desc1, varTmp);
gcInsertVarPtrDscSplit(desc2, varTmp);
#ifdef DEBUG
if (compiler->verbose)
{
printf("New (1 of 3): ");
gcDumpVarPtrDsc(varTmp);
printf("New (2 of 3): ");
gcDumpVarPtrDsc(desc1);
printf("New (3 of 3): ");
gcDumpVarPtrDsc(desc2);
}
#endif // DEBUG
}
else
{
// The variable lifetime started before the filter and ends
// somewhere inside it, so we only create 1 new lifetime,
// and then adjust the original lifetime to end before
// the filter.
#ifdef DEBUG
if (compiler->verbose)
{
printf("Splitting lifetime for filter.\nOld: ");
gcDumpVarPtrDsc(varTmp);
}
#endif // DEBUG
varPtrDsc* desc = new (compiler, CMK_GC) varPtrDsc;
desc->vpdVarNum = varTmp->vpdVarNum | pinned_OFFSET_FLAG;
desc->vpdBegOfs = filterBeg;
desc->vpdEndOfs = endOffs;
varTmp->vpdEndOfs = filterBeg;
gcInsertVarPtrDscSplit(desc, varTmp);
#ifdef DEBUG
if (compiler->verbose)
{
printf("New (1 of 2): ");
gcDumpVarPtrDsc(varTmp);
printf("New (2 of 2): ");
gcDumpVarPtrDsc(desc);
}
#endif // DEBUG
}
}
else
{
if (endOffs > filterEnd)
{
// The variable lifetime starts inside the filter and
// ends somewhere after it, so we create 1 new
// lifetime for the part inside the filter and adjust
// the start of the original lifetime to be the end
// of the filter
#ifdef DEBUG
if (compiler->verbose)
{
printf("Splitting lifetime for filter.\nOld: ");
gcDumpVarPtrDsc(varTmp);
}
#endif // DEBUG
varPtrDsc* desc = new (compiler, CMK_GC) varPtrDsc;
#ifndef JIT32_GCENCODER
desc->vpdVarNum = varTmp->vpdVarNum | pinned_OFFSET_FLAG;
desc->vpdBegOfs = begOffs;
desc->vpdEndOfs = filterEnd;
varTmp->vpdBegOfs = filterEnd;
#else
// Mark varTmp as pinned and generated use varPtrDsc(desc) as non-pinned
// since gcInsertVarPtrDscSplit requires that varTmp->vpdBegOfs must precede desc->vpdBegOfs
desc->vpdVarNum = varTmp->vpdVarNum;
desc->vpdBegOfs = filterEnd;
desc->vpdEndOfs = endOffs;
varTmp->vpdVarNum = varTmp->vpdVarNum | pinned_OFFSET_FLAG;
varTmp->vpdEndOfs = filterEnd;
#endif
gcInsertVarPtrDscSplit(desc, varTmp);
#ifdef DEBUG
if (compiler->verbose)
{
printf("New (1 of 2): ");
gcDumpVarPtrDsc(desc);
printf("New (2 of 2): ");
gcDumpVarPtrDsc(varTmp);
}
#endif // DEBUG
}
else
{
// The variable lifetime is completely within the filter,
// so just add the pinned flag.
#ifdef DEBUG
if (compiler->verbose)
{
printf("Pinning lifetime for filter.\nOld: ");
gcDumpVarPtrDsc(varTmp);
}
#endif // DEBUG
varTmp->vpdVarNum |= pinned_OFFSET_FLAG;
#ifdef DEBUG
if (compiler->verbose)
{
printf("New : ");
gcDumpVarPtrDsc(varTmp);
}
#endif // DEBUG
}
}
}
} // HasFilter
} // Foreach EH
}
// gcInsertVarPtrDscSplit - Insert varPtrDsc that were created by splitting lifetimes
// From gcMarkFilterVarsPinned, we may have created one or two `varPtrDsc`s due to splitting lifetimes
// and these newly created `varPtrDsc`s should be inserted in gcVarPtrList.
// However the semantics of this call depend on the architecture.
//
// x86-GCInfo requires gcVarPtrList to be sorted by vpdBegOfs.
// Every time inserting an entry we should keep the order of entries.
// So this function searches for a proper insertion point from "begin" then "desc" gets inserted.
//
// For other architectures(ones that uses GCInfo{En|De}coder), we don't need any sort.
// So the argument "begin" is unused and "desc" will be inserted at the front of the list.
void GCInfo::gcInsertVarPtrDscSplit(varPtrDsc* desc, varPtrDsc* begin)
{
assert(compiler->UsesFunclets());
#ifndef JIT32_GCENCODER
(void)begin;
desc->vpdNext = gcVarPtrList;
gcVarPtrList = desc;
#else // JIT32_GCENCODER
// "desc" and "begin" must not be null
assert(desc != nullptr);
assert(begin != nullptr);
// The caller must guarantee that desc's BegOfs is equal or greater than begin's
// since we will search for insertion point from "begin"
assert(desc->vpdBegOfs >= begin->vpdBegOfs);
varPtrDsc* varTmp = begin->vpdNext;
varPtrDsc* varInsert = begin;
while (varTmp != nullptr && varTmp->vpdBegOfs < desc->vpdBegOfs)
{
varInsert = varTmp;
varTmp = varTmp->vpdNext;
}
// Insert point cannot be null
assert(varInsert != nullptr);
desc->vpdNext = varInsert->vpdNext;
varInsert->vpdNext = desc;
#endif // JIT32_GCENCODER
}
#ifdef DEBUG
void GCInfo::gcDumpVarPtrDsc(varPtrDsc* desc)
{
const int offs = (desc->vpdVarNum & ~OFFSET_MASK);
const GCtype gcType = (desc->vpdVarNum & byref_OFFSET_FLAG) ? GCT_BYREF : GCT_GCREF;
const bool isPin = (desc->vpdVarNum & pinned_OFFSET_FLAG) != 0;
assert(compiler->UsesFunclets());
printf("[%08X] %s%s var at [%s", dspPtr(desc), GCtypeStr(gcType), isPin ? "pinned-ptr" : "",
compiler->isFramePointerUsed() ? STR_FPBASE : STR_SPBASE);
if (offs < 0)
{
printf("-0x%02X", -offs);
}
else if (offs > 0)
{
printf("+0x%02X", +offs);
}
printf("] live from %04X to %04X\n", desc->vpdBegOfs, desc->vpdEndOfs);
}
#endif // DEBUG
#ifdef JIT32_GCENCODER
#include "emit.h"
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
// (see jit.h) #define REGEN_SHORTCUTS 0
// To Regenerate the compressed info header shortcuts, define REGEN_SHORTCUTS
// and use the following command line pipe/filter to give you the 128
// most useful encodings.
//
// find . -name regen.txt | xargs cat | grep InfoHdr | sort | uniq -c | sort -r | head -128
// (see jit.h) #define REGEN_CALLPAT 0
// To Regenerate the compressed info header shortcuts, define REGEN_CALLPAT
// and use the following command line pipe/filter to give you the 80
// most useful encodings.
//
// find . -name regen.txt | xargs cat | grep CallSite | sort | uniq -c | sort -r | head -80
#if REGEN_SHORTCUTS || REGEN_CALLPAT
static FILE* logFile = NULL;
CRITICAL_SECTION logFileLock;
#endif
#if REGEN_CALLPAT
static void regenLog(unsigned codeDelta,
unsigned argMask,
unsigned regMask,
unsigned argCnt,
unsigned byrefArgMask,
unsigned byrefRegMask,
BYTE* base,
unsigned enSize)
{
CallPattern pat;
pat.fld.argCnt = (argCnt < 0xff) ? argCnt : 0xff;
pat.fld.regMask = (regMask < 0xff) ? regMask : 0xff;
pat.fld.argMask = (argMask < 0xff) ? argMask : 0xff;
pat.fld.codeDelta = (codeDelta < 0xff) ? codeDelta : 0xff;
if (logFile == NULL)
{
logFile = fopen("regen.txt", "a");
InitializeCriticalSection(&logFileLock);
}
assert(((enSize > 0) && (enSize < 256)) && ((pat.val & 0xffffff) != 0xffffff));
EnterCriticalSection(&logFileLock);
fprintf(logFile, "CallSite( 0x%08x, 0x%02x%02x, 0x", pat.val, byrefArgMask, byrefRegMask);
while (enSize > 0)
{
fprintf(logFile, "%02x", *base++);
enSize--;
}
fprintf(logFile, "),\n");
fflush(logFile);
LeaveCriticalSection(&logFileLock);
}
#endif
#if REGEN_SHORTCUTS
static void regenLog(unsigned encoding, InfoHdr* header, InfoHdr* state)
{
if (logFile == NULL)
{
logFile = fopen("regen.txt", "a");
InitializeCriticalSection(&logFileLock);
}
EnterCriticalSection(&logFileLock);
fprintf(logFile,
"InfoHdr( %2d, %2d, %1d, %1d, %1d,"
" %1d, %1d, %1d, %1d, %1d,"
" %1d, %1d, %1d, %1d, %1d, %1d,"
" %1d, %1d, %1d,"
" %1d, %2d, %2d,"
" %2d, %2d, %2d, %2d, %2d, %2d), \n",
state->prologSize, state->epilogSize, state->epilogCount, state->epilogAtEnd, state->ediSaved,
state->esiSaved, state->ebxSaved, state->ebpSaved, state->ebpFrame, state->interruptible,
state->doubleAlign, state->security, state->handlers, state->localloc, state->editNcontinue, state->varargs,
state->profCallbacks, state->genericsContext, state->genericsContextIsMethodDesc, state->returnKind,
state->argCount, state->frameSize,
(state->untrackedCnt <= SET_UNTRACKED_MAX) ? state->untrackedCnt : HAS_UNTRACKED,
(state->varPtrTableSize == 0) ? 0 : HAS_VARPTR,
(state->gsCookieOffset == INVALID_GS_COOKIE_OFFSET) ? 0 : HAS_GS_COOKIE_OFFSET,
(state->syncStartOffset == INVALID_SYNC_OFFSET) ? 0 : HAS_SYNC_OFFSET,
(state->syncStartOffset == INVALID_SYNC_OFFSET) ? 0 : HAS_SYNC_OFFSET,
(state->revPInvokeOffset == INVALID_REV_PINVOKE_OFFSET) ? 0 : HAS_REV_PINVOKE_FRAME_OFFSET);
fflush(logFile);
LeaveCriticalSection(&logFileLock);
}
#endif
/*****************************************************************************
*
* Given the four parameters return the index into the callPatternTable[]
* that is used to encoding these four items. If an exact match cannot
* found then ignore the codeDelta and search the table again for a near
* match.
* Returns 0..79 for an exact match or
* (delta<<8) | (0..79) for a near match.
* A near match will be encoded using two bytes, the first byte will
* skip the adjustment delta that prevented an exact match and the
* rest of the delta plus the other three items are encoded in the
* second byte.
*/
int FASTCALL lookupCallPattern(unsigned argCnt, unsigned regMask, unsigned argMask, unsigned codeDelta)
{
if ((argCnt <= CP_MAX_ARG_CNT) && (argMask <= CP_MAX_ARG_MASK))
{
CallPattern pat;
pat.fld.argCnt = (BYTE)argCnt;
pat.fld.regMask = (BYTE)regMask; // EBP,EBX,ESI,EDI
pat.fld.argMask = (BYTE)argMask;
pat.fld.codeDelta = (BYTE)codeDelta;
bool codeDeltaOK = (pat.fld.codeDelta == codeDelta);
unsigned bestDelta2 = 0xff;
unsigned bestPattern = 0xff;
unsigned patval = pat.val;
assert(sizeof(CallPattern) == sizeof(unsigned));
const unsigned* curp = &callPatternTable[0];
for (unsigned inx = 0; inx < 80; inx++, curp++)
{
unsigned curval = *curp;
if ((patval == curval) && codeDeltaOK)
return inx;
if (((patval ^ curval) & 0xffffff) == 0)
{
unsigned delta2 = codeDelta - (curval >> 24);
if (delta2 < bestDelta2)
{
bestDelta2 = delta2;
bestPattern = inx;
}
}
}
if (bestPattern != 0xff)
{
return (bestDelta2 << 8) | bestPattern;
}
}
return -1;
}
static bool initNeeded3(unsigned cur, unsigned tgt, unsigned max, unsigned* hint)
{
assert(cur != tgt);
unsigned tmp = tgt;
unsigned nib = 0;
unsigned cnt = 0;
while (tmp > max)
{
nib = tmp & 0x07;
tmp >>= 3;
if (tmp == cur)
{
*hint = nib;
return false;
}
cnt++;
}
*hint = tmp;
return true;
}
static bool initNeeded4(unsigned cur, unsigned tgt, unsigned max, unsigned* hint)
{
assert(cur != tgt);
unsigned tmp = tgt;
unsigned nib = 0;
unsigned cnt = 0;
while (tmp > max)
{
nib = tmp & 0x0f;
tmp >>= 4;
if (tmp == cur)
{
*hint = nib;
return false;
}
cnt++;
}
*hint = tmp;
return true;
}
static int bigEncoding3(unsigned cur, unsigned tgt, unsigned max)
{
assert(cur != tgt);
unsigned tmp = tgt;
unsigned nib = 0;
unsigned cnt = 0;
while (tmp > max)
{
nib = tmp & 0x07;
tmp >>= 3;
if (tmp == cur)
break;
cnt++;
}
return cnt;
}
static int bigEncoding4(unsigned cur, unsigned tgt, unsigned max)
{
assert(cur != tgt);
unsigned tmp = tgt;
unsigned nib = 0;
unsigned cnt = 0;
while (tmp > max)
{
nib = tmp & 0x0f;
tmp >>= 4;
if (tmp == cur)
break;
cnt++;
}
return cnt;
}
BYTE FASTCALL encodeHeaderNext(const InfoHdr& header, InfoHdr* state, BYTE& codeSet)
{
BYTE encoding = 0xff;
codeSet = 1; // codeSet is 1 or 2, depending on whether the returned encoding
// corresponds to InfoHdrAdjust, or InfoHdrAdjust2 enumerations.
if (state->argCount != header.argCount)
{
// We have one-byte encodings for 0..8
if (header.argCount <= SET_ARGCOUNT_MAX)
{
state->argCount = header.argCount;
encoding = (BYTE)(SET_ARGCOUNT + header.argCount);
goto DO_RETURN;
}
else
{
unsigned hint;
if (initNeeded4(state->argCount, header.argCount, SET_ARGCOUNT_MAX, &hint))
{
assert(hint <= SET_ARGCOUNT_MAX);
state->argCount = (unsigned short)hint;
encoding = (BYTE)(SET_ARGCOUNT + hint);
goto DO_RETURN;
}
else
{
assert(hint <= 0xf);
state->argCount <<= 4;
state->argCount += ((unsigned short)hint);
encoding = (BYTE)(NEXT_FOUR_ARGCOUNT + hint);
goto DO_RETURN;
}
}
}
if (state->frameSize != header.frameSize)
{
// We have one-byte encodings for 0..7
if (header.frameSize <= SET_FRAMESIZE_MAX)
{
state->frameSize = header.frameSize;
encoding = (BYTE)(SET_FRAMESIZE + header.frameSize);
goto DO_RETURN;
}
else
{
unsigned hint;
if (initNeeded4(state->frameSize, header.frameSize, SET_FRAMESIZE_MAX, &hint))
{
assert(hint <= SET_FRAMESIZE_MAX);
state->frameSize = hint;
encoding = (BYTE)(SET_FRAMESIZE + hint);
goto DO_RETURN;
}
else
{
assert(hint <= 0xf);
state->frameSize <<= 4;
state->frameSize += hint;
encoding = (BYTE)(NEXT_FOUR_FRAMESIZE + hint);
goto DO_RETURN;
}
}
}
if ((state->epilogCount != header.epilogCount) || (state->epilogAtEnd != header.epilogAtEnd))
{
if (header.epilogCount > SET_EPILOGCNT_MAX)
IMPL_LIMITATION("More than SET_EPILOGCNT_MAX epilogs");
state->epilogCount = header.epilogCount;
state->epilogAtEnd = header.epilogAtEnd;
encoding = SET_EPILOGCNT + header.epilogCount * 2;
if (header.epilogAtEnd)
encoding++;
goto DO_RETURN;
}
if (state->varPtrTableSize != header.varPtrTableSize)
{
assert(state->varPtrTableSize == 0 || state->varPtrTableSize == HAS_VARPTR);
if (state->varPtrTableSize == 0)
{
state->varPtrTableSize = HAS_VARPTR;
encoding = FLIP_VAR_PTR_TABLE_SZ;
goto DO_RETURN;
}
else if (header.varPtrTableSize == 0)
{
state->varPtrTableSize = 0;
encoding = FLIP_VAR_PTR_TABLE_SZ;
goto DO_RETURN;
}
}
if (state->untrackedCnt != header.untrackedCnt)
{
assert(state->untrackedCnt <= SET_UNTRACKED_MAX || state->untrackedCnt == HAS_UNTRACKED);
// We have one-byte encodings for 0..3
if (header.untrackedCnt <= SET_UNTRACKED_MAX)
{
state->untrackedCnt = header.untrackedCnt;
encoding = (BYTE)(SET_UNTRACKED + header.untrackedCnt);
goto DO_RETURN;
}
else if (state->untrackedCnt != HAS_UNTRACKED)
{
state->untrackedCnt = HAS_UNTRACKED;
encoding = FFFF_UNTRACKED_CNT;
goto DO_RETURN;
}
}
if (state->epilogSize != header.epilogSize)
{
// We have one-byte encodings for 0..10
if (header.epilogSize <= SET_EPILOGSIZE_MAX)
{
state->epilogSize = header.epilogSize;
encoding = SET_EPILOGSIZE + header.epilogSize;
goto DO_RETURN;
}
else
{
unsigned hint;
if (initNeeded3(state->epilogSize, header.epilogSize, SET_EPILOGSIZE_MAX, &hint))
{
assert(hint <= SET_EPILOGSIZE_MAX);
state->epilogSize = (BYTE)hint;
encoding = (BYTE)(SET_EPILOGSIZE + hint);
goto DO_RETURN;
}
else
{
assert(hint <= 0x7);
state->epilogSize <<= 3;
state->epilogSize += (BYTE)hint;
encoding = (BYTE)(NEXT_THREE_EPILOGSIZE + hint);
goto DO_RETURN;
}
}
}
if (state->prologSize != header.prologSize)
{
// We have one-byte encodings for 0..16
if (header.prologSize <= SET_PROLOGSIZE_MAX)
{
state->prologSize = header.prologSize;
encoding = SET_PROLOGSIZE + header.prologSize;
goto DO_RETURN;
}
else
{
unsigned hint;
assert(SET_PROLOGSIZE_MAX > 15);
if (initNeeded3(state->prologSize, header.prologSize, 15, &hint))
{
assert(hint <= 15);
state->prologSize = (BYTE)hint;
encoding = (BYTE)(SET_PROLOGSIZE + hint);
goto DO_RETURN;
}
else
{
assert(hint <= 0x7);
state->prologSize <<= 3;
state->prologSize += ((BYTE)hint);
encoding = (BYTE)(NEXT_THREE_PROLOGSIZE + hint);
goto DO_RETURN;
}
}
}
if (state->ediSaved != header.ediSaved)
{
state->ediSaved = header.ediSaved;
encoding = FLIP_EDI_SAVED;
goto DO_RETURN;
}
if (state->esiSaved != header.esiSaved)
{
state->esiSaved = header.esiSaved;
encoding = FLIP_ESI_SAVED;
goto DO_RETURN;
}
if (state->ebxSaved != header.ebxSaved)
{
state->ebxSaved = header.ebxSaved;
encoding = FLIP_EBX_SAVED;
goto DO_RETURN;
}
if (state->ebpSaved != header.ebpSaved)
{
state->ebpSaved = header.ebpSaved;
encoding = FLIP_EBP_SAVED;
goto DO_RETURN;
}
if (state->ebpFrame != header.ebpFrame)
{
state->ebpFrame = header.ebpFrame;
encoding = FLIP_EBP_FRAME;
goto DO_RETURN;
}
if (state->interruptible != header.interruptible)
{
state->interruptible = header.interruptible;
encoding = FLIP_INTERRUPTIBLE;
goto DO_RETURN;
}
#if DOUBLE_ALIGN
if (state->doubleAlign != header.doubleAlign)
{
state->doubleAlign = header.doubleAlign;
encoding = FLIP_DOUBLE_ALIGN;
goto DO_RETURN;
}
#endif
if (state->security != header.security)
{
state->security = header.security;
encoding = FLIP_SECURITY;
goto DO_RETURN;
}
if (state->handlers != header.handlers)
{
state->handlers = header.handlers;
encoding = FLIP_HANDLERS;
goto DO_RETURN;
}
if (state->localloc != header.localloc)
{
state->localloc = header.localloc;
encoding = FLIP_LOCALLOC;
goto DO_RETURN;
}
if (state->editNcontinue != header.editNcontinue)
{
state->editNcontinue = header.editNcontinue;
encoding = FLIP_EDITnCONTINUE;
goto DO_RETURN;
}
if (state->varargs != header.varargs)
{
state->varargs = header.varargs;
encoding = FLIP_VARARGS;
goto DO_RETURN;
}
if (state->profCallbacks != header.profCallbacks)
{
state->profCallbacks = header.profCallbacks;
encoding = FLIP_PROF_CALLBACKS;
goto DO_RETURN;
}
if (state->genericsContext != header.genericsContext)
{
state->genericsContext = header.genericsContext;
encoding = FLIP_HAS_GENERICS_CONTEXT;
goto DO_RETURN;
}
if (state->genericsContextIsMethodDesc != header.genericsContextIsMethodDesc)
{
state->genericsContextIsMethodDesc = header.genericsContextIsMethodDesc;
encoding = FLIP_GENERICS_CONTEXT_IS_METHODDESC;
goto DO_RETURN;
}
if (state->returnKind != header.returnKind)
{
state->returnKind = header.returnKind;
codeSet = 2; // Two byte encoding
encoding = header.returnKind;
_ASSERTE(encoding < SET_RET_KIND_MAX);
goto DO_RETURN;
}
if (state->gsCookieOffset != header.gsCookieOffset)
{
assert(state->gsCookieOffset == INVALID_GS_COOKIE_OFFSET || state->gsCookieOffset == HAS_GS_COOKIE_OFFSET);
if (state->gsCookieOffset == INVALID_GS_COOKIE_OFFSET)
{
// header.gsCookieOffset is non-zero. We can set it
// to zero using FLIP_HAS_GS_COOKIE
state->gsCookieOffset = HAS_GS_COOKIE_OFFSET;
encoding = FLIP_HAS_GS_COOKIE;
goto DO_RETURN;
}
else if (header.gsCookieOffset == INVALID_GS_COOKIE_OFFSET)
{
state->gsCookieOffset = INVALID_GS_COOKIE_OFFSET;
encoding = FLIP_HAS_GS_COOKIE;
goto DO_RETURN;
}
}
if (state->syncStartOffset != header.syncStartOffset)
{
assert(state->syncStartOffset == INVALID_SYNC_OFFSET || state->syncStartOffset == HAS_SYNC_OFFSET);
if (state->syncStartOffset == INVALID_SYNC_OFFSET)
{
// header.syncStartOffset is non-zero. We can set it
// to zero using FLIP_SYNC
state->syncStartOffset = HAS_SYNC_OFFSET;
encoding = FLIP_SYNC;
goto DO_RETURN;
}
else if (header.syncStartOffset == INVALID_SYNC_OFFSET)
{
state->syncStartOffset = INVALID_SYNC_OFFSET;
encoding = FLIP_SYNC;
goto DO_RETURN;
}
}
if (state->revPInvokeOffset != header.revPInvokeOffset)
{
assert(state->revPInvokeOffset == INVALID_REV_PINVOKE_OFFSET ||
state->revPInvokeOffset == HAS_REV_PINVOKE_FRAME_OFFSET);
if (state->revPInvokeOffset == INVALID_REV_PINVOKE_OFFSET)
{
// header.revPInvokeOffset is non-zero.
state->revPInvokeOffset = HAS_REV_PINVOKE_FRAME_OFFSET;
encoding = FLIP_REV_PINVOKE_FRAME;
goto DO_RETURN;
}
else if (header.revPInvokeOffset == INVALID_REV_PINVOKE_OFFSET)
{
state->revPInvokeOffset = INVALID_REV_PINVOKE_OFFSET;
encoding = FLIP_REV_PINVOKE_FRAME;
goto DO_RETURN;
}
}
DO_RETURN:
_ASSERTE(encoding < MORE_BYTES_TO_FOLLOW);
if (!state->isHeaderMatch(header))
encoding |= MORE_BYTES_TO_FOLLOW;
return encoding;
}
static int measureDistance(const InfoHdr& header, const InfoHdrSmall* p, int closeness)
{
int distance = 0;
if (p->untrackedCnt != header.untrackedCnt)
{
if (header.untrackedCnt > 3)
{
if (p->untrackedCnt != HAS_UNTRACKED)
distance += 1;
}
else
{
distance += 1;
}
if (distance >= closeness)
return distance;
}
if (p->varPtrTableSize != header.varPtrTableSize)
{
if (header.varPtrTableSize != 0)
{
if (p->varPtrTableSize != HAS_VARPTR)
distance += 1;
}
else
{
assert(p->varPtrTableSize == HAS_VARPTR);
distance += 1;
}
if (distance >= closeness)
return distance;
}
if (p->frameSize != header.frameSize)
{
distance += 1;
if (distance >= closeness)
return distance;