-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ast.h
672 lines (531 loc) · 19.3 KB
/
Ast.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#ifndef AST_H
#define AST_H
#include "all.h"
#include "Value.h"
#include "ProgramElem.h"
#include "SymTabEntry.h"
#include "IntermediateCodeGen.h"
#include "Instruction.h"
class BlockEntry;
//class EFSA;
class EventEntry;
class FunctionEntry;
class LabelEntry;
class OpNode;
//enum class OpCode;
class PatNode;
//enum class PatNode;
class PrimitivePatNode;
class RulezNode;
class SymTabEntry;
class VariableEntry;
class WhileNode;
/*****************************************************************************
Here is the class hierarchy:
ProgramElem
|
AstNode
+--------------------------------------------+----------------+
| | | |
BasePatNode ExprNode RuleNode StmtNode
| | |
| | |
| +---------+----------+-----------+ |
| | | | | |
| RefExprNode OpNode ValueNode InvocationNode |
| |
| +---------------+------+-----+---------+
| | | | |
| ExprStmtNode CompoundStmtNode IfNode WhileNode
|
|
+-+------------------+
| |
| |
PrimitivePatNode PatNode
******************************************************************************/
class AstNode: public ProgramElem {
public:
enum class NodeType {
PAT_NODE,
EXPR_NODE,
REACTION_NODE,
STMT_NODE,
SEQUENCE_NODE,
RULE_NODE
};
public:
AstNode(NodeType nt, int line=0, int column=0, string file="");
AstNode(const AstNode&); // copy constructor
virtual ~AstNode() {};
//virtual AstNode* clone() = 0;
NodeType nodeType() const { return nodeType_;}
virtual const Type* typeCheck() {return NULL;};
virtual void typePrint(ostream& os, int indent=0) const { return; };
virtual void print(ostream& os, int indent=0) const=0;
virtual void codeGen(IntermediateCodeGen *list) { return; };
virtual void renameRV(string prefix) {}; // new names start with given prefix
virtual bool operator==(const AstNode&) const { return false; };
virtual bool operator!=(const AstNode& a) const
{ return !operator==(a); };
private:
NodeType nodeType_;
const AstNode* operator=(const AstNode& other); /* disable asg */
};
inline ostream& operator<<(ostream& os, const AstNode& an) {
an.print(os);
return os;
};
/****************************************************************/
class ExprNode: public AstNode {
public:
enum class ExprNodeType {
REF_EXPR_NODE,
OP_NODE,
VALUE_NODE,
INV_NODE
};
public:
ExprNode(ExprNodeType et, const Value* val=0, int line=0, int column=0,
string file=""); // val is saved, but not copied
ExprNode(const ExprNode&);
virtual ~ExprNode() {};
virtual ExprNode* clone() const=0;
ExprNodeType exprNodeType() const { return exprType_;};
void exprNodeType(ExprNodeType t) { exprType_ = t; };
const Value* value() const { return val_; }
const Type* coercedType() const { return coercedType_; }
void coercedType(const Type* type) { coercedType_ = type; }
virtual int getDestAddrReg(void) { return -1; };
void print(ostream& os, int indent=0) const=0;
const Type* typeCheck() {return NULL;};
void typePrint(ostream& os, int indent=0) const { return; };
void codeGen(IntermediateCodeGen *list);
virtual void args(vector<ExprNode*> * args) { return; };
private:
ExprNodeType exprType_;
const Value *val_; // reference semantics for val_ and coercedType_
const Type* coercedType_;
};
/****************************************************************/
class RefExprNode: public ExprNode {
public:
RefExprNode(string ext, const SymTabEntry* ste=NULL,
int line=0, int column=0, string file="");
RefExprNode(const RefExprNode&);
ExprNode* clone() const { return new RefExprNode(*this); }
~RefExprNode() {};
string ext() const { return ext_;};
void ext(string str) { ext_ = str;};
const SymTabEntry* symTabEntry() const { return sym_;};
void symTabEntry(const SymTabEntry *ste) { sym_ = ste;};
int getDestAddrReg(void) { return destReg_; };
void setDestAddrReg(int destReg) { destReg_ = destReg; };
void print(ostream& os, int indent=0) const;
const Type* typeCheck();
void typePrint(ostream& os, int indent=0) const;
void codeGen(IntermediateCodeGen *list);
private:
string ext_;
const SymTabEntry* sym_;
int destReg_;
};
/****************************************************************/
#define MAX_OP_ARITY 2
class OpNode: public ExprNode {
public:
enum class OpCode {
UMINUS, PLUS, MINUS, MULT, DIV, MOD,
EQ, NE, GT, LT, GE, LE,
AND, OR, NOT,
BITNOT, BITAND, BITOR, BITXOR, SHL, SHR,
ASSIGN, PRINT, INVALID
};
enum class OpPrintType {PREFIX, INFIX, POSTFIX};
struct OpInfo {
OpCode code_;
const char* name_;
int arity_;
int needParen_;
OpPrintType prtType_;
Type::TypeTag argType_[3];
// operators with > 2 args can be supported
// only if types of args k through N are identical, for 1 <= k <= 3,
// and are given by argType[k-1]
Type::TypeTag outType_;
const char *typeConstraints_;
};
public:
static const int VARIABLE = 100;
public:
OpNode(OpCode op, ExprNode *l, ExprNode *r=NULL,
int line=0, int column=0, string file="");
OpNode(const OpNode&);
ExprNode* clone() const { return new OpNode(*this); }
~OpNode() {};
OpCode opCode() const { return opCode_;};
const ExprNode* arg(unsigned int i) const
{ return (i < arg_.size())? arg_[i] : NULL; };
unsigned int arity() const { return arity_; }
void opCode(OpCode a) { opCode_ = a; };
ExprNode* arg(unsigned int i)
{ return (i < arg_.size())? arg_[i] : NULL; };
vector<ExprNode*>* args()
{ return &arg_; }
void args(vector<ExprNode*> * args)
{ arg_ = *args;
arity_ = args->size();
}
void print(ostream& os, int indent=0) const;
const Type* typeCheck();
void typePrint(ostream& os, int indent=0) const;
void codeGen(IntermediateCodeGen *list);
private:
unsigned int arity_;
OpCode opCode_;
vector<ExprNode*> arg_;
};
/****************************************************************/
class ValueNode: public ExprNode {
public:
ValueNode(Value* val=0, int line=0, int column=0, string file="")
: ExprNode(ExprNode::ExprNodeType::VALUE_NODE, val, line,column,file) {
if (val != NULL) type((Type*)(val->type()));
}
ValueNode(const ValueNode& val): ExprNode(val) {};
ExprNode* clone() const { return new ValueNode(*this); }
~ValueNode() {};
void print(ostream& os, int indent=0) const;
const Type* typeCheck();
void typePrint(ostream& os, int indent=0) const;
void codeGen(IntermediateCodeGen *list);
private:
/* val_ field is already included in ExprNode, so no new data members */
};
/****************************************************************/
class InvocationNode: public ExprNode {
// Used to represent function invocation
public:
InvocationNode(const SymTabEntry *ste, vector<ExprNode*>* param=0,
int line=0, int column=0, string file="");
InvocationNode(const InvocationNode&);
ExprNode* clone() const { return new InvocationNode(*this); }
~InvocationNode() {};
const SymTabEntry* symTabEntry() const { return ste_; };
const vector<ExprNode*>* params() const { return params_;};
vector<ExprNode*>* params() { return params_;}
void params(vector<ExprNode*>* args){ params_ = args;};
const ExprNode* param(unsigned int i) const
{ return (params_ != NULL && i < params_->size())? (const ExprNode*)((*params_)[i]) : NULL; };
ExprNode* param(unsigned int i)
{ return (ExprNode*)((const InvocationNode*)this->param(i));}
void param(ExprNode* arg, unsigned int i)
{ if (params_ != NULL && i < params_->size()) (*params_)[i] = arg;};
void print(ostream& os, int indent=0) const;
const Type* typeCheck();
void typePrint(ostream& os, int indent=0) const;
void codeGen(IntermediateCodeGen *list);
private:
vector<ExprNode*>* params_;
const SymTabEntry *ste_; // reference semantics
};
/****************************************************************/
// There are 3 kinds of PatNodes:
// PrimitivePatNodes are of the form: event|cond
// AtomicPatNodes consist of PrimitivePatNodes with one or more "||"
// operators, plus an optional negation symbol
// PatNodes consist of PatNodes or AtomicPatNodes composed with
// ".", "||" and "*" operators
// We have a single base class for pattern nodes called BasePatNode. In
// addition, the functionality of Atomic and PatNodes have been
// combined into a single class PatNode.
class BasePatNode: public AstNode {
public:
enum class PatNodeKind {PRIMITIVE, EMPTY, NEG, SEQ, OR, STAR, UNDEFINED};
public:
BasePatNode(PatNodeKind pk, int ln=0, int col=0, string f=""):
AstNode(AstNode::NodeType::PAT_NODE, ln, col, f) {
parent_ = NULL; root_ = NULL; patKind_ = pk;};
BasePatNode(const BasePatNode& bpn): AstNode(bpn) {
patKind_ = bpn.patKind_; parent_ = bpn.parent_; root_ = bpn.root_;}
~BasePatNode() {};
//virtual BasepatNode* clone() const { return new BasePatNode(*this);}
PatNodeKind kind() const { return patKind_; };
void kind(PatNodeKind p) {patKind_ = p;}
const BasePatNode* parent() const { return parent_; }
BasePatNode* parent() { return parent_;}
virtual bool hasSeqOps() const=0;
virtual bool hasNeg() const=0;
virtual bool hasAnyOrOther() const=0;
virtual bool isNegatable() const {
return ((!hasSeqOps()) && (!hasNeg())); }
virtual void codeGen(IntermediateCodeGen *list)=0;
private:
PatNodeKind patKind_;
BasePatNode* parent_;
BasePatNode* root_;
};
/****************************************************************/
class PrimitivePatNode: public BasePatNode {
public:
PrimitivePatNode(EventEntry* ee, vector<VariableEntry*>* params,
ExprNode* c=NULL,
int line=0, int column=0, string file="");
//PrimitivePatNode(const PrimitivePatNode& ppn);
~PrimitivePatNode() {};
//BasePatNode* clone() { return new PrimitivePatNode(*this); }
const EventEntry* event() const { return ee_; }
EventEntry* event() { return ee_; }
const vector<const VariableEntry*>* params() const {
return (vector<const VariableEntry*>*)params_; }
vector<VariableEntry*>* params() { return params_; }
const ExprNode* cond() const { return cond_; }
ExprNode* cond() { return cond_; }
void cond(ExprNode* c) { cond_ = c;}
ExprNode* condition() { return condition_; }
const ExprNode* condition() const { return condition_; }
const list<const OpNode*>& asgs() const {
return (list<const OpNode*>&)asgs_; }
list<OpNode*>& asgs() { return asgs_; }
bool hasSeqOps() const;
bool hasNeg() const;
bool hasAnyOrOther() const;
const Type* typeCheck();
void print(ostream& os, int indent=0) const;
void typePrint(ostream& os, int indent) const;
void codeGen(IntermediateCodeGen *list);
private:
EventEntry* ee_;
vector<VariableEntry*>* params_;
/* cond_ may contain assignments as well as other expressions */
/* condition_ contains all expresions in cond_ other than assignments */
ExprNode* cond_;
ExprNode* condition_;
list<OpNode*> asgs_;
};
/****************************************************************/
class PatNode: public BasePatNode {
public:
PatNode(int line=0, int column=0, string file="");
PatNode(PatNodeKind pk, BasePatNode *p1, BasePatNode*p2=NULL, int line=0, int column=0, string file="");
~PatNode() {};
//AstNode* clone()
// { return new PatNode(*this); }
const BasePatNode* pat1() const { return pat1_; }
BasePatNode* pat1() { return pat1_; }
const BasePatNode* pat2() const { return pat2_; }
BasePatNode* pat2() { return pat2_; }
bool hasNeg() const;
bool hasSeqOps() const;
bool hasAnyOrOther() const;
void print(ostream& os, int indent=0) const;
void typePrint(ostream& os, int indent) const;
const Type* typeCheck();
void codeGen(IntermediateCodeGen *list);
private:
PatNode(const PatNode&);
BasePatNode *pat1_;
BasePatNode *pat2_;
};
/****************************************************************/
class StmtNode: public AstNode {
public:
enum class StmtNodeKind { ILLEGAL=-1, EXPR, IF, COMPOUND, RETURN, WHILE, BREAK};
public:
StmtNode(StmtNodeKind skm, int line=0, int column=0, string file=""):
AstNode(AstNode::NodeType::STMT_NODE, line,column,file) { skind_ = skm; };
~StmtNode() {};
//AstNode* clone()
// { return new StmtNode(*this); }
const Type* typeCheck() {return NULL;}
StmtNodeKind stmtNodeKind() const { return skind_;}
void print(ostream& os, int indent) const = 0;
void typePrint(ostream& os, int indent) const = 0;
virtual void codeGen(IntermediateCodeGen *list)=0;
private:
StmtNodeKind skind_;
};
/****************************************************************/
class ReturnStmtNode: public StmtNode {
public:
ReturnStmtNode(ExprNode *e, FunctionEntry* fe,
int line=0, int column=0, string file=""):
StmtNode(StmtNode::StmtNodeKind::RETURN,line,column,file) { expr_ = e; fun_ = fe;};
~ReturnStmtNode() {};
const Type* typeCheck();
void print(ostream& os, int indent) const {
os << "return ";
if(expr_ != NULL) expr_->print(os, indent); else os << "NULL";
};
void typePrint(ostream& os, int indent) const {
os << "return ";
if(expr_ != NULL) {
if (expr_->coercedType()) {
os << "(";
expr_->coercedType()->print(os, indent);
os << ")";
}
expr_->typePrint(os, indent);
}
else os << "NULL";
};
void codeGen(IntermediateCodeGen *list);
private:
ExprNode* expr_;
FunctionEntry* fun_;
};
class BreakStmtNode: public StmtNode {
public:
BreakStmtNode(ExprNode *e, WhileNode *wsn, int line=0, int column=0, string file=""):
StmtNode(StmtNode::StmtNodeKind::BREAK,line,column,file) { expr_ = e;};
~BreakStmtNode() {};
/* TODO: Need to check specifications for break statements carefully and update the code.
Currently break simply return int in default case */
const Type* typeCheck();
void print(ostream& os, int indent) const {
os << "break";
if(expr_ != NULL) expr_->print(os, indent); else os <<";";
}
void whileStmt (WhileNode *wsn) {
wsn_ = wsn;
};
WhileNode * whileStmt(void) { return wsn_; };
void typePrint(ostream& os, int indent) const {
os << "break ";
if(expr_ != NULL) {
if (expr_->coercedType()) {
os << "(";
expr_->coercedType()->print(os, indent);
os << ")";
}
expr_->typePrint(os, indent);
}
else os << "(int);";
}
void codeGen(IntermediateCodeGen *list);
private:
ExprNode* expr_;
WhileNode* wsn_;
};
/****************************************************************/
class ExprStmtNode: public StmtNode {
public:
ExprStmtNode(ExprNode* e, int line=0, int column=0, string file=""):
StmtNode(StmtNode::StmtNodeKind::EXPR,line,column,file) { expr_ = e; };
~ExprStmtNode() {};
//AstNode* clone()
// { return new ExprStmtNode(*this); }
const Type* typeCheck();
void print(ostream& os, int indent) const {
if (expr_ != NULL) { expr_->print(os, indent); }
};
void typePrint(ostream& os, int indent) const {
if (expr_ != NULL) {
if (expr_->coercedType()) {
os << "(";
expr_->coercedType()->print(os, indent);
os << ")";
}
expr_->typePrint(os, indent);
}
};
void codeGen(IntermediateCodeGen *list);
private:
ExprNode* expr_;
};
/****************************************************************/
class CompoundStmtNode: public StmtNode{
public:
CompoundStmtNode(list<StmtNode*> *Slist, int ln=0, int col=0, string fl=""):
StmtNode(StmtNode::StmtNodeKind::COMPOUND, ln,col,fl) { stmts_ = Slist;};
~CompoundStmtNode() { };
//AstNode* clone()
// { return new CompoundStmtNode(*this); }
const list<StmtNode*>* stmts() const { return stmts_;}
list<StmtNode*>* stmts() { return stmts_;}
void add(StmtNode *s)
{ if(stmts_ != NULL) stmts_->push_back(s); };
void printWithoutBraces(ostream& os, int indent) const;
void print(ostream& os, int indent) const;
const Type* typeCheck();
void typePrintWithoutBraces(ostream& os, int indent) const;
void typePrint(ostream& os, int indent) const;
void codeGen(IntermediateCodeGen *list);
private:
CompoundStmtNode(const CompoundStmtNode&);
list<StmtNode*> *stmts_;
};
/****************************************************************/
class IfNode: public StmtNode{
public:
IfNode(ExprNode* cond, StmtNode* thenStmt,
StmtNode* elseStmt=NULL, int line=0, int column=0, string file="");
~IfNode(){};
//AstNode* clone()
// { return new IfNode(*this); }
const Type* typeCheck();
const ExprNode* cond() const {return cond_;}
const StmtNode* elseStmt() const { return else_;};
const StmtNode* thenStmt() const { return then_;};
ExprNode* cond() {return cond_;}
StmtNode* elseStmt() { return else_;};
StmtNode* thenStmt() { return then_;};
void print(ostream& os, int indent) const;
void typePrint(ostream& os, int indent) const;
void codeGen(IntermediateCodeGen *list);
private:
ExprNode *cond_;
StmtNode *then_, *else_;
IfNode(const IfNode&);
};
/****************************************************************/
class WhileNode: public StmtNode{
public:
WhileNode(ExprNode* cond, StmtNode* doStmt, int line=0, int column=0, string file="");
~WhileNode(){};
const Type* typeCheck();
const ExprNode* cond() const {return cond_;}
const StmtNode* doStmt() const { return do_;};
ExprNode* cond() {return cond_;}
StmtNode* doStmt() { return do_;};
void print(ostream& os, int indent) const;
void typePrint(ostream& os, int indent) const;
void codeGen(IntermediateCodeGen *list);
void exit_label(int label) {
label_ = label;
};
int exit_label(void) {
return label_;
}
private:
ExprNode *cond_;
StmtNode *do_;
int label_;
WhileNode(const WhileNode&);
};
/****************************************************************/
class RuleNode: public AstNode {
public:
RuleNode(BlockEntry *re, BasePatNode* pat, StmtNode* reaction,
int line=0, int column=0, string file="");
~RuleNode() {};
//AstNode* clone()
// { return new RuleNode(*this); }
const BlockEntry* ruleEntry() const { return rste_; };
BlockEntry* ruleEntry() { return rste_; };
const BasePatNode* pat() const { return pat_; };
BasePatNode* pat() { return pat_; };
const StmtNode* reaction() const { return reaction_; };
StmtNode* reaction() { return reaction_; };
void print(ostream& os, int indent=0) const;
const Type* typeCheck();
void typePrint(ostream& os, int indent) const;
void codeGen(IntermediateCodeGen *list);
private:
BlockEntry *rste_;
BasePatNode *pat_;
StmtNode *reaction_;
RuleNode(const RuleNode&);
};
/****************************************************************/
#endif