/[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 2588 by schoenebeck, Sun Jun 1 14:44:38 2014 UTC revision 2965 by schoenebeck, Mon Jul 18 09:42:28 2016 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014 - 2016 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 10  Line 10 
10  #include "ScriptVM.h"  #include "ScriptVM.h"
11    
12  #include <string.h>  #include <string.h>
13    #include <assert.h>
14  #include "../common/global_private.h"  #include "../common/global_private.h"
15  #include "tree.h"  #include "tree.h"
16    #include "CoreVMFunctions.h"
17    #include "CoreVMDynVars.h"
18    #include "editor/NkspScanner.h"
19    
20  #define DEBUG_SCRIPTVM_CORE 0  #define DEBUG_SCRIPTVM_CORE 0
21    
# Line 88  namespace LinuxSampler { Line 92  namespace LinuxSampler {
92          return max;          return max;
93      }      }
94    
95      ScriptVM::ScriptVM() : m_parserContext(NULL), fnWait(this) {      ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) {
96            m_fnMessage = new CoreVMFunction_message;
97            m_fnExit = new CoreVMFunction_exit;
98            m_fnWait = new CoreVMFunction_wait(this);
99            m_fnAbs = new CoreVMFunction_abs;
100            m_fnRandom = new CoreVMFunction_random;
101            m_fnNumElements = new CoreVMFunction_num_elements;
102            m_fnInc = new CoreVMFunction_inc;
103            m_fnDec = new CoreVMFunction_dec;
104            m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;
105            m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;
106            m_fnShLeft = new CoreVMFunction_sh_left;
107            m_fnShRight = new CoreVMFunction_sh_right;
108      }      }
109    
110      ScriptVM::~ScriptVM() {      ScriptVM::~ScriptVM() {
111            delete m_fnMessage;
112            delete m_fnExit;
113            delete m_fnWait;
114            delete m_fnAbs;
115            delete m_fnRandom;
116            delete m_fnNumElements;
117            delete m_fnInc;
118            delete m_fnDec;
119            delete m_fnShLeft;
120            delete m_fnShRight;
121            delete m_varRealTimer;
122            delete m_varPerfTimer;
123      }      }
124    
125      VMParserContext* ScriptVM::loadScript(const String& s) {      VMParserContext* ScriptVM::loadScript(const String& s) {
# Line 102  namespace LinuxSampler { Line 130  namespace LinuxSampler {
130      VMParserContext* ScriptVM::loadScript(std::istream* is) {      VMParserContext* ScriptVM::loadScript(std::istream* is) {
131          ParserContext* context = new ParserContext(this);          ParserContext* context = new ParserContext(this);
132          //printf("parserCtx=0x%lx\n", (uint64_t)context);          //printf("parserCtx=0x%lx\n", (uint64_t)context);
133            
134            context->registerBuiltInConstIntVariables( builtInConstIntVariables() );
135            context->registerBuiltInIntVariables( builtInIntVariables() );
136            context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() );
137            context->registerBuiltInDynVariables( builtInDynamicVariables() );
138    
139          context->createScanner(is);          context->createScanner(is);
140    
141          InstrScript_parse(context);          InstrScript_parse(context);
142          std::cout << "Allocating " << context->globalIntVarCount * sizeof(int) << " bytes of global int VM memory.\n";          dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int))));
143          std::cout << "Allocating " << context->globalStrVarCount << " of global VM string variables.\n";          dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount));
144          if (!context->globalIntMemory)          if (!context->globalIntMemory)
145              context->globalIntMemory = new ArrayList<int>();              context->globalIntMemory = new ArrayList<int>();
146          if (!context->globalStrMemory)          if (!context->globalStrMemory)
# Line 148  namespace LinuxSampler { Line 181  namespace LinuxSampler {
181                  _requiredMaxStackSizeFor(&*parserCtx->handlers);                  _requiredMaxStackSizeFor(&*parserCtx->handlers);
182          }          }
183          execCtx->stack.resize(parserCtx->requiredMaxStackSize);          execCtx->stack.resize(parserCtx->requiredMaxStackSize);
184          std::cout << "Created VM exec context with "          dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n",
185                    << parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)                  long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))));
                   << " bytes VM stack size.\n";  
186          //printf("execCtx=0x%lx\n", (uint64_t)execCtx);          //printf("execCtx=0x%lx\n", (uint64_t)execCtx);
187          const int polySize = parserCtx->polyphonicIntVarCount;          const int polySize = parserCtx->polyphonicIntVarCount;
188          execCtx->polyphonicIntMemory.resize(polySize);          execCtx->polyphonicIntMemory.resize(polySize);
189          memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int));          memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int));
190    
191          std::cout << "Allocated " << polySize * sizeof(int)          dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int))));
                   << " bytes polyphonic memory.\n";  
192          return execCtx;          return execCtx;
193      }      }
194    
195        std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) {
196            std::istringstream iss(s);
197            return syntaxHighlighting(&iss);
198        }
199    
200        std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {
201            NkspScanner scanner(is);
202            std::vector<SourceToken> tokens = scanner.tokens();
203            std::vector<VMSourceToken> result;
204            result.resize(tokens.size());
205            for (int i = 0; i < tokens.size(); ++i) {
206                SourceToken* st = new SourceToken;
207                *st = tokens[i];
208                result[i] = VMSourceToken(st);
209            }
210            return result;
211        }
212    
213      VMFunction* ScriptVM::functionByName(const String& name) {      VMFunction* ScriptVM::functionByName(const String& name) {
214          if (name == "message") return &fnMessage;          if (name == "message") return m_fnMessage;
215          else if (name == "exit") return &fnExit;          else if (name == "exit") return m_fnExit;
216          else if (name == "wait") return &fnWait;          else if (name == "wait") return m_fnWait;
217            else if (name == "abs") return m_fnAbs;
218            else if (name == "random") return m_fnRandom;
219            else if (name == "num_elements") return m_fnNumElements;
220            else if (name == "inc") return m_fnInc;
221            else if (name == "dec") return m_fnDec;
222            else if (name == "sh_left") return m_fnShLeft;
223            else if (name == "sh_right") return m_fnShRight;
224          return NULL;          return NULL;
225      }      }
226    
227        std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {
228            return std::map<String,VMIntRelPtr*>();
229        }
230    
231        std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() {
232            return std::map<String,VMInt8Array*>();
233        }
234    
235        std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() {
236            std::map<String,VMDynVar*> m;
237    
238            m["$NKSP_PERF_TIMER"] = m_varPerfTimer;
239            m["$NKSP_REAL_TIMER"] = m_varRealTimer;
240            m["$KSP_TIMER"] = m_varRealTimer;
241    
242            return m;
243        }
244    
245        std::map<String,int> ScriptVM::builtInConstIntVariables() {
246            std::map<String,int> m;
247    
248            m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;
249            m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;
250            m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE;
251            m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER;
252    
253            return m;
254        }
255    
256        VMEventHandler* ScriptVM::currentVMEventHandler() {
257            return m_eventHandler;
258        }
259    
260      VMParserContext* ScriptVM::currentVMParserContext() {      VMParserContext* ScriptVM::currentVMParserContext() {
261          return m_parserContext;          return m_parserContext;
262      }      }
# Line 184  namespace LinuxSampler { Line 273  namespace LinuxSampler {
273              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);
274          }          }
275    
276            // a ParserContext object is always tied to exactly one ScriptVM object
277            assert(m_parserContext->functionProvider == this);
278    
279          ExecContext* ctx = dynamic_cast<ExecContext*>(execContex);          ExecContext* ctx = dynamic_cast<ExecContext*>(execContex);
280          if (!ctx) {          if (!ctx) {
281              std::cerr << "Invalid VM exec context.\n";              std::cerr << "Invalid VM exec context.\n";
# Line 191  namespace LinuxSampler { Line 283  namespace LinuxSampler {
283          }          }
284          EventHandler* h = dynamic_cast<EventHandler*>(handler);          EventHandler* h = dynamic_cast<EventHandler*>(handler);
285          if (!h) return VM_EXEC_NOT_RUNNING;          if (!h) return VM_EXEC_NOT_RUNNING;
286            m_eventHandler = handler;
287    
288          m_parserContext->execContext = ctx;          m_parserContext->execContext = ctx;
289    
# Line 286  namespace LinuxSampler { Line 379  namespace LinuxSampler {
379              ctx->reset();              ctx->reset();
380          }          }
381    
382            m_eventHandler = NULL;
383          m_parserContext->execContext = NULL;          m_parserContext->execContext = NULL;
384          m_parserContext = NULL;          m_parserContext = NULL;
385          return ctx->status;          return ctx->status;

Legend:
Removed from v.2588  
changed lines
  Added in v.2965

  ViewVC Help
Powered by ViewVC