Example #1
0
bool Architecture::FormatFunction(
    Document      const& rDoc,
    Address       const& rAddr,
    Function      const& rFunc,
    PrintData          & rPrintData) const
{
    auto FuncLabel = rDoc.GetLabelFromAddress(rAddr);

    if (rFunc.GetSize() != 0 && rFunc.GetInstructionCounter() != 0)
    {
        std::ostringstream oss;
        oss
                << std::hex << std::showbase << std::left
                << "; size=" << rFunc.GetSize()
                << ", insn_cnt=" << rFunc.GetInstructionCounter();

        rPrintData.AppendComment(oss.str());
    }
    else
        rPrintData.AppendComment("; imported");

    Id CurId;
    if (!rDoc.RetrieveDetailId(rAddr, 0, CurId))
        return true;

    FunctionDetail FuncDtl;
    if (!rDoc.GetFunctionDetail(CurId, FuncDtl))
        return true;

    rPrintData.AppendNewLine().AppendSpace(2).AppendComment(";").AppendSpace();

    auto const& RetType = FuncDtl.GetReturnType();

    std::string FuncName;
    Label CurLbl = rDoc.GetLabelFromAddress(rAddr);
    if (CurLbl.GetType() == Label::Unknown)
        FuncName = FuncDtl.GetName();
    else
        FuncName = CurLbl.GetName();

    FormatTypeDetail(RetType, rPrintData);
    rPrintData.AppendSpace().AppendLabel(FuncName).AppendOperator("(");

    bool FirstParam = true;
    auto const& Params = FuncDtl.GetParameters();
    for (auto const& Param : Params)
    {
        if (FirstParam)
            FirstParam = false;
        else
            rPrintData.AppendOperator(",").AppendSpace();

        FormatTypeDetail(Param.GetType(), rPrintData);
        rPrintData.AppendSpace().AppendLabel(Param.GetValue().GetName());
    }

    rPrintData.AppendOperator(");");

    return true;
}
Example #2
0
bool Architecture::FormatInstruction(
    Document      const& rDoc,
    Address       const& rAddr,
    Instruction   const& rInsn,
    PrintData          & rPrintData) const
{
    char const* Sep = nullptr;

    rPrintData.AppendMnemonic(rInsn.GetName());

    std::string OpRefCmt;
    rDoc.GetComment(rAddr, OpRefCmt);

    for (unsigned int i = 0; i < OPERAND_NO; ++i)
    {
        Operand const* pOprd = rInsn.Operand(i);
        if (pOprd == nullptr)
            break;
        if (pOprd->GetType() == O_NONE)
            break;

        if (Sep != nullptr)
            rPrintData.AppendOperator(Sep).AppendSpace();
        else
            Sep = ",";

        if (!FormatOperand(rDoc, rAddr, rInsn, *pOprd, i, rPrintData))
            return false;

        Address OpRefAddr;
        if (OpRefCmt.empty() && rInsn.GetOperandReference(rDoc, i, rAddr, OpRefAddr))
        {
            Id OpId;
            if (rDoc.RetrieveDetailId(OpRefAddr, 0, OpId))
            {
                FunctionDetail FuncDtl;
                if (rDoc.GetFunctionDetail(OpId, FuncDtl))
                {
                    // TODO: provide helper to avoid this...
                    u16 CmtOff = static_cast<u16>(rPrintData.GetCurrentText().length()) - 6 - 1 - rAddr.ToString().length();

                    rPrintData.AppendSpace().AppendComment(";").AppendSpace();
                    FormatTypeDetail(FuncDtl.GetReturnType(), rPrintData);
                    rPrintData.AppendSpace().AppendLabel(FuncDtl.GetName()).AppendOperator("(");

                    if (!FuncDtl.GetParameters().empty())
                        rPrintData.AppendNewLine().AppendSpace(CmtOff).AppendComment(";").AppendSpace(3);

                    bool FirstParam = true;
                    for (auto const& rParam : FuncDtl.GetParameters())
                    {
                        if (FirstParam)
                            FirstParam = false;
                        else
                            rPrintData.AppendOperator(",").AppendNewLine().AppendSpace(CmtOff).AppendComment(";").AppendSpace(3);
                        FormatTypeDetail(rParam.GetType(), rPrintData);
                        rPrintData.AppendSpace().AppendLabel(rParam.GetValue().GetName());
                    }
                    rPrintData.AppendOperator(");");
                }
            }
        }
    }

    return true;
}