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

Side by Side Diff: src/PNaClTranslator.cpp

Issue 625243002: Convert Subzero's bitcode reader to generate ICE types. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix comment. Created 6 years, 2 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
« src/IceTypes.h ('K') | « src/IceTypes.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 a (simple) type, or a function type
54 // signature.
55 class ExtendedType {
56 ExtendedType(const ExtendedType &) = delete;
57 ExtendedType &operator=(const ExtendedType &) = delete;
58
59 public:
60 // Discriminator for LLVM-style RTTI.
61 enum ExtendedTypeKind { SimpleExtendedKind, FcnSigExtendedKind };
62 virtual ~ExtendedType() {}
63 ExtendedTypeKind getKind() const { return Kind; }
64 virtual void Print(Ice::Ostream &Stream) const = 0;
65
66 protected:
67 explicit ExtendedType(ExtendedTypeKind Kind) : Kind(Kind) {}
68
69 private:
70 ExtendedTypeKind Kind;
71 };
72
73 Ice::Ostream &operator<<(Ice::Ostream &Stream, const ExtendedType &Ty) {
74 Ty.Print(Stream);
75 return Stream;
76 }
77
78 // Models an ICE type as an extended type.
79 class SimpleExtendedType : public ExtendedType {
80 SimpleExtendedType(const SimpleExtendedType &) = delete;
81 SimpleExtendedType &operator=(const SimpleExtendedType &) = delete;
82
83 public:
84 explicit SimpleExtendedType(Ice::Type SimpleType)
85 : ExtendedType(SimpleExtendedKind), SimpleType(SimpleType) {}
86
87 ~SimpleExtendedType() final {}
88
89 Ice::Type getType() const { return SimpleType; }
90
91 void Print(Ice::Ostream &Stream) const final { Stream << SimpleType; }
92
93 static bool classof(const ExtendedType *Ty) {
94 return Ty->getKind() == SimpleExtendedKind;
95 }
96
97 private:
98 Ice::Type SimpleType;
99 };
100
101 // Models a function signature as an extended type.
102 class FcnSigExtendedType : public ExtendedType {
103 FcnSigExtendedType(const FcnSigExtendedType &) = delete;
104 FcnSigExtendedType &operator=(const FcnSigExtendedType &) = delete;
105
106 public:
107 explicit FcnSigExtendedType(Ice::Type ReturnType)
108 : ExtendedType(FcnSigExtendedKind), FcnSig(ReturnType) {}
109 const Ice::FcnSigType &getFcnSig() const { return FcnSig; }
110 void appendArgType(Ice::Type ArgType) { FcnSig.appendArgType(ArgType); }
111 void Print(Ice::Ostream &Stream) const final { Stream << FcnSig; }
112 static bool classof(const ExtendedType *Ty) {
113 return Ty->getKind() == FcnSigExtendedKind;
114 }
115
116 private:
117 Ice::FcnSigType FcnSig;
118 };
119
52 // Top-level class to read PNaCl bitcode files, and translate to ICE. 120 // Top-level class to read PNaCl bitcode files, and translate to ICE.
53 class TopLevelParser : public NaClBitcodeParser { 121 class TopLevelParser : public NaClBitcodeParser {
54 TopLevelParser(const TopLevelParser &) = delete; 122 TopLevelParser(const TopLevelParser &) = delete;
55 TopLevelParser &operator=(const TopLevelParser &) = delete; 123 TopLevelParser &operator=(const TopLevelParser &) = delete;
56 124
57 public: 125 public:
58 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, 126 TopLevelParser(Ice::Translator &Translator, const std::string &InputName,
59 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, 127 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor,
60 bool &ErrorStatus) 128 bool &ErrorStatus)
61 : NaClBitcodeParser(Cursor), Translator(Translator), 129 : NaClBitcodeParser(Cursor), Translator(Translator),
62 Mod(new Module(InputName, getGlobalContext())), DL(PNaClDataLayout), 130 Mod(new Module(InputName, getGlobalContext())), DL(PNaClDataLayout),
63 Header(Header), TypeConverter(getLLVMContext()), 131 Header(Header), TypeConverter(getLLVMContext()),
64 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), 132 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0),
65 NumFunctionBlocks(0), 133 NumFunctionBlocks(0),
66 GlobalVarPlaceHolderType(convertToLLVMType(Ice::IceType_i8)) { 134 GlobalVarPlaceHolderType(convertToLLVMType(Ice::IceType_i8)),
135 UndefinedFcnSigType(Ice::IceType_void) {
67 Mod->setDataLayout(PNaClDataLayout); 136 Mod->setDataLayout(PNaClDataLayout);
68 setErrStream(Translator.getContext()->getStrDump()); 137 setErrStream(Translator.getContext()->getStrDump());
69 } 138 }
70 139
71 ~TopLevelParser() override {} 140 ~TopLevelParser() override { DeleteContainerPointers(TypeIDValues); }
Jim Stichnoth 2014/10/05 15:35:47 I think that if possible, the memory allocation st
Karl 2014/10/06 21:15:54 Reorganized to no longer allocate extended types.
72 141
73 Ice::Translator &getTranslator() { return Translator; } 142 Ice::Translator &getTranslator() { return Translator; }
74 143
75 // Generates error with given Message. Always returns true. 144 // Generates error with given Message. Always returns true.
76 bool Error(const std::string &Message) override { 145 bool Error(const std::string &Message) override {
77 ErrorStatus = true; 146 ErrorStatus = true;
78 ++NumErrors; 147 ++NumErrors;
79 NaClBitcodeParser::Error(Message); 148 NaClBitcodeParser::Error(Message);
80 if (!AllowErrorRecovery) 149 if (!AllowErrorRecovery)
81 report_fatal_error("Unable to continue"); 150 report_fatal_error("Unable to continue");
(...skipping 12 matching lines...) Expand all
94 /// Returns the number of bytes in the bitcode header. 163 /// Returns the number of bytes in the bitcode header.
95 size_t getHeaderSize() const { return Header.getHeaderSize(); } 164 size_t getHeaderSize() const { return Header.getHeaderSize(); }
96 165
97 /// Returns the llvm context to use. 166 /// Returns the llvm context to use.
98 LLVMContext &getLLVMContext() const { return Mod->getContext(); } 167 LLVMContext &getLLVMContext() const { return Mod->getContext(); }
99 168
100 /// Changes the size of the type list to the given size. 169 /// Changes the size of the type list to the given size.
101 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } 170 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); }
102 171
103 /// Returns the type associated with the given index. 172 /// Returns the type associated with the given index.
104 Type *getTypeByID(unsigned ID) { 173 Ice::Type getTypeByID(unsigned ID) {
Jim Stichnoth 2014/10/05 15:35:47 Can this be const? I guess not because of setting
jvoung (off chromium) 2014/10/06 14:32:17 I wonder if this should be called "getSimpleTypeBy
Karl 2014/10/06 21:15:54 Done. Added constness and added qualifier to acces
105 // Note: method resizeTypeIDValues expands TypeIDValues 174 // Note: method resizeTypeIDValues expands TypeIDValues
106 // to the specified size, and fills elements with NULL. 175 // to the specified size, and fills elements with nullptr.
107 Type *Ty = ID < TypeIDValues.size() ? TypeIDValues[ID] : NULL; 176 ExtendedType *Ty = ID < TypeIDValues.size() ? TypeIDValues[ID] : nullptr;
108 if (Ty) 177 if (Ty == nullptr) {
109 return Ty; 178 reportTypeIDAsUndefined(ID);
110 return reportTypeIDAsUndefined(ID); 179 } else if (auto SimpleTy = dyn_cast<SimpleExtendedType>(Ty)) {
180 return SimpleTy->getType();
181 } else {
182 reportTypeIDNotSimple(ID, Ty);
183 }
184 // TODO(kschimpf) Remove error recovery once implementation complete.
185 return Ice::IceType_void;
186 }
187
188 /// Returns the type signature associated with the given index.
189 const Ice::FcnSigType &getFcnSigTypeByID(unsigned ID) {
190 // Note: method resizeTypeIDValues expands TypeIDValues
191 // to the specified size, and fills elements with nullptr.
192 ExtendedType *Ty = ID < TypeIDValues.size() ? TypeIDValues[ID] : nullptr;
193 if (Ty == nullptr) {
194 reportFcnSigTypeIDAsUndefined(ID);
195 } else if (auto SigTy = dyn_cast<FcnSigExtendedType>(Ty)) {
196 return SigTy->getFcnSig();
197 } else {
198 reportFcnSigTypeIDNotFcnSig(ID, Ty);
199 }
200 // TODO(kschimpf) Remove error recovery once implementation complete.
201 return UndefinedFcnSigType;
111 } 202 }
112 203
113 /// Defines type for ID. 204 /// Defines type for ID.
114 void setTypeID(unsigned ID, Type *Ty) { 205 void setTypeID(unsigned ID, ExtendedType *Ty) {
115 if (ID < TypeIDValues.size() && TypeIDValues[ID] == NULL) { 206 if (ID < TypeIDValues.size() && TypeIDValues[ID] == nullptr) {
116 TypeIDValues[ID] = Ty; 207 TypeIDValues[ID] = Ty;
117 return; 208 return;
118 } 209 }
119 reportBadSetTypeID(ID, Ty); 210 reportBadSetTypeID(ID, Ty);
120 } 211 }
121 212
122 /// Sets the next function ID to the given LLVM function. 213 /// Sets the next function ID to the given LLVM function.
123 void setNextFunctionID(Function *Fcn) { 214 void setNextFunctionID(Function *Fcn) {
124 ++NumFunctionIds; 215 ++NumFunctionIds;
125 ValueIDValues.push_back(Fcn); 216 ValueIDValues.push_back(Fcn);
(...skipping 11 matching lines...) Expand all
137 unsigned getNextFunctionBlockValueID() { 228 unsigned getNextFunctionBlockValueID() {
138 if (NumFunctionBlocks >= DefiningFunctionsList.size()) 229 if (NumFunctionBlocks >= DefiningFunctionsList.size())
139 report_fatal_error( 230 report_fatal_error(
140 "More function blocks than defined function addresses"); 231 "More function blocks than defined function addresses");
141 return DefiningFunctionsList[NumFunctionBlocks++]; 232 return DefiningFunctionsList[NumFunctionBlocks++];
142 } 233 }
143 234
144 /// Returns the LLVM IR value associatd with the global value ID. 235 /// Returns the LLVM IR value associatd with the global value ID.
145 Value *getGlobalValueByID(unsigned ID) const { 236 Value *getGlobalValueByID(unsigned ID) const {
146 if (ID >= ValueIDValues.size()) 237 if (ID >= ValueIDValues.size())
147 return NULL; 238 return nullptr;
148 return ValueIDValues[ID]; 239 return ValueIDValues[ID];
149 } 240 }
150 241
151 /// Returns the corresponding constant associated with a global value 242 /// Returns the corresponding constant associated with a global value
152 /// (i.e. relocatable). 243 /// (i.e. relocatable).
153 Ice::Constant *getOrCreateGlobalConstantByID(unsigned ID) { 244 Ice::Constant *getOrCreateGlobalConstantByID(unsigned ID) {
154 // TODO(kschimpf): Can this be built when creating global initializers? 245 // TODO(kschimpf): Can this be built when creating global initializers?
155 if (ID >= ValueIDConstants.size()) { 246 if (ID >= ValueIDConstants.size()) {
156 if (ID >= ValueIDValues.size()) 247 if (ID >= ValueIDValues.size())
157 return NULL; 248 return nullptr;
158 ValueIDConstants.resize(ValueIDValues.size()); 249 ValueIDConstants.resize(ValueIDValues.size());
159 } 250 }
160 Ice::Constant *C = ValueIDConstants[ID]; 251 Ice::Constant *C = ValueIDConstants[ID];
161 if (C != NULL) 252 if (C != nullptr)
162 return C; 253 return C;
163 Value *V = ValueIDValues[ID]; 254 Value *V = ValueIDValues[ID];
164 assert(isa<GlobalValue>(V)); 255 assert(isa<GlobalValue>(V));
165 C = getTranslator().getContext()->getConstantSym(getIcePointerType(), 0, 256 C = getTranslator().getContext()->getConstantSym(getIcePointerType(), 0,
166 V->getName()); 257 V->getName());
167 ValueIDConstants[ID] = C; 258 ValueIDConstants[ID] = C;
168 return C; 259 return C;
169 } 260 }
170 261
171 /// Returns the number of function addresses (i.e. ID's) defined in 262 /// Returns the number of function addresses (i.e. ID's) defined in
172 /// the bitcode file. 263 /// the bitcode file.
173 unsigned getNumFunctionIDs() const { return NumFunctionIds; } 264 unsigned getNumFunctionIDs() const { return NumFunctionIds; }
174 265
175 /// Returns the number of global values defined in the bitcode 266 /// Returns the number of global values defined in the bitcode
176 /// file. 267 /// file.
177 unsigned getNumGlobalValueIDs() const { return ValueIDValues.size(); } 268 unsigned getNumGlobalValueIDs() const { return ValueIDValues.size(); }
178 269
179 /// Resizes the list of value IDs to include Count global variable 270 /// Resizes the list of value IDs to include Count global variable
180 /// IDs. 271 /// IDs.
181 void resizeValueIDsForGlobalVarCount(unsigned Count) { 272 void resizeValueIDsForGlobalVarCount(unsigned Count) {
182 ValueIDValues.resize(ValueIDValues.size() + Count); 273 ValueIDValues.resize(ValueIDValues.size() + Count);
183 } 274 }
184 275
185 /// Returns the global variable address associated with the given 276 /// Returns the global variable address associated with the given
186 /// value ID. If the ID refers to a global variable address not yet 277 /// 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 278 /// defined, a placeholder is created so that we can fix it up
188 /// later. 279 /// later.
189 Constant *getOrCreateGlobalVarRef(unsigned ID) { 280 Constant *getOrCreateGlobalVarRef(unsigned ID) {
190 if (ID >= ValueIDValues.size()) 281 if (ID >= ValueIDValues.size())
191 return NULL; 282 return nullptr;
192 if (Value *C = ValueIDValues[ID]) 283 if (Value *C = ValueIDValues[ID])
193 return dyn_cast<Constant>(C); 284 return dyn_cast<Constant>(C);
194 Constant *C = new GlobalVariable(*Mod, GlobalVarPlaceHolderType, false, 285 Constant *C = new GlobalVariable(*Mod, GlobalVarPlaceHolderType, false,
195 GlobalValue::ExternalLinkage, 0); 286 GlobalValue::ExternalLinkage, 0);
196 ValueIDValues[ID] = C; 287 ValueIDValues[ID] = C;
197 return C; 288 return C;
198 } 289 }
199 290
200 /// Assigns the given global variable (address) to the given value 291 /// Assigns the given global variable (address) to the given value
201 /// ID. Returns true if ID is a valid global variable ID. Otherwise 292 /// ID. Returns true if ID is a valid global variable ID. Otherwise
202 /// returns false. 293 /// returns false.
203 bool assignGlobalVariable(GlobalVariable *GV, unsigned ID) { 294 bool assignGlobalVariable(GlobalVariable *GV, unsigned ID) {
204 if (ID < NumFunctionIds || ID >= ValueIDValues.size()) 295 if (ID < NumFunctionIds || ID >= ValueIDValues.size())
205 return false; 296 return false;
206 WeakVH &OldV = ValueIDValues[ID]; 297 WeakVH &OldV = ValueIDValues[ID];
207 if (OldV == NULL) { 298 if (OldV == nullptr) {
208 ValueIDValues[ID] = GV; 299 ValueIDValues[ID] = GV;
209 return true; 300 return true;
210 } 301 }
211 302
212 // If reached, there was a forward reference to this value. Replace it. 303 // If reached, there was a forward reference to this value. Replace it.
213 Value *PrevVal = OldV; 304 Value *PrevVal = OldV;
214 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal); 305 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal);
215 Placeholder->replaceAllUsesWith( 306 Placeholder->replaceAllUsesWith(
216 ConstantExpr::getBitCast(GV, Placeholder->getType())); 307 ConstantExpr::getBitCast(GV, Placeholder->getType()));
217 Placeholder->eraseFromParent(); 308 Placeholder->eraseFromParent();
218 ValueIDValues[ID] = GV; 309 ValueIDValues[ID] = GV;
219 return true; 310 return true;
220 } 311 }
221 312
222 /// Returns the corresponding ICE type for LLVMTy. 313 /// Returns the corresponding ICE type for LLVMTy.
223 Ice::Type convertToIceType(Type *LLVMTy) { 314 Ice::Type convertToIceType(Type *LLVMTy) {
224 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); 315 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy);
225 if (IceTy >= Ice::IceType_NUM) { 316 if (IceTy >= Ice::IceType_NUM) {
226 return convertToIceTypeError(LLVMTy); 317 return convertToIceTypeError(LLVMTy);
227 } 318 }
228 return IceTy; 319 return IceTy;
229 } 320 }
230 321
231 /// Returns the corresponding LLVM type for IceTy. 322 /// Returns the corresponding LLVM type for IceTy.
232 Type *convertToLLVMType(Ice::Type IceTy) const { 323 Type *convertToLLVMType(Ice::Type IceTy) const {
233 return TypeConverter.convertToLLVMType(IceTy); 324 return TypeConverter.convertToLLVMType(IceTy);
234 } 325 }
235 326
236 /// Returns the LLVM integer type with the given number of Bits. If 327 /// Returns the LLVM integer type with the given number of Bits. If
237 /// Bits is not a valid PNaCl type, returns NULL. 328 /// Bits is not a valid PNaCl type, returns nullptr.
238 Type *getLLVMIntegerType(unsigned Bits) const { 329 Type *getLLVMIntegerType(unsigned Bits) const {
jvoung (off chromium) 2014/10/06 14:32:17 can this be removed now?
Karl 2014/10/06 21:15:54 Removed getLLVMIntegerType and getLLVMVectorType.
jvoung (off chromium) 2014/10/07 15:54:41 Can it be removed from TypeConverter now too?
239 return TypeConverter.getLLVMIntegerType(Bits); 330 return TypeConverter.getLLVMIntegerType(Bits);
240 } 331 }
241 332
242 /// Returns the LLVM vector with the given Size and Ty. If not a 333 /// Returns the LLVM vector with the given Size and Ty. If not a
243 /// valid PNaCl vector type, returns NULL. 334 /// valid PNaCl vector type, returns nullptr.
244 Type *getLLVMVectorType(unsigned Size, Ice::Type Ty) const { 335 Type *getLLVMVectorType(unsigned Size, Ice::Type Ty) const {
245 return TypeConverter.getLLVMVectorType(Size, Ty); 336 return TypeConverter.getLLVMVectorType(Size, Ty);
246 } 337 }
247 338
248 /// Returns the model for pointer types in ICE. 339 /// Returns the model for pointer types in ICE.
249 Ice::Type getIcePointerType() const { 340 Ice::Type getIcePointerType() const {
250 return TypeConverter.getIcePointerType(); 341 return TypeConverter.getIcePointerType();
251 } 342 }
252 343
253 private: 344 private:
254 // The translator associated with the parser. 345 // The translator associated with the parser.
255 Ice::Translator &Translator; 346 Ice::Translator &Translator;
256 // The parsed module. 347 // The parsed module.
257 std::unique_ptr<Module> Mod; 348 std::unique_ptr<Module> Mod;
258 // The data layout to use. 349 // The data layout to use.
259 DataLayout DL; 350 DataLayout DL;
260 // The bitcode header. 351 // The bitcode header.
261 NaClBitcodeHeader &Header; 352 NaClBitcodeHeader &Header;
262 // Converter between LLVM and ICE types. 353 // Converter between LLVM and ICE types.
263 Ice::TypeConverter TypeConverter; 354 Ice::TypeConverter TypeConverter;
264 // The exit status that should be set to true if an error occurs. 355 // The exit status that should be set to true if an error occurs.
265 bool &ErrorStatus; 356 bool &ErrorStatus;
266 // The number of errors reported. 357 // The number of errors reported.
267 unsigned NumErrors; 358 unsigned NumErrors;
268 // The types associated with each type ID. 359 // The types associated with each type ID.
269 std::vector<Type *> TypeIDValues; 360 std::vector<ExtendedType *> TypeIDValues;
270 // The (global) value IDs. 361 // The (global) value IDs.
271 std::vector<WeakVH> ValueIDValues; 362 std::vector<WeakVH> ValueIDValues;
272 // Relocatable constants associated with ValueIDValues. 363 // Relocatable constants associated with ValueIDValues.
273 std::vector<Ice::Constant *> ValueIDConstants; 364 std::vector<Ice::Constant *> ValueIDConstants;
274 // The number of function IDs. 365 // The number of function IDs.
275 unsigned NumFunctionIds; 366 unsigned NumFunctionIds;
276 // The number of function blocks (processed so far). 367 // The number of function blocks (processed so far).
277 unsigned NumFunctionBlocks; 368 unsigned NumFunctionBlocks;
278 // The list of value IDs (in the order found) of defining function 369 // The list of value IDs (in the order found) of defining function
279 // addresses. 370 // addresses.
280 std::vector<unsigned> DefiningFunctionsList; 371 std::vector<unsigned> DefiningFunctionsList;
281 // Cached global variable placeholder type. Used for all forward 372 // Cached global variable placeholder type. Used for all forward
282 // references to global variable addresses. 373 // references to global variable addresses.
283 Type *GlobalVarPlaceHolderType; 374 Type *GlobalVarPlaceHolderType;
375 // Models an undefined function type signature.
376 Ice::FcnSigType UndefinedFcnSigType;
284 377
285 bool ParseBlock(unsigned BlockID) override; 378 bool ParseBlock(unsigned BlockID) override;
286 379
287 /// Reports that type ID is undefined, and then returns 380 /// Reports that type ID is undefined.
jvoung (off chromium) 2014/10/06 14:32:17 double vs triple slash consistency Clarify this i
Karl 2014/10/06 21:15:54 Simplified all type id look ups to go through one
288 /// the void type. 381 void reportTypeIDAsUndefined(unsigned ID);
289 Type *reportTypeIDAsUndefined(unsigned ID); 382
383 // Reports that type ID as not a simple type.
384 void reportTypeIDNotSimple(unsigned ID, const ExtendedType *Ty);
385
386 // Reports that type ID is undefined.
jvoung (off chromium) 2014/10/06 14:32:17 Clarify this is for function signature types?
Karl 2014/10/06 21:15:54 Removed. No longer needed.
387 void reportFcnSigTypeIDAsUndefined(unsigned ID);
388
389 // Reports that type ID is not a function signature.
390 void reportFcnSigTypeIDNotFcnSig(unsigned ID, const ExtendedType *Ty);
290 391
291 /// Reports error about bad call to setTypeID. 392 /// Reports error about bad call to setTypeID.
292 void reportBadSetTypeID(unsigned ID, Type *Ty); 393 void reportBadSetTypeID(unsigned ID, ExtendedType *Ty);
Jim Stichnoth 2014/10/05 15:35:48 const ExtendedType * ?
Karl 2014/10/06 21:15:54 Added to reportBadTypeIDAs.
293 394
294 // Reports that there is no corresponding ICE type for LLVMTy, and 395 // Reports that there is no corresponding ICE type for LLVMTy, and
295 // returns ICE::IceType_void. 396 // returns ICE::IceType_void.
296 Ice::Type convertToIceTypeError(Type *LLVMTy); 397 Ice::Type convertToIceTypeError(Type *LLVMTy);
297 }; 398 };
298 399
299 Type *TopLevelParser::reportTypeIDAsUndefined(unsigned ID) { 400 void TopLevelParser::reportTypeIDAsUndefined(unsigned ID) {
300 std::string Buffer; 401 std::string Buffer;
301 raw_string_ostream StrBuf(Buffer); 402 raw_string_ostream StrBuf(Buffer);
302 StrBuf << "Can't find type for type id: " << ID; 403 StrBuf << "Can't find type for type id: " << ID;
303 Error(StrBuf.str()); 404 Error(StrBuf.str());
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;
310 } 405 }
311 406
312 void TopLevelParser::reportBadSetTypeID(unsigned ID, Type *Ty) { 407 void TopLevelParser::reportTypeIDNotSimple(unsigned ID,
408 const ExtendedType *Ty) {
409 std::string Buffer;
410 raw_string_ostream StrBuf(Buffer);
411 StrBuf << "Type id " << ID << " not simple type. Found: " << *Ty;
412 Error(StrBuf.str());
413 };
414
415 void TopLevelParser::reportFcnSigTypeIDAsUndefined(unsigned ID) {
416 std::string Buffer;
417 raw_string_ostream StrBuf(Buffer);
418 StrBuf << "Can't find function signature type for type id: " << ID;
419 Error(StrBuf.str());
420 }
421
422 void TopLevelParser::reportFcnSigTypeIDNotFcnSig(unsigned ID,
423 const ExtendedType *Ty) {
424 std::string Buffer;
425 raw_string_ostream StrBuf(Buffer);
426 StrBuf << "Type id " << ID << " not function signature type. Found:" << *Ty;
427 Error(StrBuf.str());
428 }
429
430 void TopLevelParser::reportBadSetTypeID(unsigned ID, ExtendedType *Ty) {
313 std::string Buffer; 431 std::string Buffer;
314 raw_string_ostream StrBuf(Buffer); 432 raw_string_ostream StrBuf(Buffer);
315 if (ID >= TypeIDValues.size()) { 433 if (ID >= TypeIDValues.size()) {
316 StrBuf << "Type index " << ID << " out of range: can't install."; 434 StrBuf << "Type index " << ID
435 << " out of range. Can't install type: " << Ty;
317 } else { 436 } else {
318 // Must be case that index already defined. 437 // Must be case that index already defined.
319 StrBuf << "Type index " << ID << " defined as " << *TypeIDValues[ID] 438 StrBuf << "Type index " << ID << " defined as " << *TypeIDValues[ID]
320 << " and " << *Ty << "."; 439 << " and " << *Ty << ".";
321 } 440 }
322 Error(StrBuf.str()); 441 Error(StrBuf.str());
323 } 442 }
324 443
325 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { 444 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) {
326 std::string Buffer; 445 std::string Buffer;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 // Default implementation. Reports that the record is not 492 // Default implementation. Reports that the record is not
374 // understood. 493 // understood.
375 void ProcessRecord() override; 494 void ProcessRecord() override;
376 495
377 // Checks if the size of the record is Size. Return true if valid. 496 // Checks if the size of the record is Size. Return true if valid.
378 // Otherwise generates an error and returns false. 497 // Otherwise generates an error and returns false.
379 bool isValidRecordSize(unsigned Size, const char *RecordName) { 498 bool isValidRecordSize(unsigned Size, const char *RecordName) {
380 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 499 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
381 if (Values.size() == Size) 500 if (Values.size() == Size)
382 return true; 501 return true;
383 ReportRecordSizeError(Size, RecordName, NULL); 502 ReportRecordSizeError(Size, RecordName, nullptr);
384 return false; 503 return false;
385 } 504 }
386 505
387 // Checks if the size of the record is at least as large as the 506 // Checks if the size of the record is at least as large as the
388 // LowerLimit. Returns true if valid. Otherwise generates an error 507 // LowerLimit. Returns true if valid. Otherwise generates an error
389 // and returns false. 508 // and returns false.
390 bool isValidRecordSizeAtLeast(unsigned LowerLimit, const char *RecordName) { 509 bool isValidRecordSizeAtLeast(unsigned LowerLimit, const char *RecordName) {
391 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 510 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
392 if (Values.size() >= LowerLimit) 511 if (Values.size() >= LowerLimit)
393 return true; 512 return true;
(...skipping 17 matching lines...) Expand all
411 // valid. Otherwise generates an error and returns false. 530 // valid. Otherwise generates an error and returns false.
412 bool isValidRecordSizeInRange(unsigned LowerLimit, unsigned UpperLimit, 531 bool isValidRecordSizeInRange(unsigned LowerLimit, unsigned UpperLimit,
413 const char *RecordName) { 532 const char *RecordName) {
414 return isValidRecordSizeAtLeast(LowerLimit, RecordName) || 533 return isValidRecordSizeAtLeast(LowerLimit, RecordName) ||
415 isValidRecordSizeAtMost(UpperLimit, RecordName); 534 isValidRecordSizeAtMost(UpperLimit, RecordName);
416 } 535 }
417 536
418 private: 537 private:
419 /// Generates a record size error. ExpectedSize is the number 538 /// Generates a record size error. ExpectedSize is the number
420 /// of elements expected. RecordName is the name of the kind of 539 /// of elements expected. RecordName is the name of the kind of
421 /// record that has incorrect size. ContextMessage (if not NULL) 540 /// record that has incorrect size. ContextMessage (if not nullptr)
422 /// is appended to "record expects" to describe how ExpectedSize 541 /// is appended to "record expects" to describe how ExpectedSize
423 /// should be interpreted. 542 /// should be interpreted.
424 void ReportRecordSizeError(unsigned ExpectedSize, const char *RecordName, 543 void ReportRecordSizeError(unsigned ExpectedSize, const char *RecordName,
425 const char *ContextMessage); 544 const char *ContextMessage);
426 }; 545 };
427 546
428 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize, 547 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize,
429 const char *RecordName, 548 const char *RecordName,
430 const char *ContextMessage) { 549 const char *ContextMessage) {
431 std::string Buffer; 550 std::string Buffer;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 589
471 private: 590 private:
472 // The type ID that will be associated with the next type defining 591 // The type ID that will be associated with the next type defining
473 // record in the types block. 592 // record in the types block.
474 unsigned NextTypeId; 593 unsigned NextTypeId;
475 594
476 void ProcessRecord() override; 595 void ProcessRecord() override;
477 }; 596 };
478 597
479 void TypesParser::ProcessRecord() { 598 void TypesParser::ProcessRecord() {
480 Type *Ty = NULL; 599 ExtendedType *Ty = nullptr;
481 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 600 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
482 switch (Record.GetCode()) { 601 switch (Record.GetCode()) {
483 case naclbitc::TYPE_CODE_NUMENTRY: 602 case naclbitc::TYPE_CODE_NUMENTRY:
484 // NUMENTRY: [numentries] 603 // NUMENTRY: [numentries]
485 if (!isValidRecordSize(1, "Type count")) 604 if (!isValidRecordSize(1, "Type count"))
486 return; 605 return;
487 Context->resizeTypeIDValues(Values[0]); 606 Context->resizeTypeIDValues(Values[0]);
488 return; 607 return;
489 case naclbitc::TYPE_CODE_VOID: 608 case naclbitc::TYPE_CODE_VOID:
490 // VOID 609 // VOID
491 if (!isValidRecordSize(0, "Type void")) 610 if (!isValidRecordSize(0, "Type void"))
492 return; 611 return;
493 Ty = Context->convertToLLVMType(Ice::IceType_void); 612 Ty = new SimpleExtendedType(Ice::IceType_void);
494 break; 613 break;
495 case naclbitc::TYPE_CODE_FLOAT: 614 case naclbitc::TYPE_CODE_FLOAT:
496 // FLOAT 615 // FLOAT
497 if (!isValidRecordSize(0, "Type float")) 616 if (!isValidRecordSize(0, "Type float"))
498 return; 617 return;
499 Ty = Context->convertToLLVMType(Ice::IceType_f32); 618 Ty = new SimpleExtendedType(Ice::IceType_f32);
500 break; 619 break;
501 case naclbitc::TYPE_CODE_DOUBLE: 620 case naclbitc::TYPE_CODE_DOUBLE:
502 // DOUBLE 621 // DOUBLE
503 if (!isValidRecordSize(0, "Type double")) 622 if (!isValidRecordSize(0, "Type double"))
504 return; 623 return;
505 Ty = Context->convertToLLVMType(Ice::IceType_f64); 624 Ty = new SimpleExtendedType(Ice::IceType_f64);
506 break; 625 break;
507 case naclbitc::TYPE_CODE_INTEGER: 626 case naclbitc::TYPE_CODE_INTEGER:
508 // INTEGER: [width] 627 // INTEGER: [width]
509 if (!isValidRecordSize(1, "Type integer")) 628 if (!isValidRecordSize(1, "Type integer"))
510 return; 629 return;
511 Ty = Context->getLLVMIntegerType(Values[0]); 630 switch (Values[0]) {
512 if (Ty == NULL) { 631 case 1:
632 Ty = new SimpleExtendedType(Ice::IceType_i1);
633 break;
634 case 8:
635 Ty = new SimpleExtendedType(Ice::IceType_i8);
636 break;
637 case 16:
638 Ty = new SimpleExtendedType(Ice::IceType_i16);
639 break;
640 case 32:
641 Ty = new SimpleExtendedType(Ice::IceType_i32);
642 break;
643 case 64:
644 Ty = new SimpleExtendedType(Ice::IceType_i64);
645 break;
646 default:
647 break;
648 }
649 if (Ty == nullptr) {
513 std::string Buffer; 650 std::string Buffer;
514 raw_string_ostream StrBuf(Buffer); 651 raw_string_ostream StrBuf(Buffer);
515 StrBuf << "Type integer record with invalid bitsize: " << Values[0]; 652 StrBuf << "Type integer record with invalid bitsize: " << Values[0];
516 Error(StrBuf.str()); 653 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 } 654 }
521 break; 655 break;
522 case naclbitc::TYPE_CODE_VECTOR: { 656 case naclbitc::TYPE_CODE_VECTOR: {
523 // VECTOR: [numelts, eltty] 657 // VECTOR: [numelts, eltty]
524 if (!isValidRecordSize(2, "Type vector")) 658 if (!isValidRecordSize(2, "Type vector"))
525 return; 659 return;
526 Type *BaseTy = Context->getTypeByID(Values[1]); 660 Ice::Type BaseTy = Context->getTypeByID(Values[1]);
527 Ty = Context->getLLVMVectorType(Values[0], 661 Ice::SizeT Size = Values[0];
jvoung (off chromium) 2014/10/06 14:32:17 can getLLVMVectorType be removed now?
Karl 2014/10/06 21:15:54 Done.
528 Context->convertToIceType(BaseTy)); 662 switch (BaseTy) {
529 if (Ty == NULL) { 663 case Ice::IceType_i1:
664 switch (Size) {
665 case 4:
666 Ty = new SimpleExtendedType(Ice::IceType_v4i1);
667 break;
668 case 8:
669 Ty = new SimpleExtendedType(Ice::IceType_v8i1);
670 break;
671 case 16:
672 Ty = new SimpleExtendedType(Ice::IceType_v16i1);
673 break;
674 default:
675 break;
676 }
677 break;
678 case Ice::IceType_i8:
679 if (Size == 16)
680 Ty = new SimpleExtendedType(Ice::IceType_v16i8);
681 break;
682 case Ice::IceType_i16:
683 if (Size == 8)
684 Ty = new SimpleExtendedType(Ice::IceType_v8i16);
685 break;
686 case Ice::IceType_i32:
687 if (Size == 4)
688 Ty = new SimpleExtendedType(Ice::IceType_v4i32);
689 break;
690 case Ice::IceType_f32:
691 if (Size == 4)
692 Ty = new SimpleExtendedType(Ice::IceType_v4f32);
693 break;
694 default:
695 break;
696 }
697
698 if (Ty == nullptr) {
530 std::string Buffer; 699 std::string Buffer;
531 raw_string_ostream StrBuf(Buffer); 700 raw_string_ostream StrBuf(Buffer);
532 StrBuf << "Invalid type vector record: <" << Values[0] << " x " << *BaseTy 701 StrBuf << "Invalid type vector record: <" << Values[0] << " x " << BaseTy
533 << ">"; 702 << ">";
534 Error(StrBuf.str()); 703 Error(StrBuf.str());
535 Ty = Context->convertToLLVMType(Ice::IceType_void);
536 } 704 }
537 break; 705 break;
538 } 706 }
539 case naclbitc::TYPE_CODE_FUNCTION: { 707 case naclbitc::TYPE_CODE_FUNCTION: {
540 // FUNCTION: [vararg, retty, paramty x N] 708 // FUNCTION: [vararg, retty, paramty x N]
541 if (!isValidRecordSizeAtLeast(2, "Type signature")) 709 if (!isValidRecordSizeAtLeast(2, "Type signature"))
542 return; 710 return;
543 SmallVector<Type *, 8> ArgTys; 711 if (Values[0])
712 Error("Function type can't define varargs");
713 FcnSigExtendedType *FcnTy =
714 new FcnSigExtendedType(Context->getTypeByID(Values[1]));
544 for (unsigned i = 2, e = Values.size(); i != e; ++i) { 715 for (unsigned i = 2, e = Values.size(); i != e; ++i) {
545 ArgTys.push_back(Context->getTypeByID(Values[i])); 716 // Check that type void not used as argument type.
717 // Note: PNaCl restrictions can't be checked until we
718 // know the name, because we have to check for intrinsic signatures.
719 Ice::Type ArgTy = Context->getTypeByID(Values[i]);
720 if (ArgTy == Ice::IceType_void) {
721 std::string Buffer;
722 raw_string_ostream StrBuf(Buffer);
723 StrBuf << "Type for parameter " << (i - 1)
724 << " not valid. Found: " << ArgTy;
725 // TODO(kschimpf) Remove error recovery once implementation complete.
726 ArgTy = Ice::IceType_i32;
727 }
728 FcnTy->appendArgType(ArgTy);
546 } 729 }
547 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); 730 Ty = FcnTy;
548 break; 731 break;
549 } 732 }
550 default: 733 default:
551 BlockParserBaseClass::ProcessRecord(); 734 BlockParserBaseClass::ProcessRecord();
552 return; 735 return;
553 } 736 }
554 // If Ty not defined, assume error. Use void as filler. 737 // If Ty not defined, assume error. Use void as filler.
555 if (Ty == NULL) 738 // TODO(kschimpf) Remove error recovery once implementation complete.
556 Ty = Context->convertToLLVMType(Ice::IceType_void); 739 // Fix type so that we can continue.
740 if (Ty == nullptr)
741 Ty = new SimpleExtendedType(Ice::IceType_void);
557 Context->setTypeID(NextTypeId++, Ty); 742 Context->setTypeID(NextTypeId++, Ty);
558 } 743 }
559 744
560 /// Parses the globals block (i.e. global variables). 745 /// Parses the globals block (i.e. global variables).
561 class GlobalsParser : public BlockParserBaseClass { 746 class GlobalsParser : public BlockParserBaseClass {
562 public: 747 public:
563 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) 748 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser)
564 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), 749 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0),
565 Alignment(1), IsConstant(false) { 750 Alignment(1), IsConstant(false) {
566 NextGlobalID = Context->getNumFunctionIDs(); 751 NextGlobalID = Context->getNumFunctionIDs();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 if (InitializersNeeded <= Initializers.size()) { 814 if (InitializersNeeded <= Initializers.size()) {
630 Error(std::string(RecordName) + 815 Error(std::string(RecordName) +
631 " record: Too many initializers, ignoring."); 816 " record: Too many initializers, ignoring.");
632 } 817 }
633 } 818 }
634 819
635 // Takes the initializers (and other parser state values) and 820 // Takes the initializers (and other parser state values) and
636 // installs a global variable (with the initializers) into the list 821 // installs a global variable (with the initializers) into the list
637 // of ValueIDs. 822 // of ValueIDs.
638 void installGlobalVar() { 823 void installGlobalVar() {
639 Constant *Init = NULL; 824 Constant *Init = nullptr;
640 switch (Initializers.size()) { 825 switch (Initializers.size()) {
641 case 0: 826 case 0:
642 Error("No initializer for global variable in global vars block"); 827 Error("No initializer for global variable in global vars block");
643 return; 828 return;
644 case 1: 829 case 1:
645 Init = Initializers[0]; 830 Init = Initializers[0];
646 break; 831 break;
647 default: 832 default:
648 Init = ConstantStruct::getAnon(Context->getLLVMContext(), Initializers, 833 Init = ConstantStruct::getAnon(Context->getLLVMContext(), Initializers,
649 true); 834 true);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 Constant *Init = ConstantDataArray::get( 918 Constant *Init = ConstantDataArray::get(
734 Context->getLLVMContext(), ArrayRef<uint8_t>(Buf.data(), Buf.size())); 919 Context->getLLVMContext(), ArrayRef<uint8_t>(Buf.data(), Buf.size()));
735 Initializers.push_back(Init); 920 Initializers.push_back(Init);
736 break; 921 break;
737 } 922 }
738 case naclbitc::GLOBALVAR_RELOC: { 923 case naclbitc::GLOBALVAR_RELOC: {
739 // RELOC: [val, [addend]] 924 // RELOC: [val, [addend]]
740 if (!isValidRecordSizeInRange(1, 2, "Globals reloc")) 925 if (!isValidRecordSizeInRange(1, 2, "Globals reloc"))
741 return; 926 return;
742 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]); 927 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]);
743 if (BaseVal == NULL) { 928 if (BaseVal == nullptr) {
744 std::string Buffer; 929 std::string Buffer;
745 raw_string_ostream StrBuf(Buffer); 930 raw_string_ostream StrBuf(Buffer);
746 StrBuf << "Can't find global relocation value: " << Values[0]; 931 StrBuf << "Can't find global relocation value: " << Values[0];
747 Error(StrBuf.str()); 932 Error(StrBuf.str());
748 return; 933 return;
749 } 934 }
750 Type *IntPtrType = Context->convertToLLVMType(Context->getIcePointerType()); 935 Type *IntPtrType = Context->convertToLLVMType(Context->getIcePointerType());
751 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); 936 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType);
752 if (Values.size() == 2) { 937 if (Values.size() == 2) {
753 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1])); 938 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1]));
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
949 return Func->makeVariable(Ty); 1134 return Func->makeVariable(Ty);
950 } 1135 }
951 1136
952 // Generates the next available local variable using the given type. 1137 // Generates the next available local variable using the given type.
953 Ice::Variable *getNextInstVar(Ice::Type Ty) { 1138 Ice::Variable *getNextInstVar(Ice::Type Ty) {
954 assert(NextLocalInstIndex >= CachedNumGlobalValueIDs); 1139 assert(NextLocalInstIndex >= CachedNumGlobalValueIDs);
955 // Before creating one, see if a forwardtyperef has already defined it. 1140 // Before creating one, see if a forwardtyperef has already defined it.
956 uint32_t LocalIndex = NextLocalInstIndex - CachedNumGlobalValueIDs; 1141 uint32_t LocalIndex = NextLocalInstIndex - CachedNumGlobalValueIDs;
957 if (LocalIndex < LocalOperands.size()) { 1142 if (LocalIndex < LocalOperands.size()) {
958 Ice::Operand *Op = LocalOperands[LocalIndex]; 1143 Ice::Operand *Op = LocalOperands[LocalIndex];
959 if (Op != NULL) { 1144 if (Op != nullptr) {
960 if (Ice::Variable *Var = dyn_cast<Ice::Variable>(Op)) { 1145 if (Ice::Variable *Var = dyn_cast<Ice::Variable>(Op)) {
961 if (Var->getType() == Ty) { 1146 if (Var->getType() == Ty) {
962 ++NextLocalInstIndex; 1147 ++NextLocalInstIndex;
963 return Var; 1148 return Var;
964 } 1149 }
965 } 1150 }
966 std::string Buffer; 1151 std::string Buffer;
967 raw_string_ostream StrBuf(Buffer); 1152 raw_string_ostream StrBuf(Buffer);
968 StrBuf << "Illegal forward referenced instruction (" 1153 StrBuf << "Illegal forward referenced instruction ("
969 << NextLocalInstIndex << "): " << *Op; 1154 << NextLocalInstIndex << "): " << *Op;
(...skipping 30 matching lines...) Expand all
1000 } 1185 }
1001 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; 1186 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs;
1002 if (LocalIndex >= LocalOperands.size()) { 1187 if (LocalIndex >= LocalOperands.size()) {
1003 std::string Buffer; 1188 std::string Buffer;
1004 raw_string_ostream StrBuf(Buffer); 1189 raw_string_ostream StrBuf(Buffer);
1005 StrBuf << "Value index " << Index << " not defined!"; 1190 StrBuf << "Value index " << Index << " not defined!";
1006 Error(StrBuf.str()); 1191 Error(StrBuf.str());
1007 report_fatal_error("Unable to continue"); 1192 report_fatal_error("Unable to continue");
1008 } 1193 }
1009 Ice::Operand *Op = LocalOperands[LocalIndex]; 1194 Ice::Operand *Op = LocalOperands[LocalIndex];
1010 if (Op == NULL) { 1195 if (Op == nullptr) {
1011 std::string Buffer; 1196 std::string Buffer;
1012 raw_string_ostream StrBuf(Buffer); 1197 raw_string_ostream StrBuf(Buffer);
1013 StrBuf << "Value index " << Index << " not defined!"; 1198 StrBuf << "Value index " << Index << " not defined!";
1014 Error(StrBuf.str()); 1199 Error(StrBuf.str());
1015 report_fatal_error("Unable to continue"); 1200 report_fatal_error("Unable to continue");
1016 } 1201 }
1017 return Op; 1202 return Op;
1018 } 1203 }
1019 1204
1020 // Sets element Index (in the local operands list) to Op. 1205 // Sets element Index (in the local operands list) to Op.
1021 void setOperand(uint32_t Index, Ice::Operand *Op) { 1206 void setOperand(uint32_t Index, Ice::Operand *Op) {
1022 assert(Op); 1207 assert(Op);
1023 // Check if simple push works. 1208 // Check if simple push works.
1024 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; 1209 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs;
1025 if (LocalIndex == LocalOperands.size()) { 1210 if (LocalIndex == LocalOperands.size()) {
1026 LocalOperands.push_back(Op); 1211 LocalOperands.push_back(Op);
1027 return; 1212 return;
1028 } 1213 }
1029 1214
1030 // Must be forward reference, expand vector to accommodate. 1215 // Must be forward reference, expand vector to accommodate.
1031 if (LocalIndex >= LocalOperands.size()) 1216 if (LocalIndex >= LocalOperands.size())
1032 LocalOperands.resize(LocalIndex + 1); 1217 LocalOperands.resize(LocalIndex + 1);
1033 1218
1034 // If element not defined, set it. 1219 // If element not defined, set it.
1035 Ice::Operand *OldOp = LocalOperands[LocalIndex]; 1220 Ice::Operand *OldOp = LocalOperands[LocalIndex];
1036 if (OldOp == NULL) { 1221 if (OldOp == nullptr) {
1037 LocalOperands[LocalIndex] = Op; 1222 LocalOperands[LocalIndex] = Op;
1038 return; 1223 return;
1039 } 1224 }
1040 1225
1041 // See if forward reference matches. 1226 // See if forward reference matches.
1042 if (OldOp == Op) 1227 if (OldOp == Op)
1043 return; 1228 return;
1044 1229
1045 // Error has occurred. 1230 // Error has occurred.
1046 std::string Buffer; 1231 std::string Buffer;
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 return; 1651 return;
1467 CurrentNode->appendInst(Ice::InstArithmetic::create( 1652 CurrentNode->appendInst(Ice::InstArithmetic::create(
1468 Func, Opcode, getNextInstVar(Type1), Op1, Op2)); 1653 Func, Opcode, getNextInstVar(Type1), Op1, Op2));
1469 break; 1654 break;
1470 } 1655 }
1471 case naclbitc::FUNC_CODE_INST_CAST: { 1656 case naclbitc::FUNC_CODE_INST_CAST: {
1472 // CAST: [opval, destty, castopc] 1657 // CAST: [opval, destty, castopc]
1473 if (!isValidRecordSize(3, "function block cast")) 1658 if (!isValidRecordSize(3, "function block cast"))
1474 return; 1659 return;
1475 Ice::Operand *Src = getRelativeOperand(Values[0], BaseIndex); 1660 Ice::Operand *Src = getRelativeOperand(Values[0], BaseIndex);
1476 Type *CastType = Context->getTypeByID(Values[1]); 1661 Ice::Type CastType = Context->getTypeByID(Values[1]);
1477 Instruction::CastOps LLVMCastOp; 1662 Instruction::CastOps LLVMCastOp;
1478 Ice::InstCast::OpKind CastKind; 1663 Ice::InstCast::OpKind CastKind;
1479 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) || 1664 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) ||
1480 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) { 1665 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) {
1481 std::string Buffer; 1666 std::string Buffer;
1482 raw_string_ostream StrBuf(Buffer); 1667 raw_string_ostream StrBuf(Buffer);
1483 StrBuf << "Cast opcode not understood: " << Values[2]; 1668 StrBuf << "Cast opcode not understood: " << Values[2];
1484 Error(StrBuf.str()); 1669 Error(StrBuf.str());
1485 return; 1670 return;
1486 } 1671 }
1487 Type *SrcType = Context->convertToLLVMType(Src->getType()); 1672 Ice::Type SrcType = Src->getType();
1488 if (!CastInst::castIsValid(LLVMCastOp, SrcType, CastType)) { 1673 if (!CastInst::castIsValid(LLVMCastOp, Context->convertToLLVMType(SrcType),
1674 Context->convertToLLVMType(CastType))) {
1489 std::string Buffer; 1675 std::string Buffer;
1490 raw_string_ostream StrBuf(Buffer); 1676 raw_string_ostream StrBuf(Buffer);
1491 StrBuf << "Illegal cast: " << Instruction::getOpcodeName(LLVMCastOp) 1677 StrBuf << "Illegal cast: " << Instruction::getOpcodeName(LLVMCastOp)
1492 << " " << *SrcType << " to " << *CastType; 1678 << " " << SrcType << " to " << CastType;
1493 Error(StrBuf.str()); 1679 Error(StrBuf.str());
1494 return; 1680 return;
1495 } 1681 }
1496 CurrentNode->appendInst(Ice::InstCast::create( 1682 CurrentNode->appendInst(
1497 Func, CastKind, getNextInstVar(Context->convertToIceType(CastType)), 1683 Ice::InstCast::create(Func, CastKind, getNextInstVar(CastType), Src));
1498 Src));
1499 break; 1684 break;
1500 } 1685 }
1501 case naclbitc::FUNC_CODE_INST_VSELECT: { 1686 case naclbitc::FUNC_CODE_INST_VSELECT: {
1502 // VSELECT: [opval, opval, pred] 1687 // VSELECT: [opval, opval, pred]
1503 Ice::Operand *ThenVal = getRelativeOperand(Values[0], BaseIndex); 1688 Ice::Operand *ThenVal = getRelativeOperand(Values[0], BaseIndex);
1504 Ice::Type ThenType = ThenVal->getType(); 1689 Ice::Type ThenType = ThenVal->getType();
1505 Ice::Operand *ElseVal = getRelativeOperand(Values[1], BaseIndex); 1690 Ice::Operand *ElseVal = getRelativeOperand(Values[1], BaseIndex);
1506 Ice::Type ElseType = ElseVal->getType(); 1691 Ice::Type ElseType = ElseVal->getType();
1507 if (ThenType != ElseType) { 1692 if (ThenType != ElseType) {
1508 std::string Buffer; 1693 std::string Buffer;
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1671 CurrentNode->appendInst( 1856 CurrentNode->appendInst(
1672 Ice::InstRet::create(Func, getRelativeOperand(Values[0], BaseIndex))); 1857 Ice::InstRet::create(Func, getRelativeOperand(Values[0], BaseIndex)));
1673 } 1858 }
1674 InstIsTerminating = true; 1859 InstIsTerminating = true;
1675 break; 1860 break;
1676 } 1861 }
1677 case naclbitc::FUNC_CODE_INST_BR: { 1862 case naclbitc::FUNC_CODE_INST_BR: {
1678 if (Values.size() == 1) { 1863 if (Values.size() == 1) {
1679 // BR: [bb#] 1864 // BR: [bb#]
1680 Ice::CfgNode *Block = getBranchBasicBlock(Values[0]); 1865 Ice::CfgNode *Block = getBranchBasicBlock(Values[0]);
1681 if (Block == NULL) 1866 if (Block == nullptr)
1682 return; 1867 return;
1683 CurrentNode->appendInst(Ice::InstBr::create(Func, Block)); 1868 CurrentNode->appendInst(Ice::InstBr::create(Func, Block));
1684 } else { 1869 } else {
1685 // BR: [bb#, bb#, opval] 1870 // BR: [bb#, bb#, opval]
1686 if (!isValidRecordSize(3, "function block branch")) 1871 if (!isValidRecordSize(3, "function block branch"))
1687 return; 1872 return;
1688 Ice::Operand *Cond = getRelativeOperand(Values[2], BaseIndex); 1873 Ice::Operand *Cond = getRelativeOperand(Values[2], BaseIndex);
1689 if (Cond->getType() != Ice::IceType_i1) { 1874 if (Cond->getType() != Ice::IceType_i1) {
1690 std::string Buffer; 1875 std::string Buffer;
1691 raw_string_ostream StrBuf(Buffer); 1876 raw_string_ostream StrBuf(Buffer);
1692 StrBuf << "Branch condition " << *Cond << " not i1. Found: " 1877 StrBuf << "Branch condition " << *Cond << " not i1. Found: "
1693 << Cond->getType(); 1878 << Cond->getType();
1694 Error(StrBuf.str()); 1879 Error(StrBuf.str());
1695 return; 1880 return;
1696 } 1881 }
1697 Ice::CfgNode *ThenBlock = getBranchBasicBlock(Values[0]); 1882 Ice::CfgNode *ThenBlock = getBranchBasicBlock(Values[0]);
1698 Ice::CfgNode *ElseBlock = getBranchBasicBlock(Values[1]); 1883 Ice::CfgNode *ElseBlock = getBranchBasicBlock(Values[1]);
1699 if (ThenBlock == NULL || ElseBlock == NULL) 1884 if (ThenBlock == nullptr || ElseBlock == nullptr)
1700 return; 1885 return;
1701 CurrentNode->appendInst( 1886 CurrentNode->appendInst(
1702 Ice::InstBr::create(Func, Cond, ThenBlock, ElseBlock)); 1887 Ice::InstBr::create(Func, Cond, ThenBlock, ElseBlock));
1703 } 1888 }
1704 InstIsTerminating = true; 1889 InstIsTerminating = true;
1705 break; 1890 break;
1706 } 1891 }
1707 case naclbitc::FUNC_CODE_INST_SWITCH: { 1892 case naclbitc::FUNC_CODE_INST_SWITCH: {
1708 // SWITCH: [Condty, Cond, BbIndex, NumCases Case ...] 1893 // SWITCH: [Condty, Cond, BbIndex, NumCases Case ...]
1709 // where Case = [1, 1, Value, BbIndex]. 1894 // where Case = [1, 1, Value, BbIndex].
1710 // 1895 //
1711 // Note: Unlike most instructions, we don't infer the type of 1896 // Note: Unlike most instructions, we don't infer the type of
1712 // Cond, but provide it as a separate field. There are also 1897 // Cond, but provide it as a separate field. There are also
1713 // unnecesary data fields (i.e. constants 1). These were not 1898 // unnecesary data fields (i.e. constants 1). These were not
1714 // cleaned up in PNaCl bitcode because the bitcode format was 1899 // cleaned up in PNaCl bitcode because the bitcode format was
1715 // already frozen when the problem was noticed. 1900 // already frozen when the problem was noticed.
1716 if (!isValidRecordSizeAtLeast(4, "function block switch")) 1901 if (!isValidRecordSizeAtLeast(4, "function block switch"))
1717 return; 1902 return;
1718 Ice::Type CondTy = 1903 Ice::Type CondTy = Context->getTypeByID(Values[0]);
1719 Context->convertToIceType(Context->getTypeByID(Values[0]));
1720 if (!Ice::isScalarIntegerType(CondTy)) { 1904 if (!Ice::isScalarIntegerType(CondTy)) {
1721 std::string Buffer; 1905 std::string Buffer;
1722 raw_string_ostream StrBuf(Buffer); 1906 raw_string_ostream StrBuf(Buffer);
1723 StrBuf << "Case condition must be non-wide integer. Found: " << CondTy; 1907 StrBuf << "Case condition must be non-wide integer. Found: " << CondTy;
1724 Error(StrBuf.str()); 1908 Error(StrBuf.str());
1725 return; 1909 return;
1726 } 1910 }
1727 Ice::SizeT BitWidth = Ice::getScalarIntBitWidth(CondTy); 1911 Ice::SizeT BitWidth = Ice::getScalarIntBitWidth(CondTy);
1728 Ice::Operand *Cond = getRelativeOperand(Values[1], BaseIndex); 1912 Ice::Operand *Cond = getRelativeOperand(Values[1], BaseIndex);
1729 if (CondTy != Cond->getType()) { 1913 if (CondTy != Cond->getType()) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1777 if (!isValidRecordSizeAtLeast(3, "function block phi")) 1961 if (!isValidRecordSizeAtLeast(3, "function block phi"))
1778 return; 1962 return;
1779 if ((Values.size() & 0x1) == 0) { 1963 if ((Values.size() & 0x1) == 0) {
1780 // Not an odd number of values. 1964 // Not an odd number of values.
1781 std::string Buffer; 1965 std::string Buffer;
1782 raw_string_ostream StrBuf(Buffer); 1966 raw_string_ostream StrBuf(Buffer);
1783 StrBuf << "function block phi record size not valid: " << Values.size(); 1967 StrBuf << "function block phi record size not valid: " << Values.size();
1784 Error(StrBuf.str()); 1968 Error(StrBuf.str());
1785 return; 1969 return;
1786 } 1970 }
1787 Ice::Type Ty = Context->convertToIceType(Context->getTypeByID(Values[0])); 1971 Ice::Type Ty = Context->getTypeByID(Values[0]);
1788 if (Ty == Ice::IceType_void) { 1972 if (Ty == Ice::IceType_void) {
1789 Error("Phi record using type void not allowed"); 1973 Error("Phi record using type void not allowed");
1790 return; 1974 return;
1791 } 1975 }
1792 Ice::Variable *Dest = getNextInstVar(Ty); 1976 Ice::Variable *Dest = getNextInstVar(Ty);
1793 Ice::InstPhi *Phi = Ice::InstPhi::create(Func, Values.size() >> 1, Dest); 1977 Ice::InstPhi *Phi = Ice::InstPhi::create(Func, Values.size() >> 1, Dest);
1794 for (unsigned i = 1; i < Values.size(); i += 2) { 1978 for (unsigned i = 1; i < Values.size(); i += 2) {
1795 Ice::Operand *Op = 1979 Ice::Operand *Op =
1796 getRelativeOperand(NaClDecodeSignRotatedValue(Values[i]), BaseIndex); 1980 getRelativeOperand(NaClDecodeSignRotatedValue(Values[i]), BaseIndex);
1797 if (Op->getType() != Ty) { 1981 if (Op->getType() != Ty) {
(...skipping 30 matching lines...) Expand all
1828 } 2012 }
1829 case naclbitc::FUNC_CODE_INST_LOAD: { 2013 case naclbitc::FUNC_CODE_INST_LOAD: {
1830 // LOAD: [address, align, ty] 2014 // LOAD: [address, align, ty]
1831 if (!isValidRecordSize(3, "function block load")) 2015 if (!isValidRecordSize(3, "function block load"))
1832 return; 2016 return;
1833 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); 2017 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex);
1834 if (!isValidPointerType(Address, "Load")) 2018 if (!isValidPointerType(Address, "Load"))
1835 return; 2019 return;
1836 unsigned Alignment; 2020 unsigned Alignment;
1837 extractAlignment("Load", Values[1], Alignment); 2021 extractAlignment("Load", Values[1], Alignment);
1838 Ice::Type Ty = Context->convertToIceType(Context->getTypeByID(Values[2])); 2022 Ice::Type Ty = Context->getTypeByID(Values[2]);
1839 if (!isValidLoadStoreAlignment(Alignment, Ty, "Load")) 2023 if (!isValidLoadStoreAlignment(Alignment, Ty, "Load"))
1840 return; 2024 return;
1841 CurrentNode->appendInst( 2025 CurrentNode->appendInst(
1842 Ice::InstLoad::create(Func, getNextInstVar(Ty), Address, Alignment)); 2026 Ice::InstLoad::create(Func, getNextInstVar(Ty), Address, Alignment));
1843 break; 2027 break;
1844 } 2028 }
1845 case naclbitc::FUNC_CODE_INST_STORE: { 2029 case naclbitc::FUNC_CODE_INST_STORE: {
1846 // STORE: [address, value, align] 2030 // STORE: [address, value, align]
1847 if (!isValidRecordSize(3, "function block store")) 2031 if (!isValidRecordSize(3, "function block store"))
1848 return; 2032 return;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1890 << " not understood."; 2074 << " not understood.";
1891 Error(StrBuf.str()); 2075 Error(StrBuf.str());
1892 return; 2076 return;
1893 } 2077 }
1894 bool IsTailCall = static_cast<bool>(CCInfo & 1); 2078 bool IsTailCall = static_cast<bool>(CCInfo & 1);
1895 2079
1896 // Extract out the called function and its return type. 2080 // Extract out the called function and its return type.
1897 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex); 2081 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex);
1898 Ice::Operand *Callee = getOperand(CalleeIndex); 2082 Ice::Operand *Callee = getOperand(CalleeIndex);
1899 Ice::Type ReturnType = Ice::IceType_void; 2083 Ice::Type ReturnType = Ice::IceType_void;
1900 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = NULL; 2084 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr;
1901 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { 2085 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) {
1902 Function *Fcn = 2086 Function *Fcn =
1903 dyn_cast<Function>(Context->getGlobalValueByID(CalleeIndex)); 2087 dyn_cast<Function>(Context->getGlobalValueByID(CalleeIndex));
1904 if (Fcn == NULL) { 2088 if (Fcn == nullptr) {
1905 std::string Buffer; 2089 std::string Buffer;
1906 raw_string_ostream StrBuf(Buffer); 2090 raw_string_ostream StrBuf(Buffer);
1907 StrBuf << "Function call to non-function: " << *Callee; 2091 StrBuf << "Function call to non-function: " << *Callee;
1908 Error(StrBuf.str()); 2092 Error(StrBuf.str());
1909 return; 2093 return;
1910 } 2094 }
1911 2095
1912 FunctionType *FcnTy = Fcn->getFunctionType(); 2096 FunctionType *FcnTy = Fcn->getFunctionType();
1913 ReturnType = Context->convertToIceType(FcnTy->getReturnType()); 2097 ReturnType = Context->convertToIceType(FcnTy->getReturnType());
1914 2098
1915 // Check if this direct call is to an Intrinsic (starts with "llvm.") 2099 // Check if this direct call is to an Intrinsic (starts with "llvm.")
1916 static Ice::IceString LLVMPrefix("llvm."); 2100 static Ice::IceString LLVMPrefix("llvm.");
1917 Ice::IceString Name = Fcn->getName(); 2101 Ice::IceString Name = Fcn->getName();
1918 if (isStringPrefix(Name, LLVMPrefix)) { 2102 if (isStringPrefix(Name, LLVMPrefix)) {
1919 Ice::IceString Suffix = Name.substr(LLVMPrefix.size()); 2103 Ice::IceString Suffix = Name.substr(LLVMPrefix.size());
1920 IntrinsicInfo = 2104 IntrinsicInfo =
1921 getTranslator().getContext()->getIntrinsicsInfo().find(Suffix); 2105 getTranslator().getContext()->getIntrinsicsInfo().find(Suffix);
1922 if (!IntrinsicInfo) { 2106 if (!IntrinsicInfo) {
1923 std::string Buffer; 2107 std::string Buffer;
1924 raw_string_ostream StrBuf(Buffer); 2108 raw_string_ostream StrBuf(Buffer);
1925 StrBuf << "Invalid PNaCl intrinsic call to " << Name; 2109 StrBuf << "Invalid PNaCl intrinsic call to " << Name;
1926 Error(StrBuf.str()); 2110 Error(StrBuf.str());
1927 return; 2111 return;
1928 } 2112 }
1929 } 2113 }
1930 } else { 2114 } else {
1931 ReturnType = Context->convertToIceType(Context->getTypeByID(Values[2])); 2115 ReturnType = Context->getTypeByID(Values[2]);
1932 } 2116 }
1933 2117
1934 // Create the call instruction. 2118 // Create the call instruction.
1935 Ice::Variable *Dest = 2119 Ice::Variable *Dest = (ReturnType == Ice::IceType_void)
1936 (ReturnType == Ice::IceType_void) ? NULL : getNextInstVar(ReturnType); 2120 ? nullptr
2121 : getNextInstVar(ReturnType);
1937 Ice::SizeT NumParams = Values.size() - ParamsStartIndex; 2122 Ice::SizeT NumParams = Values.size() - ParamsStartIndex;
1938 Ice::InstCall *Inst = NULL; 2123 Ice::InstCall *Inst = nullptr;
1939 if (IntrinsicInfo) { 2124 if (IntrinsicInfo) {
1940 Inst = 2125 Inst =
1941 Ice::InstIntrinsicCall::create(Func, NumParams, Dest, Callee, 2126 Ice::InstIntrinsicCall::create(Func, NumParams, Dest, Callee,
1942 IntrinsicInfo->Info); 2127 IntrinsicInfo->Info);
1943 } else { 2128 } else {
1944 Inst = Ice::InstCall::create(Func, NumParams, Dest, Callee, IsTailCall); 2129 Inst = Ice::InstCall::create(Func, NumParams, Dest, Callee, IsTailCall);
1945 } 2130 }
1946 2131
1947 // Add parameters. 2132 // Add parameters.
1948 for (Ice::SizeT ParamIndex = 0; ParamIndex < NumParams; ++ParamIndex) { 2133 for (Ice::SizeT ParamIndex = 0; ParamIndex < NumParams; ++ParamIndex) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1992 } 2177 }
1993 } 2178 }
1994 2179
1995 CurrentNode->appendInst(Inst); 2180 CurrentNode->appendInst(Inst);
1996 return; 2181 return;
1997 } 2182 }
1998 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: { 2183 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: {
1999 // FORWARDTYPEREF: [opval, ty] 2184 // FORWARDTYPEREF: [opval, ty]
2000 if (!isValidRecordSize(2, "function block forward type ref")) 2185 if (!isValidRecordSize(2, "function block forward type ref"))
2001 return; 2186 return;
2002 setOperand(Values[0], createInstVar(Context->convertToIceType( 2187 setOperand(Values[0], createInstVar(Context->getTypeByID(Values[1])));
2003 Context->getTypeByID(Values[1]))));
2004 break; 2188 break;
2005 } 2189 }
2006 default: 2190 default:
2007 // Generate error message! 2191 // Generate error message!
2008 BlockParserBaseClass::ProcessRecord(); 2192 BlockParserBaseClass::ProcessRecord();
2009 break; 2193 break;
2010 } 2194 }
2011 } 2195 }
2012 2196
2013 /// Parses constants within a function block. 2197 /// Parses constants within a function block.
(...skipping 28 matching lines...) Expand all
2042 } 2226 }
2043 }; 2227 };
2044 2228
2045 void ConstantsParser::ProcessRecord() { 2229 void ConstantsParser::ProcessRecord() {
2046 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 2230 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
2047 switch (Record.GetCode()) { 2231 switch (Record.GetCode()) {
2048 case naclbitc::CST_CODE_SETTYPE: { 2232 case naclbitc::CST_CODE_SETTYPE: {
2049 // SETTYPE: [typeid] 2233 // SETTYPE: [typeid]
2050 if (!isValidRecordSize(1, "constants block set type")) 2234 if (!isValidRecordSize(1, "constants block set type"))
2051 return; 2235 return;
2052 NextConstantType = 2236 NextConstantType = Context->getTypeByID(Values[0]);
2053 Context->convertToIceType(Context->getTypeByID(Values[0]));
2054 if (NextConstantType == Ice::IceType_void) 2237 if (NextConstantType == Ice::IceType_void)
2055 Error("constants block set type not allowed for void type"); 2238 Error("constants block set type not allowed for void type");
2056 return; 2239 return;
2057 } 2240 }
2058 case naclbitc::CST_CODE_UNDEF: { 2241 case naclbitc::CST_CODE_UNDEF: {
2059 // UNDEF 2242 // UNDEF
2060 if (!isValidRecordSize(0, "constants block undef")) 2243 if (!isValidRecordSize(0, "constants block undef"))
2061 return; 2244 return;
2062 if (!isValidNextConstantType()) 2245 if (!isValidNextConstantType())
2063 return; 2246 return;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
2248 2431
2249 ~ModuleValuesymtabParser() override {} 2432 ~ModuleValuesymtabParser() override {}
2250 2433
2251 private: 2434 private:
2252 void setValueName(uint64_t Index, StringType &Name) override; 2435 void setValueName(uint64_t Index, StringType &Name) override;
2253 void setBbName(uint64_t Index, StringType &Name) override; 2436 void setBbName(uint64_t Index, StringType &Name) override;
2254 }; 2437 };
2255 2438
2256 void ModuleValuesymtabParser::setValueName(uint64_t Index, StringType &Name) { 2439 void ModuleValuesymtabParser::setValueName(uint64_t Index, StringType &Name) {
2257 Value *V = Context->getGlobalValueByID(Index); 2440 Value *V = Context->getGlobalValueByID(Index);
2258 if (V == NULL) { 2441 if (V == nullptr) {
2259 std::string Buffer; 2442 std::string Buffer;
2260 raw_string_ostream StrBuf(Buffer); 2443 raw_string_ostream StrBuf(Buffer);
2261 StrBuf << "Invalid global address ID in valuesymtab: " << Index; 2444 StrBuf << "Invalid global address ID in valuesymtab: " << Index;
2262 Error(StrBuf.str()); 2445 Error(StrBuf.str());
2263 return; 2446 return;
2264 } 2447 }
2265 V->setName(StringRef(Name.data(), Name.size())); 2448 V->setName(StringRef(Name.data(), Name.size()));
2266 } 2449 }
2267 2450
2268 void ModuleValuesymtabParser::setBbName(uint64_t Index, StringType &Name) { 2451 void ModuleValuesymtabParser::setBbName(uint64_t Index, StringType &Name) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2312 raw_string_ostream StrBuf(Buffer); 2495 raw_string_ostream StrBuf(Buffer);
2313 StrBuf << "Unknown bitstream version: " << Version; 2496 StrBuf << "Unknown bitstream version: " << Version;
2314 Error(StrBuf.str()); 2497 Error(StrBuf.str());
2315 } 2498 }
2316 return; 2499 return;
2317 } 2500 }
2318 case naclbitc::MODULE_CODE_FUNCTION: { 2501 case naclbitc::MODULE_CODE_FUNCTION: {
2319 // FUNCTION: [type, callingconv, isproto, linkage] 2502 // FUNCTION: [type, callingconv, isproto, linkage]
2320 if (!isValidRecordSize(4, "Function heading")) 2503 if (!isValidRecordSize(4, "Function heading"))
2321 return; 2504 return;
2322 Type *Ty = Context->getTypeByID(Values[0]); 2505 const Ice::FcnSigType &Ty = Context->getFcnSigTypeByID(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; 2506 CallingConv::ID CallingConv;
2332 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) { 2507 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) {
2333 std::string Buffer; 2508 std::string Buffer;
2334 raw_string_ostream StrBuf(Buffer); 2509 raw_string_ostream StrBuf(Buffer);
2335 StrBuf << "Function heading has unknown calling convention: " 2510 StrBuf << "Function heading has unknown calling convention: "
2336 << Values[1]; 2511 << Values[1];
2337 Error(StrBuf.str()); 2512 Error(StrBuf.str());
2338 return; 2513 return;
2339 } 2514 }
2340 GlobalValue::LinkageTypes Linkage; 2515 GlobalValue::LinkageTypes Linkage;
2341 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) { 2516 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) {
2342 std::string Buffer; 2517 std::string Buffer;
2343 raw_string_ostream StrBuf(Buffer); 2518 raw_string_ostream StrBuf(Buffer);
2344 StrBuf << "Function heading has unknown linkage. Found " << Values[3]; 2519 StrBuf << "Function heading has unknown linkage. Found " << Values[3];
2345 Error(StrBuf.str()); 2520 Error(StrBuf.str());
2346 return; 2521 return;
2347 } 2522 }
2348 Function *Func = Function::Create(FTy, Linkage, "", Context->getModule()); 2523 SmallVector<Type *, 8> ArgTys;
2524 for (Ice::Type ArgType : Ty.getArgList()) {
2525 ArgTys.push_back(Context->convertToLLVMType(ArgType));
2526 }
2527 Function *Func = Function::Create(
2528 FunctionType::get(Context->convertToLLVMType(Ty.getReturnType()),
2529 ArgTys, false),
2530 Linkage, "", Context->getModule());
2349 Func->setCallingConv(CallingConv); 2531 Func->setCallingConv(CallingConv);
2350 if (Values[2] == 0) 2532 if (Values[2] == 0)
2351 Context->setNextValueIDAsImplementedFunction(); 2533 Context->setNextValueIDAsImplementedFunction();
2352 Context->setNextFunctionID(Func); 2534 Context->setNextFunctionID(Func);
2353 // TODO(kschimpf) verify if Func matches PNaCl ABI. 2535 // TODO(kschimpf) verify if Func matches PNaCl ABI.
2354 return; 2536 return;
2355 } 2537 }
2356 default: 2538 default:
2357 BlockParserBaseClass::ProcessRecord(); 2539 BlockParserBaseClass::ProcessRecord();
2358 return; 2540 return;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2417 2599
2418 if (TopLevelBlocks != 1) { 2600 if (TopLevelBlocks != 1) {
2419 errs() << IRFilename 2601 errs() << IRFilename
2420 << ": Contains more than one module. Found: " << TopLevelBlocks 2602 << ": Contains more than one module. Found: " << TopLevelBlocks
2421 << "\n"; 2603 << "\n";
2422 ErrorStatus = true; 2604 ErrorStatus = true;
2423 } 2605 }
2424 } 2606 }
2425 2607
2426 } // end of namespace Ice 2608 } // end of namespace Ice
OLDNEW
« src/IceTypes.h ('K') | « src/IceTypes.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698