[hlsl-writer] Emit unary operators.
This CL emits unary operators from the HLSL backend.
Bug: tint:7
Change-Id: I997d89d62d279fc7440ba6045c56e290ec7601c1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25221
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 372d8c3..11a1c8a 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1024,6 +1024,7 @@
"src/writer/hlsl/generator_impl_identifier_test.cc",
"src/writer/hlsl/generator_impl_return_test.cc",
"src/writer/hlsl/generator_impl_test.cc",
+ "src/writer/hlsl/generator_impl_unary_op_test.cc",
"src/writer/hlsl/namer_test.cc",
]
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ca0f3ec..a67b674 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -548,6 +548,7 @@
writer/hlsl/generator_impl_identifier_test.cc
writer/hlsl/generator_impl_return_test.cc
writer/hlsl/generator_impl_test.cc
+ writer/hlsl/generator_impl_unary_op_test.cc
writer/hlsl/namer_test.cc
)
endif()
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index 076aa7b..741110f 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -17,6 +17,7 @@
#include "src/ast/binary_expression.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/return_statement.h"
+#include "src/ast/unary_op_expression.h"
namespace tint {
namespace writer {
@@ -157,6 +158,9 @@
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
}
+ if (expr->IsUnaryOp()) {
+ return EmitUnaryOp(expr->AsUnaryOp());
+ }
error_ = "unknown expression type: " + expr->str();
return false;
@@ -228,6 +232,26 @@
return false;
}
+bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) {
+ switch (expr->op()) {
+ case ast::UnaryOp::kNot:
+ out_ << "!";
+ break;
+ case ast::UnaryOp::kNegation:
+ out_ << "-";
+ break;
+ }
+ out_ << "(";
+
+ if (!EmitExpression(expr->expr())) {
+ return false;
+ }
+
+ out_ << ")";
+
+ return true;
+}
+
} // namespace hlsl
} // namespace writer
} // namespace tint
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index b6565ea..095165c 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -63,6 +63,10 @@
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitStatement(ast::Statement* stmt);
+ /// Handles a unary op expression
+ /// @param expr the expression to emit
+ /// @returns true if the expression was emitted
+ bool EmitUnaryOp(ast::UnaryOpExpression* expr);
/// Checks if the global variable is in an input or output struct
/// @param var the variable to check
diff --git a/src/writer/hlsl/generator_impl_unary_op_test.cc b/src/writer/hlsl/generator_impl_unary_op_test.cc
new file mode 100644
index 0000000..4f93177
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_unary_op_test.cc
@@ -0,0 +1,58 @@
+// 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/identifier_expression.h"
+#include "src/ast/module.h"
+#include "src/ast/unary_op_expression.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+struct UnaryOpData {
+ const char* name;
+ ast::UnaryOp op;
+};
+inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
+ out << data.op;
+ return out;
+}
+using HlslUnaryOpTest = testing::TestWithParam<UnaryOpData>;
+TEST_P(HlslUnaryOpTest, Emit) {
+ auto params = GetParam();
+
+ auto expr = std::make_unique<ast::IdentifierExpression>("expr");
+ ast::UnaryOpExpression op(params.op, std::move(expr));
+
+ ast::Module m;
+ GeneratorImpl g(&m);
+ ASSERT_TRUE(g.EmitExpression(&op)) << g.error();
+ EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
+}
+INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
+ HlslUnaryOpTest,
+ testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
+ UnaryOpData{"-",
+ ast::UnaryOp::kNegation}));
+
+} // namespace
+} // namespace hlsl
+} // namespace writer
+} // namespace tint