blob: 596c6394e38be2fdf1bf3f9db8f30ff3058e0fc5 [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
Ethan Nicholas9764ebd2019-05-01 14:43:54 -040087 void align(int divisor, int remainder);
88
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040089 void write8(uint8_t b);
90
91 void write16(uint16_t b);
92
93 void write32(uint32_t b);
94
95 void write(ByteCodeInstruction inst);
96
97 /**
98 * Based on 'type', writes the s (signed), u (unsigned), or f (float) instruction.
99 */
100 void writeTypedInstruction(const Type& type, ByteCodeInstruction s, ByteCodeInstruction u,
101 ByteCodeInstruction f);
102
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400103private:
104 // reserves 16 bits in the output code, to be filled in later with an address once we determine
105 // it
106 class DeferredLocation {
107 public:
108 DeferredLocation(ByteCodeGenerator* generator)
109 : fGenerator(*generator)
110 , fOffset(generator->fCode->size()) {
111 generator->write16(0);
112 }
113
114#ifdef SK_DEBUG
115 ~DeferredLocation() {
116 SkASSERT(fSet);
117 }
118#endif
119
120 void set() {
121 int target = fGenerator.fCode->size();
122 SkASSERT(target <= 65535);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400123 (*fGenerator.fCode)[fOffset] = target;
124 (*fGenerator.fCode)[fOffset + 1] = target >> 8;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400125#ifdef SK_DEBUG
126 fSet = true;
127#endif
128 }
129
130 private:
131 ByteCodeGenerator& fGenerator;
132 size_t fOffset;
133#ifdef SK_DEBUG
134 bool fSet = false;
135#endif
136 };
137
Brian Osman226668a2019-05-14 16:47:30 -0400138 class DeferredCallTarget {
139 public:
140 DeferredCallTarget(ByteCodeGenerator* generator, const FunctionDeclaration& function)
141 : fGenerator(*generator)
142 , fCode(generator->fCode)
143 , fOffset(generator->fCode->size())
144 , fFunction(function) {
145 generator->write8(0);
146 }
147
148 bool set() {
149 size_t idx;
150 const auto& functions(fGenerator.fOutput->fFunctions);
151 for (idx = 0; idx < functions.size(); ++idx) {
152 if (fFunction.matches(functions[idx]->fDeclaration)) {
153 break;
154 }
155 }
156 if (idx > 255 || idx > functions.size()) {
157 SkASSERT(false);
158 return false;
159 }
160 (*fCode)[fOffset] = idx;
161 return true;
162 }
163
164 private:
165 ByteCodeGenerator& fGenerator;
166 std::vector<uint8_t>* fCode;
167 size_t fOffset;
168 const FunctionDeclaration& fFunction;
169 };
170
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400171 /**
172 * Returns the local slot into which var should be stored, allocating a new slot if it has not
173 * already been assigned one. Compound variables (e.g. vectors) will consume more than one local
174 * slot, with the getLocation return value indicating where the first element should be stored.
175 */
176 int getLocation(const Variable& var);
177
178 std::unique_ptr<ByteCodeFunction> writeFunction(const FunctionDefinition& f);
179
180 void writeVarDeclarations(const VarDeclarations& decl);
181
182 void writeVariableReference(const VariableReference& ref);
183
184 void writeExpression(const Expression& expr);
185
186 /**
187 * Pushes whatever values are required by the lvalue onto the stack, and returns an LValue
188 * permitting loads and stores to it.
189 */
190 std::unique_ptr<LValue> getLValue(const Expression& expr);
191
192 void writeFunctionCall(const FunctionCall& c);
193
194 void writeConstructor(const Constructor& c);
195
Ethan Nicholas91164d12019-05-15 15:29:54 -0400196 void writeExternalValue(const ExternalValueReference& r);
197
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400198 void writeFieldAccess(const FieldAccess& f);
199
200 void writeSwizzle(const Swizzle& swizzle);
201
202 void writeBinaryExpression(const BinaryExpression& b);
203
204 void writeTernaryExpression(const TernaryExpression& t);
205
206 void writeIndexExpression(const IndexExpression& expr);
207
208 void writeLogicalAnd(const BinaryExpression& b);
209
210 void writeLogicalOr(const BinaryExpression& o);
211
212 void writeNullLiteral(const NullLiteral& n);
213
214 void writePrefixExpression(const PrefixExpression& p);
215
216 void writePostfixExpression(const PostfixExpression& p);
217
218 void writeBoolLiteral(const BoolLiteral& b);
219
220 void writeIntLiteral(const IntLiteral& i);
221
222 void writeFloatLiteral(const FloatLiteral& f);
223
224 void writeStatement(const Statement& s);
225
226 void writeBlock(const Block& b);
227
228 void writeBreakStatement(const BreakStatement& b);
229
230 void writeContinueStatement(const ContinueStatement& c);
231
232 void writeIfStatement(const IfStatement& stmt);
233
234 void writeForStatement(const ForStatement& f);
235
236 void writeWhileStatement(const WhileStatement& w);
237
238 void writeDoStatement(const DoStatement& d);
239
240 void writeSwitchStatement(const SwitchStatement& s);
241
242 void writeReturnStatement(const ReturnStatement& r);
243
244 // updates the current set of breaks to branch to the current location
245 void setBreakTargets();
246
247 // updates the current set of continues to branch to the current location
248 void setContinueTargets();
249
250 const Context& fContext;
251
252 ByteCode* fOutput;
253
254 const FunctionDefinition* fFunction;
255
256 std::vector<uint8_t>* fCode;
257
258 std::vector<const Variable*> fLocals;
259
260 std::stack<std::vector<DeferredLocation>> fContinueTargets;
261
262 std::stack<std::vector<DeferredLocation>> fBreakTargets;
263
Brian Osman226668a2019-05-14 16:47:30 -0400264 std::vector<DeferredCallTarget> fCallTargets;
265
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400266 int fParameterCount;
267
268 friend class DeferredLocation;
269 friend class ByteCodeVariableLValue;
Brian Osman1091f022019-05-16 09:42:16 -0400270 friend class ByteCodeSwizzleLValue;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400271
272 typedef CodeGenerator INHERITED;
273};
274
275}
276
277#endif