2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-09-24 14:26:51 +00:00
|
|
|
//
|
|
|
|
// 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-10-27 20:27:05 +00:00
|
|
|
#ifndef LIBSPIRV_TEXT_HANDLER_H_
|
|
|
|
#define LIBSPIRV_TEXT_HANDLER_H_
|
2015-09-24 14:26:51 +00:00
|
|
|
|
2015-10-13 19:51:12 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <type_traits>
|
2015-09-24 14:26:51 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "diagnostic.h"
|
2015-10-06 20:22:00 +00:00
|
|
|
#include "instruction.h"
|
2016-02-17 19:44:00 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
2015-10-05 17:28:46 +00:00
|
|
|
#include "text.h"
|
2015-09-24 14:26:51 +00:00
|
|
|
|
|
|
|
namespace libspirv {
|
|
|
|
// Structures
|
|
|
|
|
|
|
|
// This is a lattice for tracking types.
|
|
|
|
enum class IdTypeClass {
|
2015-11-02 14:41:20 +00:00
|
|
|
kBottom = 0, // We have no information yet.
|
2015-09-24 14:26:51 +00:00
|
|
|
kScalarIntegerType,
|
|
|
|
kScalarFloatType,
|
|
|
|
kOtherType
|
|
|
|
};
|
|
|
|
|
|
|
|
// Contains ID type information that needs to be tracked across all Ids.
|
|
|
|
// Bitwidth is only valid when type_class is kScalarIntegerType or
|
|
|
|
// kScalarFloatType.
|
|
|
|
struct IdType {
|
|
|
|
uint32_t bitwidth; // Safe to assume that we will not have > 2^32 bits.
|
2015-11-02 14:41:20 +00:00
|
|
|
bool isSigned; // This is only significant if type_class is integral.
|
2015-09-24 14:26:51 +00:00
|
|
|
IdTypeClass type_class;
|
|
|
|
};
|
|
|
|
|
2015-10-16 19:11:00 +00:00
|
|
|
// Default equality operator for IdType. Tests if all members are the same.
|
2015-11-02 14:41:20 +00:00
|
|
|
inline bool operator==(const IdType& first, const IdType& second) {
|
2015-10-16 19:11:00 +00:00
|
|
|
return (first.bitwidth == second.bitwidth) &&
|
|
|
|
(first.isSigned == second.isSigned) &&
|
|
|
|
(first.type_class == second.type_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests whether any member of the IdTypes do not match.
|
2015-11-02 14:41:20 +00:00
|
|
|
inline bool operator!=(const IdType& first, const IdType& second) {
|
2015-10-16 19:11:00 +00:00
|
|
|
return !(first == second);
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:28:46 +00:00
|
|
|
// A value representing an unknown type.
|
|
|
|
extern const IdType kUnknownType;
|
|
|
|
|
2015-09-29 15:28:34 +00:00
|
|
|
// Returns true if the type is a scalar integer type.
|
|
|
|
inline bool isScalarIntegral(const IdType& type) {
|
|
|
|
return type.type_class == IdTypeClass::kScalarIntegerType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the type is a scalar floating point type.
|
|
|
|
inline bool isScalarFloating(const IdType& type) {
|
|
|
|
return type.type_class == IdTypeClass::kScalarFloatType;
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:28:46 +00:00
|
|
|
// Returns the number of bits in the type.
|
|
|
|
// This is only valid for bottom, scalar integer, and scalar floating
|
|
|
|
// classes. For bottom, assume 32 bits.
|
|
|
|
inline int assumedBitWidth(const IdType& type) {
|
2015-11-02 14:41:20 +00:00
|
|
|
switch (type.type_class) {
|
2015-10-05 17:28:46 +00:00
|
|
|
case IdTypeClass::kBottom:
|
|
|
|
return 32;
|
|
|
|
case IdTypeClass::kScalarIntegerType:
|
|
|
|
case IdTypeClass::kScalarFloatType:
|
|
|
|
return type.bitwidth;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// We don't care about this case.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-06 23:08:49 +00:00
|
|
|
// A templated class with a static member function Clamp, where Clamp
|
|
|
|
// sets a referenced value of type T to 0 if T is an unsigned
|
|
|
|
// integer type, and returns true if it modified the referenced
|
|
|
|
// value.
|
|
|
|
template <typename T, typename = void>
|
|
|
|
class ClampToZeroIfUnsignedType {
|
|
|
|
public:
|
|
|
|
// The default specialization does not clamp the value.
|
|
|
|
static bool Clamp(T*) { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// The specialization of ClampToZeroIfUnsignedType for unsigned integer
|
|
|
|
// types.
|
|
|
|
template <typename T>
|
|
|
|
class ClampToZeroIfUnsignedType<
|
|
|
|
T, typename std::enable_if<std::is_unsigned<T>::value>::type> {
|
|
|
|
public:
|
|
|
|
static bool Clamp(T* value_pointer) {
|
|
|
|
if (*value_pointer) {
|
|
|
|
*value_pointer = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
// Encapsulates the data used during the assembly of a SPIR-V module.
|
|
|
|
class AssemblyContext {
|
|
|
|
public:
|
2016-01-06 19:43:55 +00:00
|
|
|
AssemblyContext(spv_text text, spv_diagnostic* diagnostic_arg)
|
2015-09-24 14:26:51 +00:00
|
|
|
: current_position_({}),
|
2016-01-06 19:43:55 +00:00
|
|
|
pDiagnostic_(diagnostic_arg),
|
2015-09-24 14:26:51 +00:00
|
|
|
text_(text),
|
|
|
|
bound_(1) {}
|
|
|
|
|
|
|
|
// Assigns a new integer value to the given text ID, or returns the previously
|
|
|
|
// assigned integer value if the ID has been seen before.
|
2015-11-02 14:41:20 +00:00
|
|
|
uint32_t spvNamedIdAssignOrGet(const char* textValue);
|
2015-09-24 14:26:51 +00:00
|
|
|
|
|
|
|
// Returns the largest largest numeric ID that has been assigned.
|
|
|
|
uint32_t getBound() const;
|
|
|
|
|
|
|
|
// Advances position to point to the next word in the input stream.
|
|
|
|
// Returns SPV_SUCCESS on success.
|
|
|
|
spv_result_t advance();
|
|
|
|
|
2016-03-15 20:52:40 +00:00
|
|
|
// Sets word to the next word in the input text. Fills next_position with
|
2015-09-24 14:26:51 +00:00
|
|
|
// the next location past the end of the word.
|
2016-03-15 20:52:40 +00:00
|
|
|
spv_result_t getWord(std::string* word, spv_position next_position);
|
2015-09-24 14:26:51 +00:00
|
|
|
|
|
|
|
// Returns true if the next word in the input is the start of a new Opcode.
|
|
|
|
bool startsWithOp();
|
|
|
|
|
|
|
|
// Returns true if the next word in the input is the start of a new
|
|
|
|
// instruction.
|
|
|
|
bool isStartOfNewInst();
|
|
|
|
|
|
|
|
// Returns a diagnostic object initialized with current position in the input
|
2015-10-09 19:48:09 +00:00
|
|
|
// stream, and for the given error code. Any data written to this object will
|
|
|
|
// show up in pDiagnsotic on destruction.
|
|
|
|
DiagnosticStream diagnostic(spv_result_t error) {
|
2015-10-28 17:16:56 +00:00
|
|
|
return DiagnosticStream(current_position_, pDiagnostic_, error);
|
2015-10-09 19:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a diagnostic object with the default assembly error code.
|
2015-09-24 14:26:51 +00:00
|
|
|
DiagnosticStream diagnostic() {
|
2015-10-09 19:48:09 +00:00
|
|
|
// The default failure for assembly is invalid text.
|
|
|
|
return diagnostic(SPV_ERROR_INVALID_TEXT);
|
2015-09-24 14:26:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 16:00:10 +00:00
|
|
|
// Returns then next character in the input stream.
|
2015-09-24 14:26:51 +00:00
|
|
|
char peek() const;
|
|
|
|
|
|
|
|
// Returns true if there is more text in the input stream.
|
|
|
|
bool hasText() const;
|
|
|
|
|
|
|
|
// Seeks the input stream forward by 'size' characters.
|
|
|
|
void seekForward(uint32_t size);
|
|
|
|
|
|
|
|
// Sets the current position in the input stream to the given position.
|
2015-11-02 14:41:20 +00:00
|
|
|
void setPosition(const spv_position_t& newPosition) {
|
2015-09-24 14:26:51 +00:00
|
|
|
current_position_ = newPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the current position in the input stream.
|
2015-11-02 14:41:20 +00:00
|
|
|
const spv_position_t& position() const { return current_position_; }
|
2015-09-24 14:26:51 +00:00
|
|
|
|
|
|
|
// Appends the given 32-bit value to the given instruction.
|
2015-10-05 17:28:46 +00:00
|
|
|
// Returns SPV_SUCCESS if the value could be correctly inserted in the
|
2015-09-24 14:26:51 +00:00
|
|
|
// instruction.
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_result_t binaryEncodeU32(const uint32_t value, spv_instruction_t* pInst);
|
2015-10-05 17:28:46 +00:00
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
// Appends the given string to the given instruction.
|
2015-10-05 17:28:46 +00:00
|
|
|
// Returns SPV_SUCCESS if the value could be correctly inserted in the
|
2015-09-24 14:26:51 +00:00
|
|
|
// instruction.
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_result_t binaryEncodeString(const char* value, spv_instruction_t* pInst);
|
2015-09-24 14:26:51 +00:00
|
|
|
|
2015-10-05 17:28:46 +00:00
|
|
|
// Appends the given numeric literal to the given instruction.
|
|
|
|
// Validates and respects the bitwidth supplied in the IdType argument.
|
|
|
|
// If the type is of class kBottom the value will be encoded as a
|
|
|
|
// 32-bit integer.
|
|
|
|
// Returns SPV_SUCCESS if the value could be correctly added to the
|
2015-10-14 15:31:51 +00:00
|
|
|
// instruction. Returns the given error code on failure, and emits
|
2015-11-13 16:00:10 +00:00
|
|
|
// a diagnostic if that error code is not SPV_FAILED_MATCH.
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_result_t binaryEncodeNumericLiteral(const char* numeric_literal,
|
2015-10-14 15:31:51 +00:00
|
|
|
spv_result_t error_code,
|
2015-11-02 14:41:20 +00:00
|
|
|
const IdType& type,
|
|
|
|
spv_instruction_t* pInst);
|
2015-10-05 17:28:46 +00:00
|
|
|
|
2015-09-29 15:28:34 +00:00
|
|
|
// Returns the IdType associated with this type-generating value.
|
|
|
|
// If the type has not been previously recorded with recordTypeDefinition,
|
2015-10-05 17:28:46 +00:00
|
|
|
// kUnknownType will be returned.
|
2015-09-29 15:28:34 +00:00
|
|
|
IdType getTypeOfTypeGeneratingValue(uint32_t value) const;
|
|
|
|
|
|
|
|
// Returns the IdType that represents the return value of this Value
|
|
|
|
// generating instruction.
|
|
|
|
// If the value has not been recorded with recordTypeIdForValue, or the type
|
2015-10-05 17:28:46 +00:00
|
|
|
// could not be determined kUnknownType will be returned.
|
2015-09-29 15:28:34 +00:00
|
|
|
IdType getTypeOfValueInstruction(uint32_t value) const;
|
|
|
|
|
|
|
|
// Tracks the type-defining instruction. The result of the tracking can
|
|
|
|
// later be queried using getValueType.
|
|
|
|
// pInst is expected to be completely filled in by the time this instruction
|
|
|
|
// is called.
|
|
|
|
// Returns SPV_SUCCESS on success, or SPV_ERROR_INVALID_VALUE on error.
|
|
|
|
spv_result_t recordTypeDefinition(const spv_instruction_t* pInst);
|
|
|
|
|
|
|
|
// Tracks the relationship between the value and its type.
|
|
|
|
spv_result_t recordTypeIdForValue(uint32_t value, uint32_t type);
|
|
|
|
|
2015-11-09 23:55:42 +00:00
|
|
|
// Records the given Id as being the import of the given extended instruction
|
|
|
|
// type.
|
|
|
|
spv_result_t recordIdAsExtInstImport(uint32_t id, spv_ext_inst_type_t type);
|
|
|
|
|
|
|
|
// Returns the extended instruction type corresponding to the import with
|
|
|
|
// the given Id, if it exists. Returns SPV_EXT_INST_TYPE_NONE if the
|
|
|
|
// id is not the id for an extended instruction type.
|
|
|
|
spv_ext_inst_type_t getExtInstTypeForId(uint32_t id) const;
|
|
|
|
|
2015-10-13 19:51:12 +00:00
|
|
|
// Parses a numeric value of a given type from the given text. The number
|
|
|
|
// should take up the entire string, and should be within bounds for the
|
|
|
|
// target type. On success, returns SPV_SUCCESS and populates the object
|
2015-10-14 15:31:51 +00:00
|
|
|
// referenced by value_pointer. On failure, returns the given error code,
|
|
|
|
// and emits a diagnostic if that error code is not SPV_FAILED_MATCH.
|
2015-10-13 19:51:12 +00:00
|
|
|
template <typename T>
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_result_t parseNumber(const char* text, spv_result_t error_code,
|
|
|
|
T* value_pointer,
|
|
|
|
const char* error_message_fragment) {
|
2015-10-13 19:51:12 +00:00
|
|
|
// C++11 doesn't define std::istringstream(int8_t&), so calling this method
|
|
|
|
// with a single-byte type leads to implementation-defined behaviour.
|
|
|
|
// Similarly for uint8_t.
|
2015-11-02 14:41:20 +00:00
|
|
|
static_assert(sizeof(T) > 1,
|
|
|
|
"Don't use a single-byte type this parse method");
|
2015-10-13 19:51:12 +00:00
|
|
|
|
|
|
|
std::istringstream text_stream(text);
|
|
|
|
// Allow both decimal and hex input for integers.
|
|
|
|
// It also allows octal input, but we don't care about that case.
|
|
|
|
text_stream >> std::setbase(0);
|
|
|
|
text_stream >> *value_pointer;
|
|
|
|
|
|
|
|
// We should have read something.
|
2016-02-25 21:11:16 +00:00
|
|
|
bool ok = (text[0] != 0) && !text_stream.bad();
|
2015-10-13 19:51:12 +00:00
|
|
|
// It should have been all the text.
|
|
|
|
ok = ok && text_stream.eof();
|
|
|
|
// It should have been in range.
|
|
|
|
ok = ok && !text_stream.fail();
|
2015-11-06 23:08:49 +00:00
|
|
|
|
2015-10-13 19:51:12 +00:00
|
|
|
// Work around a bug in the GNU C++11 library. It will happily parse
|
|
|
|
// "-1" for uint16_t as 65535.
|
2015-11-06 23:08:49 +00:00
|
|
|
if (ok && text[0] == '-')
|
|
|
|
ok = !ClampToZeroIfUnsignedType<T>::Clamp(value_pointer);
|
2015-10-13 19:51:12 +00:00
|
|
|
|
|
|
|
if (ok) return SPV_SUCCESS;
|
2015-10-14 15:31:51 +00:00
|
|
|
return diagnostic(error_code) << error_message_fragment << text;
|
2015-10-13 19:51:12 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
private:
|
2016-02-17 18:00:23 +00:00
|
|
|
|
2015-10-05 17:28:46 +00:00
|
|
|
// Appends the given floating point literal to the given instruction.
|
|
|
|
// Returns SPV_SUCCESS if the value was correctly parsed. Otherwise
|
2015-10-14 15:31:51 +00:00
|
|
|
// returns the given error code, and emits a diagnostic if that error
|
|
|
|
// code is not SPV_FAILED_MATCH.
|
|
|
|
// Only 32 and 64 bit floating point numbers are supported.
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_result_t binaryEncodeFloatingPointLiteral(const char* numeric_literal,
|
2015-10-14 15:31:51 +00:00
|
|
|
spv_result_t error_code,
|
2015-10-05 17:28:46 +00:00
|
|
|
const IdType& type,
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_instruction_t* pInst);
|
2015-10-05 17:28:46 +00:00
|
|
|
|
|
|
|
// Appends the given integer literal to the given instruction.
|
|
|
|
// Returns SPV_SUCCESS if the value was correctly parsed. Otherwise
|
2015-10-14 15:31:51 +00:00
|
|
|
// returns the given error code, and emits a diagnostic if that error
|
|
|
|
// code is not SPV_FAILED_MATCH.
|
|
|
|
// Integers up to 64 bits are supported.
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_result_t binaryEncodeIntegerLiteral(const char* numeric_literal,
|
2015-10-14 15:31:51 +00:00
|
|
|
spv_result_t error_code,
|
2015-11-02 14:41:20 +00:00
|
|
|
const IdType& type,
|
|
|
|
spv_instruction_t* pInst);
|
2015-10-05 17:28:46 +00:00
|
|
|
|
|
|
|
// Writes the given 64-bit literal value into the instruction.
|
|
|
|
// return SPV_SUCCESS if the value could be written in the instruction.
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_result_t binaryEncodeU64(const uint64_t value, spv_instruction_t* pInst);
|
2015-09-24 14:26:51 +00:00
|
|
|
// Maps ID names to their corresponding numerical ids.
|
|
|
|
using spv_named_id_table = std::unordered_map<std::string, uint32_t>;
|
|
|
|
// Maps type-defining IDs to their IdType.
|
|
|
|
using spv_id_to_type_map = std::unordered_map<uint32_t, IdType>;
|
|
|
|
// Maps Ids to the id of their type.
|
|
|
|
using spv_id_to_type_id = std::unordered_map<uint32_t, uint32_t>;
|
|
|
|
|
|
|
|
spv_named_id_table named_ids_;
|
|
|
|
spv_id_to_type_map types_;
|
|
|
|
spv_id_to_type_id value_types_;
|
2015-11-09 23:55:42 +00:00
|
|
|
// Maps an extended instruction import Id to the extended instruction type.
|
|
|
|
std::unordered_map<uint32_t, spv_ext_inst_type_t> import_id_to_ext_inst_type_;
|
2015-09-24 14:26:51 +00:00
|
|
|
spv_position_t current_position_;
|
2015-11-02 14:41:20 +00:00
|
|
|
spv_diagnostic* pDiagnostic_;
|
2015-09-24 14:26:51 +00:00
|
|
|
spv_text text_;
|
|
|
|
uint32_t bound_;
|
|
|
|
};
|
|
|
|
}
|
2015-10-27 20:27:05 +00:00
|
|
|
#endif // _LIBSPIRV_TEXT_HANDLER_H_
|