2 * Copyright (c) 1996-2011 Barton P. Miller
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.
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.
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.
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.
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
31 #include "ExpressionConversionVisitor.h"
34 #include "Immediate.h"
35 #include "BinaryFunction.h"
36 #include "Dereference.h"
40 #include "../rose/SgAsmExpression.h"
42 using namespace Dyninst;
43 using namespace Dyninst::InstructionAPI;
45 //#include "../rose/x86InstructionSemantics.h"
46 //#include "../rose/powerpcInstructionSemantics.h"
48 using namespace Dyninst;
49 using namespace DataflowAPI;
51 void ExpressionConversionVisitor::visit(InstructionAPI::Immediate* immed) {
54 const Result &value = immed->eval();
56 // TODO rose doesn't distinguish signed/unsigned within the value itself,
57 // only at operations?
59 // TODO rose doesn't handle large values (XMM?)
61 // build different kind of rose value object based on type
65 roseExpression = new SgAsmByteValueExpression(value.val.u8val);
69 roseExpression = new SgAsmWordValueExpression(value.val.u16val);
73 roseExpression = new SgAsmDoubleWordValueExpression(value.val.u32val);
77 // This only happens with far calls. ROSE appears to be set up to
78 // expect a 32-bit absolute destination (or doesn't handle far call at
79 // all), so give it what it wants.
80 roseExpression = new SgAsmDoubleWordValueExpression(value.val.u32val);
84 roseExpression = new SgAsmQuadWordValueExpression(value.val.u64val);
87 roseExpression = new SgAsmSingleFloatValueExpression(value.val.floatval);
90 roseExpression = new SgAsmDoubleFloatValueExpression(value.val.dblval);
93 roseExpression = NULL;
97 m_stack.push_front(roseExpression);
101 void ExpressionConversionVisitor::visit(RegisterAST* regast) {
104 m_stack.push_front(archSpecificRegisterProc(regast, addr));
105 roseExpression = m_stack.front();
109 void ExpressionConversionVisitor::visit(Dereference* deref) {
111 assert(m_stack.size());
112 SgAsmExpression *toderef = m_stack.front();
114 if(toderef == NULL) {
115 roseExpression = NULL;
120 // TODO fix some mismatched types?
122 switch (deref->eval().type)
126 type = new SgAsmTypeByte();
130 type = new SgAsmTypeWord();
134 type = new SgAsmTypeDoubleWord();
138 type = new SgAsmTypeQuadWord();
141 type = new SgAsmTypeSingleFloat();
144 type = new SgAsmTypeDoubleFloat();
152 SgAsmExpression *segReg = makeSegRegExpr();
153 SgAsmMemoryReferenceExpression* result = new SgAsmMemoryReferenceExpression(toderef, segReg);
154 result->set_type(type);
155 roseExpression = result;
158 SgAsmExpression* ExpressionConversionVisitor::archSpecificRegisterProc(InstructionAPI::RegisterAST* regast, uint64_t addr)
160 MachRegister machReg = regast->getID();
161 if(machReg.isPC()) return NULL;
169 MachRegister machReg = regast->getID();
171 // ideally this would be symbolic
172 SgAsmExpression *constAddrExpr = new SgAsmDoubleWordValueExpression(addr);
173 return constAddrExpr;
175 machReg.getROSERegister(regClass, regNum, regPos);
177 return new SgAsmx86RegisterReferenceExpression((X86RegisterClass) regClass,
179 (X86PositionInRegister) regPos);
185 int regGran = powerpc_condreggranularity_whole;
187 machReg.getROSERegister(regClass, regNum, regGran);
189 return new SgAsmPowerpcRegisterReferenceExpression((PowerpcRegisterClass) regClass,
191 (PowerpcConditionRegisterAccessGranularity) regGran);
200 SgAsmExpression* ExpressionConversionVisitor::makeSegRegExpr()
202 if (arch == Arch_x86) {
203 return new SgAsmx86RegisterReferenceExpression(x86_regclass_segment,
204 x86_segreg_none, x86_regpos_all);
211 /////////////// Visitor class /////////////////
213 void ExpressionConversionVisitor::visit(BinaryFunction* binfunc) {
214 assert(m_stack.size() >= 2);
215 SgAsmExpression *rhs = m_stack.front();
217 SgAsmExpression *lhs = m_stack.front();
219 // If the RHS didn't convert, that means it should disappear
220 // And we are just left with the LHS
222 roseExpression = NULL;
225 roseExpression = lhs;
228 roseExpression = rhs;
231 // now build either add or multiply
232 if (binfunc->isAdd())
233 roseExpression = new SgAsmBinaryAdd(lhs, rhs);
234 else if (binfunc->isMultiply())
235 roseExpression = new SgAsmBinaryMultiply(lhs, rhs);
236 else roseExpression = NULL; // error
238 m_stack.push_front(roseExpression);