/[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 2945 by schoenebeck, Thu Jul 14 00:22:26 2016 UTC revision 3551 by schoenebeck, Thu Aug 1 10:22:56 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 int _requiredMaxStackSizeFor(Statement* statement, int depth = 0) {
71          if (!statement) return 1;          if (!statement) return 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    
134          return 1; // actually just to avoid compiler warning          return 1; // actually just to avoid compiler warning
# Line 92  namespace LinuxSampler { Line 143  namespace LinuxSampler {
143          return max;          return max;
144      }      }
145    
146      ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) {      ScriptVM::ScriptVM() :
147            m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true),
148            m_acceptExitRes(false)
149        {
150          m_fnMessage = new CoreVMFunction_message;          m_fnMessage = new CoreVMFunction_message;
151          m_fnExit = new CoreVMFunction_exit;          m_fnExit = new CoreVMFunction_exit(this);
152          m_fnWait = new CoreVMFunction_wait(this);          m_fnWait = new CoreVMFunction_wait(this);
153          m_fnAbs = new CoreVMFunction_abs;          m_fnAbs = new CoreVMFunction_abs;
154          m_fnRandom = new CoreVMFunction_random;          m_fnRandom = new CoreVMFunction_random;
155          m_fnNumElements = new CoreVMFunction_num_elements;          m_fnNumElements = new CoreVMFunction_num_elements;
156          m_fnInc = new CoreVMFunction_inc;          m_fnInc = new CoreVMFunction_inc;
157          m_fnDec = new CoreVMFunction_dec;          m_fnDec = new CoreVMFunction_dec;
158            m_fnInRange = new CoreVMFunction_in_range;
159          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;
160          m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;          m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;
161            m_fnShLeft = new CoreVMFunction_sh_left;
162            m_fnShRight = new CoreVMFunction_sh_right;
163            m_fnMin = new CoreVMFunction_min;
164            m_fnMax = new CoreVMFunction_max;
165            m_fnArrayEqual = new CoreVMFunction_array_equal;
166            m_fnSearch = new CoreVMFunction_search;
167            m_fnSort = new CoreVMFunction_sort;
168      }      }
169    
170      ScriptVM::~ScriptVM() {      ScriptVM::~ScriptVM() {
# Line 114  namespace LinuxSampler { Line 176  namespace LinuxSampler {
176          delete m_fnNumElements;          delete m_fnNumElements;
177          delete m_fnInc;          delete m_fnInc;
178          delete m_fnDec;          delete m_fnDec;
179            delete m_fnInRange;
180            delete m_fnShLeft;
181            delete m_fnShRight;
182            delete m_fnMin;
183            delete m_fnMax;
184            delete m_fnArrayEqual;
185            delete m_fnSearch;
186            delete m_fnSort;
187          delete m_varRealTimer;          delete m_varRealTimer;
188          delete m_varPerfTimer;          delete m_varPerfTimer;
189      }      }
# Line 194  namespace LinuxSampler { Line 264  namespace LinuxSampler {
264      }      }
265    
266      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {
267          NkspScanner scanner(is);          try {
268          std::vector<SourceToken> tokens = scanner.tokens();              NkspScanner scanner(is);
269          std::vector<VMSourceToken> result;              std::vector<SourceToken> tokens = scanner.tokens();
270          result.resize(tokens.size());              std::vector<VMSourceToken> result;
271          for (int i = 0; i < tokens.size(); ++i) {              result.resize(tokens.size());
272              SourceToken* st = new SourceToken;              for (int i = 0; i < tokens.size(); ++i) {
273              *st = tokens[i];                  SourceToken* st = new SourceToken;
274              result[i] = VMSourceToken(st);                  *st = tokens[i];
275                    result[i] = VMSourceToken(st);
276                }
277                return result;
278            } catch (...) {
279                return std::vector<VMSourceToken>();
280          }          }
         return result;  
281      }      }
282    
283      VMFunction* ScriptVM::functionByName(const String& name) {      VMFunction* ScriptVM::functionByName(const String& name) {
# Line 215  namespace LinuxSampler { Line 289  namespace LinuxSampler {
289          else if (name == "num_elements") return m_fnNumElements;          else if (name == "num_elements") return m_fnNumElements;
290          else if (name == "inc") return m_fnInc;          else if (name == "inc") return m_fnInc;
291          else if (name == "dec") return m_fnDec;          else if (name == "dec") return m_fnDec;
292            else if (name == "in_range") return m_fnInRange;
293            else if (name == "sh_left") return m_fnShLeft;
294            else if (name == "sh_right") return m_fnShRight;
295            else if (name == "min") return m_fnMin;
296            else if (name == "max") return m_fnMax;
297            else if (name == "array_equal") return m_fnArrayEqual;
298            else if (name == "search") return m_fnSearch;
299            else if (name == "sort") return m_fnSort;
300          return NULL;          return NULL;
301      }      }
302    
303        bool ScriptVM::isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) {
304            ParserContext* parserCtx = dynamic_cast<ParserContext*>(ctx);
305            if (!parserCtx) return false;
306    
307            if (fn == m_fnMessage && parserCtx->userPreprocessorConditions.count("NKSP_NO_MESSAGE"))
308                return true;
309    
310            return false;
311        }
312    
313      std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {      std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {
314          return std::map<String,VMIntRelPtr*>();          return std::map<String,VMIntRelPtr*>();
315      }      }
# Line 237  namespace LinuxSampler { Line 329  namespace LinuxSampler {
329      }      }
330    
331      std::map<String,int> ScriptVM::builtInConstIntVariables() {      std::map<String,int> ScriptVM::builtInConstIntVariables() {
332          return std::map<String,int>();          std::map<String,int> m;
333    
334            m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;
335            m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;
336            m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE;
337            m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER;
338    
339            return m;
340      }      }
341    
342      VMEventHandler* ScriptVM::currentVMEventHandler() {      VMEventHandler* ScriptVM::currentVMEventHandler() {
# Line 253  namespace LinuxSampler { Line 352  namespace LinuxSampler {
352          return m_parserContext->execContext;          return m_parserContext->execContext;
353      }      }
354    
355        void ScriptVM::setAutoSuspendEnabled(bool b) {
356            m_autoSuspend = b;
357        }
358    
359        bool ScriptVM::isAutoSuspendEnabled() const {
360            return m_autoSuspend;
361        }
362    
363        void ScriptVM::setExitResultEnabled(bool b) {
364            m_acceptExitRes = b;
365        }
366    
367        bool ScriptVM::isExitResultEnabled() const {
368            return m_acceptExitRes;
369        }
370    
371      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
372          m_parserContext = dynamic_cast<ParserContext*>(parserContext);          m_parserContext = dynamic_cast<ParserContext*>(parserContext);
373          if (!m_parserContext) {          if (!m_parserContext) {
# Line 275  namespace LinuxSampler { Line 390  namespace LinuxSampler {
390          m_parserContext->execContext = ctx;          m_parserContext->execContext = ctx;
391    
392          ctx->status = VM_EXEC_RUNNING;          ctx->status = VM_EXEC_RUNNING;
393          StmtFlags_t flags = STMT_SUCCESS;          ctx->instructionsCount = 0;
394            ctx->clearExitRes();
395            StmtFlags_t& flags = ctx->flags;
396            int instructionsCounter = 0;
397            int synced = m_autoSuspend ? 0 : 1;
398    
399          int& frameIdx = ctx->stackFrame;          int& frameIdx = ctx->stackFrame;
400          if (frameIdx < 0) { // start condition ...          if (frameIdx < 0) { // start condition ...
# Line 352  namespace LinuxSampler { Line 471  namespace LinuxSampler {
471                          ctx->pushStack(                          ctx->pushStack(
472                              whileStmt->statements()                              whileStmt->statements()
473                          );                          );
474                            if (flags == STMT_SUCCESS && !synced &&
475                                instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT)
476                            {
477                                flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
478                                ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
479                            }
480                      } else ctx->popStack();                      } else ctx->popStack();
481                        break;
482                    }
483    
484                    case STMT_SYNC: {
485                        #if DEBUG_SCRIPTVM_CORE
486                        _printIndents(frameIdx);
487                        printf("-> STMT_SYNC\n");
488                        #endif
489                        SyncBlock* syncStmt = (SyncBlock*) frame.statement;
490                        if (!frame.subindex++ && syncStmt->statements()) {
491                            ++synced;
492                            ctx->pushStack(
493                                syncStmt->statements()
494                            );
495                        } else {
496                            ctx->popStack();
497                            --synced;
498                        }
499                        break;
500                  }                  }
501              }              }
502    
503                if (flags == STMT_SUCCESS && !synced &&
504                    instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD)
505                {
506                    flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
507                    ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
508                }
509    
510                ++instructionsCounter;
511          }          }
512    
513          if (flags & STMT_SUSPEND_SIGNALLED) {          if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) {
514              ctx->status = VM_EXEC_SUSPENDED;              ctx->status = VM_EXEC_SUSPENDED;
515                ctx->flags  = STMT_SUCCESS;
516          } else {          } else {
517              ctx->status = VM_EXEC_NOT_RUNNING;              ctx->status = VM_EXEC_NOT_RUNNING;
518              if (flags & STMT_ERROR_OCCURRED)              if (flags & STMT_ERROR_OCCURRED)
# Line 366  namespace LinuxSampler { Line 520  namespace LinuxSampler {
520              ctx->reset();              ctx->reset();
521          }          }
522    
523            ctx->instructionsCount = instructionsCounter;
524    
525          m_eventHandler = NULL;          m_eventHandler = NULL;
526          m_parserContext->execContext = NULL;          m_parserContext->execContext = NULL;
527          m_parserContext = NULL;          m_parserContext = NULL;

Legend:
Removed from v.2945  
changed lines
  Added in v.3551

  ViewVC Help
Powered by ViewVC