/[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 3590 - (hide annotations) (download)
Mon Sep 2 09:03:31 2019 UTC (4 years, 7 months ago) by schoenebeck
File size: 24016 byte(s)
NKSP: Implemented common real number math functions.

* Added built-in real number functions "round()", "ceil()", "floor()",
  "sqrt()", "log()", "log2()", "log10()", "exp()", "pow()", "sin()",
  "cos()", "tan()", "asin()", "acos()", "atan()".

* Added built-in script real number constant "~NI_MATH_PI".

* Added built-in script real number constant "~NI_MATH_E".

* Added NKSP test cases for built-in functions "round()", "ceil()",
  "floor()", "sqrt()", "log()", "log2()", "log10()", "exp()", "pow()",
  "sin()", "cos()", "tan()", "asin()", "acos()", "atan()".

* Bumped version (2.1.1.svn14).

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

  ViewVC Help
Powered by ViewVC