1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Bitcode writer implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/Bitcode/BitstreamWriter.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "ValueEnumerator.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/ParameterAttributes.h"
23 #include "llvm/TypeSymbolTable.h"
24 #include "llvm/ValueSymbolTable.h"
25 #include "llvm/Support/MathExtras.h"
28 /// 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.
33 // VALUE_SYMTAB_BLOCK abbrev id's.
34 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
39 // CONSTANTS_BLOCK abbrev id's.
40 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
41 CONSTANTS_INTEGER_ABBREV,
42 CONSTANTS_CE_CAST_Abbrev,
43 CONSTANTS_NULL_Abbrev,
45 // FUNCTION_BLOCK abbrev id's.
46 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
53 static unsigned GetEncodedCastOpcode(unsigned 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;
71 static unsigned GetEncodedBinaryOpcode(unsigned 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;
94 static void WriteStringRecord(unsigned Code, const std::string &Str,
95 unsigned AbbrevToUse, BitstreamWriter &Stream) {
96 SmallVector<unsigned, 64> Vals;
98 // Code: [strchar x N]
99 for (unsigned i = 0, e = Str.size(); i != e; ++i)
100 Vals.push_back(Str[i]);
102 // Emit the finished record.
103 Stream.EmitRecord(Code, Vals, AbbrevToUse);
106 // Emit information about parameter attributes.
107 static void WriteParamAttrTable(const ValueEnumerator &VE,
108 BitstreamWriter &Stream) {
109 const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
110 if (Attrs.empty()) return;
112 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
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));
119 Record.push_back(A->getParamAttrsAtIndex(op));
122 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
129 /// WriteTypeTable - Write out the type table for a module.
130 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
131 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
133 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
134 SmallVector<uint64_t, 64> TypeVals;
136 // 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);
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);
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);
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);
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);
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;
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:
192 Code = bitc::TYPE_CODE_INTEGER;
193 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
195 case Type::PointerTyID:
196 // POINTER: [pointee type]
197 Code = bitc::TYPE_CODE_POINTER;
198 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
199 AbbrevToUse = PtrAbbrev;
202 case Type::FunctionTyID: {
203 const FunctionType *FT = cast<FunctionType>(T);
204 // FUNCTION: [isvararg, attrid, retty, paramty x N]
205 Code = bitc::TYPE_CODE_FUNCTION;
206 TypeVals.push_back(FT->isVarArg());
207 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
208 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
209 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
210 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
211 AbbrevToUse = FunctionAbbrev;
214 case Type::StructTyID: {
215 const StructType *ST = cast<StructType>(T);
216 // STRUCT: [ispacked, eltty x N]
217 Code = bitc::TYPE_CODE_STRUCT;
218 TypeVals.push_back(ST->isPacked());
219 // Output all of the element types.
220 for (StructType::element_iterator I = ST->element_begin(),
221 E = ST->element_end(); I != E; ++I)
222 TypeVals.push_back(VE.getTypeID(*I));
223 AbbrevToUse = StructAbbrev;
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()));
232 AbbrevToUse = ArrayAbbrev;
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()));
245 // Emit the finished record.
246 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
253 static 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;
267 static unsigned getEncodedVisibility(const GlobalValue *GV) {
268 switch (GV->getVisibility()) {
269 default: assert(0 && "Invalid visibility!");
270 case GlobalValue::DefaultVisibility: return 0;
271 case GlobalValue::HiddenVisibility: return 1;
272 case GlobalValue::ProtectedVisibility: return 2;
276 // Emit top-level description of module, including target triple, inline asm,
277 // descriptors for global variables, and function prototype info.
278 static 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);
284 // Emit various pieces of data attached to a module.
285 if (!M->getTargetTriple().empty())
286 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
288 if (!M->getDataLayout().empty())
289 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
291 if (!M->getModuleInlineAsm().empty())
292 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
295 // Emit information about sections, computing how many there are. Also
296 // compute the maximum alignment value.
297 std::map<std::string, unsigned> SectionMap;
298 unsigned MaxAlignment = 0;
299 unsigned MaxGlobalType = 0;
300 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
302 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
303 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
305 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(),
311 Entry = SectionMap.size();
313 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
314 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
315 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(),
321 Entry = SectionMap.size();
324 // Emit abbrev for globals, now that we know # sections and max alignment.
325 unsigned SimpleGVarAbbrev = 0;
326 if (!M->global_empty()) {
327 // 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));
330 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
331 Log2_32_Ceil(MaxGlobalType+1)));
332 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
333 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
334 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
335 if (MaxAlignment == 0) // Alignment.
336 Abbv->Add(BitCodeAbbrevOp(0));
338 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
340 Log2_32_Ceil(MaxEncAlignment+1)));
342 if (SectionMap.empty()) // Section.
343 Abbv->Add(BitCodeAbbrevOp(0));
345 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
346 Log2_32_Ceil(SectionMap.size()+1)));
347 // Don't bother emitting vis + thread local.
348 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
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();
355 unsigned AbbrevToUse = 0;
357 // 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);
366 if (GV->isThreadLocal() ||
367 GV->getVisibility() != GlobalValue::DefaultVisibility) {
368 Vals.push_back(getEncodedVisibility(GV));
369 Vals.push_back(GV->isThreadLocal());
371 AbbrevToUse = SimpleGVarAbbrev;
374 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
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,
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));
390 unsigned AbbrevToUse = 0;
391 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
396 // Emit the alias information.
397 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
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);
409 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
410 const ValueEnumerator &VE,
411 BitstreamWriter &Stream, bool isGlobal) {
412 if (FirstVal == LastVal) return;
414 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
416 unsigned AggregateAbbrev = 0;
417 unsigned String8Abbrev = 0;
418 unsigned CString7Abbrev = 0;
419 unsigned CString6Abbrev = 0;
420 // If this is a constant pool for the module, emit module-specific abbrevs.
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);
429 // Abbrev for CST_CODE_STRING.
430 Abbv = new BitCodeAbbrev();
431 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
433 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));
439 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
440 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);
449 // FIXME: Install and use abbrevs to reduce size. Install them globally so
450 // they don't need to be reemitted for each function body.
452 SmallVector<uint64_t, 64> Record;
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));
462 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
463 CONSTANTS_SETTYPE_ABBREV);
467 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
468 assert(0 && IA && "FIXME: Inline asm writing unimp!");
471 const Constant *C = cast<Constant>(V);
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();
482 Record.push_back(V << 1);
484 Record.push_back((-V << 1) | 1);
485 Code = bitc::CST_CODE_INTEGER;
486 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
487 } 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();
494 for (unsigned i = 0; i != NWords; ++i) {
495 int64_t V = RawWords[i];
497 Record.push_back(V << 1);
499 Record.push_back((-V << 1) | 1);
501 Code = bitc::CST_CODE_WIDE_INTEGER;
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()));
508 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
509 Record.push_back(DoubleToBits((double)CFP->getValue()));
511 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
512 // Emit constant strings specially.
513 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.
519 Code = bitc::CST_CODE_STRING;
520 AbbrevToUse = String8Abbrev;
522 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
523 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
524 for (unsigned i = 0; i != NumOps; ++i) {
525 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
527 isCStr7 &= (V & 128) == 0;
529 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
533 AbbrevToUse = CString6Abbrev;
535 AbbrevToUse = CString7Abbrev;
536 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
537 isa<ConstantVector>(V)) {
538 Code = bitc::CST_CODE_AGGREGATE;
539 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
540 Record.push_back(VE.getValueID(C->getOperand(i)));
541 AbbrevToUse = AggregateAbbrev;
542 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
543 switch (CE->getOpcode()) {
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)));
550 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
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)));
559 case Instruction::GetElementPtr:
560 Code = bitc::CST_CODE_CE_GEP;
561 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)));
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)));
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)));
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)));
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)));
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());
600 assert(0 && "Unknown constant!");
602 Stream.EmitRecord(Code, Record, AbbrevToUse);
609 static void WriteModuleConstants(const ValueEnumerator &VE,
610 BitstreamWriter &Stream) {
611 const ValueEnumerator::ValueList &Vals = VE.getValues();
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)) {
617 WriteConstants(i, Vals.size(), VE, Stream, true);
623 /// 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.
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
631 static 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()));
643 /// WriteInstruction - Emit an instruction to the specified stream.
644 static void WriteInstruction(const Instruction &I, unsigned InstID,
645 ValueEnumerator &VE, BitstreamWriter &Stream,
646 SmallVector<unsigned, 64> &Vals) {
648 unsigned AbbrevToUse = 0;
649 switch (I.getOpcode()) {
651 if (Instruction::isCast(I.getOpcode())) {
652 Code = bitc::FUNC_CODE_INST_CAST;
653 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
654 Vals.push_back(VE.getTypeID(I.getType()));
655 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
657 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
658 Code = bitc::FUNC_CODE_INST_BINOP;
659 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
660 Vals.push_back(VE.getValueID(I.getOperand(1)));
661 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
665 case Instruction::GetElementPtr:
666 Code = bitc::FUNC_CODE_INST_GEP;
667 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
668 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
670 case Instruction::Select:
671 Code = bitc::FUNC_CODE_INST_SELECT;
672 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
673 Vals.push_back(VE.getValueID(I.getOperand(2)));
674 Vals.push_back(VE.getValueID(I.getOperand(0)));
676 case Instruction::ExtractElement:
677 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
678 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
679 Vals.push_back(VE.getValueID(I.getOperand(1)));
681 case Instruction::InsertElement:
682 Code = bitc::FUNC_CODE_INST_INSERTELT;
683 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
684 Vals.push_back(VE.getValueID(I.getOperand(1)));
685 Vals.push_back(VE.getValueID(I.getOperand(2)));
687 case Instruction::ShuffleVector:
688 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
689 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
690 Vals.push_back(VE.getValueID(I.getOperand(1)));
691 Vals.push_back(VE.getValueID(I.getOperand(2)));
693 case Instruction::ICmp:
694 case Instruction::FCmp:
695 Code = bitc::FUNC_CODE_INST_CMP;
696 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
697 Vals.push_back(VE.getValueID(I.getOperand(1)));
698 Vals.push_back(cast<CmpInst>(I).getPredicate());
701 case Instruction::Ret:
702 Code = bitc::FUNC_CODE_INST_RET;
703 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;
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)));
716 case Instruction::Switch:
717 Code = bitc::FUNC_CODE_INST_SWITCH;
718 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
719 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
720 Vals.push_back(VE.getValueID(I.getOperand(i)));
722 case Instruction::Invoke: {
723 Code = bitc::FUNC_CODE_INST_INVOKE;
724 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
725 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
729 // 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.
735 // Emit type/value pairs for varargs params.
736 if (FTy->isVarArg()) {
737 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
739 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
743 case Instruction::Unwind:
744 Code = bitc::FUNC_CODE_INST_UNWIND;
746 case Instruction::Unreachable:
747 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
748 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
751 case Instruction::PHI:
752 Code = bitc::FUNC_CODE_INST_PHI;
753 Vals.push_back(VE.getTypeID(I.getType()));
754 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
755 Vals.push_back(VE.getValueID(I.getOperand(i)));
758 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);
765 case Instruction::Free:
766 Code = bitc::FUNC_CODE_INST_FREE;
767 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
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);
777 case Instruction::Load:
778 Code = bitc::FUNC_CODE_INST_LOAD;
779 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
780 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
782 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
783 Vals.push_back(cast<LoadInst>(I).isVolatile());
785 case Instruction::Store:
786 Code = bitc::FUNC_CODE_INST_STORE;
787 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // val.
788 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
789 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
790 Vals.push_back(cast<StoreInst>(I).isVolatile());
792 case Instruction::Call: {
793 Code = bitc::FUNC_CODE_INST_CALL;
794 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
795 cast<CallInst>(I).isTailCall());
796 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee
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.
804 // Emit type/value pairs for varargs params.
805 if (FTy->isVarArg()) {
806 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
807 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
809 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
813 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.
821 Stream.EmitRecord(Code, Vals, AbbrevToUse);
825 // Emit names for globals/functions etc.
826 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
827 const ValueEnumerator &VE,
828 BitstreamWriter &Stream) {
829 if (VST.empty()) return;
830 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
832 // 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;
836 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
839 const ValueName &Name = *SI;
841 // Figure out the encoding to use for the name.
844 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
847 isChar6 = BitCodeAbbrevOp::isChar6(*C);
848 if ((unsigned char)*C & 128) {
850 break; // don't bother scanning the rest.
854 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
856 // VST_ENTRY: [valueid, namechar x N]
857 // VST_BBENTRY: [bbid, namechar x N]
859 if (isa<BasicBlock>(SI->getValue())) {
860 Code = bitc::VST_CODE_BBENTRY;
862 AbbrevToUse = VST_BBENTRY_6_ABBREV;
864 Code = bitc::VST_CODE_ENTRY;
866 AbbrevToUse = VST_ENTRY_6_ABBREV;
868 AbbrevToUse = VST_ENTRY_7_ABBREV;
871 NameVals.push_back(VE.getValueID(SI->getValue()));
872 for (const char *P = Name.getKeyData(),
873 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
874 NameVals.push_back((unsigned char)*P);
876 // Emit the finished record.
877 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
883 /// WriteFunction - Emit a function body to the module stream.
884 static void WriteFunction(const Function &F, ValueEnumerator &VE,
885 BitstreamWriter &Stream) {
886 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3);
887 VE.incorporateFunction(F);
889 SmallVector<unsigned, 64> Vals;
891 // Emit the number of basic blocks, so the reader can create them ahead of
893 Vals.push_back(VE.getBasicBlocks().size());
894 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
897 // FIXME: Function attributes?
899 // If there are function-local constants, emit them now.
900 unsigned CstStart, CstEnd;
901 VE.getFunctionConstantRange(CstStart, CstEnd);
902 WriteConstants(CstStart, CstEnd, VE, Stream, false);
904 // Keep a running idea of what the instruction ID is.
905 unsigned InstID = CstEnd;
907 // Finally, emit all the instructions, in order.
908 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
909 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
911 WriteInstruction(*I, InstID, VE, Stream, Vals);
912 if (I->getType() != Type::VoidTy)
916 // Emit names for all the instructions etc.
917 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
923 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
924 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
925 const ValueEnumerator &VE,
926 BitstreamWriter &Stream) {
927 if (TST.empty()) return;
929 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
931 // 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);
940 SmallVector<unsigned, 64> NameVals;
942 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
944 // TST_ENTRY: [typeid, namechar x N]
945 NameVals.push_back(VE.getTypeID(TI->second));
947 const std::string &Str = TI->first;
949 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
950 NameVals.push_back((unsigned char)Str[i]);
955 // Emit the finished record.
956 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
963 // Emit blockinfo, which defines the standard abbreviations etc.
964 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
965 // 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.
968 Stream.EnterBlockInfoBlock(2);
970 { // 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!");
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!");
991 { // 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!");
1001 { // 6-bit char6 VST_BBENTRY strings.
1002 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));
1006 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1007 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1008 Abbv) != VST_BBENTRY_6_ABBREV)
1009 assert(0 && "Unexpected abbrev ordering!");
1014 { // 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!");
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!");
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
1041 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1042 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1043 assert(0 && "Unexpected abbrev ordering!");
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!");
1053 // FIXME: This should only use space for first class types!
1055 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1056 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1057 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1058 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!");
1065 { // 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!");
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!");
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!");
1092 /// WriteModule - Emit the specified module to the bitstream.
1093 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1094 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1096 // Emit the version number if it is non-zero.
1098 SmallVector<unsigned, 1> Vals;
1099 Vals.push_back(CurVersion);
1100 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1103 // Analyze the module, enumerating globals, functions, etc.
1104 ValueEnumerator VE(M);
1106 // Emit blockinfo, which defines the standard abbreviations etc.
1107 WriteBlockInfo(VE, Stream);
1109 // Emit information about parameter attributes.
1110 WriteParamAttrTable(VE, Stream);
1112 // Emit information describing all of the types in the module.
1113 WriteTypeTable(VE, Stream);
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);
1120 WriteModuleConstants(VE, Stream);
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);
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);
1137 // Emit the type symbol table information.
1138 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1140 // Emit names for globals/functions etc.
1141 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1147 /// WriteBitcodeToFile - Write the specified module to the specified output
1149 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1150 std::vector<unsigned char> Buffer;
1151 BitstreamWriter Stream(Buffer);
1153 Buffer.reserve(256*1024);
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);
1164 WriteModule(M, Stream);
1166 // Write the generated bitstream to "Out".
1167 Out.write((char*)&Buffer.front(), Buffer.size());