text.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/text.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <cctype>
  18. #include <cstdio>
  19. #include <cstdlib>
  20. #include <cstring>
  21. #include <memory>
  22. #include <set>
  23. #include <sstream>
  24. #include <string>
  25. #include <unordered_map>
  26. #include <utility>
  27. #include <vector>
  28. #include "source/assembly_grammar.h"
  29. #include "source/binary.h"
  30. #include "source/diagnostic.h"
  31. #include "source/ext_inst.h"
  32. #include "source/instruction.h"
  33. #include "source/opcode.h"
  34. #include "source/operand.h"
  35. #include "source/spirv_constant.h"
  36. #include "source/spirv_target_env.h"
  37. #include "source/table.h"
  38. #include "source/text_handler.h"
  39. #include "source/util/bitutils.h"
  40. #include "source/util/parse_number.h"
  41. #include "spirv-tools/libspirv.h"
  42. bool spvIsValidIDCharacter(const char value) {
  43. return value == '_' || 0 != ::isalnum(value);
  44. }
  45. // Returns true if the given string represents a valid ID name.
  46. bool spvIsValidID(const char* textValue) {
  47. const char* c = textValue;
  48. for (; *c != '\0'; ++c) {
  49. if (!spvIsValidIDCharacter(*c)) {
  50. return false;
  51. }
  52. }
  53. // If the string was empty, then the ID also is not valid.
  54. return c != textValue;
  55. }
  56. // Text API
  57. spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
  58. bool isSigned = false;
  59. int numPeriods = 0;
  60. bool isString = false;
  61. const size_t len = strlen(textValue);
  62. if (len == 0) return SPV_FAILED_MATCH;
  63. for (uint64_t index = 0; index < len; ++index) {
  64. switch (textValue[index]) {
  65. case '0':
  66. case '1':
  67. case '2':
  68. case '3':
  69. case '4':
  70. case '5':
  71. case '6':
  72. case '7':
  73. case '8':
  74. case '9':
  75. break;
  76. case '.':
  77. numPeriods++;
  78. break;
  79. case '-':
  80. if (index == 0) {
  81. isSigned = true;
  82. } else {
  83. isString = true;
  84. }
  85. break;
  86. default:
  87. isString = true;
  88. index = len; // break out of the loop too.
  89. break;
  90. }
  91. }
  92. pLiteral->type = spv_literal_type_t(99);
  93. if (isString || numPeriods > 1 || (isSigned && len == 1)) {
  94. if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
  95. return SPV_FAILED_MATCH;
  96. bool escaping = false;
  97. for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
  98. if ((*val == '\\') && (!escaping)) {
  99. escaping = true;
  100. } else {
  101. // Have to save space for the null-terminator
  102. if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX)
  103. return SPV_ERROR_OUT_OF_MEMORY;
  104. pLiteral->str.push_back(*val);
  105. escaping = false;
  106. }
  107. }
  108. pLiteral->type = SPV_LITERAL_TYPE_STRING;
  109. } else if (numPeriods == 1) {
  110. double d = std::strtod(textValue, nullptr);
  111. float f = (float)d;
  112. if (d == (double)f) {
  113. pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
  114. pLiteral->value.f = f;
  115. } else {
  116. pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
  117. pLiteral->value.d = d;
  118. }
  119. } else if (isSigned) {
  120. int64_t i64 = strtoll(textValue, nullptr, 10);
  121. int32_t i32 = (int32_t)i64;
  122. if (i64 == (int64_t)i32) {
  123. pLiteral->type = SPV_LITERAL_TYPE_INT_32;
  124. pLiteral->value.i32 = i32;
  125. } else {
  126. pLiteral->type = SPV_LITERAL_TYPE_INT_64;
  127. pLiteral->value.i64 = i64;
  128. }
  129. } else {
  130. uint64_t u64 = strtoull(textValue, nullptr, 10);
  131. uint32_t u32 = (uint32_t)u64;
  132. if (u64 == (uint64_t)u32) {
  133. pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
  134. pLiteral->value.u32 = u32;
  135. } else {
  136. pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
  137. pLiteral->value.u64 = u64;
  138. }
  139. }
  140. return SPV_SUCCESS;
  141. }
  142. namespace {
  143. /// Parses an immediate integer from text, guarding against overflow. If
  144. /// successful, adds the parsed value to pInst, advances the context past it,
  145. /// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
  146. /// and returns SPV_ERROR_INVALID_TEXT.
  147. spv_result_t encodeImmediate(spvtools::AssemblyContext* context,
  148. const char* text, spv_instruction_t* pInst) {
  149. assert(*text == '!');
  150. uint32_t parse_result;
  151. if (!spvtools::utils::ParseNumber(text + 1, &parse_result)) {
  152. return context->diagnostic(SPV_ERROR_INVALID_TEXT)
  153. << "Invalid immediate integer: !" << text + 1;
  154. }
  155. context->binaryEncodeU32(parse_result, pInst);
  156. context->seekForward(static_cast<uint32_t>(strlen(text)));
  157. return SPV_SUCCESS;
  158. }
  159. } // anonymous namespace
  160. /// @brief Translate an Opcode operand to binary form
  161. ///
  162. /// @param[in] grammar the grammar to use for compilation
  163. /// @param[in, out] context the dynamic compilation info
  164. /// @param[in] type of the operand
  165. /// @param[in] textValue word of text to be parsed
  166. /// @param[out] pInst return binary Opcode
  167. /// @param[in,out] pExpectedOperands the operand types expected
  168. ///
  169. /// @return result code
  170. spv_result_t spvTextEncodeOperand(const spvtools::AssemblyGrammar& grammar,
  171. spvtools::AssemblyContext* context,
  172. const spv_operand_type_t type,
  173. const char* textValue,
  174. spv_instruction_t* pInst,
  175. spv_operand_pattern_t* pExpectedOperands) {
  176. // NOTE: Handle immediate int in the stream
  177. if ('!' == textValue[0]) {
  178. if (auto error = encodeImmediate(context, textValue, pInst)) {
  179. return error;
  180. }
  181. *pExpectedOperands =
  182. spvAlternatePatternFollowingImmediate(*pExpectedOperands);
  183. return SPV_SUCCESS;
  184. }
  185. // Optional literal operands can fail to parse. In that case use
  186. // SPV_FAILED_MATCH to avoid emitting a diagnostic. Use the following
  187. // for those situations.
  188. spv_result_t error_code_for_literals =
  189. spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
  190. switch (type) {
  191. case SPV_OPERAND_TYPE_ID:
  192. case SPV_OPERAND_TYPE_TYPE_ID:
  193. case SPV_OPERAND_TYPE_RESULT_ID:
  194. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  195. case SPV_OPERAND_TYPE_SCOPE_ID:
  196. case SPV_OPERAND_TYPE_OPTIONAL_ID: {
  197. if ('%' == textValue[0]) {
  198. textValue++;
  199. } else {
  200. return context->diagnostic() << "Expected id to start with %.";
  201. }
  202. if (!spvIsValidID(textValue)) {
  203. return context->diagnostic() << "Invalid ID " << textValue;
  204. }
  205. const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
  206. if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
  207. spvInstructionAddWord(pInst, id);
  208. // Set the extended instruction type.
  209. // The import set id is the 3rd operand of OpExtInst.
  210. if (spvIsExtendedInstruction(pInst->opcode) && pInst->words.size() == 4) {
  211. auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]);
  212. if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) {
  213. return context->diagnostic()
  214. << "Invalid extended instruction import Id "
  215. << pInst->words[2];
  216. }
  217. pInst->extInstType = ext_inst_type;
  218. }
  219. } break;
  220. case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
  221. // The assembler accepts the symbolic name for an extended instruction,
  222. // and emits its corresponding number.
  223. spv_ext_inst_desc extInst;
  224. if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst) ==
  225. SPV_SUCCESS) {
  226. // if we know about this extended instruction, push the numeric value
  227. spvInstructionAddWord(pInst, extInst->ext_inst);
  228. // Prepare to parse the operands for the extended instructions.
  229. spvPushOperandTypes(extInst->operandTypes, pExpectedOperands);
  230. } else {
  231. // if we don't know this extended instruction and the set isn't
  232. // non-semantic, we cannot process further
  233. if (!spvExtInstIsNonSemantic(pInst->extInstType)) {
  234. return context->diagnostic()
  235. << "Invalid extended instruction name '" << textValue << "'.";
  236. } else {
  237. // for non-semantic instruction sets, as long as the text name is an
  238. // integer value we can encode it since we know the form of all such
  239. // extended instructions
  240. spv_literal_t extInstValue;
  241. if (spvTextToLiteral(textValue, &extInstValue) ||
  242. extInstValue.type != SPV_LITERAL_TYPE_UINT_32) {
  243. return context->diagnostic()
  244. << "Couldn't translate unknown extended instruction name '"
  245. << textValue << "' to unsigned integer.";
  246. }
  247. spvInstructionAddWord(pInst, extInstValue.value.u32);
  248. // opcode contains an unknown number of IDs.
  249. pExpectedOperands->push_back(SPV_OPERAND_TYPE_VARIABLE_ID);
  250. }
  251. }
  252. } break;
  253. case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
  254. // The assembler accepts the symbolic name for the opcode, but without
  255. // the "Op" prefix. For example, "IAdd" is accepted. The number
  256. // of the opcode is emitted.
  257. spv::Op opcode;
  258. if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
  259. return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
  260. << " '" << textValue << "'.";
  261. }
  262. spv_opcode_desc opcodeEntry = nullptr;
  263. if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
  264. return context->diagnostic(SPV_ERROR_INTERNAL)
  265. << "OpSpecConstant opcode table out of sync";
  266. }
  267. spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
  268. // Prepare to parse the operands for the opcode. Except skip the
  269. // type Id and result Id, since they've already been processed.
  270. assert(opcodeEntry->hasType);
  271. assert(opcodeEntry->hasResult);
  272. assert(opcodeEntry->numTypes >= 2);
  273. spvPushOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
  274. } break;
  275. case SPV_OPERAND_TYPE_LITERAL_INTEGER:
  276. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
  277. // The current operand is an *unsigned* 32-bit integer.
  278. // That's just how the grammar works.
  279. spvtools::IdType expected_type = {
  280. 32, false, spvtools::IdTypeClass::kScalarIntegerType};
  281. if (auto error = context->binaryEncodeNumericLiteral(
  282. textValue, error_code_for_literals, expected_type, pInst)) {
  283. return error;
  284. }
  285. } break;
  286. case SPV_OPERAND_TYPE_LITERAL_FLOAT: {
  287. // The current operand is a 32-bit float.
  288. // That's just how the grammar works.
  289. spvtools::IdType expected_type = {
  290. 32, false, spvtools::IdTypeClass::kScalarFloatType};
  291. if (auto error = context->binaryEncodeNumericLiteral(
  292. textValue, error_code_for_literals, expected_type, pInst)) {
  293. return error;
  294. }
  295. } break;
  296. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
  297. // This is a context-independent literal number which can be a 32-bit
  298. // number of floating point value.
  299. if (auto error = context->binaryEncodeNumericLiteral(
  300. textValue, error_code_for_literals, spvtools::kUnknownType,
  301. pInst)) {
  302. return error;
  303. }
  304. break;
  305. case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
  306. case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
  307. spvtools::IdType expected_type = spvtools::kUnknownType;
  308. // The encoding for OpConstant, OpSpecConstant and OpSwitch all
  309. // depend on either their own result-id or the result-id of
  310. // one of their parameters.
  311. if (spv::Op::OpConstant == pInst->opcode ||
  312. spv::Op::OpSpecConstant == pInst->opcode) {
  313. // The type of the literal is determined by the type Id of the
  314. // instruction.
  315. expected_type =
  316. context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
  317. if (!spvtools::isScalarFloating(expected_type) &&
  318. !spvtools::isScalarIntegral(expected_type)) {
  319. spv_opcode_desc d;
  320. const char* opcode_name = "opcode";
  321. if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
  322. opcode_name = d->name;
  323. }
  324. return context->diagnostic()
  325. << "Type for " << opcode_name
  326. << " must be a scalar floating point or integer type";
  327. }
  328. } else if (pInst->opcode == spv::Op::OpSwitch) {
  329. // The type of the literal is the same as the type of the selector.
  330. expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
  331. if (!spvtools::isScalarIntegral(expected_type)) {
  332. return context->diagnostic()
  333. << "The selector operand for OpSwitch must be the result"
  334. " of an instruction that generates an integer scalar";
  335. }
  336. }
  337. if (auto error = context->binaryEncodeNumericLiteral(
  338. textValue, error_code_for_literals, expected_type, pInst)) {
  339. return error;
  340. }
  341. } break;
  342. case SPV_OPERAND_TYPE_LITERAL_STRING:
  343. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
  344. spv_literal_t literal = {};
  345. spv_result_t error = spvTextToLiteral(textValue, &literal);
  346. if (error != SPV_SUCCESS) {
  347. if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
  348. return context->diagnostic(error_code_for_literals)
  349. << "Invalid literal string '" << textValue << "'.";
  350. }
  351. if (literal.type != SPV_LITERAL_TYPE_STRING) {
  352. return context->diagnostic()
  353. << "Expected literal string, found literal number '" << textValue
  354. << "'.";
  355. }
  356. // NOTE: Special case for extended instruction library import
  357. if (spv::Op::OpExtInstImport == pInst->opcode) {
  358. const spv_ext_inst_type_t ext_inst_type =
  359. spvExtInstImportTypeGet(literal.str.c_str());
  360. if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
  361. return context->diagnostic()
  362. << "Invalid extended instruction import '" << literal.str
  363. << "'";
  364. }
  365. if ((error = context->recordIdAsExtInstImport(pInst->words[1],
  366. ext_inst_type)))
  367. return error;
  368. }
  369. if (context->binaryEncodeString(literal.str.c_str(), pInst))
  370. return SPV_ERROR_INVALID_TEXT;
  371. } break;
  372. // Masks.
  373. case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
  374. case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
  375. case SPV_OPERAND_TYPE_LOOP_CONTROL:
  376. case SPV_OPERAND_TYPE_IMAGE:
  377. case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
  378. case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
  379. case SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS:
  380. case SPV_OPERAND_TYPE_SELECTION_CONTROL:
  381. case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
  382. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
  383. case SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS:
  384. case SPV_OPERAND_TYPE_TENSOR_ADDRESSING_OPERANDS:
  385. case SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_REDUCE:
  386. case SPV_OPERAND_TYPE_OPTIONAL_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS: {
  387. uint32_t value;
  388. if (auto error = grammar.parseMaskOperand(type, textValue, &value)) {
  389. return context->diagnostic(error)
  390. << "Invalid " << spvOperandTypeStr(type) << " operand '"
  391. << textValue << "'.";
  392. }
  393. if (auto error = context->binaryEncodeU32(value, pInst)) return error;
  394. // Prepare to parse the operands for this logical operand.
  395. grammar.pushOperandTypesForMask(type, value, pExpectedOperands);
  396. } break;
  397. case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
  398. auto error = spvTextEncodeOperand(
  399. grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
  400. pInst, pExpectedOperands);
  401. if (error == SPV_FAILED_MATCH) {
  402. // It's not a literal number -- is it a literal string?
  403. error = spvTextEncodeOperand(grammar, context,
  404. SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
  405. textValue, pInst, pExpectedOperands);
  406. }
  407. if (error == SPV_FAILED_MATCH) {
  408. // It's not a literal -- is it an ID?
  409. error =
  410. spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
  411. textValue, pInst, pExpectedOperands);
  412. }
  413. if (error) {
  414. return context->diagnostic(error)
  415. << "Invalid word following !<integer>: " << textValue;
  416. }
  417. if (pExpectedOperands->empty()) {
  418. pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
  419. }
  420. } break;
  421. default: {
  422. // NOTE: All non literal operands are handled here using the operand
  423. // table.
  424. spv_operand_desc entry;
  425. if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
  426. return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
  427. << " '" << textValue << "'.";
  428. }
  429. if (context->binaryEncodeU32(entry->value, pInst)) {
  430. return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
  431. << " '" << textValue << "'.";
  432. }
  433. // Prepare to parse the operands for this logical operand.
  434. spvPushOperandTypes(entry->operandTypes, pExpectedOperands);
  435. } break;
  436. }
  437. return SPV_SUCCESS;
  438. }
  439. namespace {
  440. /// Encodes an instruction started by !<integer> at the given position in text.
  441. ///
  442. /// Puts the encoded words into *pInst. If successful, moves position past the
  443. /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
  444. /// leaves position pointing to the error in text.
  445. spv_result_t encodeInstructionStartingWithImmediate(
  446. const spvtools::AssemblyGrammar& grammar,
  447. spvtools::AssemblyContext* context, spv_instruction_t* pInst) {
  448. std::string firstWord;
  449. spv_position_t nextPosition = {};
  450. auto error = context->getWord(&firstWord, &nextPosition);
  451. if (error) return context->diagnostic(error) << "Internal Error";
  452. if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
  453. return error;
  454. }
  455. while (context->advance() != SPV_END_OF_STREAM) {
  456. // A beginning of a new instruction means we're done.
  457. if (context->isStartOfNewInst()) return SPV_SUCCESS;
  458. // Otherwise, there must be an operand that's either a literal, an ID, or
  459. // an immediate.
  460. std::string operandValue;
  461. if ((error = context->getWord(&operandValue, &nextPosition)))
  462. return context->diagnostic(error) << "Internal Error";
  463. if (operandValue == "=")
  464. return context->diagnostic() << firstWord << " not allowed before =.";
  465. // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
  466. // expanded.
  467. spv_operand_pattern_t dummyExpectedOperands;
  468. error = spvTextEncodeOperand(
  469. grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
  470. pInst, &dummyExpectedOperands);
  471. if (error) return error;
  472. context->setPosition(nextPosition);
  473. }
  474. return SPV_SUCCESS;
  475. }
  476. /// @brief Translate an instruction started by OpUnknown and the following
  477. /// operands to binary form
  478. ///
  479. /// @param[in] grammar the grammar to use for compilation
  480. /// @param[in, out] context the dynamic compilation info
  481. /// @param[out] pInst returned binary Opcode
  482. ///
  483. /// @return result code
  484. spv_result_t encodeInstructionStartingWithOpUnknown(
  485. const spvtools::AssemblyGrammar& grammar,
  486. spvtools::AssemblyContext* context, spv_instruction_t* pInst) {
  487. spv_position_t nextPosition = {};
  488. uint16_t opcode;
  489. uint16_t wordCount;
  490. // The '(' character.
  491. if (context->advance())
  492. return context->diagnostic() << "Expected '(', found end of stream.";
  493. if ('(' != context->peek()) {
  494. return context->diagnostic() << "'(' expected after OpUnknown but found '"
  495. << context->peek() << "'.";
  496. }
  497. context->seekForward(1);
  498. // The opcode enumerant.
  499. if (context->advance())
  500. return context->diagnostic()
  501. << "Expected opcode enumerant, found end of stream.";
  502. std::string opcodeString;
  503. spv_result_t error = context->getWord(&opcodeString, &nextPosition);
  504. if (error) return context->diagnostic(error) << "Internal Error";
  505. if (!spvtools::utils::ParseNumber(opcodeString.c_str(), &opcode)) {
  506. return context->diagnostic()
  507. << "Invalid opcode enumerant: \"" << opcodeString << "\".";
  508. }
  509. context->setPosition(nextPosition);
  510. // The ',' character.
  511. if (context->advance())
  512. return context->diagnostic() << "Expected ',', found end of stream.";
  513. if (',' != context->peek()) {
  514. return context->diagnostic()
  515. << "',' expected after opcode enumerant but found '"
  516. << context->peek() << "'.";
  517. }
  518. context->seekForward(1);
  519. // The number of words.
  520. if (context->advance())
  521. return context->diagnostic()
  522. << "Expected number of words, found end of stream.";
  523. std::string wordCountString;
  524. error = context->getWord(&wordCountString, &nextPosition);
  525. if (error) return context->diagnostic(error) << "Internal Error";
  526. if (!spvtools::utils::ParseNumber(wordCountString.c_str(), &wordCount)) {
  527. return context->diagnostic()
  528. << "Invalid number of words: \"" << wordCountString << "\".";
  529. }
  530. if (wordCount == 0) {
  531. return context->diagnostic() << "Number of words (which includes the "
  532. "opcode) must be greater than zero.";
  533. }
  534. context->setPosition(nextPosition);
  535. // The ')' character.
  536. if (context->advance())
  537. return context->diagnostic() << "Expected ')', found end of stream.";
  538. if (')' != context->peek()) {
  539. return context->diagnostic()
  540. << "')' expected after number of words but found '"
  541. << context->peek() << "'.";
  542. }
  543. context->seekForward(1);
  544. pInst->opcode = static_cast<spv::Op>(opcode);
  545. context->binaryEncodeU32(spvOpcodeMake(wordCount, pInst->opcode), pInst);
  546. wordCount--; // Subtract the opcode from the number of words left to read.
  547. while (wordCount-- > 0) {
  548. if (context->advance() == SPV_END_OF_STREAM) {
  549. return context->diagnostic() << "Expected " << wordCount + 1
  550. << " more operands, found end of stream.";
  551. }
  552. if (context->isStartOfNewInst()) {
  553. std::string invalid;
  554. context->getWord(&invalid, &nextPosition);
  555. return context->diagnostic()
  556. << "Unexpected start of new instruction: \"" << invalid
  557. << "\". Expected " << wordCount + 1 << " more operands";
  558. }
  559. std::string operandValue;
  560. if ((error = context->getWord(&operandValue, &nextPosition)))
  561. return context->diagnostic(error) << "Internal Error";
  562. if (operandValue == "=")
  563. return context->diagnostic() << "OpUnknown not allowed before =.";
  564. // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
  565. // expanded.
  566. spv_operand_pattern_t dummyExpectedOperands;
  567. error = spvTextEncodeOperand(
  568. grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
  569. pInst, &dummyExpectedOperands);
  570. if (error) return error;
  571. context->setPosition(nextPosition);
  572. }
  573. return SPV_SUCCESS;
  574. }
  575. /// @brief Translate single Opcode and operands to binary form
  576. ///
  577. /// @param[in] grammar the grammar to use for compilation
  578. /// @param[in, out] context the dynamic compilation info
  579. /// @param[in] text stream to translate
  580. /// @param[out] pInst returned binary Opcode
  581. /// @param[in,out] pPosition in the text stream
  582. ///
  583. /// @return result code
  584. spv_result_t spvTextEncodeOpcode(const spvtools::AssemblyGrammar& grammar,
  585. spvtools::AssemblyContext* context,
  586. spv_instruction_t* pInst) {
  587. // Check for !<integer> first.
  588. if ('!' == context->peek()) {
  589. return encodeInstructionStartingWithImmediate(grammar, context, pInst);
  590. }
  591. std::string firstWord;
  592. spv_position_t nextPosition = {};
  593. spv_result_t error = context->getWord(&firstWord, &nextPosition);
  594. if (error) return context->diagnostic() << "Internal Error";
  595. std::string opcodeName;
  596. std::string result_id;
  597. spv_position_t result_id_position = {};
  598. if (context->startsWithOp()) {
  599. opcodeName = firstWord;
  600. } else {
  601. result_id = firstWord;
  602. if ('%' != result_id.front()) {
  603. return context->diagnostic()
  604. << "Expected <opcode> or <result-id> at the beginning "
  605. "of an instruction, found '"
  606. << result_id << "'.";
  607. }
  608. result_id_position = context->position();
  609. // The '=' sign.
  610. context->setPosition(nextPosition);
  611. if (context->advance())
  612. return context->diagnostic() << "Expected '=', found end of stream.";
  613. std::string equal_sign;
  614. error = context->getWord(&equal_sign, &nextPosition);
  615. if ("=" != equal_sign)
  616. return context->diagnostic() << "'=' expected after result id but found '"
  617. << equal_sign << "'.";
  618. // The <opcode> after the '=' sign.
  619. context->setPosition(nextPosition);
  620. if (context->advance())
  621. return context->diagnostic() << "Expected opcode, found end of stream.";
  622. error = context->getWord(&opcodeName, &nextPosition);
  623. if (error) return context->diagnostic(error) << "Internal Error";
  624. if (!context->startsWithOp()) {
  625. return context->diagnostic()
  626. << "Invalid Opcode prefix '" << opcodeName << "'.";
  627. }
  628. }
  629. if (opcodeName == "OpUnknown") {
  630. if (!result_id.empty()) {
  631. return context->diagnostic()
  632. << "OpUnknown not allowed in assignment. Use an explicit result "
  633. "id operand instead.";
  634. }
  635. context->setPosition(nextPosition);
  636. return encodeInstructionStartingWithOpUnknown(grammar, context, pInst);
  637. }
  638. // NOTE: The table contains Opcode names without the "Op" prefix.
  639. const char* pInstName = opcodeName.data() + 2;
  640. spv_opcode_desc opcodeEntry;
  641. error = grammar.lookupOpcode(pInstName, &opcodeEntry);
  642. if (error) {
  643. return context->diagnostic(error)
  644. << "Invalid Opcode name '" << opcodeName << "'";
  645. }
  646. if (opcodeEntry->hasResult && result_id.empty()) {
  647. return context->diagnostic()
  648. << "Expected <result-id> at the beginning of an instruction, found '"
  649. << firstWord << "'.";
  650. }
  651. if (!opcodeEntry->hasResult && !result_id.empty()) {
  652. return context->diagnostic()
  653. << "Cannot set ID " << result_id << " because " << opcodeName
  654. << " does not produce a result ID.";
  655. }
  656. pInst->opcode = opcodeEntry->opcode;
  657. context->setPosition(nextPosition);
  658. // Reserve the first word for the instruction.
  659. spvInstructionAddWord(pInst, 0);
  660. // Maintains the ordered list of expected operand types.
  661. // For many instructions we only need the {numTypes, operandTypes}
  662. // entries in opcodeEntry. However, sometimes we need to modify
  663. // the list as we parse the operands. This occurs when an operand
  664. // has its own logical operands (such as the LocalSize operand for
  665. // ExecutionMode), or for extended instructions that may have their
  666. // own operands depending on the selected extended instruction.
  667. spv_operand_pattern_t expectedOperands;
  668. expectedOperands.reserve(opcodeEntry->numTypes);
  669. for (auto i = 0; i < opcodeEntry->numTypes; i++)
  670. expectedOperands.push_back(
  671. opcodeEntry->operandTypes[opcodeEntry->numTypes - i - 1]);
  672. while (!expectedOperands.empty()) {
  673. const spv_operand_type_t type = expectedOperands.back();
  674. expectedOperands.pop_back();
  675. // Expand optional tuples lazily.
  676. if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
  677. if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
  678. // Handle the <result-id> for value generating instructions.
  679. // We've already consumed it from the text stream. Here
  680. // we inject its words into the instruction.
  681. spv_position_t temp_pos = context->position();
  682. error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
  683. result_id.c_str(), pInst, nullptr);
  684. result_id_position = context->position();
  685. // Because we are injecting we have to reset the position afterwards.
  686. context->setPosition(temp_pos);
  687. if (error) return error;
  688. } else {
  689. // Find the next word.
  690. error = context->advance();
  691. if (error == SPV_END_OF_STREAM) {
  692. if (spvOperandIsOptional(type)) {
  693. // This would have been the last potential operand for the
  694. // instruction,
  695. // and we didn't find one. We're finished parsing this instruction.
  696. break;
  697. } else {
  698. return context->diagnostic()
  699. << "Expected operand for " << opcodeName
  700. << " instruction, but found the end of the stream.";
  701. }
  702. }
  703. assert(error == SPV_SUCCESS && "Somebody added another way to fail");
  704. if (context->isStartOfNewInst()) {
  705. if (spvOperandIsOptional(type)) {
  706. break;
  707. } else {
  708. return context->diagnostic()
  709. << "Expected operand for " << opcodeName
  710. << " instruction, but found the next instruction instead.";
  711. }
  712. }
  713. std::string operandValue;
  714. error = context->getWord(&operandValue, &nextPosition);
  715. if (error) return context->diagnostic(error) << "Internal Error";
  716. error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
  717. pInst, &expectedOperands);
  718. if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
  719. return SPV_SUCCESS;
  720. if (error) return error;
  721. context->setPosition(nextPosition);
  722. }
  723. }
  724. if (spvOpcodeGeneratesType(pInst->opcode)) {
  725. if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
  726. return SPV_ERROR_INVALID_TEXT;
  727. }
  728. } else if (opcodeEntry->hasType) {
  729. // SPIR-V dictates that if an instruction has both a return value and a
  730. // type ID then the type id is first, and the return value is second.
  731. assert(opcodeEntry->hasResult &&
  732. "Unknown opcode: has a type but no result.");
  733. context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
  734. }
  735. if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
  736. return context->diagnostic()
  737. << opcodeName << " Instruction too long: " << pInst->words.size()
  738. << " words, but the limit is "
  739. << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
  740. }
  741. pInst->words[0] =
  742. spvOpcodeMake(uint16_t(pInst->words.size()), opcodeEntry->opcode);
  743. return SPV_SUCCESS;
  744. }
  745. enum { kAssemblerVersion = 0 };
  746. // Populates a binary stream's |header|. The target environment is specified via
  747. // |env| and Id bound is via |bound|.
  748. spv_result_t SetHeader(spv_target_env env, const uint32_t bound,
  749. uint32_t* header) {
  750. if (!header) return SPV_ERROR_INVALID_BINARY;
  751. header[SPV_INDEX_MAGIC_NUMBER] = spv::MagicNumber;
  752. header[SPV_INDEX_VERSION_NUMBER] = spvVersionForTargetEnv(env);
  753. header[SPV_INDEX_GENERATOR_NUMBER] =
  754. SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS_ASSEMBLER, kAssemblerVersion);
  755. header[SPV_INDEX_BOUND] = bound;
  756. header[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
  757. return SPV_SUCCESS;
  758. }
  759. // Collects all numeric ids in the module source into |numeric_ids|.
  760. // This function is essentially a dry-run of spvTextToBinary.
  761. spv_result_t GetNumericIds(const spvtools::AssemblyGrammar& grammar,
  762. const spvtools::MessageConsumer& consumer,
  763. const spv_text text,
  764. std::set<uint32_t>* numeric_ids) {
  765. spvtools::AssemblyContext context(text, consumer);
  766. if (!text->str) return context.diagnostic() << "Missing assembly text.";
  767. if (!grammar.isValid()) {
  768. return SPV_ERROR_INVALID_TABLE;
  769. }
  770. // Skip past whitespace and comments.
  771. context.advance();
  772. while (context.hasText()) {
  773. spv_instruction_t inst;
  774. // Operand parsing sometimes involves knowing the opcode of the instruction
  775. // being parsed. A malformed input might feature such an operand *before*
  776. // the opcode is known. To guard against accessing an uninitialized opcode,
  777. // the instruction's opcode is initialized to a default value.
  778. inst.opcode = spv::Op::Max;
  779. if (spvTextEncodeOpcode(grammar, &context, &inst)) {
  780. return SPV_ERROR_INVALID_TEXT;
  781. }
  782. if (context.advance()) break;
  783. }
  784. *numeric_ids = context.GetNumericIds();
  785. return SPV_SUCCESS;
  786. }
  787. // Translates a given assembly language module into binary form.
  788. // If a diagnostic is generated, it is not yet marked as being
  789. // for a text-based input.
  790. spv_result_t spvTextToBinaryInternal(const spvtools::AssemblyGrammar& grammar,
  791. const spvtools::MessageConsumer& consumer,
  792. const spv_text text,
  793. const uint32_t options,
  794. spv_binary* pBinary) {
  795. // The ids in this set will have the same values both in source and binary.
  796. // All other ids will be generated by filling in the gaps.
  797. std::set<uint32_t> ids_to_preserve;
  798. if (options & SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS) {
  799. // Collect all numeric ids from the source into ids_to_preserve.
  800. const spv_result_t result =
  801. GetNumericIds(grammar, consumer, text, &ids_to_preserve);
  802. if (result != SPV_SUCCESS) return result;
  803. }
  804. spvtools::AssemblyContext context(text, consumer, std::move(ids_to_preserve));
  805. if (!text->str) return context.diagnostic() << "Missing assembly text.";
  806. if (!grammar.isValid()) {
  807. return SPV_ERROR_INVALID_TABLE;
  808. }
  809. if (!pBinary) return SPV_ERROR_INVALID_POINTER;
  810. std::vector<spv_instruction_t> instructions;
  811. // Skip past whitespace and comments.
  812. context.advance();
  813. while (context.hasText()) {
  814. instructions.push_back({});
  815. spv_instruction_t& inst = instructions.back();
  816. if (auto error = spvTextEncodeOpcode(grammar, &context, &inst)) {
  817. return error;
  818. }
  819. if (context.advance()) break;
  820. }
  821. size_t totalSize = SPV_INDEX_INSTRUCTION;
  822. for (auto& inst : instructions) {
  823. totalSize += inst.words.size();
  824. }
  825. uint32_t* data = new uint32_t[totalSize];
  826. if (!data) return SPV_ERROR_OUT_OF_MEMORY;
  827. uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
  828. for (auto& inst : instructions) {
  829. memcpy(data + currentIndex, inst.words.data(),
  830. sizeof(uint32_t) * inst.words.size());
  831. currentIndex += inst.words.size();
  832. }
  833. if (auto error = SetHeader(grammar.target_env(), context.getBound(), data))
  834. return error;
  835. spv_binary binary = new spv_binary_t();
  836. if (!binary) {
  837. delete[] data;
  838. return SPV_ERROR_OUT_OF_MEMORY;
  839. }
  840. binary->code = data;
  841. binary->wordCount = totalSize;
  842. *pBinary = binary;
  843. return SPV_SUCCESS;
  844. }
  845. } // anonymous namespace
  846. spv_result_t spvTextToBinary(const spv_const_context context,
  847. const char* input_text,
  848. const size_t input_text_size, spv_binary* pBinary,
  849. spv_diagnostic* pDiagnostic) {
  850. return spvTextToBinaryWithOptions(context, input_text, input_text_size,
  851. SPV_TEXT_TO_BINARY_OPTION_NONE, pBinary,
  852. pDiagnostic);
  853. }
  854. spv_result_t spvTextToBinaryWithOptions(const spv_const_context context,
  855. const char* input_text,
  856. const size_t input_text_size,
  857. const uint32_t options,
  858. spv_binary* pBinary,
  859. spv_diagnostic* pDiagnostic) {
  860. spv_context_t hijack_context = *context;
  861. if (pDiagnostic) {
  862. *pDiagnostic = nullptr;
  863. spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
  864. }
  865. spv_text_t text = {input_text, input_text_size};
  866. spvtools::AssemblyGrammar grammar(&hijack_context);
  867. spv_result_t result = spvTextToBinaryInternal(
  868. grammar, hijack_context.consumer, &text, options, pBinary);
  869. if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
  870. return result;
  871. }
  872. void spvTextDestroy(spv_text text) {
  873. if (text) {
  874. if (text->str) delete[] text->str;
  875. delete text;
  876. }
  877. }