blob: 1cd4232f2d6df93eb6859bed19b125b5d826ffea [file] [log] [blame]
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001/*
2 * Copyright 2019 Google LLC
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_BYTECODEGENERATOR
9#define SKSL_BYTECODEGENERATOR
10
11#include <stack>
12#include <tuple>
13#include <unordered_map>
14
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/sksl/SkSLByteCode.h"
16#include "src/sksl/SkSLCodeGenerator.h"
17#include "src/sksl/SkSLMemoryLayout.h"
18#include "src/sksl/ir/SkSLBinaryExpression.h"
19#include "src/sksl/ir/SkSLBlock.h"
20#include "src/sksl/ir/SkSLBoolLiteral.h"
21#include "src/sksl/ir/SkSLBreakStatement.h"
22#include "src/sksl/ir/SkSLConstructor.h"
23#include "src/sksl/ir/SkSLContinueStatement.h"
24#include "src/sksl/ir/SkSLDoStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040025#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040026#include "src/sksl/ir/SkSLExternalValueReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/sksl/ir/SkSLExpressionStatement.h"
28#include "src/sksl/ir/SkSLFieldAccess.h"
29#include "src/sksl/ir/SkSLFloatLiteral.h"
30#include "src/sksl/ir/SkSLForStatement.h"
31#include "src/sksl/ir/SkSLFunctionCall.h"
32#include "src/sksl/ir/SkSLFunctionDeclaration.h"
33#include "src/sksl/ir/SkSLFunctionDefinition.h"
34#include "src/sksl/ir/SkSLIfStatement.h"
35#include "src/sksl/ir/SkSLIndexExpression.h"
36#include "src/sksl/ir/SkSLIntLiteral.h"
37#include "src/sksl/ir/SkSLInterfaceBlock.h"
38#include "src/sksl/ir/SkSLNullLiteral.h"
39#include "src/sksl/ir/SkSLPostfixExpression.h"
40#include "src/sksl/ir/SkSLPrefixExpression.h"
41#include "src/sksl/ir/SkSLProgramElement.h"
42#include "src/sksl/ir/SkSLReturnStatement.h"
43#include "src/sksl/ir/SkSLStatement.h"
44#include "src/sksl/ir/SkSLSwitchStatement.h"
45#include "src/sksl/ir/SkSLSwizzle.h"
46#include "src/sksl/ir/SkSLTernaryExpression.h"
47#include "src/sksl/ir/SkSLVarDeclarations.h"
48#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
49#include "src/sksl/ir/SkSLVariableReference.h"
50#include "src/sksl/ir/SkSLWhileStatement.h"
51#include "src/sksl/spirv.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040052
53namespace SkSL {
54
55class ByteCodeGenerator : public CodeGenerator {
56public:
57 class LValue {
58 public:
59 LValue(ByteCodeGenerator& generator)
60 : fGenerator(generator) {}
61
62 virtual ~LValue() {}
63
64 /**
65 * Stack before call: ... lvalue
66 * Stack after call: ... lvalue load
67 */
68 virtual void load() = 0;
69
70 /**
71 * Stack before call: ... lvalue value
72 * Stack after call: ...
73 */
74 virtual void store() = 0;
75
76 protected:
77 ByteCodeGenerator& fGenerator;
78 };
79
80 ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
81 ByteCode* output)
82 : INHERITED(program, errors, nullptr)
83 , fContext(*context)
84 , fOutput(output) {}
85
86 bool generateCode() override;
87
88 void write8(uint8_t b);
89
90 void write16(uint16_t b);
91
92 void write32(uint32_t b);
93
94 void write(ByteCodeInstruction inst);
95
96 /**
97 * Based on 'type', writes the s (signed), u (unsigned), or f (float) instruction.
98 */
99 void writeTypedInstruction(const Type& type, ByteCodeInstruction s, ByteCodeInstruction u,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400100 ByteCodeInstruction f, int count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400101
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400102private:
103 // reserves 16 bits in the output code, to be filled in later with an address once we determine
104 // it
105 class DeferredLocation {
106 public:
107 DeferredLocation(ByteCodeGenerator* generator)
108 : fGenerator(*generator)
109 , fOffset(generator->fCode->size()) {
110 generator->write16(0);
111 }
112
113#ifdef SK_DEBUG
114 ~DeferredLocation() {
115 SkASSERT(fSet);
116 }
117#endif
118
119 void set() {
120 int target = fGenerator.fCode->size();
121 SkASSERT(target <= 65535);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400122 (*fGenerator.fCode)[fOffset] = target;
123 (*fGenerator.fCode)[fOffset + 1] = target >> 8;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400124#ifdef SK_DEBUG
125 fSet = true;
126#endif
127 }
128
129 private:
130 ByteCodeGenerator& fGenerator;
131 size_t fOffset;
132#ifdef SK_DEBUG
133 bool fSet = false;
134#endif
135 };
136
Brian Osman226668a2019-05-14 16:47:30 -0400137 class DeferredCallTarget {
138 public:
139 DeferredCallTarget(ByteCodeGenerator* generator, const FunctionDeclaration& function)
140 : fGenerator(*generator)
141 , fCode(generator->fCode)
142 , fOffset(generator->fCode->size())
143 , fFunction(function) {
144 generator->write8(0);
145 }
146
147 bool set() {
148 size_t idx;
149 const auto& functions(fGenerator.fOutput->fFunctions);
150 for (idx = 0; idx < functions.size(); ++idx) {
151 if (fFunction.matches(functions[idx]->fDeclaration)) {
152 break;
153 }
154 }
155 if (idx > 255 || idx > functions.size()) {
156 SkASSERT(false);
157 return false;
158 }
159 (*fCode)[fOffset] = idx;
160 return true;
161 }
162
163 private:
164 ByteCodeGenerator& fGenerator;
165 std::vector<uint8_t>* fCode;
166 size_t fOffset;
167 const FunctionDeclaration& fFunction;
168 };
169
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400170 /**
171 * Returns the local slot into which var should be stored, allocating a new slot if it has not
172 * already been assigned one. Compound variables (e.g. vectors) will consume more than one local
173 * slot, with the getLocation return value indicating where the first element should be stored.
174 */
175 int getLocation(const Variable& var);
176
177 std::unique_ptr<ByteCodeFunction> writeFunction(const FunctionDefinition& f);
178
179 void writeVarDeclarations(const VarDeclarations& decl);
180
181 void writeVariableReference(const VariableReference& ref);
182
183 void writeExpression(const Expression& expr);
184
185 /**
186 * Pushes whatever values are required by the lvalue onto the stack, and returns an LValue
187 * permitting loads and stores to it.
188 */
189 std::unique_ptr<LValue> getLValue(const Expression& expr);
190
191 void writeFunctionCall(const FunctionCall& c);
192
193 void writeConstructor(const Constructor& c);
194
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400195 void writeExternalFunctionCall(const ExternalFunctionCall& c);
196
Ethan Nicholas91164d12019-05-15 15:29:54 -0400197 void writeExternalValue(const ExternalValueReference& r);
198
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400199 void writeFieldAccess(const FieldAccess& f);
200
201 void writeSwizzle(const Swizzle& swizzle);
202
203 void writeBinaryExpression(const BinaryExpression& b);
204
205 void writeTernaryExpression(const TernaryExpression& t);
206
207 void writeIndexExpression(const IndexExpression& expr);
208
209 void writeLogicalAnd(const BinaryExpression& b);
210
211 void writeLogicalOr(const BinaryExpression& o);
212
213 void writeNullLiteral(const NullLiteral& n);
214
215 void writePrefixExpression(const PrefixExpression& p);
216
217 void writePostfixExpression(const PostfixExpression& p);
218
219 void writeBoolLiteral(const BoolLiteral& b);
220
221 void writeIntLiteral(const IntLiteral& i);
222
223 void writeFloatLiteral(const FloatLiteral& f);
224
225 void writeStatement(const Statement& s);
226
227 void writeBlock(const Block& b);
228
229 void writeBreakStatement(const BreakStatement& b);
230
231 void writeContinueStatement(const ContinueStatement& c);
232
233 void writeIfStatement(const IfStatement& stmt);
234
235 void writeForStatement(const ForStatement& f);
236
237 void writeWhileStatement(const WhileStatement& w);
238
239 void writeDoStatement(const DoStatement& d);
240
241 void writeSwitchStatement(const SwitchStatement& s);
242
243 void writeReturnStatement(const ReturnStatement& r);
244
245 // updates the current set of breaks to branch to the current location
246 void setBreakTargets();
247
248 // updates the current set of continues to branch to the current location
249 void setContinueTargets();
250
251 const Context& fContext;
252
253 ByteCode* fOutput;
254
255 const FunctionDefinition* fFunction;
256
257 std::vector<uint8_t>* fCode;
258
259 std::vector<const Variable*> fLocals;
260
261 std::stack<std::vector<DeferredLocation>> fContinueTargets;
262
263 std::stack<std::vector<DeferredLocation>> fBreakTargets;
264
Brian Osman226668a2019-05-14 16:47:30 -0400265 std::vector<DeferredCallTarget> fCallTargets;
266
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400267 int fParameterCount;
268
269 friend class DeferredLocation;
270 friend class ByteCodeVariableLValue;
Brian Osman1091f022019-05-16 09:42:16 -0400271 friend class ByteCodeSwizzleLValue;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400272
273 typedef CodeGenerator INHERITED;
274};
275
276}
277
278#endif