-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature.cpp
194 lines (162 loc) · 4.86 KB
/
feature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) 1996-2010 Barton P. Miller
*
* We provide the Paradyn Parallel Performance Tools (below
* described as "Paradyn") on an AS IS basis, and do not warrant its
* validity or performance. We reserve the right to update, modify,
* or discontinue this software at any time. We shall have no
* obligation to supply such updates or modifications or any other
* form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include<assert.h>
#include "InstructionDecoder.h"
#include "Instruction.h"
#include "CodeObject.h"
#include "CFG.h"
#include "feature.h"
using namespace Dyninst::InstructionAPI;
/** iterator implementation **/
FeatureVector::iterator&
FeatureVector::iterator::operator++() {
if(_m_fv->hasmore(_m_ind))
++_m_ind;
else
_m_ind = -1;
return *this;
}
FeatureVector::iterator
FeatureVector::iterator::operator++(int) {
FeatureVector::iterator copy(*this);
if(_m_fv->hasmore(_m_ind))
++_m_ind;
else
_m_ind = -1;
return copy;
}
FeatureVector::iterator&
FeatureVector::iterator::operator--() {
if(_m_ind > 0)
--_m_ind;
return *this;
}
FeatureVector::iterator
FeatureVector::iterator::operator--(int) {
FeatureVector::iterator copy(*this);
if(_m_ind > 0)
--_m_ind;
return copy;
}
bool
FeatureVector::iterator::operator==(const FeatureVector::iterator & obj) const {
return obj._m_fv == _m_fv && obj._m_ind == _m_ind;
}
bool
FeatureVector::iterator::operator!=(const FeatureVector::iterator & obj) const {
return !(*this == obj);
}
Feature*
FeatureVector::iterator::operator*() {
return _m_fv->get(_m_ind);
}
/** generic lookup feature implementation **/
void
LookupFeature::add_term(LookupTerm *t)
{
_terms.push_back(t);
}
LookupFeature::LookupFeature(vector<LookupTerm *> & t)
{
_terms.insert(_terms.begin(),t.begin(),t.end());
}
/** feature vector implementation **/
FeatureVector::FeatureVector() :
_begin(new iterator(this,-1)),
_end(new iterator(this,-1)),
_limited(false)
{
}
FeatureVector::FeatureVector(char * featfile) :
_begin(new iterator(this,-1)),
_end(new iterator(this,-1)),
_limited(true)
{
// FIXME load limited feature descriptions
assert(0);
}
FeatureVector::~FeatureVector() {
if(_begin)
delete _begin;
if(_end)
delete _end;
}
int
FeatureVector::eval(int size, Function *f, bool idioms, bool operands) {
_feats.clear();
(*_begin) = (*_end);
Function::blocklist::iterator bit = f->blocks().begin();
for( ; bit != f->blocks().end(); ++bit) {
Block * b = *bit;
Block::Insns insns;
b->getInsns(insns);
for (auto iit = insns.begin(); iit != insns.end(); ++iit) {
if(idioms)
iflookup.lookup(size, f, iit->first, b->end(), _feats);
if(operands)
oflookup.lookup(size, f, iit->first, b->end(), _feats);
}
}
if(!_feats.empty())
_begin->_m_ind = 0;
return _feats.size();
}
int
FeatureVector::eval_cross_blk(int size, Function *f, bool idioms, bool operands) {
_feats.clear();
(*_begin) = (*_end);
Function::blocklist::iterator bit = f->blocks().begin();
for( ; bit != f->blocks().end(); ++bit) {
Block * b = *bit;
Block::Insns insns;
b->getInsns(insns);
for (auto iit = insns.begin(); iit != insns.end(); ++iit) {
if(idioms)
iflookup.lookup_cross_blk(size, f, b, iit->first,_feats);
if(operands)
oflookup.lookup(size, f, iit->first, b->end(), _feats);
}
}
if(!_feats.empty())
_begin->_m_ind = 0;
return _feats.size();
}
bool
FeatureVector::hasmore(int index) {
return index < ((int)_feats.size())-1;
}
Feature *
FeatureVector::get(int index) {
if(index < (int)_feats.size())
return _feats[index];
else
return NULL;
}