2015-05-22 17:26:19 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-09-16 22:32:54 +00:00
|
|
|
#include "text.h"
|
|
|
|
|
2015-09-11 20:34:49 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2015-09-22 19:50:33 +00:00
|
|
|
#include <cctype>
|
2015-09-11 20:34:49 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2015-09-16 22:32:54 +00:00
|
|
|
#include <cstring>
|
2015-09-24 14:26:51 +00:00
|
|
|
#include <memory>
|
2015-09-11 20:34:49 +00:00
|
|
|
#include <string>
|
2015-09-24 14:26:51 +00:00
|
|
|
#include <sstream>
|
2015-09-16 22:32:54 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2015-09-11 20:34:49 +00:00
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
#include "binary.h"
|
2015-09-16 22:32:54 +00:00
|
|
|
#include "bitwisecast.h"
|
2015-05-22 17:26:19 +00:00
|
|
|
#include "diagnostic.h"
|
|
|
|
#include "ext_inst.h"
|
2015-09-16 22:32:54 +00:00
|
|
|
#include <libspirv/libspirv.h>
|
2015-05-22 17:26:19 +00:00
|
|
|
#include "opcode.h"
|
|
|
|
#include "operand.h"
|
2015-09-24 14:26:51 +00:00
|
|
|
#include "text_handler.h"
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-09-09 14:36:48 +00:00
|
|
|
using spvutils::BitwiseCast;
|
|
|
|
|
2015-09-22 19:50:33 +00:00
|
|
|
bool spvIsValidIDCharacter(const char value) {
|
|
|
|
return value == '_' || 0 != ::isalnum(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the given string represents a valid ID name.
|
2015-09-29 21:07:21 +00:00
|
|
|
bool spvIsValidID(const char* textValue) {
|
|
|
|
const char* c = textValue;
|
2015-09-22 19:50:33 +00:00
|
|
|
for (; *c != '\0'; ++c) {
|
|
|
|
if (!spvIsValidIDCharacter(*c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the string was empty, then the ID also is not valid.
|
|
|
|
return c != textValue;
|
|
|
|
}
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
// Text API
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-09-28 21:04:39 +00:00
|
|
|
spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
|
2015-05-22 17:26:19 +00:00
|
|
|
bool isSigned = false;
|
2015-08-24 19:33:14 +00:00
|
|
|
int numPeriods = 0;
|
2015-05-22 17:26:19 +00:00
|
|
|
bool isString = false;
|
|
|
|
|
2015-08-24 19:33:14 +00:00
|
|
|
const size_t len = strlen(textValue);
|
2015-08-24 20:27:02 +00:00
|
|
|
if (len == 0) return SPV_FAILED_MATCH;
|
|
|
|
|
2015-08-24 19:33:14 +00:00
|
|
|
for (uint64_t index = 0; index < len; ++index) {
|
2015-05-22 17:26:19 +00:00
|
|
|
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 '.':
|
2015-08-24 19:33:14 +00:00
|
|
|
numPeriods++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (index == 0) {
|
|
|
|
isSigned = true;
|
|
|
|
} else {
|
|
|
|
isString = true;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isString = true;
|
2015-09-10 21:07:21 +00:00
|
|
|
index = len; // break out of the loop too.
|
2015-05-22 17:26:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 19:33:14 +00:00
|
|
|
pLiteral->type = spv_literal_type_t(99);
|
|
|
|
|
2015-09-10 21:07:21 +00:00
|
|
|
if (isString || numPeriods > 1 || (isSigned && len == 1)) {
|
2015-08-24 19:33:14 +00:00
|
|
|
// TODO(dneto): Allow escaping.
|
2015-08-24 20:27:02 +00:00
|
|
|
if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
|
|
|
|
return SPV_FAILED_MATCH;
|
2015-05-22 17:26:19 +00:00
|
|
|
pLiteral->type = SPV_LITERAL_TYPE_STRING;
|
2015-08-24 19:33:14 +00:00
|
|
|
// Need room for the null-terminator.
|
2015-08-24 20:27:02 +00:00
|
|
|
if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
|
2015-09-10 21:07:21 +00:00
|
|
|
strncpy(pLiteral->value.str, textValue + 1, len - 2);
|
|
|
|
pLiteral->value.str[len - 2] = 0;
|
2015-08-24 19:33:14 +00:00
|
|
|
} else if (numPeriods == 1) {
|
|
|
|
double d = std::strtod(textValue, nullptr);
|
2015-05-22 17:26:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-29 21:07:21 +00:00
|
|
|
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 == '!');
|
|
|
|
const char* begin = text + 1;
|
|
|
|
char* end = nullptr;
|
2015-09-29 21:58:37 +00:00
|
|
|
const uint64_t parseResult = strtoull(begin, &end, 0);
|
2015-09-29 21:07:21 +00:00
|
|
|
size_t length = end - begin;
|
|
|
|
if (length != strlen(begin)) {
|
|
|
|
context->diagnostic() << "Invalid immediate integer '" << text << "'.";
|
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-09-29 21:58:37 +00:00
|
|
|
} else if (length > 10 || (parseResult >> 32) != 0) {
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic() << "Immediate integer '" << text
|
|
|
|
<< "' is over 32 bits.";
|
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
|
|
|
context->binaryEncodeU32(parseResult, pInst);
|
|
|
|
context->seekForward(strlen(text));
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-09-22 19:50:33 +00:00
|
|
|
/// @brief Translate an Opcode operand to binary form
|
|
|
|
///
|
2015-09-24 14:26:51 +00:00
|
|
|
/// @param[in] grammar the grammar to use for compilation
|
|
|
|
/// @param[in, out] context the dynamic compilation info
|
2015-09-22 19:50:33 +00:00
|
|
|
/// @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
|
2015-09-29 21:07:21 +00:00
|
|
|
spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
|
2015-09-24 14:26:51 +00:00
|
|
|
libspirv::AssemblyContext* context,
|
|
|
|
const spv_operand_type_t type,
|
|
|
|
const char* textValue,
|
|
|
|
spv_instruction_t* pInst,
|
|
|
|
spv_operand_pattern_t* pExpectedOperands) {
|
2015-05-22 17:26:19 +00:00
|
|
|
// NOTE: Handle immediate int in the stream
|
|
|
|
if ('!' == textValue[0]) {
|
2015-09-29 21:07:21 +00:00
|
|
|
if (auto error = encodeImmediate(context, textValue, pInst)) {
|
|
|
|
return error;
|
2015-09-11 18:31:27 +00:00
|
|
|
}
|
2015-09-29 14:38:18 +00:00
|
|
|
*pExpectedOperands =
|
|
|
|
spvAlternatePatternFollowingImmediate(*pExpectedOperands);
|
2015-05-22 17:26:19 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
2015-09-25 17:56:09 +00:00
|
|
|
case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
|
2015-08-27 17:50:05 +00:00
|
|
|
case SPV_OPERAND_TYPE_ID:
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
|
|
|
|
case SPV_OPERAND_TYPE_OPTIONAL_ID:
|
2015-09-25 17:56:09 +00:00
|
|
|
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
|
|
|
|
case SPV_OPERAND_TYPE_RESULT_ID: {
|
2015-05-22 17:26:19 +00:00
|
|
|
if ('%' == textValue[0]) {
|
|
|
|
textValue++;
|
|
|
|
} else {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Expected id to start with %.";
|
2015-09-22 19:50:33 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
|
|
|
if (!spvIsValidID(textValue)) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Invalid ID " << textValue;
|
2015-09-22 19:50:33 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
|
2015-05-22 17:26:19 +00:00
|
|
|
pInst->words[pInst->wordCount++] = id;
|
|
|
|
} break;
|
|
|
|
case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
|
|
|
|
// NOTE: Special case for extension instruction lookup
|
|
|
|
if (OpExtInst == pInst->opcode) {
|
|
|
|
spv_ext_inst_desc extInst;
|
2015-09-29 21:07:21 +00:00
|
|
|
if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Invalid extended instruction name '"
|
2015-09-29 21:07:21 +00:00
|
|
|
<< textValue << "'.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
pInst->words[pInst->wordCount++] = extInst->ext_inst;
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
|
|
|
|
// Prepare to parse the operands for the extended instructions.
|
|
|
|
spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
2015-09-15 17:36:21 +00:00
|
|
|
} // Fall through for the general case.
|
2015-09-14 19:22:23 +00:00
|
|
|
case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
|
2015-09-25 18:23:29 +00:00
|
|
|
case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
|
|
|
|
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
|
2015-05-22 17:26:19 +00:00
|
|
|
spv_literal_t literal = {};
|
2015-09-15 17:36:21 +00:00
|
|
|
spv_result_t error = spvTextToLiteral(textValue, &literal);
|
|
|
|
if (error != SPV_SUCCESS) {
|
|
|
|
if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
|
|
|
|
if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic() << "Invalid literal number '" << textValue
|
|
|
|
<< "'.";
|
2015-09-15 17:36:21 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-09-11 04:43:11 +00:00
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
switch (literal.type) {
|
2015-09-10 14:28:22 +00:00
|
|
|
// We do not have to print diagnostics here because spvBinaryEncode*
|
|
|
|
// prints diagnostic messages on failure.
|
2015-05-22 17:26:19 +00:00
|
|
|
case SPV_LITERAL_TYPE_INT_32:
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
|
|
|
|
pInst))
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
break;
|
|
|
|
case SPV_LITERAL_TYPE_INT_64: {
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
|
|
|
|
pInst))
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
|
|
|
case SPV_LITERAL_TYPE_UINT_32: {
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeU32(literal.value.u32, pInst))
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
|
|
|
case SPV_LITERAL_TYPE_UINT_64: {
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
|
|
|
|
pInst))
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
|
|
|
case SPV_LITERAL_TYPE_FLOAT_32: {
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f),
|
|
|
|
pInst))
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
|
|
|
case SPV_LITERAL_TYPE_FLOAT_64: {
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d),
|
|
|
|
pInst))
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
|
|
|
case SPV_LITERAL_TYPE_STRING: {
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic()
|
|
|
|
<< "Expected literal number, found literal string '" << textValue
|
|
|
|
<< "'.";
|
2015-09-15 17:36:21 +00:00
|
|
|
return SPV_FAILED_MATCH;
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
|
|
|
default:
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic() << "Invalid literal number '" << textValue
|
|
|
|
<< "'";
|
2015-05-22 17:26:19 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
|
|
|
} break;
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
case SPV_OPERAND_TYPE_LITERAL_STRING:
|
|
|
|
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
|
2015-09-15 17:36:21 +00:00
|
|
|
spv_literal_t literal = {};
|
|
|
|
spv_result_t error = spvTextToLiteral(textValue, &literal);
|
|
|
|
if (error != SPV_SUCCESS) {
|
|
|
|
if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
|
2015-09-11 18:31:27 +00:00
|
|
|
if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic() << "Invalid literal string '" << textValue
|
|
|
|
<< "'.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-15 17:36:21 +00:00
|
|
|
if (literal.type != SPV_LITERAL_TYPE_STRING) {
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic()
|
|
|
|
<< "Expected literal string, found literal number '" << textValue
|
|
|
|
<< "'.";
|
2015-09-15 17:36:21 +00:00
|
|
|
return SPV_FAILED_MATCH;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
// NOTE: Special case for extended instruction library import
|
|
|
|
if (OpExtInstImport == pInst->opcode) {
|
2015-09-15 17:36:21 +00:00
|
|
|
pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeString(literal.value.str, pInst))
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
2015-09-16 22:32:54 +00:00
|
|
|
case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
|
|
|
|
case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
|
|
|
|
case SPV_OPERAND_TYPE_LOOP_CONTROL:
|
2015-09-18 15:19:18 +00:00
|
|
|
case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
|
2015-09-17 21:06:10 +00:00
|
|
|
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
|
2015-09-16 22:32:54 +00:00
|
|
|
case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
|
|
|
|
uint32_t value;
|
2015-09-24 14:26:51 +00:00
|
|
|
if (grammar.parseMaskOperand(type, textValue, &value)) {
|
|
|
|
context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
|
2015-09-29 21:07:21 +00:00
|
|
|
<< textValue << "'.";
|
2015-09-16 22:32:54 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
if (auto error = context->binaryEncodeU32(value, pInst)) return error;
|
2015-09-17 21:06:10 +00:00
|
|
|
// Prepare to parse the operands for this logical operand.
|
2015-09-24 14:26:51 +00:00
|
|
|
grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
|
2015-09-28 21:04:39 +00:00
|
|
|
} break;
|
|
|
|
case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
|
|
|
|
auto error = spvTextEncodeOperand(
|
2015-09-24 14:26:51 +00:00
|
|
|
grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
|
|
|
|
pInst, pExpectedOperands);
|
2015-09-28 21:04:39 +00:00
|
|
|
if (error == SPV_FAILED_MATCH) {
|
|
|
|
// It's not a literal number -- is it a literal string?
|
2015-09-24 14:26:51 +00:00
|
|
|
error = spvTextEncodeOperand(grammar, context,
|
|
|
|
SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
|
|
|
|
textValue, pInst, pExpectedOperands);
|
2015-09-28 21:04:39 +00:00
|
|
|
}
|
|
|
|
if (error == SPV_FAILED_MATCH) {
|
|
|
|
// It's not a literal -- is it an ID?
|
2015-09-24 14:26:51 +00:00
|
|
|
error =
|
|
|
|
spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
|
|
|
|
textValue, pInst, pExpectedOperands);
|
2015-09-28 21:04:39 +00:00
|
|
|
}
|
|
|
|
if (error) {
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic() << "Invalid word following !<integer>: "
|
|
|
|
<< textValue;
|
2015-09-28 21:04:39 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (pExpectedOperands->empty()) {
|
|
|
|
pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
|
|
|
|
}
|
2015-09-16 22:32:54 +00:00
|
|
|
} break;
|
2015-05-22 17:26:19 +00:00
|
|
|
default: {
|
|
|
|
// NOTE: All non literal operands are handled here using the operand
|
|
|
|
// table.
|
|
|
|
spv_operand_desc entry;
|
2015-09-29 21:07:21 +00:00
|
|
|
if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
|
2015-09-29 21:07:21 +00:00
|
|
|
<< textValue << "'.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->binaryEncodeU32(entry->value, pInst)) {
|
|
|
|
context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
|
2015-09-29 21:07:21 +00:00
|
|
|
<< textValue << "'.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
|
|
|
|
// Prepare to parse the operands for this logical operand.
|
|
|
|
spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
|
2015-05-22 17:26:19 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-09-11 04:43:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Encodes an instruction started by !<integer> 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(
|
2015-09-29 21:07:21 +00:00
|
|
|
const libspirv::AssemblyGrammar& grammar,
|
2015-09-24 14:26:51 +00:00
|
|
|
libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
|
2015-09-11 04:43:11 +00:00
|
|
|
std::string firstWord;
|
|
|
|
spv_position_t nextPosition = {};
|
2015-09-24 14:26:51 +00:00
|
|
|
auto error = context->getWord(firstWord, &nextPosition);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Internal Error";
|
2015-09-11 18:31:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
2015-09-11 04:43:11 +00:00
|
|
|
|
2015-09-29 21:07:21 +00:00
|
|
|
if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
|
|
|
|
return error;
|
2015-09-11 18:31:27 +00:00
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
while (context->advance() != SPV_END_OF_STREAM) {
|
2015-09-11 04:43:11 +00:00
|
|
|
// A beginning of a new instruction means we're done.
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->isStartOfNewInst()) return SPV_SUCCESS;
|
2015-09-11 04:43:11 +00:00
|
|
|
|
|
|
|
// Otherwise, there must be an operand that's either a literal, an ID, or
|
|
|
|
// an immediate.
|
|
|
|
std::string operandValue;
|
2015-09-24 14:26:51 +00:00
|
|
|
if ((error = context->getWord(operandValue, &nextPosition))) {
|
|
|
|
context->diagnostic() << "Internal Error";
|
2015-09-11 04:43:11 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2015-09-11 19:03:54 +00:00
|
|
|
if (operandValue == "=") {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << firstWord << " not allowed before =.";
|
2015-09-11 19:03:54 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
|
|
|
|
2015-09-11 04:43:11 +00:00
|
|
|
// Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
|
|
|
|
// expanded.
|
|
|
|
spv_operand_pattern_t dummyExpectedOperands;
|
|
|
|
error = spvTextEncodeOperand(
|
2015-09-24 14:26:51 +00:00
|
|
|
grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
|
|
|
|
pInst, &dummyExpectedOperands);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) return error;
|
2015-09-24 14:26:51 +00:00
|
|
|
context->setPosition(nextPosition);
|
2015-09-11 04:43:11 +00:00
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-09-22 19:50:33 +00:00
|
|
|
/// @brief Translate single Opcode and operands to binary form
|
|
|
|
///
|
2015-09-24 14:26:51 +00:00
|
|
|
/// @param[in] grammar the grammar to use for compilation
|
|
|
|
/// @param[in, out] context the dynamic compilation info
|
2015-09-22 19:50:33 +00:00
|
|
|
/// @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
|
2015-09-29 21:07:21 +00:00
|
|
|
spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
|
2015-09-24 14:26:51 +00:00
|
|
|
libspirv::AssemblyContext* context,
|
|
|
|
spv_assembly_syntax_format_t format,
|
|
|
|
spv_instruction_t* pInst) {
|
2015-09-11 04:43:11 +00:00
|
|
|
// Check for !<integer> first.
|
2015-09-24 14:26:51 +00:00
|
|
|
if ('!' == context->peek()) {
|
|
|
|
return encodeInstructionStartingWithImmediate(grammar, context, pInst);
|
2015-09-11 04:43:11 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 15:50:55 +00:00
|
|
|
// An assembly instruction has two possible formats:
|
2015-09-10 18:00:00 +00:00
|
|
|
// 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
|
|
|
|
// 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
|
2015-08-21 15:50:55 +00:00
|
|
|
|
2015-09-10 18:00:00 +00:00
|
|
|
std::string firstWord;
|
2015-05-22 17:26:19 +00:00
|
|
|
spv_position_t nextPosition = {};
|
2015-09-24 14:26:51 +00:00
|
|
|
spv_result_t error = context->getWord(firstWord, &nextPosition);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Internal Error";
|
2015-09-11 18:31:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-09-10 18:00:00 +00:00
|
|
|
std::string opcodeName;
|
2015-08-21 15:50:55 +00:00
|
|
|
std::string result_id;
|
|
|
|
spv_position_t result_id_position = {};
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->startsWithOp()) {
|
2015-09-10 18:00:00 +00:00
|
|
|
opcodeName = firstWord;
|
|
|
|
} else {
|
|
|
|
// If the first word of this instruction is not an opcode, we must be
|
|
|
|
// processing AAF now.
|
2015-09-11 18:31:27 +00:00
|
|
|
if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic()
|
2015-09-11 18:31:27 +00:00
|
|
|
<< "Expected <opcode> at the beginning of an instruction, found '"
|
|
|
|
<< firstWord << "'.";
|
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-10 18:00:00 +00:00
|
|
|
|
|
|
|
result_id = firstWord;
|
2015-09-11 18:31:27 +00:00
|
|
|
if ('%' != result_id.front()) {
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic()
|
|
|
|
<< "Expected <opcode> or <result-id> at the beginning "
|
|
|
|
"of an instruction, found '"
|
|
|
|
<< result_id << "'.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
result_id_position = context->position();
|
2015-09-10 18:00:00 +00:00
|
|
|
|
|
|
|
// The '=' sign.
|
2015-09-24 14:26:51 +00:00
|
|
|
context->setPosition(nextPosition);
|
|
|
|
if (context->advance()) {
|
|
|
|
context->diagnostic() << "Expected '=', found end of stream.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-08-21 15:50:55 +00:00
|
|
|
std::string equal_sign;
|
2015-09-24 14:26:51 +00:00
|
|
|
error = context->getWord(equal_sign, &nextPosition);
|
2015-09-11 18:31:27 +00:00
|
|
|
if ("=" != equal_sign) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "'=' expected after result id.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-08-21 15:50:55 +00:00
|
|
|
|
|
|
|
// The <opcode> after the '=' sign.
|
2015-09-24 14:26:51 +00:00
|
|
|
context->setPosition(nextPosition);
|
|
|
|
if (context->advance()) {
|
|
|
|
context->diagnostic() << "Expected opcode, found end of stream.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
error = context->getWord(opcodeName, &nextPosition);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Internal Error";
|
2015-09-11 18:31:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
if (!context->startsWithOp()) {
|
|
|
|
context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-08-21 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
// NOTE: The table contains Opcode names without the "Op" prefix.
|
2015-09-28 21:04:39 +00:00
|
|
|
const char* pInstName = opcodeName.data() + 2;
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
spv_opcode_desc opcodeEntry;
|
2015-09-24 14:26:51 +00:00
|
|
|
error = grammar.lookupOpcode(pInstName, &opcodeEntry);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) {
|
2015-09-29 21:07:21 +00:00
|
|
|
context->diagnostic() << "Invalid Opcode name '" << context->getWord()
|
|
|
|
<< "'";
|
2015-09-11 18:31:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
2015-09-10 18:00:00 +00:00
|
|
|
if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
|
|
|
|
// If this instruction has <result-id>, check it follows AAF.
|
2015-09-11 18:31:27 +00:00
|
|
|
if (opcodeEntry->hasResult && result_id.empty()) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Expected <result-id> at the beginning of an "
|
2015-09-29 21:07:21 +00:00
|
|
|
"instruction, found '"
|
|
|
|
<< firstWord << "'.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-10 18:00:00 +00:00
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
pInst->opcode = opcodeEntry->opcode;
|
2015-09-24 14:26:51 +00:00
|
|
|
context->setPosition(nextPosition);
|
2015-05-22 17:26:19 +00:00
|
|
|
pInst->wordCount++;
|
|
|
|
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
// 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.
|
2015-09-10 21:07:21 +00:00
|
|
|
if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
|
2015-08-21 15:50:55 +00:00
|
|
|
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
|
|
|
|
// Handle the <result-id> for value generating instructions.
|
|
|
|
// We've already consumed it from the text stream. Here
|
|
|
|
// we inject its words into the instruction.
|
2015-09-24 14:26:51 +00:00
|
|
|
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);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) return error;
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
} else {
|
|
|
|
// Find the next word.
|
2015-09-24 14:26:51 +00:00
|
|
|
error = context->advance();
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
if (error == SPV_END_OF_STREAM) {
|
|
|
|
if (spvOperandIsOptional(type)) {
|
2015-09-10 21:07:21 +00:00
|
|
|
// This would have been the last potential operand for the
|
|
|
|
// instruction,
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
// and we didn't find one. We're finished parsing this instruction.
|
|
|
|
break;
|
|
|
|
} else {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Expected operand, found end of stream.";
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(error == SPV_SUCCESS && "Somebody added another way to fail");
|
2015-08-21 15:50:55 +00:00
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context->isStartOfNewInst()) {
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
if (spvOperandIsOptional(type)) {
|
|
|
|
break;
|
2015-05-22 17:26:19 +00:00
|
|
|
} else {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic()
|
|
|
|
<< "Expected operand, found next instruction instead.";
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
std::string operandValue;
|
2015-09-24 14:26:51 +00:00
|
|
|
error = context->getWord(operandValue, &nextPosition);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context->diagnostic() << "Internal Error";
|
2015-09-11 18:31:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
|
|
|
|
pInst, &expectedOperands);
|
2015-05-22 17:26:19 +00:00
|
|
|
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) return error;
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
context->setPosition(nextPosition);
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:05:14 +00:00
|
|
|
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.
|
2015-09-29 21:07:21 +00:00
|
|
|
spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
|
|
|
|
const spv_text text,
|
|
|
|
spv_assembly_syntax_format_t format,
|
|
|
|
spv_binary* pBinary,
|
|
|
|
spv_diagnostic* pDiagnostic) {
|
2015-09-24 14:26:51 +00:00
|
|
|
if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
|
|
|
|
libspirv::AssemblyContext context(text, pDiagnostic);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (!text->str || !text->length) {
|
2015-09-24 14:26:51 +00:00
|
|
|
context.diagnostic() << "Text stream is empty.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
if (!grammar.isValid()) {
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TABLE;
|
2015-09-24 14:26:51 +00:00
|
|
|
}
|
2015-09-11 18:31:27 +00:00
|
|
|
if (!pBinary) return SPV_ERROR_INVALID_POINTER;
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
// NOTE: Ensure diagnostic is zero initialised
|
|
|
|
*pDiagnostic = {};
|
|
|
|
|
|
|
|
std::vector<spv_instruction_t> instructions;
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
if (context.advance()) {
|
|
|
|
context.diagnostic() << "Text stream is empty.";
|
2015-09-11 18:31:27 +00:00
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
|
|
|
|
while (context.hasText()) {
|
|
|
|
spv_instruction_t inst = {};
|
|
|
|
inst.extInstType = extInstType;
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
|
|
|
|
return SPV_ERROR_INVALID_TEXT;
|
2015-09-11 18:31:27 +00:00
|
|
|
}
|
2015-09-24 14:26:51 +00:00
|
|
|
extInstType = inst.extInstType;
|
|
|
|
|
|
|
|
instructions.push_back(inst);
|
|
|
|
|
|
|
|
if (context.advance()) break;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t totalSize = SPV_INDEX_INSTRUCTION;
|
2015-09-28 21:04:39 +00:00
|
|
|
for (auto& inst : instructions) {
|
2015-05-22 17:26:19 +00:00
|
|
|
totalSize += inst.wordCount;
|
|
|
|
}
|
|
|
|
|
2015-09-28 21:04:39 +00:00
|
|
|
uint32_t* data = new uint32_t[totalSize];
|
2015-09-11 18:31:27 +00:00
|
|
|
if (!data) return SPV_ERROR_OUT_OF_MEMORY;
|
2015-05-22 17:26:19 +00:00
|
|
|
uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
|
2015-09-28 21:04:39 +00:00
|
|
|
for (auto& inst : instructions) {
|
2015-05-22 17:26:19 +00:00
|
|
|
memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
|
|
|
|
currentIndex += inst.wordCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_binary binary = new spv_binary_t();
|
2015-09-11 18:31:27 +00:00
|
|
|
if (!binary) {
|
|
|
|
delete[] data;
|
|
|
|
return SPV_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
binary->code = data;
|
|
|
|
binary->wordCount = totalSize;
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) {
|
|
|
|
spvBinaryDestroy(binary);
|
|
|
|
return error;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
*pBinary = binary;
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-09-10 21:07:21 +00:00
|
|
|
} // anonymous namespace
|
2015-09-01 22:05:14 +00:00
|
|
|
|
2015-09-28 21:04:39 +00:00
|
|
|
spv_result_t spvTextToBinary(const char* input_text,
|
2015-09-09 17:04:32 +00:00
|
|
|
const uint64_t input_text_size,
|
2015-09-01 22:05:14 +00:00
|
|
|
const spv_opcode_table opcodeTable,
|
|
|
|
const spv_operand_table operandTable,
|
|
|
|
const spv_ext_inst_table extInstTable,
|
2015-09-28 21:04:39 +00:00
|
|
|
spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
|
2015-09-10 18:00:00 +00:00
|
|
|
return spvTextWithFormatToBinary(
|
|
|
|
input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
|
|
|
|
opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t spvTextWithFormatToBinary(
|
2015-09-28 21:04:39 +00:00
|
|
|
const char* input_text, const uint64_t input_text_size,
|
2015-09-10 18:00:00 +00:00
|
|
|
spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
|
|
|
|
const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
|
2015-09-28 21:04:39 +00:00
|
|
|
spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
|
2015-09-09 17:04:32 +00:00
|
|
|
spv_text_t text = {input_text, input_text_size};
|
|
|
|
|
2015-09-29 21:07:21 +00:00
|
|
|
libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
|
2015-09-10 18:00:00 +00:00
|
|
|
spv_result_t result =
|
2015-09-24 14:26:51 +00:00
|
|
|
spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
|
2015-09-01 22:05:14 +00:00
|
|
|
if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
void spvTextDestroy(spv_text text) {
|
2015-09-11 18:31:27 +00:00
|
|
|
if (!text) return;
|
2015-05-22 17:26:19 +00:00
|
|
|
if (text->str) {
|
|
|
|
delete[] text->str;
|
|
|
|
}
|
|
|
|
delete text;
|
|
|
|
}
|