Home | History | Annotate | Download | only in sksl
      1 /*
      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 
     20 class SkRasterPipeline;
     21 
     22 namespace SkSL {
     23 
     24 class Interpreter {
     25     typedef int StackIndex;
     26 
     27     struct StatementIndex {
     28         const Statement* fStatement;
     29         size_t fIndex;
     30     };
     31 
     32 public:
     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 
     79 private:
     80     std::unique_ptr<Program> fProgram;
     81     SkRasterPipeline& fPipeline;
     82     std::vector<StatementIndex> fCurrentIndex;
     83     std::vector<std::unordered_map<const Variable*, StackIndex>> fVars;
     84     std::vector<Value> &fStack;
     85 };
     86 
     87 } // namespace
     88 
     89 #endif
     90