annotate dmd/CaseStatement.d @ 68:ee3a9f34dc48

final bits of codegen implementation to compile Phobos
author korDen
date Tue, 24 Aug 2010 16:44:34 +0400
parents cab4c37afb89
children 2e2a5c3f943a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.CaseStatement;
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.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.SwitchStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.GotoCaseStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.backend.BC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 class CaseStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Expression exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 Statement statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 int index = 0; // which case it is (since we sort this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 block* cblock = null; // back end: label for the block
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 this(Loc loc, Expression exp, Statement s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 this.exp = exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 this.statement = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
53
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
47 CaseStatement s = new CaseStatement(loc, exp.syntaxCopy(), statement.syntaxCopy());
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
48 return s;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 SwitchStatement sw = sc.sw;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 //printf("CaseStatement.semantic() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 if (sw)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 exp = exp.implicitCastTo(sc, sw.condition.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 exp = exp.optimize(WANTvalue | WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 /* This is where variables are allowed as case expressions.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 if (exp.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 { VarExp ve = cast(VarExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 VarDeclaration v = ve.var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 Type t = exp.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 if (v && (t.isintegral() || t.ty == Tclass))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 /* Flag that we need to do special code generation
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 * for this, i.e. generate a sequence of if-then-else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 sw.hasVars = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 if (sw.isFinal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 error("case variables not allowed in final switch statements");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 if (exp.op != TOKstring && exp.op != TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 error("case must be a string or an integral constant, not %s", exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 exp = new IntegerExp(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 for (int i = 0; i < sw.cases.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 CaseStatement cs = cast(CaseStatement)sw.cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 //printf("comparing '%s' with '%s'\n", exp.toChars(), cs.exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 if (cs.exp.equals(exp))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 error("duplicate case %s in switch statement", exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 sw.cases.push(cast(void*)this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 // Resolve any goto case's with no exp to this case statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 for (int i = 0; i < sw.gotoCases.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 GotoCaseStatement gcs = cast(GotoCaseStatement)sw.gotoCases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 if (!gcs.exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 gcs.cs = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 sw.gotoCases.remove(i); // remove from array
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 if (sc.sw.tf !is sc.tf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 error("switch and case are in different finally blocks");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 error("case not in switch statement");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 statement = statement.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121
68
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 63
diff changeset
122 int opCmp(Object obj)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
68
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 63
diff changeset
124 // Sort cases so we can do an efficient lookup
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 63
diff changeset
125 CaseStatement cs2 = cast(CaseStatement)obj;
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 63
diff changeset
126
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 63
diff changeset
127 return exp.opCmp(cs2.exp);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 bool usesEH()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 return statement.blockExit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 bool comeFrom()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144
63
cab4c37afb89 A bunch of implementations
korDen
parents: 53
diff changeset
145 Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 Statement inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 //printf("CaseStatement.inlineScan()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 exp = exp.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 if (statement)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 statement = statement.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 void toIR(IRState *irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 Blockx* blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 block* bcase = blx.curblock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 if (!cblock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 cblock = block_calloc(blx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 block_next(blx,BCgoto,cblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 block* bsw = irs.getSwitchBlock();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 if (bsw.BC == BCswitch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 list_append(&bsw.Bsucc,cblock); // second entry in pair
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 list_append(&bcase.Bsucc,cblock);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 if (blx.tryblock != bsw.Btry)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 error("case cannot be in different try block level from switch");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 incUsage(irs, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 if (statement)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 statement.toIR(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 }