diff --git a/BUILD.gn b/BUILD.gn index 381c9f877c..677a1ccfa8 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -845,6 +845,7 @@ source_set("tint_unittests_wgsl_writer_src") { "src/writer/wgsl/generator_impl_array_accessor_test.cc", "src/writer/wgsl/generator_impl_as_test.cc", "src/writer/wgsl/generator_impl_assign_test.cc", + "src/writer/wgsl/generator_impl_binary_test.cc", "src/writer/wgsl/generator_impl_break_test.cc", "src/writer/wgsl/generator_impl_call_test.cc", "src/writer/wgsl/generator_impl_case_test.cc", @@ -861,7 +862,6 @@ source_set("tint_unittests_wgsl_writer_src") { "src/writer/wgsl/generator_impl_kill_test.cc", "src/writer/wgsl/generator_impl_loop_test.cc", "src/writer/wgsl/generator_impl_member_accessor_test.cc", - "src/writer/wgsl/generator_impl_relational_test.cc", "src/writer/wgsl/generator_impl_return_test.cc", "src/writer/wgsl/generator_impl_switch_test.cc", "src/writer/wgsl/generator_impl_test.cc", @@ -886,6 +886,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_binary_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 82a51a40e3..6659416c9a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -465,6 +465,7 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_array_accessor_test.cc writer/wgsl/generator_impl_as_test.cc writer/wgsl/generator_impl_assign_test.cc + writer/wgsl/generator_impl_binary_test.cc writer/wgsl/generator_impl_break_test.cc writer/wgsl/generator_impl_call_test.cc writer/wgsl/generator_impl_case_test.cc @@ -481,7 +482,6 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_kill_test.cc writer/wgsl/generator_impl_loop_test.cc writer/wgsl/generator_impl_member_accessor_test.cc - writer/wgsl/generator_impl_relational_test.cc writer/wgsl/generator_impl_return_test.cc writer/wgsl/generator_impl_switch_test.cc writer/wgsl/generator_impl_type_test.cc @@ -494,6 +494,7 @@ endif() if(${TINT_BUILD_MSL_WRITER}) list(APPEND TINT_TEST_SRCS writer/msl/generator_impl_assign_test.cc + writer/msl/generator_impl_binary_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 ac1bc74fb5..b399d45d42 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -15,6 +15,7 @@ #include "src/writer/msl/generator_impl.h" #include "src/ast/assignment_statement.h" +#include "src/ast/binary_expression.h" #include "src/ast/function.h" #include "src/ast/identifier_expression.h" #include "src/ast/return_statement.h" @@ -66,7 +67,91 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { return true; } +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: + out_ << "&&"; + break; + case ast::BinaryOp::kLogicalOr: + out_ << "||"; + break; + 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_ << ">>"; + 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()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 8617c905db..5ef440701f 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -41,6 +41,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully bool EmitAssign(ast::AssignmentStatement* stmt); + /// 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 diff --git a/src/writer/msl/generator_impl_binary_test.cc b/src/writer/msl/generator_impl_binary_test.cc new file mode 100644 index 0000000000..017e6accd9 --- /dev/null +++ b/src/writer/msl/generator_impl_binary_test.cc @@ -0,0 +1,74 @@ +// 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 "gtest/gtest.h" +#include "src/ast/binary_expression.h" +#include "src/ast/identifier_expression.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +struct BinaryData { + const char* result; + ast::BinaryOp op; +}; +inline std::ostream& operator<<(std::ostream& out, BinaryData data) { + out << data.op; + return out; +} +using MslBinaryTest = testing::TestWithParam; +TEST_P(MslBinaryTest, Emit) { + auto params = GetParam(); + + auto left = std::make_unique("left"); + auto right = std::make_unique("right"); + + ast::BinaryExpression expr(params.op, std::move(left), std::move(right)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); + EXPECT_EQ(g.result(), params.result); +} +INSTANTIATE_TEST_SUITE_P( + MslGeneratorImplTest, + MslBinaryTest, + 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 msl +} // namespace writer +} // namespace tint diff --git a/src/writer/wgsl/generator_impl_relational_test.cc b/src/writer/wgsl/generator_impl_binary_test.cc similarity index 96% rename from src/writer/wgsl/generator_impl_relational_test.cc rename to src/writer/wgsl/generator_impl_binary_test.cc index 465c149c47..2858f77cb1 100644 --- a/src/writer/wgsl/generator_impl_relational_test.cc +++ b/src/writer/wgsl/generator_impl_binary_test.cc @@ -32,8 +32,8 @@ inline std::ostream& operator<<(std::ostream& out, BinaryData data) { out << data.op; return out; } -using BinaryTest = testing::TestWithParam; -TEST_P(BinaryTest, Emit) { +using WgslBinaryTest = testing::TestWithParam; +TEST_P(WgslBinaryTest, Emit) { auto params = GetParam(); auto left = std::make_unique("left"); @@ -47,7 +47,7 @@ TEST_P(BinaryTest, Emit) { } INSTANTIATE_TEST_SUITE_P( WgslGeneratorImplTest, - BinaryTest, + WgslBinaryTest, testing::Values( BinaryData{"(left & right)", ast::BinaryOp::kAnd}, BinaryData{"(left | right)", ast::BinaryOp::kOr},