annotate dmd/IfStatement.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children b7d29f613539
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.IfStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Argument;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.InlineCostState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.InlineDoState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.CondExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.AndAndExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.OrOrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.AssignExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 class IfStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 Argument arg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 Expression condition;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 Statement ifbody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 Statement elsebody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 VarDeclaration match; // for MatchExpression results
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 this(Loc loc, Argument arg, Expression condition, Statement ifbody, Statement elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 this.arg = arg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 this.condition = condition;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 this.ifbody = ifbody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 this.elsebody = elsebody;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 condition = condition.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 condition = resolveProperties(sc, condition);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 condition = condition.checkToBoolean();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 // If we can short-circuit evaluate the if statement, don't do the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 // semantic analysis of the skipped code.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 // This feature allows a limited form of conditional compilation.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 condition = condition.optimize(WANT.WANTflags);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 // Evaluate at runtime
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 uint cs0 = sc.callSuper;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 uint cs1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 Scope scd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 if (arg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 /* Declare arg, which we will set to be the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 * result of condition.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 ScopeDsymbol sym = new ScopeDsymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 sym.parent = sc.scopesym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 scd = sc.push(sym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 Type t = arg.type ? arg.type : condition.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 match = new VarDeclaration(loc, t, arg.ident, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 match.noauto = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 match.semantic(scd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 if (!scd.insert(match))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 match.parent = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 /* Generate:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 * (arg = condition)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 VarExp v = new VarExp(Loc(0), match);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 condition = new AssignExp(loc, v, condition);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 condition = condition.semantic(scd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 scd = sc.push();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 ifbody = ifbody.semantic(scd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 scd.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 cs1 = sc.callSuper;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 sc.callSuper = cs0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 elsebody = elsebody.semanticScope(sc, null, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 sc.mergeCallSuper(loc, cs1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 Expression interpret(InterState* istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 bool usesEH()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 //printf("IfStatement::blockExit(%p)\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 BE result = BE.BEnone;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 if (condition.canThrow())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 result |= BE.BEthrow;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 if (condition.isBool(true))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 result |= ifbody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 else if (condition.isBool(false))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 result |= elsebody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 result |= ifbody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 result |= elsebody.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 result |= BE.BEfallthru;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 //printf("IfStatement::blockExit(%p) = x%x\n", this, result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 IfStatement isIfStatement() { return this; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 int inlineCost(InlineCostState* ics)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 int cost;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 /* Can't declare variables inside ?: expressions, so
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 * we cannot inline if a variable is declared.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 if (arg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 return COST_MAX;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 cost = condition.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 /* Specifically allow:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 * if (condition)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 * return exp1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 * else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 * return exp2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 * Otherwise, we can't handle return statements nested in if's.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 if (elsebody && ifbody &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 ifbody.isReturnStatement() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 elsebody.isReturnStatement())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 cost += ifbody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 cost += elsebody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 //printf("cost = %d\n", cost);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 ics.nested += 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 cost += ifbody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 cost += elsebody.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 ics.nested -= 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 return cost;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 Expression doInline(InlineDoState ids)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 Expression econd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 Expression e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 Expression e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 assert(!arg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 econd = condition.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 assert(econd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 e1 = ifbody.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 e1 = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 e2 = elsebody.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 e2 = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 if (e1 && e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 e = new CondExp(econd.loc, econd, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 e.type = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 else if (e1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 e = new AndAndExp(econd.loc, econd, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 e.type = Type.tvoid;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 else if (e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 e = new OrOrExp(econd.loc, econd, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 e.type = Type.tvoid;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 e = econd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 Statement inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 condition = condition.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 ifbody = ifbody.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 elsebody = elsebody.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 void toIR(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 //printf("IfStatement::toIR('%s')\n", condition.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 IRState mystate = IRState(irs, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 // bexit is the block that gets control after this IfStatement is done
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 block* bexit = mystate.breakBlock ? mystate.breakBlock : block_calloc();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 if (match)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 /* Generate:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 * if (match = RTLSYM_IFMATCH(string, pattern)) ...
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 assert(condition.op == TOK.TOKmatch);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 e = matchexp_toelem(cast(MatchExp)condition, &mystate, RTLSYM.RTLSYM_IFMATCH);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 Symbol *s = match.toSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 symbol_add(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 e = el_bin(OPeq, TYnptr, el_var(s), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 e = condition.toElem(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 e = condition.toElem(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 block_appendexp(blx.curblock, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 block* bcond = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 block_next(blx, BC.BCiftrue, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 list_append(&bcond.Bsucc, blx.curblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 if (ifbody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 ifbody.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 list_append(&blx.curblock.Bsucc, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 if (elsebody)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 block_next(blx, BC.BCgoto, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 list_append(&bcond.Bsucc, blx.curblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 elsebody.toIR(&mystate);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 list_append(&blx.curblock.Bsucc, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 list_append(&bcond.Bsucc, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 block_next(blx, BC.BCgoto, bexit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 }