| OLD | NEW |
| 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. |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 226 |
| 227 /// WriteTypeTable - Write out the type table for a module. | 227 /// WriteTypeTable - Write out the type table for a module. |
| 228 static void WriteTypeTable(const NaClValueEnumerator &VE, | 228 static void WriteTypeTable(const NaClValueEnumerator &VE, |
| 229 NaClBitstreamWriter &Stream) { | 229 NaClBitstreamWriter &Stream) { |
| 230 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes(); | 230 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes(); |
| 231 | 231 |
| 232 Stream.EnterSubblock(naclbitc::TYPE_BLOCK_ID_NEW, | 232 Stream.EnterSubblock(naclbitc::TYPE_BLOCK_ID_NEW, |
| 233 4 /*count from # abbrevs */); | 233 4 /*count from # abbrevs */); |
| 234 SmallVector<uint64_t, 64> TypeVals; | 234 SmallVector<uint64_t, 64> TypeVals; |
| 235 | 235 |
| 236 // Note: modify to use maximum number of bits if under cutoff. Otherwise, |
| 237 // use VBR to take advantage that frequently referenced types have |
| 238 // small IDs. |
| 239 // |
| 240 // Note: Cutoff chosen based on experiments on pnacl-translate.pexe. |
| 236 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); | 241 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); |
| 242 static const uint64_t TypeVBRCutoff = 6; |
| 243 uint64_t TypeIdNumBits = (NumBits <= TypeVBRCutoff ? NumBits : TypeVBRCutoff); |
| 244 NaClBitCodeAbbrevOp::Encoding TypeIdEncoding = |
| 245 (NumBits <= TypeVBRCutoff |
| 246 ? NaClBitCodeAbbrevOp::Fixed : NaClBitCodeAbbrevOp::VBR); |
| 237 | 247 |
| 238 // Abbrev for TYPE_CODE_POINTER. | 248 // Abbrev for TYPE_CODE_POINTER. |
| 239 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 249 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 240 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER)); | 250 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER)); |
| 241 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 251 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); |
| 242 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0 | 252 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0 |
| 243 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); | 253 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); |
| 244 | 254 |
| 245 // Abbrev for TYPE_CODE_FUNCTION. | 255 // Abbrev for TYPE_CODE_FUNCTION. |
| 246 Abbv = new NaClBitCodeAbbrev(); | 256 Abbv = new NaClBitCodeAbbrev(); |
| 247 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION)); | 257 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION)); |
| 248 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg | 258 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg |
| 249 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 259 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 250 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 260 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); |
| 251 | 261 |
| 252 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); | 262 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); |
| 253 | 263 |
| 254 // Abbrev for TYPE_CODE_STRUCT_ANON. | 264 // Abbrev for TYPE_CODE_STRUCT_ANON. |
| 255 Abbv = new NaClBitCodeAbbrev(); | 265 Abbv = new NaClBitCodeAbbrev(); |
| 256 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON)); | 266 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON)); |
| 257 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked | 267 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked |
| 258 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 268 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 259 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 269 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); |
| 260 | 270 |
| 261 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); | 271 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); |
| 262 | 272 |
| 263 // Abbrev for TYPE_CODE_STRUCT_NAME. | 273 // Abbrev for TYPE_CODE_STRUCT_NAME. |
| 264 Abbv = new NaClBitCodeAbbrev(); | 274 Abbv = new NaClBitCodeAbbrev(); |
| 265 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME)); | 275 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME)); |
| 266 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 276 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 267 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); | 277 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); |
| 268 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); | 278 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); |
| 269 | 279 |
| 270 // Abbrev for TYPE_CODE_STRUCT_NAMED. | 280 // Abbrev for TYPE_CODE_STRUCT_NAMED. |
| 271 Abbv = new NaClBitCodeAbbrev(); | 281 Abbv = new NaClBitCodeAbbrev(); |
| 272 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED)); | 282 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED)); |
| 273 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked | 283 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked |
| 274 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 284 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 275 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 285 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); |
| 276 | 286 |
| 277 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); | 287 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); |
| 278 | 288 |
| 279 // Abbrev for TYPE_CODE_ARRAY. | 289 // Abbrev for TYPE_CODE_ARRAY. |
| 280 Abbv = new NaClBitCodeAbbrev(); | 290 Abbv = new NaClBitCodeAbbrev(); |
| 281 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY)); | 291 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY)); |
| 282 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size | 292 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size |
| 283 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 293 Abbv->Add(NaClBitCodeAbbrevOp(TypeIdEncoding, TypeIdNumBits)); |
| 284 | 294 |
| 285 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); | 295 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); |
| 286 | 296 |
| 287 // Emit an entry count so the reader can reserve space. | 297 // Emit an entry count so the reader can reserve space. |
| 288 TypeVals.push_back(TypeList.size()); | 298 TypeVals.push_back(TypeList.size()); |
| 289 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals); | 299 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals); |
| 290 TypeVals.clear(); | 300 TypeVals.clear(); |
| 291 | 301 |
| 292 // Loop over all of the types, emitting each in turn. | 302 // Loop over all of the types, emitting each in turn. |
| 293 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { | 303 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { |
| (...skipping 1620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1914 // Emit the module. | 1924 // Emit the module. |
| 1915 WriteModule(M, Stream); | 1925 WriteModule(M, Stream); |
| 1916 } | 1926 } |
| 1917 | 1927 |
| 1918 if (TT.isOSDarwin()) | 1928 if (TT.isOSDarwin()) |
| 1919 EmitDarwinBCHeaderAndTrailer(Buffer, TT); | 1929 EmitDarwinBCHeaderAndTrailer(Buffer, TT); |
| 1920 | 1930 |
| 1921 // Write the generated bitstream to "Out". | 1931 // Write the generated bitstream to "Out". |
| 1922 Out.write((char*)&Buffer.front(), Buffer.size()); | 1932 Out.write((char*)&Buffer.front(), Buffer.size()); |
| 1923 } | 1933 } |
| OLD | NEW |