blob: 23e70117a1dd896974e23a0b02fd5e1c19760366 [file] [log] [blame]
Chris Lattner1a9df8e2007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerfd57cec2007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Bitcode/ReaderWriter.h"
15#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner47f96bf2007-04-23 01:01:37 +000016#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000017#include "ValueEnumerator.h"
Chris Lattner2edd22b2007-04-24 00:16:04 +000018#include "llvm/Constants.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000020#include "llvm/Instructions.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000021#include "llvm/Module.h"
Chris Lattnerf0a65312007-05-04 02:59:04 +000022#include "llvm/ParameterAttributes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000023#include "llvm/TypeSymbolTable.h"
Chris Lattnerb992be12007-04-23 20:35:01 +000024#include "llvm/ValueSymbolTable.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000025#include "llvm/Support/MathExtras.h"
26using namespace llvm;
27
Chris Lattner59698302007-05-04 20:52:02 +000028/// These are manifest constants used by the bitcode writer. They do not need to
29/// be kept in sync with the reader, but need to be consistent within this file.
30enum {
31 CurVersion = 0,
32
33 // VALUE_SYMTAB_BLOCK abbrev id's.
34 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattner2453f712007-05-04 20:58:35 +000035 VST_ENTRY_7_ABBREV,
Chris Lattnerff294a42007-05-05 01:26:50 +000036 VST_ENTRY_6_ABBREV,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000037 VST_BBENTRY_6_ABBREV,
38
39 // CONSTANTS_BLOCK abbrev id's.
40 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
41 CONSTANTS_INTEGER_ABBREV,
42 CONSTANTS_CE_CAST_Abbrev,
Chris Lattner440168b2007-05-05 07:44:49 +000043 CONSTANTS_NULL_Abbrev,
44
45 // FUNCTION_BLOCK abbrev id's.
46 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV
Chris Lattner59698302007-05-04 20:52:02 +000047};
48
Chris Lattnerfd57cec2007-04-22 06:24:45 +000049
Chris Lattnerf581c3b2007-04-24 07:07:11 +000050static unsigned GetEncodedCastOpcode(unsigned Opcode) {
51 switch (Opcode) {
52 default: assert(0 && "Unknown cast instruction!");
53 case Instruction::Trunc : return bitc::CAST_TRUNC;
54 case Instruction::ZExt : return bitc::CAST_ZEXT;
55 case Instruction::SExt : return bitc::CAST_SEXT;
56 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
57 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
58 case Instruction::UIToFP : return bitc::CAST_UITOFP;
59 case Instruction::SIToFP : return bitc::CAST_SITOFP;
60 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
61 case Instruction::FPExt : return bitc::CAST_FPEXT;
62 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
63 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
64 case Instruction::BitCast : return bitc::CAST_BITCAST;
65 }
66}
67
68static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
69 switch (Opcode) {
70 default: assert(0 && "Unknown binary instruction!");
71 case Instruction::Add: return bitc::BINOP_ADD;
72 case Instruction::Sub: return bitc::BINOP_SUB;
73 case Instruction::Mul: return bitc::BINOP_MUL;
74 case Instruction::UDiv: return bitc::BINOP_UDIV;
75 case Instruction::FDiv:
76 case Instruction::SDiv: return bitc::BINOP_SDIV;
77 case Instruction::URem: return bitc::BINOP_UREM;
78 case Instruction::FRem:
79 case Instruction::SRem: return bitc::BINOP_SREM;
80 case Instruction::Shl: return bitc::BINOP_SHL;
81 case Instruction::LShr: return bitc::BINOP_LSHR;
82 case Instruction::AShr: return bitc::BINOP_ASHR;
83 case Instruction::And: return bitc::BINOP_AND;
84 case Instruction::Or: return bitc::BINOP_OR;
85 case Instruction::Xor: return bitc::BINOP_XOR;
86 }
87}
88
89
90
Chris Lattnerfd57cec2007-04-22 06:24:45 +000091static void WriteStringRecord(unsigned Code, const std::string &Str,
92 unsigned AbbrevToUse, BitstreamWriter &Stream) {
93 SmallVector<unsigned, 64> Vals;
94
Chris Lattner15e6d172007-05-04 19:11:41 +000095 // Code: [strchar x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +000096 for (unsigned i = 0, e = Str.size(); i != e; ++i)
97 Vals.push_back(Str[i]);
98
99 // Emit the finished record.
100 Stream.EmitRecord(Code, Vals, AbbrevToUse);
101}
102
Chris Lattner62bbeea2007-05-04 00:44:52 +0000103// Emit information about parameter attributes.
104static void WriteParamAttrTable(const ValueEnumerator &VE,
105 BitstreamWriter &Stream) {
106 const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
107 if (Attrs.empty()) return;
108
Chris Lattnerf0a65312007-05-04 02:59:04 +0000109 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
110
111 SmallVector<uint64_t, 64> Record;
112 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
113 const ParamAttrsList *A = Attrs[i];
114 for (unsigned op = 0, e = A->size(); op != e; ++op) {
115 Record.push_back(A->getParamIndex(op));
Chris Lattnerf6f9cd12007-05-04 03:14:09 +0000116 Record.push_back(A->getParamAttrsAtIndex(op));
Chris Lattnerf0a65312007-05-04 02:59:04 +0000117 }
118
119 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
120 Record.clear();
121 }
Chris Lattner62bbeea2007-05-04 00:44:52 +0000122
Chris Lattnerf0a65312007-05-04 02:59:04 +0000123 Stream.ExitBlock();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000124}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000125
126/// WriteTypeTable - Write out the type table for a module.
127static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
128 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
129
130 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
131 SmallVector<uint64_t, 64> TypeVals;
132
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000133 // Abbrev for TYPE_CODE_POINTER.
134 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
135 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
137 Log2_32_Ceil(VE.getTypes().size()+1)));
138 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
139
140 // Abbrev for TYPE_CODE_FUNCTION.
141 Abbv = new BitCodeAbbrev();
142 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
143 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
144 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
145 Log2_32_Ceil(VE.getParamAttrs().size()+1)));
146 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
147 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
148 Log2_32_Ceil(VE.getTypes().size()+1)));
149 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
150
151 // Abbrev for TYPE_CODE_STRUCT.
152 Abbv = new BitCodeAbbrev();
153 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
154 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
155 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
156 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
157 Log2_32_Ceil(VE.getTypes().size()+1)));
158 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
159
160 // Abbrev for TYPE_CODE_ARRAY.
161 Abbv = new BitCodeAbbrev();
162 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
163 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
164 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
165 Log2_32_Ceil(VE.getTypes().size()+1)));
166 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000167
168 // Emit an entry count so the reader can reserve space.
169 TypeVals.push_back(TypeList.size());
170 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
171 TypeVals.clear();
172
173 // Loop over all of the types, emitting each in turn.
174 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
175 const Type *T = TypeList[i].first;
176 int AbbrevToUse = 0;
177 unsigned Code = 0;
178
179 switch (T->getTypeID()) {
180 case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
181 default: assert(0 && "Unknown type!");
182 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
183 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
184 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
185 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
186 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
187 case Type::IntegerTyID:
188 // INTEGER: [width]
189 Code = bitc::TYPE_CODE_INTEGER;
190 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
191 break;
192 case Type::PointerTyID:
193 // POINTER: [pointee type]
194 Code = bitc::TYPE_CODE_POINTER;
195 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000196 AbbrevToUse = PtrAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000197 break;
198
199 case Type::FunctionTyID: {
200 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000201 // FUNCTION: [isvararg, attrid, retty, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000202 Code = bitc::TYPE_CODE_FUNCTION;
203 TypeVals.push_back(FT->isVarArg());
Chris Lattner9113e732007-05-04 03:41:34 +0000204 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000205 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000206 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
207 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000208 AbbrevToUse = FunctionAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000209 break;
210 }
211 case Type::StructTyID: {
212 const StructType *ST = cast<StructType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000213 // STRUCT: [ispacked, eltty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000214 Code = bitc::TYPE_CODE_STRUCT;
215 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000216 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000217 for (StructType::element_iterator I = ST->element_begin(),
218 E = ST->element_end(); I != E; ++I)
219 TypeVals.push_back(VE.getTypeID(*I));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000220 AbbrevToUse = StructAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000221 break;
222 }
223 case Type::ArrayTyID: {
224 const ArrayType *AT = cast<ArrayType>(T);
225 // ARRAY: [numelts, eltty]
226 Code = bitc::TYPE_CODE_ARRAY;
227 TypeVals.push_back(AT->getNumElements());
228 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000229 AbbrevToUse = ArrayAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000230 break;
231 }
232 case Type::VectorTyID: {
233 const VectorType *VT = cast<VectorType>(T);
234 // VECTOR [numelts, eltty]
235 Code = bitc::TYPE_CODE_VECTOR;
236 TypeVals.push_back(VT->getNumElements());
237 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
238 break;
239 }
240 }
241
242 // Emit the finished record.
243 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
244 TypeVals.clear();
245 }
246
247 Stream.ExitBlock();
248}
249
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000250static unsigned getEncodedLinkage(const GlobalValue *GV) {
251 switch (GV->getLinkage()) {
252 default: assert(0 && "Invalid linkage!");
253 case GlobalValue::ExternalLinkage: return 0;
254 case GlobalValue::WeakLinkage: return 1;
255 case GlobalValue::AppendingLinkage: return 2;
256 case GlobalValue::InternalLinkage: return 3;
257 case GlobalValue::LinkOnceLinkage: return 4;
258 case GlobalValue::DLLImportLinkage: return 5;
259 case GlobalValue::DLLExportLinkage: return 6;
260 case GlobalValue::ExternalWeakLinkage: return 7;
261 }
262}
263
264static unsigned getEncodedVisibility(const GlobalValue *GV) {
265 switch (GV->getVisibility()) {
266 default: assert(0 && "Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000267 case GlobalValue::DefaultVisibility: return 0;
268 case GlobalValue::HiddenVisibility: return 1;
269 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000270 }
271}
272
273// Emit top-level description of module, including target triple, inline asm,
274// descriptors for global variables, and function prototype info.
275static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
276 BitstreamWriter &Stream) {
277 // Emit the list of dependent libraries for the Module.
278 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
279 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
280
281 // Emit various pieces of data attached to a module.
282 if (!M->getTargetTriple().empty())
283 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
284 0/*TODO*/, Stream);
285 if (!M->getDataLayout().empty())
286 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
287 0/*TODO*/, Stream);
288 if (!M->getModuleInlineAsm().empty())
289 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
290 0/*TODO*/, Stream);
291
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000292 // Emit information about sections, computing how many there are. Also
293 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000294 std::map<std::string, unsigned> SectionMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000295 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000296 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000297 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
298 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000299 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000300 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000301
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000302 if (!GV->hasSection()) continue;
303 // Give section names unique ID's.
304 unsigned &Entry = SectionMap[GV->getSection()];
305 if (Entry != 0) continue;
306 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
307 0/*TODO*/, Stream);
308 Entry = SectionMap.size();
309 }
310 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000311 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000312 if (!F->hasSection()) continue;
313 // Give section names unique ID's.
314 unsigned &Entry = SectionMap[F->getSection()];
315 if (Entry != 0) continue;
316 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
317 0/*TODO*/, Stream);
318 Entry = SectionMap.size();
319 }
320
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000321 // Emit abbrev for globals, now that we know # sections and max alignment.
322 unsigned SimpleGVarAbbrev = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000323 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000324 // Add an abbrev for common globals with no visibility or thread localness.
325 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
326 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000327 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000328 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000329 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000330 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000331 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
332 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000333 Abbv->Add(BitCodeAbbrevOp(0));
334 else {
335 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000337 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000338 }
339 if (SectionMap.empty()) // Section.
340 Abbv->Add(BitCodeAbbrevOp(0));
341 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000342 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000343 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000344 // Don't bother emitting vis + thread local.
345 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
346 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000347
348 // Emit the global variable information.
349 SmallVector<unsigned, 64> Vals;
350 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
351 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000352 unsigned AbbrevToUse = 0;
353
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000354 // GLOBALVAR: [type, isconst, initid,
355 // linkage, alignment, section, visibility, threadlocal]
356 Vals.push_back(VE.getTypeID(GV->getType()));
357 Vals.push_back(GV->isConstant());
358 Vals.push_back(GV->isDeclaration() ? 0 :
359 (VE.getValueID(GV->getInitializer()) + 1));
360 Vals.push_back(getEncodedLinkage(GV));
361 Vals.push_back(Log2_32(GV->getAlignment())+1);
362 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000363 if (GV->isThreadLocal() ||
364 GV->getVisibility() != GlobalValue::DefaultVisibility) {
365 Vals.push_back(getEncodedVisibility(GV));
366 Vals.push_back(GV->isThreadLocal());
367 } else {
368 AbbrevToUse = SimpleGVarAbbrev;
369 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000370
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000371 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
372 Vals.clear();
373 }
374
375 // Emit the function proto information.
376 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
377 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
378 // visibility]
379 Vals.push_back(VE.getTypeID(F->getType()));
380 Vals.push_back(F->getCallingConv());
381 Vals.push_back(F->isDeclaration());
382 Vals.push_back(getEncodedLinkage(F));
383 Vals.push_back(Log2_32(F->getAlignment())+1);
384 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
385 Vals.push_back(getEncodedVisibility(F));
386
387 unsigned AbbrevToUse = 0;
388 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
389 Vals.clear();
390 }
Chris Lattner07d98b42007-04-26 02:46:40 +0000391
392
393 // Emit the alias information.
394 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
395 AI != E; ++AI) {
396 Vals.push_back(VE.getTypeID(AI->getType()));
397 Vals.push_back(VE.getValueID(AI->getAliasee()));
398 Vals.push_back(getEncodedLinkage(AI));
399 unsigned AbbrevToUse = 0;
400 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
401 Vals.clear();
402 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000403}
404
405
Chris Lattner2edd22b2007-04-24 00:16:04 +0000406static void WriteConstants(unsigned FirstVal, unsigned LastVal,
407 const ValueEnumerator &VE,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000408 BitstreamWriter &Stream, bool isGlobal) {
Chris Lattner2edd22b2007-04-24 00:16:04 +0000409 if (FirstVal == LastVal) return;
410
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000411 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Chris Lattnerb992be12007-04-23 20:35:01 +0000412
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000413 unsigned AggregateAbbrev = 0;
414 unsigned GEPAbbrev = 0;
415 // If this is a constant pool for the module, emit module-specific abbrevs.
416 if (isGlobal) {
417 // Abbrev for CST_CODE_AGGREGATE.
418 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
419 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
420 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
421 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
422 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
423 }
424
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000425 // FIXME: Install and use abbrevs to reduce size. Install them globally so
426 // they don't need to be reemitted for each function body.
Chris Lattner2edd22b2007-04-24 00:16:04 +0000427
428 SmallVector<uint64_t, 64> Record;
429
430 const ValueEnumerator::ValueList &Vals = VE.getValues();
431 const Type *LastTy = 0;
432 for (unsigned i = FirstVal; i != LastVal; ++i) {
433 const Value *V = Vals[i].first;
434 // If we need to switch types, do so now.
435 if (V->getType() != LastTy) {
436 LastTy = V->getType();
437 Record.push_back(VE.getTypeID(LastTy));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000438 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
439 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000440 Record.clear();
441 }
442
443 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
444 assert(0 && IA && "FIXME: Inline asm writing unimp!");
445 continue;
446 }
447 const Constant *C = cast<Constant>(V);
448 unsigned Code = -1U;
449 unsigned AbbrevToUse = 0;
450 if (C->isNullValue()) {
451 Code = bitc::CST_CODE_NULL;
452 } else if (isa<UndefValue>(C)) {
453 Code = bitc::CST_CODE_UNDEF;
454 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
455 if (IV->getBitWidth() <= 64) {
456 int64_t V = IV->getSExtValue();
457 if (V >= 0)
458 Record.push_back(V << 1);
459 else
460 Record.push_back((-V << 1) | 1);
461 Code = bitc::CST_CODE_INTEGER;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000462 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000463 } else { // Wide integers, > 64 bits in size.
464 // We have an arbitrary precision integer value to write whose
465 // bit width is > 64. However, in canonical unsigned integer
466 // format it is likely that the high bits are going to be zero.
467 // So, we only write the number of active words.
468 unsigned NWords = IV->getValue().getActiveWords();
469 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000470 for (unsigned i = 0; i != NWords; ++i) {
471 int64_t V = RawWords[i];
472 if (V >= 0)
473 Record.push_back(V << 1);
474 else
475 Record.push_back((-V << 1) | 1);
476 }
477 Code = bitc::CST_CODE_WIDE_INTEGER;
478 }
479 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
480 Code = bitc::CST_CODE_FLOAT;
481 if (CFP->getType() == Type::FloatTy) {
482 Record.push_back(FloatToBits((float)CFP->getValue()));
483 } else {
484 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
485 Record.push_back(DoubleToBits((double)CFP->getValue()));
486 }
487 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
488 isa<ConstantVector>(V)) {
489 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000490 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
491 Record.push_back(VE.getValueID(C->getOperand(i)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000492 AbbrevToUse = AggregateAbbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000493 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000494 switch (CE->getOpcode()) {
495 default:
496 if (Instruction::isCast(CE->getOpcode())) {
497 Code = bitc::CST_CODE_CE_CAST;
498 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
499 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
500 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000501 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000502 } else {
503 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
504 Code = bitc::CST_CODE_CE_BINOP;
505 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
506 Record.push_back(VE.getValueID(C->getOperand(0)));
507 Record.push_back(VE.getValueID(C->getOperand(1)));
508 }
509 break;
510 case Instruction::GetElementPtr:
511 Code = bitc::CST_CODE_CE_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000512 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
513 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
514 Record.push_back(VE.getValueID(C->getOperand(i)));
515 }
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000516 AbbrevToUse = GEPAbbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000517 break;
518 case Instruction::Select:
519 Code = bitc::CST_CODE_CE_SELECT;
520 Record.push_back(VE.getValueID(C->getOperand(0)));
521 Record.push_back(VE.getValueID(C->getOperand(1)));
522 Record.push_back(VE.getValueID(C->getOperand(2)));
523 break;
524 case Instruction::ExtractElement:
525 Code = bitc::CST_CODE_CE_EXTRACTELT;
526 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
527 Record.push_back(VE.getValueID(C->getOperand(0)));
528 Record.push_back(VE.getValueID(C->getOperand(1)));
529 break;
530 case Instruction::InsertElement:
531 Code = bitc::CST_CODE_CE_INSERTELT;
532 Record.push_back(VE.getValueID(C->getOperand(0)));
533 Record.push_back(VE.getValueID(C->getOperand(1)));
534 Record.push_back(VE.getValueID(C->getOperand(2)));
535 break;
536 case Instruction::ShuffleVector:
537 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
538 Record.push_back(VE.getValueID(C->getOperand(0)));
539 Record.push_back(VE.getValueID(C->getOperand(1)));
540 Record.push_back(VE.getValueID(C->getOperand(2)));
541 break;
542 case Instruction::ICmp:
543 case Instruction::FCmp:
544 Code = bitc::CST_CODE_CE_CMP;
545 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
546 Record.push_back(VE.getValueID(C->getOperand(0)));
547 Record.push_back(VE.getValueID(C->getOperand(1)));
548 Record.push_back(CE->getPredicate());
549 break;
550 }
Chris Lattner2edd22b2007-04-24 00:16:04 +0000551 } else {
552 assert(0 && "Unknown constant!");
553 }
554 Stream.EmitRecord(Code, Record, AbbrevToUse);
555 Record.clear();
556 }
557
558 Stream.ExitBlock();
559}
560
561static void WriteModuleConstants(const ValueEnumerator &VE,
562 BitstreamWriter &Stream) {
563 const ValueEnumerator::ValueList &Vals = VE.getValues();
564
565 // Find the first constant to emit, which is the first non-globalvalue value.
566 // We know globalvalues have been emitted by WriteModuleInfo.
567 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
568 if (!isa<GlobalValue>(Vals[i].first)) {
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000569 WriteConstants(i, Vals.size(), VE, Stream, true);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000570 return;
571 }
572 }
573}
Chris Lattnerb992be12007-04-23 20:35:01 +0000574
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000575/// WriteInstruction - Emit an instruction to the specified stream.
576static void WriteInstruction(const Instruction &I, ValueEnumerator &VE,
577 BitstreamWriter &Stream,
578 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000579 unsigned Code = 0;
580 unsigned AbbrevToUse = 0;
581 switch (I.getOpcode()) {
582 default:
583 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +0000584 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000585 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
586 Vals.push_back(VE.getTypeID(I.getType()));
587 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
588 Vals.push_back(VE.getValueID(I.getOperand(0)));
589 } else {
590 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +0000591 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000592 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
593 Vals.push_back(VE.getTypeID(I.getType()));
594 Vals.push_back(VE.getValueID(I.getOperand(0)));
595 Vals.push_back(VE.getValueID(I.getOperand(1)));
596 }
597 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000598
599 case Instruction::GetElementPtr:
600 Code = bitc::FUNC_CODE_INST_GEP;
Chris Lattnerd309c752007-05-01 02:13:26 +0000601 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
602 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
603 Vals.push_back(VE.getValueID(I.getOperand(i)));
604 }
605 break;
606 case Instruction::Select:
607 Code = bitc::FUNC_CODE_INST_SELECT;
608 Vals.push_back(VE.getTypeID(I.getType()));
609 Vals.push_back(VE.getValueID(I.getOperand(0)));
610 Vals.push_back(VE.getValueID(I.getOperand(1)));
611 Vals.push_back(VE.getValueID(I.getOperand(2)));
612 break;
613 case Instruction::ExtractElement:
614 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
615 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
616 Vals.push_back(VE.getValueID(I.getOperand(0)));
617 Vals.push_back(VE.getValueID(I.getOperand(1)));
618 break;
619 case Instruction::InsertElement:
620 Code = bitc::FUNC_CODE_INST_INSERTELT;
621 Vals.push_back(VE.getTypeID(I.getType()));
622 Vals.push_back(VE.getValueID(I.getOperand(0)));
623 Vals.push_back(VE.getValueID(I.getOperand(1)));
624 Vals.push_back(VE.getValueID(I.getOperand(2)));
625 break;
626 case Instruction::ShuffleVector:
627 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
628 Vals.push_back(VE.getTypeID(I.getType()));
629 Vals.push_back(VE.getValueID(I.getOperand(0)));
630 Vals.push_back(VE.getValueID(I.getOperand(1)));
631 Vals.push_back(VE.getValueID(I.getOperand(2)));
632 break;
633 case Instruction::ICmp:
634 case Instruction::FCmp:
635 Code = bitc::FUNC_CODE_INST_CMP;
636 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
637 Vals.push_back(VE.getValueID(I.getOperand(0)));
638 Vals.push_back(VE.getValueID(I.getOperand(1)));
639 Vals.push_back(cast<CmpInst>(I).getPredicate());
640 break;
641
642 case Instruction::Ret:
643 Code = bitc::FUNC_CODE_INST_RET;
644 if (I.getNumOperands()) {
645 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
646 Vals.push_back(VE.getValueID(I.getOperand(0)));
647 }
648 break;
649 case Instruction::Br:
650 Code = bitc::FUNC_CODE_INST_BR;
651 Vals.push_back(VE.getValueID(I.getOperand(0)));
652 if (cast<BranchInst>(I).isConditional()) {
653 Vals.push_back(VE.getValueID(I.getOperand(1)));
654 Vals.push_back(VE.getValueID(I.getOperand(2)));
655 }
656 break;
657 case Instruction::Switch:
658 Code = bitc::FUNC_CODE_INST_SWITCH;
659 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000660 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
661 Vals.push_back(VE.getValueID(I.getOperand(i)));
662 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000663 case Instruction::Invoke: {
Chris Lattnerd309c752007-05-01 02:13:26 +0000664 Code = bitc::FUNC_CODE_INST_INVOKE;
Chris Lattner76520192007-05-03 22:34:03 +0000665 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
Chris Lattnerd309c752007-05-01 02:13:26 +0000666 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
667 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
668 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal
669 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000670
Chris Lattnerd309c752007-05-01 02:13:26 +0000671 // Emit value #'s for the fixed parameters.
672 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
673 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
674 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
675 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
676
677 // Emit type/value pairs for varargs params.
678 if (FTy->isVarArg()) {
679 unsigned NumVarargs = I.getNumOperands()-3-FTy->getNumParams();
Chris Lattnerd309c752007-05-01 02:13:26 +0000680 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
681 i != e; ++i) {
682 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
683 Vals.push_back(VE.getValueID(I.getOperand(i)));
684 }
685 }
686 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000687 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000688 case Instruction::Unwind:
689 Code = bitc::FUNC_CODE_INST_UNWIND;
690 break;
691 case Instruction::Unreachable:
692 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
693 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000694
695 case Instruction::PHI:
696 Code = bitc::FUNC_CODE_INST_PHI;
697 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000698 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
699 Vals.push_back(VE.getValueID(I.getOperand(i)));
700 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000701
Chris Lattnerd309c752007-05-01 02:13:26 +0000702 case Instruction::Malloc:
703 Code = bitc::FUNC_CODE_INST_MALLOC;
704 Vals.push_back(VE.getTypeID(I.getType()));
705 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
706 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
707 break;
708
709 case Instruction::Free:
710 Code = bitc::FUNC_CODE_INST_FREE;
711 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
712 Vals.push_back(VE.getValueID(I.getOperand(0)));
713 break;
714
715 case Instruction::Alloca:
716 Code = bitc::FUNC_CODE_INST_ALLOCA;
717 Vals.push_back(VE.getTypeID(I.getType()));
718 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
719 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
720 break;
721
722 case Instruction::Load:
723 Code = bitc::FUNC_CODE_INST_LOAD;
724 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
725 Vals.push_back(VE.getValueID(I.getOperand(0))); // ptr.
726 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
727 Vals.push_back(cast<LoadInst>(I).isVolatile());
Chris Lattner440168b2007-05-05 07:44:49 +0000728 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
Chris Lattnerd309c752007-05-01 02:13:26 +0000729 break;
730 case Instruction::Store:
731 Code = bitc::FUNC_CODE_INST_STORE;
732 Vals.push_back(VE.getTypeID(I.getOperand(1)->getType())); // Pointer
733 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
734 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
735 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
736 Vals.push_back(cast<StoreInst>(I).isVolatile());
737 break;
738 case Instruction::Call: {
739 Code = bitc::FUNC_CODE_INST_CALL;
Chris Lattner76520192007-05-03 22:34:03 +0000740 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
741 cast<CallInst>(I).isTailCall());
Chris Lattnerd309c752007-05-01 02:13:26 +0000742 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
743 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
744
745 // Emit value #'s for the fixed parameters.
746 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
747 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
748 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
749 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
750
Chris Lattner60ce9b52007-05-01 07:03:37 +0000751 // Emit type/value pairs for varargs params.
752 if (FTy->isVarArg()) {
753 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
Chris Lattner60ce9b52007-05-01 07:03:37 +0000754 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
755 i != e; ++i) {
756 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
757 Vals.push_back(VE.getValueID(I.getOperand(i)));
Chris Lattnerd309c752007-05-01 02:13:26 +0000758 }
759 }
760 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000761 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000762 case Instruction::VAArg:
763 Code = bitc::FUNC_CODE_INST_VAARG;
764 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
765 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
766 Vals.push_back(VE.getTypeID(I.getType())); // restype.
767 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000768 }
769
770 Stream.EmitRecord(Code, Vals, AbbrevToUse);
771 Vals.clear();
772}
773
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000774// Emit names for globals/functions etc.
775static void WriteValueSymbolTable(const ValueSymbolTable &VST,
776 const ValueEnumerator &VE,
777 BitstreamWriter &Stream) {
778 if (VST.empty()) return;
Chris Lattnerff294a42007-05-05 01:26:50 +0000779 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2e7899d2007-05-04 20:34:50 +0000780
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000781 // FIXME: Set up the abbrev, we know how many values there are!
782 // FIXME: We know if the type names can use 7-bit ascii.
783 SmallVector<unsigned, 64> NameVals;
784
785 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
786 SI != SE; ++SI) {
Chris Lattner59698302007-05-04 20:52:02 +0000787
788 const ValueName &Name = *SI;
789
790 // Figure out the encoding to use for the name.
791 bool is7Bit = true;
Chris Lattnerff294a42007-05-05 01:26:50 +0000792 bool isChar6 = true;
793 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
794 C != E; ++C) {
795 if (isChar6)
796 isChar6 = BitCodeAbbrevOp::isChar6(*C);
797 if ((unsigned char)*C & 128) {
Chris Lattner59698302007-05-04 20:52:02 +0000798 is7Bit = false;
Chris Lattnerff294a42007-05-05 01:26:50 +0000799 break; // don't bother scanning the rest.
Chris Lattner59698302007-05-04 20:52:02 +0000800 }
Chris Lattnerff294a42007-05-05 01:26:50 +0000801 }
Chris Lattner59698302007-05-04 20:52:02 +0000802
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000803 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000804
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000805 // VST_ENTRY: [valueid, namechar x N]
806 // VST_BBENTRY: [bbid, namechar x N]
Chris Lattnere825ed52007-05-03 22:18:21 +0000807 unsigned Code;
808 if (isa<BasicBlock>(SI->getValue())) {
809 Code = bitc::VST_CODE_BBENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000810 if (isChar6)
811 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000812 } else {
813 Code = bitc::VST_CODE_ENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000814 if (isChar6)
815 AbbrevToUse = VST_ENTRY_6_ABBREV;
816 else if (is7Bit)
817 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000818 }
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000819
Chris Lattnere825ed52007-05-03 22:18:21 +0000820 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +0000821 for (const char *P = Name.getKeyData(),
822 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000823 NameVals.push_back((unsigned char)*P);
824
825 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +0000826 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000827 NameVals.clear();
828 }
829 Stream.ExitBlock();
830}
831
Chris Lattner8d35c792007-04-26 03:50:57 +0000832/// WriteFunction - Emit a function body to the module stream.
Chris Lattner198f34a2007-04-26 03:27:58 +0000833static void WriteFunction(const Function &F, ValueEnumerator &VE,
834 BitstreamWriter &Stream) {
Chris Lattner01b27452007-04-29 05:49:09 +0000835 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3);
Chris Lattner8d35c792007-04-26 03:50:57 +0000836 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000837
838 SmallVector<unsigned, 64> Vals;
839
840 // Emit the number of basic blocks, so the reader can create them ahead of
841 // time.
842 Vals.push_back(VE.getBasicBlocks().size());
843 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
844 Vals.clear();
845
846 // FIXME: Function attributes?
847
848 // If there are function-local constants, emit them now.
849 unsigned CstStart, CstEnd;
850 VE.getFunctionConstantRange(CstStart, CstEnd);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000851 WriteConstants(CstStart, CstEnd, VE, Stream, false);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000852
853 // Finally, emit all the instructions, in order.
854 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
855 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
856 WriteInstruction(*I, VE, Stream, Vals);
Chris Lattner198f34a2007-04-26 03:27:58 +0000857
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000858 // Emit names for all the instructions etc.
859 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
860
Chris Lattner8d35c792007-04-26 03:50:57 +0000861 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000862 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +0000863}
864
865/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
866static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
867 const ValueEnumerator &VE,
868 BitstreamWriter &Stream) {
869 if (TST.empty()) return;
870
871 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
872
Chris Lattner7a263ea2007-05-05 00:47:19 +0000873 // 7-bit fixed width VST_CODE_ENTRY strings.
874 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
875 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
876 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
877 Log2_32_Ceil(VE.getTypes().size()+1)));
878 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
879 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
880 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner198f34a2007-04-26 03:27:58 +0000881
882 SmallVector<unsigned, 64> NameVals;
883
884 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
885 TI != TE; ++TI) {
Chris Lattner7a263ea2007-05-05 00:47:19 +0000886 // TST_ENTRY: [typeid, namechar x N]
Chris Lattner198f34a2007-04-26 03:27:58 +0000887 NameVals.push_back(VE.getTypeID(TI->second));
888
889 const std::string &Str = TI->first;
Chris Lattner7a263ea2007-05-05 00:47:19 +0000890 bool is7Bit = true;
891 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
892 NameVals.push_back((unsigned char)Str[i]);
893 if (Str[i] & 128)
894 is7Bit = false;
895 }
Chris Lattner198f34a2007-04-26 03:27:58 +0000896
897 // Emit the finished record.
Chris Lattner7a263ea2007-05-05 00:47:19 +0000898 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
Chris Lattner198f34a2007-04-26 03:27:58 +0000899 NameVals.clear();
900 }
901
902 Stream.ExitBlock();
903}
904
Chris Lattner07faafc2007-05-04 18:26:27 +0000905// Emit blockinfo, which defines the standard abbreviations etc.
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000906static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
Chris Lattner07faafc2007-05-04 18:26:27 +0000907 // We only want to emit block info records for blocks that have multiple
908 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
909 // blocks can defined their abbrevs inline.
Chris Lattnere17b6582007-05-05 00:17:00 +0000910 Stream.EnterBlockInfoBlock(2);
Chris Lattner07faafc2007-05-04 18:26:27 +0000911
Chris Lattnere17b6582007-05-05 00:17:00 +0000912 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
913 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
914 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
915 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
916 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
917 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
918 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
919 Abbv) != VST_ENTRY_8_ABBREV)
920 assert(0 && "Unexpected abbrev ordering!");
921 }
922
923 { // 7-bit fixed width VST_ENTRY strings.
924 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
925 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
927 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
928 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
929 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
930 Abbv) != VST_ENTRY_7_ABBREV)
931 assert(0 && "Unexpected abbrev ordering!");
932 }
Chris Lattnerff294a42007-05-05 01:26:50 +0000933 { // 6-bit char6 VST_ENTRY strings.
934 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
935 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
936 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
937 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
938 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
939 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
940 Abbv) != VST_ENTRY_6_ABBREV)
941 assert(0 && "Unexpected abbrev ordering!");
942 }
943 { // 6-bit char6 VST_BBENTRY strings.
Chris Lattnere17b6582007-05-05 00:17:00 +0000944 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
945 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
946 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
947 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnerff294a42007-05-05 01:26:50 +0000948 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Chris Lattnere17b6582007-05-05 00:17:00 +0000949 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnerff294a42007-05-05 01:26:50 +0000950 Abbv) != VST_BBENTRY_6_ABBREV)
Chris Lattnere17b6582007-05-05 00:17:00 +0000951 assert(0 && "Unexpected abbrev ordering!");
952 }
953
Chris Lattner440168b2007-05-05 07:44:49 +0000954
955
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000956 { // SETTYPE abbrev for CONSTANTS_BLOCK.
957 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
958 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
960 Log2_32_Ceil(VE.getTypes().size()+1)));
961 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
962 Abbv) != CONSTANTS_SETTYPE_ABBREV)
963 assert(0 && "Unexpected abbrev ordering!");
964 }
965
966 { // INTEGER abbrev for CONSTANTS_BLOCK.
967 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
968 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
969 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
970 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
971 Abbv) != CONSTANTS_INTEGER_ABBREV)
972 assert(0 && "Unexpected abbrev ordering!");
973 }
974
975 { // CE_CAST abbrev for CONSTANTS_BLOCK.
976 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
977 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
979 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
980 Log2_32_Ceil(VE.getTypes().size()+1)));
981 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
982
983 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
984 Abbv) != CONSTANTS_CE_CAST_Abbrev)
985 assert(0 && "Unexpected abbrev ordering!");
986 }
987 { // NULL abbrev for CONSTANTS_BLOCK.
988 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
989 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
990 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
991 Abbv) != CONSTANTS_NULL_Abbrev)
992 assert(0 && "Unexpected abbrev ordering!");
993 }
994
Chris Lattner440168b2007-05-05 07:44:49 +0000995 // FIXME: This should only use space for first class types!
996
997 { // INST_LOAD abbrev for FUNCTION_BLOCK.
998 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
999 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1000 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1001 Log2_32_Ceil(VE.getTypes().size()+1)));
1002 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1003 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1004 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1005 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1006 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1007 assert(0 && "Unexpected abbrev ordering!");
1008 }
1009
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001010 Stream.ExitBlock();
1011}
1012
1013
1014/// WriteModule - Emit the specified module to the bitstream.
1015static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1016 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1017
1018 // Emit the version number if it is non-zero.
1019 if (CurVersion) {
1020 SmallVector<unsigned, 1> Vals;
1021 Vals.push_back(CurVersion);
1022 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1023 }
1024
1025 // Analyze the module, enumerating globals, functions, etc.
1026 ValueEnumerator VE(M);
1027
1028 // Emit blockinfo, which defines the standard abbreviations etc.
1029 WriteBlockInfo(VE, Stream);
1030
1031 // Emit information about parameter attributes.
1032 WriteParamAttrTable(VE, Stream);
1033
1034 // Emit information describing all of the types in the module.
1035 WriteTypeTable(VE, Stream);
1036
1037 // Emit top-level description of module, including target triple, inline asm,
1038 // descriptors for global variables, and function prototype info.
1039 WriteModuleInfo(M, VE, Stream);
1040
1041 // Emit constants.
1042 WriteModuleConstants(VE, Stream);
1043
1044 // If we have any aggregate values in the value table, purge them - these can
1045 // only be used to initialize global variables. Doing so makes the value
1046 // namespace smaller for code in functions.
1047 int NumNonAggregates = VE.PurgeAggregateValues();
1048 if (NumNonAggregates != -1) {
1049 SmallVector<unsigned, 1> Vals;
1050 Vals.push_back(NumNonAggregates);
1051 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1052 }
1053
1054 // Emit function bodies.
1055 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1056 if (!I->isDeclaration())
1057 WriteFunction(*I, VE, Stream);
1058
1059 // Emit the type symbol table information.
1060 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1061
1062 // Emit names for globals/functions etc.
1063 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1064
Chris Lattner07faafc2007-05-04 18:26:27 +00001065 Stream.ExitBlock();
1066}
1067
1068
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001069/// WriteBitcodeToFile - Write the specified module to the specified output
1070/// stream.
1071void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1072 std::vector<unsigned char> Buffer;
1073 BitstreamWriter Stream(Buffer);
1074
1075 Buffer.reserve(256*1024);
1076
1077 // Emit the file header.
1078 Stream.Emit((unsigned)'B', 8);
1079 Stream.Emit((unsigned)'C', 8);
1080 Stream.Emit(0x0, 4);
1081 Stream.Emit(0xC, 4);
1082 Stream.Emit(0xE, 4);
1083 Stream.Emit(0xD, 4);
1084
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001085 // Emit the module.
1086 WriteModule(M, Stream);
1087
1088 // Write the generated bitstream to "Out".
1089 Out.write((char*)&Buffer.front(), Buffer.size());
1090}