Beispiel #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;
}
Beispiel #2
0
void Section::Optimize(AsmFile *fil)
{
    AsmExpr::SetSection(this);
    bool done = false;
    while (!done)
    {
        int pc = 0;
        done = true;
        for (int i=0; i < instructions.size(); i++)
        {
            if (instructions[i]->IsLabel())
            {
                Label *l = instructions[i]->GetLabel();
                if (l)
                {
                    l->SetOffset(pc);
                    labels[l->GetName()] = pc;
                }
            }
            else
            {
                int n = instructions[i]->GetSize() ;
                instructions[i]->SetOffset(pc);
                instructions[i]->Optimize(pc, false);
                int m = instructions[i]->GetSize() ;
                pc += m;
                if (n != m)
                    done = false;
            }
        }
    }
    int pc = 0;
    for (int i=0; i < instructions.size(); i++)
    {
        instructions[i]->Optimize(pc, true);
        pc += instructions[i]->GetSize();
    }
}
Beispiel #3
0
void Document::AddLabel(Address const& rAddr, Label const& rLabel, bool Force)
{
  if (rLabel.GetName().empty() && Force)
  {
    RemoveLabel(rAddr);
    return;
  }

  Label OldLbl, NewLbl = rLabel;
  Address Addr;
  if (m_spDatabase->GetLabelAddress(NewLbl, Addr))
  {
    do NewLbl.IncrementVersion();
    while (m_spDatabase->GetLabelAddress(NewLbl, Addr));
  }

  if (m_spDatabase->GetLabel(rAddr, OldLbl) == true)
  {
    if (OldLbl.IsAutoGenerated())
      Force = true;

    if (!Force)
      return;

    if (OldLbl == rLabel)
      return;

    if (!m_spDatabase->RemoveLabel(rAddr))
      return;

    m_LabelUpdatedSignal(rAddr, OldLbl, true);
  }

  m_spDatabase->AddLabel(rAddr, NewLbl);
  m_LabelUpdatedSignal(rAddr, NewLbl, false);
  m_DocumentUpdatedSignal();
}
Beispiel #4
0
void AsmFile::DoLabel(std::string &name, int lineno)
{
    NeedSection();
    Label *label;
    if (caseInsensitive)
    {
        for (int i=0; i < name.size(); i++)
            name[i] = toupper(name[i]);
    }
    std::string realName = name;
    bool nl = false;
    if (name.size() > 2)
    {
        if (name[0] == '.' && name[1] == '.' && name[2] == '@')
        {
            if (name.size() == 3)
                throw new std::runtime_error("Malformed non-local label");
            nl = true;
        }
    }
    if (!nl && name[0] == '.')
    {
        if (name == "..start")
        {
            if (startupSection)
            {
                throw new std::runtime_error("Multiple start addresses specified");
            }
            label = new Label(name, labels.size(), currentSection->GetSect());
            startupSection = currentSection;
        }
        else
        {
            if (currentLabel)
            {
                realName = currentLabel->GetName() + name;
            }
        }
    } 
    if (labels[realName] != NULL)
    {
        if (realName != "..start")
        {
            throw new std::runtime_error(std::string("Label '") + name + "' already exists.");
        }
    }
    else
    {
        if (inAbsolute)
        {
            label = new Label(realName, labels.size(), 0);
            label->SetOffset(absoluteValue);
            AsmExpr::SetEqu(realName, new AsmExprNode(absoluteValue));
            if (lineno >= 0)
                listing.Add(label, lineno, preProcessor.InMacro());
        }
        else
        {
            label = new Label(realName, labels.size(), currentSection->GetSect()-1);
        }
        if (name[0] != '.')
        {
            currentLabel = label;
            AsmExpr::SetCurrentLabel(label->GetName());
        }
        thisLabel = label;
        labels[realName] = label;
        numericLabels.push_back(label);
        if (!inAbsolute)
            currentSection->InsertLabel(label);
//		if (lineno >= 0)
//			listing.Add(thisLabel, lineno, preProcessor.InMacro());
        if (realName == "..start")
            startupLabel = label;
    }
    if (GetKeyword() == Lexer::colon)
    {
        NextToken();
        thisLabel = NULL;
    }
}
//--------------------------------------------------------------------------------------------------------------
// AddLabel(). Adds a new label pLabel to the mLabels dictionary. The key is pLabel.GetName() and the value
// is pLabel. Hint: look at DataSegment::AddVariable().
//--------------------------------------------------------------------------------------------------------------
void TextSegment::AddLabel(Label const& pLabel)
{
    mLabels.Add(pLabel.GetName(), pLabel);
}