blob: 13b20fbbccfa61fcf7180ae6cbc72c576f3ee53f [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"
30#include "ast/SkSLASTStatement.h"
31#include "ast/SkSLASTSuffixExpression.h"
32#include "ast/SkSLASTTernaryExpression.h"
33#include "ast/SkSLASTVarDeclaration.h"
34#include "ast/SkSLASTVarDeclarationStatement.h"
35#include "ast/SkSLASTWhileStatement.h"
36#include "ir/SkSLBlock.h"
37#include "ir/SkSLExpression.h"
38#include "ir/SkSLExtension.h"
39#include "ir/SkSLFunctionDefinition.h"
40#include "ir/SkSLInterfaceBlock.h"
41#include "ir/SkSLModifiers.h"
ethannicholas5961bc92016-10-12 06:39:56 -070042#include "ir/SkSLModifiersDeclaration.h"
Ethan Nicholas941e7e22016-12-12 15:33:30 -050043#include "ir/SkSLProgram.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070044#include "ir/SkSLSymbolTable.h"
45#include "ir/SkSLStatement.h"
46#include "ir/SkSLType.h"
47#include "ir/SkSLTypeReference.h"
ethannicholas22f939e2016-10-13 13:25:34 -070048#include "ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070049
50namespace SkSL {
51
Ethan Nicholas3605ace2016-11-21 15:59:48 -050052struct CapValue {
53 CapValue()
54 : fKind(kInt_Kind)
55 , fValue(-1) {
56 ASSERT(false);
57 }
58
59 CapValue(bool b)
60 : fKind(kBool_Kind)
61 , fValue(b) {}
62
63 CapValue(int i)
64 : fKind(kInt_Kind)
65 , fValue(i) {}
66
67 enum {
68 kBool_Kind,
69 kInt_Kind,
70 } fKind;
71 int fValue;
72};
73
ethannicholasb3058bd2016-07-01 08:22:01 -070074/**
Ethan Nicholas11d53972016-11-28 11:23:23 -050075 * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding
ethannicholasb3058bd2016-07-01 08:22:01 -070076 * (unoptimized) intermediate representation (IR).
77 */
78class IRGenerator {
79public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050080 IRGenerator(const Context* context, std::shared_ptr<SymbolTable> root,
ethannicholasd598f792016-07-25 10:08:54 -070081 ErrorReporter& errorReporter);
ethannicholasb3058bd2016-07-01 08:22:01 -070082
Ethan Nicholas11d53972016-11-28 11:23:23 -050083 std::unique_ptr<VarDeclarations> convertVarDeclarations(const ASTVarDeclarations& decl,
ethannicholas14fe8cc2016-09-07 13:37:16 -070084 Variable::Storage storage);
ethannicholasb3058bd2016-07-01 08:22:01 -070085 std::unique_ptr<FunctionDefinition> convertFunction(const ASTFunction& f);
86 std::unique_ptr<Statement> convertStatement(const ASTStatement& statement);
87 std::unique_ptr<Expression> convertExpression(const ASTExpression& expression);
ethannicholas5961bc92016-10-12 06:39:56 -070088 std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(
89 const ASTModifiersDeclaration& m);
ethannicholasb3058bd2016-07-01 08:22:01 -070090
Ethan Nicholas941e7e22016-12-12 15:33:30 -050091 Program::Inputs fInputs;
92
ethannicholasb3058bd2016-07-01 08:22:01 -070093private:
Ethan Nicholas3605ace2016-11-21 15:59:48 -050094 /**
Ethan Nicholas941e7e22016-12-12 15:33:30 -050095 * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the
96 * settings.
Ethan Nicholas3605ace2016-11-21 15:59:48 -050097 */
Ethan Nicholas941e7e22016-12-12 15:33:30 -050098 void start(const Program::Settings* settings);
Ethan Nicholas3605ace2016-11-21 15:59:48 -050099
100 /**
101 * Performs cleanup after compilation is complete.
102 */
103 void finish();
104
ethannicholasb3058bd2016-07-01 08:22:01 -0700105 void pushSymbolTable();
106 void popSymbolTable();
107
ethannicholasd598f792016-07-25 10:08:54 -0700108 const Type* convertType(const ASTType& type);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500109 std::unique_ptr<Expression> call(Position position,
110 const FunctionDeclaration& function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700111 std::vector<std::unique_ptr<Expression>> arguments);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500112 bool determineCallCost(const FunctionDeclaration& function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700113 const std::vector<std::unique_ptr<Expression>>& arguments,
114 int* outCost);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500115 std::unique_ptr<Expression> call(Position position, std::unique_ptr<Expression> function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700116 std::vector<std::unique_ptr<Expression>> arguments);
ethannicholasd598f792016-07-25 10:08:54 -0700117 std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700118 std::unique_ptr<Block> convertBlock(const ASTBlock& block);
119 std::unique_ptr<Statement> convertBreak(const ASTBreakStatement& b);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500120 std::unique_ptr<Expression> convertConstructor(Position position,
121 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -0700122 std::vector<std::unique_ptr<Expression>> params);
123 std::unique_ptr<Statement> convertContinue(const ASTContinueStatement& c);
124 std::unique_ptr<Statement> convertDiscard(const ASTDiscardStatement& d);
125 std::unique_ptr<Statement> convertDo(const ASTDoStatement& d);
126 std::unique_ptr<Expression> convertBinaryExpression(const ASTBinaryExpression& expression);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500127 // Returns null if it cannot fold the expression. Note that unlike most other functions here, a
ethannicholas08a92112016-11-09 13:26:45 -0800128 // null return does not represent a compilation error.
129 std::unique_ptr<Expression> constantFold(const Expression& left,
130 Token::Kind op,
131 const Expression& right);
ethannicholasb3058bd2016-07-01 08:22:01 -0700132 std::unique_ptr<Extension> convertExtension(const ASTExtension& e);
133 std::unique_ptr<Statement> convertExpressionStatement(const ASTExpressionStatement& s);
134 std::unique_ptr<Statement> convertFor(const ASTForStatement& f);
135 std::unique_ptr<Expression> convertIdentifier(const ASTIdentifier& identifier);
136 std::unique_ptr<Statement> convertIf(const ASTIfStatement& s);
137 std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base,
138 const ASTExpression& index);
139 std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTInterfaceBlock& s);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500140 Modifiers convertModifiers(const Modifiers& m);
ethannicholasb3058bd2016-07-01 08:22:01 -0700141 std::unique_ptr<Expression> convertPrefixExpression(const ASTPrefixExpression& expression);
142 std::unique_ptr<Statement> convertReturn(const ASTReturnStatement& r);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500143 std::unique_ptr<Expression> getCap(Position position, SkString name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700144 std::unique_ptr<Expression> convertSuffixExpression(const ASTSuffixExpression& expression);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500145 std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500146 const SkString& field);
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500148 const SkString& fields);
ethannicholasb3058bd2016-07-01 08:22:01 -0700149 std::unique_ptr<Expression> convertTernaryExpression(const ASTTernaryExpression& expression);
150 std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTVarDeclarationStatement& s);
151 std::unique_ptr<Statement> convertWhile(const ASTWhileStatement& w);
152
153 void checkValid(const Expression& expr);
ethannicholasd598f792016-07-25 10:08:54 -0700154 void markReadFrom(const Variable& var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700155 void markWrittenTo(const Expression& expr);
156
ethannicholasd598f792016-07-25 10:08:54 -0700157 const Context& fContext;
158 const FunctionDeclaration* fCurrentFunction;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500159 const Program::Settings* fSettings;
160 std::unordered_map<SkString, CapValue> fCapsMap;
ethannicholasb3058bd2016-07-01 08:22:01 -0700161 std::shared_ptr<SymbolTable> fSymbolTable;
ethannicholas22f939e2016-10-13 13:25:34 -0700162 int fLoopLevel;
ethannicholasb3058bd2016-07-01 08:22:01 -0700163 ErrorReporter& fErrors;
164
165 friend class AutoSymbolTable;
ethannicholas22f939e2016-10-13 13:25:34 -0700166 friend class AutoLoopLevel;
ethannicholasb3058bd2016-07-01 08:22:01 -0700167 friend class Compiler;
168};
169
170}
171
172#endif