From 2b0a59d89098aef40b4400771587b6691810f97b Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sat, 5 Jan 2019 23:58:43 -0300 Subject: [PATCH] Add OpSwitch --- include/sirit/sirit.h | 5 +++++ src/instructions/flow.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) 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& literals, + const std::vector& 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 #include 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& literals, + const std::vector& labels) { + const std::size_t size = literals.size(); + assert(literals.size() == labels.size()); + auto op{std::make_unique(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) {