blob: f464e33f6a03b1b08ac738d0d2b83919bea9b9d3 [file] [log] [blame]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001//===- 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
Chris Lattner36d5e7d2007-04-23 16:04:05 +000096 if (Code == bitc::DEFINE_ABBREV) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +000097 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
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000233 if (Code == bitc::DEFINE_ABBREV) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000234 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
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000296 if (Code == bitc::DEFINE_ABBREV) {
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000297 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: {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000348 if (Record.size() < 6)
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000349 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 }
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000364 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
365 if (Record.size() >= 6) Visibility = GetDecodedVisibility(Record[6]);
366 bool isThreadLocal = false;
367 if (Record.size() >= 7) isThreadLocal = Record[7];
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000368
369 GlobalVariable *NewGV =
370 new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule);
371 NewGV->setAlignment(Alignment);
372 if (!Section.empty())
373 NewGV->setSection(Section);
374 NewGV->setVisibility(Visibility);
375 NewGV->setThreadLocal(isThreadLocal);
376
377 // TODO: Add to value table.
378 // TODO: remember initializer/global pair for later substitution.
379 break;
380 }
381 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
382 // visibility]
383 case bitc::MODULE_CODE_FUNCTION: {
384 if (Record.size() < 7)
385 return Error("Invalid MODULE_CODE_FUNCTION record");
386 const Type *Ty = getTypeByID(Record[0]);
387 if (!isa<PointerType>(Ty))
388 return Error("Function not a pointer type!");
389 const FunctionType *FTy =
390 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
391 if (!FTy)
392 return Error("Function not a pointer to function type!");
393
394 Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
395 "", TheModule);
396
397 Func->setCallingConv(Record[1]);
398 Func->setLinkage(GetDecodedLinkage(Record[3]));
399 Func->setAlignment((1 << Record[4]) >> 1);
400 if (Record[5]) {
401 if (Record[5]-1 >= SectionTable.size())
402 return Error("Invalid section ID");
403 Func->setSection(SectionTable[Record[5]-1]);
404 }
405 Func->setVisibility(GetDecodedVisibility(Record[6]));
406
407 // TODO: Add to value table.
408 // TODO: remember initializer/global pair for later substitution.
409 break;
410 }
411 }
412 Record.clear();
413 }
414
415 return Error("Premature end of bitstream");
416}
417
418
419bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length,
420 const std::string &ModuleID) {
421 TheModule = 0;
422
423 if (Length & 3)
424 return Error("Bitcode stream should be a multiple of 4 bytes in length");
425
426 BitstreamReader Stream(Buf, Buf+Length);
427
428 // Sniff for the signature.
429 if (Stream.Read(8) != 'B' ||
430 Stream.Read(8) != 'C' ||
431 Stream.Read(4) != 0x0 ||
432 Stream.Read(4) != 0xC ||
433 Stream.Read(4) != 0xE ||
434 Stream.Read(4) != 0xD)
435 return Error("Invalid bitcode signature");
436
437 // We expect a number of well-defined blocks, though we don't necessarily
438 // need to understand them all.
439 while (!Stream.AtEndOfStream()) {
440 unsigned Code = Stream.ReadCode();
441
442 if (Code != bitc::ENTER_SUBBLOCK)
443 return Error("Invalid record at top-level");
444
445 unsigned BlockID = Stream.ReadSubBlockID();
446
447 // We only know the MODULE subblock ID.
448 if (BlockID == bitc::MODULE_BLOCK_ID) {
449 if (ParseModule(Stream, ModuleID))
450 return true;
451 } else if (Stream.SkipBlock()) {
452 return Error("Malformed block record");
453 }
454 }
455
456 return false;
457}