read basic constants: null, undef, integers <= 64bits
[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/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/SmallString.h"
20 using namespace llvm;
21
22 /// ConvertToString - Convert a string from a record into an std::string, return
23 /// true on failure.
24 template<typename StrTy>
25 static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx,
26                             StrTy &Result) {
27   if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1)
28     return true;
29   
30   for (unsigned i = 0, e = Record[Idx]; i != e; ++i)
31     Result += (char)Record[Idx+i+1];
32   return false;
33 }
34
35 static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
36   switch (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;
46   }
47 }
48
49 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
50   switch (Val) {
51   default: // Map unknown visibilities to default.
52   case 0: return GlobalValue::DefaultVisibility;
53   case 1: return GlobalValue::HiddenVisibility;
54   }
55 }
56
57
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;
63   
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();
69 }
70
71
72 bool BitcodeReader::ParseTypeTable(BitstreamReader &Stream) {
73   if (Stream.EnterSubBlock())
74     return Error("Malformed block record");
75   
76   if (!TypeList.empty())
77     return Error("Multiple TYPE_BLOCKs found!");
78
79   SmallVector<uint64_t, 64> Record;
80   unsigned NumRecords = 0;
81
82   // Read all the records for this type table.
83   while (1) {
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();
89     }
90     
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");
96       continue;
97     }
98     
99     if (Code == bitc::DEFINE_ABBREV) {
100       Stream.ReadAbbrevRecord();
101       continue;
102     }
103     
104     // Read a record.
105     Record.clear();
106     const Type *ResultTy = 0;
107     switch (Stream.ReadRecord(Code, Record)) {
108     default:  // Default behavior: unknown type.
109       ResultTy = 0;
110       break;
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]);
117       continue;
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");
122       continue;
123       
124     case bitc::TYPE_CODE_VOID:      // VOID
125       ResultTy = Type::VoidTy;
126       break;
127     case bitc::TYPE_CODE_FLOAT:     // FLOAT
128       ResultTy = Type::FloatTy;
129       break;
130     case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
131       ResultTy = Type::DoubleTy;
132       break;
133     case bitc::TYPE_CODE_LABEL:     // LABEL
134       ResultTy = Type::LabelTy;
135       break;
136     case bitc::TYPE_CODE_OPAQUE:    // OPAQUE
137       ResultTy = 0;
138       break;
139     case bitc::TYPE_CODE_INTEGER:   // INTEGER: [width]
140       if (Record.size() < 1)
141         return Error("Invalid Integer type record");
142       
143       ResultTy = IntegerType::get(Record[0]);
144       break;
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));
149       break;
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));
157       
158       // FIXME: PARAM TYS.
159       ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys,
160                                    Record[0]);
161       break;
162     }
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]);
170       break;
171     }
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]);
176       break;
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]);
181       break;
182     }
183     
184     if (NumRecords == TypeList.size()) {
185       // If this is a new type slot, just append it.
186       TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get());
187       ++NumRecords;
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.
192       ++NumRecords;
193     } else {
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());
198      
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);
205       
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!");
211     }
212   }
213 }
214
215
216 bool BitcodeReader::ParseTypeSymbolTable(BitstreamReader &Stream) {
217   if (Stream.EnterSubBlock())
218     return Error("Malformed block record");
219   
220   SmallVector<uint64_t, 64> Record;
221   
222   // Read all the records for this type table.
223   std::string TypeName;
224   while (1) {
225     unsigned Code = Stream.ReadCode();
226     if (Code == bitc::END_BLOCK)
227       return Stream.ReadBlockEnd();
228     
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");
234       continue;
235     }
236     
237     if (Code == bitc::DEFINE_ABBREV) {
238       Stream.ReadAbbrevRecord();
239       continue;
240     }
241     
242     // Read a record.
243     Record.clear();
244     switch (Stream.ReadRecord(Code, Record)) {
245     default:  // Default behavior: unknown type.
246       break;
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");
253
254       TheModule->addTypeName(TypeName, TypeList[TypeID].get());
255       TypeName.clear();
256       break;
257     }
258   }
259 }
260
261 bool BitcodeReader::ParseValueSymbolTable(BitstreamReader &Stream) {
262   if (Stream.EnterSubBlock())
263     return Error("Malformed block record");
264
265   SmallVector<uint64_t, 64> Record;
266   
267   // Read all the records for this value table.
268   SmallString<128> ValueName;
269   while (1) {
270     unsigned Code = Stream.ReadCode();
271     if (Code == bitc::END_BLOCK)
272       return Stream.ReadBlockEnd();
273     
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");
279       continue;
280     }
281     
282     if (Code == bitc::DEFINE_ABBREV) {
283       Stream.ReadAbbrevRecord();
284       continue;
285     }
286     
287     // Read a record.
288     Record.clear();
289     switch (Stream.ReadRecord(Code, Record)) {
290     default:  // Default behavior: unknown type.
291       break;
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];
299       
300       V->setName(&ValueName[0], ValueName.size());
301       ValueName.clear();
302       break;
303     }
304   }
305 }
306
307 bool BitcodeReader::ParseConstants(BitstreamReader &Stream) {
308   if (Stream.EnterSubBlock())
309     return Error("Malformed block record");
310
311   SmallVector<uint64_t, 64> Record;
312   
313   // Read all the records for this value table.
314   const Type *CurTy = Type::Int32Ty;
315   while (1) {
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);
326           else
327             return Error("Global variable initializer is not a constant!");
328           GlobalInits.pop_back(); 
329         }
330       }
331       
332       return Stream.ReadBlockEnd();
333     }
334     
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");
340       continue;
341     }
342     
343     if (Code == bitc::DEFINE_ABBREV) {
344       Stream.ReadAbbrevRecord();
345       continue;
346     }
347     
348     // Read a record.
349     Record.clear();
350     Value *V = 0;
351     switch (Stream.ReadRecord(Code, Record)) {
352     default:  // Default behavior: unknown constant
353     case bitc::CST_CODE_UNDEF:     // UNDEF
354       V = UndefValue::get(CurTy);
355       break;
356     case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid]
357       if (Record.empty())
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]];
362       continue;
363     case bitc::CST_CODE_NULL:      // NULL
364       V = Constant::getNullValue(CurTy);
365       break;
366     case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
367       if (!isa<IntegerType>(CurTy))
368         return Error("Invalid type for CST_INTEGER");
369       if (Record[0] & 1)
370         V = ConstantInt::get(CurTy, -(Record[0]>>1));
371       else
372         V = ConstantInt::get(CurTy, Record[0]>>1);
373       break;
374     }
375     
376     ValueList.push_back(V);
377   }
378 }
379
380 bool BitcodeReader::ParseModule(BitstreamReader &Stream,
381                                 const std::string &ModuleID) {
382   // Reject multiple MODULE_BLOCK's in a single bitstream.
383   if (TheModule)
384     return Error("Multiple MODULE_BLOCKs in same stream");
385   
386   if (Stream.EnterSubBlock())
387     return Error("Malformed block record");
388
389   // Otherwise, create the module.
390   TheModule = new Module(ModuleID);
391   
392   SmallVector<uint64_t, 64> Record;
393   std::vector<std::string> SectionTable;
394
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();
402     }
403     
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");
409         break;
410       case bitc::TYPE_BLOCK_ID:
411         if (ParseTypeTable(Stream))
412           return true;
413         break;
414       case bitc::TYPE_SYMTAB_BLOCK_ID:
415         if (ParseTypeSymbolTable(Stream))
416           return true;
417         break;
418       case bitc::VALUE_SYMTAB_BLOCK_ID:
419         if (ParseValueSymbolTable(Stream))
420           return true;
421         break;
422       case bitc::CONSTANTS_BLOCK_ID:
423         if (ParseConstants(Stream))
424           return true;
425         break;
426       }
427       continue;
428     }
429     
430     if (Code == bitc::DEFINE_ABBREV) {
431       Stream.ReadAbbrevRecord();
432       continue;
433     }
434     
435     // Read a record.
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.
442       if (Record[0] != 0)
443         return Error("Unknown bitstream version!");
444       break;
445     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strlen, strchr x N]
446       std::string S;
447       if (ConvertToString(Record, 0, S))
448         return Error("Invalid MODULE_CODE_TRIPLE record");
449       TheModule->setTargetTriple(S);
450       break;
451     }
452     case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strlen, strchr x N]
453       std::string S;
454       if (ConvertToString(Record, 0, S))
455         return Error("Invalid MODULE_CODE_DATALAYOUT record");
456       TheModule->setDataLayout(S);
457       break;
458     }
459     case bitc::MODULE_CODE_ASM: {  // ASM: [strlen, strchr x N]
460       std::string S;
461       if (ConvertToString(Record, 0, S))
462         return Error("Invalid MODULE_CODE_ASM record");
463       TheModule->setModuleInlineAsm(S);
464       break;
465     }
466     case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strlen, strchr x N]
467       std::string S;
468       if (ConvertToString(Record, 0, S))
469         return Error("Invalid MODULE_CODE_DEPLIB record");
470       TheModule->addLibrary(S);
471       break;
472     }
473     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strlen, strchr x N]
474       std::string S;
475       if (ConvertToString(Record, 0, S))
476         return Error("Invalid MODULE_CODE_SECTIONNAME record");
477       SectionTable.push_back(S);
478       break;
479     }
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();
489       
490       bool isConstant = Record[1];
491       GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
492       unsigned Alignment = (1 << Record[4]) >> 1;
493       std::string Section;
494       if (Record[5]) {
495         if (Record[5]-1 >= SectionTable.size())
496           return Error("Invalid section ID");
497         Section = SectionTable[Record[5]-1];
498       }
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];
503
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);
511       
512       ValueList.push_back(NewGV);
513       
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));
517       break;
518     }
519     // FUNCTION:  [type, callingconv, isproto, linkage, alignment, section,
520     //             visibility]
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());
529       if (!FTy)
530         return Error("Function not a pointer to function type!");
531
532       Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
533                                     "", TheModule);
534
535       Func->setCallingConv(Record[1]);
536       Func->setLinkage(GetDecodedLinkage(Record[3]));
537       Func->setAlignment((1 << Record[4]) >> 1);
538       if (Record[5]) {
539         if (Record[5]-1 >= SectionTable.size())
540           return Error("Invalid section ID");
541         Func->setSection(SectionTable[Record[5]-1]);
542       }
543       Func->setVisibility(GetDecodedVisibility(Record[6]));
544       
545       ValueList.push_back(Func);
546       // TODO: remember initializer/global pair for later substitution.
547       break;
548     }
549     }
550     Record.clear();
551   }
552   
553   return Error("Premature end of bitstream");
554 }
555
556
557 bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length,
558                                  const std::string &ModuleID) {
559   TheModule = 0;
560   
561   if (Length & 3)
562     return Error("Bitcode stream should be a multiple of 4 bytes in length");
563   
564   BitstreamReader Stream(Buf, Buf+Length);
565   
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");
574   
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();
579     
580     if (Code != bitc::ENTER_SUBBLOCK)
581       return Error("Invalid record at top-level");
582     
583     unsigned BlockID = Stream.ReadSubBlockID();
584     
585     // We only know the MODULE subblock ID.
586     if (BlockID == bitc::MODULE_BLOCK_ID) {
587       if (ParseModule(Stream, ModuleID))
588         return true;
589     } else if (Stream.SkipBlock()) {
590       return Error("Malformed block record");
591     }
592   }
593   
594   return false;
595 }