blob: 8e1c4d24749edab72f7393e4fd2985615cf145e6 [file] [log] [blame]
Chris Lattner1a9df8e2007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerfd57cec2007-04-22 06:24:45 +00002//
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"
Chris Lattner47f96bf2007-04-23 01:01:37 +000016#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000017#include "ValueEnumerator.h"
Chris Lattner2edd22b2007-04-24 00:16:04 +000018#include "llvm/Constants.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000020#include "llvm/Instructions.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000021#include "llvm/Module.h"
Chris Lattnerf0a65312007-05-04 02:59:04 +000022#include "llvm/ParameterAttributes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000023#include "llvm/TypeSymbolTable.h"
Chris Lattnerb992be12007-04-23 20:35:01 +000024#include "llvm/ValueSymbolTable.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000025#include "llvm/Support/MathExtras.h"
26using namespace llvm;
27
Chris Lattner59698302007-05-04 20:52:02 +000028/// These are manifest constants used by the bitcode writer. They do not need to
29/// be kept in sync with the reader, but need to be consistent within this file.
30enum {
31 CurVersion = 0,
32
33 // VALUE_SYMTAB_BLOCK abbrev id's.
34 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattner2453f712007-05-04 20:58:35 +000035 VST_ENTRY_7_ABBREV,
Chris Lattnerff294a42007-05-05 01:26:50 +000036 VST_ENTRY_6_ABBREV,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000037 VST_BBENTRY_6_ABBREV,
38
39 // CONSTANTS_BLOCK abbrev id's.
40 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
41 CONSTANTS_INTEGER_ABBREV,
42 CONSTANTS_CE_CAST_Abbrev,
Chris Lattner440168b2007-05-05 07:44:49 +000043 CONSTANTS_NULL_Abbrev,
44
45 // FUNCTION_BLOCK abbrev id's.
Chris Lattner94687ac2007-05-06 01:28:01 +000046 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
47 FUNCTION_INST_RET_VOID_ABBREV,
48 FUNCTION_INST_RET_VAL_ABBREV,
49 FUNCTION_INST_UNREACHABLE_ABBREV
Chris Lattner59698302007-05-04 20:52:02 +000050};
51
Chris Lattnerfd57cec2007-04-22 06:24:45 +000052
Chris Lattnerf581c3b2007-04-24 07:07:11 +000053static unsigned GetEncodedCastOpcode(unsigned Opcode) {
54 switch (Opcode) {
55 default: assert(0 && "Unknown cast instruction!");
56 case Instruction::Trunc : return bitc::CAST_TRUNC;
57 case Instruction::ZExt : return bitc::CAST_ZEXT;
58 case Instruction::SExt : return bitc::CAST_SEXT;
59 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
60 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
61 case Instruction::UIToFP : return bitc::CAST_UITOFP;
62 case Instruction::SIToFP : return bitc::CAST_SITOFP;
63 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
64 case Instruction::FPExt : return bitc::CAST_FPEXT;
65 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
66 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
67 case Instruction::BitCast : return bitc::CAST_BITCAST;
68 }
69}
70
71static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
72 switch (Opcode) {
73 default: assert(0 && "Unknown binary instruction!");
74 case Instruction::Add: return bitc::BINOP_ADD;
75 case Instruction::Sub: return bitc::BINOP_SUB;
76 case Instruction::Mul: return bitc::BINOP_MUL;
77 case Instruction::UDiv: return bitc::BINOP_UDIV;
78 case Instruction::FDiv:
79 case Instruction::SDiv: return bitc::BINOP_SDIV;
80 case Instruction::URem: return bitc::BINOP_UREM;
81 case Instruction::FRem:
82 case Instruction::SRem: return bitc::BINOP_SREM;
83 case Instruction::Shl: return bitc::BINOP_SHL;
84 case Instruction::LShr: return bitc::BINOP_LSHR;
85 case Instruction::AShr: return bitc::BINOP_ASHR;
86 case Instruction::And: return bitc::BINOP_AND;
87 case Instruction::Or: return bitc::BINOP_OR;
88 case Instruction::Xor: return bitc::BINOP_XOR;
89 }
90}
91
92
93
Chris Lattnerfd57cec2007-04-22 06:24:45 +000094static void WriteStringRecord(unsigned Code, const std::string &Str,
95 unsigned AbbrevToUse, BitstreamWriter &Stream) {
96 SmallVector<unsigned, 64> Vals;
97
Chris Lattner15e6d172007-05-04 19:11:41 +000098 // Code: [strchar x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +000099 for (unsigned i = 0, e = Str.size(); i != e; ++i)
100 Vals.push_back(Str[i]);
101
102 // Emit the finished record.
103 Stream.EmitRecord(Code, Vals, AbbrevToUse);
104}
105
Chris Lattner62bbeea2007-05-04 00:44:52 +0000106// Emit information about parameter attributes.
107static void WriteParamAttrTable(const ValueEnumerator &VE,
108 BitstreamWriter &Stream) {
109 const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
110 if (Attrs.empty()) return;
111
Chris Lattnerf0a65312007-05-04 02:59:04 +0000112 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
113
114 SmallVector<uint64_t, 64> Record;
115 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
116 const ParamAttrsList *A = Attrs[i];
117 for (unsigned op = 0, e = A->size(); op != e; ++op) {
118 Record.push_back(A->getParamIndex(op));
Chris Lattnerf6f9cd12007-05-04 03:14:09 +0000119 Record.push_back(A->getParamAttrsAtIndex(op));
Chris Lattnerf0a65312007-05-04 02:59:04 +0000120 }
121
122 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
123 Record.clear();
124 }
Chris Lattner62bbeea2007-05-04 00:44:52 +0000125
Chris Lattnerf0a65312007-05-04 02:59:04 +0000126 Stream.ExitBlock();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000127}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000128
129/// WriteTypeTable - Write out the type table for a module.
130static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
131 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
132
133 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
134 SmallVector<uint64_t, 64> TypeVals;
135
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000136 // Abbrev for TYPE_CODE_POINTER.
137 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
138 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
139 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
140 Log2_32_Ceil(VE.getTypes().size()+1)));
141 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
142
143 // Abbrev for TYPE_CODE_FUNCTION.
144 Abbv = new BitCodeAbbrev();
145 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
146 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
147 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
148 Log2_32_Ceil(VE.getParamAttrs().size()+1)));
149 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
150 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
151 Log2_32_Ceil(VE.getTypes().size()+1)));
152 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
153
154 // Abbrev for TYPE_CODE_STRUCT.
155 Abbv = new BitCodeAbbrev();
156 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
157 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
159 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
160 Log2_32_Ceil(VE.getTypes().size()+1)));
161 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
162
163 // Abbrev for TYPE_CODE_ARRAY.
164 Abbv = new BitCodeAbbrev();
165 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
166 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
167 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
168 Log2_32_Ceil(VE.getTypes().size()+1)));
169 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000170
171 // Emit an entry count so the reader can reserve space.
172 TypeVals.push_back(TypeList.size());
173 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
174 TypeVals.clear();
175
176 // Loop over all of the types, emitting each in turn.
177 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
178 const Type *T = TypeList[i].first;
179 int AbbrevToUse = 0;
180 unsigned Code = 0;
181
182 switch (T->getTypeID()) {
183 case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
184 default: assert(0 && "Unknown type!");
185 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
186 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
187 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
188 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
189 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
190 case Type::IntegerTyID:
191 // INTEGER: [width]
192 Code = bitc::TYPE_CODE_INTEGER;
193 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
194 break;
195 case Type::PointerTyID:
196 // POINTER: [pointee type]
197 Code = bitc::TYPE_CODE_POINTER;
198 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000199 AbbrevToUse = PtrAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000200 break;
201
202 case Type::FunctionTyID: {
203 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000204 // FUNCTION: [isvararg, attrid, retty, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000205 Code = bitc::TYPE_CODE_FUNCTION;
206 TypeVals.push_back(FT->isVarArg());
Chris Lattner9113e732007-05-04 03:41:34 +0000207 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000208 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000209 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
210 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000211 AbbrevToUse = FunctionAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000212 break;
213 }
214 case Type::StructTyID: {
215 const StructType *ST = cast<StructType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000216 // STRUCT: [ispacked, eltty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000217 Code = bitc::TYPE_CODE_STRUCT;
218 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000219 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000220 for (StructType::element_iterator I = ST->element_begin(),
221 E = ST->element_end(); I != E; ++I)
222 TypeVals.push_back(VE.getTypeID(*I));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000223 AbbrevToUse = StructAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000224 break;
225 }
226 case Type::ArrayTyID: {
227 const ArrayType *AT = cast<ArrayType>(T);
228 // ARRAY: [numelts, eltty]
229 Code = bitc::TYPE_CODE_ARRAY;
230 TypeVals.push_back(AT->getNumElements());
231 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000232 AbbrevToUse = ArrayAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000233 break;
234 }
235 case Type::VectorTyID: {
236 const VectorType *VT = cast<VectorType>(T);
237 // VECTOR [numelts, eltty]
238 Code = bitc::TYPE_CODE_VECTOR;
239 TypeVals.push_back(VT->getNumElements());
240 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
241 break;
242 }
243 }
244
245 // Emit the finished record.
246 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
247 TypeVals.clear();
248 }
249
250 Stream.ExitBlock();
251}
252
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000253static unsigned getEncodedLinkage(const GlobalValue *GV) {
254 switch (GV->getLinkage()) {
255 default: assert(0 && "Invalid linkage!");
256 case GlobalValue::ExternalLinkage: return 0;
257 case GlobalValue::WeakLinkage: return 1;
258 case GlobalValue::AppendingLinkage: return 2;
259 case GlobalValue::InternalLinkage: return 3;
260 case GlobalValue::LinkOnceLinkage: return 4;
261 case GlobalValue::DLLImportLinkage: return 5;
262 case GlobalValue::DLLExportLinkage: return 6;
263 case GlobalValue::ExternalWeakLinkage: return 7;
264 }
265}
266
267static unsigned getEncodedVisibility(const GlobalValue *GV) {
268 switch (GV->getVisibility()) {
269 default: assert(0 && "Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000270 case GlobalValue::DefaultVisibility: return 0;
271 case GlobalValue::HiddenVisibility: return 1;
272 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000273 }
274}
275
276// Emit top-level description of module, including target triple, inline asm,
277// descriptors for global variables, and function prototype info.
278static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
279 BitstreamWriter &Stream) {
280 // Emit the list of dependent libraries for the Module.
281 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
282 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
283
284 // Emit various pieces of data attached to a module.
285 if (!M->getTargetTriple().empty())
286 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
287 0/*TODO*/, Stream);
288 if (!M->getDataLayout().empty())
289 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
290 0/*TODO*/, Stream);
291 if (!M->getModuleInlineAsm().empty())
292 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
293 0/*TODO*/, Stream);
294
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000295 // Emit information about sections, computing how many there are. Also
296 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000297 std::map<std::string, unsigned> SectionMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000298 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000299 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000300 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
301 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000302 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000303 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000304
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000305 if (!GV->hasSection()) continue;
306 // Give section names unique ID's.
307 unsigned &Entry = SectionMap[GV->getSection()];
308 if (Entry != 0) continue;
309 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
310 0/*TODO*/, Stream);
311 Entry = SectionMap.size();
312 }
313 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000314 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000315 if (!F->hasSection()) continue;
316 // Give section names unique ID's.
317 unsigned &Entry = SectionMap[F->getSection()];
318 if (Entry != 0) continue;
319 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
320 0/*TODO*/, Stream);
321 Entry = SectionMap.size();
322 }
323
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000324 // Emit abbrev for globals, now that we know # sections and max alignment.
325 unsigned SimpleGVarAbbrev = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000326 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000327 // Add an abbrev for common globals with no visibility or thread localness.
328 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
329 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000330 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000331 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000332 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000333 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000334 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
335 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000336 Abbv->Add(BitCodeAbbrevOp(0));
337 else {
338 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000340 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000341 }
342 if (SectionMap.empty()) // Section.
343 Abbv->Add(BitCodeAbbrevOp(0));
344 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000345 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000346 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000347 // Don't bother emitting vis + thread local.
348 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
349 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000350
351 // Emit the global variable information.
352 SmallVector<unsigned, 64> Vals;
353 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
354 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000355 unsigned AbbrevToUse = 0;
356
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000357 // GLOBALVAR: [type, isconst, initid,
358 // linkage, alignment, section, visibility, threadlocal]
359 Vals.push_back(VE.getTypeID(GV->getType()));
360 Vals.push_back(GV->isConstant());
361 Vals.push_back(GV->isDeclaration() ? 0 :
362 (VE.getValueID(GV->getInitializer()) + 1));
363 Vals.push_back(getEncodedLinkage(GV));
364 Vals.push_back(Log2_32(GV->getAlignment())+1);
365 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000366 if (GV->isThreadLocal() ||
367 GV->getVisibility() != GlobalValue::DefaultVisibility) {
368 Vals.push_back(getEncodedVisibility(GV));
369 Vals.push_back(GV->isThreadLocal());
370 } else {
371 AbbrevToUse = SimpleGVarAbbrev;
372 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000373
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000374 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
375 Vals.clear();
376 }
377
378 // Emit the function proto information.
379 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
380 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
381 // visibility]
382 Vals.push_back(VE.getTypeID(F->getType()));
383 Vals.push_back(F->getCallingConv());
384 Vals.push_back(F->isDeclaration());
385 Vals.push_back(getEncodedLinkage(F));
386 Vals.push_back(Log2_32(F->getAlignment())+1);
387 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
388 Vals.push_back(getEncodedVisibility(F));
389
390 unsigned AbbrevToUse = 0;
391 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
392 Vals.clear();
393 }
Chris Lattner07d98b42007-04-26 02:46:40 +0000394
395
396 // Emit the alias information.
397 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
398 AI != E; ++AI) {
399 Vals.push_back(VE.getTypeID(AI->getType()));
400 Vals.push_back(VE.getValueID(AI->getAliasee()));
401 Vals.push_back(getEncodedLinkage(AI));
402 unsigned AbbrevToUse = 0;
403 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
404 Vals.clear();
405 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000406}
407
408
Chris Lattner2edd22b2007-04-24 00:16:04 +0000409static void WriteConstants(unsigned FirstVal, unsigned LastVal,
410 const ValueEnumerator &VE,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000411 BitstreamWriter &Stream, bool isGlobal) {
Chris Lattner2edd22b2007-04-24 00:16:04 +0000412 if (FirstVal == LastVal) return;
413
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000414 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Chris Lattnerb992be12007-04-23 20:35:01 +0000415
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000416 unsigned AggregateAbbrev = 0;
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000417 unsigned String8Abbrev = 0;
418 unsigned CString7Abbrev = 0;
419 unsigned CString6Abbrev = 0;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000420 // If this is a constant pool for the module, emit module-specific abbrevs.
421 if (isGlobal) {
422 // Abbrev for CST_CODE_AGGREGATE.
423 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
424 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
425 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
426 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
427 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner817f08a2007-05-06 00:42:18 +0000428
429 // Abbrev for CST_CODE_STRING.
430 Abbv = new BitCodeAbbrev();
431 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
434 String8Abbrev = Stream.EmitAbbrev(Abbv);
435 // Abbrev for CST_CODE_CSTRING.
436 Abbv = new BitCodeAbbrev();
437 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
438 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattner817f08a2007-05-06 00:42:18 +0000439 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000440 CString7Abbrev = Stream.EmitAbbrev(Abbv);
441 // Abbrev for CST_CODE_CSTRING.
442 Abbv = new BitCodeAbbrev();
443 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
445 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
446 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000447 }
448
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000449 // FIXME: Install and use abbrevs to reduce size. Install them globally so
450 // they don't need to be reemitted for each function body.
Chris Lattner2edd22b2007-04-24 00:16:04 +0000451
452 SmallVector<uint64_t, 64> Record;
453
454 const ValueEnumerator::ValueList &Vals = VE.getValues();
455 const Type *LastTy = 0;
456 for (unsigned i = FirstVal; i != LastVal; ++i) {
457 const Value *V = Vals[i].first;
458 // If we need to switch types, do so now.
459 if (V->getType() != LastTy) {
460 LastTy = V->getType();
461 Record.push_back(VE.getTypeID(LastTy));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000462 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
463 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000464 Record.clear();
465 }
466
467 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
468 assert(0 && IA && "FIXME: Inline asm writing unimp!");
469 continue;
470 }
471 const Constant *C = cast<Constant>(V);
472 unsigned Code = -1U;
473 unsigned AbbrevToUse = 0;
474 if (C->isNullValue()) {
475 Code = bitc::CST_CODE_NULL;
476 } else if (isa<UndefValue>(C)) {
477 Code = bitc::CST_CODE_UNDEF;
478 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
479 if (IV->getBitWidth() <= 64) {
480 int64_t V = IV->getSExtValue();
481 if (V >= 0)
482 Record.push_back(V << 1);
483 else
484 Record.push_back((-V << 1) | 1);
485 Code = bitc::CST_CODE_INTEGER;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000486 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000487 } else { // Wide integers, > 64 bits in size.
488 // We have an arbitrary precision integer value to write whose
489 // bit width is > 64. However, in canonical unsigned integer
490 // format it is likely that the high bits are going to be zero.
491 // So, we only write the number of active words.
492 unsigned NWords = IV->getValue().getActiveWords();
493 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000494 for (unsigned i = 0; i != NWords; ++i) {
495 int64_t V = RawWords[i];
496 if (V >= 0)
497 Record.push_back(V << 1);
498 else
499 Record.push_back((-V << 1) | 1);
500 }
501 Code = bitc::CST_CODE_WIDE_INTEGER;
502 }
503 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
504 Code = bitc::CST_CODE_FLOAT;
505 if (CFP->getType() == Type::FloatTy) {
506 Record.push_back(FloatToBits((float)CFP->getValue()));
507 } else {
508 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
509 Record.push_back(DoubleToBits((double)CFP->getValue()));
510 }
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000511 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
512 // Emit constant strings specially.
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000513 unsigned NumOps = C->getNumOperands();
514 // If this is a null-terminated string, use the denser CSTRING encoding.
515 if (C->getOperand(NumOps-1)->isNullValue()) {
516 Code = bitc::CST_CODE_CSTRING;
517 --NumOps; // Don't encode the null, which isn't allowed by char6.
518 } else {
519 Code = bitc::CST_CODE_STRING;
520 AbbrevToUse = String8Abbrev;
521 }
522 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
523 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
524 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner817f08a2007-05-06 00:42:18 +0000525 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
526 Record.push_back(V);
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000527 isCStr7 &= (V & 128) == 0;
528 if (isCStrChar6)
529 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
Chris Lattner817f08a2007-05-06 00:42:18 +0000530 }
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000531
532 if (isCStrChar6)
533 AbbrevToUse = CString6Abbrev;
534 else if (isCStr7)
535 AbbrevToUse = CString7Abbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000536 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
537 isa<ConstantVector>(V)) {
538 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000539 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
540 Record.push_back(VE.getValueID(C->getOperand(i)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000541 AbbrevToUse = AggregateAbbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000542 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000543 switch (CE->getOpcode()) {
544 default:
545 if (Instruction::isCast(CE->getOpcode())) {
546 Code = bitc::CST_CODE_CE_CAST;
547 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
548 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
549 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000550 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000551 } else {
552 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
553 Code = bitc::CST_CODE_CE_BINOP;
554 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
555 Record.push_back(VE.getValueID(C->getOperand(0)));
556 Record.push_back(VE.getValueID(C->getOperand(1)));
557 }
558 break;
559 case Instruction::GetElementPtr:
560 Code = bitc::CST_CODE_CE_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000561 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
562 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
563 Record.push_back(VE.getValueID(C->getOperand(i)));
564 }
565 break;
566 case Instruction::Select:
567 Code = bitc::CST_CODE_CE_SELECT;
568 Record.push_back(VE.getValueID(C->getOperand(0)));
569 Record.push_back(VE.getValueID(C->getOperand(1)));
570 Record.push_back(VE.getValueID(C->getOperand(2)));
571 break;
572 case Instruction::ExtractElement:
573 Code = bitc::CST_CODE_CE_EXTRACTELT;
574 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
575 Record.push_back(VE.getValueID(C->getOperand(0)));
576 Record.push_back(VE.getValueID(C->getOperand(1)));
577 break;
578 case Instruction::InsertElement:
579 Code = bitc::CST_CODE_CE_INSERTELT;
580 Record.push_back(VE.getValueID(C->getOperand(0)));
581 Record.push_back(VE.getValueID(C->getOperand(1)));
582 Record.push_back(VE.getValueID(C->getOperand(2)));
583 break;
584 case Instruction::ShuffleVector:
585 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
586 Record.push_back(VE.getValueID(C->getOperand(0)));
587 Record.push_back(VE.getValueID(C->getOperand(1)));
588 Record.push_back(VE.getValueID(C->getOperand(2)));
589 break;
590 case Instruction::ICmp:
591 case Instruction::FCmp:
592 Code = bitc::CST_CODE_CE_CMP;
593 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
594 Record.push_back(VE.getValueID(C->getOperand(0)));
595 Record.push_back(VE.getValueID(C->getOperand(1)));
596 Record.push_back(CE->getPredicate());
597 break;
598 }
Chris Lattner2edd22b2007-04-24 00:16:04 +0000599 } else {
600 assert(0 && "Unknown constant!");
601 }
602 Stream.EmitRecord(Code, Record, AbbrevToUse);
603 Record.clear();
604 }
605
606 Stream.ExitBlock();
607}
608
609static void WriteModuleConstants(const ValueEnumerator &VE,
610 BitstreamWriter &Stream) {
611 const ValueEnumerator::ValueList &Vals = VE.getValues();
612
613 // Find the first constant to emit, which is the first non-globalvalue value.
614 // We know globalvalues have been emitted by WriteModuleInfo.
615 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
616 if (!isa<GlobalValue>(Vals[i].first)) {
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000617 WriteConstants(i, Vals.size(), VE, Stream, true);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000618 return;
619 }
620 }
621}
Chris Lattnerb992be12007-04-23 20:35:01 +0000622
Chris Lattner7337ab92007-05-06 00:00:00 +0000623/// PushValueAndType - The file has to encode both the value and type id for
624/// many values, because we need to know what type to create for forward
625/// references. However, most operands are not forward references, so this type
626/// field is not needed.
627///
628/// This function adds V's value ID to Vals. If the value ID is higher than the
629/// instruction ID, then it is a forward reference, and it also includes the
630/// type ID.
631static bool PushValueAndType(Value *V, unsigned InstID,
632 SmallVector<unsigned, 64> &Vals,
633 ValueEnumerator &VE) {
634 unsigned ValID = VE.getValueID(V);
635 Vals.push_back(ValID);
636 if (ValID >= InstID) {
637 Vals.push_back(VE.getTypeID(V->getType()));
638 return true;
639 }
640 return false;
641}
642
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000643/// WriteInstruction - Emit an instruction to the specified stream.
Chris Lattner7337ab92007-05-06 00:00:00 +0000644static void WriteInstruction(const Instruction &I, unsigned InstID,
645 ValueEnumerator &VE, BitstreamWriter &Stream,
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000646 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000647 unsigned Code = 0;
648 unsigned AbbrevToUse = 0;
649 switch (I.getOpcode()) {
650 default:
651 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +0000652 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000653 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000654 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000655 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000656 } else {
657 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +0000658 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000659 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000660 Vals.push_back(VE.getValueID(I.getOperand(1)));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000661 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000662 }
663 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000664
665 case Instruction::GetElementPtr:
666 Code = bitc::FUNC_CODE_INST_GEP;
Chris Lattner7337ab92007-05-06 00:00:00 +0000667 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
668 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000669 break;
670 case Instruction::Select:
671 Code = bitc::FUNC_CODE_INST_SELECT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000672 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000673 Vals.push_back(VE.getValueID(I.getOperand(2)));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000674 Vals.push_back(VE.getValueID(I.getOperand(0)));
Chris Lattnerd309c752007-05-01 02:13:26 +0000675 break;
676 case Instruction::ExtractElement:
677 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000678 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000679 Vals.push_back(VE.getValueID(I.getOperand(1)));
680 break;
681 case Instruction::InsertElement:
682 Code = bitc::FUNC_CODE_INST_INSERTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000683 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000684 Vals.push_back(VE.getValueID(I.getOperand(1)));
685 Vals.push_back(VE.getValueID(I.getOperand(2)));
686 break;
687 case Instruction::ShuffleVector:
688 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000689 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000690 Vals.push_back(VE.getValueID(I.getOperand(1)));
691 Vals.push_back(VE.getValueID(I.getOperand(2)));
692 break;
693 case Instruction::ICmp:
694 case Instruction::FCmp:
695 Code = bitc::FUNC_CODE_INST_CMP;
Chris Lattner7337ab92007-05-06 00:00:00 +0000696 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000697 Vals.push_back(VE.getValueID(I.getOperand(1)));
698 Vals.push_back(cast<CmpInst>(I).getPredicate());
699 break;
700
701 case Instruction::Ret:
702 Code = bitc::FUNC_CODE_INST_RET;
Chris Lattner94687ac2007-05-06 01:28:01 +0000703 if (!I.getNumOperands())
704 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
705 else if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
706 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
Chris Lattnerd309c752007-05-01 02:13:26 +0000707 break;
708 case Instruction::Br:
709 Code = bitc::FUNC_CODE_INST_BR;
710 Vals.push_back(VE.getValueID(I.getOperand(0)));
711 if (cast<BranchInst>(I).isConditional()) {
712 Vals.push_back(VE.getValueID(I.getOperand(1)));
713 Vals.push_back(VE.getValueID(I.getOperand(2)));
714 }
715 break;
716 case Instruction::Switch:
717 Code = bitc::FUNC_CODE_INST_SWITCH;
718 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000719 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
720 Vals.push_back(VE.getValueID(I.getOperand(i)));
721 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000722 case Instruction::Invoke: {
Chris Lattnerd309c752007-05-01 02:13:26 +0000723 Code = bitc::FUNC_CODE_INST_INVOKE;
Chris Lattner76520192007-05-03 22:34:03 +0000724 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
Chris Lattner7337ab92007-05-06 00:00:00 +0000725 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest
726 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest
727 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000728
Chris Lattnerd309c752007-05-01 02:13:26 +0000729 // Emit value #'s for the fixed parameters.
730 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
731 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
732 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
733 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
734
735 // Emit type/value pairs for varargs params.
736 if (FTy->isVarArg()) {
Chris Lattner7337ab92007-05-06 00:00:00 +0000737 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
738 i != e; ++i)
739 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
Chris Lattnerd309c752007-05-01 02:13:26 +0000740 }
741 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000742 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000743 case Instruction::Unwind:
744 Code = bitc::FUNC_CODE_INST_UNWIND;
745 break;
746 case Instruction::Unreachable:
747 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattner94687ac2007-05-06 01:28:01 +0000748 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000749 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000750
751 case Instruction::PHI:
752 Code = bitc::FUNC_CODE_INST_PHI;
753 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000754 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
755 Vals.push_back(VE.getValueID(I.getOperand(i)));
756 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000757
Chris Lattnerd309c752007-05-01 02:13:26 +0000758 case Instruction::Malloc:
759 Code = bitc::FUNC_CODE_INST_MALLOC;
760 Vals.push_back(VE.getTypeID(I.getType()));
761 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
762 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
763 break;
764
765 case Instruction::Free:
766 Code = bitc::FUNC_CODE_INST_FREE;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000767 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000768 break;
769
770 case Instruction::Alloca:
771 Code = bitc::FUNC_CODE_INST_ALLOCA;
772 Vals.push_back(VE.getTypeID(I.getType()));
773 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
774 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
775 break;
776
777 case Instruction::Load:
778 Code = bitc::FUNC_CODE_INST_LOAD;
Chris Lattner7337ab92007-05-06 00:00:00 +0000779 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
780 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
781
Chris Lattnerd309c752007-05-01 02:13:26 +0000782 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
783 Vals.push_back(cast<LoadInst>(I).isVolatile());
784 break;
785 case Instruction::Store:
786 Code = bitc::FUNC_CODE_INST_STORE;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000787 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // val.
788 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
Chris Lattnerd309c752007-05-01 02:13:26 +0000789 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
790 Vals.push_back(cast<StoreInst>(I).isVolatile());
791 break;
792 case Instruction::Call: {
793 Code = bitc::FUNC_CODE_INST_CALL;
Chris Lattner76520192007-05-03 22:34:03 +0000794 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
795 cast<CallInst>(I).isTailCall());
Chris Lattner7337ab92007-05-06 00:00:00 +0000796 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee
Chris Lattnerd309c752007-05-01 02:13:26 +0000797
798 // Emit value #'s for the fixed parameters.
799 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
800 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
801 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
802 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
803
Chris Lattner60ce9b52007-05-01 07:03:37 +0000804 // Emit type/value pairs for varargs params.
805 if (FTy->isVarArg()) {
806 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
Chris Lattner60ce9b52007-05-01 07:03:37 +0000807 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
Chris Lattner7337ab92007-05-06 00:00:00 +0000808 i != e; ++i)
809 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
Chris Lattnerd309c752007-05-01 02:13:26 +0000810 }
811 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000812 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000813 case Instruction::VAArg:
814 Code = bitc::FUNC_CODE_INST_VAARG;
815 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
816 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
817 Vals.push_back(VE.getTypeID(I.getType())); // restype.
818 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000819 }
820
821 Stream.EmitRecord(Code, Vals, AbbrevToUse);
822 Vals.clear();
823}
824
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000825// Emit names for globals/functions etc.
826static void WriteValueSymbolTable(const ValueSymbolTable &VST,
827 const ValueEnumerator &VE,
828 BitstreamWriter &Stream) {
829 if (VST.empty()) return;
Chris Lattnerff294a42007-05-05 01:26:50 +0000830 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2e7899d2007-05-04 20:34:50 +0000831
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000832 // FIXME: Set up the abbrev, we know how many values there are!
833 // FIXME: We know if the type names can use 7-bit ascii.
834 SmallVector<unsigned, 64> NameVals;
835
836 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
837 SI != SE; ++SI) {
Chris Lattner59698302007-05-04 20:52:02 +0000838
839 const ValueName &Name = *SI;
840
841 // Figure out the encoding to use for the name.
842 bool is7Bit = true;
Chris Lattnerff294a42007-05-05 01:26:50 +0000843 bool isChar6 = true;
844 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
845 C != E; ++C) {
846 if (isChar6)
847 isChar6 = BitCodeAbbrevOp::isChar6(*C);
848 if ((unsigned char)*C & 128) {
Chris Lattner59698302007-05-04 20:52:02 +0000849 is7Bit = false;
Chris Lattnerff294a42007-05-05 01:26:50 +0000850 break; // don't bother scanning the rest.
Chris Lattner59698302007-05-04 20:52:02 +0000851 }
Chris Lattnerff294a42007-05-05 01:26:50 +0000852 }
Chris Lattner59698302007-05-04 20:52:02 +0000853
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000854 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000855
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000856 // VST_ENTRY: [valueid, namechar x N]
857 // VST_BBENTRY: [bbid, namechar x N]
Chris Lattnere825ed52007-05-03 22:18:21 +0000858 unsigned Code;
859 if (isa<BasicBlock>(SI->getValue())) {
860 Code = bitc::VST_CODE_BBENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000861 if (isChar6)
862 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000863 } else {
864 Code = bitc::VST_CODE_ENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000865 if (isChar6)
866 AbbrevToUse = VST_ENTRY_6_ABBREV;
867 else if (is7Bit)
868 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000869 }
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000870
Chris Lattnere825ed52007-05-03 22:18:21 +0000871 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +0000872 for (const char *P = Name.getKeyData(),
873 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000874 NameVals.push_back((unsigned char)*P);
875
876 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +0000877 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000878 NameVals.clear();
879 }
880 Stream.ExitBlock();
881}
882
Chris Lattner8d35c792007-04-26 03:50:57 +0000883/// WriteFunction - Emit a function body to the module stream.
Chris Lattner198f34a2007-04-26 03:27:58 +0000884static void WriteFunction(const Function &F, ValueEnumerator &VE,
885 BitstreamWriter &Stream) {
Chris Lattner01b27452007-04-29 05:49:09 +0000886 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3);
Chris Lattner8d35c792007-04-26 03:50:57 +0000887 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000888
889 SmallVector<unsigned, 64> Vals;
890
891 // Emit the number of basic blocks, so the reader can create them ahead of
892 // time.
893 Vals.push_back(VE.getBasicBlocks().size());
894 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
895 Vals.clear();
896
897 // FIXME: Function attributes?
898
899 // If there are function-local constants, emit them now.
900 unsigned CstStart, CstEnd;
901 VE.getFunctionConstantRange(CstStart, CstEnd);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000902 WriteConstants(CstStart, CstEnd, VE, Stream, false);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000903
Chris Lattner7337ab92007-05-06 00:00:00 +0000904 // Keep a running idea of what the instruction ID is.
905 unsigned InstID = CstEnd;
906
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000907 // Finally, emit all the instructions, in order.
908 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner7337ab92007-05-06 00:00:00 +0000909 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
910 I != E; ++I) {
911 WriteInstruction(*I, InstID, VE, Stream, Vals);
912 if (I->getType() != Type::VoidTy)
913 ++InstID;
914 }
Chris Lattner198f34a2007-04-26 03:27:58 +0000915
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000916 // Emit names for all the instructions etc.
917 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
918
Chris Lattner8d35c792007-04-26 03:50:57 +0000919 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000920 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +0000921}
922
923/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
924static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
925 const ValueEnumerator &VE,
926 BitstreamWriter &Stream) {
927 if (TST.empty()) return;
928
929 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
930
Chris Lattner7a263ea2007-05-05 00:47:19 +0000931 // 7-bit fixed width VST_CODE_ENTRY strings.
932 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
933 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
934 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
935 Log2_32_Ceil(VE.getTypes().size()+1)));
936 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
937 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
938 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner198f34a2007-04-26 03:27:58 +0000939
940 SmallVector<unsigned, 64> NameVals;
941
942 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
943 TI != TE; ++TI) {
Chris Lattner7a263ea2007-05-05 00:47:19 +0000944 // TST_ENTRY: [typeid, namechar x N]
Chris Lattner198f34a2007-04-26 03:27:58 +0000945 NameVals.push_back(VE.getTypeID(TI->second));
946
947 const std::string &Str = TI->first;
Chris Lattner7a263ea2007-05-05 00:47:19 +0000948 bool is7Bit = true;
949 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
950 NameVals.push_back((unsigned char)Str[i]);
951 if (Str[i] & 128)
952 is7Bit = false;
953 }
Chris Lattner198f34a2007-04-26 03:27:58 +0000954
955 // Emit the finished record.
Chris Lattner7a263ea2007-05-05 00:47:19 +0000956 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
Chris Lattner198f34a2007-04-26 03:27:58 +0000957 NameVals.clear();
958 }
959
960 Stream.ExitBlock();
961}
962
Chris Lattner07faafc2007-05-04 18:26:27 +0000963// Emit blockinfo, which defines the standard abbreviations etc.
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000964static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
Chris Lattner07faafc2007-05-04 18:26:27 +0000965 // We only want to emit block info records for blocks that have multiple
966 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
967 // blocks can defined their abbrevs inline.
Chris Lattnere17b6582007-05-05 00:17:00 +0000968 Stream.EnterBlockInfoBlock(2);
Chris Lattner07faafc2007-05-04 18:26:27 +0000969
Chris Lattnere17b6582007-05-05 00:17:00 +0000970 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
971 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
974 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
975 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
976 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
977 Abbv) != VST_ENTRY_8_ABBREV)
978 assert(0 && "Unexpected abbrev ordering!");
979 }
980
981 { // 7-bit fixed width VST_ENTRY strings.
982 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
983 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
984 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
985 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
986 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
987 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
988 Abbv) != VST_ENTRY_7_ABBREV)
989 assert(0 && "Unexpected abbrev ordering!");
990 }
Chris Lattnerff294a42007-05-05 01:26:50 +0000991 { // 6-bit char6 VST_ENTRY strings.
992 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
993 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
994 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
995 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
996 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
997 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
998 Abbv) != VST_ENTRY_6_ABBREV)
999 assert(0 && "Unexpected abbrev ordering!");
1000 }
1001 { // 6-bit char6 VST_BBENTRY strings.
Chris Lattnere17b6582007-05-05 00:17:00 +00001002 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1003 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1004 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1005 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnerff294a42007-05-05 01:26:50 +00001006 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Chris Lattnere17b6582007-05-05 00:17:00 +00001007 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnerff294a42007-05-05 01:26:50 +00001008 Abbv) != VST_BBENTRY_6_ABBREV)
Chris Lattnere17b6582007-05-05 00:17:00 +00001009 assert(0 && "Unexpected abbrev ordering!");
1010 }
1011
Chris Lattner440168b2007-05-05 07:44:49 +00001012
1013
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001014 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1015 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1016 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1017 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1018 Log2_32_Ceil(VE.getTypes().size()+1)));
1019 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1020 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1021 assert(0 && "Unexpected abbrev ordering!");
1022 }
1023
1024 { // INTEGER abbrev for CONSTANTS_BLOCK.
1025 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1026 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1027 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1028 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1029 Abbv) != CONSTANTS_INTEGER_ABBREV)
1030 assert(0 && "Unexpected abbrev ordering!");
1031 }
1032
1033 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1034 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1035 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1036 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1037 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1038 Log2_32_Ceil(VE.getTypes().size()+1)));
1039 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1040
1041 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1042 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1043 assert(0 && "Unexpected abbrev ordering!");
1044 }
1045 { // NULL abbrev for CONSTANTS_BLOCK.
1046 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1047 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1048 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1049 Abbv) != CONSTANTS_NULL_Abbrev)
1050 assert(0 && "Unexpected abbrev ordering!");
1051 }
1052
Chris Lattner440168b2007-05-05 07:44:49 +00001053 // FIXME: This should only use space for first class types!
1054
1055 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1056 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1057 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattner440168b2007-05-05 07:44:49 +00001058 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1059 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1060 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1061 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1062 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1063 assert(0 && "Unexpected abbrev ordering!");
1064 }
Chris Lattner94687ac2007-05-06 01:28:01 +00001065 { // INST_RET abbrev for FUNCTION_BLOCK.
1066 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1067 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1068 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1069 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1070 assert(0 && "Unexpected abbrev ordering!");
1071 }
1072 { // INST_RET abbrev for FUNCTION_BLOCK.
1073 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1074 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1075 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1076 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1077 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1078 assert(0 && "Unexpected abbrev ordering!");
1079 }
1080 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1081 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1082 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1083 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1084 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1085 assert(0 && "Unexpected abbrev ordering!");
1086 }
Chris Lattner440168b2007-05-05 07:44:49 +00001087
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001088 Stream.ExitBlock();
1089}
1090
1091
1092/// WriteModule - Emit the specified module to the bitstream.
1093static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1094 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1095
1096 // Emit the version number if it is non-zero.
1097 if (CurVersion) {
1098 SmallVector<unsigned, 1> Vals;
1099 Vals.push_back(CurVersion);
1100 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1101 }
1102
1103 // Analyze the module, enumerating globals, functions, etc.
1104 ValueEnumerator VE(M);
1105
1106 // Emit blockinfo, which defines the standard abbreviations etc.
1107 WriteBlockInfo(VE, Stream);
1108
1109 // Emit information about parameter attributes.
1110 WriteParamAttrTable(VE, Stream);
1111
1112 // Emit information describing all of the types in the module.
1113 WriteTypeTable(VE, Stream);
1114
1115 // Emit top-level description of module, including target triple, inline asm,
1116 // descriptors for global variables, and function prototype info.
1117 WriteModuleInfo(M, VE, Stream);
1118
1119 // Emit constants.
1120 WriteModuleConstants(VE, Stream);
1121
1122 // If we have any aggregate values in the value table, purge them - these can
1123 // only be used to initialize global variables. Doing so makes the value
1124 // namespace smaller for code in functions.
1125 int NumNonAggregates = VE.PurgeAggregateValues();
1126 if (NumNonAggregates != -1) {
1127 SmallVector<unsigned, 1> Vals;
1128 Vals.push_back(NumNonAggregates);
1129 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1130 }
1131
1132 // Emit function bodies.
1133 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1134 if (!I->isDeclaration())
1135 WriteFunction(*I, VE, Stream);
1136
1137 // Emit the type symbol table information.
1138 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1139
1140 // Emit names for globals/functions etc.
1141 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1142
Chris Lattner07faafc2007-05-04 18:26:27 +00001143 Stream.ExitBlock();
1144}
1145
1146
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001147/// WriteBitcodeToFile - Write the specified module to the specified output
1148/// stream.
1149void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1150 std::vector<unsigned char> Buffer;
1151 BitstreamWriter Stream(Buffer);
1152
1153 Buffer.reserve(256*1024);
1154
1155 // Emit the file header.
1156 Stream.Emit((unsigned)'B', 8);
1157 Stream.Emit((unsigned)'C', 8);
1158 Stream.Emit(0x0, 4);
1159 Stream.Emit(0xC, 4);
1160 Stream.Emit(0xE, 4);
1161 Stream.Emit(0xD, 4);
1162
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001163 // Emit the module.
1164 WriteModule(M, Stream);
1165
1166 // Write the generated bitstream to "Out".
1167 Out.write((char*)&Buffer.front(), Buffer.size());
1168}