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