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 |
* |
* |
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()) { |
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; |
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; |
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 |
|
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; |
165 |
m_fnShRight = new CoreVMFunction_sh_right; |
m_fnShRight = new CoreVMFunction_sh_right; |
166 |
m_fnMin = new CoreVMFunction_min; |
m_fnMin = new CoreVMFunction_min; |
167 |
m_fnMax = new CoreVMFunction_max; |
m_fnMax = new CoreVMFunction_max; |
168 |
|
m_fnArrayEqual = new CoreVMFunction_array_equal; |
169 |
|
m_fnSearch = new CoreVMFunction_search; |
170 |
|
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() { |
186 |
delete m_fnShRight; |
delete m_fnShRight; |
187 |
delete m_fnMin; |
delete m_fnMin; |
188 |
delete m_fnMax; |
delete m_fnMax; |
189 |
|
delete m_fnArrayEqual; |
190 |
|
delete m_fnSearch; |
191 |
|
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 |
} |
} |
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 of global VM string variables.\n", context->globalStrVarCount)); |
217 |
if (!context->globalIntMemory) |
if (!context->globalIntMemory) |
218 |
context->globalIntMemory = new ArrayList<int>(); |
context->globalIntMemory = new ArrayList<vmint>(); |
219 |
|
if (!context->globalRealMemory) |
220 |
|
context->globalRealMemory = new ArrayList<vmfloat>(); |
221 |
if (!context->globalStrMemory) |
if (!context->globalStrMemory) |
222 |
context->globalStrMemory = new ArrayList<String>(); |
context->globalStrMemory = new ArrayList<String>(); |
223 |
context->globalIntMemory->resize(context->globalIntVarCount); |
context->globalIntMemory->resize(context->globalIntVarCount); |
224 |
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int)); |
context->globalRealMemory->resize(context->globalRealVarCount); |
225 |
|
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(vmint)); |
226 |
|
memset(&((*context->globalRealMemory)[0]), 0, context->globalRealVarCount * sizeof(vmfloat)); |
227 |
|
|
228 |
context->globalStrMemory->resize(context->globalStrVarCount); |
context->globalStrMemory->resize(context->globalStrVarCount); |
229 |
|
|
243 |
return; |
return; |
244 |
} |
} |
245 |
if (!ctx->globalIntMemory) { |
if (!ctx->globalIntMemory) { |
246 |
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"; |
247 |
|
return; |
248 |
|
} |
249 |
|
if (!ctx->globalRealMemory) { |
250 |
|
std::cerr << "Internal error: no global real number memory assigend to script VM.\n"; |
251 |
return; |
return; |
252 |
} |
} |
253 |
ctx->handlers->dump(); |
ctx->handlers->dump(); |
262 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
263 |
} |
} |
264 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
265 |
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", |
266 |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))); |
267 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
268 |
const int polySize = parserCtx->polyphonicIntVarCount; |
const vmint polySize = parserCtx->polyphonicIntVarCount; |
269 |
execCtx->polyphonicIntMemory.resize(polySize); |
execCtx->polyphonicIntMemory.resize(polySize); |
270 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(vmint)); |
271 |
|
|
272 |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
dmsg(2,("Allocated %lld bytes polyphonic memory.\n", polySize * sizeof(vmint))); |
273 |
return execCtx; |
return execCtx; |
274 |
} |
} |
275 |
|
|
279 |
} |
} |
280 |
|
|
281 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
282 |
NkspScanner scanner(is); |
try { |
283 |
std::vector<SourceToken> tokens = scanner.tokens(); |
NkspScanner scanner(is); |
284 |
std::vector<VMSourceToken> result; |
std::vector<SourceToken> tokens = scanner.tokens(); |
285 |
result.resize(tokens.size()); |
std::vector<VMSourceToken> result; |
286 |
for (int i = 0; i < tokens.size(); ++i) { |
result.resize(tokens.size()); |
287 |
SourceToken* st = new SourceToken; |
for (vmint i = 0; i < tokens.size(); ++i) { |
288 |
*st = tokens[i]; |
SourceToken* st = new SourceToken; |
289 |
result[i] = VMSourceToken(st); |
*st = tokens[i]; |
290 |
|
result[i] = VMSourceToken(st); |
291 |
|
} |
292 |
|
return result; |
293 |
|
} catch (...) { |
294 |
|
return std::vector<VMSourceToken>(); |
295 |
} |
} |
|
return result; |
|
296 |
} |
} |
297 |
|
|
298 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
309 |
else if (name == "sh_right") return m_fnShRight; |
else if (name == "sh_right") return m_fnShRight; |
310 |
else if (name == "min") return m_fnMin; |
else if (name == "min") return m_fnMin; |
311 |
else if (name == "max") return m_fnMax; |
else if (name == "max") return m_fnMax; |
312 |
|
else if (name == "array_equal") return m_fnArrayEqual; |
313 |
|
else if (name == "search") return m_fnSearch; |
314 |
|
else if (name == "sort") return m_fnSort; |
315 |
|
else if (name == "int_to_real") return m_fnIntToReal; |
316 |
|
else if (name == "real") return m_fnIntToReal; |
317 |
|
else if (name == "real_to_int") return m_fnRealToInt; |
318 |
|
else if (name == "int") return m_fnRealToInt; |
319 |
return NULL; |
return NULL; |
320 |
} |
} |
321 |
|
|
322 |
std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() { |
bool ScriptVM::isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) { |
323 |
return std::map<String,VMIntRelPtr*>(); |
ParserContext* parserCtx = dynamic_cast<ParserContext*>(ctx); |
324 |
|
if (!parserCtx) return false; |
325 |
|
|
326 |
|
if (fn == m_fnMessage && parserCtx->userPreprocessorConditions.count("NKSP_NO_MESSAGE")) |
327 |
|
return true; |
328 |
|
|
329 |
|
return false; |
330 |
|
} |
331 |
|
|
332 |
|
std::map<String,VMIntPtr*> ScriptVM::builtInIntVariables() { |
333 |
|
return std::map<String,VMIntPtr*>(); |
334 |
} |
} |
335 |
|
|
336 |
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
347 |
return m; |
return m; |
348 |
} |
} |
349 |
|
|
350 |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
std::map<String,vmint> ScriptVM::builtInConstIntVariables() { |
351 |
std::map<String,int> m; |
std::map<String,vmint> m; |
352 |
|
|
353 |
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
354 |
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
379 |
return m_autoSuspend; |
return m_autoSuspend; |
380 |
} |
} |
381 |
|
|
382 |
|
void ScriptVM::setExitResultEnabled(bool b) { |
383 |
|
m_acceptExitRes = b; |
384 |
|
} |
385 |
|
|
386 |
|
bool ScriptVM::isExitResultEnabled() const { |
387 |
|
return m_acceptExitRes; |
388 |
|
} |
389 |
|
|
390 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
391 |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
392 |
if (!m_parserContext) { |
if (!m_parserContext) { |
393 |
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"; |
394 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
395 |
} |
} |
396 |
|
|
409 |
m_parserContext->execContext = ctx; |
m_parserContext->execContext = ctx; |
410 |
|
|
411 |
ctx->status = VM_EXEC_RUNNING; |
ctx->status = VM_EXEC_RUNNING; |
412 |
StmtFlags_t flags = STMT_SUCCESS; |
ctx->instructionsCount = 0; |
413 |
int instructionsCounter = 0; |
ctx->clearExitRes(); |
414 |
|
StmtFlags_t& flags = ctx->flags; |
415 |
|
vmint instructionsCounter = 0; |
416 |
|
vmint synced = m_autoSuspend ? 0 : 1; |
417 |
|
|
418 |
int& frameIdx = ctx->stackFrame; |
int& frameIdx = ctx->stackFrame; |
419 |
if (frameIdx < 0) { // start condition ... |
if (frameIdx < 0) { // start condition ... |
469 |
if (frame.subindex < 0) ctx->popStack(); |
if (frame.subindex < 0) ctx->popStack(); |
470 |
else { |
else { |
471 |
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
472 |
frame.subindex = branchStmt->evalBranch(); |
frame.subindex = |
473 |
|
(decltype(frame.subindex)) |
474 |
|
branchStmt->evalBranch(); |
475 |
if (frame.subindex >= 0) { |
if (frame.subindex >= 0) { |
476 |
ctx->pushStack( |
ctx->pushStack( |
477 |
branchStmt->branch(frame.subindex) |
branchStmt->branch(frame.subindex) |
492 |
ctx->pushStack( |
ctx->pushStack( |
493 |
whileStmt->statements() |
whileStmt->statements() |
494 |
); |
); |
495 |
if (flags == STMT_SUCCESS && m_autoSuspend && |
if (flags == STMT_SUCCESS && !synced && |
496 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT) |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT) |
497 |
{ |
{ |
498 |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
501 |
} else ctx->popStack(); |
} else ctx->popStack(); |
502 |
break; |
break; |
503 |
} |
} |
504 |
|
|
505 |
|
case STMT_SYNC: { |
506 |
|
#if DEBUG_SCRIPTVM_CORE |
507 |
|
_printIndents(frameIdx); |
508 |
|
printf("-> STMT_SYNC\n"); |
509 |
|
#endif |
510 |
|
SyncBlock* syncStmt = (SyncBlock*) frame.statement; |
511 |
|
if (!frame.subindex++ && syncStmt->statements()) { |
512 |
|
++synced; |
513 |
|
ctx->pushStack( |
514 |
|
syncStmt->statements() |
515 |
|
); |
516 |
|
} else { |
517 |
|
ctx->popStack(); |
518 |
|
--synced; |
519 |
|
} |
520 |
|
break; |
521 |
|
} |
522 |
|
|
523 |
|
case STMT_NOOP: |
524 |
|
break; // no operation like the name suggests |
525 |
} |
} |
526 |
|
|
527 |
if (flags == STMT_SUCCESS && m_autoSuspend && |
if (flags == STMT_SUCCESS && !synced && |
528 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD) |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD) |
529 |
{ |
{ |
530 |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
534 |
++instructionsCounter; |
++instructionsCounter; |
535 |
} |
} |
536 |
|
|
537 |
if (flags & STMT_SUSPEND_SIGNALLED) { |
if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) { |
538 |
ctx->status = VM_EXEC_SUSPENDED; |
ctx->status = VM_EXEC_SUSPENDED; |
539 |
|
ctx->flags = STMT_SUCCESS; |
540 |
} else { |
} else { |
541 |
ctx->status = VM_EXEC_NOT_RUNNING; |
ctx->status = VM_EXEC_NOT_RUNNING; |
542 |
if (flags & STMT_ERROR_OCCURRED) |
if (flags & STMT_ERROR_OCCURRED) |
544 |
ctx->reset(); |
ctx->reset(); |
545 |
} |
} |
546 |
|
|
547 |
|
ctx->instructionsCount = instructionsCounter; |
548 |
|
|
549 |
m_eventHandler = NULL; |
m_eventHandler = NULL; |
550 |
m_parserContext->execContext = NULL; |
m_parserContext->execContext = NULL; |
551 |
m_parserContext = NULL; |
m_parserContext = NULL; |