1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
9 |
|
|
10 |
#include "ScriptVM.h" |
#include "ScriptVM.h" |
11 |
|
|
12 |
|
#include <string.h> |
13 |
|
#include <assert.h> |
14 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
15 |
#include "tree.h" |
#include "tree.h" |
16 |
|
#include "CoreVMFunctions.h" |
17 |
|
#include "CoreVMDynVars.h" |
18 |
|
#include "editor/NkspScanner.h" |
19 |
|
|
20 |
#define DEBUG_SCRIPTVM_CORE 0 |
#define DEBUG_SCRIPTVM_CORE 0 |
21 |
|
|
92 |
return max; |
return max; |
93 |
} |
} |
94 |
|
|
95 |
ScriptVM::ScriptVM() : fnWait(this) { |
ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) { |
96 |
m_context = new ParserContext(this); |
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 |
|
m_fnMin = new CoreVMFunction_min; |
109 |
|
m_fnMax = new CoreVMFunction_max; |
110 |
} |
} |
111 |
|
|
112 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
113 |
if (m_context) { |
delete m_fnMessage; |
114 |
if (m_context->globalIntMemory) { |
delete m_fnExit; |
115 |
delete m_context->globalIntMemory; |
delete m_fnWait; |
116 |
m_context->globalIntMemory = NULL; |
delete m_fnAbs; |
117 |
} |
delete m_fnRandom; |
118 |
delete m_context; |
delete m_fnNumElements; |
119 |
} |
delete m_fnInc; |
120 |
|
delete m_fnDec; |
121 |
|
delete m_fnShLeft; |
122 |
|
delete m_fnShRight; |
123 |
|
delete m_fnMin; |
124 |
|
delete m_fnMax; |
125 |
|
delete m_varRealTimer; |
126 |
|
delete m_varPerfTimer; |
127 |
} |
} |
128 |
|
|
129 |
void ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(const String& s) { |
130 |
std::istringstream iss(s); |
std::istringstream iss(s); |
131 |
loadScript(&iss); |
return loadScript(&iss); |
132 |
} |
} |
133 |
|
|
134 |
void ScriptVM::loadScript(std::istream* is) { |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
135 |
m_context->createScanner(is); |
ParserContext* context = new ParserContext(this); |
136 |
InstrScript_parse(m_context); |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
137 |
std::cout << "Allocating " << m_context->globalIntVarCount * sizeof(int) << " bytes of global int VM memory.\n"; |
|
138 |
std::cout << "Allocating " << m_context->globalStrVarCount << " of global VM string variables.\n"; |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
139 |
if (!m_context->globalIntMemory) |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
140 |
m_context->globalIntMemory = new ArrayList<int>(); |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
141 |
if (!m_context->globalStrMemory) |
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
142 |
m_context->globalStrMemory = new ArrayList<String>(); |
|
143 |
m_context->globalIntMemory->resize(m_context->globalIntVarCount); |
context->createScanner(is); |
144 |
m_context->globalStrMemory->resize(m_context->globalStrVarCount); |
|
145 |
} |
InstrScript_parse(context); |
146 |
|
dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int)))); |
147 |
|
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
148 |
|
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 |
|
|
157 |
std::vector<ParserIssue> ScriptVM::issues() const { |
context->destroyScanner(); |
|
return m_context->issues; |
|
|
} |
|
158 |
|
|
159 |
std::vector<ParserIssue> ScriptVM::errors() const { |
return context; |
|
return m_context->errors; |
|
160 |
} |
} |
161 |
|
|
162 |
std::vector<ParserIssue> ScriptVM::warnings() const { |
void ScriptVM::dumpParsedScript(VMParserContext* context) { |
163 |
return m_context->warnings; |
ParserContext* ctx = dynamic_cast<ParserContext*>(context); |
164 |
} |
if (!ctx) { |
|
|
|
|
void ScriptVM::dumpParsedScript() { |
|
|
if (!m_context) { |
|
165 |
std::cerr << "No VM context. So nothing to dump.\n"; |
std::cerr << "No VM context. So nothing to dump.\n"; |
166 |
return; |
return; |
167 |
} |
} |
168 |
if (!m_context->handlers) { |
if (!ctx->handlers) { |
169 |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
170 |
return; |
return; |
171 |
} |
} |
172 |
if (!m_context->globalIntMemory) { |
if (!ctx->globalIntMemory) { |
173 |
std::cerr << "Internal error: no global memory assigend to script VM.\n"; |
std::cerr << "Internal error: no global memory assigend to script VM.\n"; |
174 |
return; |
return; |
175 |
} |
} |
176 |
m_context->handlers->dump(); |
ctx->handlers->dump(); |
177 |
} |
} |
178 |
|
|
179 |
VMExecContext* ScriptVM::createExecContext() { |
VMExecContext* ScriptVM::createExecContext(VMParserContext* parserContext) { |
180 |
ExecContext* ctx = new ExecContext(); |
ParserContext* parserCtx = dynamic_cast<ParserContext*>(parserContext); |
181 |
const int stackSize = _requiredMaxStackSizeFor(&*m_context->handlers); |
ExecContext* execCtx = new ExecContext(); |
182 |
ctx->stack.resize(stackSize); |
|
183 |
std::cout << "Created VM exec context with " |
if (parserCtx->requiredMaxStackSize < 0) { |
184 |
<< stackSize * sizeof(ExecContext::StackFrame) |
parserCtx->requiredMaxStackSize = |
185 |
<< " bytes VM stack size.\n"; |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
186 |
const int polySize = m_context->polyphonicIntVarCount; |
} |
187 |
ctx->polyphonicIntMemory.resize(polySize); |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
188 |
std::cout << "Allocated " << polySize * sizeof(int) |
dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", |
189 |
<< " bytes polyphonic memory.\n"; |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
190 |
return ctx; |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
191 |
} |
const int polySize = parserCtx->polyphonicIntVarCount; |
192 |
|
execCtx->polyphonicIntMemory.resize(polySize); |
193 |
VMEventHandler* ScriptVM::eventHandler(uint index) { |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
194 |
if (!m_context) return NULL; |
|
195 |
if (!m_context->handlers) return NULL; |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
196 |
return m_context->handlers->eventHandler(index); |
return execCtx; |
197 |
} |
} |
198 |
|
|
199 |
VMEventHandler* ScriptVM::eventHandlerByName(const String& name) { |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
200 |
if (!m_context) return NULL; |
std::istringstream iss(s); |
201 |
if (!m_context->handlers) return NULL; |
return syntaxHighlighting(&iss); |
202 |
return m_context->handlers->eventHandlerByName(name); |
} |
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 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
218 |
if (name == "message") return &fnMessage; |
if (name == "message") return m_fnMessage; |
219 |
else if (name == "exit") return &fnExit; |
else if (name == "exit") return m_fnExit; |
220 |
else if (name == "wait") return &fnWait; |
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 |
|
else if (name == "inc") return m_fnInc; |
225 |
|
else if (name == "dec") return m_fnDec; |
226 |
|
else if (name == "sh_left") return m_fnShLeft; |
227 |
|
else if (name == "sh_right") return m_fnShRight; |
228 |
|
else if (name == "min") return m_fnMin; |
229 |
|
else if (name == "max") return m_fnMax; |
230 |
return NULL; |
return NULL; |
231 |
} |
} |
232 |
|
|
233 |
|
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 |
|
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 |
|
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
252 |
|
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 |
|
} |
261 |
|
|
262 |
|
VMEventHandler* ScriptVM::currentVMEventHandler() { |
263 |
|
return m_eventHandler; |
264 |
|
} |
265 |
|
|
266 |
|
VMParserContext* ScriptVM::currentVMParserContext() { |
267 |
|
return m_parserContext; |
268 |
|
} |
269 |
|
|
270 |
VMExecContext* ScriptVM::currentVMExecContext() { |
VMExecContext* ScriptVM::currentVMExecContext() { |
271 |
if (!m_context) return NULL; |
if (!m_parserContext) return NULL; |
272 |
return m_context->execContext; |
return m_parserContext->execContext; |
273 |
} |
} |
274 |
|
|
275 |
VMExecStatus_t ScriptVM::exec(VMEventHandler* handler, VMExecContext* execContex) { |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
276 |
if (!m_context) { |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
277 |
std::cerr << "No VM parser context. Did you load a script?.\n"; |
if (!m_parserContext) { |
278 |
|
std::cerr << "No VM parser context provided. Did you load a script?.\n"; |
279 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
280 |
} |
} |
281 |
|
|
282 |
|
// a ParserContext object is always tied to exactly one ScriptVM object |
283 |
|
assert(m_parserContext->functionProvider == this); |
284 |
|
|
285 |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
ExecContext* ctx = dynamic_cast<ExecContext*>(execContex); |
286 |
if (!ctx) { |
if (!ctx) { |
287 |
std::cerr << "Invalid VM exec context.\n"; |
std::cerr << "Invalid VM exec context.\n"; |
289 |
} |
} |
290 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
291 |
if (!h) return VM_EXEC_NOT_RUNNING; |
if (!h) return VM_EXEC_NOT_RUNNING; |
292 |
|
m_eventHandler = handler; |
293 |
|
|
294 |
m_context->execContext = ctx; |
m_parserContext->execContext = ctx; |
295 |
|
|
296 |
ctx->status = VM_EXEC_RUNNING; |
ctx->status = VM_EXEC_RUNNING; |
297 |
StmtFlags_t flags = STMT_SUCCESS; |
StmtFlags_t flags = STMT_SUCCESS; |
385 |
ctx->reset(); |
ctx->reset(); |
386 |
} |
} |
387 |
|
|
388 |
m_context->execContext = NULL; |
m_eventHandler = NULL; |
389 |
|
m_parserContext->execContext = NULL; |
390 |
|
m_parserContext = NULL; |
391 |
return ctx->status; |
return ctx->status; |
392 |
} |
} |
393 |
|
|