blob: ff32bc6715475333df7fe924cba1e9145b729964 [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;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400345 Token::Kind op;
346 std::unique_ptr<LValue> lvalue;
347 if (is_assignment(b.fOperator)) {
348 lvalue = this->getLValue(*b.fLeft);
349 lvalue->load();
350 op = remove_assignment(b.fOperator);
351 } else {
352 this->writeExpression(*b.fLeft);
353 op = b.fOperator;
Brian Osman16e6fd52019-05-29 11:19:00 -0400354 if (lType.kind() == Type::kScalar_Kind &&
355 (rType.kind() == Type::kVector_Kind || rType.kind() == Type::kMatrix_Kind)) {
356 for (int i = SlotCount(rType); i > 1; --i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400357 this->write(ByteCodeInstruction::kDup);
358 }
359 }
360 }
361 this->writeExpression(*b.fRight);
Brian Osman16e6fd52019-05-29 11:19:00 -0400362 if ((lType.kind() == Type::kVector_Kind || lType.kind() == Type::kMatrix_Kind) &&
363 rType.kind() == Type::kScalar_Kind) {
364 for (int i = SlotCount(lType); i > 1; --i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400365 this->write(ByteCodeInstruction::kDup);
366 }
367 }
Brian Osman7df8fff2019-05-29 14:33:57 -0400368 int count = std::max(SlotCount(lType), SlotCount(rType));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400369 switch (op) {
370 case Token::Kind::EQEQ:
371 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareIEQ,
372 ByteCodeInstruction::kCompareIEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400373 ByteCodeInstruction::kCompareFEQ,
374 count);
Brian Osman16e6fd52019-05-29 11:19:00 -0400375 // Collapse to a single bool
376 for (int i = count; i > 1; --i) {
377 this->write(ByteCodeInstruction::kAndB);
378 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400379 break;
380 case Token::Kind::GT:
381 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGT,
382 ByteCodeInstruction::kCompareUGT,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400383 ByteCodeInstruction::kCompareFGT,
384 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400385 break;
386 case Token::Kind::GTEQ:
387 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGTEQ,
388 ByteCodeInstruction::kCompareUGTEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400389 ByteCodeInstruction::kCompareFGTEQ,
390 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400391 break;
392 case Token::Kind::LT:
393 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLT,
394 ByteCodeInstruction::kCompareULT,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400395 ByteCodeInstruction::kCompareFLT,
396 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400397 break;
398 case Token::Kind::LTEQ:
399 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLTEQ,
400 ByteCodeInstruction::kCompareULTEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400401 ByteCodeInstruction::kCompareFLTEQ,
402 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400403 break;
404 case Token::Kind::MINUS:
405 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kSubtractI,
406 ByteCodeInstruction::kSubtractI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400407 ByteCodeInstruction::kSubtractF,
408 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400409 break;
410 case Token::Kind::NEQ:
411 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareINEQ,
412 ByteCodeInstruction::kCompareINEQ,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400413 ByteCodeInstruction::kCompareFNEQ,
414 count);
Brian Osman16e6fd52019-05-29 11:19:00 -0400415 // Collapse to a single bool
416 for (int i = count; i > 1; --i) {
417 this->write(ByteCodeInstruction::kOrB);
418 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400419 break;
420 case Token::Kind::PERCENT:
421 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kRemainderS,
422 ByteCodeInstruction::kRemainderU,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400423 ByteCodeInstruction::kRemainderF,
424 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400425 break;
426 case Token::Kind::PLUS:
427 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kAddI,
428 ByteCodeInstruction::kAddI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400429 ByteCodeInstruction::kAddF,
430 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400431 break;
432 case Token::Kind::SLASH:
433 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kDivideS,
434 ByteCodeInstruction::kDivideU,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400435 ByteCodeInstruction::kDivideF,
436 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400437 break;
438 case Token::Kind::STAR:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400439 this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kMultiplyI,
440 ByteCodeInstruction::kMultiplyI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400441 ByteCodeInstruction::kMultiplyF,
442 count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400443 break;
444 default:
445 SkASSERT(false);
446 }
447 if (lvalue) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400448 lvalue->store();
449 }
450}
451
452void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
453 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osman44d44762019-05-13 14:19:12 -0400454 this->write32(b.fValue ? 1 : 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400455}
456
457void ByteCodeGenerator::writeConstructor(const Constructor& c) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400458 for (const auto& arg : c.fArguments) {
459 this->writeExpression(*arg);
460 }
461 if (c.fArguments.size() == 1) {
Brian Osman29e013d2019-05-28 17:16:03 -0400462 const Type& inType = c.fArguments[0]->fType;
463 const Type& outType = c.fType;
464 TypeCategory inCategory = type_category(inType);
465 TypeCategory outCategory = type_category(outType);
466 int inCount = SlotCount(inType);
467 int outCount = SlotCount(outType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400468 if (inCategory != outCategory) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700469 SkASSERT(inCount == outCount);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400470 if (inCategory == TypeCategory::kFloat) {
471 SkASSERT(outCategory == TypeCategory::kSigned ||
472 outCategory == TypeCategory::kUnsigned);
Brian Osmanc51d7912019-05-22 15:16:16 -0700473 this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400474 } else if (outCategory == TypeCategory::kFloat) {
475 if (inCategory == TypeCategory::kSigned) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700476 this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400477 } else {
478 SkASSERT(inCategory == TypeCategory::kUnsigned);
Brian Osmanc51d7912019-05-22 15:16:16 -0700479 this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400480 }
481 } else {
482 SkASSERT(false);
483 }
484 }
Brian Osman29e013d2019-05-28 17:16:03 -0400485 if (inType.kind() == Type::kMatrix_Kind && outType.kind() == Type::kMatrix_Kind) {
486 this->write(ByteCodeInstruction::kMatrixToMatrix);
487 this->write8(inType.columns());
488 this->write8(inType.rows());
489 this->write8(outType.columns());
490 this->write8(outType.rows());
491 } else if (inCount != outCount) {
Brian Osmanc51d7912019-05-22 15:16:16 -0700492 SkASSERT(inCount == 1);
Brian Osman29e013d2019-05-28 17:16:03 -0400493 if (outType.kind() == Type::kMatrix_Kind) {
494 this->write(ByteCodeInstruction::kScalarToMatrix);
495 this->write8(outType.columns());
496 this->write8(outType.rows());
497 } else {
498 SkASSERT(outType.kind() == Type::kVector_Kind);
499 for (; inCount != outCount; ++inCount) {
500 this->write(ByteCodeInstruction::kDup);
501 }
Brian Osmanc51d7912019-05-22 15:16:16 -0700502 }
503 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400504 }
505}
506
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400507void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) {
508 int argumentCount = 0;
509 for (const auto& arg : f.fArguments) {
510 this->writeExpression(*arg);
Brian Osman07c117b2019-05-23 12:51:06 -0700511 argumentCount += SlotCount(arg->fType);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400512 }
513 this->write(ByteCodeInstruction::kCallExternal);
514 SkASSERT(argumentCount <= 255);
515 this->write8(argumentCount);
Brian Osman07c117b2019-05-23 12:51:06 -0700516 this->write8(SlotCount(f.fType));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400517 int index = fOutput->fExternalValues.size();
518 fOutput->fExternalValues.push_back(f.fFunction);
519 SkASSERT(index <= 255);
520 this->write8(index);
521}
522
Ethan Nicholas91164d12019-05-15 15:29:54 -0400523void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400524 this->write(vector_instruction(ByteCodeInstruction::kReadExternal,
Brian Osman07c117b2019-05-23 12:51:06 -0700525 SlotCount(e.fValue->type())));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400526 int index = fOutput->fExternalValues.size();
527 fOutput->fExternalValues.push_back(e.fValue);
528 SkASSERT(index <= 255);
529 this->write8(index);
530}
531
Brian Osman07c117b2019-05-23 12:51:06 -0700532void ByteCodeGenerator::writeVariableExpression(const Expression& expr) {
533 Variable::Storage storage;
534 int location = this->getLocation(expr, &storage);
535 bool isGlobal = storage == Variable::kGlobal_Storage;
536 int count = SlotCount(expr.fType);
537 if (location < 0 || count > 4) {
538 if (location >= 0) {
539 this->write(ByteCodeInstruction::kPushImmediate);
540 this->write32(location);
541 }
542 this->write(isGlobal ? ByteCodeInstruction::kLoadExtendedGlobal
543 : ByteCodeInstruction::kLoadExtended);
544 this->write8(count);
545 } else {
546 this->write(vector_instruction(isGlobal ? ByteCodeInstruction::kLoadGlobal
547 : ByteCodeInstruction::kLoad,
548 count));
549 this->write8(location);
550 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400551}
552
553void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
554 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400555 this->write32(Interpreter::Value((float) f.fValue).fUnsigned);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400556}
557
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400558void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) {
559 auto found = fIntrinsics.find(c.fFunction.fName);
560 if (found == fIntrinsics.end()) {
561 fErrors.error(c.fOffset, "unsupported intrinsic function");
562 return;
563 }
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400564 if (found->second.fIsSpecial) {
565 SkASSERT(found->second.fValue.fSpecial == SpecialIntrinsic::kDot);
566 SkASSERT(c.fArguments.size() == 2);
567 SkASSERT(SlotCount(c.fArguments[0]->fType) == SlotCount(c.fArguments[1]->fType));
568 this->write((ByteCodeInstruction) ((int) ByteCodeInstruction::kMultiplyF +
569 SlotCount(c.fArguments[0]->fType) - 1));
570 for (int i = SlotCount(c.fArguments[0]->fType); i > 1; --i) {
571 this->write(ByteCodeInstruction::kAddF);
572 }
573 } else {
574 switch (found->second.fValue.fInstruction) {
575 case ByteCodeInstruction::kCos:
576 case ByteCodeInstruction::kMix:
577 case ByteCodeInstruction::kSin:
578 case ByteCodeInstruction::kSqrt:
579 case ByteCodeInstruction::kTan:
580 SkASSERT(c.fArguments.size() > 0);
581 this->write((ByteCodeInstruction) ((int) found->second.fValue.fInstruction +
582 SlotCount(c.fArguments[0]->fType) - 1));
583 break;
584 case ByteCodeInstruction::kCross:
585 this->write(found->second.fValue.fInstruction);
586 break;
587 default:
588 SkASSERT(false);
589 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400590 }
591}
592
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400593void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) {
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400594 for (const auto& arg : f.fArguments) {
595 this->writeExpression(*arg);
596 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400597 if (f.fFunction.fBuiltin) {
598 this->writeIntrinsicCall(f);
599 return;
600 }
Brian Osman226668a2019-05-14 16:47:30 -0400601 this->write(ByteCodeInstruction::kCall);
602 fCallTargets.emplace_back(this, f.fFunction);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400603}
604
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400605void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) {
606 this->write(ByteCodeInstruction::kPushImmediate);
607 this->write32(i.fValue);
608}
609
610void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) {
611 // not yet implemented
612 abort();
613}
614
615void ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p) {
616 switch (p.fOperator) {
617 case Token::Kind::PLUSPLUS: // fall through
618 case Token::Kind::MINUSMINUS: {
Brian Osman07c117b2019-05-23 12:51:06 -0700619 SkASSERT(SlotCount(p.fOperand->fType) == 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400620 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
621 lvalue->load();
622 this->write(ByteCodeInstruction::kPushImmediate);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400623 this->write32(type_category(p.fType) == TypeCategory::kFloat
624 ? Interpreter::Value(1.0f).fUnsigned : 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400625 if (p.fOperator == Token::Kind::PLUSPLUS) {
626 this->writeTypedInstruction(p.fType,
627 ByteCodeInstruction::kAddI,
628 ByteCodeInstruction::kAddI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400629 ByteCodeInstruction::kAddF,
630 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400631 } else {
632 this->writeTypedInstruction(p.fType,
633 ByteCodeInstruction::kSubtractI,
634 ByteCodeInstruction::kSubtractI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400635 ByteCodeInstruction::kSubtractF,
636 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400637 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400638 lvalue->store();
639 break;
640 }
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400641 case Token::Kind::MINUS: {
642 this->writeExpression(*p.fOperand);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400643 this->writeTypedInstruction(p.fType,
Mike Klein12710912019-05-21 11:04:59 -0500644 ByteCodeInstruction::kNegateI,
645 ByteCodeInstruction::kNegateI,
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400646 ByteCodeInstruction::kNegateF,
Brian Osman07c117b2019-05-23 12:51:06 -0700647 SlotCount(p.fOperand->fType));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400648 break;
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400649 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400650 default:
651 SkASSERT(false);
652 }
653}
654
655void ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p) {
Brian Osmanf3fa6002019-05-17 14:26:53 -0400656 switch (p.fOperator) {
657 case Token::Kind::PLUSPLUS: // fall through
658 case Token::Kind::MINUSMINUS: {
Brian Osman07c117b2019-05-23 12:51:06 -0700659 SkASSERT(SlotCount(p.fOperand->fType) == 1);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400660 std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand);
661 lvalue->load();
662 this->write(ByteCodeInstruction::kDup);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400663 this->write(ByteCodeInstruction::kPushImmediate);
664 this->write32(type_category(p.fType) == TypeCategory::kFloat
665 ? Interpreter::Value(1.0f).fUnsigned : 1);
666 if (p.fOperator == Token::Kind::PLUSPLUS) {
667 this->writeTypedInstruction(p.fType,
668 ByteCodeInstruction::kAddI,
669 ByteCodeInstruction::kAddI,
670 ByteCodeInstruction::kAddF,
671 1);
672 } else {
673 this->writeTypedInstruction(p.fType,
674 ByteCodeInstruction::kSubtractI,
675 ByteCodeInstruction::kSubtractI,
676 ByteCodeInstruction::kSubtractF,
677 1);
678 }
679 lvalue->store();
680 this->write(ByteCodeInstruction::kPop);
Brian Osmanf3fa6002019-05-17 14:26:53 -0400681 break;
682 }
683 default:
684 SkASSERT(false);
685 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400686}
687
688void ByteCodeGenerator::writeSwizzle(const Swizzle& s) {
Brian Osman0785db02019-05-24 14:19:11 -0400689 if (swizzle_is_simple(s)) {
690 this->writeVariableExpression(s);
691 return;
692 }
693
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400694 switch (s.fBase->fKind) {
695 case Expression::kVariableReference_Kind: {
696 const Variable& var = ((VariableReference&) *s.fBase).fVariable;
Brian Osman1091f022019-05-16 09:42:16 -0400697 this->write(var.fStorage == Variable::kGlobal_Storage
698 ? ByteCodeInstruction::kLoadSwizzleGlobal
699 : ByteCodeInstruction::kLoadSwizzle);
700 this->write8(this->getLocation(var));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400701 this->write8(s.fComponents.size());
702 for (int c : s.fComponents) {
703 this->write8(c);
704 }
705 break;
706 }
707 default:
708 this->writeExpression(*s.fBase);
709 this->write(ByteCodeInstruction::kSwizzle);
710 this->write8(s.fBase->fType.columns());
711 this->write8(s.fComponents.size());
712 for (int c : s.fComponents) {
713 this->write8(c);
714 }
715 }
716}
717
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400718void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) {
Brian Osman4e93feb2019-05-16 15:38:00 -0400719 this->writeExpression(*t.fTest);
Brian Osman4e93feb2019-05-16 15:38:00 -0400720 this->write(ByteCodeInstruction::kConditionalBranch);
721 DeferredLocation trueLocation(this);
722 this->writeExpression(*t.fIfFalse);
Brian Osman4e93feb2019-05-16 15:38:00 -0400723 this->write(ByteCodeInstruction::kBranch);
724 DeferredLocation endLocation(this);
725 trueLocation.set();
726 this->writeExpression(*t.fIfTrue);
727 endLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400728}
729
730void ByteCodeGenerator::writeExpression(const Expression& e) {
731 switch (e.fKind) {
732 case Expression::kBinary_Kind:
733 this->writeBinaryExpression((BinaryExpression&) e);
734 break;
735 case Expression::kBoolLiteral_Kind:
736 this->writeBoolLiteral((BoolLiteral&) e);
737 break;
738 case Expression::kConstructor_Kind:
739 this->writeConstructor((Constructor&) e);
740 break;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400741 case Expression::kExternalFunctionCall_Kind:
742 this->writeExternalFunctionCall((ExternalFunctionCall&) e);
743 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400744 case Expression::kExternalValue_Kind:
745 this->writeExternalValue((ExternalValueReference&) e);
746 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400747 case Expression::kFieldAccess_Kind:
Brian Osman07c117b2019-05-23 12:51:06 -0700748 case Expression::kIndex_Kind:
749 case Expression::kVariableReference_Kind:
750 this->writeVariableExpression(e);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400751 break;
752 case Expression::kFloatLiteral_Kind:
753 this->writeFloatLiteral((FloatLiteral&) e);
754 break;
755 case Expression::kFunctionCall_Kind:
756 this->writeFunctionCall((FunctionCall&) e);
757 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400758 case Expression::kIntLiteral_Kind:
759 this->writeIntLiteral((IntLiteral&) e);
760 break;
761 case Expression::kNullLiteral_Kind:
762 this->writeNullLiteral((NullLiteral&) e);
763 break;
764 case Expression::kPrefix_Kind:
765 this->writePrefixExpression((PrefixExpression&) e);
766 break;
767 case Expression::kPostfix_Kind:
768 this->writePostfixExpression((PostfixExpression&) e);
769 break;
770 case Expression::kSwizzle_Kind:
771 this->writeSwizzle((Swizzle&) e);
772 break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400773 case Expression::kTernary_Kind:
774 this->writeTernaryExpression((TernaryExpression&) e);
775 break;
776 default:
777 printf("unsupported expression %s\n", e.description().c_str());
778 SkASSERT(false);
779 }
780}
781
Ethan Nicholas91164d12019-05-15 15:29:54 -0400782class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue {
783public:
784 ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index)
785 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700786 , fCount(ByteCodeGenerator::SlotCount(value.type()))
Ethan Nicholas91164d12019-05-15 15:29:54 -0400787 , fIndex(index) {}
788
789 void load() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400790 fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400791 fGenerator.write8(fIndex);
792 }
793
794 void store() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400795 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount));
796 fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400797 fGenerator.write8(fIndex);
798 }
799
800private:
801 typedef LValue INHERITED;
802
803 int fCount;
804
805 int fIndex;
806};
807
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400808class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue {
809public:
810 ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle)
811 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700812 , fSwizzle(swizzle) {}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400813
814 void load() override {
Brian Osman1091f022019-05-16 09:42:16 -0400815 fGenerator.writeSwizzle(fSwizzle);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400816 }
817
818 void store() override {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400819 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup,
820 fSwizzle.fComponents.size()));
Brian Osman07c117b2019-05-23 12:51:06 -0700821 Variable::Storage storage;
822 int location = fGenerator.getLocation(*fSwizzle.fBase, &storage);
823 bool isGlobal = storage == Variable::kGlobal_Storage;
824 if (location < 0) {
825 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleIndirectGlobal
826 : ByteCodeInstruction::kStoreSwizzleIndirect);
827 } else {
828 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleGlobal
829 : ByteCodeInstruction::kStoreSwizzle);
830 fGenerator.write8(location);
831 }
Brian Osman1091f022019-05-16 09:42:16 -0400832 fGenerator.write8(fSwizzle.fComponents.size());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400833 for (int c : fSwizzle.fComponents) {
834 fGenerator.write8(c);
835 }
836 }
837
838private:
839 const Swizzle& fSwizzle;
840
841 typedef LValue INHERITED;
842};
843
Brian Osman07c117b2019-05-23 12:51:06 -0700844class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400845public:
Brian Osman07c117b2019-05-23 12:51:06 -0700846 ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400847 : INHERITED(*generator)
Brian Osman07c117b2019-05-23 12:51:06 -0700848 , fExpression(expr) {}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400849
850 void load() override {
Brian Osman07c117b2019-05-23 12:51:06 -0700851 fGenerator.writeVariableExpression(fExpression);
Brian Osman1091f022019-05-16 09:42:16 -0400852 }
853
854 void store() override {
Brian Osman07c117b2019-05-23 12:51:06 -0700855 int count = ByteCodeGenerator::SlotCount(fExpression.fType);
856 if (count > 4) {
857 fGenerator.write(ByteCodeInstruction::kDupN);
858 fGenerator.write8(count);
859 } else {
860 fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count));
861 }
862 Variable::Storage storage;
863 int location = fGenerator.getLocation(fExpression, &storage);
864 bool isGlobal = storage == Variable::kGlobal_Storage;
865 if (location < 0 || count > 4) {
866 if (location >= 0) {
867 fGenerator.write(ByteCodeInstruction::kPushImmediate);
868 fGenerator.write32(location);
869 }
870 fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreExtendedGlobal
871 : ByteCodeInstruction::kStoreExtended);
872 fGenerator.write8(count);
873 } else {
874 fGenerator.write(vector_instruction(isGlobal ? ByteCodeInstruction::kStoreGlobal
875 : ByteCodeInstruction::kStore,
876 count));
877 fGenerator.write8(location);
878 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400879 }
880
881private:
882 typedef LValue INHERITED;
883
Brian Osman07c117b2019-05-23 12:51:06 -0700884 const Expression& fExpression;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400885};
886
887std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) {
888 switch (e.fKind) {
Ethan Nicholas91164d12019-05-15 15:29:54 -0400889 case Expression::kExternalValue_Kind: {
890 ExternalValue* value = ((ExternalValueReference&) e).fValue;
891 int index = fOutput->fExternalValues.size();
892 fOutput->fExternalValues.push_back(value);
893 SkASSERT(index <= 255);
894 return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index));
895 }
Brian Osman07c117b2019-05-23 12:51:06 -0700896 case Expression::kFieldAccess_Kind:
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400897 case Expression::kIndex_Kind:
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400898 case Expression::kVariableReference_Kind:
Brian Osman07c117b2019-05-23 12:51:06 -0700899 return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e));
Brian Osman0785db02019-05-24 14:19:11 -0400900 case Expression::kSwizzle_Kind: {
901 const Swizzle& s = (const Swizzle&) e;
902 return swizzle_is_simple(s)
903 ? std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e))
904 : std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, s));
905 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400906 case Expression::kTernary_Kind:
907 default:
908 printf("unsupported lvalue %s\n", e.description().c_str());
909 return nullptr;
910 }
911}
912
913void ByteCodeGenerator::writeBlock(const Block& b) {
914 for (const auto& s : b.fStatements) {
915 this->writeStatement(*s);
916 }
917}
918
919void ByteCodeGenerator::setBreakTargets() {
920 std::vector<DeferredLocation>& breaks = fBreakTargets.top();
921 for (DeferredLocation& b : breaks) {
922 b.set();
923 }
924 fBreakTargets.pop();
925}
926
927void ByteCodeGenerator::setContinueTargets() {
928 std::vector<DeferredLocation>& continues = fContinueTargets.top();
929 for (DeferredLocation& c : continues) {
930 c.set();
931 }
932 fContinueTargets.pop();
933}
934
935void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) {
936 this->write(ByteCodeInstruction::kBranch);
937 fBreakTargets.top().emplace_back(this);
938}
939
940void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) {
941 this->write(ByteCodeInstruction::kBranch);
942 fContinueTargets.top().emplace_back(this);
943}
944
945void ByteCodeGenerator::writeDoStatement(const DoStatement& d) {
946 fContinueTargets.emplace();
947 fBreakTargets.emplace();
948 size_t start = fCode->size();
949 this->writeStatement(*d.fStatement);
950 this->setContinueTargets();
951 this->writeExpression(*d.fTest);
952 this->write(ByteCodeInstruction::kConditionalBranch);
953 this->write16(start);
954 this->setBreakTargets();
955}
956
957void ByteCodeGenerator::writeForStatement(const ForStatement& f) {
958 fContinueTargets.emplace();
959 fBreakTargets.emplace();
960 if (f.fInitializer) {
961 this->writeStatement(*f.fInitializer);
962 }
963 size_t start = fCode->size();
964 if (f.fTest) {
965 this->writeExpression(*f.fTest);
966 this->write(ByteCodeInstruction::kNot);
967 this->write(ByteCodeInstruction::kConditionalBranch);
968 DeferredLocation endLocation(this);
969 this->writeStatement(*f.fStatement);
970 this->setContinueTargets();
971 if (f.fNext) {
972 this->writeExpression(*f.fNext);
Brian Osman07c117b2019-05-23 12:51:06 -0700973 this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType)));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400974 }
975 this->write(ByteCodeInstruction::kBranch);
976 this->write16(start);
977 endLocation.set();
978 } else {
979 this->writeStatement(*f.fStatement);
980 this->setContinueTargets();
981 if (f.fNext) {
982 this->writeExpression(*f.fNext);
Brian Osman07c117b2019-05-23 12:51:06 -0700983 this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType)));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400984 }
985 this->write(ByteCodeInstruction::kBranch);
986 this->write16(start);
987 }
988 this->setBreakTargets();
989}
990
991void ByteCodeGenerator::writeIfStatement(const IfStatement& i) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400992 if (i.fIfFalse) {
Mike Kleinb45ee832019-05-17 11:11:11 -0500993 // if (test) { ..ifTrue.. } else { .. ifFalse .. }
994 this->writeExpression(*i.fTest);
Mike Kleinb45ee832019-05-17 11:11:11 -0500995 this->write(ByteCodeInstruction::kConditionalBranch);
996 DeferredLocation trueLocation(this);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400997 this->writeStatement(*i.fIfFalse);
Mike Kleinb45ee832019-05-17 11:11:11 -0500998 this->write(ByteCodeInstruction::kBranch);
999 DeferredLocation endLocation(this);
1000 trueLocation.set();
1001 this->writeStatement(*i.fIfTrue);
1002 endLocation.set();
1003 } else {
1004 // if (test) { ..ifTrue.. }
1005 this->writeExpression(*i.fTest);
1006 this->write(ByteCodeInstruction::kNot);
Mike Kleinb45ee832019-05-17 11:11:11 -05001007 this->write(ByteCodeInstruction::kConditionalBranch);
1008 DeferredLocation endLocation(this);
1009 this->writeStatement(*i.fIfTrue);
1010 endLocation.set();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001011 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001012}
1013
1014void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
Ethan Nicholas746035a2019-04-23 13:31:09 -04001015 this->writeExpression(*r.fExpression);
1016 this->write(ByteCodeInstruction::kReturn);
Brian Osman07c117b2019-05-23 12:51:06 -07001017 this->write8(SlotCount(r.fExpression->fType));
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001018}
1019
1020void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) {
1021 // not yet implemented
1022 abort();
1023}
1024
1025void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) {
1026 for (const auto& declStatement : v.fVars) {
1027 const VarDeclaration& decl = (VarDeclaration&) *declStatement;
1028 // we need to grab the location even if we don't use it, to ensure it
1029 // has been allocated
1030 int location = getLocation(*decl.fVar);
1031 if (decl.fValue) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001032 this->writeExpression(*decl.fValue);
Brian Osman07c117b2019-05-23 12:51:06 -07001033 int count = SlotCount(decl.fValue->fType);
1034 if (count > 4) {
1035 this->write(ByteCodeInstruction::kPushImmediate);
1036 this->write32(location);
1037 this->write(ByteCodeInstruction::kStoreExtended);
1038 this->write8(count);
1039 } else {
1040 this->write(vector_instruction(ByteCodeInstruction::kStore, count));
1041 this->write8(location);
1042 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001043 }
1044 }
1045}
1046
1047void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) {
1048 fContinueTargets.emplace();
1049 fBreakTargets.emplace();
1050 size_t start = fCode->size();
1051 this->writeExpression(*w.fTest);
1052 this->write(ByteCodeInstruction::kNot);
1053 this->write(ByteCodeInstruction::kConditionalBranch);
1054 DeferredLocation endLocation(this);
1055 this->writeStatement(*w.fStatement);
1056 this->setContinueTargets();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001057 this->write(ByteCodeInstruction::kBranch);
1058 this->write16(start);
1059 endLocation.set();
1060 this->setBreakTargets();
1061}
1062
1063void ByteCodeGenerator::writeStatement(const Statement& s) {
1064 switch (s.fKind) {
1065 case Statement::kBlock_Kind:
1066 this->writeBlock((Block&) s);
1067 break;
1068 case Statement::kBreak_Kind:
1069 this->writeBreakStatement((BreakStatement&) s);
1070 break;
1071 case Statement::kContinue_Kind:
1072 this->writeContinueStatement((ContinueStatement&) s);
1073 break;
1074 case Statement::kDiscard_Kind:
1075 // not yet implemented
1076 abort();
1077 case Statement::kDo_Kind:
1078 this->writeDoStatement((DoStatement&) s);
1079 break;
1080 case Statement::kExpression_Kind: {
1081 const Expression& expr = *((ExpressionStatement&) s).fExpression;
1082 this->writeExpression(expr);
Brian Osman07c117b2019-05-23 12:51:06 -07001083 int count = SlotCount(expr.fType);
1084 if (count > 4) {
1085 this->write(ByteCodeInstruction::kPopN);
1086 this->write8(count);
1087 } else {
1088 this->write(vector_instruction(ByteCodeInstruction::kPop, count));
1089 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001090 break;
1091 }
1092 case Statement::kFor_Kind:
1093 this->writeForStatement((ForStatement&) s);
1094 break;
1095 case Statement::kIf_Kind:
1096 this->writeIfStatement((IfStatement&) s);
1097 break;
1098 case Statement::kNop_Kind:
1099 break;
1100 case Statement::kReturn_Kind:
1101 this->writeReturnStatement((ReturnStatement&) s);
1102 break;
1103 case Statement::kSwitch_Kind:
1104 this->writeSwitchStatement((SwitchStatement&) s);
1105 break;
1106 case Statement::kVarDeclarations_Kind:
1107 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration);
1108 break;
1109 case Statement::kWhile_Kind:
1110 this->writeWhileStatement((WhileStatement&) s);
1111 break;
1112 default:
1113 SkASSERT(false);
1114 }
1115}
1116
1117}