/[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 3332 by schoenebeck, Mon Jul 24 18:51:21 2017 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 - 2016 Christian Schoenebeck   * Copyright (c) 2014 - 2017 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() : m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true) {
147          m_fnMessage = new CoreVMFunction_message;          m_fnMessage = new CoreVMFunction_message;
148          m_fnExit = new CoreVMFunction_exit;          m_fnExit = new CoreVMFunction_exit;
149          m_fnWait = new CoreVMFunction_wait(this);          m_fnWait = new CoreVMFunction_wait(this);
# Line 101  namespace LinuxSampler { Line 152  namespace LinuxSampler {
152          m_fnNumElements = new CoreVMFunction_num_elements;          m_fnNumElements = new CoreVMFunction_num_elements;
153          m_fnInc = new CoreVMFunction_inc;          m_fnInc = new CoreVMFunction_inc;
154          m_fnDec = new CoreVMFunction_dec;          m_fnDec = new CoreVMFunction_dec;
155            m_fnInRange = new CoreVMFunction_in_range;
156          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;
157          m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;          m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;
158            m_fnShLeft = new CoreVMFunction_sh_left;
159            m_fnShRight = new CoreVMFunction_sh_right;
160            m_fnMin = new CoreVMFunction_min;
161            m_fnMax = new CoreVMFunction_max;
162            m_fnArrayEqual = new CoreVMFunction_array_equal;
163            m_fnSearch = new CoreVMFunction_search;
164            m_fnSort = new CoreVMFunction_sort;
165      }      }
166    
167      ScriptVM::~ScriptVM() {      ScriptVM::~ScriptVM() {
# Line 114  namespace LinuxSampler { Line 173  namespace LinuxSampler {
173          delete m_fnNumElements;          delete m_fnNumElements;
174          delete m_fnInc;          delete m_fnInc;
175          delete m_fnDec;          delete m_fnDec;
176            delete m_fnInRange;
177            delete m_fnShLeft;
178            delete m_fnShRight;
179            delete m_fnMin;
180            delete m_fnMax;
181            delete m_fnArrayEqual;
182            delete m_fnSearch;
183            delete m_fnSort;
184          delete m_varRealTimer;          delete m_varRealTimer;
185          delete m_varPerfTimer;          delete m_varPerfTimer;
186      }      }
# Line 194  namespace LinuxSampler { Line 261  namespace LinuxSampler {
261      }      }
262    
263      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {
264          NkspScanner scanner(is);          try {
265          std::vector<SourceToken> tokens = scanner.tokens();              NkspScanner scanner(is);
266          std::vector<VMSourceToken> result;              std::vector<SourceToken> tokens = scanner.tokens();
267          result.resize(tokens.size());              std::vector<VMSourceToken> result;
268          for (int i = 0; i < tokens.size(); ++i) {              result.resize(tokens.size());
269              SourceToken* st = new SourceToken;              for (int i = 0; i < tokens.size(); ++i) {
270              *st = tokens[i];                  SourceToken* st = new SourceToken;
271              result[i] = VMSourceToken(st);                  *st = tokens[i];
272                    result[i] = VMSourceToken(st);
273                }
274                return result;
275            } catch (...) {
276                return std::vector<VMSourceToken>();
277          }          }
         return result;  
278      }      }
279    
280      VMFunction* ScriptVM::functionByName(const String& name) {      VMFunction* ScriptVM::functionByName(const String& name) {
# Line 215  namespace LinuxSampler { Line 286  namespace LinuxSampler {
286          else if (name == "num_elements") return m_fnNumElements;          else if (name == "num_elements") return m_fnNumElements;
287          else if (name == "inc") return m_fnInc;          else if (name == "inc") return m_fnInc;
288          else if (name == "dec") return m_fnDec;          else if (name == "dec") return m_fnDec;
289            else if (name == "in_range") return m_fnInRange;
290            else if (name == "sh_left") return m_fnShLeft;
291            else if (name == "sh_right") return m_fnShRight;
292            else if (name == "min") return m_fnMin;
293            else if (name == "max") return m_fnMax;
294            else if (name == "array_equal") return m_fnArrayEqual;
295            else if (name == "search") return m_fnSearch;
296            else if (name == "sort") return m_fnSort;
297          return NULL;          return NULL;
298      }      }
299    
300        bool ScriptVM::isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) {
301            ParserContext* parserCtx = dynamic_cast<ParserContext*>(ctx);
302            if (!parserCtx) return false;
303    
304            if (fn == m_fnMessage && parserCtx->userPreprocessorConditions.count("NKSP_NO_MESSAGE"))
305                return true;
306    
307            return false;
308        }
309    
310      std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {      std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {
311          return std::map<String,VMIntRelPtr*>();          return std::map<String,VMIntRelPtr*>();
312      }      }
# Line 237  namespace LinuxSampler { Line 326  namespace LinuxSampler {
326      }      }
327    
328      std::map<String,int> ScriptVM::builtInConstIntVariables() {      std::map<String,int> ScriptVM::builtInConstIntVariables() {
329          return std::map<String,int>();          std::map<String,int> m;
330    
331            m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;
332            m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;
333            m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE;
334            m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER;
335    
336            return m;
337      }      }
338    
339      VMEventHandler* ScriptVM::currentVMEventHandler() {      VMEventHandler* ScriptVM::currentVMEventHandler() {
# Line 253  namespace LinuxSampler { Line 349  namespace LinuxSampler {
349          return m_parserContext->execContext;          return m_parserContext->execContext;
350      }      }
351    
352        void ScriptVM::setAutoSuspendEnabled(bool b) {
353            m_autoSuspend = b;
354        }
355    
356        bool ScriptVM::isAutoSuspendEnabled() const {
357            return m_autoSuspend;
358        }
359    
360      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
361          m_parserContext = dynamic_cast<ParserContext*>(parserContext);          m_parserContext = dynamic_cast<ParserContext*>(parserContext);
362          if (!m_parserContext) {          if (!m_parserContext) {
# Line 275  namespace LinuxSampler { Line 379  namespace LinuxSampler {
379          m_parserContext->execContext = ctx;          m_parserContext->execContext = ctx;
380    
381          ctx->status = VM_EXEC_RUNNING;          ctx->status = VM_EXEC_RUNNING;
382          StmtFlags_t flags = STMT_SUCCESS;          ctx->instructionsCount = 0;
383            StmtFlags_t& flags = ctx->flags;
384            int instructionsCounter = 0;
385            int synced = m_autoSuspend ? 0 : 1;
386    
387          int& frameIdx = ctx->stackFrame;          int& frameIdx = ctx->stackFrame;
388          if (frameIdx < 0) { // start condition ...          if (frameIdx < 0) { // start condition ...
# Line 352  namespace LinuxSampler { Line 459  namespace LinuxSampler {
459                          ctx->pushStack(                          ctx->pushStack(
460                              whileStmt->statements()                              whileStmt->statements()
461                          );                          );
462                            if (flags == STMT_SUCCESS && !synced &&
463                                instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT)
464                            {
465                                flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
466                                ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
467                            }
468                      } else ctx->popStack();                      } else ctx->popStack();
469                        break;
470                    }
471    
472                    case STMT_SYNC: {
473                        #if DEBUG_SCRIPTVM_CORE
474                        _printIndents(frameIdx);
475                        printf("-> STMT_SYNC\n");
476                        #endif
477                        SyncBlock* syncStmt = (SyncBlock*) frame.statement;
478                        if (!frame.subindex++ && syncStmt->statements()) {
479                            ++synced;
480                            ctx->pushStack(
481                                syncStmt->statements()
482                            );
483                        } else {
484                            ctx->popStack();
485                            --synced;
486                        }
487                        break;
488                  }                  }
489              }              }
490    
491                if (flags == STMT_SUCCESS && !synced &&
492                    instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD)
493                {
494                    flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
495                    ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
496                }
497    
498                ++instructionsCounter;
499          }          }
500    
501          if (flags & STMT_SUSPEND_SIGNALLED) {          if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) {
502              ctx->status = VM_EXEC_SUSPENDED;              ctx->status = VM_EXEC_SUSPENDED;
503                ctx->flags  = STMT_SUCCESS;
504          } else {          } else {
505              ctx->status = VM_EXEC_NOT_RUNNING;              ctx->status = VM_EXEC_NOT_RUNNING;
506              if (flags & STMT_ERROR_OCCURRED)              if (flags & STMT_ERROR_OCCURRED)
# Line 366  namespace LinuxSampler { Line 508  namespace LinuxSampler {
508              ctx->reset();              ctx->reset();
509          }          }
510    
511            ctx->instructionsCount = instructionsCounter;
512    
513          m_eventHandler = NULL;          m_eventHandler = NULL;
514          m_parserContext->execContext = NULL;          m_parserContext->execContext = NULL;
515          m_parserContext = NULL;          m_parserContext = NULL;

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

  ViewVC Help
Powered by ViewVC