sirit/src/instructions/flow.cpp

75 lines
2.1 KiB
C++
Raw Normal View History

/* This file is part of the sirit project.
2019-07-14 23:48:59 +02:00
* Copyright (c) 2019 sirit
* This software may be used and distributed according to the terms of the
* 3-Clause BSD License
*/
2019-03-11 07:26:21 +01:00
#include <cassert>
#include "sirit/sirit.h"
#include "stream.h"
namespace Sirit {
2019-03-11 07:26:21 +01:00
Id Module::OpLoopMerge(Id merge_block, Id continue_target, spv::LoopControlMask loop_control,
2020-07-29 10:46:50 +02:00
std::span<const Id> literals) {
code->Reserve(4 + literals.size());
return *code << spv::Op::OpLoopMerge << merge_block << continue_target << loop_control
<< literals << EndOp{};
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) {
code->Reserve(3);
return *code << spv::Op::OpSelectionMerge << merge_block << selection_control << EndOp{};
2018-08-31 09:05:12 +02:00
}
2019-03-11 07:26:21 +01:00
Id Module::OpLabel() {
return Id{++bound};
2019-03-11 07:26:21 +01:00
}
2018-11-01 02:20:49 +01:00
Id Module::OpBranch(Id target_label) {
code->Reserve(2);
return *code << spv::Op::OpBranch << target_label << EndOp{};
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) {
code->Reserve(6);
*code << spv::Op::OpBranchConditional << condition << true_label << false_label;
2018-08-31 09:17:16 +02:00
if (true_weight != 0 || false_weight != 0) {
*code << true_weight << false_weight;
2018-08-31 09:17:16 +02:00
}
return *code << EndOp{};
2018-08-31 09:17:16 +02:00
}
2020-07-29 10:46:50 +02:00
Id Module::OpSwitch(Id selector, Id default_label, std::span<const Literal> literals,
std::span<const Id> labels) {
2019-01-06 03:58:43 +01:00
assert(literals.size() == labels.size());
const size_t size = literals.size();
code->Reserve(3 + size * 2);
*code << spv::Op::OpSwitch << selector << default_label;
2019-01-06 03:58:43 +01:00
for (std::size_t i = 0; i < size; ++i) {
*code << literals[i] << labels[i];
2019-01-06 03:58:43 +01:00
}
return *code << EndOp{};
2019-01-06 03:58:43 +01:00
}
2019-03-11 07:26:21 +01:00
Id Module::OpReturn() {
code->Reserve(1);
return *code << spv::Op::OpReturn << EndOp{};
2019-03-11 07:26:21 +01:00
}
2018-11-01 05:12:52 +01:00
Id Module::OpReturnValue(Id value) {
code->Reserve(2);
return *code << spv::Op::OpReturnValue << value << EndOp{};
2018-11-01 05:12:52 +01:00
}
2018-12-03 02:57:55 +01:00
Id Module::OpKill() {
code->Reserve(1);
return *code << spv::Op::OpKill << EndOp{};
2018-12-03 02:57:55 +01:00
}
} // namespace Sirit