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 |
(...skipping 18 matching lines...) Expand all Loading... | |
29 #pragma clang diagnostic ignored "-Wunused-parameter" | 29 #pragma clang diagnostic ignored "-Wunused-parameter" |
30 #include "llvm/ADT/SmallString.h" | 30 #include "llvm/ADT/SmallString.h" |
31 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" | 31 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" |
32 #include "llvm/Bitcode/NaCl/NaClBitcodeDefs.h" | 32 #include "llvm/Bitcode/NaCl/NaClBitcodeDefs.h" |
33 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" | 33 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" |
34 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | 34 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
35 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 35 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
36 #include "llvm/Support/Format.h" | 36 #include "llvm/Support/Format.h" |
37 #include "llvm/Support/MemoryBuffer.h" | 37 #include "llvm/Support/MemoryBuffer.h" |
38 #include "llvm/Support/raw_ostream.h" | 38 #include "llvm/Support/raw_ostream.h" |
39 #include <set> | |
Jim Stichnoth
2015/09/15 21:05:29
Can you use unordered_set instead?
Karl
2015/09/16 17:54:25
After figuring out how to define an appropriate ha
| |
39 #pragma clang diagnostic pop | 40 #pragma clang diagnostic pop |
40 | 41 |
41 namespace { | 42 namespace { |
42 using namespace llvm; | 43 using namespace llvm; |
43 | 44 |
44 // Models elements in the list of types defined in the types block. | 45 // Models elements in the list of types defined in the types block. |
45 // These elements can be undefined, a (simple) type, or a function type | 46 // These elements can be undefined, a (simple) type, or a function type |
46 // signature. Note that an extended type is undefined on construction. | 47 // signature. Note that an extended type is undefined on construction. |
47 // Use methods setAsSimpleType and setAsFuncSigType to define | 48 // Use methods setAsSimpleType and setAsFuncSigType to define |
48 // the extended type. | 49 // the extended type. |
(...skipping 1104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1153 ValuesymtabParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 1154 ValuesymtabParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
1154 : BlockParserBaseClass(BlockID, EnclosingParser) {} | 1155 : BlockParserBaseClass(BlockID, EnclosingParser) {} |
1155 | 1156 |
1156 ~ValuesymtabParser() override = default; | 1157 ~ValuesymtabParser() override = default; |
1157 | 1158 |
1158 const char *getBlockName() const override { return "valuesymtab"; } | 1159 const char *getBlockName() const override { return "valuesymtab"; } |
1159 | 1160 |
1160 protected: | 1161 protected: |
1161 using StringType = SmallString<128>; | 1162 using StringType = SmallString<128>; |
1162 | 1163 |
1164 // Returns the name to identify the kind of symbol table this is | |
1165 // in error messages. | |
1166 virtual const char *getTableKind() const = 0; | |
1167 | |
1163 // Associates Name with the value defined by the given Index. | 1168 // Associates Name with the value defined by the given Index. |
1164 virtual void setValueName(NaClBcIndexSize_t Index, StringType &Name) = 0; | 1169 virtual void setValueName(NaClBcIndexSize_t Index, StringType &Name) = 0; |
1165 | 1170 |
1166 // Associates Name with the value defined by the given Index; | 1171 // Associates Name with the value defined by the given Index; |
1167 virtual void setBbName(NaClBcIndexSize_t Index, StringType &Name) = 0; | 1172 virtual void setBbName(NaClBcIndexSize_t Index, StringType &Name) = 0; |
1168 | 1173 |
1174 // Reports that the assignment of Name to the value associated with | |
1175 // index is not possible, for the given Context. | |
1176 void reportUnableToAssign(const char *Context, NaClBcIndexSize_t Index, | |
1177 StringType &Name); | |
1178 | |
1169 private: | 1179 private: |
1180 typedef std::set<StringType> NamesSetType; | |
Jim Stichnoth
2015/09/15 21:05:29
using NamesSetType = std::set<StringType>;
(we're
Karl
2015/09/16 17:54:25
Done.
| |
1181 NamesSetType ValueNames; | |
1182 NamesSetType BlockNames; | |
1183 | |
1170 void ProcessRecord() override; | 1184 void ProcessRecord() override; |
1171 | 1185 |
1172 void convertToString(StringType &ConvertedName) { | 1186 // Extracts out ConvertedName. Returns true if unique wrt to Names. |
1187 bool convertToString(NamesSetType &Names, StringType &ConvertedName) { | |
1173 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 1188 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
1174 for (size_t i = 1, e = Values.size(); i != e; ++i) { | 1189 for (size_t i = 1, e = Values.size(); i != e; ++i) { |
1175 ConvertedName += static_cast<char>(Values[i]); | 1190 ConvertedName += static_cast<char>(Values[i]); |
1176 } | 1191 } |
1192 auto pair = Names.insert(ConvertedName); | |
Jim Stichnoth
2015/09/15 21:05:29
capitalize Pair
Karl
2015/09/16 17:54:25
Done.
| |
1193 return pair.second; | |
1177 } | 1194 } |
1195 | |
1196 void ReportDuplicateName(const char *NameCat, StringType &Name); | |
1178 }; | 1197 }; |
1179 | 1198 |
1199 void ValuesymtabParser::reportUnableToAssign(const char *Context, | |
1200 NaClBcIndexSize_t Index, | |
1201 StringType &Name) { | |
1202 std::string Buffer; | |
1203 raw_string_ostream StrBuf(Buffer); | |
1204 StrBuf << getTableKind() << " " << getBlockName() << ": " << Context | |
1205 << " name '" << Name << "' can't be associated with index " << Index; | |
1206 Error(StrBuf.str()); | |
1207 } | |
1208 | |
1209 void ValuesymtabParser::ReportDuplicateName(const char *NameCat, | |
1210 StringType &Name) { | |
1211 std::string Buffer; | |
1212 raw_string_ostream StrBuf(Buffer); | |
1213 StrBuf << getTableKind() << " " << getBlockName() << " defines duplicate " | |
1214 << NameCat << " name: '" << Name << "'"; | |
1215 Error(StrBuf.str()); | |
1216 } | |
1217 | |
1180 void ValuesymtabParser::ProcessRecord() { | 1218 void ValuesymtabParser::ProcessRecord() { |
1181 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 1219 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
1182 StringType ConvertedName; | 1220 StringType ConvertedName; |
1183 switch (Record.GetCode()) { | 1221 switch (Record.GetCode()) { |
1184 case naclbitc::VST_CODE_ENTRY: { | 1222 case naclbitc::VST_CODE_ENTRY: { |
1185 // VST_ENTRY: [ValueId, namechar x N] | 1223 // VST_ENTRY: [ValueId, namechar x N] |
1186 if (!isValidRecordSizeAtLeast(2, "value entry")) | 1224 if (!isValidRecordSizeAtLeast(2, "value entry")) |
1187 return; | 1225 return; |
1188 convertToString(ConvertedName); | 1226 if (convertToString(ValueNames, ConvertedName)) |
1189 setValueName(Values[0], ConvertedName); | 1227 setValueName(Values[0], ConvertedName); |
1228 else | |
1229 ReportDuplicateName("value", ConvertedName); | |
1190 return; | 1230 return; |
1191 } | 1231 } |
1192 case naclbitc::VST_CODE_BBENTRY: { | 1232 case naclbitc::VST_CODE_BBENTRY: { |
1193 // VST_BBENTRY: [BbId, namechar x N] | 1233 // VST_BBENTRY: [BbId, namechar x N] |
1194 if (!isValidRecordSizeAtLeast(2, "basic block entry")) | 1234 if (!isValidRecordSizeAtLeast(2, "basic block entry")) |
1195 return; | 1235 return; |
1196 convertToString(ConvertedName); | 1236 if (convertToString(BlockNames, ConvertedName)) |
1197 setBbName(Values[0], ConvertedName); | 1237 setBbName(Values[0], ConvertedName); |
1238 else | |
1239 ReportDuplicateName("block", ConvertedName); | |
1198 return; | 1240 return; |
1199 } | 1241 } |
1200 default: | 1242 default: |
1201 break; | 1243 break; |
1202 } | 1244 } |
1203 // If reached, don't know how to handle record. | 1245 // If reached, don't know how to handle record. |
1204 BlockParserBaseClass::ProcessRecord(); | 1246 BlockParserBaseClass::ProcessRecord(); |
1205 return; | 1247 return; |
1206 } | 1248 } |
1207 | 1249 |
(...skipping 1669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2877 Timer(Ice::TimerStack::TT_parseFunctionValuesymtabs, | 2919 Timer(Ice::TimerStack::TT_parseFunctionValuesymtabs, |
2878 getTranslator().getContext()) {} | 2920 getTranslator().getContext()) {} |
2879 | 2921 |
2880 private: | 2922 private: |
2881 Ice::TimerMarker Timer; | 2923 Ice::TimerMarker Timer; |
2882 // Returns the enclosing function parser. | 2924 // Returns the enclosing function parser. |
2883 FunctionParser *getFunctionParser() const { | 2925 FunctionParser *getFunctionParser() const { |
2884 return reinterpret_cast<FunctionParser *>(GetEnclosingParser()); | 2926 return reinterpret_cast<FunctionParser *>(GetEnclosingParser()); |
2885 } | 2927 } |
2886 | 2928 |
2887 void setValueName(NaClBcIndexSize_t Index, StringType &Name) override; | 2929 const char *getTableKind() const final { return "Function"; } |
2888 void setBbName(NaClBcIndexSize_t Index, StringType &Name) override; | |
2889 | 2930 |
2890 // Reports that the assignment of Name to the value associated with | 2931 void setValueName(NaClBcIndexSize_t Index, StringType &Name) final; |
2891 // index is not possible, for the given Context. | 2932 void setBbName(NaClBcIndexSize_t Index, StringType &Name) final; |
2892 void reportUnableToAssign(const char *Context, NaClBcIndexSize_t Index, | |
2893 StringType &Name) { | |
2894 std::string Buffer; | |
2895 raw_string_ostream StrBuf(Buffer); | |
2896 StrBuf << "Function-local " << Context << " name '" << Name | |
2897 << "' can't be associated with index " << Index; | |
2898 Error(StrBuf.str()); | |
2899 } | |
2900 }; | 2933 }; |
2901 | 2934 |
2902 void FunctionValuesymtabParser::setValueName(NaClBcIndexSize_t Index, | 2935 void FunctionValuesymtabParser::setValueName(NaClBcIndexSize_t Index, |
2903 StringType &Name) { | 2936 StringType &Name) { |
2904 // Note: We check when Index is too small, so that we can error recover | 2937 // Note: We check when Index is too small, so that we can error recover |
2905 // (FP->getOperand will create fatal error). | 2938 // (FP->getOperand will create fatal error). |
2906 if (Index < getFunctionParser()->getNumGlobalIDs()) { | 2939 if (Index < getFunctionParser()->getNumGlobalIDs()) { |
2907 reportUnableToAssign("instruction", Index, Name); | 2940 reportUnableToAssign("Global value", Index, Name); |
2908 return; | 2941 return; |
2909 } | 2942 } |
2910 if (isIRGenerationDisabled()) | 2943 if (isIRGenerationDisabled()) |
2911 return; | 2944 return; |
2912 Ice::Operand *Op = getFunctionParser()->getOperand(Index); | 2945 Ice::Operand *Op = getFunctionParser()->getOperand(Index); |
2913 if (Ice::Variable *V = dyn_cast<Ice::Variable>(Op)) { | 2946 if (Ice::Variable *V = dyn_cast<Ice::Variable>(Op)) { |
2914 if (Ice::BuildDefs::dump()) { | 2947 if (Ice::BuildDefs::dump()) { |
2915 std::string Nm(Name.data(), Name.size()); | 2948 std::string Nm(Name.data(), Name.size()); |
2916 V->setName(getFunctionParser()->getFunc(), Nm); | 2949 V->setName(getFunctionParser()->getFunc(), Nm); |
2917 } | 2950 } |
2918 } else { | 2951 } else { |
2919 reportUnableToAssign("variable", Index, Name); | 2952 reportUnableToAssign("Local value", Index, Name); |
2920 } | 2953 } |
2921 } | 2954 } |
2922 | 2955 |
2923 void FunctionValuesymtabParser::setBbName(NaClBcIndexSize_t Index, | 2956 void FunctionValuesymtabParser::setBbName(NaClBcIndexSize_t Index, |
2924 StringType &Name) { | 2957 StringType &Name) { |
2925 if (!Ice::BuildDefs::dump()) | 2958 if (!Ice::BuildDefs::dump()) |
2926 return; | 2959 return; |
2927 if (isIRGenerationDisabled()) | 2960 if (isIRGenerationDisabled()) |
2928 return; | 2961 return; |
2929 if (Index >= getFunctionParser()->getFunc()->getNumNodes()) { | 2962 if (Index >= getFunctionParser()->getFunc()->getNumNodes()) { |
2930 reportUnableToAssign("block", Index, Name); | 2963 reportUnableToAssign("Basic block", Index, Name); |
2931 return; | 2964 return; |
2932 } | 2965 } |
2933 std::string Nm(Name.data(), Name.size()); | 2966 std::string Nm(Name.data(), Name.size()); |
2934 if (Ice::BuildDefs::dump()) | 2967 if (Ice::BuildDefs::dump()) |
2935 getFunctionParser()->getFunc()->getNodes()[Index]->setName(Nm); | 2968 getFunctionParser()->getFunc()->getNodes()[Index]->setName(Nm); |
2936 } | 2969 } |
2937 | 2970 |
2938 bool FunctionParser::ParseBlock(unsigned BlockID) { | 2971 bool FunctionParser::ParseBlock(unsigned BlockID) { |
2939 switch (BlockID) { | 2972 switch (BlockID) { |
2940 case naclbitc::CONSTANTS_BLOCK_ID: { | 2973 case naclbitc::CONSTANTS_BLOCK_ID: { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3003 public: | 3036 public: |
3004 ModuleValuesymtabParser(unsigned BlockID, ModuleParser *MP) | 3037 ModuleValuesymtabParser(unsigned BlockID, ModuleParser *MP) |
3005 : ValuesymtabParser(BlockID, MP), | 3038 : ValuesymtabParser(BlockID, MP), |
3006 Timer(Ice::TimerStack::TT_parseModuleValuesymtabs, | 3039 Timer(Ice::TimerStack::TT_parseModuleValuesymtabs, |
3007 getTranslator().getContext()) {} | 3040 getTranslator().getContext()) {} |
3008 | 3041 |
3009 ~ModuleValuesymtabParser() override = default; | 3042 ~ModuleValuesymtabParser() override = default; |
3010 | 3043 |
3011 private: | 3044 private: |
3012 Ice::TimerMarker Timer; | 3045 Ice::TimerMarker Timer; |
3013 void setValueName(NaClBcIndexSize_t Index, StringType &Name) override; | 3046 const char *getTableKind() const final { return "Module"; } |
3014 void setBbName(NaClBcIndexSize_t Index, StringType &Name) override; | 3047 void setValueName(NaClBcIndexSize_t Index, StringType &Name) final; |
3048 void setBbName(NaClBcIndexSize_t Index, StringType &Name) final; | |
3015 }; | 3049 }; |
3016 | 3050 |
3017 void ModuleValuesymtabParser::setValueName(NaClBcIndexSize_t Index, | 3051 void ModuleValuesymtabParser::setValueName(NaClBcIndexSize_t Index, |
3018 StringType &Name) { | 3052 StringType &Name) { |
3019 Context->getGlobalDeclarationByID(Index) | 3053 Context->getGlobalDeclarationByID(Index) |
3020 ->setName(StringRef(Name.data(), Name.size())); | 3054 ->setName(StringRef(Name.data(), Name.size())); |
3021 } | 3055 } |
3022 | 3056 |
3023 void ModuleValuesymtabParser::setBbName(NaClBcIndexSize_t Index, | 3057 void ModuleValuesymtabParser::setBbName(NaClBcIndexSize_t Index, |
3024 StringType &Name) { | 3058 StringType &Name) { |
3025 std::string Buffer; | 3059 reportUnableToAssign("Basic block", Index, Name); |
3026 raw_string_ostream StrBuf(Buffer); | |
3027 StrBuf << "Can't define basic block name at global level: '" << Name | |
3028 << "' -> " << Index; | |
3029 Error(StrBuf.str()); | |
3030 } | 3060 } |
3031 | 3061 |
3032 bool ModuleParser::ParseBlock(unsigned BlockID) { | 3062 bool ModuleParser::ParseBlock(unsigned BlockID) { |
3033 switch (BlockID) { | 3063 switch (BlockID) { |
3034 case naclbitc::BLOCKINFO_BLOCK_ID: | 3064 case naclbitc::BLOCKINFO_BLOCK_ID: |
3035 return NaClBitcodeParser::ParseBlock(BlockID); | 3065 return NaClBitcodeParser::ParseBlock(BlockID); |
3036 case naclbitc::TYPE_BLOCK_ID_NEW: { | 3066 case naclbitc::TYPE_BLOCK_ID_NEW: { |
3037 TypesParser Parser(BlockID, this); | 3067 TypesParser Parser(BlockID, this); |
3038 return Parser.ParseThisBlock(); | 3068 return Parser.ParseThisBlock(); |
3039 } | 3069 } |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3171 } | 3201 } |
3172 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { | 3202 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { |
3173 ErrStream | 3203 ErrStream |
3174 << IRFilename | 3204 << IRFilename |
3175 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; | 3205 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; |
3176 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); | 3206 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); |
3177 } | 3207 } |
3178 } | 3208 } |
3179 | 3209 |
3180 } // end of namespace Ice | 3210 } // end of namespace Ice |
OLD | NEW |