2015-05-28 02:02:19 +08:00
|
|
|
//===- MIRParser.cpp - MIR serialization format parser implementation -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the class that parses the optional LLVM IR and machine
|
|
|
|
// functions that are stored in MIR files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MIRParser/MIRParser.h"
|
2015-06-23 01:02:30 +08:00
|
|
|
#include "MIParser.h"
|
2015-06-27 00:46:11 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-06-16 04:30:22 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2015-06-27 06:56:48 +08:00
|
|
|
#include "llvm/AsmParser/SlotMapping.h"
|
2015-06-16 04:30:22 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2015-06-25 03:56:10 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2015-05-29 06:41:12 +08:00
|
|
|
#include "llvm/CodeGen/MIRYamlMapping.h"
|
2015-06-20 01:43:07 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2015-06-16 04:30:22 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
2015-06-16 07:07:38 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2015-06-16 04:30:22 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2015-06-20 01:43:07 +08:00
|
|
|
#include "llvm/IR/ValueSymbolTable.h"
|
2015-05-30 01:05:41 +08:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
2015-05-28 02:02:19 +08:00
|
|
|
#include "llvm/Support/SMLoc.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
namespace llvm {
|
2015-05-28 02:02:19 +08:00
|
|
|
|
|
|
|
/// This class implements the parsing of LLVM IR that's embedded inside a MIR
|
|
|
|
/// file.
|
|
|
|
class MIRParserImpl {
|
|
|
|
SourceMgr SM;
|
|
|
|
StringRef Filename;
|
|
|
|
LLVMContext &Context;
|
2015-06-16 04:30:22 +08:00
|
|
|
StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
|
2015-06-27 06:56:48 +08:00
|
|
|
SlotMapping IRSlots;
|
2015-07-10 06:23:13 +08:00
|
|
|
/// Maps from register class names to register classes.
|
|
|
|
StringMap<const TargetRegisterClass *> Names2RegClasses;
|
2015-05-28 02:02:19 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
|
|
|
|
LLVMContext &Context);
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
void reportDiagnostic(const SMDiagnostic &Diag);
|
|
|
|
|
|
|
|
/// Report an error with the given message at unknown location.
|
|
|
|
///
|
|
|
|
/// Always returns true.
|
|
|
|
bool error(const Twine &Message);
|
|
|
|
|
2015-07-09 04:22:20 +08:00
|
|
|
/// Report an error with the given message at the given location.
|
|
|
|
///
|
|
|
|
/// Always returns true.
|
|
|
|
bool error(SMLoc Loc, const Twine &Message);
|
|
|
|
|
2015-07-01 01:55:00 +08:00
|
|
|
/// Report a given error with the location translated from the location in an
|
|
|
|
/// embedded string literal to a location in the MIR file.
|
|
|
|
///
|
|
|
|
/// Always returns true.
|
|
|
|
bool error(const SMDiagnostic &Error, SMRange SourceRange);
|
|
|
|
|
2015-05-29 06:41:12 +08:00
|
|
|
/// Try to parse the optional LLVM module and the machine functions in the MIR
|
|
|
|
/// file.
|
2015-05-28 02:02:19 +08:00
|
|
|
///
|
2015-05-29 06:41:12 +08:00
|
|
|
/// Return null if an error occurred.
|
2015-06-16 04:30:22 +08:00
|
|
|
std::unique_ptr<Module> parse();
|
2015-05-29 06:41:12 +08:00
|
|
|
|
|
|
|
/// Parse the machine function in the current YAML document.
|
|
|
|
///
|
2015-06-16 07:07:38 +08:00
|
|
|
/// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
|
|
|
|
/// A dummy IR function is created and inserted into the given module when
|
|
|
|
/// this parameter is true.
|
|
|
|
///
|
2015-05-29 06:41:12 +08:00
|
|
|
/// Return true if an error occurred.
|
2015-06-16 07:07:38 +08:00
|
|
|
bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
|
2015-05-30 01:05:41 +08:00
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
/// Initialize the machine function to the state that's described in the MIR
|
|
|
|
/// file.
|
|
|
|
///
|
|
|
|
/// Return true if error occurred.
|
|
|
|
bool initializeMachineFunction(MachineFunction &MF);
|
|
|
|
|
2015-06-20 01:43:07 +08:00
|
|
|
/// Initialize the machine basic block using it's YAML representation.
|
|
|
|
///
|
|
|
|
/// Return true if an error occurred.
|
2015-07-08 01:46:43 +08:00
|
|
|
bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
const yaml::MachineBasicBlock &YamlMBB,
|
|
|
|
const PerFunctionMIParsingState &PFS);
|
2015-06-20 01:43:07 +08:00
|
|
|
|
2015-07-11 06:51:20 +08:00
|
|
|
bool
|
|
|
|
initializeRegisterInfo(const MachineFunction &MF,
|
|
|
|
MachineRegisterInfo &RegInfo,
|
|
|
|
const yaml::MachineFunction &YamlMF,
|
|
|
|
DenseMap<unsigned, unsigned> &VirtualRegisterSlots);
|
2015-06-25 03:56:10 +08:00
|
|
|
|
2015-07-16 06:14:49 +08:00
|
|
|
bool initializeFrameInfo(const Function &F, MachineFrameInfo &MFI,
|
2015-07-11 02:13:57 +08:00
|
|
|
const yaml::MachineFunction &YamlMF);
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
|
2015-07-16 07:31:07 +08:00
|
|
|
bool initializeJumpTableInfo(MachineFunction &MF,
|
|
|
|
const yaml::MachineJumpTable &YamlJTI,
|
2015-07-16 07:38:35 +08:00
|
|
|
PerFunctionMIParsingState &PFS);
|
2015-07-16 07:31:07 +08:00
|
|
|
|
2015-05-30 01:05:41 +08:00
|
|
|
private:
|
2015-06-24 06:39:23 +08:00
|
|
|
/// Return a MIR diagnostic converted from an MI string diagnostic.
|
|
|
|
SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange);
|
|
|
|
|
2015-05-30 01:05:41 +08:00
|
|
|
/// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
|
|
|
|
SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange);
|
2015-06-16 07:07:38 +08:00
|
|
|
|
|
|
|
/// Create an empty function with the given name.
|
|
|
|
void createDummyFunction(StringRef Name, Module &M);
|
2015-07-10 06:23:13 +08:00
|
|
|
|
|
|
|
void initNames2RegClasses(const MachineFunction &MF);
|
|
|
|
|
|
|
|
/// Check if the given identifier is a name of a register class.
|
|
|
|
///
|
|
|
|
/// Return null if the name isn't a register class.
|
|
|
|
const TargetRegisterClass *getRegClass(const MachineFunction &MF,
|
|
|
|
StringRef Name);
|
2015-05-28 02:02:19 +08:00
|
|
|
};
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
} // end namespace llvm
|
2015-05-28 02:02:19 +08:00
|
|
|
|
|
|
|
MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
|
|
|
|
StringRef Filename, LLVMContext &Context)
|
|
|
|
: SM(), Filename(Filename), Context(Context) {
|
|
|
|
SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
bool MIRParserImpl::error(const Twine &Message) {
|
|
|
|
Context.diagnose(DiagnosticInfoMIRParser(
|
|
|
|
DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-09 04:22:20 +08:00
|
|
|
bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
|
|
|
|
Context.diagnose(DiagnosticInfoMIRParser(
|
|
|
|
DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-01 01:55:00 +08:00
|
|
|
bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
|
|
|
|
assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
|
|
|
|
reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
|
|
|
|
DiagnosticSeverity Kind;
|
|
|
|
switch (Diag.getKind()) {
|
|
|
|
case SourceMgr::DK_Error:
|
|
|
|
Kind = DS_Error;
|
|
|
|
break;
|
|
|
|
case SourceMgr::DK_Warning:
|
|
|
|
Kind = DS_Warning;
|
|
|
|
break;
|
|
|
|
case SourceMgr::DK_Note:
|
|
|
|
Kind = DS_Note;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
|
|
|
|
}
|
|
|
|
|
2015-05-29 06:41:12 +08:00
|
|
|
static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
|
2015-06-16 04:30:22 +08:00
|
|
|
reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
|
2015-05-29 06:41:12 +08:00
|
|
|
}
|
2015-05-28 02:02:19 +08:00
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
std::unique_ptr<Module> MIRParserImpl::parse() {
|
2015-05-29 06:41:12 +08:00
|
|
|
yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
|
2015-06-16 04:30:22 +08:00
|
|
|
/*Ctxt=*/nullptr, handleYAMLDiag, this);
|
2015-06-24 06:39:23 +08:00
|
|
|
In.setContext(&In);
|
2015-05-29 06:41:12 +08:00
|
|
|
|
|
|
|
if (!In.setCurrentDocument()) {
|
2015-06-16 04:30:22 +08:00
|
|
|
if (In.error())
|
2015-05-29 06:41:12 +08:00
|
|
|
return nullptr;
|
|
|
|
// Create an empty module when the MIR file is empty.
|
|
|
|
return llvm::make_unique<Module>(Filename, Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Module> M;
|
2015-06-16 07:07:38 +08:00
|
|
|
bool NoLLVMIR = false;
|
2015-05-28 02:02:19 +08:00
|
|
|
// Parse the block scalar manually so that we can return unique pointer
|
|
|
|
// without having to go trough YAML traits.
|
2015-05-29 06:41:12 +08:00
|
|
|
if (const auto *BSN =
|
|
|
|
dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
|
2015-06-16 04:30:22 +08:00
|
|
|
SMDiagnostic Error;
|
2015-05-29 06:41:12 +08:00
|
|
|
M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
|
2015-06-27 06:56:48 +08:00
|
|
|
Context, &IRSlots);
|
2015-05-30 01:05:41 +08:00
|
|
|
if (!M) {
|
2015-06-16 04:30:22 +08:00
|
|
|
reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
|
2015-05-29 06:41:12 +08:00
|
|
|
return M;
|
2015-05-30 01:05:41 +08:00
|
|
|
}
|
2015-05-29 06:41:12 +08:00
|
|
|
In.nextDocument();
|
|
|
|
if (!In.setCurrentDocument())
|
|
|
|
return M;
|
|
|
|
} else {
|
|
|
|
// Create an new, empty module.
|
|
|
|
M = llvm::make_unique<Module>(Filename, Context);
|
2015-06-16 07:07:38 +08:00
|
|
|
NoLLVMIR = true;
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 06:41:12 +08:00
|
|
|
// Parse the machine functions.
|
|
|
|
do {
|
2015-06-16 07:07:38 +08:00
|
|
|
if (parseMachineFunction(In, *M, NoLLVMIR))
|
2015-05-29 06:41:12 +08:00
|
|
|
return nullptr;
|
|
|
|
In.nextDocument();
|
|
|
|
} while (In.setCurrentDocument());
|
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2015-06-16 07:07:38 +08:00
|
|
|
bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
|
|
|
|
bool NoLLVMIR) {
|
2015-06-16 04:30:22 +08:00
|
|
|
auto MF = llvm::make_unique<yaml::MachineFunction>();
|
|
|
|
yaml::yamlize(In, *MF, false);
|
2015-05-29 06:41:12 +08:00
|
|
|
if (In.error())
|
|
|
|
return true;
|
2015-06-16 04:30:22 +08:00
|
|
|
auto FunctionName = MF->Name;
|
2015-06-16 06:23:23 +08:00
|
|
|
if (Functions.find(FunctionName) != Functions.end())
|
|
|
|
return error(Twine("redefinition of machine function '") + FunctionName +
|
|
|
|
"'");
|
2015-06-16 04:30:22 +08:00
|
|
|
Functions.insert(std::make_pair(FunctionName, std::move(MF)));
|
2015-06-16 07:07:38 +08:00
|
|
|
if (NoLLVMIR)
|
|
|
|
createDummyFunction(FunctionName, M);
|
2015-06-17 01:06:29 +08:00
|
|
|
else if (!M.getFunction(FunctionName))
|
|
|
|
return error(Twine("function '") + FunctionName +
|
|
|
|
"' isn't defined in the provided LLVM IR");
|
2015-06-16 04:30:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-16 07:07:38 +08:00
|
|
|
void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
|
|
|
|
auto &Context = M.getContext();
|
|
|
|
Function *F = cast<Function>(M.getOrInsertFunction(
|
|
|
|
Name, FunctionType::get(Type::getVoidTy(Context), false)));
|
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
|
|
|
|
new UnreachableInst(Context, BB);
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
|
|
|
|
auto It = Functions.find(MF.getName());
|
|
|
|
if (It == Functions.end())
|
|
|
|
return error(Twine("no machine function information for function '") +
|
|
|
|
MF.getName() + "' in the MIR file");
|
|
|
|
// TODO: Recreate the machine function.
|
2015-06-16 08:10:47 +08:00
|
|
|
const yaml::MachineFunction &YamlMF = *It->getValue();
|
|
|
|
if (YamlMF.Alignment)
|
|
|
|
MF.setAlignment(YamlMF.Alignment);
|
|
|
|
MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
|
|
|
|
MF.setHasInlineAsm(YamlMF.HasInlineAsm);
|
2015-07-11 06:51:20 +08:00
|
|
|
PerFunctionMIParsingState PFS;
|
|
|
|
if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF,
|
|
|
|
PFS.VirtualRegisterSlots))
|
2015-06-25 03:56:10 +08:00
|
|
|
return true;
|
2015-07-16 06:14:49 +08:00
|
|
|
if (initializeFrameInfo(*MF.getFunction(), *MF.getFrameInfo(), YamlMF))
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
return true;
|
2015-06-25 03:56:10 +08:00
|
|
|
|
2015-06-20 01:43:07 +08:00
|
|
|
const auto &F = *MF.getFunction();
|
|
|
|
for (const auto &YamlMBB : YamlMF.BasicBlocks) {
|
|
|
|
const BasicBlock *BB = nullptr;
|
2015-07-09 04:22:20 +08:00
|
|
|
const yaml::StringValue &Name = YamlMBB.Name;
|
|
|
|
if (!Name.Value.empty()) {
|
2015-06-20 01:43:07 +08:00
|
|
|
BB = dyn_cast_or_null<BasicBlock>(
|
2015-07-09 04:22:20 +08:00
|
|
|
F.getValueSymbolTable().lookup(Name.Value));
|
2015-06-20 04:12:03 +08:00
|
|
|
if (!BB)
|
2015-07-09 04:22:20 +08:00
|
|
|
return error(Name.SourceRange.Start,
|
|
|
|
Twine("basic block '") + Name.Value +
|
|
|
|
"' is not defined in the function '" + MF.getName() +
|
|
|
|
"'");
|
2015-06-20 01:43:07 +08:00
|
|
|
}
|
|
|
|
auto *MBB = MF.CreateMachineBasicBlock(BB);
|
|
|
|
MF.insert(MF.end(), MBB);
|
2015-07-08 01:46:43 +08:00
|
|
|
bool WasInserted =
|
|
|
|
PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
|
2015-06-27 00:46:11 +08:00
|
|
|
if (!WasInserted)
|
|
|
|
return error(Twine("redefinition of machine basic block with id #") +
|
|
|
|
Twine(YamlMBB.ID));
|
|
|
|
}
|
|
|
|
|
2015-07-10 05:21:33 +08:00
|
|
|
if (YamlMF.BasicBlocks.empty())
|
|
|
|
return error(Twine("machine function '") + Twine(MF.getName()) +
|
|
|
|
"' requires at least one machine basic block in its body");
|
2015-07-16 07:31:07 +08:00
|
|
|
// Initialize the jump table after creating all the MBBs so that the MBB
|
|
|
|
// references can be resolved.
|
|
|
|
if (!YamlMF.JumpTableInfo.Entries.empty() &&
|
|
|
|
initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
|
|
|
|
return true;
|
2015-06-27 00:46:11 +08:00
|
|
|
// Initialize the machine basic blocks after creating them all so that the
|
|
|
|
// machine instructions parser can resolve the MBB references.
|
|
|
|
unsigned I = 0;
|
|
|
|
for (const auto &YamlMBB : YamlMF.BasicBlocks) {
|
|
|
|
if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
|
2015-07-08 01:46:43 +08:00
|
|
|
PFS))
|
2015-06-20 01:43:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MIRParserImpl::initializeMachineBasicBlock(
|
2015-06-23 01:02:30 +08:00
|
|
|
MachineFunction &MF, MachineBasicBlock &MBB,
|
2015-06-27 00:46:11 +08:00
|
|
|
const yaml::MachineBasicBlock &YamlMBB,
|
2015-07-08 01:46:43 +08:00
|
|
|
const PerFunctionMIParsingState &PFS) {
|
2015-06-20 01:43:07 +08:00
|
|
|
MBB.setAlignment(YamlMBB.Alignment);
|
|
|
|
if (YamlMBB.AddressTaken)
|
|
|
|
MBB.setHasAddressTaken();
|
|
|
|
MBB.setIsLandingPad(YamlMBB.IsLandingPad);
|
2015-07-01 02:16:42 +08:00
|
|
|
SMDiagnostic Error;
|
|
|
|
// Parse the successors.
|
|
|
|
for (const auto &MBBSource : YamlMBB.Successors) {
|
|
|
|
MachineBasicBlock *SuccMBB = nullptr;
|
2015-07-08 01:46:43 +08:00
|
|
|
if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
|
2015-07-01 02:16:42 +08:00
|
|
|
Error))
|
|
|
|
return error(Error, MBBSource.SourceRange);
|
|
|
|
// TODO: Report an error when adding the same successor more than once.
|
|
|
|
MBB.addSuccessor(SuccMBB);
|
|
|
|
}
|
2015-07-15 05:24:41 +08:00
|
|
|
// Parse the liveins.
|
|
|
|
for (const auto &LiveInSource : YamlMBB.LiveIns) {
|
|
|
|
unsigned Reg = 0;
|
|
|
|
if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
|
|
|
|
IRSlots, Error))
|
|
|
|
return error(Error, LiveInSource.SourceRange);
|
|
|
|
MBB.addLiveIn(Reg);
|
|
|
|
}
|
2015-06-23 01:02:30 +08:00
|
|
|
// Parse the instructions.
|
|
|
|
for (const auto &MISource : YamlMBB.Instructions) {
|
2015-07-01 01:47:50 +08:00
|
|
|
MachineInstr *MI = nullptr;
|
2015-07-08 01:46:43 +08:00
|
|
|
if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
|
2015-07-01 01:55:00 +08:00
|
|
|
return error(Error, MISource.SourceRange);
|
2015-07-01 01:47:50 +08:00
|
|
|
MBB.insert(MBB.end(), MI);
|
2015-06-23 01:02:30 +08:00
|
|
|
}
|
2015-05-29 06:41:12 +08:00
|
|
|
return false;
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 03:56:10 +08:00
|
|
|
bool MIRParserImpl::initializeRegisterInfo(
|
2015-07-10 06:23:13 +08:00
|
|
|
const MachineFunction &MF, MachineRegisterInfo &RegInfo,
|
2015-07-11 06:51:20 +08:00
|
|
|
const yaml::MachineFunction &YamlMF,
|
|
|
|
DenseMap<unsigned, unsigned> &VirtualRegisterSlots) {
|
2015-06-25 03:56:10 +08:00
|
|
|
assert(RegInfo.isSSA());
|
|
|
|
if (!YamlMF.IsSSA)
|
|
|
|
RegInfo.leaveSSA();
|
|
|
|
assert(RegInfo.tracksLiveness());
|
|
|
|
if (!YamlMF.TracksRegLiveness)
|
|
|
|
RegInfo.invalidateLiveness();
|
|
|
|
RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
|
2015-07-10 06:23:13 +08:00
|
|
|
|
|
|
|
// Parse the virtual register information.
|
|
|
|
for (const auto &VReg : YamlMF.VirtualRegisters) {
|
|
|
|
const auto *RC = getRegClass(MF, VReg.Class.Value);
|
|
|
|
if (!RC)
|
|
|
|
return error(VReg.Class.SourceRange.Start,
|
|
|
|
Twine("use of undefined register class '") +
|
|
|
|
VReg.Class.Value + "'");
|
2015-07-11 06:51:20 +08:00
|
|
|
unsigned Reg = RegInfo.createVirtualRegister(RC);
|
|
|
|
// TODO: Report an error when the same virtual register with the same ID is
|
|
|
|
// redefined.
|
|
|
|
VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
|
2015-07-10 06:23:13 +08:00
|
|
|
}
|
2015-06-25 03:56:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-16 06:14:49 +08:00
|
|
|
bool MIRParserImpl::initializeFrameInfo(const Function &F,
|
|
|
|
MachineFrameInfo &MFI,
|
2015-07-11 02:13:57 +08:00
|
|
|
const yaml::MachineFunction &YamlMF) {
|
|
|
|
const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
|
|
|
|
MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
|
|
|
|
MFI.setHasStackMap(YamlMFI.HasStackMap);
|
|
|
|
MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
|
|
|
|
MFI.setStackSize(YamlMFI.StackSize);
|
|
|
|
MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
|
|
|
|
if (YamlMFI.MaxAlignment)
|
|
|
|
MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
|
|
|
|
MFI.setAdjustsStack(YamlMFI.AdjustsStack);
|
|
|
|
MFI.setHasCalls(YamlMFI.HasCalls);
|
|
|
|
MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
|
|
|
|
MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
|
|
|
|
MFI.setHasVAStart(YamlMFI.HasVAStart);
|
|
|
|
MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
|
2015-07-11 02:13:57 +08:00
|
|
|
|
2015-07-14 02:07:26 +08:00
|
|
|
// Initialize the fixed frame objects.
|
|
|
|
for (const auto &Object : YamlMF.FixedStackObjects) {
|
|
|
|
int ObjectIdx;
|
|
|
|
if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
|
|
|
|
ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
|
|
|
|
Object.IsImmutable, Object.IsAliased);
|
|
|
|
else
|
|
|
|
ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
|
|
|
|
MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
|
|
|
|
// TODO: Store the mapping between fixed object IDs and object indices to
|
|
|
|
// parse fixed stack object references correctly.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the ordinary frame objects.
|
2015-07-11 02:13:57 +08:00
|
|
|
for (const auto &Object : YamlMF.StackObjects) {
|
2015-07-14 08:26:26 +08:00
|
|
|
int ObjectIdx;
|
2015-07-16 06:14:49 +08:00
|
|
|
const AllocaInst *Alloca = nullptr;
|
|
|
|
const yaml::StringValue &Name = Object.Name;
|
|
|
|
if (!Name.Value.empty()) {
|
|
|
|
Alloca = dyn_cast_or_null<AllocaInst>(
|
|
|
|
F.getValueSymbolTable().lookup(Name.Value));
|
|
|
|
if (!Alloca)
|
|
|
|
return error(Name.SourceRange.Start,
|
|
|
|
"alloca instruction named '" + Name.Value +
|
|
|
|
"' isn't defined in the function '" + F.getName() +
|
|
|
|
"'");
|
|
|
|
}
|
2015-07-14 08:26:26 +08:00
|
|
|
if (Object.Type == yaml::MachineStackObject::VariableSized)
|
2015-07-16 06:14:49 +08:00
|
|
|
ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
|
2015-07-14 08:26:26 +08:00
|
|
|
else
|
|
|
|
ObjectIdx = MFI.CreateStackObject(
|
|
|
|
Object.Size, Object.Alignment,
|
2015-07-16 06:14:49 +08:00
|
|
|
Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
|
2015-07-11 02:13:57 +08:00
|
|
|
MFI.setObjectOffset(ObjectIdx, Object.Offset);
|
|
|
|
// TODO: Store the mapping between object IDs and object indices to parse
|
|
|
|
// stack object references correctly.
|
|
|
|
}
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
llvm-svn: 241844
2015-07-10 03:55:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:31:07 +08:00
|
|
|
bool MIRParserImpl::initializeJumpTableInfo(
|
|
|
|
MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
|
2015-07-16 07:38:35 +08:00
|
|
|
PerFunctionMIParsingState &PFS) {
|
2015-07-16 07:31:07 +08:00
|
|
|
MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
|
|
|
|
SMDiagnostic Error;
|
|
|
|
for (const auto &Entry : YamlJTI.Entries) {
|
|
|
|
std::vector<MachineBasicBlock *> Blocks;
|
|
|
|
for (const auto &MBBSource : Entry.Blocks) {
|
|
|
|
MachineBasicBlock *MBB = nullptr;
|
|
|
|
if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
|
|
|
|
return error(Error, MBBSource.SourceRange);
|
|
|
|
Blocks.push_back(MBB);
|
|
|
|
}
|
2015-07-16 07:38:35 +08:00
|
|
|
unsigned Index = JTI->createJumpTableIndex(Blocks);
|
|
|
|
// TODO: Report an error when the same jump table slot ID is redefined.
|
|
|
|
PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
|
2015-07-16 07:31:07 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-24 06:39:23 +08:00
|
|
|
SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange) {
|
|
|
|
assert(SourceRange.isValid() && "Invalid source range");
|
|
|
|
SMLoc Loc = SourceRange.Start;
|
|
|
|
bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
|
|
|
|
*Loc.getPointer() == '\'';
|
|
|
|
// Translate the location of the error from the location in the MI string to
|
|
|
|
// the corresponding location in the MIR file.
|
|
|
|
Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
|
|
|
|
(HasQuote ? 1 : 0));
|
|
|
|
|
|
|
|
// TODO: Translate any source ranges as well.
|
|
|
|
return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
|
|
|
|
Error.getFixIts());
|
|
|
|
}
|
|
|
|
|
2015-05-30 01:05:41 +08:00
|
|
|
SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
|
|
|
|
SMRange SourceRange) {
|
|
|
|
assert(SourceRange.isValid());
|
|
|
|
|
|
|
|
// Translate the location of the error from the location in the llvm IR string
|
|
|
|
// to the corresponding location in the MIR file.
|
|
|
|
auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
|
|
|
|
unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
|
|
|
|
unsigned Column = Error.getColumnNo();
|
|
|
|
StringRef LineStr = Error.getLineContents();
|
|
|
|
SMLoc Loc = Error.getLoc();
|
|
|
|
|
|
|
|
// Get the full line and adjust the column number by taking the indentation of
|
|
|
|
// LLVM IR into account.
|
|
|
|
for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
|
|
|
|
L != E; ++L) {
|
|
|
|
if (L.line_number() == Line) {
|
|
|
|
LineStr = *L;
|
|
|
|
Loc = SMLoc::getFromPointer(LineStr.data());
|
|
|
|
auto Indent = LineStr.find(Error.getLineContents());
|
|
|
|
if (Indent != StringRef::npos)
|
|
|
|
Column += Indent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
|
|
|
|
Error.getMessage(), LineStr, Error.getRanges(),
|
|
|
|
Error.getFixIts());
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:23:13 +08:00
|
|
|
void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
|
|
|
|
if (!Names2RegClasses.empty())
|
|
|
|
return;
|
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
|
|
|
|
const auto *RC = TRI->getRegClass(I);
|
|
|
|
Names2RegClasses.insert(
|
|
|
|
std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
|
|
|
|
StringRef Name) {
|
|
|
|
initNames2RegClasses(MF);
|
|
|
|
auto RegClassInfo = Names2RegClasses.find(Name);
|
|
|
|
if (RegClassInfo == Names2RegClasses.end())
|
|
|
|
return nullptr;
|
|
|
|
return RegClassInfo->getValue();
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
|
|
|
|
: Impl(std::move(Impl)) {}
|
|
|
|
|
|
|
|
MIRParser::~MIRParser() {}
|
|
|
|
|
|
|
|
std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
|
|
|
|
|
|
|
|
bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
|
|
|
|
return Impl->initializeMachineFunction(MF);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
|
|
|
|
SMDiagnostic &Error,
|
|
|
|
LLVMContext &Context) {
|
2015-05-28 02:02:19 +08:00
|
|
|
auto FileOrErr = MemoryBuffer::getFile(Filename);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
|
|
|
Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
|
|
"Could not open input file: " + EC.message());
|
2015-06-16 04:30:22 +08:00
|
|
|
return nullptr;
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
2015-06-16 04:30:22 +08:00
|
|
|
return createMIRParser(std::move(FileOrErr.get()), Context);
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 04:30:22 +08:00
|
|
|
std::unique_ptr<MIRParser>
|
|
|
|
llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
|
|
|
|
LLVMContext &Context) {
|
2015-05-28 02:02:19 +08:00
|
|
|
auto Filename = Contents->getBufferIdentifier();
|
2015-06-16 04:30:22 +08:00
|
|
|
return llvm::make_unique<MIRParser>(
|
|
|
|
llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
|
2015-05-28 02:02:19 +08:00
|
|
|
}
|