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" |
| 30 | #include "ast/SkSLASTStatement.h" |
| 31 | #include "ast/SkSLASTSuffixExpression.h" |
| 32 | #include "ast/SkSLASTTernaryExpression.h" |
| 33 | #include "ast/SkSLASTVarDeclaration.h" |
| 34 | #include "ast/SkSLASTVarDeclarationStatement.h" |
| 35 | #include "ast/SkSLASTWhileStatement.h" |
| 36 | #include "ir/SkSLBlock.h" |
| 37 | #include "ir/SkSLExpression.h" |
| 38 | #include "ir/SkSLExtension.h" |
| 39 | #include "ir/SkSLFunctionDefinition.h" |
| 40 | #include "ir/SkSLInterfaceBlock.h" |
| 41 | #include "ir/SkSLModifiers.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 42 | #include "ir/SkSLModifiersDeclaration.h" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 43 | #include "ir/SkSLProgram.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 44 | #include "ir/SkSLSymbolTable.h" |
| 45 | #include "ir/SkSLStatement.h" |
| 46 | #include "ir/SkSLType.h" |
| 47 | #include "ir/SkSLTypeReference.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 48 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | |
| 50 | namespace SkSL { |
| 51 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 52 | struct CapValue { |
| 53 | CapValue() |
| 54 | : fKind(kInt_Kind) |
| 55 | , fValue(-1) { |
| 56 | ASSERT(false); |
| 57 | } |
| 58 | |
| 59 | CapValue(bool b) |
| 60 | : fKind(kBool_Kind) |
| 61 | , fValue(b) {} |
| 62 | |
| 63 | CapValue(int i) |
| 64 | : fKind(kInt_Kind) |
| 65 | , fValue(i) {} |
| 66 | |
| 67 | enum { |
| 68 | kBool_Kind, |
| 69 | kInt_Kind, |
| 70 | } fKind; |
| 71 | int fValue; |
| 72 | }; |
| 73 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 74 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 75 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 76 | * (unoptimized) intermediate representation (IR). |
| 77 | */ |
| 78 | class IRGenerator { |
| 79 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 80 | IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 81 | ErrorReporter& errorReporter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 82 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 83 | std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTVarDeclarations& decl, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 84 | Variable::Storage storage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 85 | std::unique_ptr<FunctionDefinition> convertFunction(const ASTFunction& f); |
| 86 | std::unique_ptr<Statement> convertStatement(const ASTStatement& statement); |
| 87 | std::unique_ptr<Expression> convertExpression(const ASTExpression& expression); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 88 | std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration( |
| 89 | const ASTModifiersDeclaration& m); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 90 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 91 | Program::Inputs fInputs; |
| 92 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 93 | private: |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 94 | /** |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 95 | * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the |
| 96 | * settings. |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 97 | */ |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 98 | void start(const Program::Settings* settings); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Performs cleanup after compilation is complete. |
| 102 | */ |
| 103 | void finish(); |
| 104 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 105 | void pushSymbolTable(); |
| 106 | void popSymbolTable(); |
| 107 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 108 | const Type* convertType(const ASTType& type); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 109 | std::unique_ptr<Expression> call(Position position, |
| 110 | const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 111 | std::vector<std::unique_ptr<Expression>> arguments); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 112 | bool determineCallCost(const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 113 | const std::vector<std::unique_ptr<Expression>>& arguments, |
| 114 | int* outCost); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 115 | std::unique_ptr<Expression> call(Position position, std::unique_ptr<Expression> function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 116 | std::vector<std::unique_ptr<Expression>> arguments); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 117 | std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | std::unique_ptr<Block> convertBlock(const ASTBlock& block); |
| 119 | std::unique_ptr<Statement> convertBreak(const ASTBreakStatement& b); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 120 | std::unique_ptr<Expression> convertConstructor(Position position, |
| 121 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 122 | std::vector<std::unique_ptr<Expression>> params); |
| 123 | std::unique_ptr<Statement> convertContinue(const ASTContinueStatement& c); |
| 124 | std::unique_ptr<Statement> convertDiscard(const ASTDiscardStatement& d); |
| 125 | std::unique_ptr<Statement> convertDo(const ASTDoStatement& d); |
| 126 | std::unique_ptr<Expression> convertBinaryExpression(const ASTBinaryExpression& expression); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 127 | // Returns null if it cannot fold the expression. Note that unlike most other functions here, a |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 128 | // null return does not represent a compilation error. |
| 129 | std::unique_ptr<Expression> constantFold(const Expression& left, |
| 130 | Token::Kind op, |
| 131 | const Expression& right); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 132 | std::unique_ptr<Extension> convertExtension(const ASTExtension& e); |
| 133 | std::unique_ptr<Statement> convertExpressionStatement(const ASTExpressionStatement& s); |
| 134 | std::unique_ptr<Statement> convertFor(const ASTForStatement& f); |
| 135 | std::unique_ptr<Expression> convertIdentifier(const ASTIdentifier& identifier); |
| 136 | std::unique_ptr<Statement> convertIf(const ASTIfStatement& s); |
| 137 | std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base, |
| 138 | const ASTExpression& index); |
| 139 | std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTInterfaceBlock& s); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 140 | Modifiers convertModifiers(const Modifiers& m); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 141 | std::unique_ptr<Expression> convertPrefixExpression(const ASTPrefixExpression& expression); |
| 142 | std::unique_ptr<Statement> convertReturn(const ASTReturnStatement& r); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 143 | std::unique_ptr<Expression> getCap(Position position, SkString name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 144 | std::unique_ptr<Expression> convertSuffixExpression(const ASTSuffixExpression& expression); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 145 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 146 | const SkString& field); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 147 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 148 | const SkString& fields); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 149 | std::unique_ptr<Expression> convertTernaryExpression(const ASTTernaryExpression& expression); |
| 150 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTVarDeclarationStatement& s); |
| 151 | std::unique_ptr<Statement> convertWhile(const ASTWhileStatement& w); |
| 152 | |
| 153 | void checkValid(const Expression& expr); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 154 | void markReadFrom(const Variable& var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 155 | void markWrittenTo(const Expression& expr); |
| 156 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 157 | const Context& fContext; |
| 158 | const FunctionDeclaration* fCurrentFunction; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 159 | const Program::Settings* fSettings; |
| 160 | std::unordered_map<SkString, CapValue> fCapsMap; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | std::shared_ptr<SymbolTable> fSymbolTable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 162 | int fLoopLevel; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 163 | ErrorReporter& fErrors; |
| 164 | |
| 165 | friend class AutoSymbolTable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 166 | friend class AutoLoopLevel; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 167 | friend class Compiler; |
| 168 | }; |
| 169 | |
| 170 | } |
| 171 | |
| 172 | #endif |