1 //===--- Bitcode/Writer/Writer.cpp - Bitcode Writer -----------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Bitcode writer implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/Bitcode/BitstreamWriter.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "ValueEnumerator.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/TypeSymbolTable.h"
22 #include "llvm/ValueSymbolTable.h"
23 #include "llvm/Support/MathExtras.h"
26 static const unsigned CurVersion = 0;
28 static void WriteStringRecord(unsigned Code, const std::string &Str,
29 unsigned AbbrevToUse, BitstreamWriter &Stream) {
30 SmallVector<unsigned, 64> Vals;
32 // Code: [strlen, strchar x N]
33 Vals.push_back(Str.size());
34 for (unsigned i = 0, e = Str.size(); i != e; ++i)
35 Vals.push_back(Str[i]);
37 // Emit the finished record.
38 Stream.EmitRecord(Code, Vals, AbbrevToUse);
42 /// WriteTypeTable - Write out the type table for a module.
43 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
44 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
46 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
47 SmallVector<uint64_t, 64> TypeVals;
49 // FIXME: Set up abbrevs now that we know the width of the type fields, etc.
51 // Emit an entry count so the reader can reserve space.
52 TypeVals.push_back(TypeList.size());
53 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
56 // Loop over all of the types, emitting each in turn.
57 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
58 const Type *T = TypeList[i].first;
62 switch (T->getTypeID()) {
63 case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
64 default: assert(0 && "Unknown type!");
65 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
66 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
67 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
68 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
69 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
70 case Type::IntegerTyID:
72 Code = bitc::TYPE_CODE_INTEGER;
73 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
75 case Type::PointerTyID:
76 // POINTER: [pointee type]
77 Code = bitc::TYPE_CODE_POINTER;
78 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
81 case Type::FunctionTyID: {
82 const FunctionType *FT = cast<FunctionType>(T);
83 // FUNCTION: [isvararg, #pararms, paramty x N]
84 Code = bitc::TYPE_CODE_FUNCTION;
85 TypeVals.push_back(FT->isVarArg());
86 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
87 // FIXME: PARAM ATTR ID!
88 TypeVals.push_back(FT->getNumParams());
89 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
90 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
93 case Type::StructTyID: {
94 const StructType *ST = cast<StructType>(T);
95 // STRUCT: [ispacked, #elts, eltty x N]
96 Code = bitc::TYPE_CODE_STRUCT;
97 TypeVals.push_back(ST->isPacked());
98 TypeVals.push_back(ST->getNumElements());
99 // Output all of the element types...
100 for (StructType::element_iterator I = ST->element_begin(),
101 E = ST->element_end(); I != E; ++I)
102 TypeVals.push_back(VE.getTypeID(*I));
105 case Type::ArrayTyID: {
106 const ArrayType *AT = cast<ArrayType>(T);
107 // ARRAY: [numelts, eltty]
108 Code = bitc::TYPE_CODE_ARRAY;
109 TypeVals.push_back(AT->getNumElements());
110 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
113 case Type::VectorTyID: {
114 const VectorType *VT = cast<VectorType>(T);
115 // VECTOR [numelts, eltty]
116 Code = bitc::TYPE_CODE_VECTOR;
117 TypeVals.push_back(VT->getNumElements());
118 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
123 // Emit the finished record.
124 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
131 static unsigned getEncodedLinkage(const GlobalValue *GV) {
132 switch (GV->getLinkage()) {
133 default: assert(0 && "Invalid linkage!");
134 case GlobalValue::ExternalLinkage: return 0;
135 case GlobalValue::WeakLinkage: return 1;
136 case GlobalValue::AppendingLinkage: return 2;
137 case GlobalValue::InternalLinkage: return 3;
138 case GlobalValue::LinkOnceLinkage: return 4;
139 case GlobalValue::DLLImportLinkage: return 5;
140 case GlobalValue::DLLExportLinkage: return 6;
141 case GlobalValue::ExternalWeakLinkage: return 7;
145 static unsigned getEncodedVisibility(const GlobalValue *GV) {
146 switch (GV->getVisibility()) {
147 default: assert(0 && "Invalid visibility!");
148 case GlobalValue::DefaultVisibility: return 0;
149 case GlobalValue::HiddenVisibility: return 1;
153 // Emit top-level description of module, including target triple, inline asm,
154 // descriptors for global variables, and function prototype info.
155 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
156 BitstreamWriter &Stream) {
157 // Emit the list of dependent libraries for the Module.
158 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
159 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
161 // Emit various pieces of data attached to a module.
162 if (!M->getTargetTriple().empty())
163 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
165 if (!M->getDataLayout().empty())
166 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
168 if (!M->getModuleInlineAsm().empty())
169 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
172 // Emit information about sections, computing how many there are. Also
173 // compute the maximum alignment value.
174 std::map<std::string, unsigned> SectionMap;
175 unsigned MaxAlignment = 0;
176 unsigned MaxGlobalType = 0;
177 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
179 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
180 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
182 if (!GV->hasSection()) continue;
183 // Give section names unique ID's.
184 unsigned &Entry = SectionMap[GV->getSection()];
185 if (Entry != 0) continue;
186 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
188 Entry = SectionMap.size();
190 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
191 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
192 if (!F->hasSection()) continue;
193 // Give section names unique ID's.
194 unsigned &Entry = SectionMap[F->getSection()];
195 if (Entry != 0) continue;
196 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
198 Entry = SectionMap.size();
201 // Emit abbrev for globals, now that we know # sections and max alignment.
202 unsigned SimpleGVarAbbrev = 0;
203 if (!M->global_empty()) {
204 // Add an abbrev for common globals with no visibility or thread localness.
205 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
206 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
207 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth,
208 Log2_32_Ceil(MaxGlobalType+1)));
209 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, 1)); // Constant.
210 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
211 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, 3)); // Linkage.
212 if (MaxAlignment == 0) // Alignment.
213 Abbv->Add(BitCodeAbbrevOp(0));
215 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
216 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth,
217 Log2_32_Ceil(MaxEncAlignment+1)));
219 if (SectionMap.empty()) // Section.
220 Abbv->Add(BitCodeAbbrevOp(0));
222 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth,
223 Log2_32_Ceil(SectionMap.size()+1)));
224 // Don't bother emitting vis + thread local.
225 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
228 // Emit the global variable information.
229 SmallVector<unsigned, 64> Vals;
230 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
232 unsigned AbbrevToUse = 0;
234 // GLOBALVAR: [type, isconst, initid,
235 // linkage, alignment, section, visibility, threadlocal]
236 Vals.push_back(VE.getTypeID(GV->getType()));
237 Vals.push_back(GV->isConstant());
238 Vals.push_back(GV->isDeclaration() ? 0 :
239 (VE.getValueID(GV->getInitializer()) + 1));
240 Vals.push_back(getEncodedLinkage(GV));
241 Vals.push_back(Log2_32(GV->getAlignment())+1);
242 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
243 if (GV->isThreadLocal() ||
244 GV->getVisibility() != GlobalValue::DefaultVisibility) {
245 Vals.push_back(getEncodedVisibility(GV));
246 Vals.push_back(GV->isThreadLocal());
248 AbbrevToUse = SimpleGVarAbbrev;
251 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
255 // Emit the function proto information.
256 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
257 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
259 Vals.push_back(VE.getTypeID(F->getType()));
260 Vals.push_back(F->getCallingConv());
261 Vals.push_back(F->isDeclaration());
262 Vals.push_back(getEncodedLinkage(F));
263 Vals.push_back(Log2_32(F->getAlignment())+1);
264 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
265 Vals.push_back(getEncodedVisibility(F));
267 unsigned AbbrevToUse = 0;
268 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
274 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
275 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
276 const ValueEnumerator &VE,
277 BitstreamWriter &Stream) {
278 if (TST.empty()) return;
280 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
282 // FIXME: Set up the abbrev, we know how many types there are!
283 // FIXME: We know if the type names can use 7-bit ascii.
285 SmallVector<unsigned, 64> NameVals;
287 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
289 unsigned AbbrevToUse = 0;
291 // TST_ENTRY: [typeid, namelen, namechar x N]
292 NameVals.push_back(VE.getTypeID(TI->second));
294 const std::string &Str = TI->first;
295 NameVals.push_back(Str.size());
296 for (unsigned i = 0, e = Str.size(); i != e; ++i)
297 NameVals.push_back(Str[i]);
299 // Emit the finished record.
300 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, AbbrevToUse);
307 // Emit names for globals/functions etc.
308 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
309 const ValueEnumerator &VE,
310 BitstreamWriter &Stream) {
311 if (VST.empty()) return;
312 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 3);
314 // FIXME: Set up the abbrev, we know how many values there are!
315 // FIXME: We know if the type names can use 7-bit ascii.
316 SmallVector<unsigned, 64> NameVals;
318 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
320 unsigned AbbrevToUse = 0;
322 // VST_ENTRY: [valueid, namelen, namechar x N]
323 NameVals.push_back(VE.getValueID(SI->getValue()));
325 NameVals.push_back(SI->getKeyLength());
326 for (const char *P = SI->getKeyData(),
327 *E = SI->getKeyData()+SI->getKeyLength(); P != E; ++P)
328 NameVals.push_back((unsigned char)*P);
330 // Emit the finished record.
331 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, AbbrevToUse);
337 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
338 const ValueEnumerator &VE,
339 BitstreamWriter &Stream) {
340 if (FirstVal == LastVal) return;
342 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 2);
344 // FIXME: Install and use abbrevs to reduce size.
346 SmallVector<uint64_t, 64> Record;
348 const ValueEnumerator::ValueList &Vals = VE.getValues();
349 const Type *LastTy = 0;
350 for (unsigned i = FirstVal; i != LastVal; ++i) {
351 const Value *V = Vals[i].first;
352 // If we need to switch types, do so now.
353 if (V->getType() != LastTy) {
354 LastTy = V->getType();
355 Record.push_back(VE.getTypeID(LastTy));
356 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record);
360 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
361 assert(0 && IA && "FIXME: Inline asm writing unimp!");
364 const Constant *C = cast<Constant>(V);
366 unsigned AbbrevToUse = 0;
367 if (C->isNullValue()) {
368 Code = bitc::CST_CODE_NULL;
369 } else if (isa<UndefValue>(C)) {
370 Code = bitc::CST_CODE_UNDEF;
371 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
372 if (IV->getBitWidth() <= 64) {
373 int64_t V = IV->getSExtValue();
375 Record.push_back(V << 1);
377 Record.push_back((-V << 1) | 1);
378 Code = bitc::CST_CODE_INTEGER;
379 } else { // Wide integers, > 64 bits in size.
380 // We have an arbitrary precision integer value to write whose
381 // bit width is > 64. However, in canonical unsigned integer
382 // format it is likely that the high bits are going to be zero.
383 // So, we only write the number of active words.
384 unsigned NWords = IV->getValue().getActiveWords();
385 const uint64_t *RawWords = IV->getValue().getRawData();
386 Record.push_back(NWords);
387 for (unsigned i = 0; i != NWords; ++i) {
388 int64_t V = RawWords[i];
390 Record.push_back(V << 1);
392 Record.push_back((-V << 1) | 1);
394 Code = bitc::CST_CODE_WIDE_INTEGER;
396 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
397 Code = bitc::CST_CODE_FLOAT;
398 if (CFP->getType() == Type::FloatTy) {
399 Record.push_back(FloatToBits((float)CFP->getValue()));
401 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
402 Record.push_back(DoubleToBits((double)CFP->getValue()));
404 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
405 isa<ConstantVector>(V)) {
406 Code = bitc::CST_CODE_AGGREGATE;
407 Record.push_back(C->getNumOperands());
408 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
409 Record.push_back(VE.getValueID(C->getOperand(i)));
410 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
411 Code = bitc::CST_CODE_CONSTEXPR;
412 // FIXME: optimize for binops, compares, etc.
413 Record.push_back(CE->getOpcode());
414 Record.push_back(CE->getNumOperands());
415 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
416 Record.push_back(VE.getValueID(C->getOperand(i)));
417 // Compares also pass their predicate.
419 Record.push_back((unsigned)CE->getPredicate());
421 assert(0 && "Unknown constant!");
423 Stream.EmitRecord(Code, Record, AbbrevToUse);
430 static void WriteModuleConstants(const ValueEnumerator &VE,
431 BitstreamWriter &Stream) {
432 const ValueEnumerator::ValueList &Vals = VE.getValues();
434 // Find the first constant to emit, which is the first non-globalvalue value.
435 // We know globalvalues have been emitted by WriteModuleInfo.
436 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
437 if (!isa<GlobalValue>(Vals[i].first)) {
438 WriteConstants(i, Vals.size(), VE, Stream);
444 /// WriteModule - Emit the specified module to the bitstream.
445 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
446 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
448 // Emit the version number if it is non-zero.
450 SmallVector<unsigned, 1> VersionVals;
451 VersionVals.push_back(CurVersion);
452 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, VersionVals);
455 // Analyze the module, enumerating globals, functions, etc.
456 ValueEnumerator VE(M);
458 // Emit information describing all of the types in the module.
459 WriteTypeTable(VE, Stream);
461 // Emit top-level description of module, including target triple, inline asm,
462 // descriptors for global variables, and function prototype info.
463 WriteModuleInfo(M, VE, Stream);
466 WriteModuleConstants(VE, Stream);
468 // Emit the type symbol table information.
469 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
471 // Emit names for globals/functions etc.
472 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
477 /// WriteBitcodeToFile - Write the specified module to the specified output
479 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
480 std::vector<unsigned char> Buffer;
481 BitstreamWriter Stream(Buffer);
483 Buffer.reserve(256*1024);
485 // Emit the file header.
486 Stream.Emit((unsigned)'B', 8);
487 Stream.Emit((unsigned)'C', 8);
494 WriteModule(M, Stream);
496 // Write the generated bitstream to "Out".
497 Out.write((char*)&Buffer.front(), Buffer.size());