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" |
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 | */ |
| 74 | virtual void store() = 0; |
| 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 | |
| 91 | void write(ByteCodeInstruction inst); |
| 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 | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 97 | ByteCodeInstruction f, int count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 98 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 99 | private: |
| 100 | // reserves 16 bits in the output code, to be filled in later with an address once we determine |
| 101 | // it |
| 102 | class DeferredLocation { |
| 103 | public: |
| 104 | DeferredLocation(ByteCodeGenerator* generator) |
| 105 | : fGenerator(*generator) |
| 106 | , fOffset(generator->fCode->size()) { |
| 107 | generator->write16(0); |
| 108 | } |
| 109 | |
| 110 | #ifdef SK_DEBUG |
| 111 | ~DeferredLocation() { |
| 112 | SkASSERT(fSet); |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | void set() { |
| 117 | int target = fGenerator.fCode->size(); |
| 118 | SkASSERT(target <= 65535); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 119 | (*fGenerator.fCode)[fOffset] = target; |
| 120 | (*fGenerator.fCode)[fOffset + 1] = target >> 8; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 121 | #ifdef SK_DEBUG |
| 122 | fSet = true; |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | ByteCodeGenerator& fGenerator; |
| 128 | size_t fOffset; |
| 129 | #ifdef SK_DEBUG |
| 130 | bool fSet = false; |
| 131 | #endif |
| 132 | }; |
| 133 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 134 | class DeferredCallTarget { |
| 135 | public: |
| 136 | DeferredCallTarget(ByteCodeGenerator* generator, const FunctionDeclaration& function) |
| 137 | : fGenerator(*generator) |
| 138 | , fCode(generator->fCode) |
| 139 | , fOffset(generator->fCode->size()) |
| 140 | , fFunction(function) { |
| 141 | generator->write8(0); |
| 142 | } |
| 143 | |
| 144 | bool set() { |
| 145 | size_t idx; |
| 146 | const auto& functions(fGenerator.fOutput->fFunctions); |
| 147 | for (idx = 0; idx < functions.size(); ++idx) { |
| 148 | if (fFunction.matches(functions[idx]->fDeclaration)) { |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | if (idx > 255 || idx > functions.size()) { |
| 153 | SkASSERT(false); |
| 154 | return false; |
| 155 | } |
| 156 | (*fCode)[fOffset] = idx; |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | ByteCodeGenerator& fGenerator; |
| 162 | std::vector<uint8_t>* fCode; |
| 163 | size_t fOffset; |
| 164 | const FunctionDeclaration& fFunction; |
| 165 | }; |
| 166 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 167 | /** |
| 168 | * Returns the local slot into which var should be stored, allocating a new slot if it has not |
| 169 | * already been assigned one. Compound variables (e.g. vectors) will consume more than one local |
| 170 | * slot, with the getLocation return value indicating where the first element should be stored. |
| 171 | */ |
| 172 | int getLocation(const Variable& var); |
| 173 | |
| 174 | std::unique_ptr<ByteCodeFunction> writeFunction(const FunctionDefinition& f); |
| 175 | |
| 176 | void writeVarDeclarations(const VarDeclarations& decl); |
| 177 | |
| 178 | void writeVariableReference(const VariableReference& ref); |
| 179 | |
| 180 | void writeExpression(const Expression& expr); |
| 181 | |
| 182 | /** |
| 183 | * Pushes whatever values are required by the lvalue onto the stack, and returns an LValue |
| 184 | * permitting loads and stores to it. |
| 185 | */ |
| 186 | std::unique_ptr<LValue> getLValue(const Expression& expr); |
| 187 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame^] | 188 | void writeIntrinsicCall(const FunctionCall& c); |
| 189 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 190 | void writeFunctionCall(const FunctionCall& c); |
| 191 | |
| 192 | void writeConstructor(const Constructor& c); |
| 193 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 194 | void writeExternalFunctionCall(const ExternalFunctionCall& c); |
| 195 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 196 | void writeExternalValue(const ExternalValueReference& r); |
| 197 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 198 | void writeFieldAccess(const FieldAccess& f); |
| 199 | |
| 200 | void writeSwizzle(const Swizzle& swizzle); |
| 201 | |
| 202 | void writeBinaryExpression(const BinaryExpression& b); |
| 203 | |
| 204 | void writeTernaryExpression(const TernaryExpression& t); |
| 205 | |
| 206 | void writeIndexExpression(const IndexExpression& expr); |
| 207 | |
| 208 | void writeLogicalAnd(const BinaryExpression& b); |
| 209 | |
| 210 | void writeLogicalOr(const BinaryExpression& o); |
| 211 | |
| 212 | void writeNullLiteral(const NullLiteral& n); |
| 213 | |
| 214 | void writePrefixExpression(const PrefixExpression& p); |
| 215 | |
| 216 | void writePostfixExpression(const PostfixExpression& p); |
| 217 | |
| 218 | void writeBoolLiteral(const BoolLiteral& b); |
| 219 | |
| 220 | void writeIntLiteral(const IntLiteral& i); |
| 221 | |
| 222 | void writeFloatLiteral(const FloatLiteral& f); |
| 223 | |
| 224 | void writeStatement(const Statement& s); |
| 225 | |
| 226 | void writeBlock(const Block& b); |
| 227 | |
| 228 | void writeBreakStatement(const BreakStatement& b); |
| 229 | |
| 230 | void writeContinueStatement(const ContinueStatement& c); |
| 231 | |
| 232 | void writeIfStatement(const IfStatement& stmt); |
| 233 | |
| 234 | void writeForStatement(const ForStatement& f); |
| 235 | |
| 236 | void writeWhileStatement(const WhileStatement& w); |
| 237 | |
| 238 | void writeDoStatement(const DoStatement& d); |
| 239 | |
| 240 | void writeSwitchStatement(const SwitchStatement& s); |
| 241 | |
| 242 | void writeReturnStatement(const ReturnStatement& r); |
| 243 | |
| 244 | // updates the current set of breaks to branch to the current location |
| 245 | void setBreakTargets(); |
| 246 | |
| 247 | // updates the current set of continues to branch to the current location |
| 248 | void setContinueTargets(); |
| 249 | |
| 250 | const Context& fContext; |
| 251 | |
| 252 | ByteCode* fOutput; |
| 253 | |
| 254 | const FunctionDefinition* fFunction; |
| 255 | |
| 256 | std::vector<uint8_t>* fCode; |
| 257 | |
| 258 | std::vector<const Variable*> fLocals; |
| 259 | |
| 260 | std::stack<std::vector<DeferredLocation>> fContinueTargets; |
| 261 | |
| 262 | std::stack<std::vector<DeferredLocation>> fBreakTargets; |
| 263 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 264 | std::vector<DeferredCallTarget> fCallTargets; |
| 265 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 266 | int fParameterCount; |
| 267 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame^] | 268 | std::unordered_map<String, ByteCodeInstruction> fIntrinsics; |
| 269 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 270 | friend class DeferredLocation; |
| 271 | friend class ByteCodeVariableLValue; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 272 | friend class ByteCodeSwizzleLValue; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 273 | |
| 274 | typedef CodeGenerator INHERITED; |
| 275 | }; |
| 276 | |
| 277 | } |
| 278 | |
| 279 | #endif |