blob: 0636c6451d47ac1276a00577ca87896b418c19a5 [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 Zhangabf8f642016-06-28 10:23:13 -040019
20namespace spvtools {
21
22namespace {
23
24// Sets the module header. Meets the interface requirement of spvBinaryParse().
25spv_result_t SetSpvHeader(void* builder, spv_endianness_t, uint32_t magic,
26 uint32_t version, uint32_t generator,
27 uint32_t id_bound, uint32_t reserved) {
Lei Zhangf51d8232016-08-18 23:16:21 -040028 reinterpret_cast<ir::IrLoader*>(builder)
29 ->SetModuleHeader(magic, version, generator, id_bound, reserved);
Lei Zhangabf8f642016-06-28 10:23:13 -040030 return SPV_SUCCESS;
31};
32
33// Processes a parsed instruction. Meets the interface requirement of
34// spvBinaryParse().
35spv_result_t SetSpvInst(void* builder, const spv_parsed_instruction_t* inst) {
36 reinterpret_cast<ir::IrLoader*>(builder)->AddInstruction(inst);
37 return SPV_SUCCESS;
38};
39
40} // annoymous namespace
41
42spv_result_t SpvTools::Assemble(const std::string& text,
43 std::vector<uint32_t>* binary) {
44 spv_binary spvbinary = nullptr;
45 spv_diagnostic diagnostic = nullptr;
46
47 spv_result_t status = spvTextToBinary(context_, text.data(), text.size(),
48 &spvbinary, &diagnostic);
49 if (status == SPV_SUCCESS) {
50 binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
51 }
52
53 spvDiagnosticDestroy(diagnostic);
54 spvBinaryDestroy(spvbinary);
55
56 return status;
57}
58
59spv_result_t SpvTools::Disassemble(const std::vector<uint32_t>& binary,
qiningd503d752016-07-27 10:19:39 -040060 std::string* text, uint32_t options) {
Lei Zhangabf8f642016-06-28 10:23:13 -040061 spv_text spvtext = nullptr;
62 spv_diagnostic diagnostic = nullptr;
63
64 spv_result_t status = spvBinaryToText(context_, binary.data(), binary.size(),
qiningd503d752016-07-27 10:19:39 -040065 options, &spvtext, &diagnostic);
Lei Zhangabf8f642016-06-28 10:23:13 -040066 if (status == SPV_SUCCESS) {
67 text->assign(spvtext->str, spvtext->str + spvtext->length);
68 }
69
70 spvDiagnosticDestroy(diagnostic);
71 spvTextDestroy(spvtext);
72
73 return status;
74}
75
76std::unique_ptr<ir::Module> SpvTools::BuildModule(
77 const std::vector<uint32_t>& binary) {
78 spv_diagnostic diagnostic = nullptr;
79
Lei Zhangf51d8232016-08-18 23:16:21 -040080 auto module = MakeUnique<ir::Module>();
Lei Zhangabf8f642016-06-28 10:23:13 -040081 ir::IrLoader loader(module.get());
82
83 spv_result_t status =
84 spvBinaryParse(context_, &loader, binary.data(), binary.size(),
85 SetSpvHeader, SetSpvInst, &diagnostic);
86
87 spvDiagnosticDestroy(diagnostic);
88
89 loader.EndModule();
90
91 if (status == SPV_SUCCESS) return module;
92 return nullptr;
93}
94
95std::unique_ptr<ir::Module> SpvTools::BuildModule(const std::string& text) {
96 std::vector<uint32_t> binary;
97 if (Assemble(text, &binary) != SPV_SUCCESS) return nullptr;
98 return BuildModule(binary);
99}
100
101} // namespace spvtools