[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>
This commit is contained in:
parent
abfdd22acc
commit
6dab73b5c6
1
BUILD.gn
1
BUILD.gn
|
@ -1024,6 +1024,7 @@ source_set("tint_unittests_hlsl_writer_src") {
|
||||||
"src/writer/hlsl/generator_impl_identifier_test.cc",
|
"src/writer/hlsl/generator_impl_identifier_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_return_test.cc",
|
"src/writer/hlsl/generator_impl_return_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_test.cc",
|
"src/writer/hlsl/generator_impl_test.cc",
|
||||||
|
"src/writer/hlsl/generator_impl_unary_op_test.cc",
|
||||||
"src/writer/hlsl/namer_test.cc",
|
"src/writer/hlsl/namer_test.cc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -548,6 +548,7 @@ if (${TINT_BUILD_HLSL_WRITER})
|
||||||
writer/hlsl/generator_impl_identifier_test.cc
|
writer/hlsl/generator_impl_identifier_test.cc
|
||||||
writer/hlsl/generator_impl_return_test.cc
|
writer/hlsl/generator_impl_return_test.cc
|
||||||
writer/hlsl/generator_impl_test.cc
|
writer/hlsl/generator_impl_test.cc
|
||||||
|
writer/hlsl/generator_impl_unary_op_test.cc
|
||||||
writer/hlsl/namer_test.cc
|
writer/hlsl/namer_test.cc
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "src/ast/binary_expression.h"
|
#include "src/ast/binary_expression.h"
|
||||||
#include "src/ast/identifier_expression.h"
|
#include "src/ast/identifier_expression.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
|
#include "src/ast/unary_op_expression.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace writer {
|
namespace writer {
|
||||||
|
@ -157,6 +158,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
||||||
if (expr->IsIdentifier()) {
|
if (expr->IsIdentifier()) {
|
||||||
return EmitIdentifier(expr->AsIdentifier());
|
return EmitIdentifier(expr->AsIdentifier());
|
||||||
}
|
}
|
||||||
|
if (expr->IsUnaryOp()) {
|
||||||
|
return EmitUnaryOp(expr->AsUnaryOp());
|
||||||
|
}
|
||||||
|
|
||||||
error_ = "unknown expression type: " + expr->str();
|
error_ = "unknown expression type: " + expr->str();
|
||||||
return false;
|
return false;
|
||||||
|
@ -228,6 +232,26 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
return false;
|
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 hlsl
|
||||||
} // namespace writer
|
} // namespace writer
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
|
|
@ -63,6 +63,10 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param stmt the statement to emit
|
/// @param stmt the statement to emit
|
||||||
/// @returns true if the statement was emitted
|
/// @returns true if the statement was emitted
|
||||||
bool EmitStatement(ast::Statement* stmt);
|
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
|
/// Checks if the global variable is in an input or output struct
|
||||||
/// @param var the variable to check
|
/// @param var the variable to check
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue