[msl-writer] Emit switch statements.

This CL adds support for switch and case statements into the MSL writer.

Bug: tint:8
Change-Id: Ib51f943e8476c0ecfd45e9131404f96934c6e21f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24180
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-30 21:33:20 +00:00 committed by David Neto
parent 2c11bd6b68
commit 53b3283c6a
6 changed files with 310 additions and 0 deletions

View File

@ -890,6 +890,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_assign_test.cc",
"src/writer/msl/generator_impl_binary_test.cc",
"src/writer/msl/generator_impl_break_test.cc",
"src/writer/msl/generator_impl_case_test.cc",
"src/writer/msl/generator_impl_cast_test.cc",
"src/writer/msl/generator_impl_constructor_test.cc",
"src/writer/msl/generator_impl_continue_test.cc",
@ -900,6 +901,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_loop_test.cc",
"src/writer/msl/generator_impl_member_accessor_test.cc",
"src/writer/msl/generator_impl_return_test.cc",
"src/writer/msl/generator_impl_switch_test.cc",
"src/writer/msl/generator_impl_test.cc",
"src/writer/msl/generator_impl_type_test.cc",
"src/writer/msl/generator_impl_unary_op_test.cc",

View File

@ -498,6 +498,7 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator_impl_assign_test.cc
writer/msl/generator_impl_binary_test.cc
writer/msl/generator_impl_break_test.cc
writer/msl/generator_impl_case_test.cc
writer/msl/generator_impl_cast_test.cc
writer/msl/generator_impl_constructor_test.cc
writer/msl/generator_impl_continue_test.cc
@ -508,6 +509,7 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator_impl_loop_test.cc
writer/msl/generator_impl_member_accessor_test.cc
writer/msl/generator_impl_return_test.cc
writer/msl/generator_impl_switch_test.cc
writer/msl/generator_impl_test.cc
writer/msl/generator_impl_type_test.cc
writer/msl/generator_impl_unary_op_test.cc

View File

