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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 class TopLevelParser : public NaClBitcodeParser { | 154 class TopLevelParser : public NaClBitcodeParser { |
155 TopLevelParser(const TopLevelParser &) = delete; | 155 TopLevelParser(const TopLevelParser &) = delete; |
156 TopLevelParser &operator=(const TopLevelParser &) = delete; | 156 TopLevelParser &operator=(const TopLevelParser &) = delete; |
157 | 157 |
158 public: | 158 public: |
159 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; | 159 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; |
160 | 160 |
161 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, | 161 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, |
162 NaClBitstreamCursor &Cursor, bool &ErrorStatus) | 162 NaClBitstreamCursor &Cursor, bool &ErrorStatus) |
163 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), | 163 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), |
164 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), | 164 ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0), |
165 NumFunctionBlocks(0), BlockParser(nullptr) {} | 165 BlockParser(nullptr) {} |
166 | 166 |
167 ~TopLevelParser() override {} | 167 ~TopLevelParser() override {} |
168 | 168 |
169 Ice::Translator &getTranslator() { return Translator; } | 169 Ice::Translator &getTranslator() { return Translator; } |
170 | 170 |
171 void setBlockParser(BlockParserBaseClass *NewBlockParser) { | 171 void setBlockParser(BlockParserBaseClass *NewBlockParser) { |
172 BlockParser = NewBlockParser; | 172 BlockParser = NewBlockParser; |
173 } | 173 } |
174 | 174 |
175 // Generates error with given Message. Always returns true. | 175 // Generates error with given Message. Always returns true. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 const Ice::FuncSigType &getFuncSigTypeByID(unsigned ID) { | 220 const Ice::FuncSigType &getFuncSigTypeByID(unsigned ID) { |
221 const ExtendedType *Ty = getTypeByIDAsKind(ID, ExtendedType::FuncSig); | 221 const ExtendedType *Ty = getTypeByIDAsKind(ID, ExtendedType::FuncSig); |
222 if (Ty == nullptr) | 222 if (Ty == nullptr) |
223 // Return error recovery value. | 223 // Return error recovery value. |
224 return UndefinedFuncSigType; | 224 return UndefinedFuncSigType; |
225 return cast<FuncSigExtendedType>(Ty)->getSignature(); | 225 return cast<FuncSigExtendedType>(Ty)->getSignature(); |
226 } | 226 } |
227 | 227 |
228 /// Sets the next function ID to the given LLVM function. | 228 /// Sets the next function ID to the given LLVM function. |
229 void setNextFunctionID(Ice::FunctionDeclaration *Fcn) { | 229 void setNextFunctionID(Ice::FunctionDeclaration *Fcn) { |
230 ++NumFunctionIds; | |
231 FunctionDeclarationList.push_back(Fcn); | 230 FunctionDeclarationList.push_back(Fcn); |
232 } | 231 } |
233 | 232 |
234 /// Defines the next function ID as one that has an implementation | |
235 /// (i.e a corresponding function block in the bitcode). | |
236 void setNextValueIDAsImplementedFunction() { | |
237 DefiningFunctionDeclarationsList.push_back(FunctionDeclarationList.size()); | |
238 } | |
239 | |
240 /// Returns the value id that should be associated with the the | 233 /// Returns the value id that should be associated with the the |
241 /// current function block. Increments internal counters during call | 234 /// current function block. Increments internal counters during call |
242 /// so that it will be in correct position for next function block. | 235 /// so that it will be in correct position for next function block. |
243 unsigned getNextFunctionBlockValueID() { | 236 unsigned getNextFunctionBlockValueID() { |
244 if (NumFunctionBlocks >= DefiningFunctionDeclarationsList.size()) | 237 unsigned NumDeclaredFunctions = FunctionDeclarationList.size(); |
Jim Stichnoth
2015/01/26 23:12:54
It's probably a good idea to change some of these
Karl
2015/01/28 18:59:18
For this field, and the local here, I will fix it
| |
238 while (NextDefiningFunctionID < NumDeclaredFunctions && | |
239 FunctionDeclarationList[NextDefiningFunctionID]->isProto()) | |
240 ++NextDefiningFunctionID; | |
241 if (NextDefiningFunctionID >= NumDeclaredFunctions) | |
245 report_fatal_error( | 242 report_fatal_error( |
246 "More function blocks than defined function addresses"); | 243 "More function blocks than defined function addresses"); |
247 return DefiningFunctionDeclarationsList[NumFunctionBlocks++]; | 244 return NextDefiningFunctionID++; |
248 } | 245 } |
249 | 246 |
250 /// Returns the function associated with ID. | 247 /// Returns the function associated with ID. |
251 Ice::FunctionDeclaration *getFunctionByID(unsigned ID) { | 248 Ice::FunctionDeclaration *getFunctionByID(unsigned ID) { |
252 if (ID < FunctionDeclarationList.size()) | 249 if (ID < FunctionDeclarationList.size()) |
253 return FunctionDeclarationList[ID]; | 250 return FunctionDeclarationList[ID]; |
254 return reportGetFunctionByIDError(ID); | 251 return reportGetFunctionByIDError(ID); |
255 } | 252 } |
256 | 253 |
257 /// Returns the list of function declarations. | 254 /// Returns the list of function declarations. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 SuppressMangling = false; | 303 SuppressMangling = false; |
307 } | 304 } |
308 const Ice::RelocOffsetT Offset = 0; | 305 const Ice::RelocOffsetT Offset = 0; |
309 C = getTranslator().getContext()->getConstantSym(Offset, Name, | 306 C = getTranslator().getContext()->getConstantSym(Offset, Name, |
310 SuppressMangling); | 307 SuppressMangling); |
311 ValueIDConstants[ID] = C; | 308 ValueIDConstants[ID] = C; |
312 return C; | 309 return C; |
313 } | 310 } |
314 | 311 |
315 /// Returns the number of function declarations in the bitcode file. | 312 /// Returns the number of function declarations in the bitcode file. |
316 unsigned getNumFunctionIDs() const { return NumFunctionIds; } | 313 unsigned getNumFunctionIDs() const { return FunctionDeclarationList.size(); } |
317 | 314 |
318 /// Returns the number of global declarations (i.e. IDs) defined in | 315 /// Returns the number of global declarations (i.e. IDs) defined in |
319 /// the bitcode file. | 316 /// the bitcode file. |
320 unsigned getNumGlobalIDs() const { | 317 unsigned getNumGlobalIDs() const { |
321 return FunctionDeclarationList.size() + VariableDeclarations.size(); | 318 return FunctionDeclarationList.size() + VariableDeclarations.size(); |
322 } | 319 } |
323 | 320 |
324 /// Creates Count global variable declarations. | 321 /// Creates Count global variable declarations. |
325 void CreateGlobalVariables(size_t Count) { | 322 void CreateGlobalVariables(size_t Count) { |
326 assert(VariableDeclarations.empty()); | 323 assert(VariableDeclarations.empty()); |
(...skipping 11 matching lines...) Expand all Loading... | |
338 /// Returns the global variable declaration with the given index. | 335 /// Returns the global variable declaration with the given index. |
339 Ice::VariableDeclaration *getGlobalVariableByID(unsigned Index) { | 336 Ice::VariableDeclaration *getGlobalVariableByID(unsigned Index) { |
340 if (Index < VariableDeclarations.size()) | 337 if (Index < VariableDeclarations.size()) |
341 return VariableDeclarations[Index]; | 338 return VariableDeclarations[Index]; |
342 return reportGetGlobalVariableByIDError(Index); | 339 return reportGetGlobalVariableByIDError(Index); |
343 } | 340 } |
344 | 341 |
345 /// Returns the global declaration (variable or function) with the | 342 /// Returns the global declaration (variable or function) with the |
346 /// given Index. | 343 /// given Index. |
347 Ice::GlobalDeclaration *getGlobalDeclarationByID(size_t Index) { | 344 Ice::GlobalDeclaration *getGlobalDeclarationByID(size_t Index) { |
345 size_t NumFunctionIds = FunctionDeclarationList.size(); | |
348 if (Index < NumFunctionIds) | 346 if (Index < NumFunctionIds) |
349 return getFunctionByID(Index); | 347 return getFunctionByID(Index); |
350 else | 348 else |
351 return getGlobalVariableByID(Index - NumFunctionIds); | 349 return getGlobalVariableByID(Index - NumFunctionIds); |
352 } | 350 } |
353 | 351 |
354 /// Returns the list of parsed global variable declarations. | 352 /// Returns the list of parsed global variable declarations. |
355 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { | 353 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { |
356 return VariableDeclarations; | 354 return VariableDeclarations; |
357 } | 355 } |
358 | 356 |
359 private: | 357 private: |
360 // The translator associated with the parser. | 358 // The translator associated with the parser. |
361 Ice::Translator &Translator; | 359 Ice::Translator &Translator; |
362 // The bitcode header. | 360 // The bitcode header. |
363 NaClBitcodeHeader &Header; | 361 NaClBitcodeHeader &Header; |
364 // The exit status that should be set to true if an error occurs. | 362 // The exit status that should be set to true if an error occurs. |
365 bool &ErrorStatus; | 363 bool &ErrorStatus; |
366 // The number of errors reported. | 364 // The number of errors reported. |
367 unsigned NumErrors; | 365 unsigned NumErrors; |
368 // The types associated with each type ID. | 366 // The types associated with each type ID. |
369 std::vector<ExtendedType> TypeIDValues; | 367 std::vector<ExtendedType> TypeIDValues; |
370 // The set of functions. | 368 // The set of functions. |
371 FunctionDeclarationListType FunctionDeclarationList; | 369 FunctionDeclarationListType FunctionDeclarationList; |
372 // The set of global variables. | 370 // The set of global variables. |
373 Ice::Translator::VariableDeclarationListType VariableDeclarations; | 371 Ice::Translator::VariableDeclarationListType VariableDeclarations; |
374 // Relocatable constants associated with global declarations. | 372 // Relocatable constants associated with global declarations. |
375 std::vector<Ice::Constant *> ValueIDConstants; | 373 std::vector<Ice::Constant *> ValueIDConstants; |
376 // The number of function declarations (i.e. IDs). | 374 // The ID of the next possible defined function ID. |
377 unsigned NumFunctionIds; | 375 unsigned NextDefiningFunctionID; |
jvoung (off chromium)
2015/01/26 22:58:02
Might be more clear to put this next FunctionDecla
Karl
2015/01/28 18:59:18
Done.
| |
378 // The number of function blocks (processed so far). | |
379 unsigned NumFunctionBlocks; | |
380 // The list of function declaration IDs (in the order found) that | |
381 // aren't just proto declarations. | |
382 // TODO(kschimpf): Instead of using this list, just use | |
383 // FunctionDeclarationList, and the isProto member function. | |
384 std::vector<unsigned> DefiningFunctionDeclarationsList; | |
385 // Error recovery value to use when getFuncSigTypeByID fails. | 376 // Error recovery value to use when getFuncSigTypeByID fails. |
386 Ice::FuncSigType UndefinedFuncSigType; | 377 Ice::FuncSigType UndefinedFuncSigType; |
387 // The block parser currently being applied. Used for error | 378 // The block parser currently being applied. Used for error |
388 // reporting. | 379 // reporting. |
389 BlockParserBaseClass *BlockParser; | 380 BlockParserBaseClass *BlockParser; |
390 | 381 |
391 bool ParseBlock(unsigned BlockID) override; | 382 bool ParseBlock(unsigned BlockID) override; |
392 | 383 |
393 // Gets extended type associated with the given index, assuming the | 384 // Gets extended type associated with the given index, assuming the |
394 // extended type is of the WantedKind. Generates error message if | 385 // extended type is of the WantedKind. Generates error message if |
(...skipping 2510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2905 Error(StrBuf.str()); | 2896 Error(StrBuf.str()); |
2906 return; | 2897 return; |
2907 } | 2898 } |
2908 GlobalValue::LinkageTypes Linkage; | 2899 GlobalValue::LinkageTypes Linkage; |
2909 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) { | 2900 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) { |
2910 std::string Buffer; | 2901 std::string Buffer; |
2911 raw_string_ostream StrBuf(Buffer); | 2902 raw_string_ostream StrBuf(Buffer); |
2912 StrBuf << "Function address has unknown linkage. Found " << Values[3]; | 2903 StrBuf << "Function address has unknown linkage. Found " << Values[3]; |
2913 Error(StrBuf.str()); | 2904 Error(StrBuf.str()); |
2914 return; | 2905 return; |
2915 } | 2906 } |
jvoung (off chromium)
2015/01/26 22:58:02
Might be more clear to have a "bool IsProto = Valu
Karl
2015/01/28 18:59:18
Done.
| |
2916 Ice::FunctionDeclaration *Func = Ice::FunctionDeclaration::create( | 2907 Ice::FunctionDeclaration *Func = Ice::FunctionDeclaration::create( |
2917 Signature, CallingConv, Linkage, Values[2] == 0); | 2908 Signature, CallingConv, Linkage, Values[2] == 1); |
2918 if (Values[2] == 0) | |
2919 Context->setNextValueIDAsImplementedFunction(); | |
2920 Context->setNextFunctionID(Func); | 2909 Context->setNextFunctionID(Func); |
2921 return; | 2910 return; |
2922 } | 2911 } |
2923 default: | 2912 default: |
2924 BlockParserBaseClass::ProcessRecord(); | 2913 BlockParserBaseClass::ProcessRecord(); |
2925 return; | 2914 return; |
2926 } | 2915 } |
2927 } | 2916 } |
2928 | 2917 |
2929 bool TopLevelParser::ParseBlock(unsigned BlockID) { | 2918 bool TopLevelParser::ParseBlock(unsigned BlockID) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2989 | 2978 |
2990 if (TopLevelBlocks != 1) { | 2979 if (TopLevelBlocks != 1) { |
2991 errs() << IRFilename | 2980 errs() << IRFilename |
2992 << ": Contains more than one module. Found: " << TopLevelBlocks | 2981 << ": Contains more than one module. Found: " << TopLevelBlocks |
2993 << "\n"; | 2982 << "\n"; |
2994 ErrorStatus = true; | 2983 ErrorStatus = true; |
2995 } | 2984 } |
2996 } | 2985 } |
2997 | 2986 |
2998 } // end of namespace Ice | 2987 } // end of namespace Ice |
OLD | NEW |