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