1 |
/* |
2 |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
* |
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 |
#include <string.h> |
13 |
#include <assert.h> |
14 |
#include "../common/global_private.h" |
15 |
#include "tree.h" |
16 |
#include "CoreVMFunctions.h" |
17 |
#include "CoreVMDynVars.h" |
18 |
#include "editor/NkspScanner.h" |
19 |
|
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 |
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_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER; |
103 |
m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER; |
104 |
} |
105 |
|
106 |
ScriptVM::~ScriptVM() { |
107 |
delete m_fnMessage; |
108 |
delete m_fnExit; |
109 |
delete m_fnWait; |
110 |
delete m_fnAbs; |
111 |
delete m_fnRandom; |
112 |
delete m_fnNumElements; |
113 |
delete m_varRealTimer; |
114 |
delete m_varPerfTimer; |
115 |
} |
116 |
|
117 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
118 |
std::istringstream iss(s); |
119 |
return loadScript(&iss); |
120 |
} |
121 |
|
122 |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
123 |
ParserContext* context = new ParserContext(this); |
124 |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
125 |
|
126 |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
127 |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
128 |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
129 |
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
130 |
|
131 |
context->createScanner(is); |
132 |
|
133 |
InstrScript_parse(context); |
134 |
dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int)))); |
135 |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
136 |
if (!context->globalIntMemory) |
137 |
context->globalIntMemory = new ArrayList<int>(); |
138 |
if (!context->globalStrMemory) |
139 |
context->globalStrMemory = new ArrayList<String>(); |
140 |
context->globalIntMemory->resize(context->globalIntVarCount); |
141 |
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int)); |
142 |
|
143 |
context->globalStrMemory->resize(context->globalStrVarCount); |
144 |
|
145 |
context->destroyScanner(); |
146 |
|
147 |
return context; |
148 |
} |
149 |
|
150 |
void ScriptVM::dumpParsedScript(VMParserContext* context) { |
151 |
ParserContext* ctx = dynamic_cast<ParserContext*>(context); |
152 |
if (!ctx) { |
153 |
std::cerr << "No VM context. So nothing to dump.\n"; |
154 |
return; |
155 |
} |
156 |
if (!ctx->handlers) { |
157 |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
158 |
return; |
159 |
} |
160 |
if (!ctx->globalIntMemory) { |
161 |
std::cerr << "Internal error: no global memory assigend to script VM.\n"; |
162 |
return; |
163 |
} |
164 |
ctx->handlers->dump(); |
165 |
} |
166 |
|
167 |
VMExecContext* ScriptVM::createExecContext(VMParserContext* parserContext) { |
168 |
ParserContext* parserCtx = dynamic_cast<ParserContext*>(parserContext); |
169 |
ExecContext* execCtx = new ExecContext(); |
170 |
|
171 |
if (parserCtx->requiredMaxStackSize < 0) { |
172 |
parserCtx->requiredMaxStackSize = |
173 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
174 |
} |
175 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
176 |
dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", |
177 |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
178 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
179 |
const int polySize = parserCtx->polyphonicIntVarCount; |
180 |
execCtx->polyphonicIntMemory.resize(polySize); |
181 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
182 |
|
183 |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
184 |
return execCtx; |
185 |
} |
186 |
|
187 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
188 |
std::istringstream iss(s); |
189 |
return syntaxHighlighting(&iss); |
190 |
} |
191 |
|
192 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
193 |
NkspScanner scanner(is); |
194 |
std::vector<SourceToken> tokens = scanner.tokens(); |
195 |
std::vector<VMSourceToken> result; |
196 |
result.resize(tokens.size()); |
197 |
for (int i = 0; i < tokens.size(); ++i) { |
198 |
SourceToken* st = new SourceToken; |
199 |
*st = tokens[i]; |
200 |
result[i] = VMSourceToken(st); |
201 |
} |
202 |
return result; |
203 |
} |
204 |
|
205 |
VMFunction* ScriptVM::functionByName(const String& name) { |
206 |
if (name == "message") return m_fnMessage; |
207 |
else if (name == "exit") return m_fnExit; |
208 |
else if (name == "wait") return m_fnWait; |
209 |
else if (name == "abs") return m_fnAbs; |
210 |
else if (name == "random") return m_fnRandom; |
211 |
else if (name == "num_elements") return m_fnNumElements; |
212 |
return NULL; |
213 |
} |
214 |
|
215 |
std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() { |
216 |
return std::map<String,VMIntRelPtr*>(); |
217 |
} |
218 |
|
219 |
std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() { |
220 |
return std::map<String,VMInt8Array*>(); |
221 |
} |
222 |
|
223 |
std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() { |
224 |
std::map<String,VMDynVar*> m; |
225 |
|
226 |
m["$NKSP_PERF_TIMER"] = m_varPerfTimer; |
227 |
m["$NKSP_REAL_TIMER"] = m_varRealTimer; |
228 |
m["$KSP_TIMER"] = m_varRealTimer; |
229 |
|
230 |
return m; |
231 |
} |
232 |
|
233 |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
234 |
return std::map<String,int>(); |
235 |
} |
236 |
|
237 |
VMEventHandler* ScriptVM::currentVMEventHandler() { |
238 |
return m_eventHandler; |
239 |
} |
240 |
|
241 |
VMParserContext* ScriptVM::currentVMParserContext() { |
242 |
return m_parserContext; |
243 |
} |
244 |
|
245 |
VMExecContext* ScriptVM::currentVMExecContext() { |
246 |
if (!m_parserContext) return NULL; |
247 |
return m_parserContext->execContext; |
248 |
} |
249 |
|
250 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
251 |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
252 |
if (!m_parserContext) { |
253 |
std::cerr << "No VM parser context provided. Did you load a script?.\n"; |
254 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
255 |
} |
256 |
|
257 |
// a ParserContext object is always tied to exactly one ScriptVM object |
258 |
assert(m_parserContext->functionProvider == this); |
259 |
|
260 |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
261 |
if (!ctx) { |
262 |
std::cerr << "Invalid VM exec context.\n"; |
263 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
264 |
} |
265 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
266 |
if (!h) return VM_EXEC_NOT_RUNNING; |
267 |
m_eventHandler = handler; |
268 |
|
269 |
m_parserContext->execContext = ctx; |
270 |
|
271 |
ctx->status = VM_EXEC_RUNNING; |
272 |
StmtFlags_t flags = STMT_SUCCESS; |
273 |
|
274 |
int& frameIdx = ctx->stackFrame; |
275 |
if (frameIdx < 0) { // start condition ... |
276 |
frameIdx = -1; |
277 |
ctx->pushStack(h); |
278 |
} |
279 |
|
280 |
while (flags == STMT_SUCCESS && frameIdx >= 0) { |
281 |
if (frameIdx >= ctx->stack.size()) { // should never happen, otherwise it's a bug ... |
282 |
std::cerr << "CRITICAL: VM stack overflow! (" << frameIdx << ")\n"; |
283 |
flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
284 |
break; |
285 |
} |
286 |
|
287 |
ExecContext::StackFrame& frame = ctx->stack[frameIdx]; |
288 |
switch (frame.statement->statementType()) { |
289 |
case STMT_LEAF: { |
290 |
#if DEBUG_SCRIPTVM_CORE |
291 |
_printIndents(frameIdx); |
292 |
printf("-> STMT_LEAF\n"); |
293 |
#endif |
294 |
LeafStatement* leaf = (LeafStatement*) frame.statement; |
295 |
flags = leaf->exec(); |
296 |
ctx->popStack(); |
297 |
break; |
298 |
} |
299 |
|
300 |
case STMT_LIST: { |
301 |
#if DEBUG_SCRIPTVM_CORE |
302 |
_printIndents(frameIdx); |
303 |
printf("-> STMT_LIST subidx=%d\n", frame.subindex); |
304 |
#endif |
305 |
Statements* stmts = (Statements*) frame.statement; |
306 |
if (stmts->statement(frame.subindex)) { |
307 |
ctx->pushStack( |
308 |
stmts->statement(frame.subindex++) |
309 |
); |
310 |
} else { |
311 |
#if DEBUG_SCRIPTVM_CORE |
312 |
_printIndents(frameIdx); |
313 |
printf("[END OF LIST] subidx=%d\n", frame.subindex); |
314 |
#endif |
315 |
ctx->popStack(); |
316 |
} |
317 |
break; |
318 |
} |
319 |
|
320 |
case STMT_BRANCH: { |
321 |
#if DEBUG_SCRIPTVM_CORE |
322 |
_printIndents(frameIdx); |
323 |
printf("-> STMT_BRANCH\n"); |
324 |
#endif |
325 |
if (frame.subindex < 0) ctx->popStack(); |
326 |
else { |
327 |
BranchStatement* branchStmt = (BranchStatement*) frame.statement; |
328 |
frame.subindex = branchStmt->evalBranch(); |
329 |
if (frame.subindex >= 0) { |
330 |
ctx->pushStack( |
331 |
branchStmt->branch(frame.subindex) |
332 |
); |
333 |
frame.subindex = -1; |
334 |
} else ctx->popStack(); |
335 |
} |
336 |
break; |
337 |
} |
338 |
|
339 |
case STMT_LOOP: { |
340 |
#if DEBUG_SCRIPTVM_CORE |
341 |
_printIndents(frameIdx); |
342 |
printf("-> STMT_LOOP\n"); |
343 |
#endif |
344 |
While* whileStmt = (While*) frame.statement; |
345 |
if (whileStmt->evalLoopStartCondition() && whileStmt->statements()) { |
346 |
ctx->pushStack( |
347 |
whileStmt->statements() |
348 |
); |
349 |
} else ctx->popStack(); |
350 |
} |
351 |
} |
352 |
} |
353 |
|
354 |
if (flags & STMT_SUSPEND_SIGNALLED) { |
355 |
ctx->status = VM_EXEC_SUSPENDED; |
356 |
} else { |
357 |
ctx->status = VM_EXEC_NOT_RUNNING; |
358 |
if (flags & STMT_ERROR_OCCURRED) |
359 |
ctx->status = VM_EXEC_ERROR; |
360 |
ctx->reset(); |
361 |
} |
362 |
|
363 |
m_eventHandler = NULL; |
364 |
m_parserContext->execContext = NULL; |
365 |
m_parserContext = NULL; |
366 |
return ctx->status; |
367 |
} |
368 |
|
369 |
} // namespace LinuxSampler |