@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
| 508 | /// |
| 509 | /// @return result code |
| 510 | spv_result_t spvTextEncodeOpcode(const spvtools::AssemblyGrammar& grammar, |
| 511 | spvtools::AssemblyContext* context, |
| 512 | spv_instruction_t* pInst) { |
| 513 | // Check for !<integer> first. |
| 514 | if ('!' == context->peek()) { |
| 515 | return encodeInstructionStartingWithImmediate(grammar, context, pInst); |
| 516 | } |
| 517 | |
| 518 | std::string firstWord; |
| 519 | spv_position_t nextPosition = {}; |
| 520 | spv_result_t error = context->getWord(&firstWord, &nextPosition); |
| 521 | if (error) return context->diagnostic() << "Internal Error"; |
| 522 | |
| 523 | std::string opcodeName; |
| 524 | std::string result_id; |
| 525 | spv_position_t result_id_position = {}; |
| 526 | if (context->startsWithOp()) { |
| 527 | opcodeName = firstWord; |
| 528 | } else { |
| 529 | result_id = firstWord; |
| 530 | if ('%' != result_id.front()) { |
| 531 | return context->diagnostic() |
| 532 | << "Expected <opcode> or <result-id> at the beginning " |
| 533 | "of an instruction, found '" |
| 534 | << result_id << "'."; |
| 535 | } |
| 536 | result_id_position = context->position(); |
| 537 | |
| 538 | // The '=' sign. |
| 539 | context->setPosition(nextPosition); |
| 540 | if (context->advance()) |
| 541 | return context->diagnostic() << "Expected '=', found end of stream."; |
| 542 | std::string equal_sign; |
| 543 | error = context->getWord(&equal_sign, &nextPosition); |
| 544 | if ("=" != equal_sign) |
| 545 | return context->diagnostic() << "'=' expected after result id."; |
| 546 | |
| 547 | // The <opcode> after the '=' sign. |
| 548 | context->setPosition(nextPosition); |
| 549 | if (context->advance()) |
| 550 | return context->diagnostic() << "Expected opcode, found end of stream."; |
| 551 | error = context->getWord(&opcodeName, &nextPosition); |
| 552 | if (error) return context->diagnostic(error) << "Internal Error"; |
| 553 | if (!context->startsWithOp()) { |
| 554 | return context->diagnostic() |
| 555 | << "Invalid Opcode prefix '" << opcodeName << "'."; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // NOTE: The table contains Opcode names without the "Op" prefix. |
| 560 | const char* pInstName = opcodeName.data() + 2; |
| 561 | |
| 562 | spv_opcode_desc opcodeEntry; |
| 563 | error = grammar.lookupOpcode(pInstName, &opcodeEntry); |
| 564 | if (error) { |
| 565 | return context->diagnostic(error) |
| 566 | << "Invalid Opcode name '" << opcodeName << "'"; |
| 567 | } |
no test coverage detected