1 // Copyright (c) 2015-2016 The Khronos Group 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 "val/construct.h"
16
17 #include <cassert>
18 #include <cstddef>
19
20 namespace libspirv {
21
Construct(ConstructType construct_type,BasicBlock * entry,BasicBlock * exit,std::vector<Construct * > constructs)22 Construct::Construct(ConstructType construct_type,
23 BasicBlock* entry, BasicBlock* exit,
24 std::vector<Construct*> constructs)
25 : type_(construct_type),
26 corresponding_constructs_(constructs),
27 entry_block_(entry),
28 exit_block_(exit) {}
29
type() const30 ConstructType Construct::type() const { return type_; }
31
corresponding_constructs() const32 const std::vector<Construct*>& Construct::corresponding_constructs() const {
33 return corresponding_constructs_;
34 }
corresponding_constructs()35 std::vector<Construct*>& Construct::corresponding_constructs() {
36 return corresponding_constructs_;
37 }
38
ValidateConstructSize(ConstructType type,size_t size)39 bool ValidateConstructSize(ConstructType type, size_t size) {
40 switch (type) {
41 case ConstructType::kSelection: return size == 0;
42 case ConstructType::kContinue: return size == 1;
43 case ConstructType::kLoop: return size == 1;
44 case ConstructType::kCase: return size >= 1;
45 default: assert(1 == 0 && "Type not defined");
46 }
47 return false;
48 }
49
set_corresponding_constructs(std::vector<Construct * > constructs)50 void Construct::set_corresponding_constructs(
51 std::vector<Construct*> constructs) {
52 assert(ValidateConstructSize(type_, constructs.size()));
53 corresponding_constructs_ = constructs;
54 }
55
entry_block() const56 const BasicBlock* Construct::entry_block() const { return entry_block_; }
entry_block()57 BasicBlock* Construct::entry_block() { return entry_block_; }
58
exit_block() const59 const BasicBlock* Construct::exit_block() const { return exit_block_; }
exit_block()60 BasicBlock* Construct::exit_block() { return exit_block_; }
61
set_exit(BasicBlock * block)62 void Construct::set_exit(BasicBlock* block) { exit_block_ = block; }
63 } /// namespace libspirv
64