1 |
/* |
2 |
* Copyright (c) 2014 - 2020 Christian Schoenebeck |
3 |
* |
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 |
#include <string.h> |
13 |
#include <assert.h> |
14 |
#include "../common/global_private.h" |
15 |
#include "tree.h" |
16 |
#include "CoreVMFunctions.h" |
17 |
#include "CoreVMDynVars.h" |
18 |
#include "editor/NkspScanner.h" |
19 |
|
20 |
#define DEBUG_SCRIPTVM_CORE 0 |
21 |
|
22 |
/** |
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 |
int InstrScript_parse(LinuxSampler::ParserContext*); |
60 |
|
61 |
namespace LinuxSampler { |
62 |
|
63 |
#if DEBUG_SCRIPTVM_CORE |
64 |
static void _printIndents(int n) { |
65 |
for (int i = 0; i < n; ++i) printf(" "); |
66 |
fflush(stdout); |
67 |
} |
68 |
#endif |
69 |
|
70 |
static vmint _requiredMaxStackSizeFor(Statement* statement, vmint depth = 0) { |
71 |
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 |
vmint max = 0; |
88 |
for (int i = 0; stmts->statement(i); ++i) { |
89 |
vmint size = _requiredMaxStackSizeFor( stmts->statement(i), depth+1 ); |
90 |
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 |
vmint max = 0; |
102 |
for (int i = 0; branchStmt->branch(i); ++i) { |
103 |
vmint size = _requiredMaxStackSizeFor( branchStmt->branch(i), depth+1 ); |
104 |
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 |
|
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 |
|
133 |
case STMT_NOOP: |
134 |
break; // no operation like the name suggests |
135 |
} |
136 |
|
137 |
return 1; // actually just to avoid compiler warning |
138 |
} |
139 |
|
140 |
static vmint _requiredMaxStackSizeFor(EventHandlers* handlers) { |
141 |
vmint max = 1; |
142 |
for (int i = 0; i < handlers->size(); ++i) { |
143 |
vmint size = _requiredMaxStackSizeFor(handlers->eventHandler(i)); |
144 |
if (max < size) max = size; |
145 |
} |
146 |
return max; |
147 |
} |
148 |
|
149 |
ScriptVM::ScriptVM() : |
150 |
m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true), |
151 |
m_acceptExitRes(false) |
152 |
{ |
153 |
m_fnMessage = new CoreVMFunction_message; |
154 |
m_fnExit = new CoreVMFunction_exit(this); |
155 |
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 |
m_fnInc = new CoreVMFunction_inc; |
160 |
m_fnDec = new CoreVMFunction_dec; |
161 |
m_fnInRange = new CoreVMFunction_in_range; |
162 |
m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER; |
163 |
m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER; |
164 |
m_fnShLeft = new CoreVMFunction_sh_left; |
165 |
m_fnShRight = new CoreVMFunction_sh_right; |
166 |
m_fnMsb = new CoreVMFunction_msb; |
167 |
m_fnLsb = new CoreVMFunction_lsb; |
168 |
m_fnMin = new CoreVMFunction_min; |
169 |
m_fnMax = new CoreVMFunction_max; |
170 |
m_fnArrayEqual = new CoreVMFunction_array_equal; |
171 |
m_fnSearch = new CoreVMFunction_search; |
172 |
m_fnSort = new CoreVMFunction_sort; |
173 |
m_fnIntToReal = new CoreVMFunction_int_to_real; |
174 |
m_fnRealToInt = new CoreVMFunction_real_to_int; |
175 |
m_fnRound = new CoreVMFunction_round; |
176 |
m_fnCeil = new CoreVMFunction_ceil; |
177 |
m_fnFloor = new CoreVMFunction_floor; |
178 |
m_fnSqrt = new CoreVMFunction_sqrt; |
179 |
m_fnLog = new CoreVMFunction_log; |
180 |
m_fnLog2 = new CoreVMFunction_log2; |
181 |
m_fnLog10 = new CoreVMFunction_log10; |
182 |
m_fnExp = new CoreVMFunction_exp; |
183 |
m_fnPow = new CoreVMFunction_pow; |
184 |
m_fnSin = new CoreVMFunction_sin; |
185 |
m_fnCos = new CoreVMFunction_cos; |
186 |
m_fnTan = new CoreVMFunction_tan; |
187 |
m_fnAsin = new CoreVMFunction_asin; |
188 |
m_fnAcos = new CoreVMFunction_acos; |
189 |
m_fnAtan = new CoreVMFunction_atan; |
190 |
} |
191 |
|
192 |
ScriptVM::~ScriptVM() { |
193 |
delete m_fnMessage; |
194 |
delete m_fnExit; |
195 |
delete m_fnWait; |
196 |
delete m_fnAbs; |
197 |
delete m_fnRandom; |
198 |
delete m_fnNumElements; |
199 |
delete m_fnInc; |
200 |
delete m_fnDec; |
201 |
delete m_fnInRange; |
202 |
delete m_fnShLeft; |
203 |
delete m_fnShRight; |
204 |
delete m_fnMsb; |
205 |
delete m_fnLsb; |
206 |
delete m_fnMin; |
207 |
delete m_fnMax; |
208 |
delete m_fnArrayEqual; |
209 |
delete m_fnSearch; |
210 |
delete m_fnSort; |
211 |
delete m_fnIntToReal; |
212 |
delete m_fnRealToInt; |
213 |
delete m_fnRound; |
214 |
delete m_fnCeil; |
215 |
delete m_fnFloor; |
216 |
delete m_fnSqrt; |
217 |
delete m_fnLog; |
218 |
delete m_fnLog2; |
219 |
delete m_fnLog10; |
220 |
delete m_fnExp; |
221 |
delete m_fnPow; |
222 |
delete m_fnSin; |
223 |
delete m_fnCos; |
224 |
delete m_fnTan; |
225 |
delete m_fnAsin; |
226 |
delete m_fnAcos; |
227 |
delete m_fnAtan; |
228 |
delete m_varRealTimer; |
229 |
delete m_varPerfTimer; |
230 |
} |
231 |
|
232 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
233 |
std::istringstream iss(s); |
234 |
return loadScript(&iss); |
235 |
} |
236 |
|
237 |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
238 |
ParserContext* context = new ParserContext(this); |
239 |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
240 |
|
241 |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
242 |
context->registerBuiltInConstRealVariables( builtInConstRealVariables() ); |
243 |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
244 |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
245 |
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
246 |
|
247 |
context->createScanner(is); |
248 |
|
249 |
InstrScript_parse(context); |
250 |
dmsg(2,("Allocating %" PRId64 " bytes of global int VM memory.\n", int64_t(context->globalIntVarCount * sizeof(vmint)))); |
251 |
dmsg(2,("Allocating %" PRId64 " bytes of global real VM memory.\n", int64_t(context->globalRealVarCount * sizeof(vmfloat)))); |
252 |
dmsg(2,("Allocating %" PRId64 " bytes of global unit factor VM memory.\n", int64_t(context->globalUnitFactorCount * sizeof(vmfloat)))); |
253 |
dmsg(2,("Allocating %" PRId64 " of global VM string variables.\n", (int64_t)context->globalStrVarCount)); |
254 |
if (!context->globalIntMemory) |
255 |
context->globalIntMemory = new ArrayList<vmint>(); |
256 |
if (!context->globalRealMemory) |
257 |
context->globalRealMemory = new ArrayList<vmfloat>(); |
258 |
if (!context->globalUnitFactorMemory) |
259 |
context->globalUnitFactorMemory = new ArrayList<vmfloat>(); |
260 |
if (!context->globalStrMemory) |
261 |
context->globalStrMemory = new ArrayList<String>(); |
262 |
context->globalIntMemory->resize(context->globalIntVarCount); |
263 |
context->globalRealMemory->resize(context->globalRealVarCount); |
264 |
context->globalUnitFactorMemory->resize(context->globalUnitFactorCount); |
265 |
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(vmint)); |
266 |
memset(&((*context->globalRealMemory)[0]), 0, context->globalRealVarCount * sizeof(vmfloat)); |
267 |
for (vmint i = 0; i < context->globalUnitFactorCount; ++i) |
268 |
(*context->globalUnitFactorMemory)[i] = VM_NO_FACTOR; |
269 |
context->globalStrMemory->resize(context->globalStrVarCount); |
270 |
|
271 |
context->destroyScanner(); |
272 |
|
273 |
return context; |
274 |
} |
275 |
|
276 |
void ScriptVM::dumpParsedScript(VMParserContext* context) { |
277 |
ParserContext* ctx = dynamic_cast<ParserContext*>(context); |
278 |
if (!ctx) { |
279 |
std::cerr << "No VM context. So nothing to dump.\n"; |
280 |
return; |
281 |
} |
282 |
if (!ctx->handlers) { |
283 |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
284 |
return; |
285 |
} |
286 |
if (!ctx->globalIntMemory) { |
287 |
std::cerr << "Internal error: no global integer memory assigend to script VM.\n"; |
288 |
return; |
289 |
} |
290 |
if (!ctx->globalRealMemory) { |
291 |
std::cerr << "Internal error: no global real number memory assigend to script VM.\n"; |
292 |
return; |
293 |
} |
294 |
ctx->handlers->dump(); |
295 |
} |
296 |
|
297 |
VMExecContext* ScriptVM::createExecContext(VMParserContext* parserContext) { |
298 |
ParserContext* parserCtx = dynamic_cast<ParserContext*>(parserContext); |
299 |
ExecContext* execCtx = new ExecContext(); |
300 |
|
301 |
if (parserCtx->requiredMaxStackSize < 0) { |
302 |
parserCtx->requiredMaxStackSize = |
303 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
304 |
} |
305 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
306 |
dmsg(2,("Created VM exec context with %" PRId64 " bytes VM stack size.\n", |
307 |
int64_t(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
308 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
309 |
const vmint polyIntSize = parserCtx->polyphonicIntVarCount; |
310 |
execCtx->polyphonicIntMemory.resize(polyIntSize); |
311 |
memset(&execCtx->polyphonicIntMemory[0], 0, polyIntSize * sizeof(vmint)); |
312 |
|
313 |
const vmint polyRealSize = parserCtx->polyphonicRealVarCount; |
314 |
execCtx->polyphonicRealMemory.resize(polyRealSize); |
315 |
memset(&execCtx->polyphonicRealMemory[0], 0, polyRealSize * sizeof(vmfloat)); |
316 |
|
317 |
const vmint polyFactorSize = parserCtx->polyphonicUnitFactorCount; |
318 |
execCtx->polyphonicUnitFactorMemory.resize(polyFactorSize); |
319 |
for (vmint i = 0; i < polyFactorSize; ++i) |
320 |
execCtx->polyphonicUnitFactorMemory[i] = VM_NO_FACTOR; |
321 |
|
322 |
dmsg(2,("Allocated %" PRId64 " bytes polyphonic int memory.\n", int64_t(polyIntSize * sizeof(vmint)))); |
323 |
dmsg(2,("Allocated %" PRId64 " bytes polyphonic real memory.\n", int64_t(polyRealSize * sizeof(vmfloat)))); |
324 |
dmsg(2,("Allocated %" PRId64 " bytes unit factor memory.\n", int64_t(polyFactorSize * sizeof(vmfloat)))); |
325 |
return execCtx; |
326 |
} |
327 |
|
328 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
329 |
std::istringstream iss(s); |
330 |
return syntaxHighlighting(&iss); |
331 |
} |
332 |
|
333 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
334 |
try { |
335 |
NkspScanner scanner(is); |
336 |
std::vector<SourceToken> tokens = scanner.tokens(); |
337 |
std::vector<VMSourceToken> result; |
338 |
result.resize(tokens.size()); |
339 |
for (vmint i = 0; i < tokens.size(); ++i) { |
340 |
SourceToken* st = new SourceToken; |
341 |
*st = tokens[i]; |
342 |
result[i] = VMSourceToken(st); |
343 |
} |
344 |
return result; |
345 |
} catch (...) { |
346 |
return std::vector<VMSourceToken>(); |
347 |
} |
348 |
} |
349 |
|
350 |
VMFunction* ScriptVM::functionByName(const String& name) { |
351 |
if (name == "message") return m_fnMessage; |
352 |
else if (name == "exit") return m_fnExit; |
353 |
else if (name == "wait") return m_fnWait; |
354 |
else if (name == "abs") return m_fnAbs; |
355 |
else if (name == "random") return m_fnRandom; |
356 |
else if (name == "num_elements") return m_fnNumElements; |
357 |
else if (name == "inc") return m_fnInc; |
358 |
else if (name == "dec") return m_fnDec; |
359 |
else if (name == "in_range") return m_fnInRange; |
360 |
else if (name == "sh_left") return m_fnShLeft; |
361 |
else if (name == "sh_right") return m_fnShRight; |
362 |
else if (name == "msb") return m_fnMsb; |
363 |
else if (name == "lsb") return m_fnLsb; |
364 |
else if (name == "min") return m_fnMin; |
365 |
else if (name == "max") return m_fnMax; |
366 |
else if (name == "array_equal") return m_fnArrayEqual; |
367 |
else if (name == "search") return m_fnSearch; |
368 |
else if (name == "sort") return m_fnSort; |
369 |
else if (name == "int_to_real") return m_fnIntToReal; |
370 |
else if (name == "real") return m_fnIntToReal; |
371 |
else if (name == "real_to_int") return m_fnRealToInt; |
372 |
else if (name == "int") return m_fnRealToInt; |
373 |
else if (name == "round") return m_fnRound; |
374 |
else if (name == "ceil") return m_fnCeil; |
375 |
else if (name == "floor") return m_fnFloor; |
376 |
else if (name == "sqrt") return m_fnSqrt; |
377 |
else if (name == "log") return m_fnLog; |
378 |
else if (name == "log2") return m_fnLog2; |
379 |
else if (name == "log10") return m_fnLog10; |
380 |
else if (name == "exp") return m_fnExp; |
381 |
else if (name == "pow") return m_fnPow; |
382 |
else if (name == "sin") return m_fnSin; |
383 |
else if (name == "cos") return m_fnCos; |
384 |
else if (name == "tan") return m_fnTan; |
385 |
else if (name == "asin") return m_fnAsin; |
386 |
else if (name == "acos") return m_fnAcos; |
387 |
else if (name == "atan") return m_fnAtan; |
388 |
return NULL; |
389 |
} |
390 |
|
391 |
bool ScriptVM::isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) { |
392 |
ParserContext* parserCtx = dynamic_cast<ParserContext*>(ctx); |
393 |
if (!parserCtx) return false; |
394 |
|
395 |
if (fn == m_fnMessage && parserCtx->userPreprocessorConditions.count("NKSP_NO_MESSAGE")) |
396 |
return true; |
397 |
|
398 |
return false; |
399 |
} |
400 |
|
401 |
std::map<String,VMIntPtr*> ScriptVM::builtInIntVariables() { |
402 |
return std::map<String,VMIntPtr*>(); |
403 |
} |
404 |
|
405 |
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
406 |
return std::map<String,VMInt8Array*>(); |
407 |
} |
408 |
|
409 |
std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() { |
410 |
std::map<String,VMDynVar*> m; |
411 |
|
412 |
m["$NKSP_PERF_TIMER"] = m_varPerfTimer; |
413 |
m["$NKSP_REAL_TIMER"] = m_varRealTimer; |
414 |
m["$KSP_TIMER"] = m_varRealTimer; |
415 |
|
416 |
return m; |
417 |
} |
418 |
|
419 |
std::map<String,vmint> ScriptVM::builtInConstIntVariables() { |
420 |
std::map<String,vmint> m; |
421 |
|
422 |
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
423 |
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
424 |
m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE; |
425 |
m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER; |
426 |
m["$NI_CB_TYPE_RPN"] = VM_EVENT_HANDLER_RPN; |
427 |
m["$NI_CB_TYPE_NRPN"] = VM_EVENT_HANDLER_NRPN; |
428 |
|
429 |
return m; |
430 |
} |
431 |
|
432 |
std::map<String,vmfloat> ScriptVM::builtInConstRealVariables() { |
433 |
std::map<String,vmfloat> m; |
434 |
|
435 |
m["~NI_MATH_PI"] = M_PI; |
436 |
m["~NI_MATH_E"] = M_E; |
437 |
|
438 |
return m; |
439 |
} |
440 |
|
441 |
VMEventHandler* ScriptVM::currentVMEventHandler() { |
442 |
return m_eventHandler; |
443 |
} |
444 |
|
445 |
VMParserContext* ScriptVM::currentVMParserContext() { |
446 |
return m_parserContext; |
447 |
} |
448 |
|
449 |
VMExecContext* ScriptVM::currentVMExecContext() { |
450 |
if (!m_parserContext) return NULL; |
451 |
return m_parserContext->execContext; |
452 |
} |
453 |
|
454 |
void ScriptVM::setAutoSuspendEnabled(bool b) { |
455 |
m_autoSuspend = b; |
456 |
} |
457 |
|
458 |
bool ScriptVM::isAutoSuspendEnabled() const { |
459 |
return m_autoSuspend; |
460 |
} |
461 |
|
462 |
void ScriptVM::setExitResultEnabled(bool b) { |
463 |
m_acceptExitRes = b; |
464 |
} |
465 |
|
466 |
bool ScriptVM::isExitResultEnabled() const { |
467 |
return m_acceptExitRes; |
468 |
} |
469 |
|
470 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
471 |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
472 |
if (!m_parserContext) { |
473 |
std::cerr << "No VM parser context provided. Did you load a script?\n"; |
474 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
475 |
} |
476 |
|
477 |
// a ParserContext object is always tied to exactly one ScriptVM object |
478 |
assert(m_parserContext->functionProvider == this); |
479 |
|
480 |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
481 |
if (!ctx) { |
482 |
std::cerr << "Invalid VM exec context.\n"; |
483 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
484 |
} |
485 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
486 |
if (!h) return VM_EXEC_NOT_RUNNING; |
487 |
m_eventHandler = handler; |
488 |
|
489 |
m_parserContext->execContext = ctx; |
490 |
|
491 |
ctx->status = VM_EXEC_RUNNING; |
492 |
ctx->instructionsCount = 0; |
493 |
ctx->clearExitRes(); |
494 |
StmtFlags_t& flags = ctx->flags; |
495 |
vmint instructionsCounter = 0; |
496 |
vmint synced = m_autoSuspend ? 0 : 1; |
497 |
|
498 |
int& frameIdx = ctx->stackFrame; |
499 |
if (frameIdx < 0) { // start condition ... |
500 |
frameIdx = -1; |
501 |
ctx->pushStack(h); |
502 |
} |
503 |
|
504 |
while (flags == STMT_SUCCESS && frameIdx >= 0) { |
505 |
if (frameIdx >= ctx->stack.size()) { // should never happen, otherwise it's a bug ... |
506 |
std::cerr << "CRITICAL: VM stack overflow! (" << frameIdx << ")\n"; |
507 |
flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
508 |
break; |
509 |
} |
510 |
|
511 |
ExecContext::StackFrame& frame = ctx->stack[frameIdx]; |
512 |
switch (frame.statement->statementType()) { |
513 |
case STMT_LEAF: { |
514 |
#if DEBUG_SCRIPTVM_CORE |
515 |
_printIndents(frameIdx); |
516 |
printf("-> STMT_LEAF\n"); |
517 |
#endif |
518 |
LeafStatement* leaf = (LeafStatement*) frame.statement; |
519 |
flags = leaf->exec(); |
520 |
ctx->popStack(); |
521 |
break; |
522 |
} |
523 |
|
524 |
case STMT_LIST: { |
525 |
#if DEBUG_SCRIPTVM_CORE |
526 |
_printIndents(frameIdx); |
527 |
printf("-> STMT_LIST subidx=%d\n", frame.subindex); |
528 |
#endif |
529 |
Statements* stmts = (Statements*) frame.statement; |
530 |
if (stmts->statement(frame.subindex)) { |
531 |
ctx->pushStack( |
532 |
stmts->statement(frame.subindex++) |
533 |
); |
534 |
} else { |
535 |
#if DEBUG_SCRIPTVM_CORE |
536 |
_printIndents(frameIdx); |
537 |
printf("[END OF LIST] subidx=%d\n", frame.subindex); |
538 |
#endif |
539 |
ctx->popStack(); |
540 |
} |
541 |
break; |
542 |
} |
543 |
|
544 |
case STMT_BRANCH: { |
545 |
#if DEBUG_SCRIPTVM_CORE |
546 |
_printIndents(frameIdx); |
547 |
printf("-> STMT_BRANCH\n"); |
548 |
#endif |
549 |
if (frame.subindex < 0) ctx->popStack(); |
550 |
else { |
551 |
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
552 |
frame.subindex = |
553 |
(decltype(frame.subindex)) |
554 |
branchStmt->evalBranch(); |
555 |
if (frame.subindex >= 0) { |
556 |
ctx->pushStack( |
557 |
branchStmt->branch(frame.subindex) |
558 |
); |
559 |
frame.subindex = -1; |
560 |
} else ctx->popStack(); |
561 |
} |
562 |
break; |
563 |
} |
564 |
|
565 |
case STMT_LOOP: { |
566 |
#if DEBUG_SCRIPTVM_CORE |
567 |
_printIndents(frameIdx); |
568 |
printf("-> STMT_LOOP\n"); |
569 |
#endif |
570 |
While* whileStmt = (While*) frame.statement; |
571 |
if (whileStmt->evalLoopStartCondition() && whileStmt->statements()) { |
572 |
ctx->pushStack( |
573 |
whileStmt->statements() |
574 |
); |
575 |
if (flags == STMT_SUCCESS && !synced && |
576 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT) |
577 |
{ |
578 |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
579 |
ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS; |
580 |
} |
581 |
} else ctx->popStack(); |
582 |
break; |
583 |
} |
584 |
|
585 |
case STMT_SYNC: { |
586 |
#if DEBUG_SCRIPTVM_CORE |
587 |
_printIndents(frameIdx); |
588 |
printf("-> STMT_SYNC\n"); |
589 |
#endif |
590 |
SyncBlock* syncStmt = (SyncBlock*) frame.statement; |
591 |
if (!frame.subindex++ && syncStmt->statements()) { |
592 |
++synced; |
593 |
ctx->pushStack( |
594 |
syncStmt->statements() |
595 |
); |
596 |
} else { |
597 |
ctx->popStack(); |
598 |
--synced; |
599 |
} |
600 |
break; |
601 |
} |
602 |
|
603 |
case STMT_NOOP: |
604 |
break; // no operation like the name suggests |
605 |
} |
606 |
|
607 |
if (flags == STMT_SUCCESS && !synced && |
608 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD) |
609 |
{ |
610 |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
611 |
ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS; |
612 |
} |
613 |
|
614 |
++instructionsCounter; |
615 |
} |
616 |
|
617 |
if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) { |
618 |
ctx->status = VM_EXEC_SUSPENDED; |
619 |
ctx->flags = STMT_SUCCESS; |
620 |
} else { |
621 |
ctx->status = VM_EXEC_NOT_RUNNING; |
622 |
if (flags & STMT_ERROR_OCCURRED) |
623 |
ctx->status = VM_EXEC_ERROR; |
624 |
ctx->reset(); |
625 |
} |
626 |
|
627 |
ctx->instructionsCount = instructionsCounter; |
628 |
|
629 |
m_eventHandler = NULL; |
630 |
m_parserContext->execContext = NULL; |
631 |
m_parserContext = NULL; |
632 |
return ctx->status; |
633 |
} |
634 |
|
635 |
} // namespace LinuxSampler |