llvm.org GIT mirror llvm / 15e6d17
eliminate internal length fields from record. Records already know their total length git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36752 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
2 changed file(s) with 49 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
3535 template
3636 static bool ConvertToString(SmallVector &Record, unsigned Idx,
3737 StrTy &Result) {
38 if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1)
38 if (Idx > Record.size())
3939 return true;
4040
41 for (unsigned i = 0, e = Record[Idx]; i != e; ++i)
42 Result += (char)Record[Idx+i+1];
41 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
42 Result += (char)Record[i];
4343 return false;
4444 }
4545
312312 ResultTy = PointerType::get(getTypeByID(Record[0], true));
313313 break;
314314 case bitc::TYPE_CODE_FUNCTION: {
315 // FUNCTION: [vararg, attrid, retty, #pararms, paramty N]
316 if (Record.size() < 4 || Record.size() < Record[3]+4)
315 // FUNCTION: [vararg, attrid, retty, paramty x N]
316 if (Record.size() < 3)
317317 return Error("Invalid FUNCTION type record");
318318 std::vector ArgTys;
319 for (unsigned i = 0, e = Record[3]; i != e; ++i)
320 ArgTys.push_back(getTypeByID(Record[4+i], true));
319 for (unsigned i = 3, e = Record.size(); i != e; ++i)
320 ArgTys.push_back(getTypeByID(Record[i], true));
321321
322322 ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys,
323323 Record[0], getParamAttrs(Record[1]));
324324 break;
325325 }
326 case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, #elts, eltty x N]
327 if (Record.size() < 2 || Record.size() < Record[1]+2)
326 case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, eltty x N]
327 if (Record.size() < 2)
328328 return Error("Invalid STRUCT type record");
329329 std::vector EltTys;
330 for (unsigned i = 0, e = Record[1]; i != e; ++i)
331 EltTys.push_back(getTypeByID(Record[2+i], true));
330 for (unsigned i = 1, e = Record.size(); i != e; ++i)
331 EltTys.push_back(getTypeByID(Record[i], true));
332332 ResultTy = StructType::get(EltTys, Record[0]);
333333 break;
334334 }
410410 switch (Stream.ReadRecord(Code, Record)) {
411411 default: // Default behavior: unknown type.
412412 break;
413 case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namelen, namechar x N]
413 case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namechar x N]
414414 if (ConvertToString(Record, 1, TypeName))
415415 return Error("Invalid TST_ENTRY record");
416416 unsigned TypeID = Record[0];
457457 switch (Stream.ReadRecord(Code, Record)) {
458458 default: // Default behavior: unknown type.
459459 break;
460 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namelen, namechar x N]
460 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
461461 if (ConvertToString(Record, 1, ValueName))
462462 return Error("Invalid TST_ENTRY record");
463463 unsigned ValueID = Record[0];
590590 return Error("Invalid CST_INTEGER record");
591591 V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0]));
592592 break;
593 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n, n x intval]
594 if (!isa(CurTy) || Record.empty() ||
595 Record.size() < Record[0]+1)
593 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
594 if (!isa(CurTy) || Record.empty())
596595 return Error("Invalid WIDE_INTEGER record");
597596
598 unsigned NumWords = Record[0];
597 unsigned NumWords = Record.size();
599598 SmallVector Words;
600599 Words.resize(NumWords);
601600 for (unsigned i = 0; i != NumWords; ++i)
602 Words[i] = DecodeSignRotatedValue(Record[i+1]);
601 Words[i] = DecodeSignRotatedValue(Record[i]);
603602 V = ConstantInt::get(APInt(cast(CurTy)->getBitWidth(),
604603 NumWords, &Words[0]));
605604 break;
615614 V = UndefValue::get(CurTy);
616615 break;
617616
618 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n, n x value number]
619 if (Record.empty() || Record.size() < Record[0]+1)
617 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
618 if (Record.empty())
620619 return Error("Invalid CST_AGGREGATE record");
621620
622 unsigned Size = Record[0];
621 unsigned Size = Record.size();
623622 std::vector Elts;
624623
625624 if (const StructType *STy = dyn_cast(CurTy)) {
626625 for (unsigned i = 0; i != Size; ++i)
627 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1],
626 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
628627 STy->getElementType(i)));
629628 V = ConstantStruct::get(STy, Elts);
630629 } else if (const ArrayType *ATy = dyn_cast(CurTy)) {
631630 const Type *EltTy = ATy->getElementType();
632631 for (unsigned i = 0; i != Size; ++i)
633 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy));
632 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
634633 V = ConstantArray::get(ATy, Elts);
635634 } else if (const VectorType *VTy = dyn_cast(CurTy)) {
636635 const Type *EltTy = VTy->getElementType();
637636 for (unsigned i = 0; i != Size; ++i)
638 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy));
637 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
639638 V = ConstantVector::get(Elts);
640639 } else {
641640 V = UndefValue::get(CurTy);
668667 break;
669668 }
670669 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
671 if ((Record.size() & 1) == 0) return Error("Invalid CE_GEP record");
670 if (Record.size() & 1) return Error("Invalid CE_GEP record");
672671 SmallVector Elts;
673 for (unsigned i = 1, e = Record.size(); i != e; i += 2) {
672 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
674673 const Type *ElTy = getTypeByID(Record[i]);
675674 if (!ElTy) return Error("Invalid CE_GEP record");
676675 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
855854 if (Record[0] != 0)
856855 return Error("Unknown bitstream version!");
857856 break;
858 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strlen, strchr x N]
857 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
859858 std::string S;
860859 if (ConvertToString(Record, 0, S))
861860 return Error("Invalid MODULE_CODE_TRIPLE record");
862861 TheModule->setTargetTriple(S);
863862 break;
864863 }
865 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strlen, strchr x N]
864 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
866865 std::string S;
867866 if (ConvertToString(Record, 0, S))
868867 return Error("Invalid MODULE_CODE_DATALAYOUT record");
869868 TheModule->setDataLayout(S);
870869 break;
871870 }
872 case bitc::MODULE_CODE_ASM: { // ASM: [strlen, strchr x N]
871 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
873872 std::string S;
874873 if (ConvertToString(Record, 0, S))
875874 return Error("Invalid MODULE_CODE_ASM record");
876875 TheModule->setModuleInlineAsm(S);
877876 break;
878877 }
879 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strlen, strchr x N]
878 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
880879 std::string S;
881880 if (ConvertToString(Record, 0, S))
882881 return Error("Invalid MODULE_CODE_DEPLIB record");
883882 TheModule->addLibrary(S);
884883 break;
885884 }
886 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strlen, strchr x N]
885 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
887886 std::string S;
888887 if (ConvertToString(Record, 0, S))
889888 return Error("Invalid MODULE_CODE_SECTIONNAME record");
11551154 I = CastInst::create((Instruction::CastOps)Opc, Op, ResTy);
11561155 break;
11571156 }
1158 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n, n x operands]
1157 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
11591158 if (Record.size() < 2 || (Record.size() & 1))
11601159 return Error("Invalid GEP record");
11611160 const Type *OpTy = getTypeByID(Record[0]);
13531352 I = new UnreachableInst();
13541353 break;
13551354 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, #ops, val0,bb0, ...]
1356 if (Record.size() < 2 || Record.size() < 2+Record[1] || (Record[1]&1))
1355 if (Record.size() < 1 || ((Record.size()-1)&1))
13571356 return Error("Invalid PHI record");
13581357 const Type *Ty = getTypeByID(Record[0]);
13591358 if (!Ty) return Error("Invalid PHI record");
13601359
13611360 PHINode *PN = new PHINode(Ty);
1362 PN->reserveOperandSpace(Record[1]);
1363
1364 for (unsigned i = 0, e = Record[1]; i != e; i += 2) {
1365 Value *V = getFnValueByID(Record[2+i], Ty);
1366 BasicBlock *BB = getBasicBlock(Record[3+i]);
1361 PN->reserveOperandSpace(Record.size()-1);
1362
1363 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
1364 Value *V = getFnValueByID(Record[1+i], Ty);
1365 BasicBlock *BB = getBasicBlock(Record[2+i]);
13671366 if (!V || !BB) return Error("Invalid PHI record");
13681367 PN->addIncoming(V, BB);
13691368 }
7171 unsigned AbbrevToUse, BitstreamWriter &Stream) {
7272 SmallVector Vals;
7373
74 // Code: [strlen, strchar x N]
75 Vals.push_back(Str.size());
74 // Code: [strchar x N]
7675 for (unsigned i = 0, e = Str.size(); i != e; ++i)
7776 Vals.push_back(Str[i]);
7877
149148 TypeVals.push_back(FT->isVarArg());
150149 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
151150 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
152 TypeVals.push_back(FT->getNumParams());
153151 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
154152 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
155153 break;
159157 // STRUCT: [ispacked, #elts, eltty x N]
160158 Code = bitc::TYPE_CODE_STRUCT;
161159 TypeVals.push_back(ST->isPacked());
162 TypeVals.push_back(ST->getNumElements());
163 // Output all of the element types...
160 // Output all of the element types.
164161 for (StructType::element_iterator I = ST->element_begin(),
165162 E = ST->element_end(); I != E; ++I)
166163 TypeVals.push_back(VE.getTypeID(*I));
398395 // So, we only write the number of active words.
399396 unsigned NWords = IV->getValue().getActiveWords();
400397 const uint64_t *RawWords = IV->getValue().getRawData();
401 Record.push_back(NWords);
402398 for (unsigned i = 0; i != NWords; ++i) {
403399 int64_t V = RawWords[i];
404400 if (V >= 0)
419415 } else if (isa(C) || isa(V) ||
420416 isa(V)) {
421417 Code = bitc::CST_CODE_AGGREGATE;
422 Record.push_back(C->getNumOperands());
423418 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
424419 Record.push_back(VE.getValueID(C->getOperand(i)));
425420 } else if (const ConstantExpr *CE = dyn_cast(C)) {
440435 break;
441436 case Instruction::GetElementPtr:
442437 Code = bitc::CST_CODE_CE_GEP;
443 Record.push_back(CE->getNumOperands());
444438 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
445439 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
446440 Record.push_back(VE.getValueID(C->getOperand(i)));
626620 case Instruction::PHI:
627621 Code = bitc::FUNC_CODE_INST_PHI;
628622 Vals.push_back(VE.getTypeID(I.getType()));
629 Vals.push_back(I.getNumOperands());
630623 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
631624 Vals.push_back(VE.getValueID(I.getOperand(i)));
632625 break;
709702 if (VST.empty()) return;
710703 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 3);
711704
705 #if 0
706 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
707 Abbv->Add(BitCodeAbbrevOp(bitc::VST_ENTRY));
708 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth,
709 Log2_32_Ceil(MaxGlobalType+1)));
710 // Don't bother emitting vis + thread local.
711 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
712 #endif
713
714
712715 // FIXME: Set up the abbrev, we know how many values there are!
713716 // FIXME: We know if the type names can use 7-bit ascii.
714717 SmallVector NameVals;
727730 }
728731
729732 NameVals.push_back(VE.getValueID(SI->getValue()));
730 NameVals.push_back(SI->getKeyLength());
731733 for (const char *P = SI->getKeyData(),
732734 *E = SI->getKeyData()+SI->getKeyLength(); P != E; ++P)
733735 NameVals.push_back((unsigned char)*P);
793795 NameVals.push_back(VE.getTypeID(TI->second));
794796
795797 const std::string &Str = TI->first;
796 NameVals.push_back(Str.size());
797798 for (unsigned i = 0, e = Str.size(); i != e; ++i)
798799 NameVals.push_back(Str[i]);
799800