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