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 891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 std::string Buffer; | 902 std::string Buffer; |
903 raw_string_ostream StrBuf(Buffer); | 903 raw_string_ostream StrBuf(Buffer); |
904 StrBuf << "Value index " << Index << " out of range. Must be less than " | 904 StrBuf << "Value index " << Index << " out of range. Must be less than " |
905 << (LocalOperands.size() + CachedNumGlobalValueIDs); | 905 << (LocalOperands.size() + CachedNumGlobalValueIDs); |
906 Error(StrBuf.str()); | 906 Error(StrBuf.str()); |
907 report_fatal_error("Unable to continue"); | 907 report_fatal_error("Unable to continue"); |
908 } | 908 } |
909 return LocalOperands[LocalIndex]; | 909 return LocalOperands[LocalIndex]; |
910 } | 910 } |
911 | 911 |
| 912 // Returns the relative operand (wrt to next instruction) referenced by |
| 913 // the given value index. |
| 914 Ice::Operand *getRelativeOperand(uint32_t Index) { |
| 915 return getOperand(convertRelativeToAbsIndex(Index)); |
| 916 } |
| 917 |
912 // Generates type error message for binary operator Op | 918 // Generates type error message for binary operator Op |
913 // operating on Type OpTy. | 919 // operating on Type OpTy. |
914 void ReportInvalidBinaryOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy); | 920 void ReportInvalidBinaryOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy); |
915 | 921 |
916 // Validates if integer logical Op, for type OpTy, is valid. | 922 // Validates if integer logical Op, for type OpTy, is valid. |
917 // Returns true if valid. Otherwise generates error message and | 923 // Returns true if valid. Otherwise generates error message and |
918 // returns false. | 924 // returns false. |
919 bool isValidIntegerLogicalOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { | 925 bool isValidIntegerLogicalOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
920 if (Ice::isIntegerType(OpTy)) | 926 if (Ice::isIntegerType(OpTy)) |
921 return true; | 927 return true; |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1136 // Install the basic blocks, skipping bb0 which was created in the | 1142 // Install the basic blocks, skipping bb0 which was created in the |
1137 // constructor. | 1143 // constructor. |
1138 for (size_t i = 1; i < NumBbs; ++i) | 1144 for (size_t i = 1; i < NumBbs; ++i) |
1139 InstallNextBasicBlock(); | 1145 InstallNextBasicBlock(); |
1140 break; | 1146 break; |
1141 } | 1147 } |
1142 case naclbitc::FUNC_CODE_INST_BINOP: { | 1148 case naclbitc::FUNC_CODE_INST_BINOP: { |
1143 // BINOP: [opval, opval, opcode] | 1149 // BINOP: [opval, opval, opcode] |
1144 if (!isValidRecordSize(3, "function block binop")) | 1150 if (!isValidRecordSize(3, "function block binop")) |
1145 return; | 1151 return; |
1146 Ice::Operand *Op1 = getOperand(convertRelativeToAbsIndex(Values[0])); | 1152 Ice::Operand *Op1 = getRelativeOperand(Values[0]); |
1147 Ice::Operand *Op2 = getOperand(convertRelativeToAbsIndex(Values[1])); | 1153 Ice::Operand *Op2 = getRelativeOperand(Values[1]); |
1148 Ice::Type Type1 = Op1->getType(); | 1154 Ice::Type Type1 = Op1->getType(); |
1149 Ice::Type Type2 = Op2->getType(); | 1155 Ice::Type Type2 = Op2->getType(); |
1150 if (Type1 != Type2) { | 1156 if (Type1 != Type2) { |
1151 std::string Buffer; | 1157 std::string Buffer; |
1152 raw_string_ostream StrBuf(Buffer); | 1158 raw_string_ostream StrBuf(Buffer); |
1153 StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2; | 1159 StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2; |
1154 Error(StrBuf.str()); | 1160 Error(StrBuf.str()); |
1155 // TODO(kschimpf) Remove error recovery once implementation complete. | 1161 // TODO(kschimpf) Remove error recovery once implementation complete. |
1156 Op2 = Op1; | 1162 Op2 = Op1; |
1157 } | 1163 } |
1158 | 1164 |
1159 Ice::InstArithmetic::OpKind Opcode; | 1165 Ice::InstArithmetic::OpKind Opcode; |
1160 if (!convertBinopOpcode(Values[2], Type1, Opcode)) | 1166 if (!convertBinopOpcode(Values[2], Type1, Opcode)) |
1161 return; | 1167 return; |
1162 Ice::Variable *Dest = NextInstVar(Type1); | 1168 Ice::Variable *Dest = NextInstVar(Type1); |
1163 Inst = Ice::InstArithmetic::create(Func, Opcode, Dest, Op1, Op2); | 1169 Inst = Ice::InstArithmetic::create(Func, Opcode, Dest, Op1, Op2); |
1164 break; | 1170 break; |
1165 } | 1171 } |
1166 case naclbitc::FUNC_CODE_INST_CAST: { | 1172 case naclbitc::FUNC_CODE_INST_CAST: { |
1167 // CAST: [opval, destty, castopc] | 1173 // CAST: [opval, destty, castopc] |
1168 if (!isValidRecordSize(3, "function block cast")) | 1174 if (!isValidRecordSize(3, "function block cast")) |
1169 return; | 1175 return; |
1170 Ice::Operand *Src = getOperand(convertRelativeToAbsIndex(Values[0])); | 1176 Ice::Operand *Src = getRelativeOperand(Values[0]); |
1171 Type *CastType = Context->getTypeByID(Values[1]); | 1177 Type *CastType = Context->getTypeByID(Values[1]); |
1172 Instruction::CastOps LLVMCastOp; | 1178 Instruction::CastOps LLVMCastOp; |
1173 Ice::InstCast::OpKind CastKind; | 1179 Ice::InstCast::OpKind CastKind; |
1174 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) || | 1180 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) || |
1175 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) { | 1181 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) { |
1176 std::string Buffer; | 1182 std::string Buffer; |
1177 raw_string_ostream StrBuf(Buffer); | 1183 raw_string_ostream StrBuf(Buffer); |
1178 StrBuf << "Cast opcode not understood: " << Values[2]; | 1184 StrBuf << "Cast opcode not understood: " << Values[2]; |
1179 Error(StrBuf.str()); | 1185 Error(StrBuf.str()); |
1180 return; | 1186 return; |
1181 } | 1187 } |
1182 Type *SrcType = Context->convertToLLVMType(Src->getType()); | 1188 Type *SrcType = Context->convertToLLVMType(Src->getType()); |
1183 if (!CastInst::castIsValid(LLVMCastOp, SrcType, CastType)) { | 1189 if (!CastInst::castIsValid(LLVMCastOp, SrcType, CastType)) { |
1184 std::string Buffer; | 1190 std::string Buffer; |
1185 raw_string_ostream StrBuf(Buffer); | 1191 raw_string_ostream StrBuf(Buffer); |
1186 StrBuf << "Illegal cast: " << Instruction::getOpcodeName(LLVMCastOp) | 1192 StrBuf << "Illegal cast: " << Instruction::getOpcodeName(LLVMCastOp) |
1187 << " " << *SrcType << " to " << *CastType; | 1193 << " " << *SrcType << " to " << *CastType; |
1188 Error(StrBuf.str()); | 1194 Error(StrBuf.str()); |
1189 return; | 1195 return; |
1190 } | 1196 } |
1191 Ice::Variable *Dest = NextInstVar(Context->convertToIceType(CastType)); | 1197 Ice::Variable *Dest = NextInstVar(Context->convertToIceType(CastType)); |
1192 Inst = Ice::InstCast::create(Func, CastKind, Dest, Src); | 1198 Inst = Ice::InstCast::create(Func, CastKind, Dest, Src); |
1193 break; | 1199 break; |
1194 } | 1200 } |
| 1201 case naclbitc::FUNC_CODE_INST_EXTRACTELT: { |
| 1202 // EXTRACTELT: [opval, opval] |
| 1203 Ice::Operand *Vec = getRelativeOperand(Values[0]); |
| 1204 Ice::Type VecType = Vec->getType(); |
| 1205 if (!Ice::isVectorType(VecType)) { |
| 1206 std::string Buffer; |
| 1207 raw_string_ostream StrBuf(Buffer); |
| 1208 StrBuf << "Extractelement not on vector. Found: " << Vec; |
| 1209 Error(StrBuf.str()); |
| 1210 } |
| 1211 Ice::Operand *Index = getRelativeOperand(Values[1]); |
| 1212 if (Index->getType() != Ice::IceType_i32) { |
| 1213 std::string Buffer; |
| 1214 raw_string_ostream StrBuf(Buffer); |
| 1215 StrBuf << "Extractelement index not i32. Found: " << Index; |
| 1216 Error(StrBuf.str()); |
| 1217 } |
| 1218 // TODO(kschimpf): Restrict index to a legal constant index (once |
| 1219 // constants can be defined). |
| 1220 Ice::Variable *Dest = NextInstVar(typeElementType(VecType)); |
| 1221 Inst = Ice::InstExtractElement::create(Func, Dest, Vec, Index); |
| 1222 break; |
| 1223 } |
| 1224 case naclbitc::FUNC_CODE_INST_INSERTELT: { |
| 1225 // INSERTELT: [opval, opval, opval] |
| 1226 Ice::Operand *Vec = getRelativeOperand(Values[0]); |
| 1227 Ice::Type VecType = Vec->getType(); |
| 1228 if (!Ice::isVectorType(VecType)) { |
| 1229 std::string Buffer; |
| 1230 raw_string_ostream StrBuf(Buffer); |
| 1231 StrBuf << "Insertelement not on vector. Found: " << Vec; |
| 1232 Error(StrBuf.str()); |
| 1233 } |
| 1234 Ice::Operand *Elt = getRelativeOperand(Values[1]); |
| 1235 Ice::Type EltType = Elt->getType(); |
| 1236 if (EltType != typeElementType(VecType)) { |
| 1237 std::string Buffer; |
| 1238 raw_string_ostream StrBuf(Buffer); |
| 1239 StrBuf << "Insertelement element not " << typeElementType(VecType) |
| 1240 << ". Found: " << Elt; |
| 1241 Error(StrBuf.str()); |
| 1242 } |
| 1243 Ice::Operand *Index = getRelativeOperand(Values[2]); |
| 1244 if (Index->getType() != Ice::IceType_i32) { |
| 1245 std::string Buffer; |
| 1246 raw_string_ostream StrBuf(Buffer); |
| 1247 StrBuf << "Insertelement index not i32. Found: " << Index; |
| 1248 Error(StrBuf.str()); |
| 1249 } |
| 1250 // TODO(kschimpf): Restrict index to a legal constant index (once |
| 1251 // constants can be defined). |
| 1252 Ice::Variable *Dest = NextInstVar(EltType); |
| 1253 Inst = Ice::InstInsertElement::create(Func, Dest, Vec, Elt, Index); |
| 1254 break; |
| 1255 } |
1195 case naclbitc::FUNC_CODE_INST_RET: { | 1256 case naclbitc::FUNC_CODE_INST_RET: { |
1196 // RET: [opval?] | 1257 // RET: [opval?] |
1197 InstIsTerminating = true; | 1258 InstIsTerminating = true; |
1198 if (!isValidRecordSizeInRange(0, 1, "function block ret")) | 1259 if (!isValidRecordSizeInRange(0, 1, "function block ret")) |
1199 return; | 1260 return; |
1200 if (Values.size() == 0) { | 1261 if (Values.size() == 0) { |
1201 Inst = Ice::InstRet::create(Func); | 1262 Inst = Ice::InstRet::create(Func); |
1202 } else { | 1263 } else { |
1203 Inst = Ice::InstRet::create( | 1264 Inst = Ice::InstRet::create(Func, getRelativeOperand(Values[0])); |
1204 Func, getOperand(convertRelativeToAbsIndex(Values[0]))); | |
1205 } | 1265 } |
1206 break; | 1266 break; |
1207 } | 1267 } |
1208 default: | 1268 default: |
1209 // Generate error message! | 1269 // Generate error message! |
1210 BlockParserBaseClass::ProcessRecord(); | 1270 BlockParserBaseClass::ProcessRecord(); |
1211 break; | 1271 break; |
1212 } | 1272 } |
1213 if (Inst) | 1273 if (Inst) |
1214 CurrentNode->appendInst(Inst); | 1274 CurrentNode->appendInst(Inst); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1372 if (TopLevelBlocks != 1) { | 1432 if (TopLevelBlocks != 1) { |
1373 errs() << IRFilename | 1433 errs() << IRFilename |
1374 << ": Contains more than one module. Found: " << TopLevelBlocks | 1434 << ": Contains more than one module. Found: " << TopLevelBlocks |
1375 << "\n"; | 1435 << "\n"; |
1376 ErrorStatus = true; | 1436 ErrorStatus = true; |
1377 } | 1437 } |
1378 return; | 1438 return; |
1379 } | 1439 } |
1380 | 1440 |
1381 } // end of namespace Ice | 1441 } // end of namespace Ice |
OLD | NEW |