SeExpr
Interpreter.h
Go to the documentation of this file.
1/*
2 Copyright Disney Enterprises, Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License
6 and the following modification to it: Section 6 Trademarks.
7 deleted and replaced with:
8
9 6. Trademarks. This License does not grant permission to use the
10 trade names, trademarks, service marks, or product names of the
11 Licensor and its affiliates, except as required for reproducing
12 the content of the NOTICE file.
13
14 You may obtain a copy of the License at
15 http://www.apache.org/licenses/LICENSE-2.0
16*/
17#ifndef _Interpreter_h_
18#define _Interpreter_h_
19
20#include <vector>
21#include <stack>
22
23namespace SeExpr2 {
24class ExprLocalVar;
25
27template <int d>
28struct Promote {
29 // TODO: this needs a name that is prefixed by Se!
30 static int f(int* opData, double* fp, char** c, std::vector<int>& callStack) {
31 int posIn = opData[0];
32 int posOut = opData[1];
33 for (int k = posOut; k < posOut + d; k++) fp[k] = fp[posIn];
34 return 1;
35 }
36};
37
41 public:
43 std::vector<double> d;
45 std::vector<char*> s;
47 std::vector<int> opData;
48
50 typedef std::map<const ExprLocalVar*, int> VarToLoc;
52
54 typedef int (*OpF)(int*, double*, char**, std::vector<int>&);
55
56 std::vector<std::pair<OpF, int> > ops;
57 std::vector<int> callStack;
58
59 private:
62
63 public:
65 s.push_back(nullptr); // reserved for double** of variable block
66 s.push_back(nullptr); // reserved for double** of variable block
67 }
68
70 int nextPC() { return static_cast<int>(ops.size()); }
71
73 int addOp(OpF op) {
74 if (_startedOp) {
75 assert(false && "addOp called within another addOp");
76 }
77 _startedOp = true;
78 int pc = static_cast<int>(ops.size());
79 ops.push_back(std::make_pair(op, static_cast<int>(opData.size())));
80 return pc;
81 }
82
83 void endOp(bool execute = true) {
84 _startedOp = false;
85 if (execute) {
86 double* fp = &d[0];
87 char** str = &s[0];
88 int pc = static_cast<int>(ops.size()) - 1;
89 const std::pair<OpF, int>& op = ops[pc];
90 int* opCurr = &opData[0] + op.second;
91 pc += op.first(opCurr, fp, str, callStack);
92 }
93 }
94
96 int addOperand(int param) {
98 int ret = static_cast<int>(opData.size());
99 opData.push_back(param);
100 return ret;
101 }
102
104 int allocFP(int n) {
105 int ret = static_cast<int>(d.size());
106 for (int k = 0; k < n; k++) d.push_back(0);
107 return ret;
108 }
109
111 int allocPtr() {
112 int ret = static_cast<int>(s.size());
113 s.push_back(0);
114 return ret;
115 }
116
118 void eval(VarBlock* varBlock, bool debug = false);
120 void print(int pc = -1) const;
121
123};
124
126template <template <int d> class T, class T_FUNCTYPE = Interpreter::OpF>
128 switch (i) {
129 case 1:
130 return T<1>::f;
131 case 2:
132 return T<2>::f;
133 case 3:
134 return T<3>::f;
135 case 4:
136 return T<4>::f;
137 case 5:
138 return T<5>::f;
139 case 6:
140 return T<6>::f;
141 case 7:
142 return T<7>::f;
143 case 8:
144 return T<8>::f;
145 case 9:
146 return T<9>::f;
147 case 10:
148 return T<10>::f;
149 case 11:
150 return T<11>::f;
151 case 12:
152 return T<12>::f;
153 case 13:
154 return T<13>::f;
155 case 14:
156 return T<14>::f;
157 case 15:
158 return T<15>::f;
159 case 16:
160 return T<16>::f;
161 default:
162 assert(false && "Invalid dynamic parameter (not supported template)");
163 break;
164 }
165 return 0;
166}
167}
168
169#endif
void setPCStart(int pcStart)
int allocFP(int n)
! Allocate a floating point set of data of dimension n
int addOp(OpF op)
! adds an operator to the program (pointing to the data at the current location)
Definition Interpreter.h:73
int(* OpF)(int *, double *, char **, std::vector< int > &)
Op function pointer arguments are (int* currOpData,double* currD,char** c,std::stack<int>& callStacku...
Definition Interpreter.h:54
std::vector< std::pair< OpF, int > > ops
Definition Interpreter.h:56
void eval(VarBlock *varBlock, bool debug=false)
Evaluate program.
int addOperand(int param)
! Adds an operand. Note this should be done after doing the addOp!
Definition Interpreter.h:96
int allocPtr()
Allocate a pointer location (can be anything, but typically space for char*)
int nextPC()
Return the position that the next instruction will be placed at.
Definition Interpreter.h:70
std::map< const ExprLocalVar *, int > VarToLoc
Not needed for eval only building.
Definition Interpreter.h:50
void endOp(bool execute=true)
Definition Interpreter.h:83
std::vector< int > opData
Ooperands to op.
Definition Interpreter.h:47
std::vector< double > d
Double data (constants and evaluated)
Definition Interpreter.h:43
std::vector< char * > s
constant and evaluated pointer data
Definition Interpreter.h:45
void print(int pc=-1) const
Debug by printing program.
std::vector< int > callStack
Definition Interpreter.h:57
A thread local evaluation context. Just allocate and fill in with data.
Definition VarBlock.h:33
T_FUNCTYPE getTemplatizedOp(int i)
Return the function f encapsulated in class T for the dynamic i converted to a static d.
Promotes a FP[1] to FP[d].
Definition Interpreter.h:28
static int f(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition Interpreter.h:30