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

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: Fix code based on Jan's suggestions. 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,
jvoung (off chromium) 2013/05/21 17:24:17 see below, is this still part of the META_DATA_BLO
Karl 2013/05/21 23:06:52 Yes. Its added to the (global) BlockInfo vector vi
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
236 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); 281 uint64_t NumBits = NaClBitsNeededForValue(VE.getTypes().size());
237 282
238 // Abbrev for TYPE_CODE_POINTER. 283 // Abbrev for TYPE_CODE_POINTER.
239 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 284 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
240 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER)); 285 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER));
241 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); 286 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
242 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0 287 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0
243 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 288 if (TYPE_POINTER_ABBREV != Stream.EmitAbbrev(Abbv))
289 llvm_unreachable("Unexpected abbrev ordering!");
244 290
245 // Abbrev for TYPE_CODE_FUNCTION. 291 // Abbrev for TYPE_CODE_FUNCTION.
246 Abbv = new NaClBitCodeAbbrev(); 292 Abbv = new NaClBitCodeAbbrev();
247 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION)); 293 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION));
248 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg 294 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg
249 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 295 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
250 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); 296 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
251 297 if (TYPE_FUNCTION_ABBREV != Stream.EmitAbbrev(Abbv))
252 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 298 llvm_unreachable("Unexpected abbrev ordering!");
253 299
254 // Abbrev for TYPE_CODE_STRUCT_ANON. 300 // Abbrev for TYPE_CODE_STRUCT_ANON.
255 Abbv = new NaClBitCodeAbbrev(); 301 Abbv = new NaClBitCodeAbbrev();
256 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON)); 302 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON));
257 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked 303 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked
258 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 304 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
259 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); 305 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
260 306 if (TYPE_STRUCT_ANON_ABBREV != Stream.EmitAbbrev(Abbv))
261 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); 307 llvm_unreachable("Unexpected abbrev ordering!");
262 308
263 // Abbrev for TYPE_CODE_STRUCT_NAME. 309 // Abbrev for TYPE_CODE_STRUCT_NAME.
264 Abbv = new NaClBitCodeAbbrev(); 310 Abbv = new NaClBitCodeAbbrev();
265 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME)); 311 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME));
266 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 312 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
267 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); 313 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6));
268 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); 314 if (TYPE_STRUCT_NAME_ABBREV != Stream.EmitAbbrev(Abbv))
315 llvm_unreachable("Unexpected abbrev ordering!");
269 316
270 // Abbrev for TYPE_CODE_STRUCT_NAMED. 317 // Abbrev for TYPE_CODE_STRUCT_NAMED.
271 Abbv = new NaClBitCodeAbbrev(); 318 Abbv = new NaClBitCodeAbbrev();
272 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED)); 319 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED));
273 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked 320 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked
274 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 321 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
275 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); 322 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
276 323 if (TYPE_STRUCT_NAMED_ABBREV != Stream.EmitAbbrev(Abbv))
277 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); 324 llvm_unreachable("Unexpected abbrev ordering!");
278 325
279 // Abbrev for TYPE_CODE_ARRAY. 326 // Abbrev for TYPE_CODE_ARRAY.
280 Abbv = new NaClBitCodeAbbrev(); 327 Abbv = new NaClBitCodeAbbrev();
281 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY)); 328 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY));
282 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size 329 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size
283 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); 330 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits));
284 331 if (TYPE_ARRAY_ABBREV != Stream.EmitAbbrev(Abbv))
285 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 332 llvm_unreachable("Unexpected abbrev ordering!");
286 333
287 // Emit an entry count so the reader can reserve space. 334 // Emit an entry count so the reader can reserve space.
288 TypeVals.push_back(TypeList.size()); 335 TypeVals.push_back(TypeList.size());
289 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals); 336 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals);
290 TypeVals.clear(); 337 TypeVals.clear();
291 338
292 // Loop over all of the types, emitting each in turn. 339 // Loop over all of the types, emitting each in turn.
293 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 340 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
294 Type *T = TypeList[i]; 341 Type *T = TypeList[i];
295 int AbbrevToUse = 0; 342 int AbbrevToUse = 0;
(...skipping 16 matching lines...) Expand all
312 Code = naclbitc::TYPE_CODE_INTEGER; 359 Code = naclbitc::TYPE_CODE_INTEGER;
313 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 360 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
314 break; 361 break;
315 case Type::PointerTyID: { 362 case Type::PointerTyID: {
316 PointerType *PTy = cast<PointerType>(T); 363 PointerType *PTy = cast<PointerType>(T);
317 // POINTER: [pointee type, address space] 364 // POINTER: [pointee type, address space]
318 Code = naclbitc::TYPE_CODE_POINTER; 365 Code = naclbitc::TYPE_CODE_POINTER;
319 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 366 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
320 unsigned AddressSpace = PTy->getAddressSpace(); 367 unsigned AddressSpace = PTy->getAddressSpace();
321 TypeVals.push_back(AddressSpace); 368 TypeVals.push_back(AddressSpace);
322 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 369 if (AddressSpace == 0) AbbrevToUse = TYPE_POINTER_ABBREV;
323 break; 370 break;
324 } 371 }
325 case Type::FunctionTyID: { 372 case Type::FunctionTyID: {
326 FunctionType *FT = cast<FunctionType>(T); 373 FunctionType *FT = cast<FunctionType>(T);
327 // FUNCTION: [isvararg, retty, paramty x N] 374 // FUNCTION: [isvararg, retty, paramty x N]
328 Code = naclbitc::TYPE_CODE_FUNCTION; 375 Code = naclbitc::TYPE_CODE_FUNCTION;
329 TypeVals.push_back(FT->isVarArg()); 376 TypeVals.push_back(FT->isVarArg());
330 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 377 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
331 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 378 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
332 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 379 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
333 AbbrevToUse = FunctionAbbrev; 380 AbbrevToUse = TYPE_FUNCTION_ABBREV;
334 break; 381 break;
335 } 382 }
336 case Type::StructTyID: { 383 case Type::StructTyID: {
337 StructType *ST = cast<StructType>(T); 384 StructType *ST = cast<StructType>(T);
338 // STRUCT: [ispacked, eltty x N] 385 // STRUCT: [ispacked, eltty x N]
339 TypeVals.push_back(ST->isPacked()); 386 TypeVals.push_back(ST->isPacked());
340 // Output all of the element types. 387 // Output all of the element types.
341 for (StructType::element_iterator I = ST->element_begin(), 388 for (StructType::element_iterator I = ST->element_begin(),
342 E = ST->element_end(); I != E; ++I) 389 E = ST->element_end(); I != E; ++I)
343 TypeVals.push_back(VE.getTypeID(*I)); 390 TypeVals.push_back(VE.getTypeID(*I));
344 391
345 if (ST->isLiteral()) { 392 if (ST->isLiteral()) {
346 Code = naclbitc::TYPE_CODE_STRUCT_ANON; 393 Code = naclbitc::TYPE_CODE_STRUCT_ANON;
347 AbbrevToUse = StructAnonAbbrev; 394 AbbrevToUse = TYPE_STRUCT_ANON_ABBREV;
348 } else { 395 } else {
349 if (ST->isOpaque()) { 396 if (ST->isOpaque()) {
350 Code = naclbitc::TYPE_CODE_OPAQUE; 397 Code = naclbitc::TYPE_CODE_OPAQUE;
351 } else { 398 } else {
352 Code = naclbitc::TYPE_CODE_STRUCT_NAMED; 399 Code = naclbitc::TYPE_CODE_STRUCT_NAMED;
353 AbbrevToUse = StructNamedAbbrev; 400 AbbrevToUse = TYPE_STRUCT_NAMED_ABBREV;
354 } 401 }
355 402
356 // Emit the name if it is present. 403 // Emit the name if it is present.
357 if (!ST->getName().empty()) 404 if (!ST->getName().empty())
358 WriteStringRecord(naclbitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 405 WriteStringRecord(naclbitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
359 StructNameAbbrev, Stream); 406 TYPE_STRUCT_NAME_ABBREV, Stream);
360 } 407 }
361 break; 408 break;
362 } 409 }
363 case Type::ArrayTyID: { 410 case Type::ArrayTyID: {
364 ArrayType *AT = cast<ArrayType>(T); 411 ArrayType *AT = cast<ArrayType>(T);
365 // ARRAY: [numelts, eltty] 412 // ARRAY: [numelts, eltty]
366 Code = naclbitc::TYPE_CODE_ARRAY; 413 Code = naclbitc::TYPE_CODE_ARRAY;
367 TypeVals.push_back(AT->getNumElements()); 414 TypeVals.push_back(AT->getNumElements());
368 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 415 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
369 AbbrevToUse = ArrayAbbrev; 416 AbbrevToUse = TYPE_ARRAY_ABBREV;
370 break; 417 break;
371 } 418 }
372 case Type::VectorTyID: { 419 case Type::VectorTyID: {
373 VectorType *VT = cast<VectorType>(T); 420 VectorType *VT = cast<VectorType>(T);
374 // VECTOR [numelts, eltty] 421 // VECTOR [numelts, eltty]
375 Code = naclbitc::TYPE_CODE_VECTOR; 422 Code = naclbitc::TYPE_CODE_VECTOR;
376 TypeVals.push_back(VT->getNumElements()); 423 TypeVals.push_back(VT->getNumElements());
377 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 424 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
378 break; 425 break;
379 } 426 }
380 } 427 }
381 428
382 // Emit the finished record. 429 // Emit the finished record.
383 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 430 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
384 TypeVals.clear(); 431 TypeVals.clear();
385 } 432 }
386 433
387 Stream.ExitBlock(); 434 Stream.ExitBlock();
435 DEBUG(dbgs() << "<- WriteTypeTable\n");
388 } 436 }
389 437
390 static unsigned getEncodedLinkage(const GlobalValue *GV) { 438 static unsigned getEncodedLinkage(const GlobalValue *GV) {
391 switch (GV->getLinkage()) { 439 switch (GV->getLinkage()) {
392 case GlobalValue::ExternalLinkage: return 0; 440 case GlobalValue::ExternalLinkage: return 0;
393 case GlobalValue::WeakAnyLinkage: return 1; 441 case GlobalValue::WeakAnyLinkage: return 1;
394 case GlobalValue::AppendingLinkage: return 2; 442 case GlobalValue::AppendingLinkage: return 2;
395 case GlobalValue::InternalLinkage: return 3; 443 case GlobalValue::InternalLinkage: return 3;
396 case GlobalValue::LinkOnceAnyLinkage: return 4; 444 case GlobalValue::LinkOnceAnyLinkage: return 4;
397 case GlobalValue::DLLImportLinkage: return 5; 445 case GlobalValue::DLLImportLinkage: return 5;
(...skipping 28 matching lines...) Expand all
426 case GlobalVariable::InitialExecTLSModel: return 3; 474 case GlobalVariable::InitialExecTLSModel: return 3;
427 case GlobalVariable::LocalExecTLSModel: return 4; 475 case GlobalVariable::LocalExecTLSModel: return 4;
428 } 476 }
429 llvm_unreachable("Invalid TLS model"); 477 llvm_unreachable("Invalid TLS model");
430 } 478 }
431 479
432 // Emit top-level description of module, including target triple, inline asm, 480 // Emit top-level description of module, including target triple, inline asm,
433 // descriptors for global variables, and function prototype info. 481 // descriptors for global variables, and function prototype info.
434 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, 482 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE,
435 NaClBitstreamWriter &Stream) { 483 NaClBitstreamWriter &Stream) {
484 DEBUG(dbgs() << "-> WriteModuleInfo\n");
436 // Emit various pieces of data attached to a module. 485 // Emit various pieces of data attached to a module.
437 if (!M->getTargetTriple().empty()) 486 if (!M->getTargetTriple().empty())
438 WriteStringRecord(naclbitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 487 WriteStringRecord(naclbitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
439 0/*TODO*/, Stream); 488 0/*TODO*/, Stream);
440 if (!M->getDataLayout().empty()) 489 if (!M->getDataLayout().empty())
441 WriteStringRecord(naclbitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), 490 WriteStringRecord(naclbitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
442 0/*TODO*/, Stream); 491 0/*TODO*/, Stream);
443 if (!M->getModuleInlineAsm().empty()) 492 if (!M->getModuleInlineAsm().empty())
444 WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 493 WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
445 0/*TODO*/, Stream); 494 0/*TODO*/, Stream);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 unsigned &Entry = GCMap[F->getGC()]; 529 unsigned &Entry = GCMap[F->getGC()];
481 if (!Entry) { 530 if (!Entry) {
482 WriteStringRecord(naclbitc::MODULE_CODE_GCNAME, F->getGC(), 531 WriteStringRecord(naclbitc::MODULE_CODE_GCNAME, F->getGC(),
483 0/*TODO*/, Stream); 532 0/*TODO*/, Stream);
484 Entry = GCMap.size(); 533 Entry = GCMap.size();
485 } 534 }
486 } 535 }
487 } 536 }
488 537
489 // Emit abbrev for globals, now that we know # sections and max alignment. 538 // Emit abbrev for globals, now that we know # sections and max alignment.
490 unsigned SimpleGVarAbbrev = 0; 539 // Add an abbrev for common globals with no visibility or thread localness.
491 if (!M->global_empty()) { 540 {
492 // Add an abbrev for common globals with no visibility or thread localness.
493 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 541 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
494 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::MODULE_CODE_GLOBALVAR)); 542 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::MODULE_CODE_GLOBALVAR));
495 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 543 Abbv->Add(NaClBitCodeAbbrevOp(
496 Log2_32_Ceil(MaxGlobalType+1))); 544 NaClBitCodeAbbrevOp::Fixed, NaClBitsNeededForValue(MaxGlobalType)));
497 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // Constant. 545 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // Constant.
498 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // Initialize r. 546 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // Initialize r.
499 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // Linkage. 547 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // Linkage.
500 if (MaxAlignment == 0) // Alignment. 548 if (MaxAlignment == 0) // Alignment.
501 Abbv->Add(NaClBitCodeAbbrevOp(0)); 549 Abbv->Add(NaClBitCodeAbbrevOp(0));
502 else { 550 else {
503 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 551 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
504 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 552 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed,
505 Log2_32_Ceil(MaxEncAlignment+1))); 553 NaClBitsNeededForValue(MaxEncAlignment)));
506 } 554 }
507 if (SectionMap.empty()) // Section. 555 if (SectionMap.empty()) // Section.
508 Abbv->Add(NaClBitCodeAbbrevOp(0)); 556 Abbv->Add(NaClBitCodeAbbrevOp(0));
509 else 557 else
510 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 558 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed,
511 Log2_32_Ceil(SectionMap.size()+1))); 559 NaClBitsNeededForValue(SectionMap.size())));
512 // Don't bother emitting vis + thread local. 560 if (MODULE_GLOBALVAR_ABBREV != Stream.EmitAbbrev(Abbv))
513 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 561 llvm_unreachable("Unexpected abbrev ordering!");
514 } 562 }
515 563
564 // Don't bother emitting vis + thread local.
jvoung (off chromium) 2013/05/21 17:24:17 Can this comment be moved back into the globalvar
Karl 2013/05/21 23:06:52 Done.
565
516 // Emit the global variable information. 566 // Emit the global variable information.
517 SmallVector<unsigned, 64> Vals; 567 SmallVector<unsigned, 64> Vals;
518 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 568 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
519 GV != E; ++GV) { 569 GV != E; ++GV) {
520 unsigned AbbrevToUse = 0; 570 unsigned AbbrevToUse = 0;
521 571
522 // GLOBALVAR: [type, isconst, initid, 572 // GLOBALVAR: [type, isconst, initid,
523 // linkage, alignment, section, visibility, threadlocal, 573 // linkage, alignment, section, visibility, threadlocal,
524 // unnamed_addr] 574 // unnamed_addr]
525 Vals.push_back(VE.getTypeID(GV->getType())); 575 Vals.push_back(VE.getTypeID(GV->getType()));
526 Vals.push_back(GV->isConstant()); 576 Vals.push_back(GV->isConstant());
527 Vals.push_back(GV->isDeclaration() ? 0 : 577 Vals.push_back(GV->isDeclaration() ? 0 :
528 (VE.getValueID(GV->getInitializer()) + 1)); 578 (VE.getValueID(GV->getInitializer()) + 1));
529 Vals.push_back(getEncodedLinkage(GV)); 579 Vals.push_back(getEncodedLinkage(GV));
530 Vals.push_back(Log2_32(GV->getAlignment())+1); 580 Vals.push_back(Log2_32(GV->getAlignment())+1);
531 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); 581 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
532 if (GV->isThreadLocal() || 582 if (GV->isThreadLocal() ||
533 GV->getVisibility() != GlobalValue::DefaultVisibility || 583 GV->getVisibility() != GlobalValue::DefaultVisibility ||
534 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) { 584 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) {
535 Vals.push_back(getEncodedVisibility(GV)); 585 Vals.push_back(getEncodedVisibility(GV));
536 Vals.push_back(getEncodedThreadLocalMode(GV)); 586 Vals.push_back(getEncodedThreadLocalMode(GV));
537 Vals.push_back(GV->hasUnnamedAddr()); 587 Vals.push_back(GV->hasUnnamedAddr());
538 Vals.push_back(GV->isExternallyInitialized()); 588 Vals.push_back(GV->isExternallyInitialized());
539 } else { 589 } else {
540 AbbrevToUse = SimpleGVarAbbrev; 590 AbbrevToUse = MODULE_GLOBALVAR_ABBREV;
541 } 591 }
542 592
543 Stream.EmitRecord(naclbitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 593 Stream.EmitRecord(naclbitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
544 Vals.clear(); 594 Vals.clear();
545 } 595 }
546 596
547 // Emit the function proto information. 597 // Emit the function proto information.
548 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 598 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
549 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, 599 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
550 // section, visibility, gc, unnamed_addr] 600 // section, visibility, gc, unnamed_addr]
(...skipping 18 matching lines...) Expand all
569 AI != E; ++AI) { 619 AI != E; ++AI) {
570 // ALIAS: [alias type, aliasee val#, linkage, visibility] 620 // ALIAS: [alias type, aliasee val#, linkage, visibility]
571 Vals.push_back(VE.getTypeID(AI->getType())); 621 Vals.push_back(VE.getTypeID(AI->getType()));
572 Vals.push_back(VE.getValueID(AI->getAliasee())); 622 Vals.push_back(VE.getValueID(AI->getAliasee()));
573 Vals.push_back(getEncodedLinkage(AI)); 623 Vals.push_back(getEncodedLinkage(AI));
574 Vals.push_back(getEncodedVisibility(AI)); 624 Vals.push_back(getEncodedVisibility(AI));
575 unsigned AbbrevToUse = 0; 625 unsigned AbbrevToUse = 0;
576 Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 626 Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
577 Vals.clear(); 627 Vals.clear();
578 } 628 }
629 DEBUG(dbgs() << "<- WriteModuleInfo\n");
579 } 630 }
580 631
581 static uint64_t GetOptimizationFlags(const Value *V) { 632 static uint64_t GetOptimizationFlags(const Value *V) {
582 uint64_t Flags = 0; 633 uint64_t Flags = 0;
583 634
584 if (const OverflowingBinaryOperator *OBO = 635 if (const OverflowingBinaryOperator *OBO =
585 dyn_cast<OverflowingBinaryOperator>(V)) { 636 dyn_cast<OverflowingBinaryOperator>(V)) {
586 if (OBO->hasNoSignedWrap()) 637 if (OBO->hasNoSignedWrap())
587 Flags |= 1 << naclbitc::OBO_NO_SIGNED_WRAP; 638 Flags |= 1 << naclbitc::OBO_NO_SIGNED_WRAP;
588 if (OBO->hasNoUnsignedWrap()) 639 if (OBO->hasNoUnsignedWrap())
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 } 674 }
624 unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE : 675 unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE :
625 naclbitc::METADATA_NODE; 676 naclbitc::METADATA_NODE;
626 Stream.EmitRecord(MDCode, Record, 0); 677 Stream.EmitRecord(MDCode, Record, 0);
627 Record.clear(); 678 Record.clear();
628 } 679 }
629 680
630 static void WriteModuleMetadata(const Module *M, 681 static void WriteModuleMetadata(const Module *M,
631 const NaClValueEnumerator &VE, 682 const NaClValueEnumerator &VE,
632 NaClBitstreamWriter &Stream) { 683 NaClBitstreamWriter &Stream) {
684 DEBUG(dbgs() << "-> WriteModuleMetadata\n");
633 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues(); 685 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues();
634 bool StartedMetadataBlock = false; 686 bool StartedMetadataBlock = false;
635 unsigned MDSAbbrev = 0;
636 SmallVector<uint64_t, 64> Record; 687 SmallVector<uint64_t, 64> Record;
637 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 688 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
638 689
639 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { 690 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
640 if (!N->isFunctionLocal() || !N->getFunction()) { 691 if (!N->isFunctionLocal() || !N->getFunction()) {
641 if (!StartedMetadataBlock) { 692 if (!StartedMetadataBlock) {
642 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 693 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID,
694 METADATA_MAX_ABBREV);
jvoung (off chromium) 2013/05/21 17:24:17 If I'm understanding this correctly, ... if the ab
Karl 2013/05/21 23:06:52 I'm not sure what you are asking. The call to Ente
jvoung (off chromium) 2013/05/21 23:56:45 I just wasn't quite understanding how BlockInfos w
643 StartedMetadataBlock = true; 695 StartedMetadataBlock = true;
644 } 696 }
645 WriteMDNode(N, VE, Stream, Record); 697 WriteMDNode(N, VE, Stream, Record);
646 } 698 }
647 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { 699 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
648 if (!StartedMetadataBlock) { 700 if (!StartedMetadataBlock) {
649 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 701 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, METADATA_MAX_ABBREV);
650
651 // Abbrev for METADATA_STRING.
652 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
653 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::METADATA_STRING));
654 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
655 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
656 MDSAbbrev = Stream.EmitAbbrev(Abbv);
657 StartedMetadataBlock = true; 702 StartedMetadataBlock = true;
658 } 703 }
659 704
660 // Code: [strchar x N] 705 // Code: [strchar x N]
661 Record.append(MDS->begin(), MDS->end()); 706 Record.append(MDS->begin(), MDS->end());
662 707
663 // Emit the finished record. 708 // Emit the finished record.
664 Stream.EmitRecord(naclbitc::METADATA_STRING, Record, MDSAbbrev); 709 Stream.EmitRecord(naclbitc::METADATA_STRING, Record,
710 METADATA_STRING_ABBREV);
665 Record.clear(); 711 Record.clear();
666 } 712 }
667 } 713 }
668 714
669 // Write named metadata. 715 // Write named metadata.
670 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 716 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
671 E = M->named_metadata_end(); I != E; ++I) { 717 E = M->named_metadata_end(); I != E; ++I) {
672 const NamedMDNode *NMD = I; 718 const NamedMDNode *NMD = I;
673 if (!StartedMetadataBlock) { 719 if (!StartedMetadataBlock) {
674 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 720 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, METADATA_MAX_ABBREV);
675 StartedMetadataBlock = true; 721 StartedMetadataBlock = true;
676 } 722 }
677 723
678 // Write name. 724 // Write name.
679 StringRef Str = NMD->getName(); 725 StringRef Str = NMD->getName();
680 for (unsigned i = 0, e = Str.size(); i != e; ++i) 726 for (unsigned i = 0, e = Str.size(); i != e; ++i)
681 Record.push_back(Str[i]); 727 Record.push_back(Str[i]);
682 Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/); 728 Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/);
683 Record.clear(); 729 Record.clear();
684 730
685 // Write named metadata operands. 731 // Write named metadata operands.
686 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 732 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
687 Record.push_back(VE.getValueID(NMD->getOperand(i))); 733 Record.push_back(VE.getValueID(NMD->getOperand(i)));
688 Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0); 734 Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0);
689 Record.clear(); 735 Record.clear();
690 } 736 }
691 737
692 if (StartedMetadataBlock) 738 if (StartedMetadataBlock)
693 Stream.ExitBlock(); 739 Stream.ExitBlock();
740
741 DEBUG(dbgs() << "<- WriteModuleMetadata\n");
694 } 742 }
695 743
696 static void WriteFunctionLocalMetadata(const Function &F, 744 static void WriteFunctionLocalMetadata(const Function &F,
697 const NaClValueEnumerator &VE, 745 const NaClValueEnumerator &VE,
698 NaClBitstreamWriter &Stream) { 746 NaClBitstreamWriter &Stream) {
747 DEBUG(dbgs() << "-> WriteFunctionLocalMetadata\n");
699 bool StartedMetadataBlock = false; 748 bool StartedMetadataBlock = false;
700 SmallVector<uint64_t, 64> Record; 749 SmallVector<uint64_t, 64> Record;
701 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); 750 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues();
702 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 751 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
703 if (const MDNode *N = Vals[i]) 752 if (const MDNode *N = Vals[i])
704 if (N->isFunctionLocal() && N->getFunction() == &F) { 753 if (N->isFunctionLocal() && N->getFunction() == &F) {
705 if (!StartedMetadataBlock) { 754 if (!StartedMetadataBlock) {
706 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 755 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID,
756 METADATA_MAX_ABBREV);
707 StartedMetadataBlock = true; 757 StartedMetadataBlock = true;
708 } 758 }
709 WriteMDNode(N, VE, Stream, Record); 759 WriteMDNode(N, VE, Stream, Record);
710 } 760 }
711 761
712 if (StartedMetadataBlock) 762 if (StartedMetadataBlock)
713 Stream.ExitBlock(); 763 Stream.ExitBlock();
764 DEBUG(dbgs() << "<- WriteFunctionLocalMetadata\n");
714 } 765 }
715 766
716 static void WriteMetadataAttachment(const Function &F, 767 static void WriteMetadataAttachment(const Function &F,
717 const NaClValueEnumerator &VE, 768 const NaClValueEnumerator &VE,
718 NaClBitstreamWriter &Stream) { 769 NaClBitstreamWriter &Stream) {
719 Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID, 3); 770 Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID);
720 771
721 SmallVector<uint64_t, 64> Record; 772 SmallVector<uint64_t, 64> Record;
722 773
723 // Write metadata attachments 774 // Write metadata attachments
724 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 775 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
725 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; 776 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
726 777
727 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 778 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
728 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 779 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
729 I != E; ++I) { 780 I != E; ++I) {
(...skipping 20 matching lines...) Expand all
750 NaClBitstreamWriter &Stream) { 801 NaClBitstreamWriter &Stream) {
751 SmallVector<uint64_t, 64> Record; 802 SmallVector<uint64_t, 64> Record;
752 803
753 // Write metadata kinds 804 // Write metadata kinds
754 // METADATA_KIND - [n x [id, name]] 805 // METADATA_KIND - [n x [id, name]]
755 SmallVector<StringRef, 8> Names; 806 SmallVector<StringRef, 8> Names;
756 M->getMDKindNames(Names); 807 M->getMDKindNames(Names);
757 808
758 if (Names.empty()) return; 809 if (Names.empty()) return;
759 810
760 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); 811 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID);
jvoung (off chromium) 2013/05/21 17:24:17 Consistency with other entersubblock METADATA_BLOC
Karl 2013/05/21 23:06:52 Done.
761 812
762 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 813 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
763 Record.push_back(MDKindID); 814 Record.push_back(MDKindID);
764 StringRef KName = Names[MDKindID]; 815 StringRef KName = Names[MDKindID];
765 Record.append(KName.begin(), KName.end()); 816 Record.append(KName.begin(), KName.end());
766 817
767 Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0); 818 Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0);
768 Record.clear(); 819 Record.clear();
769 } 820 }
770 821
771 Stream.ExitBlock(); 822 Stream.ExitBlock();
772 } 823 }
773 824
774 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { 825 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
775 if ((int64_t)V >= 0) 826 Vals.push_back(NaClEncodeSignRotatedValue((int64_t)V));
776 Vals.push_back(V << 1);
777 else
778 Vals.push_back((-V << 1) | 1);
779 } 827 }
780 828
781 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals, 829 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals,
782 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val, 830 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val,
783 bool EmitSizeForWideNumbers = false 831 bool EmitSizeForWideNumbers = false
784 ) { 832 ) {
785 if (Val.getBitWidth() <= 64) { 833 if (Val.getBitWidth() <= 64) {
786 uint64_t V = Val.getSExtValue(); 834 uint64_t V = Val.getSExtValue();
787 emitSignedInt64(Vals, V); 835 emitSignedInt64(Vals, V);
788 Code = naclbitc::CST_CODE_INTEGER; 836 Code = naclbitc::CST_CODE_INTEGER;
(...skipping 15 matching lines...) Expand all
804 } 852 }
805 Code = naclbitc::CST_CODE_WIDE_INTEGER; 853 Code = naclbitc::CST_CODE_WIDE_INTEGER;
806 } 854 }
807 } 855 }
808 856
809 static void WriteConstants(unsigned FirstVal, unsigned LastVal, 857 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
810 const NaClValueEnumerator &VE, 858 const NaClValueEnumerator &VE,
811 NaClBitstreamWriter &Stream, bool isGlobal) { 859 NaClBitstreamWriter &Stream, bool isGlobal) {
812 if (FirstVal == LastVal) return; 860 if (FirstVal == LastVal) return;
813 861
814 Stream.EnterSubblock(naclbitc::CONSTANTS_BLOCK_ID, 4); 862 Stream.EnterSubblock(naclbitc::CONSTANTS_BLOCK_ID,
863 (isGlobal
864 ? CST_CONSTANTS_MAX_ABBREV
865 : CONSTANTS_MAX_ABBREV));
815 866
816 unsigned AggregateAbbrev = 0; 867 unsigned AggregateAbbrev = 0;
817 unsigned String8Abbrev = 0; 868 unsigned String8Abbrev = 0;
818 unsigned CString7Abbrev = 0; 869 unsigned CString7Abbrev = 0;
819 unsigned CString6Abbrev = 0; 870 unsigned CString6Abbrev = 0;
820 // If this is a constant pool for the module, emit module-specific abbrevs. 871 // If this is a constant pool for the module, emit module-specific abbrevs.
872 // Note: These abbreviations are size specific (to LastVal), and hence,
873 // can be more efficient if LastVal is known (rather then generating
874 // up-front for all constant sections).
821 if (isGlobal) { 875 if (isGlobal) {
822 // Abbrev for CST_CODE_AGGREGATE. 876 // Abbrev for CST_CODE_AGGREGATE.
823 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 877 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
824 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_AGGREGATE)); 878 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_AGGREGATE));
825 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 879 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
826 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 880 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed,
827 Log2_32_Ceil(LastVal+1))); 881 NaClBitsNeededForValue(LastVal)));
828 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 882 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
883 if (CST_CONSTANTS_AGGREGATE_ABBREV != AggregateAbbrev)
884 llvm_unreachable("Unexpected abbrev ordering!");
829 885
830 // Abbrev for CST_CODE_STRING. 886 // Abbrev for CST_CODE_STRING.
831 Abbv = new NaClBitCodeAbbrev(); 887 Abbv = new NaClBitCodeAbbrev();
832 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_STRING)); 888 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_STRING));
833 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 889 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
834 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); 890 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
835 String8Abbrev = Stream.EmitAbbrev(Abbv); 891 String8Abbrev = Stream.EmitAbbrev(Abbv);
892 if (CST_CONSTANTS_STRING_ABBREV != String8Abbrev)
893 llvm_unreachable("Unexpected abbrev ordering!");
894
836 // Abbrev for CST_CODE_CSTRING. 895 // Abbrev for CST_CODE_CSTRING.
837 Abbv = new NaClBitCodeAbbrev(); 896 Abbv = new NaClBitCodeAbbrev();
838 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); 897 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING));
839 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 898 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
840 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); 899 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7));
841 CString7Abbrev = Stream.EmitAbbrev(Abbv); 900 CString7Abbrev = Stream.EmitAbbrev(Abbv);
901 if (CST_CONSTANTS_CSTRING_7_ABBREV != CString7Abbrev)
902 llvm_unreachable("Unexpected abbrev ordering!");
903
842 // Abbrev for CST_CODE_CSTRING. 904 // Abbrev for CST_CODE_CSTRING.
843 Abbv = new NaClBitCodeAbbrev(); 905 Abbv = new NaClBitCodeAbbrev();
844 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); 906 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING));
845 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 907 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
846 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); 908 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6));
847 CString6Abbrev = Stream.EmitAbbrev(Abbv); 909 CString6Abbrev = Stream.EmitAbbrev(Abbv);
910 if (CST_CONSTANTS_CSTRING_6_ABBREV != CString6Abbrev)
911 llvm_unreachable("Unexpected abbrev ordering!");
912
913 DEBUG(dbgs() << "-- emitted abbreviations\n");
848 } 914 }
849 915
916
850 SmallVector<uint64_t, 64> Record; 917 SmallVector<uint64_t, 64> Record;
851 918
852 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); 919 const NaClValueEnumerator::ValueList &Vals = VE.getValues();
853 Type *LastTy = 0; 920 Type *LastTy = 0;
854 for (unsigned i = FirstVal; i != LastVal; ++i) { 921 for (unsigned i = FirstVal; i != LastVal; ++i) {
855 const Value *V = Vals[i].first; 922 const Value *V = Vals[i].first;
856 // If we need to switch types, do so now. 923 // If we need to switch types, do so now.
857 if (V->getType() != LastTy) { 924 if (V->getType() != LastTy) {
858 LastTy = V->getType(); 925 LastTy = V->getType();
859 Record.push_back(VE.getTypeID(LastTy)); 926 Record.push_back(VE.getTypeID(LastTy));
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 #ifndef NDEBUG 1112 #ifndef NDEBUG
1046 C->dump(); 1113 C->dump();
1047 #endif 1114 #endif
1048 llvm_unreachable("Unknown constant!"); 1115 llvm_unreachable("Unknown constant!");
1049 } 1116 }
1050 Stream.EmitRecord(Code, Record, AbbrevToUse); 1117 Stream.EmitRecord(Code, Record, AbbrevToUse);
1051 Record.clear(); 1118 Record.clear();
1052 } 1119 }
1053 1120
1054 Stream.ExitBlock(); 1121 Stream.ExitBlock();
1122 DEBUG(dbgs() << "<- WriteConstants\n");
1055 } 1123 }
1056 1124
1057 static void WriteModuleConstants(const NaClValueEnumerator &VE, 1125 static void WriteModuleConstants(const NaClValueEnumerator &VE,
1058 NaClBitstreamWriter &Stream) { 1126 NaClBitstreamWriter &Stream) {
1059 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); 1127 const NaClValueEnumerator::ValueList &Vals = VE.getValues();
1060 1128
1061 // Find the first constant to emit, which is the first non-globalvalue value. 1129 // Find the first constant to emit, which is the first non-globalvalue value.
1062 // We know globalvalues have been emitted by WriteModuleInfo. 1130 // We know globalvalues have been emitted by WriteModuleInfo.
1063 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 1131 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1064 if (!isa<GlobalValue>(Vals[i].first)) { 1132 if (!isa<GlobalValue>(Vals[i].first)) {
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
1469 1537
1470 Stream.EmitRecord(Code, Vals, AbbrevToUse); 1538 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1471 Vals.clear(); 1539 Vals.clear();
1472 } 1540 }
1473 1541
1474 // Emit names for globals/functions etc. 1542 // Emit names for globals/functions etc.
1475 static void WriteValueSymbolTable(const ValueSymbolTable &VST, 1543 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1476 const NaClValueEnumerator &VE, 1544 const NaClValueEnumerator &VE,
1477 NaClBitstreamWriter &Stream) { 1545 NaClBitstreamWriter &Stream) {
1478 if (VST.empty()) return; 1546 if (VST.empty()) return;
1479 Stream.EnterSubblock(naclbitc::VALUE_SYMTAB_BLOCK_ID, 4); 1547 Stream.EnterSubblock(naclbitc::VALUE_SYMTAB_BLOCK_ID, VST_MAX_ABBREV);
1480 1548
1481 // FIXME: Set up the abbrev, we know how many values there are! 1549 // FIXME: Set up the abbrev, we know how many values there are!
1482 // FIXME: We know if the type names can use 7-bit ascii. 1550 // FIXME: We know if the type names can use 7-bit ascii.
1483 SmallVector<unsigned, 64> NameVals; 1551 SmallVector<unsigned, 64> NameVals;
1484 1552
1485 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 1553 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1486 SI != SE; ++SI) { 1554 SI != SE; ++SI) {
1487 1555
1488 const ValueName &Name = *SI; 1556 const ValueName &Name = *SI;
1489 1557
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 // Emit the finished record. 1593 // Emit the finished record.
1526 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 1594 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
1527 NameVals.clear(); 1595 NameVals.clear();
1528 } 1596 }
1529 Stream.ExitBlock(); 1597 Stream.ExitBlock();
1530 } 1598 }
1531 1599
1532 /// WriteFunction - Emit a function body to the module stream. 1600 /// WriteFunction - Emit a function body to the module stream.
1533 static void WriteFunction(const Function &F, NaClValueEnumerator &VE, 1601 static void WriteFunction(const Function &F, NaClValueEnumerator &VE,
1534 NaClBitstreamWriter &Stream) { 1602 NaClBitstreamWriter &Stream) {
1535 Stream.EnterSubblock(naclbitc::FUNCTION_BLOCK_ID, 4); 1603 Stream.EnterSubblock(naclbitc::FUNCTION_BLOCK_ID, FUNCTION_INST_MAX_ABBREV);
1536 VE.incorporateFunction(F); 1604 VE.incorporateFunction(F);
1537 1605
1538 SmallVector<unsigned, 64> Vals; 1606 SmallVector<unsigned, 64> Vals;
1539 1607
1540 // Emit the number of basic blocks, so the reader can create them ahead of 1608 // Emit the number of basic blocks, so the reader can create them ahead of
1541 // time. 1609 // time.
1542 Vals.push_back(VE.getBasicBlocks().size()); 1610 Vals.push_back(VE.getBasicBlocks().size());
1543 Stream.EmitRecord(naclbitc::FUNC_CODE_DECLAREBLOCKS, Vals); 1611 Stream.EmitRecord(naclbitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1544 Vals.clear(); 1612 Vals.clear();
1545 1613
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1600 VE.purgeFunction(); 1668 VE.purgeFunction();
1601 Stream.ExitBlock(); 1669 Stream.ExitBlock();
1602 } 1670 }
1603 1671
1604 // Emit blockinfo, which defines the standard abbreviations etc. 1672 // Emit blockinfo, which defines the standard abbreviations etc.
1605 static void WriteBlockInfo(const NaClValueEnumerator &VE, 1673 static void WriteBlockInfo(const NaClValueEnumerator &VE,
1606 NaClBitstreamWriter &Stream) { 1674 NaClBitstreamWriter &Stream) {
1607 // We only want to emit block info records for blocks that have multiple 1675 // We only want to emit block info records for blocks that have multiple
1608 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. 1676 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
1609 // Other blocks can define their abbrevs inline. 1677 // Other blocks can define their abbrevs inline.
1610 Stream.EnterBlockInfoBlock(2); 1678 Stream.EnterBlockInfoBlock();
1611 1679
1612 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 1680 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1613 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1681 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1614 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 3)); 1682 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 3));
1615 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); 1683 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8));
1616 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); 1684 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
1617 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); 1685 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
1618 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID, 1686 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1619 Abbv) != VST_ENTRY_8_ABBREV) 1687 Abbv) != VST_ENTRY_8_ABBREV)
1620 llvm_unreachable("Unexpected abbrev ordering!"); 1688 llvm_unreachable("Unexpected abbrev ordering!");
(...skipping 28 matching lines...) Expand all
1649 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID, 1717 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1650 Abbv) != VST_BBENTRY_6_ABBREV) 1718 Abbv) != VST_BBENTRY_6_ABBREV)
1651 llvm_unreachable("Unexpected abbrev ordering!"); 1719 llvm_unreachable("Unexpected abbrev ordering!");
1652 } 1720 }
1653 1721
1654 1722
1655 1723
1656 { // SETTYPE abbrev for CONSTANTS_BLOCK. 1724 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1657 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1725 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1658 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_SETTYPE)); 1726 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_SETTYPE));
1659 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1727 Abbv->Add(NaClBitCodeAbbrevOp(
1660 Log2_32_Ceil(VE.getTypes().size()+1))); 1728 NaClBitCodeAbbrevOp::Fixed,
1729 NaClBitsNeededForValue(VE.getTypes().size())));
1661 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1730 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1662 Abbv) != CONSTANTS_SETTYPE_ABBREV) 1731 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1663 llvm_unreachable("Unexpected abbrev ordering!"); 1732 llvm_unreachable("Unexpected abbrev ordering!");
1664 } 1733 }
1665 1734
1666 { // INTEGER abbrev for CONSTANTS_BLOCK. 1735 { // INTEGER abbrev for CONSTANTS_BLOCK.
1667 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1736 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1668 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_INTEGER)); 1737 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_INTEGER));
1669 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); 1738 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8));
1670 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1739 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1671 Abbv) != CONSTANTS_INTEGER_ABBREV) 1740 Abbv) != CONSTANTS_INTEGER_ABBREV)
1672 llvm_unreachable("Unexpected abbrev ordering!"); 1741 llvm_unreachable("Unexpected abbrev ordering!");
1673 } 1742 }
1674 1743
1675 { // CE_CAST abbrev for CONSTANTS_BLOCK. 1744 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1676 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1745 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1677 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CE_CAST)); 1746 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CE_CAST));
1678 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // cast opc 1747 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // cast opc
1679 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // typeid 1748 Abbv->Add(NaClBitCodeAbbrevOp(
1680 Log2_32_Ceil(VE.getTypes().size()+1))); 1749 NaClBitCodeAbbrevOp::Fixed, // typeid
1750 NaClBitsNeededForValue(VE.getTypes().size())));
1681 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // value id 1751 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // value id
1682 1752
1683 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1753 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1684 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1754 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1685 llvm_unreachable("Unexpected abbrev ordering!"); 1755 llvm_unreachable("Unexpected abbrev ordering!");
1686 } 1756 }
1687 { // NULL abbrev for CONSTANTS_BLOCK. 1757 { // NULL abbrev for CONSTANTS_BLOCK.
1688 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1758 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1689 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_NULL)); 1759 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_NULL));
1690 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, 1760 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1722 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc 1792 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc
1723 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); // flags 1793 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); // flags
1724 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1794 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1725 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 1795 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1726 llvm_unreachable("Unexpected abbrev ordering!"); 1796 llvm_unreachable("Unexpected abbrev ordering!");
1727 } 1797 }
1728 { // INST_CAST abbrev for FUNCTION_BLOCK. 1798 { // INST_CAST abbrev for FUNCTION_BLOCK.
1729 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1799 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1730 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_CAST)); 1800 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_CAST));
1731 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // OpVal 1801 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // OpVal
1732 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // dest ty 1802 Abbv->Add(NaClBitCodeAbbrevOp(
1733 Log2_32_Ceil(VE.getTypes().size()+1))); 1803 NaClBitCodeAbbrevOp::Fixed, // dest ty
1804 NaClBitsNeededForValue(VE.getTypes().size())));
1734 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc 1805 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc
1735 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1806 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1736 Abbv) != FUNCTION_INST_CAST_ABBREV) 1807 Abbv) != FUNCTION_INST_CAST_ABBREV)
1737 llvm_unreachable("Unexpected abbrev ordering!"); 1808 llvm_unreachable("Unexpected abbrev ordering!");
1738 } 1809 }
1739 1810
1740 { // INST_RET abbrev for FUNCTION_BLOCK. 1811 { // INST_RET abbrev for FUNCTION_BLOCK.
1741 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1812 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1742 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); 1813 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET));
1743 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1814 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1744 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 1815 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1745 llvm_unreachable("Unexpected abbrev ordering!"); 1816 llvm_unreachable("Unexpected abbrev ordering!");
1746 } 1817 }
1747 { // INST_RET abbrev for FUNCTION_BLOCK. 1818 { // INST_RET abbrev for FUNCTION_BLOCK.
1748 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1819 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1749 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); 1820 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET));
1750 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // ValID 1821 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // ValID
1751 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1822 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1752 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 1823 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1753 llvm_unreachable("Unexpected abbrev ordering!"); 1824 llvm_unreachable("Unexpected abbrev ordering!");
1754 } 1825 }
1755 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 1826 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1756 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); 1827 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1757 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_UNREACHABLE)); 1828 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_UNREACHABLE));
1758 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, 1829 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1759 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 1830 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1760 llvm_unreachable("Unexpected abbrev ordering!"); 1831 llvm_unreachable("Unexpected abbrev ordering!");
1761 } 1832 }
1762 1833
1834 { // Abbrev for METADATA_STRING.
1835 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev();
1836 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::METADATA_STRING));
1837 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array));
1838 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8));
1839 if (Stream.EmitBlockInfoAbbrev(naclbitc::METADATA_BLOCK_ID,
1840 Abbv) != METADATA_STRING_ABBREV)
1841 llvm_unreachable("Unexpected abbrev ordering!");
1842 }
1843
1763 Stream.ExitBlock(); 1844 Stream.ExitBlock();
1764 } 1845 }
1765 1846
1766 /// WriteModule - Emit the specified module to the bitstream. 1847 /// WriteModule - Emit the specified module to the bitstream.
1767 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) { 1848 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) {
1768 Stream.EnterSubblock(naclbitc::MODULE_BLOCK_ID, 3); 1849 DEBUG(dbgs() << "-> WriteModule\n");
1850 Stream.EnterSubblock(naclbitc::MODULE_BLOCK_ID, MODULE_MAX_ABBREV);
1769 1851
1770 SmallVector<unsigned, 1> Vals; 1852 SmallVector<unsigned, 1> Vals;
1771 unsigned CurVersion = 1; 1853 unsigned CurVersion = 1;
1772 Vals.push_back(CurVersion); 1854 Vals.push_back(CurVersion);
1773 Stream.EmitRecord(naclbitc::MODULE_CODE_VERSION, Vals); 1855 Stream.EmitRecord(naclbitc::MODULE_CODE_VERSION, Vals);
1774 1856
1775 // Analyze the module, enumerating globals, functions, etc. 1857 // Analyze the module, enumerating globals, functions, etc.
1776 NaClValueEnumerator VE(M); 1858 NaClValueEnumerator VE(M);
1777 1859
1778 // Emit blockinfo, which defines the standard abbreviations etc. 1860 // Emit blockinfo, which defines the standard abbreviations etc.
(...skipping 23 matching lines...) Expand all
1802 1884
1803 // Emit names for globals/functions etc. 1885 // Emit names for globals/functions etc.
1804 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1886 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1805 1887
1806 // Emit function bodies. 1888 // Emit function bodies.
1807 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) 1889 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
1808 if (!F->isDeclaration()) 1890 if (!F->isDeclaration())
1809 WriteFunction(*F, VE, Stream); 1891 WriteFunction(*F, VE, Stream);
1810 1892
1811 Stream.ExitBlock(); 1893 Stream.ExitBlock();
1894 DEBUG(dbgs() << "<- WriteModule\n");
1812 } 1895 }
1813 1896
1814 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 1897 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1815 /// header and trailer to make it compatible with the system archiver. To do 1898 /// header and trailer to make it compatible with the system archiver. To do
1816 /// this we emit the following header, and then emit a trailer that pads the 1899 /// this we emit the following header, and then emit a trailer that pads the
1817 /// file out to be a multiple of 16 bytes. 1900 /// file out to be a multiple of 16 bytes.
1818 /// 1901 ///
1819 /// struct bc_header { 1902 /// struct bc_header {
1820 /// uint32_t Magic; // 0x0B17C0DE 1903 /// uint32_t Magic; // 0x0B17C0DE
1821 /// uint32_t Version; // Version, currently always 0. 1904 /// uint32_t Version; // Version, currently always 0.
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1914 // Emit the module. 1997 // Emit the module.
1915 WriteModule(M, Stream); 1998 WriteModule(M, Stream);
1916 } 1999 }
1917 2000
1918 if (TT.isOSDarwin()) 2001 if (TT.isOSDarwin())
1919 EmitDarwinBCHeaderAndTrailer(Buffer, TT); 2002 EmitDarwinBCHeaderAndTrailer(Buffer, TT);
1920 2003
1921 // Write the generated bitstream to "Out". 2004 // Write the generated bitstream to "Out".
1922 Out.write((char*)&Buffer.front(), Buffer.size()); 2005 Out.write((char*)&Buffer.front(), Buffer.size());
1923 } 2006 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698