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