/[svn]/linuxsampler/trunk/src/scriptvm/ScriptVM.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/ScriptVM.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2948 - (show annotations) (download)
Fri Jul 15 15:29:04 2016 UTC (7 years, 8 months ago) by schoenebeck
File size: 13816 byte(s)
* NKSP: Implemented built-in script function "stop_wait()".
* NKSP: Implemented built-in script variable "$NI_CALLBACK_ID".
* NKSP: Implemented built-in script variable "$NI_CALLBACK_TYPE".
* NKSP: Implemented built-in script variable "$NKSP_IGNORE_WAIT".
* NKSP: Added support for read-only built-in variables
  (respectively handled by the script parser).
* NKSP: Added built-in script constant "$NI_CB_TYPE_INIT".
* NKSP: Added built-in script constant "$NI_CB_TYPE_NOTE".
* NKSP: Added built-in script constant "$NI_CB_TYPE_RELEASE".
* NKSP: Added built-in script constant "$NI_CB_TYPE_CONTROLLER".
* Bumped version (2.0.0.svn17).

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_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() {
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) {
122 std::istringstream iss(s);
123 return loadScript(&iss);
124 }
125
126 VMParserContext* ScriptVM::loadScript(std::istream* is) {
127 ParserContext* context = new ParserContext(this);
128 //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);
136
137 InstrScript_parse(context);
138 dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int))));
139 dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount));
140 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
149 context->destroyScanner();
150
151 return context;
152 }
153
154 void ScriptVM::dumpParsedScript(VMParserContext* context) {
155 ParserContext* ctx = dynamic_cast<ParserContext*>(context);
156 if (!ctx) {
157 std::cerr << "No VM context. So nothing to dump.\n";
158 return;
159 }
160 if (!ctx->handlers) {
161 std::cerr << "No event handlers defined in script. So nothing to dump.\n";
162 return;
163 }
164 if (!ctx->globalIntMemory) {
165 std::cerr << "Internal error: no global memory assigend to script VM.\n";
166 return;
167 }
168 ctx->handlers->dump();
169 }
170
171 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 dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n",
181 long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))));
182 //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 dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int))));
188 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) {
210 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 else if (name == "inc") return m_fnInc;
217 else if (name == "dec") return m_fnDec;
218 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() {
255 return m_parserContext;
256 }
257
258 VMExecContext* ScriptVM::currentVMExecContext() {
259 if (!m_parserContext) return NULL;
260 return m_parserContext->execContext;
261 }
262
263 VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
264 m_parserContext = dynamic_cast<ParserContext*>(parserContext);
265 if (!m_parserContext) {
266 std::cerr << "No VM parser context provided. Did you load a script?.\n";
267 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);
274 if (!ctx) {
275 std::cerr << "Invalid VM exec context.\n";
276 return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);
277 }
278 EventHandler* h = dynamic_cast<EventHandler*>(handler);
279 if (!h) return VM_EXEC_NOT_RUNNING;
280 m_eventHandler = handler;
281
282 m_parserContext->execContext = ctx;
283
284 ctx->status = VM_EXEC_RUNNING;
285 StmtFlags_t flags = STMT_SUCCESS;
286
287 int& frameIdx = ctx->stackFrame;
288 if (frameIdx < 0) { // start condition ...
289 frameIdx = -1;
290 ctx->pushStack(h);
291 }
292
293 while (flags == STMT_SUCCESS && frameIdx >= 0) {
294 if (frameIdx >= ctx->stack.size()) { // should never happen, otherwise it's a bug ...
295 std::cerr << "CRITICAL: VM stack overflow! (" << frameIdx << ")\n";
296 flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
297 break;
298 }
299
300 ExecContext::StackFrame& frame = ctx->stack[frameIdx];
301 switch (frame.statement->statementType()) {
302 case STMT_LEAF: {
303 #if DEBUG_SCRIPTVM_CORE
304 _printIndents(frameIdx);
305 printf("-> STMT_LEAF\n");
306 #endif
307 LeafStatement* leaf = (LeafStatement*) frame.statement;
308 flags = leaf->exec();
309 ctx->popStack();
310 break;
311 }
312
313 case STMT_LIST: {
314 #if DEBUG_SCRIPTVM_CORE
315 _printIndents(frameIdx);
316 printf("-> STMT_LIST subidx=%d\n", frame.subindex);
317 #endif
318 Statements* stmts = (Statements*) frame.statement;
319 if (stmts->statement(frame.subindex)) {
320 ctx->pushStack(
321 stmts->statement(frame.subindex++)
322 );
323 } else {
324 #if DEBUG_SCRIPTVM_CORE
325 _printIndents(frameIdx);
326 printf("[END OF LIST] subidx=%d\n", frame.subindex);
327 #endif
328 ctx->popStack();
329 }
330 break;
331 }
332
333 case STMT_BRANCH: {
334 #if DEBUG_SCRIPTVM_CORE
335 _printIndents(frameIdx);
336 printf("-> STMT_BRANCH\n");
337 #endif
338 if (frame.subindex < 0) ctx->popStack();
339 else {
340 BranchStatement* branchStmt = (BranchStatement*) frame.statement;
341 frame.subindex = branchStmt->evalBranch();
342 if (frame.subindex >= 0) {
343 ctx->pushStack(
344 branchStmt->branch(frame.subindex)
345 );
346 frame.subindex = -1;
347 } else ctx->popStack();
348 }
349 break;
350 }
351
352 case STMT_LOOP: {
353 #if DEBUG_SCRIPTVM_CORE
354 _printIndents(frameIdx);
355 printf("-> STMT_LOOP\n");
356 #endif
357 While* whileStmt = (While*) frame.statement;
358 if (whileStmt->evalLoopStartCondition() && whileStmt->statements()) {
359 ctx->pushStack(
360 whileStmt->statements()
361 );
362 } else ctx->popStack();
363 }
364 }
365 }
366
367 if (flags & STMT_SUSPEND_SIGNALLED) {
368 ctx->status = VM_EXEC_SUSPENDED;
369 } else {
370 ctx->status = VM_EXEC_NOT_RUNNING;
371 if (flags & STMT_ERROR_OCCURRED)
372 ctx->status = VM_EXEC_ERROR;
373 ctx->reset();
374 }
375
376 m_eventHandler = NULL;
377 m_parserContext->execContext = NULL;
378 m_parserContext = NULL;
379 return ctx->status;
380 }
381
382 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC