Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 1 | // Copyright (c) 2016 Google Inc. |
| 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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 |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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. |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 14 | |
Lei Zhang | 620f05e | 2016-09-16 16:12:04 -0400 | [diff] [blame] | 15 | #include "spirv-tools/libspirv.hpp" |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 16 | |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 17 | #include "table.h" |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 18 | |
| 19 | namespace spvtools { |
| 20 | |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 21 | // Structs for holding the data members for SpvTools. |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 22 | struct SpirvTools::Impl { |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 23 | explicit Impl(spv_target_env env) : context(spvContextCreate(env)) { |
| 24 | // The default consumer in spv_context_t is a null consumer, which provides |
| 25 | // equivalent functionality (from the user's perspective) as a real consumer |
| 26 | // does nothing. |
| 27 | } |
| 28 | ~Impl() { spvContextDestroy(context); } |
| 29 | |
| 30 | spv_context context; // C interface context object. |
| 31 | }; |
| 32 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 33 | SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {} |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 34 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 35 | SpirvTools::~SpirvTools() {} |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 36 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 37 | void SpirvTools::SetMessageConsumer(MessageConsumer consumer) { |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 38 | SetContextMessageConsumer(impl_->context, std::move(consumer)); |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 41 | bool SpirvTools::Assemble(const std::string& text, |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame^] | 42 | std::vector<uint32_t>* binary, |
| 43 | uint32_t options) const { |
| 44 | return Assemble(text.data(), text.size(), binary, options); |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool SpirvTools::Assemble(const char* text, const size_t text_size, |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame^] | 48 | std::vector<uint32_t>* binary, |
| 49 | uint32_t options) const { |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 50 | spv_binary spvbinary = nullptr; |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame^] | 51 | spv_result_t status = spvTextToBinaryWithOptions( |
| 52 | impl_->context, text, text_size, options, &spvbinary, nullptr); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 53 | if (status == SPV_SUCCESS) { |
| 54 | binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount); |
| 55 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 56 | spvBinaryDestroy(spvbinary); |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 57 | return status == SPV_SUCCESS; |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 60 | bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary, |
| 61 | std::string* text, uint32_t options) const { |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 62 | return Disassemble(binary.data(), binary.size(), text, options); |
| 63 | } |
| 64 | |
| 65 | bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size, |
| 66 | std::string* text, uint32_t options) const { |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 67 | spv_text spvtext = nullptr; |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 68 | spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size, |
| 69 | options, &spvtext, nullptr); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 70 | if (status == SPV_SUCCESS) { |
| 71 | text->assign(spvtext->str, spvtext->str + spvtext->length); |
| 72 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 73 | spvTextDestroy(spvtext); |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 74 | return status == SPV_SUCCESS; |
| 75 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 76 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 77 | bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const { |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 78 | return Validate(binary.data(), binary.size()); |
| 79 | } |
| 80 | |
| 81 | bool SpirvTools::Validate(const uint32_t* binary, |
| 82 | const size_t binary_size) const { |
| 83 | return spvValidateBinary(impl_->context, binary, binary_size, nullptr) == |
| 84 | SPV_SUCCESS; |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 85 | } |
| 86 | |
David Neto | 0066a36 | 2017-03-16 15:06:12 -0400 | [diff] [blame] | 87 | bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size, |
| 88 | const spvtools::ValidatorOptions& options) const { |
| 89 | spv_const_binary_t the_binary{binary, binary_size}; |
| 90 | return spvValidateWithOptions(impl_->context, options, &the_binary, |
| 91 | nullptr) == SPV_SUCCESS; |
| 92 | } |
| 93 | |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 94 | } // namespace spvtools |