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

Side by Side Diff: src/PNaClTranslator.cpp

Issue 1484003003: Clean up implementation reading literal constants. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix issues raised in last patch. Created 5 years 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
« no previous file with comments | « src/IceAPInt.h ('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 /// \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
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. Used to read integer values
169 // from bitcode files. Based on llvm::APInt.
170 class BitcodeInt {
171 BitcodeInt() = delete;
172 BitcodeInt(const BitcodeInt &) = delete;
173 BitcodeInt &operator=(const BitcodeInt &) = delete;
174
175 public:
176 BitcodeInt(Ice::SizeT Bits, uint64_t Val) : BitWidth(Bits), Val(Val) {
177 assert(Bits && "bitwidth too small");
178 assert(Bits <= BITS_PER_WORD && "bitwidth too big");
179 clearUnusedBits();
180 }
181
182 int64_t getSExtValue() const {
183 return static_cast<int64_t>(Val << (BITS_PER_WORD - BitWidth)) >>
184 (BITS_PER_WORD - BitWidth);
185 }
186
187 template <typename IntType, typename FpType>
188 inline FpType convertToFp() const {
189 static_assert(sizeof(IntType) == sizeof(FpType),
190 "IntType and FpType should be the same width");
191 assert(BitWidth == sizeof(IntType) * CHAR_BIT);
192 auto V = static_cast<IntType>(Val);
193 return reinterpret_cast<FpType &>(V);
194 }
195
196 private:
197 /// Bits in the (internal) value.
198 static const Ice::SizeT BITS_PER_WORD = sizeof(uint64_t) * CHAR_BIT;
199
200 uint32_t BitWidth; /// The number of bits in the floating point number.
201 uint64_t Val; /// The (64-bit) equivalent integer value.
202
203 /// Clear unused high order bits.
204 void clearUnusedBits() {
205 // If all bits are used, we want to leave the value alone.
206 if (BitWidth == BITS_PER_WORD)
207 return;
208
209 // Mask out the high bits.
210 Val &= ~static_cast<uint64_t>(0) >> (BITS_PER_WORD - BitWidth);
211 }
212 };
213
170 class BlockParserBaseClass; 214 class BlockParserBaseClass;
171 215
172 // Top-level class to read PNaCl bitcode files, and translate to ICE. 216 // Top-level class to read PNaCl bitcode files, and translate to ICE.
173 class TopLevelParser : public NaClBitcodeParser { 217 class TopLevelParser : public NaClBitcodeParser {
174 TopLevelParser() = delete; 218 TopLevelParser() = delete;
175 TopLevelParser(const TopLevelParser &) = delete; 219 TopLevelParser(const TopLevelParser &) = delete;
176 TopLevelParser &operator=(const TopLevelParser &) = delete; 220 TopLevelParser &operator=(const TopLevelParser &) = delete;
177 221
178 public: 222 public:
179 TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor, 223 TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor,
(...skipping 2347 matching lines...) Expand 10 before | Expand all | Expand 10 after
2527 for (uint32_t CaseIndex = 0; CaseIndex < NumCases; 2571 for (uint32_t CaseIndex = 0; CaseIndex < NumCases;
2528 ++CaseIndex, ValCaseIndex += 4) { 2572 ++CaseIndex, ValCaseIndex += 4) {
2529 if (Values[ValCaseIndex] != 1 || Values[ValCaseIndex + 1] != 1) { 2573 if (Values[ValCaseIndex] != 1 || Values[ValCaseIndex + 1] != 1) {
2530 std::string Buffer; 2574 std::string Buffer;
2531 raw_string_ostream StrBuf(Buffer); 2575 raw_string_ostream StrBuf(Buffer);
2532 StrBuf << "Sequence [1, 1, value, label] expected for case entry " 2576 StrBuf << "Sequence [1, 1, value, label] expected for case entry "
2533 << "in switch record. (at index" << ValCaseIndex << ")"; 2577 << "in switch record. (at index" << ValCaseIndex << ")";
2534 Error(StrBuf.str()); 2578 Error(StrBuf.str());
2535 return; 2579 return;
2536 } 2580 }
2537 Ice::APInt Value(BitWidth, 2581 BitcodeInt Value(BitWidth,
2538 NaClDecodeSignRotatedValue(Values[ValCaseIndex + 2])); 2582 NaClDecodeSignRotatedValue(Values[ValCaseIndex + 2]));
2539 if (isIRGenDisabled) 2583 if (isIRGenDisabled)
2540 continue; 2584 continue;
2541 Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]); 2585 Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]);
2542 if (Label == nullptr) 2586 if (Label == nullptr)
2543 return; 2587 return;
2544 Switch->addBranch(CaseIndex, Value.getSExtValue(), Label); 2588 Switch->addBranch(CaseIndex, Value.getSExtValue(), Label);
2545 } 2589 }
2546 if (isIRGenDisabled) 2590 if (isIRGenDisabled)
2547 return; 2591 return;
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
2917 // INTEGER: [intval] 2961 // INTEGER: [intval]
2918 if (!isValidRecordSize(1, "integer")) 2962 if (!isValidRecordSize(1, "integer"))
2919 return; 2963 return;
2920 if (!isValidNextConstantType()) 2964 if (!isValidNextConstantType())
2921 return; 2965 return;
2922 if (isIRGenerationDisabled()) { 2966 if (isIRGenerationDisabled()) {
2923 FuncParser->setNextConstantID(nullptr); 2967 FuncParser->setNextConstantID(nullptr);
2924 return; 2968 return;
2925 } 2969 }
2926 if (Ice::isScalarIntegerType(NextConstantType)) { 2970 if (Ice::isScalarIntegerType(NextConstantType)) {
2927 Ice::APInt Value(Ice::getScalarIntBitWidth(NextConstantType), 2971 BitcodeInt Value(Ice::getScalarIntBitWidth(NextConstantType),
2928 NaClDecodeSignRotatedValue(Values[0])); 2972 NaClDecodeSignRotatedValue(Values[0]));
2929 if (Ice::Constant *C = getContext()->getConstantInt( 2973 if (Ice::Constant *C = getContext()->getConstantInt(
2930 NextConstantType, Value.getSExtValue())) { 2974 NextConstantType, Value.getSExtValue())) {
2931 FuncParser->setNextConstantID(C); 2975 FuncParser->setNextConstantID(C);
2932 return; 2976 return;
2933 } 2977 }
2934 } 2978 }
2935 std::string Buffer; 2979 std::string Buffer;
2936 raw_string_ostream StrBuf(Buffer); 2980 raw_string_ostream StrBuf(Buffer);
2937 StrBuf << "constant block integer record for non-integer type " 2981 StrBuf << "constant block integer record for non-integer type "
2938 << NextConstantType; 2982 << NextConstantType;
2939 Error(StrBuf.str()); 2983 Error(StrBuf.str());
2940 return; 2984 return;
2941 } 2985 }
2942 case naclbitc::CST_CODE_FLOAT: { 2986 case naclbitc::CST_CODE_FLOAT: {
2943 // FLOAT: [fpval] 2987 // FLOAT: [fpval]
2944 if (!isValidRecordSize(1, "float")) 2988 if (!isValidRecordSize(1, "float"))
2945 return; 2989 return;
2946 if (!isValidNextConstantType()) 2990 if (!isValidNextConstantType())
2947 return; 2991 return;
2948 if (isIRGenerationDisabled()) { 2992 if (isIRGenerationDisabled()) {
2949 FuncParser->setNextConstantID(nullptr); 2993 FuncParser->setNextConstantID(nullptr);
2950 return; 2994 return;
2951 } 2995 }
2952 switch (NextConstantType) { 2996 switch (NextConstantType) {
2953 case Ice::IceType_f32: { 2997 case Ice::IceType_f32: {
2954 const Ice::APInt IntValue(32, static_cast<uint32_t>(Values[0])); 2998 const BitcodeInt Value(32, static_cast<uint32_t>(Values[0]));
2955 float FpValue = Ice::convertAPIntToFp<int32_t, float>(IntValue); 2999 float FpValue = Value.convertToFp<int32_t, float>();
2956 FuncParser->setNextConstantID(getContext()->getConstantFloat(FpValue)); 3000 FuncParser->setNextConstantID(getContext()->getConstantFloat(FpValue));
2957 return; 3001 return;
2958 } 3002 }
2959 case Ice::IceType_f64: { 3003 case Ice::IceType_f64: {
2960 const Ice::APInt IntValue(64, Values[0]); 3004 const BitcodeInt Value(64, Values[0]);
2961 double FpValue = Ice::convertAPIntToFp<uint64_t, double>(IntValue); 3005 double FpValue = Value.convertToFp<uint64_t, double>();
2962 FuncParser->setNextConstantID(getContext()->getConstantDouble(FpValue)); 3006 FuncParser->setNextConstantID(getContext()->getConstantDouble(FpValue));
2963 return; 3007 return;
2964 } 3008 }
2965 default: { 3009 default: {
2966 std::string Buffer; 3010 std::string Buffer;
2967 raw_string_ostream StrBuf(Buffer); 3011 raw_string_ostream StrBuf(Buffer);
2968 StrBuf << "constant block float record for non-floating type " 3012 StrBuf << "constant block float record for non-floating type "
2969 << NextConstantType; 3013 << NextConstantType;
2970 Error(StrBuf.str()); 3014 Error(StrBuf.str());
2971 return; 3015 return;
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
3297 raw_string_ostream StrBuf(Buffer); 3341 raw_string_ostream StrBuf(Buffer);
3298 StrBuf << IRFilename << ": Does not contain a module!"; 3342 StrBuf << IRFilename << ": Does not contain a module!";
3299 llvm::report_fatal_error(StrBuf.str()); 3343 llvm::report_fatal_error(StrBuf.str());
3300 } 3344 }
3301 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { 3345 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) {
3302 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); 3346 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes");
3303 } 3347 }
3304 } 3348 }
3305 3349
3306 } // end of namespace Ice 3350 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceAPInt.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698