Mercurial > projects > ddmd
annotate dmd/IRState.d @ 192:eb38fdcb3e62 default tip
updated to compile with dmd2.062
author | korDen |
---|---|
date | Sat, 02 Mar 2013 01:25:52 -0800 |
parents | e3afd1303184 |
children |
rev | line source |
---|---|
0 | 1 module dmd.IRState; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Statement; |
5 import dmd.Module; | |
6 import dmd.Dsymbol; | |
7 import dmd.Identifier; | |
8 import dmd.Array; | |
9 import dmd.FuncDeclaration; | |
10 import dmd.Global; | |
11 import dmd.Loc; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
12 import dmd.TRUST; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
13 import dmd.TY; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
14 import dmd.TypeFunction; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
15 import dmd.Type; |
0 | 16 |
17 import dmd.backend.Symbol; | |
18 import dmd.backend.Blockx; | |
19 import dmd.backend.block; | |
20 import dmd.backend.elem; | |
21 import dmd.backend.Util; | |
22 import dmd.backend.TYM; | |
23 import dmd.backend.OPER; | |
24 | |
25 struct IRState | |
26 { | |
27 IRState* prev; | |
28 Statement statement; | |
29 Module m; // module | |
30 Dsymbol symbol; | |
31 Identifier ident; | |
32 Symbol* shidden; // hidden parameter to function | |
33 Symbol* sthis; // 'this' parameter to function (member and nested) | |
34 Symbol* sclosure; // pointer to closure instance | |
35 Blockx* blx; | |
36 Array deferToObj; // array of Dsymbol's to run toObjFile(int multiobj) on later | |
37 elem* ehidden; // transmit hidden pointer to CallExp::toElem() | |
38 Symbol* startaddress; | |
39 | |
40 block* breakBlock; | |
41 block* contBlock; | |
42 block* switchBlock; | |
43 block* defaultBlock; | |
44 | |
45 this(IRState* irs, Statement s) | |
46 { | |
47 prev = irs; | |
48 statement = s; | |
49 if (irs) | |
50 { | |
51 m = irs.m; | |
52 shidden = irs.shidden; | |
53 sclosure = irs.sclosure; | |
54 sthis = irs.sthis; | |
55 blx = irs.blx; | |
56 deferToObj = irs.deferToObj; | |
57 } | |
58 } | |
178 | 59 |
0 | 60 this(IRState* irs, Dsymbol s) |
61 { | |
62 assert(false); | |
63 } | |
178 | 64 |
0 | 65 this(Module m, Dsymbol s) |
66 { | |
67 this.m = m; | |
68 symbol = s; | |
69 } | |
70 | |
71 block* getBreakBlock(Identifier ident) | |
72 { | |
73 for (IRState* bc = &this; bc; bc = bc.prev) | |
74 { | |
75 if (ident) | |
76 { | |
77 if (bc.prev && bc.prev.ident == ident) | |
78 return bc.breakBlock; | |
79 } | |
80 else if (bc.breakBlock) | |
81 return bc.breakBlock; | |
82 } | |
83 return null; | |
84 } | |
178 | 85 |
0 | 86 block* getContBlock(Identifier ident) |
87 { | |
88 IRState* bc; | |
89 | |
90 for (bc = &this; bc; bc = bc.prev) | |
91 { | |
92 if (ident) | |
93 { | |
94 if (bc.prev && bc.prev.ident == ident) | |
95 return bc.contBlock; | |
96 } | |
97 else if (bc.contBlock) | |
98 return bc.contBlock; | |
99 } | |
100 return null; | |
101 } | |
178 | 102 |
0 | 103 block* getSwitchBlock() |
104 { | |
105 for (IRState* bc = &this; bc; bc = bc.prev) | |
106 { | |
107 if (bc.switchBlock) | |
108 return bc.switchBlock; | |
109 } | |
110 return null; | |
111 } | |
178 | 112 |
0 | 113 block* getDefaultBlock() |
114 { | |
115 for (IRState* bc = &this; bc; bc = bc.prev) | |
116 { | |
117 if (bc.defaultBlock) | |
118 return bc.defaultBlock; | |
119 } | |
120 return null; | |
121 } | |
178 | 122 |
0 | 123 FuncDeclaration getFunc() |
124 { | |
125 IRState* bc; | |
126 for (bc = &this; bc.prev; bc = bc.prev) | |
127 { | |
128 } | |
129 return cast(FuncDeclaration)(bc.symbol); | |
130 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
131 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
132 /********************** |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
133 * Return !=0 if do array bounds checking |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
134 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
135 int arrayBoundsCheck() |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
136 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
137 int result = global.params.useArrayBounds; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
138 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
139 if (result == 1) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
140 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
141 // For safe functions only |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
142 result = 0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
143 FuncDeclaration fd = getFunc(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
144 if (fd) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
145 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
146 Type t = fd.type; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
147 if (t.ty == TY.Tfunction && (cast(TypeFunction)t).trust == TRUST.TRUSTsafe) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
148 result = 1; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
149 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
150 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
151 return result; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
152 } |
0 | 153 } |
154 | |
155 /********************************************* | |
156 * Produce elem which increments the usage count for a particular line. | |
157 * Used to implement -cov switch (coverage analysis). | |
158 */ | |
159 | |
160 elem *incUsageElem(IRState *irs, Loc loc) | |
161 { | |
162 uint linnum = loc.linnum; | |
163 | |
164 if (!irs.blx.module_.cov || !linnum || loc.filename != irs.blx.module_.srcfile.toChars()) | |
165 return null; | |
166 | |
167 //printf("cov = %p, covb = %p, linnum = %u\n", irs->blx->module->cov, irs->blx->module->covb, p, linnum); | |
168 | |
169 linnum--; // from 1-based to 0-based | |
170 | |
171 /* Set bit in covb[] indicating this is a valid code line number | |
172 */ | |
173 uint* p = irs.blx.module_.covb; | |
174 if (p) // covb can be NULL if it has already been written out to its .obj file | |
175 { | |
176 p += linnum / ((*p).sizeof * 8); | |
177 *p |= 1 << (linnum & ((*p).sizeof * 8 - 1)); | |
178 } | |
179 | |
180 elem* e; | |
181 e = el_ptr(irs.blx.module_.cov); | |
182 e = el_bin(OPER.OPadd, TYM.TYnptr, e, el_long(TYM.TYuint, linnum * 4)); | |
183 e = el_una(OPER.OPind, TYM.TYuint, e); | |
184 e = el_bin(OPER.OPaddass, TYM.TYuint, e, el_long(TYM.TYuint, 1)); | |
185 | |
186 return e; | |
187 } | |
188 | |
189 /************************************** | |
190 * Add in code to increment usage count for linnum. | |
191 */ | |
192 void incUsage(IRState* irs, Loc loc) | |
193 { | |
194 if (global.params.cov && loc.linnum) | |
195 { | |
196 block_appendexp(irs.blx.curblock, incUsageElem(irs, loc)); | |
197 } | |
178 | 198 } |