| 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 |
|
| 15 |
#include <cassert> |
| 16 |
#include <cstdio> |
| 17 |
#include <cstring> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "source/spirv_target_env.h" |
| 21 |
#include "spirv-tools/libspirv.h" |
| 22 |
#include "tools/io.h" |
| 23 |
#include "tools/util/flags.h" |
| 24 |
|
| 25 |
constexpr auto kDefaultTarget = SPV_ENV_UNIVERSAL_1_6; |
| 26 |
static const std::string kHelpText = |
| 27 |
R"(%s - Create a SPIR-V binary module from SPIR-V assembly text |
| 28 |
|
| 29 |
Usage: %s [options] [<filename>] |
| 30 |
|
| 31 |
The SPIR-V assembly text is read from <filename>. |
| 32 |
Use "-" as the filename to read from standard input. |
| 33 |
Use "./-" to read from the file named "-". |
| 34 |
|
| 35 |
The SPIR-V binary module is written to file "out.spv", unless the -o option |
| 36 |
is used. |
| 37 |
|
| 38 |
Options: |
| 39 |
|
| 40 |
-h, --help Print this help. |
| 41 |
|
| 42 |
-o <filename> Set the output filename. Use '-' to mean stdout. |
| 43 |
--version Display assembler version information. |
| 44 |
--preserve-numeric-ids |
| 45 |
Numeric IDs in the binary will have the same values as in the |
| 46 |
source. Non-numeric IDs are allocated by filling in the gaps, |
| 47 |
starting with 1 and going up. |
| 48 |
--target-env %s |
| 49 |
Use specified environment. |
| 50 |
)"; |
| 51 |
|
| 52 |
// clang-format off |
| 53 |
// flag name= default_value= required= |
| 54 |
FLAG_SHORT_bool( h, false, false); |
| 55 |
FLAG_LONG_bool( help, false, false); |
| 56 |
FLAG_LONG_bool( version, false, false); |
| 57 |
FLAG_LONG_bool( preserve_numeric_ids, false, false); |
| 58 |
FLAG_SHORT_string(o, "", false); |
| 59 |
FLAG_LONG_string( target_env, "", false); |
| 60 |
// clang-format on |
| 61 |
|
| 62 |
int main(int, const char** argv) { |
| 63 |
if (!flags::Parse(argv)) { |
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
if (flags::h.value() || flags::help.value()) { |
| 68 |
const std::string target_env_list = spvTargetEnvList(19, 80); |
| 69 |
printf(kHelpText.c_str(), argv[0], argv[0], target_env_list.c_str()); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
if (flags::version.value()) { |
| 74 |
printf("%s\n", spvSoftwareVersionDetailsString()); |
| 75 |
printf("Target: %s\n", spvTargetEnvDescription(kDefaultTarget)); |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
std::string outFile = flags::o.value(); |
| 80 |
if (outFile.empty()) { |
| 81 |
outFile = "out.spv"; |
| 82 |
} |
| 83 |
|
| 84 |
uint32_t options = 0; |
| 85 |
if (flags::preserve_numeric_ids.value()) { |
| 86 |
options |= SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS; |
| 87 |
} |
| 88 |
|
| 89 |
if (flags::positional_arguments.size() > 1) { |
| 90 |
fprintf(stderr, "error: one input file at most must be specified.\n"); |
| 91 |
return 1; |
| 92 |
} |
| 93 |
std::string inFile = flags::positional_arguments.size() == 1 |
| 94 |
? flags::positional_arguments[0] |
| 95 |
: "-"; |
| 96 |
|
| 97 |
std::vector<char> contents; |
| 98 |
if (!ReadTextFile(inFile.c_str(), &contents)) return 1; |
| 99 |
|
| 100 |
// Can only deduce target after the file has been read |
| 101 |
spv_target_env target_env; |
| 102 |
if (flags::target_env.value().empty()) { |
| 103 |
if (!spvReadEnvironmentFromText(contents, &target_env)) { |
| 104 |
// Revert to default version since deduction failed |
| 105 |
target_env = kDefaultTarget; |
| 106 |
} |
| 107 |
} else if (!spvParseTargetEnv(flags::target_env.value().c_str(), |
| 108 |
&target_env)) { |
| 109 |
fprintf(stderr, "error: Unrecognized target env: %s\n", |
| 110 |
flags::target_env.value().c_str()); |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
spv_binary binary; |
| 115 |
spv_diagnostic diagnostic = nullptr; |
| 116 |
spv_context context = spvContextCreate(target_env); |
| 117 |
spv_result_t error = spvTextToBinaryWithOptions( |
| 118 |
context, contents.data(), contents.size(), options, &binary, &diagnostic); |
| 119 |
spvContextDestroy(context); |
| 120 |
if (error) { |
| 121 |
spvDiagnosticPrint(diagnostic); |
| 122 |
spvDiagnosticDestroy(diagnostic); |
| 123 |
return error; |
| 124 |
} |
| 125 |
|
| 126 |
if (!WriteFile<uint32_t>(outFile.c_str(), "wb", binary->code, |
| 127 |
binary->wordCount)) { |
| 128 |
spvBinaryDestroy(binary); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
|
| 132 |
spvBinaryDestroy(binary); |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |