summaryrefslogtreecommitdiff
path: root/usr/austin/eval/expr.go
blob: 6ab4ad46e4ca8667a71df4f0cad860e1f296a7b5 (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
// 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";
	"fmt";
	"go/ast";
	"go/scanner";
	"go/token";
	"log";
"os";
	"strconv";
	"strings";
)

// An exprContext stores information used throughout the compilation
// of an entire expression.
type exprContext struct {
	scope *Scope;
	constant bool;
	errors scanner.ErrorHandler;
}

// 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;
	evalIdealInt func() *bignum.Integer;
	evalFloat func(f *Frame) float64;
	evalIdealFloat func() *bignum.Rational;
	evalString func(f *Frame) string;
	evalPtr 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;
	// 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) genIdentOp(t Type, s *Scope, index int)
func (a *exprCompiler) genStarOp(v *exprCompiler)
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) fork(x ast.Expr) *exprCompiler {
	ec := newExprCompiler(a.exprContext, x.Pos());
	x.Visit(ec);
	return ec;
}

func (a *exprCompiler) diag(format string, args ...) {
	a.errors.Error(a.pos, fmt.Sprintf(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);
}

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) 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) DoBadExpr(x *ast.BadExpr) {
	// Do nothing.  Already reported by parser.
}

func (a *exprCompiler) DoIdent(x *ast.Ident) {
	def, dscope := a.scope.Lookup(x.Value);
	if def == nil {
		a.diag("%s: undefined", x.Value);
		return;
	}
	switch def := def.(type) {
	case *Constant:
		a.t = def.Type;
		switch _ := a.t.literal().(type) {
		case *idealIntType:
			val := def.Value.(IdealIntValue).Get();
			a.evalIdealInt = func() *bignum.Integer { return val; };
		case *idealFloatType:
			val := def.Value.(IdealFloatValue).Get();
			a.evalIdealFloat = func() *bignum.Rational { return val; };
		default:
			log.Crashf("unexpected constant type: %v", a.t);
		}
		a.desc = "constant";
	case *Variable:
		if a.constant {
			a.diag("expression must be a constant");
			return;
		}
		a.t = def.Type;
		defidx := def.Index;
		a.genIdentOp(def.Type, dscope, defidx);
		a.evalAddr = func(f *Frame) Value {
			return f.Get(dscope, 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) {
	log.Crash("Not implemented");
}

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) {
	log.Crash("Not implemented");
}

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

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

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

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

	switch vt := v.t.(type) {
	case *PtrType:
		a.t = vt.Elem();
		a.genStarOp(v);
		vf := v.asPtr();
		a.evalAddr = func(f *Frame) Value { return vf(f) };
		a.desc = "* expression";

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

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

func (a *exprCompiler) DoUnaryExpr(x *ast.UnaryExpr) {
	v := a.fork(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;
		}
		// TODO(austin) Unnamed bool?  Named bool?
		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:
		vf := v.evalAddr;
		a.evalPtr = func(f *Frame) Value { return vf(f) };

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

// 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.
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;

	// It is erroneous 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.(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 := newExprCompiler(a.exprContext, a.pos);
	res.t = t;
	res.desc = a.desc;
	switch t := t.(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;
}

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

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

	// 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 x.Op != token.SHL && x.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.compatible(r.t);
	};
	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 {
		// TODO(austin) Deal with named types
		return l.t == BoolType && r.t == BoolType;
	};

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

	case token.SUB, token.MUL, token.QUO:
		if !compat() || (!integers() && !floats()) {
			a.diagOpTypes(x.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(x.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(x.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.literal().(*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.

		// TODO(austin) Named bool 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:
		// ... booleans may be compared only for equality or
		// inequality.
		if l.t.literal() == BoolType || r.t.literal() == BoolType {
			a.diagOpTypes(x.Op, origlt, origrt);
			return;
		}

		fallthrough;
	case token.EQL, token.NEQ:
		// When comparing two operands of channel type, the
		// channel value types must be compatible but the
		// channel direction is ignored.

		// XXX(Spec) Operators: "When comparing two operands
		// of channel type, the channel value types must be
		// compatible but the channel direction is ignored."
		// By "compatible" this really means "comparison
		// compatible".  Really, the rules for type checking
		// comparison operators are entirely different from
		// other binary operators, but this just barely hints
		// at that.

		// XXX(Spec) Comparison operators: "All comparison
		// operators apply to basic types except bools."
		// "except bools" is really weird here, since this is
		// actually explained in the Comparison compatibility
		// section.
		log.Crashf("Binary op %v not implemented", x.Op);
		// TODO(austin) Unnamed bool?  Named bool?
		a.t = BoolType;

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

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

	// Compile
	switch x.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:
		if l.t.isIdeal() || r.t.isIdeal() {
			log.Crashf("&^ for ideals not implemented");
		}
		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);
		}

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

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");
}

func compileExpr(expr ast.Expr, scope *Scope, errors scanner.ErrorHandler) *exprCompiler {
	ec := newExprCompiler(&exprContext{scope, false, errors}, expr.Pos());
	expr.Visit(ec);
	if ec.t == nil {
		return nil;
	}
	return ec;
}

/*
 * Public interface
 */

type Expr struct {
	f func(f *Frame) Value;
}

func (expr *Expr) Eval(f *Frame) Value {
	return expr.f(f);
}

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

	ec := compileExpr(expr, scope, errors);
	if ec == nil {
		return nil, errors.GetError(scanner.Sorted);
	}
	// TODO(austin) This still uses Value as a generic container
	// and is the only user of the 'value' methods on each type.
	// Need to figure out a better way to do this.
	switch t := ec.t.(type) {
	case *boolType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalBool(f)) }}, nil;
	case *uintType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalUint(f)) }}, nil;
	case *intType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalInt(f)) }}, nil;
	case *idealIntType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalIdealInt()) }}, nil;
	case *floatType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalFloat(f)) }}, nil;
	case *idealFloatType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalIdealFloat()) }}, nil;
	case *stringType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalString(f)) }}, nil;
	case *PtrType:
		return &Expr{func(f *Frame) Value { return t.value(ec.evalPtr(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) genIdentOp(t Type, s *Scope, index int) {
	switch _ := t.literal().(type) {
	case *boolType:
		a.evalBool = func(f *Frame) bool { return f.Get(s, index).(BoolValue).Get() };
	case *uintType:
		a.evalUint = func(f *Frame) uint64 { return f.Get(s, index).(UintValue).Get() };
	case *intType:
		a.evalInt = func(f *Frame) int64 { return f.Get(s, index).(IntValue).Get() };
	case *floatType:
		a.evalFloat = func(f *Frame) float64 { return f.Get(s, index).(FloatValue).Get() };
	case *stringType:
		a.evalString = func(f *Frame) string { return f.Get(s, index).(StringValue).Get() };
	case *PtrType:
		a.evalPtr = func(f *Frame) Value { return f.Get(s, index).(PtrValue).Get() };
	default:
		log.Crashf("unexpected identifier type %v at %v", t.literal(), a.pos);
	}
}

func (a *exprCompiler) genStarOp(v *exprCompiler) {
	vf := v.asPtr();
	switch _ := v.t.literal().(*PtrType).Elem().literal().(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 *PtrType:
		a.evalPtr = func(f *Frame) Value { return vf(f).(PtrValue).Get() };
	default:
		log.Crashf("unexpected operand type %v at %v", v.t.literal().(*PtrType).Elem().literal(), a.pos);
	}
}

func (a *exprCompiler) genUnaryOpNeg(v *exprCompiler) {
	switch _ := v.t.literal().(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 operand type %v at %v", v.t.literal(), a.pos);
	}
}

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

func (a *exprCompiler) genUnaryOpXor(v *exprCompiler) {
	switch _ := v.t.literal().(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 operand type %v at %v", v.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpAdd(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpSub(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpMul(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpQuo(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpRem(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpAnd(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpOr(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpXor(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpAndNot(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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) };
	default:
		log.Crashf("unexpected left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpShl(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}

func (a *exprCompiler) genBinOpShr(l *exprCompiler, r *exprCompiler) {
	switch _ := l.t.literal().(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 left operand type %v at %v", l.t.literal(), a.pos);
	}
}