summaryrefslogtreecommitdiff
path: root/usr/austin/eval/expr.go
blob: 49882247394c7f4c83f59346cd8ab134562eff8d (plain)
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
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package eval

import (
	"bignum";
	"eval";
	"go/ast";
	"go/scanner";
	"go/token";
	"log";
	"os";
	"strconv";
	"strings";
)

// An exprCompiler compiles a single node in an expression.  It stores
// the whole expression's context plus information specific to this node.
// After compilation, it stores the type of the expression and its
// evaluator function.
type exprCompiler struct {
	*exprContext;
	pos token.Position;
	t Type;
	// Evaluate this node as the given type.
	evalBool func(f *Frame) bool;
	evalUint func(f *Frame) uint64;
	evalInt func(f *Frame) int64;
	// TODO(austin) evalIdealInt and evalIdealFloat shouldn't be
	// functions at all.
	evalIdealInt func() *bignum.Integer;
	evalFloat func(f *Frame) float64;
	evalIdealFloat func() *bignum.Rational;
	evalString func(f *Frame) string;
	evalArray func(f *Frame) ArrayValue;
	evalStruct func(f *Frame) StructValue;
	evalPtr func(f *Frame) Value;
	evalFunc func(f *Frame) Func;
	evalMulti func(f *Frame) []Value;
	// Evaluate to the "address of" this value; that is, the
	// settable Value object.  nil for expressions whose address
	// cannot be taken.
	evalAddr func(f *Frame) Value;
	// Execute this expression as a statement.  Only expressions
	// that are valid expression statements should set this.
	exec func(f *Frame);
	// A short string describing this expression for error
	// messages.  Only necessary if t != nil.
	desc string;
}

func newExprCompiler(c *exprContext, pos token.Position) *exprCompiler {
	return &exprCompiler{
		exprContext: c,
		pos: pos,
		desc: "<missing description>"
	};
}

// Operator generators
// TODO(austin) Remove these forward declarations
func (a *exprCompiler) genConstant(v Value)
func (a *exprCompiler) genIdentOp(level int, index int)
func (a *exprCompiler) genIndexArray(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genFuncCall(call func(f *Frame) []Value)
func (a *exprCompiler) genValue(vf func(*Frame) Value)
func (a *exprCompiler) genUnaryOpNeg(v *exprCompiler)
func (a *exprCompiler) genUnaryOpNot(v *exprCompiler)
func (a *exprCompiler) genUnaryOpXor(v *exprCompiler)
func (a *exprCompiler) genBinOpAdd(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpSub(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpMul(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpQuo(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpRem(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpAnd(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpOr(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpXor(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpAndNot(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpShl(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpShr(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpLss(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpGtr(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpLeq(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpGeq(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpEql(l *exprCompiler, r *exprCompiler)
func (a *exprCompiler) genBinOpNeq(l *exprCompiler, r *exprCompiler)
func genAssign(lt Type, r *exprCompiler) (func(lv Value, f *Frame))

func (a *exprCompiler) copy() *exprCompiler {
	ec := newExprCompiler(a.exprContext, a.pos);
	ec.desc = a.desc;
	return ec;
}

func (a *exprCompiler) copyVisit(x ast.Expr) *exprCompiler {
	ec := newExprCompiler(a.exprContext, x.Pos());
	x.Visit(ec);
	return ec;
}

func (a *exprCompiler) diag(format string, args ...) {
	a.diagAt(&a.pos, format, args);
}

func (a *exprCompiler) diagOpType(op token.Token, vt Type) {
	a.diag("illegal operand type for '%v' operator\n\t%v", op, vt);
}

func (a *exprCompiler) diagOpTypes(op token.Token, lt Type, rt Type) {
	a.diag("illegal operand types for '%v' operator\n\t%v\n\t%v", op, lt, rt);
}

/*
 * "As" functions.  These retrieve evaluator functions from an
 * exprCompiler, panicking if the requested evaluator is nil.
 */

func (a *exprCompiler) asBool() (func(f *Frame) bool) {
	if a.evalBool == nil {
		log.Crashf("tried to get %v node as boolType", a.t);
	}
	return a.evalBool;
}

func (a *exprCompiler) asUint() (func(f *Frame) uint64) {
	if a.evalUint == nil {
		log.Crashf("tried to get %v node as uintType", a.t);
	}
	return a.evalUint;
}

func (a *exprCompiler) asInt() (func(f *Frame) int64) {
	if a.evalInt == nil {
		log.Crashf("tried to get %v node as intType", a.t);
	}
	return a.evalInt;
}

func (a *exprCompiler) asIdealInt() (func() *bignum.Integer) {
	if a.evalIdealInt == nil {
		log.Crashf("tried to get %v node as idealIntType", a.t);
	}
	return a.evalIdealInt;
}

func (a *exprCompiler) asFloat() (func(f *Frame) float64) {
	if a.evalFloat == nil {
		log.Crashf("tried to get %v node as floatType", a.t);
	}
	return a.evalFloat;
}

func (a *exprCompiler) asIdealFloat() (func() *bignum.Rational) {
	if a.evalIdealFloat == nil {
		log.Crashf("tried to get %v node as idealFloatType", a.t);
	}
	return a.evalIdealFloat;
}

func (a *exprCompiler) asString() (func(f *Frame) string) {
	if a.evalString == nil {
		log.Crashf("tried to get %v node as stringType", a.t);
	}
	return a.evalString;
}

func (a *exprCompiler) asArray() (func(f *Frame) ArrayValue) {
	if a.evalArray == nil {
		log.Crashf("tried to get %v node as ArrayType", a.t);
	}
	return a.evalArray;
}

func (a *exprCompiler) asStruct() (func(f *Frame) StructValue) {
	if a.evalStruct == nil {
		log.Crashf("tried to get %v node as StructType", a.t);
	}
	return a.evalStruct;
}

func (a *exprCompiler) asPtr() (func(f *Frame) Value) {
	if a.evalPtr == nil {
		log.Crashf("tried to get %v node as PtrType", a.t);
	}
	return a.evalPtr;
}

func (a *exprCompiler) asFunc() (func(f *Frame) Func) {
	if a.evalFunc == nil {
		log.Crashf("tried to get %v node as FuncType", a.t);
	}
	return a.evalFunc;
}

func (a *exprCompiler) asMulti() (func(f *Frame) []Value) {
	if a.evalMulti == nil {
		log.Crashf("tried to get %v node as MultiType", a.t);
	}
	return a.evalMulti;
}

/*
 * Common expression manipulations
 */

// a.convertTo(t) converts the value of the analyzed expression a,
// which must be a constant, ideal number, to a new analyzed
// expression with a constant value of type t.
//
// TODO(austin) Rename to resolveIdeal or something?
func (a *exprCompiler) convertTo(t Type) *exprCompiler {
	if !a.t.isIdeal() {
		log.Crashf("attempted to convert from %v, expected ideal", a.t);
	}

	var rat *bignum.Rational;

	// XXX(Spec)  The spec says "It is erroneous".
	//
	// It is an error to assign a value with a non-zero fractional
	// part to an integer, or if the assignment would overflow or
	// underflow, or in general if the value cannot be represented
	// by the type of the variable.
	switch a.t {
	case IdealFloatType:
		rat = a.asIdealFloat()();
		if t.isInteger() && !rat.IsInt() {
			a.diag("constant %v truncated to integer", ratToString(rat));
			return nil;
		}
	case IdealIntType:
		i := a.asIdealInt()();
		rat = bignum.MakeRat(i, bignum.Nat(1));
	default:
		log.Crashf("unexpected ideal type %v", a.t);
	}

	// Check bounds
	if t, ok := t.lit().(BoundedType); ok {
		if rat.Cmp(t.minVal()) < 0 {
			a.diag("constant %v underflows %v", ratToString(rat), t);
			return nil;
		}
		if rat.Cmp(t.maxVal()) > 0 {
			a.diag("constant %v overflows %v", ratToString(rat), t);
			return nil;
		}
	}

	// Convert rat to type t.
	res := a.copy();
	res.t = t;
	switch t := t.lit().(type) {
	case *uintType:
		n, d := rat.Value();
		f := n.Quo(bignum.MakeInt(false, d));
		v := f.Abs().Value();
		res.evalUint = func(*Frame) uint64 { return v };
	case *intType:
		n, d := rat.Value();
		f := n.Quo(bignum.MakeInt(false, d));
		v := f.Value();
		res.evalInt = func(*Frame) int64 { return v };
	case *idealIntType:
		n, d := rat.Value();
		f := n.Quo(bignum.MakeInt(false, d));
		res.evalIdealInt = func() *bignum.Integer { return f };
	case *floatType:
		n, d := rat.Value();
		v := float64(n.Value())/float64(d.Value());
		res.evalFloat = func(*Frame) float64 { return v };
	case *idealFloatType:
		res.evalIdealFloat = func() *bignum.Rational { return rat };
	default:
		log.Crashf("cannot convert to type %T", t);
	}

	return res;
}

func (a *exprCompiler) genStarOp(v *exprCompiler) {
	a.genValue(v.asPtr());
}

/*
 * Assignments
 */

// An assignCompiler compiles assignment operations.  Anything other
// than short declarations should use the compileAssign wrapper.
//
// There are three valid types of assignment:
// 1) T = T
//    Assigning a single expression with single-valued type to a
//    single-valued type.
// 2) MT = T, T, ...
//    Assigning multiple expressions with single-valued types to a
//    multi-valued type.
// 3) MT = MT
//    Assigning a single expression with multi-valued type to a
//    multi-valued type.
type assignCompiler struct {
	*compiler;
	pos token.Position;
	// The RHS expressions.  This may include nil's for
	// expressions that failed to compile.
	rs []*exprCompiler;
	// The (possibly unary) MultiType of the RHS.
	rmt *MultiType;
	// Whether this is an unpack assignment (case 3).
	isUnpack bool;
	// The operation name to use in error messages, such as
	// "assignment" or "function call".
	errOp string;
	// The name to use for positions in error messages, such as
	// "argument".
	errPosName string;
}

// Type check the RHS of an assignment, returning a new assignCompiler
// and indicating if the type check succeeded.  This always returns an
// assignCompiler with rmt set, but if type checking fails, slots in
// the MultiType may be nil.  If rs contains nil's, type checking will
// fail and these expressions given a nil type.
func (a *compiler) checkAssign(pos token.Position, rs []*exprCompiler, errOp, errPosName string) (*assignCompiler, bool) {
	c := &assignCompiler{
		compiler: a,
		pos: pos,
		rs: rs,
		errOp: errOp,
		errPosName: errPosName,
	};

	// Is this an unpack?
	if len(rs) == 1 && rs[0] != nil {
		if rmt, isUnpack := rs[0].t.(*MultiType); isUnpack {
			c.rmt = rmt;
			c.isUnpack = true;
			return c, true;
		}
	}

	// Create MultiType for RHS and check that all RHS expressions
	// are single-valued.
	rts := make([]Type, len(rs));
	ok := true;
	for i, r := range rs {
		if r == nil {
			ok = false;
			continue;
		}

		if _, isMT := r.t.(*MultiType); isMT {
			r.diag("multi-valued expression not allowed in %s", errOp);
			ok = false;
			continue;
		}

		rts[i] = r.t;
	}

	c.rmt = NewMultiType(rts);
	return c, ok;
}

// compile type checks and compiles an assignment operation, returning
// a function that expects an l-value and the frame in which to
// evaluate the RHS expressions.  The l-value must have exactly the
// type given by lt.  Returns nil if type checking fails.
func (a *assignCompiler) compile(lt Type) (func(lv Value, f *Frame)) {
	lmt, isMT := lt.(*MultiType);
	rmt, isUnpack := a.rmt, a.isUnpack;

	// Create unary MultiType for single LHS
	if !isMT {
		lmt = NewMultiType([]Type{lt});
	}

	// Check that the assignment count matches
	lcount := len(lmt.Elems);
	rcount := len(rmt.Elems);
	if lcount != rcount {
		msg := "not enough";
		pos := a.pos;
		if rcount > lcount {
			msg = "too many";
			if lcount > 0 {
				pos = a.rs[lcount-1].pos;
			}
		}
		a.diagAt(&pos, "%s %ss for %s\n\t%s\n\t%s", msg, a.errPosName, a.errOp, lt, rmt);
		return nil;
	}

	bad := false;

	// TODO(austin) Deal with assignment special cases.  This is
	// tricky in the unpack case, since some of the conversions
	// can apply to single types within the multi-type.

	// Values of any type may always be assigned to variables of
	// compatible static type.
	for i, lt := range lmt.Elems {
		// Check each type individually so we can produce a
		// better error message.
		rt := rmt.Elems[i];

		// When [an ideal is] (used in an expression) assigned
		// to a variable or typed constant, the destination
		// must be able to represent the assigned value.
		if rt.isIdeal() {
			if isUnpack {
				log.Crashf("Right side of unpack contains ideal: %s", rmt);
			}
			a.rs[i] = a.rs[i].convertTo(lmt.Elems[i]);
			if a.rs[i] == nil {
				bad = true;
				continue;
			}
			rt = a.rs[i].t;
		}

		if !lt.compat(rt, false) {
			if len(a.rs) == 1 {
				a.rs[0].diag("illegal operand types for %s\n\t%v\n\t%v", a.errOp, lt, rt);
			} else {
				a.rs[i].diag("illegal operand types in %s %d of %s\n\t%v\n\t%v", a.errPosName, i+1, a.errOp, lt, rt);
			}
			bad = true;
		}
	}
	if bad {
		return nil;
	}

	// Compile
	switch {
	case !isMT:
		// Case 1
		return genAssign(lt, a.rs[0]);
	case !isUnpack:
		// Case 2
		as := make([]func(lv Value, f *Frame), len(a.rs));
		for i, r := range a.rs {
			as[i] = genAssign(lmt.Elems[i], r);
		}
		return func(lv Value, f *Frame) {
			lmv := lv.(multiV);
			for i, a := range as {
				a(lmv[i], f);
			}
		};
	default:
		// Case 3
		rf := a.rs[0].asMulti();
		return func(lv Value, f *Frame) {
			lv.Assign(multiV(rf(f)));
		};
	}
	panic();
}

// compileAssign compiles an assignment operation without the full
// generality of an assignCompiler.  See assignCompiler for a
// description of the arguments.
func (a *compiler) compileAssign(pos token.Position, lt Type, rs []*exprCompiler, errOp, errPosName string) (func(lv Value, f *Frame)) {
	ac, ok := a.checkAssign(pos, rs, errOp, errPosName);
	if !ok {
		return nil;
	}
	return ac.compile(lt);
}

/*
 * Expression visitors
 */

func (a *exprCompiler) DoBadExpr(x *ast.BadExpr) {
	// Do nothing.  Already reported by parser.
}

func (a *exprCompiler) DoIdent(x *ast.Ident) {
	level, def := a.block.Lookup(x.Value);
	if def == nil {
		a.diag("%s: undefined", x.Value);
		return;
	}
	switch def := def.(type) {
	case *Constant:
		a.t = def.Type;
		a.genConstant(def.Value);
		a.desc = "constant";
	case *Variable:
		if a.constant {
			a.diag("variable %s used in constant expression", x.Value);
			return;
		}
		if def.Type == nil {
			// Placeholder definition from an earlier error
			return;
		}
		a.t = def.Type;
		defidx := def.Index;
		a.genIdentOp(level, defidx);
		a.desc = "variable";
	case Type:
		a.diag("type %v used as expression", x.Value);
	default:
		log.Crashf("name %s has unknown type %T", x.Value, def);
	}
}

func (a *exprCompiler) doIdealInt(i *bignum.Integer) {
	a.t = IdealIntType;
	a.evalIdealInt = func() *bignum.Integer { return i };
}

func (a *exprCompiler) DoIntLit(x *ast.IntLit) {
	i, _, _2 := bignum.IntFromString(string(x.Value), 0);
	a.doIdealInt(i);
	a.desc = "integer literal";
}

func (a *exprCompiler) DoCharLit(x *ast.CharLit) {
	if x.Value[0] != '\'' {
		log.Crashf("malformed character literal %s at %v passed parser", x.Value, x.Pos());
	}
	v, mb, tail, err := strconv.UnquoteChar(string(x.Value[1:len(x.Value)]), '\'');
	if err != nil || tail != "'" {
		log.Crashf("malformed character literal %s at %v passed parser", x.Value, x.Pos());
	}
	a.doIdealInt(bignum.Int(int64(v)));
	a.desc = "character literal";
}

func (a *exprCompiler) DoFloatLit(x *ast.FloatLit) {
	f, _, n := bignum.RatFromString(string(x.Value), 0);
	if n != len(x.Value) {
		log.Crashf("malformed float literal %s at %v passed parser", x.Value, x.Pos());
	}
	a.t = IdealFloatType;
	a.evalIdealFloat = func() *bignum.Rational { return f };
	a.desc = "float literal";
}

func (a *exprCompiler) doString(s string) {
	a.t = StringType;
	a.evalString = func(*Frame) string { return s };
}

func (a *exprCompiler) DoStringLit(x *ast.StringLit) {
	s, err := strconv.Unquote(string(x.Value));
	if err != nil {
		a.diag("illegal string literal, %v", err);
		return;
	}
	a.doString(s);
	a.desc = "string literal";
}

func (a *exprCompiler) DoStringList(x *ast.StringList) {
	ss := make([]string, len(x.Strings));
	for i := 0; i < len(x.Strings); i++ {
		s, err := strconv.Unquote(string(x.Strings[i].Value));
		if err != nil {
			a.diag("illegal string literal, %v", err);
			return;
		}
		ss[i] = s;
	}
	a.doString(strings.Join(ss, ""));
	a.desc = "string literal";
}

func (a *exprCompiler) DoFuncLit(x *ast.FuncLit) {
	// TODO(austin) Closures capture their entire defining frame
	// instead of just the variables they use.

	decl := a.compileFuncType(a.block, x.Type);
	if decl == nil {
		// TODO(austin) Try compiling the body, perhaps with
		// dummy definitions for the arguments
		return;
	}

	evalFunc := a.compileFunc(a.block, decl, x.Body);
	if evalFunc == nil {
		return;
	}

	if a.constant {
		a.diag("function literal used in constant expression");
		return;
	}

	a.t = decl.Type;
	a.evalFunc = evalFunc;
}

func (a *exprCompiler) DoCompositeLit(x *ast.CompositeLit) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoParenExpr(x *ast.ParenExpr) {
	x.X.Visit(a);
}

func (a *exprCompiler) DoSelectorExpr(x *ast.SelectorExpr) {
	v := a.copyVisit(x.X);
	if v.t == nil {
		return;
	}

	// mark marks a field that matches the selector name.  It
	// tracks the best depth found so far and whether more than
	// one field has been found at that depth.
	bestDepth := -1;
	ambig := false;
	amberr := "";
	mark := func(depth int, pathName string) {
		switch {
		case bestDepth == -1 || depth < bestDepth:
			bestDepth = depth;
			ambig = false;
			amberr = "";

		case depth == bestDepth:
			ambig = true;

		default:
			log.Crashf("Marked field at depth %d, but already found one at depth %d", depth, bestDepth);
		}
		amberr += "\n\t" + pathName[1:len(pathName)];
	};

	name := x.Sel.Value;
	visited := make(map[Type] bool);

	// find recursively searches for the named field, starting at
	// type t.  If it finds the named field, it returns a function
	// which takes an exprCompiler that retrieves a value of type
	// 't' and fills 'a' to retrieve the named field.  We delay
	// exprCompiler construction to avoid filling in anything
	// until we're sure we have the right field, and to avoid
	// producing lots of garbage exprCompilers as we search.
	var find func(Type, int, string) (func (*exprCompiler));
	find = func(t Type, depth int, pathName string) (func (*exprCompiler)) {
		// Don't bother looking if we've found something shallower
		if bestDepth != -1 && bestDepth < depth {
			return nil;
		}

		// Don't check the same type twice and avoid loops
		if _, ok := visited[t]; ok {
			return nil;
		}
		visited[t] = true;

		// Implicit dereference
		deref := false;
		if ti, ok := t.(*PtrType); ok {
			deref = true;
			t = ti.Elem;
		}

		// If it's a named type, look for methods
		if ti, ok := t.(*NamedType); ok {
			method, ok := ti.methods[name];
			if ok {
				mark(depth, pathName + "." + name);
				log.Crash("Methods not implemented");
			}
			t = ti.def;
		}

		// If it's a struct type, check fields and embedded types
		var builder func(*exprCompiler);
		if t, ok := t.(*StructType); ok {
			for i, f := range t.Elems {
				var this *exprCompiler;
				var sub func(*exprCompiler);
				switch {
				case f.Name == name:
					mark(depth, pathName + "." + name);
					this = a;
					sub = func(*exprCompiler) {};

				case f.Anonymous:
					sub = find(f.Type, depth+1, pathName + "." + f.Name);
					if sub == nil {
						continue;
					}
					this = a.copy();

				default:
					continue;
				}

				// We found something.  Create a
				// builder for accessing this field.
				ft := f.Type;
				index := i;
				builder = func(parent *exprCompiler) {
					this.t = ft;
					var evalAddr func(f *Frame) Value;
					if deref {
						pf := parent.asPtr();
						evalAddr = func(f *Frame) Value {
							return pf(f).(StructValue).Field(index);
						};
					} else {
						pf := parent.asStruct();
						evalAddr = func(f *Frame) Value {
							return pf(f).Field(index);
						};
					}
					this.genValue(evalAddr);
					sub(this);
				};
			}
		}

		return builder;
	};

	builder := find(v.t, 0, "");
	if builder == nil {
		a.diag("type %v has no field or method %s", v.t, name);
		return;
	}
	if ambig {
		a.diag("field %s is ambiguous in type %v%s", name, v.t, amberr);
		return;
	}

	a.desc = "selector expression";
	builder(v);
}

func (a *exprCompiler) DoIndexExpr(x *ast.IndexExpr) {
	l, r := a.copyVisit(x.X), a.copyVisit(x.Index);
	if l.t == nil || r.t == nil {
		return;
	}

	// Type check object
	if lt, ok := l.t.lit().(*PtrType); ok {
		if et, ok := lt.Elem.lit().(*ArrayType); ok {
			// Automatic dereference
			nl := l.copy();
			nl.t = et;
			nl.genStarOp(l);
			l = nl;
		}
	}

	var at Type;
	intIndex := false;
	var maxIndex int64 = -1;

	switch lt := l.t.lit().(type) {
	case *ArrayType:
		at = lt.Elem;
		intIndex = true;
		maxIndex = lt.Len;

	// TODO(austin) Uncomment when there is a SliceType
	// case *SliceType:
	// 	a.t = lt.Elem;
	// 	intIndex = true;

	case *stringType:
		at = Uint8Type;
		intIndex = true;

	// TODO(austin) Uncomment when there is a MapType
	// case *MapType:
	// 	log.Crash("Index into map not implemented");

	default:
		a.diag("cannot index into %v", l.t);
		return;
	}

	// Type check index and convert to int if necessary
	if intIndex {
		// XXX(Spec) It's unclear if ideal floats with no
		// fractional part are allowed here.  6g allows it.  I
		// believe that's wrong.
		switch _ := r.t.lit().(type) {
		case *idealIntType:
			val := r.asIdealInt()();
			if val.IsNeg() || (maxIndex != -1 && val.Cmp(bignum.Int(maxIndex)) >= 0) {
				a.diag("array index out of bounds");
				return;
			}
			r = r.convertTo(IntType);
			if r == nil {
				return;
			}

		case *uintType:
			// Convert to int
			nr := r.copy();
			nr.t = IntType;
			rf := r.asUint();
			nr.evalInt = func(f *Frame) int64 {
				return int64(rf(f));
			};
			r = nr;

		case *intType:
			// Good as is

		default:
			a.diag("illegal operand type for index\n\t%v", r.t);
			return;
		}
	}

	a.t = at;

	// Compile
	switch lt := l.t.lit().(type) {
	case *ArrayType:
		a.t = lt.Elem;
		// TODO(austin) Bounds check
		a.genIndexArray(l, r);
		lf := l.asArray();
		rf := r.asInt();
		a.evalAddr = func(f *Frame) Value {
			return lf(f).Elem(rf(f));
		};

	case *stringType:
		// TODO(austin) Bounds check
		lf := l.asString();
		rf := r.asInt();
		// TODO(austin) This pulls over the whole string in a
		// remote setting, instead of just the one character.
		a.evalUint = func(f *Frame) uint64 {
			return uint64(lf(f)[rf(f)]);
		}

	default:
		log.Crashf("Compilation of index into %T not implemented", l.t);
	}
}

func (a *exprCompiler) DoTypeAssertExpr(x *ast.TypeAssertExpr) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoCallExpr(x *ast.CallExpr) {
	// TODO(austin) Type conversions look like calls, but will
	// fail in DoIdent right now.
	//
	// TODO(austin) Magic built-in functions
	//
	// TODO(austin) Variadic functions.

	// Compile children
	bad := false;
	l := a.copyVisit(x.Fun);
	if l.t == nil {
		bad = true;
	}
	as := make([]*exprCompiler, len(x.Args));
	for i := 0; i < len(x.Args); i++ {
		as[i] = a.copyVisit(x.Args[i]);
		if as[i].t == nil {
			bad = true;
		}
	}
	if bad {
		return;
	}

	// Type check
	if a.constant {
		a.diag("function call in constant context");
		return;
	}

	// XXX(Spec) Calling a named function type is okay.  I really
	// think there needs to be a general discussion of named
	// types.  A named type creates a new, distinct type, but the
	// type of that type is still whatever it's defined to.  Thus,
	// in "type Foo int", Foo is still an integer type and in
	// "type Foo func()", Foo is a function type.
	lt, ok := l.t.lit().(*FuncType);
	if !ok {
		a.diag("cannot call non-function type %v", l.t);
		return;
	}

	// The arguments must be single-valued expressions assignment
	// compatible with the parameters of F.
	//
	// XXX(Spec) The spec is wrong.  It can also be a single
	// multi-valued expression.
	nin := len(lt.In);
	assign := a.compileAssign(x.Pos(), NewMultiType(lt.In), as, "function call", "argument");
	if assign == nil {
		return;
	}

	nout := len(lt.Out);
	switch nout {
	case 0:
		a.t = EmptyType;
	case 1:
		a.t = lt.Out[0];
	default:
		a.t = NewMultiType(lt.Out);
	}

	// Gather argument and out types to initialize frame variables
	vts := make([]Type, nin + nout);
	for i, t := range lt.In {
		vts[i] = t;
	}
	for i, t := range lt.Out {
		vts[i+nin] = t;
	}

	// Compile
	lf := l.asFunc();
	call := func(f *Frame) []Value {
		fun := lf(f);
		fr := fun.NewFrame();
		for i, t := range vts {
			fr.Vars[i] = t.Zero();
		}
		assign(multiV(fr.Vars[0:nin]), f);
		fun.Call(fr);
		return fr.Vars[nin:nin+nout];
	};
	a.genFuncCall(call);
}

func (a *exprCompiler) DoStarExpr(x *ast.StarExpr) {
	v := a.copyVisit(x.X);
	if v.t == nil {
		return;
	}

	switch vt := v.t.lit().(type) {
	case *PtrType:
		a.t = vt.Elem;
		// TODO(austin) Deal with nil pointers
		a.genStarOp(v);
		a.desc = "indirect expression";

	default:
		a.diagOpType(token.MUL, v.t);
	}
}

func (a *exprCompiler) genUnaryAddrOf(v *exprCompiler) {
	vf := v.evalAddr;
	a.evalPtr = func(f *Frame) Value { return vf(f) };
}

var unaryOpDescs = make(map[token.Token] string)

func (a *exprCompiler) DoUnaryExpr(x *ast.UnaryExpr) {
	v := a.copyVisit(x.X);
	if v.t == nil {
		return;
	}

	// Type check
	switch x.Op {
	case token.ADD, token.SUB:
		if !v.t.isInteger() && !v.t.isFloat() {
			a.diagOpType(x.Op, v.t);
			return;
		}
		a.t = v.t;

	case token.NOT:
		if !v.t.isBoolean() {
			a.diagOpType(x.Op, v.t);
			return;
		}
		a.t = BoolType;

	case token.XOR:
		if !v.t.isInteger() {
			a.diagOpType(x.Op, v.t);
			return;
		}
		a.t = v.t;

	case token.AND:
		// The unary prefix address-of operator & generates
		// the address of its operand, which must be a
		// variable, pointer indirection, field selector, or
		// array or slice indexing operation.
		if v.evalAddr == nil {
			a.diag("cannot take the address of %s", v.desc);
			return;
		}

		// TODO(austin) Implement "It is illegal to take the
		// address of a function result variable" once I have
		// function result variables.

		a.t = NewPtrType(v.t);

	case token.ARROW:
		log.Crashf("Unary op %v not implemented", x.Op);

	default:
		log.Crashf("unknown unary operator %v", x.Op);
	}

	var ok bool;
	a.desc, ok = unaryOpDescs[x.Op];
 	if !ok {
		a.desc = "unary " + x.Op.String() + " expression";
		unaryOpDescs[x.Op] = a.desc;
	}

	// Compile
	switch x.Op {
	case token.ADD:
		// Just compile it out
		*a = *v;

	case token.SUB:
		a.genUnaryOpNeg(v);

	case token.NOT:
		a.genUnaryOpNot(v);

	case token.XOR:
		a.genUnaryOpXor(v);

	case token.AND:
		a.genUnaryAddrOf(v);

	default:
		log.Crashf("Compilation of unary op %v not implemented", x.Op);
	}
}

var binOpDescs = make(map[token.Token] string)

func (a *exprCompiler) doBinaryExpr(op token.Token, l, r *exprCompiler) {
	// Save the original types of l.t and r.t for error messages.
	origlt := l.t;
	origrt := r.t;

	// XXX(Spec) What is the exact definition of a "named type"?

	// XXX(Spec) Arithmetic operators: "Integer types" apparently
	// means all types compatible with basic integer types, though
	// this is never explained.  Likewise for float types, etc.
	// This relates to the missing explanation of named types.

	// XXX(Spec) Operators: "If both operands are ideal numbers,
	// the conversion is to ideal floats if one of the operands is
	// an ideal float (relevant for / and %)."  How is that
	// relevant only for / and %?  If I add an ideal int and an
	// ideal float, I get an ideal float.

	if op != token.SHL && op != token.SHR {
		// Except in shift expressions, if one operand has
		// numeric type and the other operand is an ideal
		// number, the ideal number is converted to match the
		// type of the other operand.
		if (l.t.isInteger() || l.t.isFloat()) && !l.t.isIdeal() && r.t.isIdeal() {
			r = r.convertTo(l.t);
		} else if (r.t.isInteger() || r.t.isFloat()) && !r.t.isIdeal() && l.t.isIdeal() {
			l = l.convertTo(r.t);
		}
		if l == nil || r == nil {
			return;
		}

		// Except in shift expressions, if both operands are
		// ideal numbers and one is an ideal float, the other
		// is converted to ideal float.
		if l.t.isIdeal() && r.t.isIdeal() {
			if l.t.isInteger() && r.t.isFloat() {
				l = l.convertTo(r.t);
			} else if l.t.isFloat() && r.t.isInteger() {
				r = r.convertTo(l.t);
			}
			if l == nil || r == nil {
				return;
			}
		}
	}

	// Useful type predicates
	compat := func() bool {
		return l.t.compat(r.t, false);
	};
	integers := func() bool {
		return l.t.isInteger() && r.t.isInteger();
	};
	floats := func() bool {
		return l.t.isFloat() && r.t.isFloat();
	};
	strings := func() bool {
		// TODO(austin) Deal with named types
		return l.t == StringType && r.t == StringType;
	};
	booleans := func() bool {
		return l.t.isBoolean() && r.t.isBoolean();
	};

	// Type check
	switch op {
	case token.ADD:
		if !compat() || (!integers() && !floats() && !strings()) {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}
		a.t = l.t;

	case token.SUB, token.MUL, token.QUO:
		if !compat() || (!integers() && !floats()) {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}
		a.t = l.t;

	case token.REM, token.AND, token.OR, token.XOR, token.AND_NOT:
		if !compat() || !integers() {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}
		a.t = l.t;

	case token.SHL, token.SHR:
		// XXX(Spec) Is it okay for the right operand to be an
		// ideal float with no fractional part?  "The right
		// operand in a shift operation must be always be of
		// unsigned integer type or an ideal number that can
		// be safely converted into an unsigned integer type
		// (§Arithmetic operators)" suggests so and 6g agrees.

		if !l.t.isInteger() || !(r.t.isInteger() || r.t.isIdeal()) {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}

		// The right operand in a shift operation must be
		// always be of unsigned integer type or an ideal
		// number that can be safely converted into an
		// unsigned integer type.
		if r.t.isIdeal() {
			r2 := r.convertTo(UintType);
			if r2 == nil {
				return;
			}

			// If the left operand is not ideal, convert
			// the right to not ideal.
			if !l.t.isIdeal() {
				r = r2;
			}

			// If both are ideal, but the right side isn't
			// an ideal int, convert it to simplify things.
			if l.t.isIdeal() && !r.t.isInteger() {
				r = r.convertTo(IdealIntType);
				if r == nil {
					log.Crashf("conversion to uintType succeeded, but conversion to idealIntType failed");
				}
			}
		} else if _, ok := r.t.lit().(*uintType); !ok {
			a.diag("right operand of shift must be unsigned");
			return;
		}

		if l.t.isIdeal() && !r.t.isIdeal() {
			// XXX(Spec) What is the meaning of "ideal >>
			// non-ideal"?  Russ says the ideal should be
			// converted to an int.  6g propagates the
			// type down from assignments as a hint.

			l = l.convertTo(IntType);
			if l == nil {
				return;
			}
		}

		// At this point, we should have one of three cases:
		// 1) uint SHIFT uint
		// 2) int SHIFT uint
		// 3) ideal int SHIFT ideal int

		a.t = l.t;

	case token.LOR, token.LAND:
		if !booleans() {
			return;
		}
		// XXX(Spec) There's no mention of *which* boolean
		// type the logical operators return.  From poking at
		// 6g, it appears to be the named boolean type, NOT
		// the type of the left operand, and NOT an unnamed
		// boolean type.

		a.t = BoolType;

	case token.ARROW:
		// The operands in channel sends differ in type: one
		// is always a channel and the other is a variable or
		// value of the channel's element type.
		log.Crash("Binary op <- not implemented");
		a.t = BoolType;

	case token.LSS, token.GTR, token.LEQ, token.GEQ:
		// XXX(Spec) It's really unclear what types which
		// comparison operators apply to.  I feel like the
		// text is trying to paint a Venn diagram for me,
		// which it's really pretty simple: <, <=, >, >= apply
		// only to numeric types and strings.  == and != apply
		// to everything except arrays and structs, and there
		// are some restrictions on when it applies to slices.

		if !compat() || (!integers() && !floats() && !strings()) {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}
		a.t = BoolType;

	case token.EQL, token.NEQ:
		// XXX(Spec) The rules for type checking comparison
		// operators are spread across three places that all
		// partially overlap with each other: the Comparison
		// Compatibility section, the Operators section, and
		// the Comparison Operators section.  The Operators
		// section should just say that operators require
		// identical types (as it does currently) except that
		// there a few special cases for comparison, which are
		// described in section X.  Currently it includes just
		// one of the four special cases.  The Comparison
		// Compatibility section and the Comparison Operators
		// section should either be merged, or at least the
		// Comparison Compatibility section should be
		// exclusively about type checking and the Comparison
		// Operators section should be exclusively about
		// semantics.

		// XXX(Spec) Comparison operators: "All comparison
		// operators apply to basic types except bools."  This
		// is very difficult to parse.  It's explained much
		// better in the Comparison Compatibility section.

		// XXX(Spec) Comparison compatibility: "Function
		// values are equal if they refer to the same
		// function." is rather vague.  It should probably be
		// similar to the way the rule for map values is
		// written: Function values are equal if they were
		// created by the same execution of a function literal
		// or refer to the same function declaration.  This is
		// *almost* but not quite waht 6g implements.  If a
		// function literals does not capture any variables,
		// then multiple executions of it will result in the
		// same closure.  Russ says he'll change that.

		// TODO(austin) Deal with remaining special cases

		if !compat() {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}
		// Arrays and structs may not be compared to anything.
		// TODO(austin) Use a multi-type switch
		if _, ok := l.t.(*ArrayType); ok {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}
		if _, ok := l.t.(*StructType); ok {
			a.diagOpTypes(op, origlt, origrt);
			return;
		}
		a.t = BoolType;

	default:
		log.Crashf("unknown binary operator %v", op);
	}

	var ok bool;
	a.desc, ok = binOpDescs[op];
	if !ok {
		a.desc = op.String() + " expression";
		binOpDescs[op] = a.desc;
	}

	// Compile
	switch op {
	case token.ADD:
		a.genBinOpAdd(l, r);

	case token.SUB:
		a.genBinOpSub(l, r);

	case token.MUL:
		a.genBinOpMul(l, r);

	case token.QUO:
		// TODO(austin) What if divisor is zero?
		// TODO(austin) Clear higher bits that may have
		// accumulated in our temporary.
		a.genBinOpQuo(l, r);

	case token.REM:
		// TODO(austin) What if divisor is zero?
		// TODO(austin) Clear higher bits that may have
		// accumulated in our temporary.
		a.genBinOpRem(l, r);

	case token.AND:
		a.genBinOpAnd(l, r);

	case token.OR:
		a.genBinOpOr(l, r);

	case token.XOR:
		a.genBinOpXor(l, r);

	case token.AND_NOT:
		a.genBinOpAndNot(l, r);

	case token.SHL:
		if l.t.isIdeal() {
			lv := l.asIdealInt()();
			rv := r.asIdealInt()();
			const maxShift = 99999;
			if rv.Cmp(bignum.Int(maxShift)) > 0 {
				a.diag("left shift by %v; exceeds implementation limit of %v", rv, maxShift);
				a.t = nil;
				return;
			}
			val := lv.Shl(uint(rv.Value()));
			a.evalIdealInt = func() *bignum.Integer { return val };
		} else {
			a.genBinOpShl(l, r);
		}

	case token.SHR:
		if l.t.isIdeal() {
			lv := l.asIdealInt()();
			rv := r.asIdealInt()();
			val := lv.Shr(uint(rv.Value()));
			a.evalIdealInt = func() *bignum.Integer { return val };
		} else {
			a.genBinOpShr(l, r);
		}

	case token.LSS:
		a.genBinOpLss(l, r);

	case token.GTR:
		a.genBinOpGtr(l, r);

	case token.LEQ:
		a.genBinOpLeq(l, r);

	case token.GEQ:
		a.genBinOpGeq(l, r);

	case token.EQL:
		a.genBinOpEql(l, r);

	case token.NEQ:
		a.genBinOpNeq(l, r);

	default:
		log.Crashf("Compilation of binary op %v not implemented", op);
	}
}

func (a *exprCompiler) DoBinaryExpr(x *ast.BinaryExpr) {
	l, r := a.copyVisit(x.X), a.copyVisit(x.Y);
	if l.t == nil || r.t == nil {
		return;
	}

	a.doBinaryExpr(x.Op, l, r);
}

func (a *exprCompiler) DoKeyValueExpr(x *ast.KeyValueExpr) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoEllipsis(x *ast.Ellipsis) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoArrayType(x *ast.ArrayType) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoStructType(x *ast.StructType) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoFuncType(x *ast.FuncType) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoInterfaceType(x *ast.InterfaceType) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoMapType(x *ast.MapType) {
	log.Crash("Not implemented");
}

func (a *exprCompiler) DoChanType(x *ast.ChanType) {
	log.Crash("Not implemented");
}

// TODO(austin) This is a hack to eliminate a circular dependency
// between type.go and expr.go
func (a *compiler) compileArrayLen(b *block, expr ast.Expr) (int64, bool) {
	lenExpr := a.compileExpr(b, expr, true);
	if lenExpr == nil {
		return 0, false;
	}

	// XXX(Spec) Are ideal floats with no fractional part okay?
	if lenExpr.t.isIdeal() {
		lenExpr = lenExpr.convertTo(IntType);
		if lenExpr == nil {
			return 0, false;
		}
	}

	if !lenExpr.t.isInteger() {
		a.diagAt(expr, "array size must be an integer");
		return 0, false;
	}

	switch _ := lenExpr.t.lit().(type) {
	case *intType:
		return lenExpr.evalInt(nil), true;
	case *uintType:
		return int64(lenExpr.evalUint(nil)), true;
	}
	log.Crashf("unexpected integer type %T", lenExpr.t);
	return 0, false;
}

func (a *compiler) compileExpr(b *block, expr ast.Expr, constant bool) *exprCompiler {
	ec := newExprCompiler(&exprContext{a, b, constant}, expr.Pos());
	expr.Visit(ec);
	if ec.t == nil {
		return nil;
	}
	return ec;
}

// extractEffect separates out any effects that the expression may
// have, returning a function that will perform those effects and a
// new exprCompiler that is guaranteed to be side-effect free.  These
// are the moral equivalents of "temp := &expr" and "*temp".  Because
// this creates a temporary variable, the caller should create a
// temporary block for the compilation of this expression and the
// evaluation of the results.
//
// Implementation limit: The expression must be addressable.
func (a *exprCompiler) extractEffect() (func(f *Frame), *exprCompiler) {
	if a.evalAddr == nil {
		// This is a much easier case, but the code is
		// completely different.
		log.Crash("extractEffect only implemented for addressable expressions");
	}

	// Create temporary
	tempBlock := a.block;
	tempType := NewPtrType(a.t);
	temp := tempBlock.DefineSlot(tempType);
	tempIdx := temp.Index;

	// Generate "temp := &e"
	addr := a.copy();
	addr.t = tempType;
	addr.genUnaryAddrOf(a);

	assign := a.compileAssign(a.pos, tempType, []*exprCompiler{addr}, "", "");
	if assign == nil {
		log.Crashf("compileAssign type check failed");
	}

	effect := func(f *Frame) {
		tempVal := tempType.Zero();
		f.Vars[tempIdx] = tempVal;
		assign(tempVal, f);
	};

	// Generate "*temp"
	getTemp := a.copy();
	getTemp.t = tempType;
	getTemp.genIdentOp(0, tempIdx);

	deref := a.copy();
	deref.t = a.t;
	deref.genStarOp(getTemp);

	return effect, deref;
}

/*
 * Testing interface
 */

type Expr struct {
	t Type;
	f func(f *Frame, out Value);
}

func (expr *Expr) Eval(f *Frame) Value {
	v := expr.t.Zero();
	expr.f(f, v);
	return v;
}

func CompileExpr(scope *Scope, expr ast.Expr) (*Expr, os.Error) {
	errors := scanner.NewErrorVector();
	cc := &compiler{errors};

	ec := cc.compileExpr(scope.block, expr, false);
	if ec == nil {
		return nil, errors.GetError(scanner.Sorted);
	}
	switch t := ec.t.lit().(type) {
	case *boolType:
		return &Expr{t, func(f *Frame, out Value) { out.(BoolValue).Set(ec.evalBool(f)) }}, nil;
	case *uintType:
		return &Expr{t, func(f *Frame, out Value) { out.(UintValue).Set(ec.evalUint(f)) }}, nil;
	case *intType:
		return &Expr{t, func(f *Frame, out Value) { out.(IntValue).Set(ec.evalInt(f)) }}, nil;
	case *idealIntType:
		return &Expr{t, func(f *Frame, out Value) { out.(*idealIntV).V = ec.evalIdealInt() }}, nil;
	case *floatType:
		return &Expr{t, func(f *Frame, out Value) { out.(FloatValue).Set(ec.evalFloat(f)) }}, nil;
	case *idealFloatType:
		return &Expr{t, func(f *Frame, out Value) { out.(*idealFloatV).V = ec.evalIdealFloat() }}, nil;
	case *stringType:
		return &Expr{t, func(f *Frame, out Value) { out.(StringValue).Set(ec.evalString(f)) }}, nil;
	case *PtrType:
		return &Expr{t, func(f *Frame, out Value) { out.(PtrValue).Set(ec.evalPtr(f)) }}, nil;
	case *FuncType:
		return &Expr{t, func(f *Frame, out Value) { out.(FuncValue).Set(ec.evalFunc(f)) }}, nil;
	}
	log.Crashf("unexpected type %v", ec.t);
	panic();
}

/*
 * Operator generators
 * Everything below here is MACHINE GENERATED by gen.py genOps
 */

func (a *exprCompiler) genConstant(v Value) {
	switch _ := a.t.lit().(type) {
	case *boolType:
		val := v.(BoolValue).Get();
		a.evalBool = func(f *Frame) bool { return val };
	case *uintType:
		val := v.(UintValue).Get();
		a.evalUint = func(f *Frame) uint64 { return val };
	case *intType:
		val := v.(IntValue).Get();
		a.evalInt = func(f *Frame) int64 { return val };
	case *idealIntType:
		val := v.(IdealIntValue).Get();
		a.evalIdealInt = func() *bignum.Integer { return val };
	case *floatType:
		val := v.(FloatValue).Get();
		a.evalFloat = func(f *Frame) float64 { return val };
	case *idealFloatType:
		val := v.(IdealFloatValue).Get();
		a.evalIdealFloat = func() *bignum.Rational { return val };
	case *stringType:
		val := v.(StringValue).Get();
		a.evalString = func(f *Frame) string { return val };
	case *ArrayType:
		val := v.(ArrayValue).Get();
		a.evalArray = func(f *Frame) ArrayValue { return val };
	case *StructType:
		val := v.(StructValue).Get();
		a.evalStruct = func(f *Frame) StructValue { return val };
	case *PtrType:
		val := v.(PtrValue).Get();
		a.evalPtr = func(f *Frame) Value { return val };
	case *FuncType:
		val := v.(FuncValue).Get();
		a.evalFunc = func(f *Frame) Func { return val };
	default:
		log.Crashf("unexpected constant type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genIdentOp(level int, index int) {
	a.evalAddr = func(f *Frame) Value { return f.Get(level, index) };
	switch _ := a.t.lit().(type) {
	case *boolType:
		a.evalBool = func(f *Frame) bool { return f.Get(level, index).(BoolValue).Get() };
	case *uintType:
		a.evalUint = func(f *Frame) uint64 { return f.Get(level, index).(UintValue).Get() };
	case *intType:
		a.evalInt = func(f *Frame) int64 { return f.Get(level, index).(IntValue).Get() };
	case *floatType:
		a.evalFloat = func(f *Frame) float64 { return f.Get(level, index).(FloatValue).Get() };
	case *stringType:
		a.evalString = func(f *Frame) string { return f.Get(level, index).(StringValue).Get() };
	case *ArrayType:
		a.evalArray = func(f *Frame) ArrayValue { return f.Get(level, index).(ArrayValue).Get() };
	case *StructType:
		a.evalStruct = func(f *Frame) StructValue { return f.Get(level, index).(StructValue).Get() };
	case *PtrType:
		a.evalPtr = func(f *Frame) Value { return f.Get(level, index).(PtrValue).Get() };
	case *FuncType:
		a.evalFunc = func(f *Frame) Func { return f.Get(level, index).(FuncValue).Get() };
	default:
		log.Crashf("unexpected identifier type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genIndexArray(l *exprCompiler, r *exprCompiler) {
	lf := l.asArray();
	rf := r.asInt();
	switch _ := a.t.lit().(type) {
	case *boolType:
		a.evalBool = func(f *Frame) bool { return lf(f).Elem(rf(f)).(BoolValue).Get() };
	case *uintType:
		a.evalUint = func(f *Frame) uint64 { return lf(f).Elem(rf(f)).(UintValue).Get() };
	case *intType:
		a.evalInt = func(f *Frame) int64 { return lf(f).Elem(rf(f)).(IntValue).Get() };
	case *floatType:
		a.evalFloat = func(f *Frame) float64 { return lf(f).Elem(rf(f)).(FloatValue).Get() };
	case *stringType:
		a.evalString = func(f *Frame) string { return lf(f).Elem(rf(f)).(StringValue).Get() };
	case *ArrayType:
		a.evalArray = func(f *Frame) ArrayValue { return lf(f).Elem(rf(f)).(ArrayValue).Get() };
	case *StructType:
		a.evalStruct = func(f *Frame) StructValue { return lf(f).Elem(rf(f)).(StructValue).Get() };
	case *PtrType:
		a.evalPtr = func(f *Frame) Value { return lf(f).Elem(rf(f)).(PtrValue).Get() };
	case *FuncType:
		a.evalFunc = func(f *Frame) Func { return lf(f).Elem(rf(f)).(FuncValue).Get() };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genFuncCall(call func(f *Frame) []Value) {
	a.exec = func(f *Frame) { call(f) };
	switch _ := a.t.lit().(type) {
	case *boolType:
		a.evalBool = func(f *Frame) bool { return call(f)[0].(BoolValue).Get() };
	case *uintType:
		a.evalUint = func(f *Frame) uint64 { return call(f)[0].(UintValue).Get() };
	case *intType:
		a.evalInt = func(f *Frame) int64 { return call(f)[0].(IntValue).Get() };
	case *floatType:
		a.evalFloat = func(f *Frame) float64 { return call(f)[0].(FloatValue).Get() };
	case *stringType:
		a.evalString = func(f *Frame) string { return call(f)[0].(StringValue).Get() };
	case *ArrayType:
		a.evalArray = func(f *Frame) ArrayValue { return call(f)[0].(ArrayValue).Get() };
	case *StructType:
		a.evalStruct = func(f *Frame) StructValue { return call(f)[0].(StructValue).Get() };
	case *PtrType:
		a.evalPtr = func(f *Frame) Value { return call(f)[0].(PtrValue).Get() };
	case *FuncType:
		a.evalFunc = func(f *Frame) Func { return call(f)[0].(FuncValue).Get() };
	case *MultiType:
		a.evalMulti = func(f *Frame) []Value { return call(f) };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genValue(vf func(*Frame) Value) {
	a.evalAddr = vf;
	switch _ := a.t.lit().(type) {
	case *boolType:
		a.evalBool = func(f *Frame) bool { return vf(f).(BoolValue).Get() };
	case *uintType:
		a.evalUint = func(f *Frame) uint64 { return vf(f).(UintValue).Get() };
	case *intType:
		a.evalInt = func(f *Frame) int64 { return vf(f).(IntValue).Get() };
	case *floatType:
		a.evalFloat = func(f *Frame) float64 { return vf(f).(FloatValue).Get() };
	case *stringType:
		a.evalString = func(f *Frame) string { return vf(f).(StringValue).Get() };
	case *ArrayType:
		a.evalArray = func(f *Frame) ArrayValue { return vf(f).(ArrayValue).Get() };
	case *StructType:
		a.evalStruct = func(f *Frame) StructValue { return vf(f).(StructValue).Get() };
	case *PtrType:
		a.evalPtr = func(f *Frame) Value { return vf(f).(PtrValue).Get() };
	case *FuncType:
		a.evalFunc = func(f *Frame) Func { return vf(f).(FuncValue).Get() };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genUnaryOpNeg(v *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		vf := v.asUint();
		a.evalUint = func(f *Frame) uint64 { return -vf(f) };
	case *intType:
		vf := v.asInt();
		a.evalInt = func(f *Frame) int64 { return -vf(f) };
	case *idealIntType:
		vf := v.asIdealInt();
		val := vf().Neg();
		a.evalIdealInt = func() *bignum.Integer { return val };
	case *floatType:
		vf := v.asFloat();
		a.evalFloat = func(f *Frame) float64 { return -vf(f) };
	case *idealFloatType:
		vf := v.asIdealFloat();
		val := vf().Neg();
		a.evalIdealFloat = func() *bignum.Rational { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genUnaryOpNot(v *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *boolType:
		vf := v.asBool();
		a.evalBool = func(f *Frame) bool { return !vf(f) };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genUnaryOpXor(v *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		vf := v.asUint();
		a.evalUint = func(f *Frame) uint64 { return ^vf(f) };
	case *intType:
		vf := v.asInt();
		a.evalInt = func(f *Frame) int64 { return ^vf(f) };
	case *idealIntType:
		vf := v.asIdealInt();
		val := vf().Neg().Sub(bignum.Int(1));
		a.evalIdealInt = func() *bignum.Integer { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpAdd(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) + rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) + rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Add(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalFloat = func(f *Frame) float64 { return lf(f) + rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Add(rf());
		a.evalIdealFloat = func() *bignum.Rational { return val };
	case *stringType:
		lf := l.asString();
		rf := r.asString();
		a.evalString = func(f *Frame) string { return lf(f) + rf(f) };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpSub(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) - rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) - rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Sub(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalFloat = func(f *Frame) float64 { return lf(f) - rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Sub(rf());
		a.evalIdealFloat = func() *bignum.Rational { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpMul(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) * rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) * rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Mul(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalFloat = func(f *Frame) float64 { return lf(f) * rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Mul(rf());
		a.evalIdealFloat = func() *bignum.Rational { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpQuo(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) / rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) / rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Quo(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalFloat = func(f *Frame) float64 { return lf(f) / rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Quo(rf());
		a.evalIdealFloat = func() *bignum.Rational { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpRem(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) % rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) % rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Rem(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpAnd(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) & rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) & rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().And(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpOr(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) | rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) | rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Or(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpXor(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) ^ rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) ^ rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Xor(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpAndNot(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) &^ rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalInt = func(f *Frame) int64 { return lf(f) &^ rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().AndNot(rf());
		a.evalIdealInt = func() *bignum.Integer { return val };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpShl(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) << rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asUint();
		a.evalInt = func(f *Frame) int64 { return lf(f) << rf(f) };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpShr(l *exprCompiler, r *exprCompiler) {
	switch _ := a.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalUint = func(f *Frame) uint64 { return lf(f) >> rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asUint();
		a.evalInt = func(f *Frame) int64 { return lf(f) >> rf(f) };
	default:
		log.Crashf("unexpected result type %v at %v", a.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpLss(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalBool = func(f *Frame) bool { return lf(f) < rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalBool = func(f *Frame) bool { return lf(f) < rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Cmp(rf()) < 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalBool = func(f *Frame) bool { return lf(f) < rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Cmp(rf()) < 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *stringType:
		lf := l.asString();
		rf := r.asString();
		a.evalBool = func(f *Frame) bool { return lf(f) < rf(f) };
	default:
		log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpGtr(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalBool = func(f *Frame) bool { return lf(f) > rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalBool = func(f *Frame) bool { return lf(f) > rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Cmp(rf()) > 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalBool = func(f *Frame) bool { return lf(f) > rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Cmp(rf()) > 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *stringType:
		lf := l.asString();
		rf := r.asString();
		a.evalBool = func(f *Frame) bool { return lf(f) > rf(f) };
	default:
		log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpLeq(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalBool = func(f *Frame) bool { return lf(f) <= rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalBool = func(f *Frame) bool { return lf(f) <= rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Cmp(rf()) <= 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalBool = func(f *Frame) bool { return lf(f) <= rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Cmp(rf()) <= 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *stringType:
		lf := l.asString();
		rf := r.asString();
		a.evalBool = func(f *Frame) bool { return lf(f) <= rf(f) };
	default:
		log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpGeq(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.lit().(type) {
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalBool = func(f *Frame) bool { return lf(f) >= rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalBool = func(f *Frame) bool { return lf(f) >= rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Cmp(rf()) >= 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalBool = func(f *Frame) bool { return lf(f) >= rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Cmp(rf()) >= 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *stringType:
		lf := l.asString();
		rf := r.asString();
		a.evalBool = func(f *Frame) bool { return lf(f) >= rf(f) };
	default:
		log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpEql(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.lit().(type) {
	case *boolType:
		lf := l.asBool();
		rf := r.asBool();
		a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Cmp(rf()) == 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Cmp(rf()) == 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *stringType:
		lf := l.asString();
		rf := r.asString();
		a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
	case *PtrType:
		lf := l.asPtr();
		rf := r.asPtr();
		a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
	case *FuncType:
		lf := l.asFunc();
		rf := r.asFunc();
		a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
	default:
		log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
	}
}

func (a *exprCompiler) genBinOpNeq(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.lit().(type) {
	case *boolType:
		lf := l.asBool();
		rf := r.asBool();
		a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
	case *uintType:
		lf := l.asUint();
		rf := r.asUint();
		a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
	case *intType:
		lf := l.asInt();
		rf := r.asInt();
		a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
	case *idealIntType:
		lf := l.asIdealInt();
		rf := r.asIdealInt();
		val := lf().Cmp(rf()) != 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *floatType:
		lf := l.asFloat();
		rf := r.asFloat();
		a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
	case *idealFloatType:
		lf := l.asIdealFloat();
		rf := r.asIdealFloat();
		val := lf().Cmp(rf()) != 0;
		a.evalBool = func(f *Frame) bool { return val };
	case *stringType:
		lf := l.asString();
		rf := r.asString();
		a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
	case *PtrType:
		lf := l.asPtr();
		rf := r.asPtr();
		a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
	case *FuncType:
		lf := l.asFunc();
		rf := r.asFunc();
		a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
	default:
		log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
	}
}

func genAssign(lt Type, r *exprCompiler) (func(lv Value, f *Frame)) {
	switch _ := lt.lit().(type) {
	case *boolType:
		rf := r.asBool();
		return func(lv Value, f *Frame) { lv.(BoolValue).Set(rf(f)) };
	case *uintType:
		rf := r.asUint();
		return func(lv Value, f *Frame) { lv.(UintValue).Set(rf(f)) };
	case *intType:
		rf := r.asInt();
		return func(lv Value, f *Frame) { lv.(IntValue).Set(rf(f)) };
	case *floatType:
		rf := r.asFloat();
		return func(lv Value, f *Frame) { lv.(FloatValue).Set(rf(f)) };
	case *stringType:
		rf := r.asString();
		return func(lv Value, f *Frame) { lv.(StringValue).Set(rf(f)) };
	case *ArrayType:
		rf := r.asArray();
		return func(lv Value, f *Frame) { lv.Assign(rf(f)) };
	case *PtrType:
		rf := r.asPtr();
		return func(lv Value, f *Frame) { lv.(PtrValue).Set(rf(f)) };
	case *FuncType:
		rf := r.asFunc();
		return func(lv Value, f *Frame) { lv.(FuncValue).Set(rf(f)) };
	default:
		log.Crashf("unexpected left operand type %v at %v", lt, r.pos);
	}
	panic();
}