blob: eabc260fa7374e9c2a4f295604260949e53cb68c [file] [log] [blame]
Lei Zhangabf8f642016-06-28 10:23:13 -04001// Copyright (c) 2016 Google Inc.
2//
David Neto9fc86582016-09-01 15:33:59 -04003// 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 Zhangabf8f642016-06-28 10:23:13 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Lei Zhangabf8f642016-06-28 10:23:13 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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 Zhangabf8f642016-06-28 10:23:13 -040014
15#include "libspirv.hpp"
16
17#include "ir_loader.h"
Lei Zhangf51d8232016-08-18 23:16:21 -040018#include "make_unique.h"
Lei Zhang755f97f2016-09-02 18:06:18 -040019#include "table.h"
Lei Zhangabf8f642016-06-28 10:23:13 -040020
21namespace spvtools {
22
23namespace {
24
25// Sets the module header. Meets the interface requirement of spvBinaryParse().
26spv_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 Zhangf51d8232016-08-18 23:16:21 -040029 reinterpret_cast<ir::IrLoader*>(builder)
30 ->SetModuleHeader(magic, version, generator, id_bound, reserved);
Lei Zhangabf8f642016-06-28 10:23:13 -040031 return SPV_SUCCESS;
32};
33
34// Processes a parsed instruction. Meets the interface requirement of
35// spvBinaryParse().
36spv_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 Zhang755f97f2016-09-02 18:06:18 -040043void SpvTools::SetMessageConsumer(MessageConsumer consumer) {
44 SetContextMessageConsumer(context_, std::move(consumer));
45}
46
Lei Zhangabf8f642016-06-28 10:23:13 -040047spv_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
64spv_result_t SpvTools::Disassemble(const std::vector<uint32_t>& binary,
qiningd503d752016-07-27 10:19:39 -040065 std::string* text, uint32_t options) {
Lei Zhangabf8f642016-06-28 10:23:13 -040066 spv_text spvtext = nullptr;
67 spv_diagnostic diagnostic = nullptr;
68
69 spv_result_t status = spvBinaryToText(context_, binary.data(), binary.size(),
qiningd503d752016-07-27 10:19:39 -040070 options, &spvtext, &diagnostic);
Lei Zhangabf8f642016-06-28 10:23:13 -040071 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
81std::unique_ptr<ir::Module> SpvTools::BuildModule(
82 const std::vector<uint32_t>& binary) {
83 spv_diagnostic diagnostic = nullptr;
84
Lei Zhangf51d8232016-08-18 23:16:21 -040085 auto module = MakeUnique<ir::Module>();
Lei Zhangabf8f642016-06-28 10:23:13 -040086 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
100std::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