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
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
|
// Copyright 2012 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 "src/regexp/jsregexp.h"
#include <memory>
#include "src/base/platform/platform.h"
#include "src/compilation-cache.h"
#include "src/elements.h"
#include "src/execution.h"
#include "src/factory.h"
#include "src/isolate-inl.h"
#include "src/messages.h"
#include "src/ostreams.h"
#include "src/regexp/interpreter-irregexp.h"
#include "src/regexp/jsregexp-inl.h"
#include "src/regexp/regexp-macro-assembler-irregexp.h"
#include "src/regexp/regexp-macro-assembler-tracer.h"
#include "src/regexp/regexp-macro-assembler.h"
#include "src/regexp/regexp-parser.h"
#include "src/regexp/regexp-stack.h"
#include "src/runtime/runtime.h"
#include "src/splay-tree-inl.h"
#include "src/string-search.h"
#include "src/unicode-decoder.h"
#ifdef V8_I18N_SUPPORT
#include "unicode/uset.h"
#include "unicode/utypes.h"
#endif // V8_I18N_SUPPORT
#ifndef V8_INTERPRETED_REGEXP
#if V8_TARGET_ARCH_IA32
#include "src/regexp/ia32/regexp-macro-assembler-ia32.h"
#elif V8_TARGET_ARCH_X64
#include "src/regexp/x64/regexp-macro-assembler-x64.h"
#elif V8_TARGET_ARCH_ARM64
#include "src/regexp/arm64/regexp-macro-assembler-arm64.h"
#elif V8_TARGET_ARCH_ARM
#include "src/regexp/arm/regexp-macro-assembler-arm.h"
#elif V8_TARGET_ARCH_PPC
#include "src/regexp/ppc/regexp-macro-assembler-ppc.h"
#elif V8_TARGET_ARCH_S390
#include "src/regexp/s390/regexp-macro-assembler-s390.h"
#elif V8_TARGET_ARCH_MIPS
#include "src/regexp/mips/regexp-macro-assembler-mips.h"
#elif V8_TARGET_ARCH_MIPS64
#include "src/regexp/mips64/regexp-macro-assembler-mips64.h"
#elif V8_TARGET_ARCH_X87
#include "src/regexp/x87/regexp-macro-assembler-x87.h"
#else
#error Unsupported target architecture.
#endif
#endif
namespace v8 {
namespace internal {
MUST_USE_RESULT
static inline MaybeHandle<Object> ThrowRegExpException(
Handle<JSRegExp> re, Handle<String> pattern, Handle<String> error_text) {
Isolate* isolate = re->GetIsolate();
THROW_NEW_ERROR(isolate, NewSyntaxError(MessageTemplate::kMalformedRegExp,
pattern, error_text),
Object);
}
inline void ThrowRegExpException(Handle<JSRegExp> re,
Handle<String> error_text) {
USE(ThrowRegExpException(re, Handle<String>(re->Pattern()), error_text));
}
ContainedInLattice AddRange(ContainedInLattice containment,
const int* ranges,
int ranges_length,
Interval new_range) {
DCHECK((ranges_length & 1) == 1);
DCHECK(ranges[ranges_length - 1] == String::kMaxCodePoint + 1);
if (containment == kLatticeUnknown) return containment;
bool inside = false;
int last = 0;
for (int i = 0; i < ranges_length; inside = !inside, last = ranges[i], i++) {
// Consider the range from last to ranges[i].
// We haven't got to the new range yet.
if (ranges[i] <= new_range.from()) continue;
// New range is wholly inside last-ranges[i]. Note that new_range.to() is
// inclusive, but the values in ranges are not.
if (last <= new_range.from() && new_range.to() < ranges[i]) {
return Combine(containment, inside ? kLatticeIn : kLatticeOut);
}
return kLatticeUnknown;
}
return containment;
}
// More makes code generation slower, less makes V8 benchmark score lower.
const int kMaxLookaheadForBoyerMoore = 8;
// In a 3-character pattern you can maximally step forwards 3 characters
// at a time, which is not always enough to pay for the extra logic.
const int kPatternTooShortForBoyerMoore = 2;
// Identifies the sort of regexps where the regexp engine is faster
// than the code used for atom matches.
static bool HasFewDifferentCharacters(Handle<String> pattern) {
int length = Min(kMaxLookaheadForBoyerMoore, pattern->length());
if (length <= kPatternTooShortForBoyerMoore) return false;
const int kMod = 128;
bool character_found[kMod];
int different = 0;
memset(&character_found[0], 0, sizeof(character_found));
for (int i = 0; i < length; i++) {
int ch = (pattern->Get(i) & (kMod - 1));
if (!character_found[ch]) {
character_found[ch] = true;
different++;
// We declare a regexp low-alphabet if it has at least 3 times as many
// characters as it has different characters.
if (different * 3 > length) return false;
}
}
return true;
}
// Generic RegExp methods. Dispatches to implementation specific methods.
MaybeHandle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
Handle<String> pattern,
JSRegExp::Flags flags) {
Isolate* isolate = re->GetIsolate();
Zone zone(isolate->allocator());
CompilationCache* compilation_cache = isolate->compilation_cache();
MaybeHandle<FixedArray> maybe_cached =
compilation_cache->LookupRegExp(pattern, flags);
Handle<FixedArray> cached;
bool in_cache = maybe_cached.ToHandle(&cached);
LOG(isolate, RegExpCompileEvent(re, in_cache));
Handle<Object> result;
if (in_cache) {
re->set_data(*cached);
return re;
}
pattern = String::Flatten(pattern);
PostponeInterruptsScope postpone(isolate);
RegExpCompileData parse_result;
FlatStringReader reader(isolate, pattern);
if (!RegExpParser::ParseRegExp(re->GetIsolate(), &zone, &reader, flags,
&parse_result)) {
// Throw an exception if we fail to parse the pattern.
return ThrowRegExpException(re, pattern, parse_result.error);
}
bool has_been_compiled = false;
if (parse_result.simple && !(flags & JSRegExp::kIgnoreCase) &&
!(flags & JSRegExp::kSticky) && !HasFewDifferentCharacters(pattern)) {
// Parse-tree is a single atom that is equal to the pattern.
AtomCompile(re, pattern, flags, pattern);
has_been_compiled = true;
} else if (parse_result.tree->IsAtom() && !(flags & JSRegExp::kIgnoreCase) &&
!(flags & JSRegExp::kSticky) && parse_result.capture_count == 0) {
RegExpAtom* atom = parse_result.tree->AsAtom();
Vector<const uc16> atom_pattern = atom->data();
Handle<String> atom_string;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, atom_string,
isolate->factory()->NewStringFromTwoByte(atom_pattern),
Object);
if (!HasFewDifferentCharacters(atom_string)) {
AtomCompile(re, pattern, flags, atom_string);
has_been_compiled = true;
}
}
if (!has_been_compiled) {
IrregexpInitialize(re, pattern, flags, parse_result.capture_count);
}
DCHECK(re->data()->IsFixedArray());
// Compilation succeeded so the data is set on the regexp
// and we can store it in the cache.
Handle<FixedArray> data(FixedArray::cast(re->data()));
compilation_cache->PutRegExp(pattern, flags, data);
return re;
}
MaybeHandle<Object> RegExpImpl::Exec(Handle<JSRegExp> regexp,
Handle<String> subject, int index,
Handle<JSObject> last_match_info) {
switch (regexp->TypeTag()) {
case JSRegExp::ATOM:
return AtomExec(regexp, subject, index, last_match_info);
case JSRegExp::IRREGEXP: {
return IrregexpExec(regexp, subject, index, last_match_info);
}
default:
UNREACHABLE();
return MaybeHandle<Object>();
}
}
// RegExp Atom implementation: Simple string search using indexOf.
void RegExpImpl::AtomCompile(Handle<JSRegExp> re,
Handle<String> pattern,
JSRegExp::Flags flags,
Handle<String> match_pattern) {
re->GetIsolate()->factory()->SetRegExpAtomData(re,
JSRegExp::ATOM,
pattern,
flags,
match_pattern);
}
static void SetAtomLastCapture(FixedArray* array,
String* subject,
int from,
int to) {
SealHandleScope shs(array->GetIsolate());
RegExpImpl::SetLastCaptureCount(array, 2);
RegExpImpl::SetLastSubject(array, subject);
RegExpImpl::SetLastInput(array, subject);
RegExpImpl::SetCapture(array, 0, from);
RegExpImpl::SetCapture(array, 1, to);
}
int RegExpImpl::AtomExecRaw(Handle<JSRegExp> regexp,
Handle<String> subject,
int index,
int32_t* output,
int output_size) {
Isolate* isolate = regexp->GetIsolate();
DCHECK(0 <= index);
DCHECK(index <= subject->length());
subject = String::Flatten(subject);
DisallowHeapAllocation no_gc; // ensure vectors stay valid
String* needle = String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex));
int needle_len = needle->length();
DCHECK(needle->IsFlat());
DCHECK_LT(0, needle_len);
if (index + needle_len > subject->length()) {
return RegExpImpl::RE_FAILURE;
}
for (int i = 0; i < output_size; i += 2) {
String::FlatContent needle_content = needle->GetFlatContent();
String::FlatContent subject_content = subject->GetFlatContent();
DCHECK(needle_content.IsFlat());
DCHECK(subject_content.IsFlat());
// dispatch on type of strings
index =
(needle_content.IsOneByte()
? (subject_content.IsOneByte()
? SearchString(isolate, subject_content.ToOneByteVector(),
needle_content.ToOneByteVector(), index)
: SearchString(isolate, subject_content.ToUC16Vector(),
needle_content.ToOneByteVector(), index))
: (subject_content.IsOneByte()
? SearchString(isolate, subject_content.ToOneByteVector(),
needle_content.ToUC16Vector(), index)
: SearchString(isolate, subject_content.ToUC16Vector(),
needle_content.ToUC16Vector(), index)));
if (index == -1) {
return i / 2; // Return number of matches.
} else {
output[i] = index;
output[i+1] = index + needle_len;
index += needle_len;
}
}
return output_size / 2;
}
Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re, Handle<String> subject,
int index,
Handle<JSObject> last_match_info) {
Isolate* isolate = re->GetIsolate();
static const int kNumRegisters = 2;
STATIC_ASSERT(kNumRegisters <= Isolate::kJSRegexpStaticOffsetsVectorSize);
int32_t* output_registers = isolate->jsregexp_static_offsets_vector();
int res = AtomExecRaw(re, subject, index, output_registers, kNumRegisters);
if (res == RegExpImpl::RE_FAILURE) return isolate->factory()->null_value();
DCHECK_EQ(res, RegExpImpl::RE_SUCCESS);
SealHandleScope shs(isolate);
FixedArray* array = FixedArray::cast(last_match_info->elements());
SetAtomLastCapture(array, *subject, output_registers[0], output_registers[1]);
return last_match_info;
}
// Irregexp implementation.
// Ensures that the regexp object contains a compiled version of the
// source for either one-byte or two-byte subject strings.
// If the compiled version doesn't already exist, it is compiled
// from the source pattern.
// If compilation fails, an exception is thrown and this function
// returns false.
bool RegExpImpl::EnsureCompiledIrregexp(Handle<JSRegExp> re,
Handle<String> sample_subject,
bool is_one_byte) {
Object* compiled_code = re->DataAt(JSRegExp::code_index(is_one_byte));
#ifdef V8_INTERPRETED_REGEXP
if (compiled_code->IsByteArray()) return true;
#else // V8_INTERPRETED_REGEXP (RegExp native code)
if (compiled_code->IsCode()) return true;
#endif
// We could potentially have marked this as flushable, but have kept
// a saved version if we did not flush it yet.
Object* saved_code = re->DataAt(JSRegExp::saved_code_index(is_one_byte));
if (saved_code->IsCode()) {
// Reinstate the code in the original place.
re->SetDataAt(JSRegExp::code_index(is_one_byte), saved_code);
DCHECK(compiled_code->IsSmi());
return true;
}
return CompileIrregexp(re, sample_subject, is_one_byte);
}
bool RegExpImpl::CompileIrregexp(Handle<JSRegExp> re,
Handle<String> sample_subject,
bool is_one_byte) {
// Compile the RegExp.
Isolate* isolate = re->GetIsolate();
Zone zone(isolate->allocator());
PostponeInterruptsScope postpone(isolate);
// If we had a compilation error the last time this is saved at the
// saved code index.
Object* entry = re->DataAt(JSRegExp::code_index(is_one_byte));
// When arriving here entry can only be a smi, either representing an
// uncompiled regexp, a previous compilation error, or code that has
// been flushed.
DCHECK(entry->IsSmi());
int entry_value = Smi::cast(entry)->value();
DCHECK(entry_value == JSRegExp::kUninitializedValue ||
entry_value == JSRegExp::kCompilationErrorValue ||
(entry_value < JSRegExp::kCodeAgeMask && entry_value >= 0));
if (entry_value == JSRegExp::kCompilationErrorValue) {
// A previous compilation failed and threw an error which we store in
// the saved code index (we store the error message, not the actual
// error). Recreate the error object and throw it.
Object* error_string = re->DataAt(JSRegExp::saved_code_index(is_one_byte));
DCHECK(error_string->IsString());
Handle<String> error_message(String::cast(error_string));
ThrowRegExpException(re, error_message);
return false;
}
JSRegExp::Flags flags = re->GetFlags();
Handle<String> pattern(re->Pattern());
pattern = String::Flatten(pattern);
RegExpCompileData compile_data;
FlatStringReader reader(isolate, pattern);
if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, flags,
&compile_data)) {
// Throw an exception if we fail to parse the pattern.
// THIS SHOULD NOT HAPPEN. We already pre-parsed it successfully once.
USE(ThrowRegExpException(re, pattern, compile_data.error));
return false;
}
RegExpEngine::CompilationResult result =
RegExpEngine::Compile(isolate, &zone, &compile_data, flags, pattern,
sample_subject, is_one_byte);
if (result.error_message != NULL) {
// Unable to compile regexp.
Handle<String> error_message = isolate->factory()->NewStringFromUtf8(
CStrVector(result.error_message)).ToHandleChecked();
ThrowRegExpException(re, error_message);
return false;
}
Handle<FixedArray> data = Handle<FixedArray>(FixedArray::cast(re->data()));
data->set(JSRegExp::code_index(is_one_byte), result.code);
SetIrregexpCaptureNameMap(*data, compile_data.capture_name_map);
int register_max = IrregexpMaxRegisterCount(*data);
if (result.num_registers > register_max) {
SetIrregexpMaxRegisterCount(*data, result.num_registers);
}
return true;
}
int RegExpImpl::IrregexpMaxRegisterCount(FixedArray* re) {
return Smi::cast(
re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
}
void RegExpImpl::SetIrregexpMaxRegisterCount(FixedArray* re, int value) {
re->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(value));
}
void RegExpImpl::SetIrregexpCaptureNameMap(FixedArray* re,
Handle<FixedArray> value) {
if (value.is_null()) {
re->set(JSRegExp::kIrregexpCaptureNameMapIndex, Smi::FromInt(0));
} else {
re->set(JSRegExp::kIrregexpCaptureNameMapIndex, *value);
}
}
int RegExpImpl::IrregexpNumberOfCaptures(FixedArray* re) {
return Smi::cast(re->get(JSRegExp::kIrregexpCaptureCountIndex))->value();
}
int RegExpImpl::IrregexpNumberOfRegisters(FixedArray* re) {
return Smi::cast(re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
}
ByteArray* RegExpImpl::IrregexpByteCode(FixedArray* re, bool is_one_byte) {
return ByteArray::cast(re->get(JSRegExp::code_index(is_one_byte)));
}
Code* RegExpImpl::IrregexpNativeCode(FixedArray* re, bool is_one_byte) {
return Code::cast(re->get(JSRegExp::code_index(is_one_byte)));
}
void RegExpImpl::IrregexpInitialize(Handle<JSRegExp> re,
Handle<String> pattern,
JSRegExp::Flags flags,
int capture_count) {
// Initialize compiled code entries to null.
re->GetIsolate()->factory()->SetRegExpIrregexpData(re,
JSRegExp::IRREGEXP,
pattern,
flags,
capture_count);
}
int RegExpImpl::IrregexpPrepare(Handle<JSRegExp> regexp,
Handle<String> subject) {
subject = String::Flatten(subject);
// Check representation of the underlying storage.
bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
if (!EnsureCompiledIrregexp(regexp, subject, is_one_byte)) return -1;
#ifdef V8_INTERPRETED_REGEXP
// Byte-code regexp needs space allocated for all its registers.
// The result captures are copied to the start of the registers array
// if the match succeeds. This way those registers are not clobbered
// when we set the last match info from last successful match.
return IrregexpNumberOfRegisters(FixedArray::cast(regexp->data())) +
(IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2;
#else // V8_INTERPRETED_REGEXP
// Native regexp only needs room to output captures. Registers are handled
// internally.
return (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2;
#endif // V8_INTERPRETED_REGEXP
}
int RegExpImpl::IrregexpExecRaw(Handle<JSRegExp> regexp,
Handle<String> subject,
int index,
int32_t* output,
int output_size) {
Isolate* isolate = regexp->GetIsolate();
Handle<FixedArray> irregexp(FixedArray::cast(regexp->data()), isolate);
DCHECK(index >= 0);
DCHECK(index <= subject->length());
DCHECK(subject->IsFlat());
bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
#ifndef V8_INTERPRETED_REGEXP
DCHECK(output_size >= (IrregexpNumberOfCaptures(*irregexp) + 1) * 2);
do {
EnsureCompiledIrregexp(regexp, subject, is_one_byte);
Handle<Code> code(IrregexpNativeCode(*irregexp, is_one_byte), isolate);
// The stack is used to allocate registers for the compiled regexp code.
// This means that in case of failure, the output registers array is left
// untouched and contains the capture results from the previous successful
// match. We can use that to set the last match info lazily.
NativeRegExpMacroAssembler::Result res =
NativeRegExpMacroAssembler::Match(code,
subject,
output,
output_size,
index,
isolate);
if (res != NativeRegExpMacroAssembler::RETRY) {
DCHECK(res != NativeRegExpMacroAssembler::EXCEPTION ||
isolate->has_pending_exception());
STATIC_ASSERT(
static_cast<int>(NativeRegExpMacroAssembler::SUCCESS) == RE_SUCCESS);
STATIC_ASSERT(
static_cast<int>(NativeRegExpMacroAssembler::FAILURE) == RE_FAILURE);
STATIC_ASSERT(static_cast<int>(NativeRegExpMacroAssembler::EXCEPTION)
== RE_EXCEPTION);
return static_cast<IrregexpResult>(res);
}
// If result is RETRY, the string has changed representation, and we
// must restart from scratch.
// In this case, it means we must make sure we are prepared to handle
// the, potentially, different subject (the string can switch between
// being internal and external, and even between being Latin1 and UC16,
// but the characters are always the same).
IrregexpPrepare(regexp, subject);
is_one_byte = subject->IsOneByteRepresentationUnderneath();
} while (true);
UNREACHABLE();
return RE_EXCEPTION;
#else // V8_INTERPRETED_REGEXP
DCHECK(output_size >= IrregexpNumberOfRegisters(*irregexp));
// We must have done EnsureCompiledIrregexp, so we can get the number of
// registers.
int number_of_capture_registers =
(IrregexpNumberOfCaptures(*irregexp) + 1) * 2;
int32_t* raw_output = &output[number_of_capture_registers];
// We do not touch the actual capture result registers until we know there
// has been a match so that we can use those capture results to set the
// last match info.
for (int i = number_of_capture_registers - 1; i >= 0; i--) {
raw_output[i] = -1;
}
Handle<ByteArray> byte_codes(IrregexpByteCode(*irregexp, is_one_byte),
isolate);
IrregexpResult result = IrregexpInterpreter::Match(isolate,
byte_codes,
subject,
raw_output,
index);
if (result == RE_SUCCESS) {
// Copy capture results to the start of the registers array.
MemCopy(output, raw_output, number_of_capture_registers * sizeof(int32_t));
}
if (result == RE_EXCEPTION) {
DCHECK(!isolate->has_pending_exception());
isolate->StackOverflow();
}
return result;
#endif // V8_INTERPRETED_REGEXP
}
MaybeHandle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
Handle<String> subject,
int previous_index,
Handle<JSObject> last_match_info) {
Isolate* isolate = regexp->GetIsolate();
DCHECK_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
// Prepare space for the return values.
#if defined(V8_INTERPRETED_REGEXP) && defined(DEBUG)
if (FLAG_trace_regexp_bytecodes) {
String* pattern = regexp->Pattern();
PrintF("\n\nRegexp match: /%s/\n\n", pattern->ToCString().get());
PrintF("\n\nSubject string: '%s'\n\n", subject->ToCString().get());
}
#endif
int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject);
if (required_registers < 0) {
// Compiling failed with an exception.
DCHECK(isolate->has_pending_exception());
return MaybeHandle<Object>();
}
int32_t* output_registers = NULL;
if (required_registers > Isolate::kJSRegexpStaticOffsetsVectorSize) {
output_registers = NewArray<int32_t>(required_registers);
}
std::unique_ptr<int32_t[]> auto_release(output_registers);
if (output_registers == NULL) {
output_registers = isolate->jsregexp_static_offsets_vector();
}
int res = RegExpImpl::IrregexpExecRaw(
regexp, subject, previous_index, output_registers, required_registers);
if (res == RE_SUCCESS) {
int capture_count =
IrregexpNumberOfCaptures(FixedArray::cast(regexp->data()));
return SetLastMatchInfo(
last_match_info, subject, capture_count, output_registers);
}
if (res == RE_EXCEPTION) {
DCHECK(isolate->has_pending_exception());
return MaybeHandle<Object>();
}
DCHECK(res == RE_FAILURE);
return isolate->factory()->null_value();
}
static void EnsureSize(Handle<JSObject> array, uint32_t minimum_size) {
if (static_cast<uint32_t>(array->elements()->length()) < minimum_size) {
array->GetElementsAccessor()->GrowCapacityAndConvert(array, minimum_size);
}
}
Handle<JSObject> RegExpImpl::SetLastMatchInfo(Handle<JSObject> last_match_info,
Handle<String> subject,
int capture_count,
int32_t* match) {
DCHECK(last_match_info->HasFastObjectElements());
int capture_register_count = (capture_count + 1) * 2;
EnsureSize(last_match_info, capture_register_count + kLastMatchOverhead);
DisallowHeapAllocation no_allocation;
FixedArray* array = FixedArray::cast(last_match_info->elements());
if (match != NULL) {
for (int i = 0; i < capture_register_count; i += 2) {
SetCapture(array, i, match[i]);
SetCapture(array, i + 1, match[i + 1]);
}
}
SetLastCaptureCount(array, capture_register_count);
SetLastSubject(array, *subject);
SetLastInput(array, *subject);
return last_match_info;
}
RegExpImpl::GlobalCache::GlobalCache(Handle<JSRegExp> regexp,
Handle<String> subject,
Isolate* isolate)
: register_array_(NULL),
register_array_size_(0),
regexp_(regexp),
subject_(subject) {
#ifdef V8_INTERPRETED_REGEXP
bool interpreted = true;
#else
bool interpreted = false;
#endif // V8_INTERPRETED_REGEXP
if (regexp_->TypeTag() == JSRegExp::ATOM) {
static const int kAtomRegistersPerMatch = 2;
registers_per_match_ = kAtomRegistersPerMatch;
// There is no distinction between interpreted and native for atom regexps.
interpreted = false;
} else {
registers_per_match_ = RegExpImpl::IrregexpPrepare(regexp_, subject_);
if (registers_per_match_ < 0) {
num_matches_ = -1; // Signal exception.
return;
}
}
DCHECK_NE(0, regexp->GetFlags() & JSRegExp::kGlobal);
if (!interpreted) {
register_array_size_ =
Max(registers_per_match_, Isolate::kJSRegexpStaticOffsetsVectorSize);
max_matches_ = register_array_size_ / registers_per_match_;
} else {
// Global loop in interpreted regexp is not implemented. We choose
// the size of the offsets vector so that it can only store one match.
register_array_size_ = registers_per_match_;
max_matches_ = 1;
}
if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
register_array_ = NewArray<int32_t>(register_array_size_);
} else {
register_array_ = isolate->jsregexp_static_offsets_vector();
}
// Set state so that fetching the results the first time triggers a call
// to the compiled regexp.
current_match_index_ = max_matches_ - 1;
num_matches_ = max_matches_;
DCHECK(registers_per_match_ >= 2); // Each match has at least one capture.
DCHECK_GE(register_array_size_, registers_per_match_);
int32_t* last_match =
®ister_array_[current_match_index_ * registers_per_match_];
last_match[0] = -1;
last_match[1] = 0;
}
int RegExpImpl::GlobalCache::AdvanceZeroLength(int last_index) {
if ((regexp_->GetFlags() & JSRegExp::kUnicode) != 0 &&
last_index + 1 < subject_->length() &&
unibrow::Utf16::IsLeadSurrogate(subject_->Get(last_index)) &&
unibrow::Utf16::IsTrailSurrogate(subject_->Get(last_index + 1))) {
// Advance over the surrogate pair.
return last_index + 2;
}
return last_index + 1;
}
// -------------------------------------------------------------------
// Implementation of the Irregexp regular expression engine.
//
// The Irregexp regular expression engine is intended to be a complete
// implementation of ECMAScript regular expressions. It generates either
// bytecodes or native code.
// The Irregexp regexp engine is structured in three steps.
// 1) The parser generates an abstract syntax tree. See ast.cc.
// 2) From the AST a node network is created. The nodes are all
// subclasses of RegExpNode. The nodes represent states when
// executing a regular expression. Several optimizations are
// performed on the node network.
// 3) From the nodes we generate either byte codes or native code
// that can actually execute the regular expression (perform
// the search). The code generation step is described in more
// detail below.
// Code generation.
//
// The nodes are divided into four main categories.
// * Choice nodes
// These represent places where the regular expression can
// match in more than one way. For example on entry to an
// alternation (foo|bar) or a repetition (*, +, ? or {}).
// * Action nodes
// These represent places where some action should be
// performed. Examples include recording the current position
// in the input string to a register (in order to implement
// captures) or other actions on register for example in order
// to implement the counters needed for {} repetitions.
// * Matching nodes
// These attempt to match some element part of the input string.
// Examples of elements include character classes, plain strings
// or back references.
// * End nodes
// These are used to implement the actions required on finding
// a successful match or failing to find a match.
//
// The code generated (whether as byte codes or native code) maintains
// some state as it runs. This consists of the following elements:
//
// * The capture registers. Used for string captures.
// * Other registers. Used for counters etc.
// * The current position.
// * The stack of backtracking information. Used when a matching node
// fails to find a match and needs to try an alternative.
//
// Conceptual regular expression execution model:
//
// There is a simple conceptual model of regular expression execution
// which will be presented first. The actual code generated is a more
// efficient simulation of the simple conceptual model:
//
// * Choice nodes are implemented as follows:
// For each choice except the last {
// push current position
// push backtrack code location
// <generate code to test for choice>
// backtrack code location:
// pop current position
// }
// <generate code to test for last choice>
//
// * Actions nodes are generated as follows
// <push affected registers on backtrack stack>
// <generate code to perform action>
// push backtrack code location
// <generate code to test for following nodes>
// backtrack code location:
// <pop affected registers to restore their state>
// <pop backtrack location from stack and go to it>
//
// * Matching nodes are generated as follows:
// if input string matches at current position
// update current position
// <generate code to test for following nodes>
// else
// <pop backtrack location from stack and go to it>
//
// Thus it can be seen that the current position is saved and restored
// by the choice nodes, whereas the registers are saved and restored by
// by the action nodes that manipulate them.
//
// The other interesting aspect of this model is that nodes are generated
// at the point where they are needed by a recursive call to Emit(). If
// the node has already been code generated then the Emit() call will
// generate a jump to the previously generated code instead. In order to
// limit recursion it is possible for the Emit() function to put the node
// on a work list for later generation and instead generate a jump. The
// destination of the jump is resolved later when the code is generated.
//
// Actual regular expression code generation.
//
// Code generation is actually more complicated than the above. In order
// to improve the efficiency of the generated code some optimizations are
// performed
//
// * Choice nodes have 1-character lookahead.
// A choice node looks at the following character and eliminates some of
// the choices immediately based on that character. This is not yet
// implemented.
// * Simple greedy loops store reduced backtracking information.
// A quantifier like /.*foo/m will greedily match the whole input. It will
// then need to backtrack to a point where it can match "foo". The naive
// implementation of this would push each character position onto the
// backtracking stack, then pop them off one by one. This would use space
// proportional to the length of the input string. However since the "."
// can only match in one way and always has a constant length (in this case
// of 1) it suffices to store the current position on the top of the stack
// once. Matching now becomes merely incrementing the current position and
// backtracking becomes decrementing the current position and checking the
// result against the stored current position. This is faster and saves
// space.
// * The current state is virtualized.
// This is used to defer expensive operations until it is clear that they
// are needed and to generate code for a node more than once, allowing
// specialized an efficient versions of the code to be created. This is
// explained in the section below.
//
// Execution state virtualization.
//
// Instead of emitting code, nodes that manipulate the state can record their
// manipulation in an object called the Trace. The Trace object can record a
// current position offset, an optional backtrack code location on the top of
// the virtualized backtrack stack and some register changes. When a node is
// to be emitted it can flush the Trace or update it. Flushing the Trace
// will emit code to bring the actual state into line with the virtual state.
// Avoiding flushing the state can postpone some work (e.g. updates of capture
// registers). Postponing work can save time when executing the regular
// expression since it may be found that the work never has to be done as a
// failure to match can occur. In addition it is much faster to jump to a
// known backtrack code location than it is to pop an unknown backtrack
// location from the stack and jump there.
//
// The virtual state found in the Trace affects code generation. For example
// the virtual state contains the difference between the actual current
// position and the virtual current position, and matching code needs to use
// this offset to attempt a match in the correct location of the input
// string. Therefore code generated for a non-trivial trace is specialized
// to that trace. The code generator therefore has the ability to generate
// code for each node several times. In order to limit the size of the
// generated code there is an arbitrary limit on how many specialized sets of
// code may be generated for a given node. If the limit is reached, the
// trace is flushed and a generic version of the code for a node is emitted.
// This is subsequently used for that node. The code emitted for non-generic
// trace is not recorded in the node and so it cannot currently be reused in
// the event that code generation is requested for an identical trace.
void RegExpTree::AppendToText(RegExpText* text, Zone* zone) {
UNREACHABLE();
}
void RegExpAtom::AppendToText(RegExpText* text, Zone* zone) {
text->AddElement(TextElement::Atom(this), zone);
}
void RegExpCharacterClass::AppendToText(RegExpText* text, Zone* zone) {
text->AddElement(TextElement::CharClass(this), zone);
}
void RegExpText::AppendToText(RegExpText* text, Zone* zone) {
for (int i = 0; i < elements()->length(); i++)
text->AddElement(elements()->at(i), zone);
}
TextElement TextElement::Atom(RegExpAtom* atom) {
return TextElement(ATOM, atom);
}
TextElement TextElement::CharClass(RegExpCharacterClass* char_class) {
return TextElement(CHAR_CLASS, char_class);
}
int TextElement::length() const {
switch (text_type()) {
case ATOM:
return atom()->length();
case CHAR_CLASS:
return 1;
}
UNREACHABLE();
return 0;
}
DispatchTable* ChoiceNode::GetTable(bool ignore_case) {
if (table_ == NULL) {
table_ = new(zone()) DispatchTable(zone());
DispatchTableConstructor cons(table_, ignore_case, zone());
cons.BuildTable(this);
}
return table_;
}
class FrequencyCollator {
public:
FrequencyCollator() : total_samples_(0) {
for (int i = 0; i < RegExpMacroAssembler::kTableSize; i++) {
frequencies_[i] = CharacterFrequency(i);
}
}
void CountCharacter(int character) {
int index = (character & RegExpMacroAssembler::kTableMask);
frequencies_[index].Increment();
total_samples_++;
}
// Does not measure in percent, but rather per-128 (the table size from the
// regexp macro assembler).
int Frequency(int in_character) {
DCHECK((in_character & RegExpMacroAssembler::kTableMask) == in_character);
if (total_samples_ < 1) return 1; // Division by zero.
int freq_in_per128 =
(frequencies_[in_character].counter() * 128) / total_samples_;
return freq_in_per128;
}
private:
class CharacterFrequency {
public:
CharacterFrequency() : counter_(0), character_(-1) { }
explicit CharacterFrequency(int character)
: counter_(0), character_(character) { }
void Increment() { counter_++; }
int counter() { return counter_; }
int character() { return character_; }
private:
int counter_;
int character_;
};
private:
CharacterFrequency frequencies_[RegExpMacroAssembler::kTableSize];
int total_samples_;
};
class RegExpCompiler {
public:
RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count,
JSRegExp::Flags flags, bool is_one_byte);
int AllocateRegister() {
if (next_register_ >= RegExpMacroAssembler::kMaxRegister) {
reg_exp_too_big_ = true;
return next_register_;
}
return next_register_++;
}
// Lookarounds to match lone surrogates for unicode character class matches
// are never nested. We can therefore reuse registers.
int UnicodeLookaroundStackRegister() {
if (unicode_lookaround_stack_register_ == kNoRegister) {
unicode_lookaround_stack_register_ = AllocateRegister();
}
return unicode_lookaround_stack_register_;
}
int UnicodeLookaroundPositionRegister() {
if (unicode_lookaround_position_register_ == kNoRegister) {
unicode_lookaround_position_register_ = AllocateRegister();
}
return unicode_lookaround_position_register_;
}
RegExpEngine::CompilationResult Assemble(RegExpMacroAssembler* assembler,
RegExpNode* start,
int capture_count,
Handle<String> pattern);
inline void AddWork(RegExpNode* node) {
if (!node->on_work_list() && !node->label()->is_bound()) {
node->set_on_work_list(true);
work_list_->Add(node);
}
}
static const int kImplementationOffset = 0;
static const int kNumberOfRegistersOffset = 0;
static const int kCodeOffset = 1;
RegExpMacroAssembler* macro_assembler() { return macro_assembler_; }
EndNode* accept() { return accept_; }
static const int kMaxRecursion = 100;
inline int recursion_depth() { return recursion_depth_; }
inline void IncrementRecursionDepth() { recursion_depth_++; }
inline void DecrementRecursionDepth() { recursion_depth_--; }
void SetRegExpTooBig() { reg_exp_too_big_ = true; }
inline bool ignore_case() { return (flags_ & JSRegExp::kIgnoreCase) != 0; }
inline bool unicode() { return (flags_ & JSRegExp::kUnicode) != 0; }
inline bool one_byte() { return one_byte_; }
inline bool optimize() { return optimize_; }
inline void set_optimize(bool value) { optimize_ = value; }
inline bool limiting_recursion() { return limiting_recursion_; }
inline void set_limiting_recursion(bool value) {
limiting_recursion_ = value;
}
bool read_backward() { return read_backward_; }
void set_read_backward(bool value) { read_backward_ = value; }
FrequencyCollator* frequency_collator() { return &frequency_collator_; }
int current_expansion_factor() { return current_expansion_factor_; }
void set_current_expansion_factor(int value) {
current_expansion_factor_ = value;
}
Isolate* isolate() const { return isolate_; }
Zone* zone() const { return zone_; }
static const int kNoRegister = -1;
private:
EndNode* accept_;
int next_register_;
int unicode_lookaround_stack_register_;
int unicode_lookaround_position_register_;
List<RegExpNode*>* work_list_;
int recursion_depth_;
RegExpMacroAssembler* macro_assembler_;
JSRegExp::Flags flags_;
bool one_byte_;
bool reg_exp_too_big_;
bool limiting_recursion_;
bool optimize_;
bool read_backward_;
int current_expansion_factor_;
FrequencyCollator frequency_collator_;
Isolate* isolate_;
Zone* zone_;
};
class RecursionCheck {
public:
explicit RecursionCheck(RegExpCompiler* compiler) : compiler_(compiler) {
compiler->IncrementRecursionDepth();
}
~RecursionCheck() { compiler_->DecrementRecursionDepth(); }
private:
RegExpCompiler* compiler_;
};
static RegExpEngine::CompilationResult IrregexpRegExpTooBig(Isolate* isolate) {
return RegExpEngine::CompilationResult(isolate, "RegExp too big");
}
// Attempts to compile the regexp using an Irregexp code generator. Returns
// a fixed array or a null handle depending on whether it succeeded.
RegExpCompiler::RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count,
JSRegExp::Flags flags, bool one_byte)
: next_register_(2 * (capture_count + 1)),
unicode_lookaround_stack_register_(kNoRegister),
unicode_lookaround_position_register_(kNoRegister),
work_list_(NULL),
recursion_depth_(0),
flags_(flags),
one_byte_(one_byte),
reg_exp_too_big_(false),
limiting_recursion_(false),
optimize_(FLAG_regexp_optimization),
read_backward_(false),
current_expansion_factor_(1),
frequency_collator_(),
isolate_(isolate),
zone_(zone) {
accept_ = new(zone) EndNode(EndNode::ACCEPT, zone);
DCHECK(next_register_ - 1 <= RegExpMacroAssembler::kMaxRegister);
}
RegExpEngine::CompilationResult RegExpCompiler::Assemble(
RegExpMacroAssembler* macro_assembler,
RegExpNode* start,
int capture_count,
Handle<String> pattern) {
Heap* heap = pattern->GetHeap();
#ifdef DEBUG
if (FLAG_trace_regexp_assembler)
macro_assembler_ =
new RegExpMacroAssemblerTracer(isolate(), macro_assembler);
else
#endif
macro_assembler_ = macro_assembler;
List <RegExpNode*> work_list(0);
work_list_ = &work_list;
Label fail;
macro_assembler_->PushBacktrack(&fail);
Trace new_trace;
start->Emit(this, &new_trace);
macro_assembler_->Bind(&fail);
macro_assembler_->Fail();
while (!work_list.is_empty()) {
RegExpNode* node = work_list.RemoveLast();
node->set_on_work_list(false);
if (!node->label()->is_bound()) node->Emit(this, &new_trace);
}
if (reg_exp_too_big_) {
macro_assembler_->AbortedCodeGeneration();
return IrregexpRegExpTooBig(isolate_);
}
Handle<HeapObject> code = macro_assembler_->GetCode(pattern);
heap->IncreaseTotalRegexpCodeGenerated(code->Size());
work_list_ = NULL;
#ifdef ENABLE_DISASSEMBLER
if (FLAG_print_code) {
CodeTracer::Scope trace_scope(heap->isolate()->GetCodeTracer());
OFStream os(trace_scope.file());
Handle<Code>::cast(code)->Disassemble(pattern->ToCString().get(), os);
}
#endif
#ifdef DEBUG
if (FLAG_trace_regexp_assembler) {
delete macro_assembler_;
}
#endif
return RegExpEngine::CompilationResult(*code, next_register_);
}
bool Trace::DeferredAction::Mentions(int that) {
if (action_type() == ActionNode::CLEAR_CAPTURES) {
Interval range = static_cast<DeferredClearCaptures*>(this)->range();
return range.Contains(that);
} else {
return reg() == that;
}
}
bool Trace::mentions_reg(int reg) {
for (DeferredAction* action = actions_;
action != NULL;
action = action->next()) {
if (action->Mentions(reg))
return true;
}
return false;
}
bool Trace::GetStoredPosition(int reg, int* cp_offset) {
DCHECK_EQ(0, *cp_offset);
for (DeferredAction* action = actions_;
action != NULL;
action = action->next()) {
if (action->Mentions(reg)) {
if (action->action_type() == ActionNode::STORE_POSITION) {
*cp_offset = static_cast<DeferredCapture*>(action)->cp_offset();
return true;
} else {
return false;
}
}
}
return false;
}
int Trace::FindAffectedRegisters(OutSet* affected_registers,
Zone* zone) {
int max_register = RegExpCompiler::kNoRegister;
for (DeferredAction* action = actions_;
action != NULL;
action = action->next()) {
if (action->action_type() == ActionNode::CLEAR_CAPTURES) {
Interval range = static_cast<DeferredClearCaptures*>(action)->range();
for (int i = range.from(); i <= range.to(); i++)
affected_registers->Set(i, zone);
if (range.to() > max_register) max_register = range.to();
} else {
affected_registers->Set(action->reg(), zone);
if (action->reg() > max_register) max_register = action->reg();
}
}
return max_register;
}
void Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler,
int max_register,
const OutSet& registers_to_pop,
const OutSet& registers_to_clear) {
for (int reg = max_register; reg >= 0; reg--) {
if (registers_to_pop.Get(reg)) {
assembler->PopRegister(reg);
} else if (registers_to_clear.Get(reg)) {
int clear_to = reg;
while (reg > 0 && registers_to_clear.Get(reg - 1)) {
reg--;
}
assembler->ClearRegisters(reg, clear_to);
}
}
}
void Trace::PerformDeferredActions(RegExpMacroAssembler* assembler,
int max_register,
const OutSet& affected_registers,
OutSet* registers_to_pop,
OutSet* registers_to_clear,
Zone* zone) {
// The "+1" is to avoid a push_limit of zero if stack_limit_slack() is 1.
const int push_limit = (assembler->stack_limit_slack() + 1) / 2;
// Count pushes performed to force a stack limit check occasionally.
int pushes = 0;
for (int reg = 0; reg <= max_register; reg++) {
if (!affected_registers.Get(reg)) {
continue;
}
// The chronologically first deferred action in the trace
// is used to infer the action needed to restore a register
// to its previous state (or not, if it's safe to ignore it).
enum DeferredActionUndoType { IGNORE, RESTORE, CLEAR };
DeferredActionUndoType undo_action = IGNORE;
int value = 0;
bool absolute = false;
bool clear = false;
static const int kNoStore = kMinInt;
int store_position = kNoStore;
// This is a little tricky because we are scanning the actions in reverse
// historical order (newest first).
for (DeferredAction* action = actions_;
action != NULL;
action = action->next()) {
if (action->Mentions(reg)) {
switch (action->action_type()) {
case ActionNode::SET_REGISTER: {
Trace::DeferredSetRegister* psr =
static_cast<Trace::DeferredSetRegister*>(action);
if (!absolute) {
value += psr->value();
absolute = true;
}
// SET_REGISTER is currently only used for newly introduced loop
// counters. They can have a significant previous value if they
// occour in a loop. TODO(lrn): Propagate this information, so
// we can set undo_action to IGNORE if we know there is no value to
// restore.
undo_action = RESTORE;
DCHECK_EQ(store_position, kNoStore);
DCHECK(!clear);
break;
}
case ActionNode::INCREMENT_REGISTER:
if (!absolute) {
value++;
}
DCHECK_EQ(store_position, kNoStore);
DCHECK(!clear);
undo_action = RESTORE;
break;
case ActionNode::STORE_POSITION: {
Trace::DeferredCapture* pc =
static_cast<Trace::DeferredCapture*>(action);
if (!clear && store_position == kNoStore) {
store_position = pc->cp_offset();
}
// For captures we know that stores and clears alternate.
// Other register, are never cleared, and if the occur
// inside a loop, they might be assigned more than once.
if (reg <= 1) {
// Registers zero and one, aka "capture zero", is
// always set correctly if we succeed. There is no
// need to undo a setting on backtrack, because we
// will set it again or fail.
undo_action = IGNORE;
} else {
undo_action = pc->is_capture() ? CLEAR : RESTORE;
}
DCHECK(!absolute);
DCHECK_EQ(value, 0);
break;
}
case ActionNode::CLEAR_CAPTURES: {
// Since we're scanning in reverse order, if we've already
// set the position we have to ignore historically earlier
// clearing operations.
if (store_position == kNoStore) {
clear = true;
}
undo_action = RESTORE;
DCHECK(!absolute);
DCHECK_EQ(value, 0);
break;
}
default:
UNREACHABLE();
break;
}
}
}
// Prepare for the undo-action (e.g., push if it's going to be popped).
if (undo_action == RESTORE) {
pushes++;
RegExpMacroAssembler::StackCheckFlag stack_check =
RegExpMacroAssembler::kNoStackLimitCheck;
if (pushes == push_limit) {
stack_check = RegExpMacroAssembler::kCheckStackLimit;
pushes = 0;
}
assembler->PushRegister(reg, stack_check);
registers_to_pop->Set(reg, zone);
} else if (undo_action == CLEAR) {
registers_to_clear->Set(reg, zone);
}
// Perform the chronologically last action (or accumulated increment)
// for the register.
if (store_position != kNoStore) {
assembler->WriteCurrentPositionToRegister(reg, store_position);
} else if (clear) {
assembler->ClearRegisters(reg, reg);
} else if (absolute) {
assembler->SetRegister(reg, value);
} else if (value != 0) {
assembler->AdvanceRegister(reg, value);
}
}
}
// This is called as we come into a loop choice node and some other tricky
// nodes. It normalizes the state of the code generator to ensure we can
// generate generic code.
void Trace::Flush(RegExpCompiler* compiler, RegExpNode* successor) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
DCHECK(!is_trivial());
if (actions_ == NULL && backtrack() == NULL) {
// Here we just have some deferred cp advances to fix and we are back to
// a normal situation. We may also have to forget some information gained
// through a quick check that was already performed.
if (cp_offset_ != 0) assembler->AdvanceCurrentPosition(cp_offset_);
// Create a new trivial state and generate the node with that.
Trace new_state;
successor->Emit(compiler, &new_state);
return;
}
// Generate deferred actions here along with code to undo them again.
OutSet affected_registers;
if (backtrack() != NULL) {
// Here we have a concrete backtrack location. These are set up by choice
// nodes and so they indicate that we have a deferred save of the current
// position which we may need to emit here.
assembler->PushCurrentPosition();
}
int max_register = FindAffectedRegisters(&affected_registers,
compiler->zone());
OutSet registers_to_pop;
OutSet registers_to_clear;
PerformDeferredActions(assembler,
max_register,
affected_registers,
®isters_to_pop,
®isters_to_clear,
compiler->zone());
if (cp_offset_ != 0) {
assembler->AdvanceCurrentPosition(cp_offset_);
}
// Create a new trivial state and generate the node with that.
Label undo;
assembler->PushBacktrack(&undo);
if (successor->KeepRecursing(compiler)) {
Trace new_state;
successor->Emit(compiler, &new_state);
} else {
compiler->AddWork(successor);
assembler->GoTo(successor->label());
}
// On backtrack we need to restore state.
assembler->Bind(&undo);
RestoreAffectedRegisters(assembler,
max_register,
registers_to_pop,
registers_to_clear);
if (backtrack() == NULL) {
assembler->Backtrack();
} else {
assembler->PopCurrentPosition();
assembler->GoTo(backtrack());
}
}
void NegativeSubmatchSuccess::Emit(RegExpCompiler* compiler, Trace* trace) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
// Omit flushing the trace. We discard the entire stack frame anyway.
if (!label()->is_bound()) {
// We are completely independent of the trace, since we ignore it,
// so this code can be used as the generic version.
assembler->Bind(label());
}
// Throw away everything on the backtrack stack since the start
// of the negative submatch and restore the character position.
assembler->ReadCurrentPositionFromRegister(current_position_register_);
assembler->ReadStackPointerFromRegister(stack_pointer_register_);
if (clear_capture_count_ > 0) {
// Clear any captures that might have been performed during the success
// of the body of the negative look-ahead.
int clear_capture_end = clear_capture_start_ + clear_capture_count_ - 1;
assembler->ClearRegisters(clear_capture_start_, clear_capture_end);
}
// Now that we have unwound the stack we find at the top of the stack the
// backtrack that the BeginSubmatch node got.
assembler->Backtrack();
}
void EndNode::Emit(RegExpCompiler* compiler, Trace* trace) {
if (!trace->is_trivial()) {
trace->Flush(compiler, this);
return;
}
RegExpMacroAssembler* assembler = compiler->macro_assembler();
if (!label()->is_bound()) {
assembler->Bind(label());
}
switch (action_) {
case ACCEPT:
assembler->Succeed();
return;
case BACKTRACK:
assembler->GoTo(trace->backtrack());
return;
case NEGATIVE_SUBMATCH_SUCCESS:
// This case is handled in a different virtual method.
UNREACHABLE();
}
UNIMPLEMENTED();
}
void GuardedAlternative::AddGuard(Guard* guard, Zone* zone) {
if (guards_ == NULL)
guards_ = new(zone) ZoneList<Guard*>(1, zone);
guards_->Add(guard, zone);
}
ActionNode* ActionNode::SetRegister(int reg,
int val,
RegExpNode* on_success) {
ActionNode* result =
new(on_success->zone()) ActionNode(SET_REGISTER, on_success);
result->data_.u_store_register.reg = reg;
result->data_.u_store_register.value = val;
return result;
}
ActionNode* ActionNode::IncrementRegister(int reg, RegExpNode* on_success) {
ActionNode* result =
new(on_success->zone()) ActionNode(INCREMENT_REGISTER, on_success);
result->data_.u_increment_register.reg = reg;
return result;
}
ActionNode* ActionNode::StorePosition(int reg,
bool is_capture,
RegExpNode* on_success) {
ActionNode* result =
new(on_success->zone()) ActionNode(STORE_POSITION, on_success);
result->data_.u_position_register.reg = reg;
result->data_.u_position_register.is_capture = is_capture;
return result;
}
ActionNode* ActionNode::ClearCaptures(Interval range,
RegExpNode* on_success) {
ActionNode* result =
new(on_success->zone()) ActionNode(CLEAR_CAPTURES, on_success);
result->data_.u_clear_captures.range_from = range.from();
result->data_.u_clear_captures.range_to = range.to();
return result;
}
ActionNode* ActionNode::BeginSubmatch(int stack_reg,
int position_reg,
RegExpNode* on_success) {
ActionNode* result =
new(on_success->zone()) ActionNode(BEGIN_SUBMATCH, on_success);
result->data_.u_submatch.stack_pointer_register = stack_reg;
result->data_.u_submatch.current_position_register = position_reg;
return result;
}
ActionNode* ActionNode::PositiveSubmatchSuccess(int stack_reg,
int position_reg,
int clear_register_count,
int clear_register_from,
RegExpNode* on_success) {
ActionNode* result =
new(on_success->zone()) ActionNode(POSITIVE_SUBMATCH_SUCCESS, on_success);
result->data_.u_submatch.stack_pointer_register = stack_reg;
result->data_.u_submatch.current_position_register = position_reg;
result->data_.u_submatch.clear_register_count = clear_register_count;
result->data_.u_submatch.clear_register_from = clear_register_from;
return result;
}
ActionNode* ActionNode::EmptyMatchCheck(int start_register,
int repetition_register,
int repetition_limit,
RegExpNode* on_success) {
ActionNode* result =
new(on_success->zone()) ActionNode(EMPTY_MATCH_CHECK, on_success);
result->data_.u_empty_match_check.start_register = start_register;
result->data_.u_empty_match_check.repetition_register = repetition_register;
result->data_.u_empty_match_check.repetition_limit = repetition_limit;
return result;
}
#define DEFINE_ACCEPT(Type) \
void Type##Node::Accept(NodeVisitor* visitor) { \
visitor->Visit##Type(this); \
}
FOR_EACH_NODE_TYPE(DEFINE_ACCEPT)
#undef DEFINE_ACCEPT
void LoopChoiceNode::Accept(NodeVisitor* visitor) {
visitor->VisitLoopChoice(this);
}
// -------------------------------------------------------------------
// Emit code.
void ChoiceNode::GenerateGuard(RegExpMacroAssembler* macro_assembler,
Guard* guard,
Trace* trace) {
switch (guard->op()) {
case Guard::LT:
DCHECK(!trace->mentions_reg(guard->reg()));
macro_assembler->IfRegisterGE(guard->reg(),
guard->value(),
trace->backtrack());
break;
case Guard::GEQ:
DCHECK(!trace->mentions_reg(guard->reg()));
macro_assembler->IfRegisterLT(guard->reg(),
guard->value(),
trace->backtrack());
break;
}
}
// Returns the number of characters in the equivalence class, omitting those
// that cannot occur in the source string because it is Latin1.
static int GetCaseIndependentLetters(Isolate* isolate, uc16 character,
bool one_byte_subject,
unibrow::uchar* letters) {
int length =
isolate->jsregexp_uncanonicalize()->get(character, '\0', letters);
// Unibrow returns 0 or 1 for characters where case independence is
// trivial.
if (length == 0) {
letters[0] = character;
length = 1;
}
if (one_byte_subject) {
int new_length = 0;
for (int i = 0; i < length; i++) {
if (letters[i] <= String::kMaxOneByteCharCode) {
letters[new_length++] = letters[i];
}
}
length = new_length;
}
return length;
}
static inline bool EmitSimpleCharacter(Isolate* isolate,
RegExpCompiler* compiler,
uc16 c,
Label* on_failure,
int cp_offset,
bool check,
bool preloaded) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
bool bound_checked = false;
if (!preloaded) {
assembler->LoadCurrentCharacter(
cp_offset,
on_failure,
check);
bound_checked = true;
}
assembler->CheckNotCharacter(c, on_failure);
return bound_checked;
}
// Only emits non-letters (things that don't have case). Only used for case
// independent matches.
static inline bool EmitAtomNonLetter(Isolate* isolate,
RegExpCompiler* compiler,
uc16 c,
Label* on_failure,
int cp_offset,
bool check,
bool preloaded) {
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
bool one_byte = compiler->one_byte();
unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
int length = GetCaseIndependentLetters(isolate, c, one_byte, chars);
if (length < 1) {
// This can't match. Must be an one-byte subject and a non-one-byte
// character. We do not need to do anything since the one-byte pass
// already handled this.
return false; // Bounds not checked.
}
bool checked = false;
// We handle the length > 1 case in a later pass.
if (length == 1) {
if (one_byte && c > String::kMaxOneByteCharCodeU) {
// Can't match - see above.
return false; // Bounds not checked.
}
if (!preloaded) {
macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check);
checked = check;
}
macro_assembler->CheckNotCharacter(c, on_failure);
}
return checked;
}
static bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler,
bool one_byte, uc16 c1, uc16 c2,
Label* on_failure) {
uc16 char_mask;
if (one_byte) {
char_mask = String::kMaxOneByteCharCode;
} else {
char_mask = String::kMaxUtf16CodeUnit;
}
uc16 exor = c1 ^ c2;
// Check whether exor has only one bit set.
if (((exor - 1) & exor) == 0) {
// If c1 and c2 differ only by one bit.
// Ecma262UnCanonicalize always gives the highest number last.
DCHECK(c2 > c1);
uc16 mask = char_mask ^ exor;
macro_assembler->CheckNotCharacterAfterAnd(c1, mask, on_failure);
return true;
}
DCHECK(c2 > c1);
uc16 diff = c2 - c1;
if (((diff - 1) & diff) == 0 && c1 >= diff) {
// If the characters differ by 2^n but don't differ by one bit then
// subtract the difference from the found character, then do the or
// trick. We avoid the theoretical case where negative numbers are
// involved in order to simplify code generation.
uc16 mask = char_mask ^ diff;
macro_assembler->CheckNotCharacterAfterMinusAnd(c1 - diff,
diff,
mask,
on_failure);
return true;
}
return false;
}
typedef bool EmitCharacterFunction(Isolate* isolate,
RegExpCompiler* compiler,
uc16 c,
Label* on_failure,
int cp_offset,
bool check,
bool preloaded);
// Only emits letters (things that have case). Only used for case independent
// matches.
static inline bool EmitAtomLetter(Isolate* isolate,
RegExpCompiler* compiler,
uc16 c,
Label* on_failure,
int cp_offset,
bool check,
bool preloaded) {
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
bool one_byte = compiler->one_byte();
unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
int length = GetCaseIndependentLetters(isolate, c, one_byte, chars);
if (length <= 1) return false;
// We may not need to check against the end of the input string
// if this character lies before a character that matched.
if (!preloaded) {
macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check);
}
Label ok;
DCHECK(unibrow::Ecma262UnCanonicalize::kMaxWidth == 4);
switch (length) {
case 2: {
if (ShortCutEmitCharacterPair(macro_assembler, one_byte, chars[0],
chars[1], on_failure)) {
} else {
macro_assembler->CheckCharacter(chars[0], &ok);
macro_assembler->CheckNotCharacter(chars[1], on_failure);
macro_assembler->Bind(&ok);
}
break;
}
case 4:
macro_assembler->CheckCharacter(chars[3], &ok);
// Fall through!
case 3:
macro_assembler->CheckCharacter(chars[0], &ok);
macro_assembler->CheckCharacter(chars[1], &ok);
macro_assembler->CheckNotCharacter(chars[2], on_failure);
macro_assembler->Bind(&ok);
break;
default:
UNREACHABLE();
break;
}
return true;
}
static void EmitBoundaryTest(RegExpMacroAssembler* masm,
int border,
Label* fall_through,
Label* above_or_equal,
Label* below) {
if (below != fall_through) {
masm->CheckCharacterLT(border, below);
if (above_or_equal != fall_through) masm->GoTo(above_or_equal);
} else {
masm->CheckCharacterGT(border - 1, above_or_equal);
}
}
static void EmitDoubleBoundaryTest(RegExpMacroAssembler* masm,
int first,
int last,
Label* fall_through,
Label* in_range,
Label* out_of_range) {
if (in_range == fall_through) {
if (first == last) {
masm->CheckNotCharacter(first, out_of_range);
} else {
masm->CheckCharacterNotInRange(first, last, out_of_range);
}
} else {
if (first == last) {
masm->CheckCharacter(first, in_range);
} else {
masm->CheckCharacterInRange(first, last, in_range);
}
if (out_of_range != fall_through) masm->GoTo(out_of_range);
}
}
// even_label is for ranges[i] to ranges[i + 1] where i - start_index is even.
// odd_label is for ranges[i] to ranges[i + 1] where i - start_index is odd.
static void EmitUseLookupTable(
RegExpMacroAssembler* masm,
ZoneList<int>* ranges,
int start_index,
int end_index,
int min_char,
Label* fall_through,
Label* even_label,
Label* odd_label) {
static const int kSize = RegExpMacroAssembler::kTableSize;
static const int kMask = RegExpMacroAssembler::kTableMask;
int base = (min_char & ~kMask);
USE(base);
// Assert that everything is on one kTableSize page.
for (int i = start_index; i <= end_index; i++) {
DCHECK_EQ(ranges->at(i) & ~kMask, base);
}
DCHECK(start_index == 0 || (ranges->at(start_index - 1) & ~kMask) <= base);
char templ[kSize];
Label* on_bit_set;
Label* on_bit_clear;
int bit;
if (even_label == fall_through) {
on_bit_set = odd_label;
on_bit_clear = even_label;
bit = 1;
} else {
on_bit_set = even_label;
on_bit_clear = odd_label;
bit = 0;
}
for (int i = 0; i < (ranges->at(start_index) & kMask) && i < kSize; i++) {
templ[i] = bit;
}
int j = 0;
bit ^= 1;
for (int i = start_index; i < end_index; i++) {
for (j = (ranges->at(i) & kMask); j < (ranges->at(i + 1) & kMask); j++) {
templ[j] = bit;
}
bit ^= 1;
}
for (int i = j; i < kSize; i++) {
templ[i] = bit;
}
Factory* factory = masm->isolate()->factory();
// TODO(erikcorry): Cache these.
Handle<ByteArray> ba = factory->NewByteArray(kSize, TENURED);
for (int i = 0; i < kSize; i++) {
ba->set(i, templ[i]);
}
masm->CheckBitInTable(ba, on_bit_set);
if (on_bit_clear != fall_through) masm->GoTo(on_bit_clear);
}
static void CutOutRange(RegExpMacroAssembler* masm,
ZoneList<int>* ranges,
int start_index,
int end_index,
int cut_index,
Label* even_label,
Label* odd_label) {
bool odd = (((cut_index - start_index) & 1) == 1);
Label* in_range_label = odd ? odd_label : even_label;
Label dummy;
EmitDoubleBoundaryTest(masm,
ranges->at(cut_index),
ranges->at(cut_index + 1) - 1,
&dummy,
in_range_label,
&dummy);
DCHECK(!dummy.is_linked());
// Cut out the single range by rewriting the array. This creates a new
// range that is a merger of the two ranges on either side of the one we
// are cutting out. The oddity of the labels is preserved.
for (int j = cut_index; j > start_index; j--) {
ranges->at(j) = ranges->at(j - 1);
}
for (int j = cut_index + 1; j < end_index; j++) {
ranges->at(j) = ranges->at(j + 1);
}
}
// Unicode case. Split the search space into kSize spaces that are handled
// with recursion.
static void SplitSearchSpace(ZoneList<int>* ranges,
int start_index,
int end_index,
int* new_start_index,
int* new_end_index,
int* border) {
static const int kSize = RegExpMacroAssembler::kTableSize;
static const int kMask = RegExpMacroAssembler::kTableMask;
int first = ranges->at(start_index);
int last = ranges->at(end_index) - 1;
*new_start_index = start_index;
*border = (ranges->at(start_index) & ~kMask) + kSize;
while (*new_start_index < end_index) {
if (ranges->at(*new_start_index) > *border) break;
(*new_start_index)++;
}
// new_start_index is the index of the first edge that is beyond the
// current kSize space.
// For very large search spaces we do a binary chop search of the non-Latin1
// space instead of just going to the end of the current kSize space. The
// heuristics are complicated a little by the fact that any 128-character
// encoding space can be quickly tested with a table lookup, so we don't
// wish to do binary chop search at a smaller granularity than that. A
// 128-character space can take up a lot of space in the ranges array if,
// for example, we only want to match every second character (eg. the lower
// case characters on some Unicode pages).
int binary_chop_index = (end_index + start_index) / 2;
// The first test ensures that we get to the code that handles the Latin1
// range with a single not-taken branch, speeding up this important
// character range (even non-Latin1 charset-based text has spaces and
// punctuation).
if (*border - 1 > String::kMaxOneByteCharCode && // Latin1 case.
end_index - start_index > (*new_start_index - start_index) * 2 &&
last - first > kSize * 2 && binary_chop_index > *new_start_index &&
ranges->at(binary_chop_index) >= first + 2 * kSize) {
int scan_forward_for_section_border = binary_chop_index;;
int new_border = (ranges->at(binary_chop_index) | kMask) + 1;
while (scan_forward_for_section_border < end_index) {
if (ranges->at(scan_forward_for_section_border) > new_border) {
*new_start_index = scan_forward_for_section_border;
*border = new_border;
break;
}
scan_forward_for_section_border++;
}
}
DCHECK(*new_start_index > start_index);
*new_end_index = *new_start_index - 1;
if (ranges->at(*new_end_index) == *border) {
(*new_end_index)--;
}
if (*border >= ranges->at(end_index)) {
*border = ranges->at(end_index);
*new_start_index = end_index; // Won't be used.
*new_end_index = end_index - 1;
}
}
// Gets a series of segment boundaries representing a character class. If the
// character is in the range between an even and an odd boundary (counting from
// start_index) then go to even_label, otherwise go to odd_label. We already
// know that the character is in the range of min_char to max_char inclusive.
// Either label can be NULL indicating backtracking. Either label can also be
// equal to the fall_through label.
static void GenerateBranches(RegExpMacroAssembler* masm, ZoneList<int>* ranges,
int start_index, int end_index, uc32 min_char,
uc32 max_char, Label* fall_through,
Label* even_label, Label* odd_label) {
DCHECK_LE(min_char, String::kMaxUtf16CodeUnit);
DCHECK_LE(max_char, String::kMaxUtf16CodeUnit);
int first = ranges->at(start_index);
int last = ranges->at(end_index) - 1;
DCHECK_LT(min_char, first);
// Just need to test if the character is before or on-or-after
// a particular character.
if (start_index == end_index) {
EmitBoundaryTest(masm, first, fall_through, even_label, odd_label);
return;
}
// Another almost trivial case: There is one interval in the middle that is
// different from the end intervals.
if (start_index + 1 == end_index) {
EmitDoubleBoundaryTest(
masm, first, last, fall_through, even_label, odd_label);
return;
}
// It's not worth using table lookup if there are very few intervals in the
// character class.
if (end_index - start_index <= 6) {
// It is faster to test for individual characters, so we look for those
// first, then try arbitrary ranges in the second round.
static int kNoCutIndex = -1;
int cut = kNoCutIndex;
for (int i = start_index; i < end_index; i++) {
if (ranges->at(i) == ranges->at(i + 1) - 1) {
cut = i;
break;
}
}
if (cut == kNoCutIndex) cut = start_index;
CutOutRange(
masm, ranges, start_index, end_index, cut, even_label, odd_label);
DCHECK_GE(end_index - start_index, 2);
GenerateBranches(masm,
ranges,
start_index + 1,
end_index - 1,
min_char,
max_char,
fall_through,
even_label,
odd_label);
return;
}
// If there are a lot of intervals in the regexp, then we will use tables to
// determine whether the character is inside or outside the character class.
static const int kBits = RegExpMacroAssembler::kTableSizeBits;
if ((max_char >> kBits) == (min_char >> kBits)) {
EmitUseLookupTable(masm,
ranges,
start_index,
end_index,
min_char,
fall_through,
even_label,
odd_label);
return;
}
if ((min_char >> kBits) != (first >> kBits)) {
masm->CheckCharacterLT(first, odd_label);
GenerateBranches(masm,
ranges,
start_index + 1,
end_index,
first,
max_char,
fall_through,
odd_label,
even_label);
return;
}
int new_start_index = 0;
int new_end_index = 0;
int border = 0;
SplitSearchSpace(ranges,
start_index,
end_index,
&new_start_index,
&new_end_index,
&border);
Label handle_rest;
Label* above = &handle_rest;
if (border == last + 1) {
// We didn't find any section that started after the limit, so everything
// above the border is one of the terminal labels.
above = (end_index & 1) != (start_index & 1) ? odd_label : even_label;
DCHECK(new_end_index == end_index - 1);
}
DCHECK_LE(start_index, new_end_index);
DCHECK_LE(new_start_index, end_index);
DCHECK_LT(start_index, new_start_index);
DCHECK_LT(new_end_index, end_index);
DCHECK(new_end_index + 1 == new_start_index ||
(new_end_index + 2 == new_start_index &&
border == ranges->at(new_end_index + 1)));
DCHECK_LT(min_char, border - 1);
DCHECK_LT(border, max_char);
DCHECK_LT(ranges->at(new_end_index), border);
DCHECK(border < ranges->at(new_start_index) ||
(border == ranges->at(new_start_index) &&
new_start_index == end_index &&
new_end_index == end_index - 1 &&
border == last + 1));
DCHECK(new_start_index == 0 || border >= ranges->at(new_start_index - 1));
masm->CheckCharacterGT(border - 1, above);
Label dummy;
GenerateBranches(masm,
ranges,
start_index,
new_end_index,
min_char,
border - 1,
&dummy,
even_label,
odd_label);
if (handle_rest.is_linked()) {
masm->Bind(&handle_rest);
bool flip = (new_start_index & 1) != (start_index & 1);
GenerateBranches(masm,
ranges,
new_start_index,
end_index,
border,
max_char,
&dummy,
flip ? odd_label : even_label,
flip ? even_label : odd_label);
}
}
static void EmitCharClass(RegExpMacroAssembler* macro_assembler,
RegExpCharacterClass* cc, bool one_byte,
Label* on_failure, int cp_offset, bool check_offset,
bool preloaded, Zone* zone) {
ZoneList<CharacterRange>* ranges = cc->ranges(zone);
CharacterRange::Canonicalize(ranges);
int max_char;
if (one_byte) {
max_char = String::kMaxOneByteCharCode;
} else {
max_char = String::kMaxUtf16CodeUnit;
}
int range_count = ranges->length();
int last_valid_range = range_count - 1;
while (last_valid_range >= 0) {
CharacterRange& range = ranges->at(last_valid_range);
if (range.from() <= max_char) {
break;
}
last_valid_range--;
}
if (last_valid_range < 0) {
if (!cc->is_negated()) {
macro_assembler->GoTo(on_failure);
}
if (check_offset) {
macro_assembler->CheckPosition(cp_offset, on_failure);
}
return;
}
if (last_valid_range == 0 &&
ranges->at(0).IsEverything(max_char)) {
if (cc->is_negated()) {
macro_assembler->GoTo(on_failure);
} else {
// This is a common case hit by non-anchored expressions.
if (check_offset) {
macro_assembler->CheckPosition(cp_offset, on_failure);
}
}
return;
}
if (!preloaded) {
macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check_offset);
}
if (cc->is_standard(zone) &&
macro_assembler->CheckSpecialCharacterClass(cc->standard_type(),
on_failure)) {
return;
}
// A new list with ascending entries. Each entry is a code unit
// where there is a boundary between code units that are part of
// the class and code units that are not. Normally we insert an
// entry at zero which goes to the failure label, but if there
// was already one there we fall through for success on that entry.
// Subsequent entries have alternating meaning (success/failure).
ZoneList<int>* range_boundaries =
new(zone) ZoneList<int>(last_valid_range, zone);
bool zeroth_entry_is_failure = !cc->is_negated();
for (int i = 0; i <= last_valid_range; i++) {
CharacterRange& range = ranges->at(i);
if (range.from() == 0) {
DCHECK_EQ(i, 0);
zeroth_entry_is_failure = !zeroth_entry_is_failure;
} else {
range_boundaries->Add(range.from(), zone);
}
range_boundaries->Add(range.to() + 1, zone);
}
int end_index = range_boundaries->length() - 1;
if (range_boundaries->at(end_index) > max_char) {
end_index--;
}
Label fall_through;
GenerateBranches(macro_assembler,
range_boundaries,
0, // start_index.
end_index,
0, // min_char.
max_char,
&fall_through,
zeroth_entry_is_failure ? &fall_through : on_failure,
zeroth_entry_is_failure ? on_failure : &fall_through);
macro_assembler->Bind(&fall_through);
}
RegExpNode::~RegExpNode() {
}
RegExpNode::LimitResult RegExpNode::LimitVersions(RegExpCompiler* compiler,
Trace* trace) {
// If we are generating a greedy loop then don't stop and don't reuse code.
if (trace->stop_node() != NULL) {
return CONTINUE;
}
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
if (trace->is_trivial()) {
if (label_.is_bound() || on_work_list() || !KeepRecursing(compiler)) {
// If a generic version is already scheduled to be generated or we have
// recursed too deeply then just generate a jump to that code.
macro_assembler->GoTo(&label_);
// This will queue it up for generation of a generic version if it hasn't
// already been queued.
compiler->AddWork(this);
return DONE;
}
// Generate generic version of the node and bind the label for later use.
macro_assembler->Bind(&label_);
return CONTINUE;
}
// We are being asked to make a non-generic version. Keep track of how many
// non-generic versions we generate so as not to overdo it.
trace_count_++;
if (KeepRecursing(compiler) && compiler->optimize() &&
trace_count_ < kMaxCopiesCodeGenerated) {
return CONTINUE;
}
// If we get here code has been generated for this node too many times or
// recursion is too deep. Time to switch to a generic version. The code for
// generic versions above can handle deep recursion properly.
bool was_limiting = compiler->limiting_recursion();
compiler->set_limiting_recursion(true);
trace->Flush(compiler, this);
compiler->set_limiting_recursion(was_limiting);
return DONE;
}
bool RegExpNode::KeepRecursing(RegExpCompiler* compiler) {
return !compiler->limiting_recursion() &&
compiler->recursion_depth() <= RegExpCompiler::kMaxRecursion;
}
int ActionNode::EatsAtLeast(int still_to_find,
int budget,
bool not_at_start) {
if (budget <= 0) return 0;
if (action_type_ == POSITIVE_SUBMATCH_SUCCESS) return 0; // Rewinds input!
return on_success()->EatsAtLeast(still_to_find,
budget - 1,
not_at_start);
}
void ActionNode::FillInBMInfo(Isolate* isolate, int offset, int budget,
BoyerMooreLookahead* bm, bool not_at_start) {
if (action_type_ == BEGIN_SUBMATCH) {
bm->SetRest(offset);
} else if (action_type_ != POSITIVE_SUBMATCH_SUCCESS) {
on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start);
}
SaveBMInfo(bm, not_at_start, offset);
}
int AssertionNode::EatsAtLeast(int still_to_find,
int budget,
bool not_at_start) {
if (budget <= 0) return 0;
// If we know we are not at the start and we are asked "how many characters
// will you match if you succeed?" then we can answer anything since false
// implies false. So lets just return the max answer (still_to_find) since
// that won't prevent us from preloading a lot of characters for the other
// branches in the node graph.
if (assertion_type() == AT_START && not_at_start) return still_to_find;
return on_success()->EatsAtLeast(still_to_find,
budget - 1,
not_at_start);
}
void AssertionNode::FillInBMInfo(Isolate* isolate, int offset, int budget,
BoyerMooreLookahead* bm, bool not_at_start) {
// Match the behaviour of EatsAtLeast on this node.
if (assertion_type() == AT_START && not_at_start) return;
on_success()->FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start);
SaveBMInfo(bm, not_at_start, offset);
}
int BackReferenceNode::EatsAtLeast(int still_to_find,
int budget,
bool not_at_start) {
if (read_backward()) return 0;
if (budget <= 0) return 0;
return on_success()->EatsAtLeast(still_to_find,
budget - 1,
not_at_start);
}
int TextNode::EatsAtLeast(int still_to_find,
int budget,
bool not_at_start) {
if (read_backward()) return 0;
int answer = Length();
if (answer >= still_to_find) return answer;
if (budget <= 0) return answer;
// We are not at start after this node so we set the last argument to 'true'.
return answer + on_success()->EatsAtLeast(still_to_find - answer,
budget - 1,
true);
}
int NegativeLookaroundChoiceNode::EatsAtLeast(int still_to_find, int budget,
bool not_at_start) {
if (budget <= 0) return 0;
// Alternative 0 is the negative lookahead, alternative 1 is what comes
// afterwards.
RegExpNode* node = alternatives_->at(1).node();
return node->EatsAtLeast(still_to_find, budget - 1, not_at_start);
}
void NegativeLookaroundChoiceNode::GetQuickCheckDetails(
QuickCheckDetails* details, RegExpCompiler* compiler, int filled_in,
bool not_at_start) {
// Alternative 0 is the negative lookahead, alternative 1 is what comes
// afterwards.
RegExpNode* node = alternatives_->at(1).node();
return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start);
}
int ChoiceNode::EatsAtLeastHelper(int still_to_find,
int budget,
RegExpNode* ignore_this_node,
bool not_at_start) {
if (budget <= 0) return 0;
int min = 100;
int choice_count = alternatives_->length();
budget = (budget - 1) / choice_count;
for (int i = 0; i < choice_count; i++) {
RegExpNode* node = alternatives_->at(i).node();
if (node == ignore_this_node) continue;
int node_eats_at_least =
node->EatsAtLeast(still_to_find, budget, not_at_start);
if (node_eats_at_least < min) min = node_eats_at_least;
if (min == 0) return 0;
}
return min;
}
int LoopChoiceNode::EatsAtLeast(int still_to_find,
int budget,
bool not_at_start) {
return EatsAtLeastHelper(still_to_find,
budget - 1,
loop_node_,
not_at_start);
}
int ChoiceNode::EatsAtLeast(int still_to_find,
int budget,
bool not_at_start) {
return EatsAtLeastHelper(still_to_find,
budget,
NULL,
not_at_start);
}
// Takes the left-most 1-bit and smears it out, setting all bits to its right.
static inline uint32_t SmearBitsRight(uint32_t v) {
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return v;
}
bool QuickCheckDetails::Rationalize(bool asc) {
bool found_useful_op = false;
uint32_t char_mask;
if (asc) {
char_mask = String::kMaxOneByteCharCode;
} else {
char_mask = String::kMaxUtf16CodeUnit;
}
mask_ = 0;
value_ = 0;
int char_shift = 0;
for (int i = 0; i < characters_; i++) {
Position* pos = &positions_[i];
if ((pos->mask & String::kMaxOneByteCharCode) != 0) {
found_useful_op = true;
}
mask_ |= (pos->mask & char_mask) << char_shift;
value_ |= (pos->value & char_mask) << char_shift;
char_shift += asc ? 8 : 16;
}
return found_useful_op;
}
bool RegExpNode::EmitQuickCheck(RegExpCompiler* compiler,
Trace* bounds_check_trace,
Trace* trace,
bool preload_has_checked_bounds,
Label* on_possible_success,
QuickCheckDetails* details,
bool fall_through_on_failure) {
if (details->characters() == 0) return false;
GetQuickCheckDetails(
details, compiler, 0, trace->at_start() == Trace::FALSE_VALUE);
if (details->cannot_match()) return false;
if (!details->Rationalize(compiler->one_byte())) return false;
DCHECK(details->characters() == 1 ||
compiler->macro_assembler()->CanReadUnaligned());
uint32_t mask = details->mask();
uint32_t value = details->value();
RegExpMacroAssembler* assembler = compiler->macro_assembler();
if (trace->characters_preloaded() != details->characters()) {
DCHECK(trace->cp_offset() == bounds_check_trace->cp_offset());
// We are attempting to preload the minimum number of characters
// any choice would eat, so if the bounds check fails, then none of the
// choices can succeed, so we can just immediately backtrack, rather
// than go to the next choice.
assembler->LoadCurrentCharacter(trace->cp_offset(),
bounds_check_trace->backtrack(),
!preload_has_checked_bounds,
details->characters());
}
bool need_mask = true;
if (details->characters() == 1) {
// If number of characters preloaded is 1 then we used a byte or 16 bit
// load so the value is already masked down.
uint32_t char_mask;
if (compiler->one_byte()) {
char_mask = String::kMaxOneByteCharCode;
} else {
char_mask = String::kMaxUtf16CodeUnit;
}
if ((mask & char_mask) == char_mask) need_mask = false;
mask &= char_mask;
} else {
// For 2-character preloads in one-byte mode or 1-character preloads in
// two-byte mode we also use a 16 bit load with zero extend.
static const uint32_t kTwoByteMask = 0xffff;
static const uint32_t kFourByteMask = 0xffffffff;
if (details->characters() == 2 && compiler->one_byte()) {
if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false;
} else if (details->characters() == 1 && !compiler->one_byte()) {
if ((mask & kTwoByteMask) == kTwoByteMask) need_mask = false;
} else {
if (mask == kFourByteMask) need_mask = false;
}
}
if (fall_through_on_failure) {
if (need_mask) {
assembler->CheckCharacterAfterAnd(value, mask, on_possible_success);
} else {
assembler->CheckCharacter(value, on_possible_success);
}
} else {
if (need_mask) {
assembler->CheckNotCharacterAfterAnd(value, mask, trace->backtrack());
} else {
assembler->CheckNotCharacter(value, trace->backtrack());
}
}
return true;
}
// Here is the meat of GetQuickCheckDetails (see also the comment on the
// super-class in the .h file).
//
// We iterate along the text object, building up for each character a
// mask and value that can be used to test for a quick failure to match.
// The masks and values for the positions will be combined into a single
// machine word for the current character width in order to be used in
// generating a quick check.
void TextNode::GetQuickCheckDetails(QuickCheckDetails* details,
RegExpCompiler* compiler,
int characters_filled_in,
bool not_at_start) {
// Do not collect any quick check details if the text node reads backward,
// since it reads in the opposite direction than we use for quick checks.
if (read_backward()) return;
Isolate* isolate = compiler->macro_assembler()->isolate();
DCHECK(characters_filled_in < details->characters());
int characters = details->characters();
int char_mask;
if (compiler->one_byte()) {
char_mask = String::kMaxOneByteCharCode;
} else {
char_mask = String::kMaxUtf16CodeUnit;
}
for (int k = 0; k < elements()->length(); k++) {
TextElement elm = elements()->at(k);
if (elm.text_type() == TextElement::ATOM) {
Vector<const uc16> quarks = elm.atom()->data();
for (int i = 0; i < characters && i < quarks.length(); i++) {
QuickCheckDetails::Position* pos =
details->positions(characters_filled_in);
uc16 c = quarks[i];
if (compiler->ignore_case()) {
unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
int length = GetCaseIndependentLetters(isolate, c,
compiler->one_byte(), chars);
if (length == 0) {
// This can happen because all case variants are non-Latin1, but we
// know the input is Latin1.
details->set_cannot_match();
pos->determines_perfectly = false;
return;
}
if (length == 1) {
// This letter has no case equivalents, so it's nice and simple
// and the mask-compare will determine definitely whether we have
// a match at this character position.
pos->mask = char_mask;
pos->value = c;
pos->determines_perfectly = true;
} else {
uint32_t common_bits = char_mask;
uint32_t bits = chars[0];
for (int j = 1; j < length; j++) {
uint32_t differing_bits = ((chars[j] & common_bits) ^ bits);
common_bits ^= differing_bits;
bits &= common_bits;
}
// If length is 2 and common bits has only one zero in it then
// our mask and compare instruction will determine definitely
// whether we have a match at this character position. Otherwise
// it can only be an approximate check.
uint32_t one_zero = (common_bits | ~char_mask);
if (length == 2 && ((~one_zero) & ((~one_zero) - 1)) == 0) {
pos->determines_perfectly = true;
}
pos->mask = common_bits;
pos->value = bits;
}
} else {
// Don't ignore case. Nice simple case where the mask-compare will
// determine definitely whether we have a match at this character
// position.
if (c > char_mask) {
details->set_cannot_match();
pos->determines_perfectly = false;
return;
}
pos->mask = char_mask;
pos->value = c;
pos->determines_perfectly = true;
}
characters_filled_in++;
DCHECK(characters_filled_in <= details->characters());
if (characters_filled_in == details->characters()) {
return;
}
}
} else {
QuickCheckDetails::Position* pos =
details->positions(characters_filled_in);
RegExpCharacterClass* tree = elm.char_class();
ZoneList<CharacterRange>* ranges = tree->ranges(zone());
if (tree->is_negated()) {
// A quick check uses multi-character mask and compare. There is no
// useful way to incorporate a negative char class into this scheme
// so we just conservatively create a mask and value that will always
// succeed.
pos->mask = 0;
pos->value = 0;
} else {
int first_range = 0;
while (ranges->at(first_range).from() > char_mask) {
first_range++;
if (first_range == ranges->length()) {
details->set_cannot_match();
pos->determines_perfectly = false;
return;
}
}
CharacterRange range = ranges->at(first_range);
uc16 from = range.from();
uc16 to = range.to();
if (to > char_mask) {
to = char_mask;
}
uint32_t differing_bits = (from ^ to);
// A mask and compare is only perfect if the differing bits form a
// number like 00011111 with one single block of trailing 1s.
if ((differing_bits & (differing_bits + 1)) == 0 &&
from + differing_bits == to) {
pos->determines_perfectly = true;
}
uint32_t common_bits = ~SmearBitsRight(differing_bits);
uint32_t bits = (from & common_bits);
for (int i = first_range + 1; i < ranges->length(); i++) {
CharacterRange range = ranges->at(i);
uc16 from = range.from();
uc16 to = range.to();
if (from > char_mask) continue;
if (to > char_mask) to = char_mask;
// Here we are combining more ranges into the mask and compare
// value. With each new range the mask becomes more sparse and
// so the chances of a false positive rise. A character class
// with multiple ranges is assumed never to be equivalent to a
// mask and compare operation.
pos->determines_perfectly = false;
uint32_t new_common_bits = (from ^ to);
new_common_bits = ~SmearBitsRight(new_common_bits);
common_bits &= new_common_bits;
bits &= new_common_bits;
uint32_t differing_bits = (from & common_bits) ^ bits;
common_bits ^= differing_bits;
bits &= common_bits;
}
pos->mask = common_bits;
pos->value = bits;
}
characters_filled_in++;
DCHECK(characters_filled_in <= details->characters());
if (characters_filled_in == details->characters()) {
return;
}
}
}
DCHECK(characters_filled_in != details->characters());
if (!details->cannot_match()) {
on_success()-> GetQuickCheckDetails(details,
compiler,
characters_filled_in,
true);
}
}
void QuickCheckDetails::Clear() {
for (int i = 0; i < characters_; i++) {
positions_[i].mask = 0;
positions_[i].value = 0;
positions_[i].determines_perfectly = false;
}
characters_ = 0;
}
void QuickCheckDetails::Advance(int by, bool one_byte) {
if (by >= characters_ || by < 0) {
DCHECK_IMPLIES(by < 0, characters_ == 0);
Clear();
return;
}
DCHECK_LE(characters_ - by, 4);
DCHECK_LE(characters_, 4);
for (int i = 0; i < characters_ - by; i++) {
positions_[i] = positions_[by + i];
}
for (int i = characters_ - by; i < characters_; i++) {
positions_[i].mask = 0;
positions_[i].value = 0;
positions_[i].determines_perfectly = false;
}
characters_ -= by;
// We could change mask_ and value_ here but we would never advance unless
// they had already been used in a check and they won't be used again because
// it would gain us nothing. So there's no point.
}
void QuickCheckDetails::Merge(QuickCheckDetails* other, int from_index) {
DCHECK(characters_ == other->characters_);
if (other->cannot_match_) {
return;
}
if (cannot_match_) {
*this = *other;
return;
}
for (int i = from_index; i < characters_; i++) {
QuickCheckDetails::Position* pos = positions(i);
QuickCheckDetails::Position* other_pos = other->positions(i);
if (pos->mask != other_pos->mask ||
pos->value != other_pos->value ||
!other_pos->determines_perfectly) {
// Our mask-compare operation will be approximate unless we have the
// exact same operation on both sides of the alternation.
pos->determines_perfectly = false;
}
pos->mask &= other_pos->mask;
pos->value &= pos->mask;
other_pos->value &= pos->mask;
uc16 differing_bits = (pos->value ^ other_pos->value);
pos->mask &= ~differing_bits;
pos->value &= pos->mask;
}
}
class VisitMarker {
public:
explicit VisitMarker(NodeInfo* info) : info_(info) {
DCHECK(!info->visited);
info->visited = true;
}
~VisitMarker() {
info_->visited = false;
}
private:
NodeInfo* info_;
};
RegExpNode* SeqRegExpNode::FilterOneByte(int depth, bool ignore_case) {
if (info()->replacement_calculated) return replacement();
if (depth < 0) return this;
DCHECK(!info()->visited);
VisitMarker marker(info());
return FilterSuccessor(depth - 1, ignore_case);
}
RegExpNode* SeqRegExpNode::FilterSuccessor(int depth, bool ignore_case) {
RegExpNode* next = on_success_->FilterOneByte(depth - 1, ignore_case);
if (next == NULL) return set_replacement(NULL);
on_success_ = next;
return set_replacement(this);
}
// We need to check for the following characters: 0x39c 0x3bc 0x178.
static inline bool RangeContainsLatin1Equivalents(CharacterRange range) {
// TODO(dcarney): this could be a lot more efficient.
return range.Contains(0x39c) ||
range.Contains(0x3bc) || range.Contains(0x178);
}
static bool RangesContainLatin1Equivalents(ZoneList<CharacterRange>* ranges) {
for (int i = 0; i < ranges->length(); i++) {
// TODO(dcarney): this could be a lot more efficient.
if (RangeContainsLatin1Equivalents(ranges->at(i))) return true;
}
return false;
}
RegExpNode* TextNode::FilterOneByte(int depth, bool ignore_case) {
if (info()->replacement_calculated) return replacement();
if (depth < 0) return this;
DCHECK(!info()->visited);
VisitMarker marker(info());
int element_count = elements()->length();
for (int i = 0; i < element_count; i++) {
TextElement elm = elements()->at(i);
if (elm.text_type() == TextElement::ATOM) {
Vector<const uc16> quarks = elm.atom()->data();
for (int j = 0; j < quarks.length(); j++) {
uint16_t c = quarks[j];
if (c <= String::kMaxOneByteCharCode) continue;
if (!ignore_case) return set_replacement(NULL);
// Here, we need to check for characters whose upper and lower cases
// are outside the Latin-1 range.
uint16_t converted = unibrow::Latin1::ConvertNonLatin1ToLatin1(c);
// Character is outside Latin-1 completely
if (converted == 0) return set_replacement(NULL);
// Convert quark to Latin-1 in place.
uint16_t* copy = const_cast<uint16_t*>(quarks.start());
copy[j] = converted;
}
} else {
DCHECK(elm.text_type() == TextElement::CHAR_CLASS);
RegExpCharacterClass* cc = elm.char_class();
ZoneList<CharacterRange>* ranges = cc->ranges(zone());
CharacterRange::Canonicalize(ranges);
// Now they are in order so we only need to look at the first.
int range_count = ranges->length();
if (cc->is_negated()) {
if (range_count != 0 &&
ranges->at(0).from() == 0 &&
ranges->at(0).to() >= String::kMaxOneByteCharCode) {
// This will be handled in a later filter.
if (ignore_case && RangesContainLatin1Equivalents(ranges)) continue;
return set_replacement(NULL);
}
} else {
if (range_count == 0 ||
ranges->at(0).from() > String::kMaxOneByteCharCode) {
// This will be handled in a later filter.
if (ignore_case && RangesContainLatin1Equivalents(ranges)) continue;
return set_replacement(NULL);
}
}
}
}
return FilterSuccessor(depth - 1, ignore_case);
}
RegExpNode* LoopChoiceNode::FilterOneByte(int depth, bool ignore_case) {
if (info()->replacement_calculated) return replacement();
if (depth < 0) return this;
if (info()->visited) return this;
{
VisitMarker marker(info());
RegExpNode* continue_replacement =
continue_node_->FilterOneByte(depth - 1, ignore_case);
// If we can't continue after the loop then there is no sense in doing the
// loop.
if (continue_replacement == NULL) return set_replacement(NULL);
}
return ChoiceNode::FilterOneByte(depth - 1, ignore_case);
}
RegExpNode* ChoiceNode::FilterOneByte(int depth, bool ignore_case) {
if (info()->replacement_calculated) return replacement();
if (depth < 0) return this;
if (info()->visited) return this;
VisitMarker marker(info());
int choice_count = alternatives_->length();
for (int i = 0; i < choice_count; i++) {
GuardedAlternative alternative = alternatives_->at(i);
if (alternative.guards() != NULL && alternative.guards()->length() != 0) {
set_replacement(this);
return this;
}
}
int surviving = 0;
RegExpNode* survivor = NULL;
for (int i = 0; i < choice_count; i++) {
GuardedAlternative alternative = alternatives_->at(i);
RegExpNode* replacement =
alternative.node()->FilterOneByte(depth - 1, ignore_case);
DCHECK(replacement != this); // No missing EMPTY_MATCH_CHECK.
if (replacement != NULL) {
alternatives_->at(i).set_node(replacement);
surviving++;
survivor = replacement;
}
}
if (surviving < 2) return set_replacement(survivor);
set_replacement(this);
if (surviving == choice_count) {
return this;
}
// Only some of the nodes survived the filtering. We need to rebuild the
// alternatives list.
ZoneList<GuardedAlternative>* new_alternatives =
new(zone()) ZoneList<GuardedAlternative>(surviving, zone());
for (int i = 0; i < choice_count; i++) {
RegExpNode* replacement =
alternatives_->at(i).node()->FilterOneByte(depth - 1, ignore_case);
if (replacement != NULL) {
alternatives_->at(i).set_node(replacement);
new_alternatives->Add(alternatives_->at(i), zone());
}
}
alternatives_ = new_alternatives;
return this;
}
RegExpNode* NegativeLookaroundChoiceNode::FilterOneByte(int depth,
bool ignore_case) {
if (info()->replacement_calculated) return replacement();
if (depth < 0) return this;
if (info()->visited) return this;
VisitMarker marker(info());
// Alternative 0 is the negative lookahead, alternative 1 is what comes
// afterwards.
RegExpNode* node = alternatives_->at(1).node();
RegExpNode* replacement = node->FilterOneByte(depth - 1, ignore_case);
if (replacement == NULL) return set_replacement(NULL);
alternatives_->at(1).set_node(replacement);
RegExpNode* neg_node = alternatives_->at(0).node();
RegExpNode* neg_replacement = neg_node->FilterOneByte(depth - 1, ignore_case);
// If the negative lookahead is always going to fail then
// we don't need to check it.
if (neg_replacement == NULL) return set_replacement(replacement);
alternatives_->at(0).set_node(neg_replacement);
return set_replacement(this);
}
void LoopChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details,
RegExpCompiler* compiler,
int characters_filled_in,
bool not_at_start) {
if (body_can_be_zero_length_ || info()->visited) return;
VisitMarker marker(info());
return ChoiceNode::GetQuickCheckDetails(details,
compiler,
characters_filled_in,
not_at_start);
}
void LoopChoiceNode::FillInBMInfo(Isolate* isolate, int offset, int budget,
BoyerMooreLookahead* bm, bool not_at_start) {
if (body_can_be_zero_length_ || budget <= 0) {
bm->SetRest(offset);
SaveBMInfo(bm, not_at_start, offset);
return;
}
ChoiceNode::FillInBMInfo(isolate, offset, budget - 1, bm, not_at_start);
SaveBMInfo(bm, not_at_start, offset);
}
void ChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details,
RegExpCompiler* compiler,
int characters_filled_in,
bool not_at_start) {
not_at_start = (not_at_start || not_at_start_);
int choice_count = alternatives_->length();
DCHECK(choice_count > 0);
alternatives_->at(0).node()->GetQuickCheckDetails(details,
compiler,
characters_filled_in,
not_at_start);
for (int i = 1; i < choice_count; i++) {
QuickCheckDetails new_details(details->characters());
RegExpNode* node = alternatives_->at(i).node();
node->GetQuickCheckDetails(&new_details, compiler,
characters_filled_in,
not_at_start);
// Here we merge the quick match details of the two branches.
details->Merge(&new_details, characters_filled_in);
}
}
// Check for [0-9A-Z_a-z].
static void EmitWordCheck(RegExpMacroAssembler* assembler,
Label* word,
Label* non_word,
bool fall_through_on_word) {
if (assembler->CheckSpecialCharacterClass(
fall_through_on_word ? 'w' : 'W',
fall_through_on_word ? non_word : word)) {
// Optimized implementation available.
return;
}
assembler->CheckCharacterGT('z', non_word);
assembler->CheckCharacterLT('0', non_word);
assembler->CheckCharacterGT('a' - 1, word);
assembler->CheckCharacterLT('9' + 1, word);
assembler->CheckCharacterLT('A', non_word);
assembler->CheckCharacterLT('Z' + 1, word);
if (fall_through_on_word) {
assembler->CheckNotCharacter('_', non_word);
} else {
assembler->CheckCharacter('_', word);
}
}
// Emit the code to check for a ^ in multiline mode (1-character lookbehind
// that matches newline or the start of input).
static void EmitHat(RegExpCompiler* compiler,
RegExpNode* on_success,
Trace* trace) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
// We will be loading the previous character into the current character
// register.
Trace new_trace(*trace);
new_trace.InvalidateCurrentCharacter();
Label ok;
if (new_trace.cp_offset() == 0) {
// The start of input counts as a newline in this context, so skip to
// ok if we are at the start.
assembler->CheckAtStart(&ok);
}
// We already checked that we are not at the start of input so it must be
// OK to load the previous character.
assembler->LoadCurrentCharacter(new_trace.cp_offset() -1,
new_trace.backtrack(),
false);
if (!assembler->CheckSpecialCharacterClass('n',
new_trace.backtrack())) {
// Newline means \n, \r, 0x2028 or 0x2029.
if (!compiler->one_byte()) {
assembler->CheckCharacterAfterAnd(0x2028, 0xfffe, &ok);
}
assembler->CheckCharacter('\n', &ok);
assembler->CheckNotCharacter('\r', new_trace.backtrack());
}
assembler->Bind(&ok);
on_success->Emit(compiler, &new_trace);
}
// Emit the code to handle \b and \B (word-boundary or non-word-boundary).
void AssertionNode::EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
Isolate* isolate = assembler->isolate();
Trace::TriBool next_is_word_character = Trace::UNKNOWN;
bool not_at_start = (trace->at_start() == Trace::FALSE_VALUE);
BoyerMooreLookahead* lookahead = bm_info(not_at_start);
if (lookahead == NULL) {
int eats_at_least =
Min(kMaxLookaheadForBoyerMoore, EatsAtLeast(kMaxLookaheadForBoyerMoore,
kRecursionBudget,
not_at_start));
if (eats_at_least >= 1) {
BoyerMooreLookahead* bm =
new(zone()) BoyerMooreLookahead(eats_at_least, compiler, zone());
FillInBMInfo(isolate, 0, kRecursionBudget, bm, not_at_start);
if (bm->at(0)->is_non_word())
next_is_word_character = Trace::FALSE_VALUE;
if (bm->at(0)->is_word()) next_is_word_character = Trace::TRUE_VALUE;
}
} else {
if (lookahead->at(0)->is_non_word())
next_is_word_character = Trace::FALSE_VALUE;
if (lookahead->at(0)->is_word())
next_is_word_character = Trace::TRUE_VALUE;
}
bool at_boundary = (assertion_type_ == AssertionNode::AT_BOUNDARY);
if (next_is_word_character == Trace::UNKNOWN) {
Label before_non_word;
Label before_word;
if (trace->characters_preloaded() != 1) {
assembler->LoadCurrentCharacter(trace->cp_offset(), &before_non_word);
}
// Fall through on non-word.
EmitWordCheck(assembler, &before_word, &before_non_word, false);
// Next character is not a word character.
assembler->Bind(&before_non_word);
Label ok;
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord);
assembler->GoTo(&ok);
assembler->Bind(&before_word);
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord);
assembler->Bind(&ok);
} else if (next_is_word_character == Trace::TRUE_VALUE) {
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord);
} else {
DCHECK(next_is_word_character == Trace::FALSE_VALUE);
BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord);
}
}
void AssertionNode::BacktrackIfPrevious(
RegExpCompiler* compiler,
Trace* trace,
AssertionNode::IfPrevious backtrack_if_previous) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
Trace new_trace(*trace);
new_trace.InvalidateCurrentCharacter();
Label fall_through, dummy;
Label* non_word = backtrack_if_previous == kIsNonWord ?
new_trace.backtrack() :
&fall_through;
Label* word = backtrack_if_previous == kIsNonWord ?
&fall_through :
new_trace.backtrack();
if (new_trace.cp_offset() == 0) {
// The start of input counts as a non-word character, so the question is
// decided if we are at the start.
assembler->CheckAtStart(non_word);
}
// We already checked that we are not at the start of input so it must be
// OK to load the previous character.
assembler->LoadCurrentCharacter(new_trace.cp_offset() - 1, &dummy, false);
EmitWordCheck(assembler, word, non_word, backtrack_if_previous == kIsNonWord);
assembler->Bind(&fall_through);
on_success()->Emit(compiler, &new_trace);
}
void AssertionNode::GetQuickCheckDetails(QuickCheckDetails* details,
RegExpCompiler* compiler,
int filled_in,
bool not_at_start) {
if (assertion_type_ == AT_START && not_at_start) {
details->set_cannot_match();
return;
}
return on_success()->GetQuickCheckDetails(details,
compiler,
filled_in,
not_at_start);
}
void AssertionNode::Emit(RegExpCompiler* compiler, Trace* trace) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
switch (assertion_type_) {
case AT_END: {
Label ok;
assembler->CheckPosition(trace->cp_offset(), &ok);
assembler->GoTo(trace->backtrack());
assembler->Bind(&ok);
break;
}
case AT_START: {
if (trace->at_start() == Trace::FALSE_VALUE) {
assembler->GoTo(trace->backtrack());
return;
}
if (trace->at_start() == Trace::UNKNOWN) {
assembler->CheckNotAtStart(trace->cp_offset(), trace->backtrack());
Trace at_start_trace = *trace;
at_start_trace.set_at_start(Trace::TRUE_VALUE);
on_success()->Emit(compiler, &at_start_trace);
return;
}
}
break;
case AFTER_NEWLINE:
EmitHat(compiler, on_success(), trace);
return;
case AT_BOUNDARY:
case AT_NON_BOUNDARY: {
EmitBoundaryCheck(compiler, trace);
return;
}
}
on_success()->Emit(compiler, trace);
}
static bool DeterminedAlready(QuickCheckDetails* quick_check, int offset) {
if (quick_check == NULL) return false;
if (offset >= quick_check->characters()) return false;
return quick_check->positions(offset)->determines_perfectly;
}
static void UpdateBoundsCheck(int index, int* checked_up_to) {
if (index > *checked_up_to) {
*checked_up_to = index;
}
}
// We call this repeatedly to generate code for each pass over the text node.
// The passes are in increasing order of difficulty because we hope one
// of the first passes will fail in which case we are saved the work of the
// later passes. for example for the case independent regexp /%[asdfghjkl]a/
// we will check the '%' in the first pass, the case independent 'a' in the
// second pass and the character class in the last pass.
//
// The passes are done from right to left, so for example to test for /bar/
// we will first test for an 'r' with offset 2, then an 'a' with offset 1
// and then a 'b' with offset 0. This means we can avoid the end-of-input
// bounds check most of the time. In the example we only need to check for
// end-of-input when loading the putative 'r'.
//
// A slight complication involves the fact that the first character may already
// be fetched into a register by the previous node. In this case we want to
// do the test for that character first. We do this in separate passes. The
// 'preloaded' argument indicates that we are doing such a 'pass'. If such a
// pass has been performed then subsequent passes will have true in
// first_element_checked to indicate that that character does not need to be
// checked again.
//
// In addition to all this we are passed a Trace, which can
// contain an AlternativeGeneration object. In this AlternativeGeneration
// object we can see details of any quick check that was already passed in
// order to get to the code we are now generating. The quick check can involve
// loading characters, which means we do not need to recheck the bounds
// up to the limit the quick check already checked. In addition the quick
// check can have involved a mask and compare operation which may simplify
// or obviate the need for further checks at some character positions.
void TextNode::TextEmitPass(RegExpCompiler* compiler,
TextEmitPassType pass,
bool preloaded,
Trace* trace,
bool first_element_checked,
int* checked_up_to) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
Isolate* isolate = assembler->isolate();
bool one_byte = compiler->one_byte();
Label* backtrack = trace->backtrack();
QuickCheckDetails* quick_check = trace->quick_check_performed();
int element_count = elements()->length();
int backward_offset = read_backward() ? -Length() : 0;
for (int i = preloaded ? 0 : element_count - 1; i >= 0; i--) {
TextElement elm = elements()->at(i);
int cp_offset = trace->cp_offset() + elm.cp_offset() + backward_offset;
if (elm.text_type() == TextElement::ATOM) {
Vector<const uc16> quarks = elm.atom()->data();
for (int j = preloaded ? 0 : quarks.length() - 1; j >= 0; j--) {
if (first_element_checked && i == 0 && j == 0) continue;
if (DeterminedAlready(quick_check, elm.cp_offset() + j)) continue;
EmitCharacterFunction* emit_function = NULL;
switch (pass) {
case NON_LATIN1_MATCH:
DCHECK(one_byte);
if (quarks[j] > String::kMaxOneByteCharCode) {
assembler->GoTo(backtrack);
return;
}
break;
case NON_LETTER_CHARACTER_MATCH:
emit_function = &EmitAtomNonLetter;
break;
case SIMPLE_CHARACTER_MATCH:
emit_function = &EmitSimpleCharacter;
break;
case CASE_CHARACTER_MATCH:
emit_function = &EmitAtomLetter;
break;
default:
break;
}
if (emit_function != NULL) {
bool bounds_check = *checked_up_to < cp_offset + j || read_backward();
bool bound_checked =
emit_function(isolate, compiler, quarks[j], backtrack,
cp_offset + j, bounds_check, preloaded);
if (bound_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to);
}
}
} else {
DCHECK_EQ(TextElement::CHAR_CLASS, elm.text_type());
if (pass == CHARACTER_CLASS_MATCH) {
if (first_element_checked && i == 0) continue;
if (DeterminedAlready(quick_check, elm.cp_offset())) continue;
RegExpCharacterClass* cc = elm.char_class();
bool bounds_check = *checked_up_to < cp_offset || read_backward();
EmitCharClass(assembler, cc, one_byte, backtrack, cp_offset,
bounds_check, preloaded, zone());
UpdateBoundsCheck(cp_offset, checked_up_to);
}
}
}
}
int TextNode::Length() {
TextElement elm = elements()->last();
DCHECK(elm.cp_offset() >= 0);
return elm.cp_offset() + elm.length();
}
bool TextNode::SkipPass(int int_pass, bool ignore_case) {
TextEmitPassType pass = static_cast<TextEmitPassType>(int_pass);
if (ignore_case) {
return pass == SIMPLE_CHARACTER_MATCH;
} else {
return pass == NON_LETTER_CHARACTER_MATCH || pass == CASE_CHARACTER_MATCH;
}
}
TextNode* TextNode::CreateForCharacterRanges(Zone* zone,
ZoneList<CharacterRange>* ranges,
bool read_backward,
RegExpNode* on_success) {
DCHECK_NOT_NULL(ranges);
ZoneList<TextElement>* elms = new (zone) ZoneList<TextElement>(1, zone);
elms->Add(
TextElement::CharClass(new (zone) RegExpCharacterClass(ranges, false)),
zone);
return new (zone) TextNode(elms, read_backward, on_success);
}
TextNode* TextNode::CreateForSurrogatePair(Zone* zone, CharacterRange lead,
CharacterRange trail,
bool read_backward,
RegExpNode* on_success) {
ZoneList<CharacterRange>* lead_ranges = CharacterRange::List(zone, lead);
ZoneList<CharacterRange>* trail_ranges = CharacterRange::List(zone, trail);
ZoneList<TextElement>* elms = new (zone) ZoneList<TextElement>(2, zone);
elms->Add(TextElement::CharClass(
new (zone) RegExpCharacterClass(lead_ranges, false)),
zone);
elms->Add(TextElement::CharClass(
new (zone) RegExpCharacterClass(trail_ranges, false)),
zone);
return new (zone) TextNode(elms, read_backward, on_success);
}
// This generates the code to match a text node. A text node can contain
// straight character sequences (possibly to be matched in a case-independent
// way) and character classes. For efficiency we do not do this in a single
// pass from left to right. Instead we pass over the text node several times,
// emitting code for some character positions every time. See the comment on
// TextEmitPass for details.
void TextNode::Emit(RegExpCompiler* compiler, Trace* trace) {
LimitResult limit_result = LimitVersions(compiler, trace);
if (limit_result == DONE) return;
DCHECK(limit_result == CONTINUE);
if (trace->cp_offset() + Length() > RegExpMacroAssembler::kMaxCPOffset) {
compiler->SetRegExpTooBig();
return;
}
if (compiler->one_byte()) {
int dummy = 0;
TextEmitPass(compiler, NON_LATIN1_MATCH, false, trace, false, &dummy);
}
bool first_elt_done = false;
int bound_checked_to = trace->cp_offset() - 1;
bound_checked_to += trace->bound_checked_up_to();
// If a character is preloaded into the current character register then
// check that now.
if (trace->characters_preloaded() == 1) {
for (int pass = kFirstRealPass; pass <= kLastPass; pass++) {
if (!SkipPass(pass, compiler->ignore_case())) {
TextEmitPass(compiler,
static_cast<TextEmitPassType>(pass),
true,
trace,
false,
&bound_checked_to);
}
}
first_elt_done = true;
}
for (int pass = kFirstRealPass; pass <= kLastPass; pass++) {
if (!SkipPass(pass, compiler->ignore_case())) {
TextEmitPass(compiler,
static_cast<TextEmitPassType>(pass),
false,
trace,
first_elt_done,
&bound_checked_to);
}
}
Trace successor_trace(*trace);
// If we advance backward, we may end up at the start.
successor_trace.AdvanceCurrentPositionInTrace(
read_backward() ? -Length() : Length(), compiler);
successor_trace.set_at_start(read_backward() ? Trace::UNKNOWN
: Trace::FALSE_VALUE);
RecursionCheck rc(compiler);
on_success()->Emit(compiler, &successor_trace);
}
void Trace::InvalidateCurrentCharacter() {
characters_preloaded_ = 0;
}
void Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) {
// We don't have an instruction for shifting the current character register
// down or for using a shifted value for anything so lets just forget that
// we preloaded any characters into it.
characters_preloaded_ = 0;
// Adjust the offsets of the quick check performed information. This
// information is used to find out what we already determined about the
// characters by means of mask and compare.
quick_check_performed_.Advance(by, compiler->one_byte());
cp_offset_ += by;
if (cp_offset_ > RegExpMacroAssembler::kMaxCPOffset) {
compiler->SetRegExpTooBig();
cp_offset_ = 0;
}
bound_checked_up_to_ = Max(0, bound_checked_up_to_ - by);
}
void TextNode::MakeCaseIndependent(Isolate* isolate, bool is_one_byte) {
int element_count = elements()->length();
for (int i = 0; i < element_count; i++) {
TextElement elm = elements()->at(i);
if (elm.text_type() == TextElement::CHAR_CLASS) {
RegExpCharacterClass* cc = elm.char_class();
// None of the standard character classes is different in the case
// independent case and it slows us down if we don't know that.
if (cc->is_standard(zone())) continue;
ZoneList<CharacterRange>* ranges = cc->ranges(zone());
CharacterRange::AddCaseEquivalents(isolate, zone(), ranges, is_one_byte);
}
}
}
int TextNode::GreedyLoopTextLength() { return Length(); }
RegExpNode* TextNode::GetSuccessorOfOmnivorousTextNode(
RegExpCompiler* compiler) {
if (read_backward()) return NULL;
if (elements()->length() != 1) return NULL;
TextElement elm = elements()->at(0);
if (elm.text_type() != TextElement::CHAR_CLASS) return NULL;
RegExpCharacterClass* node = elm.char_class();
ZoneList<CharacterRange>* ranges = node->ranges(zone());
CharacterRange::Canonicalize(ranges);
if (node->is_negated()) {
return ranges->length() == 0 ? on_success() : NULL;
}
if (ranges->length() != 1) return NULL;
uint32_t max_char;
if (compiler->one_byte()) {
max_char = String::kMaxOneByteCharCode;
} else {
max_char = String::kMaxUtf16CodeUnit;
}
return ranges->at(0).IsEverything(max_char) ? on_success() : NULL;
}
// Finds the fixed match length of a sequence of nodes that goes from
// this alternative and back to this choice node. If there are variable
// length nodes or other complications in the way then return a sentinel
// value indicating that a greedy loop cannot be constructed.
int ChoiceNode::GreedyLoopTextLengthForAlternative(
GuardedAlternative* alternative) {
int length = 0;
RegExpNode* node = alternative->node();
// Later we will generate code for all these text nodes using recursion
// so we have to limit the max number.
int recursion_depth = 0;
while (node != this) {
if (recursion_depth++ > RegExpCompiler::kMaxRecursion) {
return kNodeIsTooComplexForGreedyLoops;
}
int node_length = node->GreedyLoopTextLength();
if (node_length == kNodeIsTooComplexForGreedyLoops) {
return kNodeIsTooComplexForGreedyLoops;
}
length += node_length;
SeqRegExpNode* seq_node = static_cast<SeqRegExpNode*>(node);
node = seq_node->on_success();
}
return read_backward() ? -length : length;
}
void LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) {
DCHECK_NULL(loop_node_);
AddAlternative(alt);
loop_node_ = alt.node();
}
void LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) {
DCHECK_NULL(continue_node_);
AddAlternative(alt);
continue_node_ = alt.node();
}
void LoopChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
if (trace->stop_node() == this) {
// Back edge of greedy optimized loop node graph.
int text_length =
GreedyLoopTextLengthForAlternative(&(alternatives_->at(0)));
DCHECK(text_length != kNodeIsTooComplexForGreedyLoops);
// Update the counter-based backtracking info on the stack. This is an
// optimization for greedy loops (see below).
DCHECK(trace->cp_offset() == text_length);
macro_assembler->AdvanceCurrentPosition(text_length);
macro_assembler->GoTo(trace->loop_label());
return;
}
DCHECK_NULL(trace->stop_node());
if (!trace->is_trivial()) {
trace->Flush(compiler, this);
return;
}
ChoiceNode::Emit(compiler, trace);
}
int ChoiceNode::CalculatePreloadCharacters(RegExpCompiler* compiler,
int eats_at_least) {
int preload_characters = Min(4, eats_at_least);
if (compiler->macro_assembler()->CanReadUnaligned()) {
bool one_byte = compiler->one_byte();
if (one_byte) {
if (preload_characters > 4) preload_characters = 4;
// We can't preload 3 characters because there is no machine instruction
// to do that. We can't just load 4 because we could be reading
// beyond the end of the string, which could cause a memory fault.
if (preload_characters == 3) preload_characters = 2;
} else {
if (preload_characters > 2) preload_characters = 2;
}
} else {
if (preload_characters > 1) preload_characters = 1;
}
return preload_characters;
}
// This class is used when generating the alternatives in a choice node. It
// records the way the alternative is being code generated.
class AlternativeGeneration: public Malloced {
public:
AlternativeGeneration()
: possible_success(),
expects_preload(false),
after(),
quick_check_details() { }
Label possible_success;
bool expects_preload;
Label after;
QuickCheckDetails quick_check_details;
};
// Creates a list of AlternativeGenerations. If the list has a reasonable
// size then it is on the stack, otherwise the excess is on the heap.
class AlternativeGenerationList {
public:
AlternativeGenerationList(int count, Zone* zone)
: alt_gens_(count, zone) {
for (int i = 0; i < count && i < kAFew; i++) {
alt_gens_.Add(a_few_alt_gens_ + i, zone);
}
for (int i = kAFew; i < count; i++) {
alt_gens_.Add(new AlternativeGeneration(), zone);
}
}
~AlternativeGenerationList() {
for (int i = kAFew; i < alt_gens_.length(); i++) {
delete alt_gens_[i];
alt_gens_[i] = NULL;
}
}
AlternativeGeneration* at(int i) {
return alt_gens_[i];
}
private:
static const int kAFew = 10;
ZoneList<AlternativeGeneration*> alt_gens_;
AlternativeGeneration a_few_alt_gens_[kAFew];
};
static const uc32 kRangeEndMarker = 0x110000;
// The '2' variant is has inclusive from and exclusive to.
// This covers \s as defined in ECMA-262 5.1, 15.10.2.12,
// which include WhiteSpace (7.2) or LineTerminator (7.3) values.
static const int kSpaceRanges[] = {
'\t', '\r' + 1, ' ', ' ' + 1, 0x00A0, 0x00A1, 0x1680, 0x1681,
0x180E, 0x180F, 0x2000, 0x200B, 0x2028, 0x202A, 0x202F, 0x2030,
0x205F, 0x2060, 0x3000, 0x3001, 0xFEFF, 0xFF00, kRangeEndMarker};
static const int kSpaceRangeCount = arraysize(kSpaceRanges);
static const int kWordRanges[] = {
'0', '9' + 1, 'A', 'Z' + 1, '_', '_' + 1, 'a', 'z' + 1, kRangeEndMarker};
static const int kWordRangeCount = arraysize(kWordRanges);
static const int kDigitRanges[] = {'0', '9' + 1, kRangeEndMarker};
static const int kDigitRangeCount = arraysize(kDigitRanges);
static const int kSurrogateRanges[] = {
kLeadSurrogateStart, kLeadSurrogateStart + 1, kRangeEndMarker};
static const int kSurrogateRangeCount = arraysize(kSurrogateRanges);
static const int kLineTerminatorRanges[] = {
0x000A, 0x000B, 0x000D, 0x000E, 0x2028, 0x202A, kRangeEndMarker};
static const int kLineTerminatorRangeCount = arraysize(kLineTerminatorRanges);
void BoyerMoorePositionInfo::Set(int character) {
SetInterval(Interval(character, character));
}
void BoyerMoorePositionInfo::SetInterval(const Interval& interval) {
s_ = AddRange(s_, kSpaceRanges, kSpaceRangeCount, interval);
w_ = AddRange(w_, kWordRanges, kWordRangeCount, interval);
d_ = AddRange(d_, kDigitRanges, kDigitRangeCount, interval);
surrogate_ =
AddRange(surrogate_, kSurrogateRanges, kSurrogateRangeCount, interval);
if (interval.to() - interval.from() >= kMapSize - 1) {
if (map_count_ != kMapSize) {
map_count_ = kMapSize;
for (int i = 0; i < kMapSize; i++) map_->at(i) = true;
}
return;
}
for (int i = interval.from(); i <= interval.to(); i++) {
int mod_character = (i & kMask);
if (!map_->at(mod_character)) {
map_count_++;
map_->at(mod_character) = true;
}
if (map_count_ == kMapSize) return;
}
}
void BoyerMoorePositionInfo::SetAll() {
s_ = w_ = d_ = kLatticeUnknown;
if (map_count_ != kMapSize) {
map_count_ = kMapSize;
for (int i = 0; i < kMapSize; i++) map_->at(i) = true;
}
}
BoyerMooreLookahead::BoyerMooreLookahead(
int length, RegExpCompiler* compiler, Zone* zone)
: length_(length),
compiler_(compiler) {
if (compiler->one_byte()) {
max_char_ = String::kMaxOneByteCharCode;
} else {
max_char_ = String::kMaxUtf16CodeUnit;
}
bitmaps_ = new(zone) ZoneList<BoyerMoorePositionInfo*>(length, zone);
for (int i = 0; i < length; i++) {
bitmaps_->Add(new(zone) BoyerMoorePositionInfo(zone), zone);
}
}
// Find the longest range of lookahead that has the fewest number of different
// characters that can occur at a given position. Since we are optimizing two
// different parameters at once this is a tradeoff.
bool BoyerMooreLookahead::FindWorthwhileInterval(int* from, int* to) {
int biggest_points = 0;
// If more than 32 characters out of 128 can occur it is unlikely that we can
// be lucky enough to step forwards much of the time.
const int kMaxMax = 32;
for (int max_number_of_chars = 4;
max_number_of_chars < kMaxMax;
max_number_of_chars *= 2) {
biggest_points =
FindBestInterval(max_number_of_chars, biggest_points, from, to);
}
if (biggest_points == 0) return false;
return true;
}
// Find the highest-points range between 0 and length_ where the character
// information is not too vague. 'Too vague' means that there are more than
// max_number_of_chars that can occur at this position. Calculates the number
// of points as the product of width-of-the-range and
// probability-of-finding-one-of-the-characters, where the probability is
// calculated using the frequency distribution of the sample subject string.
int BoyerMooreLookahead::FindBestInterval(
int max_number_of_chars, int old_biggest_points, int* from, int* to) {
int biggest_points = old_biggest_points;
static const int kSize = RegExpMacroAssembler::kTableSize;
for (int i = 0; i < length_; ) {
while (i < length_ && Count(i) > max_number_of_chars) i++;
if (i == length_) break;
int remembered_from = i;
bool union_map[kSize];
for (int j = 0; j < kSize; j++) union_map[j] = false;
while (i < length_ && Count(i) <= max_number_of_chars) {
BoyerMoorePositionInfo* map = bitmaps_->at(i);
for (int j = 0; j < kSize; j++) union_map[j] |= map->at(j);
i++;
}
int frequency = 0;
for (int j = 0; j < kSize; j++) {
if (union_map[j]) {
// Add 1 to the frequency to give a small per-character boost for
// the cases where our sampling is not good enough and many
// characters have a frequency of zero. This means the frequency
// can theoretically be up to 2*kSize though we treat it mostly as
// a fraction of kSize.
frequency += compiler_->frequency_collator()->Frequency(j) + 1;
}
}
// We use the probability of skipping times the distance we are skipping to
// judge the effectiveness of this. Actually we have a cut-off: By
// dividing by 2 we switch off the skipping if the probability of skipping
// is less than 50%. This is because the multibyte mask-and-compare
// skipping in quickcheck is more likely to do well on this case.
bool in_quickcheck_range =
((i - remembered_from < 4) ||
(compiler_->one_byte() ? remembered_from <= 4 : remembered_from <= 2));
// Called 'probability' but it is only a rough estimate and can actually
// be outside the 0-kSize range.
int probability = (in_quickcheck_range ? kSize / 2 : kSize) - frequency;
int points = (i - remembered_from) * probability;
if (points > biggest_points) {
*from = remembered_from;
*to = i - 1;
biggest_points = points;
}
}
return biggest_points;
}
// Take all the characters that will not prevent a successful match if they
// occur in the subject string in the range between min_lookahead and
// max_lookahead (inclusive) measured from the current position. If the
// character at max_lookahead offset is not one of these characters, then we
// can safely skip forwards by the number of characters in the range.
int BoyerMooreLookahead::GetSkipTable(int min_lookahead,
int max_lookahead,
Handle<ByteArray> boolean_skip_table) {
const int kSize = RegExpMacroAssembler::kTableSize;
const int kSkipArrayEntry = 0;
const int kDontSkipArrayEntry = 1;
for (int i = 0; i < kSize; i++) {
boolean_skip_table->set(i, kSkipArrayEntry);
}
int skip = max_lookahead + 1 - min_lookahead;
for (int i = max_lookahead; i >= min_lookahead; i--) {
BoyerMoorePositionInfo* map = bitmaps_->at(i);
for (int j = 0; j < kSize; j++) {
if (map->at(j)) {
boolean_skip_table->set(j, kDontSkipArrayEntry);
}
}
}
return skip;
}
// See comment above on the implementation of GetSkipTable.
void BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) {
const int kSize = RegExpMacroAssembler::kTableSize;
int min_lookahead = 0;
int max_lookahead = 0;
if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) return;
bool found_single_character = false;
int single_character = 0;
for (int i = max_lookahead; i >= min_lookahead; i--) {
BoyerMoorePositionInfo* map = bitmaps_->at(i);
if (map->map_count() > 1 ||
(found_single_character && map->map_count() != 0)) {
found_single_character = false;
break;
}
for (int j = 0; j < kSize; j++) {
if (map->at(j)) {
found_single_character = true;
single_character = j;
break;
}
}
}
int lookahead_width = max_lookahead + 1 - min_lookahead;
if (found_single_character && lookahead_width == 1 && max_lookahead < 3) {
// The mask-compare can probably handle this better.
return;
}
if (found_single_character) {
Label cont, again;
masm->Bind(&again);
masm->LoadCurrentCharacter(max_lookahead, &cont, true);
if (max_char_ > kSize) {
masm->CheckCharacterAfterAnd(single_character,
RegExpMacroAssembler::kTableMask,
&cont);
} else {
masm->CheckCharacter(single_character, &cont);
}
masm->AdvanceCurrentPosition(lookahead_width);
masm->GoTo(&again);
masm->Bind(&cont);
return;
}
Factory* factory = masm->isolate()->factory();
Handle<ByteArray> boolean_skip_table = factory->NewByteArray(kSize, TENURED);
int skip_distance = GetSkipTable(
min_lookahead, max_lookahead, boolean_skip_table);
DCHECK(skip_distance != 0);
Label cont, again;
masm->Bind(&again);
masm->LoadCurrentCharacter(max_lookahead, &cont, true);
masm->CheckBitInTable(boolean_skip_table, &cont);
masm->AdvanceCurrentPosition(skip_distance);
masm->GoTo(&again);
masm->Bind(&cont);
}
/* Code generation for choice nodes.
*
* We generate quick checks that do a mask and compare to eliminate a
* choice. If the quick check succeeds then it jumps to the continuation to
* do slow checks and check subsequent nodes. If it fails (the common case)
* it falls through to the next choice.
*
* Here is the desired flow graph. Nodes directly below each other imply
* fallthrough. Alternatives 1 and 2 have quick checks. Alternative
* 3 doesn't have a quick check so we have to call the slow check.
* Nodes are marked Qn for quick checks and Sn for slow checks. The entire
* regexp continuation is generated directly after the Sn node, up to the
* next GoTo if we decide to reuse some already generated code. Some
* nodes expect preload_characters to be preloaded into the current
* character register. R nodes do this preloading. Vertices are marked
* F for failures and S for success (possible success in the case of quick
* nodes). L, V, < and > are used as arrow heads.
*
* ----------> R
* |
* V
* Q1 -----> S1
* | S /
* F| /
* | F/
* | /
* | R
* | /
* V L
* Q2 -----> S2
* | S /
* F| /
* | F/
* | /
* | R
* | /
* V L
* S3
* |
* F|
* |
* R
* |
* backtrack V
* <----------Q4
* \ F |
* \ |S
* \ F V
* \-----S4
*
* For greedy loops we push the current position, then generate the code that
* eats the input specially in EmitGreedyLoop. The other choice (the
* continuation) is generated by the normal code in EmitChoices, and steps back
* in the input to the starting position when it fails to match. The loop code
* looks like this (U is the unwind code that steps back in the greedy loop).
*
* _____
* / \
* V |
* ----------> S1 |
* /| |
* / |S |
* F/ \_____/
* /
* |<-----
* | \
* V |S
* Q2 ---> U----->backtrack
* | F /
* S| /
* V F /
* S2--/
*/
GreedyLoopState::GreedyLoopState(bool not_at_start) {
counter_backtrack_trace_.set_backtrack(&label_);
if (not_at_start) counter_backtrack_trace_.set_at_start(Trace::FALSE_VALUE);
}
void ChoiceNode::AssertGuardsMentionRegisters(Trace* trace) {
#ifdef DEBUG
int choice_count = alternatives_->length();
for (int i = 0; i < choice_count - 1; i++) {
GuardedAlternative alternative = alternatives_->at(i);
ZoneList<Guard*>* guards = alternative.guards();
int guard_count = (guards == NULL) ? 0 : guards->length();
for (int j = 0; j < guard_count; j++) {
DCHECK(!trace->mentions_reg(guards->at(j)->reg()));
}
}
#endif
}
void ChoiceNode::SetUpPreLoad(RegExpCompiler* compiler,
Trace* current_trace,
PreloadState* state) {
if (state->eats_at_least_ == PreloadState::kEatsAtLeastNotYetInitialized) {
// Save some time by looking at most one machine word ahead.
state->eats_at_least_ =
EatsAtLeast(compiler->one_byte() ? 4 : 2, kRecursionBudget,
current_trace->at_start() == Trace::FALSE_VALUE);
}
state->preload_characters_ =
CalculatePreloadCharacters(compiler, state->eats_at_least_);
state->preload_is_current_ =
(current_trace->characters_preloaded() == state->preload_characters_);
state->preload_has_checked_bounds_ = state->preload_is_current_;
}
void ChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
int choice_count = alternatives_->length();
if (choice_count == 1 && alternatives_->at(0).guards() == NULL) {
alternatives_->at(0).node()->Emit(compiler, trace);
return;
}
AssertGuardsMentionRegisters(trace);
LimitResult limit_result = LimitVersions(compiler, trace);
if (limit_result == DONE) return;
DCHECK(limit_result == CONTINUE);
// For loop nodes we already flushed (see LoopChoiceNode::Emit), but for
// other choice nodes we only flush if we are out of code size budget.
if (trace->flush_budget() == 0 && trace->actions() != NULL) {
trace->Flush(compiler, this);
return;
}
RecursionCheck rc(compiler);
PreloadState preload;
preload.init();
GreedyLoopState greedy_loop_state(not_at_start());
int text_length = GreedyLoopTextLengthForAlternative(&alternatives_->at(0));
AlternativeGenerationList alt_gens(choice_count, zone());
if (choice_count > 1 && text_length != kNodeIsTooComplexForGreedyLoops) {
trace = EmitGreedyLoop(compiler,
trace,
&alt_gens,
&preload,
&greedy_loop_state,
text_length);
} else {
// TODO(erikcorry): Delete this. We don't need this label, but it makes us
// match the traces produced pre-cleanup.
Label second_choice;
compiler->macro_assembler()->Bind(&second_choice);
preload.eats_at_least_ = EmitOptimizedUnanchoredSearch(compiler, trace);
EmitChoices(compiler,
&alt_gens,
0,
trace,
&preload);
}
// At this point we need to generate slow checks for the alternatives where
// the quick check was inlined. We can recognize these because the associated
// label was bound.
int new_flush_budget = trace->flush_budget() / choice_count;
for (int i = 0; i < choice_count; i++) {
AlternativeGeneration* alt_gen = alt_gens.at(i);
Trace new_trace(*trace);
// If there are actions to be flushed we have to limit how many times
// they are flushed. Take the budget of the parent trace and distribute
// it fairly amongst the children.
if (new_trace.actions() != NULL) {
new_trace.set_flush_budget(new_flush_budget);
}
bool next_expects_preload =
i == choice_count - 1 ? false : alt_gens.at(i + 1)->expects_preload;
EmitOutOfLineContinuation(compiler,
&new_trace,
alternatives_->at(i),
alt_gen,
preload.preload_characters_,
next_expects_preload);
}
}
Trace* ChoiceNode::EmitGreedyLoop(RegExpCompiler* compiler,
Trace* trace,
AlternativeGenerationList* alt_gens,
PreloadState* preload,
GreedyLoopState* greedy_loop_state,
int text_length) {
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
// Here we have special handling for greedy loops containing only text nodes
// and other simple nodes. These are handled by pushing the current
// position on the stack and then incrementing the current position each
// time around the switch. On backtrack we decrement the current position
// and check it against the pushed value. This avoids pushing backtrack
// information for each iteration of the loop, which could take up a lot of
// space.
DCHECK(trace->stop_node() == NULL);
macro_assembler->PushCurrentPosition();
Label greedy_match_failed;
Trace greedy_match_trace;
if (not_at_start()) greedy_match_trace.set_at_start(Trace::FALSE_VALUE);
greedy_match_trace.set_backtrack(&greedy_match_failed);
Label loop_label;
macro_assembler->Bind(&loop_label);
greedy_match_trace.set_stop_node(this);
greedy_match_trace.set_loop_label(&loop_label);
alternatives_->at(0).node()->Emit(compiler, &greedy_match_trace);
macro_assembler->Bind(&greedy_match_failed);
Label second_choice; // For use in greedy matches.
macro_assembler->Bind(&second_choice);
Trace* new_trace = greedy_loop_state->counter_backtrack_trace();
EmitChoices(compiler,
alt_gens,
1,
new_trace,
preload);
macro_assembler->Bind(greedy_loop_state->label());
// If we have unwound to the bottom then backtrack.
macro_assembler->CheckGreedyLoop(trace->backtrack());
// Otherwise try the second priority at an earlier position.
macro_assembler->AdvanceCurrentPosition(-text_length);
macro_assembler->GoTo(&second_choice);
return new_trace;
}
int ChoiceNode::EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler,
Trace* trace) {
int eats_at_least = PreloadState::kEatsAtLeastNotYetInitialized;
if (alternatives_->length() != 2) return eats_at_least;
GuardedAlternative alt1 = alternatives_->at(1);
if (alt1.guards() != NULL && alt1.guards()->length() != 0) {
return eats_at_least;
}
RegExpNode* eats_anything_node = alt1.node();
if (eats_anything_node->GetSuccessorOfOmnivorousTextNode(compiler) != this) {
return eats_at_least;
}
// Really we should be creating a new trace when we execute this function,
// but there is no need, because the code it generates cannot backtrack, and
// we always arrive here with a trivial trace (since it's the entry to a
// loop. That also implies that there are no preloaded characters, which is
// good, because it means we won't be violating any assumptions by
// overwriting those characters with new load instructions.
DCHECK(trace->is_trivial());
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
Isolate* isolate = macro_assembler->isolate();
// At this point we know that we are at a non-greedy loop that will eat
// any character one at a time. Any non-anchored regexp has such a
// loop prepended to it in order to find where it starts. We look for
// a pattern of the form ...abc... where we can look 6 characters ahead
// and step forwards 3 if the character is not one of abc. Abc need
// not be atoms, they can be any reasonably limited character class or
// small alternation.
BoyerMooreLookahead* bm = bm_info(false);
if (bm == NULL) {
eats_at_least = Min(kMaxLookaheadForBoyerMoore,
EatsAtLeast(kMaxLookaheadForBoyerMoore,
kRecursionBudget,
false));
if (eats_at_least >= 1) {
bm = new(zone()) BoyerMooreLookahead(eats_at_least,
compiler,
zone());
GuardedAlternative alt0 = alternatives_->at(0);
alt0.node()->FillInBMInfo(isolate, 0, kRecursionBudget, bm, false);
}
}
if (bm != NULL) {
bm->EmitSkipInstructions(macro_assembler);
}
return eats_at_least;
}
void ChoiceNode::EmitChoices(RegExpCompiler* compiler,
AlternativeGenerationList* alt_gens,
int first_choice,
Trace* trace,
PreloadState* preload) {
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
SetUpPreLoad(compiler, trace, preload);
// For now we just call all choices one after the other. The idea ultimately
// is to use the Dispatch table to try only the relevant ones.
int choice_count = alternatives_->length();
int new_flush_budget = trace->flush_budget() / choice_count;
for (int i = first_choice; i < choice_count; i++) {
bool is_last = i == choice_count - 1;
bool fall_through_on_failure = !is_last;
GuardedAlternative alternative = alternatives_->at(i);
AlternativeGeneration* alt_gen = alt_gens->at(i);
alt_gen->quick_check_details.set_characters(preload->preload_characters_);
ZoneList<Guard*>* guards = alternative.guards();
int guard_count = (guards == NULL) ? 0 : guards->length();
Trace new_trace(*trace);
new_trace.set_characters_preloaded(preload->preload_is_current_ ?
preload->preload_characters_ :
0);
if (preload->preload_has_checked_bounds_) {
new_trace.set_bound_checked_up_to(preload->preload_characters_);
}
new_trace.quick_check_performed()->Clear();
if (not_at_start_) new_trace.set_at_start(Trace::FALSE_VALUE);
if (!is_last) {
new_trace.set_backtrack(&alt_gen->after);
}
alt_gen->expects_preload = preload->preload_is_current_;
bool generate_full_check_inline = false;
if (compiler->optimize() &&
try_to_emit_quick_check_for_alternative(i == 0) &&
alternative.node()->EmitQuickCheck(
compiler, trace, &new_trace, preload->preload_has_checked_bounds_,
&alt_gen->possible_success, &alt_gen->quick_check_details,
fall_through_on_failure)) {
// Quick check was generated for this choice.
preload->preload_is_current_ = true;
preload->preload_has_checked_bounds_ = true;
// If we generated the quick check to fall through on possible success,
// we now need to generate the full check inline.
if (!fall_through_on_failure) {
macro_assembler->Bind(&alt_gen->possible_success);
new_trace.set_quick_check_performed(&alt_gen->quick_check_details);
new_trace.set_characters_preloaded(preload->preload_characters_);
new_trace.set_bound_checked_up_to(preload->preload_characters_);
generate_full_check_inline = true;
}
} else if (alt_gen->quick_check_details.cannot_match()) {
if (!fall_through_on_failure) {
macro_assembler->GoTo(trace->backtrack());
}
continue;
} else {
// No quick check was generated. Put the full code here.
// If this is not the first choice then there could be slow checks from
// previous cases that go here when they fail. There's no reason to
// insist that they preload characters since the slow check we are about
// to generate probably can't use it.
if (i != first_choice) {
alt_gen->expects_preload = false;
new_trace.InvalidateCurrentCharacter();
}
generate_full_check_inline = true;
}
if (generate_full_check_inline) {
if (new_trace.actions() != NULL) {
new_trace.set_flush_budget(new_flush_budget);
}
for (int j = 0; j < guard_count; j++) {
GenerateGuard(macro_assembler, guards->at(j), &new_trace);
}
alternative.node()->Emit(compiler, &new_trace);
preload->preload_is_current_ = false;
}
macro_assembler->Bind(&alt_gen->after);
}
}
void ChoiceNode::EmitOutOfLineContinuation(RegExpCompiler* compiler,
Trace* trace,
GuardedAlternative alternative,
AlternativeGeneration* alt_gen,
int preload_characters,
bool next_expects_preload) {
if (!alt_gen->possible_success.is_linked()) return;
RegExpMacroAssembler* macro_assembler = compiler->macro_assembler();
macro_assembler->Bind(&alt_gen->possible_success);
Trace out_of_line_trace(*trace);
out_of_line_trace.set_characters_preloaded(preload_characters);
out_of_line_trace.set_quick_check_performed(&alt_gen->quick_check_details);
if (not_at_start_) out_of_line_trace.set_at_start(Trace::FALSE_VALUE);
ZoneList<Guard*>* guards = alternative.guards();
int guard_count = (guards == NULL) ? 0 : guards->length();
if (next_expects_preload) {
Label reload_current_char;
out_of_line_trace.set_backtrack(&reload_current_char);
for (int j = 0; j < guard_count; j++) {
GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace);
}
alternative.node()->Emit(compiler, &out_of_line_trace);
macro_assembler->Bind(&reload_current_char);
// Reload the current character, since the next quick check expects that.
// We don't need to check bounds here because we only get into this
// code through a quick check which already did the checked load.
macro_assembler->LoadCurrentCharacter(trace->cp_offset(),
NULL,
false,
preload_characters);
macro_assembler->GoTo(&(alt_gen->after));
} else {
out_of_line_trace.set_backtrack(&(alt_gen->after));
for (int j = 0; j < guard_count; j++) {
GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace);
}
alternative.node()->Emit(compiler, &out_of_line_trace);
}
}
void ActionNode::Emit(RegExpCompiler* compiler, Trace* trace) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
LimitResult limit_result = LimitVersions(compiler, trace);
if (limit_result == DONE) return;
DCHECK(limit_result == CONTINUE);
RecursionCheck rc(compiler);
switch (action_type_) {
case STORE_POSITION: {
Trace::DeferredCapture
new_capture(data_.u_position_register.reg,
data_.u_position_register.is_capture,
trace);
Trace new_trace = *trace;
new_trace.add_action(&new_capture);
on_success()->Emit(compiler, &new_trace);
break;
}
case INCREMENT_REGISTER: {
Trace::DeferredIncrementRegister
new_increment(data_.u_increment_register.reg);
Trace new_trace = *trace;
new_trace.add_action(&new_increment);
on_success()->Emit(compiler, &new_trace);
break;
}
case SET_REGISTER: {
Trace::DeferredSetRegister
new_set(data_.u_store_register.reg, data_.u_store_register.value);
Trace new_trace = *trace;
new_trace.add_action(&new_set);
on_success()->Emit(compiler, &new_trace);
break;
}
case CLEAR_CAPTURES: {
Trace::DeferredClearCaptures
new_capture(Interval(data_.u_clear_captures.range_from,
data_.u_clear_captures.range_to));
Trace new_trace = *trace;
new_trace.add_action(&new_capture);
on_success()->Emit(compiler, &new_trace);
break;
}
case BEGIN_SUBMATCH:
if (!trace->is_trivial()) {
trace->Flush(compiler, this);
} else {
assembler->WriteCurrentPositionToRegister(
data_.u_submatch.current_position_register, 0);
assembler->WriteStackPointerToRegister(
data_.u_submatch.stack_pointer_register);
on_success()->Emit(compiler, trace);
}
break;
case EMPTY_MATCH_CHECK: {
int start_pos_reg = data_.u_empty_match_check.start_register;
int stored_pos = 0;
int rep_reg = data_.u_empty_match_check.repetition_register;
bool has_minimum = (rep_reg != RegExpCompiler::kNoRegister);
bool know_dist = trace->GetStoredPosition(start_pos_reg, &stored_pos);
if (know_dist && !has_minimum && stored_pos == trace->cp_offset()) {
// If we know we haven't advanced and there is no minimum we
// can just backtrack immediately.
assembler->GoTo(trace->backtrack());
} else if (know_dist && stored_pos < trace->cp_offset()) {
// If we know we've advanced we can generate the continuation
// immediately.
on_success()->Emit(compiler, trace);
} else if (!trace->is_trivial()) {
trace->Flush(compiler, this);
} else {
Label skip_empty_check;
// If we have a minimum number of repetitions we check the current
// number first and skip the empty check if it's not enough.
if (has_minimum) {
int limit = data_.u_empty_match_check.repetition_limit;
assembler->IfRegisterLT(rep_reg, limit, &skip_empty_check);
}
// If the match is empty we bail out, otherwise we fall through
// to the on-success continuation.
assembler->IfRegisterEqPos(data_.u_empty_match_check.start_register,
trace->backtrack());
assembler->Bind(&skip_empty_check);
on_success()->Emit(compiler, trace);
}
break;
}
case POSITIVE_SUBMATCH_SUCCESS: {
if (!trace->is_trivial()) {
trace->Flush(compiler, this);
return;
}
assembler->ReadCurrentPositionFromRegister(
data_.u_submatch.current_position_register);
assembler->ReadStackPointerFromRegister(
data_.u_submatch.stack_pointer_register);
int clear_register_count = data_.u_submatch.clear_register_count;
if (clear_register_count == 0) {
on_success()->Emit(compiler, trace);
return;
}
int clear_registers_from = data_.u_submatch.clear_register_from;
Label clear_registers_backtrack;
Trace new_trace = *trace;
new_trace.set_backtrack(&clear_registers_backtrack);
on_success()->Emit(compiler, &new_trace);
assembler->Bind(&clear_registers_backtrack);
int clear_registers_to = clear_registers_from + clear_register_count - 1;
assembler->ClearRegisters(clear_registers_from, clear_registers_to);
DCHECK(trace->backtrack() == NULL);
assembler->Backtrack();
return;
}
default:
UNREACHABLE();
}
}
void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) {
RegExpMacroAssembler* assembler = compiler->macro_assembler();
if (!trace->is_trivial()) {
trace->Flush(compiler, this);
return;
}
LimitResult limit_result = LimitVersions(compiler, trace);
if (limit_result == DONE) return;
DCHECK(limit_result == CONTINUE);
RecursionCheck rc(compiler);
DCHECK_EQ(start_reg_ + 1, end_reg_);
if (compiler->ignore_case()) {
assembler->CheckNotBackReferenceIgnoreCase(
start_reg_, read_backward(), compiler->unicode(), trace->backtrack());
} else {
assembler->CheckNotBackReference(start_reg_, read_backward(),
trace->backtrack());
}
// We are going to advance backward, so we may end up at the start.
if (read_backward()) trace->set_at_start(Trace::UNKNOWN);
// Check that the back reference does not end inside a surrogate pair.
if (compiler->unicode() && !compiler->one_byte()) {
assembler->CheckNotInSurrogatePair(trace->cp_offset(), trace->backtrack());
}
on_success()->Emit(compiler, trace);
}
// -------------------------------------------------------------------
// Dot/dotty output
#ifdef DEBUG
class DotPrinter: public NodeVisitor {
public:
DotPrinter(std::ostream& os, bool ignore_case) // NOLINT
: os_(os),
ignore_case_(ignore_case) {}
void PrintNode(const char* label, RegExpNode* node);
void Visit(RegExpNode* node);
void PrintAttributes(RegExpNode* from);
void PrintOnFailure(RegExpNode* from, RegExpNode* to);
#define DECLARE_VISIT(Type) \
virtual void Visit##Type(Type##Node* that);
FOR_EACH_NODE_TYPE(DECLARE_VISIT)
#undef DECLARE_VISIT
private:
std::ostream& os_;
bool ignore_case_;
};
void DotPrinter::PrintNode(const char* label, RegExpNode* node) {
os_ << "digraph G {\n graph [label=\"";
for (int i = 0; label[i]; i++) {
switch (label[i]) {
case '\\':
os_ << "\\\\";
break;
case '"':
os_ << "\"";
break;
default:
os_ << label[i];
break;
}
}
os_ << "\"];\n";
Visit(node);
os_ << "}" << std::endl;
}
void DotPrinter::Visit(RegExpNode* node) {
if (node->info()->visited) return;
node->info()->visited = true;
node->Accept(this);
}
void DotPrinter::PrintOnFailure(RegExpNode* from, RegExpNode* on_failure) {
os_ << " n" << from << " -> n" << on_failure << " [style=dotted];\n";
Visit(on_failure);
}
class TableEntryBodyPrinter {
public:
TableEntryBodyPrinter(std::ostream& os, ChoiceNode* choice) // NOLINT
: os_(os),
choice_(choice) {}
void Call(uc16 from, DispatchTable::Entry entry) {
OutSet* out_set = entry.out_set();
for (unsigned i = 0; i < OutSet::kFirstLimit; i++) {
if (out_set->Get(i)) {
os_ << " n" << choice() << ":s" << from << "o" << i << " -> n"
<< choice()->alternatives()->at(i).node() << ";\n";
}
}
}
private:
ChoiceNode* choice() { return choice_; }
std::ostream& os_;
ChoiceNode* choice_;
};
class TableEntryHeaderPrinter {
public:
explicit TableEntryHeaderPrinter(std::ostream& os) // NOLINT
: first_(true),
os_(os) {}
void Call(uc16 from, DispatchTable::Entry entry) {
if (first_) {
first_ = false;
} else {
os_ << "|";
}
os_ << "{\\" << AsUC16(from) << "-\\" << AsUC16(entry.to()) << "|{";
OutSet* out_set = entry.out_set();
int priority = 0;
for (unsigned i = 0; i < OutSet::kFirstLimit; i++) {
if (out_set->Get(i)) {
if (priority > 0) os_ << "|";
os_ << "<s" << from << "o" << i << "> " << priority;
priority++;
}
}
os_ << "}}";
}
private:
bool first_;
std::ostream& os_;
};
class AttributePrinter {
public:
explicit AttributePrinter(std::ostream& os) // NOLINT
: os_(os),
first_(true) {}
void PrintSeparator() {
if (first_) {
first_ = false;
} else {
os_ << "|";
}
}
void PrintBit(const char* name, bool value) {
if (!value) return;
PrintSeparator();
os_ << "{" << name << "}";
}
void PrintPositive(const char* name, int value) {
if (value < 0) return;
PrintSeparator();
os_ << "{" << name << "|" << value << "}";
}
private:
std::ostream& os_;
bool first_;
};
void DotPrinter::PrintAttributes(RegExpNode* that) {
os_ << " a" << that << " [shape=Mrecord, color=grey, fontcolor=grey, "
<< "margin=0.1, fontsize=10, label=\"{";
AttributePrinter printer(os_);
NodeInfo* info = that->info();
printer.PrintBit("NI", info->follows_newline_interest);
printer.PrintBit("WI", info->follows_word_interest);
printer.PrintBit("SI", info->follows_start_interest);
Label* label = that->label();
if (label->is_bound())
printer.PrintPositive("@", label->pos());
os_ << "}\"];\n"
<< " a" << that << " -> n" << that
<< " [style=dashed, color=grey, arrowhead=none];\n";
}
static const bool kPrintDispatchTable = false;
void DotPrinter::VisitChoice(ChoiceNode* that) {
if (kPrintDispatchTable) {
os_ << " n" << that << " [shape=Mrecord, label=\"";
TableEntryHeaderPrinter header_printer(os_);
that->GetTable(ignore_case_)->ForEach(&header_printer);
os_ << "\"]\n";
PrintAttributes(that);
TableEntryBodyPrinter body_printer(os_, that);
that->GetTable(ignore_case_)->ForEach(&body_printer);
} else {
os_ << " n" << that << " [shape=Mrecord, label=\"?\"];\n";
for (int i = 0; i < that->alternatives()->length(); i++) {
GuardedAlternative alt = that->alternatives()->at(i);
os_ << " n" << that << " -> n" << alt.node();
}
}
for (int i = 0; i < that->alternatives()->length(); i++) {
GuardedAlternative alt = that->alternatives()->at(i);
alt.node()->Accept(this);
}
}
void DotPrinter::VisitText(TextNode* that) {
Zone* zone = that->zone();
os_ << " n" << that << " [label=\"";
for (int i = 0; i < that->elements()->length(); i++) {
if (i > 0) os_ << " ";
TextElement elm = that->elements()->at(i);
switch (elm.text_type()) {
case TextElement::ATOM: {
Vector<const uc16> data = elm.atom()->data();
for (int i = 0; i < data.length(); i++) {
os_ << static_cast<char>(data[i]);
}
break;
}
case TextElement::CHAR_CLASS: {
RegExpCharacterClass* node = elm.char_class();
os_ << "[";
if (node->is_negated()) os_ << "^";
for (int j = 0; j < node->ranges(zone)->length(); j++) {
CharacterRange range = node->ranges(zone)->at(j);
os_ << AsUC16(range.from()) << "-" << AsUC16(range.to());
}
os_ << "]";
break;
}
default:
UNREACHABLE();
}
}
os_ << "\", shape=box, peripheries=2];\n";
PrintAttributes(that);
os_ << " n" << that << " -> n" << that->on_success() << ";\n";
Visit(that->on_success());
}
void DotPrinter::VisitBackReference(BackReferenceNode* that) {
os_ << " n" << that << " [label=\"$" << that->start_register() << "..$"
<< that->end_register() << "\", shape=doubleoctagon];\n";
PrintAttributes(that);
os_ << " n" << that << " -> n" << that->on_success() << ";\n";
Visit(that->on_success());
}
void DotPrinter::VisitEnd(EndNode* that) {
os_ << " n" << that << " [style=bold, shape=point];\n";
PrintAttributes(that);
}
void DotPrinter::VisitAssertion(AssertionNode* that) {
os_ << " n" << that << " [";
switch (that->assertion_type()) {
case AssertionNode::AT_END:
os_ << "label=\"$\", shape=septagon";
break;
case AssertionNode::AT_START:
os_ << "label=\"^\", shape=septagon";
break;
case AssertionNode::AT_BOUNDARY:
os_ << "label=\"\\b\", shape=septagon";
break;
case AssertionNode::AT_NON_BOUNDARY:
os_ << "label=\"\\B\", shape=septagon";
break;
case AssertionNode::AFTER_NEWLINE:
os_ << "label=\"(?<=\\n)\", shape=septagon";
break;
}
os_ << "];\n";
PrintAttributes(that);
RegExpNode* successor = that->on_success();
os_ << " n" << that << " -> n" << successor << ";\n";
Visit(successor);
}
void DotPrinter::VisitAction(ActionNode* that) {
os_ << " n" << that << " [";
switch (that->action_type_) {
case ActionNode::SET_REGISTER:
os_ << "label=\"$" << that->data_.u_store_register.reg
<< ":=" << that->data_.u_store_register.value << "\", shape=octagon";
break;
case ActionNode::INCREMENT_REGISTER:
os_ << "label=\"$" << that->data_.u_increment_register.reg
<< "++\", shape=octagon";
break;
case ActionNode::STORE_POSITION:
os_ << "label=\"$" << that->data_.u_position_register.reg
<< ":=$pos\", shape=octagon";
break;
case ActionNode::BEGIN_SUBMATCH:
os_ << "label=\"$" << that->data_.u_submatch.current_position_register
<< ":=$pos,begin\", shape=septagon";
break;
case ActionNode::POSITIVE_SUBMATCH_SUCCESS:
os_ << "label=\"escape\", shape=septagon";
break;
case ActionNode::EMPTY_MATCH_CHECK:
os_ << "label=\"$" << that->data_.u_empty_match_check.start_register
<< "=$pos?,$" << that->data_.u_empty_match_check.repetition_register
<< "<" << that->data_.u_empty_match_check.repetition_limit
<< "?\", shape=septagon";
break;
case ActionNode::CLEAR_CAPTURES: {
os_ << "label=\"clear $" << that->data_.u_clear_captures.range_from
<< " to $" << that->data_.u_clear_captures.range_to
<< "\", shape=septagon";
break;
}
}
os_ << "];\n";
PrintAttributes(that);
RegExpNode* successor = that->on_success();
os_ << " n" << that << " -> n" << successor << ";\n";
Visit(successor);
}
class DispatchTableDumper {
public:
explicit DispatchTableDumper(std::ostream& os) : os_(os) {}
void Call(uc16 key, DispatchTable::Entry entry);
private:
std::ostream& os_;
};
void DispatchTableDumper::Call(uc16 key, DispatchTable::Entry entry) {
os_ << "[" << AsUC16(key) << "-" << AsUC16(entry.to()) << "]: {";
OutSet* set = entry.out_set();
bool first = true;
for (unsigned i = 0; i < OutSet::kFirstLimit; i++) {
if (set->Get(i)) {
if (first) {
first = false;
} else {
os_ << ", ";
}
os_ << i;
}
}
os_ << "}\n";
}
void DispatchTable::Dump() {
OFStream os(stderr);
DispatchTableDumper dumper(os);
tree()->ForEach(&dumper);
}
void RegExpEngine::DotPrint(const char* label,
RegExpNode* node,
bool ignore_case) {
OFStream os(stdout);
DotPrinter printer(os, ignore_case);
printer.PrintNode(label, node);
}
#endif // DEBUG
// -------------------------------------------------------------------
// Tree to graph conversion
RegExpNode* RegExpAtom::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
ZoneList<TextElement>* elms =
new(compiler->zone()) ZoneList<TextElement>(1, compiler->zone());
elms->Add(TextElement::Atom(this), compiler->zone());
return new (compiler->zone())
TextNode(elms, compiler->read_backward(), on_success);
}
RegExpNode* RegExpText::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
return new (compiler->zone())
TextNode(elements(), compiler->read_backward(), on_success);
}
static bool CompareInverseRanges(ZoneList<CharacterRange>* ranges,
const int* special_class,
int length) {
length--; // Remove final marker.
DCHECK(special_class[length] == kRangeEndMarker);
DCHECK(ranges->length() != 0);
DCHECK(length != 0);
DCHECK(special_class[0] != 0);
if (ranges->length() != (length >> 1) + 1) {
return false;
}
CharacterRange range = ranges->at(0);
if (range.from() != 0) {
return false;
}
for (int i = 0; i < length; i += 2) {
if (special_class[i] != (range.to() + 1)) {
return false;
}
range = ranges->at((i >> 1) + 1);
if (special_class[i+1] != range.from()) {
return false;
}
}
if (range.to() != String::kMaxCodePoint) {
return false;
}
return true;
}
static bool CompareRanges(ZoneList<CharacterRange>* ranges,
const int* special_class,
int length) {
length--; // Remove final marker.
DCHECK(special_class[length] == kRangeEndMarker);
if (ranges->length() * 2 != length) {
return false;
}
for (int i = 0; i < length; i += 2) {
CharacterRange range = ranges->at(i >> 1);
if (range.from() != special_class[i] ||
range.to() != special_class[i + 1] - 1) {
return false;
}
}
return true;
}
bool RegExpCharacterClass::is_standard(Zone* zone) {
// TODO(lrn): Remove need for this function, by not throwing away information
// along the way.
if (is_negated_) {
return false;
}
if (set_.is_standard()) {
return true;
}
if (CompareRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) {
set_.set_standard_set_type('s');
return true;
}
if (CompareInverseRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) {
set_.set_standard_set_type('S');
return true;
}
if (CompareInverseRanges(set_.ranges(zone),
kLineTerminatorRanges,
kLineTerminatorRangeCount)) {
set_.set_standard_set_type('.');
return true;
}
if (CompareRanges(set_.ranges(zone),
kLineTerminatorRanges,
kLineTerminatorRangeCount)) {
set_.set_standard_set_type('n');
return true;
}
if (CompareRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) {
set_.set_standard_set_type('w');
return true;
}
if (CompareInverseRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) {
set_.set_standard_set_type('W');
return true;
}
return false;
}
UnicodeRangeSplitter::UnicodeRangeSplitter(Zone* zone,
ZoneList<CharacterRange>* base)
: zone_(zone),
table_(zone),
bmp_(nullptr),
lead_surrogates_(nullptr),
trail_surrogates_(nullptr),
non_bmp_(nullptr) {
// The unicode range splitter categorizes given character ranges into:
// - Code points from the BMP representable by one code unit.
// - Code points outside the BMP that need to be split into surrogate pairs.
// - Lone lead surrogates.
// - Lone trail surrogates.
// Lone surrogates are valid code points, even though no actual characters.
// They require special matching to make sure we do not split surrogate pairs.
// We use the dispatch table to accomplish this. The base range is split up
// by the table by the overlay ranges, and the Call callback is used to
// filter and collect ranges for each category.
for (int i = 0; i < base->length(); i++) {
table_.AddRange(base->at(i), kBase, zone_);
}
// Add overlay ranges.
table_.AddRange(CharacterRange::Range(0, kLeadSurrogateStart - 1),
kBmpCodePoints, zone_);
table_.AddRange(CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd),
kLeadSurrogates, zone_);
table_.AddRange(
CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd),
kTrailSurrogates, zone_);
table_.AddRange(
CharacterRange::Range(kTrailSurrogateEnd + 1, kNonBmpStart - 1),
kBmpCodePoints, zone_);
table_.AddRange(CharacterRange::Range(kNonBmpStart, kNonBmpEnd),
kNonBmpCodePoints, zone_);
table_.ForEach(this);
}
void UnicodeRangeSplitter::Call(uc32 from, DispatchTable::Entry entry) {
OutSet* outset = entry.out_set();
if (!outset->Get(kBase)) return;
ZoneList<CharacterRange>** target = NULL;
if (outset->Get(kBmpCodePoints)) {
target = &bmp_;
} else if (outset->Get(kLeadSurrogates)) {
target = &lead_surrogates_;
} else if (outset->Get(kTrailSurrogates)) {
target = &trail_surrogates_;
} else {
DCHECK(outset->Get(kNonBmpCodePoints));
target = &non_bmp_;
}
if (*target == NULL) *target = new (zone_) ZoneList<CharacterRange>(2, zone_);
(*target)->Add(CharacterRange::Range(entry.from(), entry.to()), zone_);
}
void AddBmpCharacters(RegExpCompiler* compiler, ChoiceNode* result,
RegExpNode* on_success, UnicodeRangeSplitter* splitter) {
ZoneList<CharacterRange>* bmp = splitter->bmp();
if (bmp == nullptr) return;
result->AddAlternative(GuardedAlternative(TextNode::CreateForCharacterRanges(
compiler->zone(), bmp, compiler->read_backward(), on_success)));
}
void AddNonBmpSurrogatePairs(RegExpCompiler* compiler, ChoiceNode* result,
RegExpNode* on_success,
UnicodeRangeSplitter* splitter) {
ZoneList<CharacterRange>* non_bmp = splitter->non_bmp();
if (non_bmp == nullptr) return;
DCHECK(compiler->unicode());
DCHECK(!compiler->one_byte());
Zone* zone = compiler->zone();
CharacterRange::Canonicalize(non_bmp);
for (int i = 0; i < non_bmp->length(); i++) {
// Match surrogate pair.
// E.g. [\u10005-\u11005] becomes
// \ud800[\udc05-\udfff]|
// [\ud801-\ud803][\udc00-\udfff]|
// \ud804[\udc00-\udc05]
uc32 from = non_bmp->at(i).from();
uc32 to = non_bmp->at(i).to();
uc16 from_l = unibrow::Utf16::LeadSurrogate(from);
uc16 from_t = unibrow::Utf16::TrailSurrogate(from);
uc16 to_l = unibrow::Utf16::LeadSurrogate(to);
uc16 to_t = unibrow::Utf16::TrailSurrogate(to);
if (from_l == to_l) {
// The lead surrogate is the same.
result->AddAlternative(
GuardedAlternative(TextNode::CreateForSurrogatePair(
zone, CharacterRange::Singleton(from_l),
CharacterRange::Range(from_t, to_t), compiler->read_backward(),
on_success)));
} else {
if (from_t != kTrailSurrogateStart) {
// Add [from_l][from_t-\udfff]
result->AddAlternative(
GuardedAlternative(TextNode::CreateForSurrogatePair(
zone, CharacterRange::Singleton(from_l),
CharacterRange::Range(from_t, kTrailSurrogateEnd),
compiler->read_backward(), on_success)));
from_l++;
}
if (to_t != kTrailSurrogateEnd) {
// Add [to_l][\udc00-to_t]
result->AddAlternative(
GuardedAlternative(TextNode::CreateForSurrogatePair(
zone, CharacterRange::Singleton(to_l),
CharacterRange::Range(kTrailSurrogateStart, to_t),
compiler->read_backward(), on_success)));
to_l--;
}
if (from_l <= to_l) {
// Add [from_l-to_l][\udc00-\udfff]
result->AddAlternative(
GuardedAlternative(TextNode::CreateForSurrogatePair(
zone, CharacterRange::Range(from_l, to_l),
CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd),
compiler->read_backward(), on_success)));
}
}
}
}
RegExpNode* NegativeLookaroundAgainstReadDirectionAndMatch(
RegExpCompiler* compiler, ZoneList<CharacterRange>* lookbehind,
ZoneList<CharacterRange>* match, RegExpNode* on_success,
bool read_backward) {
Zone* zone = compiler->zone();
RegExpNode* match_node = TextNode::CreateForCharacterRanges(
zone, match, read_backward, on_success);
int stack_register = compiler->UnicodeLookaroundStackRegister();
int position_register = compiler->UnicodeLookaroundPositionRegister();
RegExpLookaround::Builder lookaround(false, match_node, stack_register,
position_register);
RegExpNode* negative_match = TextNode::CreateForCharacterRanges(
zone, lookbehind, !read_backward, lookaround.on_match_success());
return lookaround.ForMatch(negative_match);
}
RegExpNode* MatchAndNegativeLookaroundInReadDirection(
RegExpCompiler* compiler, ZoneList<CharacterRange>* match,
ZoneList<CharacterRange>* lookahead, RegExpNode* on_success,
bool read_backward) {
Zone* zone = compiler->zone();
int stack_register = compiler->UnicodeLookaroundStackRegister();
int position_register = compiler->UnicodeLookaroundPositionRegister();
RegExpLookaround::Builder lookaround(false, on_success, stack_register,
position_register);
RegExpNode* negative_match = TextNode::CreateForCharacterRanges(
zone, lookahead, read_backward, lookaround.on_match_success());
return TextNode::CreateForCharacterRanges(
zone, match, read_backward, lookaround.ForMatch(negative_match));
}
void AddLoneLeadSurrogates(RegExpCompiler* compiler, ChoiceNode* result,
RegExpNode* on_success,
UnicodeRangeSplitter* splitter) {
ZoneList<CharacterRange>* lead_surrogates = splitter->lead_surrogates();
if (lead_surrogates == nullptr) return;
Zone* zone = compiler->zone();
// E.g. \ud801 becomes \ud801(?![\udc00-\udfff]).
ZoneList<CharacterRange>* trail_surrogates = CharacterRange::List(
zone, CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd));
RegExpNode* match;
if (compiler->read_backward()) {
// Reading backward. Assert that reading forward, there is no trail
// surrogate, and then backward match the lead surrogate.
match = NegativeLookaroundAgainstReadDirectionAndMatch(
compiler, trail_surrogates, lead_surrogates, on_success, true);
} else {
// Reading forward. Forward match the lead surrogate and assert that
// no trail surrogate follows.
match = MatchAndNegativeLookaroundInReadDirection(
compiler, lead_surrogates, trail_surrogates, on_success, false);
}
result->AddAlternative(GuardedAlternative(match));
}
void AddLoneTrailSurrogates(RegExpCompiler* compiler, ChoiceNode* result,
RegExpNode* on_success,
UnicodeRangeSplitter* splitter) {
ZoneList<CharacterRange>* trail_surrogates = splitter->trail_surrogates();
if (trail_surrogates == nullptr) return;
Zone* zone = compiler->zone();
// E.g. \udc01 becomes (?<![\ud800-\udbff])\udc01
ZoneList<CharacterRange>* lead_surrogates = CharacterRange::List(
zone, CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd));
RegExpNode* match;
if (compiler->read_backward()) {
// Reading backward. Backward match the trail surrogate and assert that no
// lead surrogate precedes it.
match = MatchAndNegativeLookaroundInReadDirection(
compiler, trail_surrogates, lead_surrogates, on_success, true);
} else {
// Reading forward. Assert that reading backward, there is no lead
// surrogate, and then forward match the trail surrogate.
match = NegativeLookaroundAgainstReadDirectionAndMatch(
compiler, lead_surrogates, trail_surrogates, on_success, false);
}
result->AddAlternative(GuardedAlternative(match));
}
RegExpNode* UnanchoredAdvance(RegExpCompiler* compiler,
RegExpNode* on_success) {
// This implements ES2015 21.2.5.2.3, AdvanceStringIndex.
DCHECK(!compiler->read_backward());
Zone* zone = compiler->zone();
// Advance any character. If the character happens to be a lead surrogate and
// we advanced into the middle of a surrogate pair, it will work out, as
// nothing will match from there. We will have to advance again, consuming
// the associated trail surrogate.
ZoneList<CharacterRange>* range = CharacterRange::List(
zone, CharacterRange::Range(0, String::kMaxUtf16CodeUnit));
return TextNode::CreateForCharacterRanges(zone, range, false, on_success);
}
void AddUnicodeCaseEquivalents(RegExpCompiler* compiler,
ZoneList<CharacterRange>* ranges) {
#ifdef V8_I18N_SUPPORT
// Use ICU to compute the case fold closure over the ranges.
DCHECK(compiler->unicode());
DCHECK(compiler->ignore_case());
USet* set = uset_openEmpty();
for (int i = 0; i < ranges->length(); i++) {
uset_addRange(set, ranges->at(i).from(), ranges->at(i).to());
}
ranges->Clear();
uset_closeOver(set, USET_CASE_INSENSITIVE);
// Full case mapping map single characters to multiple characters.
// Those are represented as strings in the set. Remove them so that
// we end up with only simple and common case mappings.
uset_removeAllStrings(set);
int item_count = uset_getItemCount(set);
int item_result = 0;
UErrorCode ec = U_ZERO_ERROR;
Zone* zone = compiler->zone();
for (int i = 0; i < item_count; i++) {
uc32 start = 0;
uc32 end = 0;
item_result += uset_getItem(set, i, &start, &end, nullptr, 0, &ec);
ranges->Add(CharacterRange::Range(start, end), zone);
}
// No errors and everything we collected have been ranges.
DCHECK_EQ(U_ZERO_ERROR, ec);
DCHECK_EQ(0, item_result);
uset_close(set);
#else
// Fallback if ICU is not included.
CharacterRange::AddCaseEquivalents(compiler->isolate(), compiler->zone(),
ranges, compiler->one_byte());
#endif // V8_I18N_SUPPORT
CharacterRange::Canonicalize(ranges);
}
RegExpNode* RegExpCharacterClass::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
set_.Canonicalize();
Zone* zone = compiler->zone();
ZoneList<CharacterRange>* ranges = this->ranges(zone);
if (compiler->unicode() && compiler->ignore_case()) {
AddUnicodeCaseEquivalents(compiler, ranges);
}
if (compiler->unicode() && !compiler->one_byte()) {
if (is_negated()) {
ZoneList<CharacterRange>* negated =
new (zone) ZoneList<CharacterRange>(2, zone);
CharacterRange::Negate(ranges, negated, zone);
ranges = negated;
}
if (ranges->length() == 0) {
ranges->Add(CharacterRange::Everything(), zone);
RegExpCharacterClass* fail =
new (zone) RegExpCharacterClass(ranges, true);
return new (zone) TextNode(fail, compiler->read_backward(), on_success);
}
if (standard_type() == '*') {
return UnanchoredAdvance(compiler, on_success);
} else {
ChoiceNode* result = new (zone) ChoiceNode(2, zone);
UnicodeRangeSplitter splitter(zone, ranges);
AddBmpCharacters(compiler, result, on_success, &splitter);
AddNonBmpSurrogatePairs(compiler, result, on_success, &splitter);
AddLoneLeadSurrogates(compiler, result, on_success, &splitter);
AddLoneTrailSurrogates(compiler, result, on_success, &splitter);
return result;
}
} else {
return new (zone) TextNode(this, compiler->read_backward(), on_success);
}
}
int CompareFirstChar(RegExpTree* const* a, RegExpTree* const* b) {
RegExpAtom* atom1 = (*a)->AsAtom();
RegExpAtom* atom2 = (*b)->AsAtom();
uc16 character1 = atom1->data().at(0);
uc16 character2 = atom2->data().at(0);
if (character1 < character2) return -1;
if (character1 > character2) return 1;
return 0;
}
static unibrow::uchar Canonical(
unibrow::Mapping<unibrow::Ecma262Canonicalize>* canonicalize,
unibrow::uchar c) {
unibrow::uchar chars[unibrow::Ecma262Canonicalize::kMaxWidth];
int length = canonicalize->get(c, '\0', chars);
DCHECK_LE(length, 1);
unibrow::uchar canonical = c;
if (length == 1) canonical = chars[0];
return canonical;
}
int CompareFirstCharCaseIndependent(
unibrow::Mapping<unibrow::Ecma262Canonicalize>* canonicalize,
RegExpTree* const* a, RegExpTree* const* b) {
RegExpAtom* atom1 = (*a)->AsAtom();
RegExpAtom* atom2 = (*b)->AsAtom();
unibrow::uchar character1 = atom1->data().at(0);
unibrow::uchar character2 = atom2->data().at(0);
if (character1 == character2) return 0;
if (character1 >= 'a' || character2 >= 'a') {
character1 = Canonical(canonicalize, character1);
character2 = Canonical(canonicalize, character2);
}
return static_cast<int>(character1) - static_cast<int>(character2);
}
// We can stable sort runs of atoms, since the order does not matter if they
// start with different characters.
// Returns true if any consecutive atoms were found.
bool RegExpDisjunction::SortConsecutiveAtoms(RegExpCompiler* compiler) {
ZoneList<RegExpTree*>* alternatives = this->alternatives();
int length = alternatives->length();
bool found_consecutive_atoms = false;
for (int i = 0; i < length; i++) {
while (i < length) {
RegExpTree* alternative = alternatives->at(i);
if (alternative->IsAtom()) break;
i++;
}
// i is length or it is the index of an atom.
if (i == length) break;
int first_atom = i;
i++;
while (i < length) {
RegExpTree* alternative = alternatives->at(i);
if (!alternative->IsAtom()) break;
i++;
}
// Sort atoms to get ones with common prefixes together.
// This step is more tricky if we are in a case-independent regexp,
// because it would change /is|I/ to /I|is/, and order matters when
// the regexp parts don't match only disjoint starting points. To fix
// this we have a version of CompareFirstChar that uses case-
// independent character classes for comparison.
DCHECK_LT(first_atom, alternatives->length());
DCHECK_LE(i, alternatives->length());
DCHECK_LE(first_atom, i);
if (compiler->ignore_case()) {
unibrow::Mapping<unibrow::Ecma262Canonicalize>* canonicalize =
compiler->isolate()->regexp_macro_assembler_canonicalize();
auto compare_closure =
[canonicalize](RegExpTree* const* a, RegExpTree* const* b) {
return CompareFirstCharCaseIndependent(canonicalize, a, b);
};
alternatives->StableSort(compare_closure, first_atom, i - first_atom);
} else {
alternatives->StableSort(CompareFirstChar, first_atom, i - first_atom);
}
if (i - first_atom > 1) found_consecutive_atoms = true;
}
return found_consecutive_atoms;
}
// Optimizes ab|ac|az to a(?:b|c|d).
void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) {
Zone* zone = compiler->zone();
ZoneList<RegExpTree*>* alternatives = this->alternatives();
int length = alternatives->length();
int write_posn = 0;
int i = 0;
while (i < length) {
RegExpTree* alternative = alternatives->at(i);
if (!alternative->IsAtom()) {
alternatives->at(write_posn++) = alternatives->at(i);
i++;
continue;
}
RegExpAtom* atom = alternative->AsAtom();
unibrow::uchar common_prefix = atom->data().at(0);
int first_with_prefix = i;
int prefix_length = atom->length();
i++;
while (i < length) {
alternative = alternatives->at(i);
if (!alternative->IsAtom()) break;
atom = alternative->AsAtom();
unibrow::uchar new_prefix = atom->data().at(0);
if (new_prefix != common_prefix) {
if (!compiler->ignore_case()) break;
unibrow::Mapping<unibrow::Ecma262Canonicalize>* canonicalize =
compiler->isolate()->regexp_macro_assembler_canonicalize();
new_prefix = Canonical(canonicalize, new_prefix);
common_prefix = Canonical(canonicalize, common_prefix);
if (new_prefix != common_prefix) break;
}
prefix_length = Min(prefix_length, atom->length());
i++;
}
if (i > first_with_prefix + 2) {
// Found worthwhile run of alternatives with common prefix of at least one
// character. The sorting function above did not sort on more than one
// character for reasons of correctness, but there may still be a longer
// common prefix if the terms were similar or presorted in the input.
// Find out how long the common prefix is.
int run_length = i - first_with_prefix;
atom = alternatives->at(first_with_prefix)->AsAtom();
for (int j = 1; j < run_length && prefix_length > 1; j++) {
RegExpAtom* old_atom =
alternatives->at(j + first_with_prefix)->AsAtom();
for (int k = 1; k < prefix_length; k++) {
if (atom->data().at(k) != old_atom->data().at(k)) {
prefix_length = k;
break;
}
}
}
RegExpAtom* prefix =
new (zone) RegExpAtom(atom->data().SubVector(0, prefix_length));
ZoneList<RegExpTree*>* pair = new (zone) ZoneList<RegExpTree*>(2, zone);
pair->Add(prefix, zone);
ZoneList<RegExpTree*>* suffixes =
new (zone) ZoneList<RegExpTree*>(run_length, zone);
for (int j = 0; j < run_length; j++) {
RegExpAtom* old_atom =
alternatives->at(j + first_with_prefix)->AsAtom();
int len = old_atom->length();
if (len == prefix_length) {
suffixes->Add(new (zone) RegExpEmpty(), zone);
} else {
RegExpTree* suffix = new (zone) RegExpAtom(
old_atom->data().SubVector(prefix_length, old_atom->length()));
suffixes->Add(suffix, zone);
}
}
pair->Add(new (zone) RegExpDisjunction(suffixes), zone);
alternatives->at(write_posn++) = new (zone) RegExpAlternative(pair);
} else {
// Just copy any non-worthwhile alternatives.
for (int j = first_with_prefix; j < i; j++) {
alternatives->at(write_posn++) = alternatives->at(j);
}
}
}
alternatives->Rewind(write_posn); // Trim end of array.
}
// Optimizes b|c|z to [bcz].
void RegExpDisjunction::FixSingleCharacterDisjunctions(
RegExpCompiler* compiler) {
Zone* zone = compiler->zone();
ZoneList<RegExpTree*>* alternatives = this->alternatives();
int length = alternatives->length();
int write_posn = 0;
int i = 0;
while (i < length) {
RegExpTree* alternative = alternatives->at(i);
if (!alternative->IsAtom()) {
alternatives->at(write_posn++) = alternatives->at(i);
i++;
continue;
}
RegExpAtom* atom = alternative->AsAtom();
if (atom->length() != 1) {
alternatives->at(write_posn++) = alternatives->at(i);
i++;
continue;
}
int first_in_run = i;
i++;
while (i < length) {
alternative = alternatives->at(i);
if (!alternative->IsAtom()) break;
atom = alternative->AsAtom();
if (atom->length() != 1) break;
i++;
}
if (i > first_in_run + 1) {
// Found non-trivial run of single-character alternatives.
int run_length = i - first_in_run;
ZoneList<CharacterRange>* ranges =
new (zone) ZoneList<CharacterRange>(2, zone);
for (int j = 0; j < run_length; j++) {
RegExpAtom* old_atom = alternatives->at(j + first_in_run)->AsAtom();
DCHECK_EQ(old_atom->length(), 1);
ranges->Add(CharacterRange::Singleton(old_atom->data().at(0)), zone);
}
alternatives->at(write_posn++) =
new (zone) RegExpCharacterClass(ranges, false);
} else {
// Just copy any trivial alternatives.
for (int j = first_in_run; j < i; j++) {
alternatives->at(write_posn++) = alternatives->at(j);
}
}
}
alternatives->Rewind(write_posn); // Trim end of array.
}
RegExpNode* RegExpDisjunction::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
ZoneList<RegExpTree*>* alternatives = this->alternatives();
if (alternatives->length() > 2) {
bool found_consecutive_atoms = SortConsecutiveAtoms(compiler);
if (found_consecutive_atoms) RationalizeConsecutiveAtoms(compiler);
FixSingleCharacterDisjunctions(compiler);
if (alternatives->length() == 1) {
return alternatives->at(0)->ToNode(compiler, on_success);
}
}
int length = alternatives->length();
ChoiceNode* result =
new(compiler->zone()) ChoiceNode(length, compiler->zone());
for (int i = 0; i < length; i++) {
GuardedAlternative alternative(alternatives->at(i)->ToNode(compiler,
on_success));
result->AddAlternative(alternative);
}
return result;
}
RegExpNode* RegExpQuantifier::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
return ToNode(min(),
max(),
is_greedy(),
body(),
compiler,
on_success);
}
// Scoped object to keep track of how much we unroll quantifier loops in the
// regexp graph generator.
class RegExpExpansionLimiter {
public:
static const int kMaxExpansionFactor = 6;
RegExpExpansionLimiter(RegExpCompiler* compiler, int factor)
: compiler_(compiler),
saved_expansion_factor_(compiler->current_expansion_factor()),
ok_to_expand_(saved_expansion_factor_ <= kMaxExpansionFactor) {
DCHECK(factor > 0);
if (ok_to_expand_) {
if (factor > kMaxExpansionFactor) {
// Avoid integer overflow of the current expansion factor.
ok_to_expand_ = false;
compiler->set_current_expansion_factor(kMaxExpansionFactor + 1);
} else {
int new_factor = saved_expansion_factor_ * factor;
ok_to_expand_ = (new_factor <= kMaxExpansionFactor);
compiler->set_current_expansion_factor(new_factor);
}
}
}
~RegExpExpansionLimiter() {
compiler_->set_current_expansion_factor(saved_expansion_factor_);
}
bool ok_to_expand() { return ok_to_expand_; }
private:
RegExpCompiler* compiler_;
int saved_expansion_factor_;
bool ok_to_expand_;
DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpExpansionLimiter);
};
RegExpNode* RegExpQuantifier::ToNode(int min,
int max,
bool is_greedy,
RegExpTree* body,
RegExpCompiler* compiler,
RegExpNode* on_success,
bool not_at_start) {
// x{f, t} becomes this:
//
// (r++)<-.
// | `
// | (x)
// v ^
// (r=0)-->(?)---/ [if r < t]
// |
// [if r >= f] \----> ...
//
// 15.10.2.5 RepeatMatcher algorithm.
// The parser has already eliminated the case where max is 0. In the case
// where max_match is zero the parser has removed the quantifier if min was
// > 0 and removed the atom if min was 0. See AddQuantifierToAtom.
// If we know that we cannot match zero length then things are a little
// simpler since we don't need to make the special zero length match check
// from step 2.1. If the min and max are small we can unroll a little in
// this case.
static const int kMaxUnrolledMinMatches = 3; // Unroll (foo)+ and (foo){3,}
static const int kMaxUnrolledMaxMatches = 3; // Unroll (foo)? and (foo){x,3}
if (max == 0) return on_success; // This can happen due to recursion.
bool body_can_be_empty = (body->min_match() == 0);
int body_start_reg = RegExpCompiler::kNoRegister;
Interval capture_registers = body->CaptureRegisters();
bool needs_capture_clearing = !capture_registers.is_empty();
Zone* zone = compiler->zone();
if (body_can_be_empty) {
body_start_reg = compiler->AllocateRegister();
} else if (compiler->optimize() && !needs_capture_clearing) {
// Only unroll if there are no captures and the body can't be
// empty.
{
RegExpExpansionLimiter limiter(
compiler, min + ((max != min) ? 1 : 0));
if (min > 0 && min <= kMaxUnrolledMinMatches && limiter.ok_to_expand()) {
int new_max = (max == kInfinity) ? max : max - min;
// Recurse once to get the loop or optional matches after the fixed
// ones.
RegExpNode* answer = ToNode(
0, new_max, is_greedy, body, compiler, on_success, true);
// Unroll the forced matches from 0 to min. This can cause chains of
// TextNodes (which the parser does not generate). These should be
// combined if it turns out they hinder good code generation.
for (int i = 0; i < min; i++) {
answer = body->ToNode(compiler, answer);
}
return answer;
}
}
if (max <= kMaxUnrolledMaxMatches && min == 0) {
DCHECK(max > 0); // Due to the 'if' above.
RegExpExpansionLimiter limiter(compiler, max);
if (limiter.ok_to_expand()) {
// Unroll the optional matches up to max.
RegExpNode* answer = on_success;
for (int i = 0; i < max; i++) {
ChoiceNode* alternation = new(zone) ChoiceNode(2, zone);
if (is_greedy) {
alternation->AddAlternative(
GuardedAlternative(body->ToNode(compiler, answer)));
alternation->AddAlternative(GuardedAlternative(on_success));
} else {
alternation->AddAlternative(GuardedAlternative(on_success));
alternation->AddAlternative(
GuardedAlternative(body->ToNode(compiler, answer)));
}
answer = alternation;
if (not_at_start && !compiler->read_backward()) {
alternation->set_not_at_start();
}
}
return answer;
}
}
}
bool has_min = min > 0;
bool has_max = max < RegExpTree::kInfinity;
bool needs_counter = has_min || has_max;
int reg_ctr = needs_counter
? compiler->AllocateRegister()
: RegExpCompiler::kNoRegister;
LoopChoiceNode* center = new (zone)
LoopChoiceNode(body->min_match() == 0, compiler->read_backward(), zone);
if (not_at_start && !compiler->read_backward()) center->set_not_at_start();
RegExpNode* loop_return = needs_counter
? static_cast<RegExpNode*>(ActionNode::IncrementRegister(reg_ctr, center))
: static_cast<RegExpNode*>(center);
if (body_can_be_empty) {
// If the body can be empty we need to check if it was and then
// backtrack.
loop_return = ActionNode::EmptyMatchCheck(body_start_reg,
reg_ctr,
min,
loop_return);
}
RegExpNode* body_node = body->ToNode(compiler, loop_return);
if (body_can_be_empty) {
// If the body can be empty we need to store the start position
// so we can bail out if it was empty.
body_node = ActionNode::StorePosition(body_start_reg, false, body_node);
}
if (needs_capture_clearing) {
// Before entering the body of this loop we need to clear captures.
body_node = ActionNode::ClearCaptures(capture_registers, body_node);
}
GuardedAlternative body_alt(body_node);
if (has_max) {
Guard* body_guard =
new(zone) Guard(reg_ctr, Guard::LT, max);
body_alt.AddGuard(body_guard, zone);
}
GuardedAlternative rest_alt(on_success);
if (has_min) {
Guard* rest_guard = new(compiler->zone()) Guard(reg_ctr, Guard::GEQ, min);
rest_alt.AddGuard(rest_guard, zone);
}
if (is_greedy) {
center->AddLoopAlternative(body_alt);
center->AddContinueAlternative(rest_alt);
} else {
center->AddContinueAlternative(rest_alt);
center->AddLoopAlternative(body_alt);
}
if (needs_counter) {
return ActionNode::SetRegister(reg_ctr, 0, center);
} else {
return center;
}
}
RegExpNode* RegExpAssertion::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
NodeInfo info;
Zone* zone = compiler->zone();
switch (assertion_type()) {
case START_OF_LINE:
return AssertionNode::AfterNewline(on_success);
case START_OF_INPUT:
return AssertionNode::AtStart(on_success);
case BOUNDARY:
return AssertionNode::AtBoundary(on_success);
case NON_BOUNDARY:
return AssertionNode::AtNonBoundary(on_success);
case END_OF_INPUT:
return AssertionNode::AtEnd(on_success);
case END_OF_LINE: {
// Compile $ in multiline regexps as an alternation with a positive
// lookahead in one side and an end-of-input on the other side.
// We need two registers for the lookahead.
int stack_pointer_register = compiler->AllocateRegister();
int position_register = compiler->AllocateRegister();
// The ChoiceNode to distinguish between a newline and end-of-input.
ChoiceNode* result = new(zone) ChoiceNode(2, zone);
// Create a newline atom.
ZoneList<CharacterRange>* newline_ranges =
new(zone) ZoneList<CharacterRange>(3, zone);
CharacterRange::AddClassEscape('n', newline_ranges, zone);
RegExpCharacterClass* newline_atom = new (zone) RegExpCharacterClass('n');
TextNode* newline_matcher = new (zone) TextNode(
newline_atom, false, ActionNode::PositiveSubmatchSuccess(
stack_pointer_register, position_register,
0, // No captures inside.
-1, // Ignored if no captures.
on_success));
// Create an end-of-input matcher.
RegExpNode* end_of_line = ActionNode::BeginSubmatch(
stack_pointer_register,
position_register,
newline_matcher);
// Add the two alternatives to the ChoiceNode.
GuardedAlternative eol_alternative(end_of_line);
result->AddAlternative(eol_alternative);
GuardedAlternative end_alternative(AssertionNode::AtEnd(on_success));
result->AddAlternative(end_alternative);
return result;
}
default:
UNREACHABLE();
}
return on_success;
}
RegExpNode* RegExpBackReference::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
return new (compiler->zone())
BackReferenceNode(RegExpCapture::StartRegister(index()),
RegExpCapture::EndRegister(index()),
compiler->read_backward(), on_success);
}
RegExpNode* RegExpEmpty::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
return on_success;
}
RegExpLookaround::Builder::Builder(bool is_positive, RegExpNode* on_success,
int stack_pointer_register,
int position_register,
int capture_register_count,
int capture_register_start)
: is_positive_(is_positive),
on_success_(on_success),
stack_pointer_register_(stack_pointer_register),
position_register_(position_register) {
if (is_positive_) {
on_match_success_ = ActionNode::PositiveSubmatchSuccess(
stack_pointer_register, position_register, capture_register_count,
capture_register_start, on_success_);
} else {
Zone* zone = on_success_->zone();
on_match_success_ = new (zone) NegativeSubmatchSuccess(
stack_pointer_register, position_register, capture_register_count,
capture_register_start, zone);
}
}
RegExpNode* RegExpLookaround::Builder::ForMatch(RegExpNode* match) {
if (is_positive_) {
return ActionNode::BeginSubmatch(stack_pointer_register_,
position_register_, match);
} else {
Zone* zone = on_success_->zone();
// We use a ChoiceNode to represent the negative lookaround. The first
// alternative is the negative match. On success, the end node backtracks.
// On failure, the second alternative is tried and leads to success.
// NegativeLookaheadChoiceNode is a special ChoiceNode that ignores the
// first exit when calculating quick checks.
ChoiceNode* choice_node = new (zone) NegativeLookaroundChoiceNode(
GuardedAlternative(match), GuardedAlternative(on_success_), zone);
return ActionNode::BeginSubmatch(stack_pointer_register_,
position_register_, choice_node);
}
}
RegExpNode* RegExpLookaround::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
int stack_pointer_register = compiler->AllocateRegister();
int position_register = compiler->AllocateRegister();
const int registers_per_capture = 2;
const int register_of_first_capture = 2;
int register_count = capture_count_ * registers_per_capture;
int register_start =
register_of_first_capture + capture_from_ * registers_per_capture;
RegExpNode* result;
bool was_reading_backward = compiler->read_backward();
compiler->set_read_backward(type() == LOOKBEHIND);
Builder builder(is_positive(), on_success, stack_pointer_register,
position_register, register_count, register_start);
RegExpNode* match = body_->ToNode(compiler, builder.on_match_success());
result = builder.ForMatch(match);
compiler->set_read_backward(was_reading_backward);
return result;
}
RegExpNode* RegExpCapture::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
return ToNode(body(), index(), compiler, on_success);
}
RegExpNode* RegExpCapture::ToNode(RegExpTree* body,
int index,
RegExpCompiler* compiler,
RegExpNode* on_success) {
DCHECK_NOT_NULL(body);
int start_reg = RegExpCapture::StartRegister(index);
int end_reg = RegExpCapture::EndRegister(index);
if (compiler->read_backward()) std::swap(start_reg, end_reg);
RegExpNode* store_end = ActionNode::StorePosition(end_reg, true, on_success);
RegExpNode* body_node = body->ToNode(compiler, store_end);
return ActionNode::StorePosition(start_reg, true, body_node);
}
RegExpNode* RegExpAlternative::ToNode(RegExpCompiler* compiler,
RegExpNode* on_success) {
ZoneList<RegExpTree*>* children = nodes();
RegExpNode* current = on_success;
if (compiler->read_backward()) {
for (int i = 0; i < children->length(); i++) {
current = children->at(i)->ToNode(compiler, current);
}
} else {
for (int i = children->length() - 1; i >= 0; i--) {
current = children->at(i)->ToNode(compiler, current);
}
}
return current;
}
static void AddClass(const int* elmv,
int elmc,
ZoneList<CharacterRange>* ranges,
Zone* zone) {
elmc--;
DCHECK(elmv[elmc] == kRangeEndMarker);
for (int i = 0; i < elmc; i += 2) {
DCHECK(elmv[i] < elmv[i + 1]);
ranges->Add(CharacterRange::Range(elmv[i], elmv[i + 1] - 1), zone);
}
}
static void AddClassNegated(const int *elmv,
int elmc,
ZoneList<CharacterRange>* ranges,
Zone* zone) {
elmc--;
DCHECK(elmv[elmc] == kRangeEndMarker);
DCHECK(elmv[0] != 0x0000);
DCHECK(elmv[elmc - 1] != String::kMaxCodePoint);
uc16 last = 0x0000;
for (int i = 0; i < elmc; i += 2) {
DCHECK(last <= elmv[i] - 1);
DCHECK(elmv[i] < elmv[i + 1]);
ranges->Add(CharacterRange::Range(last, elmv[i] - 1), zone);
last = elmv[i + 1];
}
ranges->Add(CharacterRange::Range(last, String::kMaxCodePoint), zone);
}
void CharacterRange::AddClassEscape(uc16 type,
ZoneList<CharacterRange>* ranges,
Zone* zone) {
switch (type) {
case 's':
AddClass(kSpaceRanges, kSpaceRangeCount, ranges, zone);
break;
case 'S':
AddClassNegated(kSpaceRanges, kSpaceRangeCount, ranges, zone);
break;
case 'w':
AddClass(kWordRanges, kWordRangeCount, ranges, zone);
break;
case 'W':
AddClassNegated(kWordRanges, kWordRangeCount, ranges, zone);
break;
case 'd':
AddClass(kDigitRanges, kDigitRangeCount, ranges, zone);
break;
case 'D':
AddClassNegated(kDigitRanges, kDigitRangeCount, ranges, zone);
break;
case '.':
AddClassNegated(kLineTerminatorRanges,
kLineTerminatorRangeCount,
ranges,
zone);
break;
// This is not a character range as defined by the spec but a
// convenient shorthand for a character class that matches any
// character.
case '*':
ranges->Add(CharacterRange::Everything(), zone);
break;
// This is the set of characters matched by the $ and ^ symbols
// in multiline mode.
case 'n':
AddClass(kLineTerminatorRanges,
kLineTerminatorRangeCount,
ranges,
zone);
break;
default:
UNREACHABLE();
}
}
Vector<const int> CharacterRange::GetWordBounds() {
return Vector<const int>(kWordRanges, kWordRangeCount - 1);
}
void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone,
ZoneList<CharacterRange>* ranges,
bool is_one_byte) {
CharacterRange::Canonicalize(ranges);
int range_count = ranges->length();
for (int i = 0; i < range_count; i++) {
CharacterRange range = ranges->at(i);
uc32 bottom = range.from();
if (bottom > String::kMaxUtf16CodeUnit) return;
uc32 top = Min(range.to(), String::kMaxUtf16CodeUnit);
// Nothing to be done for surrogates.
if (bottom >= kLeadSurrogateStart && top <= kTrailSurrogateEnd) return;
if (is_one_byte && !RangeContainsLatin1Equivalents(range)) {
if (bottom > String::kMaxOneByteCharCode) return;
if (top > String::kMaxOneByteCharCode) top = String::kMaxOneByteCharCode;
}
unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
if (top == bottom) {
// If this is a singleton we just expand the one character.
int length = isolate->jsregexp_uncanonicalize()->get(bottom, '\0', chars);
for (int i = 0; i < length; i++) {
uc32 chr = chars[i];
if (chr != bottom) {
ranges->Add(CharacterRange::Singleton(chars[i]), zone);
}
}
} else {
// If this is a range we expand the characters block by block, expanding
// contiguous subranges (blocks) one at a time. The approach is as
// follows. For a given start character we look up the remainder of the
// block that contains it (represented by the end point), for instance we
// find 'z' if the character is 'c'. A block is characterized by the
// property that all characters uncanonicalize in the same way, except
// that each entry in the result is incremented by the distance from the
// first element. So a-z is a block because 'a' uncanonicalizes to ['a',
// 'A'] and the k'th letter uncanonicalizes to ['a' + k, 'A' + k]. Once
// we've found the end point we look up its uncanonicalization and
// produce a range for each element. For instance for [c-f] we look up
// ['z', 'Z'] and produce [c-f] and [C-F]. We then only add a range if
// it is not already contained in the input, so [c-f] will be skipped but
// [C-F] will be added. If this range is not completely contained in a
// block we do this for all the blocks covered by the range (handling
// characters that is not in a block as a "singleton block").
unibrow::uchar equivalents[unibrow::Ecma262UnCanonicalize::kMaxWidth];
int pos = bottom;
while (pos <= top) {
int length =
isolate->jsregexp_canonrange()->get(pos, '\0', equivalents);
uc32 block_end;
if (length == 0) {
block_end = pos;
} else {
DCHECK_EQ(1, length);
block_end = equivalents[0];
}
int end = (block_end > top) ? top : block_end;
length = isolate->jsregexp_uncanonicalize()->get(block_end, '\0',
equivalents);
for (int i = 0; i < length; i++) {
uc32 c = equivalents[i];
uc32 range_from = c - (block_end - pos);
uc32 range_to = c - (block_end - end);
if (!(bottom <= range_from && range_to <= top)) {
ranges->Add(CharacterRange::Range(range_from, range_to), zone);
}
}
pos = end + 1;
}
}
}
}
bool CharacterRange::IsCanonical(ZoneList<CharacterRange>* ranges) {
DCHECK_NOT_NULL(ranges);
int n = ranges->length();
if (n <= 1) return true;
int max = ranges->at(0).to();
for (int i = 1; i < n; i++) {
CharacterRange next_range = ranges->at(i);
if (next_range.from() <= max + 1) return false;
max = next_range.to();
}
return true;
}
ZoneList<CharacterRange>* CharacterSet::ranges(Zone* zone) {
if (ranges_ == NULL) {
ranges_ = new(zone) ZoneList<CharacterRange>(2, zone);
CharacterRange::AddClassEscape(standard_set_type_, ranges_, zone);
}
return ranges_;
}
// Move a number of elements in a zonelist to another position
// in the same list. Handles overlapping source and target areas.
static void MoveRanges(ZoneList<CharacterRange>* list,
int from,
int to,
int count) {
// Ranges are potentially overlapping.
if (from < to) {
for (int i = count - 1; i >= 0; i--) {
list->at(to + i) = list->at(from + i);
}
} else {
for (int i = 0; i < count; i++) {
list->at(to + i) = list->at(from + i);
}
}
}
static int InsertRangeInCanonicalList(ZoneList<CharacterRange>* list,
int count,
CharacterRange insert) {
// Inserts a range into list[0..count[, which must be sorted
// by from value and non-overlapping and non-adjacent, using at most
// list[0..count] for the result. Returns the number of resulting
// canonicalized ranges. Inserting a range may collapse existing ranges into
// fewer ranges, so the return value can be anything in the range 1..count+1.
uc32 from = insert.from();
uc32 to = insert.to();
int start_pos = 0;
int end_pos = count;
for (int i = count - 1; i >= 0; i--) {
CharacterRange current = list->at(i);
if (current.from() > to + 1) {
end_pos = i;
} else if (current.to() + 1 < from) {
start_pos = i + 1;
break;
}
}
// Inserted range overlaps, or is adjacent to, ranges at positions
// [start_pos..end_pos[. Ranges before start_pos or at or after end_pos are
// not affected by the insertion.
// If start_pos == end_pos, the range must be inserted before start_pos.
// if start_pos < end_pos, the entire range from start_pos to end_pos
// must be merged with the insert range.
if (start_pos == end_pos) {
// Insert between existing ranges at position start_pos.
if (start_pos < count) {
MoveRanges(list, start_pos, start_pos + 1, count - start_pos);
}
list->at(start_pos) = insert;
return count + 1;
}
if (start_pos + 1 == end_pos) {
// Replace single existing range at position start_pos.
CharacterRange to_replace = list->at(start_pos);
int new_from = Min(to_replace.from(), from);
int new_to = Max(to_replace.to(), to);
list->at(start_pos) = CharacterRange::Range(new_from, new_to);
return count;
}
// Replace a number of existing ranges from start_pos to end_pos - 1.
// Move the remaining ranges down.
int new_from = Min(list->at(start_pos).from(), from);
int new_to = Max(list->at(end_pos - 1).to(), to);
if (end_pos < count) {
MoveRanges(list, end_pos, start_pos + 1, count - end_pos);
}
list->at(start_pos) = CharacterRange::Range(new_from, new_to);
return count - (end_pos - start_pos) + 1;
}
void CharacterSet::Canonicalize() {
// Special/default classes are always considered canonical. The result
// of calling ranges() will be sorted.
if (ranges_ == NULL) return;
CharacterRange::Canonicalize(ranges_);
}
void CharacterRange::Canonicalize(ZoneList<CharacterRange>* character_ranges) {
if (character_ranges->length() <= 1) return;
// Check whether ranges are already canonical (increasing, non-overlapping,
// non-adjacent).
int n = character_ranges->length();
int max = character_ranges->at(0).to();
int i = 1;
while (i < n) {
CharacterRange current = character_ranges->at(i);
if (current.from() <= max + 1) {
break;
}
max = current.to();
i++;
}
// Canonical until the i'th range. If that's all of them, we are done.
if (i == n) return;
// The ranges at index i and forward are not canonicalized. Make them so by
// doing the equivalent of insertion sort (inserting each into the previous
// list, in order).
// Notice that inserting a range can reduce the number of ranges in the
// result due to combining of adjacent and overlapping ranges.
int read = i; // Range to insert.
int num_canonical = i; // Length of canonicalized part of list.
do {
num_canonical = InsertRangeInCanonicalList(character_ranges,
num_canonical,
character_ranges->at(read));
read++;
} while (read < n);
character_ranges->Rewind(num_canonical);
DCHECK(CharacterRange::IsCanonical(character_ranges));
}
void CharacterRange::Negate(ZoneList<CharacterRange>* ranges,
ZoneList<CharacterRange>* negated_ranges,
Zone* zone) {
DCHECK(CharacterRange::IsCanonical(ranges));
DCHECK_EQ(0, negated_ranges->length());
int range_count = ranges->length();
uc32 from = 0;
int i = 0;
if (range_count > 0 && ranges->at(0).from() == 0) {
from = ranges->at(0).to() + 1;
i = 1;
}
while (i < range_count) {
CharacterRange range = ranges->at(i);
negated_ranges->Add(CharacterRange::Range(from, range.from() - 1), zone);
from = range.to() + 1;
i++;
}
if (from < String::kMaxCodePoint) {
negated_ranges->Add(CharacterRange::Range(from, String::kMaxCodePoint),
zone);
}
}
// -------------------------------------------------------------------
// Splay tree
OutSet* OutSet::Extend(unsigned value, Zone* zone) {
if (Get(value))
return this;
if (successors(zone) != NULL) {
for (int i = 0; i < successors(zone)->length(); i++) {
OutSet* successor = successors(zone)->at(i);
if (successor->Get(value))
return successor;
}
} else {
successors_ = new(zone) ZoneList<OutSet*>(2, zone);
}
OutSet* result = new(zone) OutSet(first_, remaining_);
result->Set(value, zone);
successors(zone)->Add(result, zone);
return result;
}
void OutSet::Set(unsigned value, Zone *zone) {
if (value < kFirstLimit) {
first_ |= (1 << value);
} else {
if (remaining_ == NULL)
remaining_ = new(zone) ZoneList<unsigned>(1, zone);
if (remaining_->is_empty() || !remaining_->Contains(value))
remaining_->Add(value, zone);
}
}
bool OutSet::Get(unsigned value) const {
if (value < kFirstLimit) {
return (first_ & (1 << value)) != 0;
} else if (remaining_ == NULL) {
return false;
} else {
return remaining_->Contains(value);
}
}
const uc32 DispatchTable::Config::kNoKey = unibrow::Utf8::kBadChar;
void DispatchTable::AddRange(CharacterRange full_range, int value,
Zone* zone) {
CharacterRange current = full_range;
if (tree()->is_empty()) {
// If this is the first range we just insert into the table.
ZoneSplayTree<Config>::Locator loc;
bool inserted = tree()->Insert(current.from(), &loc);
DCHECK(inserted);
USE(inserted);
loc.set_value(Entry(current.from(), current.to(),
empty()->Extend(value, zone)));
return;
}
// First see if there is a range to the left of this one that
// overlaps.
ZoneSplayTree<Config>::Locator loc;
if (tree()->FindGreatestLessThan(current.from(), &loc)) {
Entry* entry = &loc.value();
// If we've found a range that overlaps with this one, and it
// starts strictly to the left of this one, we have to fix it
// because the following code only handles ranges that start on
// or after the start point of the range we're adding.
if (entry->from() < current.from() && entry->to() >= current.from()) {
// Snap the overlapping range in half around the start point of
// the range we're adding.
CharacterRange left =
CharacterRange::Range(entry->from(), current.from() - 1);
CharacterRange right = CharacterRange::Range(current.from(), entry->to());
// The left part of the overlapping range doesn't overlap.
// Truncate the whole entry to be just the left part.
entry->set_to(left.to());
// The right part is the one that overlaps. We add this part
// to the map and let the next step deal with merging it with
// the range we're adding.
ZoneSplayTree<Config>::Locator loc;
bool inserted = tree()->Insert(right.from(), &loc);
DCHECK(inserted);
USE(inserted);
loc.set_value(Entry(right.from(),
right.to(),
entry->out_set()));
}
}
while (current.is_valid()) {
if (tree()->FindLeastGreaterThan(current.from(), &loc) &&
(loc.value().from() <= current.to()) &&
(loc.value().to() >= current.from())) {
Entry* entry = &loc.value();
// We have overlap. If there is space between the start point of
// the range we're adding and where the overlapping range starts
// then we have to add a range covering just that space.
if (current.from() < entry->from()) {
ZoneSplayTree<Config>::Locator ins;
bool inserted = tree()->Insert(current.from(), &ins);
DCHECK(inserted);
USE(inserted);
ins.set_value(Entry(current.from(),
entry->from() - 1,
empty()->Extend(value, zone)));
current.set_from(entry->from());
}
DCHECK_EQ(current.from(), entry->from());
// If the overlapping range extends beyond the one we want to add
// we have to snap the right part off and add it separately.
if (entry->to() > current.to()) {
ZoneSplayTree<Config>::Locator ins;
bool inserted = tree()->Insert(current.to() + 1, &ins);
DCHECK(inserted);
USE(inserted);
ins.set_value(Entry(current.to() + 1,
entry->to(),
entry->out_set()));
entry->set_to(current.to());
}
DCHECK(entry->to() <= current.to());
// The overlapping range is now completely contained by the range
// we're adding so we can just update it and move the start point
// of the range we're adding just past it.
entry->AddValue(value, zone);
DCHECK(entry->to() + 1 > current.from());
current.set_from(entry->to() + 1);
} else {
// There is no overlap so we can just add the range
ZoneSplayTree<Config>::Locator ins;
bool inserted = tree()->Insert(current.from(), &ins);
DCHECK(inserted);
USE(inserted);
ins.set_value(Entry(current.from(),
current.to(),
empty()->Extend(value, zone)));
break;
}
}
}
OutSet* DispatchTable::Get(uc32 value) {
ZoneSplayTree<Config>::Locator loc;
if (!tree()->FindGreatestLessThan(value, &loc))
return empty();
Entry* entry = &loc.value();
if (value <= entry->to())
return entry->out_set();
else
return empty();
}
// -------------------------------------------------------------------
// Analysis
void Analysis::EnsureAnalyzed(RegExpNode* that) {
StackLimitCheck check(isolate());
if (check.HasOverflowed()) {
fail("Stack overflow");
return;
}
if (that->info()->been_analyzed || that->info()->being_analyzed)
return;
that->info()->being_analyzed = true;
that->Accept(this);
that->info()->being_analyzed = false;
that->info()->been_analyzed = true;
}
void Analysis::VisitEnd(EndNode* that) {
// nothing to do
}
void TextNode::CalculateOffsets() {
int element_count = elements()->length();
// Set up the offsets of the elements relative to the start. This is a fixed
// quantity since a TextNode can only contain fixed-width things.
int cp_offset = 0;
for (int i = 0; i < element_count; i++) {
TextElement& elm = elements()->at(i);
elm.set_cp_offset(cp_offset);
cp_offset += elm.length();
}
}
void Analysis::VisitText(TextNode* that) {
if (ignore_case()) {
that->MakeCaseIndependent(isolate(), is_one_byte_);
}
EnsureAnalyzed(that->on_success());
if (!has_failed()) {
that->CalculateOffsets();
}
}
void Analysis::VisitAction(ActionNode* that) {
RegExpNode* target = that->on_success();
EnsureAnalyzed(target);
if (!has_failed()) {
// If the next node is interested in what it follows then this node
// has to be interested too so it can pass the information on.
that->info()->AddFromFollowing(target->info());
}
}
void Analysis::VisitChoice(ChoiceNode* that) {
NodeInfo* info = that->info();
for (int i = 0; i < that->alternatives()->length(); i++) {
RegExpNode* node = that->alternatives()->at(i).node();
EnsureAnalyzed(node);
if (has_failed()) return;
// Anything the following nodes need to know has to be known by
// this node also, so it can pass it on.
info->AddFromFollowing(node->info());
}
}
void Analysis::VisitLoopChoice(LoopChoiceNode* that) {
NodeInfo* info = that->info();
for (int i = 0; i < that->alternatives()->length(); i++) {
RegExpNode* node = that->alternatives()->at(i).node();
if (node != that->loop_node()) {
EnsureAnalyzed(node);
if (has_failed()) return;
info->AddFromFollowing(node->info());
}
}
// Check the loop last since it may need the value of this node
// to get a correct result.
EnsureAnalyzed(that->loop_node());
if (!has_failed()) {
info->AddFromFollowing(that->loop_node()->info());
}
}
void Analysis::VisitBackReference(BackReferenceNode* that) {
EnsureAnalyzed(that->on_success());
}
void Analysis::VisitAssertion(AssertionNode* that) {
EnsureAnalyzed(that->on_success());
}
void BackReferenceNode::FillInBMInfo(Isolate* isolate, int offset, int budget,
BoyerMooreLookahead* bm,
bool not_at_start) {
// Working out the set of characters that a backreference can match is too
// hard, so we just say that any character can match.
bm->SetRest(offset);
SaveBMInfo(bm, not_at_start, offset);
}
STATIC_ASSERT(BoyerMoorePositionInfo::kMapSize ==
RegExpMacroAssembler::kTableSize);
void ChoiceNode::FillInBMInfo(Isolate* isolate, int offset, int budget,
BoyerMooreLookahead* bm, bool not_at_start) {
ZoneList<GuardedAlternative>* alts = alternatives();
budget = (budget - 1) / alts->length();
for (int i = 0; i < alts->length(); i++) {
GuardedAlternative& alt = alts->at(i);
if (alt.guards() != NULL && alt.guards()->length() != 0) {
bm->SetRest(offset); // Give up trying to fill in info.
SaveBMInfo(bm, not_at_start, offset);
return;
}
alt.node()->FillInBMInfo(isolate, offset, budget, bm, not_at_start);
}
SaveBMInfo(bm, not_at_start, offset);
}
void TextNode::FillInBMInfo(Isolate* isolate, int initial_offset, int budget,
BoyerMooreLookahead* bm, bool not_at_start) {
if (initial_offset >= bm->length()) return;
int offset = initial_offset;
int max_char = bm->max_char();
for (int i = 0; i < elements()->length(); i++) {
if (offset >= bm->length()) {
if (initial_offset == 0) set_bm_info(not_at_start, bm);
return;
}
TextElement text = elements()->at(i);
if (text.text_type() == TextElement::ATOM) {
RegExpAtom* atom = text.atom();
for (int j = 0; j < atom->length(); j++, offset++) {
if (offset >= bm->length()) {
if (initial_offset == 0) set_bm_info(not_at_start, bm);
return;
}
uc16 character = atom->data()[j];
if (bm->compiler()->ignore_case()) {
unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
int length = GetCaseIndependentLetters(
isolate, character, bm->max_char() == String::kMaxOneByteCharCode,
chars);
for (int j = 0; j < length; j++) {
bm->Set(offset, chars[j]);
}
} else {
if (character <= max_char) bm->Set(offset, character);
}
}
} else {
DCHECK_EQ(TextElement::CHAR_CLASS, text.text_type());
RegExpCharacterClass* char_class = text.char_class();
ZoneList<CharacterRange>* ranges = char_class->ranges(zone());
if (char_class->is_negated()) {
bm->SetAll(offset);
} else {
for (int k = 0; k < ranges->length(); k++) {
CharacterRange& range = ranges->at(k);
if (range.from() > max_char) continue;
int to = Min(max_char, static_cast<int>(range.to()));
bm->SetInterval(offset, Interval(range.from(), to));
}
}
offset++;
}
}
if (offset >= bm->length()) {
if (initial_offset == 0) set_bm_info(not_at_start, bm);
return;
}
on_success()->FillInBMInfo(isolate, offset, budget - 1, bm,
true); // Not at start after a text node.
if (initial_offset == 0) set_bm_info(not_at_start, bm);
}
// -------------------------------------------------------------------
// Dispatch table construction
void DispatchTableConstructor::VisitEnd(EndNode* that) {
AddRange(CharacterRange::Everything());
}
void DispatchTableConstructor::BuildTable(ChoiceNode* node) {
node->set_being_calculated(true);
ZoneList<GuardedAlternative>* alternatives = node->alternatives();
for (int i = 0; i < alternatives->length(); i++) {
set_choice_index(i);
alternatives->at(i).node()->Accept(this);
}
node->set_being_calculated(false);
}
class AddDispatchRange {
public:
explicit AddDispatchRange(DispatchTableConstructor* constructor)
: constructor_(constructor) { }
void Call(uc32 from, DispatchTable::Entry entry);
private:
DispatchTableConstructor* constructor_;
};
void AddDispatchRange::Call(uc32 from, DispatchTable::Entry entry) {
constructor_->AddRange(CharacterRange::Range(from, entry.to()));
}
void DispatchTableConstructor::VisitChoice(ChoiceNode* node) {
if (node->being_calculated())
return;
DispatchTable* table = node->GetTable(ignore_case_);
AddDispatchRange adder(this);
table->ForEach(&adder);
}
void DispatchTableConstructor::VisitBackReference(BackReferenceNode* that) {
// TODO(160): Find the node that we refer back to and propagate its start
// set back to here. For now we just accept anything.
AddRange(CharacterRange::Everything());
}
void DispatchTableConstructor::VisitAssertion(AssertionNode* that) {
RegExpNode* target = that->on_success();
target->Accept(this);
}
static int CompareRangeByFrom(const CharacterRange* a,
const CharacterRange* b) {
return Compare<uc16>(a->from(), b->from());
}
void DispatchTableConstructor::AddInverse(ZoneList<CharacterRange>* ranges) {
ranges->Sort(CompareRangeByFrom);
uc16 last = 0;
for (int i = 0; i < ranges->length(); i++) {
CharacterRange range = ranges->at(i);
if (last < range.from())
AddRange(CharacterRange::Range(last, range.from() - 1));
if (range.to() >= last) {
if (range.to() == String::kMaxCodePoint) {
return;
} else {
last = range.to() + 1;
}
}
}
AddRange(CharacterRange::Range(last, String::kMaxCodePoint));
}
void DispatchTableConstructor::VisitText(TextNode* that) {
TextElement elm = that->elements()->at(0);
switch (elm.text_type()) {
case TextElement::ATOM: {
uc16 c = elm.atom()->data()[0];
AddRange(CharacterRange::Range(c, c));
break;
}
case TextElement::CHAR_CLASS: {
RegExpCharacterClass* tree = elm.char_class();
ZoneList<CharacterRange>* ranges = tree->ranges(that->zone());
if (tree->is_negated()) {
AddInverse(ranges);
} else {
for (int i = 0; i < ranges->length(); i++)
AddRange(ranges->at(i));
}
break;
}
default: {
UNIMPLEMENTED();
}
}
}
void DispatchTableConstructor::VisitAction(ActionNode* that) {
RegExpNode* target = that->on_success();
target->Accept(this);
}
RegExpNode* OptionallyStepBackToLeadSurrogate(RegExpCompiler* compiler,
RegExpNode* on_success) {
// If the regexp matching starts within a surrogate pair, step back
// to the lead surrogate and start matching from there.
DCHECK(!compiler->read_backward());
Zone* zone = compiler->zone();
ZoneList<CharacterRange>* lead_surrogates = CharacterRange::List(
zone, CharacterRange::Range(kLeadSurrogateStart, kLeadSurrogateEnd));
ZoneList<CharacterRange>* trail_surrogates = CharacterRange::List(
zone, CharacterRange::Range(kTrailSurrogateStart, kTrailSurrogateEnd));
ChoiceNode* optional_step_back = new (zone) ChoiceNode(2, zone);
int stack_register = compiler->UnicodeLookaroundStackRegister();
int position_register = compiler->UnicodeLookaroundPositionRegister();
RegExpNode* step_back = TextNode::CreateForCharacterRanges(
zone, lead_surrogates, true, on_success);
RegExpLookaround::Builder builder(true, step_back, stack_register,
position_register);
RegExpNode* match_trail = TextNode::CreateForCharacterRanges(
zone, trail_surrogates, false, builder.on_match_success());
optional_step_back->AddAlternative(
GuardedAlternative(builder.ForMatch(match_trail)));
optional_step_back->AddAlternative(GuardedAlternative(on_success));
return optional_step_back;
}
RegExpEngine::CompilationResult RegExpEngine::Compile(
Isolate* isolate, Zone* zone, RegExpCompileData* data,
JSRegExp::Flags flags, Handle<String> pattern,
Handle<String> sample_subject, bool is_one_byte) {
if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) {
return IrregexpRegExpTooBig(isolate);
}
bool ignore_case = flags & JSRegExp::kIgnoreCase;
bool is_sticky = flags & JSRegExp::kSticky;
bool is_global = flags & JSRegExp::kGlobal;
bool is_unicode = flags & JSRegExp::kUnicode;
RegExpCompiler compiler(isolate, zone, data->capture_count, flags,
is_one_byte);
if (compiler.optimize()) compiler.set_optimize(!TooMuchRegExpCode(pattern));
// Sample some characters from the middle of the string.
static const int kSampleSize = 128;
sample_subject = String::Flatten(sample_subject);
int chars_sampled = 0;
int half_way = (sample_subject->length() - kSampleSize) / 2;
for (int i = Max(0, half_way);
i < sample_subject->length() && chars_sampled < kSampleSize;
i++, chars_sampled++) {
compiler.frequency_collator()->CountCharacter(sample_subject->Get(i));
}
// Wrap the body of the regexp in capture #0.
RegExpNode* captured_body = RegExpCapture::ToNode(data->tree,
0,
&compiler,
compiler.accept());
RegExpNode* node = captured_body;
bool is_end_anchored = data->tree->IsAnchoredAtEnd();
bool is_start_anchored = data->tree->IsAnchoredAtStart();
int max_length = data->tree->max_match();
if (!is_start_anchored && !is_sticky) {
// Add a .*? at the beginning, outside the body capture, unless
// this expression is anchored at the beginning or sticky.
RegExpNode* loop_node = RegExpQuantifier::ToNode(
0, RegExpTree::kInfinity, false, new (zone) RegExpCharacterClass('*'),
&compiler, captured_body, data->contains_anchor);
if (data->contains_anchor) {
// Unroll loop once, to take care of the case that might start
// at the start of input.
ChoiceNode* first_step_node = new(zone) ChoiceNode(2, zone);
first_step_node->AddAlternative(GuardedAlternative(captured_body));
first_step_node->AddAlternative(GuardedAlternative(new (zone) TextNode(
new (zone) RegExpCharacterClass('*'), false, loop_node)));
node = first_step_node;
} else {
node = loop_node;
}
}
if (is_one_byte) {
node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, ignore_case);
// Do it again to propagate the new nodes to places where they were not
// put because they had not been calculated yet.
if (node != NULL) {
node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, ignore_case);
}
} else if (compiler.unicode() && (is_global || is_sticky)) {
node = OptionallyStepBackToLeadSurrogate(&compiler, node);
}
if (node == NULL) node = new(zone) EndNode(EndNode::BACKTRACK, zone);
data->node = node;
Analysis analysis(isolate, flags, is_one_byte);
analysis.EnsureAnalyzed(node);
if (analysis.has_failed()) {
const char* error_message = analysis.error_message();
return CompilationResult(isolate, error_message);
}
// Create the correct assembler for the architecture.
#ifndef V8_INTERPRETED_REGEXP
// Native regexp implementation.
NativeRegExpMacroAssembler::Mode mode =
is_one_byte ? NativeRegExpMacroAssembler::LATIN1
: NativeRegExpMacroAssembler::UC16;
#if V8_TARGET_ARCH_IA32
RegExpMacroAssemblerIA32 macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_X64
RegExpMacroAssemblerX64 macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_ARM
RegExpMacroAssemblerARM macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_ARM64
RegExpMacroAssemblerARM64 macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_S390
RegExpMacroAssemblerS390 macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_PPC
RegExpMacroAssemblerPPC macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_MIPS
RegExpMacroAssemblerMIPS macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_MIPS64
RegExpMacroAssemblerMIPS macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#elif V8_TARGET_ARCH_X87
RegExpMacroAssemblerX87 macro_assembler(isolate, zone, mode,
(data->capture_count + 1) * 2);
#else
#error "Unsupported architecture"
#endif
#else // V8_INTERPRETED_REGEXP
// Interpreted regexp implementation.
EmbeddedVector<byte, 1024> codes;
RegExpMacroAssemblerIrregexp macro_assembler(isolate, codes, zone);
#endif // V8_INTERPRETED_REGEXP
macro_assembler.set_slow_safe(TooMuchRegExpCode(pattern));
// Inserted here, instead of in Assembler, because it depends on information
// in the AST that isn't replicated in the Node structure.
static const int kMaxBacksearchLimit = 1024;
if (is_end_anchored &&
!is_start_anchored &&
max_length < kMaxBacksearchLimit) {
macro_assembler.SetCurrentPositionFromEnd(max_length);
}
if (is_global) {
RegExpMacroAssembler::GlobalMode mode = RegExpMacroAssembler::GLOBAL;
if (data->tree->min_match() > 0) {
mode = RegExpMacroAssembler::GLOBAL_NO_ZERO_LENGTH_CHECK;
} else if (is_unicode) {
mode = RegExpMacroAssembler::GLOBAL_UNICODE;
}
macro_assembler.set_global_mode(mode);
}
return compiler.Assemble(¯o_assembler,
node,
data->capture_count,
pattern);
}
bool RegExpEngine::TooMuchRegExpCode(Handle<String> pattern) {
Heap* heap = pattern->GetHeap();
bool too_much = pattern->length() > RegExpImpl::kRegExpTooLargeToOptimize;
if (heap->total_regexp_code_generated() > RegExpImpl::kRegExpCompiledLimit &&
heap->memory_allocator()->SizeExecutable() >
RegExpImpl::kRegExpExecutableMemoryLimit) {
too_much = true;
}
return too_much;
}
Object* RegExpResultsCache::Lookup(Heap* heap, String* key_string,
Object* key_pattern,
FixedArray** last_match_cache,
ResultsCacheType type) {
FixedArray* cache;
if (!key_string->IsInternalizedString()) return Smi::FromInt(0);
if (type == STRING_SPLIT_SUBSTRINGS) {
DCHECK(key_pattern->IsString());
if (!key_pattern->IsInternalizedString()) return Smi::FromInt(0);
cache = heap->string_split_cache();
} else {
DCHECK(type == REGEXP_MULTIPLE_INDICES);
DCHECK(key_pattern->IsFixedArray());
cache = heap->regexp_multiple_cache();
}
uint32_t hash = key_string->Hash();
uint32_t index = ((hash & (kRegExpResultsCacheSize - 1)) &
~(kArrayEntriesPerCacheEntry - 1));
if (cache->get(index + kStringOffset) != key_string ||
cache->get(index + kPatternOffset) != key_pattern) {
index =
((index + kArrayEntriesPerCacheEntry) & (kRegExpResultsCacheSize - 1));
if (cache->get(index + kStringOffset) != key_string ||
cache->get(index + kPatternOffset) != key_pattern) {
return Smi::FromInt(0);
}
}
*last_match_cache = FixedArray::cast(cache->get(index + kLastMatchOffset));
return cache->get(index + kArrayOffset);
}
void RegExpResultsCache::Enter(Isolate* isolate, Handle<String> key_string,
Handle<Object> key_pattern,
Handle<FixedArray> value_array,
Handle<FixedArray> last_match_cache,
ResultsCacheType type) {
Factory* factory = isolate->factory();
Handle<FixedArray> cache;
if (!key_string->IsInternalizedString()) return;
if (type == STRING_SPLIT_SUBSTRINGS) {
DCHECK(key_pattern->IsString());
if (!key_pattern->IsInternalizedString()) return;
cache = factory->string_split_cache();
} else {
DCHECK(type == REGEXP_MULTIPLE_INDICES);
DCHECK(key_pattern->IsFixedArray());
cache = factory->regexp_multiple_cache();
}
uint32_t hash = key_string->Hash();
uint32_t index = ((hash & (kRegExpResultsCacheSize - 1)) &
~(kArrayEntriesPerCacheEntry - 1));
if (cache->get(index + kStringOffset) == Smi::FromInt(0)) {
cache->set(index + kStringOffset, *key_string);
cache->set(index + kPatternOffset, *key_pattern);
cache->set(index + kArrayOffset, *value_array);
cache->set(index + kLastMatchOffset, *last_match_cache);
} else {
uint32_t index2 =
((index + kArrayEntriesPerCacheEntry) & (kRegExpResultsCacheSize - 1));
if (cache->get(index2 + kStringOffset) == Smi::FromInt(0)) {
cache->set(index2 + kStringOffset, *key_string);
cache->set(index2 + kPatternOffset, *key_pattern);
cache->set(index2 + kArrayOffset, *value_array);
cache->set(index2 + kLastMatchOffset, *last_match_cache);
} else {
cache->set(index2 + kStringOffset, Smi::FromInt(0));
cache->set(index2 + kPatternOffset, Smi::FromInt(0));
cache->set(index2 + kArrayOffset, Smi::FromInt(0));
cache->set(index2 + kLastMatchOffset, Smi::FromInt(0));
cache->set(index + kStringOffset, *key_string);
cache->set(index + kPatternOffset, *key_pattern);
cache->set(index + kArrayOffset, *value_array);
cache->set(index + kLastMatchOffset, *last_match_cache);
}
}
// If the array is a reasonably short list of substrings, convert it into a
// list of internalized strings.
if (type == STRING_SPLIT_SUBSTRINGS && value_array->length() < 100) {
for (int i = 0; i < value_array->length(); i++) {
Handle<String> str(String::cast(value_array->get(i)), isolate);
Handle<String> internalized_str = factory->InternalizeString(str);
value_array->set(i, *internalized_str);
}
}
// Convert backing store to a copy-on-write array.
value_array->set_map_no_write_barrier(*factory->fixed_cow_array_map());
}
void RegExpResultsCache::Clear(FixedArray* cache) {
for (int i = 0; i < kRegExpResultsCacheSize; i++) {
cache->set(i, Smi::FromInt(0));
}
}
} // namespace internal
} // namespace v8
|