add support for BLOCKINFO records at the module level. This fixes the reader
[oota-llvm.git] / lib / Bitcode / Reader / BitcodeReader.cpp
index 48604ca91daacfb8f993293737f5438b754527b5..79ddcf799e9cfbd4bf38615b64c7c4379e0540e0 100644 (file)
@@ -17,6 +17,7 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/Instructions.h"
 #include "llvm/Module.h"
+#include "llvm/ParameterAttributes.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -26,17 +27,20 @@ BitcodeReader::~BitcodeReader() {
   delete Buffer;
 }
 
+//===----------------------------------------------------------------------===//
+//  Helper functions to implement forward reference resolution, etc.
+//===----------------------------------------------------------------------===//
 
 /// ConvertToString - Convert a string from a record into an std::string, return
 /// true on failure.
 template<typename StrTy>
 static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx,
                             StrTy &Result) {
-  if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1)
+  if (Idx > Record.size())
     return true;
   
-  for (unsigned i = 0, e = Record[Idx]; i != e; ++i)
-    Result += (char)Record[Idx+i+1];
+  for (unsigned i = Idx, e = Record.size(); i != e; ++i)
+    Result += (char)Record[i];
   return false;
 }
 
@@ -173,8 +177,69 @@ const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
   return TypeList.back().get();
 }
 
+//===----------------------------------------------------------------------===//
+//  Functions for parsing blocks from the bitcode file
+//===----------------------------------------------------------------------===//
+
+bool BitcodeReader::ParseParamAttrBlock() {
+  if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
+    return Error("Malformed block record");
+  
+  if (!ParamAttrs.empty())
+    return Error("Multiple PARAMATTR blocks found!");
+  
+  SmallVector<uint64_t, 64> Record;
+  
+  ParamAttrsVector Attrs;
+  
+  // Read all the records.
+  while (1) {
+    unsigned Code = Stream.ReadCode();
+    if (Code == bitc::END_BLOCK) {
+      if (Stream.ReadBlockEnd())
+        return Error("Error at end of PARAMATTR block");
+      return false;
+    }
+    
+    if (Code == bitc::ENTER_SUBBLOCK) {
+      // No known subblocks, always skip them.
+      Stream.ReadSubBlockID();
+      if (Stream.SkipBlock())
+        return Error("Malformed block record");
+      continue;
+    }
+    
+    if (Code == bitc::DEFINE_ABBREV) {
+      Stream.ReadAbbrevRecord();
+      continue;
+    }
+    
+    // Read a record.
+    Record.clear();
+    switch (Stream.ReadRecord(Code, Record)) {
+    default:  // Default behavior: ignore.
+      break;
+    case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...]
+      if (Record.size() & 1)
+        return Error("Invalid ENTRY record");
+
+      ParamAttrsWithIndex PAWI;
+      for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
+        PAWI.index = Record[i];
+        PAWI.attrs = Record[i+1];
+        Attrs.push_back(PAWI);
+      }
+      ParamAttrs.push_back(ParamAttrsList::get(Attrs));
+      Attrs.clear();
+      break;
+    }
+    }    
+  }
+}
+
+
 bool BitcodeReader::ParseTypeTable() {
-  if (Stream.EnterSubBlock())
+  if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID))
     return Error("Malformed block record");
   
   if (!TypeList.empty())
@@ -221,12 +286,6 @@ bool BitcodeReader::ParseTypeTable() {
         return Error("Invalid TYPE_CODE_NUMENTRY record");
       TypeList.reserve(Record[0]);
       continue;
-    case bitc::TYPE_CODE_META:      // TYPE_CODE_META: [metacode]...
-      // No metadata supported yet.
-      if (Record.size() < 1)
-        return Error("Invalid TYPE_CODE_META record");
-      continue;
-      
     case bitc::TYPE_CODE_VOID:      // VOID
       ResultTy = Type::VoidTy;
       break;
@@ -254,24 +313,23 @@ bool BitcodeReader::ParseTypeTable() {
       ResultTy = PointerType::get(getTypeByID(Record[0], true));
       break;
     case bitc::TYPE_CODE_FUNCTION: {
-      // FUNCTION: [vararg, retty, #pararms, paramty N]
-      if (Record.size() < 3 || Record.size() < Record[2]+3)
+      // FUNCTION: [vararg, attrid, retty, paramty x N]
+      if (Record.size() < 3)
         return Error("Invalid FUNCTION type record");
       std::vector<const Type*> ArgTys;
-      for (unsigned i = 0, e = Record[2]; i != e; ++i)
-        ArgTys.push_back(getTypeByID(Record[3+i], true));
+      for (unsigned i = 3, e = Record.size(); i != e; ++i)
+        ArgTys.push_back(getTypeByID(Record[i], true));
       
-      // FIXME: PARAM TYS.
-      ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys,
-                                   Record[0]);
+      ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys,
+                                   Record[0], getParamAttrs(Record[1]));
       break;
     }
