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

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

Issue 14314016: Copy LLVM bitcode reader to generate a PNaCl wire format reader. (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: 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 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" 14 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
15 #include "NaClValueEnumerator.h" 15 #include "NaClValueEnumerator.h"
16 #include "llvm/ADT/Triple.h" 16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h" 17 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h"
18 #include "llvm/Bitcode/LLVMBitCodes.h" 18 #include "llvm/Bitcode/NaCl/NaClBitCodes.h"
19 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h" 20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/InlineAsm.h" 21 #include "llvm/IR/InlineAsm.h"
22 #include "llvm/IR/Instructions.h" 22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h" 23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h" 24 #include "llvm/IR/Operator.h"
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"
(...skipping 27 matching lines...) Expand all
56 FUNCTION_INST_RET_VAL_ABBREV, 56 FUNCTION_INST_RET_VAL_ABBREV,
57 FUNCTION_INST_UNREACHABLE_ABBREV, 57 FUNCTION_INST_UNREACHABLE_ABBREV,
58 58
59 // SwitchInst Magic 59 // SwitchInst Magic
60 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 60 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
61 }; 61 };
62 62
63 static unsigned GetEncodedCastOpcode(unsigned Opcode) { 63 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
64 switch (Opcode) { 64 switch (Opcode) {
65 default: llvm_unreachable("Unknown cast instruction!"); 65 default: llvm_unreachable("Unknown cast instruction!");
66 case Instruction::Trunc : return bitc::CAST_TRUNC; 66 case Instruction::Trunc : return naclbitc::CAST_TRUNC;
67 case Instruction::ZExt : return bitc::CAST_ZEXT; 67 case Instruction::ZExt : return naclbitc::CAST_ZEXT;
68 case Instruction::SExt : return bitc::CAST_SEXT; 68 case Instruction::SExt : return naclbitc::CAST_SEXT;
69 case Instruction::FPToUI : return bitc::CAST_FPTOUI; 69 case Instruction::FPToUI : return naclbitc::CAST_FPTOUI;
70 case Instruction::FPToSI : return bitc::CAST_FPTOSI; 70 case Instruction::FPToSI : return naclbitc::CAST_FPTOSI;
71 case Instruction::UIToFP : return bitc::CAST_UITOFP; 71 case Instruction::UIToFP : return naclbitc::CAST_UITOFP;
72 case Instruction::SIToFP : return bitc::CAST_SITOFP; 72 case Instruction::SIToFP : return naclbitc::CAST_SITOFP;
73 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; 73 case Instruction::FPTrunc : return naclbitc::CAST_FPTRUNC;
74 case Instruction::FPExt : return bitc::CAST_FPEXT; 74 case Instruction::FPExt : return naclbitc::CAST_FPEXT;
75 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; 75 case Instruction::PtrToInt: return naclbitc::CAST_PTRTOINT;
76 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; 76 case Instruction::IntToPtr: return naclbitc::CAST_INTTOPTR;
77 case Instruction::BitCast : return bitc::CAST_BITCAST; 77 case Instruction::BitCast : return naclbitc::CAST_BITCAST;
78 } 78 }
79 } 79 }
80 80
81 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { 81 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
82 switch (Opcode) { 82 switch (Opcode) {
83 default: llvm_unreachable("Unknown binary instruction!"); 83 default: llvm_unreachable("Unknown binary instruction!");
84 case Instruction::Add: 84 case Instruction::Add:
85 case Instruction::FAdd: return bitc::BINOP_ADD; 85 case Instruction::FAdd: return naclbitc::BINOP_ADD;
86 case Instruction::Sub: 86 case Instruction::Sub:
87 case Instruction::FSub: return bitc::BINOP_SUB; 87 case Instruction::FSub: return naclbitc::BINOP_SUB;
88 case Instruction::Mul: 88 case Instruction::Mul:
89 case Instruction::FMul: return bitc::BINOP_MUL; 89 case Instruction::FMul: return naclbitc::BINOP_MUL;
90 case Instruction::UDiv: return bitc::BINOP_UDIV; 90 case Instruction::UDiv: return naclbitc::BINOP_UDIV;
91 case Instruction::FDiv: 91 case Instruction::FDiv:
92 case Instruction::SDiv: return bitc::BINOP_SDIV; 92 case Instruction::SDiv: return naclbitc::BINOP_SDIV;
93 case Instruction::URem: return bitc::BINOP_UREM; 93 case Instruction::URem: return naclbitc::BINOP_UREM;
94 case Instruction::FRem: 94 case Instruction::FRem:
95 case Instruction::SRem: return bitc::BINOP_SREM; 95 case Instruction::SRem: return naclbitc::BINOP_SREM;
96 case Instruction::Shl: return bitc::BINOP_SHL; 96 case Instruction::Shl: return naclbitc::BINOP_SHL;
97 case Instruction::LShr: return bitc::BINOP_LSHR; 97 case Instruction::LShr: return naclbitc::BINOP_LSHR;
98 case Instruction::AShr: return bitc::BINOP_ASHR; 98 case Instruction::AShr: return naclbitc::BINOP_ASHR;
99 case Instruction::And: return bitc::BINOP_AND; 99 case Instruction::And: return naclbitc::BINOP_AND;
100 case Instruction::Or: return bitc::BINOP_OR; 100 case Instruction::Or: return naclbitc::BINOP_OR;
101 case Instruction::Xor: return bitc::BINOP_XOR; 101 case Instruction::Xor: return naclbitc::BINOP_XOR;
102 } 102 }
103 } 103 }
104 104
105 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) { 105 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
106 switch (Op) { 106 switch (Op) {
107 default: llvm_unreachable("Unknown RMW operation!"); 107 default: llvm_unreachable("Unknown RMW operation!");
108 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG; 108 case AtomicRMWInst::Xchg: return naclbitc::RMW_XCHG;
109 case AtomicRMWInst::Add: return bitc::RMW_ADD; 109 case AtomicRMWInst::Add: return naclbitc::RMW_ADD;
110 case AtomicRMWInst::Sub: return bitc::RMW_SUB; 110 case AtomicRMWInst::Sub: return naclbitc::RMW_SUB;
111 case AtomicRMWInst::And: return bitc::RMW_AND; 111 case AtomicRMWInst::And: return naclbitc::RMW_AND;
112 case AtomicRMWInst::Nand: return bitc::RMW_NAND; 112 case AtomicRMWInst::Nand: return naclbitc::RMW_NAND;
113 case AtomicRMWInst::Or: return bitc::RMW_OR; 113 case AtomicRMWInst::Or: return naclbitc::RMW_OR;
114 case AtomicRMWInst::Xor: return bitc::RMW_XOR; 114 case AtomicRMWInst::Xor: return naclbitc::RMW_XOR;
115 case AtomicRMWInst::Max: return bitc::RMW_MAX; 115 case AtomicRMWInst::Max: return naclbitc::RMW_MAX;
116 case AtomicRMWInst::Min: return bitc::RMW_MIN; 116 case AtomicRMWInst::Min: return naclbitc::RMW_MIN;
117 case AtomicRMWInst::UMax: return bitc::RMW_UMAX; 117 case AtomicRMWInst::UMax: return naclbitc::RMW_UMAX;
118 case AtomicRMWInst::UMin: return bitc::RMW_UMIN; 118 case AtomicRMWInst::UMin: return naclbitc::RMW_UMIN;
119 } 119 }
120 } 120 }
121 121
122 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) { 122 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
123 switch (Ordering) { 123 switch (Ordering) {
124 case NotAtomic: return bitc::ORDERING_NOTATOMIC; 124 case NotAtomic: return naclbitc::ORDERING_NOTATOMIC;
125 case Unordered: return bitc::ORDERING_UNORDERED; 125 case Unordered: return naclbitc::ORDERING_UNORDERED;
126 case Monotonic: return bitc::ORDERING_MONOTONIC; 126 case Monotonic: return naclbitc::ORDERING_MONOTONIC;
127 case Acquire: return bitc::ORDERING_ACQUIRE; 127 case Acquire: return naclbitc::ORDERING_ACQUIRE;
128 case Release: return bitc::ORDERING_RELEASE; 128 case Release: return naclbitc::ORDERING_RELEASE;
129 case AcquireRelease: return bitc::ORDERING_ACQREL; 129 case AcquireRelease: return naclbitc::ORDERING_ACQREL;
130 case SequentiallyConsistent: return bitc::ORDERING_SEQCST; 130 case SequentiallyConsistent: return naclbitc::ORDERING_SEQCST;
131 } 131 }
132 llvm_unreachable("Invalid ordering"); 132 llvm_unreachable("Invalid ordering");
133 } 133 }
134 134
135 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) { 135 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
136 switch (SynchScope) { 136 switch (SynchScope) {
137 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD; 137 case SingleThread: return naclbitc::SYNCHSCOPE_SINGLETHREAD;
138 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD; 138 case CrossThread: return naclbitc::SYNCHSCOPE_CROSSTHREAD;
139 } 139 }
140 llvm_unreachable("Invalid synch scope"); 140 llvm_unreachable("Invalid synch scope");
141 } 141 }
142 142
143 static void WriteStringRecord(unsigned Code, StringRef Str, 143 static void WriteStringRecord(unsigned Code, StringRef Str,
144 unsigned AbbrevToUse, NaClBitstreamWriter &Stream) { 144 unsigned AbbrevToUse, NaClBitstreamWriter &Stream) {
145 SmallVector<unsigned, 64> Vals; 145 SmallVector<unsigned, 64> Vals;
146 146
147 // Code: [strchar x N] 147 // Code: [strchar x N]
148 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 148 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
149 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i])) 149 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
150 AbbrevToUse = 0; 150 AbbrevToUse = 0;
151 Vals.push_back(Str[i]); 151 Vals.push_back(Str[i]);
152 } 152 }
153 153
154 // Emit the finished record. 154 // Emit the finished record.
155 Stream.EmitRecord(Code, Vals, AbbrevToUse); 155 Stream.EmitRecord(Code, Vals, AbbrevToUse);
156 } 156 }
157 157
158 static void WriteAttributeGroupTable(const NaClValueEnumerator &VE, 158 static void WriteAttributeGroupTable(const NaClValueEnumerator &VE,
159 NaClBitstreamWriter &Stream) { 159 NaClBitstreamWriter &Stream) {
160 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups(); 160 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
161 if (AttrGrps.empty()) return; 161 if (AttrGrps.empty()) return;
162 162
163 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3); 163 Stream.EnterSubblock(naclbitc::PARAMATTR_GROUP_BLOCK_ID, 3);
164 164
165 SmallVector<uint64_t, 64> Record; 165 SmallVector<uint64_t, 64> Record;
166 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) { 166 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
167 AttributeSet AS = AttrGrps[i]; 167 AttributeSet AS = AttrGrps[i];
168 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) { 168 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
169 AttributeSet A = AS.getSlotAttributes(i); 169 AttributeSet A = AS.getSlotAttributes(i);
170 170
171 Record.push_back(VE.getAttributeGroupID(A)); 171 Record.push_back(VE.getAttributeGroupID(A));
172 Record.push_back(AS.getSlotIndex(i)); 172 Record.push_back(AS.getSlotIndex(i));
173 173
(...skipping 14 matching lines...) Expand all
188 Record.push_back(Val.empty() ? 3 : 4); 188 Record.push_back(Val.empty() ? 3 : 4);
189 Record.append(Kind.begin(), Kind.end()); 189 Record.append(Kind.begin(), Kind.end());
190 Record.push_back(0); 190 Record.push_back(0);
191 if (!Val.empty()) { 191 if (!Val.empty()) {
192 Record.append(Val.begin(), Val.end()); 192 Record.append(Val.begin(), Val.end());
193 Record.push_back(0); 193 Record.push_back(0);
194 } 194 }
195 } 195 }
196 } 196 }
197 197
198 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record); 198 Stream.EmitRecord(naclbitc::PARAMATTR_GRP_CODE_ENTRY, Record);
199 Record.clear(); 199 Record.clear();
200 } 200 }
201 } 201 }
202 202
203 Stream.ExitBlock(); 203 Stream.ExitBlock();
204 } 204 }
205 205
206 static void WriteAttributeTable(const NaClValueEnumerator &VE, 206 static void WriteAttributeTable(const NaClValueEnumerator &VE,
207 NaClBitstreamWriter &Stream) { 207 NaClBitstreamWriter &Stream) {
208 const std::vector<AttributeSet> &Attrs = VE.getAttributes(); 208 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
209 if (Attrs.empty()) return; 209 if (Attrs.empty()) return;
210 210
211 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); 211 Stream.EnterSubblock(naclbitc::PARAMATTR_BLOCK_ID, 3);
212 212
213 SmallVector<uint64_t, 64> Record; 213 SmallVector<uint64_t, 64> Record;
214 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { 214 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
215 const AttributeSet &A = Attrs[i]; 215 const AttributeSet &A = Attrs[i];
216 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) 216 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
217 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i))); 217 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
218 218
219 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); 219 Stream.EmitRecord(naclbitc::PARAMATTR_CODE_ENTRY, Record);
220 Record.clear(); 220 Record.clear();
221 } 221 }
222 222
223 Stream.ExitBlock(); 223 Stream.ExitBlock();
224 } 224 }
225 225
226 /// WriteTypeTable - Write out the type table for a module. 226 /// WriteTypeTable - Write out the type table for a module.
227 static void WriteTypeTable(const NaClValueEnumerator &VE, 227 static void WriteTypeTable(const NaClValueEnumerator &VE,
228 NaClBitstreamWriter &Stream) { 228 NaClBitstreamWriter &Stream) {
229 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes(); 229 const NaClValueEnumerator::TypeList &TypeList = VE.getTypes();
230 230
231 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); 231 Stream.EnterSubblock(naclbitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */) ;
232 SmallVector<uint64_t, 64> TypeVals; 232 SmallVector<uint64_t, 64> TypeVals;
233 233
234 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); 234 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1);
235 235
236 // Abbrev for TYPE_CODE_POINTER. 236 // Abbrev for TYPE_CODE_POINTER.
237 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 237 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
238 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); 238 Abbv->Add(BitCodeAbbrevOp(naclbitc::TYPE_CODE_POINTER));
239 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 239 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
240 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 240 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
241 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 241 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
242 242
243 // Abbrev for TYPE_CODE_FUNCTION. 243 // Abbrev for TYPE_CODE_FUNCTION.
244 Abbv = new BitCodeAbbrev(); 244 Abbv = new BitCodeAbbrev();
245 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); 245 Abbv->Add(BitCodeAbbrevOp(naclbitc::TYPE_CODE_FUNCTION));
246 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg 246 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
247 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 247 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
248 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 248 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
249 249
250 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 250 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
251 251
252 // Abbrev for TYPE_CODE_STRUCT_ANON. 252 // Abbrev for TYPE_CODE_STRUCT_ANON.
253 Abbv = new BitCodeAbbrev(); 253 Abbv = new BitCodeAbbrev();
254 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON)); 254 Abbv->Add(BitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_ANON));
255 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 255 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
256 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 256 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
257 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 257 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
258 258
259 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); 259 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
260 260
261 // Abbrev for TYPE_CODE_STRUCT_NAME. 261 // Abbrev for TYPE_CODE_STRUCT_NAME.
262 Abbv = new BitCodeAbbrev(); 262 Abbv = new BitCodeAbbrev();
263 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME)); 263 Abbv->Add(BitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAME));
264 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 264 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
265 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 265 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
266 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); 266 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
267 267
268 // Abbrev for TYPE_CODE_STRUCT_NAMED. 268 // Abbrev for TYPE_CODE_STRUCT_NAMED.
269 Abbv = new BitCodeAbbrev(); 269 Abbv = new BitCodeAbbrev();
270 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED)); 270 Abbv->Add(BitCodeAbbrevOp(naclbitc::TYPE_CODE_STRUCT_NAMED));
271 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 271 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
272 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 272 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
273 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 273 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
274 274
275 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); 275 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
276 276
277 // Abbrev for TYPE_CODE_ARRAY. 277 // Abbrev for TYPE_CODE_ARRAY.
278 Abbv = new BitCodeAbbrev(); 278 Abbv = new BitCodeAbbrev();
279 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); 279 Abbv->Add(BitCodeAbbrevOp(naclbitc::TYPE_CODE_ARRAY));
280 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size 280 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
281 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 281 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
282 282
283 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 283 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
284 284
285 // Emit an entry count so the reader can reserve space. 285 // Emit an entry count so the reader can reserve space.
286 TypeVals.push_back(TypeList.size()); 286 TypeVals.push_back(TypeList.size());
287 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); 287 Stream.EmitRecord(naclbitc::TYPE_CODE_NUMENTRY, TypeVals);
288 TypeVals.clear(); 288 TypeVals.clear();
289 289
290 // Loop over all of the types, emitting each in turn. 290 // Loop over all of the types, emitting each in turn.
291 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 291 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
292 Type *T = TypeList[i]; 292 Type *T = TypeList[i];
293 int AbbrevToUse = 0; 293 int AbbrevToUse = 0;
294 unsigned Code = 0; 294 unsigned Code = 0;
295 295
296 switch (T->getTypeID()) { 296 switch (T->getTypeID()) {
297 default: llvm_unreachable("Unknown type!"); 297 default: llvm_unreachable("Unknown type!");
298 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; 298 case Type::VoidTyID: Code = naclbitc::TYPE_CODE_VOID; break;
299 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break; 299 case Type::HalfTyID: Code = naclbitc::TYPE_CODE_HALF; break;
300 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; 300 case Type::FloatTyID: Code = naclbitc::TYPE_CODE_FLOAT; break;
301 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; 301 case Type::DoubleTyID: Code = naclbitc::TYPE_CODE_DOUBLE; break;
302 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; 302 case Type::X86_FP80TyID: Code = naclbitc::TYPE_CODE_X86_FP80; break;
303 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; 303 case Type::FP128TyID: Code = naclbitc::TYPE_CODE_FP128; break;
304 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; 304 case Type::PPC_FP128TyID: Code = naclbitc::TYPE_CODE_PPC_FP128; break;
305 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; 305 case Type::LabelTyID: Code = naclbitc::TYPE_CODE_LABEL; break;
306 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break; 306 case Type::MetadataTyID: Code = naclbitc::TYPE_CODE_METADATA; break;
307 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break; 307 case Type::X86_MMXTyID: Code = naclbitc::TYPE_CODE_X86_MMX; break;
308 case Type::IntegerTyID: 308 case Type::IntegerTyID:
309 // INTEGER: [width] 309 // INTEGER: [width]
310 Code = bitc::TYPE_CODE_INTEGER; 310 Code = naclbitc::TYPE_CODE_INTEGER;
311 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 311 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
312 break; 312 break;
313 case Type::PointerTyID: { 313 case Type::PointerTyID: {
314 PointerType *PTy = cast<PointerType>(T); 314 PointerType *PTy = cast<PointerType>(T);
315 // POINTER: [pointee type, address space] 315 // POINTER: [pointee type, address space]
316 Code = bitc::TYPE_CODE_POINTER; 316 Code = naclbitc::TYPE_CODE_POINTER;
317 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 317 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
318 unsigned AddressSpace = PTy->getAddressSpace(); 318 unsigned AddressSpace = PTy->getAddressSpace();
319 TypeVals.push_back(AddressSpace); 319 TypeVals.push_back(AddressSpace);
320 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 320 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
321 break; 321 break;
322 } 322 }
323 case Type::FunctionTyID: { 323 case Type::FunctionTyID: {
324 FunctionType *FT = cast<FunctionType>(T); 324 FunctionType *FT = cast<FunctionType>(T);
325 // FUNCTION: [isvararg, retty, paramty x N] 325 // FUNCTION: [isvararg, retty, paramty x N]
326 Code = bitc::TYPE_CODE_FUNCTION; 326 Code = naclbitc::TYPE_CODE_FUNCTION;
327 TypeVals.push_back(FT->isVarArg()); 327 TypeVals.push_back(FT->isVarArg());
328 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 328 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
329 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 329 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
330 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 330 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
331 AbbrevToUse = FunctionAbbrev; 331 AbbrevToUse = FunctionAbbrev;
332 break; 332 break;
333 } 333 }
334 case Type::StructTyID: { 334 case Type::StructTyID: {
335 StructType *ST = cast<StructType>(T); 335 StructType *ST = cast<StructType>(T);
336 // STRUCT: [ispacked, eltty x N] 336 // STRUCT: [ispacked, eltty x N]
337 TypeVals.push_back(ST->isPacked()); 337 TypeVals.push_back(ST->isPacked());
338 // Output all of the element types. 338 // Output all of the element types.
339 for (StructType::element_iterator I = ST->element_begin(), 339 for (StructType::element_iterator I = ST->element_begin(),
340 E = ST->element_end(); I != E; ++I) 340 E = ST->element_end(); I != E; ++I)
341 TypeVals.push_back(VE.getTypeID(*I)); 341 TypeVals.push_back(VE.getTypeID(*I));
342 342
343 if (ST->isLiteral()) { 343 if (ST->isLiteral()) {
344 Code = bitc::TYPE_CODE_STRUCT_ANON; 344 Code = naclbitc::TYPE_CODE_STRUCT_ANON;
345 AbbrevToUse = StructAnonAbbrev; 345 AbbrevToUse = StructAnonAbbrev;
346 } else { 346 } else {
347 if (ST->isOpaque()) { 347 if (ST->isOpaque()) {
348 Code = bitc::TYPE_CODE_OPAQUE; 348 Code = naclbitc::TYPE_CODE_OPAQUE;
349 } else { 349 } else {
350 Code = bitc::TYPE_CODE_STRUCT_NAMED; 350 Code = naclbitc::TYPE_CODE_STRUCT_NAMED;
351 AbbrevToUse = StructNamedAbbrev; 351 AbbrevToUse = StructNamedAbbrev;
352 } 352 }
353 353
354 // Emit the name if it is present. 354 // Emit the name if it is present.
355 if (!ST->getName().empty()) 355 if (!ST->getName().empty())
356 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 356 WriteStringRecord(naclbitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
357 StructNameAbbrev, Stream); 357 StructNameAbbrev, Stream);
358 } 358 }
359 break; 359 break;
360 } 360 }
361 case Type::ArrayTyID: { 361 case Type::ArrayTyID: {
362 ArrayType *AT = cast<ArrayType>(T); 362 ArrayType *AT = cast<ArrayType>(T);
363 // ARRAY: [numelts, eltty] 363 // ARRAY: [numelts, eltty]
364 Code = bitc::TYPE_CODE_ARRAY; 364 Code = naclbitc::TYPE_CODE_ARRAY;
365 TypeVals.push_back(AT->getNumElements()); 365 TypeVals.push_back(AT->getNumElements());
366 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 366 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
367 AbbrevToUse = ArrayAbbrev; 367 AbbrevToUse = ArrayAbbrev;
368 break; 368 break;
369 } 369 }
370 case Type::VectorTyID: { 370 case Type::VectorTyID: {
371 VectorType *VT = cast<VectorType>(T); 371 VectorType *VT = cast<VectorType>(T);
372 // VECTOR [numelts, eltty] 372 // VECTOR [numelts, eltty]
373 Code = bitc::TYPE_CODE_VECTOR; 373 Code = naclbitc::TYPE_CODE_VECTOR;
374 TypeVals.push_back(VT->getNumElements()); 374 TypeVals.push_back(VT->getNumElements());
375 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 375 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
376 break; 376 break;
377 } 377 }
378 } 378 }
379 379
380 // Emit the finished record. 380 // Emit the finished record.
381 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 381 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
382 TypeVals.clear(); 382 TypeVals.clear();
383 } 383 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 } 426 }
427 llvm_unreachable("Invalid TLS model"); 427 llvm_unreachable("Invalid TLS model");
428 } 428 }
429 429
430 // Emit top-level description of module, including target triple, inline asm, 430 // Emit top-level description of module, including target triple, inline asm,
431 // descriptors for global variables, and function prototype info. 431 // descriptors for global variables, and function prototype info.
432 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE, 432 static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE,
433 NaClBitstreamWriter &Stream) { 433 NaClBitstreamWriter &Stream) {
434 // Emit various pieces of data attached to a module. 434 // Emit various pieces of data attached to a module.
435 if (!M->getTargetTriple().empty()) 435 if (!M->getTargetTriple().empty())
436 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 436 WriteStringRecord(naclbitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
437 0/*TODO*/, Stream); 437 0/*TODO*/, Stream);
438 if (!M->getDataLayout().empty()) 438 if (!M->getDataLayout().empty())
439 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), 439 WriteStringRecord(naclbitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
440 0/*TODO*/, Stream); 440 0/*TODO*/, Stream);
441 if (!M->getModuleInlineAsm().empty()) 441 if (!M->getModuleInlineAsm().empty())
442 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 442 WriteStringRecord(naclbitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
443 0/*TODO*/, Stream); 443 0/*TODO*/, Stream);
444 444
445 // Emit information about sections and GC, computing how many there are. Also 445 // Emit information about sections and GC, computing how many there are. Also
446 // compute the maximum alignment value. 446 // compute the maximum alignment value.
447 std::map<std::string, unsigned> SectionMap; 447 std::map<std::string, unsigned> SectionMap;
448 std::map<std::string, unsigned> GCMap; 448 std::map<std::string, unsigned> GCMap;
449 unsigned MaxAlignment = 0; 449 unsigned MaxAlignment = 0;
450 unsigned MaxGlobalType = 0; 450 unsigned MaxGlobalType = 0;
451 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 451 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
452 GV != E; ++GV) { 452 GV != E; ++GV) {
453 MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); 453 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
454 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); 454 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
455 if (GV->hasSection()) { 455 if (GV->hasSection()) {
456 // Give section names unique ID's. 456 // Give section names unique ID's.
457 unsigned &Entry = SectionMap[GV->getSection()]; 457 unsigned &Entry = SectionMap[GV->getSection()];
458 if (!Entry) { 458 if (!Entry) {
459 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), 459 WriteStringRecord(naclbitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
460 0/*TODO*/, Stream); 460 0/*TODO*/, Stream);
461 Entry = SectionMap.size(); 461 Entry = SectionMap.size();
462 } 462 }
463 } 463 }
464 } 464 }
465 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 465 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
466 MaxAlignment = std::max(MaxAlignment, F->getAlignment()); 466 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
467 if (F->hasSection()) { 467 if (F->hasSection()) {
468 // Give section names unique ID's. 468 // Give section names unique ID's.
469 unsigned &Entry = SectionMap[F->getSection()]; 469 unsigned &Entry = SectionMap[F->getSection()];
470 if (!Entry) { 470 if (!Entry) {
471 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), 471 WriteStringRecord(naclbitc::MODULE_CODE_SECTIONNAME, F->getSection(),
472 0/*TODO*/, Stream); 472 0/*TODO*/, Stream);
473 Entry = SectionMap.size(); 473 Entry = SectionMap.size();
474 } 474 }
475 } 475 }
476 if (F->hasGC()) { 476 if (F->hasGC()) {
477 // Same for GC names. 477 // Same for GC names.
478 unsigned &Entry = GCMap[F->getGC()]; 478 unsigned &Entry = GCMap[F->getGC()];
479 if (!Entry) { 479 if (!Entry) {
480 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(), 480 WriteStringRecord(naclbitc::MODULE_CODE_GCNAME, F->getGC(),
481 0/*TODO*/, Stream); 481 0/*TODO*/, Stream);
482 Entry = GCMap.size(); 482 Entry = GCMap.size();
483 } 483 }
484 } 484 }
485 } 485 }
486 486
487 // Emit abbrev for globals, now that we know # sections and max alignment. 487 // Emit abbrev for globals, now that we know # sections and max alignment.
488 unsigned SimpleGVarAbbrev = 0; 488 unsigned SimpleGVarAbbrev = 0;
489 if (!M->global_empty()) { 489 if (!M->global_empty()) {
490 // Add an abbrev for common globals with no visibility or thread localness. 490 // Add an abbrev for common globals with no visibility or thread localness.
491 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 491 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
492 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 492 Abbv->Add(BitCodeAbbrevOp(naclbitc::MODULE_CODE_GLOBALVAR));
493 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 493 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
494 Log2_32_Ceil(MaxGlobalType+1))); 494 Log2_32_Ceil(MaxGlobalType+1)));
495 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. 495 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
496 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 496 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
497 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. 497 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
498 if (MaxAlignment == 0) // Alignment. 498 if (MaxAlignment == 0) // Alignment.
499 Abbv->Add(BitCodeAbbrevOp(0)); 499 Abbv->Add(BitCodeAbbrevOp(0));
500 else { 500 else {
501 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 501 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
502 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 502 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
(...skipping 28 matching lines...) Expand all
531 GV->getVisibility() != GlobalValue::DefaultVisibility || 531 GV->getVisibility() != GlobalValue::DefaultVisibility ||
532 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) { 532 GV->hasUnnamedAddr() || GV->isExternallyInitialized()) {
533 Vals.push_back(getEncodedVisibility(GV)); 533 Vals.push_back(getEncodedVisibility(GV));
534 Vals.push_back(getEncodedThreadLocalMode(GV)); 534 Vals.push_back(getEncodedThreadLocalMode(GV));
535 Vals.push_back(GV->hasUnnamedAddr()); 535 Vals.push_back(GV->hasUnnamedAddr());
536 Vals.push_back(GV->isExternallyInitialized()); 536 Vals.push_back(GV->isExternallyInitialized());
537 } else { 537 } else {
538 AbbrevToUse = SimpleGVarAbbrev; 538 AbbrevToUse = SimpleGVarAbbrev;
539 } 539 }
540 540
541 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 541 Stream.EmitRecord(naclbitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
542 Vals.clear(); 542 Vals.clear();
543 } 543 }
544 544
545 // Emit the function proto information. 545 // Emit the function proto information.
546 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 546 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
547 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, 547 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
548 // section, visibility, gc, unnamed_addr] 548 // section, visibility, gc, unnamed_addr]
549 Vals.push_back(VE.getTypeID(F->getType())); 549 Vals.push_back(VE.getTypeID(F->getType()));
550 Vals.push_back(F->getCallingConv()); 550 Vals.push_back(F->getCallingConv());
551 Vals.push_back(F->isDeclaration()); 551 Vals.push_back(F->isDeclaration());
552 Vals.push_back(getEncodedLinkage(F)); 552 Vals.push_back(getEncodedLinkage(F));
553 Vals.push_back(VE.getAttributeID(F->getAttributes())); 553 Vals.push_back(VE.getAttributeID(F->getAttributes()));
554 Vals.push_back(Log2_32(F->getAlignment())+1); 554 Vals.push_back(Log2_32(F->getAlignment())+1);
555 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); 555 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
556 Vals.push_back(getEncodedVisibility(F)); 556 Vals.push_back(getEncodedVisibility(F));
557 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0); 557 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
558 Vals.push_back(F->hasUnnamedAddr()); 558 Vals.push_back(F->hasUnnamedAddr());
559 559
560 unsigned AbbrevToUse = 0; 560 unsigned AbbrevToUse = 0;
561 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 561 Stream.EmitRecord(naclbitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
562 Vals.clear(); 562 Vals.clear();
563 } 563 }
564 564
565 // Emit the alias information. 565 // Emit the alias information.
566 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); 566 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
567 AI != E; ++AI) { 567 AI != E; ++AI) {
568 // ALIAS: [alias type, aliasee val#, linkage, visibility] 568 // ALIAS: [alias type, aliasee val#, linkage, visibility]
569 Vals.push_back(VE.getTypeID(AI->getType())); 569 Vals.push_back(VE.getTypeID(AI->getType()));
570 Vals.push_back(VE.getValueID(AI->getAliasee())); 570 Vals.push_back(VE.getValueID(AI->getAliasee()));
571 Vals.push_back(getEncodedLinkage(AI)); 571 Vals.push_back(getEncodedLinkage(AI));
572 Vals.push_back(getEncodedVisibility(AI)); 572 Vals.push_back(getEncodedVisibility(AI));
573 unsigned AbbrevToUse = 0; 573 unsigned AbbrevToUse = 0;
574 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 574 Stream.EmitRecord(naclbitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
575 Vals.clear(); 575 Vals.clear();
576 } 576 }
577 } 577 }
578 578
579 static uint64_t GetOptimizationFlags(const Value *V) { 579 static uint64_t GetOptimizationFlags(const Value *V) {
580 uint64_t Flags = 0; 580 uint64_t Flags = 0;
581 581
582 if (const OverflowingBinaryOperator *OBO = 582 if (const OverflowingBinaryOperator *OBO =
583 dyn_cast<OverflowingBinaryOperator>(V)) { 583 dyn_cast<OverflowingBinaryOperator>(V)) {
584 if (OBO->hasNoSignedWrap()) 584 if (OBO->hasNoSignedWrap())
585 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; 585 Flags |= 1 << naclbitc::OBO_NO_SIGNED_WRAP;
586 if (OBO->hasNoUnsignedWrap()) 586 if (OBO->hasNoUnsignedWrap())
587 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; 587 Flags |= 1 << naclbitc::OBO_NO_UNSIGNED_WRAP;
588 } else if (const PossiblyExactOperator *PEO = 588 } else if (const PossiblyExactOperator *PEO =
589 dyn_cast<PossiblyExactOperator>(V)) { 589 dyn_cast<PossiblyExactOperator>(V)) {
590 if (PEO->isExact()) 590 if (PEO->isExact())
591 Flags |= 1 << bitc::PEO_EXACT; 591 Flags |= 1 << naclbitc::PEO_EXACT;
592 } else if (const FPMathOperator *FPMO = 592 } else if (const FPMathOperator *FPMO =
593 dyn_cast<const FPMathOperator>(V)) { 593 dyn_cast<const FPMathOperator>(V)) {
594 if (FPMO->hasUnsafeAlgebra()) 594 if (FPMO->hasUnsafeAlgebra())
595 Flags |= FastMathFlags::UnsafeAlgebra; 595 Flags |= FastMathFlags::UnsafeAlgebra;
596 if (FPMO->hasNoNaNs()) 596 if (FPMO->hasNoNaNs())
597 Flags |= FastMathFlags::NoNaNs; 597 Flags |= FastMathFlags::NoNaNs;
598 if (FPMO->hasNoInfs()) 598 if (FPMO->hasNoInfs())
599 Flags |= FastMathFlags::NoInfs; 599 Flags |= FastMathFlags::NoInfs;
600 if (FPMO->hasNoSignedZeros()) 600 if (FPMO->hasNoSignedZeros())
601 Flags |= FastMathFlags::NoSignedZeros; 601 Flags |= FastMathFlags::NoSignedZeros;
(...skipping 10 matching lines...) Expand all
612 SmallVector<uint64_t, 64> &Record) { 612 SmallVector<uint64_t, 64> &Record) {
613 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 613 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
614 if (N->getOperand(i)) { 614 if (N->getOperand(i)) {
615 Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); 615 Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
616 Record.push_back(VE.getValueID(N->getOperand(i))); 616 Record.push_back(VE.getValueID(N->getOperand(i)));
617 } else { 617 } else {
618 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); 618 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
619 Record.push_back(0); 619 Record.push_back(0);
620 } 620 }
621 } 621 }
622 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE : 622 unsigned MDCode = N->isFunctionLocal() ? naclbitc::METADATA_FN_NODE :
623 bitc::METADATA_NODE; 623 naclbitc::METADATA_NODE;
624 Stream.EmitRecord(MDCode, Record, 0); 624 Stream.EmitRecord(MDCode, Record, 0);
625 Record.clear(); 625 Record.clear();
626 } 626 }
627 627
628 static void WriteModuleMetadata(const Module *M, 628 static void WriteModuleMetadata(const Module *M,
629 const NaClValueEnumerator &VE, 629 const NaClValueEnumerator &VE,
630 NaClBitstreamWriter &Stream) { 630 NaClBitstreamWriter &Stream) {
631 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues(); 631 const NaClValueEnumerator::ValueList &Vals = VE.getMDValues();
632 bool StartedMetadataBlock = false; 632 bool StartedMetadataBlock = false;
633 unsigned MDSAbbrev = 0; 633 unsigned MDSAbbrev = 0;
634 SmallVector<uint64_t, 64> Record; 634 SmallVector<uint64_t, 64> Record;
635 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 635 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
636 636
637 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { 637 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
638 if (!N->isFunctionLocal() || !N->getFunction()) { 638 if (!N->isFunctionLocal() || !N->getFunction()) {
639 if (!StartedMetadataBlock) { 639 if (!StartedMetadataBlock) {
640 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 640 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3);
641 StartedMetadataBlock = true; 641 StartedMetadataBlock = true;
642 } 642 }
643 WriteMDNode(N, VE, Stream, Record); 643 WriteMDNode(N, VE, Stream, Record);
644 } 644 }
645 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { 645 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
646 if (!StartedMetadataBlock) { 646 if (!StartedMetadataBlock) {
647 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 647 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3);
648 648
649 // Abbrev for METADATA_STRING. 649 // Abbrev for METADATA_STRING.
650 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 650 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
651 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); 651 Abbv->Add(BitCodeAbbrevOp(naclbitc::METADATA_STRING));
652 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 652 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
653 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 653 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
654 MDSAbbrev = Stream.EmitAbbrev(Abbv); 654 MDSAbbrev = Stream.EmitAbbrev(Abbv);
655 StartedMetadataBlock = true; 655 StartedMetadataBlock = true;
656 } 656 }
657 657
658 // Code: [strchar x N] 658 // Code: [strchar x N]
659 Record.append(MDS->begin(), MDS->end()); 659 Record.append(MDS->begin(), MDS->end());
660 660
661 // Emit the finished record. 661 // Emit the finished record.
662 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); 662 Stream.EmitRecord(naclbitc::METADATA_STRING, Record, MDSAbbrev);
663 Record.clear(); 663 Record.clear();
664 } 664 }
665 } 665 }
666 666
667 // Write named metadata. 667 // Write named metadata.
668 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 668 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
669 E = M->named_metadata_end(); I != E; ++I) { 669 E = M->named_metadata_end(); I != E; ++I) {
670 const NamedMDNode *NMD = I; 670 const NamedMDNode *NMD = I;
671 if (!StartedMetadataBlock) { 671 if (!StartedMetadataBlock) {
672 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 672 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3);
673 StartedMetadataBlock = true; 673 StartedMetadataBlock = true;
674 } 674 }
675 675
676 // Write name. 676 // Write name.
677 StringRef Str = NMD->getName(); 677 StringRef Str = NMD->getName();
678 for (unsigned i = 0, e = Str.size(); i != e; ++i) 678 for (unsigned i = 0, e = Str.size(); i != e; ++i)
679 Record.push_back(Str[i]); 679 Record.push_back(Str[i]);
680 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); 680 Stream.EmitRecord(naclbitc::METADATA_NAME, Record, 0/*TODO*/);
681 Record.clear(); 681 Record.clear();
682 682
683 // Write named metadata operands. 683 // Write named metadata operands.
684 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 684 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
685 Record.push_back(VE.getValueID(NMD->getOperand(i))); 685 Record.push_back(VE.getValueID(NMD->getOperand(i)));
686 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); 686 Stream.EmitRecord(naclbitc::METADATA_NAMED_NODE, Record, 0);
687 Record.clear(); 687 Record.clear();
688 } 688 }
689 689
690 if (StartedMetadataBlock) 690 if (StartedMetadataBlock)
691 Stream.ExitBlock(); 691 Stream.ExitBlock();
692 } 692 }
693 693
694 static void WriteFunctionLocalMetadata(const Function &F, 694 static void WriteFunctionLocalMetadata(const Function &F,
695 const NaClValueEnumerator &VE, 695 const NaClValueEnumerator &VE,
696 NaClBitstreamWriter &Stream) { 696 NaClBitstreamWriter &Stream) {
697 bool StartedMetadataBlock = false; 697 bool StartedMetadataBlock = false;
698 SmallVector<uint64_t, 64> Record; 698 SmallVector<uint64_t, 64> Record;
699 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); 699 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues();
700 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 700 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
701 if (const MDNode *N = Vals[i]) 701 if (const MDNode *N = Vals[i])
702 if (N->isFunctionLocal() && N->getFunction() == &F) { 702 if (N->isFunctionLocal() && N->getFunction() == &F) {
703 if (!StartedMetadataBlock) { 703 if (!StartedMetadataBlock) {
704 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 704 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3);
705 StartedMetadataBlock = true; 705 StartedMetadataBlock = true;
706 } 706 }
707 WriteMDNode(N, VE, Stream, Record); 707 WriteMDNode(N, VE, Stream, Record);
708 } 708 }
709 709
710 if (StartedMetadataBlock) 710 if (StartedMetadataBlock)
711 Stream.ExitBlock(); 711 Stream.ExitBlock();
712 } 712 }
713 713
714 static void WriteMetadataAttachment(const Function &F, 714 static void WriteMetadataAttachment(const Function &F,
715 const NaClValueEnumerator &VE, 715 const NaClValueEnumerator &VE,
716 NaClBitstreamWriter &Stream) { 716 NaClBitstreamWriter &Stream) {
717 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); 717 Stream.EnterSubblock(naclbitc::METADATA_ATTACHMENT_ID, 3);
718 718
719 SmallVector<uint64_t, 64> Record; 719 SmallVector<uint64_t, 64> Record;
720 720
721 // Write metadata attachments 721 // Write metadata attachments
722 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 722 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
723 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; 723 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
724 724
725 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 725 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
726 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 726 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
727 I != E; ++I) { 727 I != E; ++I) {
728 MDs.clear(); 728 MDs.clear();
729 I->getAllMetadataOtherThanDebugLoc(MDs); 729 I->getAllMetadataOtherThanDebugLoc(MDs);
730 730
731 // If no metadata, ignore instruction. 731 // If no metadata, ignore instruction.
732 if (MDs.empty()) continue; 732 if (MDs.empty()) continue;
733 733
734 Record.push_back(VE.getInstructionID(I)); 734 Record.push_back(VE.getInstructionID(I));
735 735
736 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 736 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
737 Record.push_back(MDs[i].first); 737 Record.push_back(MDs[i].first);
738 Record.push_back(VE.getValueID(MDs[i].second)); 738 Record.push_back(VE.getValueID(MDs[i].second));
739 } 739 }
740 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 740 Stream.EmitRecord(naclbitc::METADATA_ATTACHMENT, Record, 0);
741 Record.clear(); 741 Record.clear();
742 } 742 }
743 743
744 Stream.ExitBlock(); 744 Stream.ExitBlock();
745 } 745 }
746 746
747 static void WriteModuleMetadataStore(const Module *M, NaClBitstreamWriter &Strea m) { 747 static void WriteModuleMetadataStore(const Module *M, NaClBitstreamWriter &Strea m) {
748 SmallVector<uint64_t, 64> Record; 748 SmallVector<uint64_t, 64> Record;
749 749
750 // Write metadata kinds 750 // Write metadata kinds
751 // METADATA_KIND - [n x [id, name]] 751 // METADATA_KIND - [n x [id, name]]
752 SmallVector<StringRef, 8> Names; 752 SmallVector<StringRef, 8> Names;
753 M->getMDKindNames(Names); 753 M->getMDKindNames(Names);
754 754
755 if (Names.empty()) return; 755 if (Names.empty()) return;
756 756
757 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 757 Stream.EnterSubblock(naclbitc::METADATA_BLOCK_ID, 3);
758 758
759 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 759 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
760 Record.push_back(MDKindID); 760 Record.push_back(MDKindID);
761 StringRef KName = Names[MDKindID]; 761 StringRef KName = Names[MDKindID];
762 Record.append(KName.begin(), KName.end()); 762 Record.append(KName.begin(), KName.end());
763 763
764 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); 764 Stream.EmitRecord(naclbitc::METADATA_KIND, Record, 0);
765 Record.clear(); 765 Record.clear();
766 } 766 }
767 767
768 Stream.ExitBlock(); 768 Stream.ExitBlock();
769 } 769 }
770 770
771 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { 771 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
772 if ((int64_t)V >= 0) 772 if ((int64_t)V >= 0)
773 Vals.push_back(V << 1); 773 Vals.push_back(V << 1);
774 else 774 else
775 Vals.push_back((-V << 1) | 1); 775 Vals.push_back((-V << 1) | 1);
776 } 776 }
777 777
778 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals, 778 static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals,
779 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val, 779 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val,
780 bool EmitSizeForWideNumbers = false 780 bool EmitSizeForWideNumbers = false
781 ) { 781 ) {
782 if (Val.getBitWidth() <= 64) { 782 if (Val.getBitWidth() <= 64) {
783 uint64_t V = Val.getSExtValue(); 783 uint64_t V = Val.getSExtValue();
784 emitSignedInt64(Vals, V); 784 emitSignedInt64(Vals, V);
785 Code = bitc::CST_CODE_INTEGER; 785 Code = naclbitc::CST_CODE_INTEGER;
786 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 786 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
787 } else { 787 } else {
788 // Wide integers, > 64 bits in size. 788 // Wide integers, > 64 bits in size.
789 // We have an arbitrary precision integer value to write whose 789 // We have an arbitrary precision integer value to write whose
790 // bit width is > 64. However, in canonical unsigned integer 790 // bit width is > 64. However, in canonical unsigned integer
791 // format it is likely that the high bits are going to be zero. 791 // format it is likely that the high bits are going to be zero.
792 // So, we only write the number of active words. 792 // So, we only write the number of active words.
793 unsigned NWords = Val.getActiveWords(); 793 unsigned NWords = Val.getActiveWords();
794 794
795 if (EmitSizeForWideNumbers) 795 if (EmitSizeForWideNumbers)
796 Vals.push_back(NWords); 796 Vals.push_back(NWords);
797 797
798 const uint64_t *RawWords = Val.getRawData(); 798 const uint64_t *RawWords = Val.getRawData();
799 for (unsigned i = 0; i != NWords; ++i) { 799 for (unsigned i = 0; i != NWords; ++i) {
800 emitSignedInt64(Vals, RawWords[i]); 800 emitSignedInt64(Vals, RawWords[i]);
801 } 801 }
802 Code = bitc::CST_CODE_WIDE_INTEGER; 802 Code = naclbitc::CST_CODE_WIDE_INTEGER;
803 } 803 }
804 } 804 }
805 805
806 static void WriteConstants(unsigned FirstVal, unsigned LastVal, 806 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
807 const NaClValueEnumerator &VE, 807 const NaClValueEnumerator &VE,
808 NaClBitstreamWriter &Stream, bool isGlobal) { 808 NaClBitstreamWriter &Stream, bool isGlobal) {
809 if (FirstVal == LastVal) return; 809 if (FirstVal == LastVal) return;
810 810
811 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 811 Stream.EnterSubblock(naclbitc::CONSTANTS_BLOCK_ID, 4);
812 812
813 unsigned AggregateAbbrev = 0; 813 unsigned AggregateAbbrev = 0;
814 unsigned String8Abbrev = 0; 814 unsigned String8Abbrev = 0;
815 unsigned CString7Abbrev = 0; 815 unsigned CString7Abbrev = 0;
816 unsigned CString6Abbrev = 0; 816 unsigned CString6Abbrev = 0;
817 // If this is a constant pool for the module, emit module-specific abbrevs. 817 // If this is a constant pool for the module, emit module-specific abbrevs.
818 if (isGlobal) { 818 if (isGlobal) {
819 // Abbrev for CST_CODE_AGGREGATE. 819 // Abbrev for CST_CODE_AGGREGATE.
820 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 820 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
821 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 821 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_AGGREGATE));
822 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 822 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
823 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 823 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
824 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 824 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
825 825
826 // Abbrev for CST_CODE_STRING. 826 // Abbrev for CST_CODE_STRING.
827 Abbv = new BitCodeAbbrev(); 827 Abbv = new BitCodeAbbrev();
828 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 828 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_STRING));
829 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 829 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
830 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 830 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
831 String8Abbrev = Stream.EmitAbbrev(Abbv); 831 String8Abbrev = Stream.EmitAbbrev(Abbv);
832 // Abbrev for CST_CODE_CSTRING. 832 // Abbrev for CST_CODE_CSTRING.
833 Abbv = new BitCodeAbbrev(); 833 Abbv = new BitCodeAbbrev();
834 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 834 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING));
835 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 835 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
836 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 836 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
837 CString7Abbrev = Stream.EmitAbbrev(Abbv); 837 CString7Abbrev = Stream.EmitAbbrev(Abbv);
838 // Abbrev for CST_CODE_CSTRING. 838 // Abbrev for CST_CODE_CSTRING.
839 Abbv = new BitCodeAbbrev(); 839 Abbv = new BitCodeAbbrev();
840 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 840 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_CSTRING));
841 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 841 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
842 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 842 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
843 CString6Abbrev = Stream.EmitAbbrev(Abbv); 843 CString6Abbrev = Stream.EmitAbbrev(Abbv);
844 } 844 }
845 845
846 SmallVector<uint64_t, 64> Record; 846 SmallVector<uint64_t, 64> Record;
847 847
848 const NaClValueEnumerator::ValueList &Vals = VE.getValues(); 848 const NaClValueEnumerator::ValueList &Vals = VE.getValues();
849 Type *LastTy = 0; 849 Type *LastTy = 0;
850 for (unsigned i = FirstVal; i != LastVal; ++i) { 850 for (unsigned i = FirstVal; i != LastVal; ++i) {
851 const Value *V = Vals[i].first; 851 const Value *V = Vals[i].first;
852 // If we need to switch types, do so now. 852 // If we need to switch types, do so now.
853 if (V->getType() != LastTy) { 853 if (V->getType() != LastTy) {
854 LastTy = V->getType(); 854 LastTy = V->getType();
855 Record.push_back(VE.getTypeID(LastTy)); 855 Record.push_back(VE.getTypeID(LastTy));
856 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 856 Stream.EmitRecord(naclbitc::CST_CODE_SETTYPE, Record,
857 CONSTANTS_SETTYPE_ABBREV); 857 CONSTANTS_SETTYPE_ABBREV);
858 Record.clear(); 858 Record.clear();
859 } 859 }
860 860
861 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 861 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
862 Record.push_back(unsigned(IA->hasSideEffects()) | 862 Record.push_back(unsigned(IA->hasSideEffects()) |
863 unsigned(IA->isAlignStack()) << 1 | 863 unsigned(IA->isAlignStack()) << 1 |
864 unsigned(IA->getDialect()&1) << 2); 864 unsigned(IA->getDialect()&1) << 2);
865 865
866 // Add the asm string. 866 // Add the asm string.
867 const std::string &AsmStr = IA->getAsmString(); 867 const std::string &AsmStr = IA->getAsmString();
868 Record.push_back(AsmStr.size()); 868 Record.push_back(AsmStr.size());
869 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) 869 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
870 Record.push_back(AsmStr[i]); 870 Record.push_back(AsmStr[i]);
871 871
872 // Add the constraint string. 872 // Add the constraint string.
873 const std::string &ConstraintStr = IA->getConstraintString(); 873 const std::string &ConstraintStr = IA->getConstraintString();
874 Record.push_back(ConstraintStr.size()); 874 Record.push_back(ConstraintStr.size());
875 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) 875 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
876 Record.push_back(ConstraintStr[i]); 876 Record.push_back(ConstraintStr[i]);
877 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 877 Stream.EmitRecord(naclbitc::CST_CODE_INLINEASM, Record);
878 Record.clear(); 878 Record.clear();
879 continue; 879 continue;
880 } 880 }
881 const Constant *C = cast<Constant>(V); 881 const Constant *C = cast<Constant>(V);
882 unsigned Code = -1U; 882 unsigned Code = -1U;
883 unsigned AbbrevToUse = 0; 883 unsigned AbbrevToUse = 0;
884 if (C->isNullValue()) { 884 if (C->isNullValue()) {
885 Code = bitc::CST_CODE_NULL; 885 Code = naclbitc::CST_CODE_NULL;
886 } else if (isa<UndefValue>(C)) { 886 } else if (isa<UndefValue>(C)) {
887 Code = bitc::CST_CODE_UNDEF; 887 Code = naclbitc::CST_CODE_UNDEF;
888 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 888 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
889 EmitAPInt(Record, Code, AbbrevToUse, IV->getValue()); 889 EmitAPInt(Record, Code, AbbrevToUse, IV->getValue());
890 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 890 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
891 Code = bitc::CST_CODE_FLOAT; 891 Code = naclbitc::CST_CODE_FLOAT;
892 Type *Ty = CFP->getType(); 892 Type *Ty = CFP->getType();
893 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) { 893 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
894 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 894 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
895 } else if (Ty->isX86_FP80Ty()) { 895 } else if (Ty->isX86_FP80Ty()) {
896 // api needed to prevent premature destruction 896 // api needed to prevent premature destruction
897 // bits are not in the same order as a normal i80 APInt, compensate. 897 // bits are not in the same order as a normal i80 APInt, compensate.
898 APInt api = CFP->getValueAPF().bitcastToAPInt(); 898 APInt api = CFP->getValueAPF().bitcastToAPInt();
899 const uint64_t *p = api.getRawData(); 899 const uint64_t *p = api.getRawData();
900 Record.push_back((p[1] << 48) | (p[0] >> 16)); 900 Record.push_back((p[1] << 48) | (p[0] >> 16));
901 Record.push_back(p[0] & 0xffffLL); 901 Record.push_back(p[0] & 0xffffLL);
902 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { 902 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
903 APInt api = CFP->getValueAPF().bitcastToAPInt(); 903 APInt api = CFP->getValueAPF().bitcastToAPInt();
904 const uint64_t *p = api.getRawData(); 904 const uint64_t *p = api.getRawData();
905 Record.push_back(p[0]); 905 Record.push_back(p[0]);
906 Record.push_back(p[1]); 906 Record.push_back(p[1]);
907 } else { 907 } else {
908 assert (0 && "Unknown FP type!"); 908 assert (0 && "Unknown FP type!");
909 } 909 }
910 } else if (isa<ConstantDataSequential>(C) && 910 } else if (isa<ConstantDataSequential>(C) &&
911 cast<ConstantDataSequential>(C)->isString()) { 911 cast<ConstantDataSequential>(C)->isString()) {
912 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); 912 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
913 // Emit constant strings specially. 913 // Emit constant strings specially.
914 unsigned NumElts = Str->getNumElements(); 914 unsigned NumElts = Str->getNumElements();
915 // If this is a null-terminated string, use the denser CSTRING encoding. 915 // If this is a null-terminated string, use the denser CSTRING encoding.
916 if (Str->isCString()) { 916 if (Str->isCString()) {
917 Code = bitc::CST_CODE_CSTRING; 917 Code = naclbitc::CST_CODE_CSTRING;
918 --NumElts; // Don't encode the null, which isn't allowed by char6. 918 --NumElts; // Don't encode the null, which isn't allowed by char6.
919 } else { 919 } else {
920 Code = bitc::CST_CODE_STRING; 920 Code = naclbitc::CST_CODE_STRING;
921 AbbrevToUse = String8Abbrev; 921 AbbrevToUse = String8Abbrev;
922 } 922 }
923 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 923 bool isCStr7 = Code == naclbitc::CST_CODE_CSTRING;
924 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 924 bool isCStrChar6 = Code == naclbitc::CST_CODE_CSTRING;
925 for (unsigned i = 0; i != NumElts; ++i) { 925 for (unsigned i = 0; i != NumElts; ++i) {
926 unsigned char V = Str->getElementAsInteger(i); 926 unsigned char V = Str->getElementAsInteger(i);
927 Record.push_back(V); 927 Record.push_back(V);
928 isCStr7 &= (V & 128) == 0; 928 isCStr7 &= (V & 128) == 0;
929 if (isCStrChar6) 929 if (isCStrChar6)
930 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 930 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
931 } 931 }
932 932
933 if (isCStrChar6) 933 if (isCStrChar6)
934 AbbrevToUse = CString6Abbrev; 934 AbbrevToUse = CString6Abbrev;
935 else if (isCStr7) 935 else if (isCStr7)
936 AbbrevToUse = CString7Abbrev; 936 AbbrevToUse = CString7Abbrev;
937 } else if (const ConstantDataSequential *CDS = 937 } else if (const ConstantDataSequential *CDS =
938 dyn_cast<ConstantDataSequential>(C)) { 938 dyn_cast<ConstantDataSequential>(C)) {
939 Code = bitc::CST_CODE_DATA; 939 Code = naclbitc::CST_CODE_DATA;
940 Type *EltTy = CDS->getType()->getElementType(); 940 Type *EltTy = CDS->getType()->getElementType();
941 if (isa<IntegerType>(EltTy)) { 941 if (isa<IntegerType>(EltTy)) {
942 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) 942 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
943 Record.push_back(CDS->getElementAsInteger(i)); 943 Record.push_back(CDS->getElementAsInteger(i));
944 } else if (EltTy->isFloatTy()) { 944 } else if (EltTy->isFloatTy()) {
945 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 945 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
946 union { float F; uint32_t I; }; 946 union { float F; uint32_t I; };
947 F = CDS->getElementAsFloat(i); 947 F = CDS->getElementAsFloat(i);
948 Record.push_back(I); 948 Record.push_back(I);
949 } 949 }
950 } else { 950 } else {
951 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type"); 951 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
952 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 952 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
953 union { double F; uint64_t I; }; 953 union { double F; uint64_t I; };
954 F = CDS->getElementAsDouble(i); 954 F = CDS->getElementAsDouble(i);
955 Record.push_back(I); 955 Record.push_back(I);
956 } 956 }
957 } 957 }
958 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || 958 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
959 isa<ConstantVector>(C)) { 959 isa<ConstantVector>(C)) {
960 Code = bitc::CST_CODE_AGGREGATE; 960 Code = naclbitc::CST_CODE_AGGREGATE;
961 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 961 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
962 Record.push_back(VE.getValueID(C->getOperand(i))); 962 Record.push_back(VE.getValueID(C->getOperand(i)));
963 AbbrevToUse = AggregateAbbrev; 963 AbbrevToUse = AggregateAbbrev;
964 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 964 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
965 switch (CE->getOpcode()) { 965 switch (CE->getOpcode()) {
966 default: 966 default:
967 if (Instruction::isCast(CE->getOpcode())) { 967 if (Instruction::isCast(CE->getOpcode())) {
968 Code = bitc::CST_CODE_CE_CAST; 968 Code = naclbitc::CST_CODE_CE_CAST;
969 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 969 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
970 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 970 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
971 Record.push_back(VE.getValueID(C->getOperand(0))); 971 Record.push_back(VE.getValueID(C->getOperand(0)));
972 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 972 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
973 } else { 973 } else {
974 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 974 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
975 Code = bitc::CST_CODE_CE_BINOP; 975 Code = naclbitc::CST_CODE_CE_BINOP;
976 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 976 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
977 Record.push_back(VE.getValueID(C->getOperand(0))); 977 Record.push_back(VE.getValueID(C->getOperand(0)));
978 Record.push_back(VE.getValueID(C->getOperand(1))); 978 Record.push_back(VE.getValueID(C->getOperand(1)));
979 uint64_t Flags = GetOptimizationFlags(CE); 979 uint64_t Flags = GetOptimizationFlags(CE);
980 if (Flags != 0) 980 if (Flags != 0)
981 Record.push_back(Flags); 981 Record.push_back(Flags);
982 } 982 }
983 break; 983 break;
984 case Instruction::GetElementPtr: 984 case Instruction::GetElementPtr:
985 Code = bitc::CST_CODE_CE_GEP; 985 Code = naclbitc::CST_CODE_CE_GEP;
986 if (cast<GEPOperator>(C)->isInBounds()) 986 if (cast<GEPOperator>(C)->isInBounds())
987 Code = bitc::CST_CODE_CE_INBOUNDS_GEP; 987 Code = naclbitc::CST_CODE_CE_INBOUNDS_GEP;
988 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 988 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
989 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 989 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
990 Record.push_back(VE.getValueID(C->getOperand(i))); 990 Record.push_back(VE.getValueID(C->getOperand(i)));
991 } 991 }
992 break; 992 break;
993 case Instruction::Select: 993 case Instruction::Select:
994 Code = bitc::CST_CODE_CE_SELECT; 994 Code = naclbitc::CST_CODE_CE_SELECT;
995 Record.push_back(VE.getValueID(C->getOperand(0))); 995 Record.push_back(VE.getValueID(C->getOperand(0)));
996 Record.push_back(VE.getValueID(C->getOperand(1))); 996 Record.push_back(VE.getValueID(C->getOperand(1)));
997 Record.push_back(VE.getValueID(C->getOperand(2))); 997 Record.push_back(VE.getValueID(C->getOperand(2)));
998 break; 998 break;
999 case Instruction::ExtractElement: 999 case Instruction::ExtractElement:
1000 Code = bitc::CST_CODE_CE_EXTRACTELT; 1000 Code = naclbitc::CST_CODE_CE_EXTRACTELT;
1001 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 1001 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1002 Record.push_back(VE.getValueID(C->getOperand(0))); 1002 Record.push_back(VE.getValueID(C->getOperand(0)));
1003 Record.push_back(VE.getValueID(C->getOperand(1))); 1003 Record.push_back(VE.getValueID(C->getOperand(1)));
1004 break; 1004 break;
1005 case Instruction::InsertElement: 1005 case Instruction::InsertElement:
1006 Code = bitc::CST_CODE_CE_INSERTELT; 1006 Code = naclbitc::CST_CODE_CE_INSERTELT;
1007 Record.push_back(VE.getValueID(C->getOperand(0))); 1007 Record.push_back(VE.getValueID(C->getOperand(0)));
1008 Record.push_back(VE.getValueID(C->getOperand(1))); 1008 Record.push_back(VE.getValueID(C->getOperand(1)));
1009 Record.push_back(VE.getValueID(C->getOperand(2))); 1009 Record.push_back(VE.getValueID(C->getOperand(2)));
1010 break; 1010 break;
1011 case Instruction::ShuffleVector: 1011 case Instruction::ShuffleVector:
1012 // If the return type and argument types are the same, this is a 1012 // If the return type and argument types are the same, this is a
1013 // standard shufflevector instruction. If the types are different, 1013 // standard shufflevector instruction. If the types are different,
1014 // then the shuffle is widening or truncating the input vectors, and 1014 // then the shuffle is widening or truncating the input vectors, and
1015 // the argument type must also be encoded. 1015 // the argument type must also be encoded.
1016 if (C->getType() == C->getOperand(0)->getType()) { 1016 if (C->getType() == C->getOperand(0)->getType()) {
1017 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 1017 Code = naclbitc::CST_CODE_CE_SHUFFLEVEC;
1018 } else { 1018 } else {
1019 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 1019 Code = naclbitc::CST_CODE_CE_SHUFVEC_EX;
1020 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 1020 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1021 } 1021 }
1022 Record.push_back(VE.getValueID(C->getOperand(0))); 1022 Record.push_back(VE.getValueID(C->getOperand(0)));
1023 Record.push_back(VE.getValueID(C->getOperand(1))); 1023 Record.push_back(VE.getValueID(C->getOperand(1)));
1024 Record.push_back(VE.getValueID(C->getOperand(2))); 1024 Record.push_back(VE.getValueID(C->getOperand(2)));
1025 break; 1025 break;
1026 case Instruction::ICmp: 1026 case Instruction::ICmp:
1027 case Instruction::FCmp: 1027 case Instruction::FCmp:
1028 Code = bitc::CST_CODE_CE_CMP; 1028 Code = naclbitc::CST_CODE_CE_CMP;
1029 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 1029 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1030 Record.push_back(VE.getValueID(C->getOperand(0))); 1030 Record.push_back(VE.getValueID(C->getOperand(0)));
1031 Record.push_back(VE.getValueID(C->getOperand(1))); 1031 Record.push_back(VE.getValueID(C->getOperand(1)));
1032 Record.push_back(CE->getPredicate()); 1032 Record.push_back(CE->getPredicate());
1033 break; 1033 break;
1034 } 1034 }
1035 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 1035 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
1036 Code = bitc::CST_CODE_BLOCKADDRESS; 1036 Code = naclbitc::CST_CODE_BLOCKADDRESS;
1037 Record.push_back(VE.getTypeID(BA->getFunction()->getType())); 1037 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
1038 Record.push_back(VE.getValueID(BA->getFunction())); 1038 Record.push_back(VE.getValueID(BA->getFunction()));
1039 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); 1039 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
1040 } else { 1040 } else {
1041 #ifndef NDEBUG 1041 #ifndef NDEBUG
1042 C->dump(); 1042 C->dump();
1043 #endif 1043 #endif
1044 llvm_unreachable("Unknown constant!"); 1044 llvm_unreachable("Unknown constant!");
1045 } 1045 }
1046 Stream.EmitRecord(Code, Record, AbbrevToUse); 1046 Stream.EmitRecord(Code, Record, AbbrevToUse);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1112 /// WriteInstruction - Emit an instruction to the specified stream. 1112 /// WriteInstruction - Emit an instruction to the specified stream.
1113 static void WriteInstruction(const Instruction &I, unsigned InstID, 1113 static void WriteInstruction(const Instruction &I, unsigned InstID,
1114 NaClValueEnumerator &VE, NaClBitstreamWriter &Strea m, 1114 NaClValueEnumerator &VE, NaClBitstreamWriter &Strea m,
1115 SmallVector<unsigned, 64> &Vals) { 1115 SmallVector<unsigned, 64> &Vals) {
1116 unsigned Code = 0; 1116 unsigned Code = 0;
1117 unsigned AbbrevToUse = 0; 1117 unsigned AbbrevToUse = 0;
1118 VE.setInstructionID(&I); 1118 VE.setInstructionID(&I);
1119 switch (I.getOpcode()) { 1119 switch (I.getOpcode()) {
1120 default: 1120 default:
1121 if (Instruction::isCast(I.getOpcode())) { 1121 if (Instruction::isCast(I.getOpcode())) {
1122 Code = bitc::FUNC_CODE_INST_CAST; 1122 Code = naclbitc::FUNC_CODE_INST_CAST;
1123 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1123 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1124 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 1124 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
1125 Vals.push_back(VE.getTypeID(I.getType())); 1125 Vals.push_back(VE.getTypeID(I.getType()));
1126 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 1126 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
1127 } else { 1127 } else {
1128 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 1128 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
1129 Code = bitc::FUNC_CODE_INST_BINOP; 1129 Code = naclbitc::FUNC_CODE_INST_BINOP;
1130 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1130 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1131 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 1131 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
1132 pushValue(I.getOperand(1), InstID, Vals, VE); 1132 pushValue(I.getOperand(1), InstID, Vals, VE);
1133 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 1133 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
1134 uint64_t Flags = GetOptimizationFlags(&I); 1134 uint64_t Flags = GetOptimizationFlags(&I);
1135 if (Flags != 0) { 1135 if (Flags != 0) {
1136 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) 1136 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
1137 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; 1137 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
1138 Vals.push_back(Flags); 1138 Vals.push_back(Flags);
1139 } 1139 }
1140 } 1140 }
1141 break; 1141 break;
1142 1142
1143 case Instruction::GetElementPtr: 1143 case Instruction::GetElementPtr:
1144 Code = bitc::FUNC_CODE_INST_GEP; 1144 Code = naclbitc::FUNC_CODE_INST_GEP;
1145 if (cast<GEPOperator>(&I)->isInBounds()) 1145 if (cast<GEPOperator>(&I)->isInBounds())
1146 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP; 1146 Code = naclbitc::FUNC_CODE_INST_INBOUNDS_GEP;
1147 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1147 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1148 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1148 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1149 break; 1149 break;
1150 case Instruction::ExtractValue: { 1150 case Instruction::ExtractValue: {
1151 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 1151 Code = naclbitc::FUNC_CODE_INST_EXTRACTVAL;
1152 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1152 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1153 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 1153 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
1154 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) 1154 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1155 Vals.push_back(*i); 1155 Vals.push_back(*i);
1156 break; 1156 break;
1157 } 1157 }
1158 case Instruction::InsertValue: { 1158 case Instruction::InsertValue: {
1159 Code = bitc::FUNC_CODE_INST_INSERTVAL; 1159 Code = naclbitc::FUNC_CODE_INST_INSERTVAL;
1160 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1160 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1161 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1161 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1162 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 1162 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
1163 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) 1163 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1164 Vals.push_back(*i); 1164 Vals.push_back(*i);
1165 break; 1165 break;
1166 } 1166 }
1167 case Instruction::Select: 1167 case Instruction::Select:
1168 Code = bitc::FUNC_CODE_INST_VSELECT; 1168 Code = naclbitc::FUNC_CODE_INST_VSELECT;
1169 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1169 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1170 pushValue(I.getOperand(2), InstID, Vals, VE); 1170 pushValue(I.getOperand(2), InstID, Vals, VE);
1171 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1171 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1172 break; 1172 break;
1173 case Instruction::ExtractElement: 1173 case Instruction::ExtractElement:
1174 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 1174 Code = naclbitc::FUNC_CODE_INST_EXTRACTELT;
1175 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1175 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1176 pushValue(I.getOperand(1), InstID, Vals, VE); 1176 pushValue(I.getOperand(1), InstID, Vals, VE);
1177 break; 1177 break;
1178 case Instruction::InsertElement: 1178 case Instruction::InsertElement:
1179 Code = bitc::FUNC_CODE_INST_INSERTELT; 1179 Code = naclbitc::FUNC_CODE_INST_INSERTELT;
1180 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1180 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1181 pushValue(I.getOperand(1), InstID, Vals, VE); 1181 pushValue(I.getOperand(1), InstID, Vals, VE);
1182 pushValue(I.getOperand(2), InstID, Vals, VE); 1182 pushValue(I.getOperand(2), InstID, Vals, VE);
1183 break; 1183 break;
1184 case Instruction::ShuffleVector: 1184 case Instruction::ShuffleVector:
1185 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 1185 Code = naclbitc::FUNC_CODE_INST_SHUFFLEVEC;
1186 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1186 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1187 pushValue(I.getOperand(1), InstID, Vals, VE); 1187 pushValue(I.getOperand(1), InstID, Vals, VE);
1188 pushValue(I.getOperand(2), InstID, Vals, VE); 1188 pushValue(I.getOperand(2), InstID, Vals, VE);
1189 break; 1189 break;
1190 case Instruction::ICmp: 1190 case Instruction::ICmp:
1191 case Instruction::FCmp: 1191 case Instruction::FCmp:
1192 // compare returning Int1Ty or vector of Int1Ty 1192 // compare returning Int1Ty or vector of Int1Ty
1193 Code = bitc::FUNC_CODE_INST_CMP2; 1193 Code = naclbitc::FUNC_CODE_INST_CMP2;
1194 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1194 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1195 pushValue(I.getOperand(1), InstID, Vals, VE); 1195 pushValue(I.getOperand(1), InstID, Vals, VE);
1196 Vals.push_back(cast<CmpInst>(I).getPredicate()); 1196 Vals.push_back(cast<CmpInst>(I).getPredicate());
1197 break; 1197 break;
1198 1198
1199 case Instruction::Ret: 1199 case Instruction::Ret:
1200 { 1200 {
1201 Code = bitc::FUNC_CODE_INST_RET; 1201 Code = naclbitc::FUNC_CODE_INST_RET;
1202 unsigned NumOperands = I.getNumOperands(); 1202 unsigned NumOperands = I.getNumOperands();
1203 if (NumOperands == 0) 1203 if (NumOperands == 0)
1204 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 1204 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1205 else if (NumOperands == 1) { 1205 else if (NumOperands == 1) {
1206 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1206 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1207 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 1207 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1208 } else { 1208 } else {
1209 for (unsigned i = 0, e = NumOperands; i != e; ++i) 1209 for (unsigned i = 0, e = NumOperands; i != e; ++i)
1210 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1210 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1211 } 1211 }
1212 } 1212 }
1213 break; 1213 break;
1214 case Instruction::Br: 1214 case Instruction::Br:
1215 { 1215 {
1216 Code = bitc::FUNC_CODE_INST_BR; 1216 Code = naclbitc::FUNC_CODE_INST_BR;
1217 const BranchInst &II = cast<BranchInst>(I); 1217 const BranchInst &II = cast<BranchInst>(I);
1218 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 1218 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1219 if (II.isConditional()) { 1219 if (II.isConditional()) {
1220 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 1220 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1221 pushValue(II.getCondition(), InstID, Vals, VE); 1221 pushValue(II.getCondition(), InstID, Vals, VE);
1222 } 1222 }
1223 } 1223 }
1224 break; 1224 break;
1225 case Instruction::Switch: 1225 case Instruction::Switch:
1226 { 1226 {
1227 // Redefine Vals, since here we need to use 64 bit values 1227 // Redefine Vals, since here we need to use 64 bit values
1228 // explicitly to store large APInt numbers. 1228 // explicitly to store large APInt numbers.
1229 SmallVector<uint64_t, 128> Vals64; 1229 SmallVector<uint64_t, 128> Vals64;
1230 1230
1231 Code = bitc::FUNC_CODE_INST_SWITCH; 1231 Code = naclbitc::FUNC_CODE_INST_SWITCH;
1232 const SwitchInst &SI = cast<SwitchInst>(I); 1232 const SwitchInst &SI = cast<SwitchInst>(I);
1233 1233
1234 uint32_t SwitchRecordHeader = SI.hash() | (SWITCH_INST_MAGIC << 16); 1234 uint32_t SwitchRecordHeader = SI.hash() | (SWITCH_INST_MAGIC << 16);
1235 Vals64.push_back(SwitchRecordHeader); 1235 Vals64.push_back(SwitchRecordHeader);
1236 1236
1237 Vals64.push_back(VE.getTypeID(SI.getCondition()->getType())); 1237 Vals64.push_back(VE.getTypeID(SI.getCondition()->getType()));
1238 pushValue64(SI.getCondition(), InstID, Vals64, VE); 1238 pushValue64(SI.getCondition(), InstID, Vals64, VE);
1239 Vals64.push_back(VE.getValueID(SI.getDefaultDest())); 1239 Vals64.push_back(VE.getValueID(SI.getDefaultDest()));
1240 Vals64.push_back(SI.getNumCases()); 1240 Vals64.push_back(SI.getNumCases());
1241 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); 1241 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1277 } 1277 }
1278 1278
1279 Stream.EmitRecord(Code, Vals64, AbbrevToUse); 1279 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
1280 1280
1281 // Also do expected action - clear external Vals collection: 1281 // Also do expected action - clear external Vals collection:
1282 Vals.clear(); 1282 Vals.clear();
1283 return; 1283 return;
1284 } 1284 }
1285 break; 1285 break;
1286 case Instruction::IndirectBr: 1286 case Instruction::IndirectBr:
1287 Code = bitc::FUNC_CODE_INST_INDIRECTBR; 1287 Code = naclbitc::FUNC_CODE_INST_INDIRECTBR;
1288 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1288 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1289 // Encode the address operand as relative, but not the basic blocks. 1289 // Encode the address operand as relative, but not the basic blocks.
1290 pushValue(I.getOperand(0), InstID, Vals, VE); 1290 pushValue(I.getOperand(0), InstID, Vals, VE);
1291 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) 1291 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
1292 Vals.push_back(VE.getValueID(I.getOperand(i))); 1292 Vals.push_back(VE.getValueID(I.getOperand(i)));
1293 break; 1293 break;
1294 1294
1295 case Instruction::Invoke: { 1295 case Instruction::Invoke: {
1296 const InvokeInst *II = cast<InvokeInst>(&I); 1296 const InvokeInst *II = cast<InvokeInst>(&I);
1297 const Value *Callee(II->getCalledValue()); 1297 const Value *Callee(II->getCalledValue());
1298 PointerType *PTy = cast<PointerType>(Callee->getType()); 1298 PointerType *PTy = cast<PointerType>(Callee->getType());
1299 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1299 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1300 Code = bitc::FUNC_CODE_INST_INVOKE; 1300 Code = naclbitc::FUNC_CODE_INST_INVOKE;
1301 1301
1302 Vals.push_back(VE.getAttributeID(II->getAttributes())); 1302 Vals.push_back(VE.getAttributeID(II->getAttributes()));
1303 Vals.push_back(II->getCallingConv()); 1303 Vals.push_back(II->getCallingConv());
1304 Vals.push_back(VE.getValueID(II->getNormalDest())); 1304 Vals.push_back(VE.getValueID(II->getNormalDest()));
1305 Vals.push_back(VE.getValueID(II->getUnwindDest())); 1305 Vals.push_back(VE.getValueID(II->getUnwindDest()));
1306 PushValueAndType(Callee, InstID, Vals, VE); 1306 PushValueAndType(Callee, InstID, Vals, VE);
1307 1307
1308 // Emit value #'s for the fixed parameters. 1308 // Emit value #'s for the fixed parameters.
1309 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 1309 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1310 pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param. 1310 pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param.
1311 1311
1312 // Emit type/value pairs for varargs params. 1312 // Emit type/value pairs for varargs params.
1313 if (FTy->isVarArg()) { 1313 if (FTy->isVarArg()) {
1314 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; 1314 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
1315 i != e; ++i) 1315 i != e; ++i)
1316 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 1316 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
1317 } 1317 }
1318 break; 1318 break;
1319 } 1319 }
1320 case Instruction::Resume: 1320 case Instruction::Resume:
1321 Code = bitc::FUNC_CODE_INST_RESUME; 1321 Code = naclbitc::FUNC_CODE_INST_RESUME;
1322 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1322 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1323 break; 1323 break;
1324 case Instruction::Unreachable: 1324 case Instruction::Unreachable:
1325 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 1325 Code = naclbitc::FUNC_CODE_INST_UNREACHABLE;
1326 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 1326 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
1327 break; 1327 break;
1328 1328
1329 case Instruction::PHI: { 1329 case Instruction::PHI: {
1330 const PHINode &PN = cast<PHINode>(I); 1330 const PHINode &PN = cast<PHINode>(I);
1331 Code = bitc::FUNC_CODE_INST_PHI; 1331 Code = naclbitc::FUNC_CODE_INST_PHI;
1332 // With the newer instruction encoding, forward references could give 1332 // With the newer instruction encoding, forward references could give
1333 // negative valued IDs. This is most common for PHIs, so we use 1333 // negative valued IDs. This is most common for PHIs, so we use
1334 // signed VBRs. 1334 // signed VBRs.
1335 SmallVector<uint64_t, 128> Vals64; 1335 SmallVector<uint64_t, 128> Vals64;
1336 Vals64.push_back(VE.getTypeID(PN.getType())); 1336 Vals64.push_back(VE.getTypeID(PN.getType()));
1337 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 1337 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1338 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE); 1338 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
1339 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i))); 1339 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
1340 } 1340 }
1341 // Emit a Vals64 vector and exit. 1341 // Emit a Vals64 vector and exit.
1342 Stream.EmitRecord(Code, Vals64, AbbrevToUse); 1342 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
1343 Vals64.clear(); 1343 Vals64.clear();
1344 return; 1344 return;
1345 } 1345 }
1346 1346
1347 case Instruction::LandingPad: { 1347 case Instruction::LandingPad: {
1348 const LandingPadInst &LP = cast<LandingPadInst>(I); 1348 const LandingPadInst &LP = cast<LandingPadInst>(I);
1349 Code = bitc::FUNC_CODE_INST_LANDINGPAD; 1349 Code = naclbitc::FUNC_CODE_INST_LANDINGPAD;
1350 Vals.push_back(VE.getTypeID(LP.getType())); 1350 Vals.push_back(VE.getTypeID(LP.getType()));
1351 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE); 1351 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
1352 Vals.push_back(LP.isCleanup()); 1352 Vals.push_back(LP.isCleanup());
1353 Vals.push_back(LP.getNumClauses()); 1353 Vals.push_back(LP.getNumClauses());
1354 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) { 1354 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
1355 if (LP.isCatch(I)) 1355 if (LP.isCatch(I))
1356 Vals.push_back(LandingPadInst::Catch); 1356 Vals.push_back(LandingPadInst::Catch);
1357 else 1357 else
1358 Vals.push_back(LandingPadInst::Filter); 1358 Vals.push_back(LandingPadInst::Filter);
1359 PushValueAndType(LP.getClause(I), InstID, Vals, VE); 1359 PushValueAndType(LP.getClause(I), InstID, Vals, VE);
1360 } 1360 }
1361 break; 1361 break;
1362 } 1362 }
1363 1363
1364 case Instruction::Alloca: 1364 case Instruction::Alloca:
1365 Code = bitc::FUNC_CODE_INST_ALLOCA; 1365 Code = naclbitc::FUNC_CODE_INST_ALLOCA;
1366 Vals.push_back(VE.getTypeID(I.getType())); 1366 Vals.push_back(VE.getTypeID(I.getType()));
1367 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1367 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1368 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 1368 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1369 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); 1369 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
1370 break; 1370 break;
1371 1371
1372 case Instruction::Load: 1372 case Instruction::Load:
1373 if (cast<LoadInst>(I).isAtomic()) { 1373 if (cast<LoadInst>(I).isAtomic()) {
1374 Code = bitc::FUNC_CODE_INST_LOADATOMIC; 1374 Code = naclbitc::FUNC_CODE_INST_LOADATOMIC;
1375 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1375 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1376 } else { 1376 } else {
1377 Code = bitc::FUNC_CODE_INST_LOAD; 1377 Code = naclbitc::FUNC_CODE_INST_LOAD;
1378 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 1378 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
1379 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 1379 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
1380 } 1380 }
1381 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 1381 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1382 Vals.push_back(cast<LoadInst>(I).isVolatile()); 1382 Vals.push_back(cast<LoadInst>(I).isVolatile());
1383 if (cast<LoadInst>(I).isAtomic()) { 1383 if (cast<LoadInst>(I).isAtomic()) {
1384 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering())); 1384 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
1385 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope())); 1385 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
1386 } 1386 }
1387 break; 1387 break;
1388 case Instruction::Store: 1388 case Instruction::Store:
1389 if (cast<StoreInst>(I).isAtomic()) 1389 if (cast<StoreInst>(I).isAtomic())
1390 Code = bitc::FUNC_CODE_INST_STOREATOMIC; 1390 Code = naclbitc::FUNC_CODE_INST_STOREATOMIC;
1391 else 1391 else
1392 Code = bitc::FUNC_CODE_INST_STORE; 1392 Code = naclbitc::FUNC_CODE_INST_STORE;
1393 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr 1393 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
1394 pushValue(I.getOperand(0), InstID, Vals, VE); // val. 1394 pushValue(I.getOperand(0), InstID, Vals, VE); // val.
1395 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 1395 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1396 Vals.push_back(cast<StoreInst>(I).isVolatile()); 1396 Vals.push_back(cast<StoreInst>(I).isVolatile());
1397 if (cast<StoreInst>(I).isAtomic()) { 1397 if (cast<StoreInst>(I).isAtomic()) {
1398 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering())); 1398 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
1399 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope())); 1399 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
1400 } 1400 }
1401 break; 1401 break;
1402 case Instruction::AtomicCmpXchg: 1402 case Instruction::AtomicCmpXchg:
1403 Code = bitc::FUNC_CODE_INST_CMPXCHG; 1403 Code = naclbitc::FUNC_CODE_INST_CMPXCHG;
1404 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 1404 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1405 pushValue(I.getOperand(1), InstID, Vals, VE); // cmp. 1405 pushValue(I.getOperand(1), InstID, Vals, VE); // cmp.
1406 pushValue(I.getOperand(2), InstID, Vals, VE); // newval. 1406 pushValue(I.getOperand(2), InstID, Vals, VE); // newval.
1407 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile()); 1407 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
1408 Vals.push_back(GetEncodedOrdering( 1408 Vals.push_back(GetEncodedOrdering(
1409 cast<AtomicCmpXchgInst>(I).getOrdering())); 1409 cast<AtomicCmpXchgInst>(I).getOrdering()));
1410 Vals.push_back(GetEncodedSynchScope( 1410 Vals.push_back(GetEncodedSynchScope(
1411 cast<AtomicCmpXchgInst>(I).getSynchScope())); 1411 cast<AtomicCmpXchgInst>(I).getSynchScope()));
1412 break; 1412 break;
1413 case Instruction::AtomicRMW: 1413 case Instruction::AtomicRMW:
1414 Code = bitc::FUNC_CODE_INST_ATOMICRMW; 1414 Code = naclbitc::FUNC_CODE_INST_ATOMICRMW;
1415 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 1415 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1416 pushValue(I.getOperand(1), InstID, Vals, VE); // val. 1416 pushValue(I.getOperand(1), InstID, Vals, VE); // val.
1417 Vals.push_back(GetEncodedRMWOperation( 1417 Vals.push_back(GetEncodedRMWOperation(
1418 cast<AtomicRMWInst>(I).getOperation())); 1418 cast<AtomicRMWInst>(I).getOperation()));
1419 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile()); 1419 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
1420 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering())); 1420 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
1421 Vals.push_back(GetEncodedSynchScope( 1421 Vals.push_back(GetEncodedSynchScope(
1422 cast<AtomicRMWInst>(I).getSynchScope())); 1422 cast<AtomicRMWInst>(I).getSynchScope()));
1423 break; 1423 break;
1424 case Instruction::Fence: 1424 case Instruction::Fence:
1425 Code = bitc::FUNC_CODE_INST_FENCE; 1425 Code = naclbitc::FUNC_CODE_INST_FENCE;
1426 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering())); 1426 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
1427 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope())); 1427 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
1428 break; 1428 break;
1429 case Instruction::Call: { 1429 case Instruction::Call: {
1430 const CallInst &CI = cast<CallInst>(I); 1430 const CallInst &CI = cast<CallInst>(I);
1431 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType()); 1431 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
1432 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1432 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1433 1433
1434 Code = bitc::FUNC_CODE_INST_CALL; 1434 Code = naclbitc::FUNC_CODE_INST_CALL;
1435 1435
1436 Vals.push_back(VE.getAttributeID(CI.getAttributes())); 1436 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1437 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall())); 1437 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
1438 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee 1438 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
1439 1439
1440 // Emit value #'s for the fixed parameters. 1440 // Emit value #'s for the fixed parameters.
1441 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) { 1441 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
1442 // Check for labels (can happen with asm labels). 1442 // Check for labels (can happen with asm labels).
1443 if (FTy->getParamType(i)->isLabelTy()) 1443 if (FTy->getParamType(i)->isLabelTy())
1444 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); 1444 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
1445 else 1445 else
1446 pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param. 1446 pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param.
1447 } 1447 }
1448 1448
1449 // Emit type/value pairs for varargs params. 1449 // Emit type/value pairs for varargs params.
1450 if (FTy->isVarArg()) { 1450 if (FTy->isVarArg()) {
1451 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands(); 1451 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
1452 i != e; ++i) 1452 i != e; ++i)
1453 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs 1453 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
1454 } 1454 }
1455 break; 1455 break;
1456 } 1456 }
1457 case Instruction::VAArg: 1457 case Instruction::VAArg:
1458 Code = bitc::FUNC_CODE_INST_VAARG; 1458 Code = naclbitc::FUNC_CODE_INST_VAARG;
1459 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 1459 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
1460 pushValue(I.getOperand(0), InstID, Vals, VE); // valist. 1460 pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
1461 Vals.push_back(VE.getTypeID(I.getType())); // restype. 1461 Vals.push_back(VE.getTypeID(I.getType())); // restype.
1462 break; 1462 break;
1463 } 1463 }
1464 1464
1465 Stream.EmitRecord(Code, Vals, AbbrevToUse); 1465 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1466 Vals.clear(); 1466 Vals.clear();
1467 } 1467 }
1468 1468
1469 // Emit names for globals/functions etc. 1469 // Emit names for globals/functions etc.
1470 static void WriteValueSymbolTable(const ValueSymbolTable &VST, 1470 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1471 const NaClValueEnumerator &VE, 1471 const NaClValueEnumerator &VE,
1472 NaClBitstreamWriter &Stream) { 1472 NaClBitstreamWriter &Stream) {
1473 if (VST.empty()) return; 1473 if (VST.empty()) return;
1474 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 1474 Stream.EnterSubblock(naclbitc::VALUE_SYMTAB_BLOCK_ID, 4);
1475 1475
1476 // FIXME: Set up the abbrev, we know how many values there are! 1476 // FIXME: Set up the abbrev, we know how many values there are!
1477 // FIXME: We know if the type names can use 7-bit ascii. 1477 // FIXME: We know if the type names can use 7-bit ascii.
1478 SmallVector<unsigned, 64> NameVals; 1478 SmallVector<unsigned, 64> NameVals;
1479 1479
1480 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 1480 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1481 SI != SE; ++SI) { 1481 SI != SE; ++SI) {
1482 1482
1483 const ValueName &Name = *SI; 1483 const ValueName &Name = *SI;
1484 1484
1485 // Figure out the encoding to use for the name. 1485 // Figure out the encoding to use for the name.
1486 bool is7Bit = true; 1486 bool is7Bit = true;
1487 bool isChar6 = true; 1487 bool isChar6 = true;
1488 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 1488 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1489 C != E; ++C) { 1489 C != E; ++C) {
1490 if (isChar6) 1490 if (isChar6)
1491 isChar6 = BitCodeAbbrevOp::isChar6(*C); 1491 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1492 if ((unsigned char)*C & 128) { 1492 if ((unsigned char)*C & 128) {
1493 is7Bit = false; 1493 is7Bit = false;
1494 break; // don't bother scanning the rest. 1494 break; // don't bother scanning the rest.
1495 } 1495 }
1496 } 1496 }
1497 1497
1498 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 1498 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
1499 1499
1500 // VST_ENTRY: [valueid, namechar x N] 1500 // VST_ENTRY: [valueid, namechar x N]
1501 // VST_BBENTRY: [bbid, namechar x N] 1501 // VST_BBENTRY: [bbid, namechar x N]
1502 unsigned Code; 1502 unsigned Code;
1503 if (isa<BasicBlock>(SI->getValue())) { 1503 if (isa<BasicBlock>(SI->getValue())) {
1504 Code = bitc::VST_CODE_BBENTRY; 1504 Code = naclbitc::VST_CODE_BBENTRY;
1505 if (isChar6) 1505 if (isChar6)
1506 AbbrevToUse = VST_BBENTRY_6_ABBREV; 1506 AbbrevToUse = VST_BBENTRY_6_ABBREV;
1507 } else { 1507 } else {
1508 Code = bitc::VST_CODE_ENTRY; 1508 Code = naclbitc::VST_CODE_ENTRY;
1509 if (isChar6) 1509 if (isChar6)
1510 AbbrevToUse = VST_ENTRY_6_ABBREV; 1510 AbbrevToUse = VST_ENTRY_6_ABBREV;
1511 else if (is7Bit) 1511 else if (is7Bit)
1512 AbbrevToUse = VST_ENTRY_7_ABBREV; 1512 AbbrevToUse = VST_ENTRY_7_ABBREV;
1513 } 1513 }
1514 1514
1515 NameVals.push_back(VE.getValueID(SI->getValue())); 1515 NameVals.push_back(VE.getValueID(SI->getValue()));
1516 for (const char *P = Name.getKeyData(), 1516 for (const char *P = Name.getKeyData(),
1517 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 1517 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
1518 NameVals.push_back((unsigned char)*P); 1518 NameVals.push_back((unsigned char)*P);
1519 1519
1520 // Emit the finished record. 1520 // Emit the finished record.
1521 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 1521 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
1522 NameVals.clear(); 1522 NameVals.clear();
1523 } 1523 }
1524 Stream.ExitBlock(); 1524 Stream.ExitBlock();
1525 } 1525 }
1526 1526
1527 /// WriteFunction - Emit a function body to the module stream. 1527 /// WriteFunction - Emit a function body to the module stream.
1528 static void WriteFunction(const Function &F, NaClValueEnumerator &VE, 1528 static void WriteFunction(const Function &F, NaClValueEnumerator &VE,
1529 NaClBitstreamWriter &Stream) { 1529 NaClBitstreamWriter &Stream) {
1530 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); 1530 Stream.EnterSubblock(naclbitc::FUNCTION_BLOCK_ID, 4);
1531 VE.incorporateFunction(F); 1531 VE.incorporateFunction(F);
1532 1532
1533 SmallVector<unsigned, 64> Vals; 1533 SmallVector<unsigned, 64> Vals;
1534 1534
1535 // Emit the number of basic blocks, so the reader can create them ahead of 1535 // Emit the number of basic blocks, so the reader can create them ahead of
1536 // time. 1536 // time.
1537 Vals.push_back(VE.getBasicBlocks().size()); 1537 Vals.push_back(VE.getBasicBlocks().size());
1538 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 1538 Stream.EmitRecord(naclbitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1539 Vals.clear(); 1539 Vals.clear();
1540 1540
1541 // If there are function-local constants, emit them now. 1541 // If there are function-local constants, emit them now.
1542 unsigned CstStart, CstEnd; 1542 unsigned CstStart, CstEnd;
1543 VE.getFunctionConstantRange(CstStart, CstEnd); 1543 VE.getFunctionConstantRange(CstStart, CstEnd);
1544 WriteConstants(CstStart, CstEnd, VE, Stream, false); 1544 WriteConstants(CstStart, CstEnd, VE, Stream, false);
1545 1545
1546 // If there is function-local metadata, emit it now. 1546 // If there is function-local metadata, emit it now.
1547 WriteFunctionLocalMetadata(F, VE, Stream); 1547 WriteFunctionLocalMetadata(F, VE, Stream);
1548 1548
(...skipping 15 matching lines...) Expand all
1564 1564
1565 // If the instruction has metadata, write a metadata attachment later. 1565 // If the instruction has metadata, write a metadata attachment later.
1566 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); 1566 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1567 1567
1568 // If the instruction has a debug location, emit it. 1568 // If the instruction has a debug location, emit it.
1569 DebugLoc DL = I->getDebugLoc(); 1569 DebugLoc DL = I->getDebugLoc();
1570 if (DL.isUnknown()) { 1570 if (DL.isUnknown()) {
1571 // nothing todo. 1571 // nothing todo.
1572 } else if (DL == LastDL) { 1572 } else if (DL == LastDL) {
1573 // Just repeat the same debug loc as last time. 1573 // Just repeat the same debug loc as last time.
1574 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); 1574 Stream.EmitRecord(naclbitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
1575 } else { 1575 } else {
1576 MDNode *Scope, *IA; 1576 MDNode *Scope, *IA;
1577 DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); 1577 DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1578 1578
1579 Vals.push_back(DL.getLine()); 1579 Vals.push_back(DL.getLine());
1580 Vals.push_back(DL.getCol()); 1580 Vals.push_back(DL.getCol());
1581 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); 1581 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1582 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); 1582 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
1583 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); 1583 Stream.EmitRecord(naclbitc::FUNC_CODE_DEBUG_LOC, Vals);
1584 Vals.clear(); 1584 Vals.clear();
1585 1585
1586 LastDL = DL; 1586 LastDL = DL;
1587 } 1587 }
1588 } 1588 }
1589 1589
1590 // Emit names for all the instructions etc. 1590 // Emit names for all the instructions etc.
1591 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 1591 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1592 1592
1593 if (NeedsMetadataAttachment) 1593 if (NeedsMetadataAttachment)
1594 WriteMetadataAttachment(F, VE, Stream); 1594 WriteMetadataAttachment(F, VE, Stream);
1595 VE.purgeFunction(); 1595 VE.purgeFunction();
1596 Stream.ExitBlock(); 1596 Stream.ExitBlock();
1597 } 1597 }
1598 1598
1599 // Emit blockinfo, which defines the standard abbreviations etc. 1599 // Emit blockinfo, which defines the standard abbreviations etc.
1600 static void WriteBlockInfo(const NaClValueEnumerator &VE, NaClBitstreamWriter &S tream) { 1600 static void WriteBlockInfo(const NaClValueEnumerator &VE, NaClBitstreamWriter &S tream) {
1601 // We only want to emit block info records for blocks that have multiple 1601 // We only want to emit block info records for blocks that have multiple
1602 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. 1602 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
1603 // Other blocks can define their abbrevs inline. 1603 // Other blocks can define their abbrevs inline.
1604 Stream.EnterBlockInfoBlock(2); 1604 Stream.EnterBlockInfoBlock(2);
1605 1605
1606 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 1606 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1607 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1607 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1608 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 1608 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1609 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1609 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1610 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1610 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1611 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1611 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1612 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1612 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1613 Abbv) != VST_ENTRY_8_ABBREV) 1613 Abbv) != VST_ENTRY_8_ABBREV)
1614 llvm_unreachable("Unexpected abbrev ordering!"); 1614 llvm_unreachable("Unexpected abbrev ordering!");
1615 } 1615 }
1616 1616
1617 { // 7-bit fixed width VST_ENTRY strings. 1617 { // 7-bit fixed width VST_ENTRY strings.
1618 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1618 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1619 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1619 Abbv->Add(BitCodeAbbrevOp(naclbitc::VST_CODE_ENTRY));
1620 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1620 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1621 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1621 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1622 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 1622 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1623 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1623 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1624 Abbv) != VST_ENTRY_7_ABBREV) 1624 Abbv) != VST_ENTRY_7_ABBREV)
1625 llvm_unreachable("Unexpected abbrev ordering!"); 1625 llvm_unreachable("Unexpected abbrev ordering!");
1626 } 1626 }
1627 { // 6-bit char6 VST_ENTRY strings. 1627 { // 6-bit char6 VST_ENTRY strings.
1628 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1628 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1629 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1629 Abbv->Add(BitCodeAbbrevOp(naclbitc::VST_CODE_ENTRY));
1630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1632 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1632 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1633 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1633 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1634 Abbv) != VST_ENTRY_6_ABBREV) 1634 Abbv) != VST_ENTRY_6_ABBREV)
1635 llvm_unreachable("Unexpected abbrev ordering!"); 1635 llvm_unreachable("Unexpected abbrev ordering!");
1636 } 1636 }
1637 { // 6-bit char6 VST_BBENTRY strings. 1637 { // 6-bit char6 VST_BBENTRY strings.
1638 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1638 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1639 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 1639 Abbv->Add(BitCodeAbbrevOp(naclbitc::VST_CODE_BBENTRY));
1640 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1640 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1641 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1641 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1642 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1642 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1643 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1643 if (Stream.EmitBlockInfoAbbrev(naclbitc::VALUE_SYMTAB_BLOCK_ID,
1644 Abbv) != VST_BBENTRY_6_ABBREV) 1644 Abbv) != VST_BBENTRY_6_ABBREV)
1645 llvm_unreachable("Unexpected abbrev ordering!"); 1645 llvm_unreachable("Unexpected abbrev ordering!");
1646 } 1646 }
1647 1647
1648 1648
1649 1649
1650 { // SETTYPE abbrev for CONSTANTS_BLOCK. 1650 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1651 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1651 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1652 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 1652 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_SETTYPE));
1653 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1653 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1654 Log2_32_Ceil(VE.getTypes().size()+1))); 1654 Log2_32_Ceil(VE.getTypes().size()+1)));
1655 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1655 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1656 Abbv) != CONSTANTS_SETTYPE_ABBREV) 1656 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1657 llvm_unreachable("Unexpected abbrev ordering!"); 1657 llvm_unreachable("Unexpected abbrev ordering!");
1658 } 1658 }
1659 1659
1660 { // INTEGER abbrev for CONSTANTS_BLOCK. 1660 { // INTEGER abbrev for CONSTANTS_BLOCK.
1661 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1661 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1662 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 1662 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_INTEGER));
1663 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1663 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1664 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1664 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1665 Abbv) != CONSTANTS_INTEGER_ABBREV) 1665 Abbv) != CONSTANTS_INTEGER_ABBREV)
1666 llvm_unreachable("Unexpected abbrev ordering!"); 1666 llvm_unreachable("Unexpected abbrev ordering!");
1667 } 1667 }
1668 1668
1669 { // CE_CAST abbrev for CONSTANTS_BLOCK. 1669 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1670 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1670 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1671 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 1671 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_CE_CAST));
1672 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 1672 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1673 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 1673 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1674 Log2_32_Ceil(VE.getTypes().size()+1))); 1674 Log2_32_Ceil(VE.getTypes().size()+1)));
1675 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 1675 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1676 1676
1677 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1677 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1678 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1678 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1679 llvm_unreachable("Unexpected abbrev ordering!"); 1679 llvm_unreachable("Unexpected abbrev ordering!");
1680 } 1680 }
1681 { // NULL abbrev for CONSTANTS_BLOCK. 1681 { // NULL abbrev for CONSTANTS_BLOCK.
1682 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1682 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1683 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 1683 Abbv->Add(BitCodeAbbrevOp(naclbitc::CST_CODE_NULL));
1684 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1684 if (Stream.EmitBlockInfoAbbrev(naclbitc::CONSTANTS_BLOCK_ID,
1685 Abbv) != CONSTANTS_NULL_Abbrev) 1685 Abbv) != CONSTANTS_NULL_Abbrev)
1686 llvm_unreachable("Unexpected abbrev ordering!"); 1686 llvm_unreachable("Unexpected abbrev ordering!");
1687 } 1687 }
1688 1688
1689 // FIXME: This should only use space for first class types! 1689 // FIXME: This should only use space for first class types!
1690 1690
1691 { // INST_LOAD abbrev for FUNCTION_BLOCK. 1691 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1692 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1692 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1693 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 1693 Abbv->Add(BitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_LOAD));
1694 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 1694 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1695 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 1695 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1696 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 1696 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1697 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1697 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1698 Abbv) != FUNCTION_INST_LOAD_ABBREV) 1698 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1699 llvm_unreachable("Unexpected abbrev ordering!"); 1699 llvm_unreachable("Unexpected abbrev ordering!");
1700 } 1700 }
1701 { // INST_BINOP abbrev for FUNCTION_BLOCK. 1701 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1702 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1702 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1703 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1703 Abbv->Add(BitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_BINOP));
1704 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1704 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1705 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1705 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1706 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1706 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1707 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1707 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1708 Abbv) != FUNCTION_INST_BINOP_ABBREV) 1708 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1709 llvm_unreachable("Unexpected abbrev ordering!"); 1709 llvm_unreachable("Unexpected abbrev ordering!");
1710 } 1710 }
1711 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. 1711 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1712 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1712 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1713 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1713 Abbv->Add(BitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_BINOP));
1714 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1714 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1716 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1716 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1717 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags 1717 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1718 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1718 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1719 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 1719 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1720 llvm_unreachable("Unexpected abbrev ordering!"); 1720 llvm_unreachable("Unexpected abbrev ordering!");
1721 } 1721 }
1722 { // INST_CAST abbrev for FUNCTION_BLOCK. 1722 { // INST_CAST abbrev for FUNCTION_BLOCK.
1723 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1723 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1724 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 1724 Abbv->Add(BitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_CAST));
1725 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal 1725 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1726 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 1726 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1727 Log2_32_Ceil(VE.getTypes().size()+1))); 1727 Log2_32_Ceil(VE.getTypes().size()+1)));
1728 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1728 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1729 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1729 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1730 Abbv) != FUNCTION_INST_CAST_ABBREV) 1730 Abbv) != FUNCTION_INST_CAST_ABBREV)
1731 llvm_unreachable("Unexpected abbrev ordering!"); 1731 llvm_unreachable("Unexpected abbrev ordering!");
1732 } 1732 }
1733 1733
1734 { // INST_RET abbrev for FUNCTION_BLOCK. 1734 { // INST_RET abbrev for FUNCTION_BLOCK.
1735 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1735 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1736 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1736 Abbv->Add(BitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET));
1737 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1737 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1738 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 1738 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1739 llvm_unreachable("Unexpected abbrev ordering!"); 1739 llvm_unreachable("Unexpected abbrev ordering!");
1740 } 1740 }
1741 { // INST_RET abbrev for FUNCTION_BLOCK. 1741 { // INST_RET abbrev for FUNCTION_BLOCK.
1742 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1742 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1743 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1743 Abbv->Add(BitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_RET));
1744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID 1744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1745 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1745 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1746 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 1746 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1747 llvm_unreachable("Unexpected abbrev ordering!"); 1747 llvm_unreachable("Unexpected abbrev ordering!");
1748 } 1748 }
1749 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 1749 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1750 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1750 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1751 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 1751 Abbv->Add(BitCodeAbbrevOp(naclbitc::FUNC_CODE_INST_UNREACHABLE));
1752 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1752 if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID,
1753 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 1753 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1754 llvm_unreachable("Unexpected abbrev ordering!"); 1754 llvm_unreachable("Unexpected abbrev ordering!");
1755 } 1755 }
1756 1756
1757 Stream.ExitBlock(); 1757 Stream.ExitBlock();
1758 } 1758 }
1759 1759
1760 /// WriteModule - Emit the specified module to the bitstream. 1760 /// WriteModule - Emit the specified module to the bitstream.
1761 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) { 1761 static void WriteModule(const Module *M, NaClBitstreamWriter &Stream) {
1762 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 1762 Stream.EnterSubblock(naclbitc::MODULE_BLOCK_ID, 3);
1763 1763
1764 SmallVector<unsigned, 1> Vals; 1764 SmallVector<unsigned, 1> Vals;
1765 unsigned CurVersion = 1; 1765 unsigned CurVersion = 1;
1766 Vals.push_back(CurVersion); 1766 Vals.push_back(CurVersion);
1767 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 1767 Stream.EmitRecord(naclbitc::MODULE_CODE_VERSION, Vals);
1768 1768
1769 // Analyze the module, enumerating globals, functions, etc. 1769 // Analyze the module, enumerating globals, functions, etc.
1770 NaClValueEnumerator VE(M); 1770 NaClValueEnumerator VE(M);
1771 1771
1772 // Emit blockinfo, which defines the standard abbreviations etc. 1772 // Emit blockinfo, which defines the standard abbreviations etc.
1773 WriteBlockInfo(VE, Stream); 1773 WriteBlockInfo(VE, Stream);
1774 1774
1775 // Emit information about attribute groups. 1775 // Emit information about attribute groups.
1776 WriteAttributeGroupTable(VE, Stream); 1776 WriteAttributeGroupTable(VE, Stream);
1777 1777
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1908 // Emit the module. 1908 // Emit the module.
1909 WriteModule(M, Stream); 1909 WriteModule(M, Stream);
1910 } 1910 }
1911 1911
1912 if (TT.isOSDarwin()) 1912 if (TT.isOSDarwin())
1913 EmitDarwinBCHeaderAndTrailer(Buffer, TT); 1913 EmitDarwinBCHeaderAndTrailer(Buffer, TT);
1914 1914
1915 // Write the generated bitstream to "Out". 1915 // Write the generated bitstream to "Out".
1916 Out.write((char*)&Buffer.front(), Buffer.size()); 1916 Out.write((char*)&Buffer.front(), Buffer.size());
1917 } 1917 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698