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 | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame^] | 19 | #include "table.h" |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 20 | |
| 21 | namespace spvtools { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // Sets the module header. Meets the interface requirement of spvBinaryParse(). |
| 26 | spv_result_t SetSpvHeader(void* builder, spv_endianness_t, uint32_t magic, |
| 27 | uint32_t version, uint32_t generator, |
| 28 | uint32_t id_bound, uint32_t reserved) { |
Lei Zhang | f51d823 | 2016-08-18 23:16:21 -0400 | [diff] [blame] | 29 | reinterpret_cast<ir::IrLoader*>(builder) |
| 30 | ->SetModuleHeader(magic, version, generator, id_bound, reserved); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 31 | return SPV_SUCCESS; |
| 32 | }; |
| 33 | |
| 34 | // Processes a parsed instruction. Meets the interface requirement of |
| 35 | // spvBinaryParse(). |
| 36 | spv_result_t SetSpvInst(void* builder, const spv_parsed_instruction_t* inst) { |
| 37 | reinterpret_cast<ir::IrLoader*>(builder)->AddInstruction(inst); |
| 38 | return SPV_SUCCESS; |
| 39 | }; |
| 40 | |
| 41 | } // annoymous namespace |
| 42 | |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame^] | 43 | void SpvTools::SetMessageConsumer(MessageConsumer consumer) { |
| 44 | SetContextMessageConsumer(context_, std::move(consumer)); |
| 45 | } |
| 46 | |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 47 | spv_result_t SpvTools::Assemble(const std::string& text, |
| 48 | std::vector<uint32_t>* binary) { |
| 49 | spv_binary spvbinary = nullptr; |
| 50 | spv_diagnostic diagnostic = nullptr; |
| 51 | |
| 52 | spv_result_t status = spvTextToBinary(context_, text.data(), text.size(), |
| 53 | &spvbinary, &diagnostic); |
| 54 | if (status == SPV_SUCCESS) { |
| 55 | binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount); |
| 56 | } |
| 57 | |
| 58 | spvDiagnosticDestroy(diagnostic); |
| 59 | spvBinaryDestroy(spvbinary); |
| 60 | |
| 61 | return status; |
| 62 | } |
| 63 | |
| 64 | spv_result_t SpvTools::Disassemble(const std::vector<uint32_t>& binary, |
qining | d503d75 | 2016-07-27 10:19:39 -0400 | [diff] [blame] | 65 | std::string* text, uint32_t options) { |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 66 | spv_text spvtext = nullptr; |
| 67 | spv_diagnostic diagnostic = nullptr; |
| 68 | |
| 69 | spv_result_t status = spvBinaryToText(context_, binary.data(), binary.size(), |
qining | d503d75 | 2016-07-27 10:19:39 -0400 | [diff] [blame] | 70 | options, &spvtext, &diagnostic); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 71 | if (status == SPV_SUCCESS) { |
| 72 | text->assign(spvtext->str, spvtext->str + spvtext->length); |
| 73 | } |
| 74 | |
| 75 | spvDiagnosticDestroy(diagnostic); |
| 76 | spvTextDestroy(spvtext); |
| 77 | |
| 78 | return status; |
| 79 | } |
| 80 | |
| 81 | std::unique_ptr<ir::Module> SpvTools::BuildModule( |
| 82 | const std::vector<uint32_t>& binary) { |
| 83 | spv_diagnostic diagnostic = nullptr; |
| 84 | |
Lei Zhang | f51d823 | 2016-08-18 23:16:21 -0400 | [diff] [blame] | 85 | auto module = MakeUnique<ir::Module>(); |
Lei Zhang | abf8f64 | 2016-06-28 10:23:13 -0400 | [diff] [blame] | 86 | ir::IrLoader loader(module.get()); |
| 87 | |
| 88 | spv_result_t status = |
| 89 | spvBinaryParse(context_, &loader, binary.data(), binary.size(), |
| 90 | SetSpvHeader, SetSpvInst, &diagnostic); |
| 91 | |
| 92 | spvDiagnosticDestroy(diagnostic); |
| 93 | |
| 94 | loader.EndModule(); |
| 95 | |
| 96 | if (status == SPV_SUCCESS) return module; |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | std::unique_ptr<ir::Module> SpvTools::BuildModule(const std::string& text) { |
| 101 | std::vector<uint32_t> binary; |
| 102 | if (Assemble(text, &binary) != SPV_SUCCESS) return nullptr; |
| 103 | return BuildModule(binary); |
| 104 | } |
| 105 | |
| 106 | } // namespace spvtools |