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