/[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 2965 - (show annotations) (download)
Mon Jul 18 09:42:28 2016 UTC (7 years, 9 months ago) by schoenebeck
File size: 14083 byte(s)
* NKSP: Implemented built-in script function "sh_left()".
* NKSP: Implemented built-in script function "sh_right()".
* Bumped version (2.0.0.svn22).

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

  ViewVC Help
Powered by ViewVC