[hlsl-writer] Emit case statements.
This CL adds emission of case statements to the HLSL backend. Bug: tint:7 Change-Id: I5d0dd7ecfe4ef032a03777c29f3d0d00e584a93a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25222 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
6dab73b5c6
commit
557a0f8e63
1
BUILD.gn
1
BUILD.gn
|
@ -1020,6 +1020,7 @@ source_set("tint_unittests_hlsl_writer_src") {
|
||||||
sources = [
|
sources = [
|
||||||
"src/writer/hlsl/generator_impl_binary_test.cc",
|
"src/writer/hlsl/generator_impl_binary_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_break_test.cc",
|
"src/writer/hlsl/generator_impl_break_test.cc",
|
||||||
|
"src/writer/hlsl/generator_impl_case_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_continue_test.cc",
|
"src/writer/hlsl/generator_impl_continue_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_identifier_test.cc",
|
"src/writer/hlsl/generator_impl_identifier_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_return_test.cc",
|
"src/writer/hlsl/generator_impl_return_test.cc",
|
||||||
|
|
|
@ -544,6 +544,7 @@ if (${TINT_BUILD_HLSL_WRITER})
|
||||||
list(APPEND TINT_TEST_SRCS
|
list(APPEND TINT_TEST_SRCS
|
||||||
writer/hlsl/generator_impl_binary_test.cc
|
writer/hlsl/generator_impl_binary_test.cc
|
||||||
writer/hlsl/generator_impl_break_test.cc
|
writer/hlsl/generator_impl_break_test.cc
|
||||||
|
writer/hlsl/generator_impl_case_test.cc
|
||||||
writer/hlsl/generator_impl_continue_test.cc
|
writer/hlsl/generator_impl_continue_test.cc
|
||||||
writer/hlsl/generator_impl_identifier_test.cc
|
writer/hlsl/generator_impl_identifier_test.cc
|
||||||
writer/hlsl/generator_impl_return_test.cc
|
writer/hlsl/generator_impl_return_test.cc
|
||||||
|
|
|
@ -15,13 +15,29 @@
|
||||||
#include "src/writer/hlsl/generator_impl.h"
|
#include "src/writer/hlsl/generator_impl.h"
|
||||||
|
|
||||||
#include "src/ast/binary_expression.h"
|
#include "src/ast/binary_expression.h"
|
||||||
|
#include "src/ast/bool_literal.h"
|
||||||
|
#include "src/ast/case_statement.h"
|
||||||
|
#include "src/ast/float_literal.h"
|
||||||
#include "src/ast/identifier_expression.h"
|
#include "src/ast/identifier_expression.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
|
#include "src/ast/sint_literal.h"
|
||||||
|
#include "src/ast/uint_literal.h"
|
||||||
#include "src/ast/unary_op_expression.h"
|
#include "src/ast/unary_op_expression.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace writer {
|
namespace writer {
|
||||||
namespace hlsl {
|
namespace hlsl {
|
||||||
|
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(ast::Module* module) : module_(module) {}
|
GeneratorImpl::GeneratorImpl(ast::Module* module) : module_(module) {}
|
||||||
|
|
||||||
|
@ -145,6 +161,50 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
|
||||||
return true;
|
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::EmitContinue(ast::ContinueStatement*) {
|
bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
|
||||||
make_indent();
|
make_indent();
|
||||||
out_ << "continue;" << std::endl;
|
out_ << "continue;" << std::endl;
|
||||||
|
@ -197,6 +257,31 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitLiteral(ast::Literal* lit) {
|
||||||
|
if (lit->IsBool()) {
|
||||||
|
out_ << (lit->AsBool()->IsTrue() ? "true" : "false");
|
||||||
|
} else if (lit->IsFloat()) {
|
||||||
|
auto flags = out_.flags();
|
||||||
|
auto precision = out_.precision();
|
||||||
|
|
||||||
|
out_.flags(flags | std::ios_base::showpoint);
|
||||||
|
out_.precision(std::numeric_limits<float>::max_digits10);
|
||||||
|
|
||||||
|
out_ << lit->AsFloat()->value() << "f";
|
||||||
|
|
||||||
|
out_.precision(precision);
|
||||||
|
out_.flags(flags);
|
||||||
|
} else if (lit->IsSint()) {
|
||||||
|
out_ << lit->AsSint()->value();
|
||||||
|
} else if (lit->IsUint()) {
|
||||||
|
out_ << lit->AsUint()->value() << "u";
|
||||||
|
} else {
|
||||||
|
error_ = "unknown literal type";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
|
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
|
||||||
make_indent();
|
make_indent();
|
||||||
|
|
||||||
|
@ -224,6 +309,11 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
if (stmt->IsContinue()) {
|
if (stmt->IsContinue()) {
|
||||||
return EmitContinue(stmt->AsContinue());
|
return EmitContinue(stmt->AsContinue());
|
||||||
}
|
}
|
||||||
|
if (stmt->IsFallthrough()) {
|
||||||
|
make_indent();
|
||||||
|
out_ << "/* fallthrough */" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if (stmt->IsReturn()) {
|
if (stmt->IsReturn()) {
|
||||||
return EmitReturn(stmt->AsReturn());
|
return EmitReturn(stmt->AsReturn());
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#ifndef SRC_WRITER_HLSL_GENERATOR_IMPL_H_
|
#ifndef SRC_WRITER_HLSL_GENERATOR_IMPL_H_
|
||||||
#define SRC_WRITER_HLSL_GENERATOR_IMPL_H_
|
#define SRC_WRITER_HLSL_GENERATOR_IMPL_H_
|
||||||
|
|
||||||
|
#include "src/ast/literal.h"
|
||||||
#include "src/ast/module.h"
|
#include "src/ast/module.h"
|
||||||
#include "src/scope_stack.h"
|
#include "src/scope_stack.h"
|
||||||
#include "src/writer/hlsl/namer.h"
|
#include "src/writer/hlsl/namer.h"
|
||||||
|
@ -43,6 +44,10 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param stmt the statement to emit
|
/// @param stmt the statement to emit
|
||||||
/// @returns true if the statement was emitted successfully
|
/// @returns true if the statement was emitted successfully
|
||||||
bool EmitBreak(ast::BreakStatement* stmt);
|
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 a continue statement
|
/// Handles a continue statement
|
||||||
/// @param stmt the statement to emit
|
/// @param stmt the statement to emit
|
||||||
/// @returns true if the statement was emitted successfully
|
/// @returns true if the statement was emitted successfully
|
||||||
|
@ -51,6 +56,10 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param expr the expression
|
/// @param expr the expression
|
||||||
/// @returns true if the expression was emitted
|
/// @returns true if the expression was emitted
|
||||||
bool EmitExpression(ast::Expression* expr);
|
bool EmitExpression(ast::Expression* expr);
|
||||||
|
/// Handles a literal
|
||||||
|
/// @param lit the literal to emit
|
||||||
|
/// @returns true if the literal was successfully emitted
|
||||||
|
bool EmitLiteral(ast::Literal* lit);
|
||||||
/// Handles generating an identifier expression
|
/// Handles generating an identifier expression
|
||||||
/// @param expr the identifier expression
|
/// @param expr the identifier expression
|
||||||
/// @returns true if the identifeir was emitted
|
/// @returns true if the identifeir was emitted
|
||||||
|
|
|
@ -0,0 +1,141 @@
|
||||||
|
// 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/module.h"
|
||||||
|
#include "src/ast/sint_literal.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_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));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( case 5: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, 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));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( case 5: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, 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));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( case 5: {
|
||||||
|
/* fallthrough */
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, 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));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( case 5:
|
||||||
|
case 6: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_Case_Default) {
|
||||||
|
ast::CaseStatement c;
|
||||||
|
|
||||||
|
ast::StatementList body;
|
||||||
|
body.push_back(std::make_unique<ast::BreakStatement>());
|
||||||
|
|
||||||
|
c.set_body(std::move(body));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( default: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace hlsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
Loading…
Reference in New Issue