Chromium Code Reviews| 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 14 matching lines...) Expand all Loading... | |
| 25 #include "llvm/IR/ValueSymbolTable.h" | 25 #include "llvm/IR/ValueSymbolTable.h" |
| 26 #include "llvm/Support/CommandLine.h" | 26 #include "llvm/Support/CommandLine.h" |
| 27 #include "llvm/Support/ErrorHandling.h" | 27 #include "llvm/Support/ErrorHandling.h" |
| 28 #include "llvm/Support/MathExtras.h" | 28 #include "llvm/Support/MathExtras.h" |
| 29 #include "llvm/Support/Program.h" | 29 #include "llvm/Support/Program.h" |
| 30 #include "llvm/Support/raw_ostream.h" | 30 #include "llvm/Support/raw_ostream.h" |
| 31 #include <cctype> | 31 #include <cctype> |
| 32 #include <map> | 32 #include <map> |
| 33 using namespace llvm; | 33 using namespace llvm; |
| 34 | 34 |
| 35 /* To turn on debugging for this file, change value of NACL_DEBUGGING to 1. | |
|
Karl
2013/05/17 20:52:18
Replaced this with llvm's DEBUG.
| |
| 36 */ | |
| 37 #define NACL_DEBUGGING 0 | |
| 38 #include "llvm/Bitcode/NaCl/NaClDebugging.h" | |
| 39 | |
| 35 /// These are manifest constants used by the bitcode writer. They do not need to | 40 /// These are manifest constants used by the bitcode writer. They do not need to |
| 36 /// be kept in sync with the reader, but need to be consistent within this file. | 41 /// be kept in sync with the reader, but need to be consistent within this file. |
| 37 enum { | 42 enum { |
| 38 // VALUE_SYMTAB_BLOCK abbrev id's. | 43 // VALUE_SYMTAB_BLOCK abbrev id's. |
| 39 VST_ENTRY_8_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, | 44 VST_ENTRY_8_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, |
| 40 VST_ENTRY_7_ABBREV, | 45 VST_ENTRY_7_ABBREV, |
| 41 VST_ENTRY_6_ABBREV, | 46 VST_ENTRY_6_ABBREV, |
| 42 VST_BBENTRY_6_ABBREV, | 47 VST_BBENTRY_6_ABBREV, |
| 48 VST_MAX_ABBREV = VST_BBENTRY_6_ABBREV, | |
| 43 | 49 |
| 44 // CONSTANTS_BLOCK abbrev id's. | 50 // CONSTANTS_BLOCK abbrev id's. |
| 45 CONSTANTS_SETTYPE_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, | 51 CONSTANTS_SETTYPE_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, |
| 46 CONSTANTS_INTEGER_ABBREV, | 52 CONSTANTS_INTEGER_ABBREV, |
| 47 CONSTANTS_CE_CAST_Abbrev, | 53 CONSTANTS_CE_CAST_Abbrev, |
| 48 CONSTANTS_NULL_Abbrev, | 54 CONSTANTS_NULL_Abbrev, |
| 55 CONSTANTS_MAX_ABBREV = CONSTANTS_NULL_Abbrev, | |
| 56 | |
| 57 // CONSTANTS_BLOCK abbrev id's when global (extends list above). | |
| 58 CST_CONSTANTS_AGGREGATE_ABBREV = CONSTANTS_MAX_ABBREV+1, | |
| 59 CST_CONSTANTS_STRING_ABBREV, | |
| 60 CST_CONSTANTS_CSTRING_7_ABBREV, | |
| 61 CST_CONSTANTS_CSTRING_6_ABBREV, | |
| 62 CST_CONSTANTS_MAX_ABBREV = CST_CONSTANTS_CSTRING_6_ABBREV, | |
| 49 | 63 |
| 50 // FUNCTION_BLOCK abbrev id's. | 64 // FUNCTION_BLOCK abbrev id's. |
| 51 FUNCTION_INST_LOAD_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, | 65 FUNCTION_INST_LOAD_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, |
| 52 FUNCTION_INST_BINOP_ABBREV, | 66 FUNCTION_INST_BINOP_ABBREV, |
| 53 FUNCTION_INST_BINOP_FLAGS_ABBREV, | 67 FUNCTION_INST_BINOP_FLAGS_ABBREV, |
| 54 FUNCTION_INST_CAST_ABBREV, | 68 FUNCTION_INST_CAST_ABBREV, |
| 55 FUNCTION_INST_RET_VOID_ABBREV, | 69 FUNCTION_INST_RET_VOID_ABBREV, |
| 56 FUNCTION_INST_RET_VAL_ABBREV, | 70 FUNCTION_INST_RET_VAL_ABBREV, |
| 57 FUNCTION_INST_UNREACHABLE_ABBREV, | 71 FUNCTION_INST_UNREACHABLE_ABBREV, |
| 72 FUNCTION_INST_MAX_ABBREV = FUNCTION_INST_UNREACHABLE_ABBREV, | |
| 73 | |
| 74 // TYPE_BLOCK_ID_NEW abbrev id's. | |
| 75 TYPE_POINTER_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, | |
| 76 TYPE_FUNCTION_ABBREV, | |
| 77 TYPE_STRUCT_ANON_ABBREV, | |
| 78 TYPE_STRUCT_NAME_ABBREV, | |
| 79 TYPE_STRUCT_NAMED_ABBREV, | |
| 80 TYPE_ARRAY_ABBREV, | |
| 81 TYPE_MAX_ABBREV = TYPE_ARRAY_ABBREV, | |
| 82 | |
| 83 // META_DATA_BLOCK abbrev id's. | |
| 84 METADATA_STRING_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, | |
| 85 METADATA_MAX_ABBREV = METADATA_STRING_ABBREV, | |
| 86 | |
| 87 // MODULE_BLOCK abbrev id's. | |
| 88 MODULE_GLOBALVAR_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV, | |
| 89 MODULE_MAX_ABBREV = MODULE_GLOBALVAR_ABBREV, | |
| 58 | 90 |
| 59 // SwitchInst Magic | 91 // SwitchInst Magic |
| 60 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex | 92 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
| 61 }; | 93 }; |
| 62 | 94 |
| 63 static unsigned GetEncodedCastOpcode(unsigned Opcode) { | 95 static unsigned GetEncodedCastOpcode(unsigned Opcode) { |
| 64 switch (Opcode) { | 96 switch (Opcode) { |
| 65 default: llvm_unreachable("Unknown cast instruction!"); | 97 default: llvm_unreachable("Unknown cast instruction!"); |
| 66 case Instruction::Trunc : return naclbitc::CAST_TRUNC; | 98 case Instruction::Trunc : return naclbitc::CAST_TRUNC; |
| 67 case Instruction::ZExt : return naclbitc::CAST_ZEXT; | 99 case Instruction::ZExt : return naclbitc::CAST_ZEXT; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 } | 185 } |
| 154 | 186 |
| 155 // Emit the finished record. | 187 // Emit the finished record. |
| 156 Stream.EmitRecord(Code, Vals, AbbrevToUse); | 188 Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 157 } | 189 } |
| 158 | 190 |
| 159 static void WriteAttributeGroupTable(const NaClValueEnumerator &VE, | 191 static void WriteAttributeGroupTable(const NaClValueEnumerator &VE, |
| 160 NaClBitstreamWriter &Stream) { | 192 NaClBitstreamWriter &Stream) { |
| 161 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups(); | 193 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups(); |
| 162 if (AttrGrps.empty()) return; | 194 if (AttrGrps.empty()) return; |
| 195 NACL_DEBUG(outs() << "-> WriteAbbributeGroupTable\n"); | |
| 163 | 196 |
| 164 Stream.EnterSubblock(naclbitc::PARAMATTR_GROUP_BLOCK_ID, 3); | 197 Stream.EnterSubblock(naclbitc::PARAMATTR_GROUP_BLOCK_ID); |
| 165 | 198 |
| 166 SmallVector<uint64_t, 64> Record; | 199 SmallVector<uint64_t, 64> Record; |
| 167 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) { | 200 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) { |
| 168 AttributeSet AS = AttrGrps[i]; | 201 AttributeSet AS = AttrGrps[i]; |
| 169 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) { | 202 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) { |
| 170 AttributeSet A = AS.getSlotAttributes(i); | 203 AttributeSet A = AS.getSlotAttributes(i); |
| 171 | 204 |
| 172 Record.push_back(VE.getAttributeGroupID(A)); | 205 Record.push_back(VE.getAttributeGroupID(A)); |
| 173 Record.push_back(AS.getSlotIndex(i)); | 206 Record.push_back(AS.getSlotIndex(i)); |
| 174 | 207 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 195 } | 228 } |
| 196 } | 229 } |
| 197 } | 230 } |
| 198 | 231 |
| 199 Stream.EmitRecord(naclbitc::PARAMATTR_GRP_CODE_ENTRY, Record); | 232 Stream.EmitRecord(naclbitc::PARAMATTR_GRP_CODE_ENTRY, Record); |
| 200 Record.clear(); | 233 Record.clear(); |
| 201 } | 234 } |
| 202 } | 235 } |
| 203 | 236 |
| 204 Stream.ExitBlock(); | 237 Stream.ExitBlock(); |
| 205 } | 238 NACL_DEBUG(outs() << "<- WriteAbbributeGroupTable\n");} |
| 206 | 239 |
| 207 static void WriteAttributeTable(const NaClValueEnumerator &VE, | 240 static void WriteAttributeTable(const NaClValueEnumerator &VE, |
| 208 NaClBitstreamWriter &Stream) { | 241 NaClBitstreamWriter &Stream) { |
| 209 const std::vector<AttributeSet> &Attrs = VE.getAttributes(); | 242 const std::vector<AttributeSet> &Attrs = VE.getAttributes(); |
| 210 if (Attrs.empty()) return; | 243 if (Attrs.empty()) return; |
| 244 NACL_DEBUG(outs() << "-> WriteAttributeTable\n"); | |
| 211 | 245 |
| 212 Stream.EnterSubblock(naclbitc::PARAMATTR_BLOCK_ID, 3); | 246 Stream.EnterSubblock(naclbitc::PARAMATTR_BLOCK_ID); |
| 213 | 247 |
| 214 SmallVector<uint64_t, 64> Record; | 248 SmallVector<uint64_t, 64> Record; |
| 215 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { | 249 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
| 216 const AttributeSet &A = Attrs[i]; | 250 const AttributeSet &A = Attrs[i]; |
| 217 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) | 251 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) |
| 218 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i))); | 252 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i))); |
| 219 | 253 |
| 220 Stream.EmitRecord(naclbitc::PARAMATTR_CODE_ENTRY, Record); | 254 Stream.EmitRecord(naclbitc::PARAMATTR_CODE_ENTRY, Record); |
| 221 Record.clear(); | 255 Record.clear(); |
| 222 } | 256 } |
| 223 | 257 |
| 224 Stream.ExitBlock(); | 258 Stream.ExitBlock(); |
| 259 NACL_DEBUG(outs() << "<- WriteAttributeTable\n"); | |
| 225 } | 260 } |
| 226 | 261 |
| 227 /// WriteTypeTable - Write out the type table for a module. | 262 /// WriteTypeTable - Write out the type table for a module. |
| 228 static void WriteTypeTable(const NaClValueEnumerator &VE, | 263 static void WriteTypeTable(const NaClValueEnumerator &VE, |
| 229 NaClBitstreamWriter &Stream) { | 264 NaClBitstreamWriter &Stream) { |
| 265 NACL_DEBUG(outs() << "-> WriteTypeTable\n"); | |
| 230 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes(); | 266 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes(); |
| 231 | 267 |
| 232 Stream.EnterSubblock(naclbitc::TYPE_BLOCK_ID_NEW, | 268 Stream.EnterSubblock(naclbitc::TYPE_BLOCK_ID_NEW, TYPE_MAX_ABBREV); |
| 233 4 /*count from # abbrevs */); | 269 |
| 234 SmallVector<uint64_t, 64> TypeVals; | 270 SmallVector<uint64_t, 64> TypeVals; |
| 235 | 271 |
| 236 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); | 272 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); |
| 237 | 273 |
| 238 // Abbrev for TYPE_CODE_POINTER. | 274 // Abbrev for TYPE_CODE_POINTER. |
| 239 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 275 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 240 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER)); | 276 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER)); |
| 241 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 277 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); |
| 242 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0 | 278 Abbv->Add(NaClBitCodeAbbrevOp(0)); // Addrspace = 0 |
| 243 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); | 279 if (TYPE_POINTER_ABBREV != Stream.EmitAbbrev(Abbv)) |
| 280 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 244 | 281 |
| 245 // Abbrev for TYPE_CODE_FUNCTION. | 282 // Abbrev for TYPE_CODE_FUNCTION. |
| 246 Abbv = new NaClBitCodeAbbrev(); | 283 Abbv = new NaClBitCodeAbbrev(); |
| 247 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION)); | 284 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION)); |
| 248 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg | 285 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // isvararg |
| 249 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 286 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 250 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 287 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); |
| 251 | 288 if (TYPE_FUNCTION_ABBREV != Stream.EmitAbbrev(Abbv)) |
| 252 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); | 289 llvm_unreachable("Unexpected abbrev ordering!"); |
| 253 | 290 |
| 254 // Abbrev for TYPE_CODE_STRUCT_ANON. | 291 // Abbrev for TYPE_CODE_STRUCT_ANON. |
| 255 Abbv = new NaClBitCodeAbbrev(); | 292 Abbv = new NaClBitCodeAbbrev(); |
| 256 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON)); | 293 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON)); |
| 257 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked | 294 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked |
| 258 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 295 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 259 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 296 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); |
| 260 | 297 if (TYPE_STRUCT_ANON_ABBREV != Stream.EmitAbbrev(Abbv)) |
| 261 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); | 298 llvm_unreachable("Unexpected abbrev ordering!"); |
| 262 | 299 |
| 263 // Abbrev for TYPE_CODE_STRUCT_NAME. | 300 // Abbrev for TYPE_CODE_STRUCT_NAME. |
| 264 Abbv = new NaClBitCodeAbbrev(); | 301 Abbv = new NaClBitCodeAbbrev(); |
| 265 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME)); | 302 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME)); |
| 266 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 303 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 267 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); | 304 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); |
| 268 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); | 305 if (TYPE_STRUCT_NAME_ABBREV != Stream.EmitAbbrev(Abbv)) |
| 306 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 269 | 307 |
| 270 // Abbrev for TYPE_CODE_STRUCT_NAMED. | 308 // Abbrev for TYPE_CODE_STRUCT_NAMED. |
| 271 Abbv = new NaClBitCodeAbbrev(); | 309 Abbv = new NaClBitCodeAbbrev(); |
| 272 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED)); | 310 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED)); |
| 273 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked | 311 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // ispacked |
| 274 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 312 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 275 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 313 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); |
| 276 | 314 if (TYPE_STRUCT_NAMED_ABBREV != Stream.EmitAbbrev(Abbv)) |
| 277 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); | 315 llvm_unreachable("Unexpected abbrev ordering!"); |
| 278 | 316 |
| 279 // Abbrev for TYPE_CODE_ARRAY. | 317 // Abbrev for TYPE_CODE_ARRAY. |
| 280 Abbv = new NaClBitCodeAbbrev(); | 318 Abbv = new NaClBitCodeAbbrev(); |
| 281 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY)); | 319 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY)); |
| 282 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size | 320 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // size |
| 283 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); | 321 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, NumBits)); |
| 284 | 322 if (TYPE_ARRAY_ABBREV != Stream.EmitAbbrev(Abbv)) |
| 285 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); | 323 llvm_unreachable("Unexpected abbrev ordering!"); |
| 286 | 324 |
| 287 // Emit an entry count so the reader can reserve space. | 325 // Emit an entry count so the reader can reserve space. |
| 288 TypeVals.push_back(TypeList.size()); | 326 TypeVals.push_back(TypeList.size()); |
| 289 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals); | 327 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals); |
| 290 TypeVals.clear(); | 328 TypeVals.clear(); |
| 291 | 329 |
| 292 // Loop over all of the types, emitting each in turn. | 330 // Loop over all of the types, emitting each in turn. |
| 293 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { | 331 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { |
| 294 Type *T = TypeList[i]; | 332 Type *T = TypeList[i]; |
| 295 int AbbrevToUse = 0; | 333 int AbbrevToUse = 0; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 312 Code = naclbitc::TYPE_CODE_INTEGER; | 350 Code = naclbitc::TYPE_CODE_INTEGER; |
| 313 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); | 351 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); |
| 314 break; | 352 break; |
| 315 case Type::PointerTyID: { | 353 case Type::PointerTyID: { |
| 316 PointerType *PTy = cast<PointerType>(T); | 354 PointerType *PTy = cast<PointerType>(T); |
| 317 // POINTER: [pointee type, address space] | 355 // POINTER: [pointee type, address space] |
| 318 Code = naclbitc::TYPE_CODE_POINTER; | 356 Code = naclbitc::TYPE_CODE_POINTER; |
| 319 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); | 357 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); |
| 320 unsigned AddressSpace = PTy->getAddressSpace(); | 358 unsigned AddressSpace = PTy->getAddressSpace(); |
| 321 TypeVals.push_back(AddressSpace); | 359 TypeVals.push_back(AddressSpace); |
| 322 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; | 360 if (AddressSpace == 0) AbbrevToUse = TYPE_POINTER_ABBREV; |
| 323 break; | 361 break; |
| 324 } | 362 } |
| 325 case Type::FunctionTyID: { | 363 case Type::FunctionTyID: { |
| 326 FunctionType *FT = cast<FunctionType>(T); | 364 FunctionType *FT = cast<FunctionType>(T); |
| 327 // FUNCTION: [isvararg, retty, paramty x N] | 365 // FUNCTION: [isvararg, retty, paramty x N] |
| 328 Code = naclbitc::TYPE_CODE_FUNCTION; | 366 Code = naclbitc::TYPE_CODE_FUNCTION; |
| 329 TypeVals.push_back(FT->isVarArg()); | 367 TypeVals.push_back(FT->isVarArg()); |
| 330 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); | 368 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); |
| 331 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) | 369 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) |
| 332 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); | 370 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); |
| 333 AbbrevToUse = FunctionAbbrev; | 371 AbbrevToUse = TYPE_FUNCTION_ABBREV; |
| 334 break; | 372 break; |
| 335 } | 373 } |
| 336 case Type::StructTyID: { | 374 case Type::StructTyID: { |
| 337 StructType *ST = cast<StructType>(T); | 375 StructType *ST = cast<StructType>(T); |
| 338 // STRUCT: [ispacked, eltty x N] | 376 // STRUCT: [ispacked, eltty x N] |
| 339 TypeVals.push_back(ST->isPacked()); | 377 TypeVals.push_back(ST->isPacked()); |
| 340 // Output all of the element types. | 378 // Output all of the element types. |
| 341 for (StructType::element_iterator I = ST->element_begin(), | 379 for (StructType::element_iterator I = ST->element_begin(), |
| 342 E = ST->element_end(); I != E; ++I) | 380 E = ST->element_end(); I != E; ++I) |
| 343 TypeVals.push_back(VE.getTypeID(*I)); | 381 TypeVals.push_back(VE.getTypeID(*I)); |
| 344 | 382 |
| 345 if (ST->isLiteral()) { | 383 if (ST->isLiteral()) { |
| 346 Code = naclbitc::TYPE_CODE_STRUCT_ANON; | 384 Code = naclbitc::TYPE_CODE_STRUCT_ANON; |
| 347 AbbrevToUse = StructAnonAbbrev; | 385 AbbrevToUse = TYPE_STRUCT_ANON_ABBREV; |
| 348 } else { | 386 } else { |
| 349 if (ST->isOpaque()) { | 387 if (ST->isOpaque()) { |
| 350 Code = naclbitc::TYPE_CODE_OPAQUE; | 388 Code = naclbitc::TYPE_CODE_OPAQUE; |
| 351 } else { | 389 } else { |
| 352 Code = naclbitc::TYPE_CODE_STRUCT_NAMED; | 390 Code = naclbitc::TYPE_CODE_STRUCT_NAMED; |
| 353 AbbrevToUse = StructNamedAbbrev; | 391 AbbrevToUse = TYPE_STRUCT_NAMED_ABBREV; |
| 354 } | 392 } |
| 355 | 393 |
| 356 // Emit the name if it is present. | 394 // Emit the name if it is present. |
| 357 if (!ST->getName().empty()) | 395 if (!ST->getName().empty()) |
| 358 WriteStringRecord(naclbitc::TYPE_CODE_STRUCT_NAME, ST->getName(), | 396 WriteStringRecord(naclbitc::TYPE_CODE_STRUCT_NAME, ST->getName(), |
| 359 StructNameAbbrev, Stream); | 397 TYPE_STRUCT_NAME_ABBREV, Stream); |
| 360 } | 398 } |
| 361 break; | 399 break; |
| 362 } | 400 } |
| 363 case Type::ArrayTyID: { | 401 case Type::ArrayTyID: { |
| 364 ArrayType *AT = cast<ArrayType>(T); | 402 ArrayType *AT = cast<ArrayType>(T); |
| 365 // ARRAY: [numelts, eltty] | 403 // ARRAY: [numelts, eltty] |
| 366 Code = naclbitc::TYPE_CODE_ARRAY; | 404 Code = naclbitc::TYPE_CODE_ARRAY; |
| 367 TypeVals.push_back(AT->getNumElements()); | 405 TypeVals.push_back(AT->getNumElements()); |
| 368 TypeVals.push_back(VE.getTypeID(AT->getElementType())); | 406 TypeVals.push_back(VE.getTypeID(AT->getElementType())); |
| 369 AbbrevToUse = ArrayAbbrev; | 407 AbbrevToUse = TYPE_ARRAY_ABBREV; |
| 370 break; | 408 break; |
| 371 } | 409 } |
| 372 case Type::VectorTyID: { | 410 case Type::VectorTyID: { |
| 373 VectorType *VT = cast<VectorType>(T); | 411 VectorType *VT = cast<VectorType>(T); |
| 374 // VECTOR [numelts, eltty] | 412 // VECTOR [numelts, eltty] |
| 375 Code = naclbitc::TYPE_CODE_VECTOR; | 413 Code = naclbitc::TYPE_CODE_VECTOR; |
| 376 TypeVals.push_back(VT->getNumElements()); | 414 TypeVals.push_back(VT->getNumElements()); |
| 377 TypeVals.push_back(VE.getTypeID(VT->getElementType())); | 415 TypeVals.push_back(VE.getTypeID(VT->getElementType())); |
| 378 break; | 416 break; |
| 379 } | 417 } |
| 380 } | 418 } |
| 381 | 419 |
| 382 // Emit the finished record. | 420 // Emit the finished record. |
| 383 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); | 421 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); |
| 384 TypeVals.clear(); | 422 TypeVals.clear(); |
| 385 } | 423 } |
| 386 | 424 |
| 387 Stream.ExitBlock(); | 425 Stream.ExitBlock(); |
| 426 NACL_DEBUG(outs() << "<- WriteTypeTable\n"); | |
| 388 } | 427 } |
| 389 | 428 |
| 390 static unsigned getEncodedLinkage(const GlobalValue *GV) { | 429 static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 391 switch (GV->getLinkage()) { | 430 switch (GV->getLinkage()) { |
| 392 case GlobalValue::ExternalLinkage: return 0; | 431 case GlobalValue::ExternalLinkage: return 0; |
| 393 case GlobalValue::WeakAnyLinkage: return 1; | 432 case GlobalValue::WeakAnyLinkage: return 1; |
| 394 case GlobalValue::AppendingLinkage: return 2; | 433 case GlobalValue::AppendingLinkage: return 2; |
| 395 case GlobalValue::InternalLinkage: return 3; | 434 case GlobalValue::InternalLinkage: return 3; |
| 396 case GlobalValue::LinkOnceAnyLinkage: return 4; | 435 case GlobalValue::LinkOnceAnyLinkage: return 4; |
| 397 case GlobalValue::DLLImportLinkage: return 5; | 436 case GlobalValue::DLLImportLinkage: return 5; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 426 case GlobalVariable::InitialExecTLSModel: return 3; | 465 case GlobalVariable::InitialExecTLSModel: return 3; |
| 427 case GlobalVariable::LocalExecTLSModel: return 4; | 466 case GlobalVariable::LocalExecTLSModel: return 4; |
| 428 } | 467 } |
| 429 llvm_unreachable("Invalid TLS model"); | 468 llvm_unreachable("Invalid TLS model"); |
| 430 } | 469 } |
| 431 | 470 |
| 432 // Emit top-level description of module, including target triple, inline asm, | 471 // Emit top-level description of module, including target triple, inline asm, |
| 433 // descriptors for global variables, and function prototype info. | 472 // descriptors for global variables, and function prototype info. |
| 434 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, | 473 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, |
| 435 NaClBitstreamWriter &Stream) { | 474 NaClBitstreamWriter &Stream) { |
| 475 NACL_DEBUG(outs() << "-> WriteModuleInfo\n"); | |
| 436 // Emit various pieces of data attached to a module. | 476 // Emit various pieces of data attached to a module. |
| 437 if (!M->getTargetTriple().empty()) | 477 if (!M->getTargetTriple().empty()) |
| 438 WriteStringRecord(naclbitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), | 478 WriteStringRecord(naclbitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), |
| 439 0/*TODO*/, Stream); | 479 0/*TODO*/, Stream); |
| 440 if (!M->getDataLayout().empty()) | 480 if (!M->getDataLayout().empty()) |
| 441 WriteStringRecord(naclbitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), | 481 WriteStringRecord(naclbitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), |
| 442 0/*TODO*/, Stream); | 482 0/*TODO*/, Stream); |
| 443 if (!M->getModuleInlineAsm().empty()) | 483 if (!M->getModuleInlineAsm().empty()) |
| 444 WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), | 484 WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), |
| 445 0/*TODO*/, Stream); | 485 0/*TODO*/, Stream); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 504 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, | 544 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, |
| 505 Log2_32_Ceil(MaxEncAlignment+1))); | 545 Log2_32_Ceil(MaxEncAlignment+1))); |
| 506 } | 546 } |
| 507 if (SectionMap.empty()) // Section. | 547 if (SectionMap.empty()) // Section. |
| 508 Abbv->Add(NaClBitCodeAbbrevOp(0)); | 548 Abbv->Add(NaClBitCodeAbbrevOp(0)); |
| 509 else | 549 else |
| 510 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, | 550 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, |
| 511 Log2_32_Ceil(SectionMap.size()+1))); | 551 Log2_32_Ceil(SectionMap.size()+1))); |
| 512 // Don't bother emitting vis + thread local. | 552 // Don't bother emitting vis + thread local. |
| 513 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); | 553 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); |
| 554 if (MODULE_GLOBALVAR_ABBREV != SimpleGVarAbbrev) | |
|
jvoung (off chromium)
2013/05/14 23:47:14
to be consistent with the other refactorings, you
Karl
2013/05/17 20:52:18
Added this to all levels, so that abbreviations ca
| |
| 555 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 514 } | 556 } |
| 515 | 557 |
| 516 // Emit the global variable information. | 558 // Emit the global variable information. |
| 517 SmallVector<unsigned, 64> Vals; | 559 SmallVector<unsigned, 64> Vals; |
| 518 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); | 560 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 519 GV != E; ++GV) { | 561 GV != E; ++GV) { |
| 520 unsigned AbbrevToUse = 0; | 562 unsigned AbbrevToUse = 0; |
| 521 | 563 |
| 522 // GLOBALVAR: [type, isconst, initid, | 564 // GLOBALVAR: [type, isconst, initid, |
| 523 // linkage, alignment, section, visibility, threadlocal, | 565 // linkage, alignment, section, visibility, threadlocal, |
| 524 // unnamed_addr] | 566 // unnamed_addr] |
| 525 Vals.push_back(VE.getTypeID(GV->getType())); | 567 Vals.push_back(VE.getTypeID(GV->getType())); |
| 526 Vals.push_back(GV->isConstant()); | 568 Vals.push_back(GV->isConstant()); |
| 527 Vals.push_back(GV->isDeclaration() ? 0 : | 569 Vals.push_back(GV->isDeclaration() ? 0 : |
| 528 (VE.getValueID(GV->getInitializer()) + 1)); | 570 (VE.getValueID(GV->getInitializer()) + 1)); |
| 529 Vals.push_back(getEncodedLinkage(GV)); | 571 Vals.push_back(getEncodedLinkage(GV)); |
| 530 Vals.push_back(Log2_32(GV->getAlignment())+1); | 572 Vals.push_back(Log2_32(GV->getAlignment())+1); |
| 531 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); | 573 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); |
| 532 if (GV->isThreadLocal() || | 574 if (GV->isThreadLocal() || |
| 533 GV->getVisibility() != GlobalValue::DefaultVisibility || | 575 GV->getVisibility() != GlobalValue::DefaultVisibility || |
| 534 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) { | 576 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) { |
| 535 Vals.push_back(getEncodedVisibility(GV)); | 577 Vals.push_back(getEncodedVisibility(GV)); |
| 536 Vals.push_back(getEncodedThreadLocalMode(GV)); | 578 Vals.push_back(getEncodedThreadLocalMode(GV)); |
| 537 Vals.push_back(GV->hasUnnamedAddr()); | 579 Vals.push_back(GV->hasUnnamedAddr()); |
| 538 Vals.push_back(GV->isExternallyInitialized()); | 580 Vals.push_back(GV->isExternallyInitialized()); |
| 539 } else { | 581 } else { |
| 540 AbbrevToUse = SimpleGVarAbbrev; | 582 AbbrevToUse = MODULE_GLOBALVAR_ABBREV; |
| 541 } | 583 } |
| 542 | 584 |
| 543 Stream.EmitRecord(naclbitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); | 585 Stream.EmitRecord(naclbitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); |
| 544 Vals.clear(); | 586 Vals.clear(); |
| 545 } | 587 } |
| 546 | 588 |
| 547 // Emit the function proto information. | 589 // Emit the function proto information. |
| 548 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { | 590 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
| 549 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, | 591 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, |
| 550 // section, visibility, gc, unnamed_addr] | 592 // section, visibility, gc, unnamed_addr] |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 569 AI != E; ++AI) { | 611 AI != E; ++AI) { |
| 570 // ALIAS: [alias type, aliasee val#, linkage, visibility] | 612 // ALIAS: [alias type, aliasee val#, linkage, visibility] |
| 571 Vals.push_back(VE.getTypeID(AI->getType())); | 613 Vals.push_back(VE.getTypeID(AI->getType())); |
| 572 Vals.push_back(VE.getValueID(AI->getAliasee())); | 614 Vals.push_back(VE.getValueID(AI->getAliasee())); |
| 573 Vals.push_back(getEncodedLinkage(AI)); | 615 Vals.push_back(getEncodedLinkage(AI)); |
| 574 Vals.push_back(getEncodedVisibility(AI)); | 616 Vals.push_back(getEncodedVisibility(AI)); |
| 575 unsigned AbbrevToUse = 0; | 617 unsigned AbbrevToUse = 0; |
| 576 Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); | 618 Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); |
| 577 Vals.clear(); | 619 Vals.clear(); |
| 578 } | 620 } |
| 621 NACL_DEBUG(outs() << "<- WriteModuleInfo\n"); | |
| 579 } | 622 } |
| 580 | 623 |
| 581 static uint64_t GetOptimizationFlags(const Value *V) { | 624 static uint64_t GetOptimizationFlags(const Value *V) { |
| 582 uint64_t Flags = 0; | 625 uint64_t Flags = 0; |
| 583 | 626 |
| 584 if (const OverflowingBinaryOperator *OBO = | 627 if (const OverflowingBinaryOperator *OBO = |
| 585 dyn_cast<OverflowingBinaryOperator>(V)) { | 628 dyn_cast<OverflowingBinaryOperator>(V)) { |
| 586 if (OBO->hasNoSignedWrap()) | 629 if (OBO->hasNoSignedWrap()) |
| 587 Flags |= 1 << naclbitc::OBO_NO_SIGNED_WRAP; | 630 Flags |= 1 << naclbitc::OBO_NO_SIGNED_WRAP; |
| 588 if (OBO->hasNoUnsignedWrap()) | 631 if (OBO->hasNoUnsignedWrap()) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 623 } | 666 } |
| 624 unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE : | 667 unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE : |
| 625 naclbitc::METADATA_NODE; | 668 naclbitc::METADATA_NODE; |
| 626 Stream.EmitRecord(MDCode, Record, 0); | 669 Stream.EmitRecord(MDCode, Record, 0); |
| 627 Record.clear(); | 670 Record.clear(); |
| 628 } | 671 } |
| 629 | 672 |
| 630 static void WriteModuleMetadata(const Module *M, | 673 static void WriteModuleMetadata(const Module *M, |
| 631 const NaClValueEnumerator &VE, | 674 const NaClValueEnumerator &VE, |
| 632 NaClBitstreamWriter &Stream) { | 675 NaClBitstreamWriter &Stream) { |
| 676 NACL_DEBUG(outs() << "-> WriteModuleMetadata\n"); | |
| 633 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues(); | 677 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues(); |
| 634 bool StartedMetadataBlock = false; | 678 bool StartedMetadataBlock = false; |
| 635 unsigned MDSAbbrev = 0; | 679 unsigned MDSAbbrev = 0; |
| 636 SmallVector<uint64_t, 64> Record; | 680 SmallVector<uint64_t, 64> Record; |
| 637 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { | 681 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 638 | 682 |
| 639 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { | 683 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { |
| 640 if (!N->isFunctionLocal() || !N->getFunction()) { | 684 if (!N->isFunctionLocal() || !N->getFunction()) { |
| 641 if (!StartedMetadataBlock) { | 685 if (!StartedMetadataBlock) { |
| 642 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); | 686 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, |
| 687 METADATA_MAX_ABBREV); | |
| 643 StartedMetadataBlock = true; | 688 StartedMetadataBlock = true; |
| 644 } | 689 } |
| 645 WriteMDNode(N, VE, Stream, Record); | 690 WriteMDNode(N, VE, Stream, Record); |
| 646 } | 691 } |
| 647 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { | 692 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { |
| 648 if (!StartedMetadataBlock) { | 693 if (!StartedMetadataBlock) { |
| 649 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); | 694 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, METADATA_MAX_ABBREV); |
| 650 | 695 |
| 651 // Abbrev for METADATA_STRING. | 696 // Abbrev for METADATA_STRING. |
|
Karl
2013/05/17 20:52:18
Moved this local abbreviation to the "global" crea
| |
| 652 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 697 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 653 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::METADATA_STRING)); | 698 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::METADATA_STRING)); |
| 654 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 699 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 655 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); | 700 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); |
| 656 MDSAbbrev = Stream.EmitAbbrev(Abbv); | 701 MDSAbbrev = Stream.EmitAbbrev(Abbv); |
| 702 if (METADATA_STRING_ABBREV != MDSAbbrev) | |
| 703 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 657 StartedMetadataBlock = true; | 704 StartedMetadataBlock = true; |
| 658 } | 705 } |
| 659 | 706 |
| 660 // Code: [strchar x N] | 707 // Code: [strchar x N] |
| 661 Record.append(MDS->begin(), MDS->end()); | 708 Record.append(MDS->begin(), MDS->end()); |
| 662 | 709 |
| 663 // Emit the finished record. | 710 // Emit the finished record. |
| 664 Stream.EmitRecord(naclbitc::METADATA_STRING, Record, MDSAbbrev); | 711 Stream.EmitRecord(naclbitc::METADATA_STRING, Record, MDSAbbrev); |
| 665 Record.clear(); | 712 Record.clear(); |
| 666 } | 713 } |
| 667 } | 714 } |
| 668 | 715 |
| 669 // Write named metadata. | 716 // Write named metadata. |
| 670 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), | 717 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), |
| 671 E = M->named_metadata_end(); I != E; ++I) { | 718 E = M->named_metadata_end(); I != E; ++I) { |
| 672 const NamedMDNode *NMD = I; | 719 const NamedMDNode *NMD = I; |
| 673 if (!StartedMetadataBlock) { | 720 if (!StartedMetadataBlock) { |
| 674 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); | 721 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, METADATA_MAX_ABBREV); |
| 675 StartedMetadataBlock = true; | 722 StartedMetadataBlock = true; |
| 676 } | 723 } |
| 677 | 724 |
| 678 // Write name. | 725 // Write name. |
| 679 StringRef Str = NMD->getName(); | 726 StringRef Str = NMD->getName(); |
| 680 for (unsigned i = 0, e = Str.size(); i != e; ++i) | 727 for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 681 Record.push_back(Str[i]); | 728 Record.push_back(Str[i]); |
| 682 Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/); | 729 Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/); |
| 683 Record.clear(); | 730 Record.clear(); |
| 684 | 731 |
| 685 // Write named metadata operands. | 732 // Write named metadata operands. |
| 686 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) | 733 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) |
| 687 Record.push_back(VE.getValueID(NMD->getOperand(i))); | 734 Record.push_back(VE.getValueID(NMD->getOperand(i))); |
| 688 Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0); | 735 Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0); |
| 689 Record.clear(); | 736 Record.clear(); |
| 690 } | 737 } |
| 691 | 738 |
| 692 if (StartedMetadataBlock) | 739 if (StartedMetadataBlock) |
| 693 Stream.ExitBlock(); | 740 Stream.ExitBlock(); |
| 741 | |
| 742 NACL_DEBUG(outs() << "<- WriteModuleMetadata\n"); | |
| 694 } | 743 } |
| 695 | 744 |
| 696 static void WriteFunctionLocalMetadata(const Function &F, | 745 static void WriteFunctionLocalMetadata(const Function &F, |
| 697 const NaClValueEnumerator &VE, | 746 const NaClValueEnumerator &VE, |
| 698 NaClBitstreamWriter &Stream) { | 747 NaClBitstreamWriter &Stream) { |
| 748 NACL_DEBUG(outs() << "-> WriteFunctionLocalMetadata\n"); | |
| 699 bool StartedMetadataBlock = false; | 749 bool StartedMetadataBlock = false; |
| 700 SmallVector<uint64_t, 64> Record; | 750 SmallVector<uint64_t, 64> Record; |
| 701 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); | 751 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); |
| 702 for (unsigned i = 0, e = Vals.size(); i != e; ++i) | 752 for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 703 if (const MDNode *N = Vals[i]) | 753 if (const MDNode *N = Vals[i]) |
| 704 if (N->isFunctionLocal() && N->getFunction() == &F) { | 754 if (N->isFunctionLocal() && N->getFunction() == &F) { |
| 705 if (!StartedMetadataBlock) { | 755 if (!StartedMetadataBlock) { |
| 706 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); | 756 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, |
| 757 METADATA_MAX_ABBREV); | |
| 707 StartedMetadataBlock = true; | 758 StartedMetadataBlock = true; |
| 708 } | 759 } |
| 709 WriteMDNode(N, VE, Stream, Record); | 760 WriteMDNode(N, VE, Stream, Record); |
| 710 } | 761 } |
| 711 | 762 |
| 712 if (StartedMetadataBlock) | 763 if (StartedMetadataBlock) |
| 713 Stream.ExitBlock(); | 764 Stream.ExitBlock(); |
| 765 NACL_DEBUG(outs() << "<- WriteFunctionLocalMetadata\n"); | |
| 714 } | 766 } |
| 715 | 767 |
| 716 static void WriteMetadataAttachment(const Function &F, | 768 static void WriteMetadataAttachment(const Function &F, |
| 717 const NaClValueEnumerator &VE, | 769 const NaClValueEnumerator &VE, |
| 718 NaClBitstreamWriter &Stream) { | 770 NaClBitstreamWriter &Stream) { |
| 719 Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID, 3); | 771 Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID); |
| 720 | 772 |
| 721 SmallVector<uint64_t, 64> Record; | 773 SmallVector<uint64_t, 64> Record; |
| 722 | 774 |
| 723 // Write metadata attachments | 775 // Write metadata attachments |
| 724 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] | 776 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] |
| 725 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; | 777 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; |
| 726 | 778 |
| 727 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) | 779 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 728 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); | 780 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 729 I != E; ++I) { | 781 I != E; ++I) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 750 NaClBitstreamWriter &Stream) { | 802 NaClBitstreamWriter &Stream) { |
| 751 SmallVector<uint64_t, 64> Record; | 803 SmallVector<uint64_t, 64> Record; |
| 752 | 804 |
| 753 // Write metadata kinds | 805 // Write metadata kinds |
| 754 // METADATA_KIND - [n x [id, name]] | 806 // METADATA_KIND - [n x [id, name]] |
| 755 SmallVector<StringRef, 8> Names; | 807 SmallVector<StringRef, 8> Names; |
| 756 M->getMDKindNames(Names); | 808 M->getMDKindNames(Names); |
| 757 | 809 |
| 758 if (Names.empty()) return; | 810 if (Names.empty()) return; |
| 759 | 811 |
| 760 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3); | 812 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID); |
| 761 | 813 |
| 762 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { | 814 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { |
| 763 Record.push_back(MDKindID); | 815 Record.push_back(MDKindID); |
| 764 StringRef KName = Names[MDKindID]; | 816 StringRef KName = Names[MDKindID]; |
| 765 Record.append(KName.begin(), KName.end()); | 817 Record.append(KName.begin(), KName.end()); |
| 766 | 818 |
| 767 Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0); | 819 Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0); |
| 768 Record.clear(); | 820 Record.clear(); |
| 769 } | 821 } |
| 770 | 822 |
| 771 Stream.ExitBlock(); | 823 Stream.ExitBlock(); |
| 772 } | 824 } |
| 773 | 825 |
| 774 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { | 826 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { |
|
Karl
2013/05/17 20:52:18
Rewrote this to use NaClEncodeSignRotatedValue def
| |
| 775 if ((int64_t)V >= 0) | 827 if ((int64_t)V >= 0) |
| 776 Vals.push_back(V << 1); | 828 Vals.push_back(V << 1); |
| 777 else | 829 else |
| 778 Vals.push_back((-V << 1) | 1); | 830 Vals.push_back((-V << 1) | 1); |
| 779 } | 831 } |
| 780 | 832 |
| 781 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals, | 833 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals, |
| 782 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val, | 834 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val, |
| 783 bool EmitSizeForWideNumbers = false | 835 bool EmitSizeForWideNumbers = false |
| 784 ) { | 836 ) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 804 } | 856 } |
| 805 Code = naclbitc::CST_CODE_WIDE_INTEGER; | 857 Code = naclbitc::CST_CODE_WIDE_INTEGER; |
| 806 } | 858 } |
| 807 } | 859 } |
| 808 | 860 |
| 809 static void WriteConstants(unsigned FirstVal, unsigned LastVal, | 861 static void WriteConstants(unsigned FirstVal, unsigned LastVal, |
| 810 const NaClValueEnumerator &VE, | 862 const NaClValueEnumerator &VE, |
| 811 NaClBitstreamWriter &Stream, bool isGlobal) { | 863 NaClBitstreamWriter &Stream, bool isGlobal) { |
| 812 if (FirstVal == LastVal) return; | 864 if (FirstVal == LastVal) return; |
| 813 | 865 |
| 814 Stream.EnterSubblock(naclbitc::CONSTANTS_BLOCK_ID, 4); | 866 NACL_DEBUG(outs() << "-> WriteConstants(" << isGlobal << ")\n"); |
| 867 Stream.EnterSubblock(naclbitc::CONSTANTS_BLOCK_ID, | |
| 868 (isGlobal | |
| 869 ? CST_CONSTANTS_MAX_ABBREV | |
| 870 : CONSTANTS_MAX_ABBREV)); | |
| 815 | 871 |
| 816 unsigned AggregateAbbrev = 0; | 872 unsigned AggregateAbbrev = 0; |
|
Karl
2013/05/17 20:52:18
Note that the constant abbreviations (or at least
| |
| 817 unsigned String8Abbrev = 0; | 873 unsigned String8Abbrev = 0; |
| 818 unsigned CString7Abbrev = 0; | 874 unsigned CString7Abbrev = 0; |
| 819 unsigned CString6Abbrev = 0; | 875 unsigned CString6Abbrev = 0; |
| 820 // If this is a constant pool for the module, emit module-specific abbrevs. | 876 // If this is a constant pool for the module, emit module-specific abbrevs. |
| 821 if (isGlobal) { | 877 if (isGlobal) { |
| 822 // Abbrev for CST_CODE_AGGREGATE. | 878 // Abbrev for CST_CODE_AGGREGATE. |
| 823 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 879 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 824 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_AGGREGATE)); | 880 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_AGGREGATE)); |
| 825 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 881 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 826 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, | 882 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, |
| 827 Log2_32_Ceil(LastVal+1))); | 883 Log2_32_Ceil(LastVal+1))); |
| 828 AggregateAbbrev = Stream.EmitAbbrev(Abbv); | 884 AggregateAbbrev = Stream.EmitAbbrev(Abbv); |
| 885 if (CST_CONSTANTS_AGGREGATE_ABBREV != AggregateAbbrev) | |
| 886 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 829 | 887 |
| 830 // Abbrev for CST_CODE_STRING. | 888 // Abbrev for CST_CODE_STRING. |
| 831 Abbv = new NaClBitCodeAbbrev(); | 889 Abbv = new NaClBitCodeAbbrev(); |
| 832 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_STRING)); | 890 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_STRING)); |
| 833 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 891 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 834 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); | 892 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); |
| 835 String8Abbrev = Stream.EmitAbbrev(Abbv); | 893 String8Abbrev = Stream.EmitAbbrev(Abbv); |
| 894 if (CST_CONSTANTS_STRING_ABBREV != String8Abbrev) | |
| 895 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 896 | |
| 836 // Abbrev for CST_CODE_CSTRING. | 897 // Abbrev for CST_CODE_CSTRING. |
| 837 Abbv = new NaClBitCodeAbbrev(); | 898 Abbv = new NaClBitCodeAbbrev(); |
| 838 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); | 899 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); |
| 839 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 900 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 840 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); | 901 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); |
| 841 CString7Abbrev = Stream.EmitAbbrev(Abbv); | 902 CString7Abbrev = Stream.EmitAbbrev(Abbv); |
| 903 if (CST_CONSTANTS_CSTRING_7_ABBREV != CString7Abbrev) | |
| 904 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 905 | |
| 842 // Abbrev for CST_CODE_CSTRING. | 906 // Abbrev for CST_CODE_CSTRING. |
| 843 Abbv = new NaClBitCodeAbbrev(); | 907 Abbv = new NaClBitCodeAbbrev(); |
| 844 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); | 908 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING)); |
| 845 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 909 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 846 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); | 910 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); |
| 847 CString6Abbrev = Stream.EmitAbbrev(Abbv); | 911 CString6Abbrev = Stream.EmitAbbrev(Abbv); |
| 912 if (CST_CONSTANTS_CSTRING_6_ABBREV != CString6Abbrev) | |
| 913 llvm_unreachable("Unexpected abbrev ordering!"); | |
| 848 } | 914 } |
| 849 | 915 |
| 850 SmallVector<uint64_t, 64> Record; | 916 SmallVector<uint64_t, 64> Record; |
| 851 | 917 |
| 852 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); | 918 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); |
| 853 Type *LastTy = 0; | 919 Type *LastTy = 0; |
| 854 for (unsigned i = FirstVal; i != LastVal; ++i) { | 920 for (unsigned i = FirstVal; i != LastVal; ++i) { |
| 855 const Value *V = Vals[i].first; | 921 const Value *V = Vals[i].first; |
| 856 // If we need to switch types, do so now. | 922 // If we need to switch types, do so now. |
| 857 if (V->getType() != LastTy) { | 923 if (V->getType() != LastTy) { |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1045 #ifndef NDEBUG | 1111 #ifndef NDEBUG |
| 1046 C->dump(); | 1112 C->dump(); |
| 1047 #endif | 1113 #endif |
| 1048 llvm_unreachable("Unknown constant!"); | 1114 llvm_unreachable("Unknown constant!"); |
| 1049 } | 1115 } |
| 1050 Stream.EmitRecord(Code, Record, AbbrevToUse); | 1116 Stream.EmitRecord(Code, Record, AbbrevToUse); |
| 1051 Record.clear(); | 1117 Record.clear(); |
| 1052 } | 1118 } |
| 1053 | 1119 |
| 1054 Stream.ExitBlock(); | 1120 Stream.ExitBlock(); |
| 1121 NACL_DEBUG(outs() << "<- WriteConstants\n"); | |
| 1055 } | 1122 } |
| 1056 | 1123 |
| 1057 static void WriteModuleConstants(const NaClValueEnumerator &VE, | 1124 static void WriteModuleConstants(const NaClValueEnumerator &VE, |
| 1058 NaClBitstreamWriter &Stream) { | 1125 NaClBitstreamWriter &Stream) { |
| 1059 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); | 1126 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); |
| 1060 | 1127 |
| 1061 // Find the first constant to emit, which is the first non-globalvalue value. | 1128 // Find the first constant to emit, which is the first non-globalvalue value. |
| 1062 // We know globalvalues have been emitted by WriteModuleInfo. | 1129 // We know globalvalues have been emitted by WriteModuleInfo. |
| 1063 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { | 1130 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 1064 if (!isa<GlobalValue>(Vals[i].first)) { | 1131 if (!isa<GlobalValue>(Vals[i].first)) { |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1469 | 1536 |
| 1470 Stream.EmitRecord(Code, Vals, AbbrevToUse); | 1537 Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 1471 Vals.clear(); | 1538 Vals.clear(); |
| 1472 } | 1539 } |
| 1473 | 1540 |
| 1474 // Emit names for globals/functions etc. | 1541 // Emit names for globals/functions etc. |
| 1475 static void WriteValueSymbolTable(const ValueSymbolTable &VST, | 1542 static void WriteValueSymbolTable(const ValueSymbolTable &VST, |
| 1476 const NaClValueEnumerator &VE, | 1543 const NaClValueEnumerator &VE, |
| 1477 NaClBitstreamWriter &Stream) { | 1544 NaClBitstreamWriter &Stream) { |
| 1478 if (VST.empty()) return; | 1545 if (VST.empty()) return; |
| 1479 Stream.EnterSubblock(naclbitc::VALUE_SYMTAB_BLOCK_ID, 4); | 1546 Stream.EnterSubblock(naclbitc::VALUE_SYMTAB_BLOCK_ID, VST_MAX_ABBREV); |
| 1480 | 1547 |
| 1481 // FIXME: Set up the abbrev, we know how many values there are! | 1548 // 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. | 1549 // FIXME: We know if the type names can use 7-bit ascii. |
| 1483 SmallVector<unsigned, 64> NameVals; | 1550 SmallVector<unsigned, 64> NameVals; |
| 1484 | 1551 |
| 1485 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); | 1552 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); |
| 1486 SI != SE; ++SI) { | 1553 SI != SE; ++SI) { |
| 1487 | 1554 |
| 1488 const ValueName &Name = *SI; | 1555 const ValueName &Name = *SI; |
| 1489 | 1556 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1525 // Emit the finished record. | 1592 // Emit the finished record. |
| 1526 Stream.EmitRecord(Code, NameVals, AbbrevToUse); | 1593 Stream.EmitRecord(Code, NameVals, AbbrevToUse); |
| 1527 NameVals.clear(); | 1594 NameVals.clear(); |
| 1528 } | 1595 } |
| 1529 Stream.ExitBlock(); | 1596 Stream.ExitBlock(); |
| 1530 } | 1597 } |
| 1531 | 1598 |
| 1532 /// WriteFunction - Emit a function body to the module stream. | 1599 /// WriteFunction - Emit a function body to the module stream. |
| 1533 static void WriteFunction(const Function &F, NaClValueEnumerator &VE, | 1600 static void WriteFunction(const Function &F, NaClValueEnumerator &VE, |
| 1534 NaClBitstreamWriter &Stream) { | 1601 NaClBitstreamWriter &Stream) { |
| 1535 Stream.EnterSubblock(naclbitc::FUNCTION_BLOCK_ID, 4); | 1602 Stream.EnterSubblock(naclbitc::FUNCTION_BLOCK_ID, FUNCTION_INST_MAX_ABBREV); |
| 1536 VE.incorporateFunction(F); | 1603 VE.incorporateFunction(F); |
| 1537 | 1604 |
| 1538 SmallVector<unsigned, 64> Vals; | 1605 SmallVector<unsigned, 64> Vals; |
| 1539 | 1606 |
| 1540 // Emit the number of basic blocks, so the reader can create them ahead of | 1607 // Emit the number of basic blocks, so the reader can create them ahead of |
| 1541 // time. | 1608 // time. |
| 1542 Vals.push_back(VE.getBasicBlocks().size()); | 1609 Vals.push_back(VE.getBasicBlocks().size()); |
| 1543 Stream.EmitRecord(naclbitc::FUNC_CODE_DECLAREBLOCKS, Vals); | 1610 Stream.EmitRecord(naclbitc::FUNC_CODE_DECLAREBLOCKS, Vals); |
| 1544 Vals.clear(); | 1611 Vals.clear(); |
| 1545 | 1612 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1600 VE.purgeFunction(); | 1667 VE.purgeFunction(); |
| 1601 Stream.ExitBlock(); | 1668 Stream.ExitBlock(); |
| 1602 } | 1669 } |
| 1603 | 1670 |
| 1604 // Emit blockinfo, which defines the standard abbreviations etc. | 1671 // Emit blockinfo, which defines the standard abbreviations etc. |
| 1605 static void WriteBlockInfo(const NaClValueEnumerator &VE, | 1672 static void WriteBlockInfo(const NaClValueEnumerator &VE, |
| 1606 NaClBitstreamWriter &Stream) { | 1673 NaClBitstreamWriter &Stream) { |
| 1607 // We only want to emit block info records for blocks that have multiple | 1674 // We only want to emit block info records for blocks that have multiple |
| 1608 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. | 1675 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. |
| 1609 // Other blocks can define their abbrevs inline. | 1676 // Other blocks can define their abbrevs inline. |
| 1610 Stream.EnterBlockInfoBlock(2); | 1677 Stream.EnterBlockInfoBlock(); |
| 1611 | 1678 |
| 1612 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. | 1679 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. |
| 1613 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1680 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1614 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 3)); | 1681 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 3)); |
| 1615 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); | 1682 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); |
| 1616 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 1683 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 1617 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); | 1684 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 8)); |
| 1618 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID, | 1685 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID, |
| 1619 Abbv) != VST_ENTRY_8_ABBREV) | 1686 Abbv) != VST_ENTRY_8_ABBREV) |
| 1620 llvm_unreachable("Unexpected abbrev ordering!"); | 1687 llvm_unreachable("Unexpected abbrev ordering!"); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1650 Abbv) != VST_BBENTRY_6_ABBREV) | 1717 Abbv) != VST_BBENTRY_6_ABBREV) |
| 1651 llvm_unreachable("Unexpected abbrev ordering!"); | 1718 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1652 } | 1719 } |
| 1653 | 1720 |
| 1654 | 1721 |
| 1655 | 1722 |
| 1656 { // SETTYPE abbrev for CONSTANTS_BLOCK. | 1723 { // SETTYPE abbrev for CONSTANTS_BLOCK. |
| 1657 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1724 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1658 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_SETTYPE)); | 1725 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_SETTYPE)); |
| 1659 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, | 1726 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, |
| 1660 Log2_32_Ceil(VE.getTypes().size()+1))); | 1727 Log2_32_Ceil(VE.getTypes().size()+1))); |
|
Karl
2013/05/17 20:52:18
Modified to use NaClBitsNeededForValue.
| |
| 1661 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, | 1728 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, |
| 1662 Abbv) != CONSTANTS_SETTYPE_ABBREV) | 1729 Abbv) != CONSTANTS_SETTYPE_ABBREV) |
| 1663 llvm_unreachable("Unexpected abbrev ordering!"); | 1730 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1664 } | 1731 } |
| 1665 | 1732 |
| 1666 { // INTEGER abbrev for CONSTANTS_BLOCK. | 1733 { // INTEGER abbrev for CONSTANTS_BLOCK. |
| 1667 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1734 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1668 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_INTEGER)); | 1735 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_INTEGER)); |
| 1669 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); | 1736 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); |
| 1670 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, | 1737 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, |
| 1671 Abbv) != CONSTANTS_INTEGER_ABBREV) | 1738 Abbv) != CONSTANTS_INTEGER_ABBREV) |
| 1672 llvm_unreachable("Unexpected abbrev ordering!"); | 1739 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1673 } | 1740 } |
| 1674 | 1741 |
| 1675 { // CE_CAST abbrev for CONSTANTS_BLOCK. | 1742 { // CE_CAST abbrev for CONSTANTS_BLOCK. |
| 1676 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1743 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1677 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CE_CAST)); | 1744 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_CE_CAST)); |
| 1678 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // cast opc | 1745 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // cast opc |
| 1679 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // typeid | 1746 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // typeid |
| 1680 Log2_32_Ceil(VE.getTypes().size()+1))); | 1747 Log2_32_Ceil(VE.getTypes().size()+1))); |
|
Karl
2013/05/17 20:52:18
Modified to use NaClBitsNeededForValue.
| |
| 1681 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // value id | 1748 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 8)); // value id |
| 1682 | 1749 |
| 1683 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, | 1750 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, |
| 1684 Abbv) != CONSTANTS_CE_CAST_Abbrev) | 1751 Abbv) != CONSTANTS_CE_CAST_Abbrev) |
| 1685 llvm_unreachable("Unexpected abbrev ordering!"); | 1752 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1686 } | 1753 } |
| 1687 { // NULL abbrev for CONSTANTS_BLOCK. | 1754 { // NULL abbrev for CONSTANTS_BLOCK. |
| 1688 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1755 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1689 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_NULL)); | 1756 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::CST_CODE_NULL)); |
| 1690 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, | 1757 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1723 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); // flags | 1790 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 7)); // flags |
| 1724 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, | 1791 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, |
| 1725 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) | 1792 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) |
| 1726 llvm_unreachable("Unexpected abbrev ordering!"); | 1793 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1727 } | 1794 } |
| 1728 { // INST_CAST abbrev for FUNCTION_BLOCK. | 1795 { // INST_CAST abbrev for FUNCTION_BLOCK. |
| 1729 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1796 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1730 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_CAST)); | 1797 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_CAST)); |
| 1731 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // OpVal | 1798 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // OpVal |
| 1732 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // dest ty | 1799 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, // dest ty |
| 1733 Log2_32_Ceil(VE.getTypes().size()+1))); | 1800 Log2_32_Ceil(VE.getTypes().size()+1))); |
|
Karl
2013/05/17 20:52:18
Modified to use NaClBitsNeededForValue.
| |
| 1734 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc | 1801 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 4)); // opc |
| 1735 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, | 1802 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, |
| 1736 Abbv) != FUNCTION_INST_CAST_ABBREV) | 1803 Abbv) != FUNCTION_INST_CAST_ABBREV) |
| 1737 llvm_unreachable("Unexpected abbrev ordering!"); | 1804 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1738 } | 1805 } |
| 1739 | 1806 |
| 1740 { // INST_RET abbrev for FUNCTION_BLOCK. | 1807 { // INST_RET abbrev for FUNCTION_BLOCK. |
| 1741 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1808 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1742 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); | 1809 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); |
| 1743 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, | 1810 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, |
| 1744 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) | 1811 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) |
| 1745 llvm_unreachable("Unexpected abbrev ordering!"); | 1812 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1746 } | 1813 } |
| 1747 { // INST_RET abbrev for FUNCTION_BLOCK. | 1814 { // INST_RET abbrev for FUNCTION_BLOCK. |
| 1748 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1815 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1749 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); | 1816 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET)); |
| 1750 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // ValID | 1817 Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // ValID |
| 1751 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, | 1818 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, |
| 1752 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) | 1819 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) |
| 1753 llvm_unreachable("Unexpected abbrev ordering!"); | 1820 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1754 } | 1821 } |
| 1755 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. | 1822 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. |
| 1756 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 1823 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 1757 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_UNREACHABLE)); | 1824 Abbv->Add(NaClBitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_UNREACHABLE)); |
| 1758 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, | 1825 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, |
| 1759 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) | 1826 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) |
| 1760 llvm_unreachable("Unexpected abbrev ordering!"); | 1827 llvm_unreachable("Unexpected abbrev ordering!"); |
| 1761 } | 1828 } |
|
Karl
2013/05/17 20:52:18
Added Abbreviations for METADATA_STRING, defining
| |
| 1762 | 1829 |
| 1763 Stream.ExitBlock(); | 1830 Stream.ExitBlock(); |
| 1764 } | 1831 } |
| 1765 | 1832 |
| 1766 /// WriteModule - Emit the specified module to the bitstream. | 1833 /// WriteModule - Emit the specified module to the bitstream. |
| 1767 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) { | 1834 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) { |
| 1768 Stream.EnterSubblock(naclbitc::MODULE_BLOCK_ID, 3); | 1835 NACL_DEBUG(outs() << "-> WriteModule\n"); |
| 1836 Stream.EnterSubblock(naclbitc::MODULE_BLOCK_ID, MODULE_MAX_ABBREV); | |
| 1769 | 1837 |
| 1770 SmallVector<unsigned, 1> Vals; | 1838 SmallVector<unsigned, 1> Vals; |
| 1771 unsigned CurVersion = 1; | 1839 unsigned CurVersion = 1; |
| 1772 Vals.push_back(CurVersion); | 1840 Vals.push_back(CurVersion); |
| 1773 Stream.EmitRecord(naclbitc::MODULE_CODE_VERSION, Vals); | 1841 Stream.EmitRecord(naclbitc::MODULE_CODE_VERSION, Vals); |
| 1774 | 1842 |
| 1775 // Analyze the module, enumerating globals, functions, etc. | 1843 // Analyze the module, enumerating globals, functions, etc. |
| 1776 NaClValueEnumerator VE(M); | 1844 NaClValueEnumerator VE(M); |
| 1777 | 1845 |
| 1778 // Emit blockinfo, which defines the standard abbreviations etc. | 1846 // Emit blockinfo, which defines the standard abbreviations etc. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1802 | 1870 |
| 1803 // Emit names for globals/functions etc. | 1871 // Emit names for globals/functions etc. |
| 1804 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); | 1872 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); |
| 1805 | 1873 |
| 1806 // Emit function bodies. | 1874 // Emit function bodies. |
| 1807 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) | 1875 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) |
| 1808 if (!F->isDeclaration()) | 1876 if (!F->isDeclaration()) |
| 1809 WriteFunction(*F, VE, Stream); | 1877 WriteFunction(*F, VE, Stream); |
| 1810 | 1878 |
| 1811 Stream.ExitBlock(); | 1879 Stream.ExitBlock(); |
| 1880 NACL_DEBUG(outs() << "<- WriteModule\n"); | |
| 1812 } | 1881 } |
| 1813 | 1882 |
| 1814 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a | 1883 /// 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 | 1884 /// 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 | 1885 /// this we emit the following header, and then emit a trailer that pads the |
| 1817 /// file out to be a multiple of 16 bytes. | 1886 /// file out to be a multiple of 16 bytes. |
| 1818 /// | 1887 /// |
| 1819 /// struct bc_header { | 1888 /// struct bc_header { |
| 1820 /// uint32_t Magic; // 0x0B17C0DE | 1889 /// uint32_t Magic; // 0x0B17C0DE |
| 1821 /// uint32_t Version; // Version, currently always 0. | 1890 /// uint32_t Version; // Version, currently always 0. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1914 // Emit the module. | 1983 // Emit the module. |
| 1915 WriteModule(M, Stream); | 1984 WriteModule(M, Stream); |
| 1916 } | 1985 } |
| 1917 | 1986 |
| 1918 if (TT.isOSDarwin()) | 1987 if (TT.isOSDarwin()) |
| 1919 EmitDarwinBCHeaderAndTrailer(Buffer, TT); | 1988 EmitDarwinBCHeaderAndTrailer(Buffer, TT); |
| 1920 | 1989 |
| 1921 // Write the generated bitstream to "Out". | 1990 // Write the generated bitstream to "Out". |
| 1922 Out.write((char*)&Buffer.front(), Buffer.size()); | 1991 Out.write((char*)&Buffer.front(), Buffer.size()); |
| 1923 } | 1992 } |
| OLD | NEW |