Emit loop statement.
This CL emits the loop statement from the WGSL generator. Bug: tint:4 Change-Id: I45be066ed306a71df053f7e61e6201a10a3d1056 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17283 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
5f64aee763
commit
0d00402124
|
@ -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
|
||||
|
|
|
@ -62,6 +62,8 @@ class LoopStatement : public Statement {
|
|||
const std::vector<std::unique_ptr<Statement>>& 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; }
|
||||
|
|
|
@ -60,6 +60,25 @@ TEST_F(LoopStatementTest, IsLoop) {
|
|||
EXPECT_TRUE(l.IsLoop());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) {
|
||||
std::vector<std::unique_ptr<Statement>> body;
|
||||
body.push_back(std::make_unique<KillStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), {});
|
||||
EXPECT_FALSE(l.has_continuing());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, HasContinuing_WithContinuing) {
|
||||
std::vector<std::unique_ptr<Statement>> body;
|
||||
body.push_back(std::make_unique<KillStatement>());
|
||||
|
||||
std::vector<std::unique_ptr<Statement>> continuing;
|
||||
continuing.push_back(std::make_unique<NopStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
EXPECT_TRUE(l.has_continuing());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid) {
|
||||
std::vector<std::unique_ptr<Statement>> body;
|
||||
body.push_back(std::make_unique<KillStatement>());
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 <memory>
|
||||
#include <vector>
|
||||
|
||||
#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<std::unique_ptr<ast::Statement>> body;
|
||||
body.push_back(std::make_unique<ast::KillStatement>());
|
||||
|
||||
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<std::unique_ptr<ast::Statement>> body;
|
||||
body.push_back(std::make_unique<ast::KillStatement>());
|
||||
|
||||
std::vector<std::unique_ptr<ast::Statement>> continuing;
|
||||
continuing.push_back(std::make_unique<ast::NopStatement>());
|
||||
|
||||
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
|
Loading…
Reference in New Issue