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 | |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame^] | 11 | #include <map> |
| 12 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 13 | #include "src/sksl/SkSLASTFile.h" |
| 14 | #include "src/sksl/SkSLASTNode.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 15 | #include "src/sksl/SkSLErrorReporter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLBlock.h" |
| 17 | #include "src/sksl/ir/SkSLExpression.h" |
| 18 | #include "src/sksl/ir/SkSLExtension.h" |
| 19 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 20 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 21 | #include "src/sksl/ir/SkSLModifiers.h" |
| 22 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 23 | #include "src/sksl/ir/SkSLProgram.h" |
| 24 | #include "src/sksl/ir/SkSLSection.h" |
| 25 | #include "src/sksl/ir/SkSLStatement.h" |
| 26 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 27 | #include "src/sksl/ir/SkSLType.h" |
| 28 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 29 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 30 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | |
| 32 | namespace SkSL { |
| 33 | |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 34 | struct Swizzle; |
| 35 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 37 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 38 | * (unoptimized) intermediate representation (IR). |
| 39 | */ |
| 40 | class IRGenerator { |
| 41 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 42 | IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 43 | ErrorReporter& errorReporter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 44 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 45 | void convertProgram(Program::Kind kind, |
| 46 | const char* text, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 47 | size_t length, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 48 | SymbolTable& types, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 49 | std::vector<std::unique_ptr<ProgramElement>>* result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 50 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 51 | /** |
| 52 | * If both operands are compile-time constants and can be folded, returns an expression |
| 53 | * representing the folded value. Otherwise, returns null. Note that unlike most other functions |
| 54 | * here, null does not represent a compilation error. |
| 55 | */ |
| 56 | std::unique_ptr<Expression> constantFold(const Expression& left, |
| 57 | Token::Kind op, |
| 58 | const Expression& right) const; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 59 | |
| 60 | std::unique_ptr<Expression> getArg(int offset, String name) const; |
| 61 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 62 | Program::Inputs fInputs; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 63 | const Program::Settings* fSettings; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 64 | const Context& fContext; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 65 | Program::Kind fKind; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 66 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 67 | private: |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 68 | /** |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 69 | * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the |
| 70 | * settings. |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 71 | */ |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 72 | void start(const Program::Settings* settings, |
| 73 | std::vector<std::unique_ptr<ProgramElement>>* inherited); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Performs cleanup after compilation is complete. |
| 77 | */ |
| 78 | void finish(); |
| 79 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 80 | void pushSymbolTable(); |
| 81 | void popSymbolTable(); |
| 82 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 83 | std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTNode& decl, |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 84 | Variable::Storage storage); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 85 | void convertFunction(const ASTNode& f); |
| 86 | std::unique_ptr<Statement> convertStatement(const ASTNode& statement); |
| 87 | std::unique_ptr<Expression> convertExpression(const ASTNode& expression); |
| 88 | std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(const ASTNode& m); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 89 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 90 | const Type* convertType(const ASTNode& type); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 91 | std::unique_ptr<Expression> call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 92 | const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 93 | std::vector<std::unique_ptr<Expression>> arguments); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 94 | int callCost(const FunctionDeclaration& function, |
| 95 | const std::vector<std::unique_ptr<Expression>>& arguments); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 96 | std::unique_ptr<Expression> call(int offset, std::unique_ptr<Expression> function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 97 | std::vector<std::unique_ptr<Expression>> arguments); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 98 | int coercionCost(const Expression& expr, const Type& type); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 99 | 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] | 100 | std::unique_ptr<Block> convertBlock(const ASTNode& block); |
| 101 | std::unique_ptr<Statement> convertBreak(const ASTNode& b); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 102 | std::unique_ptr<Expression> convertNumberConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 103 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 104 | const Type& type, |
| 105 | std::vector<std::unique_ptr<Expression>> params); |
| 106 | std::unique_ptr<Expression> convertCompoundConstructor( |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 107 | int offset, |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 108 | const Type& type, |
| 109 | std::vector<std::unique_ptr<Expression>> params); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 110 | std::unique_ptr<Expression> convertConstructor(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 111 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 112 | std::vector<std::unique_ptr<Expression>> params); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 113 | std::unique_ptr<Statement> convertContinue(const ASTNode& c); |
| 114 | std::unique_ptr<Statement> convertDiscard(const ASTNode& d); |
| 115 | std::unique_ptr<Statement> convertDo(const ASTNode& d); |
| 116 | std::unique_ptr<Statement> convertSwitch(const ASTNode& s); |
| 117 | std::unique_ptr<Expression> convertBinaryExpression(const ASTNode& expression); |
| 118 | std::unique_ptr<Extension> convertExtension(int offset, StringFragment name); |
| 119 | std::unique_ptr<Statement> convertExpressionStatement(const ASTNode& s); |
| 120 | std::unique_ptr<Statement> convertFor(const ASTNode& f); |
| 121 | std::unique_ptr<Expression> convertIdentifier(const ASTNode& identifier); |
| 122 | std::unique_ptr<Statement> convertIf(const ASTNode& s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 123 | std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 124 | const ASTNode& index); |
| 125 | std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTNode& s); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 126 | Modifiers convertModifiers(const Modifiers& m); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 127 | std::unique_ptr<Expression> convertPrefixExpression(const ASTNode& expression); |
| 128 | std::unique_ptr<Statement> convertReturn(const ASTNode& r); |
| 129 | std::unique_ptr<Section> convertSection(const ASTNode& e); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 130 | std::unique_ptr<Expression> getCap(int offset, String name); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 131 | std::unique_ptr<Expression> convertCallExpression(const ASTNode& expression); |
| 132 | std::unique_ptr<Expression> convertFieldExpression(const ASTNode& expression); |
| 133 | std::unique_ptr<Expression> convertIndexExpression(const ASTNode& expression); |
| 134 | std::unique_ptr<Expression> convertPostfixExpression(const ASTNode& expression); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 135 | std::unique_ptr<Expression> convertTypeField(int offset, const Type& type, |
| 136 | StringFragment field); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 137 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 138 | StringFragment field); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 139 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 140 | StringFragment fields); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 141 | std::unique_ptr<Expression> convertTernaryExpression(const ASTNode& expression); |
| 142 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTNode& s); |
| 143 | std::unique_ptr<Statement> convertWhile(const ASTNode& w); |
| 144 | void convertEnum(const ASTNode& e); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 145 | std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 146 | // returns a statement which converts sk_Position from device to normalized coordinates |
| 147 | std::unique_ptr<Statement> getNormalizeSkPositionCode(); |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 148 | void removeSampleMask(std::vector<std::unique_ptr<ProgramElement>>* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 149 | |
| 150 | void checkValid(const Expression& expr); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 151 | void setRefKind(const Expression& expr, VariableReference::RefKind kind); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 152 | void getConstantInt(const Expression& value, int64_t* out); |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 153 | bool checkSwizzleWrite(const Swizzle& swizzle); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 154 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 155 | std::unique_ptr<ASTFile> fFile; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 156 | const FunctionDeclaration* fCurrentFunction; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 157 | std::unordered_map<String, Program::Settings::Value> fCapsMap; |
| 158 | std::shared_ptr<SymbolTable> fRootSymbolTable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 159 | std::shared_ptr<SymbolTable> fSymbolTable; |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame^] | 160 | // Symbols which have definitions in the include files. The bool tells us whether this |
| 161 | // intrinsic has been included already. |
| 162 | std::map<StringFragment, std::pair<std::unique_ptr<ProgramElement>, bool>>* fIntrinsics = nullptr; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 163 | // holds extra temp variable declarations needed for the current function |
| 164 | std::vector<std::unique_ptr<Statement>> fExtraVars; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 165 | int fLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 166 | int fSwitchLevel; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 167 | // count of temporary variables we have created |
| 168 | int fTmpCount; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | ErrorReporter& fErrors; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 170 | int fInvocations; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 171 | std::vector<std::unique_ptr<ProgramElement>>* fProgramElements; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 172 | const Variable* fSkPerVertex = nullptr; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 173 | Variable* fRTAdjust; |
| 174 | Variable* fRTAdjustInterfaceBlock; |
| 175 | int fRTAdjustFieldIndex; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 176 | bool fStarted = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | |
| 178 | friend class AutoSymbolTable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 179 | friend class AutoLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 180 | friend class AutoSwitchLevel; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | friend class Compiler; |
| 182 | }; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | #endif |