32ae8d5e1fba212ad0bb8ffde358f1211cc8a5b6
[oota-llvm.git] / lib / Bitcode / Writer / BitcodeWriter.cpp
1 //===--- Bitcode/Writer/Writer.cpp - Bitcode Writer -----------------------===//
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 // Bitcode writer implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
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/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/TypeSymbolTable.h"
21 #include "llvm/ValueSymbolTable.h"
22 #include "llvm/Support/MathExtras.h"
23 using namespace llvm;
24
25 static const unsigned CurVersion = 0;
26
27 static void WriteStringRecord(unsigned Code, const std::string &Str, 
28                               unsigned AbbrevToUse, BitstreamWriter &Stream) {
29   SmallVector<unsigned, 64> Vals;
30   
31   // Code: [strlen, strchar x N]
32   Vals.push_back(Str.size());
33   for (unsigned i = 0, e = Str.size(); i != e; ++i)
34     Vals.push_back(Str[i]);
35     
36   // Emit the finished record.
37   Stream.EmitRecord(Code, Vals, AbbrevToUse);
38 }
39
40
41 /// WriteTypeTable - Write out the type table for a module.
42 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
43   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
44   
45   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
46   SmallVector<uint64_t, 64> TypeVals;
47   
48   // FIXME: Set up abbrevs now that we know the width of the type fields, etc.
49   
50   // Emit an entry count so the reader can reserve space.
51   TypeVals.push_back(TypeList.size());
52   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
53   TypeVals.clear();
54   
55   // Loop over all of the types, emitting each in turn.
56   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
57     const Type *T = TypeList[i].first;
58     int AbbrevToUse = 0;
59     unsigned Code = 0;
60     
61     switch (T->getTypeID()) {
62     case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
63     default: assert(0 && "Unknown type!");
64     case Type::VoidTyID:   Code = bitc::TYPE_CODE_VOID;   break;
65     case Type::FloatTyID:  Code = bitc::TYPE_CODE_FLOAT;  break;
66     case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
67     case Type::LabelTyID:  Code = bitc::TYPE_CODE_LABEL;  break;
68     case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
69     case Type::IntegerTyID:
70       // INTEGER: [width]
71       Code = bitc::TYPE_CODE_INTEGER;
72       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
73       break;
74     case Type::PointerTyID:
75       // POINTER: [pointee type]
76       Code = bitc::TYPE_CODE_POINTER;
77       TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
78       break;
79
80     case Type::FunctionTyID: {
81       const FunctionType *FT = cast<FunctionType>(T);
82       // FUNCTION: [isvararg, #pararms, paramty x N]
83       Code = bitc::TYPE_CODE_FUNCTION;
84       TypeVals.push_back(FT->isVarArg());
85       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
86       // FIXME: PARAM ATTR ID!
87       TypeVals.push_back(FT->getNumParams());
88       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
89         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
90       break;
91     }
92     case Type::StructTyID: {
93       const StructType *ST = cast<StructType>(T);
94       // STRUCT: [ispacked, #elts, eltty x N]
95       Code = bitc::TYPE_CODE_STRUCT;
96       TypeVals.push_back(ST->isPacked());
97       TypeVals.push_back(ST->getNumElements());
98       // Output all of the element types...
99       for (StructType::element_iterator I = ST->element_begin(),
100            E = ST->element_end(); I != E; ++I)
101         TypeVals.push_back(VE.getTypeID(*I));
102       break;
103     }
104     case Type::ArrayTyID: {
105       const ArrayType *AT = cast<ArrayType>(T);
106       // ARRAY: [numelts, eltty]
107       Code = bitc::TYPE_CODE_ARRAY;
108       TypeVals.push_back(AT->getNumElements());
109       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
110       break;
111     }
112     case Type::VectorTyID: {
113       const VectorType *VT = cast<VectorType>(T);
114       // VECTOR [numelts, eltty]
115       Code = bitc::TYPE_CODE_VECTOR;
116       TypeVals.push_back(VT->getNumElements());
117       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
118       break;
119     }
120     }
121
122     // Emit the finished record.
123     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
124     TypeVals.clear();
125   }
126   
127   Stream.ExitBlock();
128 }
129
130 static unsigned getEncodedLinkage(const GlobalValue *GV) {
131   switch (GV->getLinkage()) {
132   default: assert(0 && "Invalid linkage!");
133   case GlobalValue::ExternalLinkage:     return 0;
134   case GlobalValue::WeakLinkage:         return 1;
135   case GlobalValue::AppendingLinkage:    return 2;
136   case GlobalValue::InternalLinkage:     return 3;
137   case GlobalValue::LinkOnceLinkage:     return 4;
138   case GlobalValue::DLLImportLinkage:    return 5;
139   case GlobalValue::DLLExportLinkage:    return 6;
140   case GlobalValue::ExternalWeakLinkage: return 7;
141   }
142 }
143
144 static unsigned getEncodedVisibility(const GlobalValue *GV) {
145   switch (GV->getVisibility()) {
146   default: assert(0 && "Invalid visibility!");
147   case GlobalValue::DefaultVisibility: return 0;
148   case GlobalValue::HiddenVisibility:  return 1;
149   }
150 }
151
152 // Emit top-level description of module, including target triple, inline asm,
153 // descriptors for global variables, and function prototype info.
154 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
155                             BitstreamWriter &Stream) {
156   // Emit the list of dependent libraries for the Module.
157   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
158     WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
159
160   // Emit various pieces of data attached to a module.
161   if (!M->getTargetTriple().empty())
162     WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
163                       0/*TODO*/, Stream);
164   if (!M->getDataLayout().empty())
165     WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
166                       0/*TODO*/, Stream);
167   if (!M->getModuleInlineAsm().empty())
168     WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
169                       0/*TODO*/, Stream);
170
171   // Emit information about sections, computing how many there are.  Also
172   // compute the maximum alignment value.
173   std::map<std::string, unsigned> SectionMap;
174   unsigned MaxAlignment = 0;
175   unsigned MaxGlobalType = 0;
176   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
177        GV != E; ++GV) {
178     MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
179     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
180     
181     if (!GV->hasSection()) continue;
182     // Give section names unique ID's.
183     unsigned &Entry = SectionMap[GV->getSection()];
184     if (Entry != 0) continue;
185     WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
186                       0/*TODO*/, Stream);
187     Entry = SectionMap.size();
188   }
189   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
190     MaxAlignment = std::max(MaxAlignment, F->getAlignment());
191     if (!F->hasSection()) continue;
192     // Give section names unique ID's.
193     unsigned &Entry = SectionMap[F->getSection()];
194     if (Entry != 0) continue;
195     WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
196                       0/*TODO*/, Stream);
197     Entry = SectionMap.size();
198   }
199   
200   // Emit abbrev for globals, now that we know # sections and max alignment.
201   unsigned SimpleGVarAbbrev = 0;
202   if (!M->global_empty()) { 
203     // Add an abbrev for common globals with no visibility or thread localness.
204     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
205     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
206     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth,
207                               Log2_32_Ceil(MaxGlobalType+1)));
208     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, 1)); // Constant.
209     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
210     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, 3)); // Linkage.
211     if (MaxAlignment == 0)                                     // Alignment.
212       Abbv->Add(BitCodeAbbrevOp(0));
213     else {
214       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
215       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth,
216                                Log2_32_Ceil(MaxEncAlignment+1)));
217     }
218     if (SectionMap.empty())                                    // Section.
219       Abbv->Add(BitCodeAbbrevOp(0));
220     else
221       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth,
222                                Log2_32_Ceil(SectionMap.size())));
223     // Don't bother emitting vis + thread local.
224     SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
225   }
226   
227   // Emit the global variable information.
228   SmallVector<unsigned, 64> Vals;
229   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
230        GV != E; ++GV) {
231     unsigned AbbrevToUse = 0;
232
233     // GLOBALVAR: [type, isconst, initid, 
234     //             linkage, alignment, section, visibility, threadlocal]
235     Vals.push_back(VE.getTypeID(GV->getType()));
236     Vals.push_back(GV->isConstant());
237     Vals.push_back(GV->isDeclaration() ? 0 :
238                    (VE.getValueID(GV->getInitializer()) + 1));
239     Vals.push_back(getEncodedLinkage(GV));
240     Vals.push_back(Log2_32(GV->getAlignment())+1);
241     Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
242     if (GV->isThreadLocal() || 
243         GV->getVisibility() != GlobalValue::DefaultVisibility) {
244       Vals.push_back(getEncodedVisibility(GV));
245       Vals.push_back(GV->isThreadLocal());
246     } else {
247       AbbrevToUse = SimpleGVarAbbrev;
248     }
249     
250     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
251     Vals.clear();
252   }
253
254   // Emit the function proto information.
255   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
256     // FUNCTION:  [type, callingconv, isproto, linkage, alignment, section,
257     //             visibility]
258     Vals.push_back(VE.getTypeID(F->getType()));
259     Vals.push_back(F->getCallingConv());
260     Vals.push_back(F->isDeclaration());
261     Vals.push_back(getEncodedLinkage(F));
262     Vals.push_back(Log2_32(F->getAlignment())+1);
263     Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
264     Vals.push_back(getEncodedVisibility(F));
265     
266     unsigned AbbrevToUse = 0;
267     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
268     Vals.clear();
269   }
270 }
271
272
273 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
274 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
275                                  const ValueEnumerator &VE,
276                                  BitstreamWriter &Stream) {
277   if (TST.empty()) return;
278   
279   Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
280   
281   // FIXME: Set up the abbrev, we know how many types there are!
282   // FIXME: We know if the type names can use 7-bit ascii.
283   
284   SmallVector<unsigned, 64> NameVals;
285   
286   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
287        TI != TE; ++TI) {
288     unsigned AbbrevToUse = 0;
289     
290     // TST_ENTRY: [typeid, namelen, namechar x N]
291     NameVals.push_back(VE.getTypeID(TI->second));
292     
293     const std::string &Str = TI->first;
294     NameVals.push_back(Str.size());
295     for (unsigned i = 0, e = Str.size(); i != e; ++i)
296       NameVals.push_back(Str[i]);
297     
298     // Emit the finished record.
299     Stream.EmitRecord(bitc::TST_ENTRY_CODE, NameVals, AbbrevToUse);
300     NameVals.clear();
301   }
302   
303   Stream.ExitBlock();
304 }
305
306 // Emit names for globals/functions etc.
307 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
308                                   const ValueEnumerator &VE,
309                                   BitstreamWriter &Stream) {
310   if (VST.empty()) return;
311   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 3);
312
313   // FIXME: Set up the abbrev, we know how many values there are!
314   // FIXME: We know if the type names can use 7-bit ascii.
315   SmallVector<unsigned, 64> NameVals;
316
317   for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
318        SI != SE; ++SI) {
319     unsigned AbbrevToUse = 0;
320
321     // VST_ENTRY: [valueid, namelen, namechar x N]
322     NameVals.push_back(VE.getValueID(SI->getValue()));
323   
324     NameVals.push_back(SI->getKeyLength());
325     for (const char *P = SI->getKeyData(),
326          *E = SI->getKeyData()+SI->getKeyLength(); P != E; ++P)
327       NameVals.push_back((unsigned char)*P);
328     
329     // Emit the finished record.
330     Stream.EmitRecord(bitc::VST_ENTRY_CODE, NameVals, AbbrevToUse);
331     NameVals.clear();
332   }
333   Stream.ExitBlock();
334 }
335
336
337
338 /// WriteModule - Emit the specified module to the bitstream.
339 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
340   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
341   
342   // Emit the version number if it is non-zero.
343   if (CurVersion) {
344     SmallVector<unsigned, 1> VersionVals;
345     VersionVals.push_back(CurVersion);
346     Stream.EmitRecord(bitc::MODULE_CODE_VERSION, VersionVals);
347   }
348   
349   // Analyze the module, enumerating globals, functions, etc.
350   ValueEnumerator VE(M);
351   
352   // Emit information describing all of the types in the module.
353   WriteTypeTable(VE, Stream);
354   
355   // FIXME: Emit constants.
356   
357   // Emit top-level description of module, including target triple, inline asm,
358   // descriptors for global variables, and function prototype info.
359   WriteModuleInfo(M, VE, Stream);
360   
361   // Emit the type symbol table information.
362   WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
363   
364   // Emit names for globals/functions etc.
365   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
366   
367   Stream.ExitBlock();
368 }
369
370 /// WriteBitcodeToFile - Write the specified module to the specified output
371 /// stream.
372 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
373   std::vector<unsigned char> Buffer;
374   BitstreamWriter Stream(Buffer);
375   
376   Buffer.reserve(256*1024);
377   
378   // Emit the file header.
379   Stream.Emit((unsigned)'B', 8);
380   Stream.Emit((unsigned)'C', 8);
381   Stream.Emit(0x0, 4);
382   Stream.Emit(0xC, 4);
383   Stream.Emit(0xE, 4);
384   Stream.Emit(0xD, 4);
385
386   // Emit the module.
387   WriteModule(M, Stream);
388   
389   // Write the generated bitstream to "Out".
390   Out.write((char*)&Buffer.front(), Buffer.size());
391 }