-
Notifications
You must be signed in to change notification settings - Fork 1
/
instruction.cpp
188 lines (164 loc) · 5.06 KB
/
instruction.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
//
// Copyright (c) 2002 David Eriksson <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// $Id: instruction.cpp,v 1.8 2007/01/30 09:49:30 wjhengeveld Exp $
//
// C++ headers
//
#include <stack>
//
// IDA headers
//
#include <ida.hpp>
//
// Local headers
//
#include "instruction.hpp"
#include "analysis.hpp"
/**
* Apply InstructionVisitor on a list of nodes
*/
void Accept(Node_list &nodes, InstructionVisitor &visitor)/*{{{*/
{
for (Node_list::iterator i = nodes.begin();
i != nodes.end();
i++)
{
visitor.NodeBegin(*i);
Accept((**i).Instructions(), visitor);
visitor.NodeEnd();
}
}/*}}}*/
/**
* Apply visitor on a list of instructions
*/
void Accept(Instruction_list &instructions, InstructionVisitor &visitor)/*{{{*/
{
for (Instruction_list::iterator i = instructions.begin();
i != instructions.end();
i++)
{
(**i).Accept(visitor);
}
}/*}}}*/
const int BoolArray::POWER_OF_2[BoolArray::SIZE] =/*{{{*/
{
1 << 0, 1 << 1, 1 << 2, 8, 16, 32, 64, 128,
256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 21
};/*}}}*/
/* Find DU-chains {{{ */
class FindDefintionUseChainsHelper
{
public:
FindDefintionUseChainsHelper(Instruction_list &instructions)
: mInstructions(instructions)
{}
void OnDefinedRegister(Instruction_list::iterator item,
Instruction_ptr instr, int reg)
{
// XXX: not for register variables
for (item++; item != mInstructions.end(); item++)
{
Instruction_ptr next = *item;
// Used here?
if (next->Uses().Get(reg))
{
instr->AddToDuChain(reg, next->Address());
}
// Defined here?
if (next->Definitions().Get(reg))
{
break;
}
}
if (mInstructions.end() == item)
{
// We got to the end of the instruction list,
// that means this is the last defintion of this register!
// TODO: check that it is in liveout too!
instr->SetLastDefinition(reg);
}
}
void OnInstruction(Instruction_list::iterator item)
{
Instruction_ptr instr = *item;
BoolArray &def = instr->Definitions();
for (int reg = 0; reg < BoolArray::SIZE; reg++)
{
// Is register i is defined here?
if (def.Get(reg))
{
OnDefinedRegister(item, instr, reg);
}
}
}
private:
Instruction_list &mInstructions;
};
void Instruction::FindDefintionUseChains(Instruction_list &instructions)
{
FindDefintionUseChainsHelper helper(instructions);
for (Instruction_list::iterator item = instructions.begin();
item != instructions.end();
item++)
{
helper.OnInstruction(item);
#if 1
Instruction_ptr instr = *item;
msg("%p DU chain:\n", instr->Address());
for (RegisterToAddress_map::iterator du = instr->mDuChain.begin();
du != instr->mDuChain.end();
du++)
{
msg("\t%s -> %p\n", Register::Name(du->first).c_str(), du->second);
}
#endif
}
}/*}}}*/
// outputs:
// { var:[addr, addr], var:[addr, addr] }
std::ostream &operator<<(std::ostream &os, const RegisterToAddress_map &vs)
{
unsigned short reg = 0xffff;
bool bFirstAddr = true;
os << "{ ";
for (RegisterToAddress_map::const_iterator i = vs.begin(); i != vs.end(); ++i)
{
if (reg != 0xffff && reg != (*i).first)
os << "], ";
if (reg == 0xffff || reg != (*i).first)
{
reg = (*i).first;
os << "R" << reg;
os << "[";
bFirstAddr = true;
}
if (!bFirstAddr)
os << ", ";
os << boost::format("%08lx") % (*i).second;
bFirstAddr = false;
}
if (!bFirstAddr)
os << "] ";
os << "}";
return os;
}