[hlsl-writer] Add function handling.

This CL adds the beginning of function handling to the HLSL generator.

Bug: tint:7
Change-Id: Id40109c342e7a128b1fe79a0c50967e1dbd125eb
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26662
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-08-19 17:32:25 +00:00 committed by Commit Bot service account
parent 94a374fa84
commit be89a06b03
5 changed files with 1200 additions and 0 deletions

View File

@ -1067,6 +1067,7 @@ source_set("tint_unittests_hlsl_writer_src") {
"src/writer/hlsl/generator_impl_constructor_test.cc", "src/writer/hlsl/generator_impl_constructor_test.cc",
"src/writer/hlsl/generator_impl_continue_test.cc", "src/writer/hlsl/generator_impl_continue_test.cc",
"src/writer/hlsl/generator_impl_discard_test.cc", "src/writer/hlsl/generator_impl_discard_test.cc",
"src/writer/hlsl/generator_impl_function_test.cc",
"src/writer/hlsl/generator_impl_identifier_test.cc", "src/writer/hlsl/generator_impl_identifier_test.cc",
"src/writer/hlsl/generator_impl_if_test.cc", "src/writer/hlsl/generator_impl_if_test.cc",
"src/writer/hlsl/generator_impl_loop_test.cc", "src/writer/hlsl/generator_impl_loop_test.cc",

View File

@ -578,6 +578,7 @@ if (${TINT_BUILD_HLSL_WRITER})
writer/hlsl/generator_impl_constructor_test.cc writer/hlsl/generator_impl_constructor_test.cc
writer/hlsl/generator_impl_continue_test.cc writer/hlsl/generator_impl_continue_test.cc
writer/hlsl/generator_impl_discard_test.cc writer/hlsl/generator_impl_discard_test.cc
writer/hlsl/generator_impl_function_test.cc
writer/hlsl/generator_impl_identifier_test.cc writer/hlsl/generator_impl_identifier_test.cc
writer/hlsl/generator_impl_if_test.cc writer/hlsl/generator_impl_if_test.cc
writer/hlsl/generator_impl_loop_test.cc writer/hlsl/generator_impl_loop_test.cc

View File

@ -74,6 +74,18 @@ bool GeneratorImpl::Generate() {
out_ << std::endl; out_ << std::endl;
} }
for (const auto& func : module_->functions()) {
if (!EmitFunction(func.get())) {
return false;
}
}
for (const auto& ep : module_->entry_points()) {
if (!EmitEntryPointFunction(ep.get())) {
return false;
}
out_ << std::endl;
}
return true; return true;
} }
@ -487,6 +499,77 @@ bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
return EmitBlock(stmt->body()); return EmitBlock(stmt->body());
} }
bool GeneratorImpl::EmitFunction(ast::Function* func) {
make_indent();
// Entry points will be emitted later, skip for now.
if (module_->IsFunctionEntryPoint(func->name())) {
return true;
}
auto name = func->name();
if (!EmitType(func->return_type(), "")) {
return false;
}
out_ << " " << namer_.NameFor(name) << "(";
bool first = true;
for (const auto& v : func->params()) {
if (!first) {
out_ << ", ";
}
first = false;
if (!EmitType(v->type(), v->name())) {
return false;
}
// Array name is output as part of the type
if (!v->type()->IsArray()) {
out_ << " " << v->name();
}
}
out_ << ") ";
if (!EmitBlockAndNewline(func->body())) {
return false;
}
return true;
}
bool GeneratorImpl::EmitEntryPointFunction(ast::EntryPoint* ep) {
make_indent();
auto current_ep_name = ep->name();
if (current_ep_name.empty()) {
current_ep_name = ep->function_name();
}
auto* func = module_->FindFunctionByName(ep->function_name());
if (func == nullptr) {
error_ = "unable to find function for entry point: " + ep->function_name();
return false;
}
out_ << "void " << namer_.NameFor(current_ep_name) << "() {" << std::endl;
increment_indent();
for (const auto& s : *(func->body())) {
if (!EmitStatement(s.get())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}" << std::endl;
return true;
}
bool GeneratorImpl::EmitLiteral(ast::Literal* lit) { bool GeneratorImpl::EmitLiteral(ast::Literal* lit) {
if (lit->IsBool()) { if (lit->IsBool()) {
out_ << (lit->AsBool()->IsTrue() ? "true" : "false"); out_ << (lit->AsBool()->IsTrue() ? "true" : "false");

View File

@ -106,6 +106,14 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the expression /// @param expr the expression
/// @returns true if the expression was emitted /// @returns true if the expression was emitted
bool EmitExpression(ast::Expression* expr); bool EmitExpression(ast::Expression* expr);
/// Handles generating a function
/// @param func the function to generate
/// @returns true if the function was emitted
bool EmitFunction(ast::Function* func);
/// Handles emitting the entry point function
/// @param ep the entry point
/// @returns true if the entry point function was emitted
bool EmitEntryPointFunction(ast::EntryPoint* ep);
/// Handles an if statement /// Handles an if statement
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted /// @returns true if the statement was successfully emitted

File diff suppressed because it is too large Load Diff