From b1de84316c4d34328b22bbd820dba04b4ca5f70c Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Fri, 26 Jun 2020 21:27:59 +0000 Subject: [PATCH] [msl-writer] Add cast conversion. This CL adds conversion of casts to MSL. Bug: tint:8 Change-Id: Iecfb9a5b413b1d10372b4d2fec31c0956b1475a0 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23822 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/msl/generator_impl.cc | 20 +++++++- src/writer/msl/generator_impl.h | 4 ++ src/writer/msl/generator_impl_cast_test.cc | 56 ++++++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/writer/msl/generator_impl_cast_test.cc diff --git a/BUILD.gn b/BUILD.gn index ae14e27494..e90aa057b1 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -888,6 +888,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_as_test.cc", "src/writer/msl/generator_impl_assign_test.cc", "src/writer/msl/generator_impl_binary_test.cc", + "src/writer/msl/generator_impl_cast_test.cc", "src/writer/msl/generator_impl_constructor_test.cc", "src/writer/msl/generator_impl_function_test.cc", "src/writer/msl/generator_impl_identifier_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b16315c804..e1cd7a4991 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -496,6 +496,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_as_test.cc writer/msl/generator_impl_assign_test.cc writer/msl/generator_impl_binary_test.cc + writer/msl/generator_impl_cast_test.cc writer/msl/generator_impl_constructor_test.cc writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_identifier_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index b82090a765..252e90290c 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -18,6 +18,7 @@ #include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" +#include "src/ast/cast_expression.h" #include "src/ast/float_literal.h" #include "src/ast/function.h" #include "src/ast/identifier_expression.h" @@ -141,8 +142,9 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { // implementation-defined behaviour for negative LHS. We may have to // generate extra code to implement WGSL-specified behaviour for negative // LHS. - out_ << ">>"; + out_ << R"(>>)"; break; + case ast::BinaryOp::kAdd: out_ << "+"; break; @@ -172,6 +174,19 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { return true; } +bool GeneratorImpl::EmitCast(ast::CastExpression* expr) { + if (!EmitType(expr->type(), "")) { + return false; + } + + out_ << "("; + if (!EmitExpression(expr->expr())) { + return false; + } + out_ << ")"; + return true; +} + bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) { if (expr->IsScalarConstructor()) { return EmitScalarConstructor(expr->AsScalarConstructor()); @@ -239,6 +254,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsBinary()) { return EmitBinary(expr->AsBinary()); } + if (expr->IsCast()) { + return EmitCast(expr->AsCast()); + } if (expr->IsConstructor()) { return EmitConstructor(expr->AsConstructor()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 3f1cba7101..3c3b1b5c0e 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -52,6 +52,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise bool EmitBinary(ast::BinaryExpression* expr); + /// Handles generating a cast expression + /// @param expr the cast expression + /// @returns true if the cast was emitted + bool EmitCast(ast::CastExpression* expr); /// Handles generating constructor expressions /// @param expr the constructor expression /// @returns true if the expression was emitted diff --git a/src/writer/msl/generator_impl_cast_test.cc b/src/writer/msl/generator_impl_cast_test.cc new file mode 100644 index 0000000000..04dece4f78 --- /dev/null +++ b/src/writer/msl/generator_impl_cast_test.cc @@ -0,0 +1,56 @@ +// 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/cast_expression.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/vector_type.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Scalar) { + ast::type::F32Type f32; + auto id = std::make_unique("id"); + ast::CastExpression cast(&f32, std::move(id)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&cast)) << g.error(); + EXPECT_EQ(g.result(), "float(id)"); +} + +TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Vector) { + ast::type::F32Type f32; + ast::type::VectorType vec3(&f32, 3); + + auto id = std::make_unique("id"); + ast::CastExpression cast(&vec3, std::move(id)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&cast)) << g.error(); + EXPECT_EQ(g.result(), "float3(id)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint