blob: 28aa1cd387405b1d9b26632eb335aa24e235e982 [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;
322 case TypeCategory::kFloat:
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400323 this->write(vector_instruction(f, count));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400324 break;
325 default:
326 SkASSERT(false);
327 }
328}
329
330void ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b) {
331 if (b.fOperator == Token::Kind::EQ) {
332 std::unique_ptr<LValue> lvalue = this->getLValue(*b.fLeft);
333 this->writeExpression(*b.fRight);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400334 lvalue->store();
335 return;
336 }
337 Token::Kind op;
338 std::unique_ptr<LValue> lvalue;
339 if (is_assignment(b.fOperator)) {
340 lvalue = this->getLValue(*b.fLeft);
341 lvalue->load();
342 op = remove_assignment(b.fOperator);
343 } else {
344 this->writeExpression(*b.fLeft);
345 op = b.fOperator;
346 if (b.fLeft->fType.kind() == Type::kScalar_Kind &&
347 b.fRight->fType.kind() == Type::kVector_Kind) {
348 for (int i = b.fRight->fType.columns(); i > 1; --i) {
349 this->write(ByteCodeInstruction::kDup);
350 }
351 }
352 }
353 this->writeExpression(*b.fRight);
354 if (b.fLeft->fType.kind() == Type::kVector_Kind &&
355 b.fRight->fType.kind() == Type::kScalar_Kind) {
356 for (int i = b.fLeft->fType.columns(); i > 1; --i) {
357 this->write(ByteCodeInstruction::kDup);
358 }
359 }
Brian Osman07c117b2019-05-23 12:51:06 -0700360 int count = SlotCount(b.fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400361 switch (op) {
362 case Token::Kind::EQEQ:
363 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareIEQ,
364 ByteCodeInstruction::kCompareIEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400365 ByteCodeInstruction::kCompareFEQ,
366 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400367 break;
368 case Token::Kind::GT:
369 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGT,
370 ByteCodeInstruction::kCompareUGT,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400371 ByteCodeInstruction::kCompareFGT,
372 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400373 break;
374 case Token::Kind::GTEQ:
375 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGTEQ,
376 ByteCodeInstruction::kCompareUGTEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400377 ByteCodeInstruction::kCompareFGTEQ,
378 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400379 break;
380 case Token::Kind::LT:
381 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLT,
382 ByteCodeInstruction::kCompareULT,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400383 ByteCodeInstruction::kCompareFLT,
384 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400385 break;
386 case Token::Kind::LTEQ:
387 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLTEQ,
388 ByteCodeInstruction::kCompareULTEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400389 ByteCodeInstruction::kCompareFLTEQ,
390 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400391 break;
392 case Token::Kind::MINUS:
393 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kSubtractI,
394 ByteCodeInstruction::kSubtractI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400395 ByteCodeInstruction::kSubtractF,
396 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400397 break;
398 case Token::Kind::NEQ:
399 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareINEQ,
400 ByteCodeInstruction::kCompareINEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400401 ByteCodeInstruction::kCompareFNEQ,
402 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400403 break;
404 case Token::Kind::PERCENT:
405 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kRemainderS,
406 ByteCodeInstruction::kRemainderU,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400407 ByteCodeInstruction::kRemainderF,
408 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400409 break;
410 case Token::Kind::PLUS:
411 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kAddI,
412 ByteCodeInstruction::kAddI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400413 ByteCodeInstruction::kAddF,
414 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400415 break;
416 case Token::Kind::SLASH:
417 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kDivideS,
418 ByteCodeInstruction::kDivideU,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400419 ByteCodeInstruction::kDivideF,
420 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400421 break;
422 case Token::Kind::STAR:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400423 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kMultiplyI,
424 ByteCodeInstruction::kMultiplyI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400425 ByteCodeInstruction::kMultiplyF,
426 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400427 break;
428 default:
429 SkASSERT(false);
430 }
431 if (lvalue) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400432 lvalue->store();
433 }
434}
435
436void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
437 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osman44d44762019-05-13 14:19:12 -0400438 this->write32(b.fValue ? 1 : 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400439}
440
441void ByteCodeGenerator::writeConstructor(const Constructor& c) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400442 for (const auto& arg : c.fArguments) {
443 this->writeExpression(*arg);
444 }
445 if (c.fArguments.size() == 1) {
446 TypeCategory inCategory = type_category(c.fArguments[0]->fType);
447 TypeCategory outCategory = type_category(c.fType);
Brian Osmanc51d7912019-05-22 15:16:16 -0700448 int inCount = c.fArguments[0]->fType.columns();
449 int outCount = c.fType.columns();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400450 if (inCategory != outCategory) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700451 SkASSERT(inCount == outCount);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400452 if (inCategory == TypeCategory::kFloat) {
453 SkASSERT(outCategory == TypeCategory::kSigned ||
454 outCategory == TypeCategory::kUnsigned);
Brian Osmanc51d7912019-05-22 15:16:16 -0700455 this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400456 } else if (outCategory == TypeCategory::kFloat) {
457 if (inCategory == TypeCategory::kSigned) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700458 this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400459 } else {
460 SkASSERT(inCategory == TypeCategory::kUnsigned);
Brian Osmanc51d7912019-05-22 15:16:16 -0700461 this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400462 }
463 } else {
464 SkASSERT(false);
465 }
466 }
Brian Osmanc51d7912019-05-22 15:16:16 -0700467 if (inCount != outCount) {
468 SkASSERT(inCount == 1);
469 for (; inCount != outCount; ++inCount) {
470 this->write(ByteCodeInstruction::kDup);
471 }
472 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400473 }
474}
475
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400476void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) {
477 int argumentCount = 0;
478 for (const auto& arg : f.fArguments) {
479 this->writeExpression(*arg);
Brian Osman07c117b2019-05-23 12:51:06 -0700480 argumentCount += SlotCount(arg->fType);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400481 }
482 this->write(ByteCodeInstruction::kCallExternal);
483 SkASSERT(argumentCount <= 255);
484 this->write8(argumentCount);
Brian Osman07c117b2019-05-23 12:51:06 -0700485 this->write8(SlotCount(f.fType));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400486 int index = fOutput->fExternalValues.size();
487 fOutput->fExternalValues.push_back(f.fFunction);
488 SkASSERT(index <= 255);
489 this->write8(index);
490}
491
Ethan Nicholas91164d12019-05-15 15:29:54 -0400492void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400493 this->write(vector_instruction(ByteCodeInstruction::kReadExternal,
Brian Osman07c117b2019-05-23 12:51:06 -0700494 SlotCount(e.fValue->type())));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400495 int index = fOutput->fExternalValues.size();
496 fOutput->fExternalValues.push_back(e.fValue);
497 SkASSERT(index <= 255);
498 this->write8(index);
499}
500
Brian Osman07c117b2019-05-23 12:51:06 -0700501void ByteCodeGenerator::writeVariableExpression(const Expression& expr) {
502 Variable::Storage storage;
503 int location = this->getLocation(expr, &storage);
504 bool isGlobal = storage == Variable::kGlobal_Storage;
505 int count = SlotCount(expr.fType);
506 if (location < 0 || count > 4) {
507 if (location >= 0) {
508 this->write(ByteCodeInstruction::kPushImmediate);
509 this->write32(location);
510 }
511 this->write(isGlobal ? ByteCodeInstruction::kLoadExtendedGlobal
512 : ByteCodeInstruction::kLoadExtended);
513 this->write8(count);
514 } else {
515 this->write(vector_instruction(isGlobal ? ByteCodeInstruction::kLoadGlobal
516 : ByteCodeInstruction::kLoad,
517 count));
518 this->write8(location);
519 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400520}
521
522void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
523 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400524 this->write32(Interpreter::Value((float) f.fValue).fUnsigned);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400525}
526
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400527void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) {
528 auto found = fIntrinsics.find(c.fFunction.fName);
529 if (found == fIntrinsics.end()) {
530 fErrors.error(c.fOffset, "unsupported intrinsic function");
531 return;
532 }
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400533 if (found->second.fIsSpecial) {
534 SkASSERT(found->second.fValue.fSpecial == SpecialIntrinsic::kDot);
535 SkASSERT(c.fArguments.size() == 2);
536 SkASSERT(SlotCount(c.fArguments[0]->fType) == SlotCount(c.fArguments[1]->fType));
537 this->write((ByteCodeInstruction) ((int) ByteCodeInstruction::kMultiplyF +
538 SlotCount(c.fArguments[0]->fType) - 1));
539 for (int i = SlotCount(c.fArguments[0]->fType); i > 1; --i) {
540 this->write(ByteCodeInstruction::kAddF);
541 }
542 } else {
543 switch (found->second.fValue.fInstruction) {
544 case ByteCodeInstruction::kCos:
545 case ByteCodeInstruction::kMix:
546 case ByteCodeInstruction::kSin:
547 case ByteCodeInstruction::kSqrt:
548 case ByteCodeInstruction::kTan:
549 SkASSERT(c.fArguments.size() > 0);
550 this->write((ByteCodeInstruction) ((int) found->second.fValue.fInstruction +
551 SlotCount(c.fArguments[0]->fType) - 1));
552 break;
553 case ByteCodeInstruction::kCross:
554 this->write(found->second.fValue.fInstruction);
555 break;
556 default:
557 SkASSERT(false);
558 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400559 }
560}
561
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400562void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) {
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400563 for (const auto& arg : f.fArguments) {
564 this->writeExpression(*arg);
565 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400566 if (f.fFunction.fBuiltin) {
567 this->writeIntrinsicCall(f);
568 return;
569 }
Brian Osman226668a2019-05-14 16:47:30 -0400570 this->write(ByteCodeInstruction::kCall);
571 fCallTargets.emplace_back(this, f.fFunction);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400572}
573
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400574void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) {
575 this->write(ByteCodeInstruction::kPushImmediate);
576 this->write32(i.fValue);
577}
578
579void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) {
580 // not yet implemented
581 abort();
582}
583
584void ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p) {
585 switch (p.fOperator) {
586 case Token::Kind::PLUSPLUS: // fall through
587 case Token::Kind::MINUSMINUS: {
Brian Osman07c117b2019-05-23 12:51:06 -0700588 SkASSERT(SlotCount(p.fOperand->fType) == 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400589 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
590 lvalue->load();
591 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400592 this->write32(type_category(p.fType) == TypeCategory::kFloat
593 ? Interpreter::Value(1.0f).fUnsigned : 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400594 if (p.fOperator == Token::Kind::PLUSPLUS) {
595 this->writeTypedInstruction(p.fType,
596 ByteCodeInstruction::kAddI,
597 ByteCodeInstruction::kAddI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400598 ByteCodeInstruction::kAddF,
599 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400600 } else {
601 this->writeTypedInstruction(p.fType,
602 ByteCodeInstruction::kSubtractI,
603 ByteCodeInstruction::kSubtractI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400604 ByteCodeInstruction::kSubtractF,
605 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400606 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400607 lvalue->store();
608 break;
609 }
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400610 case Token::Kind::MINUS: {
611 this->writeExpression(*p.fOperand);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400612 this->writeTypedInstruction(p.fType,
Mike Klein12710912019-05-21 11:04:59 -0500613 ByteCodeInstruction::kNegateI,
614 ByteCodeInstruction::kNegateI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400615 ByteCodeInstruction::kNegateF,
Brian Osman07c117b2019-05-23 12:51:06 -0700616 SlotCount(p.fOperand->fType));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400617 break;
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400618 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400619 default:
620 SkASSERT(false);
621 }
622}
623
624void ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p) {
Brian Osmanf3fa6002019-05-17 14:26:53 -0400625 switch (p.fOperator) {
626 case Token::Kind::PLUSPLUS: // fall through
627 case Token::Kind::MINUSMINUS: {
Brian Osman07c117b2019-05-23 12:51:06 -0700628 SkASSERT(SlotCount(p.fOperand->fType) == 1);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400629 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
630 lvalue->load();
631 this->write(ByteCodeInstruction::kDup);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400632 this->write(ByteCodeInstruction::kPushImmediate);
633 this->write32(type_category(p.fType) == TypeCategory::kFloat
634 ? Interpreter::Value(1.0f).fUnsigned : 1);
635 if (p.fOperator == Token::Kind::PLUSPLUS) {
636 this->writeTypedInstruction(p.fType,
637 ByteCodeInstruction::kAddI,
638 ByteCodeInstruction::kAddI,
639 ByteCodeInstruction::kAddF,
640 1);
641 } else {
642 this->writeTypedInstruction(p.fType,
643 ByteCodeInstruction::kSubtractI,
644 ByteCodeInstruction::kSubtractI,
645 ByteCodeInstruction::kSubtractF,
646 1);
647 }
648 lvalue->store();
649 this->write(ByteCodeInstruction::kPop);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400650 break;
651 }
652 default:
653 SkASSERT(false);
654 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400655}
656
657void ByteCodeGenerator::writeSwizzle(const Swizzle& s) {
Brian Osman0785db02019-05-24 14:19:11 -0400658 if (swizzle_is_simple(s)) {
659 this->writeVariableExpression(s);
660 return;
661 }
662
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400663 switch (s.fBase->fKind) {
664 case Expression::kVariableReference_Kind: {
665 const Variable& var = ((VariableReference&) *s.fBase).fVariable;
Brian Osman1091f022019-05-16 09:42:16 -0400666 this->write(var.fStorage == Variable::kGlobal_Storage
667 ? ByteCodeInstruction::kLoadSwizzleGlobal
668 : ByteCodeInstruction::kLoadSwizzle);
669 this->write8(this->getLocation(var));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400670 this->write8(s.fComponents.size());
671 for (int c : s.fComponents) {
672 this->write8(c);
673 }
674 break;
675 }
676 default:
677 this->writeExpression(*s.fBase);
678 this->write(ByteCodeInstruction::kSwizzle);
679 this->write8(s.fBase->fType.columns());
680 this->write8(s.fComponents.size());
681 for (int c : s.fComponents) {
682 this->write8(c);
683 }
684 }
685}
686
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400687void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) {
Brian Osman4e93feb2019-05-16 15:38:00 -0400688 this->writeExpression(*t.fTest);
Brian Osman4e93feb2019-05-16 15:38:00 -0400689 this->write(ByteCodeInstruction::kConditionalBranch);
690 DeferredLocation trueLocation(this);
691 this->writeExpression(*t.fIfFalse);
Brian Osman4e93feb2019-05-16 15:38:00 -0400692 this->write(ByteCodeInstruction::kBranch);
693 DeferredLocation endLocation(this);
694 trueLocation.set();
695 this->writeExpression(*t.fIfTrue);
696 endLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400697}
698
699void ByteCodeGenerator::writeExpression(const Expression& e) {
700 switch (e.fKind) {
701 case Expression::kBinary_Kind:
702 this->writeBinaryExpression((BinaryExpression&) e);
703 break;
704 case Expression::kBoolLiteral_Kind:
705 this->writeBoolLiteral((BoolLiteral&) e);
706 break;
707 case Expression::kConstructor_Kind:
708 this->writeConstructor((Constructor&) e);
709 break;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400710 case Expression::kExternalFunctionCall_Kind:
711 this->writeExternalFunctionCall((ExternalFunctionCall&) e);
712 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400713 case Expression::kExternalValue_Kind:
714 this->writeExternalValue((ExternalValueReference&) e);
715 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400716 case Expression::kFieldAccess_Kind:
Brian Osman07c117b2019-05-23 12:51:06 -0700717 case Expression::kIndex_Kind:
718 case Expression::kVariableReference_Kind:
719 this->writeVariableExpression(e);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400720 break;
721 case Expression::kFloatLiteral_Kind:
722 this->writeFloatLiteral((FloatLiteral&) e);
723 break;
724 case Expression::kFunctionCall_Kind:
725 this->writeFunctionCall((FunctionCall&) e);
726 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400727 case Expression::kIntLiteral_Kind:
728 this->writeIntLiteral((IntLiteral&) e);
729 break;
730 case Expression::kNullLiteral_Kind:
731 this->writeNullLiteral((NullLiteral&) e);
732 break;
733 case Expression::kPrefix_Kind:
734 this->writePrefixExpression((PrefixExpression&) e);
735 break;
736 case Expression::kPostfix_Kind:
737 this->writePostfixExpression((PostfixExpression&) e);
738 break;
739 case Expression::kSwizzle_Kind:
740 this->writeSwizzle((Swizzle&) e);
741 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400742 case Expression::kTernary_Kind:
743 this->writeTernaryExpression((TernaryExpression&) e);
744 break;
745 default:
746 printf("unsupported expression %s\n", e.description().c_str());
747 SkASSERT(false);
748 }
749}
750
Ethan Nicholas91164d12019-05-15 15:29:54 -0400751class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue {
752public:
753 ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index)
754 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700755 , fCount(ByteCodeGenerator::SlotCount(value.type()))
Ethan Nicholas91164d12019-05-15 15:29:54 -0400756 , fIndex(index) {}
757
758 void load() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400759 fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400760 fGenerator.write8(fIndex);
761 }
762
763 void store() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400764 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount));
765 fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400766 fGenerator.write8(fIndex);
767 }
768
769private:
770 typedef LValue INHERITED;
771
772 int fCount;
773
774 int fIndex;
775};
776
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400777class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue {
778public:
779 ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle)
780 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700781 , fSwizzle(swizzle) {}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400782
783 void load() override {
Brian Osman1091f022019-05-16 09:42:16 -0400784 fGenerator.writeSwizzle(fSwizzle);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400785 }
786
787 void store() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400788 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup,
789 fSwizzle.fComponents.size()));
Brian Osman07c117b2019-05-23 12:51:06 -0700790 Variable::Storage storage;
791 int location = fGenerator.getLocation(*fSwizzle.fBase, &storage);
792 bool isGlobal = storage == Variable::kGlobal_Storage;
793 if (location < 0) {
794 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleIndirectGlobal
795 : ByteCodeInstruction::kStoreSwizzleIndirect);
796 } else {
797 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleGlobal
798 : ByteCodeInstruction::kStoreSwizzle);
799 fGenerator.write8(location);
800 }
Brian Osman1091f022019-05-16 09:42:16 -0400801 fGenerator.write8(fSwizzle.fComponents.size());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400802 for (int c : fSwizzle.fComponents) {
803 fGenerator.write8(c);
804 }
805 }
806
807private:
808 const Swizzle& fSwizzle;
809
810 typedef LValue INHERITED;
811};
812
Brian Osman07c117b2019-05-23 12:51:06 -0700813class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400814public:
Brian Osman07c117b2019-05-23 12:51:06 -0700815 ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400816 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700817 , fExpression(expr) {}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400818
819 void load() override {
Brian Osman07c117b2019-05-23 12:51:06 -0700820 fGenerator.writeVariableExpression(fExpression);
Brian Osman1091f022019-05-16 09:42:16 -0400821 }
822
823 void store() override {
Brian Osman07c117b2019-05-23 12:51:06 -0700824 int count = ByteCodeGenerator::SlotCount(fExpression.fType);
825 if (count > 4) {
826 fGenerator.write(ByteCodeInstruction::kDupN);
827 fGenerator.write8(count);
828 } else {
829 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count));
830 }
831 Variable::Storage storage;
832 int location = fGenerator.getLocation(fExpression, &storage);
833 bool isGlobal = storage == Variable::kGlobal_Storage;
834 if (location < 0 || count > 4) {
835 if (location >= 0) {
836 fGenerator.write(ByteCodeInstruction::kPushImmediate);
837 fGenerator.write32(location);
838 }
839 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreExtendedGlobal
840 : ByteCodeInstruction::kStoreExtended);
841 fGenerator.write8(count);
842 } else {
843 fGenerator.write(vector_instruction(isGlobal ? ByteCodeInstruction::kStoreGlobal
844 : ByteCodeInstruction::kStore,
845 count));
846 fGenerator.write8(location);
847 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400848 }
849
850private:
851 typedef LValue INHERITED;
852
Brian Osman07c117b2019-05-23 12:51:06 -0700853 const Expression& fExpression;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400854};
855
856std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) {
857 switch (e.fKind) {
Ethan Nicholas91164d12019-05-15 15:29:54 -0400858 case Expression::kExternalValue_Kind: {
859 ExternalValue* value = ((ExternalValueReference&) e).fValue;
860 int index = fOutput->fExternalValues.size();
861 fOutput->fExternalValues.push_back(value);
862 SkASSERT(index <= 255);
863 return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index));
864 }
Brian Osman07c117b2019-05-23 12:51:06 -0700865 case Expression::kFieldAccess_Kind:
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400866 case Expression::kIndex_Kind:
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400867 case Expression::kVariableReference_Kind:
Brian Osman07c117b2019-05-23 12:51:06 -0700868 return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e));
Brian Osman0785db02019-05-24 14:19:11 -0400869 case Expression::kSwizzle_Kind: {
870 const Swizzle& s = (const Swizzle&) e;
871 return swizzle_is_simple(s)
872 ? std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e))
873 : std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, s));
874 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400875 case Expression::kTernary_Kind:
876 default:
877 printf("unsupported lvalue %s\n", e.description().c_str());
878 return nullptr;
879 }
880}
881
882void ByteCodeGenerator::writeBlock(const Block& b) {
883 for (const auto& s : b.fStatements) {
884 this->writeStatement(*s);
885 }
886}
887
888void ByteCodeGenerator::setBreakTargets() {
889 std::vector<DeferredLocation>& breaks = fBreakTargets.top();
890 for (DeferredLocation& b : breaks) {
891 b.set();
892 }
893 fBreakTargets.pop();
894}
895
896void ByteCodeGenerator::setContinueTargets() {
897 std::vector<DeferredLocation>& continues = fContinueTargets.top();
898 for (DeferredLocation& c : continues) {
899 c.set();
900 }
901 fContinueTargets.pop();
902}
903
904void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) {
905 this->write(ByteCodeInstruction::kBranch);
906 fBreakTargets.top().emplace_back(this);
907}
908
909void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) {
910 this->write(ByteCodeInstruction::kBranch);
911 fContinueTargets.top().emplace_back(this);
912}
913
914void ByteCodeGenerator::writeDoStatement(const DoStatement& d) {
915 fContinueTargets.emplace();
916 fBreakTargets.emplace();
917 size_t start = fCode->size();
918 this->writeStatement(*d.fStatement);
919 this->setContinueTargets();
920 this->writeExpression(*d.fTest);
921 this->write(ByteCodeInstruction::kConditionalBranch);
922 this->write16(start);
923 this->setBreakTargets();
924}
925
926void ByteCodeGenerator::writeForStatement(const ForStatement& f) {
927 fContinueTargets.emplace();
928 fBreakTargets.emplace();
929 if (f.fInitializer) {
930 this->writeStatement(*f.fInitializer);
931 }
932 size_t start = fCode->size();
933 if (f.fTest) {
934 this->writeExpression(*f.fTest);
935 this->write(ByteCodeInstruction::kNot);
936 this->write(ByteCodeInstruction::kConditionalBranch);
937 DeferredLocation endLocation(this);
938 this->writeStatement(*f.fStatement);
939 this->setContinueTargets();
940 if (f.fNext) {
941 this->writeExpression(*f.fNext);
Brian Osman07c117b2019-05-23 12:51:06 -0700942 this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType)));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400943 }
944 this->write(ByteCodeInstruction::kBranch);
945 this->write16(start);
946 endLocation.set();
947 } else {
948 this->writeStatement(*f.fStatement);
949 this->setContinueTargets();
950 if (f.fNext) {
951 this->writeExpression(*f.fNext);
Brian Osman07c117b2019-05-23 12:51:06 -0700952 this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType)));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400953 }
954 this->write(ByteCodeInstruction::kBranch);
955 this->write16(start);
956 }
957 this->setBreakTargets();
958}
959
960void ByteCodeGenerator::writeIfStatement(const IfStatement& i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400961 if (i.fIfFalse) {
Mike Kleinb45ee832019-05-17 11:11:11 -0500962 // if (test) { ..ifTrue.. } else { .. ifFalse .. }
963 this->writeExpression(*i.fTest);
Mike Kleinb45ee832019-05-17 11:11:11 -0500964 this->write(ByteCodeInstruction::kConditionalBranch);
965 DeferredLocation trueLocation(this);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400966 this->writeStatement(*i.fIfFalse);
Mike Kleinb45ee832019-05-17 11:11:11 -0500967 this->write(ByteCodeInstruction::kBranch);
968 DeferredLocation endLocation(this);
969 trueLocation.set();
970 this->writeStatement(*i.fIfTrue);
971 endLocation.set();
972 } else {
973 // if (test) { ..ifTrue.. }
974 this->writeExpression(*i.fTest);
975 this->write(ByteCodeInstruction::kNot);
Mike Kleinb45ee832019-05-17 11:11:11 -0500976 this->write(ByteCodeInstruction::kConditionalBranch);
977 DeferredLocation endLocation(this);
978 this->writeStatement(*i.fIfTrue);
979 endLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400980 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400981}
982
983void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
Ethan Nicholas746035a2019-04-23 13:31:09 -0400984 this->writeExpression(*r.fExpression);
985 this->write(ByteCodeInstruction::kReturn);
Brian Osman07c117b2019-05-23 12:51:06 -0700986 this->write8(SlotCount(r.fExpression->fType));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400987}
988
989void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) {
990 // not yet implemented
991 abort();
992}
993
994void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) {
995 for (const auto& declStatement : v.fVars) {
996 const VarDeclaration& decl = (VarDeclaration&) *declStatement;
997 // we need to grab the location even if we don't use it, to ensure it
998 // has been allocated
999 int location = getLocation(*decl.fVar);
1000 if (decl.fValue) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001001 this->writeExpression(*decl.fValue);
Brian Osman07c117b2019-05-23 12:51:06 -07001002 int count = SlotCount(decl.fValue->fType);
1003 if (count > 4) {
1004 this->write(ByteCodeInstruction::kPushImmediate);
1005 this->write32(location);
1006 this->write(ByteCodeInstruction::kStoreExtended);
1007 this->write8(count);
1008 } else {
1009 this->write(vector_instruction(ByteCodeInstruction::kStore, count));
1010 this->write8(location);
1011 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001012 }
1013 }
1014}
1015
1016void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) {
1017 fContinueTargets.emplace();
1018 fBreakTargets.emplace();
1019 size_t start = fCode->size();
1020 this->writeExpression(*w.fTest);
1021 this->write(ByteCodeInstruction::kNot);
1022 this->write(ByteCodeInstruction::kConditionalBranch);
1023 DeferredLocation endLocation(this);
1024 this->writeStatement(*w.fStatement);
1025 this->setContinueTargets();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001026 this->write(ByteCodeInstruction::kBranch);
1027 this->write16(start);
1028 endLocation.set();
1029 this->setBreakTargets();
1030}
1031
1032void ByteCodeGenerator::writeStatement(const Statement& s) {
1033 switch (s.fKind) {
1034 case Statement::kBlock_Kind:
1035 this->writeBlock((Block&) s);
1036 break;
1037 case Statement::kBreak_Kind:
1038 this->writeBreakStatement((BreakStatement&) s);
1039 break;
1040 case Statement::kContinue_Kind:
1041 this->writeContinueStatement((ContinueStatement&) s);
1042 break;
1043 case Statement::kDiscard_Kind:
1044 // not yet implemented
1045 abort();
1046 case Statement::kDo_Kind:
1047 this->writeDoStatement((DoStatement&) s);
1048 break;
1049 case Statement::kExpression_Kind: {
1050 const Expression& expr = *((ExpressionStatement&) s).fExpression;
1051 this->writeExpression(expr);
Brian Osman07c117b2019-05-23 12:51:06 -07001052 int count = SlotCount(expr.fType);
1053 if (count > 4) {
1054 this->write(ByteCodeInstruction::kPopN);
1055 this->write8(count);
1056 } else {
1057 this->write(vector_instruction(ByteCodeInstruction::kPop, count));
1058 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001059 break;
1060 }
1061 case Statement::kFor_Kind:
1062 this->writeForStatement((ForStatement&) s);
1063 break;
1064 case Statement::kIf_Kind:
1065 this->writeIfStatement((IfStatement&) s);
1066 break;
1067 case Statement::kNop_Kind:
1068 break;
1069 case Statement::kReturn_Kind:
1070 this->writeReturnStatement((ReturnStatement&) s);
1071 break;
1072 case Statement::kSwitch_Kind:
1073 this->writeSwitchStatement((SwitchStatement&) s);
1074 break;
1075 case Statement::kVarDeclarations_Kind:
1076 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration);
1077 break;
1078 case Statement::kWhile_Kind:
1079 this->writeWhileStatement((WhileStatement&) s);
1080 break;
1081 default:
1082 SkASSERT(false);
1083 }
1084}
1085
1086}