1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
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 |
|
|
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 |
|
m_fnMin = new CoreVMFunction_min; |
109 |
|
m_fnMax = new CoreVMFunction_max; |
110 |
} |
} |
111 |
|
|
112 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
113 |
|
delete m_fnMessage; |
114 |
|
delete m_fnExit; |
115 |
|
delete m_fnWait; |
116 |
|
delete m_fnAbs; |
117 |
|
delete m_fnRandom; |
118 |
|
delete m_fnNumElements; |
119 |
|
delete m_fnInc; |
120 |
|
delete m_fnDec; |
121 |
|
delete m_fnShLeft; |
122 |
|
delete m_fnShRight; |
123 |
|
delete m_fnMin; |
124 |
|
delete m_fnMax; |
125 |
|
delete m_varRealTimer; |
126 |
|
delete m_varPerfTimer; |
127 |
} |
} |
128 |
|
|
129 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(const String& s) { |
138 |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
139 |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
140 |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
141 |
|
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
142 |
|
|
143 |
context->createScanner(is); |
context->createScanner(is); |
144 |
|
|
145 |
InstrScript_parse(context); |
InstrScript_parse(context); |
146 |
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)))); |
147 |
std::cout << "Allocating " << context->globalStrVarCount << " of global VM string variables.\n"; |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
148 |
if (!context->globalIntMemory) |
if (!context->globalIntMemory) |
149 |
context->globalIntMemory = new ArrayList<int>(); |
context->globalIntMemory = new ArrayList<int>(); |
150 |
if (!context->globalStrMemory) |
if (!context->globalStrMemory) |
185 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
186 |
} |
} |
187 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
188 |
std::cout << "Created VM exec context with " |
dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", |
189 |
<< parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame) |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
|
<< " bytes VM stack size.\n"; |
|
190 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
191 |
const int polySize = parserCtx->polyphonicIntVarCount; |
const int polySize = parserCtx->polyphonicIntVarCount; |
192 |
execCtx->polyphonicIntMemory.resize(polySize); |
execCtx->polyphonicIntMemory.resize(polySize); |
193 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
194 |
|
|
195 |
std::cout << "Allocated " << polySize * sizeof(int) |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
|
<< " bytes polyphonic memory.\n"; |
|
196 |
return execCtx; |
return execCtx; |
197 |
} |
} |
198 |
|
|
199 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
200 |
|
std::istringstream iss(s); |
201 |
|
return syntaxHighlighting(&iss); |
202 |
|
} |
203 |
|
|
204 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
205 |
|
NkspScanner scanner(is); |
206 |
|
std::vector<SourceToken> tokens = scanner.tokens(); |
207 |
|
std::vector<VMSourceToken> result; |
208 |
|
result.resize(tokens.size()); |
209 |
|
for (int i = 0; i < tokens.size(); ++i) { |
210 |
|
SourceToken* st = new SourceToken; |
211 |
|
*st = tokens[i]; |
212 |
|
result[i] = VMSourceToken(st); |
213 |
|
} |
214 |
|
return result; |
215 |
|
} |
216 |
|
|
217 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
218 |
if (name == "message") return &fnMessage; |
if (name == "message") return m_fnMessage; |
219 |
else if (name == "exit") return &fnExit; |
else if (name == "exit") return m_fnExit; |
220 |
else if (name == "wait") return &fnWait; |
else if (name == "wait") return m_fnWait; |
221 |
|
else if (name == "abs") return m_fnAbs; |
222 |
|
else if (name == "random") return m_fnRandom; |
223 |
|
else if (name == "num_elements") return m_fnNumElements; |
224 |
|
else if (name == "inc") return m_fnInc; |
225 |
|
else if (name == "dec") return m_fnDec; |
226 |
|
else if (name == "sh_left") return m_fnShLeft; |
227 |
|
else if (name == "sh_right") return m_fnShRight; |
228 |
|
else if (name == "min") return m_fnMin; |
229 |
|
else if (name == "max") return m_fnMax; |
230 |
return NULL; |
return NULL; |
231 |
} |
} |
232 |
|
|
238 |
return std::map<String,VMInt8Array*>(); |
return std::map<String,VMInt8Array*>(); |
239 |
} |
} |
240 |
|
|
241 |
|
std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() { |
242 |
|
std::map<String,VMDynVar*> m; |
243 |
|
|
244 |
|
m["$NKSP_PERF_TIMER"] = m_varPerfTimer; |
245 |
|
m["$NKSP_REAL_TIMER"] = m_varRealTimer; |
246 |
|
m["$KSP_TIMER"] = m_varRealTimer; |
247 |
|
|
248 |
|
return m; |
249 |
|
} |
250 |
|
|
251 |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
252 |
return std::map<String,int>(); |
std::map<String,int> m; |
253 |
|
|
254 |
|
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
255 |
|
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
256 |
|
m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE; |
257 |
|
m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER; |
258 |
|
|
259 |
|
return m; |
260 |
|
} |
261 |
|
|
262 |
|
VMEventHandler* ScriptVM::currentVMEventHandler() { |
263 |
|
return m_eventHandler; |
264 |
} |
} |
265 |
|
|
266 |
VMParserContext* ScriptVM::currentVMParserContext() { |
VMParserContext* ScriptVM::currentVMParserContext() { |
279 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
280 |
} |
} |
281 |
|
|
282 |
|
// a ParserContext object is always tied to exactly one ScriptVM object |
283 |
|
assert(m_parserContext->functionProvider == this); |
284 |
|
|
285 |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
286 |
if (!ctx) { |
if (!ctx) { |
287 |
std::cerr << "Invalid VM exec context.\n"; |
std::cerr << "Invalid VM exec context.\n"; |
289 |
} |
} |
290 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
291 |
if (!h) return VM_EXEC_NOT_RUNNING; |
if (!h) return VM_EXEC_NOT_RUNNING; |
292 |
|
m_eventHandler = handler; |
293 |
|
|
294 |
m_parserContext->execContext = ctx; |
m_parserContext->execContext = ctx; |
295 |
|
|
385 |
ctx->reset(); |
ctx->reset(); |
386 |
} |
} |
387 |
|
|
388 |
|
m_eventHandler = NULL; |
389 |
m_parserContext->execContext = NULL; |
m_parserContext->execContext = NULL; |
390 |
m_parserContext = NULL; |
m_parserContext = NULL; |
391 |
return ctx->status; |
return ctx->status; |