blob: c5be80fc508eb58d65153456c4f344a8e490fc52 [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 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"
Chris Lattnerf0a65312007-05-04 02:59:04 +000023#include "llvm/ParameterAttributes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000024#include "llvm/TypeSymbolTable.h"
Chris Lattnerb992be12007-04-23 20:35:01 +000025#include "llvm/ValueSymbolTable.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000026#include "llvm/Support/MathExtras.h"
27using namespace llvm;
28
Chris Lattner59698302007-05-04 20:52:02 +000029/// These are manifest constants used by the bitcode writer. They do not need to
30/// be kept in sync with the reader, but need to be consistent within this file.
31enum {
32 CurVersion = 0,
33
34 // VALUE_SYMTAB_BLOCK abbrev id's.
35 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattner2453f712007-05-04 20:58:35 +000036 VST_ENTRY_7_ABBREV,
Chris Lattnerff294a42007-05-05 01:26:50 +000037 VST_ENTRY_6_ABBREV,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000038 VST_BBENTRY_6_ABBREV,
39
40 // CONSTANTS_BLOCK abbrev id's.
41 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
42 CONSTANTS_INTEGER_ABBREV,
43 CONSTANTS_CE_CAST_Abbrev,
Chris Lattner440168b2007-05-05 07:44:49 +000044 CONSTANTS_NULL_Abbrev,
45
46 // FUNCTION_BLOCK abbrev id's.
Chris Lattner94687ac2007-05-06 01:28:01 +000047 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000048 FUNCTION_INST_BINOP_ABBREV,
49 FUNCTION_INST_CAST_ABBREV,
Chris Lattner94687ac2007-05-06 01:28:01 +000050 FUNCTION_INST_RET_VOID_ABBREV,
51 FUNCTION_INST_RET_VAL_ABBREV,
52 FUNCTION_INST_UNREACHABLE_ABBREV
Chris Lattner59698302007-05-04 20:52:02 +000053};
54
Chris Lattnerfd57cec2007-04-22 06:24:45 +000055
Chris Lattnerf581c3b2007-04-24 07:07:11 +000056static unsigned GetEncodedCastOpcode(unsigned Opcode) {
57 switch (Opcode) {
58 default: assert(0 && "Unknown cast instruction!");
59 case Instruction::Trunc : return bitc::CAST_TRUNC;
60 case Instruction::ZExt : return bitc::CAST_ZEXT;
61 case Instruction::SExt : return bitc::CAST_SEXT;
62 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
63 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
64 case Instruction::UIToFP : return bitc::CAST_UITOFP;
65 case Instruction::SIToFP : return bitc::CAST_SITOFP;
66 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
67 case Instruction::FPExt : return bitc::CAST_FPEXT;
68 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
69 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
70 case Instruction::BitCast : return bitc::CAST_BITCAST;
71 }
72}
73
74static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
75 switch (Opcode) {
76 default: assert(0 && "Unknown binary instruction!");
77 case Instruction::Add: return bitc::BINOP_ADD;
78 case Instruction::Sub: return bitc::BINOP_SUB;
79 case Instruction::Mul: return bitc::BINOP_MUL;
80 case Instruction::UDiv: return bitc::BINOP_UDIV;
81 case Instruction::FDiv:
82 case Instruction::SDiv: return bitc::BINOP_SDIV;
83 case Instruction::URem: return bitc::BINOP_UREM;
84 case Instruction::FRem:
85 case Instruction::SRem: return bitc::BINOP_SREM;
86 case Instruction::Shl: return bitc::BINOP_SHL;
87 case Instruction::LShr: return bitc::BINOP_LSHR;
88 case Instruction::AShr: return bitc::BINOP_ASHR;
89 case Instruction::And: return bitc::BINOP_AND;
90 case Instruction::Or: return bitc::BINOP_OR;
91 case Instruction::Xor: return bitc::BINOP_XOR;
92 }
93}
94
95
96
Chris Lattnerfd57cec2007-04-22 06:24:45 +000097static void WriteStringRecord(unsigned Code, const std::string &Str,
98 unsigned AbbrevToUse, BitstreamWriter &Stream) {
99 SmallVector<unsigned, 64> Vals;
100
Chris Lattner15e6d172007-05-04 19:11:41 +0000101 // Code: [strchar x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000102 for (unsigned i = 0, e = Str.size(); i != e; ++i)
103 Vals.push_back(Str[i]);
104
105 // Emit the finished record.
106 Stream.EmitRecord(Code, Vals, AbbrevToUse);
107}
108
Chris Lattner62bbeea2007-05-04 00:44:52 +0000109// Emit information about parameter attributes.
110static void WriteParamAttrTable(const ValueEnumerator &VE,
111 BitstreamWriter &Stream) {
112 const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
113 if (Attrs.empty()) return;
114
Chris Lattnerf0a65312007-05-04 02:59:04 +0000115 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
116
117 SmallVector<uint64_t, 64> Record;
118 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
119 const ParamAttrsList *A = Attrs[i];
120 for (unsigned op = 0, e = A->size(); op != e; ++op) {
121 Record.push_back(A->getParamIndex(op));
Chris Lattnerf6f9cd12007-05-04 03:14:09 +0000122 Record.push_back(A->getParamAttrsAtIndex(op));
Chris Lattnerf0a65312007-05-04 02:59:04 +0000123 }
124
125 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
126 Record.clear();
127 }
Chris Lattner62bbeea2007-05-04 00:44:52 +0000128
Chris Lattnerf0a65312007-05-04 02:59:04 +0000129 Stream.ExitBlock();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000130}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000131
132/// WriteTypeTable - Write out the type table for a module.
133static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
134 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
135
136 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
137 SmallVector<uint64_t, 64> TypeVals;
138
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000139 // Abbrev for TYPE_CODE_POINTER.
140 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
141 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
142 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
143 Log2_32_Ceil(VE.getTypes().size()+1)));
144 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
145
146 // Abbrev for TYPE_CODE_FUNCTION.
147 Abbv = new BitCodeAbbrev();
148 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
149 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
150 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
151 Log2_32_Ceil(VE.getParamAttrs().size()+1)));
152 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
153 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
154 Log2_32_Ceil(VE.getTypes().size()+1)));
155 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
156
157 // Abbrev for TYPE_CODE_STRUCT.
158 Abbv = new BitCodeAbbrev();
159 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
161 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
162 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
163 Log2_32_Ceil(VE.getTypes().size()+1)));
164 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
165
166 // Abbrev for TYPE_CODE_ARRAY.
167 Abbv = new BitCodeAbbrev();
168 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
169 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
170 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
171 Log2_32_Ceil(VE.getTypes().size()+1)));
172 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000173
174 // Emit an entry count so the reader can reserve space.
175 TypeVals.push_back(TypeList.size());
176 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
177 TypeVals.clear();
178
179 // Loop over all of the types, emitting each in turn.
180 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
181 const Type *T = TypeList[i].first;
182 int AbbrevToUse = 0;
183 unsigned Code = 0;
184
185 switch (T->getTypeID()) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000186 default: assert(0 && "Unknown type!");
187 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
188 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
189 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000190 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
191 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
192 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000193 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
194 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
195 case Type::IntegerTyID:
196 // INTEGER: [width]
197 Code = bitc::TYPE_CODE_INTEGER;
198 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
199 break;
200 case Type::PointerTyID:
201 // POINTER: [pointee type]
202 Code = bitc::TYPE_CODE_POINTER;
203 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000204 AbbrevToUse = PtrAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000205 break;
206
207 case Type::FunctionTyID: {
208 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000209 // FUNCTION: [isvararg, attrid, retty, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000210 Code = bitc::TYPE_CODE_FUNCTION;
211 TypeVals.push_back(FT->isVarArg());
Chris Lattner9113e732007-05-04 03:41:34 +0000212 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000213 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000214 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
215 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000216 AbbrevToUse = FunctionAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000217 break;
218 }
219 case Type::StructTyID: {
220 const StructType *ST = cast<StructType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000221 // STRUCT: [ispacked, eltty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000222 Code = bitc::TYPE_CODE_STRUCT;
223 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000224 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000225 for (StructType::element_iterator I = ST->element_begin(),
226 E = ST->element_end(); I != E; ++I)
227 TypeVals.push_back(VE.getTypeID(*I));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000228 AbbrevToUse = StructAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000229 break;
230 }
231 case Type::ArrayTyID: {
232 const ArrayType *AT = cast<ArrayType>(T);
233 // ARRAY: [numelts, eltty]
234 Code = bitc::TYPE_CODE_ARRAY;
235 TypeVals.push_back(AT->getNumElements());
236 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000237 AbbrevToUse = ArrayAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000238 break;
239 }
240 case Type::VectorTyID: {
241 const VectorType *VT = cast<VectorType>(T);
242 // VECTOR [numelts, eltty]
243 Code = bitc::TYPE_CODE_VECTOR;
244 TypeVals.push_back(VT->getNumElements());
245 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
246 break;
247 }
248 }
249
250 // Emit the finished record.
251 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
252 TypeVals.clear();
253 }
254
255 Stream.ExitBlock();
256}
257
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000258static unsigned getEncodedLinkage(const GlobalValue *GV) {
259 switch (GV->getLinkage()) {
260 default: assert(0 && "Invalid linkage!");
Chris Lattner384003d2007-05-11 23:51:59 +0000261 case GlobalValue::GhostLinkage: // Map ghost linkage onto external.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000262 case GlobalValue::ExternalLinkage: return 0;
263 case GlobalValue::WeakLinkage: return 1;
264 case GlobalValue::AppendingLinkage: return 2;
265 case GlobalValue::InternalLinkage: return 3;
266 case GlobalValue::LinkOnceLinkage: return 4;
267 case GlobalValue::DLLImportLinkage: return 5;
268 case GlobalValue::DLLExportLinkage: return 6;
269 case GlobalValue::ExternalWeakLinkage: return 7;
270 }
271}
272
273static unsigned getEncodedVisibility(const GlobalValue *GV) {
274 switch (GV->getVisibility()) {
275 default: assert(0 && "Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000276 case GlobalValue::DefaultVisibility: return 0;
277 case GlobalValue::HiddenVisibility: return 1;
278 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000279 }
280}
281
282// Emit top-level description of module, including target triple, inline asm,
283// descriptors for global variables, and function prototype info.
284static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
285 BitstreamWriter &Stream) {
286 // Emit the list of dependent libraries for the Module.
287 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
288 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
289
290 // Emit various pieces of data attached to a module.
291 if (!M->getTargetTriple().empty())
292 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
293 0/*TODO*/, Stream);
294 if (!M->getDataLayout().empty())
295 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
296 0/*TODO*/, Stream);
297 if (!M->getModuleInlineAsm().empty())
298 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
299 0/*TODO*/, Stream);
300
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000301 // Emit information about sections, computing how many there are. Also
302 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000303 std::map<std::string, unsigned> SectionMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000304 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000305 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000306 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
307 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000308 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000309 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000310
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000311 if (!GV->hasSection()) continue;
312 // Give section names unique ID's.
313 unsigned &Entry = SectionMap[GV->getSection()];
314 if (Entry != 0) continue;
315 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
316 0/*TODO*/, Stream);
317 Entry = SectionMap.size();
318 }
319 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000320 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000321 if (!F->hasSection()) continue;
322 // Give section names unique ID's.
323 unsigned &Entry = SectionMap[F->getSection()];
324 if (Entry != 0) continue;
325 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
326 0/*TODO*/, Stream);
327 Entry = SectionMap.size();
328 }
329
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000330 // Emit abbrev for globals, now that we know # sections and max alignment.
331 unsigned SimpleGVarAbbrev = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000332 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000333 // Add an abbrev for common globals with no visibility or thread localness.
334 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
335 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000337 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000340 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
341 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000342 Abbv->Add(BitCodeAbbrevOp(0));
343 else {
344 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000345 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000346 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000347 }
348 if (SectionMap.empty()) // Section.
349 Abbv->Add(BitCodeAbbrevOp(0));
350 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000352 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000353 // Don't bother emitting vis + thread local.
354 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
355 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000356
357 // Emit the global variable information.
358 SmallVector<unsigned, 64> Vals;
359 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
360 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000361 unsigned AbbrevToUse = 0;
362
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000363 // GLOBALVAR: [type, isconst, initid,
364 // linkage, alignment, section, visibility, threadlocal]
365 Vals.push_back(VE.getTypeID(GV->getType()));
366 Vals.push_back(GV->isConstant());
367 Vals.push_back(GV->isDeclaration() ? 0 :
368 (VE.getValueID(GV->getInitializer()) + 1));
369 Vals.push_back(getEncodedLinkage(GV));
370 Vals.push_back(Log2_32(GV->getAlignment())+1);
371 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000372 if (GV->isThreadLocal() ||
373 GV->getVisibility() != GlobalValue::DefaultVisibility) {
374 Vals.push_back(getEncodedVisibility(GV));
375 Vals.push_back(GV->isThreadLocal());
376 } else {
377 AbbrevToUse = SimpleGVarAbbrev;
378 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000379
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000380 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
381 Vals.clear();
382 }
383
384 // Emit the function proto information.
385 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
386 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
387 // visibility]
388 Vals.push_back(VE.getTypeID(F->getType()));
389 Vals.push_back(F->getCallingConv());
390 Vals.push_back(F->isDeclaration());
391 Vals.push_back(getEncodedLinkage(F));
Chris Lattnera9bb7132007-05-08 05:38:01 +0000392
393 // Note: we emit the param attr ID number for the function type of this
394 // function. In the future, we intend for attrs to be properties of
395 // functions, instead of on the type. This is to support this future work.
396 Vals.push_back(VE.getParamAttrID(F->getFunctionType()->getParamAttrs()));
397
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000398 Vals.push_back(Log2_32(F->getAlignment())+1);
399 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
400 Vals.push_back(getEncodedVisibility(F));
401
402 unsigned AbbrevToUse = 0;
403 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
404 Vals.clear();
405 }
Chris Lattner07d98b42007-04-26 02:46:40 +0000406
407
408 // Emit the alias information.
409 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
410 AI != E; ++AI) {
411 Vals.push_back(VE.getTypeID(AI->getType()));
412 Vals.push_back(VE.getValueID(AI->getAliasee()));
413 Vals.push_back(getEncodedLinkage(AI));
414 unsigned AbbrevToUse = 0;
415 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
416 Vals.clear();
417 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000418}
419
420
Chris Lattner2edd22b2007-04-24 00:16:04 +0000421static void WriteConstants(unsigned FirstVal, unsigned LastVal,
422 const ValueEnumerator &VE,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000423 BitstreamWriter &Stream, bool isGlobal) {
Chris Lattner2edd22b2007-04-24 00:16:04 +0000424 if (FirstVal == LastVal) return;
425
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000426 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
Chris Lattnerb992be12007-04-23 20:35:01 +0000427
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000428 unsigned AggregateAbbrev = 0;
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000429 unsigned String8Abbrev = 0;
430 unsigned CString7Abbrev = 0;
431 unsigned CString6Abbrev = 0;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000432 // If this is a constant pool for the module, emit module-specific abbrevs.
433 if (isGlobal) {
434 // Abbrev for CST_CODE_AGGREGATE.
435 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
436 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
437 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
438 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
439 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner817f08a2007-05-06 00:42:18 +0000440
441 // Abbrev for CST_CODE_STRING.
442 Abbv = new BitCodeAbbrev();
443 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000445 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
446 String8Abbrev = Stream.EmitAbbrev(Abbv);
447 // Abbrev for CST_CODE_CSTRING.
448 Abbv = new BitCodeAbbrev();
449 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
450 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattner817f08a2007-05-06 00:42:18 +0000451 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000452 CString7Abbrev = Stream.EmitAbbrev(Abbv);
453 // Abbrev for CST_CODE_CSTRING.
454 Abbv = new BitCodeAbbrev();
455 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
456 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
457 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
458 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000459 }
460
Chris Lattner2edd22b2007-04-24 00:16:04 +0000461 SmallVector<uint64_t, 64> Record;
462
463 const ValueEnumerator::ValueList &Vals = VE.getValues();
464 const Type *LastTy = 0;
465 for (unsigned i = FirstVal; i != LastVal; ++i) {
466 const Value *V = Vals[i].first;
467 // If we need to switch types, do so now.
468 if (V->getType() != LastTy) {
469 LastTy = V->getType();
470 Record.push_back(VE.getTypeID(LastTy));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000471 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
472 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000473 Record.clear();
474 }
475
476 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner2bce93a2007-05-06 01:58:20 +0000477 Record.push_back(unsigned(IA->hasSideEffects()));
478
479 // Add the asm string.
480 const std::string &AsmStr = IA->getAsmString();
481 Record.push_back(AsmStr.size());
482 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
483 Record.push_back(AsmStr[i]);
484
485 // Add the constraint string.
486 const std::string &ConstraintStr = IA->getConstraintString();
487 Record.push_back(ConstraintStr.size());
488 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
489 Record.push_back(ConstraintStr[i]);
490 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
491 Record.clear();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000492 continue;
493 }
494 const Constant *C = cast<Constant>(V);
495 unsigned Code = -1U;
496 unsigned AbbrevToUse = 0;
497 if (C->isNullValue()) {
498 Code = bitc::CST_CODE_NULL;
499 } else if (isa<UndefValue>(C)) {
500 Code = bitc::CST_CODE_UNDEF;
501 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
502 if (IV->getBitWidth() <= 64) {
503 int64_t V = IV->getSExtValue();
504 if (V >= 0)
505 Record.push_back(V << 1);
506 else
507 Record.push_back((-V << 1) | 1);
508 Code = bitc::CST_CODE_INTEGER;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000509 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000510 } else { // Wide integers, > 64 bits in size.
511 // We have an arbitrary precision integer value to write whose
512 // bit width is > 64. However, in canonical unsigned integer
513 // format it is likely that the high bits are going to be zero.
514 // So, we only write the number of active words.
515 unsigned NWords = IV->getValue().getActiveWords();
516 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000517 for (unsigned i = 0; i != NWords; ++i) {
518 int64_t V = RawWords[i];
519 if (V >= 0)
520 Record.push_back(V << 1);
521 else
522 Record.push_back((-V << 1) | 1);
523 }
524 Code = bitc::CST_CODE_WIDE_INTEGER;
525 }
526 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
527 Code = bitc::CST_CODE_FLOAT;
528 if (CFP->getType() == Type::FloatTy) {
529 Record.push_back(FloatToBits((float)CFP->getValue()));
530 } else {
531 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
532 Record.push_back(DoubleToBits((double)CFP->getValue()));
533 }
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000534 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
535 // Emit constant strings specially.
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000536 unsigned NumOps = C->getNumOperands();
537 // If this is a null-terminated string, use the denser CSTRING encoding.
538 if (C->getOperand(NumOps-1)->isNullValue()) {
539 Code = bitc::CST_CODE_CSTRING;
540 --NumOps; // Don't encode the null, which isn't allowed by char6.
541 } else {
542 Code = bitc::CST_CODE_STRING;
543 AbbrevToUse = String8Abbrev;
544 }
545 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
546 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
547 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner817f08a2007-05-06 00:42:18 +0000548 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
549 Record.push_back(V);
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000550 isCStr7 &= (V & 128) == 0;
551 if (isCStrChar6)
552 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
Chris Lattner817f08a2007-05-06 00:42:18 +0000553 }
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000554
555 if (isCStrChar6)
556 AbbrevToUse = CString6Abbrev;
557 else if (isCStr7)
558 AbbrevToUse = CString7Abbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000559 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
560 isa<ConstantVector>(V)) {
561 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000562 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
563 Record.push_back(VE.getValueID(C->getOperand(i)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000564 AbbrevToUse = AggregateAbbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000565 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000566 switch (CE->getOpcode()) {
567 default:
568 if (Instruction::isCast(CE->getOpcode())) {
569 Code = bitc::CST_CODE_CE_CAST;
570 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
571 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
572 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000573 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000574 } else {
575 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
576 Code = bitc::CST_CODE_CE_BINOP;
577 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
578 Record.push_back(VE.getValueID(C->getOperand(0)));
579 Record.push_back(VE.getValueID(C->getOperand(1)));
580 }
581 break;
582 case Instruction::GetElementPtr:
583 Code = bitc::CST_CODE_CE_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000584 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
585 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
586 Record.push_back(VE.getValueID(C->getOperand(i)));
587 }
588 break;
589 case Instruction::Select:
590 Code = bitc::CST_CODE_CE_SELECT;
591 Record.push_back(VE.getValueID(C->getOperand(0)));
592 Record.push_back(VE.getValueID(C->getOperand(1)));
593 Record.push_back(VE.getValueID(C->getOperand(2)));
594 break;
595 case Instruction::ExtractElement:
596 Code = bitc::CST_CODE_CE_EXTRACTELT;
597 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
598 Record.push_back(VE.getValueID(C->getOperand(0)));
599 Record.push_back(VE.getValueID(C->getOperand(1)));
600 break;
601 case Instruction::InsertElement:
602 Code = bitc::CST_CODE_CE_INSERTELT;
603 Record.push_back(VE.getValueID(C->getOperand(0)));
604 Record.push_back(VE.getValueID(C->getOperand(1)));
605 Record.push_back(VE.getValueID(C->getOperand(2)));
606 break;
607 case Instruction::ShuffleVector:
608 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
609 Record.push_back(VE.getValueID(C->getOperand(0)));
610 Record.push_back(VE.getValueID(C->getOperand(1)));
611 Record.push_back(VE.getValueID(C->getOperand(2)));
612 break;
613 case Instruction::ICmp:
614 case Instruction::FCmp:
615 Code = bitc::CST_CODE_CE_CMP;
616 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
617 Record.push_back(VE.getValueID(C->getOperand(0)));
618 Record.push_back(VE.getValueID(C->getOperand(1)));
619 Record.push_back(CE->getPredicate());
620 break;
621 }
Chris Lattner2edd22b2007-04-24 00:16:04 +0000622 } else {
623 assert(0 && "Unknown constant!");
624 }
625 Stream.EmitRecord(Code, Record, AbbrevToUse);
626 Record.clear();
627 }
628
629 Stream.ExitBlock();
630}
631
632static void WriteModuleConstants(const ValueEnumerator &VE,
633 BitstreamWriter &Stream) {
634 const ValueEnumerator::ValueList &Vals = VE.getValues();
635
636 // Find the first constant to emit, which is the first non-globalvalue value.
637 // We know globalvalues have been emitted by WriteModuleInfo.
638 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
639 if (!isa<GlobalValue>(Vals[i].first)) {
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000640 WriteConstants(i, Vals.size(), VE, Stream, true);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000641 return;
642 }
643 }
644}
Chris Lattnerb992be12007-04-23 20:35:01 +0000645
Chris Lattner7337ab92007-05-06 00:00:00 +0000646/// PushValueAndType - The file has to encode both the value and type id for
647/// many values, because we need to know what type to create for forward
648/// references. However, most operands are not forward references, so this type
649/// field is not needed.
650///
651/// This function adds V's value ID to Vals. If the value ID is higher than the
652/// instruction ID, then it is a forward reference, and it also includes the
653/// type ID.
654static bool PushValueAndType(Value *V, unsigned InstID,
655 SmallVector<unsigned, 64> &Vals,
656 ValueEnumerator &VE) {
657 unsigned ValID = VE.getValueID(V);
658 Vals.push_back(ValID);
659 if (ValID >= InstID) {
660 Vals.push_back(VE.getTypeID(V->getType()));
661 return true;
662 }
663 return false;
664}
665
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000666/// WriteInstruction - Emit an instruction to the specified stream.
Chris Lattner7337ab92007-05-06 00:00:00 +0000667static void WriteInstruction(const Instruction &I, unsigned InstID,
668 ValueEnumerator &VE, BitstreamWriter &Stream,
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000669 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000670 unsigned Code = 0;
671 unsigned AbbrevToUse = 0;
672 switch (I.getOpcode()) {
673 default:
674 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +0000675 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000676 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
677 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000678 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000679 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000680 } else {
681 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +0000682 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000683 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
684 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000685 Vals.push_back(VE.getValueID(I.getOperand(1)));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000686 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000687 }
688 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000689
690 case Instruction::GetElementPtr:
691 Code = bitc::FUNC_CODE_INST_GEP;
Chris Lattner7337ab92007-05-06 00:00:00 +0000692 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
693 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000694 break;
695 case Instruction::Select:
696 Code = bitc::FUNC_CODE_INST_SELECT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000697 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000698 Vals.push_back(VE.getValueID(I.getOperand(2)));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000699 Vals.push_back(VE.getValueID(I.getOperand(0)));
Chris Lattnerd309c752007-05-01 02:13:26 +0000700 break;
701 case Instruction::ExtractElement:
702 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000703 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000704 Vals.push_back(VE.getValueID(I.getOperand(1)));
705 break;
706 case Instruction::InsertElement:
707 Code = bitc::FUNC_CODE_INST_INSERTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000708 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000709 Vals.push_back(VE.getValueID(I.getOperand(1)));
710 Vals.push_back(VE.getValueID(I.getOperand(2)));
711 break;
712 case Instruction::ShuffleVector:
713 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000714 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000715 Vals.push_back(VE.getValueID(I.getOperand(1)));
716 Vals.push_back(VE.getValueID(I.getOperand(2)));
717 break;
718 case Instruction::ICmp:
719 case Instruction::FCmp:
720 Code = bitc::FUNC_CODE_INST_CMP;
Chris Lattner7337ab92007-05-06 00:00:00 +0000721 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000722 Vals.push_back(VE.getValueID(I.getOperand(1)));
723 Vals.push_back(cast<CmpInst>(I).getPredicate());
724 break;
725
726 case Instruction::Ret:
727 Code = bitc::FUNC_CODE_INST_RET;
Chris Lattner94687ac2007-05-06 01:28:01 +0000728 if (!I.getNumOperands())
729 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
730 else if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
731 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
Chris Lattnerd309c752007-05-01 02:13:26 +0000732 break;
733 case Instruction::Br:
734 Code = bitc::FUNC_CODE_INST_BR;
735 Vals.push_back(VE.getValueID(I.getOperand(0)));
736 if (cast<BranchInst>(I).isConditional()) {
737 Vals.push_back(VE.getValueID(I.getOperand(1)));
738 Vals.push_back(VE.getValueID(I.getOperand(2)));
739 }
740 break;
741 case Instruction::Switch:
742 Code = bitc::FUNC_CODE_INST_SWITCH;
743 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000744 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
745 Vals.push_back(VE.getValueID(I.getOperand(i)));
746 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000747 case Instruction::Invoke: {
Chris Lattnera9bb7132007-05-08 05:38:01 +0000748 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
749 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattnerd309c752007-05-01 02:13:26 +0000750 Code = bitc::FUNC_CODE_INST_INVOKE;
Chris Lattnera9bb7132007-05-08 05:38:01 +0000751
752 // Note: we emit the param attr ID number for the function type of this
753 // function. In the future, we intend for attrs to be properties of
754 // functions, instead of on the type. This is to support this future work.
755 Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs()));
756
Chris Lattner76520192007-05-03 22:34:03 +0000757 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
Chris Lattner7337ab92007-05-06 00:00:00 +0000758 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest
759 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest
760 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000761
Chris Lattnerd309c752007-05-01 02:13:26 +0000762 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +0000763 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
764 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
765
766 // Emit type/value pairs for varargs params.
767 if (FTy->isVarArg()) {
Chris Lattner7337ab92007-05-06 00:00:00 +0000768 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
769 i != e; ++i)
770 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
Chris Lattnerd309c752007-05-01 02:13:26 +0000771 }
772 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000773 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000774 case Instruction::Unwind:
775 Code = bitc::FUNC_CODE_INST_UNWIND;
776 break;
777 case Instruction::Unreachable:
778 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattner94687ac2007-05-06 01:28:01 +0000779 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000780 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000781
782 case Instruction::PHI:
783 Code = bitc::FUNC_CODE_INST_PHI;
784 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +0000785 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
786 Vals.push_back(VE.getValueID(I.getOperand(i)));
787 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000788
Chris Lattnerd309c752007-05-01 02:13:26 +0000789 case Instruction::Malloc:
790 Code = bitc::FUNC_CODE_INST_MALLOC;
791 Vals.push_back(VE.getTypeID(I.getType()));
792 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
793 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
794 break;
795
796 case Instruction::Free:
797 Code = bitc::FUNC_CODE_INST_FREE;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000798 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000799 break;
800
801 case Instruction::Alloca:
802 Code = bitc::FUNC_CODE_INST_ALLOCA;
803 Vals.push_back(VE.getTypeID(I.getType()));
804 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
805 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
806 break;
807
808 case Instruction::Load:
809 Code = bitc::FUNC_CODE_INST_LOAD;
Chris Lattner7337ab92007-05-06 00:00:00 +0000810 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
811 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
812
Chris Lattnerd309c752007-05-01 02:13:26 +0000813 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
814 Vals.push_back(cast<LoadInst>(I).isVolatile());
815 break;
816 case Instruction::Store:
817 Code = bitc::FUNC_CODE_INST_STORE;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000818 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // val.
819 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
Chris Lattnerd309c752007-05-01 02:13:26 +0000820 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
821 Vals.push_back(cast<StoreInst>(I).isVolatile());
822 break;
823 case Instruction::Call: {
Chris Lattnera9bb7132007-05-08 05:38:01 +0000824 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
825 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
826
Chris Lattnerd309c752007-05-01 02:13:26 +0000827 Code = bitc::FUNC_CODE_INST_CALL;
Chris Lattnera9bb7132007-05-08 05:38:01 +0000828
829 // Note: we emit the param attr ID number for the function type of this
830 // function. In the future, we intend for attrs to be properties of
831 // functions, instead of on the type. This is to support this future work.
832 Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs()));
833
Chris Lattner76520192007-05-03 22:34:03 +0000834 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
Jeff Cohen332376b2007-05-06 03:12:47 +0000835 unsigned(cast<CallInst>(I).isTailCall()));
Chris Lattner7337ab92007-05-06 00:00:00 +0000836 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee
Chris Lattnerd309c752007-05-01 02:13:26 +0000837
838 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +0000839 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
840 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
841
Chris Lattner60ce9b52007-05-01 07:03:37 +0000842 // Emit type/value pairs for varargs params.
843 if (FTy->isVarArg()) {
844 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
Chris Lattner60ce9b52007-05-01 07:03:37 +0000845 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
Chris Lattner7337ab92007-05-06 00:00:00 +0000846 i != e; ++i)
847 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
Chris Lattnerd309c752007-05-01 02:13:26 +0000848 }
849 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +0000850 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000851 case Instruction::VAArg:
852 Code = bitc::FUNC_CODE_INST_VAARG;
853 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
854 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
855 Vals.push_back(VE.getTypeID(I.getType())); // restype.
856 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000857 }
858
859 Stream.EmitRecord(Code, Vals, AbbrevToUse);
860 Vals.clear();
861}
862
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000863// Emit names for globals/functions etc.
864static void WriteValueSymbolTable(const ValueSymbolTable &VST,
865 const ValueEnumerator &VE,
866 BitstreamWriter &Stream) {
867 if (VST.empty()) return;
Chris Lattnerff294a42007-05-05 01:26:50 +0000868 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2e7899d2007-05-04 20:34:50 +0000869
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000870 // FIXME: Set up the abbrev, we know how many values there are!
871 // FIXME: We know if the type names can use 7-bit ascii.
872 SmallVector<unsigned, 64> NameVals;
873
874 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
875 SI != SE; ++SI) {
Chris Lattner59698302007-05-04 20:52:02 +0000876
877 const ValueName &Name = *SI;
878
879 // Figure out the encoding to use for the name.
880 bool is7Bit = true;
Chris Lattnerff294a42007-05-05 01:26:50 +0000881 bool isChar6 = true;
882 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
883 C != E; ++C) {
884 if (isChar6)
885 isChar6 = BitCodeAbbrevOp::isChar6(*C);
886 if ((unsigned char)*C & 128) {
Chris Lattner59698302007-05-04 20:52:02 +0000887 is7Bit = false;
Chris Lattnerff294a42007-05-05 01:26:50 +0000888 break; // don't bother scanning the rest.
Chris Lattner59698302007-05-04 20:52:02 +0000889 }
Chris Lattnerff294a42007-05-05 01:26:50 +0000890 }
Chris Lattner59698302007-05-04 20:52:02 +0000891
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000892 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000893
Chris Lattnerfd1ae952007-05-04 21:31:13 +0000894 // VST_ENTRY: [valueid, namechar x N]
895 // VST_BBENTRY: [bbid, namechar x N]
Chris Lattnere825ed52007-05-03 22:18:21 +0000896 unsigned Code;
897 if (isa<BasicBlock>(SI->getValue())) {
898 Code = bitc::VST_CODE_BBENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000899 if (isChar6)
900 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000901 } else {
902 Code = bitc::VST_CODE_ENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +0000903 if (isChar6)
904 AbbrevToUse = VST_ENTRY_6_ABBREV;
905 else if (is7Bit)
906 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +0000907 }
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000908
Chris Lattnere825ed52007-05-03 22:18:21 +0000909 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +0000910 for (const char *P = Name.getKeyData(),
911 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000912 NameVals.push_back((unsigned char)*P);
913
914 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +0000915 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000916 NameVals.clear();
917 }
918 Stream.ExitBlock();
919}
920
Chris Lattner8d35c792007-04-26 03:50:57 +0000921/// WriteFunction - Emit a function body to the module stream.
Chris Lattner198f34a2007-04-26 03:27:58 +0000922static void WriteFunction(const Function &F, ValueEnumerator &VE,
923 BitstreamWriter &Stream) {
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000924 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chris Lattner8d35c792007-04-26 03:50:57 +0000925 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000926
927 SmallVector<unsigned, 64> Vals;
928
929 // Emit the number of basic blocks, so the reader can create them ahead of
930 // time.
931 Vals.push_back(VE.getBasicBlocks().size());
932 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
933 Vals.clear();
934
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000935 // If there are function-local constants, emit them now.
936 unsigned CstStart, CstEnd;
937 VE.getFunctionConstantRange(CstStart, CstEnd);
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000938 WriteConstants(CstStart, CstEnd, VE, Stream, false);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000939
Chris Lattner7337ab92007-05-06 00:00:00 +0000940 // Keep a running idea of what the instruction ID is.
941 unsigned InstID = CstEnd;
942
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000943 // Finally, emit all the instructions, in order.
944 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner7337ab92007-05-06 00:00:00 +0000945 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
946 I != E; ++I) {
947 WriteInstruction(*I, InstID, VE, Stream, Vals);
948 if (I->getType() != Type::VoidTy)
949 ++InstID;
950 }
Chris Lattner198f34a2007-04-26 03:27:58 +0000951
Chris Lattnerbe1f9932007-05-01 02:14:57 +0000952 // Emit names for all the instructions etc.
953 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
954
Chris Lattner8d35c792007-04-26 03:50:57 +0000955 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000956 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +0000957}
958
959/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
960static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
961 const ValueEnumerator &VE,
962 BitstreamWriter &Stream) {
963 if (TST.empty()) return;
964
965 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
966
Chris Lattner7a263ea2007-05-05 00:47:19 +0000967 // 7-bit fixed width VST_CODE_ENTRY strings.
968 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
969 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
970 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
971 Log2_32_Ceil(VE.getTypes().size()+1)));
972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
974 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner198f34a2007-04-26 03:27:58 +0000975
976 SmallVector<unsigned, 64> NameVals;
977
978 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
979 TI != TE; ++TI) {
Chris Lattner7a263ea2007-05-05 00:47:19 +0000980 // TST_ENTRY: [typeid, namechar x N]
Chris Lattner198f34a2007-04-26 03:27:58 +0000981 NameVals.push_back(VE.getTypeID(TI->second));
982
983 const std::string &Str = TI->first;
Chris Lattner7a263ea2007-05-05 00:47:19 +0000984 bool is7Bit = true;
985 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
986 NameVals.push_back((unsigned char)Str[i]);
987 if (Str[i] & 128)
988 is7Bit = false;
989 }
Chris Lattner198f34a2007-04-26 03:27:58 +0000990
991 // Emit the finished record.
Chris Lattner7a263ea2007-05-05 00:47:19 +0000992 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
Chris Lattner198f34a2007-04-26 03:27:58 +0000993 NameVals.clear();
994 }
995
996 Stream.ExitBlock();
997}
998
Chris Lattner07faafc2007-05-04 18:26:27 +0000999// Emit blockinfo, which defines the standard abbreviations etc.
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001000static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
Chris Lattner07faafc2007-05-04 18:26:27 +00001001 // We only want to emit block info records for blocks that have multiple
1002 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1003 // blocks can defined their abbrevs inline.
Chris Lattnere17b6582007-05-05 00:17:00 +00001004 Stream.EnterBlockInfoBlock(2);
Chris Lattner07faafc2007-05-04 18:26:27 +00001005
Chris Lattnere17b6582007-05-05 00:17:00 +00001006 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1007 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1008 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1009 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1010 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1011 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1012 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1013 Abbv) != VST_ENTRY_8_ABBREV)
1014 assert(0 && "Unexpected abbrev ordering!");
1015 }
1016
1017 { // 7-bit fixed width VST_ENTRY strings.
1018 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1019 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1020 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1021 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1022 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1023 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1024 Abbv) != VST_ENTRY_7_ABBREV)
1025 assert(0 && "Unexpected abbrev ordering!");
1026 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001027 { // 6-bit char6 VST_ENTRY strings.
1028 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1029 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1030 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1031 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1032 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1033 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1034 Abbv) != VST_ENTRY_6_ABBREV)
1035 assert(0 && "Unexpected abbrev ordering!");
1036 }
1037 { // 6-bit char6 VST_BBENTRY strings.
Chris Lattnere17b6582007-05-05 00:17:00 +00001038 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1039 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1040 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1041 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnerff294a42007-05-05 01:26:50 +00001042 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Chris Lattnere17b6582007-05-05 00:17:00 +00001043 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnerff294a42007-05-05 01:26:50 +00001044 Abbv) != VST_BBENTRY_6_ABBREV)
Chris Lattnere17b6582007-05-05 00:17:00 +00001045 assert(0 && "Unexpected abbrev ordering!");
1046 }
1047
Chris Lattner440168b2007-05-05 07:44:49 +00001048
1049
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001050 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1051 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1052 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1053 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1054 Log2_32_Ceil(VE.getTypes().size()+1)));
1055 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1056 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1057 assert(0 && "Unexpected abbrev ordering!");
1058 }
1059
1060 { // INTEGER abbrev for CONSTANTS_BLOCK.
1061 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1062 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1063 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1064 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1065 Abbv) != CONSTANTS_INTEGER_ABBREV)
1066 assert(0 && "Unexpected abbrev ordering!");
1067 }
1068
1069 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1070 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1071 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1072 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1073 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1074 Log2_32_Ceil(VE.getTypes().size()+1)));
1075 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1076
1077 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1078 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1079 assert(0 && "Unexpected abbrev ordering!");
1080 }
1081 { // NULL abbrev for CONSTANTS_BLOCK.
1082 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1083 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1084 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1085 Abbv) != CONSTANTS_NULL_Abbrev)
1086 assert(0 && "Unexpected abbrev ordering!");
1087 }
1088
Chris Lattner440168b2007-05-05 07:44:49 +00001089 // FIXME: This should only use space for first class types!
1090
1091 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1092 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1093 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattner440168b2007-05-05 07:44:49 +00001094 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1095 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1096 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1097 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1098 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1099 assert(0 && "Unexpected abbrev ordering!");
1100 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001101 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1102 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1103 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1104 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1105 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1106 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1107 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1108 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1109 assert(0 && "Unexpected abbrev ordering!");
1110 }
1111 { // INST_CAST abbrev for FUNCTION_BLOCK.
1112 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1113 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1114 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1116 Log2_32_Ceil(VE.getTypes().size()+1)));
1117 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1118 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1119 Abbv) != FUNCTION_INST_CAST_ABBREV)
1120 assert(0 && "Unexpected abbrev ordering!");
1121 }
1122
Chris Lattner94687ac2007-05-06 01:28:01 +00001123 { // INST_RET abbrev for FUNCTION_BLOCK.
1124 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1125 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1126 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1127 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1128 assert(0 && "Unexpected abbrev ordering!");
1129 }
1130 { // INST_RET abbrev for FUNCTION_BLOCK.
1131 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1132 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1133 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1134 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1135 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1136 assert(0 && "Unexpected abbrev ordering!");
1137 }
1138 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1139 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1140 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1141 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1142 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1143 assert(0 && "Unexpected abbrev ordering!");
1144 }
Chris Lattner440168b2007-05-05 07:44:49 +00001145
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001146 Stream.ExitBlock();
1147}
1148
1149
1150/// WriteModule - Emit the specified module to the bitstream.
1151static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1152 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1153
1154 // Emit the version number if it is non-zero.
1155 if (CurVersion) {
1156 SmallVector<unsigned, 1> Vals;
1157 Vals.push_back(CurVersion);
1158 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1159 }
1160
1161 // Analyze the module, enumerating globals, functions, etc.
1162 ValueEnumerator VE(M);
1163
1164 // Emit blockinfo, which defines the standard abbreviations etc.
1165 WriteBlockInfo(VE, Stream);
1166
1167 // Emit information about parameter attributes.
1168 WriteParamAttrTable(VE, Stream);
1169
1170 // Emit information describing all of the types in the module.
1171 WriteTypeTable(VE, Stream);
1172
1173 // Emit top-level description of module, including target triple, inline asm,
1174 // descriptors for global variables, and function prototype info.
1175 WriteModuleInfo(M, VE, Stream);
1176
1177 // Emit constants.
1178 WriteModuleConstants(VE, Stream);
1179
1180 // If we have any aggregate values in the value table, purge them - these can
1181 // only be used to initialize global variables. Doing so makes the value
1182 // namespace smaller for code in functions.
1183 int NumNonAggregates = VE.PurgeAggregateValues();
1184 if (NumNonAggregates != -1) {
1185 SmallVector<unsigned, 1> Vals;
1186 Vals.push_back(NumNonAggregates);
1187 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1188 }
1189
1190 // Emit function bodies.
1191 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1192 if (!I->isDeclaration())
1193 WriteFunction(*I, VE, Stream);
1194
1195 // Emit the type symbol table information.
1196 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1197
1198 // Emit names for globals/functions etc.
1199 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1200
Chris Lattner07faafc2007-05-04 18:26:27 +00001201 Stream.ExitBlock();
1202}
1203
1204
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001205/// WriteBitcodeToFile - Write the specified module to the specified output
1206/// stream.
1207void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1208 std::vector<unsigned char> Buffer;
1209 BitstreamWriter Stream(Buffer);
1210
1211 Buffer.reserve(256*1024);
1212
1213 // Emit the file header.
1214 Stream.Emit((unsigned)'B', 8);
1215 Stream.Emit((unsigned)'C', 8);
1216 Stream.Emit(0x0, 4);
1217 Stream.Emit(0xC, 4);
1218 Stream.Emit(0xE, 4);
1219 Stream.Emit(0xD, 4);
1220
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001221 // Emit the module.
1222 WriteModule(M, Stream);
1223
1224 // Write the generated bitstream to "Out".
1225 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattner4eab2e52007-05-06 19:19:23 +00001226
1227 // Make sure it hits disk now.
1228 Out.flush();
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001229}