blob: 0a2979dd736d5644c4664ca446ccda99fab502dc [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#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"
ethannicholas5961bc92016-10-12 06:39:56 -070027#include "ast/SkSLASTModifiersDeclaration.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070028#include "ast/SkSLASTPrefixExpression.h"
29#include "ast/SkSLASTReturnStatement.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040030#include "ast/SkSLASTSection.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070031#include "ast/SkSLASTStatement.h"
32#include "ast/SkSLASTSuffixExpression.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050033#include "ast/SkSLASTSwitchStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070034#include "ast/SkSLASTTernaryExpression.h"
35#include "ast/SkSLASTVarDeclaration.h"
36#include "ast/SkSLASTVarDeclarationStatement.h"
37#include "ast/SkSLASTWhileStatement.h"
38#include "ir/SkSLBlock.h"
39#include "ir/SkSLExpression.h"
40#include "ir/SkSLExtension.h"
41#include "ir/SkSLFunctionDefinition.h"
42#include "ir/SkSLInterfaceBlock.h"
43#include "ir/SkSLModifiers.h"
ethannicholas5961bc92016-10-12 06:39:56 -070044#include "ir/SkSLModifiersDeclaration.h"
Ethan Nicholas941e7e22016-12-12 15:33:30 -050045#include "ir/SkSLProgram.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040046#include "ir/SkSLSection.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070047#include "ir/SkSLSymbolTable.h"
48#include "ir/SkSLStatement.h"
49#include "ir/SkSLType.h"
50#include "ir/SkSLTypeReference.h"
ethannicholas22f939e2016-10-13 13:25:34 -070051#include "ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070052
53namespace SkSL {
54
55/**
Ethan Nicholas11d53972016-11-28 11:23:23 -050056 * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding
ethannicholasb3058bd2016-07-01 08:22:01 -070057 * (unoptimized) intermediate representation (IR).
58 */
59class IRGenerator {
60public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050061 IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root,
ethannicholasd598f792016-07-25 10:08:54 -070062 ErrorReporter& errorReporter);
ethannicholasb3058bd2016-07-01 08:22:01 -070063
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070064 void convertProgram(const char* text,
65 size_t length,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040066 SymbolTable& types,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040067 std::vector<std::unique_ptr<ProgramElement>>* result);
ethannicholasb3058bd2016-07-01 08:22:01 -070068
Ethan Nicholas86a43402017-01-19 13:32:00 -050069 /**
70 * If both operands are compile-time constants and can be folded, returns an expression
71 * representing the folded value. Otherwise, returns null. Note that unlike most other functions
72 * here, null does not represent a compilation error.
73 */
74 std::unique_ptr<Expression> constantFold(const Expression& left,
75 Token::Kind op,
76 const Expression& right) const;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050077 Program::Inputs fInputs;
Ethan Nicholas762466e2017-06-29 10:03:38 -040078 const Program::Settings* fSettings;
Ethan Nicholas86a43402017-01-19 13:32:00 -050079 const Context& fContext;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050080
ethannicholasb3058bd2016-07-01 08:22:01 -070081private:
Ethan Nicholas3605ace2016-11-21 15:59:48 -050082 /**
Ethan Nicholas941e7e22016-12-12 15:33:30 -050083 * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the
84 * settings.
Ethan Nicholas3605ace2016-11-21 15:59:48 -050085 */
Ethan Nicholas941e7e22016-12-12 15:33:30 -050086 void start(const Program::Settings* settings);
Ethan Nicholas3605ace2016-11-21 15:59:48 -050087
88 /**
89 * Performs cleanup after compilation is complete.
90 */
91 void finish();
92
ethannicholasb3058bd2016-07-01 08:22:01 -070093 void pushSymbolTable();
94 void popSymbolTable();
95
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040096 std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTVarDeclarations& decl,
97 Variable::Storage storage);
98 void convertFunction(const ASTFunction& f,
99 std::vector<std::unique_ptr<ProgramElement>>* out);
100 std::unique_ptr<Statement> convertStatement(const ASTStatement& statement);
101 std::unique_ptr<Expression> convertExpression(const ASTExpression& expression);
102 std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(
103 const ASTModifiersDeclaration& m);
104
ethannicholasd598f792016-07-25 10:08:54 -0700105 const Type* convertType(const ASTType& type);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700106 std::unique_ptr<Expression> call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -0500107 const FunctionDeclaration& function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 std::vector<std::unique_ptr<Expression>> arguments);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400109 int callCost(const FunctionDeclaration& function,
110 const std::vector<std::unique_ptr<Expression>>& arguments);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700111 std::unique_ptr<Expression> call(int offset, std::unique_ptr<Expression> function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700112 std::vector<std::unique_ptr<Expression>> arguments);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400113 int coercionCost(const Expression& expr, const Type& type);
ethannicholasd598f792016-07-25 10:08:54 -0700114 std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700115 std::unique_ptr<Block> convertBlock(const ASTBlock& block);
116 std::unique_ptr<Statement> convertBreak(const ASTBreakStatement& b);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500117 std::unique_ptr<Expression> convertNumberConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700118 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -0500119 const Type& type,
120 std::vector<std::unique_ptr<Expression>> params);
121 std::unique_ptr<Expression> convertCompoundConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700122 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -0500123 const Type& type,
124 std::vector<std::unique_ptr<Expression>> params);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700125 std::unique_ptr<Expression> convertConstructor(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -0500126 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -0700127 std::vector<std::unique_ptr<Expression>> params);
128 std::unique_ptr<Statement> convertContinue(const ASTContinueStatement& c);
129 std::unique_ptr<Statement> convertDiscard(const ASTDiscardStatement& d);
130 std::unique_ptr<Statement> convertDo(const ASTDoStatement& d);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500131 std::unique_ptr<Statement> convertSwitch(const ASTSwitchStatement& s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700132 std::unique_ptr<Expression> convertBinaryExpression(const ASTBinaryExpression& expression);
133 std::unique_ptr<Extension> convertExtension(const ASTExtension& e);
134 std::unique_ptr<Statement> convertExpressionStatement(const ASTExpressionStatement& s);
135 std::unique_ptr<Statement> convertFor(const ASTForStatement& f);
136 std::unique_ptr<Expression> convertIdentifier(const ASTIdentifier& identifier);
137 std::unique_ptr<Statement> convertIf(const ASTIfStatement& s);
138 std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base,
139 const ASTExpression& index);
140 std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTInterfaceBlock& s);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500141 Modifiers convertModifiers(const Modifiers& m);
ethannicholasb3058bd2016-07-01 08:22:01 -0700142 std::unique_ptr<Expression> convertPrefixExpression(const ASTPrefixExpression& expression);
143 std::unique_ptr<Statement> convertReturn(const ASTReturnStatement& r);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400144 std::unique_ptr<Section> convertSection(const ASTSection& e);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700145 std::unique_ptr<Expression> getCap(int offset, String name);
146 std::unique_ptr<Expression> getArg(int offset, String name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 std::unique_ptr<Expression> convertSuffixExpression(const ASTSuffixExpression& expression);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500148 std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700149 StringFragment field);
ethannicholasb3058bd2016-07-01 08:22:01 -0700150 std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700151 StringFragment fields);
ethannicholasb3058bd2016-07-01 08:22:01 -0700152 std::unique_ptr<Expression> convertTernaryExpression(const ASTTernaryExpression& expression);
153 std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTVarDeclarationStatement& s);
154 std::unique_ptr<Statement> convertWhile(const ASTWhileStatement& w);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400155 std::unique_ptr<Block> applyInvocationIDWorkaround(
156 std::unique_ptr<Block> main,
157 std::vector<std::unique_ptr<ProgramElement>>* out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700158
Ethan Nicholas762466e2017-06-29 10:03:38 -0400159 /**
160 * Wraps an expression in code that applies a colorspace transformation to it. This is used
161 * to implement texture(sampler, coord, colorSpaceXForm).
162 */
163 std::unique_ptr<Expression> applyColorSpace(std::unique_ptr<Expression> texture,
Ethan Nicholas68990be2017-07-13 09:36:52 -0400164 std::unique_ptr<Expression> xform);
Ethan Nicholas5338f992017-04-19 15:54:07 -0400165 void fixRectSampling(std::vector<std::unique_ptr<Expression>>& arguments);
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 void checkValid(const Expression& expr);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500167 void markWrittenTo(const Expression& expr, bool readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -0700168
ethannicholasd598f792016-07-25 10:08:54 -0700169 const FunctionDeclaration* fCurrentFunction;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400170 std::unordered_map<String, Program::Settings::Value> fCapsMap;
171 std::shared_ptr<SymbolTable> fRootSymbolTable;
ethannicholasb3058bd2016-07-01 08:22:01 -0700172 std::shared_ptr<SymbolTable> fSymbolTable;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400173 // holds extra temp variable declarations needed for the current function
174 std::vector<std::unique_ptr<Statement>> fExtraVars;
ethannicholas22f939e2016-10-13 13:25:34 -0700175 int fLoopLevel;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500176 int fSwitchLevel;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400177 // count of temporary variables we have created
178 int fTmpCount;
ethannicholasb3058bd2016-07-01 08:22:01 -0700179 ErrorReporter& fErrors;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400180 int fInvocations;
ethannicholasb3058bd2016-07-01 08:22:01 -0700181
182 friend class AutoSymbolTable;
ethannicholas22f939e2016-10-13 13:25:34 -0700183 friend class AutoLoopLevel;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500184 friend class AutoSwitchLevel;
ethannicholasb3058bd2016-07-01 08:22:01 -0700185 friend class Compiler;
186};
187
188}
189
190#endif