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" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | |
| 54 | namespace SkSL { |
| 55 | |
| 56 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 57 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 58 | * (unoptimized) intermediate representation (IR). |
| 59 | */ |
| 60 | class IRGenerator { |
| 61 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 62 | IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 63 | ErrorReporter& errorReporter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 64 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 65 | void convertProgram(const char* text, |
| 66 | size_t length, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 67 | SymbolTable& types, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 68 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 69 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 70 | /** |
| 71 | * If both operands are compile-time constants and can be folded, returns an expression |
| 72 | * representing the folded value. Otherwise, returns null. Note that unlike most other functions |
| 73 | * here, null does not represent a compilation error. |
| 74 | */ |
| 75 | std::unique_ptr<Expression> constantFold(const Expression& left, |
| 76 | Token::Kind op, |
| 77 | const Expression& right) const; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 78 | Program::Inputs fInputs; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 79 | const Program::Settings* fSettings; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 80 | const Context& fContext; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 81 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 82 | private: |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 83 | /** |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 84 | * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the |
| 85 | * settings. |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 86 | */ |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 87 | void start(const Program::Settings* settings); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Performs cleanup after compilation is complete. |
| 91 | */ |
| 92 | void finish(); |
| 93 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 94 | void pushSymbolTable(); |
| 95 | void popSymbolTable(); |
| 96 | |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 97 | std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTVarDeclarations& decl, |
| 98 | Variable::Storage storage); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame^] | 99 | void convertFunction(const ASTFunction& f); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 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 | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame^] | 148 | std::unique_ptr<Expression> convertTypeField(int offset, const Type& type, |
| 149 | StringFragment field); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 150 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 151 | StringFragment field); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 152 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 153 | StringFragment fields); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 154 | std::unique_ptr<Expression> convertTernaryExpression(const ASTTernaryExpression& expression); |
| 155 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTVarDeclarationStatement& s); |
| 156 | std::unique_ptr<Statement> convertWhile(const ASTWhileStatement& w); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame^] | 157 | void convertEnum(const ASTEnum& e); |
| 158 | std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 159 | |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 160 | void fixRectSampling(std::vector<std::unique_ptr<Expression>>& arguments); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | void checkValid(const Expression& expr); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 162 | void markWrittenTo(const Expression& expr, bool readWrite); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame^] | 163 | void getConstantInt(const Expression& value, int64_t* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 164 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 165 | const FunctionDeclaration* fCurrentFunction; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 166 | std::unordered_map<String, Program::Settings::Value> fCapsMap; |
| 167 | std::shared_ptr<SymbolTable> fRootSymbolTable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | std::shared_ptr<SymbolTable> fSymbolTable; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 169 | // holds extra temp variable declarations needed for the current function |
| 170 | std::vector<std::unique_ptr<Statement>> fExtraVars; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 171 | int fLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 172 | int fSwitchLevel; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 173 | // count of temporary variables we have created |
| 174 | int fTmpCount; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 175 | ErrorReporter& fErrors; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 176 | int fInvocations; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame^] | 177 | std::vector<std::unique_ptr<ProgramElement>>* fProgramElements; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 178 | |
| 179 | friend class AutoSymbolTable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 180 | friend class AutoLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 181 | friend class AutoSwitchLevel; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 182 | friend class Compiler; |
| 183 | }; |
| 184 | |
| 185 | } |
| 186 | |
| 187 | #endif |