72
|
1 module dmd.CmpExp;
|
|
2
|
|
3 import dmd.Expression;
|
|
4 import dmd.Identifier;
|
|
5 import dmd.backend.elem;
|
|
6 import dmd.InterState;
|
|
7 import dmd.Loc;
|
|
8 import dmd.TOK;
|
|
9 import dmd.Scope;
|
|
10 import dmd.IRState;
|
0
|
11 import dmd.Type;
|
|
12 import dmd.Id;
|
|
13 import dmd.TY;
|
|
14 import dmd.ErrorExp;
|
|
15 import dmd.IntegerExp;
|
|
16 import dmd.MATCH;
|
|
17 import dmd.BinExp;
|
|
18 import dmd.WANT;
|
|
19 import dmd.GlobalExpressions;
|
|
20
|
|
21 import dmd.expression.Util;
|
|
22 import dmd.codegen.Util;
|
|
23 import dmd.expression.Cmp;
|
|
24
|
|
25 import dmd.backend.Util;
|
|
26 import dmd.backend.RTLSYM;
|
|
27 import dmd.backend.TYM;
|
|
28 import dmd.backend.OPER;
|
72
|
29 import dmd.backend.rel;
|
|
30
|
0
|
31 class CmpExp : BinExp
|
|
32 {
|
|
33 this(TOK op, Loc loc, Expression e1, Expression e2)
|
72
|
34 {
|
0
|
35 super(loc, op, CmpExp.sizeof, e1, e2);
|
|
36 }
|
|
37
|
72
|
38 override Expression semantic(Scope sc)
|
0
|
39 {
|
|
40 Expression e;
|
|
41
|
|
42 version (LOGSEMANTIC) {
|
|
43 printf("CmpExp.semantic('%s')\n", toChars());
|
|
44 }
|
|
45 if (type)
|
|
46 return this;
|
|
47
|
|
48 BinExp.semanticp(sc);
|
|
49
|
79
|
50 Type t1 = e1.type.toBasetype();
|
|
51 Type t2 = e2.type.toBasetype();
|
|
52 if (t1.ty == Tclass && e2.op == TOKnull ||
|
|
53 t2.ty == Tclass && e1.op == TOKnull)
|
0
|
54 {
|
|
55 error("do not use null when comparing class types");
|
|
56 }
|
|
57
|
|
58 e = op_overload(sc);
|
|
59 if (e)
|
|
60 {
|
|
61 if (!e.type.isscalar() && e.type.equals(e1.type))
|
|
62 {
|
|
63 error("recursive opCmp expansion");
|
|
64 e = new ErrorExp();
|
|
65 }
|
|
66 else
|
|
67 {
|
|
68 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type.tint32));
|
|
69 e = e.semantic(sc);
|
|
70 }
|
|
71 return e;
|
|
72 }
|
|
73
|
79
|
74 // Disallow comparing T[]==T and T==T[]
|
|
75 if (e1.op == TOKslice && t1.ty == Tarray && e2.implicitConvTo(t1.nextOf()) ||
|
|
76 e2.op == TOKslice && t2.ty == Tarray && e1.implicitConvTo(t2.nextOf()))
|
|
77 {
|
|
78 incompatibleTypes();
|
|
79 return new ErrorExp();
|
|
80 }
|
|
81
|
0
|
82 typeCombine(sc);
|
|
83 type = Type.tboolean;
|
|
84
|
|
85 // Special handling for array comparisons
|
|
86 t1 = e1.type.toBasetype();
|
|
87 t2 = e2.type.toBasetype();
|
|
88 if ((t1.ty == Tarray || t1.ty == Tsarray || t1.ty == Tpointer) &&
|
|
89 (t2.ty == Tarray || t2.ty == Tsarray || t2.ty == Tpointer))
|
|
90 {
|
|
91 if (t1.nextOf().implicitConvTo(t2.nextOf()) < MATCHconst &&
|
|
92 t2.nextOf().implicitConvTo(t1.nextOf()) < MATCHconst &&
|
|
93 (t1.nextOf().ty != Tvoid && t2.nextOf().ty != Tvoid))
|
|
94 error("array comparison type mismatch, %s vs %s", t1.nextOf().toChars(), t2.nextOf().toChars());
|
|
95 e = this;
|
|
96 }
|
|
97 else if (t1.ty == Tstruct || t2.ty == Tstruct ||
|
|
98 (t1.ty == Tclass && t2.ty == Tclass))
|
|
99 {
|
|
100 if (t2.ty == Tstruct)
|
|
101 error("need member function opCmp() for %s %s to compare", t2.toDsymbol(sc).kind(), t2.toChars());
|
|
102 else
|
|
103 error("need member function opCmp() for %s %s to compare", t1.toDsymbol(sc).kind(), t1.toChars());
|
|
104 e = this;
|
|
105 }
|
|
106 /// static if (true) {
|
|
107 else if (t1.iscomplex() || t2.iscomplex())
|
|
108 {
|
|
109 error("compare not defined for complex operands");
|
|
110 e = new ErrorExp();
|
|
111 }
|
|
112 /// }
|
|
113 else
|
|
114 {
|
|
115 e1.rvalue();
|
|
116 e2.rvalue();
|
|
117 e = this;
|
|
118 }
|
|
119
|
|
120 //printf("CmpExp: %s, type = %s\n", e.toChars(), e.type.toChars());
|
|
121 return e;
|
|
122 }
|
|
123
|
72
|
124 override Expression optimize(int result)
|
0
|
125 {
|
|
126 Expression e;
|
|
127
|
|
128 //printf("CmpExp::optimize() %s\n", toChars());
|
|
129 e1 = e1.optimize(WANTvalue | (result & WANTinterpret));
|
|
130 e2 = e2.optimize(WANTvalue | (result & WANTinterpret));
|
|
131
|
|
132 Expression e1 = fromConstInitializer(result, this.e1);
|
|
133 Expression e2 = fromConstInitializer(result, this.e2);
|
|
134
|
|
135 e = Cmp(op, type, e1, e2);
|
|
136 if (e is EXP_CANT_INTERPRET)
|
|
137 e = this;
|
|
138 return e;
|
|
139 }
|
|
140
|
72
|
141 override Expression interpret(InterState istate)
|
0
|
142 {
|
|
143 assert(false);
|
|
144 }
|
|
145
|
72
|
146 override int isBit()
|
0
|
147 {
|
|
148 assert(false);
|
|
149 }
|
|
150
|
72
|
151 override bool isCommutative()
|
0
|
152 {
|
|
153 return true;
|
|
154 }
|
|
155
|
72
|
156 override Identifier opId()
|
0
|
157 {
|
|
158 return Id.cmp;
|
|
159 }
|
|
160
|
72
|
161 override elem* toElem(IRState* irs)
|
0
|
162 {
|
|
163 elem *e;
|
|
164 OPER eop;
|
|
165 Type t1 = e1.type.toBasetype();
|
|
166 Type t2 = e2.type.toBasetype();
|
|
167
|
|
168 switch (op)
|
|
169 {
|
|
170 case TOKlt: eop = OPlt; break;
|
|
171 case TOKgt: eop = OPgt; break;
|
|
172 case TOKle: eop = OPle; break;
|
|
173 case TOKge: eop = OPge; break;
|
|
174 case TOKequal: eop = OPeqeq; break;
|
|
175 case TOKnotequal: eop = OPne; break;
|
|
176
|
|
177 // NCEG floating point compares
|
|
178 case TOKunord: eop = OPunord; break;
|
|
179 case TOKlg: eop = OPlg; break;
|
|
180 case TOKleg: eop = OPleg; break;
|
|
181 case TOKule: eop = OPule; break;
|
|
182 case TOKul: eop = OPul; break;
|
|
183 case TOKuge: eop = OPuge; break;
|
|
184 case TOKug: eop = OPug; break;
|
|
185 case TOKue: eop = OPue; break;
|
|
186 default:
|
|
187 dump(0);
|
|
188 assert(0);
|
|
189 }
|
|
190 if (!t1.isfloating())
|
|
191 {
|
|
192 // Convert from floating point compare to equivalent
|
|
193 // integral compare
|
|
194 eop = cast(OPER)rel_integral(eop);
|
|
195 }
|
|
196 if (cast(int)eop > 1 && t1.ty == Tclass && t2.ty == Tclass)
|
|
197 {
|
|
198 static if (true) {
|
|
199 assert(0);
|
|
200 } else {
|
|
201 elem *ec1;
|
|
202 elem *ec2;
|
|
203
|
|
204 ec1 = e1.toElem(irs);
|
|
205 ec2 = e2.toElem(irs);
|
|
206 e = el_bin(OPcall,TYint,el_var(rtlsym[RTLSYM_OBJ_CMP]),el_param(ec1, ec2));
|
|
207 e = el_bin(eop, TYint, e, el_long(TYint, 0));
|
|
208 }
|
|
209 }
|
|
210 else if (cast(int)eop > 1 &&
|
|
211 (t1.ty == Tarray || t1.ty == Tsarray) &&
|
|
212 (t2.ty == Tarray || t2.ty == Tsarray))
|
|
213 {
|
|
214 elem* ea1;
|
|
215 elem* ea2;
|
|
216 elem* ep;
|
|
217 Type telement = t1.nextOf().toBasetype();
|
|
218 int rtlfunc;
|
|
219
|
|
220 ea1 = e1.toElem(irs);
|
|
221 ea1 = array_toDarray(t1, ea1);
|
|
222 ea2 = e2.toElem(irs);
|
|
223 ea2 = array_toDarray(t2, ea2);
|
|
224
|
|
225 version (DMDV2) {
|
|
226 ep = el_params(telement.arrayOf().getInternalTypeInfo(null).toElem(irs),
|
|
227 ea2, ea1, null);
|
|
228 rtlfunc = RTLSYM_ARRAYCMP2;
|
|
229 } else {
|
|
230 ep = el_params(telement.getInternalTypeInfo(null).toElem(irs), ea2, ea1, null);
|
|
231 rtlfunc = RTLSYM_ARRAYCMP;
|
|
232 }
|
|
233 e = el_bin(OPcall, TYint, el_var(rtlsym[rtlfunc]), ep);
|
|
234 e = el_bin(eop, TYint, e, el_long(TYint, 0));
|
|
235 el_setLoc(e,loc);
|
|
236 }
|
|
237 else
|
|
238 {
|
|
239 if (cast(int)eop <= 1)
|
|
240 {
|
|
241 /* The result is determinate, create:
|
|
242 * (e1 , e2) , eop
|
|
243 */
|
|
244 e = toElemBin(irs,OPcomma);
|
|
245 e = el_bin(OPcomma,e.Ety,e,el_long(e.Ety,cast(int)eop));
|
|
246 }
|
|
247 else
|
|
248 e = toElemBin(irs,eop);
|
|
249 }
|
|
250 return e;
|
|
251 }
|
|
252 }
|
|
253
|