From 41aa1a1ed8ea9391b09039a35abce8684b170573 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 21 Jul 2020 15:58:50 +0000 Subject: [PATCH] [hlsl-writer] Add switch statement support This CL adds emission of switch statements to the HLSL backend. Bug: tint:7 Change-Id: Ie6f41031878a1f5a945f9310429d28fb0e4a98c8 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25223 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/hlsl/generator_impl.cc | 28 +++++++ src/writer/hlsl/generator_impl.h | 4 + src/writer/hlsl/generator_impl_switch_test.cc | 76 +++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 src/writer/hlsl/generator_impl_switch_test.cc diff --git a/BUILD.gn b/BUILD.gn index 89476233e2..2c087a571e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1024,6 +1024,7 @@ source_set("tint_unittests_hlsl_writer_src") { "src/writer/hlsl/generator_impl_continue_test.cc", "src/writer/hlsl/generator_impl_identifier_test.cc", "src/writer/hlsl/generator_impl_return_test.cc", + "src/writer/hlsl/generator_impl_switch_test.cc", "src/writer/hlsl/generator_impl_test.cc", "src/writer/hlsl/generator_impl_unary_op_test.cc", "src/writer/hlsl/namer_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e1b5e85b9f..c5520c3f57 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -548,6 +548,7 @@ if (${TINT_BUILD_HLSL_WRITER}) writer/hlsl/generator_impl_continue_test.cc writer/hlsl/generator_impl_identifier_test.cc writer/hlsl/generator_impl_return_test.cc + writer/hlsl/generator_impl_switch_test.cc writer/hlsl/generator_impl_test.cc writer/hlsl/generator_impl_unary_op_test.cc writer/hlsl/namer_test.cc diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index e830366d81..f4105bafdd 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -21,6 +21,7 @@ #include "src/ast/identifier_expression.h" #include "src/ast/return_statement.h" #include "src/ast/sint_literal.h" +#include "src/ast/switch_statement.h" #include "src/ast/uint_literal.h" #include "src/ast/unary_op_expression.h" @@ -317,11 +318,38 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsReturn()) { return EmitReturn(stmt->AsReturn()); } + if (stmt->IsSwitch()) { + return EmitSwitch(stmt->AsSwitch()); + } error_ = "unknown statement type: " + stmt->str(); return false; } +bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) { + make_indent(); + + out_ << "switch("; + if (!EmitExpression(stmt->condition())) { + return false; + } + out_ << ") {" << std::endl; + + increment_indent(); + + for (const auto& s : stmt->body()) { + if (!EmitCase(s.get())) { + return false; + } + } + + decrement_indent(); + make_indent(); + out_ << "}" << std::endl; + + return true; +} + bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { switch (expr->op()) { case ast::UnaryOp::kNot: diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index b11e61e50b..8e4f546495 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -72,6 +72,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was emitted bool EmitStatement(ast::Statement* stmt); + /// Handles generating a switch statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted + bool EmitSwitch(ast::SwitchStatement* stmt); /// Handles a unary op expression /// @param expr the expression to emit /// @returns true if the expression was emitted diff --git a/src/writer/hlsl/generator_impl_switch_test.cc b/src/writer/hlsl/generator_impl_switch_test.cc new file mode 100644 index 0000000000..1c1719f389 --- /dev/null +++ b/src/writer/hlsl/generator_impl_switch_test.cc @@ -0,0 +1,76 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "gtest/gtest.h" +#include "src/ast/break_statement.h" +#include "src/ast/case_statement.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/module.h" +#include "src/ast/sint_literal.h" +#include "src/ast/switch_statement.h" +#include "src/ast/type/i32_type.h" +#include "src/writer/hlsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace hlsl { +namespace { + +using HlslGeneratorImplTest = testing::Test; + +TEST_F(HlslGeneratorImplTest, Emit_Switch) { + auto def = std::make_unique(); + ast::StatementList def_body; + def_body.push_back(std::make_unique()); + def->set_body(std::move(def_body)); + + ast::type::I32Type i32; + ast::CaseSelectorList case_val; + case_val.push_back(std::make_unique(&i32, 5)); + + ast::StatementList case_body; + case_body.push_back(std::make_unique()); + + auto case_stmt = std::make_unique(std::move(case_val), + std::move(case_body)); + + ast::CaseStatementList body; + body.push_back(std::move(case_stmt)); + body.push_back(std::move(def)); + + auto cond = std::make_unique("cond"); + ast::SwitchStatement s(std::move(cond), std::move(body)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&s)) << g.error(); + EXPECT_EQ(g.result(), R"( switch(cond) { + case 5: { + break; + } + default: { + break; + } + } +)"); +} + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint