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