mirror of
https://github.com/RPCS3/llvm.git
synced 2025-05-21 21:06:04 +00:00

This commit connects the machine function analysis pass (which creates machine functions) to the MIR parser, which will initialize the machine functions with the state from the MIR file and reconstruct the machine IR. This commit introduces a new interface called 'MachineFunctionInitializer', which can be used to provide custom initialization for the machine functions. This commit also introduces a new diagnostic class called 'DiagnosticInfoMIRParser' which is used for MIR parsing errors. This commit modifies the default diagnostic handling in LLVMContext - now the the diagnostics are printed directly into llvm::errs() so that the MIR parsing errors can be printed with colours. Reviewers: Justin Bogner Differential Revision: http://reviews.llvm.org/D9928 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239753 91177308-0d34-0410-b5e6-96231b3b80d8
233 lines
7.5 KiB
C++
233 lines
7.5 KiB
C++
//===- 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"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/ADT/StringMap.h"
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "llvm/AsmParser/Parser.h"
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
#include "llvm/CodeGen/MIRYamlMapping.h"
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/Support/LineIterator.h"
|
|
#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;
|
|
|
|
namespace llvm {
|
|
|
|
/// This class implements the parsing of LLVM IR that's embedded inside a MIR
|
|
/// file.
|
|
class MIRParserImpl {
|
|
SourceMgr SM;
|
|
StringRef Filename;
|
|
LLVMContext &Context;
|
|
StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
|
|
|
|
public:
|
|
MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
|
|
LLVMContext &Context);
|
|
|
|
void reportDiagnostic(const SMDiagnostic &Diag);
|
|
|
|
/// Report an error with the given message at unknown location.
|
|
///
|
|
/// Always returns true.
|
|
bool error(const Twine &Message);
|
|
|
|
/// Try to parse the optional LLVM module and the machine functions in the MIR
|
|
/// file.
|
|
///
|
|
/// Return null if an error occurred.
|
|
std::unique_ptr<Module> parse();
|
|
|
|
/// Parse the machine function in the current YAML document.
|
|
///
|
|
/// Return true if an error occurred.
|
|
bool parseMachineFunction(yaml::Input &In);
|
|
|
|
/// Initialize the machine function to the state that's described in the MIR
|
|
/// file.
|
|
///
|
|
/// Return true if error occurred.
|
|
bool initializeMachineFunction(MachineFunction &MF);
|
|
|
|
private:
|
|
/// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
|
|
SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
|
|
SMRange SourceRange);
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
|
|
StringRef Filename, LLVMContext &Context)
|
|
: SM(), Filename(Filename), Context(Context) {
|
|
SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
|
|
}
|
|
|
|
bool MIRParserImpl::error(const Twine &Message) {
|
|
Context.diagnose(DiagnosticInfoMIRParser(
|
|
DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
|
|
return true;
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
|
|
reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
|
|
}
|
|
|
|
std::unique_ptr<Module> MIRParserImpl::parse() {
|
|
yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
|
|
/*Ctxt=*/nullptr, handleYAMLDiag, this);
|
|
|
|
if (!In.setCurrentDocument()) {
|
|
if (In.error())
|
|
return nullptr;
|
|
// Create an empty module when the MIR file is empty.
|
|
return llvm::make_unique<Module>(Filename, Context);
|
|
}
|
|
|
|
std::unique_ptr<Module> M;
|
|
// Parse the block scalar manually so that we can return unique pointer
|
|
// without having to go trough YAML traits.
|
|
if (const auto *BSN =
|
|
dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
|
|
SMDiagnostic Error;
|
|
M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
|
|
Context);
|
|
if (!M) {
|
|
reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
|
|
return M;
|
|
}
|
|
In.nextDocument();
|
|
if (!In.setCurrentDocument())
|
|
return M;
|
|
} else {
|
|
// Create an new, empty module.
|
|
M = llvm::make_unique<Module>(Filename, Context);
|
|
}
|
|
|
|
// Parse the machine functions.
|
|
do {
|
|
if (parseMachineFunction(In))
|
|
return nullptr;
|
|
In.nextDocument();
|
|
} while (In.setCurrentDocument());
|
|
|
|
return M;
|
|
}
|
|
|
|
bool MIRParserImpl::parseMachineFunction(yaml::Input &In) {
|
|
auto MF = llvm::make_unique<yaml::MachineFunction>();
|
|
yaml::yamlize(In, *MF, false);
|
|
if (In.error())
|
|
return true;
|
|
auto FunctionName = MF->Name;
|
|
Functions.insert(std::make_pair(FunctionName, std::move(MF)));
|
|
return false;
|
|
}
|
|
|
|
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.
|
|
return false;
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
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) {
|
|
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());
|
|
return nullptr;
|
|
}
|
|
return createMIRParser(std::move(FileOrErr.get()), Context);
|
|
}
|
|
|
|
std::unique_ptr<MIRParser>
|
|
llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
|
|
LLVMContext &Context) {
|
|
auto Filename = Contents->getBufferIdentifier();
|
|
return llvm::make_unique<MIRParser>(
|
|
llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
|
|
}
|