[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 <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-21 15:58:56 +00:00 committed by dan sinclair
parent 41aa1a1ed8
commit a0b73e8aca
5 changed files with 75 additions and 0 deletions

View File

@ -1018,6 +1018,7 @@ 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_assign_test.cc",
"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_break_test.cc",
"src/writer/hlsl/generator_impl_case_test.cc", "src/writer/hlsl/generator_impl_case_test.cc",

View File

@ -542,6 +542,7 @@ 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_assign_test.cc
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_break_test.cc
writer/hlsl/generator_impl_case_test.cc writer/hlsl/generator_impl_case_test.cc

View File

@ -14,6 +14,7 @@
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/generator_impl.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/binary_expression.h" #include "src/ast/binary_expression.h"
#include "src/ast/bool_literal.h" #include "src/ast/bool_literal.h"
#include "src/ast/case_statement.h" #include "src/ast/case_statement.h"
@ -72,6 +73,24 @@ std::string GeneratorImpl::current_ep_var_name(VarType type) {
return name; 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) { bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
out_ << "("; out_ << "(";
@ -304,6 +323,9 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
} }
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign());
}
if (stmt->IsBreak()) { if (stmt->IsBreak()) {
return EmitBreak(stmt->AsBreak()); return EmitBreak(stmt->AsBreak());
} }

View File

@ -36,6 +36,10 @@ class GeneratorImpl : public TextGenerator {
/// @returns true on successful generation; false otherwise /// @returns true on successful generation; false otherwise
bool Generate(); 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 /// Handles generating a binary expression
/// @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

View File

@ -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 <memory>
#include <vector>
#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<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("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