Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8ee6dd97c824c00e86b110c763978f8f17499a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
/*******************************************************************************
 * Copyright (c) 2011-2012 Vrije Universiteit Brussel.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Dennis Wagelaar, Vrije Universiteit Brussel - initial API and
 *         implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.m2m.atl.emftvm.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.m2m.atl.emftvm.Add;
import org.eclipse.m2m.atl.emftvm.And;
import org.eclipse.m2m.atl.emftvm.BranchInstruction;
import org.eclipse.m2m.atl.emftvm.CodeBlock;
import org.eclipse.m2m.atl.emftvm.EmftvmPackage;
import org.eclipse.m2m.atl.emftvm.Enditerate;
import org.eclipse.m2m.atl.emftvm.ExecEnv;
import org.eclipse.m2m.atl.emftvm.Feature;
import org.eclipse.m2m.atl.emftvm.Field;
import org.eclipse.m2m.atl.emftvm.Findtype;
import org.eclipse.m2m.atl.emftvm.Get;
import org.eclipse.m2m.atl.emftvm.GetStatic;
import org.eclipse.m2m.atl.emftvm.GetSuper;
import org.eclipse.m2m.atl.emftvm.GetTrans;
import org.eclipse.m2m.atl.emftvm.Getcb;
import org.eclipse.m2m.atl.emftvm.Goto;
import org.eclipse.m2m.atl.emftvm.If;
import org.eclipse.m2m.atl.emftvm.Ifn;
import org.eclipse.m2m.atl.emftvm.Ifte;
import org.eclipse.m2m.atl.emftvm.Implies;
import org.eclipse.m2m.atl.emftvm.InputRuleElement;
import org.eclipse.m2m.atl.emftvm.Insert;
import org.eclipse.m2m.atl.emftvm.Instruction;
import org.eclipse.m2m.atl.emftvm.Invoke;
import org.eclipse.m2m.atl.emftvm.InvokeAllCbs;
import org.eclipse.m2m.atl.emftvm.InvokeCb;
import org.eclipse.m2m.atl.emftvm.InvokeCbS;
import org.eclipse.m2m.atl.emftvm.InvokeStatic;
import org.eclipse.m2m.atl.emftvm.InvokeSuper;
import org.eclipse.m2m.atl.emftvm.Iterate;
import org.eclipse.m2m.atl.emftvm.LineNumber;
import org.eclipse.m2m.atl.emftvm.Load;
import org.eclipse.m2m.atl.emftvm.LocalVariable;
import org.eclipse.m2m.atl.emftvm.Match;
import org.eclipse.m2m.atl.emftvm.MatchS;
import org.eclipse.m2m.atl.emftvm.Model;
import org.eclipse.m2m.atl.emftvm.Module;
import org.eclipse.m2m.atl.emftvm.New;
import org.eclipse.m2m.atl.emftvm.Operation;
import org.eclipse.m2m.atl.emftvm.Or;
import org.eclipse.m2m.atl.emftvm.Push;
import org.eclipse.m2m.atl.emftvm.Remove;
import org.eclipse.m2m.atl.emftvm.Rule;
import org.eclipse.m2m.atl.emftvm.RuleMode;
import org.eclipse.m2m.atl.emftvm.Set;
import org.eclipse.m2m.atl.emftvm.SetStatic;
import org.eclipse.m2m.atl.emftvm.Store;
import org.eclipse.m2m.atl.emftvm.jit.CodeBlockJIT;
import org.eclipse.m2m.atl.emftvm.jit.JITCodeBlock;
import org.eclipse.m2m.atl.emftvm.util.DuplicateEntryException;
import org.eclipse.m2m.atl.emftvm.util.EMFTVMUtil;
import org.eclipse.m2m.atl.emftvm.util.LazyBagOnCollection;
import org.eclipse.m2m.atl.emftvm.util.LazyList;
import org.eclipse.m2m.atl.emftvm.util.LazyListOnList;
import org.eclipse.m2m.atl.emftvm.util.LazySetOnSet;
import org.eclipse.m2m.atl.emftvm.util.NativeTypes;
import org.eclipse.m2m.atl.emftvm.util.Stack;
import org.eclipse.m2m.atl.emftvm.util.StackFrame;
import org.eclipse.m2m.atl.emftvm.util.VMException;
import org.eclipse.m2m.atl.emftvm.util.VMMonitor;


/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Code Block</b></em>'.
 * @author <a href="mailto:dennis.wagelaar@vub.ac.be">Dennis Wagelaar</a>
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getMaxLocals <em>Max Locals</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getMaxStack <em>Max Stack</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getCode <em>Code</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getLineNumbers <em>Line Numbers</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getLocalVariables <em>Local Variables</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getMatcherFor <em>Matcher For</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getApplierFor <em>Applier For</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getPostApplyFor <em>Post Apply For</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getBodyFor <em>Body For</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getInitialiserFor <em>Initialiser For</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getNested <em>Nested</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getNestedFor <em>Nested For</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getParentFrame <em>Parent Frame</em>}</li>
 *   <li>{@link org.eclipse.m2m.atl.emftvm.impl.CodeBlockImpl#getBindingFor <em>Binding For</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class CodeBlockImpl extends EObjectImpl implements CodeBlock {

	/**
	 * The default value of the '{@link #getMaxLocals() <em>Max Locals</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getMaxLocals()
	 * @generated
	 * @ordered
	 */
	protected static final int MAX_LOCALS_EDEFAULT = -1;

	/**
	 * The default value of the '{@link #getMaxStack() <em>Max Stack</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getMaxStack()
	 * @generated
	 * @ordered
	 */
	protected static final int MAX_STACK_EDEFAULT = -1;

	/**
	 * The cached value of the '{@link #getMaxLocals() <em>Max Locals</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getMaxLocals()
	 * @generated NOT
	 * @ordered
	 */
	protected int maxLocals = MAX_LOCALS_EDEFAULT;

	/**
	 * The cached value of the '{@link #getMaxStack() <em>Max Stack</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getMaxStack()
	 * @generated NOT
	 * @ordered
	 */
	protected int maxStack = MAX_STACK_EDEFAULT;

	/**
	 * The cached value of the '{@link #getCode() <em>Code</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCode()
	 * @generated
	 * @ordered
	 */
	protected EList<Instruction> code;

	/**
	 * The cached value of the '{@link #getLineNumbers() <em>Line Numbers</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLineNumbers()
	 * @generated
	 * @ordered
	 */
	protected EList<LineNumber> lineNumbers;

	/**
	 * The cached value of the '{@link #getLocalVariables() <em>Local Variables</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLocalVariables()
	 * @generated
	 * @ordered
	 */
	protected EList<LocalVariable> localVariables;

	/**
	 * The cached value of the '{@link #getNested() <em>Nested</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNested()
	 * @generated
	 * @ordered
	 */
	protected EList<CodeBlock> nested;

	/**
	 * The default value of the '{@link #getParentFrame() <em>Parent Frame</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getParentFrame()
	 * @generated
	 * @ordered
	 */
	protected static final StackFrame PARENT_FRAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getParentFrame() <em>Parent Frame</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getParentFrame()
	 * @generated NOT
	 * @ordered
	 */
	protected Map<Thread, StackFrame> parentFrame = Collections.synchronizedMap(new WeakHashMap<Thread, StackFrame>());

	/**
	 * Singleton instance of the {@link ExecEnv} {@link EClass}.
	 */
	protected static final EClass EXEC_ENV = EmftvmPackage.eINSTANCE.getExecEnv();

	private static final Object[] EMPTY = new Object[0];
	private static final int JIT_THRESHOLD = 100; // require > JIT_THRESHOLD runs before JIT-ing

	private boolean ruleSet;
	private Rule rule;
	private Map<Instruction, EList<Instruction>> predecessors = new HashMap<Instruction, EList<Instruction>>();
	private Map<Instruction, EList<Instruction>> allPredecessors = new HashMap<Instruction, EList<Instruction>>();
	private Map<Instruction, EList<Instruction>> nlPredecessors = new HashMap<Instruction, EList<Instruction>>();
	private JITCodeBlock jitCodeBlock;
	private int runcount;

	/**
	 * <!-- begin-user-doc -->
	 * Creates a new {@link CodeBlockImpl}.
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected CodeBlockImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * Returns the {@link EClass} that correspond to this metaclass.
	 * @return the {@link EClass} that correspond to this metaclass.
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EmftvmPackage.Literals.CODE_BLOCK;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public int getMaxLocals() {
		if (maxLocals == MAX_LOCALS_EDEFAULT) {
			for (LocalVariable lv : getLocalVariables()) {
				maxLocals = Math.max(maxLocals, lv.getSlot());
			}
			maxLocals++; // highest index + 1
		}
		return maxLocals;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setMaxLocals(int newMaxLocals) {
		int oldMaxLocals = maxLocals;
		maxLocals = newMaxLocals;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__MAX_LOCALS, oldMaxLocals, maxLocals));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public int getMaxStack() {
		if (maxStack == MAX_STACK_EDEFAULT) {
			maxStack = 0;
			for (Instruction instr : getCode()) {
				maxStack = Math.max(maxStack, instr.getStackLevel());
			}
		}
		return maxStack;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setMaxStack(int newMaxStack) {
		int oldMaxStack = maxStack;
		maxStack = newMaxStack;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__MAX_STACK, oldMaxStack, maxStack));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Instruction> getCode() {
		if (code == null) {
			code = new EObjectContainmentWithInverseEList<Instruction>(Instruction.class, this, EmftvmPackage.CODE_BLOCK__CODE, EmftvmPackage.INSTRUCTION__OWNING_BLOCK);
		}
		return code;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<LineNumber> getLineNumbers() {
		if (lineNumbers == null) {
			lineNumbers = new EObjectContainmentWithInverseEList<LineNumber>(LineNumber.class, this, EmftvmPackage.CODE_BLOCK__LINE_NUMBERS, EmftvmPackage.LINE_NUMBER__OWNING_BLOCK);
		}
		return lineNumbers;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<LocalVariable> getLocalVariables() {
		if (localVariables == null) {
			localVariables = new EObjectContainmentWithInverseEList<LocalVariable>(LocalVariable.class, this, EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES, EmftvmPackage.LOCAL_VARIABLE__OWNING_BLOCK);
		}
		return localVariables;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Rule getMatcherFor() {
		if (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__MATCHER_FOR) return null;
		return (Rule)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * @see CodeBlockImpl#setMatcherFor(Rule)
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetMatcherFor(Rule newMatcherFor, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newMatcherFor, EmftvmPackage.CODE_BLOCK__MATCHER_FOR, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setMatcherFor(Rule newMatcherFor) {
		if (newMatcherFor != eInternalContainer() || (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__MATCHER_FOR && newMatcherFor != null)) {
			if (EcoreUtil.isAncestor(this, newMatcherFor))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newMatcherFor != null)
				msgs = ((InternalEObject)newMatcherFor).eInverseAdd(this, EmftvmPackage.RULE__MATCHER, Rule.class, msgs);
			msgs = basicSetMatcherFor(newMatcherFor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__MATCHER_FOR, newMatcherFor, newMatcherFor));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Rule getApplierFor() {
		if (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__APPLIER_FOR) return null;
		return (Rule)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * @see #setApplierFor(Rule)
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetApplierFor(Rule newApplierFor, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newApplierFor, EmftvmPackage.CODE_BLOCK__APPLIER_FOR, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setApplierFor(Rule newApplierFor) {
		if (newApplierFor != eInternalContainer() || (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__APPLIER_FOR && newApplierFor != null)) {
			if (EcoreUtil.isAncestor(this, newApplierFor))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newApplierFor != null)
				msgs = ((InternalEObject)newApplierFor).eInverseAdd(this, EmftvmPackage.RULE__APPLIER, Rule.class, msgs);
			msgs = basicSetApplierFor(newApplierFor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__APPLIER_FOR, newApplierFor, newApplierFor));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Rule getPostApplyFor() {
		if (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR) return null;
		return (Rule)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * @see #setPostApplyFor(Rule)
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetPostApplyFor(Rule newPostApplyFor, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newPostApplyFor, EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setPostApplyFor(Rule newPostApplyFor) {
		if (newPostApplyFor != eInternalContainer() || (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR && newPostApplyFor != null)) {
			if (EcoreUtil.isAncestor(this, newPostApplyFor))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newPostApplyFor != null)
				msgs = ((InternalEObject)newPostApplyFor).eInverseAdd(this, EmftvmPackage.RULE__POST_APPLY, Rule.class, msgs);
			msgs = basicSetPostApplyFor(newPostApplyFor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR, newPostApplyFor, newPostApplyFor));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Operation getBodyFor() {
		if (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__BODY_FOR) return null;
		return (Operation)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * @see #setBodyFor(Operation)
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetBodyFor(Operation newBodyFor, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newBodyFor, EmftvmPackage.CODE_BLOCK__BODY_FOR, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBodyFor(Operation newBodyFor) {
		if (newBodyFor != eInternalContainer() || (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__BODY_FOR && newBodyFor != null)) {
			if (EcoreUtil.isAncestor(this, newBodyFor))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newBodyFor != null)
				msgs = ((InternalEObject)newBodyFor).eInverseAdd(this, EmftvmPackage.OPERATION__BODY, Operation.class, msgs);
			msgs = basicSetBodyFor(newBodyFor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__BODY_FOR, newBodyFor, newBodyFor));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Field getInitialiserFor() {
		if (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__INITIALISER_FOR) return null;
		return (Field)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * @see #setInitialiserFor(Field)
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetInitialiserFor(Field newInitialiserFor, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newInitialiserFor, EmftvmPackage.CODE_BLOCK__INITIALISER_FOR, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setInitialiserFor(Field newInitialiserFor) {
		if (newInitialiserFor != eInternalContainer() || (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__INITIALISER_FOR && newInitialiserFor != null)) {
			if (EcoreUtil.isAncestor(this, newInitialiserFor))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newInitialiserFor != null)
				msgs = ((InternalEObject)newInitialiserFor).eInverseAdd(this, EmftvmPackage.FIELD__INITIALISER, Field.class, msgs);
			msgs = basicSetInitialiserFor(newInitialiserFor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__INITIALISER_FOR, newInitialiserFor, newInitialiserFor));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<CodeBlock> getNested() {
		if (nested == null) {
			nested = new EObjectContainmentWithInverseEList<CodeBlock>(CodeBlock.class, this, EmftvmPackage.CODE_BLOCK__NESTED, EmftvmPackage.CODE_BLOCK__NESTED_FOR);
		}
		return nested;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public CodeBlock getNestedFor() {
		if (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__NESTED_FOR) return null;
		return (CodeBlock)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * @see #setNestedFor(CodeBlock)
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetNestedFor(CodeBlock newNestedFor, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newNestedFor, EmftvmPackage.CODE_BLOCK__NESTED_FOR, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setNestedFor(CodeBlock newNestedFor) {
		if (newNestedFor != eInternalContainer() || (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__NESTED_FOR && newNestedFor != null)) {
			if (EcoreUtil.isAncestor(this, newNestedFor))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newNestedFor != null)
				msgs = ((InternalEObject)newNestedFor).eInverseAdd(this, EmftvmPackage.CODE_BLOCK__NESTED, CodeBlock.class, msgs);
			msgs = basicSetNestedFor(newNestedFor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__NESTED_FOR, newNestedFor, newNestedFor));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public StackFrame getParentFrame() {
		return parentFrame.get(Thread.currentThread());
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setParentFrame(final StackFrame newParentFrame) {
		final Thread currentThread = Thread.currentThread();
		final StackFrame oldParentFrame = parentFrame.get(currentThread);
		parentFrame.put(currentThread, newParentFrame);
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__PARENT_FRAME, oldParentFrame, newParentFrame));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public InputRuleElement getBindingFor() {
		if (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__BINDING_FOR) return null;
		return (InputRuleElement)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * @see #setBindingFor(InputRuleElement)
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetBindingFor(InputRuleElement newBindingFor, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newBindingFor, EmftvmPackage.CODE_BLOCK__BINDING_FOR, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBindingFor(InputRuleElement newBindingFor) {
		if (newBindingFor != eInternalContainer() || (eContainerFeatureID() != EmftvmPackage.CODE_BLOCK__BINDING_FOR && newBindingFor != null)) {
			if (EcoreUtil.isAncestor(this, newBindingFor))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newBindingFor != null)
				msgs = ((InternalEObject)newBindingFor).eInverseAdd(this, EmftvmPackage.INPUT_RULE_ELEMENT__BINDING, InputRuleElement.class, msgs);
			msgs = basicSetBindingFor(newBindingFor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EmftvmPackage.CODE_BLOCK__BINDING_FOR, newBindingFor, newBindingFor));
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}

	 * @see org.eclipse.m2m.atl.emftvm.CodeBlock#execute(StackFrame)
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Object execute(final StackFrame frame) {
		final JITCodeBlock jcb = getJITCodeBlock();
		if (jcb != null) {
			return jcb.execute(frame);
		}
		return internalExecute(frame);
	}

	private Object internalExecute(final StackFrame frame) {
		runcount += 1; // increase invocation counter to trigger JIT

		int pc = 0;
		final EList<Instruction> code = getCode();
		final int codeSize = code.size();
		final ExecEnv env = frame.getEnv();
		final VMMonitor monitor = env.getMonitor();
		final Stack stack = new Stack(getMaxStack());
		CodeBlock cb;
		int argcount;

		if (monitor != null) {
			monitor.enter(frame);
		}

		try {
			LOOP: while (pc < codeSize) {
				Instruction instr = code.get(pc++);
				if (monitor != null) {
					if (monitor.isTerminated()) {
						throw new VMException(frame, "Execution terminated.");
					} else {
						frame.setPc(pc);
						monitor.step(frame);
					}
				}
				switch (instr.getOpcode()) {
				case PUSH:
					stack.push(((Push) instr).getValue());
					break;
				case PUSHT:
					stack.push(true);
					break;
				case PUSHF:
					stack.push(false);
					break;
				case POP:
					stack.popv();
					break;
				case LOAD:
					stack.push(frame.getLocal(((Load) instr).getCbOffset(), ((Load) instr).getSlot()));
					break;
				case STORE:
					frame.setLocal(stack.pop(), ((Store) instr).getCbOffset(), ((Store) instr).getSlot());
					break;
				case SET:
					frame.setPc(pc);
					set(stack.pop(), stack.pop(), ((Set) instr).getFieldname(), frame);
					break;
				case GET:
					frame.setPc(pc);
					stack.push(get(((Get) instr).getFieldname(), frame, stack.pop()));
					break;
				case GET_TRANS:
					frame.setPc(pc);
					stack.push(getTrans(((GetTrans) instr).getFieldname(), frame, stack.pop()));
					break;
				case SET_STATIC:
					setStatic(stack.pop(), stack.pop(), ((SetStatic) instr).getFieldname(), env);
					break;
				case GET_STATIC:
					frame.setPc(pc);
					stack.push(getStatic(((GetStatic) instr).getFieldname(), frame, stack.pop()));
					break;
				case FINDTYPE:
					stack.push(frame.getEnv().findType(((Findtype) instr).getModelname(), ((Findtype) instr).getTypename()));
					break;
				case FINDTYPE_S:
					stack.push(frame.getEnv().findType((String) stack.pop(), (String) stack.pop()));
					break;
				case NEW:
					stack.push(newInstr(((New) instr).getModelname(), stack.pop(), frame));
					break;
				case NEW_S:
					stack.push(newInstr((String) stack.pop(), stack.pop(), frame));
					break;
				case DELETE:
					frame.setPc(pc);
					delete(frame, (EObject) stack.pop());
					break;
				case DUP:
					stack.dup();
					break;
				case DUP_X1:
					stack.dupX1();
					break;
				case SWAP:
					stack.swap();
					break;
				case SWAP_X1:
					stack.swapX1();
					break;
				case IF:
					if ((Boolean) stack.pop()) {
						pc = ((If) instr).getOffset();
					}
					break;
				case IFN:
					if (!(Boolean) stack.pop()) {
						pc = ((Ifn) instr).getOffset();
					}
					break;
				case GOTO:
					pc = ((Goto) instr).getOffset();
					break;
				case ITERATE:
					Iterator<?> i = ((Collection<?>) stack.pop()).iterator();
					if (i.hasNext()) {
						stack.push(i);
						stack.push(i.next());
					} else {
						pc = ((Iterate) instr).getOffset(); // jump over ENDITERATE
					}
					break;
				case ENDITERATE:
					i = (Iterator<?>) stack.pop();
					if (i.hasNext()) {
						stack.push(i);
						stack.push(i.next());
						pc = ((Enditerate) instr).getOffset(); // jump to first loop instruction
					}
					break;
				case INVOKE:
					frame.setPc(pc);
					stack.push(invoke((Invoke) instr, frame, stack));
					break;
				case INVOKE_STATIC:
					frame.setPc(pc);
					stack.push(invokeStatic(((InvokeStatic) instr).getOpname(), ((InvokeStatic) instr).getArgcount(), frame, stack));
					break;
				case INVOKE_SUPER:
					frame.setPc(pc);
					stack.push(invokeSuper(getOperation(), ((InvokeSuper) instr).getOpname(), ((InvokeSuper) instr).getArgcount(), frame,
							stack));
					break;
				case ALLINST:
					stack.push(EMFTVMUtil.findAllInstances((EClass) stack.pop(), env));
					break;
				case ALLINST_IN:
					stack.push(EMFTVMUtil.findAllInstIn(stack.pop(), (EClass) stack.pop(), env));
					break;
				case ISNULL:
					stack.push(stack.pop() == null);
					break;
				case GETENVTYPE:
					stack.push(EXEC_ENV);
					break;
				case NOT:
					stack.push(!(Boolean) stack.pop());
					break;
				case AND:
					cb = ((And) instr).getCodeBlock();
					frame.setPc(pc);
					stack.push((Boolean) stack.pop() && (Boolean) cb.execute(new StackFrame(frame, cb)));
					break;
				case OR:
					cb = ((Or) instr).getCodeBlock();
					frame.setPc(pc);
					stack.push((Boolean) stack.pop() || (Boolean) cb.execute(new StackFrame(frame, cb)));
					break;
				case XOR:
					stack.push((Boolean) stack.pop() ^ (Boolean) stack.pop());
					break;
				case IMPLIES:
					cb = ((Implies) instr).getCodeBlock();
					frame.setPc(pc);
					stack.push(!(Boolean) stack.pop() || (Boolean) cb.execute(new StackFrame(frame, cb)));
					break;
				case IFTE:
					frame.setPc(pc);
					if ((Boolean) stack.pop()) {
						cb = ((Ifte) instr).getThenCb();
					} else {
						cb = ((Ifte) instr).getElseCb();
					}
					stack.push(cb.execute(new StackFrame(frame, cb)));
					break;
				case RETURN:
					break LOOP;
				case GETCB:
					stack.push(((Getcb) instr).getCodeBlock());
					break;
				case INVOKE_ALL_CBS:
					frame.setPc(pc);
					// Use Java's left-to-right evaluation semantics:
					// stack = [..., arg1, arg2]
					argcount = ((InvokeAllCbs) instr).getArgcount();
					Object[] args = argcount > 0 ? stack.pop(argcount) : EMPTY;
					for (CodeBlock ncb : getNested()) {
						if (ncb.getStackLevel() > 0) {
							stack.push(ncb.execute(frame.getSubFrame(ncb, args)));
						} else {
							ncb.execute(frame.getSubFrame(ncb, args));
						}
					}
					break;
				case INVOKE_CB:
					cb = ((InvokeCb) instr).getCodeBlock();
					frame.setPc(pc);
					// Use Java's left-to-right evaluation semantics:
					// stack = [..., arg1, arg2]
					argcount = ((InvokeCb) instr).getArgcount();
					if (cb.getStackLevel() > 0) {
						stack.push(cb.execute(frame.getSubFrame(cb, argcount > 0 ? stack.pop(argcount) : EMPTY)));
					} else {
						cb.execute(frame.getSubFrame(cb, argcount > 0 ? stack.pop(argcount) : EMPTY));
					}
					break;
				case INVOKE_CB_S:
					cb = (CodeBlock) stack.pop();
					frame.setPc(pc);
					// Use Java's left-to-right evaluation semantics:
					// stack = [..., arg1, arg2]
					argcount = ((InvokeCbS) instr).getArgcount();
					// unknown code block => always produce one stack element
					stack.push(cb.execute(frame.getSubFrame(cb, argcount > 0 ? stack.pop(argcount) : EMPTY)));
					break;
				case MATCH:
					frame.setPc(pc);
					// Use Java's left-to-right evaluation semantics:
					// stack = [..., arg1, arg2]
					argcount = ((Match) instr).getArgcount();
					stack.push(argcount > 0 ? matchOne(frame, findRule(frame.getEnv(), ((Match) instr).getRulename()),
							stack.pop(argcount, new EObject[argcount])) : matchOne(frame,
							findRule(frame.getEnv(), ((Match) instr).getRulename())));
					break;
				case MATCH_S:
					frame.setPc(pc);
					// stack = [..., arg1, arg2, rule]
					argcount = ((MatchS) instr).getArgcount();
					stack.push(argcount > 0 ? matchOne(frame, (Rule) stack.pop(), stack.pop(argcount, new EObject[argcount])) : matchOne(
							frame, (Rule) stack.pop()));
					break;
				case ADD:
					add(-1, stack.pop(), stack.pop(), ((Add) instr).getFieldname(), frame);
					break;
				case REMOVE:
					remove(stack.pop(), stack.pop(), ((Remove) instr).getFieldname(), frame);
					break;
				case INSERT:
					add((Integer) stack.pop(), stack.pop(), stack.pop(), ((Insert) instr).getFieldname(), frame);
					break;
				case GET_SUPER:
					frame.setPc(pc);
					stack.push(getSuper(getField(), ((GetSuper) instr).getFieldname(), frame, stack.pop()));
					break;
				case GETENV:
					stack.push(env);
					break;
				default:
					throw new VMException(frame, String.format("Unsupported opcode: %s", instr.getOpcode()));
				} // switch
			} // while
		} catch (VMException e) {
			throw e;
		} catch (Exception e) {
			frame.setPc(pc);
			throw new VMException(frame, e);
		}

		if (monitor != null) {
			monitor.leave(frame);
		}

		final CodeBlockJIT jc = env.getJITCompiler();
		if (jc != null && runcount > JIT_THRESHOLD) { // JIT everything that runs more than JIT_THRESHOLD
			synchronized (this) {
				if (getJITCodeBlock() == null) {
					try {
						setJITCodeBlock(jc.jit(this));
					} catch (Exception e) {
						frame.setPc(pc);
						throw new VMException(frame, e);
					}
				}
			}
		}

		return stack.stackEmpty() ? null : stack.pop();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public int getStackLevel() {
		final EList<Instruction> code = getCode();
		if (code.isEmpty()) {
			return 0;
		}
		return code.get(code.size() - 1).getStackLevel();
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Module getModule() {
		final EObject container = eContainer();
		if (container != null) {
			switch (container.eClass().getClassifierID()) {
			case EmftvmPackage.FEATURE:
			case EmftvmPackage.FIELD:
			case EmftvmPackage.OPERATION:
				return ((Feature)container).getModule();
			case EmftvmPackage.RULE:
				return ((Rule)container).getModule();
			case EmftvmPackage.INPUT_RULE_ELEMENT:
				return ((InputRuleElement)container).getInputFor().getModule();
			case EmftvmPackage.CODE_BLOCK:
				return ((CodeBlock)container).getModule();
			default:
				break;
			}
		}
		return null;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Operation getOperation() {
		final EObject container = eContainer();
		if (container != null) {
			switch (container.eClass().getClassifierID()) {
			case EmftvmPackage.OPERATION:
				return (Operation)container;
			case EmftvmPackage.CODE_BLOCK:
				return ((CodeBlock)container).getOperation();
			default:
				break;
			}
		}
		return null;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Field getField() {
		final EObject container = eContainer();
		if (container != null) {
			switch (container.eClass().getClassifierID()) {
			case EmftvmPackage.FIELD:
				return (Field)container;
			case EmftvmPackage.CODE_BLOCK:
				return ((CodeBlock)container).getField();
			default:
				break;
			}
		}
		return null;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Instruction> getPredecessors(final Instruction i) {
		if (!predecessors.containsKey(i)) {
			final EList<Instruction> preds = new BasicEList<Instruction>();
			final EList<Instruction> code = getCode();
			final int index = code.indexOf(i);
			assert index > -1;
			if (index > 0) {
				Instruction prev = code.get(index - 1);
				if (!(prev instanceof Goto)) {
					preds.add(prev);
				}
				for (Instruction i2 : code) {
					if (i2 instanceof BranchInstruction && ((BranchInstruction)i2).getTarget() == prev) {
						preds.add(i2);
					}
				}
			}
			predecessors.put(i, ECollections.unmodifiableEList(preds));
		}
		return predecessors.get(i);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Instruction> getAllPredecessors(final Instruction i) {
		if (!allPredecessors.containsKey(i)) {
			final EList<Instruction> predecessors = new BasicEList<Instruction>();
			allPredecessors(i, predecessors);
			allPredecessors.put(i, ECollections.unmodifiableEList(predecessors));
		}
		return allPredecessors.get(i);
	}

	/**
	 * Collects the transitive closure of predecessor instructions for <code>i</code>.
	 * @param i the instruction to collect the predecessors for.
	 * @param currentPreds the predecessor instructions.
	 * @return the predecessor instructions.
	 */
	private EList<Instruction> allPredecessors(final Instruction i, final EList<Instruction> currentPreds) {
		final EList<Instruction> preds = getPredecessors(i);
		for (Instruction pred : preds) {
			if (!currentPreds.contains(pred)) {
				currentPreds.add(pred);
				allPredecessors(pred, currentPreds);
			}
		}
		return currentPreds;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Instruction> getNonLoopingPredecessors(Instruction i) {
		if (!nlPredecessors.containsKey(i)) {
			final EList<Instruction> code = getCode();
			final int index = code.indexOf(i);
			final EList<Instruction> preds = new BasicEList<Instruction>();
			for (Instruction p : getPredecessors(i)) {
				if (code.indexOf(p) < index || !getAllPredecessors(p).contains(i)) {
					preds.add(p);
				}
			}
			nlPredecessors.put(i, ECollections.unmodifiableEList(preds));
		}
		return nlPredecessors.get(i);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case EmftvmPackage.CODE_BLOCK__CODE:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getCode()).basicAdd(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__LINE_NUMBERS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getLineNumbers()).basicAdd(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getLocalVariables()).basicAdd(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__MATCHER_FOR:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetMatcherFor((Rule)otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__APPLIER_FOR:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetApplierFor((Rule)otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetPostApplyFor((Rule)otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__BODY_FOR:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetBodyFor((Operation)otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__INITIALISER_FOR:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetInitialiserFor((Field)otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__NESTED:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getNested()).basicAdd(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__NESTED_FOR:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetNestedFor((CodeBlock)otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__BINDING_FOR:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetBindingFor((InputRuleElement)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case EmftvmPackage.CODE_BLOCK__CODE:
				return ((InternalEList<?>)getCode()).basicRemove(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__LINE_NUMBERS:
				return ((InternalEList<?>)getLineNumbers()).basicRemove(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES:
				return ((InternalEList<?>)getLocalVariables()).basicRemove(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__MATCHER_FOR:
				return basicSetMatcherFor(null, msgs);
			case EmftvmPackage.CODE_BLOCK__APPLIER_FOR:
				return basicSetApplierFor(null, msgs);
			case EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR:
				return basicSetPostApplyFor(null, msgs);
			case EmftvmPackage.CODE_BLOCK__BODY_FOR:
				return basicSetBodyFor(null, msgs);
			case EmftvmPackage.CODE_BLOCK__INITIALISER_FOR:
				return basicSetInitialiserFor(null, msgs);
			case EmftvmPackage.CODE_BLOCK__NESTED:
				return ((InternalEList<?>)getNested()).basicRemove(otherEnd, msgs);
			case EmftvmPackage.CODE_BLOCK__NESTED_FOR:
				return basicSetNestedFor(null, msgs);
			case EmftvmPackage.CODE_BLOCK__BINDING_FOR:
				return basicSetBindingFor(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case EmftvmPackage.CODE_BLOCK__MATCHER_FOR:
				return eInternalContainer().eInverseRemove(this, EmftvmPackage.RULE__MATCHER, Rule.class, msgs);
			case EmftvmPackage.CODE_BLOCK__APPLIER_FOR:
				return eInternalContainer().eInverseRemove(this, EmftvmPackage.RULE__APPLIER, Rule.class, msgs);
			case EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR:
				return eInternalContainer().eInverseRemove(this, EmftvmPackage.RULE__POST_APPLY, Rule.class, msgs);
			case EmftvmPackage.CODE_BLOCK__BODY_FOR:
				return eInternalContainer().eInverseRemove(this, EmftvmPackage.OPERATION__BODY, Operation.class, msgs);
			case EmftvmPackage.CODE_BLOCK__INITIALISER_FOR:
				return eInternalContainer().eInverseRemove(this, EmftvmPackage.FIELD__INITIALISER, Field.class, msgs);
			case EmftvmPackage.CODE_BLOCK__NESTED_FOR:
				return eInternalContainer().eInverseRemove(this, EmftvmPackage.CODE_BLOCK__NESTED, CodeBlock.class, msgs);
			case EmftvmPackage.CODE_BLOCK__BINDING_FOR:
				return eInternalContainer().eInverseRemove(this, EmftvmPackage.INPUT_RULE_ELEMENT__BINDING, InputRuleElement.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EmftvmPackage.CODE_BLOCK__MAX_LOCALS:
				return getMaxLocals();
			case EmftvmPackage.CODE_BLOCK__MAX_STACK:
				return getMaxStack();
			case EmftvmPackage.CODE_BLOCK__CODE:
				return getCode();
			case EmftvmPackage.CODE_BLOCK__LINE_NUMBERS:
				return getLineNumbers();
			case EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES:
				return getLocalVariables();
			case EmftvmPackage.CODE_BLOCK__MATCHER_FOR:
				return getMatcherFor();
			case EmftvmPackage.CODE_BLOCK__APPLIER_FOR:
				return getApplierFor();
			case EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR:
				return getPostApplyFor();
			case EmftvmPackage.CODE_BLOCK__BODY_FOR:
				return getBodyFor();
			case EmftvmPackage.CODE_BLOCK__INITIALISER_FOR:
				return getInitialiserFor();
			case EmftvmPackage.CODE_BLOCK__NESTED:
				return getNested();
			case EmftvmPackage.CODE_BLOCK__NESTED_FOR:
				return getNestedFor();
			case EmftvmPackage.CODE_BLOCK__PARENT_FRAME:
				return getParentFrame();
			case EmftvmPackage.CODE_BLOCK__BINDING_FOR:
				return getBindingFor();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EmftvmPackage.CODE_BLOCK__MAX_LOCALS:
				setMaxLocals((Integer)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__MAX_STACK:
				setMaxStack((Integer)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__CODE:
				getCode().clear();
				getCode().addAll((Collection<? extends Instruction>)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__LINE_NUMBERS:
				getLineNumbers().clear();
				getLineNumbers().addAll((Collection<? extends LineNumber>)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES:
				getLocalVariables().clear();
				getLocalVariables().addAll((Collection<? extends LocalVariable>)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__MATCHER_FOR:
				setMatcherFor((Rule)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__APPLIER_FOR:
				setApplierFor((Rule)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR:
				setPostApplyFor((Rule)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__BODY_FOR:
				setBodyFor((Operation)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__INITIALISER_FOR:
				setInitialiserFor((Field)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__NESTED:
				getNested().clear();
				getNested().addAll((Collection<? extends CodeBlock>)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__NESTED_FOR:
				setNestedFor((CodeBlock)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__PARENT_FRAME:
				setParentFrame((StackFrame)newValue);
				return;
			case EmftvmPackage.CODE_BLOCK__BINDING_FOR:
				setBindingFor((InputRuleElement)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EmftvmPackage.CODE_BLOCK__MAX_LOCALS:
				setMaxLocals(MAX_LOCALS_EDEFAULT);
				return;
			case EmftvmPackage.CODE_BLOCK__MAX_STACK:
				setMaxStack(MAX_STACK_EDEFAULT);
				return;
			case EmftvmPackage.CODE_BLOCK__CODE:
				getCode().clear();
				return;
			case EmftvmPackage.CODE_BLOCK__LINE_NUMBERS:
				getLineNumbers().clear();
				return;
			case EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES:
				getLocalVariables().clear();
				return;
			case EmftvmPackage.CODE_BLOCK__MATCHER_FOR:
				setMatcherFor((Rule)null);
				return;
			case EmftvmPackage.CODE_BLOCK__APPLIER_FOR:
				setApplierFor((Rule)null);
				return;
			case EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR:
				setPostApplyFor((Rule)null);
				return;
			case EmftvmPackage.CODE_BLOCK__BODY_FOR:
				setBodyFor((Operation)null);
				return;
			case EmftvmPackage.CODE_BLOCK__INITIALISER_FOR:
				setInitialiserFor((Field)null);
				return;
			case EmftvmPackage.CODE_BLOCK__NESTED:
				getNested().clear();
				return;
			case EmftvmPackage.CODE_BLOCK__NESTED_FOR:
				setNestedFor((CodeBlock)null);
				return;
			case EmftvmPackage.CODE_BLOCK__PARENT_FRAME:
				setParentFrame(PARENT_FRAME_EDEFAULT);
				return;
			case EmftvmPackage.CODE_BLOCK__BINDING_FOR:
				setBindingFor((InputRuleElement)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EmftvmPackage.CODE_BLOCK__MAX_LOCALS:
				return getMaxLocals() != MAX_LOCALS_EDEFAULT;
			case EmftvmPackage.CODE_BLOCK__MAX_STACK:
				return getMaxStack() != MAX_STACK_EDEFAULT;
			case EmftvmPackage.CODE_BLOCK__CODE:
				return code != null && !code.isEmpty();
			case EmftvmPackage.CODE_BLOCK__LINE_NUMBERS:
				return lineNumbers != null && !lineNumbers.isEmpty();
			case EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES:
				return localVariables != null && !localVariables.isEmpty();
			case EmftvmPackage.CODE_BLOCK__MATCHER_FOR:
				return getMatcherFor() != null;
			case EmftvmPackage.CODE_BLOCK__APPLIER_FOR:
				return getApplierFor() != null;
			case EmftvmPackage.CODE_BLOCK__POST_APPLY_FOR:
				return getPostApplyFor() != null;
			case EmftvmPackage.CODE_BLOCK__BODY_FOR:
				return getBodyFor() != null;
			case EmftvmPackage.CODE_BLOCK__INITIALISER_FOR:
				return getInitialiserFor() != null;
			case EmftvmPackage.CODE_BLOCK__NESTED:
				return nested != null && !nested.isEmpty();
			case EmftvmPackage.CODE_BLOCK__NESTED_FOR:
				return getNestedFor() != null;
			case EmftvmPackage.CODE_BLOCK__PARENT_FRAME:
				return PARENT_FRAME_EDEFAULT == null ? parentFrame != null : !PARENT_FRAME_EDEFAULT.equals(parentFrame);
			case EmftvmPackage.CODE_BLOCK__BINDING_FOR:
				return getBindingFor() != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void eNotify(Notification notification) {
		super.eNotify(notification);
		switch (notification.getFeatureID(null)) {
		case EmftvmPackage.CODE_BLOCK__CODE:
			codeChanged();
			for (Instruction i : getCode()) {
				i.eNotify(notification);
			}
			break;
		case EmftvmPackage.CODE_BLOCK__LOCAL_VARIABLES:
			localVariablesChanged();
			break;
		case EmftvmPackage.CODE_BLOCK__NESTED:
			nestedChanged();
			break;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean eNotificationRequired() {
		return true;
	}

	/**
	 * <!-- begin-user-doc. -->
	 * {@inheritDoc}
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();
	
		StringBuffer result = new StringBuffer();
		final EObject container = eContainer();
		if (container != null) {
			result.append(container);
			if (container instanceof CodeBlock) {
				result.append('@');
				result.append(((CodeBlock)container).getNested().indexOf(this));
			} else if (container instanceof Field) {
				// nothing
			} else if (container instanceof Operation) {
				// nothing
			} else if (container instanceof InputRuleElement) {
				result.append('@');
				result.append(((InputRuleElement)container).getInputFor());
			} else if (container instanceof Rule) {
				final Rule r = (Rule)container;
				if (r.getMatcher() == this) {
					result.append("@matcher");
				} else if (r.getApplier() == this) {
					result.append("@applier");
				} else if (r.getPostApply() == this) {
					result.append("@postApply");
				} else {
					result.append("@unknown");
				}
			} else {
				result.append("@unknown");
			}
		} else {
			result.append("@uncontained");
		}
		return result.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	public JITCodeBlock getJITCodeBlock() {
		return jitCodeBlock;
	}

	/**
	 * {@inheritDoc}
	 */
	public void setJITCodeBlock(final JITCodeBlock jcb) {
		this.jitCodeBlock = jcb;
	}

	/**
	 * Returns the {@link Module} (for debugger).
	 * @return the {@link Module}
	 * @see CodeBlockImpl#getModule()
	 */
	public Module getASM() {
		return getModule();
	}

	/**
	 * {@inheritDoc}
	 */
	public Rule getRule() {
		if (!ruleSet) {
			CodeBlock cb = this;
			while (cb != null) {
				if (cb.eContainer() instanceof Rule) {
					rule = (Rule)cb.eContainer();
					break;
				} else {
					cb = cb.getNestedFor();
				}
			}
			ruleSet = true;
		}
		return rule;
	}

	/**
	 * @param env
	 * @param type
	 * @param name
	 * @return The {@link Field} with the given <code>type</code> and <code>name</code>, if any, otherwise <code>null</code>
	 */
	private Field findField(final ExecEnv env, Object type, String name) {
		final Rule rule = getRule();
		final Field field;
		if (rule != null) {
			field = rule.findField(type, name);
		} else {
			field = null;
		}
		if (field == null) {
			return env.findField(type, name);
		} else {
			return field;
		}
	}

	/**
	 * @param env
	 * @param type
	 * @param name
	 * @return The static {@link Field} with the given <code>type</code> and <code>name</code>, if any, otherwise <code>null</code>
	 */
	private Field findStaticField(final ExecEnv env, Object type, String name) {
		final Rule rule = getRule();
		final Field field;
		if (rule != null) {
			field = rule.findStaticField(type, name);
		} else {
			field = null;
		}
		if (field == null) {
			return env.findStaticField(type, name);
		} else {
			return field;
		}
	}

	/**
	 * Implements the SET instruction.
	 * @param v value
	 * @param o object
	 * @param propname the property name
	 * @param frame the current stack frame
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private void set(final Object v, final Object o, final String propname, final StackFrame frame) 
	throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		final ExecEnv env = frame.getEnv();
		if (o instanceof EObject) {
			final EObject eo = (EObject)o;
			final EClass type = eo.eClass();
			final boolean queueSet = env.getCurrentPhase() == RuleMode.AUTOMATIC_SINGLE && env.getInoutModelOf(eo) != null;
			final Field field = findField(env, type, propname);
			if (field != null) {
				if (field.getRule() == null) {
					if (queueSet) {
						env.queueForSet(field, o, v, frame);
					} else {
						if (env.getInputModelOf(eo) != null) {
							throw new IllegalArgumentException(
									String.format("Cannot set properties of %s, as it is contained in an input model",
											EMFTVMUtil.toPrettyString(eo, env)));
						}
						if (env.getOutputModelOf(eo) != null) {
							throw new IllegalArgumentException(String.format(
									"Setting transient field %s of %s, which cannot be read back as it is contained in an output model",
									propname, EMFTVMUtil.toPrettyString(eo, env)));
						}
						field.setValue(o, v);
					}
				} else {
					// Treat rule fields as local variables
					field.setValue(o, v);
				}
				return;
			}
			final EStructuralFeature sf = type.getEStructuralFeature(propname);
			if (sf != null) {
				if (queueSet) {
					env.queueForSet(sf, eo, v, frame);
				} else {
					EMFTVMUtil.set(env, eo, sf, v);
				}
				return;
			}
			final Resource resource = eo.eResource();
			if (EMFTVMUtil.XMI_ID_FEATURE.equals(propname) && resource instanceof XMIResource) { //$NON-NLS-1$
				if (queueSet) {
					env.queueXmiIDForSet(eo, v, frame);
				} else {
					((XMIResource)resource).setID(eo, v.toString());
				}
				return;
			}
			throw new NoSuchFieldException(String.format("Field %s::%s not found", 
					EMFTVMUtil.toPrettyString(type, env), propname));
		}

		// o is a regular Java object
		final Class<?> type = o == null ? Void.TYPE : o.getClass();
		final Field field = findField(env, type, propname);
		if (field != null) {
			field.setValue(o, v);
			return;
		}
		try {
			final java.lang.reflect.Field f = type.getField(propname);
			f.set(o, v);
		} catch (NoSuchFieldException e) {
			throw new NoSuchFieldException(String.format("Field %s::%s not found", 
					EMFTVMUtil.toPrettyString(type, env), propname));
		}
	}

	/**
	 * Adds <code>v</code> to <code>o.propname</code>. Implements the ADD and INSERT instructions.
	 * 
	 * @param index
	 *            the insertion index (-1 for end)
	 * @param v
	 *            value
	 * @param o
	 *            object
	 * @param propname
	 *            the property name
	 * @param frame
	 *            the current stack frame
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	private void add(final int index, final Object v, final Object o, final String propname, final StackFrame frame)
	throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		final ExecEnv env = frame.getEnv();
		if (o instanceof EObject) {
			final EObject eo = (EObject)o;
			final EClass type = eo.eClass();
			final boolean queueSet = env.getCurrentPhase() == RuleMode.AUTOMATIC_SINGLE && env.getInoutModelOf(eo) != null;
			final Field field = findField(env, type, propname);
			if (field != null) {
				if (field.getRule() == null) {
					if (queueSet) {
						env.queueForAdd(field, o, v, index, frame);
					} else {
						if (env.getInputModelOf(eo) != null) {
							throw new IllegalArgumentException(String.format(
									"Cannot add to properties of %s, as it is contained in an input model",
									EMFTVMUtil.toPrettyString(eo, env)));
						}
						if (env.getOutputModelOf(eo) != null) {
							throw new IllegalArgumentException(String.format(
									"Adding to transient field %s of %s, which cannot be read back as %1s is contained in an output model",
									propname, EMFTVMUtil.toPrettyString(eo, env)));
						}
						field.addValue(o, v, index, frame);
					}
				} else {
					// Treat rule fields as local variables
					field.addValue(o, v, index, frame);
				}
				return;
			}
			final EStructuralFeature sf = type.getEStructuralFeature(propname);
			if (sf != null) {
				if (queueSet) {
					env.queueForAdd(sf, eo, v, index, frame);
				} else {
					EMFTVMUtil.add(env, eo, sf, v, index);
				}
				return;
			}
			final Resource resource = eo.eResource();
			if (EMFTVMUtil.XMI_ID_FEATURE.equals(propname) && resource instanceof XMIResource) { //$NON-NLS-1$
				if (queueSet) {
					env.queueXmiIDForAdd(eo, v, index, frame);
				} else {
					if (((XMIResource) resource).getID(eo) != null) {
						throw new IllegalArgumentException(String.format(
								"Cannot add %s to field %s::%s: maximum multiplicity of 1 reached", v, EMFTVMUtil.toPrettyString(eo, env),
								propname));
					}
					if (index > 0) {
						throw new IndexOutOfBoundsException(String.valueOf(index));
					}
					((XMIResource) resource).setID(eo, v.toString());
				}
				return;
			}
			throw new NoSuchFieldException(String.format("Field %s::%s not found", 
					EMFTVMUtil.toPrettyString(type, env), propname));
		}

		// o is a regular Java object
		final Class<?> type = o == null ? Void.TYPE : o.getClass();
		final Field field = findField(env, type, propname);
		if (field != null) {
			field.addValue(o, v, index, frame);
			return;
		}
		throw new NoSuchFieldException(String.format("Field %s::%s not found", EMFTVMUtil.toPrettyString(type, env), propname));
	}

	/**
	 * Implements the REMOVE instruction.
	 * 
	 * @param v
	 *            value
	 * @param o
	 *            object
	 * @param propname
	 *            the property name
	 * @param frame
	 *            the current stack frame
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	private void remove(final Object v, final Object o, final String propname, final StackFrame frame)
	throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		final ExecEnv env = frame.getEnv();
		if (o instanceof EObject) {
			final EObject eo = (EObject)o;
			final EClass type = eo.eClass();
			final boolean queueSet = env.getCurrentPhase() == RuleMode.AUTOMATIC_SINGLE && env.getInoutModelOf(eo) != null;
			final Field field = findField(env, type, propname);
			if (field != null) {
				if (field.getRule() == null) {
					if (queueSet) {
						env.queueForRemove(field, o, v, frame);
					} else {
						if (env.getInputModelOf(eo) != null) {
							throw new IllegalArgumentException(String.format(
									"Cannot remove from properties of %s, as it is contained in an input model",
									EMFTVMUtil.toPrettyString(eo, env)));
						}
						if (env.getOutputModelOf(eo) != null) {
							throw new IllegalArgumentException(
									String.format(
											"Removing from transient field %s of %s, which cannot be read back as %1s is contained in an output model",
											propname, EMFTVMUtil.toPrettyString(eo, env)));
						}
						field.removeValue(o, v, frame);
					}
				} else {
					// Treat rule fields as local variables
					field.removeValue(o, v, frame);
				}
				return;
			}
			final EStructuralFeature sf = type.getEStructuralFeature(propname);
			if (sf != null) {
				if (queueSet) {
					env.queueForRemove(sf, eo, v, frame);
				} else {
					EMFTVMUtil.remove(env, eo, sf, v);
				}
				return;
			}
			final Resource resource = eo.eResource();
			if (EMFTVMUtil.XMI_ID_FEATURE.equals(propname) && resource instanceof XMIResource) { //$NON-NLS-1$
				if (queueSet) {
					env.queueXmiIDForRemove(eo, v, frame);
				} else {
					final XMIResource xmiRes = (XMIResource) resource;
					final Object xmiID = xmiRes.getID(eo);
					if (xmiID == null ? v == null : xmiID.equals(v)) {
						xmiRes.setID(eo, null);
					}
				}
				return;
			}
			throw new NoSuchFieldException(String.format("Field %s::%s not found", 
					EMFTVMUtil.toPrettyString(type, env), propname));
		}

		// o is a regular Java object
		final Class<?> type = o == null ? Void.TYPE : o.getClass();
		final Field field = findField(env, type, propname);
		if (field != null) {
			field.removeValue(o, v, frame);
			return;
		}
		throw new NoSuchFieldException(String.format("Field %s::%s not found", EMFTVMUtil.toPrettyString(type, env), propname));
	}

	/**
	 * Implements the GET instruction.
	 * 
	 * @param propname
	 * @param env
	 * @param frame
	 * @param o
	 *            the object on which to GET the property
	 * @return the property value
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	@SuppressWarnings("unchecked")
	private Object get(final String propname, final StackFrame frame, final Object o) throws NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		final ExecEnv env = frame.getEnv();

		if (o instanceof EObject) {
			final EObject eo = (EObject)o;
			final EClass type = eo.eClass();
			final Field field = findField(env, type, propname);
			if (field != null) {
				if (field.getRule() == null && env.getOutputModelOf(eo) != null) {
					throw new IllegalArgumentException(String.format("Cannot read properties of %s, as it is contained in an output model",
							EMFTVMUtil.toPrettyString(eo, env)));
				}
				return field.getValue(o, frame);
			}
			final EStructuralFeature sf = type.getEStructuralFeature(propname);
			if (sf != null) {
				return EMFTVMUtil.get(env, eo, sf);
			}
			final Resource resource = eo.eResource();
			if (EMFTVMUtil.XMI_ID_FEATURE.equals(propname) && resource instanceof XMIResource) { //$NON-NLS-1$
				return ((XMIResource)resource).getID(eo);
			}
			throw new NoSuchFieldException(String.format("Field %s::%s not found", 
					EMFTVMUtil.toPrettyString(type, env), propname));
		}

		// o is a regular Java object
		final Class<?> type = o == null ? Void.TYPE : o.getClass();
		final Field field = findField(env, type, propname);
		if (field != null) {
			return field.getValue(o, frame);
		}
		try {
			final java.lang.reflect.Field f = type.getField(propname);
			final Object result = f.get(o);
			if (result instanceof List<?>) {
				return new LazyListOnList<Object>((List<Object>)result);
			} else if (result instanceof java.util.Set<?>) {
				return new LazySetOnSet<Object>((java.util.Set<Object>)result);
			} else if (result instanceof Collection<?>) {
				return new LazyBagOnCollection<Object>((Collection<Object>)result);
			} else {
				return result;
			}
		} catch (NoSuchFieldException e) {
			throw new NoSuchFieldException(String.format("Field %s::%s not found", 
					EMFTVMUtil.toPrettyString(type, env), propname));
		}
	}

	/**
	 * Implements the GET_TRANS instruction.
	 * @param propname
	 * @param env
	 * @param frame
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private Collection<Object> getTrans(final String propname, final StackFrame frame, final Object o) throws NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		final ExecEnv env = frame.getEnv();

		if (o instanceof EObject) {
			final EObject eo = (EObject)o;
			final EClass type = eo.eClass();
			final Field field = findField(env, type, propname);
			if (field != null) {
				if (field.getRule() == null && env.getOutputModelOf(eo) != null) {
					throw new IllegalArgumentException(String.format("Cannot read properties of %s, as it is contained in an output model",
							EMFTVMUtil.toPrettyString(eo, env)));
				}
				return EMFTVMUtil.getTrans(o, field, frame, new LazyList<Object>());
			} else {
				final EStructuralFeature sf = type.getEStructuralFeature(propname);
				if (sf == null) {
					throw new NoSuchFieldException(String.format("Field %s::%s not found", 
							EMFTVMUtil.toPrettyString(type, env), propname));
				}
				return EMFTVMUtil.getTrans(eo, sf, env, new LazyList<Object>());
			}
		} else {
			final Class<?> type = o.getClass();
			final Field field = findField(env, type, propname);
			if (field != null) {
				return EMFTVMUtil.getTrans(o, field, frame, new LazyList<Object>());
			} else {
				final java.lang.reflect.Field f = type.getField(propname);
				return EMFTVMUtil.getTrans(o, f, new LazyList<Object>());
			}
		}
	}

	/**
	 * Implements the GET_SUPER instruction.
	 * @param fieldCtx the current {@link Field} context
	 * @param propname
	 * @param env
	 * @param frame
	 * @return the property value
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@SuppressWarnings("unchecked")
	private Object getSuper(final Field fieldCtx, final String propname, final StackFrame frame, final Object o)
			throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		if (fieldCtx == null) {
			throw new IllegalArgumentException("GET_SUPER can only be used in fields");
		}
		final EClassifier context = fieldCtx.getEContext();
		if (context == null) {
			throw new IllegalArgumentException(String.format("Field misses context type: %s", fieldCtx));
		}

		final ExecEnv env = frame.getEnv();

		final List<?> superTypes;
		if (context instanceof EClass) {
			superTypes = ((EClass)context).getESuperTypes();
		} else {
			final Class<?> ic = context.getInstanceClass();
			if (ic == null) {
				throw new IllegalArgumentException(String.format("Primitive EMF type without instance class %s", context));
			}
			superTypes = Collections.singletonList(ic.getSuperclass());
		}

		final java.util.Set<Object> superFs = new LinkedHashSet<Object>();
		if (o instanceof EObject) {
			// o may have EStructuralFeatures
			for (Object superType : superTypes) {
				Object superF = env.findField(superType, propname);
				if (superF != null) {
					superFs.add(superF);
				} else if (superType instanceof EClass) {
					superF = ((EClass)superType).getEStructuralFeature(propname);
					if (superF != null) {
						superFs.add(superF);
					} else if (((EClass)superType).getInstanceClass() != null) {
						try {
							superF = ((EClass)superType).getInstanceClass().getField(propname);
							assert superF != null;
							superFs.add(superF);
						} catch (NoSuchFieldException e) {
							// not found - skip
						}
					}
				} else if (superType instanceof Class<?>) {
					try {
						superF = ((Class<?>)superType).getField(propname);
						assert superF != null;
						superFs.add(superF);
					} catch (NoSuchFieldException e) {
						// not found - skip
					}
				}
			}
		} else {
			// o is a regular Java object - may be null
			for (Object superType : superTypes) {
				Object superF = env.findField(superType, propname);
				if (superF != null) {
					superFs.add(superF);
				} else if (superType instanceof EClass && ((EClass)superType).getInstanceClass() != null) {
					try {
						superF = ((EClass)superType).getInstanceClass().getField(propname);
						assert superF != null;
						superFs.add(superF);
					} catch (NoSuchFieldException e) {
						// not found - skip
					}
				} else if (superType instanceof Class<?>) {
					try {
						superF = ((Class<?>)superType).getField(propname);
						assert superF != null;
						superFs.add(superF);
					} catch (NoSuchFieldException e) {
						// not found - skip
					}
				}
			}
		}

		if (superFs.size() > 1) {
			throw new DuplicateEntryException(String.format(
					"More than one super-field found for context %s: %s",
					context, superFs));
		}
		if (!superFs.isEmpty()) {
			final Object superF = superFs.iterator().next();
			if (superF instanceof Field) {
				final Field field = (Field) superF;
				if (o instanceof EObject) {
					final EObject eo = (EObject) o;
					if (field.getRule() == null && env.getOutputModelOf(eo) != null) {
						throw new IllegalArgumentException(String.format(
								"Cannot read properties of %s, as it is contained in an output model", EMFTVMUtil.toPrettyString(eo, env)));
					}
				}
				return field.getValue(o, frame);
			} else if (superF instanceof EStructuralFeature) {
				return EMFTVMUtil.get(env, (EObject)o, (EStructuralFeature)superF);
			} else {
				final Object result = ((java.lang.reflect.Field)superF).get(o);
				if (result instanceof List<?>) {
					return new LazyListOnList<Object>((List<Object>)result);
				} else if (result instanceof java.util.Set<?>) {
					return new LazySetOnSet<Object>((java.util.Set<Object>)result);
				} else if (result instanceof Collection<?>) {
					return new LazyBagOnCollection<Object>((Collection<Object>)result);
				} else {
					return result;
				}
			}
		}

		throw new NoSuchFieldException(String.format("Super-field of %s::%s not found", 
				EMFTVMUtil.toPrettyString(context, env), propname));
	}

	/**
	 * Implements the SET_STATIC instruction.
	 * @param v value
	 * @param o object
	 * @param propname the property name
	 * @param env the execution environment
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private void setStatic(final Object v, final Object o, final String propname, final ExecEnv env)
	throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		final Object ort = EMFTVMUtil.getRegistryType(o);

		if (ort instanceof EClass) {
			final EClass type = (EClass)ort;
			final Field field = findStaticField(env, type, propname);
			if (field != null) {
				field.setStaticValue(v);
			} else {
				throw new NoSuchFieldException(String.format("Field %s::%s not found", 
						EMFTVMUtil.toPrettyString(type, env), propname));
			}
		} else if (ort instanceof Class<?>) {
			final Class<?> type = (Class<?>)ort;
			final Field field = findStaticField(env, type, propname);
			if (field != null) {
				field.setValue(ort, v);	
			} else {
				final java.lang.reflect.Field f = type.getField(propname);
				if (Modifier.isStatic(f.getModifiers())) {
					f.set(null, v);
				} else {
					throw new NoSuchFieldException(String.format("Field %s::%s not found", 
							EMFTVMUtil.toPrettyString(type, env), propname));
				}
			}
		} else {
			throw new IllegalArgumentException(String.format("%s is not a type", 
					EMFTVMUtil.toPrettyString(ort, env)));
		}
	}

	/**
	 * Implements the GET_STATIC instruction.
	 * @param propname
	 * @param frame
	 * @return the property value
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private Object getStatic(final String propname, final StackFrame frame, final Object o) throws NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		final ExecEnv env = frame.getEnv();
		final Object oType = EMFTVMUtil.getRegistryType(o);

		if (oType instanceof EClass) {
			final EClass type = (EClass)oType;
			final Field field = findStaticField(env, type, propname);
			if (field != null) {
				return field.getStaticValue(frame);
			} else {
				throw new NoSuchFieldException(String.format("Field %s::%s not found", 
						EMFTVMUtil.toPrettyString(type, env), propname));
			}
		} else if (oType instanceof Class<?>) {
			final Class<?> type = (Class<?>)oType;
			final Field field = findStaticField(env, type, propname);
			if (field != null) {
				return field.getStaticValue(frame);
			} else {
				final java.lang.reflect.Field f = type.getField(propname);
				if (Modifier.isStatic(f.getModifiers())) {
					return f.get(null);
				} else {
					throw new NoSuchFieldException(String.format("Field %s::%s not found", 
							EMFTVMUtil.toPrettyString(type, env), propname));
				}
			}
		} else {
			throw new IllegalArgumentException(String.format("%s is not a type", oType));
		}
	}

	/**
	 * Implements the NEW and NEW_S instructions.
	 * @param modelname
	 * @param type
	 * @param fram
	 * @return the new object
	 */
	private static Object newInstr(final String modelname, final Object type, final StackFrame frame) {
		final ExecEnv env = frame.getEnv();
		if (type instanceof EClass) {
			final EClass eType = (EClass)type;
			Model model = env.getOutputModels().get(modelname);
			if (model == null) {
				model = env.getInoutModels().get(modelname);
			}
			if (model == null) {
				throw new IllegalArgumentException(String.format("Inout/output model %s not found", modelname));
			}
			return model.newElement(eType);
		} else {
			try {
				return NativeTypes.newInstance((Class<?>)type);
			} catch (Exception e) {
				throw new IllegalArgumentException(e);
			}
		}
	}

	/**
	 * Implements the DELETE instruction.
	 * @param frame
	 */
	private static void delete(final StackFrame frame, final EObject element) {
		final ExecEnv env = frame.getEnv();
		final Resource res = element.eResource();
		if (res == null) {
			throw new IllegalArgumentException(String.format(
					"Element %s is cannot be deleted; not contained in a model", 
					EMFTVMUtil.toPrettyString(element, env)));
		}
		final Model model = env.getInputModelOf(element);
		if (model != null) {
			throw new IllegalArgumentException(String.format(
					"Element %s is cannot be deleted; contained in input model %s", 
					EMFTVMUtil.toPrettyString(element, env), env.getModelID(model)));
		}
		env.queueForDelete(element, frame);
	}

	/**
	 * Implements the INVOKE instruction.
	 * @param instr the INVOKE instruction
	 * @param frame the current stack frame
	 * @return the invocation result
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private static Object invoke(final Invoke instr, final StackFrame frame, final Stack stack)
			throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		final String opname = instr.getOpname();
		final int argcount = instr.getArgcount(); 
		final Object o;
		final Operation op;
		final Method method;
		switch (argcount) {
		case 0:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., self, arg1, arg2]
			o = stack.pop();
			op = frame.getEnv().findOperation(
					EMFTVMUtil.getArgumentType(o),
					opname);
			method = EMFTVMUtil.findNativeMethod(op, o, opname);
			if (method != null) {
				// Only record new method if it is more general than the existing method
				final Method oldMethod = instr.getNativeMethod();
				if (oldMethod == null || method.getDeclaringClass().isAssignableFrom(oldMethod.getDeclaringClass())) {
					instr.setNativeMethod(method); // record invoked method for JIT compiler
				}
				return EMFTVMUtil.invokeNative(frame, o, method);
			}
			if (op != null) {
				final CodeBlock body = op.getBody();
				return body.execute(frame.getSubFrame(body, o));
			}
			throw new UnsupportedOperationException(String.format("%s::%s()", 
					EMFTVMUtil.getTypeName(frame.getEnv(), EMFTVMUtil.getArgumentType(o)), 
					opname));
		case 1:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., self, arg1, arg2]
			final Object arg = stack.pop();
			o = stack.pop();
			op = frame.getEnv().findOperation(
					EMFTVMUtil.getArgumentType(o),
					opname, 
					EMFTVMUtil.getArgumentType(arg));
			method = EMFTVMUtil.findNativeMethod(op, o, opname, arg);
			if (method != null) {
				// Only record new method if it is more general than the existing method
				final Method oldMethod = instr.getNativeMethod();
				if (oldMethod == null || method.getDeclaringClass().isAssignableFrom(oldMethod.getDeclaringClass())) {
					instr.setNativeMethod(method); // record invoked method for JIT compiler
				}
				return EMFTVMUtil.invokeNative(frame, o, method, arg);
			}
			if (op != null) {
				final CodeBlock body = op.getBody();
				return body.execute(frame.getSubFrame(body, o, arg));
			}
			throw new UnsupportedOperationException(String.format("%s::%s(%s)", 
					EMFTVMUtil.getTypeName(frame.getEnv(), EMFTVMUtil.getArgumentType(o)), 
					opname, 
					EMFTVMUtil.getTypeName(frame.getEnv(), EMFTVMUtil.getArgumentType(arg))));
		default:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., self, arg1, arg2]
			final Object[] args = stack.pop(argcount);
			//TODO treat context as a regular argument (cf. Java's Method.invoke())
			o = stack.pop();
			op = frame.getEnv().findOperation(
					EMFTVMUtil.getArgumentType(o),
					opname, 
					EMFTVMUtil.getArgumentTypes(args));
			method = EMFTVMUtil.findNativeMethod(op, o, opname, args);
			if (method != null) {
				// Only record new method if it is more general than the existing method
				final Method oldMethod = instr.getNativeMethod();
				if (oldMethod == null || method.getDeclaringClass().isAssignableFrom(oldMethod.getDeclaringClass())) {
					instr.setNativeMethod(method); // record invoked method for JIT compiler
				}
				return EMFTVMUtil.invokeNative(frame, o, method, args);
			}
			if (op != null) {
				final CodeBlock body = op.getBody();
				return body.execute(frame.getSubFrame(body, o, args));
			}
			throw new UnsupportedOperationException(String.format("%s::%s(%s)", 
					EMFTVMUtil.getTypeName(frame.getEnv(), EMFTVMUtil.getArgumentType(o)), 
					opname, 
					EMFTVMUtil.getTypeNames(frame.getEnv(), EMFTVMUtil.getArgumentTypes(args))));
		}
	}

	/**
	 * Implements the INVOKE_STATIC instruction.
	 * @param opname
	 * @param argcount
	 * @param frame
	 * @return the invocation result
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private static Object invokeStatic(final String opname, final int argcount, final StackFrame frame, final Stack stack)
			throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		final ExecEnv env = frame.getEnv();
		final Object type;
		final Operation op;
		switch (argcount) {
		case 0:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., type, arg1, arg2]
			type = stack.pop();

			if (type == null) {
				throw new IllegalArgumentException(String.format("Cannot invoke static operation %s on null type", opname));
			}

			if (type == env.eClass()) { // Lazy and called rule invocations are indistinguishable from static operations in ATL
				final Rule rule = env.findRule(opname);
				if (rule != null && rule.getMode() == RuleMode.MANUAL) {
					return matchOne(frame, rule);
				}
			}

			op = env.findStaticOperation(type, opname);
			if (op != null) {
				final CodeBlock body = op.getBody();
				return body.execute(new StackFrame(frame, body)); // no need to copy arguments
			}
			if (type instanceof Class<?>) {
				return EMFTVMUtil.invokeNativeStatic(frame, (Class<?>)type, opname);
			}
			throw new UnsupportedOperationException(String.format("static %s::%s()", 
					EMFTVMUtil.getTypeName(env, type), 
					opname));
		case 1:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., type, arg1, arg2]
			final Object arg = stack.pop();
			type = stack.pop();

			if (type == null) {
				throw new IllegalArgumentException(String.format("Cannot invoke static operation %s on null type", opname));
			}

			if (type == env.eClass()) { // Lazy and called rule invocations are indistinguishable from static operations in ATL
				final Rule rule = env.findRule(opname);
				if (rule != null && rule.getMode() == RuleMode.MANUAL) {
					return matchOne(frame, rule, new Object[] { arg });
				}
			}

			op = env.findStaticOperation(type, opname, EMFTVMUtil.getArgumentType(arg));
			if (op != null) {
				final CodeBlock body = op.getBody();
				return body.execute(frame.getSubFrame(body, arg));
			}
			if (type instanceof Class<?>) {
				return EMFTVMUtil.invokeNativeStatic(frame, (Class<?>)type, opname, arg);
			}
			throw new UnsupportedOperationException(String.format("static %s::%s(%s)", 
					EMFTVMUtil.getTypeName(env, type), 
					opname, 
					EMFTVMUtil.getTypeName(env, EMFTVMUtil.getArgumentType(arg))));
		default:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., type, arg1, arg2]
			final Object[] args = stack.pop(argcount);
			type = stack.pop();

			if (type == null) {
				throw new IllegalArgumentException(String.format("Cannot invoke static operation %s on null type", opname));
			}

			if (type == env.eClass()) { // Lazy and called rule invocations are indistinguishable from static operations in ATL
				final Rule rule = env.findRule(opname);
				if (rule != null && rule.getMode() == RuleMode.MANUAL) {
					return matchOne(frame, rule, args);
				}
			}

			//TODO treat context type as a regular argument (cf. Java's Method.invoke())
			op = env.findStaticOperation(type, opname, EMFTVMUtil.getArgumentTypes(args));
			if (op != null) {
				final CodeBlock body = op.getBody();
				return body.execute(frame.getSubFrame(body, args));
			}
			if (type instanceof Class<?>) {
				return EMFTVMUtil.invokeNativeStatic(frame, (Class<?>)type, opname, args);
			}
			throw new UnsupportedOperationException(String.format("static %s::%s(%s)", 
					EMFTVMUtil.getTypeName(env, type), 
					opname, 
					EMFTVMUtil.getTypeNames(env, EMFTVMUtil.getArgumentTypes(args))));
		}
	}

	/**
	 * Implements the INVOKE_SUPER instruction.
	 * @param eContext the current execution context type
	 * @param opname
	 * @param argcount
	 * @param frame
	 * @return the invocation result
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private static Object invokeSuper(final Operation op, final String opname, final int argcount, final StackFrame frame, final Stack stack)
			throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		if (op == null) {
			throw new IllegalArgumentException("INVOKE_SUPER can only be used in operations");
		}
		final EClassifier context = op.getEContext();
		if (context == null) {
			throw new IllegalArgumentException(String.format("Operation misses context type: %s", op));
		}

		final java.util.Set<Operation> ops = new LinkedHashSet<Operation>();
		final Class<?> ic = context.getInstanceClass();
		final List<?> superTypes;
		if (context instanceof EClass) {
			superTypes = ((EClass)context).getESuperTypes();
		} else {
			if (ic == null) {
				throw new IllegalArgumentException(String.format("Primitive EMF type without instance class %s", context));
			}
			superTypes = Collections.singletonList(ic.getSuperclass());
		}

		final ExecEnv env = frame.getEnv();
		Operation superOp = null;
		final Object o;
		final Method method;

		switch (argcount) {
		case 0:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., self, arg1, arg2]
			o = stack.pop();

			for (Object superType : superTypes) {
				superOp = env.findOperation(superType, opname);
				if (superOp != null) {
					ops.add(superOp);
				}
			}
			if (ops.size() > 1) {
				throw new DuplicateEntryException(String.format(
						"More than one super-operation found for context %s: %s",
						context, ops));
			}
			if (!ops.isEmpty()) {
				superOp = ops.iterator().next();
			}

			method = EMFTVMUtil.findNativeSuperMethod(superOp, ic, opname);
			if (method != null) {
				return EMFTVMUtil.invokeNative(frame, o, method);
			}
			if (superOp != null) {
				final CodeBlock body = superOp.getBody();
				return body.execute(frame.getSubFrame(body, o));
			}

			throw new UnsupportedOperationException(String.format("super %s::%s()", 
					EMFTVMUtil.getTypeName(env, context), 
					opname));
		case 1:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., self, arg1, arg2]
			final Object arg = stack.pop();
			o = stack.pop();

			for (Object superType : superTypes) {
				superOp = env.findOperation(superType, opname, EMFTVMUtil.getArgumentType(arg));
				if (superOp != null) {
					ops.add(superOp);
				}
			}
			if (ops.size() > 1) {
				throw new DuplicateEntryException(String.format(
						"More than one super-operation found for context %s: %s",
						context, ops));
			}
			if (!ops.isEmpty()) {
				superOp = ops.iterator().next();
			}

			method = EMFTVMUtil.findNativeSuperMethod(superOp, ic, opname, arg);
			if (method != null) {
				return EMFTVMUtil.invokeNative(frame, o, method, arg);
			}
			if (superOp != null) {
				final CodeBlock body = superOp.getBody();
				return body.execute(frame.getSubFrame(body, o, arg));
			}

			throw new UnsupportedOperationException(String.format("super %s::%s(%s)", 
					EMFTVMUtil.getTypeName(env, context), 
					opname, 
					EMFTVMUtil.getTypeName(env, EMFTVMUtil.getArgumentType(arg))));
		default:
			// Use Java's left-to-right evaluation semantics:
			// stack = [..., self, arg1, arg2]
			final Object[] args = stack.pop(argcount);
			o = stack.pop();

			for (Object superType : superTypes) {
				superOp = env.findOperation(superType, opname, EMFTVMUtil.getArgumentTypes(args));
				if (superOp != null) {
					ops.add(superOp);
				}
			}
			if (ops.size() > 1) {
				throw new DuplicateEntryException(String.format(
						"More than one super-operation found for context %s: %s",
						context, ops));
			}
			if (!ops.isEmpty()) {
				superOp = ops.iterator().next();
			}

			method = EMFTVMUtil.findNativeSuperMethod(superOp, ic, opname, args);
			if (method != null) {
				return EMFTVMUtil.invokeNative(frame, o, method, args);
			}
			if (superOp != null) {
				final CodeBlock body = superOp.getBody();
				return body.execute(frame.getSubFrame(body, o, args));
			}

			throw new UnsupportedOperationException(String.format("super %s::%s(%s)", 
					EMFTVMUtil.getTypeName(env, context), 
					opname, 
					EMFTVMUtil.getTypeNames(env, EMFTVMUtil.getArgumentTypes(args))));
		}
	}

	/**
	 * Finds the rule referred to by <pre>instr</pre>.
	 * @param env
	 * @param rulename
	 * @return the rule mentioned by instr
	 * @throws IllegalArgumentException if rule not found
	 */
	private static Rule findRule(final ExecEnv env, final String rulename) {
		final Rule rule = env.findRule(rulename);
		if (rule == null) {
			throw new IllegalArgumentException(String.format("Rule %s not found", rulename));
		}
		return rule;
	}

	/**
	 * Executes <code>rule</code> with <code>args</code>.
	 * @param frame the current stack frame
	 * @param rule the rule
	 * @param args the rule arguments
	 */
	private static Object matchOne(final StackFrame frame, final Rule rule, final Object[] args) {
		final int argcount = args.length;
		if (argcount != rule.getInputElements().size()) {
			throw new VMException(frame, String.format(
					"Rule %s has different amount of input elements than expected: %d instead of %d",
					rule.getName(), rule.getInputElements().size(), argcount));
		}
		return rule.matchManual(frame, args);
	}

	/**
	 * Executes <code>rule</code> without arguments.
	 * @param frame the current stack frame
	 * @param rule the rule
	 */
	private static Object matchOne(final StackFrame frame, final Rule rule) {
		if (rule.getInputElements().size() != 0) {
			throw new VMException(frame, String.format(
					"Rule %s has different amount of input elements than expected: %d instead of %d",
					rule.getName(), rule.getInputElements().size(), 0));
		}
		return rule.matchManual(frame, EMPTY);
	}

	/**
	 * Clears values derived from {@link #getCode()}.
	 */
	private void codeChanged() {
		predecessors.clear();
		allPredecessors.clear();
		nlPredecessors.clear();
		eUnset(EmftvmPackage.CODE_BLOCK__MAX_STACK);
		setJITCodeBlock(null);
	}

	/**
	 * Clears values derived from {@link #getLocalVariables()}.
	 */
	private void localVariablesChanged() {
		eUnset(EmftvmPackage.CODE_BLOCK__MAX_LOCALS);
		setJITCodeBlock(null);
	}

	/**
	 * Clears values derived from {@link #getNested()}.
	 */
	private void nestedChanged() {
		setJITCodeBlock(null);
	}

} //CodeBlockImpl

Back to the top