OLD | NEW |
---|---|
1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// | 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
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 // This file implements the PNaCl bitcode file to Ice, to machine code | 10 // This file implements the PNaCl bitcode file to Ice, to machine code |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 using namespace llvm; | 42 using namespace llvm; |
43 | 43 |
44 namespace { | 44 namespace { |
45 | 45 |
46 // TODO(kschimpf) Remove error recovery once implementation complete. | 46 // TODO(kschimpf) Remove error recovery once implementation complete. |
47 static cl::opt<bool> AllowErrorRecovery( | 47 static cl::opt<bool> AllowErrorRecovery( |
48 "allow-pnacl-reader-error-recovery", | 48 "allow-pnacl-reader-error-recovery", |
49 cl::desc("Allow error recovery when reading PNaCl bitcode."), | 49 cl::desc("Allow error recovery when reading PNaCl bitcode."), |
50 cl::init(false)); | 50 cl::init(false)); |
51 | 51 |
52 // Models elements in the list of types defined in the types block. | |
53 // These elements can be undefined, a (simple) type, or a function type | |
54 // signature. Note that an extended type is undefined on construction. | |
55 // Use methods setAsSimpleType and setAsFuncSigType to define | |
56 // the extended type. | |
57 class ExtendedType { | |
58 | |
59 public: | |
60 /// Discriminator for LLVM-style RTTI. | |
61 enum TypeKind { Undefined, Simple, FuncSig }; | |
62 | |
63 explicit ExtendedType() : Kind(Undefined) {} | |
Jim Stichnoth
2014/10/06 22:48:52
I don't think you need "explicit" on a zero-arg ct
Karl
2014/10/07 20:15:58
Done.
| |
64 | |
65 explicit ExtendedType(const ExtendedType &Ty) | |
66 : Signature(Ty.Signature), Kind(Ty.Kind) {} | |
67 | |
68 ExtendedType &operator=(const ExtendedType &Ty) { | |
Jim Stichnoth
2014/10/06 22:48:52
This is a shallow copy, so can you just use the de
Karl
2014/10/07 20:15:59
Removed assignment, not needed. Replaced explicit
| |
69 Kind = Ty.Kind; | |
70 Signature = Ty.Signature; | |
71 return *this; | |
72 } | |
73 | |
74 virtual ~ExtendedType() {} | |
75 | |
76 ExtendedType::TypeKind getKind() const { return Kind; } | |
77 void Dump(Ice::Ostream &Stream) const; | |
78 | |
79 /// Changes the extended type to a simple type with the given | |
80 /// value. | |
81 void setAsSimpleType(Ice::Type Ty) { | |
82 assert(Kind == Undefined); | |
83 Kind = Simple; | |
84 Signature.setReturnType(Ty); | |
85 } | |
86 | |
87 /// Changes the extended type to an (empty) function signature type. | |
88 void setAsFunctionType() { | |
89 assert(Kind == Undefined); | |
90 Kind = FuncSig; | |
91 } | |
92 | |
93 /// Changes type back to undefined type. | |
94 void resetToUndefined() { | |
95 if (Kind != Undefined) { | |
96 Kind = Undefined; | |
97 Signature.reset(); | |
98 } | |
99 } | |
100 | |
101 protected: | |
102 // Note: For simple types, the return type of the signature will | |
jvoung (off chromium)
2014/10/07 15:54:42
extra space between sig and will
Karl
2014/10/07 20:15:59
Done.
| |
103 // be used to hold the simple type. | |
104 Ice::FuncSigType Signature; | |
105 | |
106 private: | |
107 ExtendedType::TypeKind Kind; | |
108 }; | |
109 | |
110 Ice::Ostream &operator<<(Ice::Ostream &Stream, const ExtendedType &Ty) { | |
111 Ty.Dump(Stream); | |
112 return Stream; | |
113 } | |
114 | |
115 Ice::Ostream &operator<<(Ice::Ostream &Stream, ExtendedType::TypeKind Kind) { | |
116 Stream << "ExtendedType::"; | |
117 switch (Kind) { | |
118 case ExtendedType::Undefined: | |
119 Stream << "Undefined"; | |
120 break; | |
121 case ExtendedType::Simple: | |
122 Stream << "Simple"; | |
123 break; | |
124 case ExtendedType::FuncSig: | |
125 Stream << "FuncSig"; | |
126 break; | |
127 default: | |
128 Stream << "??"; | |
129 break; | |
130 } | |
131 return Stream; | |
132 } | |
133 | |
134 // Models an ICE type as an extended type. | |
135 class SimpleExtendedType : public ExtendedType { | |
136 SimpleExtendedType(const SimpleExtendedType &) = delete; | |
137 SimpleExtendedType &operator=(const SimpleExtendedType &) = delete; | |
138 | |
139 public: | |
140 Ice::Type getType() const { return Signature.getReturnType(); } | |
141 | |
142 static bool classof(const ExtendedType *Ty) { | |
143 return Ty->getKind() == Simple; | |
144 } | |
145 }; | |
146 | |
147 // Models a function signature as an extended type. | |
148 class FuncSigExtendedType : public ExtendedType { | |
149 FuncSigExtendedType(const FuncSigExtendedType &) = delete; | |
150 FuncSigExtendedType &operator=(const FuncSigExtendedType &) = delete; | |
151 | |
152 public: | |
153 const Ice::FuncSigType &getSignature() const { return Signature; } | |
154 void setReturnType(Ice::Type ReturnType) { | |
155 Signature.setReturnType(ReturnType); | |
156 } | |
157 void appendArgType(Ice::Type ArgType) { Signature.appendArgType(ArgType); } | |
158 static bool classof(const ExtendedType *Ty) { | |
159 return Ty->getKind() == FuncSig; | |
160 } | |
161 }; | |
162 | |
163 void ExtendedType::Dump(Ice::Ostream &Stream) const { | |
164 Stream << Kind; | |
165 switch (Kind) { | |
166 case Simple: { | |
167 Stream << " " << Signature.getReturnType(); | |
168 break; | |
169 } | |
170 case FuncSig: { | |
171 Stream << " " << Signature; | |
172 } | |
173 default: | |
174 break; | |
175 } | |
176 } | |
177 | |
52 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 178 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
53 class TopLevelParser : public NaClBitcodeParser { | 179 class TopLevelParser : public NaClBitcodeParser { |
54 TopLevelParser(const TopLevelParser &) = delete; | 180 TopLevelParser(const TopLevelParser &) = delete; |
55 TopLevelParser &operator=(const TopLevelParser &) = delete; | 181 TopLevelParser &operator=(const TopLevelParser &) = delete; |
56 | 182 |
57 public: | 183 public: |
58 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, | 184 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, |
59 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, | 185 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, |
60 bool &ErrorStatus) | 186 bool &ErrorStatus) |
61 : NaClBitcodeParser(Cursor), Translator(Translator), | 187 : NaClBitcodeParser(Cursor), Translator(Translator), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 | 219 |
94 /// Returns the number of bytes in the bitcode header. | 220 /// Returns the number of bytes in the bitcode header. |
95 size_t getHeaderSize() const { return Header.getHeaderSize(); } | 221 size_t getHeaderSize() const { return Header.getHeaderSize(); } |
96 | 222 |
97 /// Returns the llvm context to use. | 223 /// Returns the llvm context to use. |
98 LLVMContext &getLLVMContext() const { return Mod->getContext(); } | 224 LLVMContext &getLLVMContext() const { return Mod->getContext(); } |
99 | 225 |
100 /// Changes the size of the type list to the given size. | 226 /// Changes the size of the type list to the given size. |
101 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } | 227 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } |
102 | 228 |
103 /// Returns the type associated with the given index. | 229 /// Returns the undefined type associated with type ID. |
104 Type *getTypeByID(unsigned ID) { | 230 ExtendedType *getUndefinedTypeByID(unsigned ID) const { |
105 // Note: method resizeTypeIDValues expands TypeIDValues | 231 ExtendedType *Ty = const_cast<::ExtendedType *>( |
106 // to the specified size, and fills elements with NULL. | 232 getTypeByIDAsKind(ID, ExtendedType::Undefined)); |
107 Type *Ty = ID < TypeIDValues.size() ? TypeIDValues[ID] : NULL; | 233 if (Ty == nullptr) |
108 if (Ty) | 234 Ty = const_cast<TopLevelParser *>(this)->getDummyUndefinedExtendedType(); |
jvoung (off chromium)
2014/10/07 15:54:41
In this case, how does the result of getDummyUndef
Karl
2014/10/07 20:15:58
This method is called only once for each ID, to ge
| |
109 return Ty; | 235 return Ty; |
110 return reportTypeIDAsUndefined(ID); | |
111 } | 236 } |
112 | 237 |
113 /// Defines type for ID. | 238 /// Returns the type associated with the given index. |
114 void setTypeID(unsigned ID, Type *Ty) { | 239 Ice::Type getSimpleTypeByID(unsigned ID) const { |
115 if (ID < TypeIDValues.size() && TypeIDValues[ID] == NULL) { | 240 const ExtendedType *Ty = getTypeByIDAsKind(ID, ExtendedType::Simple); |
116 TypeIDValues[ID] = Ty; | 241 if (Ty == nullptr) |
117 return; | 242 // Return error recovery value. |
118 } | 243 return Ice::IceType_void; |
119 reportBadSetTypeID(ID, Ty); | 244 return cast<SimpleExtendedType>(Ty)->getType(); |
245 } | |
246 | |
247 /// Returns the type signature associated with the given index. | |
248 const Ice::FuncSigType &getFuncSigTypeByID(unsigned ID) const { | |
249 const ExtendedType *Ty = getTypeByIDAsKind(ID, ExtendedType::FuncSig); | |
250 if (Ty == nullptr) | |
251 // Return error recovery value. | |
252 return UndefinedFuncSigType; | |
253 return cast<FuncSigExtendedType>(Ty)->getSignature(); | |
120 } | 254 } |
121 | 255 |
122 /// Sets the next function ID to the given LLVM function. | 256 /// Sets the next function ID to the given LLVM function. |
123 void setNextFunctionID(Function *Fcn) { | 257 void setNextFunctionID(Function *Fcn) { |
124 ++NumFunctionIds; | 258 ++NumFunctionIds; |
125 ValueIDValues.push_back(Fcn); | 259 ValueIDValues.push_back(Fcn); |
126 } | 260 } |
127 | 261 |
128 /// Defines the next function ID as one that has an implementation | 262 /// Defines the next function ID as one that has an implementation |
129 /// (i.e a corresponding function block in the bitcode). | 263 /// (i.e a corresponding function block in the bitcode). |
130 void setNextValueIDAsImplementedFunction() { | 264 void setNextValueIDAsImplementedFunction() { |
131 DefiningFunctionsList.push_back(ValueIDValues.size()); | 265 DefiningFunctionsList.push_back(ValueIDValues.size()); |
132 } | 266 } |
133 | 267 |
134 /// Returns the value id that should be associated with the the | 268 /// Returns the value id that should be associated with the the |
135 /// current function block. Increments internal counters during call | 269 /// current function block. Increments internal counters during call |
136 /// so that it will be in correct position for next function block. | 270 /// so that it will be in correct position for next function block. |
137 unsigned getNextFunctionBlockValueID() { | 271 unsigned getNextFunctionBlockValueID() { |
138 if (NumFunctionBlocks >= DefiningFunctionsList.size()) | 272 if (NumFunctionBlocks >= DefiningFunctionsList.size()) |
139 report_fatal_error( | 273 report_fatal_error( |
140 "More function blocks than defined function addresses"); | 274 "More function blocks than defined function addresses"); |
141 return DefiningFunctionsList[NumFunctionBlocks++]; | 275 return DefiningFunctionsList[NumFunctionBlocks++]; |
142 } | 276 } |
143 | 277 |
144 /// Returns the LLVM IR value associatd with the global value ID. | 278 /// Returns the LLVM IR value associatd with the global value ID. |
145 Value *getGlobalValueByID(unsigned ID) const { | 279 Value *getGlobalValueByID(unsigned ID) const { |
146 if (ID >= ValueIDValues.size()) | 280 if (ID >= ValueIDValues.size()) |
147 return NULL; | 281 return nullptr; |
148 return ValueIDValues[ID]; | 282 return ValueIDValues[ID]; |
149 } | 283 } |
150 | 284 |
151 /// Returns the corresponding constant associated with a global value | 285 /// Returns the corresponding constant associated with a global value |
152 /// (i.e. relocatable). | 286 /// (i.e. relocatable). |
153 Ice::Constant *getOrCreateGlobalConstantByID(unsigned ID) { | 287 Ice::Constant *getOrCreateGlobalConstantByID(unsigned ID) { |
154 // TODO(kschimpf): Can this be built when creating global initializers? | 288 // TODO(kschimpf): Can this be built when creating global initializers? |
155 if (ID >= ValueIDConstants.size()) { | 289 if (ID >= ValueIDConstants.size()) { |
156 if (ID >= ValueIDValues.size()) | 290 if (ID >= ValueIDValues.size()) |
157 return NULL; | 291 return nullptr; |
158 ValueIDConstants.resize(ValueIDValues.size()); | 292 ValueIDConstants.resize(ValueIDValues.size()); |
159 } | 293 } |
160 Ice::Constant *C = ValueIDConstants[ID]; | 294 Ice::Constant *C = ValueIDConstants[ID]; |
161 if (C != NULL) | 295 if (C != nullptr) |
162 return C; | 296 return C; |
163 Value *V = ValueIDValues[ID]; | 297 Value *V = ValueIDValues[ID]; |
164 assert(isa<GlobalValue>(V)); | 298 assert(isa<GlobalValue>(V)); |
165 C = getTranslator().getContext()->getConstantSym(getIcePointerType(), 0, | 299 C = getTranslator().getContext()->getConstantSym(getIcePointerType(), 0, |
166 V->getName()); | 300 V->getName()); |
167 ValueIDConstants[ID] = C; | 301 ValueIDConstants[ID] = C; |
168 return C; | 302 return C; |
169 } | 303 } |
170 | 304 |
171 /// Returns the number of function addresses (i.e. ID's) defined in | 305 /// Returns the number of function addresses (i.e. ID's) defined in |
172 /// the bitcode file. | 306 /// the bitcode file. |
173 unsigned getNumFunctionIDs() const { return NumFunctionIds; } | 307 unsigned getNumFunctionIDs() const { return NumFunctionIds; } |
174 | 308 |
175 /// Returns the number of global values defined in the bitcode | 309 /// Returns the number of global values defined in the bitcode |
176 /// file. | 310 /// file. |
177 unsigned getNumGlobalValueIDs() const { return ValueIDValues.size(); } | 311 unsigned getNumGlobalValueIDs() const { return ValueIDValues.size(); } |
178 | 312 |
179 /// Resizes the list of value IDs to include Count global variable | 313 /// Resizes the list of value IDs to include Count global variable |
180 /// IDs. | 314 /// IDs. |
181 void resizeValueIDsForGlobalVarCount(unsigned Count) { | 315 void resizeValueIDsForGlobalVarCount(unsigned Count) { |
182 ValueIDValues.resize(ValueIDValues.size() + Count); | 316 ValueIDValues.resize(ValueIDValues.size() + Count); |
183 } | 317 } |
184 | 318 |
185 /// Returns the global variable address associated with the given | 319 /// Returns the global variable address associated with the given |
186 /// value ID. If the ID refers to a global variable address not yet | 320 /// value ID. If the ID refers to a global variable address not yet |
187 /// defined, a placeholder is created so that we can fix it up | 321 /// defined, a placeholder is created so that we can fix it up |
188 /// later. | 322 /// later. |
189 Constant *getOrCreateGlobalVarRef(unsigned ID) { | 323 Constant *getOrCreateGlobalVarRef(unsigned ID) { |
190 if (ID >= ValueIDValues.size()) | 324 if (ID >= ValueIDValues.size()) |
191 return NULL; | 325 return nullptr; |
192 if (Value *C = ValueIDValues[ID]) | 326 if (Value *C = ValueIDValues[ID]) |
193 return dyn_cast<Constant>(C); | 327 return dyn_cast<Constant>(C); |
194 Constant *C = new GlobalVariable(*Mod, GlobalVarPlaceHolderType, false, | 328 Constant *C = new GlobalVariable(*Mod, GlobalVarPlaceHolderType, false, |
195 GlobalValue::ExternalLinkage, 0); | 329 GlobalValue::ExternalLinkage, 0); |
196 ValueIDValues[ID] = C; | 330 ValueIDValues[ID] = C; |
197 return C; | 331 return C; |
198 } | 332 } |
199 | 333 |
200 /// Assigns the given global variable (address) to the given value | 334 /// Assigns the given global variable (address) to the given value |
201 /// ID. Returns true if ID is a valid global variable ID. Otherwise | 335 /// ID. Returns true if ID is a valid global variable ID. Otherwise |
202 /// returns false. | 336 /// returns false. |
203 bool assignGlobalVariable(GlobalVariable *GV, unsigned ID) { | 337 bool assignGlobalVariable(GlobalVariable *GV, unsigned ID) { |
204 if (ID < NumFunctionIds || ID >= ValueIDValues.size()) | 338 if (ID < NumFunctionIds || ID >= ValueIDValues.size()) |
205 return false; | 339 return false; |
206 WeakVH &OldV = ValueIDValues[ID]; | 340 WeakVH &OldV = ValueIDValues[ID]; |
207 if (OldV == NULL) { | 341 if (OldV == nullptr) { |
208 ValueIDValues[ID] = GV; | 342 ValueIDValues[ID] = GV; |
209 return true; | 343 return true; |
210 } | 344 } |
211 | 345 |
212 // If reached, there was a forward reference to this value. Replace it. | 346 // If reached, there was a forward reference to this value. Replace it. |
213 Value *PrevVal = OldV; | 347 Value *PrevVal = OldV; |
214 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal); | 348 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal); |
215 Placeholder->replaceAllUsesWith( | 349 Placeholder->replaceAllUsesWith( |
216 ConstantExpr::getBitCast(GV, Placeholder->getType())); | 350 ConstantExpr::getBitCast(GV, Placeholder->getType())); |
217 Placeholder->eraseFromParent(); | 351 Placeholder->eraseFromParent(); |
218 ValueIDValues[ID] = GV; | 352 ValueIDValues[ID] = GV; |
219 return true; | 353 return true; |
220 } | 354 } |
221 | 355 |
222 /// Returns the corresponding ICE type for LLVMTy. | 356 /// Returns the corresponding ICE type for LLVMTy. |
223 Ice::Type convertToIceType(Type *LLVMTy) { | 357 Ice::Type convertToIceType(Type *LLVMTy) { |
224 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); | 358 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); |
225 if (IceTy >= Ice::IceType_NUM) { | 359 if (IceTy >= Ice::IceType_NUM) { |
226 return convertToIceTypeError(LLVMTy); | 360 return convertToIceTypeError(LLVMTy); |
227 } | 361 } |
228 return IceTy; | 362 return IceTy; |
229 } | 363 } |
230 | 364 |
231 /// Returns the corresponding LLVM type for IceTy. | 365 /// Returns the corresponding LLVM type for IceTy. |
232 Type *convertToLLVMType(Ice::Type IceTy) const { | 366 Type *convertToLLVMType(Ice::Type IceTy) const { |
233 return TypeConverter.convertToLLVMType(IceTy); | 367 return TypeConverter.convertToLLVMType(IceTy); |
234 } | 368 } |
235 | 369 |
236 /// Returns the LLVM integer type with the given number of Bits. If | |
237 /// Bits is not a valid PNaCl type, returns NULL. | |
238 Type *getLLVMIntegerType(unsigned Bits) const { | |
239 return TypeConverter.getLLVMIntegerType(Bits); | |
240 } | |
241 | |
242 /// Returns the LLVM vector with the given Size and Ty. If not a | |
243 /// valid PNaCl vector type, returns NULL. | |
244 Type *getLLVMVectorType(unsigned Size, Ice::Type Ty) const { | |
245 return TypeConverter.getLLVMVectorType(Size, Ty); | |
246 } | |
247 | |
248 /// Returns the model for pointer types in ICE. | 370 /// Returns the model for pointer types in ICE. |
249 Ice::Type getIcePointerType() const { | 371 Ice::Type getIcePointerType() const { |
250 return TypeConverter.getIcePointerType(); | 372 return TypeConverter.getIcePointerType(); |
251 } | 373 } |
252 | 374 |
253 private: | 375 private: |
254 // The translator associated with the parser. | 376 // The translator associated with the parser. |
255 Ice::Translator &Translator; | 377 Ice::Translator &Translator; |
256 // The parsed module. | 378 // The parsed module. |
257 std::unique_ptr<Module> Mod; | 379 std::unique_ptr<Module> Mod; |
258 // The data layout to use. | 380 // The data layout to use. |
259 DataLayout DL; | 381 DataLayout DL; |
260 // The bitcode header. | 382 // The bitcode header. |
261 NaClBitcodeHeader &Header; | 383 NaClBitcodeHeader &Header; |
262 // Converter between LLVM and ICE types. | 384 // Converter between LLVM and ICE types. |
263 Ice::TypeConverter TypeConverter; | 385 Ice::TypeConverter TypeConverter; |
264 // The exit status that should be set to true if an error occurs. | 386 // The exit status that should be set to true if an error occurs. |
265 bool &ErrorStatus; | 387 bool &ErrorStatus; |
266 // The number of errors reported. | 388 // The number of errors reported. |
267 unsigned NumErrors; | 389 unsigned NumErrors; |
268 // The types associated with each type ID. | 390 // The types associated with each type ID. |
269 std::vector<Type *> TypeIDValues; | 391 std::vector<ExtendedType> TypeIDValues; |
270 // The (global) value IDs. | 392 // The (global) value IDs. |
271 std::vector<WeakVH> ValueIDValues; | 393 std::vector<WeakVH> ValueIDValues; |
272 // Relocatable constants associated with ValueIDValues. | 394 // Relocatable constants associated with ValueIDValues. |
273 std::vector<Ice::Constant *> ValueIDConstants; | 395 std::vector<Ice::Constant *> ValueIDConstants; |
274 // The number of function IDs. | 396 // The number of function IDs. |
275 unsigned NumFunctionIds; | 397 unsigned NumFunctionIds; |
276 // The number of function blocks (processed so far). | 398 // The number of function blocks (processed so far). |
277 unsigned NumFunctionBlocks; | 399 unsigned NumFunctionBlocks; |
278 // The list of value IDs (in the order found) of defining function | 400 // The list of value IDs (in the order found) of defining function |
279 // addresses. | 401 // addresses. |
280 std::vector<unsigned> DefiningFunctionsList; | 402 std::vector<unsigned> DefiningFunctionsList; |
281 // Cached global variable placeholder type. Used for all forward | 403 // Cached global variable placeholder type. Used for all forward |
282 // references to global variable addresses. | 404 // references to global variable addresses. |
283 Type *GlobalVarPlaceHolderType; | 405 Type *GlobalVarPlaceHolderType; |
406 // Models an undefined function type signature. | |
407 Ice::FuncSigType UndefinedFuncSigType; | |
408 // Models an badly indexed undefined extended type. | |
Jim Stichnoth
2014/10/06 22:48:52
a badly
Karl
2014/10/07 20:15:59
Removed the need for this field, so this has been
| |
409 ExtendedType DummyUndefinedExtendedType; | |
284 | 410 |
285 bool ParseBlock(unsigned BlockID) override; | 411 bool ParseBlock(unsigned BlockID) override; |
286 | 412 |
287 /// Reports that type ID is undefined, and then returns | 413 // Gets extended type associated with the given index, assuming the |
288 /// the void type. | 414 // extended type is of the WantedKind. Generates error message if |
289 Type *reportTypeIDAsUndefined(unsigned ID); | 415 // corresponding extended type of WantedKind can't be found, and |
416 // returns nullptr. | |
417 const ExtendedType * | |
418 getTypeByIDAsKind(unsigned ID, ExtendedType::TypeKind WantedKind) const { | |
419 const ExtendedType *Ty = nullptr; | |
420 if (ID < TypeIDValues.size()) { | |
421 Ty = &TypeIDValues[ID]; | |
422 if (Ty->getKind() == WantedKind) | |
423 return Ty; | |
424 } | |
425 // Note: We turn off constness so that we can generate an error message. | |
Jim Stichnoth
2014/10/06 22:48:51
"... so that we can set ErrorStatus"
Karl
2014/10/07 20:15:59
Done.
| |
426 const_cast<TopLevelParser *>(this)->reportBadTypeIDAs(ID, Ty, WantedKind); | |
427 return nullptr; | |
428 } | |
290 | 429 |
291 /// Reports error about bad call to setTypeID. | 430 // Reports that type ID is undefined. |
292 void reportBadSetTypeID(unsigned ID, Type *Ty); | 431 void reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty, |
432 ExtendedType::TypeKind WantedType); | |
433 | |
434 // Returns dummy undefined extended type. Used to guarantee | |
435 // that getUndefinedByTypeID always returns a valid extended type. | |
jvoung (off chromium)
2014/10/07 15:54:41
getUndefinedTypeByID?
Karl
2014/10/07 20:15:58
This method has been removed, so nothing to be fix
| |
436 ExtendedType *getDummyUndefinedExtendedType(); | |
293 | 437 |
294 // Reports that there is no corresponding ICE type for LLVMTy, and | 438 // Reports that there is no corresponding ICE type for LLVMTy, and |
295 // returns ICE::IceType_void. | 439 // returns ICE::IceType_void. |
296 Ice::Type convertToIceTypeError(Type *LLVMTy); | 440 Ice::Type convertToIceTypeError(Type *LLVMTy); |
297 }; | 441 }; |
298 | 442 |
299 Type *TopLevelParser::reportTypeIDAsUndefined(unsigned ID) { | 443 ExtendedType *TopLevelParser::getDummyUndefinedExtendedType() { |
300 std::string Buffer; | 444 ExtendedType *Ty = &DummyUndefinedExtendedType; |
301 raw_string_ostream StrBuf(Buffer); | 445 // Note: Reset to make sure we wipe out any changes (accidentally) |
Jim Stichnoth
2014/10/06 22:48:52
As we discussed in person, try to get rid of const
Karl
2014/10/07 20:15:59
Done.
| |
302 StrBuf << "Can't find type for type id: " << ID; | 446 // made to it. |
303 Error(StrBuf.str()); | 447 Ty->resetToUndefined(); |
304 // TODO(kschimpf) Remove error recovery once implementation complete. | |
305 Type *Ty = TypeConverter.convertToLLVMType(Ice::IceType_void); | |
306 // To reduce error messages, update type list if possible. | |
307 if (ID < TypeIDValues.size()) | |
308 TypeIDValues[ID] = Ty; | |
309 return Ty; | 448 return Ty; |
310 } | 449 } |
311 | 450 |
312 void TopLevelParser::reportBadSetTypeID(unsigned ID, Type *Ty) { | 451 void TopLevelParser::reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty, |
452 ExtendedType::TypeKind WantedType) { | |
313 std::string Buffer; | 453 std::string Buffer; |
314 raw_string_ostream StrBuf(Buffer); | 454 raw_string_ostream StrBuf(Buffer); |
315 if (ID >= TypeIDValues.size()) { | 455 if (Ty == nullptr) { |
316 StrBuf << "Type index " << ID << " out of range: can't install."; | 456 StrBuf << "Can't find extend type for type id: " << ID; |
317 } else { | 457 } else { |
318 // Must be case that index already defined. | 458 StrBuf << "Type id " << ID << " not " << WantedType << ". Found: " << *Ty; |
319 StrBuf << "Type index " << ID << " defined as " << *TypeIDValues[ID] | |
320 << " and " << *Ty << "."; | |
321 } | 459 } |
322 Error(StrBuf.str()); | 460 Error(StrBuf.str()); |
323 } | 461 } |
324 | 462 |
325 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { | 463 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { |
326 std::string Buffer; | 464 std::string Buffer; |
327 raw_string_ostream StrBuf(Buffer); | 465 raw_string_ostream StrBuf(Buffer); |
328 StrBuf << "Invalid LLVM type: " << *LLVMTy; | 466 StrBuf << "Invalid LLVM type: " << *LLVMTy; |
329 Error(StrBuf.str()); | 467 Error(StrBuf.str()); |
330 return Ice::IceType_void; | 468 return Ice::IceType_void; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 // Default implementation. Reports that the record is not | 511 // Default implementation. Reports that the record is not |
374 // understood. | 512 // understood. |
375 void ProcessRecord() override; | 513 void ProcessRecord() override; |
376 | 514 |
377 // Checks if the size of the record is Size. Return true if valid. | 515 // Checks if the size of the record is Size. Return true if valid. |
378 // Otherwise generates an error and returns false. | 516 // Otherwise generates an error and returns false. |
379 bool isValidRecordSize(unsigned Size, const char *RecordName) { | 517 bool isValidRecordSize(unsigned Size, const char *RecordName) { |
380 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 518 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
381 if (Values.size() == Size) | 519 if (Values.size() == Size) |
382 return true; | 520 return true; |
383 ReportRecordSizeError(Size, RecordName, NULL); | 521 ReportRecordSizeError(Size, RecordName, nullptr); |
384 return false; | 522 return false; |
385 } | 523 } |
386 | 524 |
387 // Checks if the size of the record is at least as large as the | 525 // Checks if the size of the record is at least as large as the |
388 // LowerLimit. Returns true if valid. Otherwise generates an error | 526 // LowerLimit. Returns true if valid. Otherwise generates an error |
389 // and returns false. | 527 // and returns false. |
390 bool isValidRecordSizeAtLeast(unsigned LowerLimit, const char *RecordName) { | 528 bool isValidRecordSizeAtLeast(unsigned LowerLimit, const char *RecordName) { |
391 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 529 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
392 if (Values.size() >= LowerLimit) | 530 if (Values.size() >= LowerLimit) |
393 return true; | 531 return true; |
(...skipping 17 matching lines...) Expand all Loading... | |
411 // valid. Otherwise generates an error and returns false. | 549 // valid. Otherwise generates an error and returns false. |
412 bool isValidRecordSizeInRange(unsigned LowerLimit, unsigned UpperLimit, | 550 bool isValidRecordSizeInRange(unsigned LowerLimit, unsigned UpperLimit, |
413 const char *RecordName) { | 551 const char *RecordName) { |
414 return isValidRecordSizeAtLeast(LowerLimit, RecordName) || | 552 return isValidRecordSizeAtLeast(LowerLimit, RecordName) || |
415 isValidRecordSizeAtMost(UpperLimit, RecordName); | 553 isValidRecordSizeAtMost(UpperLimit, RecordName); |
416 } | 554 } |
417 | 555 |
418 private: | 556 private: |
419 /// Generates a record size error. ExpectedSize is the number | 557 /// Generates a record size error. ExpectedSize is the number |
420 /// of elements expected. RecordName is the name of the kind of | 558 /// of elements expected. RecordName is the name of the kind of |
421 /// record that has incorrect size. ContextMessage (if not NULL) | 559 /// record that has incorrect size. ContextMessage (if not nullptr) |
422 /// is appended to "record expects" to describe how ExpectedSize | 560 /// is appended to "record expects" to describe how ExpectedSize |
423 /// should be interpreted. | 561 /// should be interpreted. |
424 void ReportRecordSizeError(unsigned ExpectedSize, const char *RecordName, | 562 void ReportRecordSizeError(unsigned ExpectedSize, const char *RecordName, |
425 const char *ContextMessage); | 563 const char *ContextMessage); |
426 }; | 564 }; |
427 | 565 |
428 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize, | 566 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize, |
429 const char *RecordName, | 567 const char *RecordName, |
430 const char *ContextMessage) { | 568 const char *ContextMessage) { |
431 std::string Buffer; | 569 std::string Buffer; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 : BlockParserBaseClass(BlockID, EnclosingParser), NextTypeId(0) {} | 605 : BlockParserBaseClass(BlockID, EnclosingParser), NextTypeId(0) {} |
468 | 606 |
469 ~TypesParser() override {} | 607 ~TypesParser() override {} |
470 | 608 |
471 private: | 609 private: |
472 // The type ID that will be associated with the next type defining | 610 // The type ID that will be associated with the next type defining |
473 // record in the types block. | 611 // record in the types block. |
474 unsigned NextTypeId; | 612 unsigned NextTypeId; |
475 | 613 |
476 void ProcessRecord() override; | 614 void ProcessRecord() override; |
615 | |
616 void setNextTypeIDAsSimpleType(Ice::Type Ty) { | |
617 Context->getUndefinedTypeByID(NextTypeId++)->setAsSimpleType(Ty); | |
618 } | |
477 }; | 619 }; |
478 | 620 |
479 void TypesParser::ProcessRecord() { | 621 void TypesParser::ProcessRecord() { |
480 Type *Ty = NULL; | |
481 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 622 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
482 switch (Record.GetCode()) { | 623 switch (Record.GetCode()) { |
483 case naclbitc::TYPE_CODE_NUMENTRY: | 624 case naclbitc::TYPE_CODE_NUMENTRY: |
484 // NUMENTRY: [numentries] | 625 // NUMENTRY: [numentries] |
485 if (!isValidRecordSize(1, "Type count")) | 626 if (!isValidRecordSize(1, "Type count")) |
486 return; | 627 return; |
487 Context->resizeTypeIDValues(Values[0]); | 628 Context->resizeTypeIDValues(Values[0]); |
488 return; | 629 return; |
489 case naclbitc::TYPE_CODE_VOID: | 630 case naclbitc::TYPE_CODE_VOID: |
490 // VOID | 631 // VOID |
491 if (!isValidRecordSize(0, "Type void")) | 632 if (!isValidRecordSize(0, "Type void")) |
492 return; | 633 return; |
493 Ty = Context->convertToLLVMType(Ice::IceType_void); | 634 setNextTypeIDAsSimpleType(Ice::IceType_void); |
494 break; | 635 return; |
495 case naclbitc::TYPE_CODE_FLOAT: | 636 case naclbitc::TYPE_CODE_FLOAT: |
496 // FLOAT | 637 // FLOAT |
497 if (!isValidRecordSize(0, "Type float")) | 638 if (!isValidRecordSize(0, "Type float")) |
498 return; | 639 return; |
499 Ty = Context->convertToLLVMType(Ice::IceType_f32); | 640 setNextTypeIDAsSimpleType(Ice::IceType_f32); |
500 break; | 641 return; |
501 case naclbitc::TYPE_CODE_DOUBLE: | 642 case naclbitc::TYPE_CODE_DOUBLE: |
502 // DOUBLE | 643 // DOUBLE |
503 if (!isValidRecordSize(0, "Type double")) | 644 if (!isValidRecordSize(0, "Type double")) |
504 return; | 645 return; |
505 Ty = Context->convertToLLVMType(Ice::IceType_f64); | 646 setNextTypeIDAsSimpleType(Ice::IceType_f64); |
506 break; | 647 return; |
507 case naclbitc::TYPE_CODE_INTEGER: | 648 case naclbitc::TYPE_CODE_INTEGER: |
508 // INTEGER: [width] | 649 // INTEGER: [width] |
509 if (!isValidRecordSize(1, "Type integer")) | 650 if (!isValidRecordSize(1, "Type integer")) |
510 return; | 651 return; |
511 Ty = Context->getLLVMIntegerType(Values[0]); | 652 switch (Values[0]) { |
512 if (Ty == NULL) { | 653 case 1: |
654 setNextTypeIDAsSimpleType(Ice::IceType_i1); | |
655 return; | |
656 case 8: | |
657 setNextTypeIDAsSimpleType(Ice::IceType_i8); | |
658 return; | |
659 case 16: | |
660 setNextTypeIDAsSimpleType(Ice::IceType_i16); | |
661 return; | |
662 case 32: | |
663 setNextTypeIDAsSimpleType(Ice::IceType_i32); | |
664 return; | |
665 case 64: | |
666 setNextTypeIDAsSimpleType(Ice::IceType_i64); | |
667 return; | |
668 default: | |
669 break; | |
670 } | |
671 { | |
513 std::string Buffer; | 672 std::string Buffer; |
514 raw_string_ostream StrBuf(Buffer); | 673 raw_string_ostream StrBuf(Buffer); |
515 StrBuf << "Type integer record with invalid bitsize: " << Values[0]; | 674 StrBuf << "Type integer record with invalid bitsize: " << Values[0]; |
516 Error(StrBuf.str()); | 675 Error(StrBuf.str()); |
517 // TODO(kschimpf) Remove error recovery once implementation complete. | |
518 // Fix type so that we can continue. | |
519 Ty = Context->convertToLLVMType(Ice::IceType_i32); | |
520 } | 676 } |
521 break; | 677 return; |
522 case naclbitc::TYPE_CODE_VECTOR: { | 678 case naclbitc::TYPE_CODE_VECTOR: { |
523 // VECTOR: [numelts, eltty] | 679 // VECTOR: [numelts, eltty] |
524 if (!isValidRecordSize(2, "Type vector")) | 680 if (!isValidRecordSize(2, "Type vector")) |
525 return; | 681 return; |
526 Type *BaseTy = Context->getTypeByID(Values[1]); | 682 Ice::Type BaseTy = Context->getSimpleTypeByID(Values[1]); |
527 Ty = Context->getLLVMVectorType(Values[0], | 683 Ice::SizeT Size = Values[0]; |
528 Context->convertToIceType(BaseTy)); | 684 switch (BaseTy) { |
529 if (Ty == NULL) { | 685 case Ice::IceType_i1: |
686 switch (Size) { | |
687 case 4: | |
688 setNextTypeIDAsSimpleType(Ice::IceType_v4i1); | |
689 return; | |
690 case 8: | |
691 setNextTypeIDAsSimpleType(Ice::IceType_v8i1); | |
692 return; | |
693 case 16: | |
694 setNextTypeIDAsSimpleType(Ice::IceType_v16i1); | |
695 return; | |
696 default: | |
697 break; | |
698 } | |
699 break; | |
700 case Ice::IceType_i8: | |
701 if (Size == 16) { | |
702 setNextTypeIDAsSimpleType(Ice::IceType_v16i8); | |
703 return; | |
704 } | |
705 break; | |
706 case Ice::IceType_i16: | |
707 if (Size == 8) { | |
708 setNextTypeIDAsSimpleType(Ice::IceType_v8i16); | |
709 return; | |
710 } | |
711 break; | |
712 case Ice::IceType_i32: | |
713 if (Size == 4) { | |
714 setNextTypeIDAsSimpleType(Ice::IceType_v4i32); | |
715 return; | |
716 } | |
717 break; | |
718 case Ice::IceType_f32: | |
719 if (Size == 4) { | |
720 setNextTypeIDAsSimpleType(Ice::IceType_v4f32); | |
721 return; | |
722 } | |
723 break; | |
724 default: | |
725 break; | |
726 } | |
727 { | |
530 std::string Buffer; | 728 std::string Buffer; |
531 raw_string_ostream StrBuf(Buffer); | 729 raw_string_ostream StrBuf(Buffer); |
532 StrBuf << "Invalid type vector record: <" << Values[0] << " x " << *BaseTy | 730 StrBuf << "Invalid type vector record: <" << Values[0] << " x " << BaseTy |
533 << ">"; | 731 << ">"; |
534 Error(StrBuf.str()); | 732 Error(StrBuf.str()); |
535 Ty = Context->convertToLLVMType(Ice::IceType_void); | |
536 } | 733 } |
537 break; | 734 return; |
538 } | 735 } |
539 case naclbitc::TYPE_CODE_FUNCTION: { | 736 case naclbitc::TYPE_CODE_FUNCTION: { |
540 // FUNCTION: [vararg, retty, paramty x N] | 737 // FUNCTION: [vararg, retty, paramty x N] |
541 if (!isValidRecordSizeAtLeast(2, "Type signature")) | 738 if (!isValidRecordSizeAtLeast(2, "Type signature")) |
542 return; | 739 return; |
543 SmallVector<Type *, 8> ArgTys; | 740 if (Values[0]) |
741 Error("Function type can't define varargs"); | |
742 ExtendedType *Ty = Context->getUndefinedTypeByID(NextTypeId++); | |
743 Ty->setAsFunctionType(); | |
744 FuncSigExtendedType *FcnTy = cast<FuncSigExtendedType>(Ty); | |
745 FcnTy->setReturnType(Context->getSimpleTypeByID(Values[1])); | |
544 for (unsigned i = 2, e = Values.size(); i != e; ++i) { | 746 for (unsigned i = 2, e = Values.size(); i != e; ++i) { |
545 ArgTys.push_back(Context->getTypeByID(Values[i])); | 747 // Check that type void not used as argument type. |
748 // Note: PNaCl restrictions can't be checked until we | |
749 // know the name, because we have to check for intrinsic signatures. | |
750 Ice::Type ArgTy = Context->getSimpleTypeByID(Values[i]); | |
751 if (ArgTy == Ice::IceType_void) { | |
752 std::string Buffer; | |
753 raw_string_ostream StrBuf(Buffer); | |
754 StrBuf << "Type for parameter " << (i - 1) | |
755 << " not valid. Found: " << ArgTy; | |
756 // TODO(kschimpf) Remove error recovery once implementation complete. | |
757 ArgTy = Ice::IceType_i32; | |
758 } | |
759 FcnTy->appendArgType(ArgTy); | |
546 } | 760 } |
547 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); | 761 return; |
548 break; | |
549 } | 762 } |
550 default: | 763 default: |
551 BlockParserBaseClass::ProcessRecord(); | 764 BlockParserBaseClass::ProcessRecord(); |
552 return; | 765 return; |
553 } | 766 } |
554 // If Ty not defined, assume error. Use void as filler. | 767 // We shoudn't reach here! |
555 if (Ty == NULL) | 768 assert(false); |
556 Ty = Context->convertToLLVMType(Ice::IceType_void); | |
557 Context->setTypeID(NextTypeId++, Ty); | |
558 } | 769 } |
559 | 770 |
560 /// Parses the globals block (i.e. global variables). | 771 /// Parses the globals block (i.e. global variables). |
561 class GlobalsParser : public BlockParserBaseClass { | 772 class GlobalsParser : public BlockParserBaseClass { |
562 public: | 773 public: |
563 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 774 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
564 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), | 775 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), |
565 Alignment(1), IsConstant(false) { | 776 Alignment(1), IsConstant(false) { |
566 NextGlobalID = Context->getNumFunctionIDs(); | 777 NextGlobalID = Context->getNumFunctionIDs(); |
567 } | 778 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 if (InitializersNeeded <= Initializers.size()) { | 840 if (InitializersNeeded <= Initializers.size()) { |
630 Error(std::string(RecordName) + | 841 Error(std::string(RecordName) + |
631 " record: Too many initializers, ignoring."); | 842 " record: Too many initializers, ignoring."); |
632 } | 843 } |
633 } | 844 } |
634 | 845 |
635 // Takes the initializers (and other parser state values) and | 846 // Takes the initializers (and other parser state values) and |
636 // installs a global variable (with the initializers) into the list | 847 // installs a global variable (with the initializers) into the list |
637 // of ValueIDs. | 848 // of ValueIDs. |
638 void installGlobalVar() { | 849 void installGlobalVar() { |
639 Constant *Init = NULL; | 850 Constant *Init = nullptr; |
640 switch (Initializers.size()) { | 851 switch (Initializers.size()) { |
641 case 0: | 852 case 0: |
642 Error("No initializer for global variable in global vars block"); | 853 Error("No initializer for global variable in global vars block"); |
643 return; | 854 return; |
644 case 1: | 855 case 1: |
645 Init = Initializers[0]; | 856 Init = Initializers[0]; |
646 break; | 857 break; |
647 default: | 858 default: |
648 Init = ConstantStruct::getAnon(Context->getLLVMContext(), Initializers, | 859 Init = ConstantStruct::getAnon(Context->getLLVMContext(), Initializers, |
649 true); | 860 true); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
733 Constant *Init = ConstantDataArray::get( | 944 Constant *Init = ConstantDataArray::get( |
734 Context->getLLVMContext(), ArrayRef<uint8_t>(Buf.data(), Buf.size())); | 945 Context->getLLVMContext(), ArrayRef<uint8_t>(Buf.data(), Buf.size())); |
735 Initializers.push_back(Init); | 946 Initializers.push_back(Init); |
736 break; | 947 break; |
737 } | 948 } |
738 case naclbitc::GLOBALVAR_RELOC: { | 949 case naclbitc::GLOBALVAR_RELOC: { |
739 // RELOC: [val, [addend]] | 950 // RELOC: [val, [addend]] |
740 if (!isValidRecordSizeInRange(1, 2, "Globals reloc")) | 951 if (!isValidRecordSizeInRange(1, 2, "Globals reloc")) |
741 return; | 952 return; |
742 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]); | 953 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]); |
743 if (BaseVal == NULL) { | 954 if (BaseVal == nullptr) { |
744 std::string Buffer; | 955 std::string Buffer; |
745 raw_string_ostream StrBuf(Buffer); | 956 raw_string_ostream StrBuf(Buffer); |
746 StrBuf << "Can't find global relocation value: " << Values[0]; | 957 StrBuf << "Can't find global relocation value: " << Values[0]; |
747 Error(StrBuf.str()); | 958 Error(StrBuf.str()); |
748 return; | 959 return; |
749 } | 960 } |
750 Type *IntPtrType = Context->convertToLLVMType(Context->getIcePointerType()); | 961 Type *IntPtrType = Context->convertToLLVMType(Context->getIcePointerType()); |
751 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); | 962 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); |
752 if (Values.size() == 2) { | 963 if (Values.size() == 2) { |
753 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1])); | 964 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1])); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
949 return Func->makeVariable(Ty); | 1160 return Func->makeVariable(Ty); |
950 } | 1161 } |
951 | 1162 |
952 // Generates the next available local variable using the given type. | 1163 // Generates the next available local variable using the given type. |
953 Ice::Variable *getNextInstVar(Ice::Type Ty) { | 1164 Ice::Variable *getNextInstVar(Ice::Type Ty) { |
954 assert(NextLocalInstIndex >= CachedNumGlobalValueIDs); | 1165 assert(NextLocalInstIndex >= CachedNumGlobalValueIDs); |
955 // Before creating one, see if a forwardtyperef has already defined it. | 1166 // Before creating one, see if a forwardtyperef has already defined it. |
956 uint32_t LocalIndex = NextLocalInstIndex - CachedNumGlobalValueIDs; | 1167 uint32_t LocalIndex = NextLocalInstIndex - CachedNumGlobalValueIDs; |
957 if (LocalIndex < LocalOperands.size()) { | 1168 if (LocalIndex < LocalOperands.size()) { |
958 Ice::Operand *Op = LocalOperands[LocalIndex]; | 1169 Ice::Operand *Op = LocalOperands[LocalIndex]; |
959 if (Op != NULL) { | 1170 if (Op != nullptr) { |
960 if (Ice::Variable *Var = dyn_cast<Ice::Variable>(Op)) { | 1171 if (Ice::Variable *Var = dyn_cast<Ice::Variable>(Op)) { |
961 if (Var->getType() == Ty) { | 1172 if (Var->getType() == Ty) { |
962 ++NextLocalInstIndex; | 1173 ++NextLocalInstIndex; |
963 return Var; | 1174 return Var; |
964 } | 1175 } |
965 } | 1176 } |
966 std::string Buffer; | 1177 std::string Buffer; |
967 raw_string_ostream StrBuf(Buffer); | 1178 raw_string_ostream StrBuf(Buffer); |
968 StrBuf << "Illegal forward referenced instruction (" | 1179 StrBuf << "Illegal forward referenced instruction (" |
969 << NextLocalInstIndex << "): " << *Op; | 1180 << NextLocalInstIndex << "): " << *Op; |
(...skipping 30 matching lines...) Expand all Loading... | |
1000 } | 1211 } |
1001 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; | 1212 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; |
1002 if (LocalIndex >= LocalOperands.size()) { | 1213 if (LocalIndex >= LocalOperands.size()) { |
1003 std::string Buffer; | 1214 std::string Buffer; |
1004 raw_string_ostream StrBuf(Buffer); | 1215 raw_string_ostream StrBuf(Buffer); |
1005 StrBuf << "Value index " << Index << " not defined!"; | 1216 StrBuf << "Value index " << Index << " not defined!"; |
1006 Error(StrBuf.str()); | 1217 Error(StrBuf.str()); |
1007 report_fatal_error("Unable to continue"); | 1218 report_fatal_error("Unable to continue"); |
1008 } | 1219 } |
1009 Ice::Operand *Op = LocalOperands[LocalIndex]; | 1220 Ice::Operand *Op = LocalOperands[LocalIndex]; |
1010 if (Op == NULL) { | 1221 if (Op == nullptr) { |
1011 std::string Buffer; | 1222 std::string Buffer; |
1012 raw_string_ostream StrBuf(Buffer); | 1223 raw_string_ostream StrBuf(Buffer); |
1013 StrBuf << "Value index " << Index << " not defined!"; | 1224 StrBuf << "Value index " << Index << " not defined!"; |
1014 Error(StrBuf.str()); | 1225 Error(StrBuf.str()); |
1015 report_fatal_error("Unable to continue"); | 1226 report_fatal_error("Unable to continue"); |
1016 } | 1227 } |
1017 return Op; | 1228 return Op; |
1018 } | 1229 } |
1019 | 1230 |
1020 // Sets element Index (in the local operands list) to Op. | 1231 // Sets element Index (in the local operands list) to Op. |
1021 void setOperand(uint32_t Index, Ice::Operand *Op) { | 1232 void setOperand(uint32_t Index, Ice::Operand *Op) { |
1022 assert(Op); | 1233 assert(Op); |
1023 // Check if simple push works. | 1234 // Check if simple push works. |
1024 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; | 1235 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; |
1025 if (LocalIndex == LocalOperands.size()) { | 1236 if (LocalIndex == LocalOperands.size()) { |
1026 LocalOperands.push_back(Op); | 1237 LocalOperands.push_back(Op); |
1027 return; | 1238 return; |
1028 } | 1239 } |
1029 | 1240 |
1030 // Must be forward reference, expand vector to accommodate. | 1241 // Must be forward reference, expand vector to accommodate. |
1031 if (LocalIndex >= LocalOperands.size()) | 1242 if (LocalIndex >= LocalOperands.size()) |
1032 LocalOperands.resize(LocalIndex + 1); | 1243 LocalOperands.resize(LocalIndex + 1); |
1033 | 1244 |
1034 // If element not defined, set it. | 1245 // If element not defined, set it. |
1035 Ice::Operand *OldOp = LocalOperands[LocalIndex]; | 1246 Ice::Operand *OldOp = LocalOperands[LocalIndex]; |
1036 if (OldOp == NULL) { | 1247 if (OldOp == nullptr) { |
1037 LocalOperands[LocalIndex] = Op; | 1248 LocalOperands[LocalIndex] = Op; |
1038 return; | 1249 return; |
1039 } | 1250 } |
1040 | 1251 |
1041 // See if forward reference matches. | 1252 // See if forward reference matches. |
1042 if (OldOp == Op) | 1253 if (OldOp == Op) |
1043 return; | 1254 return; |
1044 | 1255 |
1045 // Error has occurred. | 1256 // Error has occurred. |
1046 std::string Buffer; | 1257 std::string Buffer; |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1466 return; | 1677 return; |
1467 CurrentNode->appendInst(Ice::InstArithmetic::create( | 1678 CurrentNode->appendInst(Ice::InstArithmetic::create( |
1468 Func, Opcode, getNextInstVar(Type1), Op1, Op2)); | 1679 Func, Opcode, getNextInstVar(Type1), Op1, Op2)); |
1469 break; | 1680 break; |
1470 } | 1681 } |
1471 case naclbitc::FUNC_CODE_INST_CAST: { | 1682 case naclbitc::FUNC_CODE_INST_CAST: { |
1472 // CAST: [opval, destty, castopc] | 1683 // CAST: [opval, destty, castopc] |
1473 if (!isValidRecordSize(3, "function block cast")) | 1684 if (!isValidRecordSize(3, "function block cast")) |
1474 return; | 1685 return; |
1475 Ice::Operand *Src = getRelativeOperand(Values[0], BaseIndex); | 1686 Ice::Operand *Src = getRelativeOperand(Values[0], BaseIndex); |
1476 Type *CastType = Context->getTypeByID(Values[1]); | 1687 Ice::Type CastType = Context->getSimpleTypeByID(Values[1]); |
1477 Instruction::CastOps LLVMCastOp; | 1688 Instruction::CastOps LLVMCastOp; |
1478 Ice::InstCast::OpKind CastKind; | 1689 Ice::InstCast::OpKind CastKind; |
1479 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) || | 1690 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) || |
1480 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) { | 1691 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) { |
1481 std::string Buffer; | 1692 std::string Buffer; |
1482 raw_string_ostream StrBuf(Buffer); | 1693 raw_string_ostream StrBuf(Buffer); |
1483 StrBuf << "Cast opcode not understood: " << Values[2]; | 1694 StrBuf << "Cast opcode not understood: " << Values[2]; |
1484 Error(StrBuf.str()); | 1695 Error(StrBuf.str()); |
1485 return; | 1696 return; |
1486 } | 1697 } |
1487 Type *SrcType = Context->convertToLLVMType(Src->getType()); | 1698 Ice::Type SrcType = Src->getType(); |
1488 if (!CastInst::castIsValid(LLVMCastOp, SrcType, CastType)) { | 1699 if (!CastInst::castIsValid(LLVMCastOp, Context->convertToLLVMType(SrcType), |
1700 Context->convertToLLVMType(CastType))) { | |
1489 std::string Buffer; | 1701 std::string Buffer; |
1490 raw_string_ostream StrBuf(Buffer); | 1702 raw_string_ostream StrBuf(Buffer); |
1491 StrBuf << "Illegal cast: " << Instruction::getOpcodeName(LLVMCastOp) | 1703 StrBuf << "Illegal cast: " << Instruction::getOpcodeName(LLVMCastOp) |
1492 << " " << *SrcType << " to " << *CastType; | 1704 << " " << SrcType << " to " << CastType; |
1493 Error(StrBuf.str()); | 1705 Error(StrBuf.str()); |
1494 return; | 1706 return; |
1495 } | 1707 } |
1496 CurrentNode->appendInst(Ice::InstCast::create( | 1708 CurrentNode->appendInst( |
1497 Func, CastKind, getNextInstVar(Context->convertToIceType(CastType)), | 1709 Ice::InstCast::create(Func, CastKind, getNextInstVar(CastType), Src)); |
1498 Src)); | |
1499 break; | 1710 break; |
1500 } | 1711 } |
1501 case naclbitc::FUNC_CODE_INST_VSELECT: { | 1712 case naclbitc::FUNC_CODE_INST_VSELECT: { |
1502 // VSELECT: [opval, opval, pred] | 1713 // VSELECT: [opval, opval, pred] |
1503 Ice::Operand *ThenVal = getRelativeOperand(Values[0], BaseIndex); | 1714 Ice::Operand *ThenVal = getRelativeOperand(Values[0], BaseIndex); |
1504 Ice::Type ThenType = ThenVal->getType(); | 1715 Ice::Type ThenType = ThenVal->getType(); |
1505 Ice::Operand *ElseVal = getRelativeOperand(Values[1], BaseIndex); | 1716 Ice::Operand *ElseVal = getRelativeOperand(Values[1], BaseIndex); |
1506 Ice::Type ElseType = ElseVal->getType(); | 1717 Ice::Type ElseType = ElseVal->getType(); |
1507 if (ThenType != ElseType) { | 1718 if (ThenType != ElseType) { |
1508 std::string Buffer; | 1719 std::string Buffer; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1671 CurrentNode->appendInst( | 1882 CurrentNode->appendInst( |
1672 Ice::InstRet::create(Func, getRelativeOperand(Values[0], BaseIndex))); | 1883 Ice::InstRet::create(Func, getRelativeOperand(Values[0], BaseIndex))); |
1673 } | 1884 } |
1674 InstIsTerminating = true; | 1885 InstIsTerminating = true; |
1675 break; | 1886 break; |
1676 } | 1887 } |
1677 case naclbitc::FUNC_CODE_INST_BR: { | 1888 case naclbitc::FUNC_CODE_INST_BR: { |
1678 if (Values.size() == 1) { | 1889 if (Values.size() == 1) { |
1679 // BR: [bb#] | 1890 // BR: [bb#] |
1680 Ice::CfgNode *Block = getBranchBasicBlock(Values[0]); | 1891 Ice::CfgNode *Block = getBranchBasicBlock(Values[0]); |
1681 if (Block == NULL) | 1892 if (Block == nullptr) |
1682 return; | 1893 return; |
1683 CurrentNode->appendInst(Ice::InstBr::create(Func, Block)); | 1894 CurrentNode->appendInst(Ice::InstBr::create(Func, Block)); |
1684 } else { | 1895 } else { |
1685 // BR: [bb#, bb#, opval] | 1896 // BR: [bb#, bb#, opval] |
1686 if (!isValidRecordSize(3, "function block branch")) | 1897 if (!isValidRecordSize(3, "function block branch")) |
1687 return; | 1898 return; |
1688 Ice::Operand *Cond = getRelativeOperand(Values[2], BaseIndex); | 1899 Ice::Operand *Cond = getRelativeOperand(Values[2], BaseIndex); |
1689 if (Cond->getType() != Ice::IceType_i1) { | 1900 if (Cond->getType() != Ice::IceType_i1) { |
1690 std::string Buffer; | 1901 std::string Buffer; |
1691 raw_string_ostream StrBuf(Buffer); | 1902 raw_string_ostream StrBuf(Buffer); |
1692 StrBuf << "Branch condition " << *Cond << " not i1. Found: " | 1903 StrBuf << "Branch condition " << *Cond << " not i1. Found: " |
1693 << Cond->getType(); | 1904 << Cond->getType(); |
1694 Error(StrBuf.str()); | 1905 Error(StrBuf.str()); |
1695 return; | 1906 return; |
1696 } | 1907 } |
1697 Ice::CfgNode *ThenBlock = getBranchBasicBlock(Values[0]); | 1908 Ice::CfgNode *ThenBlock = getBranchBasicBlock(Values[0]); |
1698 Ice::CfgNode *ElseBlock = getBranchBasicBlock(Values[1]); | 1909 Ice::CfgNode *ElseBlock = getBranchBasicBlock(Values[1]); |
1699 if (ThenBlock == NULL || ElseBlock == NULL) | 1910 if (ThenBlock == nullptr || ElseBlock == nullptr) |
1700 return; | 1911 return; |
1701 CurrentNode->appendInst( | 1912 CurrentNode->appendInst( |
1702 Ice::InstBr::create(Func, Cond, ThenBlock, ElseBlock)); | 1913 Ice::InstBr::create(Func, Cond, ThenBlock, ElseBlock)); |
1703 } | 1914 } |
1704 InstIsTerminating = true; | 1915 InstIsTerminating = true; |
1705 break; | 1916 break; |
1706 } | 1917 } |
1707 case naclbitc::FUNC_CODE_INST_SWITCH: { | 1918 case naclbitc::FUNC_CODE_INST_SWITCH: { |
1708 // SWITCH: [Condty, Cond, BbIndex, NumCases Case ...] | 1919 // SWITCH: [Condty, Cond, BbIndex, NumCases Case ...] |
1709 // where Case = [1, 1, Value, BbIndex]. | 1920 // where Case = [1, 1, Value, BbIndex]. |
1710 // | 1921 // |
1711 // Note: Unlike most instructions, we don't infer the type of | 1922 // Note: Unlike most instructions, we don't infer the type of |
1712 // Cond, but provide it as a separate field. There are also | 1923 // Cond, but provide it as a separate field. There are also |
1713 // unnecesary data fields (i.e. constants 1). These were not | 1924 // unnecesary data fields (i.e. constants 1). These were not |
1714 // cleaned up in PNaCl bitcode because the bitcode format was | 1925 // cleaned up in PNaCl bitcode because the bitcode format was |
1715 // already frozen when the problem was noticed. | 1926 // already frozen when the problem was noticed. |
1716 if (!isValidRecordSizeAtLeast(4, "function block switch")) | 1927 if (!isValidRecordSizeAtLeast(4, "function block switch")) |
1717 return; | 1928 return; |
1718 Ice::Type CondTy = | 1929 Ice::Type CondTy = Context->getSimpleTypeByID(Values[0]); |
1719 Context->convertToIceType(Context->getTypeByID(Values[0])); | |
1720 if (!Ice::isScalarIntegerType(CondTy)) { | 1930 if (!Ice::isScalarIntegerType(CondTy)) { |
1721 std::string Buffer; | 1931 std::string Buffer; |
1722 raw_string_ostream StrBuf(Buffer); | 1932 raw_string_ostream StrBuf(Buffer); |
1723 StrBuf << "Case condition must be non-wide integer. Found: " << CondTy; | 1933 StrBuf << "Case condition must be non-wide integer. Found: " << CondTy; |
1724 Error(StrBuf.str()); | 1934 Error(StrBuf.str()); |
1725 return; | 1935 return; |
1726 } | 1936 } |
1727 Ice::SizeT BitWidth = Ice::getScalarIntBitWidth(CondTy); | 1937 Ice::SizeT BitWidth = Ice::getScalarIntBitWidth(CondTy); |
1728 Ice::Operand *Cond = getRelativeOperand(Values[1], BaseIndex); | 1938 Ice::Operand *Cond = getRelativeOperand(Values[1], BaseIndex); |
1729 if (CondTy != Cond->getType()) { | 1939 if (CondTy != Cond->getType()) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1777 if (!isValidRecordSizeAtLeast(3, "function block phi")) | 1987 if (!isValidRecordSizeAtLeast(3, "function block phi")) |
1778 return; | 1988 return; |
1779 if ((Values.size() & 0x1) == 0) { | 1989 if ((Values.size() & 0x1) == 0) { |
1780 // Not an odd number of values. | 1990 // Not an odd number of values. |
1781 std::string Buffer; | 1991 std::string Buffer; |
1782 raw_string_ostream StrBuf(Buffer); | 1992 raw_string_ostream StrBuf(Buffer); |
1783 StrBuf << "function block phi record size not valid: " << Values.size(); | 1993 StrBuf << "function block phi record size not valid: " << Values.size(); |
1784 Error(StrBuf.str()); | 1994 Error(StrBuf.str()); |
1785 return; | 1995 return; |
1786 } | 1996 } |
1787 Ice::Type Ty = Context->convertToIceType(Context->getTypeByID(Values[0])); | 1997 Ice::Type Ty = Context->getSimpleTypeByID(Values[0]); |
1788 if (Ty == Ice::IceType_void) { | 1998 if (Ty == Ice::IceType_void) { |
1789 Error("Phi record using type void not allowed"); | 1999 Error("Phi record using type void not allowed"); |
1790 return; | 2000 return; |
1791 } | 2001 } |
1792 Ice::Variable *Dest = getNextInstVar(Ty); | 2002 Ice::Variable *Dest = getNextInstVar(Ty); |
1793 Ice::InstPhi *Phi = Ice::InstPhi::create(Func, Values.size() >> 1, Dest); | 2003 Ice::InstPhi *Phi = Ice::InstPhi::create(Func, Values.size() >> 1, Dest); |
1794 for (unsigned i = 1; i < Values.size(); i += 2) { | 2004 for (unsigned i = 1; i < Values.size(); i += 2) { |
1795 Ice::Operand *Op = | 2005 Ice::Operand *Op = |
1796 getRelativeOperand(NaClDecodeSignRotatedValue(Values[i]), BaseIndex); | 2006 getRelativeOperand(NaClDecodeSignRotatedValue(Values[i]), BaseIndex); |
1797 if (Op->getType() != Ty) { | 2007 if (Op->getType() != Ty) { |
(...skipping 30 matching lines...) Expand all Loading... | |
1828 } | 2038 } |
1829 case naclbitc::FUNC_CODE_INST_LOAD: { | 2039 case naclbitc::FUNC_CODE_INST_LOAD: { |
1830 // LOAD: [address, align, ty] | 2040 // LOAD: [address, align, ty] |
1831 if (!isValidRecordSize(3, "function block load")) | 2041 if (!isValidRecordSize(3, "function block load")) |
1832 return; | 2042 return; |
1833 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); | 2043 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); |
1834 if (!isValidPointerType(Address, "Load")) | 2044 if (!isValidPointerType(Address, "Load")) |
1835 return; | 2045 return; |
1836 unsigned Alignment; | 2046 unsigned Alignment; |
1837 extractAlignment("Load", Values[1], Alignment); | 2047 extractAlignment("Load", Values[1], Alignment); |
1838 Ice::Type Ty = Context->convertToIceType(Context->getTypeByID(Values[2])); | 2048 Ice::Type Ty = Context->getSimpleTypeByID(Values[2]); |
1839 if (!isValidLoadStoreAlignment(Alignment, Ty, "Load")) | 2049 if (!isValidLoadStoreAlignment(Alignment, Ty, "Load")) |
1840 return; | 2050 return; |
1841 CurrentNode->appendInst( | 2051 CurrentNode->appendInst( |
1842 Ice::InstLoad::create(Func, getNextInstVar(Ty), Address, Alignment)); | 2052 Ice::InstLoad::create(Func, getNextInstVar(Ty), Address, Alignment)); |
1843 break; | 2053 break; |
1844 } | 2054 } |
1845 case naclbitc::FUNC_CODE_INST_STORE: { | 2055 case naclbitc::FUNC_CODE_INST_STORE: { |
1846 // STORE: [address, value, align] | 2056 // STORE: [address, value, align] |
1847 if (!isValidRecordSize(3, "function block store")) | 2057 if (!isValidRecordSize(3, "function block store")) |
1848 return; | 2058 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1890 << " not understood."; | 2100 << " not understood."; |
1891 Error(StrBuf.str()); | 2101 Error(StrBuf.str()); |
1892 return; | 2102 return; |
1893 } | 2103 } |
1894 bool IsTailCall = static_cast<bool>(CCInfo & 1); | 2104 bool IsTailCall = static_cast<bool>(CCInfo & 1); |
1895 | 2105 |
1896 // Extract out the called function and its return type. | 2106 // Extract out the called function and its return type. |
1897 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex); | 2107 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex); |
1898 Ice::Operand *Callee = getOperand(CalleeIndex); | 2108 Ice::Operand *Callee = getOperand(CalleeIndex); |
1899 Ice::Type ReturnType = Ice::IceType_void; | 2109 Ice::Type ReturnType = Ice::IceType_void; |
1900 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = NULL; | 2110 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr; |
1901 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { | 2111 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { |
1902 Function *Fcn = | 2112 Function *Fcn = |
1903 dyn_cast<Function>(Context->getGlobalValueByID(CalleeIndex)); | 2113 dyn_cast<Function>(Context->getGlobalValueByID(CalleeIndex)); |
1904 if (Fcn == NULL) { | 2114 if (Fcn == nullptr) { |
1905 std::string Buffer; | 2115 std::string Buffer; |
1906 raw_string_ostream StrBuf(Buffer); | 2116 raw_string_ostream StrBuf(Buffer); |
1907 StrBuf << "Function call to non-function: " << *Callee; | 2117 StrBuf << "Function call to non-function: " << *Callee; |
1908 Error(StrBuf.str()); | 2118 Error(StrBuf.str()); |
1909 return; | 2119 return; |
1910 } | 2120 } |
1911 | 2121 |
1912 FunctionType *FcnTy = Fcn->getFunctionType(); | 2122 FunctionType *FcnTy = Fcn->getFunctionType(); |
1913 ReturnType = Context->convertToIceType(FcnTy->getReturnType()); | 2123 ReturnType = Context->convertToIceType(FcnTy->getReturnType()); |
1914 | 2124 |
1915 // Check if this direct call is to an Intrinsic (starts with "llvm.") | 2125 // Check if this direct call is to an Intrinsic (starts with "llvm.") |
1916 static Ice::IceString LLVMPrefix("llvm."); | 2126 static Ice::IceString LLVMPrefix("llvm."); |
1917 Ice::IceString Name = Fcn->getName(); | 2127 Ice::IceString Name = Fcn->getName(); |
1918 if (isStringPrefix(Name, LLVMPrefix)) { | 2128 if (isStringPrefix(Name, LLVMPrefix)) { |
1919 Ice::IceString Suffix = Name.substr(LLVMPrefix.size()); | 2129 Ice::IceString Suffix = Name.substr(LLVMPrefix.size()); |
1920 IntrinsicInfo = | 2130 IntrinsicInfo = |
1921 getTranslator().getContext()->getIntrinsicsInfo().find(Suffix); | 2131 getTranslator().getContext()->getIntrinsicsInfo().find(Suffix); |
1922 if (!IntrinsicInfo) { | 2132 if (!IntrinsicInfo) { |
1923 std::string Buffer; | 2133 std::string Buffer; |
1924 raw_string_ostream StrBuf(Buffer); | 2134 raw_string_ostream StrBuf(Buffer); |
1925 StrBuf << "Invalid PNaCl intrinsic call to " << Name; | 2135 StrBuf << "Invalid PNaCl intrinsic call to " << Name; |
1926 Error(StrBuf.str()); | 2136 Error(StrBuf.str()); |
1927 return; | 2137 return; |
1928 } | 2138 } |
1929 } | 2139 } |
1930 } else { | 2140 } else { |
1931 ReturnType = Context->convertToIceType(Context->getTypeByID(Values[2])); | 2141 ReturnType = Context->getSimpleTypeByID(Values[2]); |
1932 } | 2142 } |
1933 | 2143 |
1934 // Create the call instruction. | 2144 // Create the call instruction. |
1935 Ice::Variable *Dest = | 2145 Ice::Variable *Dest = (ReturnType == Ice::IceType_void) |
1936 (ReturnType == Ice::IceType_void) ? NULL : getNextInstVar(ReturnType); | 2146 ? nullptr |
2147 : getNextInstVar(ReturnType); | |
1937 Ice::SizeT NumParams = Values.size() - ParamsStartIndex; | 2148 Ice::SizeT NumParams = Values.size() - ParamsStartIndex; |
1938 Ice::InstCall *Inst = NULL; | 2149 Ice::InstCall *Inst = nullptr; |
1939 if (IntrinsicInfo) { | 2150 if (IntrinsicInfo) { |
1940 Inst = | 2151 Inst = |
1941 Ice::InstIntrinsicCall::create(Func, NumParams, Dest, Callee, | 2152 Ice::InstIntrinsicCall::create(Func, NumParams, Dest, Callee, |
1942 IntrinsicInfo->Info); | 2153 IntrinsicInfo->Info); |
1943 } else { | 2154 } else { |
1944 Inst = Ice::InstCall::create(Func, NumParams, Dest, Callee, IsTailCall); | 2155 Inst = Ice::InstCall::create(Func, NumParams, Dest, Callee, IsTailCall); |
1945 } | 2156 } |
1946 | 2157 |
1947 // Add parameters. | 2158 // Add parameters. |
1948 for (Ice::SizeT ParamIndex = 0; ParamIndex < NumParams; ++ParamIndex) { | 2159 for (Ice::SizeT ParamIndex = 0; ParamIndex < NumParams; ++ParamIndex) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1992 } | 2203 } |
1993 } | 2204 } |
1994 | 2205 |
1995 CurrentNode->appendInst(Inst); | 2206 CurrentNode->appendInst(Inst); |
1996 return; | 2207 return; |
1997 } | 2208 } |
1998 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: { | 2209 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: { |
1999 // FORWARDTYPEREF: [opval, ty] | 2210 // FORWARDTYPEREF: [opval, ty] |
2000 if (!isValidRecordSize(2, "function block forward type ref")) | 2211 if (!isValidRecordSize(2, "function block forward type ref")) |
2001 return; | 2212 return; |
2002 setOperand(Values[0], createInstVar(Context->convertToIceType( | 2213 setOperand(Values[0], createInstVar(Context->getSimpleTypeByID(Values[1]))); |
2003 Context->getTypeByID(Values[1])))); | |
2004 break; | 2214 break; |
2005 } | 2215 } |
2006 default: | 2216 default: |
2007 // Generate error message! | 2217 // Generate error message! |
2008 BlockParserBaseClass::ProcessRecord(); | 2218 BlockParserBaseClass::ProcessRecord(); |
2009 break; | 2219 break; |
2010 } | 2220 } |
2011 } | 2221 } |
2012 | 2222 |
2013 /// Parses constants within a function block. | 2223 /// Parses constants within a function block. |
(...skipping 28 matching lines...) Expand all Loading... | |
2042 } | 2252 } |
2043 }; | 2253 }; |
2044 | 2254 |
2045 void ConstantsParser::ProcessRecord() { | 2255 void ConstantsParser::ProcessRecord() { |
2046 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 2256 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
2047 switch (Record.GetCode()) { | 2257 switch (Record.GetCode()) { |
2048 case naclbitc::CST_CODE_SETTYPE: { | 2258 case naclbitc::CST_CODE_SETTYPE: { |
2049 // SETTYPE: [typeid] | 2259 // SETTYPE: [typeid] |
2050 if (!isValidRecordSize(1, "constants block set type")) | 2260 if (!isValidRecordSize(1, "constants block set type")) |
2051 return; | 2261 return; |
2052 NextConstantType = | 2262 NextConstantType = Context->getSimpleTypeByID(Values[0]); |
2053 Context->convertToIceType(Context->getTypeByID(Values[0])); | |
2054 if (NextConstantType == Ice::IceType_void) | 2263 if (NextConstantType == Ice::IceType_void) |
2055 Error("constants block set type not allowed for void type"); | 2264 Error("constants block set type not allowed for void type"); |
2056 return; | 2265 return; |
2057 } | 2266 } |
2058 case naclbitc::CST_CODE_UNDEF: { | 2267 case naclbitc::CST_CODE_UNDEF: { |
2059 // UNDEF | 2268 // UNDEF |
2060 if (!isValidRecordSize(0, "constants block undef")) | 2269 if (!isValidRecordSize(0, "constants block undef")) |
2061 return; | 2270 return; |
2062 if (!isValidNextConstantType()) | 2271 if (!isValidNextConstantType()) |
2063 return; | 2272 return; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2248 | 2457 |
2249 ~ModuleValuesymtabParser() override {} | 2458 ~ModuleValuesymtabParser() override {} |
2250 | 2459 |
2251 private: | 2460 private: |
2252 void setValueName(uint64_t Index, StringType &Name) override; | 2461 void setValueName(uint64_t Index, StringType &Name) override; |
2253 void setBbName(uint64_t Index, StringType &Name) override; | 2462 void setBbName(uint64_t Index, StringType &Name) override; |
2254 }; | 2463 }; |
2255 | 2464 |
2256 void ModuleValuesymtabParser::setValueName(uint64_t Index, StringType &Name) { | 2465 void ModuleValuesymtabParser::setValueName(uint64_t Index, StringType &Name) { |
2257 Value *V = Context->getGlobalValueByID(Index); | 2466 Value *V = Context->getGlobalValueByID(Index); |
2258 if (V == NULL) { | 2467 if (V == nullptr) { |
2259 std::string Buffer; | 2468 std::string Buffer; |
2260 raw_string_ostream StrBuf(Buffer); | 2469 raw_string_ostream StrBuf(Buffer); |
2261 StrBuf << "Invalid global address ID in valuesymtab: " << Index; | 2470 StrBuf << "Invalid global address ID in valuesymtab: " << Index; |
2262 Error(StrBuf.str()); | 2471 Error(StrBuf.str()); |
2263 return; | 2472 return; |
2264 } | 2473 } |
2265 V->setName(StringRef(Name.data(), Name.size())); | 2474 V->setName(StringRef(Name.data(), Name.size())); |
2266 } | 2475 } |
2267 | 2476 |
2268 void ModuleValuesymtabParser::setBbName(uint64_t Index, StringType &Name) { | 2477 void ModuleValuesymtabParser::setBbName(uint64_t Index, StringType &Name) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2312 raw_string_ostream StrBuf(Buffer); | 2521 raw_string_ostream StrBuf(Buffer); |
2313 StrBuf << "Unknown bitstream version: " << Version; | 2522 StrBuf << "Unknown bitstream version: " << Version; |
2314 Error(StrBuf.str()); | 2523 Error(StrBuf.str()); |
2315 } | 2524 } |
2316 return; | 2525 return; |
2317 } | 2526 } |
2318 case naclbitc::MODULE_CODE_FUNCTION: { | 2527 case naclbitc::MODULE_CODE_FUNCTION: { |
2319 // FUNCTION: [type, callingconv, isproto, linkage] | 2528 // FUNCTION: [type, callingconv, isproto, linkage] |
2320 if (!isValidRecordSize(4, "Function heading")) | 2529 if (!isValidRecordSize(4, "Function heading")) |
2321 return; | 2530 return; |
2322 Type *Ty = Context->getTypeByID(Values[0]); | 2531 const Ice::FuncSigType &Ty = Context->getFuncSigTypeByID(Values[0]); |
2323 FunctionType *FTy = dyn_cast<FunctionType>(Ty); | |
2324 if (FTy == NULL) { | |
2325 std::string Buffer; | |
2326 raw_string_ostream StrBuf(Buffer); | |
2327 StrBuf << "Function heading expects function type. Found: " << Ty; | |
2328 Error(StrBuf.str()); | |
2329 return; | |
2330 } | |
2331 CallingConv::ID CallingConv; | 2532 CallingConv::ID CallingConv; |
2332 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) { | 2533 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) { |
2333 std::string Buffer; | 2534 std::string Buffer; |
2334 raw_string_ostream StrBuf(Buffer); | 2535 raw_string_ostream StrBuf(Buffer); |
2335 StrBuf << "Function heading has unknown calling convention: " | 2536 StrBuf << "Function heading has unknown calling convention: " |
2336 << Values[1]; | 2537 << Values[1]; |
2337 Error(StrBuf.str()); | 2538 Error(StrBuf.str()); |
2338 return; | 2539 return; |
2339 } | 2540 } |
2340 GlobalValue::LinkageTypes Linkage; | 2541 GlobalValue::LinkageTypes Linkage; |
2341 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) { | 2542 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) { |
2342 std::string Buffer; | 2543 std::string Buffer; |
2343 raw_string_ostream StrBuf(Buffer); | 2544 raw_string_ostream StrBuf(Buffer); |
2344 StrBuf << "Function heading has unknown linkage. Found " << Values[3]; | 2545 StrBuf << "Function heading has unknown linkage. Found " << Values[3]; |
2345 Error(StrBuf.str()); | 2546 Error(StrBuf.str()); |
2346 return; | 2547 return; |
2347 } | 2548 } |
2348 Function *Func = Function::Create(FTy, Linkage, "", Context->getModule()); | 2549 SmallVector<Type *, 8> ArgTys; |
2550 for (Ice::Type ArgType : Ty.getArgList()) { | |
2551 ArgTys.push_back(Context->convertToLLVMType(ArgType)); | |
2552 } | |
2553 Function *Func = Function::Create( | |
2554 FunctionType::get(Context->convertToLLVMType(Ty.getReturnType()), | |
2555 ArgTys, false), | |
2556 Linkage, "", Context->getModule()); | |
2349 Func->setCallingConv(CallingConv); | 2557 Func->setCallingConv(CallingConv); |
2350 if (Values[2] == 0) | 2558 if (Values[2] == 0) |
2351 Context->setNextValueIDAsImplementedFunction(); | 2559 Context->setNextValueIDAsImplementedFunction(); |
2352 Context->setNextFunctionID(Func); | 2560 Context->setNextFunctionID(Func); |
2353 // TODO(kschimpf) verify if Func matches PNaCl ABI. | 2561 // TODO(kschimpf) verify if Func matches PNaCl ABI. |
2354 return; | 2562 return; |
2355 } | 2563 } |
2356 default: | 2564 default: |
2357 BlockParserBaseClass::ProcessRecord(); | 2565 BlockParserBaseClass::ProcessRecord(); |
2358 return; | 2566 return; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2417 | 2625 |
2418 if (TopLevelBlocks != 1) { | 2626 if (TopLevelBlocks != 1) { |
2419 errs() << IRFilename | 2627 errs() << IRFilename |
2420 << ": Contains more than one module. Found: " << TopLevelBlocks | 2628 << ": Contains more than one module. Found: " << TopLevelBlocks |
2421 << "\n"; | 2629 << "\n"; |
2422 ErrorStatus = true; | 2630 ErrorStatus = true; |
2423 } | 2631 } |
2424 } | 2632 } |
2425 | 2633 |
2426 } // end of namespace Ice | 2634 } // end of namespace Ice |
OLD | NEW |