/* * Copyright 2016 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SKSL_IRGENERATOR #define SKSL_IRGENERATOR #include "SkSLErrorReporter.h" #include "ast/SkSLASTBinaryExpression.h" #include "ast/SkSLASTBlock.h" #include "ast/SkSLASTBreakStatement.h" #include "ast/SkSLASTCallSuffix.h" #include "ast/SkSLASTContinueStatement.h" #include "ast/SkSLASTDiscardStatement.h" #include "ast/SkSLASTDoStatement.h" #include "ast/SkSLASTExpression.h" #include "ast/SkSLASTExpressionStatement.h" #include "ast/SkSLASTExtension.h" #include "ast/SkSLASTForStatement.h" #include "ast/SkSLASTFunction.h" #include "ast/SkSLASTIdentifier.h" #include "ast/SkSLASTIfStatement.h" #include "ast/SkSLASTInterfaceBlock.h" #include "ast/SkSLASTModifiersDeclaration.h" #include "ast/SkSLASTPrefixExpression.h" #include "ast/SkSLASTReturnStatement.h" #include "ast/SkSLASTStatement.h" #include "ast/SkSLASTSuffixExpression.h" #include "ast/SkSLASTTernaryExpression.h" #include "ast/SkSLASTVarDeclaration.h" #include "ast/SkSLASTVarDeclarationStatement.h" #include "ast/SkSLASTWhileStatement.h" #include "ir/SkSLBlock.h" #include "ir/SkSLExpression.h" #include "ir/SkSLExtension.h" #include "ir/SkSLFunctionDefinition.h" #include "ir/SkSLInterfaceBlock.h" #include "ir/SkSLModifiers.h" #include "ir/SkSLModifiersDeclaration.h" #include "ir/SkSLProgram.h" #include "ir/SkSLSymbolTable.h" #include "ir/SkSLStatement.h" #include "ir/SkSLType.h" #include "ir/SkSLTypeReference.h" #include "ir/SkSLVarDeclarations.h" namespace SkSL { struct CapValue { CapValue() : fKind(kInt_Kind) , fValue(-1) { ASSERT(false); } CapValue(bool b) : fKind(kBool_Kind) , fValue(b) {} CapValue(int i) : fKind(kInt_Kind) , fValue(i) {} enum { kBool_Kind, kInt_Kind, } fKind; int fValue; }; /** * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding * (unoptimized) intermediate representation (IR). */ class IRGenerator { public: IRGenerator(const Context* context, std::shared_ptr root, ErrorReporter& errorReporter); std::unique_ptr convertVarDeclarations(const ASTVarDeclarations& decl, Variable::Storage storage); std::unique_ptr convertFunction(const ASTFunction& f); std::unique_ptr convertStatement(const ASTStatement& statement); std::unique_ptr convertExpression(const ASTExpression& expression); std::unique_ptr convertModifiersDeclaration( const ASTModifiersDeclaration& m); Program::Inputs fInputs; private: /** * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the * settings. */ void start(const Program::Settings* settings); /** * Performs cleanup after compilation is complete. */ void finish(); void pushSymbolTable(); void popSymbolTable(); const Type* convertType(const ASTType& type); std::unique_ptr call(Position position, const FunctionDeclaration& function, std::vector> arguments); bool determineCallCost(const FunctionDeclaration& function, const std::vector>& arguments, int* outCost); std::unique_ptr call(Position position, std::unique_ptr function, std::vector> arguments); std::unique_ptr coerce(std::unique_ptr expr, const Type& type); std::unique_ptr convertBlock(const ASTBlock& block); std::unique_ptr convertBreak(const ASTBreakStatement& b); std::unique_ptr convertConstructor(Position position, const Type& type, std::vector> params); std::unique_ptr convertContinue(const ASTContinueStatement& c); std::unique_ptr convertDiscard(const ASTDiscardStatement& d); std::unique_ptr convertDo(const ASTDoStatement& d); std::unique_ptr convertBinaryExpression(const ASTBinaryExpression& expression); // Returns null if it cannot fold the expression. Note that unlike most other functions here, a // null return does not represent a compilation error. std::unique_ptr constantFold(const Expression& left, Token::Kind op, const Expression& right); std::unique_ptr convertExtension(const ASTExtension& e); std::unique_ptr convertExpressionStatement(const ASTExpressionStatement& s); std::unique_ptr convertFor(const ASTForStatement& f); std::unique_ptr convertIdentifier(const ASTIdentifier& identifier); std::unique_ptr convertIf(const ASTIfStatement& s); std::unique_ptr convertIndex(std::unique_ptr base, const ASTExpression& index); std::unique_ptr convertInterfaceBlock(const ASTInterfaceBlock& s); Modifiers convertModifiers(const Modifiers& m); std::unique_ptr convertPrefixExpression(const ASTPrefixExpression& expression); std::unique_ptr convertReturn(const ASTReturnStatement& r); std::unique_ptr getCap(Position position, SkString name); std::unique_ptr convertSuffixExpression(const ASTSuffixExpression& expression); std::unique_ptr convertField(std::unique_ptr base, const SkString& field); std::unique_ptr convertSwizzle(std::unique_ptr base, const SkString& fields); std::unique_ptr convertTernaryExpression(const ASTTernaryExpression& expression); std::unique_ptr convertVarDeclarationStatement(const ASTVarDeclarationStatement& s); std::unique_ptr convertWhile(const ASTWhileStatement& w); void checkValid(const Expression& expr); void markReadFrom(const Variable& var); void markWrittenTo(const Expression& expr); const Context& fContext; const FunctionDeclaration* fCurrentFunction; const Program::Settings* fSettings; std::unordered_map fCapsMap; std::shared_ptr fSymbolTable; int fLoopLevel; ErrorReporter& fErrors; friend class AutoSymbolTable; friend class AutoLoopLevel; friend class Compiler; }; } #endif