-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIR.h
1347 lines (974 loc) · 39.2 KB
/
IR.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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "Semantikin.h"
#include "AbstractSyntaxTree.h"
#include "IRVisitors.h"
#include <string>
#include <list>
#include <memory>
#include <sstream>
#include <iomanip>
#include <map>
#include <iterator>
#include <cstddef>
using std::stringstream;
using std::string;
using std::list;
using std::map;
using std::shared_ptr;
using std::make_shared;
using namespace Parser;
namespace IR {
bool isADefinition(Instruction_sptr instr);
STEntry_sptr whatIsDefined(Instruction_sptr instr);
void getTgtSTEntry(const Instruction_sptr& instr, STEntry_set_sptr& res) ;
/* This class is the parent of all IR instructions. */
class Instruction : public std::enable_shared_from_this<Instruction> {
protected:
// This is only used when dumping the IR to a visual format
string _fillColor = "ffffff";
unsigned int _myColor = 0;
Instruction_sptr _tgt = nullptr;
Instruction_sptr _chd2 = nullptr;
Instruction_sptr _chd3 = nullptr;
/// This will point to an instance of MachineInstruction once the
/// instruction selection pass is executed.
MachineInstruction_sptr _macInstr = nullptr;
/* Pointer to next/prev operation that should be performed */
Instruction* _next = nullptr;
Instruction* _prev = nullptr;
BasicBlock_sptr _parent = nullptr;
public:
Instruction()
{ }
Instruction(Instruction_sptr tgt, Instruction_sptr chd2=nullptr, Instruction_sptr chd3=nullptr) :
_tgt(tgt), _chd2(chd2), _chd3(chd3), _next(nullptr)
{ }
// Set/get the color of this IR node. Used for graphical visualization of the IR tree
void myColor(unsigned int mc) { this->_myColor = mc; }
unsigned int myColor() { return this->_myColor; }
void fillColor(const string& fc) { this->_fillColor = fc; }
const string& fillColor() { return this->_fillColor; }
// Used to Get/set the operands of the instruction
void tgt(Instruction_sptr chd) { this->_tgt = chd; }
Instruction_sptr tgt() { return this->_tgt; }
void chd2(Instruction_sptr chd) { this->_chd2 = chd; }
Instruction_sptr chd2() { return this->_chd2; }
void chd3(Instruction_sptr chd) { this->_chd3 = chd; }
Instruction_sptr chd3() { return this->_chd3; }
virtual void next(Instruction* nxt) { this->_next = nxt; }
virtual Instruction* next() { return this->_next; }
void prev(Instruction* prv) { this->_prev = prv; }
Instruction* prev() { return this->_prev; }
void parent(BasicBlock_sptr parent) { this->_parent = parent; }
BasicBlock_sptr parent() { return this->_parent; }
/// Set the MachineInstruction that implement the this IR instruction
void macInstr(MachineInstruction_sptr macInstr) { this->_macInstr = macInstr; };
MachineInstruction_sptr macInstr(void) { return this->_macInstr; };
// Returns a set of symbol table entries that are used by this instruction
virtual STEntry_set_sptr uses() { return make_shared<STEntry_set>(); }
// Returns a set (usually unitary) of symbol table entries that are defined by this instruction
virtual STEntry_set_sptr defs() { return make_shared<STEntry_set>(); }
virtual bool isADefinition() { return true; }
virtual const shared_ptr<vector<STEntry_sptr>> arguments() { return nullptr; }
virtual void addArgument(STEntry_sptr argument) { }
/** Used to traverse the IR tree. */
virtual void accept(IRTreeVisitor* visitor) = 0;
/** Used to dump in a "human readable" way the instruction's target operand */
virtual string tgtDataName() = 0;
virtual void dump(stringstream& buffer) = 0;
virtual ~Instruction() {};
};
/* Base class for all instructions that change control flow. */
class BranchInstruction : public Instruction {
protected:
BasicBlock_sptr _lbl1;
BasicBlock_sptr _lbl2;
public:
BranchInstruction(BasicBlock_sptr lbl) : Instruction(nullptr, nullptr, nullptr), _lbl1(lbl), _lbl2(nullptr)
{ }
BranchInstruction(Instruction_sptr exp, BasicBlock_sptr lbl) : Instruction(exp, nullptr, nullptr), _lbl1(lbl), _lbl2(nullptr)
{ }
BranchInstruction(Instruction_sptr exp, BasicBlock_sptr lbl1, BasicBlock_sptr lbl2) : Instruction(exp, nullptr, nullptr), _lbl1(lbl1), _lbl2(lbl2)
{ }
BasicBlock_sptr lbl1() { return this->_lbl1; }
void lbl1(BasicBlock_sptr lbl) { this->_lbl1 = lbl; }
BasicBlock_sptr lbl2() { return this->_lbl2; }
void lbl2(BasicBlock_sptr lbl) { this->_lbl2 = lbl; }
bool isADefinition() { return false; }
};
class InstructionIterator : public std::iterator<std::bidirectional_iterator_tag, IR::Instruction> {
private:
Instruction* _current = nullptr;
public:
InstructionIterator(Instruction* head) : _current(head) {
}
InstructionIterator operator=(const InstructionIterator& other) {
return this->_current = other.current();
}
Instruction* current() const {
return this->_current;
}
InstructionIterator& operator++() {
this->_current = this->current()->next();
return *this;
}
InstructionIterator operator++(int) {
InstructionIterator clone(*this);
this->_current = this->current()->next();
return clone;
}
bool operator==(const InstructionIterator& other) {
return this->current() == other.current();
}
bool operator!=(const InstructionIterator& other) {
return !(*this == other);
}
Instruction& operator*() {
return *this->current();
}
Instruction& operator->() {
return *this->current();
}
};
class InstrReverseIterator : public std::iterator<std::bidirectional_iterator_tag, IR::Instruction> {
private:
Instruction* _current = nullptr;
public:
InstrReverseIterator(Instruction* head) : _current(head) {
}
InstrReverseIterator operator=(const InstrReverseIterator& other) {
return this->_current = other.current();
}
Instruction* current() const {
return this->_current;
}
InstrReverseIterator& operator++() {
this->_current = this->current()->prev();
return *this;
}
InstrReverseIterator operator++(int) {
InstrReverseIterator clone(*this);
this->_current = this->current()->prev();
return clone;
}
bool operator==(const InstrReverseIterator& other) {
return this->current() == other.current();
}
bool operator!=(const InstrReverseIterator& other) {
return !(*this == other);
}
Instruction& operator*() {
return *this->current();
}
Instruction& operator->() {
return *this->current();
}
};
class InstructionSequence {
private:
Instruction* _head;
Instruction* _tail;
public:
typedef InstructionIterator iterator;
typedef InstrReverseIterator reverse_iterator;
typedef ptrdiff_t difference_type;
typedef size_t size_type;
typedef Instruction value_type;
typedef Instruction* pointer;
typedef Instruction& reference;
InstructionSequence(Instruction* head, Instruction* tail) : _head(head), _tail(tail) { }
iterator begin() { return iterator(this->_head); }
reverse_iterator rbegin() { return reverse_iterator(this->_tail); }
iterator end() { return iterator(nullptr); }
reverse_iterator rend() { return reverse_iterator(nullptr); }
};
/** Well, this represents a basic block =) */
class BasicBlock : public std::enable_shared_from_this<BasicBlock> {
private:
int _id;
int _usageCounter;
Instruction_list_sptr _subtrees;
Instruction* _firstInstruction;
Instruction* _lastInstruction;
BasicBlock_list_sptr _preds;
BasicBlock_list_sptr _succs;
public:
BasicBlock(int id) :
_id(id),
_usageCounter(0),
_subtrees( make_shared<Instruction_list>() ),
_firstInstruction( nullptr ),
_lastInstruction( nullptr ),
_preds( make_shared<BasicBlock_list>() ),
_succs( make_shared<BasicBlock_list>() )
{
}
int id() const { return _id; }
void id(int id) { _id = id; }
int& usageCounter() { return _usageCounter; }
void usageCounter(int uc) { _usageCounter = uc; }
Instruction_list_sptr subtrees() const { return _subtrees; }
// This is updated here and not in "appendInstruction" because the order of
// appended instruction may not necessarily be the order of execution computed
// by the tree canonicalizer.
void firstInstruction(Instruction* first) { this->_firstInstruction = first; }
Instruction* firstInstruction() { return this->_firstInstruction; }
void lastInstruction(Instruction* last) { this->_lastInstruction = last; }
Instruction* lastInstruction() { return this->_lastInstruction; }
InstructionSequence instructions() {
return InstructionSequence(this->_firstInstruction, this->_lastInstruction);
}
BasicBlock_list_sptr succs() {
return this->_succs;
}
BasicBlock_list_sptr preds() {
return this->_preds;
}
void addPredecessor(BasicBlock_sptr pred) {
this->_preds->push_back(pred);
}
void appendInstruction(Instruction_sptr instr) {
/// Instructions can reference their parent (i.e., the basic block they are in)
/// using this member field.
instr->parent( this->shared_from_this() );
/// Add the instruction to the BB's list of instructions
this->_subtrees->push_back(instr);
/// Lets check if the current instruction is a branch, i.e., ends the
/// current basic block
auto branch = std::dynamic_pointer_cast<IR::BranchInstruction>(instr);
// If the instruction is a branch we make some checkings and add
// edges of the CFG.
if (branch != nullptr) {
if (this->succs() != nullptr && this->succs()->size() > 0) {
cout << "Error: appending a *second* branch instruction in a basic block." << endl;
exit(-1);
}
// all kinds of branches have the first label
this->succs()->push_back(branch->lbl1());
// the target basic block receive a predecessor
branch->lbl1()->addPredecessor(this->shared_from_this());
// When the branch is a goto or a ret it does not have the second label
// But when it is a Conditional Branch it has
if (branch->lbl2() != nullptr) {
this->succs()->push_back(branch->lbl2());
branch->lbl2()->addPredecessor(this->shared_from_this());
}
}
this->_lastInstruction = instr.get();
}
/// Insert instruction <instr> just before the instruction <nextInstr>.
void insertInstruction(Instruction_sptr newInstr, Instruction_sptr nextInstr) {
assert(newInstr->parent() != this->shared_from_this() && "Trying to insert re-insert an instruction into a basic block.");
assert(newInstr->parent() == nullptr && "Trying to insert an instruction into a basic block A that is already member of another basic block 'B'.");
assert((newInstr->prev() == nullptr && newInstr->next() == nullptr) && "Trying to insert an instruction that already has prev/next pointers set.");
assert((std::dynamic_pointer_cast<IR::BranchInstruction>(newInstr) == nullptr) && "Trying to insert a branch instruction in a basic block. Not supported yet." );
/// Instructions can reference their parent (i.e., the basic block they are in)
/// using this member field.
newInstr->parent( this->shared_from_this() );
Instruction_sptr prev = nullptr;
for (auto it = _subtrees->begin(), end = _subtrees->end(); it != end; it++) {
/// Which linking should I respect here? The next and prev pointers or
/// just the order of instructions in the _subtrees. Sadly I don't recall now
/// why I added this prev-next pointers.
if (*it == nextInstr) {
this->_subtrees->insert(it, newInstr);
if (prev != nullptr) {
newInstr->next( prev->next() );
newInstr->prev( prev.get() );
prev->next( newInstr.get() );
}
nextInstr->prev( newInstr.get() );
break;
}
prev = *it;
}
}
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dumpToDot(stringstream& buffer);
};
/* Represent function definitions. */
class Function {
private:
/* Pointer to the symbol table entry describing this function. */
STEntry_sptr _addr;
/* Pointer to this function's symbol table. */
shared_ptr<SymbolTable> _symbTable;
/* Keep track of how many times each name was used inside the
* same function so we can rename them. */
map<string, int> nameVersions;
/* The set of basic blocks composing this function */
BasicBlock_list_sptr _bbs;
/* Points to the current basic block being constructed. Used *only* during
* IR construction */
BasicBlock_sptr _currentBasicBlock;
/* This points to the function's entry basic block. */
BasicBlock_sptr _entryBasicBlock;
BasicBlock_list_sptr topologicalSort();
public:
// This will be used to specify the order in which a client wants to
// traverse the nodes of the CFG.
enum class CFGBasicBlockOrder {
TOPO_ORDER,
REVERSE_TOPO_ORDER
};
Function(shared_ptr<SymbolTable> st) :
_symbTable(st),
_bbs(nullptr)
{
this->_bbs = make_shared< list<shared_ptr<BasicBlock>> >();
this->_currentBasicBlock = make_shared<BasicBlock>(0);
this->_entryBasicBlock = this->_currentBasicBlock;
this->_bbs->push_back( this->_currentBasicBlock );
}
string name() { return dynamic_cast<STFunctionDeclaration*>(this->_addr.get())->getLabel(); }
// No order specified. Just return the order of the internal structure.
BasicBlock_list_sptr bbs() {
return this->_bbs;
};
BasicBlock_list_sptr bbs(CFGBasicBlockOrder order) {
if (order == CFGBasicBlockOrder::TOPO_ORDER) {
return topologicalSort();
}
else {
cout << "CRITICAL: Invalid CFG node order specified." << endl;
exit(1);
}
}
/* Methods related to setting/getting the function's declaration. */
void addr(STEntry_sptr addr) { this->_addr = addr; }
STEntry_sptr addr() { return this->_addr; }
/* Methods related to IR construction. */
void appendBasicBlock(BasicBlock_sptr bb);
void appendInstruction(shared_ptr<IR::Instruction> instr);
/* Symbol Table Management Methods. */
void symbolTable(shared_ptr<SymbolTable> st) { this->_symbTable = st; }
shared_ptr<SymbolTable> symbolTable() { return this->_symbTable; }
void addSymbolTable(shared_ptr<SymbolTable> st);
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
/* Mostly debug related methods. */
void dump(stringstream& output) {
output << endl << "Code for function: " << this->name() << endl;
for (auto& bb : *this->bbs()) {
output << "BB" << bb->id() << ": " << endl;
for (auto& instr : bb->instructions()) {
instr.dump(output);
}
output << endl;
}
}
};
/* Represent a whole compilation unit. */
class Module {
private:
shared_ptr<list<shared_ptr<Function>>> _functions;
shared_ptr<SymbolTable> _symbTable;
public:
Module() : _functions(shared_ptr<list<shared_ptr<Function>>>(new list<shared_ptr<Function>>())) { }
void addFunction(shared_ptr<Function> fun) { this->_functions->push_back(fun); }
Function_sptr_list_sptr functions() const { return this->_functions; }
void symbolTable(shared_ptr<SymbolTable> st) { this->_symbTable = st; }
shared_ptr<SymbolTable> symbolTable() { return this->_symbTable; }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump() {
std::stringstream buffer;
buffer << "This is the IR: " << endl;
buffer << std::setfill('-') << std::setw(80) << "-" << endl;
for (auto function : *this->_functions) {
function->dump(buffer);
buffer << endl << endl;
}
cout << buffer.str();
}
};
/********************************************************/
/********************************************************/
/********************************************************/
/********************************************************/
/* Parent class of all instructions that are actually
* placeholder for data. Like immediates, registers and
* memory addresses.
*/
class Data : public Instruction {
public:
void next(Instruction* nxt) {
cout << "IR Error: Operands should not have next ptrs." << endl;
}
Instruction* next() {
cout << "IR Error: Operands do not have next ptrs." << endl;
return this->_next;
}
virtual STEntry_sptr value() = 0;
virtual STEntry_set_sptr uses() { auto tmp = make_shared<STEntry_set>(); tmp->insert(this->value()); return tmp; }
bool isADefinition() { return false; }
};
class Immediate : public Data {
private:
STConstantDef_sptr _value;
public:
Immediate(STEntry_sptr val) : _value( std::dynamic_pointer_cast<STConstantDef>(val) )
{ }
Immediate(STConstantDef_sptr val) : _value(val)
{ }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
STEntry_sptr value() { return this->_value; }
string tgtDataName() { return Util::escapeStr(this->_value->getName()); }
void dump(stringstream& buffer) {
buffer << "Imm;" << endl;
}
};
class Register : public Data {
private:
STRegister_sptr _value;
public:
Register(STEntry_sptr val) : _value( std::dynamic_pointer_cast<STRegister>(val) )
{ }
Register(STRegister_sptr val) : _value(val)
{ }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
STEntry_sptr value() { return this->_value; }
string tgtDataName() { return this->_value->getName(); }
void dump(stringstream& buffer) { buffer << "Reg;" << endl; }
};
class Memory : public Data {
private:
STVariableDeclaration_sptr _value;
public:
Memory(STEntry_sptr val) : _value( std::dynamic_pointer_cast<STVariableDeclaration>(val) )
{ }
Memory(STVariableDeclaration_sptr val) : _value(val)
{ }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
STEntry_sptr value() { return this->_value; }
string tgtDataName() { return this->_value->getName(); }
void dump(stringstream& buffer) {
buffer << "Mem;" << endl;
}
};
// Represents the label of a function
class Func : public Data {
private:
STFunctionDecl_sptr _value;
public:
Func(STEntry_sptr val) : _value( std::dynamic_pointer_cast<STFunctionDeclaration>(val) )
{ }
Func(STFunctionDecl_sptr val) : _value(val)
{ }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
STEntry_sptr value() { return this->_value; }
string tgtDataName() { return this->_value->getName(); }
void dump(stringstream& buffer) {
buffer << "Func;" << endl;
}
};
/********************************************************/
/********************************************************/
/********************************************************/
/********************************************************/
/* Parent class of all data movement instructions. */
class Copy : public Instruction {
public:
Copy(Instruction_sptr tgt, Instruction_sptr value) : Instruction(tgt, value, nullptr) {
assert(tgt != nullptr && value != nullptr && "A parameter to IR::Copy is null.\n");
}
STEntry_set_sptr uses() {
auto tmp = make_shared<STEntry_set>();
getTgtSTEntry(this->chd2(), tmp);
return tmp;
}
STEntry_set_sptr defs() {
auto tmp = make_shared<STEntry_set>();
tmp->insert( std::dynamic_pointer_cast<Data>(this->tgt())->value());
return tmp;
}
};
class ScalarCopy : public Copy {
public:
using Copy::Copy;
/** Used to obtain the "human readable" name of the destination operand of the operation. */
string tgtDataName() { return this->_tgt->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << ";" << endl;
}
};
class CopyFromArray : public Copy {
using Copy::Copy;
/** Used to obtain the "human readable" name of the destination operand of the operation. */
string tgtDataName() { return this->_tgt->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
STEntry_set_sptr defs() {
auto tmp = make_shared<STEntry_set>();
getTgtSTEntry(this->tgt(), tmp);
return tmp;
}
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = *" << this->_chd2->tgtDataName() << ";" << endl;
}
};
class CopyToArray : public Copy {
using Copy::Copy;
/** Used to obtain the "human readable" name of the destination operand of the operation. */
string tgtDataName() { return this->_tgt->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
STEntry_set_sptr defs() {
auto tmp = make_shared<STEntry_set>();
getTgtSTEntry(this->tgt(), tmp);
return tmp;
}
void dump(stringstream& buffer) {
buffer << "*" << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << ";" << endl;
}
};
/********************************************************/
/********************************************************/
/********************************************************/
/********************************************************/
/* Parent class of all integer arithmetic instructions. */
class IntegerArithmetic : public Instruction {
public:
IntegerArithmetic(Instruction_sptr tgt, Instruction_sptr chd2, Instruction_sptr chd3) : Instruction(tgt, chd2, chd3)
{ }
STEntry_set_sptr defs() {
auto tmp = make_shared<STEntry_set>();
tmp->insert( std::dynamic_pointer_cast<Data>(this->tgt())->value());
return tmp;
}
};
class BinaryIntegerArithmetic : public IntegerArithmetic {
public:
BinaryIntegerArithmetic(Instruction_sptr tgt, Instruction_sptr chd2, Instruction_sptr chd3) : IntegerArithmetic(tgt, chd2, chd3)
{ }
STEntry_set_sptr uses() {
auto tmp = make_shared<STEntry_set>();
getTgtSTEntry(this->chd2(), tmp);
getTgtSTEntry(this->chd3(), tmp);
return tmp;
}
};
class UnaryIntegerArithmetic : public IntegerArithmetic {
public:
UnaryIntegerArithmetic(Instruction_sptr tgt, Instruction_sptr chd2) : IntegerArithmetic(tgt, chd2, nullptr) { }
STEntry_set_sptr uses() {
auto tmp = make_shared<STEntry_set>();
getTgtSTEntry(this->chd2(), tmp);
return tmp;
}
};
class IAdd : public BinaryIntegerArithmetic {
public:
using BinaryIntegerArithmetic::BinaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << " + " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class ISub : public BinaryIntegerArithmetic {
public:
using BinaryIntegerArithmetic::BinaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << " - " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class IMul : public BinaryIntegerArithmetic {
public:
using BinaryIntegerArithmetic::BinaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << " * " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class IDiv : public BinaryIntegerArithmetic {
public:
using BinaryIntegerArithmetic::BinaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << " / " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class IMod : public BinaryIntegerArithmetic {
public:
using BinaryIntegerArithmetic::BinaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << " % " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class IMinus : public UnaryIntegerArithmetic {
public:
using UnaryIntegerArithmetic::UnaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = -" << this->_chd2->tgtDataName() << ";" << endl;
}
};
class IInc : public UnaryIntegerArithmetic {
public:
using UnaryIntegerArithmetic::UnaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << " + 1;" << endl;
}
};
class IDec : public UnaryIntegerArithmetic {
public:
using UnaryIntegerArithmetic::UnaryIntegerArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->_tgt->tgtDataName() << " = " << this->_chd2->tgtDataName() << " - 1;" << endl;
}
};
/********************************************************/
/********************************************************/
/********************************************************/
/********************************************************/
/* Parent class of all binary arithmetic instructions. */
class BitArithmetic : public Instruction {
public:
BitArithmetic(Instruction_sptr tgt, Instruction_sptr chd2, Instruction_sptr chd3) : Instruction(tgt, chd2, chd3)
{ }
STEntry_set_sptr uses() {
auto tmp = make_shared<STEntry_set>();
getTgtSTEntry(this->chd2(), tmp);
getTgtSTEntry(this->chd3(), tmp);
return tmp;
}
STEntry_set_sptr defs() {
auto tmp = make_shared<STEntry_set>();
tmp->insert( std::dynamic_pointer_cast<Data>(this->tgt())->value());
return tmp;
}
};
class BinAnd : public BitArithmetic {
public:
using BitArithmetic::BitArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->tgt()->tgtDataName() << " = " << this->chd2()->tgtDataName() << " & " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class BinOr : public BitArithmetic {
public:
using BitArithmetic::BitArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->tgt()->tgtDataName() << " = " << this->chd2()->tgtDataName() << " | " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class BinXor : public BitArithmetic {
public:
using BitArithmetic::BitArithmetic;
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }
/* Used to traverse the IR tree. */
void accept(IRTreeVisitor* visitor) { visitor->visit(this); }
void dump(stringstream& buffer) {
buffer << this->tgt()->tgtDataName() << " = " << this->chd2()->tgtDataName() << " ^ " << this->chd3()->tgtDataName() << ";" << endl;
}
};
class BinNot : public BitArithmetic {
public:
BinNot(Instruction_sptr tgt, Instruction_sptr chd2) : BitArithmetic(tgt, chd2, nullptr)
{ }
/** Used to dump in a "human readable" way the instruction's target operand */
string tgtDataName() { return this->tgt()->tgtDataName(); }