blob: 50bd23621057c778b68ee0b6f51d7ac585382e30 [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
John Stilesddefaee2020-08-11 15:13:26 -040011#include <unordered_map>
John Stilesb8e010c2020-08-11 18:05:39 -040012#include <unordered_set>
Ethan Nicholasdb80f692019-11-22 14:06:12 -050013
Ethan Nicholasfc994162019-06-06 10:04:27 -040014#include "src/sksl/SkSLASTFile.h"
15#include "src/sksl/SkSLASTNode.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050016#include "src/sksl/SkSLErrorReporter.h"
John Stiles4c412bc2020-10-13 11:19:41 -040017#include "src/sksl/SkSLInliner.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
John Stiles2d4f9592020-10-30 10:29:12 -040036class ExternalValue;
Ethan Nicholas1e9f7f32020-10-08 05:28:32 -040037class FunctionCall;
Brian Osman3d87e9f2020-10-08 11:50:22 -040038struct ParsedModule;
Ethan Nicholascb0f4092019-04-19 11:26:50 -040039struct Swizzle;
40
ethannicholasb3058bd2016-07-01 08:22:01 -070041/**
John Stiles810c8cf2020-08-26 19:46:27 -040042 * Intrinsics are passed between the Compiler and the IRGenerator using IRIntrinsicMaps.
43 */
Brian Osman2b469eb2020-09-21 11:32:10 -040044class IRIntrinsicMap {
45public:
46 IRIntrinsicMap(IRIntrinsicMap* parent) : fParent(parent) {}
47
48 void insertOrDie(String key, std::unique_ptr<ProgramElement> element) {
49 SkASSERT(fIntrinsics.find(key) == fIntrinsics.end());
50 fIntrinsics[key] = Intrinsic{std::move(element), false};
51 }
52
Brian Osmanafa18ee2020-10-07 17:47:45 -040053 const ProgramElement* find(const String& key) {
54 auto iter = fIntrinsics.find(key);
55 if (iter == fIntrinsics.end()) {
56 return fParent ? fParent->find(key) : nullptr;
57 }
58 return iter->second.fIntrinsic.get();
59 }
60
Brian Osman2b469eb2020-09-21 11:32:10 -040061 // Only returns an intrinsic that isn't already marked as included, and then marks it.
Brian Osman00a8b5b2020-10-02 09:06:04 -040062 const ProgramElement* findAndInclude(const String& key) {
Brian Osman2b469eb2020-09-21 11:32:10 -040063 auto iter = fIntrinsics.find(key);
64 if (iter == fIntrinsics.end()) {
65 return fParent ? fParent->findAndInclude(key) : nullptr;
66 }
67 if (iter->second.fAlreadyIncluded) {
68 return nullptr;
69 }
70 iter->second.fAlreadyIncluded = true;
71 return iter->second.fIntrinsic.get();
72 }
73
74 void resetAlreadyIncluded() {
75 for (auto& pair : fIntrinsics) {
76 pair.second.fAlreadyIncluded = false;
77 }
78 if (fParent) {
79 fParent->resetAlreadyIncluded();
80 }
81 }
82
83private:
84 struct Intrinsic {
85 std::unique_ptr<ProgramElement> fIntrinsic;
86 bool fAlreadyIncluded = false;
87 };
88
89 std::unordered_map<String, Intrinsic> fIntrinsics;
90 IRIntrinsicMap* fParent = nullptr;
John Stiles810c8cf2020-08-26 19:46:27 -040091};
John Stiles810c8cf2020-08-26 19:46:27 -040092
93/**
Ethan Nicholas11d53972016-11-28 11:23:23 -050094 * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding
ethannicholasb3058bd2016-07-01 08:22:01 -070095 * (unoptimized) intermediate representation (IR).
96 */
97class IRGenerator {
98public:
John Stiles4c412bc2020-10-13 11:19:41 -040099 IRGenerator(const Context* context, Inliner* inliner, ErrorReporter& errorReporter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700100
Brian Osman88cda172020-10-09 12:05:16 -0400101 struct IRBundle {
102 std::vector<std::unique_ptr<ProgramElement>> fElements;
103 std::unique_ptr<ModifiersPool> fModifiers;
104 std::shared_ptr<SymbolTable> fSymbolTable;
105 Program::Inputs fInputs;
106 };
107
108 /**
109 * If externalValues is supplied, those values are registered in the symbol table of the
110 * Program, but ownership is *not* transferred. It is up to the caller to keep them alive.
111 */
112 IRBundle convertProgram(Program::Kind kind,
113 const Program::Settings* settings,
Brian Osmand7e76592020-11-02 12:26:22 -0500114 const ShaderCapsClass* caps,
Brian Osman88cda172020-10-09 12:05:16 -0400115 const ParsedModule& base,
116 bool isBuiltinCode,
117 const char* text,
118 size_t length,
119 const std::vector<std::unique_ptr<ExternalValue>>* externalValues);
ethannicholasb3058bd2016-07-01 08:22:01 -0700120
Ethan Nicholas86a43402017-01-19 13:32:00 -0500121 /**
122 * If both operands are compile-time constants and can be folded, returns an expression
123 * representing the folded value. Otherwise, returns null. Note that unlike most other functions
124 * here, null does not represent a compilation error.
125 */
126 std::unique_ptr<Expression> constantFold(const Expression& left,
127 Token::Kind op,
128 const Expression& right) const;
Ethan Nicholas00543112018-07-31 09:44:36 -0400129
Ethan Nicholas01ec7e82020-10-08 12:10:12 -0400130 // both of these functions return null and report an error if the setting does not exist
131 const Type* typeForSetting(int offset, String name) const;
132 std::unique_ptr<Expression> valueForSetting(int offset, String name) const;
133
Brian Osman88cda172020-10-09 12:05:16 -0400134 const Program::Settings* settings() const { return fSettings; }
135
Ethan Nicholas86a43402017-01-19 13:32:00 -0500136 const Context& fContext;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500137
ethannicholasb3058bd2016-07-01 08:22:01 -0700138private:
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500139 /**
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400140 * Relinquishes ownership of the Modifiers that have been collected so far and returns them.
141 */
142 std::unique_ptr<ModifiersPool> releaseModifiers();
143
ethannicholasb3058bd2016-07-01 08:22:01 -0700144 void pushSymbolTable();
145 void popSymbolTable();
146
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400147 void checkModifiers(int offset, const Modifiers& modifiers, int permitted);
John Stiles8f2a0cf2020-10-13 12:48:21 -0400148 StatementArray convertVarDeclarations(const ASTNode& decl, Variable::Storage storage);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400149 void convertFunction(const ASTNode& f);
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400150 std::unique_ptr<Statement> convertSingleStatement(const ASTNode& statement);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400151 std::unique_ptr<Statement> convertStatement(const ASTNode& statement);
152 std::unique_ptr<Expression> convertExpression(const ASTNode& expression);
153 std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(const ASTNode& m);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400154
Brian Osmand8070392020-09-09 15:50:02 -0400155 const Type* convertType(const ASTNode& type, bool allowVoid = false);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700156 std::unique_ptr<Expression> call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -0500157 const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -0400158 ExpressionArray arguments);
Brian Osman0acb5b52020-09-02 13:45:47 -0400159 CoercionCost callCost(const FunctionDeclaration& function,
John Stiles8e3b6be2020-10-13 11:14:08 -0400160 const ExpressionArray& arguments);
161 std::unique_ptr<Expression> call(int offset,
162 std::unique_ptr<Expression> function,
163 ExpressionArray arguments);
Brian Osman0acb5b52020-09-02 13:45:47 -0400164 CoercionCost coercionCost(const Expression& expr, const Type& type);
ethannicholasd598f792016-07-25 10:08:54 -0700165 std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400166 std::unique_ptr<Block> convertBlock(const ASTNode& block);
167 std::unique_ptr<Statement> convertBreak(const ASTNode& b);
John Stiles8e3b6be2020-10-13 11:14:08 -0400168 std::unique_ptr<Expression> convertNumberConstructor(int offset,
169 const Type& type,
170 ExpressionArray params);
171 std::unique_ptr<Expression> convertCompoundConstructor(int offset,
172 const Type& type,
173 ExpressionArray params);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700174 std::unique_ptr<Expression> convertConstructor(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -0500175 const Type& type,
John Stiles8e3b6be2020-10-13 11:14:08 -0400176 ExpressionArray params);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400177 std::unique_ptr<Statement> convertContinue(const ASTNode& c);
178 std::unique_ptr<Statement> convertDiscard(const ASTNode& d);
179 std::unique_ptr<Statement> convertDo(const ASTNode& d);
180 std::unique_ptr<Statement> convertSwitch(const ASTNode& s);
181 std::unique_ptr<Expression> convertBinaryExpression(const ASTNode& expression);
182 std::unique_ptr<Extension> convertExtension(int offset, StringFragment name);
183 std::unique_ptr<Statement> convertExpressionStatement(const ASTNode& s);
184 std::unique_ptr<Statement> convertFor(const ASTNode& f);
185 std::unique_ptr<Expression> convertIdentifier(const ASTNode& identifier);
186 std::unique_ptr<Statement> convertIf(const ASTNode& s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base,
Ethan Nicholasfc994162019-06-06 10:04:27 -0400188 const ASTNode& index);
189 std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTNode& s);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500190 Modifiers convertModifiers(const Modifiers& m);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400191 std::unique_ptr<Expression> convertPrefixExpression(const ASTNode& expression);
192 std::unique_ptr<Statement> convertReturn(const ASTNode& r);
193 std::unique_ptr<Section> convertSection(const ASTNode& e);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400194 std::unique_ptr<Expression> convertCallExpression(const ASTNode& expression);
195 std::unique_ptr<Expression> convertFieldExpression(const ASTNode& expression);
196 std::unique_ptr<Expression> convertIndexExpression(const ASTNode& expression);
197 std::unique_ptr<Expression> convertPostfixExpression(const ASTNode& expression);
Brian Osman6518d772020-09-10 16:50:06 -0400198 std::unique_ptr<Expression> convertScopeExpression(const ASTNode& expression);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500199 std::unique_ptr<Expression> convertTypeField(int offset, const Type& type,
200 StringFragment field);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500201 std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700202 StringFragment field);
ethannicholasb3058bd2016-07-01 08:22:01 -0700203 std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700204 StringFragment fields);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400205 std::unique_ptr<Expression> convertTernaryExpression(const ASTNode& expression);
206 std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTNode& s);
207 std::unique_ptr<Statement> convertWhile(const ASTNode& w);
208 void convertEnum(const ASTNode& e);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500209 std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000210 // returns a statement which converts sk_Position from device to normalized coordinates
211 std::unique_ptr<Statement> getNormalizeSkPositionCode();
ethannicholasb3058bd2016-07-01 08:22:01 -0700212
213 void checkValid(const Expression& expr);
John Stiles403a3632020-08-20 12:11:48 -0400214 bool setRefKind(Expression& expr, VariableReference::RefKind kind);
Brian Osman3e3db6c2020-08-14 09:42:12 -0400215 bool getConstantInt(const Expression& value, int64_t* out);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400216 void copyIntrinsicIfNeeded(const FunctionDeclaration& function);
Brian Osman8e2ef022020-09-30 13:26:43 -0400217 void cloneBuiltinVariables();
ethannicholasb3058bd2016-07-01 08:22:01 -0700218
Brian Osman88cda172020-10-09 12:05:16 -0400219 Program::Inputs fInputs;
220 const Program::Settings* fSettings = nullptr;
Brian Osmand7e76592020-11-02 12:26:22 -0500221 const ShaderCapsClass* fCaps = nullptr;
Brian Osman88cda172020-10-09 12:05:16 -0400222 Program::Kind fKind;
223
John Stiles4c412bc2020-10-13 11:19:41 -0400224 Inliner* fInliner = nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400225 std::unique_ptr<ASTFile> fFile;
Brian Osman88cda172020-10-09 12:05:16 -0400226 const FunctionDeclaration* fCurrentFunction = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400227 std::unordered_map<String, Program::Settings::Value> fCapsMap;
Brian Osman88cda172020-10-09 12:05:16 -0400228 std::shared_ptr<SymbolTable> fSymbolTable = nullptr;
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400229 // additional statements that need to be inserted before the one that convertStatement is
230 // currently working on
John Stiles8f2a0cf2020-10-13 12:48:21 -0400231 StatementArray fExtraStatements;
John Stiles810c8cf2020-08-26 19:46:27 -0400232 // Symbols which have definitions in the include files.
233 IRIntrinsicMap* fIntrinsics = nullptr;
John Stilesb8e010c2020-08-11 18:05:39 -0400234 std::unordered_set<const FunctionDeclaration*> fReferencedIntrinsics;
Brian Osman88cda172020-10-09 12:05:16 -0400235 int fLoopLevel = 0;
236 int fSwitchLevel = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700237 ErrorReporter& fErrors;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400238 int fInvocations;
Brian Osman88cda172020-10-09 12:05:16 -0400239 std::vector<std::unique_ptr<ProgramElement>>* fProgramElements = nullptr;
240 const Variable* fRTAdjust = nullptr;
241 const Variable* fRTAdjustInterfaceBlock = nullptr;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000242 int fRTAdjustFieldIndex;
John Stiles881a10c2020-09-19 10:13:24 -0400243 bool fCanInline = true;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400244 // true if we are currently processing one of the built-in SkSL include files
245 bool fIsBuiltinCode;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400246 std::unique_ptr<ModifiersPool> fModifiers;
ethannicholasb3058bd2016-07-01 08:22:01 -0700247
248 friend class AutoSymbolTable;
ethannicholas22f939e2016-10-13 13:25:34 -0700249 friend class AutoLoopLevel;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500250 friend class AutoSwitchLevel;
John Stilesd1c4dac2020-08-11 18:50:50 -0400251 friend class AutoDisableInline;
ethannicholasb3058bd2016-07-01 08:22:01 -0700252 friend class Compiler;
253};
254
John Stilesa6841be2020-08-06 14:11:56 -0400255} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700256
257#endif