Update copyright to LGPL on all files
[dyninst.git] / dyninstAPI / h / BPatch_basicBlock.h
1 /*
2  * Copyright (c) 1996-2009 Barton P. Miller
3  * 
4  * We provide the Paradyn Parallel Performance Tools (below
5  * described as "Paradyn") on an AS IS basis, and do not warrant its
6  * validity or performance.  We reserve the right to update, modify,
7  * or discontinue this software at any time.  We shall have no
8  * obligation to supply such updates or modifications or any other
9  * form of support to you.
10  * 
11  * By your use of Paradyn, you understand and agree that we (or any
12  * other person or entity with proprietary rights in Paradyn) are
13  * under no obligation to provide either maintenance services,
14  * update services, notices of latent defects, or correction of
15  * defects for Paradyn.
16  * 
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  * 
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * Lesser General Public License for more details.
26  * 
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31
32 #ifndef _BPatch_basicBlock_h_
33 #define _BPatch_basicBlock_h_
34
35 #include "BPatch_point.h"
36 #include "BPatch_dll.h"
37 #include "BPatch_Vector.h"
38 #include "BPatch_Set.h"
39 #include "BPatch_sourceBlock.h" 
40 #include "BPatch_instruction.h"
41 #include "BPatch_eventLock.h"
42 #include "Instruction.h"
43 //#include "BPatch_edge.h"
44
45 class image;
46 class int_function;
47 class int_basicBlock;
48
49 /* Currently all this bitarray stuff is just for power, 
50    but could be extended as we do liveness stuff for other platforms */
51
52
53 /** class for machine code basic blocks. We assume the user can not 
54   * create basic blocks using its constructor. It is not safe. 
55   * basic blocks are used for reading purposes not for inserting
56   * a new code to the machine executable other than instrumentation code
57   *
58   * @see BPatch_flowGraph
59   * @see BPatch_sourceBlock
60   * @see BPatch_basicBlockLoop
61   */
62 class BPatch_flowGraph;
63 #ifdef DYNINST_CLASS_NAME
64 #undef DYNINST_CLASS_NAME
65 #endif
66 #define DYNINST_CLASS_NAME BPatch_basicBlock
67 struct insnPredicate : public std::unary_function<Dyninst::InstructionAPI::Instruction::Ptr, bool>
68 {
69     virtual result_type operator()(argument_type arg) = 0;
70     virtual ~insnPredicate() {}
71     
72 };
73
74 class BPATCH_DLL_EXPORT BPatch_basicBlock : public BPatch_eventLock {
75         friend class BPatch_flowGraph;
76         friend class TarjanDominator;
77         friend class dominatorCFG;
78         friend class InstrucIter;
79         friend class int_function;
80         friend class BPatch_instruction;
81         friend std::ostream& operator<<(std::ostream&,BPatch_basicBlock&);
82
83  private:
84    /** the internal basic block structure **/
85    int_basicBlock *iblock;
86
87    /** the flow graph that contains this basic block */
88    BPatch_flowGraph *flowGraph;
89    
90    /** set of basic blocks that this basicblock dominates immediately*/
91    BPatch_Set<BPatch_basicBlock*>* immediateDominates;
92    
93    /** basic block which is the immediate dominator of the basic block */
94    BPatch_basicBlock *immediateDominator;
95    
96    /** same as previous two fields, but for postdominator tree */
97    BPatch_Set<BPatch_basicBlock*> *immediatePostDominates;
98    BPatch_basicBlock *immediatePostDominator;
99    
100    /** the source block(source lines) that basic block corresponds*/
101    BPatch_Vector<BPatch_sourceBlock*> *sourceBlocks;
102    
103    /** the instructions within this block */
104    BPatch_Vector<BPatch_instruction*> *instructions;
105    
106    /** the incoming edges */
107    BPatch_Set<BPatch_edge*> incomingEdges;
108  
109    /** the outgoing edges */
110    BPatch_Set<BPatch_edge*> outgoingEdges;
111
112  protected:
113
114    /** constructor of class */
115    BPatch_basicBlock(int_basicBlock *ib, BPatch_flowGraph *fg);
116
117
118    
119    BPatch_Vector<BPatch_point*>*
120            findPointByPredicate(insnPredicate& f);
121
122  public:
123    
124    // Internal functions. Don't use these unless you know what you're
125    // doing.
126    int_basicBlock *lowlevel_block()  { return iblock; }
127
128
129         /** BPatch_basicBlock::getSources   */
130         /** method that returns the predecessors of the basic block */
131
132    API_EXPORT_V(Int, (srcs),
133                 void,getSources,(BPatch_Vector<BPatch_basicBlock*> &srcs));
134
135         /** BPatch_basicBlock::getTargets   */
136         /** method that returns the successors  of the basic block */
137
138    API_EXPORT_V(Int, (targets),
139                 void,getTargets,(BPatch_Vector<BPatch_basicBlock*> &targets));
140
141         /** BPatch_basicBlock::dominates   */
142         /** returns true if argument is dominated by this basic block */
143
144    API_EXPORT(Int, (block),
145               bool,dominates,(BPatch_basicBlock *block));
146
147    /** BPatch_basicBlock::getImmediateDominiator   */
148    /** return the immediate dominator of a basic block */
149
150    API_EXPORT(Int, (),
151               BPatch_basicBlock*,getImmediateDominator,());
152
153         /** BPatch_basicBlock::getImmediateDominates   */
154         /** method that returns the basic blocks immediately dominated by   */
155         /** the basic block */
156
157    API_EXPORT_V(Int, (blocks),
158                 void,getImmediateDominates,(BPatch_Vector<BPatch_basicBlock*> &blocks));
159
160         /** BPatch_basicBlock::getAllDominates   */
161         /** method that returns all basic blocks dominated by the basic block */
162
163    API_EXPORT_V(Int, (blocks),
164                 void,getAllDominates,(BPatch_Set<BPatch_basicBlock*> &blocks));
165
166         /** the previous four methods, but for postdominators */
167
168         /** BPatch_basicBlock::postdominates   */
169
170    API_EXPORT(Int, (block),
171               bool,postdominates,(BPatch_basicBlock *block));
172
173         /** BPatch_basicBlock::getImmediatePostDominator   */
174
175    API_EXPORT(Int, (),
176               BPatch_basicBlock*,getImmediatePostDominator,());
177
178         /** BPatch_basicBlock::getImmediatePostDominates   */
179
180    API_EXPORT_V(Int, (blocks),
181                 void,getImmediatePostDominates,(BPatch_Vector<BPatch_basicBlock*> &blocks));
182
183         /** BPatch_basicBlock::getAllPostDominates   */
184
185    API_EXPORT_V(Int, (blocks),
186                 void,getAllPostDominates,(BPatch_Set<BPatch_basicBlock*> &blocks));
187         
188         /** BPatch_basicBlock::getSourceBlocks   */
189         /** returns the source block corresponding to the basic block */
190
191    API_EXPORT(Int, (blocks),
192               bool,getSourceBlocks,(BPatch_Vector<BPatch_sourceBlock*> &blocks));
193
194         /** BPatch_basicBlock::getBlockNumber   */
195         /** returns the block id */
196
197    API_EXPORT(Int, (),
198               int,getBlockNumber,());
199
200    /** BPatch_basicBlock::setEmtryBlock   */
201    /** sets whether this block is an entry block (or not) */
202
203    /** BPatch_basicBlock::isEntryBlock   */
204
205    API_EXPORT(Int, (),
206               bool,isEntryBlock,() CONST_EXPORT);
207
208         /** BPatch_basicBlock::isExitBlock   */
209
210    API_EXPORT(Int, (),
211               bool,isExitBlock,() CONST_EXPORT);
212
213         /** BPatch_basicBlock::size   */
214
215    API_EXPORT(Int, (),
216               unsigned,size,() CONST_EXPORT);
217
218         /** BPatch_basicBlock::getStartAddress   */
219    //these always return absolute address
220
221    API_EXPORT(Int, (),
222               unsigned long,getStartAddress,() CONST_EXPORT);
223
224         /** BPatch_basicBlock::getLastInsnAddress   */
225
226    API_EXPORT(Int, (),
227               unsigned long,getLastInsnAddress,() CONST_EXPORT);
228        
229    /** BPatch_basicBlock::getEndAddress    */
230         
231    API_EXPORT(Int, (),
232               unsigned long, getEndAddress, () CONST_EXPORT);
233
234         /** BPatch_basicBlock::~BPatch_basicBlock   */
235         /** destructor of class */
236
237    API_EXPORT_DTOR(_dtor, (),
238                    ~,BPatch_basicBlock,());
239         
240         /** BPatch_basicBlock::getAddressRange   */
241         /** return the start and end addresses of the basic block */
242
243    API_EXPORT(Int, (_startAddress, _endAddress),
244               bool,getAddressRange,(void*& _startAddress, void*& _endAddress));
245
246 #ifdef IBM_BPATCH_COMPAT
247    //  dummy placeholder.  I think this is only used by dpcl in a debug routine
248    API_EXPORT(Int, (_startLine, _endLine),
249               bool,getLineNumbers,(unsigned int &_startLine, unsigned int  &_endLine));       
250 #endif
251         /** BPatch_basicBlock::findEntryPoint   */
252         /** return point at the start of the basic block */
253
254    API_EXPORT(Int, (),
255               BPatch_point*, findEntryPoint,());
256
257         /** BPatch_basicBlock::findExitPoint   */
258         /** return point at the start of the basic block */
259    
260     API_EXPORT(Int, (),
261               BPatch_point*, findExitPoint,());
262
263    /** BPatch_basicBlock::findPoint   */
264         /** return a set of points within the basic block */
265
266    API_EXPORT(Int, (ops),
267               BPatch_Vector<BPatch_point*> *,findPoint,(const BPatch_Set<BPatch_opCode>& ops));
268
269    API_EXPORT(Int, (filter),
270               BPatch_Vector<BPatch_point*> *,findPoint,(bool(*filter)(Dyninst::InstructionAPI::Instruction::Ptr)));
271    /** BPatch_basicBlock::getInstructions   */
272         /** return the instructions that belong to the block */
273
274    API_EXPORT(Int, (),
275              BPatch_Vector<BPatch_instruction *> *,getInstructions,());
276
277    API_EXPORT(Int, (insns),
278               bool, getInstructions, (std::vector<Dyninst::InstructionAPI::Instruction::Ptr>& insns));
279    API_EXPORT(Addrs, (insnInstances),
280               bool, getInstructions, (std::vector<std::pair<Dyninst::InstructionAPI::Instruction::Ptr, Dyninst::Address> >& insnInstances));
281
282
283         /** BPatch_basicBlock::getIncomingEdges   */
284         /** returns the incoming edges */
285
286    API_EXPORT_V(Int, (inc),
287                 void,getIncomingEdges,(BPatch_Vector<BPatch_edge*> &inc));
288         
289         /** BPatch_basicBlock::getOutgoingEdges   */
290         /** returns the outgoming edges */
291
292
293    API_EXPORT_V(Int, (out),
294                 void,getOutgoingEdges,(BPatch_Vector<BPatch_edge*> &out));
295
296
297
298    int blockNo() const;
299     
300    struct compare {
301       int operator()(const BPatch_basicBlock *b1, 
302                      const BPatch_basicBlock *b2) const 
303       {
304          if (b1->blockNo() < b2->blockNo())
305             return -1;
306          if (b1->blockNo() > b2->blockNo())
307             return 1;
308          return 0;
309       }
310    };
311 };
312
313 #endif /* _BPatch_basicBlock_h_ */