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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2970 - (hide annotations) (download)
Thu Jul 21 16:22:55 2016 UTC (7 years, 9 months ago) by schoenebeck
File size: 14311 byte(s)
* NKSP: Implemented built-in script function "min()".
* NKSP: Implemented built-in script function "max()".
* Bumped version (2.0.0.svn23).

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

  ViewVC Help
Powered by ViewVC