blob: 5633f0ffad954eefda26a7c8594b62f9f3868a53 [file] [log] [blame]
Chris Lattner1a9df8e2007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerfd57cec2007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfd57cec2007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Bitcode/ReaderWriter.h"
15#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner47f96bf2007-04-23 01:01:37 +000016#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000017#include "ValueEnumerator.h"
Chris Lattner2edd22b2007-04-24 00:16:04 +000018#include "llvm/Constants.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000019#include "llvm/DerivedTypes.h"
Chris Lattner2bce93a2007-05-06 01:58:20 +000020#include "llvm/InlineAsm.h"
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000021#include "llvm/Instructions.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000022#include "llvm/Module.h"
23#include "llvm/TypeSymbolTable.h"
Chris Lattnerb992be12007-04-23 20:35:01 +000024#include "llvm/ValueSymbolTable.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnere35f1ca2008-08-23 21:33:24 +000026#include "llvm/Support/Streams.h"
Daniel Dunbard1ce3b42008-10-22 17:39:14 +000027#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovb0a882f2008-06-06 07:24:01 +000028#include "llvm/System/Program.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000029using namespace llvm;
30
Chris Lattner59698302007-05-04 20:52:02 +000031/// These are manifest constants used by the bitcode writer. They do not need to
32/// be kept in sync with the reader, but need to be consistent within this file.
33enum {
34 CurVersion = 0,
35
36 // VALUE_SYMTAB_BLOCK abbrev id's.
37 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattner2453f712007-05-04 20:58:35 +000038 VST_ENTRY_7_ABBREV,
Chris Lattnerff294a42007-05-05 01:26:50 +000039 VST_ENTRY_6_ABBREV,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000040 VST_BBENTRY_6_ABBREV,
41
42 // CONSTANTS_BLOCK abbrev id's.
43 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
44 CONSTANTS_INTEGER_ABBREV,
45 CONSTANTS_CE_CAST_Abbrev,
Chris Lattner440168b2007-05-05 07:44:49 +000046 CONSTANTS_NULL_Abbrev,
47
48 // FUNCTION_BLOCK abbrev id's.
Chris Lattner94687ac2007-05-06 01:28:01 +000049 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000050 FUNCTION_INST_BINOP_ABBREV,
51 FUNCTION_INST_CAST_ABBREV,
Chris Lattner94687ac2007-05-06 01:28:01 +000052 FUNCTION_INST_RET_VOID_ABBREV,
53 FUNCTION_INST_RET_VAL_ABBREV,
54 FUNCTION_INST_UNREACHABLE_ABBREV
Chris Lattner59698302007-05-04 20:52:02 +000055};
56
Chris Lattnerfd57cec2007-04-22 06:24:45 +000057
Chris Lattnerf581c3b2007-04-24 07:07:11 +000058static unsigned GetEncodedCastOpcode(unsigned Opcode) {
59 switch (Opcode) {
60 default: assert(0 && "Unknown cast instruction!");
61 case Instruction::Trunc : return bitc::CAST_TRUNC;
62 case Instruction::ZExt : return bitc::CAST_ZEXT;
63 case Instruction::SExt : return bitc::CAST_SEXT;
64 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
65 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
66 case Instruction::UIToFP : return bitc::CAST_UITOFP;
67 case Instruction::SIToFP : return bitc::CAST_SITOFP;
68 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
69 case Instruction::FPExt : return bitc::CAST_FPEXT;
70 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
71 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
72 case Instruction::BitCast : return bitc::CAST_BITCAST;
73 }
74}
75
76static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
77 switch (Opcode) {
78 default: assert(0 && "Unknown binary instruction!");
79 case Instruction::Add: return bitc::BINOP_ADD;
80 case Instruction::Sub: return bitc::BINOP_SUB;
81 case Instruction::Mul: return bitc::BINOP_MUL;
82 case Instruction::UDiv: return bitc::BINOP_UDIV;
83 case Instruction::FDiv:
84 case Instruction::SDiv: return bitc::BINOP_SDIV;
85 case Instruction::URem: return bitc::BINOP_UREM;
86 case Instruction::FRem:
87 case Instruction::SRem: return bitc::BINOP_SREM;
88 case Instruction::Shl: return bitc::BINOP_SHL;
89 case Instruction::LShr: return bitc::BINOP_LSHR;
90 case Instruction::AShr: return bitc::BINOP_ASHR;
91 case Instruction::And: return bitc::BINOP_AND;
92 case Instruction::Or: return bitc::BINOP_OR;
93 case Instruction::Xor: return bitc::BINOP_XOR;
94 }
95}
96
97
98
Chris Lattnerfd57cec2007-04-22 06:24:45 +000099static void WriteStringRecord(unsigned Code, const std::string &Str,
100 unsigned AbbrevToUse, BitstreamWriter &Stream) {
101 SmallVector<unsigned, 64> Vals;
102
Chris Lattner15e6d172007-05-04 19:11:41 +0000103 // Code: [strchar x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000104 for (unsigned i = 0, e = Str.size(); i != e; ++i)
105 Vals.push_back(Str[i]);
106
107 // Emit the finished record.
108 Stream.EmitRecord(Code, Vals, AbbrevToUse);
109}
110
Chris Lattner62bbeea2007-05-04 00:44:52 +0000111// Emit information about parameter attributes.
Devang Patel05988662008-09-25 21:00:45 +0000112static void WriteAttributeTable(const ValueEnumerator &VE,
Chris Lattner62bbeea2007-05-04 00:44:52 +0000113 BitstreamWriter &Stream) {
Devang Patel05988662008-09-25 21:00:45 +0000114 const std::vector<AttrListPtr> &Attrs = VE.getAttributes();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000115 if (Attrs.empty()) return;
116
Chris Lattnerf0a65312007-05-04 02:59:04 +0000117 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
118
119 SmallVector<uint64_t, 64> Record;
120 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000121 const AttrListPtr &A = Attrs[i];
Chris Lattner58d74912008-03-12 17:45:29 +0000122 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000123 const AttributeWithIndex &PAWI = A.getSlot(i);
Chris Lattner58d74912008-03-12 17:45:29 +0000124 Record.push_back(PAWI.Index);
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000125
126 // FIXME: remove in LLVM 3.0
127 // Store the alignment in the bitcode as a 16-bit raw value instead of a
128 // 5-bit log2 encoded value. Shift the bits above the alignment up by
129 // 11 bits.
130 uint64_t FauxAttr = PAWI.Attrs & 0xffff;
131 if (PAWI.Attrs & Attribute::Alignment)
132 FauxAttr |= (1ull<<16)<<(((PAWI.Attrs & Attribute::Alignment)-1) >> 16);
133 FauxAttr |= (PAWI.Attrs & (0x3FFull << 21)) << 11;
134
135 Record.push_back(FauxAttr);
Chris Lattnerf0a65312007-05-04 02:59:04 +0000136 }
137
138 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
139 Record.clear();
140 }
Chris Lattner62bbeea2007-05-04 00:44:52 +0000141
Chris Lattnerf0a65312007-05-04 02:59:04 +0000142 Stream.ExitBlock();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000143}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000144
145/// WriteTypeTable - Write out the type table for a module.
146static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
147 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
148
149 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
150 SmallVector<uint64_t, 64> TypeVals;
151
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000152 // Abbrev for TYPE_CODE_POINTER.
153 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
154 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
155 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
156 Log2_32_Ceil(VE.getTypes().size()+1)));
Christopher Lambd49e18d2007-12-12 08:44:39 +0000157 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000158 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
159
160 // Abbrev for TYPE_CODE_FUNCTION.
161 Abbv = new BitCodeAbbrev();
162 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
163 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnera1afde72007-11-27 17:48:06 +0000164 Abbv->Add(BitCodeAbbrevOp(0)); // FIXME: DEAD value, remove in LLVM 3.0
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000165 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
166 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
167 Log2_32_Ceil(VE.getTypes().size()+1)));
168 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
169
170 // Abbrev for TYPE_CODE_STRUCT.
171 Abbv = new BitCodeAbbrev();
172 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
173 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
174 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
175 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
176 Log2_32_Ceil(VE.getTypes().size()+1)));
177 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
178
179 // Abbrev for TYPE_CODE_ARRAY.
180 Abbv = new BitCodeAbbrev();
181 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
182 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
183 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
184 Log2_32_Ceil(VE.getTypes().size()+1)));
185 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000186
187 // Emit an entry count so the reader can reserve space.
188 TypeVals.push_back(TypeList.size());
189 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
190 TypeVals.clear();
191
192 // Loop over all of the types, emitting each in turn.
193 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
194 const Type *T = TypeList[i].first;
195 int AbbrevToUse = 0;
196 unsigned Code = 0;
197
198 switch (T->getTypeID()) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000199 default: assert(0 && "Unknown type!");
200 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
201 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
202 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000203 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
204 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
205 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000206 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
207 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
208 case Type::IntegerTyID:
209 // INTEGER: [width]
210 Code = bitc::TYPE_CODE_INTEGER;
211 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
212 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000213 case Type::PointerTyID: {
Christopher Lambfe63fb92007-12-11 08:59:05 +0000214 const PointerType *PTy = cast<PointerType>(T);
Christopher Lambd49e18d2007-12-12 08:44:39 +0000215 // POINTER: [pointee type, address space]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000216 Code = bitc::TYPE_CODE_POINTER;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000217 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lambd49e18d2007-12-12 08:44:39 +0000218 unsigned AddressSpace = PTy->getAddressSpace();
219 TypeVals.push_back(AddressSpace);
220 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000221 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000222 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000223 case Type::FunctionTyID: {
224 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattnera1afde72007-11-27 17:48:06 +0000225 // FUNCTION: [isvararg, attrid, retty, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000226 Code = bitc::TYPE_CODE_FUNCTION;
227 TypeVals.push_back(FT->isVarArg());
Chris Lattnera1afde72007-11-27 17:48:06 +0000228 TypeVals.push_back(0); // FIXME: DEAD: remove in llvm 3.0
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000229 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000230 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
231 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000232 AbbrevToUse = FunctionAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000233 break;
234 }
235 case Type::StructTyID: {
236 const StructType *ST = cast<StructType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000237 // STRUCT: [ispacked, eltty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000238 Code = bitc::TYPE_CODE_STRUCT;
239 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000240 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000241 for (StructType::element_iterator I = ST->element_begin(),
242 E = ST->element_end(); I != E; ++I)
243 TypeVals.push_back(VE.getTypeID(*I));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000244 AbbrevToUse = StructAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000245 break;
246 }
247 case Type::ArrayTyID: {
248 const ArrayType *AT = cast<ArrayType>(T);
249 // ARRAY: [numelts, eltty]
250 Code = bitc::TYPE_CODE_ARRAY;
251 TypeVals.push_back(AT->getNumElements());
252 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000253 AbbrevToUse = ArrayAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000254 break;
255 }
256 case Type::VectorTyID: {
257 const VectorType *VT = cast<VectorType>(T);
258 // VECTOR [numelts, eltty]
259 Code = bitc::TYPE_CODE_VECTOR;
260 TypeVals.push_back(VT->getNumElements());
261 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
262 break;
263 }
264 }
265
266 // Emit the finished record.
267 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
268 TypeVals.clear();
269 }
270
271 Stream.ExitBlock();
272}
273
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000274static unsigned getEncodedLinkage(const GlobalValue *GV) {
275 switch (GV->getLinkage()) {
276 default: assert(0 && "Invalid linkage!");
Chris Lattner384003d2007-05-11 23:51:59 +0000277 case GlobalValue::GhostLinkage: // Map ghost linkage onto external.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000278 case GlobalValue::ExternalLinkage: return 0;
279 case GlobalValue::WeakLinkage: return 1;
280 case GlobalValue::AppendingLinkage: return 2;
281 case GlobalValue::InternalLinkage: return 3;
282 case GlobalValue::LinkOnceLinkage: return 4;
283 case GlobalValue::DLLImportLinkage: return 5;
284 case GlobalValue::DLLExportLinkage: return 6;
285 case GlobalValue::ExternalWeakLinkage: return 7;
Dale Johannesenaafce772008-05-14 20:12:51 +0000286 case GlobalValue::CommonLinkage: return 8;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000287 case GlobalValue::PrivateLinkage: return 9;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000288 }
289}
290
291static unsigned getEncodedVisibility(const GlobalValue *GV) {
292 switch (GV->getVisibility()) {
293 default: assert(0 && "Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000294 case GlobalValue::DefaultVisibility: return 0;
295 case GlobalValue::HiddenVisibility: return 1;
296 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000297 }
298}
299
300// Emit top-level description of module, including target triple, inline asm,
301// descriptors for global variables, and function prototype info.
302static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
303 BitstreamWriter &Stream) {
304 // Emit the list of dependent libraries for the Module.
305 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
306 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
307
308 // Emit various pieces of data attached to a module.
309 if (!M->getTargetTriple().empty())
310 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
311 0/*TODO*/, Stream);
312 if (!M->getDataLayout().empty())
313 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
314 0/*TODO*/, Stream);
315 if (!M->getModuleInlineAsm().empty())
316 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
317 0/*TODO*/, Stream);
318
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000319 // Emit information about sections and GC, computing how many there are. Also
320 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000321 std::map<std::string, unsigned> SectionMap;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000322 std::map<std::string, unsigned> GCMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000323 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000324 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000325 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
326 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000327 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000328 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000329
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000330 if (!GV->hasSection()) continue;
331 // Give section names unique ID's.
332 unsigned &Entry = SectionMap[GV->getSection()];
333 if (Entry != 0) continue;
334 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
335 0/*TODO*/, Stream);
336 Entry = SectionMap.size();
337 }
338 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000339 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000340 if (F->hasSection()) {
341 // Give section names unique ID's.
342 unsigned &Entry = SectionMap[F->getSection()];
343 if (!Entry) {
344 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
345 0/*TODO*/, Stream);
346 Entry = SectionMap.size();
347 }
348 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000349 if (F->hasGC()) {
350 // Same for GC names.
351 unsigned &Entry = GCMap[F->getGC()];
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000352 if (!Entry) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000353 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(),
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000354 0/*TODO*/, Stream);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000355 Entry = GCMap.size();
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000356 }
357 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000358 }
359
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000360 // Emit abbrev for globals, now that we know # sections and max alignment.
361 unsigned SimpleGVarAbbrev = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000362 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000363 // Add an abbrev for common globals with no visibility or thread localness.
364 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
365 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000366 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000367 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000368 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Dale Johannesen66676462008-05-15 20:49:28 +0000370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000371 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000372 Abbv->Add(BitCodeAbbrevOp(0));
373 else {
374 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000375 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000376 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000377 }
378 if (SectionMap.empty()) // Section.
379 Abbv->Add(BitCodeAbbrevOp(0));
380 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000381 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000382 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000383 // Don't bother emitting vis + thread local.
384 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
385 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000386
387 // Emit the global variable information.
388 SmallVector<unsigned, 64> Vals;
389 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
390 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000391 unsigned AbbrevToUse = 0;
392
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000393 // GLOBALVAR: [type, isconst, initid,
394 // linkage, alignment, section, visibility, threadlocal]
395 Vals.push_back(VE.getTypeID(GV->getType()));
396 Vals.push_back(GV->isConstant());
397 Vals.push_back(GV->isDeclaration() ? 0 :
398 (VE.getValueID(GV->getInitializer()) + 1));
399 Vals.push_back(getEncodedLinkage(GV));
400 Vals.push_back(Log2_32(GV->getAlignment())+1);
401 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000402 if (GV->isThreadLocal() ||
403 GV->getVisibility() != GlobalValue::DefaultVisibility) {
404 Vals.push_back(getEncodedVisibility(GV));
405 Vals.push_back(GV->isThreadLocal());
406 } else {
407 AbbrevToUse = SimpleGVarAbbrev;
408 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000409
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000410 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
411 Vals.clear();
412 }
413
414 // Emit the function proto information.
415 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Duncan Sandsdc024672007-11-27 13:23:08 +0000416 // FUNCTION: [type, callingconv, isproto, paramattr,
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000417 // linkage, alignment, section, visibility, gc]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000418 Vals.push_back(VE.getTypeID(F->getType()));
419 Vals.push_back(F->getCallingConv());
420 Vals.push_back(F->isDeclaration());
421 Vals.push_back(getEncodedLinkage(F));
Devang Patel05988662008-09-25 21:00:45 +0000422 Vals.push_back(VE.getAttributeID(F->getAttributes()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000423 Vals.push_back(Log2_32(F->getAlignment())+1);
424 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
425 Vals.push_back(getEncodedVisibility(F));
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000426 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000427
428 unsigned AbbrevToUse = 0;
429 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
430 Vals.clear();
431 }
Chris Lattner07d98b42007-04-26 02:46:40 +0000432
433
434 // Emit the alias information.
435 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
436 AI != E; ++AI) {
437 Vals.push_back(VE.getTypeID(AI->getType()));
438 Vals.push_back(VE.getValueID(AI->getAliasee()));
439 Vals.push_back(getEncodedLinkage(AI));
Anton Korobeynikovf8342b92008-03-11 21:40:17 +0000440 Vals.push_back(getEncodedVisibility(AI));
Chris Lattner07d98b42007-04-26 02:46:40 +0000441 unsigned AbbrevToUse = 0;
442 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
443 Vals.clear();
444 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000445}
446
447
Chris Lattner2edd22b2007-04-24 00:16:04 +0000448static void WriteConstants(unsigned FirstVal, unsigned LastVal,
449 const ValueEnumerator &VE,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000450 BitstreamWriter &Stream, bool isGlobal) {
Chris Lattner2edd22b2007-04-24 00:16:04 +0000451 if (FirstVal == LastVal) return;
452
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000453 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Chris Lattnerb992be12007-04-23 20:35:01 +0000454
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000455 unsigned AggregateAbbrev = 0;
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000456 unsigned String8Abbrev = 0;
457 unsigned CString7Abbrev = 0;
458 unsigned CString6Abbrev = 0;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000459 // If this is a constant pool for the module, emit module-specific abbrevs.
460 if (isGlobal) {
461 // Abbrev for CST_CODE_AGGREGATE.
462 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
463 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
464 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
465 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
466 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner817f08a2007-05-06 00:42:18 +0000467
468 // Abbrev for CST_CODE_STRING.
469 Abbv = new BitCodeAbbrev();
470 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
471 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000472 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
473 String8Abbrev = Stream.EmitAbbrev(Abbv);
474 // Abbrev for CST_CODE_CSTRING.
475 Abbv = new BitCodeAbbrev();
476 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
477 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattner817f08a2007-05-06 00:42:18 +0000478 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000479 CString7Abbrev = Stream.EmitAbbrev(Abbv);
480 // Abbrev for CST_CODE_CSTRING.
481 Abbv = new BitCodeAbbrev();
482 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
483 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
484 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
485 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000486 }
487
Chris Lattner2edd22b2007-04-24 00:16:04 +0000488 SmallVector<uint64_t, 64> Record;
489
490 const ValueEnumerator::ValueList &Vals = VE.getValues();
491 const Type *LastTy = 0;
492 for (unsigned i = FirstVal; i != LastVal; ++i) {
493 const Value *V = Vals[i].first;
494 // If we need to switch types, do so now.
495 if (V->getType() != LastTy) {
496 LastTy = V->getType();
497 Record.push_back(VE.getTypeID(LastTy));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000498 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
499 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000500 Record.clear();
501 }
502
503 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner2bce93a2007-05-06 01:58:20 +0000504 Record.push_back(unsigned(IA->hasSideEffects()));
505
506 // Add the asm string.
507 const std::string &AsmStr = IA->getAsmString();
508 Record.push_back(AsmStr.size());
509 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
510 Record.push_back(AsmStr[i]);
511
512 // Add the constraint string.
513 const std::string &ConstraintStr = IA->getConstraintString();
514 Record.push_back(ConstraintStr.size());
515 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
516 Record.push_back(ConstraintStr[i]);
517 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
518 Record.clear();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000519 continue;
520 }
521 const Constant *C = cast<Constant>(V);
522 unsigned Code = -1U;
523 unsigned AbbrevToUse = 0;
524 if (C->isNullValue()) {
525 Code = bitc::CST_CODE_NULL;
526 } else if (isa<UndefValue>(C)) {
527 Code = bitc::CST_CODE_UNDEF;
528 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
529 if (IV->getBitWidth() <= 64) {
530 int64_t V = IV->getSExtValue();
531 if (V >= 0)
532 Record.push_back(V << 1);
533 else
534 Record.push_back((-V << 1) | 1);
535 Code = bitc::CST_CODE_INTEGER;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000536 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000537 } else { // Wide integers, > 64 bits in size.
538 // We have an arbitrary precision integer value to write whose
539 // bit width is > 64. However, in canonical unsigned integer
540 // format it is likely that the high bits are going to be zero.
541 // So, we only write the number of active words.
542 unsigned NWords = IV->getValue().getActiveWords();
543 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000544 for (unsigned i = 0; i != NWords; ++i) {
545 int64_t V = RawWords[i];
546 if (V >= 0)
547 Record.push_back(V << 1);
548 else
549 Record.push_back((-V << 1) | 1);
550 }
551 Code = bitc::CST_CODE_WIDE_INTEGER;
552 }
553 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
554 Code = bitc::CST_CODE_FLOAT;
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000555 const Type *Ty = CFP->getType();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000556 if (Ty == Type::FloatTy || Ty == Type::DoubleTy) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000557 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000558 } else if (Ty == Type::X86_FP80Ty) {
Dale Johannesen693717f2007-09-26 23:20:33 +0000559 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000560 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000561 const uint64_t *p = api.getRawData();
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000562 Record.push_back(p[0]);
563 Record.push_back((uint16_t)p[1]);
Dale Johannesena471c2e2007-10-11 18:07:22 +0000564 } else if (Ty == Type::FP128Ty || Ty == Type::PPC_FP128Ty) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000565 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000566 const uint64_t *p = api.getRawData();
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000567 Record.push_back(p[0]);
568 Record.push_back(p[1]);
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000569 } else {
570 assert (0 && "Unknown FP type!");
Chris Lattner2edd22b2007-04-24 00:16:04 +0000571 }
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000572 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
573 // Emit constant strings specially.
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000574 unsigned NumOps = C->getNumOperands();
575 // If this is a null-terminated string, use the denser CSTRING encoding.
576 if (C->getOperand(NumOps-1)->isNullValue()) {
577 Code = bitc::CST_CODE_CSTRING;
578 --NumOps; // Don't encode the null, which isn't allowed by char6.
579 } else {
580 Code = bitc::CST_CODE_STRING;
581 AbbrevToUse = String8Abbrev;
582 }
583 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
584 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
585 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner817f08a2007-05-06 00:42:18 +0000586 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
587 Record.push_back(V);
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000588 isCStr7 &= (V & 128) == 0;
589 if (isCStrChar6)
590 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
Chris Lattner817f08a2007-05-06 00:42:18 +0000591 }
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000592
593 if (isCStrChar6)
594 AbbrevToUse = CString6Abbrev;
595 else if (isCStr7)
596 AbbrevToUse = CString7Abbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000597 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
598 isa<ConstantVector>(V)) {
599 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000600 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
601 Record.push_back(VE.getValueID(C->getOperand(i)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000602 AbbrevToUse = AggregateAbbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000603 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000604 switch (CE->getOpcode()) {
605 default:
606 if (Instruction::isCast(CE->getOpcode())) {
607 Code = bitc::CST_CODE_CE_CAST;
608 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
609 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
610 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000611 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000612 } else {
613 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
614 Code = bitc::CST_CODE_CE_BINOP;
615 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
616 Record.push_back(VE.getValueID(C->getOperand(0)));
617 Record.push_back(VE.getValueID(C->getOperand(1)));
618 }
619 break;
620 case Instruction::GetElementPtr:
621 Code = bitc::CST_CODE_CE_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000622 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
623 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
624 Record.push_back(VE.getValueID(C->getOperand(i)));
625 }
626 break;
627 case Instruction::Select:
628 Code = bitc::CST_CODE_CE_SELECT;
629 Record.push_back(VE.getValueID(C->getOperand(0)));
630 Record.push_back(VE.getValueID(C->getOperand(1)));
631 Record.push_back(VE.getValueID(C->getOperand(2)));
632 break;
633 case Instruction::ExtractElement:
634 Code = bitc::CST_CODE_CE_EXTRACTELT;
635 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
636 Record.push_back(VE.getValueID(C->getOperand(0)));
637 Record.push_back(VE.getValueID(C->getOperand(1)));
638 break;
639 case Instruction::InsertElement:
640 Code = bitc::CST_CODE_CE_INSERTELT;
641 Record.push_back(VE.getValueID(C->getOperand(0)));
642 Record.push_back(VE.getValueID(C->getOperand(1)));
643 Record.push_back(VE.getValueID(C->getOperand(2)));
644 break;
645 case Instruction::ShuffleVector:
Nate Begeman0f123cf2009-02-12 21:28:33 +0000646 // If the return type and argument types are the same, this is a
647 // standard shufflevector instruction. If the types are different,
648 // then the shuffle is widening or truncating the input vectors, and
649 // the argument type must also be encoded.
650 if (C->getType() == C->getOperand(0)->getType()) {
651 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
652 } else {
653 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
654 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
655 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000656 Record.push_back(VE.getValueID(C->getOperand(0)));
657 Record.push_back(VE.getValueID(C->getOperand(1)));
658 Record.push_back(VE.getValueID(C->getOperand(2)));
659 break;
660 case Instruction::ICmp:
661 case Instruction::FCmp:
Nate Begemanac80ade2008-05-12 19:01:56 +0000662 case Instruction::VICmp:
663 case Instruction::VFCmp:
Dan Gohmanf72fb672008-09-09 01:02:47 +0000664 if (isa<VectorType>(C->getOperand(0)->getType())
665 && (CE->getOpcode() == Instruction::ICmp
666 || CE->getOpcode() == Instruction::FCmp)) {
667 // compare returning vector of Int1Ty
668 assert(0 && "Unsupported constant!");
669 } else {
670 Code = bitc::CST_CODE_CE_CMP;
671 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000672 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
673 Record.push_back(VE.getValueID(C->getOperand(0)));
674 Record.push_back(VE.getValueID(C->getOperand(1)));
675 Record.push_back(CE->getPredicate());
676 break;
677 }
Chris Lattner2edd22b2007-04-24 00:16:04 +0000678 } else {
679 assert(0 && "Unknown constant!");
680 }
681 Stream.EmitRecord(Code, Record, AbbrevToUse);
682 Record.clear();
683 }
684
685 Stream.ExitBlock();
686}
687
688static void WriteModuleConstants(const ValueEnumerator &VE,
689 BitstreamWriter &Stream) {
690 const ValueEnumerator::ValueList &Vals = VE.getValues();
691
692 // Find the first constant to emit, which is the first non-globalvalue value.
693 // We know globalvalues have been emitted by WriteModuleInfo.
694 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
695 if (!isa<GlobalValue>(Vals[i].first)) {
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000696 WriteConstants(i, Vals.size(), VE, Stream, true);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000697 return;
698 }
699 }
700}
Chris Lattnerb992be12007-04-23 20:35:01 +0000701
Chris Lattner7337ab92007-05-06 00:00:00 +0000702/// PushValueAndType - The file has to encode both the value and type id for
703/// many values, because we need to know what type to create for forward
704/// references. However, most operands are not forward references, so this type
705/// field is not needed.
706///
707/// This function adds V's value ID to Vals. If the value ID is higher than the
708/// instruction ID, then it is a forward reference, and it also includes the
709/// type ID.
Gabor Greif75a46eb2009-01-16 18:40:27 +0000710static bool PushValueAndType(const Value *V, unsigned InstID,
Chris Lattner7337ab92007-05-06 00:00:00 +0000711 SmallVector<unsigned, 64> &Vals,
712 ValueEnumerator &VE) {
713 unsigned ValID = VE.getValueID(V);
714 Vals.push_back(ValID);
715 if (ValID >= InstID) {
716 Vals.push_back(VE.getTypeID(V->getType()));
717 return true;
718 }
719 return false;
720}
721
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000722/// WriteInstruction - Emit an instruction to the specified stream.
Chris Lattner7337ab92007-05-06 00:00:00 +0000723static void WriteInstruction(const Instruction &I, unsigned InstID,
724 ValueEnumerator &VE, BitstreamWriter &Stream,
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000725 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000726 unsigned Code = 0;
727 unsigned AbbrevToUse = 0;
728 switch (I.getOpcode()) {
729 default:
730 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +0000731 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000732 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
733 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000734 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000735 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000736 } else {
737 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +0000738 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000739 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
740 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000741 Vals.push_back(VE.getValueID(I.getOperand(1)));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000742 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000743 }
744 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000745
746 case Instruction::GetElementPtr:
747 Code = bitc::FUNC_CODE_INST_GEP;
Chris Lattner7337ab92007-05-06 00:00:00 +0000748 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
749 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000750 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000751 case Instruction::ExtractValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000752 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000753 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
754 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
755 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
756 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +0000757 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000758 }
759 case Instruction::InsertValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000760 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000761 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
762 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
763 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
764 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
765 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +0000766 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000767 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000768 case Instruction::Select:
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +0000769 Code = bitc::FUNC_CODE_INST_VSELECT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000770 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000771 Vals.push_back(VE.getValueID(I.getOperand(2)));
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +0000772 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000773 break;
774 case Instruction::ExtractElement:
775 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000776 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000777 Vals.push_back(VE.getValueID(I.getOperand(1)));
778 break;
779 case Instruction::InsertElement:
780 Code = bitc::FUNC_CODE_INST_INSERTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000781 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000782 Vals.push_back(VE.getValueID(I.getOperand(1)));
783 Vals.push_back(VE.getValueID(I.getOperand(2)));
784 break;
785 case Instruction::ShuffleVector:
786 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000787 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000788 Vals.push_back(VE.getValueID(I.getOperand(1)));
789 Vals.push_back(VE.getValueID(I.getOperand(2)));
790 break;
791 case Instruction::ICmp:
792 case Instruction::FCmp:
Nate Begemanac80ade2008-05-12 19:01:56 +0000793 case Instruction::VICmp:
794 case Instruction::VFCmp:
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +0000795 if (I.getOpcode() == Instruction::ICmp
796 || I.getOpcode() == Instruction::FCmp) {
797 // compare returning Int1Ty or vector of Int1Ty
798 Code = bitc::FUNC_CODE_INST_CMP2;
Dan Gohmanf72fb672008-09-09 01:02:47 +0000799 } else {
800 Code = bitc::FUNC_CODE_INST_CMP;
801 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000802 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000803 Vals.push_back(VE.getValueID(I.getOperand(1)));
804 Vals.push_back(cast<CmpInst>(I).getPredicate());
805 break;
806
Devang Pateld9d99ff2008-02-26 01:29:32 +0000807 case Instruction::Ret:
808 {
809 Code = bitc::FUNC_CODE_INST_RET;
810 unsigned NumOperands = I.getNumOperands();
Devang Pateld9d99ff2008-02-26 01:29:32 +0000811 if (NumOperands == 0)
812 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
813 else if (NumOperands == 1) {
814 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
815 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
816 } else {
817 for (unsigned i = 0, e = NumOperands; i != e; ++i)
818 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
819 }
820 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000821 break;
822 case Instruction::Br:
Gabor Greifae6ab1e2009-01-30 18:27:21 +0000823 {
824 Code = bitc::FUNC_CODE_INST_BR;
825 BranchInst &II(cast<BranchInst>(I));
826 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
827 if (II.isConditional()) {
828 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
829 Vals.push_back(VE.getValueID(II.getCondition()));
830 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000831 }
832 break;
833 case Instruction::Switch:
834 Code = bitc::FUNC_CODE_INST_SWITCH;
835 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000836 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
837 Vals.push_back(VE.getValueID(I.getOperand(i)));
838 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000839 case Instruction::Invoke: {
Gabor Greif75a46eb2009-01-16 18:40:27 +0000840 const InvokeInst *II = cast<InvokeInst>(&I);
841 const Value *Callee(II->getCalledValue());
842 const PointerType *PTy = cast<PointerType>(Callee->getType());
Chris Lattnera9bb7132007-05-08 05:38:01 +0000843 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattnerd309c752007-05-01 02:13:26 +0000844 Code = bitc::FUNC_CODE_INST_INVOKE;
Chris Lattnera9bb7132007-05-08 05:38:01 +0000845
Devang Patel05988662008-09-25 21:00:45 +0000846 Vals.push_back(VE.getAttributeID(II->getAttributes()));
Duncan Sandsdc024672007-11-27 13:23:08 +0000847 Vals.push_back(II->getCallingConv());
Gabor Greif8fe53b72009-01-07 22:39:29 +0000848 Vals.push_back(VE.getValueID(II->getNormalDest()));
849 Vals.push_back(VE.getValueID(II->getUnwindDest()));
Gabor Greif75a46eb2009-01-16 18:40:27 +0000850 PushValueAndType(Callee, InstID, Vals, VE);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000851
Chris Lattnerd309c752007-05-01 02:13:26 +0000852 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +0000853 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
854 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
855
856 // Emit type/value pairs for varargs params.
857 if (FTy->isVarArg()) {
Chris Lattner7337ab92007-05-06 00:00:00 +0000858 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
859 i != e; ++i)
860 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
Chris Lattnerd309c752007-05-01 02:13:26 +0000861 }
862 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000863 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000864 case Instruction::Unwind:
865 Code = bitc::FUNC_CODE_INST_UNWIND;
866 break;
867 case Instruction::Unreachable:
868 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattner94687ac2007-05-06 01:28:01 +0000869 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000870 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000871
872 case Instruction::PHI:
873 Code = bitc::FUNC_CODE_INST_PHI;
874 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000875 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
876 Vals.push_back(VE.getValueID(I.getOperand(i)));
877 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000878
Chris Lattnerd309c752007-05-01 02:13:26 +0000879 case Instruction::Malloc:
880 Code = bitc::FUNC_CODE_INST_MALLOC;
881 Vals.push_back(VE.getTypeID(I.getType()));
882 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
883 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
884 break;
885
886 case Instruction::Free:
887 Code = bitc::FUNC_CODE_INST_FREE;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000888 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000889 break;
890
891 case Instruction::Alloca:
892 Code = bitc::FUNC_CODE_INST_ALLOCA;
893 Vals.push_back(VE.getTypeID(I.getType()));
894 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
895 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
896 break;
897
898 case Instruction::Load:
899 Code = bitc::FUNC_CODE_INST_LOAD;
Chris Lattner7337ab92007-05-06 00:00:00 +0000900 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
901 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
902
Chris Lattnerd309c752007-05-01 02:13:26 +0000903 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
904 Vals.push_back(cast<LoadInst>(I).isVolatile());
905 break;
906 case Instruction::Store:
Christopher Lambfe63fb92007-12-11 08:59:05 +0000907 Code = bitc::FUNC_CODE_INST_STORE2;
908 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
909 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
Chris Lattnerd309c752007-05-01 02:13:26 +0000910 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
911 Vals.push_back(cast<StoreInst>(I).isVolatile());
912 break;
913 case Instruction::Call: {
Chris Lattnera9bb7132007-05-08 05:38:01 +0000914 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
915 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
916
Chris Lattnerd309c752007-05-01 02:13:26 +0000917 Code = bitc::FUNC_CODE_INST_CALL;
Chris Lattnera9bb7132007-05-08 05:38:01 +0000918
Duncan Sandsdc024672007-11-27 13:23:08 +0000919 const CallInst *CI = cast<CallInst>(&I);
Devang Patel05988662008-09-25 21:00:45 +0000920 Vals.push_back(VE.getAttributeID(CI->getAttributes()));
Duncan Sandsdc024672007-11-27 13:23:08 +0000921 Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
922 PushValueAndType(CI->getOperand(0), InstID, Vals, VE); // Callee
Chris Lattnerd309c752007-05-01 02:13:26 +0000923
924 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +0000925 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
926 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
927
Chris Lattner60ce9b52007-05-01 07:03:37 +0000928 // Emit type/value pairs for varargs params.
929 if (FTy->isVarArg()) {
930 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
Chris Lattner60ce9b52007-05-01 07:03:37 +0000931 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
Chris Lattner7337ab92007-05-06 00:00:00 +0000932 i != e; ++i)
933 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
Chris Lattnerd309c752007-05-01 02:13:26 +0000934 }
935 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000936 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000937 case Instruction::VAArg:
938 Code = bitc::FUNC_CODE_INST_VAARG;
939 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
940 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
941 Vals.push_back(VE.getTypeID(I.getType())); // restype.
942 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000943 }
944
945 Stream.EmitRecord(Code, Vals, AbbrevToUse);
946 Vals.clear();
947}
948
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000949// Emit names for globals/functions etc.
950static void WriteValueSymbolTable(const ValueSymbolTable &VST,
951 const ValueEnumerator &VE,
952 BitstreamWriter &Stream) {
953 if (VST.empty()) return;
Chris Lattnerff294a42007-05-05 01:26:50 +0000954 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2e7899d2007-05-04 20:34:50 +0000955
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000956 // FIXME: Set up the abbrev, we know how many values there are!
957 // FIXME: We know if the type names can use 7-bit ascii.
958 SmallVector<unsigned, 64> NameVals;
959
960 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
961 SI != SE; ++SI) {
Chris Lattner59698302007-05-04 20:52:02 +0000962
963 const ValueName &Name = *SI;
964
965 // Figure out the encoding to use for the name.
966 bool is7Bit = true;
Chris Lattnerff294a42007-05-05 01:26:50 +0000967 bool isChar6 = true;
968 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
969 C != E; ++C) {
970 if (isChar6)
971 isChar6 = BitCodeAbbrevOp::isChar6(*C);
972 if ((unsigned char)*C & 128) {
Chris Lattner59698302007-05-04 20:52:02 +0000973 is7Bit = false;
Chris Lattnerff294a42007-05-05 01:26:50 +0000974 break; // don't bother scanning the rest.
Chris Lattner59698302007-05-04 20:52:02 +0000975 }
Chris Lattnerff294a42007-05-05 01:26:50 +0000976 }
Chris Lattner59698302007-05-04 20:52:02 +0000977
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000978 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000979
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000980 // VST_ENTRY: [valueid, namechar x N]
981 // VST_BBENTRY: [bbid, namechar x N]
Chris Lattnere825ed52007-05-03 22:18:21 +0000982 unsigned Code;
983 if (isa<BasicBlock>(SI->getValue())) {
984 Code = bitc::VST_CODE_BBENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000985 if (isChar6)
986 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000987 } else {
988 Code = bitc::VST_CODE_ENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000989 if (isChar6)
990 AbbrevToUse = VST_ENTRY_6_ABBREV;
991 else if (is7Bit)
992 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000993 }
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000994
Chris Lattnere825ed52007-05-03 22:18:21 +0000995 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +0000996 for (const char *P = Name.getKeyData(),
997 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000998 NameVals.push_back((unsigned char)*P);
999
1000 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +00001001 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001002 NameVals.clear();
1003 }
1004 Stream.ExitBlock();
1005}
1006
Chris Lattner8d35c792007-04-26 03:50:57 +00001007/// WriteFunction - Emit a function body to the module stream.
Chris Lattner198f34a2007-04-26 03:27:58 +00001008static void WriteFunction(const Function &F, ValueEnumerator &VE,
1009 BitstreamWriter &Stream) {
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001010 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chris Lattner8d35c792007-04-26 03:50:57 +00001011 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001012
1013 SmallVector<unsigned, 64> Vals;
1014
1015 // Emit the number of basic blocks, so the reader can create them ahead of
1016 // time.
1017 Vals.push_back(VE.getBasicBlocks().size());
1018 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1019 Vals.clear();
1020
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001021 // If there are function-local constants, emit them now.
1022 unsigned CstStart, CstEnd;
1023 VE.getFunctionConstantRange(CstStart, CstEnd);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001024 WriteConstants(CstStart, CstEnd, VE, Stream, false);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001025
Chris Lattner7337ab92007-05-06 00:00:00 +00001026 // Keep a running idea of what the instruction ID is.
1027 unsigned InstID = CstEnd;
1028
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001029 // Finally, emit all the instructions, in order.
Nick Lewycky280a6e62008-04-25 16:53:59 +00001030 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner7337ab92007-05-06 00:00:00 +00001031 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1032 I != E; ++I) {
1033 WriteInstruction(*I, InstID, VE, Stream, Vals);
1034 if (I->getType() != Type::VoidTy)
1035 ++InstID;
1036 }
Chris Lattner198f34a2007-04-26 03:27:58 +00001037
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001038 // Emit names for all the instructions etc.
1039 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1040
Chris Lattner8d35c792007-04-26 03:50:57 +00001041 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001042 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +00001043}
1044
1045/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
1046static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
1047 const ValueEnumerator &VE,
1048 BitstreamWriter &Stream) {
1049 if (TST.empty()) return;
1050
1051 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
1052
Chris Lattner7a263ea2007-05-05 00:47:19 +00001053 // 7-bit fixed width VST_CODE_ENTRY strings.
1054 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1055 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1056 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1057 Log2_32_Ceil(VE.getTypes().size()+1)));
1058 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1059 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1060 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner198f34a2007-04-26 03:27:58 +00001061
1062 SmallVector<unsigned, 64> NameVals;
1063
1064 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
1065 TI != TE; ++TI) {
Chris Lattner7a263ea2007-05-05 00:47:19 +00001066 // TST_ENTRY: [typeid, namechar x N]
Chris Lattner198f34a2007-04-26 03:27:58 +00001067 NameVals.push_back(VE.getTypeID(TI->second));
1068
1069 const std::string &Str = TI->first;
Chris Lattner7a263ea2007-05-05 00:47:19 +00001070 bool is7Bit = true;
1071 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1072 NameVals.push_back((unsigned char)Str[i]);
1073 if (Str[i] & 128)
1074 is7Bit = false;
1075 }
Chris Lattner198f34a2007-04-26 03:27:58 +00001076
1077 // Emit the finished record.
Chris Lattner7a263ea2007-05-05 00:47:19 +00001078 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
Chris Lattner198f34a2007-04-26 03:27:58 +00001079 NameVals.clear();
1080 }
1081
1082 Stream.ExitBlock();
1083}
1084
Chris Lattner07faafc2007-05-04 18:26:27 +00001085// Emit blockinfo, which defines the standard abbreviations etc.
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001086static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
Chris Lattner07faafc2007-05-04 18:26:27 +00001087 // We only want to emit block info records for blocks that have multiple
1088 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1089 // blocks can defined their abbrevs inline.
Chris Lattnere17b6582007-05-05 00:17:00 +00001090 Stream.EnterBlockInfoBlock(2);
Chris Lattner07faafc2007-05-04 18:26:27 +00001091
Chris Lattnere17b6582007-05-05 00:17:00 +00001092 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1093 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1094 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1095 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1096 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1097 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1098 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1099 Abbv) != VST_ENTRY_8_ABBREV)
1100 assert(0 && "Unexpected abbrev ordering!");
1101 }
1102
1103 { // 7-bit fixed width VST_ENTRY strings.
1104 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1105 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1106 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1107 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1108 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1109 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1110 Abbv) != VST_ENTRY_7_ABBREV)
1111 assert(0 && "Unexpected abbrev ordering!");
1112 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001113 { // 6-bit char6 VST_ENTRY strings.
1114 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1115 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1116 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1117 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1118 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1119 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1120 Abbv) != VST_ENTRY_6_ABBREV)
1121 assert(0 && "Unexpected abbrev ordering!");
1122 }
1123 { // 6-bit char6 VST_BBENTRY strings.
Chris Lattnere17b6582007-05-05 00:17:00 +00001124 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1125 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1126 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1127 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnerff294a42007-05-05 01:26:50 +00001128 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Chris Lattnere17b6582007-05-05 00:17:00 +00001129 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnerff294a42007-05-05 01:26:50 +00001130 Abbv) != VST_BBENTRY_6_ABBREV)
Chris Lattnere17b6582007-05-05 00:17:00 +00001131 assert(0 && "Unexpected abbrev ordering!");
1132 }
1133
Chris Lattner440168b2007-05-05 07:44:49 +00001134
1135
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001136 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1137 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1138 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1139 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1140 Log2_32_Ceil(VE.getTypes().size()+1)));
1141 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1142 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1143 assert(0 && "Unexpected abbrev ordering!");
1144 }
1145
1146 { // INTEGER abbrev for CONSTANTS_BLOCK.
1147 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1148 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1149 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1150 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1151 Abbv) != CONSTANTS_INTEGER_ABBREV)
1152 assert(0 && "Unexpected abbrev ordering!");
1153 }
1154
1155 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1156 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1157 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1159 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1160 Log2_32_Ceil(VE.getTypes().size()+1)));
1161 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1162
1163 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1164 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1165 assert(0 && "Unexpected abbrev ordering!");
1166 }
1167 { // NULL abbrev for CONSTANTS_BLOCK.
1168 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1169 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1170 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1171 Abbv) != CONSTANTS_NULL_Abbrev)
1172 assert(0 && "Unexpected abbrev ordering!");
1173 }
1174
Chris Lattner440168b2007-05-05 07:44:49 +00001175 // FIXME: This should only use space for first class types!
1176
1177 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1178 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1179 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattner440168b2007-05-05 07:44:49 +00001180 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1181 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1182 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1183 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1184 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1185 assert(0 && "Unexpected abbrev ordering!");
1186 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001187 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1188 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1189 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1190 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1191 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1192 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1193 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1194 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1195 assert(0 && "Unexpected abbrev ordering!");
1196 }
1197 { // INST_CAST abbrev for FUNCTION_BLOCK.
1198 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1199 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1200 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1201 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1202 Log2_32_Ceil(VE.getTypes().size()+1)));
1203 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1204 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1205 Abbv) != FUNCTION_INST_CAST_ABBREV)
1206 assert(0 && "Unexpected abbrev ordering!");
1207 }
1208
Chris Lattner94687ac2007-05-06 01:28:01 +00001209 { // INST_RET abbrev for FUNCTION_BLOCK.
1210 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1211 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1212 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1213 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1214 assert(0 && "Unexpected abbrev ordering!");
1215 }
1216 { // INST_RET abbrev for FUNCTION_BLOCK.
1217 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1218 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1219 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1220 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1221 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1222 assert(0 && "Unexpected abbrev ordering!");
1223 }
1224 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1225 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1226 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1227 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1228 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1229 assert(0 && "Unexpected abbrev ordering!");
1230 }
Chris Lattner440168b2007-05-05 07:44:49 +00001231
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001232 Stream.ExitBlock();
1233}
1234
1235
1236/// WriteModule - Emit the specified module to the bitstream.
1237static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1238 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1239
1240 // Emit the version number if it is non-zero.
1241 if (CurVersion) {
1242 SmallVector<unsigned, 1> Vals;
1243 Vals.push_back(CurVersion);
1244 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1245 }
1246
1247 // Analyze the module, enumerating globals, functions, etc.
1248 ValueEnumerator VE(M);
1249
1250 // Emit blockinfo, which defines the standard abbreviations etc.
1251 WriteBlockInfo(VE, Stream);
1252
1253 // Emit information about parameter attributes.
Devang Patel05988662008-09-25 21:00:45 +00001254 WriteAttributeTable(VE, Stream);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001255
1256 // Emit information describing all of the types in the module.
1257 WriteTypeTable(VE, Stream);
1258
1259 // Emit top-level description of module, including target triple, inline asm,
1260 // descriptors for global variables, and function prototype info.
1261 WriteModuleInfo(M, VE, Stream);
1262
1263 // Emit constants.
1264 WriteModuleConstants(VE, Stream);
1265
1266 // If we have any aggregate values in the value table, purge them - these can
1267 // only be used to initialize global variables. Doing so makes the value
1268 // namespace smaller for code in functions.
1269 int NumNonAggregates = VE.PurgeAggregateValues();
1270 if (NumNonAggregates != -1) {
1271 SmallVector<unsigned, 1> Vals;
1272 Vals.push_back(NumNonAggregates);
1273 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1274 }
1275
1276 // Emit function bodies.
1277 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1278 if (!I->isDeclaration())
1279 WriteFunction(*I, VE, Stream);
1280
1281 // Emit the type symbol table information.
1282 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1283
1284 // Emit names for globals/functions etc.
1285 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1286
Chris Lattner07faafc2007-05-04 18:26:27 +00001287 Stream.ExitBlock();
1288}
1289
Chris Lattner6fa6a322008-07-09 05:14:23 +00001290/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1291/// header and trailer to make it compatible with the system archiver. To do
1292/// this we emit the following header, and then emit a trailer that pads the
1293/// file out to be a multiple of 16 bytes.
1294///
1295/// struct bc_header {
1296/// uint32_t Magic; // 0x0B17C0DE
1297/// uint32_t Version; // Version, currently always 0.
1298/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1299/// uint32_t BitcodeSize; // Size of traditional bitcode file.
1300/// uint32_t CPUType; // CPU specifier.
1301/// ... potentially more later ...
1302/// };
1303enum {
1304 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1305 DarwinBCHeaderSize = 5*4
1306};
1307
1308static void EmitDarwinBCHeader(BitstreamWriter &Stream,
1309 const std::string &TT) {
1310 unsigned CPUType = ~0U;
1311
1312 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*. The CPUType is a
1313 // magic number from /usr/include/mach/machine.h. It is ok to reproduce the
1314 // specific constants here because they are implicitly part of the Darwin ABI.
1315 enum {
1316 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
1317 DARWIN_CPU_TYPE_X86 = 7,
1318 DARWIN_CPU_TYPE_POWERPC = 18
1319 };
1320
1321 if (TT.find("x86_64-") == 0)
1322 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
1323 else if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
1324 TT[4] == '-' && TT[1] - '3' < 6)
1325 CPUType = DARWIN_CPU_TYPE_X86;
1326 else if (TT.find("powerpc-") == 0)
1327 CPUType = DARWIN_CPU_TYPE_POWERPC;
1328 else if (TT.find("powerpc64-") == 0)
1329 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
1330
1331 // Traditional Bitcode starts after header.
1332 unsigned BCOffset = DarwinBCHeaderSize;
1333
1334 Stream.Emit(0x0B17C0DE, 32);
1335 Stream.Emit(0 , 32); // Version.
1336 Stream.Emit(BCOffset , 32);
1337 Stream.Emit(0 , 32); // Filled in later.
1338 Stream.Emit(CPUType , 32);
1339}
1340
1341/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
1342/// finalize the header.
1343static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
1344 // Update the size field in the header.
1345 Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
1346
1347 // If the file is not a multiple of 16 bytes, insert dummy padding.
1348 while (BufferSize & 15) {
1349 Stream.Emit(0, 8);
1350 ++BufferSize;
1351 }
1352}
1353
Chris Lattner07faafc2007-05-04 18:26:27 +00001354
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001355/// WriteBitcodeToFile - Write the specified module to the specified output
1356/// stream.
1357void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
Daniel Dunbard1ce3b42008-10-22 17:39:14 +00001358 raw_os_ostream RawOut(Out);
Daniel Dunbare2e9d8e2008-10-23 19:37:34 +00001359 // If writing to stdout, set binary mode.
1360 if (llvm::cout == Out)
1361 sys::Program::ChangeStdoutToBinary();
Daniel Dunbard1ce3b42008-10-22 17:39:14 +00001362 WriteBitcodeToFile(M, RawOut);
1363}
1364
1365/// WriteBitcodeToFile - Write the specified module to the specified output
1366/// stream.
1367void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001368 std::vector<unsigned char> Buffer;
1369 BitstreamWriter Stream(Buffer);
1370
1371 Buffer.reserve(256*1024);
Chris Lattner38e77212008-12-19 18:37:59 +00001372
1373 WriteBitcodeToStream( M, Stream );
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001374
Chris Lattner38e77212008-12-19 18:37:59 +00001375 // If writing to stdout, set binary mode.
1376 if (&llvm::outs() == &Out)
1377 sys::Program::ChangeStdoutToBinary();
1378
1379 // Write the generated bitstream to "Out".
1380 Out.write((char*)&Buffer.front(), Buffer.size());
1381
1382 // Make sure it hits disk now.
1383 Out.flush();
1384}
1385
1386/// WriteBitcodeToStream - Write the specified module to the specified output
1387/// stream.
1388void llvm::WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) {
Chris Lattner6fa6a322008-07-09 05:14:23 +00001389 // If this is darwin, emit a file header and trailer if needed.
1390 bool isDarwin = M->getTargetTriple().find("-darwin") != std::string::npos;
1391 if (isDarwin)
1392 EmitDarwinBCHeader(Stream, M->getTargetTriple());
1393
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001394 // Emit the file header.
1395 Stream.Emit((unsigned)'B', 8);
1396 Stream.Emit((unsigned)'C', 8);
1397 Stream.Emit(0x0, 4);
1398 Stream.Emit(0xC, 4);
1399 Stream.Emit(0xE, 4);
1400 Stream.Emit(0xD, 4);
1401
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001402 // Emit the module.
1403 WriteModule(M, Stream);
Chris Lattner6fa6a322008-07-09 05:14:23 +00001404
1405 if (isDarwin)
Chris Lattner38e77212008-12-19 18:37:59 +00001406 EmitDarwinBCTrailer(Stream, Stream.getBuffer().size());
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001407}