1 |
/* -*- c++ -*- |
/* -*- c++ -*- |
2 |
* |
* |
3 |
* Copyright (c) 2014 - 2016 Christian Schoenebeck and Andreas Persson |
* Copyright (c) 2014 - 2017 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; } |
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 |
}; |
}; |
115 |
typedef Ref<Args,Node> ArgsRef; |
typedef Ref<Args,Node> ArgsRef; |
116 |
|
|
117 |
class Variable : virtual public Expression { |
class Variable : virtual public VMVariable, virtual public Expression { |
118 |
public: |
public: |
119 |
virtual bool isConstExpr() const { return bConst; } |
bool isConstExpr() const OVERRIDE { return bConst; } |
120 |
|
bool isAssignable() const OVERRIDE { return !bConst; } |
121 |
virtual void assign(Expression* expr) = 0; |
virtual void assign(Expression* expr) = 0; |
122 |
|
void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); } |
123 |
protected: |
protected: |
124 |
Variable(ParserContext* ctx, int _memPos, bool _bConst) |
Variable(ParserContext* ctx, int _memPos, bool _bConst) |
125 |
: context(ctx), memPos(_memPos), bConst(_bConst) {} |
: context(ctx), memPos(_memPos), bConst(_bConst) {} |
160 |
VMIntRelPtr* ptr; |
VMIntRelPtr* ptr; |
161 |
public: |
public: |
162 |
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
163 |
void assign(Expression* expr); |
bool isAssignable() const OVERRIDE { return !ptr->readonly; } |
164 |
int evalInt(); |
void assign(Expression* expr) OVERRIDE; |
165 |
void dump(int level = 0); |
int evalInt() OVERRIDE; |
166 |
|
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; } |
199 |
BuiltInIntArrayVariable(const String& name, VMInt8Array* array); |
BuiltInIntArrayVariable(const String& name, VMInt8Array* array); |
200 |
int arraySize() const { return array->size; } |
int arraySize() const { return array->size; } |
201 |
int evalIntElement(uint i); |
int evalIntElement(uint i); |
202 |
|
bool isAssignable() const OVERRIDE { return !array->readonly; } |
203 |
void assignIntElement(uint i, int value); |
void assignIntElement(uint i, int value); |
204 |
void dump(int level = 0); |
void dump(int level = 0); |
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, virtual public IntArrayExpr { |
338 |
|
VMDynVar* dynVar; |
339 |
|
String varName; |
340 |
|
public: |
341 |
|
DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v); |
342 |
|
ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); } |
343 |
|
bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); } |
344 |
|
bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); } |
345 |
|
bool isPolyphonic() const OVERRIDE { return false; } |
346 |
|
void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); } |
347 |
|
VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); } |
348 |
|
int evalInt() OVERRIDE; |
349 |
|
String evalStr() OVERRIDE; |
350 |
|
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) { return dynVar->asIntArray()->assignIntElement(i, value); } |
354 |
|
void dump(int level = 0) OVERRIDE; |
355 |
|
}; |
356 |
|
typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef; |
357 |
|
|
358 |
class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr { |
class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr { |
359 |
String functionName; |
String functionName; |
360 |
ArgsRef args; |
ArgsRef args; |
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: |
572 |
}; |
}; |
573 |
typedef Ref<Or,Node> OrRef; |
typedef Ref<Or,Node> OrRef; |
574 |
|
|
575 |
|
class BitwiseOr : virtual public BinaryOp, virtual public IntExpr { |
576 |
|
public: |
577 |
|
BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
578 |
|
int evalInt(); |
579 |
|
void dump(int level = 0); |
580 |
|
}; |
581 |
|
typedef Ref<BitwiseOr,Node> BitwiseOrRef; |
582 |
|
|
583 |
class And : virtual public BinaryOp, virtual public IntExpr { |
class And : virtual public BinaryOp, virtual public IntExpr { |
584 |
public: |
public: |
585 |
And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
588 |
}; |
}; |
589 |
typedef Ref<And,Node> AndRef; |
typedef Ref<And,Node> AndRef; |
590 |
|
|
591 |
|
class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr { |
592 |
|
public: |
593 |
|
BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
594 |
|
int evalInt(); |
595 |
|
void dump(int level = 0); |
596 |
|
}; |
597 |
|
typedef Ref<BitwiseAnd,Node> BitwiseAndRef; |
598 |
|
|
599 |
class Not : virtual public IntExpr { |
class Not : virtual public IntExpr { |
600 |
IntExprRef expr; |
IntExprRef expr; |
601 |
public: |
public: |
607 |
}; |
}; |
608 |
typedef Ref<Not,Node> NotRef; |
typedef Ref<Not,Node> NotRef; |
609 |
|
|
610 |
|
class BitwiseNot : virtual public IntExpr { |
611 |
|
IntExprRef expr; |
612 |
|
public: |
613 |
|
BitwiseNot(IntExprRef expr) : expr(expr) {} |
614 |
|
int evalInt() { return ~expr->evalInt(); } |
615 |
|
void dump(int level = 0); |
616 |
|
bool isConstExpr() const { return expr->isConstExpr(); } |
617 |
|
bool isPolyphonic() const { return expr->isPolyphonic(); } |
618 |
|
}; |
619 |
|
typedef Ref<BitwiseNot,Node> BitwiseNotRef; |
620 |
|
|
621 |
class ParserContext : public VMParserContext { |
class ParserContext : public VMParserContext { |
622 |
public: |
public: |
623 |
struct Error { |
struct Error { |
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; |
638 |
|
|
639 |
std::map<String,VariableRef> vartable; |
std::map<String,VariableRef> vartable; |
640 |
|
std::map<String,StatementsRef> userFnTable; |
641 |
int globalIntVarCount; |
int globalIntVarCount; |
642 |
int globalStrVarCount; |
int globalStrVarCount; |
643 |
int polyphonicIntVarCount; |
int polyphonicIntVarCount; |
669 |
IntVariableRef globalIntVar(const String& name); |
IntVariableRef globalIntVar(const String& name); |
670 |
StringVariableRef globalStrVar(const String& name); |
StringVariableRef globalStrVar(const String& name); |
671 |
VariableRef variableByName(const String& name); |
VariableRef variableByName(const String& name); |
672 |
void addErr(int line, const char* txt); |
StatementsRef userFunctionByName(const String& name); |
673 |
void addWrn(int line, 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); |
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); |
688 |
void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars); |
void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars); |
689 |
void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars); |
void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars); |
690 |
|
void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars); |
691 |
}; |
}; |
692 |
|
|
693 |
class ExecContext : public VMExecContext { |
class ExecContext : public VMExecContext { |
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 |
|
|
713 |
ExecContext() : |
ExecContext() : |
714 |
status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {} |
status(VM_EXEC_NOT_RUNNING), flags(STMT_SUCCESS), stackFrame(-1), |
715 |
|
suspendMicroseconds(0), instructionsCount(0) {} |
716 |
|
|
717 |
virtual ~ExecContext() {} |
virtual ~ExecContext() {} |
718 |
|
|
735 |
stack[0].statement = NULL; |
stack[0].statement = NULL; |
736 |
stack[0].subindex = -1; |
stack[0].subindex = -1; |
737 |
stackFrame = -1; |
stackFrame = -1; |
738 |
|
flags = STMT_SUCCESS; |
739 |
} |
} |
740 |
|
|
741 |
int suspensionTimeMicroseconds() const OVERRIDE { |
int suspensionTimeMicroseconds() const OVERRIDE { |
742 |
return suspendMicroseconds; |
return suspendMicroseconds; |
743 |
} |
} |
744 |
|
|
745 |
|
void resetPolyphonicData() OVERRIDE { |
746 |
|
if (polyphonicIntMemory.empty()) return; |
747 |
|
memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int)); |
748 |
|
} |
749 |
|
|
750 |
|
size_t instructionsPerformed() const OVERRIDE { |
751 |
|
return instructionsCount; |
752 |
|
} |
753 |
|
|
754 |
|
void signalAbort() OVERRIDE { |
755 |
|
flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED); |
756 |
|
} |
757 |
|
|
758 |
|
void forkTo(VMExecContext* ectx) const OVERRIDE; |
759 |
}; |
}; |
760 |
|
|
761 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |