Move AssemblyGrammar into its own source files
authorDavid Neto <dneto@google.com>
Tue, 27 Oct 2015 19:31:10 +0000 (15:31 -0400)
committerDavid Neto <dneto@google.com>
Mon, 2 Nov 2015 18:52:05 +0000 (13:52 -0500)
CMakeLists.txt
source/assembly_grammar.cpp [new file with mode: 0644]
source/assembly_grammar.h [new file with mode: 0644]
source/binary.cpp
source/text.cpp
source/text_handler.cpp
source/text_handler.h
test/UnitSPIRV.h

index ce2c366..af4762c 100644 (file)
@@ -103,6 +103,7 @@ include_directories(
 set(SPIRV_SOURCES
   ${CMAKE_CURRENT_SOURCE_DIR}/include/libspirv/libspirv.h
   ${CMAKE_CURRENT_SOURCE_DIR}/include/util/bitutils.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.h
   ${CMAKE_CURRENT_SOURCE_DIR}/source/binary.h
   ${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.h
   ${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.h
@@ -113,6 +114,7 @@ set(SPIRV_SOURCES
   ${CMAKE_CURRENT_SOURCE_DIR}/source/text.h
   ${CMAKE_CURRENT_SOURCE_DIR}/source/text_handler.h
   ${CMAKE_CURRENT_SOURCE_DIR}/source/validate.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/source/binary.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.cpp
diff --git a/source/assembly_grammar.cpp b/source/assembly_grammar.cpp
new file mode 100644 (file)
index 0000000..e956442
--- /dev/null
@@ -0,0 +1,137 @@
+// 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 "assembly_grammar.h"
+
+#include <algorithm>
+#include <cstring>
+
+#include "ext_inst.h"
+#include "opcode.h"
+
+namespace {
+
+/// @brief Parses a mask expression string for the given operand type.
+///
+/// A mask expression is a sequence of one or more terms separated by '|',
+/// where each term a named enum value for the given type.  No whitespace
+/// is permitted.
+///
+/// On success, the value is written to pValue.
+///
+/// @param[in] operandTable operand lookup table
+/// @param[in] type of the operand
+/// @param[in] textValue word of text to be parsed
+/// @param[out] pValue where the resulting value is written
+///
+/// @return result code
+spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable,
+                                     const spv_operand_type_t type,
+                                     const char *textValue, uint32_t *pValue) {
+  if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
+  size_t text_length = strlen(textValue);
+  if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
+  const char *text_end = textValue + text_length;
+
+  // We only support mask expressions in ASCII, so the separator value is a
+  // char.
+  const char separator = '|';
+
+  // Accumulate the result by interpreting one word at a time, scanning
+  // from left to right.
+  uint32_t value = 0;
+  const char *begin = textValue;  // The left end of the current word.
+  const char *end = nullptr;  // One character past the end of the current word.
+  do {
+    end = std::find(begin, text_end, separator);
+
+    spv_operand_desc entry = nullptr;
+    if (spvOperandTableNameLookup(operandTable, type, begin, end - begin,
+                                  &entry)) {
+      return SPV_ERROR_INVALID_TEXT;
+    }
+    value |= entry->value;
+
+    // Advance to the next word by skipping over the separator.
+    begin = end + 1;
+  } while (end != text_end);
+
+  *pValue = value;
+  return SPV_SUCCESS;
+}
+}  // anonymous namespace
+
+namespace libspirv {
+
+bool AssemblyGrammar::isValid() const {
+  return operandTable_ && opcodeTable_ && extInstTable_;
+}
+
+spv_result_t AssemblyGrammar::lookupOpcode(const char *name,
+                                           spv_opcode_desc *desc) const {
+  return spvOpcodeTableNameLookup(opcodeTable_, name, desc);
+}
+
+spv_result_t AssemblyGrammar::lookupOpcode(Op opcode,
+                                           spv_opcode_desc *desc) const {
+  return spvOpcodeTableValueLookup(opcodeTable_, opcode, desc);
+}
+
+spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
+                                            const char *name, size_t name_len,
+                                            spv_operand_desc *desc) const {
+  return spvOperandTableNameLookup(operandTable_, type, name, name_len, desc);
+}
+
+spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
+                                            uint32_t operand,
+                                            spv_operand_desc *desc) const {
+  return spvOperandTableValueLookup(operandTable_, type, operand, desc);
+}
+
+spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
+                                               const char *textValue,
+                                               uint32_t *pValue) const {
+  return spvTextParseMaskOperand(operandTable_, type, textValue, pValue);
+}
+spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
+                                            const char *textValue,
+                                            spv_ext_inst_desc *extInst) const {
+  return spvExtInstTableNameLookup(extInstTable_, type, textValue, extInst);
+}
+
+spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
+                                            uint32_t firstWord,
+                                            spv_ext_inst_desc *extInst) const {
+  return spvExtInstTableValueLookup(extInstTable_, type, firstWord, extInst);
+}
+
+void AssemblyGrammar::prependOperandTypesForMask(
+    const spv_operand_type_t type, const uint32_t mask,
+    spv_operand_pattern_t *pattern) const {
+  spvPrependOperandTypesForMask(operandTable_, type, mask, pattern);
+}
+}  // namespace libspirv
diff --git a/source/assembly_grammar.h b/source/assembly_grammar.h
new file mode 100644 (file)
index 0000000..9cb9c4a
--- /dev/null
@@ -0,0 +1,114 @@
+// 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.
+
+#ifndef _LIBSPIRV_UTIL_ASSEMBLY_GRAMMAR_H_
+#define _LIBSPIRV_UTIL_ASSEMBLY_GRAMMAR_H_
+
+#include <libspirv/libspirv.h>
+#include "operand.h"
+
+namespace libspirv {
+
+// Encapsulates the grammar to use for SPIR-V assembly.
+// Contains methods to query for valid instructions and operands.
+class AssemblyGrammar {
+ public:
+  AssemblyGrammar(const spv_operand_table operand_table,
+                  const spv_opcode_table opcode_table,
+                  const spv_ext_inst_table ext_inst_table)
+      : operandTable_(operand_table),
+        opcodeTable_(opcode_table),
+        extInstTable_(ext_inst_table) {}
+
+  // Returns true if the compilation_data has been initialized with valid data.
+  bool isValid() const;
+
+  // Fills in the desc parameter with the information about the opcode
+  // of the given name. Returns SPV_SUCCESS if the opcode was found, and
+  // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
+  spv_result_t lookupOpcode(const char *name, spv_opcode_desc *desc) const;
+
+  // Fills in the desc parameter with the information about the opcode
+  // of the valid. Returns SPV_SUCCESS if the opcode was found, and
+  // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
+  spv_result_t lookupOpcode(Op opcode, spv_opcode_desc *desc) const;
+
+  // Fills in the desc parameter with the information about the given
+  // operand. Returns SPV_SUCCESS if the operand was found, and
+  // SPV_ERROR_INVALID_LOOKUP otherwise.
+  spv_result_t lookupOperand(spv_operand_type_t type, const char *name,
+                             size_t name_len, spv_operand_desc *desc) const;
+
+  // Fills in the desc parameter with the information about the given
+  // operand. Returns SPV_SUCCESS if the operand was found, and
+  // SPV_ERROR_INVALID_LOOKUP otherwise.
+  spv_result_t lookupOperand(spv_operand_type_t type, uint32_t operand,
+                             spv_operand_desc *desc) const;
+
+  // Parses a mask expression string for the given operand type.
+  //
+  // A mask expression is a sequence of one or more terms separated by '|',
+  // where each term is a named enum value for a given type. No whitespace
+  // is permitted.
+  //
+  // On success, the value is written to pValue, and SPV_SUCCESS is returend.
+  // The operand type is defined by the type parameter, and the text to be
+  // parsed is defined by the textValue parameter.
+  spv_result_t parseMaskOperand(const spv_operand_type_t type,
+                                const char *textValue, uint32_t *pValue) const;
+
+  // Writes the extended operand with the given type and text to the *extInst
+  // parameter.
+  // Returns SPV_SUCCESS if the value could be found.
+  spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char *textValue,
+                             spv_ext_inst_desc *extInst) const;
+
+  // Writes the extended operand with the given type and first encoded word
+  // to the *extInst parameter.
+  // Returns SPV_SUCCESS if the value could be found.
+  spv_result_t lookupExtInst(spv_ext_inst_type_t type, uint32_t firstWord,
+                             spv_ext_inst_desc *extInst) const;
+
+  // Inserts the operands expected after the given typed mask onto the front
+  // of the given pattern.
+  //
+  // Each set bit in the mask represents zero or more operand types that should
+  // be prepended onto the pattern. Opearnds for a less significant bit always
+  // appear before operands for a more significatn bit.
+  //
+  // If a set bit is unknown, then we assume it has no operands.
+  void prependOperandTypesForMask(const spv_operand_type_t type,
+                                  const uint32_t mask,
+                                  spv_operand_pattern_t *pattern) const;
+
+ private:
+  const spv_operand_table operandTable_;
+  const spv_opcode_table opcodeTable_;
+  const spv_ext_inst_table extInstTable_;
+};
+}
+#endif  // _LIBSPIRV_UTIL_ASSEMBLY_GRAMMAR_H_
+
index 9df80d3..de29106 100644 (file)
 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
 
