16 |
|
|
17 |
#include "../common/global.h" |
#include "../common/global.h" |
18 |
#include <vector> |
#include <vector> |
19 |
|
#include <map> |
20 |
|
#include <stddef.h> // offsetof() |
21 |
|
|
22 |
namespace LinuxSampler { |
namespace LinuxSampler { |
23 |
|
|
48 |
VM_EXEC_ERROR = (1<<2), |
VM_EXEC_ERROR = (1<<2), |
49 |
}; |
}; |
50 |
|
|
51 |
|
class VMIntExpr; |
52 |
|
class VMStringExpr; |
53 |
|
|
54 |
class VMExpr { |
class VMExpr { |
55 |
public: |
public: |
56 |
virtual ExprType_t exprType() const = 0; |
virtual ExprType_t exprType() const = 0; |
57 |
|
VMIntExpr* asInt() const; |
58 |
|
VMStringExpr* asString() const; |
59 |
}; |
}; |
60 |
|
|
61 |
class VMIntExpr : virtual public VMExpr { |
class VMIntExpr : virtual public VMExpr { |
92 |
virtual VMFnResult* exec(VMFnArgs* args) = 0; |
virtual VMFnResult* exec(VMFnArgs* args) = 0; |
93 |
}; |
}; |
94 |
|
|
95 |
|
/** |
96 |
|
* POD base of VMIntRelPtr and VMInt8RelPtr structures. Not intended to be |
97 |
|
* used directly. Use VMIntRelPtr or VMInt8RelPtr instead. |
98 |
|
*/ |
99 |
|
struct VMRelPtr { |
100 |
|
void** base; ///< Base pointer. |
101 |
|
int offset; ///< Offset (in bytes) to base pointer. |
102 |
|
}; |
103 |
|
|
104 |
|
/** @brief Pointer to built-in VM integer variable (of C/C++ type int). |
105 |
|
* |
106 |
|
* Used for defining built-in integer script variables. |
107 |
|
* |
108 |
|
* @b CAUTION: You may only use this class for pointing to C/C++ variables |
109 |
|
* of type "int" (which on most systems is 32 bit in size). If the C/C++ int |
110 |
|
* variable you want to reference is only 8 bit in size, then you @b must |
111 |
|
* use VMInt8RelPtr instead! |
112 |
|
* |
113 |
|
* For efficiency reasons the actual native C/C++ int variable is referenced |
114 |
|
* by two components here. The actual native int C/C++ variable in memory |
115 |
|
* is dereferenced at VM run-time by taking the @c base pointer dereference |
116 |
|
* and adding @c offset bytes. This has the advantage that for a large |
117 |
|
* number of built-in int variables, only one (or few) base pointer need |
118 |
|
* to be re-assigned before running a script, instead of updating each |
119 |
|
* built-in variable each time before a script is executed. |
120 |
|
* |
121 |
|
* Refer to DECLARE_VMINT() for example code. |
122 |
|
* |
123 |
|
* @see VMInt8RelPtr, DECLARE_VMINT() |
124 |
|
*/ |
125 |
|
struct VMIntRelPtr : VMRelPtr { |
126 |
|
VMIntRelPtr() { |
127 |
|
base = NULL; |
128 |
|
offset = 0; |
129 |
|
} |
130 |
|
VMIntRelPtr(const VMRelPtr& data) { |
131 |
|
base = data.base; |
132 |
|
offset = data.offset; |
133 |
|
} |
134 |
|
virtual int evalInt() { return *(int*)&(*(uint8_t**)base)[offset]; } |
135 |
|
virtual void assign(int i) { *(int*)&(*(uint8_t**)base)[offset] = i; } |
136 |
|
}; |
137 |
|
|
138 |
|
/** @brief Pointer to built-in VM integer variable (of C/C++ type int8_t). |
139 |
|
* |
140 |
|
* Used for defining built-in integer script variables. |
141 |
|
* |
142 |
|
* @b CAUTION: You may only use this class for pointing to C/C++ variables |
143 |
|
* of type "int8_t" (8 bit integer). If the C/C++ int variable you want to |
144 |
|
* reference is an "int" type (which is 32 bit on most systems), then you |
145 |
|
* @b must use VMIntRelPtr instead! |
146 |
|
* |
147 |
|
* For efficiency reasons the actual native C/C++ int variable is referenced |
148 |
|
* by two components here. The actual native int C/C++ variable in memory |
149 |
|
* is dereferenced at VM run-time by taking the @c base pointer dereference |
150 |
|
* and adding @c offset bytes. This has the advantage that for a large |
151 |
|
* number of built-in int variables, only one (or few) base pointer need |
152 |
|
* to be re-assigned before running a script, instead of updating each |
153 |
|
* built-in variable each time before a script is executed. |
154 |
|
* |
155 |
|
* Refer to DECLARE_VMINT() for example code. |
156 |
|
* |
157 |
|
* @see VMIntRelPtr, DECLARE_VMINT() |
158 |
|
*/ |
159 |
|
struct VMInt8RelPtr : VMIntRelPtr { |
160 |
|
VMInt8RelPtr() : VMIntRelPtr() {} |
161 |
|
VMInt8RelPtr(const VMRelPtr& data) : VMIntRelPtr(data) {} |
162 |
|
virtual int evalInt() OVERRIDE { |
163 |
|
return *(uint8_t*)&(*(uint8_t**)base)[offset]; |
164 |
|
} |
165 |
|
virtual void assign(int i) OVERRIDE { |
166 |
|
*(uint8_t*)&(*(uint8_t**)base)[offset] = i; |
167 |
|
} |
168 |
|
}; |
169 |
|
|
170 |
|
/** |
171 |
|
* Convenience macro for initializing VMIntRelPtr and VMInt8RelPtr |
172 |
|
* structures. Example: |
173 |
|
* @code |
174 |
|
* struct Foo { |
175 |
|
* uint8_t a; |
176 |
|
* int b; |
177 |
|
* }; |
178 |
|
* |
179 |
|
* Foo foo1 = (Foo) { 1, 3000 }; |
180 |
|
* Foo foo2 = (Foo) { 2, 4000 }; |
181 |
|
* |
182 |
|
* Foo* pFoo; |
183 |
|
* |
184 |
|
* VMInt8RelPtr var1 = DECLARE_VMINT(pFoo, class Foo, a); |
185 |
|
* VMIntRelPtr var2 = DECLARE_VMINT(pFoo, class Foo, b); |
186 |
|
* |
187 |
|
* pFoo = &foo1; |
188 |
|
* printf("%d\n", var1->evalInt()); // will print 1 |
189 |
|
* printf("%d\n", var2->evalInt()); // will print 3000 |
190 |
|
* |
191 |
|
* pFoo = &foo2; |
192 |
|
* printf("%d\n", var1->evalInt()); // will print 2 |
193 |
|
* printf("%d\n", var2->evalInt()); // will print 4000 |
194 |
|
* @endcode |
195 |
|
*/ |
196 |
|
#define DECLARE_VMINT(basePtr, T_struct, T_member) ( \ |
197 |
|
(VMRelPtr) { \ |
198 |
|
(void**) &basePtr, \ |
199 |
|
offsetof(T_struct, T_member) \ |
200 |
|
} \ |
201 |
|
) \ |
202 |
|
|
203 |
|
/** @brief Built-in VM 8 bit integer array variable. |
204 |
|
* |
205 |
|
* Used for defining built-in integer array script variables. |
206 |
|
*/ |
207 |
|
struct VMInt8Array { |
208 |
|
int8_t* data; |
209 |
|
int size; |
210 |
|
|
211 |
|
VMInt8Array() : data(NULL), size(0) {} |
212 |
|
}; |
213 |
|
|
214 |
class VMFunctionProvider { |
class VMFunctionProvider { |
215 |
public: |
public: |
216 |
virtual VMFunction* functionByName(const String& name) = 0; |
virtual VMFunction* functionByName(const String& name) = 0; |
217 |
|
virtual std::map<String,VMIntRelPtr*> builtInIntVariables() = 0; |
218 |
|
virtual std::map<String,VMInt8Array*> builtInIntArrayVariables() = 0; |
219 |
|
virtual std::map<String,int> builtInConstIntVariables() = 0; |
220 |
}; |
}; |
221 |
|
|
222 |
|
/** @brief Execution state of a virtual machine. |
223 |
|
* |
224 |
|
* An instance of this abstract base class represents exactly one execution |
225 |
|
* state of a virtual machine. This encompasses most notably the VM |
226 |
|
* execution stack, and VM polyphonic variables. You might see it as one |
227 |
|
* virtual thread of the virtual machine. |
228 |
|
* |
229 |
|
* @see VMParserContext |
230 |
|
*/ |
231 |
class VMExecContext { |
class VMExecContext { |
232 |
public: |
public: |
233 |
virtual ~VMExecContext() {} |
virtual ~VMExecContext() {} |
270 |
return "invalid"; |
return "invalid"; |
271 |
} |
} |
272 |
|
|
273 |
|
/** @brief Virtual machine representation of a script. |
274 |
|
* |
275 |
|
* An instance of this abstract base class represents a parsed script, |
276 |
|
* translated into a virtual machine. You should first check if there were |
277 |
|
* any parser errors. If there were any parser errors, you should refrain |
278 |
|
* from executing the virtual machine. Otherwise if there were no parser |
279 |
|
* errors (i.e. only warnings), then you might access one of the script's |
280 |
|
* event handlers by i.e. calling eventHandlerByName() and pass the |
281 |
|
* respective event handler to the ScriptVM class (or to one of its |
282 |
|
* descendants) for execution. |
283 |
|
* |
284 |
|
* @see VMExecContext |
285 |
|
*/ |
286 |
class VMParserContext { |
class VMParserContext { |
287 |
public: |
public: |
288 |
virtual ~VMParserContext() {} |
virtual ~VMParserContext() {} |