[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 <dneto@google.com>
This commit is contained in:
parent
557a0f8e63
commit
41aa1a1ed8
1
BUILD.gn
1
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",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 <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/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::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));
|
||||
|
||||
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
|
Loading…
Reference in New Issue