/[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 3311 by schoenebeck, Sat Jul 15 16:24:59 2017 UTC revision 3581 by schoenebeck, Fri Aug 30 11:40:25 2019 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 - 2017 Christian Schoenebeck   * Copyright (c) 2014 - 2019 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 67  namespace LinuxSampler { Line 67  namespace LinuxSampler {
67      }      }
68      #endif      #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 84  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 98  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 129  namespace LinuxSampler { Line 129  namespace LinuxSampler {
129                  else                  else
130                      return 1;                      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), m_autoSuspend(true) {      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;
# Line 162  namespace LinuxSampler { Line 168  namespace LinuxSampler {
168          m_fnArrayEqual = new CoreVMFunction_array_equal;          m_fnArrayEqual = new CoreVMFunction_array_equal;
169          m_fnSearch = new CoreVMFunction_search;          m_fnSearch = new CoreVMFunction_search;
170          m_fnSort = new CoreVMFunction_sort;          m_fnSort = new CoreVMFunction_sort;
171            m_fnIntToReal = new CoreVMFunction_int_to_real;
172            m_fnRealToInt = new CoreVMFunction_real_to_int;
173      }      }
174    
175      ScriptVM::~ScriptVM() {      ScriptVM::~ScriptVM() {
# Line 181  namespace LinuxSampler { Line 189  namespace LinuxSampler {
189          delete m_fnArrayEqual;          delete m_fnArrayEqual;
190          delete m_fnSearch;          delete m_fnSearch;
191          delete m_fnSort;          delete m_fnSort;
192            delete m_fnIntToReal;
193            delete m_fnRealToInt;
194          delete m_varRealTimer;          delete m_varRealTimer;
195          delete m_varPerfTimer;          delete m_varPerfTimer;
196      }      }
# Line 202  namespace LinuxSampler { Line 212  namespace LinuxSampler {
212          context->createScanner(is);          context->createScanner(is);
213    
214          InstrScript_parse(context);          InstrScript_parse(context);
215          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)));
216          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)));
217            dmsg(2,("Allocating %lld bytes of global unit factor VM memory.\n", context->globalUnitFactorCount * sizeof(vmfloat)));
218            dmsg(2,("Allocating %lld of global VM string variables.\n", context->globalStrVarCount));
219          if (!context->globalIntMemory)          if (!context->globalIntMemory)
220              context->globalIntMemory = new ArrayList<int>();              context->globalIntMemory = new ArrayList<vmint>();
221            if (!context->globalRealMemory)
222                context->globalRealMemory = new ArrayList<vmfloat>();
223            if (!context->globalUnitFactorMemory)
224                context->globalUnitFactorMemory = new ArrayList<vmfloat>();
225          if (!context->globalStrMemory)          if (!context->globalStrMemory)
226              context->globalStrMemory = new ArrayList<String>();              context->globalStrMemory = new ArrayList<String>();
227          context->globalIntMemory->resize(context->globalIntVarCount);          context->globalIntMemory->resize(context->globalIntVarCount);
228          memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int));          context->globalRealMemory->resize(context->globalRealVarCount);
229                    context->globalUnitFactorMemory->resize(context->globalUnitFactorCount);
230            memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(vmint));
231            memset(&((*context->globalRealMemory)[0]), 0, context->globalRealVarCount * sizeof(vmfloat));
232            for (vmint i = 0; i < context->globalUnitFactorCount; ++i)
233                (*context->globalUnitFactorMemory)[i] = VM_NO_FACTOR;
234          context->globalStrMemory->resize(context->globalStrVarCount);          context->globalStrMemory->resize(context->globalStrVarCount);
235    
236          context->destroyScanner();          context->destroyScanner();
# Line 229  namespace LinuxSampler { Line 249  namespace LinuxSampler {
249              return;              return;
250          }          }
251          if (!ctx->globalIntMemory) {          if (!ctx->globalIntMemory) {
252              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";
253                return;
254            }
255            if (!ctx->globalRealMemory) {
256                std::cerr << "Internal error: no global real number memory assigend to script VM.\n";
257              return;              return;
258          }          }
259          ctx->handlers->dump();          ctx->handlers->dump();
# Line 244  namespace LinuxSampler { Line 268  namespace LinuxSampler {
268                  _requiredMaxStackSizeFor(&*parserCtx->handlers);                  _requiredMaxStackSizeFor(&*parserCtx->handlers);
269          }          }
270          execCtx->stack.resize(parserCtx->requiredMaxStackSize);          execCtx->stack.resize(parserCtx->requiredMaxStackSize);
271          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",
272                  long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))));                  parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)));
273          //printf("execCtx=0x%lx\n", (uint64_t)execCtx);          //printf("execCtx=0x%lx\n", (uint64_t)execCtx);
274          const int polySize = parserCtx->polyphonicIntVarCount;          const vmint polyIntSize = parserCtx->polyphonicIntVarCount;
275          execCtx->polyphonicIntMemory.resize(polySize);          execCtx->polyphonicIntMemory.resize(polyIntSize);
276          memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int));          memset(&execCtx->polyphonicIntMemory[0], 0, polyIntSize * sizeof(vmint));
277    
278          dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int))));          const vmint polyRealSize = parserCtx->polyphonicRealVarCount;
279            execCtx->polyphonicRealMemory.resize(polyRealSize);
280            memset(&execCtx->polyphonicRealMemory[0], 0, polyRealSize * sizeof(vmfloat));
281    
282            const vmint polyFactorSize = parserCtx->polyphonicUnitFactorCount;
283            execCtx->polyphonicUnitFactorMemory.resize(polyFactorSize);
284            for (vmint i = 0; i < polyFactorSize; ++i)
285                execCtx->polyphonicUnitFactorMemory[i] = VM_NO_FACTOR;
286    
287            dmsg(2,("Allocated %lld bytes polyphonic int memory.\n", polyIntSize * sizeof(vmint)));
288            dmsg(2,("Allocated %lld bytes polyphonic real memory.\n", polyRealSize * sizeof(vmfloat)));
289            dmsg(2,("Allocated %lld bytes unit factor memory.\n", polyFactorSize * sizeof(vmfloat)));
290          return execCtx;          return execCtx;
291      }      }
292    
# Line 261  namespace LinuxSampler { Line 296  namespace LinuxSampler {
296      }      }
297    
298      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {
299          NkspScanner scanner(is);          try {
300          std::vector<SourceToken> tokens = scanner.tokens();              NkspScanner scanner(is);
301          std::vector<VMSourceToken> result;              std::vector<SourceToken> tokens = scanner.tokens();
302          result.resize(tokens.size());              std::vector<VMSourceToken> result;
303          for (int i = 0; i < tokens.size(); ++i) {              result.resize(tokens.size());
304              SourceToken* st = new SourceToken;              for (vmint i = 0; i < tokens.size(); ++i) {
305              *st = tokens[i];                  SourceToken* st = new SourceToken;
306              result[i] = VMSourceToken(st);                  *st = tokens[i];
307                    result[i] = VMSourceToken(st);
308                }
309                return result;
310            } catch (...) {
311                return std::vector<VMSourceToken>();
312          }          }
         return result;  
313      }      }
314    
315      VMFunction* ScriptVM::functionByName(const String& name) {      VMFunction* ScriptVM::functionByName(const String& name) {
# Line 290  namespace LinuxSampler { Line 329  namespace LinuxSampler {
329          else if (name == "array_equal") return m_fnArrayEqual;          else if (name == "array_equal") return m_fnArrayEqual;
330          else if (name == "search") return m_fnSearch;          else if (name == "search") return m_fnSearch;
331          else if (name == "sort") return m_fnSort;          else if (name == "sort") return m_fnSort;
332            else if (name == "int_to_real") return m_fnIntToReal;
333            else if (name == "real") return m_fnIntToReal;
334            else if (name == "real_to_int") return m_fnRealToInt;
335            else if (name == "int") return m_fnRealToInt;
336          return NULL;          return NULL;
337      }      }
338    
# Line 303  namespace LinuxSampler { Line 346  namespace LinuxSampler {
346          return false;          return false;
347      }      }
348    
349      std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {      std::map<String,VMIntPtr*> ScriptVM::builtInIntVariables() {
350          return std::map<String,VMIntRelPtr*>();          return std::map<String,VMIntPtr*>();
351      }      }
352    
353      std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() {      std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() {
# Line 321  namespace LinuxSampler { Line 364  namespace LinuxSampler {
364          return m;          return m;
365      }      }
366    
367      std::map<String,int> ScriptVM::builtInConstIntVariables() {      std::map<String,vmint> ScriptVM::builtInConstIntVariables() {
368          std::map<String,int> m;          std::map<String,vmint> m;
369    
370          m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;          m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;
371          m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;          m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;
# Line 353  namespace LinuxSampler { Line 396  namespace LinuxSampler {
396          return m_autoSuspend;          return m_autoSuspend;
397      }      }
398    
399        void ScriptVM::setExitResultEnabled(bool b) {
400            m_acceptExitRes = b;
401        }
402    
403        bool ScriptVM::isExitResultEnabled() const {
404            return m_acceptExitRes;
405        }
406    
407      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
408          m_parserContext = dynamic_cast<ParserContext*>(parserContext);          m_parserContext = dynamic_cast<ParserContext*>(parserContext);
409          if (!m_parserContext) {          if (!m_parserContext) {
410              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";
411              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);
412          }          }
413    
# Line 376  namespace LinuxSampler { Line 427  namespace LinuxSampler {
427    
428          ctx->status = VM_EXEC_RUNNING;          ctx->status = VM_EXEC_RUNNING;
429          ctx->instructionsCount = 0;          ctx->instructionsCount = 0;
430            ctx->clearExitRes();
431          StmtFlags_t& flags = ctx->flags;          StmtFlags_t& flags = ctx->flags;
432          int instructionsCounter = 0;          vmint instructionsCounter = 0;
433          int synced = m_autoSuspend ? 0 : 1;          vmint synced = m_autoSuspend ? 0 : 1;
434    
435          int& frameIdx = ctx->stackFrame;          int& frameIdx = ctx->stackFrame;
436          if (frameIdx < 0) { // start condition ...          if (frameIdx < 0) { // start condition ...
# Line 434  namespace LinuxSampler { Line 486  namespace LinuxSampler {
486                      if (frame.subindex < 0) ctx->popStack();                      if (frame.subindex < 0) ctx->popStack();
487                      else {                      else {
488                          BranchStatement* branchStmt = (BranchStatement*) frame.statement;                          BranchStatement* branchStmt = (BranchStatement*) frame.statement;
489                          frame.subindex = branchStmt->evalBranch();                          frame.subindex =
490                                (decltype(frame.subindex))
491                                    branchStmt->evalBranch();
492                          if (frame.subindex >= 0) {                          if (frame.subindex >= 0) {
493                              ctx->pushStack(                              ctx->pushStack(
494                                  branchStmt->branch(frame.subindex)                                  branchStmt->branch(frame.subindex)
# Line 482  namespace LinuxSampler { Line 536  namespace LinuxSampler {
536                      }                      }
537                      break;                      break;
538                  }                  }
539    
540                    case STMT_NOOP:
541                        break; // no operation like the name suggests
542              }              }
543    
544              if (flags == STMT_SUCCESS && !synced &&              if (flags == STMT_SUCCESS && !synced &&

Legend:
Removed from v.3311  
changed lines
  Added in v.3581

  ViewVC Help
Powered by ViewVC