blob: d4843f91f8a22f89218a5f9d8869f54c65379d67 [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
Ethan Nicholasdb80f692019-11-22 14:06:12 -050011#include <map>
John Stilesddefaee2020-08-11 15:13:26 -040012#include <unordered_map>
John Stilesb8e010c2020-08-11 18:05:39 -040013#include <unordered_set>
Ethan Nicholasdb80f692019-11-22 14:06:12 -050014
Ethan Nicholasfc994162019-06-06 10:04:27 -040015#include "src/sksl/SkSLASTFile.h"
16#include "src/sksl/SkSLASTNode.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050017#include "src/sksl/SkSLErrorReporter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/sksl/ir/SkSLBlock.h"
19#include "src/sksl/ir/SkSLExpression.h"
20#include "src/sksl/ir/SkSLExtension.h"
21#include "src/sksl/ir/SkSLFunctionDefinition.h"
22#include "src/sksl/ir/SkSLInterfaceBlock.h"
23#include "src/sksl/ir/SkSLModifiers.h"
24#include "src/sksl/ir/SkSLModifiersDeclaration.h"
25#include "src/sksl/ir/SkSLProgram.h"
26#include "src/sksl/ir/SkSLSection.h"
27#include "src/sksl/ir/SkSLStatement.h"
28#include "src/sksl/ir/SkSLSymbolTable.h"
29#include "src/sksl/ir/SkSLType.h"
30#include "src/sksl/ir/SkSLTypeReference.h"
31#include "src/sksl/ir/SkSLVarDeclarations.h"
32#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070033
34namespace SkSL {
35
Ethan Nicholascb0f4092019-04-19 11:26:50 -040036struct Swizzle;
37
ethannicholasb3058bd2016-07-01 08:22:01 -070038/**
Ethan Nicholas11d53972016-11-28 11:23:23 -050039 * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding
ethannicholasb3058bd2016-07-01 08:22:01 -070040 * (unoptimized) intermediate representation (IR).
41 */
42class IRGenerator {
43public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050044 IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root,
ethannicholasd598f792016-07-25 10:08:54 -070045 ErrorReporter& errorReporter);
ethannicholasb3058bd2016-07-01 08:22:01 -070046
Robert Phillipsfe8da172018-01-24 14:52:02 +000047 void convertProgram(Program::Kind kind,
48 const char* text,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070049 size_t length,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040050 std::vector<std::unique_ptr<ProgramElement>>* result);
ethannicholasb3058bd2016-07-01 08:22:01 -070051
Ethan Nicholas86a43402017-01-19 13:32:00 -050052 /**
53 * If both operands are compile-time constants and can be folded, returns an expression
54 * representing the folded value. Otherwise, returns null. Note that unlike most other functions
55 * here, null does not represent a compilation error.
56 */
57 std::unique_ptr<Expression> constantFold(const Expression& left,
58 Token::Kind op,
59 const Expression& right) const;
Ethan Nicholas00543112018-07-31 09:44:36 -040060
Ethan Nicholas941e7e22016-12-12 15:33:30 -050061 Program::Inputs fInputs;
Ethan Nicholas762466e2017-06-29 10:03:38 -040062 const Program::Settings* fSettings;
Ethan Nicholas86a43402017-01-19 13:32:00 -050063 const Context& fContext;
Ethan Nicholas00543112018-07-31 09:44:36 -040064 Program::Kind fKind;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050065
ethannicholasb3058bd2016-07-01 08:22:01 -070066private:
Ethan Nicholas3605ace2016-11-21 15:59:48 -050067 /**
Ethan Nicholas941e7e22016-12-12 15:33:30 -050068 * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the
69 * settings.
Ethan Nicholas3605ace2016-11-21 15:59:48 -050070 */
Ethan Nicholas3c6ae622018-04-24 13:06:09 -040071 void start(const Program::Settings* settings,
Ethan Nicholasc18bb512020-07-28 14:46:53 -040072 std::vector<std::unique_ptr<ProgramElement>>* inherited,
73 bool isBuiltinCode = false);
Ethan Nicholas3605ace2016-11-21 15:59:48 -050074
75 /**
76 * Performs cleanup after compilation is complete.
77 */
78 void finish();
79
ethannicholasb3058bd2016-07-01 08:22:01 -070080 void pushSymbolTable();
81 void popSymbolTable();
82
Ethan Nicholasfc994162019-06-06 10:04:27 -040083 std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTNode& decl,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040084 Variable::Storage storage);
Ethan Nicholasfc994162019-06-06 10:04:27 -040085 void convertFunction(const ASTNode& f);
Ethan Nicholas70728ef2020-05-28 07:09:00 -040086 std::unique_ptr<Statement> convertSingleStatement(const ASTNode& statement);
Ethan Nicholasfc994162019-06-06 10:04:27 -040087 std::unique_ptr<Statement> convertStatement(const ASTNode& statement);
88 std::unique_ptr<Expression> convertExpression(const ASTNode& expression);
89 std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(const ASTNode& m);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040090
Ethan Nicholasfc994162019-06-06 10:04:27 -040091 const Type* convertType(const ASTNode& type);
John Stilesddefaee2020-08-11 15:13:26 -040092 std::unique_ptr<Expression> inlineExpression(
93 int offset,
94 std::unordered_map<const Variable*, const Variable*>* varMap,
95 const Expression& expression);
96 std::unique_ptr<Statement> inlineStatement(
97 int offset,
98 std::unordered_map<const Variable*, const Variable*>* varMap,
99 const Variable* returnVar,
100 bool haveEarlyReturns,
101 const Statement& statement);
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400102 std::unique_ptr<Expression> inlineCall(int offset, const FunctionDefinition& function,
103 std::vector<std::unique_ptr<Expression>> arguments);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700104 std::unique_ptr<Expression> call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -0500105 const FunctionDeclaration& function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700106 std::vector<std::unique_ptr<Expression>> arguments);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400107 int callCost(const FunctionDeclaration& function,
108 const std::vector<std::unique_ptr<Expression>>& arguments);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700109 std::unique_ptr<Expression> call(int offset, std::unique_ptr<Expression> function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 std::vector<std::unique_ptr<Expression>> arguments);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400111 int coercionCost(const Expression& expr, const Type& type);
ethannicholasd598f792016-07-25 10:08:54 -0700112 std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400113 std::unique_ptr<Block> convertBlock(const ASTNode& block);
114 std::unique_ptr<Statement> convertBreak(const ASTNode& b);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500115 std::unique_ptr<Expression> convertNumberConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700116 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -0500117 const Type& type,
118 std::vector<std::unique_ptr<Expression>> params);
119 std::unique_ptr<Expression> convertCompoundConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700120 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -0500121 const Type& type,
122 std::vector<std::unique_ptr<Expression>> params);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700123 std::unique_ptr<Expression> convertConstructor(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -0500124 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -0700125 std::vector<std::unique_ptr<Expression>> params);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400126 std::unique_ptr<Statement> convertContinue(const ASTNode& c);
127 std::unique_ptr<Statement> convertDiscard(const ASTNode& d);
128 std::unique_ptr<Statement> convertDo(const ASTNode& d);
129 std::unique_ptr<Statement> convertSwitch(const ASTNode& s);
130 std::unique_ptr<Expression> convertBinaryExpression(const ASTNode& expression);
131 std::unique_ptr<Extension> convertExtension(int offset, StringFragment name);
132 std::unique_ptr<Statement> convertExpressionStatement(const ASTNode& s);
133 std::unique_ptr<Statement> convertFor(const ASTNode& f);
134 std::unique_ptr<Expression> convertIdentifier(const ASTNode& identifier);
135 std::unique_ptr<Statement> convertIf(const ASTNode& s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700136 std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base,
Ethan Nicholasfc994162019-06-06 10:04:27 -0400137 const ASTNode& index);
138 std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTNode& s);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500139 Modifiers convertModifiers(const Modifiers& m);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400140 std::unique_ptr<Expression> convertPrefixExpression(const ASTNode& expression);
141 std::unique_ptr<Statement> convertReturn(const ASTNode& r);
142 std::unique_ptr<Section> convertSection(const ASTNode& e);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700143 std::unique_ptr<Expression> getCap(int offset, String name);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400144 std::unique_ptr<Expression> convertCallExpression(const ASTNode& expression);
145 std::unique_ptr<Expression> convertFieldExpression(const ASTNode& expression);
146 std::unique_ptr<Expression> convertIndexExpression(const ASTNode& expression);
147 std::unique_ptr<Expression> convertPostfixExpression(const ASTNode& expression);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400148 std::unique_ptr<Expression> findEnumRef(int offset,
149 const Type& type,
150 StringFragment field,
151 std::vector<std::unique_ptr<ProgramElement>>& elements);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500152 std::unique_ptr<Expression> convertTypeField(int offset, const Type& type,
153 StringFragment field);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500154 std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700155 StringFragment field);
ethannicholasb3058bd2016-07-01 08:22:01 -0700156 std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700157 StringFragment fields);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400158 std::unique_ptr<Expression> convertTernaryExpression(const ASTNode& expression);
159 std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTNode& s);
160 std::unique_ptr<Statement> convertWhile(const ASTNode& w);
161 void convertEnum(const ASTNode& e);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500162 std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000163 // returns a statement which converts sk_Position from device to normalized coordinates
164 std::unique_ptr<Statement> getNormalizeSkPositionCode();
ethannicholasb3058bd2016-07-01 08:22:01 -0700165
166 void checkValid(const Expression& expr);
Ethan Nicholas4fadce42020-07-30 13:29:30 -0400167 bool setRefKind(const Expression& expr, VariableReference::RefKind kind);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500168 void getConstantInt(const Expression& value, int64_t* out);
Ethan Nicholascb0f4092019-04-19 11:26:50 -0400169 bool checkSwizzleWrite(const Swizzle& swizzle);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400170 void copyIntrinsicIfNeeded(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700171
Ethan Nicholasfc994162019-06-06 10:04:27 -0400172 std::unique_ptr<ASTFile> fFile;
ethannicholasd598f792016-07-25 10:08:54 -0700173 const FunctionDeclaration* fCurrentFunction;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400174 std::unordered_map<String, Program::Settings::Value> fCapsMap;
175 std::shared_ptr<SymbolTable> fRootSymbolTable;
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 std::shared_ptr<SymbolTable> fSymbolTable;
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400177 // additional statements that need to be inserted before the one that convertStatement is
178 // currently working on
179 std::vector<std::unique_ptr<Statement>> fExtraStatements;
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500180 // Symbols which have definitions in the include files. The bool tells us whether this
181 // intrinsic has been included already.
Brian Osman08f986d2020-05-13 17:06:46 -0400182 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* fIntrinsics = nullptr;
John Stilesb8e010c2020-08-11 18:05:39 -0400183 std::unordered_set<const FunctionDeclaration*> fReferencedIntrinsics;
ethannicholas22f939e2016-10-13 13:25:34 -0700184 int fLoopLevel;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500185 int fSwitchLevel;
ethannicholasb3058bd2016-07-01 08:22:01 -0700186 ErrorReporter& fErrors;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400187 int fInvocations;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400188 std::vector<std::unique_ptr<ProgramElement>>* fInherited;
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500189 std::vector<std::unique_ptr<ProgramElement>>* fProgramElements;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400190 const Variable* fSkPerVertex = nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400191 const Variable* fRTAdjust;
192 const Variable* fRTAdjustInterfaceBlock;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000193 int fRTAdjustFieldIndex;
Michael Ludwig9861b7c2020-06-23 18:37:17 -0400194 int fInlineVarCounter;
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400195 bool fCanInline = true;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400196 // true if we are currently processing one of the built-in SkSL include files
197 bool fIsBuiltinCode;
ethannicholasb3058bd2016-07-01 08:22:01 -0700198
199 friend class AutoSymbolTable;
ethannicholas22f939e2016-10-13 13:25:34 -0700200 friend class AutoLoopLevel;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500201 friend class AutoSwitchLevel;
ethannicholasb3058bd2016-07-01 08:22:01 -0700202 friend class Compiler;
203};
204
John Stilesa6841be2020-08-06 14:11:56 -0400205} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700206
207#endif