@brief Translate single Opcode and operands to binary form @param[in] grammar the grammar to use for compilation @param[in, out] context the dynamic compilation info @param[in] text stream to translate @param[out] pInst returned binary Opcode @param[in,out] pPosition in the text stream @return result code
| 654 | /// |
| 655 | /// @return result code |
| 656 | spv_result_t spvTextEncodeOpcode(const spvtools::AssemblyGrammar& grammar, |
| 657 | spvtools::AssemblyContext* context, |
| 658 | spv_instruction_t* pInst) { |
| 659 | // Check for !<integer> first. |
| 660 | if ('!' == context->peek()) { |
| 661 | return encodeInstructionStartingWithImmediate(grammar, context, pInst); |
| 662 | } |
| 663 | |
| 664 | std::string firstWord; |
| 665 | spv_position_t nextPosition = {}; |
| 666 | spv_result_t error = context->getWord(&firstWord, &nextPosition); |
| 667 | if (error) return context->diagnostic() << "Internal Error"; |
| 668 | |
| 669 | std::string opcodeName; |
| 670 | std::string result_id; |
| 671 | spv_position_t result_id_position = {}; |
| 672 | if (context->startsWithOp()) { |
| 673 | opcodeName = firstWord; |
| 674 | } else { |
| 675 | result_id = firstWord; |
| 676 | if ('%' != result_id.front()) { |
| 677 | return context->diagnostic() |
| 678 | << "Expected <opcode> or <result-id> at the beginning " |
| 679 | "of an instruction, found '" |
| 680 | << result_id << "'."; |
| 681 | } |
| 682 | result_id_position = context->position(); |
| 683 | |
| 684 | // The '=' sign. |
| 685 | context->setPosition(nextPosition); |
| 686 | if (context->advance()) |
| 687 | return context->diagnostic() << "Expected '=', found end of stream."; |
| 688 | std::string equal_sign; |
| 689 | error = context->getWord(&equal_sign, &nextPosition); |
| 690 | if ("=" != equal_sign) |
| 691 | return context->diagnostic() << "'=' expected after result id but found '" |
| 692 | << equal_sign << "'."; |
| 693 | |
| 694 | // The <opcode> after the '=' sign. |
| 695 | context->setPosition(nextPosition); |
| 696 | if (context->advance()) |
| 697 | return context->diagnostic() << "Expected opcode, found end of stream."; |
| 698 | error = context->getWord(&opcodeName, &nextPosition); |
| 699 | if (error) return context->diagnostic(error) << "Internal Error"; |
| 700 | if (!context->startsWithOp()) { |
| 701 | return context->diagnostic() |
| 702 | << "Invalid Opcode prefix '" << opcodeName << "'."; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (opcodeName == "OpUnknown") { |
| 707 | if (!result_id.empty()) { |
| 708 | return context->diagnostic() |
| 709 | << "OpUnknown not allowed in assignment. Use an explicit result " |
| 710 | "id operand instead."; |
| 711 | } |
| 712 | context->setPosition(nextPosition); |
| 713 | return encodeInstructionStartingWithOpUnknown(grammar, context, pInst); |
no test coverage detected