diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e7ae9cf1e3..31cf5d29a8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -419,6 +419,7 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_import_test.cc writer/wgsl/generator_impl_initializer_test.cc writer/wgsl/generator_impl_kill_test.cc + writer/wgsl/generator_impl_loop_test.cc writer/wgsl/generator_impl_member_accessor_test.cc writer/wgsl/generator_impl_nop_test.cc writer/wgsl/generator_impl_regardless_test.cc diff --git a/src/ast/loop_statement.h b/src/ast/loop_statement.h index 917eaa6e0d..73e8b7f942 100644 --- a/src/ast/loop_statement.h +++ b/src/ast/loop_statement.h @@ -62,6 +62,8 @@ class LoopStatement : public Statement { const std::vector>& continuing() const { return continuing_; } + /// @returns true if there are continuing statements in the loop + bool has_continuing() const { return !continuing_.empty(); } /// @returns true if this is a loop statement bool IsLoop() const override { return true; } diff --git a/src/ast/loop_statement_test.cc b/src/ast/loop_statement_test.cc index d49636fca6..09455da0b4 100644 --- a/src/ast/loop_statement_test.cc +++ b/src/ast/loop_statement_test.cc @@ -60,6 +60,25 @@ TEST_F(LoopStatementTest, IsLoop) { EXPECT_TRUE(l.IsLoop()); } +TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) { + std::vector> body; + body.push_back(std::make_unique()); + + LoopStatement l(std::move(body), {}); + EXPECT_FALSE(l.has_continuing()); +} + +TEST_F(LoopStatementTest, HasContinuing_WithContinuing) { + std::vector> body; + body.push_back(std::make_unique()); + + std::vector> continuing; + continuing.push_back(std::make_unique()); + + LoopStatement l(std::move(body), std::move(continuing)); + EXPECT_TRUE(l.has_continuing()); +} + TEST_F(LoopStatementTest, IsValid) { std::vector> body; body.push_back(std::make_unique()); diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 4ad4db9046..08373377cf 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -35,6 +35,7 @@ #include "src/ast/initializer_expression.h" #include "src/ast/int_literal.h" #include "src/ast/location_decoration.h" +#include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/regardless_statement.h" #include "src/ast/relational_expression.h" @@ -671,6 +672,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsKill()) { return EmitKill(stmt->AsKill()); } + if (stmt->IsLoop()) { + return EmitLoop(stmt->AsLoop()); + } if (stmt->IsNop()) { return EmitNop(stmt->AsNop()); } @@ -803,6 +807,43 @@ bool GeneratorImpl::EmitKill(ast::KillStatement*) { return true; } +bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { + make_indent(); + + out_ << "loop {" << std::endl; + increment_indent(); + + for (const auto& s : stmt->body()) { + if (!EmitStatement(s.get())) { + return false; + } + } + + if (stmt->has_continuing()) { + out_ << std::endl; + + make_indent(); + out_ << "continuing {" << std::endl; + + increment_indent(); + for (const auto& s : stmt->continuing()) { + if (!EmitStatement(s.get())) { + return false; + } + } + + decrement_indent(); + make_indent(); + out_ << "}" << std::endl; + } + + decrement_indent(); + make_indent(); + out_ << "}" << std::endl; + + return true; +} + bool GeneratorImpl::EmitNop(ast::NopStatement*) { make_indent(); out_ << "nop;" << std::endl; diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 62425f84d2..9b1b42b034 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -138,6 +138,10 @@ class GeneratorImpl { /// @param lit the literal to emit /// @returns true if the literal was successfully emitted bool EmitLiteral(ast::Literal* lit); + /// Handles a loop statement + /// @param stmt the statement to emit + /// @returns true if the statement was emtited + bool EmitLoop(ast::LoopStatement* stmt); /// Handles a member accessor expression /// @param expr the member accessor expression /// @returns true if the member accessor was emitted diff --git a/src/writer/wgsl/generator_impl_loop_test.cc b/src/writer/wgsl/generator_impl_loop_test.cc new file mode 100644 index 0000000000..beb2a5ccd3 --- /dev/null +++ b/src/writer/wgsl/generator_impl_loop_test.cc @@ -0,0 +1,73 @@ +// 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 + +#include "gtest/gtest.h" +#include "src/ast/kill_statement.h" +#include "src/ast/loop_statement.h" +#include "src/ast/nop_statement.h" +#include "src/writer/wgsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace wgsl { +namespace { + +using GeneratorImplTest = testing::Test; + +TEST_F(GeneratorImplTest, Emit_Loop) { + std::vector> body; + body.push_back(std::make_unique()); + + ast::LoopStatement l(std::move(body), {}); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&l)) << g.error(); + EXPECT_EQ(g.result(), R"( loop { + kill; + } +)"); +} + +TEST_F(GeneratorImplTest, Emit_LoopWithContinuing) { + std::vector> body; + body.push_back(std::make_unique()); + + std::vector> continuing; + continuing.push_back(std::make_unique()); + + ast::LoopStatement l(std::move(body), std::move(continuing)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&l)) << g.error(); + EXPECT_EQ(g.result(), R"( loop { + kill; + + continuing { + nop; + } + } +)"); +} + +} // namespace +} // namespace wgsl +} // namespace writer +} // namespace tint