blob: eb5d0df4ddf650880ba10723f3a53026014134dc [file] [log] [blame]
/*
* 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 "src/sksl/SkSLByteCode.h"
#include "src/sksl/ir/SkSLAppendStage.h"
#include "src/sksl/ir/SkSLExpression.h"
#include "src/sksl/ir/SkSLFunctionCall.h"
#include "src/sksl/ir/SkSLFunctionDefinition.h"
#include "src/sksl/ir/SkSLProgram.h"
#include "src/sksl/ir/SkSLStatement.h"
#include <stack>
namespace SkSL {
class Interpreter {
typedef int StackIndex;
public:
union Value {
Value() {}
Value(float f)
: fFloat(f) {}
Value(int32_t s)
: fSigned(s) {}
Value(uint32_t u)
: fUnsigned(u) {}
Value(bool b)
: fBool(b) {}
float fFloat;
int32_t fSigned;
uint32_t fUnsigned;
bool fBool;
};
enum TypeKind {
kFloat_TypeKind,
kInt_TypeKind,
kBool_TypeKind
};
/**
* 'inputs' contains the values of global 'in' variables in source order.
*/
Interpreter(std::unique_ptr<Program> program, std::unique_ptr<ByteCode> byteCode,
Value inputs[] = nullptr);
/**
* Invokes the specified function with the given arguments. 'out' and 'inout' parameters will
* result in the 'args' array being modified. The return value is stored in 'outReturn' (may be
* null, in which case the return value is discarded).
*/
void run(const ByteCodeFunction& f, Value args[], Value* outReturn);
/**
* Updates the global inputs.
*/
void setInputs(Value inputs[]);
private:
void run(const ByteCodeFunction& f, Value* stack, Value args[], Value* outReturn);
void disassemble(const ByteCodeFunction& f);
std::unique_ptr<Program> fProgram;
std::unique_ptr<ByteCode> fByteCode;
std::vector<Value> fGlobals;
};
} // namespace
#endif