-#include <libspirv/libspirv.h>
 #include "binary.h"
+
+#include <cassert>
+#include <cstring>
+#include <sstream>
+#include <unordered_map>
+
+#include <libspirv/libspirv.h>
+#include "assembly_grammar.h"
 #include "diagnostic.h"
 #include "ext_inst.h"
 #include "instruction.h"
 #include "operand.h"
 #include "text_handler.h"
 
-#include <assert.h>
-#include <string.h>
-
-#include <sstream>
-#include <unordered_map>
-
 // Binary API
 
 enum {
index 388ebdb..8853ed2 100644 (file)
@@ -38,6 +38,7 @@
 #include <unordered_map>
 #include <vector>
 
+#include "assembly_grammar.h"
 #include "binary.h"
 #include "diagnostic.h"
 #include "ext_inst.h"
index 88f7827..803d914 100644 (file)
 
 #include "text_handler.h"
 
-#include <algorithm>
 #include <cassert>
 #include <cstdlib>
 #include <cstring>
 #include <tuple>
 
+#include "assembly_grammar.h"
 #include "binary.h"
 #include "ext_inst.h"
 #include "instruction.h"
@@ -163,110 +163,12 @@ bool startsWithOp(spv_text text, spv_position position) {
   return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
 }
 
-/// @brief Parses a mask expression string for the given operand type.
-///
-/// A mask expression is a sequence of one or more terms separated by '|',
-/// where each term a named enum value for the given type.  No whitespace
-/// is permitted.
-///
-/// On success, the value is written to pValue.
-///
-/// @param[in] operandTable operand lookup table
-/// @param[in] type of the operand
-/// @param[in] textValue word of text to be parsed
-/// @param[out] pValue where the resulting value is written
-///
-/// @return result code
-spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable,
-                                     const spv_operand_type_t type,
-                                     const char *textValue, uint32_t *pValue) {
-  if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
-  size_t text_length = strlen(textValue);
-  if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
-  const char *text_end = textValue + text_length;
-
-  // We only support mask expressions in ASCII, so the separator value is a
-  // char.
-  const char separator = '|';
-
-  // Accumulate the result by interpreting one word at a time, scanning
-  // from left to right.
-  uint32_t value = 0;
-  const char *begin = textValue;  // The left end of the current word.
-  const char *end = nullptr;  // One character past the end of the current word.
-  do {
-    end = std::find(begin, text_end, separator);
-
-    spv_operand_desc entry = nullptr;
-    if (spvOperandTableNameLookup(operandTable, type, begin, end - begin,
-                                  &entry)) {
-      return SPV_ERROR_INVALID_TEXT;
-    }
-    value |= entry->value;
-
-    // Advance to the next word by skipping over the separator.
-    begin = end + 1;
-  } while (end != text_end);
-
-  *pValue = value;
-  return SPV_SUCCESS;
-}
-
 }  // anonymous namespace
 
 namespace libspirv {
 
 const IdType kUnknownType = {0, false, IdTypeClass::kBottom};
 
-bool AssemblyGrammar::isValid() const {
-  return operandTable_ && opcodeTable_ && extInstTable_;
-}
-
-spv_result_t AssemblyGrammar::lookupOpcode(const char *name,
-                                           spv_opcode_desc *desc) const {
-  return spvOpcodeTableNameLookup(opcodeTable_, name, desc);
-}
-
-spv_result_t AssemblyGrammar::lookupOpcode(Op opcode,
-                                           spv_opcode_desc *desc) const {
-  return spvOpcodeTableValueLookup(opcodeTable_, opcode, desc);
-}
-
-spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
-                                            const char *name, size_t name_len,
-                                            spv_operand_desc *desc) const {
-  return spvOperandTableNameLookup(operandTable_, type, name, name_len, desc);
-}
-
-spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
-                                            uint32_t operand,
-                                            spv_operand_desc *desc) const {
-  return spvOperandTableValueLookup(operandTable_, type, operand, desc);
-}
-
-spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
-                                               const char *textValue,
-                                               uint32_t *pValue) const {
-  return spvTextParseMaskOperand(operandTable_, type, textValue, pValue);
-}
-spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
-                                            const char *textValue,
-                                            spv_ext_inst_desc *extInst) const {
-  return spvExtInstTableNameLookup(extInstTable_, type, textValue, extInst);
-}
-
-spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
-                                            uint32_t firstWord,
-                                            spv_ext_inst_desc *extInst) const {
-  return spvExtInstTableValueLookup(extInstTable_, type, firstWord, extInst);
-}
-
-void AssemblyGrammar::prependOperandTypesForMask(
-    const spv_operand_type_t type, const uint32_t mask,
-    spv_operand_pattern_t *pattern) const {
-  spvPrependOperandTypesForMask(operandTable_, type, mask, pattern);
-}
-
 // TODO(dneto): Reorder AssemblyContext definitions to match declaration order.
 
 // This represents all of the data that is only valid for the duration of
index adbde16..9d9c125 100644 (file)
@@ -36,7 +36,6 @@
 #include <libspirv/libspirv.h>
 #include "diagnostic.h"
 #include "instruction.h"
-#include "operand.h"
 #include "text.h"
 
 namespace libspirv {
@@ -102,86 +101,6 @@ inline int assumedBitWidth(const IdType& type) {
   return 0;
 }
 
-// Encapsulates the grammar to use for SPIR-V assembly.
-// Contains methods to query for valid instructions and operands.
-class AssemblyGrammar {
- public:
-  AssemblyGrammar(const spv_operand_table operand_table,
-                  const spv_opcode_table opcode_table,
-                  const spv_ext_inst_table ext_inst_table)
-      : operandTable_(operand_table),
-        opcodeTable_(opcode_table),
-        extInstTable_(ext_inst_table) {}
-
-  // Returns true if the compilation_data has been initialized with valid data.
-  bool isValid() const;
-
-  // Fills in the desc parameter with the information about the opcode
-  // of the given name. Returns SPV_SUCCESS if the opcode was found, and
-  // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
-  spv_result_t lookupOpcode(const char *name, spv_opcode_desc *desc) const;
-
-  // Fills in the desc parameter with the information about the opcode
-  // of the valid. Returns SPV_SUCCESS if the opcode was found, and
-  // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
-  spv_result_t lookupOpcode(Op opcode, spv_opcode_desc *desc) const;
-
-  // Fills in the desc parameter with the information about the given
-  // operand. Returns SPV_SUCCESS if the operand was found, and
-  // SPV_ERROR_INVALID_LOOKUP otherwise.
-  spv_result_t lookupOperand(spv_operand_type_t type, const char *name,
-                             size_t name_len, spv_operand_desc *desc) const;
-
-  // Fills in the desc parameter with the information about the given
-  // operand. Returns SPV_SUCCESS if the operand was found, and
-  // SPV_ERROR_INVALID_LOOKUP otherwise.
-  spv_result_t lookupOperand(spv_operand_type_t type, uint32_t operand,
-                             spv_operand_desc *desc) const;
-
-  // Parses a mask expression string for the given operand type.
-  //
-  // A mask expression is a sequence of one or more terms separated by '|',
-  // where each term is a named enum value for a given type. No whitespace
-  // is permitted.
-  //
-  // On success, the value is written to pValue, and SPV_SUCCESS is returend.
-  // The operand type is defined by the type parameter, and the text to be
-  // parsed is defined by the textValue parameter.
-  spv_result_t parseMaskOperand(const spv_operand_type_t type,
-                                const char *textValue, uint32_t *pValue) const;
-
-
-  // Writes the extended operand with the given type and text to the *extInst
-  // parameter.
-  // Returns SPV_SUCCESS if the value could be found.
-  spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char *textValue,
-                             spv_ext_inst_desc *extInst) const;
-
-  // Writes the extended operand with the given type and first encoded word
-  // to the *extInst parameter.
-  // Returns SPV_SUCCESS if the value could be found.
-  spv_result_t lookupExtInst(spv_ext_inst_type_t type,
-                             uint32_t firstWord,
-                             spv_ext_inst_desc *extInst) const;
-
-  // Inserts the operands expected after the given typed mask onto the front
-  // of the given pattern.
-  //
-  // Each set bit in the mask represents zero or more operand types that should
-  // be prepended onto the pattern. Opearnds for a less significant bit always
-  // appear before operands for a more significatn bit.
-  //
-  // If a set bit is unknown, then we assume it has no operands.
-  void prependOperandTypesForMask(const spv_operand_type_t type,
-                                  const uint32_t mask,
-                                  spv_operand_pattern_t *pattern) const;
-
- private:
-  const spv_operand_table operandTable_;
-  const spv_opcode_table opcodeTable_;
-  const spv_ext_inst_table extInstTable_;
-};
-
 // Encapsulates the data used during the assembly of a SPIR-V module.
 class AssemblyContext {
  public:
index aca53f8..4686d25 100644 (file)
@@ -28,6 +28,7 @@
 #define _CODEPLAY_UNITBIL_H_
 
 #include <libspirv/libspirv.h>
+#include "../source/assembly_grammar.h"
 #include "../source/binary.h"
 #include "../source/diagnostic.h"
 #include "../source/opcode.h"