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