[llvm-commits] CVS: llvm/lib/Bitcode/Reader/BitcodeReader.cpp BitcodeReader.h ReaderWrappers.cpp
Chris Lattner
sabre at nondot.org
Sun Apr 29 00:54:54 PDT 2007
Changes in directory llvm/lib/Bitcode/Reader:
BitcodeReader.cpp updated: 1.15 -> 1.16
BitcodeReader.h updated: 1.10 -> 1.11
ReaderWrappers.cpp (r1.2) removed
---
Log message:
Switch the bitcode reader interface to take a MemoryBuffer instead of knowing
anything about disk I/O itself. This greatly simplifies its interface -
eliminating the need for the ReaderWrappers.cpp file.
This adds a new option to llvm-dis (-bitcode) which instructs it to read
the input file as bitcode. Until/unless the bytecode reader is taught to
read from MemoryBuffer, there is no way to handle stdin reading without it.
I don't plan to switch the bytecode reader over, I'd rather delete it :),
so the option will stay around temporarily.
---
Diffs of the changes: (+62 -10)
BitcodeReader.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++-----
BitcodeReader.h | 17 +++++++++++-----
2 files changed, 62 insertions(+), 10 deletions(-)
Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp
diff -u llvm/lib/Bitcode/Reader/BitcodeReader.cpp:1.15 llvm/lib/Bitcode/Reader/BitcodeReader.cpp:1.16
--- llvm/lib/Bitcode/Reader/BitcodeReader.cpp:1.15 Sat Apr 28 09:57:59 2007
+++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp Sun Apr 29 02:54:31 2007
@@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/Bitcode/ReaderWriter.h"
#include "BitcodeReader.h"
#include "llvm/Bitcode/BitstreamReader.h"
#include "llvm/Constants.h"
@@ -18,8 +19,14 @@
#include "llvm/Module.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/Support/MemoryBuffer.h"
using namespace llvm;
+BitcodeReader::~BitcodeReader() {
+ delete Buffer;
+}
+
+
/// ConvertToString - Convert a string from a record into an std::string, return
/// true on failure.
template<typename StrTy>
@@ -852,14 +859,14 @@
}
-bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length,
- const std::string &ModuleID) {
+bool BitcodeReader::ParseBitcode() {
TheModule = 0;
- if (Length & 3)
+ if (Buffer->getBufferSize() & 3)
return Error("Bitcode stream should be a multiple of 4 bytes in length");
- BitstreamReader Stream(Buf, Buf+Length);
+ unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
+ BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
// Sniff for the signature.
if (Stream.Read(8) != 'B' ||
@@ -882,7 +889,7 @@
// We only know the MODULE subblock ID.
if (BlockID == bitc::MODULE_BLOCK_ID) {
- if (ParseModule(Stream, ModuleID))
+ if (ParseModule(Stream, Buffer->getBufferIdentifier()))
return true;
} else if (Stream.SkipBlock()) {
return Error("Malformed block record");
@@ -891,3 +898,41 @@
return false;
}
+
+//===----------------------------------------------------------------------===//
+// External interface
+//===----------------------------------------------------------------------===//
+
+/// getBitcodeModuleProvider - lazy function-at-a-time loading from a file.
+///
+ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer,
+ std::string *ErrMsg) {
+ BitcodeReader *R = new BitcodeReader(Buffer);
+ if (R->ParseBitcode()) {
+ if (ErrMsg)
+ *ErrMsg = R->getErrorString();
+
+ // Don't let the BitcodeReader dtor delete 'Buffer'.
+ R->releaseMemoryBuffer();
+ delete R;
+ return 0;
+ }
+ return R;
+}
+
+/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
+/// If an error occurs, return null and fill in *ErrMsg if non-null.
+Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg){
+ BitcodeReader *R;
+ R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, ErrMsg));
+ if (!R) return 0;
+
+ // Read the whole module, get a pointer to it, tell ModuleProvider not to
+ // delete it when its dtor is run.
+ Module *M = R->releaseModule(ErrMsg);
+
+ // Don't let the BitcodeReader dtor delete 'Buffer'.
+ R->releaseMemoryBuffer();
+ delete R;
+ return M;
+}
Index: llvm/lib/Bitcode/Reader/BitcodeReader.h
diff -u llvm/lib/Bitcode/Reader/BitcodeReader.h:1.10 llvm/lib/Bitcode/Reader/BitcodeReader.h:1.11
--- llvm/lib/Bitcode/Reader/BitcodeReader.h:1.10 Wed Apr 25 22:27:58 2007
+++ llvm/lib/Bitcode/Reader/BitcodeReader.h Sun Apr 29 02:54:31 2007
@@ -22,6 +22,7 @@
namespace llvm {
class BitstreamReader;
+ class MemoryBuffer;
class BitcodeReaderValueList : public User {
std::vector<Use> Uses;
@@ -57,6 +58,7 @@
class BitcodeReader : public ModuleProvider {
+ MemoryBuffer *Buffer;
const char *ErrorString;
std::vector<PATypeHolder> TypeList;
@@ -64,10 +66,16 @@
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
public:
- BitcodeReader() : ErrorString(0) {}
- virtual ~BitcodeReader() {}
+ BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {}
+ ~BitcodeReader();
- virtual void FreeState() {}
+
+ /// releaseMemoryBuffer - This causes the reader to completely forget about
+ /// the memory buffer it contains, which prevents the buffer from being
+ /// destroyed when it is deleted.
+ void releaseMemoryBuffer() {
+ Buffer = 0;
+ }
virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
// FIXME: TODO
@@ -89,8 +97,7 @@
/// @brief Main interface to parsing a bitcode buffer.
/// @returns true if an error occurred.
- bool ParseBitcode(unsigned char *Buf, unsigned Length,
- const std::string &ModuleID);
+ bool ParseBitcode();
private:
const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
More information about the llvm-commits
mailing list