1 |
/* -*- c++ -*- |
/* -*- c++ -*- |
2 |
* |
* |
3 |
* Copyright (c) 2014 - 2016 Christian Schoenebeck and Andreas Persson |
* Copyright (c) 2014 - 2019 Christian Schoenebeck and Andreas Persson |
4 |
* |
* |
5 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
6 |
* |
* |
20 |
#include <iostream> |
#include <iostream> |
21 |
#include <map> |
#include <map> |
22 |
#include <set> |
#include <set> |
23 |
|
#include <string.h> // for memset() |
24 |
#include "../common/global.h" |
#include "../common/global.h" |
25 |
#include "../common/Ref.h" |
#include "../common/Ref.h" |
26 |
#include "../common/ArrayList.h" |
#include "../common/ArrayList.h" |
36 |
STMT_LIST, |
STMT_LIST, |
37 |
STMT_BRANCH, |
STMT_BRANCH, |
38 |
STMT_LOOP, |
STMT_LOOP, |
39 |
|
STMT_SYNC, |
40 |
|
STMT_NOOP, |
41 |
}; |
}; |
42 |
|
|
43 |
class Node { |
class Node { |
66 |
}; |
}; |
67 |
typedef Ref<IntExpr,Node> IntExprRef; |
typedef Ref<IntExpr,Node> IntExprRef; |
68 |
|
|
69 |
|
class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression { |
70 |
|
public: |
71 |
|
ExprType_t exprType() const { return INT_ARR_EXPR; } |
72 |
|
String evalCastToStr(); |
73 |
|
}; |
74 |
|
typedef Ref<IntArrayExpr,Node> IntArrayExprRef; |
75 |
|
|
76 |
class StringExpr : virtual public VMStringExpr, virtual public Expression { |
class StringExpr : virtual public VMStringExpr, virtual public Expression { |
77 |
public: |
public: |
78 |
ExprType_t exprType() const { return STRING_EXPR; } |
ExprType_t exprType() const { return STRING_EXPR; } |
82 |
typedef Ref<StringExpr,Node> StringExprRef; |
typedef Ref<StringExpr,Node> StringExprRef; |
83 |
|
|
84 |
class IntLiteral : virtual public IntExpr { |
class IntLiteral : virtual public IntExpr { |
|
int value; |
|
85 |
public: |
public: |
86 |
|
int value; |
87 |
IntLiteral(int value) : value(value) { } |
IntLiteral(int value) : value(value) { } |
88 |
int evalInt(); |
int evalInt(); |
89 |
void dump(int level = 0); |
void dump(int level = 0); |
108 |
std::vector<ExpressionRef> args; |
std::vector<ExpressionRef> args; |
109 |
void add(ExpressionRef arg) { args.push_back(arg); } |
void add(ExpressionRef arg) { args.push_back(arg); } |
110 |
void dump(int level = 0); |
void dump(int level = 0); |
111 |
int argsCount() const { return args.size(); } |
int argsCount() const { return (int) args.size(); } |
112 |
VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; } |
VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; } |
113 |
bool isPolyphonic() const; |
bool isPolyphonic() const; |
114 |
}; |
}; |
161 |
public: |
public: |
162 |
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
163 |
bool isAssignable() const OVERRIDE { return !ptr->readonly; } |
bool isAssignable() const OVERRIDE { return !ptr->readonly; } |
164 |
void assign(Expression* expr); |
void assign(Expression* expr) OVERRIDE; |
165 |
int evalInt(); |
int evalInt() OVERRIDE; |
166 |
void dump(int level = 0); |
void dump(int level = 0) OVERRIDE; |
167 |
}; |
}; |
168 |
typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef; |
typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef; |
169 |
|
|
174 |
}; |
}; |
175 |
typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef; |
typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef; |
176 |
|
|
177 |
class IntArrayVariable : public Variable, virtual public VMIntArrayExpr { |
class IntArrayVariable : public Variable, virtual public IntArrayExpr { |
178 |
ArrayList<int> values; |
ArrayList<int> values; |
179 |
public: |
public: |
180 |
IntArrayVariable(ParserContext* ctx, int size); |
IntArrayVariable(ParserContext* ctx, int size); |
181 |
IntArrayVariable(ParserContext* ctx, int size, ArgsRef values); |
IntArrayVariable(ParserContext* ctx, int size, ArgsRef values, bool _bConst = false); |
182 |
void assign(Expression* expr) {} // ignore scalar assignment |
void assign(Expression* expr) {} // ignore scalar assignment |
183 |
String evalCastToStr() { return ""; } // ignore scalar cast to string |
String evalCastToStr() { return ""; } // ignore scalar cast to string |
184 |
ExprType_t exprType() const { return INT_ARR_EXPR; } |
ExprType_t exprType() const { return INT_ARR_EXPR; } |
197 |
VMInt8Array* array; |
VMInt8Array* array; |
198 |
public: |
public: |
199 |
BuiltInIntArrayVariable(const String& name, VMInt8Array* array); |
BuiltInIntArrayVariable(const String& name, VMInt8Array* array); |
200 |
int arraySize() const { return array->size; } |
int arraySize() const OVERRIDE { return array->size; } |
201 |
int evalIntElement(uint i); |
int evalIntElement(uint i) OVERRIDE; |
202 |
void assignIntElement(uint i, int value); |
bool isAssignable() const OVERRIDE { return !array->readonly; } |
203 |
void dump(int level = 0); |
void assignIntElement(uint i, int value) OVERRIDE; |
204 |
|
void dump(int level = 0) OVERRIDE; |
205 |
}; |
}; |
206 |
typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef; |
typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef; |
207 |
|
|
208 |
class IntArrayElement : public IntVariable { |
class IntArrayElement : public IntVariable { |
209 |
IntArrayVariableRef array; |
IntArrayExprRef array; |
210 |
IntExprRef index; |
IntExprRef index; |
211 |
public: |
public: |
212 |
IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex); |
IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex); |
213 |
void assign(Expression* expr); |
void assign(Expression* expr); |
214 |
int evalInt(); |
int evalInt(); |
215 |
void dump(int level = 0); |
void dump(int level = 0); |
301 |
class NoOperation : public Statement { |
class NoOperation : public Statement { |
302 |
public: |
public: |
303 |
NoOperation() : Statement() {} |
NoOperation() : Statement() {} |
304 |
StmtType_t statementType() const { return STMT_LEAF; } |
StmtType_t statementType() const { return STMT_NOOP; } |
305 |
void dump(int level = 0) {} |
void dump(int level = 0) {} |
306 |
bool isPolyphonic() const { return false; } |
bool isPolyphonic() const { return false; } |
307 |
}; |
}; |
334 |
virtual Statements* branch(uint i) const = 0; |
virtual Statements* branch(uint i) const = 0; |
335 |
}; |
}; |
336 |
|
|
337 |
class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr { |
class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr { |
338 |
VMDynVar* dynVar; |
VMDynVar* dynVar; |
339 |
String varName; |
String varName; |
340 |
public: |
public: |
344 |
bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); } |
bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); } |
345 |
bool isPolyphonic() const OVERRIDE { return false; } |
bool isPolyphonic() const OVERRIDE { return false; } |
346 |
void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); } |
void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); } |
347 |
|
VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); } |
348 |
int evalInt() OVERRIDE; |
int evalInt() OVERRIDE; |
349 |
String evalStr() OVERRIDE; |
String evalStr() OVERRIDE; |
350 |
String evalCastToStr() OVERRIDE; |
String evalCastToStr() OVERRIDE; |
351 |
|
int arraySize() const OVERRIDE { return dynVar->asIntArray()->arraySize(); } |
352 |
|
int evalIntElement(uint i) OVERRIDE { return dynVar->asIntArray()->evalIntElement(i); } |
353 |
|
void assignIntElement(uint i, int value) OVERRIDE { return dynVar->asIntArray()->assignIntElement(i, value); } |
354 |
void dump(int level = 0) OVERRIDE; |
void dump(int level = 0) OVERRIDE; |
355 |
}; |
}; |
356 |
typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef; |
typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef; |
362 |
public: |
public: |
363 |
FunctionCall(const char* function, ArgsRef args, VMFunction* fn) : |
FunctionCall(const char* function, ArgsRef args, VMFunction* fn) : |
364 |
functionName(function), args(args), fn(fn) { } |
functionName(function), args(args), fn(fn) { } |
365 |
void dump(int level = 0); |
void dump(int level = 0) OVERRIDE; |
366 |
StmtFlags_t exec(); |
StmtFlags_t exec() OVERRIDE; |
367 |
int evalInt(); |
int evalInt() OVERRIDE; |
368 |
String evalStr(); |
VMIntArrayExpr* asIntArray() const OVERRIDE; |
369 |
bool isConstExpr() const { return false; } |
String evalStr() OVERRIDE; |
370 |
ExprType_t exprType() const; |
bool isConstExpr() const OVERRIDE { return false; } |
371 |
String evalCastToStr(); |
ExprType_t exprType() const OVERRIDE; |
372 |
bool isPolyphonic() const { return args->isPolyphonic(); } |
String evalCastToStr() OVERRIDE; |
373 |
|
bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); } |
374 |
protected: |
protected: |
375 |
VMFnResult* execVMFn(); |
VMFnResult* execVMFn(); |
376 |
}; |
}; |
377 |
typedef Ref<FunctionCall,Node> FunctionCallRef; |
typedef Ref<FunctionCall,Node> FunctionCallRef; |
378 |
|
|
379 |
|
class NoFunctionCall : public FunctionCall { |
380 |
|
public: |
381 |
|
NoFunctionCall() : FunctionCall("nothing", new Args, NULL) {} |
382 |
|
StmtType_t statementType() const { return STMT_NOOP; } |
383 |
|
}; |
384 |
|
typedef Ref<NoFunctionCall,Node> NoFunctionCallRef; |
385 |
|
|
386 |
class EventHandler : virtual public Statements, virtual public VMEventHandler { |
class EventHandler : virtual public Statements, virtual public VMEventHandler { |
387 |
StatementsRef statements; |
StatementsRef statements; |
388 |
bool usingPolyphonics; |
bool usingPolyphonics; |
437 |
int evalInt() { return 0; } |
int evalInt() { return 0; } |
438 |
EventHandler* eventHandlerByName(const String& name) const; |
EventHandler* eventHandlerByName(const String& name) const; |
439 |
EventHandler* eventHandler(uint index) const; |
EventHandler* eventHandler(uint index) const; |
440 |
inline uint size() const { return args.size(); } |
inline uint size() const { return (int) args.size(); } |
441 |
bool isPolyphonic() const; |
bool isPolyphonic() const; |
442 |
}; |
}; |
443 |
typedef Ref<EventHandlers,Node> EventHandlersRef; |
typedef Ref<EventHandlers,Node> EventHandlersRef; |
507 |
bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); } |
bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); } |
508 |
}; |
}; |
509 |
|
|
510 |
|
class SyncBlock : public Statement { |
511 |
|
StatementsRef m_statements; |
512 |
|
public: |
513 |
|
SyncBlock(StatementsRef statements) : m_statements(statements) {} |
514 |
|
StmtType_t statementType() const { return STMT_SYNC; } |
515 |
|
void dump(int level = 0); |
516 |
|
Statements* statements() const; |
517 |
|
bool isPolyphonic() const { return m_statements->isPolyphonic(); } |
518 |
|
}; |
519 |
|
typedef Ref<SyncBlock,Node> SyncBlockRef; |
520 |
|
|
521 |
class Neg : public IntExpr { |
class Neg : public IntExpr { |
522 |
IntExprRef expr; |
IntExprRef expr; |
523 |
public: |
public: |
631 |
std::vector<ParserIssue> vErrors; |
std::vector<ParserIssue> vErrors; |
632 |
std::vector<ParserIssue> vWarnings; |
std::vector<ParserIssue> vWarnings; |
633 |
std::vector<ParserIssue> vIssues; |
std::vector<ParserIssue> vIssues; |
634 |
|
std::vector<CodeBlock> vPreprocessorComments; |
635 |
|
|
636 |
std::set<String> builtinPreprocessorConditions; |
std::set<String> builtinPreprocessorConditions; |
637 |
std::set<String> userPreprocessorConditions; |
std::set<String> userPreprocessorConditions; |
672 |
StatementsRef userFunctionByName(const String& name); |
StatementsRef userFunctionByName(const String& name); |
673 |
void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
674 |
void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
675 |
|
void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn); |
676 |
void createScanner(std::istream* is); |
void createScanner(std::istream* is); |
677 |
void destroyScanner(); |
void destroyScanner(); |
678 |
bool setPreprocessorCondition(const char* name); |
bool setPreprocessorCondition(const char* name); |
681 |
std::vector<ParserIssue> issues() const OVERRIDE; |
std::vector<ParserIssue> issues() const OVERRIDE; |
682 |
std::vector<ParserIssue> errors() const OVERRIDE; |
std::vector<ParserIssue> errors() const OVERRIDE; |
683 |
std::vector<ParserIssue> warnings() const OVERRIDE; |
std::vector<ParserIssue> warnings() const OVERRIDE; |
684 |
|
std::vector<CodeBlock> preprocessorComments() const OVERRIDE; |
685 |
VMEventHandler* eventHandler(uint index) OVERRIDE; |
VMEventHandler* eventHandler(uint index) OVERRIDE; |
686 |
VMEventHandler* eventHandlerByName(const String& name) OVERRIDE; |
VMEventHandler* eventHandlerByName(const String& name) OVERRIDE; |
687 |
void registerBuiltInConstIntVariables(const std::map<String,int>& vars); |
void registerBuiltInConstIntVariables(const std::map<String,int>& vars); |
704 |
|
|
705 |
ArrayList<int> polyphonicIntMemory; |
ArrayList<int> polyphonicIntMemory; |
706 |
VMExecStatus_t status; |
VMExecStatus_t status; |
707 |
|
StmtFlags_t flags; |
708 |
ArrayList<StackFrame> stack; |
ArrayList<StackFrame> stack; |
709 |
int stackFrame; |
int stackFrame; |
710 |
int suspendMicroseconds; |
int suspendMicroseconds; |
711 |
|
size_t instructionsCount; |
712 |
|
struct ExitRes { |
713 |
|
Expression* value; |
714 |
|
IntLiteral intLiteral; |
715 |
|
StringLiteral stringLiteral; |
716 |
|
|
717 |
ExecContext() : |
ExitRes() : intLiteral(0), stringLiteral("") { } |
718 |
status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {} |
} exitRes; |
719 |
|
|
720 |
|
ExecContext(); |
721 |
virtual ~ExecContext() {} |
virtual ~ExecContext() {} |
722 |
|
|
723 |
inline void pushStack(Statement* stmt) { |
inline void pushStack(Statement* stmt) { |
739 |
stack[0].statement = NULL; |
stack[0].statement = NULL; |
740 |
stack[0].subindex = -1; |
stack[0].subindex = -1; |
741 |
stackFrame = -1; |
stackFrame = -1; |
742 |
|
flags = STMT_SUCCESS; |
743 |
|
} |
744 |
|
|
745 |
|
inline void clearExitRes() { |
746 |
|
exitRes.value = NULL; |
747 |
} |
} |
748 |
|
|
749 |
int suspensionTimeMicroseconds() const OVERRIDE { |
int suspensionTimeMicroseconds() const OVERRIDE { |
750 |
return suspendMicroseconds; |
return suspendMicroseconds; |
751 |
} |
} |
752 |
|
|
753 |
|
void resetPolyphonicData() OVERRIDE { |
754 |
|
if (polyphonicIntMemory.empty()) return; |
755 |
|
memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int)); |
756 |
|
} |
757 |
|
|
758 |
|
size_t instructionsPerformed() const OVERRIDE { |
759 |
|
return instructionsCount; |
760 |
|
} |
761 |
|
|
762 |
|
void signalAbort() OVERRIDE { |
763 |
|
flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED); |
764 |
|
} |
765 |
|
|
766 |
|
void forkTo(VMExecContext* ectx) const OVERRIDE; |
767 |
|
|
768 |
|
VMExpr* exitResult() OVERRIDE { |
769 |
|
return exitRes.value; |
770 |
|
} |
771 |
}; |
}; |
772 |
|
|
773 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |