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 | |
| 11 | #include <stack> |
| 12 | #include <tuple> |
| 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" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLExternalValueReference.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 27 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 28 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 29 | #include "src/sksl/ir/SkSLForStatement.h" |
| 30 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 31 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 32 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 33 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 34 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 35 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 36 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 37 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 38 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 39 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 40 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 41 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 42 | #include "src/sksl/ir/SkSLStatement.h" |
| 43 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 44 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 45 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 46 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 47 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 48 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 49 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 50 | #include "src/sksl/spirv.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 51 | |
| 52 | namespace SkSL { |
| 53 | |
| 54 | class ByteCodeGenerator : public CodeGenerator { |
| 55 | public: |
| 56 | class LValue { |
| 57 | public: |
| 58 | LValue(ByteCodeGenerator& generator) |
| 59 | : fGenerator(generator) {} |
| 60 | |
| 61 | virtual ~LValue() {} |
| 62 | |
| 63 | /** |
| 64 | * Stack before call: ... lvalue |
| 65 | * Stack after call: ... lvalue load |
| 66 | */ |
| 67 | virtual void load() = 0; |
| 68 | |
| 69 | /** |
| 70 | * Stack before call: ... lvalue value |
| 71 | * Stack after call: ... |
| 72 | */ |
| 73 | virtual void store() = 0; |
| 74 | |
| 75 | protected: |
| 76 | ByteCodeGenerator& fGenerator; |
| 77 | }; |
| 78 | |
| 79 | ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 80 | ByteCode* output) |
| 81 | : INHERITED(program, errors, nullptr) |
| 82 | , fContext(*context) |
| 83 | , fOutput(output) {} |
| 84 | |
| 85 | bool generateCode() override; |
| 86 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 87 | void align(int divisor, int remainder); |
| 88 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 89 | void write8(uint8_t b); |
| 90 | |
| 91 | void write16(uint16_t b); |
| 92 | |
| 93 | void write32(uint32_t b); |
| 94 | |
| 95 | void write(ByteCodeInstruction inst); |
| 96 | |
| 97 | /** |
| 98 | * Based on 'type', writes the s (signed), u (unsigned), or f (float) instruction. |
| 99 | */ |
| 100 | void writeTypedInstruction(const Type& type, ByteCodeInstruction s, ByteCodeInstruction u, |
| 101 | ByteCodeInstruction f); |
| 102 | |
| 103 | /** |
| 104 | * Pushes the storage location of an lvalue to the stack. |
| 105 | */ |
| 106 | void writeTarget(const Expression& expr); |
| 107 | |
| 108 | private: |
| 109 | // reserves 16 bits in the output code, to be filled in later with an address once we determine |
| 110 | // it |
| 111 | class DeferredLocation { |
| 112 | public: |
| 113 | DeferredLocation(ByteCodeGenerator* generator) |
| 114 | : fGenerator(*generator) |
| 115 | , fOffset(generator->fCode->size()) { |
| 116 | generator->write16(0); |
| 117 | } |
| 118 | |
| 119 | #ifdef SK_DEBUG |
| 120 | ~DeferredLocation() { |
| 121 | SkASSERT(fSet); |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | void set() { |
| 126 | int target = fGenerator.fCode->size(); |
| 127 | SkASSERT(target <= 65535); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 128 | (*fGenerator.fCode)[fOffset] = target; |
| 129 | (*fGenerator.fCode)[fOffset + 1] = target >> 8; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 130 | #ifdef SK_DEBUG |
| 131 | fSet = true; |
| 132 | #endif |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | ByteCodeGenerator& fGenerator; |
| 137 | size_t fOffset; |
| 138 | #ifdef SK_DEBUG |
| 139 | bool fSet = false; |
| 140 | #endif |
| 141 | }; |
| 142 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 143 | class DeferredCallTarget { |
| 144 | public: |
| 145 | DeferredCallTarget(ByteCodeGenerator* generator, const FunctionDeclaration& function) |
| 146 | : fGenerator(*generator) |
| 147 | , fCode(generator->fCode) |
| 148 | , fOffset(generator->fCode->size()) |
| 149 | , fFunction(function) { |
| 150 | generator->write8(0); |
| 151 | } |
| 152 | |
| 153 | bool set() { |
| 154 | size_t idx; |
| 155 | const auto& functions(fGenerator.fOutput->fFunctions); |
| 156 | for (idx = 0; idx < functions.size(); ++idx) { |
| 157 | if (fFunction.matches(functions[idx]->fDeclaration)) { |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | if (idx > 255 || idx > functions.size()) { |
| 162 | SkASSERT(false); |
| 163 | return false; |
| 164 | } |
| 165 | (*fCode)[fOffset] = idx; |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | ByteCodeGenerator& fGenerator; |
| 171 | std::vector<uint8_t>* fCode; |
| 172 | size_t fOffset; |
| 173 | const FunctionDeclaration& fFunction; |
| 174 | }; |
| 175 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 176 | /** |
| 177 | * Returns the local slot into which var should be stored, allocating a new slot if it has not |
| 178 | * already been assigned one. Compound variables (e.g. vectors) will consume more than one local |
| 179 | * slot, with the getLocation return value indicating where the first element should be stored. |
| 180 | */ |
| 181 | int getLocation(const Variable& var); |
| 182 | |
| 183 | std::unique_ptr<ByteCodeFunction> writeFunction(const FunctionDefinition& f); |
| 184 | |
| 185 | void writeVarDeclarations(const VarDeclarations& decl); |
| 186 | |
| 187 | void writeVariableReference(const VariableReference& ref); |
| 188 | |
| 189 | void writeExpression(const Expression& expr); |
| 190 | |
| 191 | /** |
| 192 | * Pushes whatever values are required by the lvalue onto the stack, and returns an LValue |
| 193 | * permitting loads and stores to it. |
| 194 | */ |
| 195 | std::unique_ptr<LValue> getLValue(const Expression& expr); |
| 196 | |
| 197 | void writeFunctionCall(const FunctionCall& c); |
| 198 | |
| 199 | void writeConstructor(const Constructor& c); |
| 200 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 201 | void writeExternalValue(const ExternalValueReference& r); |
| 202 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 203 | void writeFieldAccess(const FieldAccess& f); |
| 204 | |
| 205 | void writeSwizzle(const Swizzle& swizzle); |
| 206 | |
| 207 | void writeBinaryExpression(const BinaryExpression& b); |
| 208 | |
| 209 | void writeTernaryExpression(const TernaryExpression& t); |
| 210 | |
| 211 | void writeIndexExpression(const IndexExpression& expr); |
| 212 | |
| 213 | void writeLogicalAnd(const BinaryExpression& b); |
| 214 | |
| 215 | void writeLogicalOr(const BinaryExpression& o); |
| 216 | |
| 217 | void writeNullLiteral(const NullLiteral& n); |
| 218 | |
| 219 | void writePrefixExpression(const PrefixExpression& p); |
| 220 | |
| 221 | void writePostfixExpression(const PostfixExpression& p); |
| 222 | |
| 223 | void writeBoolLiteral(const BoolLiteral& b); |
| 224 | |
| 225 | void writeIntLiteral(const IntLiteral& i); |
| 226 | |
| 227 | void writeFloatLiteral(const FloatLiteral& f); |
| 228 | |
| 229 | void writeStatement(const Statement& s); |
| 230 | |
| 231 | void writeBlock(const Block& b); |
| 232 | |
| 233 | void writeBreakStatement(const BreakStatement& b); |
| 234 | |
| 235 | void writeContinueStatement(const ContinueStatement& c); |
| 236 | |
| 237 | void writeIfStatement(const IfStatement& stmt); |
| 238 | |
| 239 | void writeForStatement(const ForStatement& f); |
| 240 | |
| 241 | void writeWhileStatement(const WhileStatement& w); |
| 242 | |
| 243 | void writeDoStatement(const DoStatement& d); |
| 244 | |
| 245 | void writeSwitchStatement(const SwitchStatement& s); |
| 246 | |
| 247 | void writeReturnStatement(const ReturnStatement& r); |
| 248 | |
| 249 | // updates the current set of breaks to branch to the current location |
| 250 | void setBreakTargets(); |
| 251 | |
| 252 | // updates the current set of continues to branch to the current location |
| 253 | void setContinueTargets(); |
| 254 | |
| 255 | const Context& fContext; |
| 256 | |
| 257 | ByteCode* fOutput; |
| 258 | |
| 259 | const FunctionDefinition* fFunction; |
| 260 | |
| 261 | std::vector<uint8_t>* fCode; |
| 262 | |
| 263 | std::vector<const Variable*> fLocals; |
| 264 | |
| 265 | std::stack<std::vector<DeferredLocation>> fContinueTargets; |
| 266 | |
| 267 | std::stack<std::vector<DeferredLocation>> fBreakTargets; |
| 268 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 269 | std::vector<DeferredCallTarget> fCallTargets; |
| 270 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 271 | int fParameterCount; |
| 272 | |
| 273 | friend class DeferredLocation; |
| 274 | friend class ByteCodeVariableLValue; |
| 275 | |
| 276 | typedef CodeGenerator INHERITED; |
| 277 | }; |
| 278 | |
| 279 | } |
| 280 | |
| 281 | #endif |