sirit/src/instructions/flow.cpp

84 lines
2.4 KiB
C++
Raw Normal View History

/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
2018-11-16 08:10:10 +01:00
* Lesser General Public License version 3 or any later version.
*/
2019-03-11 07:26:21 +01:00
#include <cassert>
#include <vector>
#include "common_types.h"
#include "op.h"
#include "sirit/sirit.h"
namespace Sirit {
2019-03-11 07:26:21 +01:00
Id Module::OpLoopMerge(Id merge_block, Id continue_target, spv::LoopControlMask loop_control,
const std::vector<Id>& literals) {
auto op{std::make_unique<Op>(spv::Op::OpLoopMerge)};
2018-08-31 08:55:43 +02:00
op->Add(merge_block);
op->Add(continue_target);
op->Add(static_cast<u32>(loop_control));
2018-08-31 08:55:43 +02:00
op->Add(literals);
return AddCode(std::move(op));
2018-08-31 08:55:43 +02:00
}
2019-03-11 07:26:21 +01:00
Id Module::OpSelectionMerge(Id merge_block, spv::SelectionControlMask selection_control) {
auto op{std::make_unique<Op>(spv::Op::OpSelectionMerge)};
2018-08-31 09:05:12 +02:00
op->Add(merge_block);
op->Add(static_cast<u32>(selection_control));
return AddCode(std::move(op));
2018-08-31 09:05:12 +02:00
}
2019-03-11 07:26:21 +01:00
Id Module::OpLabel() {
return AddCode(spv::Op::OpLabel, bound++);
}
2018-11-01 02:20:49 +01:00
Id Module::OpBranch(Id target_label) {
auto op{std::make_unique<Op>(spv::Op::OpBranch)};
2018-08-31 09:10:05 +02:00
op->Add(target_label);
return AddCode(std::move(op));
2018-08-31 09:10:05 +02:00
}
2019-03-11 07:26:21 +01:00
Id Module::OpBranchConditional(Id condition, Id true_label, Id false_label, u32 true_weight,
u32 false_weight) {
auto op{std::make_unique<Op>(spv::Op::OpBranchConditional)};
2018-08-31 09:17:16 +02:00
op->Add(condition);
op->Add(true_label);
op->Add(false_label);
if (true_weight != 0 || false_weight != 0) {
op->Add(true_weight);
op->Add(false_weight);
2018-08-31 09:17:16 +02:00
}
return AddCode(std::move(op));
2018-08-31 09:17:16 +02:00
}
2019-03-11 07:26:21 +01:00
Id Module::OpSwitch(Id selector, Id default_label, const std::vector<Literal>& literals,
2019-01-06 03:58:43 +01:00
const std::vector<Id>& labels) {
const std::size_t size = literals.size();
assert(literals.size() == labels.size());
auto op{std::make_unique<Op>(spv::Op::OpSwitch)};
op->Add(selector);
op->Add(default_label);
for (std::size_t i = 0; i < size; ++i) {
op->Add(literals[i]);
op->Add(labels[i]);
}
return AddCode(std::move(op));
}
2019-03-11 07:26:21 +01:00
Id Module::OpReturn() {
return AddCode(spv::Op::OpReturn);
}
2018-11-01 05:12:52 +01:00
Id Module::OpReturnValue(Id value) {
auto op{std::make_unique<Op>(spv::Op::OpReturnValue)};
op->Add(value);
return AddCode(std::move(op));
}
2018-12-03 02:57:55 +01:00
Id Module::OpKill() {
return AddCode(std::make_unique<Op>(spv::Op::OpKill));
}
} // namespace Sirit