From 70c605fc474f6816f681387c1f98ede63beec0c8 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 23 Jun 2020 17:54:33 +0000 Subject: [PATCH] [metal-writer] Emit assignment statements. This CL adds assignment statements to the Metal backend. Bug: tint:8 Change-Id: Iaf4faa62124948fd0e785d5bebd20aae778ba050 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23705 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/msl/generator_impl.cc | 22 ++++++++++ src/writer/msl/generator_impl.h | 4 ++ src/writer/msl/generator_impl_assign_test.cc | 45 ++++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 src/writer/msl/generator_impl_assign_test.cc diff --git a/BUILD.gn b/BUILD.gn index 77f2c59e4b..381c9f877c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -885,6 +885,7 @@ source_set("tint_unittests_wgsl_writer_src") { source_set("tint_unittests_msl_writer_src") { sources = [ + "src/writer/msl/generator_impl_assign_test.cc", "src/writer/msl/generator_impl_function_test.cc", "src/writer/msl/generator_impl_identifier_test.cc", "src/writer/msl/generator_impl_return_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ba497fc651..82a51a40e3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -493,6 +493,7 @@ endif() if(${TINT_BUILD_MSL_WRITER}) list(APPEND TINT_TEST_SRCS + writer/msl/generator_impl_assign_test.cc writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_identifier_test.cc writer/msl/generator_impl_return_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index a8caafbc5e..ac1bc74fb5 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -14,6 +14,7 @@ #include "src/writer/msl/generator_impl.h" +#include "src/ast/assignment_statement.h" #include "src/ast/function.h" #include "src/ast/identifier_expression.h" #include "src/ast/return_statement.h" @@ -47,6 +48,24 @@ bool GeneratorImpl::Generate(const ast::Module& module) { return true; } +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::EmitExpression(ast::Expression* expr) { if (expr->IsIdentifier()) { return EmitIdentifier(expr->AsIdentifier()); @@ -138,6 +157,9 @@ bool GeneratorImpl::EmitStatementBlockAndNewline( } bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { + if (stmt->IsAssign()) { + return EmitAssign(stmt->AsAssign()); + } if (stmt->IsReturn()) { return EmitReturn(stmt->AsReturn()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index e91b0c5c21..8617c905db 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -37,6 +37,10 @@ class GeneratorImpl : public TextGenerator { /// @returns true on successful generation; false otherwise bool Generate(const ast::Module& module); + /// Handles an assignment statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted successfully + bool EmitAssign(ast::AssignmentStatement* stmt); /// Handles generate an Expression /// @param expr the expression /// @returns true if the expression was emitted diff --git a/src/writer/msl/generator_impl_assign_test.cc b/src/writer/msl/generator_impl_assign_test.cc new file mode 100644 index 0000000000..749ddf8312 --- /dev/null +++ b/src/writer/msl/generator_impl_assign_test.cc @@ -0,0 +1,45 @@ +// 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/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Assign) { + auto lhs = std::make_unique("lhs"); + auto rhs = std::make_unique("rhs"); + ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); + EXPECT_EQ(g.result(), " lhs = rhs;\n"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint