[hlsl-writer] Add binary operator emission.

Emits the binary operators.

Bug: tint:7
Change-Id: I6f4fce3b6fb646b8340e39538b041715a676ac28
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25164
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-21 15:58:20 +00:00
parent fbbc617888
commit dee9136c20
5 changed files with 170 additions and 0 deletions

View File

@ -1018,6 +1018,7 @@ source_set("tint_unittests_msl_writer_src") {
source_set("tint_unittests_hlsl_writer_src") {
sources = [
"src/writer/hlsl/generator_impl_binary_test.cc",
"src/writer/hlsl/generator_impl_identifier_test.cc",
"src/writer/hlsl/generator_impl_test.cc",
"src/writer/hlsl/namer_test.cc",

View File

@ -542,6 +542,7 @@ endif()
if (${TINT_BUILD_HLSL_WRITER})
list(APPEND TINT_TEST_SRCS
writer/hlsl/generator_impl_binary_test.cc
writer/hlsl/generator_impl_identifier_test.cc
writer/hlsl/generator_impl_test.cc
writer/hlsl/namer_test.cc

View File

@ -14,6 +14,7 @@
#include "src/writer/hlsl/generator_impl.h"
#include "src/ast/binary_expression.h"
#include "src/ast/identifier_expression.h"
namespace tint {
@ -52,7 +53,94 @@ std::string GeneratorImpl::current_ep_var_name(VarType type) {
return name;
}
bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
out_ << "(";
if (!EmitExpression(expr->lhs())) {
return false;
}
out_ << " ";
switch (expr->op()) {
case ast::BinaryOp::kAnd:
out_ << "&";
break;
case ast::BinaryOp::kOr:
out_ << "|";
break;
case ast::BinaryOp::kXor:
out_ << "^";
break;
case ast::BinaryOp::kLogicalAnd:
// TODO(dsinclair): Implement support ...
error_ = "&& not supported yet";
return false;
case ast::BinaryOp::kLogicalOr:
// TODO(dsinclair): Implement support ...
error_ = "|| not supported yet";
return false;
case ast::BinaryOp::kEqual:
out_ << "==";
break;
case ast::BinaryOp::kNotEqual:
out_ << "!=";
break;
case ast::BinaryOp::kLessThan:
out_ << "<";
break;
case ast::BinaryOp::kGreaterThan:
out_ << ">";
break;
case ast::BinaryOp::kLessThanEqual:
out_ << "<=";
break;
case ast::BinaryOp::kGreaterThanEqual:
out_ << ">=";
break;
case ast::BinaryOp::kShiftLeft:
out_ << "<<";
break;
case ast::BinaryOp::kShiftRight:
// TODO(dsinclair): MSL is based on C++14, and >> in C++14 has
// implementation-defined behaviour for negative LHS. We may have to
// generate extra code to implement WGSL-specified behaviour for negative
// LHS.
out_ << R"(>>)";
break;
case ast::BinaryOp::kAdd:
out_ << "+";
break;
case ast::BinaryOp::kSubtract:
out_ << "-";
break;
case ast::BinaryOp::kMultiply:
out_ << "*";
break;
case ast::BinaryOp::kDivide:
out_ << "/";
break;
case ast::BinaryOp::kModulo:
out_ << "%";
break;
case ast::BinaryOp::kNone:
error_ = "missing binary operation type";
return false;
}
out_ << " ";
if (!EmitExpression(expr->rhs())) {
return false;
}
out_ << ")";
return true;
}
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsBinary()) {
return EmitBinary(expr->AsBinary());
}
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
}

View File

@ -35,6 +35,10 @@ class GeneratorImpl : public TextGenerator {
/// @returns true on successful generation; false otherwise
bool Generate();
/// Handles generating a binary expression
/// @param expr the binary expression
/// @returns true if the expression was emitted, false otherwise
bool EmitBinary(ast::BinaryExpression* expr);
/// Handles generate an Expression
/// @param expr the expression
/// @returns true if the expression was emitted

View File

@ -0,0 +1,76 @@
// 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 "gtest/gtest.h"
#include "src/ast/binary_expression.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 {
struct BinaryData {
const char* result;
ast::BinaryOp op;
};
inline std::ostream& operator<<(std::ostream& out, BinaryData data) {
out << data.op;
return out;
}
using HlslBinaryTest = testing::TestWithParam<BinaryData>;
TEST_P(HlslBinaryTest, Emit) {
auto params = GetParam();
auto left = std::make_unique<ast::IdentifierExpression>("left");
auto right = std::make_unique<ast::IdentifierExpression>("right");
ast::BinaryExpression expr(params.op, std::move(left), std::move(right));
ast::Module m;
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
EXPECT_EQ(g.result(), params.result);
}
INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest,
HlslBinaryTest,
testing::Values(
BinaryData{"(left & right)", ast::BinaryOp::kAnd},
BinaryData{"(left | right)", ast::BinaryOp::kOr},
BinaryData{"(left ^ right)", ast::BinaryOp::kXor},
// BinaryData{"(left && right)", ast::BinaryOp::kLogicalAnd},
// BinaryData{"(left || right)", ast::BinaryOp::kLogicalOr},
BinaryData{"(left == right)", ast::BinaryOp::kEqual},
BinaryData{"(left != right)", ast::BinaryOp::kNotEqual},
BinaryData{"(left < right)", ast::BinaryOp::kLessThan},
BinaryData{"(left > right)", ast::BinaryOp::kGreaterThan},
BinaryData{"(left <= right)", ast::BinaryOp::kLessThanEqual},
BinaryData{"(left >= right)", ast::BinaryOp::kGreaterThanEqual},
BinaryData{"(left << right)", ast::BinaryOp::kShiftLeft},
BinaryData{"(left >> right)", ast::BinaryOp::kShiftRight},
BinaryData{"(left + right)", ast::BinaryOp::kAdd},
BinaryData{"(left - right)", ast::BinaryOp::kSubtract},
BinaryData{"(left * right)", ast::BinaryOp::kMultiply},
BinaryData{"(left / right)", ast::BinaryOp::kDivide},
BinaryData{"(left % right)", ast::BinaryOp::kModulo}));
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint