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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 0
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Module;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.backend.OPER;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 struct IRState
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 IRState* prev;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 Statement statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 Module m; // module
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 Dsymbol symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Identifier ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 Symbol* shidden; // hidden parameter to function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 Symbol* sthis; // 'this' parameter to function (member and nested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 Symbol* sclosure; // pointer to closure instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 Blockx* blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 Array deferToObj; // array of Dsymbol's to run toObjFile(int multiobj) on later
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 elem* ehidden; // transmit hidden pointer to CallExp::toElem()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 Symbol* startaddress;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 block* breakBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 block* contBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 block* switchBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 block* defaultBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 this(IRState* irs, Statement s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 prev = irs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 statement = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 if (irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 m = irs.m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 shidden = irs.shidden;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 sclosure = irs.sclosure;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 sthis = irs.sthis;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 deferToObj = irs.deferToObj;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 130
diff changeset
59
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 this(IRState* irs, Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 130
diff changeset
64
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 this(Module m, Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 this.m = m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 symbol = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 block* getBreakBlock(Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 for (IRState* bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 if (ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 if (bc.prev && bc.prev.ident == ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 return bc.breakBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 else if (bc.breakBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 return bc.breakBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 130
diff changeset
85
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 block* getContBlock(Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 IRState* bc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 for (bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 if (ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 if (bc.prev && bc.prev.ident == ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 return bc.contBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 else if (bc.contBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 return bc.contBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 130
diff changeset
102
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 block* getSwitchBlock()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 for (IRState* bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 if (bc.switchBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 return bc.switchBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 130
diff changeset
112
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 block* getDefaultBlock()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 for (IRState* bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 if (bc.defaultBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 return bc.defaultBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 130
diff changeset
122
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 FuncDeclaration getFunc()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 IRState* bc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 for (bc = &this; bc.prev; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 return cast(FuncDeclaration)(bc.symbol);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 /*********************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 * Produce elem which increments the usage count for a particular line.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 * Used to implement -cov switch (coverage analysis).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 elem *incUsageElem(IRState *irs, Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 uint linnum = loc.linnum;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 if (!irs.blx.module_.cov || !linnum || loc.filename != irs.blx.module_.srcfile.toChars())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 //printf("cov = %p, covb = %p, linnum = %u\n", irs->blx->module->cov, irs->blx->module->covb, p, linnum);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 linnum--; // from 1-based to 0-based
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 /* Set bit in covb[] indicating this is a valid code line number
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 uint* p = irs.blx.module_.covb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 if (p) // covb can be NULL if it has already been written out to its .obj file
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 p += linnum / ((*p).sizeof * 8);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 *p |= 1 << (linnum & ((*p).sizeof * 8 - 1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 e = el_ptr(irs.blx.module_.cov);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 e = el_bin(OPER.OPadd, TYM.TYnptr, e, el_long(TYM.TYuint, linnum * 4));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 e = el_una(OPER.OPind, TYM.TYuint, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 e = el_bin(OPER.OPaddass, TYM.TYuint, e, el_long(TYM.TYuint, 1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 /**************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 * Add in code to increment usage count for linnum.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 void incUsage(IRState* irs, Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 if (global.params.cov && loc.linnum)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 block_appendexp(irs.blx.curblock, incUsageElem(irs, loc));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 130
diff changeset
198 }