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 |
} |
} |
107 |
|
|
108 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
109 |
|
delete m_fnMessage; |
110 |
|
delete m_fnExit; |
111 |
|
delete m_fnWait; |
112 |
|
delete m_fnAbs; |
113 |
|
delete m_fnRandom; |
114 |
|
delete m_fnNumElements; |
115 |
|
delete m_fnInc; |
116 |
|
delete m_fnDec; |
117 |
|
delete m_varRealTimer; |
118 |
|
delete m_varPerfTimer; |
119 |
} |
} |
120 |
|
|
121 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(const String& s) { |
126 |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
127 |
ParserContext* context = new ParserContext(this); |
ParserContext* context = new ParserContext(this); |
128 |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
129 |
|
|
130 |
|
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
131 |
|
context->registerBuiltInIntVariables( builtInIntVariables() ); |
132 |
|
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
133 |
|
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
134 |
|
|
135 |
context->createScanner(is); |
context->createScanner(is); |
136 |
|
|
137 |
InstrScript_parse(context); |
InstrScript_parse(context); |
138 |
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)))); |
139 |
std::cout << "Allocating " << context->globalStrVarCount << " of global VM string variables.\n"; |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
140 |
if (!context->globalIntMemory) |
if (!context->globalIntMemory) |
141 |
context->globalIntMemory = new ArrayList<int>(); |
context->globalIntMemory = new ArrayList<int>(); |
142 |
if (!context->globalStrMemory) |
if (!context->globalStrMemory) |
177 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
178 |
} |
} |
179 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
180 |
std::cout << "Created VM exec context with " |
dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", |
181 |
<< parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame) |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
|
<< " bytes VM stack size.\n"; |
|
182 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
183 |
const int polySize = parserCtx->polyphonicIntVarCount; |
const int polySize = parserCtx->polyphonicIntVarCount; |
184 |
execCtx->polyphonicIntMemory.resize(polySize); |
execCtx->polyphonicIntMemory.resize(polySize); |
185 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
186 |
|
|
187 |
std::cout << "Allocated " << polySize * sizeof(int) |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
|
<< " bytes polyphonic memory.\n"; |
|
188 |
return execCtx; |
return execCtx; |
189 |
} |
} |
190 |
|
|
191 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
192 |
|
std::istringstream iss(s); |
193 |
|
return syntaxHighlighting(&iss); |
194 |
|
} |
195 |
|
|
196 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
197 |
|
NkspScanner scanner(is); |
198 |
|
std::vector<SourceToken> tokens = scanner.tokens(); |
199 |
|
std::vector<VMSourceToken> result; |
200 |
|
result.resize(tokens.size()); |
201 |
|
for (int i = 0; i < tokens.size(); ++i) { |
202 |
|
SourceToken* st = new SourceToken; |
203 |
|
*st = tokens[i]; |
204 |
|
result[i] = VMSourceToken(st); |
205 |
|
} |
206 |
|
return result; |
207 |
|
} |
208 |
|
|
209 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
210 |
if (name == "message") return &fnMessage; |
if (name == "message") return m_fnMessage; |
211 |
else if (name == "exit") return &fnExit; |
else if (name == "exit") return m_fnExit; |
212 |
else if (name == "wait") return &fnWait; |
else if (name == "wait") return m_fnWait; |
213 |
|
else if (name == "abs") return m_fnAbs; |
214 |
|
else if (name == "random") return m_fnRandom; |
215 |
|
else if (name == "num_elements") return m_fnNumElements; |
216 |
|
else if (name == "inc") return m_fnInc; |
217 |
|
else if (name == "dec") return m_fnDec; |
218 |
return NULL; |
return NULL; |
219 |
} |
} |
220 |
|
|
221 |
|
std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() { |
222 |
|
return std::map<String,VMIntRelPtr*>(); |
223 |
|
} |
224 |
|
|
225 |
|
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
226 |
|
return std::map<String,VMInt8Array*>(); |
227 |
|
} |
228 |
|
|
229 |
|
std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() { |
230 |
|
std::map<String,VMDynVar*> m; |
231 |
|
|
232 |
|
m["$NKSP_PERF_TIMER"] = m_varPerfTimer; |
233 |
|
m["$NKSP_REAL_TIMER"] = m_varRealTimer; |
234 |
|
m["$KSP_TIMER"] = m_varRealTimer; |
235 |
|
|
236 |
|
return m; |
237 |
|
} |
238 |
|
|
239 |
|
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
240 |
|
std::map<String,int> m; |
241 |
|
|
242 |
|
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
243 |
|
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
244 |
|
m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE; |
245 |
|
m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER; |
246 |
|
|
247 |
|
return m; |
248 |
|
} |
249 |
|
|
250 |
|
VMEventHandler* ScriptVM::currentVMEventHandler() { |
251 |
|
return m_eventHandler; |
252 |
|
} |
253 |
|
|
254 |
VMParserContext* ScriptVM::currentVMParserContext() { |
VMParserContext* ScriptVM::currentVMParserContext() { |
255 |
return m_parserContext; |
return m_parserContext; |
256 |
} |
} |
267 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
268 |
} |
} |
269 |
|
|
270 |
|
// a ParserContext object is always tied to exactly one ScriptVM object |
271 |
|
assert(m_parserContext->functionProvider == this); |
272 |
|
|
273 |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
274 |
if (!ctx) { |
if (!ctx) { |
275 |
std::cerr << "Invalid VM exec context.\n"; |
std::cerr << "Invalid VM exec context.\n"; |
277 |
} |
} |
278 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
279 |
if (!h) return VM_EXEC_NOT_RUNNING; |
if (!h) return VM_EXEC_NOT_RUNNING; |
280 |
|
m_eventHandler = handler; |
281 |
|
|
282 |
m_parserContext->execContext = ctx; |
m_parserContext->execContext = ctx; |
283 |
|
|
373 |
ctx->reset(); |
ctx->reset(); |
374 |
} |
} |
375 |
|
|
376 |
|
m_eventHandler = NULL; |
377 |
m_parserContext->execContext = NULL; |
m_parserContext->execContext = NULL; |
378 |
m_parserContext = NULL; |
m_parserContext = NULL; |
379 |
return ctx->status; |
return ctx->status; |