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
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
\* */
// $Id$
package scalac.backend.msil;
import scalac.Global;
import scalac.Unit;
import scalac.ApplicationError;
import scalac.util.Debug;
import scalac.util.Name;
import scalac.util.Names;
import scalac.util.Debug;
import scalac.ast.Tree;
import Tree.*;
import scalac.symtab.Symbol;
import scalac.symtab.Scope;
import scalac.symtab.Kinds;
import scalac.symtab.TypeTags;
import scalac.symtab.Modifiers;
import scalac.symtab.Definitions;
import Scope.SymbolIterator;
import scalac.backend.Primitive;
import scalac.backend.Primitives;
import ch.epfl.lamp.compiler.msil.*;
import ch.epfl.lamp.compiler.msil.emit.*;
import ch.epfl.lamp.util.Position;
import Item.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.IOException;
/**
* Generates MS IL code via the Reflection.Emit API
* (ch.epfl.lamp.compiler.msil.emit package)
*
* @author Nikolay Mihaylov
*/
public class GenMSIL /*implements Modifiers */ {
final Map assemblies;
// The position of the paramater in the parameter list
final Map/*<Symbol, Integer>*/ params = new HashMap();
// The LocalBuilder corresponding to the local variable symbol
final Map/*<Symbol, LocalBuilder>*/ locals = new HashMap();
AssemblyBuilder defaultAssembly, currAssembly;
ModuleBuilder currModule, scalaModule;
ILGenerator code;
final Global global;
final TypeCreator tc;
final Definitions defs;
final Primitives primitives;
final ItemFactory items;
Symbol currentPackage;
static final Item TRUE_ITEM = Item.CondItem(Test.True, null, null);
static final Item FALSE_ITEM = Item.CondItem(Test.False, null, null);
final Symbol STRING_CONCAT;
static final Type SCALA_UNIT = TypeCreator.getJavaType("scala.Unit");
static final FieldInfo RUNTIME_UNIT_VAL =
TypeCreator.getJavaType("scala.runtime.RunTime").GetField("UNIT_VAL");
/**
*/
public GenMSIL(Global global, GenMSILPhase phase) {
this.global = global;
defs = global.definitions;
primitives = global.primitives;
assemblies = phase.assemblies;
tc = new TypeCreator(this, phase);
items = new ItemFactory(this);
currentPackage = defs.ROOT_CLASS; /// ???
scalaModule = getPackage("scala", false);
STRING_CONCAT = defs.STRING_CLASS.members().
lookup(Name.fromString("concat"));
}
/** Generate .NET assembly */
public ModuleBuilder getPackage(String name, boolean isRealAssembly) {
AssemblyBuilder assem = (AssemblyBuilder) assemblies.get(name);
if (assem == null) {
AssemblyName an = new AssemblyName();
an.Name = name;
assem = EmitUtils.DefineDynamicAssembly(an);
if (isRealAssembly)
assemblies.put(name, assem);
}
ModuleBuilder module = (ModuleBuilder) assem.GetModule(name);
if (module == null) {
module = assem.DefineDynamicModule(name, name + ".dll");
}
return module;
}
/**
*/
public void apply() {
if (global.target == global.TARGET_MSIL) {
currModule = getPackage("prog", true);
main = (MethodBuilder) currModule.GetMethod("Main", Type.EmptyTypes);
if (main == null) {
main = currModule.DefineGlobalMethod
("Main", MethodAttributes.Static,
Type.GetType("System.Void"),
new Type[] {Type.GetType("System.String[]")} );
main.DefineParameter(0, 0L, "args");
}
// create mappings from Symbols to MSIL Types
tc.traverse(global.units);
try {
for (int i = 0; i < global.units.length; i++) {
apply(global.units[i]);
}
} catch (Throwable e) {
//log("params: " + params);
//log("locals: " + locals);
if (code != null) {
MethodBase method = code.owner;
log("Processing method " + method.DeclaringType +
"::" +method.Name);
}
if (global.debug) e.printStackTrace();
throw (Error) e;
}
tc.createTypes();
main.GetILGenerator().Emit(OpCodes.Ret);
((AssemblyBuilder)currModule.Assembly).SetEntryPoint(main);
try {
Iterator iter = assemblies.values().iterator();
while (iter.hasNext()) {
AssemblyBuilder assem = (AssemblyBuilder) iter.next();
// log("Types defined in assembly: " + assem.FullName);
// Type[] types = assem.GetTypes();
// for (int i = 0; i < types.length; i++)
// log("\t" + types[i]);
assem.Save(assem.FullName + ".il");
}
}
catch (IOException e) {
if (global.debug) e.printStackTrace();
}
}
}
MethodBuilder main;
static final Type STRING_ARRAY = Type.GetType("System.String[]");
final Map mains = new HashMap();
void checkMain(MethodBase method) {
//log("checking method: " + method);
if ( ! currentClass.isModuleClass() )
return;
if (method.IsConstructor || method.IsAbstract ||
!method.Name.equals("main"))
return;
ParameterInfo[] params = method.GetParameters();
if (params.length != 1)
return;
if (params[0].ParameterType != STRING_ARRAY)
return;
//log("'main' method found: " + method);
mains.put(currentClass, method);
ILGenerator code = main.GetILGenerator();
code.Emit(OpCodes.Ldsfld, tc.getModuleField(currentClass));
code.Emit(OpCodes.Ldarg_0);
code.Emit(OpCodes.Callvirt, (MethodInfo)method);
if (!returnsVoid(main))
code.Emit(OpCodes.Pop);
}
Unit currUnit;
/**
*/
public void apply(Unit unit) {
currUnit = unit;
for (int i = 0; i < unit.body.length; i++) {
Tree tree = unit.body[i];
Symbol sym = tree.symbol();
switch (tree) {
case Empty: break;
case ClassDef(_, _, _, _, _, Template(_, Tree[] body)):
genClass(sym, body);
break;
case PackageDef(Tree packaged, Template impl):
//String packageName = packaged.symbol().fullNameString();
//log("Package: " + dumpSym(packaged.symbol()));// +
//"; symbol.fullName() = " + packaged.symbol().fullName());
//currModule = getPackage(packageName);
genPackage(currAssembly, impl.body);
break;
case ValDef(_, _, _, _):
//log("ValDef: " + dumpSym(tree.symbol()));
break;
default:
throw new ApplicationError
("Illegal top-level definition: " + Debug.show(tree));
}
}
}
/** Generate the code for Package definition
*/
public void genPackage(AssemblyBuilder assembly, Tree[] body) {
for (int i = 0; i < body.length; i++) {
Symbol sym = body[i].symbol();
//log("genPackage: " + dumpSym(body[i].symbol()));
switch (body[i]) {
case Empty:
break;
case ClassDef(_, _, _, _, _, Template(_, Tree[] classBody)):
genClass(sym, classBody);
break;
case ValDef(_, _, Tree tpe, Tree rhs):
if (sym.isModule()) {
} else
throw new ApplicationError();
//log("ValDef: " + dumpSym(tree.symbol()));
break;
case PackageDef(Tree packaged, Template(_ , Tree[] body2)):
//log("genPackage: nested package: " + dumpSym(sym));
genPackage(currAssembly, body2);
break;
default:
log("genPackage: Only class definitions expected in a package!: Found instead: " +
dumpSym(sym));
}
}
}
public Symbol currentClass;
public MethodBase currentMethod;
/** Generate the code for a class
*/
public void genClass(Symbol clazz, Tree[] body) {
//log("genClass: " + dumpSym(clazz));
//log("\tfullName = " + clazz.fullName());
//log("\tqualifier = " + Debug.show(clazz.qualifier()));
//log("class members: " + clazz.members());
Symbol outerClass = currentClass;
currentClass = clazz;
if ( clazz.isModule() || clazz.isModuleClass() ) {
//log("genClass: initializing module field for module " + dumpSym(clazz));
tc.getModuleField(clazz);
}
for (int i = 0; i < body.length; i++) {
Symbol sym = body[i].symbol();
switch (body[i]) {
case Empty:
break;
case ValDef(_, _, _, _):
// just to generate the field declaration
// the rhs should've been moved to the constructor body
tc.getField(sym);
break;
case ClassDef(_, _, _, _, _, Template impl):
genClass(sym, impl.body);
break;
case DefDef(_, _, _, ValDef[][] vparams, Tree tpe, Tree rhs):
//log("genClass.DefDef: " + dumpSym(sym));
//log("DefDef: type = " + Debug.show(tpe.type));
currentMethod = tc.getMethod(sym);
if (!currentMethod.IsAbstract) {
checkMain(currentMethod);
genDef(sym, vparams[0], rhs, type2MSILType(tpe.type));
}
break;
default:
assert false : "Illegal class body definition: " + body[i];
}
}
currentClass = outerClass;
} //genClass()
boolean lastStatement;
static final boolean enableTailCalls = true;
/** Generate the code for Methods
*/
public void genDef(Symbol sym, ValDef[] parameters, Tree rhs, MSILType toType) {
MethodBase method = tc.getMethod(sym);
//log("genDef: " + method.getSignature());
params.clear();
locals.clear();
int argOffset = method.IsStatic ? 0 : 1;
for (int i = 0; i < parameters.length; i++) {
params.put(parameters[i].symbol(), new Integer(i + argOffset));
}
if (method.IsConstructor) {
ConstructorInfo ctor = (ConstructorInfo) method;
code = ((ConstructorBuilder)ctor).GetILGenerator();
FieldInfo moduleField = tc.getModuleField(currentClass);
if (moduleField != null) {
// log("genDef: initializing " + moduleField +
// " for class " + method.DeclaringType);
// emit the call to the superconstructor
switch (rhs) {
case Block(Tree[] stats):
// this is the call to the super constructor
drop(gen(stats[0], MSILType.VOID));
break;
default:
drop(gen(rhs, MSILType.VOID));
}
ConstructorBuilder cctor = ((TypeBuilder)(method.DeclaringType)).
DefineConstructor(MethodAttributes.Static |
MethodAttributes.Public,
CallingConventions.Standard,
Type.EmptyTypes);
currentMethod = cctor;
ILGenerator ctorCode = code;
code = cctor.GetILGenerator();
// initialize the static module reference
code.Emit(OpCodes.Newobj, ctor);
code.Emit(OpCodes.Stsfld, moduleField);
switch (rhs) {
case Block(Tree[] stats):
int n = stats.length;
assert n > 0;
for (int i = 1; i < n; i++)
drop(gen(stats[i], MSILType.VOID));
break;
}
code.Emit(OpCodes.Ret);
code = ctorCode;
} else
drop(gen(rhs, MSILType.VOID));
}
else {
lastStatement = true;
code = ((MethodBuilder)method).GetILGenerator();
Item item = gen(rhs, toType);
if (returnsVoid(method))
drop(item);
else
coerce(load(item), toType); // coerce???
}
code.Emit(OpCodes.Ret);
lastStatement = false;
code = null;
} // genDef();
/** check if the result type of a method is void
*/
public boolean returnsVoid(MethodBase method) {
return method.IsConstructor ||
(((MethodInfo)method).ReturnType == TypeCreator.VOID);
}
/** emit the code for this'
*/
void emitThis() {
if (currentMethod.IsStatic) {
if (currentMethod.IsConstructor) {
code.Emit(OpCodes.Ldsfld, tc.getModuleField(currentClass));
} else
throw new ApplicationError
("Static methods don't have 'this' pointer");
} else
code.Emit(OpCodes.Ldarg_0);
}
/** Generate code for array of trees
*/
public Item gen(Tree[] trees) {
int n = trees.length;
if (n == 0)
return items.VoidItem();
boolean tmpLastStatement = lastStatement; lastStatement = false;
for (int i = 0; i < n-1; i++) {
drop(gen(trees[i], MSILType.VOID));
}
lastStatement = tmpLastStatement;
return gen(trees[n-1], type2MSILType(trees[n-1].type));
}
public Item gen(Tree tree) {
return gen(tree, type2MSILType(tree.type));
}
Item check(Item i) {
assert i != null;
return i;
}
int pos;
/** The main generator function. It keeps track of
* the current position in the tree for better error messages
*/
Item gen(Tree tree, MSILType toType) {
int tmpPos = pos;
pos = tree.pos;
Item i = null;
//i = gen2(tree, toType);
try { i = gen2(tree, toType); }
catch (Throwable e) {
logErr(tree.pos, "gen(): Exception");
if (global.debug) e.printStackTrace();
System.exit(1);
throw (Error) e;
}
pos = tmpPos;
return i;
}
/** Generate the code corresponding to a tree node
*/
Item gen2(Tree tree, MSILType toType) {
Symbol sym = tree.hasSymbol() ? tree.symbol() : null;
switch (tree) {
case Empty:
return items.VoidItem();
case Block(Tree[] stats):
return gen(stats);
case ValDef(_, Name name, Tree tpe, Tree rhs):
LocalBuilder local = code.DeclareLocal(tc.getType(sym));
local.SetLocalSymInfo(name.toString());
locals.put(sym, local);
if (rhs == Tree.Empty)
return items.VoidItem();
MSILType type = type2MSILType(tpe.type);
load(coerce(gen(rhs, type), type));
return check(store(items.LocalItem(type, local)));
case Ident(Name name):
//log("Ident: " + Debug.show(tree));
if (sym == defs.NULL)
//return items.LiteralItem(MSILType.NULL_REF, null);
return items.LiteralItem(MSILType.OBJECT, null);
MSILType type = type2MSILType(sym.type());
if ( sym.isModule() ) // ???
return load(items.StaticItem
(type2MSILType(tree.type), tc.getModuleField(sym)));
else {
Integer slot = (Integer) params.get(sym);
if (slot != null) {
return items.ArgItem(type, slot.intValue());
} else {
LocalBuilder local = (LocalBuilder) locals.get(sym);
if (local != null)
return items.LocalItem(type, local);
}
return items.SelectItem(type,
items.SelfItem(tc.getType(currentClass)),
tc.getField(sym));
}
case Select(Tree qualifier, Name selector):
if (sym.isModule()) {
//log("gen: Select from a module: " + sym);
if (sym.isJava())
logErr("gen.Select: Cannot treat Java class '" +
sym.fullNameString() + "' as value:\n\t" +
dumpSym(sym));
else { // scala module
Type module = tc.getType(sym);
return items.StaticItem(MSILType.REF(module),
tc.getModuleField(sym));
}
}
if (!qualifier.hasSymbol()) {
// log("gen2(): " + Debug.show(tree));
// log("\tqualifier = " + Debug.show(qualifier));
return items.SelectItem(type2MSILType(tree.type),
gen(qualifier), tc.getField(sym));
}
//??? if ((qualifier.symbol().flags & NOVAL) == NOVAL) {
// log("gen.Select: owner NOVAL");
// return items.StaticItem(type2MSILType(tree.type), tc.getField(sym));
// }
// check if the tree corresponds to a static Java field
if (qualifier.symbol().isModule() && sym.isJava()) {
//log("gen.Select: static Java field");
return items.StaticItem
(type2MSILType(tree.type), tc.getField(sym));
}
if ( qualifier.symbol().isModule() ) { // ?????
//log("gen: Select from a non-Java module: " + qualifier.symbol() + "::" + selector);
return items.SelectItem
(type2MSILType(tree.type), gen(qualifier), tc.getField(sym));
}
if ( sym.isValue() || sym.isVariable() ) {
return items.SelectItem
(type2MSILType(sym.type()),
gen(qualifier, type2MSILType(qualifier.type)),
tc.getField(sym));
}
log("gen.Select: Dunno what to do with: " + dumpSym(sym));
break;
case Apply(Tree fun, Tree[] args):
return check(genApply(fun, args, type2MSILType(tree.type)));
case Assign(Tree lhs, Tree rhs):
boolean tmpLastStatement = lastStatement; lastStatement = false;
MSILType type = type2MSILType(lhs.type);
Item var = gen(lhs, type);
load(gen(rhs, type));
lastStatement = tmpLastStatement;
return check(store(var));
case Typed(Literal(Object value), Tree tpe):
log("Typed.Literal: " + Debug.show(tree));
return items.LiteralItem(type2MSILType(tpe.type), value);
case Typed(Tree expr, Tree tpe):
//log("gen.Typed: processing node: " + Debug.show(tree));
return gen(expr, type2MSILType(tpe.type));
case New(Template(Tree[] baseClasses, Tree[] body)):
assert body.length == 0 : "Template should not have a body!";
switch (baseClasses[0]) {
case Apply(Tree fun, Tree[] args):
//MethodBase mctor = tc.getMethod(fun.symbol());
//log("GenMSIL.New: " + dumpSym(fun.symbol()));
//log("\t" + mctor);
ConstructorInfo ctor = (ConstructorInfo) tc.getMethod(fun.symbol());
loadArgs(args, ctor.GetParameters());
code.Emit(OpCodes.Newobj, ctor);
return items.StackItem(MSILType.REF(ctor.DeclaringType));
default:
throw new ApplicationError("Dunno what to do!");
}
case This(_):
return items.SelfItem(tc.getType(currentClass));
case Super(_):
//logErr("Not implemented yet: Super(" + Debug.show(sym) + ")");
//log("gen.Super(_): Super.symbol() = " + dumpSym(sym));
//log("gen.Super(tpe): tpe.symbol() = " + dumpSym(tpe.symbol()));
//log("gen.Super(tpe): tpe = " + Debug.show(tpe));
//log("gen.Super(tpe): tpe.type() = " + Debug.show(tpe.type()));
//return items.VoidItem();
Item retItem = items.SelfItem(tc.getType(currentClass));
//log("gen.Super: generated item: " + retItem);
return retItem;
case Literal(Object value):
//log("Literal: " + Debug.show(tree));
//log("\ttype = " + Debug.show(tree.type));
MSILType t = type2MSILType(tree.type);
//log("\tmsil type = " + t);
return items.LiteralItem(t, value);
case If(Tree cond, Tree thenp, Tree elsep):
Item item = genIf(cond, thenp, elsep, toType);
return check(item);
default:
throw new ApplicationError("Dunno what to do: " + tree);
}
throw new ApplicationError
("Dunno what to do with tree node: " + Debug.show(tree));
} //gen()
Item genIf(Tree condp, Tree thenp, Tree elsep, MSILType toType) {
Item iThen = null;
//log("genIf: coerce to type: " + toType);
//log(" thenTree = " + Debug.show(thenp));
//log(" elseTree = " + Debug.show(elsep));
if (elsep == Tree.Empty && toType == MSILType.VOID) {
boolean tmpLastStatement = lastStatement; lastStatement = false;
Item condi = gen(condp, type2MSILType(condp.type));
Item.CondItem cond = mkCond(condi); //gen(cond, type2MSILType(cond.type)));
lastStatement = tmpLastStatement;
if (cond.success == null) {
Chain success = new Chain(code.DefineLabel(), null);
branch(cond.test, success);
iThen = coerce(load(gen(thenp, toType)), toType);
resolve(success);
resolve(cond.failure);
} else {
Chain fail = (cond.failure != null) ?
cond.failure : new Chain(code.DefineLabel(), null);
branch(cond.test, fail);
resolve(cond.success);
iThen = coerce(load(gen(thenp, toType)), toType);
resolve(fail);
}
} else {
boolean tmpLastStatement = lastStatement; lastStatement = false;
Item condi = gen(condp, type2MSILType(condp.type));
Item.CondItem cond = mkCond(condi); //gen(cond, type2MSILType(cond.type)));
lastStatement = tmpLastStatement;
Chain fail = cond.failure != null ?
cond.failure : new Chain(code.DefineLabel(), null);
branch(cond.test, fail);
resolve(cond.success);
Item thenItem = gen(thenp, toType);
iThen = load(coerce(thenItem, toType));
Label exit = null;
if (lastStatement)
code.Emit(OpCodes.Ret);
else {
if (lastStatement)
code.Emit(OpCodes.Ret);
else {
exit = code.DefineLabel();
code.Emit(OpCodes.Br, exit);
}
}
resolve(fail);
Item iElse= null;
if (elsep == Tree.Empty) {
iElse = items.StaticItem(MSILType.REF(SCALA_UNIT), RUNTIME_UNIT_VAL);
iElse = coerce(iElse, toType);
} else
iElse = load(coerce(gen(elsep, toType), toType));
resolve(exit);
//log("\tgenIf: 'then' item = " + iThen + "; 'else' item" + iElse);
}
return iThen;
} //genIf
Item coerce(Item item, MSILType toType) {
//log("coerce(): coercing item " + item + " to " + toType);
assert toType != null;
if (toType == MSILType.VOID)
return drop(item);
switch (item) {
case VoidItem():
case SelfItem():
return item;
case StackItem():
if (item.type == MSILType.BOOL && toType == MSILType.I4) //?????????
return items.StackItem(toType);
if (item.type != toType) {
emitConvert(toType);
return items.StackItem(toType);
}
return item;
case LiteralItem(MSILType type, Object value):
//log("coercing Literal " + type + " -> " + toType);
switch (type) {
case REF(_): return item;
default:
return (type == toType) ? item : items.LiteralItem(toType, value);
}
//return (type == toType) ? item : items.LiteralItem(toType, value);
default:
return coerce(load(item), toType);
}
}
void emitConvert(MSILType toType) {
switch (toType) {
case I1: code.Emit(OpCodes.Conv_I1); break;
case I2: code.Emit(OpCodes.Conv_I2); break;
case I4: code.Emit(OpCodes.Conv_I4); break;
case I8: code.Emit(OpCodes.Conv_I8); break;
case R4: code.Emit(OpCodes.Conv_R4); break;
case R8: code.Emit(OpCodes.Conv_R8); break;
case CHAR: code.Emit(OpCodes.Conv_U2); break;
case REF(_): break;
case ARRAY(_): break;
default:
logErr("emitConvert: " + toType);
}
}
/** Generate the code for an Apply node */
Item genApply(Tree fun, Tree[] args, MSILType resType) {
boolean tmpLastStatement = lastStatement; lastStatement = false;
Symbol sym = fun.symbol();
// if (primitives.isPrimitive(sym)) {
// log("genApply: primitive " + primitives.getPrimitive(sym).tag +
// " = " + dumpSym(sym));
// }
switch (fun) {
case Ident(_):
emitThis();
lastStatement = tmpLastStatement;
return check(invokeMethod(sym, args, resType, true));
case Select(Tree qualifier, Name selector):
// log("\tfun: " + Debug.show(fun));
// log("\tqualifier: " + Debug.show(qualifier));
// log("\tqualifier.symbol(): " + Debug.show(qualifier.symbol()));
// log("\tqualifier.type: " + Debug.show(qualifier.type));
if (sym == primitives.BOX_UVALUE) {
return items.StaticItem(MSILType.REF(SCALA_UNIT), RUNTIME_UNIT_VAL);
}
// if (sym == global.primitives.AS_UVALUE) {
// return coerce(gen(qualifier, MSILType.VOID), MSILType.VOID);
// }
if (sym == defs.EQEQ) {
return genEq(qualifier, args[0]);
}
if (sym == defs.BANGEQ) {
return negate(genEq(qualifier, args[0]));
}
if (sym == STRING_CONCAT) {
assert args.length == 1;
load(gen(qualifier));
load(gen(args[0]));
code.Emit(OpCodes.Call, TypeCreator.CONCAT_OBJECT_OBJECT);
return items.StackItem(MSILType.STRING_REF);
}
if (isPrimitiveArrayOp(sym)) {
Primitive prim = primitives.getPrimitive(sym);
loadArgs(args, tc.getMethod(sym).GetParameters());
Type elemType = getArrayType(prim);
switch (prim) {
case ZARRAY_LENGTH: case BARRAY_LENGTH: case SARRAY_LENGTH:
case CARRAY_LENGTH: case IARRAY_LENGTH: case LARRAY_LENGTH:
case FARRAY_LENGTH: case DARRAY_LENGTH: case OARRAY_LENGTH:
code.Emit(OpCodes.Ldlen);
return items.StackItem(MSILType.I4);
case NEW_ZARRAY:
case NEW_BARRAY:
case NEW_SARRAY:
case NEW_CARRAY:
case NEW_IARRAY:
case NEW_LARRAY:
case NEW_FARRAY:
case NEW_DARRAY:
//case NEW_OARRAY:
code.Emit(OpCodes.Newarr, elemType);
return items.StackItem(MSILType.ARRAY(type2MSILType(elemType)));
case ZARRAY_GET: case BARRAY_GET: case SARRAY_GET:
case CARRAY_GET: case IARRAY_GET: case LARRAY_GET:
case FARRAY_GET: case DARRAY_GET: case OARRAY_GET:
emitLdelem(MSILArrayElemType(elemType));
return items.StackItem(MSILType.BOOL);
case ZARRAY_SET: case BARRAY_SET: case SARRAY_SET:
case CARRAY_SET: case IARRAY_SET: case LARRAY_SET:
case FARRAY_SET: case DARRAY_SET: case OARRAY_SET:
emitStelem(MSILArrayElemType(elemType));
return items.VoidItem();
}
}
if (isPrimitiveOp(sym)) {
assert args.length <= 1;
Tree right = args.length > 0 ? args[0] : Tree.Empty;
return primitiveOp(qualifier, sym, right, resType);
}
switch (qualifier.type) {
case TypeRef(_, _, _):
case SingleType(_, _):
// log("genApply.Select: " + dumpSym(sym));
// log("\tfun: " + Debug.show(fun));
// log("\tqualifier: " + Debug.show(qualifier));
// log("\tqualifier.symbol(): " + Debug.show(qualifier.symbol()));
// log("\tqualifier.type: " + Debug.show(qualifier.type));
MethodBase method = tc.getMethod(sym);
boolean virtualCall = false;
if (!method.IsStatic || becomesStatic(sym)) {
//log("genApply.Select: qualifier = " + Debug.show(qualifier));
//log("genApply.Select: qualifier.type() = " +
//Debug.show(qualifier.type()));
/// FIXME after the Super attribution is correct
switch (qualifier) {
case Super(_):
load(items.SelfItem(tc.getType(currentClass)));
break;
default:
load(gen(qualifier));
virtualCall = true;
}
//load(gen(qualifier));
}
lastStatement = tmpLastStatement;
return check(invokeMethod(sym, args, resType, virtualCall));
default:
throw new ApplicationError();
} // switch(qualifier.type)
case TypeApply(Tree tfun, Tree[] targs):
final Symbol tsym = tfun.symbol();
if (primitives.isPrimitive(tsym)) {
// log("genApply.TypeApply: primitive " + primitives.getPrimitive(tsym).tag +
// " = " + dumpSym(tsym));
return primitiveOp(((Select)tfun).qualifier, tsym, targs[0], resType);
}
log("genApply.TypeApply.Select: Dunno how to process TypeApply: "
+ Debug.show(fun));
log("\tTypeApply.fun: " + Debug.show(tfun));
log("\tTypeApply.fun.symbol(): " + tsym);
throw new ApplicationError();
default:
throw new ApplicationError
("genApply: Unknown function node: " + Debug.show(fun));
} // switch (fun)
} //genApply()
/** Generate code for scala's '==' */
Item genEq(Tree left, Tree right) {
LocalBuilder tmpLocal = (LocalBuilder)locals.get(defs.EQEQ);
if (tmpLocal == null) {
tmpLocal = code.DeclareLocal(TypeCreator.SYSTEM_OBJECT);
locals.put(defs.EQEQ, tmpLocal);
}
Label l1 = code.DefineLabel(), l2 = code.DefineLabel();
load(gen(left));
load(gen(right));
code.Emit(OpCodes.Stloc, tmpLocal);
code.Emit(OpCodes.Dup);
code.Emit(OpCodes.Ldnull);
code.Emit(OpCodes.Bne_Un, l1);
code.Emit(OpCodes.Ldloc, tmpLocal);
code.Emit(OpCodes.Ceq);
code.Emit(OpCodes.Br, l2);
code.MarkLabel(l1);
code.Emit(OpCodes.Ldloc, tmpLocal);
code.Emit(OpCodes.Callvirt, TypeCreator.OBJECT_EQUALS);
code.MarkLabel(l2);
return items.StackItem(MSILType.BOOL);
}
MSILType arithmCoercion(MSILType t1, MSILType t2) {
if (t1 == t2) return t1;
int i, j, n = MSILType.ARITHM_PRECISION.length;
for (i = 0; i < n; i++)
if (t1 == MSILType.ARITHM_PRECISION[i])
break;
for (j = 0; j < n; j++)
if (t2 == MSILType.ARITHM_PRECISION[j])
break;
if (i >= n || j >= n)
log("arithmCoercion(): cannot find coercion for (" +
t1 + ", " + t2 + ")");
else
return MSILType.ARITHM_PRECISION[Math.max(i, j)];
return null;
}
public boolean isPrimitiveOp(Symbol sym) {
switch (primitives.getPrimitive(sym)) {
case POS: case NEG: case NOT:
case ADD: case SUB: case MUL: case DIV: case MOD:
case OR: case XOR: case AND:
case LSL: case LSR: case ASR:
case EQ: case NE: case LT: case LE: case GT: case GE:
case ZNOT: case ZOR: case ZAND:
case IS: case AS:
case CONCAT:
case THROW:
return true;
default:
return false;
}
}
public boolean isPrimitiveArrayOp(Symbol sym) {
switch (primitives.getPrimitive(sym)) {
case NEW_ZARRAY: case NEW_BARRAY: case NEW_SARRAY:
case NEW_CARRAY: case NEW_IARRAY: case NEW_LARRAY:
case NEW_FARRAY: case NEW_DARRAY: //case NEW_OARRAY:
case ZARRAY_LENGTH: case BARRAY_LENGTH: case SARRAY_LENGTH:
case CARRAY_LENGTH: case IARRAY_LENGTH: case LARRAY_LENGTH:
case FARRAY_LENGTH: case DARRAY_LENGTH: case OARRAY_LENGTH:
case ZARRAY_GET: case BARRAY_GET: case SARRAY_GET:
case CARRAY_GET: case IARRAY_GET: case LARRAY_GET:
case FARRAY_GET: case DARRAY_GET: case OARRAY_GET:
case ZARRAY_SET: case BARRAY_SET: case SARRAY_SET:
case CARRAY_SET: case IARRAY_SET: case LARRAY_SET:
case FARRAY_SET: case DARRAY_SET: case OARRAY_SET:
return true;
default:
return false;
}
}
//------------------------------------------------------------------------
public Item primitiveOp(Tree left, Symbol opSym, Tree right,
MSILType resType)
{
assert primitives.isPrimitive(opSym);
Primitive op = primitives.getPrimitive(opSym);
// log("primaryOp(): primitive index = " + op.tag);
// log("\t" + dumpSym(opSym));
// treat some special cases
switch (op) {
case BOX:
return invokeMethod(opSym, new Tree[]{right}, resType, false);
case CONCAT:
// log("primaryOp().CONCAT: string concat!");
load(gen(left));
load(gen(right));
code.Emit(OpCodes.Call, TypeCreator.CONCAT_OBJECT_OBJECT);
return items.StackItem(MSILType.STRING_REF);
case ADD:
if (tc.getTypeFromType(right.type) == TypeCreator.SYSTEM_STRING) {
log("primaryOp().ADD: string concat!");
load(gen(left));
load(gen(right));
code.Emit(OpCodes.Call, TypeCreator.CONCAT_OBJECT_OBJECT);
return items.StackItem(MSILType.STRING_REF);
}
break;
case IS:
// log("primitiveOp(): Any.is");
load(gen(left));
final Type type = tc.getTypeFromType(right.type);
code.Emit(OpCodes.Isinst, type);
return mkCond(items.StackItem(MSILType.REF(type)));
case AS:
// log("primitiveOp(): Any.as");
Item i = load(gen(left));
final Type type = tc.getTypeFromType(right.type);
if (!i.type.isType(type) && type != TypeCreator.SYSTEM_OBJECT) {
//log("genApply: casting item: " + i + " to type: " + type);
code.Emit(OpCodes.Castclass, type);
}
return items.StackItem(MSILType.REF(type));
}
Item iLeft = null;
switch (left) {
case Apply(Tree fun , Tree[] args):
Primitive p = primitives.getPrimitive(fun.symbol());
//log("primitiveOp: primitive.tag = " + p.tag);
switch (p) {
case BOX:
assert args.length == 1;
iLeft = gen(args[0]);
break;
default:
iLeft = gen(left);
}
break;
default:
iLeft = gen(left);
}
switch (op) {
case THROW:
//log("primitiveOp().THROW: " + Debug.show(left));
load(iLeft);
code.Emit(OpCodes.Throw);
code.Emit(OpCodes.Ldnull);
return items.StackItem(MSILType.NULL_REF);
case POS: return load(coerce(iLeft, resType));
case NEG:
iLeft = load(coerce(iLeft, resType));
code.Emit(OpCodes.Neg);
return iLeft;
case NOT:
iLeft = load(coerce(iLeft, resType));
code.Emit(OpCodes.Not);
return iLeft;
case ZNOT:
return negate(mkCond(iLeft));
case ZOR:
Item.CondItem lcond = mkCond(iLeft), rcond = null;
Chain success = lcond.success, failure = lcond.failure;
switch (lcond.test) {
case True:
return lcond;
case False:
return mkCond(gen(right, MSILType.BOOL));
case And(Test t):
success = (success != null) ?
success : new Chain(code.DefineLabel(), null);
branch(negate(lcond.test), success);
resolve(failure);
failure = null;
break;
default:
success = (success != null) ?
success : new Chain(code.DefineLabel(), null);
branch(negate(lcond.test), success);
}
rcond = mkCond((gen(right, MSILType.BOOL)));
rcond = items.CondItem(Test.Or(rcond.test),
merge(success, rcond.success),
merge(failure, rcond.failure));
return rcond;
case ZAND:
Item.CondItem lcond = mkCond(iLeft), rcond = null;
Chain success = lcond.success, failure = lcond.failure;
switch (lcond.test) {
case False:
return lcond;
case True:
return mkCond(gen(right, MSILType.BOOL));
case Or(Test t):
failure = (failure != null) ?
failure : new Chain(code.DefineLabel(), null);
branch(lcond.test, failure);
resolve(success);
success = null;
break;
default:
failure = (failure != null) ?
failure : new Chain(code.DefineLabel(), null);
branch(lcond.test, failure);
}
rcond = mkCond(gen(right, MSILType.BOOL));
rcond = items.CondItem(Test.And(rcond.test),
merge(success, rcond.success),
merge(failure, rcond.failure));
return rcond;
}
MSILType toType = arithmCoercion(primitiveType(left.type),
primitiveType(right.type));
load(coerce(iLeft, toType));
load(coerce(gen(right, toType), toType));
switch (op) {
case ADD: code.Emit(OpCodes.Add); return items.StackItem(resType);
case SUB: code.Emit(OpCodes.Sub); return items.StackItem(resType);
case MUL: code.Emit(OpCodes.Mul); return items.StackItem(resType);
case DIV: code.Emit(OpCodes.Div); return items.StackItem(resType);
case MOD: code.Emit(OpCodes.Rem); return items.StackItem(resType);
case OR: code.Emit(OpCodes.Or); return items.StackItem(resType);
case XOR: code.Emit(OpCodes.Xor); return items.StackItem(resType);
case AND: code.Emit(OpCodes.And); return items.StackItem(resType);
case LSL: code.Emit(OpCodes.Shl); return items.StackItem(resType);
case LSR: code.Emit(OpCodes.Shr); return items.StackItem(resType);
case ASR: code.Emit(OpCodes.Shr_Un); return items.StackItem(resType);
case EQ: return items.CondItem(Test.Binary(Test.EQ, toType), null, null);
case NE: return items.CondItem(Test.Binary(Test.NE, toType), null, null);
case LT: return items.CondItem(Test.Binary(Test.LT_IS, toType), null, null);
case LE: return items.CondItem(Test.Binary(Test.LE_IS, toType), null, null);
case GT: return items.CondItem(Test.Binary(Test.GT_IS, toType), null, null);
case GE: return items.CondItem(Test.Binary(Test.GE_IS, toType), null, null);
}
log("primitiveOp(): dunno what to do with primitive: " + op.tag);
return items.StackItem(resType);
}
void loadArgs(Tree[] args, ParameterInfo[] params) {
boolean tmpLastStatement = lastStatement;
lastStatement = false;
for (int i = 0; i < args.length; i++) {
MSILType toType = type2MSILType(params[i].ParameterType); //type2MSILType(args[i].type);
load(coerce(gen(args[i], toType), toType));
}
lastStatement = tmpLastStatement;
}
protected boolean isStaticMember(Symbol sym) {
return sym.owner().isModuleClass() && sym.owner().isJava();
}
private boolean becomesStatic(Symbol sym) {
MethodBase method = tc.getMethod(sym);
//return method.IsStatic && method.DeclaringType == TypeCreator.MONITOR;
boolean becomesStatic = !isStaticMember(sym) && method.IsStatic;
// if (becomesStatic) {
// log("method becomes static from: " + dumpSym(sym));
// log("the new method is " + method);
// }
return becomesStatic;
}
/** Generate code for method invocation
*/
Item invokeMethod(Symbol fun, Tree[] args, MSILType resType,
boolean virtualCall) {
//log("invokeMethod: " + dumpSym(fun));
MethodBase method = tc.getMethod(fun);
assert method != null : "Coudn't resolve method: " + dumpSym(fun);
//log("\tmethod found: " + method);
Item res = null;
if (method.IsStatic) {
ParameterInfo[] params = method.GetParameters();
if (becomesStatic(fun)) {
assert params.length == args.length + 1;
ParameterInfo[] newParams = new ParameterInfo[params.length - 1];
for (int i = 0; i < newParams.length; i++)
newParams[i] = params[i + 1];
params = newParams;
}
loadArgs(args, params);
code.Emit(OpCodes.Call, (MethodInfo)method);
res = returnsVoid(method) ? items.VoidItem() :
items.StackItem(resType);
} else if (method.IsConstructor) {
// used only for calls to super constructor
//emitThis();
loadArgs(args, method.GetParameters());
code.Emit(OpCodes.Call, (ConstructorInfo)method);
res = items.VoidItem();
} else {
loadArgs(args, method.GetParameters());
if (enableTailCalls && lastStatement && method == currentMethod)
code.Emit(OpCodes.Tailcall);
code.Emit(virtualCall ? OpCodes.Callvirt : OpCodes.Call,
(MethodInfo)method);
res = returnsVoid(method) ? items.VoidItem() : items.StackItem(resType);
}
return check(res);
}
public MSILType type2MSILType(scalac.symtab.Type type) {
switch (type) {
case UnboxedType(int kind):
return MSILType.fromKind(kind);
case TypeRef(_, Symbol s, _):
//log("TypeRef: " + dumpSym(s));
return MSILType.REF(tc.getType(s));
case ThisType(Symbol s):
//log("TypeRef: " + dumpSym(s));
return MSILType.REF(tc.getType(s));
case UnboxedArrayType(scalac.symtab.Type t):
return MSILType.ARRAY(type2MSILType(t));
default:
log("type2MSILType: don't know how to convert type " + Debug.show(type));
//return MSILType.NULL_REF;
return MSILType.OBJECT;
//logErr("type2MSILType: " + Debug.show(type));
//throw new ApplicationError();
}
}
public MSILType type2MSILType(Type type) {
if (type == TypeCreator.BYTE) return MSILType.I1;
if (type == TypeCreator.SHORT) return MSILType.I2;
if (type == TypeCreator.INT) return MSILType.I4;
if (type == TypeCreator.LONG) return MSILType.I8;
if (type == TypeCreator.FLOAT) return MSILType.R4;
if (type == TypeCreator.DOUBLE) return MSILType.R8;
if (type == TypeCreator.CHAR) return MSILType.CHAR;
if (type == TypeCreator.BOOLEAN)return MSILType.BOOL;
if (type == TypeCreator.SYSTEM_STRING) return MSILType.STRING_REF;
MSILType mtype = MSILType.REF(type);
//log("type2MSILType: convert " + type + " to " + mtype);
return mtype;
}
public MSILType primitiveType(scalac.symtab.Type type) {
MSILType mtype = type2MSILType(type);
switch (mtype) {
case REF(Type t):
if (t == TypeCreator.SCALA_BYTE) return MSILType.I1;
if (t == TypeCreator.SCALA_SHORT) return MSILType.I2;
if (t == TypeCreator.SCALA_INT) return MSILType.I4;
if (t == TypeCreator.SCALA_LONG) return MSILType.I8;
if (t == TypeCreator.SCALA_FLOAT) return MSILType.R4;
if (t == TypeCreator.SCALA_DOUBLE) return MSILType.R8;
if (t == TypeCreator.SCALA_CHAR) return MSILType.CHAR;
if (t == TypeCreator.SCALA_BOOLEAN) return MSILType.BOOL;
return type2MSILType(t);
case ARRAY(_): log("primitiveType: cannot convert " + mtype); return null;
default: return mtype;
}
}
public MSILType MSILArrayElemType(Type type) {
MSILType mtype = type2MSILType(type);
switch (mtype) {
case BOOL: return MSILType.I1;
case CHAR: return MSILType.I4;
default: return mtype;
}
}
public Type getArrayType(Primitive p) {
switch (p) {
case NEW_ZARRAY: case ZARRAY_LENGTH: case ZARRAY_GET: case ZARRAY_SET:
return TypeCreator.BOOLEAN;
case NEW_BARRAY: case BARRAY_LENGTH: case BARRAY_GET: case BARRAY_SET:
return TypeCreator.BYTE;
case NEW_SARRAY: case SARRAY_LENGTH: case SARRAY_GET: case SARRAY_SET:
return TypeCreator.SHORT;
case NEW_CARRAY: case CARRAY_LENGTH: case CARRAY_GET: case CARRAY_SET:
return TypeCreator.CHAR;
case NEW_IARRAY: case IARRAY_LENGTH: case IARRAY_GET: case IARRAY_SET:
return TypeCreator.INT;
case NEW_LARRAY: case LARRAY_LENGTH: case LARRAY_GET: case LARRAY_SET:
return TypeCreator.LONG;
case NEW_FARRAY: case FARRAY_LENGTH: case FARRAY_GET: case FARRAY_SET:
return TypeCreator.FLOAT;
case NEW_DARRAY: case DARRAY_LENGTH: case DARRAY_GET: case DARRAY_SET:
return TypeCreator.DOUBLE;
case NEW_OARRAY: case OARRAY_LENGTH: case OARRAY_GET: case OARRAY_SET:
return TypeCreator.SYSTEM_OBJECT;
}
log("getArrayElemType(): unknown primitive " + p.tag);
return null;
}
public Item emitLdelem(MSILType type) {
switch (type) {
case I1: code.Emit(OpCodes.Ldelem_I1); break;
case I2: code.Emit(OpCodes.Ldelem_I2); break;
case I4: code.Emit(OpCodes.Ldelem_I4); break;
case I8: code.Emit(OpCodes.Ldelem_I8); break;
case R4: code.Emit(OpCodes.Ldelem_R4); break;
case R8: code.Emit(OpCodes.Ldelem_R8); break;
case ARRAY(_):
case REF(_):
code.Emit(OpCodes.Ldelem_Ref); break;
}
return items.StackItem(type);
}
public Item emitStelem(MSILType type) {
switch (type) {
case I1: code.Emit(OpCodes.Stelem_I1); break;
case I2: code.Emit(OpCodes.Stelem_I2); break;
case I4: code.Emit(OpCodes.Stelem_I4); break;
case I8: code.Emit(OpCodes.Stelem_I8); break;
case R4: code.Emit(OpCodes.Stelem_R4); break;
case R8: code.Emit(OpCodes.Stelem_R8); break;
case ARRAY(_):
case REF(_):
code.Emit(OpCodes.Stelem_Ref); break;
}
return items.StackItem(type);
}
/** load an item onto the stack
*/
public Item load(Item that) {
switch (that) {
case VoidItem():
return that;
case StackItem():
return (StackItem) that;
case LiteralItem(MSILType type, Object value):
return loadLiteral(type, value);
case SelfItem():
emitThis();
return items.StackItem(that.type);
case ArgItem(int slot):
if (slot > 255)
code.Emit(OpCodes.Ldarg, slot);
else if(slot > 3)
code.Emit(OpCodes.Ldarg_S, slot);
else switch (slot) {
case 0: code.Emit(OpCodes.Ldarg_0); break;
case 1: code.Emit(OpCodes.Ldarg_1); break;
case 2: code.Emit(OpCodes.Ldarg_2); break;
case 3: code.Emit(OpCodes.Ldarg_3); break;
}
return items.StackItem(that.type);
case LocalItem(LocalBuilder local):
code.Emit(OpCodes.Ldloc, local);
return items.StackItem(that.type);
case StaticItem(FieldInfo field):
code.Emit(OpCodes.Ldsfld, field);
return items.StackItem(that.type);
case SelectItem(Item qual, FieldInfo field):
Item i = load(qual);
// switch (i.type) {
// case REF(Type t):
// if (t.IsInterface) {
// Symbol s = tc.getSymbol(t);
// s = (Symbol) global.PHASE.ADDINTERFACES.interfaceToClass.get(s);
// Type classType = tc.getType(s);
// log("load.SelectItem: warning: inserting class cast from " + t + " to " + classType);
// code.Emit(OpCodes.Castclass, classType);
// }
// break;
// default:
// }
code.Emit(OpCodes.Ldfld, field);
//log("load(Item): SelectItem; type = " + that.type);
return items.StackItem(that.type);
case CondItem(Test test, Chain success, Chain failure):
load(test);
switch (test) {
case Or(_):
case And(_):
//log("Loading " + that);
Label exit = null;
if (lastStatement)
code.Emit(OpCodes.Ret);
else {
exit = code.DefineLabel();
code.Emit(OpCodes.Br, exit);
}
if (failure != null) {
resolve(failure);
load(FALSE_ITEM);
if (success != null) {
if (lastStatement)
code.Emit(OpCodes.Ret);
else
code.Emit(OpCodes.Br, exit);
}
}
if (success != null) {
resolve(success);
load(TRUE_ITEM);
}
resolve(exit);
//log("Loading " + that);
break;
}
return items.StackItem(MSILType.BOOL);
default:
throw new ApplicationError("load item: " + that);
}
}
/**
*/
void load(Test test) {
switch (test) {
case False:
code.Emit(OpCodes.Ldc_I4_0);
break;
case True:
code.Emit(OpCodes.Ldc_I4_1);
break;
case Bool(boolean value):
if (value) negate_load();
break;
case Binary(int opcode, MSILType type):
code.Emit(load(opcode));
if (negate_load(opcode)) negate_load();
break;
case And(Test t):
load(t);
break;
case Or(Test t):
load(t);
break;
default:
throw new ApplicationError(test.getClass().getName());
}
}
/**
* Generate the code for a literal
*/
protected Item.StackItem loadLiteral(MSILType type, Object obj) {
switch (type) {
case I1:
case I2:
case I4:
case CHAR:
int i = (type == MSILType.CHAR) ?
(int)((Character) obj).charValue() :
((Number)obj).intValue();
switch (i) {
case -1:code.Emit(OpCodes.Ldc_I4_M1); break;
case 0: code.Emit(OpCodes.Ldc_I4_0); break;
case 1: code.Emit(OpCodes.Ldc_I4_1); break;
case 2: code.Emit(OpCodes.Ldc_I4_2); break;
case 3: code.Emit(OpCodes.Ldc_I4_3); break;
case 4: code.Emit(OpCodes.Ldc_I4_4); break;
case 5: code.Emit(OpCodes.Ldc_I4_5); break;
case 6: code.Emit(OpCodes.Ldc_I4_6); break;
case 7: code.Emit(OpCodes.Ldc_I4_7); break;
case 8: code.Emit(OpCodes.Ldc_I4_8); break;
default:
if (i >= -128 && i <= 127)
code.Emit(OpCodes.Ldc_I4_S, i);
else
code.Emit(OpCodes.Ldc_I4, i);
}
break;
case I8:
code.Emit(OpCodes.Ldc_I8, ((Number)obj).longValue());
break;
case R4:
code.Emit(OpCodes.Ldc_R4, ((Number)obj).floatValue());
break;
case R8:
code.Emit(OpCodes.Ldc_R8, ((Number)obj).doubleValue());
break;
case BOOL:
if (((Boolean)obj).booleanValue())
code.Emit(OpCodes.Ldc_I4_1);
else
code.Emit(OpCodes.Ldc_I4_0);
break;
case VOID:
code.Emit(OpCodes.Ldsfld, RUNTIME_UNIT_VAL);
break;
case REF(Type refType):
if (obj == null) {
code.Emit(OpCodes.Ldnull);
} else if (refType == TypeCreator.SYSTEM_STRING) {
code.Emit(OpCodes.Ldstr, obj.toString());
} else {
throw new ApplicationError
("loadLiteral(): unexpected literal type: " + refType +
"; value = " + obj);
}
break;
default:
throw new ApplicationError
("loadLiteral(): Unknown literal type: " + type);
}
return items.StackItem(type);
} // genLiteral()
/**
*/
public Item store(Item that) {
switch (that) {
case ArgItem(int slot):
code.Emit(OpCodes.Starg, slot);
break;
case LocalItem(LocalBuilder local):
code.Emit(OpCodes.Stloc, local);
break;
case StaticItem(FieldInfo field):
code.Emit(OpCodes.Stsfld, field);
break;
case SelectItem(Item qual, FieldInfo field):
load(qual);
code.Emit(OpCodes.Stfld, field);
break;
default:
throw new ApplicationError("Cannot store item: " + that);
}
return items.VoidItem();
}
/**
*/
Item drop(Item that) {
switch (that) {
case VoidItem():
case SelfItem():
case LiteralItem(_, _):
case ArgItem(_):
case LocalItem(_):
case StaticItem(_):
break;
case StackItem():
code.Emit(OpCodes.Pop);
break;
case SelectItem(Item qual, _):
drop(qual);
break;
case CondItem(Test test, _, _):
switch (test) {
case False:
case True:
break;
case Bool(_):
code.Emit(OpCodes.Pop);
break;
case And(_):
case Or(_):
drop(load(that));
break;
case Binary(_, _):
code.Emit(OpCodes.Pop);
code.Emit(OpCodes.Pop);
break;
default :
throw new ApplicationError(that.getClass().getName());
}
break;
default:
drop(load(that));
}
return Item.VoidItem();
}
public Item.CondItem mkCond(Item that) {
switch (that) {
case CondItem(_, _, _): return (Item.CondItem) that;
default:
load(that);
return items.CondItem(Test.Bool(false), null, null);
}
}
public Item.CondItem negate(Item item) {
Item.CondItem cond = mkCond(item);
// swap the failure and success chains
return items.CondItem(negate(cond.test), cond.success, cond.failure);
}
void negate_load() {
code.Emit(OpCodes.Ldc_I4_1);
code.Emit(OpCodes.Xor);
}
public Test negate(Test that) {
switch (that) {
case False:
return Test.True;
case True:
return Test.False;
case Bool(boolean value):
return Test.Bool(!value);
case And(Test test):
return Test.Or(negate(test));
case Or(Test test):
return Test.And(negate(test));
case Binary(int opcode, MSILType type):
return Test.Binary(negate(opcode), type);
default:
throw new ApplicationError(that.getClass().getName());
}
}
Label branch(Test test, Chain chain) {
assert chain != null;
Label label = chain.label;
switch (test) {
case False:
code.Emit(OpCodes.Br, label);
return label;
case True:
return null;
case Bool(boolean value):
code.Emit(value ? OpCodes.Brtrue : OpCodes.Brfalse, label);
return label;
case And(Test t):
return branch(t, chain);
case Or(Test t):
return branch(t, chain);
case Binary(int opcode, MSILType type):
code.Emit(branch(negate(opcode)), label);
return label;
default:
throw new ApplicationError();
}
}
static Chain merge(Chain c1, Chain c2) {
if (c1 == null) return c2;
if (c2 == null) return c1;
Chain c = c1;
for(; c.next != null; c = c.next) ;
c.next = c2;
return c1;
}
void resolve(Chain c) {
for (; c != null; c = c.next)
code.MarkLabel(c.label);
}
void resolve(Label l) {
if (l != null)
code.MarkLabel(l);
}
public static boolean negate_load(int opcode) {
switch (opcode) {
case Test.LT_IS: return false;
case Test.LE_IS: return true;
case Test.GT_IS: return false;
case Test.GE_IS: return true;
case Test.LT_IU: return false;
case Test.LE_IU: return true;
case Test.GT_IU: return false;
case Test.GE_IU: return true;
case Test.LT_RO: return false;
case Test.LE_RO: return true;
case Test.GT_RO: return false;
case Test.GE_RO: return true;
case Test.LT_RU: return false;
case Test.LE_RU: return true;
case Test.GT_RU: return false;
case Test.GE_RU: return true;
case Test.EQ : return false;
case Test.NE : return true;
default : throw new ApplicationError("" + opcode);
}
}
public static OpCode load(int opcode) {
switch (opcode) {
case Test.LT_IS: return OpCodes.Clt;
case Test.LE_IS: return OpCodes.Cgt; // negate
case Test.GT_IS: return OpCodes.Cgt;
case Test.GE_IS: return OpCodes.Clt; // negate
case Test.LT_IU: return OpCodes.Clt_Un;
case Test.LE_IU: return OpCodes.Cgt_Un; // negate
case Test.GT_IU: return OpCodes.Cgt_Un;
case Test.GE_IU: return OpCodes.Clt_Un; // negate
case Test.LT_RO: return OpCodes.Clt;
case Test.LE_RO: return OpCodes.Cgt_Un; // negate
case Test.GT_RO: return OpCodes.Cgt;
case Test.GE_RO: return OpCodes.Clt_Un; // negate
case Test.LT_RU: return OpCodes.Clt_Un;
case Test.LE_RU: return OpCodes.Cgt; // negate
case Test.GT_RU: return OpCodes.Cgt_Un;
case Test.GE_RU: return OpCodes.Clt; // negate
case Test.EQ : return OpCodes.Ceq;
case Test.NE : return OpCodes.Ceq; // negate
default : throw new ApplicationError("" + opcode);
}
}
public static int negate(int opcode) {
switch (opcode) {
case Test.LT_IS: return Test.GE_IS;
case Test.LE_IS: return Test.GT_IS;
case Test.GT_IS: return Test.LE_IS;
case Test.GE_IS: return Test.LT_IS;
case Test.LT_IU: return Test.GE_IU;
case Test.LE_IU: return Test.GT_IU;
case Test.GT_IU: return Test.LE_IU;
case Test.GE_IU: return Test.LT_IU;
case Test.LT_RO: return Test.GE_RU;
case Test.LE_RO: return Test.GT_RU;
case Test.GT_RO: return Test.LE_RU;
case Test.GE_RO: return Test.LT_RU;
case Test.LT_RU: return Test.GE_RO;
case Test.LE_RU: return Test.GT_RO;
case Test.GT_RU: return Test.LE_RO;
case Test.GE_RU: return Test.LT_RO;
case Test.EQ : return Test.NE;
case Test.NE : return Test.EQ;
default : throw new ApplicationError("" + opcode);
}
}
public static OpCode branch(int opcode) {
switch (opcode) {
case Test.LT_IS:
case Test.LT_RO: return OpCodes.Blt;
case Test.LE_IS:
case Test.LE_RO: return OpCodes.Ble;
case Test.GT_IS:
case Test.GT_RO: return OpCodes.Bgt;
case Test.GE_IS:
case Test.GE_RO: return OpCodes.Bge;
case Test.LT_IU:
case Test.LT_RU: return OpCodes.Blt_Un;
case Test.LE_IU:
case Test.LE_RU: return OpCodes.Ble_Un;
case Test.GT_IU:
case Test.GT_RU: return OpCodes.Bgt_Un;
case Test.GE_IU:
case Test.GE_RU: return OpCodes.Bge_Un;
case Test.EQ : return OpCodes.Beq;
case Test.NE : return OpCodes.Bne_Un;
default : throw new ApplicationError("" + opcode);
}
}
///////////////////////////////////////////////////////////////////////////
public static String dumpSym(Symbol sym) {
if (sym == null) return "<null>";
if (sym == Symbol.NONE) return "NoSymbol";
return "symbol = " + Debug.show(sym) +
"; owner = " + Debug.show(sym.owner()) +
"; info = " + Debug.show(sym.info()) +
"; kind = " + sym.kind +
"; flags = " + Integer.toHexString(sym.flags);
}
/**
*/
void log(String message) {
global.reporter.printMessage(message);
}
void logErr(int pos, String message) {
global.reporter.printMessage(currUnit.position(pos), message);
}
void logErr(String message) {
if (code != null) {
MethodBase method = code.owner;
System.err.print("Processing " + method.DeclaringType.FullName +
"::" + method.Name + " -> ");
}
System.err.println(message);
throw new ApplicationError();
//log(1, message);
}
private static final int DEBUG_LEVEL = 2;
private void log(int level, String message) {
if (level < DEBUG_LEVEL)
global.log(message);
}
void emitComment(String str) {
code.Emit(OpCodes.Ldstr, str);
code.Emit(OpCodes.Pop);
}
} // class GenMSIL
public class MSILType {
public case BOOL;
public case CHAR;
public case I1;
public case I2;
public case U2;
public case I4;
public case I8;
public case R4;
public case R8;
public case REF(Type t);
public case ARRAY(MSILType t);
public case VOID;
public static final MSILType NULL_REF = REF(null);
public static final MSILType OBJECT = REF(TypeCreator.SYSTEM_OBJECT);
public static final MSILType STRING_REF = REF(TypeCreator.SYSTEM_STRING);
public static final MSILType [] ARITHM_PRECISION =
new MSILType[] {I1, I2, I4, I8, R4, R8};
public static MSILType fromKind(int kind) {
switch (kind) {
case TypeTags.BYTE: return I1;
case TypeTags.CHAR: return CHAR;
case TypeTags.SHORT: return I2;
case TypeTags.INT: return I4;
case TypeTags.LONG: return I8;
case TypeTags.FLOAT: return R4;
case TypeTags.DOUBLE: return R8;
case TypeTags.BOOLEAN: return BOOL;
case TypeTags.UNIT: return VOID;
case TypeTags.STRING: return STRING_REF;
default:
throw new ApplicationError("Unknown kind: " + kind);
}
}
public boolean isType(Type type) {
switch (this) {
case REF(Type t): return (type == t);
default:
return false;
}
}
public String toString() {
switch (this) {
case I1: return "I1";
case I2: return "I2";
case U2: return "U2";
case I4: return "I4";
case I8: return "I8";
case R4: return "R4";
case R8: return "R8";
case BOOL: return "BOOL";
case CHAR: return "CHAR";
case REF(Type t): return "REF(" + t + ")";
case ARRAY(MSILType t): return "ARRAY(" + t + ")";
case VOID: return "VOID";
default: return getClass().getName();
}
}
}
/** class representing the items
*/
class Item {
public MSILType type;
public case VoidItem();
public case StackItem();
public case SelfItem();
public case LiteralItem(MSILType typ, Object value);
public case ArgItem(int slot);
public case LocalItem(LocalBuilder local);
public case StaticItem(FieldInfo field);
public case SelectItem(Item qual, FieldInfo field);
public case CondItem(Test test, Chain success, Chain failure);
public String toString() {
switch (this) {
case VoidItem(): return "VoidItem: " + type;
case StackItem(): return "StackItem: " + type ;
case SelfItem(): return "this: " + type;
case LiteralItem(_, Object value): return "LiteralItem(" + value + "): " + type;
case ArgItem( int slot): return "ArgItem(" + slot + "): " + type;
case LocalItem( LocalBuilder local): return "LocalItem(" + local + "): " + type;
case StaticItem( FieldInfo field): return "StaticItem(" + field + "): " + type;
case SelectItem(_, FieldInfo field): return "SelectItem(" + field + "): " +type;
case CondItem(Test test, Chain success, Chain failure):
return "CondItem(" + test + ", " + success + ", " + failure + "): " + type;
}
return "??Item??";
}
}
/** class implementing a chain (list) of labels
*/
class Chain {
Label label;
Chain next;
Chain(Label l, Chain n) { label = l; next = n; }
}
class ItemFactory {
GenMSIL coder;
private static final Item.VoidItem VOID = Item.VoidItem();
static { VOID.type = MSILType.VOID; }
public ItemFactory(GenMSIL _coder) {
coder = _coder;
}
public Item.VoidItem VoidItem() {
return VOID;
}
public Item.StackItem StackItem(MSILType type) {
Item.StackItem item = Item.StackItem();
item.type = type;
return item;
}
public Item.SelfItem SelfItem(Type t) {
Item.SelfItem item = Item.SelfItem();
item.type = MSILType.REF(t);
return item;
}
public Item.LiteralItem LiteralItem(MSILType type, Object value) {
Item.LiteralItem item = Item.LiteralItem(type, value);
item.type = type;
return item;
}
public Item.ArgItem ArgItem(MSILType type, int slot) {
Item.ArgItem item = Item.ArgItem(slot);
item.type = type;
return item;
}
public Item.LocalItem LocalItem(MSILType type, LocalBuilder local) {
Item.LocalItem item = Item.LocalItem(local);
item.type = type;
return item;
}
public Item.StaticItem StaticItem(MSILType type, FieldInfo field) {
assert field.IsStatic;
Item.StaticItem item = Item.StaticItem(field);
item.type = type;
return item;
}
public Item.SelectItem SelectItem(MSILType type, Item qualifier, FieldInfo field) {
assert !field.IsStatic;
Item.SelectItem item = Item.SelectItem(coder.load(qualifier), field);
item.type = type;
return item;
}
public Item.CondItem CondItem(Test test, Chain success, Chain failure) {
Item.CondItem item = Item.CondItem(test, success, failure);
item.type = MSILType.BOOL;
return item;
}
}
/** class representing the possible tests in conditional item
*/
public class Test {
public case False;
public case True;
public case Bool(boolean value);
public case Or(Test test);
public case And(Test test);
public case Binary(int opcode, MSILType type);
public static final int LT_IS = 0x00;
public static final int LE_IS = 0x01;
public static final int GT_IS = 0x02;
public static final int GE_IS = 0x03;
public static final int LT_IU = 0x04;
public static final int LE_IU = 0x05;
public static final int GT_IU = 0x06;
public static final int GE_IU = 0x07;
public static final int LT_RO = 0x08;
public static final int LE_RO = 0x09;
public static final int GT_RO = 0x0A;
public static final int GE_RO = 0x0B;
public static final int LT_RU = 0x0C;
public static final int LE_RU = 0x0D;
public static final int GT_RU = 0x0E;
public static final int GE_RU = 0x0F;
public static final int EQ = 0x10;
public static final int NE = 0x11;
public static String toString(int opcode) {
switch (opcode) {
case LT_IS: return "GE_IS";
case LE_IS: return "GT_IS";
case GT_IS: return "LE_IS";
case GE_IS: return "LT_IS";
case LT_IU: return "GE_IU";
case LE_IU: return "GT_IU";
case GT_IU: return "LE_IU";
case GE_IU: return "LT_IU";
case LT_RO: return "GE_RU";
case LE_RO: return "GT_RU";
case GT_RO: return "LE_RU";
case GE_RO: return "LT_RU";
case LT_RU: return "GE_RO";
case LE_RU: return "GT_RO";
case GT_RU: return "LE_RO";
case GE_RU: return "LT_RO";
case EQ : return "NE";
case NE : return "EQ";
default : throw new InternalError("" + opcode);
}
}
public String toString() {
switch (this) {
case False:
return "False";
case True:
return "True";
case Bool(boolean value):
return "Test(" + value + ")";
case Or(Test test):
return "Or(" + test + ")";
case And(Test test):
return "And(" + test + ")";
case Binary(int opcode, MSILType type):
return "Binary(" + toString(opcode) +"," + type + ")";
default:
throw new ApplicationError(getClass().getName());
}
}
} // class Test
|