annotate dmd/IRState.d @ 114:e28b18c23469

added a module dmd.common for commonly used stuff it currently holds code for consistency checking of predefined versions also added a VisualD project file
author Trass3r
date Wed, 01 Sep 2010 18:21:58 +0200
parents 10317f0c89a5
children 60bb0fe4563e
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;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.backend.Blockx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.backend.block;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.backend.OPER;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 struct IRState
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 IRState* prev;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 Statement statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 Module m; // module
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 Dsymbol symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 Identifier ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 Symbol* shidden; // hidden parameter to function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 Symbol* sthis; // 'this' parameter to function (member and nested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 Symbol* sclosure; // pointer to closure instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Blockx* blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 Array deferToObj; // array of Dsymbol's to run toObjFile(int multiobj) on later
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 elem* ehidden; // transmit hidden pointer to CallExp::toElem()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 Symbol* startaddress;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 block* breakBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 block* contBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 block* switchBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 block* defaultBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 this(IRState* irs, Statement s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 prev = irs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 statement = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 if (irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 m = irs.m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 shidden = irs.shidden;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 sclosure = irs.sclosure;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 sthis = irs.sthis;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 blx = irs.blx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 deferToObj = irs.deferToObj;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 this(IRState* irs, Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 this(Module m, Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 this.m = m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 symbol = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 block* getBreakBlock(Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 for (IRState* bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 if (ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 if (bc.prev && bc.prev.ident == ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 return bc.breakBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 else if (bc.breakBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 return bc.breakBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 block* getContBlock(Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 IRState* bc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 for (bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 if (ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 if (bc.prev && bc.prev.ident == ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 return bc.contBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 else if (bc.contBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 return bc.contBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 block* getSwitchBlock()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 for (IRState* bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 if (bc.switchBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 return bc.switchBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 block* getDefaultBlock()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 for (IRState* bc = &this; bc; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 if (bc.defaultBlock)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 return bc.defaultBlock;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 FuncDeclaration getFunc()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 IRState* bc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 for (bc = &this; bc.prev; bc = bc.prev)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 return cast(FuncDeclaration)(bc.symbol);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 /*********************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 * Produce elem which increments the usage count for a particular line.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 * Used to implement -cov switch (coverage analysis).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 elem *incUsageElem(IRState *irs, Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 uint linnum = loc.linnum;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 if (!irs.blx.module_.cov || !linnum || loc.filename != irs.blx.module_.srcfile.toChars())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 //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
142
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 linnum--; // from 1-based to 0-based
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 /* Set bit in covb[] indicating this is a valid code line number
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 uint* p = irs.blx.module_.covb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 if (p) // covb can be NULL if it has already been written out to its .obj file
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 p += linnum / ((*p).sizeof * 8);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 *p |= 1 << (linnum & ((*p).sizeof * 8 - 1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 e = el_ptr(irs.blx.module_.cov);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 e = el_bin(OPER.OPadd, TYM.TYnptr, e, el_long(TYM.TYuint, linnum * 4));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 e = el_una(OPER.OPind, TYM.TYuint, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 e = el_bin(OPER.OPaddass, TYM.TYuint, e, el_long(TYM.TYuint, 1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 /**************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 * Add in code to increment usage count for linnum.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 void incUsage(IRState* irs, Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 if (global.params.cov && loc.linnum)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 block_appendexp(irs.blx.curblock, incUsageElem(irs, loc));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 }