blob: 98c8a3d21de1ff7390c9ba69c7480bf8c76d9b2e [file] [log] [blame]
David Netofcc7d582015-10-27 15:31:10 -04001// 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 Neto0ca6b592015-10-30 16:06:15 -040030#include <cassert>
David Netofcc7d582015-10-27 15:31:10 -040031#include <cstring>
32
33#include "ext_inst.h"
34#include "opcode.h"
David Neto0ca6b592015-10-30 16:06:15 -040035#include "operand.h"
David Netofcc7d582015-10-27 15:31:10 -040036
37namespace {
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
53spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable,
54 const spv_operand_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -050055 const char* textValue, uint32_t* pValue) {
David Netofcc7d582015-10-27 15:31:10 -040056 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 Zhang1a0334e2015-11-02 09:41:20 -050059 const char* text_end = textValue + text_length;
David Netofcc7d582015-10-27 15:31:10 -040060
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 Zhang1a0334e2015-11-02 09:41:20 -050068 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 Netofcc7d582015-10-27 15:31:10 -040070 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 Neto0ca6b592015-10-30 16:06:15 -040087
88// Returns the operand table.
89spv_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.
97spv_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.
105spv_ext_inst_table GetDefaultExtInstTable() {
106 spv_ext_inst_table result = nullptr;
107 spvExtInstTableGet(&result);
108 assert(result);
109 return result;
110}
111
David Netofcc7d582015-10-27 15:31:10 -0400112} // anonymous namespace
113
114namespace libspirv {
115
David Neto0ca6b592015-10-30 16:06:15 -0400116AssemblyGrammar::AssemblyGrammar()
117 : AssemblyGrammar(GetDefaultOperandTable(), GetDefaultOpcodeTable(),
118 GetDefaultExtInstTable()) {
119 assert(isValid());
120}
121
David Netofcc7d582015-10-27 15:31:10 -0400122bool AssemblyGrammar::isValid() const {
123 return operandTable_ && opcodeTable_ && extInstTable_;
124}
125
Lei Zhang1a0334e2015-11-02 09:41:20 -0500126spv_result_t AssemblyGrammar::lookupOpcode(const char* name,
127 spv_opcode_desc* desc) const {
David Netofcc7d582015-10-27 15:31:10 -0400128 return spvOpcodeTableNameLookup(opcodeTable_, name, desc);
129}
130
Lei Zhangb36e7042015-10-28 13:40:52 -0400131spv_result_t AssemblyGrammar::lookupOpcode(SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500132 spv_opcode_desc* desc) const {
David Netofcc7d582015-10-27 15:31:10 -0400133 return spvOpcodeTableValueLookup(opcodeTable_, opcode, desc);
134}
135
136spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500137 const char* name, size_t name_len,
138 spv_operand_desc* desc) const {
David Netofcc7d582015-10-27 15:31:10 -0400139 return spvOperandTableNameLookup(operandTable_, type, name, name_len, desc);
140}
141
142spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
143 uint32_t operand,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500144 spv_operand_desc* desc) const {
David Netofcc7d582015-10-27 15:31:10 -0400145 return spvOperandTableValueLookup(operandTable_, type, operand, desc);
146}
147
148spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500149 const char* textValue,
150 uint32_t* pValue) const {
David Netofcc7d582015-10-27 15:31:10 -0400151 return spvTextParseMaskOperand(operandTable_, type, textValue, pValue);
152}
153spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500154 const char* textValue,
155 spv_ext_inst_desc* extInst) const {
David Netofcc7d582015-10-27 15:31:10 -0400156 return spvExtInstTableNameLookup(extInstTable_, type, textValue, extInst);
157}
158
159spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
160 uint32_t firstWord,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500161 spv_ext_inst_desc* extInst) const {
David Netofcc7d582015-10-27 15:31:10 -0400162 return spvExtInstTableValueLookup(extInstTable_, type, firstWord, extInst);
163}
164
165void AssemblyGrammar::prependOperandTypesForMask(
166 const spv_operand_type_t type, const uint32_t mask,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500167 spv_operand_pattern_t* pattern) const {
David Netofcc7d582015-10-27 15:31:10 -0400168 spvPrependOperandTypesForMask(operandTable_, type, mask, pattern);
169}
170} // namespace libspirv