Mercurial > projects > ddmd
annotate dmd/MinExp.d @ 182:b64060ab22df
Now compileable with dmd2.050
author | korDen |
---|---|
date | Sat, 30 Oct 2010 05:05:32 +0400 |
parents | e3afd1303184 |
children | b0d41ff5e0df |
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 { | |
178 | 30 register(); |
0 | 31 super(loc, TOK.TOKmin, MinExp.sizeof, e1, e2); |
32 } | |
33 | |
72 | 34 override Expression semantic(Scope sc) |
0 | 35 { |
72 | 36 Expression e; |
37 Type t1; | |
38 Type t2; | |
39 | |
40 version (LOGSEMANTIC) { | |
41 printf("MinExp.semantic('%s')\n", toChars()); | |
42 } | |
43 if (type) | |
44 return this; | |
45 | |
46 super.semanticp(sc); | |
47 | |
48 e = op_overload(sc); | |
49 if (e) | |
50 return e; | |
51 | |
52 e = this; | |
53 t1 = e1.type.toBasetype(); | |
54 t2 = e2.type.toBasetype(); | |
55 if (t1.ty == TY.Tpointer) | |
56 { | |
57 if (t2.ty == TY.Tpointer) | |
58 { // Need to divide the result by the stride | |
59 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
60 long stride; | |
61 Expression ee; | |
62 | |
63 typeCombine(sc); // make sure pointer types are compatible | |
64 type = Type.tptrdiff_t; | |
65 stride = t2.nextOf().size(); | |
66 if (stride == 0) | |
67 { | |
68 ee = new IntegerExp(loc, 0, Type.tptrdiff_t); | |
69 } | |
70 else | |
71 { | |
72 ee = new DivExp(loc, this, new IntegerExp(Loc(0), stride, Type.tptrdiff_t)); | |
73 ee.type = Type.tptrdiff_t; | |
74 } | |
75 return ee; | |
76 } | |
77 else if (t2.isintegral()) | |
78 e = scaleFactor(sc); | |
79 else | |
80 { | |
81 error("incompatible types for minus"); | |
82 return new ErrorExp(); | |
83 } | |
84 } | |
85 else if (t2.ty == TY.Tpointer) | |
86 { | |
87 type = e2.type; | |
88 error("can't subtract pointer from %s", e1.type.toChars()); | |
89 return new ErrorExp(); | |
90 } | |
91 else | |
92 { | |
93 typeCombine(sc); | |
94 t1 = e1.type.toBasetype(); | |
95 t2 = e2.type.toBasetype(); | |
96 if ((t1.isreal() && t2.isimaginary()) || | |
97 (t1.isimaginary() && t2.isreal())) | |
98 { | |
99 switch (type.ty) | |
100 { | |
101 case TY.Tfloat32: | |
102 case TY.Timaginary32: | |
103 type = Type.tcomplex32; | |
104 break; | |
105 | |
106 case TY.Tfloat64: | |
107 case TY.Timaginary64: | |
108 type = Type.tcomplex64; | |
109 break; | |
110 | |
111 case TY.Tfloat80: | |
112 case TY.Timaginary80: | |
113 type = Type.tcomplex80; | |
114 break; | |
115 | |
116 default: | |
117 assert(0); | |
118 } | |
119 } | |
120 } | |
0 | 121 return e; |
122 } | |
123 | |
72 | 124 override Expression optimize(int result) |
0 | 125 { |
72 | 126 Expression e; |
127 | |
128 e1 = e1.optimize(result); | |
129 e2 = e2.optimize(result); | |
130 if (e1.isConst() && e2.isConst()) | |
131 { | |
132 if (e2.op == TOK.TOKsymoff) | |
133 return this; | |
134 e = Min(type, e1, e2); | |
135 } | |
136 else | |
137 e = this; | |
138 | |
0 | 139 return e; |
140 } | |
141 | |
72 | 142 override Expression interpret(InterState istate) |
0 | 143 { |
115
6caaf0256da1
+ interpretation of (non-assign) binary expressions
Trass3r
parents:
114
diff
changeset
|
144 return interpretCommon(istate, &Min); |
0 | 145 } |
146 | |
72 | 147 override void buildArrayIdent(OutBuffer buf, Expressions arguments) |
0 | 148 { |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
149 Exp_buildArrayIdent(buf, arguments, "Min"); |
0 | 150 } |
151 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
123
diff
changeset
|
152 override Expression buildArrayLoop(Parameters fparams) |
0 | 153 { |
123 | 154 return Exp_buildArrayLoop!(typeof(this))(fparams); |
0 | 155 } |
156 | |
72 | 157 override Identifier opId() |
0 | 158 { |
159 return Id.sub; | |
160 } | |
161 | |
72 | 162 override Identifier opId_r() |
0 | 163 { |
164 return Id.sub_r; | |
165 } | |
166 | |
72 | 167 override elem* toElem(IRState* irs) |
0 | 168 { |
72 | 169 Type tb1 = e1.type.toBasetype(); |
170 Type tb2 = e2.type.toBasetype(); | |
171 | |
172 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray)) | |
173 { | |
174 error("Array operation %s not implemented", toChars()); | |
175 return el_long(type.totym(), 0); // error recovery | |
176 } | |
177 | |
178 return toElemBin(irs, OPER.OPmin); | |
0 | 179 } |
180 } | |
181 |