Chromium Code Reviews| 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 /// \file | 10 /// \file |
| 11 /// This file implements the PNaCl bitcode file to Ice, to machine code | 11 /// This file implements the PNaCl bitcode file to Ice, to machine code |
| 12 /// translator. | 12 /// translator. |
| 13 /// | 13 /// |
| 14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
| 15 | 15 |
| 16 #include "PNaClTranslator.h" | 16 #include "PNaClTranslator.h" |
| 17 | 17 |
| 18 #include "IceAPInt.h" | |
| 19 #include "IceAPFloat.h" | |
| 20 #include "IceCfg.h" | 18 #include "IceCfg.h" |
| 21 #include "IceCfgNode.h" | 19 #include "IceCfgNode.h" |
| 22 #include "IceClFlags.h" | 20 #include "IceClFlags.h" |
| 23 #include "IceDefs.h" | 21 #include "IceDefs.h" |
| 24 #include "IceGlobalInits.h" | 22 #include "IceGlobalInits.h" |
| 25 #include "IceInst.h" | 23 #include "IceInst.h" |
| 26 #include "IceOperand.h" | 24 #include "IceOperand.h" |
| 27 | 25 |
| 28 #pragma clang diagnostic push | 26 #pragma clang diagnostic push |
| 29 #pragma clang diagnostic ignored "-Wunused-parameter" | 27 #pragma clang diagnostic ignored "-Wunused-parameter" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 break; | 158 break; |
| 161 } | 159 } |
| 162 case FuncSig: { | 160 case FuncSig: { |
| 163 Stream << " " << Signature; | 161 Stream << " " << Signature; |
| 164 } | 162 } |
| 165 default: | 163 default: |
| 166 break; | 164 break; |
| 167 } | 165 } |
| 168 } | 166 } |
| 169 | 167 |
| 168 // Models integer literals as a sequence of bits. Based on llvm::APInt. | |
| 169 class SzInt { | |
|
Jim Stichnoth
2015/11/30 21:58:11
Can this go into an anonymous namespace?
And to a
Jim Stichnoth
2015/11/30 23:20:22
Sorry, I see now that it's already in a ginormous
| |
| 170 SzInt() = delete; | |
| 171 SzInt(const SzInt &) = delete; | |
| 172 SzInt &operator=(const SzInt &) = delete; | |
| 173 | |
| 174 public: | |
| 175 SzInt(Ice::SizeT Bits, uint64_t Val) : BitWidth(Bits), Val(Val) { | |
| 176 assert(Bits && "bitwidth too small"); | |
| 177 assert(Bits <= BITS_PER_WORD && "bitwidth too big"); | |
| 178 clearUnusedBits(); | |
| 179 } | |
| 180 | |
| 181 int64_t getSExtValue() const { | |
| 182 return static_cast<int64_t>(Val << (BITS_PER_WORD - BitWidth)) >> | |
| 183 (BITS_PER_WORD - BitWidth); | |
| 184 } | |
| 185 | |
| 186 template <typename IntType, typename FpType> | |
| 187 inline FpType convertToFp() const { | |
| 188 static_assert(sizeof(IntType) == sizeof(FpType), | |
| 189 "IntType and FpType should be the same width"); | |
| 190 assert(BitWidth == sizeof(IntType) * CHAR_BIT); | |
|
rkotlerimgtec
2015/11/30 22:12:48
I think this form of using unions to do integer to
Jim Stichnoth
2015/11/30 23:20:22
This is a good point, unions aren't supposed to be
Karl
2015/12/01 00:00:49
Done.
| |
| 191 union { | |
| 192 IntType IntValue; | |
| 193 FpType FpValue; | |
| 194 } Converter; | |
| 195 Converter.IntValue = static_cast<IntType>(Val); | |
| 196 return Converter.FpValue; | |
| 197 } | |
| 198 | |
| 199 private: | |
| 200 /// Bits in the (internal) value. | |
| 201 static const Ice::SizeT BITS_PER_WORD = sizeof(uint64_t) * CHAR_BIT; | |
| 202 | |
| 203 uint32_t BitWidth; /// The number of bits in the floating point number. | |
| 204 uint64_t Val; /// The (64-bit) equivalent integer value. | |
| 205 | |
| 206 /// Clear unused high order bits. | |
| 207 void clearUnusedBits() { | |
| 208 // If all bits are used, we want to leave the value alone. | |
| 209 if (BitWidth == BITS_PER_WORD) | |
| 210 return; | |
| 211 | |
| 212 // Mask out the high bits. | |
| 213 Val &= ~static_cast<uint64_t>(0) >> (BITS_PER_WORD - BitWidth); | |
| 214 } | |
| 215 }; | |
| 216 | |
| 170 class BlockParserBaseClass; | 217 class BlockParserBaseClass; |
| 171 | 218 |
| 172 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 219 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
| 173 class TopLevelParser : public NaClBitcodeParser { | 220 class TopLevelParser : public NaClBitcodeParser { |
| 174 TopLevelParser() = delete; | 221 TopLevelParser() = delete; |
| 175 TopLevelParser(const TopLevelParser &) = delete; | 222 TopLevelParser(const TopLevelParser &) = delete; |
| 176 TopLevelParser &operator=(const TopLevelParser &) = delete; | 223 TopLevelParser &operator=(const TopLevelParser &) = delete; |
| 177 | 224 |
| 178 public: | 225 public: |
| 179 TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor, | 226 TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor, |
| (...skipping 2347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2527 for (uint32_t CaseIndex = 0; CaseIndex < NumCases; | 2574 for (uint32_t CaseIndex = 0; CaseIndex < NumCases; |
| 2528 ++CaseIndex, ValCaseIndex += 4) { | 2575 ++CaseIndex, ValCaseIndex += 4) { |
| 2529 if (Values[ValCaseIndex] != 1 || Values[ValCaseIndex + 1] != 1) { | 2576 if (Values[ValCaseIndex] != 1 || Values[ValCaseIndex + 1] != 1) { |
| 2530 std::string Buffer; | 2577 std::string Buffer; |
| 2531 raw_string_ostream StrBuf(Buffer); | 2578 raw_string_ostream StrBuf(Buffer); |
| 2532 StrBuf << "Sequence [1, 1, value, label] expected for case entry " | 2579 StrBuf << "Sequence [1, 1, value, label] expected for case entry " |
| 2533 << "in switch record. (at index" << ValCaseIndex << ")"; | 2580 << "in switch record. (at index" << ValCaseIndex << ")"; |
| 2534 Error(StrBuf.str()); | 2581 Error(StrBuf.str()); |
| 2535 return; | 2582 return; |
| 2536 } | 2583 } |
| 2537 Ice::APInt Value(BitWidth, | 2584 SzInt Value(BitWidth, |
| 2538 NaClDecodeSignRotatedValue(Values[ValCaseIndex + 2])); | 2585 NaClDecodeSignRotatedValue(Values[ValCaseIndex + 2])); |
| 2539 if (isIRGenDisabled) | 2586 if (isIRGenDisabled) |
| 2540 continue; | 2587 continue; |
| 2541 Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]); | 2588 Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]); |
| 2542 if (Label == nullptr) | 2589 if (Label == nullptr) |
| 2543 return; | 2590 return; |
| 2544 Switch->addBranch(CaseIndex, Value.getSExtValue(), Label); | 2591 Switch->addBranch(CaseIndex, Value.getSExtValue(), Label); |
| 2545 } | 2592 } |
| 2546 if (isIRGenDisabled) | 2593 if (isIRGenDisabled) |
| 2547 return; | 2594 return; |
| 2548 CurrentNode->appendInst(Switch.release()); | 2595 CurrentNode->appendInst(Switch.release()); |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2917 // INTEGER: [intval] | 2964 // INTEGER: [intval] |
| 2918 if (!isValidRecordSize(1, "integer")) | 2965 if (!isValidRecordSize(1, "integer")) |
| 2919 return; | 2966 return; |
| 2920 if (!isValidNextConstantType()) | 2967 if (!isValidNextConstantType()) |
| 2921 return; | 2968 return; |
| 2922 if (isIRGenerationDisabled()) { | 2969 if (isIRGenerationDisabled()) { |
| 2923 FuncParser->setNextConstantID(nullptr); | 2970 FuncParser->setNextConstantID(nullptr); |
| 2924 return; | 2971 return; |
| 2925 } | 2972 } |
| 2926 if (Ice::isScalarIntegerType(NextConstantType)) { | 2973 if (Ice::isScalarIntegerType(NextConstantType)) { |
| 2927 Ice::APInt Value(Ice::getScalarIntBitWidth(NextConstantType), | 2974 SzInt Value(Ice::getScalarIntBitWidth(NextConstantType), |
| 2928 NaClDecodeSignRotatedValue(Values[0])); | 2975 NaClDecodeSignRotatedValue(Values[0])); |
| 2929 if (Ice::Constant *C = getContext()->getConstantInt( | 2976 if (Ice::Constant *C = getContext()->getConstantInt( |
| 2930 NextConstantType, Value.getSExtValue())) { | 2977 NextConstantType, Value.getSExtValue())) { |
| 2931 FuncParser->setNextConstantID(C); | 2978 FuncParser->setNextConstantID(C); |
| 2932 return; | 2979 return; |
| 2933 } | 2980 } |
| 2934 } | 2981 } |
| 2935 std::string Buffer; | 2982 std::string Buffer; |
| 2936 raw_string_ostream StrBuf(Buffer); | 2983 raw_string_ostream StrBuf(Buffer); |
| 2937 StrBuf << "constant block integer record for non-integer type " | 2984 StrBuf << "constant block integer record for non-integer type " |
| 2938 << NextConstantType; | 2985 << NextConstantType; |
| 2939 Error(StrBuf.str()); | 2986 Error(StrBuf.str()); |
| 2940 return; | 2987 return; |
| 2941 } | 2988 } |
| 2942 case naclbitc::CST_CODE_FLOAT: { | 2989 case naclbitc::CST_CODE_FLOAT: { |
| 2943 // FLOAT: [fpval] | 2990 // FLOAT: [fpval] |
| 2944 if (!isValidRecordSize(1, "float")) | 2991 if (!isValidRecordSize(1, "float")) |
| 2945 return; | 2992 return; |
| 2946 if (!isValidNextConstantType()) | 2993 if (!isValidNextConstantType()) |
| 2947 return; | 2994 return; |
| 2948 if (isIRGenerationDisabled()) { | 2995 if (isIRGenerationDisabled()) { |
| 2949 FuncParser->setNextConstantID(nullptr); | 2996 FuncParser->setNextConstantID(nullptr); |
| 2950 return; | 2997 return; |
| 2951 } | 2998 } |
| 2952 switch (NextConstantType) { | 2999 switch (NextConstantType) { |
| 2953 case Ice::IceType_f32: { | 3000 case Ice::IceType_f32: { |
| 2954 const Ice::APInt IntValue(32, static_cast<uint32_t>(Values[0])); | 3001 const SzInt Value(32, static_cast<uint32_t>(Values[0])); |
| 2955 float FpValue = Ice::convertAPIntToFp<int32_t, float>(IntValue); | 3002 float FpValue = Value.convertToFp<int32_t, float>(); |
| 2956 FuncParser->setNextConstantID(getContext()->getConstantFloat(FpValue)); | 3003 FuncParser->setNextConstantID(getContext()->getConstantFloat(FpValue)); |
| 2957 return; | 3004 return; |
| 2958 } | 3005 } |
| 2959 case Ice::IceType_f64: { | 3006 case Ice::IceType_f64: { |
| 2960 const Ice::APInt IntValue(64, Values[0]); | 3007 const SzInt Value(64, Values[0]); |
| 2961 double FpValue = Ice::convertAPIntToFp<uint64_t, double>(IntValue); | 3008 double FpValue = Value.convertToFp<uint64_t, double>(); |
| 2962 FuncParser->setNextConstantID(getContext()->getConstantDouble(FpValue)); | 3009 FuncParser->setNextConstantID(getContext()->getConstantDouble(FpValue)); |
| 2963 return; | 3010 return; |
| 2964 } | 3011 } |
| 2965 default: { | 3012 default: { |
| 2966 std::string Buffer; | 3013 std::string Buffer; |
| 2967 raw_string_ostream StrBuf(Buffer); | 3014 raw_string_ostream StrBuf(Buffer); |
| 2968 StrBuf << "constant block float record for non-floating type " | 3015 StrBuf << "constant block float record for non-floating type " |
| 2969 << NextConstantType; | 3016 << NextConstantType; |
| 2970 Error(StrBuf.str()); | 3017 Error(StrBuf.str()); |
| 2971 return; | 3018 return; |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3297 raw_string_ostream StrBuf(Buffer); | 3344 raw_string_ostream StrBuf(Buffer); |
| 3298 StrBuf << IRFilename << ": Does not contain a module!"; | 3345 StrBuf << IRFilename << ": Does not contain a module!"; |
| 3299 llvm::report_fatal_error(StrBuf.str()); | 3346 llvm::report_fatal_error(StrBuf.str()); |
| 3300 } | 3347 } |
| 3301 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { | 3348 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { |
| 3302 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); | 3349 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); |
| 3303 } | 3350 } |
| 3304 } | 3351 } |
| 3305 | 3352 |
| 3306 } // end of namespace Ice | 3353 } // end of namespace Ice |
| OLD | NEW |