aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorReinUsesLisp <[email protected]>2019-01-05 23:58:43 -0300
committerReinUsesLisp <[email protected]>2019-01-05 23:58:43 -0300
commit2b0a59d89098aef40b4400771587b6691810f97b (patch)
treeca596ccd4e8be20cf735a67e394c77965e07056c
parente7971b445177d3a4b793def9c1479479371312c2 (diff)
downloadsirit-2b0a59d89098aef40b4400771587b6691810f97b.tar.gz
sirit-2b0a59d89098aef40b4400771587b6691810f97b.zip
Add OpSwitch
-rw-r--r--include/sirit/sirit.h5
-rw-r--r--src/instructions/flow.cpp16
2 files changed, 21 insertions, 0 deletions
diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h
index 253e152..eded07e 100644
--- a/include/sirit/sirit.h
+++ b/include/sirit/sirit.h
@@ -192,6 +192,11 @@ class Module {
std::uint32_t true_weight = 0,
std::uint32_t false_weight = 0);
+ /// Multi-way branch to one of the operand label.
+ Id OpSwitch(Id selector, Id default_label,
+ const std::vector<Literal>& literals,
+ const std::vector<Id>& labels);
+
/// Returns with no value from a function with void return type.
Id OpReturn();
diff --git a/src/instructions/flow.cpp b/src/instructions/flow.cpp
index 8ff856e..4c0036f 100644
--- a/src/instructions/flow.cpp
+++ b/src/instructions/flow.cpp
@@ -7,6 +7,7 @@
#include "common_types.h"
#include "op.h"
#include "sirit/sirit.h"
+#include <cassert>
#include <vector>
namespace Sirit {
@@ -51,6 +52,21 @@ Id Module::OpBranchConditional(Id condition, Id true_label, Id false_label,
return AddCode(std::move(op));
}
+Id Module::OpSwitch(Id selector, Id default_label,
+ const std::vector<Literal>& literals,
+ 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));
+}
+
Id Module::OpReturn() { return AddCode(spv::Op::OpReturn); }
Id Module::OpReturnValue(Id value) {