blob: 449e380541a460965b7de8560d6dddd097bfbdad [file] [log] [blame]
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001/*
2 * Copyright 2018 Google Inc.
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_INTERPRETER
9#define SKSL_INTERPRETER
10
11#include "ir/SkSLAppendStage.h"
12#include "ir/SkSLExpression.h"
13#include "ir/SkSLFunctionCall.h"
14#include "ir/SkSLFunctionDefinition.h"
15#include "ir/SkSLProgram.h"
16#include "ir/SkSLStatement.h"
17
18#include <stack>
19
20class SkRasterPipeline;
21
22namespace SkSL {
23
24class Interpreter {
25 typedef int StackIndex;
26
27 struct StatementIndex {
28 const Statement* fStatement;
29 size_t fIndex;
30 };
31
32public:
33 union Value {
34 Value(float f)
35 : fFloat(f) {}
36
37 Value(int i)
38 : fInt(i) {}
39
40 Value(bool b)
41 : fBool(b) {}
42
43 float fFloat;
44 int fInt;
45 bool fBool;
46 };
47
48 enum TypeKind {
49 kFloat_TypeKind,
50 kInt_TypeKind,
51 kBool_TypeKind
52 };
53
54 Interpreter(std::unique_ptr<Program> program, SkRasterPipeline* pipeline, std::vector<Value>* stack)
55 : fProgram(std::move(program))
56 , fPipeline(*pipeline)
57 , fStack(*stack) {}
58
59 void run();
60
61 void run(const FunctionDefinition& f);
62
63 void push(Value value);
64
65 Value pop();
66
67 StackIndex stackAlloc(int count);
68
69 void runStatement();
70
71 StackIndex getLValue(const Expression& expr);
72
73 Value call(const FunctionCall& c);
74
75 void appendStage(const AppendStage& c);
76
77 Value evaluate(const Expression& expr);
78
79private:
80 std::unique_ptr<Program> fProgram;
81 SkRasterPipeline& fPipeline;
82 std::vector<StatementIndex> fCurrentIndex;
Ethan Nicholasee04df42018-08-02 14:32:22 -040083 std::vector<std::unordered_map<const Variable*, StackIndex>> fVars;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040084 std::vector<Value> &fStack;
85};
86
87} // namespace
88
89#endif