Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp

Issue 14813032: Make abbreviations explicit in pnacl-freeze/thaw. (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Small cleanups suggested by Jan in CL. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===--- Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp - Bitcode Writer -------===// 1 //===--- Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp - Bitcode Writer -------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // Bitcode writer implementation. 10 // Bitcode writer implementation.
11 // 11 //
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 #define DEBUG_TYPE "NaClBitcodeWriter"
15
14 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" 16 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
15 #include "NaClValueEnumerator.h" 17 #include "NaClValueEnumerator.h"
16 #include "llvm/ADT/Triple.h" 18 #include "llvm/ADT/Triple.h"
17 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h" 19 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h"
18 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" 20 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h"
19 #include "llvm/IR/Constants.h" 21 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h" 22 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/InlineAsm.h" 23 #include "llvm/IR/InlineAsm.h"
22 #include "llvm/IR/Instructions.h" 24 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h" 25 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h" 26 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/ValueSymbolTable.h" 27 #include "llvm/IR/ValueSymbolTable.h"
28 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/ErrorHandling.h" 30 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MathExtras.h" 31 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/Program.h" 32 #include "llvm/Support/Program.h"
30 #include "llvm/Support/raw_ostream.h" 33 #include "llvm/Support/raw_ostream.h"
31 #include <cctype> 34 #include <cctype>
32 #include <map> 35 #include <map>
33 using namespace llvm; 36 using namespace llvm;
34 37
35 /// These are manifest constants used by the bitcode writer. They do not need to 38 /// These are manifest constants used by the bitcode writer. They do
36 /// be kept in sync with the reader, but need to be consistent within this file. 39 /// not need to be kept in sync with the reader, but need to be
40 /// consistent within this file.
41 ///
42 /// Note that for each block type GROUP, the last entry should be of
43 /// the form:
44 ///
45 /// GROUP_MAX_ABBREV = GROUP_LAST_ABBREV,
46 ///
47 /// where GROUP_LAST_ABBREV is the last defined abbreviation. See
48 /// include file "llvm/Bitcode/NaCl/NaClBitCodes.h" for more
49 /// information on how groups should be defined.
37 enum { 50 enum {
38 // VALUE_SYMTAB_BLOCK abbrev id's. 51 // VALUE_SYMTAB_BLOCK abbrev id's.
39 VST_ENTRY_8_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, 52 VST_ENTRY_8_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
40 VST_ENTRY_7_ABBREV, 53 VST_ENTRY_7_ABBREV,
41 VST_ENTRY_6_ABBREV, 54 VST_ENTRY_6_ABBREV,
42 VST_BBENTRY_6_ABBREV, 55 VST_BBENTRY_6_ABBREV,
56 VST_MAX_ABBREV = VST_BBENTRY_6_ABBREV,
43 57
44 // CONSTANTS_BLOCK abbrev id's. 58 // CONSTANTS_BLOCK abbrev id's.
45 CONSTANTS_SETTYPE_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, 59 CONSTANTS_SETTYPE_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
46 CONSTANTS_INTEGER_ABBREV, 60 CONSTANTS_INTEGER_ABBREV,
47 CONSTANTS_CE_CAST_Abbrev, 61 CONSTANTS_CE_CAST_Abbrev,
48 CONSTANTS_NULL_Abbrev, 62 CONSTANTS_NULL_Abbrev,
63 CONSTANTS_MAX_ABBREV = CONSTANTS_NULL_Abbrev,
64
65 // CONSTANTS_BLOCK abbrev id's when global (extends list above).
66 CST_CONSTANTS_AGGREGATE_ABBREV = CONSTANTS_MAX_ABBREV+1,
67 CST_CONSTANTS_STRING_ABBREV,
68 CST_CONSTANTS_CSTRING_7_ABBREV,
69 CST_CONSTANTS_CSTRING_6_ABBREV,
70 CST_CONSTANTS_MAX_ABBREV = CST_CONSTANTS_CSTRING_6_ABBREV,
49 71
50 // FUNCTION_BLOCK abbrev id's. 72 // FUNCTION_BLOCK abbrev id's.
51 FUNCTION_INST_LOAD_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, 73 FUNCTION_INST_LOAD_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
52 FUNCTION_INST_BINOP_ABBREV, 74 FUNCTION_INST_BINOP_ABBREV,
53 FUNCTION_INST_BINOP_FLAGS_ABBREV, 75 FUNCTION_INST_BINOP_FLAGS_ABBREV,
54 FUNCTION_INST_CAST_ABBREV, 76 FUNCTION_INST_CAST_ABBREV,
55 FUNCTION_INST_RET_VOID_ABBREV, 77 FUNCTION_INST_RET_VOID_ABBREV,
56 FUNCTION_INST_RET_VAL_ABBREV, 78 FUNCTION_INST_RET_VAL_ABBREV,
57 FUNCTION_INST_UNREACHABLE_ABBREV, 79 FUNCTION_INST_UNREACHABLE_ABBREV,
80 FUNCTION_INST_MAX_ABBREV = FUNCTION_INST_UNREACHABLE_ABBREV,
81
82 // TYPE_BLOCK_ID_NEW abbrev id's.
83 TYPE_POINTER_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
84 TYPE_FUNCTION_ABBREV,
85 TYPE_STRUCT_ANON_ABBREV,
86 TYPE_STRUCT_NAME_ABBREV,
87 TYPE_STRUCT_NAMED_ABBREV,
88 TYPE_ARRAY_ABBREV,
89 TYPE_MAX_ABBREV = TYPE_ARRAY_ABBREV,
90
91 // META_DATA_BLOCK abbrev id's.
92 METADATA_STRING_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
93 METADATA_MAX_ABBREV = METADATA_STRING_ABBREV,
94
95 // MODULE_BLOCK abbrev id's.
96 MODULE_GLOBALVAR_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
97 MODULE_MAX_ABBREV = MODULE_GLOBALVAR_ABBREV,
58 98
59 // SwitchInst Magic 99 // SwitchInst Magic
60 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 100 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
61 }; 101 };
62 102
63 static unsigned GetEncodedCastOpcode(unsigned Opcode) { 103 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
64 switch (Opcode) { 104 switch (Opcode) {
65 default: llvm_unreachable("Unknown cast instruction!"); 105 default: llvm_unreachable("Unknown cast instruction!");
66 case Instruction::Trunc : return naclbitc::CAST_TRUNC; 106 case Instruction::Trunc : return naclbitc::CAST_TRUNC;
67 case Instruction::ZExt : return naclbitc::CAST_ZEXT; 107 case Instruction::ZExt : return naclbitc::CAST_ZEXT;
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 } 193 }
154 194
155 // Emit the finished record. 195 // Emit the finished record.
156 Stream.EmitRecord(Code, Vals, AbbrevToUse); 196 Stream.EmitRecord(Code, Vals, AbbrevToUse);
157 } 197 }
158 198
159 static void WriteAttributeGroupTable(const NaClValueEnumerator &VE, 199 static void WriteAttributeGroupTable(const NaClValueEnumerator &VE,
160 NaClBitstreamWriter &Stream) { 200 NaClBitstreamWriter &Stream) {
161 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups(); 201 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
162 if (AttrGrps.empty()) return; 202 if (AttrGrps.empty()) return;
203 DEBUG(dbgs() << "-> WriteAbbributeGroupTable\n");
163 204
164 Stream.EnterSubblock(naclbitc::PARAMATTR_GROUP_BLOCK_ID, 3); 205 Stream.EnterSubblock(naclbitc::PARAMATTR_GROUP_BLOCK_ID);
165 206
166 SmallVector<uint64_t, 64> Record; 207 SmallVector<uint64_t, 64> Record;
167 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) { 208 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
168 AttributeSet AS = AttrGrps[i]; 209 AttributeSet AS = AttrGrps[i];
169 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) { 210 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
170 AttributeSet A = AS.getSlotAttributes(i); 211 AttributeSet A = AS.getSlotAttributes(i);
171 212
172 Record.push_back(VE.getAttributeGroupID(A)); 213 Record.push_back(VE.getAttributeGroupID(A));
173 Record.push_back(AS.getSlotIndex(i)); 214 Record.push_back(AS.getSlotIndex(i));
174 215
(...skipping 20 matching lines...) Expand all
195 } 236 }
196 } 237 }
197 } 238 }
198 239
199 Stream.EmitRecord(naclbitc::PARAMATTR_GRP_CODE_ENTRY, Record); 240 Stream.EmitRecord(naclbitc::PARAMATTR_GRP_CODE_ENTRY, Record);
200 Record.clear(); 241 Record.clear();
201 } 242 }
202 } 243 }
203 244
204 Stream.ExitBlock(); 245 Stream.ExitBlock();
246 DEBUG(dbgs() << "<- WriteAbbributeGroupTable\n");
205 } 247 }
206 248
207 static void WriteAttributeTable(const NaClValueEnumerator &VE, 249 static void WriteAttributeTable(const NaClValueEnumerator &VE,
208 NaClBitstreamWriter &Stream) { 250 NaClBitstreamWriter &Stream) {
209 const std::vector<AttributeSet> &Attrs = VE.getAttributes(); 251 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
210 if (Attrs.empty()) return; 252 if (Attrs.empty()) return;
253 DEBUG(dbgs() << "-> WriteAttributeTable\n");
211 254
212 Stream.EnterSubblock(naclbitc::PARAMATTR_BLOCK_ID, 3); 255 Stream.EnterSubblock(naclbitc::PARAMATTR_BLOCK_ID);
213 256
214 SmallVector<uint64_t, 64> Record; 257 SmallVector<uint64_t, 64> Record;
215 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { 258 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
216 const AttributeSet &A = Attrs[i]; 259 const AttributeSet &A = Attrs[i];
217 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) 260 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
218 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i))); 261 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
219 262
220 Stream.EmitRecord(naclbitc::PARAMATTR_CODE_ENTRY, Record); 263 Stream.EmitRecord(naclbitc::PARAMATTR_CODE_ENTRY, Record);
221 Record.clear(); 264 Record.clear();
222 } 265 }
223 266
224 Stream.ExitBlock(); 267 Stream.ExitBlock();
268 DEBUG(dbgs() << "<- WriteAttributeTable\n");
225 } 269 }
226 270
227 /// WriteTypeTable - Write out the type table for a module. 271 /// WriteTypeTable - Write out the type table for a module.
228 static void WriteTypeTable(const NaClValueEnumerator &VE, 272 static void WriteTypeTable(const NaClValueEnumerator &VE,
229 NaClBitstreamWriter &Stream) { 273 NaClBitstreamWriter &Stream) {
274 DEBUG(dbgs() << "-> WriteTypeTable\n");
230 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes(); 275 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes();
231 276
232 Stream.EnterSubblock(naclbitc::TYPE_BLOCK_ID_NEW, 277 Stream.EnterSubblock(naclbitc::TYPE_BLOCK_ID_NEW, TYPE_MAX_ABBREV);
233 4 /*count from # abbrevs */); 278
234 SmallVector<uint64_t, 64> TypeVals; 279 SmallVector<uint64_t, 64> TypeVals;
235 280
281
236 // Note: modify to use maximum number of bits if under cutoff. Otherwise, 282 // Note: modify to use maximum number of bits if under cutoff. Otherwise,
237 // use VBR to take advantage that frequently referenced types have 283 // use VBR to take advantage that frequently referenced types have
238 // small IDs. 284 // small IDs.
239 // 285 //
240 // Note: Cutoff chosen based on experiments on pnacl-translate.pexe. 286 // Note: Cutoff chosen based on experiments on pnacl-translate.pexe.
241 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); 287 uint64_t NumBits = NaClBitsNeededForValue(VE.getTypes().size());
242 static const uint64_t TypeVBRCutoff = 6; 288 static const uint64_t TypeVBRCutoff = 6;
243 uint64_t TypeIdNumBits = (NumBits <= TypeVBRCutoff ? NumBits : TypeVBRCutoff); 289 uint64_t TypeIdNumBits = (NumBits <= TypeVBRCutoff ? NumBits : TypeVBRCutoff);
244 NaClBitCodeAbbrevOp::Encoding TypeIdEncoding = 290 NaClBitCodeAbbrevOp::Encoding TypeIdEncoding =
245 (NumBits <= TypeVBRCutoff 291 (NumBits <= TypeVBRCutoff
246 ? NaClBitCodeAbbrevOp::Fixed : NaClBitCodeAbbrevOp::VBR); 292 ? NaClBitCodeAbbrevOp::Fixed : NaClBitCodeAbbrevOp::VBR);
247 293
248 // Abbrev for TYPE_CODE_POINTER. 294 // Abbrev for TYPE_CODE_POINTER.
249 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 295 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
250 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER)); 296 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER));
251 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); 297 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits));
252 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0 298 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0
253 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 299 if (TYPE_POINTER_ABBREV != Stream.EmitAbbrev(Abbv))
300 llvm_unreachable("Unexpected abbrev ordering!");
254 301
255 // Abbrev for TYPE_CODE_FUNCTION. 302 // Abbrev for TYPE_CODE_FUNCTION.
256 Abbv = new NaClBitCodeAbbrev(); 303 Abbv = new NaClBitCodeAbbrev();
257 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION)); 304 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION));
258 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg 305 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg
259 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 306 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
260 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); 307 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
261 308 if (TYPE_FUNCTION_ABBREV != Stream.EmitAbbrev(Abbv))
262 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 309 llvm_unreachable("Unexpected abbrev ordering!");
263 310
264 // Abbrev for TYPE_CODE_STRUCT_ANON. 311 // Abbrev for TYPE_CODE_STRUCT_ANON.
265 Abbv = new NaClBitCodeAbbrev(); 312 Abbv = new NaClBitCodeAbbrev();
266 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON)); 313 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON));
267 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked 314 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked
268 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 315 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
269 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); 316 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
270 317 if (TYPE_STRUCT_ANON_ABBREV != Stream.EmitAbbrev(Abbv))
271 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); 318 llvm_unreachable("Unexpected abbrev ordering!");
272 319
273 // Abbrev for TYPE_CODE_STRUCT_NAME. 320 // Abbrev for TYPE_CODE_STRUCT_NAME.
274 Abbv = new NaClBitCodeAbbrev(); 321 Abbv = new NaClBitCodeAbbrev();
275 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME)); 322 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME));
276 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 323 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
277 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); 324 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6));
278 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); 325 if (TYPE_STRUCT_NAME_ABBREV != Stream.EmitAbbrev(Abbv))
326 llvm_unreachable("Unexpected abbrev ordering!");
279 327
280 // Abbrev for TYPE_CODE_STRUCT_NAMED. 328 // Abbrev for TYPE_CODE_STRUCT_NAMED.
281 Abbv = new NaClBitCodeAbbrev(); 329 Abbv = new NaClBitCodeAbbrev();
282 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED)); 330 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED));
283 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked 331 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked
284 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 332 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
285 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); 333 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
286 334 if (TYPE_STRUCT_NAMED_ABBREV != Stream.EmitAbbrev(Abbv))
287 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); 335 llvm_unreachable("Unexpected abbrev ordering!");
288 336
289 // Abbrev for TYPE_CODE_ARRAY. 337 // Abbrev for TYPE_CODE_ARRAY.
290 Abbv = new NaClBitCodeAbbrev(); 338 Abbv = new NaClBitCodeAbbrev();
291 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY)); 339 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY));
292 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size 340 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size
293 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); 341 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
294 342 if (TYPE_ARRAY_ABBREV != Stream.EmitAbbrev(Abbv))
295 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 343 llvm_unreachable("Unexpected abbrev ordering!");
296 344
297 // Emit an entry count so the reader can reserve space. 345 // Emit an entry count so the reader can reserve space.
298 TypeVals.push_back(TypeList.size()); 346 TypeVals.push_back(TypeList.size());
299 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals); 347 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals);
300 TypeVals.clear(); 348 TypeVals.clear();
301 349
302 // Loop over all of the types, emitting each in turn. 350 // Loop over all of the types, emitting each in turn.
303 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 351 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
304 Type *T = TypeList[i]; 352 Type *T = TypeList[i];
305 int AbbrevToUse = 0; 353 int AbbrevToUse = 0;
(...skipping 16 matching lines...) Expand all
322 Code = naclbitc::TYPE_CODE_INTEGER; 370 Code = naclbitc::TYPE_CODE_INTEGER;
323 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 371 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
324 break; 372 break;
325 case Type::PointerTyID: { 373 case Type::PointerTyID: {
326 PointerType *PTy = cast<PointerType>(T); 374 PointerType *PTy = cast<PointerType>(T);
327 // POINTER: [pointee type, address space] 375 // POINTER: [pointee type, address space]
328 Code = naclbitc::TYPE_CODE_POINTER; 376 Code = naclbitc::TYPE_CODE_POINTER;
329 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 377 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
330 unsigned AddressSpace = PTy->getAddressSpace(); 378 unsigned AddressSpace = PTy->getAddressSpace();
331 TypeVals.push_back(AddressSpace); 379 TypeVals.push_back(AddressSpace);
332 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 380 if (AddressSpace == 0) AbbrevToUse = TYPE_POINTER_ABBREV;
333 break; 381 break;
334 } 382 }
335 case Type::FunctionTyID: { 383 case Type::FunctionTyID: {
336 FunctionType *FT = cast<FunctionType>(T); 384 FunctionType *FT = cast<FunctionType>(T);
337 // FUNCTION: [isvararg, retty, paramty x N] 385 // FUNCTION: [isvararg, retty, paramty x N]
338 Code = naclbitc::TYPE_CODE_FUNCTION; 386 Code = naclbitc::TYPE_CODE_FUNCTION;
339 TypeVals.push_back(FT->isVarArg()); 387 TypeVals.push_back(FT->isVarArg());
340 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 388 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
341 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 389 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
342 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 390 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
343 AbbrevToUse = FunctionAbbrev; 391 AbbrevToUse = TYPE_FUNCTION_ABBREV;
344 break; 392 break;
345 } 393 }
346 case Type::StructTyID: { 394 case Type::StructTyID: {
347 StructType *ST = cast<StructType>(T); 395 StructType *ST = cast<StructType>(T);
348 // STRUCT: [ispacked, eltty x N] 396 // STRUCT: [ispacked, eltty x N]
349 TypeVals.push_back(ST->isPacked()); 397 TypeVals.push_back(ST->isPacked());
350 // Output all of the element types. 398 // Output all of the element types.
351 for (StructType::element_iterator I = ST->element_begin(), 399 for (StructType::element_iterator I = ST->element_begin(),
352 E = ST->element_end(); I != E; ++I) 400 E = ST->element_end(); I != E; ++I)
353 TypeVals.push_back(VE.getTypeID(*I)); 401 TypeVals.push_back(VE.getTypeID(*I));
354 402
355 if (ST->isLiteral()) { 403 if (ST->isLiteral()) {
356 Code = naclbitc::TYPE_CODE_STRUCT_ANON; 404 Code = naclbitc::TYPE_CODE_STRUCT_ANON;
357 AbbrevToUse = StructAnonAbbrev; 405 AbbrevToUse = TYPE_STRUCT_ANON_ABBREV;
358 } else { 406 } else {
359 if (ST->isOpaque()) { 407 if (ST->isOpaque()) {
360 Code = naclbitc::TYPE_CODE_OPAQUE; 408 Code = naclbitc::TYPE_CODE_OPAQUE;
361 } else { 409 } else {
362 Code = naclbitc::TYPE_CODE_STRUCT_NAMED; 410 Code = naclbitc::TYPE_CODE_STRUCT_NAMED;
363 AbbrevToUse = StructNamedAbbrev; 411 AbbrevToUse = TYPE_STRUCT_NAMED_ABBREV;
364 } 412 }
365 413
366 // Emit the name if it is present. 414 // Emit the name if it is present.
367 if (!ST->getName().empty()) 415 if (!ST->getName().empty())
368 WriteStringRecord(naclbitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 416 WriteStringRecord(naclbitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
369 StructNameAbbrev, Stream); 417 TYPE_STRUCT_NAME_ABBREV, Stream);
370 } 418 }
371 break; 419 break;
372 } 420 }
373 case Type::ArrayTyID: { 421 case Type::ArrayTyID: {
374 ArrayType *AT = cast<ArrayType>(T); 422 ArrayType *AT = cast<ArrayType>(T);
375 // ARRAY: [numelts, eltty] 423 // ARRAY: [numelts, eltty]
376 Code = naclbitc::TYPE_CODE_ARRAY; 424 Code = naclbitc::TYPE_CODE_ARRAY;
377 TypeVals.push_back(AT->getNumElements()); 425 TypeVals.push_back(AT->getNumElements());
378 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 426 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
379 AbbrevToUse = ArrayAbbrev; 427 AbbrevToUse = TYPE_ARRAY_ABBREV;
380 break; 428 break;
381 } 429 }
382 case Type::VectorTyID: { 430 case Type::VectorTyID: {
383 VectorType *VT = cast<VectorType>(T); 431 VectorType *VT = cast<VectorType>(T);
384 // VECTOR [numelts, eltty] 432 // VECTOR [numelts, eltty]
385 Code = naclbitc::TYPE_CODE_VECTOR; 433 Code = naclbitc::TYPE_CODE_VECTOR;
386 TypeVals.push_back(VT->getNumElements()); 434 TypeVals.push_back(VT->getNumElements());
387 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 435 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
388 break; 436 break;
389 } 437 }
390 } 438 }
391 439
392 // Emit the finished record. 440 // Emit the finished record.
393 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 441 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
394 TypeVals.clear(); 442 TypeVals.clear();
395 } 443 }
396 444
397 Stream.ExitBlock(); 445 Stream.ExitBlock();
446 DEBUG(dbgs() << "<- WriteTypeTable\n");
398 } 447 }
399 448
400 static unsigned getEncodedLinkage(const GlobalValue *GV) { 449 static unsigned getEncodedLinkage(const GlobalValue *GV) {
401 switch (GV->getLinkage()) { 450 switch (GV->getLinkage()) {
402 case GlobalValue::ExternalLinkage: return 0; 451 case GlobalValue::ExternalLinkage: return 0;
403 case GlobalValue::WeakAnyLinkage: return 1; 452 case GlobalValue::WeakAnyLinkage: return 1;
404 case GlobalValue::AppendingLinkage: return 2; 453 case GlobalValue::AppendingLinkage: return 2;
405 case GlobalValue::InternalLinkage: return 3; 454 case GlobalValue::InternalLinkage: return 3;
406 case GlobalValue::LinkOnceAnyLinkage: return 4; 455 case GlobalValue::LinkOnceAnyLinkage: return 4;
407 case GlobalValue::DLLImportLinkage: return 5; 456 case GlobalValue::DLLImportLinkage: return 5;
(...skipping 28 matching lines...) Expand all
436 case GlobalVariable::InitialExecTLSModel: return 3; 485 case GlobalVariable::InitialExecTLSModel: return 3;
437 case GlobalVariable::LocalExecTLSModel: return 4; 486 case GlobalVariable::LocalExecTLSModel: return 4;
438 } 487 }
439 llvm_unreachable("Invalid TLS model"); 488 llvm_unreachable("Invalid TLS model");
440 } 489 }
441 490
442 // Emit top-level description of module, including target triple, inline asm, 491 // Emit top-level description of module, including target triple, inline asm,
443 // descriptors for global variables, and function prototype info. 492 // descriptors for global variables, and function prototype info.
444 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, 493 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE,
445 NaClBitstreamWriter &Stream) { 494 NaClBitstreamWriter &Stream) {
495 DEBUG(dbgs() << "-> WriteModuleInfo\n");
446 // Emit various pieces of data attached to a module. 496 // Emit various pieces of data attached to a module.
447 if (!M->getTargetTriple().empty()) 497 if (!M->getTargetTriple().empty())
448 WriteStringRecord(naclbitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 498 WriteStringRecord(naclbitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
449 0/*TODO*/, Stream); 499 0/*TODO*/, Stream);
450 if (!M->getDataLayout().empty()) 500 if (!M->getDataLayout().empty())
451 WriteStringRecord(naclbitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), 501 WriteStringRecord(naclbitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
452 0/*TODO*/, Stream); 502 0/*TODO*/, Stream);
453 if (!M->getModuleInlineAsm().empty()) 503 if (!M->getModuleInlineAsm().empty())
454 WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 504 WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
455 0/*TODO*/, Stream); 505 0/*TODO*/, Stream);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 unsigned &Entry = GCMap[F->getGC()]; 540 unsigned &Entry = GCMap[F->getGC()];
491 if (!Entry) { 541 if (!Entry) {
492 WriteStringRecord(naclbitc::MODULE_CODE_GCNAME, F->getGC(), 542 WriteStringRecord(naclbitc::MODULE_CODE_GCNAME, F->getGC(),
493 0/*TODO*/, Stream); 543 0/*TODO*/, Stream);
494 Entry = GCMap.size(); 544 Entry = GCMap.size();
495 } 545 }
496 } 546 }
497 } 547 }
498 548
499 // Emit abbrev for globals, now that we know # sections and max alignment. 549 // Emit abbrev for globals, now that we know # sections and max alignment.
500 unsigned SimpleGVarAbbrev = 0; 550 // Add an abbrev for common globals with no visibility or thread localness.
501 if (!M->global_empty()) { 551 // Don't bother emitting vis + thread local abbreviation.
502 // Add an abbrev for common globals with no visibility or thread localness. 552 {
503 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 553 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
504 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::MODULE_CODE_GLOBALVAR)); 554 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::MODULE_CODE_GLOBALVAR));
505 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 555 Abbv->Add(NaClBitCodeAbbrevOp(
506 Log2_32_Ceil(MaxGlobalType+1))); 556 NaClBitCodeAbbrevOp::Fixed, NaClBitsNeededForValue(MaxGlobalType)));
507 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // Constant. 557 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // Constant.
508 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // Initialize r. 558 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // Initialize r.
509 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // Linkage. 559 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // Linkage.
510 if (MaxAlignment == 0) // Alignment. 560 if (MaxAlignment == 0) // Alignment.
511 Abbv->Add(NaClBitCodeAbbrevOp(0)); 561 Abbv->Add(NaClBitCodeAbbrevOp(0));
512 else { 562 else {
513 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 563 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
514 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 564 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed,
515 Log2_32_Ceil(MaxEncAlignment+1))); 565 NaClBitsNeededForValue(MaxEncAlignment)));
516 } 566 }
517 if (SectionMap.empty()) // Section. 567 if (SectionMap.empty()) // Section.
518 Abbv->Add(NaClBitCodeAbbrevOp(0)); 568 Abbv->Add(NaClBitCodeAbbrevOp(0));
519 else 569 else
520 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 570 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed,
521 Log2_32_Ceil(SectionMap.size()+1))); 571 NaClBitsNeededForValue(SectionMap.size())));
522 // Don't bother emitting vis + thread local. 572 if (MODULE_GLOBALVAR_ABBREV != Stream.EmitAbbrev(Abbv))
523 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 573 llvm_unreachable("Unexpected abbrev ordering!");
524 } 574 }
525 575
526 // Emit the global variable information. 576 // Emit the global variable information.
527 SmallVector<unsigned, 64> Vals; 577 SmallVector<unsigned, 64> Vals;
528 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 578 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
529 GV != E; ++GV) { 579 GV != E; ++GV) {
530 unsigned AbbrevToUse = 0; 580 unsigned AbbrevToUse = 0;
531 581
532 // GLOBALVAR: [type, isconst, initid, 582 // GLOBALVAR: [type, isconst, initid,
533 // linkage, alignment, section, visibility, threadlocal, 583 // linkage, alignment, section, visibility, threadlocal,
534 // unnamed_addr] 584 // unnamed_addr]
535 Vals.push_back(VE.getTypeID(GV->getType())); 585 Vals.push_back(VE.getTypeID(GV->getType()));
536 Vals.push_back(GV->isConstant()); 586 Vals.push_back(GV->isConstant());
537 Vals.push_back(GV->isDeclaration() ? 0 : 587 Vals.push_back(GV->isDeclaration() ? 0 :
538 (VE.getValueID(GV->getInitializer()) + 1)); 588 (VE.getValueID(GV->getInitializer()) + 1));
539 Vals.push_back(getEncodedLinkage(GV)); 589 Vals.push_back(getEncodedLinkage(GV));
540 Vals.push_back(Log2_32(GV->getAlignment())+1); 590 Vals.push_back(Log2_32(GV->getAlignment())+1);
541 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); 591 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
542 if (GV->isThreadLocal() || 592 if (GV->isThreadLocal() ||
543 GV->getVisibility() != GlobalValue::DefaultVisibility || 593 GV->getVisibility() != GlobalValue::DefaultVisibility ||
544 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) { 594 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) {
545 Vals.push_back(getEncodedVisibility(GV)); 595 Vals.push_back(getEncodedVisibility(GV));
546 Vals.push_back(getEncodedThreadLocalMode(GV)); 596 Vals.push_back(getEncodedThreadLocalMode(GV));
547 Vals.push_back(GV->hasUnnamedAddr()); 597 Vals.push_back(GV->hasUnnamedAddr());
548 Vals.push_back(GV->isExternallyInitialized()); 598 Vals.push_back(GV->isExternallyInitialized());
549 } else { 599 } else {
550 AbbrevToUse = SimpleGVarAbbrev; 600 AbbrevToUse = MODULE_GLOBALVAR_ABBREV;
551 } 601 }
552 602
553 Stream.EmitRecord(naclbitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 603 Stream.EmitRecord(naclbitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
554 Vals.clear(); 604 Vals.clear();
555 } 605 }
556 606
557 // Emit the function proto information. 607 // Emit the function proto information.
558 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 608 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
559 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, 609 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
560 // section, visibility, gc, unnamed_addr] 610 // section, visibility, gc, unnamed_addr]
(...skipping 18 matching lines...) Expand all
579 AI != E; ++AI) { 629 AI != E; ++AI) {
580 // ALIAS: [alias type, aliasee val#, linkage, visibility] 630 // ALIAS: [alias type, aliasee val#, linkage, visibility]
581 Vals.push_back(VE.getTypeID(AI->getType())); 631 Vals.push_back(VE.getTypeID(AI->getType()));
582 Vals.push_back(VE.getValueID(AI->getAliasee())); 632 Vals.push_back(VE.getValueID(AI->getAliasee()));
583 Vals.push_back(getEncodedLinkage(AI)); 633 Vals.push_back(getEncodedLinkage(AI));
584 Vals.push_back(getEncodedVisibility(AI)); 634 Vals.push_back(getEncodedVisibility(AI));
585 unsigned AbbrevToUse = 0; 635 unsigned AbbrevToUse = 0;
586 Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 636 Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
587 Vals.clear(); 637 Vals.clear();
588 } 638 }
639 DEBUG(dbgs() << "<- WriteModuleInfo\n");
589 } 640 }
590 641
591 static uint64_t GetOptimizationFlags(const Value *V) { 642 static uint64_t GetOptimizationFlags(const Value *V) {
592 uint64_t Flags = 0; 643 uint64_t Flags = 0;
593 644
594 if (const OverflowingBinaryOperator *OBO = 645 if (const OverflowingBinaryOperator *OBO =
595 dyn_cast<OverflowingBinaryOperator>(V)) { 646 dyn_cast<OverflowingBinaryOperator>(V)) {
596 if (OBO->hasNoSignedWrap()) 647 if (OBO->hasNoSignedWrap())
597 Flags |= 1 << naclbitc::OBO_NO_SIGNED_WRAP; 648 Flags |= 1 << naclbitc::OBO_NO_SIGNED_WRAP;
598 if (OBO->hasNoUnsignedWrap()) 649 if (OBO->hasNoUnsignedWrap())
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 } 684 }
634 unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE : 685 unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE :
635 naclbitc::METADATA_NODE; 686 naclbitc::METADATA_NODE;
636 Stream.EmitRecord(MDCode, Record, 0); 687 Stream.EmitRecord(MDCode, Record, 0);
637 Record.clear(); 688 Record.clear();
638 } 689 }
639 690
640 static void WriteModuleMetadata(const Module *M, 691 static void WriteModuleMetadata(const Module *M,
641 const NaClValueEnumerator &VE, 692 const NaClValueEnumerator &VE,
642 NaClBitstreamWriter &Stream) { 693 NaClBitstreamWriter &Stream) {
694 DEBUG(dbgs() << "-> WriteModuleMetadata\n");
643 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues(); 695 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues();
644 bool StartedMetadataBlock = false; 696 bool StartedMetadataBlock = false;
645 unsigned MDSAbbrev = 0;
646 SmallVector<uint64_t, 64> Record; 697 SmallVector<uint64_t, 64> Record;
647 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 698 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
648 699
649 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { 700 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
650 if (!N->isFunctionLocal() || !N->getFunction()) { 701 if (!N->isFunctionLocal() || !N->getFunction()) {
651 if (!StartedMetadataBlock) { 702 if (!StartedMetadataBlock) {
652 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 703 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID);
653 StartedMetadataBlock = true; 704 StartedMetadataBlock = true;
654 } 705 }
655 WriteMDNode(N, VE, Stream, Record); 706 WriteMDNode(N, VE, Stream, Record);
656 } 707 }
657 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { 708 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
658 if (!StartedMetadataBlock) { 709 if (!StartedMetadataBlock) {
659 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 710 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID);
660
661 // Abbrev for METADATA_STRING.
662 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
663 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::METADATA_STRING));
664 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
665 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
666 MDSAbbrev = Stream.EmitAbbrev(Abbv);
667 StartedMetadataBlock = true; 711 StartedMetadataBlock = true;
668 } 712 }
669 713
670 // Code: [strchar x N] 714 // Code: [strchar x N]
671 Record.append(MDS->begin(), MDS->end()); 715 Record.append(MDS->begin(), MDS->end());
672 716
673 // Emit the finished record. 717 // Emit the finished record.
674 Stream.EmitRecord(naclbitc::METADATA_STRING, Record, MDSAbbrev); 718 Stream.EmitRecord(naclbitc::METADATA_STRING, Record,
719 METADATA_STRING_ABBREV);
675 Record.clear(); 720 Record.clear();
676 } 721 }
677 } 722 }
678 723
679 // Write named metadata. 724 // Write named metadata.
680 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 725 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
681 E = M->named_metadata_end(); I != E; ++I) { 726 E = M->named_metadata_end(); I != E; ++I) {
682 const NamedMDNode *NMD = I; 727 const NamedMDNode *NMD = I;
683 if (!StartedMetadataBlock) { 728 if (!StartedMetadataBlock) {
684 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 729 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID);
685 StartedMetadataBlock = true; 730 StartedMetadataBlock = true;
686 } 731 }
687 732
688 // Write name. 733 // Write name.
689 StringRef Str = NMD->getName(); 734 StringRef Str = NMD->getName();
690 for (unsigned i = 0, e = Str.size(); i != e; ++i) 735 for (unsigned i = 0, e = Str.size(); i != e; ++i)
691 Record.push_back(Str[i]); 736 Record.push_back(Str[i]);
692 Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/); 737 Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/);
693 Record.clear(); 738 Record.clear();
694 739
695 // Write named metadata operands. 740 // Write named metadata operands.
696 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 741 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
697 Record.push_back(VE.getValueID(NMD->getOperand(i))); 742 Record.push_back(VE.getValueID(NMD->getOperand(i)));
698 Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0); 743 Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0);
699 Record.clear(); 744 Record.clear();
700 } 745 }
701 746
702 if (StartedMetadataBlock) 747 if (StartedMetadataBlock)
703 Stream.ExitBlock(); 748 Stream.ExitBlock();
749
750 DEBUG(dbgs() << "<- WriteModuleMetadata\n");
704 } 751 }
705 752
706 static void WriteFunctionLocalMetadata(const Function &F, 753 static void WriteFunctionLocalMetadata(const Function &F,
707 const NaClValueEnumerator &VE, 754 const NaClValueEnumerator &VE,
708 NaClBitstreamWriter &Stream) { 755 NaClBitstreamWriter &Stream) {
756 DEBUG(dbgs() << "-> WriteFunctionLocalMetadata\n");
709 bool StartedMetadataBlock = false; 757 bool StartedMetadataBlock = false;
710 SmallVector<uint64_t, 64> Record; 758 SmallVector<uint64_t, 64> Record;
711 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); 759 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues();
712 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 760 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
713 if (const MDNode *N = Vals[i]) 761 if (const MDNode *N = Vals[i])
714 if (N->isFunctionLocal() && N->getFunction() == &F) { 762 if (N->isFunctionLocal() && N->getFunction() == &F) {
715 if (!StartedMetadataBlock) { 763 if (!StartedMetadataBlock) {
716 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 764 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID);
717 StartedMetadataBlock = true; 765 StartedMetadataBlock = true;
718 } 766 }
719 WriteMDNode(N, VE, Stream, Record); 767 WriteMDNode(N, VE, Stream, Record);
720 } 768 }
721 769
722 if (StartedMetadataBlock) 770 if (StartedMetadataBlock)
723 Stream.ExitBlock(); 771 Stream.ExitBlock();
772 DEBUG(dbgs() << "<- WriteFunctionLocalMetadata\n");
724 } 773 }
725 774
726 static void WriteMetadataAttachment(const Function &F, 775 static void WriteMetadataAttachment(const Function &F,
727 const NaClValueEnumerator &VE, 776 const NaClValueEnumerator &VE,
728 NaClBitstreamWriter &Stream) { 777 NaClBitstreamWriter &Stream) {
729 Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID, 3); 778 Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID);
730 779
731 SmallVector<uint64_t, 64> Record; 780 SmallVector<uint64_t, 64> Record;
732 781
733 // Write metadata attachments 782 // Write metadata attachments
734 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 783 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
735 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; 784 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
736 785
737 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 786 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
738 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 787 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
739 I != E; ++I) { 788 I != E; ++I) {
(...skipping 20 matching lines...) Expand all
760 NaClBitstreamWriter &Stream) { 809 NaClBitstreamWriter &Stream) {
761 SmallVector<uint64_t, 64> Record; 810 SmallVector<uint64_t, 64> Record;
762 811
763 // Write metadata kinds 812 // Write metadata kinds
764 // METADATA_KIND - [n x [id, name]] 813 // METADATA_KIND - [n x [id, name]]
765 SmallVector<StringRef, 8> Names; 814 SmallVector<StringRef, 8> Names;
766 M->getMDKindNames(Names); 815 M->getMDKindNames(Names);
767 816
768 if (Names.empty()) return; 817 if (Names.empty()) return;
769 818
770 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 819 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID);
771 820
772 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 821 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
773 Record.push_back(MDKindID); 822 Record.push_back(MDKindID);
774 StringRef KName = Names[MDKindID]; 823 StringRef KName = Names[MDKindID];
775 Record.append(KName.begin(), KName.end()); 824 Record.append(KName.begin(), KName.end());
776 825
777 Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0); 826 Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0);
778 Record.clear(); 827 Record.clear();
779 } 828 }
780 829
781 Stream.ExitBlock(); 830 Stream.ExitBlock();
782 } 831 }
783 832
784 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { 833 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
785 if ((int64_t)V >= 0) 834 Vals.push_back(NaClEncodeSignRotatedValue((int64_t)V));
786 Vals.push_back(V << 1);
787 else
788 Vals.push_back((-V << 1) | 1);
789 } 835 }
790 836
791 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals, 837 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals,
792 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val, 838 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val,
793 bool EmitSizeForWideNumbers = false 839 bool EmitSizeForWideNumbers = false
794 ) { 840 ) {
795 if (Val.getBitWidth() <= 64) { 841 if (Val.getBitWidth() <= 64) {
796 uint64_t V = Val.getSExtValue(); 842 uint64_t V = Val.getSExtValue();
797 emitSignedInt64(Vals, V); 843 emitSignedInt64(Vals, V);
798 Code = naclbitc::CST_CODE_INTEGER; 844 Code = naclbitc::CST_CODE_INTEGER;
(...skipping 15 matching lines...) Expand all
814 } 860 }
815 Code = naclbitc::CST_CODE_WIDE_INTEGER; 861 Code = naclbitc::CST_CODE_WIDE_INTEGER;
816 } 862 }
817 } 863 }
818 864
819 static void WriteConstants(unsigned FirstVal, unsigned LastVal, 865 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
820 const NaClValueEnumerator &VE, 866 const NaClValueEnumerator &VE,
821 NaClBitstreamWriter &Stream, bool isGlobal) { 867 NaClBitstreamWriter &Stream, bool isGlobal) {
822 if (FirstVal == LastVal) return; 868 if (FirstVal == LastVal) return;
823 869
824 Stream.EnterSubblock(naclbitc::CONSTANTS_BLOCK_ID, 4); 870 Stream.EnterSubblock(naclbitc::CONSTANTS_BLOCK_ID,
871 (isGlobal
872 ? CST_CONSTANTS_MAX_ABBREV
873 : CONSTANTS_MAX_ABBREV));
825 874
826 unsigned AggregateAbbrev = 0; 875 unsigned AggregateAbbrev = 0;
827 unsigned String8Abbrev = 0; 876 unsigned String8Abbrev = 0;
828 unsigned CString7Abbrev = 0; 877 unsigned CString7Abbrev = 0;
829 unsigned CString6Abbrev = 0; 878 unsigned CString6Abbrev = 0;
830 // If this is a constant pool for the module, emit module-specific abbrevs. 879 // If this is a constant pool for the module, emit module-specific abbrevs.
880 // Note: These abbreviations are size specific (to LastVal), and hence,
881 // can be more efficient if LastVal is known (rather then generating
882 // up-front for all constant sections).
831 if (isGlobal) { 883 if (isGlobal) {
832 // Abbrev for CST_CODE_AGGREGATE. 884 // Abbrev for CST_CODE_AGGREGATE.
833 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 885 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
834 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_AGGREGATE)); 886 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_AGGREGATE));
835 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 887 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
836 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 888 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed,
837 Log2_32_Ceil(LastVal+1))); 889 NaClBitsNeededForValue(LastVal)));
838 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 890 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
891 if (CST_CONSTANTS_AGGREGATE_ABBREV != AggregateAbbrev)
892 llvm_unreachable("Unexpected abbrev ordering!");
839 893
840 // Abbrev for CST_CODE_STRING. 894 // Abbrev for CST_CODE_STRING.
841 Abbv = new NaClBitCodeAbbrev(); 895 Abbv = new NaClBitCodeAbbrev();
842 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_STRING)); 896 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_STRING));
843 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 897 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
844 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); 898 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
845 String8Abbrev = Stream.EmitAbbrev(Abbv); 899 String8Abbrev = Stream.EmitAbbrev(Abbv);
900 if (CST_CONSTANTS_STRING_ABBREV != String8Abbrev)
901 llvm_unreachable("Unexpected abbrev ordering!");
902
846 // Abbrev for CST_CODE_CSTRING. 903 // Abbrev for CST_CODE_CSTRING.
847 Abbv = new NaClBitCodeAbbrev(); 904 Abbv = new NaClBitCodeAbbrev();
848 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); 905 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING));
849 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 906 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
850 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); 907 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7));
851 CString7Abbrev = Stream.EmitAbbrev(Abbv); 908 CString7Abbrev = Stream.EmitAbbrev(Abbv);
909 if (CST_CONSTANTS_CSTRING_7_ABBREV != CString7Abbrev)
910 llvm_unreachable("Unexpected abbrev ordering!");
911
852 // Abbrev for CST_CODE_CSTRING. 912 // Abbrev for CST_CODE_CSTRING.
853 Abbv = new NaClBitCodeAbbrev(); 913 Abbv = new NaClBitCodeAbbrev();
854 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); 914 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING));
855 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 915 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
856 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); 916 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6));
857 CString6Abbrev = Stream.EmitAbbrev(Abbv); 917 CString6Abbrev = Stream.EmitAbbrev(Abbv);
918 if (CST_CONSTANTS_CSTRING_6_ABBREV != CString6Abbrev)
919 llvm_unreachable("Unexpected abbrev ordering!");
920
921 DEBUG(dbgs() << "-- emitted abbreviations\n");
858 } 922 }
859 923
924
860 SmallVector<uint64_t, 64> Record; 925 SmallVector<uint64_t, 64> Record;
861 926
862 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); 927 const NaClValueEnumerator::ValueList &Vals = VE.getValues();
863 Type *LastTy = 0; 928 Type *LastTy = 0;
864 for (unsigned i = FirstVal; i != LastVal; ++i) { 929 for (unsigned i = FirstVal; i != LastVal; ++i) {
865 const Value *V = Vals[i].first; 930 const Value *V = Vals[i].first;
866 // If we need to switch types, do so now. 931 // If we need to switch types, do so now.
867 if (V->getType() != LastTy) { 932 if (V->getType() != LastTy) {
868 LastTy = V->getType(); 933 LastTy = V->getType();
869 Record.push_back(VE.getTypeID(LastTy)); 934 Record.push_back(VE.getTypeID(LastTy));
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 #ifndef NDEBUG 1120 #ifndef NDEBUG
1056 C->dump(); 1121 C->dump();
1057 #endif 1122 #endif
1058 llvm_unreachable("Unknown constant!"); 1123 llvm_unreachable("Unknown constant!");
1059 } 1124 }
1060 Stream.EmitRecord(Code, Record, AbbrevToUse); 1125 Stream.EmitRecord(Code, Record, AbbrevToUse);
1061 Record.clear(); 1126 Record.clear();
1062 } 1127 }
1063 1128
1064 Stream.ExitBlock(); 1129 Stream.ExitBlock();
1130 DEBUG(dbgs() << "<- WriteConstants\n");
1065 } 1131 }
1066 1132
1067 static void WriteModuleConstants(const NaClValueEnumerator &VE, 1133 static void WriteModuleConstants(const NaClValueEnumerator &VE,
1068 NaClBitstreamWriter &Stream) { 1134 NaClBitstreamWriter &Stream) {
1069 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); 1135 const NaClValueEnumerator::ValueList &Vals = VE.getValues();
1070 1136
1071 // Find the first constant to emit, which is the first non-globalvalue value. 1137 // Find the first constant to emit, which is the first non-globalvalue value.
1072 // We know globalvalues have been emitted by WriteModuleInfo. 1138 // We know globalvalues have been emitted by WriteModuleInfo.
1073 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 1139 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1074 if (!isa<GlobalValue>(Vals[i].first)) { 1140 if (!isa<GlobalValue>(Vals[i].first)) {
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
1479 1545
1480 Stream.EmitRecord(Code, Vals, AbbrevToUse); 1546 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1481 Vals.clear(); 1547 Vals.clear();
1482 } 1548 }
1483 1549
1484 // Emit names for globals/functions etc. 1550 // Emit names for globals/functions etc.
1485 static void WriteValueSymbolTable(const ValueSymbolTable &VST, 1551 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1486 const NaClValueEnumerator &VE, 1552 const NaClValueEnumerator &VE,
1487 NaClBitstreamWriter &Stream) { 1553 NaClBitstreamWriter &Stream) {
1488 if (VST.empty()) return; 1554 if (VST.empty()) return;
1489 Stream.EnterSubblock(naclbitc::VALUE_SYMTAB_BLOCK_ID, 4); 1555 Stream.EnterSubblock(naclbitc::VALUE_SYMTAB_BLOCK_ID);
1490 1556
1491 // FIXME: Set up the abbrev, we know how many values there are! 1557 // FIXME: Set up the abbrev, we know how many values there are!
1492 // FIXME: We know if the type names can use 7-bit ascii. 1558 // FIXME: We know if the type names can use 7-bit ascii.
1493 SmallVector<unsigned, 64> NameVals; 1559 SmallVector<unsigned, 64> NameVals;
1494 1560
1495 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 1561 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1496 SI != SE; ++SI) { 1562 SI != SE; ++SI) {
1497 1563
1498 const ValueName &Name = *SI; 1564 const ValueName &Name = *SI;
1499 1565
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1535 // Emit the finished record. 1601 // Emit the finished record.
1536 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 1602 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
1537 NameVals.clear(); 1603 NameVals.clear();
1538 } 1604 }
1539 Stream.ExitBlock(); 1605 Stream.ExitBlock();
1540 } 1606 }
1541 1607
1542 /// WriteFunction - Emit a function body to the module stream. 1608 /// WriteFunction - Emit a function body to the module stream.
1543 static void WriteFunction(const Function &F, NaClValueEnumerator &VE, 1609 static void WriteFunction(const Function &F, NaClValueEnumerator &VE,
1544 NaClBitstreamWriter &Stream) { 1610 NaClBitstreamWriter &Stream) {
1545 Stream.EnterSubblock(naclbitc::FUNCTION_BLOCK_ID, 4); 1611 Stream.EnterSubblock(naclbitc::FUNCTION_BLOCK_ID);
1546 VE.incorporateFunction(F); 1612 VE.incorporateFunction(F);
1547 1613
1548 SmallVector<unsigned, 64> Vals; 1614 SmallVector<unsigned, 64> Vals;
1549 1615
1550 // Emit the number of basic blocks, so the reader can create them ahead of 1616 // Emit the number of basic blocks, so the reader can create them ahead of
1551 // time. 1617 // time.
1552 Vals.push_back(VE.getBasicBlocks().size()); 1618 Vals.push_back(VE.getBasicBlocks().size());
1553 Stream.EmitRecord(naclbitc::FUNC_CODE_DECLAREBLOCKS, Vals); 1619 Stream.EmitRecord(naclbitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1554 Vals.clear(); 1620 Vals.clear();
1555 1621
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1608 if (NeedsMetadataAttachment) 1674 if (NeedsMetadataAttachment)
1609 WriteMetadataAttachment(F, VE, Stream); 1675 WriteMetadataAttachment(F, VE, Stream);
1610 VE.purgeFunction(); 1676 VE.purgeFunction();
1611 Stream.ExitBlock(); 1677 Stream.ExitBlock();
1612 } 1678 }
1613 1679
1614 // Emit blockinfo, which defines the standard abbreviations etc. 1680 // Emit blockinfo, which defines the standard abbreviations etc.
1615 static void WriteBlockInfo(const NaClValueEnumerator &VE, 1681 static void WriteBlockInfo(const NaClValueEnumerator &VE,
1616 NaClBitstreamWriter &Stream) { 1682 NaClBitstreamWriter &Stream) {
1617 // We only want to emit block info records for blocks that have multiple 1683 // We only want to emit block info records for blocks that have multiple
1618 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. 1684 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK,
1685 // and METADATA_BLOCK_ID.
1619 // Other blocks can define their abbrevs inline. 1686 // Other blocks can define their abbrevs inline.
1620 Stream.EnterBlockInfoBlock(2); 1687 Stream.EnterBlockInfoBlock();
1621 1688
1622 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 1689 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1623 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1690 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1624 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 3)); 1691 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 3));
1625 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); 1692 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8));
1626 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 1693 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
1627 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); 1694 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
1628 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID, 1695 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1629 Abbv) != VST_ENTRY_8_ABBREV) 1696 Abbv) != VST_ENTRY_8_ABBREV)
1630 llvm_unreachable("Unexpected abbrev ordering!"); 1697 llvm_unreachable("Unexpected abbrev ordering!");
(...skipping 28 matching lines...) Expand all
1659 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID, 1726 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1660 Abbv) != VST_BBENTRY_6_ABBREV) 1727 Abbv) != VST_BBENTRY_6_ABBREV)
1661 llvm_unreachable("Unexpected abbrev ordering!"); 1728 llvm_unreachable("Unexpected abbrev ordering!");
1662 } 1729 }
1663 1730
1664 1731
1665 1732
1666 { // SETTYPE abbrev for CONSTANTS_BLOCK. 1733 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1667 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1734 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1668 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_SETTYPE)); 1735 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_SETTYPE));
1669 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1736 Abbv->Add(NaClBitCodeAbbrevOp(
1670 Log2_32_Ceil(VE.getTypes().size()+1))); 1737 NaClBitCodeAbbrevOp::Fixed,
1738 NaClBitsNeededForValue(VE.getTypes().size())));
1671 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1739 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1672 Abbv) != CONSTANTS_SETTYPE_ABBREV) 1740 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1673 llvm_unreachable("Unexpected abbrev ordering!"); 1741 llvm_unreachable("Unexpected abbrev ordering!");
1674 } 1742 }
1675 1743
1676 { // INTEGER abbrev for CONSTANTS_BLOCK. 1744 { // INTEGER abbrev for CONSTANTS_BLOCK.
1677 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1745 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1678 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_INTEGER)); 1746 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_INTEGER));
1679 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); 1747 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8));
1680 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1748 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1681 Abbv) != CONSTANTS_INTEGER_ABBREV) 1749 Abbv) != CONSTANTS_INTEGER_ABBREV)
1682 llvm_unreachable("Unexpected abbrev ordering!"); 1750 llvm_unreachable("Unexpected abbrev ordering!");
1683 } 1751 }
1684 1752
1685 { // CE_CAST abbrev for CONSTANTS_BLOCK. 1753 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1686 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1754 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1687 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CE_CAST)); 1755 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CE_CAST));
1688 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // cast opc 1756 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // cast opc
1689 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // typeid 1757 Abbv->Add(NaClBitCodeAbbrevOp(
1690 Log2_32_Ceil(VE.getTypes().size()+1))); 1758 NaClBitCodeAbbrevOp::Fixed, // typeid
1759 NaClBitsNeededForValue(VE.getTypes().size())));
1691 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // value id 1760 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // value id
1692 1761
1693 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1762 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1694 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1763 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1695 llvm_unreachable("Unexpected abbrev ordering!"); 1764 llvm_unreachable("Unexpected abbrev ordering!");
1696 } 1765 }
1697 { // NULL abbrev for CONSTANTS_BLOCK. 1766 { // NULL abbrev for CONSTANTS_BLOCK.
1698 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1767 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1699 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_NULL)); 1768 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_NULL));
1700 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1769 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1732 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc 1801 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc
1733 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); // flags 1802 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); // flags
1734 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1803 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1735 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 1804 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1736 llvm_unreachable("Unexpected abbrev ordering!"); 1805 llvm_unreachable("Unexpected abbrev ordering!");
1737 } 1806 }
1738 { // INST_CAST abbrev for FUNCTION_BLOCK. 1807 { // INST_CAST abbrev for FUNCTION_BLOCK.
1739 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1808 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1740 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_CAST)); 1809 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_CAST));
1741 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // OpVal 1810 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // OpVal
1742 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // dest ty 1811 Abbv->Add(NaClBitCodeAbbrevOp(
1743 Log2_32_Ceil(VE.getTypes().size()+1))); 1812 NaClBitCodeAbbrevOp::Fixed, // dest ty
1813 NaClBitsNeededForValue(VE.getTypes().size())));
1744 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc 1814 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc
1745 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1815 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1746 Abbv) != FUNCTION_INST_CAST_ABBREV) 1816 Abbv) != FUNCTION_INST_CAST_ABBREV)
1747 llvm_unreachable("Unexpected abbrev ordering!"); 1817 llvm_unreachable("Unexpected abbrev ordering!");
1748 } 1818 }
1749 1819
1750 { // INST_RET abbrev for FUNCTION_BLOCK. 1820 { // INST_RET abbrev for FUNCTION_BLOCK.
1751 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1821 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1752 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); 1822 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET));
1753 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1823 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1754 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 1824 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1755 llvm_unreachable("Unexpected abbrev ordering!"); 1825 llvm_unreachable("Unexpected abbrev ordering!");
1756 } 1826 }
1757 { // INST_RET abbrev for FUNCTION_BLOCK. 1827 { // INST_RET abbrev for FUNCTION_BLOCK.
1758 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1828 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1759 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); 1829 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET));
1760 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // ValID 1830 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // ValID
1761 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1831 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1762 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 1832 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1763 llvm_unreachable("Unexpected abbrev ordering!"); 1833 llvm_unreachable("Unexpected abbrev ordering!");
1764 } 1834 }
1765 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 1835 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1766 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1836 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1767 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_UNREACHABLE)); 1837 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_UNREACHABLE));
1768 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1838 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1769 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 1839 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1770 llvm_unreachable("Unexpected abbrev ordering!"); 1840 llvm_unreachable("Unexpected abbrev ordering!");
1771 } 1841 }
1772 1842
1843 { // Abbrev for METADATA_STRING.
1844 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1845 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::METADATA_STRING));
1846 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
1847 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
1848 if (Stream.EmitBlockInfoAbbrev(naclbitc::METADATA_BLOCK_ID,
1849 Abbv) != METADATA_STRING_ABBREV)
1850 llvm_unreachable("Unexpected abbrev ordering!");
1851 }
1852
1773 Stream.ExitBlock(); 1853 Stream.ExitBlock();
1774 } 1854 }
1775 1855
1776 /// WriteModule - Emit the specified module to the bitstream. 1856 /// WriteModule - Emit the specified module to the bitstream.
1777 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) { 1857 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) {
1778 Stream.EnterSubblock(naclbitc::MODULE_BLOCK_ID, 3); 1858 DEBUG(dbgs() << "-> WriteModule\n");
1859 Stream.EnterSubblock(naclbitc::MODULE_BLOCK_ID, MODULE_MAX_ABBREV);
1779 1860
1780 SmallVector<unsigned, 1> Vals; 1861 SmallVector<unsigned, 1> Vals;
1781 unsigned CurVersion = 1; 1862 unsigned CurVersion = 1;
1782 Vals.push_back(CurVersion); 1863 Vals.push_back(CurVersion);
1783 Stream.EmitRecord(naclbitc::MODULE_CODE_VERSION, Vals); 1864 Stream.EmitRecord(naclbitc::MODULE_CODE_VERSION, Vals);
1784 1865
1785 // Analyze the module, enumerating globals, functions, etc. 1866 // Analyze the module, enumerating globals, functions, etc.
1786 NaClValueEnumerator VE(M); 1867 NaClValueEnumerator VE(M);
1787 1868
1788 // Emit blockinfo, which defines the standard abbreviations etc. 1869 // Emit blockinfo, which defines the standard abbreviations etc.
(...skipping 23 matching lines...) Expand all
1812 1893
1813 // Emit names for globals/functions etc. 1894 // Emit names for globals/functions etc.
1814 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1895 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1815 1896
1816 // Emit function bodies. 1897 // Emit function bodies.
1817 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) 1898 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
1818 if (!F->isDeclaration()) 1899 if (!F->isDeclaration())
1819 WriteFunction(*F, VE, Stream); 1900 WriteFunction(*F, VE, Stream);
1820 1901
1821 Stream.ExitBlock(); 1902 Stream.ExitBlock();
1903 DEBUG(dbgs() << "<- WriteModule\n");
1822 } 1904 }
1823 1905
1824 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 1906 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1825 /// header and trailer to make it compatible with the system archiver. To do 1907 /// header and trailer to make it compatible with the system archiver. To do
1826 /// this we emit the following header, and then emit a trailer that pads the 1908 /// this we emit the following header, and then emit a trailer that pads the
1827 /// file out to be a multiple of 16 bytes. 1909 /// file out to be a multiple of 16 bytes.
1828 /// 1910 ///
1829 /// struct bc_header { 1911 /// struct bc_header {
1830 /// uint32_t Magic; // 0x0B17C0DE 1912 /// uint32_t Magic; // 0x0B17C0DE
1831 /// uint32_t Version; // Version, currently always 0. 1913 /// uint32_t Version; // Version, currently always 0.
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1924 // Emit the module. 2006 // Emit the module.
1925 WriteModule(M, Stream); 2007 WriteModule(M, Stream);
1926 } 2008 }
1927 2009
1928 if (TT.isOSDarwin()) 2010 if (TT.isOSDarwin())
1929 EmitDarwinBCHeaderAndTrailer(Buffer, TT); 2011 EmitDarwinBCHeaderAndTrailer(Buffer, TT);
1930 2012
1931 // Write the generated bitstream to "Out". 2013 // Write the generated bitstream to "Out".
1932 Out.write((char*)&Buffer.front(), Buffer.size()); 2014 Out.write((char*)&Buffer.front(), Buffer.size());
1933 } 2015 }
OLDNEW
« no previous file with comments | « lib/Bitcode/NaCl/Reader/NaClBitstreamReader.cpp ('k') | tools/pnacl-bcanalyzer/pnacl-bcanalyzer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698