blob: c647828eeb42b99a2f11e384a699c536488db093 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Bitcode/ReaderWriter.h"
15#include "llvm/Bitcode/BitstreamWriter.h"
16#include "llvm/Bitcode/LLVMBitCodes.h"
17#include "ValueEnumerator.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/InlineAsm.h"
21#include "llvm/Instructions.h"
22#include "llvm/Module.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/TypeSymbolTable.h"
24#include "llvm/ValueSymbolTable.h"
25#include "llvm/Support/MathExtras.h"
Chris Lattner5088dae2008-08-23 21:33:24 +000026#include "llvm/Support/Streams.h"
Anton Korobeynikova460a3a2008-06-06 07:24:01 +000027#include "llvm/System/Program.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028using namespace llvm;
29
30/// These are manifest constants used by the bitcode writer. They do not need to
31/// be kept in sync with the reader, but need to be consistent within this file.
32enum {
33 CurVersion = 0,
34
35 // VALUE_SYMTAB_BLOCK abbrev id's.
36 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
37 VST_ENTRY_7_ABBREV,
38 VST_ENTRY_6_ABBREV,
39 VST_BBENTRY_6_ABBREV,
40
41 // CONSTANTS_BLOCK abbrev id's.
42 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
43 CONSTANTS_INTEGER_ABBREV,
44 CONSTANTS_CE_CAST_Abbrev,
45 CONSTANTS_NULL_Abbrev,
46
47 // FUNCTION_BLOCK abbrev id's.
48 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
49 FUNCTION_INST_BINOP_ABBREV,
50 FUNCTION_INST_CAST_ABBREV,
51 FUNCTION_INST_RET_VOID_ABBREV,
52 FUNCTION_INST_RET_VAL_ABBREV,
53 FUNCTION_INST_UNREACHABLE_ABBREV
54};
55
56
57static unsigned GetEncodedCastOpcode(unsigned Opcode) {
58 switch (Opcode) {
59 default: assert(0 && "Unknown cast instruction!");
60 case Instruction::Trunc : return bitc::CAST_TRUNC;
61 case Instruction::ZExt : return bitc::CAST_ZEXT;
62 case Instruction::SExt : return bitc::CAST_SEXT;
63 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
64 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
65 case Instruction::UIToFP : return bitc::CAST_UITOFP;
66 case Instruction::SIToFP : return bitc::CAST_SITOFP;
67 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
68 case Instruction::FPExt : return bitc::CAST_FPEXT;
69 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
70 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
71 case Instruction::BitCast : return bitc::CAST_BITCAST;
72 }
73}
74
75static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
76 switch (Opcode) {
77 default: assert(0 && "Unknown binary instruction!");
78 case Instruction::Add: return bitc::BINOP_ADD;
79 case Instruction::Sub: return bitc::BINOP_SUB;
80 case Instruction::Mul: return bitc::BINOP_MUL;
81 case Instruction::UDiv: return bitc::BINOP_UDIV;
82 case Instruction::FDiv:
83 case Instruction::SDiv: return bitc::BINOP_SDIV;
84 case Instruction::URem: return bitc::BINOP_UREM;
85 case Instruction::FRem:
86 case Instruction::SRem: return bitc::BINOP_SREM;
87 case Instruction::Shl: return bitc::BINOP_SHL;
88 case Instruction::LShr: return bitc::BINOP_LSHR;
89 case Instruction::AShr: return bitc::BINOP_ASHR;
90 case Instruction::And: return bitc::BINOP_AND;
91 case Instruction::Or: return bitc::BINOP_OR;
92 case Instruction::Xor: return bitc::BINOP_XOR;
93 }
94}
95
96
97
98static void WriteStringRecord(unsigned Code, const std::string &Str,
99 unsigned AbbrevToUse, BitstreamWriter &Stream) {
100 SmallVector<unsigned, 64> Vals;
101
102 // Code: [strchar x N]
103 for (unsigned i = 0, e = Str.size(); i != e; ++i)
104 Vals.push_back(Str[i]);
105
106 // Emit the finished record.
107 Stream.EmitRecord(Code, Vals, AbbrevToUse);
108}
109
110// Emit information about parameter attributes.
111static void WriteParamAttrTable(const ValueEnumerator &VE,
112 BitstreamWriter &Stream) {
Chris Lattner1c8733e2008-03-12 17:45:29 +0000113 const std::vector<PAListPtr> &Attrs = VE.getParamAttrs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 if (Attrs.empty()) return;
115
116 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
117
118 SmallVector<uint64_t, 64> Record;
119 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Chris Lattner1c8733e2008-03-12 17:45:29 +0000120 const PAListPtr &A = Attrs[i];
121 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
122 const ParamAttrsWithIndex &PAWI = A.getSlot(i);
123 Record.push_back(PAWI.Index);
124 Record.push_back(PAWI.Attrs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 }
126
127 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
128 Record.clear();
129 }
130
131 Stream.ExitBlock();
132}
133
134/// WriteTypeTable - Write out the type table for a module.
135static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
136 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
137
138 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
139 SmallVector<uint64_t, 64> TypeVals;
140
141 // Abbrev for TYPE_CODE_POINTER.
142 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
143 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
144 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
145 Log2_32_Ceil(VE.getTypes().size()+1)));
Christopher Lamb20a39e92007-12-12 08:44:39 +0000146 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
148
149 // Abbrev for TYPE_CODE_FUNCTION.
150 Abbv = new BitCodeAbbrev();
151 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
152 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattner70b644d2007-11-27 17:48:06 +0000153 Abbv->Add(BitCodeAbbrevOp(0)); // FIXME: DEAD value, remove in LLVM 3.0
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
155 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
156 Log2_32_Ceil(VE.getTypes().size()+1)));
157 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
158
159 // Abbrev for TYPE_CODE_STRUCT.
160 Abbv = new BitCodeAbbrev();
161 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
162 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
163 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
164 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
165 Log2_32_Ceil(VE.getTypes().size()+1)));
166 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
167
168 // Abbrev for TYPE_CODE_ARRAY.
169 Abbv = new BitCodeAbbrev();
170 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
171 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
172 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
173 Log2_32_Ceil(VE.getTypes().size()+1)));
174 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
175
176 // Emit an entry count so the reader can reserve space.
177 TypeVals.push_back(TypeList.size());
178 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
179 TypeVals.clear();
180
181 // Loop over all of the types, emitting each in turn.
182 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
183 const Type *T = TypeList[i].first;
184 int AbbrevToUse = 0;
185 unsigned Code = 0;
186
187 switch (T->getTypeID()) {
188 default: assert(0 && "Unknown type!");
189 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
190 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
191 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
Dale Johannesenf325d9f2007-08-03 01:03:46 +0000192 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
193 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
194 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
196 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
197 case Type::IntegerTyID:
198 // INTEGER: [width]
199 Code = bitc::TYPE_CODE_INTEGER;
200 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
201 break;
Duncan Sandsc7ef4d12007-12-11 12:20:47 +0000202 case Type::PointerTyID: {
Christopher Lamb44d62f62007-12-11 08:59:05 +0000203 const PointerType *PTy = cast<PointerType>(T);
Christopher Lamb20a39e92007-12-12 08:44:39 +0000204 // POINTER: [pointee type, address space]
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 Code = bitc::TYPE_CODE_POINTER;
Christopher Lamb44d62f62007-12-11 08:59:05 +0000206 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lamb20a39e92007-12-12 08:44:39 +0000207 unsigned AddressSpace = PTy->getAddressSpace();
208 TypeVals.push_back(AddressSpace);
209 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 break;
Duncan Sandsc7ef4d12007-12-11 12:20:47 +0000211 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 case Type::FunctionTyID: {
213 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattner70b644d2007-11-27 17:48:06 +0000214 // FUNCTION: [isvararg, attrid, retty, paramty x N]
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 Code = bitc::TYPE_CODE_FUNCTION;
216 TypeVals.push_back(FT->isVarArg());
Chris Lattner70b644d2007-11-27 17:48:06 +0000217 TypeVals.push_back(0); // FIXME: DEAD: remove in llvm 3.0
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
219 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
220 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
221 AbbrevToUse = FunctionAbbrev;
222 break;
223 }
224 case Type::StructTyID: {
225 const StructType *ST = cast<StructType>(T);
226 // STRUCT: [ispacked, eltty x N]
227 Code = bitc::TYPE_CODE_STRUCT;
228 TypeVals.push_back(ST->isPacked());
229 // Output all of the element types.
230 for (StructType::element_iterator I = ST->element_begin(),
231 E = ST->element_end(); I != E; ++I)
232 TypeVals.push_back(VE.getTypeID(*I));
233 AbbrevToUse = StructAbbrev;
234 break;
235 }
236 case Type::ArrayTyID: {
237 const ArrayType *AT = cast<ArrayType>(T);
238 // ARRAY: [numelts, eltty]
239 Code = bitc::TYPE_CODE_ARRAY;
240 TypeVals.push_back(AT->getNumElements());
241 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
242 AbbrevToUse = ArrayAbbrev;
243 break;
244 }
245 case Type::VectorTyID: {
246 const VectorType *VT = cast<VectorType>(T);
247 // VECTOR [numelts, eltty]
248 Code = bitc::TYPE_CODE_VECTOR;
249 TypeVals.push_back(VT->getNumElements());
250 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
251 break;
252 }
253 }
254
255 // Emit the finished record.
256 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
257 TypeVals.clear();
258 }
259
260 Stream.ExitBlock();
261}
262
263static unsigned getEncodedLinkage(const GlobalValue *GV) {
264 switch (GV->getLinkage()) {
265 default: assert(0 && "Invalid linkage!");
266 case GlobalValue::GhostLinkage: // Map ghost linkage onto external.
267 case GlobalValue::ExternalLinkage: return 0;
268 case GlobalValue::WeakLinkage: return 1;
269 case GlobalValue::AppendingLinkage: return 2;
270 case GlobalValue::InternalLinkage: return 3;
271 case GlobalValue::LinkOnceLinkage: return 4;
272 case GlobalValue::DLLImportLinkage: return 5;
273 case GlobalValue::DLLExportLinkage: return 6;
274 case GlobalValue::ExternalWeakLinkage: return 7;
Dale Johannesen49c44122008-05-14 20:12:51 +0000275 case GlobalValue::CommonLinkage: return 8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 }
277}
278
279static unsigned getEncodedVisibility(const GlobalValue *GV) {
280 switch (GV->getVisibility()) {
281 default: assert(0 && "Invalid visibility!");
282 case GlobalValue::DefaultVisibility: return 0;
283 case GlobalValue::HiddenVisibility: return 1;
284 case GlobalValue::ProtectedVisibility: return 2;
285 }
286}
287
288// Emit top-level description of module, including target triple, inline asm,
289// descriptors for global variables, and function prototype info.
290static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
291 BitstreamWriter &Stream) {
292 // Emit the list of dependent libraries for the Module.
293 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
294 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
295
296 // Emit various pieces of data attached to a module.
297 if (!M->getTargetTriple().empty())
298 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
299 0/*TODO*/, Stream);
300 if (!M->getDataLayout().empty())
301 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
302 0/*TODO*/, Stream);
303 if (!M->getModuleInlineAsm().empty())
304 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
305 0/*TODO*/, Stream);
306
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000307 // Emit information about sections and GC, computing how many there are. Also
308 // compute the maximum alignment value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 std::map<std::string, unsigned> SectionMap;
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000310 std::map<std::string, unsigned> GCMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 unsigned MaxAlignment = 0;
312 unsigned MaxGlobalType = 0;
313 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
314 GV != E; ++GV) {
315 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
316 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
317
318 if (!GV->hasSection()) continue;
319 // Give section names unique ID's.
320 unsigned &Entry = SectionMap[GV->getSection()];
321 if (Entry != 0) continue;
322 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
323 0/*TODO*/, Stream);
324 Entry = SectionMap.size();
325 }
326 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
327 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Gordon Henriksen13fe5e32007-12-10 03:18:06 +0000328 if (F->hasSection()) {
329 // Give section names unique ID's.
330 unsigned &Entry = SectionMap[F->getSection()];
331 if (!Entry) {
332 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
333 0/*TODO*/, Stream);
334 Entry = SectionMap.size();
335 }
336 }
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000337 if (F->hasGC()) {
338 // Same for GC names.
339 unsigned &Entry = GCMap[F->getGC()];
Gordon Henriksen13fe5e32007-12-10 03:18:06 +0000340 if (!Entry) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000341 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(),
Gordon Henriksen13fe5e32007-12-10 03:18:06 +0000342 0/*TODO*/, Stream);
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000343 Entry = GCMap.size();
Gordon Henriksen13fe5e32007-12-10 03:18:06 +0000344 }
345 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 }
347
348 // Emit abbrev for globals, now that we know # sections and max alignment.
349 unsigned SimpleGVarAbbrev = 0;
350 if (!M->global_empty()) {
351 // Add an abbrev for common globals with no visibility or thread localness.
352 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
353 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
354 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
355 Log2_32_Ceil(MaxGlobalType+1)));
356 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
357 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Dale Johannesend75a89e2008-05-15 20:49:28 +0000358 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 if (MaxAlignment == 0) // Alignment.
360 Abbv->Add(BitCodeAbbrevOp(0));
361 else {
362 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
363 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
364 Log2_32_Ceil(MaxEncAlignment+1)));
365 }
366 if (SectionMap.empty()) // Section.
367 Abbv->Add(BitCodeAbbrevOp(0));
368 else
369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
370 Log2_32_Ceil(SectionMap.size()+1)));
371 // Don't bother emitting vis + thread local.
372 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
373 }
374
375 // Emit the global variable information.
376 SmallVector<unsigned, 64> Vals;
377 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
378 GV != E; ++GV) {
379 unsigned AbbrevToUse = 0;
380
381 // GLOBALVAR: [type, isconst, initid,
382 // linkage, alignment, section, visibility, threadlocal]
383 Vals.push_back(VE.getTypeID(GV->getType()));
384 Vals.push_back(GV->isConstant());
385 Vals.push_back(GV->isDeclaration() ? 0 :
386 (VE.getValueID(GV->getInitializer()) + 1));
387 Vals.push_back(getEncodedLinkage(GV));
388 Vals.push_back(Log2_32(GV->getAlignment())+1);
389 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
390 if (GV->isThreadLocal() ||
391 GV->getVisibility() != GlobalValue::DefaultVisibility) {
392 Vals.push_back(getEncodedVisibility(GV));
393 Vals.push_back(GV->isThreadLocal());
394 } else {
395 AbbrevToUse = SimpleGVarAbbrev;
396 }
397
398 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
399 Vals.clear();
400 }
401
402 // Emit the function proto information.
403 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000404 // FUNCTION: [type, callingconv, isproto, paramattr,
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000405 // linkage, alignment, section, visibility, gc]
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 Vals.push_back(VE.getTypeID(F->getType()));
407 Vals.push_back(F->getCallingConv());
408 Vals.push_back(F->isDeclaration());
409 Vals.push_back(getEncodedLinkage(F));
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000410 Vals.push_back(VE.getParamAttrID(F->getParamAttrs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 Vals.push_back(Log2_32(F->getAlignment())+1);
412 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
413 Vals.push_back(getEncodedVisibility(F));
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000414 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
Devang Patelf30ee902008-09-02 21:47:13 +0000415 Vals.push_back(F->getNotes());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416
417 unsigned AbbrevToUse = 0;
418 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
419 Vals.clear();
420 }
421
422
423 // Emit the alias information.
424 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
425 AI != E; ++AI) {
426 Vals.push_back(VE.getTypeID(AI->getType()));
427 Vals.push_back(VE.getValueID(AI->getAliasee()));
428 Vals.push_back(getEncodedLinkage(AI));
Anton Korobeynikov902a0112008-03-11 21:40:17 +0000429 Vals.push_back(getEncodedVisibility(AI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 unsigned AbbrevToUse = 0;
431 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
432 Vals.clear();
433 }
434}
435
436
437static void WriteConstants(unsigned FirstVal, unsigned LastVal,
438 const ValueEnumerator &VE,
439 BitstreamWriter &Stream, bool isGlobal) {
440 if (FirstVal == LastVal) return;
441
442 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
443
444 unsigned AggregateAbbrev = 0;
445 unsigned String8Abbrev = 0;
446 unsigned CString7Abbrev = 0;
447 unsigned CString6Abbrev = 0;
448 // If this is a constant pool for the module, emit module-specific abbrevs.
449 if (isGlobal) {
450 // Abbrev for CST_CODE_AGGREGATE.
451 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
452 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
453 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
454 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
455 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
456
457 // Abbrev for CST_CODE_STRING.
458 Abbv = new BitCodeAbbrev();
459 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
460 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
461 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
462 String8Abbrev = Stream.EmitAbbrev(Abbv);
463 // Abbrev for CST_CODE_CSTRING.
464 Abbv = new BitCodeAbbrev();
465 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
466 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
467 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
468 CString7Abbrev = Stream.EmitAbbrev(Abbv);
469 // Abbrev for CST_CODE_CSTRING.
470 Abbv = new BitCodeAbbrev();
471 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
472 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
473 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
474 CString6Abbrev = Stream.EmitAbbrev(Abbv);
475 }
476
477 SmallVector<uint64_t, 64> Record;
478
479 const ValueEnumerator::ValueList &Vals = VE.getValues();
480 const Type *LastTy = 0;
481 for (unsigned i = FirstVal; i != LastVal; ++i) {
482 const Value *V = Vals[i].first;
483 // If we need to switch types, do so now.
484 if (V->getType() != LastTy) {
485 LastTy = V->getType();
486 Record.push_back(VE.getTypeID(LastTy));
487 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
488 CONSTANTS_SETTYPE_ABBREV);
489 Record.clear();
490 }
491
492 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
493 Record.push_back(unsigned(IA->hasSideEffects()));
494
495 // Add the asm string.
496 const std::string &AsmStr = IA->getAsmString();
497 Record.push_back(AsmStr.size());
498 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
499 Record.push_back(AsmStr[i]);
500
501 // Add the constraint string.
502 const std::string &ConstraintStr = IA->getConstraintString();
503 Record.push_back(ConstraintStr.size());
504 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
505 Record.push_back(ConstraintStr[i]);
506 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
507 Record.clear();
508 continue;
509 }
510 const Constant *C = cast<Constant>(V);
511 unsigned Code = -1U;
512 unsigned AbbrevToUse = 0;
513 if (C->isNullValue()) {
514 Code = bitc::CST_CODE_NULL;
515 } else if (isa<UndefValue>(C)) {
516 Code = bitc::CST_CODE_UNDEF;
517 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
518 if (IV->getBitWidth() <= 64) {
519 int64_t V = IV->getSExtValue();
520 if (V >= 0)
521 Record.push_back(V << 1);
522 else
523 Record.push_back((-V << 1) | 1);
524 Code = bitc::CST_CODE_INTEGER;
525 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
526 } else { // Wide integers, > 64 bits in size.
527 // We have an arbitrary precision integer value to write whose
528 // bit width is > 64. However, in canonical unsigned integer
529 // format it is likely that the high bits are going to be zero.
530 // So, we only write the number of active words.
531 unsigned NWords = IV->getValue().getActiveWords();
532 const uint64_t *RawWords = IV->getValue().getRawData();
533 for (unsigned i = 0; i != NWords; ++i) {
534 int64_t V = RawWords[i];
535 if (V >= 0)
536 Record.push_back(V << 1);
537 else
538 Record.push_back((-V << 1) | 1);
539 }
540 Code = bitc::CST_CODE_WIDE_INTEGER;
541 }
542 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
543 Code = bitc::CST_CODE_FLOAT;
Dale Johannesene617f912007-08-09 22:51:36 +0000544 const Type *Ty = CFP->getType();
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000545 if (Ty == Type::FloatTy || Ty == Type::DoubleTy) {
546 Record.push_back(CFP->getValueAPF().convertToAPInt().getZExtValue());
Dale Johannesen1616e902007-09-11 18:32:33 +0000547 } else if (Ty == Type::X86_FP80Ty) {
Dale Johannesen693aa822007-09-26 23:20:33 +0000548 // api needed to prevent premature destruction
549 APInt api = CFP->getValueAPF().convertToAPInt();
550 const uint64_t *p = api.getRawData();
Dale Johannesen1616e902007-09-11 18:32:33 +0000551 Record.push_back(p[0]);
552 Record.push_back((uint16_t)p[1]);
Dale Johannesen2aef5692007-10-11 18:07:22 +0000553 } else if (Ty == Type::FP128Ty || Ty == Type::PPC_FP128Ty) {
Dale Johannesen693aa822007-09-26 23:20:33 +0000554 APInt api = CFP->getValueAPF().convertToAPInt();
555 const uint64_t *p = api.getRawData();
Dale Johannesen1616e902007-09-11 18:32:33 +0000556 Record.push_back(p[0]);
557 Record.push_back(p[1]);
Dale Johannesene617f912007-08-09 22:51:36 +0000558 } else {
559 assert (0 && "Unknown FP type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 }
561 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
562 // Emit constant strings specially.
563 unsigned NumOps = C->getNumOperands();
564 // If this is a null-terminated string, use the denser CSTRING encoding.
565 if (C->getOperand(NumOps-1)->isNullValue()) {
566 Code = bitc::CST_CODE_CSTRING;
567 --NumOps; // Don't encode the null, which isn't allowed by char6.
568 } else {
569 Code = bitc::CST_CODE_STRING;
570 AbbrevToUse = String8Abbrev;
571 }
572 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
573 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
574 for (unsigned i = 0; i != NumOps; ++i) {
575 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
576 Record.push_back(V);
577 isCStr7 &= (V & 128) == 0;
578 if (isCStrChar6)
579 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
580 }
581
582 if (isCStrChar6)
583 AbbrevToUse = CString6Abbrev;
584 else if (isCStr7)
585 AbbrevToUse = CString7Abbrev;
586 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
587 isa<ConstantVector>(V)) {
588 Code = bitc::CST_CODE_AGGREGATE;
589 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
590 Record.push_back(VE.getValueID(C->getOperand(i)));
591 AbbrevToUse = AggregateAbbrev;
592 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
593 switch (CE->getOpcode()) {
594 default:
595 if (Instruction::isCast(CE->getOpcode())) {
596 Code = bitc::CST_CODE_CE_CAST;
597 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
598 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
599 Record.push_back(VE.getValueID(C->getOperand(0)));
600 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
601 } else {
602 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
603 Code = bitc::CST_CODE_CE_BINOP;
604 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
605 Record.push_back(VE.getValueID(C->getOperand(0)));
606 Record.push_back(VE.getValueID(C->getOperand(1)));
607 }
608 break;
609 case Instruction::GetElementPtr:
610 Code = bitc::CST_CODE_CE_GEP;
611 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
612 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
613 Record.push_back(VE.getValueID(C->getOperand(i)));
614 }
615 break;
616 case Instruction::Select:
617 Code = bitc::CST_CODE_CE_SELECT;
618 Record.push_back(VE.getValueID(C->getOperand(0)));
619 Record.push_back(VE.getValueID(C->getOperand(1)));
620 Record.push_back(VE.getValueID(C->getOperand(2)));
621 break;
622 case Instruction::ExtractElement:
623 Code = bitc::CST_CODE_CE_EXTRACTELT;
624 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
625 Record.push_back(VE.getValueID(C->getOperand(0)));
626 Record.push_back(VE.getValueID(C->getOperand(1)));
627 break;
628 case Instruction::InsertElement:
629 Code = bitc::CST_CODE_CE_INSERTELT;
630 Record.push_back(VE.getValueID(C->getOperand(0)));
631 Record.push_back(VE.getValueID(C->getOperand(1)));
632 Record.push_back(VE.getValueID(C->getOperand(2)));
633 break;
634 case Instruction::ShuffleVector:
635 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
636 Record.push_back(VE.getValueID(C->getOperand(0)));
637 Record.push_back(VE.getValueID(C->getOperand(1)));
638 Record.push_back(VE.getValueID(C->getOperand(2)));
639 break;
640 case Instruction::ICmp:
641 case Instruction::FCmp:
Nate Begeman646fa482008-05-12 19:01:56 +0000642 case Instruction::VICmp:
643 case Instruction::VFCmp:
Dan Gohmanb60ca3c2008-09-09 01:02:47 +0000644 if (isa<VectorType>(C->getOperand(0)->getType())
645 && (CE->getOpcode() == Instruction::ICmp
646 || CE->getOpcode() == Instruction::FCmp)) {
647 // compare returning vector of Int1Ty
648 assert(0 && "Unsupported constant!");
649 } else {
650 Code = bitc::CST_CODE_CE_CMP;
651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
653 Record.push_back(VE.getValueID(C->getOperand(0)));
654 Record.push_back(VE.getValueID(C->getOperand(1)));
655 Record.push_back(CE->getPredicate());
656 break;
657 }
658 } else {
659 assert(0 && "Unknown constant!");
660 }
661 Stream.EmitRecord(Code, Record, AbbrevToUse);
662 Record.clear();
663 }
664
665 Stream.ExitBlock();
666}
667
668static void WriteModuleConstants(const ValueEnumerator &VE,
669 BitstreamWriter &Stream) {
670 const ValueEnumerator::ValueList &Vals = VE.getValues();
671
672 // Find the first constant to emit, which is the first non-globalvalue value.
673 // We know globalvalues have been emitted by WriteModuleInfo.
674 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
675 if (!isa<GlobalValue>(Vals[i].first)) {
676 WriteConstants(i, Vals.size(), VE, Stream, true);
677 return;
678 }
679 }
680}
681
682/// PushValueAndType - The file has to encode both the value and type id for
683/// many values, because we need to know what type to create for forward
684/// references. However, most operands are not forward references, so this type
685/// field is not needed.
686///
687/// This function adds V's value ID to Vals. If the value ID is higher than the
688/// instruction ID, then it is a forward reference, and it also includes the
689/// type ID.
690static bool PushValueAndType(Value *V, unsigned InstID,
691 SmallVector<unsigned, 64> &Vals,
692 ValueEnumerator &VE) {
693 unsigned ValID = VE.getValueID(V);
694 Vals.push_back(ValID);
695 if (ValID >= InstID) {
696 Vals.push_back(VE.getTypeID(V->getType()));
697 return true;
698 }
699 return false;
700}
701
702/// WriteInstruction - Emit an instruction to the specified stream.
703static void WriteInstruction(const Instruction &I, unsigned InstID,
704 ValueEnumerator &VE, BitstreamWriter &Stream,
705 SmallVector<unsigned, 64> &Vals) {
706 unsigned Code = 0;
707 unsigned AbbrevToUse = 0;
708 switch (I.getOpcode()) {
709 default:
710 if (Instruction::isCast(I.getOpcode())) {
711 Code = bitc::FUNC_CODE_INST_CAST;
712 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
713 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
714 Vals.push_back(VE.getTypeID(I.getType()));
715 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
716 } else {
717 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
718 Code = bitc::FUNC_CODE_INST_BINOP;
719 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
720 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
721 Vals.push_back(VE.getValueID(I.getOperand(1)));
722 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
723 }
724 break;
725
726 case Instruction::GetElementPtr:
727 Code = bitc::FUNC_CODE_INST_GEP;
728 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
729 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
730 break;
Dan Gohmanaa91c1d2008-05-31 19:11:15 +0000731 case Instruction::ExtractValue: {
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000732 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Dan Gohmanaa91c1d2008-05-31 19:11:15 +0000733 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
734 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
735 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
736 Vals.push_back(*i);
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000737 break;
Dan Gohmanaa91c1d2008-05-31 19:11:15 +0000738 }
739 case Instruction::InsertValue: {
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000740 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Dan Gohmanaa91c1d2008-05-31 19:11:15 +0000741 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
742 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
743 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
744 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
745 Vals.push_back(*i);
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000746 break;
Dan Gohmanaa91c1d2008-05-31 19:11:15 +0000747 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 case Instruction::Select:
Dan Gohman6fa5bce2008-09-16 01:01:33 +0000749 Code = bitc::FUNC_CODE_INST_VSELECT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
751 Vals.push_back(VE.getValueID(I.getOperand(2)));
Dan Gohman6fa5bce2008-09-16 01:01:33 +0000752 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 break;
754 case Instruction::ExtractElement:
755 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
756 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
757 Vals.push_back(VE.getValueID(I.getOperand(1)));
758 break;
759 case Instruction::InsertElement:
760 Code = bitc::FUNC_CODE_INST_INSERTELT;
761 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
762 Vals.push_back(VE.getValueID(I.getOperand(1)));
763 Vals.push_back(VE.getValueID(I.getOperand(2)));
764 break;
765 case Instruction::ShuffleVector:
766 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
767 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
768 Vals.push_back(VE.getValueID(I.getOperand(1)));
769 Vals.push_back(VE.getValueID(I.getOperand(2)));
770 break;
771 case Instruction::ICmp:
772 case Instruction::FCmp:
Nate Begeman646fa482008-05-12 19:01:56 +0000773 case Instruction::VICmp:
774 case Instruction::VFCmp:
Dan Gohman6fa5bce2008-09-16 01:01:33 +0000775 if (I.getOpcode() == Instruction::ICmp
776 || I.getOpcode() == Instruction::FCmp) {
777 // compare returning Int1Ty or vector of Int1Ty
778 Code = bitc::FUNC_CODE_INST_CMP2;
Dan Gohmanb60ca3c2008-09-09 01:02:47 +0000779 } else {
780 Code = bitc::FUNC_CODE_INST_CMP;
781 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
783 Vals.push_back(VE.getValueID(I.getOperand(1)));
784 Vals.push_back(cast<CmpInst>(I).getPredicate());
785 break;
786
Devang Patelb7fbc8b2008-02-26 01:29:32 +0000787 case Instruction::Ret:
788 {
789 Code = bitc::FUNC_CODE_INST_RET;
790 unsigned NumOperands = I.getNumOperands();
Devang Patelb7fbc8b2008-02-26 01:29:32 +0000791 if (NumOperands == 0)
792 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
793 else if (NumOperands == 1) {
794 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
795 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
796 } else {
797 for (unsigned i = 0, e = NumOperands; i != e; ++i)
798 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
799 }
800 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 break;
802 case Instruction::Br:
803 Code = bitc::FUNC_CODE_INST_BR;
804 Vals.push_back(VE.getValueID(I.getOperand(0)));
805 if (cast<BranchInst>(I).isConditional()) {
806 Vals.push_back(VE.getValueID(I.getOperand(1)));
807 Vals.push_back(VE.getValueID(I.getOperand(2)));
808 }
809 break;
810 case Instruction::Switch:
811 Code = bitc::FUNC_CODE_INST_SWITCH;
812 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
813 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
814 Vals.push_back(VE.getValueID(I.getOperand(i)));
815 break;
816 case Instruction::Invoke: {
817 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
818 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
819 Code = bitc::FUNC_CODE_INST_INVOKE;
820
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000821 const InvokeInst *II = cast<InvokeInst>(&I);
822 Vals.push_back(VE.getParamAttrID(II->getParamAttrs()));
823 Vals.push_back(II->getCallingConv());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest
825 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest
826 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee
827
828 // Emit value #'s for the fixed parameters.
829 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
830 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
831
832 // Emit type/value pairs for varargs params.
833 if (FTy->isVarArg()) {
834 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
835 i != e; ++i)
836 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
837 }
838 break;
839 }
840 case Instruction::Unwind:
841 Code = bitc::FUNC_CODE_INST_UNWIND;
842 break;
843 case Instruction::Unreachable:
844 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
845 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
846 break;
847
848 case Instruction::PHI:
849 Code = bitc::FUNC_CODE_INST_PHI;
850 Vals.push_back(VE.getTypeID(I.getType()));
851 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
852 Vals.push_back(VE.getValueID(I.getOperand(i)));
853 break;
854
855 case Instruction::Malloc:
856 Code = bitc::FUNC_CODE_INST_MALLOC;
857 Vals.push_back(VE.getTypeID(I.getType()));
858 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
859 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
860 break;
861
862 case Instruction::Free:
863 Code = bitc::FUNC_CODE_INST_FREE;
864 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
865 break;
866
867 case Instruction::Alloca:
868 Code = bitc::FUNC_CODE_INST_ALLOCA;
869 Vals.push_back(VE.getTypeID(I.getType()));
870 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
871 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
872 break;
873
874 case Instruction::Load:
875 Code = bitc::FUNC_CODE_INST_LOAD;
876 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
877 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
878
879 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
880 Vals.push_back(cast<LoadInst>(I).isVolatile());
881 break;
882 case Instruction::Store:
Christopher Lamb44d62f62007-12-11 08:59:05 +0000883 Code = bitc::FUNC_CODE_INST_STORE2;
884 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
885 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
887 Vals.push_back(cast<StoreInst>(I).isVolatile());
888 break;
889 case Instruction::Call: {
890 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
891 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
892
893 Code = bitc::FUNC_CODE_INST_CALL;
894
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000895 const CallInst *CI = cast<CallInst>(&I);
896 Vals.push_back(VE.getParamAttrID(CI->getParamAttrs()));
897 Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
898 PushValueAndType(CI->getOperand(0), InstID, Vals, VE); // Callee
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899
900 // Emit value #'s for the fixed parameters.
901 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
902 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
903
904 // Emit type/value pairs for varargs params.
905 if (FTy->isVarArg()) {
906 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
907 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
908 i != e; ++i)
909 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
910 }
911 break;
912 }
913 case Instruction::VAArg:
914 Code = bitc::FUNC_CODE_INST_VAARG;
915 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
916 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
917 Vals.push_back(VE.getTypeID(I.getType())); // restype.
918 break;
919 }
920
921 Stream.EmitRecord(Code, Vals, AbbrevToUse);
922 Vals.clear();
923}
924
925// Emit names for globals/functions etc.
926static void WriteValueSymbolTable(const ValueSymbolTable &VST,
927 const ValueEnumerator &VE,
928 BitstreamWriter &Stream) {
929 if (VST.empty()) return;
930 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
931
932 // FIXME: Set up the abbrev, we know how many values there are!
933 // FIXME: We know if the type names can use 7-bit ascii.
934 SmallVector<unsigned, 64> NameVals;
935
936 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
937 SI != SE; ++SI) {
938
939 const ValueName &Name = *SI;
940
941 // Figure out the encoding to use for the name.
942 bool is7Bit = true;
943 bool isChar6 = true;
944 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
945 C != E; ++C) {
946 if (isChar6)
947 isChar6 = BitCodeAbbrevOp::isChar6(*C);
948 if ((unsigned char)*C & 128) {
949 is7Bit = false;
950 break; // don't bother scanning the rest.
951 }
952 }
953
954 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
955
956 // VST_ENTRY: [valueid, namechar x N]
957 // VST_BBENTRY: [bbid, namechar x N]
958 unsigned Code;
959 if (isa<BasicBlock>(SI->getValue())) {
960 Code = bitc::VST_CODE_BBENTRY;
961 if (isChar6)
962 AbbrevToUse = VST_BBENTRY_6_ABBREV;
963 } else {
964 Code = bitc::VST_CODE_ENTRY;
965 if (isChar6)
966 AbbrevToUse = VST_ENTRY_6_ABBREV;
967 else if (is7Bit)
968 AbbrevToUse = VST_ENTRY_7_ABBREV;
969 }
970
971 NameVals.push_back(VE.getValueID(SI->getValue()));
972 for (const char *P = Name.getKeyData(),
973 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
974 NameVals.push_back((unsigned char)*P);
975
976 // Emit the finished record.
977 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
978 NameVals.clear();
979 }
980 Stream.ExitBlock();
981}
982
983/// WriteFunction - Emit a function body to the module stream.
984static void WriteFunction(const Function &F, ValueEnumerator &VE,
985 BitstreamWriter &Stream) {
986 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
987 VE.incorporateFunction(F);
988
989 SmallVector<unsigned, 64> Vals;
990
991 // Emit the number of basic blocks, so the reader can create them ahead of
992 // time.
993 Vals.push_back(VE.getBasicBlocks().size());
994 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
995 Vals.clear();
996
997 // If there are function-local constants, emit them now.
998 unsigned CstStart, CstEnd;
999 VE.getFunctionConstantRange(CstStart, CstEnd);
1000 WriteConstants(CstStart, CstEnd, VE, Stream, false);
1001
1002 // Keep a running idea of what the instruction ID is.
1003 unsigned InstID = CstEnd;
1004
1005 // Finally, emit all the instructions, in order.
Nick Lewyckyd8aa33a2008-04-25 16:53:59 +00001006 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1008 I != E; ++I) {
1009 WriteInstruction(*I, InstID, VE, Stream, Vals);
1010 if (I->getType() != Type::VoidTy)
1011 ++InstID;
1012 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013
1014 // Emit names for all the instructions etc.
1015 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1016
1017 VE.purgeFunction();
1018 Stream.ExitBlock();
1019}
1020
1021/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
1022static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
1023 const ValueEnumerator &VE,
1024 BitstreamWriter &Stream) {
1025 if (TST.empty()) return;
1026
1027 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
1028
1029 // 7-bit fixed width VST_CODE_ENTRY strings.
1030 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1031 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1032 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1033 Log2_32_Ceil(VE.getTypes().size()+1)));
1034 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1035 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1036 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
1037
1038 SmallVector<unsigned, 64> NameVals;
1039
1040 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
1041 TI != TE; ++TI) {
1042 // TST_ENTRY: [typeid, namechar x N]
1043 NameVals.push_back(VE.getTypeID(TI->second));
1044
1045 const std::string &Str = TI->first;
1046 bool is7Bit = true;
1047 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1048 NameVals.push_back((unsigned char)Str[i]);
1049 if (Str[i] & 128)
1050 is7Bit = false;
1051 }
1052
1053 // Emit the finished record.
1054 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
1055 NameVals.clear();
1056 }
1057
1058 Stream.ExitBlock();
1059}
1060
1061// Emit blockinfo, which defines the standard abbreviations etc.
1062static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1063 // We only want to emit block info records for blocks that have multiple
1064 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1065 // blocks can defined their abbrevs inline.
1066 Stream.EnterBlockInfoBlock(2);
1067
1068 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1069 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1070 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1071 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1072 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1073 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1074 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1075 Abbv) != VST_ENTRY_8_ABBREV)
1076 assert(0 && "Unexpected abbrev ordering!");
1077 }
1078
1079 { // 7-bit fixed width VST_ENTRY strings.
1080 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1081 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1082 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1083 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1084 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1085 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1086 Abbv) != VST_ENTRY_7_ABBREV)
1087 assert(0 && "Unexpected abbrev ordering!");
1088 }
1089 { // 6-bit char6 VST_ENTRY strings.
1090 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1091 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1092 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1093 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1094 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1095 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1096 Abbv) != VST_ENTRY_6_ABBREV)
1097 assert(0 && "Unexpected abbrev ordering!");
1098 }
1099 { // 6-bit char6 VST_BBENTRY strings.
1100 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1101 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1102 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1103 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1104 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1105 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1106 Abbv) != VST_BBENTRY_6_ABBREV)
1107 assert(0 && "Unexpected abbrev ordering!");
1108 }
1109
1110
1111
1112 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1113 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1114 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1116 Log2_32_Ceil(VE.getTypes().size()+1)));
1117 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1118 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1119 assert(0 && "Unexpected abbrev ordering!");
1120 }
1121
1122 { // INTEGER abbrev for CONSTANTS_BLOCK.
1123 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1124 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1126 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1127 Abbv) != CONSTANTS_INTEGER_ABBREV)
1128 assert(0 && "Unexpected abbrev ordering!");
1129 }
1130
1131 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1132 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1133 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1134 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1135 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1136 Log2_32_Ceil(VE.getTypes().size()+1)));
1137 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1138
1139 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1140 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1141 assert(0 && "Unexpected abbrev ordering!");
1142 }
1143 { // NULL abbrev for CONSTANTS_BLOCK.
1144 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1145 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1146 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1147 Abbv) != CONSTANTS_NULL_Abbrev)
1148 assert(0 && "Unexpected abbrev ordering!");
1149 }
1150
1151 // FIXME: This should only use space for first class types!
1152
1153 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1154 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1155 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1156 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1157 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1159 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1160 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1161 assert(0 && "Unexpected abbrev ordering!");
1162 }
1163 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1164 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1165 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1166 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1167 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1168 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1169 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1170 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1171 assert(0 && "Unexpected abbrev ordering!");
1172 }
1173 { // INST_CAST abbrev for FUNCTION_BLOCK.
1174 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1175 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1176 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1177 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1178 Log2_32_Ceil(VE.getTypes().size()+1)));
1179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1180 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1181 Abbv) != FUNCTION_INST_CAST_ABBREV)
1182 assert(0 && "Unexpected abbrev ordering!");
1183 }
1184
1185 { // INST_RET abbrev for FUNCTION_BLOCK.
1186 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1187 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1188 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1189 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1190 assert(0 && "Unexpected abbrev ordering!");
1191 }
1192 { // INST_RET abbrev for FUNCTION_BLOCK.
1193 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1194 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1195 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1196 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1197 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1198 assert(0 && "Unexpected abbrev ordering!");
1199 }
1200 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1201 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1202 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1203 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1204 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1205 assert(0 && "Unexpected abbrev ordering!");
1206 }
1207
1208 Stream.ExitBlock();
1209}
1210
1211
1212/// WriteModule - Emit the specified module to the bitstream.
1213static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1214 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1215
1216 // Emit the version number if it is non-zero.
1217 if (CurVersion) {
1218 SmallVector<unsigned, 1> Vals;
1219 Vals.push_back(CurVersion);
1220 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1221 }
1222
1223 // Analyze the module, enumerating globals, functions, etc.
1224 ValueEnumerator VE(M);
1225
1226 // Emit blockinfo, which defines the standard abbreviations etc.
1227 WriteBlockInfo(VE, Stream);
1228
1229 // Emit information about parameter attributes.
1230 WriteParamAttrTable(VE, Stream);
1231
1232 // Emit information describing all of the types in the module.
1233 WriteTypeTable(VE, Stream);
1234
1235 // Emit top-level description of module, including target triple, inline asm,
1236 // descriptors for global variables, and function prototype info.
1237 WriteModuleInfo(M, VE, Stream);
1238
1239 // Emit constants.
1240 WriteModuleConstants(VE, Stream);
1241
1242 // If we have any aggregate values in the value table, purge them - these can
1243 // only be used to initialize global variables. Doing so makes the value
1244 // namespace smaller for code in functions.
1245 int NumNonAggregates = VE.PurgeAggregateValues();
1246 if (NumNonAggregates != -1) {
1247 SmallVector<unsigned, 1> Vals;
1248 Vals.push_back(NumNonAggregates);
1249 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1250 }
1251
1252 // Emit function bodies.
1253 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1254 if (!I->isDeclaration())
1255 WriteFunction(*I, VE, Stream);
1256
1257 // Emit the type symbol table information.
1258 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1259
1260 // Emit names for globals/functions etc.
1261 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1262
1263 Stream.ExitBlock();
1264}
1265
Chris Lattner65b13ff2008-07-09 05:14:23 +00001266/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1267/// header and trailer to make it compatible with the system archiver. To do
1268/// this we emit the following header, and then emit a trailer that pads the
1269/// file out to be a multiple of 16 bytes.
1270///
1271/// struct bc_header {
1272/// uint32_t Magic; // 0x0B17C0DE
1273/// uint32_t Version; // Version, currently always 0.
1274/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1275/// uint32_t BitcodeSize; // Size of traditional bitcode file.
1276/// uint32_t CPUType; // CPU specifier.
1277/// ... potentially more later ...
1278/// };
1279enum {
1280 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1281 DarwinBCHeaderSize = 5*4
1282};
1283
1284static void EmitDarwinBCHeader(BitstreamWriter &Stream,
1285 const std::string &TT) {
1286 unsigned CPUType = ~0U;
1287
1288 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*. The CPUType is a
1289 // magic number from /usr/include/mach/machine.h. It is ok to reproduce the
1290 // specific constants here because they are implicitly part of the Darwin ABI.
1291 enum {
1292 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
1293 DARWIN_CPU_TYPE_X86 = 7,
1294 DARWIN_CPU_TYPE_POWERPC = 18
1295 };
1296
1297 if (TT.find("x86_64-") == 0)
1298 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
1299 else if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
1300 TT[4] == '-' && TT[1] - '3' < 6)
1301 CPUType = DARWIN_CPU_TYPE_X86;
1302 else if (TT.find("powerpc-") == 0)
1303 CPUType = DARWIN_CPU_TYPE_POWERPC;
1304 else if (TT.find("powerpc64-") == 0)
1305 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
1306
1307 // Traditional Bitcode starts after header.
1308 unsigned BCOffset = DarwinBCHeaderSize;
1309
1310 Stream.Emit(0x0B17C0DE, 32);
1311 Stream.Emit(0 , 32); // Version.
1312 Stream.Emit(BCOffset , 32);
1313 Stream.Emit(0 , 32); // Filled in later.
1314 Stream.Emit(CPUType , 32);
1315}
1316
1317/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
1318/// finalize the header.
1319static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
1320 // Update the size field in the header.
1321 Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
1322
1323 // If the file is not a multiple of 16 bytes, insert dummy padding.
1324 while (BufferSize & 15) {
1325 Stream.Emit(0, 8);
1326 ++BufferSize;
1327 }
1328}
1329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330
1331/// WriteBitcodeToFile - Write the specified module to the specified output
1332/// stream.
1333void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1334 std::vector<unsigned char> Buffer;
1335 BitstreamWriter Stream(Buffer);
1336
1337 Buffer.reserve(256*1024);
1338
Chris Lattner65b13ff2008-07-09 05:14:23 +00001339 // If this is darwin, emit a file header and trailer if needed.
1340 bool isDarwin = M->getTargetTriple().find("-darwin") != std::string::npos;
1341 if (isDarwin)
1342 EmitDarwinBCHeader(Stream, M->getTargetTriple());
1343
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 // Emit the file header.
1345 Stream.Emit((unsigned)'B', 8);
1346 Stream.Emit((unsigned)'C', 8);
1347 Stream.Emit(0x0, 4);
1348 Stream.Emit(0xC, 4);
1349 Stream.Emit(0xE, 4);
1350 Stream.Emit(0xD, 4);
1351
1352 // Emit the module.
1353 WriteModule(M, Stream);
Chris Lattner65b13ff2008-07-09 05:14:23 +00001354
1355 if (isDarwin)
1356 EmitDarwinBCTrailer(Stream, Buffer.size());
1357
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358
Anton Korobeynikova460a3a2008-06-06 07:24:01 +00001359 // If writing to stdout, set binary mode.
1360 if (llvm::cout == Out)
Chris Lattner65b13ff2008-07-09 05:14:23 +00001361 sys::Program::ChangeStdoutToBinary();
Anton Korobeynikova460a3a2008-06-06 07:24:01 +00001362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 // Write the generated bitstream to "Out".
1364 Out.write((char*)&Buffer.front(), Buffer.size());
1365
1366 // Make sure it hits disk now.
1367 Out.flush();
1368}