1 |
schoenebeck |
2581 |
/* |
2 |
schoenebeck |
3551 |
* Copyright (c) 2014 - 2019 Christian Schoenebeck |
3 |
schoenebeck |
2581 |
* |
4 |
|
|
* http://www.linuxsampler.org |
5 |
|
|
* |
6 |
|
|
* This file is part of LinuxSampler and released under the same terms. |
7 |
|
|
* See README file for details. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "ScriptVM.h" |
11 |
|
|
|
12 |
schoenebeck |
2588 |
#include <string.h> |
13 |
schoenebeck |
2619 |
#include <assert.h> |
14 |
schoenebeck |
2581 |
#include "../common/global_private.h" |
15 |
|
|
#include "tree.h" |
16 |
schoenebeck |
2885 |
#include "CoreVMFunctions.h" |
17 |
schoenebeck |
2942 |
#include "CoreVMDynVars.h" |
18 |
schoenebeck |
2885 |
#include "editor/NkspScanner.h" |
19 |
schoenebeck |
2581 |
|
20 |
|
|
#define DEBUG_SCRIPTVM_CORE 0 |
21 |
|
|
|
22 |
schoenebeck |
2974 |
/** |
23 |
|
|
* Maximum amount of VM instructions to be executed per ScriptVM::exec() call |
24 |
|
|
* in case loops are involved, before the script got automatically suspended |
25 |
|
|
* for a certain amount of time to avoid any RT instability issues. |
26 |
|
|
* |
27 |
|
|
* The following value takes a max. execution time of 300 microseconds as aimed |
28 |
|
|
* target, assuming an execution time of approximately 5 microseconds per |
29 |
|
|
* instruction this leads to the very approximate value set below. |
30 |
|
|
*/ |
31 |
|
|
#define SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT 70 |
32 |
|
|
|
33 |
|
|
/** |
34 |
|
|
* Absolute maximum amount of VM instructions to be executed per |
35 |
|
|
* ScriptVM::exec() call (even if no loops are involved), before the script got |
36 |
|
|
* automatically suspended for a certain amount of time to avoid any RT |
37 |
|
|
* instability issues. |
38 |
|
|
* |
39 |
|
|
* A distinction between "soft" and "hard" limit is done here ATM because a |
40 |
|
|
* script author typically expects that his script might be interrupted |
41 |
|
|
* automatically if he is using while() loops, however he might not be |
42 |
|
|
* prepared that his script might also be interrupted if no loop is involved |
43 |
|
|
* (i.e. on very large scripts). |
44 |
|
|
* |
45 |
|
|
* The following value takes a max. execution time of 1000 microseconds as |
46 |
|
|
* aimed target, assuming an execution time of approximately 5 microseconds per |
47 |
|
|
* instruction this leads to the very approximate value set below. |
48 |
|
|
*/ |
49 |
|
|
#define SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD 210 |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* In case either SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT or |
53 |
|
|
* SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD was exceeded when calling |
54 |
|
|
* ScriptVM::exec() : the amount of microseconds the respective script |
55 |
|
|
* execution instance should be automatically suspended by the VM. |
56 |
|
|
*/ |
57 |
|
|
#define SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS 1000 |
58 |
|
|
|
59 |
schoenebeck |
2581 |
int InstrScript_parse(LinuxSampler::ParserContext*); |
60 |
|
|
|
61 |
|
|
namespace LinuxSampler { |
62 |
|
|
|
63 |
schoenebeck |
3034 |
#if DEBUG_SCRIPTVM_CORE |
64 |
schoenebeck |
2581 |
static void _printIndents(int n) { |
65 |
|
|
for (int i = 0; i < n; ++i) printf(" "); |
66 |
|
|
fflush(stdout); |
67 |
|
|
} |
68 |
schoenebeck |
3034 |
#endif |
69 |
schoenebeck |
2581 |
|
70 |
schoenebeck |
3557 |
static vmint _requiredMaxStackSizeFor(Statement* statement, vmint depth = 0) { |
71 |
schoenebeck |
2581 |
if (!statement) return 1; |
72 |
|
|
|
73 |
|
|
switch (statement->statementType()) { |
74 |
|
|
case STMT_LEAF: |
75 |
|
|
#if DEBUG_SCRIPTVM_CORE |
76 |
|
|
_printIndents(depth); |
77 |
|
|
printf("-> STMT_LEAF\n"); |
78 |
|
|
#endif |
79 |
|
|
return 1; |
80 |
|
|
|
81 |
|
|
case STMT_LIST: { |
82 |
|
|
#if DEBUG_SCRIPTVM_CORE |
83 |
|
|
_printIndents(depth); |
84 |
|
|
printf("-> STMT_LIST\n"); |
85 |
|
|
#endif |
86 |
|
|
Statements* stmts = (Statements*) statement; |
87 |
schoenebeck |
3557 |
vmint max = 0; |
88 |
schoenebeck |
2581 |
for (int i = 0; stmts->statement(i); ++i) { |
89 |
schoenebeck |
3557 |
vmint size = _requiredMaxStackSizeFor( stmts->statement(i), depth+1 ); |
90 |
schoenebeck |
2581 |
if (max < size) max = size; |
91 |
|
|
} |
92 |
|
|
return max + 1; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
case STMT_BRANCH: { |
96 |
|
|
#if DEBUG_SCRIPTVM_CORE |
97 |
|
|
_printIndents(depth); |
98 |
|
|
printf("-> STMT_BRANCH\n"); |
99 |
|
|
#endif |
100 |
|
|
BranchStatement* branchStmt = (BranchStatement*) statement; |
101 |
schoenebeck |
3557 |
vmint max = 0; |
102 |
schoenebeck |
2581 |
for (int i = 0; branchStmt->branch(i); ++i) { |
103 |
schoenebeck |
3557 |
vmint size = _requiredMaxStackSizeFor( branchStmt->branch(i), depth+1 ); |
104 |
schoenebeck |
2581 |
if (max < size) max = size; |
105 |
|
|
} |
106 |
|
|
return max + 1; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
case STMT_LOOP: { |
110 |
|
|
#if DEBUG_SCRIPTVM_CORE |
111 |
|
|
_printIndents(depth); |
112 |
|
|
printf("-> STMT_LOOP\n"); |
113 |
|
|
#endif |
114 |
|
|
While* whileStmt = (While*) statement; |
115 |
|
|
if (whileStmt->statements()) |
116 |
|
|
return _requiredMaxStackSizeFor( whileStmt->statements() ) + 1; |
117 |
|
|
else |
118 |
|
|
return 1; |
119 |
|
|
} |
120 |
schoenebeck |
3260 |
|
121 |
|
|
case STMT_SYNC: { |
122 |
|
|
#if DEBUG_SCRIPTVM_CORE |
123 |
|
|
_printIndents(depth); |
124 |
|
|
printf("-> STMT_SYNC\n"); |
125 |
|
|
#endif |
126 |
|
|
SyncBlock* syncStmt = (SyncBlock*) statement; |
127 |
|
|
if (syncStmt->statements()) |
128 |
|
|
return _requiredMaxStackSizeFor( syncStmt->statements() ) + 1; |
129 |
|
|
else |
130 |
|
|
return 1; |
131 |
|
|
} |
132 |
schoenebeck |
3557 |
|
133 |
|
|
case STMT_NOOP: |
134 |
|
|
break; // no operation like the name suggests |
135 |
schoenebeck |
2581 |
} |
136 |
|
|
|
137 |
|
|
return 1; // actually just to avoid compiler warning |
138 |
|
|
} |
139 |
|
|
|
140 |
schoenebeck |
3557 |
static vmint _requiredMaxStackSizeFor(EventHandlers* handlers) { |
141 |
|
|
vmint max = 1; |
142 |
schoenebeck |
2581 |
for (int i = 0; i < handlers->size(); ++i) { |
143 |
schoenebeck |
3557 |
vmint size = _requiredMaxStackSizeFor(handlers->eventHandler(i)); |
144 |
schoenebeck |
2581 |
if (max < size) max = size; |
145 |
|
|
} |
146 |
|
|
return max; |
147 |
|
|
} |
148 |
|
|
|
149 |
schoenebeck |
3551 |
ScriptVM::ScriptVM() : |
150 |
|
|
m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true), |
151 |
|
|
m_acceptExitRes(false) |
152 |
|
|
{ |
153 |
schoenebeck |
2885 |
m_fnMessage = new CoreVMFunction_message; |
154 |
schoenebeck |
3551 |
m_fnExit = new CoreVMFunction_exit(this); |
155 |
schoenebeck |
2885 |
m_fnWait = new CoreVMFunction_wait(this); |
156 |
|
|
m_fnAbs = new CoreVMFunction_abs; |
157 |
|
|
m_fnRandom = new CoreVMFunction_random; |
158 |
|
|
m_fnNumElements = new CoreVMFunction_num_elements; |
159 |
schoenebeck |
2945 |
m_fnInc = new CoreVMFunction_inc; |
160 |
|
|
m_fnDec = new CoreVMFunction_dec; |
161 |
schoenebeck |
3076 |
m_fnInRange = new CoreVMFunction_in_range; |
162 |
schoenebeck |
2942 |
m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER; |
163 |
|
|
m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER; |
164 |
schoenebeck |
2965 |
m_fnShLeft = new CoreVMFunction_sh_left; |
165 |
|
|
m_fnShRight = new CoreVMFunction_sh_right; |
166 |
schoenebeck |
2970 |
m_fnMin = new CoreVMFunction_min; |
167 |
|
|
m_fnMax = new CoreVMFunction_max; |
168 |
schoenebeck |
3221 |
m_fnArrayEqual = new CoreVMFunction_array_equal; |
169 |
|
|
m_fnSearch = new CoreVMFunction_search; |
170 |
|
|
m_fnSort = new CoreVMFunction_sort; |
171 |
schoenebeck |
2581 |
} |
172 |
|
|
|
173 |
|
|
ScriptVM::~ScriptVM() { |
174 |
schoenebeck |
2885 |
delete m_fnMessage; |
175 |
|
|
delete m_fnExit; |
176 |
|
|
delete m_fnWait; |
177 |
|
|
delete m_fnAbs; |
178 |
|
|
delete m_fnRandom; |
179 |
|
|
delete m_fnNumElements; |
180 |
schoenebeck |
2945 |
delete m_fnInc; |
181 |
|
|
delete m_fnDec; |
182 |
schoenebeck |
3076 |
delete m_fnInRange; |
183 |
schoenebeck |
2965 |
delete m_fnShLeft; |
184 |
|
|
delete m_fnShRight; |
185 |
schoenebeck |
2970 |
delete m_fnMin; |
186 |
|
|
delete m_fnMax; |
187 |
schoenebeck |
3221 |
delete m_fnArrayEqual; |
188 |
|
|
delete m_fnSearch; |
189 |
|
|
delete m_fnSort; |
190 |
schoenebeck |
2942 |
delete m_varRealTimer; |
191 |
|
|
delete m_varPerfTimer; |
192 |
schoenebeck |
2581 |
} |
193 |
|
|
|
194 |
schoenebeck |
2588 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
195 |
schoenebeck |
2581 |
std::istringstream iss(s); |
196 |
schoenebeck |
2588 |
return loadScript(&iss); |
197 |
schoenebeck |
2581 |
} |
198 |
|
|
|
199 |
schoenebeck |
2588 |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
200 |
|
|
ParserContext* context = new ParserContext(this); |
201 |
|
|
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
202 |
schoenebeck |
2594 |
|
203 |
|
|
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
204 |
|
|
context->registerBuiltInIntVariables( builtInIntVariables() ); |
205 |
|
|
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
206 |
schoenebeck |
2942 |
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
207 |
schoenebeck |
2594 |
|
208 |
schoenebeck |
2588 |
context->createScanner(is); |
209 |
schoenebeck |
2581 |
|
210 |
schoenebeck |
2588 |
InstrScript_parse(context); |
211 |
schoenebeck |
3557 |
dmsg(2,("Allocating %lld bytes of global int VM memory.\n", context->globalIntVarCount * sizeof(vmint))); |
212 |
|
|
dmsg(2,("Allocating %lld of global VM string variables.\n", context->globalStrVarCount)); |
213 |
schoenebeck |
2588 |
if (!context->globalIntMemory) |
214 |
schoenebeck |
3557 |
context->globalIntMemory = new ArrayList<vmint>(); |
215 |
schoenebeck |
2588 |
if (!context->globalStrMemory) |
216 |
|
|
context->globalStrMemory = new ArrayList<String>(); |
217 |
|
|
context->globalIntMemory->resize(context->globalIntVarCount); |
218 |
schoenebeck |
3557 |
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(vmint)); |
219 |
schoenebeck |
2588 |
|
220 |
|
|
context->globalStrMemory->resize(context->globalStrVarCount); |
221 |
schoenebeck |
2581 |
|
222 |
schoenebeck |
2588 |
context->destroyScanner(); |
223 |
schoenebeck |
2581 |
|
224 |
schoenebeck |
2588 |
return context; |
225 |
schoenebeck |
2581 |
} |
226 |
|
|
|
227 |
schoenebeck |
2588 |
void ScriptVM::dumpParsedScript(VMParserContext* context) { |
228 |
|
|
ParserContext* ctx = dynamic_cast<ParserContext*>(context); |
229 |
|
|
if (!ctx) { |
230 |
schoenebeck |
2581 |
std::cerr << "No VM context. So nothing to dump.\n"; |
231 |
|
|
return; |
232 |
|
|
} |
233 |
schoenebeck |
2588 |
if (!ctx->handlers) { |
234 |
schoenebeck |
2581 |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
235 |
|
|
return; |
236 |
|
|
} |
237 |
schoenebeck |
2588 |
if (!ctx->globalIntMemory) { |
238 |
schoenebeck |
2581 |
std::cerr << "Internal error: no global memory assigend to script VM.\n"; |
239 |
|
|
return; |
240 |
|
|
} |
241 |
schoenebeck |
2588 |
ctx->handlers->dump(); |
242 |
schoenebeck |
2581 |
} |
243 |
|
|
|
244 |
schoenebeck |
2588 |
VMExecContext* ScriptVM::createExecContext(VMParserContext* parserContext) { |
245 |
|
|
ParserContext* parserCtx = dynamic_cast<ParserContext*>(parserContext); |
246 |
|
|
ExecContext* execCtx = new ExecContext(); |
247 |
|
|
|
248 |
|
|
if (parserCtx->requiredMaxStackSize < 0) { |
249 |
|
|
parserCtx->requiredMaxStackSize = |
250 |
|
|
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
251 |
|
|
} |
252 |
|
|
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
253 |
schoenebeck |
3557 |
dmsg(2,("Created VM exec context with %lld bytes VM stack size.\n", |
254 |
|
|
parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))); |
255 |
schoenebeck |
2588 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
256 |
schoenebeck |
3557 |
const vmint polySize = parserCtx->polyphonicIntVarCount; |
257 |
schoenebeck |
2588 |
execCtx->polyphonicIntMemory.resize(polySize); |
258 |
schoenebeck |
3557 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(vmint)); |
259 |
schoenebeck |
2588 |
|
260 |
schoenebeck |
3557 |
dmsg(2,("Allocated %lld bytes polyphonic memory.\n", polySize * sizeof(vmint))); |
261 |
schoenebeck |
2588 |
return execCtx; |
262 |
schoenebeck |
2581 |
} |
263 |
|
|
|
264 |
schoenebeck |
2885 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
265 |
|
|
std::istringstream iss(s); |
266 |
|
|
return syntaxHighlighting(&iss); |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
270 |
schoenebeck |
3332 |
try { |
271 |
|
|
NkspScanner scanner(is); |
272 |
|
|
std::vector<SourceToken> tokens = scanner.tokens(); |
273 |
|
|
std::vector<VMSourceToken> result; |
274 |
|
|
result.resize(tokens.size()); |
275 |
schoenebeck |
3557 |
for (vmint i = 0; i < tokens.size(); ++i) { |
276 |
schoenebeck |
3332 |
SourceToken* st = new SourceToken; |
277 |
|
|
*st = tokens[i]; |
278 |
|
|
result[i] = VMSourceToken(st); |
279 |
|
|
} |
280 |
|
|
return result; |
281 |
|
|
} catch (...) { |
282 |
|
|
return std::vector<VMSourceToken>(); |
283 |
schoenebeck |
2885 |
} |
284 |
|
|
} |
285 |
|
|
|
286 |
schoenebeck |
2581 |
VMFunction* ScriptVM::functionByName(const String& name) { |
287 |
schoenebeck |
2885 |
if (name == "message") return m_fnMessage; |
288 |
|
|
else if (name == "exit") return m_fnExit; |
289 |
|
|
else if (name == "wait") return m_fnWait; |
290 |
|
|
else if (name == "abs") return m_fnAbs; |
291 |
|
|
else if (name == "random") return m_fnRandom; |
292 |
|
|
else if (name == "num_elements") return m_fnNumElements; |
293 |
schoenebeck |
2945 |
else if (name == "inc") return m_fnInc; |
294 |
|
|
else if (name == "dec") return m_fnDec; |
295 |
schoenebeck |
3076 |
else if (name == "in_range") return m_fnInRange; |
296 |
schoenebeck |
2965 |
else if (name == "sh_left") return m_fnShLeft; |
297 |
|
|
else if (name == "sh_right") return m_fnShRight; |
298 |
schoenebeck |
2970 |
else if (name == "min") return m_fnMin; |
299 |
|
|
else if (name == "max") return m_fnMax; |
300 |
schoenebeck |
3221 |
else if (name == "array_equal") return m_fnArrayEqual; |
301 |
|
|
else if (name == "search") return m_fnSearch; |
302 |
|
|
else if (name == "sort") return m_fnSort; |
303 |
schoenebeck |
2581 |
return NULL; |
304 |
|
|
} |
305 |
schoenebeck |
2588 |
|
306 |
schoenebeck |
3311 |
bool ScriptVM::isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) { |
307 |
|
|
ParserContext* parserCtx = dynamic_cast<ParserContext*>(ctx); |
308 |
|
|
if (!parserCtx) return false; |
309 |
|
|
|
310 |
|
|
if (fn == m_fnMessage && parserCtx->userPreprocessorConditions.count("NKSP_NO_MESSAGE")) |
311 |
|
|
return true; |
312 |
|
|
|
313 |
|
|
return false; |
314 |
|
|
} |
315 |
|
|
|
316 |
schoenebeck |
3557 |
std::map<String,VMIntPtr*> ScriptVM::builtInIntVariables() { |
317 |
|
|
return std::map<String,VMIntPtr*>(); |
318 |
schoenebeck |
2594 |
} |
319 |
|
|
|
320 |
|
|
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
321 |
|
|
return std::map<String,VMInt8Array*>(); |
322 |
|
|
} |
323 |
|
|
|
324 |
schoenebeck |
2942 |
std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() { |
325 |
|
|
std::map<String,VMDynVar*> m; |
326 |
|
|
|
327 |
|
|
m["$NKSP_PERF_TIMER"] = m_varPerfTimer; |
328 |
|
|
m["$NKSP_REAL_TIMER"] = m_varRealTimer; |
329 |
|
|
m["$KSP_TIMER"] = m_varRealTimer; |
330 |
|
|
|
331 |
|
|
return m; |
332 |
|
|
} |
333 |
|
|
|
334 |
schoenebeck |
3557 |
std::map<String,vmint> ScriptVM::builtInConstIntVariables() { |
335 |
|
|
std::map<String,vmint> m; |
336 |
schoenebeck |
2948 |
|
337 |
|
|
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
338 |
|
|
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
339 |
|
|
m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE; |
340 |
|
|
m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER; |
341 |
|
|
|
342 |
|
|
return m; |
343 |
schoenebeck |
2594 |
} |
344 |
|
|
|
345 |
schoenebeck |
2879 |
VMEventHandler* ScriptVM::currentVMEventHandler() { |
346 |
|
|
return m_eventHandler; |
347 |
|
|
} |
348 |
|
|
|
349 |
schoenebeck |
2588 |
VMParserContext* ScriptVM::currentVMParserContext() { |
350 |
|
|
return m_parserContext; |
351 |
|
|
} |
352 |
|
|
|
353 |
schoenebeck |
2581 |
VMExecContext* ScriptVM::currentVMExecContext() { |
354 |
schoenebeck |
2588 |
if (!m_parserContext) return NULL; |
355 |
|
|
return m_parserContext->execContext; |
356 |
schoenebeck |
2581 |
} |
357 |
|
|
|
358 |
schoenebeck |
2974 |
void ScriptVM::setAutoSuspendEnabled(bool b) { |
359 |
|
|
m_autoSuspend = b; |
360 |
|
|
} |
361 |
|
|
|
362 |
|
|
bool ScriptVM::isAutoSuspendEnabled() const { |
363 |
|
|
return m_autoSuspend; |
364 |
|
|
} |
365 |
|
|
|
366 |
schoenebeck |
3551 |
void ScriptVM::setExitResultEnabled(bool b) { |
367 |
|
|
m_acceptExitRes = b; |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
bool ScriptVM::isExitResultEnabled() const { |
371 |
|
|
return m_acceptExitRes; |
372 |
|
|
} |
373 |
|
|
|
374 |
schoenebeck |
2588 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
375 |
|
|
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
376 |
|
|
if (!m_parserContext) { |
377 |
schoenebeck |
3557 |
std::cerr << "No VM parser context provided. Did you load a script?\n"; |
378 |
schoenebeck |
2581 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
379 |
|
|
} |
380 |
|
|
|
381 |
schoenebeck |
2611 |
// a ParserContext object is always tied to exactly one ScriptVM object |
382 |
|
|
assert(m_parserContext->functionProvider == this); |
383 |
|
|
|
384 |
schoenebeck |
2581 |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
385 |
|
|
if (!ctx) { |
386 |
|
|
std::cerr << "Invalid VM exec context.\n"; |
387 |
|
|
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
388 |
|
|
} |
389 |
|
|
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
390 |
|
|
if (!h) return VM_EXEC_NOT_RUNNING; |
391 |
schoenebeck |
2879 |
m_eventHandler = handler; |
392 |
schoenebeck |
2581 |
|
393 |
schoenebeck |
2588 |
m_parserContext->execContext = ctx; |
394 |
schoenebeck |
2581 |
|
395 |
|
|
ctx->status = VM_EXEC_RUNNING; |
396 |
schoenebeck |
3221 |
ctx->instructionsCount = 0; |
397 |
schoenebeck |
3551 |
ctx->clearExitRes(); |
398 |
schoenebeck |
3277 |
StmtFlags_t& flags = ctx->flags; |
399 |
schoenebeck |
3557 |
vmint instructionsCounter = 0; |
400 |
|
|
vmint synced = m_autoSuspend ? 0 : 1; |
401 |
schoenebeck |
2581 |
|
402 |
|
|
int& frameIdx = ctx->stackFrame; |
403 |
|
|
if (frameIdx < 0) { // start condition ... |
404 |
|
|
frameIdx = -1; |
405 |
|
|
ctx->pushStack(h); |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
while (flags == STMT_SUCCESS && frameIdx >= 0) { |
409 |
|
|
if (frameIdx >= ctx->stack.size()) { // should never happen, otherwise it's a bug ... |
410 |
|
|
std::cerr << "CRITICAL: VM stack overflow! (" << frameIdx << ")\n"; |
411 |
|
|
flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
412 |
|
|
break; |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
ExecContext::StackFrame& frame = ctx->stack[frameIdx]; |
416 |
|
|
switch (frame.statement->statementType()) { |
417 |
|
|
case STMT_LEAF: { |
418 |
|
|
#if DEBUG_SCRIPTVM_CORE |
419 |
|
|
_printIndents(frameIdx); |
420 |
|
|
printf("-> STMT_LEAF\n"); |
421 |
|
|
#endif |
422 |
|
|
LeafStatement* leaf = (LeafStatement*) frame.statement; |
423 |
|
|
flags = leaf->exec(); |
424 |
|
|
ctx->popStack(); |
425 |
|
|
break; |
426 |
|
|
} |
427 |
|
|
|
428 |
|
|
case STMT_LIST: { |
429 |
|
|
#if DEBUG_SCRIPTVM_CORE |
430 |
|
|
_printIndents(frameIdx); |
431 |
|
|
printf("-> STMT_LIST subidx=%d\n", frame.subindex); |
432 |
|
|
#endif |
433 |
|
|
Statements* stmts = (Statements*) frame.statement; |
434 |
|
|
if (stmts->statement(frame.subindex)) { |
435 |
|
|
ctx->pushStack( |
436 |
|
|
stmts->statement(frame.subindex++) |
437 |
|
|
); |
438 |
|
|
} else { |
439 |
|
|
#if DEBUG_SCRIPTVM_CORE |
440 |
|
|
_printIndents(frameIdx); |
441 |
|
|
printf("[END OF LIST] subidx=%d\n", frame.subindex); |
442 |
|
|
#endif |
443 |
|
|
ctx->popStack(); |
444 |
|
|
} |
445 |
|
|
break; |
446 |
|
|
} |
447 |
|
|
|
448 |
|
|
case STMT_BRANCH: { |
449 |
|
|
#if DEBUG_SCRIPTVM_CORE |
450 |
|
|
_printIndents(frameIdx); |
451 |
|
|
printf("-> STMT_BRANCH\n"); |
452 |
|
|
#endif |
453 |
|
|
if (frame.subindex < 0) ctx->popStack(); |
454 |
|
|
else { |
455 |
|
|
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
456 |
schoenebeck |
3557 |
frame.subindex = |
457 |
|
|
(decltype(frame.subindex)) |
458 |
|
|
branchStmt->evalBranch(); |
459 |
schoenebeck |
2581 |
if (frame.subindex >= 0) { |
460 |
|
|
ctx->pushStack( |
461 |
|
|
branchStmt->branch(frame.subindex) |
462 |
|
|
); |
463 |
|
|
frame.subindex = -1; |
464 |
|
|
} else ctx->popStack(); |
465 |
|
|
} |
466 |
|
|
break; |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
case STMT_LOOP: { |
470 |
|
|
#if DEBUG_SCRIPTVM_CORE |
471 |
|
|
_printIndents(frameIdx); |
472 |
|
|
printf("-> STMT_LOOP\n"); |
473 |
|
|
#endif |
474 |
|
|
While* whileStmt = (While*) frame.statement; |
475 |
|
|
if (whileStmt->evalLoopStartCondition() && whileStmt->statements()) { |
476 |
|
|
ctx->pushStack( |
477 |
|
|
whileStmt->statements() |
478 |
|
|
); |
479 |
schoenebeck |
3260 |
if (flags == STMT_SUCCESS && !synced && |
480 |
schoenebeck |
2974 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT) |
481 |
|
|
{ |
482 |
|
|
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
483 |
|
|
ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS; |
484 |
|
|
} |
485 |
schoenebeck |
2581 |
} else ctx->popStack(); |
486 |
schoenebeck |
2974 |
break; |
487 |
schoenebeck |
2581 |
} |
488 |
schoenebeck |
3260 |
|
489 |
|
|
case STMT_SYNC: { |
490 |
|
|
#if DEBUG_SCRIPTVM_CORE |
491 |
|
|
_printIndents(frameIdx); |
492 |
|
|
printf("-> STMT_SYNC\n"); |
493 |
|
|
#endif |
494 |
|
|
SyncBlock* syncStmt = (SyncBlock*) frame.statement; |
495 |
|
|
if (!frame.subindex++ && syncStmt->statements()) { |
496 |
|
|
++synced; |
497 |
|
|
ctx->pushStack( |
498 |
|
|
syncStmt->statements() |
499 |
|
|
); |
500 |
|
|
} else { |
501 |
|
|
ctx->popStack(); |
502 |
|
|
--synced; |
503 |
|
|
} |
504 |
|
|
break; |
505 |
|
|
} |
506 |
schoenebeck |
3557 |
|
507 |
|
|
case STMT_NOOP: |
508 |
|
|
break; // no operation like the name suggests |
509 |
schoenebeck |
2581 |
} |
510 |
schoenebeck |
2974 |
|
511 |
schoenebeck |
3260 |
if (flags == STMT_SUCCESS && !synced && |
512 |
schoenebeck |
2974 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD) |
513 |
|
|
{ |
514 |
|
|
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
515 |
|
|
ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS; |
516 |
|
|
} |
517 |
|
|
|
518 |
|
|
++instructionsCounter; |
519 |
schoenebeck |
2581 |
} |
520 |
|
|
|
521 |
schoenebeck |
3277 |
if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) { |
522 |
schoenebeck |
2581 |
ctx->status = VM_EXEC_SUSPENDED; |
523 |
schoenebeck |
3277 |
ctx->flags = STMT_SUCCESS; |
524 |
schoenebeck |
2581 |
} else { |
525 |
|
|
ctx->status = VM_EXEC_NOT_RUNNING; |
526 |
|
|
if (flags & STMT_ERROR_OCCURRED) |
527 |
|
|
ctx->status = VM_EXEC_ERROR; |
528 |
|
|
ctx->reset(); |
529 |
|
|
} |
530 |
|
|
|
531 |
schoenebeck |
3221 |
ctx->instructionsCount = instructionsCounter; |
532 |
|
|
|
533 |
schoenebeck |
2879 |
m_eventHandler = NULL; |
534 |
schoenebeck |
2588 |
m_parserContext->execContext = NULL; |
535 |
|
|
m_parserContext = NULL; |
536 |
schoenebeck |
2581 |
return ctx->status; |
537 |
|
|
} |
538 |
|
|
|
539 |
|
|
} // namespace LinuxSampler |