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 | |
| 8 | #ifndef SKSL_BYTECODEGENERATOR |
| 9 | #define SKSL_BYTECODEGENERATOR |
| 10 | |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 11 | #include <algorithm> |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 12 | #include <stack> |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 13 | #include <unordered_map> |
| 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/sksl/SkSLByteCode.h" |
| 16 | #include "src/sksl/SkSLCodeGenerator.h" |
| 17 | #include "src/sksl/SkSLMemoryLayout.h" |
| 18 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 19 | #include "src/sksl/ir/SkSLBlock.h" |
| 20 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 21 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 22 | #include "src/sksl/ir/SkSLConstructor.h" |
| 23 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 24 | #include "src/sksl/ir/SkSLDoStatement.h" |
Mike Klein | fe0aeb3 | 2019-05-20 10:55:11 -0500 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLExternalValueReference.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 29 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 30 | #include "src/sksl/ir/SkSLForStatement.h" |
| 31 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 32 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 33 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 34 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 35 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 36 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 37 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 38 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 39 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 40 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 41 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 42 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 43 | #include "src/sksl/ir/SkSLStatement.h" |
| 44 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 45 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 46 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 47 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 48 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 49 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 50 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 51 | #include "src/sksl/spirv.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 52 | |
| 53 | namespace SkSL { |
| 54 | |
| 55 | class ByteCodeGenerator : public CodeGenerator { |
| 56 | public: |
| 57 | class LValue { |
| 58 | public: |
| 59 | LValue(ByteCodeGenerator& generator) |
| 60 | : fGenerator(generator) {} |
| 61 | |
| 62 | virtual ~LValue() {} |
| 63 | |
| 64 | /** |
| 65 | * Stack before call: ... lvalue |
| 66 | * Stack after call: ... lvalue load |
| 67 | */ |
| 68 | virtual void load() = 0; |
| 69 | |
| 70 | /** |
| 71 | * Stack before call: ... lvalue value |
| 72 | * Stack after call: ... |
| 73 | */ |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 74 | virtual void store(bool discard) = 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 75 | |
| 76 | protected: |
| 77 | ByteCodeGenerator& fGenerator; |
| 78 | }; |
| 79 | |
| 80 | ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 81 | ByteCode* output); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 82 | |
| 83 | bool generateCode() override; |
| 84 | |
| 85 | void write8(uint8_t b); |
| 86 | |
| 87 | void write16(uint16_t b); |
| 88 | |
| 89 | void write32(uint32_t b); |
| 90 | |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 91 | void write(ByteCodeInstruction inst, int count = kUnusedStackCount); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Based on 'type', writes the s (signed), u (unsigned), or f (float) instruction. |
| 95 | */ |
| 96 | void writeTypedInstruction(const Type& type, ByteCodeInstruction s, ByteCodeInstruction u, |
Ethan Nicholas | c70027b | 2019-09-05 16:50:52 -0400 | [diff] [blame] | 97 | ByteCodeInstruction f, int count, bool writeCount = true); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 98 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 99 | static int SlotCount(const Type& type); |
| 100 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 101 | private: |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 102 | static constexpr int kUnusedStackCount = INT32_MAX; |
| 103 | static int StackUsage(ByteCodeInstruction, int count); |
| 104 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 105 | // reserves 16 bits in the output code, to be filled in later with an address once we determine |
| 106 | // it |
| 107 | class DeferredLocation { |
| 108 | public: |
| 109 | DeferredLocation(ByteCodeGenerator* generator) |
| 110 | : fGenerator(*generator) |
| 111 | , fOffset(generator->fCode->size()) { |
| 112 | generator->write16(0); |
| 113 | } |
| 114 | |
| 115 | #ifdef SK_DEBUG |
| 116 | ~DeferredLocation() { |
| 117 | SkASSERT(fSet); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | void set() { |
| 122 | int target = fGenerator.fCode->size(); |
| 123 | SkASSERT(target <= 65535); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 124 | (*fGenerator.fCode)[fOffset] = target; |
| 125 | (*fGenerator.fCode)[fOffset + 1] = target >> 8; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 126 | #ifdef SK_DEBUG |
| 127 | fSet = true; |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | ByteCodeGenerator& fGenerator; |
| 133 | size_t fOffset; |
| 134 | #ifdef SK_DEBUG |
| 135 | bool fSet = false; |
| 136 | #endif |
| 137 | }; |
| 138 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 139 | // Intrinsics which do not simply map to a single opcode |
| 140 | enum class SpecialIntrinsic { |
| 141 | kDot, |
| 142 | }; |
| 143 | |
| 144 | struct Intrinsic { |
| 145 | Intrinsic(ByteCodeInstruction instruction) |
| 146 | : fIsSpecial(false) |
| 147 | , fValue(instruction) {} |
| 148 | |
| 149 | Intrinsic(SpecialIntrinsic special) |
| 150 | : fIsSpecial(true) |
| 151 | , fValue(special) {} |
| 152 | |
| 153 | bool fIsSpecial; |
| 154 | |
| 155 | union Value { |
| 156 | Value(ByteCodeInstruction instruction) |
| 157 | : fInstruction(instruction) {} |
| 158 | |
| 159 | Value(SpecialIntrinsic special) |
| 160 | : fSpecial(special) {} |
| 161 | |
| 162 | ByteCodeInstruction fInstruction; |
| 163 | SpecialIntrinsic fSpecial; |
| 164 | } fValue; |
| 165 | }; |
| 166 | |
Brian Osman | 1c110a0 | 2019-10-01 14:53:32 -0400 | [diff] [blame^] | 167 | |
| 168 | // Similar to Variable::Storage, but locals and parameters are grouped together, and globals |
| 169 | // are further subidivided into uniforms and other (writable) globals. |
| 170 | enum class Storage { |
| 171 | kLocal, // include parameters |
| 172 | kGlobal, // non-uniform globals |
| 173 | kUniform, // uniform globals |
| 174 | }; |
| 175 | |
| 176 | struct Location { |
| 177 | int fSlot; |
| 178 | Storage fStorage; |
| 179 | |
| 180 | // Not really invalid, but a "safe" placeholder to be more explicit at call-sites |
| 181 | static Location MakeInvalid() { return { 0, Storage::kLocal }; } |
| 182 | |
| 183 | Location makeOnStack() { return { -1, fStorage }; } |
| 184 | bool isOnStack() const { return fSlot < 0; } |
| 185 | |
| 186 | Location operator+(int offset) { |
| 187 | SkASSERT(fSlot >= 0); |
| 188 | return { fSlot + offset, fStorage }; |
| 189 | } |
| 190 | |
| 191 | ByteCodeInstruction selectLoad(ByteCodeInstruction local, |
| 192 | ByteCodeInstruction global, |
| 193 | ByteCodeInstruction uniform) const { |
| 194 | switch (fStorage) { |
| 195 | case Storage::kLocal: return local; |
| 196 | case Storage::kGlobal: return global; |
| 197 | case Storage::kUniform: return uniform; |
| 198 | } |
| 199 | SkUNREACHABLE; |
| 200 | } |
| 201 | |
| 202 | ByteCodeInstruction selectStore(ByteCodeInstruction local, |
| 203 | ByteCodeInstruction global) const { |
| 204 | switch (fStorage) { |
| 205 | case Storage::kLocal: return local; |
| 206 | case Storage::kGlobal: return global; |
| 207 | case Storage::kUniform: SK_ABORT("Trying to store to a uniform"); break; |
| 208 | } |
| 209 | return local; |
| 210 | } |
| 211 | }; |
| 212 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 213 | /** |
| 214 | * Returns the local slot into which var should be stored, allocating a new slot if it has not |
| 215 | * already been assigned one. Compound variables (e.g. vectors) will consume more than one local |
| 216 | * slot, with the getLocation return value indicating where the first element should be stored. |
| 217 | */ |
Brian Osman | 1c110a0 | 2019-10-01 14:53:32 -0400 | [diff] [blame^] | 218 | Location getLocation(const Variable& var); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 219 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 220 | /** |
| 221 | * As above, but computes the (possibly dynamic) address of an expression involving indexing & |
| 222 | * field access. If the address is known, it's returned. If not, -1 is returned, and the |
| 223 | * location will be left on the top of the stack. |
| 224 | */ |
Brian Osman | 1c110a0 | 2019-10-01 14:53:32 -0400 | [diff] [blame^] | 225 | Location getLocation(const Expression& expr); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 226 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 227 | std::unique_ptr<ByteCodeFunction> writeFunction(const FunctionDefinition& f); |
| 228 | |
| 229 | void writeVarDeclarations(const VarDeclarations& decl); |
| 230 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 231 | void writeVariableExpression(const Expression& expr); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 232 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 233 | void writeExpression(const Expression& expr, bool discard = false); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * Pushes whatever values are required by the lvalue onto the stack, and returns an LValue |
| 237 | * permitting loads and stores to it. |
| 238 | */ |
| 239 | std::unique_ptr<LValue> getLValue(const Expression& expr); |
| 240 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 241 | void writeIntrinsicCall(const FunctionCall& c); |
| 242 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 243 | void writeFunctionCall(const FunctionCall& c); |
| 244 | |
| 245 | void writeConstructor(const Constructor& c); |
| 246 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 247 | void writeExternalFunctionCall(const ExternalFunctionCall& c); |
| 248 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 249 | void writeExternalValue(const ExternalValueReference& r); |
| 250 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 251 | void writeSwizzle(const Swizzle& swizzle); |
| 252 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 253 | bool writeBinaryExpression(const BinaryExpression& b, bool discard); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 254 | |
| 255 | void writeTernaryExpression(const TernaryExpression& t); |
| 256 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 257 | void writeNullLiteral(const NullLiteral& n); |
| 258 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 259 | bool writePrefixExpression(const PrefixExpression& p, bool discard); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 260 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 261 | bool writePostfixExpression(const PostfixExpression& p, bool discard); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 262 | |
| 263 | void writeBoolLiteral(const BoolLiteral& b); |
| 264 | |
| 265 | void writeIntLiteral(const IntLiteral& i); |
| 266 | |
| 267 | void writeFloatLiteral(const FloatLiteral& f); |
| 268 | |
| 269 | void writeStatement(const Statement& s); |
| 270 | |
| 271 | void writeBlock(const Block& b); |
| 272 | |
| 273 | void writeBreakStatement(const BreakStatement& b); |
| 274 | |
| 275 | void writeContinueStatement(const ContinueStatement& c); |
| 276 | |
| 277 | void writeIfStatement(const IfStatement& stmt); |
| 278 | |
| 279 | void writeForStatement(const ForStatement& f); |
| 280 | |
| 281 | void writeWhileStatement(const WhileStatement& w); |
| 282 | |
| 283 | void writeDoStatement(const DoStatement& d); |
| 284 | |
| 285 | void writeSwitchStatement(const SwitchStatement& s); |
| 286 | |
| 287 | void writeReturnStatement(const ReturnStatement& r); |
| 288 | |
| 289 | // updates the current set of breaks to branch to the current location |
| 290 | void setBreakTargets(); |
| 291 | |
| 292 | // updates the current set of continues to branch to the current location |
| 293 | void setContinueTargets(); |
| 294 | |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 295 | void enterLoop() { |
| 296 | fLoopCount++; |
| 297 | fMaxLoopCount = std::max(fMaxLoopCount, fLoopCount); |
| 298 | } |
| 299 | |
| 300 | void exitLoop() { |
| 301 | SkASSERT(fLoopCount > 0); |
| 302 | fLoopCount--; |
| 303 | } |
| 304 | |
| 305 | void enterCondition() { |
| 306 | fConditionCount++; |
| 307 | fMaxConditionCount = std::max(fMaxConditionCount, fConditionCount); |
| 308 | } |
| 309 | |
| 310 | void exitCondition() { |
| 311 | SkASSERT(fConditionCount > 0); |
| 312 | fConditionCount--; |
| 313 | } |
| 314 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 315 | const Context& fContext; |
| 316 | |
| 317 | ByteCode* fOutput; |
| 318 | |
| 319 | const FunctionDefinition* fFunction; |
| 320 | |
| 321 | std::vector<uint8_t>* fCode; |
| 322 | |
| 323 | std::vector<const Variable*> fLocals; |
| 324 | |
| 325 | std::stack<std::vector<DeferredLocation>> fContinueTargets; |
| 326 | |
| 327 | std::stack<std::vector<DeferredLocation>> fBreakTargets; |
| 328 | |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 329 | std::vector<const FunctionDefinition*> fFunctions; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 330 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 331 | int fParameterCount; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 332 | int fStackCount; |
| 333 | int fMaxStackCount; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 334 | |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 335 | int fLoopCount; |
| 336 | int fMaxLoopCount; |
| 337 | int fConditionCount; |
| 338 | int fMaxConditionCount; |
| 339 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 340 | const std::unordered_map<String, Intrinsic> fIntrinsics; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 341 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 342 | friend class DeferredLocation; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 343 | friend class ByteCodeExpressionLValue; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 344 | friend class ByteCodeSwizzleLValue; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 345 | |
| 346 | typedef CodeGenerator INHERITED; |
| 347 | }; |
| 348 | |
| 349 | } |
| 350 | |
| 351 | #endif |