aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/SkSLInterpreter.h
blob: 93f32a1617f477b5de7ec67b8dffbbaed08e6511 (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
/*
 * 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_INTERPRETER
#define SKSL_INTERPRETER

#include "ir/SkSLAppendStage.h"
#include "ir/SkSLExpression.h"
#include "ir/SkSLFunctionCall.h"
#include "ir/SkSLFunctionDefinition.h"
#include "ir/SkSLProgram.h"
#include "ir/SkSLStatement.h"

#include <stack>

class SkRasterPipeline;

namespace SkSL {

class Interpreter {
    typedef int StackIndex;

    struct StatementIndex {
        const Statement* fStatement;
        size_t fIndex;
    };

public:
    union Value {
        Value(float f)
        : fFloat(f) {}

        Value(int i)
        : fInt(i) {}

        Value(bool b)
        : fBool(b) {}

        float fFloat;
        int fInt;
        bool fBool;
    };

    enum TypeKind {
        kFloat_TypeKind,
        kInt_TypeKind,
        kBool_TypeKind
    };

    Interpreter(std::unique_ptr<Program> program, SkRasterPipeline* pipeline, std::vector<Value>* stack)
    : fProgram(std::move(program))
    , fPipeline(*pipeline)
    , fStack(*stack) {}

    void run();

    void run(const FunctionDefinition& f);

    void push(Value value);

    Value pop();

    StackIndex stackAlloc(int count);

    void runStatement();

    StackIndex getLValue(const Expression& expr);

    Value call(const FunctionCall& c);

    void appendStage(const AppendStage& c);

    Value evaluate(const Expression& expr);

private:
    std::unique_ptr<Program> fProgram;
    SkRasterPipeline& fPipeline;
    std::vector<StatementIndex> fCurrentIndex;
    std::vector<std::map<const Variable*, StackIndex>> fVars;
    std::vector<Value> &fStack;
};

} // namespace

#endif