@ -20,6 +20,7 @@
#include "src/ast/binary_expression.h"
#include "src/ast/bool_literal.h"
#include "src/ast/break_statement.h"
#include "src/ast/case_statement.h"
#include "src/ast/cast_expression.h"
#include "src/ast/continue_statement.h"
#include "src/ast/else_statement.h"
@ -31,6 +32,7 @@
#include "src/ast/member_accessor_expression.h"
#include "src/ast/return_statement.h"
#include "src/ast/sint_literal.h"
#include "src/ast/switch_statement.h"
#include "src/ast/type/alias_type.h"
#include "src/ast/type/array_type.h"
#include "src/ast/type/bool_type.h"
@ -48,6 +50,17 @@
namespace tint {
namespace writer {
namespace msl {
namespace {
bool last_is_break_or_fallthrough(const ast::StatementList& stmts) {
if (stmts.empty()) {
return false;
}
return stmts.back()->IsBreak() || stmts.back()->IsFallthrough();
}
} // namespace
GeneratorImpl::GeneratorImpl() = default;
@ -222,6 +235,50 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
return true;
}
bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) {
make_indent();
if (stmt->IsDefault()) {
out_ << "default:";
} else {
bool first = true;
for (const auto& selector : stmt->selectors()) {
if (!first) {
out_ << std::endl;
make_indent();
}
first = false;
out_ << "case ";
if (!EmitLiteral(selector.get())) {
return false;
}
out_ << ":";
}
}
out_ << " {" << std::endl;
increment_indent();
for (const auto& s : stmt->body()) {
if (!EmitStatement(s.get())) {
return false;
}
}
if (!last_is_break_or_fallthrough(stmt->body())) {
make_indent();
out_ << "break;" << std::endl;
}
decrement_indent();
make_indent();
out_ << "}" << std::endl;
return true;
}
bool GeneratorImpl::EmitCast(ast::CastExpression* expr) {
if (!EmitType(expr->type(), "")) {
return false;
@ -567,6 +624,11 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsContinue()) {
return EmitContinue(stmt->AsContinue());
}
if (stmt->IsFallthrough()) {
make_indent();
out_ << "/* fallthrough */" << std::endl;
return true;
}
if (stmt->IsIf()) {
return EmitIf(stmt->AsIf());
}
@ -579,11 +641,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";
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::EmitType(ast::type::Type* type, const std::string& name) {
if (type->IsAlias()) {
auto* alias = type->AsAlias();

View File

@ -64,6 +64,10 @@ class GeneratorImpl : public TextGenerator {
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBreak(ast::BreakStatement* stmt);
/// Handles a case statement
/// @param stmt the statement
/// @returns true if the statment was emitted successfully
bool EmitCase(ast::CaseStatement* stmt);
/// Handles generating a cast expression
/// @param expr the cast expression
/// @returns true if the cast was emitted
@ -135,6 +139,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 generating type
/// @param type the type to generate
/// @param name the name of the variable, only used for array emission

View File

@ -0,0 +1,135 @@
// 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 <memory>
#include "gtest/gtest.h"
#include "src/ast/break_statement.h"
#include "src/ast/case_statement.h"
#include "src/ast/fallthrough_statement.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/sint_literal.h"
#include "src/ast/type/i32_type.h"
#include "src/writer/msl/generator_impl.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = testing::Test;
TEST_F(MslGeneratorImplTest, Emit_Case) {
ast::type::I32Type i32;
ast::StatementList body;
body.push_back(std::make_unique<ast::BreakStatement>());
ast::CaseSelectorList lit;
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
ast::CaseStatement c(std::move(lit), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
EXPECT_EQ(g.result(), R"( case 5: {
break;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Case_BreaksByDefault) {
ast::type::I32Type i32;
ast::StatementList body;
ast::CaseSelectorList lit;
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
ast::CaseStatement c(std::move(lit), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
EXPECT_EQ(g.result(), R"( case 5: {
break;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Case_WithFallthrough) {
ast::type::I32Type i32;
ast::StatementList body;
body.push_back(std::make_unique<ast::FallthroughStatement>());
ast::CaseSelectorList lit;
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
ast::CaseStatement c(std::move(lit), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
EXPECT_EQ(g.result(), R"( case 5: {
/* fallthrough */
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Case_MultipleSelectors) {
ast::type::I32Type i32;
ast::StatementList body;
body.push_back(std::make_unique<ast::BreakStatement>());
ast::CaseSelectorList lit;
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 6));
ast::CaseStatement c(std::move(lit), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
EXPECT_EQ(g.result(), R"( case 5:
case 6: {
break;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Case_Default) {
ast::CaseStatement c;
ast::StatementList body;
body.push_back(std::make_unique<ast::BreakStatement>());
c.set_body(std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
EXPECT_EQ(g.result(), R"( default: {
break;
}
)");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint

View File

@ -0,0 +1,74 @@
// 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 <memory>
#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/sint_literal.h"
#include "src/ast/switch_statement.h"
#include "src/ast/type/i32_type.h"
#include "src/writer/msl/generator_impl.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = testing::Test;
TEST_F(MslGeneratorImplTest, Emit_Switch) {
auto def = std::make_unique<ast::CaseStatement>();
ast::StatementList def_body;
def_body.push_back(std::make_unique<ast::BreakStatement>());
def->set_body(std::move(def_body));
ast::type::I32Type i32;
ast::CaseSelectorList case_val;
case_val.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
ast::StatementList case_body;
case_body.push_back(std::make_unique<ast::BreakStatement>());
auto case_stmt = std::make_unique<ast::CaseStatement>(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<ast::IdentifierExpression>("cond");
ast::SwitchStatement s(std::move(cond), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&s)) << g.error();
EXPECT_EQ(g.result(), R"( switch(cond) {
case 5: {
break;
}
default: {
break;
}
}
)");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint