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 Type t1;
|
|
42 Type t2;
|
|
43
|
|
44 version (LOGSEMANTIC) {
|
|
45 printf("CmpExp.semantic('%s')\n", toChars());
|
|
46 }
|
|
47 if (type)
|
|
48 return this;
|
|
49
|
|
50 BinExp.semanticp(sc);
|
|
51
|
|
52 if (e1.type.toBasetype().ty == Tclass && e2.op == TOKnull ||
|
|
53 e2.type.toBasetype().ty == Tclass && e1.op == TOKnull)
|
|
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
|
|
74 typeCombine(sc);
|
|
75 type = Type.tboolean;
|
|
76
|
|
77 // Special handling for array comparisons
|
|
78 t1 = e1.type.toBasetype();
|
|
79 t2 = e2.type.toBasetype();
|
|
80 if ((t1.ty == Tarray || t1.ty == Tsarray || t1.ty == Tpointer) &&
|
|
81 (t2.ty == Tarray || t2.ty == Tsarray || t2.ty == Tpointer))
|
|
82 {
|
|
83 if (t1.nextOf().implicitConvTo(t2.nextOf()) < MATCHconst &&
|
|
84 t2.nextOf().implicitConvTo(t1.nextOf()) < MATCHconst &&
|
|
85 (t1.nextOf().ty != Tvoid && t2.nextOf().ty != Tvoid))
|
|
86 error("array comparison type mismatch, %s vs %s", t1.nextOf().toChars(), t2.nextOf().toChars());
|
|
87 e = this;
|
|
88 }
|
|
89 else if (t1.ty == Tstruct || t2.ty == Tstruct ||
|
|
90 (t1.ty == Tclass && t2.ty == Tclass))
|
|
91 {
|
|
92 if (t2.ty == Tstruct)
|
|
93 error("need member function opCmp() for %s %s to compare", t2.toDsymbol(sc).kind(), t2.toChars());
|
|
94 else
|
|
95 error("need member function opCmp() for %s %s to compare", t1.toDsymbol(sc).kind(), t1.toChars());
|
|
96 e = this;
|
|
97 }
|
|
98 /// static if (true) {
|
|
99 else if (t1.iscomplex() || t2.iscomplex())
|
|
100 {
|
|
101 error("compare not defined for complex operands");
|
|
102 e = new ErrorExp();
|
|
103 }
|
|
104 /// }
|
|
105 else
|
|
106 {
|
|
107 e1.rvalue();
|
|
108 e2.rvalue();
|
|
109 e = this;
|
|
110 }
|
|
111
|
|
112 //printf("CmpExp: %s, type = %s\n", e.toChars(), e.type.toChars());
|
|
113 return e;
|
|
114 }
|
|
115
|
72
|
116 override Expression optimize(int result)
|
0
|
117 {
|
|
118 Expression e;
|
|
119
|
|
120 //printf("CmpExp::optimize() %s\n", toChars());
|
|
121 e1 = e1.optimize(WANTvalue | (result & WANTinterpret));
|
|
122 e2 = e2.optimize(WANTvalue | (result & WANTinterpret));
|
|
123
|
|
124 Expression e1 = fromConstInitializer(result, this.e1);
|
|
125 Expression e2 = fromConstInitializer(result, this.e2);
|
|
126
|
|
127 e = Cmp(op, type, e1, e2);
|
|
128 if (e is EXP_CANT_INTERPRET)
|
|
129 e = this;
|
|
130 return e;
|
|
131 }
|
|
132
|
72
|
133 override Expression interpret(InterState istate)
|
0
|
134 {
|
|
135 assert(false);
|
|
136 }
|
|
137
|
72
|
138 override int isBit()
|
0
|
139 {
|
|
140 assert(false);
|
|
141 }
|
|
142
|
72
|
143 override bool isCommutative()
|
0
|
144 {
|
|
145 return true;
|
|
146 }
|
|
147
|
72
|
148 override Identifier opId()
|
0
|
149 {
|
|
150 return Id.cmp;
|
|
151 }
|
|
152
|
72
|
153 override elem* toElem(IRState* irs)
|
0
|
154 {
|
|
155 elem *e;
|
|
156 OPER eop;
|
|
157 Type t1 = e1.type.toBasetype();
|
|
158 Type t2 = e2.type.toBasetype();
|
|
159
|
|
160 switch (op)
|
|
161 {
|
|
162 case TOKlt: eop = OPlt; break;
|
|
163 case TOKgt: eop = OPgt; break;
|
|
164 case TOKle: eop = OPle; break;
|
|
165 case TOKge: eop = OPge; break;
|
|
166 case TOKequal: eop = OPeqeq; break;
|
|
167 case TOKnotequal: eop = OPne; break;
|
|
168
|
|
169 // NCEG floating point compares
|
|
170 case TOKunord: eop = OPunord; break;
|
|
171 case TOKlg: eop = OPlg; break;
|
|
172 case TOKleg: eop = OPleg; break;
|
|
173 case TOKule: eop = OPule; break;
|
|
174 case TOKul: eop = OPul; break;
|
|
175 case TOKuge: eop = OPuge; break;
|
|
176 case TOKug: eop = OPug; break;
|
|
177 case TOKue: eop = OPue; break;
|
|
178 default:
|
|
179 dump(0);
|
|
180 assert(0);
|
|
181 }
|
|
182 if (!t1.isfloating())
|
|
183 {
|
|
184 // Convert from floating point compare to equivalent
|
|
185 // integral compare
|
|
186 eop = cast(OPER)rel_integral(eop);
|
|
187 }
|
|
188 if (cast(int)eop > 1 && t1.ty == Tclass && t2.ty == Tclass)
|
|
189 {
|
|
190 static if (true) {
|
|
191 assert(0);
|
|
192 } else {
|
|
193 elem *ec1;
|
|
194 elem *ec2;
|
|
195
|
|
196 ec1 = e1.toElem(irs);
|
|
197 ec2 = e2.toElem(irs);
|
|
198 e = el_bin(OPcall,TYint,el_var(rtlsym[RTLSYM_OBJ_CMP]),el_param(ec1, ec2));
|
|
199 e = el_bin(eop, TYint, e, el_long(TYint, 0));
|
|
200 }
|
|
201 }
|
|
202 else if (cast(int)eop > 1 &&
|
|
203 (t1.ty == Tarray || t1.ty == Tsarray) &&
|
|
204 (t2.ty == Tarray || t2.ty == Tsarray))
|
|
205 {
|
|
206 elem* ea1;
|
|
207 elem* ea2;
|
|
208 elem* ep;
|
|
209 Type telement = t1.nextOf().toBasetype();
|
|
210 int rtlfunc;
|
|
211
|
|
212 ea1 = e1.toElem(irs);
|
|
213 ea1 = array_toDarray(t1, ea1);
|
|
214 ea2 = e2.toElem(irs);
|
|
215 ea2 = array_toDarray(t2, ea2);
|
|
216
|
|
217 version (DMDV2) {
|
|
218 ep = el_params(telement.arrayOf().getInternalTypeInfo(null).toElem(irs),
|
|
219 ea2, ea1, null);
|
|
220 rtlfunc = RTLSYM_ARRAYCMP2;
|
|
221 } else {
|
|
222 ep = el_params(telement.getInternalTypeInfo(null).toElem(irs), ea2, ea1, null);
|
|
223 rtlfunc = RTLSYM_ARRAYCMP;
|
|
224 }
|
|
225 e = el_bin(OPcall, TYint, el_var(rtlsym[rtlfunc]), ep);
|
|
226 e = el_bin(eop, TYint, e, el_long(TYint, 0));
|
|
227 el_setLoc(e,loc);
|
|
228 }
|
|
229 else
|
|
230 {
|
|
231 if (cast(int)eop <= 1)
|
|
232 {
|
|
233 /* The result is determinate, create:
|
|
234 * (e1 , e2) , eop
|
|
235 */
|
|
236 e = toElemBin(irs,OPcomma);
|
|
237 e = el_bin(OPcomma,e.Ety,e,el_long(e.Ety,cast(int)eop));
|
|
238 }
|
|
239 else
|
|
240 e = toElemBin(irs,eop);
|
|
241 }
|
|
242 return e;
|
|
243 }
|
|
244 }
|
|
245
|