| 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 | */ |
| 7 | |
| 8 | #ifndef SKSL_IRGENERATOR |
| 9 | #define SKSL_IRGENERATOR |
| 10 | |
| 11 | #include <map> |
| 12 | #include <unordered_map> |
| 13 | #include <unordered_set> |
| 14 | |
| 15 | #include "src/sksl/SkSLASTFile.h" |
| 16 | #include "src/sksl/SkSLASTNode.h" |
| 17 | #include "src/sksl/SkSLErrorReporter.h" |
| 18 | #include "src/sksl/ir/SkSLBlock.h" |
| 19 | #include "src/sksl/ir/SkSLExpression.h" |
| 20 | #include "src/sksl/ir/SkSLExtension.h" |
| 21 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 22 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 23 | #include "src/sksl/ir/SkSLModifiers.h" |
| 24 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 25 | #include "src/sksl/ir/SkSLProgram.h" |
| 26 | #include "src/sksl/ir/SkSLSection.h" |
| 27 | #include "src/sksl/ir/SkSLStatement.h" |
| 28 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 29 | #include "src/sksl/ir/SkSLType.h" |
| 30 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 31 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 32 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 33 | |
| 34 | namespace SkSL { |
| 35 | |
| 36 | struct Swizzle; |
| 37 | |
| 38 | /** |
| 39 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
| 40 | * (unoptimized) intermediate representation (IR). |
| 41 | */ |
| 42 | class IRGenerator { |
| 43 | public: |
| 44 | IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root, |
| 45 | ErrorReporter& errorReporter); |
| 46 | |
| 47 | void convertProgram(Program::Kind kind, |
| 48 | const char* text, |
| 49 | size_t length, |
| 50 | std::vector<std::unique_ptr<ProgramElement>>* result); |
| 51 | |
| 52 | /** |
| 53 | * If both operands are compile-time constants and can be folded, returns an expression |
| 54 | * representing the folded value. Otherwise, returns null. Note that unlike most other functions |
| 55 | * here, null does not represent a compilation error. |
| 56 | */ |
| 57 | std::unique_ptr<Expression> constantFold(const Expression& left, |
| 58 | Token::Kind op, |
| 59 | const Expression& right) const; |
| 60 | |
| 61 | Program::Inputs fInputs; |
| 62 | const Program::Settings* fSettings; |
| 63 | const Context& fContext; |
| 64 | Program::Kind fKind; |
| 65 | |
| 66 | private: |
| 67 | /** |
| 68 | * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the |
| 69 | * settings. |
| 70 | */ |
| 71 | void start(const Program::Settings* settings, |
| 72 | std::vector<std::unique_ptr<ProgramElement>>* inherited, |
| 73 | bool isBuiltinCode = false); |
| 74 | |
| 75 | /** |
| 76 | * Performs cleanup after compilation is complete. |
| 77 | */ |
| 78 | void finish(); |
| 79 | |
| 80 | void pushSymbolTable(); |
| 81 | void popSymbolTable(); |
| 82 | |
| 83 | std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTNode& decl, |
| 84 | Variable::Storage storage); |
| 85 | void convertFunction(const ASTNode& f); |
| 86 | std::unique_ptr<Statement> convertSingleStatement(const ASTNode& statement); |
| 87 | std::unique_ptr<Statement> convertStatement(const ASTNode& statement); |
| 88 | std::unique_ptr<Expression> convertExpression(const ASTNode& expression); |
| 89 | std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(const ASTNode& m); |
| 90 | |
| 91 | const Type* convertType(const ASTNode& type); |
| 92 | std::unique_ptr<Expression> inlineExpression( |
| 93 | int offset, |
| 94 | std::unordered_map<const Variable*, const Variable*>* varMap, |
| 95 | const Expression& expression); |
| 96 | std::unique_ptr<Statement> inlineStatement( |
| 97 | int offset, |
| 98 | std::unordered_map<const Variable*, const Variable*>* varMap, |
| 99 | const Variable* returnVar, |
| 100 | bool haveEarlyReturns, |
| 101 | const Statement& statement); |
| 102 | std::unique_ptr<Expression> inlineCall(int offset, const FunctionDefinition& function, |
| 103 | std::vector<std::unique_ptr<Expression>> arguments); |
| 104 | std::unique_ptr<Expression> call(int offset, |
| 105 | const FunctionDeclaration& function, |
| 106 | std::vector<std::unique_ptr<Expression>> arguments); |
| 107 | bool isSafeToInline(const FunctionDefinition& function); |
| 108 | int callCost(const FunctionDeclaration& function, |
| 109 | const std::vector<std::unique_ptr<Expression>>& arguments); |
| 110 | std::unique_ptr<Expression> call(int offset, std::unique_ptr<Expression> function, |
| 111 | std::vector<std::unique_ptr<Expression>> arguments); |
| 112 | int coercionCost(const Expression& expr, const Type& type); |
| 113 | std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type); |
| 114 | std::unique_ptr<Block> convertBlock(const ASTNode& block); |
| 115 | std::unique_ptr<Statement> convertBreak(const ASTNode& b); |
| 116 | std::unique_ptr<Expression> convertNumberConstructor( |
| 117 | int offset, |
| 118 | const Type& type, |
| 119 | std::vector<std::unique_ptr<Expression>> params); |
| 120 | std::unique_ptr<Expression> convertCompoundConstructor( |
| 121 | int offset, |
| 122 | const Type& type, |
| 123 | std::vector<std::unique_ptr<Expression>> params); |
| 124 | std::unique_ptr<Expression> convertConstructor(int offset, |
| 125 | const Type& type, |
| 126 | std::vector<std::unique_ptr<Expression>> params); |
| 127 | std::unique_ptr<Statement> convertContinue(const ASTNode& c); |
| 128 | std::unique_ptr<Statement> convertDiscard(const ASTNode& d); |
| 129 | std::unique_ptr<Statement> convertDo(const ASTNode& d); |
| 130 | std::unique_ptr<Statement> convertSwitch(const ASTNode& s); |
| 131 | std::unique_ptr<Expression> convertBinaryExpression(const ASTNode& expression); |
| 132 | std::unique_ptr<Extension> convertExtension(int offset, StringFragment name); |
| 133 | std::unique_ptr<Statement> convertExpressionStatement(const ASTNode& s); |
| 134 | std::unique_ptr<Statement> convertFor(const ASTNode& f); |
| 135 | std::unique_ptr<Expression> convertIdentifier(const ASTNode& identifier); |
| 136 | std::unique_ptr<Statement> convertIf(const ASTNode& s); |
| 137 | std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base, |
| 138 | const ASTNode& index); |
| 139 | std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTNode& s); |
| 140 | Modifiers convertModifiers(const Modifiers& m); |
| 141 | std::unique_ptr<Expression> convertPrefixExpression(const ASTNode& expression); |
| 142 | std::unique_ptr<Statement> convertReturn(const ASTNode& r); |
| 143 | std::unique_ptr<Section> convertSection(const ASTNode& e); |
| 144 | std::unique_ptr<Expression> getCap(int offset, String name); |
| 145 | std::unique_ptr<Expression> convertCallExpression(const ASTNode& expression); |
| 146 | std::unique_ptr<Expression> convertFieldExpression(const ASTNode& expression); |
| 147 | std::unique_ptr<Expression> convertIndexExpression(const ASTNode& expression); |
| 148 | std::unique_ptr<Expression> convertPostfixExpression(const ASTNode& expression); |
| 149 | std::unique_ptr<Expression> findEnumRef(int offset, |
| 150 | const Type& type, |
| 151 | StringFragment field, |
| 152 | std::vector<std::unique_ptr<ProgramElement>>& elements); |
| 153 | std::unique_ptr<Expression> convertTypeField(int offset, const Type& type, |
| 154 | StringFragment field); |
| 155 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
| 156 | StringFragment field); |
| 157 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, |
| 158 | StringFragment fields); |
| 159 | std::unique_ptr<Expression> convertTernaryExpression(const ASTNode& expression); |
| 160 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTNode& s); |
| 161 | std::unique_ptr<Statement> convertWhile(const ASTNode& w); |
| 162 | void convertEnum(const ASTNode& e); |
| 163 | std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main); |
| 164 | // returns a statement which converts sk_Position from device to normalized coordinates |
| 165 | std::unique_ptr<Statement> getNormalizeSkPositionCode(); |
| 166 | |
| 167 | void checkValid(const Expression& expr); |
| 168 | bool setRefKind(const Expression& expr, VariableReference::RefKind kind); |
| 169 | bool getConstantInt(const Expression& value, int64_t* out); |
| 170 | bool checkSwizzleWrite(const Swizzle& swizzle); |
| 171 | void copyIntrinsicIfNeeded(const FunctionDeclaration& function); |
| 172 | |
| 173 | std::unique_ptr<ASTFile> fFile; |
| 174 | const FunctionDeclaration* fCurrentFunction; |
| 175 | std::unordered_map<String, Program::Settings::Value> fCapsMap; |
| 176 | std::shared_ptr<SymbolTable> fRootSymbolTable; |
| 177 | std::shared_ptr<SymbolTable> fSymbolTable; |
| 178 | // additional statements that need to be inserted before the one that convertStatement is |
| 179 | // currently working on |
| 180 | std::vector<std::unique_ptr<Statement>> ; |
| 181 | // Symbols which have definitions in the include files. The bool tells us whether this |
| 182 | // intrinsic has been included already. |
| 183 | std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* fIntrinsics = nullptr; |
| 184 | std::unordered_set<const FunctionDeclaration*> fReferencedIntrinsics; |
| 185 | int fLoopLevel; |
| 186 | int fSwitchLevel; |
| 187 | ErrorReporter& fErrors; |
| 188 | int fInvocations; |
| 189 | std::vector<std::unique_ptr<ProgramElement>>* fInherited; |
| 190 | std::vector<std::unique_ptr<ProgramElement>>* fProgramElements; |
| 191 | const Variable* fSkPerVertex = nullptr; |
| 192 | const Variable* fRTAdjust; |
| 193 | const Variable* fRTAdjustInterfaceBlock; |
| 194 | int fRTAdjustFieldIndex; |
| 195 | int fInlineVarCounter; |
| 196 | bool fCanInline = true; |
| 197 | // true if we are currently processing one of the built-in SkSL include files |
| 198 | bool fIsBuiltinCode; |
| 199 | |
| 200 | friend class AutoSymbolTable; |
| 201 | friend class AutoLoopLevel; |
| 202 | friend class AutoSwitchLevel; |
| 203 | friend class AutoDisableInline; |
| 204 | friend class Compiler; |
| 205 | }; |
| 206 | |
| 207 | } // namespace SkSL |
| 208 | |
| 209 | #endif |
| 210 | |