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 | |
| 11 | #include "SkSLErrorReporter.h" |
| 12 | #include "ast/SkSLASTBinaryExpression.h" |
| 13 | #include "ast/SkSLASTBlock.h" |
| 14 | #include "ast/SkSLASTBreakStatement.h" |
| 15 | #include "ast/SkSLASTCallSuffix.h" |
| 16 | #include "ast/SkSLASTContinueStatement.h" |
| 17 | #include "ast/SkSLASTDiscardStatement.h" |
| 18 | #include "ast/SkSLASTDoStatement.h" |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 19 | #include "ast/SkSLASTEnum.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | #include "ast/SkSLASTExpression.h" |
| 21 | #include "ast/SkSLASTExpressionStatement.h" |
| 22 | #include "ast/SkSLASTExtension.h" |
| 23 | #include "ast/SkSLASTForStatement.h" |
| 24 | #include "ast/SkSLASTFunction.h" |
| 25 | #include "ast/SkSLASTIdentifier.h" |
| 26 | #include "ast/SkSLASTIfStatement.h" |
| 27 | #include "ast/SkSLASTInterfaceBlock.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 28 | #include "ast/SkSLASTModifiersDeclaration.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | #include "ast/SkSLASTPrefixExpression.h" |
| 30 | #include "ast/SkSLASTReturnStatement.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 31 | #include "ast/SkSLASTSection.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 32 | #include "ast/SkSLASTStatement.h" |
| 33 | #include "ast/SkSLASTSuffixExpression.h" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 34 | #include "ast/SkSLASTSwitchStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | #include "ast/SkSLASTTernaryExpression.h" |
| 36 | #include "ast/SkSLASTVarDeclaration.h" |
| 37 | #include "ast/SkSLASTVarDeclarationStatement.h" |
| 38 | #include "ast/SkSLASTWhileStatement.h" |
| 39 | #include "ir/SkSLBlock.h" |
| 40 | #include "ir/SkSLExpression.h" |
| 41 | #include "ir/SkSLExtension.h" |
| 42 | #include "ir/SkSLFunctionDefinition.h" |
| 43 | #include "ir/SkSLInterfaceBlock.h" |
| 44 | #include "ir/SkSLModifiers.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 45 | #include "ir/SkSLModifiersDeclaration.h" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 46 | #include "ir/SkSLProgram.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 47 | #include "ir/SkSLSection.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | #include "ir/SkSLSymbolTable.h" |
| 49 | #include "ir/SkSLStatement.h" |
| 50 | #include "ir/SkSLType.h" |
| 51 | #include "ir/SkSLTypeReference.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 52 | #include "ir/SkSLVarDeclarations.h" |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 53 | #include "ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 54 | |
| 55 | namespace SkSL { |
| 56 | |
| 57 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 58 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 59 | * (unoptimized) intermediate representation (IR). |
| 60 | */ |
| 61 | class IRGenerator { |
| 62 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 63 | IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 64 | ErrorReporter& errorReporter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 65 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 66 | void convertProgram(Program::Kind kind, |
| 67 | const char* text, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 68 | size_t length, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 69 | SymbolTable& types, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 70 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 71 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 72 | /** |
| 73 | * If both operands are compile-time constants and can be folded, returns an expression |
| 74 | * representing the folded value. Otherwise, returns null. Note that unlike most other functions |
| 75 | * here, null does not represent a compilation error. |
| 76 | */ |
| 77 | std::unique_ptr<Expression> constantFold(const Expression& left, |
| 78 | Token::Kind op, |
| 79 | const Expression& right) const; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 80 | |
| 81 | std::unique_ptr<Expression> getArg(int offset, String name) const; |
| 82 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 83 | Program::Inputs fInputs; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 84 | const Program::Settings* fSettings; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 85 | const Context& fContext; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 86 | Program::Kind fKind; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 87 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | private: |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 89 | /** |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 90 | * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the |
| 91 | * settings. |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 92 | */ |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 93 | void start(const Program::Settings* settings, |
| 94 | std::vector<std::unique_ptr<ProgramElement>>* inherited); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Performs cleanup after compilation is complete. |
| 98 | */ |
| 99 | void finish(); |
| 100 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 101 | void pushSymbolTable(); |
| 102 | void popSymbolTable(); |
| 103 | |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 104 | std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTVarDeclarations& decl, |
| 105 | Variable::Storage storage); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 106 | void convertFunction(const ASTFunction& f); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 107 | std::unique_ptr<Statement> convertStatement(const ASTStatement& statement); |
| 108 | std::unique_ptr<Expression> convertExpression(const ASTExpression& expression); |
| 109 | std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration( |
| 110 | const ASTModifiersDeclaration& m); |
| 111 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 112 | const Type* convertType(const ASTType& type); |
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); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 116 | int callCost(const FunctionDeclaration& function, |
| 117 | const std::vector<std::unique_ptr<Expression>>& arguments); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 118 | std::unique_ptr<Expression> call(int offset, std::unique_ptr<Expression> function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 119 | std::vector<std::unique_ptr<Expression>> arguments); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 120 | int coercionCost(const Expression& expr, const Type& type); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 121 | std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 122 | std::unique_ptr<Expression> convertAppend(int offset, |
| 123 | const std::vector<std::unique_ptr<ASTExpression>>& args); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 124 | std::unique_ptr<Block> convertBlock(const ASTBlock& block); |
| 125 | std::unique_ptr<Statement> convertBreak(const ASTBreakStatement& b); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 126 | std::unique_ptr<Expression> convertNumberConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 127 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 128 | const Type& type, |
| 129 | std::vector<std::unique_ptr<Expression>> params); |
| 130 | std::unique_ptr<Expression> convertCompoundConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 131 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 132 | const Type& type, |
| 133 | std::vector<std::unique_ptr<Expression>> params); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 134 | std::unique_ptr<Expression> convertConstructor(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 135 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 136 | std::vector<std::unique_ptr<Expression>> params); |
| 137 | std::unique_ptr<Statement> convertContinue(const ASTContinueStatement& c); |
| 138 | std::unique_ptr<Statement> convertDiscard(const ASTDiscardStatement& d); |
| 139 | std::unique_ptr<Statement> convertDo(const ASTDoStatement& d); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 140 | std::unique_ptr<Statement> convertSwitch(const ASTSwitchStatement& s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 141 | std::unique_ptr<Expression> convertBinaryExpression(const ASTBinaryExpression& expression); |
| 142 | std::unique_ptr<Extension> convertExtension(const ASTExtension& e); |
| 143 | std::unique_ptr<Statement> convertExpressionStatement(const ASTExpressionStatement& s); |
| 144 | std::unique_ptr<Statement> convertFor(const ASTForStatement& f); |
| 145 | std::unique_ptr<Expression> convertIdentifier(const ASTIdentifier& identifier); |
| 146 | std::unique_ptr<Statement> convertIf(const ASTIfStatement& s); |
| 147 | std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base, |
| 148 | const ASTExpression& index); |
| 149 | std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTInterfaceBlock& s); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 150 | Modifiers convertModifiers(const Modifiers& m); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 151 | std::unique_ptr<Expression> convertPrefixExpression(const ASTPrefixExpression& expression); |
| 152 | std::unique_ptr<Statement> convertReturn(const ASTReturnStatement& r); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 153 | std::unique_ptr<Section> convertSection(const ASTSection& e); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 154 | std::unique_ptr<Expression> getCap(int offset, String name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 155 | std::unique_ptr<Expression> convertSuffixExpression(const ASTSuffixExpression& expression); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 156 | std::unique_ptr<Expression> convertTypeField(int offset, const Type& type, |
| 157 | StringFragment field); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 158 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 159 | StringFragment field); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 160 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 161 | StringFragment fields); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | std::unique_ptr<Expression> convertTernaryExpression(const ASTTernaryExpression& expression); |
| 163 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTVarDeclarationStatement& s); |
| 164 | std::unique_ptr<Statement> convertWhile(const ASTWhileStatement& w); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 165 | void convertEnum(const ASTEnum& e); |
| 166 | std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 167 | // returns a statement which converts sk_Position from device to normalized coordinates |
| 168 | std::unique_ptr<Statement> getNormalizeSkPositionCode(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | |
| 170 | void checkValid(const Expression& expr); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 171 | void setRefKind(const Expression& expr, VariableReference::RefKind kind); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 172 | void getConstantInt(const Expression& value, int64_t* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 173 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 174 | const FunctionDeclaration* fCurrentFunction; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 175 | std::unordered_map<String, Program::Settings::Value> fCapsMap; |
| 176 | std::shared_ptr<SymbolTable> fRootSymbolTable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | std::shared_ptr<SymbolTable> fSymbolTable; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 178 | // holds extra temp variable declarations needed for the current function |
| 179 | std::vector<std::unique_ptr<Statement>> fExtraVars; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 180 | int fLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 181 | int fSwitchLevel; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 182 | // count of temporary variables we have created |
| 183 | int fTmpCount; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 | ErrorReporter& fErrors; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 185 | int fInvocations; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 186 | std::vector<std::unique_ptr<ProgramElement>>* fProgramElements; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 187 | const Variable* fSkPerVertex = nullptr; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 188 | Variable* fRTAdjust; |
| 189 | Variable* fRTAdjustInterfaceBlock; |
| 190 | int fRTAdjustFieldIndex; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 191 | bool fStarted = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 192 | |
| 193 | friend class AutoSymbolTable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 194 | friend class AutoLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 195 | friend class AutoSwitchLevel; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 196 | friend class Compiler; |
| 197 | }; |
| 198 | |
| 199 | } |
| 200 | |
| 201 | #endif |