mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-21 21:29:41 +00:00
string_ostream is a safe and efficient string builder that combines opaque stack storage with a built-in ostream interface. small_string_ostream<bytes> additionally permits an explicit stack storage size other than the default 128 bytes to be provided. Beyond that, storage is transferred to the heap. This convenient class can be used in most places an std::string+raw_string_ostream pair or SmallString<>+raw_svector_ostream pair would previously have been used, in order to guarantee consistent access without byte truncation. The patch also converts much of LLVM to use the new facility. These changes include several probable bug fixes for truncated output, a programming error that's no longer possible with the new interface. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211749 91177308-0d34-0410-b5e6-96231b3b80d8
120 lines
4.0 KiB
C++
120 lines
4.0 KiB
C++
//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
|
#include "llvm-c/Core.h"
|
|
#include "llvm-c/IRReader.h"
|
|
#include "llvm/AsmParser/Parser.h"
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
#include "llvm/Support/SourceMgr.h"
|
|
#include "llvm/Support/Timer.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include <system_error>
|
|
|
|
using namespace llvm;
|
|
|
|
namespace llvm {
|
|
extern bool TimePassesIsEnabled;
|
|
}
|
|
|
|
static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
|
|
static const char *const TimeIRParsingName = "Parse IR";
|
|
|
|
|
|
Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
|
|
LLVMContext &Context) {
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
|
std::string ErrMsg;
|
|
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
|
|
if (std::error_code EC = ModuleOrErr.getError()) {
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
EC.message());
|
|
// ParseBitcodeFile does not take ownership of the Buffer in the
|
|
// case of an error.
|
|
delete Buffer;
|
|
return nullptr;
|
|
}
|
|
return ModuleOrErr.get();
|
|
}
|
|
|
|
return ParseAssembly(Buffer, nullptr, Err, Context);
|
|
}
|
|
|
|
Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
|
|
LLVMContext &Context) {
|
|
std::unique_ptr<MemoryBuffer> File;
|
|
if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
"Could not open input file: " + ec.message());
|
|
return nullptr;
|
|
}
|
|
|
|
return getLazyIRModule(File.release(), Err, Context);
|
|
}
|
|
|
|
Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
|
|
LLVMContext &Context) {
|
|
NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
|
|
TimePassesIsEnabled);
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
|
ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
|
|
Module *M = nullptr;
|
|
if (std::error_code EC = ModuleOrErr.getError())
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
EC.message());
|
|
else
|
|
M = ModuleOrErr.get();
|
|
// parseBitcodeFile does not take ownership of the Buffer.
|
|
delete Buffer;
|
|
return M;
|
|
}
|
|
|
|
return ParseAssembly(Buffer, nullptr, Err, Context);
|
|
}
|
|
|
|
Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
|
|
LLVMContext &Context) {
|
|
std::unique_ptr<MemoryBuffer> File;
|
|
if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
"Could not open input file: " + ec.message());
|
|
return nullptr;
|
|
}
|
|
|
|
return ParseIR(File.release(), Err, Context);
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// C API.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
|
|
LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
|
|
char **OutMessage) {
|
|
SMDiagnostic Diag;
|
|
|
|
*OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
|
|
|
|
if(!*OutM) {
|
|
if (OutMessage) {
|
|
string_ostream os;
|
|
Diag.print(nullptr, os, false);
|
|
*OutMessage = strndup(os.str().data(), os.str().size());
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|