[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 <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-14 20:37:38 +00:00 committed by dan sinclair
parent de2dd68f2b
commit c5a5f9666f
5 changed files with 106 additions and 0 deletions

View File

@ -892,6 +892,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_assign_test.cc", "src/writer/msl/generator_impl_assign_test.cc",
"src/writer/msl/generator_impl_binary_test.cc", "src/writer/msl/generator_impl_binary_test.cc",
"src/writer/msl/generator_impl_break_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_case_test.cc",
"src/writer/msl/generator_impl_cast_test.cc", "src/writer/msl/generator_impl_cast_test.cc",
"src/writer/msl/generator_impl_constructor_test.cc", "src/writer/msl/generator_impl_constructor_test.cc",

View File

@ -500,6 +500,7 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator_impl_assign_test.cc writer/msl/generator_impl_assign_test.cc
writer/msl/generator_impl_binary_test.cc writer/msl/generator_impl_binary_test.cc
writer/msl/generator_impl_break_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_case_test.cc
writer/msl/generator_impl_cast_test.cc writer/msl/generator_impl_cast_test.cc
writer/msl/generator_impl_constructor_test.cc writer/msl/generator_impl_constructor_test.cc

View File

@ -20,6 +20,7 @@
#include "src/ast/binary_expression.h" #include "src/ast/binary_expression.h"
#include "src/ast/bool_literal.h" #include "src/ast/bool_literal.h"
#include "src/ast/break_statement.h" #include "src/ast/break_statement.h"
#include "src/ast/call_expression.h"
#include "src/ast/case_statement.h" #include "src/ast/case_statement.h"
#include "src/ast/cast_expression.h" #include "src/ast/cast_expression.h"
#include "src/ast/continue_statement.h" #include "src/ast/continue_statement.h"
@ -29,6 +30,7 @@
#include "src/ast/function.h" #include "src/ast/function.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/if_statement.h" #include "src/ast/if_statement.h"
#include "src/ast/intrinsic.h"
#include "src/ast/location_decoration.h" #include "src/ast/location_decoration.h"
#include "src/ast/loop_statement.h" #include "src/ast/loop_statement.h"
#include "src/ast/member_accessor_expression.h" #include "src/ast/member_accessor_expression.h"
@ -266,6 +268,48 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
return true; 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) { bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) {
make_indent(); make_indent();
@ -528,6 +572,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsBinary()) { if (expr->IsBinary()) {
return EmitBinary(expr->AsBinary()); return EmitBinary(expr->AsBinary());
} }
if (expr->IsCall()) {
return EmitCall(expr->AsCall());
}
if (expr->IsCast()) { if (expr->IsCast()) {
return EmitCast(expr->AsCast()); return EmitCast(expr->AsCast());
} }

View File

@ -66,6 +66,10 @@ class GeneratorImpl : public TextGenerator {
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitBreak(ast::BreakStatement* stmt); 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 /// Handles a case statement
/// @param stmt the statement /// @param stmt the statement
/// @returns true if the statment was emitted successfully /// @returns true if the statment was emitted successfully

View File

@ -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 <memory>
#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<ast::IdentifierExpression>("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<ast::IdentifierExpression>("my_func");
ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("param1"));
params.push_back(std::make_unique<ast::IdentifierExpression>("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