blob: 7983e7ea517f2b8e55caada002aa26d8ab5d771c [file] [log] [blame]
Mike Reed8520e762020-04-30 12:06:23 -04001/*
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04002 * 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLByteCodeGenerator.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04009
Brian Osmanb08cc022020-04-02 11:38:40 -040010#include <algorithm>
11
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040012namespace SkSL {
13
Brian Osmanb08cc022020-04-02 11:38:40 -040014static TypeCategory type_category(const Type& type) {
15 switch (type.kind()) {
16 case Type::Kind::kVector_Kind:
17 case Type::Kind::kMatrix_Kind:
18 return type_category(type.componentType());
19 default:
20 if (type.fName == "bool") {
21 return TypeCategory::kBool;
22 } else if (type.fName == "int" ||
23 type.fName == "short" ||
24 type.fName == "$intLiteral") {
25 return TypeCategory::kSigned;
26 } else if (type.fName == "uint" ||
27 type.fName == "ushort") {
28 return TypeCategory::kUnsigned;
29 } else {
30 SkASSERT(type.fName == "float" ||
31 type.fName == "half" ||
32 type.fName == "$floatLiteral");
33 return TypeCategory::kFloat;
34 }
35 ABORT("unsupported type: %s\n", type.displayName().c_str());
36 }
37}
38
39
40ByteCodeGenerator::ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
41 ByteCode* output)
Ethan Nicholas82162ee2019-05-21 16:05:08 -040042 : INHERITED(program, errors, nullptr)
Brian Osmanb08cc022020-04-02 11:38:40 -040043 , fContext(*context)
Ethan Nicholasae9633b2019-05-24 12:46:34 -040044 , fOutput(output)
Brian Osman3479a952020-05-04 10:22:53 -040045 // If you're adding new intrinsics here, ensure that they're declared in sksl_interp.inc, so
46 // they're available to "generic" interpreter programs (eg particles).
47 // You can probably copy the declarations from sksl_gpu.inc.
Ethan Nicholasae9633b2019-05-24 12:46:34 -040048 , fIntrinsics {
Brian Osmand5f937b2020-05-04 12:07:29 -040049 { "atan", ByteCodeInstruction::kATan },
50 { "clamp", SpecialIntrinsic::kClamp },
51 { "cos", ByteCodeInstruction::kCos },
52 { "dot", SpecialIntrinsic::kDot },
53 { "fract", ByteCodeInstruction::kFract },
54 { "inverse", ByteCodeInstruction::kInverse2x2 },
55 { "length", SpecialIntrinsic::kLength },
56 { "max", SpecialIntrinsic::kMax },
57 { "min", SpecialIntrinsic::kMin },
58 { "mix", SpecialIntrinsic::kMix },
59 { "pow", ByteCodeInstruction::kPow },
Brian Osmana43d8202020-06-17 16:50:39 -040060 { "sample", SpecialIntrinsic::kSample },
Brian Osmand5f937b2020-05-04 12:07:29 -040061 { "saturate", SpecialIntrinsic::kSaturate },
62 { "sin", ByteCodeInstruction::kSin },
63 { "sqrt", ByteCodeInstruction::kSqrt },
64 { "tan", ByteCodeInstruction::kTan },
Brian Osman8842b372020-05-01 15:07:49 -040065
66 { "lessThan", { ByteCodeInstruction::kCompareFLT,
67 ByteCodeInstruction::kCompareSLT,
68 ByteCodeInstruction::kCompareULT } },
69 { "lessThanEqual", { ByteCodeInstruction::kCompareFLTEQ,
70 ByteCodeInstruction::kCompareSLTEQ,
71 ByteCodeInstruction::kCompareULTEQ } },
72 { "greaterThan", { ByteCodeInstruction::kCompareFGT,
73 ByteCodeInstruction::kCompareSGT,
74 ByteCodeInstruction::kCompareUGT } },
75 { "greaterThanEqual", { ByteCodeInstruction::kCompareFGTEQ,
76 ByteCodeInstruction::kCompareSGTEQ,
77 ByteCodeInstruction::kCompareUGTEQ } },
78 { "equal", { ByteCodeInstruction::kCompareFEQ,
79 ByteCodeInstruction::kCompareIEQ,
80 ByteCodeInstruction::kCompareIEQ } },
81 { "notEqual", { ByteCodeInstruction::kCompareFNEQ,
82 ByteCodeInstruction::kCompareINEQ,
83 ByteCodeInstruction::kCompareINEQ } },
84
85 { "any", SpecialIntrinsic::kAny },
86 { "all", SpecialIntrinsic::kAll },
87 { "not", ByteCodeInstruction::kNotB },
88 } {}
Brian Osmanb08cc022020-04-02 11:38:40 -040089
Ethan Nicholas82162ee2019-05-21 16:05:08 -040090
Brian Osman07c117b2019-05-23 12:51:06 -070091int ByteCodeGenerator::SlotCount(const Type& type) {
Brian Osmanfba386b2019-06-20 14:54:15 -040092 if (type.kind() == Type::kOther_Kind) {
93 return 0;
94 } else if (type.kind() == Type::kStruct_Kind) {
Brian Osman07c117b2019-05-23 12:51:06 -070095 int slots = 0;
96 for (const auto& f : type.fields()) {
97 slots += SlotCount(*f.fType);
98 }
99 SkASSERT(slots <= 255);
100 return slots;
101 } else if (type.kind() == Type::kArray_Kind) {
102 int columns = type.columns();
103 SkASSERT(columns >= 0);
104 int slots = columns * SlotCount(type.componentType());
105 SkASSERT(slots <= 255);
106 return slots;
107 } else {
108 return type.columns() * type.rows();
109 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400110}
111
Brian Osman1c110a02019-10-01 14:53:32 -0400112static inline bool is_uniform(const SkSL::Variable& var) {
113 return var.fModifiers.fFlags & Modifiers::kUniform_Flag;
114}
115
Brian Osmaneadfeb92020-01-09 12:43:03 -0500116static inline bool is_in(const SkSL::Variable& var) {
117 return var.fModifiers.fFlags & Modifiers::kIn_Flag;
118}
Brian Osmanb08cc022020-04-02 11:38:40 -0400119
120void ByteCodeGenerator::gatherUniforms(const Type& type, const String& name) {
121 if (type.kind() == Type::kOther_Kind) {
122 return;
123 } else if (type.kind() == Type::kStruct_Kind) {
124 for (const auto& f : type.fields()) {
125 this->gatherUniforms(*f.fType, name + "." + f.fName);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500126 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400127 } else if (type.kind() == Type::kArray_Kind) {
128 for (int i = 0; i < type.columns(); ++i) {
129 this->gatherUniforms(type.componentType(), String::printf("%s[%d]", name.c_str(), i));
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500130 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400131 } else {
132 fOutput->fUniforms.push_back({ name, type_category(type), type.rows(), type.columns(),
133 fOutput->fUniformSlotCount });
134 fOutput->fUniformSlotCount += type.columns() * type.rows();
135 }
136}
137
138bool ByteCodeGenerator::generateCode() {
139 for (const auto& e : fProgram) {
140 switch (e.fKind) {
141 case ProgramElement::kFunction_Kind: {
142 std::unique_ptr<ByteCodeFunction> f = this->writeFunction((FunctionDefinition&) e);
143 if (!f) {
144 return false;
145 }
146 fOutput->fFunctions.push_back(std::move(f));
147 fFunctions.push_back(&(FunctionDefinition&)e);
148 break;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500149 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400150 case ProgramElement::kVar_Kind: {
151 VarDeclarations& decl = (VarDeclarations&) e;
152 for (const auto& v : decl.fVars) {
153 const Variable* declVar = ((VarDeclaration&) *v).fVar;
Brian Osmana43d8202020-06-17 16:50:39 -0400154 if (declVar->fType == *fContext.fFragmentProcessor_Type) {
155 fOutput->fChildFPCount++;
156 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400157 if (declVar->fModifiers.fLayout.fBuiltin >= 0 || is_in(*declVar)) {
158 continue;
159 }
160 if (is_uniform(*declVar)) {
161 this->gatherUniforms(declVar->fType, declVar->fName);
162 } else {
163 fOutput->fGlobalSlotCount += SlotCount(declVar->fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400164 }
165 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400166 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400167 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400168 default:
169 ; // ignore
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400170 }
171 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400172 return 0 == fErrors.errorCount();
173}
174
175std::unique_ptr<ByteCodeFunction> ByteCodeGenerator::writeFunction(const FunctionDefinition& f) {
176 fFunction = &f;
177 std::unique_ptr<ByteCodeFunction> result(new ByteCodeFunction(&f.fDeclaration));
178 fParameterCount = result->fParameterCount;
179 fLoopCount = fMaxLoopCount = 0;
180 fConditionCount = fMaxConditionCount = 0;
181 fStackCount = fMaxStackCount = 0;
182 fCode = &result->fCode;
183
184 this->writeStatement(*f.fBody);
185 if (0 == fErrors.errorCount()) {
186 SkASSERT(fLoopCount == 0);
187 SkASSERT(fConditionCount == 0);
188 SkASSERT(fStackCount == 0);
189 }
190 this->write(ByteCodeInstruction::kReturn, 0);
191 this->write8(0);
192
193 result->fLocalCount = fLocals.size();
194 result->fConditionCount = fMaxConditionCount;
195 result->fLoopCount = fMaxLoopCount;
196 result->fStackCount = fMaxStackCount;
197
198 const Type& returnType = f.fDeclaration.fReturnType;
199 if (returnType != *fContext.fVoid_Type) {
200 result->fReturnCount = SlotCount(returnType);
201 }
202 fLocals.clear();
203 fFunction = nullptr;
204 return result;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400205}
206
Brian Osman0785db02019-05-24 14:19:11 -0400207// A "simple" Swizzle is based on a variable (or a compound variable like a struct or array), and
208// that references consecutive values, such that it can be implemented using normal load/store ops
209// with an offset. Note that all single-component swizzles (of suitable base types) are simple.
210static bool swizzle_is_simple(const Swizzle& s) {
211 switch (s.fBase->fKind) {
212 case Expression::kFieldAccess_Kind:
213 case Expression::kIndex_Kind:
214 case Expression::kVariableReference_Kind:
215 break;
216 default:
217 return false;
218 }
219
220 for (size_t i = 1; i < s.fComponents.size(); ++i) {
221 if (s.fComponents[i] != s.fComponents[i - 1] + 1) {
222 return false;
223 }
224 }
225 return true;
226}
227
Brian Osmanb08cc022020-04-02 11:38:40 -0400228int ByteCodeGenerator::StackUsage(ByteCodeInstruction inst, int count_) {
229 // Ensures that we use count iff we're passed a non-default value. Most instructions have an
230 // implicit count, so the caller shouldn't need to worry about it (or count makes no sense).
231 // The asserts avoids callers thinking they're supplying useful information in that scenario,
232 // or failing to supply necessary information for the ops that need a count.
233 struct CountValue {
234 operator int() {
235 SkASSERT(val != ByteCodeGenerator::kUnusedStackCount);
236 SkDEBUGCODE(used = true);
237 return val;
238 }
239 ~CountValue() {
240 SkASSERT(used || val == ByteCodeGenerator::kUnusedStackCount);
241 }
242 int val;
243 SkDEBUGCODE(bool used = false;)
244 } count = { count_ };
245
246 switch (inst) {
247 // Unary functions/operators that don't change stack depth at all:
248#define VECTOR_UNARY_OP(base) \
249 case ByteCodeInstruction::base: \
250 case ByteCodeInstruction::base ## 2: \
251 case ByteCodeInstruction::base ## 3: \
252 case ByteCodeInstruction::base ## 4: \
253 return 0;
254
255 VECTOR_UNARY_OP(kConvertFtoI)
256 VECTOR_UNARY_OP(kConvertStoF)
257 VECTOR_UNARY_OP(kConvertUtoF)
258
Mike Reed8520e762020-04-30 12:06:23 -0400259 VECTOR_UNARY_OP(kATan)
Brian Osmanb08cc022020-04-02 11:38:40 -0400260 VECTOR_UNARY_OP(kCos)
Mike Reed8520e762020-04-30 12:06:23 -0400261 VECTOR_UNARY_OP(kFract)
Brian Osmanb08cc022020-04-02 11:38:40 -0400262 VECTOR_UNARY_OP(kSin)
263 VECTOR_UNARY_OP(kSqrt)
264 VECTOR_UNARY_OP(kTan)
265
266 VECTOR_UNARY_OP(kNegateF)
267 VECTOR_UNARY_OP(kNegateI)
Brian Osman8842b372020-05-01 15:07:49 -0400268 VECTOR_UNARY_OP(kNotB)
Brian Osmanb08cc022020-04-02 11:38:40 -0400269
270 case ByteCodeInstruction::kInverse2x2:
271 case ByteCodeInstruction::kInverse3x3:
272 case ByteCodeInstruction::kInverse4x4: return 0;
273
274 case ByteCodeInstruction::kClampIndex: return 0;
Brian Osmanb08cc022020-04-02 11:38:40 -0400275 case ByteCodeInstruction::kNegateFN: return 0;
276 case ByteCodeInstruction::kShiftLeft: return 0;
277 case ByteCodeInstruction::kShiftRightS: return 0;
278 case ByteCodeInstruction::kShiftRightU: return 0;
279
280#undef VECTOR_UNARY_OP
281
282 // Binary functions/operators that do a 2 -> 1 reduction (possibly N times)
283#define VECTOR_BINARY_OP(base) \
284 case ByteCodeInstruction::base: return -1; \
285 case ByteCodeInstruction::base ## 2: return -2; \
286 case ByteCodeInstruction::base ## 3: return -3; \
287 case ByteCodeInstruction::base ## 4: return -4;
288
289#define VECTOR_MATRIX_BINARY_OP(base) \
290 VECTOR_BINARY_OP(base) \
291 case ByteCodeInstruction::base ## N: return -count;
292
293 case ByteCodeInstruction::kAndB: return -1;
294 case ByteCodeInstruction::kOrB: return -1;
295 case ByteCodeInstruction::kXorB: return -1;
296
297 VECTOR_BINARY_OP(kAddI)
298 VECTOR_MATRIX_BINARY_OP(kAddF)
299
300 VECTOR_BINARY_OP(kCompareIEQ)
301 VECTOR_MATRIX_BINARY_OP(kCompareFEQ)
302 VECTOR_BINARY_OP(kCompareINEQ)
303 VECTOR_MATRIX_BINARY_OP(kCompareFNEQ)
304 VECTOR_BINARY_OP(kCompareSGT)
305 VECTOR_BINARY_OP(kCompareUGT)
306 VECTOR_BINARY_OP(kCompareFGT)
307 VECTOR_BINARY_OP(kCompareSGTEQ)
308 VECTOR_BINARY_OP(kCompareUGTEQ)
309 VECTOR_BINARY_OP(kCompareFGTEQ)
310 VECTOR_BINARY_OP(kCompareSLT)
311 VECTOR_BINARY_OP(kCompareULT)
312 VECTOR_BINARY_OP(kCompareFLT)
313 VECTOR_BINARY_OP(kCompareSLTEQ)
314 VECTOR_BINARY_OP(kCompareULTEQ)
315 VECTOR_BINARY_OP(kCompareFLTEQ)
316
317 VECTOR_BINARY_OP(kDivideS)
318 VECTOR_BINARY_OP(kDivideU)
319 VECTOR_MATRIX_BINARY_OP(kDivideF)
Brian Osmand5f937b2020-05-04 12:07:29 -0400320 VECTOR_BINARY_OP(kMaxF)
321 VECTOR_BINARY_OP(kMaxS)
322 VECTOR_BINARY_OP(kMinF)
323 VECTOR_BINARY_OP(kMinS)
Brian Osmanb08cc022020-04-02 11:38:40 -0400324 VECTOR_BINARY_OP(kMultiplyI)
325 VECTOR_MATRIX_BINARY_OP(kMultiplyF)
Florin Malita3facc9c2020-05-04 09:26:15 -0400326 VECTOR_BINARY_OP(kPow)
Brian Osmanb08cc022020-04-02 11:38:40 -0400327 VECTOR_BINARY_OP(kRemainderF)
328 VECTOR_BINARY_OP(kRemainderS)
329 VECTOR_BINARY_OP(kRemainderU)
330 VECTOR_BINARY_OP(kSubtractI)
331 VECTOR_MATRIX_BINARY_OP(kSubtractF)
332
333#undef VECTOR_BINARY_OP
334#undef VECTOR_MATRIX_BINARY_OP
335
336 // Ops that push or load data to grow the stack:
337 case ByteCodeInstruction::kDup:
338 case ByteCodeInstruction::kLoad:
339 case ByteCodeInstruction::kLoadGlobal:
340 case ByteCodeInstruction::kLoadUniform:
341 case ByteCodeInstruction::kReadExternal:
342 case ByteCodeInstruction::kPushImmediate:
343 return 1;
344
345 case ByteCodeInstruction::kDup2:
346 case ByteCodeInstruction::kLoad2:
347 case ByteCodeInstruction::kLoadGlobal2:
348 case ByteCodeInstruction::kLoadUniform2:
349 case ByteCodeInstruction::kReadExternal2:
350 return 2;
351
352 case ByteCodeInstruction::kDup3:
353 case ByteCodeInstruction::kLoad3:
354 case ByteCodeInstruction::kLoadGlobal3:
355 case ByteCodeInstruction::kLoadUniform3:
356 case ByteCodeInstruction::kReadExternal3:
357 return 3;
358
359 case ByteCodeInstruction::kDup4:
360 case ByteCodeInstruction::kLoad4:
361 case ByteCodeInstruction::kLoadGlobal4:
362 case ByteCodeInstruction::kLoadUniform4:
363 case ByteCodeInstruction::kReadExternal4:
364 return 4;
365
366 case ByteCodeInstruction::kDupN:
Brian Osmanb08cc022020-04-02 11:38:40 -0400367 return count;
368
369 // Pushes 'count' values, minus one for the 'address' that's consumed first
370 case ByteCodeInstruction::kLoadExtended:
371 case ByteCodeInstruction::kLoadExtendedGlobal:
372 case ByteCodeInstruction::kLoadExtendedUniform:
373 return count - 1;
374
375 // Ops that pop or store data to shrink the stack:
376 case ByteCodeInstruction::kPop:
377 case ByteCodeInstruction::kStore:
378 case ByteCodeInstruction::kStoreGlobal:
379 case ByteCodeInstruction::kWriteExternal:
380 return -1;
381
382 case ByteCodeInstruction::kPop2:
383 case ByteCodeInstruction::kStore2:
384 case ByteCodeInstruction::kStoreGlobal2:
385 case ByteCodeInstruction::kWriteExternal2:
386 return -2;
387
388 case ByteCodeInstruction::kPop3:
389 case ByteCodeInstruction::kStore3:
390 case ByteCodeInstruction::kStoreGlobal3:
391 case ByteCodeInstruction::kWriteExternal3:
392 return -3;
393
394 case ByteCodeInstruction::kPop4:
395 case ByteCodeInstruction::kStore4:
396 case ByteCodeInstruction::kStoreGlobal4:
397 case ByteCodeInstruction::kWriteExternal4:
398 return -4;
399
400 case ByteCodeInstruction::kPopN:
401 case ByteCodeInstruction::kStoreSwizzle:
402 case ByteCodeInstruction::kStoreSwizzleGlobal:
403 return -count;
404
405 // Consumes 'count' values, plus one for the 'address'
406 case ByteCodeInstruction::kStoreExtended:
407 case ByteCodeInstruction::kStoreExtendedGlobal:
408 case ByteCodeInstruction::kStoreSwizzleIndirect:
409 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal:
410 return -count - 1;
411
412 // Strange ops where the caller computes the delta for us:
413 case ByteCodeInstruction::kCallExternal:
414 case ByteCodeInstruction::kMatrixToMatrix:
415 case ByteCodeInstruction::kMatrixMultiply:
416 case ByteCodeInstruction::kReserve:
417 case ByteCodeInstruction::kReturn:
418 case ByteCodeInstruction::kScalarToMatrix:
419 case ByteCodeInstruction::kSwizzle:
420 return count;
421
422 // Miscellaneous
423
Brian Osmana43d8202020-06-17 16:50:39 -0400424 // (X, Y) -> (R, G, B, A)
425 case ByteCodeInstruction::kSampleExplicit: return 4 - 2;
426 // (float3x3) -> (R, G, B, A)
427 case ByteCodeInstruction::kSampleMatrix: return 4 - 9;
428
Brian Osman8842b372020-05-01 15:07:49 -0400429 // kMix does a 3 -> 1 reduction (A, B, M -> A -or- B) for each component
430 case ByteCodeInstruction::kMix: return -2;
431 case ByteCodeInstruction::kMix2: return -4;
432 case ByteCodeInstruction::kMix3: return -6;
433 case ByteCodeInstruction::kMix4: return -8;
434
435 // kLerp works the same way (producing lerp(A, B, T) for each component)
436 case ByteCodeInstruction::kLerp: return -2;
437 case ByteCodeInstruction::kLerp2: return -4;
438 case ByteCodeInstruction::kLerp3: return -6;
439 case ByteCodeInstruction::kLerp4: return -8;
440
Brian Osmanb08cc022020-04-02 11:38:40 -0400441 // kCall is net-zero. Max stack depth is adjusted in writeFunctionCall.
442 case ByteCodeInstruction::kCall: return 0;
443 case ByteCodeInstruction::kBranch: return 0;
444 case ByteCodeInstruction::kBranchIfAllFalse: return 0;
445
446 case ByteCodeInstruction::kMaskPush: return -1;
447 case ByteCodeInstruction::kMaskPop: return 0;
448 case ByteCodeInstruction::kMaskNegate: return 0;
449 case ByteCodeInstruction::kMaskBlend: return -count;
450
451 case ByteCodeInstruction::kLoopBegin: return 0;
452 case ByteCodeInstruction::kLoopNext: return 0;
453 case ByteCodeInstruction::kLoopMask: return -1;
454 case ByteCodeInstruction::kLoopEnd: return 0;
455 case ByteCodeInstruction::kLoopBreak: return 0;
456 case ByteCodeInstruction::kLoopContinue: return 0;
Brian Osmanb08cc022020-04-02 11:38:40 -0400457 }
Brian Osmand5f937b2020-05-04 12:07:29 -0400458
459 SkUNREACHABLE;
Brian Osmanb08cc022020-04-02 11:38:40 -0400460}
461
462ByteCodeGenerator::Location ByteCodeGenerator::getLocation(const Variable& var) {
463 // given that we seldom have more than a couple of variables, linear search is probably the most
464 // efficient way to handle lookups
465 switch (var.fStorage) {
466 case Variable::kLocal_Storage: {
467 for (int i = fLocals.size() - 1; i >= 0; --i) {
468 if (fLocals[i] == &var) {
469 SkASSERT(fParameterCount + i <= 255);
470 return { fParameterCount + i, Storage::kLocal };
471 }
472 }
473 int result = fParameterCount + fLocals.size();
474 fLocals.push_back(&var);
475 for (int i = 0; i < SlotCount(var.fType) - 1; ++i) {
476 fLocals.push_back(nullptr);
477 }
478 SkASSERT(result <= 255);
479 return { result, Storage::kLocal };
480 }
481 case Variable::kParameter_Storage: {
482 int offset = 0;
483 for (const auto& p : fFunction->fDeclaration.fParameters) {
484 if (p == &var) {
485 SkASSERT(offset <= 255);
486 return { offset, Storage::kLocal };
487 }
488 offset += SlotCount(p->fType);
489 }
490 SkASSERT(false);
491 return Location::MakeInvalid();
492 }
493 case Variable::kGlobal_Storage: {
Brian Osmana43d8202020-06-17 16:50:39 -0400494 if (var.fType == *fContext.fFragmentProcessor_Type) {
495 int offset = 0;
496 for (const auto& e : fProgram) {
497 if (e.fKind == ProgramElement::kVar_Kind) {
498 VarDeclarations& decl = (VarDeclarations&) e;
499 for (const auto& v : decl.fVars) {
500 const Variable* declVar = ((VarDeclaration&) *v).fVar;
501 if (declVar->fType != *fContext.fFragmentProcessor_Type) {
502 continue;
503 }
504 if (declVar == &var) {
505 SkASSERT(offset <= 255);
506 return { offset, Storage::kChildFP };
507 }
508 offset++;
509 }
510 }
511 }
512 SkASSERT(false);
513 return Location::MakeInvalid();
514 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400515 if (is_in(var)) {
516 // If you see this error, it means the program is using raw 'in' variables. You
517 // should either specialize the program (Compiler::specialize) to bake in the final
518 // values of the 'in' variables, or not use 'in' variables (maybe you meant to use
519 // 'uniform' instead?).
520 fErrors.error(var.fOffset,
521 "'in' variable is not specialized or has unsupported type");
522 return Location::MakeInvalid();
523 }
524 int offset = 0;
525 bool isUniform = is_uniform(var);
526 for (const auto& e : fProgram) {
527 if (e.fKind == ProgramElement::kVar_Kind) {
528 VarDeclarations& decl = (VarDeclarations&) e;
529 for (const auto& v : decl.fVars) {
530 const Variable* declVar = ((VarDeclaration&) *v).fVar;
531 if (declVar->fModifiers.fLayout.fBuiltin >= 0 || is_in(*declVar)) {
532 continue;
533 }
534 if (isUniform != is_uniform(*declVar)) {
535 continue;
536 }
537 if (declVar == &var) {
538 SkASSERT(offset <= 255);
539 return { offset, isUniform ? Storage::kUniform : Storage::kGlobal };
540 }
541 offset += SlotCount(declVar->fType);
542 }
543 }
544 }
545 SkASSERT(false);
546 return Location::MakeInvalid();
547 }
548 default:
549 SkASSERT(false);
550 return Location::MakeInvalid();
551 }
552}
553
Brian Osman1c110a02019-10-01 14:53:32 -0400554ByteCodeGenerator::Location ByteCodeGenerator::getLocation(const Expression& expr) {
Brian Osman07c117b2019-05-23 12:51:06 -0700555 switch (expr.fKind) {
556 case Expression::kFieldAccess_Kind: {
Brian Osmanb08cc022020-04-02 11:38:40 -0400557 const FieldAccess& f = (const FieldAccess&)expr;
558 Location baseLoc = this->getLocation(*f.fBase);
Brian Osman07c117b2019-05-23 12:51:06 -0700559 int offset = 0;
560 for (int i = 0; i < f.fFieldIndex; ++i) {
561 offset += SlotCount(*f.fBase->fType.fields()[i].fType);
562 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400563 if (baseLoc.isOnStack()) {
564 if (offset != 0) {
565 this->write(ByteCodeInstruction::kPushImmediate);
566 this->write32(offset);
567 this->write(ByteCodeInstruction::kAddI);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500568 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400569 return baseLoc;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500570 } else {
Brian Osmanb08cc022020-04-02 11:38:40 -0400571 return baseLoc + offset;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500572 }
Ben Wagner470e0ac2020-01-22 16:59:21 -0500573 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400574 case Expression::kIndex_Kind: {
575 const IndexExpression& i = (const IndexExpression&)expr;
576 int stride = SlotCount(i.fType);
577 int length = i.fBase->fType.columns();
578 SkASSERT(length <= 255);
579 int offset = -1;
580 if (i.fIndex->isConstant()) {
581 int64_t index = i.fIndex->getConstantInt();
582 if (index < 0 || index >= length) {
583 fErrors.error(i.fIndex->fOffset, "Array index out of bounds.");
584 return Location::MakeInvalid();
585 }
586 offset = index * stride;
587 } else {
588 if (i.fIndex->hasSideEffects()) {
589 // Having a side-effect in an indexer is technically safe for an rvalue,
590 // but with lvalues we have to evaluate the indexer twice, so make it an error.
591 fErrors.error(i.fIndex->fOffset,
592 "Index expressions with side-effects not supported in byte code.");
593 return Location::MakeInvalid();
594 }
595 this->writeExpression(*i.fIndex);
596 this->write(ByteCodeInstruction::kClampIndex);
597 this->write8(length);
598 if (stride != 1) {
599 this->write(ByteCodeInstruction::kPushImmediate);
600 this->write32(stride);
601 this->write(ByteCodeInstruction::kMultiplyI);
Brian Osmanb08cc022020-04-02 11:38:40 -0400602 }
603 }
604 Location baseLoc = this->getLocation(*i.fBase);
605
606 // Are both components known statically?
607 if (!baseLoc.isOnStack() && offset >= 0) {
608 return baseLoc + offset;
609 }
610
611 // At least one component is dynamic (and on the stack).
612
613 // If the other component is zero, we're done
614 if (baseLoc.fSlot == 0 || offset == 0) {
615 return baseLoc.makeOnStack();
616 }
617
618 // Push the non-dynamic component (if any) to the stack, then add the two
619 if (!baseLoc.isOnStack()) {
620 this->write(ByteCodeInstruction::kPushImmediate);
621 this->write32(baseLoc.fSlot);
622 }
623 if (offset >= 0) {
624 this->write(ByteCodeInstruction::kPushImmediate);
625 this->write32(offset);
626 }
627 this->write(ByteCodeInstruction::kAddI);
Brian Osmanb08cc022020-04-02 11:38:40 -0400628 return baseLoc.makeOnStack();
629 }
Brian Osman0785db02019-05-24 14:19:11 -0400630 case Expression::kSwizzle_Kind: {
Brian Osmanb08cc022020-04-02 11:38:40 -0400631 const Swizzle& s = (const Swizzle&)expr;
Brian Osman0785db02019-05-24 14:19:11 -0400632 SkASSERT(swizzle_is_simple(s));
Brian Osmanb08cc022020-04-02 11:38:40 -0400633 Location baseLoc = this->getLocation(*s.fBase);
634 int offset = s.fComponents[0];
635 if (baseLoc.isOnStack()) {
636 if (offset != 0) {
637 this->write(ByteCodeInstruction::kPushImmediate);
638 this->write32(offset);
639 this->write(ByteCodeInstruction::kAddI);
Brian Osmanb08cc022020-04-02 11:38:40 -0400640 }
641 return baseLoc;
642 } else {
643 return baseLoc + offset;
644 }
Brian Osman0785db02019-05-24 14:19:11 -0400645 }
Brian Osman07c117b2019-05-23 12:51:06 -0700646 case Expression::kVariableReference_Kind: {
Brian Osmanb08cc022020-04-02 11:38:40 -0400647 const Variable& var = ((const VariableReference&)expr).fVariable;
Brian Osman07c117b2019-05-23 12:51:06 -0700648 return this->getLocation(var);
649 }
650 default:
651 SkASSERT(false);
Brian Osmanb08cc022020-04-02 11:38:40 -0400652 return Location::MakeInvalid();
Brian Osman07c117b2019-05-23 12:51:06 -0700653 }
654}
655
Brian Osmanb08cc022020-04-02 11:38:40 -0400656void ByteCodeGenerator::write8(uint8_t b) {
657 fCode->push_back(b);
Ethan Nicholas2cde3a12020-01-21 09:23:13 -0500658}
659
Brian Osmanb08cc022020-04-02 11:38:40 -0400660void ByteCodeGenerator::write16(uint16_t i) {
661 size_t n = fCode->size();
662 fCode->resize(n+2);
663 memcpy(fCode->data() + n, &i, 2);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500664}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500665
Brian Osmanb08cc022020-04-02 11:38:40 -0400666void ByteCodeGenerator::write32(uint32_t i) {
667 size_t n = fCode->size();
668 fCode->resize(n+4);
669 memcpy(fCode->data() + n, &i, 4);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500670}
671
Brian Osmanb08cc022020-04-02 11:38:40 -0400672void ByteCodeGenerator::write(ByteCodeInstruction i, int count) {
673 switch (i) {
674 case ByteCodeInstruction::kLoopBegin: this->enterLoop(); break;
675 case ByteCodeInstruction::kLoopEnd: this->exitLoop(); break;
Ethan Nicholas2329da02020-01-24 15:49:33 -0500676
Brian Osmanb08cc022020-04-02 11:38:40 -0400677 case ByteCodeInstruction::kMaskPush: this->enterCondition(); break;
678 case ByteCodeInstruction::kMaskPop:
679 case ByteCodeInstruction::kMaskBlend: this->exitCondition(); break;
680 default: /* Do nothing */ break;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500681 }
Brian Osmanab8f3842020-04-07 09:30:44 -0400682 this->write16((uint16_t)i);
Brian Osmanb08cc022020-04-02 11:38:40 -0400683 fStackCount += StackUsage(i, count);
684 fMaxStackCount = std::max(fMaxStackCount, fStackCount);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500685}
686
Brian Osmanb08cc022020-04-02 11:38:40 -0400687static ByteCodeInstruction vector_instruction(ByteCodeInstruction base, int count) {
688 SkASSERT(count >= 1 && count <= 4);
689 return ((ByteCodeInstruction) ((int) base + 1 - count));
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500690}
691
Brian Osmanb08cc022020-04-02 11:38:40 -0400692void ByteCodeGenerator::writeTypedInstruction(const Type& type, ByteCodeInstruction s,
693 ByteCodeInstruction u, ByteCodeInstruction f,
Brian Osmanab8f3842020-04-07 09:30:44 -0400694 int count) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500695 switch (type_category(type)) {
Brian Osman8842b372020-05-01 15:07:49 -0400696 case TypeCategory::kBool:
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500697 case TypeCategory::kSigned:
Brian Osmanb08cc022020-04-02 11:38:40 -0400698 this->write(vector_instruction(s, count));
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500699 break;
700 case TypeCategory::kUnsigned:
Brian Osmanb08cc022020-04-02 11:38:40 -0400701 this->write(vector_instruction(u, count));
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500702 break;
703 case TypeCategory::kFloat: {
Brian Osmanb08cc022020-04-02 11:38:40 -0400704 if (count > 4) {
705 this->write((ByteCodeInstruction)((int)f + 1), count);
Brian Osmanab8f3842020-04-07 09:30:44 -0400706 this->write8(count);
Brian Osmanb08cc022020-04-02 11:38:40 -0400707 } else {
708 this->write(vector_instruction(f, count));
709 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500710 break;
711 }
712 default:
713 SkASSERT(false);
714 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500715}
716
Brian Osmanb08cc022020-04-02 11:38:40 -0400717bool ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b, bool discard) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400718 if (b.fOperator == Token::Kind::TK_EQ) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500719 std::unique_ptr<LValue> lvalue = this->getLValue(*b.fLeft);
Brian Osmanb08cc022020-04-02 11:38:40 -0400720 this->writeExpression(*b.fRight);
721 lvalue->store(discard);
722 discard = false;
723 return discard;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500724 }
725 const Type& lType = b.fLeft->fType;
726 const Type& rType = b.fRight->fType;
727 bool lVecOrMtx = (lType.kind() == Type::kVector_Kind || lType.kind() == Type::kMatrix_Kind);
728 bool rVecOrMtx = (rType.kind() == Type::kVector_Kind || rType.kind() == Type::kMatrix_Kind);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500729 Token::Kind op;
730 std::unique_ptr<LValue> lvalue;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500731 if (is_assignment(b.fOperator)) {
732 lvalue = this->getLValue(*b.fLeft);
Brian Osmanb08cc022020-04-02 11:38:40 -0400733 lvalue->load();
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500734 op = remove_assignment(b.fOperator);
735 } else {
Brian Osmanb08cc022020-04-02 11:38:40 -0400736 this->writeExpression(*b.fLeft);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500737 op = b.fOperator;
738 if (!lVecOrMtx && rVecOrMtx) {
Brian Osmanb08cc022020-04-02 11:38:40 -0400739 for (int i = SlotCount(rType); i > 1; --i) {
740 this->write(ByteCodeInstruction::kDup);
Brian Osmanb08cc022020-04-02 11:38:40 -0400741 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500742 }
743 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500744 int count = std::max(SlotCount(lType), SlotCount(rType));
Brian Osmanb08cc022020-04-02 11:38:40 -0400745 SkDEBUGCODE(TypeCategory tc = type_category(lType));
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500746 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400747 case Token::Kind::TK_LOGICALAND: {
Brian Osmanb08cc022020-04-02 11:38:40 -0400748 SkASSERT(tc == SkSL::TypeCategory::kBool && count == 1);
749 this->write(ByteCodeInstruction::kDup);
Brian Osmanb08cc022020-04-02 11:38:40 -0400750 this->write(ByteCodeInstruction::kMaskPush);
751 this->write(ByteCodeInstruction::kBranchIfAllFalse);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500752 DeferredLocation falseLocation(this);
Brian Osmanb08cc022020-04-02 11:38:40 -0400753 this->writeExpression(*b.fRight);
754 this->write(ByteCodeInstruction::kAndB);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500755 falseLocation.set();
Brian Osmanb08cc022020-04-02 11:38:40 -0400756 this->write(ByteCodeInstruction::kMaskPop);
757 return false;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500758 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400759 case Token::Kind::TK_LOGICALOR: {
Brian Osmanb08cc022020-04-02 11:38:40 -0400760 SkASSERT(tc == SkSL::TypeCategory::kBool && count == 1);
761 this->write(ByteCodeInstruction::kDup);
Brian Osmanb08cc022020-04-02 11:38:40 -0400762 this->write(ByteCodeInstruction::kNotB);
763 this->write(ByteCodeInstruction::kMaskPush);
764 this->write(ByteCodeInstruction::kBranchIfAllFalse);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500765 DeferredLocation falseLocation(this);
Brian Osmanb08cc022020-04-02 11:38:40 -0400766 this->writeExpression(*b.fRight);
767 this->write(ByteCodeInstruction::kOrB);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500768 falseLocation.set();
Brian Osmanb08cc022020-04-02 11:38:40 -0400769 this->write(ByteCodeInstruction::kMaskPop);
770 return false;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500771 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400772 case Token::Kind::TK_SHL:
773 case Token::Kind::TK_SHR: {
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500774 SkASSERT(count == 1 && (tc == SkSL::TypeCategory::kSigned ||
775 tc == SkSL::TypeCategory::kUnsigned));
776 if (!b.fRight->isConstant()) {
777 fErrors.error(b.fRight->fOffset, "Shift amounts must be constant");
Brian Osmanb08cc022020-04-02 11:38:40 -0400778 return false;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500779 }
780 int64_t shift = b.fRight->getConstantInt();
781 if (shift < 0 || shift > 31) {
782 fErrors.error(b.fRight->fOffset, "Shift amount out of range");
Brian Osmanb08cc022020-04-02 11:38:40 -0400783 return false;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500784 }
785
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400786 if (op == Token::Kind::TK_SHL) {
Brian Osmanb08cc022020-04-02 11:38:40 -0400787 this->write(ByteCodeInstruction::kShiftLeft);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500788 } else {
789 this->write(type_category(lType) == TypeCategory::kSigned
Brian Osmanb08cc022020-04-02 11:38:40 -0400790 ? ByteCodeInstruction::kShiftRightS
791 : ByteCodeInstruction::kShiftRightU);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500792 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400793 this->write8(shift);
794 return false;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500795 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500796
797 default:
798 break;
799 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400800 this->writeExpression(*b.fRight);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500801 if (lVecOrMtx && !rVecOrMtx) {
Brian Osmanb08cc022020-04-02 11:38:40 -0400802 for (int i = SlotCount(lType); i > 1; --i) {
803 this->write(ByteCodeInstruction::kDup);
Brian Osmanb08cc022020-04-02 11:38:40 -0400804 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500805 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400806 // Special case for M*V, V*M, M*M (but not V*V!)
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400807 if (op == Token::Kind::TK_STAR && lVecOrMtx && rVecOrMtx &&
Brian Osmanb08cc022020-04-02 11:38:40 -0400808 !(lType.kind() == Type::kVector_Kind && rType.kind() == Type::kVector_Kind)) {
809 this->write(ByteCodeInstruction::kMatrixMultiply,
810 SlotCount(b.fType) - (SlotCount(lType) + SlotCount(rType)));
811 int rCols = rType.columns(),
812 rRows = rType.rows(),
813 lCols = lType.columns(),
814 lRows = lType.rows();
815 // M*V treats the vector as a column
816 if (rType.kind() == Type::kVector_Kind) {
817 std::swap(rCols, rRows);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500818 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400819 SkASSERT(lCols == rRows);
820 SkASSERT(SlotCount(b.fType) == lRows * rCols);
821 this->write8(lCols);
822 this->write8(lRows);
823 this->write8(rCols);
824 } else {
825 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400826 case Token::Kind::TK_EQEQ:
Brian Osmanb08cc022020-04-02 11:38:40 -0400827 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareIEQ,
828 ByteCodeInstruction::kCompareIEQ,
829 ByteCodeInstruction::kCompareFEQ,
830 count);
831 // Collapse to a single bool
832 for (int i = count; i > 1; --i) {
833 this->write(ByteCodeInstruction::kAndB);
834 }
835 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400836 case Token::Kind::TK_GT:
Brian Osmanb08cc022020-04-02 11:38:40 -0400837 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSGT,
838 ByteCodeInstruction::kCompareUGT,
839 ByteCodeInstruction::kCompareFGT,
840 count);
841 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400842 case Token::Kind::TK_GTEQ:
Brian Osmanb08cc022020-04-02 11:38:40 -0400843 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSGTEQ,
844 ByteCodeInstruction::kCompareUGTEQ,
845 ByteCodeInstruction::kCompareFGTEQ,
846 count);
847 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400848 case Token::Kind::TK_LT:
Brian Osmanb08cc022020-04-02 11:38:40 -0400849 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSLT,
850 ByteCodeInstruction::kCompareULT,
851 ByteCodeInstruction::kCompareFLT,
852 count);
853 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400854 case Token::Kind::TK_LTEQ:
Brian Osmanb08cc022020-04-02 11:38:40 -0400855 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSLTEQ,
856 ByteCodeInstruction::kCompareULTEQ,
857 ByteCodeInstruction::kCompareFLTEQ,
858 count);
859 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400860 case Token::Kind::TK_MINUS:
Brian Osmanb08cc022020-04-02 11:38:40 -0400861 this->writeTypedInstruction(lType, ByteCodeInstruction::kSubtractI,
862 ByteCodeInstruction::kSubtractI,
863 ByteCodeInstruction::kSubtractF,
864 count);
865 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400866 case Token::Kind::TK_NEQ:
Brian Osmanb08cc022020-04-02 11:38:40 -0400867 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareINEQ,
868 ByteCodeInstruction::kCompareINEQ,
869 ByteCodeInstruction::kCompareFNEQ,
870 count);
871 // Collapse to a single bool
872 for (int i = count; i > 1; --i) {
873 this->write(ByteCodeInstruction::kOrB);
874 }
875 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400876 case Token::Kind::TK_PERCENT:
Brian Osmanb08cc022020-04-02 11:38:40 -0400877 this->writeTypedInstruction(lType, ByteCodeInstruction::kRemainderS,
878 ByteCodeInstruction::kRemainderU,
879 ByteCodeInstruction::kRemainderF,
880 count);
881 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400882 case Token::Kind::TK_PLUS:
Brian Osmanb08cc022020-04-02 11:38:40 -0400883 this->writeTypedInstruction(lType, ByteCodeInstruction::kAddI,
884 ByteCodeInstruction::kAddI,
885 ByteCodeInstruction::kAddF,
886 count);
887 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400888 case Token::Kind::TK_SLASH:
Brian Osmanb08cc022020-04-02 11:38:40 -0400889 this->writeTypedInstruction(lType, ByteCodeInstruction::kDivideS,
890 ByteCodeInstruction::kDivideU,
891 ByteCodeInstruction::kDivideF,
892 count);
893 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400894 case Token::Kind::TK_STAR:
Brian Osmanb08cc022020-04-02 11:38:40 -0400895 this->writeTypedInstruction(lType, ByteCodeInstruction::kMultiplyI,
896 ByteCodeInstruction::kMultiplyI,
897 ByteCodeInstruction::kMultiplyF,
898 count);
899 break;
900
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400901 case Token::Kind::TK_LOGICALXOR:
Brian Osmanb08cc022020-04-02 11:38:40 -0400902 SkASSERT(tc == SkSL::TypeCategory::kBool && count == 1);
903 this->write(ByteCodeInstruction::kXorB);
904 break;
905
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400906 case Token::Kind::TK_BITWISEAND:
Brian Osmanb08cc022020-04-02 11:38:40 -0400907 SkASSERT(count == 1 && (tc == SkSL::TypeCategory::kSigned ||
908 tc == SkSL::TypeCategory::kUnsigned));
909 this->write(ByteCodeInstruction::kAndB);
910 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400911 case Token::Kind::TK_BITWISEOR:
Brian Osmanb08cc022020-04-02 11:38:40 -0400912 SkASSERT(count == 1 && (tc == SkSL::TypeCategory::kSigned ||
913 tc == SkSL::TypeCategory::kUnsigned));
914 this->write(ByteCodeInstruction::kOrB);
915 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400916 case Token::Kind::TK_BITWISEXOR:
Brian Osmanb08cc022020-04-02 11:38:40 -0400917 SkASSERT(count == 1 && (tc == SkSL::TypeCategory::kSigned ||
918 tc == SkSL::TypeCategory::kUnsigned));
919 this->write(ByteCodeInstruction::kXorB);
920 break;
921
922 default:
923 fErrors.error(b.fOffset, SkSL::String::printf("Unsupported binary operator '%s'",
924 Compiler::OperatorName(op)));
925 break;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500926 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500927 }
928 if (lvalue) {
Brian Osmanb08cc022020-04-02 11:38:40 -0400929 lvalue->store(discard);
930 discard = false;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500931 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400932 return discard;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500933}
934
Brian Osmanb08cc022020-04-02 11:38:40 -0400935void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
936 this->write(ByteCodeInstruction::kPushImmediate);
937 this->write32(b.fValue ? ~0 : 0);
938}
939
940void ByteCodeGenerator::writeConstructor(const Constructor& c) {
941 for (const auto& arg : c.fArguments) {
942 this->writeExpression(*arg);
943 }
944 if (c.fArguments.size() == 1) {
945 const Type& inType = c.fArguments[0]->fType;
946 const Type& outType = c.fType;
947 TypeCategory inCategory = type_category(inType);
948 TypeCategory outCategory = type_category(outType);
949 int inCount = SlotCount(inType);
950 int outCount = SlotCount(outType);
951 if (inCategory != outCategory) {
952 SkASSERT(inCount == outCount);
953 if (inCategory == TypeCategory::kFloat) {
954 SkASSERT(outCategory == TypeCategory::kSigned ||
955 outCategory == TypeCategory::kUnsigned);
956 this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount));
957 } else if (outCategory == TypeCategory::kFloat) {
958 if (inCategory == TypeCategory::kSigned) {
959 this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount));
960 } else {
961 SkASSERT(inCategory == TypeCategory::kUnsigned);
962 this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount));
963 }
964 } else {
965 SkASSERT(false);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500966 }
967 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400968 if (inType.kind() == Type::kMatrix_Kind && outType.kind() == Type::kMatrix_Kind) {
969 this->write(ByteCodeInstruction::kMatrixToMatrix,
970 SlotCount(outType) - SlotCount(inType));
971 this->write8(inType.columns());
972 this->write8(inType.rows());
973 this->write8(outType.columns());
974 this->write8(outType.rows());
975 } else if (inCount != outCount) {
976 SkASSERT(inCount == 1);
977 if (outType.kind() == Type::kMatrix_Kind) {
978 this->write(ByteCodeInstruction::kScalarToMatrix, SlotCount(outType) - 1);
979 this->write8(outType.columns());
980 this->write8(outType.rows());
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500981 } else {
Brian Osmanb08cc022020-04-02 11:38:40 -0400982 SkASSERT(outType.kind() == Type::kVector_Kind);
983 for (; inCount != outCount; ++inCount) {
984 this->write(ByteCodeInstruction::kDup);
Brian Osmanb08cc022020-04-02 11:38:40 -0400985 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500986 }
987 }
988 }
989}
990
Brian Osmanb08cc022020-04-02 11:38:40 -0400991void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500992 int argumentCount = 0;
993 for (const auto& arg : f.fArguments) {
Brian Osmanb08cc022020-04-02 11:38:40 -0400994 this->writeExpression(*arg);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500995 argumentCount += SlotCount(arg->fType);
996 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400997 this->write(ByteCodeInstruction::kCallExternal, SlotCount(f.fType) - argumentCount);
998 SkASSERT(argumentCount <= 255);
999 this->write8(argumentCount);
1000 this->write8(SlotCount(f.fType));
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001001 int index = fOutput->fExternalValues.size();
1002 fOutput->fExternalValues.push_back(f.fFunction);
1003 SkASSERT(index <= 255);
Brian Osmanb08cc022020-04-02 11:38:40 -04001004 this->write8(index);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001005}
1006
Brian Osmanb08cc022020-04-02 11:38:40 -04001007void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) {
1008 int count = SlotCount(e.fValue->type());
1009 this->write(vector_instruction(ByteCodeInstruction::kReadExternal, count));
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001010 int index = fOutput->fExternalValues.size();
1011 fOutput->fExternalValues.push_back(e.fValue);
1012 SkASSERT(index <= 255);
Brian Osmanb08cc022020-04-02 11:38:40 -04001013 this->write8(index);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001014}
1015
Brian Osmanb08cc022020-04-02 11:38:40 -04001016void ByteCodeGenerator::writeVariableExpression(const Expression& expr) {
1017 Location location = this->getLocation(expr);
1018 int count = SlotCount(expr.fType);
Brian Osmanefb08402020-04-13 16:30:44 -04001019 if (count == 0) {
1020 return;
1021 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001022 if (location.isOnStack() || count > 4) {
1023 if (!location.isOnStack()) {
1024 this->write(ByteCodeInstruction::kPushImmediate);
1025 this->write32(location.fSlot);
1026 }
1027 this->write(location.selectLoad(ByteCodeInstruction::kLoadExtended,
1028 ByteCodeInstruction::kLoadExtendedGlobal,
1029 ByteCodeInstruction::kLoadExtendedUniform),
1030 count);
1031 this->write8(count);
1032 } else {
1033 this->write(vector_instruction(location.selectLoad(ByteCodeInstruction::kLoad,
1034 ByteCodeInstruction::kLoadGlobal,
1035 ByteCodeInstruction::kLoadUniform),
1036 count));
Brian Osmanb08cc022020-04-02 11:38:40 -04001037 this->write8(location.fSlot);
1038 }
1039}
1040
1041static inline uint32_t float_to_bits(float x) {
1042 uint32_t u;
1043 memcpy(&u, &x, sizeof(uint32_t));
1044 return u;
1045}
1046
1047void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
1048 this->write(ByteCodeInstruction::kPushImmediate);
1049 this->write32(float_to_bits(f.fValue));
1050}
1051
Brian Osman8842b372020-05-01 15:07:49 -04001052static bool is_generic_type(const Type* type, const Type* generic) {
1053 const std::vector<const Type*>& concrete(generic->coercibleTypes());
1054 return std::find(concrete.begin(), concrete.end(), type) != concrete.end();
1055}
1056
Brian Osmanb08cc022020-04-02 11:38:40 -04001057void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) {
1058 auto found = fIntrinsics.find(c.fFunction.fName);
1059 if (found == fIntrinsics.end()) {
1060 fErrors.error(c.fOffset, String::printf("Unsupported intrinsic: '%s'",
1061 String(c.fFunction.fName).c_str()));
1062 return;
1063 }
Mike Klein45be0772020-05-01 09:13:18 -05001064 Intrinsic intrin = found->second;
Brian Osmanb08cc022020-04-02 11:38:40 -04001065 int count = SlotCount(c.fArguments[0]->fType);
Brian Osmand5f937b2020-05-04 12:07:29 -04001066
1067 // Several intrinsics have variants where one argument is either scalar, or the same size as
1068 // the first argument. Call dupSmallerType(SlotCount(argType)) to ensure equal component count.
1069 auto dupSmallerType = [count, this](int smallCount) {
1070 SkASSERT(smallCount == 1 || smallCount == count);
1071 for (int i = smallCount; i < count; ++i) {
1072 this->write(ByteCodeInstruction::kDup);
1073 }
1074 };
1075
Brian Osmana43d8202020-06-17 16:50:39 -04001076 if (intrin.is_special && intrin.special == SpecialIntrinsic::kSample) {
1077 // Sample is very special, the first argument is an FP, which can't be pushed to the stack
1078 if (c.fArguments.size() != 2 ||
1079 c.fArguments[0]->fType != *fContext.fFragmentProcessor_Type ||
1080 (c.fArguments[1]->fType != *fContext.fFloat2_Type &&
1081 c.fArguments[1]->fType != *fContext.fFloat3x3_Type)) {
1082 fErrors.error(c.fOffset, "Unsupported form of sample");
1083 return;
1084 }
1085
1086 // Write our coords or matrix
1087 this->writeExpression(*c.fArguments[1]);
1088
1089 this->write(c.fArguments[1]->fType == *fContext.fFloat3x3_Type
1090 ? ByteCodeInstruction::kSampleMatrix
1091 : ByteCodeInstruction::kSampleExplicit);
1092
1093 Location childLoc = this->getLocation(*c.fArguments[0]);
1094 SkASSERT(childLoc.fStorage == Storage::kChildFP);
1095 this->write8(childLoc.fSlot);
1096 return;
1097 }
1098
Brian Osmand5f937b2020-05-04 12:07:29 -04001099 if (intrin.is_special && (intrin.special == SpecialIntrinsic::kClamp ||
1100 intrin.special == SpecialIntrinsic::kSaturate)) {
1101 // These intrinsics are extra-special, we need instructions interleaved with arguments
1102 bool saturate = (intrin.special == SpecialIntrinsic::kSaturate);
1103 SkASSERT(c.fArguments.size() == (saturate ? 1 : 3));
1104 int limitCount = saturate ? 1 : SlotCount(c.fArguments[1]->fType);
1105
1106 // 'x'
1107 this->writeExpression(*c.fArguments[0]);
1108
1109 // 'minVal'
1110 if (saturate) {
1111 this->write(ByteCodeInstruction::kPushImmediate);
1112 this->write32(float_to_bits(0.0f));
1113 } else {
1114 this->writeExpression(*c.fArguments[1]);
1115 }
1116 dupSmallerType(limitCount);
1117 this->writeTypedInstruction(c.fArguments[0]->fType,
1118 ByteCodeInstruction::kMaxS,
1119 ByteCodeInstruction::kMaxS,
1120 ByteCodeInstruction::kMaxF,
1121 count);
1122
1123 // 'maxVal'
1124 if (saturate) {
1125 this->write(ByteCodeInstruction::kPushImmediate);
1126 this->write32(float_to_bits(1.0f));
1127 } else {
1128 SkASSERT(limitCount == SlotCount(c.fArguments[2]->fType));
1129 this->writeExpression(*c.fArguments[2]);
1130 }
1131 dupSmallerType(limitCount);
1132 this->writeTypedInstruction(c.fArguments[0]->fType,
1133 ByteCodeInstruction::kMinS,
1134 ByteCodeInstruction::kMinS,
1135 ByteCodeInstruction::kMinF,
1136 count);
1137 return;
1138 }
1139
1140 // All other intrinsics can handle their arguments being on the stack in order
1141 for (const auto& arg : c.fArguments) {
1142 this->writeExpression(*arg);
1143 }
1144
Mike Klein45be0772020-05-01 09:13:18 -05001145 if (intrin.is_special) {
1146 switch (intrin.special) {
Brian Osman8842b372020-05-01 15:07:49 -04001147 case SpecialIntrinsic::kAll: {
1148 for (int i = count-1; i --> 0;) {
1149 this->write(ByteCodeInstruction::kAndB);
1150 }
1151 } break;
1152
1153 case SpecialIntrinsic::kAny: {
1154 for (int i = count-1; i --> 0;) {
1155 this->write(ByteCodeInstruction::kOrB);
1156 }
1157 } break;
1158
Brian Osman15c98cb2020-02-27 18:36:57 +00001159 case SpecialIntrinsic::kDot: {
1160 SkASSERT(c.fArguments.size() == 2);
Brian Osmanb08cc022020-04-02 11:38:40 -04001161 SkASSERT(count == SlotCount(c.fArguments[1]->fType));
1162 this->write(vector_instruction(ByteCodeInstruction::kMultiplyF, count));
Mike Klein45be0772020-05-01 09:13:18 -05001163 for (int i = count-1; i --> 0;) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001164 this->write(ByteCodeInstruction::kAddF);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001165 }
Mike Klein45be0772020-05-01 09:13:18 -05001166 } break;
1167
1168 case SpecialIntrinsic::kLength: {
1169 SkASSERT(c.fArguments.size() == 1);
1170 this->write(vector_instruction(ByteCodeInstruction::kDup , count));
1171 this->write(vector_instruction(ByteCodeInstruction::kMultiplyF, count));
1172 for (int i = count-1; i --> 0;) {
1173 this->write(ByteCodeInstruction::kAddF);
1174 }
1175 this->write(ByteCodeInstruction::kSqrt);
1176 } break;
1177
Brian Osmand5f937b2020-05-04 12:07:29 -04001178 case SpecialIntrinsic::kMax:
1179 case SpecialIntrinsic::kMin: {
1180 SkASSERT(c.fArguments.size() == 2);
1181 // There are variants where the second argument is scalar
1182 dupSmallerType(SlotCount(c.fArguments[1]->fType));
1183 if (intrin.special == SpecialIntrinsic::kMax) {
1184 this->writeTypedInstruction(c.fArguments[0]->fType,
1185 ByteCodeInstruction::kMaxS,
1186 ByteCodeInstruction::kMaxS,
1187 ByteCodeInstruction::kMaxF,
1188 count);
1189 } else {
1190 this->writeTypedInstruction(c.fArguments[0]->fType,
1191 ByteCodeInstruction::kMinS,
1192 ByteCodeInstruction::kMinS,
1193 ByteCodeInstruction::kMinF,
1194 count);
1195 }
1196 } break;
1197
Brian Osman8842b372020-05-01 15:07:49 -04001198 case SpecialIntrinsic::kMix: {
1199 // Two main variants of mix to handle
1200 SkASSERT(c.fArguments.size() == 3);
1201 SkASSERT(count == SlotCount(c.fArguments[1]->fType));
1202 int selectorCount = SlotCount(c.fArguments[2]->fType);
1203
1204 if (is_generic_type(&c.fArguments[2]->fType, fContext.fGenBType_Type.get())) {
1205 // mix(genType, genType, genBoolType)
1206 SkASSERT(selectorCount == count);
1207 this->write(vector_instruction(ByteCodeInstruction::kMix, count));
1208 } else {
1209 // mix(genType, genType, genType) or mix(genType, genType, float)
Brian Osmand5f937b2020-05-04 12:07:29 -04001210 dupSmallerType(selectorCount);
Brian Osman8842b372020-05-01 15:07:49 -04001211 this->write(vector_instruction(ByteCodeInstruction::kLerp, count));
1212 }
1213 } break;
1214
Brian Osmanb08cc022020-04-02 11:38:40 -04001215 default:
1216 SkASSERT(false);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001217 }
1218 } else {
Brian Osman8842b372020-05-01 15:07:49 -04001219 switch (intrin.inst_f) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001220 case ByteCodeInstruction::kInverse2x2: {
1221 SkASSERT(c.fArguments.size() > 0);
1222 auto op = ByteCodeInstruction::kInverse2x2;
1223 switch (count) {
1224 case 4: break; // float2x2
1225 case 9: op = ByteCodeInstruction::kInverse3x3; break;
1226 case 16: op = ByteCodeInstruction::kInverse4x4; break;
1227 default: SkASSERT(false);
1228 }
1229 this->write(op);
1230 break;
Brian Osman15c98cb2020-02-27 18:36:57 +00001231 }
Mike Klein45be0772020-05-01 09:13:18 -05001232
Brian Osmanb08cc022020-04-02 11:38:40 -04001233 default:
Brian Osman8842b372020-05-01 15:07:49 -04001234 this->writeTypedInstruction(c.fArguments[0]->fType, intrin.inst_s, intrin.inst_u,
1235 intrin.inst_f, count);
1236 break;
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001237 }
1238 }
1239}
1240
Brian Osmanb08cc022020-04-02 11:38:40 -04001241void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001242 // Find the index of the function we're calling. We explicitly do not allow calls to functions
1243 // before they're defined. This is an easy-to-understand rule that prevents recursion.
Brian Osmanb08cc022020-04-02 11:38:40 -04001244 int idx = -1;
1245 for (size_t i = 0; i < fFunctions.size(); ++i) {
1246 if (f.fFunction.matches(fFunctions[i]->fDeclaration)) {
1247 idx = i;
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001248 break;
1249 }
1250 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001251 if (idx == -1) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001252 this->writeIntrinsicCall(f);
1253 return;
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001254 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001255
1256
1257 if (idx > 255) {
1258 fErrors.error(f.fOffset, "Function count limit exceeded");
1259 return;
1260 } else if (idx >= (int) fFunctions.size()) {
1261 fErrors.error(f.fOffset, "Call to undefined function");
1262 return;
1263 }
1264
1265 // We may need to deal with out parameters, so the sequence is tricky
1266 if (int returnCount = SlotCount(f.fType)) {
1267 this->write(ByteCodeInstruction::kReserve, returnCount);
1268 this->write8(returnCount);
1269 }
1270
1271 int argCount = f.fArguments.size();
1272 std::vector<std::unique_ptr<LValue>> lvalues;
1273 for (int i = 0; i < argCount; ++i) {
1274 const auto& param = f.fFunction.fParameters[i];
1275 const auto& arg = f.fArguments[i];
1276 if (param->fModifiers.fFlags & Modifiers::kOut_Flag) {
1277 lvalues.emplace_back(this->getLValue(*arg));
1278 lvalues.back()->load();
1279 } else {
1280 this->writeExpression(*arg);
1281 }
1282 }
1283
1284 // The space used by the call is based on the callee, but it also unwinds all of that before
1285 // we continue execution. We adjust our max stack depths below.
1286 this->write(ByteCodeInstruction::kCall);
1287 this->write8(idx);
1288
1289 const ByteCodeFunction* callee = fOutput->fFunctions[idx].get();
1290 fMaxLoopCount = std::max(fMaxLoopCount, fLoopCount + callee->fLoopCount);
1291 fMaxConditionCount = std::max(fMaxConditionCount, fConditionCount + callee->fConditionCount);
1292 fMaxStackCount = std::max(fMaxStackCount, fStackCount + callee->fLocalCount
1293 + callee->fStackCount);
1294
1295 // After the called function returns, the stack will still contain our arguments. We have to
1296 // pop them (storing any out parameters back to their lvalues as we go). We glob together slot
1297 // counts for all parameters that aren't out-params, so we can pop them in one big chunk.
1298 int popCount = 0;
1299 auto pop = [&]() {
1300 if (popCount > 4) {
1301 this->write(ByteCodeInstruction::kPopN, popCount);
1302 this->write8(popCount);
1303 } else if (popCount > 0) {
1304 this->write(vector_instruction(ByteCodeInstruction::kPop, popCount));
1305 }
1306 popCount = 0;
1307 };
1308
1309 for (int i = argCount - 1; i >= 0; --i) {
1310 const auto& param = f.fFunction.fParameters[i];
1311 const auto& arg = f.fArguments[i];
1312 if (param->fModifiers.fFlags & Modifiers::kOut_Flag) {
1313 pop();
1314 lvalues.back()->store(true);
1315 lvalues.pop_back();
1316 } else {
1317 popCount += SlotCount(arg->fType);
1318 }
1319 }
1320 pop();
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001321}
1322
Brian Osmanb08cc022020-04-02 11:38:40 -04001323void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) {
1324 this->write(ByteCodeInstruction::kPushImmediate);
1325 this->write32(i.fValue);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001326}
1327
Brian Osmanb08cc022020-04-02 11:38:40 -04001328void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) {
1329 // not yet implemented
1330 abort();
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001331}
1332
Brian Osmanb08cc022020-04-02 11:38:40 -04001333bool ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p, bool discard) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001334 switch (p.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001335 case Token::Kind::TK_PLUSPLUS: // fall through
1336 case Token::Kind::TK_MINUSMINUS: {
Brian Osmanb08cc022020-04-02 11:38:40 -04001337 SkASSERT(SlotCount(p.fOperand->fType) == 1);
1338 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
1339 lvalue->load();
1340 this->write(ByteCodeInstruction::kPushImmediate);
1341 this->write32(type_category(p.fType) == TypeCategory::kFloat ? float_to_bits(1.0f) : 1);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001342 if (p.fOperator == Token::Kind::TK_PLUSPLUS) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001343 this->writeTypedInstruction(p.fType,
1344 ByteCodeInstruction::kAddI,
1345 ByteCodeInstruction::kAddI,
1346 ByteCodeInstruction::kAddF,
1347 1);
1348 } else {
1349 this->writeTypedInstruction(p.fType,
1350 ByteCodeInstruction::kSubtractI,
1351 ByteCodeInstruction::kSubtractI,
1352 ByteCodeInstruction::kSubtractF,
1353 1);
1354 }
1355 lvalue->store(discard);
1356 discard = false;
1357 break;
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001358 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001359 case Token::Kind::TK_MINUS: {
Brian Osmanb08cc022020-04-02 11:38:40 -04001360 this->writeExpression(*p.fOperand);
1361 this->writeTypedInstruction(p.fType,
1362 ByteCodeInstruction::kNegateI,
1363 ByteCodeInstruction::kNegateI,
1364 ByteCodeInstruction::kNegateF,
Brian Osmanab8f3842020-04-07 09:30:44 -04001365 SlotCount(p.fOperand->fType));
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001366 break;
1367 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001368 case Token::Kind::TK_LOGICALNOT:
1369 case Token::Kind::TK_BITWISENOT: {
Brian Osmanb08cc022020-04-02 11:38:40 -04001370 SkASSERT(SlotCount(p.fOperand->fType) == 1);
1371 SkDEBUGCODE(TypeCategory tc = type_category(p.fOperand->fType));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001372 SkASSERT((p.fOperator == Token::Kind::TK_LOGICALNOT && tc == TypeCategory::kBool) ||
1373 (p.fOperator == Token::Kind::TK_BITWISENOT && (tc == TypeCategory::kSigned ||
Brian Osmanb08cc022020-04-02 11:38:40 -04001374 tc == TypeCategory::kUnsigned)));
1375 this->writeExpression(*p.fOperand);
1376 this->write(ByteCodeInstruction::kNotB);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001377 break;
1378 }
1379 default:
1380 SkASSERT(false);
1381 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001382 return discard;
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001383}
1384
Brian Osmanb08cc022020-04-02 11:38:40 -04001385bool ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p, bool discard) {
1386 switch (p.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001387 case Token::Kind::TK_PLUSPLUS: // fall through
1388 case Token::Kind::TK_MINUSMINUS: {
Brian Osmanb08cc022020-04-02 11:38:40 -04001389 SkASSERT(SlotCount(p.fOperand->fType) == 1);
1390 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
1391 lvalue->load();
1392 // If we're not supposed to discard the result, then make a copy *before* the +/-
1393 if (!discard) {
1394 this->write(ByteCodeInstruction::kDup);
Brian Osmanb08cc022020-04-02 11:38:40 -04001395 }
1396 this->write(ByteCodeInstruction::kPushImmediate);
1397 this->write32(type_category(p.fType) == TypeCategory::kFloat ? float_to_bits(1.0f) : 1);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001398 if (p.fOperator == Token::Kind::TK_PLUSPLUS) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001399 this->writeTypedInstruction(p.fType,
1400 ByteCodeInstruction::kAddI,
1401 ByteCodeInstruction::kAddI,
1402 ByteCodeInstruction::kAddF,
1403 1);
1404 } else {
1405 this->writeTypedInstruction(p.fType,
1406 ByteCodeInstruction::kSubtractI,
1407 ByteCodeInstruction::kSubtractI,
1408 ByteCodeInstruction::kSubtractF,
1409 1);
1410 }
1411 // Always consume the result as part of the store
1412 lvalue->store(true);
1413 discard = false;
1414 break;
1415 }
1416 default:
1417 SkASSERT(false);
1418 }
1419 return discard;
1420}
1421
1422void ByteCodeGenerator::writeSwizzle(const Swizzle& s) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001423 if (swizzle_is_simple(s)) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001424 this->writeVariableExpression(s);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001425 return;
1426 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001427
Brian Osman3711c662020-06-18 14:42:21 -04001428 this->writeExpression(*s.fBase);
1429 this->write(ByteCodeInstruction::kSwizzle, s.fComponents.size() - s.fBase->fType.columns());
1430 this->write8(s.fBase->fType.columns());
1431 this->write8(s.fComponents.size());
1432 for (int c : s.fComponents) {
1433 this->write8(c);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001434 }
1435}
1436
Brian Osmanb08cc022020-04-02 11:38:40 -04001437void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001438 int count = SlotCount(t.fType);
1439 SkASSERT(count == SlotCount(t.fIfTrue->fType));
1440 SkASSERT(count == SlotCount(t.fIfFalse->fType));
1441
Brian Osmanb08cc022020-04-02 11:38:40 -04001442 this->writeExpression(*t.fTest);
1443 this->write(ByteCodeInstruction::kMaskPush);
1444 this->writeExpression(*t.fIfTrue);
1445 this->write(ByteCodeInstruction::kMaskNegate);
1446 this->writeExpression(*t.fIfFalse);
1447 this->write(ByteCodeInstruction::kMaskBlend, count);
1448 this->write8(count);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001449}
1450
Brian Osmanb08cc022020-04-02 11:38:40 -04001451void ByteCodeGenerator::writeExpression(const Expression& e, bool discard) {
1452 switch (e.fKind) {
1453 case Expression::kBinary_Kind:
1454 discard = this->writeBinaryExpression((BinaryExpression&) e, discard);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001455 break;
Brian Osmanb08cc022020-04-02 11:38:40 -04001456 case Expression::kBoolLiteral_Kind:
1457 this->writeBoolLiteral((BoolLiteral&) e);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001458 break;
Brian Osmanb08cc022020-04-02 11:38:40 -04001459 case Expression::kConstructor_Kind:
1460 this->writeConstructor((Constructor&) e);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001461 break;
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001462 case Expression::kExternalFunctionCall_Kind:
Brian Osmanb08cc022020-04-02 11:38:40 -04001463 this->writeExternalFunctionCall((ExternalFunctionCall&) e);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001464 break;
1465 case Expression::kExternalValue_Kind:
Brian Osmanb08cc022020-04-02 11:38:40 -04001466 this->writeExternalValue((ExternalValueReference&) e);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001467 break;
1468 case Expression::kFieldAccess_Kind:
1469 case Expression::kIndex_Kind:
1470 case Expression::kVariableReference_Kind:
Brian Osmanb08cc022020-04-02 11:38:40 -04001471 this->writeVariableExpression(e);
1472 break;
1473 case Expression::kFloatLiteral_Kind:
1474 this->writeFloatLiteral((FloatLiteral&) e);
1475 break;
1476 case Expression::kFunctionCall_Kind:
1477 this->writeFunctionCall((FunctionCall&) e);
1478 break;
1479 case Expression::kIntLiteral_Kind:
1480 this->writeIntLiteral((IntLiteral&) e);
1481 break;
1482 case Expression::kNullLiteral_Kind:
1483 this->writeNullLiteral((NullLiteral&) e);
1484 break;
1485 case Expression::kPrefix_Kind:
1486 discard = this->writePrefixExpression((PrefixExpression&) e, discard);
1487 break;
1488 case Expression::kPostfix_Kind:
1489 discard = this->writePostfixExpression((PostfixExpression&) e, discard);
1490 break;
1491 case Expression::kSwizzle_Kind:
1492 this->writeSwizzle((Swizzle&) e);
1493 break;
1494 case Expression::kTernary_Kind:
1495 this->writeTernaryExpression((TernaryExpression&) e);
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001496 break;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001497 default:
1498#ifdef SK_DEBUG
Brian Osmanb08cc022020-04-02 11:38:40 -04001499 printf("unsupported expression %s\n", e.description().c_str());
Ben Wagner470e0ac2020-01-22 16:59:21 -05001500#endif
Brian Osmanb08cc022020-04-02 11:38:40 -04001501 SkASSERT(false);
1502 }
1503 if (discard) {
1504 int count = SlotCount(e.fType);
1505 if (count > 4) {
1506 this->write(ByteCodeInstruction::kPopN, count);
1507 this->write8(count);
1508 } else if (count != 0) {
1509 this->write(vector_instruction(ByteCodeInstruction::kPop, count));
1510 }
1511 discard = false;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001512 }
Ethan Nicholas7deb1c22020-01-22 10:31:55 -05001513}
1514
Brian Osmanb08cc022020-04-02 11:38:40 -04001515class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue {
1516public:
1517 ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index)
1518 : INHERITED(*generator)
1519 , fCount(ByteCodeGenerator::SlotCount(value.type()))
1520 , fIndex(index) {}
1521
1522 void load() override {
1523 fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount));
Brian Osmanb08cc022020-04-02 11:38:40 -04001524 fGenerator.write8(fIndex);
1525 }
1526
1527 void store(bool discard) override {
1528 if (!discard) {
1529 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount));
Brian Osmanb08cc022020-04-02 11:38:40 -04001530 }
1531 fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount));
Brian Osmanb08cc022020-04-02 11:38:40 -04001532 fGenerator.write8(fIndex);
1533 }
1534
1535private:
1536 typedef LValue INHERITED;
1537
1538 int fCount;
1539
1540 int fIndex;
1541};
1542
1543class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue {
1544public:
1545 ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle)
1546 : INHERITED(*generator)
1547 , fSwizzle(swizzle) {}
1548
1549 void load() override {
1550 fGenerator.writeSwizzle(fSwizzle);
1551 }
1552
1553 void store(bool discard) override {
1554 int count = fSwizzle.fComponents.size();
1555 if (!discard) {
1556 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count));
Brian Osmanb08cc022020-04-02 11:38:40 -04001557 }
1558 ByteCodeGenerator::Location location = fGenerator.getLocation(*fSwizzle.fBase);
1559 if (location.isOnStack()) {
1560 fGenerator.write(location.selectStore(ByteCodeInstruction::kStoreSwizzleIndirect,
1561 ByteCodeInstruction::kStoreSwizzleIndirectGlobal),
1562 count);
1563 } else {
1564 fGenerator.write(location.selectStore(ByteCodeInstruction::kStoreSwizzle,
1565 ByteCodeInstruction::kStoreSwizzleGlobal),
1566 count);
1567 fGenerator.write8(location.fSlot);
1568 }
1569 fGenerator.write8(count);
1570 for (int c : fSwizzle.fComponents) {
1571 fGenerator.write8(c);
1572 }
1573 }
1574
1575private:
1576 const Swizzle& fSwizzle;
1577
1578 typedef LValue INHERITED;
1579};
1580
1581class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue {
1582public:
1583 ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr)
1584 : INHERITED(*generator)
1585 , fExpression(expr) {}
1586
1587 void load() override {
1588 fGenerator.writeVariableExpression(fExpression);
1589 }
1590
1591 void store(bool discard) override {
1592 int count = ByteCodeGenerator::SlotCount(fExpression.fType);
1593 if (!discard) {
1594 if (count > 4) {
1595 fGenerator.write(ByteCodeInstruction::kDupN, count);
1596 fGenerator.write8(count);
1597 } else {
1598 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count));
Brian Osmanb08cc022020-04-02 11:38:40 -04001599 }
1600 }
1601 ByteCodeGenerator::Location location = fGenerator.getLocation(fExpression);
1602 if (location.isOnStack() || count > 4) {
1603 if (!location.isOnStack()) {
1604 fGenerator.write(ByteCodeInstruction::kPushImmediate);
1605 fGenerator.write32(location.fSlot);
1606 }
1607 fGenerator.write(location.selectStore(ByteCodeInstruction::kStoreExtended,
1608 ByteCodeInstruction::kStoreExtendedGlobal),
1609 count);
1610 fGenerator.write8(count);
1611 } else {
1612 fGenerator.write(
1613 vector_instruction(location.selectStore(ByteCodeInstruction::kStore,
1614 ByteCodeInstruction::kStoreGlobal),
1615 count));
1616 fGenerator.write8(location.fSlot);
1617 }
1618 }
1619
1620private:
1621 typedef LValue INHERITED;
1622
1623 const Expression& fExpression;
1624};
1625
1626std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) {
1627 switch (e.fKind) {
1628 case Expression::kExternalValue_Kind: {
1629 ExternalValue* value = ((ExternalValueReference&) e).fValue;
1630 int index = fOutput->fExternalValues.size();
1631 fOutput->fExternalValues.push_back(value);
1632 SkASSERT(index <= 255);
1633 return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index));
1634 }
1635 case Expression::kFieldAccess_Kind:
1636 case Expression::kIndex_Kind:
1637 case Expression::kVariableReference_Kind:
1638 return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e));
1639 case Expression::kSwizzle_Kind: {
1640 const Swizzle& s = (const Swizzle&) e;
1641 return swizzle_is_simple(s)
1642 ? std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e))
1643 : std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, s));
1644 }
1645 case Expression::kTernary_Kind:
1646 default:
1647#ifdef SK_DEBUG
1648 ABORT("unsupported lvalue %s\n", e.description().c_str());
1649#endif
1650 return nullptr;
1651 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001652}
1653
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001654void ByteCodeGenerator::writeBlock(const Block& b) {
1655 for (const auto& s : b.fStatements) {
1656 this->writeStatement(*s);
1657 }
1658}
1659
Brian Osmanb08cc022020-04-02 11:38:40 -04001660void ByteCodeGenerator::setBreakTargets() {
1661 std::vector<DeferredLocation>& breaks = fBreakTargets.top();
1662 for (DeferredLocation& b : breaks) {
1663 b.set();
1664 }
1665 fBreakTargets.pop();
1666}
1667
1668void ByteCodeGenerator::setContinueTargets() {
1669 std::vector<DeferredLocation>& continues = fContinueTargets.top();
1670 for (DeferredLocation& c : continues) {
1671 c.set();
1672 }
1673 fContinueTargets.pop();
1674}
1675
1676void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) {
1677 // TODO: Include BranchIfAllFalse to top-most LoopNext
1678 this->write(ByteCodeInstruction::kLoopBreak);
1679}
1680
1681void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) {
1682 // TODO: Include BranchIfAllFalse to top-most LoopNext
1683 this->write(ByteCodeInstruction::kLoopContinue);
1684}
1685
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001686void ByteCodeGenerator::writeDoStatement(const DoStatement& d) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001687 this->write(ByteCodeInstruction::kLoopBegin);
1688 size_t start = fCode->size();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001689 this->writeStatement(*d.fStatement);
Brian Osmanb08cc022020-04-02 11:38:40 -04001690 this->write(ByteCodeInstruction::kLoopNext);
1691 this->writeExpression(*d.fTest);
1692 this->write(ByteCodeInstruction::kLoopMask);
1693 // TODO: Could shorten this with kBranchIfAnyTrue
1694 this->write(ByteCodeInstruction::kBranchIfAllFalse);
Brian Osman569f12f2019-06-13 11:23:57 -04001695 DeferredLocation endLocation(this);
Brian Osmanb08cc022020-04-02 11:38:40 -04001696 this->write(ByteCodeInstruction::kBranch);
1697 this->write16(start);
Brian Osman569f12f2019-06-13 11:23:57 -04001698 endLocation.set();
Brian Osmanb08cc022020-04-02 11:38:40 -04001699 this->write(ByteCodeInstruction::kLoopEnd);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001700}
1701
1702void ByteCodeGenerator::writeForStatement(const ForStatement& f) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001703 fContinueTargets.emplace();
1704 fBreakTargets.emplace();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001705 if (f.fInitializer) {
1706 this->writeStatement(*f.fInitializer);
1707 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001708 this->write(ByteCodeInstruction::kLoopBegin);
1709 size_t start = fCode->size();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001710 if (f.fTest) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001711 this->writeExpression(*f.fTest);
1712 this->write(ByteCodeInstruction::kLoopMask);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001713 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001714 this->write(ByteCodeInstruction::kBranchIfAllFalse);
Brian Osman569f12f2019-06-13 11:23:57 -04001715 DeferredLocation endLocation(this);
1716 this->writeStatement(*f.fStatement);
Brian Osmanb08cc022020-04-02 11:38:40 -04001717 this->write(ByteCodeInstruction::kLoopNext);
Brian Osman569f12f2019-06-13 11:23:57 -04001718 if (f.fNext) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001719 this->writeExpression(*f.fNext, true);
Brian Osman569f12f2019-06-13 11:23:57 -04001720 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001721 this->write(ByteCodeInstruction::kBranch);
1722 this->write16(start);
Brian Osman569f12f2019-06-13 11:23:57 -04001723 endLocation.set();
Brian Osmanb08cc022020-04-02 11:38:40 -04001724 this->write(ByteCodeInstruction::kLoopEnd);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001725}
1726
1727void ByteCodeGenerator::writeIfStatement(const IfStatement& i) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001728 this->writeExpression(*i.fTest);
1729 this->write(ByteCodeInstruction::kMaskPush);
1730 this->write(ByteCodeInstruction::kBranchIfAllFalse);
Brian Osman569f12f2019-06-13 11:23:57 -04001731 DeferredLocation falseLocation(this);
1732 this->writeStatement(*i.fIfTrue);
1733 falseLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001734 if (i.fIfFalse) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001735 this->write(ByteCodeInstruction::kMaskNegate);
1736 this->write(ByteCodeInstruction::kBranchIfAllFalse);
Brian Osman569f12f2019-06-13 11:23:57 -04001737 DeferredLocation endLocation(this);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001738 this->writeStatement(*i.fIfFalse);
Mike Kleinb45ee832019-05-17 11:11:11 -05001739 endLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001740 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001741 this->write(ByteCodeInstruction::kMaskPop);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001742}
1743
Brian Osmanb08cc022020-04-02 11:38:40 -04001744void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
1745 if (fLoopCount || fConditionCount) {
Brian Osman4a47da72019-07-12 11:30:32 -04001746 fErrors.error(r.fOffset, "return not allowed inside conditional or loop");
1747 return;
1748 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001749 int count = SlotCount(r.fExpression->fType);
1750 this->writeExpression(*r.fExpression);
1751
1752 // Technically, the kReturn also pops fOutput->fLocalCount values from the stack, too, but we
1753 // haven't counted pushing those (they're outside the scope of our stack tracking). Instead,
1754 // we account for those in writeFunction().
1755
1756 // This is all fine because we don't allow conditional returns, so we only return once anyway.
1757 this->write(ByteCodeInstruction::kReturn, -count);
1758 this->write8(count);
1759}
1760
1761void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) {
1762 // not yet implemented
1763 abort();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001764}
1765
1766void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) {
1767 for (const auto& declStatement : v.fVars) {
1768 const VarDeclaration& decl = (VarDeclaration&) *declStatement;
Brian Osmanb08cc022020-04-02 11:38:40 -04001769 // we need to grab the location even if we don't use it, to ensure it has been allocated
1770 Location location = this->getLocation(*decl.fVar);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001771 if (decl.fValue) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001772 this->writeExpression(*decl.fValue);
1773 int count = SlotCount(decl.fValue->fType);
1774 if (count > 4) {
1775 this->write(ByteCodeInstruction::kPushImmediate);
1776 this->write32(location.fSlot);
1777 this->write(ByteCodeInstruction::kStoreExtended, count);
1778 this->write8(count);
1779 } else {
1780 this->write(vector_instruction(ByteCodeInstruction::kStore, count));
1781 this->write8(location.fSlot);
1782 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001783 }
1784 }
1785}
1786
1787void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001788 this->write(ByteCodeInstruction::kLoopBegin);
1789 size_t cond = fCode->size();
1790 this->writeExpression(*w.fTest);
1791 this->write(ByteCodeInstruction::kLoopMask);
1792 this->write(ByteCodeInstruction::kBranchIfAllFalse);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001793 DeferredLocation endLocation(this);
1794 this->writeStatement(*w.fStatement);
Brian Osmanb08cc022020-04-02 11:38:40 -04001795 this->write(ByteCodeInstruction::kLoopNext);
1796 this->write(ByteCodeInstruction::kBranch);
1797 this->write16(cond);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001798 endLocation.set();
Brian Osmanb08cc022020-04-02 11:38:40 -04001799 this->write(ByteCodeInstruction::kLoopEnd);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001800}
1801
1802void ByteCodeGenerator::writeStatement(const Statement& s) {
1803 switch (s.fKind) {
1804 case Statement::kBlock_Kind:
1805 this->writeBlock((Block&) s);
1806 break;
1807 case Statement::kBreak_Kind:
Brian Osmanb08cc022020-04-02 11:38:40 -04001808 this->writeBreakStatement((BreakStatement&) s);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001809 break;
1810 case Statement::kContinue_Kind:
Brian Osmanb08cc022020-04-02 11:38:40 -04001811 this->writeContinueStatement((ContinueStatement&) s);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001812 break;
Brian Osmanb08cc022020-04-02 11:38:40 -04001813 case Statement::kDiscard_Kind:
1814 // not yet implemented
1815 abort();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001816 case Statement::kDo_Kind:
1817 this->writeDoStatement((DoStatement&) s);
1818 break;
Brian Osman3e29f1d2019-05-28 09:35:05 -04001819 case Statement::kExpression_Kind:
Brian Osmanb08cc022020-04-02 11:38:40 -04001820 this->writeExpression(*((ExpressionStatement&) s).fExpression, true);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001821 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001822 case Statement::kFor_Kind:
1823 this->writeForStatement((ForStatement&) s);
1824 break;
1825 case Statement::kIf_Kind:
1826 this->writeIfStatement((IfStatement&) s);
1827 break;
1828 case Statement::kNop_Kind:
1829 break;
1830 case Statement::kReturn_Kind:
Brian Osmanb08cc022020-04-02 11:38:40 -04001831 this->writeReturnStatement((ReturnStatement&) s);
1832 break;
1833 case Statement::kSwitch_Kind:
1834 this->writeSwitchStatement((SwitchStatement&) s);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001835 break;
1836 case Statement::kVarDeclarations_Kind:
1837 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration);
1838 break;
1839 case Statement::kWhile_Kind:
1840 this->writeWhileStatement((WhileStatement&) s);
1841 break;
1842 default:
Brian Osmanb08cc022020-04-02 11:38:40 -04001843 SkASSERT(false);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001844 }
1845}
1846
Brian Osmanb08cc022020-04-02 11:38:40 -04001847ByteCodeFunction::ByteCodeFunction(const FunctionDeclaration* declaration)
1848 : fName(declaration->fName) {
Brian Osman80164412019-06-07 13:00:23 -04001849 fParameterCount = 0;
Brian Osmanb08cc022020-04-02 11:38:40 -04001850 for (const auto& p : declaration->fParameters) {
1851 int slots = ByteCodeGenerator::SlotCount(p->fType);
1852 fParameters.push_back({ slots, (bool)(p->fModifiers.fFlags & Modifiers::kOut_Flag) });
1853 fParameterCount += slots;
Brian Osman80164412019-06-07 13:00:23 -04001854 }
1855}
1856
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001857}