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
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
|
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "v8.h"
#if V8_TARGET_ARCH_ARM64
#include "cpu-profiler.h"
#include "unicode.h"
#include "log.h"
#include "code-stubs.h"
#include "regexp-stack.h"
#include "macro-assembler.h"
#include "regexp-macro-assembler.h"
#include "arm64/regexp-macro-assembler-arm64.h"
namespace v8 {
namespace internal {
#ifndef V8_INTERPRETED_REGEXP
/*
* This assembler uses the following register assignment convention:
* - w19 : Used to temporarely store a value before a call to C code.
* See CheckNotBackReferenceIgnoreCase.
* - x20 : Pointer to the current code object (Code*),
* it includes the heap object tag.
* - w21 : Current position in input, as negative offset from
* the end of the string. Please notice that this is
* the byte offset, not the character offset!
* - w22 : Currently loaded character. Must be loaded using
* LoadCurrentCharacter before using any of the dispatch methods.
* - x23 : Points to tip of backtrack stack.
* - w24 : Position of the first character minus one: non_position_value.
* Used to initialize capture registers.
* - x25 : Address at the end of the input string: input_end.
* Points to byte after last character in input.
* - x26 : Address at the start of the input string: input_start.
* - w27 : Where to start in the input string.
* - x28 : Output array pointer.
* - x29/fp : Frame pointer. Used to access arguments, local variables and
* RegExp registers.
* - x16/x17 : IP registers, used by assembler. Very volatile.
* - csp : Points to tip of C stack.
*
* - x0-x7 : Used as a cache to store 32 bit capture registers. These
* registers need to be retained every time a call to C code
* is done.
*
* The remaining registers are free for computations.
* Each call to a public method should retain this convention.
*
* The stack will have the following structure:
*
* Location Name Description
* (as referred to in
* the code)
*
* - fp[104] isolate Address of the current isolate.
* - fp[96] return_address Secondary link/return address
* used by an exit frame if this is a
* native call.
* ^^^ csp when called ^^^
* - fp[88] lr Return from the RegExp code.
* - fp[80] r29 Old frame pointer (CalleeSaved).
* - fp[0..72] r19-r28 Backup of CalleeSaved registers.
* - fp[-8] direct_call 1 => Direct call from JavaScript code.
* 0 => Call through the runtime system.
* - fp[-16] stack_base High end of the memory area to use as
* the backtracking stack.
* - fp[-24] output_size Output may fit multiple sets of matches.
* - fp[-32] input Handle containing the input string.
* - fp[-40] success_counter
* ^^^^^^^^^^^^^ From here and downwards we store 32 bit values ^^^^^^^^^^^^^
* - fp[-44] register N Capture registers initialized with
* - fp[-48] register N + 1 non_position_value.
* ... The first kNumCachedRegisters (N) registers
* ... are cached in x0 to x7.
* ... Only positions must be stored in the first
* - ... num_saved_registers_ registers.
* - ...
* - register N + num_registers - 1
* ^^^^^^^^^ csp ^^^^^^^^^
*
* The first num_saved_registers_ registers are initialized to point to
* "character -1" in the string (i.e., char_size() bytes before the first
* character of the string). The remaining registers start out as garbage.
*
* The data up to the return address must be placed there by the calling
* code and the remaining arguments are passed in registers, e.g. by calling the
* code entry as cast to a function with the signature:
* int (*match)(String* input,
* int start_offset,
* Address input_start,
* Address input_end,
* int* output,
* int output_size,
* Address stack_base,
* bool direct_call = false,
* Address secondary_return_address, // Only used by native call.
* Isolate* isolate)
* The call is performed by NativeRegExpMacroAssembler::Execute()
* (in regexp-macro-assembler.cc) via the CALL_GENERATED_REGEXP_CODE macro
* in arm64/simulator-arm64.h.
* When calling as a non-direct call (i.e., from C++ code), the return address
* area is overwritten with the LR register by the RegExp code. When doing a
* direct call from generated code, the return address is placed there by
* the calling code, as in a normal exit frame.
*/
#define __ ACCESS_MASM(masm_)
RegExpMacroAssemblerARM64::RegExpMacroAssemblerARM64(
Mode mode,
int registers_to_save,
Zone* zone)
: NativeRegExpMacroAssembler(zone),
masm_(new MacroAssembler(zone->isolate(), NULL, kRegExpCodeSize)),
mode_(mode),
num_registers_(registers_to_save),
num_saved_registers_(registers_to_save),
entry_label_(),
start_label_(),
success_label_(),
backtrack_label_(),
exit_label_() {
__ SetStackPointer(csp);
ASSERT_EQ(0, registers_to_save % 2);
// We can cache at most 16 W registers in x0-x7.
STATIC_ASSERT(kNumCachedRegisters <= 16);
STATIC_ASSERT((kNumCachedRegisters % 2) == 0);
__ B(&entry_label_); // We'll write the entry code later.
__ Bind(&start_label_); // And then continue from here.
}
RegExpMacroAssemblerARM64::~RegExpMacroAssemblerARM64() {
delete masm_;
// Unuse labels in case we throw away the assembler without calling GetCode.
entry_label_.Unuse();
start_label_.Unuse();
success_label_.Unuse();
backtrack_label_.Unuse();
exit_label_.Unuse();
check_preempt_label_.Unuse();
stack_overflow_label_.Unuse();
}
int RegExpMacroAssemblerARM64::stack_limit_slack() {
return RegExpStack::kStackLimitSlack;
}
void RegExpMacroAssemblerARM64::AdvanceCurrentPosition(int by) {
if (by != 0) {
__ Add(current_input_offset(),
current_input_offset(), by * char_size());
}
}
void RegExpMacroAssemblerARM64::AdvanceRegister(int reg, int by) {
ASSERT((reg >= 0) && (reg < num_registers_));
if (by != 0) {
Register to_advance;
RegisterState register_state = GetRegisterState(reg);
switch (register_state) {
case STACKED:
__ Ldr(w10, register_location(reg));
__ Add(w10, w10, by);
__ Str(w10, register_location(reg));
break;
case CACHED_LSW:
to_advance = GetCachedRegister(reg);
__ Add(to_advance, to_advance, by);
break;
case CACHED_MSW:
to_advance = GetCachedRegister(reg);
__ Add(to_advance, to_advance,
static_cast<int64_t>(by) << kWRegSizeInBits);
break;
default:
UNREACHABLE();
break;
}
}
}
void RegExpMacroAssemblerARM64::Backtrack() {
CheckPreemption();
Pop(w10);
__ Add(x10, code_pointer(), Operand(w10, UXTW));
__ Br(x10);
}
void RegExpMacroAssemblerARM64::Bind(Label* label) {
__ Bind(label);
}
void RegExpMacroAssemblerARM64::CheckCharacter(uint32_t c, Label* on_equal) {
CompareAndBranchOrBacktrack(current_character(), c, eq, on_equal);
}
void RegExpMacroAssemblerARM64::CheckCharacterGT(uc16 limit,
Label* on_greater) {
CompareAndBranchOrBacktrack(current_character(), limit, hi, on_greater);
}
void RegExpMacroAssemblerARM64::CheckAtStart(Label* on_at_start) {
Label not_at_start;
// Did we start the match at the start of the input string?
CompareAndBranchOrBacktrack(start_offset(), 0, ne, ¬_at_start);
// If we did, are we still at the start of the input string?
__ Add(x10, input_end(), Operand(current_input_offset(), SXTW));
__ Cmp(x10, input_start());
BranchOrBacktrack(eq, on_at_start);
__ Bind(¬_at_start);
}
void RegExpMacroAssemblerARM64::CheckNotAtStart(Label* on_not_at_start) {
// Did we start the match at the start of the input string?
CompareAndBranchOrBacktrack(start_offset(), 0, ne, on_not_at_start);
// If we did, are we still at the start of the input string?
__ Add(x10, input_end(), Operand(current_input_offset(), SXTW));
__ Cmp(x10, input_start());
BranchOrBacktrack(ne, on_not_at_start);
}
void RegExpMacroAssemblerARM64::CheckCharacterLT(uc16 limit, Label* on_less) {
CompareAndBranchOrBacktrack(current_character(), limit, lo, on_less);
}
void RegExpMacroAssemblerARM64::CheckCharacters(Vector<const uc16> str,
int cp_offset,
Label* on_failure,
bool check_end_of_string) {
// This method is only ever called from the cctests.
if (check_end_of_string) {
// Is last character of required match inside string.
CheckPosition(cp_offset + str.length() - 1, on_failure);
}
Register characters_address = x11;
__ Add(characters_address,
input_end(),
Operand(current_input_offset(), SXTW));
if (cp_offset != 0) {
__ Add(characters_address, characters_address, cp_offset * char_size());
}
for (int i = 0; i < str.length(); i++) {
if (mode_ == ASCII) {
__ Ldrb(w10, MemOperand(characters_address, 1, PostIndex));
ASSERT(str[i] <= String::kMaxOneByteCharCode);
} else {
__ Ldrh(w10, MemOperand(characters_address, 2, PostIndex));
}
CompareAndBranchOrBacktrack(w10, str[i], ne, on_failure);
}
}
void RegExpMacroAssemblerARM64::CheckGreedyLoop(Label* on_equal) {
__ Ldr(w10, MemOperand(backtrack_stackpointer()));
__ Cmp(current_input_offset(), w10);
__ Cset(x11, eq);
__ Add(backtrack_stackpointer(),
backtrack_stackpointer(), Operand(x11, LSL, kWRegSizeLog2));
BranchOrBacktrack(eq, on_equal);
}
void RegExpMacroAssemblerARM64::CheckNotBackReferenceIgnoreCase(
int start_reg,
Label* on_no_match) {
Label fallthrough;
Register capture_start_offset = w10;
// Save the capture length in a callee-saved register so it will
// be preserved if we call a C helper.
Register capture_length = w19;
ASSERT(kCalleeSaved.IncludesAliasOf(capture_length));
// Find length of back-referenced capture.
ASSERT((start_reg % 2) == 0);
if (start_reg < kNumCachedRegisters) {
__ Mov(capture_start_offset.X(), GetCachedRegister(start_reg));
__ Lsr(x11, GetCachedRegister(start_reg), kWRegSizeInBits);
} else {
__ Ldp(w11, capture_start_offset, capture_location(start_reg, x10));
}
__ Sub(capture_length, w11, capture_start_offset); // Length to check.
// Succeed on empty capture (including no capture).
__ Cbz(capture_length, &fallthrough);
// Check that there are enough characters left in the input.
__ Cmn(capture_length, current_input_offset());
BranchOrBacktrack(gt, on_no_match);
if (mode_ == ASCII) {
Label success;
Label fail;
Label loop_check;
Register capture_start_address = x12;
Register capture_end_addresss = x13;
Register current_position_address = x14;
__ Add(capture_start_address,
input_end(),
Operand(capture_start_offset, SXTW));
__ Add(capture_end_addresss,
capture_start_address,
Operand(capture_length, SXTW));
__ Add(current_position_address,
input_end(),
Operand(current_input_offset(), SXTW));
Label loop;
__ Bind(&loop);
__ Ldrb(w10, MemOperand(capture_start_address, 1, PostIndex));
__ Ldrb(w11, MemOperand(current_position_address, 1, PostIndex));
__ Cmp(w10, w11);
__ B(eq, &loop_check);
// Mismatch, try case-insensitive match (converting letters to lower-case).
__ Orr(w10, w10, 0x20); // Convert capture character to lower-case.
__ Orr(w11, w11, 0x20); // Also convert input character.
__ Cmp(w11, w10);
__ B(ne, &fail);
__ Sub(w10, w10, 'a');
__ Cmp(w10, 'z' - 'a'); // Is w10 a lowercase letter?
__ B(ls, &loop_check); // In range 'a'-'z'.
// Latin-1: Check for values in range [224,254] but not 247.
__ Sub(w10, w10, 224 - 'a');
__ Cmp(w10, 254 - 224);
__ Ccmp(w10, 247 - 224, ZFlag, ls); // Check for 247.
__ B(eq, &fail); // Weren't Latin-1 letters.
__ Bind(&loop_check);
__ Cmp(capture_start_address, capture_end_addresss);
__ B(lt, &loop);
__ B(&success);
__ Bind(&fail);
BranchOrBacktrack(al, on_no_match);
__ Bind(&success);
// Compute new value of character position after the matched part.
__ Sub(current_input_offset().X(), current_position_address, input_end());
if (masm_->emit_debug_code()) {
__ Cmp(current_input_offset().X(), Operand(current_input_offset(), SXTW));
__ Ccmp(current_input_offset(), 0, NoFlag, eq);
// The current input offset should be <= 0, and fit in a W register.
__ Check(le, kOffsetOutOfRange);
}
} else {
ASSERT(mode_ == UC16);
int argument_count = 4;
// The cached registers need to be retained.
CPURegList cached_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 7);
ASSERT((cached_registers.Count() * 2) == kNumCachedRegisters);
__ PushCPURegList(cached_registers);
// Put arguments into arguments registers.
// Parameters are
// x0: Address byte_offset1 - Address captured substring's start.
// x1: Address byte_offset2 - Address of current character position.
// w2: size_t byte_length - length of capture in bytes(!)
// x3: Isolate* isolate
// Address of start of capture.
__ Add(x0, input_end(), Operand(capture_start_offset, SXTW));
// Length of capture.
__ Mov(w2, capture_length);
// Address of current input position.
__ Add(x1, input_end(), Operand(current_input_offset(), SXTW));
// Isolate.
__ Mov(x3, ExternalReference::isolate_address(isolate()));
{
AllowExternalCallThatCantCauseGC scope(masm_);
ExternalReference function =
ExternalReference::re_case_insensitive_compare_uc16(isolate());
__ CallCFunction(function, argument_count);
}
// Check if function returned non-zero for success or zero for failure.
CompareAndBranchOrBacktrack(x0, 0, eq, on_no_match);
// On success, increment position by length of capture.
__ Add(current_input_offset(), current_input_offset(), capture_length);
// Reset the cached registers.
__ PopCPURegList(cached_registers);
}
__ Bind(&fallthrough);
}
void RegExpMacroAssemblerARM64::CheckNotBackReference(
int start_reg,
Label* on_no_match) {
Label fallthrough;
Register capture_start_address = x12;
Register capture_end_address = x13;
Register current_position_address = x14;
Register capture_length = w15;
// Find length of back-referenced capture.
ASSERT((start_reg % 2) == 0);
if (start_reg < kNumCachedRegisters) {
__ Mov(x10, GetCachedRegister(start_reg));
__ Lsr(x11, GetCachedRegister(start_reg), kWRegSizeInBits);
} else {
__ Ldp(w11, w10, capture_location(start_reg, x10));
}
__ Sub(capture_length, w11, w10); // Length to check.
// Succeed on empty capture (including no capture).
__ Cbz(capture_length, &fallthrough);
// Check that there are enough characters left in the input.
__ Cmn(capture_length, current_input_offset());
BranchOrBacktrack(gt, on_no_match);
// Compute pointers to match string and capture string
__ Add(capture_start_address, input_end(), Operand(w10, SXTW));
__ Add(capture_end_address,
capture_start_address,
Operand(capture_length, SXTW));
__ Add(current_position_address,
input_end(),
Operand(current_input_offset(), SXTW));
Label loop;
__ Bind(&loop);
if (mode_ == ASCII) {
__ Ldrb(w10, MemOperand(capture_start_address, 1, PostIndex));
__ Ldrb(w11, MemOperand(current_position_address, 1, PostIndex));
} else {
ASSERT(mode_ == UC16);
__ Ldrh(w10, MemOperand(capture_start_address, 2, PostIndex));
__ Ldrh(w11, MemOperand(current_position_address, 2, PostIndex));
}
__ Cmp(w10, w11);
BranchOrBacktrack(ne, on_no_match);
__ Cmp(capture_start_address, capture_end_address);
__ B(lt, &loop);
// Move current character position to position after match.
__ Sub(current_input_offset().X(), current_position_address, input_end());
if (masm_->emit_debug_code()) {
__ Cmp(current_input_offset().X(), Operand(current_input_offset(), SXTW));
__ Ccmp(current_input_offset(), 0, NoFlag, eq);
// The current input offset should be <= 0, and fit in a W register.
__ Check(le, kOffsetOutOfRange);
}
__ Bind(&fallthrough);
}
void RegExpMacroAssemblerARM64::CheckNotCharacter(unsigned c,
Label* on_not_equal) {
CompareAndBranchOrBacktrack(current_character(), c, ne, on_not_equal);
}
void RegExpMacroAssemblerARM64::CheckCharacterAfterAnd(uint32_t c,
uint32_t mask,
Label* on_equal) {
__ And(w10, current_character(), mask);
CompareAndBranchOrBacktrack(w10, c, eq, on_equal);
}
void RegExpMacroAssemblerARM64::CheckNotCharacterAfterAnd(unsigned c,
unsigned mask,
Label* on_not_equal) {
__ And(w10, current_character(), mask);
CompareAndBranchOrBacktrack(w10, c, ne, on_not_equal);
}
void RegExpMacroAssemblerARM64::CheckNotCharacterAfterMinusAnd(
uc16 c,
uc16 minus,
uc16 mask,
Label* on_not_equal) {
ASSERT(minus < String::kMaxUtf16CodeUnit);
__ Sub(w10, current_character(), minus);
__ And(w10, w10, mask);
CompareAndBranchOrBacktrack(w10, c, ne, on_not_equal);
}
void RegExpMacroAssemblerARM64::CheckCharacterInRange(
uc16 from,
uc16 to,
Label* on_in_range) {
__ Sub(w10, current_character(), from);
// Unsigned lower-or-same condition.
CompareAndBranchOrBacktrack(w10, to - from, ls, on_in_range);
}
void RegExpMacroAssemblerARM64::CheckCharacterNotInRange(
uc16 from,
uc16 to,
Label* on_not_in_range) {
__ Sub(w10, current_character(), from);
// Unsigned higher condition.
CompareAndBranchOrBacktrack(w10, to - from, hi, on_not_in_range);
}
void RegExpMacroAssemblerARM64::CheckBitInTable(
Handle<ByteArray> table,
Label* on_bit_set) {
__ Mov(x11, Operand(table));
if ((mode_ != ASCII) || (kTableMask != String::kMaxOneByteCharCode)) {
__ And(w10, current_character(), kTableMask);
__ Add(w10, w10, ByteArray::kHeaderSize - kHeapObjectTag);
} else {
__ Add(w10, current_character(), ByteArray::kHeaderSize - kHeapObjectTag);
}
__ Ldrb(w11, MemOperand(x11, w10, UXTW));
CompareAndBranchOrBacktrack(w11, 0, ne, on_bit_set);
}
bool RegExpMacroAssemblerARM64::CheckSpecialCharacterClass(uc16 type,
Label* on_no_match) {
// Range checks (c in min..max) are generally implemented by an unsigned
// (c - min) <= (max - min) check
switch (type) {
case 's':
// Match space-characters
if (mode_ == ASCII) {
// One byte space characters are '\t'..'\r', ' ' and \u00a0.
Label success;
// Check for ' ' or 0x00a0.
__ Cmp(current_character(), ' ');
__ Ccmp(current_character(), 0x00a0, ZFlag, ne);
__ B(eq, &success);
// Check range 0x09..0x0d.
__ Sub(w10, current_character(), '\t');
CompareAndBranchOrBacktrack(w10, '\r' - '\t', hi, on_no_match);
__ Bind(&success);
return true;
}
return false;
case 'S':
// The emitted code for generic character classes is good enough.
return false;
case 'd':
// Match ASCII digits ('0'..'9').
__ Sub(w10, current_character(), '0');
CompareAndBranchOrBacktrack(w10, '9' - '0', hi, on_no_match);
return true;
case 'D':
// Match ASCII non-digits.
__ Sub(w10, current_character(), '0');
CompareAndBranchOrBacktrack(w10, '9' - '0', ls, on_no_match);
return true;
case '.': {
// Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
// Here we emit the conditional branch only once at the end to make branch
// prediction more efficient, even though we could branch out of here
// as soon as a character matches.
__ Cmp(current_character(), 0x0a);
__ Ccmp(current_character(), 0x0d, ZFlag, ne);
if (mode_ == UC16) {
__ Sub(w10, current_character(), 0x2028);
// If the Z flag was set we clear the flags to force a branch.
__ Ccmp(w10, 0x2029 - 0x2028, NoFlag, ne);
// ls -> !((C==1) && (Z==0))
BranchOrBacktrack(ls, on_no_match);
} else {
BranchOrBacktrack(eq, on_no_match);
}
return true;
}
case 'n': {
// Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
// We have to check all 4 newline characters before emitting
// the conditional branch.
__ Cmp(current_character(), 0x0a);
__ Ccmp(current_character(), 0x0d, ZFlag, ne);
if (mode_ == UC16) {
__ Sub(w10, current_character(), 0x2028);
// If the Z flag was set we clear the flags to force a fall-through.
__ Ccmp(w10, 0x2029 - 0x2028, NoFlag, ne);
// hi -> (C==1) && (Z==0)
BranchOrBacktrack(hi, on_no_match);
} else {
BranchOrBacktrack(ne, on_no_match);
}
return true;
}
case 'w': {
if (mode_ != ASCII) {
// Table is 128 entries, so all ASCII characters can be tested.
CompareAndBranchOrBacktrack(current_character(), 'z', hi, on_no_match);
}
ExternalReference map = ExternalReference::re_word_character_map();
__ Mov(x10, map);
__ Ldrb(w10, MemOperand(x10, current_character(), UXTW));
CompareAndBranchOrBacktrack(w10, 0, eq, on_no_match);
return true;
}
case 'W': {
Label done;
if (mode_ != ASCII) {
// Table is 128 entries, so all ASCII characters can be tested.
__ Cmp(current_character(), 'z');
__ B(hi, &done);
}
ExternalReference map = ExternalReference::re_word_character_map();
__ Mov(x10, map);
__ Ldrb(w10, MemOperand(x10, current_character(), UXTW));
CompareAndBranchOrBacktrack(w10, 0, ne, on_no_match);
__ Bind(&done);
return true;
}
case '*':
// Match any character.
return true;
// No custom implementation (yet): s(UC16), S(UC16).
default:
return false;
}
}
void RegExpMacroAssemblerARM64::Fail() {
__ Mov(w0, FAILURE);
__ B(&exit_label_);
}
Handle<HeapObject> RegExpMacroAssemblerARM64::GetCode(Handle<String> source) {
Label return_w0;
// Finalize code - write the entry point code now we know how many
// registers we need.
// Entry code:
__ Bind(&entry_label_);
// Arguments on entry:
// x0: String* input
// x1: int start_offset
// x2: byte* input_start
// x3: byte* input_end
// x4: int* output array
// x5: int output array size
// x6: Address stack_base
// x7: int direct_call
// The stack pointer should be csp on entry.
// csp[8]: address of the current isolate
// csp[0]: secondary link/return address used by native call
// Tell the system that we have a stack frame. Because the type is MANUAL, no
// code is generated.
FrameScope scope(masm_, StackFrame::MANUAL);
// Push registers on the stack, only push the argument registers that we need.
CPURegList argument_registers(x0, x5, x6, x7);
CPURegList registers_to_retain = kCalleeSaved;
ASSERT(kCalleeSaved.Count() == 11);
registers_to_retain.Combine(lr);
ASSERT(csp.Is(__ StackPointer()));
__ PushCPURegList(registers_to_retain);
__ PushCPURegList(argument_registers);
// Set frame pointer in place.
__ Add(frame_pointer(), csp, argument_registers.Count() * kPointerSize);
// Initialize callee-saved registers.
__ Mov(start_offset(), w1);
__ Mov(input_start(), x2);
__ Mov(input_end(), x3);
__ Mov(output_array(), x4);
// Set the number of registers we will need to allocate, that is:
// - success_counter (X register)
// - (num_registers_ - kNumCachedRegisters) (W registers)
int num_wreg_to_allocate = num_registers_ - kNumCachedRegisters;
// Do not allocate registers on the stack if they can all be cached.
if (num_wreg_to_allocate < 0) { num_wreg_to_allocate = 0; }
// Make room for the success_counter.
num_wreg_to_allocate += 2;
// Make sure the stack alignment will be respected.
int alignment = masm_->ActivationFrameAlignment();
ASSERT_EQ(alignment % 16, 0);
int align_mask = (alignment / kWRegSize) - 1;
num_wreg_to_allocate = (num_wreg_to_allocate + align_mask) & ~align_mask;
// Check if we have space on the stack.
Label stack_limit_hit;
Label stack_ok;
ExternalReference stack_limit =
ExternalReference::address_of_stack_limit(isolate());
__ Mov(x10, stack_limit);
__ Ldr(x10, MemOperand(x10));
__ Subs(x10, csp, x10);
// Handle it if the stack pointer is already below the stack limit.
__ B(ls, &stack_limit_hit);
// Check if there is room for the variable number of registers above
// the stack limit.
__ Cmp(x10, num_wreg_to_allocate * kWRegSize);
__ B(hs, &stack_ok);
// Exit with OutOfMemory exception. There is not enough space on the stack
// for our working registers.
__ Mov(w0, EXCEPTION);
__ B(&return_w0);
__ Bind(&stack_limit_hit);
CallCheckStackGuardState(x10);
// If returned value is non-zero, we exit with the returned value as result.
__ Cbnz(w0, &return_w0);
__ Bind(&stack_ok);
// Allocate space on stack.
__ Claim(num_wreg_to_allocate, kWRegSize);
// Initialize success_counter with 0.
__ Str(wzr, MemOperand(frame_pointer(), kSuccessCounter));
// Find negative length (offset of start relative to end).
__ Sub(x10, input_start(), input_end());
if (masm_->emit_debug_code()) {
// Check that the input string length is < 2^30.
__ Neg(x11, x10);
__ Cmp(x11, (1<<30) - 1);
__ Check(ls, kInputStringTooLong);
}
__ Mov(current_input_offset(), w10);
// The non-position value is used as a clearing value for the
// capture registers, it corresponds to the position of the first character
// minus one.
__ Sub(non_position_value(), current_input_offset(), char_size());
__ Sub(non_position_value(), non_position_value(),
Operand(start_offset(), LSL, (mode_ == UC16) ? 1 : 0));
// We can store this value twice in an X register for initializing
// on-stack registers later.
__ Orr(twice_non_position_value(),
non_position_value().X(),
Operand(non_position_value().X(), LSL, kWRegSizeInBits));
// Initialize code pointer register.
__ Mov(code_pointer(), Operand(masm_->CodeObject()));
Label load_char_start_regexp, start_regexp;
// Load newline if index is at start, previous character otherwise.
__ Cbnz(start_offset(), &load_char_start_regexp);
__ Mov(current_character(), '\n');
__ B(&start_regexp);
// Global regexp restarts matching here.
__ Bind(&load_char_start_regexp);
// Load previous char as initial value of current character register.
LoadCurrentCharacterUnchecked(-1, 1);
__ Bind(&start_regexp);
// Initialize on-stack registers.
if (num_saved_registers_ > 0) {
ClearRegisters(0, num_saved_registers_ - 1);
}
// Initialize backtrack stack pointer.
__ Ldr(backtrack_stackpointer(), MemOperand(frame_pointer(), kStackBase));
// Execute
__ B(&start_label_);
if (backtrack_label_.is_linked()) {
__ Bind(&backtrack_label_);
Backtrack();
}
if (success_label_.is_linked()) {
Register first_capture_start = w15;
// Save captures when successful.
__ Bind(&success_label_);
if (num_saved_registers_ > 0) {
// V8 expects the output to be an int32_t array.
Register capture_start = w12;
Register capture_end = w13;
Register input_length = w14;
// Copy captures to output.
// Get string length.
__ Sub(x10, input_end(), input_start());
if (masm_->emit_debug_code()) {
// Check that the input string length is < 2^30.
__ Cmp(x10, (1<<30) - 1);
__ Check(ls, kInputStringTooLong);
}
// input_start has a start_offset offset on entry. We need to include
// it when computing the length of the whole string.
if (mode_ == UC16) {
__ Add(input_length, start_offset(), Operand(w10, LSR, 1));
} else {
__ Add(input_length, start_offset(), w10);
}
// Copy the results to the output array from the cached registers first.
for (int i = 0;
(i < num_saved_registers_) && (i < kNumCachedRegisters);
i += 2) {
__ Mov(capture_start.X(), GetCachedRegister(i));
__ Lsr(capture_end.X(), capture_start.X(), kWRegSizeInBits);
if ((i == 0) && global_with_zero_length_check()) {
// Keep capture start for the zero-length check later.
__ Mov(first_capture_start, capture_start);
}
// Offsets need to be relative to the start of the string.
if (mode_ == UC16) {
__ Add(capture_start, input_length, Operand(capture_start, ASR, 1));
__ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
} else {
__ Add(capture_start, input_length, capture_start);
__ Add(capture_end, input_length, capture_end);
}
// The output pointer advances for a possible global match.
__ Stp(capture_start,
capture_end,
MemOperand(output_array(), kPointerSize, PostIndex));
}
// Only carry on if there are more than kNumCachedRegisters capture
// registers.
int num_registers_left_on_stack =
num_saved_registers_ - kNumCachedRegisters;
if (num_registers_left_on_stack > 0) {
Register base = x10;
// There are always an even number of capture registers. A couple of
// registers determine one match with two offsets.
ASSERT_EQ(0, num_registers_left_on_stack % 2);
__ Add(base, frame_pointer(), kFirstCaptureOnStack);
// We can unroll the loop here, we should not unroll for less than 2
// registers.
STATIC_ASSERT(kNumRegistersToUnroll > 2);
if (num_registers_left_on_stack <= kNumRegistersToUnroll) {
for (int i = 0; i < num_registers_left_on_stack / 2; i++) {
__ Ldp(capture_end,
capture_start,
MemOperand(base, -kPointerSize, PostIndex));
if ((i == 0) && global_with_zero_length_check()) {
// Keep capture start for the zero-length check later.
__ Mov(first_capture_start, capture_start);
}
// Offsets need to be relative to the start of the string.
if (mode_ == UC16) {
__ Add(capture_start,
input_length,
Operand(capture_start, ASR, 1));
__ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
} else {
__ Add(capture_start, input_length, capture_start);
__ Add(capture_end, input_length, capture_end);
}
// The output pointer advances for a possible global match.
__ Stp(capture_start,
capture_end,
MemOperand(output_array(), kPointerSize, PostIndex));
}
} else {
Label loop, start;
__ Mov(x11, num_registers_left_on_stack);
__ Ldp(capture_end,
capture_start,
MemOperand(base, -kPointerSize, PostIndex));
if (global_with_zero_length_check()) {
__ Mov(first_capture_start, capture_start);
}
__ B(&start);
__ Bind(&loop);
__ Ldp(capture_end,
capture_start,
MemOperand(base, -kPointerSize, PostIndex));
__ Bind(&start);
if (mode_ == UC16) {
__ Add(capture_start, input_length, Operand(capture_start, ASR, 1));
__ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
} else {
__ Add(capture_start, input_length, capture_start);
__ Add(capture_end, input_length, capture_end);
}
// The output pointer advances for a possible global match.
__ Stp(capture_start,
capture_end,
MemOperand(output_array(), kPointerSize, PostIndex));
__ Sub(x11, x11, 2);
__ Cbnz(x11, &loop);
}
}
}
if (global()) {
Register success_counter = w0;
Register output_size = x10;
// Restart matching if the regular expression is flagged as global.
// Increment success counter.
__ Ldr(success_counter, MemOperand(frame_pointer(), kSuccessCounter));
__ Add(success_counter, success_counter, 1);
__ Str(success_counter, MemOperand(frame_pointer(), kSuccessCounter));
// Capture results have been stored, so the number of remaining global
// output registers is reduced by the number of stored captures.
__ Ldr(output_size, MemOperand(frame_pointer(), kOutputSize));
__ Sub(output_size, output_size, num_saved_registers_);
// Check whether we have enough room for another set of capture results.
__ Cmp(output_size, num_saved_registers_);
__ B(lt, &return_w0);
// The output pointer is already set to the next field in the output
// array.
// Update output size on the frame before we restart matching.
__ Str(output_size, MemOperand(frame_pointer(), kOutputSize));
if (global_with_zero_length_check()) {
// Special case for zero-length matches.
__ Cmp(current_input_offset(), first_capture_start);
// Not a zero-length match, restart.
__ B(ne, &load_char_start_regexp);
// Offset from the end is zero if we already reached the end.
__ Cbz(current_input_offset(), &return_w0);
// Advance current position after a zero-length match.
__ Add(current_input_offset(),
current_input_offset(),
Operand((mode_ == UC16) ? 2 : 1));
}
__ B(&load_char_start_regexp);
} else {
__ Mov(w0, SUCCESS);
}
}
if (exit_label_.is_linked()) {
// Exit and return w0
__ Bind(&exit_label_);
if (global()) {
__ Ldr(w0, MemOperand(frame_pointer(), kSuccessCounter));
}
}
__ Bind(&return_w0);
// Set stack pointer back to first register to retain
ASSERT(csp.Is(__ StackPointer()));
__ Mov(csp, fp);
// Restore registers.
__ PopCPURegList(registers_to_retain);
__ Ret();
Label exit_with_exception;
// Registers x0 to x7 are used to store the first captures, they need to be
// retained over calls to C++ code.
CPURegList cached_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 7);
ASSERT((cached_registers.Count() * 2) == kNumCachedRegisters);
if (check_preempt_label_.is_linked()) {
__ Bind(&check_preempt_label_);
SaveLinkRegister();
// The cached registers need to be retained.
__ PushCPURegList(cached_registers);
CallCheckStackGuardState(x10);
// Returning from the regexp code restores the stack (csp <- fp)
// so we don't need to drop the link register from it before exiting.
__ Cbnz(w0, &return_w0);
// Reset the cached registers.
__ PopCPURegList(cached_registers);
RestoreLinkRegister();
__ Ret();
}
if (stack_overflow_label_.is_linked()) {
__ Bind(&stack_overflow_label_);
SaveLinkRegister();
// The cached registers need to be retained.
__ PushCPURegList(cached_registers);
// Call GrowStack(backtrack_stackpointer(), &stack_base)
__ Mov(x2, ExternalReference::isolate_address(isolate()));
__ Add(x1, frame_pointer(), kStackBase);
__ Mov(x0, backtrack_stackpointer());
ExternalReference grow_stack =
ExternalReference::re_grow_stack(isolate());
__ CallCFunction(grow_stack, 3);
// If return NULL, we have failed to grow the stack, and
// must exit with a stack-overflow exception.
// Returning from the regexp code restores the stack (csp <- fp)
// so we don't need to drop the link register from it before exiting.
__ Cbz(w0, &exit_with_exception);
// Otherwise use return value as new stack pointer.
__ Mov(backtrack_stackpointer(), x0);
// Reset the cached registers.
__ PopCPURegList(cached_registers);
RestoreLinkRegister();
__ Ret();
}
if (exit_with_exception.is_linked()) {
__ Bind(&exit_with_exception);
__ Mov(w0, EXCEPTION);
__ B(&return_w0);
}
CodeDesc code_desc;
masm_->GetCode(&code_desc);
Handle<Code> code = isolate()->factory()->NewCode(
code_desc, Code::ComputeFlags(Code::REGEXP), masm_->CodeObject());
PROFILE(masm_->isolate(), RegExpCodeCreateEvent(*code, *source));
return Handle<HeapObject>::cast(code);
}
void RegExpMacroAssemblerARM64::GoTo(Label* to) {
BranchOrBacktrack(al, to);
}
void RegExpMacroAssemblerARM64::IfRegisterGE(int reg, int comparand,
Label* if_ge) {
Register to_compare = GetRegister(reg, w10);
CompareAndBranchOrBacktrack(to_compare, comparand, ge, if_ge);
}
void RegExpMacroAssemblerARM64::IfRegisterLT(int reg, int comparand,
Label* if_lt) {
Register to_compare = GetRegister(reg, w10);
CompareAndBranchOrBacktrack(to_compare, comparand, lt, if_lt);
}
void RegExpMacroAssemblerARM64::IfRegisterEqPos(int reg, Label* if_eq) {
Register to_compare = GetRegister(reg, w10);
__ Cmp(to_compare, current_input_offset());
BranchOrBacktrack(eq, if_eq);
}
RegExpMacroAssembler::IrregexpImplementation
RegExpMacroAssemblerARM64::Implementation() {
return kARM64Implementation;
}
void RegExpMacroAssemblerARM64::LoadCurrentCharacter(int cp_offset,
Label* on_end_of_input,
bool check_bounds,
int characters) {
// TODO(pielan): Make sure long strings are caught before this, and not
// just asserted in debug mode.
ASSERT(cp_offset >= -1); // ^ and \b can look behind one character.
// Be sane! (And ensure that an int32_t can be used to index the string)
ASSERT(cp_offset < (1<<30));
if (check_bounds) {
CheckPosition(cp_offset + characters - 1, on_end_of_input);
}
LoadCurrentCharacterUnchecked(cp_offset, characters);
}
void RegExpMacroAssemblerARM64::PopCurrentPosition() {
Pop(current_input_offset());
}
void RegExpMacroAssemblerARM64::PopRegister(int register_index) {
Pop(w10);
StoreRegister(register_index, w10);
}
void RegExpMacroAssemblerARM64::PushBacktrack(Label* label) {
if (label->is_bound()) {
int target = label->pos();
__ Mov(w10, target + Code::kHeaderSize - kHeapObjectTag);
} else {
__ Adr(x10, label, MacroAssembler::kAdrFar);
__ Sub(x10, x10, code_pointer());
if (masm_->emit_debug_code()) {
__ Cmp(x10, kWRegMask);
// The code offset has to fit in a W register.
__ Check(ls, kOffsetOutOfRange);
}
}
Push(w10);
CheckStackLimit();
}
void RegExpMacroAssemblerARM64::PushCurrentPosition() {
Push(current_input_offset());
}
void RegExpMacroAssemblerARM64::PushRegister(int register_index,
StackCheckFlag check_stack_limit) {
Register to_push = GetRegister(register_index, w10);
Push(to_push);
if (check_stack_limit) CheckStackLimit();
}
void RegExpMacroAssemblerARM64::ReadCurrentPositionFromRegister(int reg) {
Register cached_register;
RegisterState register_state = GetRegisterState(reg);
switch (register_state) {
case STACKED:
__ Ldr(current_input_offset(), register_location(reg));
break;
case CACHED_LSW:
cached_register = GetCachedRegister(reg);
__ Mov(current_input_offset(), cached_register.W());
break;
case CACHED_MSW:
cached_register = GetCachedRegister(reg);
__ Lsr(current_input_offset().X(), cached_register, kWRegSizeInBits);
break;
default:
UNREACHABLE();
break;
}
}
void RegExpMacroAssemblerARM64::ReadStackPointerFromRegister(int reg) {
Register read_from = GetRegister(reg, w10);
__ Ldr(x11, MemOperand(frame_pointer(), kStackBase));
__ Add(backtrack_stackpointer(), x11, Operand(read_from, SXTW));
}
void RegExpMacroAssemblerARM64::SetCurrentPositionFromEnd(int by) {
Label after_position;
__ Cmp(current_input_offset(), -by * char_size());
__ B(ge, &after_position);
__ Mov(current_input_offset(), -by * char_size());
// On RegExp code entry (where this operation is used), the character before
// the current position is expected to be already loaded.
// We have advanced the position, so it's safe to read backwards.
LoadCurrentCharacterUnchecked(-1, 1);
__ Bind(&after_position);
}
void RegExpMacroAssemblerARM64::SetRegister(int register_index, int to) {
ASSERT(register_index >= num_saved_registers_); // Reserved for positions!
Register set_to = wzr;
if (to != 0) {
set_to = w10;
__ Mov(set_to, to);
}
StoreRegister(register_index, set_to);
}
bool RegExpMacroAssemblerARM64::Succeed() {
__ B(&success_label_);
return global();
}
void RegExpMacroAssemblerARM64::WriteCurrentPositionToRegister(int reg,
int cp_offset) {
Register position = current_input_offset();
if (cp_offset != 0) {
position = w10;
__ Add(position, current_input_offset(), cp_offset * char_size());
}
StoreRegister(reg, position);
}
void RegExpMacroAssemblerARM64::ClearRegisters(int reg_from, int reg_to) {
ASSERT(reg_from <= reg_to);
int num_registers = reg_to - reg_from + 1;
// If the first capture register is cached in a hardware register but not
// aligned on a 64-bit one, we need to clear the first one specifically.
if ((reg_from < kNumCachedRegisters) && ((reg_from % 2) != 0)) {
StoreRegister(reg_from, non_position_value());
num_registers--;
reg_from++;
}
// Clear cached registers in pairs as far as possible.
while ((num_registers >= 2) && (reg_from < kNumCachedRegisters)) {
ASSERT(GetRegisterState(reg_from) == CACHED_LSW);
__ Mov(GetCachedRegister(reg_from), twice_non_position_value());
reg_from += 2;
num_registers -= 2;
}
if ((num_registers % 2) == 1) {
StoreRegister(reg_from, non_position_value());
num_registers--;
reg_from++;
}
if (num_registers > 0) {
// If there are some remaining registers, they are stored on the stack.
ASSERT(reg_from >= kNumCachedRegisters);
// Move down the indexes of the registers on stack to get the correct offset
// in memory.
reg_from -= kNumCachedRegisters;
reg_to -= kNumCachedRegisters;
// We should not unroll the loop for less than 2 registers.
STATIC_ASSERT(kNumRegistersToUnroll > 2);
// We position the base pointer to (reg_from + 1).
int base_offset = kFirstRegisterOnStack -
kWRegSize - (kWRegSize * reg_from);
if (num_registers > kNumRegistersToUnroll) {
Register base = x10;
__ Add(base, frame_pointer(), base_offset);
Label loop;
__ Mov(x11, num_registers);
__ Bind(&loop);
__ Str(twice_non_position_value(),
MemOperand(base, -kPointerSize, PostIndex));
__ Sub(x11, x11, 2);
__ Cbnz(x11, &loop);
} else {
for (int i = reg_from; i <= reg_to; i += 2) {
__ Str(twice_non_position_value(),
MemOperand(frame_pointer(), base_offset));
base_offset -= kWRegSize * 2;
}
}
}
}
void RegExpMacroAssemblerARM64::WriteStackPointerToRegister(int reg) {
__ Ldr(x10, MemOperand(frame_pointer(), kStackBase));
__ Sub(x10, backtrack_stackpointer(), x10);
if (masm_->emit_debug_code()) {
__ Cmp(x10, Operand(w10, SXTW));
// The stack offset needs to fit in a W register.
__ Check(eq, kOffsetOutOfRange);
}
StoreRegister(reg, w10);
}
// Helper function for reading a value out of a stack frame.
template <typename T>
static T& frame_entry(Address re_frame, int frame_offset) {
return *reinterpret_cast<T*>(re_frame + frame_offset);
}
int RegExpMacroAssemblerARM64::CheckStackGuardState(Address* return_address,
Code* re_code,
Address re_frame,
int start_offset,
const byte** input_start,
const byte** input_end) {
Isolate* isolate = frame_entry<Isolate*>(re_frame, kIsolate);
if (isolate->stack_guard()->IsStackOverflow()) {
isolate->StackOverflow();
return EXCEPTION;
}
// If not real stack overflow the stack guard was used to interrupt
// execution for another purpose.
// If this is a direct call from JavaScript retry the RegExp forcing the call
// through the runtime system. Currently the direct call cannot handle a GC.
if (frame_entry<int>(re_frame, kDirectCall) == 1) {
return RETRY;
}
// Prepare for possible GC.
HandleScope handles(isolate);
Handle<Code> code_handle(re_code);
Handle<String> subject(frame_entry<String*>(re_frame, kInput));
// Current string.
bool is_ascii = subject->IsOneByteRepresentationUnderneath();
ASSERT(re_code->instruction_start() <= *return_address);
ASSERT(*return_address <=
re_code->instruction_start() + re_code->instruction_size());
Object* result = Execution::HandleStackGuardInterrupt(isolate);
if (*code_handle != re_code) { // Return address no longer valid
int delta = code_handle->address() - re_code->address();
// Overwrite the return address on the stack.
*return_address += delta;
}
if (result->IsException()) {
return EXCEPTION;
}
Handle<String> subject_tmp = subject;
int slice_offset = 0;
// Extract the underlying string and the slice offset.
if (StringShape(*subject_tmp).IsCons()) {
subject_tmp = Handle<String>(ConsString::cast(*subject_tmp)->first());
} else if (StringShape(*subject_tmp).IsSliced()) {
SlicedString* slice = SlicedString::cast(*subject_tmp);
subject_tmp = Handle<String>(slice->parent());
slice_offset = slice->offset();
}
// String might have changed.
if (subject_tmp->IsOneByteRepresentation() != is_ascii) {
// If we changed between an ASCII and an UC16 string, the specialized
// code cannot be used, and we need to restart regexp matching from
// scratch (including, potentially, compiling a new version of the code).
return RETRY;
}
// Otherwise, the content of the string might have moved. It must still
// be a sequential or external string with the same content.
// Update the start and end pointers in the stack frame to the current
// location (whether it has actually moved or not).
ASSERT(StringShape(*subject_tmp).IsSequential() ||
StringShape(*subject_tmp).IsExternal());
// The original start address of the characters to match.
const byte* start_address = *input_start;
// Find the current start address of the same character at the current string
// position.
const byte* new_address = StringCharacterPosition(*subject_tmp,
start_offset + slice_offset);
if (start_address != new_address) {
// If there is a difference, update the object pointer and start and end
// addresses in the RegExp stack frame to match the new value.
const byte* end_address = *input_end;
int byte_length = static_cast<int>(end_address - start_address);
frame_entry<const String*>(re_frame, kInput) = *subject;
*input_start = new_address;
*input_end = new_address + byte_length;
} else if (frame_entry<const String*>(re_frame, kInput) != *subject) {
// Subject string might have been a ConsString that underwent
// short-circuiting during GC. That will not change start_address but
// will change pointer inside the subject handle.
frame_entry<const String*>(re_frame, kInput) = *subject;
}
return 0;
}
void RegExpMacroAssemblerARM64::CheckPosition(int cp_offset,
Label* on_outside_input) {
CompareAndBranchOrBacktrack(current_input_offset(),
-cp_offset * char_size(),
ge,
on_outside_input);
}
bool RegExpMacroAssemblerARM64::CanReadUnaligned() {
// TODO(pielan): See whether or not we should disable unaligned accesses.
return !slow_safe();
}
// Private methods:
void RegExpMacroAssemblerARM64::CallCheckStackGuardState(Register scratch) {
// Allocate space on the stack to store the return address. The
// CheckStackGuardState C++ function will override it if the code
// moved. Allocate extra space for 2 arguments passed by pointers.
// AAPCS64 requires the stack to be 16 byte aligned.
int alignment = masm_->ActivationFrameAlignment();
ASSERT_EQ(alignment % 16, 0);
int align_mask = (alignment / kXRegSize) - 1;
int xreg_to_claim = (3 + align_mask) & ~align_mask;
ASSERT(csp.Is(__ StackPointer()));
__ Claim(xreg_to_claim);
// CheckStackGuardState needs the end and start addresses of the input string.
__ Poke(input_end(), 2 * kPointerSize);
__ Add(x5, csp, 2 * kPointerSize);
__ Poke(input_start(), kPointerSize);
__ Add(x4, csp, kPointerSize);
__ Mov(w3, start_offset());
// RegExp code frame pointer.
__ Mov(x2, frame_pointer());
// Code* of self.
__ Mov(x1, Operand(masm_->CodeObject()));
// We need to pass a pointer to the return address as first argument.
// The DirectCEntry stub will place the return address on the stack before
// calling so the stack pointer will point to it.
__ Mov(x0, csp);
ExternalReference check_stack_guard_state =
ExternalReference::re_check_stack_guard_state(isolate());
__ Mov(scratch, check_stack_guard_state);
DirectCEntryStub stub(isolate());
stub.GenerateCall(masm_, scratch);
// The input string may have been moved in memory, we need to reload it.
__ Peek(input_start(), kPointerSize);
__ Peek(input_end(), 2 * kPointerSize);
ASSERT(csp.Is(__ StackPointer()));
__ Drop(xreg_to_claim);
// Reload the Code pointer.
__ Mov(code_pointer(), Operand(masm_->CodeObject()));
}
void RegExpMacroAssemblerARM64::BranchOrBacktrack(Condition condition,
Label* to) {
if (condition == al) { // Unconditional.
if (to == NULL) {
Backtrack();
return;
}
__ B(to);
return;
}
if (to == NULL) {
to = &backtrack_label_;
}
// TODO(ulan): do direct jump when jump distance is known and fits in imm19.
Condition inverted_condition = InvertCondition(condition);
Label no_branch;
__ B(inverted_condition, &no_branch);
__ B(to);
__ Bind(&no_branch);
}
void RegExpMacroAssemblerARM64::CompareAndBranchOrBacktrack(Register reg,
int immediate,
Condition condition,
Label* to) {
if ((immediate == 0) && ((condition == eq) || (condition == ne))) {
if (to == NULL) {
to = &backtrack_label_;
}
// TODO(ulan): do direct jump when jump distance is known and fits in imm19.
Label no_branch;
if (condition == eq) {
__ Cbnz(reg, &no_branch);
} else {
__ Cbz(reg, &no_branch);
}
__ B(to);
__ Bind(&no_branch);
} else {
__ Cmp(reg, immediate);
BranchOrBacktrack(condition, to);
}
}
void RegExpMacroAssemblerARM64::CheckPreemption() {
// Check for preemption.
ExternalReference stack_limit =
ExternalReference::address_of_stack_limit(isolate());
__ Mov(x10, stack_limit);
__ Ldr(x10, MemOperand(x10));
ASSERT(csp.Is(__ StackPointer()));
__ Cmp(csp, x10);
CallIf(&check_preempt_label_, ls);
}
void RegExpMacroAssemblerARM64::CheckStackLimit() {
ExternalReference stack_limit =
ExternalReference::address_of_regexp_stack_limit(isolate());
__ Mov(x10, stack_limit);
__ Ldr(x10, MemOperand(x10));
__ Cmp(backtrack_stackpointer(), x10);
CallIf(&stack_overflow_label_, ls);
}
void RegExpMacroAssemblerARM64::Push(Register source) {
ASSERT(source.Is32Bits());
ASSERT(!source.is(backtrack_stackpointer()));
__ Str(source,
MemOperand(backtrack_stackpointer(),
-static_cast<int>(kWRegSize),
PreIndex));
}
void RegExpMacroAssemblerARM64::Pop(Register target) {
ASSERT(target.Is32Bits());
ASSERT(!target.is(backtrack_stackpointer()));
__ Ldr(target,
MemOperand(backtrack_stackpointer(), kWRegSize, PostIndex));
}
Register RegExpMacroAssemblerARM64::GetCachedRegister(int register_index) {
ASSERT(register_index < kNumCachedRegisters);
return Register::Create(register_index / 2, kXRegSizeInBits);
}
Register RegExpMacroAssemblerARM64::GetRegister(int register_index,
Register maybe_result) {
ASSERT(maybe_result.Is32Bits());
ASSERT(register_index >= 0);
if (num_registers_ <= register_index) {
num_registers_ = register_index + 1;
}
Register result;
RegisterState register_state = GetRegisterState(register_index);
switch (register_state) {
case STACKED:
__ Ldr(maybe_result, register_location(register_index));
result = maybe_result;
break;
case CACHED_LSW:
result = GetCachedRegister(register_index).W();
break;
case CACHED_MSW:
__ Lsr(maybe_result.X(), GetCachedRegister(register_index),
kWRegSizeInBits);
result = maybe_result;
break;
default:
UNREACHABLE();
break;
}
ASSERT(result.Is32Bits());
return result;
}
void RegExpMacroAssemblerARM64::StoreRegister(int register_index,
Register source) {
ASSERT(source.Is32Bits());
ASSERT(register_index >= 0);
if (num_registers_ <= register_index) {
num_registers_ = register_index + 1;
}
Register cached_register;
RegisterState register_state = GetRegisterState(register_index);
switch (register_state) {
case STACKED:
__ Str(source, register_location(register_index));
break;
case CACHED_LSW:
cached_register = GetCachedRegister(register_index);
if (!source.Is(cached_register.W())) {
__ Bfi(cached_register, source.X(), 0, kWRegSizeInBits);
}
break;
case CACHED_MSW:
cached_register = GetCachedRegister(register_index);
__ Bfi(cached_register, source.X(), kWRegSizeInBits, kWRegSizeInBits);
break;
default:
UNREACHABLE();
break;
}
}
void RegExpMacroAssemblerARM64::CallIf(Label* to, Condition condition) {
Label skip_call;
if (condition != al) __ B(&skip_call, InvertCondition(condition));
__ Bl(to);
__ Bind(&skip_call);
}
void RegExpMacroAssemblerARM64::RestoreLinkRegister() {
ASSERT(csp.Is(__ StackPointer()));
__ Pop(lr, xzr);
__ Add(lr, lr, Operand(masm_->CodeObject()));
}
void RegExpMacroAssemblerARM64::SaveLinkRegister() {
ASSERT(csp.Is(__ StackPointer()));
__ Sub(lr, lr, Operand(masm_->CodeObject()));
__ Push(xzr, lr);
}
MemOperand RegExpMacroAssemblerARM64::register_location(int register_index) {
ASSERT(register_index < (1<<30));
ASSERT(register_index >= kNumCachedRegisters);
if (num_registers_ <= register_index) {
num_registers_ = register_index + 1;
}
register_index -= kNumCachedRegisters;
int offset = kFirstRegisterOnStack - register_index * kWRegSize;
return MemOperand(frame_pointer(), offset);
}
MemOperand RegExpMacroAssemblerARM64::capture_location(int register_index,
Register scratch) {
ASSERT(register_index < (1<<30));
ASSERT(register_index < num_saved_registers_);
ASSERT(register_index >= kNumCachedRegisters);
ASSERT_EQ(register_index % 2, 0);
register_index -= kNumCachedRegisters;
int offset = kFirstCaptureOnStack - register_index * kWRegSize;
// capture_location is used with Stp instructions to load/store 2 registers.
// The immediate field in the encoding is limited to 7 bits (signed).
if (is_int7(offset)) {
return MemOperand(frame_pointer(), offset);
} else {
__ Add(scratch, frame_pointer(), offset);
return MemOperand(scratch);
}
}
void RegExpMacroAssemblerARM64::LoadCurrentCharacterUnchecked(int cp_offset,
int characters) {
Register offset = current_input_offset();
// The ldr, str, ldrh, strh instructions can do unaligned accesses, if the CPU
// and the operating system running on the target allow it.
// If unaligned load/stores are not supported then this function must only
// be used to load a single character at a time.
// ARMv8 supports unaligned accesses but V8 or the kernel can decide to
// disable it.
// TODO(pielan): See whether or not we should disable unaligned accesses.
if (!CanReadUnaligned()) {
ASSERT(characters == 1);
}
if (cp_offset != 0) {
if (masm_->emit_debug_code()) {
__ Mov(x10, cp_offset * char_size());
__ Add(x10, x10, Operand(current_input_offset(), SXTW));
__ Cmp(x10, Operand(w10, SXTW));
// The offset needs to fit in a W register.
__ Check(eq, kOffsetOutOfRange);
} else {
__ Add(w10, current_input_offset(), cp_offset * char_size());
}
offset = w10;
}
if (mode_ == ASCII) {
if (characters == 4) {
__ Ldr(current_character(), MemOperand(input_end(), offset, SXTW));
} else if (characters == 2) {
__ Ldrh(current_character(), MemOperand(input_end(), offset, SXTW));
} else {
ASSERT(characters == 1);
__ Ldrb(current_character(), MemOperand(input_end(), offset, SXTW));
}
} else {
ASSERT(mode_ == UC16);
if (characters == 2) {
__ Ldr(current_character(), MemOperand(input_end(), offset, SXTW));
} else {
ASSERT(characters == 1);
__ Ldrh(current_character(), MemOperand(input_end(), offset, SXTW));
}
}
}
#endif // V8_INTERPRETED_REGEXP
}} // namespace v8::internal
#endif // V8_TARGET_ARCH_ARM64
|