1 |
schoenebeck |
2581 |
/* |
2 |
schoenebeck |
2879 |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
schoenebeck |
2581 |
* |
4 |
|
|
* http://www.linuxsampler.org |
5 |
|
|
* |
6 |
|
|
* This file is part of LinuxSampler and released under the same terms. |
7 |
|
|
* See README file for details. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "ScriptVM.h" |
11 |
|
|
|
12 |
schoenebeck |
2588 |
#include <string.h> |
13 |
schoenebeck |
2619 |
#include <assert.h> |
14 |
schoenebeck |
2581 |
#include "../common/global_private.h" |
15 |
|
|
#include "tree.h" |
16 |
schoenebeck |
2885 |
#include "CoreVMFunctions.h" |
17 |
schoenebeck |
2942 |
#include "CoreVMDynVars.h" |
18 |
schoenebeck |
2885 |
#include "editor/NkspScanner.h" |
19 |
schoenebeck |
2581 |
|
20 |
|
|
#define DEBUG_SCRIPTVM_CORE 0 |
21 |
|
|
|
22 |
|
|
int InstrScript_parse(LinuxSampler::ParserContext*); |
23 |
|
|
|
24 |
|
|
namespace LinuxSampler { |
25 |
|
|
|
26 |
|
|
static void _printIndents(int n) { |
27 |
|
|
for (int i = 0; i < n; ++i) printf(" "); |
28 |
|
|
fflush(stdout); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
static int _requiredMaxStackSizeFor(Statement* statement, int depth = 0) { |
32 |
|
|
if (!statement) return 1; |
33 |
|
|
|
34 |
|
|
switch (statement->statementType()) { |
35 |
|
|
case STMT_LEAF: |
36 |
|
|
#if DEBUG_SCRIPTVM_CORE |
37 |
|
|
_printIndents(depth); |
38 |
|
|
printf("-> STMT_LEAF\n"); |
39 |
|
|
#endif |
40 |
|
|
return 1; |
41 |
|
|
|
42 |
|
|
case STMT_LIST: { |
43 |
|
|
#if DEBUG_SCRIPTVM_CORE |
44 |
|
|
_printIndents(depth); |
45 |
|
|
printf("-> STMT_LIST\n"); |
46 |
|
|
#endif |
47 |
|
|
Statements* stmts = (Statements*) statement; |
48 |
|
|
int max = 0; |
49 |
|
|
for (int i = 0; stmts->statement(i); ++i) { |
50 |
|
|
int size = _requiredMaxStackSizeFor( stmts->statement(i), depth+1 ); |
51 |
|
|
if (max < size) max = size; |
52 |
|
|
} |
53 |
|
|
return max + 1; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
case STMT_BRANCH: { |
57 |
|
|
#if DEBUG_SCRIPTVM_CORE |
58 |
|
|
_printIndents(depth); |
59 |
|
|
printf("-> STMT_BRANCH\n"); |
60 |
|
|
#endif |
61 |
|
|
BranchStatement* branchStmt = (BranchStatement*) statement; |
62 |
|
|
int max = 0; |
63 |
|
|
for (int i = 0; branchStmt->branch(i); ++i) { |
64 |
|
|
int size = _requiredMaxStackSizeFor( branchStmt->branch(i), depth+1 ); |
65 |
|
|
if (max < size) max = size; |
66 |
|
|
} |
67 |
|
|
return max + 1; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
case STMT_LOOP: { |
71 |
|
|
#if DEBUG_SCRIPTVM_CORE |
72 |
|
|
_printIndents(depth); |
73 |
|
|
printf("-> STMT_LOOP\n"); |
74 |
|
|
#endif |
75 |
|
|
While* whileStmt = (While*) statement; |
76 |
|
|
if (whileStmt->statements()) |
77 |
|
|
return _requiredMaxStackSizeFor( whileStmt->statements() ) + 1; |
78 |
|
|
else |
79 |
|
|
return 1; |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
return 1; // actually just to avoid compiler warning |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
static int _requiredMaxStackSizeFor(EventHandlers* handlers) { |
87 |
|
|
int max = 1; |
88 |
|
|
for (int i = 0; i < handlers->size(); ++i) { |
89 |
|
|
int size = _requiredMaxStackSizeFor(handlers->eventHandler(i)); |
90 |
|
|
if (max < size) max = size; |
91 |
|
|
} |
92 |
|
|
return max; |
93 |
|
|
} |
94 |
|
|
|
95 |
schoenebeck |
2885 |
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 |
schoenebeck |
2945 |
m_fnInc = new CoreVMFunction_inc; |
103 |
|
|
m_fnDec = new CoreVMFunction_dec; |
104 |
schoenebeck |
2942 |
m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER; |
105 |
|
|
m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER; |
106 |
schoenebeck |
2581 |
} |
107 |
|
|
|
108 |
|
|
ScriptVM::~ScriptVM() { |
109 |
schoenebeck |
2885 |
delete m_fnMessage; |
110 |
|
|
delete m_fnExit; |
111 |
|
|
delete m_fnWait; |
112 |
|
|
delete m_fnAbs; |
113 |
|
|
delete m_fnRandom; |
114 |
|
|
delete m_fnNumElements; |
115 |
schoenebeck |
2945 |
delete m_fnInc; |
116 |
|
|
delete m_fnDec; |
117 |
schoenebeck |
2942 |
delete m_varRealTimer; |
118 |
|
|
delete m_varPerfTimer; |
119 |
schoenebeck |
2581 |
} |
120 |
|
|
|
121 |
schoenebeck |
2588 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
122 |
schoenebeck |
2581 |
std::istringstream iss(s); |
123 |
schoenebeck |
2588 |
return loadScript(&iss); |
124 |
schoenebeck |
2581 |
} |
125 |
|
|
|
126 |
schoenebeck |
2588 |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
127 |
|
|
ParserContext* context = new ParserContext(this); |
128 |
|
|
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
129 |
schoenebeck |
2594 |
|
130 |
|
|
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
131 |
|
|
context->registerBuiltInIntVariables( builtInIntVariables() ); |
132 |
|
|
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
133 |
schoenebeck |
2942 |
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
134 |
schoenebeck |
2594 |
|
135 |
schoenebeck |
2588 |
context->createScanner(is); |
136 |
schoenebeck |
2581 |
|
137 |
schoenebeck |
2588 |
InstrScript_parse(context); |
138 |
persson |
2837 |
dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int)))); |
139 |
schoenebeck |
2611 |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
140 |
schoenebeck |
2588 |
if (!context->globalIntMemory) |
141 |
|
|
context->globalIntMemory = new ArrayList<int>(); |
142 |
|
|
if (!context->globalStrMemory) |
143 |
|
|
context->globalStrMemory = new ArrayList<String>(); |
144 |
|
|
context->globalIntMemory->resize(context->globalIntVarCount); |
145 |
|
|
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int)); |
146 |
|
|
|
147 |
|
|
context->globalStrMemory->resize(context->globalStrVarCount); |
148 |
schoenebeck |
2581 |
|
149 |
schoenebeck |
2588 |
context->destroyScanner(); |
150 |
schoenebeck |
2581 |
|
151 |
schoenebeck |
2588 |
return context; |
152 |
schoenebeck |
2581 |
} |
153 |
|
|
|
154 |
schoenebeck |
2588 |
void ScriptVM::dumpParsedScript(VMParserContext* context) { |
155 |
|
|
ParserContext* ctx = dynamic_cast<ParserContext*>(context); |
156 |
|
|
if (!ctx) { |
157 |
schoenebeck |
2581 |
std::cerr << "No VM context. So nothing to dump.\n"; |
158 |
|
|
return; |
159 |
|
|
} |
160 |
schoenebeck |
2588 |
if (!ctx->handlers) { |
161 |
schoenebeck |
2581 |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
162 |
|
|
return; |
163 |
|
|
} |
164 |
schoenebeck |
2588 |
if (!ctx->globalIntMemory) { |
165 |
schoenebeck |
2581 |
std::cerr << "Internal error: no global memory assigend to script VM.\n"; |
166 |
|
|
return; |
167 |
|
|
} |
168 |
schoenebeck |
2588 |
ctx->handlers->dump(); |
169 |
schoenebeck |
2581 |
} |
170 |
|
|
|
171 |
schoenebeck |
2588 |
VMExecContext* ScriptVM::createExecContext(VMParserContext* parserContext) { |
172 |
|
|
ParserContext* parserCtx = dynamic_cast<ParserContext*>(parserContext); |
173 |
|
|
ExecContext* execCtx = new ExecContext(); |
174 |
|
|
|
175 |
|
|
if (parserCtx->requiredMaxStackSize < 0) { |
176 |
|
|
parserCtx->requiredMaxStackSize = |
177 |
|
|
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
178 |
|
|
} |
179 |
|
|
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
180 |
persson |
2837 |
dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", |
181 |
|
|
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
182 |
schoenebeck |
2588 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
183 |
|
|
const int polySize = parserCtx->polyphonicIntVarCount; |
184 |
|
|
execCtx->polyphonicIntMemory.resize(polySize); |
185 |
|
|
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
186 |
|
|
|
187 |
persson |
2837 |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
188 |
schoenebeck |
2588 |
return execCtx; |
189 |
schoenebeck |
2581 |
} |
190 |
|
|
|
191 |
schoenebeck |
2885 |
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 |
schoenebeck |
2581 |
VMFunction* ScriptVM::functionByName(const String& name) { |
210 |
schoenebeck |
2885 |
if (name == "message") return m_fnMessage; |
211 |
|
|
else if (name == "exit") return m_fnExit; |
212 |
|
|
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 |
schoenebeck |
2945 |
else if (name == "inc") return m_fnInc; |
217 |
|
|
else if (name == "dec") return m_fnDec; |
218 |
schoenebeck |
2581 |
return NULL; |
219 |
|
|
} |
220 |
schoenebeck |
2588 |
|
221 |
schoenebeck |
2594 |
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 |
schoenebeck |
2942 |
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 |
schoenebeck |
2594 |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
240 |
|
|
return std::map<String,int>(); |
241 |
|
|
} |
242 |
|
|
|
243 |
schoenebeck |
2879 |
VMEventHandler* ScriptVM::currentVMEventHandler() { |
244 |
|
|
return m_eventHandler; |
245 |
|
|
} |
246 |
|
|
|
247 |
schoenebeck |
2588 |
VMParserContext* ScriptVM::currentVMParserContext() { |
248 |
|
|
return m_parserContext; |
249 |
|
|
} |
250 |
|
|
|
251 |
schoenebeck |
2581 |
VMExecContext* ScriptVM::currentVMExecContext() { |
252 |
schoenebeck |
2588 |
if (!m_parserContext) return NULL; |
253 |
|
|
return m_parserContext->execContext; |
254 |
schoenebeck |
2581 |
} |
255 |
|
|
|
256 |
schoenebeck |
2588 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
257 |
|
|
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
258 |
|
|
if (!m_parserContext) { |
259 |
|
|
std::cerr << "No VM parser context provided. Did you load a script?.\n"; |
260 |
schoenebeck |
2581 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
261 |
|
|
} |
262 |
|
|
|
263 |
schoenebeck |
2611 |
// a ParserContext object is always tied to exactly one ScriptVM object |
264 |
|
|
assert(m_parserContext->functionProvider == this); |
265 |
|
|
|
266 |
schoenebeck |
2581 |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
267 |
|
|
if (!ctx) { |
268 |
|
|
std::cerr << "Invalid VM exec context.\n"; |
269 |
|
|
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
270 |
|
|
} |
271 |
|
|
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
272 |
|
|
if (!h) return VM_EXEC_NOT_RUNNING; |
273 |
schoenebeck |
2879 |
m_eventHandler = handler; |
274 |
schoenebeck |
2581 |
|
275 |
schoenebeck |
2588 |
m_parserContext->execContext = ctx; |
276 |
schoenebeck |
2581 |
|
277 |
|
|
ctx->status = VM_EXEC_RUNNING; |
278 |
|
|
StmtFlags_t flags = STMT_SUCCESS; |
279 |
|
|
|
280 |
|
|
int& frameIdx = ctx->stackFrame; |
281 |
|
|
if (frameIdx < 0) { // start condition ... |
282 |
|
|
frameIdx = -1; |
283 |
|
|
ctx->pushStack(h); |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
while (flags == STMT_SUCCESS && frameIdx >= 0) { |
287 |
|
|
if (frameIdx >= ctx->stack.size()) { // should never happen, otherwise it's a bug ... |
288 |
|
|
std::cerr << "CRITICAL: VM stack overflow! (" << frameIdx << ")\n"; |
289 |
|
|
flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
290 |
|
|
break; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
ExecContext::StackFrame& frame = ctx->stack[frameIdx]; |
294 |
|
|
switch (frame.statement->statementType()) { |
295 |
|
|
case STMT_LEAF: { |
296 |
|
|
#if DEBUG_SCRIPTVM_CORE |
297 |
|
|
_printIndents(frameIdx); |
298 |
|
|
printf("-> STMT_LEAF\n"); |
299 |
|
|
#endif |
300 |
|
|
LeafStatement* leaf = (LeafStatement*) frame.statement; |
301 |
|
|
flags = leaf->exec(); |
302 |
|
|
ctx->popStack(); |
303 |
|
|
break; |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
case STMT_LIST: { |
307 |
|
|
#if DEBUG_SCRIPTVM_CORE |
308 |
|
|
_printIndents(frameIdx); |
309 |
|
|
printf("-> STMT_LIST subidx=%d\n", frame.subindex); |
310 |
|
|
#endif |
311 |
|
|
Statements* stmts = (Statements*) frame.statement; |
312 |
|
|
if (stmts->statement(frame.subindex)) { |
313 |
|
|
ctx->pushStack( |
314 |
|
|
stmts->statement(frame.subindex++) |
315 |
|
|
); |
316 |
|
|
} else { |
317 |
|
|
#if DEBUG_SCRIPTVM_CORE |
318 |
|
|
_printIndents(frameIdx); |
319 |
|
|
printf("[END OF LIST] subidx=%d\n", frame.subindex); |
320 |
|
|
#endif |
321 |
|
|
ctx->popStack(); |
322 |
|
|
} |
323 |
|
|
break; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
case STMT_BRANCH: { |
327 |
|
|
#if DEBUG_SCRIPTVM_CORE |
328 |
|
|
_printIndents(frameIdx); |
329 |
|
|
printf("-> STMT_BRANCH\n"); |
330 |
|
|
#endif |
331 |
|
|
if (frame.subindex < 0) ctx->popStack(); |
332 |
|
|
else { |
333 |
|
|
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
334 |
|
|
frame.subindex = branchStmt->evalBranch(); |
335 |
|
|
if (frame.subindex >= 0) { |
336 |
|
|
ctx->pushStack( |
337 |
|
|
branchStmt->branch(frame.subindex) |
338 |
|
|
); |
339 |
|
|
frame.subindex = -1; |
340 |
|
|
} else ctx->popStack(); |
341 |
|
|
} |
342 |
|
|
break; |
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
case STMT_LOOP: { |
346 |
|
|
#if DEBUG_SCRIPTVM_CORE |
347 |
|
|
_printIndents(frameIdx); |
348 |
|
|
printf("-> STMT_LOOP\n"); |
349 |
|
|
#endif |
350 |
|
|
While* whileStmt = (While*) frame.statement; |
351 |
|
|
if (whileStmt->evalLoopStartCondition() && whileStmt->statements()) { |
352 |
|
|
ctx->pushStack( |
353 |
|
|
whileStmt->statements() |
354 |
|
|
); |
355 |
|
|
} else ctx->popStack(); |
356 |
|
|
} |
357 |
|
|
} |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
if (flags & STMT_SUSPEND_SIGNALLED) { |
361 |
|
|
ctx->status = VM_EXEC_SUSPENDED; |
362 |
|
|
} else { |
363 |
|
|
ctx->status = VM_EXEC_NOT_RUNNING; |
364 |
|
|
if (flags & STMT_ERROR_OCCURRED) |
365 |
|
|
ctx->status = VM_EXEC_ERROR; |
366 |
|
|
ctx->reset(); |
367 |
|
|
} |
368 |
|
|
|
369 |
schoenebeck |
2879 |
m_eventHandler = NULL; |
370 |
schoenebeck |
2588 |
m_parserContext->execContext = NULL; |
371 |
|
|
m_parserContext = NULL; |
372 |
schoenebeck |
2581 |
return ctx->status; |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
} // namespace LinuxSampler |