mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-07-06 05:06:07 +00:00
[hlsl-writer] Emit break, return and continue.
This CL updates the HLSL writer to emit break, return and continue statements. Bug: tint:7 Change-Id: I03eafc343e57e9e8d1efaf930023099d6f85fc57 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25220 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
dee9136c20
commit
abfdd22acc
3
BUILD.gn
3
BUILD.gn
@ -1019,7 +1019,10 @@ source_set("tint_unittests_msl_writer_src") {
|
|||||||
source_set("tint_unittests_hlsl_writer_src") {
|
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_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_test.cc",
|
"src/writer/hlsl/generator_impl_test.cc",
|
||||||
"src/writer/hlsl/namer_test.cc",
|
"src/writer/hlsl/namer_test.cc",
|
||||||
]
|
]
|
||||||
|
@ -543,7 +543,10 @@ endif()
|
|||||||
if (${TINT_BUILD_HLSL_WRITER})
|
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_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_test.cc
|
writer/hlsl/generator_impl_test.cc
|
||||||
writer/hlsl/namer_test.cc
|
writer/hlsl/namer_test.cc
|
||||||
)
|
)
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#include "src/ast/binary_expression.h"
|
#include "src/ast/binary_expression.h"
|
||||||
#include "src/ast/identifier_expression.h"
|
#include "src/ast/identifier_expression.h"
|
||||||
|
#include "src/ast/return_statement.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace writer {
|
namespace writer {
|
||||||
@ -137,6 +138,18 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
|
||||||
|
make_indent();
|
||||||
|
out_ << "break;" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
|
||||||
|
make_indent();
|
||||||
|
out_ << "continue;" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
||||||
if (expr->IsBinary()) {
|
if (expr->IsBinary()) {
|
||||||
return EmitBinary(expr->AsBinary());
|
return EmitBinary(expr->AsBinary());
|
||||||
@ -180,6 +193,41 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
|
||||||
|
make_indent();
|
||||||
|
|
||||||
|
out_ << "return";
|
||||||
|
|
||||||
|
if (generating_entry_point_) {
|
||||||
|
auto out_data = ep_name_to_out_data_.find(current_ep_name_);
|
||||||
|
if (out_data != ep_name_to_out_data_.end()) {
|
||||||
|
out_ << " " << out_data->second.var_name;
|
||||||
|
}
|
||||||
|
} else if (stmt->has_value()) {
|
||||||
|
out_ << " ";
|
||||||
|
if (!EmitExpression(stmt->value())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out_ << ";" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
|
if (stmt->IsBreak()) {
|
||||||
|
return EmitBreak(stmt->AsBreak());
|
||||||
|
}
|
||||||
|
if (stmt->IsContinue()) {
|
||||||
|
return EmitContinue(stmt->AsContinue());
|
||||||
|
}
|
||||||
|
if (stmt->IsReturn()) {
|
||||||
|
return EmitReturn(stmt->AsReturn());
|
||||||
|
}
|
||||||
|
|
||||||
|
error_ = "unknown statement type: " + stmt->str();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace hlsl
|
} // namespace hlsl
|
||||||
} // namespace writer
|
} // namespace writer
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
@ -39,6 +39,14 @@ class GeneratorImpl : public TextGenerator {
|
|||||||
/// @param expr the binary expression
|
/// @param expr the binary expression
|
||||||
/// @returns true if the expression was emitted, false otherwise
|
/// @returns true if the expression was emitted, false otherwise
|
||||||
bool EmitBinary(ast::BinaryExpression* expr);
|
bool EmitBinary(ast::BinaryExpression* expr);
|
||||||
|
/// Handles a break statement
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was emitted successfully
|
||||||
|
bool EmitBreak(ast::BreakStatement* stmt);
|
||||||
|
/// Handles a continue statement
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was emitted successfully
|
||||||
|
bool EmitContinue(ast::ContinueStatement* stmt);
|
||||||
/// Handles generate an Expression
|
/// Handles generate an Expression
|
||||||
/// @param expr the expression
|
/// @param expr the expression
|
||||||
/// @returns true if the expression was emitted
|
/// @returns true if the expression was emitted
|
||||||
@ -47,6 +55,14 @@ class GeneratorImpl : public TextGenerator {
|
|||||||
/// @param expr the identifier expression
|
/// @param expr the identifier expression
|
||||||
/// @returns true if the identifeir was emitted
|
/// @returns true if the identifeir was emitted
|
||||||
bool EmitIdentifier(ast::IdentifierExpression* expr);
|
bool EmitIdentifier(ast::IdentifierExpression* expr);
|
||||||
|
/// Handles return statements
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was successfully emitted
|
||||||
|
bool EmitReturn(ast::ReturnStatement* stmt);
|
||||||
|
/// Handles statement
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was emitted
|
||||||
|
bool EmitStatement(ast::Statement* stmt);
|
||||||
|
|
||||||
/// Checks if the global variable is in an input or output struct
|
/// Checks if the global variable is in an input or output struct
|
||||||
/// @param var the variable to check
|
/// @param var the variable to check
|
||||||
@ -66,6 +82,7 @@ class GeneratorImpl : public TextGenerator {
|
|||||||
Namer namer_;
|
Namer namer_;
|
||||||
ast::Module* module_ = nullptr;
|
ast::Module* module_ = nullptr;
|
||||||
std::string current_ep_name_;
|
std::string current_ep_name_;
|
||||||
|
bool generating_entry_point_ = false;
|
||||||
ScopeStack<ast::Variable*> global_variables_;
|
ScopeStack<ast::Variable*> global_variables_;
|
||||||
std::unordered_map<std::string, EntryPointData> ep_name_to_in_data_;
|
std::unordered_map<std::string, EntryPointData> ep_name_to_in_data_;
|
||||||
std::unordered_map<std::string, EntryPointData> ep_name_to_out_data_;
|
std::unordered_map<std::string, EntryPointData> ep_name_to_out_data_;
|
||||||
|
44
src/writer/hlsl/generator_impl_break_test.cc
Normal file
44
src/writer/hlsl/generator_impl_break_test.cc
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// 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 <vector>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "src/ast/break_statement.h"
|
||||||
|
#include "src/ast/module.h"
|
||||||
|
#include "src/writer/hlsl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace hlsl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using HlslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_Break) {
|
||||||
|
ast::BreakStatement b;
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), " break;\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace hlsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
44
src/writer/hlsl/generator_impl_continue_test.cc
Normal file
44
src/writer/hlsl/generator_impl_continue_test.cc
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// 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 <vector>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "src/ast/continue_statement.h"
|
||||||
|
#include "src/ast/module.h"
|
||||||
|
#include "src/writer/hlsl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace hlsl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using HlslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_Continue) {
|
||||||
|
ast::ContinueStatement c;
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), " continue;\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace hlsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
57
src/writer/hlsl/generator_impl_return_test.cc
Normal file
57
src/writer/hlsl/generator_impl_return_test.cc
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// 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 <vector>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "src/ast/identifier_expression.h"
|
||||||
|
#include "src/ast/module.h"
|
||||||
|
#include "src/ast/return_statement.h"
|
||||||
|
#include "src/writer/hlsl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace hlsl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using HlslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_Return) {
|
||||||
|
ast::ReturnStatement r;
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), " return;\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_ReturnWithValue) {
|
||||||
|
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
|
||||||
|
ast::ReturnStatement r(std::move(expr));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), " return expr;\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace hlsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
Loading…
x
Reference in New Issue
Block a user