Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLByteCodeGenerator.h" |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 9 | #include "src/sksl/SkSLInterpreter.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 10 | |
| 11 | namespace SkSL { |
| 12 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 13 | ByteCodeGenerator::ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 14 | ByteCode* output) |
| 15 | : INHERITED(program, errors, nullptr) |
| 16 | , fContext(*context) |
| 17 | , fOutput(output) { |
| 18 | fIntrinsics["cos"] = ByteCodeInstruction::kCos; |
| 19 | fIntrinsics["sin"] = ByteCodeInstruction::kSin; |
| 20 | fIntrinsics["sqrt"] = ByteCodeInstruction::kSqrt; |
| 21 | fIntrinsics["tan"] = ByteCodeInstruction::kTan; |
| 22 | } |
| 23 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 24 | int ByteCodeGenerator::SlotCount(const Type& type) { |
| 25 | if (type.kind() == Type::kStruct_Kind) { |
| 26 | int slots = 0; |
| 27 | for (const auto& f : type.fields()) { |
| 28 | slots += SlotCount(*f.fType); |
| 29 | } |
| 30 | SkASSERT(slots <= 255); |
| 31 | return slots; |
| 32 | } else if (type.kind() == Type::kArray_Kind) { |
| 33 | int columns = type.columns(); |
| 34 | SkASSERT(columns >= 0); |
| 35 | int slots = columns * SlotCount(type.componentType()); |
| 36 | SkASSERT(slots <= 255); |
| 37 | return slots; |
| 38 | } else { |
| 39 | return type.columns() * type.rows(); |
| 40 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | bool ByteCodeGenerator::generateCode() { |
| 44 | for (const auto& e : fProgram) { |
| 45 | switch (e.fKind) { |
| 46 | case ProgramElement::kFunction_Kind: { |
| 47 | std::unique_ptr<ByteCodeFunction> f = this->writeFunction((FunctionDefinition&) e); |
| 48 | if (!f) { |
| 49 | return false; |
| 50 | } |
| 51 | fOutput->fFunctions.push_back(std::move(f)); |
| 52 | break; |
| 53 | } |
| 54 | case ProgramElement::kVar_Kind: { |
| 55 | VarDeclarations& decl = (VarDeclarations&) e; |
| 56 | for (const auto& v : decl.fVars) { |
| 57 | const Variable* declVar = ((VarDeclaration&) *v).fVar; |
| 58 | if (declVar->fModifiers.fLayout.fBuiltin >= 0) { |
| 59 | continue; |
| 60 | } |
| 61 | if (declVar->fModifiers.fFlags & Modifiers::kIn_Flag) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 62 | for (int i = SlotCount(declVar->fType); i > 0; --i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 63 | fOutput->fInputSlots.push_back(fOutput->fGlobalCount++); |
| 64 | } |
| 65 | } else { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 66 | fOutput->fGlobalCount += SlotCount(declVar->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | break; |
| 70 | } |
| 71 | default: |
| 72 | ; // ignore |
| 73 | } |
| 74 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 75 | for (auto& call : fCallTargets) { |
| 76 | if (!call.set()) { |
| 77 | return false; |
| 78 | } |
| 79 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
| 83 | std::unique_ptr<ByteCodeFunction> ByteCodeGenerator::writeFunction(const FunctionDefinition& f) { |
| 84 | fFunction = &f; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 85 | std::unique_ptr<ByteCodeFunction> result(new ByteCodeFunction(&f.fDeclaration)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 86 | fParameterCount = 0; |
| 87 | for (const auto& p : f.fDeclaration.fParameters) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 88 | fParameterCount += SlotCount(p->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 89 | } |
| 90 | fCode = &result->fCode; |
| 91 | this->writeStatement(*f.fBody); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 92 | this->write(ByteCodeInstruction::kReturn); |
| 93 | this->write8(0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 94 | result->fParameterCount = fParameterCount; |
| 95 | result->fLocalCount = fLocals.size(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 96 | const Type& returnType = f.fDeclaration.fReturnType; |
| 97 | if (returnType != *fContext.fVoid_Type) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 98 | result->fReturnCount = SlotCount(returnType); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 99 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 100 | fLocals.clear(); |
| 101 | fFunction = nullptr; |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | enum class TypeCategory { |
| 106 | kBool, |
| 107 | kSigned, |
| 108 | kUnsigned, |
| 109 | kFloat, |
| 110 | }; |
| 111 | |
| 112 | static TypeCategory type_category(const Type& type) { |
| 113 | switch (type.kind()) { |
| 114 | case Type::Kind::kVector_Kind: |
| 115 | case Type::Kind::kMatrix_Kind: |
| 116 | return type_category(type.componentType()); |
| 117 | default: |
| 118 | if (type.fName == "bool") { |
| 119 | return TypeCategory::kBool; |
| 120 | } else if (type.fName == "int" || type.fName == "short") { |
| 121 | return TypeCategory::kSigned; |
| 122 | } else if (type.fName == "uint" || type.fName == "ushort") { |
| 123 | return TypeCategory::kUnsigned; |
| 124 | } else { |
| 125 | SkASSERT(type.fName == "float" || type.fName == "half"); |
| 126 | return TypeCategory::kFloat; |
| 127 | } |
| 128 | ABORT("unsupported type: %s\n", type.description().c_str()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | int ByteCodeGenerator::getLocation(const Variable& var) { |
| 133 | // given that we seldom have more than a couple of variables, linear search is probably the most |
| 134 | // efficient way to handle lookups |
| 135 | switch (var.fStorage) { |
| 136 | case Variable::kLocal_Storage: { |
| 137 | for (int i = fLocals.size() - 1; i >= 0; --i) { |
| 138 | if (fLocals[i] == &var) { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 139 | SkASSERT(fParameterCount + i <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 140 | return fParameterCount + i; |
| 141 | } |
| 142 | } |
| 143 | int result = fParameterCount + fLocals.size(); |
| 144 | fLocals.push_back(&var); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 145 | for (int i = 0; i < SlotCount(var.fType) - 1; ++i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 146 | fLocals.push_back(nullptr); |
| 147 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 148 | SkASSERT(result <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 149 | return result; |
| 150 | } |
| 151 | case Variable::kParameter_Storage: { |
| 152 | int offset = 0; |
| 153 | for (const auto& p : fFunction->fDeclaration.fParameters) { |
| 154 | if (p == &var) { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 155 | SkASSERT(offset <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 156 | return offset; |
| 157 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 158 | offset += SlotCount(p->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 159 | } |
| 160 | SkASSERT(false); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 161 | return 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 162 | } |
| 163 | case Variable::kGlobal_Storage: { |
| 164 | int offset = 0; |
| 165 | for (const auto& e : fProgram) { |
| 166 | if (e.fKind == ProgramElement::kVar_Kind) { |
| 167 | VarDeclarations& decl = (VarDeclarations&) e; |
| 168 | for (const auto& v : decl.fVars) { |
| 169 | const Variable* declVar = ((VarDeclaration&) *v).fVar; |
| 170 | if (declVar->fModifiers.fLayout.fBuiltin >= 0) { |
| 171 | continue; |
| 172 | } |
| 173 | if (declVar == &var) { |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 174 | SkASSERT(offset <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 175 | return offset; |
| 176 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 177 | offset += SlotCount(declVar->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | SkASSERT(false); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 182 | return 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 183 | } |
| 184 | default: |
| 185 | SkASSERT(false); |
| 186 | return 0; |
| 187 | } |
| 188 | } |
| 189 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 190 | int ByteCodeGenerator::getLocation(const Expression& expr, Variable::Storage* storage) { |
| 191 | switch (expr.fKind) { |
| 192 | case Expression::kFieldAccess_Kind: { |
| 193 | const FieldAccess& f = (const FieldAccess&)expr; |
| 194 | int baseAddr = this->getLocation(*f.fBase, storage); |
| 195 | int offset = 0; |
| 196 | for (int i = 0; i < f.fFieldIndex; ++i) { |
| 197 | offset += SlotCount(*f.fBase->fType.fields()[i].fType); |
| 198 | } |
| 199 | if (baseAddr < 0) { |
| 200 | this->write(ByteCodeInstruction::kPushImmediate); |
| 201 | this->write32(offset); |
| 202 | this->write(ByteCodeInstruction::kAddI); |
| 203 | return -1; |
| 204 | } else { |
| 205 | return baseAddr + offset; |
| 206 | } |
| 207 | } |
| 208 | case Expression::kIndex_Kind: { |
| 209 | const IndexExpression& i = (const IndexExpression&)expr; |
| 210 | int stride = SlotCount(i.fType); |
| 211 | int offset = -1; |
| 212 | if (i.fIndex->isConstant()) { |
| 213 | offset = i.fIndex->getConstantInt() * stride; |
| 214 | } else { |
| 215 | this->writeExpression(*i.fIndex); |
| 216 | this->write(ByteCodeInstruction::kPushImmediate); |
| 217 | this->write32(stride); |
| 218 | this->write(ByteCodeInstruction::kMultiplyI); |
| 219 | } |
| 220 | int baseAddr = this->getLocation(*i.fBase, storage); |
| 221 | if (baseAddr >= 0 && offset >= 0) { |
| 222 | return baseAddr + offset; |
| 223 | } |
| 224 | if (baseAddr >= 0) { |
| 225 | this->write(ByteCodeInstruction::kPushImmediate); |
| 226 | this->write32(baseAddr); |
| 227 | } |
| 228 | if (offset >= 0) { |
| 229 | this->write(ByteCodeInstruction::kPushImmediate); |
| 230 | this->write32(offset); |
| 231 | } |
| 232 | this->write(ByteCodeInstruction::kAddI); |
| 233 | return -1; |
| 234 | } |
| 235 | case Expression::kVariableReference_Kind: { |
| 236 | const Variable& var = ((const VariableReference&)expr).fVariable; |
| 237 | *storage = var.fStorage; |
| 238 | return this->getLocation(var); |
| 239 | } |
| 240 | default: |
| 241 | SkASSERT(false); |
| 242 | return 0; |
| 243 | } |
| 244 | } |
| 245 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 246 | void ByteCodeGenerator::write8(uint8_t b) { |
| 247 | fCode->push_back(b); |
| 248 | } |
| 249 | |
| 250 | void ByteCodeGenerator::write16(uint16_t i) { |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 251 | size_t n = fCode->size(); |
| 252 | fCode->resize(n+2); |
| 253 | memcpy(fCode->data() + n, &i, 2); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void ByteCodeGenerator::write32(uint32_t i) { |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 257 | size_t n = fCode->size(); |
| 258 | fCode->resize(n+4); |
| 259 | memcpy(fCode->data() + n, &i, 4); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void ByteCodeGenerator::write(ByteCodeInstruction i) { |
Mike Klein | 108e935 | 2019-05-21 11:05:17 -0500 | [diff] [blame] | 263 | this->write16((uint16_t)i); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 266 | static ByteCodeInstruction vector_instruction(ByteCodeInstruction base, int count) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 267 | SkASSERT(count >= 1 && count <= 4); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 268 | return ((ByteCodeInstruction) ((int) base + count - 1)); |
| 269 | } |
| 270 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 271 | void ByteCodeGenerator::writeTypedInstruction(const Type& type, ByteCodeInstruction s, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 272 | ByteCodeInstruction u, ByteCodeInstruction f, |
| 273 | int count) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 274 | switch (type_category(type)) { |
| 275 | case TypeCategory::kSigned: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 276 | this->write(vector_instruction(s, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 277 | break; |
| 278 | case TypeCategory::kUnsigned: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 279 | this->write(vector_instruction(u, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 280 | break; |
| 281 | case TypeCategory::kFloat: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 282 | this->write(vector_instruction(f, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 283 | break; |
| 284 | default: |
| 285 | SkASSERT(false); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b) { |
| 290 | if (b.fOperator == Token::Kind::EQ) { |
| 291 | std::unique_ptr<LValue> lvalue = this->getLValue(*b.fLeft); |
| 292 | this->writeExpression(*b.fRight); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 293 | lvalue->store(); |
| 294 | return; |
| 295 | } |
| 296 | Token::Kind op; |
| 297 | std::unique_ptr<LValue> lvalue; |
| 298 | if (is_assignment(b.fOperator)) { |
| 299 | lvalue = this->getLValue(*b.fLeft); |
| 300 | lvalue->load(); |
| 301 | op = remove_assignment(b.fOperator); |
| 302 | } else { |
| 303 | this->writeExpression(*b.fLeft); |
| 304 | op = b.fOperator; |
| 305 | if (b.fLeft->fType.kind() == Type::kScalar_Kind && |
| 306 | b.fRight->fType.kind() == Type::kVector_Kind) { |
| 307 | for (int i = b.fRight->fType.columns(); i > 1; --i) { |
| 308 | this->write(ByteCodeInstruction::kDup); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | this->writeExpression(*b.fRight); |
| 313 | if (b.fLeft->fType.kind() == Type::kVector_Kind && |
| 314 | b.fRight->fType.kind() == Type::kScalar_Kind) { |
| 315 | for (int i = b.fLeft->fType.columns(); i > 1; --i) { |
| 316 | this->write(ByteCodeInstruction::kDup); |
| 317 | } |
| 318 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 319 | int count = SlotCount(b.fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 320 | switch (op) { |
| 321 | case Token::Kind::EQEQ: |
| 322 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareIEQ, |
| 323 | ByteCodeInstruction::kCompareIEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 324 | ByteCodeInstruction::kCompareFEQ, |
| 325 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 326 | break; |
| 327 | case Token::Kind::GT: |
| 328 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGT, |
| 329 | ByteCodeInstruction::kCompareUGT, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 330 | ByteCodeInstruction::kCompareFGT, |
| 331 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 332 | break; |
| 333 | case Token::Kind::GTEQ: |
| 334 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGTEQ, |
| 335 | ByteCodeInstruction::kCompareUGTEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 336 | ByteCodeInstruction::kCompareFGTEQ, |
| 337 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 338 | break; |
| 339 | case Token::Kind::LT: |
| 340 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLT, |
| 341 | ByteCodeInstruction::kCompareULT, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 342 | ByteCodeInstruction::kCompareFLT, |
| 343 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 344 | break; |
| 345 | case Token::Kind::LTEQ: |
| 346 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLTEQ, |
| 347 | ByteCodeInstruction::kCompareULTEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 348 | ByteCodeInstruction::kCompareFLTEQ, |
| 349 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 350 | break; |
| 351 | case Token::Kind::MINUS: |
| 352 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kSubtractI, |
| 353 | ByteCodeInstruction::kSubtractI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 354 | ByteCodeInstruction::kSubtractF, |
| 355 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 356 | break; |
| 357 | case Token::Kind::NEQ: |
| 358 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareINEQ, |
| 359 | ByteCodeInstruction::kCompareINEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 360 | ByteCodeInstruction::kCompareFNEQ, |
| 361 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 362 | break; |
| 363 | case Token::Kind::PERCENT: |
| 364 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kRemainderS, |
| 365 | ByteCodeInstruction::kRemainderU, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 366 | ByteCodeInstruction::kRemainderF, |
| 367 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 368 | break; |
| 369 | case Token::Kind::PLUS: |
| 370 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kAddI, |
| 371 | ByteCodeInstruction::kAddI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 372 | ByteCodeInstruction::kAddF, |
| 373 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 374 | break; |
| 375 | case Token::Kind::SLASH: |
| 376 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kDivideS, |
| 377 | ByteCodeInstruction::kDivideU, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 378 | ByteCodeInstruction::kDivideF, |
| 379 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 380 | break; |
| 381 | case Token::Kind::STAR: |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 382 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kMultiplyI, |
| 383 | ByteCodeInstruction::kMultiplyI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 384 | ByteCodeInstruction::kMultiplyF, |
| 385 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 386 | break; |
| 387 | default: |
| 388 | SkASSERT(false); |
| 389 | } |
| 390 | if (lvalue) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 391 | lvalue->store(); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
| 396 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | 44d4476 | 2019-05-13 14:19:12 -0400 | [diff] [blame] | 397 | this->write32(b.fValue ? 1 : 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void ByteCodeGenerator::writeConstructor(const Constructor& c) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 401 | for (const auto& arg : c.fArguments) { |
| 402 | this->writeExpression(*arg); |
| 403 | } |
| 404 | if (c.fArguments.size() == 1) { |
| 405 | TypeCategory inCategory = type_category(c.fArguments[0]->fType); |
| 406 | TypeCategory outCategory = type_category(c.fType); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 407 | int inCount = c.fArguments[0]->fType.columns(); |
| 408 | int outCount = c.fType.columns(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 409 | if (inCategory != outCategory) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 410 | SkASSERT(inCount == outCount); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 411 | if (inCategory == TypeCategory::kFloat) { |
| 412 | SkASSERT(outCategory == TypeCategory::kSigned || |
| 413 | outCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 414 | this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 415 | } else if (outCategory == TypeCategory::kFloat) { |
| 416 | if (inCategory == TypeCategory::kSigned) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 417 | this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 418 | } else { |
| 419 | SkASSERT(inCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 420 | this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 421 | } |
| 422 | } else { |
| 423 | SkASSERT(false); |
| 424 | } |
| 425 | } |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 426 | if (inCount != outCount) { |
| 427 | SkASSERT(inCount == 1); |
| 428 | for (; inCount != outCount; ++inCount) { |
| 429 | this->write(ByteCodeInstruction::kDup); |
| 430 | } |
| 431 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 435 | void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) { |
| 436 | int argumentCount = 0; |
| 437 | for (const auto& arg : f.fArguments) { |
| 438 | this->writeExpression(*arg); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 439 | argumentCount += SlotCount(arg->fType); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 440 | } |
| 441 | this->write(ByteCodeInstruction::kCallExternal); |
| 442 | SkASSERT(argumentCount <= 255); |
| 443 | this->write8(argumentCount); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 444 | this->write8(SlotCount(f.fType)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 445 | int index = fOutput->fExternalValues.size(); |
| 446 | fOutput->fExternalValues.push_back(f.fFunction); |
| 447 | SkASSERT(index <= 255); |
| 448 | this->write8(index); |
| 449 | } |
| 450 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 451 | void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 452 | this->write(vector_instruction(ByteCodeInstruction::kReadExternal, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 453 | SlotCount(e.fValue->type()))); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 454 | int index = fOutput->fExternalValues.size(); |
| 455 | fOutput->fExternalValues.push_back(e.fValue); |
| 456 | SkASSERT(index <= 255); |
| 457 | this->write8(index); |
| 458 | } |
| 459 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 460 | void ByteCodeGenerator::writeVariableExpression(const Expression& expr) { |
| 461 | Variable::Storage storage; |
| 462 | int location = this->getLocation(expr, &storage); |
| 463 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 464 | int count = SlotCount(expr.fType); |
| 465 | if (location < 0 || count > 4) { |
| 466 | if (location >= 0) { |
| 467 | this->write(ByteCodeInstruction::kPushImmediate); |
| 468 | this->write32(location); |
| 469 | } |
| 470 | this->write(isGlobal ? ByteCodeInstruction::kLoadExtendedGlobal |
| 471 | : ByteCodeInstruction::kLoadExtended); |
| 472 | this->write8(count); |
| 473 | } else { |
| 474 | this->write(vector_instruction(isGlobal ? ByteCodeInstruction::kLoadGlobal |
| 475 | : ByteCodeInstruction::kLoad, |
| 476 | count)); |
| 477 | this->write8(location); |
| 478 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
| 482 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 483 | this->write32(Interpreter::Value((float) f.fValue).fUnsigned); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 484 | } |
| 485 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 486 | void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) { |
| 487 | auto found = fIntrinsics.find(c.fFunction.fName); |
| 488 | if (found == fIntrinsics.end()) { |
| 489 | fErrors.error(c.fOffset, "unsupported intrinsic function"); |
| 490 | return; |
| 491 | } |
| 492 | switch (found->second) { |
| 493 | case ByteCodeInstruction::kCos: // fall through |
| 494 | case ByteCodeInstruction::kSin: // fall through |
| 495 | case ByteCodeInstruction::kSqrt: // fall through |
| 496 | case ByteCodeInstruction::kTan: |
| 497 | SkASSERT(c.fArguments.size() == 1); |
| 498 | this->write((ByteCodeInstruction) ((int) found->second + |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 499 | SlotCount(c.fArguments[0]->fType) - 1)); |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 500 | break; |
| 501 | default: |
| 502 | SkASSERT(false); |
| 503 | } |
| 504 | } |
| 505 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 506 | void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) { |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 507 | if (f.fFunction.fBuiltin) { |
| 508 | this->writeIntrinsicCall(f); |
| 509 | return; |
| 510 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 511 | for (const auto& arg : f.fArguments) { |
| 512 | this->writeExpression(*arg); |
| 513 | } |
| 514 | this->write(ByteCodeInstruction::kCall); |
| 515 | fCallTargets.emplace_back(this, f.fFunction); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 516 | } |
| 517 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 518 | void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
| 519 | this->write(ByteCodeInstruction::kPushImmediate); |
| 520 | this->write32(i.fValue); |
| 521 | } |
| 522 | |
| 523 | void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) { |
| 524 | // not yet implemented |
| 525 | abort(); |
| 526 | } |
| 527 | |
| 528 | void ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p) { |
| 529 | switch (p.fOperator) { |
| 530 | case Token::Kind::PLUSPLUS: // fall through |
| 531 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 532 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 533 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 534 | lvalue->load(); |
| 535 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 536 | this->write32(type_category(p.fType) == TypeCategory::kFloat |
| 537 | ? Interpreter::Value(1.0f).fUnsigned : 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 538 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 539 | this->writeTypedInstruction(p.fType, |
| 540 | ByteCodeInstruction::kAddI, |
| 541 | ByteCodeInstruction::kAddI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 542 | ByteCodeInstruction::kAddF, |
| 543 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 544 | } else { |
| 545 | this->writeTypedInstruction(p.fType, |
| 546 | ByteCodeInstruction::kSubtractI, |
| 547 | ByteCodeInstruction::kSubtractI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 548 | ByteCodeInstruction::kSubtractF, |
| 549 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 550 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 551 | lvalue->store(); |
| 552 | break; |
| 553 | } |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 554 | case Token::Kind::MINUS: { |
| 555 | this->writeExpression(*p.fOperand); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 556 | this->writeTypedInstruction(p.fType, |
Mike Klein | 1271091 | 2019-05-21 11:04:59 -0500 | [diff] [blame] | 557 | ByteCodeInstruction::kNegateI, |
| 558 | ByteCodeInstruction::kNegateI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 559 | ByteCodeInstruction::kNegateF, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 560 | SlotCount(p.fOperand->fType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 561 | break; |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 562 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 563 | default: |
| 564 | SkASSERT(false); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p) { |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 569 | switch (p.fOperator) { |
| 570 | case Token::Kind::PLUSPLUS: // fall through |
| 571 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 572 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 573 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 574 | lvalue->load(); |
| 575 | this->write(ByteCodeInstruction::kDup); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 576 | this->write(ByteCodeInstruction::kPushImmediate); |
| 577 | this->write32(type_category(p.fType) == TypeCategory::kFloat |
| 578 | ? Interpreter::Value(1.0f).fUnsigned : 1); |
| 579 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 580 | this->writeTypedInstruction(p.fType, |
| 581 | ByteCodeInstruction::kAddI, |
| 582 | ByteCodeInstruction::kAddI, |
| 583 | ByteCodeInstruction::kAddF, |
| 584 | 1); |
| 585 | } else { |
| 586 | this->writeTypedInstruction(p.fType, |
| 587 | ByteCodeInstruction::kSubtractI, |
| 588 | ByteCodeInstruction::kSubtractI, |
| 589 | ByteCodeInstruction::kSubtractF, |
| 590 | 1); |
| 591 | } |
| 592 | lvalue->store(); |
| 593 | this->write(ByteCodeInstruction::kPop); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 594 | break; |
| 595 | } |
| 596 | default: |
| 597 | SkASSERT(false); |
| 598 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void ByteCodeGenerator::writeSwizzle(const Swizzle& s) { |
| 602 | switch (s.fBase->fKind) { |
| 603 | case Expression::kVariableReference_Kind: { |
| 604 | const Variable& var = ((VariableReference&) *s.fBase).fVariable; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 605 | this->write(var.fStorage == Variable::kGlobal_Storage |
| 606 | ? ByteCodeInstruction::kLoadSwizzleGlobal |
| 607 | : ByteCodeInstruction::kLoadSwizzle); |
| 608 | this->write8(this->getLocation(var)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 609 | this->write8(s.fComponents.size()); |
| 610 | for (int c : s.fComponents) { |
| 611 | this->write8(c); |
| 612 | } |
| 613 | break; |
| 614 | } |
| 615 | default: |
| 616 | this->writeExpression(*s.fBase); |
| 617 | this->write(ByteCodeInstruction::kSwizzle); |
| 618 | this->write8(s.fBase->fType.columns()); |
| 619 | this->write8(s.fComponents.size()); |
| 620 | for (int c : s.fComponents) { |
| 621 | this->write8(c); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 626 | void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) { |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 627 | this->writeExpression(*t.fTest); |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 628 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 629 | DeferredLocation trueLocation(this); |
| 630 | this->writeExpression(*t.fIfFalse); |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 631 | this->write(ByteCodeInstruction::kBranch); |
| 632 | DeferredLocation endLocation(this); |
| 633 | trueLocation.set(); |
| 634 | this->writeExpression(*t.fIfTrue); |
| 635 | endLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void ByteCodeGenerator::writeExpression(const Expression& e) { |
| 639 | switch (e.fKind) { |
| 640 | case Expression::kBinary_Kind: |
| 641 | this->writeBinaryExpression((BinaryExpression&) e); |
| 642 | break; |
| 643 | case Expression::kBoolLiteral_Kind: |
| 644 | this->writeBoolLiteral((BoolLiteral&) e); |
| 645 | break; |
| 646 | case Expression::kConstructor_Kind: |
| 647 | this->writeConstructor((Constructor&) e); |
| 648 | break; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 649 | case Expression::kExternalFunctionCall_Kind: |
| 650 | this->writeExternalFunctionCall((ExternalFunctionCall&) e); |
| 651 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 652 | case Expression::kExternalValue_Kind: |
| 653 | this->writeExternalValue((ExternalValueReference&) e); |
| 654 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 655 | case Expression::kFieldAccess_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 656 | case Expression::kIndex_Kind: |
| 657 | case Expression::kVariableReference_Kind: |
| 658 | this->writeVariableExpression(e); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 659 | break; |
| 660 | case Expression::kFloatLiteral_Kind: |
| 661 | this->writeFloatLiteral((FloatLiteral&) e); |
| 662 | break; |
| 663 | case Expression::kFunctionCall_Kind: |
| 664 | this->writeFunctionCall((FunctionCall&) e); |
| 665 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 666 | case Expression::kIntLiteral_Kind: |
| 667 | this->writeIntLiteral((IntLiteral&) e); |
| 668 | break; |
| 669 | case Expression::kNullLiteral_Kind: |
| 670 | this->writeNullLiteral((NullLiteral&) e); |
| 671 | break; |
| 672 | case Expression::kPrefix_Kind: |
| 673 | this->writePrefixExpression((PrefixExpression&) e); |
| 674 | break; |
| 675 | case Expression::kPostfix_Kind: |
| 676 | this->writePostfixExpression((PostfixExpression&) e); |
| 677 | break; |
| 678 | case Expression::kSwizzle_Kind: |
| 679 | this->writeSwizzle((Swizzle&) e); |
| 680 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 681 | case Expression::kTernary_Kind: |
| 682 | this->writeTernaryExpression((TernaryExpression&) e); |
| 683 | break; |
| 684 | default: |
| 685 | printf("unsupported expression %s\n", e.description().c_str()); |
| 686 | SkASSERT(false); |
| 687 | } |
| 688 | } |
| 689 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 690 | class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue { |
| 691 | public: |
| 692 | ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index) |
| 693 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 694 | , fCount(ByteCodeGenerator::SlotCount(value.type())) |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 695 | , fIndex(index) {} |
| 696 | |
| 697 | void load() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 698 | fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 699 | fGenerator.write8(fIndex); |
| 700 | } |
| 701 | |
| 702 | void store() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 703 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount)); |
| 704 | fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 705 | fGenerator.write8(fIndex); |
| 706 | } |
| 707 | |
| 708 | private: |
| 709 | typedef LValue INHERITED; |
| 710 | |
| 711 | int fCount; |
| 712 | |
| 713 | int fIndex; |
| 714 | }; |
| 715 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 716 | class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue { |
| 717 | public: |
| 718 | ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle) |
| 719 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 720 | , fSwizzle(swizzle) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 721 | |
| 722 | void load() override { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 723 | fGenerator.writeSwizzle(fSwizzle); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void store() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 727 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, |
| 728 | fSwizzle.fComponents.size())); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 729 | Variable::Storage storage; |
| 730 | int location = fGenerator.getLocation(*fSwizzle.fBase, &storage); |
| 731 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 732 | if (location < 0) { |
| 733 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleIndirectGlobal |
| 734 | : ByteCodeInstruction::kStoreSwizzleIndirect); |
| 735 | } else { |
| 736 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleGlobal |
| 737 | : ByteCodeInstruction::kStoreSwizzle); |
| 738 | fGenerator.write8(location); |
| 739 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 740 | fGenerator.write8(fSwizzle.fComponents.size()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 741 | for (int c : fSwizzle.fComponents) { |
| 742 | fGenerator.write8(c); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | private: |
| 747 | const Swizzle& fSwizzle; |
| 748 | |
| 749 | typedef LValue INHERITED; |
| 750 | }; |
| 751 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 752 | class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 753 | public: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 754 | ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 755 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 756 | , fExpression(expr) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 757 | |
| 758 | void load() override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 759 | fGenerator.writeVariableExpression(fExpression); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | void store() override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 763 | int count = ByteCodeGenerator::SlotCount(fExpression.fType); |
| 764 | if (count > 4) { |
| 765 | fGenerator.write(ByteCodeInstruction::kDupN); |
| 766 | fGenerator.write8(count); |
| 767 | } else { |
| 768 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count)); |
| 769 | } |
| 770 | Variable::Storage storage; |
| 771 | int location = fGenerator.getLocation(fExpression, &storage); |
| 772 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 773 | if (location < 0 || count > 4) { |
| 774 | if (location >= 0) { |
| 775 | fGenerator.write(ByteCodeInstruction::kPushImmediate); |
| 776 | fGenerator.write32(location); |
| 777 | } |
| 778 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreExtendedGlobal |
| 779 | : ByteCodeInstruction::kStoreExtended); |
| 780 | fGenerator.write8(count); |
| 781 | } else { |
| 782 | fGenerator.write(vector_instruction(isGlobal ? ByteCodeInstruction::kStoreGlobal |
| 783 | : ByteCodeInstruction::kStore, |
| 784 | count)); |
| 785 | fGenerator.write8(location); |
| 786 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | private: |
| 790 | typedef LValue INHERITED; |
| 791 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 792 | const Expression& fExpression; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 793 | }; |
| 794 | |
| 795 | std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) { |
| 796 | switch (e.fKind) { |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 797 | case Expression::kExternalValue_Kind: { |
| 798 | ExternalValue* value = ((ExternalValueReference&) e).fValue; |
| 799 | int index = fOutput->fExternalValues.size(); |
| 800 | fOutput->fExternalValues.push_back(value); |
| 801 | SkASSERT(index <= 255); |
| 802 | return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index)); |
| 803 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 804 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 805 | case Expression::kIndex_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 806 | case Expression::kVariableReference_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 807 | return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 808 | case Expression::kSwizzle_Kind: |
| 809 | return std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, (Swizzle&) e)); |
| 810 | case Expression::kTernary_Kind: |
| 811 | default: |
| 812 | printf("unsupported lvalue %s\n", e.description().c_str()); |
| 813 | return nullptr; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | void ByteCodeGenerator::writeBlock(const Block& b) { |
| 818 | for (const auto& s : b.fStatements) { |
| 819 | this->writeStatement(*s); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | void ByteCodeGenerator::setBreakTargets() { |
| 824 | std::vector<DeferredLocation>& breaks = fBreakTargets.top(); |
| 825 | for (DeferredLocation& b : breaks) { |
| 826 | b.set(); |
| 827 | } |
| 828 | fBreakTargets.pop(); |
| 829 | } |
| 830 | |
| 831 | void ByteCodeGenerator::setContinueTargets() { |
| 832 | std::vector<DeferredLocation>& continues = fContinueTargets.top(); |
| 833 | for (DeferredLocation& c : continues) { |
| 834 | c.set(); |
| 835 | } |
| 836 | fContinueTargets.pop(); |
| 837 | } |
| 838 | |
| 839 | void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) { |
| 840 | this->write(ByteCodeInstruction::kBranch); |
| 841 | fBreakTargets.top().emplace_back(this); |
| 842 | } |
| 843 | |
| 844 | void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) { |
| 845 | this->write(ByteCodeInstruction::kBranch); |
| 846 | fContinueTargets.top().emplace_back(this); |
| 847 | } |
| 848 | |
| 849 | void ByteCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 850 | fContinueTargets.emplace(); |
| 851 | fBreakTargets.emplace(); |
| 852 | size_t start = fCode->size(); |
| 853 | this->writeStatement(*d.fStatement); |
| 854 | this->setContinueTargets(); |
| 855 | this->writeExpression(*d.fTest); |
| 856 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 857 | this->write16(start); |
| 858 | this->setBreakTargets(); |
| 859 | } |
| 860 | |
| 861 | void ByteCodeGenerator::writeForStatement(const ForStatement& f) { |
| 862 | fContinueTargets.emplace(); |
| 863 | fBreakTargets.emplace(); |
| 864 | if (f.fInitializer) { |
| 865 | this->writeStatement(*f.fInitializer); |
| 866 | } |
| 867 | size_t start = fCode->size(); |
| 868 | if (f.fTest) { |
| 869 | this->writeExpression(*f.fTest); |
| 870 | this->write(ByteCodeInstruction::kNot); |
| 871 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 872 | DeferredLocation endLocation(this); |
| 873 | this->writeStatement(*f.fStatement); |
| 874 | this->setContinueTargets(); |
| 875 | if (f.fNext) { |
| 876 | this->writeExpression(*f.fNext); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 877 | this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType))); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 878 | } |
| 879 | this->write(ByteCodeInstruction::kBranch); |
| 880 | this->write16(start); |
| 881 | endLocation.set(); |
| 882 | } else { |
| 883 | this->writeStatement(*f.fStatement); |
| 884 | this->setContinueTargets(); |
| 885 | if (f.fNext) { |
| 886 | this->writeExpression(*f.fNext); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 887 | this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType))); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 888 | } |
| 889 | this->write(ByteCodeInstruction::kBranch); |
| 890 | this->write16(start); |
| 891 | } |
| 892 | this->setBreakTargets(); |
| 893 | } |
| 894 | |
| 895 | void ByteCodeGenerator::writeIfStatement(const IfStatement& i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 896 | if (i.fIfFalse) { |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 897 | // if (test) { ..ifTrue.. } else { .. ifFalse .. } |
| 898 | this->writeExpression(*i.fTest); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 899 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 900 | DeferredLocation trueLocation(this); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 901 | this->writeStatement(*i.fIfFalse); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 902 | this->write(ByteCodeInstruction::kBranch); |
| 903 | DeferredLocation endLocation(this); |
| 904 | trueLocation.set(); |
| 905 | this->writeStatement(*i.fIfTrue); |
| 906 | endLocation.set(); |
| 907 | } else { |
| 908 | // if (test) { ..ifTrue.. } |
| 909 | this->writeExpression(*i.fTest); |
| 910 | this->write(ByteCodeInstruction::kNot); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 911 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 912 | DeferredLocation endLocation(this); |
| 913 | this->writeStatement(*i.fIfTrue); |
| 914 | endLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 915 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 919 | this->writeExpression(*r.fExpression); |
| 920 | this->write(ByteCodeInstruction::kReturn); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 921 | this->write8(SlotCount(r.fExpression->fType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) { |
| 925 | // not yet implemented |
| 926 | abort(); |
| 927 | } |
| 928 | |
| 929 | void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) { |
| 930 | for (const auto& declStatement : v.fVars) { |
| 931 | const VarDeclaration& decl = (VarDeclaration&) *declStatement; |
| 932 | // we need to grab the location even if we don't use it, to ensure it |
| 933 | // has been allocated |
| 934 | int location = getLocation(*decl.fVar); |
| 935 | if (decl.fValue) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 936 | this->writeExpression(*decl.fValue); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 937 | int count = SlotCount(decl.fValue->fType); |
| 938 | if (count > 4) { |
| 939 | this->write(ByteCodeInstruction::kPushImmediate); |
| 940 | this->write32(location); |
| 941 | this->write(ByteCodeInstruction::kStoreExtended); |
| 942 | this->write8(count); |
| 943 | } else { |
| 944 | this->write(vector_instruction(ByteCodeInstruction::kStore, count)); |
| 945 | this->write8(location); |
| 946 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) { |
| 952 | fContinueTargets.emplace(); |
| 953 | fBreakTargets.emplace(); |
| 954 | size_t start = fCode->size(); |
| 955 | this->writeExpression(*w.fTest); |
| 956 | this->write(ByteCodeInstruction::kNot); |
| 957 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 958 | DeferredLocation endLocation(this); |
| 959 | this->writeStatement(*w.fStatement); |
| 960 | this->setContinueTargets(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 961 | this->write(ByteCodeInstruction::kBranch); |
| 962 | this->write16(start); |
| 963 | endLocation.set(); |
| 964 | this->setBreakTargets(); |
| 965 | } |
| 966 | |
| 967 | void ByteCodeGenerator::writeStatement(const Statement& s) { |
| 968 | switch (s.fKind) { |
| 969 | case Statement::kBlock_Kind: |
| 970 | this->writeBlock((Block&) s); |
| 971 | break; |
| 972 | case Statement::kBreak_Kind: |
| 973 | this->writeBreakStatement((BreakStatement&) s); |
| 974 | break; |
| 975 | case Statement::kContinue_Kind: |
| 976 | this->writeContinueStatement((ContinueStatement&) s); |
| 977 | break; |
| 978 | case Statement::kDiscard_Kind: |
| 979 | // not yet implemented |
| 980 | abort(); |
| 981 | case Statement::kDo_Kind: |
| 982 | this->writeDoStatement((DoStatement&) s); |
| 983 | break; |
| 984 | case Statement::kExpression_Kind: { |
| 985 | const Expression& expr = *((ExpressionStatement&) s).fExpression; |
| 986 | this->writeExpression(expr); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 987 | int count = SlotCount(expr.fType); |
| 988 | if (count > 4) { |
| 989 | this->write(ByteCodeInstruction::kPopN); |
| 990 | this->write8(count); |
| 991 | } else { |
| 992 | this->write(vector_instruction(ByteCodeInstruction::kPop, count)); |
| 993 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 994 | break; |
| 995 | } |
| 996 | case Statement::kFor_Kind: |
| 997 | this->writeForStatement((ForStatement&) s); |
| 998 | break; |
| 999 | case Statement::kIf_Kind: |
| 1000 | this->writeIfStatement((IfStatement&) s); |
| 1001 | break; |
| 1002 | case Statement::kNop_Kind: |
| 1003 | break; |
| 1004 | case Statement::kReturn_Kind: |
| 1005 | this->writeReturnStatement((ReturnStatement&) s); |
| 1006 | break; |
| 1007 | case Statement::kSwitch_Kind: |
| 1008 | this->writeSwitchStatement((SwitchStatement&) s); |
| 1009 | break; |
| 1010 | case Statement::kVarDeclarations_Kind: |
| 1011 | this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration); |
| 1012 | break; |
| 1013 | case Statement::kWhile_Kind: |
| 1014 | this->writeWhileStatement((WhileStatement&) s); |
| 1015 | break; |
| 1016 | default: |
| 1017 | SkASSERT(false); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | } |