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
|
/*
* Copyright (C) 2009 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.
*/
/*
* This file contains codegen for the Thumb ISA and is intended to be
* includes by:
*
* Codegen-$(TARGET_ARCH_VARIANT).c
*
*/
#define SET_CCODE (cUnit->setCCode = true) /* codegen changes CCode */
#define UNSET_CCODE (cUnit->setCCode = false) /* codegen does not change CCode */
static int coreTemps[] = {r0, r1, r2, r3, r4PC, r7, r8, r9, r10, r11, r12};
static int fpTemps[] = {fr16, fr17, fr18, fr19, fr20, fr21, fr22, fr23,
fr24, fr25, fr26, fr27, fr28, fr29, fr30, fr31};
static int encodeImmSingle(int value)
{
int res;
int bitA = (value & 0x80000000) >> 31;
int notBitB = (value & 0x40000000) >> 30;
int bitB = (value & 0x20000000) >> 29;
int bSmear = (value & 0x3e000000) >> 25;
int slice = (value & 0x01f80000) >> 19;
int zeroes = (value & 0x0007ffff);
if (zeroes != 0)
return -1;
if (bitB) {
if ((notBitB != 0) || (bSmear != 0x1f))
return -1;
} else {
if ((notBitB != 1) || (bSmear != 0x0))
return -1;
}
res = (bitA << 7) | (bitB << 6) | slice;
return res;
}
static ArmLIR *loadFPConstantValue(CompilationUnit *cUnit, int rDest,
int value)
{
int encodedImm = encodeImmSingle(value);
assert(SINGLEREG(rDest));
if (value == 0) {
// TODO: we need better info about the target CPU. a vector exclusive or
// would probably be better here if we could rely on its existance.
// Load an immediate +2.0 (which encodes to 0)
newLIR2(cUnit, kThumb2Vmovs_IMM8, rDest, 0);
// +0.0 = +2.0 - +2.0
return newLIR3(cUnit, kThumb2Vsubs, rDest, rDest, rDest);
} else if (encodedImm >= 0) {
return newLIR2(cUnit, kThumb2Vmovs_IMM8, rDest, encodedImm);
}
ArmLIR *dataTarget = scanLiteralPool(cUnit->literalList, value, 0);
if (dataTarget == NULL) {
dataTarget = addWordData(cUnit, &cUnit->literalList, value);
}
ArmLIR *loadPcRel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
loadPcRel->opcode = kThumb2Vldrs;
loadPcRel->generic.target = (LIR *) dataTarget;
loadPcRel->operands[0] = rDest;
loadPcRel->operands[1] = r15pc;
setupResourceMasks(loadPcRel);
setMemRefType(loadPcRel, true, kLiteral);
loadPcRel->aliasInfo = dataTarget->operands[0];
dvmCompilerAppendLIR(cUnit, (LIR *) loadPcRel);
return loadPcRel;
}
static int leadingZeros(u4 val)
{
u4 alt;
int n;
int count;
count = 16;
n = 32;
do {
alt = val >> count;
if (alt != 0) {
n = n - count;
val = alt;
}
count >>= 1;
} while (count);
return n - val;
}
/*
* Determine whether value can be encoded as a Thumb2 modified
* immediate. If not, return -1. If so, return i:imm3:a:bcdefgh form.
*/
static int modifiedImmediate(u4 value)
{
int zLeading;
int zTrailing;
u4 b0 = value & 0xff;
/* Note: case of value==0 must use 0:000:0:0000000 encoding */
if (value <= 0xFF)
return b0; // 0:000:a:bcdefgh
if (value == ((b0 << 16) | b0))
return (0x1 << 8) | b0; /* 0:001:a:bcdefgh */
if (value == ((b0 << 24) | (b0 << 16) | (b0 << 8) | b0))
return (0x3 << 8) | b0; /* 0:011:a:bcdefgh */
b0 = (value >> 8) & 0xff;
if (value == ((b0 << 24) | (b0 << 8)))
return (0x2 << 8) | b0; /* 0:010:a:bcdefgh */
/* Can we do it with rotation? */
zLeading = leadingZeros(value);
zTrailing = 32 - leadingZeros(~value & (value - 1));
/* A run of eight or fewer active bits? */
if ((zLeading + zTrailing) < 24)
return -1; /* No - bail */
/* left-justify the constant, discarding msb (known to be 1) */
value <<= zLeading + 1;
/* Create bcdefgh */
value >>= 25;
/* Put it all together */
return value | ((0x8 + zLeading) << 7); /* [01000..11111]:bcdefgh */
}
/*
* Load a immediate using a shortcut if possible; otherwise
* grab from the per-translation literal pool.
*
* No additional register clobbering operation performed. Use this version when
* 1) rDest is freshly returned from dvmCompilerAllocTemp or
* 2) The codegen is under fixed register usage
*/
static ArmLIR *loadConstantNoClobber(CompilationUnit *cUnit, int rDest,
int value)
{
ArmLIR *res;
int modImm;
if (FPREG(rDest)) {
return loadFPConstantValue(cUnit, rDest, value);
}
/* Check Modified immediate special cases */
modImm = modifiedImmediate(value);
if (modImm >= 0) {
res = newLIR2(cUnit, kThumb2MovImmShift, rDest, modImm);
return res;
}
modImm = modifiedImmediate(~value);
if (modImm >= 0) {
res = newLIR2(cUnit, kThumb2MvnImmShift, rDest, modImm);
return res;
}
/* 16-bit immediate? */
if ((value & 0xffff) == value) {
res = newLIR2(cUnit, kThumb2MovImm16, rDest, value);
return res;
}
/* No shortcut - go ahead and use literal pool */
ArmLIR *dataTarget = scanLiteralPool(cUnit->literalList, value, 0);
if (dataTarget == NULL) {
dataTarget = addWordData(cUnit, &cUnit->literalList, value);
}
ArmLIR *loadPcRel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
loadPcRel->opcode = kThumb2LdrPcRel12;
loadPcRel->generic.target = (LIR *) dataTarget;
loadPcRel->operands[0] = rDest;
setupResourceMasks(loadPcRel);
setMemRefType(loadPcRel, true, kLiteral);
loadPcRel->aliasInfo = dataTarget->operands[0];
res = loadPcRel;
dvmCompilerAppendLIR(cUnit, (LIR *) loadPcRel);
/*
* To save space in the constant pool, we use the ADD_RRI8 instruction to
* add up to 255 to an existing constant value.
*/
if (dataTarget->operands[0] != value) {
opRegImm(cUnit, kOpAdd, rDest, value - dataTarget->operands[0]);
}
return res;
}
/*
* Load an immediate value into a fixed or temp register. Target
* register is clobbered, and marked inUse.
*/
static ArmLIR *loadConstant(CompilationUnit *cUnit, int rDest, int value)
{
if (dvmCompilerIsTemp(cUnit, rDest)) {
dvmCompilerClobber(cUnit, rDest);
dvmCompilerMarkInUse(cUnit, rDest);
}
return loadConstantNoClobber(cUnit, rDest, value);
}
/*
* Load a class pointer value into a fixed or temp register. Target
* register is clobbered, and marked inUse.
*/
static ArmLIR *loadClassPointer(CompilationUnit *cUnit, int rDest, int value)
{
ArmLIR *res;
cUnit->hasClassLiterals = true;
if (dvmCompilerIsTemp(cUnit, rDest)) {
dvmCompilerClobber(cUnit, rDest);
dvmCompilerMarkInUse(cUnit, rDest);
}
ArmLIR *dataTarget = scanLiteralPool(cUnit->classPointerList, value, 0);
if (dataTarget == NULL) {
dataTarget = addWordData(cUnit, &cUnit->classPointerList, value);
/* Counts the number of class pointers in this translation */
cUnit->numClassPointers++;
}
ArmLIR *loadPcRel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
loadPcRel->opcode = kThumb2LdrPcRel12;
loadPcRel->generic.target = (LIR *) dataTarget;
loadPcRel->operands[0] = rDest;
setupResourceMasks(loadPcRel);
setMemRefType(loadPcRel, true, kLiteral);
loadPcRel->aliasInfo = dataTarget->operands[0];
res = loadPcRel;
dvmCompilerAppendLIR(cUnit, (LIR *) loadPcRel);
return res;
}
static ArmLIR *opNone(CompilationUnit *cUnit, OpKind op)
{
ArmOpcode opcode = kThumbBkpt;
switch (op) {
case kOpUncondBr:
opcode = kThumbBUncond;
break;
default:
assert(0);
}
return newLIR0(cUnit, opcode);
}
static ArmLIR *opCondBranch(CompilationUnit *cUnit, ArmConditionCode cc)
{
return newLIR2(cUnit, kThumb2BCond, 0 /* offset to be patched */, cc);
}
static ArmLIR *opImm(CompilationUnit *cUnit, OpKind op, int value)
{
ArmOpcode opcode = kThumbBkpt;
switch (op) {
case kOpPush: {
if ((value & 0xff00) == 0) {
opcode = kThumbPush;
} else if ((value & 0xff00) == (1 << r14lr)) {
/* Thumb push can handle lr, which is encoded by bit 8 */
opcode = kThumbPush;
value = (value & 0xff) | (1<<8);
} else {
opcode = kThumb2Push;
}
break;
}
case kOpPop: {
if ((value & 0xff00) == 0) {
opcode = kThumbPop;
} else if ((value & 0xff00) == (1 << r15pc)) {
/* Thumb pop can handle pc, which is encoded by bit 8 */
opcode = kThumbPop;
value = (value & 0xff) | (1<<8);
} else {
opcode = kThumb2Pop;
}
break;
}
default:
assert(0);
}
return newLIR1(cUnit, opcode, value);
}
static ArmLIR *opReg(CompilationUnit *cUnit, OpKind op, int rDestSrc)
{
ArmOpcode opcode = kThumbBkpt;
switch (op) {
case kOpBlx:
opcode = kThumbBlxR;
break;
default:
assert(0);
}
return newLIR1(cUnit, opcode, rDestSrc);
}
__attribute__((weak)) ArmLIR *opRegRegShiftThumb2(CompilationUnit *cUnit, OpKind op,
int rDestSrc1, int rSrc2, int shift)
{
return NULL;
}
static ArmLIR *opRegRegShift(CompilationUnit *cUnit, OpKind op, int rDestSrc1,
int rSrc2, int shift)
{
ArmLIR *res;
if((res = opRegRegShiftThumb2(cUnit, op, rDestSrc1, rSrc2, shift)))
return res;
bool thumbForm = ((shift == 0) && LOWREG(rDestSrc1) && LOWREG(rSrc2));
ArmOpcode opcode = kThumbBkpt;
switch (op) {
case kOpAdc:
opcode = (thumbForm) ? kThumbAdcRR : kThumb2AdcRRR;
break;
case kOpAnd:
opcode = (thumbForm) ? kThumbAndRR : kThumb2AndRRR;
break;
case kOpBic:
opcode = (thumbForm) ? kThumbBicRR : kThumb2BicRRR;
break;
case kOpCmn:
assert(shift == 0);
opcode = (thumbForm) ? kThumbCmnRR : kThumb2CmnRR;
break;
case kOpCmp:
if (thumbForm)
opcode = kThumbCmpRR;
else if ((shift == 0) && !LOWREG(rDestSrc1) && !LOWREG(rSrc2))
opcode = kThumbCmpHH;
else if ((shift == 0) && LOWREG(rDestSrc1))
opcode = kThumbCmpLH;
else if (shift == 0)
opcode = kThumbCmpHL;
else
opcode = kThumb2CmpRR;
break;
case kOpXor:
opcode = (thumbForm) ? kThumbEorRR : kThumb2EorRRR;
break;
case kOpMov:
assert(shift == 0);
if (LOWREG(rDestSrc1) && LOWREG(rSrc2))
opcode = kThumbMovRR;
else if (!LOWREG(rDestSrc1) && !LOWREG(rSrc2))
opcode = kThumbMovRR_H2H;
else if (LOWREG(rDestSrc1))
opcode = kThumbMovRR_H2L;
else
opcode = kThumbMovRR_L2H;
break;
case kOpMul:
assert(shift == 0);
opcode = (thumbForm) ? kThumbMul : kThumb2MulRRR;
break;
case kOpMvn:
opcode = (thumbForm) ? kThumbMvn : kThumb2MvnRR;
break;
case kOpNeg:
assert(shift == 0);
opcode = (thumbForm) ? kThumbNeg : kThumb2NegRR;
break;
case kOpOr:
opcode = (thumbForm) ? kThumbOrr : kThumb2OrrRRR;
break;
case kOpSbc:
opcode = (thumbForm) ? kThumbSbc : kThumb2SbcRRR;
break;
case kOpTst:
opcode = (thumbForm) ? kThumbTst : kThumb2TstRR;
break;
case kOpLsl:
assert(shift == 0);
opcode = (thumbForm) ? kThumbLslRR : kThumb2LslRRR;
break;
case kOpLsr:
assert(shift == 0);
opcode = (thumbForm) ? kThumbLsrRR : kThumb2LsrRRR;
break;
case kOpAsr:
assert(shift == 0);
opcode = (thumbForm) ? kThumbAsrRR : kThumb2AsrRRR;
break;
case kOpRor:
assert(shift == 0);
opcode = (thumbForm) ? kThumbRorRR : kThumb2RorRRR;
break;
case kOpAdd:
opcode = (thumbForm) ? kThumbAddRRR : kThumb2AddRRR;
break;
case kOpSub:
opcode = (thumbForm) ? kThumbSubRRR : kThumb2SubRRR;
break;
case kOp2Byte:
assert(shift == 0);
return newLIR4(cUnit, kThumb2Sbfx, rDestSrc1, rSrc2, 0, 8);
case kOp2Short:
assert(shift == 0);
return newLIR4(cUnit, kThumb2Sbfx, rDestSrc1, rSrc2, 0, 16);
case kOp2Char:
assert(shift == 0);
return newLIR4(cUnit, kThumb2Ubfx, rDestSrc1, rSrc2, 0, 16);
default:
assert(0);
break;
}
assert(opcode >= 0);
if (getEncoding(opcode)->flags & IS_BINARY_OP)
return newLIR2(cUnit, opcode, rDestSrc1, rSrc2);
else if (getEncoding(opcode)->flags & IS_TERTIARY_OP) {
if (getEncoding(opcode)->fieldLoc[2].kind == kFmtShift)
return newLIR3(cUnit, opcode, rDestSrc1, rSrc2, shift);
else
return newLIR3(cUnit, opcode, rDestSrc1, rDestSrc1, rSrc2);
} else if (getEncoding(opcode)->flags & IS_QUAD_OP)
return newLIR4(cUnit, opcode, rDestSrc1, rDestSrc1, rSrc2, shift);
else {
assert(0);
return NULL;
}
}
static ArmLIR *opRegReg(CompilationUnit *cUnit, OpKind op, int rDestSrc1,
int rSrc2)
{
return opRegRegShift(cUnit, op, rDestSrc1, rSrc2, 0);
}
__attribute__((weak)) ArmLIR *opRegRegRegShiftThumb2(CompilationUnit *cUnit, OpKind op,
int rDest, int rSrc1, int rSrc2, int shift)
{
return NULL;
}
static ArmLIR *opRegRegRegShift(CompilationUnit *cUnit, OpKind op,
int rDest, int rSrc1, int rSrc2, int shift)
{
ArmLIR *res;
if((res = opRegRegRegShiftThumb2(cUnit, op, rDest, rSrc1, rSrc2, shift)))
return res;
ArmOpcode opcode = kThumbBkpt;
bool thumbForm = (shift == 0) && LOWREG(rDest) && LOWREG(rSrc1) &&
LOWREG(rSrc2);
switch (op) {
case kOpAdd:
opcode = (thumbForm) ? kThumbAddRRR : kThumb2AddRRR;
break;
case kOpSub:
opcode = (thumbForm) ? kThumbSubRRR : kThumb2SubRRR;
break;
case kOpAdc:
opcode = kThumb2AdcRRR;
break;
case kOpAnd:
opcode = kThumb2AndRRR;
break;
case kOpBic:
opcode = kThumb2BicRRR;
break;
case kOpXor:
opcode = kThumb2EorRRR;
break;
case kOpMul:
assert(shift == 0);
opcode = kThumb2MulRRR;
break;
case kOpOr:
opcode = kThumb2OrrRRR;
break;
case kOpSbc:
opcode = kThumb2SbcRRR;
break;
case kOpLsl:
assert(shift == 0);
opcode = kThumb2LslRRR;
break;
case kOpLsr:
assert(shift == 0);
opcode = kThumb2LsrRRR;
break;
case kOpAsr:
assert(shift == 0);
opcode = kThumb2AsrRRR;
break;
case kOpRor:
assert(shift == 0);
opcode = kThumb2RorRRR;
break;
default:
assert(0);
break;
}
assert(opcode >= 0);
if (getEncoding(opcode)->flags & IS_QUAD_OP)
return newLIR4(cUnit, opcode, rDest, rSrc1, rSrc2, shift);
else {
assert(getEncoding(opcode)->flags & IS_TERTIARY_OP);
return newLIR3(cUnit, opcode, rDest, rSrc1, rSrc2);
}
}
static ArmLIR *opRegRegReg(CompilationUnit *cUnit, OpKind op, int rDest,
int rSrc1, int rSrc2)
{
return opRegRegRegShift(cUnit, op, rDest, rSrc1, rSrc2, 0);
}
__attribute__((weak)) ArmLIR *opRegRegImmThumb2(CompilationUnit *cUnit, OpKind op, int rDest,
int rSrc1, int value)
{
return NULL;
}
static ArmLIR *opRegRegImm(CompilationUnit *cUnit, OpKind op, int rDest,
int rSrc1, int value)
{
ArmLIR *res;
if((res = opRegRegImmThumb2(cUnit, op, rDest, rSrc1, value)))
return res;
bool neg = (value < 0);
int absValue = (neg) ? -value : value;
ArmOpcode opcode = kThumbBkpt;
ArmOpcode altOpcode = kThumbBkpt;
bool allLowRegs = (LOWREG(rDest) && LOWREG(rSrc1));
int modImm = modifiedImmediate(value);
int modImmNeg = modifiedImmediate(-value);
switch(op) {
case kOpLsl:
if (allLowRegs)
return newLIR3(cUnit, kThumbLslRRI5, rDest, rSrc1, value);
else
return newLIR3(cUnit, kThumb2LslRRI5, rDest, rSrc1, value);
case kOpLsr:
if (allLowRegs)
return newLIR3(cUnit, kThumbLsrRRI5, rDest, rSrc1, value);
else
return newLIR3(cUnit, kThumb2LsrRRI5, rDest, rSrc1, value);
case kOpAsr:
if (allLowRegs)
return newLIR3(cUnit, kThumbAsrRRI5, rDest, rSrc1, value);
else
return newLIR3(cUnit, kThumb2AsrRRI5, rDest, rSrc1, value);
case kOpRor:
return newLIR3(cUnit, kThumb2RorRRI5, rDest, rSrc1, value);
case kOpAdd:
if (LOWREG(rDest) && (rSrc1 == r13sp) &&
(value <= 1020) && ((value & 0x3)==0)) {
return newLIR3(cUnit, kThumbAddSpRel, rDest, rSrc1,
value >> 2);
} else if (LOWREG(rDest) && (rSrc1 == r15pc) &&
(value <= 1020) && ((value & 0x3)==0)) {
return newLIR3(cUnit, kThumbAddPcRel, rDest, rSrc1,
value >> 2);
}
opcode = kThumb2AddRRI8;
altOpcode = kThumb2AddRRR;
// Note: intentional fallthrough
case kOpSub:
if (allLowRegs && ((absValue & 0x7) == absValue)) {
if (op == kOpAdd)
opcode = (neg) ? kThumbSubRRI3 : kThumbAddRRI3;
else
opcode = (neg) ? kThumbAddRRI3 : kThumbSubRRI3;
return newLIR3(cUnit, opcode, rDest, rSrc1, absValue);
} else if ((absValue & 0xff) == absValue) {
if (op == kOpAdd)
opcode = (neg) ? kThumb2SubRRI12 : kThumb2AddRRI12;
else
opcode = (neg) ? kThumb2AddRRI12 : kThumb2SubRRI12;
return newLIR3(cUnit, opcode, rDest, rSrc1, absValue);
}
if (modImmNeg >= 0) {
op = (op == kOpAdd) ? kOpSub : kOpAdd;
modImm = modImmNeg;
}
if (op == kOpSub) {
opcode = kThumb2SubRRI8;
altOpcode = kThumb2SubRRR;
}
break;
case kOpAdc:
opcode = kThumb2AdcRRI8;
altOpcode = kThumb2AdcRRR;
break;
case kOpSbc:
opcode = kThumb2SbcRRI8;
altOpcode = kThumb2SbcRRR;
break;
case kOpOr:
opcode = kThumb2OrrRRI8;
altOpcode = kThumb2OrrRRR;
break;
case kOpAnd:
opcode = kThumb2AndRRI8;
altOpcode = kThumb2AndRRR;
break;
case kOpXor:
opcode = kThumb2EorRRI8;
altOpcode = kThumb2EorRRR;
break;
case kOpMul:
//TUNING: power of 2, shift & add
modImm = -1;
altOpcode = kThumb2MulRRR;
break;
case kOpCmp: {
int modImm = modifiedImmediate(value);
ArmLIR *res;
if (modImm >= 0) {
res = newLIR2(cUnit, kThumb2CmpRI8, rSrc1, modImm);
} else {
int rTmp = dvmCompilerAllocTemp(cUnit);
res = loadConstant(cUnit, rTmp, value);
opRegReg(cUnit, kOpCmp, rSrc1, rTmp);
dvmCompilerFreeTemp(cUnit, rTmp);
}
return res;
}
default:
assert(0);
}
if (modImm >= 0) {
return newLIR3(cUnit, opcode, rDest, rSrc1, modImm);
} else {
int rScratch = dvmCompilerAllocTemp(cUnit);
loadConstant(cUnit, rScratch, value);
if (getEncoding(altOpcode)->flags & IS_QUAD_OP)
res = newLIR4(cUnit, altOpcode, rDest, rSrc1, rScratch, 0);
else
res = newLIR3(cUnit, altOpcode, rDest, rSrc1, rScratch);
dvmCompilerFreeTemp(cUnit, rScratch);
return res;
}
}
__attribute__((weak)) ArmLIR *opRegImmThumb2(CompilationUnit *cUnit, OpKind op, int rDestSrc1,
int value)
{
return NULL;
}
/* Handle Thumb-only variants here - otherwise punt to opRegRegImm */
static ArmLIR *opRegImm(CompilationUnit *cUnit, OpKind op, int rDestSrc1,
int value)
{
ArmLIR *res;
if((res = opRegImmThumb2(cUnit, op, rDestSrc1, value)))
return res;
bool neg = (value < 0);
int absValue = (neg) ? -value : value;
bool shortForm = (((absValue & 0xff) == absValue) && LOWREG(rDestSrc1));
ArmOpcode opcode = kThumbBkpt;
switch (op) {
case kOpAdd:
if ( !neg && (rDestSrc1 == r13sp) && (value <= 508)) { /* sp */
assert((value & 0x3) == 0);
return newLIR1(cUnit, kThumbAddSpI7, value >> 2);
} else if (shortForm) {
opcode = (neg) ? kThumbSubRI8 : kThumbAddRI8;
}
break;
case kOpSub:
if (!neg && (rDestSrc1 == r13sp) && (value <= 508)) { /* sp */
assert((value & 0x3) == 0);
return newLIR1(cUnit, kThumbSubSpI7, value >> 2);
} else if (shortForm) {
opcode = (neg) ? kThumbAddRI8 : kThumbSubRI8;
}
break;
case kOpCmp:
if (LOWREG(rDestSrc1) && shortForm)
opcode = (shortForm) ? kThumbCmpRI8 : kThumbCmpRR;
else if (LOWREG(rDestSrc1))
opcode = kThumbCmpRR;
else {
shortForm = false;
opcode = kThumbCmpHL;
}
break;
default:
/* Punt to opRegRegImm - if bad case catch it there */
shortForm = false;
break;
}
if (shortForm)
return newLIR2(cUnit, opcode, rDestSrc1, absValue);
else {
return opRegRegImm(cUnit, op, rDestSrc1, rDestSrc1, value);
}
}
/*
* Determine whether value can be encoded as a Thumb2 floating point
* immediate. If not, return -1. If so return encoded 8-bit value.
*/
static int encodeImmDoubleHigh(int value)
{
int res;
int bitA = (value & 0x80000000) >> 31;
int notBitB = (value & 0x40000000) >> 30;
int bitB = (value & 0x20000000) >> 29;
int bSmear = (value & 0x3fc00000) >> 22;
int slice = (value & 0x003f0000) >> 16;
int zeroes = (value & 0x0000ffff);
if (zeroes != 0)
return -1;
if (bitB) {
if ((notBitB != 0) || (bSmear != 0x1f))
return -1;
} else {
if ((notBitB != 1) || (bSmear != 0x0))
return -1;
}
res = (bitA << 7) | (bitB << 6) | slice;
return res;
}
static int encodeImmDouble(int valLo, int valHi)
{
int res = -1;
if (valLo == 0)
res = encodeImmDoubleHigh(valHi);
return res;
}
static ArmLIR *loadConstantValueWide(CompilationUnit *cUnit, int rDestLo,
int rDestHi, int valLo, int valHi)
{
int encodedImm = encodeImmDouble(valLo, valHi);
ArmLIR *res;
int targetReg = S2D(rDestLo, rDestHi);
if (FPREG(rDestLo)) {
if ((valLo == 0) && (valHi == 0)) {
// TODO: we need better info about the target CPU. a vector
// exclusive or would probably be better here if we could rely on
// its existance.
// Load an immediate +2.0 (which encodes to 0)
newLIR2(cUnit, kThumb2Vmovd_IMM8, targetReg, 0);
// +0.0 = +2.0 - +2.0
res = newLIR3(cUnit, kThumb2Vsubd, targetReg, targetReg, targetReg);
} else if (encodedImm >= 0) {
res = newLIR2(cUnit, kThumb2Vmovd_IMM8, targetReg, encodedImm);
} else {
ArmLIR* dataTarget = scanLiteralPoolWide(cUnit->literalList, valLo, valHi);
if (dataTarget == NULL) {
dataTarget = addWideData(cUnit, &cUnit->literalList, valLo, valHi);
}
ArmLIR *loadPcRel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
loadPcRel->opcode = kThumb2Vldrd;
loadPcRel->generic.target = (LIR *) dataTarget;
loadPcRel->operands[0] = targetReg;
loadPcRel->operands[1] = r15pc;
setupResourceMasks(loadPcRel);
setMemRefType(loadPcRel, true, kLiteral);
// TODO: rework literal load disambiguation to more cleanly handle 64-bit loads
loadPcRel->aliasInfo = (uintptr_t)dataTarget;
dvmCompilerAppendLIR(cUnit, (LIR *) loadPcRel);
res = loadPcRel;
}
} else {
res = loadConstantNoClobber(cUnit, rDestLo, valLo);
loadConstantNoClobber(cUnit, rDestHi, valHi);
}
return res;
}
static int encodeShift(int code, int amount) {
return ((amount & 0x1f) << 2) | code;
}
static ArmLIR *loadBaseIndexed(CompilationUnit *cUnit, int rBase,
int rIndex, int rDest, int scale, OpSize size)
{
bool allLowRegs = LOWREG(rBase) && LOWREG(rIndex) && LOWREG(rDest);
ArmLIR *load;
ArmOpcode opcode = kThumbBkpt;
bool thumbForm = (allLowRegs && (scale == 0));
int regPtr;
if (FPREG(rDest)) {
assert(SINGLEREG(rDest));
assert((size == kWord) || (size == kSingle));
opcode = kThumb2Vldrs;
size = kSingle;
} else {
if (size == kSingle)
size = kWord;
}
switch (size) {
case kSingle:
regPtr = dvmCompilerAllocTemp(cUnit);
if (scale) {
newLIR4(cUnit, kThumb2AddRRR, regPtr, rBase, rIndex,
encodeShift(kArmLsl, scale));
} else {
opRegRegReg(cUnit, kOpAdd, regPtr, rBase, rIndex);
}
load = newLIR3(cUnit, opcode, rDest, regPtr, 0);
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
load->flags.insertWrapper = true;
#endif
return load;
case kWord:
opcode = (thumbForm) ? kThumbLdrRRR : kThumb2LdrRRR;
break;
case kUnsignedHalf:
opcode = (thumbForm) ? kThumbLdrhRRR : kThumb2LdrhRRR;
break;
case kSignedHalf:
opcode = (thumbForm) ? kThumbLdrshRRR : kThumb2LdrshRRR;
break;
case kUnsignedByte:
opcode = (thumbForm) ? kThumbLdrbRRR : kThumb2LdrbRRR;
break;
case kSignedByte:
opcode = (thumbForm) ? kThumbLdrsbRRR : kThumb2LdrsbRRR;
break;
default:
assert(0);
}
if (thumbForm)
load = newLIR3(cUnit, opcode, rDest, rBase, rIndex);
else
load = newLIR4(cUnit, opcode, rDest, rBase, rIndex, scale);
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
load->flags.insertWrapper = true;
#endif
return load;
}
static ArmLIR *storeBaseIndexed(CompilationUnit *cUnit, int rBase,
int rIndex, int rSrc, int scale, OpSize size)
{
bool allLowRegs = LOWREG(rBase) && LOWREG(rIndex) && LOWREG(rSrc);
ArmLIR *store;
ArmOpcode opcode = kThumbBkpt;
bool thumbForm = (allLowRegs && (scale == 0));
int regPtr;
if (FPREG(rSrc)) {
assert(SINGLEREG(rSrc));
assert((size == kWord) || (size == kSingle));
opcode = kThumb2Vstrs;
size = kSingle;
} else {
if (size == kSingle)
size = kWord;
}
switch (size) {
case kSingle:
regPtr = dvmCompilerAllocTemp(cUnit);
if (scale) {
newLIR4(cUnit, kThumb2AddRRR, regPtr, rBase, rIndex,
encodeShift(kArmLsl, scale));
} else {
opRegRegReg(cUnit, kOpAdd, regPtr, rBase, rIndex);
}
store = newLIR3(cUnit, opcode, rSrc, regPtr, 0);
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
store->flags.insertWrapper = true;
#endif
return store;
case kWord:
opcode = (thumbForm) ? kThumbStrRRR : kThumb2StrRRR;
break;
case kUnsignedHalf:
case kSignedHalf:
opcode = (thumbForm) ? kThumbStrhRRR : kThumb2StrhRRR;
break;
case kUnsignedByte:
case kSignedByte:
opcode = (thumbForm) ? kThumbStrbRRR : kThumb2StrbRRR;
break;
default:
assert(0);
}
if (thumbForm)
store = newLIR3(cUnit, opcode, rSrc, rBase, rIndex);
else
store = newLIR4(cUnit, opcode, rSrc, rBase, rIndex, scale);
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
store->flags.insertWrapper = true;
#endif
return store;
}
/*
* Load value from base + displacement. Optionally perform null check
* on base (which must have an associated sReg and MIR). If not
* performing null check, incoming MIR can be null.
*/
static ArmLIR *loadBaseDispBody(CompilationUnit *cUnit, MIR *mir, int rBase,
int displacement, int rDest, int rDestHi,
OpSize size, int sReg)
{
ArmLIR *res, *load;
ArmOpcode opcode = kThumbBkpt;
bool shortForm = false;
bool thumb2Form = (displacement < 4092 && displacement >= 0);
bool allLowRegs = (LOWREG(rBase) && LOWREG(rDest));
int encodedDisp = displacement;
switch (size) {
case kDouble:
case kLong:
if (FPREG(rDest)) {
if (SINGLEREG(rDest)) {
assert(FPREG(rDestHi));
rDest = S2D(rDest, rDestHi);
}
opcode = kThumb2Vldrd;
if (displacement <= 1020) {
shortForm = true;
encodedDisp >>= 2;
}
break;
} else {
res = loadBaseDispBody(cUnit, mir, rBase, displacement, rDest,
-1, kWord, sReg);
loadBaseDispBody(cUnit, NULL, rBase, displacement + 4, rDestHi,
-1, kWord, INVALID_SREG);
return res;
}
case kSingle:
case kWord:
if (FPREG(rDest)) {
opcode = kThumb2Vldrs;
if (displacement <= 1020) {
shortForm = true;
encodedDisp >>= 2;
}
break;
}
if (LOWREG(rDest) && (rBase == r15pc) &&
(displacement <= 1020) && (displacement >= 0)) {
shortForm = true;
encodedDisp >>= 2;
opcode = kThumbLdrPcRel;
} else if (LOWREG(rDest) && (rBase == r13sp) &&
(displacement <= 1020) && (displacement >= 0)) {
shortForm = true;
encodedDisp >>= 2;
opcode = kThumbLdrSpRel;
} else if (allLowRegs && displacement < 128 && displacement >= 0) {
assert((displacement & 0x3) == 0);
shortForm = true;
encodedDisp >>= 2;
opcode = kThumbLdrRRI5;
} else if (thumb2Form) {
shortForm = true;
opcode = kThumb2LdrRRI12;
}
break;
case kUnsignedHalf:
if (allLowRegs && displacement < 64 && displacement >= 0) {
assert((displacement & 0x1) == 0);
shortForm = true;
encodedDisp >>= 1;
opcode = kThumbLdrhRRI5;
} else if (displacement < 4092 && displacement >= 0) {
shortForm = true;
opcode = kThumb2LdrhRRI12;
}
break;
case kSignedHalf:
if (thumb2Form) {
shortForm = true;
opcode = kThumb2LdrshRRI12;
}
break;
case kUnsignedByte:
if (allLowRegs && displacement < 32 && displacement >= 0) {
shortForm = true;
opcode = kThumbLdrbRRI5;
} else if (thumb2Form) {
shortForm = true;
opcode = kThumb2LdrbRRI12;
}
break;
case kSignedByte:
if (thumb2Form) {
shortForm = true;
opcode = kThumb2LdrsbRRI12;
}
break;
default:
assert(0);
}
if (shortForm) {
load = res = newLIR3(cUnit, opcode, rDest, rBase, encodedDisp);
} else {
int regOffset = dvmCompilerAllocTemp(cUnit);
res = loadConstant(cUnit, regOffset, encodedDisp);
load = loadBaseIndexed(cUnit, rBase, regOffset, rDest, 0, size);
dvmCompilerFreeTemp(cUnit, regOffset);
}
if (rBase == r5FP) {
annotateDalvikRegAccess(load, displacement >> 2, true /* isLoad */);
}
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
load->flags.insertWrapper = true;
#endif
return load;
}
static ArmLIR *loadBaseDisp(CompilationUnit *cUnit, MIR *mir, int rBase,
int displacement, int rDest, OpSize size,
int sReg)
{
return loadBaseDispBody(cUnit, mir, rBase, displacement, rDest, -1,
size, sReg);
}
static ArmLIR *loadBaseDispWide(CompilationUnit *cUnit, MIR *mir, int rBase,
int displacement, int rDestLo, int rDestHi,
int sReg)
{
return loadBaseDispBody(cUnit, mir, rBase, displacement, rDestLo, rDestHi,
kLong, sReg);
}
static ArmLIR *storeBaseDispBody(CompilationUnit *cUnit, int rBase,
int displacement, int rSrc, int rSrcHi,
OpSize size)
{
ArmLIR *res, *store;
ArmOpcode opcode = kThumbBkpt;
bool shortForm = false;
bool thumb2Form = (displacement < 4092 && displacement >= 0);
bool allLowRegs = (LOWREG(rBase) && LOWREG(rSrc));
int encodedDisp = displacement;
switch (size) {
case kLong:
case kDouble:
if (!FPREG(rSrc)) {
res = storeBaseDispBody(cUnit, rBase, displacement, rSrc,
-1, kWord);
storeBaseDispBody(cUnit, rBase, displacement + 4, rSrcHi,
-1, kWord);
return res;
}
if (SINGLEREG(rSrc)) {
assert(FPREG(rSrcHi));
rSrc = S2D(rSrc, rSrcHi);
}
opcode = kThumb2Vstrd;
if (displacement <= 1020) {
shortForm = true;
encodedDisp >>= 2;
}
break;
case kSingle:
case kWord:
if (FPREG(rSrc)) {
assert(SINGLEREG(rSrc));
opcode = kThumb2Vstrs;
if (displacement <= 1020) {
shortForm = true;
encodedDisp >>= 2;
}
break;
}
if (allLowRegs && displacement < 128 && displacement >= 0) {
assert((displacement & 0x3) == 0);
shortForm = true;
encodedDisp >>= 2;
opcode = kThumbStrRRI5;
} else if (thumb2Form) {
shortForm = true;
opcode = kThumb2StrRRI12;
}
break;
case kUnsignedHalf:
case kSignedHalf:
if (allLowRegs && displacement < 64 && displacement >= 0) {
assert((displacement & 0x1) == 0);
shortForm = true;
encodedDisp >>= 1;
opcode = kThumbStrhRRI5;
} else if (thumb2Form) {
shortForm = true;
opcode = kThumb2StrhRRI12;
}
break;
case kUnsignedByte:
case kSignedByte:
if (allLowRegs && displacement < 32 && displacement >= 0) {
shortForm = true;
opcode = kThumbStrbRRI5;
} else if (thumb2Form) {
shortForm = true;
opcode = kThumb2StrbRRI12;
}
break;
default:
assert(0);
}
if (shortForm) {
store = res = newLIR3(cUnit, opcode, rSrc, rBase, encodedDisp);
} else {
int rScratch = dvmCompilerAllocTemp(cUnit);
res = loadConstant(cUnit, rScratch, encodedDisp);
store = storeBaseIndexed(cUnit, rBase, rScratch, rSrc, 0, size);
dvmCompilerFreeTemp(cUnit, rScratch);
}
if (rBase == r5FP) {
annotateDalvikRegAccess(store, displacement >> 2, false /* isLoad */);
}
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
store->flags.insertWrapper = true;
#endif
return res;
}
static ArmLIR *storeBaseDisp(CompilationUnit *cUnit, int rBase,
int displacement, int rSrc, OpSize size)
{
return storeBaseDispBody(cUnit, rBase, displacement, rSrc, -1, size);
}
static ArmLIR *storeBaseDispWide(CompilationUnit *cUnit, int rBase,
int displacement, int rSrcLo, int rSrcHi)
{
return storeBaseDispBody(cUnit, rBase, displacement, rSrcLo, rSrcHi, kLong);
}
static ArmLIR *loadMultiple(CompilationUnit *cUnit, int rBase, int rMask)
{
ArmLIR *res;
genBarrier(cUnit);
if (LOWREG(rBase) && ((rMask & 0xff)==rMask)) {
res = newLIR2(cUnit, kThumbLdmia, rBase, rMask);
} else {
res = newLIR2(cUnit, kThumb2Ldmia, rBase, rMask);
}
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
res->flags.insertWrapper = true;
#endif
genBarrier(cUnit);
return res;
}
static ArmLIR *storeMultiple(CompilationUnit *cUnit, int rBase, int rMask)
{
ArmLIR *res;
genBarrier(cUnit);
if (LOWREG(rBase) && ((rMask & 0xff)==rMask)) {
res = newLIR2(cUnit, kThumbStmia, rBase, rMask);
} else {
res = newLIR2(cUnit, kThumb2Stmia, rBase, rMask);
}
#if defined(WITH_SELF_VERIFICATION)
if (cUnit->heapMemOp)
res->flags.insertWrapper = true;
#endif
genBarrier(cUnit);
return res;
}
static void storePair(CompilationUnit *cUnit, int base, int lowReg, int highReg)
{
storeBaseDispWide(cUnit, base, 0, lowReg, highReg);
}
#ifndef WITH_QC_PERF
static void storePair(CompilationUnit *cUnit, int base, int displacement, int lowReg, int highReg)
{
storeBaseDispWide(cUnit, base, displacement, lowReg, highReg);
}
#endif
static void loadPair(CompilationUnit *cUnit, int base, int lowReg, int highReg)
{
loadBaseDispWide(cUnit, NULL, base, 0, lowReg, highReg, INVALID_SREG);
}
#ifndef WITH_QC_PERF
static void loadPair(CompilationUnit *cUnit, int base, int displacement, int lowReg, int highReg)
{
loadBaseDispWide(cUnit, NULL, base, displacement, lowReg, highReg, INVALID_SREG);
}
#endif
/*
* Generate a register comparison to an immediate and branch. Caller
* is responsible for setting branch target field.
*/
static ArmLIR *genCmpImmBranch(CompilationUnit *cUnit,
ArmConditionCode cond, int reg,
int checkValue)
{
ArmLIR *branch;
int modImm;
if ((LOWREG(reg)) && (checkValue == 0) &&
((cond == kArmCondEq) || (cond == kArmCondNe))) {
branch = newLIR2(cUnit,
(cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
reg, 0);
} else {
modImm = modifiedImmediate(checkValue);
if (LOWREG(reg) && ((checkValue & 0xff) == checkValue)) {
newLIR2(cUnit, kThumbCmpRI8, reg, checkValue);
} else if (modImm >= 0) {
newLIR2(cUnit, kThumb2CmpRI8, reg, modImm);
} else {
int tReg = dvmCompilerAllocTemp(cUnit);
loadConstant(cUnit, tReg, checkValue);
opRegReg(cUnit, kOpCmp, reg, tReg);
}
branch = newLIR2(cUnit, kThumbBCond, 0, cond);
}
return branch;
}
static ArmLIR *fpRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
{
ArmLIR* res = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
res->operands[0] = rDest;
res->operands[1] = rSrc;
if (rDest == rSrc) {
res->flags.isNop = true;
} else {
assert(DOUBLEREG(rDest) == DOUBLEREG(rSrc));
if (DOUBLEREG(rDest)) {
res->opcode = kThumb2Vmovd;
} else {
if (SINGLEREG(rDest)) {
res->opcode = SINGLEREG(rSrc) ? kThumb2Vmovs : kThumb2Fmsr;
} else {
assert(SINGLEREG(rSrc));
res->opcode = kThumb2Fmrs;
}
}
res->operands[0] = rDest;
res->operands[1] = rSrc;
}
setupResourceMasks(res);
return res;
}
static ArmLIR* genRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
{
ArmLIR* res;
ArmOpcode opcode;
if (FPREG(rDest) || FPREG(rSrc))
return fpRegCopy(cUnit, rDest, rSrc);
res = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
if (LOWREG(rDest) && LOWREG(rSrc))
opcode = kThumb2MovRR;
else if (!LOWREG(rDest) && !LOWREG(rSrc))
opcode = kThumbMovRR_H2H;
else if (LOWREG(rDest))
opcode = kThumbMovRR_H2L;
else
opcode = kThumbMovRR_L2H;
res->operands[0] = rDest;
res->operands[1] = rSrc;
res->opcode = opcode;
setupResourceMasks(res);
if (rDest == rSrc) {
res->flags.isNop = true;
}
return res;
}
static ArmLIR* genRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
{
ArmLIR *res = genRegCopyNoInsert(cUnit, rDest, rSrc);
dvmCompilerAppendLIR(cUnit, (LIR*)res);
return res;
}
static void genRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
int srcLo, int srcHi)
{
bool destFP = FPREG(destLo) && FPREG(destHi);
bool srcFP = FPREG(srcLo) && FPREG(srcHi);
assert(FPREG(srcLo) == FPREG(srcHi));
assert(FPREG(destLo) == FPREG(destHi));
if (destFP) {
if (srcFP) {
genRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi));
} else {
newLIR3(cUnit, kThumb2Fmdrr, S2D(destLo, destHi), srcLo, srcHi);
}
} else {
if (srcFP) {
newLIR3(cUnit, kThumb2Fmrrd, destLo, destHi, S2D(srcLo, srcHi));
} else {
// Handle overlap
if (srcHi == destLo) {
genRegCopy(cUnit, destHi, srcHi);
genRegCopy(cUnit, destLo, srcLo);
} else {
genRegCopy(cUnit, destLo, srcLo);
genRegCopy(cUnit, destHi, srcHi);
}
}
}
}
#if defined(WITH_SELF_VERIFICATION)
static void genSelfVerificationPreBranch(CompilationUnit *cUnit,
ArmLIR *origLIR) {
ArmLIR *push = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
push->opcode = kThumbPush;
/* Thumb push can handle LR (encoded at bit 8) */
push->operands[0] = (1 << r5FP | 1 << 8);
setupResourceMasks(push);
dvmCompilerInsertLIRBefore((LIR *) origLIR, (LIR *) push);
}
static void genSelfVerificationPostBranch(CompilationUnit *cUnit,
ArmLIR *origLIR) {
ArmLIR *pop = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
/* Thumb pop cannot store into LR - use Thumb2 here */
pop->opcode = kThumb2Pop;
pop->operands[0] = (1 << r5FP | 1 << r14lr);
setupResourceMasks(pop);
dvmCompilerInsertLIRAfter((LIR *) origLIR, (LIR *) pop);
}
#endif
|