/[svn]/linuxsampler/trunk/src/scriptvm/tree.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/tree.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2645 - (show annotations) (download) (as text)
Wed Jun 18 00:14:57 2014 UTC (5 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 19797 byte(s)
* RT instrument scripts: Pass/preserve polyphonic variable data
  from respective "note" event handler to "release" event handler.
* Fixed theoretical memory leak regarding instrument scripts.
* Bumped version (1.0.0.svn54).

1 /* -*- c++ -*-
2 *
3 * Copyright (c) 2014 Christian Schoenebeck and Andreas Persson
4 *
5 * http://www.linuxsampler.org
6 *
7 * This file is part of LinuxSampler and released under the same terms.
8 * See README file for details.
9 */
10
11 // This header defines VM core implementation internal data types only used
12 // inside the parser and core VM implementation of this source directory. Not
13 // intended to be used in other source code parts (other source code
14 // directories) of the sampler.
15
16 #ifndef LS_INSTRPARSERTREE_H
17 #define LS_INSTRPARSERTREE_H
18
19 #include <vector>
20 #include <iostream>
21 #include <map>
22 #include <set>
23 #include "../common/global.h"
24 #include "../common/Ref.h"
25 #include "../common/ArrayList.h"
26 #include "common.h"
27
28 namespace LinuxSampler {
29
30 class ParserContext;
31 class ExecContext;
32
33 enum StmtType_t {
34 STMT_LEAF,
35 STMT_LIST,
36 STMT_BRANCH,
37 STMT_LOOP,
38 };
39
40 class Node {
41 public:
42 Node();
43 virtual ~Node();
44 virtual void dump(int level = 0) = 0;
45 virtual bool isPolyphonic() const = 0;
46 void printIndents(int n);
47 };
48 typedef Ref<Node> NodeRef;
49
50 class Expression : virtual public VMExpr, virtual public Node {
51 public:
52 virtual ExprType_t exprType() const = 0;
53 virtual bool isConstExpr() const = 0;
54 virtual String evalCastToStr() = 0;
55 };
56 typedef Ref<Expression,Node> ExpressionRef;
57
58 class IntExpr : virtual public VMIntExpr, virtual public Expression {
59 public:
60 ExprType_t exprType() const { return INT_EXPR; }
61 virtual int evalInt() = 0;
62 String evalCastToStr();
63 };
64 typedef Ref<IntExpr,Node> IntExprRef;
65
66 class StringExpr : virtual public VMStringExpr, virtual public Expression {
67 public:
68 ExprType_t exprType() const { return STRING_EXPR; }
69 virtual String evalStr() = 0;
70 String evalCastToStr() { return evalStr(); }
71 };
72 typedef Ref<StringExpr,Node> StringExprRef;
73
74 class IntLiteral : virtual public IntExpr {
75 int value;
76 public:
77 IntLiteral(int value) : value(value) { }
78 int evalInt();
79 void dump(int level = 0);
80 bool isConstExpr() const { return true; }
81 bool isPolyphonic() const { return false; }
82 };
83 typedef Ref<IntLiteral,Node> IntLiteralRef;
84
85 class StringLiteral : virtual public StringExpr {
86 public:
87 String value;
88 StringLiteral(const String& value) : value(value) { }
89 bool isConstExpr() const { return true; }
90 void dump(int level = 0);
91 String evalStr() { return value; }
92 bool isPolyphonic() const { return false; }
93 };
94 typedef Ref<StringLiteral,Node> StringLiteralRef;
95
96 class Args : virtual public VMFnArgs, virtual public Node {
97 public:
98 std::vector<ExpressionRef> args;
99 void add(ExpressionRef arg) { args.push_back(arg); }
100 void dump(int level = 0);
101 int argsCount() const { return args.size(); }
102 VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; }
103 bool isPolyphonic() const;
104 };
105 typedef Ref<Args,Node> ArgsRef;
106
107 class Variable : virtual public Expression {
108 public:
109 virtual bool isConstExpr() const { return bConst; }
110 virtual void assign(Expression* expr) = 0;
111 protected:
112 Variable(ParserContext* ctx, int _memPos, bool _bConst)
113 : context(ctx), memPos(_memPos), bConst(_bConst) {}
114
115 ParserContext* context;
116 int memPos;
117 bool bConst;
118 };
119 typedef Ref<Variable,Node> VariableRef;
120
121 class IntVariable : public Variable, virtual public IntExpr {
122 bool polyphonic;
123 public:
124 IntVariable(ParserContext* ctx);
125 void assign(Expression* expr);
126 int evalInt();
127 void dump(int level = 0);
128 bool isPolyphonic() const { return polyphonic; }
129 protected:
130 IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size = 1);
131 };
132 typedef Ref<IntVariable,Node> IntVariableRef;
133
134 class ConstIntVariable : public IntVariable {
135 public:
136 int value;
137
138 ConstIntVariable(int value);
139 //ConstIntVariable(ParserContext* ctx, int value = 0);
140 void assign(Expression* expr);
141 int evalInt();
142 void dump(int level = 0);
143 };
144 typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
145
146 class BuiltInIntVariable : public IntVariable {
147 String name;
148 VMIntRelPtr* ptr;
149 public:
150 BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
151 void assign(Expression* expr);
152 int evalInt();
153 void dump(int level = 0);
154 };
155 typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
156
157 class PolyphonicIntVariable : public IntVariable {
158 public:
159 PolyphonicIntVariable(ParserContext* ctx);
160 void dump(int level = 0);
161 };
162 typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
163
164 class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {
165 ArrayList<int> values;
166 public:
167 IntArrayVariable(ParserContext* ctx, int size);
168 IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);
169 void assign(Expression* expr) {} // ignore scalar assignment
170 String evalCastToStr() { return ""; } // ignore scalar cast to string
171 ExprType_t exprType() const { return INT_ARR_EXPR; }
172 virtual int arraySize() const { return values.size(); }
173 virtual int evalIntElement(uint i);
174 virtual void assignIntElement(uint i, int value);
175 void dump(int level = 0);
176 bool isPolyphonic() const { return false; }
177 protected:
178 IntArrayVariable(ParserContext* ctx, bool bConst);
179 };
180 typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;
181
182 class BuiltInIntArrayVariable : public IntArrayVariable {
183 String name;
184 VMInt8Array* array;
185 public:
186 BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
187 int arraySize() const { return array->size; }
188 int evalIntElement(uint i);
189 void assignIntElement(uint i, int value);
190 void dump(int level = 0);
191 };
192 typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
193
194 class IntArrayElement : public IntVariable {
195 IntArrayVariableRef array;
196 IntExprRef index;
197 public:
198 IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex);
199 void assign(Expression* expr);
200 int evalInt();
201 void dump(int level = 0);
202 };
203 typedef Ref<IntArrayElement,Node> IntArrayElementRef;
204
205 class StringVariable : public Variable, virtual public StringExpr {
206 public:
207 StringVariable(ParserContext* ctx);
208 void assign(Expression* expr);
209 String evalStr();
210 void dump(int level = 0);
211 bool isPolyphonic() const { return false; }
212 protected:
213 StringVariable(ParserContext* ctx, bool bConst);
214 };
215 typedef Ref<StringVariable,Node> StringVariableRef;
216
217 class ConstStringVariable : public StringVariable {
218 public:
219 String value;
220
221 ConstStringVariable(ParserContext* ctx, String value = "");
222 void assign(Expression* expr);
223 String evalStr();
224 void dump(int level = 0);
225 };
226 typedef Ref<ConstStringVariable,Node> ConstStringVariableRef;
227
228 class BinaryOp : virtual public Expression {
229 protected:
230 ExpressionRef lhs;
231 ExpressionRef rhs;
232 public:
233 BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
234 bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }
235 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
236 };
237 typedef Ref<BinaryOp,Node> BinaryOpRef;
238
239 class Add : virtual public BinaryOp, virtual public IntExpr {
240 public:
241 Add(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
242 int evalInt();
243 void dump(int level = 0);
244 };
245 typedef Ref<Add,Node> AddRef;
246
247 class Sub : virtual public BinaryOp, virtual public IntExpr {
248 public:
249 Sub(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
250 int evalInt();
251 void dump(int level = 0);
252 };
253 typedef Ref<Sub,Node> SubRef;
254
255 class Mul : virtual public BinaryOp, virtual public IntExpr {
256 public:
257 Mul(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
258 int evalInt();
259 void dump(int level = 0);
260 };
261 typedef Ref<Mul,Node> MulRef;
262
263 class Div : virtual public BinaryOp, virtual public IntExpr {
264 public:
265 Div(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
266 int evalInt();
267 void dump(int level = 0);
268 };
269 typedef Ref<Div,Node> DivRef;
270
271 class Mod : virtual public BinaryOp, virtual public IntExpr {
272 public:
273 Mod(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
274 int evalInt();
275 void dump(int level = 0);
276 };
277 typedef Ref<Mod,Node> ModRef;
278
279 class Statement : virtual public Node {
280 public:
281 virtual StmtType_t statementType() const = 0;
282 };
283 typedef Ref<Statement,Node> StatementRef;
284
285 // Just used by parser to avoid "not a statement" parser warning, will be
286 // filtered out by parser. So it will not be part of the VM tree after parsing.
287 class NoOperation : public Statement {
288 public:
289 NoOperation() : Statement() {}
290 StmtType_t statementType() const { return STMT_LEAF; }
291 void dump(int level = 0) {}
292 bool isPolyphonic() const { return false; }
293 };
294 typedef Ref<NoOperation,Node> NoOperationRef;
295
296 bool isNoOperation(StatementRef statement);
297
298 class LeafStatement : public Statement {
299 public:
300 virtual StmtFlags_t exec() = 0;
301 virtual StmtType_t statementType() const { return STMT_LEAF; }
302 };
303 typedef Ref<LeafStatement,Node> LeafStatementRef;
304
305 class Statements : public Statement {
306 std::vector<StatementRef> args;
307 public:
308 void add(StatementRef arg) { args.push_back(arg); }
309 void dump(int level = 0);
310 StmtType_t statementType() const { return STMT_LIST; }
311 virtual Statement* statement(uint i);
312 bool isPolyphonic() const;
313 };
314 typedef Ref<Statements,Node> StatementsRef;
315
316 class BranchStatement : public Statement {
317 public:
318 StmtType_t statementType() const { return STMT_BRANCH; }
319 virtual int evalBranch() = 0;
320 virtual Statements* branch(uint i) const = 0;
321 };
322
323 class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
324 String functionName;
325 ArgsRef args;
326 VMFunction* fn;
327 public:
328 FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
329 functionName(function), args(args), fn(fn) { }
330 void dump(int level = 0);
331 StmtFlags_t exec();
332 int evalInt();
333 String evalStr();
334 bool isConstExpr() const { return false; }
335 ExprType_t exprType() const;
336 String evalCastToStr();
337 bool isPolyphonic() const { return args->isPolyphonic(); }
338 protected:
339 VMFnResult* execVMFn();
340 };
341 typedef Ref<FunctionCall,Node> FunctionCallRef;
342
343 class EventHandler : virtual public Statements, virtual public VMEventHandler {
344 StatementsRef statements;
345 bool usingPolyphonics;
346 public:
347 void dump(int level = 0);
348 StmtFlags_t exec();
349 EventHandler(StatementsRef statements);
350 Statement* statement(uint i) { return statements->statement(i); }
351 bool isPolyphonic() const { return usingPolyphonics; }
352 };
353 typedef Ref<EventHandler,Node> EventHandlerRef;
354
355 class OnNote : public EventHandler {
356 public:
357 OnNote(StatementsRef statements) : EventHandler(statements) {}
358 String eventHandlerName() const { return "note"; }
359 };
360 typedef Ref<OnNote,Node> OnNoteRef;
361
362 class OnInit : public EventHandler {
363 public:
364 OnInit(StatementsRef statements) : EventHandler(statements) {}
365 String eventHandlerName() const { return "init"; }
366 };
367 typedef Ref<OnInit,Node> OnInitRef;
368
369 class OnRelease : public EventHandler {
370 public:
371 OnRelease(StatementsRef statements) : EventHandler(statements) {}
372 String eventHandlerName() const { return "release"; }
373 };
374 typedef Ref<OnRelease,Node> OnReleaseRef;
375
376 class OnController : public EventHandler {
377 public:
378 OnController(StatementsRef statements) : EventHandler(statements) {}
379 String eventHandlerName() const { return "controller"; }
380 };
381 typedef Ref<OnController,Node> OnControllerRef;
382
383 class EventHandlers : virtual public Node {
384 std::vector<EventHandlerRef> args;
385 public:
386 EventHandlers();
387 ~EventHandlers();
388 void add(EventHandlerRef arg);
389 void dump(int level = 0);
390 int evalInt() { return 0; }
391 EventHandler* eventHandlerByName(const String& name) const;
392 EventHandler* eventHandler(uint index) const;
393 inline uint size() const { return args.size(); }
394 bool isPolyphonic() const;
395 };
396 typedef Ref<EventHandlers,Node> EventHandlersRef;
397
398 class Assignment : public LeafStatement {
399 protected:
400 VariableRef variable;
401 ExpressionRef value;
402 public:
403 Assignment(VariableRef variable, ExpressionRef value);
404 void dump(int level = 0);
405 StmtFlags_t exec();
406 bool isPolyphonic() const { return variable->isPolyphonic() || value->isPolyphonic(); }
407 };
408 typedef Ref<Assignment,Node> AssignmentRef;
409
410 class If : public BranchStatement {
411 IntExprRef condition;
412 StatementsRef ifStatements;
413 StatementsRef elseStatements;
414 public:
415 If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) :
416 condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }
417 If(IntExprRef condition, StatementsRef statements) :
418 condition(condition), ifStatements(statements) { }
419 void dump(int level = 0);
420 int evalBranch();
421 Statements* branch(uint i) const;
422 bool isPolyphonic() const;
423 };
424 typedef Ref<If,Node> IfRef;
425
426 struct CaseBranch {
427 IntExprRef from;
428 IntExprRef to;
429 StatementsRef statements;
430 };
431
432 typedef std::vector<CaseBranch> CaseBranches;
433
434 class SelectCase : public BranchStatement {
435 IntExprRef select;
436 CaseBranches branches;
437 public:
438 SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }
439 void dump(int level = 0);
440 int evalBranch();
441 Statements* branch(uint i) const;
442 //void addBranch(IntExprRef condition, StatementsRef statements);
443 //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
444 //void addBranch(CaseBranchRef branch);
445 //void addBranches(CaseBranchesRef branches);
446 bool isPolyphonic() const;
447 };
448 typedef Ref<SelectCase,Node> SelectCaseRef;
449
450 class While : public Statement {
451 IntExprRef m_condition;
452 StatementsRef m_statements;
453 public:
454 While(IntExprRef condition, StatementsRef statements) :
455 m_condition(condition), m_statements(statements) {}
456 StmtType_t statementType() const { return STMT_LOOP; }
457 void dump(int level = 0);
458 bool evalLoopStartCondition();
459 Statements* statements() const;
460 bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
461 };
462
463 class Neg : public IntExpr {
464 IntExprRef expr;
465 public:
466 Neg(IntExprRef expr) : expr(expr) { }
467 int evalInt() { return (expr) ? -expr->evalInt() : 0; }
468 void dump(int level = 0);
469 bool isConstExpr() const { return expr->isConstExpr(); }
470 bool isPolyphonic() const { return expr->isPolyphonic(); }
471 };
472 typedef Ref<Neg,Node> NegRef;
473
474 class ConcatString : public StringExpr {
475 ExpressionRef lhs;
476 ExpressionRef rhs;
477 public:
478 ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}
479 String evalStr();
480 void dump(int level = 0);
481 bool isConstExpr() const;
482 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
483 };
484 typedef Ref<ConcatString,Node> ConcatStringRef;
485
486 class Relation : public IntExpr {
487 public:
488 enum Type {
489 LESS_THAN,
490 GREATER_THAN,
491 LESS_OR_EQUAL,
492 GREATER_OR_EQUAL,
493 EQUAL,
494 NOT_EQUAL
495 };
496 Relation(IntExprRef lhs, Type type, IntExprRef rhs) :
497 lhs(lhs), rhs(rhs), type(type) {}
498 int evalInt();
499 void dump(int level = 0);
500 bool isConstExpr() const;
501 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
502 private:
503 IntExprRef lhs;
504 IntExprRef rhs;
505 Type type;
506 };
507 typedef Ref<Relation,Node> RelationRef;
508
509 class Or : virtual public BinaryOp, virtual public IntExpr {
510 public:
511 Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
512 int evalInt();
513 void dump(int level = 0);
514 };
515 typedef Ref<Or,Node> OrRef;
516
517 class And : virtual public BinaryOp, virtual public IntExpr {
518 public:
519 And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
520 int evalInt();
521 void dump(int level = 0);
522 };
523 typedef Ref<And,Node> AndRef;
524
525 class Not : virtual public IntExpr {
526 IntExprRef expr;
527 public:
528 Not(IntExprRef expr) : expr(expr) {}
529 int evalInt() { return !expr->evalInt(); }
530 void dump(int level = 0);
531 bool isConstExpr() const { return expr->isConstExpr(); }
532 bool isPolyphonic() const { return expr->isPolyphonic(); }
533 };
534 typedef Ref<Not,Node> NotRef;
535
536 class ParserContext : public VMParserContext {
537 public:
538 struct Error {
539 String txt;
540 int line;
541 };
542 typedef Error Warning;
543
544 void* scanner;
545 std::istream* is;
546 std::vector<ParserIssue> vErrors;
547 std::vector<ParserIssue> vWarnings;
548 std::vector<ParserIssue> vIssues;
549
550 std::set<String> builtinPreprocessorConditions;
551 std::set<String> userPreprocessorConditions;
552
553 std::map<String,VariableRef> vartable;
554 int globalIntVarCount;
555 int globalStrVarCount;
556 int polyphonicIntVarCount;
557
558 EventHandlersRef handlers;
559
560 OnInitRef onInit;
561 OnNoteRef onNote;
562 OnReleaseRef onRelease;
563 OnControllerRef onController;
564
565 ArrayList<int>* globalIntMemory;
566 ArrayList<String>* globalStrMemory;
567 int requiredMaxStackSize;
568
569 VMFunctionProvider* functionProvider;
570
571 ExecContext* execContext;
572
573 ParserContext(VMFunctionProvider* parent) :
574 scanner(NULL), is(NULL),
575 globalIntVarCount(0), globalStrVarCount(0), polyphonicIntVarCount(0),
576 globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1),
577 functionProvider(parent), execContext(NULL)
578 {
579 }
580 virtual ~ParserContext();
581 VariableRef globalVar(const String& name);
582 IntVariableRef globalIntVar(const String& name);
583 StringVariableRef globalStrVar(const String& name);
584 VariableRef variableByName(const String& name);
585 void addErr(int line, const char* txt);
586 void addWrn(int line, const char* txt);
587 void createScanner(std::istream* is);
588 void destroyScanner();
589 bool setPreprocessorCondition(const char* name);
590 bool resetPreprocessorCondition(const char* name);
591 bool isPreprocessorConditionSet(const char* name);
592 std::vector<ParserIssue> issues() const OVERRIDE;
593 std::vector<ParserIssue> errors() const OVERRIDE;
594 std::vector<ParserIssue> warnings() const OVERRIDE;
595 VMEventHandler* eventHandler(uint index) OVERRIDE;
596 VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
597 void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
598 void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
599 void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
600 };
601
602 class ExecContext : public VMExecContext {
603 public:
604 struct StackFrame {
605 Statement* statement;
606 int subindex;
607
608 StackFrame() {
609 statement = NULL;
610 subindex = -1;
611 }
612 };
613
614 ArrayList<int> polyphonicIntMemory;
615 VMExecStatus_t status;
616 ArrayList<StackFrame> stack;
617 int stackFrame;
618 int suspendMicroseconds;
619
620 ExecContext() :
621 status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {}
622
623 virtual ~ExecContext() {}
624
625 inline void pushStack(Statement* stmt) {
626 stackFrame++;
627 //printf("pushStack() -> %d\n", stackFrame);
628 if (stackFrame >= stack.size()) return;
629 stack[stackFrame].statement = stmt;
630 stack[stackFrame].subindex = 0;
631 }
632
633 inline void popStack() {
634 stack[stackFrame].statement = NULL;
635 stack[stackFrame].subindex = -1;
636 stackFrame--;
637 //printf("popStack() -> %d\n", stackFrame);
638 }
639
640 inline void reset() {
641 stack[0].statement = NULL;
642 stack[0].subindex = -1;
643 stackFrame = -1;
644 }
645
646 int suspensionTimeMicroseconds() const OVERRIDE {
647 return suspendMicroseconds;
648 }
649 };
650
651 } // namespace LinuxSampler
652
653 #endif // LS_INSTRPARSERTREE_H

  ViewVC Help
Powered by ViewVC