text.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 diagostic. 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 (pInst->opcode == SpvOpExtInst && 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. return context->diagnostic()
  226. << "Invalid extended instruction name '" << textValue << "'.";
  227. }
  228. spvInstructionAddWord(pInst, extInst->ext_inst);
  229. // Prepare to parse the operands for the extended instructions.
  230. spvPushOperandTypes(extInst->operandTypes, pExpectedOperands);
  231. } break;
  232. case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
  233. // The assembler accepts the symbolic name for the opcode, but without
  234. // the "Op" prefix. For example, "IAdd" is accepted. The number
  235. // of the opcode is emitted.
  236. SpvOp opcode;
  237. if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
  238. return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
  239. << " '" << textValue << "'.";
  240. }
  241. spv_opcode_desc opcodeEntry = nullptr;
  242. if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
  243. return context->diagnostic(SPV_ERROR_INTERNAL)
  244. << "OpSpecConstant opcode table out of sync";
  245. }
  246. spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
  247. // Prepare to parse the operands for the opcode. Except skip the
  248. // type Id and result Id, since they've already been processed.
  249. assert(opcodeEntry->hasType);
  250. assert(opcodeEntry->hasResult);
  251. assert(opcodeEntry->numTypes >= 2);
  252. spvPushOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
  253. } break;
  254. case SPV_OPERAND_TYPE_LITERAL_INTEGER:
  255. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
  256. // The current operand is an *unsigned* 32-bit integer.
  257. // That's just how the grammar works.
  258. spvtools::IdType expected_type = {
  259. 32, false, spvtools::IdTypeClass::kScalarIntegerType};
  260. if (auto error = context->binaryEncodeNumericLiteral(
  261. textValue, error_code_for_literals, expected_type, pInst)) {
  262. return error;
  263. }
  264. } break;
  265. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
  266. // This is a context-independent literal number which can be a 32-bit
  267. // number of floating point value.
  268. if (auto error = context->binaryEncodeNumericLiteral(
  269. textValue, error_code_for_literals, spvtools::kUnknownType,
  270. pInst)) {
  271. return error;
  272. }
  273. break;
  274. case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
  275. case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
  276. spvtools::IdType expected_type = spvtools::kUnknownType;
  277. // The encoding for OpConstant, OpSpecConstant and OpSwitch all
  278. // depend on either their own result-id or the result-id of
  279. // one of their parameters.
  280. if (SpvOpConstant == pInst->opcode ||
  281. SpvOpSpecConstant == pInst->opcode) {
  282. // The type of the literal is determined by the type Id of the
  283. // instruction.
  284. expected_type =
  285. context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
  286. if (!spvtools::isScalarFloating(expected_type) &&
  287. !spvtools::isScalarIntegral(expected_type)) {
  288. spv_opcode_desc d;
  289. const char* opcode_name = "opcode";
  290. if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
  291. opcode_name = d->name;
  292. }
  293. return context->diagnostic()
  294. << "Type for " << opcode_name
  295. << " must be a scalar floating point or integer type";
  296. }
  297. } else if (pInst->opcode == SpvOpSwitch) {
  298. // The type of the literal is the same as the type of the selector.
  299. expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
  300. if (!spvtools::isScalarIntegral(expected_type)) {
  301. return context->diagnostic()
  302. << "The selector operand for OpSwitch must be the result"
  303. " of an instruction that generates an integer scalar";
  304. }
  305. }
  306. if (auto error = context->binaryEncodeNumericLiteral(
  307. textValue, error_code_for_literals, expected_type, pInst)) {
  308. return error;
  309. }
  310. } break;
  311. case SPV_OPERAND_TYPE_LITERAL_STRING:
  312. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
  313. spv_literal_t literal = {};
  314. spv_result_t error = spvTextToLiteral(textValue, &literal);
  315. if (error != SPV_SUCCESS) {
  316. if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
  317. return context->diagnostic(error_code_for_literals)
  318. << "Invalid literal string '" << textValue << "'.";
  319. }
  320. if (literal.type != SPV_LITERAL_TYPE_STRING) {
  321. return context->diagnostic()
  322. << "Expected literal string, found literal number '" << textValue
  323. << "'.";
  324. }
  325. // NOTE: Special case for extended instruction library import
  326. if (SpvOpExtInstImport == pInst->opcode) {
  327. const spv_ext_inst_type_t ext_inst_type =
  328. spvExtInstImportTypeGet(literal.str.c_str());
  329. if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
  330. return context->diagnostic()
  331. << "Invalid extended instruction import '" << literal.str
  332. << "'";
  333. }
  334. if ((error = context->recordIdAsExtInstImport(pInst->words[1],
  335. ext_inst_type)))
  336. return error;
  337. }
  338. if (context->binaryEncodeString(literal.str.c_str(), pInst))
  339. return SPV_ERROR_INVALID_TEXT;
  340. } break;
  341. // Masks.
  342. case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
  343. case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
  344. case SPV_OPERAND_TYPE_LOOP_CONTROL:
  345. case SPV_OPERAND_TYPE_IMAGE:
  346. case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
  347. case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
  348. case SPV_OPERAND_TYPE_SELECTION_CONTROL:
  349. case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: {
  350. uint32_t value;
  351. if (grammar.parseMaskOperand(type, textValue, &value)) {
  352. return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
  353. << " operand '" << textValue << "'.";
  354. }
  355. if (auto error = context->binaryEncodeU32(value, pInst)) return error;
  356. // Prepare to parse the operands for this logical operand.
  357. grammar.pushOperandTypesForMask(type, value, pExpectedOperands);
  358. } break;
  359. case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
  360. auto error = spvTextEncodeOperand(
  361. grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
  362. pInst, pExpectedOperands);
  363. if (error == SPV_FAILED_MATCH) {
  364. // It's not a literal number -- is it a literal string?
  365. error = spvTextEncodeOperand(grammar, context,
  366. SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
  367. textValue, pInst, pExpectedOperands);
  368. }
  369. if (error == SPV_FAILED_MATCH) {
  370. // It's not a literal -- is it an ID?
  371. error =
  372. spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
  373. textValue, pInst, pExpectedOperands);
  374. }
  375. if (error) {
  376. return context->diagnostic(error)
  377. << "Invalid word following !<integer>: " << textValue;
  378. }
  379. if (pExpectedOperands->empty()) {
  380. pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
  381. }
  382. } break;
  383. default: {
  384. // NOTE: All non literal operands are handled here using the operand
  385. // table.
  386. spv_operand_desc entry;
  387. if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
  388. return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
  389. << " '" << textValue << "'.";
  390. }
  391. if (context->binaryEncodeU32(entry->value, pInst)) {
  392. return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
  393. << " '" << textValue << "'.";
  394. }
  395. // Prepare to parse the operands for this logical operand.
  396. spvPushOperandTypes(entry->operandTypes, pExpectedOperands);
  397. } break;
  398. }
  399. return SPV_SUCCESS;
  400. }
  401. namespace {
  402. /// Encodes an instruction started by !<integer> at the given position in text.
  403. ///
  404. /// Puts the encoded words into *pInst. If successful, moves position past the
  405. /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
  406. /// leaves position pointing to the error in text.
  407. spv_result_t encodeInstructionStartingWithImmediate(
  408. const spvtools::AssemblyGrammar& grammar,
  409. spvtools::AssemblyContext* context, spv_instruction_t* pInst) {
  410. std::string firstWord;
  411. spv_position_t nextPosition = {};
  412. auto error = context->getWord(&firstWord, &nextPosition);
  413. if (error) return context->diagnostic(error) << "Internal Error";
  414. if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
  415. return error;
  416. }
  417. while (context->advance() != SPV_END_OF_STREAM) {
  418. // A beginning of a new instruction means we're done.
  419. if (context->isStartOfNewInst()) return SPV_SUCCESS;
  420. // Otherwise, there must be an operand that's either a literal, an ID, or
  421. // an immediate.
  422. std::string operandValue;
  423. if ((error = context->getWord(&operandValue, &nextPosition)))
  424. return context->diagnostic(error) << "Internal Error";
  425. if (operandValue == "=")
  426. return context->diagnostic() << firstWord << " not allowed before =.";
  427. // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
  428. // expanded.
  429. spv_operand_pattern_t dummyExpectedOperands;
  430. error = spvTextEncodeOperand(
  431. grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
  432. pInst, &dummyExpectedOperands);
  433. if (error) return error;
  434. context->setPosition(nextPosition);
  435. }
  436. return SPV_SUCCESS;
  437. }
  438. /// @brief Translate single Opcode and operands to binary form
  439. ///
  440. /// @param[in] grammar the grammar to use for compilation
  441. /// @param[in, out] context the dynamic compilation info
  442. /// @param[in] text stream to translate
  443. /// @param[out] pInst returned binary Opcode
  444. /// @param[in,out] pPosition in the text stream
  445. ///
  446. /// @return result code
  447. spv_result_t spvTextEncodeOpcode(const spvtools::AssemblyGrammar& grammar,
  448. spvtools::AssemblyContext* context,
  449. spv_instruction_t* pInst) {
  450. // Check for !<integer> first.
  451. if ('!' == context->peek()) {
  452. return encodeInstructionStartingWithImmediate(grammar, context, pInst);
  453. }
  454. std::string firstWord;
  455. spv_position_t nextPosition = {};
  456. spv_result_t error = context->getWord(&firstWord, &nextPosition);
  457. if (error) return context->diagnostic() << "Internal Error";
  458. std::string opcodeName;
  459. std::string result_id;
  460. spv_position_t result_id_position = {};
  461. if (context->startsWithOp()) {
  462. opcodeName = firstWord;
  463. } else {
  464. result_id = firstWord;
  465. if ('%' != result_id.front()) {
  466. return context->diagnostic()
  467. << "Expected <opcode> or <result-id> at the beginning "
  468. "of an instruction, found '"
  469. << result_id << "'.";
  470. }
  471. result_id_position = context->position();
  472. // The '=' sign.
  473. context->setPosition(nextPosition);
  474. if (context->advance())
  475. return context->diagnostic() << "Expected '=', found end of stream.";
  476. std::string equal_sign;
  477. error = context->getWord(&equal_sign, &nextPosition);
  478. if ("=" != equal_sign)
  479. return context->diagnostic() << "'=' expected after result id.";
  480. // The <opcode> after the '=' sign.
  481. context->setPosition(nextPosition);
  482. if (context->advance())
  483. return context->diagnostic() << "Expected opcode, found end of stream.";
  484. error = context->getWord(&opcodeName, &nextPosition);
  485. if (error) return context->diagnostic(error) << "Internal Error";
  486. if (!context->startsWithOp()) {
  487. return context->diagnostic()
  488. << "Invalid Opcode prefix '" << opcodeName << "'.";
  489. }
  490. }
  491. // NOTE: The table contains Opcode names without the "Op" prefix.
  492. const char* pInstName = opcodeName.data() + 2;
  493. spv_opcode_desc opcodeEntry;
  494. error = grammar.lookupOpcode(pInstName, &opcodeEntry);
  495. if (error) {
  496. return context->diagnostic(error)
  497. << "Invalid Opcode name '" << opcodeName << "'";
  498. }
  499. if (opcodeEntry->hasResult && result_id.empty()) {
  500. return context->diagnostic()
  501. << "Expected <result-id> at the beginning of an instruction, found '"
  502. << firstWord << "'.";
  503. }
  504. if (!opcodeEntry->hasResult && !result_id.empty()) {
  505. return context->diagnostic()
  506. << "Cannot set ID " << result_id << " because " << opcodeName
  507. << " does not produce a result ID.";
  508. }
  509. pInst->opcode = opcodeEntry->opcode;
  510. context->setPosition(nextPosition);
  511. // Reserve the first word for the instruction.
  512. spvInstructionAddWord(pInst, 0);
  513. // Maintains the ordered list of expected operand types.
  514. // For many instructions we only need the {numTypes, operandTypes}
  515. // entries in opcodeEntry. However, sometimes we need to modify
  516. // the list as we parse the operands. This occurs when an operand
  517. // has its own logical operands (such as the LocalSize operand for
  518. // ExecutionMode), or for extended instructions that may have their
  519. // own operands depending on the selected extended instruction.
  520. spv_operand_pattern_t expectedOperands;
  521. expectedOperands.reserve(opcodeEntry->numTypes);
  522. for (auto i = 0; i < opcodeEntry->numTypes; i++)
  523. expectedOperands.push_back(
  524. opcodeEntry->operandTypes[opcodeEntry->numTypes - i - 1]);
  525. while (!expectedOperands.empty()) {
  526. const spv_operand_type_t type = expectedOperands.back();
  527. expectedOperands.pop_back();
  528. // Expand optional tuples lazily.
  529. if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
  530. if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
  531. // Handle the <result-id> for value generating instructions.
  532. // We've already consumed it from the text stream. Here
  533. // we inject its words into the instruction.
  534. spv_position_t temp_pos = context->position();
  535. error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
  536. result_id.c_str(), pInst, nullptr);
  537. result_id_position = context->position();
  538. // Because we are injecting we have to reset the position afterwards.
  539. context->setPosition(temp_pos);
  540. if (error) return error;
  541. } else {
  542. // Find the next word.
  543. error = context->advance();
  544. if (error == SPV_END_OF_STREAM) {
  545. if (spvOperandIsOptional(type)) {
  546. // This would have been the last potential operand for the
  547. // instruction,
  548. // and we didn't find one. We're finished parsing this instruction.
  549. break;
  550. } else {
  551. return context->diagnostic()
  552. << "Expected operand, found end of stream.";
  553. }
  554. }
  555. assert(error == SPV_SUCCESS && "Somebody added another way to fail");
  556. if (context->isStartOfNewInst()) {
  557. if (spvOperandIsOptional(type)) {
  558. break;
  559. } else {
  560. return context->diagnostic()
  561. << "Expected operand, found next instruction instead.";
  562. }
  563. }
  564. std::string operandValue;
  565. error = context->getWord(&operandValue, &nextPosition);
  566. if (error) return context->diagnostic(error) << "Internal Error";
  567. error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
  568. pInst, &expectedOperands);
  569. if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
  570. return SPV_SUCCESS;
  571. if (error) return error;
  572. context->setPosition(nextPosition);
  573. }
  574. }
  575. if (spvOpcodeGeneratesType(pInst->opcode)) {
  576. if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
  577. return SPV_ERROR_INVALID_TEXT;
  578. }
  579. } else if (opcodeEntry->hasType) {
  580. // SPIR-V dictates that if an instruction has both a return value and a
  581. // type ID then the type id is first, and the return value is second.
  582. assert(opcodeEntry->hasResult &&
  583. "Unknown opcode: has a type but no result.");
  584. context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
  585. }
  586. if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
  587. return context->diagnostic()
  588. << "Instruction too long: " << pInst->words.size()
  589. << " words, but the limit is "
  590. << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
  591. }
  592. pInst->words[0] =
  593. spvOpcodeMake(uint16_t(pInst->words.size()), opcodeEntry->opcode);
  594. return SPV_SUCCESS;
  595. }
  596. enum { kAssemblerVersion = 0 };
  597. // Populates a binary stream's |header|. The target environment is specified via
  598. // |env| and Id bound is via |bound|.
  599. spv_result_t SetHeader(spv_target_env env, const uint32_t bound,
  600. uint32_t* header) {
  601. if (!header) return SPV_ERROR_INVALID_BINARY;
  602. header[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber;
  603. header[SPV_INDEX_VERSION_NUMBER] = spvVersionForTargetEnv(env);
  604. header[SPV_INDEX_GENERATOR_NUMBER] =
  605. SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS_ASSEMBLER, kAssemblerVersion);
  606. header[SPV_INDEX_BOUND] = bound;
  607. header[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
  608. return SPV_SUCCESS;
  609. }
  610. // Collects all numeric ids in the module source into |numeric_ids|.
  611. // This function is essentially a dry-run of spvTextToBinary.
  612. spv_result_t GetNumericIds(const spvtools::AssemblyGrammar& grammar,
  613. const spvtools::MessageConsumer& consumer,
  614. const spv_text text,
  615. std::set<uint32_t>* numeric_ids) {
  616. spvtools::AssemblyContext context(text, consumer);
  617. if (!text->str) return context.diagnostic() << "Missing assembly text.";
  618. if (!grammar.isValid()) {
  619. return SPV_ERROR_INVALID_TABLE;
  620. }
  621. // Skip past whitespace and comments.
  622. context.advance();
  623. while (context.hasText()) {
  624. spv_instruction_t inst;
  625. if (spvTextEncodeOpcode(grammar, &context, &inst)) {
  626. return SPV_ERROR_INVALID_TEXT;
  627. }
  628. if (context.advance()) break;
  629. }
  630. *numeric_ids = context.GetNumericIds();
  631. return SPV_SUCCESS;
  632. }
  633. // Translates a given assembly language module into binary form.
  634. // If a diagnostic is generated, it is not yet marked as being
  635. // for a text-based input.
  636. spv_result_t spvTextToBinaryInternal(const spvtools::AssemblyGrammar& grammar,
  637. const spvtools::MessageConsumer& consumer,
  638. const spv_text text,
  639. const uint32_t options,
  640. spv_binary* pBinary) {
  641. // The ids in this set will have the same values both in source and binary.
  642. // All other ids will be generated by filling in the gaps.
  643. std::set<uint32_t> ids_to_preserve;
  644. if (options & SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS) {
  645. // Collect all numeric ids from the source into ids_to_preserve.
  646. const spv_result_t result =
  647. GetNumericIds(grammar, consumer, text, &ids_to_preserve);
  648. if (result != SPV_SUCCESS) return result;
  649. }
  650. spvtools::AssemblyContext context(text, consumer, std::move(ids_to_preserve));
  651. if (!text->str) return context.diagnostic() << "Missing assembly text.";
  652. if (!grammar.isValid()) {
  653. return SPV_ERROR_INVALID_TABLE;
  654. }
  655. if (!pBinary) return SPV_ERROR_INVALID_POINTER;
  656. std::vector<spv_instruction_t> instructions;
  657. // Skip past whitespace and comments.
  658. context.advance();
  659. while (context.hasText()) {
  660. instructions.push_back({});
  661. spv_instruction_t& inst = instructions.back();
  662. if (spvTextEncodeOpcode(grammar, &context, &inst)) {
  663. return SPV_ERROR_INVALID_TEXT;
  664. }
  665. if (context.advance()) break;
  666. }
  667. size_t totalSize = SPV_INDEX_INSTRUCTION;
  668. for (auto& inst : instructions) {
  669. totalSize += inst.words.size();
  670. }
  671. uint32_t* data = new uint32_t[totalSize];
  672. if (!data) return SPV_ERROR_OUT_OF_MEMORY;
  673. uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
  674. for (auto& inst : instructions) {
  675. memcpy(data + currentIndex, inst.words.data(),
  676. sizeof(uint32_t) * inst.words.size());
  677. currentIndex += inst.words.size();
  678. }
  679. if (auto error = SetHeader(grammar.target_env(), context.getBound(), data))
  680. return error;
  681. spv_binary binary = new spv_binary_t();
  682. if (!binary) {
  683. delete[] data;
  684. return SPV_ERROR_OUT_OF_MEMORY;
  685. }
  686. binary->code = data;
  687. binary->wordCount = totalSize;
  688. *pBinary = binary;
  689. return SPV_SUCCESS;
  690. }
  691. } // anonymous namespace
  692. spv_result_t spvTextToBinary(const spv_const_context context,
  693. const char* input_text,
  694. const size_t input_text_size, spv_binary* pBinary,
  695. spv_diagnostic* pDiagnostic) {
  696. return spvTextToBinaryWithOptions(context, input_text, input_text_size,
  697. SPV_TEXT_TO_BINARY_OPTION_NONE, pBinary,
  698. pDiagnostic);
  699. }
  700. spv_result_t spvTextToBinaryWithOptions(const spv_const_context context,
  701. const char* input_text,
  702. const size_t input_text_size,
  703. const uint32_t options,
  704. spv_binary* pBinary,
  705. spv_diagnostic* pDiagnostic) {
  706. spv_context_t hijack_context = *context;
  707. if (pDiagnostic) {
  708. *pDiagnostic = nullptr;
  709. spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
  710. }
  711. spv_text_t text = {input_text, input_text_size};
  712. spvtools::AssemblyGrammar grammar(&hijack_context);
  713. spv_result_t result = spvTextToBinaryInternal(
  714. grammar, hijack_context.consumer, &text, options, pBinary);
  715. if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
  716. return result;
  717. }
  718. void spvTextDestroy(spv_text text) {
  719. if (text) {
  720. if (text->str) delete[] text->str;
  721. delete text;
  722. }
  723. }