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