diff options
Diffstat (limited to 'lib/Bitcode')
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | 114 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h | 15 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp | 64 |
3 files changed, 19 insertions, 174 deletions
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp index 0b7506fad6..09a94c08cf 100644 --- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/DataStream.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; enum { @@ -92,15 +93,6 @@ static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { } } -static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { - switch (Val) { - default: // Map unknown visibilities to default. - case 0: return GlobalValue::DefaultVisibility; - case 1: return GlobalValue::HiddenVisibility; - case 2: return GlobalValue::ProtectedVisibility; - } -} - static int GetDecodedCastOpcode(unsigned Val) { switch (Val) { default: return -1; @@ -868,27 +860,6 @@ bool NaClBitcodeReader::ParseValueSymbolTable() { } } -/// ResolveAliasInits - Resolve all of the initializers for aliases that we can. -bool NaClBitcodeReader::ResolveAliasInits() { - std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; - - AliasInitWorklist.swap(AliasInits); - - while (!AliasInitWorklist.empty()) { - unsigned ValID = AliasInitWorklist.back().second; - if (ValID >= ValueList.size()) { - AliasInits.push_back(AliasInitWorklist.back()); - } else { - if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) - AliasInitWorklist.back().first->setAliasee(C); - else - return Error("Alias initializer is not a constant!"); - } - AliasInitWorklist.pop_back(); - } - return false; -} - static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { SmallVector<uint64_t, 8> Words(Vals.size()); std::transform(Vals.begin(), Vals.end(), Words.begin(), @@ -1358,12 +1329,6 @@ bool NaClBitcodeReader::RememberAndSkipFunctionBody() { } bool NaClBitcodeReader::GlobalCleanup() { - // Patch the initializers for globals and aliases up. - ResolveAliasInits(); - - if (!AliasInits.empty()) - return Error("Malformed Alias Initializer"); - // Look for intrinsic functions which need to be upgraded at some point for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); FI != FE; ++FI) { @@ -1377,7 +1342,6 @@ bool NaClBitcodeReader::GlobalCleanup() { GI = TheModule->global_begin(), GE = TheModule->global_end(); GI != GE; ++GI) UpgradeGlobalVariable(GI); - std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); return false; } @@ -1389,8 +1353,6 @@ bool NaClBitcodeReader::ParseModule(bool Resume) { return Error("Malformed block record"); SmallVector<uint64_t, 64> Record; - std::vector<std::string> SectionTable; - std::vector<std::string> GCTable; // Read all the records for this module. while (1) { @@ -1429,7 +1391,7 @@ bool NaClBitcodeReader::ParseModule(bool Resume) { SeenValueSymbolTable = true; break; case naclbitc::CONSTANTS_BLOCK_ID: - if (ParseConstants() || ResolveAliasInits()) + if (ParseConstants()) return true; break; case naclbitc::FUNCTION_BLOCK_ID: @@ -1469,10 +1431,16 @@ bool NaClBitcodeReader::ParseModule(bool Resume) { break; } - // Read a record. - switch (Stream.readRecord(Entry.ID, Record)) { - default: break; // Default behavior, ignore unknown content. + unsigned Selector = Stream.readRecord(Entry.ID, Record); + switch (Selector) { + default: { + std::string Message; + raw_string_ostream StrM(Message); + StrM << "Invalid MODULE_CODE: " << Selector; + StrM.flush(); + return Error(Message); + } case naclbitc::MODULE_CODE_VERSION: { // VERSION: [version#] if (Record.size() < 1) return Error("Malformed MODULE_CODE_VERSION"); @@ -1489,40 +1457,6 @@ bool NaClBitcodeReader::ParseModule(bool Resume) { } break; } - case naclbitc::MODULE_CODE_ASM: { // ASM: [strchr x N] - std::string S; - if (ConvertToString(Record, 0, S)) - return Error("Invalid MODULE_CODE_ASM record"); - TheModule->setModuleInlineAsm(S); - break; - } - case naclbitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] - // FIXME: Remove in 4.0. - std::string S; - if (ConvertToString(Record, 0, S)) - return Error("Invalid MODULE_CODE_DEPLIB record"); - // Ignore value. - break; - } - case naclbitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] - std::string S; - if (ConvertToString(Record, 0, S)) - return Error("Invalid MODULE_CODE_SECTIONNAME record"); - SectionTable.push_back(S); - break; - } - case naclbitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] - std::string S; - if (ConvertToString(Record, 0, S)) - return Error("Invalid MODULE_CODE_GCNAME record"); - GCTable.push_back(S); - break; - } - // GLOBALVAR: [pointer type, isconst, initid, - // linkage, alignment, section, visibility, threadlocal, - // unnamed_addr] - case naclbitc::MODULE_CODE_GLOBALVAR: - return Error("Invalid MODULE_CODE_GLOBALVAR record"); // FUNCTION: [type, callingconv, isproto, linkage] case naclbitc::MODULE_CODE_FUNCTION: { if (Record.size() < 4) @@ -1552,32 +1486,6 @@ bool NaClBitcodeReader::ParseModule(bool Resume) { } break; } - // ALIAS: [alias type, aliasee val#, linkage] - // ALIAS: [alias type, aliasee val#, linkage, visibility] - case naclbitc::MODULE_CODE_ALIAS: { - if (Record.size() < 3) - return Error("Invalid MODULE_ALIAS record"); - Type *Ty = getTypeByID(Record[0]); - if (!Ty) return Error("Invalid MODULE_ALIAS record"); - if (!Ty->isPointerTy()) - return Error("Function not a pointer type!"); - - GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), - "", 0, TheModule); - // Old bitcode files didn't have visibility field. - if (Record.size() > 3) - NewGA->setVisibility(GetDecodedVisibility(Record[3])); - ValueList.push_back(NewGA); - AliasInits.push_back(std::make_pair(NewGA, Record[1])); - break; - } - /// MODULE_CODE_PURGEVALS: [numvals] - case naclbitc::MODULE_CODE_PURGEVALS: - // Trim down the value list to the specified size. - if (Record.size() < 1 || Record[0] > ValueList.size()) - return Error("Invalid MODULE_PURGEVALS record"); - ValueList.shrinkTo(Record[0]); - break; } Record.clear(); } diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h index 4105eb53b8..3b9d8982d5 100644 --- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h @@ -126,14 +126,12 @@ class NaClBitcodeReader : public GVMaterializer { uint64_t NextUnreadBit; bool SeenValueSymbolTable; - const char *ErrorString; + std::string ErrorString; std::vector<Type*> TypeList; NaClBitcodeReaderValueList ValueList; SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords; - std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; - /// FunctionBBs - While parsing a function body, this is a list of the basic /// blocks for the function. std::vector<BasicBlock*> FunctionBBs; @@ -179,7 +177,7 @@ public: bool AcceptSupportedOnly = true) : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false), LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false), - ErrorString(0), ValueList(C), + ValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), AcceptSupportedBitcodeOnly(AcceptSupportedOnly) { } @@ -187,7 +185,7 @@ public: bool AcceptSupportedOnly = true) : Context(C), TheModule(0), Buffer(0), BufferOwned(false), LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), - ErrorString(0), ValueList(C), + ValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), AcceptSupportedBitcodeOnly(AcceptSupportedOnly) { } @@ -213,7 +211,11 @@ public: ErrorString = Str; return true; } - const char *getErrorString() const { return ErrorString; } + bool Error(const std::string &Str) { + ErrorString = Str; + return true; + } + const char *getErrorString() const { return ErrorString.c_str(); } /// @brief Main interface to parsing a bitcode buffer. /// @returns true if an error occurred. @@ -282,7 +284,6 @@ private: bool RememberAndSkipFunctionBody(); bool ParseFunctionBody(Function *F); bool GlobalCleanup(); - bool ResolveAliasInits(); bool ParseUseLists(); bool InitStream(); bool InitStreamFromBuffer(); diff --git a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp index 33d0d84cb5..9884014b4b 100644 --- a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp +++ b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp @@ -405,15 +405,6 @@ static unsigned getEncodedLinkage(const GlobalValue *GV) { llvm_unreachable("Invalid linkage"); } -static unsigned getEncodedVisibility(const GlobalValue *GV) { - switch (GV->getVisibility()) { - case GlobalValue::DefaultVisibility: return 0; - case GlobalValue::HiddenVisibility: return 1; - case GlobalValue::ProtectedVisibility: return 2; - } - llvm_unreachable("Invalid visibility"); -} - /// \brief Function to convert constant initializers for global /// variables into corresponding bitcode. Takes advantage that these /// global variable initializations are normalized (see @@ -539,48 +530,6 @@ static void WriteGlobalVars(const Module *M, static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, NaClBitstreamWriter &Stream) { DEBUG(dbgs() << "-> WriteModuleInfo\n"); - // Emit various pieces of data attached to a module. - if (!M->getModuleInlineAsm().empty()) - WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), - 0/*TODO*/, Stream); - - // Emit information about sections and GC, computing how many there are. Also - // compute the maximum alignment value. - // TODO(kschimpf): Remove code for SectionMap and GCMap. - std::map<std::string, unsigned> SectionMap; - std::map<std::string, unsigned> GCMap; - for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); - GV != E; ++GV) { - if (GV->hasSection()) { - // Give section names unique ID's. - unsigned &Entry = SectionMap[GV->getSection()]; - if (!Entry) { - WriteStringRecord(naclbitc::MODULE_CODE_SECTIONNAME, GV->getSection(), - 0/*TODO*/, Stream); - Entry = SectionMap.size(); - } - } - } - for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { - if (F->hasSection()) { - // Give section names unique ID's. - unsigned &Entry = SectionMap[F->getSection()]; - if (!Entry) { - WriteStringRecord(naclbitc::MODULE_CODE_SECTIONNAME, F->getSection(), - 0/*TODO*/, Stream); - Entry = SectionMap.size(); - } - } - if (F->hasGC()) { - // Same for GC names. - unsigned &Entry = GCMap[F->getGC()]; - if (!Entry) { - WriteStringRecord(naclbitc::MODULE_CODE_GCNAME, F->getGC(), - 0/*TODO*/, Stream); - Entry = GCMap.size(); - } - } - } // Emit the function proto information. Note: We do this before // global variables, so that global variable initializations can @@ -600,19 +549,6 @@ static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, // Emit the global variable information. WriteGlobalVars(M, VE, Stream); - - // Emit the alias information. - for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); - AI != E; ++AI) { - // ALIAS: [alias type, aliasee val#, linkage, visibility] - Vals.push_back(VE.getTypeID(AI->getType())); - Vals.push_back(VE.getValueID(AI->getAliasee())); - Vals.push_back(getEncodedLinkage(AI)); - Vals.push_back(getEncodedVisibility(AI)); - unsigned AbbrevToUse = 0; - Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); - Vals.clear(); - } DEBUG(dbgs() << "<- WriteModuleInfo\n"); } |