ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_IRGENERATOR |
| 9 | #define SKSL_IRGENERATOR |
| 10 | |
John Stiles | ddefaee | 2020-08-11 15:13:26 -0400 | [diff] [blame] | 11 | #include <unordered_map> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 12 | #include <unordered_set> |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 13 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 14 | #include "src/sksl/SkSLASTFile.h" |
| 15 | #include "src/sksl/SkSLASTNode.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLErrorReporter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/sksl/ir/SkSLBlock.h" |
| 18 | #include "src/sksl/ir/SkSLExpression.h" |
| 19 | #include "src/sksl/ir/SkSLExtension.h" |
| 20 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 21 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 22 | #include "src/sksl/ir/SkSLModifiers.h" |
| 23 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 24 | #include "src/sksl/ir/SkSLProgram.h" |
| 25 | #include "src/sksl/ir/SkSLSection.h" |
| 26 | #include "src/sksl/ir/SkSLStatement.h" |
| 27 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 28 | #include "src/sksl/ir/SkSLType.h" |
| 29 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 30 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 31 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 32 | |
| 33 | namespace SkSL { |
| 34 | |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 35 | struct Swizzle; |
| 36 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | /** |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame^] | 38 | * Intrinsics are passed between the Compiler and the IRGenerator using IRIntrinsicMaps. |
| 39 | */ |
| 40 | struct IRIntrinsic { |
| 41 | std::unique_ptr<ProgramElement> fIntrinsic; |
| 42 | bool fAlreadyIncluded = false; |
| 43 | }; |
| 44 | using IRIntrinsicMap = std::unordered_map<String, IRIntrinsic>; |
| 45 | |
| 46 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 47 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | * (unoptimized) intermediate representation (IR). |
| 49 | */ |
| 50 | class IRGenerator { |
| 51 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 52 | IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 53 | ErrorReporter& errorReporter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 54 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 55 | void convertProgram(Program::Kind kind, |
| 56 | const char* text, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 57 | size_t length, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 58 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 59 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 60 | /** |
| 61 | * If both operands are compile-time constants and can be folded, returns an expression |
| 62 | * representing the folded value. Otherwise, returns null. Note that unlike most other functions |
| 63 | * here, null does not represent a compilation error. |
| 64 | */ |
| 65 | std::unique_ptr<Expression> constantFold(const Expression& left, |
| 66 | Token::Kind op, |
| 67 | const Expression& right) const; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 68 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 69 | Program::Inputs fInputs; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 70 | const Program::Settings* fSettings; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 71 | const Context& fContext; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 72 | Program::Kind fKind; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 73 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 74 | private: |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 75 | /** |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 76 | * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the |
| 77 | * settings. |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 78 | */ |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 79 | void start(const Program::Settings* settings, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 80 | std::vector<std::unique_ptr<ProgramElement>>* inherited, |
| 81 | bool isBuiltinCode = false); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Performs cleanup after compilation is complete. |
| 85 | */ |
| 86 | void finish(); |
| 87 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | void pushSymbolTable(); |
| 89 | void popSymbolTable(); |
| 90 | |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 91 | void checkModifiers(int offset, const Modifiers& modifiers, int permitted); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 92 | std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTNode& decl, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 93 | Variable::Storage storage); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 94 | void convertFunction(const ASTNode& f); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 95 | std::unique_ptr<Statement> convertSingleStatement(const ASTNode& statement); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 96 | std::unique_ptr<Statement> convertStatement(const ASTNode& statement); |
| 97 | std::unique_ptr<Expression> convertExpression(const ASTNode& expression); |
| 98 | std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(const ASTNode& m); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 99 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 100 | const Type* convertType(const ASTNode& type); |
John Stiles | ddefaee | 2020-08-11 15:13:26 -0400 | [diff] [blame] | 101 | std::unique_ptr<Expression> inlineExpression( |
| 102 | int offset, |
| 103 | std::unordered_map<const Variable*, const Variable*>* varMap, |
| 104 | const Expression& expression); |
| 105 | std::unique_ptr<Statement> inlineStatement( |
| 106 | int offset, |
| 107 | std::unordered_map<const Variable*, const Variable*>* varMap, |
| 108 | const Variable* returnVar, |
| 109 | bool haveEarlyReturns, |
| 110 | const Statement& statement); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 111 | std::unique_ptr<Expression> inlineCall(int offset, const FunctionDefinition& function, |
| 112 | std::vector<std::unique_ptr<Expression>> arguments); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 113 | std::unique_ptr<Expression> call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 114 | const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 115 | std::vector<std::unique_ptr<Expression>> arguments); |
John Stiles | 8c91c93 | 2020-08-13 14:35:35 -0400 | [diff] [blame] | 116 | bool isSafeToInline(const FunctionDefinition& function); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 117 | int callCost(const FunctionDeclaration& function, |
| 118 | const std::vector<std::unique_ptr<Expression>>& arguments); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 119 | std::unique_ptr<Expression> call(int offset, std::unique_ptr<Expression> function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 120 | std::vector<std::unique_ptr<Expression>> arguments); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 121 | int coercionCost(const Expression& expr, const Type& type); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 122 | std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 123 | std::unique_ptr<Block> convertBlock(const ASTNode& block); |
| 124 | std::unique_ptr<Statement> convertBreak(const ASTNode& b); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 125 | std::unique_ptr<Expression> convertNumberConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 126 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 127 | const Type& type, |
| 128 | std::vector<std::unique_ptr<Expression>> params); |
| 129 | std::unique_ptr<Expression> convertCompoundConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 130 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 131 | const Type& type, |
| 132 | std::vector<std::unique_ptr<Expression>> params); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 133 | std::unique_ptr<Expression> convertConstructor(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 134 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 135 | std::vector<std::unique_ptr<Expression>> params); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 136 | std::unique_ptr<Statement> convertContinue(const ASTNode& c); |
| 137 | std::unique_ptr<Statement> convertDiscard(const ASTNode& d); |
| 138 | std::unique_ptr<Statement> convertDo(const ASTNode& d); |
| 139 | std::unique_ptr<Statement> convertSwitch(const ASTNode& s); |
| 140 | std::unique_ptr<Expression> convertBinaryExpression(const ASTNode& expression); |
| 141 | std::unique_ptr<Extension> convertExtension(int offset, StringFragment name); |
| 142 | std::unique_ptr<Statement> convertExpressionStatement(const ASTNode& s); |
| 143 | std::unique_ptr<Statement> convertFor(const ASTNode& f); |
| 144 | std::unique_ptr<Expression> convertIdentifier(const ASTNode& identifier); |
| 145 | std::unique_ptr<Statement> convertIf(const ASTNode& s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 146 | std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 147 | const ASTNode& index); |
| 148 | std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTNode& s); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 149 | Modifiers convertModifiers(const Modifiers& m); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 150 | std::unique_ptr<Expression> convertPrefixExpression(const ASTNode& expression); |
| 151 | std::unique_ptr<Statement> convertReturn(const ASTNode& r); |
| 152 | std::unique_ptr<Section> convertSection(const ASTNode& e); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 153 | std::unique_ptr<Expression> getCap(int offset, String name); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 154 | std::unique_ptr<Expression> convertCallExpression(const ASTNode& expression); |
| 155 | std::unique_ptr<Expression> convertFieldExpression(const ASTNode& expression); |
| 156 | std::unique_ptr<Expression> convertIndexExpression(const ASTNode& expression); |
| 157 | std::unique_ptr<Expression> convertPostfixExpression(const ASTNode& expression); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 158 | std::unique_ptr<Expression> findEnumRef(int offset, |
| 159 | const Type& type, |
| 160 | StringFragment field, |
| 161 | std::vector<std::unique_ptr<ProgramElement>>& elements); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 162 | std::unique_ptr<Expression> convertTypeField(int offset, const Type& type, |
| 163 | StringFragment field); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 164 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 165 | StringFragment field); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 167 | StringFragment fields); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 168 | std::unique_ptr<Expression> convertTernaryExpression(const ASTNode& expression); |
| 169 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTNode& s); |
| 170 | std::unique_ptr<Statement> convertWhile(const ASTNode& w); |
| 171 | void convertEnum(const ASTNode& e); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 172 | std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 173 | // returns a statement which converts sk_Position from device to normalized coordinates |
| 174 | std::unique_ptr<Statement> getNormalizeSkPositionCode(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 175 | |
| 176 | void checkValid(const Expression& expr); |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 177 | bool setRefKind(Expression& expr, VariableReference::RefKind kind); |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 178 | bool getConstantInt(const Expression& value, int64_t* out); |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 179 | bool checkSwizzleWrite(const Swizzle& swizzle); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 180 | void copyIntrinsicIfNeeded(const FunctionDeclaration& function); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 182 | std::unique_ptr<ASTFile> fFile; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 183 | const FunctionDeclaration* fCurrentFunction; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 184 | std::unordered_map<String, Program::Settings::Value> fCapsMap; |
| 185 | std::shared_ptr<SymbolTable> fRootSymbolTable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 186 | std::shared_ptr<SymbolTable> fSymbolTable; |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 187 | // additional statements that need to be inserted before the one that convertStatement is |
| 188 | // currently working on |
| 189 | std::vector<std::unique_ptr<Statement>> fExtraStatements; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame^] | 190 | // Symbols which have definitions in the include files. |
| 191 | IRIntrinsicMap* fIntrinsics = nullptr; |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 192 | std::unordered_set<const FunctionDeclaration*> fReferencedIntrinsics; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 193 | int fLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 194 | int fSwitchLevel; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 195 | ErrorReporter& fErrors; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 196 | int fInvocations; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 197 | std::vector<std::unique_ptr<ProgramElement>>* fInherited; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 198 | std::vector<std::unique_ptr<ProgramElement>>* fProgramElements; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 199 | const Variable* fSkPerVertex = nullptr; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 200 | const Variable* fRTAdjust; |
| 201 | const Variable* fRTAdjustInterfaceBlock; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 202 | int fRTAdjustFieldIndex; |
Michael Ludwig | 9861b7c | 2020-06-23 18:37:17 -0400 | [diff] [blame] | 203 | int fInlineVarCounter; |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 204 | bool fCanInline = true; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 205 | // true if we are currently processing one of the built-in SkSL include files |
| 206 | bool fIsBuiltinCode; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 207 | |
| 208 | friend class AutoSymbolTable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 209 | friend class AutoLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 210 | friend class AutoSwitchLevel; |
John Stiles | d1c4dac | 2020-08-11 18:50:50 -0400 | [diff] [blame] | 211 | friend class AutoDisableInline; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 212 | friend class Compiler; |
| 213 | }; |
| 214 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 215 | } // namespace SkSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 216 | |
| 217 | #endif |