1 #include <featureTests.h>
2 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
3 #include "sage3basic.h"
6 #include "AsmUnparser.h"
7 #include "Diagnostics.h"
13 using namespace Diagnostics;
18 ASSERT_not_null(insn);
24 case x86_branch_prediction_none:
break;
25 case x86_branch_prediction_taken: result +=
",pt";
break;
26 case x86_branch_prediction_not_taken: result +=
",pn";
break;
28 ASSERT_not_reachable(
"bad x86 branch prediction: " +
38 std::string unparseX86Register(
SgAsmInstruction *insn, RegisterDescriptor reg,
const RegisterDictionary *registers) {
40 registers = RegisterDictionary::dictionary_amd64();
41 std::string name = registers->lookup(reg);
43 name = AsmUnparser::invalid_register(insn, reg, registers);
47 std::string unparseX86Register(RegisterDescriptor reg,
const RegisterDictionary *registers) {
48 return unparseX86Register(NULL, reg, registers);
51 static std::string x86ValToLabel(uint64_t val,
const AsmUnparser::LabelMap *labels)
56 AsmUnparser::LabelMap::const_iterator li = labels->find(val);
57 if (li==labels->end())
63 static std::string x86TypeToPtrName(
SgAsmType* ty) {
65 mlog[
ERROR] <<
"x86TypeToPtrName: null type\n";
70 switch (it->get_nBits()) {
71 case 8:
return "byte";
72 case 16:
return "word";
73 case 32:
return "dword";
74 case 64:
return "qword";
77 switch (ft->get_nBits()) {
78 case 32:
return "float";
79 case 64:
return "double";
80 case 80:
return "ldouble";
87 ASSERT_not_reachable(
"unhandled type: " + ty->
toString());
90 std::string unparseX86Expression(
SgAsmExpression *expr,
const AsmUnparser::LabelMap *labels,
91 const RegisterDictionary *registers,
bool leaMode) {
92 std::string result =
"";
93 if (expr == NULL)
return "BOGUS:NULL";
96 case V_SgAsmBinaryAdd:
97 result = unparseX86Expression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers,
false) +
" + " +
98 unparseX86Expression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, registers,
false);
101 case V_SgAsmBinarySubtract:
102 result = unparseX86Expression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers,
false) +
" - " +
103 unparseX86Expression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, registers,
false);
106 case V_SgAsmBinaryMultiply:
107 result = unparseX86Expression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, registers,
false) +
"*" +
108 unparseX86Expression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, registers,
false);
111 case V_SgAsmMemoryReferenceExpression: {
114 result += x86TypeToPtrName(mr->
get_type()) +
" " +
117 result +=
"[" + unparseX86Expression(mr->
get_address(), labels, registers,
false) +
"]";
121 case V_SgAsmDirectRegisterExpression: {
122 SgAsmInstruction *insn = SageInterface::getEnclosingNode<SgAsmInstruction>(expr);
124 result = unparseX86Register(insn, rr->get_descriptor(), registers);
128 case V_SgAsmIndirectRegisterExpression: {
129 SgAsmInstruction *insn = SageInterface::getEnclosingNode<SgAsmInstruction>(expr);
131 result = unparseX86Register(insn, rr->get_descriptor(), registers);
132 if (!result.empty() &&
'0'==result[result.size()-1])
133 result = result.substr(0, result.size()-1);
138 case V_SgAsmIntegerValueExpression: {
140 ASSERT_not_null(ival);
145 value > 0x0000ffff && value < 0xffff0000) {
156 label =x86ValToLabel(value, labels);
165 ASSERT_not_reachable(
"invalid x86 expression: " + expr->
class_name());
174 std::string unparseX86Expression(
SgAsmExpression *expr,
const AsmUnparser::LabelMap *labels,
175 const RegisterDictionary *registers) {
179 insn = isSgAsmX86Instruction(node);
181 ASSERT_not_null(insn);
182 return unparseX86Expression(expr, labels, registers, insn->
get_kind()==x86_lea);
SgAsmType * get_type() const
Property: Type of expression.
ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
SgAsmExpression * get_segment() const
Property: Optional memory segment register.
Base class for machine instructions.
ROSE_DLL_API Sawyer::Message::Facility mlog
Diagnostic facility for the ROSE library as a whole.
const std::string & get_comment() const
Property: Comment.
SgAsmIntegerType * buildTypeU64()
64-bit unsigned.
Rose::BinaryAnalysis::X86BranchPrediction get_branchPrediction() const
Property: An enum constant describing branch prediction.
Error messages that indicate an abnormal situation from which the program was able to at least partia...
SgAsmExpression * get_address() const
Property: Memory address expression.
Main namespace for the ROSE library.
virtual VariantT variantT() const
returns new style SageIII enum values
const std::string & get_mnemonic() const
Property: Instruction mnemonic string.
Reference to memory locations.
virtual std::string toString() const
Convert a type to a string.
ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment)
Append an assembly comment to a string.
std::string get_label(bool quiet=false) const
Returns a label for the value.
Expression representing a machine register.
Base class for integer values.
bool get_lockPrefix() const
Property: Whether the x86 lock prefix was present.
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.
Represents one Intel x86 machine instruction.
Base class for expressions.
virtual std::string class_name() const
returns a string representing the class name
Base class for binary types.
Registers accessed indirectly.
SgNode * get_parent() const
Access function for parent node.
Rose::BinaryAnalysis::X86InstructionKind get_kind() const
Property: Instruction kind.
uint64_t get_absoluteValue(size_t nbits=0) const
Returns the current absolute value zero filled to 64 bits.
SgAsmVectorType * buildTypeVector(size_t, SgAsmType *)
Fixed-size, packed array.
Base class for vector types.