blob: 172db3ac785725a27446bfa1f36d6bb2b43ec8b1 [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,
35 VST_ENTRY_7_ABBREV
36
37};
38
Chris Lattnerfd57cec2007-04-22 06:24:45 +000039
Chris Lattnerf581c3b2007-04-24 07:07:11 +000040static unsigned GetEncodedCastOpcode(unsigned Opcode) {
41 switch (Opcode) {
42 default: assert(0 && "Unknown cast instruction!");
43 case Instruction::Trunc : return bitc::CAST_TRUNC;
44 case Instruction::ZExt : return bitc::CAST_ZEXT;
45 case Instruction::SExt : return bitc::CAST_SEXT;
46 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
47 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
48 case Instruction::UIToFP : return bitc::CAST_UITOFP;
49 case Instruction::SIToFP : return bitc::CAST_SITOFP;
50 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
51 case Instruction::FPExt : return bitc::CAST_FPEXT;
52 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
53 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
54 case Instruction::BitCast : return bitc::CAST_BITCAST;
55 }
56}
57
58static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
59 switch (Opcode) {
60 default: assert(0 && "Unknown binary instruction!");
61 case Instruction::Add: return bitc::BINOP_ADD;
62 case Instruction::Sub: return bitc::BINOP_SUB;
63 case Instruction::Mul: return bitc::BINOP_MUL;
64 case Instruction::UDiv: return bitc::BINOP_UDIV;
65 case Instruction::FDiv:
66 case Instruction::SDiv: return bitc::BINOP_SDIV;
67 case Instruction::URem: return bitc::BINOP_UREM;
68 case Instruction::FRem:
69 case Instruction::SRem: return bitc::BINOP_SREM;
70 case Instruction::Shl: return bitc::BINOP_SHL;
71 case Instruction::LShr: return bitc::BINOP_LSHR;
72 case Instruction::AShr: return bitc::BINOP_ASHR;
73 case Instruction::And: return bitc::BINOP_AND;
74 case Instruction::Or: return bitc::BINOP_OR;
75 case Instruction::Xor: return bitc::BINOP_XOR;
76 }
77}
78
79
80
Chris Lattnerfd57cec2007-04-22 06:24:45 +000081static void WriteStringRecord(unsigned Code, const std::string &Str,
82 unsigned AbbrevToUse, BitstreamWriter &Stream) {
83 SmallVector<unsigned, 64> Vals;
84
Chris Lattner15e6d172007-05-04 19:11:41 +000085 // Code: [strchar x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +000086 for (unsigned i = 0, e = Str.size(); i != e; ++i)
87 Vals.push_back(Str[i]);
88
89 // Emit the finished record.
90 Stream.EmitRecord(Code, Vals, AbbrevToUse);
91}
92
Chris Lattner62bbeea2007-05-04 00:44:52 +000093// Emit information about parameter attributes.
94static void WriteParamAttrTable(const ValueEnumerator &VE,
95 BitstreamWriter &Stream) {
96 const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
97 if (Attrs.empty()) return;
98
Chris Lattnerf0a65312007-05-04 02:59:04 +000099 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
100
101 SmallVector<uint64_t, 64> Record;
102 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
103 const ParamAttrsList *A = Attrs[i];
104 for (unsigned op = 0, e = A->size(); op != e; ++op) {
105 Record.push_back(A->getParamIndex(op));
Chris Lattnerf6f9cd12007-05-04 03:14:09 +0000106 Record.push_back(A->getParamAttrsAtIndex(op));
Chris Lattnerf0a65312007-05-04 02:59:04 +0000107 }
108
109 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
110 Record.clear();
111 }
Chris Lattner62bbeea2007-05-04 00:44:52 +0000112
Chris Lattnerf0a65312007-05-04 02:59:04 +0000113 Stream.ExitBlock();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000114}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000115
116/// WriteTypeTable - Write out the type table for a module.
117static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
118 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
119
120 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
121 SmallVector<uint64_t, 64> TypeVals;
122
123 // FIXME: Set up abbrevs now that we know the width of the type fields, etc.
124
125 // Emit an entry count so the reader can reserve space.
126 TypeVals.push_back(TypeList.size());
127 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
128 TypeVals.clear();
129
130 // Loop over all of the types, emitting each in turn.
131 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
132 const Type *T = TypeList[i].first;
133 int AbbrevToUse = 0;
134 unsigned Code = 0;
135
136 switch (T->getTypeID()) {
137 case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
138 default: assert(0 && "Unknown type!");
139 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
140 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
141 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
142 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
143 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
144 case Type::IntegerTyID:
145 // INTEGER: [width]
146 Code = bitc::TYPE_CODE_INTEGER;
147 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
148 break;
149 case Type::PointerTyID:
150 // POINTER: [pointee type]
151 Code = bitc::TYPE_CODE_POINTER;
152 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
153 break;
154
155 case Type::FunctionTyID: {
156 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattner9113e732007-05-04 03:41:34 +0000157 // FUNCTION: [isvararg, attrid, #pararms, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000158 Code = bitc::TYPE_CODE_FUNCTION;
159 TypeVals.push_back(FT->isVarArg());
Chris Lattner9113e732007-05-04 03:41:34 +0000160 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000161 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000162 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
163 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
164 break;
165 }
166 case Type::StructTyID: {
167 const StructType *ST = cast<StructType>(T);
168 // STRUCT: [ispacked, #elts, eltty x N]
169 Code = bitc::TYPE_CODE_STRUCT;
170 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000171 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000172 for (StructType::element_iterator I = ST->element_begin(),
173 E = ST->element_end(); I != E; ++I)
174 TypeVals.push_back(VE.getTypeID(*I));
175 break;
176 }
177 case Type::ArrayTyID: {
178 const ArrayType *AT = cast<ArrayType>(T);
179 // ARRAY: [numelts, eltty]
180 Code = bitc::TYPE_CODE_ARRAY;
181 TypeVals.push_back(AT->getNumElements());
182 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
183 break;
184 }
185 case Type::VectorTyID: {
186 const VectorType *VT = cast<VectorType>(T);
187 // VECTOR [numelts, eltty]
188 Code = bitc::TYPE_CODE_VECTOR;
189 TypeVals.push_back(VT->getNumElements());
190 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
191 break;
192 }
193 }
194
195 // Emit the finished record.
196 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
197 TypeVals.clear();
198 }
199
200 Stream.ExitBlock();
201}
202
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000203static unsigned getEncodedLinkage(const GlobalValue *GV) {
204 switch (GV->getLinkage()) {
205 default: assert(0 && "Invalid linkage!");
206 case GlobalValue::ExternalLinkage: return 0;
207 case GlobalValue::WeakLinkage: return 1;
208 case GlobalValue::AppendingLinkage: return 2;
209 case GlobalValue::InternalLinkage: return 3;
210 case GlobalValue::LinkOnceLinkage: return 4;
211 case GlobalValue::DLLImportLinkage: return 5;
212 case GlobalValue::DLLExportLinkage: return 6;
213 case GlobalValue::ExternalWeakLinkage: return 7;
214 }
215}
216
217static unsigned getEncodedVisibility(const GlobalValue *GV) {
218 switch (GV->getVisibility()) {
219 default: assert(0 && "Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000220 case GlobalValue::DefaultVisibility: return 0;
221 case GlobalValue::HiddenVisibility: return 1;
222 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000223 }
224}
225
226// Emit top-level description of module, including target triple, inline asm,
227// descriptors for global variables, and function prototype info.
228static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
229 BitstreamWriter &Stream) {
230 // Emit the list of dependent libraries for the Module.
231 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
232 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
233
234 // Emit various pieces of data attached to a module.
235 if (!M->getTargetTriple().empty())
236 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
237 0/*TODO*/, Stream);
238 if (!M->getDataLayout().empty())
239 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
240 0/*TODO*/, Stream);
241 if (!M->getModuleInlineAsm().empty())
242 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
243 0/*TODO*/, Stream);
244
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000245 // Emit information about sections, computing how many there are. Also
246 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000247 std::map<std::string, unsigned> SectionMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000248 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000249 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000250 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
251 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000252 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000253 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000254
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000255 if (!GV->hasSection()) continue;
256 // Give section names unique ID's.
257 unsigned &Entry = SectionMap[GV->getSection()];
258 if (Entry != 0) continue;
259 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
260 0/*TODO*/, Stream);
261 Entry = SectionMap.size();
262 }
263 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000264 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000265 if (!F->hasSection()) continue;
266 // Give section names unique ID's.
267 unsigned &Entry = SectionMap[F->getSection()];
268 if (Entry != 0) continue;
269 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
270 0/*TODO*/, Stream);
271 Entry = SectionMap.size();
272 }
273
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000274 // Emit abbrev for globals, now that we know # sections and max alignment.
275 unsigned SimpleGVarAbbrev = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000276 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000277 // Add an abbrev for common globals with no visibility or thread localness.
278 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
279 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000280 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000281 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000282 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000283 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000284 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
285 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000286 Abbv->Add(BitCodeAbbrevOp(0));
287 else {
288 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000289 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000290 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000291 }
292 if (SectionMap.empty()) // Section.
293 Abbv->Add(BitCodeAbbrevOp(0));
294 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000295 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000296 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000297 // Don't bother emitting vis + thread local.
298 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
299 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000300
301 // Emit the global variable information.
302 SmallVector<unsigned, 64> Vals;
303 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
304 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000305 unsigned AbbrevToUse = 0;
306
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000307 // GLOBALVAR: [type, isconst, initid,
308 // linkage, alignment, section, visibility, threadlocal]
309 Vals.push_back(VE.getTypeID(GV->getType()));
310 Vals.push_back(GV->isConstant());
311 Vals.push_back(GV->isDeclaration() ? 0 :
312 (VE.getValueID(GV->getInitializer()) + 1));
313 Vals.push_back(getEncodedLinkage(GV));
314 Vals.push_back(Log2_32(GV->getAlignment())+1);
315 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000316 if (GV->isThreadLocal() ||
317 GV->getVisibility() != GlobalValue::DefaultVisibility) {
318 Vals.push_back(getEncodedVisibility(GV));
319 Vals.push_back(GV->isThreadLocal());
320 } else {
321 AbbrevToUse = SimpleGVarAbbrev;
322 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000323
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000324 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
325 Vals.clear();
326 }
327
328 // Emit the function proto information.
329 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
330 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
331 // visibility]
332 Vals.push_back(VE.getTypeID(F->getType()));
333 Vals.push_back(F->getCallingConv());
334 Vals.push_back(F->isDeclaration());
335 Vals.push_back(getEncodedLinkage(F));
336 Vals.push_back(Log2_32(F->getAlignment())+1);
337 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
338 Vals.push_back(getEncodedVisibility(F));
339
340 unsigned AbbrevToUse = 0;
341 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
342 Vals.clear();
343 }
Chris Lattner07d98b42007-04-26 02:46:40 +0000344
345
346 // Emit the alias information.
347 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
348 AI != E; ++AI) {
349 Vals.push_back(VE.getTypeID(AI->getType()));
350 Vals.push_back(VE.getValueID(AI->getAliasee()));
351 Vals.push_back(getEncodedLinkage(AI));
352 unsigned AbbrevToUse = 0;
353 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
354 Vals.clear();
355 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000356}
357
358
Chris Lattner2edd22b2007-04-24 00:16:04 +0000359static void WriteConstants(unsigned FirstVal, unsigned LastVal,
360 const ValueEnumerator &VE,
361 BitstreamWriter &Stream) {
362 if (FirstVal == LastVal) return;
363
364 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 2);
Chris Lattnerb992be12007-04-23 20:35:01 +0000365
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000366 // FIXME: Install and use abbrevs to reduce size. Install them globally so
367 // they don't need to be reemitted for each function body.
Chris Lattner2edd22b2007-04-24 00:16:04 +0000368
369 SmallVector<uint64_t, 64> Record;
370
371 const ValueEnumerator::ValueList &Vals = VE.getValues();
372 const Type *LastTy = 0;
373 for (unsigned i = FirstVal; i != LastVal; ++i) {
374 const Value *V = Vals[i].first;
375 // If we need to switch types, do so now.
376 if (V->getType() != LastTy) {
377 LastTy = V->getType();
378 Record.push_back(VE.getTypeID(LastTy));
379 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record);
380 Record.clear();
381 }
382
383 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
384 assert(0 && IA && "FIXME: Inline asm writing unimp!");
385 continue;
386 }
387 const Constant *C = cast<Constant>(V);
388 unsigned Code = -1U;
389 unsigned AbbrevToUse = 0;
390 if (C->isNullValue()) {
391 Code = bitc::CST_CODE_NULL;
392 } else if (isa<UndefValue>(C)) {
393 Code = bitc::CST_CODE_UNDEF;
394 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
395 if (IV->getBitWidth() <= 64) {
396 int64_t V = IV->getSExtValue();
397 if (V >= 0)
398 Record.push_back(V << 1);
399 else
400 Record.push_back((-V << 1) | 1);
401 Code = bitc::CST_CODE_INTEGER;
402 } else { // Wide integers, > 64 bits in size.
403 // We have an arbitrary precision integer value to write whose
404 // bit width is > 64. However, in canonical unsigned integer
405 // format it is likely that the high bits are going to be zero.
406 // So, we only write the number of active words.
407 unsigned NWords = IV->getValue().getActiveWords();
408 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000409 for (unsigned i = 0; i != NWords; ++i) {
410 int64_t V = RawWords[i];
411 if (V >= 0)
412 Record.push_back(V << 1);
413 else
414 Record.push_back((-V << 1) | 1);
415 }
416 Code = bitc::CST_CODE_WIDE_INTEGER;
417 }
418 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
419 Code = bitc::CST_CODE_FLOAT;
420 if (CFP->getType() == Type::FloatTy) {
421 Record.push_back(FloatToBits((float)CFP->getValue()));
422 } else {
423 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
424 Record.push_back(DoubleToBits((double)CFP->getValue()));
425 }
426 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
427 isa<ConstantVector>(V)) {
428 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000429 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
430 Record.push_back(VE.getValueID(C->getOperand(i)));
431 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000432 switch (CE->getOpcode()) {
433 default:
434 if (Instruction::isCast(CE->getOpcode())) {
435 Code = bitc::CST_CODE_CE_CAST;
436 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
437 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
438 Record.push_back(VE.getValueID(C->getOperand(0)));
439 } else {
440 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
441 Code = bitc::CST_CODE_CE_BINOP;
442 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
443 Record.push_back(VE.getValueID(C->getOperand(0)));
444 Record.push_back(VE.getValueID(C->getOperand(1)));
445 }
446 break;
447 case Instruction::GetElementPtr:
448 Code = bitc::CST_CODE_CE_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000449 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
450 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
451 Record.push_back(VE.getValueID(C->getOperand(i)));
452 }
453 break;
454 case Instruction::Select:
455 Code = bitc::CST_CODE_CE_SELECT;
456 Record.push_back(VE.getValueID(C->getOperand(0)));
457 Record.push_back(VE.getValueID(C->getOperand(1)));
458 Record.push_back(VE.getValueID(C->getOperand(2)));
459 break;
460 case Instruction::ExtractElement:
461 Code = bitc::CST_CODE_CE_EXTRACTELT;
462 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
463 Record.push_back(VE.getValueID(C->getOperand(0)));
464 Record.push_back(VE.getValueID(C->getOperand(1)));
465 break;
466 case Instruction::InsertElement:
467 Code = bitc::CST_CODE_CE_INSERTELT;
468 Record.push_back(VE.getValueID(C->getOperand(0)));
469 Record.push_back(VE.getValueID(C->getOperand(1)));
470 Record.push_back(VE.getValueID(C->getOperand(2)));
471 break;
472 case Instruction::ShuffleVector:
473 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
474 Record.push_back(VE.getValueID(C->getOperand(0)));
475 Record.push_back(VE.getValueID(C->getOperand(1)));
476 Record.push_back(VE.getValueID(C->getOperand(2)));
477 break;
478 case Instruction::ICmp:
479 case Instruction::FCmp:
480 Code = bitc::CST_CODE_CE_CMP;
481 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
482 Record.push_back(VE.getValueID(C->getOperand(0)));
483 Record.push_back(VE.getValueID(C->getOperand(1)));
484 Record.push_back(CE->getPredicate());
485 break;
486 }
Chris Lattner2edd22b2007-04-24 00:16:04 +0000487 } else {
488 assert(0 && "Unknown constant!");
489 }
490 Stream.EmitRecord(Code, Record, AbbrevToUse);
491 Record.clear();
492 }
493
494 Stream.ExitBlock();
495}
496
497static void WriteModuleConstants(const ValueEnumerator &VE,
498 BitstreamWriter &Stream) {
499 const ValueEnumerator::ValueList &Vals = VE.getValues();
500
501 // Find the first constant to emit, which is the first non-globalvalue value.
502 // We know globalvalues have been emitted by WriteModuleInfo.
503 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
504 if (!isa<GlobalValue>(Vals[i].first)) {
505 WriteConstants(i, Vals.size(), VE, Stream);
506 return;
507 }
508 }
509}
Chris Lattnerb992be12007-04-23 20:35:01 +0000510
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000511/// WriteInstruction - Emit an instruction to the specified stream.
512static void WriteInstruction(const Instruction &I, ValueEnumerator &VE,
513 BitstreamWriter &Stream,
514 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000515 unsigned Code = 0;
516 unsigned AbbrevToUse = 0;
517 switch (I.getOpcode()) {
518 default:
519 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +0000520 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000521 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
522 Vals.push_back(VE.getTypeID(I.getType()));
523 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
524 Vals.push_back(VE.getValueID(I.getOperand(0)));
525 } else {
526 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +0000527 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000528 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
529 Vals.push_back(VE.getTypeID(I.getType()));
530 Vals.push_back(VE.getValueID(I.getOperand(0)));
531 Vals.push_back(VE.getValueID(I.getOperand(1)));
532 }
533 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000534
535 case Instruction::GetElementPtr:
536 Code = bitc::FUNC_CODE_INST_GEP;
Chris Lattnerd309c752007-05-01 02:13:26 +0000537 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
538 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
539 Vals.push_back(VE.getValueID(I.getOperand(i)));
540 }
541 break;
542 case Instruction::Select:
543 Code = bitc::FUNC_CODE_INST_SELECT;
544 Vals.push_back(VE.getTypeID(I.getType()));
545 Vals.push_back(VE.getValueID(I.getOperand(0)));
546 Vals.push_back(VE.getValueID(I.getOperand(1)));
547 Vals.push_back(VE.getValueID(I.getOperand(2)));
548 break;
549 case Instruction::ExtractElement:
550 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
551 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
552 Vals.push_back(VE.getValueID(I.getOperand(0)));
553 Vals.push_back(VE.getValueID(I.getOperand(1)));
554 break;
555 case Instruction::InsertElement:
556 Code = bitc::FUNC_CODE_INST_INSERTELT;
557 Vals.push_back(VE.getTypeID(I.getType()));
558 Vals.push_back(VE.getValueID(I.getOperand(0)));
559 Vals.push_back(VE.getValueID(I.getOperand(1)));
560 Vals.push_back(VE.getValueID(I.getOperand(2)));
561 break;
562 case Instruction::ShuffleVector:
563 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
564 Vals.push_back(VE.getTypeID(I.getType()));
565 Vals.push_back(VE.getValueID(I.getOperand(0)));
566 Vals.push_back(VE.getValueID(I.getOperand(1)));
567 Vals.push_back(VE.getValueID(I.getOperand(2)));
568 break;
569 case Instruction::ICmp:
570 case Instruction::FCmp:
571 Code = bitc::FUNC_CODE_INST_CMP;
572 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
573 Vals.push_back(VE.getValueID(I.getOperand(0)));
574 Vals.push_back(VE.getValueID(I.getOperand(1)));
575 Vals.push_back(cast<CmpInst>(I).getPredicate());
576 break;
577
578 case Instruction::Ret:
579 Code = bitc::FUNC_CODE_INST_RET;
580 if (I.getNumOperands()) {
581 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
582 Vals.push_back(VE.getValueID(I.getOperand(0)));
583 }
584 break;
585 case Instruction::Br:
586 Code = bitc::FUNC_CODE_INST_BR;
587 Vals.push_back(VE.getValueID(I.getOperand(0)));
588 if (cast<BranchInst>(I).isConditional()) {
589 Vals.push_back(VE.getValueID(I.getOperand(1)));
590 Vals.push_back(VE.getValueID(I.getOperand(2)));
591 }
592 break;
593 case Instruction::Switch:
594 Code = bitc::FUNC_CODE_INST_SWITCH;
595 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000596 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
597 Vals.push_back(VE.getValueID(I.getOperand(i)));
598 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000599 case Instruction::Invoke: {
Chris Lattnerd309c752007-05-01 02:13:26 +0000600 Code = bitc::FUNC_CODE_INST_INVOKE;
Chris Lattner76520192007-05-03 22:34:03 +0000601 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
Chris Lattnerd309c752007-05-01 02:13:26 +0000602 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
603 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
604 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal
605 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000606
Chris Lattnerd309c752007-05-01 02:13:26 +0000607 // Emit value #'s for the fixed parameters.
608 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
609 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
610 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
611 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
612
613 // Emit type/value pairs for varargs params.
614 if (FTy->isVarArg()) {
615 unsigned NumVarargs = I.getNumOperands()-3-FTy->getNumParams();
Chris Lattnerd309c752007-05-01 02:13:26 +0000616 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
617 i != e; ++i) {
618 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
619 Vals.push_back(VE.getValueID(I.getOperand(i)));
620 }
621 }
622 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000623 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000624 case Instruction::Unwind:
625 Code = bitc::FUNC_CODE_INST_UNWIND;
626 break;
627 case Instruction::Unreachable:
628 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
629 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000630
631 case Instruction::PHI:
632 Code = bitc::FUNC_CODE_INST_PHI;
633 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000634 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
635 Vals.push_back(VE.getValueID(I.getOperand(i)));
636 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000637
Chris Lattnerd309c752007-05-01 02:13:26 +0000638 case Instruction::Malloc:
639 Code = bitc::FUNC_CODE_INST_MALLOC;
640 Vals.push_back(VE.getTypeID(I.getType()));
641 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
642 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
643 break;
644
645 case Instruction::Free:
646 Code = bitc::FUNC_CODE_INST_FREE;
647 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
648 Vals.push_back(VE.getValueID(I.getOperand(0)));
649 break;
650
651 case Instruction::Alloca:
652 Code = bitc::FUNC_CODE_INST_ALLOCA;
653 Vals.push_back(VE.getTypeID(I.getType()));
654 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
655 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
656 break;
657
658 case Instruction::Load:
659 Code = bitc::FUNC_CODE_INST_LOAD;
660 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
661 Vals.push_back(VE.getValueID(I.getOperand(0))); // ptr.
662 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
663 Vals.push_back(cast<LoadInst>(I).isVolatile());
664 break;
665 case Instruction::Store:
666 Code = bitc::FUNC_CODE_INST_STORE;
667 Vals.push_back(VE.getTypeID(I.getOperand(1)->getType())); // Pointer
668 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
669 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
670 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
671 Vals.push_back(cast<StoreInst>(I).isVolatile());
672 break;
673 case Instruction::Call: {
674 Code = bitc::FUNC_CODE_INST_CALL;
Chris Lattner76520192007-05-03 22:34:03 +0000675 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
676 cast<CallInst>(I).isTailCall());
Chris Lattnerd309c752007-05-01 02:13:26 +0000677 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
678 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
679
680 // Emit value #'s for the fixed parameters.
681 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
682 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
683 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
684 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
685
Chris Lattner60ce9b52007-05-01 07:03:37 +0000686 // Emit type/value pairs for varargs params.
687 if (FTy->isVarArg()) {
688 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
Chris Lattner60ce9b52007-05-01 07:03:37 +0000689 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
690 i != e; ++i) {
691 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
692 Vals.push_back(VE.getValueID(I.getOperand(i)));
Chris Lattnerd309c752007-05-01 02:13:26 +0000693 }
694 }
695 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000696 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000697 case Instruction::VAArg:
698 Code = bitc::FUNC_CODE_INST_VAARG;
699 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
700 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
701 Vals.push_back(VE.getTypeID(I.getType())); // restype.
702 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000703 }
704
705 Stream.EmitRecord(Code, Vals, AbbrevToUse);
706 Vals.clear();
707}
708
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000709// Emit names for globals/functions etc.
710static void WriteValueSymbolTable(const ValueSymbolTable &VST,
711 const ValueEnumerator &VE,
712 BitstreamWriter &Stream) {
713 if (VST.empty()) return;
714 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 3);
Chris Lattner2e7899d2007-05-04 20:34:50 +0000715
Chris Lattner59698302007-05-04 20:52:02 +0000716 { // 8-bit fixed width VST_ENTRY strings.
717 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
718 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
719 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
720 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
721 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
722 if (Stream.EmitAbbrev(Abbv) != VST_ENTRY_8_ABBREV)
723 assert(0 && "Unexpected abbrev ordering!");
724 }
725
726 { // 7-bit fixed width VST_ENTRY strings.
727 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
728 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
729 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
730 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
732 if (Stream.EmitAbbrev(Abbv) != VST_ENTRY_7_ABBREV)
733 assert(0 && "Unexpected abbrev ordering!");
734 }
Chris Lattner15e6d172007-05-04 19:11:41 +0000735
736
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000737 // FIXME: Set up the abbrev, we know how many values there are!
738 // FIXME: We know if the type names can use 7-bit ascii.
739 SmallVector<unsigned, 64> NameVals;
740
741 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
742 SI != SE; ++SI) {
Chris Lattner59698302007-05-04 20:52:02 +0000743
744 const ValueName &Name = *SI;
745
746 // Figure out the encoding to use for the name.
747 bool is7Bit = true;
748 for (unsigned i = 0, e = Name.getKeyLength(); i != e; ++i)
749 if ((unsigned char)Name.getKeyData()[i] & 128) {
750 is7Bit = false;
751 break;
752 }
753
754
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000755 unsigned AbbrevToUse = 0;
756
Chris Lattnere825ed52007-05-03 22:18:21 +0000757 // VST_ENTRY: [valueid, namelen, namechar x N]
758 // VST_BBENTRY: [bbid, namelen, namechar x N]
759 unsigned Code;
760 if (isa<BasicBlock>(SI->getValue())) {
761 Code = bitc::VST_CODE_BBENTRY;
762 } else {
763 Code = bitc::VST_CODE_ENTRY;
Chris Lattner59698302007-05-04 20:52:02 +0000764 AbbrevToUse = is7Bit ? VST_ENTRY_7_ABBREV : VST_ENTRY_8_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000765 }
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000766
Chris Lattnere825ed52007-05-03 22:18:21 +0000767 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +0000768 for (const char *P = Name.getKeyData(),
769 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000770 NameVals.push_back((unsigned char)*P);
771
772 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +0000773 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000774 NameVals.clear();
775 }
776 Stream.ExitBlock();
777}
778
Chris Lattner8d35c792007-04-26 03:50:57 +0000779/// WriteFunction - Emit a function body to the module stream.
Chris Lattner198f34a2007-04-26 03:27:58 +0000780static void WriteFunction(const Function &F, ValueEnumerator &VE,
781 BitstreamWriter &Stream) {
Chris Lattner01b27452007-04-29 05:49:09 +0000782 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3);
Chris Lattner8d35c792007-04-26 03:50:57 +0000783 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000784
785 SmallVector<unsigned, 64> Vals;
786
787 // Emit the number of basic blocks, so the reader can create them ahead of
788 // time.
789 Vals.push_back(VE.getBasicBlocks().size());
790 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
791 Vals.clear();
792
793 // FIXME: Function attributes?
794
795 // If there are function-local constants, emit them now.
796 unsigned CstStart, CstEnd;
797 VE.getFunctionConstantRange(CstStart, CstEnd);
798 WriteConstants(CstStart, CstEnd, VE, Stream);
799
800 // Finally, emit all the instructions, in order.
801 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
802 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
803 WriteInstruction(*I, VE, Stream, Vals);
Chris Lattner198f34a2007-04-26 03:27:58 +0000804
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000805 // Emit names for all the instructions etc.
806 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
807
Chris Lattner8d35c792007-04-26 03:50:57 +0000808 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000809 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +0000810}
811
812/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
813static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
814 const ValueEnumerator &VE,
815 BitstreamWriter &Stream) {
816 if (TST.empty()) return;
817
818 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
819
820 // FIXME: Set up the abbrev, we know how many types there are!
821 // FIXME: We know if the type names can use 7-bit ascii.
822
823 SmallVector<unsigned, 64> NameVals;
824
825 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
826 TI != TE; ++TI) {
827 unsigned AbbrevToUse = 0;
828
829 // TST_ENTRY: [typeid, namelen, namechar x N]
830 NameVals.push_back(VE.getTypeID(TI->second));
831
832 const std::string &Str = TI->first;
Chris Lattner198f34a2007-04-26 03:27:58 +0000833 for (unsigned i = 0, e = Str.size(); i != e; ++i)
834 NameVals.push_back(Str[i]);
835
836 // Emit the finished record.
837 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, AbbrevToUse);
838 NameVals.clear();
839 }
840
841 Stream.ExitBlock();
842}
843
Chris Lattner198f34a2007-04-26 03:27:58 +0000844
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000845/// WriteModule - Emit the specified module to the bitstream.
846static void WriteModule(const Module *M, BitstreamWriter &Stream) {
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000847 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000848
849 // Emit the version number if it is non-zero.
850 if (CurVersion) {
Chris Lattner198f34a2007-04-26 03:27:58 +0000851 SmallVector<unsigned, 1> Vals;
852 Vals.push_back(CurVersion);
853 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000854 }
855
856 // Analyze the module, enumerating globals, functions, etc.
857 ValueEnumerator VE(M);
858
Chris Lattner62bbeea2007-05-04 00:44:52 +0000859 // Emit information about parameter attributes.
860 WriteParamAttrTable(VE, Stream);
861
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000862 // Emit information describing all of the types in the module.
863 WriteTypeTable(VE, Stream);
864
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000865 // Emit top-level description of module, including target triple, inline asm,
866 // descriptors for global variables, and function prototype info.
867 WriteModuleInfo(M, VE, Stream);
868
Chris Lattner2edd22b2007-04-24 00:16:04 +0000869 // Emit constants.
870 WriteModuleConstants(VE, Stream);
871
Chris Lattner51d5f292007-04-26 03:32:43 +0000872 // If we have any aggregate values in the value table, purge them - these can
873 // only be used to initialize global variables. Doing so makes the value
874 // namespace smaller for code in functions.
Chris Lattner198f34a2007-04-26 03:27:58 +0000875 int NumNonAggregates = VE.PurgeAggregateValues();
876 if (NumNonAggregates != -1) {
877 SmallVector<unsigned, 1> Vals;
878 Vals.push_back(NumNonAggregates);
879 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
880 }
881
882 // Emit function bodies.
883 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner51d5f292007-04-26 03:32:43 +0000884 if (!I->isDeclaration())
885 WriteFunction(*I, VE, Stream);
Chris Lattner198f34a2007-04-26 03:27:58 +0000886
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000887 // Emit the type symbol table information.
888 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
Chris Lattnerb992be12007-04-23 20:35:01 +0000889
890 // Emit names for globals/functions etc.
891 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
892
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000893 Stream.ExitBlock();
894}
895
Chris Lattner07faafc2007-05-04 18:26:27 +0000896// Emit blockinfo, which defines the standard abbreviations etc.
897static void WriteBlockInfo(BitstreamWriter &Stream) {
898 // We only want to emit block info records for blocks that have multiple
899 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
900 // blocks can defined their abbrevs inline.
901 Stream.EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, 2);
902
903#if 0
904 // Configure TYPE_SYMTAB_BLOCK's.
905
906 // Add an abbrev for VST_ENTRY where the characters each fit in 7 bits.
907 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
908 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
909 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8); // Value ID
Chris Lattner2e7899d2007-05-04 20:34:50 +0000910 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
Chris Lattner07faafc2007-05-04 18:26:27 +0000911
912 xxx = Stream.EmitAbbrev(Abbv);
913#endif
914 Stream.ExitBlock();
915}
916
917
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000918/// WriteBitcodeToFile - Write the specified module to the specified output
919/// stream.
920void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
921 std::vector<unsigned char> Buffer;
922 BitstreamWriter Stream(Buffer);
923
924 Buffer.reserve(256*1024);
925
926 // Emit the file header.
927 Stream.Emit((unsigned)'B', 8);
928 Stream.Emit((unsigned)'C', 8);
929 Stream.Emit(0x0, 4);
930 Stream.Emit(0xC, 4);
931 Stream.Emit(0xE, 4);
932 Stream.Emit(0xD, 4);
933
Chris Lattner07faafc2007-05-04 18:26:27 +0000934 // Emit blockinfo, which defines the standard abbreviations etc.
935 WriteBlockInfo(Stream);
936
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000937 // Emit the module.
938 WriteModule(M, Stream);
939
940 // Write the generated bitstream to "Out".
941 Out.write((char*)&Buffer.front(), Buffer.size());
942}