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; |
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; |
208 |
context->createScanner(is); |
context->createScanner(is); |
209 |
|
|
210 |
InstrScript_parse(context); |
InstrScript_parse(context); |
211 |
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))); |
212 |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
dmsg(2,("Allocating %lld of global VM string variables.\n", context->globalStrVarCount)); |
213 |
if (!context->globalIntMemory) |
if (!context->globalIntMemory) |
214 |
context->globalIntMemory = new ArrayList<int>(); |
context->globalIntMemory = new ArrayList<vmint>(); |
215 |
if (!context->globalStrMemory) |
if (!context->globalStrMemory) |
216 |
context->globalStrMemory = new ArrayList<String>(); |
context->globalStrMemory = new ArrayList<String>(); |
217 |
context->globalIntMemory->resize(context->globalIntVarCount); |
context->globalIntMemory->resize(context->globalIntVarCount); |
218 |
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int)); |
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(vmint)); |
219 |
|
|
220 |
context->globalStrMemory->resize(context->globalStrVarCount); |
context->globalStrMemory->resize(context->globalStrVarCount); |
221 |
|
|
250 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
251 |
} |
} |
252 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
253 |
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", |
254 |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))); |
255 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
256 |
const int polySize = parserCtx->polyphonicIntVarCount; |
const vmint polySize = parserCtx->polyphonicIntVarCount; |
257 |
execCtx->polyphonicIntMemory.resize(polySize); |
execCtx->polyphonicIntMemory.resize(polySize); |
258 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(vmint)); |
259 |
|
|
260 |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
dmsg(2,("Allocated %lld bytes polyphonic memory.\n", polySize * sizeof(vmint))); |
261 |
return execCtx; |
return execCtx; |
262 |
} |
} |
263 |
|
|
267 |
} |
} |
268 |
|
|
269 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
270 |
NkspScanner scanner(is); |
try { |
271 |
std::vector<SourceToken> tokens = scanner.tokens(); |
NkspScanner scanner(is); |
272 |
std::vector<VMSourceToken> result; |
std::vector<SourceToken> tokens = scanner.tokens(); |
273 |
result.resize(tokens.size()); |
std::vector<VMSourceToken> result; |
274 |
for (int i = 0; i < tokens.size(); ++i) { |
result.resize(tokens.size()); |
275 |
SourceToken* st = new SourceToken; |
for (vmint i = 0; i < tokens.size(); ++i) { |
276 |
*st = tokens[i]; |
SourceToken* st = new SourceToken; |
277 |
result[i] = VMSourceToken(st); |
*st = tokens[i]; |
278 |
|
result[i] = VMSourceToken(st); |
279 |
|
} |
280 |
|
return result; |
281 |
|
} catch (...) { |
282 |
|
return std::vector<VMSourceToken>(); |
283 |
} |
} |
|
return result; |
|
284 |
} |
} |
285 |
|
|
286 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
313 |
return false; |
return false; |
314 |
} |
} |
315 |
|
|
316 |
std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() { |
std::map<String,VMIntPtr*> ScriptVM::builtInIntVariables() { |
317 |
return std::map<String,VMIntRelPtr*>(); |
return std::map<String,VMIntPtr*>(); |
318 |
} |
} |
319 |
|
|
320 |
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
331 |
return m; |
return m; |
332 |
} |
} |
333 |
|
|
334 |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
std::map<String,vmint> ScriptVM::builtInConstIntVariables() { |
335 |
std::map<String,int> m; |
std::map<String,vmint> m; |
336 |
|
|
337 |
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
338 |
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
363 |
return m_autoSuspend; |
return m_autoSuspend; |
364 |
} |
} |
365 |
|
|
366 |
|
void ScriptVM::setExitResultEnabled(bool b) { |
367 |
|
m_acceptExitRes = b; |
368 |
|
} |
369 |
|
|
370 |
|
bool ScriptVM::isExitResultEnabled() const { |
371 |
|
return m_acceptExitRes; |
372 |
|
} |
373 |
|
|
374 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
375 |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
376 |
if (!m_parserContext) { |
if (!m_parserContext) { |
377 |
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"; |
378 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
379 |
} |
} |
380 |
|
|
394 |
|
|
395 |
ctx->status = VM_EXEC_RUNNING; |
ctx->status = VM_EXEC_RUNNING; |
396 |
ctx->instructionsCount = 0; |
ctx->instructionsCount = 0; |
397 |
|
ctx->clearExitRes(); |
398 |
StmtFlags_t& flags = ctx->flags; |
StmtFlags_t& flags = ctx->flags; |
399 |
int instructionsCounter = 0; |
vmint instructionsCounter = 0; |
400 |
int synced = m_autoSuspend ? 0 : 1; |
vmint synced = m_autoSuspend ? 0 : 1; |
401 |
|
|
402 |
int& frameIdx = ctx->stackFrame; |
int& frameIdx = ctx->stackFrame; |
403 |
if (frameIdx < 0) { // start condition ... |
if (frameIdx < 0) { // start condition ... |
453 |
if (frame.subindex < 0) ctx->popStack(); |
if (frame.subindex < 0) ctx->popStack(); |
454 |
else { |
else { |
455 |
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
456 |
frame.subindex = branchStmt->evalBranch(); |
frame.subindex = |
457 |
|
(decltype(frame.subindex)) |
458 |
|
branchStmt->evalBranch(); |
459 |
if (frame.subindex >= 0) { |
if (frame.subindex >= 0) { |
460 |
ctx->pushStack( |
ctx->pushStack( |
461 |
branchStmt->branch(frame.subindex) |
branchStmt->branch(frame.subindex) |
503 |
} |
} |
504 |
break; |
break; |
505 |
} |
} |
506 |
|
|
507 |
|
case STMT_NOOP: |
508 |
|
break; // no operation like the name suggests |
509 |
} |
} |
510 |
|
|
511 |
if (flags == STMT_SUCCESS && !synced && |
if (flags == STMT_SUCCESS && !synced && |