/* * 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 "src/sksl/SkSLASTFile.h" #include "src/sksl/SkSLASTNode.h" #include "src/sksl/SkSLErrorReporter.h" #include "src/sksl/ir/SkSLBlock.h" #include "src/sksl/ir/SkSLExpression.h" #include "src/sksl/ir/SkSLExtension.h" #include "src/sksl/ir/SkSLFunctionDefinition.h" #include "src/sksl/ir/SkSLInterfaceBlock.h" #include "src/sksl/ir/SkSLModifiers.h" #include "src/sksl/ir/SkSLModifiersDeclaration.h" #include "src/sksl/ir/SkSLProgram.h" #include "src/sksl/ir/SkSLSection.h" #include "src/sksl/ir/SkSLStatement.h" #include "src/sksl/ir/SkSLSymbolTable.h" #include "src/sksl/ir/SkSLType.h" #include "src/sksl/ir/SkSLTypeReference.h" #include "src/sksl/ir/SkSLVarDeclarations.h" #include "src/sksl/ir/SkSLVariableReference.h" namespace SkSL { struct Swizzle; /** * 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); void convertProgram(Program::Kind kind, const char* text, size_t length, SymbolTable& types, std::vector>* result); /** * If both operands are compile-time constants and can be folded, returns an expression * representing the folded value. Otherwise, returns null. Note that unlike most other functions * here, null does not represent a compilation error. */ std::unique_ptr constantFold(const Expression& left, Token::Kind op, const Expression& right) const; std::unique_ptr getArg(int offset, String name) const; Program::Inputs fInputs; const Program::Settings* fSettings; const Context& fContext; Program::Kind fKind; private: /** * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the * settings. */ void start(const Program::Settings* settings, std::vector>* inherited); /** * Performs cleanup after compilation is complete. */ void finish(); void pushSymbolTable(); void popSymbolTable(); std::unique_ptr convertVarDeclarations(const ASTNode& decl, Variable::Storage storage); void convertFunction(const ASTNode& f); std::unique_ptr convertStatement(const ASTNode& statement); std::unique_ptr convertExpression(const ASTNode& expression); std::unique_ptr convertModifiersDeclaration(const ASTNode& m); const Type* convertType(const ASTNode& type); std::unique_ptr call(int offset, const FunctionDeclaration& function, std::vector> arguments); int callCost(const FunctionDeclaration& function, const std::vector>& arguments); std::unique_ptr call(int offset, std::unique_ptr function, std::vector> arguments); int coercionCost(const Expression& expr, const Type& type); std::unique_ptr coerce(std::unique_ptr expr, const Type& type); std::unique_ptr convertAppend(int offset, const std::vector& args); std::unique_ptr convertBlock(const ASTNode& block); std::unique_ptr convertBreak(const ASTNode& b); std::unique_ptr convertNumberConstructor( int offset, const Type& type, std::vector> params); std::unique_ptr convertCompoundConstructor( int offset, const Type& type, std::vector> params); std::unique_ptr convertConstructor(int offset, const Type& type, std::vector> params); std::unique_ptr convertContinue(const ASTNode& c); std::unique_ptr convertDiscard(const ASTNode& d); std::unique_ptr convertDo(const ASTNode& d); std::unique_ptr convertSwitch(const ASTNode& s); std::unique_ptr convertBinaryExpression(const ASTNode& expression); std::unique_ptr convertExtension(int offset, StringFragment name); std::unique_ptr convertExpressionStatement(const ASTNode& s); std::unique_ptr convertFor(const ASTNode& f); std::unique_ptr convertIdentifier(const ASTNode& identifier); std::unique_ptr convertIf(const ASTNode& s); std::unique_ptr convertIndex(std::unique_ptr base, const ASTNode& index); std::unique_ptr convertInterfaceBlock(const ASTNode& s); Modifiers convertModifiers(const Modifiers& m); std::unique_ptr convertPrefixExpression(const ASTNode& expression); std::unique_ptr convertReturn(const ASTNode& r); std::unique_ptr
convertSection(const ASTNode& e); std::unique_ptr getCap(int offset, String name); std::unique_ptr convertCallExpression(const ASTNode& expression); std::unique_ptr convertFieldExpression(const ASTNode& expression); std::unique_ptr convertIndexExpression(const ASTNode& expression); std::unique_ptr convertPostfixExpression(const ASTNode& expression); std::unique_ptr convertTypeField(int offset, const Type& type, StringFragment field); std::unique_ptr convertField(std::unique_ptr base, StringFragment field); std::unique_ptr convertSwizzle(std::unique_ptr base, StringFragment fields); std::unique_ptr convertTernaryExpression(const ASTNode& expression); std::unique_ptr convertVarDeclarationStatement(const ASTNode& s); std::unique_ptr convertWhile(const ASTNode& w); void convertEnum(const ASTNode& e); std::unique_ptr applyInvocationIDWorkaround(std::unique_ptr main); // returns a statement which converts sk_Position from device to normalized coordinates std::unique_ptr getNormalizeSkPositionCode(); void checkValid(const Expression& expr); void setRefKind(const Expression& expr, VariableReference::RefKind kind); void getConstantInt(const Expression& value, int64_t* out); bool checkSwizzleWrite(const Swizzle& swizzle); std::unique_ptr fFile; const FunctionDeclaration* fCurrentFunction; std::unordered_map fCapsMap; std::shared_ptr fRootSymbolTable; std::shared_ptr fSymbolTable; // holds extra temp variable declarations needed for the current function std::vector> fExtraVars; int fLoopLevel; int fSwitchLevel; // count of temporary variables we have created int fTmpCount; ErrorReporter& fErrors; int fInvocations; std::vector>* fProgramElements; const Variable* fSkPerVertex = nullptr; Variable* fRTAdjust; Variable* fRTAdjustInterfaceBlock; int fRTAdjustFieldIndex; bool fStarted = false; friend class AutoSymbolTable; friend class AutoLoopLevel; friend class AutoSwitchLevel; friend class Compiler; }; } #endif