1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitcodeReader class.
12 //===----------------------------------------------------------------------===//
14 #include "BitcodeReader.h"
15 #include "llvm/Bitcode/BitstreamReader.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/MathExtras.h"
23 /// ConvertToString - Convert a string from a record into an std::string, return
25 template<typename StrTy>
26 static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx,
28 if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1)
31 for (unsigned i = 0, e = Record[Idx]; i != e; ++i)
32 Result += (char)Record[Idx+i+1];
36 static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
38 default: // Map unknown/new linkages to external
39 case 0: return GlobalValue::ExternalLinkage;
40 case 1: return GlobalValue::WeakLinkage;
41 case 2: return GlobalValue::AppendingLinkage;
42 case 3: return GlobalValue::InternalLinkage;
43 case 4: return GlobalValue::LinkOnceLinkage;
44 case 5: return GlobalValue::DLLImportLinkage;
45 case 6: return GlobalValue::DLLExportLinkage;
46 case 7: return GlobalValue::ExternalWeakLinkage;
50 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
52 default: // Map unknown visibilities to default.
53 case 0: return GlobalValue::DefaultVisibility;
54 case 1: return GlobalValue::HiddenVisibility;
59 const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
60 // If the TypeID is in range, return it.
61 if (ID < TypeList.size())
62 return TypeList[ID].get();
63 if (!isTypeTable) return 0;
65 // The type table allows forward references. Push as many Opaque types as
66 // needed to get up to ID.
67 while (TypeList.size() <= ID)
68 TypeList.push_back(OpaqueType::get());
69 return TypeList.back().get();
73 bool BitcodeReader::ParseTypeTable(BitstreamReader &Stream) {
74 if (Stream.EnterSubBlock())
75 return Error("Malformed block record");
77 if (!TypeList.empty())
78 return Error("Multiple TYPE_BLOCKs found!");
80 SmallVector<uint64_t, 64> Record;
81 unsigned NumRecords = 0;
83 // Read all the records for this type table.
85 unsigned Code = Stream.ReadCode();
86 if (Code == bitc::END_BLOCK) {
87 if (NumRecords != TypeList.size())
88 return Error("Invalid type forward reference in TYPE_BLOCK");
89 return Stream.ReadBlockEnd();
92 if (Code == bitc::ENTER_SUBBLOCK) {
93 // No known subblocks, always skip them.
94 Stream.ReadSubBlockID();
95 if (Stream.SkipBlock())
96 return Error("Malformed block record");
100 if (Code == bitc::DEFINE_ABBREV) {
101 Stream.ReadAbbrevRecord();
107 const Type *ResultTy = 0;
108 switch (Stream.ReadRecord(Code, Record)) {
109 default: // Default behavior: unknown type.
112 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
113 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
114 // type list. This allows us to reserve space.
115 if (Record.size() < 1)
116 return Error("Invalid TYPE_CODE_NUMENTRY record");
117 TypeList.reserve(Record[0]);
119 case bitc::TYPE_CODE_META: // TYPE_CODE_META: [metacode]...
120 // No metadata supported yet.
121 if (Record.size() < 1)
122 return Error("Invalid TYPE_CODE_META record");
125 case bitc::TYPE_CODE_VOID: // VOID
126 ResultTy = Type::VoidTy;
128 case bitc::TYPE_CODE_FLOAT: // FLOAT
129 ResultTy = Type::FloatTy;
131 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
132 ResultTy = Type::DoubleTy;
134 case bitc::TYPE_CODE_LABEL: // LABEL
135 ResultTy = Type::LabelTy;
137 case bitc::TYPE_CODE_OPAQUE: // OPAQUE
140 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
141 if (Record.size() < 1)
142 return Error("Invalid Integer type record");
144 ResultTy = IntegerType::get(Record[0]);
146 case bitc::TYPE_CODE_POINTER: // POINTER: [pointee type]
147 if (Record.size() < 1)
148 return Error("Invalid POINTER type record");
149 ResultTy = PointerType::get(getTypeByID(Record[0], true));
151 case bitc::TYPE_CODE_FUNCTION: {
152 // FUNCTION: [vararg, retty, #pararms, paramty N]
153 if (Record.size() < 3 || Record.size() < Record[2]+3)
154 return Error("Invalid FUNCTION type record");
155 std::vector<const Type*> ArgTys;
156 for (unsigned i = 0, e = Record[2]; i != e; ++i)
157 ArgTys.push_back(getTypeByID(Record[3+i], true));
160 ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys,
164 case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, #elts, eltty x N]
165 if (Record.size() < 2 || Record.size() < Record[1]+2)
166 return Error("Invalid STRUCT type record");
167 std::vector<const Type*> EltTys;
168 for (unsigned i = 0, e = Record[1]; i != e; ++i)
169 EltTys.push_back(getTypeByID(Record[2+i], true));
170 ResultTy = StructType::get(EltTys, Record[0]);
173 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
174 if (Record.size() < 2)
175 return Error("Invalid ARRAY type record");
176 ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]);
178 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
179 if (Record.size() < 2)
180 return Error("Invalid VECTOR type record");
181 ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]);
185 if (NumRecords == TypeList.size()) {
186 // If this is a new type slot, just append it.
187 TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get());
189 } else if (ResultTy == 0) {
190 // Otherwise, this was forward referenced, so an opaque type was created,
191 // but the result type is actually just an opaque. Leave the one we
192 // created previously.
195 // Otherwise, this was forward referenced, so an opaque type was created.
196 // Resolve the opaque type to the real type now.
197 assert(NumRecords < TypeList.size() && "Typelist imbalance");
198 const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get());
200 // Don't directly push the new type on the Tab. Instead we want to replace
201 // the opaque type we previously inserted with the new concrete value. The
202 // refinement from the abstract (opaque) type to the new type causes all
203 // uses of the abstract type to use the concrete type (NewTy). This will
204 // also cause the opaque type to be deleted.
205 const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy);
207 // This should have replaced the old opaque type with the new type in the
208 // value table... or with a preexisting type that was already in the
209 // system. Let's just make sure it did.
210 assert(TypeList[NumRecords-1].get() != OldTy &&
211 "refineAbstractType didn't work!");
217 bool BitcodeReader::ParseTypeSymbolTable(BitstreamReader &Stream) {
218 if (Stream.EnterSubBlock())
219 return Error("Malformed block record");
221 SmallVector<uint64_t, 64> Record;
223 // Read all the records for this type table.
224 std::string TypeName;
226 unsigned Code = Stream.ReadCode();
227 if (Code == bitc::END_BLOCK)
228 return Stream.ReadBlockEnd();
230 if (Code == bitc::ENTER_SUBBLOCK) {
231 // No known subblocks, always skip them.
232 Stream.ReadSubBlockID();
233 if (Stream.SkipBlock())
234 return Error("Malformed block record");
238 if (Code == bitc::DEFINE_ABBREV) {
239 Stream.ReadAbbrevRecord();
245 switch (Stream.ReadRecord(Code, Record)) {
246 default: // Default behavior: unknown type.
248 case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namelen, namechar x N]
249 if (ConvertToString(Record, 1, TypeName))
250 return Error("Invalid TST_ENTRY record");
251 unsigned TypeID = Record[0];
252 if (TypeID >= TypeList.size())
253 return Error("Invalid Type ID in TST_ENTRY record");
255 TheModule->addTypeName(TypeName, TypeList[TypeID].get());
262 bool BitcodeReader::ParseValueSymbolTable(BitstreamReader &Stream) {
263 if (Stream.EnterSubBlock())
264 return Error("Malformed block record");
266 SmallVector<uint64_t, 64> Record;
268 // Read all the records for this value table.
269 SmallString<128> ValueName;
271 unsigned Code = Stream.ReadCode();
272 if (Code == bitc::END_BLOCK)
273 return Stream.ReadBlockEnd();
275 if (Code == bitc::ENTER_SUBBLOCK) {
276 // No known subblocks, always skip them.
277 Stream.ReadSubBlockID();
278 if (Stream.SkipBlock())
279 return Error("Malformed block record");
283 if (Code == bitc::DEFINE_ABBREV) {
284 Stream.ReadAbbrevRecord();
290 switch (Stream.ReadRecord(Code, Record)) {
291 default: // Default behavior: unknown type.
293 case bitc::TST_CODE_ENTRY: // VST_ENTRY: [valueid, namelen, namechar x N]
294 if (ConvertToString(Record, 1, ValueName))
295 return Error("Invalid TST_ENTRY record");
296 unsigned ValueID = Record[0];
297 if (ValueID >= ValueList.size())
298 return Error("Invalid Value ID in VST_ENTRY record");
299 Value *V = ValueList[ValueID];
301 V->setName(&ValueName[0], ValueName.size());
308 /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in
309 /// the LSB for dense VBR encoding.
310 static uint64_t DecodeSignRotatedValue(uint64_t V) {
315 // There is no such thing as -0 with integers. "-0" really means MININT.
319 bool BitcodeReader::ParseConstants(BitstreamReader &Stream) {
320 if (Stream.EnterSubBlock())
321 return Error("Malformed block record");
323 SmallVector<uint64_t, 64> Record;
325 // Read all the records for this value table.
326 const Type *CurTy = Type::Int32Ty;
328 unsigned Code = Stream.ReadCode();
329 if (Code == bitc::END_BLOCK) {
330 // If there are global var inits to process, do so now.
331 if (!GlobalInits.empty()) {
332 while (!GlobalInits.empty()) {
333 unsigned ValID = GlobalInits.back().second;
334 if (ValID >= ValueList.size())
335 return Error("Invalid value ID for global var init!");
336 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
337 GlobalInits.back().first->setInitializer(C);
339 return Error("Global variable initializer is not a constant!");
340 GlobalInits.pop_back();
344 return Stream.ReadBlockEnd();
347 if (Code == bitc::ENTER_SUBBLOCK) {
348 // No known subblocks, always skip them.
349 Stream.ReadSubBlockID();
350 if (Stream.SkipBlock())
351 return Error("Malformed block record");
355 if (Code == bitc::DEFINE_ABBREV) {
356 Stream.ReadAbbrevRecord();
363 switch (Stream.ReadRecord(Code, Record)) {
364 default: // Default behavior: unknown constant
365 case bitc::CST_CODE_UNDEF: // UNDEF
366 V = UndefValue::get(CurTy);
368 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
370 return Error("Malformed CST_SETTYPE record");
371 if (Record[0] >= TypeList.size())
372 return Error("Invalid Type ID in CST_SETTYPE record");
373 CurTy = TypeList[Record[0]];
374 continue; // Skip the ValueList manipulation.
375 case bitc::CST_CODE_NULL: // NULL
376 V = Constant::getNullValue(CurTy);
378 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
379 if (!isa<IntegerType>(CurTy) || Record.empty())
380 return Error("Invalid CST_INTEGER record");
381 V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0]));
383 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n, n x intval]
384 if (!isa<IntegerType>(CurTy) || Record.empty() ||
385 Record.size() < Record[0]+1)
386 return Error("Invalid WIDE_INTEGER record");
388 unsigned NumWords = Record[0];
389 uint64_t *Data = new uint64_t[NumWords];
390 for (unsigned i = 0; i != NumWords; ++i)
391 Data[i] = DecodeSignRotatedValue(Record[i+1]);
392 V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(),
396 case bitc::CST_CODE_FLOAT: // FLOAT: [fpval]
398 return Error("Invalid FLOAT record");
399 if (CurTy == Type::FloatTy)
400 V = ConstantFP::get(CurTy, BitsToFloat(Record[0]));
401 else if (CurTy == Type::DoubleTy)
402 V = ConstantFP::get(CurTy, BitsToDouble(Record[0]));
404 V = UndefValue::get(CurTy);
408 ValueList.push_back(V);
412 bool BitcodeReader::ParseModule(BitstreamReader &Stream,
413 const std::string &ModuleID) {
414 // Reject multiple MODULE_BLOCK's in a single bitstream.
416 return Error("Multiple MODULE_BLOCKs in same stream");
418 if (Stream.EnterSubBlock())
419 return Error("Malformed block record");
421 // Otherwise, create the module.
422 TheModule = new Module(ModuleID);
424 SmallVector<uint64_t, 64> Record;
425 std::vector<std::string> SectionTable;
427 // Read all the records for this module.
428 while (!Stream.AtEndOfStream()) {
429 unsigned Code = Stream.ReadCode();
430 if (Code == bitc::END_BLOCK) {
431 if (!GlobalInits.empty())
432 return Error("Malformed global initializer set");
433 return Stream.ReadBlockEnd();
436 if (Code == bitc::ENTER_SUBBLOCK) {
437 switch (Stream.ReadSubBlockID()) {
438 default: // Skip unknown content.
439 if (Stream.SkipBlock())
440 return Error("Malformed block record");
442 case bitc::TYPE_BLOCK_ID:
443 if (ParseTypeTable(Stream))
446 case bitc::TYPE_SYMTAB_BLOCK_ID:
447 if (ParseTypeSymbolTable(Stream))
450 case bitc::VALUE_SYMTAB_BLOCK_ID:
451 if (ParseValueSymbolTable(Stream))
454 case bitc::CONSTANTS_BLOCK_ID:
455 if (ParseConstants(Stream))
462 if (Code == bitc::DEFINE_ABBREV) {
463 Stream.ReadAbbrevRecord();
468 switch (Stream.ReadRecord(Code, Record)) {
469 default: break; // Default behavior, ignore unknown content.
470 case bitc::MODULE_CODE_VERSION: // VERSION: [version#]
471 if (Record.size() < 1)
472 return Error("Malformed MODULE_CODE_VERSION");
473 // Only version #0 is supported so far.
475 return Error("Unknown bitstream version!");
477 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strlen, strchr x N]
479 if (ConvertToString(Record, 0, S))
480 return Error("Invalid MODULE_CODE_TRIPLE record");
481 TheModule->setTargetTriple(S);
484 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strlen, strchr x N]
486 if (ConvertToString(Record, 0, S))
487 return Error("Invalid MODULE_CODE_DATALAYOUT record");
488 TheModule->setDataLayout(S);
491 case bitc::MODULE_CODE_ASM: { // ASM: [strlen, strchr x N]
493 if (ConvertToString(Record, 0, S))
494 return Error("Invalid MODULE_CODE_ASM record");
495 TheModule->setModuleInlineAsm(S);
498 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strlen, strchr x N]
500 if (ConvertToString(Record, 0, S))
501 return Error("Invalid MODULE_CODE_DEPLIB record");
502 TheModule->addLibrary(S);
505 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strlen, strchr x N]
507 if (ConvertToString(Record, 0, S))
508 return Error("Invalid MODULE_CODE_SECTIONNAME record");
509 SectionTable.push_back(S);
512 // GLOBALVAR: [type, isconst, initid,
513 // linkage, alignment, section, visibility, threadlocal]
514 case bitc::MODULE_CODE_GLOBALVAR: {
515 if (Record.size() < 6)
516 return Error("Invalid MODULE_CODE_GLOBALVAR record");
517 const Type *Ty = getTypeByID(Record[0]);
518 if (!isa<PointerType>(Ty))
519 return Error("Global not a pointer type!");
520 Ty = cast<PointerType>(Ty)->getElementType();
522 bool isConstant = Record[1];
523 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
524 unsigned Alignment = (1 << Record[4]) >> 1;
527 if (Record[5]-1 >= SectionTable.size())
528 return Error("Invalid section ID");
529 Section = SectionTable[Record[5]-1];
531 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
532 if (Record.size() >= 6) Visibility = GetDecodedVisibility(Record[6]);
533 bool isThreadLocal = false;
534 if (Record.size() >= 7) isThreadLocal = Record[7];
536 GlobalVariable *NewGV =
537 new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule);
538 NewGV->setAlignment(Alignment);
539 if (!Section.empty())
540 NewGV->setSection(Section);
541 NewGV->setVisibility(Visibility);
542 NewGV->setThreadLocal(isThreadLocal);
544 ValueList.push_back(NewGV);
546 // Remember which value to use for the global initializer.
547 if (unsigned InitID = Record[2])
548 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
551 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
553 case bitc::MODULE_CODE_FUNCTION: {
554 if (Record.size() < 7)
555 return Error("Invalid MODULE_CODE_FUNCTION record");
556 const Type *Ty = getTypeByID(Record[0]);
557 if (!isa<PointerType>(Ty))
558 return Error("Function not a pointer type!");
559 const FunctionType *FTy =
560 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
562 return Error("Function not a pointer to function type!");
564 Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
567 Func->setCallingConv(Record[1]);
568 Func->setLinkage(GetDecodedLinkage(Record[3]));
569 Func->setAlignment((1 << Record[4]) >> 1);
571 if (Record[5]-1 >= SectionTable.size())
572 return Error("Invalid section ID");
573 Func->setSection(SectionTable[Record[5]-1]);
575 Func->setVisibility(GetDecodedVisibility(Record[6]));
577 ValueList.push_back(Func);
578 // TODO: remember initializer/global pair for later substitution.
585 return Error("Premature end of bitstream");
589 bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length,
590 const std::string &ModuleID) {
594 return Error("Bitcode stream should be a multiple of 4 bytes in length");
596 BitstreamReader Stream(Buf, Buf+Length);
598 // Sniff for the signature.
599 if (Stream.Read(8) != 'B' ||
600 Stream.Read(8) != 'C' ||
601 Stream.Read(4) != 0x0 ||
602 Stream.Read(4) != 0xC ||
603 Stream.Read(4) != 0xE ||
604 Stream.Read(4) != 0xD)
605 return Error("Invalid bitcode signature");
607 // We expect a number of well-defined blocks, though we don't necessarily
608 // need to understand them all.
609 while (!Stream.AtEndOfStream()) {
610 unsigned Code = Stream.ReadCode();
612 if (Code != bitc::ENTER_SUBBLOCK)
613 return Error("Invalid record at top-level");
615 unsigned BlockID = Stream.ReadSubBlockID();
617 // We only know the MODULE subblock ID.
618 if (BlockID == bitc::MODULE_BLOCK_ID) {
619 if (ParseModule(Stream, ModuleID))
621 } else if (Stream.SkipBlock()) {
622 return Error("Malformed block record");