-    case bitc::TYPE_CODE_STRUCT: {  // STRUCT: [ispacked, #elts, eltty x N]
-      if (Record.size() < 2 || Record.size() < Record[1]+2)
+    case bitc::TYPE_CODE_STRUCT: {  // STRUCT: [ispacked, eltty x N]
+      if (Record.size() < 2)
         return Error("Invalid STRUCT type record");
       std::vector<const Type*> EltTys;
-      for (unsigned i = 0, e = Record[1]; i != e; ++i)
-        EltTys.push_back(getTypeByID(Record[2+i], true));
+      for (unsigned i = 1, e = Record.size(); i != e; ++i)
+        EltTys.push_back(getTypeByID(Record[i], true));
       ResultTy = StructType::get(EltTys, Record[0]);
       break;
     }
@@ -320,7 +378,7 @@ bool BitcodeReader::ParseTypeTable() {
 
 
 bool BitcodeReader::ParseTypeSymbolTable() {
-  if (Stream.EnterSubBlock())
+  if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID))
     return Error("Malformed block record");
   
   SmallVector<uint64_t, 64> Record;
@@ -353,7 +411,7 @@ bool BitcodeReader::ParseTypeSymbolTable() {
     switch (Stream.ReadRecord(Code, Record)) {
     default:  // Default behavior: unknown type.
       break;
-    case bitc::TST_CODE_ENTRY:    // TST_ENTRY: [typeid, namelen, namechar x N]
+    case bitc::TST_CODE_ENTRY:    // TST_ENTRY: [typeid, namechar x N]
       if (ConvertToString(Record, 1, TypeName))
         return Error("Invalid TST_ENTRY record");
       unsigned TypeID = Record[0];
@@ -368,7 +426,7 @@ bool BitcodeReader::ParseTypeSymbolTable() {
 }
 
 bool BitcodeReader::ParseValueSymbolTable() {
-  if (Stream.EnterSubBlock())
+  if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
     return Error("Malformed block record");
 
   SmallVector<uint64_t, 64> Record;
@@ -400,7 +458,7 @@ bool BitcodeReader::ParseValueSymbolTable() {
     switch (Stream.ReadRecord(Code, Record)) {
     default:  // Default behavior: unknown type.
       break;
-    case bitc::VST_CODE_ENTRY: {  // VST_ENTRY: [valueid, namelen, namechar x N]
+    case bitc::VST_CODE_ENTRY: {  // VST_ENTRY: [valueid, namechar x N]
       if (ConvertToString(Record, 1, ValueName))
         return Error("Invalid TST_ENTRY record");
       unsigned ValueID = Record[0];
@@ -478,7 +536,7 @@ bool BitcodeReader::ResolveGlobalAndAliasInits() {
 
 
 bool BitcodeReader::ParseConstants() {
-  if (Stream.EnterSubBlock())
+  if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
     return Error("Malformed block record");
 
   SmallVector<uint64_t, 64> Record;
@@ -533,16 +591,15 @@ bool BitcodeReader::ParseConstants() {
         return Error("Invalid CST_INTEGER record");
       V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0]));
       break;
-    case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n, n x intval]
-      if (!isa<IntegerType>(CurTy) || Record.empty() ||
-          Record.size() < Record[0]+1)
+    case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
+      if (!isa<IntegerType>(CurTy) || Record.empty())
         return Error("Invalid WIDE_INTEGER record");
       
-      unsigned NumWords = Record[0];
+      unsigned NumWords = Record.size();
       SmallVector<uint64_t, 8> Words;
       Words.resize(NumWords);
       for (unsigned i = 0; i != NumWords; ++i)
-        Words[i] = DecodeSignRotatedValue(Record[i+1]);
+        Words[i] = DecodeSignRotatedValue(Record[i]);
       V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(),
                                  NumWords, &Words[0]));
       break;
@@ -558,27 +615,27 @@ bool BitcodeReader::ParseConstants() {
         V = UndefValue::get(CurTy);
       break;
       
-    case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n, n x value number]
-      if (Record.empty() || Record.size() < Record[0]+1)
+    case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
+      if (Record.empty())
         return Error("Invalid CST_AGGREGATE record");
       
-      unsigned Size = Record[0];
+      unsigned Size = Record.size();
       std::vector<Constant*> Elts;
       
       if (const StructType *STy = dyn_cast<StructType>(CurTy)) {
         for (unsigned i = 0; i != Size; ++i)
-          Elts.push_back(ValueList.getConstantFwdRef(Record[i+1],
+          Elts.push_back(ValueList.getConstantFwdRef(Record[i],
                                                      STy->getElementType(i)));
         V = ConstantStruct::get(STy, Elts);
       } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
         const Type *EltTy = ATy->getElementType();
         for (unsigned i = 0; i != Size; ++i)
-          Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy));
+          Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
         V = ConstantArray::get(ATy, Elts);
       } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
         const Type *EltTy = VTy->getElementType();
         for (unsigned i = 0; i != Size; ++i)
-          Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy));
+          Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
         V = ConstantVector::get(Elts);
       } else {
         V = UndefValue::get(CurTy);
@@ -611,9 +668,9 @@ bool BitcodeReader::ParseConstants() {
       break;
     }  
     case bitc::CST_CODE_CE_GEP: {  // CE_GEP:        [n x operands]
-      if ((Record.size() & 1) == 0) return Error("Invalid CE_GEP record");
+      if (Record.size() & 1) return Error("Invalid CE_GEP record");
       SmallVector<Constant*, 16> Elts;
-      for (unsigned i = 1, e = Record.size(); i != e; i += 2) {
+      for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
         const Type *ElTy = getTypeByID(Record[i]);
         if (!ElTy) return Error("Invalid CE_GEP record");
         Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
@@ -711,7 +768,7 @@ bool BitcodeReader::ParseModule(const std::string &ModuleID) {
   if (TheModule)
     return Error("Multiple MODULE_BLOCKs in same stream");
   
-  if (Stream.EnterSubBlock())
+  if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
     return Error("Malformed block record");
 
   // Otherwise, create the module.
@@ -748,6 +805,14 @@ bool BitcodeReader::ParseModule(const std::string &ModuleID) {
         if (Stream.SkipBlock())
           return Error("Malformed block record");
         break;
+      case bitc::BLOCKINFO_BLOCK_ID:
+        if (Stream.ReadBlockInfoBlock())
+          return Error("Malformed BlockInfoBlock");
+        break;
+      case bitc::PARAMATTR_BLOCK_ID:
+        if (ParseParamAttrBlock())
+          return true;
+        break;
       case bitc::TYPE_BLOCK_ID:
         if (ParseTypeTable())
           return true;
@@ -794,35 +859,35 @@ bool BitcodeReader::ParseModule(const std::string &ModuleID) {
       if (Record[0] != 0)
         return Error("Unknown bitstream version!");
       break;
-    case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strlen, strchr x N]
+    case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
       std::string S;
       if (ConvertToString(Record, 0, S))
         return Error("Invalid MODULE_CODE_TRIPLE record");
       TheModule->setTargetTriple(S);
       break;
     }
-    case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strlen, strchr x N]
+    case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N]
       std::string S;
       if (ConvertToString(Record, 0, S))
         return Error("Invalid MODULE_CODE_DATALAYOUT record");
       TheModule->setDataLayout(S);
       break;
     }
-    case bitc::MODULE_CODE_ASM: {  // ASM: [strlen, strchr x N]
+    case bitc::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 bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strlen, strchr x N]
+    case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N]
       std::string S;
       if (ConvertToString(Record, 0, S))
         return Error("Invalid MODULE_CODE_DEPLIB record");
       TheModule->addLibrary(S);
       break;
     }
