aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/SkSLInterpreter.cpp
blob: 5879274dd78d0522429c3a9235a22927fc911ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_STANDALONE

#include "SkSLInterpreter.h"
#include "ir/SkSLBinaryExpression.h"
#include "ir/SkSLExpressionStatement.h"
#include "ir/SkSLForStatement.h"
#include "ir/SkSLFunctionCall.h"
#include "ir/SkSLFunctionReference.h"
#include "ir/SkSLIfStatement.h"
#include "ir/SkSLIndexExpression.h"
#include "ir/SkSLPostfixExpression.h"
#include "ir/SkSLPrefixExpression.h"
#include "ir/SkSLProgram.h"
#include "ir/SkSLStatement.h"
#include "ir/SkSLTernaryExpression.h"
#include "ir/SkSLVarDeclarations.h"
#include "ir/SkSLVarDeclarationsStatement.h"
#include "ir/SkSLVariableReference.h"
#include "SkRasterPipeline.h"
#include "../jumper/SkJumper.h"

namespace SkSL {

void Interpreter::run() {
    for (const auto& e : *fProgram) {
        if (ProgramElement::kFunction_Kind == e.fKind) {
            const FunctionDefinition& f = (const FunctionDefinition&) e;
            if ("appendStages" == f.fDeclaration.fName) {
                this->run(f);
                return;
            }
        }
    }
    SkASSERT(false);
}

static int SizeOf(const Type& type) {
    return 1;
}

void Interpreter::run(const FunctionDefinition& f) {
    fVars.emplace_back();
    StackIndex current = (StackIndex) fStack.size();
    for (int i = f.fDeclaration.fParameters.size() - 1; i >= 0; --i) {
        current -= SizeOf(f.fDeclaration.fParameters[i]->fType);
        fVars.back()[f.fDeclaration.fParameters[i]] = current;
    }
    fCurrentIndex.push_back({ f.fBody.get(), 0 });
    while (fCurrentIndex.size()) {
        this->runStatement();
    }
}

void Interpreter::push(Value value) {
    fStack.push_back(value);
}

Interpreter::Value Interpreter::pop() {
    auto iter = fStack.end() - 1;
    Value result = *iter;
    fStack.erase(iter);
    return result;
}

 Interpreter::StackIndex Interpreter::stackAlloc(int count) {
    int result = fStack.size();
    for (int i = 0; i < count; ++i) {
        fStack.push_back(Value((int) 0xDEADBEEF));
    }
    return result;
}

void Interpreter::runStatement() {
    const Statement& stmt = *fCurrentIndex.back().fStatement;
    const size_t index = fCurrentIndex.back().fIndex;
    fCurrentIndex.pop_back();
    switch (stmt.fKind) {
        case Statement::kBlock_Kind: {
            const Block& b = (const Block&) stmt;
            if (!b.fStatements.size()) {
                break;
            }
            SkASSERT(index < b.fStatements.size());
            if (index < b.fStatements.size() - 1) {
                fCurrentIndex.push_back({ &b, index + 1 });
            }
            fCurrentIndex.push_back({ b.fStatements[index].get(), 0 });
            break;
        }
        case Statement::kBreak_Kind:
            SkASSERT(index == 0);
            abort();
        case Statement::kContinue_Kind:
            SkASSERT(index == 0);
            abort();
        case Statement::kDiscard_Kind:
            SkASSERT(index == 0);
            abort();
        case Statement::kDo_Kind:
            abort();
        case Statement::kExpression_Kind:
            SkASSERT(index == 0);
            this->evaluate(*((const ExpressionStatement&) stmt).fExpression);
            break;
        case Statement::kFor_Kind: {
            ForStatement& f = (ForStatement&) stmt;
            switch (index) {
                case 0:
                    // initializer
                    fCurrentIndex.push_back({ &f, 1 });
                    if (f.fInitializer) {
                        fCurrentIndex.push_back({ f.fInitializer.get(), 0 });
                    }
                    break;
                case 1:
                    // test & body
                    if (f.fTest && !evaluate(*f.fTest).fBool) {
                        break;
                    } else {
                        fCurrentIndex.push_back({ &f, 2 });
                        fCurrentIndex.push_back({ f.fStatement.get(), 0 });
                    }
                    break;
                case 2:
                    // next
                    if (f.fNext) {
                        this->evaluate(*f.fNext);
                    }
                    fCurrentIndex.push_back({ &f, 1 });
                    break;
                default:
                    SkASSERT(false);
            }
            break;
        }
        case Statement::kGroup_Kind:
            abort();
        case Statement::kIf_Kind: {
            IfStatement& i = (IfStatement&) stmt;
            if (evaluate(*i.fTest).fBool) {
                fCurrentIndex.push_back({ i.fIfTrue.get(), 0 });
            } else if (i.fIfFalse) {
                fCurrentIndex.push_back({ i.fIfFalse.get(), 0 });
            }
            break;
        }
        case Statement::kNop_Kind:
            SkASSERT(index == 0);
            break;
        case Statement::kReturn_Kind:
            SkASSERT(index == 0);
            abort();
        case Statement::kSwitch_Kind:
            abort();
        case Statement::kVarDeclarations_Kind:
            SkASSERT(index == 0);
            for (const auto& decl :((const VarDeclarationsStatement&) stmt).fDeclaration->fVars) {
                const Variable* var = ((VarDeclaration&) *decl).fVar;
                StackIndex pos = this->stackAlloc(SizeOf(var->fType));
                fVars.back()[var] = pos;
                if (var->fInitialValue) {
                    fStack[pos] = this->evaluate(*var->fInitialValue);
                }
            }
            break;
        case Statement::kWhile_Kind:
            abort();
        default:
            abort();
    }
}

static Interpreter::TypeKind type_kind(const Type& type) {
    if (type.fName == "int") {
        return Interpreter::kInt_TypeKind;
    } else if (type.fName == "float") {
        return Interpreter::kFloat_TypeKind;
    }
    ABORT("unsupported type: %s\n", type.description().c_str());
}

Interpreter::StackIndex Interpreter::getLValue(const Expression& expr) {
    switch (expr.fKind) {
        case Expression::kFieldAccess_Kind:
            break;
        case Expression::kIndex_Kind: {
            const IndexExpression& idx = (const IndexExpression&) expr;
            return this->evaluate(*idx.fBase).fInt + this->evaluate(*idx.fIndex).fInt;
        }
        case Expression::kSwizzle_Kind:
            break;
        case Expression::kVariableReference_Kind:
            SkASSERT(fVars.size());
            SkASSERT(fVars.back().find(&((VariableReference&) expr).fVariable) !=
                   fVars.back().end());
            return fVars.back()[&((VariableReference&) expr).fVariable];
        case Expression::kTernary_Kind: {
            const TernaryExpression& t = (const TernaryExpression&) expr;
            return this->getLValue(this->evaluate(*t.fTest).fBool ? *t.fIfTrue : *t.fIfFalse);
        }
        case Expression::kTypeReference_Kind:
            break;
        default:
            break;
    }
    ABORT("unsupported lvalue");
}

struct CallbackCtx : public SkJumper_CallbackCtx {
    Interpreter* fInterpreter;
    const FunctionDefinition* fFunction;
};

static void do_callback(SkJumper_CallbackCtx* raw, int activePixels) {
    CallbackCtx& ctx = (CallbackCtx&) *raw;
    for (int i = 0; i < activePixels; ++i) {
        ctx.fInterpreter->push(Interpreter::Value(ctx.rgba[i * 4 + 0]));
        ctx.fInterpreter->push(Interpreter::Value(ctx.rgba[i * 4 + 1]));
        ctx.fInterpreter->push(Interpreter::Value(ctx.rgba[i * 4 + 2]));
        ctx.fInterpreter->run(*ctx.fFunction);
        ctx.read_from[i * 4 + 2] = ctx.fInterpreter->pop().fFloat;
        ctx.read_from[i * 4 + 1] = ctx.fInterpreter->pop().fFloat;
        ctx.read_from[i * 4 + 0] = ctx.fInterpreter->pop().fFloat;
    }
}

void Interpreter::appendStage(const AppendStage& a) {
    switch (a.fStage) {
        case SkRasterPipeline::matrix_4x5: {
            SkASSERT(a.fArguments.size() == 1);
            StackIndex transpose = evaluate(*a.fArguments[0]).fInt;
            fPipeline.append(SkRasterPipeline::matrix_4x5, &fStack[transpose]);
            break;
        }
        case SkRasterPipeline::callback: {
            SkASSERT(a.fArguments.size() == 1);
            CallbackCtx* ctx = new CallbackCtx();
            ctx->fInterpreter = this;
            ctx->fn = do_callback;
            for (const auto& e : *fProgram) {
                if (ProgramElement::kFunction_Kind == e.fKind) {
                    const FunctionDefinition& f = (const FunctionDefinition&) e;
                    if (&f.fDeclaration ==
                                      ((const FunctionReference&) *a.fArguments[0]).fFunctions[0]) {
                        ctx->fFunction = &f;
                    }
                }
            }
            fPipeline.append(SkRasterPipeline::callback, ctx);
            break;
        }
        default:
            fPipeline.append(a.fStage);
    }
}

Interpreter::Value Interpreter::call(const FunctionCall& c) {
    abort();
}

Interpreter::Value Interpreter::evaluate(const Expression& expr) {
    switch (expr.fKind) {
        case Expression::kAppendStage_Kind:
            this->appendStage((const AppendStage&) expr);
            return Value((int) 0xDEADBEEF);
        case Expression::kBinary_Kind: {
            #define ARITHMETIC(op) {                               \
                Value left = this->evaluate(*b.fLeft);             \
                Value right = this->evaluate(*b.fRight);           \
                switch (type_kind(b.fLeft->fType)) {               \
                    case kFloat_TypeKind:                          \
                        return Value(left.fFloat op right.fFloat); \
                    case kInt_TypeKind:                            \
                        return Value(left.fInt op right.fInt);     \
                    default:                                       \
                        abort();                                   \
                }                                                  \
            }
            #define BITWISE(op) {                                  \
                Value left = this->evaluate(*b.fLeft);             \
                Value right = this->evaluate(*b.fRight);           \
                switch (type_kind(b.fLeft->fType)) {               \
                    case kInt_TypeKind:                            \
                        return Value(left.fInt op right.fInt);     \
                    default:                                       \
                        abort();                                   \
                }                                                  \
            }
            #define LOGIC(op) {                                    \
                Value left = this->evaluate(*b.fLeft);             \
                Value right = this->evaluate(*b.fRight);           \
                switch (type_kind(b.fLeft->fType)) {               \
                    case kFloat_TypeKind:                          \
                        return Value(left.fFloat op right.fFloat); \
                    case kInt_TypeKind:                            \
                        return Value(left.fInt op right.fInt);     \
                    default:                                       \
                        abort();                                   \
                }                                                  \
            }
            #define COMPOUND_ARITHMETIC(op) {                      \
                StackIndex left = this->getLValue(*b.fLeft);       \
                Value right = this->evaluate(*b.fRight);           \
                Value result = fStack[left];                       \
                switch (type_kind(b.fLeft->fType)) {               \
                    case kFloat_TypeKind:                          \
                        result.fFloat op right.fFloat;             \
                        break;                                     \
                    case kInt_TypeKind:                            \
                        result.fInt op right.fInt;                 \
                        break;                                     \
                    default:                                       \
                        abort();                                   \
                }                                                  \
                fStack[left] = result;                             \
                return result;                                     \
            }
            #define COMPOUND_BITWISE(op) {                         \
                StackIndex left = this->getLValue(*b.fLeft);       \
                Value right = this->evaluate(*b.fRight);           \
                Value result = fStack[left];                       \
                switch (type_kind(b.fLeft->fType)) {               \
                    case kInt_TypeKind:                            \
                        result.fInt op right.fInt;                 \
                        break;                                     \
                    default:                                       \
                        abort();                                   \
                }                                                  \
                fStack[left] = result;                             \
                return result;                                     \
            }
            const BinaryExpression& b = (const BinaryExpression&) expr;
            switch (b.fOperator) {
                case Token::PLUS:       ARITHMETIC(+)
                case Token::MINUS:      ARITHMETIC(-)
                case Token::STAR:       ARITHMETIC(*)
                case Token::SLASH:      ARITHMETIC(/)
                case Token::BITWISEAND: BITWISE(&)
                case Token::BITWISEOR:  BITWISE(|)
                case Token::BITWISEXOR: BITWISE(^)
                case Token::LT:         LOGIC(<)
                case Token::GT:         LOGIC(>)
                case Token::LTEQ:       LOGIC(<=)
                case Token::GTEQ:       LOGIC(>=)
                case Token::LOGICALAND: {
                    Value result = this->evaluate(*b.fLeft);
                    if (result.fBool) {
                        result = this->evaluate(*b.fRight);
                    }
                    return result;
                }
                case Token::LOGICALOR: {
                    Value result = this->evaluate(*b.fLeft);
                    if (!result.fBool) {
                        result = this->evaluate(*b.fRight);
                    }
                    return result;
                }
                case Token::EQ: {
                    StackIndex left = this->getLValue(*b.fLeft);
                    Value right = this->evaluate(*b.fRight);
                    fStack[left] = right;
                    return right;
                }
                case Token::PLUSEQ:       COMPOUND_ARITHMETIC(+=)
                case Token::MINUSEQ:      COMPOUND_ARITHMETIC(-=)
                case Token::STAREQ:       COMPOUND_ARITHMETIC(*=)
                case Token::SLASHEQ:      COMPOUND_ARITHMETIC(/=)
                case Token::BITWISEANDEQ: COMPOUND_BITWISE(&=)
                case Token::BITWISEOREQ:  COMPOUND_BITWISE(|=)
                case Token::BITWISEXOREQ: COMPOUND_BITWISE(^=)
                default:
                    ABORT("unsupported operator: %s\n", expr.description().c_str());
            }
            break;
        }
        case Expression::kBoolLiteral_Kind:
            return Value(((const BoolLiteral&) expr).fValue);
        case Expression::kConstructor_Kind:
            break;
        case Expression::kIntLiteral_Kind:
            return Value((int) ((const IntLiteral&) expr).fValue);
        case Expression::kFieldAccess_Kind:
            break;
        case Expression::kFloatLiteral_Kind:
            return Value((float) ((const FloatLiteral&) expr).fValue);
        case Expression::kFunctionCall_Kind:
            return this->call((const FunctionCall&) expr);
        case Expression::kIndex_Kind: {
            const IndexExpression& idx = (const IndexExpression&) expr;
            StackIndex pos = this->evaluate(*idx.fBase).fInt +
                             this->evaluate(*idx.fIndex).fInt;
            return fStack[pos];
        }
        case Expression::kPrefix_Kind: {
            const PrefixExpression& p = (const PrefixExpression&) expr;
            switch (p.fOperator) {
                case Token::MINUS: {
                    Value base = this->evaluate(*p.fOperand);
                    switch (type_kind(p.fType)) {
                        case kFloat_TypeKind:
                            return Value(-base.fFloat);
                        case kInt_TypeKind:
                            return Value(-base.fInt);
                        default:
                            abort();
                    }
                }
                case Token::LOGICALNOT: {
                    Value base = this->evaluate(*p.fOperand);
                    return Value(!base.fBool);
                }
                default:
                    abort();
            }
        }
        case Expression::kPostfix_Kind: {
            const PostfixExpression& p = (const PostfixExpression&) expr;
            StackIndex lvalue = this->getLValue(*p.fOperand);
            Value result = fStack[lvalue];
            switch (type_kind(p.fType)) {
                case kFloat_TypeKind:
                    if (Token::PLUSPLUS == p.fOperator) {
                        ++fStack[lvalue].fFloat;
                    } else {
                        SkASSERT(Token::MINUSMINUS == p.fOperator);
                        --fStack[lvalue].fFloat;
                    }
                    break;
                case kInt_TypeKind:
                    if (Token::PLUSPLUS == p.fOperator) {
                        ++fStack[lvalue].fInt;
                    } else {
                        SkASSERT(Token::MINUSMINUS == p.fOperator);
                        --fStack[lvalue].fInt;
                    }
                    break;
                default:
                    abort();
            }
            return result;
        }
        case Expression::kSetting_Kind:
            break;
        case Expression::kSwizzle_Kind:
            break;
        case Expression::kVariableReference_Kind:
            SkASSERT(fVars.size());
            SkASSERT(fVars.back().find(&((VariableReference&) expr).fVariable) !=
                   fVars.back().end());
            return fStack[fVars.back()[&((VariableReference&) expr).fVariable]];
        case Expression::kTernary_Kind: {
            const TernaryExpression& t = (const TernaryExpression&) expr;
            return this->evaluate(this->evaluate(*t.fTest).fBool ? *t.fIfTrue : *t.fIfFalse);
        }
        case Expression::kTypeReference_Kind:
            break;
        default:
            break;
    }
    ABORT("unsupported expression: %s\n", expr.description().c_str());
}

} // namespace

#endif