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 | */ |
| 7 | |
| 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" |
| 27 | #include "ast/SkSLASTModifiers.h" |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -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" |
| 31 | #include "ast/SkSLASTStatement.h" |
| 32 | #include "ast/SkSLASTSuffixExpression.h" |
| 33 | #include "ast/SkSLASTTernaryExpression.h" |
| 34 | #include "ast/SkSLASTVarDeclaration.h" |
| 35 | #include "ast/SkSLASTVarDeclarationStatement.h" |
| 36 | #include "ast/SkSLASTWhileStatement.h" |
| 37 | #include "ir/SkSLBlock.h" |
| 38 | #include "ir/SkSLExpression.h" |
| 39 | #include "ir/SkSLExtension.h" |
| 40 | #include "ir/SkSLFunctionDefinition.h" |
| 41 | #include "ir/SkSLInterfaceBlock.h" |
| 42 | #include "ir/SkSLModifiers.h" |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 43 | #include "ir/SkSLModifiersDeclaration.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" |
| 48 | #include "ir/SkSLVarDeclaration.h" |
| 49 | |
| 50 | namespace SkSL { |
| 51 | |
| 52 | /** |
| 53 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
| 54 | * (unoptimized) intermediate representation (IR). |
| 55 | */ |
| 56 | class IRGenerator { |
| 57 | public: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 58 | IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root, |
| 59 | ErrorReporter& errorReporter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 60 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 61 | std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTVarDeclarations& decl, |
| 62 | Variable::Storage storage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 63 | std::unique_ptr<FunctionDefinition> convertFunction(const ASTFunction& f); |
| 64 | std::unique_ptr<Statement> convertStatement(const ASTStatement& statement); |
| 65 | std::unique_ptr<Expression> convertExpression(const ASTExpression& expression); |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 66 | std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration( |
| 67 | const ASTModifiersDeclaration& m); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | void pushSymbolTable(); |
| 71 | void popSymbolTable(); |
| 72 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 73 | const Type* convertType(const ASTType& type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 74 | std::unique_ptr<Expression> call(Position position, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 75 | const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 76 | std::vector<std::unique_ptr<Expression>> arguments); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 77 | bool determineCallCost(const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 78 | const std::vector<std::unique_ptr<Expression>>& arguments, |
| 79 | int* outCost); |
| 80 | std::unique_ptr<Expression> call(Position position, std::unique_ptr<Expression> function, |
| 81 | std::vector<std::unique_ptr<Expression>> arguments); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 82 | std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 83 | std::unique_ptr<Block> convertBlock(const ASTBlock& block); |
| 84 | std::unique_ptr<Statement> convertBreak(const ASTBreakStatement& b); |
| 85 | std::unique_ptr<Expression> convertConstructor(Position position, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 86 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 87 | std::vector<std::unique_ptr<Expression>> params); |
| 88 | std::unique_ptr<Statement> convertContinue(const ASTContinueStatement& c); |
| 89 | std::unique_ptr<Statement> convertDiscard(const ASTDiscardStatement& d); |
| 90 | std::unique_ptr<Statement> convertDo(const ASTDoStatement& d); |
| 91 | std::unique_ptr<Expression> convertBinaryExpression(const ASTBinaryExpression& expression); |
| 92 | std::unique_ptr<Extension> convertExtension(const ASTExtension& e); |
| 93 | std::unique_ptr<Statement> convertExpressionStatement(const ASTExpressionStatement& s); |
| 94 | std::unique_ptr<Statement> convertFor(const ASTForStatement& f); |
| 95 | std::unique_ptr<Expression> convertIdentifier(const ASTIdentifier& identifier); |
| 96 | std::unique_ptr<Statement> convertIf(const ASTIfStatement& s); |
| 97 | std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base, |
| 98 | const ASTExpression& index); |
| 99 | std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTInterfaceBlock& s); |
| 100 | Modifiers convertModifiers(const ASTModifiers& m); |
| 101 | std::unique_ptr<Expression> convertPrefixExpression(const ASTPrefixExpression& expression); |
| 102 | std::unique_ptr<Statement> convertReturn(const ASTReturnStatement& r); |
| 103 | std::unique_ptr<Expression> convertSuffixExpression(const ASTSuffixExpression& expression); |
| 104 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
| 105 | const std::string& field); |
| 106 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, |
| 107 | const std::string& fields); |
| 108 | std::unique_ptr<Expression> convertTernaryExpression(const ASTTernaryExpression& expression); |
| 109 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTVarDeclarationStatement& s); |
| 110 | std::unique_ptr<Statement> convertWhile(const ASTWhileStatement& w); |
| 111 | |
| 112 | void checkValid(const Expression& expr); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 113 | void markReadFrom(const Variable& var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 114 | void markWrittenTo(const Expression& expr); |
| 115 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 116 | const Context& fContext; |
| 117 | const FunctionDeclaration* fCurrentFunction; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | std::shared_ptr<SymbolTable> fSymbolTable; |
| 119 | ErrorReporter& fErrors; |
| 120 | |
| 121 | friend class AutoSymbolTable; |
| 122 | friend class Compiler; |
| 123 | }; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | #endif |