/[svn]/linuxsampler/trunk/src/scriptvm/ScriptVM.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/scriptvm/ScriptVM.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3690 - (hide annotations) (download)
Fri Jan 3 10:18:21 2020 UTC (4 years, 2 months ago) by schoenebeck
File size: 24352 byte(s)
NKSP: Added support for RPN and NRPN event handlers:

* NKSP language: Added support for RPN event handler
  ("on rpn ... end on" in instrument scripts).

* NKSP language: Added support for NRPN event handler
  ("on nrpn ... end on" in instrument scripts).

* Added built-in read-only variables "$RPN_ADDRESS" and "$RPN_VALUE" which
  may be read from the new RPN/NRPN script handlers to get the (N)RPN
  parameter that had been changed and its new value.

* Added built-in const variables "$NI_CB_TYPE_RPN" and "$NI_CB_TYPE_NRPN"
  which are identifying the new (N)RPN handlers as such at script runtime.

* Bumped version (2.1.1.svn30).

1 schoenebeck 2581 /*
2 schoenebeck 3690 * Copyright (c) 2014 - 2020 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 3678 m_fnMsb = new CoreVMFunction_msb;
167     m_fnLsb = new CoreVMFunction_lsb;
168 schoenebeck 2970 m_fnMin = new CoreVMFunction_min;
169     m_fnMax = new CoreVMFunction_max;
170 schoenebeck 3221 m_fnArrayEqual = new CoreVMFunction_array_equal;
171     m_fnSearch = new CoreVMFunction_search;
172     m_fnSort = new CoreVMFunction_sort;
173 schoenebeck 3573 m_fnIntToReal = new CoreVMFunction_int_to_real;
174     m_fnRealToInt = new CoreVMFunction_real_to_int;
175 schoenebeck 3590 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 schoenebeck 2581 }
191    
192     ScriptVM::~ScriptVM() {
193 schoenebeck 2885 delete m_fnMessage;
194     delete m_fnExit;
195     delete m_fnWait;
196     delete m_fnAbs;
197     delete m_fnRandom;
198     delete m_fnNumElements;
199 schoenebeck 2945 delete m_fnInc;
200     delete m_fnDec;
201 schoenebeck 3076 delete m_fnInRange;
202 schoenebeck 2965 delete m_fnShLeft;
203     delete m_fnShRight;
204 schoenebeck 3678 delete m_fnMsb;
205     delete m_fnLsb;
206 schoenebeck 2970 delete m_fnMin;
207     delete m_fnMax;
208 schoenebeck 3221 delete m_fnArrayEqual;
209     delete m_fnSearch;
210     delete m_fnSort;
211 schoenebeck 3573 delete m_fnIntToReal;
212     delete m_fnRealToInt;
213 schoenebeck 3590 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 schoenebeck 2942 delete m_varRealTimer;
229     delete m_varPerfTimer;
230 schoenebeck 2581 }
231    
232 schoenebeck 2588 VMParserContext* ScriptVM::loadScript(const String& s) {
233 schoenebeck 2581 std::istringstream iss(s);
234 schoenebeck 2588 return loadScript(&iss);
235 schoenebeck 2581 }
236    
237 schoenebeck 2588 VMParserContext* ScriptVM::loadScript(std::istream* is) {
238     ParserContext* context = new ParserContext(this);
239     //printf("parserCtx=0x%lx\n", (uint64_t)context);
240 schoenebeck 2594
241     context->registerBuiltInConstIntVariables( builtInConstIntVariables() );
242 schoenebeck 3590 context->registerBuiltInConstRealVariables( builtInConstRealVariables() );
243 schoenebeck 2594 context->registerBuiltInIntVariables( builtInIntVariables() );
244     context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() );
245 schoenebeck 2942 context->registerBuiltInDynVariables( builtInDynamicVariables() );
246 schoenebeck 2594
247 schoenebeck 2588 context->createScanner(is);
248 schoenebeck 2581
249 schoenebeck 2588 InstrScript_parse(context);
250 schoenebeck 3557 dmsg(2,("Allocating %lld bytes of global int VM memory.\n", context->globalIntVarCount * sizeof(vmint)));
251 schoenebeck 3581 dmsg(2,("Allocating %lld bytes of global real VM memory.\n", context->globalRealVarCount * sizeof(vmfloat)));
252     dmsg(2,("Allocating %lld bytes of global unit factor VM memory.\n", context->globalUnitFactorCount * sizeof(vmfloat)));
253 schoenebeck 3557 dmsg(2,("Allocating %lld of global VM string variables.\n", context->globalStrVarCount));
254 schoenebeck 2588 if (!context->globalIntMemory)
255 schoenebeck 3557 context->globalIntMemory = new ArrayList<vmint>();
256 schoenebeck 3573 if (!context->globalRealMemory)
257     context->globalRealMemory = new ArrayList<vmfloat>();
258 schoenebeck 3581 if (!context->globalUnitFactorMemory)
259     context->globalUnitFactorMemory = new ArrayList<vmfloat>();
260 schoenebeck 2588 if (!context->globalStrMemory)
261     context->globalStrMemory = new ArrayList<String>();
262     context->globalIntMemory->resize(context->globalIntVarCount);
263 schoenebeck 3573 context->globalRealMemory->resize(context->globalRealVarCount);
264 schoenebeck 3581 context->globalUnitFactorMemory->resize(context->globalUnitFactorCount);
265 schoenebeck 3557 memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(vmint));
266 schoenebeck 3573 memset(&((*context->globalRealMemory)[0]), 0, context->globalRealVarCount * sizeof(vmfloat));
267 schoenebeck 3581 for (vmint i = 0; i < context->globalUnitFactorCount; ++i)
268     (*context->globalUnitFactorMemory)[i] = VM_NO_FACTOR;
269 schoenebeck 2588 context->globalStrMemory->resize(context->globalStrVarCount);
270 schoenebeck 2581
271 schoenebeck 2588 context->destroyScanner();
272 schoenebeck 2581
273 schoenebeck 2588 return context;
274 schoenebeck 2581 }
275    
276 schoenebeck 2588 void ScriptVM::dumpParsedScript(VMParserContext* context) {
277     ParserContext* ctx = dynamic_cast<ParserContext*>(context);
278     if (!ctx) {
279 schoenebeck 2581 std::cerr << "No VM context. So nothing to dump.\n";
280     return;
281     }
282 schoenebeck 2588 if (!ctx->handlers) {
283 schoenebeck 2581 std::cerr << "No event handlers defined in script. So nothing to dump.\n";
284     return;
285     }
286 schoenebeck 2588 if (!ctx->globalIntMemory) {
287 schoenebeck 3573 std::cerr << "Internal error: no global integer memory assigend to script VM.\n";
288 schoenebeck 2581 return;
289     }
290 schoenebeck 3573 if (!ctx->globalRealMemory) {
291     std::cerr << "Internal error: no global real number memory assigend to script VM.\n";
292     return;
293     }
294 schoenebeck 2588 ctx->handlers->dump();
295 schoenebeck 2581 }
296    
297 schoenebeck 2588 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 schoenebeck 3557 dmsg(2,("Created VM exec context with %lld bytes VM stack size.\n",
307     parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)));
308 schoenebeck 2588 //printf("execCtx=0x%lx\n", (uint64_t)execCtx);
309 schoenebeck 3581 const vmint polyIntSize = parserCtx->polyphonicIntVarCount;
310     execCtx->polyphonicIntMemory.resize(polyIntSize);
311     memset(&execCtx->polyphonicIntMemory[0], 0, polyIntSize * sizeof(vmint));
312 schoenebeck 2588
313 schoenebeck 3581 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 %lld bytes polyphonic int memory.\n", polyIntSize * sizeof(vmint)));
323     dmsg(2,("Allocated %lld bytes polyphonic real memory.\n", polyRealSize * sizeof(vmfloat)));
324     dmsg(2,("Allocated %lld bytes unit factor memory.\n", polyFactorSize * sizeof(vmfloat)));
325 schoenebeck 2588 return execCtx;
326 schoenebeck 2581 }
327    
328 schoenebeck 2885 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 schoenebeck 3332 try {
335     NkspScanner scanner(is);
336     std::vector<SourceToken> tokens = scanner.tokens();
337     std::vector<VMSourceToken> result;
338     result.resize(tokens.size());
339 schoenebeck 3557 for (vmint i = 0; i < tokens.size(); ++i) {
340 schoenebeck 3332 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 schoenebeck 2885 }
348     }
349    
350 schoenebeck 2581 VMFunction* ScriptVM::functionByName(const String& name) {
351 schoenebeck 2885 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 schoenebeck 2945 else if (name == "inc") return m_fnInc;
358     else if (name == "dec") return m_fnDec;
359 schoenebeck 3076 else if (name == "in_range") return m_fnInRange;
360 schoenebeck 2965 else if (name == "sh_left") return m_fnShLeft;
361     else if (name == "sh_right") return m_fnShRight;
362 schoenebeck 3678 else if (name == "msb") return m_fnMsb;
363     else if (name == "lsb") return m_fnLsb;
364 schoenebeck 2970 else if (name == "min") return m_fnMin;
365     else if (name == "max") return m_fnMax;
366 schoenebeck 3221 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 schoenebeck 3573 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 schoenebeck 3590 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 schoenebeck 2581 return NULL;
389     }
390 schoenebeck 2588
391 schoenebeck 3311 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 schoenebeck 3557 std::map<String,VMIntPtr*> ScriptVM::builtInIntVariables() {
402     return std::map<String,VMIntPtr*>();
403 schoenebeck 2594 }
404    
405     std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() {
406     return std::map<String,VMInt8Array*>();
407     }
408    
409 schoenebeck 2942 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 schoenebeck 3557 std::map<String,vmint> ScriptVM::builtInConstIntVariables() {
420     std::map<String,vmint> m;
421 schoenebeck 2948
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 schoenebeck 3690 m["$NI_CB_TYPE_RPN"] = VM_EVENT_HANDLER_RPN;
427     m["$NI_CB_TYPE_NRPN"] = VM_EVENT_HANDLER_NRPN;
428 schoenebeck 2948
429     return m;
430 schoenebeck 2594 }
431    
432 schoenebeck 3590 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 schoenebeck 2879 VMEventHandler* ScriptVM::currentVMEventHandler() {
442     return m_eventHandler;
443     }
444    
445 schoenebeck 2588 VMParserContext* ScriptVM::currentVMParserContext() {
446     return m_parserContext;
447     }
448    
449 schoenebeck 2581 VMExecContext* ScriptVM::currentVMExecContext() {
450 schoenebeck 2588 if (!m_parserContext) return NULL;
451     return m_parserContext->execContext;
452 schoenebeck 2581 }
453    
454 schoenebeck 2974 void ScriptVM::setAutoSuspendEnabled(bool b) {
455     m_autoSuspend = b;
456     }
457    
458     bool ScriptVM::isAutoSuspendEnabled() const {
459     return m_autoSuspend;
460     }
461    
462 schoenebeck 3551 void ScriptVM::setExitResultEnabled(bool b) {
463     m_acceptExitRes = b;
464     }
465    
466     bool ScriptVM::isExitResultEnabled() const {
467     return m_acceptExitRes;
468     }
469    
470 schoenebeck 2588 VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
471     m_parserContext = dynamic_cast<ParserContext*>(parserContext);
472     if (!m_parserContext) {
473 schoenebeck 3557 std::cerr << "No VM parser context provided. Did you load a script?\n";
474 schoenebeck 2581 return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);
475     }
476    
477 schoenebeck 2611 // a ParserContext object is always tied to exactly one ScriptVM object
478     assert(m_parserContext->functionProvider == this);
479    
480 schoenebeck 2581 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 schoenebeck 2879 m_eventHandler = handler;
488 schoenebeck 2581
489 schoenebeck 2588 m_parserContext->execContext = ctx;
490 schoenebeck 2581
491     ctx->status = VM_EXEC_RUNNING;
492 schoenebeck 3221 ctx->instructionsCount = 0;
493 schoenebeck 3551 ctx->clearExitRes();
494 schoenebeck 3277 StmtFlags_t& flags = ctx->flags;
495 schoenebeck 3557 vmint instructionsCounter = 0;
496     vmint synced = m_autoSuspend ? 0 : 1;
497 schoenebeck 2581
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 schoenebeck 3557 frame.subindex =
553     (decltype(frame.subindex))
554     branchStmt->evalBranch();
555 schoenebeck 2581 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 schoenebeck 3260 if (flags == STMT_SUCCESS && !synced &&
576 schoenebeck 2974 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 schoenebeck 2581 } else ctx->popStack();
582 schoenebeck 2974 break;
583 schoenebeck 2581 }
584 schoenebeck 3260
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 schoenebeck 3557
603     case STMT_NOOP:
604     break; // no operation like the name suggests
605 schoenebeck 2581 }
606 schoenebeck 2974
607 schoenebeck 3260 if (flags == STMT_SUCCESS && !synced &&
608 schoenebeck 2974 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 schoenebeck 2581 }
616    
617 schoenebeck 3277 if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) {
618 schoenebeck 2581 ctx->status = VM_EXEC_SUSPENDED;
619 schoenebeck 3277 ctx->flags = STMT_SUCCESS;
620 schoenebeck 2581 } 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 schoenebeck 3221 ctx->instructionsCount = instructionsCounter;
628    
629 schoenebeck 2879 m_eventHandler = NULL;
630 schoenebeck 2588 m_parserContext->execContext = NULL;
631     m_parserContext = NULL;
632 schoenebeck 2581 return ctx->status;
633     }
634    
635     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC