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