blob: e2c1d893279d3e3a870e636047a1b6f5a75c09b2 [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001// Copyright (c) 2015 The Khronos Group Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and/or associated documentation files (the
5// "Materials"), to deal in the Materials without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Materials, and to
8// permit persons to whom the Materials are furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Materials.
13//
14// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17// https://www.khronos.org/registry/
18//
19// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
26
27#include <libspirv/libspirv.h>
28#include "binary.h"
29#include "diagnostic.h"
30#include "ext_inst.h"
31#include "opcode.h"
32#include "operand.h"
33#include "text.h"
34
35#include <assert.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include <string>
41#include <vector>
42#include <unordered_map>
43
44// Structures
45
46struct spv_named_id_table_t {
47 std::unordered_map<std::string, uint32_t> namedIds;
48};
49
50// Text API
51
52std::string getWord(const char *str) {
53 size_t index = 0;
54 while (true) {
55 switch (str[index]) {
56 case '\0':
57 case '\t':
58 case '\n':
59 case ' ':
60 break;
61 default:
62 index++;
63 }
64 }
65 return std::string(str, str + index);
66}
67
68spv_named_id_table spvNamedIdTableCreate() {
69 return new spv_named_id_table_t();
70}
71
72void spvNamedIdTableDestory(spv_named_id_table table) { delete table; }
73
74uint32_t spvNamedIdAssignOrGet(spv_named_id_table table, const char *textValue,
75 uint32_t *pBound) {
76 if (table->namedIds.end() == table->namedIds.find(textValue)) {
77 table->namedIds[textValue] = *pBound;
78 }
79 return table->namedIds[textValue];
80}
81
82int32_t spvTextIsNamedId(const char *textValue) {
83 // TODO: Strengthen the parsing of textValue to only include allow names that
84 // match: ([a-z]|[A-Z])(_|[a-z]|[A-Z]|[0-9])*
85 switch (textValue[0]) {
86 case '0':
87 case '1':
88 case '2':
89 case '3':
90 case '4':
91 case '5':
92 case '6':
93 case '7':
94 case '8':
95 case '9':
96 return false;
97 default:
98 break;
99 }
100 return true;
101}
102
103spv_result_t spvTextAdvanceLine(const spv_text text, spv_position position) {
104 while (true) {
105 switch (text->str[position->index]) {
106 case '\0':
107 return SPV_END_OF_STREAM;
108 case '\n':
109 position->column = 0;
110 position->line++;
111 position->index++;
112 return SPV_SUCCESS;
113 default:
114 position->line++;
115 position->index++;
116 break;
117 }
118 }
119}
120
121spv_result_t spvTextAdvance(const spv_text text, spv_position position) {
122 // NOTE: Consume white space, otherwise don't advance.
123 switch (text->str[position->index]) {
124 case '\0':
125 return SPV_END_OF_STREAM;
126 case ';':
127 return spvTextAdvanceLine(text, position);
128 case ' ':
129 case '\t':
130 position->column++;
131 position->index++;
132 return spvTextAdvance(text, position);
133 case '\n':
134 position->column = 0;
135 position->line++;
136 position->index++;
137 return spvTextAdvance(text, position);
138 default:
139 break;
140 }
141
142 return SPV_SUCCESS;
143}
144
145spv_result_t spvTextWordGet(const spv_text text,
146 const spv_position startPosition, std::string &word,
147 spv_position endPosition) {
148 spvCheck(!text->str || !text->length, return SPV_ERROR_INVALID_TEXT);
149 spvCheck(!startPosition || !endPosition, return SPV_ERROR_INVALID_POINTER);
150
151 *endPosition = *startPosition;
152
153 // NOTE: Assumes first character is not white space!
154 while (true) {
155 switch (text->str[endPosition->index]) {
156 case ' ':
157 case '\t':
158 case '\n':
159 case '\0': { // NOTE: End of word found!
160 word.assign(text->str + startPosition->index,
161 (size_t)(endPosition->index - startPosition->index));
162 return SPV_SUCCESS;
163 }
164 default:
165 break;
166 }
167
168 endPosition->column++;
169 endPosition->index++;
170 }
171}
172
173spv_result_t spvTextStringGet(const spv_text text,
174 const spv_position startPosition,
175 std::string &string, spv_position endPosition) {
176 spvCheck(!text->str || !text->length, return SPV_ERROR_INVALID_TEXT);
177 spvCheck(!startPosition || !endPosition, return SPV_ERROR_INVALID_POINTER);
178
179 spvCheck('"' != text->str[startPosition->index],
180 return SPV_ERROR_INVALID_TEXT);
181
182 *endPosition = *startPosition;
183
184 // NOTE: Assumes first character is not white space
185 while (true) {
186 endPosition->column++;
187 endPosition->index++;
188
189 switch (text->str[endPosition->index]) {
190 case '"': {
191 endPosition->column++;
192 endPosition->index++;
193
194 string.assign(text->str + startPosition->index,
195 (size_t)(endPosition->index - startPosition->index));
196
197 return SPV_SUCCESS;
198 }
199 case '\n':
200 case '\0':
201 return SPV_ERROR_INVALID_TEXT;
202 default:
203 break;
204 }
205 }
206}
207
208spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue) {
209 char *endPtr = nullptr;
210 *pValue = strtoul(textValue, &endPtr, 0);
211 if (0 == *pValue && textValue == endPtr) {
212 return SPV_ERROR_INVALID_TEXT;
213 }
214 return SPV_SUCCESS;
215}
216
217spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) {
218 bool isSigned = false;
219 bool isFloat = false;
220 bool isString = false;
221
222 if ('-' == textValue[0]) {
223 isSigned = true;
224 }
225
226 for (uint64_t index = 0; index < strlen(textValue); ++index) {
227 switch (textValue[index]) {
228 case '0':
229 case '1':
230 case '2':
231 case '3':
232 case '4':
233 case '5':
234 case '6':
235 case '7':
236 case '8':
237 case '9':
238 break;
239 case '.':
240 isFloat = true;
241 break;
242 default:
243 isString = true;
244 break;
245 }
246 }
247
248 if (isString) {
249 pLiteral->type = SPV_LITERAL_TYPE_STRING;
250 strncpy(pLiteral->value.str, textValue, strlen(textValue));
251 } else if (isFloat) {
252 double d = strtod(textValue, nullptr);
253 float f = (float)d;
254 if (d == (double)f) {
255 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
256 pLiteral->value.f = f;
257 } else {
258 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
259 pLiteral->value.d = d;
260 }
261 } else if (isSigned) {
262 int64_t i64 = strtoll(textValue, nullptr, 10);
263 int32_t i32 = (int32_t)i64;
264 if (i64 == (int64_t)i32) {
265 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
266 pLiteral->value.i32 = i32;
267 } else {
268 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
269 pLiteral->value.i64 = i64;
270 }
271 } else {
272 uint64_t u64 = strtoull(textValue, nullptr, 10);
273 uint32_t u32 = (uint32_t)u64;
274 if (u64 == (uint64_t)u32) {
275 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
276 pLiteral->value.u32 = u32;
277 } else {
278 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
279 pLiteral->value.u64 = u64;
280 }
281 }
282
283 return SPV_SUCCESS;
284}
285
286spv_result_t spvTextEncodeOperand(
287 const spv_operand_type_t type, const char *textValue,
288 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
289 spv_named_id_table namedIdTable, spv_instruction_t *pInst,
290 const spv_operand_type_t **ppExtraOperands, uint32_t *pBound,
291 const spv_position position, spv_diagnostic *pDiagnostic) {
292 // NOTE: Handle immediate int in the stream
293 if ('!' == textValue[0]) {
294 const char *begin = textValue + 1;
295 char *end = nullptr;
296 uint32_t immediateInt = strtoul(begin, &end, 0);
297 size_t size = strlen(textValue);
298 size_t length = (end - begin);
299 spvCheck(size - 1 != length, DIAGNOSTIC << "Invalid immediate integer '"
300 << textValue << "'.";
301 return SPV_ERROR_INVALID_TEXT);
302 position->column += size;
303 position->index += size;
304 pInst->words[pInst->wordCount] = immediateInt;
305 pInst->wordCount += 1;
306 return SPV_SUCCESS;
307 }
308
309 switch (type) {
310 case SPV_OPERAND_TYPE_ID: {
311 if ('$' == textValue[0]) {
312 textValue++;
313 }
314 // TODO: Force all ID's to be prefixed with '$'.
315 uint32_t id = 0;
316 if (spvTextIsNamedId(textValue)) {
317 id = spvNamedIdAssignOrGet(namedIdTable, textValue, pBound);
318 } else {
319 spvCheck(spvTextToUInt32(textValue, &id),
320 DIAGNOSTIC << "Invalid ID '" << textValue << "'.";
321 return SPV_ERROR_INVALID_TEXT);
322 }
323 pInst->words[pInst->wordCount++] = id;
324 if (*pBound <= id) {
325 *pBound = id + 1;
326 }
327 } break;
328 case SPV_OPERAND_TYPE_RESULT_ID: {
329 if ('%' == textValue[0]) {
330 textValue++;
331 }
332 // TODO: Force all Result ID's to be prefixed with '%'.
333 uint32_t id = 0;
334 if (spvTextIsNamedId(textValue)) {
335 id = spvNamedIdAssignOrGet(namedIdTable, textValue, pBound);
336 } else {
337 spvCheck(spvTextToUInt32(textValue, &id),
338 DIAGNOSTIC << "Invalid result ID '" << textValue << "'.";
339 return SPV_ERROR_INVALID_TEXT);
340 }
341 pInst->words[pInst->wordCount++] = id;
342 if (*pBound <= id) {
343 *pBound = id + 1;
344 }
345 } break;
346 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
347 // NOTE: Special case for extension instruction lookup
348 if (OpExtInst == pInst->opcode) {
349 spv_ext_inst_desc extInst;
350 spvCheck(spvExtInstTableNameLookup(extInstTable, pInst->extInstType,
351 textValue, &extInst),
352 DIAGNOSTIC << "Invalid extended instruction name '"
353 << textValue << "'.";
354 return SPV_ERROR_INVALID_TEXT);
355 pInst->words[pInst->wordCount++] = extInst->ext_inst;
356 *ppExtraOperands = extInst->operandTypes;
357 return SPV_SUCCESS;
358 }
359
360 // TODO: Literal numbers can be any number up to 64 bits wide. This
361 // includes integers and floating point numbers.
362 spvCheck(spvTextToUInt32(textValue, &pInst->words[pInst->wordCount++]),
363 DIAGNOSTIC << "Invalid literal number '" << textValue << "'.";
364 return SPV_ERROR_INVALID_TEXT);
365 } break;
366 case SPV_OPERAND_TYPE_LITERAL: {
367 spv_literal_t literal = {};
368 spvCheck(spvTextToLiteral(textValue, &literal),
369 DIAGNOSTIC << "Invalid literal '" << textValue << "'.";
370 return SPV_ERROR_INVALID_TEXT);
371 switch (literal.type) {
372 case SPV_LITERAL_TYPE_INT_32:
373 spvCheck(spvBinaryEncodeU32((uint32_t)literal.value.i32, pInst,
374 position, pDiagnostic),
375 return SPV_ERROR_INVALID_TEXT);
376 break;
377 case SPV_LITERAL_TYPE_INT_64: {
378 spvCheck(spvBinaryEncodeU64((uint64_t)literal.value.i64, pInst,
379 position, pDiagnostic),
380 return SPV_ERROR_INVALID_TEXT);
381 } break;
382 case SPV_LITERAL_TYPE_UINT_32: {
383 spvCheck(spvBinaryEncodeU32(literal.value.u32, pInst, position,
384 pDiagnostic),
385 return SPV_ERROR_INVALID_TEXT);
386 } break;
387 case SPV_LITERAL_TYPE_UINT_64: {
388 spvCheck(spvBinaryEncodeU64((uint64_t)literal.value.u64, pInst,
389 position, pDiagnostic),
390 return SPV_ERROR_INVALID_TEXT);
391 } break;
392 case SPV_LITERAL_TYPE_FLOAT_32: {
393 spvCheck(spvBinaryEncodeU32((uint32_t)literal.value.f, pInst,
394 position, pDiagnostic),
395 return SPV_ERROR_INVALID_TEXT);
396 } break;
397 case SPV_LITERAL_TYPE_FLOAT_64: {
398 spvCheck(spvBinaryEncodeU64((uint64_t)literal.value.d, pInst,
399 position, pDiagnostic),
400 return SPV_ERROR_INVALID_TEXT);
401 } break;
402 case SPV_LITERAL_TYPE_STRING: {
403 spvCheck(spvBinaryEncodeString(literal.value.str, pInst, position,
404 pDiagnostic),
405 return SPV_ERROR_INVALID_TEXT);
406 } break;
407 default:
408 DIAGNOSTIC << "Invalid literal '" << textValue << "'";
409 return SPV_ERROR_INVALID_TEXT;
410 }
411 } break;
412 case SPV_OPERAND_TYPE_LITERAL_STRING: {
413 size_t len = strlen(textValue);
414 spvCheck('"' != textValue[0] && '"' != textValue[len - 1],
415 DIAGNOSTIC << "Invalid literal string '" << textValue
416 << "', expected quotes.";
417 return SPV_ERROR_INVALID_TEXT);
418 // NOTE: Strip quotes
419 std::string text(textValue + 1, len - 2);
420
421 // NOTE: Special case for extended instruction library import
422 if (OpExtInstImport == pInst->opcode) {
423 pInst->extInstType = spvExtInstImportTypeGet(text.c_str());
424 }
425
426 spvCheck(
427 spvBinaryEncodeString(text.c_str(), pInst, position, pDiagnostic),
428 return SPV_ERROR_INVALID_TEXT);
429 } break;
430 default: {
431 // NOTE: All non literal operands are handled here using the operand
432 // table.
433 spv_operand_desc entry;
434 spvCheck(spvOperandTableNameLookup(operandTable, type, textValue, &entry),
435 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '"
436 << textValue << "'.";
437 return SPV_ERROR_INVALID_TEXT;);
438 spvCheck(spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic),
439 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '"
440 << textValue << "'.";
441 return SPV_ERROR_INVALID_TEXT;);
442 if (ppExtraOperands && entry->operandTypes[0] != SPV_OPERAND_TYPE_NONE) {
443 *ppExtraOperands = entry->operandTypes;
444 }
445 } break;
446 }
447 return SPV_SUCCESS;
448}
449
450spv_result_t spvTextEncodeOpcode(
451 const spv_text text, const spv_opcode_table opcodeTable,
452 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
453 spv_named_id_table namedIdTable, uint32_t *pBound, spv_instruction_t *pInst,
454 spv_position position, spv_diagnostic *pDiagnostic) {
455 std::string opcodeName;
456 spv_position_t nextPosition = {};
457 spvCheck(spvTextWordGet(text, position, opcodeName, &nextPosition),
458 return SPV_ERROR_INTERNAL);
459
460 bool immediate = false;
461 spvCheck('!' == text->str[position->index], immediate = true);
462 if (!immediate) {
463 spvCheck('O' != opcodeName[0] || 'p' != opcodeName[1],
464 DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'.";
465 return SPV_ERROR_INVALID_TEXT);
466 }
467
468 // NOTE: Handle insertion of an immediate integer into the binary stream
469 if (immediate) {
470 const char *begin = opcodeName.data() + 1;
471 char *end = nullptr;
472 uint32_t immediateInt = strtoul(begin, &end, 0);
473 size_t size = opcodeName.size() - 1;
474 spvCheck(size != (size_t)(end - begin),
475 DIAGNOSTIC << "Invalid immediate integer '" << opcodeName << "'.";
476 return SPV_ERROR_INVALID_TEXT);
477 position->column += opcodeName.size();
478 position->index += opcodeName.size();
479 pInst->words[0] = immediateInt;
480 pInst->wordCount = 1;
481 return SPV_SUCCESS;
482 }
483
484 // NOTE: The table contains Opcode names without the "Op" prefix.
485 const char *pInstName = opcodeName.data() + 2;
486
487 spv_opcode_desc opcodeEntry;
488 spv_result_t error =
489 spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry);
490 spvCheck(error, DIAGNOSTIC << "Invalid Opcode name '"
491 << getWord(text->str + position->index) << "'";
492 return error);
493 pInst->opcode = opcodeEntry->opcode;
494 *position = nextPosition;
495 pInst->wordCount++;
496
497 // NOTE: Process the fixed size operands
498 const spv_operand_type_t *extraOperandTypes = nullptr;
499 for (int32_t operandIndex = 0; operandIndex < (opcodeEntry->wordCount - 1);
500 ++operandIndex) {
501 spvCheck(spvTextAdvance(text, position),
502 DIAGNOSTIC << "Expected operand, found end of stream.";
503 return SPV_ERROR_INVALID_TEXT);
504
505 std::string operandValue;
506 error = spvTextWordGet(text, position, operandValue, &nextPosition);
507 spvCheck(error, return error);
508
509 error = spvTextEncodeOperand(
510 opcodeEntry->operandTypes[operandIndex], operandValue.c_str(),
511 operandTable, extInstTable, namedIdTable, pInst, &extraOperandTypes,
512 pBound, position, pDiagnostic);
513 spvCheck(error, return error);
514
515 *position = nextPosition;
516 }
517
518 if (spvOpcodeIsVariable(opcodeEntry)) {
519 if (!extraOperandTypes) {
520 // NOTE: Handle variable length not defined by an immediate previously
521 // encountered in the Opcode.
522 spv_operand_type_t type =
523 opcodeEntry->operandTypes[opcodeEntry->wordCount - 1];
524
525 while (!spvTextAdvance(text, position)) {
526 std::string textValue;
527 spvTextWordGet(text, position, textValue, &nextPosition);
528
529 // NOTE: Check if the next text word is an Opcode
530 if ('O' == textValue[0] && 'p' == textValue[1]) {
531 // NOTE: This is the end of the current instruction stream and we
532 // break out of this loop
533 break;
534 } else {
535 if (SPV_OPERAND_TYPE_LITERAL_STRING == type) {
536 spvCheck(spvTextAdvance(text, position),
537 DIAGNOSTIC << "Invalid string, found end of stream.";
538 return SPV_ERROR_INVALID_TEXT);
539
540 std::string string;
541 spvCheck(spvTextStringGet(text, position, string, &nextPosition),
542 DIAGNOSTIC << "Invalid string, new line or end of stream.";
543 return SPV_ERROR_INVALID_TEXT);
544 spvCheck(
545 spvTextEncodeOperand(type, string.c_str(), operandTable,
546 extInstTable, namedIdTable, pInst, nullptr,
547 pBound, position, pDiagnostic),
548 return SPV_ERROR_INVALID_TEXT);
549 } else {
550 spvCheck(
551 spvTextEncodeOperand(type, textValue.c_str(), operandTable,
552 extInstTable, namedIdTable, pInst, nullptr,
553 pBound, position, pDiagnostic),
554 return SPV_ERROR_INVALID_TEXT);
555 }
556 *position = nextPosition;
557 }
558 }
559 } else {
560 // NOTE: Process the variable size operands defined by an immediate
561 // previously encountered in the Opcode.
562 uint64_t extraOperandsIndex = 0;
563 while (extraOperandTypes[extraOperandsIndex]) {
564 spvCheck(spvTextAdvance(text, position),
565 DIAGNOSTIC << "Expected operand, found end of stream.";
566 return SPV_ERROR_INVALID_TEXT);
567
568 std::string operandValue;
569 error = spvTextWordGet(text, position, operandValue, &nextPosition);
570
571 error = spvTextEncodeOperand(extraOperandTypes[extraOperandsIndex],
572 operandValue.c_str(), operandTable,
573 extInstTable, namedIdTable, pInst, nullptr,
574 pBound, position, pDiagnostic);
575 spvCheck(error, return error);
576
577 *position = nextPosition;
578
579 extraOperandsIndex++;
580 }
581 }
582 }
583
584 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
585
586 return SPV_SUCCESS;
587}
588
589spv_result_t spvTextToBinary(const spv_text text,
590 const spv_opcode_table opcodeTable,
591 const spv_operand_table operandTable,
592 const spv_ext_inst_table extInstTable,
593 spv_binary *pBinary, spv_diagnostic *pDiagnostic) {
594 spv_position_t position = {};
595 spvCheck(!text->str || !text->length, DIAGNOSTIC << "Text stream is empty.";
596 return SPV_ERROR_INVALID_TEXT);
597 spvCheck(!opcodeTable || !operandTable || !extInstTable,
598 return SPV_ERROR_INVALID_TABLE);
599 spvCheck(!pBinary, return SPV_ERROR_INVALID_POINTER);
600 spvCheck(!pDiagnostic, return SPV_ERROR_INVALID_DIAGNOSTIC);
601
602 // NOTE: Ensure diagnostic is zero initialised
603 *pDiagnostic = {};
604
605 uint32_t bound = 1;
606
607 std::vector<spv_instruction_t> instructions;
608
609 spvCheck(spvTextAdvance(text, &position), DIAGNOSTIC
610 << "Text stream is empty.";
611 return SPV_ERROR_INVALID_TEXT);
612
613 spv_named_id_table namedIdTable = spvNamedIdTableCreate();
614 spvCheck(!namedIdTable, return SPV_ERROR_OUT_OF_MEMORY);
615
616 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
617 while (text->length > position.index) {
618 spv_instruction_t inst = {};
619 inst.extInstType = extInstType;
620
621 spvCheck(spvTextEncodeOpcode(text, opcodeTable, operandTable, extInstTable,
622 namedIdTable, &bound, &inst, &position,
623 pDiagnostic),
624 spvNamedIdTableDestory(namedIdTable);
625 return SPV_ERROR_INVALID_TEXT);
626 extInstType = inst.extInstType;
627
628 instructions.push_back(inst);
629
630 spvCheck(spvTextAdvance(text, &position), break);
631 }
632
633 spvNamedIdTableDestory(namedIdTable);
634
635 size_t totalSize = SPV_INDEX_INSTRUCTION;
636 for (auto &inst : instructions) {
637 totalSize += inst.wordCount;
638 }
639
640 uint32_t *data = new uint32_t[totalSize];
641 spvCheck(!data, return SPV_ERROR_OUT_OF_MEMORY);
642 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
643 for (auto &inst : instructions) {
644 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
645 currentIndex += inst.wordCount;
646 }
647
648 spv_binary binary = new spv_binary_t();
649 spvCheck(!binary, delete[] data; return SPV_ERROR_OUT_OF_MEMORY);
650 binary->code = data;
651 binary->wordCount = totalSize;
652
653 spv_result_t error = spvBinaryHeaderSet(binary, bound);
654 spvCheck(error, spvBinaryDestroy(binary); return error);
655
656 *pBinary = binary;
657
658 return SPV_SUCCESS;
659}
660
661void spvTextDestroy(spv_text text) {
662 spvCheck(!text, return );
663 if (text->str) {
664 delete[] text->str;
665 }
666 delete text;
667}