blob: 53a4ffb8987da8532d3e5b6b5da2a07d82722171 [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
Lei Zhang620f05e2016-09-16 16:12:04 -040015#include "spirv-tools/libspirv.hpp"
Lei Zhangabf8f642016-06-28 10:23:13 -040016
dan sinclaireda2cfb2018-08-03 15:06:09 -040017#include <string>
18#include <utility>
19#include <vector>
20
21#include "source/table.h"
Lei Zhangabf8f642016-06-28 10:23:13 -040022
23namespace spvtools {
24
Pierre Moreau7183ad52018-01-03 01:54:55 +010025Context::Context(spv_target_env env) : context_(spvContextCreate(env)) {}
26
27Context::Context(Context&& other) : context_(other.context_) {
28 other.context_ = nullptr;
29}
30
31Context& Context::operator=(Context&& other) {
32 spvContextDestroy(context_);
33 context_ = other.context_;
34 other.context_ = nullptr;
35
36 return *this;
37}
38
39Context::~Context() { spvContextDestroy(context_); }
40
41void Context::SetMessageConsumer(MessageConsumer consumer) {
dan sinclair3dad1cd2018-07-07 09:38:00 -040042 SetContextMessageConsumer(context_, std::move(consumer));
Pierre Moreau7183ad52018-01-03 01:54:55 +010043}
44
45spv_context& Context::CContext() { return context_; }
46
47const spv_context& Context::CContext() const { return context_; }
48
Lei Zhang8654caa2016-09-09 10:46:23 -040049// Structs for holding the data members for SpvTools.
Lei Zhang92411ca2016-09-16 15:56:30 -040050struct SpirvTools::Impl {
Lei Zhang8654caa2016-09-09 10:46:23 -040051 explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
52 // The default consumer in spv_context_t is a null consumer, which provides
53 // equivalent functionality (from the user's perspective) as a real consumer
54 // does nothing.
55 }
56 ~Impl() { spvContextDestroy(context); }
57
58 spv_context context; // C interface context object.
59};
60
Lei Zhang92411ca2016-09-16 15:56:30 -040061SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {}
Lei Zhang8654caa2016-09-09 10:46:23 -040062
Lei Zhang92411ca2016-09-16 15:56:30 -040063SpirvTools::~SpirvTools() {}
Lei Zhang8654caa2016-09-09 10:46:23 -040064
Lei Zhang92411ca2016-09-16 15:56:30 -040065void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
dan sinclair3dad1cd2018-07-07 09:38:00 -040066 SetContextMessageConsumer(impl_->context, std::move(consumer));
Lei Zhang755f97f2016-09-02 18:06:18 -040067}
68
Lei Zhang92411ca2016-09-16 15:56:30 -040069bool SpirvTools::Assemble(const std::string& text,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040070 std::vector<uint32_t>* binary,
71 uint32_t options) const {
72 return Assemble(text.data(), text.size(), binary, options);
Lei Zhang5edf0542016-09-20 18:03:37 -040073}
74
75bool SpirvTools::Assemble(const char* text, const size_t text_size,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040076 std::vector<uint32_t>* binary,
77 uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040078 spv_binary spvbinary = nullptr;
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040079 spv_result_t status = spvTextToBinaryWithOptions(
80 impl_->context, text, text_size, options, &spvbinary, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040081 if (status == SPV_SUCCESS) {
82 binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
83 }
Lei Zhangabf8f642016-06-28 10:23:13 -040084 spvBinaryDestroy(spvbinary);
Lei Zhang8654caa2016-09-09 10:46:23 -040085 return status == SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -040086}
87
Lei Zhang92411ca2016-09-16 15:56:30 -040088bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
89 std::string* text, uint32_t options) const {
Lei Zhang5edf0542016-09-20 18:03:37 -040090 return Disassemble(binary.data(), binary.size(), text, options);
91}
92
93bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size,
94 std::string* text, uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040095 spv_text spvtext = nullptr;
Lei Zhang5edf0542016-09-20 18:03:37 -040096 spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size,
97 options, &spvtext, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040098 if (status == SPV_SUCCESS) {
99 text->assign(spvtext->str, spvtext->str + spvtext->length);
100 }
Lei Zhangabf8f642016-06-28 10:23:13 -0400101 spvTextDestroy(spvtext);
Lei Zhang8654caa2016-09-09 10:46:23 -0400102 return status == SPV_SUCCESS;
103}
Lei Zhangabf8f642016-06-28 10:23:13 -0400104
Lei Zhang92411ca2016-09-16 15:56:30 -0400105bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
Lei Zhang5edf0542016-09-20 18:03:37 -0400106 return Validate(binary.data(), binary.size());
107}
108
109bool SpirvTools::Validate(const uint32_t* binary,
110 const size_t binary_size) const {
111 return spvValidateBinary(impl_->context, binary, binary_size, nullptr) ==
112 SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -0400113}
114
David Neto0066a362017-03-16 15:06:12 -0400115bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size,
dan sinclair3dad1cd2018-07-07 09:38:00 -0400116 const ValidatorOptions& options) const {
David Neto0066a362017-03-16 15:06:12 -0400117 spv_const_binary_t the_binary{binary, binary_size};
118 return spvValidateWithOptions(impl_->context, options, &the_binary,
119 nullptr) == SPV_SUCCESS;
120}
121
Lei Zhangabf8f642016-06-28 10:23:13 -0400122} // namespace spvtools