SgAsmRiscOperation* buildRiscOperation(SgAsmRiscOperation::RiscOperator op) { SgAsmRiscOperation *retval = new SgAsmRiscOperation(op); SgAsmExprListExp *operands = new SgAsmExprListExp; operands->set_parent(retval); retval->set_operands(operands); return retval; }
/* Returns true if the instruction modifies the instruction pointer (r15). */ static bool modifies_ip(SgAsmArmInstruction *insn) { switch (insn->get_kind()) { /* Branch instructions */ case arm_b: case arm_bl: case arm_blx: case arm_bx: case arm_bxj: return true; /* Comparison instructions */ case arm_cmn: case arm_cmp: case arm_teq: case arm_tst: return false; /* Load multiple registers instructions. Second argument is the set of registers to load. If the instruction * pointer (r15) can be one of them. */ case arm_ldm: case arm_ldmda: case arm_ldmdb: case arm_ldmia: case arm_ldmib: { const std::vector<SgAsmExpression*> &exprs = insn->get_operandList()->get_operands(); ROSE_ASSERT(exprs.size()>=2); SgAsmExprListExp *elist = isSgAsmExprListExp(exprs[1]); if (!elist) { SgAsmUnaryArmSpecialRegisterList *rlist = isSgAsmUnaryArmSpecialRegisterList(exprs[1]); ROSE_ASSERT(rlist); elist = isSgAsmExprListExp(rlist->get_operand()); ROSE_ASSERT(elist); } for (size_t i=0; i<elist->get_expressions().size(); i++) { SgAsmRegisterReferenceExpression *reg = isSgAsmRegisterReferenceExpression(elist->get_expressions()[i]); ROSE_ASSERT(reg); if (reg->get_descriptor().get_major()==arm_regclass_gpr && reg->get_descriptor().get_minor()==15) { return true; } } return false; } /* Interrupt-causing instructions */ case arm_bkpt: case arm_swi: case arm_undefined: return true; /* Other instructions modify the instruction pointer if it's the first (destination) argument. */ default: { const std::vector<SgAsmExpression*> &exprs = insn->get_operandList()->get_operands(); if (exprs.size()>=1) { SgAsmRegisterReferenceExpression *rre = isSgAsmRegisterReferenceExpression(exprs[0]); if (rre && rre->get_descriptor().get_major()==arm_regclass_gpr && rre->get_descriptor().get_minor()==15) { return true; } } } } return false; }
/* Helper function for unparseArmExpression(SgAsmExpression*) * * If this function is called for an EXPR node that cannot appear at the top of an ARM instruction operand tree then the node * might create two strings: the primary expression return value and an additional string returned through the SUFFIX * argument. What to do with the additional string depends on layers higher up in the call stack. * * The sign will be prepended to the result if EXPR is a value expression of some sort. */ static std::string unparseArmExpression(SgAsmExpression* expr, const AsmUnparser::LabelMap *labels, ArmSignForExpressionUnparsing sign, std::string *suffix=NULL) { std::string result, extra; if (!isSgAsmValueExpression(expr)) { result += unparseArmSign(sign); } switch (expr->variantT()) { case V_SgAsmBinaryMultiply: ROSE_ASSERT (isSgAsmByteValueExpression(isSgAsmBinaryExpression(expr)->get_rhs())); result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, arm_sign_none) + "*" + StringUtility::numberToString(isSgAsmByteValueExpression(isSgAsmBinaryExpression(expr)->get_rhs())); break; case V_SgAsmBinaryLsl: result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, arm_sign_none) + ", lsl " + unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, arm_sign_none); break; case V_SgAsmBinaryLsr: result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, arm_sign_none) + ", lsr " + unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, arm_sign_none); break; case V_SgAsmBinaryAsr: result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, arm_sign_none) + ", asr " + unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, arm_sign_none); break; case V_SgAsmBinaryRor: result = unparseArmExpression(isSgAsmBinaryExpression(expr)->get_lhs(), labels, arm_sign_none) + ", ror " + unparseArmExpression(isSgAsmBinaryExpression(expr)->get_rhs(), labels, arm_sign_none); break; case V_SgAsmUnaryRrx: result = unparseArmExpression(isSgAsmUnaryExpression(expr)->get_operand(), labels, arm_sign_none) + ", rrx"; break; case V_SgAsmUnaryArmSpecialRegisterList: result += unparseArmExpression(isSgAsmUnaryExpression(expr)->get_operand(), labels, arm_sign_none) + "^"; break; case V_SgAsmExprListExp: { SgAsmExprListExp* el = isSgAsmExprListExp(expr); const std::vector<SgAsmExpression*>& exprs = el->get_expressions(); result += "{"; for (size_t i = 0; i < exprs.size(); ++i) { if (i != 0) result += ", "; result += unparseArmExpression(exprs[i], labels, arm_sign_none); } result += "}"; break; } case V_SgAsmBinaryAdd: { /* This node cannot appear at the top of an ARM instruction operand tree */ SgAsmBinaryExpression *e = isSgAsmBinaryExpression(expr); result += unparseArmExpression(e->get_lhs(), labels, arm_sign_none) + ", " + unparseArmExpression(e->get_rhs(), labels, arm_sign_plus); break; } case V_SgAsmBinarySubtract: { /* This node cannot appear at the top of an ARM instruction operand tree */ SgAsmBinaryExpression *e = isSgAsmBinaryExpression(expr); result += unparseArmExpression(e->get_lhs(), labels, arm_sign_none) + ", " + unparseArmExpression(e->get_rhs(), labels, arm_sign_minus); break; } case V_SgAsmBinaryAddPreupdate: { /* This node cannot appear at the top of an ARM instruction operand tree */ SgAsmBinaryExpression *e = isSgAsmBinaryExpression(expr); result += unparseArmExpression(e->get_lhs(), labels, arm_sign_none) + ", " + unparseArmExpression(e->get_rhs(), labels, arm_sign_plus); extra = "!"; break; } case V_SgAsmBinarySubtractPreupdate: { /* This node cannot appear at the top of an ARM instruction operand tree */ SgAsmBinaryExpression *e = isSgAsmBinaryExpression(expr); result += unparseArmExpression(e->get_lhs(), labels, arm_sign_none) + ", " + unparseArmExpression(e->get_rhs(), labels, arm_sign_minus); extra = "!"; break; } case V_SgAsmBinaryAddPostupdate: { /* Two styles of syntax depending on whether this is at top-level or inside a memory reference expression. */ SgAsmBinaryExpression *e = isSgAsmBinaryExpression(expr); if (suffix) { result += unparseArmExpression(e->get_lhs(), labels, arm_sign_none); extra = ", " + unparseArmExpression(e->get_rhs(), labels, arm_sign_plus); } else { /* Used by LDM* and STM* instructions outside memory reference expressions. RHS is unused. */ result = unparseArmExpression(e->get_lhs(), labels, arm_sign_none) + "!"; } break; } case V_SgAsmBinarySubtractPostupdate: { /* Two styles of syntax depending on whether this is at top-level or inside a memory reference expression. */ SgAsmBinaryExpression *e = isSgAsmBinaryExpression(expr); if (suffix) { result += unparseArmExpression(e->get_lhs(), labels, arm_sign_none); extra = ", " + unparseArmExpression(e->get_rhs(), labels, arm_sign_minus); } else { /* Used by LDM* and STM* instructions outside memory reference expressions. RHS is unused. */ result += unparseArmExpression(e->get_lhs(), labels, arm_sign_none) + "!"; } break; } case V_SgAsmMemoryReferenceExpression: { SgAsmMemoryReferenceExpression* mr = isSgAsmMemoryReferenceExpression(expr); SgAsmExpression* addr = mr->get_address(); switch (addr->variantT()) { case V_SgAsmRegisterReferenceExpression: case V_SgAsmBinaryAdd: case V_SgAsmBinarySubtract: case V_SgAsmBinaryAddPreupdate: case V_SgAsmBinarySubtractPreupdate: case V_SgAsmBinaryAddPostupdate: case V_SgAsmBinarySubtractPostupdate: break; default: ROSE_ASSERT (!"Bad addressing mode"); } std::string suffix; result += "[" + unparseArmExpression(addr, labels, arm_sign_none, &suffix) + "]"; result += suffix; break; } case V_SgAsmArmRegisterReferenceExpression: result += unparseArmRegister(isSgAsmArmRegisterReferenceExpression(expr)); break; case V_SgAsmByteValueExpression: case V_SgAsmWordValueExpression: case V_SgAsmDoubleWordValueExpression: { SgAsmValueExpression *ve = isSgAsmValueExpression(expr); assert(ve!=NULL); uint64_t v = SageInterface::getAsmConstant(ve); result += "#" + unparseArmSign(sign) + StringUtility::numberToString(v); if (labels && v!=0) { AsmUnparser::LabelMap::const_iterator li=labels->find(v); if (li!=labels->end()) result += "<" + li->second + ">"; } break; } default: { std::cerr << "Unhandled expression kind " << expr->class_name() << std::endl; ROSE_ASSERT (false); } } /* The extra data should be passed back up the call stack so it can be inserted into the ultimate return string. We can't * insert it here because the string can't be generated strictly left-to-right. If "suffix" is the null pointer then the * caller isn't expecting a suffix and we'll have to just do our best -- the result will not be valid ARM assembly. */ if (extra.size()>0 && !suffix) result = "\"" + result + "\" and \"" + extra + "\""; if (suffix) *suffix = extra; if (expr->get_replacement() != "") { result += " <" + expr->get_replacement() + ">"; } return result; }