track global inits
[oota-llvm.git] / lib / Bitcode / Reader / BitcodeReader.cpp
1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header defines the BitcodeReader class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "BitcodeReader.h"
15 #include "llvm/Bitcode/BitstreamReader.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/ADT/SmallString.h"
19 using namespace llvm;
20
21 /// ConvertToString - Convert a string from a record into an std::string, return
22 /// true on failure.
23 template<typename StrTy>
24 static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx,
25                             StrTy &Result) {
26   if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1)
27     return true;
28   
29   for (unsigned i = 0, e = Record[Idx]; i != e; ++i)
30     Result += (char)Record[Idx+i+1];
31   return false;
32 }
33
34 static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
35   switch (Val) {
36   default: // Map unknown/new linkages to external
37   case 0: return GlobalValue::ExternalLinkage;
38   case 1: return GlobalValue::WeakLinkage;
39   case 2: return GlobalValue::AppendingLinkage;
40   case 3: return GlobalValue::InternalLinkage;
41   case 4: return GlobalValue::LinkOnceLinkage;
42   case 5: return GlobalValue::DLLImportLinkage;
43   case 6: return GlobalValue::DLLExportLinkage;
44   case 7: return GlobalValue::ExternalWeakLinkage;
45   }
46 }
47
48 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
49   switch (Val) {
50   default: // Map unknown visibilities to default.
51   case 0: return GlobalValue::DefaultVisibility;
52   case 1: return GlobalValue::HiddenVisibility;
53   }
54 }
55
56
57 const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
58   // If the TypeID is in range, return it.
59   if (ID < TypeList.size())
60     return TypeList[ID].get();
61   if (!isTypeTable) return 0;
62   
63   // The type table allows forward references.  Push as many Opaque types as
64   // needed to get up to ID.
65   while (TypeList.size() <= ID)
66     TypeList.push_back(OpaqueType::get());
67   return TypeList.back().get();
68 }
69
70
71 bool BitcodeReader::ParseTypeTable(BitstreamReader &Stream) {
72   if (Stream.EnterSubBlock())
73     return Error("Malformed block record");
74   
75   if (!TypeList.empty())
76     return Error("Multiple TYPE_BLOCKs found!");
77
78   SmallVector<uint64_t, 64> Record;
79   unsigned NumRecords = 0;
80
81   // Read all the records for this type table.
82   while (1) {
83     unsigned Code = Stream.ReadCode();
84     if (Code == bitc::END_BLOCK) {
85       if (NumRecords != TypeList.size())
86         return Error("Invalid type forward reference in TYPE_BLOCK");
87       return Stream.ReadBlockEnd();
88     }
89     
90     if (Code == bitc::ENTER_SUBBLOCK) {
91       // No known subblocks, always skip them.
92       Stream.ReadSubBlockID();
93       if (Stream.SkipBlock())
94         return Error("Malformed block record");
95       continue;
96     }
97     
98     if (Code == bitc::DEFINE_ABBREV) {
99       Stream.ReadAbbrevRecord();
100       continue;
101     }
102     
103     // Read a record.
104     Record.clear();
105     const Type *ResultTy = 0;
106     switch (Stream.ReadRecord(Code, Record)) {
107     default:  // Default behavior: unknown type.
108       ResultTy = 0;
109       break;
110     case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
111       // TYPE_CODE_NUMENTRY contains a count of the number of types in the
112       // type list.  This allows us to reserve space.
113       if (Record.size() < 1)
114         return Error("Invalid TYPE_CODE_NUMENTRY record");
115       TypeList.reserve(Record[0]);
116       continue;
117     case bitc::TYPE_CODE_META:      // TYPE_CODE_META: [metacode]...
118       // No metadata supported yet.
119       if (Record.size() < 1)
120         return Error("Invalid TYPE_CODE_META record");
121       continue;
122       
123     case bitc::TYPE_CODE_VOID:      // VOID
124       ResultTy = Type::VoidTy;
125       break;
126     case bitc::TYPE_CODE_FLOAT:     // FLOAT
127       ResultTy = Type::FloatTy;
128       break;
129     case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
130       ResultTy = Type::DoubleTy;
131       break;
132     case bitc::TYPE_CODE_LABEL:     // LABEL
133       ResultTy = Type::LabelTy;
134       break;
135     case bitc::TYPE_CODE_OPAQUE:    // OPAQUE
136       ResultTy = 0;
137       break;
138     case bitc::TYPE_CODE_INTEGER:   // INTEGER: [width]
139       if (Record.size() < 1)
140         return Error("Invalid Integer type record");
141       
142       ResultTy = IntegerType::get(Record[0]);
143       break;
144     case bitc::TYPE_CODE_POINTER:   // POINTER: [pointee type]
145       if (Record.size() < 1)
146         return Error("Invalid POINTER type record");
147       ResultTy = PointerType::get(getTypeByID(Record[0], true));
148       break;
149     case bitc::TYPE_CODE_FUNCTION: {
150       // FUNCTION: [vararg, retty, #pararms, paramty N]
151       if (Record.size() < 3 || Record.size() < Record[2]+3)
152         return Error("Invalid FUNCTION type record");
153       std::vector<const Type*> ArgTys;
154       for (unsigned i = 0, e = Record[2]; i != e; ++i)
155         ArgTys.push_back(getTypeByID(Record[3+i], true));
156       
157       // FIXME: PARAM TYS.
158       ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys,
159                                    Record[0]);
160       break;
161     }
162     case bitc::TYPE_CODE_STRUCT: {  // STRUCT: [ispacked, #elts, eltty x N]
163       if (Record.size() < 2 || Record.size() < Record[1]+2)
164         return Error("Invalid STRUCT type record");
165       std::vector<const Type*> EltTys;
166       for (unsigned i = 0, e = Record[1]; i != e; ++i)
167         EltTys.push_back(getTypeByID(Record[2+i], true));
168       ResultTy = StructType::get(EltTys, Record[0]);
169       break;
170     }
171     case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
172       if (Record.size() < 2)
173         return Error("Invalid ARRAY type record");
174       ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]);
175       break;
176     case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty]
177       if (Record.size() < 2)
178         return Error("Invalid VECTOR type record");
179       ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]);
180       break;
181     }
182     
183     if (NumRecords == TypeList.size()) {
184       // If this is a new type slot, just append it.
185       TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get());
186       ++NumRecords;
187     } else if (ResultTy == 0) {
188       // Otherwise, this was forward referenced, so an opaque type was created,
189       // but the result type is actually just an opaque.  Leave the one we
190       // created previously.
191       ++NumRecords;
192     } else {
193       // Otherwise, this was forward referenced, so an opaque type was created.
194       // Resolve the opaque type to the real type now.
195       assert(NumRecords < TypeList.size() && "Typelist imbalance");
196       const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get());
197      
198       // Don't directly push the new type on the Tab. Instead we want to replace
199       // the opaque type we previously inserted with the new concrete value. The
200       // refinement from the abstract (opaque) type to the new type causes all
201       // uses of the abstract type to use the concrete type (NewTy). This will
202       // also cause the opaque type to be deleted.
203       const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy);
204       
205       // This should have replaced the old opaque type with the new type in the
206       // value table... or with a preexisting type that was already in the system.
207       // Let's just make sure it did.
208       assert(TypeList[NumRecords-1].get() != OldTy &&
209              "refineAbstractType didn't work!");
210     }
211   }
212 }
213
214
215 bool BitcodeReader::ParseTypeSymbolTable(BitstreamReader &Stream) {
216   if (Stream.EnterSubBlock())
217     return Error("Malformed block record");
218   
219   SmallVector<uint64_t, 64> Record;
220   
221   // Read all the records for this type table.
222   std::string TypeName;
223   while (1) {
224     unsigned Code = Stream.ReadCode();
225     if (Code == bitc::END_BLOCK)
226       return Stream.ReadBlockEnd();
227     
228     if (Code == bitc::ENTER_SUBBLOCK) {
229       // No known subblocks, always skip them.
230       Stream.ReadSubBlockID();
231       if (Stream.SkipBlock())
232         return Error("Malformed block record");
233       continue;
234     }
235     
236     if (Code == bitc::DEFINE_ABBREV) {
237       Stream.ReadAbbrevRecord();
238       continue;
239     }
240     
241     // Read a record.
242     Record.clear();
243     switch (Stream.ReadRecord(Code, Record)) {
244     default:  // Default behavior: unknown type.
245       break;
246     case bitc::TST_CODE_ENTRY:    // TST_ENTRY: [typeid, namelen, namechar x N]
247       if (ConvertToString(Record, 1, TypeName))
248         return Error("Invalid TST_ENTRY record");
249       unsigned TypeID = Record[0];
250       if (TypeID >= TypeList.size())
251         return Error("Invalid Type ID in TST_ENTRY record");
252
253       TheModule->addTypeName(TypeName, TypeList[TypeID].get());
254       TypeName.clear();
255       break;
256     }
257   }
258 }
259
260 bool BitcodeReader::ParseValueSymbolTable(BitstreamReader &Stream) {
261   if (Stream.EnterSubBlock())
262     return Error("Malformed block record");
263
264   SmallVector<uint64_t, 64> Record;
265   
266   // Read all the records for this value table.
267   SmallString<128> ValueName;
268   while (1) {
269     unsigned Code = Stream.ReadCode();
270     if (Code == bitc::END_BLOCK)
271       return Stream.ReadBlockEnd();
272     
273     if (Code == bitc::ENTER_SUBBLOCK) {
274       // No known subblocks, always skip them.
275       Stream.ReadSubBlockID();
276       if (Stream.SkipBlock())
277         return Error("Malformed block record");
278       continue;
279     }
280     
281     if (Code == bitc::DEFINE_ABBREV) {
282       Stream.ReadAbbrevRecord();
283       continue;
284     }
285     
286     // Read a record.
287     Record.clear();
288     switch (Stream.ReadRecord(Code, Record)) {
289     default:  // Default behavior: unknown type.
290       break;
291     case bitc::TST_CODE_ENTRY:    // VST_ENTRY: [valueid, namelen, namechar x N]
292       if (ConvertToString(Record, 1, ValueName))
293         return Error("Invalid TST_ENTRY record");
294       unsigned ValueID = Record[0];
295       if (ValueID >= ValueList.size())
296         return Error("Invalid Value ID in VST_ENTRY record");
297       Value *V = ValueList[ValueID];
298       
299       V->setName(&ValueName[0], ValueName.size());
300       ValueName.clear();
301       break;
302     }
303   }
304 }
305
306
307 bool BitcodeReader::ParseModule(BitstreamReader &Stream,
308                                 const std::string &ModuleID) {
309   // Reject multiple MODULE_BLOCK's in a single bitstream.
310   if (TheModule)
311     return Error("Multiple MODULE_BLOCKs in same stream");
312   
313   if (Stream.EnterSubBlock())
314     return Error("Malformed block record");
315
316   // Otherwise, create the module.
317   TheModule = new Module(ModuleID);
318   
319   SmallVector<uint64_t, 64> Record;
320   std::vector<std::string> SectionTable;
321
322   // Read all the records for this module.
323   while (!Stream.AtEndOfStream()) {
324     unsigned Code = Stream.ReadCode();
325     if (Code == bitc::END_BLOCK)
326       return Stream.ReadBlockEnd();
327     
328     if (Code == bitc::ENTER_SUBBLOCK) {
329       switch (Stream.ReadSubBlockID()) {
330       default:  // Skip unknown content.
331         if (Stream.SkipBlock())
332           return Error("Malformed block record");
333         break;
334       case bitc::TYPE_BLOCK_ID:
335         if (ParseTypeTable(Stream))
336           return true;
337         break;
338       case bitc::TYPE_SYMTAB_BLOCK_ID:
339         if (ParseTypeSymbolTable(Stream))
340           return true;
341         break;
342       case bitc::VALUE_SYMTAB_BLOCK_ID:
343         if (ParseValueSymbolTable(Stream))
344           return true;
345         break;
346       }
347       continue;
348     }
349     
350     if (Code == bitc::DEFINE_ABBREV) {
351       Stream.ReadAbbrevRecord();
352       continue;
353     }
354     
355     // Read a record.
356     switch (Stream.ReadRecord(Code, Record)) {
357     default: break;  // Default behavior, ignore unknown content.
358     case bitc::MODULE_CODE_VERSION:  // VERSION: [version#]
359       if (Record.size() < 1)
360         return Error("Malformed MODULE_CODE_VERSION");
361       if (!GlobalInits.empty())
362         return Error("Malformed global initializer set");
363       // Only version #0 is supported so far.
364       if (Record[0] != 0)
365         return Error("Unknown bitstream version!");
366       break;
367     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strlen, strchr x N]
368       std::string S;
369       if (ConvertToString(Record, 0, S))
370         return Error("Invalid MODULE_CODE_TRIPLE record");
371       TheModule->setTargetTriple(S);
372       break;
373     }
374     case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strlen, strchr x N]
375       std::string S;
376       if (ConvertToString(Record, 0, S))
377         return Error("Invalid MODULE_CODE_DATALAYOUT record");
378       TheModule->setDataLayout(S);
379       break;
380     }
381     case bitc::MODULE_CODE_ASM: {  // ASM: [strlen, strchr x N]
382       std::string S;
383       if (ConvertToString(Record, 0, S))
384         return Error("Invalid MODULE_CODE_ASM record");
385       TheModule->setModuleInlineAsm(S);
386       break;
387     }
388     case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strlen, strchr x N]
389       std::string S;
390       if (ConvertToString(Record, 0, S))
391         return Error("Invalid MODULE_CODE_DEPLIB record");
392       TheModule->addLibrary(S);
393       break;
394     }
395     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strlen, strchr x N]
396       std::string S;
397       if (ConvertToString(Record, 0, S))
398         return Error("Invalid MODULE_CODE_SECTIONNAME record");
399       SectionTable.push_back(S);
400       break;
401     }
402     // GLOBALVAR: [type, isconst, initid, 
403     //             linkage, alignment, section, visibility, threadlocal]
404     case bitc::MODULE_CODE_GLOBALVAR: {
405       if (Record.size() < 6)
406         return Error("Invalid MODULE_CODE_GLOBALVAR record");
407       const Type *Ty = getTypeByID(Record[0]);
408       if (!isa<PointerType>(Ty))
409         return Error("Global not a pointer type!");
410       Ty = cast<PointerType>(Ty)->getElementType();
411       
412       bool isConstant = Record[1];
413       GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
414       unsigned Alignment = (1 << Record[4]) >> 1;
415       std::string Section;
416       if (Record[5]) {
417         if (Record[5]-1 >= SectionTable.size())
418           return Error("Invalid section ID");
419         Section = SectionTable[Record[5]-1];
420       }
421       GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
422       if (Record.size() >= 6) Visibility = GetDecodedVisibility(Record[6]);
423       bool isThreadLocal = false;
424       if (Record.size() >= 7) isThreadLocal = Record[7];
425
426       GlobalVariable *NewGV =
427         new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule);
428       NewGV->setAlignment(Alignment);
429       if (!Section.empty())
430         NewGV->setSection(Section);
431       NewGV->setVisibility(Visibility);
432       NewGV->setThreadLocal(isThreadLocal);
433       
434       ValueList.push_back(NewGV);
435       
436       // Remember which value to use for the global initializer.
437       if (unsigned InitID = Record[2])
438         GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
439       break;
440     }
441     // FUNCTION:  [type, callingconv, isproto, linkage, alignment, section,
442     //             visibility]
443     case bitc::MODULE_CODE_FUNCTION: {
444       if (Record.size() < 7)
445         return Error("Invalid MODULE_CODE_FUNCTION record");
446       const Type *Ty = getTypeByID(Record[0]);
447       if (!isa<PointerType>(Ty))
448         return Error("Function not a pointer type!");
449       const FunctionType *FTy =
450         dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
451       if (!FTy)
452         return Error("Function not a pointer to function type!");
453
454       Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
455                                     "", TheModule);
456
457       Func->setCallingConv(Record[1]);
458       Func->setLinkage(GetDecodedLinkage(Record[3]));
459       Func->setAlignment((1 << Record[4]) >> 1);
460       if (Record[5]) {
461         if (Record[5]-1 >= SectionTable.size())
462           return Error("Invalid section ID");
463         Func->setSection(SectionTable[Record[5]-1]);
464       }
465       Func->setVisibility(GetDecodedVisibility(Record[6]));
466       
467       ValueList.push_back(Func);
468       // TODO: remember initializer/global pair for later substitution.
469       break;
470     }
471     }
472     Record.clear();
473   }
474   
475   return Error("Premature end of bitstream");
476 }
477
478
479 bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length,
480                                  const std::string &ModuleID) {
481   TheModule = 0;
482   
483   if (Length & 3)
484     return Error("Bitcode stream should be a multiple of 4 bytes in length");
485   
486   BitstreamReader Stream(Buf, Buf+Length);
487   
488   // Sniff for the signature.
489   if (Stream.Read(8) != 'B' ||
490       Stream.Read(8) != 'C' ||
491       Stream.Read(4) != 0x0 ||
492       Stream.Read(4) != 0xC ||
493       Stream.Read(4) != 0xE ||
494       Stream.Read(4) != 0xD)
495     return Error("Invalid bitcode signature");
496   
497   // We expect a number of well-defined blocks, though we don't necessarily
498   // need to understand them all.
499   while (!Stream.AtEndOfStream()) {
500     unsigned Code = Stream.ReadCode();
501     
502     if (Code != bitc::ENTER_SUBBLOCK)
503       return Error("Invalid record at top-level");
504     
505     unsigned BlockID = Stream.ReadSubBlockID();
506     
507     // We only know the MODULE subblock ID.
508     if (BlockID == bitc::MODULE_BLOCK_ID) {
509       if (ParseModule(Stream, ModuleID))
510         return true;
511     } else if (Stream.SkipBlock()) {
512       return Error("Malformed block record");
513     }
514   }
515   
516   return false;
517 }