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
|
/*
* Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
* Copyright (C) 2007 Maks Orlovich
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "Nodes.h"
#include "NodeConstructors.h"
#include "BytecodeGenerator.h"
#include "CallFrame.h"
#include "Debugger.h"
#include "JIT.h"
#include "JSFunction.h"
#include "JSGlobalObject.h"
#include "JSStaticScopeObject.h"
#include "LabelScope.h"
#include "Lexer.h"
#include "Operations.h"
#include "Parser.h"
#include "PropertyNameArray.h"
#include "RegExpObject.h"
#include "SamplingTool.h"
#include <wtf/Assertions.h>
#include <wtf/RefCountedLeakCounter.h>
#include <wtf/Threading.h>
using namespace WTF;
namespace JSC {
static void substitute(UString& string, const UString& substring);
// ------------------------------ ThrowableExpressionData --------------------------------
static void substitute(UString& string, const UString& substring)
{
int position = string.find("%s");
ASSERT(position != -1);
UString newString = string.substr(0, position);
newString.append(substring);
newString.append(string.substr(position + 2));
string = newString;
}
RegisterID* ThrowableExpressionData::emitThrowError(BytecodeGenerator& generator, ErrorType e, const char* msg)
{
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
RegisterID* exception = generator.emitNewError(generator.newTemporary(), e, jsString(generator.globalData(), msg));
generator.emitThrow(exception);
return exception;
}
RegisterID* ThrowableExpressionData::emitThrowError(BytecodeGenerator& generator, ErrorType e, const char* msg, const Identifier& label)
{
UString message = msg;
substitute(message, label.ustring());
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
RegisterID* exception = generator.emitNewError(generator.newTemporary(), e, jsString(generator.globalData(), message));
generator.emitThrow(exception);
return exception;
}
// ------------------------------ StatementNode --------------------------------
void StatementNode::setLoc(int firstLine, int lastLine)
{
m_line = firstLine;
m_lastLine = lastLine;
}
// ------------------------------ SourceElements --------------------------------
void SourceElements::append(StatementNode* statement)
{
if (statement->isEmptyStatement())
return;
m_statements.append(statement);
}
// ------------------------------ NullNode -------------------------------------
RegisterID* NullNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult())
return 0;
return generator.emitLoad(dst, jsNull());
}
// ------------------------------ BooleanNode ----------------------------------
RegisterID* BooleanNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult())
return 0;
return generator.emitLoad(dst, m_value);
}
// ------------------------------ NumberNode -----------------------------------
RegisterID* NumberNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult())
return 0;
return generator.emitLoad(dst, m_double);
}
// ------------------------------ StringNode -----------------------------------
RegisterID* StringNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult())
return 0;
return generator.emitLoad(dst, m_value);
}
// ------------------------------ RegExpNode -----------------------------------
RegisterID* RegExpNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegExp> regExp = RegExp::create(generator.globalData(), m_pattern, m_flags);
if (!regExp->isValid())
return emitThrowError(generator, SyntaxError, ("Invalid regular expression: " + UString(regExp->errorMessage())).UTF8String().c_str());
if (dst == generator.ignoredResult())
return 0;
return generator.emitNewRegExp(generator.finalDestination(dst), regExp.get());
}
// ------------------------------ ThisNode -------------------------------------
RegisterID* ThisNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult())
return 0;
return generator.moveToDestinationIfNeeded(dst, generator.thisRegister());
}
// ------------------------------ ResolveNode ----------------------------------
bool ResolveNode::isPure(BytecodeGenerator& generator) const
{
return generator.isLocal(m_ident);
}
RegisterID* ResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (RegisterID* local = generator.registerFor(m_ident)) {
if (dst == generator.ignoredResult())
return 0;
return generator.moveToDestinationIfNeeded(dst, local);
}
generator.emitExpressionInfo(m_startOffset + m_ident.size(), m_ident.size(), 0);
return generator.emitResolve(generator.finalDestination(dst), m_ident);
}
// ------------------------------ ArrayNode ------------------------------------
RegisterID* ArrayNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
// FIXME: Should we put all of this code into emitNewArray?
unsigned length = 0;
ElementNode* firstPutElement;
for (firstPutElement = m_element; firstPutElement; firstPutElement = firstPutElement->next()) {
if (firstPutElement->elision())
break;
++length;
}
if (!firstPutElement && !m_elision)
return generator.emitNewArray(generator.finalDestination(dst), m_element);
RefPtr<RegisterID> array = generator.emitNewArray(generator.tempDestination(dst), m_element);
for (ElementNode* n = firstPutElement; n; n = n->next()) {
RegisterID* value = generator.emitNode(n->value());
length += n->elision();
generator.emitPutByIndex(array.get(), length++, value);
}
if (m_elision) {
RegisterID* value = generator.emitLoad(0, jsNumber(generator.globalData(), m_elision + length));
generator.emitPutById(array.get(), generator.propertyNames().length, value);
}
return generator.moveToDestinationIfNeeded(dst, array.get());
}
bool ArrayNode::isSimpleArray() const
{
if (m_elision || m_optional)
return false;
for (ElementNode* ptr = m_element; ptr; ptr = ptr->next()) {
if (ptr->elision())
return false;
}
return true;
}
ArgumentListNode* ArrayNode::toArgumentList(JSGlobalData* globalData) const
{
ASSERT(!m_elision && !m_optional);
ElementNode* ptr = m_element;
if (!ptr)
return 0;
ArgumentListNode* head = new (globalData) ArgumentListNode(globalData, ptr->value());
ArgumentListNode* tail = head;
ptr = ptr->next();
for (; ptr; ptr = ptr->next()) {
ASSERT(!ptr->elision());
tail = new (globalData) ArgumentListNode(globalData, tail, ptr->value());
}
return head;
}
// ------------------------------ ObjectLiteralNode ----------------------------
RegisterID* ObjectLiteralNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (!m_list) {
if (dst == generator.ignoredResult())
return 0;
return generator.emitNewObject(generator.finalDestination(dst));
}
return generator.emitNode(dst, m_list);
}
// ------------------------------ PropertyListNode -----------------------------
RegisterID* PropertyListNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> newObj = generator.tempDestination(dst);
generator.emitNewObject(newObj.get());
for (PropertyListNode* p = this; p; p = p->m_next) {
RegisterID* value = generator.emitNode(p->m_node->m_assign);
switch (p->m_node->m_type) {
case PropertyNode::Constant: {
generator.emitPutById(newObj.get(), p->m_node->name(), value);
break;
}
case PropertyNode::Getter: {
generator.emitPutGetter(newObj.get(), p->m_node->name(), value);
break;
}
case PropertyNode::Setter: {
generator.emitPutSetter(newObj.get(), p->m_node->name(), value);
break;
}
default:
ASSERT_NOT_REACHED();
}
}
return generator.moveToDestinationIfNeeded(dst, newObj.get());
}
// ------------------------------ BracketAccessorNode --------------------------------
RegisterID* BracketAccessorNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNodeForLeftHandSide(m_base, m_subscriptHasAssignments, m_subscript->isPure(generator));
RegisterID* property = generator.emitNode(m_subscript);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitGetByVal(generator.finalDestination(dst), base.get(), property);
}
// ------------------------------ DotAccessorNode --------------------------------
RegisterID* DotAccessorNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RegisterID* base = generator.emitNode(m_base);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitGetById(generator.finalDestination(dst), base, m_ident);
}
// ------------------------------ ArgumentListNode -----------------------------
RegisterID* ArgumentListNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
ASSERT(m_expr);
return generator.emitNode(dst, m_expr);
}
// ------------------------------ NewExprNode ----------------------------------
RegisterID* NewExprNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> func = generator.emitNode(m_expr);
return generator.emitConstruct(generator.finalDestination(dst), func.get(), m_args, divot(), startOffset(), endOffset());
}
// ------------------------------ EvalFunctionCallNode ----------------------------------
RegisterID* EvalFunctionCallNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> func = generator.tempDestination(dst);
RefPtr<RegisterID> thisRegister = generator.newTemporary();
generator.emitExpressionInfo(divot() - startOffset() + 4, 4, 0);
generator.emitResolveWithBase(thisRegister.get(), func.get(), generator.propertyNames().eval);
return generator.emitCallEval(generator.finalDestination(dst, func.get()), func.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
// ------------------------------ FunctionCallValueNode ----------------------------------
RegisterID* FunctionCallValueNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> func = generator.emitNode(m_expr);
RefPtr<RegisterID> thisRegister = generator.emitLoad(generator.newTemporary(), jsNull());
return generator.emitCall(generator.finalDestination(dst, func.get()), func.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
// ------------------------------ FunctionCallResolveNode ----------------------------------
RegisterID* FunctionCallResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (RefPtr<RegisterID> local = generator.registerFor(m_ident)) {
RefPtr<RegisterID> thisRegister = generator.emitLoad(generator.newTemporary(), jsNull());
return generator.emitCall(generator.finalDestination(dst, thisRegister.get()), local.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
int index = 0;
size_t depth = 0;
JSObject* globalObject = 0;
if (generator.findScopedProperty(m_ident, index, depth, false, globalObject) && index != missingSymbolMarker()) {
RefPtr<RegisterID> func = generator.emitGetScopedVar(generator.newTemporary(), depth, index, globalObject);
RefPtr<RegisterID> thisRegister = generator.emitLoad(generator.newTemporary(), jsNull());
return generator.emitCall(generator.finalDestination(dst, func.get()), func.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
RefPtr<RegisterID> func = generator.newTemporary();
RefPtr<RegisterID> thisRegister = generator.newTemporary();
int identifierStart = divot() - startOffset();
generator.emitExpressionInfo(identifierStart + m_ident.size(), m_ident.size(), 0);
generator.emitResolveFunction(thisRegister.get(), func.get(), m_ident);
return generator.emitCall(generator.finalDestination(dst, func.get()), func.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
// ------------------------------ FunctionCallBracketNode ----------------------------------
RegisterID* FunctionCallBracketNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNode(m_base);
RegisterID* property = generator.emitNode(m_subscript);
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
RefPtr<RegisterID> function = generator.emitGetByVal(generator.tempDestination(dst), base.get(), property);
RefPtr<RegisterID> thisRegister = generator.emitMove(generator.newTemporary(), base.get());
return generator.emitCall(generator.finalDestination(dst, function.get()), function.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
// ------------------------------ FunctionCallDotNode ----------------------------------
RegisterID* FunctionCallDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNode(m_base);
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
generator.emitMethodCheck();
RefPtr<RegisterID> function = generator.emitGetById(generator.tempDestination(dst), base.get(), m_ident);
RefPtr<RegisterID> thisRegister = generator.emitMove(generator.newTemporary(), base.get());
return generator.emitCall(generator.finalDestination(dst, function.get()), function.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
RegisterID* CallFunctionCallDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<Label> realCall = generator.newLabel();
RefPtr<Label> end = generator.newLabel();
RefPtr<RegisterID> base = generator.emitNode(m_base);
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
RefPtr<RegisterID> function = generator.emitGetById(generator.tempDestination(dst), base.get(), m_ident);
RefPtr<RegisterID> finalDestination = generator.finalDestination(dst, function.get());
generator.emitJumpIfNotFunctionCall(function.get(), realCall.get());
{
RefPtr<RegisterID> realFunction = generator.emitMove(generator.tempDestination(dst), base.get());
RefPtr<RegisterID> thisRegister = generator.newTemporary();
ArgumentListNode* oldList = m_args->m_listNode;
if (m_args->m_listNode && m_args->m_listNode->m_expr) {
generator.emitNode(thisRegister.get(), m_args->m_listNode->m_expr);
m_args->m_listNode = m_args->m_listNode->m_next;
} else
generator.emitLoad(thisRegister.get(), jsNull());
generator.emitCall(finalDestination.get(), realFunction.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
generator.emitJump(end.get());
m_args->m_listNode = oldList;
}
generator.emitLabel(realCall.get());
{
RefPtr<RegisterID> thisRegister = generator.emitMove(generator.newTemporary(), base.get());
generator.emitCall(finalDestination.get(), function.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
generator.emitLabel(end.get());
return finalDestination.get();
}
static bool areTrivialApplyArguments(ArgumentsNode* args)
{
return !args->m_listNode || !args->m_listNode->m_expr || !args->m_listNode->m_next
|| (!args->m_listNode->m_next->m_next && args->m_listNode->m_next->m_expr->isSimpleArray());
}
RegisterID* ApplyFunctionCallDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
// A few simple cases can be trivially handled as ordinary function calls.
// function.apply(), function.apply(arg) -> identical to function.call
// function.apply(thisArg, [arg0, arg1, ...]) -> can be trivially coerced into function.call(thisArg, arg0, arg1, ...) and saves object allocation
bool mayBeCall = areTrivialApplyArguments(m_args);
RefPtr<Label> realCall = generator.newLabel();
RefPtr<Label> end = generator.newLabel();
RefPtr<RegisterID> base = generator.emitNode(m_base);
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
RefPtr<RegisterID> function = generator.emitGetById(generator.tempDestination(dst), base.get(), m_ident);
RefPtr<RegisterID> finalDestination = generator.finalDestination(dst, function.get());
generator.emitJumpIfNotFunctionApply(function.get(), realCall.get());
{
if (mayBeCall) {
RefPtr<RegisterID> realFunction = generator.emitMove(generator.tempDestination(dst), base.get());
RefPtr<RegisterID> thisRegister = generator.newTemporary();
ArgumentListNode* oldList = m_args->m_listNode;
if (m_args->m_listNode && m_args->m_listNode->m_expr) {
generator.emitNode(thisRegister.get(), m_args->m_listNode->m_expr);
m_args->m_listNode = m_args->m_listNode->m_next;
if (m_args->m_listNode) {
ASSERT(m_args->m_listNode->m_expr->isSimpleArray());
ASSERT(!m_args->m_listNode->m_next);
m_args->m_listNode = static_cast<ArrayNode*>(m_args->m_listNode->m_expr)->toArgumentList(generator.globalData());
}
} else
generator.emitLoad(thisRegister.get(), jsNull());
generator.emitCall(finalDestination.get(), realFunction.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
m_args->m_listNode = oldList;
} else {
ASSERT(m_args->m_listNode && m_args->m_listNode->m_next);
RefPtr<RegisterID> realFunction = generator.emitMove(generator.newTemporary(), base.get());
RefPtr<RegisterID> argsCountRegister = generator.newTemporary();
RefPtr<RegisterID> thisRegister = generator.newTemporary();
RefPtr<RegisterID> argsRegister = generator.newTemporary();
generator.emitNode(thisRegister.get(), m_args->m_listNode->m_expr);
ArgumentListNode* args = m_args->m_listNode->m_next;
bool isArgumentsApply = false;
if (args->m_expr->isResolveNode()) {
ResolveNode* resolveNode = static_cast<ResolveNode*>(args->m_expr);
isArgumentsApply = generator.willResolveToArguments(resolveNode->identifier());
if (isArgumentsApply)
generator.emitMove(argsRegister.get(), generator.uncheckedRegisterForArguments());
}
if (!isArgumentsApply)
generator.emitNode(argsRegister.get(), args->m_expr);
while ((args = args->m_next))
generator.emitNode(args->m_expr);
generator.emitLoadVarargs(argsCountRegister.get(), argsRegister.get());
generator.emitCallVarargs(finalDestination.get(), realFunction.get(), thisRegister.get(), argsCountRegister.get(), divot(), startOffset(), endOffset());
}
generator.emitJump(end.get());
}
generator.emitLabel(realCall.get());
{
RefPtr<RegisterID> thisRegister = generator.emitMove(generator.newTemporary(), base.get());
generator.emitCall(finalDestination.get(), function.get(), thisRegister.get(), m_args, divot(), startOffset(), endOffset());
}
generator.emitLabel(end.get());
return finalDestination.get();
}
// ------------------------------ PostfixResolveNode ----------------------------------
static RegisterID* emitPreIncOrDec(BytecodeGenerator& generator, RegisterID* srcDst, Operator oper)
{
return (oper == OpPlusPlus) ? generator.emitPreInc(srcDst) : generator.emitPreDec(srcDst);
}
static RegisterID* emitPostIncOrDec(BytecodeGenerator& generator, RegisterID* dst, RegisterID* srcDst, Operator oper)
{
return (oper == OpPlusPlus) ? generator.emitPostInc(dst, srcDst) : generator.emitPostDec(dst, srcDst);
}
RegisterID* PostfixResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (RegisterID* local = generator.registerFor(m_ident)) {
if (generator.isLocalConstant(m_ident)) {
if (dst == generator.ignoredResult())
return 0;
return generator.emitToJSNumber(generator.finalDestination(dst), local);
}
if (dst == generator.ignoredResult())
return emitPreIncOrDec(generator, local, m_operator);
return emitPostIncOrDec(generator, generator.finalDestination(dst), local, m_operator);
}
int index = 0;
size_t depth = 0;
JSObject* globalObject = 0;
if (generator.findScopedProperty(m_ident, index, depth, true, globalObject) && index != missingSymbolMarker()) {
RefPtr<RegisterID> value = generator.emitGetScopedVar(generator.newTemporary(), depth, index, globalObject);
RegisterID* oldValue;
if (dst == generator.ignoredResult()) {
oldValue = 0;
emitPreIncOrDec(generator, value.get(), m_operator);
} else {
oldValue = emitPostIncOrDec(generator, generator.finalDestination(dst), value.get(), m_operator);
}
generator.emitPutScopedVar(depth, index, value.get(), globalObject);
return oldValue;
}
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
RefPtr<RegisterID> value = generator.newTemporary();
RefPtr<RegisterID> base = generator.emitResolveWithBase(generator.newTemporary(), value.get(), m_ident);
RegisterID* oldValue;
if (dst == generator.ignoredResult()) {
oldValue = 0;
emitPreIncOrDec(generator, value.get(), m_operator);
} else {
oldValue = emitPostIncOrDec(generator, generator.finalDestination(dst), value.get(), m_operator);
}
generator.emitPutById(base.get(), m_ident, value.get());
return oldValue;
}
// ------------------------------ PostfixBracketNode ----------------------------------
RegisterID* PostfixBracketNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNode(m_base);
RefPtr<RegisterID> property = generator.emitNode(m_subscript);
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
RefPtr<RegisterID> value = generator.emitGetByVal(generator.newTemporary(), base.get(), property.get());
RegisterID* oldValue;
if (dst == generator.ignoredResult()) {
oldValue = 0;
if (m_operator == OpPlusPlus)
generator.emitPreInc(value.get());
else
generator.emitPreDec(value.get());
} else {
oldValue = (m_operator == OpPlusPlus) ? generator.emitPostInc(generator.finalDestination(dst), value.get()) : generator.emitPostDec(generator.finalDestination(dst), value.get());
}
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutByVal(base.get(), property.get(), value.get());
return oldValue;
}
// ------------------------------ PostfixDotNode ----------------------------------
RegisterID* PostfixDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNode(m_base);
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
RefPtr<RegisterID> value = generator.emitGetById(generator.newTemporary(), base.get(), m_ident);
RegisterID* oldValue;
if (dst == generator.ignoredResult()) {
oldValue = 0;
if (m_operator == OpPlusPlus)
generator.emitPreInc(value.get());
else
generator.emitPreDec(value.get());
} else {
oldValue = (m_operator == OpPlusPlus) ? generator.emitPostInc(generator.finalDestination(dst), value.get()) : generator.emitPostDec(generator.finalDestination(dst), value.get());
}
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutById(base.get(), m_ident, value.get());
return oldValue;
}
// ------------------------------ PostfixErrorNode -----------------------------------
RegisterID* PostfixErrorNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
return emitThrowError(generator, ReferenceError, m_operator == OpPlusPlus ? "Postfix ++ operator applied to value that is not a reference." : "Postfix -- operator applied to value that is not a reference.");
}
// ------------------------------ DeleteResolveNode -----------------------------------
RegisterID* DeleteResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (generator.registerFor(m_ident))
return generator.emitUnexpectedLoad(generator.finalDestination(dst), false);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
RegisterID* base = generator.emitResolveBase(generator.tempDestination(dst), m_ident);
return generator.emitDeleteById(generator.finalDestination(dst, base), base, m_ident);
}
// ------------------------------ DeleteBracketNode -----------------------------------
RegisterID* DeleteBracketNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> r0 = generator.emitNode(m_base);
RegisterID* r1 = generator.emitNode(m_subscript);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitDeleteByVal(generator.finalDestination(dst), r0.get(), r1);
}
// ------------------------------ DeleteDotNode -----------------------------------
RegisterID* DeleteDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RegisterID* r0 = generator.emitNode(m_base);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitDeleteById(generator.finalDestination(dst), r0, m_ident);
}
// ------------------------------ DeleteValueNode -----------------------------------
RegisterID* DeleteValueNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitNode(generator.ignoredResult(), m_expr);
// delete on a non-location expression ignores the value and returns true
return generator.emitUnexpectedLoad(generator.finalDestination(dst), true);
}
// ------------------------------ VoidNode -------------------------------------
RegisterID* VoidNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult()) {
generator.emitNode(generator.ignoredResult(), m_expr);
return 0;
}
RefPtr<RegisterID> r0 = generator.emitNode(m_expr);
return generator.emitLoad(dst, jsUndefined());
}
// ------------------------------ TypeOfValueNode -----------------------------------
RegisterID* TypeOfResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (RegisterID* local = generator.registerFor(m_ident)) {
if (dst == generator.ignoredResult())
return 0;
return generator.emitTypeOf(generator.finalDestination(dst), local);
}
RefPtr<RegisterID> scratch = generator.emitResolveBase(generator.tempDestination(dst), m_ident);
generator.emitGetById(scratch.get(), scratch.get(), m_ident);
if (dst == generator.ignoredResult())
return 0;
return generator.emitTypeOf(generator.finalDestination(dst, scratch.get()), scratch.get());
}
// ------------------------------ TypeOfValueNode -----------------------------------
RegisterID* TypeOfValueNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult()) {
generator.emitNode(generator.ignoredResult(), m_expr);
return 0;
}
RefPtr<RegisterID> src = generator.emitNode(m_expr);
return generator.emitTypeOf(generator.finalDestination(dst), src.get());
}
// ------------------------------ PrefixResolveNode ----------------------------------
RegisterID* PrefixResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (RegisterID* local = generator.registerFor(m_ident)) {
if (generator.isLocalConstant(m_ident)) {
if (dst == generator.ignoredResult())
return 0;
RefPtr<RegisterID> r0 = generator.emitUnexpectedLoad(generator.finalDestination(dst), (m_operator == OpPlusPlus) ? 1.0 : -1.0);
return generator.emitBinaryOp(op_add, r0.get(), local, r0.get(), OperandTypes());
}
emitPreIncOrDec(generator, local, m_operator);
return generator.moveToDestinationIfNeeded(dst, local);
}
int index = 0;
size_t depth = 0;
JSObject* globalObject = 0;
if (generator.findScopedProperty(m_ident, index, depth, false, globalObject) && index != missingSymbolMarker()) {
RefPtr<RegisterID> propDst = generator.emitGetScopedVar(generator.tempDestination(dst), depth, index, globalObject);
emitPreIncOrDec(generator, propDst.get(), m_operator);
generator.emitPutScopedVar(depth, index, propDst.get(), globalObject);
return generator.moveToDestinationIfNeeded(dst, propDst.get());
}
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
RefPtr<RegisterID> propDst = generator.tempDestination(dst);
RefPtr<RegisterID> base = generator.emitResolveWithBase(generator.newTemporary(), propDst.get(), m_ident);
emitPreIncOrDec(generator, propDst.get(), m_operator);
generator.emitPutById(base.get(), m_ident, propDst.get());
return generator.moveToDestinationIfNeeded(dst, propDst.get());
}
// ------------------------------ PrefixBracketNode ----------------------------------
RegisterID* PrefixBracketNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNode(m_base);
RefPtr<RegisterID> property = generator.emitNode(m_subscript);
RefPtr<RegisterID> propDst = generator.tempDestination(dst);
generator.emitExpressionInfo(divot() + m_subexpressionDivotOffset, m_subexpressionStartOffset, endOffset() - m_subexpressionDivotOffset);
RegisterID* value = generator.emitGetByVal(propDst.get(), base.get(), property.get());
if (m_operator == OpPlusPlus)
generator.emitPreInc(value);
else
generator.emitPreDec(value);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutByVal(base.get(), property.get(), value);
return generator.moveToDestinationIfNeeded(dst, propDst.get());
}
// ------------------------------ PrefixDotNode ----------------------------------
RegisterID* PrefixDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNode(m_base);
RefPtr<RegisterID> propDst = generator.tempDestination(dst);
generator.emitExpressionInfo(divot() + m_subexpressionDivotOffset, m_subexpressionStartOffset, endOffset() - m_subexpressionDivotOffset);
RegisterID* value = generator.emitGetById(propDst.get(), base.get(), m_ident);
if (m_operator == OpPlusPlus)
generator.emitPreInc(value);
else
generator.emitPreDec(value);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutById(base.get(), m_ident, value);
return generator.moveToDestinationIfNeeded(dst, propDst.get());
}
// ------------------------------ PrefixErrorNode -----------------------------------
RegisterID* PrefixErrorNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
return emitThrowError(generator, ReferenceError, m_operator == OpPlusPlus ? "Prefix ++ operator applied to value that is not a reference." : "Prefix -- operator applied to value that is not a reference.");
}
// ------------------------------ Unary Operation Nodes -----------------------------------
RegisterID* UnaryOpNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RegisterID* src = generator.emitNode(m_expr);
return generator.emitUnaryOp(opcodeID(), generator.finalDestination(dst), src);
}
// ------------------------------ Binary Operation Nodes -----------------------------------
// BinaryOpNode::emitStrcat:
//
// This node generates an op_strcat operation. This opcode can handle concatenation of three or
// more values, where we can determine a set of separate op_add operations would be operating on
// string values.
//
// This function expects to be operating on a graph of AST nodes looking something like this:
//
// (a)... (b)
// \ /
// (+) (c)
// \ /
// [d] ((+))
// \ /
// [+=]
//
// The assignment operation is optional, if it exists the register holding the value on the
// lefthand side of the assignment should be passing as the optional 'lhs' argument.
//
// The method should be called on the node at the root of the tree of regular binary add
// operations (marked in the diagram with a double set of parentheses). This node must
// be performing a string concatenation (determined by statically detecting that at least
// one child must be a string).
//
// Since the minimum number of values being concatenated together is expected to be 3, if
// a lhs to a concatenating assignment is not provided then the root add should have at
// least one left child that is also an add that can be determined to be operating on strings.
//
RegisterID* BinaryOpNode::emitStrcat(BytecodeGenerator& generator, RegisterID* dst, RegisterID* lhs, ReadModifyResolveNode* emitExpressionInfoForMe)
{
ASSERT(isAdd());
ASSERT(resultDescriptor().definitelyIsString());
// Create a list of expressions for all the adds in the tree of nodes we can convert into
// a string concatenation. The rightmost node (c) is added first. The rightmost node is
// added first, and the leftmost child is never added, so the vector produced for the
// example above will be [ c, b ].
Vector<ExpressionNode*, 16> reverseExpressionList;
reverseExpressionList.append(m_expr2);
// Examine the left child of the add. So long as this is a string add, add its right-child
// to the list, and keep processing along the left fork.
ExpressionNode* leftMostAddChild = m_expr1;
while (leftMostAddChild->isAdd() && leftMostAddChild->resultDescriptor().definitelyIsString()) {
reverseExpressionList.append(static_cast<AddNode*>(leftMostAddChild)->m_expr2);
leftMostAddChild = static_cast<AddNode*>(leftMostAddChild)->m_expr1;
}
Vector<RefPtr<RegisterID>, 16> temporaryRegisters;
// If there is an assignment, allocate a temporary to hold the lhs after conversion.
// We could possibly avoid this (the lhs is converted last anyway, we could let the
// op_strcat node handle its conversion if required).
if (lhs)
temporaryRegisters.append(generator.newTemporary());
// Emit code for the leftmost node ((a) in the example).
temporaryRegisters.append(generator.newTemporary());
RegisterID* leftMostAddChildTempRegister = temporaryRegisters.last().get();
generator.emitNode(leftMostAddChildTempRegister, leftMostAddChild);
// Note on ordering of conversions:
//
// We maintain the same ordering of conversions as we would see if the concatenations
// was performed as a sequence of adds (otherwise this optimization could change
// behaviour should an object have been provided a valueOf or toString method).
//
// Considering the above example, the sequnce of execution is:
// * evaluate operand (a)
// * evaluate operand (b)
// * convert (a) to primitive <- (this would be triggered by the first add)
// * convert (b) to primitive <- (ditto)
// * evaluate operand (c)
// * convert (c) to primitive <- (this would be triggered by the second add)
// And optionally, if there is an assignment:
// * convert (d) to primitive <- (this would be triggered by the assigning addition)
//
// As such we do not plant an op to convert the leftmost child now. Instead, use
// 'leftMostAddChildTempRegister' as a flag to trigger generation of the conversion
// once the second node has been generated. However, if the leftmost child is an
// immediate we can trivially determine that no conversion will be required.
// If this is the case
if (leftMostAddChild->isString())
leftMostAddChildTempRegister = 0;
while (reverseExpressionList.size()) {
ExpressionNode* node = reverseExpressionList.last();
reverseExpressionList.removeLast();
// Emit the code for the current node.
temporaryRegisters.append(generator.newTemporary());
generator.emitNode(temporaryRegisters.last().get(), node);
// On the first iteration of this loop, when we first reach this point we have just
// generated the second node, which means it is time to convert the leftmost operand.
if (leftMostAddChildTempRegister) {
generator.emitToPrimitive(leftMostAddChildTempRegister, leftMostAddChildTempRegister);
leftMostAddChildTempRegister = 0; // Only do this once.
}
// Plant a conversion for this node, if necessary.
if (!node->isString())
generator.emitToPrimitive(temporaryRegisters.last().get(), temporaryRegisters.last().get());
}
ASSERT(temporaryRegisters.size() >= 3);
// Certain read-modify nodes require expression info to be emitted *after* m_right has been generated.
// If this is required the node is passed as 'emitExpressionInfoForMe'; do so now.
if (emitExpressionInfoForMe)
generator.emitExpressionInfo(emitExpressionInfoForMe->divot(), emitExpressionInfoForMe->startOffset(), emitExpressionInfoForMe->endOffset());
// If there is an assignment convert the lhs now. This will also copy lhs to
// the temporary register we allocated for it.
if (lhs)
generator.emitToPrimitive(temporaryRegisters[0].get(), lhs);
return generator.emitStrcat(generator.finalDestination(dst, temporaryRegisters[0].get()), temporaryRegisters[0].get(), temporaryRegisters.size());
}
RegisterID* BinaryOpNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
OpcodeID opcodeID = this->opcodeID();
if (opcodeID == op_add && m_expr1->isAdd() && m_expr1->resultDescriptor().definitelyIsString())
return emitStrcat(generator, dst);
if (opcodeID == op_neq) {
if (m_expr1->isNull() || m_expr2->isNull()) {
RefPtr<RegisterID> src = generator.tempDestination(dst);
generator.emitNode(src.get(), m_expr1->isNull() ? m_expr2 : m_expr1);
return generator.emitUnaryOp(op_neq_null, generator.finalDestination(dst, src.get()), src.get());
}
}
RefPtr<RegisterID> src1 = generator.emitNodeForLeftHandSide(m_expr1, m_rightHasAssignments, m_expr2->isPure(generator));
RegisterID* src2 = generator.emitNode(m_expr2);
return generator.emitBinaryOp(opcodeID, generator.finalDestination(dst, src1.get()), src1.get(), src2, OperandTypes(m_expr1->resultDescriptor(), m_expr2->resultDescriptor()));
}
RegisterID* EqualNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (m_expr1->isNull() || m_expr2->isNull()) {
RefPtr<RegisterID> src = generator.tempDestination(dst);
generator.emitNode(src.get(), m_expr1->isNull() ? m_expr2 : m_expr1);
return generator.emitUnaryOp(op_eq_null, generator.finalDestination(dst, src.get()), src.get());
}
RefPtr<RegisterID> src1 = generator.emitNodeForLeftHandSide(m_expr1, m_rightHasAssignments, m_expr2->isPure(generator));
RegisterID* src2 = generator.emitNode(m_expr2);
return generator.emitEqualityOp(op_eq, generator.finalDestination(dst, src1.get()), src1.get(), src2);
}
RegisterID* StrictEqualNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> src1 = generator.emitNodeForLeftHandSide(m_expr1, m_rightHasAssignments, m_expr2->isPure(generator));
RegisterID* src2 = generator.emitNode(m_expr2);
return generator.emitEqualityOp(op_stricteq, generator.finalDestination(dst, src1.get()), src1.get(), src2);
}
RegisterID* ReverseBinaryOpNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> src1 = generator.emitNodeForLeftHandSide(m_expr1, m_rightHasAssignments, m_expr2->isPure(generator));
RegisterID* src2 = generator.emitNode(m_expr2);
return generator.emitBinaryOp(opcodeID(), generator.finalDestination(dst, src1.get()), src2, src1.get(), OperandTypes(m_expr2->resultDescriptor(), m_expr1->resultDescriptor()));
}
RegisterID* ThrowableBinaryOpNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> src1 = generator.emitNodeForLeftHandSide(m_expr1, m_rightHasAssignments, m_expr2->isPure(generator));
RegisterID* src2 = generator.emitNode(m_expr2);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitBinaryOp(opcodeID(), generator.finalDestination(dst, src1.get()), src1.get(), src2, OperandTypes(m_expr1->resultDescriptor(), m_expr2->resultDescriptor()));
}
RegisterID* InstanceOfNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> src1 = generator.emitNodeForLeftHandSide(m_expr1, m_rightHasAssignments, m_expr2->isPure(generator));
RefPtr<RegisterID> src2 = generator.emitNode(m_expr2);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitGetByIdExceptionInfo(op_instanceof);
RegisterID* src2Prototype = generator.emitGetById(generator.newTemporary(), src2.get(), generator.globalData()->propertyNames->prototype);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitInstanceOf(generator.finalDestination(dst, src1.get()), src1.get(), src2.get(), src2Prototype);
}
// ------------------------------ LogicalOpNode ----------------------------
RegisterID* LogicalOpNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> temp = generator.tempDestination(dst);
RefPtr<Label> target = generator.newLabel();
generator.emitNode(temp.get(), m_expr1);
if (m_operator == OpLogicalAnd)
generator.emitJumpIfFalse(temp.get(), target.get());
else
generator.emitJumpIfTrue(temp.get(), target.get());
generator.emitNode(temp.get(), m_expr2);
generator.emitLabel(target.get());
return generator.moveToDestinationIfNeeded(dst, temp.get());
}
// ------------------------------ ConditionalNode ------------------------------
RegisterID* ConditionalNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> newDst = generator.finalDestination(dst);
RefPtr<Label> beforeElse = generator.newLabel();
RefPtr<Label> afterElse = generator.newLabel();
RegisterID* cond = generator.emitNode(m_logical);
generator.emitJumpIfFalse(cond, beforeElse.get());
generator.emitNode(newDst.get(), m_expr1);
generator.emitJump(afterElse.get());
generator.emitLabel(beforeElse.get());
generator.emitNode(newDst.get(), m_expr2);
generator.emitLabel(afterElse.get());
return newDst.get();
}
// ------------------------------ ReadModifyResolveNode -----------------------------------
// FIXME: should this be moved to be a method on BytecodeGenerator?
static ALWAYS_INLINE RegisterID* emitReadModifyAssignment(BytecodeGenerator& generator, RegisterID* dst, RegisterID* src1, ExpressionNode* m_right, Operator oper, OperandTypes types, ReadModifyResolveNode* emitExpressionInfoForMe = 0)
{
OpcodeID opcodeID;
switch (oper) {
case OpMultEq:
opcodeID = op_mul;
break;
case OpDivEq:
opcodeID = op_div;
break;
case OpPlusEq:
if (m_right->isAdd() && m_right->resultDescriptor().definitelyIsString())
return static_cast<AddNode*>(m_right)->emitStrcat(generator, dst, src1, emitExpressionInfoForMe);
opcodeID = op_add;
break;
case OpMinusEq:
opcodeID = op_sub;
break;
case OpLShift:
opcodeID = op_lshift;
break;
case OpRShift:
opcodeID = op_rshift;
break;
case OpURShift:
opcodeID = op_urshift;
break;
case OpAndEq:
opcodeID = op_bitand;
break;
case OpXOrEq:
opcodeID = op_bitxor;
break;
case OpOrEq:
opcodeID = op_bitor;
break;
case OpModEq:
opcodeID = op_mod;
break;
default:
ASSERT_NOT_REACHED();
return dst;
}
RegisterID* src2 = generator.emitNode(m_right);
// Certain read-modify nodes require expression info to be emitted *after* m_right has been generated.
// If this is required the node is passed as 'emitExpressionInfoForMe'; do so now.
if (emitExpressionInfoForMe)
generator.emitExpressionInfo(emitExpressionInfoForMe->divot(), emitExpressionInfoForMe->startOffset(), emitExpressionInfoForMe->endOffset());
return generator.emitBinaryOp(opcodeID, dst, src1, src2, types);
}
RegisterID* ReadModifyResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (RegisterID* local = generator.registerFor(m_ident)) {
if (generator.isLocalConstant(m_ident)) {
return emitReadModifyAssignment(generator, generator.finalDestination(dst), local, m_right, m_operator, OperandTypes(ResultType::unknownType(), m_right->resultDescriptor()));
}
if (generator.leftHandSideNeedsCopy(m_rightHasAssignments, m_right->isPure(generator))) {
RefPtr<RegisterID> result = generator.newTemporary();
generator.emitMove(result.get(), local);
emitReadModifyAssignment(generator, result.get(), result.get(), m_right, m_operator, OperandTypes(ResultType::unknownType(), m_right->resultDescriptor()));
generator.emitMove(local, result.get());
return generator.moveToDestinationIfNeeded(dst, result.get());
}
RegisterID* result = emitReadModifyAssignment(generator, local, local, m_right, m_operator, OperandTypes(ResultType::unknownType(), m_right->resultDescriptor()));
return generator.moveToDestinationIfNeeded(dst, result);
}
int index = 0;
size_t depth = 0;
JSObject* globalObject = 0;
if (generator.findScopedProperty(m_ident, index, depth, true, globalObject) && index != missingSymbolMarker()) {
RefPtr<RegisterID> src1 = generator.emitGetScopedVar(generator.tempDestination(dst), depth, index, globalObject);
RegisterID* result = emitReadModifyAssignment(generator, generator.finalDestination(dst, src1.get()), src1.get(), m_right, m_operator, OperandTypes(ResultType::unknownType(), m_right->resultDescriptor()));
generator.emitPutScopedVar(depth, index, result, globalObject);
return result;
}
RefPtr<RegisterID> src1 = generator.tempDestination(dst);
generator.emitExpressionInfo(divot() - startOffset() + m_ident.size(), m_ident.size(), 0);
RefPtr<RegisterID> base = generator.emitResolveWithBase(generator.newTemporary(), src1.get(), m_ident);
RegisterID* result = emitReadModifyAssignment(generator, generator.finalDestination(dst, src1.get()), src1.get(), m_right, m_operator, OperandTypes(ResultType::unknownType(), m_right->resultDescriptor()), this);
return generator.emitPutById(base.get(), m_ident, result);
}
// ------------------------------ AssignResolveNode -----------------------------------
RegisterID* AssignResolveNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (RegisterID* local = generator.registerFor(m_ident)) {
if (generator.isLocalConstant(m_ident))
return generator.emitNode(dst, m_right);
RegisterID* result = generator.emitNode(local, m_right);
return generator.moveToDestinationIfNeeded(dst, result);
}
int index = 0;
size_t depth = 0;
JSObject* globalObject = 0;
if (generator.findScopedProperty(m_ident, index, depth, true, globalObject) && index != missingSymbolMarker()) {
if (dst == generator.ignoredResult())
dst = 0;
RegisterID* value = generator.emitNode(dst, m_right);
generator.emitPutScopedVar(depth, index, value, globalObject);
return value;
}
RefPtr<RegisterID> base = generator.emitResolveBase(generator.newTemporary(), m_ident);
if (dst == generator.ignoredResult())
dst = 0;
RegisterID* value = generator.emitNode(dst, m_right);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitPutById(base.get(), m_ident, value);
}
// ------------------------------ AssignDotNode -----------------------------------
RegisterID* AssignDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNodeForLeftHandSide(m_base, m_rightHasAssignments, m_right->isPure(generator));
RefPtr<RegisterID> value = generator.destinationForAssignResult(dst);
RegisterID* result = generator.emitNode(value.get(), m_right);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutById(base.get(), m_ident, result);
return generator.moveToDestinationIfNeeded(dst, result);
}
// ------------------------------ ReadModifyDotNode -----------------------------------
RegisterID* ReadModifyDotNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNodeForLeftHandSide(m_base, m_rightHasAssignments, m_right->isPure(generator));
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
RefPtr<RegisterID> value = generator.emitGetById(generator.tempDestination(dst), base.get(), m_ident);
RegisterID* updatedValue = emitReadModifyAssignment(generator, generator.finalDestination(dst, value.get()), value.get(), m_right, m_operator, OperandTypes(ResultType::unknownType(), m_right->resultDescriptor()));
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
return generator.emitPutById(base.get(), m_ident, updatedValue);
}
// ------------------------------ AssignErrorNode -----------------------------------
RegisterID* AssignErrorNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
return emitThrowError(generator, ReferenceError, "Left side of assignment is not a reference.");
}
// ------------------------------ AssignBracketNode -----------------------------------
RegisterID* AssignBracketNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNodeForLeftHandSide(m_base, m_subscriptHasAssignments || m_rightHasAssignments, m_subscript->isPure(generator) && m_right->isPure(generator));
RefPtr<RegisterID> property = generator.emitNodeForLeftHandSide(m_subscript, m_rightHasAssignments, m_right->isPure(generator));
RefPtr<RegisterID> value = generator.destinationForAssignResult(dst);
RegisterID* result = generator.emitNode(value.get(), m_right);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutByVal(base.get(), property.get(), result);
return generator.moveToDestinationIfNeeded(dst, result);
}
// ------------------------------ ReadModifyBracketNode -----------------------------------
RegisterID* ReadModifyBracketNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<RegisterID> base = generator.emitNodeForLeftHandSide(m_base, m_subscriptHasAssignments || m_rightHasAssignments, m_subscript->isPure(generator) && m_right->isPure(generator));
RefPtr<RegisterID> property = generator.emitNodeForLeftHandSide(m_subscript, m_rightHasAssignments, m_right->isPure(generator));
generator.emitExpressionInfo(divot() - m_subexpressionDivotOffset, startOffset() - m_subexpressionDivotOffset, m_subexpressionEndOffset);
RefPtr<RegisterID> value = generator.emitGetByVal(generator.tempDestination(dst), base.get(), property.get());
RegisterID* updatedValue = emitReadModifyAssignment(generator, generator.finalDestination(dst, value.get()), value.get(), m_right, m_operator, OperandTypes(ResultType::unknownType(), m_right->resultDescriptor()));
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutByVal(base.get(), property.get(), updatedValue);
return updatedValue;
}
// ------------------------------ CommaNode ------------------------------------
RegisterID* CommaNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitNode(generator.ignoredResult(), m_expr1);
return generator.emitNode(dst, m_expr2);
}
// ------------------------------ ConstDeclNode ------------------------------------
RegisterID* ConstDeclNode::emitCodeSingle(BytecodeGenerator& generator)
{
if (RegisterID* local = generator.constRegisterFor(m_ident)) {
if (!m_init)
return local;
return generator.emitNode(local, m_init);
}
// FIXME: While this code should only be hit in eval code, it will potentially
// assign to the wrong base if m_ident exists in an intervening dynamic scope.
RefPtr<RegisterID> base = generator.emitResolveBase(generator.newTemporary(), m_ident);
RegisterID* value = m_init ? generator.emitNode(m_init) : generator.emitLoad(0, jsUndefined());
return generator.emitPutById(base.get(), m_ident, value);
}
RegisterID* ConstDeclNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
RegisterID* result = 0;
for (ConstDeclNode* n = this; n; n = n->m_next)
result = n->emitCodeSingle(generator);
return result;
}
// ------------------------------ ConstStatementNode -----------------------------
RegisterID* ConstStatementNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
return generator.emitNode(m_next);
}
// ------------------------------ Helper functions for handling Vectors of StatementNode -------------------------------
static inline void statementListEmitCode(const StatementVector& statements, BytecodeGenerator& generator, RegisterID* dst)
{
size_t size = statements.size();
for (size_t i = 0; i < size; ++i)
generator.emitNode(dst, statements[i]);
}
// ------------------------------ BlockNode ------------------------------------
RegisterID* BlockNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
statementListEmitCode(m_children, generator, dst);
return 0;
}
// ------------------------------ EmptyStatementNode ---------------------------
RegisterID* EmptyStatementNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
return dst;
}
// ------------------------------ DebuggerStatementNode ---------------------------
RegisterID* DebuggerStatementNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(DidReachBreakpoint, firstLine(), lastLine());
return dst;
}
// ------------------------------ ExprStatementNode ----------------------------
RegisterID* ExprStatementNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
ASSERT(m_expr);
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
return generator.emitNode(dst, m_expr);
}
// ------------------------------ VarStatementNode ----------------------------
RegisterID* VarStatementNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
ASSERT(m_expr);
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
return generator.emitNode(m_expr);
}
// ------------------------------ IfNode ---------------------------------------
RegisterID* IfNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
RefPtr<Label> afterThen = generator.newLabel();
RegisterID* cond = generator.emitNode(m_condition);
generator.emitJumpIfFalse(cond, afterThen.get());
generator.emitNode(dst, m_ifBlock);
generator.emitLabel(afterThen.get());
// FIXME: This should return the last statement executed so that it can be returned as a Completion.
return 0;
}
// ------------------------------ IfElseNode ---------------------------------------
RegisterID* IfElseNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
RefPtr<Label> beforeElse = generator.newLabel();
RefPtr<Label> afterElse = generator.newLabel();
RegisterID* cond = generator.emitNode(m_condition);
generator.emitJumpIfFalse(cond, beforeElse.get());
generator.emitNode(dst, m_ifBlock);
generator.emitJump(afterElse.get());
generator.emitLabel(beforeElse.get());
generator.emitNode(dst, m_elseBlock);
generator.emitLabel(afterElse.get());
// FIXME: This should return the last statement executed so that it can be returned as a Completion.
return 0;
}
// ------------------------------ DoWhileNode ----------------------------------
RegisterID* DoWhileNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<LabelScope> scope = generator.newLabelScope(LabelScope::Loop);
RefPtr<Label> topOfLoop = generator.newLabel();
generator.emitLabel(topOfLoop.get());
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
RefPtr<RegisterID> result = generator.emitNode(dst, m_statement);
generator.emitLabel(scope->continueTarget());
generator.emitDebugHook(WillExecuteStatement, m_expr->lineNo(), m_expr->lineNo());
RegisterID* cond = generator.emitNode(m_expr);
generator.emitJumpIfTrue(cond, topOfLoop.get());
generator.emitLabel(scope->breakTarget());
return result.get();
}
// ------------------------------ WhileNode ------------------------------------
RegisterID* WhileNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<LabelScope> scope = generator.newLabelScope(LabelScope::Loop);
generator.emitJump(scope->continueTarget());
RefPtr<Label> topOfLoop = generator.newLabel();
generator.emitLabel(topOfLoop.get());
generator.emitNode(dst, m_statement);
generator.emitLabel(scope->continueTarget());
generator.emitDebugHook(WillExecuteStatement, m_expr->lineNo(), m_expr->lineNo());
RegisterID* cond = generator.emitNode(m_expr);
generator.emitJumpIfTrue(cond, topOfLoop.get());
generator.emitLabel(scope->breakTarget());
// FIXME: This should return the last statement executed so that it can be returned as a Completion
return 0;
}
// ------------------------------ ForNode --------------------------------------
RegisterID* ForNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult())
dst = 0;
RefPtr<LabelScope> scope = generator.newLabelScope(LabelScope::Loop);
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
if (m_expr1)
generator.emitNode(generator.ignoredResult(), m_expr1);
RefPtr<Label> condition = generator.newLabel();
generator.emitJump(condition.get());
RefPtr<Label> topOfLoop = generator.newLabel();
generator.emitLabel(topOfLoop.get());
RefPtr<RegisterID> result = generator.emitNode(dst, m_statement);
generator.emitLabel(scope->continueTarget());
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
if (m_expr3)
generator.emitNode(generator.ignoredResult(), m_expr3);
generator.emitLabel(condition.get());
if (m_expr2) {
RegisterID* cond = generator.emitNode(m_expr2);
generator.emitJumpIfTrue(cond, topOfLoop.get());
} else
generator.emitJump(topOfLoop.get());
generator.emitLabel(scope->breakTarget());
return result.get();
}
// ------------------------------ ForInNode ------------------------------------
RegisterID* ForInNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
RefPtr<LabelScope> scope = generator.newLabelScope(LabelScope::Loop);
if (!m_lexpr->isLocation())
return emitThrowError(generator, ReferenceError, "Left side of for-in statement is not a reference.");
RefPtr<Label> continueTarget = generator.newLabel();
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
if (m_init)
generator.emitNode(generator.ignoredResult(), m_init);
RegisterID* forInBase = generator.emitNode(m_expr);
RefPtr<RegisterID> iter = generator.emitGetPropertyNames(generator.newTemporary(), forInBase);
generator.emitJump(scope->continueTarget());
RefPtr<Label> loopStart = generator.newLabel();
generator.emitLabel(loopStart.get());
RegisterID* propertyName;
if (m_lexpr->isResolveNode()) {
const Identifier& ident = static_cast<ResolveNode*>(m_lexpr)->identifier();
propertyName = generator.registerFor(ident);
if (!propertyName) {
propertyName = generator.newTemporary();
RefPtr<RegisterID> protect = propertyName;
RegisterID* base = generator.emitResolveBase(generator.newTemporary(), ident);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitPutById(base, ident, propertyName);
}
} else if (m_lexpr->isDotAccessorNode()) {
DotAccessorNode* assignNode = static_cast<DotAccessorNode*>(m_lexpr);
const Identifier& ident = assignNode->identifier();
propertyName = generator.newTemporary();
RefPtr<RegisterID> protect = propertyName;
RegisterID* base = generator.emitNode(assignNode->base());
generator.emitExpressionInfo(assignNode->divot(), assignNode->startOffset(), assignNode->endOffset());
generator.emitPutById(base, ident, propertyName);
} else {
ASSERT(m_lexpr->isBracketAccessorNode());
BracketAccessorNode* assignNode = static_cast<BracketAccessorNode*>(m_lexpr);
propertyName = generator.newTemporary();
RefPtr<RegisterID> protect = propertyName;
RefPtr<RegisterID> base = generator.emitNode(assignNode->base());
RegisterID* subscript = generator.emitNode(assignNode->subscript());
generator.emitExpressionInfo(assignNode->divot(), assignNode->startOffset(), assignNode->endOffset());
generator.emitPutByVal(base.get(), subscript, propertyName);
}
generator.emitNode(dst, m_statement);
generator.emitLabel(scope->continueTarget());
generator.emitNextPropertyName(propertyName, iter.get(), loopStart.get());
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
generator.emitLabel(scope->breakTarget());
return dst;
}
// ------------------------------ ContinueNode ---------------------------------
// ECMA 12.7
RegisterID* ContinueNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
LabelScope* scope = generator.continueTarget(m_ident);
if (!scope)
return m_ident.isEmpty()
? emitThrowError(generator, SyntaxError, "Invalid continue statement.")
: emitThrowError(generator, SyntaxError, "Undefined label: '%s'.", m_ident);
generator.emitJumpScopes(scope->continueTarget(), scope->scopeDepth());
return dst;
}
// ------------------------------ BreakNode ------------------------------------
// ECMA 12.8
RegisterID* BreakNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
LabelScope* scope = generator.breakTarget(m_ident);
if (!scope)
return m_ident.isEmpty()
? emitThrowError(generator, SyntaxError, "Invalid break statement.")
: emitThrowError(generator, SyntaxError, "Undefined label: '%s'.", m_ident);
generator.emitJumpScopes(scope->breakTarget(), scope->scopeDepth());
return dst;
}
// ------------------------------ ReturnNode -----------------------------------
RegisterID* ReturnNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
if (generator.codeType() != FunctionCode)
return emitThrowError(generator, SyntaxError, "Invalid return statement.");
if (dst == generator.ignoredResult())
dst = 0;
RegisterID* r0 = m_value ? generator.emitNode(dst, m_value) : generator.emitLoad(dst, jsUndefined());
RefPtr<RegisterID> returnRegister;
if (generator.scopeDepth()) {
RefPtr<Label> l0 = generator.newLabel();
if (generator.hasFinaliser() && !r0->isTemporary()) {
returnRegister = generator.emitMove(generator.newTemporary(), r0);
r0 = returnRegister.get();
}
generator.emitJumpScopes(l0.get(), 0);
generator.emitLabel(l0.get());
}
generator.emitDebugHook(WillLeaveCallFrame, firstLine(), lastLine());
return generator.emitReturn(r0);
}
// ------------------------------ WithNode -------------------------------------
RegisterID* WithNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
RefPtr<RegisterID> scope = generator.newTemporary();
generator.emitNode(scope.get(), m_expr); // scope must be protected until popped
generator.emitExpressionInfo(m_divot, m_expressionLength, 0);
generator.emitPushScope(scope.get());
RegisterID* result = generator.emitNode(dst, m_statement);
generator.emitPopScope();
return result;
}
// ------------------------------ CaseBlockNode --------------------------------
enum SwitchKind {
SwitchUnset = 0,
SwitchNumber = 1,
SwitchString = 2,
SwitchNeither = 3
};
static void processClauseList(ClauseListNode* list, Vector<ExpressionNode*, 8>& literalVector, SwitchKind& typeForTable, bool& singleCharacterSwitch, int32_t& min_num, int32_t& max_num)
{
for (; list; list = list->getNext()) {
ExpressionNode* clauseExpression = list->getClause()->expr();
literalVector.append(clauseExpression);
if (clauseExpression->isNumber()) {
double value = static_cast<NumberNode*>(clauseExpression)->value();
JSValue jsValue = JSValue::makeInt32Fast(static_cast<int32_t>(value));
if ((typeForTable & ~SwitchNumber) || !jsValue || (jsValue.getInt32Fast() != value)) {
typeForTable = SwitchNeither;
break;
}
int32_t intVal = static_cast<int32_t>(value);
ASSERT(intVal == value);
if (intVal < min_num)
min_num = intVal;
if (intVal > max_num)
max_num = intVal;
typeForTable = SwitchNumber;
continue;
}
if (clauseExpression->isString()) {
if (typeForTable & ~SwitchString) {
typeForTable = SwitchNeither;
break;
}
const UString& value = static_cast<StringNode*>(clauseExpression)->value().ustring();
if (singleCharacterSwitch &= value.size() == 1) {
int32_t intVal = value.rep()->data()[0];
if (intVal < min_num)
min_num = intVal;
if (intVal > max_num)
max_num = intVal;
}
typeForTable = SwitchString;
continue;
}
typeForTable = SwitchNeither;
break;
}
}
SwitchInfo::SwitchType CaseBlockNode::tryOptimizedSwitch(Vector<ExpressionNode*, 8>& literalVector, int32_t& min_num, int32_t& max_num)
{
SwitchKind typeForTable = SwitchUnset;
bool singleCharacterSwitch = true;
processClauseList(m_list1, literalVector, typeForTable, singleCharacterSwitch, min_num, max_num);
processClauseList(m_list2, literalVector, typeForTable, singleCharacterSwitch, min_num, max_num);
if (typeForTable == SwitchUnset || typeForTable == SwitchNeither)
return SwitchInfo::SwitchNone;
if (typeForTable == SwitchNumber) {
int32_t range = max_num - min_num;
if (min_num <= max_num && range <= 1000 && (range / literalVector.size()) < 10)
return SwitchInfo::SwitchImmediate;
return SwitchInfo::SwitchNone;
}
ASSERT(typeForTable == SwitchString);
if (singleCharacterSwitch) {
int32_t range = max_num - min_num;
if (min_num <= max_num && range <= 1000 && (range / literalVector.size()) < 10)
return SwitchInfo::SwitchCharacter;
}
return SwitchInfo::SwitchString;
}
RegisterID* CaseBlockNode::emitBytecodeForBlock(BytecodeGenerator& generator, RegisterID* switchExpression, RegisterID* dst)
{
RefPtr<Label> defaultLabel;
Vector<RefPtr<Label>, 8> labelVector;
Vector<ExpressionNode*, 8> literalVector;
int32_t min_num = std::numeric_limits<int32_t>::max();
int32_t max_num = std::numeric_limits<int32_t>::min();
SwitchInfo::SwitchType switchType = tryOptimizedSwitch(literalVector, min_num, max_num);
if (switchType != SwitchInfo::SwitchNone) {
// Prepare the various labels
for (uint32_t i = 0; i < literalVector.size(); i++)
labelVector.append(generator.newLabel());
defaultLabel = generator.newLabel();
generator.beginSwitch(switchExpression, switchType);
} else {
// Setup jumps
for (ClauseListNode* list = m_list1; list; list = list->getNext()) {
RefPtr<RegisterID> clauseVal = generator.newTemporary();
generator.emitNode(clauseVal.get(), list->getClause()->expr());
generator.emitBinaryOp(op_stricteq, clauseVal.get(), clauseVal.get(), switchExpression, OperandTypes());
labelVector.append(generator.newLabel());
generator.emitJumpIfTrue(clauseVal.get(), labelVector[labelVector.size() - 1].get());
}
for (ClauseListNode* list = m_list2; list; list = list->getNext()) {
RefPtr<RegisterID> clauseVal = generator.newTemporary();
generator.emitNode(clauseVal.get(), list->getClause()->expr());
generator.emitBinaryOp(op_stricteq, clauseVal.get(), clauseVal.get(), switchExpression, OperandTypes());
labelVector.append(generator.newLabel());
generator.emitJumpIfTrue(clauseVal.get(), labelVector[labelVector.size() - 1].get());
}
defaultLabel = generator.newLabel();
generator.emitJump(defaultLabel.get());
}
RegisterID* result = 0;
size_t i = 0;
for (ClauseListNode* list = m_list1; list; list = list->getNext()) {
generator.emitLabel(labelVector[i++].get());
statementListEmitCode(list->getClause()->children(), generator, dst);
}
if (m_defaultClause) {
generator.emitLabel(defaultLabel.get());
statementListEmitCode(m_defaultClause->children(), generator, dst);
}
for (ClauseListNode* list = m_list2; list; list = list->getNext()) {
generator.emitLabel(labelVector[i++].get());
statementListEmitCode(list->getClause()->children(), generator, dst);
}
if (!m_defaultClause)
generator.emitLabel(defaultLabel.get());
ASSERT(i == labelVector.size());
if (switchType != SwitchInfo::SwitchNone) {
ASSERT(labelVector.size() == literalVector.size());
generator.endSwitch(labelVector.size(), labelVector.data(), literalVector.data(), defaultLabel.get(), min_num, max_num);
}
return result;
}
// ------------------------------ SwitchNode -----------------------------------
RegisterID* SwitchNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
RefPtr<LabelScope> scope = generator.newLabelScope(LabelScope::Switch);
RefPtr<RegisterID> r0 = generator.emitNode(m_expr);
RegisterID* r1 = m_block->emitBytecodeForBlock(generator, r0.get(), dst);
generator.emitLabel(scope->breakTarget());
return r1;
}
// ------------------------------ LabelNode ------------------------------------
RegisterID* LabelNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
if (generator.breakTarget(m_name))
return emitThrowError(generator, SyntaxError, "Duplicate label: %s.", m_name);
RefPtr<LabelScope> scope = generator.newLabelScope(LabelScope::NamedLabel, &m_name);
RegisterID* r0 = generator.emitNode(dst, m_statement);
generator.emitLabel(scope->breakTarget());
return r0;
}
// ------------------------------ ThrowNode ------------------------------------
RegisterID* ThrowNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
if (dst == generator.ignoredResult())
dst = 0;
RefPtr<RegisterID> expr = generator.emitNode(m_expr);
generator.emitExpressionInfo(divot(), startOffset(), endOffset());
generator.emitThrow(expr.get());
return 0;
}
// ------------------------------ TryNode --------------------------------------
RegisterID* TryNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
generator.emitDebugHook(WillExecuteStatement, firstLine(), lastLine());
RefPtr<Label> tryStartLabel = generator.newLabel();
RefPtr<Label> tryEndLabel = generator.newLabel();
RefPtr<Label> finallyStart;
RefPtr<RegisterID> finallyReturnAddr;
if (m_finallyBlock) {
finallyStart = generator.newLabel();
finallyReturnAddr = generator.newTemporary();
generator.pushFinallyContext(finallyStart.get(), finallyReturnAddr.get());
}
generator.emitLabel(tryStartLabel.get());
generator.emitNode(dst, m_tryBlock);
generator.emitLabel(tryEndLabel.get());
if (m_catchBlock) {
RefPtr<Label> handlerEndLabel = generator.newLabel();
generator.emitJump(handlerEndLabel.get());
RefPtr<RegisterID> exceptionRegister = generator.emitCatch(generator.newTemporary(), tryStartLabel.get(), tryEndLabel.get());
if (m_catchHasEval) {
RefPtr<RegisterID> dynamicScopeObject = generator.emitNewObject(generator.newTemporary());
generator.emitPutById(dynamicScopeObject.get(), m_exceptionIdent, exceptionRegister.get());
generator.emitMove(exceptionRegister.get(), dynamicScopeObject.get());
generator.emitPushScope(exceptionRegister.get());
} else
generator.emitPushNewScope(exceptionRegister.get(), m_exceptionIdent, exceptionRegister.get());
generator.emitNode(dst, m_catchBlock);
generator.emitPopScope();
generator.emitLabel(handlerEndLabel.get());
}
if (m_finallyBlock) {
generator.popFinallyContext();
// there may be important registers live at the time we jump
// to a finally block (such as for a return or throw) so we
// ref the highest register ever used as a conservative
// approach to not clobbering anything important
RefPtr<RegisterID> highestUsedRegister = generator.highestUsedRegister();
RefPtr<Label> finallyEndLabel = generator.newLabel();
generator.emitJumpSubroutine(finallyReturnAddr.get(), finallyStart.get());
// Use a label to record the subtle fact that sret will return to the
// next instruction. sret is the only way to jump without an explicit label.
generator.emitLabel(generator.newLabel().get());
generator.emitJump(finallyEndLabel.get());
// Finally block for exception path
RefPtr<RegisterID> tempExceptionRegister = generator.emitCatch(generator.newTemporary(), tryStartLabel.get(), generator.emitLabel(generator.newLabel().get()).get());
generator.emitJumpSubroutine(finallyReturnAddr.get(), finallyStart.get());
// Use a label to record the subtle fact that sret will return to the
// next instruction. sret is the only way to jump without an explicit label.
generator.emitLabel(generator.newLabel().get());
generator.emitThrow(tempExceptionRegister.get());
// emit the finally block itself
generator.emitLabel(finallyStart.get());
generator.emitNode(dst, m_finallyBlock);
generator.emitSubroutineReturn(finallyReturnAddr.get());
generator.emitLabel(finallyEndLabel.get());
}
return dst;
}
// -----------------------------ScopeNodeData ---------------------------
ScopeNodeData::ScopeNodeData(ParserArena& arena, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, int numConstants)
: m_numConstants(numConstants)
{
m_arena.swap(arena);
if (varStack)
m_varStack.swap(*varStack);
if (funcStack)
m_functionStack.swap(*funcStack);
if (children)
children->releaseContentsIntoVector(m_children);
}
void ScopeNodeData::mark()
{
FunctionStack::iterator end = m_functionStack.end();
for (FunctionStack::iterator ptr = m_functionStack.begin(); ptr != end; ++ptr) {
FunctionBodyNode* body = (*ptr)->body();
if (!body->isGenerated())
continue;
body->generatedBytecode().mark();
}
}
// ------------------------------ ScopeNode -----------------------------
ScopeNode::ScopeNode(JSGlobalData* globalData)
: StatementNode(globalData)
, ParserArenaRefCounted(globalData)
, m_features(NoFeatures)
{
#if ENABLE(CODEBLOCK_SAMPLING)
if (SamplingTool* sampler = globalData->interpreter->sampler())
sampler->notifyOfScope(this);
#endif
}
ScopeNode::ScopeNode(JSGlobalData* globalData, const SourceCode& source, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, CodeFeatures features, int numConstants)
: StatementNode(globalData)
, ParserArenaRefCounted(globalData)
, m_data(new ScopeNodeData(globalData->parser->arena(), children, varStack, funcStack, numConstants))
, m_features(features)
, m_source(source)
{
#if ENABLE(CODEBLOCK_SAMPLING)
if (SamplingTool* sampler = globalData->interpreter->sampler())
sampler->notifyOfScope(this);
#endif
}
// ------------------------------ ProgramNode -----------------------------
inline ProgramNode::ProgramNode(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
: ScopeNode(globalData, source, children, varStack, funcStack, features, numConstants)
{
}
PassRefPtr<ProgramNode> ProgramNode::create(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
{
RefPtr<ProgramNode> node = new ProgramNode(globalData, children, varStack, funcStack, source, features, numConstants);
ASSERT(node->data()->m_arena.last() == node);
node->data()->m_arena.removeLast();
ASSERT(!node->data()->m_arena.contains(node.get()));
return node.release();
}
RegisterID* ProgramNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
generator.emitDebugHook(WillExecuteProgram, firstLine(), lastLine());
RefPtr<RegisterID> dstRegister = generator.newTemporary();
generator.emitLoad(dstRegister.get(), jsUndefined());
statementListEmitCode(children(), generator, dstRegister.get());
generator.emitDebugHook(DidExecuteProgram, firstLine(), lastLine());
generator.emitEnd(dstRegister.get());
return 0;
}
void ProgramNode::generateBytecode(ScopeChainNode* scopeChainNode)
{
ScopeChain scopeChain(scopeChainNode);
JSGlobalObject* globalObject = scopeChain.globalObject();
m_code.set(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider()));
OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(this, globalObject->debugger(), scopeChain, &globalObject->symbolTable(), m_code.get()));
generator->generate();
destroyData();
}
#if ENABLE(JIT)
void ProgramNode::generateJITCode(ScopeChainNode* scopeChainNode)
{
bytecode(scopeChainNode);
ASSERT(m_code);
ASSERT(!m_jitCode);
JIT::compile(scopeChainNode->globalData, m_code.get());
ASSERT(m_jitCode);
}
#endif
// ------------------------------ EvalNode -----------------------------
inline EvalNode::EvalNode(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
: ScopeNode(globalData, source, children, varStack, funcStack, features, numConstants)
{
}
PassRefPtr<EvalNode> EvalNode::create(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
{
RefPtr<EvalNode> node = new EvalNode(globalData, children, varStack, funcStack, source, features, numConstants);
ASSERT(node->data()->m_arena.last() == node);
node->data()->m_arena.removeLast();
ASSERT(!node->data()->m_arena.contains(node.get()));
return node.release();
}
RegisterID* EvalNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
generator.emitDebugHook(WillExecuteProgram, firstLine(), lastLine());
RefPtr<RegisterID> dstRegister = generator.newTemporary();
generator.emitLoad(dstRegister.get(), jsUndefined());
statementListEmitCode(children(), generator, dstRegister.get());
generator.emitDebugHook(DidExecuteProgram, firstLine(), lastLine());
generator.emitEnd(dstRegister.get());
return 0;
}
void EvalNode::generateBytecode(ScopeChainNode* scopeChainNode)
{
ScopeChain scopeChain(scopeChainNode);
JSGlobalObject* globalObject = scopeChain.globalObject();
m_code.set(new EvalCodeBlock(this, globalObject, source().provider(), scopeChain.localDepth()));
OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(this, globalObject->debugger(), scopeChain, &m_code->symbolTable(), m_code.get()));
generator->generate();
// Eval code needs to hang on to its declaration stacks to keep declaration info alive until Interpreter::execute time,
// so the entire ScopeNodeData cannot be destoyed.
children().clear();
}
EvalCodeBlock& EvalNode::bytecodeForExceptionInfoReparse(ScopeChainNode* scopeChainNode, CodeBlock* codeBlockBeingRegeneratedFrom)
{
ASSERT(!m_code);
ScopeChain scopeChain(scopeChainNode);
JSGlobalObject* globalObject = scopeChain.globalObject();
m_code.set(new EvalCodeBlock(this, globalObject, source().provider(), scopeChain.localDepth()));
OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(this, globalObject->debugger(), scopeChain, &m_code->symbolTable(), m_code.get()));
generator->setRegeneratingForExceptionInfo(codeBlockBeingRegeneratedFrom);
generator->generate();
return *m_code;
}
void EvalNode::mark()
{
// We don't need to mark our own CodeBlock as the JSGlobalObject takes care of that
data()->mark();
}
#if ENABLE(JIT)
void EvalNode::generateJITCode(ScopeChainNode* scopeChainNode)
{
bytecode(scopeChainNode);
ASSERT(m_code);
ASSERT(!m_jitCode);
JIT::compile(scopeChainNode->globalData, m_code.get());
ASSERT(m_jitCode);
}
#endif
// ------------------------------ FunctionBodyNode -----------------------------
inline FunctionBodyNode::FunctionBodyNode(JSGlobalData* globalData)
: ScopeNode(globalData)
, m_parameters(0)
, m_parameterCount(0)
{
}
inline FunctionBodyNode::FunctionBodyNode(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
: ScopeNode(globalData, sourceCode, children, varStack, funcStack, features, numConstants)
, m_parameters(0)
, m_parameterCount(0)
{
}
FunctionBodyNode::~FunctionBodyNode()
{
for (size_t i = 0; i < m_parameterCount; ++i)
m_parameters[i].~Identifier();
fastFree(m_parameters);
}
void FunctionBodyNode::finishParsing(const SourceCode& source, ParameterNode* firstParameter)
{
Vector<Identifier> parameters;
for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam())
parameters.append(parameter->ident());
size_t count = parameters.size();
setSource(source);
finishParsing(parameters.releaseBuffer(), count);
}
void FunctionBodyNode::finishParsing(Identifier* parameters, size_t parameterCount)
{
ASSERT(!source().isNull());
m_parameters = parameters;
m_parameterCount = parameterCount;
}
void FunctionBodyNode::mark()
{
if (m_code)
m_code->mark();
}
#if ENABLE(JIT)
PassRefPtr<FunctionBodyNode> FunctionBodyNode::createNativeThunk(JSGlobalData* globalData)
{
RefPtr<FunctionBodyNode> body = new FunctionBodyNode(globalData);
globalData->parser->arena().reset();
body->m_code.set(new CodeBlock(body.get()));
body->m_jitCode = JITCode(JITCode::HostFunction(globalData->jitStubs.ctiNativeCallThunk()));
return body.release();
}
#endif
bool FunctionBodyNode::isHostFunction() const
{
return m_code && m_code->codeType() == NativeCode;
}
FunctionBodyNode* FunctionBodyNode::create(JSGlobalData* globalData)
{
return new FunctionBodyNode(globalData);
}
PassRefPtr<FunctionBodyNode> FunctionBodyNode::create(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
{
RefPtr<FunctionBodyNode> node = new FunctionBodyNode(globalData, children, varStack, funcStack, sourceCode, features, numConstants);
ASSERT(node->data()->m_arena.last() == node);
node->data()->m_arena.removeLast();
ASSERT(!node->data()->m_arena.contains(node.get()));
return node.release();
}
void FunctionBodyNode::generateBytecode(ScopeChainNode* scopeChainNode)
{
// This branch is only necessary since you can still create a non-stub FunctionBodyNode by
// calling Parser::parse<FunctionBodyNode>().
if (!data())
scopeChainNode->globalData->parser->reparseInPlace(scopeChainNode->globalData, this);
ASSERT(data());
ScopeChain scopeChain(scopeChainNode);
JSGlobalObject* globalObject = scopeChain.globalObject();
m_code.set(new CodeBlock(this, FunctionCode, source().provider(), source().startOffset()));
OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(this, globalObject->debugger(), scopeChain, &m_code->symbolTable(), m_code.get()));
generator->generate();
destroyData();
}
#if ENABLE(JIT)
void FunctionBodyNode::generateJITCode(ScopeChainNode* scopeChainNode)
{
bytecode(scopeChainNode);
ASSERT(m_code);
ASSERT(!m_jitCode);
JIT::compile(scopeChainNode->globalData, m_code.get());
ASSERT(m_jitCode);
}
#endif
CodeBlock& FunctionBodyNode::bytecodeForExceptionInfoReparse(ScopeChainNode* scopeChainNode, CodeBlock* codeBlockBeingRegeneratedFrom)
{
ASSERT(!m_code);
ScopeChain scopeChain(scopeChainNode);
JSGlobalObject* globalObject = scopeChain.globalObject();
m_code.set(new CodeBlock(this, FunctionCode, source().provider(), source().startOffset()));
OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(this, globalObject->debugger(), scopeChain, &m_code->symbolTable(), m_code.get()));
generator->setRegeneratingForExceptionInfo(codeBlockBeingRegeneratedFrom);
generator->generate();
return *m_code;
}
RegisterID* FunctionBodyNode::emitBytecode(BytecodeGenerator& generator, RegisterID*)
{
generator.emitDebugHook(DidEnterCallFrame, firstLine(), lastLine());
statementListEmitCode(children(), generator, generator.ignoredResult());
if (children().size() && children().last()->isBlock()) {
BlockNode* blockNode = static_cast<BlockNode*>(children().last());
if (blockNode->children().size() && blockNode->children().last()->isReturnNode())
return 0;
}
RegisterID* r0 = generator.emitLoad(0, jsUndefined());
generator.emitDebugHook(WillLeaveCallFrame, firstLine(), lastLine());
generator.emitReturn(r0);
return 0;
}
UString FunctionBodyNode::paramString() const
{
UString s("");
for (size_t pos = 0; pos < m_parameterCount; ++pos) {
if (!s.isEmpty())
s += ", ";
s += parameters()[pos].ustring();
}
return s;
}
Identifier* FunctionBodyNode::copyParameters()
{
Identifier* parameters = static_cast<Identifier*>(fastMalloc(m_parameterCount * sizeof(Identifier)));
VectorCopier<false, Identifier>::uninitializedCopy(m_parameters, m_parameters + m_parameterCount, parameters);
return parameters;
}
// ------------------------------ FuncDeclNode ---------------------------------
JSFunction* FuncDeclNode::makeFunction(ExecState* exec, ScopeChainNode* scopeChain)
{
return new (exec) JSFunction(exec, m_ident, m_body.get(), scopeChain);
}
RegisterID* FuncDeclNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
if (dst == generator.ignoredResult())
dst = 0;
return dst;
}
// ------------------------------ FuncExprNode ---------------------------------
RegisterID* FuncExprNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst)
{
return generator.emitNewFunctionExpression(generator.finalDestination(dst), this);
}
JSFunction* FuncExprNode::makeFunction(ExecState* exec, ScopeChainNode* scopeChain)
{
JSFunction* func = new (exec) JSFunction(exec, m_ident, m_body.get(), scopeChain);
/*
The Identifier in a FunctionExpression can be referenced from inside
the FunctionExpression's FunctionBody to allow the function to call
itself recursively. However, unlike in a FunctionDeclaration, the
Identifier in a FunctionExpression cannot be referenced from and
does not affect the scope enclosing the FunctionExpression.
*/
if (!m_ident.isNull()) {
JSStaticScopeObject* functionScopeObject = new (exec) JSStaticScopeObject(exec, m_ident, func, ReadOnly | DontDelete);
func->scope().push(functionScopeObject);
}
return func;
}
} // namespace JSC
|