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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2948 by schoenebeck, Fri Jul 15 15:29:04 2016 UTC revision 3678 by schoenebeck, Fri Dec 27 22:46:08 2019 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 - 2016 Christian Schoenebeck   * Copyright (c) 2014 - 2019 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 19  Line 19 
19    
20  #define DEBUG_SCRIPTVM_CORE 0  #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*);  int InstrScript_parse(LinuxSampler::ParserContext*);
60    
61  namespace LinuxSampler {  namespace LinuxSampler {
62    
63        #if DEBUG_SCRIPTVM_CORE
64      static void _printIndents(int n) {      static void _printIndents(int n) {
65          for (int i = 0; i < n; ++i) printf("  ");          for (int i = 0; i < n; ++i) printf("  ");
66          fflush(stdout);          fflush(stdout);
67      }      }
68        #endif
69    
70      static int _requiredMaxStackSizeFor(Statement* statement, int depth = 0) {      static vmint _requiredMaxStackSizeFor(Statement* statement, vmint depth = 0) {
71          if (!statement) return 1;          if (!statement) return 1;
72    
73          switch (statement->statementType()) {          switch (statement->statementType()) {
# Line 45  namespace LinuxSampler { Line 84  namespace LinuxSampler {
84                  printf("-> STMT_LIST\n");                  printf("-> STMT_LIST\n");
85                  #endif                  #endif
86                  Statements* stmts = (Statements*) statement;                  Statements* stmts = (Statements*) statement;
87                  int max = 0;                  vmint max = 0;
88                  for (int i = 0; stmts->statement(i); ++i) {                  for (int i = 0; stmts->statement(i); ++i) {
89                      int size = _requiredMaxStackSizeFor( stmts->statement(i), depth+1 );                      vmint size = _requiredMaxStackSizeFor( stmts->statement(i), depth+1 );
90                      if (max < size) max = size;                      if (max < size) max = size;
91                  }                  }
92                  return max + 1;                  return max + 1;
# Line 59  namespace LinuxSampler { Line 98  namespace LinuxSampler {
98                  printf("-> STMT_BRANCH\n");                  printf("-> STMT_BRANCH\n");
99                  #endif                  #endif
100                  BranchStatement* branchStmt = (BranchStatement*) statement;                  BranchStatement* branchStmt = (BranchStatement*) statement;
101                  int max = 0;                  vmint max = 0;
102                  for (int i = 0; branchStmt->branch(i); ++i) {                  for (int i = 0; branchStmt->branch(i); ++i) {
103                      int size = _requiredMaxStackSizeFor( branchStmt->branch(i), depth+1 );                      vmint size = _requiredMaxStackSizeFor( branchStmt->branch(i), depth+1 );
104                      if (max < size) max = size;                      if (max < size) max = size;
105                  }                  }
106                  return max + 1;                  return max + 1;
# Line 78  namespace LinuxSampler { Line 117  namespace LinuxSampler {
117                  else                  else
118                      return 1;                      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          return 1; // actually just to avoid compiler warning
138      }      }
139    
140      static int _requiredMaxStackSizeFor(EventHandlers* handlers) {      static vmint _requiredMaxStackSizeFor(EventHandlers* handlers) {
141          int max = 1;          vmint max = 1;
142          for (int i = 0; i < handlers->size(); ++i) {          for (int i = 0; i < handlers->size(); ++i) {
143              int size = _requiredMaxStackSizeFor(handlers->eventHandler(i));              vmint size = _requiredMaxStackSizeFor(handlers->eventHandler(i));
144              if (max < size) max = size;              if (max < size) max = size;
145          }          }
146          return max;          return max;
147      }      }
148    
149      ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) {      ScriptVM::ScriptVM() :
150            m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true),
151            m_acceptExitRes(false)
152        {
153          m_fnMessage = new CoreVMFunction_message;          m_fnMessage = new CoreVMFunction_message;
154          m_fnExit = new CoreVMFunction_exit;          m_fnExit = new CoreVMFunction_exit(this);
155          m_fnWait = new CoreVMFunction_wait(this);          m_fnWait = new CoreVMFunction_wait(this);
156          m_fnAbs = new CoreVMFunction_abs;          m_fnAbs = new CoreVMFunction_abs;
157          m_fnRandom = new CoreVMFunction_random;          m_fnRandom = new CoreVMFunction_random;
158          m_fnNumElements = new CoreVMFunction_num_elements;          m_fnNumElements = new CoreVMFunction_num_elements;
159          m_fnInc = new CoreVMFunction_inc;          m_fnInc = new CoreVMFunction_inc;
160          m_fnDec = new CoreVMFunction_dec;          m_fnDec = new CoreVMFunction_dec;
161            m_fnInRange = new CoreVMFunction_in_range;
162          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;
163          m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;          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() {      ScriptVM::~ScriptVM() {
# Line 114  namespace LinuxSampler { Line 198  namespace LinuxSampler {
198          delete m_fnNumElements;          delete m_fnNumElements;
199          delete m_fnInc;          delete m_fnInc;
200          delete m_fnDec;          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;          delete m_varRealTimer;
229          delete m_varPerfTimer;          delete m_varPerfTimer;
230      }      }
# Line 128  namespace LinuxSampler { Line 239  namespace LinuxSampler {
239          //printf("parserCtx=0x%lx\n", (uint64_t)context);          //printf("parserCtx=0x%lx\n", (uint64_t)context);
240    
241          context->registerBuiltInConstIntVariables( builtInConstIntVariables() );          context->registerBuiltInConstIntVariables( builtInConstIntVariables() );
242            context->registerBuiltInConstRealVariables( builtInConstRealVariables() );
243          context->registerBuiltInIntVariables( builtInIntVariables() );          context->registerBuiltInIntVariables( builtInIntVariables() );
244          context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() );          context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() );
245          context->registerBuiltInDynVariables( builtInDynamicVariables() );          context->registerBuiltInDynVariables( builtInDynamicVariables() );
# Line 135  namespace LinuxSampler { Line 247  namespace LinuxSampler {
247          context->createScanner(is);          context->createScanner(is);
248    
249          InstrScript_parse(context);          InstrScript_parse(context);
250          dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int))));          dmsg(2,("Allocating %lld bytes of global int VM memory.\n", context->globalIntVarCount * sizeof(vmint)));
251          dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount));          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            dmsg(2,("Allocating %lld of global VM string variables.\n", context->globalStrVarCount));
254          if (!context->globalIntMemory)          if (!context->globalIntMemory)
255              context->globalIntMemory = new ArrayList<int>();              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)          if (!context->globalStrMemory)
261              context->globalStrMemory = new ArrayList<String>();              context->globalStrMemory = new ArrayList<String>();
262          context->globalIntMemory->resize(context->globalIntVarCount);          context->globalIntMemory->resize(context->globalIntVarCount);
263          memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int));          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);          context->globalStrMemory->resize(context->globalStrVarCount);
270    
271          context->destroyScanner();          context->destroyScanner();
# Line 162  namespace LinuxSampler { Line 284  namespace LinuxSampler {
284              return;              return;
285          }          }
286          if (!ctx->globalIntMemory) {          if (!ctx->globalIntMemory) {
287              std::cerr << "Internal error: no global memory assigend to script VM.\n";              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;              return;
293          }          }
294          ctx->handlers->dump();          ctx->handlers->dump();
# Line 177  namespace LinuxSampler { Line 303  namespace LinuxSampler {
303                  _requiredMaxStackSizeFor(&*parserCtx->handlers);                  _requiredMaxStackSizeFor(&*parserCtx->handlers);
304          }          }
305          execCtx->stack.resize(parserCtx->requiredMaxStackSize);          execCtx->stack.resize(parserCtx->requiredMaxStackSize);
306          dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n",          dmsg(2,("Created VM exec context with %lld bytes VM stack size.\n",
307                  long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))));                  parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)));
308          //printf("execCtx=0x%lx\n", (uint64_t)execCtx);          //printf("execCtx=0x%lx\n", (uint64_t)execCtx);
309          const int polySize = parserCtx->polyphonicIntVarCount;          const vmint polyIntSize = parserCtx->polyphonicIntVarCount;
310          execCtx->polyphonicIntMemory.resize(polySize);          execCtx->polyphonicIntMemory.resize(polyIntSize);
311          memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int));          memset(&execCtx->polyphonicIntMemory[0], 0, polyIntSize * sizeof(vmint));
312    
313          dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int))));          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          return execCtx;          return execCtx;
326      }      }
327    
# Line 194  namespace LinuxSampler { Line 331  namespace LinuxSampler {
331      }      }
332    
333      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {
334          NkspScanner scanner(is);          try {
335          std::vector<SourceToken> tokens = scanner.tokens();              NkspScanner scanner(is);
336          std::vector<VMSourceToken> result;              std::vector<SourceToken> tokens = scanner.tokens();
337          result.resize(tokens.size());              std::vector<VMSourceToken> result;
338          for (int i = 0; i < tokens.size(); ++i) {              result.resize(tokens.size());
339              SourceToken* st = new SourceToken;              for (vmint i = 0; i < tokens.size(); ++i) {
340              *st = tokens[i];                  SourceToken* st = new SourceToken;
341              result[i] = VMSourceToken(st);                  *st = tokens[i];
342                    result[i] = VMSourceToken(st);
343                }
344                return result;
345            } catch (...) {
346                return std::vector<VMSourceToken>();
347          }          }
         return result;  
348      }      }
349    
350      VMFunction* ScriptVM::functionByName(const String& name) {      VMFunction* ScriptVM::functionByName(const String& name) {
# Line 215  namespace LinuxSampler { Line 356  namespace LinuxSampler {
356          else if (name == "num_elements") return m_fnNumElements;          else if (name == "num_elements") return m_fnNumElements;
357          else if (name == "inc") return m_fnInc;          else if (name == "inc") return m_fnInc;
358          else if (name == "dec") return m_fnDec;          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;          return NULL;
389      }      }
390    
391      std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {      bool ScriptVM::isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) {
392          return std::map<String,VMIntRelPtr*>();          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() {      std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() {
# Line 236  namespace LinuxSampler { Line 416  namespace LinuxSampler {
416          return m;          return m;
417      }      }
418    
419      std::map<String,int> ScriptVM::builtInConstIntVariables() {      std::map<String,vmint> ScriptVM::builtInConstIntVariables() {
420          std::map<String,int> m;          std::map<String,vmint> m;
421    
422          m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;          m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;
423          m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;          m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;
# Line 247  namespace LinuxSampler { Line 427  namespace LinuxSampler {
427          return m;          return m;
428      }      }
429    
430        std::map<String,vmfloat> ScriptVM::builtInConstRealVariables() {
431            std::map<String,vmfloat> m;
432    
433            m["~NI_MATH_PI"] = M_PI;
434            m["~NI_MATH_E"] = M_E;
435    
436            return m;
437        }
438    
439      VMEventHandler* ScriptVM::currentVMEventHandler() {      VMEventHandler* ScriptVM::currentVMEventHandler() {
440          return m_eventHandler;          return m_eventHandler;
441      }      }
# Line 260  namespace LinuxSampler { Line 449  namespace LinuxSampler {
449          return m_parserContext->execContext;          return m_parserContext->execContext;
450      }      }
451    
452        void ScriptVM::setAutoSuspendEnabled(bool b) {
453            m_autoSuspend = b;
454        }
455    
456        bool ScriptVM::isAutoSuspendEnabled() const {
457            return m_autoSuspend;
458        }
459    
460        void ScriptVM::setExitResultEnabled(bool b) {
461            m_acceptExitRes = b;
462        }
463    
464        bool ScriptVM::isExitResultEnabled() const {
465            return m_acceptExitRes;
466        }
467    
468      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
469          m_parserContext = dynamic_cast<ParserContext*>(parserContext);          m_parserContext = dynamic_cast<ParserContext*>(parserContext);
470          if (!m_parserContext) {          if (!m_parserContext) {
471              std::cerr << "No VM parser context provided. Did you load a script?.\n";              std::cerr << "No VM parser context provided. Did you load a script?\n";
472              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);
473          }          }
474    
# Line 282  namespace LinuxSampler { Line 487  namespace LinuxSampler {
487          m_parserContext->execContext = ctx;          m_parserContext->execContext = ctx;
488    
489          ctx->status = VM_EXEC_RUNNING;          ctx->status = VM_EXEC_RUNNING;
490          StmtFlags_t flags = STMT_SUCCESS;          ctx->instructionsCount = 0;
491            ctx->clearExitRes();
492            StmtFlags_t& flags = ctx->flags;
493            vmint instructionsCounter = 0;
494            vmint synced = m_autoSuspend ? 0 : 1;
495    
496          int& frameIdx = ctx->stackFrame;          int& frameIdx = ctx->stackFrame;
497          if (frameIdx < 0) { // start condition ...          if (frameIdx < 0) { // start condition ...
# Line 338  namespace LinuxSampler { Line 547  namespace LinuxSampler {
547                      if (frame.subindex < 0) ctx->popStack();                      if (frame.subindex < 0) ctx->popStack();
548                      else {                      else {
549                          BranchStatement* branchStmt = (BranchStatement*) frame.statement;                          BranchStatement* branchStmt = (BranchStatement*) frame.statement;
550                          frame.subindex = branchStmt->evalBranch();                          frame.subindex =
551                                (decltype(frame.subindex))
552                                    branchStmt->evalBranch();
553                          if (frame.subindex >= 0) {                          if (frame.subindex >= 0) {
554                              ctx->pushStack(                              ctx->pushStack(
555                                  branchStmt->branch(frame.subindex)                                  branchStmt->branch(frame.subindex)
# Line 359  namespace LinuxSampler { Line 570  namespace LinuxSampler {
570                          ctx->pushStack(                          ctx->pushStack(
571                              whileStmt->statements()                              whileStmt->statements()
572                          );                          );
573                            if (flags == STMT_SUCCESS && !synced &&
574                                instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT)
575                            {
576                                flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
577                                ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
578                            }
579                      } else ctx->popStack();                      } else ctx->popStack();
580                        break;
581                    }
582    
583                    case STMT_SYNC: {
584                        #if DEBUG_SCRIPTVM_CORE
585                        _printIndents(frameIdx);
586                        printf("-> STMT_SYNC\n");
587                        #endif
588                        SyncBlock* syncStmt = (SyncBlock*) frame.statement;
589                        if (!frame.subindex++ && syncStmt->statements()) {
590                            ++synced;
591                            ctx->pushStack(
592                                syncStmt->statements()
593                            );
594                        } else {
595                            ctx->popStack();
596                            --synced;
597                        }
598                        break;
599                  }                  }
600    
601                    case STMT_NOOP:
602                        break; // no operation like the name suggests
603              }              }
604    
605                if (flags == STMT_SUCCESS && !synced &&
606                    instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD)
607                {
608                    flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
609                    ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
610                }
611    
612                ++instructionsCounter;
613          }          }
614    
615          if (flags & STMT_SUSPEND_SIGNALLED) {          if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) {
616              ctx->status = VM_EXEC_SUSPENDED;              ctx->status = VM_EXEC_SUSPENDED;
617                ctx->flags  = STMT_SUCCESS;
618          } else {          } else {
619              ctx->status = VM_EXEC_NOT_RUNNING;              ctx->status = VM_EXEC_NOT_RUNNING;
620              if (flags & STMT_ERROR_OCCURRED)              if (flags & STMT_ERROR_OCCURRED)
# Line 373  namespace LinuxSampler { Line 622  namespace LinuxSampler {
622              ctx->reset();              ctx->reset();
623          }          }
624    
625            ctx->instructionsCount = instructionsCounter;
626    
627          m_eventHandler = NULL;          m_eventHandler = NULL;
628          m_parserContext->execContext = NULL;          m_parserContext->execContext = NULL;
629          m_parserContext = NULL;          m_parserContext = NULL;

Legend:
Removed from v.2948  
changed lines
  Added in v.3678

  ViewVC Help
Powered by ViewVC