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 | |
| 15 | #include "libspirv.hpp" |
| 16 | |
| 17 | #include "ir_loader.h" |
Lei Zhang | f51d823 | 2016-08-18 23:16:21 -0400 | [diff] [blame] | 18 | #include "make_unique.h" |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 19 | #include "message.h" |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 20 | #include "table.h" |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 21 | |
| 22 | namespace spvtools { |
| 23 | |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 24 | // Structs for holding the data members for SpvTools. |
| 25 | struct SpvTools::Impl { |
| 26 | explicit Impl(spv_target_env env) : context(spvContextCreate(env)) { |
| 27 | // The default consumer in spv_context_t is a null consumer, which provides |
| 28 | // equivalent functionality (from the user's perspective) as a real consumer |
| 29 | // does nothing. |
| 30 | } |
| 31 | ~Impl() { spvContextDestroy(context); } |
| 32 | |
| 33 | spv_context context; // C interface context object. |
| 34 | }; |
| 35 | |
| 36 | SpvTools::SpvTools(spv_target_env env) : impl_(new Impl(env)) {} |
| 37 | |
| 38 | SpvTools::~SpvTools() {} |
| 39 | |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 40 | void SpvTools::SetMessageConsumer(MessageConsumer consumer) { |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 41 | SetContextMessageConsumer(impl_->context, std::move(consumer)); |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 44 | bool SpvTools::Assemble(const std::string& text, |
| 45 | std::vector<uint32_t>* binary) const { |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 46 | spv_binary spvbinary = nullptr; |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 47 | spv_result_t status = spvTextToBinary(impl_->context, text.data(), |
| 48 | text.size(), &spvbinary, nullptr); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 49 | if (status == SPV_SUCCESS) { |
| 50 | binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount); |
| 51 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 52 | spvBinaryDestroy(spvbinary); |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 53 | return status == SPV_SUCCESS; |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 56 | bool SpvTools::Disassemble(const std::vector<uint32_t>& binary, |
| 57 | std::string* text, uint32_t options) const { |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 58 | spv_text spvtext = nullptr; |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 59 | spv_result_t status = spvBinaryToText( |
| 60 | impl_->context, binary.data(), binary.size(), options, &spvtext, nullptr); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 61 | if (status == SPV_SUCCESS) { |
| 62 | text->assign(spvtext->str, spvtext->str + spvtext->length); |
| 63 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 64 | spvTextDestroy(spvtext); |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 65 | return status == SPV_SUCCESS; |
| 66 | } |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 67 | |
Lei Zhang | 8654caa | 2016-09-09 10:46:23 -0400 | [diff] [blame] | 68 | bool SpvTools::Validate(const std::vector<uint32_t>& binary) const { |
| 69 | spv_const_binary_t b = {binary.data(), binary.size()}; |
| 70 | return spvValidate(impl_->context, &b, nullptr) == SPV_SUCCESS; |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 73 | } // namespace spvtools |