llvm.org GIT mirror llvm / c453f76
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36554 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
5 changed file(s) with 96 addition(s) and 119 deletion(s). Raw diff Collapse all Expand all
1919 namespace llvm {
2020 class Module;
2121 class ModuleProvider;
22 class MemoryBuffer;
2223
23 ModuleProvider *getBitcodeModuleProvider(const std::string &Filename,
24 /// getBitcodeModuleProvider - Read the header of the specified bitcode buffer
25 /// and prepare for lazy deserialization of function bodies. If successful,
26 /// this takes ownership of 'buffer' and returns a non-null pointer. On
27 /// error, this returns null, *does not* take ownership of Buffer, and fills
28 /// in *ErrMsg with an error description if ErrMsg is non-null.
29 ModuleProvider *getBitcodeModuleProvider(MemoryBuffer *Buffer,
2430 std::string *ErrMsg = 0);
2531
26
2732 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
28 /// If an error occurs, return null and fill in *ErrMsg if non-null.
29 Module *ParseBitcodeFile(const std::string &Filename,
30 std::string *ErrMsg = 0);
33 /// If an error occurs, this returns null and fills in *ErrMsg if it is
34 /// non-null. This method *never* takes ownership of Buffer.
35 Module *ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg = 0);
3136
3237 /// WriteBitcodeToFile - Write the specified module to the specified output
3338 /// stream.
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/Bitcode/ReaderWriter.h"
1314 #include "BitcodeReader.h"
1415 #include "llvm/Bitcode/BitstreamReader.h"
1516 #include "llvm/Constants.h"
1718 #include "llvm/Module.h"
1819 #include "llvm/ADT/SmallString.h"
1920 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/MemoryBuffer.h"
2022 using namespace llvm;
23
24 BitcodeReader::~BitcodeReader() {
25 delete Buffer;
26 }
27
2128
2229 /// ConvertToString - Convert a string from a record into an std::string, return
2330 /// true on failure.
851858 }
852859
853860
854 bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length,
855 const std::string &ModuleID) {
861 bool BitcodeReader::ParseBitcode() {
856862 TheModule = 0;
857863
858 if (Length & 3)
864 if (Buffer->getBufferSize() & 3)
859865 return Error("Bitcode stream should be a multiple of 4 bytes in length");
860866
861 BitstreamReader Stream(Buf, Buf+Length);
867 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
868 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
862869
863870 // Sniff for the signature.
864871 if (Stream.Read(8) != 'B' ||
881888
882889 // We only know the MODULE subblock ID.
883890 if (BlockID == bitc::MODULE_BLOCK_ID) {
884 if (ParseModule(Stream, ModuleID))
891 if (ParseModule(Stream, Buffer->getBufferIdentifier()))
885892 return true;
886893 } else if (Stream.SkipBlock()) {
887894 return Error("Malformed block record");
890897
891898 return false;
892899 }
900
901 //===----------------------------------------------------------------------===//
902 // External interface
903 //===----------------------------------------------------------------------===//
904
905 /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file.
906 ///
907 ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer,
908 std::string *ErrMsg) {
909 BitcodeReader *R = new BitcodeReader(Buffer);
910 if (R->ParseBitcode()) {
911 if (ErrMsg)
912 *ErrMsg = R->getErrorString();
913
914 // Don't let the BitcodeReader dtor delete 'Buffer'.
915 R->releaseMemoryBuffer();
916 delete R;
917 return 0;
918 }
919 return R;
920 }
921
922 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
923 /// If an error occurs, return null and fill in *ErrMsg if non-null.
924 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg){
925 BitcodeReader *R;
926 R = static_cast(getBitcodeModuleProvider(Buffer, ErrMsg));
927 if (!R) return 0;
928
929 // Read the whole module, get a pointer to it, tell ModuleProvider not to
930 // delete it when its dtor is run.
931 Module *M = R->releaseModule(ErrMsg);
932
933 // Don't let the BitcodeReader dtor delete 'Buffer'.
934 R->releaseMemoryBuffer();
935 delete R;
936 return M;
937 }
2121
2222 namespace llvm {
2323 class BitstreamReader;
24 class MemoryBuffer;
2425
2526 class BitcodeReaderValueList : public User {
2627 std::vector Uses;
5657
5758
5859 class BitcodeReader : public ModuleProvider {
60 MemoryBuffer *Buffer;
5961 const char *ErrorString;
6062
6163 std::vector TypeList;
6365 std::vector > GlobalInits;
6466 std::vector > AliasInits;
6567 public:
66 BitcodeReader() : ErrorString(0) {}
67 virtual ~BitcodeReader() {}
68 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {}
69 ~BitcodeReader();
6870
69 virtual void FreeState() {}
71
72 /// releaseMemoryBuffer - This causes the reader to completely forget about
73 /// the memory buffer it contains, which prevents the buffer from being
74 /// destroyed when it is deleted.
75 void releaseMemoryBuffer() {
76 Buffer = 0;
77 }
7078
7179 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
7280 // FIXME: TODO
8896
8997 /// @brief Main interface to parsing a bitcode buffer.
9098 /// @returns true if an error occurred.
91 bool ParseBitcode(unsigned char *Buf, unsigned Length,
92 const std::string &ModuleID);
99 bool ParseBitcode();
93100 private:
94101 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
95102
+0
-98
lib/Bitcode/Reader/ReaderWrappers.cpp less more
None //===- ReaderWrappers.cpp - Parse bitcode from file or buffer -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements loading and parsing a bitcode file and parsing a
10 // module from a memory buffer.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "BitcodeReader.h"
16 #include "llvm/System/MappedFile.h"
17 using namespace llvm;
18
19 //===----------------------------------------------------------------------===//
20 // BitcodeFileReader - Read from an mmap'able file descriptor.
21
22 namespace {
23 /// BitcodeFileReader - parses bitcode from a file.
24 ///
25 class BitcodeFileReader : public BitcodeReader {
26 private:
27 std::string Filename;
28 sys::MappedFile File;
29
30 BitcodeFileReader(const BitcodeFileReader&); // DO NOT IMPLEMENT
31 void operator=(const BitcodeFileReader&); // DO NOT IMPLEMENT
32 public:
33 BitcodeFileReader(const std::string &FN) : Filename(FN) {}
34 bool Read(std::string *ErrMsg);
35
36 void FreeState() {
37 BitcodeReader::FreeState();
38 File.close();
39 }
40 };
41 }
42
43 bool BitcodeFileReader::Read(std::string *ErrMsg) {
44 if (File.open(sys::Path(Filename), sys::MappedFile::READ_ACCESS, ErrMsg))
45 return true;
46 if (!File.map(ErrMsg)) {
47 File.close();
48 return true;
49 }
50 unsigned char *Buffer = reinterpret_cast(File.base());
51 if (!ParseBitcode(Buffer, File.size(), Filename))
52 return false;
53 assert(getErrorString() && "Didn't set an error string?");
54 if (ErrMsg) *ErrMsg = getErrorString();
55 return true;
56 }
57
58
59
60 //===----------------------------------------------------------------------===//
61 // External interface
62 //===----------------------------------------------------------------------===//
63
64 /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file.
65 ///
66 ModuleProvider *llvm::getBitcodeModuleProvider(const std::string &Filename,
67 std::string *ErrMsg) {
68 if (Filename != std::string("-")) {
69 BitcodeFileReader *R = new BitcodeFileReader(Filename);
70 if (R->Read(ErrMsg)) {
71 delete R;
72 return 0;
73 }
74 return R;
75 }
76
77 assert(0 && "FIXME: stdin reading unimp!");
78 #if 0
79 // Read from stdin
80 BytecodeStdinReader *R = new BytecodeStdinReader();
81 if (R->Read(ErrMsg)) {
82 delete R;
83 return 0;
84 }
85 return R;
86 #endif
87 }
88
89 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
90 /// If an error occurs, return null and fill in *ErrMsg if non-null.
91 Module *llvm::ParseBitcodeFile(const std::string &Filename,std::string *ErrMsg){
92 ModuleProvider *MP = getBitcodeModuleProvider(Filename, ErrMsg);
93 if (!MP) return 0;
94 Module *M = MP->releaseModule(ErrMsg);
95 delete MP;
96 return M;
97 }
2323 #include "llvm/Support/Compressor.h"
2424 #include "llvm/Support/CommandLine.h"
2525 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/MemoryBuffer.h"
2627 #include "llvm/Support/Streams.h"
2728 #include "llvm/System/Signals.h"
2829 #include
4344 static cl::opt
4445 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
4546
47 static cl::opt
48 Bitcode("bitcode", cl::desc("Read a bitcode file"));
49
4650 int main(int argc, char **argv) {
4751 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
4852 try {
5458
5559 std::auto_ptr M;
5660
57 if (InputFilename != "-")
58 M.reset(ParseBitcodeFile(InputFilename, &ErrorMessage));
59
60 if (M.get() == 0)
61 M.reset(ParseBytecodeFile(InputFilename,Compressor::decompressToNewBuffer,
61 if (Bitcode) {
62 MemoryBuffer *Buffer;
63 if (InputFilename == "-") {
64 Buffer = MemoryBuffer::getSTDIN();
65 } else {
66 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
67 }
68
69 if (Buffer == 0)
70 ErrorMessage = "Error reading file '" + InputFilename + "'";
71 else
72 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
73
74 delete Buffer;
75 } else {
76 M.reset(ParseBytecodeFile(InputFilename,
77 Compressor::decompressToNewBuffer,
6278 &ErrorMessage));
79 }
80
6381 if (M.get() == 0) {
6482 cerr << argv[0] << ": ";
6583 if (ErrorMessage.size())
6886 cerr << "bytecode didn't read correctly.\n";
6987 return 1;
7088 }
71
89
7290 if (DontPrint) {
7391 // Just use stdout. We won't actually print anything on it.
7492 } else if (OutputFilename != "") { // Specified an output filename?