-    case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strlen, strchr x N]
+    case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
       std::string S;
       if (ConvertToString(Record, 0, S))
         return Error("Invalid MODULE_CODE_SECTIONNAME record");
@@ -961,11 +1026,19 @@ bool BitcodeReader::ParseBitcode() {
     unsigned BlockID = Stream.ReadSubBlockID();
     
     // We only know the MODULE subblock ID.
-    if (BlockID == bitc::MODULE_BLOCK_ID) {
+    switch (BlockID) {
+    case bitc::BLOCKINFO_BLOCK_ID:
+      if (Stream.ReadBlockInfoBlock())
+        return Error("Malformed BlockInfoBlock");
+      break;
+    case bitc::MODULE_BLOCK_ID:
       if (ParseModule(Buffer->getBufferIdentifier()))
         return true;
-    } else if (Stream.SkipBlock()) {
-      return Error("Malformed block record");
+      break;
+    default:
+      if (Stream.SkipBlock())
+        return Error("Malformed block record");
+      break;
     }
   }
   
@@ -1011,7 +1084,7 @@ Module *BitcodeReader::materializeModule(std::string *ErrInfo) {
 
 /// ParseFunctionBody - Lazily parse the specified function body block.
 bool BitcodeReader::ParseFunctionBody(Function *F) {
-  if (Stream.EnterSubBlock())
+  if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
     return Error("Malformed block record");
   
   unsigned ModuleValueListSize = ValueList.size();
@@ -1094,7 +1167,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
       I = CastInst::create((Instruction::CastOps)Opc, Op, ResTy);
       break;
     }
-    case bitc::FUNC_CODE_INST_GEP: { // GEP: [n, n x operands]
+    case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
       if (Record.size() < 2 || (Record.size() & 1))
         return Error("Invalid GEP record");
       const Type *OpTy = getTypeByID(Record[0]);
@@ -1292,17 +1365,17 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
       I = new UnreachableInst();
       break;
     case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, #ops, val0,bb0, ...]
-      if (Record.size() < 2 || Record.size() < 2+Record[1] || (Record[1]&1))
+      if (Record.size() < 1 || ((Record.size()-1)&1))
         return Error("Invalid PHI record");
       const Type *Ty = getTypeByID(Record[0]);
       if (!Ty) return Error("Invalid PHI record");
       
       PHINode *PN = new PHINode(Ty);
-      PN->reserveOperandSpace(Record[1]);
+      PN->reserveOperandSpace(Record.size()-1);
       
-      for (unsigned i = 0, e = Record[1]; i != e; i += 2) {
-        Value *V = getFnValueByID(Record[2+i], Ty);
-        BasicBlock *BB = getBasicBlock(Record[3+i]);
+      for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
+        Value *V = getFnValueByID(Record[1+i], Ty);
+        BasicBlock *BB = getBasicBlock(Record[2+i]);
         if (!V || !BB) return Error("Invalid PHI record");
         PN->addIncoming(V, BB);
       }
@@ -1361,7 +1434,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
       Value *Ptr = getFnValueByID(Record[2], OpTy);
       if (!OpTy || !Op || !Ptr)
         return Error("Invalid STORE record");
-      I = new StoreInst(Op, Ptr, (1 << Record[3]) >> 1, Record[4]);
+      I = new StoreInst(Op, Ptr, Record[4], (1 << Record[3]) >> 1);
       break;
     }
     case bitc::FUNC_CODE_INST_CALL: { // CALL: [cc, fnty, fnid, arg0, arg1...]
@@ -1446,8 +1519,8 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
           delete A;
         }
       }
+      return Error("Never resolved value found in function!");
     }
-    return Error("Never resolved value found in function!");
   }
   
   // Trim the value list down to the size it was before we parsed this function.