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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*! \file LowerJump.cpp
\brief This file lowers the following bytecodes: IF_XXX, GOTO
*/
#include <math.h>
#include "libdex/DexOpcodes.h"
#include "libdex/DexFile.h"
#include "Lower.h"
#include "NcgAot.h"
#include "enc_wrapper.h"
#include "interp/InterpDefs.h"
#include "NcgHelper.h"
LabelMap* globalMap;
LabelMap* globalShortMap;//make sure for each bytecode, there is no duplicated label
LabelMap* globalWorklist = NULL;
LabelMap* globalShortWorklist;
int globalMapNum;
int globalWorklistNum;
int globalDataWorklistNum;
int VMAPIWorklistNum;
int globalPCWorklistNum;
int chainingWorklistNum;
LabelMap* globalDataWorklist = NULL;
LabelMap* globalPCWorklist = NULL;
LabelMap* chainingWorklist = NULL;
LabelMap* VMAPIWorklist = NULL;
char* ncgClassData;
char* ncgClassDataPtr;
char* ncgMethodData;
char* ncgMethodDataPtr;
int ncgClassNum;
int ncgMethodNum;
NCGWorklist* globalNCGWorklist;
DataWorklist* methodDataWorklist;
#ifdef ENABLE_TRACING
MapWorklist* methodMapWorklist;
#endif
/*!
\brief search globalShortMap to find the entry for the given label
*/
LabelMap* findItemForShortLabel(const char* label) {
LabelMap* ptr = globalShortMap;
while(ptr != NULL) {
if(!strcmp(label, ptr->label)) {
return ptr;
}
ptr = ptr->nextItem;
}
return NULL;
}
//assume size of "jump reg" is 2
#define JUMP_REG_SIZE 2
#define ADD_REG_REG_SIZE 3
/*!
\brief update value of the immediate in the given jump instruction
check whether the immediate is out of range for the pre-set size
*/
int updateJumpInst(char* jumpInst, OpndSize immSize, int relativeNCG) {
#ifdef DEBUG_NCG_JUMP
ALOGI("update jump inst @ %p with %d", jumpInst, relativeNCG);
#endif
if(immSize == OpndSize_8) { //-128 to 127
if(relativeNCG >= 128 || relativeNCG < -128) {
ALOGE("pre-allocated space for a forward jump is not big enough");
dvmAbort();
}
}
if(immSize == OpndSize_16) { //-2^16 to 2^16-1
if(relativeNCG >= 32768 || relativeNCG < -32768) {
ALOGE("pre-allocated space for a forward jump is not big enough");
dvmAbort();
}
}
encoder_update_imm(relativeNCG, jumpInst);
return 0;
}
/*!
\brief insert a label
It takes argument checkDup, if checkDup is true, an entry is created in globalShortMap, entries in globalShortWorklist are checked, if there exists a match, the immediate in the jump instruction is updated and the entry is removed from globalShortWorklist;
otherwise, an entry is created in globalMap.
*/
int insertLabel(const char* label, bool checkDup) {
LabelMap* item = NULL;
if(!checkDup) {
item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
snprintf(item->label, LABEL_SIZE, "%s", label);
item->codePtr = stream;
item->nextItem = globalMap;
globalMap = item;
#ifdef DEBUG_NCG_CODE_SIZE
ALOGI("insert global label %s %p", label, stream);
#endif
globalMapNum++;
return 0;
}
item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
snprintf(item->label, LABEL_SIZE, "%s", label);
item->codePtr = stream;
item->nextItem = globalShortMap;
globalShortMap = item;
#ifdef DEBUG_NCG
ALOGI("insert short-term label %s %p", label, stream);
#endif
LabelMap* ptr = globalShortWorklist;
LabelMap* ptr_prevItem = NULL;
while(ptr != NULL) {
if(!strcmp(ptr->label, label)) {
//perform work
int relativeNCG = stream - ptr->codePtr;
unsigned instSize = encoder_get_inst_size(ptr->codePtr);
relativeNCG -= instSize; //size of the instruction
#ifdef DEBUG_NCG
ALOGI("perform work short-term %p for label %s relative %d", ptr->codePtr, label, relativeNCG);
#endif
updateJumpInst(ptr->codePtr, ptr->size, relativeNCG);
//remove work
if(ptr_prevItem == NULL) {
globalShortWorklist = ptr->nextItem;
free(ptr);
ptr = globalShortWorklist; //ptr_prevItem is still NULL
}
else {
ptr_prevItem->nextItem = ptr->nextItem;
free(ptr);
ptr = ptr_prevItem->nextItem;
}
}
else {
ptr_prevItem = ptr;
ptr = ptr->nextItem;
}
} //while
return 0;
}
/*!
\brief search globalMap to find the entry for the given label
*/
char* findCodeForLabel(const char* label) {
LabelMap* ptr = globalMap;
while(ptr != NULL) {
if(!strcmp(label, ptr->label)) {
return ptr->codePtr;
}
ptr = ptr->nextItem;
}
return NULL;
}
/*!
\brief search globalShortMap to find the entry for the given label
*/
char* findCodeForShortLabel(const char* label) {
LabelMap* ptr = globalShortMap;
while(ptr != NULL) {
if(!strcmp(label, ptr->label)) {
return ptr->codePtr;
}
ptr = ptr->nextItem;
}
return NULL;
}
int insertLabelWorklist(const char* label, OpndSize immSize) {
LabelMap* item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
snprintf(item->label, LABEL_SIZE, "%s", label);
item->codePtr = stream;
item->size = immSize;
item->nextItem = globalWorklist;
globalWorklist = item;
#ifdef DEBUG_NCG
ALOGI("insert globalWorklist: %s %p", label, stream);
#endif
return 0;
}
int insertShortWorklist(const char* label, OpndSize immSize) {
LabelMap* item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
snprintf(item->label, LABEL_SIZE, "%s", label);
item->codePtr = stream;
item->size = immSize;
item->nextItem = globalShortWorklist;
globalShortWorklist = item;
#ifdef DEBUG_NCG
ALOGI("insert globalShortWorklist: %s %p", label, stream);
#endif
return 0;
}
/*!
\brief free memory allocated for globalMap
*/
void freeLabelMap() {
LabelMap* ptr = globalMap;
while(ptr != NULL) {
globalMap = ptr->nextItem;
free(ptr);
ptr = globalMap;
}
}
/*!
\brief free memory allocated for globalShortMap
*/
void freeShortMap() {
LabelMap* ptr = globalShortMap;
while(ptr != NULL) {
globalShortMap = ptr->nextItem;
free(ptr);
ptr = globalShortMap;
}
globalShortMap = NULL;
}
int insertGlobalPCWorklist(char * offset, char * codeStart)
{
LabelMap* item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
snprintf(item->label, LABEL_SIZE, "%s", "export_pc");
item->size = OpndSize_32;
item->codePtr = offset; //points to the immediate operand
item->addend = codeStart - streamMethodStart; //relative code pointer
item->nextItem = globalPCWorklist;
globalPCWorklist = item;
globalPCWorklistNum ++;
#ifdef DEBUG_NCG
ALOGI("insert globalPCWorklist: %p %p %p %x %p", globalDvmNcg->streamCode, codeStart, streamCode, item->addend, item->codePtr);
#endif
return 0;
}
int insertChainingWorklist(int bbId, char * codeStart)
{
LabelMap* item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
item->size = OpndSize_32;
item->codePtr = codeStart; //points to the move instruction
item->addend = bbId; //relative code pointer
item->nextItem = chainingWorklist;
chainingWorklist = item;
#ifdef DEBUG_NCG
ALOGI("insertChainingWorklist: %p basic block %d", codeStart, bbId);
#endif
return 0;
}
int insertGlobalDataWorklist(char * offset, const char* label)
{
LabelMap* item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
snprintf(item->label, LABEL_SIZE, "%s", label);
item->codePtr = offset;
item->size = OpndSize_32;
item->nextItem = globalDataWorklist;
globalDataWorklist = item;
globalDataWorklistNum ++;
#ifdef DEBUG_NCG
ALOGI("insert globalDataWorklist: %s %p", label, offset);
#endif
return 0;
}
int insertVMAPIWorklist(char * offset, const char* label)
{
LabelMap* item = (LabelMap*)malloc(sizeof(LabelMap));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
snprintf(item->label, LABEL_SIZE, "%s", label);
item->codePtr = offset;
item->size = OpndSize_32;
item->nextItem = VMAPIWorklist;
VMAPIWorklist = item;
VMAPIWorklistNum ++;
#ifdef DEBUG_NCG
ALOGI("insert VMAPIWorklist: %s %p", label, offset);
#endif
return 0;
}
////////////////////////////////////////////////
int updateImmRMInst(char* moveInst, const char* label, int relativeNCG); //forward declaration
//////////////////// performLabelWorklist is defined differently for code cache
void performChainingWorklist() {
LabelMap* ptr = chainingWorklist;
while(ptr != NULL) {
int tmpNCG = traceLabelList[ptr->addend].lop.generic.offset;
char* NCGaddr = streamMethodStart + tmpNCG;
updateImmRMInst(ptr->codePtr, "", (int)NCGaddr);
chainingWorklist = ptr->nextItem;
free(ptr);
ptr = chainingWorklist;
}
}
void freeChainingWorklist() {
LabelMap* ptr = chainingWorklist;
while(ptr != NULL) {
chainingWorklist = ptr->nextItem;
free(ptr);
ptr = chainingWorklist;
}
}
//Work only for initNCG
void performLabelWorklist() {
LabelMap* ptr = globalWorklist;
while(ptr != NULL) {
#ifdef DEBUG_NCG
ALOGI("perform work global %p for label %s", ptr->codePtr, ptr->label);
#endif
char* targetCode = findCodeForLabel(ptr->label);
assert(targetCode != NULL);
int relativeNCG = targetCode - ptr->codePtr;
unsigned instSize = encoder_get_inst_size(ptr->codePtr);
relativeNCG -= instSize; //size of the instruction
updateJumpInst(ptr->codePtr, ptr->size, relativeNCG);
globalWorklist = ptr->nextItem;
free(ptr);
ptr = globalWorklist;
}
}
void freeLabelWorklist() {
LabelMap* ptr = globalWorklist;
while(ptr != NULL) {
globalWorklist = ptr->nextItem;
free(ptr);
ptr = globalWorklist;
}
}
///////////////////////////////////////////////////
/*!
\brief update value of the immediate in the given move instruction
*/
int updateImmRMInst(char* moveInst, const char* label, int relativeNCG) {
#ifdef DEBUG_NCG
ALOGI("perform work ImmRM inst @ %p for label %s with %d", moveInst, label, relativeNCG);
#endif
encoder_update_imm_rm(relativeNCG, moveInst);
return 0;
}
//! maximum instruction size for jump,jcc,call: 6 for jcc rel32
#define MAX_JCC_SIZE 6
//! minimum instruction size for jump,jcc,call: 2
#define MIN_JCC_SIZE 2
/*!
\brief estimate size of the immediate
Somehow, 16 bit jump does not work. This function will return either 8 bit or 32 bit
EXAMPLE:
native code at A: ...
native code at B: jump relOffset (target is A)
native code at B':
--> relOffset = A - B' = A - B - size of the jump instruction
Argument "target" is equal to A - B. To determine size of the immediate, we check tha value of "target - size of the jump instructoin"
*/
OpndSize estOpndSizeFromImm(int target) {
if(target-MIN_JCC_SIZE < 128 && target-MAX_JCC_SIZE >= -128) return OpndSize_8;
#ifdef SUPPORT_IMM_16
if(target-MIN_JCC_SIZE < 32768 && target-MAX_JCC_SIZE >= -32768) return OpndSize_16;
#endif
return OpndSize_32;
}
/*!
\brief return size of a jump or call instruction
*/
unsigned getJmpCallInstSize(OpndSize size, JmpCall_type type) {
if(type == JmpCall_uncond) {
if(size == OpndSize_8) return 2;
if(size == OpndSize_16) return 4;
return 5;
}
if(type == JmpCall_cond) {
if(size == OpndSize_8) return 2;
if(size == OpndSize_16) return 5;
return 6;
}
if(type == JmpCall_reg) {
assert(size == OpndSize_32);
return JUMP_REG_SIZE;
}
if(type == JmpCall_call) {
assert(size != OpndSize_8);
if(size == OpndSize_16) return 4;
return 5;
}
return 0;
}
/*!
\brief check whether a branch target is already handled, if yes, return the size of the immediate; otherwise, call insertShortWorklist or insertLabelWorklist.
If the branch target is not handled, call insertShortWorklist or insertLabelWorklist depending on isShortTerm, unknown is set to true, immSize is set to 32 if isShortTerm is false, set to 32 if isShortTerm is true and target is check_cast_null, set to 8 otherwise.
If the branch target is handled, call estOpndSizeFromImm to set immSize for jump instruction, returns the value of the immediate
*/
int getRelativeOffset(const char* target, bool isShortTerm, JmpCall_type type, bool* unknown, OpndSize* immSize) {
char* targetPtrInStream = NULL;
if(isShortTerm) targetPtrInStream = findCodeForShortLabel(target);
else targetPtrInStream = findCodeForLabel(target);
int relOffset;
*unknown = false;
if(targetPtrInStream == NULL) {
//branch target is not handled yet
relOffset = 0;
*unknown = true;
if(isShortTerm) {
/* for backward jump, at this point, we don't know how far the target is from this jump
since the lable is only used within a single bytecode, we assume OpndSize_8 is big enough
but there are special cases where we should use 32 bit offset
*/
if(!strcmp(target, ".check_cast_null") || !strcmp(target, ".stackOverflow") ||
!strcmp(target, ".invokeChain") ||
!strcmp(target, ".new_instance_done") ||
!strcmp(target, ".new_array_done") ||
!strcmp(target, ".fill_array_data_done") ||
!strcmp(target, ".inlined_string_compare_done") ||
!strncmp(target, "after_exception", 15)) {
#ifdef SUPPORT_IMM_16
*immSize = OpndSize_16;
#else
*immSize = OpndSize_32;
#endif
} else {
*immSize = OpndSize_8;
}
#ifdef DEBUG_NCG_JUMP
ALOGI("insert to short worklist %s %d", target, *immSize);
#endif
insertShortWorklist(target, *immSize);
}
else {
#ifdef SUPPORT_IMM_16
*immSize = OpndSize_16;
#else
*immSize = OpndSize_32;
#endif
insertLabelWorklist(target, *immSize);
}
if(type == JmpCall_call) { //call sz16 does not work in gdb
*immSize = OpndSize_32;
}
return 0;
}
else if (!isShortTerm) {
#ifdef SUPPORT_IMM_16
*immSize = OpndSize_16;
#else
*immSize = OpndSize_32;
#endif
insertLabelWorklist(target, *immSize);
}
#ifdef DEBUG_NCG
ALOGI("backward branch @ %p for label %s", stream, target);
#endif
relOffset = targetPtrInStream - stream;
if(type == JmpCall_call) *immSize = OpndSize_32;
else
*immSize = estOpndSizeFromImm(relOffset);
relOffset -= getJmpCallInstSize(*immSize, type);
return relOffset;
}
/*!
\brief generate a single native instruction "jcc imm" to jump to a label
*/
void conditional_jump(ConditionCode cc, const char* target, bool isShortTerm) {
if(jumpToException(target) && currentExceptionBlockIdx >= 0) { //jump to the exceptionThrow block
condJumpToBasicBlock(stream, cc, currentExceptionBlockIdx);
return;
}
Mnemonic m = (Mnemonic)(Mnemonic_Jcc + cc);
bool unknown;
OpndSize size;
int imm = 0;
imm = getRelativeOffset(target, isShortTerm, JmpCall_cond, &unknown, &size);
dump_label(m, size, imm, target, isShortTerm);
}
/*!
\brief generate a single native instruction "jmp imm" to jump to ".invokeArgsDone"
*/
void goto_invokeArgsDone() {
unconditional_jump_global_API(".invokeArgsDone", false);
}
/*!
\brief generate a single native instruction "jmp imm" to jump to a label
If the target is ".invokeArgsDone" and mode is NCG O1, extra work is performed to dump content of virtual registers to memory.
*/
void unconditional_jump(const char* target, bool isShortTerm) {
if(jumpToException(target) && currentExceptionBlockIdx >= 0) { //jump to the exceptionThrow block
jumpToBasicBlock(stream, currentExceptionBlockIdx);
return;
}
Mnemonic m = Mnemonic_JMP;
bool unknown;
OpndSize size;
if(gDvm.executionMode == kExecutionModeNcgO1) {
//for other three labels used by JIT: invokeArgsDone_formal, _native, _jit
if(!strncmp(target, ".invokeArgsDone", 15)) {
touchEcx(); //keep ecx live, if ecx was spilled, it is loaded here
beforeCall(target); //
}
if(!strcmp(target, ".invokeArgsDone")) {
nextVersionOfHardReg(PhysicalReg_EDX, 1); //edx will be used in a function
call("ncgGetEIP"); //must be immediately before JMP
}
}
int imm = 0;
imm = getRelativeOffset(target, isShortTerm, JmpCall_uncond, &unknown, &size);
dump_label(m, size, imm, target, isShortTerm);
if(gDvm.executionMode == kExecutionModeNcgO1) {
if(!strncmp(target, ".invokeArgsDone", 15)) {
afterCall(target); //un-spill before executing the next bytecode
}
}
}
/*!
\brief generate a single native instruction "jcc imm"
*/
void conditional_jump_int(ConditionCode cc, int target, OpndSize size) {
Mnemonic m = (Mnemonic)(Mnemonic_Jcc + cc);
dump_ncg(m, size, target);
}
/*!
\brief generate a single native instruction "jmp imm"
*/
void unconditional_jump_int(int target, OpndSize size) {
Mnemonic m = Mnemonic_JMP;
dump_ncg(m, size, target);
}
/*!
\brief generate a single native instruction "jmp reg"
*/
void unconditional_jump_reg(int reg, bool isPhysical) {
dump_reg(Mnemonic_JMP, ATOM_NORMAL, OpndSize_32, reg, isPhysical, LowOpndRegType_gp);
}
/*!
\brief generate a single native instruction to call a function
If mode is NCG O1, extra work is performed to dump content of virtual registers to memory.
*/
void call(const char* target) {
if(gDvm.executionMode == kExecutionModeNcgO1) {
beforeCall(target);
}
Mnemonic m = Mnemonic_CALL;
bool dummy;
OpndSize size;
int relOffset = 0;
relOffset = getRelativeOffset(target, false, JmpCall_call, &dummy, &size);
dump_label(m, size, relOffset, target, false);
if(gDvm.executionMode == kExecutionModeNcgO1) {
afterCall(target);
}
}
/*!
\brief generate a single native instruction to call a function
*/
void call_reg(int reg, bool isPhysical) {
Mnemonic m = Mnemonic_CALL;
dump_reg(m, ATOM_NORMAL, OpndSize_32, reg, isPhysical, LowOpndRegType_gp);
}
void call_reg_noalloc(int reg, bool isPhysical) {
Mnemonic m = Mnemonic_CALL;
dump_reg_noalloc(m, OpndSize_32, reg, isPhysical, LowOpndRegType_gp);
}
/*!
\brief generate a single native instruction to call a function
*/
void call_mem(int disp, int reg, bool isPhysical) {
Mnemonic m = Mnemonic_CALL;
dump_mem(m, ATOM_NORMAL, OpndSize_32, disp, reg, isPhysical);
}
/*!
\brief insert an entry to globalNCGWorklist
*/
int insertNCGWorklist(s4 relativePC, OpndSize immSize) {
int offsetNCG2 = stream - streamMethodStart;
#ifdef DEBUG_NCG
ALOGI("insert NCGWorklist (goto forward) @ %p offsetPC %x relativePC %x offsetNCG %x", stream, offsetPC, relativePC, offsetNCG2);
#endif
NCGWorklist* item = (NCGWorklist*)malloc(sizeof(NCGWorklist));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
item->relativePC = relativePC;
item->offsetPC = offsetPC;
item->offsetNCG = offsetNCG2;
item->codePtr = stream;
item->size = immSize;
item->nextItem = globalNCGWorklist;
globalNCGWorklist = item;
return 0;
}
#ifdef ENABLE_TRACING
int insertMapWorklist(s4 BCOffset, s4 NCGOffset, int isStartOfPC) {
return 0;
}
#endif
/*!
\brief insert an entry to methodDataWorklist
This function is used by bytecode FILL_ARRAY_DATA, PACKED_SWITCH, SPARSE_SWITCH
*/
int insertDataWorklist(s4 relativePC, char* codePtr1) {
//insert according to offsetPC+relativePC, smallest at the head
DataWorklist* item = (DataWorklist*)malloc(sizeof(DataWorklist));
if(item == NULL) {
ALOGE("Memory allocation failed");
return -1;
}
item->relativePC = relativePC;
item->offsetPC = offsetPC;
item->codePtr = codePtr1;
item->codePtr2 = stream; //jump_reg for switch
DataWorklist* ptr = methodDataWorklist;
DataWorklist* prev_ptr = NULL;
while(ptr != NULL) {
int tmpPC = ptr->offsetPC + ptr->relativePC;
int tmpPC2 = relativePC + offsetPC;
if(tmpPC2 < tmpPC) {
break;
}
prev_ptr = ptr;
ptr = ptr->nextItem;
}
//insert item before ptr
if(prev_ptr != NULL) {
prev_ptr->nextItem = item;
}
else methodDataWorklist = item;
item->nextItem = ptr;
return 0;
}
/*!
\brief work on globalNCGWorklist
*/
int performNCGWorklist() {
NCGWorklist* ptr = globalNCGWorklist;
while(ptr != NULL) {
ALOGV("perform NCG worklist: @ %p target block %d target NCG %x",
ptr->codePtr, ptr->relativePC, traceLabelList[ptr->relativePC].lop.generic.offset);
int tmpNCG = traceLabelList[ptr->relativePC].lop.generic.offset;
assert(tmpNCG >= 0);
int relativeNCG = tmpNCG - ptr->offsetNCG;
unsigned instSize = encoder_get_inst_size(ptr->codePtr);
relativeNCG -= instSize;
updateJumpInst(ptr->codePtr, ptr->size, relativeNCG);
globalNCGWorklist = ptr->nextItem;
free(ptr);
ptr = globalNCGWorklist;
}
return 0;
}
void freeNCGWorklist() {
NCGWorklist* ptr = globalNCGWorklist;
while(ptr != NULL) {
globalNCGWorklist = ptr->nextItem;
free(ptr);
ptr = globalNCGWorklist;
}
}
/*!
\brief used by bytecode SWITCH
targetPC points to start of the data section
Code sequence for SWITCH
call ncgGetEIP
@codeInst: add_reg_reg %eax, %edx
jump_reg %edx
This function returns the offset in native code between add_reg_reg and the data section
*/
int getRelativeNCGForSwitch(int targetPC, char* codeInst) {
int tmpNCG = mapFromBCtoNCG[targetPC];
int offsetNCG2 = codeInst - streamMethodStart;
int relativeOff = tmpNCG - offsetNCG2;
return relativeOff;
}
/*!
\brief work on methodDataWorklist
*/
int performDataWorklist() {
DataWorklist* ptr = methodDataWorklist;
if(ptr == NULL) return 0;
char* codeCacheEnd = ((char *) gDvmJit.codeCache) + gDvmJit.codeCacheSize - CODE_CACHE_PADDING;
u2 insnsSize = dvmGetMethodInsnsSize(currentMethod); //bytecode
//align stream to multiple of 4
int alignBytes = (int)stream & 3;
if(alignBytes != 0) alignBytes = 4-alignBytes;
stream += alignBytes;
while(ptr != NULL) {
int tmpPC = ptr->offsetPC + ptr->relativePC;
int endPC = insnsSize;
if(ptr->nextItem != NULL) endPC = ptr->nextItem->offsetPC + ptr->nextItem->relativePC;
mapFromBCtoNCG[tmpPC] = stream - streamMethodStart; //offsetNCG in byte
//handle fill_array_data, packed switch & sparse switch
u2 tmpInst = *(currentMethod->insns + ptr->offsetPC);
u2* sizePtr;
s4* entryPtr_bytecode;
u2 tSize, iVer;
u4 sz;
if (gDvmJit.codeCacheFull == true) {
// We are out of code cache space. Skip writing data/code to
// code cache. Simply free the item.
methodDataWorklist = ptr->nextItem;
free(ptr);
ptr = methodDataWorklist;
}
switch (INST_INST(tmpInst)) {
case OP_FILL_ARRAY_DATA:
sz = (endPC-tmpPC)*sizeof(u2);
if ((stream + sz) < codeCacheEnd) {
memcpy(stream, (u2*)currentMethod->insns+tmpPC, sz);
#ifdef DEBUG_NCG_CODE_SIZE
ALOGI("copy data section to stream %p: start at %d, %d bytes", stream, tmpPC, sz);
#endif
#ifdef DEBUG_NCG
ALOGI("update data section at %p with %d", ptr->codePtr, stream-ptr->codePtr);
#endif
updateImmRMInst(ptr->codePtr, "", stream - ptr->codePtr);
stream += sz;
} else {
gDvmJit.codeCacheFull = true;
}
break;
case OP_PACKED_SWITCH:
updateImmRMInst(ptr->codePtr, "", stream-ptr->codePtr);
sizePtr = (u2*)currentMethod->insns+tmpPC + 1 /*signature*/;
entryPtr_bytecode = (s4*)(sizePtr + 1 /*size*/ + 2 /*firstKey*/);
tSize = *(sizePtr);
sz = tSize * 4; /* expected size needed in stream */
if ((stream + sz) < codeCacheEnd) {
for(iVer = 0; iVer < tSize; iVer++) {
//update entries
s4 relativePC = *entryPtr_bytecode; //relative to ptr->offsetPC
//need stream, offsetPC,
int relativeNCG = getRelativeNCGForSwitch(relativePC+ptr->offsetPC, ptr->codePtr2);
#ifdef DEBUG_NCG_CODE_SIZE
ALOGI("convert target from %d to %d", relativePC+ptr->offsetPC, relativeNCG);
#endif
*((s4*)stream) = relativeNCG;
stream += 4;
entryPtr_bytecode++;
}
} else {
gDvmJit.codeCacheFull = true;
}
break;
case OP_SPARSE_SWITCH:
updateImmRMInst(ptr->codePtr, "", stream-ptr->codePtr);
sizePtr = (u2*)currentMethod->insns+tmpPC + 1 /*signature*/;
s4* keyPtr_bytecode = (s4*)(sizePtr + 1 /*size*/);
tSize = *(sizePtr);
entryPtr_bytecode = (s4*)(keyPtr_bytecode + tSize);
sz = tSize * (sizeof(s4) + 4); /* expected size needed in stream */
if ((stream + sz) < codeCacheEnd) {
memcpy(stream, keyPtr_bytecode, tSize*sizeof(s4));
stream += tSize*sizeof(s4);
for(iVer = 0; iVer < tSize; iVer++) {
//update entries
s4 relativePC = *entryPtr_bytecode; //relative to ptr->offsetPC
//need stream, offsetPC,
int relativeNCG = getRelativeNCGForSwitch(relativePC+ptr->offsetPC, ptr->codePtr2);
*((s4*)stream) = relativeNCG;
stream += 4;
entryPtr_bytecode++;
}
} else {
gDvmJit.codeCacheFull = true;
}
break;
}
//remove the item
methodDataWorklist = ptr->nextItem;
free(ptr);
ptr = methodDataWorklist;
}
return 0;
}
void freeDataWorklist() {
DataWorklist* ptr = methodDataWorklist;
while(ptr != NULL) {
methodDataWorklist = ptr->nextItem;
free(ptr);
ptr = methodDataWorklist;
}
}
//////////////////////////
/*!
\brief check whether a branch target (specified by relative offset in bytecode) is already handled, if yes, return the size of the immediate; otherwise, call insertNCGWorklist.
If the branch target is not handled, call insertNCGWorklist, unknown is set to true, immSize is set to 32.
If the branch target is handled, call estOpndSizeFromImm to set immSize for jump instruction, returns the value of the immediate
*/
int getRelativeNCG(s4 tmp, JmpCall_type type, bool* unknown, OpndSize* size) {//tmp: relativePC
int tmpNCG = traceLabelList[tmp].lop.generic.offset;
*unknown = false;
if(tmpNCG <0) {
*unknown = true;
#ifdef SUPPORT_IMM_16
*size = OpndSize_16;
#else
*size = OpndSize_32;
#endif
insertNCGWorklist(tmp, *size);
return 0;
}
int offsetNCG2 = stream - streamMethodStart;
#ifdef DEBUG_NCG
ALOGI("goto backward @ %p offsetPC %d relativePC %d offsetNCG %d relativeNCG %d", stream, offsetPC, tmp, offsetNCG2, tmpNCG-offsetNCG2);
#endif
int relativeOff = tmpNCG - offsetNCG2;
*size = estOpndSizeFromImm(relativeOff);
return relativeOff - getJmpCallInstSize(*size, type);
}
/*!
\brief a helper function to handle backward branch
input: jump target in %eax; at end of the function, jump to %eax
*/
int common_backwardBranch() {
insertLabel("common_backwardBranch", false);
spill_reg(PhysicalReg_EAX, true);
call("common_periodicChecks_entry");
unspill_reg(PhysicalReg_EAX, true);
unconditional_jump_reg(PhysicalReg_EAX, true);
return 0;
}
//when this is called from JIT, there is no need to check GC
int common_goto(s4 tmp) { //tmp: target basic block id
bool unknown;
OpndSize size;
constVREndOfBB();
globalVREndOfBB(currentMethod);
int relativeNCG = tmp;
relativeNCG = getRelativeNCG(tmp, JmpCall_uncond, &unknown, &size);
unconditional_jump_int(relativeNCG, size);
return 1;
}
int common_if(s4 tmp, ConditionCode cc_next, ConditionCode cc) {
bool unknown;
OpndSize size;
int relativeNCG = traceCurrentBB->taken ? traceCurrentBB->taken->id : 0;
if(traceCurrentBB->taken)
relativeNCG = getRelativeNCG(traceCurrentBB->taken->id, JmpCall_cond, &unknown, &size);
conditional_jump_int(cc, relativeNCG, size);
relativeNCG = traceCurrentBB->fallThrough ? traceCurrentBB->fallThrough->id : 0;
if(traceCurrentBB->fallThrough)
relativeNCG = getRelativeNCG(traceCurrentBB->fallThrough->id, JmpCall_uncond, &unknown, &size);
unconditional_jump_int(relativeNCG, size);
return 2;
}
/*!
\brief helper function to handle null object error
*/
int common_errNullObject() {
insertLabel("common_errNullObject", false);
move_imm_to_reg(OpndSize_32, 0, PhysicalReg_EAX, true);
move_imm_to_reg(OpndSize_32, LstrNullPointerException, PhysicalReg_ECX, true);
unconditional_jump("common_throw", false);
return 0;
}
/*!
\brief helper function to handle string index error
*/
int common_StringIndexOutOfBounds() {
insertLabel("common_StringIndexOutOfBounds", false);
move_imm_to_reg(OpndSize_32, 0, PhysicalReg_EAX, true);
move_imm_to_reg(OpndSize_32, LstrStringIndexOutOfBoundsException, PhysicalReg_ECX, true);
unconditional_jump("common_throw", false);
return 0;
}
/*!
\brief helper function to handle array index error
*/
int common_errArrayIndex() {
insertLabel("common_errArrayIndex", false);
move_imm_to_reg(OpndSize_32, 0, PhysicalReg_EAX, true);
move_imm_to_reg(OpndSize_32, LstrArrayIndexException, PhysicalReg_ECX, true);
unconditional_jump("common_throw", false);
return 0;
}
/*!
\brief helper function to handle array store error
*/
int common_errArrayStore() {
insertLabel("common_errArrayStore", false);
move_imm_to_reg(OpndSize_32, 0, PhysicalReg_EAX, true);
move_imm_to_reg(OpndSize_32, LstrArrayStoreException, PhysicalReg_ECX, true);
unconditional_jump("common_throw", false);
return 0;
}
/*!
\brief helper function to handle negative array size error
*/
int common_errNegArraySize() {
insertLabel("common_errNegArraySize", false);
move_imm_to_reg(OpndSize_32, 0, PhysicalReg_EAX, true);
move_imm_to_reg(OpndSize_32, LstrNegativeArraySizeException, PhysicalReg_ECX, true);
unconditional_jump("common_throw", false);
return 0;
}
/*!
\brief helper function to handle divide-by-zero error
*/
int common_errDivideByZero() {
insertLabel("common_errDivideByZero", false);
move_imm_to_reg(OpndSize_32, LstrDivideByZero, PhysicalReg_EAX, true);
move_imm_to_reg(OpndSize_32, LstrArithmeticException, PhysicalReg_ECX, true);
unconditional_jump("common_throw", false);
return 0;
}
/*!
\brief helper function to handle no such method error
*/
int common_errNoSuchMethod() {
insertLabel("common_errNoSuchMethod", false);
move_imm_to_reg(OpndSize_32, 0, PhysicalReg_EAX, true);
move_imm_to_reg(OpndSize_32, LstrNoSuchMethodError, PhysicalReg_ECX, true);
unconditional_jump("common_throw", false);
return 0;
}
int call_dvmFindCatchBlock();
#define P_GPR_1 PhysicalReg_ESI //self callee-saved
#define P_GPR_2 PhysicalReg_EBX //exception callee-saved
#define P_GPR_3 PhysicalReg_EAX //method that caused exception
/*!
\brief helper function common_exceptionThrown
*/
int common_exceptionThrown() {
insertLabel("common_exceptionThrown", false);
typedef void (*vmHelper)(int);
vmHelper funcPtr = dvmJitToExceptionThrown;
move_imm_to_reg(OpndSize_32, (int)funcPtr, C_SCRATCH_1, isScratchPhysical);
unconditional_jump_reg(C_SCRATCH_1, isScratchPhysical);
return 0;
}
#undef P_GPR_1
#undef P_GPR_2
#undef P_GPR_3
/*!
\brief helper function to throw an exception with message
INPUT: obj_reg(%eax), exceptionPtrReg(%ecx)
SCRATCH: C_SCRATCH_1(%esi) & C_SCRATCH_2(%edx)
OUTPUT: no
*/
int throw_exception_message(int exceptionPtrReg, int obj_reg, bool isPhysical,
int startLR/*logical register index*/, bool startPhysical) {
insertLabel("common_throw_message", false);
scratchRegs[0] = PhysicalReg_ESI; scratchRegs[1] = PhysicalReg_EDX;
scratchRegs[2] = PhysicalReg_Null; scratchRegs[3] = PhysicalReg_Null;
move_mem_to_reg(OpndSize_32, offObject_clazz, obj_reg, isPhysical, C_SCRATCH_1, isScratchPhysical);
move_mem_to_reg(OpndSize_32, offClassObject_descriptor, C_SCRATCH_1, isScratchPhysical, C_SCRATCH_2, isScratchPhysical);
load_effective_addr(-8, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
move_reg_to_mem(OpndSize_32, C_SCRATCH_2, isScratchPhysical, 4, PhysicalReg_ESP, true);
move_reg_to_mem(OpndSize_32, exceptionPtrReg, true, 0, PhysicalReg_ESP, true);
call_dvmThrowWithMessage();
load_effective_addr(8, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
unconditional_jump("common_exceptionThrown", false);
return 0;
}
/*!
\brief helper function to throw an exception
scratch: C_SCRATCH_1(%edx)
*/
int throw_exception(int exceptionPtrReg, int immReg,
int startLR/*logical register index*/, bool startPhysical) {
insertLabel("common_throw", false);
scratchRegs[0] = PhysicalReg_EDX; scratchRegs[1] = PhysicalReg_Null;
scratchRegs[2] = PhysicalReg_Null; scratchRegs[3] = PhysicalReg_Null;
load_effective_addr(-8, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
move_reg_to_mem(OpndSize_32, immReg, true, 4, PhysicalReg_ESP, true);
move_reg_to_mem(OpndSize_32, exceptionPtrReg, true, 0, PhysicalReg_ESP, true);
call_dvmThrow();
load_effective_addr(8, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
unconditional_jump("common_exceptionThrown", false);
return 0;
}
//! lower bytecode GOTO
//!
int op_goto() {
s2 tmp = traceCurrentBB->taken->id;
int retval = common_goto(tmp);
rPC += 1;
return retval;
}
//! lower bytecode GOTO_16
//!
int op_goto_16() {
s2 tmp = traceCurrentBB->taken->id;
int retval = common_goto(tmp);
rPC += 2;
return retval;
}
//! lower bytecode GOTO_32
//!
int op_goto_32() {
s2 tmp = traceCurrentBB->taken->id;
int retval = common_goto((s4)tmp);
rPC += 3;
return retval;
}
#define P_GPR_1 PhysicalReg_EBX
//! lower bytecode PACKED_SWITCH
//!
int op_packed_switch() {
u4 tmp = (u4)FETCH(1);
tmp |= (u4)FETCH(2) << 16;
u2 vA = INST_AA(inst);
#ifdef DEBUG_EACH_BYTECODE
u2 tSize = 0;
s4 firstKey = 0;
s4* entries = NULL;
#else
u2* switchData = rPC + (s4)tmp;
if (*switchData++ != kPackedSwitchSignature) {
/* should have been caught by verifier */
dvmThrowInternalError(
"bad packed switch magic");
return 0; //no_op
}
u2 tSize = *switchData++;
assert(tSize > 0);
s4 firstKey = *switchData++;
firstKey |= (*switchData++) << 16;
s4* entries = (s4*) switchData;
assert(((u4)entries & 0x3) == 0);
#endif
get_virtual_reg(vA, OpndSize_32, 1, false);
//dvmNcgHandlePackedSwitch: testVal, size, first_key, targets
load_effective_addr(-16, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
move_imm_to_mem(OpndSize_32, tSize, 8, PhysicalReg_ESP, true);
move_imm_to_mem(OpndSize_32, firstKey, 4, PhysicalReg_ESP, true);
/* "entries" is constant for JIT
it is the 1st argument to dvmJitHandlePackedSwitch */
move_imm_to_mem(OpndSize_32, (int)entries, 0, PhysicalReg_ESP, true);
move_reg_to_mem(OpndSize_32, 1, false, 12, PhysicalReg_ESP, true);
//if value out of range, fall through (no_op)
//return targets[testVal - first_key]
scratchRegs[0] = PhysicalReg_SCRATCH_1;
call_dvmJitHandlePackedSwitch();
load_effective_addr(16, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
//TODO: eax should be absolute address, call globalVREndOfBB, constVREndOfBB
//conditional_jump_global_API(Condition_LE, "common_backwardBranch", false);
constVREndOfBB();
globalVREndOfBB(currentMethod); //update GG VRs
//get rPC, %eax has the relative PC offset
alu_binary_imm_reg(OpndSize_32, add_opc, (int)rPC, PhysicalReg_EAX, true);
scratchRegs[0] = PhysicalReg_SCRATCH_2;
#if defined(WITH_JIT_TUNING)
/* Fall back to interpreter after resolving address of switch target.
* Indicate a kSwitchOverflow. Note: This is not an "overflow". But it helps
* count the times we return from a Switch
*/
move_imm_to_mem(OpndSize_32, kSwitchOverflow, 0, PhysicalReg_ESP, true);
#endif
jumpToInterpNoChain();
rPC += 3;
return 0;
}
#undef P_GPR_1
#define P_GPR_1 PhysicalReg_EBX
//! lower bytecode SPARSE_SWITCH
//!
int op_sparse_switch() {
u4 tmp = (u4)FETCH(1);
tmp |= (u4)FETCH(2) << 16;
u2 vA = INST_AA(inst);
#ifdef DEBUG_EACH_BYTECODE
u2 tSize = 0;
const s4* keys = NULL;
s4* entries = NULL;
#else
u2* switchData = rPC + (s4)tmp;
if (*switchData++ != kSparseSwitchSignature) {
/* should have been caught by verifier */
dvmThrowInternalError(
"bad sparse switch magic");
return 0; //no_op
}
u2 tSize = *switchData++;
assert(tSize > 0);
const s4* keys = (const s4*) switchData;
assert(((u4)keys & 0x3) == 0);
s4* entries = (s4*)switchData + tSize;
assert(((u4)entries & 0x3) == 0);
#endif
get_virtual_reg(vA, OpndSize_32, 1, false);
//dvmNcgHandleSparseSwitch: keys, size, testVal
load_effective_addr(-12, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
move_imm_to_mem(OpndSize_32, tSize, 4, PhysicalReg_ESP, true);
/* "keys" is constant for JIT
it is the 1st argument to dvmJitHandleSparseSwitch */
move_imm_to_mem(OpndSize_32, (int)keys, 0, PhysicalReg_ESP, true);
move_reg_to_mem(OpndSize_32, 1, false, 8, PhysicalReg_ESP, true);
scratchRegs[0] = PhysicalReg_SCRATCH_1;
//if testVal is in keys, return the corresponding target
//otherwise, fall through (no_op)
call_dvmJitHandleSparseSwitch();
load_effective_addr(12, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
//TODO: eax should be absolute address, call globalVREndOfBB constVREndOfBB
//conditional_jump_global_API(Condition_LE, "common_backwardBranch", false);
constVREndOfBB();
globalVREndOfBB(currentMethod);
//get rPC, %eax has the relative PC offset
alu_binary_imm_reg(OpndSize_32, add_opc, (int)rPC, PhysicalReg_EAX, true);
scratchRegs[0] = PhysicalReg_SCRATCH_2;
#if defined(WITH_JIT_TUNING)
/* Fall back to interpreter after resolving address of switch target.
* Indicate a kSwitchOverflow. Note: This is not an "overflow". But it helps
* count the times we return from a Switch
*/
move_imm_to_mem(OpndSize_32, kSwitchOverflow, 0, PhysicalReg_ESP, true);
#endif
jumpToInterpNoChain();
rPC += 3;
return 0;
}
#undef P_GPR_1
#define P_GPR_1 PhysicalReg_EBX
//! lower bytecode IF_EQ
//!
int op_if_eq() {
u2 vA = INST_A(inst);
u2 vB = INST_B(inst);
s2 tmp = (s2)FETCH(1);
get_virtual_reg(vA, OpndSize_32, 1, false);
compare_VR_reg(OpndSize_32, vB, 1, false);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_NE, Condition_E);
rPC += 2;
return 0;
}
//! lower bytecode IF_NE
//!
int op_if_ne() {
u2 vA = INST_A(inst);
u2 vB = INST_B(inst);
s2 tmp = (s2)FETCH(1);
get_virtual_reg(vA, OpndSize_32, 1, false);
compare_VR_reg(OpndSize_32, vB, 1, false);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_E, Condition_NE);
rPC += 2;
return 0;
}
//! lower bytecode IF_LT
//!
int op_if_lt() {
u2 vA = INST_A(inst);
u2 vB = INST_B(inst);
s2 tmp = (s2)FETCH(1);
get_virtual_reg(vA, OpndSize_32, 1, false);
compare_VR_reg(OpndSize_32, vB, 1, false);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_GE, Condition_L);
rPC += 2;
return 0;
}
//! lower bytecode IF_GE
//!
int op_if_ge() {
u2 vA = INST_A(inst);
u2 vB = INST_B(inst);
s2 tmp = (s2)FETCH(1);
get_virtual_reg(vA, OpndSize_32, 1, false);
compare_VR_reg(OpndSize_32, vB, 1, false);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_L, Condition_GE);
rPC += 2;
return 0;
}
//! lower bytecode IF_GT
//!
int op_if_gt() {
u2 vA = INST_A(inst);
u2 vB = INST_B(inst);
s2 tmp = (s2)FETCH(1);
get_virtual_reg(vA, OpndSize_32, 1, false);
compare_VR_reg(OpndSize_32, vB, 1, false);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_LE, Condition_G);
rPC += 2;
return 0;
}
//! lower bytecode IF_LE
//!
int op_if_le() {
u2 vA = INST_A(inst);
u2 vB = INST_B(inst);
s2 tmp = (s2)FETCH(1);
get_virtual_reg(vA, OpndSize_32, 1, false);
compare_VR_reg(OpndSize_32, vB, 1, false);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_G, Condition_LE);
rPC += 2;
return 0;
}
#undef P_GPR_1
//! lower bytecode IF_EQZ
//!
int op_if_eqz() {
u2 vA = INST_AA(inst);
s2 tmp = (s2)FETCH(1);
compare_imm_VR(OpndSize_32,
0, vA);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_NE, Condition_E);
rPC += 2;
return 0;
}
//! lower bytecode IF_NEZ
//!
int op_if_nez() {
u2 vA = INST_AA(inst);
s2 tmp = (s2)FETCH(1);
compare_imm_VR(OpndSize_32,
0, vA);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_E, Condition_NE);
rPC += 2;
return 0;
}
//! lower bytecode IF_LTZ
//!
int op_if_ltz() {
u2 vA = INST_AA(inst);
s2 tmp = (s2)FETCH(1);
compare_imm_VR(OpndSize_32,
0, vA);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_GE, Condition_L);
rPC += 2;
return 0;
}
//! lower bytecode IF_GEZ
//!
int op_if_gez() {
u2 vA = INST_AA(inst);
s2 tmp = (s2)FETCH(1);
compare_imm_VR(OpndSize_32,
0, vA);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_L, Condition_GE);
rPC += 2;
return 0;
}
//! lower bytecode IF_GTZ
//!
int op_if_gtz() {
u2 vA = INST_AA(inst);
s2 tmp = (s2)FETCH(1);
compare_imm_VR(OpndSize_32,
0, vA);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_LE, Condition_G);
rPC += 2;
return 0;
}
//! lower bytecode IF_LEZ
//!
int op_if_lez() {
u2 vA = INST_AA(inst);
s2 tmp = (s2)FETCH(1);
compare_imm_VR(OpndSize_32,
0, vA);
constVREndOfBB();
globalVREndOfBB(currentMethod);
common_if(tmp, Condition_G, Condition_LE);
rPC += 2;
return 0;
}
#define P_GPR_1 PhysicalReg_ECX
#define P_GPR_2 PhysicalReg_EBX
/*!
\brief helper function common_periodicChecks4 to check GC request
BCOffset in %edx
*/
int common_periodicChecks4() {
insertLabel("common_periodicChecks4", false);
#if (!defined(ENABLE_TRACING))
get_self_pointer(PhysicalReg_ECX, true);
move_mem_to_reg(OpndSize_32, offsetof(Thread, suspendCount), PhysicalReg_ECX, true, PhysicalReg_EAX, true);
compare_imm_reg(OpndSize_32, 0, PhysicalReg_EAX, true); //suspendCount
conditional_jump(Condition_NE, "common_handleSuspend4", true); //called once
x86_return();
insertLabel("common_handleSuspend4", true);
push_reg_to_stack(OpndSize_32, PhysicalReg_ECX, true);
call_dvmCheckSuspendPending();
load_effective_addr(4, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
x86_return();
#else
///////////////////
//get debuggerActive: 3 memory accesses, and $7
move_mem_to_reg(OpndSize_32, offGlue_pSelfSuspendCount, PhysicalReg_Glue, true, P_GPR_1, true);
move_mem_to_reg(OpndSize_32, offGlue_pIntoDebugger, PhysicalReg_Glue, true, P_GPR_2, true);
compare_imm_mem(OpndSize_32, 0, 0, P_GPR_1, true); //suspendCount
conditional_jump(Condition_NE, "common_handleSuspend4_1", true); //called once
compare_imm_mem(OpndSize_32, 0, 0, P_GPR_2, true); //debugger active
conditional_jump(Condition_NE, "common_debuggerActive4", true);
//recover registers and return
x86_return();
insertLabel("common_handleSuspend4_1", true);
push_mem_to_stack(OpndSize_32, offGlue_self, PhysicalReg_Glue, true);
call_dvmCheckSuspendPending();
load_effective_addr(4, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
x86_return();
insertLabel("common_debuggerActive4", true);
//%edx: offsetBC (at run time, get method->insns_bytecode, then calculate BCPointer)
move_mem_to_reg(OpndSize_32, offGlue_method, PhysicalReg_Glue, true, P_GPR_1, true);
move_mem_to_reg(OpndSize_32, offMethod_insns_bytecode, P_GPR_1, true, P_GPR_2, true);
alu_binary_reg_reg(OpndSize_32, add_opc, P_GPR_2, true, PhysicalReg_EDX, true);
move_imm_to_mem(OpndSize_32, 0, offGlue_entryPoint, PhysicalReg_Glue, true);
unconditional_jump("common_gotoBail", false); //update glue->rPC with edx
#endif
return 0;
}
//input: %edx PC adjustment
//CHECK: should %edx be saved before calling dvmCheckSuspendPending?
/*!
\brief helper function common_periodicChecks_entry to check GC request
*/
int common_periodicChecks_entry() {
insertLabel("common_periodicChecks_entry", false);
scratchRegs[0] = PhysicalReg_ESI; scratchRegs[1] = PhysicalReg_EAX;
scratchRegs[2] = PhysicalReg_Null; scratchRegs[3] = PhysicalReg_Null;
get_suspendCount(P_GPR_1, true);
//get debuggerActive: 3 memory accesses, and $7
#if 0 //defined(WITH_DEBUGGER)
get_debuggerActive(P_GPR_2, true);
#endif
compare_imm_reg(OpndSize_32, 0, P_GPR_1, true); //suspendCount
conditional_jump(Condition_NE, "common_handleSuspend", true); //called once
#if 0 //defined(WITH_DEBUGGER)
#ifdef NCG_DEBUG
compare_imm_reg(OpndSize_32, 0, P_GPR_2, true); //debugger active
conditional_jump(Condition_NE, "common_debuggerActive", true);
#endif
#endif
//recover registers and return
x86_return();
insertLabel("common_handleSuspend", true);
get_self_pointer(P_GPR_1, true);
load_effective_addr(-4, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
move_reg_to_mem(OpndSize_32, P_GPR_1, true, 0, PhysicalReg_ESP, true);
call_dvmCheckSuspendPending();
load_effective_addr(4, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
x86_return();
#ifdef NCG_DEBUG
insertLabel("common_debuggerActive", true);
//adjust PC!!! use 0(%esp) TODO
set_glue_entryPoint_imm(0); //kInterpEntryInstr);
unconditional_jump("common_gotoBail", false);
#endif
return 0;
}
#undef P_GPR_1
#undef P_GPR_2
/*!
\brief helper function common_gotoBail
input: %edx: BCPointer %esi: Glue
set %eax to 1 (switch interpreter = true), recover the callee-saved registers and return
*/
int common_gotoBail() {
insertLabel("common_gotoBail", false);
//scratchRegs[0] = PhysicalReg_EDX; scratchRegs[1] = PhysicalReg_ESI;
//scratchRegs[2] = PhysicalReg_Null; scratchRegs[3] = PhysicalReg_Null;
//save_pc_fp_to_glue();
get_self_pointer(PhysicalReg_EAX, true);
move_reg_to_mem(OpndSize_32, PhysicalReg_FP, true, offsetof(Thread, interpSave.curFrame), PhysicalReg_EAX, true);
move_reg_to_mem(OpndSize_32, PhysicalReg_EDX, true, offsetof(Thread, interpSave.pc), PhysicalReg_EAX, true);
move_mem_to_reg(OpndSize_32, offsetof(Thread, interpSave.bailPtr), PhysicalReg_EAX, true, PhysicalReg_ESP, true);
move_reg_to_reg(OpndSize_32, PhysicalReg_ESP, true, PhysicalReg_EBP, true);
load_effective_addr(FRAME_SIZE-4, PhysicalReg_EBP, true, PhysicalReg_EBP, true);
move_imm_to_reg(OpndSize_32, 1, PhysicalReg_EAX, true); //return value
move_mem_to_reg(OpndSize_32, -4, PhysicalReg_EBP, true, PhysicalReg_EDI, true);
move_mem_to_reg(OpndSize_32, -8, PhysicalReg_EBP, true, PhysicalReg_ESI, true);
move_mem_to_reg(OpndSize_32, -12, PhysicalReg_EBP, true, PhysicalReg_EBX, true);
move_reg_to_reg(OpndSize_32, PhysicalReg_EBP, true, PhysicalReg_ESP, true);
move_mem_to_reg(OpndSize_32, 0, PhysicalReg_ESP, true, PhysicalReg_EBP, true);
load_effective_addr(4, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
x86_return();
return 0;
}
/*!
\brief helper function common_gotoBail_0
set %eax to 0, recover the callee-saved registers and return
*/
int common_gotoBail_0() {
insertLabel("common_gotoBail_0", false);
get_self_pointer(PhysicalReg_EAX, true);
move_reg_to_mem(OpndSize_32, PhysicalReg_FP, true, offsetof(Thread, interpSave.curFrame), PhysicalReg_EAX, true);
move_reg_to_mem(OpndSize_32, PhysicalReg_EDX, true, offsetof(Thread, interpSave.pc), PhysicalReg_EAX, true);
/*
movl offThread_bailPtr(%ecx),%esp # Restore "setjmp" esp
movl %esp,%ebp
addl $(FRAME_SIZE-4), %ebp # Restore %ebp at point of setjmp
movl EDI_SPILL(%ebp),%edi
movl ESI_SPILL(%ebp),%esi
movl EBX_SPILL(%ebp),%ebx
movl %ebp, %esp # strip frame
pop %ebp # restore caller's ebp
ret # return to dvmMterpStdRun's caller
*/
move_mem_to_reg(OpndSize_32, offsetof(Thread, interpSave.bailPtr), PhysicalReg_EAX, true, PhysicalReg_ESP, true);
move_reg_to_reg(OpndSize_32, PhysicalReg_ESP, true, PhysicalReg_EBP, true);
load_effective_addr(FRAME_SIZE-4, PhysicalReg_EBP, true, PhysicalReg_EBP, true);
move_imm_to_reg(OpndSize_32, 0, PhysicalReg_EAX, true); //return value
move_mem_to_reg(OpndSize_32, -4, PhysicalReg_EBP, true, PhysicalReg_EDI, true);
move_mem_to_reg(OpndSize_32, -8, PhysicalReg_EBP, true, PhysicalReg_ESI, true);
move_mem_to_reg(OpndSize_32, -12, PhysicalReg_EBP, true, PhysicalReg_EBX, true);
move_reg_to_reg(OpndSize_32, PhysicalReg_EBP, true, PhysicalReg_ESP, true);
move_mem_to_reg(OpndSize_32, 0, PhysicalReg_ESP, true, PhysicalReg_EBP, true);
load_effective_addr(4, PhysicalReg_ESP, true, PhysicalReg_ESP, true);
x86_return();
return 0;
}
|