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 | |
Pierre Moreau | 7183ad5 | 2018-01-03 01:54:55 +0100 | [diff] [blame] | 21 | Context::Context(spv_target_env env) : context_(spvContextCreate(env)) {} |
| 22 | |
| 23 | Context::Context(Context&& other) : context_(other.context_) { |
| 24 | other.context_ = nullptr; |
| 25 | } |
| 26 | |
| 27 | Context& Context::operator=(Context&& other) { |
| 28 | spvContextDestroy(context_); |
| 29 | context_ = other.context_; |
| 30 | other.context_ = nullptr; |
| 31 | |
| 32 | return *this; |
| 33 | } |
| 34 | |
| 35 | Context::~Context() { spvContextDestroy(context_); } |
| 36 | |
| 37 | void Context::SetMessageConsumer(MessageConsumer consumer) { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 38 | SetContextMessageConsumer(context_, std::move(consumer)); |
Pierre Moreau | 7183ad5 | 2018-01-03 01:54:55 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | spv_context& Context::CContext() { return context_; } |
| 42 | |
| 43 | const spv_context& Context::CContext() const { return context_; } |
| 44 | |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 45 | // Structs for holding the data members for SpvTools. |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 46 | struct SpirvTools::Impl { |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 47 | explicit Impl(spv_target_env env) : context(spvContextCreate(env)) { |
| 48 | // The default consumer in spv_context_t is a null consumer, which provides |
| 49 | // equivalent functionality (from the user's perspective) as a real consumer |
| 50 | // does nothing. |
| 51 | } |
| 52 | ~Impl() { spvContextDestroy(context); } |
| 53 | |
| 54 | spv_context context; // C interface context object. |
| 55 | }; |
| 56 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 57 | SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {} |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 58 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 59 | SpirvTools::~SpirvTools() {} |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 60 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 61 | void SpirvTools::SetMessageConsumer(MessageConsumer consumer) { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 62 | SetContextMessageConsumer(impl_->context, std::move(consumer)); |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 65 | bool SpirvTools::Assemble(const std::string& text, |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 66 | std::vector<uint32_t>* binary, |
| 67 | uint32_t options) const { |
| 68 | return Assemble(text.data(), text.size(), binary, options); |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool SpirvTools::Assemble(const char* text, const size_t text_size, |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 72 | std::vector<uint32_t>* binary, |
| 73 | uint32_t options) const { |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 74 | spv_binary spvbinary = nullptr; |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 75 | spv_result_t status = spvTextToBinaryWithOptions( |
| 76 | impl_->context, text, text_size, options, &spvbinary, nullptr); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 77 | if (status == SPV_SUCCESS) { |
| 78 | binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount); |
| 79 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 80 | spvBinaryDestroy(spvbinary); |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 81 | return status == SPV_SUCCESS; |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 84 | bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary, |
| 85 | std::string* text, uint32_t options) const { |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 86 | return Disassemble(binary.data(), binary.size(), text, options); |
| 87 | } |
| 88 | |
| 89 | bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size, |
| 90 | std::string* text, uint32_t options) const { |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 91 | spv_text spvtext = nullptr; |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 92 | spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size, |
| 93 | options, &spvtext, nullptr); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 94 | if (status == SPV_SUCCESS) { |
| 95 | text->assign(spvtext->str, spvtext->str + spvtext->length); |
| 96 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 97 | spvTextDestroy(spvtext); |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 98 | return status == SPV_SUCCESS; |
| 99 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 100 | |
Lei Zhang | 92411ca | 2016-09-16 15:56:30 -0400 | [diff] [blame] | 101 | bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const { |
Lei Zhang | 5edf054 | 2016-09-20 18:03:37 -0400 | [diff] [blame] | 102 | return Validate(binary.data(), binary.size()); |
| 103 | } |
| 104 | |
| 105 | bool SpirvTools::Validate(const uint32_t* binary, |
| 106 | const size_t binary_size) const { |
| 107 | return spvValidateBinary(impl_->context, binary, binary_size, nullptr) == |
| 108 | SPV_SUCCESS; |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 109 | } |
| 110 | |
David Neto | 0066a36 | 2017-03-16 15:06:12 -0400 | [diff] [blame] | 111 | bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size, |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 112 | const ValidatorOptions& options) const { |
David Neto | 0066a36 | 2017-03-16 15:06:12 -0400 | [diff] [blame] | 113 | spv_const_binary_t the_binary{binary, binary_size}; |
| 114 | return spvValidateWithOptions(impl_->context, options, &the_binary, |
| 115 | nullptr) == SPV_SUCCESS; |
| 116 | } |
| 117 | |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 118 | } // namespace spvtools |