forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
4324 lines (3768 loc) · 123 KB
/
utils.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.
// ===================================================================================================
// Portions of the code implemented below are based on the 'Berkeley SoftFloat Release 3e' algorithms.
// ===================================================================================================
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Utils.cpp XX
XX XX
XX Has miscellaneous utility functions XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "opcode.h"
#include "jitstd/algorithm.h"
/*****************************************************************************/
#define DECLARE_DATA
// clang-format off
extern
const signed char opcodeSizes[] =
{
#define InlineNone_size 0
#define ShortInlineVar_size 1
#define InlineVar_size 2
#define ShortInlineI_size 1
#define InlineI_size 4
#define InlineI8_size 8
#define ShortInlineR_size 4
#define InlineR_size 8
#define ShortInlineBrTarget_size 1
#define InlineBrTarget_size 4
#define InlineMethod_size 4
#define InlineField_size 4
#define InlineType_size 4
#define InlineString_size 4
#define InlineSig_size 4
#define InlineRVA_size 4
#define InlineTok_size 4
#define InlineSwitch_size 0 // for now
#define InlinePhi_size 0 // for now
#define InlineVarTok_size 0 // remove
#define OPDEF(name,string,pop,push,oprType,opcType,l,s1,s2,ctrl) oprType ## _size ,
#include "opcode.def"
#undef OPDEF
#undef InlineNone_size
#undef ShortInlineVar_size
#undef InlineVar_size
#undef ShortInlineI_size
#undef InlineI_size
#undef InlineI8_size
#undef ShortInlineR_size
#undef InlineR_size
#undef ShortInlineBrTarget_size
#undef InlineBrTarget_size
#undef InlineMethod_size
#undef InlineField_size
#undef InlineType_size
#undef InlineString_size
#undef InlineSig_size
#undef InlineRVA_size
#undef InlineTok_size
#undef InlineSwitch_size
#undef InlinePhi_size
};
// clang-format on
const BYTE varTypeClassification[] = {
#define DEF_TP(tn, nm, jitType, sz, sze, asze, st, al, regTyp, regFld, csr, ctr, tf) tf,
#include "typelist.h"
#undef DEF_TP
};
const BYTE varTypeRegister[] = {
#define DEF_TP(tn, nm, jitType, sz, sze, asze, st, al, regTyp, regFld, csr, ctr, tf) regTyp,
#include "typelist.h"
#undef DEF_TP
};
/*****************************************************************************/
/*****************************************************************************/
#ifdef DEBUG
extern const char* const opcodeNames[] = {
#define OPDEF(name, string, pop, push, oprType, opcType, l, s1, s2, ctrl) string,
#include "opcode.def"
#undef OPDEF
};
extern const BYTE opcodeArgKinds[] = {
#define OPDEF(name, string, pop, push, oprType, opcType, l, s1, s2, ctrl) (BYTE) oprType,
#include "opcode.def"
#undef OPDEF
};
#endif
/*****************************************************************************/
const char* varTypeName(var_types vt)
{
static const char* const varTypeNames[] = {
#define DEF_TP(tn, nm, jitType, sz, sze, asze, st, al, regTyp, regFld, csr, ctr, tf) nm,
#include "typelist.h"
#undef DEF_TP
};
assert((unsigned)vt < ArrLen(varTypeNames));
return varTypeNames[vt];
}
/*****************************************************************************
*
* Return the name of the given register.
*/
const char* getRegName(regNumber reg)
{
// Special-case REG_NA; it's not in the regNames array, but we might want to print it.
if (reg == REG_NA)
{
return "NA";
}
static const char* const regNames[] = {
#if defined(TARGET_ARM64)
#define REGDEF(name, rnum, mask, xname, wname) xname,
#else
#define REGDEF(name, rnum, mask, sname) sname,
#endif
#include "register.h"
};
assert(reg < ArrLen(regNames));
return regNames[reg];
}
const char* getRegName(unsigned reg) // this is for gcencode.cpp and disasm.cpp that dont use the regNumber type
{
return getRegName((regNumber)reg);
}
#if defined(DEBUG)
const char* getRegNameFloat(regNumber reg, var_types type)
{
#ifdef TARGET_ARM
assert(genIsValidFloatReg(reg));
if (type == TYP_FLOAT)
return getRegName(reg);
else
{
const char* regName;
switch (reg)
{
default:
assert(!"Bad double register");
regName = "d??";
break;
case REG_F0:
regName = "d0";
break;
case REG_F2:
regName = "d2";
break;
case REG_F4:
regName = "d4";
break;
case REG_F6:
regName = "d6";
break;
case REG_F8:
regName = "d8";
break;
case REG_F10:
regName = "d10";
break;
case REG_F12:
regName = "d12";
break;
case REG_F14:
regName = "d14";
break;
case REG_F16:
regName = "d16";
break;
case REG_F18:
regName = "d18";
break;
case REG_F20:
regName = "d20";
break;
case REG_F22:
regName = "d22";
break;
case REG_F24:
regName = "d24";
break;
case REG_F26:
regName = "d26";
break;
case REG_F28:
regName = "d28";
break;
case REG_F30:
regName = "d30";
break;
}
return regName;
}
#elif defined(TARGET_ARM64)
static const char* regNamesFloat[] = {
#define REGDEF(name, rnum, mask, xname, wname) xname,
#include "register.h"
};
assert((unsigned)reg < ArrLen(regNamesFloat));
return regNamesFloat[reg];
#elif defined(TARGET_LOONGARCH64)
static const char* regNamesFloat[] = {
#define REGDEF(name, rnum, mask, sname) sname,
#include "register.h"
};
assert((unsigned)reg < ArrLen(regNamesFloat));
return regNamesFloat[reg];
#else
static const char* regNamesFloat[] = {
#define REGDEF(name, rnum, mask, sname) "x" sname,
#include "register.h"
};
#ifdef FEATURE_SIMD
static const char* regNamesYMM[] = {
#define REGDEF(name, rnum, mask, sname) "y" sname,
#include "register.h"
};
static const char* regNamesZMM[] = {
#define REGDEF(name, rnum, mask, sname) "z" sname,
#include "register.h"
};
#endif // FEATURE_SIMD
assert((unsigned)reg < ArrLen(regNamesFloat));
#if defined(FEATURE_SIMD) && defined(TARGET_XARCH)
if (type == TYP_SIMD64)
{
return regNamesZMM[reg];
}
else if (type == TYP_SIMD32)
{
return regNamesYMM[reg];
}
#endif // FEATURE_SIMD && TARGET_XARCH
return regNamesFloat[reg];
#endif
}
/*****************************************************************************
*
* Displays a range of registers
* -- This is a helper used by dspRegMask
*/
const char* dspRegRange(regMaskTP regMask, size_t& minSiz, const char* sep, regNumber regFirst, regNumber regLast)
{
#ifdef FEATURE_MASKED_HW_INTRINSICS
assert(((regFirst == REG_INT_FIRST) && (regLast == REG_INT_LAST)) ||
((regFirst == REG_FP_FIRST) && (regLast == REG_FP_LAST)) ||
((regFirst == REG_MASK_FIRST) && (regLast == REG_MASK_LAST)));
#else
assert(((regFirst == REG_INT_FIRST) && (regLast == REG_INT_LAST)) ||
((regFirst == REG_FP_FIRST) && (regLast == REG_FP_LAST)));
#endif // FEATURE_MASKED_HW_INTRINSICS
if (strlen(sep) > 0)
{
// We've already printed something.
sep = " ";
}
bool inRegRange = false;
regNumber regPrev = REG_NA;
regNumber regHead = REG_NA; // When we start a range, remember the first register of the range, so we don't use
// range notation if the range contains just a single register.
for (regNumber regNum = regFirst; regNum <= regLast; regNum = REG_NEXT(regNum))
{
regMaskTP regBit = genRegMask(regNum);
if ((regMask & regBit).IsNonEmpty())
{
// We have a register to display. It gets displayed now if:
// 1. This is the first register to display of a new range of registers (possibly because
// no register has ever been displayed).
// 2. This is the last register of an acceptable range (either the last register of a type,
// or the last of a range that is displayed with range notation).
if (!inRegRange)
{
// It's the first register of a potential range.
const char* nam = getRegName(regNum);
printf("%s%s", sep, nam);
minSiz -= strlen(sep) + strlen(nam);
// What kind of separator should we use for this range (if it is indeed going to be a range)?
if (genIsValidIntReg(regNum))
{
// By default, we're not starting a potential register range.
sep = " ";
#if defined(TARGET_AMD64)
// For AMD64, create ranges for int registers R8 through R15, but not the "old" registers.
if (regNum >= REG_R8)
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#elif defined(TARGET_ARM64)
// R17 and R28 can't be the start of a range, since the range would include TEB or FP
if ((regNum < REG_R17) || ((REG_R19 <= regNum) && (regNum < REG_R28)))
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#elif defined(TARGET_ARM)
if (regNum < REG_R12)
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#elif defined(TARGET_X86)
// No register ranges
#elif defined(TARGET_LOONGARCH64)
if (REG_A0 <= regNum && regNum <= REG_T8)
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#elif defined(TARGET_RISCV64)
if ((REG_A0 <= regNum && REG_A7 >= regNum) || REG_T0 == regNum || REG_T1 == regNum ||
(REG_T2 <= regNum && REG_T6 >= regNum))
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#else // TARGET*
#error Unsupported or unset target architecture
#endif // TARGET*
}
else
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
}
#if defined(TARGET_ARM64)
else if ((regNum == regLast) || (regNum == REG_R17) // last register before TEB
|| (regNum == REG_R28)) // last register before FP
#elif defined(TARGET_LOONGARCH64)
else if ((regNum == regLast) || (regNum == REG_A7) || (regNum == REG_T8))
#else
else if (regNum == regLast)
#endif
{
// We've already printed a register and hit the end of a range
const char* nam = getRegName(regNum);
printf("%s%s", sep, nam);
minSiz -= strlen(sep) + strlen(nam);
regHead = REG_NA;
inRegRange = false; // No longer in the middle of a register range
sep = " ";
}
}
else if (inRegRange)
{
assert(regHead != REG_NA);
if (regPrev != regHead)
{
// Close out the previous range, if it included more than one register.
const char* nam = getRegName(regPrev);
printf("%s%s", sep, nam);
minSiz -= (strlen(sep) + strlen(nam));
}
regHead = REG_NA;
inRegRange = false;
sep = " ";
}
regPrev = regNum;
}
return sep;
}
/*****************************************************************************
*
* Displays a register set.
* TODO-ARM64-Cleanup: don't allow ip0, ip1 as part of a range.
*/
void dspRegMask(regMaskTP regMask, size_t minSiz)
{
const char* sep = "";
printf("[");
sep = dspRegRange(regMask, minSiz, sep, REG_INT_FIRST, REG_INT_LAST);
sep = dspRegRange(regMask, minSiz, sep, REG_FP_FIRST, REG_FP_LAST);
#ifdef FEATURE_MASKED_HW_INTRINSICS
sep = dspRegRange(regMask, minSiz, sep, REG_MASK_FIRST, REG_MASK_LAST);
#endif // FEATURE_MASKED_HW_INTRINSICS
printf("]");
while ((int)minSiz > 0)
{
printf(" ");
minSiz--;
}
}
//------------------------------------------------------------------------
// dumpILBytes: Helper for dumpSingleInstr() to dump hex bytes of an IL stream,
// aligning up to a minimum alignment width.
//
// Arguments:
// codeAddr - Pointer to IL byte stream to display.
// codeSize - Number of bytes of IL byte stream to display.
// alignSize - Pad out to this many characters, if fewer than this were written.
//
void dumpILBytes(const BYTE* const codeAddr,
unsigned codeSize,
unsigned alignSize) // number of characters to write, for alignment
{
for (IL_OFFSET offs = 0; offs < codeSize; ++offs)
{
printf(" %02x", *(codeAddr + offs));
}
unsigned charsWritten = 3 * codeSize;
for (unsigned i = charsWritten; i < alignSize; i++)
{
printf(" ");
}
}
//------------------------------------------------------------------------
// dumpSingleInstr: Display a single IL instruction.
//
// Arguments:
// codeAddr - Base pointer to a stream of IL instructions.
// offs - Offset from codeAddr of the IL instruction to display.
// prefix - Optional string to prefix the IL instruction with (if nullptr, no prefix is output).
//
// Return Value:
// Size of the displayed IL instruction in the instruction stream, in bytes. (Add this to 'offs' to
// get to the next instruction.)
//
unsigned dumpSingleInstr(const BYTE* const codeAddr, IL_OFFSET offs, const char* prefix)
{
const BYTE* opcodePtr = codeAddr + offs;
const BYTE* startOpcodePtr = opcodePtr;
const unsigned ALIGN_WIDTH = 3 * 6; // assume 3 characters * (1 byte opcode + 4 bytes data + 1 prefix byte) for
// most things
if (prefix != nullptr)
{
printf("%s", prefix);
}
OPCODE opcode = (OPCODE)getU1LittleEndian(opcodePtr);
opcodePtr += sizeof(int8_t);
DECODE_OPCODE:
if (opcode >= CEE_COUNT)
{
printf("\nIllegal opcode: %02X\n", (int)opcode);
return (IL_OFFSET)(opcodePtr - startOpcodePtr);
}
/* Get the size of additional parameters */
size_t sz = opcodeSizes[opcode];
unsigned argKind = opcodeArgKinds[opcode];
/* See what kind of an opcode we have, then */
switch (opcode)
{
case CEE_PREFIX1:
opcode = OPCODE(getU1LittleEndian(opcodePtr) + 256);
opcodePtr += sizeof(int8_t);
goto DECODE_OPCODE;
default:
{
int64_t iOp;
double dOp;
int jOp;
DWORD jOp2;
switch (argKind)
{
case InlineNone:
dumpILBytes(startOpcodePtr, (unsigned)(opcodePtr - startOpcodePtr), ALIGN_WIDTH);
printf(" %-12s", opcodeNames[opcode]);
break;
case ShortInlineVar:
iOp = getU1LittleEndian(opcodePtr);
goto INT_OP;
case ShortInlineI:
iOp = getI1LittleEndian(opcodePtr);
goto INT_OP;
case InlineVar:
iOp = getU2LittleEndian(opcodePtr);
goto INT_OP;
case InlineTok:
case InlineMethod:
case InlineField:
case InlineType:
case InlineString:
case InlineSig:
case InlineI:
iOp = getI4LittleEndian(opcodePtr);
goto INT_OP;
case InlineI8:
iOp = getU4LittleEndian(opcodePtr);
iOp |= (int64_t)getU4LittleEndian(opcodePtr + 4) << 32;
goto INT_OP;
INT_OP:
dumpILBytes(startOpcodePtr, (unsigned)((opcodePtr - startOpcodePtr) + sz), ALIGN_WIDTH);
printf(" %-12s 0x%X", opcodeNames[opcode], iOp);
break;
case ShortInlineR:
dOp = FloatingPointUtils::convertToDouble(getR4LittleEndian(opcodePtr));
goto FLT_OP;
case InlineR:
dOp = getR8LittleEndian(opcodePtr);
goto FLT_OP;
FLT_OP:
dumpILBytes(startOpcodePtr, (unsigned)((opcodePtr - startOpcodePtr) + sz), ALIGN_WIDTH);
printf(" %-12s %f", opcodeNames[opcode], dOp);
break;
case ShortInlineBrTarget:
jOp = getI1LittleEndian(opcodePtr);
goto JMP_OP;
case InlineBrTarget:
jOp = getI4LittleEndian(opcodePtr);
goto JMP_OP;
JMP_OP:
dumpILBytes(startOpcodePtr, (unsigned)((opcodePtr - startOpcodePtr) + sz), ALIGN_WIDTH);
printf(" %-12s %d (IL_%04x)", opcodeNames[opcode], jOp, (int)(opcodePtr + sz - codeAddr) + jOp);
break;
case InlineSwitch:
jOp2 = getU4LittleEndian(opcodePtr);
opcodePtr += 4;
opcodePtr += jOp2 * 4; // Jump over the table
dumpILBytes(startOpcodePtr, (unsigned)(opcodePtr - startOpcodePtr), ALIGN_WIDTH);
printf(" %-12s", opcodeNames[opcode]);
break;
case InlinePhi:
jOp2 = getU1LittleEndian(opcodePtr);
opcodePtr += 1;
opcodePtr += jOp2 * 2; // Jump over the table
dumpILBytes(startOpcodePtr, (unsigned)(opcodePtr - startOpcodePtr), ALIGN_WIDTH);
printf(" %-12s", opcodeNames[opcode]);
break;
default:
assert(!"Bad argKind");
}
opcodePtr += sz;
break;
}
}
printf("\n");
return (IL_OFFSET)(opcodePtr - startOpcodePtr);
}
//------------------------------------------------------------------------
// dumpILRange: Display a range of IL instructions from an IL instruction stream.
//
// Arguments:
// codeAddr - Pointer to IL byte stream to display.
// codeSize - Number of bytes of IL byte stream to display.
//
void dumpILRange(const BYTE* const codeAddr, unsigned codeSize) // in bytes
{
for (IL_OFFSET offs = 0; offs < codeSize;)
{
char prefix[100];
sprintf_s(prefix, ArrLen(prefix), "IL_%04x ", offs);
unsigned codeBytesDumped = dumpSingleInstr(codeAddr, offs, prefix);
offs += codeBytesDumped;
}
}
/*****************************************************************************
*
* Display a variable set.
*/
const char* genES2str(BitVecTraits* traits, EXPSET_TP set)
{
const int bufSize = 65; // Supports a BitVec of up to 256 bits
static char num1[bufSize];
static char num2[bufSize];
static char* nump = num1;
assert(bufSize > roundUp(BitVecTraits::GetSize(traits), (unsigned)sizeof(char)) / 8);
char* temp = nump;
nump = (nump == num1) ? num2 : num1;
sprintf_s(temp, bufSize, "%s", BitVecOps::ToString(traits, set));
return temp;
}
//------------------------------------------------------------------------
// refCntWtd2str: Return a string representation of a weighted ref count
//
// Arguments:
// refCntWtd - weight to format
// padForDecimalPlaces - (default: false) If true, pad any integral or non-numeric
// output on the right with three spaces, representing space
// for ".00". This makes "1" line up with "2.34" at the "2" column.
// This is used for formatting the BasicBlock list.
//
const char* refCntWtd2str(weight_t refCntWtd, bool padForDecimalPlaces)
{
const int bufSize = 17;
static char num1[bufSize];
static char num2[bufSize];
static char* nump = num1;
char* temp = nump;
const char* strDecimalPaddingString = "";
if (padForDecimalPlaces)
{
strDecimalPaddingString = " ";
}
nump = (nump == num1) ? num2 : num1;
if (refCntWtd >= BB_MAX_WEIGHT)
{
sprintf_s(temp, bufSize, "MAX%s", strDecimalPaddingString);
}
else
{
weight_t scaledWeight = refCntWtd / BB_UNITY_WEIGHT;
weight_t intPart = (weight_t)floor(scaledWeight);
bool isLarge = intPart > 1e9;
bool isSmall = (intPart < 1e-2) && (intPart != 0);
// Use g format for high dynamic range counts.
//
if (isLarge || isSmall)
{
sprintf_s(temp, bufSize, "%.2g", scaledWeight);
}
else
{
if (intPart == scaledWeight)
{
sprintf_s(temp, bufSize, "%lld%s", (long long)intPart, strDecimalPaddingString);
}
else
{
sprintf_s(temp, bufSize, "%.2f", scaledWeight);
}
}
}
return temp;
}
#endif // DEBUG
#if defined(DEBUG)
//------------------------------------------------------------------------
// Contains: check if the range includes a particular hash
//
// Arguments:
// hash -- hash value to check
bool ConfigMethodRange::Contains(unsigned hash)
{
_ASSERT(m_inited == 1);
// No ranges specified means all methods included.
if (m_lastRange == 0)
{
return true;
}
for (unsigned i = 0; i < m_lastRange; i++)
{
if ((m_ranges[i].m_low <= hash) && (hash <= m_ranges[i].m_high))
{
return true;
}
}
return false;
}
//------------------------------------------------------------------------
// InitRanges: parse the range string and set up the range info
//
// Arguments:
// rangeStr -- string to parse (may be nullptr)
// capacity -- number ranges to allocate in the range array
//
// Notes:
// Does some internal error checking; clients can use Error()
// to determine if the range string couldn't be fully parsed
// because of bad characters or too many entries, or had values
// that were too large to represent.
void ConfigMethodRange::InitRanges(const char* rangeStr, unsigned capacity)
{
// Make sure that the memory was zero initialized
assert(m_inited == 0 || m_inited == 1);
assert(m_entries == 0);
assert(m_ranges == nullptr);
assert(m_lastRange == 0);
// Flag any strange-looking requests
assert(capacity < 100000);
if (rangeStr == nullptr)
{
m_inited = 1;
return;
}
// Allocate some persistent memory
ICorJitHost* jitHost = g_jitHost;
m_ranges = (Range*)jitHost->allocateMemory(capacity * sizeof(Range));
m_entries = capacity;
const char* p = rangeStr;
unsigned lastRange = 0;
bool setHighPart = false;
while ((*p != 0) && (lastRange < m_entries))
{
while ((*p == ' ') || (*p == ','))
{
p++;
}
int i = 0;
while ((('0' <= *p) && (*p <= '9')) || (('A' <= *p) && (*p <= 'F')) || (('a' <= *p) && (*p <= 'f')))
{
int n = 0;
if (('0' <= *p) && (*p <= '9'))
{
n = (*p++) - '0';
}
else if (('A' <= *p) && (*p <= 'F'))
{
n = (*p++) - 'A' + 10;
}
else if (('a' <= *p) && (*p <= 'f'))
{
n = (*p++) - 'a' + 10;
}
int j = 16 * i + n;
// Check for overflow
if ((m_badChar != 0) && (j <= i))
{
m_badChar = (p - rangeStr) + 1;
}
i = j;
}
// Was this the high part of a low-high pair?
if (setHighPart)
{
// Yep, set it and move to the next range
m_ranges[lastRange].m_high = i;
// Sanity check that range is proper
if ((m_badChar != 0) && (m_ranges[lastRange].m_high < m_ranges[lastRange].m_low))
{
m_badChar = (p - rangeStr) + 1;
}
lastRange++;
setHighPart = false;
continue;
}
// Must have been looking for the low part of a range
m_ranges[lastRange].m_low = i;
while (*p == ' ')
{
p++;
}
// Was that the low part of a low-high pair?
if (*p == '-')
{
// Yep, skip the dash and set high part next time around.
p++;
setHighPart = true;
continue;
}
// Else we have a point range, so set high = low
m_ranges[lastRange].m_high = i;
lastRange++;
}
// If we didn't parse the full range string, note index of the the
// first bad char.
if ((m_badChar != 0) && (*p != 0))
{
m_badChar = (p - rangeStr) + 1;
}
// Finish off any remaining open range
if (setHighPart)
{
m_ranges[lastRange].m_high = UINT_MAX;
lastRange++;
}
assert(lastRange <= m_entries);
m_lastRange = lastRange;
m_inited = 1;
}
//------------------------------------------------------------------------
// Dump: dump hash ranges to stdout
//
void ConfigMethodRange::Dump()
{
if (m_inited != 1)
{
printf("<uninitialized method range>\n");
return;
}
if (m_lastRange == 0)
{
printf("<empty method range>\n");
return;
}
printf("<method range with %d entries>\n", m_lastRange);
for (unsigned i = 0; i < m_lastRange; i++)
{
if (m_ranges[i].m_low == m_ranges[i].m_high)
{
printf("%i [0x%08x]\n", i, m_ranges[i].m_low);
}
else
{
printf("%i [0x%08x-0x%08x]\n", i, m_ranges[i].m_low, m_ranges[i].m_high);
}
}
}
//------------------------------------------------------------------------
// Init: parse a string to set up a ConfigIntArray
//
// Arguments:
// str -- string to parse (may be nullptr)
//
// Notes:
// Values are separated decimal with no whitespace.
// Separators are any digit not '-' or '0-9'
//
void ConfigIntArray::Init(const char* str)
{
// Count the number of values
//
const char* p = str;
unsigned numValues = 0;
while (*p != 0)
{
if ((*p == '-') || (('0' <= *p) && (*p <= '9')))
{
if (*p == '-')
{
p++;
}
while (('0' <= *p) && (*p <= '9'))
{
p++;
}
numValues++;
}
else
{
p++;
}
}
m_length = numValues;
m_values = (int*)g_jitHost->allocateMemory(numValues * sizeof(int));
numValues = 0;
p = str;
int currentValue = 0;
bool isNegative = false;
while (*p != 0)
{
if ((*p == '-') || (('0' <= *p) && (*p <= '9')))
{
if (*p == '-')
{
isNegative = true;
p++;
}
while (('0' <= *p) && (*p <= '9'))
{
currentValue = currentValue * 10 + (*p++) - '0';
}
if (isNegative)
{
currentValue = -currentValue;
}
m_values[numValues++] = currentValue;
currentValue = 0;
}
else
{
p++;
}
}
}
//------------------------------------------------------------------------
// Dump: dump config array to stdout
//
void ConfigIntArray::Dump()
{
if (m_values == nullptr)
{
printf("<uninitialized config int array>\n");
return;
}
if (m_length == 0)
{
printf("<empty config int array>\n");