blob: b9aac93b1a3eaf5936b066ec6621c499e67173ab [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLByteCodeGenerator.h"
Brian Osmanf3fa6002019-05-17 14:26:53 -04009#include "src/sksl/SkSLInterpreter.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040010
11namespace SkSL {
12
Ethan Nicholas82162ee2019-05-21 16:05:08 -040013ByteCodeGenerator::ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
14 ByteCode* output)
15 : INHERITED(program, errors, nullptr)
16 , fContext(*context)
Ethan Nicholasae9633b2019-05-24 12:46:34 -040017 , fOutput(output)
18 , fIntrinsics {
19 { "cos", ByteCodeInstruction::kCos },
20 { "cross", ByteCodeInstruction::kCross },
21 { "dot", SpecialIntrinsic::kDot },
22 { "sin", ByteCodeInstruction::kSin },
23 { "sqrt", ByteCodeInstruction::kSqrt },
24 { "tan", ByteCodeInstruction::kTan },
25 { "mix", ByteCodeInstruction::kMix },
26 } {}
27
Ethan Nicholas82162ee2019-05-21 16:05:08 -040028
Brian Osman07c117b2019-05-23 12:51:06 -070029int ByteCodeGenerator::SlotCount(const Type& type) {
30 if (type.kind() == Type::kStruct_Kind) {
31 int slots = 0;
32 for (const auto& f : type.fields()) {
33 slots += SlotCount(*f.fType);
34 }
35 SkASSERT(slots <= 255);
36 return slots;
37 } else if (type.kind() == Type::kArray_Kind) {
38 int columns = type.columns();
39 SkASSERT(columns >= 0);
40 int slots = columns * SlotCount(type.componentType());
41 SkASSERT(slots <= 255);
42 return slots;
43 } else {
44 return type.columns() * type.rows();
45 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040046}
47
48bool ByteCodeGenerator::generateCode() {
49 for (const auto& e : fProgram) {
50 switch (e.fKind) {
51 case ProgramElement::kFunction_Kind: {
52 std::unique_ptr<ByteCodeFunction> f = this->writeFunction((FunctionDefinition&) e);
53 if (!f) {
54 return false;
55 }
56 fOutput->fFunctions.push_back(std::move(f));
57 break;
58 }
59 case ProgramElement::kVar_Kind: {
60 VarDeclarations& decl = (VarDeclarations&) e;
61 for (const auto& v : decl.fVars) {
62 const Variable* declVar = ((VarDeclaration&) *v).fVar;
63 if (declVar->fModifiers.fLayout.fBuiltin >= 0) {
64 continue;
65 }
66 if (declVar->fModifiers.fFlags & Modifiers::kIn_Flag) {
Brian Osman07c117b2019-05-23 12:51:06 -070067 for (int i = SlotCount(declVar->fType); i > 0; --i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040068 fOutput->fInputSlots.push_back(fOutput->fGlobalCount++);
69 }
70 } else {
Brian Osman07c117b2019-05-23 12:51:06 -070071 fOutput->fGlobalCount += SlotCount(declVar->fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040072 }
73 }
74 break;
75 }
76 default:
77 ; // ignore
78 }
79 }
Brian Osman226668a2019-05-14 16:47:30 -040080 for (auto& call : fCallTargets) {
81 if (!call.set()) {
82 return false;
83 }
84 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040085 return true;
86}
87
88std::unique_ptr<ByteCodeFunction> ByteCodeGenerator::writeFunction(const FunctionDefinition& f) {
89 fFunction = &f;
Brian Osman226668a2019-05-14 16:47:30 -040090 std::unique_ptr<ByteCodeFunction> result(new ByteCodeFunction(&f.fDeclaration));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040091 fParameterCount = 0;
92 for (const auto& p : f.fDeclaration.fParameters) {
Brian Osman07c117b2019-05-23 12:51:06 -070093 fParameterCount += SlotCount(p->fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040094 }
95 fCode = &result->fCode;
96 this->writeStatement(*f.fBody);
Ethan Nicholas7e603db2019-05-03 12:57:47 -040097 this->write(ByteCodeInstruction::kReturn);
98 this->write8(0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040099 result->fParameterCount = fParameterCount;
100 result->fLocalCount = fLocals.size();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400101 const Type& returnType = f.fDeclaration.fReturnType;
102 if (returnType != *fContext.fVoid_Type) {
Brian Osman07c117b2019-05-23 12:51:06 -0700103 result->fReturnCount = SlotCount(returnType);
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400104 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400105 fLocals.clear();
106 fFunction = nullptr;
107 return result;
108}
109
110enum class TypeCategory {
111 kBool,
112 kSigned,
113 kUnsigned,
114 kFloat,
115};
116
117static TypeCategory type_category(const Type& type) {
118 switch (type.kind()) {
119 case Type::Kind::kVector_Kind:
120 case Type::Kind::kMatrix_Kind:
121 return type_category(type.componentType());
122 default:
123 if (type.fName == "bool") {
124 return TypeCategory::kBool;
125 } else if (type.fName == "int" || type.fName == "short") {
126 return TypeCategory::kSigned;
127 } else if (type.fName == "uint" || type.fName == "ushort") {
128 return TypeCategory::kUnsigned;
129 } else {
130 SkASSERT(type.fName == "float" || type.fName == "half");
131 return TypeCategory::kFloat;
132 }
133 ABORT("unsupported type: %s\n", type.description().c_str());
134 }
135}
136
Brian Osman0785db02019-05-24 14:19:11 -0400137// A "simple" Swizzle is based on a variable (or a compound variable like a struct or array), and
138// that references consecutive values, such that it can be implemented using normal load/store ops
139// with an offset. Note that all single-component swizzles (of suitable base types) are simple.
140static bool swizzle_is_simple(const Swizzle& s) {
141 switch (s.fBase->fKind) {
142 case Expression::kFieldAccess_Kind:
143 case Expression::kIndex_Kind:
144 case Expression::kVariableReference_Kind:
145 break;
146 default:
147 return false;
148 }
149
150 for (size_t i = 1; i < s.fComponents.size(); ++i) {
151 if (s.fComponents[i] != s.fComponents[i - 1] + 1) {
152 return false;
153 }
154 }
155 return true;
156}
157
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400158int ByteCodeGenerator::getLocation(const Variable& var) {
159 // given that we seldom have more than a couple of variables, linear search is probably the most
160 // efficient way to handle lookups
161 switch (var.fStorage) {
162 case Variable::kLocal_Storage: {
163 for (int i = fLocals.size() - 1; i >= 0; --i) {
164 if (fLocals[i] == &var) {
Brian Osman1091f022019-05-16 09:42:16 -0400165 SkASSERT(fParameterCount + i <= 255);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400166 return fParameterCount + i;
167 }
168 }
169 int result = fParameterCount + fLocals.size();
170 fLocals.push_back(&var);
Brian Osman07c117b2019-05-23 12:51:06 -0700171 for (int i = 0; i < SlotCount(var.fType) - 1; ++i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400172 fLocals.push_back(nullptr);
173 }
Brian Osman1091f022019-05-16 09:42:16 -0400174 SkASSERT(result <= 255);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400175 return result;
176 }
177 case Variable::kParameter_Storage: {
178 int offset = 0;
179 for (const auto& p : fFunction->fDeclaration.fParameters) {
180 if (p == &var) {
Brian Osman1091f022019-05-16 09:42:16 -0400181 SkASSERT(offset <= 255);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400182 return offset;
183 }
Brian Osman07c117b2019-05-23 12:51:06 -0700184 offset += SlotCount(p->fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400185 }
186 SkASSERT(false);
Brian Osman1091f022019-05-16 09:42:16 -0400187 return 0;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400188 }
189 case Variable::kGlobal_Storage: {
190 int offset = 0;
191 for (const auto& e : fProgram) {
192 if (e.fKind == ProgramElement::kVar_Kind) {
193 VarDeclarations& decl = (VarDeclarations&) e;
194 for (const auto& v : decl.fVars) {
195 const Variable* declVar = ((VarDeclaration&) *v).fVar;
196 if (declVar->fModifiers.fLayout.fBuiltin >= 0) {
197 continue;
198 }
199 if (declVar == &var) {
Brian Osmanb7451292019-05-15 13:02:13 -0400200 SkASSERT(offset <= 255);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400201 return offset;
202 }
Brian Osman07c117b2019-05-23 12:51:06 -0700203 offset += SlotCount(declVar->fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400204 }
205 }
206 }
207 SkASSERT(false);
Brian Osman1091f022019-05-16 09:42:16 -0400208 return 0;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400209 }
210 default:
211 SkASSERT(false);
212 return 0;
213 }
214}
215
Brian Osman0785db02019-05-24 14:19:11 -0400216// TODO: Elide Add 0 and Mul 1 sequences
Brian Osman07c117b2019-05-23 12:51:06 -0700217int ByteCodeGenerator::getLocation(const Expression& expr, Variable::Storage* storage) {
218 switch (expr.fKind) {
219 case Expression::kFieldAccess_Kind: {
220 const FieldAccess& f = (const FieldAccess&)expr;
221 int baseAddr = this->getLocation(*f.fBase, storage);
222 int offset = 0;
223 for (int i = 0; i < f.fFieldIndex; ++i) {
224 offset += SlotCount(*f.fBase->fType.fields()[i].fType);
225 }
226 if (baseAddr < 0) {
227 this->write(ByteCodeInstruction::kPushImmediate);
228 this->write32(offset);
229 this->write(ByteCodeInstruction::kAddI);
230 return -1;
231 } else {
232 return baseAddr + offset;
233 }
234 }
235 case Expression::kIndex_Kind: {
236 const IndexExpression& i = (const IndexExpression&)expr;
237 int stride = SlotCount(i.fType);
238 int offset = -1;
239 if (i.fIndex->isConstant()) {
240 offset = i.fIndex->getConstantInt() * stride;
241 } else {
242 this->writeExpression(*i.fIndex);
243 this->write(ByteCodeInstruction::kPushImmediate);
244 this->write32(stride);
245 this->write(ByteCodeInstruction::kMultiplyI);
246 }
247 int baseAddr = this->getLocation(*i.fBase, storage);
248 if (baseAddr >= 0 && offset >= 0) {
249 return baseAddr + offset;
250 }
251 if (baseAddr >= 0) {
252 this->write(ByteCodeInstruction::kPushImmediate);
253 this->write32(baseAddr);
254 }
255 if (offset >= 0) {
256 this->write(ByteCodeInstruction::kPushImmediate);
257 this->write32(offset);
258 }
259 this->write(ByteCodeInstruction::kAddI);
260 return -1;
261 }
Brian Osman0785db02019-05-24 14:19:11 -0400262 case Expression::kSwizzle_Kind: {
263 const Swizzle& s = (const Swizzle&)expr;
264 SkASSERT(swizzle_is_simple(s));
265 int baseAddr = this->getLocation(*s.fBase, storage);
266 int offset = s.fComponents[0];
267 if (baseAddr < 0) {
268 this->write(ByteCodeInstruction::kPushImmediate);
269 this->write32(offset);
270 this->write(ByteCodeInstruction::kAddI);
271 return -1;
272 } else {
273 return baseAddr + offset;
274 }
275 }
Brian Osman07c117b2019-05-23 12:51:06 -0700276 case Expression::kVariableReference_Kind: {
277 const Variable& var = ((const VariableReference&)expr).fVariable;
278 *storage = var.fStorage;
279 return this->getLocation(var);
280 }
281 default:
282 SkASSERT(false);
283 return 0;
284 }
285}
286
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400287void ByteCodeGenerator::write8(uint8_t b) {
288 fCode->push_back(b);
289}
290
291void ByteCodeGenerator::write16(uint16_t i) {
Mike Klein76346ac2019-05-17 11:57:10 -0500292 size_t n = fCode->size();
293 fCode->resize(n+2);
294 memcpy(fCode->data() + n, &i, 2);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400295}
296
297void ByteCodeGenerator::write32(uint32_t i) {
Mike Klein76346ac2019-05-17 11:57:10 -0500298 size_t n = fCode->size();
299 fCode->resize(n+4);
300 memcpy(fCode->data() + n, &i, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400301}
302
303void ByteCodeGenerator::write(ByteCodeInstruction i) {
Mike Klein108e9352019-05-21 11:05:17 -0500304 this->write16((uint16_t)i);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400305}
306
Mike Klein76346ac2019-05-17 11:57:10 -0500307static ByteCodeInstruction vector_instruction(ByteCodeInstruction base, int count) {
Brian Osman07c117b2019-05-23 12:51:06 -0700308 SkASSERT(count >= 1 && count <= 4);
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400309 return ((ByteCodeInstruction) ((int) base + count - 1));
310}
311
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400312void ByteCodeGenerator::writeTypedInstruction(const Type& type, ByteCodeInstruction s,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400313 ByteCodeInstruction u, ByteCodeInstruction f,
314 int count) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400315 switch (type_category(type)) {
316 case TypeCategory::kSigned:
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400317 this->write(vector_instruction(s, count));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400318 break;
319 case TypeCategory::kUnsigned:
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400320 this->write(vector_instruction(u, count));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400321 break;
Brian Osman1e855b22019-05-29 15:21:52 -0400322 case TypeCategory::kFloat: {
323 if (count > 4) {
324 this->write((ByteCodeInstruction)((int)f + 4));
325 this->write8(count);
326 } else {
327 this->write(vector_instruction(f, count));
328 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400329 break;
Brian Osman1e855b22019-05-29 15:21:52 -0400330 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400331 default:
332 SkASSERT(false);
333 }
334}
335
336void ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b) {
337 if (b.fOperator == Token::Kind::EQ) {
338 std::unique_ptr<LValue> lvalue = this->getLValue(*b.fLeft);
339 this->writeExpression(*b.fRight);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400340 lvalue->store();
341 return;
342 }
Brian Osman16e6fd52019-05-29 11:19:00 -0400343 const Type& lType = b.fLeft->fType;
344 const Type& rType = b.fRight->fType;
Brian Osman909231c2019-05-29 15:34:36 -0400345 bool lVecOrMtx = (lType.kind() == Type::kVector_Kind || lType.kind() == Type::kMatrix_Kind);
346 bool rVecOrMtx = (rType.kind() == Type::kVector_Kind || rType.kind() == Type::kMatrix_Kind);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400347 Token::Kind op;
348 std::unique_ptr<LValue> lvalue;
349 if (is_assignment(b.fOperator)) {
350 lvalue = this->getLValue(*b.fLeft);
351 lvalue->load();
352 op = remove_assignment(b.fOperator);
353 } else {
354 this->writeExpression(*b.fLeft);
355 op = b.fOperator;
Brian Osman909231c2019-05-29 15:34:36 -0400356 if (!lVecOrMtx && rVecOrMtx) {
Brian Osman16e6fd52019-05-29 11:19:00 -0400357 for (int i = SlotCount(rType); i > 1; --i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400358 this->write(ByteCodeInstruction::kDup);
359 }
360 }
361 }
362 this->writeExpression(*b.fRight);
Brian Osman909231c2019-05-29 15:34:36 -0400363 if (lVecOrMtx && !rVecOrMtx) {
Brian Osman16e6fd52019-05-29 11:19:00 -0400364 for (int i = SlotCount(lType); i > 1; --i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400365 this->write(ByteCodeInstruction::kDup);
366 }
367 }
Brian Osman909231c2019-05-29 15:34:36 -0400368 // Special case for M*V, V*M, M*M (but not V*V!)
369 if (op == Token::Kind::STAR && lVecOrMtx && rVecOrMtx &&
370 !(lType.kind() == Type::kVector_Kind && rType.kind() == Type::kVector_Kind)) {
371 this->write(ByteCodeInstruction::kMatrixMultiply);
372 int rCols = rType.columns(),
373 rRows = rType.rows(),
374 lCols = lType.columns(),
375 lRows = lType.rows();
376 // M*V treats the vector as a column
377 if (rType.kind() == Type::kVector_Kind) {
378 std::swap(rCols, rRows);
379 }
380 SkASSERT(lCols == rRows);
381 SkASSERT(SlotCount(b.fType) == lRows * rCols);
382 this->write8(lCols);
383 this->write8(lRows);
384 this->write8(rCols);
385 } else {
386 int count = std::max(SlotCount(lType), SlotCount(rType));
387 switch (op) {
388 case Token::Kind::EQEQ:
389 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareIEQ,
390 ByteCodeInstruction::kCompareIEQ,
391 ByteCodeInstruction::kCompareFEQ,
392 count);
393 // Collapse to a single bool
394 for (int i = count; i > 1; --i) {
395 this->write(ByteCodeInstruction::kAndB);
396 }
397 break;
398 case Token::Kind::GT:
399 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSGT,
400 ByteCodeInstruction::kCompareUGT,
401 ByteCodeInstruction::kCompareFGT,
402 count);
403 break;
404 case Token::Kind::GTEQ:
405 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSGTEQ,
406 ByteCodeInstruction::kCompareUGTEQ,
407 ByteCodeInstruction::kCompareFGTEQ,
408 count);
409 break;
410 case Token::Kind::LT:
411 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSLT,
412 ByteCodeInstruction::kCompareULT,
413 ByteCodeInstruction::kCompareFLT,
414 count);
415 break;
416 case Token::Kind::LTEQ:
417 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSLTEQ,
418 ByteCodeInstruction::kCompareULTEQ,
419 ByteCodeInstruction::kCompareFLTEQ,
420 count);
421 break;
422 case Token::Kind::MINUS:
423 this->writeTypedInstruction(lType, ByteCodeInstruction::kSubtractI,
424 ByteCodeInstruction::kSubtractI,
425 ByteCodeInstruction::kSubtractF,
426 count);
427 break;
428 case Token::Kind::NEQ:
429 this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareINEQ,
430 ByteCodeInstruction::kCompareINEQ,
431 ByteCodeInstruction::kCompareFNEQ,
432 count);
433 // Collapse to a single bool
434 for (int i = count; i > 1; --i) {
435 this->write(ByteCodeInstruction::kOrB);
436 }
437 break;
438 case Token::Kind::PERCENT:
439 this->writeTypedInstruction(lType, ByteCodeInstruction::kRemainderS,
440 ByteCodeInstruction::kRemainderU,
441 ByteCodeInstruction::kRemainderF,
442 count);
443 break;
444 case Token::Kind::PLUS:
445 this->writeTypedInstruction(lType, ByteCodeInstruction::kAddI,
446 ByteCodeInstruction::kAddI,
447 ByteCodeInstruction::kAddF,
448 count);
449 break;
450 case Token::Kind::SLASH:
451 this->writeTypedInstruction(lType, ByteCodeInstruction::kDivideS,
452 ByteCodeInstruction::kDivideU,
453 ByteCodeInstruction::kDivideF,
454 count);
455 break;
456 case Token::Kind::STAR:
457 this->writeTypedInstruction(lType, ByteCodeInstruction::kMultiplyI,
458 ByteCodeInstruction::kMultiplyI,
459 ByteCodeInstruction::kMultiplyF,
460 count);
461 break;
462 default:
463 SkASSERT(false);
464 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400465 }
466 if (lvalue) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400467 lvalue->store();
468 }
469}
470
471void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
472 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osman44d44762019-05-13 14:19:12 -0400473 this->write32(b.fValue ? 1 : 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400474}
475
476void ByteCodeGenerator::writeConstructor(const Constructor& c) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400477 for (const auto& arg : c.fArguments) {
478 this->writeExpression(*arg);
479 }
480 if (c.fArguments.size() == 1) {
Brian Osman29e013d2019-05-28 17:16:03 -0400481 const Type& inType = c.fArguments[0]->fType;
482 const Type& outType = c.fType;
483 TypeCategory inCategory = type_category(inType);
484 TypeCategory outCategory = type_category(outType);
485 int inCount = SlotCount(inType);
486 int outCount = SlotCount(outType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400487 if (inCategory != outCategory) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700488 SkASSERT(inCount == outCount);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400489 if (inCategory == TypeCategory::kFloat) {
490 SkASSERT(outCategory == TypeCategory::kSigned ||
491 outCategory == TypeCategory::kUnsigned);
Brian Osmanc51d7912019-05-22 15:16:16 -0700492 this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400493 } else if (outCategory == TypeCategory::kFloat) {
494 if (inCategory == TypeCategory::kSigned) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700495 this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400496 } else {
497 SkASSERT(inCategory == TypeCategory::kUnsigned);
Brian Osmanc51d7912019-05-22 15:16:16 -0700498 this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400499 }
500 } else {
501 SkASSERT(false);
502 }
503 }
Brian Osman29e013d2019-05-28 17:16:03 -0400504 if (inType.kind() == Type::kMatrix_Kind && outType.kind() == Type::kMatrix_Kind) {
505 this->write(ByteCodeInstruction::kMatrixToMatrix);
506 this->write8(inType.columns());
507 this->write8(inType.rows());
508 this->write8(outType.columns());
509 this->write8(outType.rows());
510 } else if (inCount != outCount) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700511 SkASSERT(inCount == 1);
Brian Osman29e013d2019-05-28 17:16:03 -0400512 if (outType.kind() == Type::kMatrix_Kind) {
513 this->write(ByteCodeInstruction::kScalarToMatrix);
514 this->write8(outType.columns());
515 this->write8(outType.rows());
516 } else {
517 SkASSERT(outType.kind() == Type::kVector_Kind);
518 for (; inCount != outCount; ++inCount) {
519 this->write(ByteCodeInstruction::kDup);
520 }
Brian Osmanc51d7912019-05-22 15:16:16 -0700521 }
522 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400523 }
524}
525
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400526void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) {
527 int argumentCount = 0;
528 for (const auto& arg : f.fArguments) {
529 this->writeExpression(*arg);
Brian Osman07c117b2019-05-23 12:51:06 -0700530 argumentCount += SlotCount(arg->fType);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400531 }
532 this->write(ByteCodeInstruction::kCallExternal);
533 SkASSERT(argumentCount <= 255);
534 this->write8(argumentCount);
Brian Osman07c117b2019-05-23 12:51:06 -0700535 this->write8(SlotCount(f.fType));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400536 int index = fOutput->fExternalValues.size();
537 fOutput->fExternalValues.push_back(f.fFunction);
538 SkASSERT(index <= 255);
539 this->write8(index);
540}
541
Ethan Nicholas91164d12019-05-15 15:29:54 -0400542void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400543 this->write(vector_instruction(ByteCodeInstruction::kReadExternal,
Brian Osman07c117b2019-05-23 12:51:06 -0700544 SlotCount(e.fValue->type())));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400545 int index = fOutput->fExternalValues.size();
546 fOutput->fExternalValues.push_back(e.fValue);
547 SkASSERT(index <= 255);
548 this->write8(index);
549}
550
Brian Osman07c117b2019-05-23 12:51:06 -0700551void ByteCodeGenerator::writeVariableExpression(const Expression& expr) {
552 Variable::Storage storage;
553 int location = this->getLocation(expr, &storage);
554 bool isGlobal = storage == Variable::kGlobal_Storage;
555 int count = SlotCount(expr.fType);
556 if (location < 0 || count > 4) {
557 if (location >= 0) {
558 this->write(ByteCodeInstruction::kPushImmediate);
559 this->write32(location);
560 }
561 this->write(isGlobal ? ByteCodeInstruction::kLoadExtendedGlobal
562 : ByteCodeInstruction::kLoadExtended);
563 this->write8(count);
564 } else {
565 this->write(vector_instruction(isGlobal ? ByteCodeInstruction::kLoadGlobal
566 : ByteCodeInstruction::kLoad,
567 count));
568 this->write8(location);
569 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400570}
571
572void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
573 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400574 this->write32(Interpreter::Value((float) f.fValue).fUnsigned);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400575}
576
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400577void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) {
578 auto found = fIntrinsics.find(c.fFunction.fName);
579 if (found == fIntrinsics.end()) {
580 fErrors.error(c.fOffset, "unsupported intrinsic function");
581 return;
582 }
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400583 if (found->second.fIsSpecial) {
584 SkASSERT(found->second.fValue.fSpecial == SpecialIntrinsic::kDot);
585 SkASSERT(c.fArguments.size() == 2);
586 SkASSERT(SlotCount(c.fArguments[0]->fType) == SlotCount(c.fArguments[1]->fType));
587 this->write((ByteCodeInstruction) ((int) ByteCodeInstruction::kMultiplyF +
588 SlotCount(c.fArguments[0]->fType) - 1));
589 for (int i = SlotCount(c.fArguments[0]->fType); i > 1; --i) {
590 this->write(ByteCodeInstruction::kAddF);
591 }
592 } else {
593 switch (found->second.fValue.fInstruction) {
594 case ByteCodeInstruction::kCos:
595 case ByteCodeInstruction::kMix:
596 case ByteCodeInstruction::kSin:
597 case ByteCodeInstruction::kSqrt:
598 case ByteCodeInstruction::kTan:
599 SkASSERT(c.fArguments.size() > 0);
600 this->write((ByteCodeInstruction) ((int) found->second.fValue.fInstruction +
601 SlotCount(c.fArguments[0]->fType) - 1));
602 break;
603 case ByteCodeInstruction::kCross:
604 this->write(found->second.fValue.fInstruction);
605 break;
606 default:
607 SkASSERT(false);
608 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400609 }
610}
611
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400612void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) {
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400613 for (const auto& arg : f.fArguments) {
614 this->writeExpression(*arg);
615 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400616 if (f.fFunction.fBuiltin) {
617 this->writeIntrinsicCall(f);
618 return;
619 }
Brian Osman226668a2019-05-14 16:47:30 -0400620 this->write(ByteCodeInstruction::kCall);
621 fCallTargets.emplace_back(this, f.fFunction);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400622}
623
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400624void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) {
625 this->write(ByteCodeInstruction::kPushImmediate);
626 this->write32(i.fValue);
627}
628
629void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) {
630 // not yet implemented
631 abort();
632}
633
634void ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p) {
635 switch (p.fOperator) {
636 case Token::Kind::PLUSPLUS: // fall through
637 case Token::Kind::MINUSMINUS: {
Brian Osman07c117b2019-05-23 12:51:06 -0700638 SkASSERT(SlotCount(p.fOperand->fType) == 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400639 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
640 lvalue->load();
641 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400642 this->write32(type_category(p.fType) == TypeCategory::kFloat
643 ? Interpreter::Value(1.0f).fUnsigned : 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400644 if (p.fOperator == Token::Kind::PLUSPLUS) {
645 this->writeTypedInstruction(p.fType,
646 ByteCodeInstruction::kAddI,
647 ByteCodeInstruction::kAddI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400648 ByteCodeInstruction::kAddF,
649 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400650 } else {
651 this->writeTypedInstruction(p.fType,
652 ByteCodeInstruction::kSubtractI,
653 ByteCodeInstruction::kSubtractI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400654 ByteCodeInstruction::kSubtractF,
655 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400656 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400657 lvalue->store();
658 break;
659 }
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400660 case Token::Kind::MINUS: {
661 this->writeExpression(*p.fOperand);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400662 this->writeTypedInstruction(p.fType,
Mike Klein12710912019-05-21 11:04:59 -0500663 ByteCodeInstruction::kNegateI,
664 ByteCodeInstruction::kNegateI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400665 ByteCodeInstruction::kNegateF,
Brian Osman07c117b2019-05-23 12:51:06 -0700666 SlotCount(p.fOperand->fType));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400667 break;
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400668 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400669 default:
670 SkASSERT(false);
671 }
672}
673
674void ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p) {
Brian Osmanf3fa6002019-05-17 14:26:53 -0400675 switch (p.fOperator) {
676 case Token::Kind::PLUSPLUS: // fall through
677 case Token::Kind::MINUSMINUS: {
Brian Osman07c117b2019-05-23 12:51:06 -0700678 SkASSERT(SlotCount(p.fOperand->fType) == 1);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400679 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
680 lvalue->load();
681 this->write(ByteCodeInstruction::kDup);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400682 this->write(ByteCodeInstruction::kPushImmediate);
683 this->write32(type_category(p.fType) == TypeCategory::kFloat
684 ? Interpreter::Value(1.0f).fUnsigned : 1);
685 if (p.fOperator == Token::Kind::PLUSPLUS) {
686 this->writeTypedInstruction(p.fType,
687 ByteCodeInstruction::kAddI,
688 ByteCodeInstruction::kAddI,
689 ByteCodeInstruction::kAddF,
690 1);
691 } else {
692 this->writeTypedInstruction(p.fType,
693 ByteCodeInstruction::kSubtractI,
694 ByteCodeInstruction::kSubtractI,
695 ByteCodeInstruction::kSubtractF,
696 1);
697 }
698 lvalue->store();
699 this->write(ByteCodeInstruction::kPop);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400700 break;
701 }
702 default:
703 SkASSERT(false);
704 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400705}
706
707void ByteCodeGenerator::writeSwizzle(const Swizzle& s) {
Brian Osman0785db02019-05-24 14:19:11 -0400708 if (swizzle_is_simple(s)) {
709 this->writeVariableExpression(s);
710 return;
711 }
712
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400713 switch (s.fBase->fKind) {
714 case Expression::kVariableReference_Kind: {
715 const Variable& var = ((VariableReference&) *s.fBase).fVariable;
Brian Osman1091f022019-05-16 09:42:16 -0400716 this->write(var.fStorage == Variable::kGlobal_Storage
717 ? ByteCodeInstruction::kLoadSwizzleGlobal
718 : ByteCodeInstruction::kLoadSwizzle);
719 this->write8(this->getLocation(var));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400720 this->write8(s.fComponents.size());
721 for (int c : s.fComponents) {
722 this->write8(c);
723 }
724 break;
725 }
726 default:
727 this->writeExpression(*s.fBase);
728 this->write(ByteCodeInstruction::kSwizzle);
729 this->write8(s.fBase->fType.columns());
730 this->write8(s.fComponents.size());
731 for (int c : s.fComponents) {
732 this->write8(c);
733 }
734 }
735}
736
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400737void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) {
Brian Osman4e93feb2019-05-16 15:38:00 -0400738 this->writeExpression(*t.fTest);
Brian Osman4e93feb2019-05-16 15:38:00 -0400739 this->write(ByteCodeInstruction::kConditionalBranch);
740 DeferredLocation trueLocation(this);
741 this->writeExpression(*t.fIfFalse);
Brian Osman4e93feb2019-05-16 15:38:00 -0400742 this->write(ByteCodeInstruction::kBranch);
743 DeferredLocation endLocation(this);
744 trueLocation.set();
745 this->writeExpression(*t.fIfTrue);
746 endLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400747}
748
749void ByteCodeGenerator::writeExpression(const Expression& e) {
750 switch (e.fKind) {
751 case Expression::kBinary_Kind:
752 this->writeBinaryExpression((BinaryExpression&) e);
753 break;
754 case Expression::kBoolLiteral_Kind:
755 this->writeBoolLiteral((BoolLiteral&) e);
756 break;
757 case Expression::kConstructor_Kind:
758 this->writeConstructor((Constructor&) e);
759 break;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400760 case Expression::kExternalFunctionCall_Kind:
761 this->writeExternalFunctionCall((ExternalFunctionCall&) e);
762 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400763 case Expression::kExternalValue_Kind:
764 this->writeExternalValue((ExternalValueReference&) e);
765 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400766 case Expression::kFieldAccess_Kind:
Brian Osman07c117b2019-05-23 12:51:06 -0700767 case Expression::kIndex_Kind:
768 case Expression::kVariableReference_Kind:
769 this->writeVariableExpression(e);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400770 break;
771 case Expression::kFloatLiteral_Kind:
772 this->writeFloatLiteral((FloatLiteral&) e);
773 break;
774 case Expression::kFunctionCall_Kind:
775 this->writeFunctionCall((FunctionCall&) e);
776 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400777 case Expression::kIntLiteral_Kind:
778 this->writeIntLiteral((IntLiteral&) e);
779 break;
780 case Expression::kNullLiteral_Kind:
781 this->writeNullLiteral((NullLiteral&) e);
782 break;
783 case Expression::kPrefix_Kind:
784 this->writePrefixExpression((PrefixExpression&) e);
785 break;
786 case Expression::kPostfix_Kind:
787 this->writePostfixExpression((PostfixExpression&) e);
788 break;
789 case Expression::kSwizzle_Kind:
790 this->writeSwizzle((Swizzle&) e);
791 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400792 case Expression::kTernary_Kind:
793 this->writeTernaryExpression((TernaryExpression&) e);
794 break;
795 default:
796 printf("unsupported expression %s\n", e.description().c_str());
797 SkASSERT(false);
798 }
799}
800
Ethan Nicholas91164d12019-05-15 15:29:54 -0400801class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue {
802public:
803 ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index)
804 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700805 , fCount(ByteCodeGenerator::SlotCount(value.type()))
Ethan Nicholas91164d12019-05-15 15:29:54 -0400806 , fIndex(index) {}
807
808 void load() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400809 fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400810 fGenerator.write8(fIndex);
811 }
812
813 void store() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400814 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount));
815 fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400816 fGenerator.write8(fIndex);
817 }
818
819private:
820 typedef LValue INHERITED;
821
822 int fCount;
823
824 int fIndex;
825};
826
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400827class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue {
828public:
829 ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle)
830 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700831 , fSwizzle(swizzle) {}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400832
833 void load() override {
Brian Osman1091f022019-05-16 09:42:16 -0400834 fGenerator.writeSwizzle(fSwizzle);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400835 }
836
837 void store() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400838 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup,
839 fSwizzle.fComponents.size()));
Brian Osman07c117b2019-05-23 12:51:06 -0700840 Variable::Storage storage;
841 int location = fGenerator.getLocation(*fSwizzle.fBase, &storage);
842 bool isGlobal = storage == Variable::kGlobal_Storage;
843 if (location < 0) {
844 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleIndirectGlobal
845 : ByteCodeInstruction::kStoreSwizzleIndirect);
846 } else {
847 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleGlobal
848 : ByteCodeInstruction::kStoreSwizzle);
849 fGenerator.write8(location);
850 }
Brian Osman1091f022019-05-16 09:42:16 -0400851 fGenerator.write8(fSwizzle.fComponents.size());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400852 for (int c : fSwizzle.fComponents) {
853 fGenerator.write8(c);
854 }
855 }
856
857private:
858 const Swizzle& fSwizzle;
859
860 typedef LValue INHERITED;
861};
862
Brian Osman07c117b2019-05-23 12:51:06 -0700863class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400864public:
Brian Osman07c117b2019-05-23 12:51:06 -0700865 ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400866 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700867 , fExpression(expr) {}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400868
869 void load() override {
Brian Osman07c117b2019-05-23 12:51:06 -0700870 fGenerator.writeVariableExpression(fExpression);
Brian Osman1091f022019-05-16 09:42:16 -0400871 }
872
873 void store() override {
Brian Osman07c117b2019-05-23 12:51:06 -0700874 int count = ByteCodeGenerator::SlotCount(fExpression.fType);
875 if (count > 4) {
876 fGenerator.write(ByteCodeInstruction::kDupN);
877 fGenerator.write8(count);
878 } else {
879 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count));
880 }
881 Variable::Storage storage;
882 int location = fGenerator.getLocation(fExpression, &storage);
883 bool isGlobal = storage == Variable::kGlobal_Storage;
884 if (location < 0 || count > 4) {
885 if (location >= 0) {
886 fGenerator.write(ByteCodeInstruction::kPushImmediate);
887 fGenerator.write32(location);
888 }
889 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreExtendedGlobal
890 : ByteCodeInstruction::kStoreExtended);
891 fGenerator.write8(count);
892 } else {
893 fGenerator.write(vector_instruction(isGlobal ? ByteCodeInstruction::kStoreGlobal
894 : ByteCodeInstruction::kStore,
895 count));
896 fGenerator.write8(location);
897 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400898 }
899
900private:
901 typedef LValue INHERITED;
902
Brian Osman07c117b2019-05-23 12:51:06 -0700903 const Expression& fExpression;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400904};
905
906std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) {
907 switch (e.fKind) {
Ethan Nicholas91164d12019-05-15 15:29:54 -0400908 case Expression::kExternalValue_Kind: {
909 ExternalValue* value = ((ExternalValueReference&) e).fValue;
910 int index = fOutput->fExternalValues.size();
911 fOutput->fExternalValues.push_back(value);
912 SkASSERT(index <= 255);
913 return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index));
914 }
Brian Osman07c117b2019-05-23 12:51:06 -0700915 case Expression::kFieldAccess_Kind:
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400916 case Expression::kIndex_Kind:
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400917 case Expression::kVariableReference_Kind:
Brian Osman07c117b2019-05-23 12:51:06 -0700918 return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e));
Brian Osman0785db02019-05-24 14:19:11 -0400919 case Expression::kSwizzle_Kind: {
920 const Swizzle& s = (const Swizzle&) e;
921 return swizzle_is_simple(s)
922 ? std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e))
923 : std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, s));
924 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400925 case Expression::kTernary_Kind:
926 default:
927 printf("unsupported lvalue %s\n", e.description().c_str());
928 return nullptr;
929 }
930}
931
932void ByteCodeGenerator::writeBlock(const Block& b) {
933 for (const auto& s : b.fStatements) {
934 this->writeStatement(*s);
935 }
936}
937
938void ByteCodeGenerator::setBreakTargets() {
939 std::vector<DeferredLocation>& breaks = fBreakTargets.top();
940 for (DeferredLocation& b : breaks) {
941 b.set();
942 }
943 fBreakTargets.pop();
944}
945
946void ByteCodeGenerator::setContinueTargets() {
947 std::vector<DeferredLocation>& continues = fContinueTargets.top();
948 for (DeferredLocation& c : continues) {
949 c.set();
950 }
951 fContinueTargets.pop();
952}
953
954void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) {
955 this->write(ByteCodeInstruction::kBranch);
956 fBreakTargets.top().emplace_back(this);
957}
958
959void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) {
960 this->write(ByteCodeInstruction::kBranch);
961 fContinueTargets.top().emplace_back(this);
962}
963
964void ByteCodeGenerator::writeDoStatement(const DoStatement& d) {
965 fContinueTargets.emplace();
966 fBreakTargets.emplace();
967 size_t start = fCode->size();
968 this->writeStatement(*d.fStatement);
969 this->setContinueTargets();
970 this->writeExpression(*d.fTest);
971 this->write(ByteCodeInstruction::kConditionalBranch);
972 this->write16(start);
973 this->setBreakTargets();
974}
975
976void ByteCodeGenerator::writeForStatement(const ForStatement& f) {
977 fContinueTargets.emplace();
978 fBreakTargets.emplace();
979 if (f.fInitializer) {
980 this->writeStatement(*f.fInitializer);
981 }
982 size_t start = fCode->size();
983 if (f.fTest) {
984 this->writeExpression(*f.fTest);
985 this->write(ByteCodeInstruction::kNot);
986 this->write(ByteCodeInstruction::kConditionalBranch);
987 DeferredLocation endLocation(this);
988 this->writeStatement(*f.fStatement);
989 this->setContinueTargets();
990 if (f.fNext) {
991 this->writeExpression(*f.fNext);
Brian Osman07c117b2019-05-23 12:51:06 -0700992 this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType)));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400993 }
994 this->write(ByteCodeInstruction::kBranch);
995 this->write16(start);
996 endLocation.set();
997 } else {
998 this->writeStatement(*f.fStatement);
999 this->setContinueTargets();
1000 if (f.fNext) {
1001 this->writeExpression(*f.fNext);
Brian Osman07c117b2019-05-23 12:51:06 -07001002 this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType)));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001003 }
1004 this->write(ByteCodeInstruction::kBranch);
1005 this->write16(start);
1006 }
1007 this->setBreakTargets();
1008}
1009
1010void ByteCodeGenerator::writeIfStatement(const IfStatement& i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001011 if (i.fIfFalse) {
Mike Kleinb45ee832019-05-17 11:11:11 -05001012 // if (test) { ..ifTrue.. } else { .. ifFalse .. }
1013 this->writeExpression(*i.fTest);
Mike Kleinb45ee832019-05-17 11:11:11 -05001014 this->write(ByteCodeInstruction::kConditionalBranch);
1015 DeferredLocation trueLocation(this);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001016 this->writeStatement(*i.fIfFalse);
Mike Kleinb45ee832019-05-17 11:11:11 -05001017 this->write(ByteCodeInstruction::kBranch);
1018 DeferredLocation endLocation(this);
1019 trueLocation.set();
1020 this->writeStatement(*i.fIfTrue);
1021 endLocation.set();
1022 } else {
1023 // if (test) { ..ifTrue.. }
1024 this->writeExpression(*i.fTest);
1025 this->write(ByteCodeInstruction::kNot);
Mike Kleinb45ee832019-05-17 11:11:11 -05001026 this->write(ByteCodeInstruction::kConditionalBranch);
1027 DeferredLocation endLocation(this);
1028 this->writeStatement(*i.fIfTrue);
1029 endLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001030 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001031}
1032
1033void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
Ethan Nicholas746035a2019-04-23 13:31:09 -04001034 this->writeExpression(*r.fExpression);
1035 this->write(ByteCodeInstruction::kReturn);
Brian Osman07c117b2019-05-23 12:51:06 -07001036 this->write8(SlotCount(r.fExpression->fType));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001037}
1038
1039void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) {
1040 // not yet implemented
1041 abort();
1042}
1043
1044void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) {
1045 for (const auto& declStatement : v.fVars) {
1046 const VarDeclaration& decl = (VarDeclaration&) *declStatement;
1047 // we need to grab the location even if we don't use it, to ensure it
1048 // has been allocated
1049 int location = getLocation(*decl.fVar);
1050 if (decl.fValue) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001051 this->writeExpression(*decl.fValue);
Brian Osman07c117b2019-05-23 12:51:06 -07001052 int count = SlotCount(decl.fValue->fType);
1053 if (count > 4) {
1054 this->write(ByteCodeInstruction::kPushImmediate);
1055 this->write32(location);
1056 this->write(ByteCodeInstruction::kStoreExtended);
1057 this->write8(count);
1058 } else {
1059 this->write(vector_instruction(ByteCodeInstruction::kStore, count));
1060 this->write8(location);
1061 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001062 }
1063 }
1064}
1065
1066void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) {
1067 fContinueTargets.emplace();
1068 fBreakTargets.emplace();
1069 size_t start = fCode->size();
1070 this->writeExpression(*w.fTest);
1071 this->write(ByteCodeInstruction::kNot);
1072 this->write(ByteCodeInstruction::kConditionalBranch);
1073 DeferredLocation endLocation(this);
1074 this->writeStatement(*w.fStatement);
1075 this->setContinueTargets();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001076 this->write(ByteCodeInstruction::kBranch);
1077 this->write16(start);
1078 endLocation.set();
1079 this->setBreakTargets();
1080}
1081
1082void ByteCodeGenerator::writeStatement(const Statement& s) {
1083 switch (s.fKind) {
1084 case Statement::kBlock_Kind:
1085 this->writeBlock((Block&) s);
1086 break;
1087 case Statement::kBreak_Kind:
1088 this->writeBreakStatement((BreakStatement&) s);
1089 break;
1090 case Statement::kContinue_Kind:
1091 this->writeContinueStatement((ContinueStatement&) s);
1092 break;
1093 case Statement::kDiscard_Kind:
1094 // not yet implemented
1095 abort();
1096 case Statement::kDo_Kind:
1097 this->writeDoStatement((DoStatement&) s);
1098 break;
1099 case Statement::kExpression_Kind: {
1100 const Expression& expr = *((ExpressionStatement&) s).fExpression;
1101 this->writeExpression(expr);
Brian Osman07c117b2019-05-23 12:51:06 -07001102 int count = SlotCount(expr.fType);
1103 if (count > 4) {
1104 this->write(ByteCodeInstruction::kPopN);
1105 this->write8(count);
1106 } else {
1107 this->write(vector_instruction(ByteCodeInstruction::kPop, count));
1108 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001109 break;
1110 }
1111 case Statement::kFor_Kind:
1112 this->writeForStatement((ForStatement&) s);
1113 break;
1114 case Statement::kIf_Kind:
1115 this->writeIfStatement((IfStatement&) s);
1116 break;
1117 case Statement::kNop_Kind:
1118 break;
1119 case Statement::kReturn_Kind:
1120 this->writeReturnStatement((ReturnStatement&) s);
1121 break;
1122 case Statement::kSwitch_Kind:
1123 this->writeSwitchStatement((SwitchStatement&) s);
1124 break;
1125 case Statement::kVarDeclarations_Kind:
1126 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration);
1127 break;
1128 case Statement::kWhile_Kind:
1129 this->writeWhileStatement((WhileStatement&) s);
1130 break;
1131 default:
1132 SkASSERT(false);
1133 }
1134}
1135
1136}