-
Notifications
You must be signed in to change notification settings - Fork 77
/
JIT.c
1779 lines (1602 loc) · 56.8 KB
/
JIT.c
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
// Copyright (c) 2012 DotNetAnywhere
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Compat.h"
#include "Sys.h"
#include "JIT.h"
#include "JIT_OpCodes.h"
#include "CIL_OpCodes.h"
#include "MetaData.h"
#include "Types.h"
#include "Type.h"
#include "InternalCall.h"
#include "Heap.h"
#include "PInvoke.h"
#define CorILMethod_TinyFormat 0x02
#define CorILMethod_MoreSects 0x08
#define CorILMethod_Sect_EHTable 0x01
#define CorILMethod_Sect_FatFormat 0x40
#define CorILMethod_Sect_MoreSects 0x80
#define DYNAMIC_OK 0x100
#define DYNAMIC_JUMP_TARGET 0x200
#define DYNAMIC_EX_START 0x400
#define DYNAMIC_EX_END 0x800
#define DYNAMIC_BYTE_COUNT_MASK 0xff
typedef struct tOps_ tOps;
struct tOps_ {
U32 *p;
U32 capacity;
U32 ofs;
};
typedef struct tTypeStack_ tTypeStack;
struct tTypeStack_ {
tMD_TypeDef **ppTypes;
U32 ofs;
U32 maxBytes; // The max size of the stack in bytes
};
#define InitOps(ops_, initialCapacity) ops_.capacity = initialCapacity; ops_.ofs = 0; ops_.p = malloc((initialCapacity) * sizeof(I32));
#define DeleteOps(ops_) free(ops_.p)
// Turn this into a MACRO at some point?
static U32 Translate(U32 op, U32 getDynamic) {
if (op >= JIT_OPCODE_MAXNUM) {
Crash("Illegal opcode: %d", op);
}
if (jitCodeInfo[op].pEnd == NULL) {
Crash("Opcode not available: 0x%08x", op);
}
if (getDynamic) {
return (U32)jitCodeInfo[op].isDynamic;
} else {
return (U32)jitCodeInfo[op].pStart;
}
}
#ifdef GEN_COMBINED_OPCODES
#define PushU32(v) PushU32_(&ops, (U32)(v)); PushU32_(&isDynamic, 0)
#define PushI32(v) PushU32_(&ops, (U32)(v)); PushU32_(&isDynamic, 0)
#define PushFloat(v) convFloat.f=(float)(v); PushU32_(&ops, convFloat.u32); PushU32_(&isDynamic, 0)
#define PushDouble(v) convDouble.d=(double)(v); PushU32_(&ops, convDouble.u32.a); PushU32_(&ops, convDouble.u32.b); PushU32_(&isDynamic, 0); PushU32_(&isDynamic, 0)
#define PushPTR(ptr) PushU32_(&ops, (U32)(ptr)); PushU32_(&isDynamic, 0)
#define PushOp(op) PushU32_(&ops, Translate((U32)(op), 0)); PushU32_(&isDynamic, Translate((U32)(op), 1))
#define PushOpParam(op, param) PushOp(op); PushU32_(&ops, (U32)(param)); PushU32_(&isDynamic, 0)
#else
#define PushU32(v) PushU32_(&ops, (U32)(v))
#define PushI32(v) PushU32_(&ops, (U32)(v))
#define PushFloat(v) convFloat.f=(float)(v); PushU32_(&ops, convFloat.u32)
#define PushDouble(v) convDouble.d=(double)(v); PushU32_(&ops, convDouble.u32.a); PushU32_(&ops, convDouble.u32.b)
#define PushPTR(ptr) PushU32_(&ops, (U32)(ptr))
#define PushOp(op) PushU32_(&ops, Translate((U32)(op), 0))
#define PushOpParam(op, param) PushOp(op); PushU32_(&ops, (U32)(param))
#endif
#define PushBranch() PushU32_(&branchOffsets, ops.ofs)
#define PushStackType(type) PushStackType_(&typeStack, type);
#define PopStackType() (typeStack.ppTypes[--typeStack.ofs])
#define PopStackTypeDontCare() typeStack.ofs--
#define PopStackTypeMulti(number) typeStack.ofs -= number
#define PopStackTypeAll() typeStack.ofs = 0;
#define MayCopyTypeStack() if (u32Value > cilOfs) ppTypeStacks[u32Value] = DeepCopyTypeStack(&typeStack)
static void PushStackType_(tTypeStack *pTypeStack, tMD_TypeDef *pType) {
U32 i, size;
MetaData_Fill_TypeDef(pType, NULL, NULL);
pTypeStack->ppTypes[pTypeStack->ofs++] = pType;
// Count current stack size in bytes
size = 0;
for (i=0; i<pTypeStack->ofs; i++) {
size += pTypeStack->ppTypes[i]->stackSize;
}
if (size > pTypeStack->maxBytes) {
pTypeStack->maxBytes = size;
}
//printf("Stack ofs = %d; Max stack size: %d (0x%x)\n", pTypeStack->ofs, size, size);
}
static void PushU32_(tOps *pOps, U32 v) {
if (pOps->ofs >= pOps->capacity) {
pOps->capacity <<= 1;
// printf("a.pOps->p = 0x%08x size=%d\n", pOps->p, pOps->capacity * sizeof(U32));
pOps->p = realloc(pOps->p, pOps->capacity * sizeof(U32));
}
pOps->p[pOps->ofs++] = v;
}
static U32 GetUnalignedU32(U8 *pCIL, U32 *pCILOfs) {
U32 a,b,c,d;
a = pCIL[(*pCILOfs)++];
b = pCIL[(*pCILOfs)++];
c = pCIL[(*pCILOfs)++];
d = pCIL[(*pCILOfs)++];
return a | (b << 8) | (c << 16) | (d << 24);
}
static tTypeStack* DeepCopyTypeStack(tTypeStack *pToCopy) {
tTypeStack *pCopy;
pCopy = TMALLOC(tTypeStack);
pCopy->maxBytes = pToCopy->maxBytes;
pCopy->ofs = pToCopy->ofs;
if (pToCopy->ofs > 0) {
pCopy->ppTypes = malloc(pToCopy->ofs * sizeof(tMD_TypeDef*));
memcpy(pCopy->ppTypes, pToCopy->ppTypes, pToCopy->ofs * sizeof(tMD_TypeDef*));
} else {
pCopy->ppTypes = NULL;
}
return pCopy;
}
static void RestoreTypeStack(tTypeStack *pMainStack, tTypeStack *pCopyFrom) {
// This does not effect maxBytes, as the current value will always be equal
// or greater than the value being copied from.
if (pCopyFrom == NULL) {
pMainStack->ofs = 0;
} else {
pMainStack->ofs = pCopyFrom->ofs;
if (pCopyFrom->ppTypes != NULL) {
memcpy(pMainStack->ppTypes, pCopyFrom->ppTypes, pCopyFrom->ofs * sizeof(tMD_TypeDef*));
}
}
}
#ifdef GEN_COMBINED_OPCODES
static U32 FindOpCode(void *pAddr) {
U32 i;
for (i=0; i<JIT_OPCODE_MAXNUM; i++) {
if (jitCodeInfo[i].pStart == pAddr) {
return i;
}
}
Crash("Cannot find opcode for address: 0x%08x", (U32)pAddr);
FAKE_RETURN;
}
static U32 combinedMemSize = 0;
static U32 GenCombined(tOps *pOps, tOps *pIsDynamic, U32 startOfs, U32 count, U32 *pCombinedSize, void **ppMem) {
U32 memSize;
U32 ofs;
void *pCombined;
U32 opCopyToOfs;
U32 shrinkOpsBy;
U32 goNextSize = (U32)((char*)jitCodeGoNext.pEnd - (char*)jitCodeGoNext.pStart);
// Get length of final combined code chunk
memSize = 0;
for (ofs=0; ofs < count; ofs++) {
U32 opcode = FindOpCode((void*)pOps->p[startOfs + ofs]);
U32 size = (U32)((char*)jitCodeInfo[opcode].pEnd - (char*)jitCodeInfo[opcode].pStart);
memSize += size;
ofs += (pIsDynamic->p[startOfs + ofs] & DYNAMIC_BYTE_COUNT_MASK) >> 2;
}
// Add length of GoNext code
memSize += goNextSize;
pCombined = malloc(memSize);
*ppMem = pCombined;
combinedMemSize += memSize;
*pCombinedSize = memSize;
//log_f(0, "Combined JIT size: %d\n", combinedMemSize);
// Copy the bits of code into place
memSize = 0;
opCopyToOfs = 1;
for (ofs=0; ofs < count; ofs++) {
U32 extraOpBytes;
U32 opcode = FindOpCode((void*)pOps->p[startOfs + ofs]);
U32 size = (U32)((char*)jitCodeInfo[opcode].pEnd - (char*)jitCodeInfo[opcode].pStart);
memcpy((char*)pCombined + memSize, jitCodeInfo[opcode].pStart, size);
memSize += size;
extraOpBytes = pIsDynamic->p[startOfs + ofs] & DYNAMIC_BYTE_COUNT_MASK;
memmove(&pOps->p[startOfs + opCopyToOfs], &pOps->p[startOfs + ofs + 1], extraOpBytes);
opCopyToOfs += extraOpBytes >> 2;
ofs += extraOpBytes >> 2;
}
shrinkOpsBy = ofs - opCopyToOfs;
// Add GoNext code
memcpy((char*)pCombined + memSize, jitCodeGoNext.pStart, goNextSize);
pOps->p[startOfs] = (U32)pCombined;
return shrinkOpsBy;
}
#endif
static U32* JITit(tMD_MethodDef *pMethodDef, U8 *pCIL, U32 codeSize, tParameter *pLocals, tJITted *pJITted, U32 genCombinedOpcodes) {
U32 maxStack = pJITted->maxStack;
U32 i;
U32 cilOfs;
tOps ops; // The JITted op-codes
tOps branchOffsets; // Filled with all the branch instructions that need offsets fixing
U32 *pJITOffsets; // To store the JITted code offset of each CIL byte.
// Only CIL bytes that are the first byte of an instruction will have meaningful data
tTypeStack **ppTypeStacks; // To store the evaluation stack state for forward jumps
U32 *pFinalOps;
tMD_TypeDef *pStackType;
tTypeStack typeStack;
#ifdef GEN_COMBINED_OPCODES
tOps isDynamic;
#endif
I32 i32Value;
U32 u32Value, u32Value2, ofs;
uConvFloat convFloat;
uConvDouble convDouble;
tMD_TypeDef *pTypeA, *pTypeB;
PTR pMem;
tMetaData *pMetaData;
pMetaData = pMethodDef->pMetaData;
pJITOffsets = malloc(codeSize * sizeof(U32));
// + 1 to handle cases where the stack is being restored at the last instruction in a method
ppTypeStacks = malloc((codeSize + 1) * sizeof(tTypeStack*));
memset(ppTypeStacks, 0, (codeSize + 1) * sizeof(tTypeStack*));
typeStack.maxBytes = 0;
typeStack.ofs = 0;
typeStack.ppTypes = malloc(maxStack * sizeof(tMD_TypeDef*));
// Set up all exception 'catch' blocks with the correct stack information,
// So they'll have just the exception type on the stack when entered
for (i=0; i<pJITted->numExceptionHandlers; i++) {
tExceptionHeader *pEx;
pEx = &pJITted->pExceptionHeaders[i];
if (pEx->flags == COR_ILEXCEPTION_CLAUSE_EXCEPTION) {
tTypeStack *pTypeStack;
ppTypeStacks[pEx->handlerStart] = pTypeStack = TMALLOC(tTypeStack);
pTypeStack->maxBytes = 4;
pTypeStack->ofs = 1;
pTypeStack->ppTypes = TMALLOC(tMD_TypeDef*);
pTypeStack->ppTypes[0] = pEx->u.pCatchTypeDef;
}
}
InitOps(ops, 32);
InitOps(branchOffsets, 16);
#ifdef GEN_COMBINED_OPCODES
InitOps(isDynamic, 32);
#endif
cilOfs = 0;
do {
U8 op;
// Set the JIT offset for this CIL opcode
pJITOffsets[cilOfs] = ops.ofs;
op = pCIL[cilOfs++];
//printf("Opcode: 0x%02x\n", op);
switch (op) {
case CIL_NOP:
PushOp(JIT_NOP);
break;
case CIL_LDNULL:
PushOp(JIT_LOAD_NULL);
PushStackType(types[TYPE_SYSTEM_OBJECT]);
break;
case CIL_DUP:
pStackType = PopStackType();
PushStackType(pStackType);
PushStackType(pStackType);
switch (pStackType->stackSize) {
case 4:
PushOp(JIT_DUP_4);
break;
case 8:
PushOp(JIT_DUP_8);
break;
default:
PushOpParam(JIT_DUP_GENERAL, pStackType->stackSize);
break;
}
break;
case CIL_POP:
pStackType = PopStackType();
if (pStackType->stackSize == 4) {
PushOp(JIT_POP_4);
} else {
PushOpParam(JIT_POP, pStackType->stackSize);
}
break;
case CIL_LDC_I4_M1:
case CIL_LDC_I4_0:
case CIL_LDC_I4_1:
case CIL_LDC_I4_2:
case CIL_LDC_I4_3:
case CIL_LDC_I4_4:
case CIL_LDC_I4_5:
case CIL_LDC_I4_6:
case CIL_LDC_I4_7:
case CIL_LDC_I4_8:
i32Value = (I8)op - (I8)CIL_LDC_I4_0;
goto cilLdcI4;
case CIL_LDC_I4_S:
i32Value = (I8)pCIL[cilOfs++];
goto cilLdcI4;
case CIL_LDC_I4:
i32Value = (I32)GetUnalignedU32(pCIL, &cilOfs);
cilLdcI4:
if (i32Value >= -1 && i32Value <= 2) {
PushOp(JIT_LOAD_I4_0 + i32Value);
} else {
PushOp(JIT_LOAD_I32);
PushI32(i32Value);
}
PushStackType(types[TYPE_SYSTEM_INT32]);
break;
case CIL_LDC_I8:
PushOp(JIT_LOAD_I64);
u32Value = GetUnalignedU32(pCIL, &cilOfs);
PushU32(u32Value);
u32Value = GetUnalignedU32(pCIL, &cilOfs);
PushU32(u32Value);
PushStackType(types[TYPE_SYSTEM_INT64]);
break;
case CIL_LDC_R4:
convFloat.u32 = GetUnalignedU32(pCIL, &cilOfs);
PushStackType(types[TYPE_SYSTEM_SINGLE]);
PushOp(JIT_LOAD_F32);
PushFloat(convFloat.f);
break;
case CIL_LDC_R8:
convDouble.u32.a = GetUnalignedU32(pCIL, &cilOfs);
convDouble.u32.b = GetUnalignedU32(pCIL, &cilOfs);
PushStackType(types[TYPE_SYSTEM_DOUBLE]);
PushOp(JIT_LOAD_F64);
PushDouble(convDouble.d);
break;
case CIL_LDARG_0:
case CIL_LDARG_1:
case CIL_LDARG_2:
case CIL_LDARG_3:
u32Value = op - CIL_LDARG_0;
goto cilLdArg;
case CIL_LDARG_S:
u32Value = pCIL[cilOfs++];
cilLdArg:
pStackType = pMethodDef->pParams[u32Value].pTypeDef;
ofs = pMethodDef->pParams[u32Value].offset;
if (pStackType->stackSize == 4 && ofs < 32) {
PushOp(JIT_LOADPARAMLOCAL_0 + (ofs >> 2));
} else {
PushOpParam(JIT_LOADPARAMLOCAL_TYPEID + pStackType->stackType, ofs);
// if it's a valuetype then push the TypeDef of it afterwards
if (pStackType->stackType == EVALSTACK_VALUETYPE) {
PushPTR(pStackType);
}
}
PushStackType(pStackType);
break;
case CIL_LDARGA_S:
// Get the argument number to load the address of
u32Value = pCIL[cilOfs++];
PushOpParam(JIT_LOAD_PARAMLOCAL_ADDR, pMethodDef->pParams[u32Value].offset);
PushStackType(types[TYPE_SYSTEM_INTPTR]);
break;
case CIL_STARG_S:
// Get the argument number to store the arg of
u32Value = pCIL[cilOfs++];
pStackType = PopStackType();
ofs = pMethodDef->pParams[u32Value].offset;
if (pStackType->stackSize == 4 && ofs < 32) {
PushOp(JIT_STOREPARAMLOCAL_0 + (ofs >> 2));
} else {
PushOpParam(JIT_STOREPARAMLOCAL_TYPEID + pStackType->stackType, ofs);
// if it's a valuetype then push the TypeDef of it afterwards
if (pStackType->stackType == EVALSTACK_VALUETYPE) {
PushPTR(pStackType);
}
}
break;
case CIL_LDLOC_0:
case CIL_LDLOC_1:
case CIL_LDLOC_2:
case CIL_LDLOC_3:
// Push opcode and offset into locals memory
u32Value = op - CIL_LDLOC_0;
goto cilLdLoc;
case CIL_LDLOC_S:
// Push opcode and offset into locals memory
u32Value = pCIL[cilOfs++];
cilLdLoc:
pStackType = pLocals[u32Value].pTypeDef;
ofs = pMethodDef->parameterStackSize + pLocals[u32Value].offset;
if (pStackType->stackSize == 4 && ofs < 32) {
PushOp(JIT_LOADPARAMLOCAL_0 + (ofs >> 2));
} else {
PushOpParam(JIT_LOADPARAMLOCAL_TYPEID + pStackType->stackType, ofs);
// if it's a valuetype then push the TypeDef of it afterwards
if (pStackType->stackType == EVALSTACK_VALUETYPE) {
PushPTR(pStackType);
}
}
PushStackType(pStackType);
break;
case CIL_STLOC_0:
case CIL_STLOC_1:
case CIL_STLOC_2:
case CIL_STLOC_3:
u32Value = op - CIL_STLOC_0;
goto cilStLoc;
case CIL_STLOC_S:
u32Value = pCIL[cilOfs++];
cilStLoc:
pStackType = PopStackType();
ofs = pMethodDef->parameterStackSize + pLocals[u32Value].offset;
if (pStackType->stackSize == 4 && ofs < 32) {
PushOp(JIT_STOREPARAMLOCAL_0 + (ofs >> 2));
} else {
PushOpParam(JIT_STOREPARAMLOCAL_TYPEID + pStackType->stackType, ofs);
// if it's a valuetype then push the TypeDef of it afterwards
if (pStackType->stackType == EVALSTACK_VALUETYPE) {
PushPTR(pStackType);
}
}
break;
case CIL_LDLOCA_S:
// Get the local number to load the address of
u32Value = pCIL[cilOfs++];
PushOpParam(JIT_LOAD_PARAMLOCAL_ADDR, pMethodDef->parameterStackSize + pLocals[u32Value].offset);
PushStackType(types[TYPE_SYSTEM_INTPTR]);
break;
case CIL_LDIND_I1:
u32Value = TYPE_SYSTEM_SBYTE;
goto cilLdInd;
case CIL_LDIND_U1:
u32Value = TYPE_SYSTEM_BYTE;
goto cilLdInd;
case CIL_LDIND_I2:
u32Value = TYPE_SYSTEM_INT16;
goto cilLdInd;
case CIL_LDIND_U2:
u32Value = TYPE_SYSTEM_UINT16;
goto cilLdInd;
case CIL_LDIND_I4:
u32Value = TYPE_SYSTEM_INT32;
goto cilLdInd;
case CIL_LDIND_U4:
u32Value = TYPE_SYSTEM_UINT32;
goto cilLdInd;
case CIL_LDIND_I8:
u32Value = TYPE_SYSTEM_INT64;
goto cilLdInd;
case CIL_LDIND_R4:
u32Value = TYPE_SYSTEM_SINGLE;
goto cilLdInd;
case CIL_LDIND_R8:
u32Value = TYPE_SYSTEM_DOUBLE;
goto cilLdInd;
case CIL_LDIND_REF:
u32Value = TYPE_SYSTEM_OBJECT;
goto cilLdInd;
case CIL_LDIND_I:
u32Value = TYPE_SYSTEM_INTPTR;
cilLdInd:
PopStackTypeDontCare(); // don't care what it is
PushOp(JIT_LOADINDIRECT_I8 + (op - CIL_LDIND_I1));
PushStackType(types[u32Value]);
break;
case CIL_STIND_REF:
case CIL_STIND_I1:
case CIL_STIND_I2:
case CIL_STIND_I4:
PopStackTypeMulti(2); // Don't care what they are
PushOp(JIT_STOREINDIRECT_REF + (op - CIL_STIND_REF));
break;
case CIL_RET:
PushOp(JIT_RETURN);
RestoreTypeStack(&typeStack, ppTypeStacks[cilOfs]);
break;
case CIL_CALL:
case CIL_CALLVIRT:
{
tMD_MethodDef *pCallMethod;
tMD_TypeDef *pBoxCallType;
U32 derefRefType;
u32Value2 = 0;
cilCallVirtConstrained:
pBoxCallType = NULL;
derefRefType = 0;
u32Value = GetUnalignedU32(pCIL, &cilOfs);
pCallMethod = MetaData_GetMethodDefFromDefRefOrSpec(pMetaData, u32Value, pMethodDef->pParentType->ppClassTypeArgs, pMethodDef->ppMethodTypeArgs);
if (pCallMethod->isFilled == 0) {
tMD_TypeDef *pTypeDef;
pTypeDef = MetaData_GetTypeDefFromMethodDef(pCallMethod);
MetaData_Fill_TypeDef(pTypeDef, NULL, NULL);
}
if (u32Value2 != 0) {
// There is a 'constrained' prefix
tMD_TypeDef *pConstrainedType;
pConstrainedType = MetaData_GetTypeDefFromDefRefOrSpec(pMetaData, u32Value2, pMethodDef->pParentType->ppClassTypeArgs, pMethodDef->ppMethodTypeArgs);
if (TYPE_ISINTERFACE(pCallMethod->pParentType)) {
u32Value2 = 0xffffffff;
// Find the interface that we're dealing with
for (i=0; i<pConstrainedType->numInterfaces; i++) {
if (pConstrainedType->pInterfaceMaps[i].pInterface == pCallMethod->pParentType) {
u32Value2 = pConstrainedType->pInterfaceMaps[i].pVTableLookup[pCallMethod->vTableOfs];
break;
}
}
Assert(u32Value2 != 0xffffffff);
if (pConstrainedType->pVTable[u32Value2]->pParentType == pConstrainedType) {
// This method is implemented on this class, so make it a normal CALL op
op = CIL_CALL;
pCallMethod = pConstrainedType->pVTable[u32Value2];
}
} else {
if (pConstrainedType->isValueType) {
tMD_MethodDef *pImplMethod;
// If pConstraintedType directly implements the call then don't do anything
// otherwise the 'this' pointer must be boxed (BoxedCall)
pImplMethod = pConstrainedType->pVTable[pCallMethod->vTableOfs];
if (pImplMethod->pParentType == pConstrainedType) {
op = CIL_CALL;
pCallMethod = pImplMethod;
} else {
pBoxCallType = pConstrainedType;
}
} else {
// Reference-type, so dereference the PTR to 'this' and use that for the 'this' for the call.
derefRefType = 1;
}
}
}
// Pop stack type for each argument. Don't actually care what these are,
// except the last one which will be the 'this' object type of a non-static method
//printf("Call %s() - popping %d stack args\n", pCallMethod->name, pCallMethod->numberOfParameters);
for (i=0; i<pCallMethod->numberOfParameters; i++) {
pStackType = PopStackType();
}
// the stack type of the 'this' object will now be in stackType (if there is one)
if (METHOD_ISSTATIC(pCallMethod)) {
pStackType = types[TYPE_SYSTEM_OBJECT];
}
MetaData_Fill_TypeDef(pStackType, NULL, NULL);
if (TYPE_ISINTERFACE(pCallMethod->pParentType) && op == CIL_CALLVIRT) {
PushOp(JIT_CALL_INTERFACE);
} else if (pCallMethod->pParentType->pParent == types[TYPE_SYSTEM_MULTICASTDELEGATE]) {
PushOp(JIT_INVOKE_DELEGATE);
} else {
switch (pStackType->stackType)
{
case EVALSTACK_INTNATIVE: // Not really right, but it'll work on 32-bit
case EVALSTACK_O:
if (derefRefType) {
PushOp(JIT_DEREF_CALLVIRT);
} else {
if (pBoxCallType != NULL) {
PushOp(JIT_BOX_CALLVIRT);
PushPTR(pBoxCallType);
} else {
PushOp((op == CIL_CALL)?JIT_CALL_O:JIT_CALLVIRT_O);
}
}
break;
case EVALSTACK_PTR:
case EVALSTACK_VALUETYPE:
if (derefRefType) {
PushOp(JIT_DEREF_CALLVIRT);
} else if (pBoxCallType != NULL) {
PushOp(JIT_BOX_CALLVIRT);
PushPTR(pBoxCallType);
} else {
PushOp(JIT_CALL_PTR);
}
break;
default:
Crash("JITit(): Cannot CALL or CALLVIRT with stack type: %d", pStackType->stackType);
}
}
PushPTR(pCallMethod);
if (pCallMethod->pReturnType != NULL) {
PushStackType(pCallMethod->pReturnType);
}
}
break;
case CIL_BR_S: // unconditional branch
u32Value = (I8)pCIL[cilOfs++];
goto cilBr;
case CIL_BR:
u32Value = GetUnalignedU32(pCIL, &cilOfs);
cilBr:
// Put a temporary CIL offset value into the JITted code. This will be updated later
u32Value = cilOfs + (I32)u32Value;
MayCopyTypeStack();
PushOp(JIT_BRANCH);
PushBranch();
PushU32(u32Value);
// Restore the stack state
RestoreTypeStack(&typeStack, ppTypeStacks[cilOfs]);
break;
case CIL_SWITCH:
// This is the int containing the switch value. Don't care what it is.
PopStackTypeDontCare();
// The number of switch jump targets
i32Value = (I32)GetUnalignedU32(pCIL, &cilOfs);
// Set up the offset from which the jump offsets are calculated
u32Value2 = cilOfs + (i32Value << 2);
PushOpParam(JIT_SWITCH, i32Value);
for (i=0; i<(U32)i32Value; i++) {
// A jump target
u32Value = u32Value2 + (I32)GetUnalignedU32(pCIL, &cilOfs);
PushBranch();
MayCopyTypeStack();
// Push the jump target.
// It is needed to allow the branch offset to be correctly updated later.
PushU32(u32Value);
}
break;
case CIL_BRFALSE_S:
case CIL_BRTRUE_S:
u32Value = (I8)pCIL[cilOfs++];
u32Value2 = JIT_BRANCH_FALSE + (op - CIL_BRFALSE_S);
goto cilBrFalseTrue;
case CIL_BRFALSE:
case CIL_BRTRUE:
u32Value = GetUnalignedU32(pCIL, &cilOfs);
u32Value2 = JIT_BRANCH_FALSE + (op - CIL_BRFALSE);
cilBrFalseTrue:
PopStackTypeDontCare(); // Don't care what it is
// Put a temporary CIL offset value into the JITted code. This will be updated later
u32Value = cilOfs + (I32)u32Value;
MayCopyTypeStack();
PushOp(u32Value2);
PushBranch();
PushU32(u32Value);
break;
case CIL_BEQ_S:
case CIL_BGE_S:
case CIL_BGT_S:
case CIL_BLE_S:
case CIL_BLT_S:
case CIL_BNE_UN_S:
case CIL_BGE_UN_S:
case CIL_BGT_UN_S:
case CIL_BLE_UN_S:
case CIL_BLT_UN_S:
u32Value = (I8)pCIL[cilOfs++];
u32Value2 = CIL_BEQ_S;
goto cilBrCond;
case CIL_BEQ:
case CIL_BGE:
case CIL_BGT:
case CIL_BLE:
case CIL_BLT:
case CIL_BNE_UN:
case CIL_BGE_UN:
case CIL_BGT_UN:
case CIL_BLE_UN:
case CIL_BLT_UN:
u32Value = GetUnalignedU32(pCIL, &cilOfs);
u32Value2 = CIL_BEQ;
cilBrCond:
pTypeB = PopStackType();
pTypeA = PopStackType();
u32Value = cilOfs + (I32)u32Value;
MayCopyTypeStack();
if ((pTypeA->stackType == EVALSTACK_INT32 && pTypeB->stackType == EVALSTACK_INT32) ||
(pTypeA->stackType == EVALSTACK_O && pTypeB->stackType == EVALSTACK_O)) {
PushOp(JIT_BEQ_I32I32 + (op - u32Value2));
} else if (pTypeA->stackType == EVALSTACK_INT64 && pTypeB->stackType == EVALSTACK_INT64) {
PushOp(JIT_BEQ_I64I64 + (op - u32Value2));
} else if (pTypeA->stackType == EVALSTACK_F32 && pTypeB->stackType == EVALSTACK_F32) {
PushOp(JIT_BEQ_F32F32 + (op - u32Value2));
} else if (pTypeA->stackType == EVALSTACK_F64 && pTypeB->stackType == EVALSTACK_F64) {
PushOp(JIT_BEQ_F64F64 + (op - u32Value2));
} else {
Crash("JITit(): Cannot perform conditional branch on stack types: %d and %d", pTypeA->stackType, pTypeB->stackType);
}
PushBranch();
PushU32(u32Value);
break;
case CIL_ADD_OVF:
case CIL_ADD_OVF_UN:
case CIL_MUL_OVF:
case CIL_MUL_OVF_UN:
case CIL_SUB_OVF:
case CIL_SUB_OVF_UN:
u32Value = (CIL_ADD_OVF - CIL_ADD) + (JIT_ADD_I32I32 - JIT_ADD_OVF_I32I32);
goto cilBinaryArithOp;
case CIL_ADD:
case CIL_SUB:
case CIL_MUL:
case CIL_DIV:
case CIL_DIV_UN:
case CIL_REM:
case CIL_REM_UN:
case CIL_AND:
case CIL_OR:
case CIL_XOR:
u32Value = 0;
cilBinaryArithOp:
pTypeB = PopStackType();
pTypeA = PopStackType();
if (pTypeA->stackType == EVALSTACK_INT32 && pTypeB->stackType == EVALSTACK_INT32) {
PushOp(JIT_ADD_I32I32 + (op - CIL_ADD) - u32Value);
PushStackType(types[TYPE_SYSTEM_INT32]);
} else if (pTypeA->stackType == EVALSTACK_INT64 && pTypeB->stackType == EVALSTACK_INT64) {
PushOp(JIT_ADD_I64I64 + (op - CIL_ADD) - u32Value);
PushStackType(types[TYPE_SYSTEM_INT64]);
} else if (pTypeA->stackType == EVALSTACK_F32 && pTypeB->stackType == EVALSTACK_F32) {
PushOp(JIT_ADD_F32F32 + (op - CIL_ADD) - u32Value);
PushStackType(pTypeA);
} else if (pTypeA->stackType == EVALSTACK_F64 && pTypeB->stackType == EVALSTACK_F64) {
PushOp(JIT_ADD_F64F64 + (op - CIL_ADD) - u32Value);
PushStackType(pTypeA);
} else {
Crash("JITit(): Cannot perform binary numeric operand on stack types: %d and %d", pTypeA->stackType, pTypeB->stackType);
}
break;
case CIL_NEG:
case CIL_NOT:
pTypeA = PopStackType();
if (pTypeA->stackType == EVALSTACK_INT32) {
PushOp(JIT_NEG_I32 + (op - CIL_NEG));
PushStackType(types[TYPE_SYSTEM_INT32]);
} else if (pTypeA->stackType == EVALSTACK_INT64) {
PushOp(JIT_NEG_I64 + (op - CIL_NEG));
PushStackType(types[TYPE_SYSTEM_INT64]);
} else {
Crash("JITit(): Cannot perform unary operand on stack types: %d", pTypeA->stackType);
}
break;
case CIL_SHL:
case CIL_SHR:
case CIL_SHR_UN:
PopStackTypeDontCare(); // Don't care about the shift amount
pTypeA = PopStackType(); // Do care about the value to shift
if (pTypeA->stackType == EVALSTACK_INT32) {
PushOp(JIT_SHL_I32 - CIL_SHL + op);
PushStackType(types[TYPE_SYSTEM_INT32]);
} else if (pTypeA->stackType == EVALSTACK_INT64) {
PushOp(JIT_SHL_I64 - CIL_SHL + op);
PushStackType(types[TYPE_SYSTEM_INT64]);
} else {
Crash("JITit(): Cannot perform shift operation on type: %s", pTypeA->name);
}
break;
// Conversion operations
{
U32 toType;
U32 toBitCount;
U32 convOpOffset;
case CIL_CONV_I1:
case CIL_CONV_OVF_I1: // Fix this later - will never overflow
case CIL_CONV_OVF_I1_UN: // Fix this later - will never overflow
toBitCount = 8;
toType = TYPE_SYSTEM_SBYTE;
goto cilConvInt32;
case CIL_CONV_I2:
case CIL_CONV_OVF_I2: // Fix this later - will never overflow
case CIL_CONV_OVF_I2_UN: // Fix this later - will never overflow
toBitCount = 16;
toType = TYPE_SYSTEM_INT16;
goto cilConvInt32;
case CIL_CONV_I4:
case CIL_CONV_OVF_I4: // Fix this later - will never overflow
case CIL_CONV_OVF_I4_UN: // Fix this later - will never overflow
case CIL_CONV_I: // Only on 32-bit
case CIL_CONV_OVF_I_UN: // Only on 32-bit; Fix this later - will never overflow
toBitCount = 32;
toType = TYPE_SYSTEM_INT32;
cilConvInt32:
convOpOffset = JIT_CONV_OFFSET_I32;
goto cilConv;
case CIL_CONV_U1:
case CIL_CONV_OVF_U1: // Fix this later - will never overflow
case CIL_CONV_OVF_U1_UN: // Fix this later - will never overflow
toBitCount = 8;
toType = TYPE_SYSTEM_BYTE;
goto cilConvUInt32;
case CIL_CONV_U2:
case CIL_CONV_OVF_U2: // Fix this later - will never overflow
case CIL_CONV_OVF_U2_UN: // Fix this later - will never overflow
toBitCount = 16;
toType = TYPE_SYSTEM_UINT16;
goto cilConvUInt32;
case CIL_CONV_U4:
case CIL_CONV_OVF_U4: // Fix this later - will never overflow
case CIL_CONV_OVF_U4_UN: // Fix this later - will never overflow
case CIL_CONV_U: // Only on 32-bit
case CIL_CONV_OVF_U_UN: // Only on 32-bit; Fix this later - will never overflow
toBitCount = 32;
toType = TYPE_SYSTEM_UINT32;
cilConvUInt32:
convOpOffset = JIT_CONV_OFFSET_U32;
goto cilConv;
case CIL_CONV_I8:
case CIL_CONV_OVF_I8: // Fix this later - will never overflow
case CIL_CONV_OVF_I8_UN: // Fix this later - will never overflow
toType = TYPE_SYSTEM_INT64;
convOpOffset = JIT_CONV_OFFSET_I64;
goto cilConv;
case CIL_CONV_U8:
case CIL_CONV_OVF_U8: // Fix this later - will never overflow
case CIL_CONV_OVF_U8_UN: // Fix this later - will never overflow
toType = TYPE_SYSTEM_UINT64;
convOpOffset = JIT_CONV_OFFSET_U64;
goto cilConv;
case CIL_CONV_R4:
toType = TYPE_SYSTEM_SINGLE;
convOpOffset = JIT_CONV_OFFSET_R32;
goto cilConv;
case CIL_CONV_R8:
case CIL_CONV_R_UN:
toType = TYPE_SYSTEM_DOUBLE;
convOpOffset = JIT_CONV_OFFSET_R64;
goto cilConv;
cilConv:
pStackType = PopStackType();
{
U32 opCodeBase;
U32 useParam = 0, param;
// This is the types that the conversion is from.
switch (pStackType->stackType) {
case EVALSTACK_INT64:
opCodeBase = (pStackType == types[TYPE_SYSTEM_INT64])?JIT_CONV_FROM_I64:JIT_CONV_FROM_U64;
break;
case EVALSTACK_INT32:
case EVALSTACK_PTR: // Only on 32-bit
opCodeBase =
(pStackType == types[TYPE_SYSTEM_BYTE] ||
pStackType == types[TYPE_SYSTEM_UINT16] ||
pStackType == types[TYPE_SYSTEM_UINT32] ||
pStackType == types[TYPE_SYSTEM_UINTPTR])?JIT_CONV_FROM_U32:JIT_CONV_FROM_I32;
break;
case EVALSTACK_F64:
opCodeBase = JIT_CONV_FROM_R64;
break;
case EVALSTACK_F32:
opCodeBase = JIT_CONV_FROM_R32;
break;
default:
Crash("JITit() Conv cannot handle stack type %d", pStackType->stackType);
}
// This is the types that the conversion is to.
switch (convOpOffset) {
case JIT_CONV_OFFSET_I32:
useParam = 1;
param = 32 - toBitCount;
break;
case JIT_CONV_OFFSET_U32:
useParam = 1;
// Next line is really (1 << toBitCount) - 1
// But it's done like this to work when toBitCount == 32
param = (((1 << (toBitCount - 1)) - 1) << 1) + 1;
break;
case JIT_CONV_OFFSET_I64:
case JIT_CONV_OFFSET_U64:
case JIT_CONV_OFFSET_R32:
case JIT_CONV_OFFSET_R64:
break;
default:
Crash("JITit() Conv cannot handle convOpOffset %d", convOpOffset);
}
PushOp(opCodeBase + convOpOffset);
if (useParam) {
PushU32(param);
}
}
PushStackType(types[toType]);
break;
}
#ifdef OLD_CONV
case CIL_CONV_OVF_I1:
case CIL_CONV_OVF_I2:
case CIL_CONV_OVF_I4:
u32Value = TYPE_SYSTEM_INT32;
goto convOvf;
case CIL_CONV_OVF_I8:
u32Value = TYPE_SYSTEM_INT64;
goto convOvf;
case CIL_CONV_OVF_U1:
case CIL_CONV_OVF_U2:
case CIL_CONV_OVF_U4:
u32Value = TYPE_SYSTEM_UINT32;
goto convOvf;
case CIL_CONV_OVF_U8:
u32Value = TYPE_SYSTEM_UINT64;
convOvf:
pStackType = PopStackType();
PushOpParam(JIT_CONV_OVF_I1 + (op - CIL_CONV_OVF_I1), pStackType->stackType);
PushStackType(types[u32Value]);
break;
case CIL_CONV_I1:
case CIL_CONV_I2:
case CIL_CONV_I4:
u32Value = TYPE_SYSTEM_INT32;
goto conv1;
case CIL_CONV_I8:
u32Value = TYPE_SYSTEM_INT64;
goto conv1;
case CIL_CONV_R4:
u32Value = TYPE_SYSTEM_SINGLE;
goto conv1;
case CIL_CONV_R8:
u32Value = TYPE_SYSTEM_DOUBLE;
goto conv1;
case CIL_CONV_U4:
u32Value = TYPE_SYSTEM_UINT32;
goto conv1;
case CIL_CONV_U8:
u32Value = TYPE_SYSTEM_UINT64;
conv1:
pStackType = PopStackType();
PushOpParam(JIT_CONV_I1 + (op - CIL_CONV_I1), pStackType->stackType);
PushStackType(types[u32Value]);