Mercurial > projects > ddmd
annotate dmd/MinExp.d @ 115:6caaf0256da1
+ interpretation of (non-assign) binary expressions
+ BinExp.isunsigned
+ EqualExp.isBit
author | Trass3r |
---|---|
date | Thu, 02 Sep 2010 01:29:29 +0200 |
parents | e28b18c23469 |
children | 9e39c7de8438 |
rev | line source |
---|---|
72 | 1 module dmd.MinExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.TY; | |
6 import dmd.ErrorExp; | |
7 import dmd.Identifier; | |
8 import dmd.IntegerExp; | |
9 import dmd.DivExp; | |
10 import dmd.Type; | |
11 import dmd.InterState; | |
12 import dmd.OutBuffer; | |
13 import dmd.Loc; | |
14 import dmd.Scope; | |
15 import dmd.IRState; | |
16 import dmd.ArrayTypes; | |
17 import dmd.BinExp; | |
18 import dmd.TOK; | |
19 import dmd.Id; | |
20 import dmd.expression.Min; | |
21 | |
22 import dmd.backend.elem; | |
23 import dmd.backend.Util; | |
24 import dmd.backend.OPER; | |
25 | |
0 | 26 class MinExp : BinExp |
27 { | |
28 this(Loc loc, Expression e1, Expression e2) | |
29 { | |
30 super(loc, TOK.TOKmin, MinExp.sizeof, e1, e2); | |
31 } | |
32 | |
72 | 33 override Expression semantic(Scope sc) |
0 | 34 { |
72 | 35 Expression e; |
36 Type t1; | |
37 Type t2; | |
38 | |
39 version (LOGSEMANTIC) { | |
40 printf("MinExp.semantic('%s')\n", toChars()); | |
41 } | |
42 if (type) | |
43 return this; | |
44 | |
45 super.semanticp(sc); | |
46 | |
47 e = op_overload(sc); | |
48 if (e) | |
49 return e; | |
50 | |
51 e = this; | |
52 t1 = e1.type.toBasetype(); | |
53 t2 = e2.type.toBasetype(); | |
54 if (t1.ty == TY.Tpointer) | |
55 { | |
56 if (t2.ty == TY.Tpointer) | |
57 { // Need to divide the result by the stride | |
58 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
59 long stride; | |
60 Expression ee; | |
61 | |
62 typeCombine(sc); // make sure pointer types are compatible | |
63 type = Type.tptrdiff_t; | |
64 stride = t2.nextOf().size(); | |
65 if (stride == 0) | |
66 { | |
67 ee = new IntegerExp(loc, 0, Type.tptrdiff_t); | |
68 } | |
69 else | |
70 { | |
71 ee = new DivExp(loc, this, new IntegerExp(Loc(0), stride, Type.tptrdiff_t)); | |
72 ee.type = Type.tptrdiff_t; | |
73 } | |
74 return ee; | |
75 } | |
76 else if (t2.isintegral()) | |
77 e = scaleFactor(sc); | |
78 else | |
79 { | |
80 error("incompatible types for minus"); | |
81 return new ErrorExp(); | |
82 } | |
83 } | |
84 else if (t2.ty == TY.Tpointer) | |
85 { | |
86 type = e2.type; | |
87 error("can't subtract pointer from %s", e1.type.toChars()); | |
88 return new ErrorExp(); | |
89 } | |
90 else | |
91 { | |
92 typeCombine(sc); | |
93 t1 = e1.type.toBasetype(); | |
94 t2 = e2.type.toBasetype(); | |
95 if ((t1.isreal() && t2.isimaginary()) || | |
96 (t1.isimaginary() && t2.isreal())) | |
97 { | |
98 switch (type.ty) | |
99 { | |
100 case TY.Tfloat32: | |
101 case TY.Timaginary32: | |
102 type = Type.tcomplex32; | |
103 break; | |
104 | |
105 case TY.Tfloat64: | |
106 case TY.Timaginary64: | |
107 type = Type.tcomplex64; | |
108 break; | |
109 | |
110 case TY.Tfloat80: | |
111 case TY.Timaginary80: | |
112 type = Type.tcomplex80; | |
113 break; | |
114 | |
115 default: | |
116 assert(0); | |
117 } | |
118 } | |
119 } | |
0 | 120 return e; |
121 } | |
122 | |
72 | 123 override Expression optimize(int result) |
0 | 124 { |
72 | 125 Expression e; |
126 | |
127 e1 = e1.optimize(result); | |
128 e2 = e2.optimize(result); | |
129 if (e1.isConst() && e2.isConst()) | |
130 { | |
131 if (e2.op == TOK.TOKsymoff) | |
132 return this; | |
133 e = Min(type, e1, e2); | |
134 } | |
135 else | |
136 e = this; | |
137 | |
0 | 138 return e; |
139 } | |
140 | |
72 | 141 override Expression interpret(InterState istate) |
0 | 142 { |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
143 return interpretCommon(istate, &Min); |
0 | 144 } |
145 | |
72 | 146 override void buildArrayIdent(OutBuffer buf, Expressions arguments) |
0 | 147 { |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
148 Exp_buildArrayIdent(buf, arguments, "Min"); |
0 | 149 } |
150 | |
72 | 151 override Expression buildArrayLoop(Arguments fparams) |
0 | 152 { |
72 | 153 /* Evaluate assign expressions left to right |
154 */ | |
155 Expression ex1 = e1.buildArrayLoop(fparams); | |
156 Expression ex2 = e2.buildArrayLoop(fparams); | |
157 Expression e = new MinExp(Loc(0), ex1, ex2); | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
158 return e; |
0 | 159 } |
160 | |
72 | 161 override Identifier opId() |
0 | 162 { |
163 return Id.sub; | |
164 } | |
165 | |
72 | 166 override Identifier opId_r() |
0 | 167 { |
168 return Id.sub_r; | |
169 } | |
170 | |
72 | 171 override elem* toElem(IRState* irs) |
0 | 172 { |
72 | 173 Type tb1 = e1.type.toBasetype(); |
174 Type tb2 = e2.type.toBasetype(); | |
175 | |
176 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray)) | |
177 { | |
178 error("Array operation %s not implemented", toChars()); | |
179 return el_long(type.totym(), 0); // error recovery | |
180 } | |
181 | |
182 return toElemBin(irs, OPER.OPmin); | |
0 | 183 } |
184 } | |
185 |