From c5a5f9666f21d5363dbafe34482f7ac5dfc7d05e Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 14 Jul 2020 20:37:38 +0000 Subject: [PATCH] [msl-writer] Handle emitting user function calls. This CL adds support for calling user defined functions from the MSL backend. Intrinsics and imports are not handled yet. Bug: tint:8 Change-Id: I45c3078d014ab89cc0eec76dd626759077e1a890 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24763 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/msl/generator_impl.cc | 47 +++++++++++++++++++ src/writer/msl/generator_impl.h | 4 ++ src/writer/msl/generator_impl_call_test.cc | 53 ++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 src/writer/msl/generator_impl_call_test.cc diff --git a/BUILD.gn b/BUILD.gn index 65890d6fec..533f899a02 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -892,6 +892,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_assign_test.cc", "src/writer/msl/generator_impl_binary_test.cc", "src/writer/msl/generator_impl_break_test.cc", + "src/writer/msl/generator_impl_call_test.cc", "src/writer/msl/generator_impl_case_test.cc", "src/writer/msl/generator_impl_cast_test.cc", "src/writer/msl/generator_impl_constructor_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fe8ba84a4c..d45e6cdb3f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -500,6 +500,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_assign_test.cc writer/msl/generator_impl_binary_test.cc writer/msl/generator_impl_break_test.cc + writer/msl/generator_impl_call_test.cc writer/msl/generator_impl_case_test.cc writer/msl/generator_impl_cast_test.cc writer/msl/generator_impl_constructor_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index fe838dfeca..5568d7ea5f 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -20,6 +20,7 @@ #include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/break_statement.h" +#include "src/ast/call_expression.h" #include "src/ast/case_statement.h" #include "src/ast/cast_expression.h" #include "src/ast/continue_statement.h" @@ -29,6 +30,7 @@ #include "src/ast/function.h" #include "src/ast/identifier_expression.h" #include "src/ast/if_statement.h" +#include "src/ast/intrinsic.h" #include "src/ast/location_decoration.h" #include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" @@ -266,6 +268,48 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement*) { return true; } +bool GeneratorImpl::EmitCall(ast::CallExpression* expr) { + if (!expr->func()->IsIdentifier()) { + error_ = "invalid function name"; + return 0; + } + + auto* ident = expr->func()->AsIdentifier(); + + if (!ident->has_path() && ast::intrinsic::IsIntrinsic(ident->name())) { + // TODO(dsinclair): Generate intrinsic + error_ = "intrinsics not generated yet"; + return false; + } + + if (!ident->has_path()) { + if (!EmitExpression(expr->func())) { + return false; + } + out_ << "("; + + bool first = true; + const auto& params = expr->params(); + for (const auto& param : params) { + if (!first) { + out_ << ", "; + } + first = false; + + if (!EmitExpression(param.get())) { + return false; + } + } + + out_ << ")"; + } else { + // TODO(dsinclair): Handle imported function + error_ = "imported calls not handled yet"; + return false; + } + return true; +} + bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { make_indent(); @@ -528,6 +572,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsBinary()) { return EmitBinary(expr->AsBinary()); } + if (expr->IsCall()) { + return EmitCall(expr->AsCall()); + } if (expr->IsCast()) { return EmitCast(expr->AsCast()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index f731c61e8f..4c94f3c446 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -66,6 +66,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully bool EmitBreak(ast::BreakStatement* stmt); + /// Handles generating a call expression + /// @param expr the call expression + /// @returns true if the call expression is emitted + bool EmitCall(ast::CallExpression* expr); /// Handles a case statement /// @param stmt the statement /// @returns true if the statment was emitted successfully diff --git a/src/writer/msl/generator_impl_call_test.cc b/src/writer/msl/generator_impl_call_test.cc new file mode 100644 index 0000000000..b9347cbabd --- /dev/null +++ b/src/writer/msl/generator_impl_call_test.cc @@ -0,0 +1,53 @@ +// 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/call_expression.h" +#include "src/ast/identifier_expression.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithoutParams) { + auto id = std::make_unique("my_func"); + ast::CallExpression call(std::move(id), {}); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&call)) << g.error(); + EXPECT_EQ(g.result(), "my_func()"); +} + +TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithParams) { + auto id = std::make_unique("my_func"); + ast::ExpressionList params; + params.push_back(std::make_unique("param1")); + params.push_back(std::make_unique("param2")); + ast::CallExpression call(std::move(id), std::move(params)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&call)) << g.error(); + EXPECT_EQ(g.result(), "my_func(param1, param2)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint