Otawa  0.10
XCFGVisitor.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * Copyright (c) 2006 IRIT-UPS
4  *
5  * XCFGVisitor class interface.
6  */
7 #ifndef OTAWA_DFA_XCFG_VISITOR_H
8 #define OTAWA_DFA_XCFG_VISITOR_H
9 
10 #include <elm/assert.h>
11 #include <otawa/cfg.h>
12 #include <otawa/cfg/CFGCollector.h>
13 #include <elm/genstruct/FragTable.h>
15 #include <elm/util/Pair.h>
16 
17 namespace otawa { namespace dfa {
18 
19 using namespace elm;
20 using namespace elm::genstruct;
21 
22 extern Identifier<int> INDEX;
23 
24 // XCFGVisitor class
25 template <class P>
26 class XCFGVisitor {
27  typedef struct node_t {
29  int cfg, to, from;
30  inline node_t(void): bb(0), cfg(-1), to(-1), from(-1) { }
31  } node_t;
32  P& dom;
35  int *offs;
37 
38 public:
39 
40  XCFGVisitor(const CFGCollection& cfgs, P& domain);
41  ~XCFGVisitor(void);
42 
43  typedef typename P::domain_t domain_t;
44  inline domain_t *empty(void) { return dom.empty(); }
45  inline void free(domain_t *d) { dom.free(d); }
46  inline domain_t *gen(int node)
47  { return dom.gen(cfgs[nodes[node].cfg], nodes[node].bb); }
48  inline domain_t *preserve(int node)
49  { return dom.preserve(cfgs[nodes[node].cfg], nodes[node].bb); }
50 
52  inline int index(const key_t& key)
53  { return offs[INDEX(key.fst)] + otawa::INDEX(key.snd); }
54 
55  inline int size(void) { return nodes.length(); }
56  void visitPreds(XIterativeDFA< XCFGVisitor<P> >& engine, int node);
57  void visitSuccs(XIterativeDFA< XCFGVisitor<P> >& engine, int node);
58 };
59 
60 // Inlines
61 template <class D>
63 : dom(domain),
64  cfgs(_cfgs),
65  offs(new int[cfgs.count() + 1]),
66  preds(new Vector<int>[cfgs.count()])
67 {
68  // Compute CFG offsets
69  int i = 0;
70  offs[0] = 0;
72  INDEX(cfg) = i++;
73  offs[i] = offs[i - 1] + cfg->countBB();
74  }
75  nodes.alloc(offs[i]);
76 
77  // Record nodes and CFG next nodes
78  // !!NOTE!! Does not support CFG ending with a branch on another CFG
79  // !!TOCHECK!! For multicall
80  int bbi = 0, cfgi = 0;
81  for(CFGCollection::Iterator cfg(cfgs); cfg; cfg++, cfgi++)
82  for(CFG::BBIterator bb(cfg); bb; bb++, bbi++) {
83  nodes[bbi].bb = bb;
84  nodes[bbi].cfg = cfgi;
85  BasicBlock *next = 0;
86  //CFG *called = 0;
88  if(edge->kind() == Edge::CALL) {
89  CFG *called = edge->calledCFG();
90  int called_index = INDEX(called);
91  nodes[bbi].to = offs[called_index];
92  nodes[offs[cfgi] + next->number()].from =
93  offs[called_index + 1] - 1;
94  preds[called_index].add(bbi);
95  }
96  /*else
97  next = edge->target();*/
98  }
99  /*if(called) {
100  int called_index = INDEX(called);
101  nodes[bbi].to = offs[called_index];
102  nodes[offs[cfgi] + next->number()].from =
103  offs[called_index + 1] - 1;
104  preds[called_index].add(bbi);
105  }*/
106  }
107 }
108 
109 
110 template <class D>
112 
113  // Release INDEX on CFGs
114  for(CFGCollection::Iterator cfg(cfgs); cfg; cfg++)
115  cfg->removeProp(&INDEX);
116 
117  // Free memory
118  delete [] offs;
119  delete [] preds;
120 }
121 
122 
123 template <class D>
125 {
126  node_t info = nodes[node];
127  if(info.bb->isEntry()) {
128  Vector<int>& pred = preds[info.cfg];
129  for(int i = 0; i < pred.length(); i++)
130  engine.nextPred(pred[i]);
131  }
132  else if(info.from != -1)
133  engine.nextPred(info.from);
134  else
135  for(BasicBlock::InIterator edge(info.bb); edge; edge++)
136  engine.nextPred(offs[info.cfg] + edge->source()->number());
137 }
138 
139 
140 template <class D>
142 }
143 
144 } } // otawa::dfa
145 
146 #endif // OTAWA_DFA_XCFG_VISITOR_H
Definition: CFG.h:48
int cfg
Definition: XCFGVisitor.h:29
int from
Definition: XCFGVisitor.h:29
dtd::Element edge(dtd::make("edge", _EDGE).attr(source).attr(target).attr(called))
This class implements the visitor concept of XIterativeDFA class and allows applying an analysis on a...
Definition: XCFGVisitor.h:26
~XCFGVisitor(void)
Definition: XCFGVisitor.h:111
void visitSuccs(XIterativeDFA< XCFGVisitor< P > > &engine, int node)
Definition: XCFGVisitor.h:141
domain_t * gen(int node)
Definition: XCFGVisitor.h:46
Kind of an edge matching a sub-program call.
Definition: Edge.h:40
int size(void)
Definition: XCFGVisitor.h:55
Identifier< int > INDEX
Identifier used for storing in each basic block from the CFG its index.
Definition: CFG.h:39
const CFGCollection & cfgs
Definition: XCFGVisitor.h:33
dtd::Element bb(dtd::make("bb", _BB).attr(id).attr(address).attr(size))
Control Flow Graph representation.
Definition: CFG.h:42
bool isEntry(void) const
Test if the basic block is the CFG entry.
Definition: BasicBlock.h:136
FragTable< node_t > nodes
Definition: XCFGVisitor.h:34
dtd::RefAttr< CFG * > called("called", dtd::STRICT)
domain_t * preserve(int node)
Definition: XCFGVisitor.h:48
void free(domain_t *d)
Definition: XCFGVisitor.h:45
int length(void) const
Contains a collection of CFGs (used with INVOLVED_CFGS property).
Definition: features.h:45
Definition: BasicBlock.h:165
int number(void) const
Get the number hooked on this basic block, that is, value of ID_Index property.
Definition: BasicBlock.h:146
node_t(void)
Definition: XCFGVisitor.h:30
int * offs
Definition: XCFGVisitor.h:35
domain_t * empty(void)
Definition: XCFGVisitor.h:44
Vector< int > * preds
Definition: XCFGVisitor.h:36
Iterator on the CFG contained in a CFGCollection.
Definition: features.h:54
void visitPreds(XIterativeDFA< XCFGVisitor< P > > &engine, int node)
Definition: XCFGVisitor.h:124
Definition: BasicBlock.h:160
P::domain_t domain_t
Definition: XCFGVisitor.h:43
Identifier< int > INDEX
Identifier used for internal use.
dtd::Element cfg(dtd::make("cfg", _CFG).attr(id).content((entry,*bb, exit,*edge)))
BasicBlock * bb
Definition: XCFGVisitor.h:28
int index(const key_t &key)
Definition: XCFGVisitor.h:52
This is the minimal definition of a basic block.
Definition: BasicBlock.h:43
P & dom
Definition: XCFGVisitor.h:32
int to
Definition: XCFGVisitor.h:29
The DFAEngine implements an extended Iterative Data Flow Algorithm, that is, it work on graphs with a...
Definition: XIterativeDFA.h:16
Pair< CFG *, BasicBlock * > key_t
Definition: XCFGVisitor.h:51
XCFGVisitor(const CFGCollection &cfgs, P &domain)
Build the visitor to apply on the given domain and CFG collection.
Definition: XCFGVisitor.h:62
Definition: XCFGVisitor.h:27