1 | // Copyright (c) 2016 Google Inc. |
2 | // |
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 |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
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. |
14 | |
15 | #include "source/opt/build_module.h" |
16 | |
17 | #include <utility> |
18 | #include <vector> |
19 | |
20 | #include "source/opt/ir_context.h" |
21 | #include "source/opt/ir_loader.h" |
22 | #include "source/table.h" |
23 | #include "source/util/make_unique.h" |
24 | |
25 | namespace spvtools { |
26 | namespace { |
27 | |
28 | // Sets the module header for IrLoader. Meets the interface requirement of |
29 | // spvBinaryParse(). |
30 | spv_result_t (void* builder, spv_endianness_t, uint32_t magic, |
31 | uint32_t version, uint32_t generator, |
32 | uint32_t id_bound, uint32_t reserved) { |
33 | reinterpret_cast<opt::IrLoader*>(builder)->SetModuleHeader( |
34 | magic, version, generator, id_bound, reserved); |
35 | return SPV_SUCCESS; |
36 | } |
37 | |
38 | // Processes a parsed instruction for IrLoader. Meets the interface requirement |
39 | // of spvBinaryParse(). |
40 | spv_result_t SetSpvInst(void* builder, const spv_parsed_instruction_t* inst) { |
41 | if (reinterpret_cast<opt::IrLoader*>(builder)->AddInstruction(inst)) { |
42 | return SPV_SUCCESS; |
43 | } |
44 | return SPV_ERROR_INVALID_BINARY; |
45 | } |
46 | |
47 | } // namespace |
48 | |
49 | std::unique_ptr<opt::IRContext> BuildModule(spv_target_env env, |
50 | MessageConsumer consumer, |
51 | const uint32_t* binary, |
52 | const size_t size) { |
53 | auto context = spvContextCreate(env); |
54 | SetContextMessageConsumer(context, consumer); |
55 | |
56 | auto irContext = MakeUnique<opt::IRContext>(env, consumer); |
57 | opt::IrLoader loader(consumer, irContext->module()); |
58 | |
59 | spv_result_t status = spvBinaryParse(context, &loader, binary, size, |
60 | SetSpvHeader, SetSpvInst, nullptr); |
61 | loader.EndModule(); |
62 | |
63 | spvContextDestroy(context); |
64 | |
65 | return status == SPV_SUCCESS ? std::move(irContext) : nullptr; |
66 | } |
67 | |
68 | std::unique_ptr<opt::IRContext> BuildModule(spv_target_env env, |
69 | MessageConsumer consumer, |
70 | const std::string& text, |
71 | uint32_t assemble_options) { |
72 | SpirvTools t(env); |
73 | t.SetMessageConsumer(consumer); |
74 | std::vector<uint32_t> binary; |
75 | if (!t.Assemble(text, &binary, assemble_options)) return nullptr; |
76 | return BuildModule(env, consumer, binary.data(), binary.size()); |
77 | } |
78 | |
79 | } // namespace spvtools |
80 | |