Line data Source code
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/SkSLASTModifiersDeclaration.h"
28 : #include "ast/SkSLASTPrefixExpression.h"
29 : #include "ast/SkSLASTReturnStatement.h"
30 : #include "ast/SkSLASTStatement.h"
31 : #include "ast/SkSLASTSuffixExpression.h"
32 : #include "ast/SkSLASTSwitchStatement.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"
43 : #include "ir/SkSLModifiersDeclaration.h"
44 : #include "ir/SkSLProgram.h"
45 : #include "ir/SkSLSymbolTable.h"
46 : #include "ir/SkSLStatement.h"
47 : #include "ir/SkSLType.h"
48 : #include "ir/SkSLTypeReference.h"
49 : #include "ir/SkSLVarDeclarations.h"
50 :
51 : namespace SkSL {
52 :
53 : struct CapValue {
54 : CapValue()
55 : : fKind(kInt_Kind)
56 : , fValue(-1) {
57 : ASSERT(false);
58 : }
59 :
60 0 : CapValue(bool b)
61 0 : : fKind(kBool_Kind)
62 0 : , fValue(b) {}
63 :
64 0 : CapValue(int i)
65 0 : : fKind(kInt_Kind)
66 0 : , fValue(i) {}
67 :
68 : enum {
69 : kBool_Kind,
70 : kInt_Kind,
71 : } fKind;
72 : int fValue;
73 : };
74 :
75 : /**
76 : * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding
77 : * (unoptimized) intermediate representation (IR).
78 : */
79 0 : class IRGenerator {
80 : public:
81 : IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root,
82 : ErrorReporter& errorReporter);
83 :
84 : std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTVarDeclarations& decl,
85 : Variable::Storage storage);
86 : std::unique_ptr<FunctionDefinition> convertFunction(const ASTFunction& f);
87 : std::unique_ptr<Statement> convertStatement(const ASTStatement& statement);
88 : std::unique_ptr<Expression> convertExpression(const ASTExpression& expression);
89 : std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(
90 : const ASTModifiersDeclaration& m);
91 :
92 : /**
93 : * If both operands are compile-time constants and can be folded, returns an expression
94 : * representing the folded value. Otherwise, returns null. Note that unlike most other functions
95 : * here, null does not represent a compilation error.
96 : */
97 : std::unique_ptr<Expression> constantFold(const Expression& left,
98 : Token::Kind op,
99 : const Expression& right) const;
100 : Program::Inputs fInputs;
101 : const Context& fContext;
102 :
103 : private:
104 : /**
105 : * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the
106 : * settings.
107 : */
108 : void start(const Program::Settings* settings);
109 :
110 : /**
111 : * Performs cleanup after compilation is complete.
112 : */
113 : void finish();
114 :
115 : void pushSymbolTable();
116 : void popSymbolTable();
117 :
118 : const Type* convertType(const ASTType& type);
119 : std::unique_ptr<Expression> call(Position position,
120 : const FunctionDeclaration& function,
121 : std::vector<std::unique_ptr<Expression>> arguments);
122 : bool determineCallCost(const FunctionDeclaration& function,
123 : const std::vector<std::unique_ptr<Expression>>& arguments,
124 : int* outCost);
125 : std::unique_ptr<Expression> call(Position position, std::unique_ptr<Expression> function,
126 : std::vector<std::unique_ptr<Expression>> arguments);
127 : std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type);
128 : std::unique_ptr<Block> convertBlock(const ASTBlock& block);
129 : std::unique_ptr<Statement> convertBreak(const ASTBreakStatement& b);
130 : std::unique_ptr<Expression> convertNumberConstructor(
131 : Position position,
132 : const Type& type,
133 : std::vector<std::unique_ptr<Expression>> params);
134 : std::unique_ptr<Expression> convertCompoundConstructor(
135 : Position position,
136 : const Type& type,
137 : std::vector<std::unique_ptr<Expression>> params);
138 : std::unique_ptr<Expression> convertConstructor(Position position,
139 : const Type& type,
140 : std::vector<std::unique_ptr<Expression>> params);
141 : std::unique_ptr<Statement> convertContinue(const ASTContinueStatement& c);
142 : std::unique_ptr<Statement> convertDiscard(const ASTDiscardStatement& d);
143 : std::unique_ptr<Statement> convertDo(const ASTDoStatement& d);
144 : std::unique_ptr<Statement> convertSwitch(const ASTSwitchStatement& s);
145 : std::unique_ptr<Expression> convertBinaryExpression(const ASTBinaryExpression& expression);
146 : std::unique_ptr<Extension> convertExtension(const ASTExtension& e);
147 : std::unique_ptr<Statement> convertExpressionStatement(const ASTExpressionStatement& s);
148 : std::unique_ptr<Statement> convertFor(const ASTForStatement& f);
149 : std::unique_ptr<Expression> convertIdentifier(const ASTIdentifier& identifier);
150 : std::unique_ptr<Statement> convertIf(const ASTIfStatement& s);
151 : std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base,
152 : const ASTExpression& index);
153 : std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTInterfaceBlock& s);
154 : Modifiers convertModifiers(const Modifiers& m);
155 : std::unique_ptr<Expression> convertPrefixExpression(const ASTPrefixExpression& expression);
156 : std::unique_ptr<Statement> convertReturn(const ASTReturnStatement& r);
157 : std::unique_ptr<Expression> getCap(Position position, String name);
158 : std::unique_ptr<Expression> convertSuffixExpression(const ASTSuffixExpression& expression);
159 : std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base,
160 : const String& field);
161 : std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base,
162 : const String& fields);
163 : std::unique_ptr<Expression> convertTernaryExpression(const ASTTernaryExpression& expression);
164 : std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTVarDeclarationStatement& s);
165 : std::unique_ptr<Statement> convertWhile(const ASTWhileStatement& w);
166 :
167 : void checkValid(const Expression& expr);
168 : void markWrittenTo(const Expression& expr, bool readWrite);
169 :
170 : const FunctionDeclaration* fCurrentFunction;
171 : const Program::Settings* fSettings;
172 : std::unordered_map<String, CapValue> fCapsMap;
173 : std::shared_ptr<SymbolTable> fSymbolTable;
174 : int fLoopLevel;
175 : int fSwitchLevel;
176 : ErrorReporter& fErrors;
177 :
178 : friend class AutoSymbolTable;
179 : friend class AutoLoopLevel;
180 : friend class AutoSwitchLevel;
181 : friend class Compiler;
182 : };
183 :
184 : }
185 :
186 : #endif
|