Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 8 | #include "include/sksl/DSLFunction.h" |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 9 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 10 | #include "include/sksl/DSLVar.h" |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLAnalysis.h" |
| 12 | #include "src/sksl/SkSLCompiler.h" |
| 13 | #include "src/sksl/SkSLIRGenerator.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 14 | #include "src/sksl/dsl/priv/DSLWriter.h" |
Ethan Nicholas | 57709e1 | 2021-08-10 15:02:51 -0400 | [diff] [blame^] | 15 | #include "src/sksl/ir/SkSLFunctionPrototype.h" |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 17 | |
| 18 | namespace SkSL { |
| 19 | |
| 20 | namespace dsl { |
| 21 | |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 22 | void DSLFunction::init(DSLModifiers modifiers, const DSLType& returnType, skstd::string_view name, |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 23 | SkTArray<DSLParameter*> params) { |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 24 | // Conservatively assume all user-defined functions have side effects. |
| 25 | if (!DSLWriter::IsModule()) { |
| 26 | modifiers.fModifiers.fFlags |= Modifiers::kHasSideEffects_Flag; |
| 27 | } |
| 28 | |
Ethan Nicholas | 459777a | 2021-07-16 11:16:27 -0400 | [diff] [blame] | 29 | if (DSLWriter::Settings().fForceNoInline) { |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 30 | // Apply the `noinline` modifier to every function. This allows us to test Runtime |
| 31 | // Effects without any inlining, even when the code is later added to a paint. |
| 32 | modifiers.fModifiers.fFlags &= ~Modifiers::kInline_Flag; |
| 33 | modifiers.fModifiers.fFlags |= Modifiers::kNoInline_Flag; |
| 34 | } |
| 35 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 36 | std::vector<std::unique_ptr<Variable>> paramVars; |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 37 | paramVars.reserve(params.size()); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 38 | for (DSLParameter* param : params) { |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 39 | if (param->fDeclared) { |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 40 | DSLWriter::ReportError("error: parameter has already been used in another function\n"); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 41 | } |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 42 | SkASSERT(!param->fInitialValue.valid()); |
| 43 | SkASSERT(!param->fDeclaration); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 44 | param->fDeclared = true; |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 45 | std::unique_ptr<SkSL::Variable> paramVar = DSLWriter::CreateParameterVar(*param); |
Ethan Nicholas | 6b3c6fc | 2021-05-04 15:36:06 -0400 | [diff] [blame] | 46 | if (!paramVar) { |
| 47 | return; |
| 48 | } |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 49 | paramVars.push_back(std::move(paramVar)); |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 50 | } |
Ethan Nicholas | 6b3c6fc | 2021-05-04 15:36:06 -0400 | [diff] [blame] | 51 | SkASSERT(paramVars.size() == params.size()); |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 52 | fDecl = SkSL::FunctionDeclaration::Convert(DSLWriter::Context(), |
| 53 | *DSLWriter::SymbolTable(), |
John Stiles | 0b82279 | 2021-05-04 17:41:53 -0400 | [diff] [blame] | 54 | /*offset=*/-1, |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 55 | DSLWriter::Modifiers(modifiers.fModifiers), |
Ethan Nicholas | e0531f5 | 2021-07-26 16:07:23 -0400 | [diff] [blame] | 56 | name == "main" ? name : DSLWriter::Name(name), |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 57 | std::move(paramVars), &returnType.skslType(), |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 58 | DSLWriter::IsModule()); |
| 59 | DSLWriter::ReportErrors(); |
| 60 | if (fDecl) { |
| 61 | for (size_t i = 0; i < params.size(); ++i) { |
| 62 | params[i]->fVar = fDecl->parameters()[i]; |
| 63 | } |
Ethan Nicholas | 57709e1 | 2021-08-10 15:02:51 -0400 | [diff] [blame^] | 64 | // We don't know when this function is going to be defined; go ahead and add a prototype in |
| 65 | // case the definition is delayed. If we end up defining the function immediately, we'll |
| 66 | // remove the prototype in define(). |
| 67 | DSLWriter::ProgramElements().push_back(std::make_unique<SkSL::FunctionPrototype>( |
| 68 | /*offset=*/-1, fDecl, DSLWriter::IsModule())); |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 69 | } |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 72 | void DSLFunction::define(DSLBlock block) { |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 73 | if (!fDecl) { |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 74 | // Evidently we failed to create the declaration; error should already have been reported. |
| 75 | // Release the block so we don't fail its destructor assert. |
| 76 | block.release(); |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 77 | return; |
| 78 | } |
Ethan Nicholas | 57709e1 | 2021-08-10 15:02:51 -0400 | [diff] [blame^] | 79 | if (!DSLWriter::ProgramElements().empty()) { |
| 80 | // If the last ProgramElement was the prototype for this function, it was unnecessary and we |
| 81 | // can remove it. |
| 82 | const SkSL::ProgramElement& last = *DSLWriter::ProgramElements().back(); |
| 83 | if (last.is<SkSL::FunctionPrototype>()) { |
| 84 | const SkSL::FunctionPrototype& prototype = last.as<SkSL::FunctionPrototype>(); |
| 85 | if (&prototype.declaration() == fDecl) { |
| 86 | DSLWriter::ProgramElements().pop_back(); |
| 87 | } |
| 88 | } |
| 89 | } |
Ethan Nicholas | 80f6235 | 2021-04-09 12:25:03 -0400 | [diff] [blame] | 90 | SkASSERTF(!fDecl->definition(), "function '%s' already defined", fDecl->description().c_str()); |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 91 | std::unique_ptr<Block> body = block.release(); |
| 92 | body = DSLWriter::IRGenerator().finalizeFunction(*fDecl, std::move(body)); |
Ethan Nicholas | 80f6235 | 2021-04-09 12:25:03 -0400 | [diff] [blame] | 93 | auto function = std::make_unique<SkSL::FunctionDefinition>(/*offset=*/-1, fDecl, |
John Stiles | 958f4b5 | 2021-03-17 16:39:49 -0400 | [diff] [blame] | 94 | /*builtin=*/false, std::move(body)); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 95 | DSLWriter::ReportErrors(); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 96 | fDecl->fDefinition = function.get(); |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 97 | DSLWriter::ProgramElements().push_back(std::move(function)); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 100 | DSLExpression DSLFunction::call(SkTArray<DSLWrapper<DSLExpression>> args) { |
Ethan Nicholas | 80f6235 | 2021-04-09 12:25:03 -0400 | [diff] [blame] | 101 | ExpressionArray released; |
| 102 | released.reserve_back(args.size()); |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 103 | for (DSLWrapper<DSLExpression>& arg : args) { |
| 104 | released.push_back(arg->release()); |
Ethan Nicholas | 80f6235 | 2021-04-09 12:25:03 -0400 | [diff] [blame] | 105 | } |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 106 | std::unique_ptr<SkSL::Expression> result = DSLWriter::Call(*fDecl, std::move(released)); |
| 107 | return result ? DSLExpression(std::move(result)) : DSLExpression(); |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 110 | } // namespace dsl |
| 111 | |
| 112 | } // namespace SkSL |