David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 1 | // Copyright (c) 2015 The Khronos Group Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | // copy of this software and/or associated documentation files (the |
| 5 | // "Materials"), to deal in the Materials without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Materials, and to |
| 8 | // permit persons to whom the Materials are furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included |
| 12 | // in all copies or substantial portions of the Materials. |
| 13 | // |
| 14 | // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS |
| 15 | // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS |
| 16 | // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT |
| 17 | // https://www.khronos.org/registry/ |
| 18 | // |
| 19 | // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 26 | |
| 27 | #include "assembly_grammar.h" |
| 28 | |
| 29 | #include <algorithm> |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame^] | 30 | #include <cassert> |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 31 | #include <cstring> |
| 32 | |
| 33 | #include "ext_inst.h" |
| 34 | #include "opcode.h" |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame^] | 35 | #include "operand.h" |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 36 | |
| 37 | namespace { |
| 38 | |
| 39 | /// @brief Parses a mask expression string for the given operand type. |
| 40 | /// |
| 41 | /// A mask expression is a sequence of one or more terms separated by '|', |
| 42 | /// where each term a named enum value for the given type. No whitespace |
| 43 | /// is permitted. |
| 44 | /// |
| 45 | /// On success, the value is written to pValue. |
| 46 | /// |
| 47 | /// @param[in] operandTable operand lookup table |
| 48 | /// @param[in] type of the operand |
| 49 | /// @param[in] textValue word of text to be parsed |
| 50 | /// @param[out] pValue where the resulting value is written |
| 51 | /// |
| 52 | /// @return result code |
| 53 | spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable, |
| 54 | const spv_operand_type_t type, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 55 | const char* textValue, uint32_t* pValue) { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 56 | if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT; |
| 57 | size_t text_length = strlen(textValue); |
| 58 | if (text_length == 0) return SPV_ERROR_INVALID_TEXT; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 59 | const char* text_end = textValue + text_length; |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 60 | |
| 61 | // We only support mask expressions in ASCII, so the separator value is a |
| 62 | // char. |
| 63 | const char separator = '|'; |
| 64 | |
| 65 | // Accumulate the result by interpreting one word at a time, scanning |
| 66 | // from left to right. |
| 67 | uint32_t value = 0; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 68 | const char* begin = textValue; // The left end of the current word. |
| 69 | const char* end = nullptr; // One character past the end of the current word. |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 70 | do { |
| 71 | end = std::find(begin, text_end, separator); |
| 72 | |
| 73 | spv_operand_desc entry = nullptr; |
| 74 | if (spvOperandTableNameLookup(operandTable, type, begin, end - begin, |
| 75 | &entry)) { |
| 76 | return SPV_ERROR_INVALID_TEXT; |
| 77 | } |
| 78 | value |= entry->value; |
| 79 | |
| 80 | // Advance to the next word by skipping over the separator. |
| 81 | begin = end + 1; |
| 82 | } while (end != text_end); |
| 83 | |
| 84 | *pValue = value; |
| 85 | return SPV_SUCCESS; |
| 86 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame^] | 87 | |
| 88 | // Returns the operand table. |
| 89 | spv_operand_table GetDefaultOperandTable() { |
| 90 | spv_operand_table result = nullptr; |
| 91 | spvOperandTableGet(&result); |
| 92 | assert(result); |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | // Returns the opcode table. |
| 97 | spv_opcode_table GetDefaultOpcodeTable() { |
| 98 | spv_opcode_table result = nullptr; |
| 99 | spvOpcodeTableGet(&result); |
| 100 | assert(result); |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | // Returns the extended instruction table. |
| 105 | spv_ext_inst_table GetDefaultExtInstTable() { |
| 106 | spv_ext_inst_table result = nullptr; |
| 107 | spvExtInstTableGet(&result); |
| 108 | assert(result); |
| 109 | return result; |
| 110 | } |
| 111 | |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 112 | } // anonymous namespace |
| 113 | |
| 114 | namespace libspirv { |
| 115 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame^] | 116 | AssemblyGrammar::AssemblyGrammar() |
| 117 | : AssemblyGrammar(GetDefaultOperandTable(), GetDefaultOpcodeTable(), |
| 118 | GetDefaultExtInstTable()) { |
| 119 | assert(isValid()); |
| 120 | } |
| 121 | |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 122 | bool AssemblyGrammar::isValid() const { |
| 123 | return operandTable_ && opcodeTable_ && extInstTable_; |
| 124 | } |
| 125 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 126 | spv_result_t AssemblyGrammar::lookupOpcode(const char* name, |
| 127 | spv_opcode_desc* desc) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 128 | return spvOpcodeTableNameLookup(opcodeTable_, name, desc); |
| 129 | } |
| 130 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 131 | spv_result_t AssemblyGrammar::lookupOpcode(SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 132 | spv_opcode_desc* desc) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 133 | return spvOpcodeTableValueLookup(opcodeTable_, opcode, desc); |
| 134 | } |
| 135 | |
| 136 | spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 137 | const char* name, size_t name_len, |
| 138 | spv_operand_desc* desc) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 139 | return spvOperandTableNameLookup(operandTable_, type, name, name_len, desc); |
| 140 | } |
| 141 | |
| 142 | spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type, |
| 143 | uint32_t operand, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 144 | spv_operand_desc* desc) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 145 | return spvOperandTableValueLookup(operandTable_, type, operand, desc); |
| 146 | } |
| 147 | |
| 148 | spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 149 | const char* textValue, |
| 150 | uint32_t* pValue) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 151 | return spvTextParseMaskOperand(operandTable_, type, textValue, pValue); |
| 152 | } |
| 153 | spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 154 | const char* textValue, |
| 155 | spv_ext_inst_desc* extInst) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 156 | return spvExtInstTableNameLookup(extInstTable_, type, textValue, extInst); |
| 157 | } |
| 158 | |
| 159 | spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type, |
| 160 | uint32_t firstWord, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 161 | spv_ext_inst_desc* extInst) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 162 | return spvExtInstTableValueLookup(extInstTable_, type, firstWord, extInst); |
| 163 | } |
| 164 | |
| 165 | void AssemblyGrammar::prependOperandTypesForMask( |
| 166 | const spv_operand_type_t type, const uint32_t mask, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 167 | spv_operand_pattern_t* pattern) const { |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 168 | spvPrependOperandTypesForMask(operandTable_, type, mask, pattern); |
| 169 | } |
| 170 | } // namespace libspirv |