Files
revng-revng/lib/Yield/Verify.cpp
Alessandro Di Federico f68b7866b3 Introduce BasicBlockID
This commit introduces `BasicBlockID` as the unique identifier for a
`efa::BasicBlock` into the CFG. A `BasicBlockID` is defined by a
`MetaAddress` plus an incremental integer. This enables us to have
multiple instances of the same block in a single function, which is
particularly useful when inlining multiple times the same function.

Apart from this, the commit also does the following:

* It drops representing `MetaAddress`es a `structs` in the IR. This created
  several issues related to ABI. We now represent them as strings.

* It defines more functions in `support.h`, instead of defining prototypes
  by hand in `CodeGenerator.cpp` and the like. Specifically, `unknownPC`
  and `raise_exception_helper`. We also introduce a C "constructor" for
  `PlainMetaAddress`.

* It significantly reduces the API of `GeneratedCodeBasicInfo`, which
  was supposed to be put on a diet since a long time.  Specifically,
  many jump target related methods have been moved to free functions in
  `IRHelpers.h`.  Also `GCBI::getSuccessors` has been pushed into its
  only user, `PruneRetSuccessors`, to prevent further usage of a
  deprecated API. In the future, it would be nice to drop it entirely.

* It introduces `efa::BasicBlock::InlinedFrom`.

* Introduce an enum to represent named argument indices for `newpc`.
  This enables us to more effectively manipulate its argument list.

* It improves the verification and error reporting for
  `efa::FunctionMetadata`.

* Update tests.

This commit is preliminary to another piece of work to improve the
generality of inlining beyond the simple "fake function" scenario, for
which the feature was originally conceived.
2023-02-23 14:51:10 +01:00

93 lines
3.0 KiB
C++

/// \file Verify.cpp
/// \brief
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "revng/Yield/BasicBlock.h"
#include "revng/Yield/CallEdge.h"
#include "revng/Yield/Function.h"
#include "revng/Yield/Instruction.h"
#include "revng/Yield/Tag.h"
bool yield::Tag::verify(model::VerifyHelper &VH) const {
if (Type() == TagType::Invalid)
return VH.fail("The type of this tag is not valid.");
if (From() == std::string::npos)
return VH.fail("This tag doesn't have a starting point.");
if (To() == std::string::npos)
return VH.fail("This tag doesn't have an ending point.");
if (From() >= To())
return VH.fail("This tag doesn't have a positive length.");
return true;
}
bool yield::Instruction::verify(model::VerifyHelper &VH) const {
if (Address().isInvalid())
return VH.fail("An instruction has to have a valid address.");
if (Disassembled().empty())
return VH.fail("The disassembled view of an instruction cannot be empty.");
if (RawBytes().empty())
return VH.fail("An instruction has to be at least one byte big.");
for (const auto &Tag : Tags()) {
if (!Tag.verify(VH))
return VH.fail("Tag verification failed");
if (Tag.From() >= Disassembled().size()
|| Tag.To() >= Disassembled().size())
return VH.fail("Tag boundaries must not exceed the size of the text.");
}
return true;
}
bool yield::BasicBlock::verify(model::VerifyHelper &VH) const {
if (not ID().isValid())
return VH.fail("A basic block has to have a valid start address.");
if (End().isInvalid())
return VH.fail("A basic block has to have a valid end address.");
if (Instructions().empty())
return VH.fail("A basic block has to store at least a single instruction.");
MetaAddress PreviousAddress = MetaAddress::invalid();
for (const auto &Instruction : Instructions()) {
if (!Instruction.verify(VH))
return VH.fail("Instuction verification failed.");
if (PreviousAddress.isValid() && Instruction.Address() >= PreviousAddress) {
return VH.fail("Instructions must be strongly ordered and their size "
"must be bigger than zero.");
}
PreviousAddress = Instruction.Address();
}
if (PreviousAddress.isInvalid() || PreviousAddress >= End()) {
return VH.fail("The size of the last instruction must be bigger than "
"zero.");
}
if (HasDelaySlot() && Instructions().size() < 2) {
return VH.fail("A basic block with a delay slot must contain at least two "
"instructions.");
}
return true;
}
bool yield::Function::verify(model::VerifyHelper &VH) const {
if (Entry().isInvalid())
return VH.fail("A function has to have a valid entry point.");
if (ControlFlowGraph().empty())
return VH.fail("A function has to store at least a single basic block.");
for (const auto &BasicBlock : ControlFlowGraph())
if (!BasicBlock.verify(VH))
return VH.fail("Basic block verification failed.");
return true;
}