SkSLIRGenerator.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #ifndef SKSL_IRGENERATOR
  8. #define SKSL_IRGENERATOR
  9. #include "src/sksl/SkSLASTFile.h"
  10. #include "src/sksl/SkSLASTNode.h"
  11. #include "src/sksl/SkSLErrorReporter.h"
  12. #include "src/sksl/ir/SkSLBlock.h"
  13. #include "src/sksl/ir/SkSLExpression.h"
  14. #include "src/sksl/ir/SkSLExtension.h"
  15. #include "src/sksl/ir/SkSLFunctionDefinition.h"
  16. #include "src/sksl/ir/SkSLInterfaceBlock.h"
  17. #include "src/sksl/ir/SkSLModifiers.h"
  18. #include "src/sksl/ir/SkSLModifiersDeclaration.h"
  19. #include "src/sksl/ir/SkSLProgram.h"
  20. #include "src/sksl/ir/SkSLSection.h"
  21. #include "src/sksl/ir/SkSLStatement.h"
  22. #include "src/sksl/ir/SkSLSymbolTable.h"
  23. #include "src/sksl/ir/SkSLType.h"
  24. #include "src/sksl/ir/SkSLTypeReference.h"
  25. #include "src/sksl/ir/SkSLVarDeclarations.h"
  26. #include "src/sksl/ir/SkSLVariableReference.h"
  27. namespace SkSL {
  28. struct Swizzle;
  29. /**
  30. * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding
  31. * (unoptimized) intermediate representation (IR).
  32. */
  33. class IRGenerator {
  34. public:
  35. IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root,
  36. ErrorReporter& errorReporter);
  37. void convertProgram(Program::Kind kind,
  38. const char* text,
  39. size_t length,
  40. SymbolTable& types,
  41. std::vector<std::unique_ptr<ProgramElement>>* result);
  42. /**
  43. * If both operands are compile-time constants and can be folded, returns an expression
  44. * representing the folded value. Otherwise, returns null. Note that unlike most other functions
  45. * here, null does not represent a compilation error.
  46. */
  47. std::unique_ptr<Expression> constantFold(const Expression& left,
  48. Token::Kind op,
  49. const Expression& right) const;
  50. std::unique_ptr<Expression> getArg(int offset, String name) const;
  51. Program::Inputs fInputs;
  52. const Program::Settings* fSettings;
  53. const Context& fContext;
  54. Program::Kind fKind;
  55. private:
  56. /**
  57. * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the
  58. * settings.
  59. */
  60. void start(const Program::Settings* settings,
  61. std::vector<std::unique_ptr<ProgramElement>>* inherited);
  62. /**
  63. * Performs cleanup after compilation is complete.
  64. */
  65. void finish();
  66. void pushSymbolTable();
  67. void popSymbolTable();
  68. std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTNode& decl,
  69. Variable::Storage storage);
  70. void convertFunction(const ASTNode& f);
  71. std::unique_ptr<Statement> convertStatement(const ASTNode& statement);
  72. std::unique_ptr<Expression> convertExpression(const ASTNode& expression);
  73. std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(const ASTNode& m);
  74. const Type* convertType(const ASTNode& type);
  75. std::unique_ptr<Expression> call(int offset,
  76. const FunctionDeclaration& function,
  77. std::vector<std::unique_ptr<Expression>> arguments);
  78. int callCost(const FunctionDeclaration& function,
  79. const std::vector<std::unique_ptr<Expression>>& arguments);
  80. std::unique_ptr<Expression> call(int offset, std::unique_ptr<Expression> function,
  81. std::vector<std::unique_ptr<Expression>> arguments);
  82. int coercionCost(const Expression& expr, const Type& type);
  83. std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type);
  84. std::unique_ptr<Expression> convertAppend(int offset, const std::vector<ASTNode>& args);
  85. std::unique_ptr<Block> convertBlock(const ASTNode& block);
  86. std::unique_ptr<Statement> convertBreak(const ASTNode& b);
  87. std::unique_ptr<Expression> convertNumberConstructor(
  88. int offset,
  89. const Type& type,
  90. std::vector<std::unique_ptr<Expression>> params);
  91. std::unique_ptr<Expression> convertCompoundConstructor(
  92. int offset,
  93. const Type& type,
  94. std::vector<std::unique_ptr<Expression>> params);
  95. std::unique_ptr<Expression> convertConstructor(int offset,
  96. const Type& type,
  97. std::vector<std::unique_ptr<Expression>> params);
  98. std::unique_ptr<Statement> convertContinue(const ASTNode& c);
  99. std::unique_ptr<Statement> convertDiscard(const ASTNode& d);
  100. std::unique_ptr<Statement> convertDo(const ASTNode& d);
  101. std::unique_ptr<Statement> convertSwitch(const ASTNode& s);
  102. std::unique_ptr<Expression> convertBinaryExpression(const ASTNode& expression);
  103. std::unique_ptr<Extension> convertExtension(int offset, StringFragment name);
  104. std::unique_ptr<Statement> convertExpressionStatement(const ASTNode& s);
  105. std::unique_ptr<Statement> convertFor(const ASTNode& f);
  106. std::unique_ptr<Expression> convertIdentifier(const ASTNode& identifier);
  107. std::unique_ptr<Statement> convertIf(const ASTNode& s);
  108. std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base,
  109. const ASTNode& index);
  110. std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTNode& s);
  111. Modifiers convertModifiers(const Modifiers& m);
  112. std::unique_ptr<Expression> convertPrefixExpression(const ASTNode& expression);
  113. std::unique_ptr<Statement> convertReturn(const ASTNode& r);
  114. std::unique_ptr<Section> convertSection(const ASTNode& e);
  115. std::unique_ptr<Expression> getCap(int offset, String name);
  116. std::unique_ptr<Expression> convertCallExpression(const ASTNode& expression);
  117. std::unique_ptr<Expression> convertFieldExpression(const ASTNode& expression);
  118. std::unique_ptr<Expression> convertIndexExpression(const ASTNode& expression);
  119. std::unique_ptr<Expression> convertPostfixExpression(const ASTNode& expression);
  120. std::unique_ptr<Expression> convertTypeField(int offset, const Type& type,
  121. StringFragment field);
  122. std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base,
  123. StringFragment field);
  124. std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base,
  125. StringFragment fields);
  126. std::unique_ptr<Expression> convertTernaryExpression(const ASTNode& expression);
  127. std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTNode& s);
  128. std::unique_ptr<Statement> convertWhile(const ASTNode& w);
  129. void convertEnum(const ASTNode& e);
  130. std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main);
  131. // returns a statement which converts sk_Position from device to normalized coordinates
  132. std::unique_ptr<Statement> getNormalizeSkPositionCode();
  133. void checkValid(const Expression& expr);
  134. void setRefKind(const Expression& expr, VariableReference::RefKind kind);
  135. void getConstantInt(const Expression& value, int64_t* out);
  136. bool checkSwizzleWrite(const Swizzle& swizzle);
  137. std::unique_ptr<ASTFile> fFile;
  138. const FunctionDeclaration* fCurrentFunction;
  139. std::unordered_map<String, Program::Settings::Value> fCapsMap;
  140. std::shared_ptr<SymbolTable> fRootSymbolTable;
  141. std::shared_ptr<SymbolTable> fSymbolTable;
  142. // holds extra temp variable declarations needed for the current function
  143. std::vector<std::unique_ptr<Statement>> fExtraVars;
  144. int fLoopLevel;
  145. int fSwitchLevel;
  146. // count of temporary variables we have created
  147. int fTmpCount;
  148. ErrorReporter& fErrors;
  149. int fInvocations;
  150. std::vector<std::unique_ptr<ProgramElement>>* fProgramElements;
  151. const Variable* fSkPerVertex = nullptr;
  152. Variable* fRTAdjust;
  153. Variable* fRTAdjustInterfaceBlock;
  154. int fRTAdjustFieldIndex;
  155. bool fStarted = false;
  156. friend class AutoSymbolTable;
  157. friend class AutoLoopLevel;
  158. friend class AutoSwitchLevel;
  159. friend class Compiler;
  160. };
  161. }
  162. #endif