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"
22 /// ConvertToString - Convert a string from a record into an std::string, return
24 template<typename StrTy>
25 static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx,
27 if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1)
30 for (unsigned i = 0, e = Record[Idx]; i != e; ++i)
31 Result += (char)Record[Idx+i+1];
35 static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
37 default: // Map unknown/new linkages to external
38 case 0: return GlobalValue::ExternalLinkage;
39 case 1: return GlobalValue::WeakLinkage;
40 case 2: return GlobalValue::AppendingLinkage;
41 case 3: return GlobalValue::InternalLinkage;
42 case 4: return GlobalValue::LinkOnceLinkage;
43 case 5: return GlobalValue::DLLImportLinkage;
44 case 6: return GlobalValue::DLLExportLinkage;
45 case 7: return GlobalValue::ExternalWeakLinkage;
49 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
51 default: // Map unknown visibilities to default.
52 case 0: return GlobalValue::DefaultVisibility;
53 case 1: return GlobalValue::HiddenVisibility;
58 const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
59 // If the TypeID is in range, return it.
60 if (ID < TypeList.size())
61 return TypeList[ID].get();
62 if (!isTypeTable) return 0;
64 // The type table allows forward references. Push as many Opaque types as
65 // needed to get up to ID.
66 while (TypeList.size() <= ID)
67 TypeList.push_back(OpaqueType::get());
68 return TypeList.back().get();
72 bool BitcodeReader::ParseTypeTable(BitstreamReader &Stream) {
73 if (Stream.EnterSubBlock())
74 return Error("Malformed block record");
76 if (!TypeList.empty())
77 return Error("Multiple TYPE_BLOCKs found!");
79 SmallVector<uint64_t, 64> Record;
80 unsigned NumRecords = 0;
82 // Read all the records for this type table.
84 unsigned Code = Stream.ReadCode();
85 if (Code == bitc::END_BLOCK) {
86 if (NumRecords != TypeList.size())
87 return Error("Invalid type forward reference in TYPE_BLOCK");
88 return Stream.ReadBlockEnd();
91 if (Code == bitc::ENTER_SUBBLOCK) {
92 // No known subblocks, always skip them.
93 Stream.ReadSubBlockID();
94 if (Stream.SkipBlock())
95 return Error("Malformed block record");
99 if (Code == bitc::DEFINE_ABBREV) {
100 Stream.ReadAbbrevRecord();
106 const Type *ResultTy = 0;
107 switch (Stream.ReadRecord(Code, Record)) {
108 default: // Default behavior: unknown type.
111 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
112 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
113 // type list. This allows us to reserve space.
114 if (Record.size() < 1)
115 return Error("Invalid TYPE_CODE_NUMENTRY record");
116 TypeList.reserve(Record[0]);
118 case bitc::TYPE_CODE_META: // TYPE_CODE_META: [metacode]...
119 // No metadata supported yet.
120 if (Record.size() < 1)
121 return Error("Invalid TYPE_CODE_META record");
124 case bitc::TYPE_CODE_VOID: // VOID
125 ResultTy = Type::VoidTy;
127 case bitc::TYPE_CODE_FLOAT: // FLOAT
128 ResultTy = Type::FloatTy;
130 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
131 ResultTy = Type::DoubleTy;
133 case bitc::TYPE_CODE_LABEL: // LABEL
134 ResultTy = Type::LabelTy;
136 case bitc::TYPE_CODE_OPAQUE: // OPAQUE
139 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
140 if (Record.size() < 1)
141 return Error("Invalid Integer type record");
143 ResultTy = IntegerType::get(Record[0]);
145 case bitc::TYPE_CODE_POINTER: // POINTER: [pointee type]
146 if (Record.size() < 1)
147 return Error("Invalid POINTER type record");
148 ResultTy = PointerType::get(getTypeByID(Record[0], true));
150 case bitc::TYPE_CODE_FUNCTION: {
151 // FUNCTION: [vararg, retty, #pararms, paramty N]
152 if (Record.size() < 3 || Record.size() < Record[2]+3)
153 return Error("Invalid FUNCTION type record");
154 std::vector<const Type*> ArgTys;
155 for (unsigned i = 0, e = Record[2]; i != e; ++i)
156 ArgTys.push_back(getTypeByID(Record[3+i], true));
159 ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys,
163 case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, #elts, eltty x N]
164 if (Record.size() < 2 || Record.size() < Record[1]+2)
165 return Error("Invalid STRUCT type record");
166 std::vector<const Type*> EltTys;
167 for (unsigned i = 0, e = Record[1]; i != e; ++i)
168 EltTys.push_back(getTypeByID(Record[2+i], true));
169 ResultTy = StructType::get(EltTys, Record[0]);
172 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
173 if (Record.size() < 2)
174 return Error("Invalid ARRAY type record");
175 ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]);
177 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
178 if (Record.size() < 2)
179 return Error("Invalid VECTOR type record");
180 ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]);
184 if (NumRecords == TypeList.size()) {
185 // If this is a new type slot, just append it.
186 TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get());
188 } else if (ResultTy == 0) {
189 // Otherwise, this was forward referenced, so an opaque type was created,
190 // but the result type is actually just an opaque. Leave the one we
191 // created previously.
194 // Otherwise, this was forward referenced, so an opaque type was created.
195 // Resolve the opaque type to the real type now.
196 assert(NumRecords < TypeList.size() && "Typelist imbalance");
197 const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get());
199 // Don't directly push the new type on the Tab. Instead we want to replace
200 // the opaque type we previously inserted with the new concrete value. The
201 // refinement from the abstract (opaque) type to the new type causes all
202 // uses of the abstract type to use the concrete type (NewTy). This will
203 // also cause the opaque type to be deleted.
204 const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy);
206 // This should have replaced the old opaque type with the new type in the
207 // value table... or with a preexisting type that was already in the system.
208 // Let's just make sure it did.
209 assert(TypeList[NumRecords-1].get() != OldTy &&
210 "refineAbstractType didn't work!");
216 bool BitcodeReader::ParseTypeSymbolTable(BitstreamReader &Stream) {
217 if (Stream.EnterSubBlock())
218 return Error("Malformed block record");
220 SmallVector<uint64_t, 64> Record;
222 // Read all the records for this type table.
223 std::string TypeName;
225 unsigned Code = Stream.ReadCode();
226 if (Code == bitc::END_BLOCK)
227 return Stream.ReadBlockEnd();
229 if (Code == bitc::ENTER_SUBBLOCK) {
230 // No known subblocks, always skip them.
231 Stream.ReadSubBlockID();
232 if (Stream.SkipBlock())
233 return Error("Malformed block record");
237 if (Code == bitc::DEFINE_ABBREV) {
238 Stream.ReadAbbrevRecord();
244 switch (Stream.ReadRecord(Code, Record)) {
245 default: // Default behavior: unknown type.
247 case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namelen, namechar x N]
248 if (ConvertToString(Record, 1, TypeName))
249 return Error("Invalid TST_ENTRY record");
250 unsigned TypeID = Record[0];
251 if (TypeID >= TypeList.size())
252 return Error("Invalid Type ID in TST_ENTRY record");
254 TheModule->addTypeName(TypeName, TypeList[TypeID].get());
261 bool BitcodeReader::ParseValueSymbolTable(BitstreamReader &Stream) {
262 if (Stream.EnterSubBlock())
263 return Error("Malformed block record");
265 SmallVector<uint64_t, 64> Record;
267 // Read all the records for this value table.
268 SmallString<128> ValueName;
270 unsigned Code = Stream.ReadCode();
271 if (Code == bitc::END_BLOCK)
272 return Stream.ReadBlockEnd();
274 if (Code == bitc::ENTER_SUBBLOCK) {
275 // No known subblocks, always skip them.
276 Stream.ReadSubBlockID();
277 if (Stream.SkipBlock())
278 return Error("Malformed block record");
282 if (Code == bitc::DEFINE_ABBREV) {
283 Stream.ReadAbbrevRecord();
289 switch (Stream.ReadRecord(Code, Record)) {
290 default: // Default behavior: unknown type.
292 case bitc::TST_CODE_ENTRY: // VST_ENTRY: [valueid, namelen, namechar x N]
293 if (ConvertToString(Record, 1, ValueName))
294 return Error("Invalid TST_ENTRY record");
295 unsigned ValueID = Record[0];
296 if (ValueID >= ValueList.size())
297 return Error("Invalid Value ID in VST_ENTRY record");
298 Value *V = ValueList[ValueID];
300 V->setName(&ValueName[0], ValueName.size());
307 bool BitcodeReader::ParseConstants(BitstreamReader &Stream) {
308 if (Stream.EnterSubBlock())
309 return Error("Malformed block record");
311 SmallVector<uint64_t, 64> Record;
313 // Read all the records for this value table.
314 const Type *CurTy = Type::Int32Ty;
316 unsigned Code = Stream.ReadCode();
317 if (Code == bitc::END_BLOCK) {
318 // If there are global var inits to process, do so now.
319 if (!GlobalInits.empty()) {
320 while (!GlobalInits.empty()) {
321 unsigned ValID = GlobalInits.back().second;
322 if (ValID >= ValueList.size())
323 return Error("Invalid value ID for global var init!");
324 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
325 GlobalInits.back().first->setInitializer(C);
327 return Error("Global variable initializer is not a constant!");
328 GlobalInits.pop_back();
332 return Stream.ReadBlockEnd();
335 if (Code == bitc::ENTER_SUBBLOCK) {
336 // No known subblocks, always skip them.
337 Stream.ReadSubBlockID();
338 if (Stream.SkipBlock())
339 return Error("Malformed block record");
343 if (Code == bitc::DEFINE_ABBREV) {
344 Stream.ReadAbbrevRecord();
351 switch (Stream.ReadRecord(Code, Record)) {
352 default: // Default behavior: unknown constant
353 case bitc::CST_CODE_UNDEF: // UNDEF
354 V = UndefValue::get(CurTy);
356 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
358 return Error("Malformed CST_SETTYPE record");
359 if (Record[0] >= TypeList.size())
360 return Error("Invalid Type ID in CST_SETTYPE record");
361 CurTy = TypeList[Record[0]];
363 case bitc::CST_CODE_NULL: // NULL
364 V = Constant::getNullValue(CurTy);
366 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
367 if (!isa<IntegerType>(CurTy))
368 return Error("Invalid type for CST_INTEGER");
370 V = ConstantInt::get(CurTy, -(Record[0]>>1));
372 V = ConstantInt::get(CurTy, Record[0]>>1);
376 ValueList.push_back(V);
380 bool BitcodeReader::ParseModule(BitstreamReader &Stream,
381 const std::string &ModuleID) {
382 // Reject multiple MODULE_BLOCK's in a single bitstream.
384 return Error("Multiple MODULE_BLOCKs in same stream");
386 if (Stream.EnterSubBlock())
387 return Error("Malformed block record");
389 // Otherwise, create the module.
390 TheModule = new Module(ModuleID);
392 SmallVector<uint64_t, 64> Record;
393 std::vector<std::string> SectionTable;
395 // Read all the records for this module.
396 while (!Stream.AtEndOfStream()) {
397 unsigned Code = Stream.ReadCode();
398 if (Code == bitc::END_BLOCK) {
399 if (!GlobalInits.empty())
400 return Error("Malformed global initializer set");
401 return Stream.ReadBlockEnd();
404 if (Code == bitc::ENTER_SUBBLOCK) {
405 switch (Stream.ReadSubBlockID()) {
406 default: // Skip unknown content.
407 if (Stream.SkipBlock())
408 return Error("Malformed block record");
410 case bitc::TYPE_BLOCK_ID:
411 if (ParseTypeTable(Stream))
414 case bitc::TYPE_SYMTAB_BLOCK_ID:
415 if (ParseTypeSymbolTable(Stream))
418 case bitc::VALUE_SYMTAB_BLOCK_ID:
419 if (ParseValueSymbolTable(Stream))
422 case bitc::CONSTANTS_BLOCK_ID:
423 if (ParseConstants(Stream))
430 if (Code == bitc::DEFINE_ABBREV) {
431 Stream.ReadAbbrevRecord();
436 switch (Stream.ReadRecord(Code, Record)) {
437 default: break; // Default behavior, ignore unknown content.
438 case bitc::MODULE_CODE_VERSION: // VERSION: [version#]
439 if (Record.size() < 1)
440 return Error("Malformed MODULE_CODE_VERSION");
441 // Only version #0 is supported so far.
443 return Error("Unknown bitstream version!");
445 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strlen, strchr x N]
447 if (ConvertToString(Record, 0, S))
448 return Error("Invalid MODULE_CODE_TRIPLE record");
449 TheModule->setTargetTriple(S);
452 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strlen, strchr x N]
454 if (ConvertToString(Record, 0, S))
455 return Error("Invalid MODULE_CODE_DATALAYOUT record");
456 TheModule->setDataLayout(S);
459 case bitc::MODULE_CODE_ASM: { // ASM: [strlen, strchr x N]
461 if (ConvertToString(Record, 0, S))
462 return Error("Invalid MODULE_CODE_ASM record");
463 TheModule->setModuleInlineAsm(S);
466 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strlen, strchr x N]
468 if (ConvertToString(Record, 0, S))
469 return Error("Invalid MODULE_CODE_DEPLIB record");
470 TheModule->addLibrary(S);
473 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strlen, strchr x N]
475 if (ConvertToString(Record, 0, S))
476 return Error("Invalid MODULE_CODE_SECTIONNAME record");
477 SectionTable.push_back(S);
480 // GLOBALVAR: [type, isconst, initid,
481 // linkage, alignment, section, visibility, threadlocal]
482 case bitc::MODULE_CODE_GLOBALVAR: {
483 if (Record.size() < 6)
484 return Error("Invalid MODULE_CODE_GLOBALVAR record");
485 const Type *Ty = getTypeByID(Record[0]);
486 if (!isa<PointerType>(Ty))
487 return Error("Global not a pointer type!");
488 Ty = cast<PointerType>(Ty)->getElementType();
490 bool isConstant = Record[1];
491 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
492 unsigned Alignment = (1 << Record[4]) >> 1;
495 if (Record[5]-1 >= SectionTable.size())
496 return Error("Invalid section ID");
497 Section = SectionTable[Record[5]-1];
499 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
500 if (Record.size() >= 6) Visibility = GetDecodedVisibility(Record[6]);
501 bool isThreadLocal = false;
502 if (Record.size() >= 7) isThreadLocal = Record[7];
504 GlobalVariable *NewGV =
505 new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule);
506 NewGV->setAlignment(Alignment);
507 if (!Section.empty())
508 NewGV->setSection(Section);
509 NewGV->setVisibility(Visibility);
510 NewGV->setThreadLocal(isThreadLocal);
512 ValueList.push_back(NewGV);
514 // Remember which value to use for the global initializer.
515 if (unsigned InitID = Record[2])
516 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
519 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
521 case bitc::MODULE_CODE_FUNCTION: {
522 if (Record.size() < 7)
523 return Error("Invalid MODULE_CODE_FUNCTION record");
524 const Type *Ty = getTypeByID(Record[0]);
525 if (!isa<PointerType>(Ty))
526 return Error("Function not a pointer type!");
527 const FunctionType *FTy =
528 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
530 return Error("Function not a pointer to function type!");
532 Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
535 Func->setCallingConv(Record[1]);
536 Func->setLinkage(GetDecodedLinkage(Record[3]));
537 Func->setAlignment((1 << Record[4]) >> 1);
539 if (Record[5]-1 >= SectionTable.size())
540 return Error("Invalid section ID");
541 Func->setSection(SectionTable[Record[5]-1]);
543 Func->setVisibility(GetDecodedVisibility(Record[6]));
545 ValueList.push_back(Func);
546 // TODO: remember initializer/global pair for later substitution.
553 return Error("Premature end of bitstream");
557 bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length,
558 const std::string &ModuleID) {
562 return Error("Bitcode stream should be a multiple of 4 bytes in length");
564 BitstreamReader Stream(Buf, Buf+Length);
566 // Sniff for the signature.
567 if (Stream.Read(8) != 'B' ||
568 Stream.Read(8) != 'C' ||
569 Stream.Read(4) != 0x0 ||
570 Stream.Read(4) != 0xC ||
571 Stream.Read(4) != 0xE ||
572 Stream.Read(4) != 0xD)
573 return Error("Invalid bitcode signature");
575 // We expect a number of well-defined blocks, though we don't necessarily
576 // need to understand them all.
577 while (!Stream.AtEndOfStream()) {
578 unsigned Code = Stream.ReadCode();
580 if (Code != bitc::ENTER_SUBBLOCK)
581 return Error("Invalid record at top-level");
583 unsigned BlockID = Stream.ReadSubBlockID();
585 // We only know the MODULE subblock ID.
586 if (BlockID == bitc::MODULE_BLOCK_ID) {
587 if (ParseModule(Stream, ModuleID))
589 } else if (Stream.SkipBlock()) {
590 return Error("Malformed block record");