text.cpp 29 KB

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