// Copyright (c) 2015 The Khronos Group Inc. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and/or associated documentation files (the // "Materials"), to deal in the Materials without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Materials, and to // permit persons to whom the Materials are furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Materials. // // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT // https://www.khronos.org/registry/ // // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. #include "text.h" #include #include #include #include #include #include #include #include #include #include #include #include "binary.h" #include "diagnostic.h" #include "ext_inst.h" #include "instruction.h" #include #include "opcode.h" #include "operand.h" #include "text_handler.h" #include "util/bitwisecast.h" bool spvIsValidIDCharacter(const char value) { return value == '_' || 0 != ::isalnum(value); } // Returns true if the given string represents a valid ID name. bool spvIsValidID(const char* textValue) { const char* c = textValue; for (; *c != '\0'; ++c) { if (!spvIsValidIDCharacter(*c)) { return false; } } // If the string was empty, then the ID also is not valid. return c != textValue; } // Text API spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) { bool isSigned = false; int numPeriods = 0; bool isString = false; const size_t len = strlen(textValue); if (len == 0) return SPV_FAILED_MATCH; for (uint64_t index = 0; index < len; ++index) { switch (textValue[index]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': break; case '.': numPeriods++; break; case '-': if (index == 0) { isSigned = true; } else { isString = true; } break; default: isString = true; index = len; // break out of the loop too. break; } } pLiteral->type = spv_literal_type_t(99); if (isString || numPeriods > 1 || (isSigned && len == 1)) { if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') return SPV_FAILED_MATCH; bool escaping = false; size_t write_index = 0; for (const char* val = textValue + 1; val != textValue + len - 1; ++val) { if ((*val == '\\') && (!escaping)) { escaping = true; } else { // Have to save space for the null-terminator if (write_index >= sizeof(pLiteral->value.str) - 1) return SPV_ERROR_OUT_OF_MEMORY; pLiteral->value.str[write_index] = *val; escaping = false; ++write_index; } } pLiteral->type = SPV_LITERAL_TYPE_STRING; pLiteral->value.str[write_index] = '\0'; } else if (numPeriods == 1) { double d = std::strtod(textValue, nullptr); float f = (float)d; if (d == (double)f) { pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32; pLiteral->value.f = f; } else { pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64; pLiteral->value.d = d; } } else if (isSigned) { int64_t i64 = strtoll(textValue, nullptr, 10); int32_t i32 = (int32_t)i64; if (i64 == (int64_t)i32) { pLiteral->type = SPV_LITERAL_TYPE_INT_32; pLiteral->value.i32 = i32; } else { pLiteral->type = SPV_LITERAL_TYPE_INT_64; pLiteral->value.i64 = i64; } } else { uint64_t u64 = strtoull(textValue, nullptr, 10); uint32_t u32 = (uint32_t)u64; if (u64 == (uint64_t)u32) { pLiteral->type = SPV_LITERAL_TYPE_UINT_32; pLiteral->value.u32 = u32; } else { pLiteral->type = SPV_LITERAL_TYPE_UINT_64; pLiteral->value.u64 = u64; } } return SPV_SUCCESS; } namespace { /// Parses an immediate integer from text, guarding against overflow. If /// successful, adds the parsed value to pInst, advances the context past it, /// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics, /// and returns SPV_ERROR_INVALID_TEXT. spv_result_t encodeImmediate(libspirv::AssemblyContext* context, const char* text, spv_instruction_t* pInst) { assert(*text == '!'); uint32_t parse_result; if (auto error = context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result, "Invalid immediate integer: !")) return error; context->binaryEncodeU32(parse_result, pInst); context->seekForward(strlen(text)); return SPV_SUCCESS; } } // anonymous namespace /// @brief Translate an Opcode operand to binary form /// /// @param[in] grammar the grammar to use for compilation /// @param[in, out] context the dynamic compilation info /// @param[in] type of the operand /// @param[in] textValue word of text to be parsed /// @param[out] pInst return binary Opcode /// @param[in,out] pExpectedOperands the operand types expected /// /// @return result code spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar, libspirv::AssemblyContext* context, const spv_operand_type_t type, const char* textValue, spv_instruction_t* pInst, spv_operand_pattern_t* pExpectedOperands) { // NOTE: Handle immediate int in the stream if ('!' == textValue[0]) { if (auto error = encodeImmediate(context, textValue, pInst)) { return error; } *pExpectedOperands = spvAlternatePatternFollowingImmediate(*pExpectedOperands); return SPV_SUCCESS; } // Optional literal operands can fail to parse. In that case use // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following // for those situations. spv_result_t error_code_for_literals = spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT; switch (type) { case SPV_OPERAND_TYPE_EXECUTION_SCOPE: case SPV_OPERAND_TYPE_ID: case SPV_OPERAND_TYPE_TYPE_ID: case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: case SPV_OPERAND_TYPE_OPTIONAL_ID: case SPV_OPERAND_TYPE_MEMORY_SEMANTICS: case SPV_OPERAND_TYPE_RESULT_ID: { if ('%' == textValue[0]) { textValue++; } else { return context->diagnostic() << "Expected id to start with %."; } if (!spvIsValidID(textValue)) { return context->diagnostic() << "Invalid ID " << textValue; } const uint32_t id = context->spvNamedIdAssignOrGet(textValue); if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id; spvInstructionAddWord(pInst, id); } break; case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: { // The assembler accepts the symbolic name for an extended instruction, // and emits its corresponding number. spv_ext_inst_desc extInst; if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) { return context->diagnostic() << "Invalid extended instruction name '" << textValue << "'."; } spvInstructionAddWord(pInst, extInst->ext_inst); // Prepare to parse the operands for the extended instructions. spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands); return SPV_SUCCESS; } break; case SPV_OPERAND_TYPE_LITERAL_INTEGER: case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER: case SPV_OPERAND_TYPE_LITERAL_INTEGER_IN_OPTIONAL_TUPLE: case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: { libspirv::IdType expected_type = libspirv::kUnknownType; if (type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER) { // From now, it's safe to assume that the current operand is an unsigned // integer, apart from the special cases handled below. expected_type = {32, false, libspirv::IdTypeClass::kScalarIntegerType}; // The encoding for OpConstant, OpSpecConstant and OpSwitch all // depend on either their own result-id or the result-id of // one of their parameters. if (OpConstant == pInst->opcode || OpSpecConstant == pInst->opcode) { // Special cases for encoding possibly non-32-bit literals here. expected_type = context->getTypeOfTypeGeneratingValue(pInst->resultTypeId); if (!libspirv::isScalarFloating(expected_type) && !libspirv::isScalarIntegral(expected_type)) { spv_opcode_desc d; const char* opcode_name = "opcode"; if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) { opcode_name = d->name; } return context->diagnostic() << "Type for " << opcode_name << " must be a scalar floating point or integer type"; } } else if (pInst->opcode == OpSwitch) { // We need to know the type of the selector. expected_type = context->getTypeOfValueInstruction(pInst->words[1]); if (!libspirv::isScalarIntegral(expected_type)) { context->diagnostic() << "The selector operand for OpSwitch must be the result" " of an instruction that generates an integer scalar"; return SPV_ERROR_INVALID_TEXT; } } } if (auto error = context->binaryEncodeNumericLiteral( textValue, error_code_for_literals, expected_type, pInst)) { return error; } } break; case SPV_OPERAND_TYPE_LITERAL_STRING: case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { spv_literal_t literal = {}; spv_result_t error = spvTextToLiteral(textValue, &literal); if (error != SPV_SUCCESS) { if (error == SPV_ERROR_OUT_OF_MEMORY) return error; return context->diagnostic(error_code_for_literals) << "Invalid literal string '" << textValue << "'."; } if (literal.type != SPV_LITERAL_TYPE_STRING) { return context->diagnostic() << "Expected literal string, found literal number '" << textValue << "'."; } // NOTE: Special case for extended instruction library import if (OpExtInstImport == pInst->opcode) { pInst->extInstType = spvExtInstImportTypeGet(literal.value.str); } if (context->binaryEncodeString(literal.value.str, pInst)) return SPV_ERROR_INVALID_TEXT; } break; case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: case SPV_OPERAND_TYPE_FUNCTION_CONTROL: case SPV_OPERAND_TYPE_LOOP_CONTROL: case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: case SPV_OPERAND_TYPE_SELECTION_CONTROL: { uint32_t value; if (grammar.parseMaskOperand(type, textValue, &value)) { return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '" << textValue << "'."; } if (auto error = context->binaryEncodeU32(value, pInst)) return error; // Prepare to parse the operands for this logical operand. grammar.prependOperandTypesForMask(type, value, pExpectedOperands); } break; case SPV_OPERAND_TYPE_OPTIONAL_CIV: { auto error = spvTextEncodeOperand( grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue, pInst, pExpectedOperands); if (error == SPV_FAILED_MATCH) { // It's not a literal number -- is it a literal string? error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING, textValue, pInst, pExpectedOperands); } if (error == SPV_FAILED_MATCH) { // It's not a literal -- is it an ID? error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID, textValue, pInst, pExpectedOperands); } if (error) { return context->diagnostic(error) << "Invalid word following !: " << textValue; } if (pExpectedOperands->empty()) { pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV); } } break; default: { // NOTE: All non literal operands are handled here using the operand // table. spv_operand_desc entry; if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) { return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '" << textValue << "'."; } if (context->binaryEncodeU32(entry->value, pInst)) { return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '" << textValue << "'."; } // Prepare to parse the operands for this logical operand. spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); } break; } return SPV_SUCCESS; } namespace { /// Encodes an instruction started by ! at the given position in text. /// /// Puts the encoded words into *pInst. If successful, moves position past the /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and /// leaves position pointing to the error in text. spv_result_t encodeInstructionStartingWithImmediate( const libspirv::AssemblyGrammar& grammar, libspirv::AssemblyContext* context, spv_instruction_t* pInst) { std::string firstWord; spv_position_t nextPosition = {}; auto error = context->getWord(firstWord, &nextPosition); if (error) return context->diagnostic(error) << "Internal Error"; if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) { return error; } while (context->advance() != SPV_END_OF_STREAM) { // A beginning of a new instruction means we're done. if (context->isStartOfNewInst()) return SPV_SUCCESS; // Otherwise, there must be an operand that's either a literal, an ID, or // an immediate. std::string operandValue; if ((error = context->getWord(operandValue, &nextPosition))) return context->diagnostic(error) << "Internal Error"; if (operandValue == "=") return context->diagnostic() << firstWord << " not allowed before =."; // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be // expanded. spv_operand_pattern_t dummyExpectedOperands; error = spvTextEncodeOperand( grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(), pInst, &dummyExpectedOperands); if (error) return error; context->setPosition(nextPosition); } return SPV_SUCCESS; } } // anonymous namespace /// @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[in] format the assembly syntax format of text /// @param[out] pInst returned binary Opcode /// @param[in,out] pPosition in the text stream /// /// @return result code spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar, libspirv::AssemblyContext* context, spv_assembly_syntax_format_t format, spv_instruction_t* pInst) { // Check for ! first. if ('!' == context->peek()) { return encodeInstructionStartingWithImmediate(grammar, context, pInst); } // An assembly instruction has two possible formats: // 1(CAF): ..., e.g., "OpTypeVoid %void". // 2(AAF): = ..., e.g., "%void = OpTypeVoid". std::string firstWord; spv_position_t nextPosition = {}; spv_result_t error = context->getWord(firstWord, &nextPosition); if (error) return context->diagnostic() << "Internal Error"; std::string opcodeName; std::string result_id; spv_position_t result_id_position = {}; if (context->startsWithOp()) { opcodeName = firstWord; } else { // If the first word of this instruction is not an opcode, we must be // processing AAF now. if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) { return context->diagnostic() << "Expected at the beginning of an instruction, found '" << firstWord << "'."; } result_id = firstWord; if ('%' != result_id.front()) { return context->diagnostic() << "Expected or at the beginning " "of an instruction, found '" << result_id << "'."; } result_id_position = context->position(); // The '=' sign. context->setPosition(nextPosition); if (context->advance()) return context->diagnostic() << "Expected '=', found end of stream."; std::string equal_sign; error = context->getWord(equal_sign, &nextPosition); if ("=" != equal_sign) return context->diagnostic() << "'=' expected after result id."; // The after the '=' sign. context->setPosition(nextPosition); if (context->advance()) return context->diagnostic() << "Expected opcode, found end of stream."; error = context->getWord(opcodeName, &nextPosition); if (error) return context->diagnostic(error) << "Internal Error"; if (!context->startsWithOp()) { return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'."; } } // NOTE: The table contains Opcode names without the "Op" prefix. const char* pInstName = opcodeName.data() + 2; spv_opcode_desc opcodeEntry; error = grammar.lookupOpcode(pInstName, &opcodeEntry); if (error) { return context->diagnostic(error) << "Invalid Opcode name '" << context->getWord() << "'"; } if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) { // If this instruction has , check it follows AAF. if (opcodeEntry->hasResult && result_id.empty()) { return context->diagnostic() << "Expected at the beginning of an " "instruction, found '" << firstWord << "'."; } } pInst->opcode = opcodeEntry->opcode; context->setPosition(nextPosition); // Reserve the first word for the instruction. spvInstructionAddWord(pInst, 0); // Maintains the ordered list of expected operand types. // For many instructions we only need the {numTypes, operandTypes} // entries in opcodeEntry. However, sometimes we need to modify // the list as we parse the operands. This occurs when an operand // has its own logical operands (such as the LocalSize operand for // ExecutionMode), or for extended instructions that may have their // own operands depending on the selected extended instruction. spv_operand_pattern_t expectedOperands( opcodeEntry->operandTypes, opcodeEntry->operandTypes + opcodeEntry->numTypes); while (!expectedOperands.empty()) { const spv_operand_type_t type = expectedOperands.front(); expectedOperands.pop_front(); // Expand optional tuples lazily. if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue; if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) { // Handle the for value generating instructions. // We've already consumed it from the text stream. Here // we inject its words into the instruction. spv_position_t temp_pos = context->position(); error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID, result_id.c_str(), pInst, nullptr); result_id_position = context->position(); // Because we are injecting we have to reset the position afterwards. context->setPosition(temp_pos); if (error) return error; } else { // Find the next word. error = context->advance(); if (error == SPV_END_OF_STREAM) { if (spvOperandIsOptional(type)) { // This would have been the last potential operand for the // instruction, // and we didn't find one. We're finished parsing this instruction. break; } else { return context->diagnostic() << "Expected operand, found end of stream."; } } assert(error == SPV_SUCCESS && "Somebody added another way to fail"); if (context->isStartOfNewInst()) { if (spvOperandIsOptional(type)) { break; } else { return context->diagnostic() << "Expected operand, found next instruction instead."; } } std::string operandValue; error = context->getWord(operandValue, &nextPosition); if (error) return context->diagnostic(error) << "Internal Error"; error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(), pInst, &expectedOperands); if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type)) return SPV_SUCCESS; if (error) return error; context->setPosition(nextPosition); } } if (spvOpcodeGeneratesType(pInst->opcode)) { if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) { return SPV_ERROR_INVALID_TEXT; } } else if (opcodeEntry->hasType) { // SPIR-V dictates that if an instruction has both a return value and a // type ID then the type id is first, and the return value is second. assert(opcodeEntry->hasResult && "Unknown opcode: has a type but no result."); context->recordTypeIdForValue(pInst->words[2], pInst->words[1]); } if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { return context->diagnostic() << "Instruction too long: " << pInst->words.size() << " words, but the limit is " << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX; } pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode); return SPV_SUCCESS; } namespace { // Translates a given assembly language module into binary form. // If a diagnostic is generated, it is not yet marked as being // for a text-based input. spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar, const spv_text text, spv_assembly_syntax_format_t format, spv_binary* pBinary, spv_diagnostic* pDiagnostic) { if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; libspirv::AssemblyContext context(text, pDiagnostic); if (!text->str || !text->length) return context.diagnostic() << "Text stream is empty."; if (!grammar.isValid()) { return SPV_ERROR_INVALID_TABLE; } if (!pBinary) return SPV_ERROR_INVALID_POINTER; // NOTE: Ensure diagnostic is zero initialised *pDiagnostic = {}; std::vector instructions; if (context.advance()) return context.diagnostic() << "Text stream is empty."; spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; while (context.hasText()) { instructions.push_back({}); spv_instruction_t& inst = instructions.back(); inst.extInstType = extInstType; if (spvTextEncodeOpcode(grammar, &context, format, &inst)) { return SPV_ERROR_INVALID_TEXT; } extInstType = inst.extInstType; if (context.advance()) break; } size_t totalSize = SPV_INDEX_INSTRUCTION; for (auto& inst : instructions) { totalSize += inst.words.size(); } uint32_t* data = new uint32_t[totalSize]; if (!data) return SPV_ERROR_OUT_OF_MEMORY; uint64_t currentIndex = SPV_INDEX_INSTRUCTION; for (auto& inst : instructions) { memcpy(data + currentIndex, inst.words.data(), sizeof(uint32_t) * inst.words.size()); currentIndex += inst.words.size(); } spv_binary binary = new spv_binary_t(); if (!binary) { delete[] data; return SPV_ERROR_OUT_OF_MEMORY; } binary->code = data; binary->wordCount = totalSize; spv_result_t error = spvBinaryHeaderSet(binary, context.getBound()); if (error) { spvBinaryDestroy(binary); return error; } *pBinary = binary; return SPV_SUCCESS; } } // anonymous namespace spv_result_t spvTextToBinary(const char* input_text, const uint64_t input_text_size, const spv_opcode_table opcodeTable, const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, spv_binary* pBinary, spv_diagnostic* pDiagnostic) { return spvTextWithFormatToBinary( input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); } spv_result_t spvTextWithFormatToBinary( const char* input_text, const uint64_t input_text_size, spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable, const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, spv_binary* pBinary, spv_diagnostic* pDiagnostic) { spv_text_t text = {input_text, input_text_size}; libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable); spv_result_t result = spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic); if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; return result; } void spvTextDestroy(spv_text text) { if (!text) return; if (text->str) { delete[] text->str; } delete text; }