1 #include <rosePublicConfig.h>
2 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
3 #include "sage3basic.h"
6 #include "AsmUnparser_compat.h"
8 #include "Diagnostics.h"
11 using namespace Diagnostics;
17 registers = RegisterDictionary::dictionary_arm7();
19 std::string name = registers->lookup(rdesc);
21 SgAsmInstruction *insn = SageInterface::getEnclosingNode<SgAsmInstruction>(reg);
22 name = AsmUnparser::invalid_register(insn, rdesc, registers);
29 if (dre->get_psr_mask() & 1) name +=
"c";
30 if (dre->get_psr_mask() & 2) name +=
"x";
31 if (dre->get_psr_mask() & 4) name +=
"s";
32 if (dre->get_psr_mask() & 8) name +=
"f";
39 static std::string unparseArmCondition(ArmInstructionCondition cond) {
41 std::string retval = stringifyBinaryAnalysisArmInstructionCondition(cond,
"arm_cond_");
44 std::string retval =
"";
47 ASSERT_require(retval[0]!=
'(');
51 static std::string unparseArmSign(ArmSignForExpressionUnparsing sign) {
53 case arm_sign_none:
return "";
54 case arm_sign_plus:
return "+";
55 case arm_sign_minus:
return "-";
57 ASSERT_not_reachable(
"invalid ARM sign for expression unparsing: " + stringifyArmSignForExpressionUnparsing(sign));
59 return "error in unparseArmSign";
71 static std::string unparseArmExpression(
SgAsmExpression* expr,
const AsmUnparser::LabelMap *labels,
72 const RegisterDictionary *registers,
73 ArmSignForExpressionUnparsing sign, std::string *suffix=NULL)
75 std::string result, extra;
76 if (!isSgAsmValueExpression(expr)) {
77 result += unparseArmSign(sign);
80 case V_SgAsmBinaryMultiply:
81 ASSERT_require(isSgAsmIntegerValueExpression(isSgAsmBinaryExpression(expr)->get_rhs()));
82 result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers, arm_sign_none) +
"*" +
85 case V_SgAsmBinaryLsl:
86 result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers, arm_sign_none) +
", lsl " +
87 unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, registers, arm_sign_none);
89 case V_SgAsmBinaryLsr:
90 result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers, arm_sign_none) +
", lsr " +
91 unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, registers, arm_sign_none);
93 case V_SgAsmBinaryAsr:
94 result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers, arm_sign_none) +
", asr " +
95 unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, registers, arm_sign_none);
97 case V_SgAsmBinaryRor:
98 result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers, arm_sign_none) +
", ror " +
99 unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, registers, arm_sign_none);
101 case V_SgAsmUnaryRrx:
102 result = unparseArmExpression(isSgAsmUnaryExpression(expr)->get_operand(), labels, registers, arm_sign_none) +
105 case V_SgAsmUnaryArmSpecialRegisterList:
106 result += unparseArmExpression(isSgAsmUnaryExpression(expr)->get_operand(), labels, registers, arm_sign_none) +
"^";
108 case V_SgAsmExprListExp: {
112 for (
size_t i = 0; i < exprs.size(); ++i) {
113 if (i != 0) result +=
", ";
114 result += unparseArmExpression(exprs[i], labels, registers, arm_sign_none);
121 case V_SgAsmBinaryAdd: {
124 result += unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none) +
", " +
125 unparseArmExpression(e->
get_rhs(), labels, registers, arm_sign_plus);
129 case V_SgAsmBinarySubtract: {
132 result += unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none) +
", " +
133 unparseArmExpression(e->
get_rhs(), labels, registers, arm_sign_minus);
137 case V_SgAsmBinaryAddPreupdate: {
140 result += unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none) +
", " +
141 unparseArmExpression(e->
get_rhs(), labels, registers, arm_sign_plus);
146 case V_SgAsmBinarySubtractPreupdate: {
149 result += unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none) +
", " +
150 unparseArmExpression(e->
get_rhs(), labels, registers, arm_sign_minus);
155 case V_SgAsmBinaryAddPostupdate: {
159 result += unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none);
160 extra =
", " + unparseArmExpression(e->
get_rhs(), labels, registers, arm_sign_plus);
163 result = unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none) +
"!";
168 case V_SgAsmBinarySubtractPostupdate: {
172 result += unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none);
173 extra =
", " + unparseArmExpression(e->
get_rhs(), labels, registers, arm_sign_minus);
176 result += unparseArmExpression(e->
get_lhs(), labels, registers, arm_sign_none) +
"!";
181 case V_SgAsmMemoryReferenceExpression: {
185 case V_SgAsmDirectRegisterExpression:
186 case V_SgAsmIndirectRegisterExpression:
187 case V_SgAsmBinaryAdd:
188 case V_SgAsmBinarySubtract:
189 case V_SgAsmBinaryAddPreupdate:
190 case V_SgAsmBinarySubtractPreupdate:
191 case V_SgAsmBinaryAddPostupdate:
192 case V_SgAsmBinarySubtractPostupdate:
194 default: ASSERT_not_reachable(
"bad addressing mode: " + stringifyVariantT(addr->
variantT()));
198 result +=
"[" + unparseArmExpression(addr, labels, registers, arm_sign_none, &suffix) +
"]";
203 case V_SgAsmDirectRegisterExpression:
204 result += unparseArmRegister(isSgAsmDirectRegisterExpression(expr), registers);
206 case V_SgAsmIntegerValueExpression: {
212 AsmUnparser::LabelMap::const_iterator li=labels->find(v);
213 if (li!=labels->end())
220 ASSERT_not_reachable(
"unhandled expression kind: " + expr->
class_name());
227 if (extra.size()>0 && !suffix)
228 result =
"\"" + result +
"\" and \"" + extra +
"\"";
238 ASSERT_not_null(insn);
240 std::string cond = unparseArmCondition(insn->
get_condition());
248 std::string unparseArmExpression(
SgAsmExpression *expr,
const AsmUnparser::LabelMap *labels,
249 const RegisterDictionary *registers) {
253 insn = isSgAsmArmInstruction(node);
255 ASSERT_not_null(insn);
259 ASSERT_require(insn->
operand(0) == expr);
261 ASSERT_not_null(tgt);
264 return unparseArmExpression(expr, labels, registers, arm_sign_none);
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
Base class for references to a machine register.
SgAsmExpression * get_lhs() const
Property: Left-hand side operand.
Base class for machine instructions.
const std::string & get_comment() const
Property: Comment.
List of expression nodes.
SgAsmExpression * get_address() const
Property: Memory address expression.
Main namespace for the ROSE library.
const SgAsmExpressionPtrList & get_expressions() const
Property: Pointers to expressions.
virtual VariantT variantT() const
returns new style SageIII enum values
const std::string & get_mnemonic() const
Property: Instruction mnemonic string.
SgAsmExpression * get_rhs() const
Property: Right-hand side operand.
Reference to memory locations.
ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment)
Append an assembly comment to a string.
Expression representing a machine register.
Base class for integer values.
This class represents the base class for all IR nodes within Sage III.
ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits=0)
Convert a virtual address to a string.
size_t get_significantBits() const
Return the number of significant bits in the value.
int get_positionOfConditionInMnemonic() const
Property: Bit position of condition bits in instruction menmonic.
Base class for expressions.
virtual std::string class_name() const
returns a string representing the class name
Represents one ARM machine instruction.
SgNode * get_parent() const
Access function for parent node.
Rose::BinaryAnalysis::ArmInstructionKind get_kind() const
Property: Instruction kind.
size_t nOperands() const
Number of operands.
Rose::BinaryAnalysis::ArmInstructionCondition get_condition() const
Property: Arm instruction condition.
uint64_t get_absoluteValue(size_t nbits=0) const
Returns the current absolute value zero filled to 64 bits.
SgAsmExpression * operand(size_t) const
Nth operand.
Rose::BinaryAnalysis::RegisterDescriptor get_descriptor() const
Property: Descriptor for accessed register.
Base class for binary expressions.
Program status registers.