llvm.org GIT mirror llvm / b9fc96d
Bitcode: Introduce a BitcodeFileContents data type. NFCI. This data type includes the contents of a bitcode file. Right now a bitcode file can only contain modules, but a later change will add a symbol table. Differential Revision: https://reviews.llvm.org/D33969 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305019 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
6 changed file(s) with 47 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
3939 return std::move(*Val);
4040 }
4141
42 struct BitcodeFileContents;
43
4244 /// Represents a module in a bitcode file.
4345 class BitcodeModule {
4446 // This covers the identification (if present) and module blocks.
6062 IdentificationBit(IdentificationBit), ModuleBit(ModuleBit) {}
6163
6264 // Calls the ctor.
63 friend Expected>
64 getBitcodeModuleList(MemoryBufferRef Buffer);
65 friend Expected
66 getBitcodeFileContents(MemoryBufferRef Buffer);
6567
6668 Expected> getModuleImpl(LLVMContext &Context,
6769 bool MaterializeAll,
9799 /// into CombinedIndex.
98100 Error readSummary(ModuleSummaryIndex &CombinedIndex, unsigned ModuleId);
99101 };
102
103 struct BitcodeFileContents {
104 std::vector Mods;
105 };
106
107 /// Returns the contents of a bitcode file.
108 Expected getBitcodeFileContents(MemoryBufferRef Buffer);
100109
101110 /// Returns a list of modules in the specified bitcode buffer.
102111 Expected>
1919 #include "llvm/Object/SymbolicFile.h"
2020
2121 namespace llvm {
22 class BitcodeModule;
2223 class Mangler;
2324 class Module;
2425 class GlobalValue;
3636
3737 namespace llvm {
3838
39 class BitcodeModule;
39 struct BitcodeFileContents;
4040
4141 namespace irsymtab {
4242
324324 };
325325
326326 /// Reads the contents of a bitcode file, creating its irsymtab if necessary.
327 Expected readBitcode(ArrayRef Mods);
327 Expected readBitcode(const BitcodeFileContents &BFC);
328328
329329 } // end namespace irsymtab
330330 } // end namespace llvm
53695369
53705370 Expected>
53715371 llvm::getBitcodeModuleList(MemoryBufferRef Buffer) {
5372 auto FOrErr = getBitcodeFileContents(Buffer);
5373 if (!FOrErr)
5374 return FOrErr.takeError();
5375 return std::move(FOrErr->Mods);
5376 }
5377
5378 Expected
5379 llvm::getBitcodeFileContents(MemoryBufferRef Buffer) {
53725380 Expected StreamOrErr = initStream(Buffer);
53735381 if (!StreamOrErr)
53745382 return StreamOrErr.takeError();
53755383 BitstreamCursor &Stream = *StreamOrErr;
53765384
5377 std::vector Modules;
5385 BitcodeFileContents F;
53785386 while (true) {
53795387 uint64_t BCBegin = Stream.getCurrentByteNo();
53805388
53825390 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to
53835391 // the end that there cannot possibly be another module, stop looking.
53845392 if (BCBegin + 8 >= Stream.getBitcodeBytes().size())
5385 return Modules;
5393 return F;
53865394
53875395 BitstreamEntry Entry = Stream.advance();
53885396 switch (Entry.Kind) {
54085416 if (Stream.SkipBlock())
54095417 return error("Malformed block");
54105418
5411 Modules.push_back({Stream.getBitcodeBytes().slice(
5412 BCBegin, Stream.getCurrentByteNo() - BCBegin),
5413 Buffer.getBufferIdentifier(), IdentificationBit,
5414 ModuleBit});
5419 F.Mods.push_back({Stream.getBitcodeBytes().slice(
5420 BCBegin, Stream.getCurrentByteNo() - BCBegin),
5421 Buffer.getBufferIdentifier(), IdentificationBit,
5422 ModuleBit});
54155423 continue;
54165424 }
54175425
54235431 // not have its own string table. A bitcode file may have multiple
54245432 // string tables if it was created by binary concatenation, for example
54255433 // with "llvm-cat -b".
5426 for (auto I = Modules.rbegin(), E = Modules.rend(); I != E; ++I) {
5434 for (auto I = F.Mods.rbegin(), E = F.Mods.rend(); I != E; ++I) {
54275435 if (!I->Strtab.empty())
54285436 break;
54295437 I->Strtab = *Strtab;
146146 if (!BCOrErr)
147147 return errorCodeToError(BCOrErr.getError());
148148
149 Expected> BMsOrErr =
150 getBitcodeModuleList(*BCOrErr);
151 if (!BMsOrErr)
152 return BMsOrErr.takeError();
149 Expected BFCOrErr = getBitcodeFileContents(*BCOrErr);
150 if (!BFCOrErr)
151 return BFCOrErr.takeError();
153152
154 Expected FCOrErr = irsymtab::readBitcode(*BMsOrErr);
153 Expected FCOrErr = irsymtab::readBitcode(*BFCOrErr);
155154 if (!FCOrErr)
156155 return FCOrErr.takeError();
157156
158 F.Mods = std::move(*BMsOrErr);
157 F.Mods = std::move(BFCOrErr->Mods);
159158 F.Symtab = std::move(FCOrErr->Symtab);
160159 F.Strtab = std::move(FCOrErr->Strtab);
161160 F.TheReader = std::move(FCOrErr->TheReader);
261261 return Builder(Symtab, Strtab).build(Mods);
262262 }
263263
264 Expected irsymtab::readBitcode(ArrayRef BMs) {
264 // Upgrade a vector of bitcode modules created by an old version of LLVM by
265 // creating an irsymtab for them in the current format.
266 static Expected upgrade(ArrayRef BMs) {
265267 FileContents FC;
266 if (BMs.empty())
267 return make_error("Bitcode file does not contain any modules",
268 inconvertibleErrorCode());
269268
270269 LLVMContext Ctx;
271270 std::vector Mods;
292291 {FC.Strtab.data(), FC.Strtab.size()}};
293292 return std::move(FC);
294293 }
294
295 Expected irsymtab::readBitcode(const BitcodeFileContents &BFC) {
296 if (BFC.Mods.empty())
297 return make_error("Bitcode file does not contain any modules",
298 inconvertibleErrorCode());
299
300 // Right now we have no on-disk representation of symbol tables, so we always
301 // upgrade.
302 return upgrade(BFC.Mods);
303 }