From a0b73e8acaf03639e632478ee8d7b72fe02897ce Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 21 Jul 2020 15:58:56 +0000 Subject: [PATCH] [hlsl-writer] Add assignment statement. This CL adds the assign statement emission to the HLSL backend. Bug: tint:7 Change-Id: I3e46ac09170ea1af7444ae89267a82e1d1c42c52 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25224 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/hlsl/generator_impl.cc | 22 +++++++++ src/writer/hlsl/generator_impl.h | 4 ++ src/writer/hlsl/generator_impl_assign_test.cc | 47 +++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 src/writer/hlsl/generator_impl_assign_test.cc diff --git a/BUILD.gn b/BUILD.gn index 2c087a571e..d179b200e5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1018,6 +1018,7 @@ source_set("tint_unittests_msl_writer_src") { source_set("tint_unittests_hlsl_writer_src") { sources = [ + "src/writer/hlsl/generator_impl_assign_test.cc", "src/writer/hlsl/generator_impl_binary_test.cc", "src/writer/hlsl/generator_impl_break_test.cc", "src/writer/hlsl/generator_impl_case_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c5520c3f57..a3389976bf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -542,6 +542,7 @@ endif() if (${TINT_BUILD_HLSL_WRITER}) list(APPEND TINT_TEST_SRCS + writer/hlsl/generator_impl_assign_test.cc writer/hlsl/generator_impl_binary_test.cc writer/hlsl/generator_impl_break_test.cc writer/hlsl/generator_impl_case_test.cc diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index f4105bafdd..d7718a3b84 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -14,6 +14,7 @@ #include "src/writer/hlsl/generator_impl.h" +#include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/case_statement.h" @@ -72,6 +73,24 @@ std::string GeneratorImpl::current_ep_var_name(VarType type) { return name; } +bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { + make_indent(); + + if (!EmitExpression(stmt->lhs())) { + return false; + } + + out_ << " = "; + + if (!EmitExpression(stmt->rhs())) { + return false; + } + + out_ << ";" << std::endl; + + return true; +} + bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { out_ << "("; @@ -304,6 +323,9 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { } bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { + if (stmt->IsAssign()) { + return EmitAssign(stmt->AsAssign()); + } if (stmt->IsBreak()) { return EmitBreak(stmt->AsBreak()); } diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index 8e4f546495..9931639127 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -36,6 +36,10 @@ class GeneratorImpl : public TextGenerator { /// @returns true on successful generation; false otherwise bool Generate(); + /// Handles an assignment statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted successfully + bool EmitAssign(ast::AssignmentStatement* stmt); /// Handles generating a binary expression /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise diff --git a/src/writer/hlsl/generator_impl_assign_test.cc b/src/writer/hlsl/generator_impl_assign_test.cc new file mode 100644 index 0000000000..b8e551aacc --- /dev/null +++ b/src/writer/hlsl/generator_impl_assign_test.cc @@ -0,0 +1,47 @@ +// 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/assignment_statement.h" +#include "src/ast/identifier_expression.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_Assign) { + auto lhs = std::make_unique("lhs"); + auto rhs = std::make_unique("rhs"); + ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); + EXPECT_EQ(g.result(), " lhs = rhs;\n"); +} + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint